[project @ 1997-05-18 21:55:39 by sof]
[ghc-hetmet.git] / ghc / compiler / types / Class.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[Class]{The @Class@ datatype}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module Class (
10         GenClass(..), SYN_IE(Class),
11
12         mkClass,
13         classKey, classOps, classGlobalIds,
14         classSuperDictSelId, classOpId, classDefaultMethodId,
15         classSig, classBigSig, classInstEnv,
16         isSuperClassOf,
17         classOpTagByOccName, classOpTagByOccName_maybe,
18
19         GenClassOp(..), SYN_IE(ClassOp),
20         mkClassOp,
21         classOpTag, classOpString,
22         classOpLocalType,
23
24         SYN_IE(ClassInstEnv)
25     ) where
26
27 CHK_Ubiq() -- debugging consistency check
28
29 IMPORT_DELOOPER(TyLoop)
30 --IMPORT_DELOOPER(IdLoop)
31
32
33 --import TyVar
34 --import Id
35 --import Type
36 #if __GLASGOW_HASKELL__ >= 202
37 import Name
38 #endif
39
40 import TyCon            --( TyCon )
41 import TyVar            --( SYN_IE(TyVar), GenTyVar )
42 import Usage            --( GenUsage, SYN_IE(Usage), SYN_IE(UVar) )
43
44 import MatchEnv         ( MatchEnv )
45 import Maybes           ( assocMaybe )
46 import Name             ( changeUnique, Name, OccName, occNameString )
47 import Outputable
48 import Unique           -- Keys for built-in classes
49 import UniqFM           ( SYN_IE(Uniquable))
50 import Pretty           ( Doc, hsep, ptext )
51 import PprStyle         ( PprStyle(..) )
52 import SrcLoc           ( SrcLoc )
53 import Util
54 \end{code}
55
56 %************************************************************************
57 %*                                                                      *
58 \subsection[Class-basic]{@Class@: basic definition}
59 %*                                                                      *
60 %************************************************************************
61
62 A @Class@ corresponds to a Greek kappa in the static semantics:
63
64 The parameterisation wrt tyvar and uvar is only necessary to
65 get appropriately general instances of Ord3 for GenType.
66
67 \begin{code}
68 data GenClassOp ty
69   = ClassOp     OccName -- The operation name
70
71                 Int     -- Unique within a class; starts at 1
72
73                 ty      -- Type; the class tyvar is free (you can find
74                         -- it from the class). This means that a
75                         -- ClassOp doesn't make much sense outside the
76                         -- context of its parent class.
77
78 data GenClass tyvar uvar
79   = Class
80         Unique          -- Key for fast comparison
81         Name
82
83         tyvar           -- The class type variable
84
85         [GenClass tyvar uvar]   -- Immediate superclasses, and the
86         [Id]                    -- corresponding selector functions to
87                                 -- extract them from a dictionary of this
88                                 -- class
89
90         [GenClassOp (GenType tyvar uvar)] -- The * class operations
91         [Id]                              --     * selector functions
92         [Id]                              --     * default methods
93                           -- They are all ordered by tag.  The
94                           -- selector ids are less innocent than they
95                           -- look, because their IdInfos contains
96                           -- suitable specialisation information.  In
97                           -- particular, constant methods are
98                           -- instances of selectors at suitably simple
99                           -- types.
100
101         ClassInstEnv      -- Gives details of all the instances of this class
102
103         [(GenClass tyvar uvar, [GenClass tyvar uvar])]
104                           -- Indirect superclasses;
105                           --   (k,[k1,...,kn]) means that
106                           --   k is an immediate superclass of k1
107                           --   k1 is an immediate superclass of k2
108                           --   ... and kn is an immediate superclass
109                           -- of this class.  (This is all redundant
110                           -- information, since it can be derived from
111                           -- the superclass information above.)
112
113 type Class        = GenClass TyVar UVar
114 type ClassOp      = GenClassOp Type
115
116 type ClassInstEnv = MatchEnv Type Id            -- The Ids are dfuns
117 \end{code}
118
119 The @mkClass@ function fills in the indirect superclasses.
120
121 \begin{code}
122 mkClass :: Unique -> Name -> TyVar
123         -> [Class] -> [Id]
124         -> [ClassOp] -> [Id] -> [Id]
125         -> ClassInstEnv
126         -> Class
127
128 mkClass uniq full_name tyvar super_classes superdict_sels
129         class_ops dict_sels defms class_insts
130   = Class uniq (changeUnique full_name uniq) tyvar
131                 super_classes superdict_sels
132                 class_ops dict_sels defms
133                 class_insts
134                 trans_clos
135   where
136     trans_clos :: [(Class,[Class])]
137     trans_clos = transitiveClosure succ (==) [ (clas, []) | clas <- super_classes ]
138
139     succ (clas@(Class _ _ _ super_classes _ _ _ _ _ _), links)
140       = [(super, (clas:links)) | super <- super_classes]
141 \end{code}
142
143 %************************************************************************
144 %*                                                                      *
145 \subsection[Class-selectors]{@Class@: simple selectors}
146 %*                                                                      *
147 %************************************************************************
148
149 The rest of these functions are just simple selectors.
150
151 \begin{code}
152 classKey (Class key _ _ _ _ _ _ _ _ _) = key
153 classOps (Class _ _ _ _ _ ops _ _ _ _) = ops
154 classGlobalIds (Class _ _ _ _ _ _ sels defm_ids _ _) = sels ++ defm_ids
155
156 classOpId (Class _ _ _ _ _ ops op_ids _ _ _) op
157   = op_ids !! (classOpTag op - 1)
158
159 classDefaultMethodId (Class _ _ _ _ _ ops _ defm_ids _ _) idx
160   = defm_ids !! idx
161
162 classSuperDictSelId (Class _ _ _ scs scsel_ids _ _ _ _ _) super_clas
163   = assoc "classSuperDictSelId" (scs `zip` scsel_ids) super_clas
164
165 classSig :: GenClass t u -> (t, [GenClass t u], [GenClassOp (GenType t u)])
166 classSig (Class _ _ tyvar super_classes _ ops _ _ _ _)
167   = (tyvar, super_classes, ops)
168
169 classBigSig (Class _ _ tyvar super_classes sdsels ops sels defms _ _)
170   = (tyvar, super_classes, sdsels, ops, sels, defms)
171
172 classInstEnv (Class _ _ _ _ _ _ _ _ inst_env _) = inst_env
173 \end{code}
174
175 @a `isSuperClassOf` b@ returns @Nothing@ if @a@ is not a superclass of
176 @b@, but if it is, it returns $@Just@~[k_1,\ldots,k_n]$, where the
177 $k_1,\ldots,k_n$ are exactly as described in the definition of the
178 @GenClass@ constructor above.
179
180 \begin{code}
181 isSuperClassOf :: Class -> Class -> Maybe [Class]
182 clas `isSuperClassOf` (Class _ _ _ _ _ _ _ _ _ links) = assocMaybe links clas
183 \end{code}
184
185 %************************************************************************
186 %*                                                                      *
187 \subsection[Class-instances]{Instance declarations for @Class@}
188 %*                                                                      *
189 %************************************************************************
190
191 We compare @Classes@ by their keys (which include @Uniques@).
192
193 \begin{code}
194 instance Ord3 (GenClass tyvar uvar) where
195   cmp (Class k1 _ _ _ _ _ _ _ _ _) (Class k2 _ _ _ _ _ _ _ _ _)  = cmp k1 k2
196
197 instance Eq (GenClass tyvar uvar) where
198     (Class k1 _ _ _ _ _ _ _ _ _) == (Class k2 _ _ _ _ _ _ _ _ _) = k1 == k2
199     (Class k1 _ _ _ _ _ _ _ _ _) /= (Class k2 _ _ _ _ _ _ _ _ _) = k1 /= k2
200
201 instance Ord (GenClass tyvar uvar) where
202     (Class k1 _ _ _ _ _ _ _ _ _) <= (Class k2 _ _ _ _ _ _ _ _ _) = k1 <= k2
203     (Class k1 _ _ _ _ _ _ _ _ _) <  (Class k2 _ _ _ _ _ _ _ _ _) = k1 <  k2
204     (Class k1 _ _ _ _ _ _ _ _ _) >= (Class k2 _ _ _ _ _ _ _ _ _) = k1 >= k2
205     (Class k1 _ _ _ _ _ _ _ _ _) >  (Class k2 _ _ _ _ _ _ _ _ _) = k1 >  k2
206     _tagCmp a b = case cmp a b of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
207 \end{code}
208
209 \begin{code}
210 instance Uniquable (GenClass tyvar uvar) where
211     uniqueOf (Class u _ _ _ _ _ _ _ _ _) = u
212
213 instance NamedThing (GenClass tyvar uvar) where
214     getName (Class _ n _ _ _ _ _ _ _ _) = n
215
216 instance NamedThing (GenClassOp ty) where
217     getOccName (ClassOp occ _ _) = occ
218 \end{code}
219
220
221 %************************************************************************
222 %*                                                                      *
223 \subsection[ClassOp-basic]{@ClassOp@: type and basic functions}
224 %*                                                                      *
225 %************************************************************************
226
227 A @ClassOp@ represents a a class operation.  From it and its parent
228 class we can construct the dictionary-selector @Id@ for the
229 operation/superclass dictionary, and the @Id@ for its default method.
230 It appears in a list inside the @Class@ object.
231
232 The type of a method in a @ClassOp@ object is its local type; that is,
233 without the overloading of the class itself.  For example, in the
234 declaration
235 \begin{pseudocode}
236         class Foo a where
237                 op :: Ord b => a -> b -> a
238 \end{pseudocode}
239 the type recorded for @op@ in the @ClassOp@ list of the @Class@ object is
240 just
241         $\forall \beta.~
242                 @Ord@~\beta \Rightarrow
243                 \alpha \rightarrow \beta \rightarrow alpha$
244
245 (where $\alpha$ is the class type variable recorded in the @Class@
246 object).  Of course, the type of @op@ recorded in the GVE will be its
247 ``full'' type
248
249         $\forall \alpha \forall \beta.~
250                 @Foo@~\alpha \Rightarrow
251                 ~@Ord@~\beta \Rightarrow \alpha
252                 \rightarrow \beta \rightarrow alpha$
253
254 ******************************************************************
255 **** That is, the type variables of a class op selector
256 ***  are all at the outer level.
257 ******************************************************************
258
259 \begin{code}
260 mkClassOp :: OccName -> Int -> ty -> GenClassOp ty
261 mkClassOp name tag ty = ClassOp name tag ty
262
263 classOpTag :: GenClassOp ty -> Int
264 classOpTag    (ClassOp _ tag _) = tag
265
266 classOpString :: GenClassOp ty -> FAST_STRING
267 classOpString (ClassOp occ _ _) = occNameString occ
268
269 classOpLocalType :: GenClassOp ty -> ty {-SigmaType-}
270 classOpLocalType (ClassOp _ _ ty) = ty
271 \end{code}
272
273 Rather unsavoury ways of getting ClassOp tags:
274 \begin{code}
275 classOpTagByOccName_maybe :: Class -> OccName -> Maybe Int
276 classOpTagByOccName       :: Class -> OccName -> Int
277
278 classOpTagByOccName clas op
279   = case (classOpTagByOccName_maybe clas op) of
280       Just tag -> tag
281 #ifdef DEBUG
282       Nothing  -> pprPanic "classOpTagByOccName:" (hsep (ppr PprDebug op : map (ptext . classOpString) (classOps clas)))
283 #endif
284
285 classOpTagByOccName_maybe clas op
286   = go (classOps clas) 1
287   where
288     go []                     _   = Nothing
289     go (ClassOp occ _ _ : ns) tag = if occ == op
290                                     then Just tag
291                                     else go ns (tag+1)
292 \end{code}
293
294 %************************************************************************
295 %*                                                                      *
296 \subsection[ClassOp-instances]{Instance declarations for @ClassOp@}
297 %*                                                                      *
298 %************************************************************************
299
300 @ClassOps@ are compared by their tags.
301
302 \begin{code}
303 instance Eq (GenClassOp ty) where
304     (ClassOp _ i1 _) == (ClassOp _ i2 _) = i1 == i2
305     (ClassOp _ i1 _) /= (ClassOp _ i2 _) = i1 == i2
306
307 instance Ord (GenClassOp ty) where
308     (ClassOp _ i1 _) <= (ClassOp _ i2 _) = i1 <= i2
309     (ClassOp _ i1 _) <  (ClassOp _ i2 _) = i1 <  i2
310     (ClassOp _ i1 _) >= (ClassOp _ i2 _) = i1 >= i2
311     (ClassOp _ i1 _) >  (ClassOp _ i2 _) = i1 >  i2
312     -- ToDo: something for _tagCmp? (WDP 94/10)
313 \end{code}