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, classSelIds, classDictArgTys,
14 classSuperDictSelId, classDefaultMethodId,
15 classBigSig, classInstEnv,
21 CHK_Ubiq() -- debugging consistency check
23 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
24 IMPORT_DELOOPER(TyLoop)
25 IMPORT_DELOOPER(IdLoop)
27 import {-# SOURCE #-} Id ( Id, idType, idName )
28 import {-# SOURCE #-} Type
29 import {-# SOURCE #-} TysWiredIn
30 import {-# SOURCE #-} TysPrim
33 #if __GLASGOW_HASKELL__ >= 202
37 import TyCon ( TyCon )
38 import TyVar ( SYN_IE(TyVar), GenTyVar )
39 import Usage ( GenUsage, SYN_IE(Usage), SYN_IE(UVar) )
41 import MatchEnv ( MatchEnv )
42 import Maybes ( assocMaybe )
43 import Name ( changeUnique, Name, OccName, occNameString )
44 import Unique -- Keys for built-in classes
45 import Pretty ( Doc, hsep, ptext )
46 import SrcLoc ( SrcLoc )
51 %************************************************************************
53 \subsection[Class-basic]{@Class@: basic definition}
55 %************************************************************************
57 A @Class@ corresponds to a Greek kappa in the static semantics:
59 The parameterisation wrt tyvar and uvar is only necessary to
60 get appropriately general instances of Ord3 for GenType.
63 data GenClass tyvar uvar
65 Unique -- Key for fast comparison
68 tyvar -- The class type variable
70 [GenClass tyvar uvar] -- Immediate superclasses, and the
71 [Id] -- corresponding selector functions to
72 -- extract them from a dictionary of this
75 [Id] -- * selector functions
76 [Maybe Id] -- * default methods
77 -- They are all ordered by tag. The
78 -- selector ids are less innocent than they
79 -- look, because their IdInfos contains
80 -- suitable specialisation information. In
81 -- particular, constant methods are
82 -- instances of selectors at suitably simple
85 ClassInstEnv -- Gives details of all the instances of this class
87 [(GenClass tyvar uvar, [GenClass tyvar uvar])]
88 -- Indirect superclasses;
89 -- (k,[k1,...,kn]) means that
90 -- k is an immediate superclass of k1
91 -- k1 is an immediate superclass of k2
92 -- ... and kn is an immediate superclass
93 -- of this class. (This is all redundant
94 -- information, since it can be derived from
95 -- the superclass information above.)
97 type Class = GenClass TyVar UVar
99 type ClassInstEnv = MatchEnv Type Id -- The Ids are dfuns
102 The @mkClass@ function fills in the indirect superclasses.
105 mkClass :: Unique -> Name -> TyVar
107 -> [Id] -> [Maybe Id]
111 mkClass uniq full_name tyvar super_classes superdict_sels
112 dict_sels defms class_insts
113 = Class uniq (changeUnique full_name uniq) tyvar
114 super_classes superdict_sels
119 trans_clos :: [(Class,[Class])]
120 trans_clos = transitiveClosure succ (==) [ (clas, []) | clas <- super_classes ]
122 succ (clas@(Class _ _ _ super_classes _ _ _ _ _), links)
123 = [(super, (clas:links)) | super <- super_classes]
126 %************************************************************************
128 \subsection[Class-selectors]{@Class@: simple selectors}
130 %************************************************************************
132 The rest of these functions are just simple selectors.
135 classKey (Class key _ _ _ _ _ _ _ _) = key
136 classSelIds (Class _ _ _ _ _ sels _ _ _) = sels
138 classDefaultMethodId (Class _ _ _ _ _ _ defm_ids _ _) idx
141 classSuperDictSelId (Class _ _ _ scs scsel_ids _ _ _ _) super_clas
142 = assoc "classSuperDictSelId" (scs `zip` scsel_ids) super_clas
144 classBigSig (Class _ _ tyvar super_classes sdsels sels defms _ _)
145 = (tyvar, super_classes, sdsels, sels, defms)
147 classInstEnv (Class _ _ _ _ _ _ _ inst_env _) = inst_env
149 classDictArgTys :: Class -> Type -> [Type] -- Types of components of the dictionary (C ty)
150 classDictArgTys (Class _ _ _ _ sc_sel_ids meth_sel_ids _ _ _) ty
151 = map mk_arg_ty (sc_sel_ids ++ meth_sel_ids)
153 mk_arg_ty id = case splitRhoTy (applyTy (idType id) ty) of
154 (sel_theta, meth_ty) -> ASSERT( length sel_theta == 1 )
158 @a `isSuperClassOf` b@ returns @Nothing@ if @a@ is not a superclass of
159 @b@, but if it is, it returns $@Just@~[k_1,\ldots,k_n]$, where the
160 $k_1,\ldots,k_n$ are exactly as described in the definition of the
161 @GenClass@ constructor above.
164 isSuperClassOf :: Class -> Class -> Maybe [Class]
165 clas `isSuperClassOf` (Class _ _ _ _ _ _ _ _ links) = assocMaybe links clas
168 %************************************************************************
170 \subsection[Class-instances]{Instance declarations for @Class@}
172 %************************************************************************
174 We compare @Classes@ by their keys (which include @Uniques@).
177 instance Ord3 (GenClass tyvar uvar) where
178 cmp (Class k1 _ _ _ _ _ _ _ _) (Class k2 _ _ _ _ _ _ _ _) = cmp k1 k2
180 instance Eq (GenClass tyvar uvar) where
181 (Class k1 _ _ _ _ _ _ _ _) == (Class k2 _ _ _ _ _ _ _ _) = k1 == k2
182 (Class k1 _ _ _ _ _ _ _ _) /= (Class k2 _ _ _ _ _ _ _ _) = k1 /= k2
184 instance Ord (GenClass tyvar uvar) where
185 (Class k1 _ _ _ _ _ _ _ _) <= (Class k2 _ _ _ _ _ _ _ _) = k1 <= k2
186 (Class k1 _ _ _ _ _ _ _ _) < (Class k2 _ _ _ _ _ _ _ _) = k1 < k2
187 (Class k1 _ _ _ _ _ _ _ _) >= (Class k2 _ _ _ _ _ _ _ _) = k1 >= k2
188 (Class k1 _ _ _ _ _ _ _ _) > (Class k2 _ _ _ _ _ _ _ _) = k1 > k2
189 _tagCmp a b = case cmp a b of { LT_ -> _LT; EQ_ -> _EQ; GT__ -> _GT }
193 instance Uniquable (GenClass tyvar uvar) where
194 uniqueOf (Class u _ _ _ _ _ _ _ _) = u
196 instance NamedThing (GenClass tyvar uvar) where
197 getName (Class _ n _ _ _ _ _ _ _) = n