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,
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 noOfDataCons :: Int, -- Number of data constructors
106 -- Usually this is the same as the length of the
107 -- dataCons field, but the latter may be empty if
108 -- we imported the type abstractly. But even if we import
109 -- abstractly we still need to know the number of constructors
110 -- so we can get the return convention right. Tiresome!
112 algTyConFlavour :: AlgTyConFlavour,
113 algTyConRec :: RecFlag, -- Tells whether the data type is part of
114 -- a mutually-recursive group or not
116 genInfo :: Maybe (EP Id), -- Convert T <-> Tring
117 -- Some TyCons don't have it;
118 -- e.g. the TyCon for a Class dictionary,
119 -- and TyCons with unboxed arguments
121 algTyConClass :: Bool -- True if this tycon comes from a class declaration
124 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
125 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
126 tyConUnique :: Unique,
130 tyConArgVrcs :: ArgVrcs,
131 primTyConRep :: PrimRep
136 tyConUnique :: Unique,
140 tyConBoxed :: Boxity,
141 tyConTyVars :: [TyVar],
143 genInfo :: Maybe (EP Id) -- Generic type and conv funs
147 tyConUnique :: Unique,
152 tyConTyVars :: [TyVar], -- Bound tyvars
153 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
154 -- Acts as a template for the expansion when
155 -- the tycon is applied to some types.
156 tyConArgVrcs :: ArgVrcs
159 | KindCon { -- Type constructor at the kind level
160 tyConUnique :: Unique,
162 tyConKind :: SuperKind,
166 | SuperKindCon { -- The type of kind variables or boxity variables,
167 tyConUnique :: Unique,
171 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
174 = DataTyCon -- Data type
175 | EnumTyCon -- Special sort of enumeration type
176 | NewTyCon Type -- Newtype, with its *ultimate* representation type
177 -- By 'ultimate' I mean that the rep type is not itself
178 -- a newtype or type synonym.
180 -- The rep type has explicit for-alls for the tyvars of
182 -- newtype T a = MkT [(a,Int)]
183 -- The rep type is forall a. [(a,Int)]
185 -- The rep type isn't entirely simple:
186 -- for a recursive newtype we pick () as the rep type
190 %************************************************************************
192 \subsection{TyCon Construction}
194 %************************************************************************
196 Note: the TyCon constructors all take a Kind as one argument, even though
197 they could, in principle, work out their Kind from their other arguments.
198 But to do so they need functions from Types, and that makes a nasty
199 module mutual-recursion. And they aren't called from many places.
200 So we compromise, and move their Kind calculation to the call site.
203 mkSuperKindCon :: Name -> SuperKindCon
204 mkSuperKindCon name = SuperKindCon {
205 tyConUnique = nameUnique name,
209 mkKindCon :: Name -> SuperKind -> KindCon
212 tyConUnique = nameUnique name,
218 mkFunTyCon :: Name -> Kind -> TyCon
221 tyConUnique = nameUnique name,
227 tyConGenInfo :: TyCon -> Maybe (EP Id)
228 tyConGenInfo (AlgTyCon { genInfo = info }) = info
229 tyConGenInfo (TupleTyCon { genInfo = info }) = info
230 tyConGenInfo other = Nothing
232 tyConGenIds :: TyCon -> [Id]
233 -- Returns the generic-programming Ids; these Ids need bindings
234 tyConGenIds tycon = case tyConGenInfo tycon of
236 Just (EP from to) -> [from,to]
238 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
239 -- but now you also have to pass in the generic information about the type
240 -- constructor - you can get hold of it easily (see Generics module)
241 mkAlgTyCon name kind tyvars theta argvrcs cons ncons flavour rec
245 tyConUnique = nameUnique name,
247 tyConArity = length tyvars,
248 tyConTyVars = tyvars,
249 tyConArgVrcs = argvrcs,
250 algTyConTheta = theta,
252 noOfDataCons = ncons,
253 algTyConClass = False,
254 algTyConFlavour = flavour,
259 mkClassTyCon name kind tyvars argvrcs con clas flavour
262 tyConUnique = nameUnique name,
264 tyConArity = length tyvars,
265 tyConTyVars = tyvars,
266 tyConArgVrcs = argvrcs,
270 algTyConClass = True,
271 algTyConFlavour = flavour,
272 algTyConRec = NonRecursive,
277 mkTupleTyCon name kind arity tyvars con boxed gen_info
279 tyConUnique = nameUnique name,
284 tyConTyVars = tyvars,
289 mkPrimTyCon name kind arity arg_vrcs rep
292 tyConUnique = nameUnique name,
295 tyConArgVrcs = arg_vrcs,
299 mkSynTyCon name kind arity tyvars rhs argvrcs
302 tyConUnique = nameUnique name,
305 tyConTyVars = tyvars,
307 tyConArgVrcs = argvrcs
310 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
315 isFunTyCon (FunTyCon {}) = True
318 isPrimTyCon (PrimTyCon {}) = True
319 isPrimTyCon _ = False
321 isUnLiftedTyCon (PrimTyCon {}) = True
322 isUnLiftedTyCon (TupleTyCon { tyConBoxed = boxity}) = not (isBoxed boxity)
323 isUnLiftedTyCon _ = False
325 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
326 isBoxedTyCon (AlgTyCon {}) = True
327 isBoxedTyCon (FunTyCon {}) = True
328 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
329 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
331 -- isAlgTyCon returns True for both @data@ and @newtype@
332 isAlgTyCon (AlgTyCon {}) = True
333 isAlgTyCon (TupleTyCon {}) = True
334 isAlgTyCon other = False
336 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
337 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
340 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
341 isDataTyCon other = False
343 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
344 isNewTyCon other = False
346 newTyConRep (AlgTyCon {algTyConFlavour = NewTyCon rep}) = Just rep
347 newTyConRep other = Nothing
350 -- has *one* constructor,
351 -- is *not* existential
353 -- may be DataType or NewType,
354 -- may be unboxed or not,
355 -- may be recursive or not
356 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
357 isProductTyCon (TupleTyCon {}) = True
358 isProductTyCon other = False
360 isSynTyCon (SynTyCon {}) = True
363 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
364 isEnumerationTyCon other = False
366 -- The unit tycon didn't used to be classed as a tuple tycon
367 -- but I thought that was silly so I've undone it
368 -- If it can't be for some reason, it should be a AlgTyCon
369 isTupleTyCon (TupleTyCon {}) = True
370 isTupleTyCon other = False
372 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
373 isUnboxedTupleTyCon other = False
375 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
376 isBoxedTupleTyCon other = False
378 tupleTyConBoxity tc = tyConBoxed tc
380 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
381 isRecursiveTyCon other = False
385 tyConDataCons :: TyCon -> [DataCon]
386 tyConDataCons tycon = ASSERT2( not (null cons), ppr tycon ) cons
388 cons = tyConDataConsIfAvailable tycon
390 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
391 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
392 tyConDataConsIfAvailable other = []
393 -- You may think this last equation should fail,
394 -- but it's quite convenient to return no constructors for
395 -- a synonym; see for example the call in TcTyClsDecls.
397 tyConFamilySize :: TyCon -> Int
398 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
399 tyConFamilySize (TupleTyCon {}) = 1
401 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
404 tyConPrimRep :: TyCon -> PrimRep
405 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
406 tyConPrimRep _ = PtrRep
410 tyConTheta :: TyCon -> ClassContext
411 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
412 -- should ask about anything else
415 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
416 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
417 actually computed (in another file).
420 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
422 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
423 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
424 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
425 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
426 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
427 tyConArgVrcs_maybe _ = Nothing
431 getSynTyConDefn :: TyCon -> ([TyVar], Type)
432 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
436 maybeTyConSingleCon :: TyCon -> Maybe DataCon
437 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
438 maybeTyConSingleCon (AlgTyCon {}) = Nothing
439 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
440 maybeTyConSingleCon (PrimTyCon {}) = Nothing
441 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
442 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
447 isClassTyCon :: TyCon -> Bool
448 isClassTyCon (AlgTyCon {algTyConClass = is_class_tycon}) = is_class_tycon
449 isClassTyCon other_tycon = False
453 %************************************************************************
455 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
457 %************************************************************************
459 @TyCon@s are compared by comparing their @Unique@s.
461 The strictness analyser needs @Ord@. It is a lexicographic order with
462 the property @(a<=b) || (b<=a)@.
465 instance Eq TyCon where
466 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
467 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
469 instance Ord TyCon where
470 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
471 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
472 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
473 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
474 compare a b = getUnique a `compare` getUnique b
476 instance Uniquable TyCon where
477 getUnique tc = tyConUnique tc
479 instance Outputable TyCon where
480 ppr tc = ppr (getName tc)
482 instance NamedThing TyCon where
487 %************************************************************************
489 \subsection{Kind constructors}
491 %************************************************************************
493 @matchesTyCon tc1 tc2@ checks whether an appliation
494 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
495 except that at the kind level tc2 might have more boxity info than tc1.
498 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
499 -> TyCon -- Inferred (e.g. type of actual arg to function)
502 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
504 uniq1 = tyConUnique tc1
505 uniq2 = tyConUnique tc2