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, 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 Class ( ClassContext )
58 import Var ( TyVar, Id )
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 :: ClassContext,
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 :: Bool -- True if this tycon comes 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 = False,
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 = True,
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 ) cons
392 cons = tyConDataConsIfAvailable tycon
394 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
395 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
396 tyConDataConsIfAvailable other = []
397 -- You may think this last equation should fail,
398 -- but it's quite convenient to return no constructors for
399 -- a synonym; see for example the call in TcTyClsDecls.
401 tyConFamilySize :: TyCon -> Int
402 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
403 tyConFamilySize (TupleTyCon {}) = 1
405 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
408 tyConSelIds :: TyCon -> [Id]
409 tyConSelIds (AlgTyCon {selIds = sels}) = sels
410 tyConSelIds other_tycon = []
414 tyConPrimRep :: TyCon -> PrimRep
415 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
416 tyConPrimRep _ = PtrRep
420 tyConTheta :: TyCon -> ClassContext
421 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
422 -- should ask about anything else
425 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
426 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
427 actually computed (in another file).
430 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
432 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
433 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
434 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
435 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
436 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
437 tyConArgVrcs_maybe _ = Nothing
441 getSynTyConDefn :: TyCon -> ([TyVar], Type)
442 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
446 maybeTyConSingleCon :: TyCon -> Maybe DataCon
447 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
448 maybeTyConSingleCon (AlgTyCon {}) = Nothing
449 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
450 maybeTyConSingleCon (PrimTyCon {}) = Nothing
451 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
452 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
457 isClassTyCon :: TyCon -> Bool
458 isClassTyCon (AlgTyCon {algTyConClass = is_class_tycon}) = is_class_tycon
459 isClassTyCon other_tycon = False
463 %************************************************************************
465 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
467 %************************************************************************
469 @TyCon@s are compared by comparing their @Unique@s.
471 The strictness analyser needs @Ord@. It is a lexicographic order with
472 the property @(a<=b) || (b<=a)@.
475 instance Eq TyCon where
476 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
477 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
479 instance Ord TyCon where
480 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
481 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
482 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
483 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
484 compare a b = getUnique a `compare` getUnique b
486 instance Uniquable TyCon where
487 getUnique tc = tyConUnique tc
489 instance Outputable TyCon where
490 ppr tc = ppr (getName tc)
492 instance NamedThing TyCon where
497 %************************************************************************
499 \subsection{Kind constructors}
501 %************************************************************************
503 @matchesTyCon tc1 tc2@ checks whether an appliation
504 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
505 except that at the kind level tc2 might have more boxity info than tc1.
508 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
509 -> TyCon -- Inferred (e.g. type of actual arg to function)
512 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
514 uniq1 = tyConUnique tc1
515 uniq2 = tyConUnique tc2