1 {-# LANGUAGE ConstraintKinds #-}
3 {-# LANGUAGE PolyKinds #-}
4 {-# LANGUAGE UndecidableInstances #-}
5 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 module Language.Symantic.Compiling.Term
7 ( module Language.Symantic.Compiling.Term
8 , module Language.Symantic.Compiling.Term.Grammar
11 import Data.Proxy (Proxy(..))
12 import qualified Data.Text as Text
13 import Data.Type.Equality ((:~:)(..))
14 import GHC.Exts (Constraint)
16 import Language.Symantic.Helper.Data.Type.List
17 import Language.Symantic.Parsing
18 import Language.Symantic.Typing
20 import Language.Symantic.Compiling.Term.Grammar
26 (forall term. ( Sym_of_Ifaces is term
27 , Sym_of_Iface (Proxy (->)) term
31 -- | An open term (i.e. with a /lambda type context/).
32 -- The data type wraps a universal quantification
33 -- over an interpreter @term@
34 -- qualified by the symantics of a term.
36 -- Moreover the term is abstracted by a /lambda term context/
37 -- built top-down by 'compileO',
38 -- to enable a /Higher-Order Abstract Syntax/ (HOAS)
39 -- for /lambda abstractions/ ('lam').
41 -- This data type is used to keep a parsed term polymorphic enough
42 -- to stay interpretable by different interpreters.
44 -- * @(@'Sym_of_Ifaces'@ is term)@
45 -- is needed when a symantic method includes a polymorphic type
46 -- and thus calls: 'compileO'.
48 -- * @(@'Sym_of_Ifaces'@ ls term)@ and @(@'Sym_of_Ifaces'@ rs term)@
49 -- make a zipper needed to be able to write the recursing 'CompileR' instance.
51 -- * @(@'Sym_of_Iface'@ (@'Proxy'@ (->)) term)@
52 -- is needed to handle partially applied functions.
53 data TermO ctx h is ls rs
55 (forall term. ( Sym_of_Ifaces is term
56 , Sym_of_Ifaces ls term
57 , Sym_of_Ifaces rs term
58 , Sym_of_Iface (Proxy (->)) term
59 ) => TeCtx term ctx -> term h)
62 -- | Existential 'Term', with its 'Type'.
64 = forall (h:: *). ETerm
69 -- | Convenient class wrapping 'CompileR' to initiate its recursion.
70 class CompileR cs is '[] is => Compile cs is where
71 compileO :: EToken meta is -> CompileT meta ctx ret cs is '[] is
72 instance CompileR cs is '[] is => Compile cs is where
73 compileO (EToken tok) = compileR tok
75 -- | Like 'compileO' but for a term with an empty /lambda context/.
79 -> Either (Error_Term meta cs is) (ETerm cs is)
80 compile tok = closeContext (compileO tok)
84 -- | Wrap 'CompileT' to keep the /lambda context/ fully polymorphic,
85 -- this is useful in 'withContext' to help GHC's type solver, which
86 -- "Cannot instantiate unification variable with a type involving foralls".
87 data CompileO meta ret cs is ls rs
89 { unCompileO :: forall ctx. CompileT meta ctx ret cs is ls rs }
91 -- | Compile with given /lambda context/.
94 => f (TeName, ETerm cs is)
95 -> (forall hs. CompileT meta hs ret cs is ls rs)
96 -> CompileT meta ctx ret cs is ls rs
97 withContext env comp = unCompileO $
99 (\e (CompileO c) -> CompileO $ pushContext e c)
102 -- | Compile with a /lambda context/ augmented by given 'ETerm'.
104 :: (TeName, ETerm cs is)
105 -> (forall hs. CompileT meta hs ret cs is ls rs)
106 -> CompileT meta ctx ret cs is ls rs
107 pushContext (n, ETerm ty_n (Term te_n)) co ctx k =
108 co (TyCtxS n ty_n ctx) $ \ty_y (TermO y) ->
110 \c -> y (te_n `TeCtxS` c)
112 -- | Compile with the empty /lambda context/.
114 :: (forall hs. CompileT meta hs (ETerm cs is) cs is '[] is)
115 -> Either (Error_Term meta cs is) (ETerm cs is)
117 co TyCtxZ $ \typ (TermO te) ->
118 Right $ ETerm typ $ Term $ te TeCtxZ
120 -- ** Type 'CompileT'
121 -- | Convenient type synonym defining a term parser.
122 type CompileT meta ctx ret cs is ls rs
123 = TyCtx TeName cs ctx
124 -- ^ The bound variables in scope and their types:
125 -- built top-down in the heterogeneous list @ctx@,
126 -- from the closest including /lambda abstraction/ to the farest.
129 -> TermO ctx h is ls rs
130 -> Either (Error_Term meta cs is) ret )
131 -- ^ The accumulating continuation, called bottom-up.
132 -> Either (Error_Term meta cs is) ret
134 -- ** Class 'CompileR'
135 -- | Intermediate type class to construct an instance of 'Compile'
136 -- from many instances of 'CompileI', one for each item of @is@.
138 -- * @is@: starting list of /term constants/.
139 -- * @ls@: done list of /term constants/.
140 -- * @rs@: remaining list of /term constants/.
141 class CompileR (cs::[*]) (is::[*]) (ls::[*]) (rs::[*]) where
142 compileR :: TokenR meta is rs i -> CompileT meta ctx ret cs is ls rs
144 -- | Recurse into into the given 'TokenR'
145 -- to call the 'compileI' instance associated
146 -- to the 'TokenT' it contains.
149 , CompileR cs is (i ': ls) (r ': rs)
150 ) => CompileR cs is ls (i ': r ': rs) where
153 TokenZ _m t -> compileI t ctx k
155 compileR t ctx $ \typ (TermO te :: TermO ctx h is (i ': ls) (r ': rs)) ->
156 k typ (TermO te :: TermO ctx h is ls (i ': r ': rs))
157 -- | End the recursion.
160 CompileR cs is ls (i ': '[]) where
161 compileR (TokenZ _m t) ctx k = compileI t ctx k
162 compileR TokenS{} _ctx _k = error "Oops, the impossible happened..."
164 -- ** Class 'CompileI'
165 -- | Handle the work of 'Compile' for a given /interface/ @i@.
166 class CompileI (cs::[*]) (is::[*]) (i:: *) where
167 compileI :: TokenT meta is i -> CompileT meta ctx ret cs is ls (i ': rs)
169 -- * Type family 'Sym_of_Ifaces'
170 type family Sym_of_Ifaces (is::[*]) (term:: * -> *) :: Constraint where
171 Sym_of_Ifaces '[] term = ()
172 Sym_of_Ifaces (i ': is) term = (Sym_of_Iface i term, Sym_of_Ifaces is term)
174 -- ** Type family 'Sym_of_Iface'
175 type family Sym_of_Iface (i:: *) :: {-term-}(* -> *) -> Constraint
177 -- * Type 'TyConsts_of_Ifaces'
178 type TyConsts_of_Ifaces is = Nub (TyConsts_of_IfaceR is)
180 -- ** Type family 'TyConsts_of_IfaceR'
181 type family TyConsts_of_IfaceR (is::[*]) where
182 TyConsts_of_IfaceR '[] = '[]
183 TyConsts_of_IfaceR (i ': is) = TyConsts_of_Iface i ++ TyConsts_of_IfaceR is
185 -- ** Type family 'TyConsts_of_Iface'
186 type family TyConsts_of_Iface (i:: *) :: [*]
189 -- | GADT for a typing context:
190 -- accumulating at each /lambda abstraction/
191 -- the 'Type' of the introduced variable.
192 data TyCtx (name:: *) (cs::[*]) (hs::[*]) where
193 TyCtxZ :: TyCtx name cs '[]
197 -> TyCtx name cs (h ': hs)
201 -- | GADT for an evaluating context:
202 -- accumulating at each /lambda abstraction/
203 -- the @term@ of the introduced variable.
204 data TeCtx (term:: * -> *) (hs::[*]) where
205 TeCtxZ :: TeCtx term '[]
208 -> TeCtx term (h ': hs)
211 -- * Type 'Error_Term'
212 data Error_Term meta (cs::[*]) (is::[*])
213 = Error_Term_unbound TeName
214 | Error_Term_Typing (Error_Type meta '[Proxy Token_Type])
215 | Error_Term_Con_Type
217 (Con_Type meta cs '[Proxy Token_Type])
218 (Con_Type meta cs is))
219 | Error_Term_Con_Kind (Con_Kind meta is)
220 deriving instance (Eq meta, Eq_Token meta is) => Eq (Error_Term meta cs is)
221 deriving instance (Show meta, Show_Token meta is, Show_TyConst cs) => Show (Error_Term meta cs is)
224 data Con_Type meta cs ts
225 = Con_TyEq (Either (At meta '[Proxy Token_Type] (EType cs))
226 (At meta ts (EType cs)))
227 (At meta ts (EType cs))
228 | Con_TyApp (At meta ts (EType cs))
229 | Con_TyCon (At meta ts (KType cs Constraint))
230 | Con_TyFam (At meta ts TyFamName) [EType cs]
234 ) => Eq (Con_Type meta cs ts)
239 ) => Show (Con_Type meta cs ts)
241 instance MonoLift (Error_Type meta '[Proxy Token_Type]) (Error_Term meta cs ts) where
242 olift = Error_Term_Typing . olift
243 instance MonoLift (Error_Term meta cs ts) (Error_Term meta cs ts) where
246 MonoLift (Con_Type meta cs is) (Error_Term meta cs is) where
247 olift = Error_Term_Con_Type . Right
249 MonoLift (Con_Type meta cs '[Proxy Token_Type]) (Error_Term meta cs is) where
250 olift = Error_Term_Con_Type . Left
251 instance MonoLift (Con_Kind meta '[Proxy Token_Type]) (Error_Term meta cs is) where
252 olift = Error_Term_Typing . Error_Type_Con_Kind
253 instance MonoLift (Con_Kind meta is) (Error_Term meta cs is) where
254 olift = Error_Term_Con_Kind
258 :: MonoLift (Con_Type meta cs ts) err
259 => At meta ts (Type cs x)
260 -> At meta ts (Type cs y)
261 -> ((x :~: y) -> Either err ret) -> Either err ret
263 case unAt x `eq_Type` unAt y of
265 Nothing -> Left $ olift $
266 Con_TyEq (Right $ EType <$> x) (EType <$> y)
269 :: MonoLift (Con_Type meta cs ts) err
270 => At meta '[Proxy Token_Type] (Type cs x)
271 -> At meta ts (Type cs y)
272 -> ((x :~: y) -> Either err ret) -> Either err ret
273 check_Type_is x y k =
274 case unAt x `eq_Type` unAt y of
276 Nothing -> Left $ olift $
277 Con_TyEq (Left $ EType <$> x) (EType <$> y)
280 :: MonoLift (Con_Type meta cs ts) err
281 => At meta ts (Type cs (fa::kfa))
282 -> (forall ka f a. (fa :~: f a)
283 -> Type cs (f::ka -> kfa)
291 Con_TyApp (EType <$> typ)
294 :: ( MonoLift (Con_Type meta cs ts) err
295 , MonoLift (Con_Kind meta ts) err )
296 => Type cs (f:: * -> *)
297 -> At meta ts (Type cs fa)
298 -> (forall a. (fa :~: f a)
302 check_TyEq1 typ ty_fa k =
303 check_TyApp ty_fa $ \Refl ty_f ty_a ->
305 (At Nothing $ SKiType `SKiArrow` SKiType)
306 (kind_of ty_f <$ ty_fa) $ \Refl ->
309 (ty_f <$ ty_fa) $ \Refl ->
313 :: ( MonoLift (Con_Type meta cs ts) err
314 , MonoLift (Con_Kind meta ts) err )
315 => Type cs (f:: * -> * -> *)
316 -> At meta ts (Type cs fab)
317 -> (forall a b. (fab :~: f a b)
322 check_TyEq2 typ ty_fab k =
323 check_TyApp ty_fab $ \Refl ty_fa ty_b ->
324 check_TyApp (ty_fa <$ ty_fab) $ \Refl ty_f ty_a ->
326 (At Nothing $ SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
327 (kind_of ty_f <$ ty_fab) $ \Refl ->
330 (ty_f <$ ty_fab) $ \Refl ->
335 , MonoLift (Con_Type meta cs ts) err )
336 => At meta ts (Type cs (q::Constraint))
337 -> (TyCon q -> Either err ret)
340 case proj_TyCon $ unAt typ of
341 Just TyCon -> k TyCon
342 Nothing -> Left $ olift $
343 Con_TyCon (KType <$> typ)
347 , MonoLift (Con_Type meta cs ts) err
348 , MonoLift (Con_Kind meta ts) err )
350 -> At meta ts (Type cs (fa:: *))
351 -> (forall f a. (fa :~: f a)
353 -> Type cs (f:: * -> *)
357 check_TyCon1 con ty_fa k =
358 check_TyApp ty_fa $ \Refl ty_f ty_a ->
360 (At Nothing (SKiType `SKiArrow` SKiType))
361 (kind_of ty_f <$ ty_fa) $ \Refl ->
362 check_TyCon ((con :$ ty_f) <$ ty_fa) $ \TyCon ->
363 k Refl TyCon ty_f ty_a
366 :: ( MonoLift (Con_Type meta cs ts) err
371 -> (Type cs (TyFam fam hs) -> Either err ret)
373 check_TyFam fam tys k =
374 case proj_TyFam (unAt fam) tys of
376 Nothing -> Left $ olift $
378 (Text.pack . show <$> fam)