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