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)
159 ) => Proxy ex -> ast -> ty x -> ty y
160 -> (x :~: y -> Either (Error_of_Expr ast root) ret)
161 -> Either (Error_of_Expr ast root) ret
162 check_eq_type ex ast x y k =
163 case x `eq_type` y of
165 Nothing -> Left $ error_expr ex $
166 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_Type0' or higher
192 -- is an instance of a given 'Constraint',
193 -- or raise 'Error_Expr_Constraint_missing'.
194 check_constraint_type
195 :: forall ast ex c root ty h ret.
196 ( root ~ Root_of_Expr ex
197 , ty ~ Type_Root_of_Expr ex
198 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
199 (Error_of_Expr ast root)
200 , Constraint_Type c ty
202 => Proxy ex -> Proxy c -> ast -> ty h
203 -> (Dict (c h) -> Either (Error_of_Expr ast root) ret)
204 -> Either (Error_of_Expr ast root) ret
205 check_constraint_type ex c ast ty k =
206 case constraint_type c ty of
208 Nothing -> Left $ error_expr ex $
209 Error_Expr_Constraint_missing ast
211 -- FIXME: not easy to report the constraint
212 -- and still support 'Eq' and 'Show' deriving.
215 -- | Parsing utility to check that a 'Type_Type1' or higher
216 -- is an instance of a given 'Constraint',
217 -- or raise 'Error_Expr_Constraint_missing'.
218 check_constraint_type1
219 :: forall ast ex c root ty h a ret.
220 ( root ~ Root_of_Expr ex
221 , ty ~ Type_Root_of_Expr ex
222 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
223 (Error_of_Expr ast root)
224 , Constraint_Type1 c ty
225 ) => Proxy ex -> Proxy c -> ast -> ty (h a)
226 -> (Dict (c h) -> Either (Error_of_Expr ast root) ret)
227 -> Either (Error_of_Expr ast root) ret
228 check_constraint_type1 ex c ast ty k =
229 case constraint_type1 c ty of
231 Nothing -> Left $ error_expr ex $
232 Error_Expr_Constraint_missing ast
235 -- | Parsing utility to check that the given type is at least a 'Type_Type1'
236 -- or raise 'Error_Expr_Type_mismatch'.
238 :: forall ast ex root ty h ret.
239 ( root ~ Root_of_Expr ex
240 , ty ~ Type_Root_of_Expr ex
241 , Unlift_Type1 (Type_of_Expr root)
242 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
243 (Error_of_Expr ast root)
244 ) => Proxy ex -> ast -> ty h
247 , Lift_Type1 t1 ty (Type_Var0 :|: Type_Var1 :|: Type_of_Expr root)
248 ) -> Either (Error_of_Expr ast root) ret)
249 -> Either (Error_of_Expr ast root) ret
250 check_type1 ex ast ty k =
251 (`fromMaybe` unlift_type1 (unType_Root ty) (Just . k)) $
254 Error_Expr_Type_mismatch ast
255 (Exists_Type (type_var1 SZero (type_var0 SZero)
261 -- | Like 'expr_from' but for a root expression.
265 , Root_of_Expr root ~ root
266 ) => Proxy root -> ast
267 -> Either (Error_of_Expr ast root)
268 (Exists_Type_and_Repr (Type_Root_of_Expr root)
270 root_expr_from _ex ast =
271 expr_from (Proxy::Proxy root) ast
272 Context_Empty $ \ty (Forall_Repr_with_Context repr) ->
273 Right $ Exists_Type_and_Repr ty $
274 Forall_Repr $ repr Context_Empty
276 -- | Parse a literal.
278 :: forall ty lit ex ast hs ret.
279 ( ty ~ Type_Root_of_Expr ex
281 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
282 (Error_of_Expr ast (Root_of_Expr ex))
283 ) => (forall repr. Sym_of_Expr ex repr => lit -> repr lit)
284 -> ty lit -> Lambda_Var_Name
285 -> Expr_From ast ex hs ret
286 lit_from lit ty_lit toread ex ast _ctx k =
287 case read_safe toread of
288 Left err -> Left $ error_expr ex $ Error_Expr_Read err ast
289 Right (i::lit) -> k ty_lit $ Forall_Repr_with_Context $ const $ lit i
291 -- | Parse a unary class operator.
293 :: forall root ty cl ex ast hs ret.
294 ( ty ~ Type_Root_of_Expr ex
295 , root ~ Root_of_Expr ex
298 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
299 (Error_of_Expr ast root)
300 , Root_of_Expr root ~ root
301 , Constraint_Type cl ty
302 ) => (forall lit repr. (cl lit, Sym_of_Expr ex repr) => repr lit -> repr lit)
304 -> Expr_From ast ex hs ret
305 class_op1_from op cl 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_constraint_type ex cl ast_x ty_x $ \Dict ->
309 k ty_x $ Forall_Repr_with_Context (op . x)
311 -- | Parse a binary class operator.
313 :: forall root ty cl ex ast hs ret.
314 ( ty ~ Type_Root_of_Expr ex
315 , root ~ Root_of_Expr ex
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 , Constraint_Type cl ty
322 ) => (forall lit repr. (cl lit, Sym_of_Expr ex repr) => repr lit -> repr lit -> repr lit)
323 -> Proxy cl -> ast -> ast
324 -> Expr_From ast ex hs ret
325 class_op2_from op cl ast_x ast_y ex ast ctx k =
326 expr_from (Proxy::Proxy root) ast_x ctx $ \ty_x (Forall_Repr_with_Context x) ->
327 expr_from (Proxy::Proxy root) ast_y ctx $ \ty_y (Forall_Repr_with_Context y) ->
328 check_constraint_type ex cl ast_x ty_x $ \Dict ->
329 check_constraint_type ex cl ast_y ty_y $ \Dict ->
330 check_eq_type ex ast ty_x ty_y $ \Refl ->
331 k ty_x $ Forall_Repr_with_Context $
334 -- | Parse a unary operator.
336 :: forall root ty lit ex ast hs ret.
337 ( ty ~ Type_Root_of_Expr ex
338 , root ~ Root_of_Expr ex
341 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
342 (Error_of_Expr ast root)
343 , Root_of_Expr root ~ root
344 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit)
346 -> Expr_From ast ex hs ret
347 op1_from op ty_lit ast_x ex ast ctx k =
348 expr_from (Proxy::Proxy root) ast_x ctx $
349 \ty_x (Forall_Repr_with_Context x) ->
350 check_eq_type ex ast ty_lit ty_x $ \Refl ->
351 k ty_x $ Forall_Repr_with_Context (op . x)
353 -- | Parse a binary operator.
355 :: forall root ty lit ex ast hs ret.
356 ( ty ~ Type_Root_of_Expr ex
357 , root ~ Root_of_Expr ex
360 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
361 (Error_of_Expr ast root)
362 , Root_of_Expr root ~ root
363 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit -> repr lit)
364 -> ty lit -> ast -> ast
365 -> Expr_From ast ex hs ret
366 op2_from op ty_lit ast_x ast_y ex ast ctx k =
367 expr_from (Proxy::Proxy root) ast_x ctx $ \ty_x (Forall_Repr_with_Context x) ->
368 expr_from (Proxy::Proxy root) ast_y ctx $ \ty_y (Forall_Repr_with_Context y) ->
369 check_eq_type ex ast ty_lit ty_x $ \Refl ->
370 check_eq_type ex ast ty_lit ty_y $ \Refl ->
371 k ty_x $ Forall_Repr_with_Context $