1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE ConstraintKinds #-}
3 {-# LANGUAGE InstanceSigs #-}
4 {-# LANGUAGE QuantifiedConstraints #-}
5 {-# LANGUAGE RankNTypes #-}
6 {-# LANGUAGE UndecidableInstances #-}
7 {-# OPTIONS_GHC -Wno-partial-fields #-}
9 module Symantic.Compiler.Term where
11 import Data.Function qualified as Fun
12 import Data.Monoid (Monoid)
13 import GHC.Types (Type)
14 import Symantic.Semantics.Eval (Eval (..))
15 import Symantic.Semantics.Forall
16 import Symantic.Syntaxes.Classes (Instantiable (..), Syntax, Syntaxes, Unabstractable (..))
17 import Symantic.Syntaxes.Classes qualified as Sym
18 import Type.Reflection (Typeable)
19 import Unsafe.Coerce (unsafeCoerce)
21 import Symantic.Typer.Type (Ty, monoTy)
23 -- * Class 'AbstractableTy'
24 class AbstractableTy ty sem where
25 -- | Lambda term abstraction, in HOAS (Higher-Order Abstract Syntax) style.
26 lamTy :: ty a -> (sem a -> sem b) -> sem (a -> b)
31 AbstractableTy (Ty prov '[]) sem =>
35 fun = lamTy (monoTy @_ @prov)
38 ( forall sem. Syntaxes syns sem => AbstractableTy (Ty prov '[]) sem
39 -- , forall sem a. syn sem => AbstractableLam sem a
40 -- , forall sem. syn sem => AbstractableLam sem a
41 -- , forall sem. syn sem => Typeable sem -- user instance not accepted
42 -- , forall s1 s2. (syn s1, syn s2) => s1 ~ s2 -- crazy...
44 AbstractableTy (Ty prov '[]) (Forall syns)
46 lamTy aTy f = Forall (lamTy aTy (\a -> let Forall b = f (forallSem a) in b))
48 -- Safe here because (a :: sem a) and (b :: sem b) share the same 'sem'.
49 forallSem :: sem a -> Forall syns a
50 forallSem a = Forall (unsafeCoerce a)
53 data OpenTerm (syns :: [Syntax]) (vs :: [Type]) (a :: Type) where
54 -- | 'E' contains embedded closed (i.e. already abstracted) terms.
55 E :: Forall syns a -> OpenTerm syns vs a
56 -- | 'V' represents a reference to the innermost/top environment variable, i.e. Z
57 V :: OpenTerm syns (a ': vs) a
58 -- | 'N' represents internalizing the innermost bound variable as a
59 -- function argument. In other words, we can represent an open
60 -- term referring to a certain variable as a function which
61 -- takes that variable as an argument.
62 N :: OpenTerm syns vs (a -> b) -> OpenTerm syns (a ': vs) b
63 -- | 'W' is a special variant of N for efficiency,
64 -- in the case where the term does not refer
65 -- to the topmost variable at all.
66 W :: OpenTerm syns vs b -> OpenTerm syns (a ': vs) b
69 -- ( forall sem. Syntaxes syns sem => AbstractableTy (Ty prov '[]) sem
70 -- , Syntaxes syns (Forall syns)
72 -- AbstractableTy (Ty prov '[]) (OpenTerm syns '[])
74 -- lamTy aTy f = E (lamTy aTy (unE Fun.. f Fun.. E))
76 ( forall sem. Syntaxes syns sem => Unabstractable sem
77 , Syntaxes syns (Forall syns)
79 Unabstractable (OpenTerm syns vs)
88 instance AbstractableTy (Ty prov '[]) Eval where
89 lamTy _aTy f = Eval (unEval Fun.. f Fun.. Eval)
91 runOpenTerm :: Syntaxes syns Eval => OpenTerm syns '[] a -> a
92 runOpenTerm t | E (Forall sem) <- t = unEval sem
94 unE :: OpenTerm syns '[] a -> Forall syns a
95 unE t = case t of E t' -> t'
98 ( forall sem. Syntaxes syns sem => Unabstractable sem
99 , forall sem. Syntaxes syns sem => Instantiable sem
100 , Syntaxes syns (Forall syns)
102 Instantiable (OpenTerm syns vs)
108 ( forall sem. Syntaxes syns sem => Unabstractable sem
109 , forall sem. Syntaxes syns sem => Instantiable sem
110 , Syntaxes syns (Forall syns)
112 OpenTerm syns vs (a -> b) ->
113 OpenTerm syns vs a ->
115 E d `appOpenTerm` N e = N (E ((Sym..) .@ d) `appOpenTerm` e)
116 E d `appOpenTerm` V = N (E d)
117 E d `appOpenTerm` W e = W (E d `appOpenTerm` e)
118 E d1 `appOpenTerm` E d2 = E (d1 .@ d2)
119 N e `appOpenTerm` E d = N (E (Sym.flip .@ Sym.flip .@ d) `appOpenTerm` e)
120 N e `appOpenTerm` V = N (E Sym.ap `appOpenTerm` e `appOpenTerm` E Sym.id)
121 N e1 `appOpenTerm` N e2 = N (E Sym.ap `appOpenTerm` e1 `appOpenTerm` e2)
122 N e1 `appOpenTerm` W e2 = N (E Sym.flip `appOpenTerm` e1 `appOpenTerm` e2)
123 V `appOpenTerm` E d = N (E (Sym.flip .@ Sym.id .@ d))
124 V `appOpenTerm` N e = N (E (Sym.ap .@ Sym.id) `appOpenTerm` e)
125 V `appOpenTerm` W e = N (E (Sym.flip .@ Sym.id) `appOpenTerm` e)
126 W e `appOpenTerm` E d = W (e `appOpenTerm` E d)
127 W e `appOpenTerm` V = N e
128 W e1 `appOpenTerm` N e2 = N (E (Sym..) `appOpenTerm` e1 `appOpenTerm` e2)
129 W e1 `appOpenTerm` W e2 = W (e1 `appOpenTerm` e2)