2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
16 AlgTyConRhs(..), visibleDataCons,
20 isFunTyCon, isUnLiftedTyCon, isProductTyCon,
21 isAlgTyCon, isDataTyCon, isNewTyCon, isClosedNewTyCon, isSynTyCon,
22 isClosedSynTyCon, isPrimTyCon,
23 isEnumerationTyCon, isGadtSyntaxTyCon, isOpenTyCon,
24 assocTyConArgPoss_maybe, isTyConAssoc, setTyConArgPoss,
25 isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
26 isRecursiveTyCon, newTyConRep, newTyConRhs, newTyConCo_maybe,
27 isHiBootTyCon, isSuperKindTyCon,
28 isCoercionTyCon_maybe, isCoercionTyCon,
31 tcExpandTyCon_maybe, coreExpandTyCon_maybe,
33 makeTyConAbstract, isAbstractTyCon,
35 mkForeignTyCon, isForeignTyCon,
52 algTyConRhs, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
56 isClassTyCon, tyConClass_maybe,
57 isFamInstTyCon, tyConFamInst_maybe, tyConFamilyCoercion_maybe,
58 synTyConDefn, synTyConRhs, synTyConType, synTyConResKind,
59 tyConExtName, -- External name for foreign types
67 #include "HsVersions.h"
69 import {-# SOURCE #-} TypeRep ( Kind, Type, PredType )
70 import {-# SOURCE #-} DataCon ( DataCon, isVanillaDataCon )
83 %************************************************************************
85 \subsection{The data type}
87 %************************************************************************
92 tyConUnique :: Unique,
99 | AlgTyCon { -- Data type, and newtype decls.
100 -- All lifted, all boxed
101 tyConUnique :: Unique,
106 tyConTyVars :: [TyVar], -- Scopes over (a) the algTcStupidTheta
107 -- (b) the cached types in
108 -- algTyConRhs.NewTyCon
109 -- (c) the family instance
111 -- But not over the data constructors
113 algTcSelIds :: [Id], -- Its record selectors (empty if none)
115 algTcGadtSyntax :: Bool, -- True <=> the data type was declared using GADT syntax
116 -- That doesn't mean it's a true GADT; only that the "where"
117 -- form was used. This field is used only to guide
119 algTcStupidTheta :: [PredType], -- The "stupid theta" for the data type
120 -- (always empty for GADTs)
122 algTcRhs :: AlgTyConRhs, -- Data constructors in here
124 algTcRec :: RecFlag, -- Tells whether the data type is part
125 -- of a mutually-recursive group or not
127 hasGenerics :: Bool, -- True <=> generic to/from functions are available
128 -- (in the exports of the data type's source module)
130 algTcParent :: TyConParent -- Gives the class or family tycon for
131 -- derived tycons representing classes
132 -- or family instances, respectively.
136 tyConUnique :: Unique,
140 tyConBoxed :: Boxity,
141 tyConTyVars :: [TyVar],
147 tyConUnique :: Unique,
152 tyConTyVars :: [TyVar], -- Bound tyvars
154 synTcRhs :: SynTyConRhs, -- Expanded type in here
156 synTcParent :: TyConParent -- Gives the family tycon of
157 -- representation tycons of family
162 | PrimTyCon { -- Primitive types; cannot be defined in Haskell
163 -- Now includes foreign-imported types
164 -- Also includes Kinds
165 tyConUnique :: Unique,
168 tyConArity :: Arity, -- SLPJ Oct06: I'm not sure what the significance
169 -- of the arity of a primtycon is!
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] -> (Type,Type)
186 } -- INVARAINT: coKindFun is always applied to exactly 'arity' args
187 -- E.g. for trans (c1 :: ta=tb) (c2 :: tb=tc), the coKindFun returns
188 -- the kind as a pair of types: (ta,tc)
190 | SuperKindTyCon { -- Super Kinds, TY (box) and CO (diamond).
191 -- They have no kind; and arity zero
192 tyConUnique :: Unique,
196 type FieldLabel = Name
198 -- Right hand sides of type constructors for algebraic types
202 -- We know nothing about this data type, except that it's represented by a
203 -- pointer. Used when we export a data type abstractly into an hi file.
207 -- The constructor represents an open family without a fixed right hand
208 -- side. Additional instances can appear at any time.
210 -- These are introduced by either a top level decl:
212 -- or an assoicated data type decl, in a class decl:
218 otArgPoss :: Maybe [Int]
219 -- Nothing <=> top-level indexed type family
220 -- Just ns <=> associated (not toplevel) family
221 -- In the latter case, for each tyvar in the AT decl, 'ns' gives the
222 -- position of that tyvar in the class argument list (starting from 0).
223 -- NB: Length is less than tyConArity iff higher kind signature.
228 data_cons :: [DataCon],
229 -- The constructors; can be empty if the user declares
230 -- the type to have no constructors
231 -- INVARIANT: Kept in order of increasing tag
232 -- (see the tag assignment in DataCon.mkDataCon)
233 is_enum :: Bool -- Cached: True <=> an enumeration type
234 } -- Includes data types with no constructors.
237 data_con :: DataCon, -- The unique constructor; it has no existentials
239 nt_rhs :: Type, -- Cached: the argument type of the constructor
240 -- = the representation type of the tycon
241 -- The free tyvars of this type are the tyConTyVars
243 nt_co :: Maybe TyCon, -- The coercion used to create the newtype
244 -- from the representation
245 -- optional for non-recursive newtypes
246 -- See Note [Newtype coercions]
248 nt_etad_rhs :: ([TyVar], Type) ,
249 -- The same again, but this time eta-reduced
250 -- hence the [TyVar] which may be shorter than the declared
251 -- arity of the TyCon. See Note [Newtype eta]
253 nt_rep :: Type -- Cached: the *ultimate* representation type
254 -- By 'ultimate' I mean that the top-level constructor
255 -- of the rep type is not itself a newtype or type synonym.
256 -- The rep type isn't entirely simple:
257 -- for a recursive newtype we pick () as the rep type
260 -- This one does not need to be eta reduced; hence its
261 -- free type variables are conveniently tyConTyVars
263 -- newtype T a = MkT [(a,Int)]
264 -- The rep type is [(a,Int)]
265 -- NB: the rep type isn't necessarily the original RHS of the
266 -- newtype decl, because the rep type looks through other
269 visibleDataCons :: AlgTyConRhs -> [DataCon]
270 visibleDataCons AbstractTyCon = []
271 visibleDataCons OpenTyCon {} = []
272 visibleDataCons (DataTyCon{ data_cons = cs }) = cs
273 visibleDataCons (NewTyCon{ data_con = c }) = [c]
275 -- Both type classes as well as family instances imply implicit
276 -- type constructors. These implicit type constructors refer to their parent
277 -- structure (ie, the class or family from which they derive) using a type of
278 -- the following form. We use `TyConParent' for both algebraic and synonym
279 -- types, but the variant `ClassTyCon' will only be used by algebraic tycons.
282 = NoParentTyCon -- An ordinary type constructor has no parent.
284 | ClassTyCon -- Type constructors representing a class dictionary.
285 Class -- INVARIANT: the classTyCon of this Class is the current tycon
287 | FamilyTyCon -- Type constructors representing an instance of a type
288 TyCon -- The type family
289 [Type] -- Instance types; free variables are the tyConTyVars
290 -- of the current TyCon (not the family one)
291 -- INVARIANT: the number of types matches the arity
292 -- of the family tycon
293 TyCon -- A CoercionTyCon identifying the representation
294 -- type with the type instance family.
295 -- c.f. Note [Newtype coercions]
298 -- E.g. data intance T [a] = ...
299 -- gives a representation tycon:
301 -- axiom co a :: T [a] ~ :R7T a
302 -- with :R7T's algTcParent = FamilyTyCon T [a] co
304 okParent :: Name -> TyConParent -> Bool -- Checks invariants
305 okParent tc_name NoParentTyCon = True
306 okParent tc_name (ClassTyCon cls) = tyConName (classTyCon cls) == tc_name
307 okParent tc_name (FamilyTyCon fam_tc tys co_tc) = tyConArity fam_tc == length tys
311 = OpenSynTyCon Kind -- Type family: *result* kind given
312 (Maybe [Int]) -- for associated families: for each tyvars in
313 -- the AT decl, gives the position of that
314 -- tyvar in the class argument list (starting
316 -- NB: Length is less than tyConArity
317 -- if higher kind signature.
319 | SynonymTyCon Type -- Mentioning head type vars. Acts as a template for
320 -- the expansion when the tycon is applied to some
324 Note [Newtype coercions]
325 ~~~~~~~~~~~~~~~~~~~~~~~~
327 The NewTyCon field nt_co is a a TyCon (a coercion constructor in fact)
328 which is used for coercing from the representation type of the
329 newtype, to the newtype itself. For example,
331 newtype T a = MkT (a -> a)
333 the NewTyCon for T will contain nt_co = CoT where CoT t : T t :=: t ->
334 t. This TyCon is a CoercionTyCon, so it does not have a kind on its
335 own; it basically has its own typing rule for the fully-applied
336 version. If the newtype T has k type variables then CoT has arity at
337 most k. In the case that the right hand side is a type application
338 ending with the same type variables as the left hand side, we
339 "eta-contract" the coercion. So if we had
341 newtype S a = MkT [a]
343 then we would generate the arity 0 coercion CoS : S :=: []. The
344 primary reason we do this is to make newtype deriving cleaner.
346 In the paper we'd write
347 axiom CoT : (forall t. T t) :=: (forall t. [t])
348 and then when we used CoT at a particular type, s, we'd say
350 which encodes as (TyConApp instCoercionTyCon [TyConApp CoT [], s])
352 But in GHC we instead make CoT into a new piece of type syntax, CoercionTyCon,
353 (like instCoercionTyCon, symCoercionTyCon etc), which must always
354 be saturated, but which encodes as
356 In the vocabulary of the paper it's as if we had axiom declarations
358 axiom CoT t : T t :=: [t]
363 newtype Parser m a = MkParser (Foogle m a)
364 Are these two types equal (to Core)?
367 Well, yes. But to see that easily we eta-reduce the RHS type of
368 Parser, in this case to ([], Froogle), so that even unsaturated applications
369 of Parser will work right. This eta reduction is done when the type
370 constructor is built, and cached in NewTyCon. The cached field is
371 only used in coreExpandTyCon_maybe.
373 Here's an example that I think showed up in practice
375 newtype T a = MkT [a]
376 newtype Foo m = MkFoo (forall a. m a -> Int)
382 w2 = MkFoo (\(MkT x) -> case w1 of MkFoo f -> f x)
384 After desugaring, and discading the data constructors for the newtypes,
388 And now Lint complains unless Foo T == Foo [], and that requires T==[]
391 Note [Indexed data types] (aka data type families)
392 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
393 See also Note [Wrappers for data instance tycons] in MkId.lhs
398 data instance T (b,c) where
399 T1 :: b -> c -> T (b,c)
402 * T is the "family TyCon"
404 * We make "representation TyCon" :R1T, thus:
406 T1 :: forall b c. b -> c -> :R1T b c
408 * It has a top-level coercion connecting it to the family TyCon
410 axiom :Co:R1T b c : T (b,c) ~ :R1T b c
412 * The data contructor T1 has a wrapper (which is what the source-level
415 $WT1 :: forall b c. b -> c -> T (b,c)
416 $WT1 b c (x::b) (y::c) = T1 b c x y `cast` sym (:Co:R1T b c)
418 * The representation TyCon :R1T has an AlgTyConParent of
420 FamilyTyCon T [(b,c)] :Co:R1T
424 %************************************************************************
428 %************************************************************************
430 A PrimRep is an abstraction of a type. It contains information that
431 the code generator needs in order to pass arguments, return results,
432 and store values of this type.
434 A PrimRep is somewhat similar to a CgRep (see codeGen/SMRep) and a
435 MachRep (see cmm/MachOp), although each of these types has a distinct
436 and clearly defined purpose:
438 - A PrimRep is a CgRep + information about signedness + information
439 about primitive pointers (AddrRep). Signedness and primitive
440 pointers are required when passing a primitive type to a foreign
441 function, but aren't needed for call/return conventions of Haskell
444 - A MachRep is a basic machine type (non-void, doesn't contain
445 information on pointerhood or signedness, but contains some
446 reps that don't have corresponding Haskell types).
452 | IntRep -- signed, word-sized
453 | WordRep -- unsinged, word-sized
454 | Int64Rep -- signed, 64 bit (32-bit words only)
455 | Word64Rep -- unsigned, 64 bit (32-bit words only)
456 | AddrRep -- a pointer, but not to a Haskell value
460 -- Size of a PrimRep, in bytes
461 sizeofPrimRep :: PrimRep -> Int
462 sizeofPrimRep IntRep = wORD_SIZE
463 sizeofPrimRep WordRep = wORD_SIZE
464 sizeofPrimRep Int64Rep = wORD64_SIZE
465 sizeofPrimRep Word64Rep= wORD64_SIZE
466 sizeofPrimRep FloatRep = 4
467 sizeofPrimRep DoubleRep= 8
468 sizeofPrimRep AddrRep = wORD_SIZE
469 sizeofPrimRep PtrRep = wORD_SIZE
470 sizeofPrimRep VoidRep = 0
473 %************************************************************************
475 \subsection{TyCon Construction}
477 %************************************************************************
479 Note: the TyCon constructors all take a Kind as one argument, even though
480 they could, in principle, work out their Kind from their other arguments.
481 But to do so they need functions from Types, and that makes a nasty
482 module mutual-recursion. And they aren't called from many places.
483 So we compromise, and move their Kind calculation to the call site.
486 mkFunTyCon :: Name -> Kind -> TyCon
489 tyConUnique = nameUnique name,
495 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
496 -- but now you also have to pass in the generic information about the type
497 -- constructor - you can get hold of it easily (see Generics module)
498 mkAlgTyCon name kind tyvars stupid rhs sel_ids parent is_rec gen_info gadt_syn
501 tyConUnique = nameUnique name,
503 tyConArity = length tyvars,
504 tyConTyVars = tyvars,
505 algTcStupidTheta = stupid,
507 algTcSelIds = sel_ids,
508 algTcParent = ASSERT( okParent name parent ) parent,
510 algTcGadtSyntax = gadt_syn,
511 hasGenerics = gen_info
514 mkClassTyCon name kind tyvars rhs clas is_rec =
515 mkAlgTyCon name kind tyvars [] rhs [] (ClassTyCon clas) is_rec False False
517 mkTupleTyCon name kind arity tyvars con boxed gen_info
519 tyConUnique = nameUnique name,
524 tyConTyVars = tyvars,
526 hasGenerics = gen_info
529 -- Foreign-imported (.NET) type constructors are represented
530 -- as primitive, but *lifted*, TyCons for now. They are lifted
531 -- because the Haskell type T representing the (foreign) .NET
532 -- type T is actually implemented (in ILX) as a thunk<T>
533 mkForeignTyCon name ext_name kind arity
536 tyConUnique = nameUnique name,
539 primTyConRep = PtrRep, -- they all do
541 tyConExtName = ext_name
545 -- most Prim tycons are lifted
546 mkPrimTyCon name kind arity rep
547 = mkPrimTyCon' name kind arity rep True
549 mkVoidPrimTyCon name kind arity
550 = mkPrimTyCon' name kind arity VoidRep True
552 -- but RealWorld is lifted
553 mkLiftedPrimTyCon name kind arity rep
554 = mkPrimTyCon' name kind arity rep False
556 mkPrimTyCon' name kind arity rep is_unlifted
559 tyConUnique = nameUnique name,
563 isUnLifted = is_unlifted,
564 tyConExtName = Nothing
567 mkSynTyCon name kind tyvars rhs parent
570 tyConUnique = nameUnique name,
572 tyConArity = length tyvars,
573 tyConTyVars = tyvars,
578 mkCoercionTyCon name arity kindRule
581 tyConUnique = nameUnique name,
586 -- Super kinds always have arity zero
587 mkSuperKindTyCon name
590 tyConUnique = nameUnique name
595 isFunTyCon :: TyCon -> Bool
596 isFunTyCon (FunTyCon {}) = True
599 isAbstractTyCon :: TyCon -> Bool
600 isAbstractTyCon (AlgTyCon { algTcRhs = AbstractTyCon }) = True
601 isAbstractTyCon _ = False
603 makeTyConAbstract :: TyCon -> TyCon
604 makeTyConAbstract tc@(AlgTyCon {}) = tc { algTcRhs = AbstractTyCon }
605 makeTyConAbstract tc = pprPanic "makeTyConAbstract" (ppr tc)
607 isPrimTyCon :: TyCon -> Bool
608 isPrimTyCon (PrimTyCon {}) = True
609 isPrimTyCon _ = False
611 isUnLiftedTyCon :: TyCon -> Bool
612 isUnLiftedTyCon (PrimTyCon {isUnLifted = is_unlifted}) = is_unlifted
613 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
614 isUnLiftedTyCon _ = False
616 -- isAlgTyCon returns True for both @data@ and @newtype@
617 isAlgTyCon :: TyCon -> Bool
618 isAlgTyCon (AlgTyCon {}) = True
619 isAlgTyCon (TupleTyCon {}) = True
620 isAlgTyCon other = False
622 isDataTyCon :: TyCon -> Bool
623 -- isDataTyCon returns True for data types that are definitely
624 -- represented by heap-allocated constructors.
625 -- These are srcutinised by Core-level @case@ expressions, and they
626 -- get info tables allocated for them.
627 -- True for all @data@ types
628 -- False for newtypes
632 -- NB: for a data type family, T, only the *instance* tycons are
633 -- get an info table etc. The family tycon does not.
634 -- Hence False for OpenTyCon
635 isDataTyCon tc@(AlgTyCon {algTcRhs = rhs})
637 OpenTyCon {} -> False
640 AbstractTyCon -> False -- We don't know, so return False
641 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
642 isDataTyCon other = False
644 isNewTyCon :: TyCon -> Bool
645 isNewTyCon (AlgTyCon {algTcRhs = rhs}) =
649 isNewTyCon other = False
651 -- This is an important refinement as typical newtype optimisations do *not*
652 -- hold for newtype families. Why? Given a type `T a', if T is a newtype
653 -- family, there is no unique right hand side by which `T a' can be replaced
656 isClosedNewTyCon :: TyCon -> Bool
657 isClosedNewTyCon tycon = isNewTyCon tycon && not (isOpenTyCon tycon)
659 isProductTyCon :: TyCon -> Bool
661 -- has *one* constructor,
662 -- is *not* existential
664 -- may be DataType, NewType
665 -- may be unboxed or not,
666 -- may be recursive or not
668 isProductTyCon tc@(AlgTyCon {}) = case algTcRhs tc of
669 DataTyCon{ data_cons = [data_con] }
670 -> isVanillaDataCon data_con
673 isProductTyCon (TupleTyCon {}) = True
674 isProductTyCon other = False
676 isSynTyCon :: TyCon -> Bool
677 isSynTyCon (SynTyCon {}) = True
680 -- As for newtypes, it is in some contexts important to distinguish between
681 -- closed synonyms and synonym families, as synonym families have no unique
682 -- right hand side to which a synonym family application can expand.
684 isClosedSynTyCon :: TyCon -> Bool
685 isClosedSynTyCon tycon = isSynTyCon tycon && not (isOpenTyCon tycon)
687 isGadtSyntaxTyCon :: TyCon -> Bool
688 isGadtSyntaxTyCon (AlgTyCon { algTcGadtSyntax = res }) = res
689 isGadtSyntaxTyCon other = False
691 isEnumerationTyCon :: TyCon -> Bool
692 isEnumerationTyCon (AlgTyCon {algTcRhs = DataTyCon { is_enum = res }}) = res
693 isEnumerationTyCon other = False
695 isOpenTyCon :: TyCon -> Bool
696 isOpenTyCon (SynTyCon {synTcRhs = OpenSynTyCon _ _}) = True
697 isOpenTyCon (AlgTyCon {algTcRhs = OpenTyCon {} }) = True
698 isOpenTyCon _ = False
700 assocTyConArgPoss_maybe :: TyCon -> Maybe [Int]
701 assocTyConArgPoss_maybe (AlgTyCon {
702 algTcRhs = OpenTyCon {otArgPoss = poss}}) = poss
703 assocTyConArgPoss_maybe (SynTyCon { synTcRhs = OpenSynTyCon _ poss }) = poss
704 assocTyConArgPoss_maybe _ = Nothing
706 isTyConAssoc :: TyCon -> Bool
707 isTyConAssoc = isJust . assocTyConArgPoss_maybe
709 setTyConArgPoss :: TyCon -> [Int] -> TyCon
710 setTyConArgPoss tc@(AlgTyCon { algTcRhs = rhs }) poss =
711 tc { algTcRhs = rhs {otArgPoss = Just poss} }
712 setTyConArgPoss tc@(SynTyCon { synTcRhs = OpenSynTyCon ki _ }) poss =
713 tc { synTcRhs = OpenSynTyCon ki (Just poss) }
714 setTyConArgPoss tc _ = pprPanic "setTyConArgPoss" (ppr tc)
716 isTupleTyCon :: TyCon -> Bool
717 -- The unit tycon didn't used to be classed as a tuple tycon
718 -- but I thought that was silly so I've undone it
719 -- If it can't be for some reason, it should be a AlgTyCon
721 -- NB: when compiling Data.Tuple, the tycons won't reply True to
722 -- isTupleTyCon, becuase they are built as AlgTyCons. However they
723 -- get spat into the interface file as tuple tycons, so I don't think
725 isTupleTyCon (TupleTyCon {}) = True
726 isTupleTyCon other = False
728 isUnboxedTupleTyCon :: TyCon -> Bool
729 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
730 isUnboxedTupleTyCon other = False
732 isBoxedTupleTyCon :: TyCon -> Bool
733 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
734 isBoxedTupleTyCon other = False
736 tupleTyConBoxity tc = tyConBoxed tc
738 isRecursiveTyCon :: TyCon -> Bool
739 isRecursiveTyCon (AlgTyCon {algTcRec = Recursive}) = True
740 isRecursiveTyCon other = False
742 isHiBootTyCon :: TyCon -> Bool
743 -- Used for knot-tying in hi-boot files
744 isHiBootTyCon (AlgTyCon {algTcRhs = AbstractTyCon}) = True
745 isHiBootTyCon other = False
747 isForeignTyCon :: TyCon -> Bool
748 -- isForeignTyCon identifies foreign-imported type constructors
749 isForeignTyCon (PrimTyCon {tyConExtName = Just _}) = True
750 isForeignTyCon other = False
752 isSuperKindTyCon :: TyCon -> Bool
753 isSuperKindTyCon (SuperKindTyCon {}) = True
754 isSuperKindTyCon other = False
756 isCoercionTyCon_maybe :: TyCon -> Maybe (Arity, [Type] -> (Type,Type))
757 isCoercionTyCon_maybe (CoercionTyCon {tyConArity = ar, coKindFun = rule})
759 isCoercionTyCon_maybe other = Nothing
761 isCoercionTyCon :: TyCon -> Bool
762 isCoercionTyCon (CoercionTyCon {}) = True
763 isCoercionTyCon other = False
765 -- Identifies implicit tycons that, in particular, do not go into interface
766 -- files (because they are implicitly reconstructed when the interface is
771 -- * associated families are implicit, as they are re-constructed from
772 -- the class declaration in which they reside, and
773 -- * family instances are *not* implicit as they represent the instance body
774 -- (similar to a dfun does that for a class instance).
776 isImplicitTyCon :: TyCon -> Bool
777 isImplicitTyCon tycon | isTyConAssoc tycon = True
778 | isSynTyCon tycon = False
779 | isAlgTyCon tycon = isClassTyCon tycon ||
781 isImplicitTyCon _other = True
782 -- catches: FunTyCon, PrimTyCon,
783 -- CoercionTyCon, SuperKindTyCon
787 -----------------------------------------------
788 -- Expand type-constructor applications
789 -----------------------------------------------
792 tcExpandTyCon_maybe, coreExpandTyCon_maybe
794 -> [Type] -- Args to tycon
795 -> Maybe ([(TyVar,Type)], -- Substitution
796 Type, -- Body type (not yet substituted)
797 [Type]) -- Leftover args
799 -- For the *typechecker* view, we expand synonyms only
800 tcExpandTyCon_maybe (SynTyCon {tyConTyVars = tvs,
801 synTcRhs = SynonymTyCon rhs }) tys
803 tcExpandTyCon_maybe other_tycon tys = Nothing
806 -- For the *Core* view, we expand synonyms only as well
808 coreExpandTyCon_maybe (AlgTyCon {algTcRec = NonRecursive, -- Not recursive
809 algTcRhs = NewTyCon { nt_etad_rhs = etad_rhs, nt_co = Nothing }}) tys
810 = case etad_rhs of -- Don't do this in the pattern match, lest we accidentally
811 -- match the etad_rhs of a *recursive* newtype
812 (tvs,rhs) -> expand tvs rhs tys
814 coreExpandTyCon_maybe tycon tys = tcExpandTyCon_maybe tycon tys
818 expand :: [TyVar] -> Type -- Template
820 -> Maybe ([(TyVar,Type)], Type, [Type]) -- Expansion
822 = case n_tvs `compare` length tys of
823 LT -> Just (tvs `zip` tys, rhs, drop n_tvs tys)
824 EQ -> Just (tvs `zip` tys, rhs, [])
831 tyConHasGenerics :: TyCon -> Bool
832 tyConHasGenerics (AlgTyCon {hasGenerics = hg}) = hg
833 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
834 tyConHasGenerics other = False -- Synonyms
836 tyConDataCons :: TyCon -> [DataCon]
837 -- It's convenient for tyConDataCons to return the
838 -- empty list for type synonyms etc
839 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
841 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
842 tyConDataCons_maybe (AlgTyCon {algTcRhs = DataTyCon { data_cons = cons }}) = Just cons
843 tyConDataCons_maybe (AlgTyCon {algTcRhs = NewTyCon { data_con = con }}) = Just [con]
844 tyConDataCons_maybe (TupleTyCon {dataCon = con}) = Just [con]
845 tyConDataCons_maybe other = Nothing
847 tyConFamilySize :: TyCon -> Int
848 tyConFamilySize (AlgTyCon {algTcRhs = DataTyCon {data_cons = cons}}) =
850 tyConFamilySize (AlgTyCon {algTcRhs = NewTyCon {}}) = 1
851 tyConFamilySize (AlgTyCon {algTcRhs = OpenTyCon {}}) = 0
852 tyConFamilySize (TupleTyCon {}) = 1
854 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
857 tyConSelIds :: TyCon -> [Id]
858 tyConSelIds (AlgTyCon {algTcSelIds = fs}) = fs
859 tyConSelIds other_tycon = []
861 algTyConRhs :: TyCon -> AlgTyConRhs
862 algTyConRhs (AlgTyCon {algTcRhs = rhs}) = rhs
863 algTyConRhs (TupleTyCon {dataCon = con}) = DataTyCon { data_cons = [con], is_enum = False }
864 algTyConRhs other = pprPanic "algTyConRhs" (ppr other)
868 newTyConRhs :: TyCon -> ([TyVar], Type)
869 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rhs = rhs }}) = (tvs, rhs)
870 newTyConRhs tycon = pprPanic "newTyConRhs" (ppr tycon)
872 newTyConRep :: TyCon -> ([TyVar], Type)
873 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rep = rep }}) = (tvs, rep)
874 newTyConRep tycon = pprPanic "newTyConRep" (ppr tycon)
876 newTyConCo_maybe :: TyCon -> Maybe TyCon
877 newTyConCo_maybe (AlgTyCon {algTcRhs = NewTyCon { nt_co = co }}) = co
878 newTyConCo_maybe _ = Nothing
880 tyConPrimRep :: TyCon -> PrimRep
881 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
882 tyConPrimRep tc = ASSERT(not (isUnboxedTupleTyCon tc)) PtrRep
886 tyConStupidTheta :: TyCon -> [PredType]
887 tyConStupidTheta (AlgTyCon {algTcStupidTheta = stupid}) = stupid
888 tyConStupidTheta (TupleTyCon {}) = []
889 tyConStupidTheta tycon = pprPanic "tyConStupidTheta" (ppr tycon)
893 synTyConDefn :: TyCon -> ([TyVar], Type)
894 synTyConDefn (SynTyCon {tyConTyVars = tyvars, synTcRhs = SynonymTyCon ty})
896 synTyConDefn tycon = pprPanic "getSynTyConDefn" (ppr tycon)
898 synTyConRhs :: TyCon -> SynTyConRhs
899 synTyConRhs (SynTyCon {synTcRhs = rhs}) = rhs
900 synTyConRhs tc = pprPanic "synTyConRhs" (ppr tc)
902 synTyConType :: TyCon -> Type
903 synTyConType tc = case synTcRhs tc of
905 _ -> pprPanic "synTyConType" (ppr tc)
907 synTyConResKind :: TyCon -> Kind
908 synTyConResKind (SynTyCon {synTcRhs = OpenSynTyCon kind _}) = kind
909 synTyConResKind tycon = pprPanic "synTyConResKind" (ppr tycon)
913 maybeTyConSingleCon :: TyCon -> Maybe DataCon
914 maybeTyConSingleCon (AlgTyCon {algTcRhs = DataTyCon {data_cons = [c] }}) = Just c
915 maybeTyConSingleCon (AlgTyCon {algTcRhs = NewTyCon { data_con = c }}) = Just c
916 maybeTyConSingleCon (AlgTyCon {}) = Nothing
917 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
918 maybeTyConSingleCon (PrimTyCon {}) = Nothing
919 maybeTyConSingleCon (FunTyCon {}) = Nothing -- case at funty
920 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
924 isClassTyCon :: TyCon -> Bool
925 isClassTyCon (AlgTyCon {algTcParent = ClassTyCon _}) = True
926 isClassTyCon other_tycon = False
928 tyConClass_maybe :: TyCon -> Maybe Class
929 tyConClass_maybe (AlgTyCon {algTcParent = ClassTyCon clas}) = Just clas
930 tyConClass_maybe other_tycon = Nothing
932 isFamInstTyCon :: TyCon -> Bool
933 isFamInstTyCon (AlgTyCon {algTcParent = FamilyTyCon _ _ _ }) = True
934 isFamInstTyCon (SynTyCon {synTcParent = FamilyTyCon _ _ _ }) = True
935 isFamInstTyCon other_tycon = False
937 tyConFamInst_maybe :: TyCon -> Maybe (TyCon, [Type])
938 tyConFamInst_maybe (AlgTyCon {algTcParent = FamilyTyCon fam instTys _}) =
940 tyConFamInst_maybe (SynTyCon {synTcParent = FamilyTyCon fam instTys _}) =
942 tyConFamInst_maybe other_tycon =
945 tyConFamilyCoercion_maybe :: TyCon -> Maybe TyCon
946 tyConFamilyCoercion_maybe (AlgTyCon {algTcParent = FamilyTyCon _ _ coe}) =
948 tyConFamilyCoercion_maybe (SynTyCon {synTcParent = FamilyTyCon _ _ coe}) =
950 tyConFamilyCoercion_maybe other_tycon =
955 %************************************************************************
957 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
959 %************************************************************************
961 @TyCon@s are compared by comparing their @Unique@s.
963 The strictness analyser needs @Ord@. It is a lexicographic order with
964 the property @(a<=b) || (b<=a)@.
967 instance Eq TyCon where
968 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
969 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
971 instance Ord TyCon where
972 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
973 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
974 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
975 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
976 compare a b = getUnique a `compare` getUnique b
978 instance Uniquable TyCon where
979 getUnique tc = tyConUnique tc
981 instance Outputable TyCon where
982 ppr tc = ppr (getName tc)
984 instance NamedThing TyCon where