2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
8 TyCon, KindCon, SuperKindCon, ArgVrcs,
11 DataConDetails(..), visibleDataCons,
13 isFunTyCon, isUnLiftedTyCon, isProductTyCon,
14 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
16 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
17 isRecursiveTyCon, newTyConRep, isHiBootTyCon,
19 mkForeignTyCon, isForeignTyCon,
35 tyConArgVrcs_maybe, tyConArgVrcs,
36 tyConDataConDetails, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
41 isClassTyCon, tyConClass_maybe,
43 tyConExtName, -- External name for foreign types
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(..), isBoxed )
63 import Name ( Name, nameUnique, NamedThing(getName) )
64 import PrelNames ( Unique, Uniquable(..), anyBoxConKey )
65 import PrimRep ( PrimRep(..), isFollowableRep )
66 import Maybes ( orElse, expectJust )
71 %************************************************************************
73 \subsection{The data type}
75 %************************************************************************
79 type SuperKindCon = TyCon
83 tyConUnique :: Unique,
90 | AlgTyCon { -- Tuples, data type, and newtype decls.
91 -- All lifted, all boxed
92 tyConUnique :: Unique,
97 tyConTyVars :: [TyVar],
99 algTyConTheta :: [PredType],
101 dataCons :: DataConDetails DataCon,
103 selIds :: [Id], -- Its record selectors (if any)
105 algTyConFlavour :: AlgTyConFlavour,
106 algTyConRec :: RecFlag, -- Tells whether the data type is part of
107 -- a mutually-recursive group or not
109 hasGenerics :: Bool, -- True <=> generic to/from functions are available
110 -- (in the exports of the data type's source module)
112 algTyConClass :: Maybe Class
113 -- Just cl if this tycon came from a class declaration
116 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
117 -- Now includes foreign-imported types
118 tyConUnique :: Unique,
123 primTyConRep :: PrimRep, -- Many primitive tycons are unboxed, but some are
124 -- boxed (represented by pointers). The PrimRep tells.
126 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
127 -- but foreign-imported ones may not be
128 tyConExtName :: Maybe FastString -- Just xx for foreign-imported types
133 tyConUnique :: Unique,
137 tyConBoxed :: Boxity,
138 tyConTyVars :: [TyVar],
144 tyConUnique :: Unique,
149 tyConTyVars :: [TyVar], -- Bound tyvars
150 synTyConDefn :: Type, -- Right-hand side, mentioning these type vars.
151 -- Acts as a template for the expansion when
152 -- the tycon is applied to some types.
156 | KindCon { -- Type constructor at the kind level
157 tyConUnique :: Unique,
159 tyConKind :: SuperKind,
163 | SuperKindCon { -- The type of kind variables or boxity variables,
164 tyConUnique :: Unique,
168 type ArgVrcs = [(Bool,Bool)] -- Tyvar variance info: [(occPos,occNeg)]
169 -- [] means "no information, assume the worst"
172 = DataTyCon Bool -- Data type; True <=> an enumeration type
174 | NewTyCon Type -- Newtype, with its *ultimate* representation type
175 -- By 'ultimate' I mean that the rep type is not itself
176 -- a newtype or type synonym.
177 -- The rep type isn't entirely simple:
178 -- for a recursive newtype we pick () as the rep type
181 -- The rep type has free type variables the tyConTyVars
183 -- newtype T a = MkT [(a,Int)]
184 -- The rep type is [(a,Int)]
185 -- NB: the rep type isn't necessarily the original RHS of the
186 -- newtype decl, because the rep type looks through other
187 -- newtypes. If you want hte original RHS, look at the
188 -- argument type of the data constructor.
190 data DataConDetails datacon
191 = DataCons [datacon] -- Its data constructors, with fully polymorphic types
192 -- A type can have zero constructors
194 | Unknown -- Used only when We're importing this data type from an
195 -- hi-boot file, so we don't know what its constructors are
197 visibleDataCons (DataCons cs) = cs
198 visibleDataCons other = []
202 %************************************************************************
204 \subsection{TyCon Construction}
206 %************************************************************************
208 Note: the TyCon constructors all take a Kind as one argument, even though
209 they could, in principle, work out their Kind from their other arguments.
210 But to do so they need functions from Types, and that makes a nasty
211 module mutual-recursion. And they aren't called from many places.
212 So we compromise, and move their Kind calculation to the call site.
215 mkSuperKindCon :: Name -> SuperKindCon
216 mkSuperKindCon name = SuperKindCon {
217 tyConUnique = nameUnique name,
221 mkKindCon :: Name -> SuperKind -> KindCon
224 tyConUnique = nameUnique name,
230 mkFunTyCon :: Name -> Kind -> TyCon
233 tyConUnique = nameUnique name,
239 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
240 -- but now you also have to pass in the generic information about the type
241 -- constructor - you can get hold of it easily (see Generics module)
242 mkAlgTyCon name kind tyvars theta argvrcs cons sels flavour is_rec gen_info
245 tyConUnique = nameUnique name,
247 tyConArity = length tyvars,
248 tyConTyVars = tyvars,
250 algTyConTheta = theta,
253 algTyConClass = Nothing,
254 algTyConFlavour = flavour,
255 algTyConRec = is_rec,
256 hasGenerics = gen_info
259 mkClassTyCon name kind tyvars argvrcs con clas flavour is_rec
262 tyConUnique = nameUnique name,
264 tyConArity = length tyvars,
265 tyConTyVars = tyvars,
268 dataCons = DataCons [con],
270 algTyConClass = Just clas,
271 algTyConFlavour = flavour,
272 algTyConRec = is_rec,
277 mkTupleTyCon name kind arity tyvars con boxed gen_info
279 tyConUnique = nameUnique name,
284 tyConTyVars = tyvars,
286 hasGenerics = gen_info
289 -- Foreign-imported (.NET) type constructors are represented
290 -- as primitive, but *lifted*, TyCons for now. They are lifted
291 -- because the Haskell type T representing the (foreign) .NET
292 -- type T is actually implemented (in ILX) as a thunk<T>
294 mkForeignTyCon name ext_name kind arity arg_vrcs
297 tyConUnique = nameUnique name,
301 primTyConRep = PtrRep,
303 tyConExtName = ext_name
307 -- most Prim tycons are lifted
308 mkPrimTyCon name kind arity arg_vrcs rep
309 = mkPrimTyCon' name kind arity arg_vrcs rep True
311 -- but RealWorld is lifted
312 mkLiftedPrimTyCon name kind arity arg_vrcs rep
313 = mkPrimTyCon' name kind arity arg_vrcs rep False
315 mkPrimTyCon' name kind arity arg_vrcs rep is_unlifted
318 tyConUnique = nameUnique name,
323 isUnLifted = is_unlifted,
324 tyConExtName = Nothing
327 mkSynTyCon name kind tyvars rhs argvrcs
330 tyConUnique = nameUnique name,
332 tyConArity = length tyvars,
333 tyConTyVars = tyvars,
340 isFunTyCon :: TyCon -> Bool
341 isFunTyCon (FunTyCon {}) = True
344 isPrimTyCon :: TyCon -> Bool
345 isPrimTyCon (PrimTyCon {}) = True
346 isPrimTyCon _ = False
348 isUnLiftedTyCon :: TyCon -> Bool
349 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
350 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
351 isUnLiftedTyCon _ = False
354 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
355 isBoxedTyCon :: TyCon -> Bool
356 isBoxedTyCon (AlgTyCon {}) = True
357 isBoxedTyCon (FunTyCon {}) = True
358 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
359 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
362 -- isAlgTyCon returns True for both @data@ and @newtype@
363 isAlgTyCon :: TyCon -> Bool
364 isAlgTyCon (AlgTyCon {}) = True
365 isAlgTyCon (TupleTyCon {}) = True
366 isAlgTyCon other = False
368 isDataTyCon :: TyCon -> Bool
369 -- isDataTyCon returns True for data types that are represented by
370 -- heap-allocated constructors.
371 -- These are srcutinised by Core-level @case@ expressions, and they
372 -- get info tables allocated for them.
373 -- True for all @data@ types
374 -- False for newtypes
376 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data})
377 = case new_or_data of
381 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
382 isDataTyCon other = False
384 isNewTyCon :: TyCon -> Bool
385 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True
386 isNewTyCon other = False
388 isProductTyCon :: TyCon -> Bool
390 -- has *one* constructor,
391 -- is *not* existential
393 -- may be DataType or NewType,
394 -- may be unboxed or not,
395 -- may be recursive or not
396 isProductTyCon (AlgTyCon {dataCons = DataCons [data_con]}) = not (isExistentialDataCon data_con)
397 isProductTyCon (TupleTyCon {}) = True
398 isProductTyCon other = False
400 isSynTyCon :: TyCon -> Bool
401 isSynTyCon (SynTyCon {}) = True
404 isEnumerationTyCon :: TyCon -> Bool
405 isEnumerationTyCon (AlgTyCon {algTyConFlavour = DataTyCon is_enum}) = is_enum
406 isEnumerationTyCon other = False
408 isTupleTyCon :: TyCon -> Bool
409 -- The unit tycon didn't used to be classed as a tuple tycon
410 -- but I thought that was silly so I've undone it
411 -- If it can't be for some reason, it should be a AlgTyCon
412 isTupleTyCon (TupleTyCon {}) = True
413 isTupleTyCon other = False
415 isUnboxedTupleTyCon :: TyCon -> Bool
416 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
417 isUnboxedTupleTyCon other = False
419 isBoxedTupleTyCon :: TyCon -> Bool
420 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
421 isBoxedTupleTyCon other = False
423 tupleTyConBoxity tc = tyConBoxed tc
425 isRecursiveTyCon :: TyCon -> Bool
426 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
427 isRecursiveTyCon other = False
429 isHiBootTyCon :: TyCon -> Bool
430 -- Used for knot-tying in hi-boot files
431 isHiBootTyCon (AlgTyCon {dataCons = Unknown}) = True
432 isHiBootTyCon other = False
434 isForeignTyCon :: TyCon -> Bool
435 -- isForeignTyCon identifies foreign-imported type constructors
436 -- For the moment, they are primitive but lifted, but that may change
437 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
438 isForeignTyCon other = False
442 tyConHasGenerics :: TyCon -> Bool
443 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
444 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
445 tyConHasGenerics other = False -- Synonyms
447 tyConDataConDetails :: TyCon -> DataConDetails DataCon
448 tyConDataConDetails (AlgTyCon {dataCons = cons}) = cons
449 tyConDataConDetails (TupleTyCon {dataCon = con}) = DataCons [con]
450 tyConDataConDetails other = pprPanic "tyConDataConDetails" (ppr other)
452 tyConDataCons :: TyCon -> [DataCon]
453 -- It's convenient for tyConDataCons to return the
454 -- empty list for type synonyms etc
455 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
457 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
458 tyConDataCons_maybe (AlgTyCon {dataCons = DataCons cons}) = Just cons
459 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
460 tyConDataCons_maybe other = Nothing
462 tyConFamilySize :: TyCon -> Int
463 tyConFamilySize (AlgTyCon {dataCons = DataCons cs}) = length cs
464 tyConFamilySize (TupleTyCon {}) = 1
466 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
469 tyConSelIds :: TyCon -> [Id]
470 tyConSelIds (AlgTyCon {selIds = sels}) = sels
471 tyConSelIds other_tycon = []
475 newTyConRep :: TyCon -> ([TyVar], Type)
476 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTyConFlavour = NewTyCon rep}) = (tvs, rep)
478 tyConPrimRep :: TyCon -> PrimRep
479 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
480 tyConPrimRep tc = ASSERT( not (isUnboxedTupleTyCon tc) )
482 -- We should not be asking what the representation of an
483 -- unboxed tuple is, because it isn't a first class value.
487 tyConTheta :: TyCon -> [PredType]
488 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
489 tyConTheta (TupleTyCon {}) = []
490 -- shouldn't ask about anything else
493 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
494 each tyvar, if available. See @calcAlgTyConArgVrcs@ for how this is
495 actually computed (in another file).
498 tyConArgVrcs :: TyCon -> ArgVrcs
499 tyConArgVrcs tc = expectJust "tyConArgVrcs" (tyConArgVrcs_maybe tc)
501 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
502 tyConArgVrcs_maybe (FunTyCon {}) = Just [(False,True),(True,False)]
503 tyConArgVrcs_maybe (AlgTyCon {argVrcs = oi}) = Just oi
504 tyConArgVrcs_maybe (PrimTyCon {argVrcs = oi}) = Just oi
505 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity}) = Just (replicate arity (True,False))
506 tyConArgVrcs_maybe (SynTyCon {argVrcs = oi}) = Just oi
507 tyConArgVrcs_maybe _ = Nothing
511 getSynTyConDefn :: TyCon -> ([TyVar], Type)
512 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
516 maybeTyConSingleCon :: TyCon -> Maybe DataCon
517 maybeTyConSingleCon (AlgTyCon {dataCons = DataCons [c]}) = Just c
518 maybeTyConSingleCon (AlgTyCon {}) = Nothing
519 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
520 maybeTyConSingleCon (PrimTyCon {}) = Nothing
521 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
522 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
526 isClassTyCon :: TyCon -> Bool
527 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
528 isClassTyCon other_tycon = False
530 tyConClass_maybe :: TyCon -> Maybe Class
531 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
532 tyConClass_maybe ther_tycon = Nothing
536 %************************************************************************
538 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
540 %************************************************************************
542 @TyCon@s are compared by comparing their @Unique@s.
544 The strictness analyser needs @Ord@. It is a lexicographic order with
545 the property @(a<=b) || (b<=a)@.
548 instance Eq TyCon where
549 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
550 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
552 instance Ord TyCon where
553 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
554 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
555 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
556 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
557 compare a b = getUnique a `compare` getUnique b
559 instance Uniquable TyCon where
560 getUnique tc = tyConUnique tc
562 instance Outputable TyCon where
563 ppr tc = ppr (getName tc)
565 instance NamedThing TyCon where