1 {-# language ConstraintKinds #-}
2 {-# LANGUAGE AllowAmbiguousTypes #-}
4 {-# LANGUAGE TypeInType #-}
5 {-# LANGUAGE PolyKinds #-}
6 {-# LANGUAGE UndecidableInstances #-}
7 {-# LANGUAGE UndecidableSuperClasses #-}
8 {-# OPTIONS_GHC -fno-warn-orphans #-}
9 module Language.Symantic.Typing.Type
10 ( module Language.Symantic.Typing.Type
17 import Control.Applicative (Alternative(..))
18 import Data.Maybe (isJust)
20 import Data.Semigroup ((<>))
22 import Data.Type.Equality
24 import qualified Data.Kind as K
25 import qualified Data.Set as Set
27 import Language.Symantic.Grammar
28 import Language.Symantic.Typing.List
29 import Language.Symantic.Typing.Kind
30 import Language.Symantic.Typing.Variable
33 data Type (src::K.Type) (vs::[K.Type]) (t::kt) where
34 TyConst :: src -> Len vs
37 TyApp :: src -> Type src vs f
40 -- NOTE: @f a@ decomposition made possible
41 -- at the cost of requiring @TypeInType@.
42 TyVar :: src -> NameVar
45 TyFam :: src -> Len vs
48 -> Type src vs (Fam fam hs)
50 TyPi :: src -> Type src (arg ': vs) res
51 -> Type (Pi arg res) src vs
52 TyQuant :: src -> NameHint
54 -> (forall (v::kv). Type src vs v -> TypeK src vs k)
56 TyEq :: src -> Type x src vs
58 -> Type (x #~ y) src vs
61 instance Source src => Eq (Type src vs t) where
62 x == y = isJust $ eqType x y
63 instance SourceInj (TypeVT src) src => TestEquality (Type src vs) where
66 type instance SourceOf (Type src vs t) = src
67 instance Source src => Sourced (Type src vs t) where
68 sourceOf (TyConst src _l _c) = src
69 sourceOf (TyApp src _f _a) = src
70 sourceOf (TyVar src _n _v) = src
71 sourceOf (TyFam src _l _f _as) = src
73 setSource (TyConst _src l c) src = TyConst src l c
74 setSource (TyApp _src f a) src = TyApp src f a
75 setSource (TyVar _src n v) src = TyVar src n v
76 setSource (TyFam _src l f as) src = TyFam src l f as
78 instance SourceInj (TypeVT src) src => KindOf (Type src vs) where
79 kindOf t = kindOfType t `withSource` TypeVT t
81 instance ConstsOf (Type src vs t) where
82 constsOf (TyConst _src _len c) = Set.singleton $ ConstC c
83 constsOf TyVar{} = Set.empty
84 constsOf (TyApp _src f a) = constsOf f `Set.union` constsOf a
85 constsOf (TyFam _src _len fam as) = constsOf fam `Set.union` constsOf as
87 type instance VarsOf (Type src vs t) = vs
88 instance LenVars (Type src vs t) where
89 lenVars (TyConst _src len _c) = len
90 lenVars (TyApp _src f _a) = lenVars f
91 lenVars (TyVar _src _n v) = lenVars v
92 lenVars (TyFam _src l _f _as) = l
93 instance UsedVarsOf (Type src vs t) where
94 usedVarsOf vs TyConst{} k = k vs
95 usedVarsOf vs (TyFam _src _len _fam as) k = usedVarsOf vs as k
96 usedVarsOf vs (TyApp _src f a) k =
97 usedVarsOf vs f $ \vs' ->
99 usedVarsOf vs (TyVar _src _n v) k =
100 case insertUsedVars v vs of
103 instance VarOccursIn (Type src vs t) where
104 varOccursIn v (TyVar _src _n v') | Just{} <- v `eqVarKi` v' = False
105 varOccursIn _v TyVar{} = False
106 varOccursIn _v TyConst{} = False
107 varOccursIn v (TyApp _src f a) = varOccursIn v f || varOccursIn v a
108 varOccursIn v (TyFam _src _len _fam as) = varOccursIn v as
109 instance AllocVars (Type src) where
110 allocVarsL len (TyConst src l c) = TyConst src (addLen len l) c
111 allocVarsL len (TyApp src f a) = TyApp src (allocVarsL len f) (allocVarsL len a)
112 allocVarsL len (TyVar src n v) = TyVar src n $ allocVarsL len v
113 allocVarsL len (TyFam src l f as) = TyFam src (addLen len l) f $ allocVarsL len `mapTys` as
115 allocVarsR len (TyConst src l c) = TyConst src (addLen l len) c
116 allocVarsR len (TyApp src f a) = TyApp src (allocVarsR len f) (allocVarsR len a)
117 allocVarsR len (TyVar src n v) = TyVar src n $ allocVarsR len v
118 allocVarsR len (TyFam src l f as) = TyFam src (addLen l len) f $ allocVarsR len `mapTys` as
120 -- | Like 'TyConst', but using 'noSource', 'lenInj' and 'constInj'.
122 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
124 forall kc (c::kc) src vs.
128 KindInjP (Ty_of_Type kc) =>
129 Type_of_Ty (Ty_of_Type kc) ~ kc =>
131 tyConst = TyConst noSource lenInj constInj
132 -- NOTE: The forall brings @c@ first in the type variables,
133 -- which is convenient to use @TypeApplications@.
135 -- | Like 'tyConst', but not using 'lenInj'.
137 forall kc (c::kc) src vs.
140 KindInjP (Ty_of_Type kc) =>
141 Type_of_Ty (Ty_of_Type kc) ~ kc =>
144 tyConstLen len = TyConst noSource len constInj
146 -- | Like 'TyApp', but using 'noSource'.
152 tyApp = TyApp noSource
155 -- | Like 'TyVar', but using 'noSource'.
161 tyVar = TyVar noSource
163 -- | Like 'TyFam', but using 'noSource'.
165 -- FIXME: remove @kc@ when GHC's #12933 is fixed.
167 forall kc (c::kc) as vs src.
171 KindInjP (Ty_of_Type kc) =>
172 Type_of_Ty (Ty_of_Type kc) ~ kc =>
174 Type src vs (Fam c as)
175 tyFam = TyFam noSource lenInj (constInj @c)
177 -- | Qualify a 'Type'.
184 Type src vs (q #> t)) ->
187 case proveConstraint q of
188 Just Dict -> f Dict t
193 -- | Existential 'Type' of a known 'Kind'.
194 data TypeK src vs kt = forall (t::kt). TypeK (Type src vs t)
195 type instance SourceOf (TypeK src vs ki) = src
196 type instance VarsOf (TypeK src vs ki) = vs
197 instance Source src => Eq (TypeK src vs ki) where
198 TypeK x == TypeK y = isJust $ eqType x y
201 -- | Existential for 'Type'.
202 data TypeT src vs = forall t. TypeT (Type src vs t)
203 type instance SourceOf (TypeT src vs) = src
204 type instance VarsOf (TypeT src vs) = vs
205 instance Source src => Eq (TypeT src vs) where
206 TypeT x == TypeT y = isJust $ eqTypeKi x y
209 -- | Existential polymorphic 'Type'.
210 data TypeVT src = forall vs t. TypeVT (Type src vs t)
211 type instance SourceOf (TypeVT src) = src
212 instance Source src => Eq (TypeVT src) where
213 TypeVT x == TypeVT y =
214 let (x', y') = appendVars x y in
215 isJust $ eqTypeKi x' y'
218 -- | /Type constant/.
219 data Const src (c::kc) where
224 type instance SourceOf (Const src c) = src
225 instance ConstsOf (Const src c) where
226 constsOf = Set.singleton . ConstC
227 instance SourceInj (ConstC src) src => KindOf (Const src) where
228 kindOf c@(Const kc) = kc `withSource` ConstC c
230 kindOfConst :: Const src (t::kt) -> Kind src kt
231 kindOfConst (Const kc) = kc
233 -- ** Class 'Constable'
237 , ClassInstancesFor c
243 , ClassInstancesFor c
247 -- ** Class 'ConstsOf'
248 -- | Return the 'Const's used by something.
249 class ConstsOf a where
250 constsOf :: a -> Set (ConstC (SourceOf a))
252 -- ** Class 'FixityOf'
253 -- | Return the 'Fixity' of something.
254 class FixityOf (c::kc) where
255 fixityOf :: proxy c -> Maybe Fixity
256 fixityOf _c = Nothing
257 instance FixityOf (c::K.Type)
258 instance FixityOf (c::K.Constraint)
261 instance Show (Const src c) where
262 showsPrec p c@Const{} = showsPrec p $ typeRep c
266 = forall c. ConstC (Const src c)
267 instance Eq (ConstC src) where
268 ConstC x == ConstC y = isJust $ x `eqConstKi` y
269 instance Ord (ConstC src) where
270 ConstC x `compare` ConstC y = x `ordConst` y
273 -- | The function 'Type' @(->)@,
274 -- with an infix notation more readable.
281 (~>) a b = (tyConstLen @(K (->)) @(->) (lenVars a) `tyApp` a) `tyApp` b
283 instance FixityOf (->) where
284 fixityOf _c = Just $ Fixity2 $ infixR 0
285 {- NOTE: defined in Lib.Function
286 instance ClassInstancesFor (->)
287 instance TypeInstancesFor (->)
290 -- ** Type family 'FunArg'
291 type family FunArg (fun::K.Type) :: K.Type where
292 FunArg (q #> f) = FunArg f
295 -- ** Type family 'FunRes'
296 type family FunRes (fun::K.Type) :: K.Type where
297 FunRes (q #> f) = FunRes f
300 -- | Return, when the given 'Type' is a function,
301 -- the argument of that function.
304 SourceInj (TypeVT src) src =>
307 Maybe ( Type src tys (FunArg t)
308 , Type src tys (FunRes t) )
309 unTyFun ty_ini = go ty_ini
313 Maybe ( Type src tys (FunArg x)
314 , Type src tys (FunRes x) )
315 go (TyApp _ (TyApp _ (TyConst _ _ f) a) b)
316 | Just HRefl <- proj_ConstKi @(K (->)) @(->) f
317 = Just ((a `withSource` TypeVT ty_ini), (b `withSource` TypeVT ty_ini))
318 go (TyApp _ (TyApp _ (TyConst _ _ f) _a) b)
319 | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) f
322 go TyConst{} = Nothing
327 -- | /Type constant/ to qualify a 'Type'.
328 newtype (#>) (q::K.Constraint) (a::K.Type) = Qual (q => a)
329 instance FixityOf (#>) where
330 fixityOf _c = Just $ Fixity2 $ infixR 0
331 instance ClassInstancesFor (#>)
332 instance TypeInstancesFor (#>)
334 -- | Qualify a 'Type'.
340 (#>) q t = (tyConstLen @(K (#>)) @(#>) (lenVars q) `tyApp` q) `tyApp` t
342 -- NOTE: should actually be (-1)
343 -- to compose well with @infixr 0 (->)@
344 -- but this is not allowed by GHC.
347 -- | 'K.Constraint' union.
348 class (x, y) => x # y
349 instance (x, y) => x # y
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
373 Type src vs (()::K.Constraint)
374 noConstraint = tyConstLen @K.Constraint @(()::K.Constraint) lenInj
379 Type src vs (()::K.Constraint)
380 noConstraintLen = tyConstLen @K.Constraint @(()::K.Constraint)
382 instance ClassInstancesFor (()::K.Constraint) where
383 proveConstraintFor _c q
384 | Just HRefl <- proj_ConstKiTy @K.Constraint @(()::K.Constraint) q
386 proveConstraintFor _c _q = Nothing
387 instance TypeInstancesFor (()::K.Constraint)
390 -- | /Type equality/ 'K.Constraint'.
391 class (x ~ y) => x #~ y
392 instance (x ~ y) => x #~ y
393 instance FixityOf (#~) where
394 fixityOf _ = Just $ Fixity2 $ infixB SideL 0
398 ) => ClassInstancesFor ((#~)::k -> k -> K.Constraint) where
399 proveConstraintFor _c (TyApp _ (TyApp _ c a) b)
400 | Just HRefl <- proj_ConstKiTy @(k -> k -> K.Constraint) @(#~) c
401 , Just Refl <- eqType a b
403 proveConstraintFor _c _q = Nothing
404 instance TypeInstancesFor (#~)
406 -- | Constraint two 'Type's to be equal.
412 Type src vs (a :: k) ->
413 Type src vs (b :: k) ->
415 (#~) a b = (tyConstLen @(K (#~)) @(#~) (lenVars a) `tyApp` a) `tyApp` b
418 -- | /Type equality/.
421 Type src tys (x::k) ->
422 Type src tys (y::k) ->
424 eqType x y | Just HRefl <- eqTypeKi x y = Just Refl
425 eqType _x _y = Nothing
427 -- | /Heterogeneous type equality/:
428 -- return two proofs when two 'Type's are equals:
429 -- one for the type and one for the kind.
432 Type src tys (x::kx) ->
433 Type src tys (y::ky) ->
435 eqTypeKi (TyConst _sx _lx x) (TyConst _sy _ly y) = eqConstKi x y
436 eqTypeKi (TyVar _sx _nx x) (TyVar _sy _ny y) = eqVarKi x y
437 eqTypeKi (TyApp _sx fx ax) (TyApp _sy fy ay)
438 | Just HRefl <- eqTypeKi fx fy
439 , Just HRefl <- eqTypeKi ax ay
441 eqTypeKi (TyFam _ _lx fx ax) (TyFam _ _ly fy ay)
442 | Just HRefl <- eqConstKi fx fy
443 , Just HRefl <- eqTypes ax ay
445 -- NOTE: 'TyFam' is expanded as much as possible.
446 eqTypeKi x@TyFam{} y = eqTypeKi (expandFam x) y
447 eqTypeKi x y@TyFam{} = eqTypeKi x (expandFam y)
448 eqTypeKi _x _y = Nothing
455 eqTypes TypesZ TypesZ = Just HRefl
456 eqTypes (x `TypesS` sx) (y `TypesS` sy)
457 | Just HRefl <- eqTypeKi x y
458 , Just HRefl <- eqTypes sx sy
460 eqTypes _x _y = Nothing
463 -- | Compare two 'Type's.
466 Type src tys (x::kx) ->
467 Type src tys (y::ky) ->
469 -- NOTE: 'TyFam' is expanded as much as possible.
470 ordType (TyFam _ _lx fx ax) (TyFam _ _ly fy ay) = ordConst fx fy <> ordTypes ax ay
471 ordType x@TyFam{} y = ordType (expandFam x) y
472 ordType x y@TyFam{} = ordType x (expandFam y)
473 ordType (TyConst _sx _lx x) (TyConst _sy _ly y) = ordConst x y
474 ordType TyConst{} _y = LT
475 ordType _x TyConst{} = GT
476 ordType (TyVar _sx _kx x) (TyVar _sy _ky y) = ordVarKi x y
477 ordType TyVar{} _y = LT
478 ordType _x TyVar{} = GT
479 ordType (TyApp _sx fx xx) (TyApp _sy fy xy) = ordType fx fy <> ordType xx xy
480 -- ordType TyApp{} _y = LT
481 -- ordType _x TyApp{} = GT
488 ordTypes TypesZ TypesZ = EQ
489 ordTypes TypesZ TypesS{} = LT
490 ordTypes TypesS{} TypesZ = GT
491 ordTypes (x `TypesS` sx) (y `TypesS` sy) = ordType x y <> ordTypes sx sy
494 -- | Captures the proof of a 'K.Constraint'
495 -- (and its dictionaries when it contains type classes):
496 -- pattern matching on the 'Dict' constructor
497 -- brings the 'K.Constraint' into scope.
501 -- ** Class 'ClassInstancesFor'
502 class ClassInstancesFor c where
508 proveConstraintFor _c _q = Nothing
510 proveConstraint :: Source src => Type src vs q -> Maybe (Qual q)
511 proveConstraint q = foldr proj Nothing $ constsOf q
512 where proj (ConstC c@Const{}) = (<|> proveConstraintFor c q)
519 KindInjP (Ty_of_Type (K c)) =>
520 Type_of_Ty (Ty_of_Type (K c)) ~ K c =>
522 constInj = Const $ kindInjP @(Ty_of_Type (K c)) noSource
533 {- XXX: not working because of GHC's #12933
537 (K c ~ Type_of_Ty (Ty_of_Type (K c))) =>
541 proj_ConstKi (Const ku) = do
542 Refl <- eqKind ku $ kindInj @(K c) @()
547 -- | Like 'proj_ConstKi', but with kind equality already known.
553 proj_Const Const{} = eqT
555 -- | Like 'proj_Const', but on a 'Type'.
561 proj_ConstTy (TyConst _src _len Const{}) = eqT
562 proj_ConstTy _ = Nothing
564 -- | XXX: 'proj_ConstKi' must be given the @kc@ kind explicitely,
565 -- when used multiple times in the same pattern match,
566 -- because of GHC's #12933.
568 forall kc (c::kc) u src.
570 (kc ~ Type_of_Ty (Ty_of_Type kc)) =>
571 KindInjP (Ty_of_Type kc) =>
574 proj_ConstKi (Const ku) = do
575 Refl <- eqKind ku $ kindInjP @(Ty_of_Type kc) ()
579 -- | Like 'proj_ConstKi', but on a 'Type'.
581 forall kc (c::kc) u src vs.
583 (kc ~ Type_of_Ty (Ty_of_Type kc)) =>
584 KindInjP (Ty_of_Type kc) =>
587 proj_ConstKiTy (TyConst _src _len c) = proj_ConstKi c
588 proj_ConstKiTy _ = Nothing
591 Const src_x (x::k) ->
592 Const src_y (y::k) ->
594 eqConst (Const _kx) (Const _ky) = eqT
595 instance Eq (Const src c) where
596 x == y = isJust $ eqConstKi x y
597 instance Ord (Const src c) where
601 forall src_x src_y kx ky x y.
602 Const src_x (x::kx) ->
603 Const src_y (y::ky) ->
605 eqConstKi (Const kx) (Const ky)
606 | Just Refl <- eqKind kx ky
607 , Just (Refl::x:~:y) <- eqT
609 eqConstKi _x _y = Nothing
612 forall src_x src_y kx ky x y.
613 Const src_x (x::kx) ->
614 Const src_y (y::ky) ->
616 ordConst x@Const{} y@Const{} =
617 typeRep x `compare` typeRep y
621 Type src tys (t::kt) ->
623 normalizeQualsTy = go
627 Type src tys (a::ka) ->
630 go (TyApp s1 (TyApp s0 f q0) q1)
631 | Just HRefl <- proj_ConstKiTy @(K (#)) @(#) f
633 , TypeK q1' <- go q1 =
634 case (proveConstraint q0', proveConstraint q1') of
635 (Just{}, Just{}) -> TypeK $ tyConstLen @K.Constraint @(()::K.Constraint) $ lenVars q0
636 (Just{}, Nothing) -> TypeK q1'
637 (Nothing, Just{}) -> TypeK q0'
638 (Nothing, Nothing) ->
639 case q0' `ordType` q1' of
640 LT -> TypeK $ TyApp s1 (TyApp s0 f q0') q1'
642 GT -> TypeK $ TyApp s0 (TyApp s1 f q1') q0'
644 go (TyApp s1 (TyApp s0 f0@(TyConst sf0 lf0 _) q0) (TyApp s2 (TyApp s3 f1 q1) t))
645 | Just HRefl <- proj_ConstKiTy @(K (#>)) @(#>) f0
646 , Just HRefl <- proj_ConstKiTy @(K (#>)) @(#>) f1
648 , TypeK q1' <- go q1 =
649 case (proveConstraint q0', proveConstraint q1') of
650 (Just{}, Just{}) -> TypeK t
651 (Just{}, Nothing) -> TypeK $ TyApp s2 (TyApp s3 f1 q1) t
652 (Nothing, Just{}) -> TypeK $ TyApp s1 (TyApp s0 f0 q0) t
653 (Nothing, Nothing) ->
654 case q0' `ordType` q1' of
656 let q0q1 = TyApp s2 (TyApp s0 (TyConst sf0 lf0 $ constInj @(#)) q0') q1' in
657 TypeK $ TyApp s2 (TyApp s3 f1 q0q1) t
658 EQ -> TypeK $ TyApp s2 (TyApp s3 f1 q1') t
660 let q1q0 = TyApp s0 (TyApp s2 (TyConst sf0 lf0 $ constInj @(#)) q1') q0' in
661 TypeK $ TyApp s2 (TyApp s3 f1 q1q0) t
663 go (TyApp _sa (TyApp _sq (TyConst _sc _lc c) q) t)
664 | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) c =
665 case proveConstraint q of
673 | Just Refl <- KiConstraint () `eqKind` kindOfType q
674 = case proveConstraint q of
675 Just{} -> TypeK $ tyConstLen @K.Constraint @(()::K.Constraint) $ lenVars q
680 = TypeK $ TyApp src f' a'
681 go t@TyFam{} = go (expandFam t)
682 go t@TyConst{} = TypeK t
683 go t@TyVar{} = TypeK t
685 -- * Type family 'Fam'
686 -- | Apply the /type family/ selected by @fam@
687 -- to a list of types (within a 'Proxy').
689 -- | NOTE: |Fam|'s 'Kind' MUST be the same than @fam@'s.
690 type family Fam (fam::k) (hs::[K.Type]) :: k
691 -- type family FamKi fam :: K.Type
693 -- ** Class 'TypeInstancesFor'
694 class TypeInstancesFor (c::kc) where
701 -> Maybe (Type src vs (Fam fam ts))
702 expandFamFor _c _len _fam _as = Nothing
704 -- ** Class 'ExpandFam'
705 class ExpandFam a where
706 expandFam :: Source (SourceOf a) => a -> a
707 instance ExpandFam (Type src vs t) where
708 expandFam t@TyConst{} = t
709 expandFam t@TyVar{} = t
710 expandFam (TyApp src f a) = TyApp src (expandFam f) (expandFam a)
711 expandFam t@(TyFam _src len fam as) =
712 case foldr proj Nothing $ constsOf fam `Set.union` constsOf as of
714 Just t' -> expandFam t'
715 where proj (ConstC c@Const{}) = (<|> expandFamFor c len fam as)
716 -- XXX: this can loop…
718 -- | Like 'kindOf' but keep the old 'Source'.
719 kindOfType :: Type src vs (t::kt) -> Kind src kt
720 kindOfType (TyConst _src _l c) = kindOfConst c
721 kindOfType (TyApp _src f _a) =
723 KiFun _src_kf _kx kf -> kf
724 kindOfType (TyVar _src _n v) = kindOfVar v
725 kindOfType (TyFam _src _len fam _as) = kindOfConst fam
727 -- | Remove unused 'Var's from ther context.
729 Type src vs (t::kt) ->
730 (forall vs'. Type src vs' (t::kt) -> ret) -> ret
731 normalizeVarsTy ty k =
732 usedVarsOf UsedVarsZ ty $ \vs ->
736 UsedVars src vs vs' ->
739 goTy vs (TyConst src _len c) = TyConst src (lenVars vs) c
740 goTy vs (TyApp src f a) = TyApp src (goTy vs f) (goTy vs a)
741 goTy vs (TyVar src n v) =
742 case v `lookupUsedVars` vs of
743 Nothing -> error "[BUG] normalizeVarsTy: impossible lookup fail"
744 Just v' -> TyVar src n v'
745 goTy vs (TyFam src _len fam as) = TyFam src (lenVars vs) fam $ goTys vs as
748 UsedVars src vs vs' ->
751 goTys _vs TypesZ = TypesZ
752 goTys vs (TypesS t ts) =
757 data Types src vs (ts::[K.Type]) where
758 TypesZ :: Types src vs '[]
759 TypesS :: Type src vs t
761 -> Types src vs (Proxy t ': ts)
764 type instance SourceOf (Types src vs ts) = src
765 instance ConstsOf (Types src vs ts) where
766 constsOf TypesZ = Set.empty
767 constsOf (TypesS t ts) = constsOf t `Set.union` constsOf ts
768 type instance VarsOf (Types src vs ts) = vs
769 instance UsedVarsOf (Types src vs ts) where
770 usedVarsOf vs TypesZ k = k vs
771 usedVarsOf vs (TypesS t ts) k =
772 usedVarsOf vs t $ \vs' ->
774 instance VarOccursIn (Types src vs ts) where
775 varOccursIn _v TypesZ = False
776 varOccursIn v (TypesS t ts) = varOccursIn v t || varOccursIn v ts
779 (forall kt (t::kt). Type src vs t -> Type src vs' t) ->
782 mapTys _f TypesZ = TypesZ
783 mapTys f (TypesS t ts) = f t `TypesS` mapTys f ts
786 (forall k (t::k). Type src vs t -> acc -> acc) ->
787 Types src vs ts -> acc -> acc
788 foldlTys _f TypesZ acc = acc
789 foldlTys f (TypesS t ts) acc = foldlTys f ts (f t acc)
793 typeOf :: a t -> Type (SourceOf (a t)) (VarsOf (a t)) t