2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[Class]{The @Class@ datatype}
7 #include "HsVersions.h"
10 GenClass(..), SYN_IE(Class),
13 classKey, classOps, classGlobalIds,
14 classSuperDictSelId, classOpId, classDefaultMethodId,
15 classSig, classBigSig, classInstEnv,
17 classOpTagByOccName, classOpTagByOccName_maybe,
19 GenClassOp(..), SYN_IE(ClassOp),
21 classOpTag, classOpString,
27 CHK_Ubiq() -- debugging consistency check
29 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
30 IMPORT_DELOOPER(TyLoop)
32 import {-# SOURCE #-} Id
33 import {-# SOURCE #-} Type
34 import {-# SOURCE #-} TysWiredIn
35 import {-# SOURCE #-} TysPrim
38 #if __GLASGOW_HASKELL__ >= 202
42 import TyCon ( TyCon )
43 import TyVar ( SYN_IE(TyVar), GenTyVar )
44 import Usage ( GenUsage, SYN_IE(Usage), SYN_IE(UVar) )
46 import MatchEnv ( MatchEnv )
47 import Maybes ( assocMaybe )
48 import Name ( changeUnique, Name, OccName, occNameString )
50 import Unique -- Keys for built-in classes
51 import UniqFM ( SYN_IE(Uniquable))
52 import Pretty ( Doc, hsep, ptext )
53 import SrcLoc ( SrcLoc )
57 %************************************************************************
59 \subsection[Class-basic]{@Class@: basic definition}
61 %************************************************************************
63 A @Class@ corresponds to a Greek kappa in the static semantics:
65 The parameterisation wrt tyvar and uvar is only necessary to
66 get appropriately general instances of Ord3 for GenType.
70 = ClassOp OccName -- The operation name
72 Int -- Unique within a class; starts at 1
74 ty -- Type; the class tyvar is free (you can find
75 -- it from the class). This means that a
76 -- ClassOp doesn't make much sense outside the
77 -- context of its parent class.
79 data GenClass tyvar uvar
81 Unique -- Key for fast comparison
84 tyvar -- The class type variable
86 [GenClass tyvar uvar] -- Immediate superclasses, and the
87 [Id] -- corresponding selector functions to
88 -- extract them from a dictionary of this
91 [GenClassOp (GenType tyvar uvar)] -- The * class operations
92 [Id] -- * selector functions
93 [Id] -- * default methods
94 -- They are all ordered by tag. The
95 -- selector ids are less innocent than they
96 -- look, because their IdInfos contains
97 -- suitable specialisation information. In
98 -- particular, constant methods are
99 -- instances of selectors at suitably simple
102 ClassInstEnv -- Gives details of all the instances of this class
104 [(GenClass tyvar uvar, [GenClass tyvar uvar])]
105 -- Indirect superclasses;
106 -- (k,[k1,...,kn]) means that
107 -- k is an immediate superclass of k1
108 -- k1 is an immediate superclass of k2
109 -- ... and kn is an immediate superclass
110 -- of this class. (This is all redundant
111 -- information, since it can be derived from
112 -- the superclass information above.)
114 type Class = GenClass TyVar UVar
115 type ClassOp = GenClassOp Type
117 type ClassInstEnv = MatchEnv Type Id -- The Ids are dfuns
120 The @mkClass@ function fills in the indirect superclasses.
123 mkClass :: Unique -> Name -> TyVar
125 -> [ClassOp] -> [Id] -> [Id]
129 mkClass uniq full_name tyvar super_classes superdict_sels
130 class_ops dict_sels defms class_insts
131 = Class uniq (changeUnique full_name uniq) tyvar
132 super_classes superdict_sels
133 class_ops dict_sels defms
137 trans_clos :: [(Class,[Class])]
138 trans_clos = transitiveClosure succ (==) [ (clas, []) | clas <- super_classes ]
140 succ (clas@(Class _ _ _ super_classes _ _ _ _ _ _), links)
141 = [(super, (clas:links)) | super <- super_classes]
144 %************************************************************************
146 \subsection[Class-selectors]{@Class@: simple selectors}
148 %************************************************************************
150 The rest of these functions are just simple selectors.
153 classKey (Class key _ _ _ _ _ _ _ _ _) = key
154 classOps (Class _ _ _ _ _ ops _ _ _ _) = ops
155 classGlobalIds (Class _ _ _ _ _ _ sels defm_ids _ _) = sels ++ defm_ids
157 classOpId (Class _ _ _ _ _ ops op_ids _ _ _) op
158 = op_ids !! (classOpTag op - 1)
160 classDefaultMethodId (Class _ _ _ _ _ ops _ defm_ids _ _) idx
163 classSuperDictSelId (Class _ _ _ scs scsel_ids _ _ _ _ _) super_clas
164 = assoc "classSuperDictSelId" (scs `zip` scsel_ids) super_clas
166 classSig :: GenClass t u -> (t, [GenClass t u], [GenClassOp (GenType t u)])
167 classSig (Class _ _ tyvar super_classes _ ops _ _ _ _)
168 = (tyvar, super_classes, ops)
170 classBigSig (Class _ _ tyvar super_classes sdsels ops sels defms _ _)
171 = (tyvar, super_classes, sdsels, ops, sels, defms)
173 classInstEnv (Class _ _ _ _ _ _ _ _ inst_env _) = inst_env
176 @a `isSuperClassOf` b@ returns @Nothing@ if @a@ is not a superclass of
177 @b@, but if it is, it returns $@Just@~[k_1,\ldots,k_n]$, where the
178 $k_1,\ldots,k_n$ are exactly as described in the definition of the
179 @GenClass@ constructor above.
182 isSuperClassOf :: Class -> Class -> Maybe [Class]
183 clas `isSuperClassOf` (Class _ _ _ _ _ _ _ _ _ links) = assocMaybe links clas
186 %************************************************************************
188 \subsection[Class-instances]{Instance declarations for @Class@}
190 %************************************************************************
192 We compare @Classes@ by their keys (which include @Uniques@).
195 instance Ord3 (GenClass tyvar uvar) where
196 cmp (Class k1 _ _ _ _ _ _ _ _ _) (Class k2 _ _ _ _ _ _ _ _ _) = cmp k1 k2
198 instance Eq (GenClass tyvar uvar) where
199 (Class k1 _ _ _ _ _ _ _ _ _) == (Class k2 _ _ _ _ _ _ _ _ _) = k1 == k2
200 (Class k1 _ _ _ _ _ _ _ _ _) /= (Class k2 _ _ _ _ _ _ _ _ _) = k1 /= k2
202 instance Ord (GenClass tyvar uvar) where
203 (Class k1 _ _ _ _ _ _ _ _ _) <= (Class k2 _ _ _ _ _ _ _ _ _) = k1 <= k2
204 (Class k1 _ _ _ _ _ _ _ _ _) < (Class k2 _ _ _ _ _ _ _ _ _) = k1 < k2
205 (Class k1 _ _ _ _ _ _ _ _ _) >= (Class k2 _ _ _ _ _ _ _ _ _) = k1 >= k2
206 (Class k1 _ _ _ _ _ _ _ _ _) > (Class k2 _ _ _ _ _ _ _ _ _) = k1 > k2
207 _tagCmp a b = case cmp a b of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
211 instance Uniquable (GenClass tyvar uvar) where
212 uniqueOf (Class u _ _ _ _ _ _ _ _ _) = u
214 instance NamedThing (GenClass tyvar uvar) where
215 getName (Class _ n _ _ _ _ _ _ _ _) = n
217 instance NamedThing (GenClassOp ty) where
218 getOccName (ClassOp occ _ _) = occ
222 %************************************************************************
224 \subsection[ClassOp-basic]{@ClassOp@: type and basic functions}
226 %************************************************************************
228 A @ClassOp@ represents a a class operation. From it and its parent
229 class we can construct the dictionary-selector @Id@ for the
230 operation/superclass dictionary, and the @Id@ for its default method.
231 It appears in a list inside the @Class@ object.
233 The type of a method in a @ClassOp@ object is its local type; that is,
234 without the overloading of the class itself. For example, in the
238 op :: Ord b => a -> b -> a
240 the type recorded for @op@ in the @ClassOp@ list of the @Class@ object is
243 @Ord@~\beta \Rightarrow
244 \alpha \rightarrow \beta \rightarrow alpha$
246 (where $\alpha$ is the class type variable recorded in the @Class@
247 object). Of course, the type of @op@ recorded in the GVE will be its
250 $\forall \alpha \forall \beta.~
251 @Foo@~\alpha \Rightarrow
252 ~@Ord@~\beta \Rightarrow \alpha
253 \rightarrow \beta \rightarrow alpha$
255 ******************************************************************
256 **** That is, the type variables of a class op selector
257 *** are all at the outer level.
258 ******************************************************************
261 mkClassOp :: OccName -> Int -> ty -> GenClassOp ty
262 mkClassOp name tag ty = ClassOp name tag ty
264 classOpTag :: GenClassOp ty -> Int
265 classOpTag (ClassOp _ tag _) = tag
267 classOpString :: GenClassOp ty -> FAST_STRING
268 classOpString (ClassOp occ _ _) = occNameString occ
270 classOpLocalType :: GenClassOp ty -> ty {-SigmaType-}
271 classOpLocalType (ClassOp _ _ ty) = ty
274 Rather unsavoury ways of getting ClassOp tags:
276 classOpTagByOccName_maybe :: Class -> OccName -> Maybe Int
277 classOpTagByOccName :: Class -> OccName -> Int
279 classOpTagByOccName clas op
280 = case (classOpTagByOccName_maybe clas op) of
283 Nothing -> pprPanic "classOpTagByOccName:" (hsep (ppr PprDebug op : map (ptext . classOpString) (classOps clas)))
286 classOpTagByOccName_maybe clas op
287 = go (classOps clas) 1
290 go (ClassOp occ _ _ : ns) tag = if occ == op
295 %************************************************************************
297 \subsection[ClassOp-instances]{Instance declarations for @ClassOp@}
299 %************************************************************************
301 @ClassOps@ are compared by their tags.
304 instance Eq (GenClassOp ty) where
305 (ClassOp _ i1 _) == (ClassOp _ i2 _) = i1 == i2
306 (ClassOp _ i1 _) /= (ClassOp _ i2 _) = i1 == i2
308 instance Ord (GenClassOp ty) where
309 (ClassOp _ i1 _) <= (ClassOp _ i2 _) = i1 <= i2
310 (ClassOp _ i1 _) < (ClassOp _ i2 _) = i1 < i2
311 (ClassOp _ i1 _) >= (ClassOp _ i2 _) = i1 >= i2
312 (ClassOp _ i1 _) > (ClassOp _ i2 _) = i1 > i2
313 -- ToDo: something for _tagCmp? (WDP 94/10)