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