99afac952bf4b622621327ba37b085ccceb19970
[ghc-hetmet.git] / compiler / types / TyCon.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[TyCon]{The @TyCon@ datatype}
5
6 \begin{code}
7 module TyCon(
8         TyCon, FieldLabel,
9
10         PrimRep(..),
11         tyConPrimRep,
12
13         AlgTyConRhs(..), visibleDataCons,
14
15         isFunTyCon, isUnLiftedTyCon, isProductTyCon, 
16         isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
17         isEnumerationTyCon, isGadtSyntaxTyCon,
18         isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
19         isRecursiveTyCon, newTyConRep, newTyConRhs, newTyConCo,
20         isHiBootTyCon, isSuperKindTyCon,
21         isCoercionTyCon_maybe, isCoercionTyCon,
22
23         tcExpandTyCon_maybe, coreExpandTyCon_maybe,
24
25         makeTyConAbstract, isAbstractTyCon,
26
27         mkForeignTyCon, isForeignTyCon,
28
29         mkAlgTyCon,
30         mkClassTyCon,
31         mkFunTyCon,
32         mkPrimTyCon,
33         mkVoidPrimTyCon,
34         mkLiftedPrimTyCon,
35         mkTupleTyCon,
36         mkSynTyCon,
37         mkSuperKindTyCon,
38         mkCoercionTyCon,
39
40         tyConName,
41         tyConKind,
42         tyConUnique,
43         tyConTyVars,
44         algTyConRhs, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
45         tyConSelIds,
46         tyConStupidTheta,
47         tyConArity,
48         isClassTyCon, tyConClass_maybe,
49         synTyConDefn, synTyConRhs,
50         tyConExtName,           -- External name for foreign types
51
52         maybeTyConSingleCon,
53
54         -- Generics
55         tyConHasGenerics
56 ) where
57
58 #include "HsVersions.h"
59
60 import {-# SOURCE #-} TypeRep ( Kind, Type, Coercion, PredType )
61 import {-# SOURCE #-} DataCon ( DataCon, isVanillaDataCon )
62
63 import Var              ( TyVar, Id )
64 import Class            ( Class )
65 import BasicTypes       ( Arity, RecFlag(..), Boxity(..), isBoxed )
66 import Name             ( Name, nameUnique, NamedThing(getName) )
67 import PrelNames        ( Unique, Uniquable(..) )
68 import Maybes           ( orElse )
69 import Outputable
70 import FastString
71 \end{code}
72
73 %************************************************************************
74 %*                                                                      *
75 \subsection{The data type}
76 %*                                                                      *
77 %************************************************************************
78
79 \begin{code}
80 data TyCon
81   = FunTyCon {
82         tyConUnique :: Unique,
83         tyConName   :: Name,
84         tyConKind   :: Kind,
85         tyConArity  :: Arity
86     }
87
88
89   | AlgTyCon {          -- Data type, and newtype decls.
90                         -- All lifted, all boxed
91         tyConUnique :: Unique,
92         tyConName   :: Name,
93         tyConKind   :: Kind,
94         tyConArity  :: Arity,
95         
96         tyConTyVars :: [TyVar],         -- Scopes over (a) the [PredType] in AlgTyConRhs.DataTyCon
97                                         --             (b) the cached types in AlgTyConRhs.NewTyCon
98                                         -- But not over the data constructors
99         algTcSelIds :: [Id],            -- Its record selectors (empty if none): 
100
101         algTcGadtSyntax  :: Bool,       -- True <=> the data type was declared using GADT syntax
102                                         -- That doesn't mean it's a true GADT; only that the "where"
103                                         --      form was used. This field is used only to guide
104                                         --      pretty-printinng
105         algTcStupidTheta :: [PredType], -- The "stupid theta" for the data type
106                                         -- (always empty for GADTs)
107
108         algTcRhs :: AlgTyConRhs,        -- Data constructors in here
109
110         algTcRec :: RecFlag,            -- Tells whether the data type is part of 
111                                         -- a mutually-recursive group or not
112
113         hasGenerics :: Bool,            -- True <=> generic to/from functions are available
114                                         -- (in the exports of the data type's source module)
115
116         algTcClass :: Maybe Class
117                 -- Just cl if this tycon came from a class declaration
118     }
119
120   | TupleTyCon {
121         tyConUnique :: Unique,
122         tyConName   :: Name,
123         tyConKind   :: Kind,
124         tyConArity  :: Arity,
125         tyConBoxed  :: Boxity,
126         tyConTyVars :: [TyVar],
127         dataCon     :: DataCon,
128         hasGenerics :: Bool
129     }
130
131   | SynTyCon {
132         tyConUnique :: Unique,
133         tyConName   :: Name,
134         tyConKind   :: Kind,
135         tyConArity  :: Arity,
136
137         tyConTyVars :: [TyVar],         -- Bound tyvars
138         synTcRhs    :: Type             -- Right-hand side, mentioning these type vars.
139                                         -- Acts as a template for the expansion when
140                                         -- the tycon is applied to some types.
141     }
142
143   | PrimTyCon {                 -- Primitive types; cannot be defined in Haskell
144                                 -- Now includes foreign-imported types
145                                 -- Also includes Kinds
146         tyConUnique   :: Unique,
147         tyConName     :: Name,
148         tyConKind     :: Kind,
149         tyConArity    :: Arity,
150
151         primTyConRep  :: PrimRep,
152                         -- Many primitive tycons are unboxed, but some are
153                         -- boxed (represented by pointers). The CgRep tells.
154
155         isUnLifted   :: Bool,           -- Most primitive tycons are unlifted, 
156                                         -- but foreign-imported ones may not be
157         tyConExtName :: Maybe FastString        -- Just xx for foreign-imported types
158     }
159
160   | CoercionTyCon {     -- E.g. (:=:), sym, trans, left, right
161                         -- INVARIANT: coercions are always fully applied
162         tyConUnique :: Unique,
163         tyConName   :: Name,
164         tyConArity  :: Arity,
165         coKindFun   :: [Type] -> Kind
166     }
167         
168   | SuperKindTyCon {    -- Super Kinds, TY (box) and CO (diamond).
169                         -- They have no kind; and arity zero
170         tyConUnique :: Unique,
171         tyConName   :: Name
172     }
173
174 type KindCon = TyCon
175
176 type SuperKindCon = TyCon
177
178 type FieldLabel = Name
179
180 data AlgTyConRhs
181   = AbstractTyCon       -- We know nothing about this data type, except 
182                         -- that it's represented by a pointer
183                         -- Used when we export a data type abstractly into
184                         -- an hi file
185
186   | DataTyCon {
187         data_cons :: [DataCon],
188                         -- The constructors; can be empty if the user declares
189                         --   the type to have no constructors
190                         -- INVARIANT: Kept in order of increasing tag
191                         --            (see the tag assignment in DataCon.mkDataCon)
192         is_enum :: Bool         -- Cached: True <=> an enumeration type
193     }                   --         Includes data types with no constructors.
194
195   | NewTyCon {
196         data_con :: DataCon,    -- The unique constructor; it has no existentials
197
198         nt_rhs :: Type,         -- Cached: the argument type of the constructor
199                                 --  = the representation type of the tycon
200                                 -- The free tyvars of this type are the tyConTyVars
201       
202         nt_co :: Maybe TyCon,   -- The coercion used to create the newtype
203                                 -- from the representation
204                                 -- optional for non-recursive newtypes
205                                 -- See Note [Newtype coercions]
206
207         nt_etad_rhs :: ([TyVar], Type) ,
208                         -- The same again, but this time eta-reduced
209                         -- hence the [TyVar] which may be shorter than the declared 
210                         -- arity of the TyCon.  See Note [Newtype eta]
211
212         nt_rep :: Type  -- Cached: the *ultimate* representation type
213                         -- By 'ultimate' I mean that the top-level constructor
214                         -- of the rep type is not itself a newtype or type synonym.
215                         -- The rep type isn't entirely simple:
216                         --  for a recursive newtype we pick () as the rep type
217                         --      newtype T = MkT T
218                         -- 
219                         -- This one does not need to be eta reduced; hence its
220                         -- free type variables are conveniently tyConTyVars
221                         -- Thus:
222                         --      newtype T a = MkT [(a,Int)]
223                         -- The rep type is [(a,Int)]
224                         -- NB: the rep type isn't necessarily the original RHS of the
225                         --     newtype decl, because the rep type looks through other
226     }                   --     newtypes.
227
228 visibleDataCons :: AlgTyConRhs -> [DataCon]
229 visibleDataCons AbstractTyCon                 = []
230 visibleDataCons (DataTyCon{ data_cons = cs }) = cs
231 visibleDataCons (NewTyCon{ data_con = c })    = [c]
232 \end{code}
233
234 Note [Newtype coercions]
235 ~~~~~~~~~~~~~~~~~~~~~~~~
236
237 The NewTyCon field nt_co is a a TyCon (a coercion constructor in fact)
238 which is used for coercing from the representation type of the
239 newtype, to the newtype itself. For example,
240
241    newtype T a = MkT [a]
242
243 the NewTyCon for T will contain nt_co = CoT where CoT t : [t] :=: T t.
244 This TyCon is a CoercionTyCon, so it does not have a kind on its own;
245 it basically has its own typing rule for the fully-applied version.
246 If the newtype T has k type variables then CoT has arity k.
247
248 In the paper we'd write
249         axiom CoT : (forall t. [t]) :=: (forall t. T t)
250 and then when we used CoT at a particular type, s, we'd say
251         CoT @ s
252 which encodes as (TyConApp instCoercionTyCon [TyConApp CoT [], s])
253
254 But in GHC we instead make CoT into a new piece of type syntax
255 (like instCoercionTyCon, symCoercionTyCon etc), which must always
256 be saturated, but which encodes as
257         TyConAp CoT [s]
258 In the vocabulary of the paper it's as if we had axiom declarations
259 like
260         axiom CoT t : ([t] :=: T t)
261
262 Note [Newtype eta]
263 ~~~~~~~~~~~~~~~~~~
264 Consider
265         newtype Parser m a = MkParser (Foogle m a)
266 Are these two types equal (to Core)?
267         Monad (Parser m) 
268         Monad (Foogle m)
269 Well, yes.  But to see that easily we eta-reduce the RHS type of
270 Parser, in this case to ([], Froogle), so that even unsaturated applications
271 of Parser will work right.  This eta reduction is done when the type 
272 constructor is built, and cached in NewTyCon.  The cached field is
273 only used in coreExpandTyCon_maybe.
274  
275 Here's an example that I think showed up in practice
276 Source code:
277         newtype T a = MkT [a]
278         newtype Foo m = MkFoo (forall a. m a -> Int)
279
280         w1 :: Foo []
281         w1 = ...
282         
283         w2 :: Foo T
284         w2 = MkFoo (\(MkT x) -> case w1 of MkFoo f -> f x)
285
286 After desugaring, and discading the data constructors for the newtypes,
287 we get:
288         w2 :: Foo T
289         w2 = w1
290 And now Lint complains unless Foo T == Foo [], and that requires T==[]
291
292
293 %************************************************************************
294 %*                                                                      *
295 \subsection{PrimRep}
296 %*                                                                      *
297 %************************************************************************
298
299 A PrimRep is an abstraction of a type.  It contains information that
300 the code generator needs in order to pass arguments, return results,
301 and store values of this type.
302
303 A PrimRep is somewhat similar to a CgRep (see codeGen/SMRep) and a
304 MachRep (see cmm/MachOp), although each of these types has a distinct
305 and clearly defined purpose:
306
307   - A PrimRep is a CgRep + information about signedness + information
308     about primitive pointers (AddrRep).  Signedness and primitive
309     pointers are required when passing a primitive type to a foreign
310     function, but aren't needed for call/return conventions of Haskell
311     functions.
312
313   - A MachRep is a basic machine type (non-void, doesn't contain
314     information on pointerhood or signedness, but contains some
315     reps that don't have corresponding Haskell types).
316
317 \begin{code}
318 data PrimRep
319   = VoidRep
320   | PtrRep
321   | IntRep              -- signed, word-sized
322   | WordRep             -- unsinged, word-sized
323   | Int64Rep            -- signed, 64 bit (32-bit words only)
324   | Word64Rep           -- unsigned, 64 bit (32-bit words only)
325   | AddrRep             -- a pointer, but not to a Haskell value
326   | FloatRep
327   | DoubleRep
328 \end{code}
329
330 %************************************************************************
331 %*                                                                      *
332 \subsection{TyCon Construction}
333 %*                                                                      *
334 %************************************************************************
335
336 Note: the TyCon constructors all take a Kind as one argument, even though
337 they could, in principle, work out their Kind from their other arguments.
338 But to do so they need functions from Types, and that makes a nasty
339 module mutual-recursion.  And they aren't called from many places.
340 So we compromise, and move their Kind calculation to the call site.
341
342 \begin{code}
343 mkFunTyCon :: Name -> Kind -> TyCon
344 mkFunTyCon name kind 
345   = FunTyCon { 
346         tyConUnique = nameUnique name,
347         tyConName   = name,
348         tyConKind   = kind,
349         tyConArity  = 2
350     }
351
352 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
353 -- but now you also have to pass in the generic information about the type
354 -- constructor - you can get hold of it easily (see Generics module)
355 mkAlgTyCon name kind tyvars stupid rhs sel_ids is_rec gen_info gadt_syn
356   = AlgTyCon {  
357         tyConName        = name,
358         tyConUnique      = nameUnique name,
359         tyConKind        = kind,
360         tyConArity       = length tyvars,
361         tyConTyVars      = tyvars,
362         algTcStupidTheta = stupid,
363         algTcRhs         = rhs,
364         algTcSelIds      = sel_ids,
365         algTcClass       = Nothing,
366         algTcRec         = is_rec,
367         algTcGadtSyntax  = gadt_syn,
368         hasGenerics = gen_info
369     }
370
371 mkClassTyCon name kind tyvars rhs clas is_rec
372   = AlgTyCon {  
373         tyConName        = name,
374         tyConUnique      = nameUnique name,
375         tyConKind        = kind,
376         tyConArity       = length tyvars,
377         tyConTyVars      = tyvars,
378         algTcStupidTheta = [],
379         algTcRhs         = rhs,
380         algTcSelIds      = [],
381         algTcClass       = Just clas,
382         algTcRec         = is_rec,
383         algTcGadtSyntax  = False,       -- Doesn't really matter
384         hasGenerics = False
385     }
386
387
388 mkTupleTyCon name kind arity tyvars con boxed gen_info
389   = TupleTyCon {
390         tyConUnique = nameUnique name,
391         tyConName = name,
392         tyConKind = kind,
393         tyConArity = arity,
394         tyConBoxed = boxed,
395         tyConTyVars = tyvars,
396         dataCon = con,
397         hasGenerics = gen_info
398     }
399
400 -- Foreign-imported (.NET) type constructors are represented
401 -- as primitive, but *lifted*, TyCons for now. They are lifted
402 -- because the Haskell type T representing the (foreign) .NET
403 -- type T is actually implemented (in ILX) as a thunk<T>
404 mkForeignTyCon name ext_name kind arity
405   = PrimTyCon {
406         tyConName    = name,
407         tyConUnique  = nameUnique name,
408         tyConKind    = kind,
409         tyConArity   = arity,
410         primTyConRep = PtrRep, -- they all do
411         isUnLifted   = False,
412         tyConExtName = ext_name
413     }
414
415
416 -- most Prim tycons are lifted
417 mkPrimTyCon name kind arity rep
418   = mkPrimTyCon' name kind arity rep True  
419
420 mkVoidPrimTyCon name kind arity 
421   = mkPrimTyCon' name kind arity VoidRep True  
422
423 -- but RealWorld is lifted
424 mkLiftedPrimTyCon name kind arity rep
425   = mkPrimTyCon' name kind arity rep False
426
427 mkPrimTyCon' name kind arity rep is_unlifted
428   = PrimTyCon {
429         tyConName    = name,
430         tyConUnique  = nameUnique name,
431         tyConKind    = kind,
432         tyConArity   = arity,
433         primTyConRep = rep,
434         isUnLifted   = is_unlifted,
435         tyConExtName = Nothing
436     }
437
438 mkSynTyCon name kind tyvars rhs
439   = SynTyCon {  
440         tyConName = name,
441         tyConUnique = nameUnique name,
442         tyConKind = kind,
443         tyConArity = length tyvars,
444         tyConTyVars = tyvars,
445         synTcRhs = rhs
446     }
447
448 mkCoercionTyCon name arity kindRule
449   = CoercionTyCon {
450         tyConName = name,
451         tyConUnique = nameUnique name,
452         tyConArity = arity,
453         coKindFun = kindRule
454     }
455
456 -- Super kinds always have arity zero
457 mkSuperKindTyCon name
458   = SuperKindTyCon {
459         tyConName = name,
460         tyConUnique = nameUnique name
461   }
462 \end{code}
463
464 \begin{code}
465 isFunTyCon :: TyCon -> Bool
466 isFunTyCon (FunTyCon {}) = True
467 isFunTyCon _             = False
468
469 isAbstractTyCon :: TyCon -> Bool
470 isAbstractTyCon (AlgTyCon { algTcRhs = AbstractTyCon }) = True
471 isAbstractTyCon _ = False
472
473 makeTyConAbstract :: TyCon -> TyCon
474 makeTyConAbstract tc@(AlgTyCon {}) = tc { algTcRhs = AbstractTyCon }
475 makeTyConAbstract tc = pprPanic "makeTyConAbstract" (ppr tc)
476
477 isPrimTyCon :: TyCon -> Bool
478 isPrimTyCon (PrimTyCon {}) = True
479 isPrimTyCon _              = False
480
481 isUnLiftedTyCon :: TyCon -> Bool
482 isUnLiftedTyCon (PrimTyCon  {isUnLifted = is_unlifted}) = is_unlifted
483 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity})      = not (isBoxed boxity)
484 isUnLiftedTyCon _                                       = False
485
486 -- isAlgTyCon returns True for both @data@ and @newtype@
487 isAlgTyCon :: TyCon -> Bool
488 isAlgTyCon (AlgTyCon {})   = True
489 isAlgTyCon (TupleTyCon {}) = True
490 isAlgTyCon other           = False
491
492 isDataTyCon :: TyCon -> Bool
493 -- isDataTyCon returns True for data types that are represented by
494 -- heap-allocated constructors.
495 -- These are srcutinised by Core-level @case@ expressions, and they
496 -- get info tables allocated for them.
497 --      True for all @data@ types
498 --      False for newtypes
499 --                unboxed tuples
500 isDataTyCon tc@(AlgTyCon {algTcRhs = rhs})  
501   = case rhs of
502         DataTyCon {}  -> True
503         NewTyCon {}   -> False
504         AbstractTyCon -> pprPanic "isDataTyCon" (ppr tc)
505
506 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
507 isDataTyCon other = False
508
509 isNewTyCon :: TyCon -> Bool
510 isNewTyCon (AlgTyCon {algTcRhs = NewTyCon {}}) = True 
511 isNewTyCon other                               = False
512
513 isProductTyCon :: TyCon -> Bool
514 -- A "product" tycon
515 --      has *one* constructor, 
516 --      is *not* existential
517 -- but
518 --      may be  DataType, NewType
519 --      may be  unboxed or not, 
520 --      may be  recursive or not
521 -- 
522 isProductTyCon tc@(AlgTyCon {}) = case algTcRhs tc of
523                                     DataTyCon{ data_cons = [data_con] } 
524                                                 -> isVanillaDataCon data_con
525                                     NewTyCon {} -> True
526                                     other       -> False
527 isProductTyCon (TupleTyCon {})  = True   
528 isProductTyCon other            = False
529
530 isSynTyCon :: TyCon -> Bool
531 isSynTyCon (SynTyCon {}) = True
532 isSynTyCon _             = False
533
534 isGadtSyntaxTyCon :: TyCon -> Bool
535 isGadtSyntaxTyCon (AlgTyCon { algTcGadtSyntax = res }) = res
536 isGadtSyntaxTyCon other                                = False
537
538 isEnumerationTyCon :: TyCon -> Bool
539 isEnumerationTyCon (AlgTyCon {algTcRhs = DataTyCon { is_enum = res }}) = res
540 isEnumerationTyCon other                                               = False
541
542 isTupleTyCon :: TyCon -> Bool
543 -- The unit tycon didn't used to be classed as a tuple tycon
544 -- but I thought that was silly so I've undone it
545 -- If it can't be for some reason, it should be a AlgTyCon
546 --
547 -- NB: when compiling Data.Tuple, the tycons won't reply True to
548 -- isTupleTyCon, becuase they are built as AlgTyCons.  However they
549 -- get spat into the interface file as tuple tycons, so I don't think
550 -- it matters.
551 isTupleTyCon (TupleTyCon {}) = True
552 isTupleTyCon other           = False
553
554 isUnboxedTupleTyCon :: TyCon -> Bool
555 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
556 isUnboxedTupleTyCon other = False
557
558 isBoxedTupleTyCon :: TyCon -> Bool
559 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
560 isBoxedTupleTyCon other = False
561
562 tupleTyConBoxity tc = tyConBoxed tc
563
564 isRecursiveTyCon :: TyCon -> Bool
565 isRecursiveTyCon (AlgTyCon {algTcRec = Recursive}) = True
566 isRecursiveTyCon other                                = False
567
568 isHiBootTyCon :: TyCon -> Bool
569 -- Used for knot-tying in hi-boot files
570 isHiBootTyCon (AlgTyCon {algTcRhs = AbstractTyCon}) = True
571 isHiBootTyCon other                                 = False
572
573 isForeignTyCon :: TyCon -> Bool
574 -- isForeignTyCon identifies foreign-imported type constructors
575 isForeignTyCon (PrimTyCon {tyConExtName = Just _}) = True
576 isForeignTyCon other                               = False
577
578 isSuperKindTyCon :: TyCon -> Bool
579 isSuperKindTyCon (SuperKindTyCon {}) = True
580 isSuperKindTyCon other               = False
581
582 isCoercionTyCon_maybe :: TyCon -> Maybe (Arity, [Type] -> Kind)
583 isCoercionTyCon_maybe (CoercionTyCon {tyConArity = ar, coKindFun = rule}) 
584   = Just (ar, rule)
585 isCoercionTyCon_maybe other = Nothing
586
587 isCoercionTyCon (CoercionTyCon {}) = True
588 isCoercionTyCon other              = False
589 \end{code}
590
591
592 -----------------------------------------------
593 --      Expand type-constructor applications
594 -----------------------------------------------
595
596 \begin{code}
597 tcExpandTyCon_maybe, coreExpandTyCon_maybe 
598         :: TyCon 
599         -> [Type]                       -- Args to tycon
600         -> Maybe ([(TyVar,Type)],       -- Substitution
601                   Type,                 -- Body type (not yet substituted)
602                   [Type])               -- Leftover args
603
604 -- For the *typechecker* view, we expand synonyms only
605 tcExpandTyCon_maybe (SynTyCon {tyConTyVars = tvs, synTcRhs = rhs }) tys
606    = expand tvs rhs tys
607 tcExpandTyCon_maybe other_tycon tys = Nothing
608
609 ---------------
610 -- For the *Core* view, we expand synonyms only as well
611
612 coreExpandTyCon_maybe (AlgTyCon {algTcRec = NonRecursive,       -- Not recursive
613          algTcRhs = NewTyCon { nt_etad_rhs = etad_rhs, nt_co = Nothing }}) tys
614    = case etad_rhs of   -- Don't do this in the pattern match, lest we accidentally
615                         -- match the etad_rhs of a *recursive* newtype
616         (tvs,rhs) -> expand tvs rhs tys
617
618 coreExpandTyCon_maybe tycon tys = tcExpandTyCon_maybe tycon tys
619
620
621 ----------------
622 expand  :: [TyVar] -> Type                      -- Template
623         -> [Type]                               -- Args
624         -> Maybe ([(TyVar,Type)], Type, [Type]) -- Expansion
625 expand tvs rhs tys
626   = case n_tvs `compare` length tys of
627         LT -> Just (tvs `zip` tys, rhs, drop n_tvs tys)
628         EQ -> Just (tvs `zip` tys, rhs, [])
629         GT -> Nothing
630    where
631      n_tvs = length tvs
632 \end{code}
633
634 \begin{code}
635 tyConHasGenerics :: TyCon -> Bool
636 tyConHasGenerics (AlgTyCon {hasGenerics = hg})   = hg
637 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
638 tyConHasGenerics other                           = False        -- Synonyms
639
640 tyConDataCons :: TyCon -> [DataCon]
641 -- It's convenient for tyConDataCons to return the
642 -- empty list for type synonyms etc
643 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
644
645 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
646 tyConDataCons_maybe (AlgTyCon {algTcRhs = DataTyCon { data_cons = cons }}) = Just cons
647 tyConDataCons_maybe (AlgTyCon {algTcRhs = NewTyCon { data_con = con }})    = Just [con]
648 tyConDataCons_maybe (TupleTyCon {dataCon = con})                           = Just [con]
649 tyConDataCons_maybe other                                                  = Nothing
650
651 tyConFamilySize  :: TyCon -> Int
652 tyConFamilySize (AlgTyCon {algTcRhs = DataTyCon { data_cons = cons }}) = length cons
653 tyConFamilySize (AlgTyCon {algTcRhs = NewTyCon {}}) = 1
654 tyConFamilySize (TupleTyCon {})                     = 1
655 #ifdef DEBUG
656 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
657 #endif
658
659 tyConSelIds :: TyCon -> [Id]
660 tyConSelIds (AlgTyCon {algTcSelIds = fs}) = fs
661 tyConSelIds other_tycon                   = []
662
663 algTyConRhs :: TyCon -> AlgTyConRhs
664 algTyConRhs (AlgTyCon {algTcRhs = rhs})  = rhs
665 algTyConRhs (TupleTyCon {dataCon = con}) = DataTyCon { data_cons = [con], is_enum = False }
666 algTyConRhs other = pprPanic "algTyConRhs" (ppr other)
667 \end{code}
668
669 \begin{code}
670 newTyConRhs :: TyCon -> ([TyVar], Type)
671 newTyConRhs (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rhs = rhs }}) = (tvs, rhs)
672 newTyConRhs tycon = pprPanic "newTyConRhs" (ppr tycon)
673
674 newTyConRep :: TyCon -> ([TyVar], Type)
675 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_rep = rep }}) = (tvs, rep)
676 newTyConRep tycon = pprPanic "newTyConRep" (ppr tycon)
677
678 newTyConCo :: TyCon -> Maybe TyCon
679 newTyConCo (AlgTyCon {tyConTyVars = tvs, algTcRhs = NewTyCon { nt_co = co }}) = co
680 newTyConCo tycon = pprPanic "newTyConCo" (ppr tycon)
681
682 tyConPrimRep :: TyCon -> PrimRep
683 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
684 tyConPrimRep tc = ASSERT(not (isUnboxedTupleTyCon tc)) PtrRep
685 \end{code}
686
687 \begin{code}
688 tyConStupidTheta :: TyCon -> [PredType]
689 tyConStupidTheta (AlgTyCon {algTcStupidTheta = stupid}) = stupid
690 tyConStupidTheta (TupleTyCon {})                        = []
691 tyConStupidTheta tycon = pprPanic "tyConStupidTheta" (ppr tycon)
692 \end{code}
693
694 \begin{code}
695 synTyConDefn :: TyCon -> ([TyVar], Type)
696 synTyConDefn (SynTyCon {tyConTyVars = tyvars, synTcRhs = ty}) = (tyvars,ty)
697 synTyConDefn tycon = pprPanic "getSynTyConDefn" (ppr tycon)
698
699 synTyConRhs :: TyCon -> Type
700 synTyConRhs tc = synTcRhs tc
701 \end{code}
702
703 \begin{code}
704 maybeTyConSingleCon :: TyCon -> Maybe DataCon
705 maybeTyConSingleCon (AlgTyCon {algTcRhs = DataTyCon {data_cons = [c] }}) = Just c
706 maybeTyConSingleCon (AlgTyCon {algTcRhs = NewTyCon { data_con = c }})    = Just c
707 maybeTyConSingleCon (AlgTyCon {})                = Nothing
708 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
709 maybeTyConSingleCon (PrimTyCon {})               = Nothing
710 maybeTyConSingleCon (FunTyCon {})                = Nothing  -- case at funty
711 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
712 \end{code}
713
714 \begin{code}
715 isClassTyCon :: TyCon -> Bool
716 isClassTyCon (AlgTyCon {algTcClass = Just _}) = True
717 isClassTyCon other_tycon                         = False
718
719 tyConClass_maybe :: TyCon -> Maybe Class
720 tyConClass_maybe (AlgTyCon {algTcClass = maybe_clas}) = maybe_clas
721 tyConClass_maybe ther_tycon                              = Nothing
722 \end{code}
723
724
725 %************************************************************************
726 %*                                                                      *
727 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
728 %*                                                                      *
729 %************************************************************************
730
731 @TyCon@s are compared by comparing their @Unique@s.
732
733 The strictness analyser needs @Ord@. It is a lexicographic order with
734 the property @(a<=b) || (b<=a)@.
735
736 \begin{code}
737 instance Eq TyCon where
738     a == b = case (a `compare` b) of { EQ -> True;   _ -> False }
739     a /= b = case (a `compare` b) of { EQ -> False;  _ -> True  }
740
741 instance Ord TyCon where
742     a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
743     a <  b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
744     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
745     a >  b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
746     compare a b = getUnique a `compare` getUnique b
747
748 instance Uniquable TyCon where
749     getUnique tc = tyConUnique tc
750
751 instance Outputable TyCon where
752     ppr tc  = ppr (getName tc) 
753
754 instance NamedThing TyCon where
755     getName = tyConName
756 \end{code}