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, classSelIds,
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)
31 import TyCon ( TyCon )
32 import TyVar ( SYN_IE(TyVar), GenTyVar )
33 import Usage ( GenUsage, SYN_IE(Usage), SYN_IE(UVar) )
35 import MatchEnv ( MatchEnv )
36 import Maybes ( assocMaybe )
37 import Name ( changeUnique, Name, OccName, occNameString )
38 import Unique -- Keys for built-in classes
39 import Pretty ( SYN_IE(Pretty), ppCat, ppPStr )
40 import PprStyle ( PprStyle(..) )
41 import SrcLoc ( SrcLoc )
45 %************************************************************************
47 \subsection[Class-basic]{@Class@: basic definition}
49 %************************************************************************
51 A @Class@ corresponds to a Greek kappa in the static semantics:
53 The parameterisation wrt tyvar and uvar is only necessary to
54 get appropriately general instances of Ord3 for GenType.
58 = ClassOp OccName -- The operation name
60 Int -- Unique within a class; starts at 1
62 ty -- Type; the class tyvar is free (you can find
63 -- it from the class). This means that a
64 -- ClassOp doesn't make much sense outside the
65 -- context of its parent class.
67 data GenClass tyvar uvar
69 Unique -- Key for fast comparison
72 tyvar -- The class type variable
74 [GenClass tyvar uvar] -- Immediate superclasses, and the
75 [Id] -- corresponding selector functions to
76 -- extract them from a dictionary of this
79 [GenClassOp (GenType tyvar uvar)] -- The * class operations
80 [Id] -- * selector functions
81 [Id] -- * default methods
82 -- They are all ordered by tag. The
83 -- selector ids are less innocent than they
84 -- look, because their IdInfos contains
85 -- suitable specialisation information. In
86 -- particular, constant methods are
87 -- instances of selectors at suitably simple
90 ClassInstEnv -- Gives details of all the instances of this class
92 [(GenClass tyvar uvar, [GenClass tyvar uvar])]
93 -- Indirect superclasses;
94 -- (k,[k1,...,kn]) means that
95 -- k is an immediate superclass of k1
96 -- k1 is an immediate superclass of k2
97 -- ... and kn is an immediate superclass
98 -- of this class. (This is all redundant
99 -- information, since it can be derived from
100 -- the superclass information above.)
102 type Class = GenClass TyVar UVar
103 type ClassOp = GenClassOp Type
105 type ClassInstEnv = MatchEnv Type Id -- The Ids are dfuns
108 The @mkClass@ function fills in the indirect superclasses.
111 mkClass :: Unique -> Name -> TyVar
113 -> [ClassOp] -> [Id] -> [Id]
117 mkClass uniq full_name tyvar super_classes superdict_sels
118 class_ops dict_sels defms class_insts
119 = Class uniq (changeUnique full_name uniq) tyvar
120 super_classes superdict_sels
121 class_ops dict_sels defms
125 trans_clos :: [(Class,[Class])]
126 trans_clos = transitiveClosure succ (==) [ (clas, []) | clas <- super_classes ]
128 succ (clas@(Class _ _ _ super_classes _ _ _ _ _ _), links)
129 = [(super, (clas:links)) | super <- super_classes]
132 %************************************************************************
134 \subsection[Class-selectors]{@Class@: simple selectors}
136 %************************************************************************
138 The rest of these functions are just simple selectors.
141 classKey (Class key _ _ _ _ _ _ _ _ _) = key
142 classOps (Class _ _ _ _ _ ops _ _ _ _) = ops
143 classSelIds (Class _ _ _ _ _ _ sels _ _ _) = sels
145 classOpId (Class _ _ _ _ _ ops op_ids _ _ _) op
146 = op_ids !! (classOpTag op - 1)
147 classDefaultMethodId (Class _ _ _ _ _ ops _ defm_ids _ _) op
148 = defm_ids !! (classOpTag op - 1)
149 classSuperDictSelId (Class _ _ _ scs scsel_ids _ _ _ _ _) super_clas
150 = assoc "classSuperDictSelId" (scs `zip` scsel_ids) super_clas
152 classSig :: GenClass t u -> (t, [GenClass t u], [GenClassOp (GenType t u)])
153 classSig (Class _ _ tyvar super_classes _ ops _ _ _ _)
154 = (tyvar, super_classes, ops)
156 classBigSig (Class _ _ tyvar super_classes sdsels ops sels defms _ _)
157 = (tyvar, super_classes, sdsels, ops, sels, defms)
159 classInstEnv (Class _ _ _ _ _ _ _ _ inst_env _) = inst_env
162 @a `isSuperClassOf` b@ returns @Nothing@ if @a@ is not a superclass of
163 @b@, but if it is, it returns $@Just@~[k_1,\ldots,k_n]$, where the
164 $k_1,\ldots,k_n$ are exactly as described in the definition of the
165 @GenClass@ constructor above.
168 isSuperClassOf :: Class -> Class -> Maybe [Class]
169 clas `isSuperClassOf` (Class _ _ _ _ _ _ _ _ _ links) = assocMaybe links clas
172 %************************************************************************
174 \subsection[Class-instances]{Instance declarations for @Class@}
176 %************************************************************************
178 We compare @Classes@ by their keys (which include @Uniques@).
181 instance Ord3 (GenClass tyvar uvar) where
182 cmp (Class k1 _ _ _ _ _ _ _ _ _) (Class k2 _ _ _ _ _ _ _ _ _) = cmp k1 k2
184 instance Eq (GenClass tyvar uvar) where
185 (Class k1 _ _ _ _ _ _ _ _ _) == (Class k2 _ _ _ _ _ _ _ _ _) = k1 == k2
186 (Class k1 _ _ _ _ _ _ _ _ _) /= (Class k2 _ _ _ _ _ _ _ _ _) = k1 /= k2
188 instance Ord (GenClass tyvar uvar) where
189 (Class k1 _ _ _ _ _ _ _ _ _) <= (Class k2 _ _ _ _ _ _ _ _ _) = k1 <= k2
190 (Class k1 _ _ _ _ _ _ _ _ _) < (Class k2 _ _ _ _ _ _ _ _ _) = k1 < k2
191 (Class k1 _ _ _ _ _ _ _ _ _) >= (Class k2 _ _ _ _ _ _ _ _ _) = k1 >= k2
192 (Class k1 _ _ _ _ _ _ _ _ _) > (Class k2 _ _ _ _ _ _ _ _ _) = k1 > k2
193 _tagCmp a b = case cmp a b of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
197 instance Uniquable (GenClass tyvar uvar) where
198 uniqueOf (Class u _ _ _ _ _ _ _ _ _) = u
200 instance NamedThing (GenClass tyvar uvar) where
201 getName (Class _ n _ _ _ _ _ _ _ _) = n
203 instance NamedThing (GenClassOp ty) where
204 getOccName (ClassOp occ _ _) = occ
208 %************************************************************************
210 \subsection[ClassOp-basic]{@ClassOp@: type and basic functions}
212 %************************************************************************
214 A @ClassOp@ represents a a class operation. From it and its parent
215 class we can construct the dictionary-selector @Id@ for the
216 operation/superclass dictionary, and the @Id@ for its default method.
217 It appears in a list inside the @Class@ object.
219 The type of a method in a @ClassOp@ object is its local type; that is,
220 without the overloading of the class itself. For example, in the
224 op :: Ord b => a -> b -> a
226 the type recorded for @op@ in the @ClassOp@ list of the @Class@ object is
229 @Ord@~\beta \Rightarrow
230 \alpha \rightarrow \beta \rightarrow alpha$
232 (where $\alpha$ is the class type variable recorded in the @Class@
233 object). Of course, the type of @op@ recorded in the GVE will be its
236 $\forall \alpha \forall \beta.~
237 @Foo@~\alpha \Rightarrow
238 ~@Ord@~\beta \Rightarrow \alpha
239 \rightarrow \beta \rightarrow alpha$
241 ******************************************************************
242 **** That is, the type variables of a class op selector
243 *** are all at the outer level.
244 ******************************************************************
247 mkClassOp :: OccName -> Int -> ty -> GenClassOp ty
248 mkClassOp name tag ty = ClassOp name tag ty
250 classOpTag :: GenClassOp ty -> Int
251 classOpTag (ClassOp _ tag _) = tag
253 classOpString :: GenClassOp ty -> FAST_STRING
254 classOpString (ClassOp occ _ _) = occNameString occ
256 classOpLocalType :: GenClassOp ty -> ty {-SigmaType-}
257 classOpLocalType (ClassOp _ _ ty) = ty
260 Rather unsavoury ways of getting ClassOp tags:
262 classOpTagByOccName_maybe :: Class -> OccName -> Maybe Int
263 classOpTagByOccName :: Class -> OccName -> Int
265 classOpTagByOccName clas op
266 = case (classOpTagByOccName_maybe clas op) of
269 Nothing -> pprPanic "classOpTagByOccName:" (ppCat (ppr PprDebug op : map (ppPStr . classOpString) (classOps clas)))
272 classOpTagByOccName_maybe clas op
273 = go (classOps clas) 1
276 go (ClassOp occ _ _ : ns) tag = if occ == op
281 %************************************************************************
283 \subsection[ClassOp-instances]{Instance declarations for @ClassOp@}
285 %************************************************************************
287 @ClassOps@ are compared by their tags.
290 instance Eq (GenClassOp ty) where
291 (ClassOp _ i1 _) == (ClassOp _ i2 _) = i1 == i2
292 (ClassOp _ i1 _) /= (ClassOp _ i2 _) = i1 == i2
294 instance Ord (GenClassOp ty) where
295 (ClassOp _ i1 _) <= (ClassOp _ i2 _) = i1 <= i2
296 (ClassOp _ i1 _) < (ClassOp _ i2 _) = i1 < i2
297 (ClassOp _ i1 _) >= (ClassOp _ i2 _) = i1 >= i2
298 (ClassOp _ i1 _) > (ClassOp _ i2 _) = i1 > i2
299 -- ToDo: something for _tagCmp? (WDP 94/10)