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