1 {-# language ConstraintKinds #-}
2 {-# LANGUAGE AllowAmbiguousTypes #-}
3 {-# LANGUAGE PartialTypeSignatures #-}
4 {-# LANGUAGE ImpredicativeTypes #-}
5 {-# LANGUAGE LiberalTypeSynonyms #-}
7 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
8 {-# LANGUAGE TypeInType #-}
9 {-# LANGUAGE PolyKinds #-}
10 {-# LANGUAGE NoMonomorphismRestriction #-}
11 {-# LANGUAGE UndecidableInstances #-}
12 {-# LANGUAGE UndecidableSuperClasses #-}
13 {-# OPTIONS_GHC -fno-warn-orphans #-}
14 module Language.Symantic.Typing.Type
15 ( module Language.Symantic.Typing.Type
22 import Control.Applicative (Alternative(..))
23 import Data.Maybe (isJust)
25 import Data.Semigroup ((<>))
27 import Data.Type.Equality
29 import qualified Data.Kind as K
30 import qualified Data.Set as Set
32 import Language.Symantic.Grammar
33 import Language.Symantic.Typing.List
34 import Language.Symantic.Typing.Kind
35 import Language.Symantic.Typing.Variable
38 data Type (src::K.Type) (vs::[K.Type]) (t::kt) where
39 TyConst :: src -> Len vs
42 TyApp :: src -> Type src vs f
45 -- NOTE: @f a@ decomposition made possible
46 -- at the cost of requiring @TypeInType@.
47 TyVar :: src -> NameVar
50 TyFam :: src -> Len vs
53 -> Type src vs (Fam fam hs)
55 TyPi :: src -> Type src (arg ': vs) res
56 -> Type (Pi arg res) src vs
57 TyQuant :: src -> NameHint
59 -> (forall (v::kv). Type src vs v -> TypeK src vs k)
61 TyEq :: src -> Type x src vs
63 -> Type (x #~ y) src vs
66 instance Source src => Eq (Type src vs t) where
67 x == y = isJust $ eqType x y
68 instance Inj_Source (TypeVT src) src => TestEquality (Type src vs) where
71 type instance SourceOf (Type src vs t) = src
72 instance Source src => Sourced (Type src vs t) where
73 sourceOf (TyConst src _l _c) = src
74 sourceOf (TyApp src _f _a) = src
75 sourceOf (TyVar src _n _v) = src
76 sourceOf (TyFam src _l _f _as) = src
78 setSource (TyConst _src l c) src = TyConst src l c
79 setSource (TyApp _src f a) src = TyApp src f a
80 setSource (TyVar _src n v) src = TyVar src n v
81 setSource (TyFam _src l f as) src = TyFam src l f as
83 instance Inj_Source (TypeVT src) src => KindOf (Type src vs) where
84 kindOf t = kindOfType t `source` TypeVT t
86 instance ConstsOf (Type src vs t) where
87 constsOf (TyConst _src _len c) = Set.singleton $ ConstC c
88 constsOf TyVar{} = Set.empty
89 constsOf (TyApp _src f a) = constsOf f `Set.union` constsOf a
90 constsOf (TyFam _src _len fam as) = constsOf fam `Set.union` constsOf as
92 type instance VarsOf (Type src vs t) = vs
93 instance LenVars (Type src vs t) where
94 lenVars (TyConst _src len _c) = len
95 lenVars (TyApp _src f _a) = lenVars f
96 lenVars (TyVar _src _n v) = lenVars v
97 lenVars (TyFam _src l _f _as) = l
98 instance UsedVarsOf (Type src vs t) where
99 usedVarsOf vs TyConst{} k = k vs
100 usedVarsOf vs (TyFam _src _len _fam as) k = usedVarsOf vs as k
101 usedVarsOf vs (TyApp _src f a) k =
102 usedVarsOf vs f $ \vs' ->
104 usedVarsOf vs (TyVar _src _n v) k =
105 case insertUsedVars v vs of
108 instance VarOccursIn (Type src vs t) where
109 varOccursIn v (TyVar _src _n v') | Just{} <- v `eqVarKi` v' = False
110 varOccursIn _v TyVar{} = False
111 varOccursIn _v TyConst{} = False
112 varOccursIn v (TyApp _src f a) = varOccursIn v f || varOccursIn v a
113 varOccursIn v (TyFam _src _len _fam as) = varOccursIn v as
114 instance AllocVars (Type src) where
115 allocVarsL len (TyConst src l c) = TyConst src (addLen len l) c
116 allocVarsL len (TyApp src f a) = TyApp src (allocVarsL len f) (allocVarsL len a)
117 allocVarsL len (TyVar src n v) = TyVar src n $ allocVarsL len v
118 allocVarsL len (TyFam src l f as) = TyFam src (addLen len l) f $ allocVarsL len `mapTys` as
120 allocVarsR len (TyConst src l c) = TyConst src (addLen l len) c
121 allocVarsR len (TyApp src f a) = TyApp src (allocVarsR len f) (allocVarsR len a)
122 allocVarsR len (TyVar src n v) = TyVar src n $ allocVarsR len v
123 allocVarsR len (TyFam src l f as) = TyFam src (addLen l len) f $ allocVarsR len `mapTys` as
125 -- | Like 'TyConst', but using 'noSource' and 'inj_Const'.
127 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
129 forall kc (c::kc) src vs.
133 Inj_KindP (Ty_of_Type kc) =>
134 Type_of_Ty (Ty_of_Type kc) ~ kc =>
136 tyConst = TyConst noSource inj_Len inj_Const
137 -- NOTE: The forall brings @c@ first in the type variables,
138 -- which is convenient to use @TypeApplications@.
140 -- | Like 'TyConst', but using 'noSource' and 'inj_Const'.
142 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
144 forall kc (c::kc) src vs.
147 Inj_KindP (Ty_of_Type kc) =>
148 Type_of_Ty (Ty_of_Type kc) ~ kc =>
151 tyConstLen len = TyConst noSource len inj_Const
153 -- | Like 'TyApp', but using 'noSource'.
159 tyApp = TyApp noSource
162 -- | Like 'TyVar', but using 'noSource'.
168 tyVar = TyVar noSource
170 -- | Like 'TyFam', but using 'noSource'.
172 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
174 forall kc (c::kc) as vs src.
178 Inj_KindP (Ty_of_Type kc) =>
179 Type_of_Ty (Ty_of_Type kc) ~ kc =>
181 Type src vs (Fam c as)
182 tyFam = TyFam noSource inj_Len (inj_Const @c)
184 -- | Qualify a 'Type'.
191 Type src vs (q #> t)) ->
194 case proveConstraint q of
195 Just Dict -> f Dict t
200 -- | Existential 'Type' of a known 'Kind'.
201 data TypeK src vs kt = forall (t::kt). TypeK (Type src vs t)
202 type instance SourceOf (TypeK src vs ki) = src
203 type instance VarsOf (TypeK src vs ki) = vs
204 instance Source src => Eq (TypeK src vs ki) where
205 TypeK x == TypeK y = isJust $ eqType x y
208 -- | Existential for 'Type'.
209 data TypeT src vs = forall t. TypeT (Type src vs t)
210 type instance SourceOf (TypeT src vs) = src
211 type instance VarsOf (TypeT src vs) = vs
212 instance Source src => Eq (TypeT src vs) where
213 TypeT x == TypeT y = isJust $ eqTypeKi x y
216 -- | Existential polymorphic 'Type'.
217 data TypeVT src = forall vs t. TypeVT (Type src vs t)
218 type instance SourceOf (TypeVT src) = src
219 instance Source src => Eq (TypeVT src) where
220 TypeVT x == TypeVT y =
221 let (x', y') = appendVars x y in
222 isJust $ eqTypeKi x' y'
225 -- | /Type constant/.
226 data Const src (c::kc) where
231 type instance SourceOf (Const src c) = src
232 instance ConstsOf (Const src c) where
233 constsOf = Set.singleton . ConstC
234 instance Inj_Source (ConstC src) src => KindOf (Const src) where
235 kindOf c@(Const kc) = kc `source` ConstC c
237 kindOfConst :: Const src (t::kt) -> Kind src kt
238 kindOfConst (Const kc) = kc
240 -- ** Class 'Constable'
244 , ClassInstancesFor c
250 , ClassInstancesFor c
254 -- ** Class 'ConstsOf'
255 -- | Return the 'Const's used by something.
256 class ConstsOf a where
257 constsOf :: a -> Set (ConstC (SourceOf a))
259 -- ** Class 'FixityOf'
260 -- | Return the 'Fixity' of something.
261 class FixityOf (c::kc) where
262 fixityOf :: proxy c -> Maybe Fixity
263 fixityOf _c = Nothing
264 instance FixityOf (c::K.Type)
265 instance FixityOf (c::K.Constraint)
268 instance Show (Const src c) where
269 showsPrec p c@Const{} = showsPrec p $ typeRep c
273 = forall c. ConstC (Const src c)
274 instance Eq (ConstC src) where
275 ConstC x == ConstC y = isJust $ x `eqConstKi` y
276 instance Ord (ConstC src) where
277 ConstC x `compare` ConstC y = x `ordConst` y
280 -- | The function 'Type' @(->)@,
281 -- with an infix notation more readable.
288 (~>) a b = (tyConstLen @(K (->)) @(->) (lenVars a) `tyApp` a) `tyApp` b
290 instance FixityOf (->) where
291 fixityOf _c = Just $ Fixity2 $ infixR 0
292 {- NOTE: defined in Lib.Function
293 instance ClassInstancesFor (->)
294 instance TypeInstancesFor (->)
297 -- ** Type family 'FunArg'
298 type family FunArg (fun::K.Type) :: K.Type where
299 FunArg (q #> f) = FunArg f
302 -- ** Type family 'FunRes'
303 type family FunRes (fun::K.Type) :: K.Type where
304 FunRes (q #> f) = FunRes f
307 -- | Return, when the given 'Type' is a function,
308 -- the argument of that function.
311 Inj_Source (TypeVT src) src =>
314 Maybe ( Type src tys (FunArg t)
315 , Type src tys (FunRes t) )
316 unTyFun ty_ini = go ty_ini
320 Maybe ( Type src tys (FunArg x)
321 , Type src tys (FunRes x) )
322 go (TyApp _ (TyApp _ (TyConst _ _ f) a) b)
323 | Just HRefl <- proj_ConstKi @(K (->)) @(->) f
324 = Just ((a `source` TypeVT ty_ini), (b `source` TypeVT ty_ini))
325 go (TyApp _ (TyApp _ (TyConst _ _ f) _a) b)
326 | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) f
329 go TyConst{} = Nothing
334 -- | /Type constant/ to qualify a 'Type'.
335 newtype (#>) (q::K.Constraint) (a::K.Type) = Qual (q => a)
336 instance FixityOf (#>) where
337 fixityOf _c = Just $ Fixity2 $ infixR 0
338 instance ClassInstancesFor (#>)
339 instance TypeInstancesFor (#>)
346 (#>) a b = (tyConstLen @(K (#>)) @(#>) (lenVars a) `tyApp` a) `tyApp` b
348 -- NOTE: should actually be (-1)
349 -- to compose well with @infixr 0 (->)@
350 -- but this is not allowed by GHC.
353 -- | 'K.Constraint' union.
354 class (x, y) => x # y
355 instance (x, y) => x # y
356 instance FixityOf (#) where
357 fixityOf _c = Just $ Fixity2 $ infixB SideL 0
358 instance ClassInstancesFor (#) where
359 proveConstraintFor _c (TyApp _ (TyApp _ c q0) q1)
360 | Just HRefl <- proj_ConstKiTy @(K (#)) @(#) c
361 , Just Dict <- proveConstraint q0
362 , Just Dict <- proveConstraint q1
364 proveConstraintFor _c _q = Nothing
365 instance TypeInstancesFor (#)
371 Type src vs (qx # qy)
372 (#) a b = (tyConstLen @(K (#)) @(#) (lenVars a) `tyApp` a) `tyApp` b
378 Type src vs (()::K.Constraint)
379 noConstraint = tyConstLen @K.Constraint @(()::K.Constraint) inj_Len
384 Type src vs (()::K.Constraint)
385 noConstraintLen = tyConstLen @K.Constraint @(()::K.Constraint)
387 instance ClassInstancesFor (()::K.Constraint) where
388 proveConstraintFor _c q
389 | Just HRefl <- proj_ConstKiTy @K.Constraint @(()::K.Constraint) q
391 proveConstraintFor _c _q = Nothing
392 instance TypeInstancesFor (()::K.Constraint)
395 -- | /Type equality/ 'K.Constraint'.
396 class (x ~ y) => x #~ y
397 instance (x ~ y) => x #~ y
398 instance FixityOf (#~) where
399 fixityOf _ = Just $ Fixity2 $ infixB SideL 0
403 ) => ClassInstancesFor ((#~)::k -> k -> K.Constraint) where
404 proveConstraintFor _c (TyApp _ (TyApp _ c a) b)
405 | Just HRefl <- proj_ConstKiTy @(k -> k -> K.Constraint) @(#~) c
406 , Just Refl <- eqType a b
408 proveConstraintFor _c _q = Nothing
409 instance TypeInstancesFor (#~)
416 Type src vs (a :: k) ->
417 Type src vs (b :: k) ->
419 (#~) a b = (tyConstLen @(K (#~)) @(#~) (lenVars a) `tyApp` a) `tyApp` b
428 Const src ((#~)::k -> k -> K.Constraint)
429 const_EqTy = inj_Const @(#~)
432 -- | /Type equality/.
435 Type src tys (x::k) ->
436 Type src tys (y::k) ->
438 eqType x y | Just HRefl <- eqTypeKi x y = Just Refl
439 eqType _x _y = Nothing
441 -- | /Heterogeneous type equality/:
442 -- return two proofs when two 'Type's are equals:
443 -- one for the type and one for the kind.
446 Type src tys (x::kx) ->
447 Type src tys (y::ky) ->
449 eqTypeKi (TyConst _sx _lx x) (TyConst _sy _ly y) = eqConstKi x y
450 eqTypeKi (TyVar _sx _nx x) (TyVar _sy _ny y) = eqVarKi x y
451 eqTypeKi (TyApp _sx fx ax) (TyApp _sy fy ay)
452 | Just HRefl <- eqTypeKi fx fy
453 , Just HRefl <- eqTypeKi ax ay
455 eqTypeKi (TyFam _ _lx fx ax) (TyFam _ _ly fy ay)
456 | Just HRefl <- eqConstKi fx fy
457 , Just HRefl <- eqTypes ax ay
459 -- NOTE: 'TyFam' is expanded as much as possible.
460 eqTypeKi x@TyFam{} y = eqTypeKi (expandFam x) y
461 eqTypeKi x y@TyFam{} = eqTypeKi x (expandFam y)
462 eqTypeKi _x _y = Nothing
469 eqTypes TypesZ TypesZ = Just HRefl
470 eqTypes (x `TypesS` sx) (y `TypesS` sy)
471 | Just HRefl <- eqTypeKi x y
472 , Just HRefl <- eqTypes sx sy
474 eqTypes _x _y = Nothing
477 -- | Compare two 'Type's.
480 Type src tys (x::kx) ->
481 Type src tys (y::ky) ->
483 -- NOTE: 'TyFam' is expanded as much as possible.
484 ordType (TyFam _ _lx fx ax) (TyFam _ _ly fy ay) = ordConst fx fy <> ordTypes ax ay
485 ordType x@TyFam{} y = ordType (expandFam x) y
486 ordType x y@TyFam{} = ordType x (expandFam y)
487 ordType (TyConst _sx _lx x) (TyConst _sy _ly y) = ordConst x y
488 ordType TyConst{} _y = LT
489 ordType _x TyConst{} = GT
490 ordType (TyVar _sx _kx x) (TyVar _sy _ky y) = ordVarKi x y
491 ordType TyVar{} _y = LT
492 ordType _x TyVar{} = GT
493 ordType (TyApp _sx fx xx) (TyApp _sy fy xy) = ordType fx fy <> ordType xx xy
494 -- ordType TyApp{} _y = LT
495 -- ordType _x TyApp{} = GT
502 ordTypes TypesZ TypesZ = EQ
503 ordTypes TypesZ TypesS{} = LT
504 ordTypes TypesS{} TypesZ = GT
505 ordTypes (x `TypesS` sx) (y `TypesS` sy) = ordType x y <> ordTypes sx sy
508 -- | Captures the proof of a 'K.Constraint'
509 -- (and its dictionaries when it contains type classes):
510 -- pattern matching on the 'Dict' constructor
511 -- brings the 'K.Constraint' into scope.
515 -- ** Class 'ClassInstancesFor'
516 class ClassInstancesFor c where
522 proveConstraintFor _c _q = Nothing
524 proveConstraint :: Source src => Type src vs q -> Maybe (Qual q)
525 proveConstraint q = foldr proj Nothing $ constsOf q
526 where proj (ConstC c@Const{}) = (<|> proveConstraintFor c q)
533 Inj_KindP (Ty_of_Type (K c)) =>
534 Type_of_Ty (Ty_of_Type (K c)) ~ K c =>
536 inj_Const = Const $ inj_KindP @(Ty_of_Type (K c)) noSource
547 {- XXX: not working because of GHC's #12933
551 (K c ~ Type_of_Ty (Ty_of_Type (K c))) =>
555 proj_ConstKi (Const ku) = do
556 Refl <- eqKind ku $ inj_Kind @(K c) @()
561 -- | Like 'proj_ConstKi', but with kind equality already known.
567 proj_Const Const{} = eqT
569 -- | Like 'proj_Const', but on a 'Type'.
575 proj_ConstTy (TyConst _src _len Const{}) = eqT
576 proj_ConstTy _ = Nothing
578 -- | XXX: 'proj_ConstKi' must be given the @kc@ kind explicitely,
579 -- when used multiple times in the same pattern match,
580 -- because of GHC's #12933.
582 forall kc (c::kc) u src.
584 (kc ~ Type_of_Ty (Ty_of_Type kc)) =>
585 Inj_KindP (Ty_of_Type kc) =>
588 proj_ConstKi (Const ku) = do
589 Refl <- eqKind ku $ inj_KindP @(Ty_of_Type kc) ()
593 -- | Like 'proj_ConstKi', but on a 'Type'.
595 forall kc (c::kc) u src vs.
597 (kc ~ Type_of_Ty (Ty_of_Type kc)) =>
598 Inj_KindP (Ty_of_Type kc) =>
601 proj_ConstKiTy (TyConst _src _len (Const ku)) = do
602 Refl <- eqKind ku $ inj_KindP @(Ty_of_Type kc) ()
605 proj_ConstKiTy _ = Nothing
608 Const src_x (x::k) ->
609 Const src_y (y::k) ->
611 eqConst (Const _kx) (Const _ky) = eqT
612 instance Eq (Const src c) where
613 x == y = isJust $ eqConstKi x y
614 instance Ord (Const src c) where
618 forall src_x src_y kx ky x y.
619 Const src_x (x::kx) ->
620 Const src_y (y::ky) ->
622 eqConstKi (Const kx) (Const ky)
623 | Just Refl <- eqKind kx ky
624 , Just (Refl::x:~:y) <- eqT
626 eqConstKi _x _y = Nothing
629 forall src_x src_y kx ky x y.
630 Const src_x (x::kx) ->
631 Const src_y (y::ky) ->
633 ordConst x@Const{} y@Const{} =
634 typeRep x `compare` typeRep y
638 Type src tys (t::kt) ->
640 normalizeQualsTy = go
644 Type src tys (a::ka) ->
647 go (TyApp s1 (TyApp s0 f q0) q1)
648 | Just HRefl <- proj_ConstKiTy @(K (#)) @(#) f
650 , TypeK q1' <- go q1 =
651 case (proveConstraint q0', proveConstraint q1') of
652 (Just{}, Just{}) -> TypeK $ tyConstLen @K.Constraint @(()::K.Constraint) $ lenVars q0
653 (Just{}, Nothing) -> TypeK q1'
654 (Nothing, Just{}) -> TypeK q0'
655 (Nothing, Nothing) ->
656 case q0' `ordType` q1' of
657 LT -> TypeK $ TyApp s1 (TyApp s0 f q0') q1'
659 GT -> TypeK $ TyApp s0 (TyApp s1 f q1') q0'
661 go (TyApp s1 (TyApp s0 f0@(TyConst sf0 lf0 _) q0) (TyApp s2 (TyApp s3 f1 q1) t))
662 | Just HRefl <- proj_ConstKiTy @(K (#>)) @(#>) f0
663 , Just HRefl <- proj_ConstKiTy @(K (#>)) @(#>) f1
665 , TypeK q1' <- go q1 =
666 case (proveConstraint q0', proveConstraint q1') of
667 (Just{}, Just{}) -> TypeK t
668 (Just{}, Nothing) -> TypeK $ TyApp s2 (TyApp s3 f1 q1) t
669 (Nothing, Just{}) -> TypeK $ TyApp s1 (TyApp s0 f0 q0) t
670 (Nothing, Nothing) ->
671 case q0' `ordType` q1' of
673 let q0q1 = TyApp s2 (TyApp s0 (TyConst sf0 lf0 $ inj_Const @(#)) q0') q1' in
674 TypeK $ TyApp s2 (TyApp s3 f1 q0q1) t
675 EQ -> TypeK $ TyApp s2 (TyApp s3 f1 q1') t
677 let q1q0 = TyApp s0 (TyApp s2 (TyConst sf0 lf0 $ inj_Const @(#)) q1') q0' in
678 TypeK $ TyApp s2 (TyApp s3 f1 q1q0) t
680 go (TyApp _sa (TyApp _sq (TyConst _sc _lc c) q) t)
681 | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) c =
682 case proveConstraint q of
690 | Just Refl <- KiConstraint () `eqKind` kindOfType q
691 = case proveConstraint q of
692 Just{} -> TypeK $ tyConstLen @K.Constraint @(()::K.Constraint) $ lenVars q
697 = TypeK $ TyApp src f' a'
698 go t@TyFam{} = go (expandFam t)
699 go t@TyConst{} = TypeK t
700 go t@TyVar{} = TypeK t
702 -- * Type family 'Fam'
703 -- | Apply the /type family/ selected by @fam@
704 -- to a list of types (within a 'Proxy').
706 -- | NOTE: |Fam|'s 'Kind' MUST be the same than @fam@'s.
707 type family Fam (fam::k) (hs::[K.Type]) :: k
708 -- type family FamKi fam :: K.Type
710 -- ** Class 'TypeInstancesFor'
711 class TypeInstancesFor (c::kc) where
718 -> Maybe (Type src vs (Fam fam ts))
719 expandFamFor _c _len _fam _as = Nothing
721 -- ** Class 'ExpandFam'
722 class ExpandFam a where
723 expandFam :: Source (SourceOf a) => a -> a
724 instance ExpandFam (Type src vs t) where
725 expandFam t@TyConst{} = t
726 expandFam t@TyVar{} = t
727 expandFam (TyApp src f a) = TyApp src (expandFam f) (expandFam a)
728 expandFam t@(TyFam _src len fam as) =
729 case foldr proj Nothing $ constsOf fam `Set.union` constsOf as of
731 Just t' -> expandFam t'
732 where proj (ConstC c@Const{}) = (<|> expandFamFor c len fam as)
733 -- XXX: this can loop…
735 -- | Like 'kindOf' but keep the old 'Source'.
736 kindOfType :: Type src vs (t::kt) -> Kind src kt
737 kindOfType (TyConst _src _l c) = kindOfConst c
738 kindOfType (TyApp _src f _a) =
740 KiFun _src_kf _kx kf -> kf
741 kindOfType (TyVar _src _n v) = kindOfVar v
742 kindOfType (TyFam _src _len fam _as) = kindOfConst fam
744 -- | Remove unused 'Var's from ther context.
746 Type src vs (t::kt) ->
747 (forall vs'. Type src vs' (t::kt) -> ret) -> ret
748 normalizeVarsTy ty k =
749 usedVarsOf UsedVarsZ ty $ \vs ->
753 UsedVars src vs vs' ->
756 goTy vs (TyConst src _len c) = TyConst src (lenVars vs) c
757 goTy vs (TyApp src f a) = TyApp src (goTy vs f) (goTy vs a)
758 goTy vs (TyVar src n v) =
759 case v `lookupUsedVars` vs of
760 Nothing -> error "[BUG] normalizeVarsTy: impossible lookup fail"
761 Just v' -> TyVar src n v'
762 goTy vs (TyFam src _len fam as) = TyFam src (lenVars vs) fam $ goTys vs as
765 UsedVars src vs vs' ->
768 goTys _vs TypesZ = TypesZ
769 goTys vs (TypesS t ts) =
774 data Types src vs (ts::[K.Type]) where
775 TypesZ :: Types src vs '[]
776 TypesS :: Type src vs t
778 -> Types src vs (Proxy t ': ts)
781 type instance SourceOf (Types src vs ts) = src
782 instance ConstsOf (Types src vs ts) where
783 constsOf TypesZ = Set.empty
784 constsOf (TypesS t ts) = constsOf t `Set.union` constsOf ts
785 type instance VarsOf (Types src vs ts) = vs
786 instance UsedVarsOf (Types src vs ts) where
787 usedVarsOf vs TypesZ k = k vs
788 usedVarsOf vs (TypesS t ts) k =
789 usedVarsOf vs t $ \vs' ->
791 instance VarOccursIn (Types src vs ts) where
792 varOccursIn _v TypesZ = False
793 varOccursIn v (TypesS t ts) = varOccursIn v t || varOccursIn v ts
796 (forall kt (t::kt). Type src vs t -> Type src vs' t) ->
799 mapTys _f TypesZ = TypesZ
800 mapTys f (TypesS t ts) = f t `TypesS` mapTys f ts
803 (forall k (t::k). Type src vs t -> acc -> acc) ->
804 Types src vs ts -> acc -> acc
805 foldlTys _f TypesZ acc = acc
806 foldlTys f (TypesS t ts) acc = foldlTys f ts (f t acc)
810 typeOf :: a t -> Type (SourceOf (a t)) (VarsOf (a t)) t