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