fix haddock submodule pointer
[ghc-hetmet.git] / compiler / typecheck / TcCanonical.lhs
index 42c2515..66a3738 100644 (file)
@@ -1,33 +1,38 @@
 \begin{code}
 module TcCanonical(
-    mkCanonical, mkCanonicals, canWanteds, canGivens, canOccursCheck, 
-    canEq, canEqLeafTyVarLeft 
+    mkCanonical, mkCanonicals, mkCanonicalFEV, mkCanonicalFEVs, canWanteds, canGivens,
+    canOccursCheck, canEqToWorkList,
+    rewriteWithFunDeps, mkCanonicalFDAsDerived, mkCanonicalFDAsWanted
  ) where
 
 #include "HsVersions.h"
 
-import BasicTypes 
-import Type
+import BasicTypes
+import Id      ( evVarPred )
+import TcErrors
 import TcRnTypes
-
+import FunDeps
+import qualified TcMType as TcM
 import TcType
-import TcErrors
+import Type
 import Coercion
 import Class
 import TyCon
 import TypeRep
 import Name
 import Var
+import VarEnv          ( TidyEnv )
 import Outputable
-import Control.Monad    ( when, zipWithM )
+import Control.Monad    ( unless, when, zipWithM, zipWithM_, foldM )
 import MonadUtils
 import Control.Applicative ( (<|>) )
 
 import VarSet
 import Bag
 
-import Control.Monad  ( unless )
-import TcSMonad  -- The TcS Monad 
+import HsBinds
+import TcSMonad
+import FastString
 \end{code}
 
 Note [Canonicalisation]
@@ -77,15 +82,6 @@ NB: Flattening Skolems only occur in canonical constraints, which
 are never zonked, so we don't need to worry about zonking doing
 accidental unflattening.
 
-NB: Note that (unlike the OutsideIn(X) draft of 7 May 2010) we are
-actually doing the SAME thing here no matter whether we are flattening
-a wanted or a given constraint.  In both cases we simply generate some
-flattening skolem variables and some extra given constraints; we never
-generate actual unification variables or non-identity coercions.
-Hopefully this will work, although SPJ had some vague worries about
-unification variables from wanted constraints finding their way into
-the generated given constraints...?
-
 Note that we prefer to leave type synonyms unexpanded when possible,
 so when the flattener encounters one, it first asks whether its
 transitive expansion contains any type function applications.  If so,
@@ -97,106 +93,129 @@ expansions contain any type function applications would speed things
 up a bit; right now we waste a lot of energy traversing the same types
 multiple times.
 
+
 \begin{code}
+
 -- Flatten a bunch of types all at once.
-flattenMany :: CtFlavor -> [Type] -> TcS ([Xi], CanonicalCts)
+flattenMany :: CtFlavor -> [Type] -> TcS ([Xi], [Coercion], CanonicalCts)
+-- Coercions :: Xi ~ Type 
 flattenMany ctxt tys 
-  = do { (xis, cts_s) <- mapAndUnzipM (flatten ctxt) tys
-       ; return (xis, andCCans cts_s) }
+  = do { (xis, cos, cts_s) <- mapAndUnzip3M (flatten ctxt) tys
+       ; return (xis, cos, andCCans cts_s) }
 
 -- Flatten a type to get rid of type function applications, returning
 -- the new type-function-free type, and a collection of new equality
--- constraints.  See Note [Flattening] for more detail.  This needs to
--- be in the TcS monad so we can generate new flattening skolem
--- variables.
-flatten :: CtFlavor -> TcType -> TcS (Xi, CanonicalCts)
-
+-- constraints.  See Note [Flattening] for more detail.
+flatten :: CtFlavor -> TcType -> TcS (Xi, Coercion, CanonicalCts)
+-- Postcondition: Coercion :: Xi ~ TcType 
 flatten ctxt ty 
   | Just ty' <- tcView ty
-  = do { (xi, ccs) <- flatten ctxt ty'
+  = do { (xi, co, ccs) <- flatten ctxt ty'
        -- Preserve type synonyms if possible
-       -- We can tell if t' is function-free by
+       -- We can tell if ty' is function-free by
        -- whether there are any floated constraints
-       ; if isEmptyCCan ccs then
-             return (ty, emptyCCan)  
+        ; if isReflCo co then
+             return (ty, mkReflCo ty, emptyCCan)
          else
-             return (xi, ccs) }
+             return (xi, co, ccs) }
 
 flatten _ v@(TyVarTy _)
-  = return (v, emptyCCan)
+  = return (v, mkReflCo v, emptyCCan)
 
 flatten ctxt (AppTy ty1 ty2)
-  = do { (xi1,c1) <- flatten ctxt ty1
-       ; (xi2,c2) <- flatten ctxt ty2
-       ; return (mkAppTy xi1 xi2, c1 `andCCan` c2) }
+  = do { (xi1,co1,c1) <- flatten ctxt ty1
+       ; (xi2,co2,c2) <- flatten ctxt ty2
+       ; return (mkAppTy xi1 xi2, mkAppCo co1 co2, c1 `andCCan` c2) }
 
 flatten ctxt (FunTy ty1 ty2)
-  = do { (xi1,c1) <- flatten ctxt ty1
-       ; (xi2,c2) <- flatten ctxt ty2
-       ; return (mkFunTy xi1 xi2, c1 `andCCan` c2) }
+  = do { (xi1,co1,c1) <- flatten ctxt ty1
+       ; (xi2,co2,c2) <- flatten ctxt ty2
+       ; return (mkFunTy xi1 xi2, mkFunCo co1 co2, c1 `andCCan` c2) }
 
 flatten fl (TyConApp tc tys)
   -- For a normal type constructor or data family application, we just
   -- recursively flatten the arguments.
   | not (isSynFamilyTyCon tc)
-    = do { (xis,ccs) <- flattenMany fl tys
-         ; return (mkTyConApp tc xis, ccs) }
+    = do { (xis,cos,ccs) <- flattenMany fl tys
+         ; return (mkTyConApp tc xis, mkTyConAppCo tc cos, ccs) }
 
   -- Otherwise, it's a type function application, and we have to
   -- flatten it away as well, and generate a new given equality constraint
   -- between the application and a newly generated flattening skolem variable.
   | otherwise
-    = ASSERT( tyConArity tc <= length tys )    -- Type functions are saturated
-      do { (xis, ccs) <- flattenMany fl tys
-         ; let (xi_args, xi_rest) = splitAt (tyConArity tc) xis
+  = ASSERT( tyConArity tc <= length tys )      -- Type functions are saturated
+      do { (xis, cos, ccs) <- flattenMany fl tys
+         ; let (xi_args, xi_rest)  = splitAt (tyConArity tc) xis
+               (cos_args, cos_rest) = splitAt (tyConArity tc) cos 
                 -- The type function might be *over* saturated
                 -- in which case the remaining arguments should
                 -- be dealt with by AppTys
-               fam_ty = mkTyConApp tc xi_args 
-               fam_co = fam_ty -- identity 
-
-         ; xi_skol <- newFlattenSkolemTy fam_ty
-         ; cv <- newGivOrDerCoVar fam_ty xi_skol fam_co 
-
-         ; let ceq_given = CFunEqCan { cc_id     = cv 
-                                     , cc_flavor = mkGivenFlavor fl UnkSkol
-                                     , cc_fun    = tc 
-                                     , cc_tyargs = xi_args 
-                                     , cc_rhs    = xi_skol
-                                     }
-                 -- ceq_given : F xi_args ~ xi_skol
-
-         ; return ( foldl AppTy xi_skol xi_rest
-                  , ccs `extendCCans` ceq_given) }
+               fam_ty = mkTyConApp tc xi_args
+         ; (ret_co, rhs_var, ct) <-
+             do { is_cached <- lookupFlatCacheMap tc xi_args fl 
+                ; case is_cached of 
+                    Just (rhs_var,ret_co,_fl) -> return (ret_co, rhs_var, emptyCCan)
+                    Nothing
+                        | isGivenOrSolved fl ->
+                            do { rhs_var <- newFlattenSkolemTy fam_ty
+                               ; cv <- newGivenCoVar fam_ty rhs_var (mkReflCo fam_ty)
+                               ; let ct = CFunEqCan { cc_id     = cv
+                                                    , cc_flavor = fl -- Given
+                                                    , cc_fun    = tc 
+                                                    , cc_tyargs = xi_args 
+                                                    , cc_rhs    = rhs_var }
+                               ; let ret_co = mkCoVarCo cv 
+                               ; updateFlatCacheMap tc xi_args rhs_var fl ret_co 
+                               ; return $ (ret_co, rhs_var, singleCCan ct) }
+                        | otherwise ->
+                    -- Derived or Wanted: make a new *unification* flatten variable
+                            do { rhs_var <- newFlexiTcSTy (typeKind fam_ty)
+                               ; cv <- newCoVar fam_ty rhs_var
+                               ; let ct = CFunEqCan { cc_id = cv
+                                                    , cc_flavor = mkWantedFlavor fl
+                                                    -- Always Wanted, not Derived
+                                                    , cc_fun = tc
+                                                    , cc_tyargs = xi_args
+                                                    , cc_rhs    = rhs_var }
+                               ; let ret_co = mkCoVarCo cv
+                               ; updateFlatCacheMap tc xi_args rhs_var fl ret_co
+                               ; return $ (ret_co, rhs_var, singleCCan ct) } }
+         ; return ( foldl AppTy rhs_var xi_rest
+                  , foldl AppCo (mkSymCo ret_co 
+                                   `mkTransCo` mkTyConAppCo tc cos_args) 
+                                cos_rest
+                  , ccs `andCCan` ct) }
 
 flatten ctxt (PredTy pred) 
-  = do { (pred',ccs) <- flattenPred ctxt pred
-       ; return (PredTy pred', ccs) }
+  = do { (pred', co, ccs) <- flattenPred ctxt pred
+       ; return (PredTy pred', co, ccs) }
 
 flatten ctxt ty@(ForAllTy {})
 -- We allow for-alls when, but only when, no type function
 -- applications inside the forall involve the bound type variables
+-- TODO: What if it is a (t1 ~ t2) => t3
+--       Must revisit when the New Coercion API is here! 
   = do { let (tvs, rho) = splitForAllTys ty
-       ; (rho', ccs) <- flatten ctxt rho
+       ; (rho', co, ccs) <- flatten ctxt rho
        ; let bad_eqs  = filterBag is_bad ccs
              is_bad c = tyVarsOfCanonical c `intersectsVarSet` tv_set
              tv_set   = mkVarSet tvs
        ; unless (isEmptyBag bad_eqs)
                 (flattenForAllErrorTcS ctxt ty bad_eqs)
-       ; return (mkForAllTys tvs rho', ccs)  }
+       ; return (mkForAllTys tvs rho', foldr mkForAllCo co tvs, ccs)  }
 
 ---------------
-flattenPred :: CtFlavor -> TcPredType -> TcS (TcPredType, CanonicalCts)
+flattenPred :: CtFlavor -> TcPredType -> TcS (TcPredType, Coercion, CanonicalCts)
 flattenPred ctxt (ClassP cls tys)
-  = do { (tys', ccs) <- flattenMany ctxt tys
-       ; return (ClassP cls tys', ccs) }
+  = do { (tys', cos, ccs) <- flattenMany ctxt tys
+       ; return (ClassP cls tys', mkPredCo $ ClassP cls cos, ccs) }
 flattenPred ctxt (IParam nm ty)
-  = do { (ty', ccs) <- flatten ctxt ty
-       ; return (IParam nm ty', ccs) }
+  = do { (ty', co, ccs) <- flatten ctxt ty
+       ; return (IParam nm ty', mkPredCo $ IParam nm co, ccs) }
 flattenPred ctxt (EqPred ty1 ty2)
-  = do { (ty1', ccs1) <- flatten ctxt ty1
-       ; (ty2', ccs2) <- flatten ctxt ty2
-       ; return (EqPred ty1' ty2', ccs1 `andCCan` ccs2) }
+  = do { (ty1', co1, ccs1) <- flatten ctxt ty1
+       ; (ty2', co2, ccs2) <- flatten ctxt ty2
+       ; return (EqPred ty1' ty2', mkPredCo $ EqPred co1 co2, ccs1 `andCCan` ccs2) }
 \end{code}
 
 %************************************************************************
@@ -206,44 +225,187 @@ flattenPred ctxt (EqPred ty1 ty2)
 %************************************************************************
 
 \begin{code}
-canWanteds :: [WantedEvVar] -> TcS CanonicalCts 
-canWanteds = fmap andCCans . mapM (\(WantedEvVar ev loc) -> mkCanonical (Wanted loc) ev)
+canWanteds :: [WantedEvVar] -> TcS WorkList
+canWanteds = fmap unionWorkLists . mapM (\(EvVarX ev loc) -> mkCanonical (Wanted loc) ev)
+
+canGivens :: GivenLoc -> [EvVar] -> TcS WorkList
+canGivens loc givens = do { ccs <- mapM (mkCanonical (Given loc GivenOrig)) givens
+                          ; return (unionWorkLists ccs) }
 
-canGivens :: GivenLoc -> [EvVar] -> TcS CanonicalCts
-canGivens loc givens = do { ccs <- mapM (mkCanonical (Given loc)) givens
-                          ; return (andCCans ccs) }
+mkCanonicals :: CtFlavor -> [EvVar] -> TcS WorkList
+mkCanonicals fl vs = fmap unionWorkLists (mapM (mkCanonical fl) vs)
 
-mkCanonicals :: CtFlavor -> [EvVar] -> TcS CanonicalCts 
-mkCanonicals fl vs = fmap andCCans (mapM (mkCanonical fl) vs)
+mkCanonicalFEV :: FlavoredEvVar -> TcS WorkList
+mkCanonicalFEV (EvVarX ev fl) = mkCanonical fl ev
 
-mkCanonical :: CtFlavor -> EvVar -> TcS CanonicalCts 
+mkCanonicalFEVs :: Bag FlavoredEvVar -> TcS WorkList
+mkCanonicalFEVs = foldrBagM canon_one emptyWorkList
+  where        -- Preserves order (shouldn't be important, but curently
+               --                  is important for the vectoriser)
+    canon_one fev wl = do { wl' <- mkCanonicalFEV fev
+                          ; return (unionWorkList wl' wl) }
+
+
+mkCanonical :: CtFlavor -> EvVar -> TcS WorkList
 mkCanonical fl ev = case evVarPred ev of 
-                        ClassP clas tys -> canClass fl ev clas tys 
-                        IParam ip ty    -> canIP    fl ev ip ty
-                        EqPred ty1 ty2  -> canEq    fl ev ty1 ty2 
+                        ClassP clas tys -> canClassToWorkList fl ev clas tys 
+                        IParam ip ty    -> canIPToWorkList    fl ev ip ty 
+                        EqPred ty1 ty2  -> canEqToWorkList    fl ev ty1 ty2 
                          
 
-canClass :: CtFlavor -> EvVar -> Class -> [TcType] -> TcS CanonicalCts 
-canClass fl v cn tys 
-  = do { (xis,ccs) <- flattenMany fl tys 
-       ; return $ ccs `extendCCans` CDictCan { cc_id = v 
-                                             , cc_flavor = fl 
-                                             , cc_class = cn 
-                                             , cc_tyargs = xis } }
-canIP :: CtFlavor -> EvVar -> IPName Name -> TcType -> TcS CanonicalCts 
-canIP fl v nm ty 
-  = return $ singleCCan $ CIPCan { cc_id = v
-                                 , cc_flavor = fl
-                                 , cc_ip_nm = nm
-                                 , cc_ip_ty = ty } 
+canClassToWorkList :: CtFlavor -> EvVar -> Class -> [TcType] -> TcS WorkList
+canClassToWorkList fl v cn tys 
+  = do { (xis,cos,ccs) <- flattenMany fl tys  -- cos :: xis ~ tys
+       ; let no_flattening_happened = all isReflCo cos
+             dict_co = mkTyConAppCo (classTyCon cn) cos
+       ; v_new <- if no_flattening_happened  then return v
+                  else if isGivenOrSolved fl then return v
+                         -- The cos are all identities if fl=Given,
+                         -- hence nothing to do
+                  else do { v' <- newDictVar cn xis  -- D xis
+                          ; when (isWanted fl) $ setDictBind v  (EvCast v' dict_co)
+                          ; when (isGivenOrSolved fl) $ setDictBind v' (EvCast v (mkSymCo dict_co))
+                                 -- NB: No more setting evidence for derived now 
+                          ; return v' }
+
+       -- Add the superclasses of this one here, See Note [Adding superclasses]. 
+       -- But only if we are not simplifying the LHS of a rule. 
+       ; sctx <- getTcSContext
+       ; sc_cts <- if simplEqsOnly sctx then return emptyWorkList
+                   else newSCWorkFromFlavored v_new fl cn xis
+
+       ; return (sc_cts `unionWorkList` 
+                 workListFromEqs ccs `unionWorkList` 
+                 workListFromNonEq CDictCan { cc_id     = v_new
+                                           , cc_flavor = fl
+                                           , cc_class  = cn 
+                                           , cc_tyargs = xis }) }
+\end{code}
+
+Note [Adding superclasses]
+~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+Since dictionaries are canonicalized only once in their lifetime, the
+place to add their superclasses is canonicalisation (The alternative
+would be to do it during constraint solving, but we'd have to be
+extremely careful to not repeatedly introduced the same superclass in
+our worklist). Here is what we do:
+
+For Givens: 
+       We add all their superclasses as Givens. 
+
+For Wanteds: 
+       Generally speaking we want to be able to add superclasses of 
+       wanteds for two reasons:
+
+       (1) Oportunities for improvement. Example: 
+                  class (a ~ b) => C a b 
+           Wanted constraint is: C alpha beta 
+           We'd like to simply have C alpha alpha. Similar 
+           situations arise in relation to functional dependencies. 
+           
+       (2) To have minimal constraints to quantify over: 
+           For instance, if our wanted constraint is (Eq a, Ord a) 
+           we'd only like to quantify over Ord a. 
+
+       To deal with (1) above we only add the superclasses of wanteds
+       which may lead to improvement, that is: equality superclasses or 
+       superclasses with functional dependencies. 
+
+       We deal with (2) completely independently in TcSimplify. See 
+       Note [Minimize by SuperClasses] in TcSimplify. 
+
+
+       Moreover, in all cases the extra improvement constraints are 
+       Derived. Derived constraints have an identity (for now), but 
+       we don't do anything with their evidence. For instance they 
+       are never used to rewrite other constraints. 
+
+       See also [New Wanted Superclass Work] in TcInteract. 
+
+
+For Deriveds: 
+       We do nothing.
+
+Here's an example that demonstrates why we chose to NOT add
+superclasses during simplification: [Comes from ticket #4497]
+   class Num (RealOf t) => Normed t
+   type family RealOf x
+
+Assume the generated wanted constraint is: 
+   RealOf e ~ e, Normed e 
+If we were to be adding the superclasses during simplification we'd get: 
+   Num uf, Normed e, RealOf e ~ e, RealOf e ~ uf 
+==> 
+   e ~ uf, Num uf, Normed e, RealOf e ~ e 
+==> [Spontaneous solve] 
+   Num uf, Normed uf, RealOf uf ~ uf 
+
+While looks exactly like our original constraint. If we add the superclass again we'd loop. 
+By adding superclasses definitely only once, during canonicalisation, this situation can't 
+happen.
 
+\begin{code}
+
+newSCWorkFromFlavored :: EvVar -> CtFlavor -> Class -> [Xi] -> TcS WorkList
+-- Returns superclasses, see Note [Adding superclasses]
+newSCWorkFromFlavored ev orig_flavor cls xis 
+  | isDerived orig_flavor 
+  = return emptyWorkList  -- Deriveds don't yield more superclasses because we will
+                          -- add them transitively in the case of wanteds. 
+
+  | Just gk <- isGiven_maybe orig_flavor 
+  = case gk of 
+      GivenOrig -> do { let sc_theta = immSuperClasses cls xis 
+                            flavor   = orig_flavor
+                      ; sc_vars <- mapM newEvVar sc_theta
+                      ; _ <- zipWithM_ setEvBind sc_vars [EvSuperClass ev n | n <- [0..]]
+                      ; mkCanonicals flavor sc_vars }
+      GivenSolved -> return emptyWorkList 
+      -- Seems very dangerous to add the superclasses for dictionaries that may be 
+      -- partially solved because we may end up with evidence loops.
+
+  | isEmptyVarSet (tyVarsOfTypes xis)
+  = return emptyWorkList -- Wanteds with no variables yield no deriveds.
+                         -- See Note [Improvement from Ground Wanteds]
+
+  | otherwise -- Wanted case, just add those SC that can lead to improvement. 
+  = do { let sc_rec_theta = transSuperClasses cls xis 
+             impr_theta   = filter is_improvement_pty sc_rec_theta 
+             Wanted wloc  = orig_flavor
+       ; der_ids <- mapM newDerivedId impr_theta
+       ; mkCanonicals (Derived wloc) der_ids }
+
+
+is_improvement_pty :: PredType -> Bool 
+-- Either it's an equality, or has some functional dependency
+is_improvement_pty (EqPred {})      = True 
+is_improvement_pty (ClassP cls _ty) = not $ null fundeps
+ where (_,fundeps,_,_,_,_) = classExtraBigSig cls
+is_improvement_pty _ = False
+
+
+
+
+canIPToWorkList :: CtFlavor -> EvVar -> IPName Name -> TcType -> TcS WorkList
+-- See Note [Canonical implicit parameter constraints] to see why we don't 
+-- immediately canonicalize (flatten) IP constraints. 
+canIPToWorkList fl v nm ty 
+  = return $ workListFromNonEq (CIPCan { cc_id = v
+                                      , cc_flavor = fl
+                                      , cc_ip_nm = nm
+                                      , cc_ip_ty = ty })
 
 -----------------
+canEqToWorkList :: CtFlavor -> EvVar -> Type -> Type -> TcS WorkList
+canEqToWorkList fl cv ty1 ty2 = do { cts <- canEq fl cv ty1 ty2 
+                         ; return $ workListFromEqs cts }
+
 canEq :: CtFlavor -> EvVar -> Type -> Type -> TcS CanonicalCts 
 canEq fl cv ty1 ty2 
-  | tcEqType ty1 ty2   -- Dealing with equality here avoids
+  | eqType ty1 ty2     -- Dealing with equality here avoids
                        -- later spurious occurs checks for a~a
-  = do { when (isWanted fl) (setWantedCoBind cv ty1)
+  = do { when (isWanted fl) (setCoBind cv (mkReflCo ty1))
        ; return emptyCCan }
 
 -- If one side is a variable, orient and flatten, 
@@ -257,98 +419,57 @@ canEq fl cv ty1 ty2@(TyVarTy {})
        ; canEqLeaf untch fl cv (classify ty1) (classify ty2) }
       -- NB: don't use VarCls directly because tv1 or tv2 may be scolems!
 
-canEq fl cv (TyConApp fn tys) ty2 
-  | isSynFamilyTyCon fn, length tys == tyConArity fn
-  = do { untch <- getUntouchables 
-       ; canEqLeaf untch fl cv (FunCls fn tys) (classify ty2) }
-canEq fl cv ty1 (TyConApp fn tys)
-  | isSynFamilyTyCon fn, length tys == tyConArity fn
-  = do { untch <- getUntouchables 
-       ; canEqLeaf untch fl cv (classify ty1) (FunCls fn tys) }
-
-canEq fl cv s1 s2
-  | Just (t1a,t1b,t1c) <- splitCoPredTy_maybe s1, 
-    Just (t2a,t2b,t2c) <- splitCoPredTy_maybe s2
-  = do { (v1,v2,v3) <- if isWanted fl then 
-                         do { v1 <- newWantedCoVar t1a t2a
-                            ; v2 <- newWantedCoVar t1b t2b 
-                            ; v3 <- newWantedCoVar t1c t2c 
-                            ; let res_co = mkCoPredCo (mkCoVarCoercion v1) 
-                                                      (mkCoVarCoercion v2) (mkCoVarCoercion v3)
-                            ; setWantedCoBind cv res_co
-                            ; return (v1,v2,v3) }
-                       else let co_orig = mkCoVarCoercion cv 
-                                coa = mkCsel1Coercion co_orig
-                                cob = mkCsel2Coercion co_orig
-                                coc = mkCselRCoercion co_orig
-                            in do { v1 <- newGivOrDerCoVar t1a t2a coa
-                                  ; v2 <- newGivOrDerCoVar t1b t2b cob
-                                  ; v3 <- newGivOrDerCoVar t1c t2c coc 
-                                  ; return (v1,v2,v3) }
-       ; cc1 <- canEq fl v1 t1a t2a 
-       ; cc2 <- canEq fl v2 t1b t2b 
-       ; cc3 <- canEq fl v3 t1c t2c 
-       ; return (cc1 `andCCan` cc2 `andCCan` cc3) }
-
-
 -- Split up an equality between function types into two equalities.
 canEq fl cv (FunTy s1 t1) (FunTy s2 t2)
   = do { (argv, resv) <- 
              if isWanted fl then 
-                 do { argv <- newWantedCoVar s1 s2 
-                    ; resv <- newWantedCoVar t1 t2 
-                    ; setWantedCoBind cv $ 
-                      mkFunCoercion (mkCoVarCoercion argv) (mkCoVarCoercion resv) 
+                 do { argv <- newCoVar s1 s2 
+                    ; resv <- newCoVar t1 t2 
+                    ; setCoBind cv $ 
+                      mkFunCo (mkCoVarCo argv) (mkCoVarCo resv) 
                     ; return (argv,resv) } 
-             else let [arg,res] = decomposeCo 2 (mkCoVarCoercion cv) 
-                  in do { argv <- newGivOrDerCoVar s1 s2 arg 
-                        ; resv <- newGivOrDerCoVar t1 t2 res
-                        ; return (argv,resv) } 
+             else if isGivenOrSolved fl then 
+                      let [arg,res] = decomposeCo 2 (mkCoVarCo cv) 
+                      in do { argv <- newGivenCoVar s1 s2 arg 
+                            ; resv <- newGivenCoVar t1 t2 res
+                            ; return (argv,resv) } 
+
+             else -- Derived 
+                 do { argv <- newDerivedId (EqPred s1 s2)
+                    ; resv <- newDerivedId (EqPred t1 t2)
+                    ; return (argv,resv) }
+
        ; cc1 <- canEq fl argv s1 s2 -- inherit original kinds and locations
        ; cc2 <- canEq fl resv t1 t2
        ; return (cc1 `andCCan` cc2) }
 
-canEq fl cv (PredTy p1) (PredTy p2) = canEqPred p1 p2 
-  where canEqPred (IParam n1 t1) (IParam n2 t2) 
-          | n1 == n2 
-          = if isWanted fl then 
-                do { v <- newWantedCoVar t1 t2 
-                   ; setWantedCoBind cv $ mkIParamPredCo n1 (mkCoVarCoercion cv)
-                   ; canEq fl v t1 t2 } 
-            else return emptyCCan -- DV: How to decompose given IP coercions? 
-
-        canEqPred (ClassP c1 tys1) (ClassP c2 tys2) 
-          | c1 == c2 
-          = if isWanted fl then 
-               do { vs <- zipWithM newWantedCoVar tys1 tys2 
-                  ; setWantedCoBind cv $ mkClassPPredCo c1 (map mkCoVarCoercion vs) 
-                  ; andCCans <$> zipWith3M (canEq fl) vs tys1 tys2
-                  }
-            else return emptyCCan 
-          -- How to decompose given dictionary (and implicit parameter) coercions? 
-          -- You may think that the following is right: 
-          --    let cos = decomposeCo (length tys1) (mkCoVarCoercion cv) 
-          --    in  zipWith3M newGivOrDerCoVar tys1 tys2 cos
-          -- But this assumes that the coercion is a type constructor-based 
-          -- coercion, and not a PredTy (ClassP cn cos) coercion. So we chose
-          -- to not decompose these coercions. We have to get back to this 
-          -- when we clean up the Coercion API.
-
-        canEqPred p1 p2 = misMatchErrorTcS fl (mkPredTy p1) (mkPredTy p2) 
-
-
-canEq fl cv (TyConApp tc1 tys1) (TyConApp tc2 tys2) 
-  | isAlgTyCon tc1 && isAlgTyCon tc2
+canEq fl cv (TyConApp fn tys) ty2 
+  | isSynFamilyTyCon fn, length tys == tyConArity fn
+  = do { untch <- getUntouchables 
+       ; canEqLeaf untch fl cv (FunCls fn tys) (classify ty2) }
+canEq fl cv ty1 (TyConApp fn tys)
+  | isSynFamilyTyCon fn, length tys == tyConArity fn
+  = do { untch <- getUntouchables 
+       ; canEqLeaf untch fl cv (classify ty1) (FunCls fn tys) }
+
+canEq fl cv (TyConApp tc1 tys1) (TyConApp tc2 tys2)
+  | isDecomposableTyCon tc1 && isDecomposableTyCon tc2
   , tc1 == tc2
   , length tys1 == length tys2
   = -- Generate equalities for each of the corresponding arguments
-    do { argsv <- if isWanted fl then
-                    do { argsv <- zipWithM newWantedCoVar tys1 tys2
-                            ; setWantedCoBind cv $ mkTyConCoercion tc1 (map mkCoVarCoercion argsv)
-                            ; return argsv } 
-                  else 
-                    let cos = decomposeCo (length tys1) (mkCoVarCoercion cv) 
-                    in zipWith3M newGivOrDerCoVar tys1 tys2 cos
+    do { argsv 
+             <- if isWanted fl then
+                    do { argsv <- zipWithM newCoVar tys1 tys2
+                       ; setCoBind cv $ 
+                         mkTyConAppCo tc1 (map mkCoVarCo argsv)
+                       ; return argsv }
+                else if isGivenOrSolved fl then
+                    let cos = decomposeCo (length tys1) (mkCoVarCo cv)
+                    in zipWith3M newGivenCoVar tys1 tys2 cos
+
+                else -- Derived 
+                    zipWithM (\t1 t2 -> newDerivedId (EqPred t1 t2)) tys1 tys2
+
        ; andCCans <$> zipWith3M (canEq fl) argsv tys1 tys2 }
 
 -- See Note [Equality between type applications]
@@ -356,37 +477,40 @@ canEq fl cv (TyConApp tc1 tys1) (TyConApp tc2 tys2)
 canEq fl cv ty1 ty2
   | Just (s1,t1) <- tcSplitAppTy_maybe ty1
   , Just (s2,t2) <- tcSplitAppTy_maybe ty2
-    = do { (cv1,cv2) <- 
-             if isWanted fl 
-             then do { cv1 <- newWantedCoVar s1 s2 
-                     ; cv2 <- newWantedCoVar t1 t2 
-                     ; setWantedCoBind cv $ 
-                       mkAppCoercion (mkCoVarCoercion cv1) (mkCoVarCoercion cv2) 
-                     ; return (cv1,cv2) } 
-             else let co1 = mkLeftCoercion  $ mkCoVarCoercion cv 
-                      co2 = mkRightCoercion $ mkCoVarCoercion cv
-                  in do { cv1 <- newGivOrDerCoVar s1 s2 co1 
-                        ; cv2 <- newGivOrDerCoVar t1 t2 co2 
-                        ; return (cv1,cv2) } 
-         ; cc1 <- canEq fl cv1 s1 s2 
-         ; cc2 <- canEq fl cv2 t1 t2 
-         ; return (cc1 `andCCan` cc2) } 
-
-canEq fl _ s1@(ForAllTy {}) s2@(ForAllTy {})  
+    = if isWanted fl 
+      then do { cv1 <- newCoVar s1 s2 
+              ; cv2 <- newCoVar t1 t2 
+              ; setCoBind cv $ 
+                mkAppCo (mkCoVarCo cv1) (mkCoVarCo cv2) 
+              ; cc1 <- canEq fl cv1 s1 s2 
+              ; cc2 <- canEq fl cv2 t1 t2 
+              ; return (cc1 `andCCan` cc2) } 
+
+      else if isDerived fl 
+      then do { cv1 <- newDerivedId (EqPred s1 s2)
+              ; cv2 <- newDerivedId (EqPred t1 t2)
+              ; cc1 <- canEq fl cv1 s1 s2 
+              ; cc2 <- canEq fl cv2 t1 t2 
+              ; return (cc1 `andCCan` cc2) } 
+      
+      else return emptyCCan    -- We cannot decompose given applications
+                              -- because we no longer have 'left' and 'right'
+
+canEq fl cv s1@(ForAllTy {}) s2@(ForAllTy {})
  | tcIsForAllTy s1, tcIsForAllTy s2, 
    Wanted {} <- fl 
- = misMatchErrorTcS fl s1 s2 
- | otherwise 
+ = canEqFailure fl cv
+ | otherwise
  = do { traceTcS "Ommitting decomposition of given polytype equality" (pprEq s1 s2)
       ; return emptyCCan }
 
 -- Finally expand any type synonym applications.
 canEq fl cv ty1 ty2 | Just ty1' <- tcView ty1 = canEq fl cv ty1' ty2
 canEq fl cv ty1 ty2 | Just ty2' <- tcView ty2 = canEq fl cv ty1 ty2'
-canEq fl _ ty1 ty2 
-  = misMatchErrorTcS fl ty1 ty2
-
+canEq fl cv _ _                               = canEqFailure fl cv
 
+canEqFailure :: CtFlavor -> EvVar -> TcS CanonicalCts
+canEqFailure fl cv = return (singleCCan (mkFrozenError fl cv))
 \end{code}
 
 Note [Equality between type applications]
@@ -498,7 +622,7 @@ inert set is an idempotent subustitution...
 \begin{code}
 data TypeClassifier 
   = FskCls TcTyVar      -- ^ Flatten skolem 
-  | VarCls TcTyVar      -- ^ *Non-flatten-skolem* variable 
+  | VarCls TcTyVar      -- ^ Non-flatten-skolem variable 
   | FunCls TyCon [Type] -- ^ Type function, exactly saturated
   | OtherCls TcType     -- ^ Neither of the above
 
@@ -525,42 +649,42 @@ classify ty                | Just ty' <- tcView ty
                            = OtherCls ty
 
 -- See note [Canonical ordering for equality constraints].
-reOrient :: Untouchables -> TypeClassifier -> TypeClassifier -> Bool   
+reOrient :: CtFlavor -> TypeClassifier -> TypeClassifier -> Bool       
 -- (t1 `reOrient` t2) responds True 
 --   iff we should flip to (t2~t1)
 -- We try to say False if possible, to minimise evidence generation
 --
 -- Postcondition: After re-orienting, first arg is not OTherCls
-reOrient _untch (OtherCls {}) (FunCls {})   = True
-reOrient _untch (OtherCls {}) (FskCls {})   = True
-reOrient _untch (OtherCls {}) (VarCls {})   = True
-reOrient _untch (OtherCls {}) (OtherCls {}) = panic "reOrient"  -- One must be Var/Fun
+reOrient _fl (OtherCls {}) (FunCls {})   = True
+reOrient _fl (OtherCls {}) (FskCls {})   = True
+reOrient _fl (OtherCls {}) (VarCls {})   = True
+reOrient _fl (OtherCls {}) (OtherCls {}) = panic "reOrient"  -- One must be Var/Fun
+
+reOrient _fl (FunCls {})   (VarCls _tv)  = False  
+  -- But consider the following variation: isGiven fl && isMetaTyVar tv
 
-reOrient _untch (FunCls {})   (VarCls tv2)  = isMetaTyVar tv2
   -- See Note [No touchables as FunEq RHS] in TcSMonad
-reOrient _untch (FunCls {}) _               = False             -- Fun/Other on rhs
+reOrient _fl (FunCls {}) _                = False             -- Fun/Other on rhs
 
-reOrient _untch (VarCls tv1) (FunCls {})    = not $ isMetaTyVar tv1
-        -- Put function on the left, *except* if the RHS becomes
-        -- a meta-tyvar; see invariant on CFunEqCan 
-        -- and Note [No touchables as FunEq RHS]
+reOrient _fl (VarCls {}) (FunCls {})      = True 
 
-reOrient _untch (VarCls tv1) (FskCls {})    = not $ isMetaTyVar tv1
-   -- Put flatten-skolems on the left if possible:
-   --   see Note [Loopy Spontaneous Solving, Example 4] in TcInteract
+reOrient _fl (VarCls {}) (FskCls {})      = False
 
-reOrient _untch (VarCls {})  (OtherCls {})  = False
-reOrient _untch (VarCls {})  (VarCls {})    = False
+reOrient _fl (VarCls {})  (OtherCls {})   = False
+reOrient _fl (VarCls tv1)  (VarCls tv2)  
+  | isMetaTyVar tv2 && not (isMetaTyVar tv1) = True 
+  | otherwise                                = False 
+  -- Just for efficiency, see CTyEqCan invariants 
 
-reOrient _untch (FskCls {}) (VarCls tv2)    = isMetaTyVar tv2 
-      -- See Note [Loopy Spontaneous Solving, Example 4] in TcInteract
+reOrient _fl (FskCls {}) (VarCls tv2)     = isMetaTyVar tv2 
+  -- Just for efficiency, see CTyEqCan invariants
 
-reOrient _untch (FskCls {}) (FskCls {})     = False
-reOrient _untch (FskCls {}) (FunCls {})     = True 
-reOrient _untch (FskCls {}) (OtherCls {})   = False 
+reOrient _fl (FskCls {}) (FskCls {})     = False
+reOrient _fl (FskCls {}) (FunCls {})     = True 
+reOrient _fl (FskCls {}) (OtherCls {})   = False 
 
 ------------------
-canEqLeaf :: Untouchables 
+canEqLeaf :: TcsUntouchables 
           -> CtFlavor -> CoVar 
           -> TypeClassifier -> TypeClassifier -> TcS CanonicalCts 
 -- Canonicalizing "leaf" equality constraints which cannot be
@@ -570,19 +694,23 @@ canEqLeaf :: Untouchables
   -- Preconditions: 
   --    * one of the two arguments is not OtherCls
   --    * the two types are not equal (looking through synonyms)
-canEqLeaf untch fl cv cls1 cls2 
+canEqLeaf _untch fl cv cls1 cls2 
   | cls1 `re_orient` cls2
   = do { cv' <- if isWanted fl 
-                then do { cv' <- newWantedCoVar s2 s1 
-                        ; setWantedCoBind cv $ mkSymCoercion (mkCoVarCoercion cv') 
+                then do { cv' <- newCoVar s2 s1 
+                        ; setCoBind cv $ mkSymCo (mkCoVarCo cv') 
                         ; return cv' } 
-                else newGivOrDerCoVar s2 s1 (mkSymCoercion (mkCoVarCoercion cv)) 
+                else if isGivenOrSolved fl then
+                         newGivenCoVar s2 s1 (mkSymCo (mkCoVarCo cv))
+                else -- Derived
+                    newDerivedId (EqPred s2 s1)
        ; canEqLeafOriented fl cv' cls2 s1 }
 
   | otherwise
-  = canEqLeafOriented fl cv cls1 s2
+  = do { traceTcS "canEqLeaf" (ppr (unClassify cls1) $$ ppr (unClassify cls2))
+       ; canEqLeafOriented fl cv cls1 s2 }
   where
-    re_orient = reOrient untch 
+    re_orient = reOrient fl 
     s1 = unClassify cls1  
     s2 = unClassify cls2  
 
@@ -590,49 +718,80 @@ canEqLeaf untch fl cv cls1 cls2
 canEqLeafOriented :: CtFlavor -> CoVar 
                   -> TypeClassifier -> TcType -> TcS CanonicalCts 
 -- First argument is not OtherCls
-canEqLeafOriented fl cv cls1@(FunCls fn tys) s2 
-  | let k1 = kindAppResult (tyConKind fn) tys, 
+canEqLeafOriented fl cv cls1@(FunCls fn tys1) s2         -- cv : F tys1
+  | let k1 = kindAppResult (tyConKind fn) tys1,
     let k2 = typeKind s2, 
-    isGiven fl && not (k1 `compatKind` k2) -- Establish the kind invariant for CFunEqCan
-  = kindErrorTcS fl (unClassify cls1) s2   -- Eagerly fails, see Note [Kind errors] in TcInteract
+    not (k1 `compatKind` k2) -- Establish the kind invariant for CFunEqCan
+  = canEqFailure fl cv
+    -- Eagerly fails, see Note [Kind errors] in TcInteract
+
   | otherwise 
   = ASSERT2( isSynFamilyTyCon fn, ppr (unClassify cls1) )
-    do { (xis1,ccs1) <- flattenMany fl tys -- flatten type function arguments
-       ; (xi2,ccs2)  <- flatten fl s2      -- flatten entire RHS
-       ; let final_cc = CFunEqCan { cc_id     = cv 
-                                  , cc_flavor = fl 
+    do { (xis1,cos1,ccs1) <- flattenMany fl tys1 -- Flatten type function arguments
+                                                 -- cos1 :: xis1 ~ tys1
+       ; (xi2, co2, ccs2) <- flatten fl s2       -- Flatten entire RHS
+                                                 -- co2  :: xi2 ~ s2
+       ; let ccs = ccs1 `andCCan` ccs2
+             no_flattening_happened = all isReflCo (co2:cos1)
+       ; cv_new <- if no_flattening_happened  then return cv
+                   else if isGivenOrSolved fl then return cv
+                   else if isWanted fl then 
+                         do { cv' <- newCoVar (unClassify (FunCls fn xis1)) xi2
+                                 -- cv' : F xis ~ xi2
+                            ; let -- fun_co :: F xis1 ~ F tys1
+                                 fun_co = mkTyConAppCo fn cos1
+                                 -- want_co :: F tys1 ~ s2
+                                 want_co = mkSymCo fun_co
+                                           `mkTransCo` mkCoVarCo cv'
+                                           `mkTransCo` co2
+                            ; setCoBind cv  want_co
+                            ; return cv' }
+                   else -- Derived 
+                       newDerivedId (EqPred (unClassify (FunCls fn xis1)) xi2)
+
+       ; let final_cc = CFunEqCan { cc_id     = cv_new
+                                  , cc_flavor = fl
                                   , cc_fun    = fn
                                   , cc_tyargs = xis1 
                                   , cc_rhs    = xi2 }
-       ; return $ ccs1 `andCCan` ccs2 `extendCCans` final_cc }
+       ; return $ ccs `extendCCans` final_cc }
 
 -- Otherwise, we have a variable on the left, so call canEqLeafTyVarLeft
 canEqLeafOriented fl cv (FskCls tv) s2 
-  = do { (cc,ccs) <- canEqLeafTyVarLeft fl cv tv s2 
-       ; return $ ccs `extendCCans` cc } 
+  = canEqLeafTyVarLeft fl cv tv s2 
 canEqLeafOriented fl cv (VarCls tv) s2 
-  = do { (cc,ccs) <- canEqLeafTyVarLeft fl cv tv s2 
-       ; return $ ccs `extendCCans` cc } 
+  = canEqLeafTyVarLeft fl cv tv s2 
 canEqLeafOriented _ cv (OtherCls ty1) ty2 
   = pprPanic "canEqLeaf" (ppr cv $$ ppr ty1 $$ ppr ty2)
 
-canEqLeafTyVarLeft :: CtFlavor -> CoVar -> TcTyVar -> TcType -> TcS (CanonicalCt, CanonicalCts)
+canEqLeafTyVarLeft :: CtFlavor -> CoVar -> TcTyVar -> TcType -> TcS CanonicalCts
 -- Establish invariants of CTyEqCans 
-canEqLeafTyVarLeft fl cv tv s2 
-  | isGiven fl && not (k1 `compatKind` k2) -- Establish the kind invariant for CTyEqCan
-  = kindErrorTcS fl (mkTyVarTy tv) s2      -- Eagerly fails, see Note [Kind errors] in TcInteract
-
+canEqLeafTyVarLeft fl cv tv s2       -- cv : tv ~ s2
+  | not (k1 `compatKind` k2) -- Establish the kind invariant for CTyEqCan
+  = canEqFailure fl cv
+       -- Eagerly fails, see Note [Kind errors] in TcInteract
   | otherwise
-  = do { (xi2,ccs2) <- flatten fl s2      -- flatten RHS
-       ; xi2' <- canOccursCheck fl tv xi2 -- do an occurs check, and return a possibly 
-                                          -- unfolded version of the RHS, if we had to 
-                                          -- unfold any type synonyms to get rid of tv.
-       ; let final_cc = CTyEqCan { cc_id     = cv 
-                                 , cc_flavor = fl
-                                 , cc_tyvar  = tv
-                                 , cc_rhs    = xi2'
-                                 } 
-       ; return $ (final_cc, ccs2) }
+  = do { (xi2, co, ccs2) <- flatten fl s2  -- Flatten RHS   co : xi2 ~ s2
+       ; mxi2' <- canOccursCheck fl tv xi2 -- Do an occurs check, and return a possibly
+                                           -- unfolded version of the RHS, if we had to 
+                                           -- unfold any type synonyms to get rid of tv.
+       ; case mxi2' of {
+           Nothing   -> canEqFailure fl cv ;
+           Just xi2' ->
+    do { let no_flattening_happened = isReflCo co
+       ; cv_new <- if no_flattening_happened  then return cv
+                   else if isGivenOrSolved fl then return cv
+                   else if isWanted fl then 
+                         do { cv' <- newCoVar (mkTyVarTy tv) xi2'  -- cv' : tv ~ xi2
+                            ; setCoBind cv  (mkCoVarCo cv' `mkTransCo` co)
+                            ; return cv' }
+                   else -- Derived
+                       newDerivedId (EqPred (mkTyVarTy tv) xi2')
+
+       ; return $ ccs2 `extendCCans` CTyEqCan { cc_id     = cv_new
+                                              , cc_flavor = fl
+                                              , cc_tyvar  = tv
+                                              , cc_rhs    = xi2' } } } }
   where
     k1 = tyVarKind tv
     k2 = typeKind s2
@@ -646,10 +805,8 @@ canEqLeafTyVarLeft fl cv tv s2
 -- variable, then the same type is returned.
 --
 -- Precondition: the two types are not equal (looking though synonyms)
-canOccursCheck :: CtFlavor -> TcTyVar -> Xi -> TcS Xi
-canOccursCheck gw tv xi 
-  | Just xi' <- expandAway tv xi = return xi'
-  | otherwise = occursCheckErrorTcS gw tv xi
+canOccursCheck :: CtFlavor -> TcTyVar -> Xi -> TcS (Maybe Xi)
+canOccursCheck _gw tv xi = return (expandAway tv xi)
 \end{code}
 
 @expandAway tv xi@ expands synonyms in xi just enough to get rid of
@@ -678,18 +835,47 @@ expandAway tv t@(TyVarTy tv')
 expandAway tv xi
   | not (tv `elemVarSet` tyVarsOfType xi) = Just xi
 expandAway tv (AppTy ty1 ty2) 
-  = mkAppTy <$> expandAway tv ty1 <*> expandAway tv ty2
+  = do { ty1' <- expandAway tv ty1
+       ; ty2' <- expandAway tv ty2 
+       ; return (mkAppTy ty1' ty2') }
+-- mkAppTy <$> expandAway tv ty1 <*> expandAway tv ty2
 expandAway tv (FunTy ty1 ty2)
-  = mkFunTy <$> expandAway tv ty1 <*> expandAway tv ty2
-expandAway _ (ForAllTy {}) = error "blorg"  -- TODO
-expandAway _ (PredTy {})   = error "flerg"  -- TODO
-
+  = do { ty1' <- expandAway tv ty1 
+       ; ty2' <- expandAway tv ty2 
+       ; return (mkFunTy ty1' ty2') } 
+-- mkFunTy <$> expandAway tv ty1 <*> expandAway tv ty2
+expandAway tv ty@(ForAllTy {}) 
+  = let (tvs,rho) = splitForAllTys ty
+        tvs_knds  = map tyVarKind tvs 
+    in if tv `elemVarSet` tyVarsOfTypes tvs_knds then
+       -- Can't expand away the kinds unless we create 
+       -- fresh variables which we don't want to do at this point.
+           Nothing 
+       else do { rho' <- expandAway tv rho
+               ; return (mkForAllTys tvs rho') }
+expandAway tv (PredTy pred) 
+  = do { pred' <- expandAwayPred tv pred  
+       ; return (PredTy pred') }
 -- For a type constructor application, first try expanding away the
 -- offending variable from the arguments.  If that doesn't work, next
 -- see if the type constructor is a type synonym, and if so, expand
 -- it and try again.
 expandAway tv ty@(TyConApp tc tys)
-    = (mkTyConApp tc <$> mapM (expandAway tv) tys) <|> (tcView ty >>= expandAway tv)
+  = (mkTyConApp tc <$> mapM (expandAway tv) tys) <|> (tcView ty >>= expandAway tv)
+
+expandAwayPred :: TcTyVar -> TcPredType -> Maybe TcPredType 
+expandAwayPred tv (ClassP cls tys) 
+  = do { tys' <- mapM (expandAway tv) tys; return (ClassP cls tys') } 
+expandAwayPred tv (EqPred ty1 ty2)
+  = do { ty1' <- expandAway tv ty1
+       ; ty2' <- expandAway tv ty2 
+       ; return (EqPred ty1' ty2') }
+expandAwayPred tv (IParam nm ty) 
+  = do { ty' <- expandAway tv ty
+       ; return (IParam nm ty') }
+
+                
+
 \end{code}
 
 Note [Type synonyms and canonicalization]
@@ -771,4 +957,107 @@ a.  If this turns out to be impossible, we next try expanding F
 itself, and so on.
 
 
+%************************************************************************
+%*                                                                      *
+%*          Functional dependencies, instantiation of equations
+%*                                                                      *
+%************************************************************************
+
+When we spot an equality arising from a functional dependency,
+we now use that equality (a "wanted") to rewrite the work-item
+constraint right away.  This avoids two dangers
+
+ Danger 1: If we send the original constraint on down the pipeline
+           it may react with an instance declaration, and in delicate
+          situations (when a Given overlaps with an instance) that
+          may produce new insoluble goals: see Trac #4952
+
+ Danger 2: If we don't rewrite the constraint, it may re-react
+           with the same thing later, and produce the same equality
+           again --> termination worries.
+
+To achieve this required some refactoring of FunDeps.lhs (nicer
+now!).  
+
+\begin{code}
+rewriteWithFunDeps :: [Equation]
+                   -> [Xi] 
+                   -> WantedLoc 
+                   -> TcS (Maybe ([Xi], [Coercion], [(EvVar,WantedLoc)])) 
+                                           -- Not quite a WantedEvVar unfortunately
+                                           -- Because our intention could be to make 
+                                           -- it derived at the end of the day
+-- NB: The flavor of the returned EvVars will be decided by the caller
+-- Post: returns no trivial equalities (identities)
+rewriteWithFunDeps eqn_pred_locs xis wloc
+ = do { fd_ev_poss <- mapM (instFunDepEqn wloc) eqn_pred_locs
+      ; let fd_ev_pos :: [(Int,(EvVar,WantedLoc))]
+            fd_ev_pos = concat fd_ev_poss
+            (rewritten_xis, cos) = unzip (rewriteDictParams fd_ev_pos xis)
+      ; if null fd_ev_pos then return Nothing
+        else return (Just (rewritten_xis, cos, map snd fd_ev_pos)) }
+
+instFunDepEqn :: WantedLoc -> Equation -> TcS [(Int,(EvVar,WantedLoc))]
+-- Post: Returns the position index as well as the corresponding FunDep equality
+instFunDepEqn wl (FDEqn { fd_qtvs = qtvs, fd_eqs = eqs
+                        , fd_pred1 = d1, fd_pred2 = d2 })
+  = do { let tvs = varSetElems qtvs
+       ; tvs' <- mapM instFlexiTcS tvs
+       ; let subst = zipTopTvSubst tvs (mkTyVarTys tvs')
+       ; foldM (do_one subst) [] eqs }
+  where 
+    do_one subst ievs (FDEq { fd_pos = i, fd_ty_left = ty1, fd_ty_right = ty2 })
+       = let sty1 = Type.substTy subst ty1 
+             sty2 = Type.substTy subst ty2 
+         in if eqType sty1 sty2 then return ievs -- Return no trivial equalities
+            else do { ev <- newCoVar sty1 sty2
+                    ; let wl' = push_ctx wl 
+                    ; return $ (i,(ev,wl')):ievs }
+
+    push_ctx :: WantedLoc -> WantedLoc 
+    push_ctx loc = pushErrCtxt FunDepOrigin (False, mkEqnMsg d1 d2) loc
+
+mkEqnMsg :: (TcPredType, SDoc) 
+         -> (TcPredType, SDoc) -> TidyEnv -> TcM (TidyEnv, SDoc)
+mkEqnMsg (pred1,from1) (pred2,from2) tidy_env
+  = do  { zpred1 <- TcM.zonkTcPredType pred1
+        ; zpred2 <- TcM.zonkTcPredType pred2
+       ; let { tpred1 = tidyPred tidy_env zpred1
+              ; tpred2 = tidyPred tidy_env zpred2 }
+       ; let msg = vcat [ptext (sLit "When using functional dependencies to combine"),
+                         nest 2 (sep [ppr tpred1 <> comma, nest 2 from1]), 
+                         nest 2 (sep [ppr tpred2 <> comma, nest 2 from2])]
+       ; return (tidy_env, msg) }
+
+rewriteDictParams :: [(Int,(EvVar,WantedLoc))] -- A set of coercions : (pos, ty' ~ ty)
+                  -> [Type]                    -- A sequence of types: tys
+                  -> [(Type,Coercion)]         -- Returns: [(ty', co : ty' ~ ty)]
+rewriteDictParams param_eqs tys
+  = zipWith do_one tys [0..]
+  where
+    do_one :: Type -> Int -> (Type,Coercion)
+    do_one ty n = case lookup n param_eqs of
+                    Just wev -> (get_fst_ty wev, mkCoVarCo (fst wev))
+                    Nothing  -> (ty,             mkReflCo ty)  -- Identity
+
+    get_fst_ty (wev,_wloc) 
+      | EqPred ty1 _ <- evVarPred wev 
+      = ty1 
+      | otherwise 
+      = panic "rewriteDictParams: non equality fundep!?"
+
+mkCanonicalFDAsWanted :: [(EvVar,WantedLoc)] -> TcS WorkList
+mkCanonicalFDAsWanted evlocs
+  = do { ws <- mapM can_as_wanted evlocs
+       ; return (unionWorkLists ws) }
+  where can_as_wanted (ev,loc) = mkCanonicalFEV (EvVarX ev (Wanted loc))
+
+
+mkCanonicalFDAsDerived :: [(EvVar,WantedLoc)] -> TcS WorkList
+mkCanonicalFDAsDerived evlocs
+  = do { ws <- mapM can_as_derived evlocs
+       ; return (unionWorkLists ws) }
+  where can_as_derived (ev,loc) = mkCanonicalFEV (EvVarX ev (Derived loc)) 
+
 
+\end{code}
\ No newline at end of file