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 mkAlgTyConRep, --mkAlgTyCon,
32 tyConDataCons, tyConDataConsIfAvailable,
46 tyConGenIds, tyConGenInfo
49 #include "HsVersions.h"
51 import {-# SOURCE #-} TypeRep ( Type, Kind, SuperKind )
52 -- Should just be Type(Type), but this fails due to bug present up to
53 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
55 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
58 import Class ( Class, ClassContext )
59 import Var ( TyVar, Id )
60 import BasicTypes ( Arity, NewOrData(..), RecFlag(..), Boxity(..),
62 import Name ( Name, nameUnique, NamedThing(getName) )
63 import PrelNames ( Unique, Uniquable(..), anyBoxConKey )
64 import PrimRep ( PrimRep(..), isFollowableRep )
68 %************************************************************************
70 \subsection{The data type}
72 %************************************************************************
76 type SuperKindCon = TyCon
80 tyConUnique :: Unique,
87 | AlgTyCon { -- Tuples, data type, and newtype decls.
88 -- All lifted, all boxed
89 tyConUnique :: Unique,
94 tyConTyVars :: [TyVar],
95 tyConArgVrcs :: ArgVrcs,
96 algTyConTheta :: ClassContext,
98 dataCons :: [DataCon],
99 -- Its data constructors, with fully polymorphic types
100 -- This list can be empty, when we import a data type abstractly,
101 -- either (a) the interface is hand-written and doesn't give
102 -- the constructors, or
103 -- (b) in a quest for fast compilation we don't import
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 algTyConDerivings :: [Class], -- Classes which have derived instances
115 algTyConFlavour :: AlgTyConFlavour,
116 algTyConRec :: RecFlag, -- Tells whether the data type is part of
117 -- a mutually-recursive group or not
119 genInfo :: Maybe (EP Id), -- Convert T <-> Tring
120 -- Some TyCons don't have it;
121 -- e.g. the TyCon for a Class dictionary,
122 -- and TyCons with unboxed arguments
124 algTyConClass :: Bool -- True if this tycon comes from a class declaration
127 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
128 -- NB: All of these guys are *unlifted*, but not all are *unboxed*
129 tyConUnique :: Unique,
133 tyConArgVrcs :: ArgVrcs,
134 primTyConRep :: PrimRep
139 tyConUnique :: Unique,
143 tyConBoxed :: Boxity,
144 tyConTyVars :: [TyVar],
146 genInfo :: Maybe (EP Id) -- Generic type and conv funs
150 tyConUnique :: Unique,
155 tyConTyVars :: [TyVar], -- Bound tyvars
156 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
157 -- Acts as a template for the expansion when
158 -- the tycon is applied to some types.
159 tyConArgVrcs :: ArgVrcs
162 | KindCon { -- Type constructor at the kind level
163 tyConUnique :: Unique,
165 tyConKind :: SuperKind,
169 | SuperKindCon { -- The type of kind variables or boxity variables,
170 tyConUnique :: Unique,
174 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
175 -- *NB*: this is tyvar variance info, *not*
176 -- termvar usage info.
179 = DataTyCon -- Data type
180 | EnumTyCon -- Special sort of enumeration type
181 | NewTyCon Type -- Newtype, with its *ultimate* representation type
182 -- By 'ultimate' I mean that the rep type is not itself
183 -- a newtype or type synonym.
185 -- The rep type has explicit for-alls for the tyvars of
187 -- newtype T a = MkT [(a,Int)]
188 -- The rep type is forall a. [(a,Int)]
190 -- The rep type isn't entirely simple:
191 -- for a recursive newtype we pick () as the rep type
195 %************************************************************************
197 \subsection{TyCon Construction}
199 %************************************************************************
201 Note: the TyCon constructors all take a Kind as one argument, even though
202 they could, in principle, work out their Kind from their other arguments.
203 But to do so they need functions from Types, and that makes a nasty
204 module mutual-recursion. And they aren't called from many places.
205 So we compromise, and move their Kind calculation to the call site.
208 mkSuperKindCon :: Name -> SuperKindCon
209 mkSuperKindCon name = SuperKindCon {
210 tyConUnique = nameUnique name,
214 mkKindCon :: Name -> SuperKind -> KindCon
217 tyConUnique = nameUnique name,
223 mkFunTyCon :: Name -> Kind -> TyCon
226 tyConUnique = nameUnique name,
232 tyConGenInfo :: TyCon -> Maybe (EP Id)
233 tyConGenInfo (AlgTyCon { genInfo = info }) = info
234 tyConGenInfo (TupleTyCon { genInfo = info }) = info
235 tyConGenInfo other = Nothing
237 tyConGenIds :: TyCon -> [Id]
238 -- Returns the generic-programming Ids; these Ids need bindings
239 tyConGenIds tycon = case tyConGenInfo tycon of
241 Just (EP from to) -> [from,to]
243 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
244 -- but now you also have to pass in the generic information about the type
245 -- constructor - you can get hold of it easily (see Generics module)
246 mkAlgTyConRep name kind tyvars theta argvrcs cons ncons derivs flavour rec
250 tyConUnique = nameUnique name,
252 tyConArity = length tyvars,
253 tyConTyVars = tyvars,
254 tyConArgVrcs = argvrcs,
255 algTyConTheta = theta,
257 noOfDataCons = ncons,
258 algTyConDerivings = derivs,
259 algTyConClass = False,
260 algTyConFlavour = flavour,
265 mkClassTyCon name kind tyvars argvrcs con clas flavour
268 tyConUnique = nameUnique name,
270 tyConArity = length tyvars,
271 tyConTyVars = tyvars,
272 tyConArgVrcs = argvrcs,
276 algTyConDerivings = [],
277 algTyConClass = True,
278 algTyConFlavour = flavour,
279 algTyConRec = NonRecursive,
284 mkTupleTyCon name kind arity tyvars con boxed gen_info
286 tyConUnique = nameUnique name,
291 tyConTyVars = tyvars,
296 mkPrimTyCon name kind arity arg_vrcs rep
299 tyConUnique = nameUnique name,
302 tyConArgVrcs = arg_vrcs,
306 mkSynTyCon name kind arity tyvars rhs argvrcs
309 tyConUnique = nameUnique name,
312 tyConTyVars = tyvars,
314 tyConArgVrcs = argvrcs
317 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
322 isFunTyCon (FunTyCon {}) = True
325 isPrimTyCon (PrimTyCon {}) = True
326 isPrimTyCon _ = False
328 isUnLiftedTyCon (PrimTyCon {}) = True
329 isUnLiftedTyCon (TupleTyCon { tyConBoxed = boxity}) = not (isBoxed boxity)
330 isUnLiftedTyCon _ = False
332 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
333 isBoxedTyCon (AlgTyCon {}) = True
334 isBoxedTyCon (FunTyCon {}) = True
335 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
336 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
338 -- isAlgTyCon returns True for both @data@ and @newtype@
339 isAlgTyCon (AlgTyCon {}) = True
340 isAlgTyCon (TupleTyCon {}) = True
341 isAlgTyCon other = False
343 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
344 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
347 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
348 isDataTyCon other = False
350 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
351 isNewTyCon other = False
353 newTyConRep (AlgTyCon {algTyConFlavour = NewTyCon rep}) = Just rep
354 newTyConRep other = Nothing
357 -- has *one* constructor,
358 -- is *not* existential
360 -- may be DataType or NewType,
361 -- may be unboxed or not,
362 -- may be recursive or not
363 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
364 isProductTyCon (TupleTyCon {}) = True
365 isProductTyCon other = False
367 isSynTyCon (SynTyCon {}) = True
370 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
371 isEnumerationTyCon other = False
373 -- The unit tycon didn't used to be classed as a tuple tycon
374 -- but I thought that was silly so I've undone it
375 -- If it can't be for some reason, it should be a AlgTyCon
376 isTupleTyCon (TupleTyCon {}) = True
377 isTupleTyCon other = False
379 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
380 isUnboxedTupleTyCon other = False
382 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
383 isBoxedTupleTyCon other = False
385 tupleTyConBoxity tc = tyConBoxed tc
387 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
388 isRecursiveTyCon other = False
392 tyConDataCons :: TyCon -> [DataCon]
393 tyConDataCons tycon = ASSERT2( not (null cons), ppr tycon ) cons
395 cons = tyConDataConsIfAvailable tycon
397 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
398 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
399 tyConDataConsIfAvailable other = []
400 -- You may think this last equation should fail,
401 -- but it's quite convenient to return no constructors for
402 -- a synonym; see for example the call in TcTyClsDecls.
404 tyConFamilySize :: TyCon -> Int
405 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
406 tyConFamilySize (TupleTyCon {}) = 1
408 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
411 tyConPrimRep :: TyCon -> PrimRep
412 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
413 tyConPrimRep _ = PtrRep
417 tyConDerivings :: TyCon -> [Class]
418 tyConDerivings (AlgTyCon {algTyConDerivings = derivs}) = derivs
419 tyConDerivings other = []
423 tyConTheta :: TyCon -> ClassContext
424 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
425 -- should ask about anything else
428 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
429 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
430 actually computed (in another file).
433 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
435 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
436 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
437 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
438 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
439 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
440 tyConArgVrcs_maybe _ = Nothing
444 getSynTyConDefn :: TyCon -> ([TyVar], Type)
445 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
449 maybeTyConSingleCon :: TyCon -> Maybe DataCon
450 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
451 maybeTyConSingleCon (AlgTyCon {}) = Nothing
452 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
453 maybeTyConSingleCon (PrimTyCon {}) = Nothing
454 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
455 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
460 isClassTyCon :: TyCon -> Bool
461 isClassTyCon (AlgTyCon {algTyConClass = is_class_tycon}) = is_class_tycon
462 isClassTyCon other_tycon = False
466 %************************************************************************
468 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
470 %************************************************************************
472 @TyCon@s are compared by comparing their @Unique@s.
474 The strictness analyser needs @Ord@. It is a lexicographic order with
475 the property @(a<=b) || (b<=a)@.
478 instance Eq TyCon where
479 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
480 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
482 instance Ord TyCon where
483 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
484 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
485 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
486 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
487 compare a b = getUnique a `compare` getUnique b
489 instance Uniquable TyCon where
490 getUnique tc = tyConUnique tc
492 instance Outputable TyCon where
493 ppr tc = ppr (getName tc)
495 instance NamedThing TyCon where
500 %************************************************************************
502 \subsection{Kind constructors}
504 %************************************************************************
506 @matchesTyCon tc1 tc2@ checks whether an appliation
507 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
508 except that at the kind level tc2 might have more boxity info than tc1.
511 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
512 -> TyCon -- Inferred (e.g. type of actual arg to function)
515 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
517 uniq1 = tyConUnique tc1
518 uniq2 = tyConUnique tc2