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)
553 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
554 isDataTyCon other = False
556 isNewTyCon :: TyCon -> Bool
557 isNewTyCon (AlgTyCon {algTcRhs = rhs}) = case rhs of
561 isNewTyCon other = False
563 isProductTyCon :: TyCon -> Bool
565 -- has *one* constructor,
566 -- is *not* existential
568 -- may be DataType, NewType
569 -- may be unboxed or not,
570 -- may be recursive or not
572 isProductTyCon tc@(AlgTyCon {}) = case algTcRhs tc of
573 DataTyCon{ data_cons = [data_con] }
574 -> isVanillaDataCon data_con
577 isProductTyCon (TupleTyCon {}) = True
578 isProductTyCon other = False
580 isSynTyCon :: TyCon -> Bool
581 isSynTyCon (SynTyCon {}) = True
584 isGadtSyntaxTyCon :: TyCon -> Bool
585 isGadtSyntaxTyCon (AlgTyCon { algTcGadtSyntax = res }) = res
586 isGadtSyntaxTyCon other = False
588 isEnumerationTyCon :: TyCon -> Bool
589 isEnumerationTyCon (AlgTyCon {algTcRhs = DataTyCon { is_enum = res }}) = res
590 isEnumerationTyCon other = False
592 isOpenTyCon :: TyCon -> Bool
593 isOpenTyCon (SynTyCon {synTcRhs = OpenSynTyCon _}) = True
594 isOpenTyCon (AlgTyCon {algTcRhs = OpenDataTyCon }) = True
595 isOpenTyCon (AlgTyCon {algTcRhs = OpenNewTyCon }) = True
596 isOpenTyCon _ = False
598 assocTyConArgPoss_maybe :: TyCon -> Maybe [Int]
599 assocTyConArgPoss_maybe (AlgTyCon { tyConArgPoss = poss }) = poss
600 assocTyConArgPoss_maybe (SynTyCon { tyConArgPoss = poss }) = poss
601 assocTyConArgPoss_maybe _ = Nothing
603 isTyConAssoc :: TyCon -> Bool
604 isTyConAssoc = isJust . assocTyConArgPoss_maybe
606 setTyConArgPoss :: TyCon -> [Int] -> TyCon
607 setTyConArgPoss tc@(AlgTyCon {}) poss = tc { tyConArgPoss = Just poss }
608 setTyConArgPoss tc@(SynTyCon {}) poss = tc { tyConArgPoss = Just poss }
609 setTyConArgPoss tc _ = pprPanic "setTyConArgPoss" (ppr tc)
611 isTupleTyCon :: TyCon -> Bool
612 -- The unit tycon didn't used to be classed as a tuple tycon
613 -- but I thought that was silly so I've undone it
614 -- If it can't be for some reason, it should be a AlgTyCon
616 -- NB: when compiling Data.Tuple, the tycons won't reply True to
617 -- isTupleTyCon, becuase they are built as AlgTyCons. However they
618 -- get spat into the interface file as tuple tycons, so I don't think
620 isTupleTyCon (TupleTyCon {}) = True
621 isTupleTyCon other = False
623 isUnboxedTupleTyCon :: TyCon -> Bool
624 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
625 isUnboxedTupleTyCon other = False
627 isBoxedTupleTyCon :: TyCon -> Bool
628 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
629 isBoxedTupleTyCon other = False
631 tupleTyConBoxity tc = tyConBoxed tc
633 isRecursiveTyCon :: TyCon -> Bool
634 isRecursiveTyCon (AlgTyCon {algTcRec = Recursive}) = True
635 isRecursiveTyCon other = False
637 isHiBootTyCon :: TyCon -> Bool
638 -- Used for knot-tying in hi-boot files
639 isHiBootTyCon (AlgTyCon {algTcRhs = AbstractTyCon}) = True
640 isHiBootTyCon other = False
642 isForeignTyCon :: TyCon -> Bool
643 -- isForeignTyCon identifies foreign-imported type constructors
644 isForeignTyCon (PrimTyCon {tyConExtName = Just _}) = True
645 isForeignTyCon other = False
647 isSuperKindTyCon :: TyCon -> Bool
648 isSuperKindTyCon (SuperKindTyCon {}) = True
649 isSuperKindTyCon other = False
651 isCoercionTyCon_maybe :: TyCon -> Maybe (Arity, [Type] -> Kind)
652 isCoercionTyCon_maybe (CoercionTyCon {tyConArity = ar, coKindFun = rule})
654 isCoercionTyCon_maybe other = Nothing
656 isCoercionTyCon (CoercionTyCon {}) = True
657 isCoercionTyCon other = False
661 -----------------------------------------------
662 -- Expand type-constructor applications
663 -----------------------------------------------
666 tcExpandTyCon_maybe, coreExpandTyCon_maybe
668 -> [Type] -- Args to tycon
669 -> Maybe ([(TyVar,Type)], -- Substitution
670 Type, -- Body type (not yet substituted)
671 [Type]) -- Leftover args
673 -- For the *typechecker* view, we expand synonyms only
674 tcExpandTyCon_maybe (SynTyCon {tyConTyVars = tvs,
675 synTcRhs = SynonymTyCon rhs }) tys
677 tcExpandTyCon_maybe other_tycon tys = Nothing
680 -- For the *Core* view, we expand synonyms only as well
682 coreExpandTyCon_maybe (AlgTyCon {algTcRec = NonRecursive, -- Not recursive
683 algTcRhs = NewTyCon { nt_etad_rhs = etad_rhs, nt_co = Nothing }}) tys
684 = case etad_rhs of -- Don't do this in the pattern match, lest we accidentally
685 -- match the etad_rhs of a *recursive* newtype
686 (tvs,rhs) -> expand tvs rhs tys
688 coreExpandTyCon_maybe tycon tys = tcExpandTyCon_maybe tycon tys
692 expand :: [TyVar] -> Type -- Template
694 -> Maybe ([(TyVar,Type)], Type, [Type]) -- Expansion
696 = case n_tvs `compare` length tys of
697 LT -> Just (tvs `zip` tys, rhs, drop n_tvs tys)
698 EQ -> Just (tvs `zip` tys, rhs, [])
705 tyConHasGenerics :: TyCon -> Bool
706 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
707 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
708 tyConHasGenerics other = False -- Synonyms
710 tyConDataCons :: TyCon -> [DataCon]
711 -- It's convenient for tyConDataCons to return the
712 -- empty list for type synonyms etc
713 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
715 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
716 tyConDataCons_maybe (AlgTyCon {algTcRhs = DataTyCon { data_cons = cons }}) = Just cons
717 tyConDataCons_maybe (AlgTyCon {algTcRhs = NewTyCon { data_con = con }}) = Just [con]
718 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
719 tyConDataCons_maybe other = Nothing
721 tyConFamilySize :: TyCon -> Int
722 tyConFamilySize (AlgTyCon {algTcRhs = DataTyCon {data_cons = cons}}) =
724 tyConFamilySize (AlgTyCon {algTcRhs = NewTyCon {}}) = 1
725 tyConFamilySize (AlgTyCon {algTcRhs = OpenDataTyCon}) = 0
726 tyConFamilySize (TupleTyCon {}) = 1
728 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
731 tyConSelIds :: TyCon -> [Id]
732 tyConSelIds (AlgTyCon {algTcSelIds = fs}) = fs
733 tyConSelIds other_tycon = []
735 algTyConRhs :: TyCon -> AlgTyConRhs
736 algTyConRhs (AlgTyCon {algTcRhs = rhs}) = rhs
737 algTyConRhs (TupleTyCon {dataCon = con}) = DataTyCon { data_cons = [con], is_enum = False }
738 algTyConRhs other = pprPanic "algTyConRhs" (ppr other)
742 newTyConRhs :: TyCon -> ([TyVar], Type)
743 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rhs = rhs }}) = (tvs, rhs)
744 newTyConRhs tycon = pprPanic "newTyConRhs" (ppr tycon)
746 newTyConRep :: TyCon -> ([TyVar], Type)
747 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rep = rep }}) = (tvs, rep)
748 newTyConRep tycon = pprPanic "newTyConRep" (ppr tycon)
750 newTyConCo :: TyCon -> Maybe TyCon
751 newTyConCo (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_co = co }})
753 newTyConCo (AlgTyCon {tyConTyVars = tvs, algTcRhs = OpenNewTyCon})
755 newTyConCo tycon = pprPanic "newTyConCo" (ppr tycon)
757 tyConPrimRep :: TyCon -> PrimRep
758 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
759 tyConPrimRep tc = ASSERT(not (isUnboxedTupleTyCon tc)) PtrRep
763 tyConStupidTheta :: TyCon -> [PredType]
764 tyConStupidTheta (AlgTyCon {algTcStupidTheta = stupid}) = stupid
765 tyConStupidTheta (TupleTyCon {}) = []
766 tyConStupidTheta tycon = pprPanic "tyConStupidTheta" (ppr tycon)
770 synTyConDefn :: TyCon -> ([TyVar], Type)
771 synTyConDefn (SynTyCon {tyConTyVars = tyvars, synTcRhs = SynonymTyCon ty})
773 synTyConDefn tycon = pprPanic "getSynTyConDefn" (ppr tycon)
775 synTyConRhs :: TyCon -> SynTyConRhs
776 synTyConRhs (SynTyCon {synTcRhs = rhs}) = rhs
777 synTyConRhs tc = pprPanic "synTyConRhs" (ppr tc)
779 synTyConType :: TyCon -> Type
780 synTyConType tc = case synTcRhs tc of
782 _ -> pprPanic "synTyConType" (ppr tc)
784 synTyConResKind :: TyCon -> Kind
785 synTyConResKind (SynTyCon {synTcRhs = OpenSynTyCon kind}) = kind
786 synTyConResKind tycon = pprPanic "synTyConResKind" (ppr tycon)
790 maybeTyConSingleCon :: TyCon -> Maybe DataCon
791 maybeTyConSingleCon (AlgTyCon {algTcRhs = DataTyCon {data_cons = [c] }}) = Just c
792 maybeTyConSingleCon (AlgTyCon {algTcRhs = NewTyCon { data_con = c }}) = Just c
793 maybeTyConSingleCon (AlgTyCon {}) = Nothing
794 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
795 maybeTyConSingleCon (PrimTyCon {}) = Nothing
796 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
797 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
801 isClassTyCon :: TyCon -> Bool
802 isClassTyCon (AlgTyCon {algTcParent = ClassTyCon _}) = True
803 isClassTyCon other_tycon = False
805 tyConClass_maybe :: TyCon -> Maybe Class
806 tyConClass_maybe (AlgTyCon {algTcParent = ClassTyCon clas}) = Just clas
807 tyConClass_maybe ther_tycon = Nothing
809 isFamInstTyCon :: TyCon -> Bool
810 isFamInstTyCon (AlgTyCon {algTcParent = FamilyTyCon _ _ _}) = True
811 isFamInstTyCon other_tycon = False
813 tyConFamInst_maybe :: TyCon -> Maybe (TyCon, [Type])
814 tyConFamInst_maybe (AlgTyCon {algTcParent = FamilyTyCon fam instTys _}) =
816 tyConFamInst_maybe ther_tycon =
819 tyConFamilyCoercion_maybe :: TyCon -> Maybe TyCon
820 tyConFamilyCoercion_maybe (AlgTyCon {algTcParent = FamilyTyCon _ _ coe}) =
822 tyConFamilyCoercion_maybe ther_tycon =
827 %************************************************************************
829 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
831 %************************************************************************
833 @TyCon@s are compared by comparing their @Unique@s.
835 The strictness analyser needs @Ord@. It is a lexicographic order with
836 the property @(a<=b) || (b<=a)@.
839 instance Eq TyCon where
840 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
841 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
843 instance Ord TyCon where
844 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
845 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
846 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
847 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
848 compare a b = getUnique a `compare` getUnique b
850 instance Uniquable TyCon where
851 getUnique tc = tyConUnique tc
853 instance Outputable TyCon where
854 ppr tc = ppr (getName tc)
856 instance NamedThing TyCon where