1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE DefaultSignatures #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE FlexibleInstances #-}
7 {-# LANGUAGE KindSignatures #-}
8 {-# LANGUAGE MultiParamTypeClasses #-}
9 {-# LANGUAGE OverloadedStrings #-}
10 {-# LANGUAGE PolyKinds #-}
11 {-# LANGUAGE Rank2Types #-}
12 {-# LANGUAGE ScopedTypeVariables #-}
13 {-# LANGUAGE StandaloneDeriving #-}
14 {-# LANGUAGE TypeFamilies #-}
15 {-# LANGUAGE TypeInType #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE UndecidableInstances #-}
18 {-# OPTIONS_GHC -fno-warn-orphans #-}
19 module Language.Symantic.Compiling.Term where
21 import qualified Data.Function as Fun
22 import qualified Data.Kind as Kind
23 import Data.Kind hiding (Type)
24 import Data.Monoid ((<>))
25 import Data.Proxy (Proxy(..))
26 import Data.String (IsString)
27 import Data.Text (Text)
28 import qualified Data.Text as Text
29 import Data.Type.Equality ((:~:)(..))
30 import GHC.Exts (Constraint)
31 import Prelude hiding (not)
33 import Language.Symantic.Lib.Data.Type.List
34 import Language.Symantic.Typing
35 import Language.Symantic.Interpreting
36 import Language.Symantic.Transforming.Trans
39 -- | 'TermLC' applied on a 'LamCtx_Type'.
42 (forall term. ( Sym_of_Ifaces is term
47 -- | A data type embedding a universal quantification
48 -- over an interpreter @term@
49 -- and qualified by the symantics of a term.
51 -- Moreover the term is abstracted by a 'LamCtx_Term'
52 -- built top-down at parsing time
53 -- to build a /Higher-Order Abstract Syntax/ (HOAS)
54 -- for /lambda abstractions/ ('lam').
56 -- This data type is used to keep a parsed term polymorphic enough
57 -- to stay interpretable by different interpreters.
59 -- * @(@'Sym_of_Ifaces'@ is term)@
60 -- is needed when a symantic method includes a polymorphic type
61 -- and thus calls: 'term_from'.
63 -- * @(@'Sym_of_Ifaces'@ ls term)@ and @(@'Sym_of_Ifaces'@ rs term)@
64 -- are needed to be able to write the instance:
65 -- @(@'Term_fromR'@ is ls (i ': rs) ast)@.
67 -- * @(@'Sym_Lambda'@ term)@
68 -- is needed to be able to parse partially applied functions
69 -- (when their type is knowable).
70 data TermLC ctx h is ls rs
72 (forall term. ( Sym_of_Ifaces is term
73 , Sym_of_Ifaces ls term
74 , Sym_of_Ifaces rs term
76 ) => LamCtx_Term term ctx -> term h)
79 -- | Existential 'Term', with its 'Type'.
81 = forall (h::Kind.Type). ETerm
82 (Type (Consts_of_Ifaces is) h)
85 -- | Like 'term_from' but for a term with an empty /lambda context/.
88 => ast -> Either (Error_Term is ast) (ETerm is)
90 term_from ast LamCtx_TypeZ $ \ty (TermLC te) ->
91 Right $ ETerm ty $ Term $ te LamCtx_TermZ
94 -- | Convenient type synonym wrapping 'Term_fromR'
95 -- to initiate its recursion.
96 class Term_from is ast where
97 term_from :: Term_fromT ast ctx ret is '[] is
101 , Term_fromR is '[] is ast
102 ) => Term_from is ast where
103 term_from ast ctx k =
104 case replace_var (ast_lexem ast) ctx k of
105 Left Error_Term_unsupported -> term_fromR ast ctx k
111 -> LamCtx_Type is (Lexem ast) lc
113 Type (Consts_of_Ifaces is) (h::Kind.Type)
114 -> TermLC lc h is '[] is
115 -> Either (Error_Term is ast) ret )
116 -> Either (Error_Term is ast) ret
117 replace_var name lc k' =
119 LamCtx_TypeZ -> Left $ Error_Term_unsupported
120 LamCtx_TypeS n ty _ | n == name ->
121 k' ty $ TermLC $ \(te `LamCtx_TermS` _) -> te
122 LamCtx_TypeS _n _ty lc' ->
123 replace_var name lc' $ \ty (TermLC te::TermLC lc' h is '[] is) ->
124 k' ty $ TermLC $ \(_ `LamCtx_TermS` c) -> te c
126 -- ** Type 'Term_fromT'
127 -- | Convenient type synonym defining a term parser.
128 type Term_fromT ast ctx ret is ls rs
130 -> LamCtx_Type is (Lexem ast) ctx
131 -- ^ The bound variables in scope and their types:
132 -- built top-down in the heterogeneous list @ctx@,
133 -- from the closest including /lambda abstraction/ to the farest.
135 Type (Consts_of_Ifaces is) (h::Kind.Type)
136 -> TermLC ctx h is ls rs
137 -> Either (Error_Term is ast) ret )
138 -- ^ The accumulating continuation called bottom-up.
139 -> Either (Error_Term is ast) ret
141 -- ** Type 'Term_fromIT'
142 -- | Almost like 'Term_fromT', but with the remaining 'ast_nodes'
143 -- given to the continuation so that 'term_apply' can be used within
144 -- the instance 'Term_fromR'@ is ls (i ': rs) ast@.
145 type Term_fromIT ast ctx ret is ls rs
147 -> LamCtx_Type is (Lexem ast) ctx
150 -> Type (Consts_of_Ifaces is) (h::Kind.Type)
151 -> TermLC ctx h is ls rs
152 -> Either (Error_Term is ast) ret )
153 -> Either (Error_Term is ast) ret
155 -- ** Class 'Term_fromR'
156 -- | Intermediate type class to construct an instance of 'Term_from'
157 -- from many instances of 'Term_fromI', one for each item of @is@.
159 -- * @is@: starting list of /term constants/.
160 -- * @ls@: done list of /term constants/.
161 -- * @rs@: remaining list of /term constants/.
162 class Term_fromR (is::[*]) (ls::[*]) (rs::[*]) ast where
163 term_fromR :: Term_fromT ast ctx ret is ls rs
164 default term_fromR :: AST ast => Term_fromT ast ctx ret is ls rs
165 term_fromR ast _ctx _k =
166 Left $ Error_Term_unknown $
167 At (Just ast) $ ast_lexem ast
169 -- | Test whether @i@ handles the work of 'Term_from' or not,
170 -- or recurse on @rs@, preserving the starting list of /term constants/,
171 -- and keeping a list of those done to construct 'TermLC'.
173 ( Term_fromI is i ast
174 , Term_fromR is (i ': ls) rs ast
176 , Inj_Const (Consts_of_Ifaces is) (->)
177 ) => Term_fromR is ls (i ': rs) ast where
178 term_fromR ast ctx k =
179 case term_fromI ast ctx $ \as ty (TermLC te
180 ::TermLC ctx h is ls (i ': rs)) ->
181 term_apply ast as ctx ty (TermLC te) k of
182 Left Error_Term_unsupported ->
183 term_fromR ast ctx $ \ty (TermLC te
184 ::TermLC ctx h is (i ': ls) rs) ->
186 ::TermLC ctx h is ls (i ': rs))
188 -- | End the recursion.
189 instance AST ast => Term_fromR is ls '[] ast
191 -- | Apply the given @ast@s to the given 'TermLC',
192 -- or return an 'Error_Term'.
195 , Inj_Const (Consts_of_Ifaces is) (->)
197 -> LamCtx_Type is (Lexem ast) ctx
198 -> Type (Consts_of_Ifaces is) (h::Kind.Type)
199 -> TermLC ctx h is ls rs
201 Type (Consts_of_Ifaces is) (h' ::Kind.Type)
202 -> TermLC ctx h' is ls rs
203 -> Either (Error_Term is ast) ret )
204 -> Either (Error_Term is ast) ret
205 term_apply ast_f args ctx ty_f te_f@(TermLC f) k =
209 term_from ast_a ctx $ \ty_a (TermLC a) ->
210 check_type2 tyFun ast_f ty_f $ \Refl ty_arg ty_res ->
211 check_type (At (Just ast_f) ty_arg) (At (Just ast_a) ty_a) $ \Refl ->
212 term_apply ast_f as ctx ty_res (TermLC $ \c -> f c .$ a c) k
214 -- ** Class 'Term_fromI'
215 -- | Handle the work of 'Term_from' for a given /interface/ @i@,
216 -- that is: maybe it handles the given /interface/,
217 -- and if so, maybe the term can be parsed.
218 class Term_fromI (is::[*]) (i:: *) ast where
219 term_fromI :: Term_fromIT ast ctx ret is ls (i ': rs)
220 term_fromI _ast _ctx _k = Left $ Error_Term_unsupported
222 -- * Type family 'Sym_of_Ifaces'
223 type family Sym_of_Ifaces (is::[*]) (term:: * -> *) :: Constraint where
224 Sym_of_Ifaces '[] term = ()
225 Sym_of_Ifaces (i ': is) term = (Sym_of_Iface i term, Sym_of_Ifaces is term)
227 -- ** Type family 'Sym_of_Iface'
228 type family Sym_of_Iface (i:: *) :: {-term-}(* -> *) -> Constraint
230 -- * Type 'Consts_of_Ifaces'
231 type Consts_of_Ifaces is = Nub (Consts_of_IfaceR is)
233 -- ** Type family 'Consts_of_IfaceR'
234 type family Consts_of_IfaceR (is::[*]) where
235 Consts_of_IfaceR '[] = '[]
236 Consts_of_IfaceR (i ': is) = Consts_of_Iface i ++ Consts_of_IfaceR is
238 -- ** Type family 'Consts_of_Iface'
239 type family Consts_of_Iface (i::k) :: [*]
240 type instance Consts_of_Iface (Proxy Enum) = Proxy Enum ': Consts_imported_by Enum
242 -- * Type 'LamCtx_Type'
243 -- | GADT for a typing context,
244 -- accumulating an @item@ at each lambda;
245 -- used to accumulate object-types (in 'Expr_From')
246 -- or host-terms (in 'HostI')
247 -- associated with the 'LamVar's in scope.
248 data LamCtx_Type (is::[*]) (name:: *) (ctx::[*]) where
249 LamCtx_TypeZ :: LamCtx_Type is name '[]
251 -> Type (Consts_of_Ifaces is) (h::Kind.Type)
252 -> LamCtx_Type is name hs
253 -> LamCtx_Type is name (h ': hs)
254 infixr 5 `LamCtx_TypeS`
256 -- ** Type 'LamVarName'
257 type LamVarName = Text
259 -- * Type 'LamCtx_Term'
260 data LamCtx_Term (term:: * -> *) (ctx::[*]) where
261 LamCtx_TermZ :: LamCtx_Term term '[]
262 LamCtx_TermS :: term h
263 -> LamCtx_Term term hs
264 -> LamCtx_Term term (h ': hs)
265 infixr 5 `LamCtx_TermS`
267 -- * Type 'Error_Term'
268 data Error_Term (is::[*]) ast
269 = Error_Term_unknown (At ast (Lexem ast))
270 | Error_Term_unsupported
271 | Error_Term_Syntax (Error_Syntax ast)
272 | Error_Term_Constraint_not_deductible (At ast (KType (Consts_of_Ifaces is) Constraint))
273 | Error_Term_Family_instance_missing (At ast ())
274 | Error_Term_Typing (At ast (Error_Type (Consts_of_Ifaces is) ast))
275 | Error_Term_Type_mismatch (At ast (EType (Consts_of_Ifaces is)))
276 (At ast (EType (Consts_of_Ifaces is)))
277 | Error_Term_Type_is_not_an_application (At ast (EType (Consts_of_Ifaces is)))
278 deriving instance (Eq ast, Eq (Lexem ast)) => Eq (Error_Term is ast)
279 deriving instance (Show ast, Show (Lexem ast), Show_Const (Consts_of_Ifaces is)) => Show (Error_Term is ast)
281 instance Lift_Error_Syntax (Error_Term is) where
282 lift_error_syntax = Error_Term_Syntax
286 -- | Check that the 'kind_of' a given 'Type's equals a given kind,
287 -- or fail with 'Error_Type_Kind_mismatch'.
290 -> At ast (Type (Consts_of_Ifaces is) (t::kt))
291 -> ((k :~: kt) -> Either (Error_Term is ast) ret)
292 -> Either (Error_Term is ast) ret
293 check_kind ast ki (At at_ty ty) k =
294 let ki_ty = kind_of ty in
295 case eq_skind ki ki_ty of
297 Nothing -> Left $ Error_Term_Typing $
299 Error_Type_Kind_mismatch
300 (At Nothing $ EKind SKiType)
301 (At at_ty $ EKind ki_ty)
303 -- | Check that a given 'Type' is a /type application/,
304 -- or fail with 'Error_Term_Type_is_not_an_application'.
306 :: ast -> Type (Consts_of_Ifaces is) (fx::kfx)
307 -> (forall kx f x. (fx :~: f x)
308 -> Type (Consts_of_Ifaces is) (f::kx -> kfx)
309 -> Type (Consts_of_Ifaces is) (x::kx)
310 -> Either (Error_Term is ast) ret)
311 -> Either (Error_Term is ast) ret
312 check_app ast ty_fx k =
314 ty_f :$ ty_x -> k Refl ty_f ty_x
315 _ -> Left $ Error_Term_Type_is_not_an_application $
316 At (Just ast) $ EType ty_fx
318 -- | Check that two given 'Type's are equal,
319 -- or fail with 'Error_Term_Type_mismatch'.
321 :: At ast (Type (Consts_of_Ifaces is) x)
322 -> At ast (Type (Consts_of_Ifaces is) y)
323 -> ((x :~: y) -> Either (Error_Term is ast) ret)
324 -> Either (Error_Term is ast) ret
325 check_type (At at_x x) (At at_y y) k =
328 Nothing -> Left $ Error_Term_Type_mismatch
332 -- | Convenient wrapper to check for a 'Type' of kind: @* -> *@.
334 :: Type (Consts_of_Ifaces is) ty
336 -> Type (Consts_of_Ifaces is) (fx:: *)
337 -> (forall x. (fx :~: ty x)
338 -> Type (Consts_of_Ifaces is) (x:: *)
339 -> Either (Error_Term is ast) ret)
340 -> Either (Error_Term is ast) ret
341 check_type1 ty ast_ta ty_ta k =
342 check_app ast_ta ty_ta $ \Refl ty_ta_t ty_ta_a ->
343 check_kind ast_ta (SKiType `SKiArrow` SKiType) (At (Just ast_ta) ty_ta_t) $ \Refl ->
344 check_type (At Nothing ty) (At (Just ast_ta) ty_ta_t) $ \Refl ->
347 -- | Convenient wrapper to check for a 'Type' of kind: @* -> * -> *@.
349 :: Type (Consts_of_Ifaces is) (ty:: * -> * -> *)
350 -> ast -> Type (Consts_of_Ifaces is) a2b
351 -> (forall a b. (a2b :~: (ty a b))
352 -> Type (Consts_of_Ifaces is) a
353 -> Type (Consts_of_Ifaces is) b
354 -> Either (Error_Term is ast) ret)
355 -> Either (Error_Term is ast) ret
356 check_type2 ty ast ty_a2b k =
357 check_app ast ty_a2b $ \Refl ty_a2b_a2 ty_a2b_b ->
358 check_app ast ty_a2b_a2 $ \Refl ty_a2b_ty ty_a2b_a ->
359 check_kind ast (SKiType `SKiArrow` SKiType `SKiArrow` SKiType) (At (Just ast) ty_a2b_ty) $ \Refl ->
360 check_type (At Nothing ty) (At (Just ast) ty_a2b_ty) $ \Refl ->
361 k Refl ty_a2b_a ty_a2b_b
363 -- | Check that a given 'Constraint' holds,
364 -- or fail with 'Error_Term_Constraint_not_deductible'.
366 :: Proj_Con (Consts_of_Ifaces is)
367 => At ast (Type (Consts_of_Ifaces is) (q::Constraint))
368 -> (Con q -> Either (Error_Term is ast) ret)
369 -> Either (Error_Term is ast) ret
370 check_constraint (At at_q q) k =
373 Nothing -> Left $ Error_Term_Constraint_not_deductible $
376 -- | Convenient wrapper to check for a 'Constraint'
377 -- over a 'Type' of kind: @* -> *@.
379 :: Proj_Con (Consts_of_Ifaces is)
380 => Type (Consts_of_Ifaces is) con
381 -> ast -> Type (Consts_of_Ifaces is) (fx:: *)
382 -> (forall f x. (fx :~: f x)
384 -> Type (Consts_of_Ifaces is) (f:: * -> *)
385 -> Type (Consts_of_Ifaces is) (x:: *)
386 -> Either (Error_Term is ast) ret)
387 -> Either (Error_Term is ast) ret
388 check_constraint1 con ast_ta ty_ta k =
389 check_app ast_ta ty_ta $ \Refl ty_ta_t ty_ta_a ->
390 check_kind ast_ta (SKiType `SKiArrow` SKiType) (At (Just ast_ta) ty_ta_t) $ \Refl ->
391 check_constraint (At (Just ast_ta) (con :$ ty_ta_t)) $ \Con ->
392 k Refl Con ty_ta_t ty_ta_a
394 -- | Check that a given /type family/ is supported,
395 -- or fail with 'Error_Term_Family_instance_missing'.
397 :: Proj_Fam (Consts_of_Ifaces is) fam
399 -> At ast (Type (Consts_of_Ifaces is) h)
400 -> (Type (Consts_of_Ifaces is) (Fam fam h) -> Either (Error_Term is ast) ret)
401 -> Either (Error_Term is ast) ret
402 check_family fam (At at_ty ty) k =
403 case proj_fam fam ty of
405 Nothing -> Left $ Error_Term_Family_instance_missing $
408 -- * Class 'Sym_Lambda'
409 class Sym_Lambda term where
410 -- | /Lambda abstraction/.
411 lam :: (term arg -> term res) -> term ((->) arg res)
412 default lam :: Trans t term
413 => (t term arg -> t term res)
414 -> t term ((->) arg res)
415 lam f = trans_lift $ lam $ trans_apply . f . trans_lift
417 -- | /Lambda application/.
418 (.$) :: term ((->) arg res) -> term arg -> term res
419 default (.$) :: Trans t term
420 => t term ((->) arg res) -> t term arg -> t term res
421 (.$) f x = trans_lift (trans_apply f .$ trans_apply x)
423 -- | Convenient 'lam' and '.$' wrapper.
424 let_ :: term var -> (term var -> term res) -> term res
425 let_ x y = lam y .$ x
427 id :: term a -> term a
428 id a = lam Fun.id .$ a
430 const :: term a -> term b -> term a
431 const a b = lam (lam . Fun.const) .$ a .$ b
433 -- | /Lambda composition/.
434 (#) :: term (b -> c) -> term (a -> b) -> term (a -> c)
435 (#) f g = lam $ \a -> f .$ (g .$ a)
437 flip :: term (a -> b -> c) -> term (b -> a -> c)
438 flip f = lam $ \b -> lam $ \a -> f .$ a .$ b
443 type instance Sym_of_Iface (Proxy (->)) = Sym_Lambda
444 type instance Consts_of_Iface (Proxy (->)) = Proxy (->) ': Consts_imported_by (->)
445 type instance Consts_imported_by (->) =
452 instance Sym_Lambda HostI where
453 lam f = HostI (unHostI . f . HostI)
455 instance Sym_Lambda TextI where
456 lam f = TextI $ \p v ->
457 let p' = Precedence 1 in
458 let x = "x" <> Text.pack (show v) in
459 paren p p' $ "\\" <> x <> " -> "
460 <> unTextI (f (TextI $ \_p _v -> x)) p' (succ v)
461 -- (.$) = textI_infix "$" (Precedence 0)
462 (.$) (TextI a1) (TextI a2) =
464 let p' = precedence_App in
465 paren p p' $ a1 p' v <> " " <> a2 p' v
468 let p' = Precedence 2 in
469 let x = "x" <> Text.pack (show v) in
470 paren p p' $ "let" <> " " <> x <> " = "
471 <> unTextI e (Precedence 0) (succ v) <> " in "
472 <> unTextI (in_ (TextI $ \_p _v -> x)) p' (succ v)
473 (#) = textI_infix "." (Precedence 9)
475 const = textI_app2 "const"
476 flip = textI_app1 "flip"
477 instance (Sym_Lambda r1, Sym_Lambda r2) => Sym_Lambda (DupI r1 r2) where
478 lam f = dupI_1 lam_f `DupI` dupI_2 lam_f
480 (.$) = dupI2 (Proxy::Proxy Sym_Lambda) (.$)
482 instance Const_from Text cs => Const_from Text (Proxy (->) ': cs) where
483 const_from "(->)" k = k (ConstZ kind)
484 const_from s k = const_from s $ k . ConstS
485 instance Show_Const cs => Show_Const (Proxy (->) ': cs) where
486 show_const ConstZ{} = "(->)"
487 show_const (ConstS c) = show_const c
489 instance -- Proj_ConC (->)
491 , Proj_Consts cs (Consts_imported_by (->))
493 ) => Proj_ConC cs (Proxy (->)) where
494 proj_conC _ (TyConst q :$ (TyConst c :$ _r))
495 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
496 , Just Refl <- proj_const c (Proxy::Proxy (->))
498 _ | Just Refl <- proj_const q (Proxy::Proxy Functor) -> Just Con
499 | Just Refl <- proj_const q (Proxy::Proxy Applicative) -> Just Con
500 | Just Refl <- proj_const q (Proxy::Proxy Monad) -> Just Con
502 proj_conC _ (t@(TyConst q) :$ (TyConst c :$ _a :$ b))
503 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
504 , Just Refl <- proj_const c (Proxy::Proxy (->))
506 _ | Just Refl <- proj_const q (Proxy::Proxy Monoid)
507 , Just Con <- proj_con (t :$ b) -> Just Con
509 proj_conC _c _q = Nothing
510 instance -- Term_fromI (->)
512 , Lexem ast ~ LamVarName
513 , Inj_Const (Consts_of_Ifaces is) (->)
514 , Const_from (Lexem ast) (Consts_of_Ifaces is)
516 ) => Term_fromI is (Proxy (->)) ast where
517 term_fromI ast ctx k =
518 case ast_lexem ast of
519 "\\" -> -- lambda abstration
520 from_ast3 ast $ \ast_name ast_ty_arg ast_body as ->
521 either (Left . Error_Term_Typing . At (Just ast)) Fun.id $
522 type_from ast_ty_arg $ \ty_arg -> Right $
523 check_kind ast SKiType (At (Just ast) ty_arg) $ \Refl ->
525 (LamCtx_TypeS (ast_lexem ast_name) ty_arg ctx) $
526 \ty_res (TermLC res) ->
527 k as (ty_arg ~> ty_res) $ TermLC $
529 res (arg `LamCtx_TermS` c)
530 " " -> -- lambda application
531 from_ast2 ast $ \ast_lam ast_arg_actual as ->
532 term_from ast_lam ctx $ \ty_lam (TermLC lam_) ->
533 term_from ast_arg_actual ctx $ \ty_arg_actual (TermLC arg_actual) ->
534 check_type2 tyFun ast_lam ty_lam $ \Refl ty_arg ty_res ->
536 (At (Just ast_lam) ty_arg)
537 (At (Just ast_arg_actual) ty_arg_actual) $ \Refl ->
538 k as ty_res $ TermLC $
539 \c -> lam_ c .$ arg_actual c
541 from_ast3 ast $ \ast_name ast_bound ast_body as ->
542 term_from ast_bound ctx $ \ty_bound (TermLC bound) ->
543 term_from ast_body (LamCtx_TypeS (ast_lexem ast_name) ty_bound ctx) $
544 \ty_res (TermLC res) ->
545 k as ty_res $ TermLC $
546 \c -> let_ (bound c) $ \arg -> res (arg `LamCtx_TermS` c)
547 _ -> Left $ Error_Term_unsupported
549 -- | The '(->)' 'Type'
550 tyFun :: Inj_Const cs (->) => Type cs (->)
551 tyFun = TyConst inj_const
553 -- | The function 'Type' @(->)@,
554 -- with an infix notation more readable.
555 (~>) :: forall cs a b. Inj_Const cs (->)
556 => Type cs a -> Type cs b -> Type cs (a -> b)
557 (~>) a b = tyFun :$ a :$ b
560 syFun :: IsString a => [Syntax a] -> Syntax a
561 syFun = Syntax "(->)"
563 (.>) :: IsString a => Syntax a -> Syntax a -> Syntax a
564 a .> b = syFun [a, b]
567 syLam :: IsString a => Syntax a -> Syntax a -> Syntax a -> Syntax a
568 syLam arg ty body = Syntax "\\" [arg, ty, body]
570 syApp :: IsString a => Syntax a -> Syntax a -> Syntax a
571 syApp lam_ arg = Syntax " " [lam_, arg]
574 syLet :: IsString a => Syntax a -> Syntax a -> Syntax a -> Syntax a
575 syLet name bound body = Syntax "let" [name, bound, body]
580 -- | Parsing utility to check that the type resulting
581 -- from the application of a given type family to a given type
582 -- is within the type stack,
583 -- or raise 'Error_Term_Type_mismatch'.
585 :: forall ast is tf root ty h ret.
586 ( root ~ Root_of_Expr is
587 , ty ~ Type_Root_of_Expr is
589 , Error_Term_Lift (Error_Term (Error_of_Type ast ty) ty ast)
590 (Error_of_Expr ast root)
591 ) => Proxy tf -> Proxy is -> ast -> ty h
592 -> (ty (Host_of_Type0_Family tf h) -> Either (Error_of_Expr ast root) ret)
593 -> Either (Error_of_Expr ast root) ret
594 check_type0_family tf is ast ty k =
595 case type0_family tf ty of
597 Nothing -> Left $ error_expr is $
598 Error_Term_Type (error_type_lift $ Error_Type_No_Type_Family ast) ast