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