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 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
20 isRecursiveTyCon, newTyConRep, newTyConRhs, newTyConCo,
21 isHiBootTyCon, isSuperKindTyCon,
22 isCoercionTyCon_maybe, isCoercionTyCon,
24 tcExpandTyCon_maybe, coreExpandTyCon_maybe,
26 makeTyConAbstract, isAbstractTyCon,
28 mkForeignTyCon, isForeignTyCon,
45 algTyConRhs, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
49 isClassTyCon, tyConClass_maybe,
50 isFamInstTyCon, tyConFamInst_maybe, tyConFamilyCoercion_maybe,
51 synTyConDefn, synTyConRhs, synTyConType, synTyConResKind,
52 tyConExtName, -- External name for foreign types
60 #include "HsVersions.h"
62 import {-# SOURCE #-} TypeRep ( Kind, Type, Coercion, PredType )
63 import {-# SOURCE #-} DataCon ( DataCon, isVanillaDataCon )
65 import Var ( TyVar, Id )
66 import Class ( Class )
67 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
68 import Name ( Name, nameUnique, NamedThing(getName) )
69 import PrelNames ( Unique, Uniquable(..) )
70 import Maybes ( orElse )
75 %************************************************************************
77 \subsection{The data type}
79 %************************************************************************
84 tyConUnique :: Unique,
91 | AlgTyCon { -- Data type, and newtype decls.
92 -- All lifted, all boxed
93 tyConUnique :: Unique,
98 tyConTyVars :: [TyVar], -- Scopes over (a) the algTcStupidTheta
99 -- (b) the cached types in
100 -- algTyConRhs.NewTyCon
101 -- But not over the data constructors
102 algTcSelIds :: [Id], -- Its record selectors (empty if none)
104 algTcGadtSyntax :: Bool, -- True <=> the data type was declared using GADT syntax
105 -- That doesn't mean it's a true GADT; only that the "where"
106 -- form was used. This field is used only to guide
108 algTcStupidTheta :: [PredType], -- The "stupid theta" for the data type
109 -- (always empty for GADTs)
111 algTcRhs :: AlgTyConRhs, -- Data constructors in here
113 algTcRec :: RecFlag, -- Tells whether the data type is part
114 -- of a mutually-recursive group or not
116 hasGenerics :: Bool, -- True <=> generic to/from functions are available
117 -- (in the exports of the data type's source module)
119 algTcParent :: AlgTyConParent -- Gives the class or family tycon for
120 -- derived tycons representing classes
121 -- or family instances, respectively.
125 tyConUnique :: Unique,
129 tyConBoxed :: Boxity,
130 tyConTyVars :: [TyVar],
136 tyConUnique :: Unique,
141 tyConTyVars :: [TyVar], -- Bound tyvars
142 synTcRhs :: SynTyConRhs -- Expanded type in here
145 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
146 -- Now includes foreign-imported types
147 -- Also includes Kinds
148 tyConUnique :: Unique,
153 primTyConRep :: PrimRep,
154 -- Many primitive tycons are unboxed, but some are
155 -- boxed (represented by pointers). The CgRep tells.
157 isUnLifted :: Bool, -- Most primitive tycons are unlifted,
158 -- but foreign-imported ones may not be
159 tyConExtName :: Maybe FastString -- Just xx for foreign-imported types
162 | CoercionTyCon { -- E.g. (:=:), sym, trans, left, right
163 -- INVARIANT: coercions are always fully applied
164 tyConUnique :: Unique,
167 coKindFun :: [Type] -> Kind
170 | SuperKindTyCon { -- Super Kinds, TY (box) and CO (diamond).
171 -- They have no kind; and arity zero
172 tyConUnique :: Unique,
178 type SuperKindCon = TyCon
180 type FieldLabel = Name
183 = AbstractTyCon -- We know nothing about this data type, except
184 -- that it's represented by a pointer
185 -- Used when we export a data type abstractly into
188 | OpenDataTyCon -- data family (further instances can appear
189 | OpenNewTyCon -- newtype family at any time)
192 data_cons :: [DataCon],
193 -- The constructors; can be empty if the user declares
194 -- the type to have no constructors
195 -- INVARIANT: Kept in order of increasing tag
196 -- (see the tag assignment in DataCon.mkDataCon)
197 is_enum :: Bool -- Cached: True <=> an enumeration type
198 } -- Includes data types with no constructors.
201 data_con :: DataCon, -- The unique constructor; it has no existentials
203 nt_rhs :: Type, -- Cached: the argument type of the constructor
204 -- = the representation type of the tycon
205 -- The free tyvars of this type are the tyConTyVars
207 nt_co :: Maybe TyCon, -- The coercion used to create the newtype
208 -- from the representation
209 -- optional for non-recursive newtypes
210 -- See Note [Newtype coercions]
212 nt_etad_rhs :: ([TyVar], Type) ,
213 -- The same again, but this time eta-reduced
214 -- hence the [TyVar] which may be shorter than the declared
215 -- arity of the TyCon. See Note [Newtype eta]
217 nt_rep :: Type -- Cached: the *ultimate* representation type
218 -- By 'ultimate' I mean that the top-level constructor
219 -- of the rep type is not itself a newtype or type synonym.
220 -- The rep type isn't entirely simple:
221 -- for a recursive newtype we pick () as the rep type
224 -- This one does not need to be eta reduced; hence its
225 -- free type variables are conveniently tyConTyVars
227 -- newtype T a = MkT [(a,Int)]
228 -- The rep type is [(a,Int)]
229 -- NB: the rep type isn't necessarily the original RHS of the
230 -- newtype decl, because the rep type looks through other
233 visibleDataCons :: AlgTyConRhs -> [DataCon]
234 visibleDataCons AbstractTyCon = []
235 visibleDataCons OpenDataTyCon = []
236 visibleDataCons OpenNewTyCon = []
237 visibleDataCons (DataTyCon{ data_cons = cs }) = cs
238 visibleDataCons (NewTyCon{ data_con = c }) = [c]
240 -- Both type classes as well as data/newtype family instances imply implicit
241 -- type constructors. These implicit type constructors refer to their parent
242 -- structure (ie, the class or family from which they derive) using a type of
243 -- the following form.
245 data AlgTyConParent = -- An ordinary type constructor has no parent.
248 -- Type constructors representing a class dictionary.
251 -- Type constructors representing an instances of a type
253 | FamilyTyCon TyCon -- the type family
254 [Type] -- instance types
255 TyCon -- a *coercion* identifying
256 -- the representation type
257 -- with the type instance
260 = OpenSynTyCon Kind -- Type family: *result* kind given
261 | SynonymTyCon Type -- Mentioning head type vars. Acts as a template for
262 -- the expansion when the tycon is applied to some
266 Note [Newtype coercions]
267 ~~~~~~~~~~~~~~~~~~~~~~~~
269 The NewTyCon field nt_co is a a TyCon (a coercion constructor in fact)
270 which is used for coercing from the representation type of the
271 newtype, to the newtype itself. For example,
273 newtype T a = MkT (a -> a)
275 the NewTyCon for T will contain nt_co = CoT where CoT t : T t :=: t ->
276 t. This TyCon is a CoercionTyCon, so it does not have a kind on its
277 own; it basically has its own typing rule for the fully-applied
278 version. If the newtype T has k type variables then CoT has arity at
279 most k. In the case that the right hand side is a type application
280 ending with the same type variables as the left hand side, we
281 "eta-contract" the coercion. So if we had
283 newtype S a = MkT [a]
285 then we would generate the arity 0 coercion CoS : S :=: []. The
286 primary reason we do this is to make newtype deriving cleaner.
288 In the paper we'd write
289 axiom CoT : (forall t. T t) :=: (forall t. [t])
290 and then when we used CoT at a particular type, s, we'd say
292 which encodes as (TyConApp instCoercionTyCon [TyConApp CoT [], s])
294 But in GHC we instead make CoT into a new piece of type syntax
295 (like instCoercionTyCon, symCoercionTyCon etc), which must always
296 be saturated, but which encodes as
298 In the vocabulary of the paper it's as if we had axiom declarations
300 axiom CoT t : T t :=: [t]
305 newtype Parser m a = MkParser (Foogle m a)
306 Are these two types equal (to Core)?
309 Well, yes. But to see that easily we eta-reduce the RHS type of
310 Parser, in this case to ([], Froogle), so that even unsaturated applications
311 of Parser will work right. This eta reduction is done when the type
312 constructor is built, and cached in NewTyCon. The cached field is
313 only used in coreExpandTyCon_maybe.
315 Here's an example that I think showed up in practice
317 newtype T a = MkT [a]
318 newtype Foo m = MkFoo (forall a. m a -> Int)
324 w2 = MkFoo (\(MkT x) -> case w1 of MkFoo f -> f x)
326 After desugaring, and discading the data constructors for the newtypes,
330 And now Lint complains unless Foo T == Foo [], and that requires T==[]
333 %************************************************************************
337 %************************************************************************
339 A PrimRep is an abstraction of a type. It contains information that
340 the code generator needs in order to pass arguments, return results,
341 and store values of this type.
343 A PrimRep is somewhat similar to a CgRep (see codeGen/SMRep) and a
344 MachRep (see cmm/MachOp), although each of these types has a distinct
345 and clearly defined purpose:
347 - A PrimRep is a CgRep + information about signedness + information
348 about primitive pointers (AddrRep). Signedness and primitive
349 pointers are required when passing a primitive type to a foreign
350 function, but aren't needed for call/return conventions of Haskell
353 - A MachRep is a basic machine type (non-void, doesn't contain
354 information on pointerhood or signedness, but contains some
355 reps that don't have corresponding Haskell types).
361 | IntRep -- signed, word-sized
362 | WordRep -- unsinged, word-sized
363 | Int64Rep -- signed, 64 bit (32-bit words only)
364 | Word64Rep -- unsigned, 64 bit (32-bit words only)
365 | AddrRep -- a pointer, but not to a Haskell value
370 %************************************************************************
372 \subsection{TyCon Construction}
374 %************************************************************************
376 Note: the TyCon constructors all take a Kind as one argument, even though
377 they could, in principle, work out their Kind from their other arguments.
378 But to do so they need functions from Types, and that makes a nasty
379 module mutual-recursion. And they aren't called from many places.
380 So we compromise, and move their Kind calculation to the call site.
383 mkFunTyCon :: Name -> Kind -> TyCon
386 tyConUnique = nameUnique name,
392 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
393 -- but now you also have to pass in the generic information about the type
394 -- constructor - you can get hold of it easily (see Generics module)
395 mkAlgTyCon name kind tyvars stupid rhs sel_ids parent is_rec gen_info gadt_syn
398 tyConUnique = nameUnique name,
400 tyConArity = length tyvars,
401 tyConTyVars = tyvars,
402 algTcStupidTheta = stupid,
404 algTcSelIds = sel_ids,
405 algTcParent = parent,
407 algTcGadtSyntax = gadt_syn,
408 hasGenerics = gen_info
411 mkClassTyCon name kind tyvars rhs clas is_rec =
412 mkAlgTyCon name kind tyvars [] rhs [] (ClassTyCon clas) is_rec False False
414 mkTupleTyCon name kind arity tyvars con boxed gen_info
416 tyConUnique = nameUnique name,
421 tyConTyVars = tyvars,
423 hasGenerics = gen_info
426 -- Foreign-imported (.NET) type constructors are represented
427 -- as primitive, but *lifted*, TyCons for now. They are lifted
428 -- because the Haskell type T representing the (foreign) .NET
429 -- type T is actually implemented (in ILX) as a thunk<T>
430 mkForeignTyCon name ext_name kind arity
433 tyConUnique = nameUnique name,
436 primTyConRep = PtrRep, -- they all do
438 tyConExtName = ext_name
442 -- most Prim tycons are lifted
443 mkPrimTyCon name kind arity rep
444 = mkPrimTyCon' name kind arity rep True
446 mkVoidPrimTyCon name kind arity
447 = mkPrimTyCon' name kind arity VoidRep True
449 -- but RealWorld is lifted
450 mkLiftedPrimTyCon name kind arity rep
451 = mkPrimTyCon' name kind arity rep False
453 mkPrimTyCon' name kind arity rep is_unlifted
456 tyConUnique = nameUnique name,
460 isUnLifted = is_unlifted,
461 tyConExtName = Nothing
464 mkSynTyCon name kind tyvars rhs
467 tyConUnique = nameUnique name,
469 tyConArity = length tyvars,
470 tyConTyVars = tyvars,
474 mkCoercionTyCon name arity kindRule
477 tyConUnique = nameUnique name,
482 -- Super kinds always have arity zero
483 mkSuperKindTyCon name
486 tyConUnique = nameUnique name
491 isFunTyCon :: TyCon -> Bool
492 isFunTyCon (FunTyCon {}) = True
495 isAbstractTyCon :: TyCon -> Bool
496 isAbstractTyCon (AlgTyCon { algTcRhs = AbstractTyCon }) = True
497 isAbstractTyCon _ = False
499 makeTyConAbstract :: TyCon -> TyCon
500 makeTyConAbstract tc@(AlgTyCon {}) = tc { algTcRhs = AbstractTyCon }
501 makeTyConAbstract tc = pprPanic "makeTyConAbstract" (ppr tc)
503 isPrimTyCon :: TyCon -> Bool
504 isPrimTyCon (PrimTyCon {}) = True
505 isPrimTyCon _ = False
507 isUnLiftedTyCon :: TyCon -> Bool
508 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
509 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
510 isUnLiftedTyCon _ = False
512 -- isAlgTyCon returns True for both @data@ and @newtype@
513 isAlgTyCon :: TyCon -> Bool
514 isAlgTyCon (AlgTyCon {}) = True
515 isAlgTyCon (TupleTyCon {}) = True
516 isAlgTyCon other = False
518 isDataTyCon :: TyCon -> Bool
519 -- isDataTyCon returns True for data types that are represented by
520 -- heap-allocated constructors.
521 -- These are srcutinised by Core-level @case@ expressions, and they
522 -- get info tables allocated for them.
523 -- True for all @data@ types
524 -- False for newtypes
526 isDataTyCon tc@(AlgTyCon {algTcRhs = rhs})
528 OpenDataTyCon -> True
530 OpenNewTyCon -> False
532 AbstractTyCon -> pprPanic "isDataTyCon" (ppr tc)
534 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
535 isDataTyCon other = False
537 isNewTyCon :: TyCon -> Bool
538 isNewTyCon (AlgTyCon {algTcRhs = NewTyCon {}}) = True
539 isNewTyCon other = False
541 isProductTyCon :: TyCon -> Bool
543 -- has *one* constructor,
544 -- is *not* existential
546 -- may be DataType, NewType
547 -- may be unboxed or not,
548 -- may be recursive or not
550 isProductTyCon tc@(AlgTyCon {}) = case algTcRhs tc of
551 DataTyCon{ data_cons = [data_con] }
552 -> isVanillaDataCon data_con
555 isProductTyCon (TupleTyCon {}) = True
556 isProductTyCon other = False
558 isSynTyCon :: TyCon -> Bool
559 isSynTyCon (SynTyCon {}) = True
562 isGadtSyntaxTyCon :: TyCon -> Bool
563 isGadtSyntaxTyCon (AlgTyCon { algTcGadtSyntax = res }) = res
564 isGadtSyntaxTyCon other = False
566 isEnumerationTyCon :: TyCon -> Bool
567 isEnumerationTyCon (AlgTyCon {algTcRhs = DataTyCon { is_enum = res }}) = res
568 isEnumerationTyCon other = False
570 isOpenTyCon :: TyCon -> Bool
571 isOpenTyCon (SynTyCon {synTcRhs = OpenSynTyCon _}) = True
572 isOpenTyCon (AlgTyCon {algTcRhs = OpenDataTyCon }) = True
573 isOpenTyCon (AlgTyCon {algTcRhs = OpenNewTyCon }) = True
574 isOpenTyCon _ = False
576 isTupleTyCon :: TyCon -> Bool
577 -- The unit tycon didn't used to be classed as a tuple tycon
578 -- but I thought that was silly so I've undone it
579 -- If it can't be for some reason, it should be a AlgTyCon
581 -- NB: when compiling Data.Tuple, the tycons won't reply True to
582 -- isTupleTyCon, becuase they are built as AlgTyCons. However they
583 -- get spat into the interface file as tuple tycons, so I don't think
585 isTupleTyCon (TupleTyCon {}) = True
586 isTupleTyCon other = False
588 isUnboxedTupleTyCon :: TyCon -> Bool
589 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
590 isUnboxedTupleTyCon other = False
592 isBoxedTupleTyCon :: TyCon -> Bool
593 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
594 isBoxedTupleTyCon other = False
596 tupleTyConBoxity tc = tyConBoxed tc
598 isRecursiveTyCon :: TyCon -> Bool
599 isRecursiveTyCon (AlgTyCon {algTcRec = Recursive}) = True
600 isRecursiveTyCon other = False
602 isHiBootTyCon :: TyCon -> Bool
603 -- Used for knot-tying in hi-boot files
604 isHiBootTyCon (AlgTyCon {algTcRhs = AbstractTyCon}) = True
605 isHiBootTyCon other = False
607 isForeignTyCon :: TyCon -> Bool
608 -- isForeignTyCon identifies foreign-imported type constructors
609 isForeignTyCon (PrimTyCon {tyConExtName = Just _}) = True
610 isForeignTyCon other = False
612 isSuperKindTyCon :: TyCon -> Bool
613 isSuperKindTyCon (SuperKindTyCon {}) = True
614 isSuperKindTyCon other = False
616 isCoercionTyCon_maybe :: TyCon -> Maybe (Arity, [Type] -> Kind)
617 isCoercionTyCon_maybe (CoercionTyCon {tyConArity = ar, coKindFun = rule})
619 isCoercionTyCon_maybe other = Nothing
621 isCoercionTyCon (CoercionTyCon {}) = True
622 isCoercionTyCon other = False
626 -----------------------------------------------
627 -- Expand type-constructor applications
628 -----------------------------------------------
631 tcExpandTyCon_maybe, coreExpandTyCon_maybe
633 -> [Type] -- Args to tycon
634 -> Maybe ([(TyVar,Type)], -- Substitution
635 Type, -- Body type (not yet substituted)
636 [Type]) -- Leftover args
638 -- For the *typechecker* view, we expand synonyms only
639 tcExpandTyCon_maybe (SynTyCon {tyConTyVars = tvs,
640 synTcRhs = SynonymTyCon rhs }) tys
642 tcExpandTyCon_maybe other_tycon tys = Nothing
645 -- For the *Core* view, we expand synonyms only as well
647 coreExpandTyCon_maybe (AlgTyCon {algTcRec = NonRecursive, -- Not recursive
648 algTcRhs = NewTyCon { nt_etad_rhs = etad_rhs, nt_co = Nothing }}) tys
649 = case etad_rhs of -- Don't do this in the pattern match, lest we accidentally
650 -- match the etad_rhs of a *recursive* newtype
651 (tvs,rhs) -> expand tvs rhs tys
653 coreExpandTyCon_maybe tycon tys = tcExpandTyCon_maybe tycon tys
657 expand :: [TyVar] -> Type -- Template
659 -> Maybe ([(TyVar,Type)], Type, [Type]) -- Expansion
661 = case n_tvs `compare` length tys of
662 LT -> Just (tvs `zip` tys, rhs, drop n_tvs tys)
663 EQ -> Just (tvs `zip` tys, rhs, [])
670 tyConHasGenerics :: TyCon -> Bool
671 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
672 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
673 tyConHasGenerics other = False -- Synonyms
675 tyConDataCons :: TyCon -> [DataCon]
676 -- It's convenient for tyConDataCons to return the
677 -- empty list for type synonyms etc
678 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
680 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
681 tyConDataCons_maybe (AlgTyCon {algTcRhs = DataTyCon { data_cons = cons }}) = Just cons
682 tyConDataCons_maybe (AlgTyCon {algTcRhs = NewTyCon { data_con = con }}) = Just [con]
683 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
684 tyConDataCons_maybe other = Nothing
686 tyConFamilySize :: TyCon -> Int
687 tyConFamilySize (AlgTyCon {algTcRhs = DataTyCon {data_cons = cons}}) =
689 tyConFamilySize (AlgTyCon {algTcRhs = NewTyCon {}}) = 1
690 tyConFamilySize (AlgTyCon {algTcRhs = OpenDataTyCon}) = 0
691 tyConFamilySize (TupleTyCon {}) = 1
693 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
696 tyConSelIds :: TyCon -> [Id]
697 tyConSelIds (AlgTyCon {algTcSelIds = fs}) = fs
698 tyConSelIds other_tycon = []
700 algTyConRhs :: TyCon -> AlgTyConRhs
701 algTyConRhs (AlgTyCon {algTcRhs = rhs}) = rhs
702 algTyConRhs (TupleTyCon {dataCon = con}) = DataTyCon { data_cons = [con], is_enum = False }
703 algTyConRhs other = pprPanic "algTyConRhs" (ppr other)
707 newTyConRhs :: TyCon -> ([TyVar], Type)
708 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rhs = rhs }}) = (tvs, rhs)
709 newTyConRhs tycon = pprPanic "newTyConRhs" (ppr tycon)
711 newTyConRep :: TyCon -> ([TyVar], Type)
712 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rep = rep }}) = (tvs, rep)
713 newTyConRep tycon = pprPanic "newTyConRep" (ppr tycon)
715 newTyConCo :: TyCon -> Maybe TyCon
716 newTyConCo (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_co = co }}) = co
717 newTyConCo tycon = pprPanic "newTyConCo" (ppr tycon)
719 tyConPrimRep :: TyCon -> PrimRep
720 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
721 tyConPrimRep tc = ASSERT(not (isUnboxedTupleTyCon tc)) PtrRep
725 tyConStupidTheta :: TyCon -> [PredType]
726 tyConStupidTheta (AlgTyCon {algTcStupidTheta = stupid}) = stupid
727 tyConStupidTheta (TupleTyCon {}) = []
728 tyConStupidTheta tycon = pprPanic "tyConStupidTheta" (ppr tycon)
732 synTyConDefn :: TyCon -> ([TyVar], Type)
733 synTyConDefn (SynTyCon {tyConTyVars = tyvars, synTcRhs = SynonymTyCon ty})
735 synTyConDefn tycon = pprPanic "getSynTyConDefn" (ppr tycon)
737 synTyConRhs :: TyCon -> SynTyConRhs
738 synTyConRhs (SynTyCon {synTcRhs = rhs}) = rhs
739 synTyConRhs tc = pprPanic "synTyConRhs" (ppr tc)
741 synTyConType :: TyCon -> Type
742 synTyConType tc = case synTcRhs tc of
744 _ -> pprPanic "synTyConType" (ppr tc)
746 synTyConResKind :: TyCon -> Kind
747 synTyConResKind (SynTyCon {synTcRhs = OpenSynTyCon kind}) = kind
748 synTyConResKind tycon = pprPanic "synTyConResKind" (ppr tycon)
752 maybeTyConSingleCon :: TyCon -> Maybe DataCon
753 maybeTyConSingleCon (AlgTyCon {algTcRhs = DataTyCon {data_cons = [c] }}) = Just c
754 maybeTyConSingleCon (AlgTyCon {algTcRhs = NewTyCon { data_con = c }}) = Just c
755 maybeTyConSingleCon (AlgTyCon {}) = Nothing
756 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
757 maybeTyConSingleCon (PrimTyCon {}) = Nothing
758 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
759 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
763 isClassTyCon :: TyCon -> Bool
764 isClassTyCon (AlgTyCon {algTcParent = ClassTyCon _}) = True
765 isClassTyCon other_tycon = False
767 tyConClass_maybe :: TyCon -> Maybe Class
768 tyConClass_maybe (AlgTyCon {algTcParent = ClassTyCon clas}) = Just clas
769 tyConClass_maybe ther_tycon = Nothing
771 isFamInstTyCon :: TyCon -> Bool
772 isFamInstTyCon (AlgTyCon {algTcParent = FamilyTyCon _ _ _}) = True
773 isFamInstTyCon other_tycon = False
775 tyConFamInst_maybe :: TyCon -> Maybe (TyCon, [Type])
776 tyConFamInst_maybe (AlgTyCon {algTcParent = FamilyTyCon fam instTys _}) =
778 tyConFamInst_maybe ther_tycon =
781 tyConFamilyCoercion_maybe :: TyCon -> Maybe TyCon
782 tyConFamilyCoercion_maybe (AlgTyCon {algTcParent = FamilyTyCon _ _ coe}) =
784 tyConFamilyCoercion_maybe ther_tycon =
789 %************************************************************************
791 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
793 %************************************************************************
795 @TyCon@s are compared by comparing their @Unique@s.
797 The strictness analyser needs @Ord@. It is a lexicographic order with
798 the property @(a<=b) || (b<=a)@.
801 instance Eq TyCon where
802 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
803 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
805 instance Ord TyCon where
806 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
807 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
808 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
809 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
810 compare a b = getUnique a `compare` getUnique b
812 instance Uniquable TyCon where
813 getUnique tc = tyConUnique tc
815 instance Outputable TyCon where
816 ppr tc = ppr (getName tc)
818 instance NamedThing TyCon where