]> 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 qualified Data.Text as Text
18 import Data.Type.Equality ((:~:)(Refl))
19 import GHC.Prim (Constraint)
20
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 :: Expr_From ast ex hs ret
31
32 -- ** Type 'Expr_From'
33 type Expr_From ast ex hs ret
34 = Proxy ex
35 -- ^ Select the 'Expr_from' instance.
36 -> ast
37 -- ^ The input data to parse.
38 -> Context (Lambda_Var (Type_Root_of_Expr ex)) hs
39 -- ^ The bound variables in scope and their types:
40 -- held in the heterogeneous list @hs@,
41 -- from the closest including lambda abstraction to the farest.
42 -> ( forall h
43 . Type_Root_of_Expr ex h
44 -> Forall_Repr_with_Context ex hs h
45 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret )
46 -- ^ The accumulating continuation.
47 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret
48
49 -- | Parse a literal.
50 lit_from
51 :: forall ty lit ex ast hs ret.
52 ( ty ~ Type_Root_of_Expr ex
53 , Read lit
54 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
55 (Error_of_Expr ast (Root_of_Expr ex))
56 ) => (forall repr. Sym_of_Expr ex repr => lit -> repr lit)
57 -> ty lit -> Lambda_Var_Name
58 -> Expr_From ast ex hs ret
59 lit_from lit ty_lit toread ex ast _ctx k =
60 case read_safe toread of
61 Left err -> Left $ error_expr ex $ Error_Expr_Read err ast
62 Right (i::lit) -> k ty_lit $ Forall_Repr_with_Context $ const $ lit i
63
64 -- | Parse a unary operator.
65 op1_from
66 :: forall root ty lit ex ast hs ret.
67 ( ty ~ Type_Root_of_Expr ex
68 , root ~ Root_of_Expr ex
69 , Type_Eq (Type_Root_of_Expr root)
70 , Expr_from ast root
71 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
72 (Error_of_Expr ast root)
73 , Root_of_Expr root ~ root
74 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit)
75 -> ty lit -> ast
76 -> Expr_From ast ex hs ret
77 op1_from op ty_lit ast_x ex ast ctx k =
78 expr_from (Proxy::Proxy root) ast_x ctx $
79 \ty_x (Forall_Repr_with_Context x) ->
80 when_type_eq ex ast ty_lit ty_x $ \Refl ->
81 k ty_x $ Forall_Repr_with_Context (op . x)
82
83 -- | Parse a binary operator.
84 op2_from
85 :: forall root ty lit ex ast hs ret.
86 ( ty ~ Type_Root_of_Expr ex
87 , root ~ Root_of_Expr ex
88 , Type_Eq (Type_Root_of_Expr root)
89 , Expr_from ast root
90 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
91 (Error_of_Expr ast root)
92 , Root_of_Expr root ~ root
93 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit -> repr lit)
94 -> ty lit -> ast -> ast
95 -> Expr_From ast ex hs ret
96 op2_from op ty_lit ast_x ast_y ex ast ctx k =
97 expr_from (Proxy::Proxy root) ast_x ctx $ \ty_x (Forall_Repr_with_Context x) ->
98 expr_from (Proxy::Proxy root) ast_y ctx $ \ty_y (Forall_Repr_with_Context y) ->
99 when_type_eq ex ast ty_lit ty_x $ \Refl ->
100 when_type_eq ex ast ty_lit ty_y $ \Refl ->
101 k ty_x $ Forall_Repr_with_Context $
102 \c -> x c `op` y c
103
104 -- ** Type 'Context'
105
106 -- | GADT for a typing context,
107 -- accumulating an @item@ at each lambda;
108 -- used to accumulate object-types of lambda variables in 'Expr_from'
109 -- or host-terms of lambda variables in 'Repr_Host'.
110 data Context :: (* -> *) -> [*] -> * where
111 Context_Empty :: Context item '[]
112 Context_Next :: item h -> Context item hs -> Context item (h ': hs)
113 infixr 5 `Context_Next`
114
115 -- ** Type 'Lambda_Var'
116 -- | Join a name and a type.
117 --
118 -- This data type is used to handle lambda variables by name
119 -- (instead of DeBruijn indices for instance).
120 data Lambda_Var ty h
121 = Lambda_Var Lambda_Var_Name (ty h)
122 type Lambda_Var_Name = Text
123
124 -- ** Type 'Forall_Repr_with_Context'
125 -- | A data type embedding a universal quantification
126 -- over an interpreter @repr@
127 -- and qualified by the symantics of an expression.
128 --
129 -- Moreover the expression is abstracted by a 'Context'
130 -- built at parsing time to build a /Higher-Order Abstract Syntax/ (HOAS)
131 -- for lambda abstractions.
132 --
133 -- This data type is used to keep a parsed expression polymorphic enough
134 -- to stay interpretable by different interpreters.
135 --
136 -- NOTE: 'Sym_of_Expr'@ ex repr@
137 -- is needed to be able to use symantic methods of the parsed expression
138 -- into a 'Forall_Repr_with_Context'@ ex@.
139 --
140 -- NOTE: 'Sym_of_Expr'@ (@'Root_of_Expr'@ ex) repr@
141 -- is needed to be able to use an expression
142 -- out of a 'Forall_Repr_with_Context'@ (@'Root_of_Expr'@ ex)@
143 -- into a 'Forall_Repr_with_Context'@ ex@,
144 -- which happens when a symantic method includes a polymorphic type
145 -- and thus calls: 'expr_from'@ (Proxy::Proxy (@'Root_of_Expr'@ ex))@.
146 data Forall_Repr_with_Context ex hs h
147 = Forall_Repr_with_Context
148 ( forall repr. ( Sym_of_Expr ex repr
149 , Sym_of_Expr (Root_of_Expr ex) repr
150 ) => Context repr hs -> repr h )
151
152 -- ** Type 'Forall_Repr'
153 data Forall_Repr ex h
154 = Forall_Repr
155 { unForall_Repr :: forall repr
156 . Sym_of_Expr ex repr
157 => repr h }
158
159 -- ** Type family 'Root_of_Expr'
160 -- | The root expression of an expression.
161 type family Root_of_Expr (ex:: *) :: *
162
163 -- ** Type family 'Sym_of_Expr'
164 -- | The symantic of an expression.
165 type family Sym_of_Expr (ex:: *) (repr:: * -> *) :: Constraint
166
167 -- ** Type family 'Error_of_Expr'
168 -- | The error(s) of an expression.
169 type family Error_of_Expr (ast:: *) (ex:: *) :: *
170
171 -- ** Type family 'Type_of_Expr'
172 -- | The type of an expression, parameterized by a root type.
173 type family Type_of_Expr (ex:: *) :: {-root-}(* -> *) -> {-h-}* -> *
174
175 -- ** Type 'Type_Root_of_Expr'
176 -- | Convenient alias.
177 --
178 -- NOTE: include 'Type_Var' only to use it
179 -- within 'Error_Expr_Type_mismatch' so far.
180 type Type_Root_of_Expr (ex:: *)
181 = Type_Root (Type_Alt Type_Var (Type_of_Expr (Root_of_Expr ex)))
182
183 -- * Type 'Expr_Root'
184 -- | The root expression, passing itself as parameter to the given expression.
185 newtype Expr_Root (ex:: * -> *)
186 = Expr_Root (ex (Expr_Root ex))
187 type instance Root_of_Expr (Expr_Root ex) = Expr_Root ex
188 type instance Type_of_Expr (Expr_Root ex)
189 = Type_of_Expr (ex (Expr_Root ex))
190 type instance Error_of_Expr ast (Expr_Root ex)
191 = Error_Expr_Alt (Error_Expr (Error_of_Type ast (Type_Root_of_Expr (ex (Expr_Root ex))))
192 (Type_Root_of_Expr (ex (Expr_Root ex)))
193 ast)
194 (Error_of_Expr ast (ex (Expr_Root ex)))
195 type instance Sym_of_Expr (Expr_Root ex) repr
196 = Sym_of_Expr (ex (Expr_Root ex)) repr
197 instance -- Expr_from
198 ( Expr_from ast (ex (Expr_Root ex))
199 , Root_of_Expr (ex (Expr_Root ex)) ~ Expr_Root ex
200 ) => Expr_from ast (Expr_Root ex) where
201 expr_from _ex ctx ast k =
202 expr_from (Proxy::Proxy (ex (Expr_Root ex)))
203 ctx ast $ \ty (Forall_Repr_with_Context repr) ->
204 k ty (Forall_Repr_with_Context repr)
205
206 {- NOTE: useless code so far.
207 -- ** Class 'Expr_Root_Lift'
208 -- | Lift a given expression to a given root expression.
209 class Expr_Root_Lift ex root where
210 expr_root_lift :: ex root -> root
211 instance
212 Expr_Lift ex root =>
213 Expr_Root_Lift ex (Expr_Root root) where
214 expr_root_lift = Expr_Root . expr_lift
215 -}
216
217 -- * Type 'Expr_Alt'
218 -- | Expression making an alternative between two expressions.
219 data Expr_Alt curr next (root:: *)
220 = Expr_Alt_Curr (curr root)
221 | Expr_Alt_Next (next root)
222 type instance Root_of_Expr (Expr_Alt curr next root) = root
223 type instance Sym_of_Expr (Expr_Alt curr next root) repr
224 = ( Sym_of_Expr (curr root) repr
225 , Sym_of_Expr (next root) repr
226 )
227 type instance Type_of_Expr (Expr_Alt curr next root)
228 = Type_of_Expr_Alt
229 (Type_of_Expr (curr root))
230 (Type_of_Expr (next root))
231 curr next root
232 -- ** Type family 'Type_of_Expr_Alt'
233 -- | Remove 'No_Type' type when building 'Type_of_Expr'.
234 type family Type_of_Expr_Alt
235 (type_curr:: (* -> *) -> * -> *)
236 (type_next:: (* -> *) -> * -> *)
237 curr next root where
238 Type_of_Expr_Alt No_Type type_next curr next root = Type_of_Expr (next root)
239 Type_of_Expr_Alt type_curr No_Type curr next root = Type_of_Expr (curr root)
240 Type_of_Expr_Alt type_curr type_next curr next root
241 = Type_Alt (Type_of_Expr (curr root))
242 (Type_of_Expr (next root))
243
244 type instance Error_of_Expr ast (Expr_Alt curr next root)
245 = Error_of_Expr_Alt ast (curr root) (next root)
246 -- ** Type family 'Error_of_Expr_Alt'
247 -- | Remove 'No_Error_Expr' type when building the error of an expression.
248 type family Error_of_Expr_Alt ast curr next where
249 Error_of_Expr_Alt ast No_Error_Expr next = Error_of_Expr ast next
250 Error_of_Expr_Alt ast curr No_Error_Expr = Error_of_Expr ast curr
251 Error_of_Expr_Alt ast curr next
252 = Error_Expr_Alt (Error_of_Expr ast curr)
253 (Error_of_Expr ast next)
254 -- ** Type 'No_Error_Expr'
255 data No_Error_Expr
256 = No_Error_Expr
257 deriving (Eq, Show)
258
259 instance -- Expr_from
260 ( Expr_from ast (curr root)
261 , Expr_from ast (next root)
262 , Root_of_Expr (curr root) ~ root
263 , Root_of_Expr (next root) ~ root
264 , Error_Expr_Unlift (Error_Expr (Error_of_Type ast (Type_Root_of_Expr root))
265 (Type_Root_of_Expr root) ast)
266 (Error_of_Expr ast root)
267 ) => Expr_from ast (Expr_Alt curr next root) where
268 expr_from _ex ctx ast k =
269 case expr_from (Proxy::Proxy (curr root)) ctx ast $
270 \ty (Forall_Repr_with_Context repr) ->
271 Right $ k ty (Forall_Repr_with_Context repr) of
272 Right ret -> ret
273 Left err ->
274 case error_expr_unlift err of
275 Just (Error_Expr_Unsupported_here _
276 :: Error_Expr (Error_of_Type ast (Type_Root_of_Expr root))
277 (Type_Root_of_Expr root) ast) ->
278 expr_from (Proxy::Proxy (next root)) ctx ast $
279 \ty (Forall_Repr_with_Context repr) ->
280 k ty (Forall_Repr_with_Context repr)
281 _ -> Left err
282
283 {- NOTE: useless code so far.
284 -- ** Type 'Expr_Lift'
285 -- | Apply 'Peano_of_Expr' on 'Expr_LiftN'.
286 type Expr_Lift ex exs
287 = Expr_LiftN (Peano_of_Expr ex exs) ex exs
288
289 -- | Convenient wrapper around 'expr_liftN',
290 -- passing it the 'Peano' number from 'Peano_of_Expr'.
291 expr_lift
292 :: forall ex exs (root:: *).
293 Expr_Lift ex exs =>
294 ex root -> exs root
295 expr_lift = expr_liftN (Proxy::Proxy (Peano_of_Expr ex exs))
296
297 -- *** Type family 'Peano_of_Expr'
298 -- | Return a 'Peano' number derived from the location
299 -- of a given expression within a given expression stack,
300 -- which is used to avoid @OverlappingInstances@.
301 type family Peano_of_Expr
302 (ex:: * -> *)
303 (exs:: * -> *) :: * where
304 Peano_of_Expr ex ex = Zero
305 Peano_of_Expr ex (Expr_Alt ex next) = Zero
306 Peano_of_Expr other (Expr_Alt curr next) = Succ (Peano_of_Expr other next)
307
308 -- *** Class 'Expr_LiftN'
309 -- | Lift a given expression to the top of a given expression stack including it,
310 -- by constructing the appropriate sequence of 'Expr_Alt_Curr' and 'Expr_Alt_Next'.
311 class Expr_LiftN (p:: *) ex exs where
312 expr_liftN :: forall (root:: *). Proxy p -> ex root -> exs root
313 instance Expr_LiftN Zero curr curr where
314 expr_liftN _ = id
315 instance Expr_LiftN Zero curr (Expr_Alt curr next) where
316 expr_liftN _ = Expr_Alt_Curr
317 instance
318 Expr_LiftN p other next =>
319 Expr_LiftN (Succ p) other (Expr_Alt curr next) where
320 expr_liftN _ = Expr_Alt_Next . expr_liftN (Proxy::Proxy p)
321
322 -- ** Type 'Expr_Unlift'
323 -- | Apply 'Peano_of_Expr' on 'Expr_UnliftN'.
324 type Expr_Unlift ex exs
325 = Expr_UnliftN (Peano_of_Expr ex exs) ex exs
326
327 -- | Convenient wrapper around 'expr_unliftN',
328 -- passing it the 'Peano' number from 'Peano_of_Expr'.
329 expr_unlift
330 :: forall ex exs (root:: *).
331 Expr_Unlift ex exs =>
332 exs root -> Maybe (ex root)
333 expr_unlift = expr_unliftN (Proxy::Proxy (Peano_of_Expr ex exs))
334
335 -- *** Class 'Expr_UnliftN'
336 -- | Try to unlift a given expression out of a given expression stack including it,
337 -- by deconstructing the appropriate sequence of 'Expr_Alt_Curr' and 'Expr_Alt_Next'.
338 class Expr_UnliftN (p:: *) ex exs where
339 expr_unliftN :: forall (root:: *). Proxy p -> exs root -> Maybe (ex root)
340 instance Expr_UnliftN Zero curr curr where
341 expr_unliftN _ = Just
342 instance Expr_UnliftN Zero curr (Expr_Alt curr next) where
343 expr_unliftN _ (Expr_Alt_Curr x) = Just x
344 expr_unliftN _ (Expr_Alt_Next _) = Nothing
345 instance
346 Expr_UnliftN p other next =>
347 Expr_UnliftN (Succ p) other (Expr_Alt curr next) where
348 expr_unliftN _ (Expr_Alt_Next x) = expr_unliftN (Proxy::Proxy p) x
349 expr_unliftN _ (Expr_Alt_Curr _) = Nothing
350 -}
351
352 -- ** Type family 'Is_Last_Expr'
353 -- | Return whether a given expression is the last one in a given expression stack.
354 --
355 -- NOTE: each expression parser uses this type family
356 -- when it encounters unsupported syntax:
357 -- to know if it is the last expression parser component that will be tried
358 -- (and thus return 'Error_Expr_Unsupported')
359 -- or if some other expression parser component shall be tried
360 -- (and thus return 'Error_Expr_Unsupported_here',
361 -- which is then handled accordingly by the 'Expr_from' instance of 'Expr_Alt').
362 type family Is_Last_Expr (ex:: *) (exs:: *) :: Bool where
363 Is_Last_Expr ex ex = 'True
364 Is_Last_Expr ex (Expr_Root exs) = Is_Last_Expr ex (exs (Expr_Root exs))
365 Is_Last_Expr (ex root) (Expr_Alt ex next root) = 'False
366 Is_Last_Expr other (Expr_Alt curr next root) = Is_Last_Expr other (next root)
367
368 -- * Type 'Error_Expr_Alt'
369 -- | Error expression making an alternative between two error expressions.
370 data Error_Expr_Alt curr next
371 = Error_Expr_Alt_Curr curr
372 | Error_Expr_Alt_Next next
373 deriving (Eq, Show)
374
375 -- ** Type 'Error_Expr_Lift'
376 -- | Apply 'Peano_of_Error_Expr' on 'Error_Expr_LiftN'.
377 type Error_Expr_Lift err errs
378 = Error_Expr_LiftN (Peano_of_Error_Expr err errs) err errs
379
380 -- | Convenient wrapper around 'error_expr_liftN',
381 -- passing it the 'Peano' number from 'Peano_of_Error_Expr'.
382 error_expr_lift
383 :: forall err errs.
384 Error_Expr_Lift err errs => err -> errs
385 error_expr_lift = error_expr_liftN (Proxy::Proxy (Peano_of_Error_Expr err errs))
386
387 -- *** Type family 'Peano_of_Error_Expr'
388 -- | Return a 'Peano' number derived from the location
389 -- of a given error expression within a given error expression stack,
390 -- which is used to avoid @OverlappingInstances@.
391 type family Peano_of_Error_Expr (err:: *) (errs:: *) :: * where
392 Peano_of_Error_Expr err err = Zero
393 Peano_of_Error_Expr err (Error_Expr_Alt err next) = Zero
394 Peano_of_Error_Expr other (Error_Expr_Alt curr next) = Succ (Peano_of_Error_Expr other next)
395
396 -- *** Class 'Error_Expr_LiftN'
397 -- | Lift a given expression to the top of a given expression stack including it,
398 -- by constructing the appropriate sequence of 'Error_Expr_Alt_Curr' and 'Error_Expr_Alt_Next'.
399 class Error_Expr_LiftN (p:: *) err errs where
400 error_expr_liftN :: Proxy p -> err -> errs
401 instance Error_Expr_LiftN Zero curr curr where
402 error_expr_liftN _ = id
403 instance Error_Expr_LiftN Zero curr (Error_Expr_Alt curr next) where
404 error_expr_liftN _ = Error_Expr_Alt_Curr
405 instance
406 Error_Expr_LiftN p other next =>
407 Error_Expr_LiftN (Succ p) other (Error_Expr_Alt curr next) where
408 error_expr_liftN _ = Error_Expr_Alt_Next . error_expr_liftN (Proxy::Proxy p)
409
410 -- ** Type 'Error_Expr_Unlift'
411 -- | Apply 'Peano_of_Error_Expr' on 'Error_Expr_UnliftN'.
412 type Error_Expr_Unlift ex exs
413 = Error_Expr_UnliftN (Peano_of_Error_Expr ex exs) ex exs
414
415 -- | Convenient wrapper around 'error_expr_unliftN',
416 -- passing it the 'Peano' number from 'Peano_of_Error_Expr'.
417 error_expr_unlift
418 :: forall ex exs.
419 Error_Expr_Unlift ex exs => exs -> Maybe ex
420 error_expr_unlift = error_expr_unliftN (Proxy::Proxy (Peano_of_Error_Expr ex exs))
421
422 -- *** Class 'Error_Expr_UnliftN'
423 -- | Try to unlift a given expression error out of a given expression error stack including it,
424 -- by deconstructing the appropriate sequence of 'Error_Expr_Alt_Curr' and 'Error_Expr_Alt_Next'.
425 class Error_Expr_UnliftN (p:: *) ex exs where
426 error_expr_unliftN :: Proxy p -> exs -> Maybe ex
427 instance Error_Expr_UnliftN Zero curr curr where
428 error_expr_unliftN _ = Just
429 instance Error_Expr_UnliftN Zero curr (Error_Expr_Alt curr next) where
430 error_expr_unliftN _ (Error_Expr_Alt_Curr x) = Just x
431 error_expr_unliftN _ (Error_Expr_Alt_Next _) = Nothing
432 instance
433 Error_Expr_UnliftN p other next =>
434 Error_Expr_UnliftN (Succ p) other (Error_Expr_Alt curr next) where
435 error_expr_unliftN _ (Error_Expr_Alt_Next x) = error_expr_unliftN (Proxy::Proxy p) x
436 error_expr_unliftN _ (Error_Expr_Alt_Curr _) = Nothing
437
438 -- * Type 'Error_Expr_Read'
439 -- | Common expression errors.
440 data Error_Expr err_ty ty ast
441 = Error_Expr_Wrong_number_of_arguments ast Int
442 -- ^ Wrong number of arguments applied to a term,
443 -- the integer is the number of arguments expected.
444 | Error_Expr_Type_mismatch ast (Exists_Type ty) (Exists_Type ty)
445 -- ^ Mismatch between respectively expected and actual type.
446 | Error_Expr_Read Error_Read ast
447 -- ^ Error when reading a literal.
448 | Error_Expr_Type err_ty ast
449 -- ^ Error when parsing a type.
450 | Error_Expr_Unsupported ast
451 -- ^ Given syntax is supported by none
452 -- of the expression parser components
453 -- of the expression stack.
454 | Error_Expr_Unsupported_here ast
455 -- ^ Given syntax not supported by
456 -- the current expression parser component.
457 deriving (Eq, Show)
458
459 -- | Convenient wrapper around 'error_expr_lift',
460 -- passing the type family boilerplate.
461 error_expr
462 :: forall ast ex ty.
463 (ty ~ Type_Root_of_Expr ex)
464 => Error_Expr_Lift
465 (Error_Expr (Error_of_Type ast ty) ty ast)
466 (Error_of_Expr ast (Root_of_Expr ex))
467 => Proxy ex
468 -> Error_Expr (Error_of_Type ast ty) ty ast
469 -> Error_of_Expr ast (Root_of_Expr ex)
470 error_expr _ = error_expr_lift
471
472 -- | Utility to return 'Error_Expr_Unsupported'
473 -- or 'Error_Expr_Unsupported_here'
474 -- according to the given expression.
475 error_expr_unsupported
476 :: forall ast ex ty root.
477 ( root ~ Root_of_Expr ex
478 , ty ~ Type_Root_of_Expr ex
479 , Implicit_HBool (Is_Last_Expr ex root)
480 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
481 (Error_of_Expr ast root)
482 ) => Proxy ex -> ast
483 -> Error_of_Expr ast (Root_of_Expr ex)
484 error_expr_unsupported ex ast =
485 case hbool :: HBool (Is_Last_Expr ex root) of
486 HTrue -> error_expr ex $ Error_Expr_Unsupported ast
487 HFalse -> error_expr ex $ Error_Expr_Unsupported_here ast
488
489 -- | Utility to check that two types are equal
490 -- or raise 'Error_Expr_Type_mismatch'.
491 when_type_eq
492 :: forall ast ex root ty x y ret.
493 ( root ~ Root_of_Expr ex
494 , ty ~ Type_Root_of_Expr ex
495 , Type_Eq ty
496 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
497 (Error_of_Expr ast root)
498 )
499 => Proxy ex -> ast -> ty x -> ty y
500 -> (forall. x :~: y -> Either (Error_of_Expr ast root) ret)
501 -> Either (Error_of_Expr ast root) ret
502 when_type_eq ex ast x y k =
503 case x `type_eq` y of
504 Nothing -> Left $ error_expr ex $
505 Error_Expr_Type_mismatch ast
506 (Exists_Type x)
507 (Exists_Type y)
508 Just Refl -> k Refl
509
510 -- * Type 'Error_Read'
511 -- | Error parsing a host-term.
512 data Error_Read
513 = Error_Read Text
514 deriving (Eq, Show)
515
516 -- | Parse a host-term.
517 read_safe :: Read h => Text -> Either Error_Read h
518 read_safe t =
519 case reads $ Text.unpack t of
520 [(x, "")] -> Right x
521 _ -> Left $ Error_Read t