2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[TypeRep]{Type - friends' interface}
8 Type(..), TyNote(..), UsageAnn(..), -- Representation visible to friends
11 superKind, superBoxity, -- :: SuperKind
13 boxedKind, -- :: Kind :: BX
14 anyBoxKind, -- :: Kind :: BX
15 typeCon, -- :: KindCon :: BX -> KX
16 anyBoxCon, -- :: KindCon :: BX
18 boxedTypeKind, unboxedTypeKind, openTypeKind, -- Kind :: superKind
20 mkArrowKind, mkArrowKinds,
25 #include "HsVersions.h"
28 import Var ( TyVar, UVar )
32 import Name ( Name, Provenance(..), ExportFlag(..),
33 mkWiredInTyConName, mkGlobalName, mkKindOccFS, tcName,
35 import TyCon ( TyCon, KindCon,
36 mkFunTyCon, mkKindCon, mkSuperKindCon,
40 import SrcLoc ( mkBuiltinSrcLoc )
41 import PrelMods ( pREL_GHC )
42 import Unique -- quite a few *Keys
43 import Util ( thenCmp )
46 %************************************************************************
48 \subsection{Type Classifications}
50 %************************************************************************
54 *unboxed* iff its representation is other than a pointer
55 Unboxed types cannot instantiate a type variable.
56 Unboxed types are always unlifted.
58 *lifted* A type is lifted iff it has bottom as an element.
59 Closures always have lifted types: i.e. any
60 let-bound identifier in Core must have a lifted
61 type. Operationally, a lifted object is one that
63 (NOTE: previously "pointed").
65 *algebraic* A type with one or more constructors, whether declared
66 with "data" or "newtype".
67 An algebraic type is one that can be deconstructed
68 with a case expression.
69 *NOT* the same as lifted types, because we also
70 include unboxed tuples in this classification.
72 *data* A type declared with "data". Also boxed tuples.
74 *primitive* iff it is a built-in type that can't be expressed
77 Currently, all primitive types are unlifted, but that's not necessarily
78 the case. (E.g. Int could be primitive.)
80 Some primitive types are unboxed, such as Int#, whereas some are boxed
81 but unlifted (such as ByteArray#). The only primitive types that we
82 classify as algebraic are the unboxed tuples.
84 examples of type classifications:
86 Type primitive boxed lifted algebraic
87 -----------------------------------------------------------------------------
89 ByteArray# Yes Yes No No
90 (# a, b #) Yes No No Yes
91 ( a, b ) No Yes Yes Yes
94 %************************************************************************
96 \subsection{The data type}
98 %************************************************************************
102 type SuperKind = Type
105 type TyVarSubst = TyVarEnv Type
111 Type -- Function is *not* a TyConApp
114 | TyConApp -- Application of a TyCon
115 TyCon -- *Invariant* saturated appliations of FunTyCon and
116 -- synonyms have their own constructors, below.
117 [Type] -- Might not be saturated.
119 | FunTy -- Special case of TyConApp: TyConApp FunTyCon [t1,t2]
123 | NoteTy -- Saturated application of a type synonym
125 Type -- The expanded version
132 = SynNote Type -- The unexpanded version of the type synonym; always a TyConApp
133 | FTVNote TyVarSet -- The free type variables of the noted expression
134 | UsgNote UsageAnn -- The usage annotation at this node
135 | UsgForAll UVar -- Annotation variable binder
136 | IPNote Name -- It's an implicit parameter
139 = UsOnce -- Used at most once
140 | UsMany -- Used possibly many times (no info; this annotation can be omitted)
141 | UsVar UVar -- Annotation is variable (unbound OK only inside analysis)
145 %************************************************************************
149 %************************************************************************
157 kv :: KX is a kind variable
163 | AnyBox -- Used *only* for special built-in things
164 -- like error :: forall (a::*?). String -> a
165 -- Here, the 'a' can be instantiated to a boxed or
169 bxv :: BX is a boxity variable
173 | sk -> sk -- In ptic (BX -> KX)
176 mk_kind_name key str = mkGlobalName key pREL_GHC (mkKindOccFS tcName str)
177 (LocalDef mkBuiltinSrcLoc NotExported)
178 -- mk_kind_name is a bit of a hack
179 -- The LocalDef means that we print the name without
180 -- a qualifier, which is what we want for these kinds.
181 -- It's used for both Kinds and Boxities
187 superKind :: SuperKind -- KX, the type of all kinds
188 superKindName = mk_kind_name kindConKey SLIT("KX")
189 superKind = TyConApp (mkSuperKindCon superKindName) []
191 superBoxity :: SuperKind -- BX, the type of all boxities
192 superBoxityName = mk_kind_name boxityConKey SLIT("BX")
193 superBoxity = TyConApp (mkSuperKindCon superBoxityName) []
196 Define Boxed, Unboxed, AnyBox
199 boxedKind, unboxedKind, anyBoxKind :: Kind -- Of superkind superBoxity
201 boxedConName = mk_kind_name boxedConKey SLIT("*")
202 boxedKind = TyConApp (mkKindCon boxedConName superBoxity) []
204 unboxedConName = mk_kind_name unboxedConKey SLIT("#")
205 unboxedKind = TyConApp (mkKindCon unboxedConName superBoxity) []
207 anyBoxConName = mk_kind_name anyBoxConKey SLIT("?")
208 anyBoxCon = mkKindCon anyBoxConName superBoxity -- A kind of wild card
209 anyBoxKind = TyConApp anyBoxCon []
216 typeConName = mk_kind_name typeConKey SLIT("Type")
217 typeCon = mkKindCon typeConName (superBoxity `FunTy` superKind)
220 Define (Type Boxed), (Type Unboxed), (Type AnyBox)
223 boxedTypeKind, unboxedTypeKind, openTypeKind :: Kind
224 boxedTypeKind = TyConApp typeCon [boxedKind]
225 unboxedTypeKind = TyConApp typeCon [unboxedKind]
226 openTypeKind = TyConApp typeCon [anyBoxKind]
228 mkArrowKind :: Kind -> Kind -> Kind
229 mkArrowKind k1 k2 = k1 `FunTy` k2
231 mkArrowKinds :: [Kind] -> Kind -> Kind
232 mkArrowKinds arg_kinds result_kind = foldr mkArrowKind result_kind arg_kinds
236 %************************************************************************
238 \subsection{Wired-in type constructors
240 %************************************************************************
242 We define a few wired-in type constructors here to avoid module knots
245 funTyConName = mkWiredInTyConName funTyConKey pREL_GHC SLIT("(->)") funTyCon
246 funTyCon = mkFunTyCon funTyConName (mkArrowKinds [boxedTypeKind, boxedTypeKind] boxedTypeKind)
250 %************************************************************************
252 \subsection{Equality on types}
254 %************************************************************************
256 For the moment at least, type comparisons don't work if
257 there are embedded for-alls.
260 instance Eq Type where
261 ty1 == ty2 = case ty1 `cmpTy` ty2 of { EQ -> True; other -> False }
263 instance Ord Type where
264 compare ty1 ty2 = cmpTy ty1 ty2
266 cmpTy :: Type -> Type -> Ordering
268 = cmp emptyVarEnv ty1 ty2
270 -- The "env" maps type variables in ty1 to type variables in ty2
271 -- So when comparing for-alls.. (forall tv1 . t1) (forall tv2 . t2)
272 -- we in effect substitute tv2 for tv1 in t1 before continuing
273 lookup env tv1 = case lookupVarEnv env tv1 of
278 cmp env (NoteTy _ ty1) ty2 = cmp env ty1 ty2
279 cmp env ty1 (NoteTy _ ty2) = cmp env ty1 ty2
281 -- Deal with equal constructors
282 cmp env (TyVarTy tv1) (TyVarTy tv2) = lookup env tv1 `compare` tv2
283 cmp env (AppTy f1 a1) (AppTy f2 a2) = cmp env f1 f2 `thenCmp` cmp env a1 a2
284 cmp env (FunTy f1 a1) (FunTy f2 a2) = cmp env f1 f2 `thenCmp` cmp env a1 a2
285 cmp env (TyConApp tc1 tys1) (TyConApp tc2 tys2) = (tc1 `compare` tc2) `thenCmp` (cmps env tys1 tys2)
286 cmp env (ForAllTy tv1 t1) (ForAllTy tv2 t2) = cmp (extendVarEnv env tv1 tv2) t1 t2
288 -- Deal with the rest: TyVarTy < AppTy < FunTy < TyConApp < ForAllTy
289 cmp env (AppTy _ _) (TyVarTy _) = GT
291 cmp env (FunTy _ _) (TyVarTy _) = GT
292 cmp env (FunTy _ _) (AppTy _ _) = GT
294 cmp env (TyConApp _ _) (TyVarTy _) = GT
295 cmp env (TyConApp _ _) (AppTy _ _) = GT
296 cmp env (TyConApp _ _) (FunTy _ _) = GT
298 cmp env (ForAllTy _ _) other = GT
303 cmps env (t:ts) [] = GT
304 cmps env [] (t:ts) = LT
305 cmps env (t1:t1s) (t2:t2s) = cmp env t1 t2 `thenCmp` cmps env t1s t2s