2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
10 AlgTyConRhs(..), visibleDataCons,
12 isFunTyCon, isUnLiftedTyCon, isProductTyCon, isAbstractTyCon,
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 isAbstractTyCon :: TyCon -> Bool
317 isAbstractTyCon (AlgTyCon { algTyConRhs = AbstractTyCon }) = True
318 isAbstractTyCon _ = False
320 isPrimTyCon :: TyCon -> Bool
321 isPrimTyCon (PrimTyCon {}) = True
322 isPrimTyCon _ = False
324 isUnLiftedTyCon :: TyCon -> Bool
325 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
326 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
327 isUnLiftedTyCon _ = False
329 -- isAlgTyCon returns True for both @data@ and @newtype@
330 isAlgTyCon :: TyCon -> Bool
331 isAlgTyCon (AlgTyCon {}) = True
332 isAlgTyCon (TupleTyCon {}) = True
333 isAlgTyCon other = False
335 isDataTyCon :: TyCon -> Bool
336 -- isDataTyCon returns True for data types that are represented by
337 -- heap-allocated constructors.
338 -- These are srcutinised by Core-level @case@ expressions, and they
339 -- get info tables allocated for them.
340 -- True for all @data@ types
341 -- False for newtypes
343 isDataTyCon (AlgTyCon {algTyConRhs = rhs})
345 DataTyCon _ _ -> True
346 NewTyCon _ _ _ -> False
347 AbstractTyCon -> panic "isDataTyCon"
349 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
350 isDataTyCon other = False
352 isNewTyCon :: TyCon -> Bool
353 isNewTyCon (AlgTyCon {algTyConRhs = NewTyCon _ _ _}) = True
354 isNewTyCon other = False
356 isProductTyCon :: TyCon -> Bool
358 -- has *one* constructor,
359 -- is *not* existential
361 -- may be DataType or NewType,
362 -- may be unboxed or not,
363 -- may be recursive or not
364 isProductTyCon tc@(AlgTyCon {}) = case algTyConRhs tc of
365 DataTyCon [data_con] _ -> not (isExistentialDataCon data_con)
366 NewTyCon _ _ _ -> True
368 isProductTyCon (TupleTyCon {}) = True
369 isProductTyCon other = False
371 isSynTyCon :: TyCon -> Bool
372 isSynTyCon (SynTyCon {}) = True
375 isEnumerationTyCon :: TyCon -> Bool
376 isEnumerationTyCon (AlgTyCon {algTyConRhs = DataTyCon _ is_enum}) = is_enum
377 isEnumerationTyCon other = False
379 isTupleTyCon :: TyCon -> Bool
380 -- The unit tycon didn't used to be classed as a tuple tycon
381 -- but I thought that was silly so I've undone it
382 -- If it can't be for some reason, it should be a AlgTyCon
383 isTupleTyCon (TupleTyCon {}) = True
384 isTupleTyCon other = False
386 isUnboxedTupleTyCon :: TyCon -> Bool
387 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
388 isUnboxedTupleTyCon other = False
390 isBoxedTupleTyCon :: TyCon -> Bool
391 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
392 isBoxedTupleTyCon other = False
394 tupleTyConBoxity tc = tyConBoxed tc
396 isRecursiveTyCon :: TyCon -> Bool
397 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
398 isRecursiveTyCon other = False
400 isHiBootTyCon :: TyCon -> Bool
401 -- Used for knot-tying in hi-boot files
402 isHiBootTyCon (AlgTyCon {algTyConRhs = AbstractTyCon}) = True
403 isHiBootTyCon other = False
405 isForeignTyCon :: TyCon -> Bool
406 -- isForeignTyCon identifies foreign-imported type constructors
407 -- For the moment, they are primitive but lifted, but that may change
408 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
409 isForeignTyCon other = False
413 tyConHasGenerics :: TyCon -> Bool
414 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
415 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
416 tyConHasGenerics other = False -- Synonyms
418 tyConDataCons :: TyCon -> [DataCon]
419 -- It's convenient for tyConDataCons to return the
420 -- empty list for type synonyms etc
421 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
423 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
424 tyConDataCons_maybe (AlgTyCon {algTyConRhs = DataTyCon cons _}) = Just cons
425 tyConDataCons_maybe (AlgTyCon {algTyConRhs = NewTyCon con _ _}) = Just [con]
426 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
427 tyConDataCons_maybe other = Nothing
429 tyConFamilySize :: TyCon -> Int
430 tyConFamilySize (AlgTyCon {algTyConRhs = DataTyCon cons _}) = length cons
431 tyConFamilySize (AlgTyCon {algTyConRhs = NewTyCon _ _ _}) = 1
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 newTyConRep :: TyCon -> ([TyVar], Type)
444 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTyConRhs = NewTyCon _ _ rep}) = (tvs, rep)
446 newTyConRhs :: TyCon -> ([TyVar], Type)
447 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTyConRhs = NewTyCon _ rhs _}) = (tvs, rhs)
449 tyConPrimRep :: TyCon -> PrimRep
450 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
451 tyConPrimRep tc = ASSERT( not (isUnboxedTupleTyCon tc) )
453 -- We should not be asking what the representation of an
454 -- unboxed tuple is, because it isn't a first class value.
458 tyConTheta :: TyCon -> [PredType]
459 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
460 tyConTheta (TupleTyCon {}) = []
461 -- shouldn't ask about anything else
464 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
465 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
466 actually computed (in another file).
469 tyConArgVrcs :: TyCon -> ArgVrcs
470 tyConArgVrcs (FunTyCon {}) = [(False,True),(True,False)]
471 tyConArgVrcs (AlgTyCon {argVrcs = oi}) = oi
472 tyConArgVrcs (PrimTyCon {argVrcs = oi}) = oi
473 tyConArgVrcs (TupleTyCon {tyConArity = arity}) = (replicate arity (True,False))
474 tyConArgVrcs (SynTyCon {argVrcs = oi}) = oi
478 getSynTyConDefn :: TyCon -> ([TyVar], Type)
479 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
483 maybeTyConSingleCon :: TyCon -> Maybe DataCon
484 maybeTyConSingleCon (AlgTyCon {algTyConRhs = DataTyCon [c] _}) = Just c
485 maybeTyConSingleCon (AlgTyCon {algTyConRhs = NewTyCon c _ _}) = Just c
486 maybeTyConSingleCon (AlgTyCon {}) = Nothing
487 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
488 maybeTyConSingleCon (PrimTyCon {}) = Nothing
489 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
490 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
494 isClassTyCon :: TyCon -> Bool
495 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
496 isClassTyCon other_tycon = False
498 tyConClass_maybe :: TyCon -> Maybe Class
499 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
500 tyConClass_maybe ther_tycon = Nothing
504 %************************************************************************
506 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
508 %************************************************************************
510 @TyCon@s are compared by comparing their @Unique@s.
512 The strictness analyser needs @Ord@. It is a lexicographic order with
513 the property @(a<=b) || (b<=a)@.
516 instance Eq TyCon where
517 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
518 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
520 instance Ord TyCon where
521 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
522 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
523 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
524 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
525 compare a b = getUnique a `compare` getUnique b
527 instance Uniquable TyCon where
528 getUnique tc = tyConUnique tc
530 instance Outputable TyCon where
531 ppr tc = ppr (getName tc)
533 instance NamedThing TyCon where