61c4ac4b9cad1b864305ec7c505692523e11ee9b
[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, AlgTyConFlavour(..),
9
10         isFunTyCon, isUnLiftedTyCon, isBoxedTyCon, isProductTyCon,
11         isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
12         isEnumerationTyCon, 
13         isTupleTyCon, isUnboxedTupleTyCon, isBoxedTupleTyCon, tupleTyConBoxity,
14         isRecursiveTyCon, newTyConRep,
15
16         mkAlgTyCon, --mkAlgTyCon, 
17         mkClassTyCon,
18         mkFunTyCon,
19         mkPrimTyCon,
20         mkTupleTyCon,
21         mkSynTyCon,
22         mkKindCon,
23         mkSuperKindCon,
24
25         setTyConName,
26
27         tyConName,
28         tyConKind,
29         tyConUnique,
30         tyConTyVars,
31         tyConArgVrcs_maybe,
32         tyConDataCons, tyConDataConsIfAvailable, tyConFamilySize,
33         tyConSelIds,
34         tyConTheta,
35         tyConPrimRep,
36         tyConArity,
37         isClassTyCon,
38         getSynTyConDefn,
39
40         maybeTyConSingleCon,
41
42         matchesTyCon,
43
44         -- Generics
45         tyConGenIds, tyConGenInfo
46 ) where
47
48 #include "HsVersions.h"
49
50 import {-# SOURCE #-} TypeRep ( Type, PredType, Kind, SuperKind )
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 BasicTypes       ( Arity, RecFlag(..), Boxity(..), 
59                           isBoxed, EP(..) )
60 import Name             ( Name, nameUnique, NamedThing(getName) )
61 import PrelNames        ( Unique, Uniquable(..), anyBoxConKey )
62 import PrimRep          ( PrimRep(..), isFollowableRep )
63 import Outputable
64 \end{code}
65
66 %************************************************************************
67 %*                                                                      *
68 \subsection{The data type}
69 %*                                                                      *
70 %************************************************************************
71
72 \begin{code}
73 type KindCon      = TyCon
74 type SuperKindCon = TyCon
75
76 data TyCon
77   = FunTyCon {
78         tyConUnique :: Unique,
79         tyConName   :: Name,
80         tyConKind   :: Kind,
81         tyConArity  :: Arity
82     }
83
84
85   | AlgTyCon {          -- Tuples, 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         tyConArgVrcs  :: ArgVrcs,
94         algTyConTheta :: [PredType],
95
96         dataCons :: [DataCon],
97                 -- Its data constructors, with fully polymorphic types
98                 --      This list can be empty, when we import a data type abstractly,
99                 --      either (a) the interface is hand-written and doesn't give
100                 --                 the constructors, or
101                 --             (b) in a quest for fast compilation we don't import 
102                 --                 the constructors
103
104         selIds :: [Id], -- Its record selectors (if any)
105
106         noOfDataCons :: Int,    -- Number of data constructors
107                                 -- Usually this is the same as the length of the
108                                 -- dataCons field, but the latter may be empty if
109                                 -- we imported the type abstractly.  But even if we import
110                                 -- abstractly we still need to know the number of constructors
111                                 -- so we can get the return convention right.  Tiresome!
112                                 
113         algTyConFlavour :: AlgTyConFlavour,
114         algTyConRec     :: RecFlag,             -- Tells whether the data type is part of 
115                                                 -- a mutually-recursive group or not
116
117         genInfo :: Maybe (EP Id),       -- Convert T <-> Tring
118                                         -- Some TyCons don't have it; 
119                                         -- e.g. the TyCon for a Class dictionary,
120                                         -- and TyCons with unboxed arguments
121
122         algTyConClass :: Bool           -- True if this tycon comes from a class declaration
123     }
124
125   | PrimTyCon {         -- Primitive types; cannot be defined in Haskell
126                         -- NB: All of these guys are *unlifted*, but not all are *unboxed*
127         tyConUnique  :: Unique,
128         tyConName    :: Name,
129         tyConKind    :: Kind,
130         tyConArity   :: Arity,
131         tyConArgVrcs :: ArgVrcs,
132         primTyConRep :: PrimRep
133     }
134
135   | TupleTyCon {
136
137         tyConUnique :: Unique,
138         tyConName   :: Name,
139         tyConKind   :: Kind,
140         tyConArity  :: Arity,
141         tyConBoxed  :: Boxity,
142         tyConTyVars :: [TyVar],
143         dataCon     :: DataCon,
144         genInfo     :: Maybe (EP Id)            -- Generic type and conv funs 
145     }
146
147   | SynTyCon {
148         tyConUnique :: Unique,
149         tyConName   :: Name,
150         tyConKind   :: Kind,
151         tyConArity  :: Arity,
152
153         tyConTyVars     :: [TyVar],     -- Bound tyvars
154         synTyConDefn    :: Type,        -- Right-hand side, mentioning these type vars.
155                                         -- Acts as a template for the expansion when
156                                         -- the tycon is applied to some types.
157         tyConArgVrcs :: ArgVrcs
158     }
159
160   | KindCon {           -- Type constructor at the kind level
161         tyConUnique :: Unique,
162         tyConName   :: Name,
163         tyConKind   :: SuperKind,
164         tyConArity  :: Arity
165     }
166
167   | SuperKindCon        {               -- The type of kind variables or boxity variables,
168         tyConUnique :: Unique,
169         tyConName   :: Name
170     }
171
172 type ArgVrcs = [(Bool,Bool)]  -- Tyvar variance info: [(occPos,occNeg)]
173
174 data AlgTyConFlavour
175   = DataTyCon           -- Data type
176   | EnumTyCon           -- Special sort of enumeration type
177   | NewTyCon Type       -- Newtype, with its *ultimate* representation type
178                         -- By 'ultimate' I mean that the rep type is not itself
179                         -- a newtype or type synonym.
180
181                         -- The rep type has explicit for-alls for the tyvars of
182                         -- the TyCon.  Thus:
183                         --      newtype T a = MkT [(a,Int)]
184                         -- The rep type is forall a. [(a,Int)]
185                         --
186                         -- The rep type isn't entirely simple:
187                         --  for a recursive newtype we pick () as the rep type
188                         --      newtype T = MkT T
189 \end{code}
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 mkSuperKindCon :: Name -> SuperKindCon
205 mkSuperKindCon name = SuperKindCon {
206                         tyConUnique = nameUnique name,
207                         tyConName = name
208                       }
209
210 mkKindCon :: Name -> SuperKind -> KindCon
211 mkKindCon name kind
212   = KindCon { 
213         tyConUnique = nameUnique name,
214         tyConName = name,
215         tyConArity = 0,
216         tyConKind = kind
217      }
218
219 mkFunTyCon :: Name -> Kind -> TyCon
220 mkFunTyCon name kind 
221   = FunTyCon { 
222         tyConUnique = nameUnique name,
223         tyConName   = name,
224         tyConKind   = kind,
225         tyConArity  = 2
226     }
227
228 tyConGenInfo :: TyCon -> Maybe (EP Id)
229 tyConGenInfo (AlgTyCon   { genInfo = info }) = info
230 tyConGenInfo (TupleTyCon { genInfo = info }) = info
231 tyConGenInfo other                           = Nothing
232
233 tyConGenIds :: TyCon -> [Id]
234 -- Returns the generic-programming Ids; these Ids need bindings
235 tyConGenIds tycon = case tyConGenInfo tycon of
236                         Nothing           -> []
237                         Just (EP from to) -> [from,to]
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 ncons sels flavour rec 
243               gen_info
244   = AlgTyCon {  
245         tyConName               = name,
246         tyConUnique             = nameUnique name,
247         tyConKind               = kind,
248         tyConArity              = length tyvars,
249         tyConTyVars             = tyvars,
250         tyConArgVrcs            = argvrcs,
251         algTyConTheta           = theta,
252         dataCons                = cons, 
253         selIds                  = sels,
254         noOfDataCons            = ncons,
255         algTyConClass           = False,
256         algTyConFlavour         = flavour,
257         algTyConRec             = rec,
258         genInfo                 = gen_info
259     }
260
261 mkClassTyCon name kind tyvars argvrcs con clas flavour
262   = AlgTyCon {  
263         tyConName               = name,
264         tyConUnique             = nameUnique name,
265         tyConKind               = kind,
266         tyConArity              = length tyvars,
267         tyConTyVars             = tyvars,
268         tyConArgVrcs            = argvrcs,
269         algTyConTheta           = [],
270         dataCons                = [con],
271         selIds                  = [],
272         noOfDataCons            = 1,
273         algTyConClass           = True,
274         algTyConFlavour         = flavour,
275         algTyConRec             = NonRecursive,
276         genInfo                 = Nothing
277     }
278
279
280 mkTupleTyCon name kind arity tyvars con boxed gen_info
281   = TupleTyCon {
282         tyConUnique = nameUnique name,
283         tyConName = name,
284         tyConKind = kind,
285         tyConArity = arity,
286         tyConBoxed = boxed,
287         tyConTyVars = tyvars,
288         dataCon = con,
289         genInfo = gen_info
290     }
291
292 mkPrimTyCon name kind arity arg_vrcs rep 
293   = PrimTyCon {
294         tyConName = name,
295         tyConUnique = nameUnique name,
296         tyConKind = kind,
297         tyConArity = arity,
298         tyConArgVrcs = arg_vrcs,
299         primTyConRep = rep
300     }
301
302 mkSynTyCon name kind arity tyvars rhs argvrcs
303   = SynTyCon {  
304         tyConName = name,
305         tyConUnique = nameUnique name,
306         tyConKind = kind,
307         tyConArity = arity,
308         tyConTyVars = tyvars,
309         synTyConDefn = rhs,
310         tyConArgVrcs = argvrcs
311     }
312
313 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
314
315 \end{code}
316
317 \begin{code}
318 isFunTyCon (FunTyCon {}) = True
319 isFunTyCon _             = False
320
321 isPrimTyCon (PrimTyCon {}) = True
322 isPrimTyCon _              = False
323
324 isUnLiftedTyCon (PrimTyCon {}) = True
325 isUnLiftedTyCon (TupleTyCon { tyConBoxed = boxity}) = not (isBoxed boxity)
326 isUnLiftedTyCon _              = False
327
328 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
329 isBoxedTyCon (AlgTyCon {}) = True
330 isBoxedTyCon (FunTyCon {}) = True
331 isBoxedTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
332 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
333
334 -- isAlgTyCon returns True for both @data@ and @newtype@
335 isAlgTyCon (AlgTyCon {})   = True
336 isAlgTyCon (TupleTyCon {}) = True
337 isAlgTyCon other           = False
338
339 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
340 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data})  = case new_or_data of
341                                                                 NewTyCon _ -> False
342                                                                 other   -> True
343 isDataTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
344 isDataTyCon other = False
345
346 isNewTyCon (AlgTyCon {algTyConFlavour = NewTyCon _}) = True 
347 isNewTyCon other                                     = False
348
349 newTyConRep (AlgTyCon {algTyConFlavour = NewTyCon rep}) = Just rep
350 newTyConRep other                                       = Nothing
351
352 -- A "product" tycon
353 --      has *one* constructor, 
354 --      is *not* existential
355 -- but
356 --      may be  DataType or NewType, 
357 --      may be  unboxed or not, 
358 --      may be  recursive or not
359 isProductTyCon (AlgTyCon {dataCons = [data_con]}) = not (isExistentialDataCon data_con)
360 isProductTyCon (TupleTyCon {})                    = True
361 isProductTyCon other                              = False
362
363 isSynTyCon (SynTyCon {}) = True
364 isSynTyCon _             = False
365
366 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumTyCon}) = True
367 isEnumerationTyCon other                                    = False
368
369 -- The unit tycon didn't used to be classed as a tuple tycon
370 -- but I thought that was silly so I've undone it
371 -- If it can't be for some reason, it should be a AlgTyCon
372 isTupleTyCon (TupleTyCon {}) = True
373 isTupleTyCon other           = False
374
375 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = not (isBoxed boxity)
376 isUnboxedTupleTyCon other = False
377
378 isBoxedTupleTyCon (TupleTyCon {tyConBoxed = boxity}) = isBoxed boxity
379 isBoxedTupleTyCon other = False
380
381 tupleTyConBoxity tc = tyConBoxed tc
382
383 isRecursiveTyCon (AlgTyCon {algTyConRec = Recursive}) = True
384 isRecursiveTyCon other                                = False
385 \end{code}
386
387 \begin{code}
388 tyConDataCons :: TyCon -> [DataCon]
389 tyConDataCons tycon = ASSERT2( not (null cons), ppr tycon ) 
390                       ASSERT2( length cons == tyConFamilySize tycon, ppr tycon )
391                       cons
392                     where
393                       cons = tyConDataConsIfAvailable tycon
394
395 tyConDataConsIfAvailable (AlgTyCon {dataCons = cons}) = cons    -- Empty for abstract types
396 tyConDataConsIfAvailable (TupleTyCon {dataCon = con}) = [con]
397 tyConDataConsIfAvailable other                        = []
398         -- You may think this last equation should fail,
399         -- but it's quite convenient to return no constructors for
400         -- a synonym; see for example the call in TcTyClsDecls.
401
402 tyConFamilySize  :: TyCon -> Int
403 tyConFamilySize (AlgTyCon {noOfDataCons = n}) = n
404 tyConFamilySize (TupleTyCon {})               = 1
405 #ifdef DEBUG
406 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
407 #endif
408
409 tyConSelIds :: TyCon -> [Id]
410 tyConSelIds (AlgTyCon {selIds = sels}) = sels
411 tyConSelIds other_tycon                = []
412 \end{code}
413
414 \begin{code}
415 tyConPrimRep :: TyCon -> PrimRep
416 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
417 tyConPrimRep _                                = PtrRep
418 \end{code}
419
420 \begin{code}
421 tyConTheta :: TyCon -> [PredType]
422 tyConTheta (AlgTyCon {algTyConTheta = theta}) = theta
423 -- should ask about anything else
424 \end{code}
425
426 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
427 each tyvar, if available.  See @calcAlgTyConArgVrcs@ for how this is
428 actually computed (in another file).
429
430 \begin{code}
431 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
432
433 tyConArgVrcs_maybe (FunTyCon   {}                     ) = Just [(False,True),(True,False)]
434 tyConArgVrcs_maybe (AlgTyCon   {tyConArgVrcs = oi})     = Just oi
435 tyConArgVrcs_maybe (PrimTyCon  {tyConArgVrcs = oi})     = Just oi
436 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity   }) = Just (replicate arity (True,False))
437 tyConArgVrcs_maybe (SynTyCon   {tyConArgVrcs = oi })    = Just oi
438 tyConArgVrcs_maybe _                                    = Nothing
439 \end{code}
440
441 \begin{code}
442 getSynTyConDefn :: TyCon -> ([TyVar], Type)
443 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
444 \end{code}
445
446 \begin{code}
447 maybeTyConSingleCon :: TyCon -> Maybe DataCon
448 maybeTyConSingleCon (AlgTyCon {dataCons = [c]})  = Just c
449 maybeTyConSingleCon (AlgTyCon {})                = Nothing
450 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
451 maybeTyConSingleCon (PrimTyCon {})               = Nothing
452 maybeTyConSingleCon (FunTyCon {})                = Nothing  -- case at funty
453 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
454                          ppr tc
455 \end{code}
456
457 \begin{code}
458 isClassTyCon :: TyCon -> Bool
459 isClassTyCon (AlgTyCon {algTyConClass = is_class_tycon}) = is_class_tycon
460 isClassTyCon other_tycon                                 = False
461 \end{code}
462
463
464 %************************************************************************
465 %*                                                                      *
466 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
467 %*                                                                      *
468 %************************************************************************
469
470 @TyCon@s are compared by comparing their @Unique@s.
471
472 The strictness analyser needs @Ord@. It is a lexicographic order with
473 the property @(a<=b) || (b<=a)@.
474
475 \begin{code}
476 instance Eq TyCon where
477     a == b = case (a `compare` b) of { EQ -> True;   _ -> False }
478     a /= b = case (a `compare` b) of { EQ -> False;  _ -> True  }
479
480 instance Ord TyCon where
481     a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
482     a <  b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
483     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
484     a >  b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
485     compare a b = getUnique a `compare` getUnique b
486
487 instance Uniquable TyCon where
488     getUnique tc = tyConUnique tc
489
490 instance Outputable TyCon where
491     ppr tc  = ppr (getName tc) 
492
493 instance NamedThing TyCon where
494     getName = tyConName
495 \end{code}
496
497
498 %************************************************************************
499 %*                                                                      *
500 \subsection{Kind constructors}
501 %*                                                                      *
502 %************************************************************************
503
504 @matchesTyCon tc1 tc2@ checks whether an appliation
505 (tc1 t1..tn) matches (tc2 t1..tn).  By "matches" we basically mean "equals",
506 except that at the kind level tc2 might have more boxity info than tc1.
507
508 \begin{code}
509 matchesTyCon :: TyCon   -- Expected (e.g. arg type of function)
510              -> TyCon   -- Inferred (e.g. type of actual arg to function)
511              -> Bool
512
513 matchesTyCon tc1 tc2 =  uniq1 == uniq2 || uniq1 == anyBoxConKey
514                      where
515                         uniq1 = tyConUnique tc1
516                         uniq2 = tyConUnique tc2
517 \end{code}
518
519
520