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 mkForeignTyCon, isForeignTyCon,
18 mkAlgTyCon, --mkAlgTyCon,
34 tyConDataCons, tyConDataConsIfAvailable, tyConFamilySize,
39 isClassTyCon, tyConClass_maybe,
47 tyConGenIds, tyConGenInfo
50 #include "HsVersions.h"
52 import {-# SOURCE #-} TypeRep ( Type, PredType, Kind, SuperKind )
53 -- Should just be Type(Type), but this fails due to bug present up to
54 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
56 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
59 import Var ( TyVar, Id )
60 import Class ( Class )
61 import BasicTypes ( Arity, RecFlag(..), Boxity(..),
63 import Name ( Name, nameUnique, NamedThing(getName) )
64 import PrelNames ( Unique, Uniquable(..), anyBoxConKey )
65 import PrimRep ( PrimRep(..), isFollowableRep )
69 %************************************************************************
71 \subsection{The data type}
73 %************************************************************************
77 type SuperKindCon = TyCon
81 tyConUnique :: Unique,
88 | AlgTyCon { -- Tuples, data type, and newtype decls.
89 -- All lifted, all boxed
90 tyConUnique :: Unique,
95 tyConTyVars :: [TyVar],
96 tyConArgVrcs :: ArgVrcs,
97 algTyConTheta :: [PredType],
99 dataCons :: [DataCon],
100 -- Its data constructors, with fully polymorphic types
101 -- This list can be empty, when we import a data type abstractly,
102 -- either (a) the interface is hand-written and doesn't give
103 -- the constructors, or
104 -- (b) in a quest for fast compilation we don't import
107 selIds :: [Id], -- Its record selectors (if any)
109 noOfDataCons :: Int, -- Number of data constructors
110 -- Usually this is the same as the length of the
111 -- dataCons field, but the latter may be empty if
112 -- we imported the type abstractly. But even if we import
113 -- abstractly we still need to know the number of constructors
114 -- so we can get the return convention right. Tiresome!
116 algTyConFlavour :: AlgTyConFlavour,
117 algTyConRec :: RecFlag, -- Tells whether the data type is part of
118 -- a mutually-recursive group or not
120 genInfo :: Maybe (EP Id), -- Convert T <-> Tring
121 -- Some TyCons don't have it;
122 -- e.g. the TyCon for a Class dictionary,
123 -- and TyCons with unboxed arguments
125 algTyConClass :: Maybe Class -- Just cl if this tycon came from a class declaration
128 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
129 -- Now includes foreign-imported types
130 tyConUnique :: Unique,
134 tyConArgVrcs :: ArgVrcs,
135 primTyConRep :: PrimRep, -- Many primitive tycons are unboxed, but some are
136 -- boxed (represented by pointers). The PrimRep tells.
138 isUnLifted :: Bool -- Most primitive tycons are unlifted,
139 -- but foreign-imported ones may not be
144 tyConUnique :: Unique,
148 tyConBoxed :: Boxity,
149 tyConTyVars :: [TyVar],
151 genInfo :: Maybe (EP Id) -- Generic type and conv funs
155 tyConUnique :: Unique,
160 tyConTyVars :: [TyVar], -- Bound tyvars
161 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
162 -- Acts as a template for the expansion when
163 -- the tycon is applied to some types.
164 tyConArgVrcs :: ArgVrcs
167 | KindCon { -- Type constructor at the kind level
168 tyConUnique :: Unique,
170 tyConKind :: SuperKind,
174 | SuperKindCon { -- The type of kind variables or boxity variables,
175 tyConUnique :: Unique,
179 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
182 = DataTyCon -- Data type
183 | EnumTyCon -- Special sort of enumeration type
184 | NewTyCon Type -- Newtype, with its *ultimate* representation type
185 -- By 'ultimate' I mean that the rep type is not itself
186 -- a newtype or type synonym.
188 -- The rep type has explicit for-alls for the tyvars of
190 -- newtype T a = MkT [(a,Int)]
191 -- The rep type is forall a. [(a,Int)]
193 -- The rep type isn't entirely simple:
194 -- for a recursive newtype we pick () as the rep type
198 %************************************************************************
200 \subsection{TyCon Construction}
202 %************************************************************************
204 Note: the TyCon constructors all take a Kind as one argument, even though
205 they could, in principle, work out their Kind from their other arguments.
206 But to do so they need functions from Types, and that makes a nasty
207 module mutual-recursion. And they aren't called from many places.
208 So we compromise, and move their Kind calculation to the call site.
211 mkSuperKindCon :: Name -> SuperKindCon
212 mkSuperKindCon name = SuperKindCon {
213 tyConUnique = nameUnique name,
217 mkKindCon :: Name -> SuperKind -> KindCon
220 tyConUnique = nameUnique name,
226 mkFunTyCon :: Name -> Kind -> TyCon
229 tyConUnique = nameUnique name,
235 tyConGenInfo :: TyCon -> Maybe (EP Id)
236 tyConGenInfo (AlgTyCon { genInfo = info }) = info
237 tyConGenInfo (TupleTyCon { genInfo = info }) = info
238 tyConGenInfo other = Nothing
240 tyConGenIds :: TyCon -> [Id]
241 -- Returns the generic-programming Ids; these Ids need bindings
242 tyConGenIds tycon = case tyConGenInfo tycon of
244 Just (EP from to) -> [from,to]
246 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
247 -- but now you also have to pass in the generic information about the type
248 -- constructor - you can get hold of it easily (see Generics module)
249 mkAlgTyCon name kind tyvars theta argvrcs cons ncons sels flavour rec
253 tyConUnique = nameUnique name,
255 tyConArity = length tyvars,
256 tyConTyVars = tyvars,
257 tyConArgVrcs = argvrcs,
258 algTyConTheta = theta,
261 noOfDataCons = ncons,
262 algTyConClass = Nothing,
263 algTyConFlavour = flavour,
268 mkClassTyCon name kind tyvars argvrcs con clas flavour
271 tyConUnique = nameUnique name,
273 tyConArity = length tyvars,
274 tyConTyVars = tyvars,
275 tyConArgVrcs = argvrcs,
280 algTyConClass = Just clas,
281 algTyConFlavour = flavour,
282 algTyConRec = NonRecursive,
287 mkTupleTyCon name kind arity tyvars con boxed gen_info
289 tyConUnique = nameUnique name,
294 tyConTyVars = tyvars,
299 -- Foreign-imported (.NET) type constructors are represented
300 -- as primitive, but *lifted*, TyCons for now.
302 mkForeignTyCon name kind arity arg_vrcs
305 tyConUnique = nameUnique name,
308 tyConArgVrcs = arg_vrcs,
309 primTyConRep = PtrRep,
314 mkPrimTyCon name kind arity arg_vrcs rep
317 tyConUnique = nameUnique name,
320 tyConArgVrcs = arg_vrcs,
325 mkSynTyCon name kind arity tyvars rhs argvrcs
328 tyConUnique = nameUnique name,
331 tyConTyVars = tyvars,
333 tyConArgVrcs = argvrcs
336 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
341 isFunTyCon (FunTyCon {}) = True
344 isPrimTyCon (PrimTyCon {}) = True
345 isPrimTyCon _ = False
347 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
348 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
349 isUnLiftedTyCon _ = False
351 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
352 isBoxedTyCon (AlgTyCon {}) = True
353 isBoxedTyCon (FunTyCon {}) = True
354 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
355 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
357 -- isAlgTyCon returns True for both @data@ and @newtype@
358 isAlgTyCon (AlgTyCon {}) = True
359 isAlgTyCon (TupleTyCon {}) = True
360 isAlgTyCon other = False
362 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
363 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data}) = case new_or_data of
366 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
367 isDataTyCon other = False
369 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
370 isNewTyCon other = False
372 newTyConRep (AlgTyCon {algTyConFlavour = NewTyCon rep}) = Just rep
373 newTyConRep other = Nothing
376 -- has *one* constructor,
377 -- is *not* existential
379 -- may be DataType or NewType,
380 -- may be unboxed or not,
381 -- may be recursive or not
382 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
383 isProductTyCon (TupleTyCon {}) = True
384 isProductTyCon other = False
386 isSynTyCon (SynTyCon {}) = True
389 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
390 isEnumerationTyCon other = False
392 -- The unit tycon didn't used to be classed as a tuple tycon
393 -- but I thought that was silly so I've undone it
394 -- If it can't be for some reason, it should be a AlgTyCon
395 isTupleTyCon (TupleTyCon {}) = True
396 isTupleTyCon other = False
398 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
399 isUnboxedTupleTyCon other = False
401 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
402 isBoxedTupleTyCon other = False
404 tupleTyConBoxity tc = tyConBoxed tc
406 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
407 isRecursiveTyCon other = False
409 -- isForeignTyCon identifies foreign-imported type constructors
410 -- For the moment, they are primitive but lifted, but that may change
411 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
412 isForeignTyCon other = False
416 tyConDataCons :: TyCon -> [DataCon]
417 tyConDataCons tycon = ASSERT2( not (null cons), ppr tycon )
418 ASSERT2( length cons == tyConFamilySize tycon, ppr tycon )
421 cons = tyConDataConsIfAvailable tycon
423 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
424 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
425 tyConDataConsIfAvailable other = []
426 -- You may think this last equation should fail,
427 -- but it's quite convenient to return no constructors for
428 -- a synonym; see for example the call in TcTyClsDecls.
430 tyConFamilySize :: TyCon -> Int
431 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
432 tyConFamilySize (TupleTyCon {}) = 1
434 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
437 tyConSelIds :: TyCon -> [Id]
438 tyConSelIds (AlgTyCon {selIds = sels}) = sels
439 tyConSelIds other_tycon = []
443 tyConPrimRep :: TyCon -> PrimRep
444 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
445 tyConPrimRep _ = PtrRep
449 tyConTheta :: TyCon -> [PredType]
450 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
451 -- should ask about anything else
454 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
455 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
456 actually computed (in another file).
459 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
461 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
462 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
463 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
464 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
465 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
466 tyConArgVrcs_maybe _ = Nothing
470 getSynTyConDefn :: TyCon -> ([TyVar], Type)
471 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
475 maybeTyConSingleCon :: TyCon -> Maybe DataCon
476 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
477 maybeTyConSingleCon (AlgTyCon {}) = Nothing
478 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
479 maybeTyConSingleCon (PrimTyCon {}) = Nothing
480 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
481 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
486 isClassTyCon :: TyCon -> Bool
487 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
488 isClassTyCon other_tycon = False
490 tyConClass_maybe :: TyCon -> Maybe Class
491 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
492 tyConClass_maybe ther_tycon = Nothing
496 %************************************************************************
498 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
500 %************************************************************************
502 @TyCon@s are compared by comparing their @Unique@s.
504 The strictness analyser needs @Ord@. It is a lexicographic order with
505 the property @(a<=b) || (b<=a)@.
508 instance Eq TyCon where
509 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
510 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
512 instance Ord TyCon where
513 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
514 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
515 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
516 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
517 compare a b = getUnique a `compare` getUnique b
519 instance Uniquable TyCon where
520 getUnique tc = tyConUnique tc
522 instance Outputable TyCon where
523 ppr tc = ppr (getName tc)
525 instance NamedThing TyCon where
530 %************************************************************************
532 \subsection{Kind constructors}
534 %************************************************************************
536 @matchesTyCon tc1 tc2@ checks whether an appliation
537 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
538 except that at the kind level tc2 might have more boxity info than tc1.
541 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
542 -> TyCon -- Inferred (e.g. type of actual arg to function)
545 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
547 uniq1 = tyConUnique tc1
548 uniq2 = tyConUnique tc2