1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE FlexibleInstances #-}
6 {-# LANGUAGE KindSignatures #-}
7 {-# LANGUAGE MultiParamTypeClasses #-}
8 {-# LANGUAGE Rank2Types #-}
9 {-# LANGUAGE ScopedTypeVariables #-}
10 {-# LANGUAGE TypeFamilies #-}
11 {-# LANGUAGE TypeOperators #-}
12 {-# LANGUAGE UndecidableInstances #-}
13 module Language.Symantic.Expr.From where
15 import Data.Maybe (fromMaybe)
16 import Data.Proxy (Proxy(..))
17 import Data.Text (Text)
18 import Data.Type.Equality ((:~:)(Refl))
19 import GHC.Prim (Constraint)
21 import Language.Symantic.Type
22 import Language.Symantic.Expr.Root
23 import Language.Symantic.Expr.Alt
24 import Language.Symantic.Expr.Error
26 -- * Class 'Expr_from'
27 -- | Parse given @ast@ into
28 -- a 'Type_Root_of_Expr' and
29 -- a 'Forall_Repr_with_Context',
30 -- or return an 'Error_of_Expr'.
31 class Expr_from ast (ex:: *) where
32 expr_from :: Expr_From ast ex hs ret
34 ( Expr_from ast (ex (Expr_Root ex))
35 , Root_of_Expr (ex (Expr_Root ex)) ~ Expr_Root ex
36 ) => Expr_from ast (Expr_Root ex) where
37 expr_from _ex ctx ast k =
38 expr_from (Proxy::Proxy (ex (Expr_Root ex)))
39 ctx ast $ \ty (Forall_Repr_with_Context repr) ->
40 k ty (Forall_Repr_with_Context repr)
42 ( Expr_from ast (curr root)
43 , Expr_from ast (next root)
44 , Root_of_Expr (curr root) ~ root
45 , Root_of_Expr (next root) ~ root
46 , Unlift_Error_Expr (Error_Expr (Error_of_Type ast (Type_Root_of_Expr root))
47 (Type_Root_of_Expr root) ast)
48 (Error_of_Expr ast root)
49 ) => Expr_from ast (Expr_Alt curr next root) where
50 expr_from _ex ctx ast k =
51 case expr_from (Proxy::Proxy (curr root)) ctx ast $
52 \ty (Forall_Repr_with_Context repr) ->
53 Right $ k ty (Forall_Repr_with_Context repr) of
56 case unlift_error_expr err of
57 Just (Error_Expr_Unsupported_here _
58 :: Error_Expr (Error_of_Type ast (Type_Root_of_Expr root))
59 (Type_Root_of_Expr root) ast) ->
60 expr_from (Proxy::Proxy (next root)) ctx ast $
61 \ty (Forall_Repr_with_Context repr) ->
62 k ty (Forall_Repr_with_Context repr)
65 -- ** Type 'Expr_From'
66 type Expr_From ast ex hs ret
68 -- ^ Select the 'Expr_from' instance.
70 -- ^ The input data to parse.
71 -> Context (Lambda_Var (Type_Root_of_Expr ex)) hs
72 -- ^ The bound variables in scope and their types:
73 -- held in the heterogeneous list @hs@,
74 -- from the closest including lambda abstraction to the farest.
76 . Type_Root_of_Expr ex h
77 -> Forall_Repr_with_Context ex hs h
78 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret )
79 -- ^ The accumulating continuation.
80 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret
83 -- | GADT for a typing context,
84 -- accumulating an @item@ at each lambda;
85 -- used to accumulate object-types (in 'Expr_from')
86 -- or host-terms (in 'Repr_Host')
87 -- associated with the 'Lambda_Var's in scope.
88 data Context :: (* -> *) -> [*] -> * where
89 Context_Empty :: Context item '[]
90 Context_Next :: item h -> Context item hs -> Context item (h ': hs)
91 infixr 5 `Context_Next`
93 -- ** Type 'Lambda_Var'
94 -- | Join a name and a type.
96 -- This data type is used to handle lambda variables by name
97 -- (instead of DeBruijn indices for instance).
99 = Lambda_Var Lambda_Var_Name (ty h)
100 type Lambda_Var_Name = Text
102 -- ** Type 'Forall_Repr_with_Context'
103 -- | A data type embedding a universal quantification
104 -- over an interpreter @repr@
105 -- and qualified by the symantics of an expression.
107 -- Moreover the expression is abstracted by a 'Context'
108 -- built at parsing time to build a /Higher-Order Abstract Syntax/ (HOAS)
109 -- for lambda abstractions.
111 -- This data type is used to keep a parsed expression polymorphic enough
112 -- to stay interpretable by different interpreters.
114 -- NOTE: 'Sym_of_Expr'@ ex repr@
115 -- is needed to be able to use symantic methods of the parsed expression
116 -- into a 'Forall_Repr_with_Context'@ ex@.
118 -- NOTE: 'Sym_of_Expr'@ (@'Root_of_Expr'@ ex) repr@
119 -- is needed to be able to use an expression
120 -- out of a 'Forall_Repr_with_Context'@ (@'Root_of_Expr'@ ex)@
121 -- into a 'Forall_Repr_with_Context'@ ex@,
122 -- which happens when a symantic method includes a polymorphic type
123 -- and thus calls: 'expr_from'@ (Proxy::Proxy (@'Root_of_Expr'@ ex))@.
124 data Forall_Repr_with_Context ex hs h
125 = Forall_Repr_with_Context
126 ( forall repr. ( Sym_of_Expr ex repr
127 , Sym_of_Expr (Root_of_Expr ex) repr
128 ) => Context repr hs -> repr h )
130 -- ** Type 'Forall_Repr'
131 data Forall_Repr ex h
133 { unForall_Repr :: forall repr
134 . ( Sym_of_Expr ex repr
135 , Sym_of_Expr (Root_of_Expr ex) repr )
138 -- ** Type family 'Sym_of_Expr'
139 -- | The symantic of an expression.
140 type family Sym_of_Expr (ex:: *) (repr:: * -> *) :: Constraint
141 type instance Sym_of_Expr (Expr_Root ex) repr
142 = Sym_of_Expr (ex (Expr_Root ex)) repr
143 type instance Sym_of_Expr (Expr_Alt curr next root) repr
144 = ( Sym_of_Expr (curr root) repr
145 , Sym_of_Expr (next root) repr
150 -- | Parsing utility to check that two types are equal,
151 -- or raise 'Error_Expr_Type_mismatch'.
153 :: forall ast ex root ty x y ret.
154 ( root ~ Root_of_Expr ex
155 , ty ~ Type_Root_of_Expr ex
157 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
158 (Error_of_Expr ast root)
160 => Proxy ex -> ast -> ty x -> ty y
161 -> (x :~: y -> Either (Error_of_Expr ast root) ret)
162 -> Either (Error_of_Expr ast root) ret
163 check_eq_type ex ast x y k =
164 case x `eq_type` y of
166 Nothing -> Left $ error_expr ex $
167 Error_Expr_Type_mismatch ast
170 -- | Parsing utility to check that two 'Type_Type1' are equal,
171 -- or raise 'Error_Expr_Type_mismatch'.
173 :: forall ast ex root ty h1 h2 a1 a2 ret.
174 ( root ~ Root_of_Expr ex
175 , ty ~ Type_Root_of_Expr ex
177 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
178 (Error_of_Expr ast root)
180 => Proxy ex -> ast -> ty (h1 a1) -> ty (h2 a2)
181 -> (h1 :~: h2 -> Either (Error_of_Expr ast root) ret)
182 -> Either (Error_of_Expr ast root) ret
183 check_eq_type1 ex ast h1 h2 k =
184 case h1 `eq_type1` h2 of
186 Nothing -> Left $ error_expr ex $
187 Error_Expr_Type_mismatch ast
191 -- | Parsing utility to check that a type is has instance of a given 'Constraint',
192 -- or raise 'Error_Expr_Constraint_missing'.
193 check_constraint_type
194 :: forall ast ex c root ty h ret.
195 ( root ~ Root_of_Expr ex
196 , ty ~ Type_Root_of_Expr ex
197 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
198 (Error_of_Expr ast root)
199 , Constraint_Type c ty
201 => Proxy ex -> Proxy c -> ast -> ty h
202 -> (Dict (c h) -> Either (Error_of_Expr ast root) ret)
203 -> Either (Error_of_Expr ast root) ret
204 check_constraint_type ex c ast ty k =
205 case constraint_type c ty of
207 Nothing -> Left $ error_expr ex $
208 Error_Expr_Constraint_missing ast
210 -- FIXME: not easy to report the constraint
211 -- and still support 'Eq' and 'Show' deriving.
214 -- | Parsing utility to check that a type is has instance of a given 'Constraint'1,
215 -- or raise 'Error_Expr_Constraint_missing'.
216 check_constraint1_type
217 :: forall ast ex c root ty h a ret.
218 ( root ~ Root_of_Expr ex
219 , ty ~ Type_Root_of_Expr ex
220 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
221 (Error_of_Expr ast root)
222 , Constraint_Type1 c ty
224 => Proxy ex -> Proxy c -> ast -> ty (h a)
225 -> (Dict (c h) -> Either (Error_of_Expr ast root) ret)
226 -> Either (Error_of_Expr ast root) ret
227 check_constraint1_type ex c ast ty k =
228 case constraint_type1 c ty of
230 Nothing -> Left $ error_expr ex $
231 Error_Expr_Constraint_missing ast
234 -- | Parsing utility to check that the given type is a 'Type_Type1'
235 -- or raise 'Error_Expr_Type_mismatch'.
237 :: forall ast ex root ty h ret.
238 ( root ~ Root_of_Expr ex
239 , ty ~ Type_Root_of_Expr ex
240 , Unlift_Type1 (Type_of_Expr root)
241 , String_from_Type ty
242 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
243 (Error_of_Expr ast root)
245 => Proxy ex -> ast -> ty h
248 , Lift_Type1 t1 (Type_Var0 :|: Type_Var1 :|: Type_of_Expr root)
249 ) -> Either (Error_of_Expr ast root) ret)
250 -> Either (Error_of_Expr ast root) ret
251 check_type1 ex ast ty k =
252 (`fromMaybe` unlift_type1 (unType_Root ty) (Just . k)) $
255 Error_Expr_Type_mismatch ast
256 (Exists_Type (type_var1 SZero (type_var0 SZero)
257 :: Type_Root_of_Expr ex (Var1 Var0)))
262 -- | Like 'expr_from' but for a root expression.
264 :: forall lam ast root.
266 , Root_of_Expr root ~ root )
267 => Proxy root -> Proxy lam -> ast
268 -> Either (Error_of_Expr ast root)
269 (Exists_Type_and_Repr (Type_Root_of_Expr root)
271 root_expr_from _ex _lam ast =
272 expr_from (Proxy::Proxy root) ast
273 Context_Empty $ \ty (Forall_Repr_with_Context repr) ->
274 Right $ Exists_Type_and_Repr ty $
275 Forall_Repr $ repr Context_Empty
277 -- | Parse a literal.
279 :: forall ty lit ex ast hs ret.
280 ( ty ~ Type_Root_of_Expr ex
282 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
283 (Error_of_Expr ast (Root_of_Expr ex))
284 ) => (forall repr. Sym_of_Expr ex repr => lit -> repr lit)
285 -> ty lit -> Lambda_Var_Name
286 -> Expr_From ast ex hs ret
287 lit_from lit ty_lit toread ex ast _ctx k =
288 case read_safe toread of
289 Left err -> Left $ error_expr ex $ Error_Expr_Read err ast
290 Right (i::lit) -> k ty_lit $ Forall_Repr_with_Context $ const $ lit i
292 -- | Parse a unary operator.
294 :: forall root ty lit ex ast hs ret.
295 ( ty ~ Type_Root_of_Expr ex
296 , root ~ Root_of_Expr ex
297 , Eq_Type (Type_Root_of_Expr root)
299 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
300 (Error_of_Expr ast root)
301 , Root_of_Expr root ~ root
302 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit)
304 -> Expr_From ast ex hs ret
305 op1_from op ty_lit ast_x ex ast ctx k =
306 expr_from (Proxy::Proxy root) ast_x ctx $
307 \ty_x (Forall_Repr_with_Context x) ->
308 check_eq_type ex ast ty_lit ty_x $ \Refl ->
309 k ty_x $ Forall_Repr_with_Context (op . x)
311 -- | Parse a binary operator.
313 :: forall root ty lit ex ast hs ret.
314 ( ty ~ Type_Root_of_Expr ex
315 , root ~ Root_of_Expr ex
316 , Eq_Type (Type_Root_of_Expr root)
318 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
319 (Error_of_Expr ast root)
320 , Root_of_Expr root ~ root
321 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit -> repr lit)
322 -> ty lit -> ast -> ast
323 -> Expr_From ast ex hs ret
324 op2_from op ty_lit ast_x ast_y ex ast ctx k =
325 expr_from (Proxy::Proxy root) ast_x ctx $ \ty_x (Forall_Repr_with_Context x) ->
326 expr_from (Proxy::Proxy root) ast_y ctx $ \ty_y (Forall_Repr_with_Context y) ->
327 check_eq_type ex ast ty_lit ty_x $ \Refl ->
328 check_eq_type ex ast ty_lit ty_y $ \Refl ->
329 k ty_x $ Forall_Repr_with_Context $