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 )
71 %************************************************************************
73 \subsection{The data type}
75 %************************************************************************
79 type SuperKindCon = TyCon
83 tyConUnique :: Unique,
90 | AlgTyCon { -- Tuples, data type, and newtype decls.
91 -- All lifted, all boxed
92 tyConUnique :: Unique,
97 tyConTyVars :: [TyVar],
98 tyConArgVrcs :: ArgVrcs,
99 algTyConTheta :: [PredType],
101 dataCons :: [DataCon],
102 -- Its data constructors, with fully polymorphic types
103 -- This list can be empty, when we import a data type abstractly,
104 -- either (a) the interface is hand-written and doesn't give
105 -- the constructors, or
106 -- (b) in a quest for fast compilation we don't import
109 selIds :: [Id], -- Its record selectors (if any)
111 noOfDataCons :: Int, -- Number of data constructors
112 -- Usually this is the same as the length of the
113 -- dataCons field, but the latter may be empty if
114 -- we imported the type abstractly. But even if we import
115 -- abstractly we still need to know the number of constructors
116 -- so we can get the return convention right. Tiresome!
118 algTyConFlavour :: AlgTyConFlavour,
119 algTyConRec :: RecFlag, -- Tells whether the data type is part of
120 -- a mutually-recursive group or not
122 genInfo :: Maybe (EP Id), -- Convert T <-> Tring
123 -- Some TyCons don't have it;
124 -- e.g. the TyCon for a Class dictionary,
125 -- and TyCons with unboxed arguments
127 algTyConClass :: Maybe Class -- Just cl if this tycon came from a class declaration
130 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
131 -- Now includes foreign-imported types
132 tyConUnique :: Unique,
136 tyConArgVrcs :: ArgVrcs,
137 primTyConRep :: PrimRep, -- Many primitive tycons are unboxed, but some are
138 -- boxed (represented by pointers). The PrimRep tells.
140 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
141 -- but foreign-imported ones may not be
142 tyConExtName :: Maybe FastString
147 tyConUnique :: Unique,
151 tyConBoxed :: Boxity,
152 tyConTyVars :: [TyVar],
154 genInfo :: Maybe (EP Id) -- Generic type and conv funs
158 tyConUnique :: Unique,
163 tyConTyVars :: [TyVar], -- Bound tyvars
164 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
165 -- Acts as a template for the expansion when
166 -- the tycon is applied to some types.
167 tyConArgVrcs :: ArgVrcs
170 | KindCon { -- Type constructor at the kind level
171 tyConUnique :: Unique,
173 tyConKind :: SuperKind,
177 | SuperKindCon { -- The type of kind variables or boxity variables,
178 tyConUnique :: Unique,
182 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
185 = DataTyCon -- Data type
187 | EnumTyCon -- Special sort of enumeration type
189 | NewTyCon Type -- Newtype, with its *ultimate* representation type
190 -- By 'ultimate' I mean that the rep type is not itself
191 -- a newtype or type synonym.
193 -- The rep type has free type variables the tyConTyVars
195 -- newtype T a = MkT [(a,Int)]
196 -- The rep type is [(a,Int)]
198 -- The rep type isn't entirely simple:
199 -- for a recursive newtype we pick () as the rep type
203 %************************************************************************
205 \subsection{TyCon Construction}
207 %************************************************************************
209 Note: the TyCon constructors all take a Kind as one argument, even though
210 they could, in principle, work out their Kind from their other arguments.
211 But to do so they need functions from Types, and that makes a nasty
212 module mutual-recursion. And they aren't called from many places.
213 So we compromise, and move their Kind calculation to the call site.
216 mkSuperKindCon :: Name -> SuperKindCon
217 mkSuperKindCon name = SuperKindCon {
218 tyConUnique = nameUnique name,
222 mkKindCon :: Name -> SuperKind -> KindCon
225 tyConUnique = nameUnique name,
231 mkFunTyCon :: Name -> Kind -> TyCon
234 tyConUnique = nameUnique name,
240 tyConGenInfo :: TyCon -> Maybe (EP Id)
241 tyConGenInfo (AlgTyCon { genInfo = info }) = info
242 tyConGenInfo (TupleTyCon { genInfo = info }) = info
243 tyConGenInfo other = Nothing
245 tyConGenIds :: TyCon -> [Id]
246 -- Returns the generic-programming Ids; these Ids need bindings
247 tyConGenIds tycon = case tyConGenInfo tycon of
249 Just (EP from to) -> [from,to]
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 cons ncons sels flavour rec
258 tyConUnique = nameUnique name,
260 tyConArity = length tyvars,
261 tyConTyVars = tyvars,
262 tyConArgVrcs = argvrcs,
263 algTyConTheta = theta,
266 noOfDataCons = ncons,
267 algTyConClass = Nothing,
268 algTyConFlavour = flavour,
273 mkClassTyCon name kind tyvars argvrcs con clas flavour rec
276 tyConUnique = nameUnique name,
278 tyConArity = length tyvars,
279 tyConTyVars = tyvars,
280 tyConArgVrcs = argvrcs,
285 algTyConClass = Just clas,
286 algTyConFlavour = flavour,
292 mkTupleTyCon name kind arity tyvars con boxed gen_info
294 tyConUnique = nameUnique name,
299 tyConTyVars = tyvars,
304 -- Foreign-imported (.NET) type constructors are represented
305 -- as primitive, but *lifted*, TyCons for now. They are lifted
306 -- because the Haskell type T representing the (foreign) .NET
307 -- type T is actually implemented (in ILX) as a thunk<T>
309 mkForeignTyCon name ext_name kind arity arg_vrcs
312 tyConUnique = nameUnique name,
315 tyConArgVrcs = arg_vrcs,
316 primTyConRep = PtrRep,
318 tyConExtName = ext_name
322 -- most Prim tycons are lifted
323 mkPrimTyCon name kind arity arg_vrcs rep
324 = mkPrimTyCon' name kind arity arg_vrcs rep True
326 -- but RealWorld is lifted
327 mkLiftedPrimTyCon name kind arity arg_vrcs rep
328 = mkPrimTyCon' name kind arity arg_vrcs rep False
330 mkPrimTyCon' name kind arity arg_vrcs rep is_unlifted
333 tyConUnique = nameUnique name,
336 tyConArgVrcs = arg_vrcs,
338 isUnLifted = is_unlifted,
339 tyConExtName = Nothing
342 mkSynTyCon name kind arity tyvars rhs argvrcs
345 tyConUnique = nameUnique name,
348 tyConTyVars = tyvars,
350 tyConArgVrcs = argvrcs
353 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
358 isFunTyCon (FunTyCon {}) = True
361 isPrimTyCon (PrimTyCon {}) = True
362 isPrimTyCon _ = False
364 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
365 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
366 isUnLiftedTyCon _ = False
368 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
369 isBoxedTyCon (AlgTyCon {}) = True
370 isBoxedTyCon (FunTyCon {}) = True
371 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
372 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
374 -- isAlgTyCon returns True for both @data@ and @newtype@
375 isAlgTyCon (AlgTyCon {}) = True
376 isAlgTyCon (TupleTyCon {}) = True
377 isAlgTyCon other = False
379 -- isDataTyCon returns True for data types that are represented by
380 -- heap-allocated constructors.
381 -- These are srcutinised by Core-level @case@ expressions, and they
382 -- get info tables allocated for them.
383 -- True for all @data@ types
384 -- False for newtypes
386 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data, algTyConRec = is_rec})
387 = case new_or_data of
391 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
392 isDataTyCon other = False
394 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
395 isNewTyCon other = False
397 newTyConRep :: TyCon -> ([TyVar], Type)
398 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTyConFlavour = NewTyCon rep}) = (tvs, rep)
401 -- has *one* constructor,
402 -- is *not* existential
404 -- may be DataType or NewType,
405 -- may be unboxed or not,
406 -- may be recursive or not
407 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
408 isProductTyCon (TupleTyCon {}) = True
409 isProductTyCon other = False
411 isSynTyCon (SynTyCon {}) = True
414 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
415 isEnumerationTyCon other = False
417 -- The unit tycon didn't used to be classed as a tuple tycon
418 -- but I thought that was silly so I've undone it
419 -- If it can't be for some reason, it should be a AlgTyCon
420 isTupleTyCon (TupleTyCon {}) = True
421 isTupleTyCon other = False
423 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
424 isUnboxedTupleTyCon other = False
426 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
427 isBoxedTupleTyCon other = False
429 tupleTyConBoxity tc = tyConBoxed tc
431 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
432 isRecursiveTyCon other = False
434 -- isForeignTyCon identifies foreign-imported type constructors
435 -- For the moment, they are primitive but lifted, but that may change
436 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
437 isForeignTyCon other = False
441 tyConDataCons :: TyCon -> [DataCon]
442 tyConDataCons tycon = ASSERT2( length cons == tyConFamilySize tycon, ppr tycon )
445 cons = tyConDataConsIfAvailable tycon
447 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
448 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
449 tyConDataConsIfAvailable other = []
450 -- You may think this last equation should fail,
451 -- but it's quite convenient to return no constructors for
452 -- a synonym; see for example the call in TcTyClsDecls.
454 tyConFamilySize :: TyCon -> Int
455 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
456 tyConFamilySize (TupleTyCon {}) = 1
458 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
461 tyConSelIds :: TyCon -> [Id]
462 tyConSelIds (AlgTyCon {selIds = sels}) = sels
463 tyConSelIds other_tycon = []
467 tyConPrimRep :: TyCon -> PrimRep
468 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
469 tyConPrimRep tc = ASSERT( not (isUnboxedTupleTyCon tc) )
471 -- We should not be asking what the representation of an
472 -- unboxed tuple is, because it isn't a first class value.
476 tyConTheta :: TyCon -> [PredType]
477 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
478 tyConTheta (TupleTyCon {}) = []
479 -- shouldn't ask about anything else
482 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
483 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
484 actually computed (in another file).
487 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
489 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
490 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
491 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
492 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
493 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
494 tyConArgVrcs_maybe _ = Nothing
498 getSynTyConDefn :: TyCon -> ([TyVar], Type)
499 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
503 maybeTyConSingleCon :: TyCon -> Maybe DataCon
504 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
505 maybeTyConSingleCon (AlgTyCon {}) = Nothing
506 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
507 maybeTyConSingleCon (PrimTyCon {}) = Nothing
508 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
509 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
514 isClassTyCon :: TyCon -> Bool
515 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
516 isClassTyCon other_tycon = False
518 tyConClass_maybe :: TyCon -> Maybe Class
519 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
520 tyConClass_maybe ther_tycon = Nothing
524 %************************************************************************
526 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
528 %************************************************************************
530 @TyCon@s are compared by comparing their @Unique@s.
532 The strictness analyser needs @Ord@. It is a lexicographic order with
533 the property @(a<=b) || (b<=a)@.
536 instance Eq TyCon where
537 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
538 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
540 instance Ord TyCon where
541 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
542 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
543 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
544 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
545 compare a b = getUnique a `compare` getUnique b
547 instance Uniquable TyCon where
548 getUnique tc = tyConUnique tc
550 instance Outputable TyCon where
551 ppr tc = ppr (getName tc)
553 instance NamedThing TyCon where
558 %************************************************************************
560 \subsection{Kind constructors}
562 %************************************************************************
564 @matchesTyCon tc1 tc2@ checks whether an appliation
565 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
566 except that at the kind level tc2 might have more boxity info than tc1.
569 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
570 -> TyCon -- Inferred (e.g. type of actual arg to function)
573 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
575 uniq1 = tyConUnique tc1
576 uniq2 = tyConUnique tc2