Deal more correctly with orphan instances
[ghc-hetmet.git] / compiler / types / FunDeps.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 2000
4 %
5
6 FunDeps - functional dependencies
7
8 It's better to read it as: "if we know these, then we're going to know these"
9
10 \begin{code}
11 module FunDeps (
12         Equation, pprEquation,
13         oclose, grow, improveOne,
14         checkInstCoverage, checkFunDeps,
15         pprFundeps
16     ) where
17
18 #include "HsVersions.h"
19
20 import Name
21 import Var
22 import Class
23 import TcGadt
24 import TcType
25 import InstEnv
26 import VarSet
27 import VarEnv
28 import Outputable
29 import Util
30 import Data.Maybe       ( isJust )
31 \end{code}
32
33
34 %************************************************************************
35 %*                                                                      *
36 \subsection{Close type variables}
37 %*                                                                      *
38 %************************************************************************
39
40 (oclose preds tvs) closes the set of type variables tvs, 
41 wrt functional dependencies in preds.  The result is a superset
42 of the argument set.  For example, if we have
43         class C a b | a->b where ...
44 then
45         oclose [C (x,y) z, C (x,p) q] {x,y} = {x,y,z}
46 because if we know x and y then that fixes z.
47
48 Using oclose
49 ~~~~~~~~~~~~
50 oclose is used
51
52 a) When determining ambiguity.  The type
53         forall a,b. C a b => a
54 is not ambiguous (given the above class decl for C) because
55 a determines b.  
56
57 b) When generalising a type T.  Usually we take FV(T) \ FV(Env),
58 but in fact we need
59         FV(T) \ (FV(Env)+)
60 where the '+' is the oclosure operation.  Notice that we do not 
61 take FV(T)+.  This puzzled me for a bit.  Consider
62
63         f = E
64
65 and suppose e have that E :: C a b => a, and suppose that b is
66 free in the environment. Then we quantify over 'a' only, giving
67 the type forall a. C a b => a.  Since a->b but we don't have b->a,
68 we might have instance decls like
69         instance C Bool Int where ...
70         instance C Char Int where ...
71 so knowing that b=Int doesn't fix 'a'; so we quantify over it.
72
73                 ---------------
74                 A WORRY: ToDo!
75                 ---------------
76 If we have      class C a b => D a b where ....
77                 class D a b | a -> b where ...
78 and the preds are [C (x,y) z], then we want to see the fd in D,
79 even though it is not explicit in C, giving [({x,y},{z})]
80
81 Similarly for instance decls?  E.g. Suppose we have
82         instance C a b => Eq (T a b) where ...
83 and we infer a type t with constraints Eq (T a b) for a particular
84 expression, and suppose that 'a' is free in the environment.  
85 We could generalise to
86         forall b. Eq (T a b) => t
87 but if we reduced the constraint, to C a b, we'd see that 'a' determines
88 b, so that a better type might be
89         t (with free constraint C a b) 
90 Perhaps it doesn't matter, because we'll still force b to be a
91 particular type at the call sites.  Generalising over too many
92 variables (provided we don't shadow anything by quantifying over a
93 variable that is actually free in the envt) may postpone errors; it
94 won't hide them altogether.
95
96
97 \begin{code}
98 oclose :: [PredType] -> TyVarSet -> TyVarSet
99 oclose preds fixed_tvs
100   | null tv_fds = fixed_tvs     -- Fast escape hatch for common case
101   | otherwise   = loop fixed_tvs
102   where
103     loop fixed_tvs
104         | new_fixed_tvs `subVarSet` fixed_tvs = fixed_tvs
105         | otherwise                           = loop new_fixed_tvs
106         where
107           new_fixed_tvs = foldl extend fixed_tvs tv_fds
108
109     extend fixed_tvs (ls,rs) | ls `subVarSet` fixed_tvs = fixed_tvs `unionVarSet` rs
110                              | otherwise                = fixed_tvs
111
112     tv_fds  :: [(TyVarSet,TyVarSet)]
113         -- In our example, tv_fds will be [ ({x,y}, {z}), ({x,p},{q}) ]
114         -- Meaning "knowing x,y fixes z, knowing x,p fixes q"
115     tv_fds  = [ (tyVarsOfTypes xs, tyVarsOfTypes ys)
116               | ClassP cls tys <- preds,                -- Ignore implicit params
117                 let (cls_tvs, cls_fds) = classTvsFds cls,
118                 fd <- cls_fds,
119                 let (xs,ys) = instFD fd cls_tvs tys
120               ]
121 \end{code}
122
123 Note [Growing the tau-tvs using constraints]
124 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125 (grow preds tvs) is the result of extend the set of tyvars tvs
126                  using all conceivable links from pred
127
128 E.g. tvs = {a}, preds = {H [a] b, K (b,Int) c, Eq e}
129 Then grow precs tvs = {a,b,c}
130
131 All the type variables from an implicit parameter are added, whether or
132 not they are mentioned in tvs; see Note [Implicit parameters and ambiguity] 
133 in TcSimplify.
134
135 See also Note [Ambiguity] in TcSimplify
136
137 \begin{code}
138 grow :: [PredType] -> TyVarSet -> TyVarSet
139 grow preds fixed_tvs 
140   | null preds = real_fixed_tvs
141   | otherwise  = loop real_fixed_tvs
142   where
143         -- Add the implicit parameters; 
144         -- see Note [Implicit parameters and ambiguity] in TcSimplify
145     real_fixed_tvs = foldr unionVarSet fixed_tvs ip_tvs
146  
147     loop fixed_tvs
148         | new_fixed_tvs `subVarSet` fixed_tvs = fixed_tvs
149         | otherwise                           = loop new_fixed_tvs
150         where
151           new_fixed_tvs = foldl extend fixed_tvs non_ip_tvs
152
153     extend fixed_tvs pred_tvs 
154         | fixed_tvs `intersectsVarSet` pred_tvs = fixed_tvs `unionVarSet` pred_tvs
155         | otherwise                             = fixed_tvs
156
157     (ip_tvs, non_ip_tvs) = partitionWith get_ip preds
158     get_ip (IParam _ ty) = Left (tyVarsOfType ty)
159     get_ip other         = Right (tyVarsOfPred other)
160 \end{code}
161     
162 %************************************************************************
163 %*                                                                      *
164 \subsection{Generate equations from functional dependencies}
165 %*                                                                      *
166 %************************************************************************
167
168
169 \begin{code}
170 type Equation = (TyVarSet, [(Type, Type)])
171 -- These pairs of types should be equal, for some
172 -- substitution of the tyvars in the tyvar set
173 -- INVARIANT: corresponding types aren't already equal
174
175 -- It's important that we have a *list* of pairs of types.  Consider
176 --      class C a b c | a -> b c where ...
177 --      instance C Int x x where ...
178 -- Then, given the constraint (C Int Bool v) we should improve v to Bool,
179 -- via the equation ({x}, [(Bool,x), (v,x)])
180 -- This would not happen if the class had looked like
181 --      class C a b c | a -> b, a -> c
182
183 -- To "execute" the equation, make fresh type variable for each tyvar in the set,
184 -- instantiate the two types with these fresh variables, and then unify.
185 --
186 -- For example, ({a,b}, (a,Int,b), (Int,z,Bool))
187 -- We unify z with Int, but since a and b are quantified we do nothing to them
188 -- We usually act on an equation by instantiating the quantified type varaibles
189 -- to fresh type variables, and then calling the standard unifier.
190
191 pprEquation (qtvs, pairs) 
192   = vcat [ptext SLIT("forall") <+> braces (pprWithCommas ppr (varSetElems qtvs)),
193           nest 2 (vcat [ ppr t1 <+> ptext SLIT(":=:") <+> ppr t2 | (t1,t2) <- pairs])]
194 \end{code}
195
196 Given a bunch of predicates that must hold, such as
197
198         C Int t1, C Int t2, C Bool t3, ?x::t4, ?x::t5
199
200 improve figures out what extra equations must hold.
201 For example, if we have
202
203         class C a b | a->b where ...
204
205 then improve will return
206
207         [(t1,t2), (t4,t5)]
208
209 NOTA BENE:
210
211   * improve does not iterate.  It's possible that when we make
212     t1=t2, for example, that will in turn trigger a new equation.
213     This would happen if we also had
214         C t1 t7, C t2 t8
215     If t1=t2, we also get t7=t8.
216
217     improve does *not* do this extra step.  It relies on the caller
218     doing so.
219
220   * The equations unify types that are not already equal.  So there
221     is no effect iff the result of improve is empty
222
223
224
225 \begin{code}
226 type Pred_Loc = (PredType, SDoc)        -- SDoc says where the Pred comes from
227
228 improveOne :: (Class -> [Instance])             -- Gives instances for given class
229            -> Pred_Loc                          -- Do improvement triggered by this
230            -> [Pred_Loc]                        -- Current constraints 
231            -> [(Equation,Pred_Loc,Pred_Loc)]    -- Derived equalities that must also hold
232                                                 -- (NB the above INVARIANT for type Equation)
233                                                 -- The Pred_Locs explain which two predicates were
234                                                 -- combined (for error messages)
235 -- Just do improvement triggered by a single, distinguised predicate
236
237 improveOne inst_env pred@(IParam ip ty, _) preds
238   = [ ((emptyVarSet, [(ty,ty2)]), pred, p2) 
239     | p2@(IParam ip2 ty2, _) <- preds
240     , ip==ip2
241     , not (ty `tcEqType` ty2)]
242
243 improveOne inst_env pred@(ClassP cls tys, _) preds
244   | tys `lengthAtLeast` 2
245   = instance_eqns ++ pairwise_eqns
246         -- NB: we put the instance equations first.   This biases the 
247         -- order so that we first improve individual constraints against the
248         -- instances (which are perhaps in a library and less likely to be
249         -- wrong; and THEN perform the pairwise checks.
250         -- The other way round, it's possible for the pairwise check to succeed
251         -- and cause a subsequent, misleading failure of one of the pair with an
252         -- instance declaration.  See tcfail143.hs for an example
253   where
254     (cls_tvs, cls_fds) = classTvsFds cls
255     instances          = inst_env cls
256     rough_tcs          = roughMatchTcs tys
257
258         -- NOTE that we iterate over the fds first; they are typically
259         -- empty, which aborts the rest of the loop.
260     pairwise_eqns :: [(Equation,Pred_Loc,Pred_Loc)]
261     pairwise_eqns       -- This group comes from pairwise comparison
262       = [ (eqn, pred, p2)
263         | fd <- cls_fds
264         , p2@(ClassP cls2 tys2, _) <- preds
265         , cls == cls2
266         , eqn <- checkClsFD emptyVarSet fd cls_tvs tys tys2
267         ]
268
269     instance_eqns :: [(Equation,Pred_Loc,Pred_Loc)]
270     instance_eqns       -- This group comes from comparing with instance decls
271       = [ (eqn, p_inst, pred)
272         | fd <- cls_fds         -- Iterate through the fundeps first, 
273                                 -- because there often are none!
274         , let trimmed_tcs = trimRoughMatchTcs cls_tvs fd rough_tcs
275                 -- Trim the rough_tcs based on the head of the fundep.
276                 -- Remember that instanceCantMatch treats both argumnents
277                 -- symmetrically, so it's ok to trim the rough_tcs,
278                 -- rather than trimming each inst_tcs in turn
279         , ispec@(Instance { is_tvs = qtvs, is_tys = tys_inst, 
280                             is_tcs = inst_tcs }) <- instances
281         , not (instanceCantMatch inst_tcs trimmed_tcs)
282         , eqn <- checkClsFD qtvs fd cls_tvs tys_inst tys
283         , let p_inst = (mkClassPred cls tys_inst, 
284                         ptext SLIT("arising from the instance declaration at")
285                         <+> ppr (getSrcLoc ispec))
286         ]
287
288 improveOne inst_env eq_pred preds
289   = []
290
291
292 checkClsFD :: TyVarSet                  -- Quantified type variables; see note below
293            -> FunDep TyVar -> [TyVar]   -- One functional dependency from the class
294            -> [Type] -> [Type]
295            -> [Equation]
296
297 checkClsFD qtvs fd clas_tvs tys1 tys2
298 -- 'qtvs' are the quantified type variables, the ones which an be instantiated 
299 -- to make the types match.  For example, given
300 --      class C a b | a->b where ...
301 --      instance C (Maybe x) (Tree x) where ..
302 --
303 -- and an Inst of form (C (Maybe t1) t2), 
304 -- then we will call checkClsFD with
305 --
306 --      qtvs = {x}, tys1 = [Maybe x,  Tree x]
307 --                  tys2 = [Maybe t1, t2]
308 --
309 -- We can instantiate x to t1, and then we want to force
310 --      (Tree x) [t1/x]  :=:   t2
311 --
312 -- This function is also used when matching two Insts (rather than an Inst
313 -- against an instance decl. In that case, qtvs is empty, and we are doing
314 -- an equality check
315 -- 
316 -- This function is also used by InstEnv.badFunDeps, which needs to *unify*
317 -- For the one-sided matching case, the qtvs are just from the template,
318 -- so we get matching
319 --
320   = ASSERT2( length tys1 == length tys2     && 
321              length tys1 == length clas_tvs 
322             , ppr tys1 <+> ppr tys2 )
323
324     case tcUnifyTys bind_fn ls1 ls2 of
325         Nothing  -> []
326         Just subst | isJust (tcUnifyTys bind_fn rs1' rs2') 
327                         -- Don't include any equations that already hold. 
328                         -- Reason: then we know if any actual improvement has happened,
329                         --         in which case we need to iterate the solver
330                         -- In making this check we must taking account of the fact that any 
331                         -- qtvs that aren't already instantiated can be instantiated to anything 
332                         -- at all
333                   -> []
334
335                   | otherwise   -- Aha!  A useful equation
336                   -> [ (qtvs', zip rs1' rs2')]
337                         -- We could avoid this substTy stuff by producing the eqn
338                         -- (qtvs, ls1++rs1, ls2++rs2)
339                         -- which will re-do the ls1/ls2 unification when the equation is
340                         -- executed.  What we're doing instead is recording the partial
341                         -- work of the ls1/ls2 unification leaving a smaller unification problem
342                   where
343                     rs1'  = substTys subst rs1 
344                     rs2'  = substTys subst rs2
345                     qtvs' = filterVarSet (`notElemTvSubst` subst) qtvs
346                         -- qtvs' are the quantified type variables
347                         -- that have not been substituted out
348                         --      
349                         -- Eg.  class C a b | a -> b
350                         --      instance C Int [y]
351                         -- Given constraint C Int z
352                         -- we generate the equation
353                         --      ({y}, [y], z)
354   where
355     bind_fn tv | tv `elemVarSet` qtvs = BindMe
356                | otherwise            = Skolem
357
358     (ls1, rs1) = instFD fd clas_tvs tys1
359     (ls2, rs2) = instFD fd clas_tvs tys2
360
361 instFD :: FunDep TyVar -> [TyVar] -> [Type] -> FunDep Type
362 instFD (ls,rs) tvs tys
363   = (map lookup ls, map lookup rs)
364   where
365     env       = zipVarEnv tvs tys
366     lookup tv = lookupVarEnv_NF env tv
367 \end{code}
368
369 \begin{code}
370 checkInstCoverage :: Class -> [Type] -> Bool
371 -- Check that the Coverage Condition is obeyed in an instance decl
372 -- For example, if we have 
373 --      class theta => C a b | a -> b
374 --      instance C t1 t2 
375 -- Then we require fv(t2) `subset` fv(t1)
376 -- See Note [Coverage Condition] below
377
378 checkInstCoverage clas inst_taus
379   = all fundep_ok fds
380   where
381     (tyvars, fds) = classTvsFds clas
382     fundep_ok fd  = tyVarsOfTypes rs `subVarSet` tyVarsOfTypes ls
383                  where
384                    (ls,rs) = instFD fd tyvars inst_taus
385 \end{code}
386
387 Note [Coverage condition]
388 ~~~~~~~~~~~~~~~~~~~~~~~~~
389 For the coverage condition, we used to require only that 
390         fv(t2) `subset` oclose(fv(t1), theta)
391
392 Example:
393         class Mul a b c | a b -> c where
394                 (.*.) :: a -> b -> c
395
396         instance Mul Int Int Int where (.*.) = (*)
397         instance Mul Int Float Float where x .*. y = fromIntegral x * y
398         instance Mul a b c => Mul a [b] [c] where x .*. v = map (x.*.) v
399
400 In the third instance, it's not the case that fv([c]) `subset` fv(a,[b]).
401 But it is the case that fv([c]) `subset` oclose( theta, fv(a,[b]) )
402
403 But it is a mistake to accept the instance because then this defn:
404         f = \ b x y -> if b then x .*. [y] else y
405 makes instance inference go into a loop, because it requires the constraint
406         Mul a [b] b
407
408
409 %************************************************************************
410 %*                                                                      *
411         Check that a new instance decl is OK wrt fundeps
412 %*                                                                      *
413 %************************************************************************
414
415 Here is the bad case:
416         class C a b | a->b where ...
417         instance C Int Bool where ...
418         instance C Int Char where ...
419
420 The point is that a->b, so Int in the first parameter must uniquely
421 determine the second.  In general, given the same class decl, and given
422
423         instance C s1 s2 where ...
424         instance C t1 t2 where ...
425
426 Then the criterion is: if U=unify(s1,t1) then U(s2) = U(t2).
427
428 Matters are a little more complicated if there are free variables in
429 the s2/t2.  
430
431         class D a b c | a -> b
432         instance D a b => D [(a,a)] [b] Int
433         instance D a b => D [a]     [b] Bool
434
435 The instance decls don't overlap, because the third parameter keeps
436 them separate.  But we want to make sure that given any constraint
437         D s1 s2 s3
438 if s1 matches 
439
440
441 \begin{code}
442 checkFunDeps :: (InstEnv, InstEnv) -> Instance
443              -> Maybe [Instance]        -- Nothing  <=> ok
444                                         -- Just dfs <=> conflict with dfs
445 -- Check wheher adding DFunId would break functional-dependency constraints
446 -- Used only for instance decls defined in the module being compiled
447 checkFunDeps inst_envs ispec
448   | null bad_fundeps = Nothing
449   | otherwise        = Just bad_fundeps
450   where
451     (ins_tvs, _, clas, ins_tys) = instanceHead ispec
452     ins_tv_set   = mkVarSet ins_tvs
453     cls_inst_env = classInstances inst_envs clas
454     bad_fundeps  = badFunDeps cls_inst_env clas ins_tv_set ins_tys
455
456 badFunDeps :: [Instance] -> Class
457            -> TyVarSet -> [Type]        -- Proposed new instance type
458            -> [Instance]
459 badFunDeps cls_insts clas ins_tv_set ins_tys 
460   = [ ispec | fd <- fds,        -- fds is often empty
461               let trimmed_tcs = trimRoughMatchTcs clas_tvs fd rough_tcs,
462               ispec@(Instance { is_tcs = inst_tcs, is_tvs = tvs, 
463                                 is_tys = tys }) <- cls_insts,
464                 -- Filter out ones that can't possibly match, 
465                 -- based on the head of the fundep
466               not (instanceCantMatch inst_tcs trimmed_tcs),     
467               notNull (checkClsFD (tvs `unionVarSet` ins_tv_set) 
468                                    fd clas_tvs tys ins_tys)
469     ]
470   where
471     (clas_tvs, fds) = classTvsFds clas
472     rough_tcs = roughMatchTcs ins_tys
473
474 trimRoughMatchTcs :: [TyVar] -> FunDep TyVar -> [Maybe Name] -> [Maybe Name]
475 -- Computing rough_tcs for a particular fundep
476 --      class C a b c | a -> b where ... 
477 -- For each instance .... => C ta tb tc
478 -- we want to match only on the types ta, tc; so our
479 -- rough-match thing must similarly be filtered.  
480 -- Hence, we Nothing-ise the tb type right here
481 trimRoughMatchTcs clas_tvs (_,rtvs) mb_tcs
482   = zipWith select clas_tvs mb_tcs
483   where
484     select clas_tv mb_tc | clas_tv `elem` rtvs = Nothing
485                          | otherwise           = mb_tc
486 \end{code}
487
488
489