2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[TypeRep]{Type - friends' interface}
8 Type(..), TyNote(..), -- Representation visible
9 SourceType(..), IPName(..), -- to friends
11 Kind, PredType, ThetaType, -- Synonyms
14 superKind, superBoxity, -- KX and BX respectively
15 liftedBoxity, unliftedBoxity, -- :: BX
17 typeCon, -- :: BX -> KX
18 liftedTypeKind, unliftedTypeKind, openTypeKind, -- :: KX
19 mkArrowKind, mkArrowKinds, -- :: KX -> KX -> KX
21 usageKindCon, -- :: KX
22 usageTypeKind, -- :: KX
23 usOnceTyCon, usManyTyCon, -- :: $
24 usOnce, usMany, -- :: $
29 #include "HsVersions.h"
37 import TyCon ( TyCon, KindCon, mkFunTyCon, mkKindCon, mkSuperKindCon )
38 import Class ( Class )
41 import PrelNames ( superKindName, superBoxityName, liftedConName,
42 unliftedConName, typeConName, openKindConName,
43 usageKindConName, usOnceTyConName, usManyTyConName,
48 %************************************************************************
50 \subsection{Type Classifications}
52 %************************************************************************
56 *unboxed* iff its representation is other than a pointer
57 Unboxed types are also unlifted.
59 *lifted* A type is lifted iff it has bottom as an element.
60 Closures always have lifted types: i.e. any
61 let-bound identifier in Core must have a lifted
62 type. Operationally, a lifted object is one that
65 Only lifted types may be unified with a type variable.
67 *algebraic* A type with one or more constructors, whether declared
68 with "data" or "newtype".
69 An algebraic type is one that can be deconstructed
70 with a case expression.
71 *NOT* the same as lifted types, because we also
72 include unboxed tuples in this classification.
74 *data* A type declared with "data". Also boxed tuples.
76 *primitive* iff it is a built-in type that can't be expressed
79 Currently, all primitive types are unlifted, but that's not necessarily
80 the case. (E.g. Int could be primitive.)
82 Some primitive types are unboxed, such as Int#, whereas some are boxed
83 but unlifted (such as ByteArray#). The only primitive types that we
84 classify as algebraic are the unboxed tuples.
86 examples of type classifications:
88 Type primitive boxed lifted algebraic
89 -----------------------------------------------------------------------------
91 ByteArray# Yes Yes No No
92 (# a, b #) Yes No No Yes
93 ( a, b ) No Yes Yes Yes
98 ----------------------
100 ----------------------
105 Then we want N to be represented as an Int, and that's what we arrange.
106 The front end of the compiler [TcType.lhs] treats N as opaque,
107 the back end treats it as transparent [Type.lhs].
109 There's a bit of a problem with recursive newtypes
111 newtype Q = MkQ (Q->Q)
113 Here the 'implicit expansion' we get from treating P and Q as transparent
114 would give rise to infinite types, which in turn makes eqType diverge.
115 Similarly splitForAllTys and splitFunTys can get into a loop.
117 Solution: for recursive newtypes use a coerce, and treat the newtype
118 and its representation as distinct right through the compiler. That's
119 what you get if you use recursive newtypes. (They are rare, so who
120 cares if they are a tiny bit less efficient.)
122 So: non-recursive newtypes are represented using a SourceTy (see below)
123 recursive newtypes are represented using a TyConApp
125 The TyCon still says "I'm a newtype", but we do not represent the
126 newtype application as a SourceType; instead as a TyConApp.
129 %************************************************************************
131 \subsection{The data type}
133 %************************************************************************
137 type SuperKind = Type
140 type TyVarSubst = TyVarEnv Type
146 Type -- Function is *not* a TyConApp
149 | TyConApp -- Application of a TyCon
150 TyCon -- *Invariant* saturated appliations of FunTyCon and
151 -- synonyms have their own constructors, below.
152 [Type] -- Might not be saturated.
154 | FunTy -- Special case of TyConApp: TyConApp FunTyCon [t1,t2]
158 | ForAllTy -- A polymorphic type
162 | SourceTy -- A high level source type
163 SourceType -- ...can be expanded to a representation type...
165 | UsageTy -- A usage-annotated type
166 Type -- - Annotation of kind $ (i.e., usage annotation)
167 Type -- - Annotated type
169 | NoteTy -- A type with a note attached
171 Type -- The expanded version
174 = FTVNote TyVarSet -- The free type variables of the noted expression
176 | SynNote Type -- Used for type synonyms
177 -- The Type is always a TyConApp, and is the un-expanded form.
178 -- The type to which the note is attached is the expanded form.
181 INVARIANT: UsageTys are optional, but may *only* appear immediately
182 under a FunTy (either argument), or at top-level of a Type permitted
183 to be annotated (such as the type of an Id). NoteTys are transparent
184 for the purposes of this rule.
186 -------------------------------------
191 represents a value whose type is the Haskell source type sty.
192 It can be expanded into its representation, but:
194 * The type checker must treat it as opaque
195 * The rest of the compiler treats it as transparent
197 There are two main uses
198 a) Haskell predicates
201 Consider these examples:
202 f :: (Eq a) => a -> Int
203 g :: (?x :: Int -> Int) => a -> Int
204 h :: (r\l) => {r} => {l::Int | r}
206 Here the "Eq a" and "?x :: Int -> Int" and "r\l" are all called *predicates*
207 Predicates are represented inside GHC by PredType:
211 = ClassP Class [Type] -- Class predicate
212 | IParam (IPName Name) Type -- Implicit parameter
213 | NType TyCon [Type] -- A *saturated*, *non-recursive* newtype application
214 -- [See notes at top about newtypes]
217 = Dupable name -- ?x: you can freely duplicate this implicit parameter
218 | MustSplit name -- %x: you must use the splitting function to duplicate it
219 deriving( Eq, Ord ) -- Ord is used in the IP name cache finite map
220 -- (used in HscTypes.OrigIParamCache)
221 -- I sometimes thisnk this type should be in BasicTypes
223 type PredType = SourceType -- A subtype for predicates
224 type ThetaType = [PredType]
227 (We don't support TREX records yet, but the setup is designed
228 to expand to allow them.)
230 A Haskell qualified type, such as that for f,g,h above, is
232 * a FunTy for the double arrow
233 * with a PredTy as the function argument
235 The predicate really does turn into a real extra argument to the
236 function. If the argument has type (PredTy p) then the predicate p is
237 represented by evidence (a dictionary, for example, of type (predRepTy p).
240 %************************************************************************
244 %************************************************************************
248 kind :: KX = kind -> kind
250 | Type liftedness -- (Type *) is printed as just *
251 -- (Type #) is printed as just #
253 | UsageKind -- Printed '$'; used for usage annotations
255 | OpenKind -- Can be lifted or unlifted
258 | kv -- A kind variable; *only* happens during kind checking
260 boxity :: BX = * -- Lifted
262 | bv -- A boxity variable; *only* happens during kind checking
264 There's a little subtyping at the kind level:
265 forall b. Type b <: OpenKind
267 That is, a type of kind (Type b) is OK in a context requiring an OpenKind
269 OpenKind, written '?', is used as the kind for certain type variables,
272 1. The universally quantified type variable(s) for special built-in
273 things like error :: forall (a::?). String -> a.
274 Here, the 'a' can be instantiated to a lifted or unlifted type.
276 2. Kind '?' is also used when the typechecker needs to create a fresh
277 type variable, one that may very well later be unified with a type.
278 For example, suppose f::a, and we see an application (f x). Then a
279 must be a function type, so we unify a with (b->c). But what kind
280 are b and c? They can be lifted or unlifted types, or indeed type schemes,
281 so we give them kind '?'.
283 When the type checker generalises over a bunch of type variables, it
284 makes any that still have kind '?' into kind '*'. So kind '?' is never
285 present in an inferred type.
288 ------------------------------------------
289 Define KX, the type of a kind
290 BX, the type of a boxity
293 superKind :: SuperKind -- KX, the type of all kinds
294 superKind = TyConApp (mkSuperKindCon superKindName) []
296 superBoxity :: SuperKind -- BX, the type of all boxities
297 superBoxity = TyConApp (mkSuperKindCon superBoxityName) []
300 ------------------------------------------
301 Define boxities: @*@ and @#@
304 liftedBoxity, unliftedBoxity :: Kind -- :: BX
305 liftedBoxity = TyConApp (mkKindCon liftedConName superBoxity) []
307 unliftedBoxity = TyConApp (mkKindCon unliftedConName superBoxity) []
310 ------------------------------------------
311 Define kinds: Type, Type *, Type #, OpenKind, and UsageKind
314 typeCon :: KindCon -- :: BX -> KX
315 typeCon = mkKindCon typeConName (superBoxity `FunTy` superKind)
317 liftedTypeKind, unliftedTypeKind, openTypeKind :: Kind -- Of superkind superKind
319 liftedTypeKind = TyConApp typeCon [liftedBoxity]
320 unliftedTypeKind = TyConApp typeCon [unliftedBoxity]
322 openKindCon = mkKindCon openKindConName superKind
323 openTypeKind = TyConApp openKindCon []
325 usageKindCon = mkKindCon usageKindConName superKind
326 usageTypeKind = TyConApp usageKindCon []
329 ------------------------------------------
333 mkArrowKind :: Kind -> Kind -> Kind
334 mkArrowKind k1 k2 = k1 `FunTy` k2
336 mkArrowKinds :: [Kind] -> Kind -> Kind
337 mkArrowKinds arg_kinds result_kind = foldr mkArrowKind result_kind arg_kinds
341 %************************************************************************
343 \subsection{Wired-in type constructors
345 %************************************************************************
347 We define a few wired-in type constructors here to avoid module knots
350 funTyCon = mkFunTyCon funTyConName (mkArrowKinds [liftedTypeKind, liftedTypeKind] liftedTypeKind)
353 ------------------------------------------
354 Usage tycons @.@ and @!@
356 The usage tycons are of kind usageTypeKind (`$'). The types contain
357 no values, and are used purely for usage annotation.
360 usOnceTyCon = mkKindCon usOnceTyConName usageTypeKind
361 usOnce = TyConApp usOnceTyCon []
363 usManyTyCon = mkKindCon usManyTyConName usageTypeKind
364 usMany = TyConApp usManyTyCon []