2 % (c) The University of Glasgow 2006
11 liftedTypeKind, unliftedTypeKind, openTypeKind,
12 argTypeKind, ubxTupleKind,
13 mkArrowKind, mkArrowKinds,
15 -- Kind constructors...
16 liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
17 argTypeKindTyCon, ubxTupleKindTyCon,
20 tySuperKind, tySuperKindTyCon,
22 pprKind, pprParendKind,
24 -- ** Deconstructing Kinds
25 kindFunResult, kindAppResult, synTyConResKind,
26 splitKindFunTys, splitKindFunTysN, splitKindFunTy_maybe,
28 -- ** Predicates on Kinds
29 isLiftedTypeKind, isUnliftedTypeKind, isOpenTypeKind,
30 isUbxTupleKind, isArgTypeKind, isKind, isTySuperKind,
31 isSuperKind, isCoercionKind,
34 isSubArgTypeKind, isSubOpenTypeKind, isSubKind, defaultKind,
39 #include "HsVersions.h"
49 %************************************************************************
53 %************************************************************************
56 isTySuperKind :: SuperKind -> Bool
57 isTySuperKind (TyConApp kc []) = kc `hasKey` tySuperKindTyConKey
58 isTySuperKind _ = False
61 -- Lastly we need a few functions on Kinds
63 isLiftedTypeKindCon :: TyCon -> Bool
64 isLiftedTypeKindCon tc = tc `hasKey` liftedTypeKindTyConKey
67 %************************************************************************
71 %************************************************************************
74 typeKind :: Type -> Kind
75 typeKind _ty@(TyConApp tc tys)
76 = ASSERT2( not (tc `hasKey` eqPredPrimTyConKey) || length tys == 2, ppr _ty )
77 -- Assertion checks for unsaturated application of (~)
78 -- See Note [The (~) TyCon] in TysPrim
79 kindAppResult (tyConKind tc) tys
81 typeKind (PredTy pred) = predKind pred
82 typeKind (AppTy fun _) = kindFunResult (typeKind fun)
83 typeKind (ForAllTy _ ty) = typeKind ty
84 typeKind (TyVarTy tyvar) = tyVarKind tyvar
85 typeKind (FunTy _arg res)
86 -- Hack alert. The kind of (Int -> Int#) is liftedTypeKind (*),
87 -- not unliftedTypKind (#)
88 -- The only things that can be after a function arrow are
89 -- (a) types (of kind openTypeKind or its sub-kinds)
90 -- (b) kinds (of super-kind TY) (e.g. * -> (* -> *))
92 | otherwise = ASSERT( isSubOpenTypeKind k) liftedTypeKind
97 predKind :: PredType -> Kind
98 predKind (EqPred {}) = unliftedTypeKind -- Coercions are unlifted
99 predKind (ClassP {}) = liftedTypeKind -- Class and implicitPredicates are
100 predKind (IParam {}) = liftedTypeKind -- always represented by lifted types
103 %************************************************************************
107 %************************************************************************
110 -- | Essentially 'funResultTy' on kinds
111 kindFunResult :: Kind -> Kind
112 kindFunResult (FunTy _ res) = res
113 kindFunResult k = pprPanic "kindFunResult" (ppr k)
115 kindAppResult :: Kind -> [arg] -> Kind
116 kindAppResult k [] = k
117 kindAppResult k (_:as) = kindAppResult (kindFunResult k) as
119 -- | Essentially 'splitFunTys' on kinds
120 splitKindFunTys :: Kind -> ([Kind],Kind)
121 splitKindFunTys (FunTy a r) = case splitKindFunTys r of
123 splitKindFunTys k = ([], k)
125 splitKindFunTy_maybe :: Kind -> Maybe (Kind,Kind)
126 splitKindFunTy_maybe (FunTy a r) = Just (a,r)
127 splitKindFunTy_maybe _ = Nothing
129 -- | Essentially 'splitFunTysN' on kinds
130 splitKindFunTysN :: Int -> Kind -> ([Kind],Kind)
131 splitKindFunTysN 0 k = ([], k)
132 splitKindFunTysN n (FunTy a r) = case splitKindFunTysN (n-1) r of
134 splitKindFunTysN n k = pprPanic "splitKindFunTysN" (ppr n <+> ppr k)
136 -- | Find the result 'Kind' of a type synonym,
137 -- after applying it to its 'arity' number of type variables
138 -- Actually this function works fine on data types too,
139 -- but they'd always return '*', so we never need to ask
140 synTyConResKind :: TyCon -> Kind
141 synTyConResKind tycon = kindAppResult (tyConKind tycon) (tyConTyVars tycon)
143 -- | See "Type#kind_subtyping" for details of the distinction between these 'Kind's
144 isUbxTupleKind, isOpenTypeKind, isArgTypeKind, isUnliftedTypeKind :: Kind -> Bool
145 isOpenTypeKindCon, isUbxTupleKindCon, isArgTypeKindCon,
146 isUnliftedTypeKindCon, isSubArgTypeKindCon :: TyCon -> Bool
148 isOpenTypeKindCon tc = tyConUnique tc == openTypeKindTyConKey
150 isOpenTypeKind (TyConApp tc _) = isOpenTypeKindCon tc
151 isOpenTypeKind _ = False
153 isUbxTupleKindCon tc = tyConUnique tc == ubxTupleKindTyConKey
155 isUbxTupleKind (TyConApp tc _) = isUbxTupleKindCon tc
156 isUbxTupleKind _ = False
158 isArgTypeKindCon tc = tyConUnique tc == argTypeKindTyConKey
160 isArgTypeKind (TyConApp tc _) = isArgTypeKindCon tc
161 isArgTypeKind _ = False
163 isUnliftedTypeKindCon tc = tyConUnique tc == unliftedTypeKindTyConKey
165 isUnliftedTypeKind (TyConApp tc _) = isUnliftedTypeKindCon tc
166 isUnliftedTypeKind _ = False
168 isSubOpenTypeKind :: Kind -> Bool
169 -- ^ True of any sub-kind of OpenTypeKind (i.e. anything except arrow)
170 isSubOpenTypeKind (FunTy k1 k2) = ASSERT2 ( isKind k1, text "isSubOpenTypeKind" <+> ppr k1 <+> text "::" <+> ppr (typeKind k1) )
171 ASSERT2 ( isKind k2, text "isSubOpenTypeKind" <+> ppr k2 <+> text "::" <+> ppr (typeKind k2) )
173 isSubOpenTypeKind (TyConApp kc []) = ASSERT( isKind (TyConApp kc []) ) True
174 isSubOpenTypeKind other = ASSERT( isKind other ) False
175 -- This is a conservative answer
176 -- It matters in the call to isSubKind in
177 -- checkExpectedKind.
179 isSubArgTypeKindCon kc
180 | isUnliftedTypeKindCon kc = True
181 | isLiftedTypeKindCon kc = True
182 | isArgTypeKindCon kc = True
185 isSubArgTypeKind :: Kind -> Bool
186 -- ^ True of any sub-kind of ArgTypeKind
187 isSubArgTypeKind (TyConApp kc []) = isSubArgTypeKindCon kc
188 isSubArgTypeKind _ = False
190 -- | Is this a super-kind (i.e. a type-of-kinds)?
191 isSuperKind :: Type -> Bool
192 isSuperKind (TyConApp (skc) []) = isSuperKindTyCon skc
193 isSuperKind _ = False
195 -- | Is this a kind (i.e. a type-of-types)?
196 isKind :: Kind -> Bool
197 isKind k = isSuperKind (typeKind k)
199 isSubKind :: Kind -> Kind -> Bool
200 -- ^ @k1 \`isSubKind\` k2@ checks that @k1@ <: @k2@
201 isSubKind (TyConApp kc1 []) (TyConApp kc2 []) = kc1 `isSubKindCon` kc2
202 isSubKind (FunTy a1 r1) (FunTy a2 r2) = (a2 `isSubKind` a1) && (r1 `isSubKind` r2)
203 isSubKind _ _ = False
205 isSubKindCon :: TyCon -> TyCon -> Bool
206 -- ^ @kc1 \`isSubKindCon\` kc2@ checks that @kc1@ <: @kc2@
208 | isLiftedTypeKindCon kc1 && isLiftedTypeKindCon kc2 = True
209 | isUnliftedTypeKindCon kc1 && isUnliftedTypeKindCon kc2 = True
210 | isUbxTupleKindCon kc1 && isUbxTupleKindCon kc2 = True
211 | isOpenTypeKindCon kc2 = True
212 -- we already know kc1 is not a fun, its a TyCon
213 | isArgTypeKindCon kc2 && isSubArgTypeKindCon kc1 = True
216 defaultKind :: Kind -> Kind
217 -- ^ Used when generalising: default kind ? and ?? to *. See "Type#kind_subtyping" for more
218 -- information on what that means
220 -- When we generalise, we make generic type variables whose kind is
221 -- simple (* or *->* etc). So generic type variables (other than
222 -- built-in constants like 'error') always have simple kinds. This is important;
225 -- We want f to get type
226 -- f :: forall (a::*). a -> Bool
228 -- f :: forall (a::??). a -> Bool
229 -- because that would allow a call like (f 3#) as well as (f True),
230 --and the calling conventions differ. This defaulting is done in TcMType.zonkTcTyVarBndr.
232 | isSubOpenTypeKind k = liftedTypeKind
233 | isSubArgTypeKind k = liftedTypeKind