Cover PredTy case in Type.tyFamInsts
[ghc-hetmet.git] / compiler / types / FunDeps.lhs
index 30549dd..7f9f050 100644 (file)
@@ -20,13 +20,16 @@ module FunDeps (
 import Name
 import Var
 import Class
-import TcGadt
 import TcType
+import Unify
 import InstEnv
 import VarSet
 import VarEnv
 import Outputable
 import Util
+import FastString
+
+import Data.List       ( nubBy )
 import Data.Maybe      ( isJust )
 \end{code}
 
@@ -76,21 +79,29 @@ uniform thing is to return {t}.
 
 However, consider
        class D a b c | b->c
-       f x = e   -- Generates constraint (D s Int t)
+       f x = e   -- 'e' generates constraint (D s Int t)
                  -- \x.e has type s->s
 Then, if (oclose (D s Int t) {}) = {t}, we'll make the function
 monomorphic in 't', thus
        f :: forall s. D s Int t => s -> s
 
-But if this function is never called, t will never be instantiated;
-the functional dependencies that fix t may well be instance decls in
+But if this function is never called, 't' will never be instantiated;
+the functional dependencies that fix 't' may well be instance decls in
 some importing module.  But the top-level defaulting of unconstrained
-type variales will fix t=GHC.Prim.Any, and that's simply a bug.
+type variables will fix t=GHC.Prim.Any, and that's simply a bug.
 
 Conclusion: oclose only returns a type variable as "fixed" if it 
 depends on at least one type variable in the input fixed_tvs.
 
 Remember, it's always sound for oclose to return a smaller set.
+An interesting example is tcfail093, where we get this inferred type:
+    class C a b | a->b
+    dup :: forall h. (Call (IO Int) h) => () -> Int -> h
+This is perhaps a bit silly, because 'h' is fixed by the (IO Int);
+previously GHC rejected this saying 'no instance for Call (IO Int) h'.
+But it's right on the borderline. If there was an extra, otherwise
+uninvolved type variable, like 's' in the type of 'f' above, then
+we must accept the function.  So, for now anyway, we accept 'dup' too.
 
 \begin{code}
 oclose :: [PredType] -> TyVarSet -> TyVarSet
@@ -138,7 +149,7 @@ See also Note [Ambiguity] in TcSimplify
 \begin{code}
 grow :: [PredType] -> TyVarSet -> TyVarSet
 grow preds fixed_tvs 
-  | null preds = real_fixed_tvs
+  | null preds = fixed_tvs
   | otherwise  = loop real_fixed_tvs
   where
        -- Add the implicit parameters; 
@@ -189,9 +200,10 @@ type Equation = (TyVarSet, [(Type, Type)])
 -- We usually act on an equation by instantiating the quantified type varaibles
 -- to fresh type variables, and then calling the standard unifier.
 
+pprEquation :: Equation -> SDoc
 pprEquation (qtvs, pairs) 
-  = vcat [ptext SLIT("forall") <+> braces (pprWithCommas ppr (varSetElems qtvs)),
-         nest 2 (vcat [ ppr t1 <+> ptext SLIT(":=:") <+> ppr t2 | (t1,t2) <- pairs])]
+  = vcat [ptext (sLit "forall") <+> braces (pprWithCommas ppr (varSetElems qtvs)),
+         nest 2 (vcat [ ppr t1 <+> ptext (sLit ":=:") <+> ppr t2 | (t1,t2) <- pairs])]
 \end{code}
 
 Given a bunch of predicates that must hold, such as
@@ -235,7 +247,7 @@ improveOne :: (Class -> [Instance])         -- Gives instances for given class
                                                -- combined (for error messages)
 -- Just do improvement triggered by a single, distinguised predicate
 
-improveOne inst_env pred@(IParam ip ty, _) preds
+improveOne _inst_env pred@(IParam ip ty, _) preds
   = [ ((emptyVarSet, [(ty,ty2)]), pred, p2) 
     | p2@(IParam ip2 ty2, _) <- preds
     , ip==ip2
@@ -282,11 +294,11 @@ improveOne inst_env pred@(ClassP cls tys, _) preds
        , not (instanceCantMatch inst_tcs trimmed_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")
+                       ptext (sLit "arising from the instance declaration at")
                        <+> ppr (getSrcLoc ispec))
        ]
 
-improveOne inst_env eq_pred preds
+improveOne _ _ _
   = []
 
 
@@ -458,7 +470,8 @@ badFunDeps :: [Instance] -> Class
           -> TyVarSet -> [Type]        -- Proposed new instance type
           -> [Instance]
 badFunDeps cls_insts clas ins_tv_set ins_tys 
-  = [ ispec | fd <- fds,       -- fds is often empty
+  = nubBy eq_inst $
+    [ ispec | fd <- fds,       -- fds is often empty, so do this first!
              let trimmed_tcs = trimRoughMatchTcs clas_tvs fd rough_tcs,
              ispec@(Instance { is_tcs = inst_tcs, is_tvs = tvs, 
                                is_tys = tys }) <- cls_insts,
@@ -471,19 +484,26 @@ badFunDeps cls_insts clas ins_tv_set ins_tys
   where
     (clas_tvs, fds) = classTvsFds clas
     rough_tcs = roughMatchTcs ins_tys
+    eq_inst i1 i2 = instanceDFunId i1 == instanceDFunId i2
+       -- An single instance may appear twice in the un-nubbed conflict list
+       -- because it may conflict with more than one fundep.  E.g.
+       --      class C a b c | a -> b, a -> c
+       --      instance C Int Bool Bool
+       --      instance C Int Char Char
+       -- The second instance conflicts with the first by *both* fundeps
 
 trimRoughMatchTcs :: [TyVar] -> FunDep TyVar -> [Maybe Name] -> [Maybe Name]
 -- Computing rough_tcs for a particular fundep
---     class C a b c | a -> b where ... 
+--     class C a b c | a -> b where ...
 -- For each instance .... => C ta tb tc
--- we want to match only on the types ta, tc; so our
+-- we want to match only on the type ta; so our
 -- rough-match thing must similarly be filtered.  
--- Hence, we Nothing-ise the tb type right here
-trimRoughMatchTcs clas_tvs (_,rtvs) mb_tcs
+-- Hence, we Nothing-ise the tb and tc types right here
+trimRoughMatchTcs clas_tvs (ltvs, _) mb_tcs
   = zipWith select clas_tvs mb_tcs
   where
-    select clas_tv mb_tc | clas_tv `elem` rtvs = Nothing
-                        | otherwise           = mb_tc
+    select clas_tv mb_tc | clas_tv `elem` ltvs = mb_tc
+                         | otherwise           = Nothing
 \end{code}