2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
8 TyCon, ArgVrcs, FieldLabel,
13 AlgTyConRhs(..), visibleDataCons,
15 isFunTyCon, isUnLiftedTyCon, isProductTyCon, isAbstractTyCon,
16 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
18 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
19 isRecursiveTyCon, newTyConRep, newTyConRhs, newTyConRhs_maybe, isHiBootTyCon,
21 mkForeignTyCon, isForeignTyCon,
36 algTcRhs, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
37 tyConFields, tyConSelIds,
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, isVanillaDataCon )
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 )
66 import Util ( equalLength )
71 %************************************************************************
73 \subsection{The data type}
75 %************************************************************************
80 tyConUnique :: Unique,
87 | AlgTyCon { -- Data type, and newtype decls.
88 -- All lifted, all boxed
89 tyConUnique :: Unique,
94 tyConTyVars :: [TyVar], -- Scopes over (a) the [PredType] in DataTyCon
95 -- (b) the cached types in NewTyCon
96 -- (c) the types in algTcFields
97 -- But not over the data constructors
100 algTcFields :: [(FieldLabel, Type, Id)],
101 -- Its fields (empty if none):
103 -- * its type (scoped over tby tyConTyVars)
104 -- * record selector (name = field name)
106 algTcRhs :: AlgTyConRhs, -- Data constructors in here
108 algTcRec :: RecFlag, -- Tells whether the data type is part of
109 -- a mutually-recursive group or not
111 hasGenerics :: Bool, -- True <=> generic to/from functions are available
112 -- (in the exports of the data type's source module)
114 algTcClass :: Maybe Class
115 -- Just cl if this tycon came from a class declaration
118 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
119 -- Now includes foreign-imported types
120 tyConUnique :: Unique,
126 primTyConRep :: PrimRep,
127 -- Many primitive tycons are unboxed, but some are
128 -- boxed (represented by pointers). The CgRep tells.
130 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
131 -- but foreign-imported ones may not be
132 tyConExtName :: Maybe FastString -- Just xx for foreign-imported types
136 tyConUnique :: Unique,
140 tyConBoxed :: Boxity,
141 tyConTyVars :: [TyVar],
147 tyConUnique :: Unique,
152 tyConTyVars :: [TyVar], -- Bound tyvars
153 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
154 -- Acts as a template for the expansion when
155 -- the tycon is applied to some types.
159 type FieldLabel = Name
161 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
162 -- [] means "no information, assume the worst"
165 = AbstractTyCon -- We know nothing about this data type, except
166 -- that it's represented by a pointer
167 -- Used when we export a data type abstractly into
171 (Maybe [PredType]) -- Just theta => this tycon was declared in H98 syntax
172 -- with the specified "stupid theta"
173 -- e.g. data Ord a => T a = ...
174 -- Nothing => this tycon was declared by giving the
175 -- type signatures for each constructor
177 -- e.g. data T a where { ... }
178 [DataCon] -- The constructors; can be empty if the user declares
179 -- the type to have no constructors
180 Bool -- Cached: True <=> an enumeration type
182 | NewTyCon -- Newtypes always have exactly one constructor
183 DataCon -- The unique constructor; it has no existentials
184 Type -- Cached: the argument type of the constructor
185 -- = the representation type of the tycon
187 Type -- Cached: the *ultimate* representation type
188 -- By 'ultimate' I mean that the rep type is not itself
189 -- a newtype or type synonym.
190 -- The rep type isn't entirely simple:
191 -- for a recursive newtype we pick () as the rep type
194 -- The rep type has free type variables the tyConTyVars
196 -- newtype T a = MkT [(a,Int)]
197 -- The rep type is [(a,Int)]
198 -- NB: the rep type isn't necessarily the original RHS of the
199 -- newtype decl, because the rep type looks through other
202 visibleDataCons :: AlgTyConRhs -> [DataCon]
203 visibleDataCons AbstractTyCon = []
204 visibleDataCons (DataTyCon _ cs _) = cs
205 visibleDataCons (NewTyCon c _ _) = [c]
208 %************************************************************************
212 %************************************************************************
214 A PrimRep is an abstraction of a type. It contains information that
215 the code generator needs in order to pass arguments, return results,
216 and store values of this type.
218 A PrimRep is somewhat similar to a CgRep (see codeGen/SMRep) and a
219 MachRep (see cmm/MachOp), although each of these types has a distinct
220 and clearly defined purpose:
222 - A PrimRep is a CgRep + information about signedness + information
223 about primitive pointers (AddrRep). Signedness and primitive
224 pointers are required when passing a primitive type to a foreign
225 function, but aren't needed for call/return conventions of Haskell
228 - A MachRep is a basic machine type (non-void, doesn't contain
229 information on pointerhood or signedness, but contains some
230 reps that don't have corresponding Haskell types).
236 | IntRep -- signed, word-sized
237 | WordRep -- unsinged, word-sized
238 | Int64Rep -- signed, 64 bit (32-bit words only)
239 | Word64Rep -- unsigned, 64 bit (32-bit words only)
240 | AddrRep -- a pointer, but not to a Haskell value
245 %************************************************************************
247 \subsection{TyCon Construction}
249 %************************************************************************
251 Note: the TyCon constructors all take a Kind as one argument, even though
252 they could, in principle, work out their Kind from their other arguments.
253 But to do so they need functions from Types, and that makes a nasty
254 module mutual-recursion. And they aren't called from many places.
255 So we compromise, and move their Kind calculation to the call site.
258 mkFunTyCon :: Name -> Kind -> TyCon
261 tyConUnique = nameUnique name,
267 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
268 -- but now you also have to pass in the generic information about the type
269 -- constructor - you can get hold of it easily (see Generics module)
270 mkAlgTyCon name kind tyvars argvrcs rhs flds is_rec gen_info
273 tyConUnique = nameUnique name,
275 tyConArity = length tyvars,
276 tyConTyVars = tyvars,
280 algTcClass = Nothing,
282 hasGenerics = gen_info
285 mkClassTyCon name kind tyvars argvrcs rhs clas is_rec
288 tyConUnique = nameUnique name,
290 tyConArity = length tyvars,
291 tyConTyVars = tyvars,
295 algTcClass = Just clas,
301 mkTupleTyCon name kind arity tyvars con boxed gen_info
303 tyConUnique = nameUnique name,
308 tyConTyVars = tyvars,
310 hasGenerics = gen_info
313 -- Foreign-imported (.NET) type constructors are represented
314 -- as primitive, but *lifted*, TyCons for now. They are lifted
315 -- because the Haskell type T representing the (foreign) .NET
316 -- type T is actually implemented (in ILX) as a thunk<T>
317 mkForeignTyCon name ext_name kind arity arg_vrcs
320 tyConUnique = nameUnique name,
324 primTyConRep = PtrRep, -- they all do
326 tyConExtName = ext_name
330 -- most Prim tycons are lifted
331 mkPrimTyCon name kind arity arg_vrcs rep
332 = mkPrimTyCon' name kind arity arg_vrcs rep True
334 -- but RealWorld is lifted
335 mkLiftedPrimTyCon name kind arity arg_vrcs rep
336 = mkPrimTyCon' name kind arity arg_vrcs rep False
338 mkPrimTyCon' name kind arity arg_vrcs rep is_unlifted
341 tyConUnique = nameUnique name,
346 isUnLifted = is_unlifted,
347 tyConExtName = Nothing
350 mkSynTyCon name kind tyvars rhs argvrcs
353 tyConUnique = nameUnique name,
355 tyConArity = length tyvars,
356 tyConTyVars = tyvars,
363 isFunTyCon :: TyCon -> Bool
364 isFunTyCon (FunTyCon {}) = True
367 isAbstractTyCon :: TyCon -> Bool
368 isAbstractTyCon (AlgTyCon { algTcRhs = AbstractTyCon }) = True
369 isAbstractTyCon _ = False
371 isPrimTyCon :: TyCon -> Bool
372 isPrimTyCon (PrimTyCon {}) = True
373 isPrimTyCon _ = False
375 isUnLiftedTyCon :: TyCon -> Bool
376 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
377 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
378 isUnLiftedTyCon _ = False
380 -- isAlgTyCon returns True for both @data@ and @newtype@
381 isAlgTyCon :: TyCon -> Bool
382 isAlgTyCon (AlgTyCon {}) = True
383 isAlgTyCon (TupleTyCon {}) = True
384 isAlgTyCon other = False
386 isDataTyCon :: TyCon -> Bool
387 -- isDataTyCon returns True for data types that are represented by
388 -- heap-allocated constructors.
389 -- These are srcutinised by Core-level @case@ expressions, and they
390 -- get info tables allocated for them.
391 -- True for all @data@ types
392 -- False for newtypes
394 isDataTyCon (AlgTyCon {algTcRhs = rhs})
396 DataTyCon _ _ _ -> True
397 NewTyCon _ _ _ -> False
398 AbstractTyCon -> panic "isDataTyCon"
400 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
401 isDataTyCon other = False
403 isNewTyCon :: TyCon -> Bool
404 isNewTyCon (AlgTyCon {algTcRhs = NewTyCon _ _ _}) = True
405 isNewTyCon other = False
407 isProductTyCon :: TyCon -> Bool
409 -- has *one* constructor,
410 -- is *not* existential
412 -- may be DataType or NewType,
413 -- may be unboxed or not,
414 -- may be recursive or not
415 isProductTyCon tc@(AlgTyCon {}) = case algTcRhs tc of
416 DataTyCon _ [data_con] _ -> isVanillaDataCon data_con
417 NewTyCon _ _ _ -> True
419 isProductTyCon (TupleTyCon {}) = True
420 isProductTyCon other = False
422 isSynTyCon :: TyCon -> Bool
423 isSynTyCon (SynTyCon {}) = True
426 isEnumerationTyCon :: TyCon -> Bool
427 isEnumerationTyCon (AlgTyCon {algTcRhs = DataTyCon _ _ is_enum}) = is_enum
428 isEnumerationTyCon other = False
430 isTupleTyCon :: TyCon -> Bool
431 -- The unit tycon didn't used to be classed as a tuple tycon
432 -- but I thought that was silly so I've undone it
433 -- If it can't be for some reason, it should be a AlgTyCon
434 isTupleTyCon (TupleTyCon {}) = True
435 isTupleTyCon other = False
437 isUnboxedTupleTyCon :: TyCon -> Bool
438 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
439 isUnboxedTupleTyCon other = False
441 isBoxedTupleTyCon :: TyCon -> Bool
442 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
443 isBoxedTupleTyCon other = False
445 tupleTyConBoxity tc = tyConBoxed tc
447 isRecursiveTyCon :: TyCon -> Bool
448 isRecursiveTyCon (AlgTyCon {algTcRec = Recursive}) = True
449 isRecursiveTyCon other = False
451 isHiBootTyCon :: TyCon -> Bool
452 -- Used for knot-tying in hi-boot files
453 isHiBootTyCon (AlgTyCon {algTcRhs = AbstractTyCon}) = True
454 isHiBootTyCon other = False
456 isForeignTyCon :: TyCon -> Bool
457 -- isForeignTyCon identifies foreign-imported type constructors
458 isForeignTyCon (PrimTyCon {tyConExtName = Just _}) = True
459 isForeignTyCon other = False
463 tyConHasGenerics :: TyCon -> Bool
464 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
465 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
466 tyConHasGenerics other = False -- Synonyms
468 tyConDataCons :: TyCon -> [DataCon]
469 -- It's convenient for tyConDataCons to return the
470 -- empty list for type synonyms etc
471 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
473 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
474 tyConDataCons_maybe (AlgTyCon {algTcRhs = DataTyCon _ cons _}) = Just cons
475 tyConDataCons_maybe (AlgTyCon {algTcRhs = NewTyCon con _ _}) = Just [con]
476 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
477 tyConDataCons_maybe other = Nothing
479 tyConFamilySize :: TyCon -> Int
480 tyConFamilySize (AlgTyCon {algTcRhs = DataTyCon _ cons _}) = length cons
481 tyConFamilySize (AlgTyCon {algTcRhs = NewTyCon _ _ _}) = 1
482 tyConFamilySize (TupleTyCon {}) = 1
484 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
487 tyConFields :: TyCon -> [(FieldLabel,Type,Id)]
488 tyConFields (AlgTyCon {algTcFields = fs}) = fs
489 tyConFields other_tycon = []
491 tyConSelIds :: TyCon -> [Id]
492 tyConSelIds tc = [id | (_,_,id) <- tyConFields tc]
496 newTyConRhs :: TyCon -> ([TyVar], Type)
497 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon _ rhs _}) = (tvs, rhs)
499 newTyConRhs_maybe :: TyCon
500 -> [Type] -- Args to tycon
501 -> Maybe ([(TyVar,Type)], -- Substitution
502 Type) -- Body type (not yet substituted)
503 -- Non-recursive newtypes are transparent to Core;
504 -- Given an application to some types, return Just (tenv, ty)
505 -- if it's a saturated, non-recursive newtype.
506 newTyConRhs_maybe (AlgTyCon {tyConTyVars = tvs,
507 algTcRec = NonRecursive, -- Not recursive
508 algTcRhs = NewTyCon _ rhs _}) tys
509 | tvs `equalLength` tys -- Saturated
510 = Just (tvs `zip` tys, rhs)
512 newTyConRhs_maybe other_tycon tys = Nothing
515 newTyConRep :: TyCon -> ([TyVar], Type)
516 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon _ _ rep}) = (tvs, rep)
518 tyConPrimRep :: TyCon -> PrimRep
519 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
520 tyConPrimRep tc = ASSERT(not (isUnboxedTupleTyCon tc)) PtrRep
524 tyConStupidTheta :: TyCon -> [PredType]
525 tyConStupidTheta (AlgTyCon {algTcRhs = DataTyCon mb_th _ _}) = mb_th `orElse` []
526 tyConStupidTheta (AlgTyCon {algTcRhs = other}) = []
527 tyConStupidTheta (TupleTyCon {}) = []
528 -- shouldn't ask about anything else
531 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
532 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
533 actually computed (in another file).
536 tyConArgVrcs :: TyCon -> ArgVrcs
537 tyConArgVrcs (FunTyCon {}) = [(False,True),(True,False)]
538 tyConArgVrcs (AlgTyCon {argVrcs = oi}) = oi
539 tyConArgVrcs (PrimTyCon {argVrcs = oi}) = oi
540 tyConArgVrcs (TupleTyCon {tyConArity = arity}) = (replicate arity (True,False))
541 tyConArgVrcs (SynTyCon {argVrcs = oi}) = oi
545 getSynTyConDefn :: TyCon -> ([TyVar], Type)
546 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
550 maybeTyConSingleCon :: TyCon -> Maybe DataCon
551 maybeTyConSingleCon (AlgTyCon {algTcRhs = DataTyCon _ [c] _}) = Just c
552 maybeTyConSingleCon (AlgTyCon {algTcRhs = NewTyCon c _ _}) = Just c
553 maybeTyConSingleCon (AlgTyCon {}) = Nothing
554 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
555 maybeTyConSingleCon (PrimTyCon {}) = Nothing
556 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
557 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
561 isClassTyCon :: TyCon -> Bool
562 isClassTyCon (AlgTyCon {algTcClass = Just _}) = True
563 isClassTyCon other_tycon = False
565 tyConClass_maybe :: TyCon -> Maybe Class
566 tyConClass_maybe (AlgTyCon {algTcClass = maybe_clas}) = maybe_clas
567 tyConClass_maybe ther_tycon = Nothing
571 %************************************************************************
573 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
575 %************************************************************************
577 @TyCon@s are compared by comparing their @Unique@s.
579 The strictness analyser needs @Ord@. It is a lexicographic order with
580 the property @(a<=b) || (b<=a)@.
583 instance Eq TyCon where
584 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
585 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
587 instance Ord TyCon where
588 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
589 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
590 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
591 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
592 compare a b = getUnique a `compare` getUnique b
594 instance Uniquable TyCon where
595 getUnique tc = tyConUnique tc
597 instance Outputable TyCon where
598 ppr tc = ppr (getName tc)
600 instance NamedThing TyCon where