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