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