1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE ConstraintKinds #-}
4 {-# LANGUAGE PatternSynonyms #-}
5 {-# LANGUAGE PolyKinds #-}
6 {-# LANGUAGE TypeInType #-}
7 {-# LANGUAGE UndecidableInstances #-}
8 {-# LANGUAGE UndecidableSuperClasses #-}
9 {-# OPTIONS_GHC -fno-warn-orphans #-}
10 {-# OPTIONS_GHC -fno-warn-missing-pattern-synonym-signatures #-}
11 module Language.Symantic.Typing.Type
12 ( module Language.Symantic.Typing.Type
19 import Control.Applicative (Alternative(..))
20 import Data.Maybe (isJust)
22 import Data.Semigroup ((<>))
24 import Data.Type.Equality
26 import qualified Data.Kind as K
27 import qualified Data.Set as Set
29 import Language.Symantic.Grammar
30 import Language.Symantic.Typing.List
31 import Language.Symantic.Typing.Kind
32 import Language.Symantic.Typing.Variable
33 import Language.Symantic.Typing.Module
36 data Type (src::K.Type) (vs::[K.Type]) (t::kt) where
37 TyConst :: src -> Len vs
40 TyApp :: src -> Type src vs f
43 -- NOTE: @f a@ decomposition made possible
44 -- at the cost of requiring @TypeInType@.
45 TyVar :: src -> NameVar
48 TyFam :: src -> Len vs
51 -> Type src vs (Fam fam hs)
53 TyPi :: src -> Type src (arg ': vs) res
54 -> Type (Pi arg res) src vs
55 TyQuant :: src -> NameHint
57 -> (forall (v::kv). Type src vs v -> TypeK src vs k)
59 TyEq :: src -> Type x src vs
61 -> Type (x #~ y) src vs
64 pattern a :$ b <- TyApp _ a b
66 pattern a :@ b <- TyApp _ a b
69 instance Source src => Eq (Type src vs t) where
70 x == y = isJust $ eqType x y
71 instance SourceInj (TypeVT src) src => TestEquality (Type src vs) where
74 type instance SourceOf (Type src vs t) = src
75 instance Source src => Sourced (Type src vs t) where
76 sourceOf (TyConst src _l _c) = src
77 sourceOf (TyApp src _f _a) = src
78 sourceOf (TyVar src _n _v) = src
79 sourceOf (TyFam src _l _f _as) = src
81 setSource (TyConst _src l c) src = TyConst src l c
82 setSource (TyApp _src f a) src = TyApp src f a
83 setSource (TyVar _src n v) src = TyVar src n v
84 setSource (TyFam _src l f as) src = TyFam src l f as
86 instance SourceInj (TypeVT src) src => KindOf (Type src vs) where
87 kindOf t = kindOfType t `withSource` TypeVT t
89 instance ConstsOf (Type src vs t) where
90 constsOf (TyConst _src _len c) = Set.singleton $ ConstC c
91 constsOf TyVar{} = Set.empty
92 constsOf (TyApp _src f a) = constsOf f `Set.union` constsOf a
93 constsOf (TyFam _src _len fam as) = constsOf fam `Set.union` constsOf as
95 type instance VarsOf (Type src vs t) = vs
96 instance LenVars (Type src vs t) where
97 lenVars (TyConst _src len _c) = len
98 lenVars (TyApp _src f _a) = lenVars f
99 lenVars (TyVar _src _n v) = lenVars v
100 lenVars (TyFam _src l _f _as) = l
101 instance UsedVarsOf (Type src vs t) where
102 usedVarsOf vs TyConst{} k = k vs
103 usedVarsOf vs (TyFam _src _len _fam as) k = usedVarsOf vs as k
104 usedVarsOf vs (TyApp _src f a) k =
105 usedVarsOf vs f $ \vs' ->
107 usedVarsOf vs (TyVar _src _n v) k =
108 case insertUsedVars v vs of
111 instance VarOccursIn (Type src vs t) where
112 varOccursIn v (TyVar _src _n v') | Just{} <- v `eqVarKi` v' = False
113 varOccursIn _v TyVar{} = False
114 varOccursIn _v TyConst{} = False
115 varOccursIn v (TyApp _src f a) = varOccursIn v f || varOccursIn v a
116 varOccursIn v (TyFam _src _len _fam as) = varOccursIn v as
117 instance AllocVars (Type src) where
118 allocVarsL len (TyConst src l c) = TyConst src (addLen len l) c
119 allocVarsL len (TyApp src f a) = TyApp src (allocVarsL len f) (allocVarsL len a)
120 allocVarsL len (TyVar src n v) = TyVar src n $ allocVarsL len v
121 allocVarsL len (TyFam src l f as) = TyFam src (addLen len l) f $ allocVarsL len `mapTys` as
123 allocVarsR len (TyConst src l c) = TyConst src (addLen l len) c
124 allocVarsR len (TyApp src f a) = TyApp src (allocVarsR len f) (allocVarsR len a)
125 allocVarsR len (TyVar src n v) = TyVar src n $ allocVarsR len v
126 allocVarsR len (TyFam src l f as) = TyFam src (addLen l len) f $ allocVarsR len `mapTys` as
128 -- | Like 'TyConst', but using 'noSource', 'lenInj' and 'constInj'.
130 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
132 forall kc (c::kc) src vs.
136 KindInjP (Ty_of_Type kc) =>
137 Type_of_Ty (Ty_of_Type kc) ~ kc =>
139 tyConst = TyConst noSource lenInj constInj
140 -- NOTE: The forall brings @c@ first in the type variables,
141 -- which is convenient to use @TypeApplications@.
143 -- | Like 'tyConst', but not using 'lenInj'.
145 forall kc (c::kc) src vs.
148 KindInjP (Ty_of_Type kc) =>
149 Type_of_Ty (Ty_of_Type kc) ~ kc =>
152 tyConstLen len = TyConst noSource len constInj
154 -- | Like 'TyApp', but using 'noSource'.
160 tyApp = TyApp noSource
163 -- | Like 'TyVar', but using 'noSource'.
169 tyVar = TyVar noSource
171 -- | Like 'TyFam', but using 'noSource'.
173 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
175 forall kc (c::kc) as vs src.
179 KindInjP (Ty_of_Type kc) =>
180 Type_of_Ty (Ty_of_Type kc) ~ kc =>
182 Type src vs (Fam c as)
183 tyFam = TyFam noSource lenInj (constInj @c)
185 -- | Qualify a 'Type'.
192 Type src vs (q #> t)) ->
195 case proveConstraint q of
196 Just Dict -> f Dict t
201 -- | Existential 'Type' of a known 'Kind'.
202 data TypeK src vs kt = forall (t::kt). TypeK (Type src vs t)
203 type instance SourceOf (TypeK src vs ki) = src
204 type instance VarsOf (TypeK src vs ki) = vs
205 instance Source src => Eq (TypeK src vs ki) where
206 TypeK x == TypeK y = isJust $ eqType x y
209 -- | Existential for 'Type'.
210 data TypeT src vs = forall t. TypeT (Type src vs t)
211 type instance SourceOf (TypeT src vs) = src
212 type instance VarsOf (TypeT src vs) = vs
213 instance Source src => Eq (TypeT src vs) where
214 TypeT x == TypeT y = isJust $ eqTypeKi x y
217 -- | Existential polymorphic 'Type'.
218 data TypeVT src = forall vs t. TypeVT (Type src vs t)
219 type instance SourceOf (TypeVT src) = src
220 instance Source src => Eq (TypeVT src) where
221 TypeVT x == TypeVT y =
222 let (x', y') = appendVars x y in
223 isJust $ eqTypeKi x' y'
226 -- | /Type constant/.
227 data Const src (c::kc) where
228 Const :: Constable c => Kind src kc -> Const src (c::kc)
230 type instance SourceOf (Const src c) = src
231 instance ConstsOf (Const src c) where
232 constsOf = Set.singleton . ConstC
233 instance SourceInj (ConstC src) src => KindOf (Const src) where
234 kindOf c@(Const kc) = kc `withSource` ConstC c
236 kindOfConst :: Const src (t::kt) -> Kind src kt
237 kindOfConst (Const kc) = kc
239 -- ** Class 'Constable'
244 , ClassInstancesFor c
251 , ClassInstancesFor c
255 -- ** Class 'ConstsOf'
256 -- | Return the 'Const's used by something.
257 class ConstsOf a where
258 constsOf :: a -> Set (ConstC (SourceOf a))
262 = forall c. ConstC (Const src c)
263 instance Eq (ConstC src) where
264 ConstC x == ConstC y = isJust $ x `eqConstKi` y
265 instance Ord (ConstC src) where
266 ConstC x `compare` ConstC y = x `ordConst` y
269 -- | The function 'Type' @(->)@,
270 -- with an infix notation more readable.
277 (~>) a b = (tyConstLen @(K (->)) @(->) (lenVars a) `tyApp` a) `tyApp` b
279 instance FixityOf (->) where
280 fixityOf _c = Just $ Fixity2 $ infixR 0
281 {- NOTE: defined in Lib.Function
282 instance ClassInstancesFor (->)
283 instance TypeInstancesFor (->)
286 -- ** Type family 'FunArg'
287 type family FunArg (fun::K.Type) :: K.Type where
288 FunArg (q #> f) = FunArg f
291 -- ** Type family 'FunRes'
292 type family FunRes (fun::K.Type) :: K.Type where
293 FunRes (q #> f) = FunRes f
296 -- | Return, when the given 'Type' is a function,
297 -- the argument of that function.
300 SourceInj (TypeVT src) src =>
303 Maybe ( Type src tys (FunArg t)
304 , Type src tys (FunRes t) )
305 unTyFun ty_ini = go ty_ini
309 Maybe ( Type src tys (FunArg x)
310 , Type src tys (FunRes x) )
311 go (TyApp _ (TyApp _ (TyConst _ _ f) a) b)
312 | Just HRefl <- proj_ConstKi @(K (->)) @(->) f
313 = Just ((a `withSource` TypeVT ty_ini), (b `withSource` TypeVT ty_ini))
314 go (TyApp _ (TyApp _ (TyConst _ _ f) _a) b)
315 | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) f
318 go TyConst{} = Nothing
323 -- | /Type constant/ to qualify a 'Type'.
324 newtype (#>) (q::K.Constraint) (a::K.Type) = Qual (q => a)
325 instance NameTyOf (#>) where
326 nameTyOf _c = [] `Mod` "=>"
327 instance FixityOf (#>) where
328 fixityOf _c = Just $ Fixity2 $ infixR 0
329 instance ClassInstancesFor (#>)
330 instance TypeInstancesFor (#>)
332 -- | Qualify a 'Type'.
338 (#>) q t = (tyConstLen @(K (#>)) @(#>) (lenVars q) `tyApp` q) `tyApp` t
340 -- NOTE: should actually be (-1)
341 -- to compose well with @infixr 0 (->)@
342 -- but this is not allowed by GHC.
345 -- | 'K.Constraint' union.
346 class (x, y) => x # y
347 instance (x, y) => x # y
348 instance NameTyOf (#) where
349 nameTyOf _c = [] `Mod` ","
350 instance FixityOf (#) where
351 fixityOf _c = Just $ Fixity2 $ infixB SideL 0
352 instance ClassInstancesFor (#) where
353 proveConstraintFor _c (TyApp _ (TyApp _ c q0) q1)
354 | Just HRefl <- proj_ConstKiTy @(K (#)) @(#) c
355 , Just Dict <- proveConstraint q0
356 , Just Dict <- proveConstraint q1
358 proveConstraintFor _c _q = Nothing
359 instance TypeInstancesFor (#)
361 -- | Unify two 'K.Constraint's.
366 Type src vs (qx # qy)
367 (#) a b = (tyConstLen @(K (#)) @(#) (lenVars a) `tyApp` a) `tyApp` b
370 -- ** Type @(() :: @'K.Constraint'@)@
371 instance NameTyOf (()::K.Constraint) where
372 nameTyOf _c = [] `Mod` "()"
373 -- instance FixityOf (()::Constraint)
374 instance ClassInstancesFor (()::K.Constraint) where
375 proveConstraintFor _c q
376 | Just HRefl <- proj_ConstKiTy @K.Constraint @(()::K.Constraint) q
378 proveConstraintFor _c _q = Nothing
379 instance TypeInstancesFor (()::K.Constraint)
381 -- | Like 'noConstraintLen', but using 'lenInj'.
382 noConstraint :: Source src => LenInj vs => Type src vs (()::K.Constraint)
383 noConstraint = tyConstLen @K.Constraint @(()::K.Constraint) lenInj
385 -- | Empty 'K.Constraint'.
386 noConstraintLen :: Source src => Len vs -> Type src vs (()::K.Constraint)
387 noConstraintLen = tyConstLen @K.Constraint @(()::K.Constraint)
390 -- | /Type equality/ 'K.Constraint'.
391 class (x ~ y) => x #~ y
392 instance (x ~ y) => x #~ y
393 instance NameTyOf (#~) where
394 nameTyOf _c = [] `Mod` "~"
396 instance FixityOf (#~) where
397 fixityOf _ = Just $ Fixity2 $ infixB SideL 0
401 ) => ClassInstancesFor ((#~)::k -> k -> K.Constraint) where
402 proveConstraintFor _c (TyApp _ (TyApp _ c a) b)
403 | Just HRefl <- proj_ConstKiTy @(k -> k -> K.Constraint) @(#~) c
404 , Just Refl <- eqType a b
406 proveConstraintFor _c _q = Nothing
407 instance TypeInstancesFor (#~)
409 -- | Constraint two 'Type's to be equal.
415 Type src vs (a :: k) ->
416 Type src vs (b :: k) ->
418 (#~) a b = (tyConstLen @(K (#~)) @(#~) (lenVars a) `tyApp` a) `tyApp` b
421 -- | /Type equality/.
424 Type src tys (x::k) ->
425 Type src tys (y::k) ->
427 eqType x y | Just HRefl <- eqTypeKi x y = Just Refl
428 eqType _x _y = Nothing
430 -- | /Heterogeneous type equality/:
431 -- return two proofs when two 'Type's are equals:
432 -- one for the type and one for the kind.
435 Type src tys (x::kx) ->
436 Type src tys (y::ky) ->
438 eqTypeKi (TyConst _sx _lx x) (TyConst _sy _ly y) = eqConstKi x y
439 eqTypeKi (TyVar _sx _nx x) (TyVar _sy _ny y) = eqVarKi x y
440 eqTypeKi (TyApp _sx fx ax) (TyApp _sy fy ay)
441 | Just HRefl <- eqTypeKi fx fy
442 , Just HRefl <- eqTypeKi ax ay
444 eqTypeKi (TyFam _ _lx fx ax) (TyFam _ _ly fy ay)
445 | Just HRefl <- eqConstKi fx fy
446 , Just HRefl <- eqTypes ax ay
448 -- NOTE: 'TyFam' is expanded as much as possible.
449 eqTypeKi x@TyFam{} y = eqTypeKi (expandFam x) y
450 eqTypeKi x y@TyFam{} = eqTypeKi x (expandFam y)
451 eqTypeKi _x _y = Nothing
458 eqTypes TypesZ TypesZ = Just HRefl
459 eqTypes (x `TypesS` sx) (y `TypesS` sy)
460 | Just HRefl <- eqTypeKi x y
461 , Just HRefl <- eqTypes sx sy
463 eqTypes _x _y = Nothing
466 -- | Compare two 'Type's.
469 Type src tys (x::kx) ->
470 Type src tys (y::ky) ->
472 -- NOTE: 'TyFam' is expanded as much as possible.
473 ordType (TyFam _ _lx fx ax) (TyFam _ _ly fy ay) = ordConst fx fy <> ordTypes ax ay
474 ordType x@TyFam{} y = ordType (expandFam x) y
475 ordType x y@TyFam{} = ordType x (expandFam y)
476 ordType (TyConst _sx _lx x) (TyConst _sy _ly y) = ordConst x y
477 ordType TyConst{} _y = LT
478 ordType _x TyConst{} = GT
479 ordType (TyVar _sx _kx x) (TyVar _sy _ky y) = ordVarKi x y
480 ordType TyVar{} _y = LT
481 ordType _x TyVar{} = GT
482 ordType (TyApp _sx fx xx) (TyApp _sy fy xy) = ordType fx fy <> ordType xx xy
483 -- ordType TyApp{} _y = LT
484 -- ordType _x TyApp{} = GT
491 ordTypes TypesZ TypesZ = EQ
492 ordTypes TypesZ TypesS{} = LT
493 ordTypes TypesS{} TypesZ = GT
494 ordTypes (x `TypesS` sx) (y `TypesS` sy) = ordType x y <> ordTypes sx sy
497 -- | Captures the proof of a 'K.Constraint'
498 -- (and its dictionaries when it contains type classes):
499 -- pattern matching on the 'Dict' constructor
500 -- brings the 'K.Constraint' into scope.
504 -- ** Class 'ClassInstancesFor'
505 class ClassInstancesFor c where
511 proveConstraintFor _c _q = Nothing
513 proveConstraint :: Source src => Type src vs q -> Maybe (Qual q)
514 proveConstraint q = foldr proj Nothing $ constsOf q
515 where proj (ConstC c@Const{}) = (<|> proveConstraintFor c q)
522 KindInjP (Ty_of_Type (K c)) =>
523 Type_of_Ty (Ty_of_Type (K c)) ~ K c =>
525 constInj = Const $ kindInjP @(Ty_of_Type (K c)) noSource
536 {- XXX: not working because of GHC's #12933
540 (K c ~ Type_of_Ty (Ty_of_Type (K c))) =>
544 proj_ConstKi (Const ku) = do
545 Refl <- eqKind ku $ kindInj @(K c) @()
550 -- | Like 'proj_ConstKi', but with kind equality already known.
556 proj_Const Const{} = eqT
558 -- | Like 'proj_Const', but on a 'Type'.
564 proj_ConstTy (TyConst _src _len Const{}) = eqT
565 proj_ConstTy _ = Nothing
567 -- | XXX: 'proj_ConstKi' must be given the @kc@ kind explicitely,
568 -- when used multiple times in the same pattern match,
569 -- because of GHC's #12933.
571 forall kc (c::kc) u src.
573 (kc ~ Type_of_Ty (Ty_of_Type kc)) =>
574 KindInjP (Ty_of_Type kc) =>
577 proj_ConstKi (Const ku) = do
578 Refl <- eqKind ku $ kindInjP @(Ty_of_Type kc) ()
582 -- | Like 'proj_ConstKi', but on a 'Type'.
584 forall kc (c::kc) u src vs.
586 (kc ~ Type_of_Ty (Ty_of_Type kc)) =>
587 KindInjP (Ty_of_Type kc) =>
590 proj_ConstKiTy (TyConst _src _len c) = proj_ConstKi c
591 proj_ConstKiTy _ = Nothing
594 Const src_x (x::k) ->
595 Const src_y (y::k) ->
597 eqConst (Const _kx) (Const _ky) = eqT
598 instance Eq (Const src c) where
599 x == y = isJust $ eqConstKi x y
600 instance Ord (Const src c) where
604 forall src_x src_y kx ky x y.
605 Const src_x (x::kx) ->
606 Const src_y (y::ky) ->
608 eqConstKi (Const kx) (Const ky)
609 | Just Refl <- eqKind kx ky
610 , Just (Refl::x:~:y) <- eqT
612 eqConstKi _x _y = Nothing
615 forall src_x src_y kx ky x y.
616 Const src_x (x::kx) ->
617 Const src_y (y::ky) ->
619 ordConst x@Const{} y@Const{} =
620 typeRep x `compare` typeRep y
624 Type src tys (t::kt) ->
626 normalizeQualsTy = go
630 Type src tys (a::ka) ->
633 go (TyApp s1 (TyApp s0 f q0) q1)
634 | Just HRefl <- proj_ConstKiTy @(K (#)) @(#) f
636 , TypeK q1' <- go q1 =
637 case (proveConstraint q0', proveConstraint q1') of
638 (Just{}, Just{}) -> TypeK $ tyConstLen @K.Constraint @(()::K.Constraint) $ lenVars q0
639 (Just{}, Nothing) -> TypeK q1'
640 (Nothing, Just{}) -> TypeK q0'
641 (Nothing, Nothing) ->
642 case q0' `ordType` q1' of
643 LT -> TypeK $ TyApp s1 (TyApp s0 f q0') q1'
645 GT -> TypeK $ TyApp s0 (TyApp s1 f q1') q0'
647 go (TyApp s1 (TyApp s0 f0@(TyConst sf0 lf0 _) q0) (TyApp s2 (TyApp s3 f1 q1) t))
648 | Just HRefl <- proj_ConstKiTy @(K (#>)) @(#>) f0
649 , Just HRefl <- proj_ConstKiTy @(K (#>)) @(#>) f1
651 , TypeK q1' <- go q1 =
652 case (proveConstraint q0', proveConstraint q1') of
653 (Just{}, Just{}) -> TypeK t
654 (Just{}, Nothing) -> TypeK $ TyApp s2 (TyApp s3 f1 q1) t
655 (Nothing, Just{}) -> TypeK $ TyApp s1 (TyApp s0 f0 q0) t
656 (Nothing, Nothing) ->
657 case q0' `ordType` q1' of
659 let q0q1 = TyApp s2 (TyApp s0 (TyConst sf0 lf0 $ constInj @(#)) q0') q1' in
660 TypeK $ TyApp s2 (TyApp s3 f1 q0q1) t
661 EQ -> TypeK $ TyApp s2 (TyApp s3 f1 q1') t
663 let q1q0 = TyApp s0 (TyApp s2 (TyConst sf0 lf0 $ constInj @(#)) q1') q0' in
664 TypeK $ TyApp s2 (TyApp s3 f1 q1q0) t
666 go (TyApp _sa (TyApp _sq (TyConst _sc _lc c) q) t)
667 | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) c =
668 case proveConstraint q of
676 | Just Refl <- KiConstraint () `eqKind` kindOfType q
677 = case proveConstraint q of
678 Just{} -> TypeK $ tyConstLen @K.Constraint @(()::K.Constraint) $ lenVars q
683 = TypeK $ TyApp src f' a'
684 go t@TyFam{} = go (expandFam t)
685 go t@TyConst{} = TypeK t
686 go t@TyVar{} = TypeK t
688 -- * Type family 'Fam'
689 -- | Apply the /type family/ selected by @fam@
690 -- to a list of types (within a 'Proxy').
692 -- | NOTE: |Fam|'s 'Kind' MUST be the same than @fam@'s.
693 type family Fam (fam::k) (hs::[K.Type]) :: k
694 -- type family FamKi fam :: K.Type
696 -- ** Class 'TypeInstancesFor'
697 class TypeInstancesFor (c::kc) where
704 -> Maybe (Type src vs (Fam fam ts))
705 expandFamFor _c _len _fam _as = Nothing
707 -- ** Class 'ExpandFam'
708 class ExpandFam a where
709 expandFam :: Source (SourceOf a) => a -> a
710 instance ExpandFam (Type src vs t) where
711 expandFam t@TyConst{} = t
712 expandFam t@TyVar{} = t
713 expandFam (TyApp src f a) = TyApp src (expandFam f) (expandFam a)
714 expandFam t@(TyFam _src len fam as) =
715 case foldr proj Nothing $ constsOf fam `Set.union` constsOf as of
717 Just t' -> expandFam t'
718 where proj (ConstC c@Const{}) = (<|> expandFamFor c len fam as)
719 -- XXX: this can loop…
721 -- | Like 'kindOf' but keep the old 'Source'.
722 kindOfType :: Type src vs (t::kt) -> Kind src kt
723 kindOfType (TyConst _src _l c) = kindOfConst c
724 kindOfType (TyApp _src f _a) =
726 KiFun _src_kf _kx kf -> kf
727 kindOfType (TyVar _src _n v) = kindOfVar v
728 kindOfType (TyFam _src _len fam _as) = kindOfConst fam
730 -- | Remove unused 'Var's from the context.
732 Type src vs (t::kt) ->
733 (forall vs'. Type src vs' (t::kt) -> ret) -> ret
734 normalizeVarsTy ty k =
735 usedVarsOf UsedVarsZ ty $ \vs ->
739 UsedVars src vs vs' ->
742 goTy vs (TyConst src _len c) = TyConst src (lenVars vs) c
743 goTy vs (TyApp src f a) = TyApp src (goTy vs f) (goTy vs a)
744 goTy vs (TyVar src n v) =
745 case v `lookupUsedVars` vs of
746 Nothing -> error "[BUG] normalizeVarsTy: impossible lookup fail"
747 Just v' -> TyVar src n v'
748 goTy vs (TyFam src _len fam as) = TyFam src (lenVars vs) fam $ goTys vs as
751 UsedVars src vs vs' ->
754 goTys _vs TypesZ = TypesZ
755 goTys vs (TypesS t ts) =
760 data Types src vs (ts::[K.Type]) where
761 TypesZ :: Types src vs '[]
762 TypesS :: Type src vs t
764 -> Types src vs (Proxy t ': ts)
767 type instance SourceOf (Types src vs ts) = src
768 instance ConstsOf (Types src vs ts) where
769 constsOf TypesZ = Set.empty
770 constsOf (TypesS t ts) = constsOf t `Set.union` constsOf ts
771 type instance VarsOf (Types src vs ts) = vs
772 instance UsedVarsOf (Types src vs ts) where
773 usedVarsOf vs TypesZ k = k vs
774 usedVarsOf vs (TypesS t ts) k =
775 usedVarsOf vs t $ \vs' ->
777 instance VarOccursIn (Types src vs ts) where
778 varOccursIn _v TypesZ = False
779 varOccursIn v (TypesS t ts) = varOccursIn v t || varOccursIn v ts
782 (forall kt (t::kt). Type src vs t -> Type src vs' t) ->
785 mapTys _f TypesZ = TypesZ
786 mapTys f (TypesS t ts) = f t `TypesS` mapTys f ts
789 (forall k (t::k). Type src vs t -> acc -> acc) ->
790 Types src vs ts -> acc -> acc
791 foldlTys _f TypesZ acc = acc
792 foldlTys f (TypesS t ts) acc = foldlTys f ts (f t acc)
796 typeOf :: a t -> Type (SourceOf (a t)) (VarsOf (a t)) t