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 mkAlgTyCon, --mkAlgTyCon,
32 tyConDataCons, tyConDataConsIfAvailable, tyConFamilySize,
45 tyConGenIds, tyConGenInfo
48 #include "HsVersions.h"
50 import {-# SOURCE #-} TypeRep ( Type, PredType, Kind, SuperKind )
51 -- Should just be Type(Type), but this fails due to bug present up to
52 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
54 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
57 import Var ( TyVar, Id )
58 import BasicTypes ( Arity, RecFlag(..), Boxity(..),
60 import Name ( Name, nameUnique, NamedThing(getName) )
61 import PrelNames ( Unique, Uniquable(..), anyBoxConKey )
62 import PrimRep ( PrimRep(..), isFollowableRep )
66 %************************************************************************
68 \subsection{The data type}
70 %************************************************************************
74 type SuperKindCon = TyCon
78 tyConUnique :: Unique,
85 | AlgTyCon { -- Tuples, data type, and newtype decls.
86 -- All lifted, all boxed
87 tyConUnique :: Unique,
92 tyConTyVars :: [TyVar],
93 tyConArgVrcs :: ArgVrcs,
94 algTyConTheta :: [PredType],
96 dataCons :: [DataCon],
97 -- Its data constructors, with fully polymorphic types
98 -- This list can be empty, when we import a data type abstractly,
99 -- either (a) the interface is hand-written and doesn't give
100 -- the constructors, or
101 -- (b) in a quest for fast compilation we don't import
104 selIds :: [Id], -- Its record selectors (if any)
106 noOfDataCons :: Int, -- Number of data constructors
107 -- Usually this is the same as the length of the
108 -- dataCons field, but the latter may be empty if
109 -- we imported the type abstractly. But even if we import
110 -- abstractly we still need to know the number of constructors
111 -- so we can get the return convention right. Tiresome!
113 algTyConFlavour :: AlgTyConFlavour,
114 algTyConRec :: RecFlag, -- Tells whether the data type is part of
115 -- a mutually-recursive group or not
117 genInfo :: Maybe (EP Id), -- Convert T <-> Tring
118 -- Some TyCons don't have it;
119 -- e.g. the TyCon for a Class dictionary,
120 -- and TyCons with unboxed arguments
122 algTyConClass :: Bool -- True if this tycon comes from a class declaration
125 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
126 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
127 tyConUnique :: Unique,
131 tyConArgVrcs :: ArgVrcs,
132 primTyConRep :: PrimRep
137 tyConUnique :: Unique,
141 tyConBoxed :: Boxity,
142 tyConTyVars :: [TyVar],
144 genInfo :: Maybe (EP Id) -- Generic type and conv funs
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.
157 tyConArgVrcs :: ArgVrcs
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)]
175 = DataTyCon -- Data type
176 | EnumTyCon -- Special sort of enumeration type
177 | NewTyCon Type -- Newtype, with its *ultimate* representation type
178 -- By 'ultimate' I mean that the rep type is not itself
179 -- a newtype or type synonym.
181 -- The rep type has explicit for-alls for the tyvars of
183 -- newtype T a = MkT [(a,Int)]
184 -- The rep type is forall a. [(a,Int)]
186 -- The rep type isn't entirely simple:
187 -- for a recursive newtype we pick () as the rep type
191 %************************************************************************
193 \subsection{TyCon Construction}
195 %************************************************************************
197 Note: the TyCon constructors all take a Kind as one argument, even though
198 they could, in principle, work out their Kind from their other arguments.
199 But to do so they need functions from Types, and that makes a nasty
200 module mutual-recursion. And they aren't called from many places.
201 So we compromise, and move their Kind calculation to the call site.
204 mkSuperKindCon :: Name -> SuperKindCon
205 mkSuperKindCon name = SuperKindCon {
206 tyConUnique = nameUnique name,
210 mkKindCon :: Name -> SuperKind -> KindCon
213 tyConUnique = nameUnique name,
219 mkFunTyCon :: Name -> Kind -> TyCon
222 tyConUnique = nameUnique name,
228 tyConGenInfo :: TyCon -> Maybe (EP Id)
229 tyConGenInfo (AlgTyCon { genInfo = info }) = info
230 tyConGenInfo (TupleTyCon { genInfo = info }) = info
231 tyConGenInfo other = Nothing
233 tyConGenIds :: TyCon -> [Id]
234 -- Returns the generic-programming Ids; these Ids need bindings
235 tyConGenIds tycon = case tyConGenInfo tycon of
237 Just (EP from to) -> [from,to]
239 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
240 -- but now you also have to pass in the generic information about the type
241 -- constructor - you can get hold of it easily (see Generics module)
242 mkAlgTyCon name kind tyvars theta argvrcs cons ncons sels flavour rec
246 tyConUnique = nameUnique name,
248 tyConArity = length tyvars,
249 tyConTyVars = tyvars,
250 tyConArgVrcs = argvrcs,
251 algTyConTheta = theta,
254 noOfDataCons = ncons,
255 algTyConClass = False,
256 algTyConFlavour = flavour,
261 mkClassTyCon name kind tyvars argvrcs con clas flavour
264 tyConUnique = nameUnique name,
266 tyConArity = length tyvars,
267 tyConTyVars = tyvars,
268 tyConArgVrcs = argvrcs,
273 algTyConClass = True,
274 algTyConFlavour = flavour,
275 algTyConRec = NonRecursive,
280 mkTupleTyCon name kind arity tyvars con boxed gen_info
282 tyConUnique = nameUnique name,
287 tyConTyVars = tyvars,
292 mkPrimTyCon name kind arity arg_vrcs rep
295 tyConUnique = nameUnique name,
298 tyConArgVrcs = arg_vrcs,
302 mkSynTyCon name kind arity tyvars rhs argvrcs
305 tyConUnique = nameUnique name,
308 tyConTyVars = tyvars,
310 tyConArgVrcs = argvrcs
313 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
318 isFunTyCon (FunTyCon {}) = True
321 isPrimTyCon (PrimTyCon {}) = True
322 isPrimTyCon _ = False
324 isUnLiftedTyCon (PrimTyCon {}) = True
325 isUnLiftedTyCon (TupleTyCon { tyConBoxed = boxity}) = not (isBoxed boxity)
326 isUnLiftedTyCon _ = False
328 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
329 isBoxedTyCon (AlgTyCon {}) = True
330 isBoxedTyCon (FunTyCon {}) = True
331 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
332 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
334 -- isAlgTyCon returns True for both @data@ and @newtype@
335 isAlgTyCon (AlgTyCon {}) = True
336 isAlgTyCon (TupleTyCon {}) = True
337 isAlgTyCon other = False
339 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
340 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
343 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
344 isDataTyCon other = False
346 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
347 isNewTyCon other = False
349 newTyConRep (AlgTyCon {algTyConFlavour = NewTyCon rep}) = Just rep
350 newTyConRep other = Nothing
353 -- has *one* constructor,
354 -- is *not* existential
356 -- may be DataType or NewType,
357 -- may be unboxed or not,
358 -- may be recursive or not
359 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
360 isProductTyCon (TupleTyCon {}) = True
361 isProductTyCon other = False
363 isSynTyCon (SynTyCon {}) = True
366 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
367 isEnumerationTyCon other = False
369 -- The unit tycon didn't used to be classed as a tuple tycon
370 -- but I thought that was silly so I've undone it
371 -- If it can't be for some reason, it should be a AlgTyCon
372 isTupleTyCon (TupleTyCon {}) = True
373 isTupleTyCon other = False
375 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
376 isUnboxedTupleTyCon other = False
378 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
379 isBoxedTupleTyCon other = False
381 tupleTyConBoxity tc = tyConBoxed tc
383 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
384 isRecursiveTyCon other = False
388 tyConDataCons :: TyCon -> [DataCon]
389 tyConDataCons tycon = ASSERT2( not (null cons), ppr tycon )
390 ASSERT2( length cons == tyConFamilySize tycon, ppr tycon )
393 cons = tyConDataConsIfAvailable tycon
395 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
396 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
397 tyConDataConsIfAvailable other = []
398 -- You may think this last equation should fail,
399 -- but it's quite convenient to return no constructors for
400 -- a synonym; see for example the call in TcTyClsDecls.
402 tyConFamilySize :: TyCon -> Int
403 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
404 tyConFamilySize (TupleTyCon {}) = 1
406 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
409 tyConSelIds :: TyCon -> [Id]
410 tyConSelIds (AlgTyCon {selIds = sels}) = sels
411 tyConSelIds other_tycon = []
415 tyConPrimRep :: TyCon -> PrimRep
416 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
417 tyConPrimRep _ = PtrRep
421 tyConTheta :: TyCon -> [PredType]
422 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
423 -- should ask about anything else
426 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
427 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
428 actually computed (in another file).
431 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
433 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
434 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
435 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
436 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
437 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
438 tyConArgVrcs_maybe _ = Nothing
442 getSynTyConDefn :: TyCon -> ([TyVar], Type)
443 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
447 maybeTyConSingleCon :: TyCon -> Maybe DataCon
448 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
449 maybeTyConSingleCon (AlgTyCon {}) = Nothing
450 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
451 maybeTyConSingleCon (PrimTyCon {}) = Nothing
452 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
453 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
458 isClassTyCon :: TyCon -> Bool
459 isClassTyCon (AlgTyCon {algTyConClass = is_class_tycon}) = is_class_tycon
460 isClassTyCon other_tycon = False
464 %************************************************************************
466 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
468 %************************************************************************
470 @TyCon@s are compared by comparing their @Unique@s.
472 The strictness analyser needs @Ord@. It is a lexicographic order with
473 the property @(a<=b) || (b<=a)@.
476 instance Eq TyCon where
477 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
478 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
480 instance Ord TyCon where
481 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
482 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
483 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
484 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
485 compare a b = getUnique a `compare` getUnique b
487 instance Uniquable TyCon where
488 getUnique tc = tyConUnique tc
490 instance Outputable TyCon where
491 ppr tc = ppr (getName tc)
493 instance NamedThing TyCon where
498 %************************************************************************
500 \subsection{Kind constructors}
502 %************************************************************************
504 @matchesTyCon tc1 tc2@ checks whether an appliation
505 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
506 except that at the kind level tc2 might have more boxity info than tc1.
509 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
510 -> TyCon -- Inferred (e.g. type of actual arg to function)
513 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
515 uniq1 = tyConUnique tc1
516 uniq2 = tyConUnique tc2