1 {-# LANGUAGE ConstraintKinds #-}
3 {-# LANGUAGE PolyKinds #-}
4 {-# LANGUAGE UndecidableInstances #-}
5 {-# LANGUAGE UndecidableSuperClasses #-}
6 {-# OPTIONS_GHC -fconstraint-solver-iterations=7 #-}
7 module Language.Symantic.Typing.Constraint where
9 import Control.Applicative ((<|>))
12 import Language.Symantic.Parsing
13 import Language.Symantic.Typing.Type
14 import Language.Symantic.Typing.Constant
17 -- | Captures the proof of a 'Constraint'
18 -- (and its dictionaries when it contains type classes):
19 -- pattern matching on the 'Dict' constructor
20 -- brings the 'Constraint' into scope.
24 -- * Type 'Proj_TyCon'
25 -- | Convenient type synonym wrapping 'Proj_TyConR'
26 -- to initiate its recursion.
27 type Proj_TyCon cs = Proj_TyConR cs cs
29 -- | Project the 'Constraint' indexed by the given 'Type'
30 -- onto its proof, captured by 'TyCon' when it holds.
32 :: forall src ctx ss cs q.
35 => Type src ctx ss cs q
37 proj_TyCon = proj_TyConR (Proxy @cs)
39 -- ** Class 'Proj_TyConR'
40 -- | Intermediate type class to construct an instance of 'Proj_TyCon'
41 -- from many instances of 'Proj_TyConC', one for each item of @cs@.
43 -- * @cs@: starting list of /type constants/.
44 -- * @rs@: remaining list of /type constants/.
45 class Proj_TyConR cs rs where
46 proj_TyConR :: Source src => Proxy rs -> Type src ctx ss cs q -> Maybe (TyCon q)
47 proj_TyConR _rs _q = Nothing
49 -- | Test whether @c@ handles the work of 'Proj_TyCon' or not,
50 -- or recurse on @rs@, preserving the starting list of /type constants/.
54 ) => Proj_TyConR cs (Proxy c ': rs) where
56 proj_TyConC (Proxy @c) q <|>
57 proj_TyConR (Proxy @rs) q
58 -- | End the recursion.
59 instance Proj_TyConR cs '[]
61 -- ** Class 'Proj_TyConC'
62 -- | Handle the work of 'Proj_TyCon' for a given /type constant/ @c@,
63 -- that is: maybe it handles the given 'Constraint',
64 -- and if so, maybe the 'Constraint' holds.
65 class Proj_TyConC cs c where
66 proj_TyConC :: Source src => Proxy c -> Type src ctx ss cs q -> Maybe (TyCon q)
67 proj_TyConC _c _q = Nothing
70 -- | 'Constraint' union.
75 => Type src ctx ss cs qx
76 -> Type src ctx ss cs qy
77 -> Type src ctx ss cs (qx # qy)
78 (#) a b = (ty @(#) `tyApp` a) `tyApp` b
81 instance Fixity_TyConstC (#) where
82 fixity_TyConstC _ = FixityInfix $ infixB L 0
87 => Inj_TyConst cs (#>)
88 => Type src ctx ss cs q
89 -> Type src ctx ss cs h
90 -> (TyCon q -> Type src ctx ss cs h -> Type src ctx ss cs h)
91 -> Type src ctx ss cs (q #> h)
93 TyApp sourceLess (TyApp sourceLess (ty @(#>)) q) $