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