Improve handling of implicit parameters
[ghc-hetmet.git] / compiler / types / FunDeps.lhs
index cad292b..b8b97b1 100644 (file)
@@ -21,8 +21,6 @@ import Name
 import Var
 import Class
 import TcGadt
-import Type
-import Coercion
 import TcType
 import InstEnv
 import VarSet
@@ -125,24 +123,43 @@ oclose preds fixed_tvs
              ]
 \end{code}
 
+Note [Growing the tau-tvs using constraints]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+(grow preds tvs) is the result of extend the set of tyvars tvs
+                using all conceivable links from pred
+
+E.g. tvs = {a}, preds = {H [a] b, K (b,Int) c, Eq e}
+Then grow precs tvs = {a,b,c}
+
+All the type variables from an implicit parameter are added, whether or
+not they are mentioned in tvs; see Note [Implicit parameters and ambiguity] 
+in TcSimplify.
+
+See also Note [Ambiguity] in TcSimplify
+
 \begin{code}
 grow :: [PredType] -> TyVarSet -> TyVarSet
--- See Note [Ambiguity] in TcSimplify
 grow preds fixed_tvs 
-  | null preds = fixed_tvs
-  | otherwise  = loop fixed_tvs
+  | null preds = real_fixed_tvs
+  | otherwise  = loop real_fixed_tvs
   where
+       -- Add the implicit parameters; 
+       -- see Note [Implicit parameters and ambiguity] in TcSimplify
+    real_fixed_tvs = foldr unionVarSet fixed_tvs ip_tvs
     loop fixed_tvs
        | new_fixed_tvs `subVarSet` fixed_tvs = fixed_tvs
        | otherwise                           = loop new_fixed_tvs
        where
-         new_fixed_tvs = foldl extend fixed_tvs pred_sets
+         new_fixed_tvs = foldl extend fixed_tvs non_ip_tvs
 
     extend fixed_tvs pred_tvs 
        | fixed_tvs `intersectsVarSet` pred_tvs = fixed_tvs `unionVarSet` pred_tvs
        | otherwise                             = fixed_tvs
 
-    pred_sets = [tyVarsOfPred pred | pred <- preds]
+    (ip_tvs, non_ip_tvs) = partitionWith get_ip preds
+    get_ip (IParam _ ty) = Left (tyVarsOfType ty)
+    get_ip other         = Right (tyVarsOfPred other)
 \end{code}
     
 %************************************************************************
@@ -276,7 +293,7 @@ improveOne inst_env pred@(ClassP cls tys, _) preds
        , let rough_fd_tcs = trimRoughMatchTcs cls_tvs fd rough_tcs
        , ispec@(Instance { is_tvs = qtvs, is_tys = tys_inst, 
                            is_tcs = mb_tcs_inst }) <- instances
-       , not (instanceCantMatch mb_tcs_inst rough_tcs)
+       , not (instanceCantMatch mb_tcs_inst rough_fd_tcs)
        , eqn <- checkClsFD qtvs fd cls_tvs tys_inst tys
        , let p_inst = (mkClassPred cls tys_inst, 
                        ptext SLIT("arising from the instance declaration at")