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