1 {-# language ConstraintKinds #-}
2 {-# LANGUAGE AllowAmbiguousTypes #-}
3 {-# LANGUAGE PartialTypeSignatures #-}
4 {-# LANGUAGE ImpredicativeTypes #-}
5 {-# LANGUAGE LiberalTypeSynonyms #-}
7 {-# LANGUAGE TypeInType #-}
8 {-# LANGUAGE PolyKinds #-}
9 {-# LANGUAGE NoMonomorphismRestriction #-}
10 {-# LANGUAGE UndecidableInstances #-}
11 {-# LANGUAGE UndecidableSuperClasses #-}
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
13 module Language.Symantic.Typing.Type
14 ( module Language.Symantic.Typing.Type
21 import Control.Applicative (Alternative(..))
22 import Data.Maybe (isJust)
24 import Data.Semigroup ((<>))
26 import Data.Type.Equality
28 import qualified Data.Kind as K
29 import qualified Data.Set as Set
31 import Language.Symantic.Grammar
32 import Language.Symantic.Typing.List
33 import Language.Symantic.Typing.Kind
34 import Language.Symantic.Typing.Variable
37 data Type (src::K.Type) (vs::[K.Type]) (t::kt) where
38 TyConst :: src -> Len vs
41 TyApp :: src -> Type src vs f
44 -- NOTE: @f a@ decomposition made possible
45 -- at the cost of requiring @TypeInType@.
46 TyVar :: src -> NameVar
49 TyFam :: src -> Len vs
52 -> Type src vs (Fam fam hs)
54 TyPi :: src -> Type src (arg ': vs) res
55 -> Type (Pi arg res) src vs
56 TyQuant :: src -> NameHint
58 -> (forall (v::kv). Type src vs v -> TypeK src vs k)
60 TyEq :: src -> Type x src vs
62 -> Type (x #~ y) src vs
65 instance Source src => Eq (Type src vs t) where
66 x == y = isJust $ eqType x y
67 instance Inj_Source (TypeVT src) src => TestEquality (Type src vs) where
70 type instance SourceOf (Type src vs t) = src
71 instance Source src => Sourced (Type src vs t) where
72 sourceOf (TyConst src _l _c) = src
73 sourceOf (TyApp src _f _a) = src
74 sourceOf (TyVar src _n _v) = src
75 sourceOf (TyFam src _l _f _as) = src
77 setSource (TyConst _src l c) src = TyConst src l c
78 setSource (TyApp _src f a) src = TyApp src f a
79 setSource (TyVar _src n v) src = TyVar src n v
80 setSource (TyFam _src l f as) src = TyFam src l f as
82 instance Inj_Source (TypeVT src) src => KindOf (Type src vs) where
83 kindOf t = kindOfType t `source` TypeVT t
85 instance ConstsOf (Type src vs t) where
86 constsOf (TyConst _src _len c) = Set.singleton $ ConstC c
87 constsOf TyVar{} = Set.empty
88 constsOf (TyApp _src f a) = constsOf f `Set.union` constsOf a
89 constsOf (TyFam _src _len fam as) = constsOf fam `Set.union` constsOf as
91 type instance VarsOf (Type src vs t) = vs
92 instance LenVars (Type src vs t) where
93 lenVars (TyConst _src len _c) = len
94 lenVars (TyApp _src f _a) = lenVars f
95 lenVars (TyVar _src _n v) = lenVars v
96 lenVars (TyFam _src l _f _as) = l
97 instance UsedVarsOf (Type src vs t) where
98 usedVarsOf vs TyConst{} k = k vs
99 usedVarsOf vs (TyFam _src _len _fam as) k = usedVarsOf vs as k
100 usedVarsOf vs (TyApp _src f a) k =
101 usedVarsOf vs f $ \vs' ->
103 usedVarsOf vs (TyVar _src _n v) k =
104 case insertUsedVars v vs of
107 instance VarOccursIn (Type src vs t) where
108 varOccursIn v (TyVar _src _n v') | Just{} <- v `eqVarKi` v' = False
109 varOccursIn _v TyVar{} = False
110 varOccursIn _v TyConst{} = False
111 varOccursIn v (TyApp _src f a) = varOccursIn v f || varOccursIn v a
112 varOccursIn v (TyFam _src _len _fam as) = varOccursIn v as
113 instance AllocVars (Type src) where
114 allocVarsL len (TyConst src l c) = TyConst src (addLen len l) c
115 allocVarsL len (TyApp src f a) = TyApp src (allocVarsL len f) (allocVarsL len a)
116 allocVarsL len (TyVar src n v) = TyVar src n $ allocVarsL len v
117 allocVarsL len (TyFam src l f as) = TyFam src (addLen len l) f $ allocVarsL len `mapTys` as
119 allocVarsR len (TyConst src l c) = TyConst src (addLen l len) c
120 allocVarsR len (TyApp src f a) = TyApp src (allocVarsR len f) (allocVarsR len a)
121 allocVarsR len (TyVar src n v) = TyVar src n $ allocVarsR len v
122 allocVarsR len (TyFam src l f as) = TyFam src (addLen l len) f $ allocVarsR len `mapTys` as
124 -- | Like 'TyConst', but using 'noSource' and 'inj_Const'.
126 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
128 forall kc (c::kc) src vs.
132 Inj_KindP (Ty_of_Type kc) =>
133 Type_of_Ty (Ty_of_Type kc) ~ kc =>
135 tyConst = TyConst noSource inj_Len inj_Const
136 -- NOTE: The forall brings @c@ first in the type variables,
137 -- which is convenient to use @TypeApplications@.
139 -- | Like 'TyConst', but using 'noSource' and 'inj_Const'.
141 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
143 forall kc (c::kc) src vs.
146 Inj_KindP (Ty_of_Type kc) =>
147 Type_of_Ty (Ty_of_Type kc) ~ kc =>
150 tyConstLen len = TyConst noSource len inj_Const
152 -- | Like 'TyApp', but using 'noSource'.
158 tyApp = TyApp noSource
161 -- | Like 'TyVar', but using 'noSource'.
167 tyVar = TyVar noSource
169 -- | Like 'TyFam', but using 'noSource'.
171 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
173 forall kc (c::kc) as vs src.
177 Inj_KindP (Ty_of_Type kc) =>
178 Type_of_Ty (Ty_of_Type kc) ~ kc =>
180 Type src vs (Fam c as)
181 tyFam = TyFam noSource inj_Len (inj_Const @c)
183 -- | Qualify a 'Type'.
190 Type src vs (q #> t)) ->
193 case proveConstraint q of
194 Just Dict -> f Dict t
199 -- | Existential 'Type' of a known 'Kind'.
200 data TypeK src vs kt = forall (t::kt). TypeK (Type src vs t)
201 type instance SourceOf (TypeK src vs ki) = src
202 type instance VarsOf (TypeK src vs ki) = vs
203 instance Source src => Eq (TypeK src vs ki) where
204 TypeK x == TypeK y = isJust $ eqType x y
207 -- | Existential for 'Type'.
208 data TypeT src vs = forall t. TypeT (Type src vs t)
209 type instance SourceOf (TypeT src vs) = src
210 type instance VarsOf (TypeT src vs) = vs
211 instance Source src => Eq (TypeT src vs) where
212 TypeT x == TypeT y = isJust $ eqTypeKi x y
215 -- | Existential polymorphic 'Type'.
216 data TypeVT src = forall vs t. TypeVT (Type src vs t)
217 type instance SourceOf (TypeVT src) = src
218 instance Source src => Eq (TypeVT src) where
219 TypeVT x == TypeVT y =
220 let (x', y') = appendVars x y in
221 isJust $ eqTypeKi x' y'
224 -- | /Type constant/.
225 data Const src (c::kc) where
230 type instance SourceOf (Const src c) = src
231 instance ConstsOf (Const src c) where
232 constsOf = Set.singleton . ConstC
233 instance Inj_Source (ConstC src) src => KindOf (Const src) where
234 kindOf c@(Const kc) = kc `source` ConstC c
236 kindOfConst :: Const src (t::kt) -> Kind src kt
237 kindOfConst (Const kc) = kc
239 -- ** Class 'Constable'
243 , ClassInstancesFor c
249 , ClassInstancesFor c
253 -- ** Class 'ConstsOf'
254 -- | Return the 'Const's used by something.
255 class ConstsOf a where
256 constsOf :: a -> Set (ConstC (SourceOf a))
258 -- ** Class 'FixityOf'
259 -- | Return the 'Fixity' of something.
260 class FixityOf (c::kc) where
261 fixityOf :: proxy c -> Maybe Fixity
262 fixityOf _c = Nothing
263 instance FixityOf (c::K.Type)
264 instance FixityOf (c::K.Constraint)
267 instance Show (Const src c) where
268 showsPrec p c@Const{} = showsPrec p $ typeRep c
272 = forall c. ConstC (Const src c)
273 instance Eq (ConstC src) where
274 ConstC x == ConstC y = isJust $ x `eqConstKi` y
275 instance Ord (ConstC src) where
276 ConstC x `compare` ConstC y = x `ordConst` y
279 -- | The function 'Type' @(->)@,
280 -- with an infix notation more readable.
287 (~>) a b = (tyConstLen @(K (->)) @(->) (lenVars a) `tyApp` a) `tyApp` b
289 instance FixityOf (->) where
290 fixityOf _c = Just $ Fixity2 $ infixR 0
291 {- NOTE: defined in Lib.Function
292 instance ClassInstancesFor (->)
293 instance TypeInstancesFor (->)
296 -- ** Type family 'FunArg'
297 type family FunArg (fun::K.Type) :: K.Type where
298 FunArg (q #> f) = FunArg f
301 -- ** Type family 'FunRes'
302 type family FunRes (fun::K.Type) :: K.Type where
303 FunRes (q #> f) = FunRes f
306 -- | Return, when the given 'Type' is a function,
307 -- the argument of that function.
310 Inj_Source (TypeVT src) src =>
313 Maybe ( Type src tys (FunArg t)
314 , Type src tys (FunRes t) )
315 unTyFun ty_ini = go ty_ini
319 Maybe ( Type src tys (FunArg x)
320 , Type src tys (FunRes x) )
321 go (TyApp _ (TyApp _ (TyConst _ _ f) a) b)
322 | Just HRefl <- proj_ConstKi @(K (->)) @(->) f
323 = Just ((a `source` TypeVT ty_ini), (b `source` TypeVT ty_ini))
324 go (TyApp _ (TyApp _ (TyConst _ _ f) _a) b)
325 | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) f
328 go TyConst{} = Nothing
333 -- | /Type constant/ to qualify a 'Type'.
334 newtype (#>) (q::K.Constraint) (a::K.Type) = Qual (q => a)
335 instance FixityOf (#>) where
336 fixityOf _c = Just $ Fixity2 $ infixR 0
337 instance ClassInstancesFor (#>)
338 instance TypeInstancesFor (#>)
345 (#>) a b = (tyConstLen @(K (#>)) @(#>) (lenVars a) `tyApp` a) `tyApp` b
347 -- NOTE: should actually be (-1)
348 -- to compose well with @infixr 0 (->)@
349 -- but this is not allowed by GHC.
352 -- | 'K.Constraint' union.
353 class (x, y) => x # y
354 instance (x, y) => x # y
355 instance FixityOf (#) where
356 fixityOf _c = Just $ Fixity2 $ infixB SideL 0
357 instance ClassInstancesFor (#) where
358 proveConstraintFor _c (TyApp _ (TyApp _ c q0) q1)
359 | Just HRefl <- proj_ConstKiTy @(K (#)) @(#) c
360 , Just Dict <- proveConstraint q0
361 , Just Dict <- proveConstraint q1
363 proveConstraintFor _c _q = Nothing
364 instance TypeInstancesFor (#)
370 Type src vs (qx # qy)
371 (#) a b = (tyConstLen @(K (#)) @(#) (lenVars a) `tyApp` a) `tyApp` b
377 Type src vs (()::K.Constraint)
378 noConstraint = tyConstLen @K.Constraint @(()::K.Constraint) inj_Len
383 Type src vs (()::K.Constraint)
384 noConstraintLen = tyConstLen @K.Constraint @(()::K.Constraint)
386 instance ClassInstancesFor (()::K.Constraint) where
387 proveConstraintFor _c q
388 | Just HRefl <- proj_ConstKiTy @K.Constraint @(()::K.Constraint) q
390 proveConstraintFor _c _q = Nothing
391 instance TypeInstancesFor (()::K.Constraint)
394 -- | /Type equality/ 'K.Constraint'.
395 class (x ~ y) => x #~ y
396 instance (x ~ y) => x #~ y
397 instance FixityOf (#~) where
398 fixityOf _ = Just $ Fixity2 $ infixB SideL 0
402 ) => ClassInstancesFor ((#~)::k -> k -> K.Constraint) where
403 proveConstraintFor _c (TyApp _ (TyApp _ c a) b)
404 | Just HRefl <- proj_ConstKiTy @(k -> k -> K.Constraint) @(#~) c
405 , Just Refl <- eqType a b
407 proveConstraintFor _c _q = Nothing
408 instance TypeInstancesFor (#~)
415 Type src vs (a :: k) ->
416 Type src vs (b :: k) ->
418 (#~) a b = (tyConstLen @(K (#~)) @(#~) (lenVars a) `tyApp` a) `tyApp` b
427 Const src ((#~)::k -> k -> K.Constraint)
428 const_EqTy = inj_Const @(#~)
431 -- | /Type equality/.
434 Type src tys (x::k) ->
435 Type src tys (y::k) ->
437 eqType x y | Just HRefl <- eqTypeKi x y = Just Refl
438 eqType _x _y = Nothing
440 -- | /Heterogeneous type equality/:
441 -- return two proofs when two 'Type's are equals:
442 -- one for the type and one for the kind.
445 Type src tys (x::kx) ->
446 Type src tys (y::ky) ->
448 eqTypeKi (TyConst _sx _lx x) (TyConst _sy _ly y) = eqConstKi x y
449 eqTypeKi (TyVar _sx _nx x) (TyVar _sy _ny y) = eqVarKi x y
450 eqTypeKi (TyApp _sx fx ax) (TyApp _sy fy ay)
451 | Just HRefl <- eqTypeKi fx fy
452 , Just HRefl <- eqTypeKi ax ay
454 eqTypeKi (TyFam _ _lx fx ax) (TyFam _ _ly fy ay)
455 | Just HRefl <- eqConstKi fx fy
456 , Just HRefl <- eqTypes ax ay
458 -- NOTE: 'TyFam' is expanded as much as possible.
459 eqTypeKi x@TyFam{} y = eqTypeKi (expandFam x) y
460 eqTypeKi x y@TyFam{} = eqTypeKi x (expandFam y)
461 eqTypeKi _x _y = Nothing
468 eqTypes TypesZ TypesZ = Just HRefl
469 eqTypes (x `TypesS` sx) (y `TypesS` sy)
470 | Just HRefl <- eqTypeKi x y
471 , Just HRefl <- eqTypes sx sy
473 eqTypes _x _y = Nothing
476 -- | Compare two 'Type's.
479 Type src tys (x::kx) ->
480 Type src tys (y::ky) ->
482 -- NOTE: 'TyFam' is expanded as much as possible.
483 ordType (TyFam _ _lx fx ax) (TyFam _ _ly fy ay) = ordConst fx fy <> ordTypes ax ay
484 ordType x@TyFam{} y = ordType (expandFam x) y
485 ordType x y@TyFam{} = ordType x (expandFam y)
486 ordType (TyConst _sx _lx x) (TyConst _sy _ly y) = ordConst x y
487 ordType TyConst{} _y = LT
488 ordType _x TyConst{} = GT
489 ordType (TyVar _sx _kx x) (TyVar _sy _ky y) = ordVarKi x y
490 ordType TyVar{} _y = LT
491 ordType _x TyVar{} = GT
492 ordType (TyApp _sx fx xx) (TyApp _sy fy xy) = ordType fx fy <> ordType xx xy
493 -- ordType TyApp{} _y = LT
494 -- ordType _x TyApp{} = GT
501 ordTypes TypesZ TypesZ = EQ
502 ordTypes TypesZ TypesS{} = LT
503 ordTypes TypesS{} TypesZ = GT
504 ordTypes (x `TypesS` sx) (y `TypesS` sy) = ordType x y <> ordTypes sx sy
507 -- | Captures the proof of a 'K.Constraint'
508 -- (and its dictionaries when it contains type classes):
509 -- pattern matching on the 'Dict' constructor
510 -- brings the 'K.Constraint' into scope.
514 -- ** Class 'ClassInstancesFor'
515 class ClassInstancesFor c where
521 proveConstraintFor _c _q = Nothing
523 proveConstraint :: Source src => Type src vs q -> Maybe (Qual q)
524 proveConstraint q = foldr proj Nothing $ constsOf q
525 where proj (ConstC c@Const{}) = (<|> proveConstraintFor c q)
532 Inj_KindP (Ty_of_Type (K c)) =>
533 Type_of_Ty (Ty_of_Type (K c)) ~ K c =>
535 inj_Const = Const $ inj_KindP @(Ty_of_Type (K c)) noSource
546 {- XXX: not working because of GHC's #12933
550 (K c ~ Type_of_Ty (Ty_of_Type (K c))) =>
554 proj_ConstKi (Const ku) = do
555 Refl <- eqKind ku $ inj_Kind @(K c) @()
560 -- | Like 'proj_ConstKi', but with kind equality already known.
566 proj_Const Const{} = eqT
568 -- | Like 'proj_Const', but on a 'Type'.
574 proj_ConstTy (TyConst _src _len Const{}) = eqT
575 proj_ConstTy _ = Nothing
577 -- | XXX: 'proj_ConstKi' must be given the @kc@ kind explicitely,
578 -- when used multiple times in the same pattern match,
579 -- because of GHC's #12933.
581 forall kc (c::kc) u src.
583 (kc ~ Type_of_Ty (Ty_of_Type kc)) =>
584 Inj_KindP (Ty_of_Type kc) =>
587 proj_ConstKi (Const ku) = do
588 Refl <- eqKind ku $ inj_KindP @(Ty_of_Type kc) ()
592 -- | Like 'proj_ConstKi', but on a 'Type'.
594 forall kc (c::kc) u src vs.
596 (kc ~ Type_of_Ty (Ty_of_Type kc)) =>
597 Inj_KindP (Ty_of_Type kc) =>
600 proj_ConstKiTy (TyConst _src _len c) = proj_ConstKi c
601 proj_ConstKiTy _ = Nothing
604 Const src_x (x::k) ->
605 Const src_y (y::k) ->
607 eqConst (Const _kx) (Const _ky) = eqT
608 instance Eq (Const src c) where
609 x == y = isJust $ eqConstKi x y
610 instance Ord (Const src c) where
614 forall src_x src_y kx ky x y.
615 Const src_x (x::kx) ->
616 Const src_y (y::ky) ->
618 eqConstKi (Const kx) (Const ky)
619 | Just Refl <- eqKind kx ky
620 , Just (Refl::x:~:y) <- eqT
622 eqConstKi _x _y = Nothing
625 forall src_x src_y kx ky x y.
626 Const src_x (x::kx) ->
627 Const src_y (y::ky) ->
629 ordConst x@Const{} y@Const{} =
630 typeRep x `compare` typeRep y
634 Type src tys (t::kt) ->
636 normalizeQualsTy = go
640 Type src tys (a::ka) ->
643 go (TyApp s1 (TyApp s0 f q0) q1)
644 | Just HRefl <- proj_ConstKiTy @(K (#)) @(#) f
646 , TypeK q1' <- go q1 =
647 case (proveConstraint q0', proveConstraint q1') of
648 (Just{}, Just{}) -> TypeK $ tyConstLen @K.Constraint @(()::K.Constraint) $ lenVars q0
649 (Just{}, Nothing) -> TypeK q1'
650 (Nothing, Just{}) -> TypeK q0'
651 (Nothing, Nothing) ->
652 case q0' `ordType` q1' of
653 LT -> TypeK $ TyApp s1 (TyApp s0 f q0') q1'
655 GT -> TypeK $ TyApp s0 (TyApp s1 f q1') q0'
657 go (TyApp s1 (TyApp s0 f0@(TyConst sf0 lf0 _) q0) (TyApp s2 (TyApp s3 f1 q1) t))
658 | Just HRefl <- proj_ConstKiTy @(K (#>)) @(#>) f0
659 , Just HRefl <- proj_ConstKiTy @(K (#>)) @(#>) f1
661 , TypeK q1' <- go q1 =
662 case (proveConstraint q0', proveConstraint q1') of
663 (Just{}, Just{}) -> TypeK t
664 (Just{}, Nothing) -> TypeK $ TyApp s2 (TyApp s3 f1 q1) t
665 (Nothing, Just{}) -> TypeK $ TyApp s1 (TyApp s0 f0 q0) t
666 (Nothing, Nothing) ->
667 case q0' `ordType` q1' of
669 let q0q1 = TyApp s2 (TyApp s0 (TyConst sf0 lf0 $ inj_Const @(#)) q0') q1' in
670 TypeK $ TyApp s2 (TyApp s3 f1 q0q1) t
671 EQ -> TypeK $ TyApp s2 (TyApp s3 f1 q1') t
673 let q1q0 = TyApp s0 (TyApp s2 (TyConst sf0 lf0 $ inj_Const @(#)) q1') q0' in
674 TypeK $ TyApp s2 (TyApp s3 f1 q1q0) t
676 go (TyApp _sa (TyApp _sq (TyConst _sc _lc c) q) t)
677 | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) c =
678 case proveConstraint q of
686 | Just Refl <- KiConstraint () `eqKind` kindOfType q
687 = case proveConstraint q of
688 Just{} -> TypeK $ tyConstLen @K.Constraint @(()::K.Constraint) $ lenVars q
693 = TypeK $ TyApp src f' a'
694 go t@TyFam{} = go (expandFam t)
695 go t@TyConst{} = TypeK t
696 go t@TyVar{} = TypeK t
698 -- * Type family 'Fam'
699 -- | Apply the /type family/ selected by @fam@
700 -- to a list of types (within a 'Proxy').
702 -- | NOTE: |Fam|'s 'Kind' MUST be the same than @fam@'s.
703 type family Fam (fam::k) (hs::[K.Type]) :: k
704 -- type family FamKi fam :: K.Type
706 -- ** Class 'TypeInstancesFor'
707 class TypeInstancesFor (c::kc) where
714 -> Maybe (Type src vs (Fam fam ts))
715 expandFamFor _c _len _fam _as = Nothing
717 -- ** Class 'ExpandFam'
718 class ExpandFam a where
719 expandFam :: Source (SourceOf a) => a -> a
720 instance ExpandFam (Type src vs t) where
721 expandFam t@TyConst{} = t
722 expandFam t@TyVar{} = t
723 expandFam (TyApp src f a) = TyApp src (expandFam f) (expandFam a)
724 expandFam t@(TyFam _src len fam as) =
725 case foldr proj Nothing $ constsOf fam `Set.union` constsOf as of
727 Just t' -> expandFam t'
728 where proj (ConstC c@Const{}) = (<|> expandFamFor c len fam as)
729 -- XXX: this can loop…
731 -- | Like 'kindOf' but keep the old 'Source'.
732 kindOfType :: Type src vs (t::kt) -> Kind src kt
733 kindOfType (TyConst _src _l c) = kindOfConst c
734 kindOfType (TyApp _src f _a) =
736 KiFun _src_kf _kx kf -> kf
737 kindOfType (TyVar _src _n v) = kindOfVar v
738 kindOfType (TyFam _src _len fam _as) = kindOfConst fam
740 -- | Remove unused 'Var's from ther context.
742 Type src vs (t::kt) ->
743 (forall vs'. Type src vs' (t::kt) -> ret) -> ret
744 normalizeVarsTy ty k =
745 usedVarsOf UsedVarsZ ty $ \vs ->
749 UsedVars src vs vs' ->
752 goTy vs (TyConst src _len c) = TyConst src (lenVars vs) c
753 goTy vs (TyApp src f a) = TyApp src (goTy vs f) (goTy vs a)
754 goTy vs (TyVar src n v) =
755 case v `lookupUsedVars` vs of
756 Nothing -> error "[BUG] normalizeVarsTy: impossible lookup fail"
757 Just v' -> TyVar src n v'
758 goTy vs (TyFam src _len fam as) = TyFam src (lenVars vs) fam $ goTys vs as
761 UsedVars src vs vs' ->
764 goTys _vs TypesZ = TypesZ
765 goTys vs (TypesS t ts) =
770 data Types src vs (ts::[K.Type]) where
771 TypesZ :: Types src vs '[]
772 TypesS :: Type src vs t
774 -> Types src vs (Proxy t ': ts)
777 type instance SourceOf (Types src vs ts) = src
778 instance ConstsOf (Types src vs ts) where
779 constsOf TypesZ = Set.empty
780 constsOf (TypesS t ts) = constsOf t `Set.union` constsOf ts
781 type instance VarsOf (Types src vs ts) = vs
782 instance UsedVarsOf (Types src vs ts) where
783 usedVarsOf vs TypesZ k = k vs
784 usedVarsOf vs (TypesS t ts) k =
785 usedVarsOf vs t $ \vs' ->
787 instance VarOccursIn (Types src vs ts) where
788 varOccursIn _v TypesZ = False
789 varOccursIn v (TypesS t ts) = varOccursIn v t || varOccursIn v ts
792 (forall kt (t::kt). Type src vs t -> Type src vs' t) ->
795 mapTys _f TypesZ = TypesZ
796 mapTys f (TypesS t ts) = f t `TypesS` mapTys f ts
799 (forall k (t::k). Type src vs t -> acc -> acc) ->
800 Types src vs ts -> acc -> acc
801 foldlTys _f TypesZ acc = acc
802 foldlTys f (TypesS t ts) acc = foldlTys f ts (f t acc)
806 typeOf :: a t -> Type (SourceOf (a t)) (VarsOf (a t)) t