]> Git — Sourcephile - haskell/symantic.git/blob - Language/LOL/Symantic/Expr/Common.hs
init
[haskell/symantic.git] / Language / LOL / Symantic / Expr / Common.hs
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE FlexibleInstances #-}
5 {-# LANGUAGE GADTs #-}
6 {-# LANGUAGE KindSignatures #-}
7 {-# LANGUAGE MultiParamTypeClasses #-}
8 {-# LANGUAGE Rank2Types #-}
9 {-# LANGUAGE ScopedTypeVariables #-}
10 {-# LANGUAGE TypeFamilies #-}
11 {-# LANGUAGE TypeOperators #-}
12 {-# LANGUAGE UndecidableInstances #-}
13 module Language.LOL.Symantic.Expr.Common where
14
15 import Data.Proxy (Proxy(..))
16 import Data.Text (Text)
17 import Data.Type.Equality ((:~:)(Refl))
18 import GHC.Prim (Constraint)
19
20 import Language.LOL.Symantic.AST
21 import Language.LOL.Symantic.Type
22
23 -- * Class 'Expr_from'
24
25 -- | Parse given @ast@ into
26 -- a 'Type_Root_of_Expr' and
27 -- a 'Forall_Repr_with_Context',
28 -- or return an 'Error_of_Expr'.
29 class Expr_from ast (ex:: *) where
30 expr_from
31 :: forall hs ret
32 . Proxy ex
33 -- ^ Select the 'Expr_from' instance for the expression @ex@.
34 -> Context (Lambda_Var (Type_Root_of_Expr ex)) hs
35 -- ^ The bound variables in scope and their types:
36 -- held in the heterogeneous list @hs@,
37 -- from the closest including lambda abstraction to the farest.
38 -> ast
39 -- ^ The input data to parse.
40 -> ( forall h
41 . Type_Root_of_Expr ex h
42 -> Forall_Repr_with_Context ex hs h
43 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret )
44 -- ^ The accumulating continuation.
45 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret
46
47 -- ** Type 'Context'
48
49 -- | GADT for a typing context,
50 -- accumulating an @item@ at each lambda;
51 -- used to accumulate object-types of lambda variables in 'Expr_from'
52 -- or host-terms of lambda variables in 'Repr_Host'.
53 data Context :: (* -> *) -> [*] -> * where
54 Context_Empty :: Context item '[]
55 Context_Next :: item h -> Context item hs -> Context item (h ': hs)
56 infixr 5 `Context_Next`
57
58 -- ** Type 'Lambda_Var'
59 -- | Join a name and a type.
60 --
61 -- This data type is used to handle lambda variables by name
62 -- (instead of DeBruijn indices for instance).
63 data Lambda_Var ty h
64 = Lambda_Var Var_Name (ty h)
65 type Var_Name = Text
66
67 -- ** Type 'Forall_Repr_with_Context'
68 -- | A data type embedding a universal quantification
69 -- over an interpreter @repr@
70 -- and qualified by the symantics of an expression.
71 --
72 -- Moreover the expression is abstracted by a 'Context'
73 -- built at parsing time to build a /Higher-Order Abstract Syntax/ (HOAS)
74 -- for lambda abstractions.
75 --
76 -- This data type is used to keep a parsed expression polymorphic enough
77 -- to stay interpretable by different interpreters.
78 --
79 -- NOTE: 'Sym_of_Expr'@ ex repr@
80 -- is needed to be able to use symantic methods of the parsed expression
81 -- into a 'Forall_Repr_with_Context'@ ex@.
82 --
83 -- NOTE: 'Sym_of_Expr'@ (@'Root_of_Expr'@ ex) repr@
84 -- is needed to be able to use an expression
85 -- out of a 'Forall_Repr_with_Context'@ (@'Root_of_Expr'@ ex)@
86 -- into a 'Forall_Repr_with_Context'@ ex@,
87 -- which happens when a symantic method includes a polymorphic type
88 -- and thus calls: 'expr_from'@ (Proxy::Proxy (@'Root_of_Expr'@ ex))@.
89 data Forall_Repr_with_Context ex hs h
90 = Forall_Repr_with_Context
91 ( forall repr. ( Sym_of_Expr ex repr
92 , Sym_of_Expr (Root_of_Expr ex) repr
93 ) => Context repr hs -> repr h )
94
95 -- ** Type 'Forall_Repr'
96 data Forall_Repr ex h
97 = Forall_Repr
98 { unForall_Repr :: forall repr
99 . Sym_of_Expr ex repr
100 => repr h }
101
102 -- ** Type family 'Root_of_Expr'
103 -- | The root expression of an expression.
104 type family Root_of_Expr (ex:: *) :: *
105
106 -- ** Type family 'Sym_of_Expr'
107 -- | The symantic of an expression.
108 type family Sym_of_Expr (ex:: *) (repr:: * -> *) :: Constraint
109
110 -- ** Type family 'Error_of_Expr'
111 -- | The error(s) of an expression.
112 type family Error_of_Expr (ast:: *) (ex:: *) :: *
113
114 -- ** Type family 'Type_of_Expr'
115 -- | The type of an expression, parameterized by a root type.
116 type family Type_of_Expr (ex:: *) :: {-root-}(* -> *) -> {-h-}* -> *
117
118 -- ** Type 'Type_Root_of_Expr'
119 -- | Convenient alias.
120 --
121 -- NOTE: include 'Type_Var' only to use it
122 -- within 'Error_Expr_Type_mismatch' so far.
123 type Type_Root_of_Expr (ex:: *)
124 = Type_Root (Type_Alt Type_Var (Type_of_Expr (Root_of_Expr ex)))
125
126 -- * Type 'Expr_Root'
127 -- | The root expression, passing itself as parameter to the given expression.
128 newtype Expr_Root (ex:: * -> *)
129 = Expr_Root (ex (Expr_Root ex))
130 type instance Root_of_Expr (Expr_Root ex) = Expr_Root ex
131 type instance Type_of_Expr (Expr_Root ex)
132 = Type_of_Expr (ex (Expr_Root ex))
133 type instance Error_of_Expr ast (Expr_Root ex)
134 = Error_Expr_Alt (Error_Expr (Error_of_Type ast (Type_Root_of_Expr (ex (Expr_Root ex))))
135 (Type_Root_of_Expr (ex (Expr_Root ex)))
136 ast)
137 (Error_of_Expr ast (ex (Expr_Root ex)))
138 type instance Sym_of_Expr (Expr_Root ex) repr
139 = Sym_of_Expr (ex (Expr_Root ex)) repr
140 instance -- Expr_from
141 ( Expr_from ast (ex (Expr_Root ex))
142 , Root_of_Expr (ex (Expr_Root ex)) ~ Expr_Root ex
143 ) => Expr_from ast (Expr_Root ex) where
144 expr_from _px_ex ctx ast k =
145 expr_from (Proxy::Proxy (ex (Expr_Root ex)))
146 ctx ast $ \ty (Forall_Repr_with_Context repr) ->
147 k ty (Forall_Repr_with_Context repr)
148
149 {- NOTE: useless code so far.
150 -- ** Class 'Expr_Root_Lift'
151 -- | Lift a given expression to a given root expression.
152 class Expr_Root_Lift ex root where
153 expr_root_lift :: ex root -> root
154 instance
155 Expr_Lift ex root =>
156 Expr_Root_Lift ex (Expr_Root root) where
157 expr_root_lift = Expr_Root . expr_lift
158 -}
159
160 -- * Type 'Expr_Alt'
161 -- | Expression making an alternative between two expressions.
162 data Expr_Alt curr next (root:: *)
163 = Expr_Alt_Curr (curr root)
164 | Expr_Alt_Next (next root)
165 type instance Root_of_Expr (Expr_Alt curr next root) = root
166 type instance Type_of_Expr (Expr_Alt curr next root)
167 = Type_Alt (Type_of_Expr (curr root))
168 (Type_of_Expr (next root))
169 type instance Error_of_Expr ast (Expr_Alt curr next root)
170 = Error_Expr_Alt (Error_of_Expr ast (curr root))
171 (Error_of_Expr ast (next root))
172 type instance Sym_of_Expr (Expr_Alt curr next root) repr
173 = ( Sym_of_Expr (curr root) repr
174 , Sym_of_Expr (next root) repr
175 )
176 instance -- Expr_from
177 ( Expr_from ast (curr root)
178 , Expr_from ast (next root)
179 , Root_of_Expr (curr root) ~ root
180 , Root_of_Expr (next root) ~ root
181 , Error_Expr_Unlift (Error_Expr (Error_of_Type ast (Type_Root_of_Expr root))
182 (Type_Root_of_Expr root) ast)
183 (Error_of_Expr ast root)
184 ) => Expr_from ast (Expr_Alt curr next root) where
185 expr_from _px_ex ctx ast k =
186 case expr_from (Proxy::Proxy (curr root)) ctx ast $
187 \ty (Forall_Repr_with_Context repr) ->
188 Right $ k ty (Forall_Repr_with_Context repr) of
189 Right ret -> ret
190 Left err ->
191 case error_expr_unlift err of
192 Just (Error_Expr_Unsupported_here _
193 :: Error_Expr (Error_of_Type ast (Type_Root_of_Expr root))
194 (Type_Root_of_Expr root) ast) ->
195 expr_from (Proxy::Proxy (next root)) ctx ast $
196 \ty (Forall_Repr_with_Context repr) ->
197 k ty (Forall_Repr_with_Context repr)
198 _ -> Left err
199
200 {- NOTE: useless code so far.
201 -- ** Type 'Expr_Lift'
202 -- | Apply 'Peano_of_Expr' on 'Expr_LiftN'.
203 type Expr_Lift ex exs
204 = Expr_LiftN (Peano_of_Expr ex exs) ex exs
205
206 -- | Convenient wrapper around 'expr_liftN',
207 -- passing it the 'Peano' number from 'Peano_of_Expr'.
208 expr_lift
209 :: forall ex exs (root:: *).
210 Expr_Lift ex exs =>
211 ex root -> exs root
212 expr_lift = expr_liftN (Proxy::Proxy (Peano_of_Expr ex exs))
213
214 -- *** Type family 'Peano_of_Expr'
215 -- | Return a 'Peano' number derived from the location
216 -- of a given expression within a given expression stack,
217 -- which is used to avoid @OverlappingInstances@.
218 type family Peano_of_Expr
219 (ex:: * -> *)
220 (exs:: * -> *) :: Peano where
221 Peano_of_Expr ex ex = Zero
222 Peano_of_Expr ex (Expr_Alt ex next) = Zero
223 Peano_of_Expr other (Expr_Alt curr next) = Succ (Peano_of_Expr other next)
224
225 -- *** Class 'Expr_LiftN'
226 -- | Lift a given expression to the top of a given expression stack including it,
227 -- by constructing the appropriate sequence of 'Expr_Alt_Curr' and 'Expr_Alt_Next'.
228 class Expr_LiftN (n:: *) ex exs where
229 expr_liftN :: forall (root:: *). Proxy n -> ex root -> exs root
230 instance Expr_LiftN Zero curr curr where
231 expr_liftN _ = id
232 instance Expr_LiftN Zero curr (Expr_Alt curr next) where
233 expr_liftN _ = Expr_Alt_Curr
234 instance
235 Expr_LiftN n other next =>
236 Expr_LiftN (Succ n) other (Expr_Alt curr next) where
237 expr_liftN _ = Expr_Alt_Next . expr_liftN (Proxy::Proxy n)
238 -}
239
240 {- NOTE: useless code so far.
241 -- ** Type 'Expr_Unlift'
242 -- | Apply 'Peano_of_Expr' on 'Expr_UnliftN'.
243 type Expr_Unlift ex exs
244 = Expr_UnliftN (Peano_of_Expr ex exs) ex exs
245
246 -- | Convenient wrapper around 'expr_unliftN',
247 -- passing it the 'Peano' number from 'Peano_of_Expr'.
248 expr_unlift
249 :: forall ex exs (root:: *).
250 Expr_Unlift ex exs =>
251 exs root -> Maybe (ex root)
252 expr_unlift = expr_unliftN (Proxy::Proxy (Peano_of_Expr ex exs))
253
254 -- *** Class 'Expr_UnliftN'
255 -- | Try to unlift a given expression out of a given expression stack including it,
256 -- by deconstructing the appropriate sequence of 'Expr_Alt_Curr' and 'Expr_Alt_Next'.
257 class Expr_UnliftN (n:: *) ex exs where
258 expr_unliftN :: forall (root:: *). Proxy n -> exs root -> Maybe (ex root)
259 instance Expr_UnliftN Zero curr curr where
260 expr_unliftN _ = Just
261 instance Expr_UnliftN Zero curr (Expr_Alt curr next) where
262 expr_unliftN _ (Expr_Alt_Curr x) = Just x
263 expr_unliftN _ (Expr_Alt_Next _) = Nothing
264 instance
265 Expr_UnliftN n other next =>
266 Expr_UnliftN (Succ n) other (Expr_Alt curr next) where
267 expr_unliftN _ (Expr_Alt_Next x) = expr_unliftN (Proxy::Proxy n) x
268 expr_unliftN _ (Expr_Alt_Curr _) = Nothing
269 -}
270
271 -- ** Type family 'Is_Last_Expr'
272 -- | Return whether a given expression is the last one in a given expression stack.
273 --
274 -- NOTE: each expression parser uses this type family
275 -- when it encounters unsupported syntax:
276 -- to know if it is the last expression parser component that will be tried
277 -- (and thus return 'Error_Expr_Unsupported')
278 -- or if some other expression parser component shall be tried
279 -- (and thus return 'Error_Expr_Unsupported_here',
280 -- which is then handled accordingly by the 'Expr_from' instance of 'Expr_Alt').
281 type family Is_Last_Expr (ex:: *) (exs:: *) :: Bool where
282 Is_Last_Expr ex ex = 'True
283 Is_Last_Expr ex (Expr_Root exs) = Is_Last_Expr ex (exs (Expr_Root exs))
284 Is_Last_Expr (ex root) (Expr_Alt ex next root) = 'False
285 Is_Last_Expr other (Expr_Alt curr next root) = Is_Last_Expr other (next root)
286
287 -- * Type 'Error_Expr_Alt'
288 -- | Error expression making an alternative between two error expressions.
289 data Error_Expr_Alt curr next
290 = Error_Expr_Alt_Curr curr
291 | Error_Expr_Alt_Next next
292 deriving (Eq, Show)
293
294 -- ** Type 'Error_Expr_Lift'
295 -- | Apply 'Peano_of_Error_Expr' on 'Error_Expr_LiftN'.
296 type Error_Expr_Lift err errs
297 = Error_Expr_LiftN (Peano_of_Error_Expr err errs) err errs
298
299 -- | Convenient wrapper around 'error_expr_liftN',
300 -- passing it the 'Peano' number from 'Peano_of_Error_Expr'.
301 error_expr_lift
302 :: forall err errs.
303 Error_Expr_Lift err errs => err -> errs
304 error_expr_lift = error_expr_liftN (Proxy::Proxy (Peano_of_Error_Expr err errs))
305
306 -- *** Type family 'Peano_of_Error_Expr'
307 -- | Return a 'Peano' number derived from the location
308 -- of a given error expression within a given error expression stack,
309 -- which is used to avoid @OverlappingInstances@.
310 type family Peano_of_Error_Expr (err:: *) (errs:: *) :: * where
311 Peano_of_Error_Expr err err = Zero
312 Peano_of_Error_Expr err (Error_Expr_Alt err next) = Zero
313 Peano_of_Error_Expr other (Error_Expr_Alt curr next) = Succ (Peano_of_Error_Expr other next)
314
315 -- *** Class 'Error_Expr_LiftN'
316 -- | Lift a given expression to the top of a given expression stack including it,
317 -- by constructing the appropriate sequence of 'Error_Expr_Alt_Curr' and 'Error_Expr_Alt_Next'.
318 class Error_Expr_LiftN (n:: *) err errs where
319 error_expr_liftN :: Proxy n -> err -> errs
320 instance Error_Expr_LiftN Zero curr curr where
321 error_expr_liftN _ = id
322 instance Error_Expr_LiftN Zero curr (Error_Expr_Alt curr next) where
323 error_expr_liftN _ = Error_Expr_Alt_Curr
324 instance
325 Error_Expr_LiftN n other next =>
326 Error_Expr_LiftN (Succ n) other (Error_Expr_Alt curr next) where
327 error_expr_liftN _ = Error_Expr_Alt_Next . error_expr_liftN (Proxy::Proxy n)
328
329 -- ** Type 'Error_Expr_Unlift'
330 -- | Apply 'Peano_of_Error_Expr' on 'Error_Expr_UnliftN'.
331 type Error_Expr_Unlift ex exs
332 = Error_Expr_UnliftN (Peano_of_Error_Expr ex exs) ex exs
333
334 -- | Convenient wrapper around 'error_expr_unliftN',
335 -- passing it the 'Peano' number from 'Peano_of_Error_Expr'.
336 error_expr_unlift
337 :: forall ex exs.
338 Error_Expr_Unlift ex exs => exs -> Maybe ex
339 error_expr_unlift = error_expr_unliftN (Proxy::Proxy (Peano_of_Error_Expr ex exs))
340
341 -- *** Class 'Error_Expr_UnliftN'
342 -- | Try to unlift a given expression error out of a given expression error stack including it,
343 -- by deconstructing the appropriate sequence of 'Error_Expr_Alt_Curr' and 'Error_Expr_Alt_Next'.
344 class Error_Expr_UnliftN (n:: *) ex exs where
345 error_expr_unliftN :: Proxy n -> exs -> Maybe ex
346 instance Error_Expr_UnliftN Zero curr curr where
347 error_expr_unliftN _ = Just
348 instance Error_Expr_UnliftN Zero curr (Error_Expr_Alt curr next) where
349 error_expr_unliftN _ (Error_Expr_Alt_Curr x) = Just x
350 error_expr_unliftN _ (Error_Expr_Alt_Next _) = Nothing
351 instance
352 Error_Expr_UnliftN n other next =>
353 Error_Expr_UnliftN (Succ n) other (Error_Expr_Alt curr next) where
354 error_expr_unliftN _ (Error_Expr_Alt_Next x) = error_expr_unliftN (Proxy::Proxy n) x
355 error_expr_unliftN _ (Error_Expr_Alt_Curr _) = Nothing
356
357 -- ** Type 'Error_Expr_Read'
358 -- | Common expression errors.
359 data Error_Expr err_ty ty ast
360 = Error_Expr_Wrong_number_of_arguments ast Int
361 -- ^ Wrong number of arguments applied to a term,
362 -- the integer is the number of arguments expected.
363 | Error_Expr_Type_mismatch ast (Exists_Type ty) (Exists_Type ty)
364 -- ^ Mismatch between respectively expected and actual type.
365 | Error_Expr_Read Error_Read ast
366 -- ^ Error when reading a literal.
367 | Error_Expr_Type err_ty ast
368 -- ^ Error when parsing a type.
369 | Error_Expr_Unsupported ast
370 -- ^ Given syntax is supported by none
371 -- of the expression parser components
372 -- of the expression stack.
373 | Error_Expr_Unsupported_here ast
374 -- ^ Given syntax not supported by
375 -- the current expression parser component.
376 deriving (Eq, Show)
377
378 -- | Convenient wrapper around 'error_expr_lift',
379 -- passing the type family boilerplate.
380 error_expr
381 :: forall ast ex ty.
382 (ty ~ Type_Root_of_Expr ex)
383 => Error_Expr_Lift
384 (Error_Expr (Error_of_Type ast ty) ty ast)
385 (Error_of_Expr ast (Root_of_Expr ex))
386 => Proxy ex
387 -> Error_Expr (Error_of_Type ast ty) ty ast
388 -> Error_of_Expr ast (Root_of_Expr ex)
389 error_expr _ = error_expr_lift
390
391 -- | Utility to return 'Error_Expr_Unsupported'
392 -- or 'Error_Expr_Unsupported_here'
393 -- according to the given expression.
394 error_expr_unsupported
395 :: forall ast ex ty root.
396 ( root ~ Root_of_Expr ex
397 , ty ~ Type_Root_of_Expr ex
398 , Implicit_HBool (Is_Last_Expr ex root)
399 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
400 (Error_of_Expr ast root)
401 ) => Proxy ex -> ast
402 -> Error_of_Expr ast (Root_of_Expr ex)
403 error_expr_unsupported px_ex ast =
404 case hbool :: HBool (Is_Last_Expr ex root) of
405 HTrue -> error_expr px_ex $ Error_Expr_Unsupported ast
406 HFalse -> error_expr px_ex $ Error_Expr_Unsupported_here ast
407
408 -- | Utility to check that two types are equal
409 -- or raise 'Error_Expr_Type_mismatch'.
410 when_type_eq
411 :: forall ast ex root ty x y ret.
412 ( root ~ Root_of_Expr ex
413 , ty ~ Type_Root_of_Expr ex
414 , Type_Eq ty
415 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
416 (Error_of_Expr ast root)
417 )
418 => Proxy ex -> ast -> ty x -> ty y
419 -> (forall. x :~: y -> Either (Error_of_Expr ast root) ret)
420 -> Either (Error_of_Expr ast root) ret
421 when_type_eq px_ex ast x y k =
422 case x `type_eq` y of
423 Nothing -> Left $ error_expr px_ex $
424 Error_Expr_Type_mismatch ast
425 (Exists_Type x)
426 (Exists_Type y)
427 Just Refl -> k Refl