2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
8 TyCon, KindCon, SuperKindCon, ArgVrcs,
11 DataConDetails(..), visibleDataCons,
13 isFunTyCon, isUnLiftedTyCon, isBoxedTyCon, isProductTyCon,
14 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
16 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
17 isRecursiveTyCon, newTyConRep,
19 mkForeignTyCon, isForeignTyCon,
38 tyConDataConDetails, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
43 isClassTyCon, tyConClass_maybe,
51 tyConGenIds, tyConGenInfo
54 #include "HsVersions.h"
56 import {-# SOURCE #-} TypeRep ( Type, PredType, Kind, SuperKind )
57 -- Should just be Type(Type), but this fails due to bug present up to
58 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
60 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
63 import Var ( TyVar, Id )
64 import Class ( Class )
65 import BasicTypes ( Arity, RecFlag(..), Boxity(..),
67 import Name ( Name, nameUnique, NamedThing(getName) )
68 import PrelNames ( Unique, Uniquable(..), anyBoxConKey )
69 import PrimRep ( PrimRep(..), isFollowableRep )
70 import Maybes ( expectJust )
75 %************************************************************************
77 \subsection{The data type}
79 %************************************************************************
83 type SuperKindCon = TyCon
87 tyConUnique :: Unique,
94 | AlgTyCon { -- Tuples, data type, and newtype decls.
95 -- All lifted, all boxed
96 tyConUnique :: Unique,
101 tyConTyVars :: [TyVar],
102 tyConArgVrcs :: ArgVrcs,
103 algTyConTheta :: [PredType],
105 dataCons :: DataConDetails DataCon,
107 selIds :: [Id], -- Its record selectors (if any)
109 algTyConFlavour :: AlgTyConFlavour,
110 algTyConRec :: RecFlag, -- Tells whether the data type is part of
111 -- a mutually-recursive group or not
113 genInfo :: Maybe (EP Id), -- Convert T <-> Tring
114 -- Some TyCons don't have it;
115 -- e.g. the TyCon for a Class dictionary,
116 -- and TyCons with unboxed arguments
118 algTyConClass :: Maybe Class
119 -- Just cl if this tycon came from a class declaration
122 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
123 -- Now includes foreign-imported types
124 tyConUnique :: Unique,
128 tyConArgVrcs :: ArgVrcs,
129 primTyConRep :: PrimRep, -- Many primitive tycons are unboxed, but some are
130 -- boxed (represented by pointers). The PrimRep tells.
132 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
133 -- but foreign-imported ones may not be
134 tyConExtName :: Maybe FastString
139 tyConUnique :: Unique,
143 tyConBoxed :: Boxity,
144 tyConTyVars :: [TyVar],
146 genInfo :: Maybe (EP Id) -- Generic type and conv funs
150 tyConUnique :: Unique,
155 tyConTyVars :: [TyVar], -- Bound tyvars
156 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
157 -- Acts as a template for the expansion when
158 -- the tycon is applied to some types.
159 tyConArgVrcs :: ArgVrcs
162 | KindCon { -- Type constructor at the kind level
163 tyConUnique :: Unique,
165 tyConKind :: SuperKind,
169 | SuperKindCon { -- The type of kind variables or boxity variables,
170 tyConUnique :: Unique,
174 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
177 = DataTyCon -- Data type
179 | EnumTyCon -- Special sort of enumeration type
181 | NewTyCon Type -- Newtype, with its *ultimate* representation type
182 -- By 'ultimate' I mean that the rep type is not itself
183 -- a newtype or type synonym.
184 -- The rep type isn't entirely simple:
185 -- for a recursive newtype we pick () as the rep type
188 -- The rep type has free type variables the tyConTyVars
190 -- newtype T a = MkT [(a,Int)]
191 -- The rep type is [(a,Int)]
192 -- NB: the rep type isn't necessarily the original RHS of the
193 -- newtype decl, because the rep type looks through other
194 -- newtypes. If you want hte original RHS, look at the
195 -- argument type of the data constructor.
197 data DataConDetails datacon
198 = DataCons [datacon] -- Its data constructors, with fully polymorphic types
199 -- A type can have zero constructors
201 | Unknown -- We're importing this data type from an hi-boot file
202 -- and we don't know what its constructors are
204 | HasCons Int -- In a quest for compilation speed we have imported
205 -- only the number of constructors (to get return
206 -- conventions right) but not the constructors themselves
208 visibleDataCons (DataCons cs) = cs
209 visibleDataCons other = []
213 %************************************************************************
215 \subsection{TyCon Construction}
217 %************************************************************************
219 Note: the TyCon constructors all take a Kind as one argument, even though
220 they could, in principle, work out their Kind from their other arguments.
221 But to do so they need functions from Types, and that makes a nasty
222 module mutual-recursion. And they aren't called from many places.
223 So we compromise, and move their Kind calculation to the call site.
226 mkSuperKindCon :: Name -> SuperKindCon
227 mkSuperKindCon name = SuperKindCon {
228 tyConUnique = nameUnique name,
232 mkKindCon :: Name -> SuperKind -> KindCon
235 tyConUnique = nameUnique name,
241 mkFunTyCon :: Name -> Kind -> TyCon
244 tyConUnique = nameUnique name,
250 tyConGenInfo :: TyCon -> Maybe (EP Id)
251 tyConGenInfo (AlgTyCon { genInfo = info }) = info
252 tyConGenInfo (TupleTyCon { genInfo = info }) = info
253 tyConGenInfo other = Nothing
255 tyConGenIds :: TyCon -> [Id]
256 -- Returns the generic-programming Ids; these Ids need bindings
257 tyConGenIds tycon = case tyConGenInfo tycon of
259 Just (EP from to) -> [from,to]
261 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
262 -- but now you also have to pass in the generic information about the type
263 -- constructor - you can get hold of it easily (see Generics module)
264 mkAlgTyCon name kind tyvars theta argvrcs cons sels flavour rec
268 tyConUnique = nameUnique name,
270 tyConArity = length tyvars,
271 tyConTyVars = tyvars,
272 tyConArgVrcs = argvrcs,
273 algTyConTheta = theta,
276 algTyConClass = Nothing,
277 algTyConFlavour = flavour,
282 mkClassTyCon name kind tyvars argvrcs con clas flavour rec
285 tyConUnique = nameUnique name,
287 tyConArity = length tyvars,
288 tyConTyVars = tyvars,
289 tyConArgVrcs = argvrcs,
291 dataCons = DataCons [con],
293 algTyConClass = Just clas,
294 algTyConFlavour = flavour,
300 mkTupleTyCon name kind arity tyvars con boxed gen_info
302 tyConUnique = nameUnique name,
307 tyConTyVars = tyvars,
312 -- Foreign-imported (.NET) type constructors are represented
313 -- as primitive, but *lifted*, TyCons for now. They are lifted
314 -- because the Haskell type T representing the (foreign) .NET
315 -- type T is actually implemented (in ILX) as a thunk<T>
317 mkForeignTyCon name ext_name kind arity arg_vrcs
320 tyConUnique = nameUnique name,
323 tyConArgVrcs = arg_vrcs,
324 primTyConRep = PtrRep,
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,
344 tyConArgVrcs = arg_vrcs,
346 isUnLifted = is_unlifted,
347 tyConExtName = Nothing
350 mkSynTyCon name kind arity tyvars rhs argvrcs
353 tyConUnique = nameUnique name,
356 tyConTyVars = tyvars,
358 tyConArgVrcs = argvrcs
361 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
366 isFunTyCon (FunTyCon {}) = True
369 isPrimTyCon (PrimTyCon {}) = True
370 isPrimTyCon _ = False
372 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
373 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
374 isUnLiftedTyCon _ = False
376 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
377 isBoxedTyCon (AlgTyCon {}) = True
378 isBoxedTyCon (FunTyCon {}) = True
379 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
380 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
382 -- isAlgTyCon returns True for both @data@ and @newtype@
383 isAlgTyCon (AlgTyCon {}) = True
384 isAlgTyCon (TupleTyCon {}) = True
385 isAlgTyCon other = False
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 {algTyConFlavour = new_or_data, algTyConRec = is_rec})
395 = case new_or_data of
399 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
400 isDataTyCon other = False
402 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
403 isNewTyCon other = False
405 newTyConRep :: TyCon -> ([TyVar], Type)
406 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTyConFlavour = NewTyCon rep}) = (tvs, rep)
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 (AlgTyCon {dataCons = DataCons [data_con]}) = not (isExistentialDataCon data_con)
416 isProductTyCon (TupleTyCon {}) = True
417 isProductTyCon other = False
419 isSynTyCon (SynTyCon {}) = True
422 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
423 isEnumerationTyCon other = False
425 -- The unit tycon didn't used to be classed as a tuple tycon
426 -- but I thought that was silly so I've undone it
427 -- If it can't be for some reason, it should be a AlgTyCon
428 isTupleTyCon (TupleTyCon {}) = True
429 isTupleTyCon other = False
431 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
432 isUnboxedTupleTyCon other = False
434 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
435 isBoxedTupleTyCon other = False
437 tupleTyConBoxity tc = tyConBoxed tc
439 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
440 isRecursiveTyCon other = False
442 -- isForeignTyCon identifies foreign-imported type constructors
443 -- For the moment, they are primitive but lifted, but that may change
444 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
445 isForeignTyCon other = False
449 tyConDataConDetails :: TyCon -> DataConDetails DataCon
450 tyConDataConDetails (AlgTyCon {dataCons = cons}) = cons
451 tyConDataConDetails (TupleTyCon {dataCon = con}) = DataCons [con]
452 tyConDataConDetails other = Unknown
454 tyConDataCons :: TyCon -> [DataCon]
455 tyConDataCons tycon = expectJust "tyConDataCons" (tyConDataCons_maybe tycon)
457 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
458 tyConDataCons_maybe (AlgTyCon {dataCons = DataCons cons}) = Just cons
459 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
460 tyConDataCons_maybe other = Nothing
462 tyConFamilySize :: TyCon -> Int
463 tyConFamilySize (AlgTyCon {dataCons = DataCons cs}) = length cs
464 tyConFamilySize (AlgTyCon {dataCons = HasCons n}) = n
465 tyConFamilySize (TupleTyCon {}) = 1
467 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
470 tyConSelIds :: TyCon -> [Id]
471 tyConSelIds (AlgTyCon {selIds = sels}) = sels
472 tyConSelIds other_tycon = []
476 tyConPrimRep :: TyCon -> PrimRep
477 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
478 tyConPrimRep tc = ASSERT( not (isUnboxedTupleTyCon tc) )
480 -- We should not be asking what the representation of an
481 -- unboxed tuple is, because it isn't a first class value.
485 tyConTheta :: TyCon -> [PredType]
486 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
487 tyConTheta (TupleTyCon {}) = []
488 -- shouldn't ask about anything else
491 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
492 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
493 actually computed (in another file).
496 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
498 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
499 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
500 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
501 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
502 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
503 tyConArgVrcs_maybe _ = Nothing
507 getSynTyConDefn :: TyCon -> ([TyVar], Type)
508 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
512 maybeTyConSingleCon :: TyCon -> Maybe DataCon
513 maybeTyConSingleCon (AlgTyCon {dataCons = DataCons [c]}) = Just c
514 maybeTyConSingleCon (AlgTyCon {}) = Nothing
515 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
516 maybeTyConSingleCon (PrimTyCon {}) = Nothing
517 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
518 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
522 isClassTyCon :: TyCon -> Bool
523 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
524 isClassTyCon other_tycon = False
526 tyConClass_maybe :: TyCon -> Maybe Class
527 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
528 tyConClass_maybe ther_tycon = Nothing
532 %************************************************************************
534 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
536 %************************************************************************
538 @TyCon@s are compared by comparing their @Unique@s.
540 The strictness analyser needs @Ord@. It is a lexicographic order with
541 the property @(a<=b) || (b<=a)@.
544 instance Eq TyCon where
545 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
546 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
548 instance Ord TyCon where
549 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
550 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
551 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
552 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
553 compare a b = getUnique a `compare` getUnique b
555 instance Uniquable TyCon where
556 getUnique tc = tyConUnique tc
558 instance Outputable TyCon where
559 ppr tc = ppr (getName tc)
561 instance NamedThing TyCon where
566 %************************************************************************
568 \subsection{Kind constructors}
570 %************************************************************************
572 @matchesTyCon tc1 tc2@ checks whether an appliation
573 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
574 except that at the kind level tc2 might have more boxity info than tc1.
577 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
578 -> TyCon -- Inferred (e.g. type of actual arg to function)
581 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
583 uniq1 = tyConUnique tc1
584 uniq2 = tyConUnique tc2