[project @ 1997-05-26 01:21:27 by sof]
[ghc-hetmet.git] / ghc / compiler / types / TyCon.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[TyCon]{The @TyCon@ datatype}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module TyCon(
10         TyCon(..),      -- NB: some pals need to see representation
11
12         SYN_IE(Arity), NewOrData(..),
13
14         isFunTyCon, isPrimTyCon, isBoxedTyCon,
15         isAlgTyCon, isDataTyCon, isSynTyCon, isNewTyCon, maybeNewTyCon,
16
17         mkDataTyCon,
18         mkFunTyCon,
19         mkPrimTyCon,
20         mkSpecTyCon,
21         mkTupleTyCon,
22
23         mkSynTyCon,
24
25         tyConKind,
26         tyConUnique,
27         tyConTyVars,
28         tyConDataCons,
29         tyConFamilySize,
30         tyConDerivings,
31         tyConTheta,
32         tyConPrimRep,
33         synTyConArity,
34         getSynTyConDefn,
35
36         maybeTyConSingleCon,
37         isEnumerationTyCon, isTupleTyCon,
38         derivedClasses
39 ) where
40
41 CHK_Ubiq()      -- debugging consistency check
42
43 IMPORT_DELOOPER(TyLoop) ( SYN_IE(Type), GenType,
44                           SYN_IE(Class), GenClass,
45                           SYN_IE(Id), GenId,
46                           splitSigmaTy, splitFunTy,
47                           tupleCon, isNullaryDataCon, idType
48                           --LATER: specMaybeTysSuffix
49                         )
50
51 import BasicTypes       ( SYN_IE(Arity), NewOrData(..) )
52 import TyVar            ( GenTyVar, alphaTyVars, alphaTyVar, betaTyVar, SYN_IE(TyVar) )
53 import Usage            ( GenUsage, SYN_IE(Usage) )
54 import Kind             ( Kind, mkBoxedTypeKind, mkArrowKind, resultKind, argKind )
55
56 import Maybes
57 import Name             ( Name, nameUnique, mkWiredInTyConName, NamedThing(getName) )
58 import Unique           ( Unique, funTyConKey )
59 import UniqFM           ( Uniquable(..) )
60 import Pretty           ( Doc )
61 import PrimRep          ( PrimRep(..) )
62 import PrelMods         ( gHC__, pREL_TUP, pREL_BASE )
63 import Lex              ( mkTupNameStr )
64 import SrcLoc           ( SrcLoc, mkBuiltinSrcLoc )
65 import Util             ( nOfThem, isIn, Ord3(..), panic, panic#, assertPanic )
66 \end{code}
67
68 \begin{code}
69 data TyCon
70   = FunTyCon            -- Kind = Type -> Type -> Type
71
72   | DataTyCon   Unique{-TyConKey-}
73                 Name
74                 Kind
75                 [TyVar]
76                 [(Class,Type)]  -- Its context
77                 [Id{-DataCon-}] -- Its data constructors, with fully polymorphic types
78                                 --      This list can be empty, when we import a data type abstractly,
79                                 --      either (a) the interface is hand-written and doesn't give
80                                 --                 the constructors, or
81                                 --             (b) in a quest for fast compilation we don't import 
82                                 --                 the constructors
83                 [Class]         -- Classes which have derived instances
84                 NewOrData
85
86   | TupleTyCon  Unique          -- cached
87                 Name            -- again, we could do without this, but
88                                 -- it makes life somewhat easier
89                 Arity   -- just a special case of DataTyCon
90                         -- Kind = BoxedTypeKind
91                         --      -> ... (n times) ...
92                         --      -> BoxedTypeKind
93                         --      -> BoxedTypeKind
94
95   | PrimTyCon           -- Primitive types; cannot be defined in Haskell
96         Unique          -- Always unboxed; hence never represented by a closure
97         Name            -- Often represented by a bit-pattern for the thing
98         Kind            -- itself (eg Int#), but sometimes by a pointer to
99         PrimRep
100
101   | SpecTyCon           -- A specialised TyCon; eg (Arr# Int#), or (List Int#)
102         TyCon
103         [Maybe Type]    -- Specialising types
104
105         --      OLD STUFF ABOUT Array types.  Use SpecTyCon instead
106         -- ([PrimRep] -> PrimRep) -- a heap-allocated object (eg ArrInt#).
107         -- The primitive types Arr# and StablePtr# have
108         -- parameters (hence arity /= 0); but the rest don't.
109         -- Only arrays use the list in a non-trivial way.
110         -- Length of that list must == arity.
111
112   | SynTyCon
113         Unique
114         Name
115         Kind
116         Arity
117         [TyVar]         -- Argument type variables
118         Type            -- Right-hand side, mentioning these type vars.
119                         -- Acts as a template for the expansion when
120                         -- the tycon is applied to some types.
121 \end{code}
122
123 \begin{code}
124 mkFunTyCon     = FunTyCon
125 mkFunTyConName = mkWiredInTyConName funTyConKey gHC__ SLIT("->") FunTyCon
126
127 mkSpecTyCon  = SpecTyCon
128 mkTupleTyCon = TupleTyCon
129
130 mkDataTyCon name = DataTyCon (nameUnique name) name
131 mkPrimTyCon name = PrimTyCon (nameUnique name) name
132 mkSynTyCon  name = SynTyCon  (nameUnique name) name
133
134 isFunTyCon FunTyCon = True
135 isFunTyCon _ = False
136
137 isPrimTyCon (PrimTyCon _ _ _ _) = True
138 isPrimTyCon _ = False
139
140 -- At present there are no unboxed non-primitive types, so
141 -- isBoxedTyCon is just the negation of isPrimTyCon.
142 isBoxedTyCon = not . isPrimTyCon
143
144 -- isAlgTyCon returns True for both @data@ and @newtype@
145 isAlgTyCon (DataTyCon _ _ _ _ _ _ _ _) = True
146 isAlgTyCon (TupleTyCon _ _ _)          = True
147 isAlgTyCon other                       = False
148
149 -- isDataTyCon returns False for @newtype@.
150 isDataTyCon (DataTyCon _ _ _ _ _ _ _ DataType) = True
151 isDataTyCon (TupleTyCon _ _ _)                 = True
152 isDataTyCon other                              = False
153
154 maybeNewTyCon :: TyCon -> Maybe ([TyVar], Type)         -- Returns representation type info
155 maybeNewTyCon (DataTyCon _ _ _ _ _ (con:null_cons) _ NewType) 
156   = ASSERT( null null_cons && null null_tys)
157     Just (tyvars, rep_ty)
158   where
159     (tyvars, theta, tau)      = splitSigmaTy (idType con)
160     (rep_ty:null_tys, res_ty) = splitFunTy tau
161
162 maybeNewTyCon other = Nothing
163
164 isNewTyCon (DataTyCon _ _ _ _ _ _ _ NewType) = True 
165 isNewTyCon other                             = False
166
167 isSynTyCon (SynTyCon _ _ _ _ _ _) = True
168 isSynTyCon _                      = False
169 \end{code}
170
171 \begin{code}
172 -- Special cases to avoid reconstructing lots of kinds
173 kind1 = mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind
174 kind2 = mkBoxedTypeKind `mkArrowKind` kind1
175
176 tyConKind :: TyCon -> Kind
177 tyConKind FunTyCon                       = kind2
178 tyConKind (DataTyCon _ _ kind _ _ _ _ _) = kind
179 tyConKind (PrimTyCon _ _ kind _)         = kind
180 tyConKind (SynTyCon _ _ k _ _ _)         = k
181
182 tyConKind (TupleTyCon _ _ n)
183   = mkArrow n
184    where
185     mkArrow 0 = mkBoxedTypeKind
186     mkArrow 1 = kind1
187     mkArrow 2 = kind2
188     mkArrow n = mkBoxedTypeKind `mkArrowKind` mkArrow (n-1)
189
190 tyConKind (SpecTyCon tc tys)
191   = spec (tyConKind tc) tys
192    where
193     spec kind []              = kind
194     spec kind (Just _  : tys) = spec (resultKind kind) tys
195     spec kind (Nothing : tys) =
196       argKind kind `mkArrowKind` spec (resultKind kind) tys
197 \end{code}
198
199 \begin{code}
200 tyConUnique :: TyCon -> Unique
201 tyConUnique FunTyCon                       = funTyConKey
202 tyConUnique (DataTyCon uniq _ _ _ _ _ _ _) = uniq
203 tyConUnique (TupleTyCon uniq _ _)          = uniq
204 tyConUnique (PrimTyCon uniq _ _ _)         = uniq
205 tyConUnique (SynTyCon uniq _ _ _ _ _)      = uniq
206 tyConUnique (SpecTyCon _ _ )               = panic "tyConUnique:SpecTyCon"
207
208 synTyConArity :: TyCon -> Maybe Arity -- Nothing <=> not a syn tycon
209 synTyConArity (SynTyCon _ _ _ arity _ _) = Just arity
210 synTyConArity _                          = Nothing
211 \end{code}
212
213 \begin{code}
214 tyConTyVars :: TyCon -> [TyVar]
215 tyConTyVars FunTyCon                      = [alphaTyVar,betaTyVar]
216 tyConTyVars (DataTyCon _ _ _ tvs _ _ _ _) = tvs
217 tyConTyVars (TupleTyCon _ _ arity)        = take arity alphaTyVars
218 tyConTyVars (SynTyCon _ _ _ _ tvs _)      = tvs
219 #ifdef DEBUG
220 tyConTyVars (PrimTyCon _ _ _ _)           = panic "tyConTyVars:PrimTyCon"
221 tyConTyVars (SpecTyCon _ _ )              = panic "tyConTyVars:SpecTyCon"
222 #endif
223 \end{code}
224
225 \begin{code}
226 tyConDataCons :: TyCon -> [Id]
227 tyConFamilySize  :: TyCon -> Int
228
229 tyConDataCons (DataTyCon _ _ _ _ _ data_cons _ _) = data_cons
230 tyConDataCons (TupleTyCon _ _ a)                  = [tupleCon a]
231 tyConDataCons other                               = []
232         -- You may think this last equation should fail,
233         -- but it's quite convenient to return no constructors for
234         -- a synonym; see for example the call in TcTyClsDecls.
235
236 tyConFamilySize (DataTyCon _ _ _ _ _ data_cons _ _) = length data_cons
237 tyConFamilySize (TupleTyCon _ _ _)                  = 1
238 #ifdef DEBUG
239 --tyConFamilySize other = pprPanic "tyConFamilySize:" (pprTyCon PprDebug other)
240 #endif
241
242 tyConPrimRep :: TyCon -> PrimRep
243 tyConPrimRep (PrimTyCon _ _ _ rep) = rep
244 tyConPrimRep _                     = PtrRep
245 \end{code}
246
247 \begin{code}
248 tyConDerivings :: TyCon -> [Class]
249 tyConDerivings (DataTyCon _ _ _ _ _ _ derivs _) = derivs
250 tyConDerivings other                            = []
251 \end{code}
252
253 \begin{code}
254 tyConTheta :: TyCon -> [(Class,Type)]
255 tyConTheta (DataTyCon _ _ _ _ theta _ _ _) = theta
256 tyConTheta (TupleTyCon _ _ _)              = []
257 -- should ask about anything else
258 \end{code}
259
260 \begin{code}
261 getSynTyConDefn :: TyCon -> ([TyVar], Type)
262 getSynTyConDefn (SynTyCon _ _ _ _ tyvars ty) = (tyvars,ty)
263 \end{code}
264
265 \begin{code}
266 maybeTyConSingleCon :: TyCon -> Maybe Id
267
268 maybeTyConSingleCon (TupleTyCon _ _ arity)        = Just (tupleCon arity)
269 maybeTyConSingleCon (DataTyCon _ _ _ _ _ [c] _ _) = Just c
270 maybeTyConSingleCon (DataTyCon _ _ _ _ _ _   _ _) = Nothing
271 maybeTyConSingleCon (PrimTyCon _ _ _ _)           = Nothing
272 maybeTyConSingleCon (SpecTyCon tc tys)            = panic "maybeTyConSingleCon:SpecTyCon"
273                                                   -- requires DataCons of TyCon
274
275 isEnumerationTyCon (TupleTyCon _ _ arity)
276   = arity == 0
277 isEnumerationTyCon (DataTyCon _ _ _ _ _ data_cons _ _)
278   = not (null data_cons) && all isNullaryDataCon data_cons
279
280
281 isTupleTyCon (TupleTyCon _ _ arity) = arity >= 2    -- treat "0-tuple" specially
282 isTupleTyCon (SpecTyCon tc tys)     = isTupleTyCon tc
283 isTupleTyCon other                  = False
284
285
286 \end{code}
287
288 @derivedFor@ reports if we have an {\em obviously}-derived instance
289 for the given class/tycon.  Of course, you might be deriving something
290 because it a superclass of some other obviously-derived class --- this
291 function doesn't deal with that.
292
293 ToDo: what about derivings for specialised tycons !!!
294
295 \begin{code}
296 derivedClasses :: TyCon -> [Class]
297 derivedClasses (DataTyCon _ _ _ _ _ _ derivs _) = derivs
298 derivedClasses something_weird                  = []
299 \end{code}
300
301 %************************************************************************
302 %*                                                                      *
303 \subsection[TyCon-instances]{Instance declarations for @TyCon@}
304 %*                                                                      *
305 %************************************************************************
306
307 @TyCon@s are compared by comparing their @Unique@s.
308
309 The strictness analyser needs @Ord@. It is a lexicographic order with
310 the property @(a<=b) || (b<=a)@.
311
312 \begin{code}
313 instance Ord3 TyCon where
314   cmp tc1 tc2 = uniqueOf tc1 `cmp` uniqueOf tc2
315
316 instance Eq TyCon where
317     a == b = case (a `cmp` b) of { EQ_ -> True;   _ -> False }
318     a /= b = case (a `cmp` b) of { EQ_ -> False;  _ -> True  }
319
320 instance Ord TyCon where
321     a <= b = case (a `cmp` b) of { LT_ -> True;  EQ_ -> True;  GT__ -> False }
322     a <  b = case (a `cmp` b) of { LT_ -> True;  EQ_ -> False; GT__ -> False }
323     a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True;  GT__ -> True  }
324     a >  b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True  }
325     _tagCmp a b = case (a `cmp` b) of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
326
327 instance Uniquable TyCon where
328     uniqueOf (DataTyCon  u _ _ _ _ _ _ _) = u
329     uniqueOf (TupleTyCon u _ _)           = u
330     uniqueOf (PrimTyCon  u _ _ _)         = u
331     uniqueOf (SynTyCon   u _ _ _ _ _)     = u
332     uniqueOf tc@(SpecTyCon _ _)           = panic "uniqueOf:SpecTyCon"
333     uniqueOf tc                           = uniqueOf (getName tc)
334 \end{code}
335
336 \begin{code}
337 instance NamedThing TyCon where
338     getName (DataTyCon _ n _ _ _ _ _ _) = n
339     getName (PrimTyCon _ n _ _)         = n
340     getName (SpecTyCon tc _)            = getName tc
341     getName (SynTyCon _ n _ _ _ _)      = n
342     getName FunTyCon                    = mkFunTyConName
343     getName (TupleTyCon _ n _)          = n
344     getName tc                          = panic "TyCon.getName"
345
346 {- LATER:
347     getName (SpecTyCon tc tys) = let (OrigName m n) = origName "????" tc in
348                              (m, n _APPEND_ specMaybeTysSuffix tys)
349     getName     other_tc           = moduleNamePair (expectJust "tycon1" (getName other_tc))
350     getName other                            = Nothing
351 -}
352
353 \end{code}