2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
8 TyCon, KindCon, SuperKindCon, ArgVrcs, AlgTyConFlavour(..),
10 isFunTyCon, isUnLiftedTyCon, isBoxedTyCon, isProductTyCon,
11 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
13 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
14 isRecursiveTyCon, newTyConRep,
16 mkForeignTyCon, isForeignTyCon,
18 mkAlgTyCon, --mkAlgTyCon,
35 tyConDataCons, tyConDataConsIfAvailable, tyConFamilySize,
40 isClassTyCon, tyConClass_maybe,
48 tyConGenIds, tyConGenInfo
51 #include "HsVersions.h"
53 import {-# SOURCE #-} TypeRep ( Type, PredType, Kind, SuperKind )
54 -- Should just be Type(Type), but this fails due to bug present up to
55 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
57 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
60 import Var ( TyVar, Id )
61 import Class ( Class )
62 import BasicTypes ( Arity, RecFlag(..), Boxity(..),
64 import Name ( Name, nameUnique, NamedThing(getName) )
65 import PrelNames ( Unique, Uniquable(..), anyBoxConKey )
66 import PrimRep ( PrimRep(..), isFollowableRep )
67 import Util ( lengthIs )
72 %************************************************************************
74 \subsection{The data type}
76 %************************************************************************
80 type SuperKindCon = TyCon
84 tyConUnique :: Unique,
91 | AlgTyCon { -- Tuples, data type, and newtype decls.
92 -- All lifted, all boxed
93 tyConUnique :: Unique,
98 tyConTyVars :: [TyVar],
99 tyConArgVrcs :: ArgVrcs,
100 algTyConTheta :: [PredType],
102 dataCons :: [DataCon],
103 -- Its data constructors, with fully polymorphic types
104 -- This list can be empty, when we import a data type abstractly,
105 -- either (a) the interface is hand-written and doesn't give
106 -- the constructors, or
107 -- (b) in a quest for fast compilation we don't import
110 selIds :: [Id], -- Its record selectors (if any)
112 noOfDataCons :: Int, -- Number of data constructors
113 -- Usually this is the same as the length of the
114 -- dataCons field, but the latter may be empty if
115 -- we imported the type abstractly. But even if we import
116 -- abstractly we still need to know the number of constructors
117 -- so we can get the return convention right. Tiresome!
119 algTyConFlavour :: AlgTyConFlavour,
120 algTyConRec :: RecFlag, -- Tells whether the data type is part of
121 -- a mutually-recursive group or not
123 genInfo :: Maybe (EP Id), -- Convert T <-> Tring
124 -- Some TyCons don't have it;
125 -- e.g. the TyCon for a Class dictionary,
126 -- and TyCons with unboxed arguments
128 algTyConClass :: Maybe Class -- Just cl if this tycon came from a class declaration
131 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
132 -- Now includes foreign-imported types
133 tyConUnique :: Unique,
137 tyConArgVrcs :: ArgVrcs,
138 primTyConRep :: PrimRep, -- Many primitive tycons are unboxed, but some are
139 -- boxed (represented by pointers). The PrimRep tells.
141 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
142 -- but foreign-imported ones may not be
143 tyConExtName :: Maybe FastString
148 tyConUnique :: Unique,
152 tyConBoxed :: Boxity,
153 tyConTyVars :: [TyVar],
155 genInfo :: Maybe (EP Id) -- Generic type and conv funs
159 tyConUnique :: Unique,
164 tyConTyVars :: [TyVar], -- Bound tyvars
165 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
166 -- Acts as a template for the expansion when
167 -- the tycon is applied to some types.
168 tyConArgVrcs :: ArgVrcs
171 | KindCon { -- Type constructor at the kind level
172 tyConUnique :: Unique,
174 tyConKind :: SuperKind,
178 | SuperKindCon { -- The type of kind variables or boxity variables,
179 tyConUnique :: Unique,
183 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
186 = DataTyCon -- Data type
188 | EnumTyCon -- Special sort of enumeration type
190 | NewTyCon Type -- Newtype, with its *ultimate* representation type
191 -- By 'ultimate' I mean that the rep type is not itself
192 -- a newtype or type synonym.
194 -- The rep type has free type variables the tyConTyVars
196 -- newtype T a = MkT [(a,Int)]
197 -- The rep type is [(a,Int)]
199 -- The rep type isn't entirely simple:
200 -- for a recursive newtype we pick () as the rep type
204 %************************************************************************
206 \subsection{TyCon Construction}
208 %************************************************************************
210 Note: the TyCon constructors all take a Kind as one argument, even though
211 they could, in principle, work out their Kind from their other arguments.
212 But to do so they need functions from Types, and that makes a nasty
213 module mutual-recursion. And they aren't called from many places.
214 So we compromise, and move their Kind calculation to the call site.
217 mkSuperKindCon :: Name -> SuperKindCon
218 mkSuperKindCon name = SuperKindCon {
219 tyConUnique = nameUnique name,
223 mkKindCon :: Name -> SuperKind -> KindCon
226 tyConUnique = nameUnique name,
232 mkFunTyCon :: Name -> Kind -> TyCon
235 tyConUnique = nameUnique name,
241 tyConGenInfo :: TyCon -> Maybe (EP Id)
242 tyConGenInfo (AlgTyCon { genInfo = info }) = info
243 tyConGenInfo (TupleTyCon { genInfo = info }) = info
244 tyConGenInfo other = Nothing
246 tyConGenIds :: TyCon -> [Id]
247 -- Returns the generic-programming Ids; these Ids need bindings
248 tyConGenIds tycon = case tyConGenInfo tycon of
250 Just (EP from to) -> [from,to]
252 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
253 -- but now you also have to pass in the generic information about the type
254 -- constructor - you can get hold of it easily (see Generics module)
255 mkAlgTyCon name kind tyvars theta argvrcs cons ncons sels flavour rec
259 tyConUnique = nameUnique name,
261 tyConArity = length tyvars,
262 tyConTyVars = tyvars,
263 tyConArgVrcs = argvrcs,
264 algTyConTheta = theta,
267 noOfDataCons = ncons,
268 algTyConClass = Nothing,
269 algTyConFlavour = flavour,
274 mkClassTyCon name kind tyvars argvrcs con clas flavour rec
277 tyConUnique = nameUnique name,
279 tyConArity = length tyvars,
280 tyConTyVars = tyvars,
281 tyConArgVrcs = argvrcs,
286 algTyConClass = Just clas,
287 algTyConFlavour = flavour,
293 mkTupleTyCon name kind arity tyvars con boxed gen_info
295 tyConUnique = nameUnique name,
300 tyConTyVars = tyvars,
305 -- Foreign-imported (.NET) type constructors are represented
306 -- as primitive, but *lifted*, TyCons for now. They are lifted
307 -- because the Haskell type T representing the (foreign) .NET
308 -- type T is actually implemented (in ILX) as a thunk<T>
310 mkForeignTyCon name ext_name kind arity arg_vrcs
313 tyConUnique = nameUnique name,
316 tyConArgVrcs = arg_vrcs,
317 primTyConRep = PtrRep,
319 tyConExtName = ext_name
323 -- most Prim tycons are lifted
324 mkPrimTyCon name kind arity arg_vrcs rep
325 = mkPrimTyCon' name kind arity arg_vrcs rep True
327 -- but RealWorld is lifted
328 mkLiftedPrimTyCon name kind arity arg_vrcs rep
329 = mkPrimTyCon' name kind arity arg_vrcs rep False
331 mkPrimTyCon' name kind arity arg_vrcs rep is_unlifted
334 tyConUnique = nameUnique name,
337 tyConArgVrcs = arg_vrcs,
339 isUnLifted = is_unlifted,
340 tyConExtName = Nothing
343 mkSynTyCon name kind arity tyvars rhs argvrcs
346 tyConUnique = nameUnique name,
349 tyConTyVars = tyvars,
351 tyConArgVrcs = argvrcs
354 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
359 isFunTyCon (FunTyCon {}) = True
362 isPrimTyCon (PrimTyCon {}) = True
363 isPrimTyCon _ = False
365 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
366 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
367 isUnLiftedTyCon _ = False
369 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
370 isBoxedTyCon (AlgTyCon {}) = True
371 isBoxedTyCon (FunTyCon {}) = True
372 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
373 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
375 -- isAlgTyCon returns True for both @data@ and @newtype@
376 isAlgTyCon (AlgTyCon {}) = True
377 isAlgTyCon (TupleTyCon {}) = True
378 isAlgTyCon other = False
380 -- isDataTyCon returns True for data types that are represented by
381 -- heap-allocated constructors.
382 -- These are srcutinised by Core-level @case@ expressions, and they
383 -- get info tables allocated for them.
384 -- True for all @data@ types
385 -- False for newtypes
387 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data, algTyConRec = is_rec})
388 = case new_or_data of
392 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
393 isDataTyCon other = False
395 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
396 isNewTyCon other = False
398 newTyConRep :: TyCon -> ([TyVar], Type)
399 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTyConFlavour = NewTyCon rep}) = (tvs, rep)
402 -- has *one* constructor,
403 -- is *not* existential
405 -- may be DataType or NewType,
406 -- may be unboxed or not,
407 -- may be recursive or not
408 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
409 isProductTyCon (TupleTyCon {}) = True
410 isProductTyCon other = False
412 isSynTyCon (SynTyCon {}) = True
415 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
416 isEnumerationTyCon other = False
418 -- The unit tycon didn't used to be classed as a tuple tycon
419 -- but I thought that was silly so I've undone it
420 -- If it can't be for some reason, it should be a AlgTyCon
421 isTupleTyCon (TupleTyCon {}) = True
422 isTupleTyCon other = False
424 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
425 isUnboxedTupleTyCon other = False
427 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
428 isBoxedTupleTyCon other = False
430 tupleTyConBoxity tc = tyConBoxed tc
432 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
433 isRecursiveTyCon other = False
435 -- isForeignTyCon identifies foreign-imported type constructors
436 -- For the moment, they are primitive but lifted, but that may change
437 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
438 isForeignTyCon other = False
442 tyConDataCons :: TyCon -> [DataCon]
443 tyConDataCons tycon = ASSERT2( cons `lengthIs` (tyConFamilySize tycon), ppr tycon )
446 cons = tyConDataConsIfAvailable tycon
448 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
449 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
450 tyConDataConsIfAvailable other = []
451 -- You may think this last equation should fail,
452 -- but it's quite convenient to return no constructors for
453 -- a synonym; see for example the call in TcTyClsDecls.
455 tyConFamilySize :: TyCon -> Int
456 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
457 tyConFamilySize (TupleTyCon {}) = 1
459 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
462 tyConSelIds :: TyCon -> [Id]
463 tyConSelIds (AlgTyCon {selIds = sels}) = sels
464 tyConSelIds other_tycon = []
468 tyConPrimRep :: TyCon -> PrimRep
469 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
470 tyConPrimRep tc = ASSERT( not (isUnboxedTupleTyCon tc) )
472 -- We should not be asking what the representation of an
473 -- unboxed tuple is, because it isn't a first class value.
477 tyConTheta :: TyCon -> [PredType]
478 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
479 tyConTheta (TupleTyCon {}) = []
480 -- shouldn't ask about anything else
483 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
484 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
485 actually computed (in another file).
488 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
490 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
491 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
492 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
493 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
494 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
495 tyConArgVrcs_maybe _ = Nothing
499 getSynTyConDefn :: TyCon -> ([TyVar], Type)
500 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
504 maybeTyConSingleCon :: TyCon -> Maybe DataCon
505 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
506 maybeTyConSingleCon (AlgTyCon {}) = Nothing
507 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
508 maybeTyConSingleCon (PrimTyCon {}) = Nothing
509 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
510 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
515 isClassTyCon :: TyCon -> Bool
516 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
517 isClassTyCon other_tycon = False
519 tyConClass_maybe :: TyCon -> Maybe Class
520 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
521 tyConClass_maybe ther_tycon = Nothing
525 %************************************************************************
527 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
529 %************************************************************************
531 @TyCon@s are compared by comparing their @Unique@s.
533 The strictness analyser needs @Ord@. It is a lexicographic order with
534 the property @(a<=b) || (b<=a)@.
537 instance Eq TyCon where
538 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
539 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
541 instance Ord TyCon where
542 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
543 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
544 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
545 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
546 compare a b = getUnique a `compare` getUnique b
548 instance Uniquable TyCon where
549 getUnique tc = tyConUnique tc
551 instance Outputable TyCon where
552 ppr tc = ppr (getName tc)
554 instance NamedThing TyCon where
559 %************************************************************************
561 \subsection{Kind constructors}
563 %************************************************************************
565 @matchesTyCon tc1 tc2@ checks whether an appliation
566 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
567 except that at the kind level tc2 might have more boxity info than tc1.
570 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
571 -> TyCon -- Inferred (e.g. type of actual arg to function)
574 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
576 uniq1 = tyConUnique tc1
577 uniq2 = tyConUnique tc2