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