2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
15 AlgTyConRhs(..), visibleDataCons,
16 AlgTyConParent(..), hasParent,
19 isFunTyCon, isUnLiftedTyCon, isProductTyCon,
20 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isClosedNewTyCon,
22 isEnumerationTyCon, isGadtSyntaxTyCon, isOpenTyCon,
23 assocTyConArgPoss_maybe, isTyConAssoc, setTyConArgPoss,
24 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
25 isRecursiveTyCon, newTyConRep, newTyConRhs, newTyConCo_maybe,
26 isHiBootTyCon, isSuperKindTyCon,
27 isCoercionTyCon_maybe, isCoercionTyCon,
30 tcExpandTyCon_maybe, coreExpandTyCon_maybe,
32 makeTyConAbstract, isAbstractTyCon,
34 mkForeignTyCon, isForeignTyCon,
51 algTyConRhs, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
55 isClassTyCon, tyConClass_maybe,
56 isFamInstTyCon, tyConFamInst_maybe, tyConFamilyCoercion_maybe,
57 synTyConDefn, synTyConRhs, synTyConType, synTyConResKind,
58 tyConExtName, -- External name for foreign types
66 #include "HsVersions.h"
68 import {-# SOURCE #-} TypeRep ( Kind, Type, PredType )
69 import {-# SOURCE #-} DataCon ( DataCon, isVanillaDataCon )
81 %************************************************************************
83 \subsection{The data type}
85 %************************************************************************
90 tyConUnique :: Unique,
97 | AlgTyCon { -- Data type, and newtype decls.
98 -- All lifted, all boxed
99 tyConUnique :: Unique,
104 tyConTyVars :: [TyVar], -- Scopes over (a) the algTcStupidTheta
105 -- (b) the cached types in
106 -- algTyConRhs.NewTyCon
107 -- (c) the family instance
109 -- But not over the data constructors
111 tyConArgPoss :: Maybe [Int], -- for associated families: for each
112 -- tyvar in the AT decl, gives the
113 -- position of that tyvar in the class
114 -- argument list (starting from 0).
115 -- NB: Length is less than tyConArity
116 -- if higher kind signature.
118 algTcSelIds :: [Id], -- Its record selectors (empty if none)
120 algTcGadtSyntax :: Bool, -- True <=> the data type was declared using GADT syntax
121 -- That doesn't mean it's a true GADT; only that the "where"
122 -- form was used. This field is used only to guide
124 algTcStupidTheta :: [PredType], -- The "stupid theta" for the data type
125 -- (always empty for GADTs)
127 algTcRhs :: AlgTyConRhs, -- Data constructors in here
129 algTcRec :: RecFlag, -- Tells whether the data type is part
130 -- of a mutually-recursive group or not
132 hasGenerics :: Bool, -- True <=> generic to/from functions are available
133 -- (in the exports of the data type's source module)
135 algTcParent :: AlgTyConParent -- Gives the class or family tycon for
136 -- derived tycons representing classes
137 -- or family instances, respectively.
141 tyConUnique :: Unique,
145 tyConBoxed :: Boxity,
146 tyConTyVars :: [TyVar],
152 tyConUnique :: Unique,
157 tyConTyVars :: [TyVar], -- Bound tyvars
159 tyConArgPoss :: Maybe [Int], -- for associated families: for each
160 -- tyvar in the AT decl, gives the
161 -- position of that tyvar in the class
162 -- argument list (starting from 0).
163 -- NB: Length is less than tyConArity
164 -- if higher kind signature.
166 synTcRhs :: SynTyConRhs -- Expanded type in here
169 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
170 -- Now includes foreign-imported types
171 -- Also includes Kinds
172 tyConUnique :: Unique,
175 tyConArity :: Arity, -- SLPJ Oct06: I'm not sure what the significance
176 -- of the arity of a primtycon is!
178 primTyConRep :: PrimRep,
179 -- Many primitive tycons are unboxed, but some are
180 -- boxed (represented by pointers). The CgRep tells.
182 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
183 -- but foreign-imported ones may not be
184 tyConExtName :: Maybe FastString -- Just xx for foreign-imported types
187 | CoercionTyCon { -- E.g. (:=:), sym, trans, left, right
188 -- INVARIANT: coercions are always fully applied
189 tyConUnique :: Unique,
192 coKindFun :: [Type] -> (Type,Type)
193 } -- INVARAINT: coKindFun is always applied to exactly 'arity' args
194 -- E.g. for trans (c1 :: ta=tb) (c2 :: tb=tc), the coKindFun returns
195 -- the kind as a pair of types: (ta,tc)
197 | SuperKindTyCon { -- Super Kinds, TY (box) and CO (diamond).
198 -- They have no kind; and arity zero
199 tyConUnique :: Unique,
203 type FieldLabel = Name
206 = AbstractTyCon -- We know nothing about this data type, except
207 -- that it's represented by a pointer
208 -- Used when we export a data type abstractly into
211 | OpenDataTyCon -- data family (further instances can appear
212 | OpenNewTyCon -- newtype family at any time)
215 data_cons :: [DataCon],
216 -- The constructors; can be empty if the user declares
217 -- the type to have no constructors
218 -- INVARIANT: Kept in order of increasing tag
219 -- (see the tag assignment in DataCon.mkDataCon)
220 is_enum :: Bool -- Cached: True <=> an enumeration type
221 } -- Includes data types with no constructors.
224 data_con :: DataCon, -- The unique constructor; it has no existentials
226 nt_rhs :: Type, -- Cached: the argument type of the constructor
227 -- = the representation type of the tycon
228 -- The free tyvars of this type are the tyConTyVars
230 nt_co :: Maybe TyCon, -- The coercion used to create the newtype
231 -- from the representation
232 -- optional for non-recursive newtypes
233 -- See Note [Newtype coercions]
235 nt_etad_rhs :: ([TyVar], Type) ,
236 -- The same again, but this time eta-reduced
237 -- hence the [TyVar] which may be shorter than the declared
238 -- arity of the TyCon. See Note [Newtype eta]
240 nt_rep :: Type -- Cached: the *ultimate* representation type
241 -- By 'ultimate' I mean that the top-level constructor
242 -- of the rep type is not itself a newtype or type synonym.
243 -- The rep type isn't entirely simple:
244 -- for a recursive newtype we pick () as the rep type
247 -- This one does not need to be eta reduced; hence its
248 -- free type variables are conveniently tyConTyVars
250 -- newtype T a = MkT [(a,Int)]
251 -- The rep type is [(a,Int)]
252 -- NB: the rep type isn't necessarily the original RHS of the
253 -- newtype decl, because the rep type looks through other
256 visibleDataCons :: AlgTyConRhs -> [DataCon]
257 visibleDataCons AbstractTyCon = []
258 visibleDataCons OpenDataTyCon = []
259 visibleDataCons OpenNewTyCon = []
260 visibleDataCons (DataTyCon{ data_cons = cs }) = cs
261 visibleDataCons (NewTyCon{ data_con = c }) = [c]
263 -- Both type classes as well as data/newtype family instances imply implicit
264 -- type constructors. These implicit type constructors refer to their parent
265 -- structure (ie, the class or family from which they derive) using a type of
266 -- the following form.
268 data AlgTyConParent = -- An ordinary type constructor has no parent.
271 -- Type constructors representing a class dictionary.
274 -- Type constructors representing an instances of a type
276 | FamilyTyCon TyCon -- the type family
277 [Type] -- instance types
278 TyCon -- a *coercion* identifying
279 -- the representation type
280 -- with the type instance
282 hasParent :: AlgTyConParent -> Bool
283 hasParent NoParentTyCon = False
284 hasParent _other = True
287 = OpenSynTyCon Kind -- Type family: *result* kind given
288 | SynonymTyCon Type -- Mentioning head type vars. Acts as a template for
289 -- the expansion when the tycon is applied to some
293 Note [Newtype coercions]
294 ~~~~~~~~~~~~~~~~~~~~~~~~
296 The NewTyCon field nt_co is a a TyCon (a coercion constructor in fact)
297 which is used for coercing from the representation type of the
298 newtype, to the newtype itself. For example,
300 newtype T a = MkT (a -> a)
302 the NewTyCon for T will contain nt_co = CoT where CoT t : T t :=: t ->
303 t. This TyCon is a CoercionTyCon, so it does not have a kind on its
304 own; it basically has its own typing rule for the fully-applied
305 version. If the newtype T has k type variables then CoT has arity at
306 most k. In the case that the right hand side is a type application
307 ending with the same type variables as the left hand side, we
308 "eta-contract" the coercion. So if we had
310 newtype S a = MkT [a]
312 then we would generate the arity 0 coercion CoS : S :=: []. The
313 primary reason we do this is to make newtype deriving cleaner.
315 In the paper we'd write
316 axiom CoT : (forall t. T t) :=: (forall t. [t])
317 and then when we used CoT at a particular type, s, we'd say
319 which encodes as (TyConApp instCoercionTyCon [TyConApp CoT [], s])
321 But in GHC we instead make CoT into a new piece of type syntax
322 (like instCoercionTyCon, symCoercionTyCon etc), which must always
323 be saturated, but which encodes as
325 In the vocabulary of the paper it's as if we had axiom declarations
327 axiom CoT t : T t :=: [t]
332 newtype Parser m a = MkParser (Foogle m a)
333 Are these two types equal (to Core)?
336 Well, yes. But to see that easily we eta-reduce the RHS type of
337 Parser, in this case to ([], Froogle), so that even unsaturated applications
338 of Parser will work right. This eta reduction is done when the type
339 constructor is built, and cached in NewTyCon. The cached field is
340 only used in coreExpandTyCon_maybe.
342 Here's an example that I think showed up in practice
344 newtype T a = MkT [a]
345 newtype Foo m = MkFoo (forall a. m a -> Int)
351 w2 = MkFoo (\(MkT x) -> case w1 of MkFoo f -> f x)
353 After desugaring, and discading the data constructors for the newtypes,
357 And now Lint complains unless Foo T == Foo [], and that requires T==[]
360 %************************************************************************
364 %************************************************************************
366 A PrimRep is an abstraction of a type. It contains information that
367 the code generator needs in order to pass arguments, return results,
368 and store values of this type.
370 A PrimRep is somewhat similar to a CgRep (see codeGen/SMRep) and a
371 MachRep (see cmm/MachOp), although each of these types has a distinct
372 and clearly defined purpose:
374 - A PrimRep is a CgRep + information about signedness + information
375 about primitive pointers (AddrRep). Signedness and primitive
376 pointers are required when passing a primitive type to a foreign
377 function, but aren't needed for call/return conventions of Haskell
380 - A MachRep is a basic machine type (non-void, doesn't contain
381 information on pointerhood or signedness, but contains some
382 reps that don't have corresponding Haskell types).
388 | IntRep -- signed, word-sized
389 | WordRep -- unsinged, word-sized
390 | Int64Rep -- signed, 64 bit (32-bit words only)
391 | Word64Rep -- unsigned, 64 bit (32-bit words only)
392 | AddrRep -- a pointer, but not to a Haskell value
397 %************************************************************************
399 \subsection{TyCon Construction}
401 %************************************************************************
403 Note: the TyCon constructors all take a Kind as one argument, even though
404 they could, in principle, work out their Kind from their other arguments.
405 But to do so they need functions from Types, and that makes a nasty
406 module mutual-recursion. And they aren't called from many places.
407 So we compromise, and move their Kind calculation to the call site.
410 mkFunTyCon :: Name -> Kind -> TyCon
413 tyConUnique = nameUnique name,
419 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
420 -- but now you also have to pass in the generic information about the type
421 -- constructor - you can get hold of it easily (see Generics module)
422 mkAlgTyCon name kind tyvars stupid rhs sel_ids parent is_rec gen_info gadt_syn
425 tyConUnique = nameUnique name,
427 tyConArity = length tyvars,
428 tyConTyVars = tyvars,
429 tyConArgPoss = Nothing,
430 algTcStupidTheta = stupid,
432 algTcSelIds = sel_ids,
433 algTcParent = parent,
435 algTcGadtSyntax = gadt_syn,
436 hasGenerics = gen_info
439 mkClassTyCon name kind tyvars rhs clas is_rec =
440 mkAlgTyCon name kind tyvars [] rhs [] (ClassTyCon clas) is_rec False False
442 mkTupleTyCon name kind arity tyvars con boxed gen_info
444 tyConUnique = nameUnique name,
449 tyConTyVars = tyvars,
451 hasGenerics = gen_info
454 -- Foreign-imported (.NET) type constructors are represented
455 -- as primitive, but *lifted*, TyCons for now. They are lifted
456 -- because the Haskell type T representing the (foreign) .NET
457 -- type T is actually implemented (in ILX) as a thunk<T>
458 mkForeignTyCon name ext_name kind arity
461 tyConUnique = nameUnique name,
464 primTyConRep = PtrRep, -- they all do
466 tyConExtName = ext_name
470 -- most Prim tycons are lifted
471 mkPrimTyCon name kind arity rep
472 = mkPrimTyCon' name kind arity rep True
474 mkVoidPrimTyCon name kind arity
475 = mkPrimTyCon' name kind arity VoidRep True
477 -- but RealWorld is lifted
478 mkLiftedPrimTyCon name kind arity rep
479 = mkPrimTyCon' name kind arity rep False
481 mkPrimTyCon' name kind arity rep is_unlifted
484 tyConUnique = nameUnique name,
488 isUnLifted = is_unlifted,
489 tyConExtName = Nothing
492 mkSynTyCon name kind tyvars rhs
495 tyConUnique = nameUnique name,
497 tyConArity = length tyvars,
498 tyConTyVars = tyvars,
499 tyConArgPoss = Nothing,
503 mkCoercionTyCon name arity kindRule
506 tyConUnique = nameUnique name,
511 -- Super kinds always have arity zero
512 mkSuperKindTyCon name
515 tyConUnique = nameUnique name
520 isFunTyCon :: TyCon -> Bool
521 isFunTyCon (FunTyCon {}) = True
524 isAbstractTyCon :: TyCon -> Bool
525 isAbstractTyCon (AlgTyCon { algTcRhs = AbstractTyCon }) = True
526 isAbstractTyCon _ = False
528 makeTyConAbstract :: TyCon -> TyCon
529 makeTyConAbstract tc@(AlgTyCon {}) = tc { algTcRhs = AbstractTyCon }
530 makeTyConAbstract tc = pprPanic "makeTyConAbstract" (ppr tc)
532 isPrimTyCon :: TyCon -> Bool
533 isPrimTyCon (PrimTyCon {}) = True
534 isPrimTyCon _ = False
536 isUnLiftedTyCon :: TyCon -> Bool
537 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
538 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
539 isUnLiftedTyCon _ = False
541 -- isAlgTyCon returns True for both @data@ and @newtype@
542 isAlgTyCon :: TyCon -> Bool
543 isAlgTyCon (AlgTyCon {}) = True
544 isAlgTyCon (TupleTyCon {}) = True
545 isAlgTyCon other = False
547 isDataTyCon :: TyCon -> Bool
548 -- isDataTyCon returns True for data types that are definitely
549 -- represented by heap-allocated constructors.
550 -- These are srcutinised by Core-level @case@ expressions, and they
551 -- get info tables allocated for them.
552 -- True for all @data@ types
553 -- False for newtypes
555 isDataTyCon tc@(AlgTyCon {algTcRhs = rhs})
557 OpenDataTyCon -> True
559 OpenNewTyCon -> False
561 AbstractTyCon -> False -- We don't know, so return False
562 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
563 isDataTyCon other = False
565 isNewTyCon :: TyCon -> Bool
566 isNewTyCon (AlgTyCon {algTcRhs = rhs}) = case rhs of
570 isNewTyCon other = False
572 -- This is an important refinement as typical newtype optimisations do *not*
573 -- hold for newtype families. Why? Given a type `T a', if T is a newtype
574 -- family, there is no unique right hand side by which `T a' can be replaced
577 isClosedNewTyCon :: TyCon -> Bool
578 isClosedNewTyCon tycon = isNewTyCon tycon && not (isOpenTyCon tycon)
580 isProductTyCon :: TyCon -> Bool
582 -- has *one* constructor,
583 -- is *not* existential
585 -- may be DataType, NewType
586 -- may be unboxed or not,
587 -- may be recursive or not
589 isProductTyCon tc@(AlgTyCon {}) = case algTcRhs tc of
590 DataTyCon{ data_cons = [data_con] }
591 -> isVanillaDataCon data_con
594 isProductTyCon (TupleTyCon {}) = True
595 isProductTyCon other = False
597 isSynTyCon :: TyCon -> Bool
598 isSynTyCon (SynTyCon {}) = True
601 isGadtSyntaxTyCon :: TyCon -> Bool
602 isGadtSyntaxTyCon (AlgTyCon { algTcGadtSyntax = res }) = res
603 isGadtSyntaxTyCon other = False
605 isEnumerationTyCon :: TyCon -> Bool
606 isEnumerationTyCon (AlgTyCon {algTcRhs = DataTyCon { is_enum = res }}) = res
607 isEnumerationTyCon other = False
609 isOpenTyCon :: TyCon -> Bool
610 isOpenTyCon (SynTyCon {synTcRhs = OpenSynTyCon _}) = True
611 isOpenTyCon (AlgTyCon {algTcRhs = OpenDataTyCon }) = True
612 isOpenTyCon (AlgTyCon {algTcRhs = OpenNewTyCon }) = True
613 isOpenTyCon _ = False
615 assocTyConArgPoss_maybe :: TyCon -> Maybe [Int]
616 assocTyConArgPoss_maybe (AlgTyCon { tyConArgPoss = poss }) = poss
617 assocTyConArgPoss_maybe (SynTyCon { tyConArgPoss = poss }) = poss
618 assocTyConArgPoss_maybe _ = Nothing
620 isTyConAssoc :: TyCon -> Bool
621 isTyConAssoc = isJust . assocTyConArgPoss_maybe
623 setTyConArgPoss :: TyCon -> [Int] -> TyCon
624 setTyConArgPoss tc@(AlgTyCon {}) poss = tc { tyConArgPoss = Just poss }
625 setTyConArgPoss tc@(SynTyCon {}) poss = tc { tyConArgPoss = Just poss }
626 setTyConArgPoss tc _ = pprPanic "setTyConArgPoss" (ppr tc)
628 isTupleTyCon :: TyCon -> Bool
629 -- The unit tycon didn't used to be classed as a tuple tycon
630 -- but I thought that was silly so I've undone it
631 -- If it can't be for some reason, it should be a AlgTyCon
633 -- NB: when compiling Data.Tuple, the tycons won't reply True to
634 -- isTupleTyCon, becuase they are built as AlgTyCons. However they
635 -- get spat into the interface file as tuple tycons, so I don't think
637 isTupleTyCon (TupleTyCon {}) = True
638 isTupleTyCon other = False
640 isUnboxedTupleTyCon :: TyCon -> Bool
641 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
642 isUnboxedTupleTyCon other = False
644 isBoxedTupleTyCon :: TyCon -> Bool
645 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
646 isBoxedTupleTyCon other = False
648 tupleTyConBoxity tc = tyConBoxed tc
650 isRecursiveTyCon :: TyCon -> Bool
651 isRecursiveTyCon (AlgTyCon {algTcRec = Recursive}) = True
652 isRecursiveTyCon other = False
654 isHiBootTyCon :: TyCon -> Bool
655 -- Used for knot-tying in hi-boot files
656 isHiBootTyCon (AlgTyCon {algTcRhs = AbstractTyCon}) = True
657 isHiBootTyCon other = False
659 isForeignTyCon :: TyCon -> Bool
660 -- isForeignTyCon identifies foreign-imported type constructors
661 isForeignTyCon (PrimTyCon {tyConExtName = Just _}) = True
662 isForeignTyCon other = False
664 isSuperKindTyCon :: TyCon -> Bool
665 isSuperKindTyCon (SuperKindTyCon {}) = True
666 isSuperKindTyCon other = False
668 isCoercionTyCon_maybe :: TyCon -> Maybe (Arity, [Type] -> (Type,Type))
669 isCoercionTyCon_maybe (CoercionTyCon {tyConArity = ar, coKindFun = rule})
671 isCoercionTyCon_maybe other = Nothing
673 isCoercionTyCon :: TyCon -> Bool
674 isCoercionTyCon (CoercionTyCon {}) = True
675 isCoercionTyCon other = False
677 isImplicitTyCon :: TyCon -> Bool
678 isImplicitTyCon SynTyCon{} = False
679 isImplicitTyCon AlgTyCon{algTcParent = parent} = hasParent parent
680 isImplicitTyCon other = True
681 -- catches: FunTyCon, TupleTyCon, PrimTyCon,
682 -- CoercionTyCon, SuperKindTyCon
686 -----------------------------------------------
687 -- Expand type-constructor applications
688 -----------------------------------------------
691 tcExpandTyCon_maybe, coreExpandTyCon_maybe
693 -> [Type] -- Args to tycon
694 -> Maybe ([(TyVar,Type)], -- Substitution
695 Type, -- Body type (not yet substituted)
696 [Type]) -- Leftover args
698 -- For the *typechecker* view, we expand synonyms only
699 tcExpandTyCon_maybe (SynTyCon {tyConTyVars = tvs,
700 synTcRhs = SynonymTyCon rhs }) tys
702 tcExpandTyCon_maybe other_tycon tys = Nothing
705 -- For the *Core* view, we expand synonyms only as well
707 coreExpandTyCon_maybe (AlgTyCon {algTcRec = NonRecursive, -- Not recursive
708 algTcRhs = NewTyCon { nt_etad_rhs = etad_rhs, nt_co = Nothing }}) tys
709 = case etad_rhs of -- Don't do this in the pattern match, lest we accidentally
710 -- match the etad_rhs of a *recursive* newtype
711 (tvs,rhs) -> expand tvs rhs tys
713 coreExpandTyCon_maybe tycon tys = tcExpandTyCon_maybe tycon tys
717 expand :: [TyVar] -> Type -- Template
719 -> Maybe ([(TyVar,Type)], Type, [Type]) -- Expansion
721 = case n_tvs `compare` length tys of
722 LT -> Just (tvs `zip` tys, rhs, drop n_tvs tys)
723 EQ -> Just (tvs `zip` tys, rhs, [])
730 tyConHasGenerics :: TyCon -> Bool
731 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
732 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
733 tyConHasGenerics other = False -- Synonyms
735 tyConDataCons :: TyCon -> [DataCon]
736 -- It's convenient for tyConDataCons to return the
737 -- empty list for type synonyms etc
738 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
740 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
741 tyConDataCons_maybe (AlgTyCon {algTcRhs = DataTyCon { data_cons = cons }}) = Just cons
742 tyConDataCons_maybe (AlgTyCon {algTcRhs = NewTyCon { data_con = con }}) = Just [con]
743 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
744 tyConDataCons_maybe other = Nothing
746 tyConFamilySize :: TyCon -> Int
747 tyConFamilySize (AlgTyCon {algTcRhs = DataTyCon {data_cons = cons}}) =
749 tyConFamilySize (AlgTyCon {algTcRhs = NewTyCon {}}) = 1
750 tyConFamilySize (AlgTyCon {algTcRhs = OpenDataTyCon}) = 0
751 tyConFamilySize (TupleTyCon {}) = 1
753 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
756 tyConSelIds :: TyCon -> [Id]
757 tyConSelIds (AlgTyCon {algTcSelIds = fs}) = fs
758 tyConSelIds other_tycon = []
760 algTyConRhs :: TyCon -> AlgTyConRhs
761 algTyConRhs (AlgTyCon {algTcRhs = rhs}) = rhs
762 algTyConRhs (TupleTyCon {dataCon = con}) = DataTyCon { data_cons = [con], is_enum = False }
763 algTyConRhs other = pprPanic "algTyConRhs" (ppr other)
767 newTyConRhs :: TyCon -> ([TyVar], Type)
768 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rhs = rhs }}) = (tvs, rhs)
769 newTyConRhs tycon = pprPanic "newTyConRhs" (ppr tycon)
771 newTyConRep :: TyCon -> ([TyVar], Type)
772 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rep = rep }}) = (tvs, rep)
773 newTyConRep tycon = pprPanic "newTyConRep" (ppr tycon)
775 newTyConCo_maybe :: TyCon -> Maybe TyCon
776 newTyConCo_maybe (AlgTyCon {algTcRhs = NewTyCon { nt_co = co }}) = co
777 newTyConCo_maybe _ = Nothing
779 tyConPrimRep :: TyCon -> PrimRep
780 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
781 tyConPrimRep tc = ASSERT(not (isUnboxedTupleTyCon tc)) PtrRep
785 tyConStupidTheta :: TyCon -> [PredType]
786 tyConStupidTheta (AlgTyCon {algTcStupidTheta = stupid}) = stupid
787 tyConStupidTheta (TupleTyCon {}) = []
788 tyConStupidTheta tycon = pprPanic "tyConStupidTheta" (ppr tycon)
792 synTyConDefn :: TyCon -> ([TyVar], Type)
793 synTyConDefn (SynTyCon {tyConTyVars = tyvars, synTcRhs = SynonymTyCon ty})
795 synTyConDefn tycon = pprPanic "getSynTyConDefn" (ppr tycon)
797 synTyConRhs :: TyCon -> SynTyConRhs
798 synTyConRhs (SynTyCon {synTcRhs = rhs}) = rhs
799 synTyConRhs tc = pprPanic "synTyConRhs" (ppr tc)
801 synTyConType :: TyCon -> Type
802 synTyConType tc = case synTcRhs tc of
804 _ -> pprPanic "synTyConType" (ppr tc)
806 synTyConResKind :: TyCon -> Kind
807 synTyConResKind (SynTyCon {synTcRhs = OpenSynTyCon kind}) = kind
808 synTyConResKind tycon = pprPanic "synTyConResKind" (ppr tycon)
812 maybeTyConSingleCon :: TyCon -> Maybe DataCon
813 maybeTyConSingleCon (AlgTyCon {algTcRhs = DataTyCon {data_cons = [c] }}) = Just c
814 maybeTyConSingleCon (AlgTyCon {algTcRhs = NewTyCon { data_con = c }}) = Just c
815 maybeTyConSingleCon (AlgTyCon {}) = Nothing
816 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
817 maybeTyConSingleCon (PrimTyCon {}) = Nothing
818 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
819 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
823 isClassTyCon :: TyCon -> Bool
824 isClassTyCon (AlgTyCon {algTcParent = ClassTyCon _}) = True
825 isClassTyCon other_tycon = False
827 tyConClass_maybe :: TyCon -> Maybe Class
828 tyConClass_maybe (AlgTyCon {algTcParent = ClassTyCon clas}) = Just clas
829 tyConClass_maybe ther_tycon = Nothing
831 isFamInstTyCon :: TyCon -> Bool
832 isFamInstTyCon (AlgTyCon {algTcParent = FamilyTyCon _ _ _ }) = True
833 isFamInstTyCon other_tycon = False
835 tyConFamInst_maybe :: TyCon -> Maybe (TyCon, [Type])
836 tyConFamInst_maybe (AlgTyCon {algTcParent = FamilyTyCon fam instTys _}) =
838 tyConFamInst_maybe ther_tycon =
841 tyConFamilyCoercion_maybe :: TyCon -> Maybe TyCon
842 tyConFamilyCoercion_maybe (AlgTyCon {algTcParent = FamilyTyCon _ _ coe}) =
844 tyConFamilyCoercion_maybe ther_tycon =
849 %************************************************************************
851 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
853 %************************************************************************
855 @TyCon@s are compared by comparing their @Unique@s.
857 The strictness analyser needs @Ord@. It is a lexicographic order with
858 the property @(a<=b) || (b<=a)@.
861 instance Eq TyCon where
862 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
863 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
865 instance Ord TyCon where
866 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
867 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
868 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
869 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
870 compare a b = getUnique a `compare` getUnique b
872 instance Uniquable TyCon where
873 getUnique tc = tyConUnique tc
875 instance Outputable TyCon where
876 ppr tc = ppr (getName tc)
878 instance NamedThing TyCon where