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, isHiBootTyCon,
19 mkForeignTyCon, isForeignTyCon,
37 tyConArgVrcs_maybe, tyConArgVrcs,
38 tyConDataConDetails, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
43 isClassTyCon, tyConClass_maybe,
45 tyConExtName, -- External name for foreign types
55 #include "HsVersions.h"
57 import {-# SOURCE #-} TypeRep ( Type, PredType, Kind, SuperKind )
58 -- Should just be Type(Type), but this fails due to bug present up to
59 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
61 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
64 import Var ( TyVar, Id )
65 import Class ( Class )
66 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
67 import Name ( Name, nameUnique, NamedThing(getName) )
68 import PrelNames ( Unique, Uniquable(..), anyBoxConKey )
69 import PrimRep ( PrimRep(..), isFollowableRep )
70 import Maybes ( orElse, 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],
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 hasGenerics :: Bool, -- True <=> generic to/from functions are available
114 -- (in the exports of the data type's source module)
116 algTyConClass :: Maybe Class
117 -- Just cl if this tycon came from a class declaration
120 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
121 -- Now includes foreign-imported types
122 tyConUnique :: Unique,
127 primTyConRep :: PrimRep, -- Many primitive tycons are unboxed, but some are
128 -- boxed (represented by pointers). The PrimRep 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
137 tyConUnique :: Unique,
141 tyConBoxed :: Boxity,
142 tyConTyVars :: [TyVar],
148 tyConUnique :: Unique,
153 tyConTyVars :: [TyVar], -- Bound tyvars
154 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
155 -- Acts as a template for the expansion when
156 -- the tycon is applied to some types.
160 | KindCon { -- Type constructor at the kind level
161 tyConUnique :: Unique,
163 tyConKind :: SuperKind,
167 | SuperKindCon { -- The type of kind variables or boxity variables,
168 tyConUnique :: Unique,
172 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
173 -- [] means "no information, assume the worst"
176 = DataTyCon Bool -- Data type; True <=> an enumeration type
178 | NewTyCon Type -- Newtype, with its *ultimate* representation type
179 -- By 'ultimate' I mean that the rep type is not itself
180 -- a newtype or type synonym.
181 -- The rep type isn't entirely simple:
182 -- for a recursive newtype we pick () as the rep type
185 -- The rep type has free type variables the tyConTyVars
187 -- newtype T a = MkT [(a,Int)]
188 -- The rep type is [(a,Int)]
189 -- NB: the rep type isn't necessarily the original RHS of the
190 -- newtype decl, because the rep type looks through other
191 -- newtypes. If you want hte original RHS, look at the
192 -- argument type of the data constructor.
194 data DataConDetails datacon
195 = DataCons [datacon] -- Its data constructors, with fully polymorphic types
196 -- A type can have zero constructors
198 | Unknown -- We're importing this data type from an hi-boot file
199 -- and we don't know what its constructors are
201 visibleDataCons (DataCons cs) = cs
202 visibleDataCons other = []
206 %************************************************************************
208 \subsection{TyCon Construction}
210 %************************************************************************
212 Note: the TyCon constructors all take a Kind as one argument, even though
213 they could, in principle, work out their Kind from their other arguments.
214 But to do so they need functions from Types, and that makes a nasty
215 module mutual-recursion. And they aren't called from many places.
216 So we compromise, and move their Kind calculation to the call site.
219 mkSuperKindCon :: Name -> SuperKindCon
220 mkSuperKindCon name = SuperKindCon {
221 tyConUnique = nameUnique name,
225 mkKindCon :: Name -> SuperKind -> KindCon
228 tyConUnique = nameUnique name,
234 mkFunTyCon :: Name -> Kind -> TyCon
237 tyConUnique = nameUnique name,
243 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
244 -- but now you also have to pass in the generic information about the type
245 -- constructor - you can get hold of it easily (see Generics module)
246 mkAlgTyCon name kind tyvars theta argvrcs cons sels flavour is_rec gen_info
249 tyConUnique = nameUnique name,
251 tyConArity = length tyvars,
252 tyConTyVars = tyvars,
254 algTyConTheta = theta,
257 algTyConClass = Nothing,
258 algTyConFlavour = flavour,
259 algTyConRec = is_rec,
260 hasGenerics = gen_info
263 mkClassTyCon name kind tyvars argvrcs con clas flavour is_rec
266 tyConUnique = nameUnique name,
268 tyConArity = length tyvars,
269 tyConTyVars = tyvars,
272 dataCons = DataCons [con],
274 algTyConClass = Just clas,
275 algTyConFlavour = flavour,
276 algTyConRec = is_rec,
281 mkTupleTyCon name kind arity tyvars con boxed gen_info
283 tyConUnique = nameUnique name,
288 tyConTyVars = tyvars,
290 hasGenerics = gen_info
293 -- Foreign-imported (.NET) type constructors are represented
294 -- as primitive, but *lifted*, TyCons for now. They are lifted
295 -- because the Haskell type T representing the (foreign) .NET
296 -- type T is actually implemented (in ILX) as a thunk<T>
298 mkForeignTyCon name ext_name kind arity arg_vrcs
301 tyConUnique = nameUnique name,
305 primTyConRep = PtrRep,
307 tyConExtName = ext_name
311 -- most Prim tycons are lifted
312 mkPrimTyCon name kind arity arg_vrcs rep
313 = mkPrimTyCon' name kind arity arg_vrcs rep True
315 -- but RealWorld is lifted
316 mkLiftedPrimTyCon name kind arity arg_vrcs rep
317 = mkPrimTyCon' name kind arity arg_vrcs rep False
319 mkPrimTyCon' name kind arity arg_vrcs rep is_unlifted
322 tyConUnique = nameUnique name,
327 isUnLifted = is_unlifted,
328 tyConExtName = Nothing
331 mkSynTyCon name kind tyvars rhs argvrcs
334 tyConUnique = nameUnique name,
336 tyConArity = length tyvars,
337 tyConTyVars = tyvars,
342 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
347 isFunTyCon :: TyCon -> Bool
348 isFunTyCon (FunTyCon {}) = True
351 isPrimTyCon :: TyCon -> Bool
352 isPrimTyCon (PrimTyCon {}) = True
353 isPrimTyCon _ = False
355 isUnLiftedTyCon :: TyCon -> Bool
356 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
357 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
358 isUnLiftedTyCon _ = False
360 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
361 isBoxedTyCon :: TyCon -> Bool
362 isBoxedTyCon (AlgTyCon {}) = True
363 isBoxedTyCon (FunTyCon {}) = True
364 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
365 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
367 -- isAlgTyCon returns True for both @data@ and @newtype@
368 isAlgTyCon :: TyCon -> Bool
369 isAlgTyCon (AlgTyCon {}) = True
370 isAlgTyCon (TupleTyCon {}) = True
371 isAlgTyCon other = False
373 isDataTyCon :: TyCon -> Bool
374 -- isDataTyCon returns True for data types that are represented by
375 -- heap-allocated constructors.
376 -- These are srcutinised by Core-level @case@ expressions, and they
377 -- get info tables allocated for them.
378 -- True for all @data@ types
379 -- False for newtypes
381 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data})
382 = case new_or_data of
386 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
387 isDataTyCon other = False
389 isNewTyCon :: TyCon -> Bool
390 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
391 isNewTyCon other = False
393 isProductTyCon :: TyCon -> Bool
395 -- has *one* constructor,
396 -- is *not* existential
398 -- may be DataType or NewType,
399 -- may be unboxed or not,
400 -- may be recursive or not
401 isProductTyCon (AlgTyCon {dataCons = DataCons [data_con]}) = not (isExistentialDataCon data_con)
402 isProductTyCon (TupleTyCon {}) = True
403 isProductTyCon other = False
405 isSynTyCon :: TyCon -> Bool
406 isSynTyCon (SynTyCon {}) = True
409 isEnumerationTyCon :: TyCon -> Bool
410 isEnumerationTyCon (AlgTyCon {algTyConFlavour = DataTyCon is_enum}) = is_enum
411 isEnumerationTyCon other = False
413 isTupleTyCon :: TyCon -> Bool
414 -- The unit tycon didn't used to be classed as a tuple tycon
415 -- but I thought that was silly so I've undone it
416 -- If it can't be for some reason, it should be a AlgTyCon
417 isTupleTyCon (TupleTyCon {}) = True
418 isTupleTyCon other = False
420 isUnboxedTupleTyCon :: TyCon -> Bool
421 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
422 isUnboxedTupleTyCon other = False
424 isBoxedTupleTyCon :: TyCon -> Bool
425 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
426 isBoxedTupleTyCon other = False
428 tupleTyConBoxity tc = tyConBoxed tc
430 isRecursiveTyCon :: TyCon -> Bool
431 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
432 isRecursiveTyCon other = False
434 isHiBootTyCon :: TyCon -> Bool
435 -- Used for knot-tying in hi-boot files
436 isHiBootTyCon (AlgTyCon {dataCons = Unknown}) = True
437 isHiBootTyCon other = False
439 isForeignTyCon :: TyCon -> Bool
440 -- isForeignTyCon identifies foreign-imported type constructors
441 -- For the moment, they are primitive but lifted, but that may change
442 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
443 isForeignTyCon other = False
447 tyConHasGenerics :: TyCon -> Bool
448 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
449 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
450 tyConHasGenerics other = False -- Synonyms
452 tyConDataConDetails :: TyCon -> DataConDetails DataCon
453 tyConDataConDetails (AlgTyCon {dataCons = cons}) = cons
454 tyConDataConDetails (TupleTyCon {dataCon = con}) = DataCons [con]
455 tyConDataConDetails other = Unknown
457 tyConDataCons :: TyCon -> [DataCon]
458 -- It's convenient for tyConDataCons to return the
459 -- empty list for type synonyms etc
460 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
462 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
463 tyConDataCons_maybe (AlgTyCon {dataCons = DataCons cons}) = Just cons
464 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
465 tyConDataCons_maybe other = Nothing
467 tyConFamilySize :: TyCon -> Int
468 tyConFamilySize (AlgTyCon {dataCons = DataCons cs}) = length cs
469 tyConFamilySize (TupleTyCon {}) = 1
471 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
474 tyConSelIds :: TyCon -> [Id]
475 tyConSelIds (AlgTyCon {selIds = sels}) = sels
476 tyConSelIds other_tycon = []
480 newTyConRep :: TyCon -> ([TyVar], Type)
481 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTyConFlavour = NewTyCon rep}) = (tvs, rep)
483 tyConPrimRep :: TyCon -> PrimRep
484 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
485 tyConPrimRep tc = ASSERT( not (isUnboxedTupleTyCon tc) )
487 -- We should not be asking what the representation of an
488 -- unboxed tuple is, because it isn't a first class value.
492 tyConTheta :: TyCon -> [PredType]
493 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
494 tyConTheta (TupleTyCon {}) = []
495 -- shouldn't ask about anything else
498 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
499 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
500 actually computed (in another file).
503 tyConArgVrcs :: TyCon -> ArgVrcs
504 tyConArgVrcs tc = expectJust "tyConArgVrcs" (tyConArgVrcs_maybe tc)
506 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
507 tyConArgVrcs_maybe (FunTyCon {}) = Just [(False,True),(True,False)]
508 tyConArgVrcs_maybe (AlgTyCon {argVrcs = oi}) = Just oi
509 tyConArgVrcs_maybe (PrimTyCon {argVrcs = oi}) = Just oi
510 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity}) = Just (replicate arity (True,False))
511 tyConArgVrcs_maybe (SynTyCon {argVrcs = oi}) = Just oi
512 tyConArgVrcs_maybe _ = Nothing
516 getSynTyConDefn :: TyCon -> ([TyVar], Type)
517 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
521 maybeTyConSingleCon :: TyCon -> Maybe DataCon
522 maybeTyConSingleCon (AlgTyCon {dataCons = DataCons [c]}) = Just c
523 maybeTyConSingleCon (AlgTyCon {}) = Nothing
524 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
525 maybeTyConSingleCon (PrimTyCon {}) = Nothing
526 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
527 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
531 isClassTyCon :: TyCon -> Bool
532 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
533 isClassTyCon other_tycon = False
535 tyConClass_maybe :: TyCon -> Maybe Class
536 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
537 tyConClass_maybe ther_tycon = Nothing
541 %************************************************************************
543 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
545 %************************************************************************
547 @TyCon@s are compared by comparing their @Unique@s.
549 The strictness analyser needs @Ord@. It is a lexicographic order with
550 the property @(a<=b) || (b<=a)@.
553 instance Eq TyCon where
554 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
555 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
557 instance Ord TyCon where
558 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
559 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
560 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
561 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
562 compare a b = getUnique a `compare` getUnique b
564 instance Uniquable TyCon where
565 getUnique tc = tyConUnique tc
567 instance Outputable TyCon where
568 ppr tc = ppr (getName tc)
570 instance NamedThing TyCon where
575 %************************************************************************
577 \subsection{Kind constructors}
579 %************************************************************************
581 @matchesTyCon tc1 tc2@ checks whether an appliation
582 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
583 except that at the kind level tc2 might have more boxity info than tc1.
586 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
587 -> TyCon -- Inferred (e.g. type of actual arg to function)
590 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
592 uniq1 = tyConUnique tc1
593 uniq2 = tyConUnique tc2