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,
35 tyConDataCons, tyConDataConsIfAvailable, tyConFamilySize,
40 isClassTyCon, tyConClass_maybe,
48 tyConGenIds, tyConGenInfo
51 #include "HsVersions.h"
53 import {-# SOURCE #-} TypeRep ( Type, PredType, Kind, SuperKind )
54 -- Should just be Type(Type), but this fails due to bug present up to
55 -- and including 4.02 involving slurping of hi-boot files. Bug is now fixed.
57 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
60 import Var ( TyVar, Id )
61 import Class ( Class )
62 import BasicTypes ( Arity, RecFlag(..), Boxity(..),
64 import Name ( Name, nameUnique, NamedThing(getName) )
65 import PrelNames ( Unique, Uniquable(..), anyBoxConKey )
66 import PrimRep ( PrimRep(..), isFollowableRep )
67 import Util ( lengthIs )
72 %************************************************************************
74 \subsection{The data type}
76 %************************************************************************
80 type SuperKindCon = TyCon
84 tyConUnique :: Unique,
91 | AlgTyCon { -- Tuples, data type, and newtype decls.
92 -- All lifted, all boxed
93 tyConUnique :: Unique,
98 tyConTyVars :: [TyVar],
99 tyConArgVrcs :: ArgVrcs,
100 algTyConTheta :: [PredType],
102 dataCons :: [DataCon],
103 -- Its data constructors, with fully polymorphic types
104 -- This list can be empty, when we import a data type
105 -- abstractly, either
106 -- (a) the interface is hand-written and doesn't give
107 -- the constructors, or
108 -- (b) in a quest for fast compilation we don't import
111 selIds :: [Id], -- Its record selectors (if any)
114 -- Number of data constructors. Usually this is the
115 -- same as the length of the dataCons field, but the
116 -- latter may be empty if we imported the type
117 -- abstractly. But even if we import abstractly we
118 -- still need to know the number of constructors so we
119 -- can get the return convention right. Tiresome!
121 algTyConFlavour :: AlgTyConFlavour,
122 algTyConRec :: RecFlag, -- Tells whether the data type is part of
123 -- a mutually-recursive group or not
125 genInfo :: Maybe (EP Id), -- Convert T <-> Tring
126 -- Some TyCons don't have it;
127 -- e.g. the TyCon for a Class dictionary,
128 -- and TyCons with unboxed arguments
130 algTyConClass :: Maybe Class
131 -- Just cl if this tycon came from a class declaration
134 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
135 -- Now includes foreign-imported types
136 tyConUnique :: Unique,
140 tyConArgVrcs :: ArgVrcs,
141 primTyConRep :: PrimRep, -- Many primitive tycons are unboxed, but some are
142 -- boxed (represented by pointers). The PrimRep tells.
144 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
145 -- but foreign-imported ones may not be
146 tyConExtName :: Maybe FastString
151 tyConUnique :: Unique,
155 tyConBoxed :: Boxity,
156 tyConTyVars :: [TyVar],
158 genInfo :: Maybe (EP Id) -- Generic type and conv funs
162 tyConUnique :: Unique,
167 tyConTyVars :: [TyVar], -- Bound tyvars
168 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
169 -- Acts as a template for the expansion when
170 -- the tycon is applied to some types.
171 tyConArgVrcs :: ArgVrcs
174 | KindCon { -- Type constructor at the kind level
175 tyConUnique :: Unique,
177 tyConKind :: SuperKind,
181 | SuperKindCon { -- The type of kind variables or boxity variables,
182 tyConUnique :: Unique,
186 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
189 = DataTyCon -- Data type
191 | EnumTyCon -- Special sort of enumeration type
193 | NewTyCon Type -- Newtype, with its *ultimate* representation type
194 -- By 'ultimate' I mean that the rep type is not itself
195 -- a newtype or type synonym.
197 -- The rep type has free type variables the tyConTyVars
199 -- newtype T a = MkT [(a,Int)]
200 -- The rep type is [(a,Int)]
202 -- The rep type isn't entirely simple:
203 -- for a recursive newtype we pick () as the rep type
207 %************************************************************************
209 \subsection{TyCon Construction}
211 %************************************************************************
213 Note: the TyCon constructors all take a Kind as one argument, even though
214 they could, in principle, work out their Kind from their other arguments.
215 But to do so they need functions from Types, and that makes a nasty
216 module mutual-recursion. And they aren't called from many places.
217 So we compromise, and move their Kind calculation to the call site.
220 mkSuperKindCon :: Name -> SuperKindCon
221 mkSuperKindCon name = SuperKindCon {
222 tyConUnique = nameUnique name,
226 mkKindCon :: Name -> SuperKind -> KindCon
229 tyConUnique = nameUnique name,
235 mkFunTyCon :: Name -> Kind -> TyCon
238 tyConUnique = nameUnique name,
244 tyConGenInfo :: TyCon -> Maybe (EP Id)
245 tyConGenInfo (AlgTyCon { genInfo = info }) = info
246 tyConGenInfo (TupleTyCon { genInfo = info }) = info
247 tyConGenInfo other = Nothing
249 tyConGenIds :: TyCon -> [Id]
250 -- Returns the generic-programming Ids; these Ids need bindings
251 tyConGenIds tycon = case tyConGenInfo tycon of
253 Just (EP from to) -> [from,to]
255 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
256 -- but now you also have to pass in the generic information about the type
257 -- constructor - you can get hold of it easily (see Generics module)
258 mkAlgTyCon name kind tyvars theta argvrcs cons ncons sels flavour rec
262 tyConUnique = nameUnique name,
264 tyConArity = length tyvars,
265 tyConTyVars = tyvars,
266 tyConArgVrcs = argvrcs,
267 algTyConTheta = theta,
270 noOfDataCons = ncons,
271 algTyConClass = Nothing,
272 algTyConFlavour = flavour,
277 mkClassTyCon name kind tyvars argvrcs con clas flavour rec
280 tyConUnique = nameUnique name,
282 tyConArity = length tyvars,
283 tyConTyVars = tyvars,
284 tyConArgVrcs = argvrcs,
289 algTyConClass = Just clas,
290 algTyConFlavour = flavour,
296 mkTupleTyCon name kind arity tyvars con boxed gen_info
298 tyConUnique = nameUnique name,
303 tyConTyVars = tyvars,
308 -- Foreign-imported (.NET) type constructors are represented
309 -- as primitive, but *lifted*, TyCons for now. They are lifted
310 -- because the Haskell type T representing the (foreign) .NET
311 -- type T is actually implemented (in ILX) as a thunk<T>
313 mkForeignTyCon name ext_name kind arity arg_vrcs
316 tyConUnique = nameUnique name,
319 tyConArgVrcs = arg_vrcs,
320 primTyConRep = PtrRep,
322 tyConExtName = ext_name
326 -- most Prim tycons are lifted
327 mkPrimTyCon name kind arity arg_vrcs rep
328 = mkPrimTyCon' name kind arity arg_vrcs rep True
330 -- but RealWorld is lifted
331 mkLiftedPrimTyCon name kind arity arg_vrcs rep
332 = mkPrimTyCon' name kind arity arg_vrcs rep False
334 mkPrimTyCon' name kind arity arg_vrcs rep is_unlifted
337 tyConUnique = nameUnique name,
340 tyConArgVrcs = arg_vrcs,
342 isUnLifted = is_unlifted,
343 tyConExtName = Nothing
346 mkSynTyCon name kind arity tyvars rhs argvrcs
349 tyConUnique = nameUnique name,
352 tyConTyVars = tyvars,
354 tyConArgVrcs = argvrcs
357 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
362 isFunTyCon (FunTyCon {}) = True
365 isPrimTyCon (PrimTyCon {}) = True
366 isPrimTyCon _ = False
368 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
369 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
370 isUnLiftedTyCon _ = False
372 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
373 isBoxedTyCon (AlgTyCon {}) = True
374 isBoxedTyCon (FunTyCon {}) = True
375 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
376 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
378 -- isAlgTyCon returns True for both @data@ and @newtype@
379 isAlgTyCon (AlgTyCon {}) = True
380 isAlgTyCon (TupleTyCon {}) = True
381 isAlgTyCon other = False
383 -- isDataTyCon returns True for data types that are represented by
384 -- heap-allocated constructors.
385 -- These are srcutinised by Core-level @case@ expressions, and they
386 -- get info tables allocated for them.
387 -- True for all @data@ types
388 -- False for newtypes
390 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data, algTyConRec = is_rec})
391 = case new_or_data of
395 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
396 isDataTyCon other = False
398 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
399 isNewTyCon other = False
401 newTyConRep :: TyCon -> ([TyVar], Type)
402 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTyConFlavour = NewTyCon rep}) = (tvs, rep)
405 -- has *one* constructor,
406 -- is *not* existential
408 -- may be DataType or NewType,
409 -- may be unboxed or not,
410 -- may be recursive or not
411 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
412 isProductTyCon (TupleTyCon {}) = True
413 isProductTyCon other = False
415 isSynTyCon (SynTyCon {}) = True
418 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
419 isEnumerationTyCon other = False
421 -- The unit tycon didn't used to be classed as a tuple tycon
422 -- but I thought that was silly so I've undone it
423 -- If it can't be for some reason, it should be a AlgTyCon
424 isTupleTyCon (TupleTyCon {}) = True
425 isTupleTyCon other = False
427 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
428 isUnboxedTupleTyCon other = False
430 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
431 isBoxedTupleTyCon other = False
433 tupleTyConBoxity tc = tyConBoxed tc
435 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
436 isRecursiveTyCon other = False
438 -- isForeignTyCon identifies foreign-imported type constructors
439 -- For the moment, they are primitive but lifted, but that may change
440 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
441 isForeignTyCon other = False
445 tyConDataCons :: TyCon -> [DataCon]
446 tyConDataCons tycon = ASSERT2( cons `lengthIs` (tyConFamilySize tycon), ppr tycon )
449 cons = tyConDataConsIfAvailable tycon
451 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons -- Empty for abstract types
452 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
453 tyConDataConsIfAvailable other = []
454 -- You may think this last equation should fail,
455 -- but it's quite convenient to return no constructors for
456 -- a synonym; see for example the call in TcTyClsDecls.
458 tyConFamilySize :: TyCon -> Int
459 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
460 tyConFamilySize (TupleTyCon {}) = 1
462 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
465 tyConSelIds :: TyCon -> [Id]
466 tyConSelIds (AlgTyCon {selIds = sels}) = sels
467 tyConSelIds other_tycon = []
471 tyConPrimRep :: TyCon -> PrimRep
472 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
473 tyConPrimRep tc = ASSERT( not (isUnboxedTupleTyCon tc) )
475 -- We should not be asking what the representation of an
476 -- unboxed tuple is, because it isn't a first class value.
480 tyConTheta :: TyCon -> [PredType]
481 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
482 tyConTheta (TupleTyCon {}) = []
483 -- shouldn't ask about anything else
486 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
487 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
488 actually computed (in another file).
491 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
493 tyConArgVrcs_maybe (FunTyCon {} ) = Just [(False,True),(True,False)]
494 tyConArgVrcs_maybe (AlgTyCon {tyConArgVrcs = oi}) = Just oi
495 tyConArgVrcs_maybe (PrimTyCon {tyConArgVrcs = oi}) = Just oi
496 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity }) = Just (replicate arity (True,False))
497 tyConArgVrcs_maybe (SynTyCon {tyConArgVrcs = oi }) = Just oi
498 tyConArgVrcs_maybe _ = Nothing
502 getSynTyConDefn :: TyCon -> ([TyVar], Type)
503 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
507 maybeTyConSingleCon :: TyCon -> Maybe DataCon
508 maybeTyConSingleCon (AlgTyCon {dataCons = [c]}) = Just c
509 maybeTyConSingleCon (AlgTyCon {}) = Nothing
510 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
511 maybeTyConSingleCon (PrimTyCon {}) = Nothing
512 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
513 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
518 isClassTyCon :: TyCon -> Bool
519 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
520 isClassTyCon other_tycon = False
522 tyConClass_maybe :: TyCon -> Maybe Class
523 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
524 tyConClass_maybe ther_tycon = Nothing
528 %************************************************************************
530 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
532 %************************************************************************
534 @TyCon@s are compared by comparing their @Unique@s.
536 The strictness analyser needs @Ord@. It is a lexicographic order with
537 the property @(a<=b) || (b<=a)@.
540 instance Eq TyCon where
541 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
542 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
544 instance Ord TyCon where
545 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
546 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
547 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
548 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
549 compare a b = getUnique a `compare` getUnique b
551 instance Uniquable TyCon where
552 getUnique tc = tyConUnique tc
554 instance Outputable TyCon where
555 ppr tc = ppr (getName tc)
557 instance NamedThing TyCon where
562 %************************************************************************
564 \subsection{Kind constructors}
566 %************************************************************************
568 @matchesTyCon tc1 tc2@ checks whether an appliation
569 (tc1 t1..tn) matches (tc2 t1..tn). By "matches" we basically mean "equals",
570 except that at the kind level tc2 might have more boxity info than tc1.
573 matchesTyCon :: TyCon -- Expected (e.g. arg type of function)
574 -> TyCon -- Inferred (e.g. type of actual arg to function)
577 matchesTyCon tc1 tc2 = uniq1 == uniq2 || uniq1 == anyBoxConKey
579 uniq1 = tyConUnique tc1
580 uniq2 = tyConUnique tc2