2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 2000
6 FunDeps - functional dependencies
8 It's better to read it as: "if we know these, then we're going to know these"
12 Equation, pprEquation,
13 oclose, grow, improveOne,
14 checkInstCoverage, checkFunDeps,
18 #include "HsVersions.h"
30 import Data.Maybe ( isJust )
34 %************************************************************************
36 \subsection{Close type variables}
38 %************************************************************************
40 oclose(vs,C) The result of extending the set of tyvars vs
41 using the functional dependencies from C
43 grow(vs,C) The result of extend the set of tyvars vs
44 using all conceivable links from C.
46 E.g. vs = {a}, C = {H [a] b, K (b,Int) c, Eq e}
47 Then grow(vs,C) = {a,b,c}
49 Note that grow(vs,C) `superset` grow(vs,simplify(C))
50 That is, simplfication can only shrink the result of grow.
53 oclose is conservative v `elem` oclose(vs,C)
54 one way: => v is definitely fixed by vs
56 grow is conservative if v might be fixed by vs
57 the other way: => v `elem` grow(vs,C)
59 ----------------------------------------------------------
60 (oclose preds tvs) closes the set of type variables tvs,
61 wrt functional dependencies in preds. The result is a superset
62 of the argument set. For example, if we have
63 class C a b | a->b where ...
65 oclose [C (x,y) z, C (x,p) q] {x,y} = {x,y,z}
66 because if we know x and y then that fixes z.
68 oclose is used (only) when generalising a type T; see extensive
71 Note [Important subtlety in oclose]
72 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73 Consider (oclose (C Int t) {}), where class C a b | a->b
74 Then, since a->b, 't' is fully determined by Int, and the
75 uniform thing is to return {t}.
79 f x = e -- 'e' generates constraint (D s Int t)
81 Then, if (oclose (D s Int t) {}) = {t}, we'll make the function
82 monomorphic in 't', thus
83 f :: forall s. D s Int t => s -> s
85 But if this function is never called, 't' will never be instantiated;
86 the functional dependencies that fix 't' may well be instance decls in
87 some importing module. But the top-level defaulting of unconstrained
88 type variables will fix t=GHC.Prim.Any, and that's simply a bug.
90 Conclusion: oclose only returns a type variable as "fixed" if it
91 depends on at least one type variable in the input fixed_tvs.
93 Remember, it's always sound for oclose to return a smaller set.
94 An interesting example is tcfail093, where we get this inferred type:
96 dup :: forall h. (Call (IO Int) h) => () -> Int -> h
97 This is perhaps a bit silly, because 'h' is fixed by the (IO Int);
98 previously GHC rejected this saying 'no instance for Call (IO Int) h'.
99 But it's right on the borderline. If there was an extra, otherwise
100 uninvolved type variable, like 's' in the type of 'f' above, then
101 we must accept the function. So, for now anyway, we accept 'dup' too.
104 oclose :: [PredType] -> TyVarSet -> TyVarSet
105 oclose preds fixed_tvs
106 | null tv_fds = fixed_tvs -- Fast escape hatch for common case
107 | isEmptyVarSet fixed_tvs = emptyVarSet -- Note [Important subtlety in oclose]
108 | otherwise = loop fixed_tvs
111 | new_fixed_tvs `subVarSet` fixed_tvs = fixed_tvs
112 | otherwise = loop new_fixed_tvs
114 new_fixed_tvs = foldl extend fixed_tvs tv_fds
116 extend fixed_tvs (ls,rs)
117 | not (isEmptyVarSet ls) -- Note [Important subtlety in oclose]
118 , ls `subVarSet` fixed_tvs = fixed_tvs `unionVarSet` rs
119 | otherwise = fixed_tvs
121 tv_fds :: [(TyVarSet,TyVarSet)]
122 -- In our example, tv_fds will be [ ({x,y}, {z}), ({x,p},{q}) ]
123 -- Meaning "knowing x,y fixes z, knowing x,p fixes q"
124 tv_fds = [ (tyVarsOfTypes xs, tyVarsOfTypes ys)
125 | ClassP cls tys <- preds, -- Ignore implicit params
126 let (cls_tvs, cls_fds) = classTvsFds cls,
128 let (xs,ys) = instFD fd cls_tvs tys
132 Note [Growing the tau-tvs using constraints]
133 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134 (grow preds tvs) is the result of extend the set of tyvars tvs
135 using all conceivable links from pred
137 E.g. tvs = {a}, preds = {H [a] b, K (b,Int) c, Eq e}
138 Then grow precs tvs = {a,b,c}
140 All the type variables from an implicit parameter are added, whether or
141 not they are mentioned in tvs; see Note [Implicit parameters and ambiguity]
144 See also Note [Ambiguity] in TcSimplify
147 grow :: [PredType] -> TyVarSet -> TyVarSet
149 | null preds = fixed_tvs
150 | otherwise = loop real_fixed_tvs
152 -- Add the implicit parameters;
153 -- see Note [Implicit parameters and ambiguity] in TcSimplify
154 real_fixed_tvs = foldr unionVarSet fixed_tvs ip_tvs
157 | new_fixed_tvs `subVarSet` fixed_tvs = fixed_tvs
158 | otherwise = loop new_fixed_tvs
160 new_fixed_tvs = foldl extend fixed_tvs non_ip_tvs
162 extend fixed_tvs pred_tvs
163 | fixed_tvs `intersectsVarSet` pred_tvs = fixed_tvs `unionVarSet` pred_tvs
164 | otherwise = fixed_tvs
166 (ip_tvs, non_ip_tvs) = partitionWith get_ip preds
167 get_ip (IParam _ ty) = Left (tyVarsOfType ty)
168 get_ip other = Right (tyVarsOfPred other)
171 %************************************************************************
173 \subsection{Generate equations from functional dependencies}
175 %************************************************************************
179 type Equation = (TyVarSet, [(Type, Type)])
180 -- These pairs of types should be equal, for some
181 -- substitution of the tyvars in the tyvar set
182 -- INVARIANT: corresponding types aren't already equal
184 -- It's important that we have a *list* of pairs of types. Consider
185 -- class C a b c | a -> b c where ...
186 -- instance C Int x x where ...
187 -- Then, given the constraint (C Int Bool v) we should improve v to Bool,
188 -- via the equation ({x}, [(Bool,x), (v,x)])
189 -- This would not happen if the class had looked like
190 -- class C a b c | a -> b, a -> c
192 -- To "execute" the equation, make fresh type variable for each tyvar in the set,
193 -- instantiate the two types with these fresh variables, and then unify.
195 -- For example, ({a,b}, (a,Int,b), (Int,z,Bool))
196 -- We unify z with Int, but since a and b are quantified we do nothing to them
197 -- We usually act on an equation by instantiating the quantified type varaibles
198 -- to fresh type variables, and then calling the standard unifier.
200 pprEquation :: Equation -> SDoc
201 pprEquation (qtvs, pairs)
202 = vcat [ptext SLIT("forall") <+> braces (pprWithCommas ppr (varSetElems qtvs)),
203 nest 2 (vcat [ ppr t1 <+> ptext SLIT(":=:") <+> ppr t2 | (t1,t2) <- pairs])]
206 Given a bunch of predicates that must hold, such as
208 C Int t1, C Int t2, C Bool t3, ?x::t4, ?x::t5
210 improve figures out what extra equations must hold.
211 For example, if we have
213 class C a b | a->b where ...
215 then improve will return
221 * improve does not iterate. It's possible that when we make
222 t1=t2, for example, that will in turn trigger a new equation.
223 This would happen if we also had
225 If t1=t2, we also get t7=t8.
227 improve does *not* do this extra step. It relies on the caller
230 * The equations unify types that are not already equal. So there
231 is no effect iff the result of improve is empty
236 type Pred_Loc = (PredType, SDoc) -- SDoc says where the Pred comes from
238 improveOne :: (Class -> [Instance]) -- Gives instances for given class
239 -> Pred_Loc -- Do improvement triggered by this
240 -> [Pred_Loc] -- Current constraints
241 -> [(Equation,Pred_Loc,Pred_Loc)] -- Derived equalities that must also hold
242 -- (NB the above INVARIANT for type Equation)
243 -- The Pred_Locs explain which two predicates were
244 -- combined (for error messages)
245 -- Just do improvement triggered by a single, distinguised predicate
247 improveOne _inst_env pred@(IParam ip ty, _) preds
248 = [ ((emptyVarSet, [(ty,ty2)]), pred, p2)
249 | p2@(IParam ip2 ty2, _) <- preds
251 , not (ty `tcEqType` ty2)]
253 improveOne inst_env pred@(ClassP cls tys, _) preds
254 | tys `lengthAtLeast` 2
255 = instance_eqns ++ pairwise_eqns
256 -- NB: we put the instance equations first. This biases the
257 -- order so that we first improve individual constraints against the
258 -- instances (which are perhaps in a library and less likely to be
259 -- wrong; and THEN perform the pairwise checks.
260 -- The other way round, it's possible for the pairwise check to succeed
261 -- and cause a subsequent, misleading failure of one of the pair with an
262 -- instance declaration. See tcfail143.hs for an example
264 (cls_tvs, cls_fds) = classTvsFds cls
265 instances = inst_env cls
266 rough_tcs = roughMatchTcs tys
268 -- NOTE that we iterate over the fds first; they are typically
269 -- empty, which aborts the rest of the loop.
270 pairwise_eqns :: [(Equation,Pred_Loc,Pred_Loc)]
271 pairwise_eqns -- This group comes from pairwise comparison
274 , p2@(ClassP cls2 tys2, _) <- preds
276 , eqn <- checkClsFD emptyVarSet fd cls_tvs tys tys2
279 instance_eqns :: [(Equation,Pred_Loc,Pred_Loc)]
280 instance_eqns -- This group comes from comparing with instance decls
281 = [ (eqn, p_inst, pred)
282 | fd <- cls_fds -- Iterate through the fundeps first,
283 -- because there often are none!
284 , let trimmed_tcs = trimRoughMatchTcs cls_tvs fd rough_tcs
285 -- Trim the rough_tcs based on the head of the fundep.
286 -- Remember that instanceCantMatch treats both argumnents
287 -- symmetrically, so it's ok to trim the rough_tcs,
288 -- rather than trimming each inst_tcs in turn
289 , ispec@(Instance { is_tvs = qtvs, is_tys = tys_inst,
290 is_tcs = inst_tcs }) <- instances
291 , not (instanceCantMatch inst_tcs trimmed_tcs)
292 , eqn <- checkClsFD qtvs fd cls_tvs tys_inst tys
293 , let p_inst = (mkClassPred cls tys_inst,
294 ptext SLIT("arising from the instance declaration at")
295 <+> ppr (getSrcLoc ispec))
302 checkClsFD :: TyVarSet -- Quantified type variables; see note below
303 -> FunDep TyVar -> [TyVar] -- One functional dependency from the class
307 checkClsFD qtvs fd clas_tvs tys1 tys2
308 -- 'qtvs' are the quantified type variables, the ones which an be instantiated
309 -- to make the types match. For example, given
310 -- class C a b | a->b where ...
311 -- instance C (Maybe x) (Tree x) where ..
313 -- and an Inst of form (C (Maybe t1) t2),
314 -- then we will call checkClsFD with
316 -- qtvs = {x}, tys1 = [Maybe x, Tree x]
317 -- tys2 = [Maybe t1, t2]
319 -- We can instantiate x to t1, and then we want to force
320 -- (Tree x) [t1/x] :=: t2
322 -- This function is also used when matching two Insts (rather than an Inst
323 -- against an instance decl. In that case, qtvs is empty, and we are doing
326 -- This function is also used by InstEnv.badFunDeps, which needs to *unify*
327 -- For the one-sided matching case, the qtvs are just from the template,
328 -- so we get matching
330 = ASSERT2( length tys1 == length tys2 &&
331 length tys1 == length clas_tvs
332 , ppr tys1 <+> ppr tys2 )
334 case tcUnifyTys bind_fn ls1 ls2 of
336 Just subst | isJust (tcUnifyTys bind_fn rs1' rs2')
337 -- Don't include any equations that already hold.
338 -- Reason: then we know if any actual improvement has happened,
339 -- in which case we need to iterate the solver
340 -- In making this check we must taking account of the fact that any
341 -- qtvs that aren't already instantiated can be instantiated to anything
345 | otherwise -- Aha! A useful equation
346 -> [ (qtvs', zip rs1' rs2')]
347 -- We could avoid this substTy stuff by producing the eqn
348 -- (qtvs, ls1++rs1, ls2++rs2)
349 -- which will re-do the ls1/ls2 unification when the equation is
350 -- executed. What we're doing instead is recording the partial
351 -- work of the ls1/ls2 unification leaving a smaller unification problem
353 rs1' = substTys subst rs1
354 rs2' = substTys subst rs2
355 qtvs' = filterVarSet (`notElemTvSubst` subst) qtvs
356 -- qtvs' are the quantified type variables
357 -- that have not been substituted out
359 -- Eg. class C a b | a -> b
360 -- instance C Int [y]
361 -- Given constraint C Int z
362 -- we generate the equation
365 bind_fn tv | tv `elemVarSet` qtvs = BindMe
368 (ls1, rs1) = instFD fd clas_tvs tys1
369 (ls2, rs2) = instFD fd clas_tvs tys2
371 instFD :: FunDep TyVar -> [TyVar] -> [Type] -> FunDep Type
372 instFD (ls,rs) tvs tys
373 = (map lookup ls, map lookup rs)
375 env = zipVarEnv tvs tys
376 lookup tv = lookupVarEnv_NF env tv
380 checkInstCoverage :: Class -> [Type] -> Bool
381 -- Check that the Coverage Condition is obeyed in an instance decl
382 -- For example, if we have
383 -- class theta => C a b | a -> b
385 -- Then we require fv(t2) `subset` fv(t1)
386 -- See Note [Coverage Condition] below
388 checkInstCoverage clas inst_taus
391 (tyvars, fds) = classTvsFds clas
392 fundep_ok fd = tyVarsOfTypes rs `subVarSet` tyVarsOfTypes ls
394 (ls,rs) = instFD fd tyvars inst_taus
397 Note [Coverage condition]
398 ~~~~~~~~~~~~~~~~~~~~~~~~~
399 For the coverage condition, we used to require only that
400 fv(t2) `subset` oclose(fv(t1), theta)
403 class Mul a b c | a b -> c where
406 instance Mul Int Int Int where (.*.) = (*)
407 instance Mul Int Float Float where x .*. y = fromIntegral x * y
408 instance Mul a b c => Mul a [b] [c] where x .*. v = map (x.*.) v
410 In the third instance, it's not the case that fv([c]) `subset` fv(a,[b]).
411 But it is the case that fv([c]) `subset` oclose( theta, fv(a,[b]) )
413 But it is a mistake to accept the instance because then this defn:
414 f = \ b x y -> if b then x .*. [y] else y
415 makes instance inference go into a loop, because it requires the constraint
419 %************************************************************************
421 Check that a new instance decl is OK wrt fundeps
423 %************************************************************************
425 Here is the bad case:
426 class C a b | a->b where ...
427 instance C Int Bool where ...
428 instance C Int Char where ...
430 The point is that a->b, so Int in the first parameter must uniquely
431 determine the second. In general, given the same class decl, and given
433 instance C s1 s2 where ...
434 instance C t1 t2 where ...
436 Then the criterion is: if U=unify(s1,t1) then U(s2) = U(t2).
438 Matters are a little more complicated if there are free variables in
441 class D a b c | a -> b
442 instance D a b => D [(a,a)] [b] Int
443 instance D a b => D [a] [b] Bool
445 The instance decls don't overlap, because the third parameter keeps
446 them separate. But we want to make sure that given any constraint
452 checkFunDeps :: (InstEnv, InstEnv) -> Instance
453 -> Maybe [Instance] -- Nothing <=> ok
454 -- Just dfs <=> conflict with dfs
455 -- Check wheher adding DFunId would break functional-dependency constraints
456 -- Used only for instance decls defined in the module being compiled
457 checkFunDeps inst_envs ispec
458 | null bad_fundeps = Nothing
459 | otherwise = Just bad_fundeps
461 (ins_tvs, _, clas, ins_tys) = instanceHead ispec
462 ins_tv_set = mkVarSet ins_tvs
463 cls_inst_env = classInstances inst_envs clas
464 bad_fundeps = badFunDeps cls_inst_env clas ins_tv_set ins_tys
466 badFunDeps :: [Instance] -> Class
467 -> TyVarSet -> [Type] -- Proposed new instance type
469 badFunDeps cls_insts clas ins_tv_set ins_tys
470 = [ ispec | fd <- fds, -- fds is often empty
471 let trimmed_tcs = trimRoughMatchTcs clas_tvs fd rough_tcs,
472 ispec@(Instance { is_tcs = inst_tcs, is_tvs = tvs,
473 is_tys = tys }) <- cls_insts,
474 -- Filter out ones that can't possibly match,
475 -- based on the head of the fundep
476 not (instanceCantMatch inst_tcs trimmed_tcs),
477 notNull (checkClsFD (tvs `unionVarSet` ins_tv_set)
478 fd clas_tvs tys ins_tys)
481 (clas_tvs, fds) = classTvsFds clas
482 rough_tcs = roughMatchTcs ins_tys
484 trimRoughMatchTcs :: [TyVar] -> FunDep TyVar -> [Maybe Name] -> [Maybe Name]
485 -- Computing rough_tcs for a particular fundep
486 -- class C a b c | a -> b where ...
487 -- For each instance .... => C ta tb tc
488 -- we want to match only on the types ta, tc; so our
489 -- rough-match thing must similarly be filtered.
490 -- Hence, we Nothing-ise the tb type right here
491 trimRoughMatchTcs clas_tvs (_,rtvs) mb_tcs
492 = zipWith select clas_tvs mb_tcs
494 select clas_tv mb_tc | clas_tv `elem` rtvs = Nothing