2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
13 AlgTyConRhs(..), visibleDataCons,
15 isFunTyCon, isUnLiftedTyCon, isProductTyCon, isAbstractTyCon,
16 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
18 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
19 isRecursiveTyCon, newTyConRep, newTyConRhs, isHiBootTyCon,
21 mkForeignTyCon, isForeignTyCon,
36 algTyConRhs, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
40 isClassTyCon, tyConClass_maybe,
42 tyConExtName, -- External name for foreign types
50 #include "HsVersions.h"
52 import {-# SOURCE #-} TypeRep ( Type, PredType )
53 -- Should just be Type(Type), but this fails due to bug present up to
54 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
56 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
59 import Var ( TyVar, Id )
60 import Class ( Class )
62 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
63 import Name ( Name, nameUnique, NamedThing(getName) )
64 import PrelNames ( Unique, Uniquable(..) )
65 import Maybes ( orElse )
70 %************************************************************************
72 \subsection{The data type}
74 %************************************************************************
79 tyConUnique :: Unique,
86 | AlgTyCon { -- Data type, and newtype decls.
87 -- All lifted, all boxed
88 tyConUnique :: Unique,
93 tyConTyVars :: [TyVar],
95 algTyConTheta :: [PredType],
97 selIds :: [Id], -- Its record selectors (if any)
99 algRhs :: AlgTyConRhs, -- Data constructors in here
101 algTyConRec :: RecFlag, -- Tells whether the data type is part of
102 -- a mutually-recursive group or not
104 hasGenerics :: Bool, -- True <=> generic to/from functions are available
105 -- (in the exports of the data type's source module)
107 algTyConClass :: Maybe Class
108 -- Just cl if this tycon came from a class declaration
111 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
112 -- Now includes foreign-imported types
113 tyConUnique :: Unique,
119 primTyConRep :: PrimRep,
120 -- Many primitive tycons are unboxed, but some are
121 -- boxed (represented by pointers). The CgRep tells.
123 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
124 -- but foreign-imported ones may not be
125 tyConExtName :: Maybe FastString -- Just xx for foreign-imported types
129 tyConUnique :: Unique,
133 tyConBoxed :: Boxity,
134 tyConTyVars :: [TyVar],
140 tyConUnique :: Unique,
145 tyConTyVars :: [TyVar], -- Bound tyvars
146 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
147 -- Acts as a template for the expansion when
148 -- the tycon is applied to some types.
152 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
153 -- [] means "no information, assume the worst"
156 = AbstractTyCon -- We know nothing about this data type, except
157 -- that it's represented by a pointer
158 -- Used when we export a data type abstractly into
162 [DataCon] -- The constructors; can be empty if the user declares
163 -- the type to have no constructors
164 Bool -- Cached: True <=> an enumeration type
166 | NewTyCon -- Newtypes always have exactly one constructor
167 DataCon -- The unique constructor; it has no existentials
168 Type -- Cached: the argument type of the constructor
169 -- = the representation type of the tycon
171 Type -- Cached: the *ultimate* representation type
172 -- By 'ultimate' I mean that the rep type is not itself
173 -- a newtype or type synonym.
174 -- The rep type isn't entirely simple:
175 -- for a recursive newtype we pick () as the rep type
178 -- The rep type has free type variables the tyConTyVars
180 -- newtype T a = MkT [(a,Int)]
181 -- The rep type is [(a,Int)]
182 -- NB: the rep type isn't necessarily the original RHS of the
183 -- newtype decl, because the rep type looks through other
186 visibleDataCons :: AlgTyConRhs -> [DataCon]
187 visibleDataCons AbstractTyCon = []
188 visibleDataCons (DataTyCon cs _) = cs
189 visibleDataCons (NewTyCon c _ _) = [c]
192 %************************************************************************
196 %************************************************************************
198 A PrimRep is an abstraction of a type. It contains information that
199 the code generator needs in order to pass arguments, return results,
200 and store values of this type.
202 A PrimRep is somewhat similar to a CgRep (see codeGen/SMRep) and a
203 MachRep (see cmm/MachOp), although each of these types has a distinct
204 and clearly defined purpose:
206 - A PrimRep is a CgRep + information about signedness + information
207 about primitive pointers (AddrRep). Signedness and primitive
208 pointers are required when passing a primitive type to a foreign
209 function, but aren't needed for call/return conventions of Haskell
212 - A MachRep is a basic machine type (non-void, doesn't contain
213 information on pointerhood or signedness, but contains some
214 reps that don't have corresponding Haskell types).
220 | IntRep -- signed, word-sized
221 | WordRep -- unsinged, word-sized
222 | Int64Rep -- signed, 64 bit (32-bit words only)
223 | Word64Rep -- unsigned, 64 bit (32-bit words only)
224 | AddrRep -- a pointer, but not to a Haskell value
229 %************************************************************************
231 \subsection{TyCon Construction}
233 %************************************************************************
235 Note: the TyCon constructors all take a Kind as one argument, even though
236 they could, in principle, work out their Kind from their other arguments.
237 But to do so they need functions from Types, and that makes a nasty
238 module mutual-recursion. And they aren't called from many places.
239 So we compromise, and move their Kind calculation to the call site.
242 mkFunTyCon :: Name -> Kind -> TyCon
245 tyConUnique = nameUnique name,
251 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
252 -- but now you also have to pass in the generic information about the type
253 -- constructor - you can get hold of it easily (see Generics module)
254 mkAlgTyCon name kind tyvars theta argvrcs rhs sels is_rec gen_info
257 tyConUnique = nameUnique name,
259 tyConArity = length tyvars,
260 tyConTyVars = tyvars,
262 algTyConTheta = theta,
265 algTyConClass = Nothing,
266 algTyConRec = is_rec,
267 hasGenerics = gen_info
270 mkClassTyCon name kind tyvars argvrcs rhs clas is_rec
273 tyConUnique = nameUnique name,
275 tyConArity = length tyvars,
276 tyConTyVars = tyvars,
281 algTyConClass = Just clas,
282 algTyConRec = is_rec,
287 mkTupleTyCon name kind arity tyvars con boxed gen_info
289 tyConUnique = nameUnique name,
294 tyConTyVars = tyvars,
296 hasGenerics = gen_info
299 -- Foreign-imported (.NET) type constructors are represented
300 -- as primitive, but *lifted*, TyCons for now. They are lifted
301 -- because the Haskell type T representing the (foreign) .NET
302 -- type T is actually implemented (in ILX) as a thunk<T>
303 mkForeignTyCon name ext_name kind arity arg_vrcs
306 tyConUnique = nameUnique name,
310 primTyConRep = PtrRep, -- they all do
312 tyConExtName = ext_name
316 -- most Prim tycons are lifted
317 mkPrimTyCon name kind arity arg_vrcs rep
318 = mkPrimTyCon' name kind arity arg_vrcs rep True
320 -- but RealWorld is lifted
321 mkLiftedPrimTyCon name kind arity arg_vrcs rep
322 = mkPrimTyCon' name kind arity arg_vrcs rep False
324 mkPrimTyCon' name kind arity arg_vrcs rep is_unlifted
327 tyConUnique = nameUnique name,
332 isUnLifted = is_unlifted,
333 tyConExtName = Nothing
336 mkSynTyCon name kind tyvars rhs argvrcs
339 tyConUnique = nameUnique name,
341 tyConArity = length tyvars,
342 tyConTyVars = tyvars,
349 isFunTyCon :: TyCon -> Bool
350 isFunTyCon (FunTyCon {}) = True
353 isAbstractTyCon :: TyCon -> Bool
354 isAbstractTyCon (AlgTyCon { algRhs = AbstractTyCon }) = True
355 isAbstractTyCon _ = False
357 isPrimTyCon :: TyCon -> Bool
358 isPrimTyCon (PrimTyCon {}) = True
359 isPrimTyCon _ = False
361 isUnLiftedTyCon :: TyCon -> Bool
362 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
363 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
364 isUnLiftedTyCon _ = False
366 -- isAlgTyCon returns True for both @data@ and @newtype@
367 isAlgTyCon :: TyCon -> Bool
368 isAlgTyCon (AlgTyCon {}) = True
369 isAlgTyCon (TupleTyCon {}) = True
370 isAlgTyCon other = False
372 algTyConRhs :: TyCon -> AlgTyConRhs
373 algTyConRhs (AlgTyCon {algRhs = rhs}) = rhs
374 algTyConRhs (TupleTyCon {dataCon = dc}) = DataTyCon [dc] False
376 isDataTyCon :: TyCon -> Bool
377 -- isDataTyCon returns True for data types that are represented by
378 -- heap-allocated constructors.
379 -- These are srcutinised by Core-level @case@ expressions, and they
380 -- get info tables allocated for them.
381 -- True for all @data@ types
382 -- False for newtypes
384 isDataTyCon (AlgTyCon {algRhs = rhs})
386 DataTyCon _ _ -> True
387 NewTyCon _ _ _ -> False
388 AbstractTyCon -> panic "isDataTyCon"
390 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
391 isDataTyCon other = False
393 isNewTyCon :: TyCon -> Bool
394 isNewTyCon (AlgTyCon {algRhs = NewTyCon _ _ _}) = True
395 isNewTyCon other = False
397 isProductTyCon :: TyCon -> Bool
399 -- has *one* constructor,
400 -- is *not* existential
402 -- may be DataType or NewType,
403 -- may be unboxed or not,
404 -- may be recursive or not
405 isProductTyCon tc@(AlgTyCon {}) = case algRhs tc of
406 DataTyCon [data_con] _ -> not (isExistentialDataCon data_con)
407 NewTyCon _ _ _ -> True
409 isProductTyCon (TupleTyCon {}) = True
410 isProductTyCon other = False
412 isSynTyCon :: TyCon -> Bool
413 isSynTyCon (SynTyCon {}) = True
416 isEnumerationTyCon :: TyCon -> Bool
417 isEnumerationTyCon (AlgTyCon {algRhs = DataTyCon _ is_enum}) = is_enum
418 isEnumerationTyCon other = False
420 isTupleTyCon :: TyCon -> Bool
421 -- The unit tycon didn't used to be classed as a tuple tycon
422 -- but I thought that was silly so I've undone it
423 -- If it can't be for some reason, it should be a AlgTyCon
424 isTupleTyCon (TupleTyCon {}) = True
425 isTupleTyCon other = False
427 isUnboxedTupleTyCon :: TyCon -> Bool
428 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
429 isUnboxedTupleTyCon other = False
431 isBoxedTupleTyCon :: TyCon -> Bool
432 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
433 isBoxedTupleTyCon other = False
435 tupleTyConBoxity tc = tyConBoxed tc
437 isRecursiveTyCon :: TyCon -> Bool
438 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
439 isRecursiveTyCon other = False
441 isHiBootTyCon :: TyCon -> Bool
442 -- Used for knot-tying in hi-boot files
443 isHiBootTyCon (AlgTyCon {algRhs = AbstractTyCon}) = True
444 isHiBootTyCon other = False
446 isForeignTyCon :: TyCon -> Bool
447 -- isForeignTyCon identifies foreign-imported type constructors
448 isForeignTyCon (PrimTyCon {tyConExtName = Just _}) = True
449 isForeignTyCon other = False
453 tyConHasGenerics :: TyCon -> Bool
454 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
455 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
456 tyConHasGenerics other = False -- Synonyms
458 tyConDataCons :: TyCon -> [DataCon]
459 -- It's convenient for tyConDataCons to return the
460 -- empty list for type synonyms etc
461 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
463 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
464 tyConDataCons_maybe (AlgTyCon {algRhs = DataTyCon cons _}) = Just cons
465 tyConDataCons_maybe (AlgTyCon {algRhs = NewTyCon con _ _}) = Just [con]
466 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
467 tyConDataCons_maybe other = Nothing
469 tyConFamilySize :: TyCon -> Int
470 tyConFamilySize (AlgTyCon {algRhs = DataTyCon cons _}) = length cons
471 tyConFamilySize (AlgTyCon {algRhs = NewTyCon _ _ _}) = 1
472 tyConFamilySize (TupleTyCon {}) = 1
474 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
477 tyConSelIds :: TyCon -> [Id]
478 tyConSelIds (AlgTyCon {selIds = sels}) = sels
479 tyConSelIds other_tycon = []
483 newTyConRep :: TyCon -> ([TyVar], Type)
484 newTyConRep (AlgTyCon {tyConTyVars = tvs, algRhs = NewTyCon _ _ rep}) = (tvs, rep)
486 newTyConRhs :: TyCon -> ([TyVar], Type)
487 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algRhs = NewTyCon _ rhs _}) = (tvs, rhs)
491 tyConPrimRep :: TyCon -> PrimRep
492 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
493 tyConPrimRep tc = ASSERT(not (isUnboxedTupleTyCon tc)) PtrRep
497 tyConTheta :: TyCon -> [PredType]
498 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
499 tyConTheta (TupleTyCon {}) = []
500 -- shouldn't ask about anything else
503 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
504 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
505 actually computed (in another file).
508 tyConArgVrcs :: TyCon -> ArgVrcs
509 tyConArgVrcs (FunTyCon {}) = [(False,True),(True,False)]
510 tyConArgVrcs (AlgTyCon {argVrcs = oi}) = oi
511 tyConArgVrcs (PrimTyCon {argVrcs = oi}) = oi
512 tyConArgVrcs (TupleTyCon {tyConArity = arity}) = (replicate arity (True,False))
513 tyConArgVrcs (SynTyCon {argVrcs = oi}) = oi
517 getSynTyConDefn :: TyCon -> ([TyVar], Type)
518 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
522 maybeTyConSingleCon :: TyCon -> Maybe DataCon
523 maybeTyConSingleCon (AlgTyCon {algRhs = DataTyCon [c] _}) = Just c
524 maybeTyConSingleCon (AlgTyCon {algRhs = NewTyCon c _ _}) = Just c
525 maybeTyConSingleCon (AlgTyCon {}) = Nothing
526 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
527 maybeTyConSingleCon (PrimTyCon {}) = Nothing
528 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
529 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
533 isClassTyCon :: TyCon -> Bool
534 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
535 isClassTyCon other_tycon = False
537 tyConClass_maybe :: TyCon -> Maybe Class
538 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
539 tyConClass_maybe ther_tycon = Nothing
543 %************************************************************************
545 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
547 %************************************************************************
549 @TyCon@s are compared by comparing their @Unique@s.
551 The strictness analyser needs @Ord@. It is a lexicographic order with
552 the property @(a<=b) || (b<=a)@.
555 instance Eq TyCon where
556 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
557 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
559 instance Ord TyCon where
560 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
561 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
562 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
563 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
564 compare a b = getUnique a `compare` getUnique b
566 instance Uniquable TyCon where
567 getUnique tc = tyConUnique tc
569 instance Outputable TyCon where
570 ppr tc = ppr (getName tc)
572 instance NamedThing TyCon where