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, isClosedNewTyCon,
19 isEnumerationTyCon, isGadtSyntaxTyCon, isOpenTyCon,
20 assocTyConArgPoss_maybe, isTyConAssoc, setTyConArgPoss,
21 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
22 isRecursiveTyCon, newTyConRep, newTyConRhs, newTyConCo_maybe,
23 isHiBootTyCon, isSuperKindTyCon,
24 isCoercionTyCon_maybe, isCoercionTyCon,
26 tcExpandTyCon_maybe, coreExpandTyCon_maybe,
28 makeTyConAbstract, isAbstractTyCon,
30 mkForeignTyCon, isForeignTyCon,
47 algTyConRhs, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
51 isClassTyCon, tyConClass_maybe,
52 isFamInstTyCon, tyConFamInst_maybe, tyConFamilyCoercion_maybe,
54 synTyConDefn, synTyConRhs, synTyConType, synTyConResKind,
55 tyConExtName, -- External name for foreign types
63 #include "HsVersions.h"
65 import {-# SOURCE #-} TypeRep ( Kind, Type, Coercion, PredType )
66 import {-# SOURCE #-} DataCon ( DataCon, isVanillaDataCon )
68 import Var ( TyVar, Id )
69 import Class ( Class )
70 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
71 import Name ( Name, nameUnique, NamedThing(getName) )
72 import PrelNames ( Unique, Uniquable(..) )
73 import Maybe ( isJust )
74 import Maybes ( orElse )
79 %************************************************************************
81 \subsection{The data type}
83 %************************************************************************
88 tyConUnique :: Unique,
95 | AlgTyCon { -- Data type, and newtype decls.
96 -- All lifted, all boxed
97 tyConUnique :: Unique,
102 tyConTyVars :: [TyVar], -- Scopes over (a) the algTcStupidTheta
103 -- (b) the cached types in
104 -- algTyConRhs.NewTyCon
105 -- But not over the data constructors
107 tyConArgPoss :: Maybe [Int], -- for associated families: for each
108 -- tyvar in the AT decl, gives the
109 -- position of that tyvar in the class
110 -- argument list (starting from 0).
111 -- NB: Length is less than tyConArity
112 -- if higher kind signature.
114 algTcSelIds :: [Id], -- Its record selectors (empty if none)
116 algTcGadtSyntax :: Bool, -- True <=> the data type was declared using GADT syntax
117 -- That doesn't mean it's a true GADT; only that the "where"
118 -- form was used. This field is used only to guide
120 algTcStupidTheta :: [PredType], -- The "stupid theta" for the data type
121 -- (always empty for GADTs)
123 algTcRhs :: AlgTyConRhs, -- Data constructors in here
125 algTcRec :: RecFlag, -- Tells whether the data type is part
126 -- of a mutually-recursive group or not
128 hasGenerics :: Bool, -- True <=> generic to/from functions are available
129 -- (in the exports of the data type's source module)
131 algTcParent :: AlgTyConParent -- Gives the class or family tycon for
132 -- derived tycons representing classes
133 -- or family instances, respectively.
137 tyConUnique :: Unique,
141 tyConBoxed :: Boxity,
142 tyConTyVars :: [TyVar],
148 tyConUnique :: Unique,
153 tyConTyVars :: [TyVar], -- Bound tyvars
155 tyConArgPoss :: Maybe [Int], -- for associated families: for each
156 -- tyvar in the AT decl, gives the
157 -- position of that tyvar in the class
158 -- argument list (starting from 0).
159 -- NB: Length is less than tyConArity
160 -- if higher kind signature.
162 synTcRhs :: SynTyConRhs -- Expanded type in here
165 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
166 -- Now includes foreign-imported types
167 -- Also includes Kinds
168 tyConUnique :: Unique,
173 primTyConRep :: PrimRep,
174 -- Many primitive tycons are unboxed, but some are
175 -- boxed (represented by pointers). The CgRep tells.
177 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
178 -- but foreign-imported ones may not be
179 tyConExtName :: Maybe FastString -- Just xx for foreign-imported types
182 | CoercionTyCon { -- E.g. (:=:), sym, trans, left, right
183 -- INVARIANT: coercions are always fully applied
184 tyConUnique :: Unique,
187 coKindFun :: [Type] -> Kind
190 | SuperKindTyCon { -- Super Kinds, TY (box) and CO (diamond).
191 -- They have no kind; and arity zero
192 tyConUnique :: Unique,
198 type SuperKindCon = TyCon
200 type FieldLabel = Name
203 = AbstractTyCon -- We know nothing about this data type, except
204 -- that it's represented by a pointer
205 -- Used when we export a data type abstractly into
208 | OpenDataTyCon -- data family (further instances can appear
209 | OpenNewTyCon -- newtype family at any time)
212 data_cons :: [DataCon],
213 -- The constructors; can be empty if the user declares
214 -- the type to have no constructors
215 -- INVARIANT: Kept in order of increasing tag
216 -- (see the tag assignment in DataCon.mkDataCon)
217 is_enum :: Bool -- Cached: True <=> an enumeration type
218 } -- Includes data types with no constructors.
221 data_con :: DataCon, -- The unique constructor; it has no existentials
223 nt_rhs :: Type, -- Cached: the argument type of the constructor
224 -- = the representation type of the tycon
225 -- The free tyvars of this type are the tyConTyVars
227 nt_co :: Maybe TyCon, -- The coercion used to create the newtype
228 -- from the representation
229 -- optional for non-recursive newtypes
230 -- See Note [Newtype coercions]
232 nt_etad_rhs :: ([TyVar], Type) ,
233 -- The same again, but this time eta-reduced
234 -- hence the [TyVar] which may be shorter than the declared
235 -- arity of the TyCon. See Note [Newtype eta]
237 nt_rep :: Type -- Cached: the *ultimate* representation type
238 -- By 'ultimate' I mean that the top-level constructor
239 -- of the rep type is not itself a newtype or type synonym.
240 -- The rep type isn't entirely simple:
241 -- for a recursive newtype we pick () as the rep type
244 -- This one does not need to be eta reduced; hence its
245 -- free type variables are conveniently tyConTyVars
247 -- newtype T a = MkT [(a,Int)]
248 -- The rep type is [(a,Int)]
249 -- NB: the rep type isn't necessarily the original RHS of the
250 -- newtype decl, because the rep type looks through other
253 visibleDataCons :: AlgTyConRhs -> [DataCon]
254 visibleDataCons AbstractTyCon = []
255 visibleDataCons OpenDataTyCon = []
256 visibleDataCons OpenNewTyCon = []
257 visibleDataCons (DataTyCon{ data_cons = cs }) = cs
258 visibleDataCons (NewTyCon{ data_con = c }) = [c]
260 -- Both type classes as well as data/newtype family instances imply implicit
261 -- type constructors. These implicit type constructors refer to their parent
262 -- structure (ie, the class or family from which they derive) using a type of
263 -- the following form.
265 data AlgTyConParent = -- An ordinary type constructor has no parent.
268 -- Type constructors representing a class dictionary.
271 -- Type constructors representing an instances of a type
273 | FamilyTyCon TyCon -- the type family
274 [Type] -- instance types
275 TyCon -- a *coercion* identifying
276 -- the representation type
277 -- with the type instance
278 Int -- index to generate unique
279 -- name (needed here to put
283 = OpenSynTyCon Kind -- Type family: *result* kind given
284 | SynonymTyCon Type -- Mentioning head type vars. Acts as a template for
285 -- the expansion when the tycon is applied to some
289 Note [Newtype coercions]
290 ~~~~~~~~~~~~~~~~~~~~~~~~
292 The NewTyCon field nt_co is a a TyCon (a coercion constructor in fact)
293 which is used for coercing from the representation type of the
294 newtype, to the newtype itself. For example,
296 newtype T a = MkT (a -> a)
298 the NewTyCon for T will contain nt_co = CoT where CoT t : T t :=: t ->
299 t. This TyCon is a CoercionTyCon, so it does not have a kind on its
300 own; it basically has its own typing rule for the fully-applied
301 version. If the newtype T has k type variables then CoT has arity at
302 most k. In the case that the right hand side is a type application
303 ending with the same type variables as the left hand side, we
304 "eta-contract" the coercion. So if we had
306 newtype S a = MkT [a]
308 then we would generate the arity 0 coercion CoS : S :=: []. The
309 primary reason we do this is to make newtype deriving cleaner.
311 In the paper we'd write
312 axiom CoT : (forall t. T t) :=: (forall t. [t])
313 and then when we used CoT at a particular type, s, we'd say
315 which encodes as (TyConApp instCoercionTyCon [TyConApp CoT [], s])
317 But in GHC we instead make CoT into a new piece of type syntax
318 (like instCoercionTyCon, symCoercionTyCon etc), which must always
319 be saturated, but which encodes as
321 In the vocabulary of the paper it's as if we had axiom declarations
323 axiom CoT t : T t :=: [t]
328 newtype Parser m a = MkParser (Foogle m a)
329 Are these two types equal (to Core)?
332 Well, yes. But to see that easily we eta-reduce the RHS type of
333 Parser, in this case to ([], Froogle), so that even unsaturated applications
334 of Parser will work right. This eta reduction is done when the type
335 constructor is built, and cached in NewTyCon. The cached field is
336 only used in coreExpandTyCon_maybe.
338 Here's an example that I think showed up in practice
340 newtype T a = MkT [a]
341 newtype Foo m = MkFoo (forall a. m a -> Int)
347 w2 = MkFoo (\(MkT x) -> case w1 of MkFoo f -> f x)
349 After desugaring, and discading the data constructors for the newtypes,
353 And now Lint complains unless Foo T == Foo [], and that requires T==[]
356 %************************************************************************
360 %************************************************************************
362 A PrimRep is an abstraction of a type. It contains information that
363 the code generator needs in order to pass arguments, return results,
364 and store values of this type.
366 A PrimRep is somewhat similar to a CgRep (see codeGen/SMRep) and a
367 MachRep (see cmm/MachOp), although each of these types has a distinct
368 and clearly defined purpose:
370 - A PrimRep is a CgRep + information about signedness + information
371 about primitive pointers (AddrRep). Signedness and primitive
372 pointers are required when passing a primitive type to a foreign
373 function, but aren't needed for call/return conventions of Haskell
376 - A MachRep is a basic machine type (non-void, doesn't contain
377 information on pointerhood or signedness, but contains some
378 reps that don't have corresponding Haskell types).
384 | IntRep -- signed, word-sized
385 | WordRep -- unsinged, word-sized
386 | Int64Rep -- signed, 64 bit (32-bit words only)
387 | Word64Rep -- unsigned, 64 bit (32-bit words only)
388 | AddrRep -- a pointer, but not to a Haskell value
393 %************************************************************************
395 \subsection{TyCon Construction}
397 %************************************************************************
399 Note: the TyCon constructors all take a Kind as one argument, even though
400 they could, in principle, work out their Kind from their other arguments.
401 But to do so they need functions from Types, and that makes a nasty
402 module mutual-recursion. And they aren't called from many places.
403 So we compromise, and move their Kind calculation to the call site.
406 mkFunTyCon :: Name -> Kind -> TyCon
409 tyConUnique = nameUnique name,
415 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
416 -- but now you also have to pass in the generic information about the type
417 -- constructor - you can get hold of it easily (see Generics module)
418 mkAlgTyCon name kind tyvars stupid rhs sel_ids parent is_rec gen_info gadt_syn
421 tyConUnique = nameUnique name,
423 tyConArity = length tyvars,
424 tyConTyVars = tyvars,
425 tyConArgPoss = Nothing,
426 algTcStupidTheta = stupid,
428 algTcSelIds = sel_ids,
429 algTcParent = parent,
431 algTcGadtSyntax = gadt_syn,
432 hasGenerics = gen_info
435 mkClassTyCon name kind tyvars rhs clas is_rec =
436 mkAlgTyCon name kind tyvars [] rhs [] (ClassTyCon clas) is_rec False False
438 mkTupleTyCon name kind arity tyvars con boxed gen_info
440 tyConUnique = nameUnique name,
445 tyConTyVars = tyvars,
447 hasGenerics = gen_info
450 -- Foreign-imported (.NET) type constructors are represented
451 -- as primitive, but *lifted*, TyCons for now. They are lifted
452 -- because the Haskell type T representing the (foreign) .NET
453 -- type T is actually implemented (in ILX) as a thunk<T>
454 mkForeignTyCon name ext_name kind arity
457 tyConUnique = nameUnique name,
460 primTyConRep = PtrRep, -- they all do
462 tyConExtName = ext_name
466 -- most Prim tycons are lifted
467 mkPrimTyCon name kind arity rep
468 = mkPrimTyCon' name kind arity rep True
470 mkVoidPrimTyCon name kind arity
471 = mkPrimTyCon' name kind arity VoidRep True
473 -- but RealWorld is lifted
474 mkLiftedPrimTyCon name kind arity rep
475 = mkPrimTyCon' name kind arity rep False
477 mkPrimTyCon' name kind arity rep is_unlifted
480 tyConUnique = nameUnique name,
484 isUnLifted = is_unlifted,
485 tyConExtName = Nothing
488 mkSynTyCon name kind tyvars rhs
491 tyConUnique = nameUnique name,
493 tyConArity = length tyvars,
494 tyConTyVars = tyvars,
495 tyConArgPoss = Nothing,
499 mkCoercionTyCon name arity kindRule
502 tyConUnique = nameUnique name,
507 -- Super kinds always have arity zero
508 mkSuperKindTyCon name
511 tyConUnique = nameUnique name
516 isFunTyCon :: TyCon -> Bool
517 isFunTyCon (FunTyCon {}) = True
520 isAbstractTyCon :: TyCon -> Bool
521 isAbstractTyCon (AlgTyCon { algTcRhs = AbstractTyCon }) = True
522 isAbstractTyCon _ = False
524 makeTyConAbstract :: TyCon -> TyCon
525 makeTyConAbstract tc@(AlgTyCon {}) = tc { algTcRhs = AbstractTyCon }
526 makeTyConAbstract tc = pprPanic "makeTyConAbstract" (ppr tc)
528 isPrimTyCon :: TyCon -> Bool
529 isPrimTyCon (PrimTyCon {}) = True
530 isPrimTyCon _ = False
532 isUnLiftedTyCon :: TyCon -> Bool
533 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
534 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
535 isUnLiftedTyCon _ = False
537 -- isAlgTyCon returns True for both @data@ and @newtype@
538 isAlgTyCon :: TyCon -> Bool
539 isAlgTyCon (AlgTyCon {}) = True
540 isAlgTyCon (TupleTyCon {}) = True
541 isAlgTyCon other = False
543 isDataTyCon :: TyCon -> Bool
544 -- isDataTyCon returns True for data types that are represented by
545 -- heap-allocated constructors.
546 -- These are srcutinised by Core-level @case@ expressions, and they
547 -- get info tables allocated for them.
548 -- True for all @data@ types
549 -- False for newtypes
551 isDataTyCon tc@(AlgTyCon {algTcRhs = rhs})
553 OpenDataTyCon -> True
555 OpenNewTyCon -> False
557 AbstractTyCon -> pprPanic "isDataTyCon" (ppr tc)
558 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
559 isDataTyCon other = False
561 isNewTyCon :: TyCon -> Bool
562 isNewTyCon (AlgTyCon {algTcRhs = rhs}) = case rhs of
566 isNewTyCon other = False
568 -- This is an important refinement as typical newtype optimisations do *not*
569 -- hold for newtype families. Why? Given a type `T a', if T is a newtype
570 -- family, there is no unique right hand side by which `T a' can be replaced
573 isClosedNewTyCon :: TyCon -> Bool
574 isClosedNewTyCon tycon = isNewTyCon tycon && not (isOpenTyCon tycon)
576 isProductTyCon :: TyCon -> Bool
578 -- has *one* constructor,
579 -- is *not* existential
581 -- may be DataType, NewType
582 -- may be unboxed or not,
583 -- may be recursive or not
585 isProductTyCon tc@(AlgTyCon {}) = case algTcRhs tc of
586 DataTyCon{ data_cons = [data_con] }
587 -> isVanillaDataCon data_con
590 isProductTyCon (TupleTyCon {}) = True
591 isProductTyCon other = False
593 isSynTyCon :: TyCon -> Bool
594 isSynTyCon (SynTyCon {}) = True
597 isGadtSyntaxTyCon :: TyCon -> Bool
598 isGadtSyntaxTyCon (AlgTyCon { algTcGadtSyntax = res }) = res
599 isGadtSyntaxTyCon other = False
601 isEnumerationTyCon :: TyCon -> Bool
602 isEnumerationTyCon (AlgTyCon {algTcRhs = DataTyCon { is_enum = res }}) = res
603 isEnumerationTyCon other = False
605 isOpenTyCon :: TyCon -> Bool
606 isOpenTyCon (SynTyCon {synTcRhs = OpenSynTyCon _}) = True
607 isOpenTyCon (AlgTyCon {algTcRhs = OpenDataTyCon }) = True
608 isOpenTyCon (AlgTyCon {algTcRhs = OpenNewTyCon }) = True
609 isOpenTyCon _ = False
611 assocTyConArgPoss_maybe :: TyCon -> Maybe [Int]
612 assocTyConArgPoss_maybe (AlgTyCon { tyConArgPoss = poss }) = poss
613 assocTyConArgPoss_maybe (SynTyCon { tyConArgPoss = poss }) = poss
614 assocTyConArgPoss_maybe _ = Nothing
616 isTyConAssoc :: TyCon -> Bool
617 isTyConAssoc = isJust . assocTyConArgPoss_maybe
619 setTyConArgPoss :: TyCon -> [Int] -> TyCon
620 setTyConArgPoss tc@(AlgTyCon {}) poss = tc { tyConArgPoss = Just poss }
621 setTyConArgPoss tc@(SynTyCon {}) poss = tc { tyConArgPoss = Just poss }
622 setTyConArgPoss tc _ = pprPanic "setTyConArgPoss" (ppr tc)
624 isTupleTyCon :: TyCon -> Bool
625 -- The unit tycon didn't used to be classed as a tuple tycon
626 -- but I thought that was silly so I've undone it
627 -- If it can't be for some reason, it should be a AlgTyCon
629 -- NB: when compiling Data.Tuple, the tycons won't reply True to
630 -- isTupleTyCon, becuase they are built as AlgTyCons. However they
631 -- get spat into the interface file as tuple tycons, so I don't think
633 isTupleTyCon (TupleTyCon {}) = True
634 isTupleTyCon other = False
636 isUnboxedTupleTyCon :: TyCon -> Bool
637 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
638 isUnboxedTupleTyCon other = False
640 isBoxedTupleTyCon :: TyCon -> Bool
641 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
642 isBoxedTupleTyCon other = False
644 tupleTyConBoxity tc = tyConBoxed tc
646 isRecursiveTyCon :: TyCon -> Bool
647 isRecursiveTyCon (AlgTyCon {algTcRec = Recursive}) = True
648 isRecursiveTyCon other = False
650 isHiBootTyCon :: TyCon -> Bool
651 -- Used for knot-tying in hi-boot files
652 isHiBootTyCon (AlgTyCon {algTcRhs = AbstractTyCon}) = True
653 isHiBootTyCon other = False
655 isForeignTyCon :: TyCon -> Bool
656 -- isForeignTyCon identifies foreign-imported type constructors
657 isForeignTyCon (PrimTyCon {tyConExtName = Just _}) = True
658 isForeignTyCon other = False
660 isSuperKindTyCon :: TyCon -> Bool
661 isSuperKindTyCon (SuperKindTyCon {}) = True
662 isSuperKindTyCon other = False
664 isCoercionTyCon_maybe :: TyCon -> Maybe (Arity, [Type] -> Kind)
665 isCoercionTyCon_maybe (CoercionTyCon {tyConArity = ar, coKindFun = rule})
667 isCoercionTyCon_maybe other = Nothing
669 isCoercionTyCon (CoercionTyCon {}) = True
670 isCoercionTyCon other = False
674 -----------------------------------------------
675 -- Expand type-constructor applications
676 -----------------------------------------------
679 tcExpandTyCon_maybe, coreExpandTyCon_maybe
681 -> [Type] -- Args to tycon
682 -> Maybe ([(TyVar,Type)], -- Substitution
683 Type, -- Body type (not yet substituted)
684 [Type]) -- Leftover args
686 -- For the *typechecker* view, we expand synonyms only
687 tcExpandTyCon_maybe (SynTyCon {tyConTyVars = tvs,
688 synTcRhs = SynonymTyCon rhs }) tys
690 tcExpandTyCon_maybe other_tycon tys = Nothing
693 -- For the *Core* view, we expand synonyms only as well
695 coreExpandTyCon_maybe (AlgTyCon {algTcRec = NonRecursive, -- Not recursive
696 algTcRhs = NewTyCon { nt_etad_rhs = etad_rhs, nt_co = Nothing }}) tys
697 = case etad_rhs of -- Don't do this in the pattern match, lest we accidentally
698 -- match the etad_rhs of a *recursive* newtype
699 (tvs,rhs) -> expand tvs rhs tys
701 coreExpandTyCon_maybe tycon tys = tcExpandTyCon_maybe tycon tys
705 expand :: [TyVar] -> Type -- Template
707 -> Maybe ([(TyVar,Type)], Type, [Type]) -- Expansion
709 = case n_tvs `compare` length tys of
710 LT -> Just (tvs `zip` tys, rhs, drop n_tvs tys)
711 EQ -> Just (tvs `zip` tys, rhs, [])
718 tyConHasGenerics :: TyCon -> Bool
719 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
720 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
721 tyConHasGenerics other = False -- Synonyms
723 tyConDataCons :: TyCon -> [DataCon]
724 -- It's convenient for tyConDataCons to return the
725 -- empty list for type synonyms etc
726 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
728 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
729 tyConDataCons_maybe (AlgTyCon {algTcRhs = DataTyCon { data_cons = cons }}) = Just cons
730 tyConDataCons_maybe (AlgTyCon {algTcRhs = NewTyCon { data_con = con }}) = Just [con]
731 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
732 tyConDataCons_maybe other = Nothing
734 tyConFamilySize :: TyCon -> Int
735 tyConFamilySize (AlgTyCon {algTcRhs = DataTyCon {data_cons = cons}}) =
737 tyConFamilySize (AlgTyCon {algTcRhs = NewTyCon {}}) = 1
738 tyConFamilySize (AlgTyCon {algTcRhs = OpenDataTyCon}) = 0
739 tyConFamilySize (TupleTyCon {}) = 1
741 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
744 tyConSelIds :: TyCon -> [Id]
745 tyConSelIds (AlgTyCon {algTcSelIds = fs}) = fs
746 tyConSelIds other_tycon = []
748 algTyConRhs :: TyCon -> AlgTyConRhs
749 algTyConRhs (AlgTyCon {algTcRhs = rhs}) = rhs
750 algTyConRhs (TupleTyCon {dataCon = con}) = DataTyCon { data_cons = [con], is_enum = False }
751 algTyConRhs other = pprPanic "algTyConRhs" (ppr other)
755 newTyConRhs :: TyCon -> ([TyVar], Type)
756 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rhs = rhs }}) = (tvs, rhs)
757 newTyConRhs tycon = pprPanic "newTyConRhs" (ppr tycon)
759 newTyConRep :: TyCon -> ([TyVar], Type)
760 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rep = rep }}) = (tvs, rep)
761 newTyConRep tycon = pprPanic "newTyConRep" (ppr tycon)
763 newTyConCo_maybe :: TyCon -> Maybe TyCon
764 newTyConCo_maybe (AlgTyCon {algTcRhs = NewTyCon { nt_co = co }}) = co
765 newTyConCo_maybe _ = Nothing
767 tyConPrimRep :: TyCon -> PrimRep
768 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
769 tyConPrimRep tc = ASSERT(not (isUnboxedTupleTyCon tc)) PtrRep
773 tyConStupidTheta :: TyCon -> [PredType]
774 tyConStupidTheta (AlgTyCon {algTcStupidTheta = stupid}) = stupid
775 tyConStupidTheta (TupleTyCon {}) = []
776 tyConStupidTheta tycon = pprPanic "tyConStupidTheta" (ppr tycon)
780 synTyConDefn :: TyCon -> ([TyVar], Type)
781 synTyConDefn (SynTyCon {tyConTyVars = tyvars, synTcRhs = SynonymTyCon ty})
783 synTyConDefn tycon = pprPanic "getSynTyConDefn" (ppr tycon)
785 synTyConRhs :: TyCon -> SynTyConRhs
786 synTyConRhs (SynTyCon {synTcRhs = rhs}) = rhs
787 synTyConRhs tc = pprPanic "synTyConRhs" (ppr tc)
789 synTyConType :: TyCon -> Type
790 synTyConType tc = case synTcRhs tc of
792 _ -> pprPanic "synTyConType" (ppr tc)
794 synTyConResKind :: TyCon -> Kind
795 synTyConResKind (SynTyCon {synTcRhs = OpenSynTyCon kind}) = kind
796 synTyConResKind tycon = pprPanic "synTyConResKind" (ppr tycon)
800 maybeTyConSingleCon :: TyCon -> Maybe DataCon
801 maybeTyConSingleCon (AlgTyCon {algTcRhs = DataTyCon {data_cons = [c] }}) = Just c
802 maybeTyConSingleCon (AlgTyCon {algTcRhs = NewTyCon { data_con = c }}) = Just c
803 maybeTyConSingleCon (AlgTyCon {}) = Nothing
804 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
805 maybeTyConSingleCon (PrimTyCon {}) = Nothing
806 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
807 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
811 isClassTyCon :: TyCon -> Bool
812 isClassTyCon (AlgTyCon {algTcParent = ClassTyCon _}) = True
813 isClassTyCon other_tycon = False
815 tyConClass_maybe :: TyCon -> Maybe Class
816 tyConClass_maybe (AlgTyCon {algTcParent = ClassTyCon clas}) = Just clas
817 tyConClass_maybe ther_tycon = Nothing
819 isFamInstTyCon :: TyCon -> Bool
820 isFamInstTyCon (AlgTyCon {algTcParent = FamilyTyCon _ _ _ _}) = True
821 isFamInstTyCon other_tycon = False
823 tyConFamInst_maybe :: TyCon -> Maybe (TyCon, [Type])
824 tyConFamInst_maybe (AlgTyCon {algTcParent = FamilyTyCon fam instTys _ _}) =
826 tyConFamInst_maybe ther_tycon =
829 tyConFamilyCoercion_maybe :: TyCon -> Maybe TyCon
830 tyConFamilyCoercion_maybe (AlgTyCon {algTcParent = FamilyTyCon _ _ coe _}) =
832 tyConFamilyCoercion_maybe ther_tycon =
835 tyConFamInstIndex :: TyCon -> Int
836 tyConFamInstIndex (AlgTyCon {algTcParent = FamilyTyCon _ _ _ index}) = index
837 tyConFamInstIndex _ =
838 panic "tyConFamInstIndex"
842 %************************************************************************
844 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
846 %************************************************************************
848 @TyCon@s are compared by comparing their @Unique@s.
850 The strictness analyser needs @Ord@. It is a lexicographic order with
851 the property @(a<=b) || (b<=a)@.
854 instance Eq TyCon where
855 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
856 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
858 instance Ord TyCon where
859 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
860 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
861 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
862 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
863 compare a b = getUnique a `compare` getUnique b
865 instance Uniquable TyCon where
866 getUnique tc = tyConUnique tc
868 instance Outputable TyCon where
869 ppr tc = ppr (getName tc)
871 instance NamedThing TyCon where