+grow :: [PredType] -> TyVarSet -> TyVarSet
+grow preds fixed_tvs
+ | null preds = fixed_tvs
+ | otherwise = loop fixed_tvs
+ where
+ 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
+
+ extend fixed_tvs pred_tvs
+ | fixed_tvs `intersectsVarSet` pred_tvs = fixed_tvs `unionVarSet` pred_tvs
+ | otherwise = fixed_tvs
+
+ pred_sets = [tyVarsOfPred pred | pred <- preds]
+\end{code}
+
+%************************************************************************
+%* *
+\subsection{Generate equations from functional dependencies}
+%* *
+%************************************************************************
+
+
+\begin{code}
+----------
+type Equation = (TyVarSet, Type, Type) -- These two types should be equal, for some
+ -- substitution of the tyvars in the tyvar set
+ -- To "execute" the equation, make fresh type variable for each tyvar in the set,
+ -- instantiate the two types with these fresh variables, and then unify.
+ --
+ -- For example, ({a,b}, (a,Int,b), (Int,z,Bool))
+ -- We unify z with Int, but since a and b are quantified we do nothing to them
+ -- We usually act on an equation by instantiating the quantified type varaibles
+ -- to fresh type variables, and then calling the standard unifier.
+ --
+ -- INVARIANT: they aren't already equal
+ --
+
+
+pprEquationDoc (eqn, doc) = vcat [pprEquation eqn, nest 2 doc]
+
+pprEquation (qtvs, t1, t2) = ptext SLIT("forall") <+> braces (pprWithCommas ppr (varSetElems qtvs))
+ <+> ppr t1 <+> ptext SLIT(":=:") <+> ppr t2
+
+----------
+improve :: InstEnv Id -- Gives instances for given class
+ -> [(PredType,SDoc)] -- Current constraints; doc says where they come from
+ -> [(Equation,SDoc)] -- Derived equalities that must also hold
+ -- (NB the above INVARIANT for type Equation)
+ -- The SDoc explains why the equation holds (for error messages)
+
+type InstEnv a = Class -> [(TyVarSet, [Type], a)]
+-- This is a bit clumsy, because InstEnv is really
+-- defined in module InstEnv. However, we don't want
+-- to define it (and ClsInstEnv) here because InstEnv
+-- is their home. Nor do we want to make a recursive
+-- module group (InstEnv imports stuff from FunDeps).
+\end{code}
+
+Given a bunch of predicates that must hold, such as
+
+ C Int t1, C Int t2, C Bool t3, ?x::t4, ?x::t5
+
+improve figures out what extra equations must hold.
+For example, if we have
+
+ class C a b | a->b where ...
+
+then improve will return
+
+ [(t1,t2), (t4,t5)]
+
+NOTA BENE:
+
+ * improve does not iterate. It's possible that when we make
+ t1=t2, for example, that will in turn trigger a new equation.
+ This would happen if we also had
+ C t1 t7, C t2 t8
+ If t1=t2, we also get t7=t8.
+
+ improve does *not* do this extra step. It relies on the caller
+ doing so.
+
+ * The equations unify types that are not already equal. So there
+ is no effect iff the result of improve is empty
+
+
+
+\begin{code}
+improve inst_env preds
+ = [ eqn | group <- equivClassesByUniq (predTyUnique . fst) preds,
+ eqn <- checkGroup inst_env group ]
+
+----------
+checkGroup :: InstEnv Id -> [(PredType,SDoc)] -> [(Equation, SDoc)]
+ -- The preds are all for the same class or implicit param
+
+checkGroup inst_env (p1@(IParam _ ty, _) : ips)
+ = -- For implicit parameters, all the types must match
+ [ ((emptyVarSet, ty, ty'), mkEqnMsg p1 p2)
+ | p2@(IParam _ ty', _) <- ips, not (ty `tcEqType` ty')]
+
+checkGroup inst_env clss@((ClassP cls _, _) : _)
+ = -- For classes life is more complicated
+ -- Suppose the class is like
+ -- classs C as | (l1 -> r1), (l2 -> r2), ... where ...
+ -- Then FOR EACH PAIR (ClassP c tys1, ClassP c tys2) in the list clss
+ -- we check whether
+ -- U l1[tys1/as] = U l2[tys2/as]
+ -- (where U is a unifier)
+ --
+ -- If so, we return the pair
+ -- U r1[tys1/as] = U l2[tys2/as]
+ --
+ -- We need to do something very similar comparing each predicate
+ -- with relevant instance decls
+ pairwise_eqns ++ instance_eqns
+
+ where
+ (cls_tvs, cls_fds) = classTvsFds cls
+ cls_inst_env = inst_env cls
+
+ -- NOTE that we iterate over the fds first; they are typically
+ -- empty, which aborts the rest of the loop.
+ pairwise_eqns :: [(Equation,SDoc)]
+ pairwise_eqns -- This group comes from pairwise comparison
+ = [ (eqn, mkEqnMsg p1 p2)
+ | fd <- cls_fds,
+ p1@(ClassP _ tys1, _) : rest <- tails clss,
+ p2@(ClassP _ tys2, _) <- rest,
+ eqn <- checkClsFD emptyVarSet fd cls_tvs tys1 tys2
+ ]
+
+ instance_eqns :: [(Equation,SDoc)]
+ instance_eqns -- This group comes from comparing with instance decls
+ = [ (eqn, mkEqnMsg p1 p2)
+ | fd <- cls_fds,
+ (qtvs, tys1, dfun_id) <- cls_inst_env,
+ let p1 = (mkClassPred cls tys1,
+ ptext SLIT("arising from the instance declaration at") <+> ppr (getSrcLoc dfun_id)),
+ p2@(ClassP _ tys2, _) <- clss,
+ eqn <- checkClsFD qtvs fd cls_tvs tys1 tys2
+ ]
+
+mkEqnMsg (pred1,from1) (pred2,from2)
+ = vcat [ptext SLIT("When using functional dependencies to combine"),
+ nest 2 (sep [ppr pred1 <> comma, nest 2 from1]),
+ nest 2 (sep [ppr pred2 <> comma, nest 2 from2])]
+
+----------
+checkClsFD :: TyVarSet -- Quantified type variables; see note below
+ -> FunDep TyVar -> [TyVar] -- One functional dependency from the class
+ -> [Type] -> [Type]
+ -> [Equation]
+
+checkClsFD qtvs fd clas_tvs tys1 tys2
+-- 'qtvs' are the quantified type variables, the ones which an be instantiated
+-- to make the types match. For example, given
+-- class C a b | a->b where ...
+-- instance C (Maybe x) (Tree x) where ..