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,
37 isClassTyCon, tyConClass_maybe,
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 Class ( Class )
59 import BasicTypes ( Arity, RecFlag(..), Boxity(..),
61 import Name ( Name, nameUnique, NamedThing(getName) )
62 import PrelNames ( Unique, Uniquable(..), anyBoxConKey )
63 import PrimRep ( PrimRep(..), isFollowableRep )
67 %************************************************************************
69 \subsection{The data type}
71 %************************************************************************
75 type SuperKindCon = TyCon
79 tyConUnique :: Unique,
86 | AlgTyCon { -- Tuples, data type, and newtype decls.
87 -- All lifted, all boxed
88 tyConUnique :: Unique,
93 tyConTyVars :: [TyVar],
94 tyConArgVrcs :: ArgVrcs,
95 algTyConTheta :: [PredType],
97 dataCons :: [DataCon],
98 -- Its data constructors, with fully polymorphic types
99 -- This list can be empty, when we import a data type abstractly,
100 -- either (a) the interface is hand-written and doesn't give
101 -- the constructors, or
102 -- (b) in a quest for fast compilation we don't import
105 selIds :: [Id], -- Its record selectors (if any)
107 noOfDataCons :: Int, -- Number of data constructors
108 -- Usually this is the same as the length of the
109 -- dataCons field, but the latter may be empty if
110 -- we imported the type abstractly. But even if we import
111 -- abstractly we still need to know the number of constructors
112 -- so we can get the return convention right. Tiresome!
114 algTyConFlavour :: AlgTyConFlavour,
115 algTyConRec :: RecFlag, -- Tells whether the data type is part of
116 -- a mutually-recursive group or not
118 genInfo :: Maybe (EP Id), -- Convert T <-> Tring
119 -- Some TyCons don't have it;
120 -- e.g. the TyCon for a Class dictionary,
121 -- and TyCons with unboxed arguments
123 algTyConClass :: Maybe Class -- Just cl if this tycon came from a class declaration
126 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
127 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
128 tyConUnique :: Unique,
132 tyConArgVrcs :: ArgVrcs,
133 primTyConRep :: PrimRep
138 tyConUnique :: Unique,
142 tyConBoxed :: Boxity,
143 tyConTyVars :: [TyVar],
145 genInfo :: Maybe (EP Id) -- Generic type and conv funs
149 tyConUnique :: Unique,
154 tyConTyVars :: [TyVar], -- Bound tyvars
155 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
156 -- Acts as a template for the expansion when
157 -- the tycon is applied to some types.
158 tyConArgVrcs :: ArgVrcs
161 | KindCon { -- Type constructor at the kind level
162 tyConUnique :: Unique,
164 tyConKind :: SuperKind,
168 | SuperKindCon { -- The type of kind variables or boxity variables,
169 tyConUnique :: Unique,
173 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
176 = DataTyCon -- Data type
177 | EnumTyCon -- Special sort of 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.
182 -- The rep type has explicit for-alls for the tyvars of
184 -- newtype T a = MkT [(a,Int)]
185 -- The rep type is forall a. [(a,Int)]
187 -- The rep type isn't entirely simple:
188 -- for a recursive newtype we pick () as the rep type
192 %************************************************************************
194 \subsection{TyCon Construction}
196 %************************************************************************
198 Note: the TyCon constructors all take a Kind as one argument, even though
199 they could, in principle, work out their Kind from their other arguments.
200 But to do so they need functions from Types, and that makes a nasty
201 module mutual-recursion. And they aren't called from many places.
202 So we compromise, and move their Kind calculation to the call site.
205 mkSuperKindCon :: Name -> SuperKindCon
206 mkSuperKindCon name = SuperKindCon {
207 tyConUnique = nameUnique name,
211 mkKindCon :: Name -> SuperKind -> KindCon
214 tyConUnique = nameUnique name,
220 mkFunTyCon :: Name -> Kind -> TyCon
223 tyConUnique = nameUnique name,
229 tyConGenInfo :: TyCon -> Maybe (EP Id)
230 tyConGenInfo (AlgTyCon { genInfo = info }) = info
231 tyConGenInfo (TupleTyCon { genInfo = info }) = info
232 tyConGenInfo other = Nothing
234 tyConGenIds :: TyCon -> [Id]
235 -- Returns the generic-programming Ids; these Ids need bindings
236 tyConGenIds tycon = case tyConGenInfo tycon of
238 Just (EP from to) -> [from,to]
240 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
241 -- but now you also have to pass in the generic information about the type
242 -- constructor - you can get hold of it easily (see Generics module)
243 mkAlgTyCon name kind tyvars theta argvrcs cons ncons sels flavour rec
247 tyConUnique = nameUnique name,
249 tyConArity = length tyvars,
250 tyConTyVars = tyvars,
251 tyConArgVrcs = argvrcs,
252 algTyConTheta = theta,
255 noOfDataCons = ncons,
256 algTyConClass = Nothing,
257 algTyConFlavour = flavour,
262 mkClassTyCon name kind tyvars argvrcs con clas flavour
265 tyConUnique = nameUnique name,
267 tyConArity = length tyvars,
268 tyConTyVars = tyvars,
269 tyConArgVrcs = argvrcs,
274 algTyConClass = Just clas,
275 algTyConFlavour = flavour,
276 algTyConRec = NonRecursive,
281 mkTupleTyCon name kind arity tyvars con boxed gen_info
283 tyConUnique = nameUnique name,
288 tyConTyVars = tyvars,
293 mkPrimTyCon name kind arity arg_vrcs rep
296 tyConUnique = nameUnique name,
299 tyConArgVrcs = arg_vrcs,
303 mkSynTyCon name kind arity tyvars rhs argvrcs
306 tyConUnique = nameUnique name,
309 tyConTyVars = tyvars,
311 tyConArgVrcs = argvrcs
314 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
319 isFunTyCon (FunTyCon {}) = True
322 isPrimTyCon (PrimTyCon {}) = True
323 isPrimTyCon _ = False
325 isUnLiftedTyCon (PrimTyCon {}) = True
326 isUnLiftedTyCon (TupleTyCon { tyConBoxed = boxity}) = not (isBoxed boxity)
327 isUnLiftedTyCon _ = False
329 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
330 isBoxedTyCon (AlgTyCon {}) = True
331 isBoxedTyCon (FunTyCon {}) = True
332 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
333 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
335 -- isAlgTyCon returns True for both @data@ and @newtype@
336 isAlgTyCon (AlgTyCon {}) = True
337 isAlgTyCon (TupleTyCon {}) = True
338 isAlgTyCon other = False
340 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
341 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
344 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
345 isDataTyCon other = False
347 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
348 isNewTyCon other = False
350 newTyConRep (AlgTyCon {algTyConFlavour = NewTyCon rep}) = Just rep
351 newTyConRep other = Nothing
354 -- has *one* constructor,
355 -- is *not* existential
357 -- may be DataType or NewType,
358 -- may be unboxed or not,
359 -- may be recursive or not
360 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
361 isProductTyCon (TupleTyCon {}) = True
362 isProductTyCon other = False
364 isSynTyCon (SynTyCon {}) = True
367 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
368 isEnumerationTyCon other = False
370 -- The unit tycon didn't used to be classed as a tuple tycon
371 -- but I thought that was silly so I've undone it
372 -- If it can't be for some reason, it should be a AlgTyCon
373 isTupleTyCon (TupleTyCon {}) = True
374 isTupleTyCon other = False
376 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
377 isUnboxedTupleTyCon other = False
379 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
380 isBoxedTupleTyCon other = False
382 tupleTyConBoxity tc = tyConBoxed tc
384 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
385 isRecursiveTyCon other = False
389 tyConDataCons :: TyCon -> [DataCon]
390 tyConDataCons tycon = ASSERT2( not (null cons), ppr tycon )
391 ASSERT2( length cons == tyConFamilySize tycon, ppr tycon )
394 cons = tyConDataConsIfAvailable tycon
396 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
397 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
398 tyConDataConsIfAvailable other = []
399 -- You may think this last equation should fail,
400 -- but it's quite convenient to return no constructors for
401 -- a synonym; see for example the call in TcTyClsDecls.
403 tyConFamilySize :: TyCon -> Int
404 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
405 tyConFamilySize (TupleTyCon {}) = 1
407 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
410 tyConSelIds :: TyCon -> [Id]
411 tyConSelIds (AlgTyCon {selIds = sels}) = sels
412 tyConSelIds other_tycon = []
416 tyConPrimRep :: TyCon -> PrimRep
417 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
418 tyConPrimRep _ = PtrRep
422 tyConTheta :: TyCon -> [PredType]
423 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
424 -- should ask about anything else
427 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
428 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
429 actually computed (in another file).
432 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
434 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
435 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
436 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
437 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
438 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
439 tyConArgVrcs_maybe _ = Nothing
443 getSynTyConDefn :: TyCon -> ([TyVar], Type)
444 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
448 maybeTyConSingleCon :: TyCon -> Maybe DataCon
449 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
450 maybeTyConSingleCon (AlgTyCon {}) = Nothing
451 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
452 maybeTyConSingleCon (PrimTyCon {}) = Nothing
453 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
454 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
459 isClassTyCon :: TyCon -> Bool
460 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
461 isClassTyCon other_tycon = False
463 tyConClass_maybe :: TyCon -> Maybe Class
464 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
465 tyConClass_maybe ther_tycon = Nothing
469 %************************************************************************
471 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
473 %************************************************************************
475 @TyCon@s are compared by comparing their @Unique@s.
477 The strictness analyser needs @Ord@. It is a lexicographic order with
478 the property @(a<=b) || (b<=a)@.
481 instance Eq TyCon where
482 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
483 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
485 instance Ord TyCon where
486 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
487 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
488 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
489 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
490 compare a b = getUnique a `compare` getUnique b
492 instance Uniquable TyCon where
493 getUnique tc = tyConUnique tc
495 instance Outputable TyCon where
496 ppr tc = ppr (getName tc)
498 instance NamedThing TyCon where
503 %************************************************************************
505 \subsection{Kind constructors}
507 %************************************************************************
509 @matchesTyCon tc1 tc2@ checks whether an appliation
510 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
511 except that at the kind level tc2 might have more boxity info than tc1.
514 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
515 -> TyCon -- Inferred (e.g. type of actual arg to function)
518 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
520 uniq1 = tyConUnique tc1
521 uniq2 = tyConUnique tc2