2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[InstEnv]{Utilities for typechecking instance declarations}
6 The bits common to TcInstDcls and TcDeriv.
10 DFunId, ClsInstEnv, InstEnv,
12 emptyInstEnv, extendInstEnv, pprInstEnv,
13 lookupInstEnv, InstLookupResult(..),
14 classInstEnv, simpleDFunClassTyCon
17 #include "HsVersions.h"
19 import Class ( Class, classTvsFds )
20 import Var ( TyVar, Id )
23 import Maybes ( MaybeErr(..), returnMaB, failMaB, thenMaB, maybeToBool )
24 import Name ( getSrcLoc )
25 import Type ( Type, tyConAppTyCon, mkTyVarTy,
26 splitDFunTy, tyVarsOfTypes
28 import PprType ( pprClassPred )
29 import FunDeps ( checkClsFD )
30 import TyCon ( TyCon )
32 import Unify ( matchTys, unifyTyListsX, allDistinctTyVars )
33 import UniqFM ( UniqFM, lookupWithDefaultUFM, addToUFM, emptyUFM, eltsUFM )
35 import ErrUtils ( Message )
40 %************************************************************************
42 \subsection{The key types}
44 %************************************************************************
49 type InstEnv = UniqFM ClsInstEnv -- Maps Class to instances for that class
51 simpleDFunClassTyCon :: DFunId -> (Class, TyCon)
52 simpleDFunClassTyCon dfun
55 (_,_,clas,[ty]) = splitDFunTy (idType dfun)
56 tycon = tyConAppTyCon ty
58 pprInstEnv :: InstEnv -> SDoc
60 = vcat [ brackets (pprWithCommas ppr (varSetElems tyvars)) <+>
61 brackets (pprWithCommas ppr tys) <+> ppr dfun
62 | cls_inst_env <- eltsUFM env
63 , (tyvars, tys, dfun) <- cls_inst_env
67 %************************************************************************
69 \subsection{Instance environments: InstEnv and ClsInstEnv}
71 %************************************************************************
74 type ClsInstEnv = [(TyVarSet, [Type], DFunId)] -- The instances for a particular class
75 -- INVARIANTs: see notes below
77 emptyInstEnv :: InstEnv
78 emptyInstEnv = emptyUFM
80 classInstEnv :: InstEnv -> Class -> ClsInstEnv
81 classInstEnv env cls = lookupWithDefaultUFM env [] cls
84 A @ClsInstEnv@ all the instances of that class. The @Id@ inside a
85 ClsInstEnv mapping is the dfun for that instance.
87 If class C maps to a list containing the item ([a,b], [t1,t2,t3], dfun), then
89 forall a b, C t1 t2 t3 can be constructed by dfun
91 or, to put it another way, we have
93 instance (...) => C t1 t2 t3, witnessed by dfun
95 There is an important consistency constraint in the elements of a ClsInstEnv:
97 * [a,b] must be a superset of the free vars of [t1,t2,t3]
99 * The dfun must itself be quantified over [a,b]
101 * More specific instances come before less specific ones,
104 Thus, the @ClassInstEnv@ for @Eq@ might contain the following entry:
105 [a] ===> dfun_Eq_List :: forall a. Eq a => Eq [a]
106 The "a" in the pattern must be one of the forall'd variables in
111 Notes on overlapping instances
112 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113 In some ClsInstEnvs, overlap is prohibited; that is, no pair of templates unify.
115 In others, overlap is permitted, but only in such a way that one can make
116 a unique choice when looking up. That is, overlap is only permitted if
117 one template matches the other, or vice versa. So this is ok:
125 If overlap is permitted, the list is kept most specific first, so that
126 the first lookup is the right choice.
129 For now we just use association lists.
131 \subsection{Avoiding a problem with overlapping}
133 Consider this little program:
136 class C a where c :: a
137 class C a => D a where d :: a
139 instance C Int where c = 17
140 instance D Int where d = 13
142 instance C a => C [a] where c = [c]
143 instance ({- C [a], -} D a) => D [a] where d = c
145 instance C [Int] where c = [37]
147 main = print (d :: [Int])
150 What do you think `main' prints (assuming we have overlapping instances, and
151 all that turned on)? Well, the instance for `D' at type `[a]' is defined to
152 be `c' at the same type, and we've got an instance of `C' at `[Int]', so the
153 answer is `[37]', right? (the generic `C [a]' instance shouldn't apply because
154 the `C [Int]' instance is more specific).
156 Ghc-4.04 gives `[37]', while ghc-4.06 gives `[17]', so 4.06 is wrong. That
157 was easy ;-) Let's just consult hugs for good measure. Wait - if I use old
158 hugs (pre-September99), I get `[17]', and stranger yet, if I use hugs98, it
159 doesn't even compile! What's going on!?
161 What hugs complains about is the `D [a]' instance decl.
164 ERROR "mj.hs" (line 10): Cannot build superclass instance
166 *** Context supplied : D a
167 *** Required superclass : C [a]
170 You might wonder what hugs is complaining about. It's saying that you
171 need to add `C [a]' to the context of the `D [a]' instance (as appears
172 in comments). But there's that `C [a]' instance decl one line above
173 that says that I can reduce the need for a `C [a]' instance to the
174 need for a `C a' instance, and in this case, I already have the
175 necessary `C a' instance (since we have `D a' explicitly in the
176 context, and `C' is a superclass of `D').
178 Unfortunately, the above reasoning indicates a premature commitment to the
179 generic `C [a]' instance. I.e., it prematurely rules out the more specific
180 instance `C [Int]'. This is the mistake that ghc-4.06 makes. The fix is to
181 add the context that hugs suggests (uncomment the `C [a]'), effectively
182 deferring the decision about which instance to use.
184 Now, interestingly enough, 4.04 has this same bug, but it's covered up
185 in this case by a little known `optimization' that was disabled in
186 4.06. Ghc-4.04 silently inserts any missing superclass context into
187 an instance declaration. In this case, it silently inserts the `C
188 [a]', and everything happens to work out.
190 (See `basicTypes/MkId:mkDictFunId' for the code in question. Search for
191 `Mark Jones', although Mark claims no credit for the `optimization' in
192 question, and would rather it stopped being called the `Mark Jones
195 So, what's the fix? I think hugs has it right. Here's why. Let's try
196 something else out with ghc-4.04. Let's add the following line:
201 Everyone raise their hand who thinks that `d :: [Int]' should give a
202 different answer from `d' :: [Int]'. Well, in ghc-4.04, it does. The
203 `optimization' only applies to instance decls, not to regular
204 bindings, giving inconsistent behavior.
206 Old hugs had this same bug. Here's how we fixed it: like GHC, the
207 list of instances for a given class is ordered, so that more specific
208 instances come before more generic ones. For example, the instance
209 list for C might contain:
210 ..., C Int, ..., C a, ...
211 When we go to look for a `C Int' instance we'll get that one first.
212 But what if we go looking for a `C b' (`b' is unconstrained)? We'll
213 pass the `C Int' instance, and keep going. But if `b' is
214 unconstrained, then we don't know yet if the more specific instance
215 will eventually apply. GHC keeps going, and matches on the generic `C
216 a'. The fix is to, at each step, check to see if there's a reverse
217 match, and if so, abort the search. This prevents hugs from
218 prematurely chosing a generic instance when a more specific one
224 %************************************************************************
226 \subsection{Looking up an instance}
228 %************************************************************************
230 @lookupInstEnv@ looks up in a @InstEnv@, using a one-way match. Since
231 the env is kept ordered, the first match must be the only one. The
232 thing we are looking up can have an arbitrary "flexi" part.
235 lookupInstEnv :: InstEnv -- The envt
236 -> Class -> [Type] -- Key
239 data InstLookupResult
240 = FoundInst -- There is a (template,substitution) pair
241 -- that makes the template match the key,
242 -- and no template is an instance of the key
245 | NoMatch Bool -- Boolean is true iff there is at least one
246 -- template that matches the key.
247 -- (but there are other template(s) that are
248 -- instances of the key, so we don't report
250 -- The NoMatch True case happens when we look up
252 -- in an InstEnv that has entries for
255 -- Then which we choose would depend on the way in which 'a'
256 -- is instantiated. So we say there is no match, but identify
257 -- it as ambiguous case in the hope of giving a better error msg.
258 -- See the notes above from Jeff Lewis
260 lookupInstEnv env key_cls key_tys
261 = find (classInstEnv env key_cls)
263 key_vars = tyVarsOfTypes key_tys
265 find [] = NoMatch False
266 find ((tpl_tyvars, tpl, dfun_id) : rest)
267 = case matchTys tpl_tyvars tpl key_tys of
269 -- Check for reverse match, so that
270 -- we bale out if a later instantiation of this
271 -- predicate might match this instance
272 -- [see notes about overlapping instances above]
273 case matchTys key_vars key_tys tpl of
275 Just (_, _) -> NoMatch (any_match rest)
276 Just (subst, leftovers) -> ASSERT( null leftovers )
277 FoundInst subst dfun_id
279 any_match rest = or [ maybeToBool (matchTys tvs tpl key_tys)
280 | (tvs,tpl,_) <- rest
285 %************************************************************************
287 \subsection{Extending an instance environment}
289 %************************************************************************
291 @extendInstEnv@ extends a @ClsInstEnv@, checking for overlaps.
293 A boolean flag controls overlap reporting.
295 True => overlap is permitted, but only if one template matches the other;
296 not if they unify but neither is
299 extendInstEnv :: DynFlags -> InstEnv -> [DFunId] -> (InstEnv, [Message])
300 -- Similar, but all we have is the DFuns
301 extendInstEnv dflags env dfun_ids = foldl (addToInstEnv dflags) (env, []) dfun_ids
304 addToInstEnv :: DynFlags
305 -> (InstEnv, [Message])
307 -> (InstEnv, [Message]) -- Resulting InstEnv and augmented error messages
309 addToInstEnv dflags (inst_env, errs) dfun_id
310 -- Check first that the new instance doesn't
311 -- conflict with another. See notes below about fundeps.
312 | not (null bad_fundeps)
313 = (inst_env, fundep_err : errs) -- Bad fundeps; report the first only
316 = case insert_into cls_inst_env of
317 Failed err -> (inst_env, err : errs)
318 Succeeded new_env -> (addToUFM inst_env clas new_env, errs)
321 cls_inst_env = classInstEnv inst_env clas
322 (ins_tvs, _, clas, ins_tys) = splitDFunTy (idType dfun_id)
323 bad_fundeps = badFunDeps cls_inst_env clas ins_tv_set ins_tys
324 fundep_err = fundepErr dfun_id (head bad_fundeps)
326 ins_tv_set = mkVarSet ins_tvs
327 ins_item = (ins_tv_set, ins_tys, dfun_id)
329 insert_into [] = returnMaB [ins_item]
330 insert_into env@(cur_item@(tpl_tvs, tpl_tys, tpl_dfun_id) : rest)
331 = case unifyTyListsX (ins_tv_set `unionVarSet` tpl_tvs) tpl_tys ins_tys of
332 Just subst -> insert_unifiable env subst
333 Nothing -> carry_on cur_item rest
335 carry_on cur_item rest = insert_into rest `thenMaB` \ rest' ->
336 returnMaB (cur_item : rest')
338 -- The two templates unify. This is acceptable iff
339 -- (a) -fallow-overlapping-instances is on
340 -- (b) one is strictly more specific than the other
341 -- [It's bad if they are identical or incomparable]
342 insert_unifiable env@(cur_item@(tpl_tvs, tpl_tys, tpl_dfun_id) : rest) subst
343 | ins_item_more_specific && cur_item_more_specific
345 failMaB (dupInstErr dfun_id tpl_dfun_id)
347 | not (dopt Opt_AllowOverlappingInstances dflags)
348 || not (ins_item_more_specific || cur_item_more_specific)
349 = -- Overlap illegal, or the two are incomparable
350 failMaB (overlapErr dfun_id tpl_dfun_id)
353 = -- OK, it's acceptable. Remaining question is whether
354 -- we drop it here or compare it with others
355 if ins_item_more_specific then
356 -- New item is an instance of current item, so drop it here
357 returnMaB (ins_item : env)
359 carry_on cur_item rest
362 ins_item_more_specific = allVars subst ins_tvs
363 cur_item_more_specific = allVars subst (varSetElems tpl_tvs)
365 allVars :: TyVarSubstEnv -> [TyVar] -> Bool
366 -- True iff all the type vars are mapped to distinct type vars
368 = allDistinctTyVars (map lookup tvs) emptyVarSet
370 lookup tv = case lookupSubstEnv subst tv of
371 Just (DoneTy ty) -> ty
372 Nothing -> mkTyVarTy tv
375 Functional dependencies
376 ~~~~~~~~~~~~~~~~~~~~~~~
377 Here is the bad case:
378 class C a b | a->b where ...
379 instance C Int Bool where ...
380 instance C Int Char where ...
382 The point is that a->b, so Int in the first parameter must uniquely
383 determine the second. In general, given the same class decl, and given
385 instance C s1 s2 where ...
386 instance C t1 t2 where ...
388 Then the criterion is: if U=unify(s1,t1) then U(s2) = U(t2).
390 Matters are a little more complicated if there are free variables in
393 class D a b c | a -> b
394 instance D a b => D [(a,a)] [b] Int
395 instance D a b => D [a] [b] Bool
397 The instance decls don't overlap, because the third parameter keeps
398 them separate. But we want to make sure that given any constraint
406 badFunDeps :: ClsInstEnv -> Class
407 -> TyVarSet -> [Type] -- Proposed new instance type
409 badFunDeps cls_inst_env clas ins_tv_set ins_tys
410 = [ dfun_id | fd <- fds,
411 (tvs, tys, dfun_id) <- cls_inst_env,
412 not (null (checkClsFD (tvs `unionVarSet` ins_tv_set) fd clas_tvs tys ins_tys))
415 (clas_tvs, fds) = classTvsFds clas
420 dupInstErr dfun1 dfun2 = addInstErr (ptext SLIT("Duplicate instance declarations:")) dfun1 dfun2
421 overlapErr dfun1 dfun2 = addInstErr (ptext SLIT("Overlapping instance declarations:")) dfun1 dfun2
422 fundepErr dfun1 dfun2 = addInstErr (ptext SLIT("Functional dependencies conflict between instance declarations:"))
425 addInstErr what dfun1 dfun2
426 = hang what 2 (ppr_dfun dfun1 $$ ppr_dfun dfun2)
428 ppr_dfun dfun = ppr (getSrcLoc dfun) <> colon <+> pprClassPred clas tys
430 (_,_,clas,tys) = splitDFunTy (idType dfun)