e81a2a37aff8a1e0c4a3e1015df001887c7ea15e
[ghc-hetmet.git] / ghc / 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, KindCon, SuperKindCon, ArgVrcs, 
9
10         AlgTyConFlavour(..), 
11         DataConDetails(..), visibleDataCons,
12
13         isFunTyCon, isUnLiftedTyCon, isProductTyCon,
14         isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
15         isEnumerationTyCon, 
16         isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
17         isRecursiveTyCon, newTyConRep, isHiBootTyCon,
18
19         mkForeignTyCon, isForeignTyCon,
20
21         mkAlgTyCon,
22         mkClassTyCon,
23         mkFunTyCon,
24         mkPrimTyCon,
25         mkLiftedPrimTyCon,
26         mkTupleTyCon,
27         mkSynTyCon,
28         mkKindCon,
29         mkSuperKindCon,
30
31         tyConName,
32         tyConKind,
33         tyConUnique,
34         tyConTyVars,
35         tyConArgVrcs_maybe, tyConArgVrcs,
36         tyConDataConDetails, tyConDataCons, tyConDataCons_maybe, tyConFamilySize,
37         tyConSelIds,
38         tyConTheta,
39         tyConPrimRep,
40         tyConArity,
41         isClassTyCon, tyConClass_maybe,
42         getSynTyConDefn,
43         tyConExtName,           -- External name for foreign types
44
45         maybeTyConSingleCon,
46
47         -- Generics
48         tyConHasGenerics
49 ) where
50
51 #include "HsVersions.h"
52
53 import {-# SOURCE #-} TypeRep ( Type, PredType, Kind, SuperKind )
54  -- Should just be Type(Type), but this fails due to bug present up to
55  -- and including 4.02 involving slurping of hi-boot files.  Bug is now fixed.
56
57 import {-# SOURCE #-} DataCon ( DataCon, isExistentialDataCon )
58
59
60 import Var              ( TyVar, Id )
61 import Class            ( Class )
62 import BasicTypes       ( Arity, RecFlag(..), Boxity(..), isBoxed )
63 import Name             ( Name, nameUnique, NamedThing(getName) )
64 import PrelNames        ( Unique, Uniquable(..), anyBoxConKey )
65 import PrimRep          ( PrimRep(..), isFollowableRep )
66 import Maybes           ( orElse, expectJust )
67 import Outputable
68 import FastString
69 \end{code}
70
71 %************************************************************************
72 %*                                                                      *
73 \subsection{The data type}
74 %*                                                                      *
75 %************************************************************************
76
77 \begin{code}
78 type KindCon      = TyCon
79 type SuperKindCon = TyCon
80
81 data TyCon
82   = FunTyCon {
83         tyConUnique :: Unique,
84         tyConName   :: Name,
85         tyConKind   :: Kind,
86         tyConArity  :: Arity
87     }
88
89
90   | AlgTyCon {          -- Tuples, 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],
98         argVrcs       :: ArgVrcs,
99         algTyConTheta :: [PredType],
100
101         dataCons :: DataConDetails DataCon,
102
103         selIds :: [Id], -- Its record selectors (if any)
104
105         algTyConFlavour :: AlgTyConFlavour,
106         algTyConRec     :: RecFlag,     -- Tells whether the data type is part of 
107                                         -- a mutually-recursive group or not
108
109         hasGenerics :: Bool,    -- True <=> generic to/from functions are available
110                                         --          (in the exports of the data type's source module)
111
112         algTyConClass :: Maybe Class
113                 -- Just cl if this tycon came from a class declaration
114     }
115
116   | PrimTyCon {                 -- Primitive types; cannot be defined in Haskell
117                                 -- Now includes foreign-imported types
118         tyConUnique  :: Unique,
119         tyConName    :: Name,
120         tyConKind    :: Kind,
121         tyConArity   :: Arity,
122         argVrcs      :: ArgVrcs,
123         primTyConRep :: PrimRep,        -- Many primitive tycons are unboxed, but some are
124                                         -- boxed (represented by pointers). The PrimRep tells.
125
126         isUnLifted   :: Bool,   -- Most primitive tycons are unlifted, 
127                                 -- but foreign-imported ones may not be
128         tyConExtName :: Maybe FastString        -- Just xx for foreign-imported types
129     }
130
131   | TupleTyCon {
132
133         tyConUnique :: Unique,
134         tyConName   :: Name,
135         tyConKind   :: Kind,
136         tyConArity  :: Arity,
137         tyConBoxed  :: Boxity,
138         tyConTyVars :: [TyVar],
139         dataCon     :: DataCon,
140         hasGenerics :: Bool
141     }
142
143   | SynTyCon {
144         tyConUnique :: Unique,
145         tyConName   :: Name,
146         tyConKind   :: Kind,
147         tyConArity  :: Arity,
148
149         tyConTyVars     :: [TyVar],     -- Bound tyvars
150         synTyConDefn    :: Type,        -- Right-hand side, mentioning these type vars.
151                                         -- Acts as a template for the expansion when
152                                         -- the tycon is applied to some types.
153         argVrcs :: ArgVrcs
154     }
155
156   | KindCon {           -- Type constructor at the kind level
157         tyConUnique :: Unique,
158         tyConName   :: Name,
159         tyConKind   :: SuperKind,
160         tyConArity  :: Arity
161     }
162
163   | SuperKindCon        {               -- The type of kind variables or boxity variables,
164         tyConUnique :: Unique,
165         tyConName   :: Name
166     }
167
168 type ArgVrcs = [(Bool,Bool)]  -- Tyvar variance info: [(occPos,occNeg)]
169         -- [] means "no information, assume the worst"
170
171 data AlgTyConFlavour
172   = DataTyCon Bool      -- Data type; True <=> an enumeration type
173
174   | NewTyCon Type       -- Newtype, with its *ultimate* representation type
175                         -- By 'ultimate' I mean that the rep type is not itself
176                         -- a newtype or type synonym.
177                         -- The rep type isn't entirely simple:
178                         --  for a recursive newtype we pick () as the rep type
179                         --      newtype T = MkT T
180                         --
181                         -- The rep type has free type variables the tyConTyVars
182                         -- Thus:
183                         --      newtype T a = MkT [(a,Int)]
184                         -- The rep type is [(a,Int)]
185         -- NB: the rep type isn't necessarily the original RHS of the
186         --     newtype decl, because the rep type looks through other
187         --     newtypes.  If you want hte original RHS, look at the 
188         --     argument type of the data constructor.
189
190 data DataConDetails datacon
191   = DataCons [datacon]  -- Its data constructors, with fully polymorphic types
192                         -- A type can have zero constructors
193
194   | Unknown             -- Used only when We're importing this data type from an 
195                         -- hi-boot file, so we don't know what its constructors are
196
197 visibleDataCons (DataCons cs) = cs
198 visibleDataCons other         = []
199 \end{code}
200
201
202 %************************************************************************
203 %*                                                                      *
204 \subsection{TyCon Construction}
205 %*                                                                      *
206 %************************************************************************
207
208 Note: the TyCon constructors all take a Kind as one argument, even though
209 they could, in principle, work out their Kind from their other arguments.
210 But to do so they need functions from Types, and that makes a nasty
211 module mutual-recursion.  And they aren't called from many places.
212 So we compromise, and move their Kind calculation to the call site.
213
214 \begin{code}
215 mkSuperKindCon :: Name -> SuperKindCon
216 mkSuperKindCon name = SuperKindCon {
217                         tyConUnique = nameUnique name,
218                         tyConName = name
219                       }
220
221 mkKindCon :: Name -> SuperKind -> KindCon
222 mkKindCon name kind
223   = KindCon { 
224         tyConUnique = nameUnique name,
225         tyConName = name,
226         tyConArity = 0,
227         tyConKind = kind
228      }
229
230 mkFunTyCon :: Name -> Kind -> TyCon
231 mkFunTyCon name kind 
232   = FunTyCon { 
233         tyConUnique = nameUnique name,
234         tyConName   = name,
235         tyConKind   = kind,
236         tyConArity  = 2
237     }
238
239 -- This is the making of a TyCon. Just the same as the old mkAlgTyCon,
240 -- but now you also have to pass in the generic information about the type
241 -- constructor - you can get hold of it easily (see Generics module)
242 mkAlgTyCon name kind tyvars theta argvrcs cons sels flavour is_rec gen_info
243   = AlgTyCon {  
244         tyConName        = name,
245         tyConUnique      = nameUnique name,
246         tyConKind        = kind,
247         tyConArity       = length tyvars,
248         tyConTyVars      = tyvars,
249         argVrcs          = argvrcs,
250         algTyConTheta    = theta,
251         dataCons         = cons, 
252         selIds           = sels,
253         algTyConClass    = Nothing,
254         algTyConFlavour  = flavour,
255         algTyConRec      = is_rec,
256         hasGenerics = gen_info
257     }
258
259 mkClassTyCon name kind tyvars argvrcs con clas flavour is_rec
260   = AlgTyCon {  
261         tyConName        = name,
262         tyConUnique      = nameUnique name,
263         tyConKind        = kind,
264         tyConArity       = length tyvars,
265         tyConTyVars      = tyvars,
266         argVrcs          = argvrcs,
267         algTyConTheta    = [],
268         dataCons         = DataCons [con],
269         selIds           = [],
270         algTyConClass    = Just clas,
271         algTyConFlavour  = flavour,
272         algTyConRec      = is_rec,
273         hasGenerics = False
274     }
275
276
277 mkTupleTyCon name kind arity tyvars con boxed gen_info
278   = TupleTyCon {
279         tyConUnique = nameUnique name,
280         tyConName = name,
281         tyConKind = kind,
282         tyConArity = arity,
283         tyConBoxed = boxed,
284         tyConTyVars = tyvars,
285         dataCon = con,
286         hasGenerics = gen_info
287     }
288
289 -- Foreign-imported (.NET) type constructors are represented
290 -- as primitive, but *lifted*, TyCons for now. They are lifted
291 -- because the Haskell type T representing the (foreign) .NET
292 -- type T is actually implemented (in ILX) as a thunk<T>
293 -- They have PtrRep
294 mkForeignTyCon name ext_name kind arity arg_vrcs
295   = PrimTyCon {
296         tyConName    = name,
297         tyConUnique  = nameUnique name,
298         tyConKind    = kind,
299         tyConArity   = arity,
300         argVrcs      = arg_vrcs,
301         primTyConRep = PtrRep,
302         isUnLifted   = False,
303         tyConExtName = ext_name
304     }
305
306
307 -- most Prim tycons are lifted
308 mkPrimTyCon name kind arity arg_vrcs rep
309   = mkPrimTyCon' name kind arity arg_vrcs rep True  
310
311 -- but RealWorld is lifted
312 mkLiftedPrimTyCon name kind arity arg_vrcs rep
313   = mkPrimTyCon' name kind arity arg_vrcs rep False
314
315 mkPrimTyCon' name kind arity arg_vrcs rep is_unlifted
316   = PrimTyCon {
317         tyConName    = name,
318         tyConUnique  = nameUnique name,
319         tyConKind    = kind,
320         tyConArity   = arity,
321         argVrcs      = arg_vrcs,
322         primTyConRep = rep,
323         isUnLifted   = is_unlifted,
324         tyConExtName = Nothing
325     }
326
327 mkSynTyCon name kind tyvars rhs argvrcs
328   = SynTyCon {  
329         tyConName = name,
330         tyConUnique = nameUnique name,
331         tyConKind = kind,
332         tyConArity = length tyvars,
333         tyConTyVars = tyvars,
334         synTyConDefn = rhs,
335         argVrcs      = argvrcs
336     }
337 \end{code}
338
339 \begin{code}
340 isFunTyCon :: TyCon -> Bool
341 isFunTyCon (FunTyCon {}) = True
342 isFunTyCon _             = False
343
344 isPrimTyCon :: TyCon -> Bool
345 isPrimTyCon (PrimTyCon {}) = True
346 isPrimTyCon _              = False
347
348 isUnLiftedTyCon :: TyCon -> Bool
349 isUnLiftedTyCon (PrimTyCon  {isUnLifted = is_unlifted}) = is_unlifted
350 isUnLiftedTyCon (TupleTyCon {tyConBoxed = boxity})      = not (isBoxed boxity)
351 isUnLiftedTyCon _                                       = False
352
353 #ifdef UNUSED
354 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
355 isBoxedTyCon :: TyCon -> Bool
356 isBoxedTyCon (AlgTyCon {}) = True
357 isBoxedTyCon (FunTyCon {}) = True
358 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
359 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
360 #endif
361
362 -- isAlgTyCon returns True for both @data@ and @newtype@
363 isAlgTyCon :: TyCon -> Bool
364 isAlgTyCon (AlgTyCon {})   = True
365 isAlgTyCon (TupleTyCon {}) = True
366 isAlgTyCon other           = False
367
368 isDataTyCon :: TyCon -> Bool
369 -- isDataTyCon returns True for data types that are represented by
370 -- heap-allocated constructors.
371 -- These are srcutinised by Core-level @case@ expressions, and they
372 -- get info tables allocated for them.
373 --      True for all @data@ types
374 --      False for newtypes
375 --                unboxed tuples
376 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data})  
377   = case new_or_data of
378         NewTyCon _ -> False
379         other      -> True
380
381 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
382 isDataTyCon other = False
383
384 isNewTyCon :: TyCon -> Bool
385 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True 
386 isNewTyCon other                                     = False
387
388 isProductTyCon :: TyCon -> Bool
389 -- A "product" tycon
390 --      has *one* constructor, 
391 --      is *not* existential
392 -- but
393 --      may be  DataType or NewType, 
394 --      may be  unboxed or not, 
395 --      may be  recursive or not
396 isProductTyCon (AlgTyCon {dataCons = DataCons [data_con]}) = not (isExistentialDataCon data_con)
397 isProductTyCon (TupleTyCon {})                             = True   
398 isProductTyCon other                                       = False
399
400 isSynTyCon :: TyCon -> Bool
401 isSynTyCon (SynTyCon {}) = True
402 isSynTyCon _             = False
403
404 isEnumerationTyCon :: TyCon -> Bool
405 isEnumerationTyCon (AlgTyCon {algTyConFlavour = DataTyCon is_enum}) = is_enum
406 isEnumerationTyCon other                                            = False
407
408 isTupleTyCon :: TyCon -> Bool
409 -- The unit tycon didn't used to be classed as a tuple tycon
410 -- but I thought that was silly so I've undone it
411 -- If it can't be for some reason, it should be a AlgTyCon
412 isTupleTyCon (TupleTyCon {}) = True
413 isTupleTyCon other           = False
414
415 isUnboxedTupleTyCon :: TyCon -> Bool
416 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
417 isUnboxedTupleTyCon other = False
418
419 isBoxedTupleTyCon :: TyCon -> Bool
420 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
421 isBoxedTupleTyCon other = False
422
423 tupleTyConBoxity tc = tyConBoxed tc
424
425 isRecursiveTyCon :: TyCon -> Bool
426 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
427 isRecursiveTyCon other                                = False
428
429 isHiBootTyCon :: TyCon -> Bool
430 -- Used for knot-tying in hi-boot files
431 isHiBootTyCon (AlgTyCon {dataCons = Unknown}) = True
432 isHiBootTyCon other                           = False
433
434 isForeignTyCon :: TyCon -> Bool
435 -- isForeignTyCon identifies foreign-imported type constructors
436 -- For the moment, they are primitive but lifted, but that may change
437 isForeignTyCon (PrimTyCon {isUnLifted = is_unlifted}) = not is_unlifted
438 isForeignTyCon other                                  = False
439 \end{code}
440
441 \begin{code}
442 tyConHasGenerics :: TyCon -> Bool
443 tyConHasGenerics (AlgTyCon {hasGenerics = hg})   = hg
444 tyConHasGenerics (TupleTyCon {hasGenerics = hg}) = hg
445 tyConHasGenerics other                           = False        -- Synonyms
446
447 tyConDataConDetails :: TyCon -> DataConDetails DataCon
448 tyConDataConDetails (AlgTyCon {dataCons = cons}) = cons
449 tyConDataConDetails (TupleTyCon {dataCon = con}) = DataCons [con]
450 tyConDataConDetails other                        = pprPanic "tyConDataConDetails" (ppr other)
451
452 tyConDataCons :: TyCon -> [DataCon]
453 -- It's convenient for tyConDataCons to return the
454 -- empty list for type synonyms etc
455 tyConDataCons tycon = tyConDataCons_maybe tycon `orElse` []
456
457 tyConDataCons_maybe :: TyCon -> Maybe [DataCon]
458 tyConDataCons_maybe (AlgTyCon {dataCons = DataCons cons}) = Just cons
459 tyConDataCons_maybe (TupleTyCon {dataCon = con})          = Just [con]
460 tyConDataCons_maybe other                                 = Nothing
461
462 tyConFamilySize  :: TyCon -> Int
463 tyConFamilySize (AlgTyCon {dataCons = DataCons cs}) = length cs
464 tyConFamilySize (TupleTyCon {})                     = 1
465 #ifdef DEBUG
466 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
467 #endif
468
469 tyConSelIds :: TyCon -> [Id]
470 tyConSelIds (AlgTyCon {selIds = sels}) = sels
471 tyConSelIds other_tycon                = []
472 \end{code}
473
474 \begin{code}
475 newTyConRep :: TyCon -> ([TyVar], Type)
476 newTyConRep (AlgTyCon {tyConTyVars = tvs, algTyConFlavour = NewTyCon rep}) = (tvs, rep)
477
478 tyConPrimRep :: TyCon -> PrimRep
479 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
480 tyConPrimRep tc                               = ASSERT( not (isUnboxedTupleTyCon tc) )
481                                                 PtrRep
482         -- We should not be asking what the representation of an
483         -- unboxed tuple is, because it isn't a first class value.
484 \end{code}
485
486 \begin{code}
487 tyConTheta :: TyCon -> [PredType]
488 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
489 tyConTheta (TupleTyCon {}) = []
490 -- shouldn't ask about anything else
491 \end{code}
492
493 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
494 each tyvar, if available.  See @calcAlgTyConArgVrcs@ for how this is
495 actually computed (in another file).
496
497 \begin{code}
498 tyConArgVrcs :: TyCon -> ArgVrcs
499 tyConArgVrcs tc = expectJust "tyConArgVrcs" (tyConArgVrcs_maybe tc)
500
501 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
502 tyConArgVrcs_maybe (FunTyCon   {})                   = Just [(False,True),(True,False)]
503 tyConArgVrcs_maybe (AlgTyCon   {argVrcs = oi})       = Just oi
504 tyConArgVrcs_maybe (PrimTyCon  {argVrcs = oi})       = Just oi
505 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity}) = Just (replicate arity (True,False))
506 tyConArgVrcs_maybe (SynTyCon   {argVrcs = oi})       = Just oi
507 tyConArgVrcs_maybe _                                 = Nothing
508 \end{code}
509
510 \begin{code}
511 getSynTyConDefn :: TyCon -> ([TyVar], Type)
512 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
513 \end{code}
514
515 \begin{code}
516 maybeTyConSingleCon :: TyCon -> Maybe DataCon
517 maybeTyConSingleCon (AlgTyCon {dataCons = DataCons [c]})  = Just c
518 maybeTyConSingleCon (AlgTyCon {})                         = Nothing
519 maybeTyConSingleCon (TupleTyCon {dataCon = con})          = Just con
520 maybeTyConSingleCon (PrimTyCon {})                        = Nothing
521 maybeTyConSingleCon (FunTyCon {})                         = Nothing  -- case at funty
522 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $ ppr tc
523 \end{code}
524
525 \begin{code}
526 isClassTyCon :: TyCon -> Bool
527 isClassTyCon (AlgTyCon {algTyConClass = Just _}) = True
528 isClassTyCon other_tycon                         = False
529
530 tyConClass_maybe :: TyCon -> Maybe Class
531 tyConClass_maybe (AlgTyCon {algTyConClass = maybe_clas}) = maybe_clas
532 tyConClass_maybe ther_tycon                              = Nothing
533 \end{code}
534
535
536 %************************************************************************
537 %*                                                                      *
538 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
539 %*                                                                      *
540 %************************************************************************
541
542 @TyCon@s are compared by comparing their @Unique@s.
543
544 The strictness analyser needs @Ord@. It is a lexicographic order with
545 the property @(a<=b) || (b<=a)@.
546
547 \begin{code}
548 instance Eq TyCon where
549     a == b = case (a `compare` b) of { EQ -> True;   _ -> False }
550     a /= b = case (a `compare` b) of { EQ -> False;  _ -> True  }
551
552 instance Ord TyCon where
553     a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
554     a <  b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
555     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
556     a >  b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
557     compare a b = getUnique a `compare` getUnique b
558
559 instance Uniquable TyCon where
560     getUnique tc = tyConUnique tc
561
562 instance Outputable TyCon where
563     ppr tc  = ppr (getName tc) 
564
565 instance NamedThing TyCon where
566     getName = tyConName
567 \end{code}