]> 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 type Type_Root_of_Expr (ex:: *)
121 = Type_Root (Type_Alt Type_Var (Type_of_Expr (Root_of_Expr ex)))
122
123 -- * Type 'Expr_Root'
124 -- | The root expression, passing itself as parameter to the given expression.
125 newtype Expr_Root (ex:: * -> *)
126 = Expr_Root (ex (Expr_Root ex))
127 type instance Root_of_Expr (Expr_Root ex) = Expr_Root ex
128 type instance Type_of_Expr (Expr_Root ex)
129 = Type_of_Expr (ex (Expr_Root ex))
130 type instance Error_of_Expr ast (Expr_Root ex)
131 = Error_Expr_Alt (Error_Expr (Error_of_Type ast (Type_Root_of_Expr (ex (Expr_Root ex))))
132 (Type_Root_of_Expr (ex (Expr_Root ex)))
133 ast)
134 (Error_of_Expr ast (ex (Expr_Root ex)))
135 type instance Sym_of_Expr (Expr_Root ex) repr
136 = Sym_of_Expr (ex (Expr_Root ex)) repr
137 instance -- Expr_from
138 ( Expr_from ast (ex (Expr_Root ex))
139 , Root_of_Expr (ex (Expr_Root ex)) ~ Expr_Root ex
140 ) => Expr_from ast (Expr_Root ex) where
141 expr_from _px_ex ctx ast k =
142 expr_from (Proxy::Proxy (ex (Expr_Root ex)))
143 ctx ast $ \ty (Forall_Repr_with_Context repr) ->
144 k ty (Forall_Repr_with_Context repr)
145
146 {- NOTE: useless code so far.
147 -- ** Class 'Expr_Root_Lift'
148 -- | Lift a given expression to a given root expression.
149 class Expr_Root_Lift ex root where
150 expr_root_lift :: ex root -> root
151 instance
152 Expr_Lift ex root =>
153 Expr_Root_Lift ex (Expr_Root root) where
154 expr_root_lift = Expr_Root . expr_lift
155 -}
156
157 -- * Type 'Expr_Alt'
158 -- | Expression making an alternative between two expressions.
159 data Expr_Alt curr next (root:: *)
160 = Expr_Alt_Curr (curr root)
161 | Expr_Alt_Next (next root)
162 type instance Root_of_Expr (Expr_Alt curr next root) = root
163 type instance Type_of_Expr (Expr_Alt curr next root)
164 = Type_Alt (Type_of_Expr (curr root))
165 (Type_of_Expr (next root))
166 type instance Error_of_Expr ast (Expr_Alt curr next root)
167 = Error_Expr_Alt (Error_of_Expr ast (curr root))
168 (Error_of_Expr ast (next root))
169 type instance Sym_of_Expr (Expr_Alt curr next root) repr
170 = ( Sym_of_Expr (curr root) repr
171 , Sym_of_Expr (next root) repr
172 )
173 instance -- Expr_from
174 ( Expr_from ast (curr root)
175 , Expr_from ast (next root)
176 , Root_of_Expr (curr root) ~ root
177 , Root_of_Expr (next root) ~ root
178 , Error_Expr_Unlift (Error_Expr (Error_of_Type ast (Type_Root_of_Expr root))
179 (Type_Root_of_Expr root) ast)
180 (Error_of_Expr ast root)
181 ) => Expr_from ast (Expr_Alt curr next root) where
182 expr_from _px_ex ctx ast k =
183 case expr_from (Proxy::Proxy (curr root)) ctx ast $
184 \ty (Forall_Repr_with_Context repr) ->
185 Right $ k ty (Forall_Repr_with_Context repr) of
186 Right ret -> ret
187 Left err ->
188 case error_expr_unlift err of
189 Just (Error_Expr_Unsupported_here _
190 :: Error_Expr (Error_of_Type ast (Type_Root_of_Expr root))
191 (Type_Root_of_Expr root) ast) ->
192 expr_from (Proxy::Proxy (next root)) ctx ast $
193 \ty (Forall_Repr_with_Context repr) ->
194 k ty (Forall_Repr_with_Context repr)
195 _ -> Left err
196
197 {- NOTE: useless code so far.
198 -- ** Type 'Expr_Lift'
199 -- | Apply 'Peano_of_Expr' on 'Expr_LiftN'.
200 type Expr_Lift ex exs
201 = Expr_LiftN (Peano_of_Expr ex exs) ex exs
202
203 -- | Convenient wrapper around 'expr_liftN',
204 -- passing it the 'Peano' number from 'Peano_of_Expr'.
205 expr_lift
206 :: forall ex exs (root:: *).
207 Expr_Lift ex exs =>
208 ex root -> exs root
209 expr_lift = expr_liftN (Proxy::Proxy (Peano_of_Expr ex exs))
210
211 -- *** Type family 'Peano_of_Expr'
212 -- | Return a 'Peano' number derived from the location
213 -- of a given expression within a given expression stack,
214 -- which is used to avoid @OverlappingInstances@.
215 type family Peano_of_Expr
216 (ex:: * -> *)
217 (exs:: * -> *) :: Peano where
218 Peano_of_Expr ex ex = Zero
219 Peano_of_Expr ex (Expr_Alt ex next) = Zero
220 Peano_of_Expr other (Expr_Alt curr next) = Succ (Peano_of_Expr other next)
221
222 -- *** Class 'Expr_LiftN'
223 -- | Lift a given expression to the top of a given expression stack including it,
224 -- by constructing the appropriate sequence of 'Expr_Alt_Curr' and 'Expr_Alt_Next'.
225 class Expr_LiftN (n:: *) ex exs where
226 expr_liftN :: forall (root:: *). Proxy n -> ex root -> exs root
227 instance Expr_LiftN Zero curr curr where
228 expr_liftN _ = id
229 instance Expr_LiftN Zero curr (Expr_Alt curr next) where
230 expr_liftN _ = Expr_Alt_Curr
231 instance
232 Expr_LiftN n other next =>
233 Expr_LiftN (Succ n) other (Expr_Alt curr next) where
234 expr_liftN _ = Expr_Alt_Next . expr_liftN (Proxy::Proxy n)
235 -}
236
237 {- NOTE: useless code so far.
238 -- ** Type 'Expr_Unlift'
239 -- | Apply 'Peano_of_Expr' on 'Expr_UnliftN'.
240 type Expr_Unlift ex exs
241 = Expr_UnliftN (Peano_of_Expr ex exs) ex exs
242
243 -- | Convenient wrapper around 'expr_unliftN',
244 -- passing it the 'Peano' number from 'Peano_of_Expr'.
245 expr_unlift
246 :: forall ex exs (root:: *).
247 Expr_Unlift ex exs =>
248 exs root -> Maybe (ex root)
249 expr_unlift = expr_unliftN (Proxy::Proxy (Peano_of_Expr ex exs))
250
251 -- *** Class 'Expr_UnliftN'
252 -- | Try to unlift a given expression out of a given expression stack including it,
253 -- by deconstructing the appropriate sequence of 'Expr_Alt_Curr' and 'Expr_Alt_Next'.
254 class Expr_UnliftN (n:: *) ex exs where
255 expr_unliftN :: forall (root:: *). Proxy n -> exs root -> Maybe (ex root)
256 instance Expr_UnliftN Zero curr curr where
257 expr_unliftN _ = Just
258 instance Expr_UnliftN Zero curr (Expr_Alt curr next) where
259 expr_unliftN _ (Expr_Alt_Curr x) = Just x
260 expr_unliftN _ (Expr_Alt_Next _) = Nothing
261 instance
262 Expr_UnliftN n other next =>
263 Expr_UnliftN (Succ n) other (Expr_Alt curr next) where
264 expr_unliftN _ (Expr_Alt_Next x) = expr_unliftN (Proxy::Proxy n) x
265 expr_unliftN _ (Expr_Alt_Curr _) = Nothing
266 -}
267
268 -- ** Type family 'Is_Last_Expr'
269 -- | Return whether a given expression is the last one in a given expression stack.
270 --
271 -- NOTE: each expression parser uses this type family
272 -- when it encounters unsupported syntax:
273 -- to know if it is the last expression parser component that will be tried
274 -- (and thus return 'Error_Expr_Unsupported')
275 -- or if some other expression parser component shall be tried
276 -- (and thus return 'Error_Expr_Unsupported_here',
277 -- which is then handled accordingly by the 'Expr_from' instance of 'Expr_Alt').
278 type family Is_Last_Expr (ex:: *) (exs:: *) :: Bool where
279 Is_Last_Expr ex ex = 'True
280 Is_Last_Expr ex (Expr_Root exs) = Is_Last_Expr ex (exs (Expr_Root exs))
281 Is_Last_Expr (ex root) (Expr_Alt ex next root) = 'False
282 Is_Last_Expr other (Expr_Alt curr next root) = Is_Last_Expr other (next root)
283
284 -- * Type 'Error_Expr_Alt'
285 -- | Error expression making an alternative between two error expressions.
286 data Error_Expr_Alt curr next
287 = Error_Expr_Alt_Curr curr
288 | Error_Expr_Alt_Next next
289 deriving (Eq, Show)
290
291 -- ** Type 'Error_Expr_Lift'
292 -- | Apply 'Peano_of_Error_Expr' on 'Error_Expr_LiftN'.
293 type Error_Expr_Lift err errs
294 = Error_Expr_LiftN (Peano_of_Error_Expr err errs) err errs
295
296 -- | Convenient wrapper around 'error_expr_liftN',
297 -- passing it the 'Peano' number from 'Peano_of_Error_Expr'.
298 error_expr_lift
299 :: forall err errs.
300 Error_Expr_Lift err errs => err -> errs
301 error_expr_lift = error_expr_liftN (Proxy::Proxy (Peano_of_Error_Expr err errs))
302
303 -- *** Type family 'Peano_of_Error_Expr'
304 -- | Return a 'Peano' number derived from the location
305 -- of a given error expression within a given error expression stack,
306 -- which is used to avoid @OverlappingInstances@.
307 type family Peano_of_Error_Expr (err:: *) (errs:: *) :: * where
308 Peano_of_Error_Expr err err = Zero
309 Peano_of_Error_Expr err (Error_Expr_Alt err next) = Zero
310 Peano_of_Error_Expr other (Error_Expr_Alt curr next) = Succ (Peano_of_Error_Expr other next)
311
312 -- *** Class 'Error_Expr_LiftN'
313 -- | Lift a given expression to the top of a given expression stack including it,
314 -- by constructing the appropriate sequence of 'Error_Expr_Alt_Curr' and 'Error_Expr_Alt_Next'.
315 class Error_Expr_LiftN (n:: *) err errs where
316 error_expr_liftN :: Proxy n -> err -> errs
317 instance Error_Expr_LiftN Zero curr curr where
318 error_expr_liftN _ = id
319 instance Error_Expr_LiftN Zero curr (Error_Expr_Alt curr next) where
320 error_expr_liftN _ = Error_Expr_Alt_Curr
321 instance
322 Error_Expr_LiftN n other next =>
323 Error_Expr_LiftN (Succ n) other (Error_Expr_Alt curr next) where
324 error_expr_liftN _ = Error_Expr_Alt_Next . error_expr_liftN (Proxy::Proxy n)
325
326 -- ** Type 'Error_Expr_Unlift'
327 -- | Apply 'Peano_of_Error_Expr' on 'Error_Expr_UnliftN'.
328 type Error_Expr_Unlift ex exs
329 = Error_Expr_UnliftN (Peano_of_Error_Expr ex exs) ex exs
330
331 -- | Convenient wrapper around 'error_expr_unliftN',
332 -- passing it the 'Peano' number from 'Peano_of_Error_Expr'.
333 error_expr_unlift
334 :: forall ex exs.
335 Error_Expr_Unlift ex exs => exs -> Maybe ex
336 error_expr_unlift = error_expr_unliftN (Proxy::Proxy (Peano_of_Error_Expr ex exs))
337
338 -- *** Class 'Error_Expr_UnliftN'
339 -- | Try to unlift a given expression error out of a given expression error stack including it,
340 -- by deconstructing the appropriate sequence of 'Error_Expr_Alt_Curr' and 'Error_Expr_Alt_Next'.
341 class Error_Expr_UnliftN (n:: *) ex exs where
342 error_expr_unliftN :: Proxy n -> exs -> Maybe ex
343 instance Error_Expr_UnliftN Zero curr curr where
344 error_expr_unliftN _ = Just
345 instance Error_Expr_UnliftN Zero curr (Error_Expr_Alt curr next) where
346 error_expr_unliftN _ (Error_Expr_Alt_Curr x) = Just x
347 error_expr_unliftN _ (Error_Expr_Alt_Next _) = Nothing
348 instance
349 Error_Expr_UnliftN n other next =>
350 Error_Expr_UnliftN (Succ n) other (Error_Expr_Alt curr next) where
351 error_expr_unliftN _ (Error_Expr_Alt_Next x) = error_expr_unliftN (Proxy::Proxy n) x
352 error_expr_unliftN _ (Error_Expr_Alt_Curr _) = Nothing
353
354 -- ** Type 'Error_Expr_Read'
355 -- | Common expression errors.
356 data Error_Expr err_ty ty ast
357 = Error_Expr_Wrong_number_of_arguments ast Int
358 -- ^ Wrong number of arguments applied to a term,
359 -- the integer is the number of arguments expected.
360 | Error_Expr_Type_mismatch ast (Exists_Type ty) (Exists_Type ty)
361 -- ^ Mismatch between respectively expected and actual type.
362 | Error_Expr_Read Error_Read ast
363 -- ^ Error when reading a literal.
364 | Error_Expr_Type err_ty ast
365 -- ^ Error when parsing a type.
366 | Error_Expr_Unsupported ast
367 -- ^ Given syntax is supported by none
368 -- of the expression parser components
369 -- of the expression stack.
370 | Error_Expr_Unsupported_here ast
371 -- ^ Given syntax not supported by
372 -- the current expression parser component.
373 deriving (Eq, Show)
374
375 -- | Convenient wrapper around 'error_expr_lift',
376 -- passing the type family boilerplate.
377 error_expr
378 :: forall ast ex ty.
379 (ty ~ Type_Root_of_Expr ex)
380 => Error_Expr_Lift
381 (Error_Expr (Error_of_Type ast ty) ty ast)
382 (Error_of_Expr ast (Root_of_Expr ex))
383 => Proxy ex
384 -> Error_Expr (Error_of_Type ast ty) ty ast
385 -> Error_of_Expr ast (Root_of_Expr ex)
386 error_expr _ = error_expr_lift
387
388 -- | Utility to return 'Error_Expr_Unsupported'
389 -- or 'Error_Expr_Unsupported_here'
390 -- according to the given expression.
391 error_expr_unsupported
392 :: forall ast ex ty root.
393 ( root ~ Root_of_Expr ex
394 , ty ~ Type_Root_of_Expr ex
395 , Implicit_HBool (Is_Last_Expr ex root)
396 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
397 (Error_of_Expr ast root)
398 ) => Proxy ex -> ast
399 -> Error_of_Expr ast (Root_of_Expr ex)
400 error_expr_unsupported px_ex ast =
401 case hbool :: HBool (Is_Last_Expr ex root) of
402 HTrue -> error_expr px_ex $ Error_Expr_Unsupported ast
403 HFalse -> error_expr px_ex $ Error_Expr_Unsupported_here ast
404
405 -- | Utility to check that two types are equal
406 -- or raise 'Error_Expr_Type_mismatch'.
407 when_type_eq
408 :: forall ast ex root ty x y ret.
409 ( root ~ Root_of_Expr ex
410 , ty ~ Type_Root_of_Expr ex
411 , Type_Eq ty
412 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
413 (Error_of_Expr ast root)
414 )
415 => Proxy ex -> ast -> ty x -> ty y
416 -> (forall. x :~: y -> Either (Error_of_Expr ast root) ret)
417 -> Either (Error_of_Expr ast root) ret
418 when_type_eq px_ex ast x y k =
419 case x `type_eq` y of
420 Nothing -> Left $ error_expr px_ex $
421 Error_Expr_Type_mismatch ast
422 (Exists_Type x)
423 (Exists_Type y)
424 Just Refl -> k Refl