2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TyCon]{The @TyCon@ datatype}
13 AlgTyConRhs(..), visibleDataCons, AlgTyConParent(..),
16 isFunTyCon, isUnLiftedTyCon, isProductTyCon,
17 isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
18 isEnumerationTyCon, isGadtSyntaxTyCon, isOpenTyCon,
19 assocTyConArgPoss_maybe, isTyConAssoc, setTyConArgPoss,
20 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
21 isRecursiveTyCon, newTyConRep, newTyConRhs, newTyConCo,
22 isHiBootTyCon, isSuperKindTyCon,
23 isCoercionTyCon_maybe, isCoercionTyCon,
25 tcExpandTyCon_maybe, coreExpandTyCon_maybe,
27 makeTyConAbstract, isAbstractTyCon,
29 mkForeignTyCon, isForeignTyCon,
46 algTyConRhs, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
50 isClassTyCon, tyConClass_maybe,
51 isFamInstTyCon, tyConFamInst_maybe, tyConFamilyCoercion_maybe,
52 synTyConDefn, synTyConRhs, synTyConType, synTyConResKind,
53 tyConExtName, -- External name for foreign types
61 #include "HsVersions.h"
63 import {-# SOURCE #-} TypeRep ( Kind, Type, Coercion, PredType )
64 import {-# SOURCE #-} DataCon ( DataCon, isVanillaDataCon )
66 import Var ( TyVar, Id )
67 import Class ( Class )
68 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
69 import Name ( Name, nameUnique, NamedThing(getName) )
70 import PrelNames ( Unique, Uniquable(..) )
71 import Maybe ( isJust )
72 import Maybes ( orElse )
77 %************************************************************************
79 \subsection{The data type}
81 %************************************************************************
86 tyConUnique :: Unique,
93 | AlgTyCon { -- Data type, and newtype decls.
94 -- All lifted, all boxed
95 tyConUnique :: Unique,
100 tyConTyVars :: [TyVar], -- Scopes over (a) the algTcStupidTheta
101 -- (b) the cached types in
102 -- algTyConRhs.NewTyCon
103 -- But not over the data constructors
105 tyConArgPoss :: Maybe [Int], -- for associated families: for each
106 -- tyvar in the AT decl, gives the
107 -- position of that tyvar in the class
108 -- argument list (starting from 0).
109 -- NB: Length is less than tyConArity
110 -- if higher kind signature.
112 algTcSelIds :: [Id], -- Its record selectors (empty if none)
114 algTcGadtSyntax :: Bool, -- True <=> the data type was declared using GADT syntax
115 -- That doesn't mean it's a true GADT; only that the "where"
116 -- form was used. This field is used only to guide
118 algTcStupidTheta :: [PredType], -- The "stupid theta" for the data type
119 -- (always empty for GADTs)
121 algTcRhs :: AlgTyConRhs, -- Data constructors in here
123 algTcRec :: RecFlag, -- Tells whether the data type is part
124 -- of a mutually-recursive group or not
126 hasGenerics :: Bool, -- True <=> generic to/from functions are available
127 -- (in the exports of the data type's source module)
129 algTcParent :: AlgTyConParent -- Gives the class or family tycon for
130 -- derived tycons representing classes
131 -- or family instances, respectively.
135 tyConUnique :: Unique,
139 tyConBoxed :: Boxity,
140 tyConTyVars :: [TyVar],
146 tyConUnique :: Unique,
151 tyConTyVars :: [TyVar], -- Bound tyvars
153 tyConArgPoss :: Maybe [Int], -- for associated families: for each
154 -- tyvar in the AT decl, gives the
155 -- position of that tyvar in the class
156 -- argument list (starting from 0).
157 -- NB: Length is less than tyConArity
158 -- if higher kind signature.
160 synTcRhs :: SynTyConRhs -- Expanded type in here
163 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
164 -- Now includes foreign-imported types
165 -- Also includes Kinds
166 tyConUnique :: Unique,
171 primTyConRep :: PrimRep,
172 -- Many primitive tycons are unboxed, but some are
173 -- boxed (represented by pointers). The CgRep tells.
175 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
176 -- but foreign-imported ones may not be
177 tyConExtName :: Maybe FastString -- Just xx for foreign-imported types
180 | CoercionTyCon { -- E.g. (:=:), sym, trans, left, right
181 -- INVARIANT: coercions are always fully applied
182 tyConUnique :: Unique,
185 coKindFun :: [Type] -> Kind
188 | SuperKindTyCon { -- Super Kinds, TY (box) and CO (diamond).
189 -- They have no kind; and arity zero
190 tyConUnique :: Unique,
196 type SuperKindCon = TyCon
198 type FieldLabel = Name
201 = AbstractTyCon -- We know nothing about this data type, except
202 -- that it's represented by a pointer
203 -- Used when we export a data type abstractly into
206 | OpenDataTyCon -- data family (further instances can appear
207 | OpenNewTyCon -- newtype family at any time)
210 data_cons :: [DataCon],
211 -- The constructors; can be empty if the user declares
212 -- the type to have no constructors
213 -- INVARIANT: Kept in order of increasing tag
214 -- (see the tag assignment in DataCon.mkDataCon)
215 is_enum :: Bool -- Cached: True <=> an enumeration type
216 } -- Includes data types with no constructors.
219 data_con :: DataCon, -- The unique constructor; it has no existentials
221 nt_rhs :: Type, -- Cached: the argument type of the constructor
222 -- = the representation type of the tycon
223 -- The free tyvars of this type are the tyConTyVars
225 nt_co :: Maybe TyCon, -- The coercion used to create the newtype
226 -- from the representation
227 -- optional for non-recursive newtypes
228 -- See Note [Newtype coercions]
230 nt_etad_rhs :: ([TyVar], Type) ,
231 -- The same again, but this time eta-reduced
232 -- hence the [TyVar] which may be shorter than the declared
233 -- arity of the TyCon. See Note [Newtype eta]
235 nt_rep :: Type -- Cached: the *ultimate* representation type
236 -- By 'ultimate' I mean that the top-level constructor
237 -- of the rep type is not itself a newtype or type synonym.
238 -- The rep type isn't entirely simple:
239 -- for a recursive newtype we pick () as the rep type
242 -- This one does not need to be eta reduced; hence its
243 -- free type variables are conveniently tyConTyVars
245 -- newtype T a = MkT [(a,Int)]
246 -- The rep type is [(a,Int)]
247 -- NB: the rep type isn't necessarily the original RHS of the
248 -- newtype decl, because the rep type looks through other
251 visibleDataCons :: AlgTyConRhs -> [DataCon]
252 visibleDataCons AbstractTyCon = []
253 visibleDataCons OpenDataTyCon = []
254 visibleDataCons OpenNewTyCon = []
255 visibleDataCons (DataTyCon{ data_cons = cs }) = cs
256 visibleDataCons (NewTyCon{ data_con = c }) = [c]
258 -- Both type classes as well as data/newtype family instances imply implicit
259 -- type constructors. These implicit type constructors refer to their parent
260 -- structure (ie, the class or family from which they derive) using a type of
261 -- the following form.
263 data AlgTyConParent = -- An ordinary type constructor has no parent.
266 -- Type constructors representing a class dictionary.
269 -- Type constructors representing an instances of a type
271 | FamilyTyCon TyCon -- the type family
272 [Type] -- instance types
273 TyCon -- a *coercion* identifying
274 -- the representation type
275 -- with the type instance
278 = OpenSynTyCon Kind -- Type family: *result* kind given
279 | SynonymTyCon Type -- Mentioning head type vars. Acts as a template for
280 -- the expansion when the tycon is applied to some
284 Note [Newtype coercions]
285 ~~~~~~~~~~~~~~~~~~~~~~~~
287 The NewTyCon field nt_co is a a TyCon (a coercion constructor in fact)
288 which is used for coercing from the representation type of the
289 newtype, to the newtype itself. For example,
291 newtype T a = MkT (a -> a)
293 the NewTyCon for T will contain nt_co = CoT where CoT t : T t :=: t ->
294 t. This TyCon is a CoercionTyCon, so it does not have a kind on its
295 own; it basically has its own typing rule for the fully-applied
296 version. If the newtype T has k type variables then CoT has arity at
297 most k. In the case that the right hand side is a type application
298 ending with the same type variables as the left hand side, we
299 "eta-contract" the coercion. So if we had
301 newtype S a = MkT [a]
303 then we would generate the arity 0 coercion CoS : S :=: []. The
304 primary reason we do this is to make newtype deriving cleaner.
306 In the paper we'd write
307 axiom CoT : (forall t. T t) :=: (forall t. [t])
308 and then when we used CoT at a particular type, s, we'd say
310 which encodes as (TyConApp instCoercionTyCon [TyConApp CoT [], s])
312 But in GHC we instead make CoT into a new piece of type syntax
313 (like instCoercionTyCon, symCoercionTyCon etc), which must always
314 be saturated, but which encodes as
316 In the vocabulary of the paper it's as if we had axiom declarations
318 axiom CoT t : T t :=: [t]
323 newtype Parser m a = MkParser (Foogle m a)
324 Are these two types equal (to Core)?
327 Well, yes. But to see that easily we eta-reduce the RHS type of
328 Parser, in this case to ([], Froogle), so that even unsaturated applications
329 of Parser will work right. This eta reduction is done when the type
330 constructor is built, and cached in NewTyCon. The cached field is
331 only used in coreExpandTyCon_maybe.
333 Here's an example that I think showed up in practice
335 newtype T a = MkT [a]
336 newtype Foo m = MkFoo (forall a. m a -> Int)
342 w2 = MkFoo (\(MkT x) -> case w1 of MkFoo f -> f x)
344 After desugaring, and discading the data constructors for the newtypes,
348 And now Lint complains unless Foo T == Foo [], and that requires T==[]
351 %************************************************************************
355 %************************************************************************
357 A PrimRep is an abstraction of a type. It contains information that
358 the code generator needs in order to pass arguments, return results,
359 and store values of this type.
361 A PrimRep is somewhat similar to a CgRep (see codeGen/SMRep) and a
362 MachRep (see cmm/MachOp), although each of these types has a distinct
363 and clearly defined purpose:
365 - A PrimRep is a CgRep + information about signedness + information
366 about primitive pointers (AddrRep). Signedness and primitive
367 pointers are required when passing a primitive type to a foreign
368 function, but aren't needed for call/return conventions of Haskell
371 - A MachRep is a basic machine type (non-void, doesn't contain
372 information on pointerhood or signedness, but contains some
373 reps that don't have corresponding Haskell types).
379 | IntRep -- signed, word-sized
380 | WordRep -- unsinged, word-sized
381 | Int64Rep -- signed, 64 bit (32-bit words only)
382 | Word64Rep -- unsigned, 64 bit (32-bit words only)
383 | AddrRep -- a pointer, but not to a Haskell value
388 %************************************************************************
390 \subsection{TyCon Construction}
392 %************************************************************************
394 Note: the TyCon constructors all take a Kind as one argument, even though
395 they could, in principle, work out their Kind from their other arguments.
396 But to do so they need functions from Types, and that makes a nasty
397 module mutual-recursion. And they aren't called from many places.
398 So we compromise, and move their Kind calculation to the call site.
401 mkFunTyCon :: Name -> Kind -> TyCon
404 tyConUnique = nameUnique name,
410 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
411 -- but now you also have to pass in the generic information about the type
412 -- constructor - you can get hold of it easily (see Generics module)
413 mkAlgTyCon name kind tyvars stupid rhs sel_ids parent is_rec gen_info gadt_syn
416 tyConUnique = nameUnique name,
418 tyConArity = length tyvars,
419 tyConTyVars = tyvars,
420 tyConArgPoss = Nothing,
421 algTcStupidTheta = stupid,
423 algTcSelIds = sel_ids,
424 algTcParent = parent,
426 algTcGadtSyntax = gadt_syn,
427 hasGenerics = gen_info
430 mkClassTyCon name kind tyvars rhs clas is_rec =
431 mkAlgTyCon name kind tyvars [] rhs [] (ClassTyCon clas) is_rec False False
433 mkTupleTyCon name kind arity tyvars con boxed gen_info
435 tyConUnique = nameUnique name,
440 tyConTyVars = tyvars,
442 hasGenerics = gen_info
445 -- Foreign-imported (.NET) type constructors are represented
446 -- as primitive, but *lifted*, TyCons for now. They are lifted
447 -- because the Haskell type T representing the (foreign) .NET
448 -- type T is actually implemented (in ILX) as a thunk<T>
449 mkForeignTyCon name ext_name kind arity
452 tyConUnique = nameUnique name,
455 primTyConRep = PtrRep, -- they all do
457 tyConExtName = ext_name
461 -- most Prim tycons are lifted
462 mkPrimTyCon name kind arity rep
463 = mkPrimTyCon' name kind arity rep True
465 mkVoidPrimTyCon name kind arity
466 = mkPrimTyCon' name kind arity VoidRep True
468 -- but RealWorld is lifted
469 mkLiftedPrimTyCon name kind arity rep
470 = mkPrimTyCon' name kind arity rep False
472 mkPrimTyCon' name kind arity rep is_unlifted
475 tyConUnique = nameUnique name,
479 isUnLifted = is_unlifted,
480 tyConExtName = Nothing
483 mkSynTyCon name kind tyvars rhs
486 tyConUnique = nameUnique name,
488 tyConArity = length tyvars,
489 tyConTyVars = tyvars,
490 tyConArgPoss = Nothing,
494 mkCoercionTyCon name arity kindRule
497 tyConUnique = nameUnique name,
502 -- Super kinds always have arity zero
503 mkSuperKindTyCon name
506 tyConUnique = nameUnique name
511 isFunTyCon :: TyCon -> Bool
512 isFunTyCon (FunTyCon {}) = True
515 isAbstractTyCon :: TyCon -> Bool
516 isAbstractTyCon (AlgTyCon { algTcRhs = AbstractTyCon }) = True
517 isAbstractTyCon _ = False
519 makeTyConAbstract :: TyCon -> TyCon
520 makeTyConAbstract tc@(AlgTyCon {}) = tc { algTcRhs = AbstractTyCon }
521 makeTyConAbstract tc = pprPanic "makeTyConAbstract" (ppr tc)
523 isPrimTyCon :: TyCon -> Bool
524 isPrimTyCon (PrimTyCon {}) = True
525 isPrimTyCon _ = False
527 isUnLiftedTyCon :: TyCon -> Bool
528 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
529 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
530 isUnLiftedTyCon _ = False
532 -- isAlgTyCon returns True for both @data@ and @newtype@
533 isAlgTyCon :: TyCon -> Bool
534 isAlgTyCon (AlgTyCon {}) = True
535 isAlgTyCon (TupleTyCon {}) = True
536 isAlgTyCon other = False
538 isDataTyCon :: TyCon -> Bool
539 -- isDataTyCon returns True for data types that are represented by
540 -- heap-allocated constructors.
541 -- These are srcutinised by Core-level @case@ expressions, and they
542 -- get info tables allocated for them.
543 -- True for all @data@ types
544 -- False for newtypes
546 isDataTyCon tc@(AlgTyCon {algTcRhs = rhs})
548 OpenDataTyCon -> True
550 OpenNewTyCon -> False
552 AbstractTyCon -> pprPanic "isDataTyCon" (ppr tc)
554 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
555 isDataTyCon other = False
557 isNewTyCon :: TyCon -> Bool
558 isNewTyCon (AlgTyCon {algTcRhs = NewTyCon {}}) = True
559 isNewTyCon other = False
561 isProductTyCon :: TyCon -> Bool
563 -- has *one* constructor,
564 -- is *not* existential
566 -- may be DataType, NewType
567 -- may be unboxed or not,
568 -- may be recursive or not
570 isProductTyCon tc@(AlgTyCon {}) = case algTcRhs tc of
571 DataTyCon{ data_cons = [data_con] }
572 -> isVanillaDataCon data_con
575 isProductTyCon (TupleTyCon {}) = True
576 isProductTyCon other = False
578 isSynTyCon :: TyCon -> Bool
579 isSynTyCon (SynTyCon {}) = True
582 isGadtSyntaxTyCon :: TyCon -> Bool
583 isGadtSyntaxTyCon (AlgTyCon { algTcGadtSyntax = res }) = res
584 isGadtSyntaxTyCon other = False
586 isEnumerationTyCon :: TyCon -> Bool
587 isEnumerationTyCon (AlgTyCon {algTcRhs = DataTyCon { is_enum = res }}) = res
588 isEnumerationTyCon other = False
590 isOpenTyCon :: TyCon -> Bool
591 isOpenTyCon (SynTyCon {synTcRhs = OpenSynTyCon _}) = True
592 isOpenTyCon (AlgTyCon {algTcRhs = OpenDataTyCon }) = True
593 isOpenTyCon (AlgTyCon {algTcRhs = OpenNewTyCon }) = True
594 isOpenTyCon _ = False
596 assocTyConArgPoss_maybe :: TyCon -> Maybe [Int]
597 assocTyConArgPoss_maybe (AlgTyCon { tyConArgPoss = poss }) = poss
598 assocTyConArgPoss_maybe (SynTyCon { tyConArgPoss = poss }) = poss
599 assocTyConArgPoss_maybe _ = Nothing
601 isTyConAssoc :: TyCon -> Bool
602 isTyConAssoc = isJust . assocTyConArgPoss_maybe
604 setTyConArgPoss :: TyCon -> [Int] -> TyCon
605 setTyConArgPoss tc@(AlgTyCon {}) poss = tc { tyConArgPoss = Just poss }
606 setTyConArgPoss tc@(SynTyCon {}) poss = tc { tyConArgPoss = Just poss }
607 setTyConArgPoss tc _ = pprPanic "setTyConArgPoss" (ppr tc)
609 isTupleTyCon :: TyCon -> Bool
610 -- The unit tycon didn't used to be classed as a tuple tycon
611 -- but I thought that was silly so I've undone it
612 -- If it can't be for some reason, it should be a AlgTyCon
614 -- NB: when compiling Data.Tuple, the tycons won't reply True to
615 -- isTupleTyCon, becuase they are built as AlgTyCons. However they
616 -- get spat into the interface file as tuple tycons, so I don't think
618 isTupleTyCon (TupleTyCon {}) = True
619 isTupleTyCon other = False
621 isUnboxedTupleTyCon :: TyCon -> Bool
622 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
623 isUnboxedTupleTyCon other = False
625 isBoxedTupleTyCon :: TyCon -> Bool
626 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
627 isBoxedTupleTyCon other = False
629 tupleTyConBoxity tc = tyConBoxed tc
631 isRecursiveTyCon :: TyCon -> Bool
632 isRecursiveTyCon (AlgTyCon {algTcRec = Recursive}) = True
633 isRecursiveTyCon other = False
635 isHiBootTyCon :: TyCon -> Bool
636 -- Used for knot-tying in hi-boot files
637 isHiBootTyCon (AlgTyCon {algTcRhs = AbstractTyCon}) = True
638 isHiBootTyCon other = False
640 isForeignTyCon :: TyCon -> Bool
641 -- isForeignTyCon identifies foreign-imported type constructors
642 isForeignTyCon (PrimTyCon {tyConExtName = Just _}) = True
643 isForeignTyCon other = False
645 isSuperKindTyCon :: TyCon -> Bool
646 isSuperKindTyCon (SuperKindTyCon {}) = True
647 isSuperKindTyCon other = False
649 isCoercionTyCon_maybe :: TyCon -> Maybe (Arity, [Type] -> Kind)
650 isCoercionTyCon_maybe (CoercionTyCon {tyConArity = ar, coKindFun = rule})
652 isCoercionTyCon_maybe other = Nothing
654 isCoercionTyCon (CoercionTyCon {}) = True
655 isCoercionTyCon other = False
659 -----------------------------------------------
660 -- Expand type-constructor applications
661 -----------------------------------------------
664 tcExpandTyCon_maybe, coreExpandTyCon_maybe
666 -> [Type] -- Args to tycon
667 -> Maybe ([(TyVar,Type)], -- Substitution
668 Type, -- Body type (not yet substituted)
669 [Type]) -- Leftover args
671 -- For the *typechecker* view, we expand synonyms only
672 tcExpandTyCon_maybe (SynTyCon {tyConTyVars = tvs,
673 synTcRhs = SynonymTyCon rhs }) tys
675 tcExpandTyCon_maybe other_tycon tys = Nothing
678 -- For the *Core* view, we expand synonyms only as well
680 coreExpandTyCon_maybe (AlgTyCon {algTcRec = NonRecursive, -- Not recursive
681 algTcRhs = NewTyCon { nt_etad_rhs = etad_rhs, nt_co = Nothing }}) tys
682 = case etad_rhs of -- Don't do this in the pattern match, lest we accidentally
683 -- match the etad_rhs of a *recursive* newtype
684 (tvs,rhs) -> expand tvs rhs tys
686 coreExpandTyCon_maybe tycon tys = tcExpandTyCon_maybe tycon tys
690 expand :: [TyVar] -> Type -- Template
692 -> Maybe ([(TyVar,Type)], Type, [Type]) -- Expansion
694 = case n_tvs `compare` length tys of
695 LT -> Just (tvs `zip` tys, rhs, drop n_tvs tys)
696 EQ -> Just (tvs `zip` tys, rhs, [])
703 tyConHasGenerics :: TyCon -> Bool
704 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
705 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
706 tyConHasGenerics other = False -- Synonyms
708 tyConDataCons :: TyCon -> [DataCon]
709 -- It's convenient for tyConDataCons to return the
710 -- empty list for type synonyms etc
711 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
713 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
714 tyConDataCons_maybe (AlgTyCon {algTcRhs = DataTyCon { data_cons = cons }}) = Just cons
715 tyConDataCons_maybe (AlgTyCon {algTcRhs = NewTyCon { data_con = con }}) = Just [con]
716 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
717 tyConDataCons_maybe other = Nothing
719 tyConFamilySize :: TyCon -> Int
720 tyConFamilySize (AlgTyCon {algTcRhs = DataTyCon {data_cons = cons}}) =
722 tyConFamilySize (AlgTyCon {algTcRhs = NewTyCon {}}) = 1
723 tyConFamilySize (AlgTyCon {algTcRhs = OpenDataTyCon}) = 0
724 tyConFamilySize (TupleTyCon {}) = 1
726 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
729 tyConSelIds :: TyCon -> [Id]
730 tyConSelIds (AlgTyCon {algTcSelIds = fs}) = fs
731 tyConSelIds other_tycon = []
733 algTyConRhs :: TyCon -> AlgTyConRhs
734 algTyConRhs (AlgTyCon {algTcRhs = rhs}) = rhs
735 algTyConRhs (TupleTyCon {dataCon = con}) = DataTyCon { data_cons = [con], is_enum = False }
736 algTyConRhs other = pprPanic "algTyConRhs" (ppr other)
740 newTyConRhs :: TyCon -> ([TyVar], Type)
741 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rhs = rhs }}) = (tvs, rhs)
742 newTyConRhs tycon = pprPanic "newTyConRhs" (ppr tycon)
744 newTyConRep :: TyCon -> ([TyVar], Type)
745 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rep = rep }}) = (tvs, rep)
746 newTyConRep tycon = pprPanic "newTyConRep" (ppr tycon)
748 newTyConCo :: TyCon -> Maybe TyCon
749 newTyConCo (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_co = co }}) = co
750 newTyConCo tycon = pprPanic "newTyConCo" (ppr tycon)
752 tyConPrimRep :: TyCon -> PrimRep
753 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
754 tyConPrimRep tc = ASSERT(not (isUnboxedTupleTyCon tc)) PtrRep
758 tyConStupidTheta :: TyCon -> [PredType]
759 tyConStupidTheta (AlgTyCon {algTcStupidTheta = stupid}) = stupid
760 tyConStupidTheta (TupleTyCon {}) = []
761 tyConStupidTheta tycon = pprPanic "tyConStupidTheta" (ppr tycon)
765 synTyConDefn :: TyCon -> ([TyVar], Type)
766 synTyConDefn (SynTyCon {tyConTyVars = tyvars, synTcRhs = SynonymTyCon ty})
768 synTyConDefn tycon = pprPanic "getSynTyConDefn" (ppr tycon)
770 synTyConRhs :: TyCon -> SynTyConRhs
771 synTyConRhs (SynTyCon {synTcRhs = rhs}) = rhs
772 synTyConRhs tc = pprPanic "synTyConRhs" (ppr tc)
774 synTyConType :: TyCon -> Type
775 synTyConType tc = case synTcRhs tc of
777 _ -> pprPanic "synTyConType" (ppr tc)
779 synTyConResKind :: TyCon -> Kind
780 synTyConResKind (SynTyCon {synTcRhs = OpenSynTyCon kind}) = kind
781 synTyConResKind tycon = pprPanic "synTyConResKind" (ppr tycon)
785 maybeTyConSingleCon :: TyCon -> Maybe DataCon
786 maybeTyConSingleCon (AlgTyCon {algTcRhs = DataTyCon {data_cons = [c] }}) = Just c
787 maybeTyConSingleCon (AlgTyCon {algTcRhs = NewTyCon { data_con = c }}) = Just c
788 maybeTyConSingleCon (AlgTyCon {}) = Nothing
789 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
790 maybeTyConSingleCon (PrimTyCon {}) = Nothing
791 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
792 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
796 isClassTyCon :: TyCon -> Bool
797 isClassTyCon (AlgTyCon {algTcParent = ClassTyCon _}) = True
798 isClassTyCon other_tycon = False
800 tyConClass_maybe :: TyCon -> Maybe Class
801 tyConClass_maybe (AlgTyCon {algTcParent = ClassTyCon clas}) = Just clas
802 tyConClass_maybe ther_tycon = Nothing
804 isFamInstTyCon :: TyCon -> Bool
805 isFamInstTyCon (AlgTyCon {algTcParent = FamilyTyCon _ _ _}) = True
806 isFamInstTyCon other_tycon = False
808 tyConFamInst_maybe :: TyCon -> Maybe (TyCon, [Type])
809 tyConFamInst_maybe (AlgTyCon {algTcParent = FamilyTyCon fam instTys _}) =
811 tyConFamInst_maybe ther_tycon =
814 tyConFamilyCoercion_maybe :: TyCon -> Maybe TyCon
815 tyConFamilyCoercion_maybe (AlgTyCon {algTcParent = FamilyTyCon _ _ coe}) =
817 tyConFamilyCoercion_maybe ther_tycon =
822 %************************************************************************
824 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
826 %************************************************************************
828 @TyCon@s are compared by comparing their @Unique@s.
830 The strictness analyser needs @Ord@. It is a lexicographic order with
831 the property @(a<=b) || (b<=a)@.
834 instance Eq TyCon where
835 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
836 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
838 instance Ord TyCon where
839 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
840 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
841 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
842 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
843 compare a b = getUnique a `compare` getUnique b
845 instance Uniquable TyCon where
846 getUnique tc = tyConUnique tc
848 instance Outputable TyCon where
849 ppr tc = ppr (getName tc)
851 instance NamedThing TyCon where