2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
10 AlgTyConRhs(..), visibleDataCons,
12 isFunTyCon, isUnLiftedTyCon, isProductTyCon,
13 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
15 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
16 isRecursiveTyCon, newTyConRep, newTyConRhs, isHiBootTyCon,
18 mkForeignTyCon, isForeignTyCon,
33 algTyConRhs, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
38 isClassTyCon, tyConClass_maybe,
40 tyConExtName, -- External name for foreign types
48 #include "HsVersions.h"
50 import {-# SOURCE #-} TypeRep ( Type, PredType )
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 Var ( TyVar, Id )
58 import Class ( Class )
60 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
61 import Name ( Name, nameUnique, NamedThing(getName) )
62 import PrelNames ( Unique, Uniquable(..) )
63 import PrimRep ( PrimRep(..) )
64 import Maybes ( orElse )
69 %************************************************************************
71 \subsection{The data type}
73 %************************************************************************
78 tyConUnique :: Unique,
85 | AlgTyCon { -- Data type, and newtype decls.
86 -- All lifted, all boxed
87 tyConUnique :: Unique,
92 tyConTyVars :: [TyVar],
94 algTyConTheta :: [PredType],
96 selIds :: [Id], -- Its record selectors (if any)
98 algTyConRhs :: AlgTyConRhs, -- Data constructors in here
100 algTyConRec :: RecFlag, -- Tells whether the data type is part of
101 -- a mutually-recursive group or not
103 hasGenerics :: Bool, -- True <=> generic to/from functions are available
104 -- (in the exports of the data type's source module)
106 algTyConClass :: Maybe Class
107 -- Just cl if this tycon came from a class declaration
110 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
111 -- Now includes foreign-imported types
112 tyConUnique :: Unique,
117 primTyConRep :: PrimRep, -- Many primitive tycons are unboxed, but some are
118 -- boxed (represented by pointers). The PrimRep tells.
120 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
121 -- but foreign-imported ones may not be
122 tyConExtName :: Maybe FastString -- Just xx for foreign-imported types
127 tyConUnique :: Unique,
131 tyConBoxed :: Boxity,
132 tyConTyVars :: [TyVar],
138 tyConUnique :: Unique,
143 tyConTyVars :: [TyVar], -- Bound tyvars
144 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
145 -- Acts as a template for the expansion when
146 -- the tycon is applied to some types.
150 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
151 -- [] means "no information, assume the worst"
154 = AbstractTyCon -- We know nothing about this data type, except
155 -- that it's represented by a pointer
156 -- Used when we export a data type abstractly into
160 [DataCon] -- The constructors; can be empty if the user declares
161 -- the type to have no constructors
162 Bool -- Cached: True <=> an enumeration type
164 | NewTyCon -- Newtypes always have exactly one constructor
165 DataCon -- The unique constructor; it has no existentials
166 Type -- Cached: the argument type of the constructor
167 -- = the representation type of the tycon
169 Type -- Cached: the *ultimate* representation type
170 -- By 'ultimate' I mean that the rep type is not itself
171 -- a newtype or type synonym.
172 -- The rep type isn't entirely simple:
173 -- for a recursive newtype we pick () as the rep type
176 -- The rep type has free type variables the tyConTyVars
178 -- newtype T a = MkT [(a,Int)]
179 -- The rep type is [(a,Int)]
180 -- NB: the rep type isn't necessarily the original RHS of the
181 -- newtype decl, because the rep type looks through other
184 visibleDataCons :: AlgTyConRhs -> [DataCon]
185 visibleDataCons AbstractTyCon = []
186 visibleDataCons (DataTyCon cs _) = cs
187 visibleDataCons (NewTyCon c _ _) = [c]
191 %************************************************************************
193 \subsection{TyCon Construction}
195 %************************************************************************
197 Note: the TyCon constructors all take a Kind as one argument, even though
198 they could, in principle, work out their Kind from their other arguments.
199 But to do so they need functions from Types, and that makes a nasty
200 module mutual-recursion. And they aren't called from many places.
201 So we compromise, and move their Kind calculation to the call site.
204 mkFunTyCon :: Name -> Kind -> TyCon
207 tyConUnique = nameUnique name,
213 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
214 -- but now you also have to pass in the generic information about the type
215 -- constructor - you can get hold of it easily (see Generics module)
216 mkAlgTyCon name kind tyvars theta argvrcs rhs sels is_rec gen_info
219 tyConUnique = nameUnique name,
221 tyConArity = length tyvars,
222 tyConTyVars = tyvars,
224 algTyConTheta = theta,
227 algTyConClass = Nothing,
228 algTyConRec = is_rec,
229 hasGenerics = gen_info
232 mkClassTyCon name kind tyvars argvrcs rhs clas is_rec
235 tyConUnique = nameUnique name,
237 tyConArity = length tyvars,
238 tyConTyVars = tyvars,
243 algTyConClass = Just clas,
244 algTyConRec = is_rec,
249 mkTupleTyCon name kind arity tyvars con boxed gen_info
251 tyConUnique = nameUnique name,
256 tyConTyVars = tyvars,
258 hasGenerics = gen_info
261 -- Foreign-imported (.NET) type constructors are represented
262 -- as primitive, but *lifted*, TyCons for now. They are lifted
263 -- because the Haskell type T representing the (foreign) .NET
264 -- type T is actually implemented (in ILX) as a thunk<T>
266 mkForeignTyCon name ext_name kind arity arg_vrcs
269 tyConUnique = nameUnique name,
273 primTyConRep = PtrRep,
275 tyConExtName = ext_name
279 -- most Prim tycons are lifted
280 mkPrimTyCon name kind arity arg_vrcs rep
281 = mkPrimTyCon' name kind arity arg_vrcs rep True
283 -- but RealWorld is lifted
284 mkLiftedPrimTyCon name kind arity arg_vrcs rep
285 = mkPrimTyCon' name kind arity arg_vrcs rep False
287 mkPrimTyCon' name kind arity arg_vrcs rep is_unlifted
290 tyConUnique = nameUnique name,
295 isUnLifted = is_unlifted,
296 tyConExtName = Nothing
299 mkSynTyCon name kind tyvars rhs argvrcs
302 tyConUnique = nameUnique name,
304 tyConArity = length tyvars,
305 tyConTyVars = tyvars,
312 isFunTyCon :: TyCon -> Bool
313 isFunTyCon (FunTyCon {}) = True
316 isPrimTyCon :: TyCon -> Bool
317 isPrimTyCon (PrimTyCon {}) = True
318 isPrimTyCon _ = False
320 isUnLiftedTyCon :: TyCon -> Bool
321 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
322 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
323 isUnLiftedTyCon _ = False
325 -- isAlgTyCon returns True for both @data@ and @newtype@
326 isAlgTyCon :: TyCon -> Bool
327 isAlgTyCon (AlgTyCon {}) = True
328 isAlgTyCon (TupleTyCon {}) = True
329 isAlgTyCon other = False
331 isDataTyCon :: TyCon -> Bool
332 -- isDataTyCon returns True for data types that are represented by
333 -- heap-allocated constructors.
334 -- These are srcutinised by Core-level @case@ expressions, and they
335 -- get info tables allocated for them.
336 -- True for all @data@ types
337 -- False for newtypes
339 isDataTyCon (AlgTyCon {algTyConRhs = rhs})
341 DataTyCon _ _ -> True
342 NewTyCon _ _ _ -> False
343 AbstractTyCon -> panic "isDataTyCon"
345 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
346 isDataTyCon other = False
348 isNewTyCon :: TyCon -> Bool
349 isNewTyCon (AlgTyCon {algTyConRhs = NewTyCon _ _ _}) = True
350 isNewTyCon other = False
352 isProductTyCon :: TyCon -> Bool
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 tc@(AlgTyCon {}) = case algTyConRhs tc of
361 DataTyCon [data_con] _ -> not (isExistentialDataCon data_con)
362 NewTyCon _ _ _ -> True
364 isProductTyCon (TupleTyCon {}) = True
365 isProductTyCon other = False
367 isSynTyCon :: TyCon -> Bool
368 isSynTyCon (SynTyCon {}) = True
371 isEnumerationTyCon :: TyCon -> Bool
372 isEnumerationTyCon (AlgTyCon {algTyConRhs = DataTyCon _ is_enum}) = is_enum
373 isEnumerationTyCon other = False
375 isTupleTyCon :: TyCon -> Bool
376 -- The unit tycon didn't used to be classed as a tuple tycon
377 -- but I thought that was silly so I've undone it
378 -- If it can't be for some reason, it should be a AlgTyCon
379 isTupleTyCon (TupleTyCon {}) = True
380 isTupleTyCon other = False
382 isUnboxedTupleTyCon :: TyCon -> Bool
383 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
384 isUnboxedTupleTyCon other = False
386 isBoxedTupleTyCon :: TyCon -> Bool
387 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
388 isBoxedTupleTyCon other = False
390 tupleTyConBoxity tc = tyConBoxed tc
392 isRecursiveTyCon :: TyCon -> Bool
393 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
394 isRecursiveTyCon other = False
396 isHiBootTyCon :: TyCon -> Bool
397 -- Used for knot-tying in hi-boot files
398 isHiBootTyCon (AlgTyCon {algTyConRhs = AbstractTyCon}) = True
399 isHiBootTyCon other = False
401 isForeignTyCon :: TyCon -> Bool
402 -- isForeignTyCon identifies foreign-imported type constructors
403 -- For the moment, they are primitive but lifted, but that may change
404 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
405 isForeignTyCon other = False
409 tyConHasGenerics :: TyCon -> Bool
410 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
411 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
412 tyConHasGenerics other = False -- Synonyms
414 tyConDataCons :: TyCon -> [DataCon]
415 -- It's convenient for tyConDataCons to return the
416 -- empty list for type synonyms etc
417 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
419 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
420 tyConDataCons_maybe (AlgTyCon {algTyConRhs = DataTyCon cons _}) = Just cons
421 tyConDataCons_maybe (AlgTyCon {algTyConRhs = NewTyCon con _ _}) = Just [con]
422 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
423 tyConDataCons_maybe other = Nothing
425 tyConFamilySize :: TyCon -> Int
426 tyConFamilySize (AlgTyCon {algTyConRhs = DataTyCon cons _}) = length cons
427 tyConFamilySize (AlgTyCon {algTyConRhs = NewTyCon _ _ _}) = 1
428 tyConFamilySize (TupleTyCon {}) = 1
430 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
433 tyConSelIds :: TyCon -> [Id]
434 tyConSelIds (AlgTyCon {selIds = sels}) = sels
435 tyConSelIds other_tycon = []
439 newTyConRep :: TyCon -> ([TyVar], Type)
440 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTyConRhs = NewTyCon _ _ rep}) = (tvs, rep)
442 newTyConRhs :: TyCon -> ([TyVar], Type)
443 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTyConRhs = NewTyCon _ rhs _}) = (tvs, rhs)
445 tyConPrimRep :: TyCon -> PrimRep
446 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
447 tyConPrimRep tc = ASSERT( not (isUnboxedTupleTyCon tc) )
449 -- We should not be asking what the representation of an
450 -- unboxed tuple is, because it isn't a first class value.
454 tyConTheta :: TyCon -> [PredType]
455 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
456 tyConTheta (TupleTyCon {}) = []
457 -- shouldn't ask about anything else
460 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
461 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
462 actually computed (in another file).
465 tyConArgVrcs :: TyCon -> ArgVrcs
466 tyConArgVrcs (FunTyCon {}) = [(False,True),(True,False)]
467 tyConArgVrcs (AlgTyCon {argVrcs = oi}) = oi
468 tyConArgVrcs (PrimTyCon {argVrcs = oi}) = oi
469 tyConArgVrcs (TupleTyCon {tyConArity = arity}) = (replicate arity (True,False))
470 tyConArgVrcs (SynTyCon {argVrcs = oi}) = oi
474 getSynTyConDefn :: TyCon -> ([TyVar], Type)
475 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
479 maybeTyConSingleCon :: TyCon -> Maybe DataCon
480 maybeTyConSingleCon (AlgTyCon {algTyConRhs = DataTyCon [c] _}) = Just c
481 maybeTyConSingleCon (AlgTyCon {algTyConRhs = NewTyCon c _ _}) = Just c
482 maybeTyConSingleCon (AlgTyCon {}) = Nothing
483 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
484 maybeTyConSingleCon (PrimTyCon {}) = Nothing
485 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
486 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
490 isClassTyCon :: TyCon -> Bool
491 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
492 isClassTyCon other_tycon = False
494 tyConClass_maybe :: TyCon -> Maybe Class
495 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
496 tyConClass_maybe ther_tycon = Nothing
500 %************************************************************************
502 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
504 %************************************************************************
506 @TyCon@s are compared by comparing their @Unique@s.
508 The strictness analyser needs @Ord@. It is a lexicographic order with
509 the property @(a<=b) || (b<=a)@.
512 instance Eq TyCon where
513 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
514 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
516 instance Ord TyCon where
517 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
518 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
519 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
520 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
521 compare a b = getUnique a `compare` getUnique b
523 instance Uniquable TyCon where
524 getUnique tc = tyConUnique tc
526 instance Outputable TyCon where
527 ppr tc = ppr (getName tc)
529 instance NamedThing TyCon where