[project @ 1999-07-14 14:40:20 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,
9
10         isFunTyCon, isUnLiftedTyCon, isBoxedTyCon, isProductTyCon,
11         isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, isPrimTyCon,
12         isEnumerationTyCon, isTupleTyCon, isUnboxedTupleTyCon,
13
14         mkAlgTyCon,
15         mkFunTyCon,
16         mkPrimTyCon,
17         mkTupleTyCon,
18         mkSynTyCon,
19         mkKindCon,
20         mkSuperKindCon,
21
22         setTyConName,
23
24         tyConKind,
25         tyConUnique,
26         tyConTyVars,
27         tyConArgVrcs_maybe,
28         tyConDataCons,
29         tyConFamilySize,
30         tyConDerivings,
31         tyConTheta,
32         tyConPrimRep,
33         tyConArity,
34         tyConClass_maybe,
35         getSynTyConDefn,
36
37         maybeTyConSingleCon,
38
39         matchesTyCon
40 ) where
41
42 #include "HsVersions.h"
43
44 import {-# SOURCE #-} Type  ( Type, Kind, SuperKind )
45 import {-# SOURCE #-} DataCon ( DataCon )
46
47 import Class            ( Class )
48 import Var              ( TyVar )
49 import BasicTypes       ( Arity, NewOrData(..), RecFlag(..) )
50 import Maybes
51 import Name             ( Name, nameUnique, NamedThing(getName) )
52 import Unique           ( Unique, Uniquable(..), anyBoxConKey )
53 import PrimRep          ( PrimRep(..), isFollowableRep )
54 import Outputable
55 \end{code}
56
57 %************************************************************************
58 %*                                                                      *
59 \subsection{The data type}
60 %*                                                                      *
61 %************************************************************************
62
63 \begin{code}
64 type KindCon      = TyCon
65 type SuperKindCon = TyCon
66
67 data TyCon
68   = FunTyCon {
69         tyConUnique :: Unique,
70         tyConName   :: Name,
71         tyConKind   :: Kind,
72         tyConArity  :: Arity
73     }
74
75
76   | AlgTyCon {          -- Tuples, data type, and newtype decls.
77                         -- All lifted, all boxed
78         tyConUnique :: Unique,
79         tyConName   :: Name,
80         tyConKind   :: Kind,
81         tyConArity  :: Arity,
82         
83         tyConTyVars      :: [TyVar],
84         dataTyConTheta   :: [(Class,[Type])],
85         dataTyConArgVrcs :: ArgVrcs,
86
87         dataCons :: [DataCon],
88                 -- Its data constructors, with fully polymorphic types
89                 --      This list can be empty, when we import a data type abstractly,
90                 --      either (a) the interface is hand-written and doesn't give
91                 --                 the constructors, or
92                 --             (b) in a quest for fast compilation we don't import 
93                 --                 the constructors
94
95         dataTyConDerivings   :: [Class],        -- Classes which have derived instances
96
97         dataTyConClass_maybe :: (Maybe Class),  -- Nothing for ordinary types; 
98                                                 -- Just c for the type constructor
99                                                 -- for dictionaries of class c.
100         algTyConFlavour :: NewOrData,
101         algTyConRec     :: RecFlag              -- Tells whether the data type is part of 
102                                                 -- a mutually-recursive group or not
103     }
104
105   | PrimTyCon {         -- Primitive types; cannot be defined in Haskell
106                         -- NB: All of these guys are *unlifted*, but not all are *unboxed*
107         tyConUnique  :: Unique,
108         tyConName    :: Name,
109         tyConKind    :: Kind,
110         tyConArity   :: Arity,
111         primTyConArgVrcs :: ArgVrcs,
112         primTyConRep :: PrimRep
113     }
114
115   | TupleTyCon {
116
117         tyConUnique :: Unique,
118         tyConName   :: Name,
119         tyConKind   :: Kind,
120         tyConArity  :: Arity,
121         tyConBoxed  :: Bool,            -- True for boxed; False for unboxed
122         tyConTyVars :: [TyVar],
123         dataCon     :: DataCon
124     }
125
126   | SynTyCon {
127         tyConUnique :: Unique,
128         tyConName   :: Name,
129         tyConKind   :: Kind,
130         tyConArity  :: Arity,
131
132         tyConTyVars     :: [TyVar],     -- Bound tyvars
133         synTyConDefn    :: Type,        -- Right-hand side, mentioning these type vars.
134                                         -- Acts as a template for the expansion when
135                                         -- the tycon is applied to some types.
136         synTyConArgVrcs :: ArgVrcs
137     }
138
139   | KindCon {           -- Type constructor at the kind level
140         tyConUnique :: Unique,
141         tyConName   :: Name,
142         tyConKind   :: SuperKind,
143         tyConArity  :: Arity
144     }
145
146   | SuperKindCon        {               -- The type of kind variables or boxity variables,
147         tyConUnique :: Unique,
148         tyConName   :: Name
149     }
150
151 type ArgVrcs = [(Bool,Bool)]  -- Tyvar variance info: [(occPos,occNeg)]
152                               -- *NB*: this is tyvar variance info, *not*
153                               --       termvar usage info.
154 \end{code}
155
156 %************************************************************************
157 %*                                                                      *
158 \subsection{TyCon Construction}
159 %*                                                                      *
160 %************************************************************************
161
162 Note: the TyCon constructors all take a Kind as one argument, even though
163 they could, in principle, work out their Kind from their other arguments.
164 But to do so they need functions from Types, and that makes a nasty
165 module mutual-recursion.  And they aren't called from many places.
166 So we compromise, and move their Kind calculation to the call site.
167
168 \begin{code}
169 mkSuperKindCon :: Name -> SuperKindCon
170 mkSuperKindCon name = SuperKindCon {
171                         tyConUnique = nameUnique name,
172                         tyConName = name
173                       }
174
175 mkKindCon :: Name -> SuperKind -> KindCon
176 mkKindCon name kind
177   = KindCon { 
178         tyConUnique = nameUnique name,
179         tyConName = name,
180         tyConArity = 0,
181         tyConKind = kind
182      }
183
184 mkFunTyCon :: Name -> Kind -> TyCon
185 mkFunTyCon name kind 
186   = FunTyCon { 
187         tyConUnique = nameUnique name,
188         tyConName   = name,
189         tyConKind   = kind,
190         tyConArity  = 2
191     }
192                             
193 mkAlgTyCon name kind tyvars theta argvrcs cons derivs maybe_clas flavour rec
194   = AlgTyCon {  
195         tyConName = name,
196         tyConUnique = nameUnique name,
197         tyConKind = kind,
198         tyConArity = length tyvars,
199         tyConTyVars = tyvars,
200         dataTyConTheta = theta,
201         dataTyConArgVrcs = argvrcs,
202         dataCons = cons,
203         dataTyConDerivings = derivs,
204         dataTyConClass_maybe = maybe_clas,
205         algTyConFlavour = flavour,
206         algTyConRec = rec
207     }
208
209 mkTupleTyCon name kind arity tyvars con boxed
210   = TupleTyCon {
211         tyConUnique = nameUnique name,
212         tyConName = name,
213         tyConKind = kind,
214         tyConArity = arity,
215         tyConBoxed = boxed,
216         tyConTyVars = tyvars,
217         dataCon = con
218     }
219
220 mkPrimTyCon name kind arity arg_vrcs rep 
221   = PrimTyCon {
222         tyConName = name,
223         tyConUnique = nameUnique name,
224         tyConKind = kind,
225         tyConArity = arity,
226         primTyConArgVrcs = arg_vrcs,
227         primTyConRep = rep
228     }
229
230 mkSynTyCon name kind arity tyvars rhs argvrcs
231   = SynTyCon {  
232         tyConName = name,
233         tyConUnique = nameUnique name,
234         tyConKind = kind,
235         tyConArity = arity,
236         tyConTyVars = tyvars,
237         synTyConDefn = rhs,
238         synTyConArgVrcs = argvrcs
239     }
240
241 setTyConName tc name = tc {tyConName = name, tyConUnique = nameUnique name}
242 \end{code}
243
244 \begin{code}
245 isFunTyCon (FunTyCon {}) = True
246 isFunTyCon _             = False
247
248 isPrimTyCon (PrimTyCon {}) = True
249 isPrimTyCon _              = False
250
251 isUnLiftedTyCon (PrimTyCon {}) = True
252 isUnLiftedTyCon (TupleTyCon { tyConBoxed = False }) = True
253 isUnLiftedTyCon _              = False
254
255 -- isBoxedTyCon should not be applied to SynTyCon, nor KindCon
256 isBoxedTyCon (AlgTyCon {}) = True
257 isBoxedTyCon (FunTyCon {}) = True
258 isBoxedTyCon (TupleTyCon {tyConBoxed = boxed}) = boxed
259 isBoxedTyCon (PrimTyCon {primTyConRep = rep}) = isFollowableRep rep
260
261 -- isAlgTyCon returns True for both @data@ and @newtype@
262 isAlgTyCon (AlgTyCon {})   = True
263 isAlgTyCon (TupleTyCon {}) = True
264 isAlgTyCon other           = False
265
266 -- isDataTyCon returns False for @newtype@ and for unboxed tuples
267 isDataTyCon (AlgTyCon {algTyConFlavour = new_or_data})  = case new_or_data of
268                                                                 NewType -> False
269                                                                 other   -> True
270 isDataTyCon (TupleTyCon {tyConBoxed = True}) = True     
271 isDataTyCon other = False
272
273 isNewTyCon (AlgTyCon {algTyConFlavour = NewType}) = True 
274 isNewTyCon other                                  = False
275
276 -- A "product" tycon is non-recursive and has one constructor, and is *not* an unboxed tuple
277 -- whether DataType or NewType
278 isProductTyCon (AlgTyCon {dataCons = [c], algTyConRec = NonRecursive}) = True
279 isProductTyCon (TupleTyCon { tyConBoxed = boxed }) = boxed
280 isProductTyCon other = False
281
282 isSynTyCon (SynTyCon {}) = True
283 isSynTyCon _             = False
284
285 isEnumerationTyCon (AlgTyCon {algTyConFlavour = EnumType}) = True
286 isEnumerationTyCon other                                   = False
287
288 -- The unit tycon isn't classed as a tuple tycon
289 isTupleTyCon (TupleTyCon {tyConArity = arity, tyConBoxed = True}) = arity >= 2
290 isTupleTyCon other = False
291
292 isUnboxedTupleTyCon (TupleTyCon {tyConBoxed = False}) = True
293 isUnboxedTupleTyCon other = False
294 \end{code}
295
296 \begin{code}
297 tyConDataCons :: TyCon -> [DataCon]
298 tyConDataCons (AlgTyCon {dataCons = cons}) = cons
299 tyConDataCons (TupleTyCon {dataCon = con}) = [con]
300 tyConDataCons other                        = []
301         -- You may think this last equation should fail,
302         -- but it's quite convenient to return no constructors for
303         -- a synonym; see for example the call in TcTyClsDecls.
304
305 tyConFamilySize  :: TyCon -> Int
306 tyConFamilySize (AlgTyCon {dataCons = cons}) = length cons
307 tyConFamilySize (TupleTyCon {}) = 1
308 #ifdef DEBUG
309 tyConFamilySize other = pprPanic "tyConFamilySize:" (ppr other)
310 #endif
311
312 tyConPrimRep :: TyCon -> PrimRep
313 tyConPrimRep (PrimTyCon {primTyConRep = rep}) = rep
314 tyConPrimRep _                                = PtrRep
315 \end{code}
316
317 \begin{code}
318 tyConDerivings :: TyCon -> [Class]
319 tyConDerivings (AlgTyCon {dataTyConDerivings = derivs}) = derivs
320 tyConDerivings other                                    = []
321 \end{code}
322
323 \begin{code}
324 tyConTheta :: TyCon -> [(Class, [Type])]
325 tyConTheta (AlgTyCon {dataTyConTheta = theta}) = theta
326 -- should ask about anything else
327 \end{code}
328
329 @tyConArgVrcs_maybe@ gives a list of (occPos,occNeg) flags, one for
330 each tyvar, if available.  See @calcAlgTyConArgVrcs@ for how this is
331 actually computed (in another file).
332
333 \begin{code}
334 tyConArgVrcs_maybe :: TyCon -> Maybe ArgVrcs
335
336 tyConArgVrcs_maybe (FunTyCon   {}                     ) = Just [(False,True),(True,False)]
337 tyConArgVrcs_maybe (AlgTyCon   {dataTyConArgVrcs = oi}) = Just oi
338 tyConArgVrcs_maybe (PrimTyCon  {primTyConArgVrcs = oi}) = Just oi
339 tyConArgVrcs_maybe (TupleTyCon {tyConArity = arity   }) = Just (replicate arity (True,False))
340 tyConArgVrcs_maybe (SynTyCon   {synTyConArgVrcs = oi }) = Just oi
341 tyConArgVrcs_maybe _                                    = Nothing
342 \end{code}
343
344 \begin{code}
345 getSynTyConDefn :: TyCon -> ([TyVar], Type)
346 getSynTyConDefn (SynTyCon {tyConTyVars = tyvars, synTyConDefn = ty}) = (tyvars,ty)
347 \end{code}
348
349 \begin{code}
350 maybeTyConSingleCon :: TyCon -> Maybe DataCon
351 maybeTyConSingleCon (AlgTyCon {dataCons = [c]})  = Just c
352 maybeTyConSingleCon (AlgTyCon {})                = Nothing
353 maybeTyConSingleCon (TupleTyCon {dataCon = con}) = Just con
354 maybeTyConSingleCon (PrimTyCon {})               = Nothing
355 maybeTyConSingleCon (FunTyCon {})                = Nothing  -- case at funty
356 maybeTyConSingleCon tc = pprPanic "maybeTyConSingleCon: unexpected tycon " $
357                          ppr tc
358 \end{code}
359
360 \begin{code}
361 tyConClass_maybe :: TyCon -> Maybe Class
362 tyConClass_maybe (AlgTyCon {dataTyConClass_maybe = maybe_cls}) = maybe_cls
363 tyConClass_maybe other_tycon                                   = Nothing
364 \end{code}
365
366
367 %************************************************************************
368 %*                                                                      *
369 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
370 %*                                                                      *
371 %************************************************************************
372
373 @TyCon@s are compared by comparing their @Unique@s.
374
375 The strictness analyser needs @Ord@. It is a lexicographic order with
376 the property @(a<=b) || (b<=a)@.
377
378 \begin{code}
379 instance Eq TyCon where
380     a == b = case (a `compare` b) of { EQ -> True;   _ -> False }
381     a /= b = case (a `compare` b) of { EQ -> False;  _ -> True  }
382
383 instance Ord TyCon where
384     a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
385     a <  b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
386     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
387     a >  b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
388     compare a b = getUnique a `compare` getUnique b
389
390 instance Uniquable TyCon where
391     getUnique tc = tyConUnique tc
392
393 instance Outputable TyCon where
394     ppr tc  = ppr (getName tc)
395
396 instance NamedThing TyCon where
397     getName = tyConName
398 \end{code}
399
400
401 %************************************************************************
402 %*                                                                      *
403 \subsection{Kind constructors}
404 %*                                                                      *
405 %************************************************************************
406
407 @matchesTyCon tc1 tc2@ checks whether an appliation
408 (tc1 t1..tn) matches (tc2 t1..tn).  By "matches" we basically mean "equals",
409 except that at the kind level tc2 might have more boxity info than tc1.
410
411 \begin{code}
412 matchesTyCon :: TyCon   -- Expected (e.g. arg type of function)
413              -> TyCon   -- Inferred (e.g. type of actual arg to function)
414              -> Bool
415
416 matchesTyCon tc1 tc2 =  uniq1 == uniq2 || uniq1 == anyBoxConKey
417                      where
418                         uniq1 = tyConUnique tc1
419                         uniq2 = tyConUnique tc2
420 \end{code}