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 IMPORT_DELOOPER(TyLoop)
30 --IMPORT_DELOOPER(IdLoop)
36 #if __GLASGOW_HASKELL__ >= 202
40 import TyCon --( TyCon )
41 import TyVar --( SYN_IE(TyVar), GenTyVar )
42 import Usage --( GenUsage, SYN_IE(Usage), SYN_IE(UVar) )
44 import MatchEnv ( MatchEnv )
45 import Maybes ( assocMaybe )
46 import Name ( changeUnique, Name, OccName, occNameString )
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 )
56 %************************************************************************
58 \subsection[Class-basic]{@Class@: basic definition}
60 %************************************************************************
62 A @Class@ corresponds to a Greek kappa in the static semantics:
64 The parameterisation wrt tyvar and uvar is only necessary to
65 get appropriately general instances of Ord3 for GenType.
69 = ClassOp OccName -- The operation name
71 Int -- Unique within a class; starts at 1
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.
78 data GenClass tyvar uvar
80 Unique -- Key for fast comparison
83 tyvar -- The class type variable
85 [GenClass tyvar uvar] -- Immediate superclasses, and the
86 [Id] -- corresponding selector functions to
87 -- extract them from a dictionary of this
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
101 ClassInstEnv -- Gives details of all the instances of this class
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.)
113 type Class = GenClass TyVar UVar
114 type ClassOp = GenClassOp Type
116 type ClassInstEnv = MatchEnv Type Id -- The Ids are dfuns
119 The @mkClass@ function fills in the indirect superclasses.
122 mkClass :: Unique -> Name -> TyVar
124 -> [ClassOp] -> [Id] -> [Id]
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
136 trans_clos :: [(Class,[Class])]
137 trans_clos = transitiveClosure succ (==) [ (clas, []) | clas <- super_classes ]
139 succ (clas@(Class _ _ _ super_classes _ _ _ _ _ _), links)
140 = [(super, (clas:links)) | super <- super_classes]
143 %************************************************************************
145 \subsection[Class-selectors]{@Class@: simple selectors}
147 %************************************************************************
149 The rest of these functions are just simple selectors.
152 classKey (Class key _ _ _ _ _ _ _ _ _) = key
153 classOps (Class _ _ _ _ _ ops _ _ _ _) = ops
154 classGlobalIds (Class _ _ _ _ _ _ sels defm_ids _ _) = sels ++ defm_ids
156 classOpId (Class _ _ _ _ _ ops op_ids _ _ _) op
157 = op_ids !! (classOpTag op - 1)
159 classDefaultMethodId (Class _ _ _ _ _ ops _ defm_ids _ _) idx
162 classSuperDictSelId (Class _ _ _ scs scsel_ids _ _ _ _ _) super_clas
163 = assoc "classSuperDictSelId" (scs `zip` scsel_ids) super_clas
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)
169 classBigSig (Class _ _ tyvar super_classes sdsels ops sels defms _ _)
170 = (tyvar, super_classes, sdsels, ops, sels, defms)
172 classInstEnv (Class _ _ _ _ _ _ _ _ inst_env _) = inst_env
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.
181 isSuperClassOf :: Class -> Class -> Maybe [Class]
182 clas `isSuperClassOf` (Class _ _ _ _ _ _ _ _ _ links) = assocMaybe links clas
185 %************************************************************************
187 \subsection[Class-instances]{Instance declarations for @Class@}
189 %************************************************************************
191 We compare @Classes@ by their keys (which include @Uniques@).
194 instance Ord3 (GenClass tyvar uvar) where
195 cmp (Class k1 _ _ _ _ _ _ _ _ _) (Class k2 _ _ _ _ _ _ _ _ _) = cmp k1 k2
197 instance Eq (GenClass tyvar uvar) where
198 (Class k1 _ _ _ _ _ _ _ _ _) == (Class k2 _ _ _ _ _ _ _ _ _) = k1 == k2
199 (Class k1 _ _ _ _ _ _ _ _ _) /= (Class k2 _ _ _ _ _ _ _ _ _) = k1 /= k2
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 }
210 instance Uniquable (GenClass tyvar uvar) where
211 uniqueOf (Class u _ _ _ _ _ _ _ _ _) = u
213 instance NamedThing (GenClass tyvar uvar) where
214 getName (Class _ n _ _ _ _ _ _ _ _) = n
216 instance NamedThing (GenClassOp ty) where
217 getOccName (ClassOp occ _ _) = occ
221 %************************************************************************
223 \subsection[ClassOp-basic]{@ClassOp@: type and basic functions}
225 %************************************************************************
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.
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
237 op :: Ord b => a -> b -> a
239 the type recorded for @op@ in the @ClassOp@ list of the @Class@ object is
242 @Ord@~\beta \Rightarrow
243 \alpha \rightarrow \beta \rightarrow alpha$
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
249 $\forall \alpha \forall \beta.~
250 @Foo@~\alpha \Rightarrow
251 ~@Ord@~\beta \Rightarrow \alpha
252 \rightarrow \beta \rightarrow alpha$
254 ******************************************************************
255 **** That is, the type variables of a class op selector
256 *** are all at the outer level.
257 ******************************************************************
260 mkClassOp :: OccName -> Int -> ty -> GenClassOp ty
261 mkClassOp name tag ty = ClassOp name tag ty
263 classOpTag :: GenClassOp ty -> Int
264 classOpTag (ClassOp _ tag _) = tag
266 classOpString :: GenClassOp ty -> FAST_STRING
267 classOpString (ClassOp occ _ _) = occNameString occ
269 classOpLocalType :: GenClassOp ty -> ty {-SigmaType-}
270 classOpLocalType (ClassOp _ _ ty) = ty
273 Rather unsavoury ways of getting ClassOp tags:
275 classOpTagByOccName_maybe :: Class -> OccName -> Maybe Int
276 classOpTagByOccName :: Class -> OccName -> Int
278 classOpTagByOccName clas op
279 = case (classOpTagByOccName_maybe clas op) of
282 Nothing -> pprPanic "classOpTagByOccName:" (hsep (ppr PprDebug op : map (ptext . classOpString) (classOps clas)))
285 classOpTagByOccName_maybe clas op
286 = go (classOps clas) 1
289 go (ClassOp occ _ _ : ns) tag = if occ == op
294 %************************************************************************
296 \subsection[ClassOp-instances]{Instance declarations for @ClassOp@}
298 %************************************************************************
300 @ClassOps@ are compared by their tags.
303 instance Eq (GenClassOp ty) where
304 (ClassOp _ i1 _) == (ClassOp _ i2 _) = i1 == i2
305 (ClassOp _ i1 _) /= (ClassOp _ i2 _) = i1 == i2
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)