    {-# LANGUAGE CPP #-}
    {-# LANGUAGE MultiWayIf #-}

    module GHC.Tc.Solver.Equality(
         solveEquality
      ) where

    import GHC.Prelude

    import GHC.Tc.Solver.Irred( solveIrred )
    import GHC.Tc.Solver.Dict( matchLocalInst, chooseInstance )
    import GHC.Tc.Solver.Rewrite
    import GHC.Tc.Solver.Monad
    import GHC.Tc.Solver.InertSet
    import GHC.Tc.Solver.Types( findFunEqsByTyCon )
    import GHC.Tc.Types.Evidence
    import GHC.Tc.Types.Constraint
    import GHC.Tc.Types.Origin
    import GHC.Tc.Utils.Unify
    import GHC.Tc.Utils.TcType
    import GHC.Tc.Instance.Family ( tcTopNormaliseNewTypeTF_maybe )
    import GHC.Tc.Instance.FunDeps( FunDepEqn(..) )
    import qualified GHC.Tc.Utils.Monad    as TcM

    import GHC.Core.Type
    import GHC.Core.Predicate
    import GHC.Core.Class
    import GHC.Core.DataCon ( dataConName )
    import GHC.Core.TyCon
    import GHC.Core.TyCo.Rep   -- cleverly decomposes types, good for completeness checking
    import GHC.Core.Coercion
    import GHC.Core.Coercion.Axiom
    import GHC.Core.Reduction
    import GHC.Core.Unify( tcUnifyTyWithTFs )
    import GHC.Core.FamInstEnv ( FamInstEnvs, FamInst(..), apartnessCheck
                               , lookupFamInstEnvByTyCon )
    import GHC.Core

    import GHC.Types.Var
    import GHC.Types.Var.Env
    import GHC.Types.Var.Set( anyVarSet )
    import GHC.Types.Name.Reader
    import GHC.Types.Basic

    import GHC.Builtin.Types ( anyTypeOfKind )

    import GHC.Utils.Outputable
    import GHC.Utils.Panic
    import GHC.Utils.Misc
    import GHC.Utils.Monad
    import GHC.Utils.Constants( debugIsOn )

    import GHC.Data.Pair
    import GHC.Data.Bag
    import Control.Monad
    import Data.Maybe ( isJust, isNothing )
    import Data.List  ( zip4 )

    import qualified Data.Semigroup as S
    import Data.Bifunctor ( bimap )
    import Data.Void( Void )

    {- *********************************************************************
    *                                                                      *
    *        Equalities
    *                                                                      *
    ************************************************************************

    Note [Canonicalising equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In order to canonicalise an equality, we look at the structure of the
    two types at hand, looking for similarities. A difficulty is that the
    types may look dissimilar before rewriting but similar after rewriting.
    However, we don't just want to jump in and rewrite right away, because
    this might be wasted effort. So, after looking for similarities and failing,
    we rewrite and then try again. Of course, we don't want to loop, so we
    track whether or not we've already rewritten.

    It is conceivable to do a better job at tracking whether or not a type
    is rewritten, but this is left as future work. (Mar '15)

    Note [Decomposing FunTy]
    ~~~~~~~~~~~~~~~~~~~~~~~~
    can_eq_nc' may attempt to decompose a FunTy that is un-zonked.  This
    means that we may very well have a FunTy containing a type of some
    unknown kind. For instance, we may have,

        FunTy (a :: k) Int

    Where k is a unification variable. So the calls to splitRuntimeRep_maybe may
    fail (returning Nothing).  In that case we'll fall through, zonk, and try again.
    Zonking should fill the variable k, meaning that decomposition will succeed the
    second time around.

    Also note that we require the FunTyFlag to match.  This will stop
    us decomposing
       (Int -> Bool)  ~  (Show a => blah)
    It's as if we treat (->) and (=>) as different type constructors, which
    indeed they are!
    -}

    solveEquality :: CtEvidence -> EqRel -> Type -> Type
                  -> SolverStage Void
    solveEquality :: CtEvidence -> EqRel -> Type -> Type -> SolverStage Void
    solveEquality CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    ty2
      = do { Pair ty1' ty2' <- CtEvidence -> EqRel -> Type -> Type -> SolverStage TypeEqn
    zonkEqTypes CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    ty2
           ; let ev' | Bool
    debugIsOn = HasDebugCallStack => CtEvidence -> Type -> CtEvidence
    CtEvidence -> Type -> CtEvidence
    setCtEvPredType CtEvidence
    ev (Type -> CtEvidence) -> Type -> CtEvidence
    forall a b. (a -> b) -> a -> b
    $
                                   Role -> Type -> Type -> Type
    mkPrimEqPredRole (EqRel -> Role
    eqRelRole EqRel
    eq_rel) Type
    ty1' Type
    ty2'
                     | Bool
    otherwise = CtEvidence
    ev
                       -- ev': satisfy the precondition of can_eq_nc

           ; mb_canon <- canonicaliseEquality ev' eq_rel ty1' ty2'

           ; case mb_canon of {

                -- An IrredCt equality may be insoluble; but maybe not!
                -- E.g.  m a ~R# m b  is not canonical, but may be
                --       solved by a quantified constraint (T15290)
                -- See Note [Looking up primitive equalities in quantified constraints]
                Left IrredCt
    irred_ct -> do { IrredCt -> SolverStage ()
    tryQCsIrredEqCt IrredCt
    irred_ct
                                    ; IrredCt -> SolverStage Void
    solveIrred IrredCt
    irred_ct } ;

                Right EqCt
    eq_ct   -> do { EqCt -> SolverStage ()
    tryInertEqs EqCt
    eq_ct
                                    ; EqCt -> SolverStage ()
    tryFunDeps  EqCt
    eq_ct
                                    ; EqCt -> SolverStage ()
    tryQCsEqCt  EqCt
    eq_ct
                                    ; TcS () -> SolverStage ()
    forall a. TcS a -> SolverStage a
    simpleStage (EqCt -> TcS ()
    updInertEqs EqCt
    eq_ct)
                                    ; CtEvidence -> String -> SolverStage Void
    forall a. CtEvidence -> String -> SolverStage a
    stopWithStage (EqCt -> CtEvidence
    eqCtEvidence EqCt
    eq_ct) String
    "Kept inert EqCt" } } }

    updInertEqs :: EqCt -> TcS ()
    updInertEqs :: EqCt -> TcS ()
    updInertEqs EqCt
    eq_ct
      = do { KickOutSpec -> CtFlavourRole -> TcS ()
    kickOutRewritable (CanEqLHS -> KickOutSpec
    KOAfterAdding (EqCt -> CanEqLHS
    eqCtLHS EqCt
    eq_ct)) (EqCt -> CtFlavourRole
    eqCtFlavourRole EqCt
    eq_ct)
           ; tc_lvl <- TcS TcLevel
    getTcLevel
           ; updInertCans (addEqToCans tc_lvl eq_ct) }

    {- *********************************************************************
    *                                                                      *
    *           zonkEqTypes
    *                                                                      *
    ********************************************************************* -}

    ---------------------------------
    -- | Compare types for equality, while zonking as necessary. Gives up
    -- as soon as it finds that two types are not equal.
    -- This is quite handy when some unification has made two
    -- types in an inert Wanted to be equal. We can discover the equality without
    -- rewriting, which is sometimes very expensive (in the case of type functions).
    -- In particular, this function makes a ~20% improvement in test case
    -- perf/compiler/T5030.
    --
    -- Returns either the (partially zonked) types in the case of
    -- inequality, or the one type in the case of equality. canEqReflexive is
    -- a good next step in the 'Right' case. Returning 'Left' is always safe.
    --
    -- NB: This does *not* look through type synonyms. In fact, it treats type
    -- synonyms as rigid constructors. In the future, it might be convenient
    -- to look at only those arguments of type synonyms that actually appear
    -- in the synonym RHS. But we're not there yet.
    zonkEqTypes :: CtEvidence -> EqRel -> TcType -> TcType -> SolverStage (Pair TcType)
    zonkEqTypes :: CtEvidence -> EqRel -> Type -> Type -> SolverStage TypeEqn
    zonkEqTypes CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    ty2
      = TcS (StopOrContinue TypeEqn) -> SolverStage TypeEqn
    forall a. TcS (StopOrContinue a) -> SolverStage a
    Stage (TcS (StopOrContinue TypeEqn) -> SolverStage TypeEqn)
    -> TcS (StopOrContinue TypeEqn) -> SolverStage TypeEqn
    forall a b. (a -> b) -> a -> b
    $ do { res <- Type -> Type -> TcS (Either TypeEqn Type)
    go Type
    ty1 Type
    ty2
                   ; case res of
                        Left TypeEqn
    pair -> TypeEqn -> TcS (StopOrContinue TypeEqn)
    forall a. a -> TcS (StopOrContinue a)
    continueWith TypeEqn
    pair
                        Right Type
    ty  -> CtEvidence -> EqRel -> Type -> TcS (StopOrContinue TypeEqn)
    forall a. CtEvidence -> EqRel -> Type -> TcS (StopOrContinue a)
    canEqReflexive CtEvidence
    ev EqRel
    eq_rel Type
    ty }
      where
        go :: TcType -> TcType -> TcS (Either (Pair TcType) TcType)
        go :: Type -> Type -> TcS (Either TypeEqn Type)
    go (TyVarTy TyVar
    tv1) (TyVarTy TyVar
    tv2) = TyVar -> TyVar -> TcS (Either TypeEqn Type)
    tyvar_tyvar TyVar
    tv1 TyVar
    tv2
        go (TyVarTy TyVar
    tv1) Type
    ty2           = SwapFlag -> TyVar -> Type -> TcS (Either TypeEqn Type)
    tyvar SwapFlag
    NotSwapped TyVar
    tv1 Type
    ty2
        go Type
    ty1 (TyVarTy TyVar
    tv2)           = SwapFlag -> TyVar -> Type -> TcS (Either TypeEqn Type)
    tyvar SwapFlag
    IsSwapped  TyVar
    tv2 Type
    ty1

        -- We handle FunTys explicitly here despite the fact that they could also be
        -- treated as an application. Why? Well, for one it's cheaper to just look
        -- at two types (the argument and result types) than four (the argument,
        -- result, and their RuntimeReps). Also, we haven't completely zonked yet,
        -- so we may run into an unzonked type variable while trying to compute the
        -- RuntimeReps of the argument and result types. This can be observed in
        -- testcase tc269.
        go (FunTy FunTyFlag
    af1 Type
    w1 Type
    arg1 Type
    res1) (FunTy FunTyFlag
    af2 Type
    w2 Type
    arg2 Type
    res2)
          | FunTyFlag
    af1 FunTyFlag -> FunTyFlag -> Bool
    forall a. Eq a => a -> a -> Bool
    == FunTyFlag
    af2
          , Type -> Type -> Bool
    eqType Type
    w1 Type
    w2
          = do { res_a <- Type -> Type -> TcS (Either TypeEqn Type)
    go Type
    arg1 Type
    arg2
               ; res_b <- go res1 res2
               ; return $ combine_rev (FunTy af1 w1) res_b res_a }

        go ty1 :: Type
    ty1@(FunTy {}) Type
    ty2 = Type -> Type -> TcS (Either TypeEqn Type)
    forall {m :: * -> *} {a} {b}.
    Monad m =>
    a -> a -> m (Either (Pair a) b)
    bale_out Type
    ty1 Type
    ty2
        go Type
    ty1 ty2 :: Type
    ty2@(FunTy {}) = Type -> Type -> TcS (Either TypeEqn Type)
    forall {m :: * -> *} {a} {b}.
    Monad m =>
    a -> a -> m (Either (Pair a) b)
    bale_out Type
    ty1 Type
    ty2

        go Type
    ty1 Type
    ty2
          | Just (TyCon
    tc1, [Type]
    tys1) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
    Type -> Maybe (TyCon, [Type])
    splitTyConAppNoView_maybe Type
    ty1
          , Just (TyCon
    tc2, [Type]
    tys2) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
    Type -> Maybe (TyCon, [Type])
    splitTyConAppNoView_maybe Type
    ty2
          = if TyCon
    tc1 TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyCon
    tc2 Bool -> Bool -> Bool
    && [Type]
    tys1 [Type] -> [Type] -> Bool
    forall a b. [a] -> [b] -> Bool
    `equalLength` [Type]
    tys2
              -- Crucial to check for equal-length args, because
              -- we cannot assume that the two args to 'go' have
              -- the same kind.  E.g go (Proxy *      (Maybe Int))
              --                        (Proxy (*->*) Maybe)
              -- We'll call (go (Maybe Int) Maybe)
              -- See #13083
            then TyCon -> [Type] -> [Type] -> TcS (Either TypeEqn Type)
    tycon TyCon
    tc1 [Type]
    tys1 [Type]
    tys2
            else Type -> Type -> TcS (Either TypeEqn Type)
    forall {m :: * -> *} {a} {b}.
    Monad m =>
    a -> a -> m (Either (Pair a) b)
    bale_out Type
    ty1 Type
    ty2

        go Type
    ty1 Type
    ty2
          | Just (Type
    ty1a, Type
    ty1b) <- Type -> Maybe (Type, Type)
    tcSplitAppTyNoView_maybe Type
    ty1
          , Just (Type
    ty2a, Type
    ty2b) <- Type -> Maybe (Type, Type)
    tcSplitAppTyNoView_maybe Type
    ty2
          = do { res_a <- Type -> Type -> TcS (Either TypeEqn Type)
    go Type
    ty1a Type
    ty2a
               ; res_b <- go ty1b ty2b
               ; return $ combine_rev mkAppTy res_b res_a }

        go ty1 :: Type
    ty1@(LitTy TyLit
    lit1) (LitTy TyLit
    lit2)
          | TyLit
    lit1 TyLit -> TyLit -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyLit
    lit2
          = Either TypeEqn Type -> TcS (Either TypeEqn Type)
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Type -> Either TypeEqn Type
    forall a b. b -> Either a b
    Right Type
    ty1)

        go Type
    ty1 Type
    ty2 = Type -> Type -> TcS (Either TypeEqn Type)
    forall {m :: * -> *} {a} {b}.
    Monad m =>
    a -> a -> m (Either (Pair a) b)
    bale_out Type
    ty1 Type
    ty2
          -- We don't handle more complex forms here

        bale_out :: a -> a -> m (Either (Pair a) b)
    bale_out a
    ty1 a
    ty2 = Either (Pair a) b -> m (Either (Pair a) b)
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either (Pair a) b -> m (Either (Pair a) b))
    -> Either (Pair a) b -> m (Either (Pair a) b)
    forall a b. (a -> b) -> a -> b
    $ Pair a -> Either (Pair a) b
    forall a b. a -> Either a b
    Left (a -> a -> Pair a
    forall a. a -> a -> Pair a
    Pair a
    ty1 a
    ty2)

        tyvar :: SwapFlag -> TcTyVar -> TcType
              -> TcS (Either (Pair TcType) TcType)
          -- Try to do as little as possible, as anything we do here is redundant
          -- with rewriting. In particular, no need to zonk kinds. That's why
          -- we don't use the already-defined zonking functions
        tyvar :: SwapFlag -> TyVar -> Type -> TcS (Either TypeEqn Type)
    tyvar SwapFlag
    swapped TyVar
    tv Type
    ty
          = case TyVar -> TcTyVarDetails
    tcTyVarDetails TyVar
    tv of
              MetaTv { mtv_ref :: TcTyVarDetails -> IORef MetaDetails
    mtv_ref = IORef MetaDetails
    ref }
                -> do { cts <- IORef MetaDetails -> TcS MetaDetails
    forall a. TcRef a -> TcS a
    readTcRef IORef MetaDetails
    ref
                      ; case cts of
                          MetaDetails
    Flexi        -> TcS (Either TypeEqn Type)
    give_up
                          Indirect Type
    ty' -> do { TyVar -> Type -> TcS ()
    forall {a} {a}. (Outputable a, Outputable a) => a -> a -> TcS ()
    trace_indirect TyVar
    tv Type
    ty'
                                             ; SwapFlag
    -> (Type -> Type -> TcS (Either TypeEqn Type))
    -> Type
    -> Type
    -> TcS (Either TypeEqn Type)
    forall a b. SwapFlag -> (a -> a -> b) -> a -> a -> b
    unSwap SwapFlag
    swapped Type -> Type -> TcS (Either TypeEqn Type)
    go Type
    ty' Type
    ty } }
              TcTyVarDetails
    _ -> TcS (Either TypeEqn Type)
    give_up
          where
            give_up :: TcS (Either TypeEqn Type)
    give_up = Either TypeEqn Type -> TcS (Either TypeEqn Type)
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either TypeEqn Type -> TcS (Either TypeEqn Type))
    -> Either TypeEqn Type -> TcS (Either TypeEqn Type)
    forall a b. (a -> b) -> a -> b
    $ TypeEqn -> Either TypeEqn Type
    forall a b. a -> Either a b
    Left (TypeEqn -> Either TypeEqn Type) -> TypeEqn -> Either TypeEqn Type
    forall a b. (a -> b) -> a -> b
    $ SwapFlag -> (Type -> Type -> TypeEqn) -> Type -> Type -> TypeEqn
    forall a b. SwapFlag -> (a -> a -> b) -> a -> a -> b
    unSwap SwapFlag
    swapped Type -> Type -> TypeEqn
    forall a. a -> a -> Pair a
    Pair (TyVar -> Type
    mkTyVarTy TyVar
    tv) Type
    ty

        tyvar_tyvar :: TyVar -> TyVar -> TcS (Either TypeEqn Type)
    tyvar_tyvar TyVar
    tv1 TyVar
    tv2
          | TyVar
    tv1 TyVar -> TyVar -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyVar
    tv2 = Either TypeEqn Type -> TcS (Either TypeEqn Type)
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Type -> Either TypeEqn Type
    forall a b. b -> Either a b
    Right (TyVar -> Type
    mkTyVarTy TyVar
    tv1))
          | Bool
    otherwise  = do { (ty1', progress1) <- TyVar -> TcS (Type, Bool)
    quick_zonk TyVar
    tv1
                            ; (ty2', progress2) <- quick_zonk tv2
                            ; if progress1 || progress2
                              then go ty1' ty2'
                              else return $ Left (Pair (TyVarTy tv1) (TyVarTy tv2)) }

        trace_indirect :: a -> a -> TcS ()
    trace_indirect a
    tv a
    ty
           = String -> SDoc -> TcS ()
    traceTcS String
    "Following filled tyvar (zonk_eq_types)"
                      (a -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr a
    tv SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> SDoc
    forall doc. IsLine doc => doc
    equals SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> a -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr a
    ty)

        quick_zonk :: TyVar -> TcS (Type, Bool)
    quick_zonk TyVar
    tv = case TyVar -> TcTyVarDetails
    tcTyVarDetails TyVar
    tv of
          MetaTv { mtv_ref :: TcTyVarDetails -> IORef MetaDetails
    mtv_ref = IORef MetaDetails
    ref }
            -> do { cts <- IORef MetaDetails -> TcS MetaDetails
    forall a. TcRef a -> TcS a
    readTcRef IORef MetaDetails
    ref
                  ; case cts of
                      MetaDetails
    Flexi        -> (Type, Bool) -> TcS (Type, Bool)
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TyVar -> Type
    TyVarTy TyVar
    tv, Bool
    False)
                      Indirect Type
    ty' -> do { TyVar -> Type -> TcS ()
    forall {a} {a}. (Outputable a, Outputable a) => a -> a -> TcS ()
    trace_indirect TyVar
    tv Type
    ty'
                                         ; (Type, Bool) -> TcS (Type, Bool)
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Type
    ty', Bool
    True) } }
          TcTyVarDetails
    _ -> (Type, Bool) -> TcS (Type, Bool)
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TyVar -> Type
    TyVarTy TyVar
    tv, Bool
    False)

          -- This happens for type families, too. But recall that failure
          -- here just means to try harder, so it's OK if the type function
          -- isn't injective.
        tycon :: TyCon -> [TcType] -> [TcType]
              -> TcS (Either (Pair TcType) TcType)
        tycon :: TyCon -> [Type] -> [Type] -> TcS (Either TypeEqn Type)
    tycon TyCon
    tc [Type]
    tys1 [Type]
    tys2
          = do { results <- (Type -> Type -> TcS (Either TypeEqn Type))
    -> [Type] -> [Type] -> TcS [Either TypeEqn Type]
    forall (m :: * -> *) a b c.
    Applicative m =>
    (a -> b -> m c) -> [a] -> [b] -> m [c]
    zipWithM Type -> Type -> TcS (Either TypeEqn Type)
    go [Type]
    tys1 [Type]
    tys2
               ; return $ case combine_results results of
                   Left Pair [Type]
    tys  -> TypeEqn -> Either TypeEqn Type
    forall a b. a -> Either a b
    Left (TyCon -> [Type] -> Type
    mkTyConApp TyCon
    tc ([Type] -> Type) -> Pair [Type] -> TypeEqn
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Pair [Type]
    tys)
                   Right [Type]
    tys -> Type -> Either TypeEqn Type
    forall a b. b -> Either a b
    Right (TyCon -> [Type] -> Type
    mkTyConApp TyCon
    tc [Type]
    tys) }

        combine_results :: [Either (Pair TcType) TcType]
                        -> Either (Pair [TcType]) [TcType]
        combine_results :: [Either TypeEqn Type] -> Either (Pair [Type]) [Type]
    combine_results = (Pair [Type] -> Pair [Type])
    -> ([Type] -> [Type])
    -> Either (Pair [Type]) [Type]
    -> Either (Pair [Type]) [Type]
    forall a b c d. (a -> b) -> (c -> d) -> Either a c -> Either b d
    forall (p :: * -> * -> *) a b c d.
    Bifunctor p =>
    (a -> b) -> (c -> d) -> p a c -> p b d
    bimap (([Type] -> [Type]) -> Pair [Type] -> Pair [Type]
    forall a b. (a -> b) -> Pair a -> Pair b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap [Type] -> [Type]
    forall a. [a] -> [a]
    reverse) [Type] -> [Type]
    forall a. [a] -> [a]
    reverse (Either (Pair [Type]) [Type] -> Either (Pair [Type]) [Type])
    -> ([Either TypeEqn Type] -> Either (Pair [Type]) [Type])
    -> [Either TypeEqn Type]
    -> Either (Pair [Type]) [Type]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                          (Either (Pair [Type]) [Type]
     -> Either TypeEqn Type -> Either (Pair [Type]) [Type])
    -> Either (Pair [Type]) [Type]
    -> [Either TypeEqn Type]
    -> Either (Pair [Type]) [Type]
    forall b a. (b -> a -> b) -> b -> [a] -> b
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    foldl' ((Type -> [Type] -> [Type])
    -> Either (Pair [Type]) [Type]
    -> Either TypeEqn Type
    -> Either (Pair [Type]) [Type]
    forall a b c.
    (a -> b -> c)
    -> Either (Pair b) b -> Either (Pair a) a -> Either (Pair c) c
    combine_rev (:)) ([Type] -> Either (Pair [Type]) [Type]
    forall a b. b -> Either a b
    Right [])

          -- combine (in reverse) a new result onto an already-combined result
        combine_rev :: (a -> b -> c)
                    -> Either (Pair b) b
                    -> Either (Pair a) a
                    -> Either (Pair c) c
        combine_rev :: forall a b c.
    (a -> b -> c)
    -> Either (Pair b) b -> Either (Pair a) a -> Either (Pair c) c
    combine_rev a -> b -> c
    f (Left Pair b
    list) (Left Pair a
    elt) = Pair c -> Either (Pair c) c
    forall a b. a -> Either a b
    Left (a -> b -> c
    f (a -> b -> c) -> Pair a -> Pair (b -> c)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Pair a
    elt     Pair (b -> c) -> Pair b -> Pair c
    forall a b. Pair (a -> b) -> Pair a -> Pair b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Pair b
    list)
        combine_rev a -> b -> c
    f (Left Pair b
    list) (Right a
    ty) = Pair c -> Either (Pair c) c
    forall a b. a -> Either a b
    Left (a -> b -> c
    f (a -> b -> c) -> Pair a -> Pair (b -> c)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> a -> Pair a
    forall a. a -> Pair a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure a
    ty Pair (b -> c) -> Pair b -> Pair c
    forall a b. Pair (a -> b) -> Pair a -> Pair b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Pair b
    list)
        combine_rev a -> b -> c
    f (Right b
    tys) (Left Pair a
    elt) = Pair c -> Either (Pair c) c
    forall a b. a -> Either a b
    Left (a -> b -> c
    f (a -> b -> c) -> Pair a -> Pair (b -> c)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Pair a
    elt     Pair (b -> c) -> Pair b -> Pair c
    forall a b. Pair (a -> b) -> Pair a -> Pair b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> b -> Pair b
    forall a. a -> Pair a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure b
    tys)
        combine_rev a -> b -> c
    f (Right b
    tys) (Right a
    ty) = c -> Either (Pair c) c
    forall a b. b -> Either a b
    Right (a -> b -> c
    f a
    ty b
    tys)

    {- *********************************************************************
    *                                                                      *
    *           canonicaliseEquality
    *                                                                      *
    ********************************************************************* -}

    canonicaliseEquality
       :: CtEvidence -> EqRel
       -> Type -> Type     -- LHS and RHS
       -> SolverStage (Either IrredCt EqCt)
    -- Precondition: in DEBUG mode, the `ctev_pred` of `ev` is (ps_ty1 ~# ps_ty2),
    --               without zonking
    -- This precondition is needed (only in DEBUG) to satisfy the assertions
    --   in mkSelCo, called in canDecomposableTyConAppOK and canDecomposableFunTy

    canonicaliseEquality :: CtEvidence
    -> EqRel -> Type -> Type -> SolverStage (Either IrredCt EqCt)
    canonicaliseEquality CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    ty2
      = TcS (StopOrContinue (Either IrredCt EqCt))
    -> SolverStage (Either IrredCt EqCt)
    forall a. TcS (StopOrContinue a) -> SolverStage a
    Stage (TcS (StopOrContinue (Either IrredCt EqCt))
     -> SolverStage (Either IrredCt EqCt))
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    -> SolverStage (Either IrredCt EqCt)
    forall a b. (a -> b) -> a -> b
    $ do { String -> SDoc -> TcS ()
    traceTcS String
    "canonicaliseEquality" (SDoc -> TcS ()) -> SDoc -> TcS ()
    forall a b. (a -> b) -> a -> b
    $
                     [SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ CtEvidence -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CtEvidence
    ev, EqRel -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr EqRel
    eq_rel, Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty1, Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty2 ]
                   ; rdr_env   <- TcS GlobalRdrEnv
    getGlobalRdrEnvTcS
                   ; fam_insts <- getFamInstEnvs
                   ; can_eq_nc False rdr_env fam_insts ev eq_rel ty1 ty1 ty2 ty2 }

    can_eq_nc
       :: Bool           -- True => both input types are rewritten
       -> GlobalRdrEnv   -- needed to see which newtypes are in scope
       -> FamInstEnvs    -- needed to unwrap data instances
       -> CtEvidence
       -> EqRel
       -> Type -> Type    -- LHS, after and before type-synonym expansion, resp
       -> Type -> Type    -- RHS, after and before type-synonym expansion, resp
       -> TcS (StopOrContinue (Either IrredCt EqCt))

    -- See Note [Comparing nullary type synonyms] in GHC.Core.TyCo.Compare
    can_eq_nc :: Bool
    -> GlobalRdrEnv
    -> (FamInstEnv, FamInstEnv)
    -> CtEvidence
    -> EqRel
    -> Type
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_nc Bool
    _flat GlobalRdrEnv
    _rdr_env (FamInstEnv, FamInstEnv)
    _envs CtEvidence
    ev EqRel
    eq_rel ty1 :: Type
    ty1@(TyConApp TyCon
    tc1 []) Type
    _ps_ty1 (TyConApp TyCon
    tc2 []) Type
    _ps_ty2
      | TyCon
    tc1 TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyCon
    tc2
      = CtEvidence
    -> EqRel -> Type -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a. CtEvidence -> EqRel -> Type -> TcS (StopOrContinue a)
    canEqReflexive CtEvidence
    ev EqRel
    eq_rel Type
    ty1

    -- Expand synonyms first; see Note [Type synonyms and canonicalization]
    can_eq_nc Bool
    rewritten GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    ps_ty1 Type
    ty2 Type
    ps_ty2
      | Just Type
    ty1' <- Type -> Maybe Type
    coreView Type
    ty1 = Bool
    -> GlobalRdrEnv
    -> (FamInstEnv, FamInstEnv)
    -> CtEvidence
    -> EqRel
    -> Type
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_nc Bool
    rewritten GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel Type
    ty1' Type
    ps_ty1 Type
    ty2  Type
    ps_ty2
      | Just Type
    ty2' <- Type -> Maybe Type
    coreView Type
    ty2 = Bool
    -> GlobalRdrEnv
    -> (FamInstEnv, FamInstEnv)
    -> CtEvidence
    -> EqRel
    -> Type
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_nc Bool
    rewritten GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel Type
    ty1  Type
    ps_ty1 Type
    ty2' Type
    ps_ty2

    -- need to check for reflexivity in the ReprEq case.
    -- See Note [Eager reflexivity check]
    -- Check only when rewritten because the zonk_eq_types check in canEqNC takes
    -- care of the non-rewritten case.
    can_eq_nc Bool
    True GlobalRdrEnv
    _rdr_env (FamInstEnv, FamInstEnv)
    _envs CtEvidence
    ev EqRel
    ReprEq Type
    ty1 Type
    _ Type
    ty2 Type
    _
      | Type
    ty1 HasDebugCallStack => Type -> Type -> Bool
    Type -> Type -> Bool
    `tcEqType` Type
    ty2
      = CtEvidence
    -> EqRel -> Type -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a. CtEvidence -> EqRel -> Type -> TcS (StopOrContinue a)
    canEqReflexive CtEvidence
    ev EqRel
    ReprEq Type
    ty1

    -- When working with ReprEq, unwrap newtypes.
    -- See Note [Unwrap newtypes first]
    -- This must be above the TyVarTy case, in order to guarantee (TyEq:N)
    can_eq_nc Bool
    _rewritten GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    ps_ty1 Type
    ty2 Type
    ps_ty2
      | EqRel
    ReprEq <- EqRel
    eq_rel
      , Just ((Bag GlobalRdrElt, TcCoercion), Type)
    stuff1 <- (FamInstEnv, FamInstEnv)
    -> GlobalRdrEnv
    -> Type
    -> Maybe ((Bag GlobalRdrElt, TcCoercion), Type)
    tcTopNormaliseNewTypeTF_maybe (FamInstEnv, FamInstEnv)
    envs GlobalRdrEnv
    rdr_env Type
    ty1
      = GlobalRdrEnv
    -> (FamInstEnv, FamInstEnv)
    -> CtEvidence
    -> SwapFlag
    -> Type
    -> ((Bag GlobalRdrElt, TcCoercion), Type)
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_newtype_nc GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev SwapFlag
    NotSwapped Type
    ty1 ((Bag GlobalRdrElt, TcCoercion), Type)
    stuff1 Type
    ty2 Type
    ps_ty2

      | EqRel
    ReprEq <- EqRel
    eq_rel
      , Just ((Bag GlobalRdrElt, TcCoercion), Type)
    stuff2 <- (FamInstEnv, FamInstEnv)
    -> GlobalRdrEnv
    -> Type
    -> Maybe ((Bag GlobalRdrElt, TcCoercion), Type)
    tcTopNormaliseNewTypeTF_maybe (FamInstEnv, FamInstEnv)
    envs GlobalRdrEnv
    rdr_env Type
    ty2
      = GlobalRdrEnv
    -> (FamInstEnv, FamInstEnv)
    -> CtEvidence
    -> SwapFlag
    -> Type
    -> ((Bag GlobalRdrElt, TcCoercion), Type)
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_newtype_nc GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev SwapFlag
    IsSwapped Type
    ty2 ((Bag GlobalRdrElt, TcCoercion), Type)
    stuff2 Type
    ty1 Type
    ps_ty1

    -- Then, get rid of casts
    can_eq_nc Bool
    rewritten GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel (CastTy Type
    ty1 TcCoercion
    co1) Type
    _ Type
    ty2 Type
    ps_ty2
      | Maybe CanEqLHS -> Bool
    forall a. Maybe a -> Bool
    isNothing (Type -> Maybe CanEqLHS
    canEqLHS_maybe Type
    ty2)  -- See (EIK3) in Note [Equalities with incompatible kinds]
      = Bool
    -> GlobalRdrEnv
    -> (FamInstEnv, FamInstEnv)
    -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> Type
    -> TcCoercion
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCast Bool
    rewritten GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel SwapFlag
    NotSwapped Type
    ty1 TcCoercion
    co1 Type
    ty2 Type
    ps_ty2
    can_eq_nc Bool
    rewritten GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    ps_ty1 (CastTy Type
    ty2 TcCoercion
    co2) Type
    _
      | Maybe CanEqLHS -> Bool
    forall a. Maybe a -> Bool
    isNothing (Type -> Maybe CanEqLHS
    canEqLHS_maybe Type
    ty1)  -- See (EIK3) in Note [Equalities with incompatible kinds]
      = Bool
    -> GlobalRdrEnv
    -> (FamInstEnv, FamInstEnv)
    -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> Type
    -> TcCoercion
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCast Bool
    rewritten GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel SwapFlag
    IsSwapped Type
    ty2 TcCoercion
    co2 Type
    ty1 Type
    ps_ty1

    ----------------------
    -- Otherwise try to decompose
    ----------------------

    -- Literals
    can_eq_nc Bool
    _rewritten GlobalRdrEnv
    _rdr_env (FamInstEnv, FamInstEnv)
    _envs CtEvidence
    ev EqRel
    eq_rel ty1 :: Type
    ty1@(LitTy TyLit
    l1) Type
    _ (LitTy TyLit
    l2) Type
    _
     | TyLit
    l1 TyLit -> TyLit -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyLit
    l2
      = do { CtEvidence -> Bool -> EvTerm -> TcS ()
    setEvBindIfWanted CtEvidence
    ev Bool
    True (TcCoercion -> EvTerm
    evCoercion (TcCoercion -> EvTerm) -> TcCoercion -> EvTerm
    forall a b. (a -> b) -> a -> b
    $ Role -> Type -> TcCoercion
    mkReflCo (EqRel -> Role
    eqRelRole EqRel
    eq_rel) Type
    ty1)
           ; CtEvidence -> String -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a. CtEvidence -> String -> TcS (StopOrContinue a)
    stopWith CtEvidence
    ev String
    "Equal LitTy" }

    -- Decompose FunTy: (s -> t) and (c => t)
    -- NB: don't decompose (Int -> blah) ~ (Show a => blah)
    can_eq_nc Bool
    _rewritten GlobalRdrEnv
    _rdr_env (FamInstEnv, FamInstEnv)
    _envs CtEvidence
    ev EqRel
    eq_rel
               (FunTy { ft_mult :: Type -> Type
    ft_mult = Type
    am1, ft_af :: Type -> FunTyFlag
    ft_af = FunTyFlag
    af1, ft_arg :: Type -> Type
    ft_arg = Type
    ty1a, ft_res :: Type -> Type
    ft_res = Type
    ty1b }) Type
    _ps_ty1
               (FunTy { ft_mult :: Type -> Type
    ft_mult = Type
    am2, ft_af :: Type -> FunTyFlag
    ft_af = FunTyFlag
    af2, ft_arg :: Type -> Type
    ft_arg = Type
    ty2a, ft_res :: Type -> Type
    ft_res = Type
    ty2b }) Type
    _ps_ty2
      | FunTyFlag
    af1 FunTyFlag -> FunTyFlag -> Bool
    forall a. Eq a => a -> a -> Bool
    == FunTyFlag
    af2  -- See Note [Decomposing FunTy]
      = CtEvidence
    -> EqRel
    -> FunTyFlag
    -> (Type, Type, Type)
    -> (Type, Type, Type)
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a.
    CtEvidence
    -> EqRel
    -> FunTyFlag
    -> (Type, Type, Type)
    -> (Type, Type, Type)
    -> TcS (StopOrContinue a)
    canDecomposableFunTy CtEvidence
    ev EqRel
    eq_rel FunTyFlag
    af1 (Type
    am1,Type
    ty1a,Type
    ty1b) (Type
    am2,Type
    ty2a,Type
    ty2b)

    -- Decompose type constructor applications
    -- NB: we have expanded type synonyms already
    can_eq_nc Bool
    rewritten GlobalRdrEnv
    _rdr_env (FamInstEnv, FamInstEnv)
    _envs CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    _ Type
    ty2 Type
    _
      | Just (TyCon
    tc1, [Type]
    tys1) <- HasCallStack => Type -> Maybe (TyCon, [Type])
    Type -> Maybe (TyCon, [Type])
    tcSplitTyConApp_maybe Type
    ty1
      , Just (TyCon
    tc2, [Type]
    tys2) <- HasCallStack => Type -> Maybe (TyCon, [Type])
    Type -> Maybe (TyCon, [Type])
    tcSplitTyConApp_maybe Type
    ty2
       -- tcSplitTyConApp_maybe: we want to catch e.g. Maybe Int ~ (Int -> Int)
       -- here for better error messages rather than decomposing into AppTys;
       -- hence not using a direct match on TyConApp

      , Bool -> Bool
    not (TyCon -> Bool
    isTypeFamilyTyCon TyCon
    tc1 Bool -> Bool -> Bool
    || TyCon -> Bool
    isTypeFamilyTyCon TyCon
    tc2)
        -- A type family at the top of LHS or RHS: we want to fall through
        -- to the canonical-LHS cases (look for canEqLHS_maybe)

      -- See (TC1) in Note [Canonicalising TyCon/TyCon equalities]
      , let role :: Role
    role            = EqRel -> Role
    eqRelRole EqRel
    eq_rel
            both_generative :: Bool
    both_generative = TyCon -> Role -> Bool
    isGenerativeTyCon TyCon
    tc1 Role
    role Bool -> Bool -> Bool
    && TyCon -> Role -> Bool
    isGenerativeTyCon TyCon
    tc2 Role
    role
      , Bool
    rewritten Bool -> Bool -> Bool
    || Bool
    both_generative
      = CtEvidence
    -> EqRel
    -> Bool
    -> TyCon
    -> [Type]
    -> TyCon
    -> [Type]
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canTyConApp CtEvidence
    ev EqRel
    eq_rel Bool
    both_generative TyCon
    tc1 [Type]
    tys1 TyCon
    tc2 [Type]
    tys2

    can_eq_nc Bool
    _rewritten GlobalRdrEnv
    _rdr_env (FamInstEnv, FamInstEnv)
    _envs CtEvidence
    ev EqRel
    eq_rel
               s1 :: Type
    s1@ForAllTy{} Type
    _
               s2 :: Type
    s2@ForAllTy{} Type
    _
      = CtEvidence
    -> EqRel
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_nc_forall CtEvidence
    ev EqRel
    eq_rel Type
    s1 Type
    s2

    -- See Note [Canonicalising type applications] about why we require rewritten types
    -- Use tcSplitAppTy, not matching on AppTy, to catch oversaturated type families
    -- NB: Only decompose AppTy for nominal equality.
    --     See Note [Decomposing AppTy equalities]
    can_eq_nc Bool
    True GlobalRdrEnv
    _rdr_env (FamInstEnv, FamInstEnv)
    _envs CtEvidence
    ev EqRel
    NomEq Type
    ty1 Type
    _ Type
    ty2 Type
    _
      | Just (Type
    t1, Type
    s1) <- Type -> Maybe (Type, Type)
    tcSplitAppTy_maybe Type
    ty1
      , Just (Type
    t2, Type
    s2) <- Type -> Maybe (Type, Type)
    tcSplitAppTy_maybe Type
    ty2
      = CtEvidence
    -> Type
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_app CtEvidence
    ev Type
    t1 Type
    s1 Type
    t2 Type
    s2

    -------------------
    -- Can't decompose.
    -------------------

    -- No similarity in type structure detected. Rewrite and try again.
    can_eq_nc Bool
    False GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel Type
    _ Type
    ps_ty1 Type
    _ Type
    ps_ty2
      = -- Rewrite the two types and try again
        do { (redn1@(Reduction _ xi1), rewriters1) <- CtEvidence -> Type -> TcS (Reduction, RewriterSet)
    rewrite CtEvidence
    ev Type
    ps_ty1
           ; (redn2@(Reduction _ xi2), rewriters2) <- rewrite ev ps_ty2
           ; new_ev <- rewriteEqEvidence (rewriters1 S.<> rewriters2) ev NotSwapped redn1 redn2
           ; traceTcS "can_eq_nc: go round again" (ppr new_ev $$ ppr xi1 $$ ppr xi2)
           ; can_eq_nc True rdr_env envs new_ev eq_rel xi1 xi1 xi2 xi2 }

    ----------------------------
    -- Look for a canonical LHS.
    -- Only rewritten types end up below here.
    ----------------------------

    -- NB: pattern match on rewritten=True: we want only rewritten types sent to canEqLHS
    -- This means we've rewritten any variables and reduced any type family redexes
    -- See also Note [No top-level newtypes on RHS of representational equalities]

    can_eq_nc Bool
    True GlobalRdrEnv
    _rdr_env (FamInstEnv, FamInstEnv)
    _envs CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    ps_ty1 Type
    ty2 Type
    ps_ty2
      | Just CanEqLHS
    can_eq_lhs1 <- Type -> Maybe CanEqLHS
    canEqLHS_maybe Type
    ty1
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "can_eq1" (Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty1 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty2)
           ; CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHS CtEvidence
    ev EqRel
    eq_rel SwapFlag
    NotSwapped CanEqLHS
    can_eq_lhs1 Type
    ps_ty1 Type
    ty2 Type
    ps_ty2 }

      | Just CanEqLHS
    can_eq_lhs2 <- Type -> Maybe CanEqLHS
    canEqLHS_maybe Type
    ty2
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "can_eq2" (Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty1 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty2)
           ; CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHS CtEvidence
    ev EqRel
    eq_rel SwapFlag
    IsSwapped CanEqLHS
    can_eq_lhs2 Type
    ps_ty2 Type
    ty1 Type
    ps_ty1 }

         -- If the type is TyConApp tc1 args1, then args1 really can't be less
         -- than tyConArity tc1. It could be *more* than tyConArity, but then we
         -- should have handled the case as an AppTy. That case only fires if
         -- _both_ sides of the equality are AppTy-like... but if one side is
         -- AppTy-like and the other isn't (and it also isn't a variable or
         -- saturated type family application, both of which are handled by
         -- can_eq_nc), we're in a failure mode and can just fall through.

    ----------------------------
    -- Fall-through. Give up.
    ----------------------------

    -- We've rewritten and the types don't match. Give up.
    can_eq_nc Bool
    True GlobalRdrEnv
    _rdr_env (FamInstEnv, FamInstEnv)
    _envs CtEvidence
    ev EqRel
    eq_rel Type
    _ Type
    ps_ty1 Type
    _ Type
    ps_ty2
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "can_eq_nc catch-all case" (Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ps_ty1 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ps_ty2)
           ; case EqRel
    eq_rel of -- See Note [Unsolved equalities]
                EqRel
    ReprEq -> CtIrredReason
    -> CtEvidence -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a.
    CtIrredReason
    -> CtEvidence -> TcS (StopOrContinue (Either IrredCt a))
    finishCanWithIrred CtIrredReason
    ReprEqReason CtEvidence
    ev
                EqRel
    NomEq  -> CtIrredReason
    -> CtEvidence -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a.
    CtIrredReason
    -> CtEvidence -> TcS (StopOrContinue (Either IrredCt a))
    finishCanWithIrred CtIrredReason
    ShapeMismatchReason CtEvidence
    ev }
              -- No need to call canEqSoftFailure/canEqHardFailure because they
              -- rewrite, and the types involved here are already rewritten

    {- Note [Unsolved equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    If we have an unsolved equality like
      (a b ~R# Int)
    that is not necessarily insoluble!  Maybe 'a' will turn out to be a newtype.
    So we want to make it a potentially-soluble Irred not an insoluble one.
    Missing this point is what caused #15431
    -}

    ---------------------------------
    can_eq_nc_forall :: CtEvidence -> EqRel
                     -> Type -> Type    -- LHS and RHS
                     -> TcS (StopOrContinue (Either IrredCt EqCt))
    -- See Note [Solving forall equalities]

    can_eq_nc_forall :: CtEvidence
    -> EqRel
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_nc_forall CtEvidence
    ev EqRel
    eq_rel Type
    s1 Type
    s2
     | CtWanted { ctev_dest :: CtEvidence -> TcEvDest
    ctev_dest = TcEvDest
    orig_dest } <- CtEvidence
    ev
     = do { let ([ForAllTyBinder]
    bndrs1, Type
    phi1, [ForAllTyBinder]
    bndrs2, Type
    phi2) = Type -> Type -> ([ForAllTyBinder], Type, [ForAllTyBinder], Type)
    split_foralls Type
    s1 Type
    s2
                flags1 :: [ForAllTyFlag]
    flags1 = [ForAllTyBinder] -> [ForAllTyFlag]
    forall tv argf. [VarBndr tv argf] -> [argf]
    binderFlags [ForAllTyBinder]
    bndrs1
                flags2 :: [ForAllTyFlag]
    flags2 = [ForAllTyBinder] -> [ForAllTyFlag]
    forall tv argf. [VarBndr tv argf] -> [argf]
    binderFlags [ForAllTyBinder]
    bndrs2

          ; if EqRel
    eq_rel EqRel -> EqRel -> Bool
    forall a. Eq a => a -> a -> Bool
    == EqRel
    NomEq Bool -> Bool -> Bool
    && Bool -> Bool
    not ((ForAllTyFlag -> ForAllTyFlag -> Bool)
    -> [ForAllTyFlag] -> [ForAllTyFlag] -> Bool
    forall a b. (a -> b -> Bool) -> [a] -> [b] -> Bool
    all2 ForAllTyFlag -> ForAllTyFlag -> Bool
    eqForAllVis [ForAllTyFlag]
    flags1 [ForAllTyFlag]
    flags2) -- Note [ForAllTy and type equality]
            then do { String -> SDoc -> TcS ()
    traceTcS String
    "Forall failure: visibility-mismatch" (SDoc -> TcS ()) -> SDoc -> TcS ()
    forall a b. (a -> b) -> a -> b
    $
                         [SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    s1, Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    s2, [ForAllTyBinder] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [ForAllTyBinder]
    bndrs1, [ForAllTyBinder] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [ForAllTyBinder]
    bndrs2
                              , [ForAllTyFlag] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [ForAllTyFlag]
    flags1, [ForAllTyFlag] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [ForAllTyFlag]
    flags2 ]
                    ; CtEvidence
    -> Type -> Type -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a.
    CtEvidence
    -> Type -> Type -> TcS (StopOrContinue (Either IrredCt a))
    canEqHardFailure CtEvidence
    ev Type
    s1 Type
    s2 }

            else do {
            String -> SDoc -> TcS ()
    traceTcS String
    "Creating implication for polytype equality" (CtEvidence -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CtEvidence
    ev)
          ; let free_tvs :: TyCoVarSet
    free_tvs     = [Type] -> TyCoVarSet
    tyCoVarsOfTypes [Type
    s1,Type
    s2]
                empty_subst1 :: Subst
    empty_subst1 = InScopeSet -> Subst
    mkEmptySubst (InScopeSet -> Subst) -> InScopeSet -> Subst
    forall a b. (a -> b) -> a -> b
    $ TyCoVarSet -> InScopeSet
    mkInScopeSet TyCoVarSet
    free_tvs
          ; skol_info <- SkolemInfoAnon -> TcS SkolemInfo
    forall (m :: * -> *). MonadIO m => SkolemInfoAnon -> m SkolemInfo
    mkSkolemInfo (Type -> SkolemInfoAnon
    UnifyForAllSkol Type
    phi1)
          ; (subst1, skol_tvs) <- tcInstSkolTyVarsX skol_info empty_subst1 $
                                  binderVars bndrs1

          ; let phi1' = HasDebugCallStack => Subst -> Type -> Type
    Subst -> Type -> Type
    substTy Subst
    subst1 Type
    phi1

                -- Unify the kinds, extend the substitution
                go :: UnifyEnv -> [TcTyVar] -> Subst
                   -> [TyVarBinder] -> [TyVarBinder] -> TcM.TcM TcCoercion
                go UnifyEnv
    uenv (TyVar
    skol_tv:[TyVar]
    skol_tvs) Subst
    subst2 (ForAllTyBinder
    bndr1:[ForAllTyBinder]
    bndrs1) (ForAllTyBinder
    bndr2:[ForAllTyBinder]
    bndrs2)
                  = do { let tv2 :: TyVar
    tv2  = ForAllTyBinder -> TyVar
    forall tv argf. VarBndr tv argf -> tv
    binderVar ForAllTyBinder
    bndr2
                             vis1 :: ForAllTyFlag
    vis1 = ForAllTyBinder -> ForAllTyFlag
    forall tv argf. VarBndr tv argf -> argf
    binderFlag ForAllTyBinder
    bndr1
                             vis2 :: ForAllTyFlag
    vis2 = ForAllTyBinder -> ForAllTyFlag
    forall tv argf. VarBndr tv argf -> argf
    binderFlag ForAllTyBinder
    bndr2

                       -- Unify the kinds, at Nominal role
                       -- See (SF1) in Note [Solving forall equalities]
                       ; kind_co <- UnifyEnv -> Type -> Type -> TcM TcCoercion
    uType (UnifyEnv
    uenv UnifyEnv -> Role -> UnifyEnv
    `setUEnvRole` Role
    Nominal)
                                          (TyVar -> Type
    tyVarKind TyVar
    skol_tv)
                                          (HasDebugCallStack => Subst -> Type -> Type
    Subst -> Type -> Type
    substTy Subst
    subst2 (TyVar -> Type
    tyVarKind TyVar
    tv2))

                       ; let subst2' = Subst -> TyVar -> Type -> Subst
    extendTvSubstAndInScope Subst
    subst2 TyVar
    tv2
                                           (Type -> TcCoercion -> Type
    mkCastTy (TyVar -> Type
    mkTyVarTy TyVar
    skol_tv) TcCoercion
    kind_co)
                             -- skol_tv is already in the in-scope set, but the
                             -- free vars of kind_co are not; hence "...AndInScope"
                       ; co <- go uenv skol_tvs subst2' bndrs1 bndrs2

                       ; return (mkNakedForAllCo skol_tv vis1 vis2 kind_co co)}
                         -- mkNaked.. because these types are not zonked, and the
                         -- assertions in mkForAllCo may fail without that zonking

                -- Done: unify phi1 ~ phi2
                go UnifyEnv
    uenv [] Subst
    subst2 [ForAllTyBinder]
    bndrs1 [ForAllTyBinder]
    bndrs2
                  = Bool -> TcM TcCoercion -> TcM TcCoercion
    forall a. HasCallStack => Bool -> a -> a
    assert ([ForAllTyBinder] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [ForAllTyBinder]
    bndrs1 Bool -> Bool -> Bool
    && [ForAllTyBinder] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [ForAllTyBinder]
    bndrs2) (TcM TcCoercion -> TcM TcCoercion)
    -> TcM TcCoercion -> TcM TcCoercion
    forall a b. (a -> b) -> a -> b
    $
                    UnifyEnv -> Type -> Type -> TcM TcCoercion
    uType UnifyEnv
    uenv Type
    phi1' (Subst -> Type -> Type
    substTyUnchecked Subst
    subst2 Type
    phi2)

                go UnifyEnv
    _ [TyVar]
    _ Subst
    _ [ForAllTyBinder]
    _ [ForAllTyBinder]
    _ = String -> TcM TcCoercion
    forall a. HasCallStack => String -> a
    panic String
    "can_eq_nc_forall"  -- case (s:ss) []

                init_subst2 = InScopeSet -> Subst
    mkEmptySubst (Subst -> InScopeSet
    getSubstInScope Subst
    subst1)

          -- Generate the constraints that live in the body of the implication
          -- See (SF5) in Note [Solving forall equalities]
          ; (lvl, (all_co, wanteds)) <- pushLevelNoWorkList (ppr skol_info)   $
                                        unifyForAllBody ev (eqRelRole eq_rel) $ \UnifyEnv
    uenv ->
                                        UnifyEnv
    -> [TyVar]
    -> Subst
    -> [ForAllTyBinder]
    -> [ForAllTyBinder]
    -> TcM TcCoercion
    go UnifyEnv
    uenv [TyVar]
    skol_tvs Subst
    init_subst2 [ForAllTyBinder]
    bndrs1 [ForAllTyBinder]
    bndrs2

          ; emitTvImplicationTcS lvl (getSkolemInfo skol_info) skol_tvs wanteds

          ; setWantedEq orig_dest all_co
          ; stopWith ev "Deferred polytype equality" } }

     | Bool
    otherwise
     = do { String -> SDoc -> TcS ()
    traceTcS String
    "Omitting decomposition of given polytype equality" (SDoc -> TcS ()) -> SDoc -> TcS ()
    forall a b. (a -> b) -> a -> b
    $
            Type -> Type -> SDoc
    pprEq Type
    s1 Type
    s2    -- See Note [Do not decompose Given polytype equalities]
          ; CtEvidence -> String -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a. CtEvidence -> String -> TcS (StopOrContinue a)
    stopWith CtEvidence
    ev String
    "Discard given polytype equality" }

     where
        split_foralls :: TcType -> TcType
                      -> ( [ForAllTyBinder], TcType
                         , [ForAllTyBinder], TcType)
        -- Split matching foralls; stop when the foralls don't match
        -- See #22537.  See (SF3) in Note [Solving forall equalities]
        -- Postcondition: the two lists of binders returned have the same length
        split_foralls :: Type -> Type -> ([ForAllTyBinder], Type, [ForAllTyBinder], Type)
    split_foralls Type
    s1 Type
    s2
          | Just (ForAllTyBinder
    bndr1, Type
    s1') <- Type -> Maybe (ForAllTyBinder, Type)
    splitForAllForAllTyBinder_maybe Type
    s1
          , Just (ForAllTyBinder
    bndr2, Type
    s2') <- Type -> Maybe (ForAllTyBinder, Type)
    splitForAllForAllTyBinder_maybe Type
    s2
          = let !([ForAllTyBinder]
    bndrs1, Type
    phi1, [ForAllTyBinder]
    bndrs2, Type
    phi2) = Type -> Type -> ([ForAllTyBinder], Type, [ForAllTyBinder], Type)
    split_foralls Type
    s1' Type
    s2'
            in (ForAllTyBinder
    bndr1ForAllTyBinder -> [ForAllTyBinder] -> [ForAllTyBinder]
    forall a. a -> [a] -> [a]
    :[ForAllTyBinder]
    bndrs1, Type
    phi1, ForAllTyBinder
    bndr2ForAllTyBinder -> [ForAllTyBinder] -> [ForAllTyBinder]
    forall a. a -> [a] -> [a]
    :[ForAllTyBinder]
    bndrs2, Type
    phi2)
        split_foralls Type
    s1 Type
    s2 = ([], Type
    s1, [], Type
    s2)

    {- Note [Solving forall equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    To solve an equality between foralls
       [W] (forall a. t1) ~ (forall b. t2)
    the basic plan is simple: just create the implication constraint
       [W] forall a. { t1 ~ (t2[a/b]) }

    The evidence we produce is a ForAllCo; see the typing rule for
    ForAllCo in Note [ForAllCo] in GHC.Tc.TyCo.Rep.

    There are lots of wrinkles of course:

    (SF1) We must check the kinds match (at Nominal role).  So from
          [W] (forall (a:ka). t1) ~ (forall (b:kb). t2)
       we actually generate the implication
          [W] forall (a:ka). { ka ~N kb,  t1 ~ t2[a/b] }
       These kind equalities are generated by the `go` loop in `can_eq_nc_forall`.
       Why Nominal role? See the typing rule for ForAllCo.

    (SF2) At Nominal role we must check that the visiblities match.
      For example
         [W] (forall a. a -> a) ~N  (forall b -> b -> b)
      should fail.  At /Representational/ role we allow this; see the
      typing rule for ForAllCo, mentioned above.

    (SF3) Consider this (#22537)
         newtype P a = MkP (forall c. (a,c))
         [W] (forall a. P a) ~R (forall a b. (a,b))
      The number of foralls does not line up.  But if we just unwrap the outer
      forall a, we'll get
         [W] P a ~R forall b. (a,b)
      Now unwrap the newtype
         [W] (forall c. (a,c)) ~R (forall b. (a,b))
      and all is good.

      Conclusion: Don't fail if the number of foralls does not line up.  Instead,
      handle as many binders as are visibly apparent on both sides, and then keep
      going with unification. See `split_foralls` in `can_eq_nc_forall`.  In the
      above example, at Representational role, the unifier will proceed to unwrap
      the newtype on the RHS and we'll end up back in can_eq_nc_forall.

    (SF4) Remember also that we might have forall z (a:z). blah
      so in that `go` loop, we must proceed one binder at a time (#13879)

    (SF5) Rather than manually gather the constraints needed in the body of the
       implication, we use `uType`.  That way we can solve some of them on the fly,
       especially Refl ones.  We use the `unifyForAllBody` wrapper for `uType`,
       because we want to /gather/ the equality constraint (to put in the implication)
       rather than /emit/ them into the monad, as `wrapUnifierTcS` does.
    -}

    {- Note [Unwrap newtypes first]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    See also Note [Decomposing newtype equalities]

    Consider
      newtype N m a = MkN (m a)
    N will get a conservative, Nominal role for its second parameter 'a',
    because it appears as an argument to the unknown 'm'. Now consider
      [W] N Maybe a  ~R#  N Maybe b

    If we /decompose/, we'll get
      [W] a ~N# b

    But if instead we /unwrap/ we'll get
      [W] Maybe a ~R# Maybe b
    which in turn gives us
      [W] a ~R# b
    which is easier to satisfy.

    Conclusion: we must unwrap newtypes before decomposing them. This happens
    in `can_eq_newtype_nc`

    We did flirt with making the /rewriter/ expand newtypes, rather than
    doing it in `can_eq_newtype_nc`.   But with recursive newtypes we want
    to be super-careful about expanding!

       newtype A = MkA [A]   -- Recursive!

       f :: A -> [A]
       f = coerce

    We have [W] A ~R# [A].  If we rewrite [A], it'll expand to
       [[[[[...]]]]]
    and blow the reduction stack.  See Note [Newtypes can blow the stack]
    in GHC.Tc.Solver.Rewrite.  But if we expand only the /top level/ of
    both sides, we get
       [W] [A] ~R# [A]
    which we can, just, solve by reflexivity.

    So we simply unwrap, on-demand, at top level, in `can_eq_newtype_nc`.

    This is all very delicate. There is a real risk of a loop in the type checker
    with recursive newtypes -- but I think we're doomed to do *something*
    delicate, as we're really trying to solve for equirecursive type
    equality. Bottom line for users: recursive newtypes do not play well with type
    inference for representational equality.  See also Section 5.3.1 and 5.3.4 of
    "Safe Zero-cost Coercions for Haskell" (JFP 2016).

    See also Note [Decomposing newtype equalities].

    --- Historical side note ---

    We flirted with doing /both/ unwrap-at-top-level /and/ rewrite-deeply;
    see #22519.  But that didn't work: see discussion in #22924. Specifically
    we got a loop with a minor variation:
       f2 :: a -> [A]
       f2 = coerce

    Note [Eager reflexivity check]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose we have

      newtype X = MkX (Int -> X)

    and

      [W] X ~R X

    Naively, we would start unwrapping X and end up in a loop. Instead,
    we do this eager reflexivity check. This is necessary only for representational
    equality because the rewriter technology deals with the similar case
    (recursive type families) for nominal equality.

    Note that this check does not catch all cases, but it will catch the cases
    we're most worried about, types like X above that are actually inhabited.

    Here's another place where this reflexivity check is key:
    Consider trying to prove (f a) ~R (f a). The AppTys in there can't
    be decomposed, because representational equality isn't congruent with respect
    to AppTy. So, when canonicalising the equality above, we get stuck and
    would normally produce a CIrredCan. However, we really do want to
    be able to solve (f a) ~R (f a). So, in the representational case only,
    we do a reflexivity check.

    (This would be sound in the nominal case, but unnecessary, and I [Richard
    E.] am worried that it would slow down the common case.)

     Note [Newtypes can blow the stack]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose we have

      newtype X = MkX (Int -> X)
      newtype Y = MkY (Int -> Y)

    and now wish to prove

      [W] X ~R Y

    This Wanted will loop, expanding out the newtypes ever deeper looking
    for a solid match or a solid discrepancy. Indeed, there is something
    appropriate to this looping, because X and Y *do* have the same representation,
    in the limit -- they're both (Fix ((->) Int)). However, no finitely-sized
    coercion will ever witness it. This loop won't actually cause GHC to hang,
    though, because we check our depth in `can_eq_newtype_nc`.
    -}

    ------------------------
    -- | We're able to unwrap a newtype. Update the bits accordingly.
    can_eq_newtype_nc :: GlobalRdrEnv -> FamInstEnvs
                      -> CtEvidence           -- ^ :: ty1 ~ ty2
                      -> SwapFlag
                      -> TcType                                    -- ^ ty1
                      -> ((Bag GlobalRdrElt, TcCoercion), TcType)  -- ^ :: ty1 ~ ty1'
                      -> TcType               -- ^ ty2
                      -> TcType               -- ^ ty2, with type synonyms
                      -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_newtype_nc :: GlobalRdrEnv
    -> (FamInstEnv, FamInstEnv)
    -> CtEvidence
    -> SwapFlag
    -> Type
    -> ((Bag GlobalRdrElt, TcCoercion), Type)
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_newtype_nc GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev SwapFlag
    swapped Type
    ty1 ((Bag GlobalRdrElt
    gres, TcCoercion
    co1), Type
    ty1') Type
    ty2 Type
    ps_ty2
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "can_eq_newtype_nc" (SDoc -> TcS ()) -> SDoc -> TcS ()
    forall a b. (a -> b) -> a -> b
    $
             [SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ CtEvidence -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CtEvidence
    ev, SwapFlag -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr SwapFlag
    swapped, TcCoercion -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TcCoercion
    co1, Bag GlobalRdrElt -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Bag GlobalRdrElt
    gres, Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty1', Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty2 ]

             -- Check for blowing our stack, and increase the depth
             -- See Note [Newtypes can blow the stack]
           ; let loc :: CtLoc
    loc = CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    ev
                 ev' :: CtEvidence
    ev' = CtEvidence
    ev CtEvidence -> CtLoc -> CtEvidence
    `setCtEvLoc` CtLoc -> CtLoc
    bumpCtLocDepth CtLoc
    loc
           ; CtLoc -> Type -> TcS ()
    checkReductionDepth CtLoc
    loc Type
    ty1

             -- Next, we record uses of newtype constructors, since coercing
             -- through newtypes is tantamount to using their constructors.
           ; Bag GlobalRdrElt -> TcS ()
    recordUsedGREs Bag GlobalRdrElt
    gres

           ; let redn1 :: Reduction
    redn1 = TcCoercion -> Type -> Reduction
    mkReduction TcCoercion
    co1 Type
    ty1'

           ; new_ev <- RewriterSet
    -> CtEvidence
    -> SwapFlag
    -> Reduction
    -> Reduction
    -> TcS CtEvidence
    rewriteEqEvidence RewriterSet
    emptyRewriterSet CtEvidence
    ev' SwapFlag
    swapped
                         Reduction
    redn1 (Role -> Type -> Reduction
    mkReflRedn Role
    Representational Type
    ps_ty2)

           ; can_eq_nc False rdr_env envs new_ev ReprEq ty1' ty1' ty2 ps_ty2 }

    ---------
    -- ^ Decompose a type application.
    -- All input types must be rewritten. See Note [Canonicalising type applications]
    -- Nominal equality only!
    can_eq_app :: CtEvidence       -- :: s1 t1 ~N s2 t2
               -> Xi -> Xi         -- s1 t1
               -> Xi -> Xi         -- s2 t2
               -> TcS (StopOrContinue (Either IrredCt EqCt))

    -- AppTys only decompose for nominal equality, so this case just leads
    -- to an irreducible constraint; see typecheck/should_compile/T10494
    -- See Note [Decomposing AppTy equalities]
    can_eq_app :: CtEvidence
    -> Type
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    can_eq_app CtEvidence
    ev Type
    s1 Type
    t1 Type
    s2 Type
    t2
      | CtWanted { ctev_dest :: CtEvidence -> TcEvDest
    ctev_dest = TcEvDest
    dest } <- CtEvidence
    ev
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "can_eq_app" ([SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "s1:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    s1, String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "t1:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    t1
                                         , String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "s2:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    s2, String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "t2:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    t2
                                         , String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "vis:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> Bool -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr (Type -> Bool
    isNextArgVisible Type
    s1) ])
           ; (co,_,_) <- CtEvidence
    -> Role
    -> (UnifyEnv -> TcM TcCoercion)
    -> TcS (TcCoercion, Cts, [TyVar])
    forall a.
    CtEvidence -> Role -> (UnifyEnv -> TcM a) -> TcS (a, Cts, [TyVar])
    wrapUnifierTcS CtEvidence
    ev Role
    Nominal ((UnifyEnv -> TcM TcCoercion) -> TcS (TcCoercion, Cts, [TyVar]))
    -> (UnifyEnv -> TcM TcCoercion) -> TcS (TcCoercion, Cts, [TyVar])
    forall a b. (a -> b) -> a -> b
    $ \UnifyEnv
    uenv ->
                -- Unify arguments t1/t2 before function s1/s2, because
                -- the former have smaller kinds, and hence simpler error messages
                -- c.f. GHC.Tc.Utils.Unify.uType (go_app)
                do { let arg_env :: UnifyEnv
    arg_env = UnifyEnv -> (CtLoc -> CtLoc) -> UnifyEnv
    updUEnvLoc UnifyEnv
    uenv (Bool -> Bool -> CtLoc -> CtLoc
    adjustCtLoc (Type -> Bool
    isNextArgVisible Type
    s1) Bool
    False)
                   ; co_t <- UnifyEnv -> Type -> Type -> TcM TcCoercion
    uType UnifyEnv
    arg_env Type
    t1 Type
    t2
                   ; co_s <- uType uenv s1 s2
                   ; return (mkAppCo co_s co_t) }
           ; setWantedEq dest co
           ; stopWith ev "Decomposed [W] AppTy" }

        -- If there is a ForAll/(->) mismatch, the use of the Left coercion
        -- below is ill-typed, potentially leading to a panic in splitTyConApp
        -- Test case: typecheck/should_run/Typeable1
        -- We could also include this mismatch check above (for W and D), but it's slow
        -- and we'll get a better error message not doing it
      | Type
    s1k Type -> Type -> Bool
    `mismatches` Type
    s2k
      = CtEvidence
    -> Type -> Type -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a.
    CtEvidence
    -> Type -> Type -> TcS (StopOrContinue (Either IrredCt a))
    canEqHardFailure CtEvidence
    ev (Type
    s1 Type -> Type -> Type
    `mkAppTy` Type
    t1) (Type
    s2 Type -> Type -> Type
    `mkAppTy` Type
    t2)

      | CtGiven { ctev_evar :: CtEvidence -> TyVar
    ctev_evar = TyVar
    evar } <- CtEvidence
    ev
      = do { let co :: TcCoercion
    co   = TyVar -> TcCoercion
    mkCoVarCo TyVar
    evar
                 co_s :: TcCoercion
    co_s = LeftOrRight -> TcCoercion -> TcCoercion
    mkLRCo LeftOrRight
    CLeft  TcCoercion
    co
                 co_t :: TcCoercion
    co_t = LeftOrRight -> TcCoercion -> TcCoercion
    mkLRCo LeftOrRight
    CRight TcCoercion
    co
           ; evar_s <- CtLoc -> (Type, EvTerm) -> TcS CtEvidence
    newGivenEvVar CtLoc
    loc ( CtEvidence -> Type -> Type -> Type
    mkTcEqPredLikeEv CtEvidence
    ev Type
    s1 Type
    s2
                                         , TcCoercion -> EvTerm
    evCoercion TcCoercion
    co_s )
           ; evar_t <- newGivenEvVar loc ( mkTcEqPredLikeEv ev t1 t2
                                         , evCoercion co_t )
           ; emitWorkNC [evar_t]
           ; startAgainWith (mkNonCanonical evar_s) }

      where
        loc :: CtLoc
    loc = CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    ev

        s1k :: Type
    s1k = HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    s1
        s2k :: Type
    s2k = HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    s2

        Type
    k1 mismatches :: Type -> Type -> Bool
    `mismatches` Type
    k2
          =  Type -> Bool
    isForAllTy Type
    k1 Bool -> Bool -> Bool
    && Bool -> Bool
    not (Type -> Bool
    isForAllTy Type
    k2)
          Bool -> Bool -> Bool
    || Bool -> Bool
    not (Type -> Bool
    isForAllTy Type
    k1) Bool -> Bool -> Bool
    && Type -> Bool
    isForAllTy Type
    k2

    -----------------------
    -- | Break apart an equality over a casted type
    -- looking like   (ty1 |> co1) ~ ty2   (modulo a swap-flag)
    canEqCast :: Bool         -- are both types rewritten?
              -> GlobalRdrEnv -> FamInstEnvs
              -> CtEvidence
              -> EqRel
              -> SwapFlag
              -> TcType -> Coercion   -- LHS (res. RHS), ty1 |> co1
              -> TcType -> TcType     -- RHS (res. LHS), ty2 both normal and pretty
              -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCast :: Bool
    -> GlobalRdrEnv
    -> (FamInstEnv, FamInstEnv)
    -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> Type
    -> TcCoercion
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCast Bool
    rewritten GlobalRdrEnv
    rdr_env (FamInstEnv, FamInstEnv)
    envs CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped Type
    ty1 TcCoercion
    co1 Type
    ty2 Type
    ps_ty2
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "Decomposing cast" ([SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ CtEvidence -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CtEvidence
    ev
                                               , Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty1 SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "|>" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> TcCoercion -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TcCoercion
    co1
                                               , Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ps_ty2 ])
           ; new_ev <- RewriterSet
    -> CtEvidence
    -> SwapFlag
    -> Reduction
    -> Reduction
    -> TcS CtEvidence
    rewriteEqEvidence RewriterSet
    emptyRewriterSet CtEvidence
    ev SwapFlag
    swapped
                          (Role -> Type -> TcCoercion -> Reduction
    mkGReflLeftRedn Role
    role Type
    ty1 TcCoercion
    co1)
                          (Role -> Type -> Reduction
    mkReflRedn Role
    role Type
    ps_ty2)
           ; can_eq_nc rewritten rdr_env envs new_ev eq_rel ty1 ty1 ty2 ps_ty2 }
      where
        role :: Role
    role = EqRel -> Role
    eqRelRole EqRel
    eq_rel

    ------------------------
    canTyConApp :: CtEvidence -> EqRel
                -> Bool  -- Both TyCons are generative
                -> TyCon -> [TcType]
                -> TyCon -> [TcType]
                -> TcS (StopOrContinue (Either IrredCt EqCt))
    -- See Note [Decomposing TyConApp equalities]
    -- Neither tc1 nor tc2 is a saturated funTyCon, nor a type family
    -- But they can be data families.
    canTyConApp :: CtEvidence
    -> EqRel
    -> Bool
    -> TyCon
    -> [Type]
    -> TyCon
    -> [Type]
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canTyConApp CtEvidence
    ev EqRel
    eq_rel Bool
    both_generative TyCon
    tc1 [Type]
    tys1 TyCon
    tc2 [Type]
    tys2
      | TyCon
    tc1 TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyCon
    tc2
      , [Type]
    tys1 [Type] -> [Type] -> Bool
    forall a b. [a] -> [b] -> Bool
    `equalLength` [Type]
    tys2
      = do { inerts <- TcS InertSet
    getInertSet
           ; if can_decompose inerts
             then canDecomposableTyConAppOK ev eq_rel tc1 tys1 tys2
             else canEqSoftFailure ev eq_rel ty1 ty2 }

      -- See Note [Skolem abstract data] in GHC.Core.Tycon
      | TyCon -> Bool
    tyConSkolem TyCon
    tc1 Bool -> Bool -> Bool
    || TyCon -> Bool
    tyConSkolem TyCon
    tc2
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "canTyConApp: skolem abstract" (TyCon -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyCon
    tc1 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ TyCon -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyCon
    tc2)
           ; CtIrredReason
    -> CtEvidence -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a.
    CtIrredReason
    -> CtEvidence -> TcS (StopOrContinue (Either IrredCt a))
    finishCanWithIrred CtIrredReason
    AbstractTyConReason CtEvidence
    ev }

      | Bool
    otherwise  -- Different TyCons
      = if Bool
    both_generative -- See (TC2) and (TC3) in
                           -- Note [Canonicalising TyCon/TyCon equalities]
        then CtEvidence
    -> Type -> Type -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a.
    CtEvidence
    -> Type -> Type -> TcS (StopOrContinue (Either IrredCt a))
    canEqHardFailure CtEvidence
    ev Type
    ty1 Type
    ty2
        else CtEvidence
    -> EqRel
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a.
    CtEvidence
    -> EqRel -> Type -> Type -> TcS (StopOrContinue (Either IrredCt a))
    canEqSoftFailure CtEvidence
    ev EqRel
    eq_rel Type
    ty1 Type
    ty2
      where
        -- Reconstruct the types for error messages. This would do
        -- the wrong thing (from a pretty printing point of view)
        -- for functions, because we've lost the FunTyFlag; but
        -- in fact we never call canTyConApp on a saturated FunTyCon
        ty1 :: Type
    ty1 = TyCon -> [Type] -> Type
    mkTyConApp TyCon
    tc1 [Type]
    tys1
        ty2 :: Type
    ty2 = TyCon -> [Type] -> Type
    mkTyConApp TyCon
    tc2 [Type]
    tys2

         -- See Note [Decomposing TyConApp equalities]
         -- and Note [Decomposing newtype equalities]
        can_decompose :: InertSet -> Bool
    can_decompose InertSet
    inerts
          =  TyCon -> Role -> Bool
    isInjectiveTyCon TyCon
    tc1 (EqRel -> Role
    eqRelRole EqRel
    eq_rel)
          Bool -> Bool -> Bool
    || (Bool -> Bool -> Bool
    forall a. HasCallStack => Bool -> a -> a
    assert (EqRel
    eq_rel EqRel -> EqRel -> Bool
    forall a. Eq a => a -> a -> Bool
    == EqRel
    ReprEq) (Bool -> Bool) -> Bool -> Bool
    forall a b. (a -> b) -> a -> b
    $
              -- assert: isInjectiveTyCon is always True for Nominal except
              --   for type synonyms/families, neither of which happen here
              -- Moreover isInjectiveTyCon is True for Representational
              --   for algebraic data types.  So we are down to newtypes
              --   and data families.
              CtEvidence -> CtFlavour
    ctEvFlavour CtEvidence
    ev CtFlavour -> CtFlavour -> Bool
    forall a. Eq a => a -> a -> Bool
    == CtFlavour
    Wanted Bool -> Bool -> Bool
    && TyCon -> InertSet -> Bool
    noGivenNewtypeReprEqs TyCon
    tc1 InertSet
    inerts)
                 -- See Note [Decomposing newtype equalities] (EX2)

    {- Note [Canonicalising TyCon/TyCon equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider

      type family TF a where TF Char = Bool
      data family DF a
      newtype instance DF Bool = MkDF Int

    Suppose we are canonicalising [W] Int ~R# DF (TF a).  Then

    (TC1) We might have an inert Given (a ~# Char), so if we rewrote the wanted
          (i.e. went around again in `can_eq_nc` with `rewritten`=True, we'd get
             [W] Int ~R# DF Bool
          and then the `tcTopNormaliseNewTypeTF_maybe` call would fire and
          we'd unwrap the newtype.  So we must do that "go round again" bit.
          Hence the complicated guard (rewritten || both_generative) in `can_eq_nc`.

    (TC2) If we can't rewrite `a` yet, we'll finish with an unsolved
             [W] Int ~R# DF (TF a)
          in the inert set. But we must use canEqSoftFailure, not canEqHardFailure,
          because it might be solved "later" when we learn more about `a`.
          Hence the use of `both_generative` in `canTyConApp`.

    (TC3) Here's another example:
             [G] Age ~R# Int
          where Age's constructor is not in scope. We don't want to report
          an "inaccessible code" error in the context of this Given!  So again
          we want `canEqSoftFailure`.

          For example, see typecheck/should_compile/T10493, repeated here:
            import Data.Ord (Down)  -- no constructor
            foo :: Coercible (Down Int) Int => Down Int -> Int
            foo = coerce

          That should compile, but only because we use canEqSoftFailure and
          not canEqHardFailure.

    Note [Fast path when decomposing TyConApps]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    If we see (T s1 t1 ~ T s2 t2), then we can just decompose to
      (s1 ~ s2, t1 ~ t2)
    and push those back into the work list.  But if
      s1 = K k1    s2 = K k2
    then we will just decompose s1~s2, and it might be better to
    do so on the spot.  An important special case is where s1=s2,
    and we get just Refl.

    So canDecomposableTyConAppOK uses wrapUnifierTcS etc to short-cut
    that work.  See also Note [Work-list ordering].

    Note [Decomposing TyConApp equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose we have
            [G/W] T ty1 ~r T ty2
    Can we decompose it, and replace it by
            [G/W] ty1 ~r' ty2
    and if so what role is r'?  (In this Note, all the "~" are primitive
    equalities "~#", but I have dropped the noisy "#" symbols.)  Lots of
    background in the paper "Safe zero-cost coercions for Haskell".

    This Note covers the topic for
      * Datatypes
      * Newtypes
      * Data families
    For the rest:
      * Type synonyms: are always expanded
      * Type families: see Note [Decomposing type family applications]
      * AppTy:         see Note [Decomposing AppTy equalities].

    ---- Roles of the decomposed constraints ----
    For a start, the role r' will always be defined like this:
      * If r=N then r' = N
      * If r=R then r' = role of T's first argument

    For example:
       data TR a = MkTR a       -- Role of T's first arg is Representational
       data TN a = MkTN (F a)   -- Role of T's first arg is Nominal

    The function tyConRolesX :: Role -> TyCon -> [Role] gets the argument
    role r' for a TyCon T at role r.  E.g.
       tyConRolesX Nominal          TR = [Nominal]
       tyConRolesX Representational TR = [Representational]

    ---- Soundness and completeness ----
    For Givens, for /soundness/ of decomposition we need, forall ty1,ty2:
        T ty1 ~r T ty2   ===>    ty1 ~r' ty2
    Here "===>" means "implies".  That is, given evidence for (co1 : T ty1 ~r T co2)
    we can produce evidence for (co2 : ty1 ~r' ty2).  But in the solver we
    /replace/ co1 with co2 in the inert set, and we don't want to lose any proofs
    thereby. So for /completeness/ of decomposition we also need the reverse:
        ty1 ~r' ty2   ===>    T ty1 ~r T ty2

    For Wanteds, for /soundness/ of decomposition we need:
        ty1 ~r' ty2   ===>    T ty1 ~r T ty2
    because if we do decompose we'll get evidence (co2 : ty1 ~r' ty2) and
    from that we want to derive evidence (T co2 : T ty1 ~r T ty2).
    For /completeness/ of decomposition we need the reverse implication too,
    else we may decompose to a new proof obligation that is stronger than
    the one we started with.  See Note [Decomposing newtype equalities].

    ---- Injectivity ----
    When do these bi-implications hold? In one direction it is easy.
    We /always/ have
        ty1 ~r'  ty2   ===>    T ty1 ~r T ty2
    This is the CO_TYCONAPP rule of the paper (Fig 5); see also the
    TyConAppCo case of GHC.Core.Lint.lintCoercion.

    In the other direction, we have
        T ty1 ~r T ty2   ==>   ty1 ~r' ty2  if T is /injective at role r/
    This is the very /definition/ of injectivity: injectivity means result
    is the same => arguments are the same, modulo the role shift.
    See comments on GHC.Core.TyCon.isInjectiveTyCon.  This is also
    the CO_NTH rule in Fig 5 of the paper, except in the paper only
    newtypes are non-injective at representation role, so the rule says "H
    is not a newtype".

    Injectivity is a bit subtle:
                     Nominal   Representational
       Datatype        YES        YES
       Newtype         YES        NO{1}
       Data family     YES        NO{2}

    {1} Consider newtype N a = MkN (F a)   -- Arg has Nominal role
        Is it true that (N t1) ~R (N t2)   ==>   t1 ~N t2  ?
        No, absolutely not.  E.g.
           type instance F Int = Int; type instance F Bool = Char
           Then (N Int) ~R (N Bool), by unwrapping, but we don't want Int~Char!

        See Note [Decomposing newtype equalities]

    {2} We must treat data families precisely like newtypes, because of the
        possibility of newtype instances. See also
        Note [Decomposing newtype equalities]. See #10534 and
        test case typecheck/should_fail/T10534.

    ---- Takeaway summary -----
    For sound and complete decomposition, we simply need injectivity;
    that is for isInjectiveTyCon to be true:

    * At Nominal role, isInjectiveTyCon is True for all the TyCons we are
      considering in this Note: datatypes, newtypes, and data families.

    * For Givens, injectivity is necessary for soundness; completeness has no
      side conditions.

    * For Wanteds, soundness has no side conditions; but injectivity is needed
      for completeness. See Note [Decomposing newtype equalities]

    This is implemented in `can_decompose` in `canTyConApp`; it looks at
    injectivity, just as specified above.

    Note [Work-list ordering]
    ~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider decomposing a TyCon equality

        (0) [W] T k_fresh (t1::k_fresh) ~ T k1 (t2::k1)

    This gives rise to 2 equalities in the solver worklist

        (1) [W] k_fresh ~ k1
        (2) [W] t1::k_fresh ~ t2::k1

    We would like to solve (1) before looking at (2), so that we don't end
    up in the complexities of canEqLHSHetero.  To do this:

    * `canDecomposableTyConAppOK` calls `uType` on the arguments
      /left-to-right/.  See the call to zipWith4M in that function.

    * `uType` keeps the bag of emitted constraints in the same
      left-to-right order.  See the use of `snocBag` in `uType_defer`.

    * `wrapUnifierTcS` adds the bag of deferred constraints from
      `do_unifications` to the work-list using `extendWorkListEqs`.

    * `extendWorkListEqs` and `selectWorkItem` together arrange that the
      list of constraints given to `extendWorkListEqs` is processed in
      left-to-right order.

    This is not a very big deal.  It reduces the number of solver steps
    in the test RaeJobTalk from 1830 to 1815, a 1% reduction.  But still,
    it doesn't cost anything either.

    Note [Decomposing type family applications]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Supose we have
       [G/W]  (F ty1) ~r  (F ty2)
    This is handled by the TyFamLHS/TyFamLHS case of canEqCanLHS2.

    We never decompose to
       [G/W]  ty1 ~r' ty2

    Instead

    * For Givens we do nothing. Injective type families have no corresponding
      evidence of their injectivity, so we cannot decompose an
      injective-type-family Given.

    * For Wanteds, for the Nominal role only, we emit new Wanteds rather like
      functional dependencies, for each injective argument position.

      E.g type family F a b   -- injective in first arg, but not second
          [W] (F s1 t1) ~N (F s2 t2)
      Emit new Wanteds
          [W] s1 ~N s2
      But retain the existing, unsolved constraint.

    Note [Decomposing newtype equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    This Note also applies to data families, which we treat like
    newtype in case of 'newtype instance'.

    As Note [Decomposing TyConApp equalities] describes, if N is injective
    at role r, we can do this decomposition?
       [G/W] (N ty1) ~r (N ty2)    to     [G/W]  ty1 ~r' ty2

    For a Given with r=R, the answer is a solid NO: newtypes are not injective at
    representational role, and we must not decompose, or we lose soundness.
    Example is wrinkle {1} in Note [Decomposing TyConApp equalities].

    For a Wanted with r=R, since newtypes are not injective at representational
    role, decomposition is sound, but we may lose completeness.  Nevertheless,
    if the newtype is abstract (so can't be unwrapped) we can only solve
    the equality by (a) using a Given or (b) decomposition.  If (a) is impossible
    (e.g. no Givens) then (b) is safe albeit potentially incomplete.

    There are two ways in which decomposing (N ty1) ~r (N ty2) could be incomplete:

    * Incompleteness example (EX1): unwrap first
          newtype Nt a = MkNt (Id a)
          type family Id a where Id a = a

          [W] Nt Int ~R Nt Age

      Because of its use of a type family, Nt's parameter will get inferred to
      have a nominal role. Thus, decomposing the wanted will yield [W] Int ~N Age,
      which is unsatisfiable. Unwrapping, though, leads to a solution.

      Conclusion: always unwrap newtypes before attempting to decompose
      them.  This is done in can_eq_nc.  Of course, we can't unwrap if the data
      constructor isn't in scope.  See Note [Unwrap newtypes first].

    * Incompleteness example (EX2): available Givens
          newtype Nt a = Mk Bool         -- NB: a is not used in the RHS,
          type role Nt representational  -- but the user gives it an R role anyway

          [G] Nt t1 ~R Nt t2
          [W] Nt alpha ~R Nt beta

      We *don't* want to decompose to [W] alpha ~R beta, because it's possible
      that alpha and beta aren't representationally equal.  And if we figure
      out (elsewhere) that alpha:=t1 and beta:=t2, we can solve the Wanted
      from the Given.  This is somewhat similar to the question of overlapping
      Givens for class constraints: see Note [Instance and Given overlap] in
      GHC.Tc.Solver.Dict.

      Conclusion: don't decompose [W] N s ~R N t, if there are any Given
      equalities that could later solve it.

      But what precisely does it mean to say "any Given equalities that could
      later solve it"?

      In #22924 we had
         [G] f a ~R# a     [W] Const (f a) a ~R# Const a a
      where Const is an abstract newtype.  If we decomposed the newtype, we
      could solve.  Not-decomposing on the grounds that (f a ~R# a) might turn
      into (Const (f a) a ~R# Const a a) seems a bit silly.

      In #22331 we had
         [G] N a ~R# N b   [W] N b ~R# N a
      (where N is abstract so we can't unwrap). Here we really /don't/ want to
      decompose, because the /only/ way to solve the Wanted is from that Given
      (with a Sym).

      In #22519 we had
         [G] a <= b     [W] IO Age ~R# IO Int

      (where IO is abstract so we can't unwrap, and newtype Age = Int; and (<=)
      is a type-level comparison on Nats).  Here we /must/ decompose, despite the
      existence of an Irred Given, or we will simply be stuck.  (Side note: We
      flirted with deep-rewriting of newtypes (see discussion on #22519 and
      !9623) but that turned out not to solve #22924, and also makes type
      inference loop more often on recursive newtypes.)

      The currently-implemented compromise is this:

        we decompose [W] N s ~R# N t unless there is a [G] N s' ~ N t'

      that is, a Given Irred equality with both sides headed with N.
      See the call to noGivenNewtypeReprEqs in canTyConApp.

      This is not perfect.  In principle a Given like [G] (a b) ~ (c d), or
      even just [G] c, could later turn into N s ~ N t.  But since the free
      vars of a Given are skolems, or at least untouchable unification
      variables, this is extremely unlikely to happen.

      Another worry: there could, just, be a CDictCan with some
      un-expanded equality superclasses; but only in some very obscure
      recursive-superclass situations.

       Yet another approach (!) is desribed in
       Note [Decomposing newtypes a bit more aggressively].

    Remember: decomposing Wanteds is always /sound/. This Note is
    only about /completeness/.

    Note [Decomposing newtypes a bit more aggressively]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    IMPORTANT: the ideas in this Note are *not* implemented. Instead, the
    current approach is detailed in Note [Decomposing newtype equalities]
    and Note [Unwrap newtypes first].
    For more details about the ideas in this Note see
      * GHC propoosal: https://github.com/ghc-proposals/ghc-proposals/pull/549
      * issue #22441
      * discussion on !9282.

    Consider [G] c, [W] (IO Int) ~R (IO Age)
    where IO is abstract, and
       newtype Age = MkAge Int   -- Not abstract
    With the above rules, if there any Given Irreds,
    the Wanted is insoluble because we can't decompose it.  But in fact,
    if we look at the defn of IO, roughly,
        newtype IO a = State# -> (State#, a)
    we can see that decomposing [W] (IO Int) ~R (IO Age) to
        [W] Int ~R Age
    definitely does not lose completeness. Why not? Because the role of
    IO's argment is representational.  Hence:

      DecomposeNewtypeIdea:
         decompose [W] (N s1 .. sn) ~R (N t1 .. tn)
         if the roles of all N's arguments are representational

    If N's arguments really /are/ representational this will not lose
    completeness.  Here "really are representational" means "if you expand
    all newtypes in N's RHS, we'd infer a representational role for each
    of N's type variables in that expansion".  See Note [Role inference]
    in GHC.Tc.TyCl.Utils.

    But the user might /override/ a phantom role with an explicit role
    annotation, and then we could (obscurely) get incompleteness.
    Consider

       module A( silly, T ) where
         newtype T a = MkT Int
         type role T representational  -- Override phantom role

         silly :: Coercion (T Int) (T Bool)
         silly = Coercion  -- Typechecks by unwrapping the newtype

         data Coercion a b where  -- Actually defined in Data.Type.Coercion
           Coercion :: Coercible a b => Coercion a b

       module B where
         import A
         f :: T Int -> T Bool
         f = case silly of Coercion -> coerce

    Here the `coerce` gives [W] (T Int) ~R (T Bool) which, if we decompose,
    we'll get stuck with (Int ~R Bool).  Instead we want to use the
    [G] (T Int) ~R (T Bool), which will be in the Irreds.

    Summary: we could adopt (DecomposeNewtypeIdea), at the cost of a very
    obscure incompleteness (above).  But no one is reporting a problem from
    the lack of decompostion, so we'll just leave it for now.  This long
    Note is just to record the thinking for our future selves.

    Note [Decomposing AppTy equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    For AppTy all the same questions arise as in
    Note [Decomposing TyConApp equalities]. We have

        s1 ~r s2,  t1 ~N t2   ==>   s1 t1 ~r s2 t2       (rule CO_APP)
        s1 t1 ~N s2 t2        ==>   s1 ~N s2,  t1 ~N t2  (CO_LEFT, CO_RIGHT)

    In the first of these, why do we need Nominal equality in (t1 ~N t2)?
    See {2} below.

    For sound and complete solving, we need both directions to decompose. So:
    * At nominal role, all is well: we have both directions.
    * At representational role, decomposition of Givens is unsound (see {1} below),
      and decomposition of Wanteds is incomplete.

    Here is an example of the incompleteness for Wanteds:

        [G] g1 :: a ~R b
        [W] w1 :: Maybe b ~R alpha a
        [W] w2 :: alpha ~N Maybe

    Suppose we see w1 before w2. If we decompose, using AppCo to prove w1, we get

        w1 := AppCo w3 w4
        [W] w3 :: Maybe ~R alpha
        [W] w4 :: b ~N a

    Note that w4 is *nominal*. A nominal role here is necessary because AppCo
    requires a nominal role on its second argument. (See {2} for an example of
    why.) Now we are stuck, because w4 is insoluble. On the other hand, if we
    see w2 first, setting alpha := Maybe, all is well, as we can decompose
    Maybe b ~R Maybe a into b ~R a.

    Another example:
        newtype Phant x = MkPhant Int
        [W] w1 :: Phant Int ~R alpha Bool
        [W] w2 :: alpha ~ Phant

    If we see w1 first, decomposing would be disastrous, as we would then try
    to solve Int ~ Bool. Instead, spotting w2 allows us to simplify w1 to become
        [W] w1' :: Phant Int ~R Phant Bool

    which can then (assuming MkPhant is in scope) be simplified to Int ~R Int,
    and all will be well. See also Note [Unwrap newtypes first].

    Bottom line:
    * Always decompose AppTy at nominal role: can_eq_app
    * Never decompose AppTy at representational role (neither Given nor Wanted):
      the lack of an equation in can_eq_nc

    Extra points
    {1}  Decomposing a Given AppTy over a representational role is simply
         unsound. For example, if we have co1 :: Phant Int ~R a Bool (for
         the newtype Phant, above), then we surely don't want any relationship
         between Int and Bool, lest we also have co2 :: Phant ~ a around.

    {2} The role on the AppCo coercion is a conservative choice, because we don't
        know the role signature of the function. For example, let's assume we could
        have a representational role on the second argument of AppCo. Then, consider

        data G a where    -- G will have a nominal role, as G is a GADT
          MkG :: G Int
        newtype Age = MkAge Int

        co1 :: G ~R a        -- by assumption
        co2 :: Age ~R Int    -- by newtype axiom
        co3 = AppCo co1 co2 :: G Age ~R a Int    -- by our broken AppCo

        and now co3 can be used to cast MkG to have type G Age, in violation of
        the way GADTs are supposed to work (which is to use nominal equality).
    -}

    canDecomposableTyConAppOK :: CtEvidence -> EqRel
                              -> TyCon -> [TcType] -> [TcType]
                              -> TcS (StopOrContinue a)
    -- Precondition: tys1 and tys2 are the same finite length, hence "OK"
    canDecomposableTyConAppOK :: forall a.
    CtEvidence
    -> EqRel -> TyCon -> [Type] -> [Type] -> TcS (StopOrContinue a)
    canDecomposableTyConAppOK CtEvidence
    ev EqRel
    eq_rel TyCon
    tc [Type]
    tys1 [Type]
    tys2
      = Bool -> TcS (StopOrContinue a) -> TcS (StopOrContinue a)
    forall a. HasCallStack => Bool -> a -> a
    assert ([Type]
    tys1 [Type] -> [Type] -> Bool
    forall a b. [a] -> [b] -> Bool
    `equalLength` [Type]
    tys2) (TcS (StopOrContinue a) -> TcS (StopOrContinue a))
    -> TcS (StopOrContinue a) -> TcS (StopOrContinue a)
    forall a b. (a -> b) -> a -> b
    $
        do { String -> SDoc -> TcS ()
    traceTcS String
    "canDecomposableTyConAppOK"
                      (CtEvidence -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CtEvidence
    ev SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ EqRel -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr EqRel
    eq_rel SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ TyCon -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyCon
    tc SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ [Type] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [Type]
    tys1 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ [Type] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [Type]
    tys2)
           ; case CtEvidence
    ev of
               CtWanted { ctev_dest :: CtEvidence -> TcEvDest
    ctev_dest = TcEvDest
    dest }
                 -- new_locs and tc_roles are both infinite, so we are
                 -- guaranteed that cos has the same length as tys1 and tys2
                 -- See Note [Fast path when decomposing TyConApps]
                 -> do { (co, _, _) <- CtEvidence
    -> Role
    -> (UnifyEnv -> TcM TcCoercion)
    -> TcS (TcCoercion, Cts, [TyVar])
    forall a.
    CtEvidence -> Role -> (UnifyEnv -> TcM a) -> TcS (a, Cts, [TyVar])
    wrapUnifierTcS CtEvidence
    ev Role
    role ((UnifyEnv -> TcM TcCoercion) -> TcS (TcCoercion, Cts, [TyVar]))
    -> (UnifyEnv -> TcM TcCoercion) -> TcS (TcCoercion, Cts, [TyVar])
    forall a b. (a -> b) -> a -> b
    $ \UnifyEnv
    uenv ->
                            do { cos <- (CtLoc -> Role -> Type -> Type -> TcM TcCoercion)
    -> [CtLoc]
    -> [Role]
    -> [Type]
    -> [Type]
    -> IOEnv (Env TcGblEnv TcLclEnv) [TcCoercion]
    forall (m :: * -> *) a b c d e.
    Monad m =>
    (a -> b -> c -> d -> m e) -> [a] -> [b] -> [c] -> [d] -> m [e]
    zipWith4M (UnifyEnv -> CtLoc -> Role -> Type -> Type -> TcM TcCoercion
    u_arg UnifyEnv
    uenv) [CtLoc]
    new_locs [Role]
    tc_roles [Type]
    tys1 [Type]
    tys2
                                        -- zipWith4M: see Note [Work-list ordering]
                                        -- in GHC.Tc.Solved.Equality
                               ; return (mkTyConAppCo role tc cos) }
                       ; setWantedEq dest co }

               CtGiven { ctev_evar :: CtEvidence -> TyVar
    ctev_evar = TyVar
    evar }
                 | let ev_co :: TcCoercion
    ev_co = TyVar -> TcCoercion
    mkCoVarCo TyVar
    evar
                 -> CtLoc -> [(Role, Type, Type, TcCoercion)] -> TcS ()
    emitNewGivens CtLoc
    loc
                           [ (Role
    r, Type
    ty1, Type
    ty2, HasDebugCallStack => CoSel -> TcCoercion -> TcCoercion
    CoSel -> TcCoercion -> TcCoercion
    mkSelCo (Int -> Role -> CoSel
    SelTyCon Int
    i Role
    r) TcCoercion
    ev_co)
                           | (Role
    r, Type
    ty1, Type
    ty2, Int
    i) <- [Role] -> [Type] -> [Type] -> [Int] -> [(Role, Type, Type, Int)]
    forall a b c d. [a] -> [b] -> [c] -> [d] -> [(a, b, c, d)]
    zip4 [Role]
    tc_roles [Type]
    tys1 [Type]
    tys2 [Int
    0..]
                           , Role
    r Role -> Role -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Role
    Phantom
                           , Bool -> Bool
    not (Type -> Bool
    isCoercionTy Type
    ty1) Bool -> Bool -> Bool
    && Bool -> Bool
    not (Type -> Bool
    isCoercionTy Type
    ty2) ]

        ; CtEvidence -> String -> TcS (StopOrContinue a)
    forall a. CtEvidence -> String -> TcS (StopOrContinue a)
    stopWith CtEvidence
    ev String
    "Decomposed TyConApp" }

      where
        loc :: CtLoc
    loc  = CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    ev
        role :: Role
    role = EqRel -> Role
    eqRelRole EqRel
    eq_rel

        u_arg :: UnifyEnv -> CtLoc -> Role -> Type -> Type -> TcM TcCoercion
    u_arg UnifyEnv
    uenv CtLoc
    arg_loc Role
    arg_role = UnifyEnv -> Type -> Type -> TcM TcCoercion
    uType UnifyEnv
    arg_env
           where
             arg_env :: UnifyEnv
    arg_env = UnifyEnv
    uenv UnifyEnv -> Role -> UnifyEnv
    `setUEnvRole` Role
    arg_role
                            UnifyEnv -> (CtLoc -> CtLoc) -> UnifyEnv
    `updUEnvLoc`  CtLoc -> CtLoc -> CtLoc
    forall a b. a -> b -> a
    const CtLoc
    arg_loc

        -- Infinite, to allow for over-saturated TyConApps
        tc_roles :: [Role]
    tc_roles = Role -> TyCon -> [Role]
    tyConRoleListX Role
    role TyCon
    tc

          -- Add nuances to the location during decomposition:
          --  * if the argument is a kind argument, remember this, so that error
          --    messages say "kind", not "type". This is determined based on whether
          --    the corresponding tyConBinder is named (that is, dependent)
          --  * if the argument is invisible, note this as well, again by
          --    looking at the corresponding binder
          -- For oversaturated tycons, we need the (repeat loc) tail, which doesn't
          -- do either of these changes. (Forgetting to do so led to #16188)
          --
          -- NB: infinite in length
        new_locs :: [CtLoc]
    new_locs = [ TyConBinder -> CtLoc -> CtLoc
    adjustCtLocTyConBinder TyConBinder
    bndr CtLoc
    loc
                   | TyConBinder
    bndr <- TyCon -> [TyConBinder]
    tyConBinders TyCon
    tc ]
                   [CtLoc] -> [CtLoc] -> [CtLoc]
    forall a. [a] -> [a] -> [a]
    ++ CtLoc -> [CtLoc]
    forall a. a -> [a]
    repeat CtLoc
    loc

    canDecomposableFunTy :: CtEvidence -> EqRel -> FunTyFlag
                         -> (Type,Type,Type)   -- (multiplicity,arg,res)
                         -> (Type,Type,Type)   -- (multiplicity,arg,res)
                         -> TcS (StopOrContinue a)
    canDecomposableFunTy :: forall a.
    CtEvidence
    -> EqRel
    -> FunTyFlag
    -> (Type, Type, Type)
    -> (Type, Type, Type)
    -> TcS (StopOrContinue a)
    canDecomposableFunTy CtEvidence
    ev EqRel
    eq_rel FunTyFlag
    af f1 :: (Type, Type, Type)
    f1@(Type
    m1,Type
    a1,Type
    r1) f2 :: (Type, Type, Type)
    f2@(Type
    m2,Type
    a2,Type
    r2)
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "canDecomposableFunTy"
                      (CtEvidence -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CtEvidence
    ev SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ EqRel -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr EqRel
    eq_rel SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ (Type, Type, Type) -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr (Type, Type, Type)
    f1 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ (Type, Type, Type) -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr (Type, Type, Type)
    f2)
           ; case CtEvidence
    ev of
               CtWanted { ctev_dest :: CtEvidence -> TcEvDest
    ctev_dest = TcEvDest
    dest }
                 -> do { (co, _, _) <- CtEvidence
    -> Role
    -> (UnifyEnv -> TcM TcCoercion)
    -> TcS (TcCoercion, Cts, [TyVar])
    forall a.
    CtEvidence -> Role -> (UnifyEnv -> TcM a) -> TcS (a, Cts, [TyVar])
    wrapUnifierTcS CtEvidence
    ev Role
    Nominal ((UnifyEnv -> TcM TcCoercion) -> TcS (TcCoercion, Cts, [TyVar]))
    -> (UnifyEnv -> TcM TcCoercion) -> TcS (TcCoercion, Cts, [TyVar])
    forall a b. (a -> b) -> a -> b
    $ \ UnifyEnv
    uenv ->
                            do { let mult_env :: UnifyEnv
    mult_env = UnifyEnv
    uenv UnifyEnv -> (CtLoc -> CtLoc) -> UnifyEnv
    `updUEnvLoc` CtLoc -> CtLoc
    toInvisibleLoc
                                                     UnifyEnv -> Role -> UnifyEnv
    `setUEnvRole` Role -> FunSel -> Role
    funRole Role
    role FunSel
    SelMult
                               ; mult <- UnifyEnv -> Type -> Type -> TcM TcCoercion
    uType UnifyEnv
    mult_env Type
    m1 Type
    m2
                               ; arg  <- uType (uenv `setUEnvRole` funRole role SelArg) a1 a2
                               ; res  <- uType (uenv `setUEnvRole` funRole role SelRes) r1 r2
                               ; return (mkNakedFunCo role af mult arg res) }
                       ; setWantedEq dest co }

               CtGiven { ctev_evar :: CtEvidence -> TyVar
    ctev_evar = TyVar
    evar }
                 | let ev_co :: TcCoercion
    ev_co = TyVar -> TcCoercion
    mkCoVarCo TyVar
    evar
                 -> CtLoc -> [(Role, Type, Type, TcCoercion)] -> TcS ()
    emitNewGivens CtLoc
    loc
                           [ (Role -> FunSel -> Role
    funRole Role
    role FunSel
    fs, Type
    ty1, Type
    ty2, HasDebugCallStack => CoSel -> TcCoercion -> TcCoercion
    CoSel -> TcCoercion -> TcCoercion
    mkSelCo (FunSel -> CoSel
    SelFun FunSel
    fs) TcCoercion
    ev_co)
                           | (FunSel
    fs, Type
    ty1, Type
    ty2) <- [ (FunSel
    SelMult, Type
    m1, Type
    m2)
                                               , (FunSel
    SelArg,  Type
    a1, Type
    a2)
                                               , (FunSel
    SelRes,  Type
    r1, Type
    r2)] ]

        ; CtEvidence -> String -> TcS (StopOrContinue a)
    forall a. CtEvidence -> String -> TcS (StopOrContinue a)
    stopWith CtEvidence
    ev String
    "Decomposed TyConApp" }

      where
        loc :: CtLoc
    loc  = CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    ev
        role :: Role
    role = EqRel -> Role
    eqRelRole EqRel
    eq_rel

    -- | Call canEqSoftFailure when canonicalizing an equality fails, but if the
    -- equality is representational, there is some hope for the future.
    canEqSoftFailure :: CtEvidence -> EqRel -> TcType -> TcType
                     -> TcS (StopOrContinue (Either IrredCt a))
    canEqSoftFailure :: forall a.
    CtEvidence
    -> EqRel -> Type -> Type -> TcS (StopOrContinue (Either IrredCt a))
    canEqSoftFailure CtEvidence
    ev EqRel
    NomEq Type
    ty1 Type
    ty2
      = CtEvidence
    -> Type -> Type -> TcS (StopOrContinue (Either IrredCt a))
    forall a.
    CtEvidence
    -> Type -> Type -> TcS (StopOrContinue (Either IrredCt a))
    canEqHardFailure CtEvidence
    ev Type
    ty1 Type
    ty2
    canEqSoftFailure CtEvidence
    ev EqRel
    ReprEq Type
    ty1 Type
    ty2
      = do { (redn1, rewriters1) <- CtEvidence -> Type -> TcS (Reduction, RewriterSet)
    rewrite CtEvidence
    ev Type
    ty1
           ; (redn2, rewriters2) <- rewrite ev ty2
                -- We must rewrite the types before putting them in the
                -- inert set, so that we are sure to kick them out when
                -- new equalities become available
           ; traceTcS "canEqSoftFailure with ReprEq" $
             vcat [ ppr ev, ppr redn1, ppr redn2 ]
           ; new_ev <- rewriteEqEvidence (rewriters1 S.<> rewriters2) ev NotSwapped redn1 redn2
           ; finishCanWithIrred ReprEqReason new_ev }

    -- | Call when canonicalizing an equality fails with utterly no hope.
    canEqHardFailure :: CtEvidence -> TcType -> TcType
                     -> TcS (StopOrContinue (Either IrredCt a))
    -- See Note [Make sure that insolubles are fully rewritten]
    canEqHardFailure :: forall a.
    CtEvidence
    -> Type -> Type -> TcS (StopOrContinue (Either IrredCt a))
    canEqHardFailure CtEvidence
    ev Type
    ty1 Type
    ty2
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "canEqHardFailure" (Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty1 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    ty2)
           ; (redn1, rewriters1) <- CtEvidence -> Type -> TcS (Reduction, RewriterSet)
    rewriteForErrors CtEvidence
    ev Type
    ty1
           ; (redn2, rewriters2) <- rewriteForErrors ev ty2
           ; new_ev <- rewriteEqEvidence (rewriters1 S.<> rewriters2) ev NotSwapped redn1 redn2
           ; finishCanWithIrred ShapeMismatchReason new_ev }

    {-
    Note [Canonicalising type applications]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Given (s1 t1) ~ ty2, how should we proceed?
    The simple thing is to see if ty2 is of form (s2 t2), and
    decompose.

    However, over-eager decomposition gives bad error messages
    for things like
       a b ~ Maybe c
       e f ~ p -> q
    Suppose (in the first example) we already know a~Array.  Then if we
    decompose the application eagerly, yielding
       a ~ Maybe
       b ~ c
    we get an error        "Can't match Array ~ Maybe",
    but we'd prefer to get "Can't match Array b ~ Maybe c".

    So instead can_eq_wanted_app rewrites the LHS and RHS, in the hope of
    replacing (a b) by (Array b), before using try_decompose_app to
    decompose it.

    Note [Make sure that insolubles are fully rewritten]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    When an equality fails, we still want to rewrite the equality
    all the way down, so that it accurately reflects
     (a) the mutable reference substitution in force at start of solving
     (b) any ty-binds in force at this point in solving
    See Note [Rewrite insolubles] in GHC.Tc.Solver.InertSet.
    And if we don't do this there is a bad danger that
    GHC.Tc.Solver.applyTyVarDefaulting will find a variable
    that has in fact been substituted.

    Note [Do not decompose Given polytype equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider [G] (forall a. t1 ~ forall a. t2).  Can we decompose this?
    No -- what would the evidence look like?  So instead we simply discard
    this given evidence.

    Note [No top-level newtypes on RHS of representational equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose we're in this situation:

     work item:  [W] c1 : a ~R b
         inert:  [G] c2 : b ~R Id a

    where
      newtype Id a = Id a

    We want to make sure canEqCanLHS sees [W] a ~R a, after b is rewritten
    and the Id newtype is unwrapped. This is assured by requiring only rewritten
    types in canEqCanLHS *and* having the newtype-unwrapping check above
    the tyvar check in can_eq_nc.

    Note that this only applies to saturated applications of newtype TyCons, as
    we can't rewrite an unsaturated application. See for example T22310, where
    we ended up with:

      newtype Compose f g a = ...

      [W] t[tau] ~# Compose Foo Bar

    Note [Put touchable variables on the left]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Ticket #10009, a very nasty example:

        f :: (UnF (F b) ~ b) => F b -> ()

        g :: forall a. (UnF (F a) ~ a) => a -> ()
        g _ = f (undefined :: F a)

    For g we get [G]  g1 : UnF (F a) ~ a
                 [W] w1 : UnF (F beta) ~ beta
                 [W] w2 : F a ~ F beta

    g1 is canonical (CEqCan). It is oriented as above because a is not touchable.
    See canEqTyVarFunEq.

    w1 is similarly canonical, though the occurs-check in canEqTyVarFunEq is key
    here.

    w2 is canonical. But which way should it be oriented? As written, we'll be
    stuck. When w2 is added to the inert set, nothing gets kicked out: g1 is
    a Given (and Wanteds don't rewrite Givens), and w2 doesn't mention the LHS
    of w2. We'll thus lose.

    But if w2 is swapped around, to

        [W] w3 : F beta ~ F a

    then we'll kick w1 out of the inert set (it mentions the LHS of w3). We then
    rewrite w1 to

        [W] w4 : UnF (F a) ~ beta

    and then, using g1, to

        [W] w5 : a ~ beta

    at which point we can unify and go on to glory. (This rewriting actually
    happens all at once, in the call to rewrite during canonicalisation.)

    But what about the new LHS makes it better? It mentions a variable (beta)
    that can appear in a Wanted -- a touchable metavariable never appears
    in a Given. On the other hand, the original LHS mentioned only variables
    that appear in Givens. We thus choose to put variables that can appear
    in Wanteds on the left.

    Ticket #12526 is another good example of this in action.

    -}

    ---------------------
    canEqCanLHS :: CtEvidence            -- ev :: lhs ~ rhs
                -> EqRel -> SwapFlag
                -> CanEqLHS              -- lhs (or, if swapped, rhs)
                -> TcType                -- lhs: pretty lhs, already rewritten
                -> TcType -> TcType      -- rhs: already rewritten
                -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHS :: CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHS CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs1 Type
    ps_xi1 Type
    xi2 Type
    ps_xi2
      | Type
    k1 HasDebugCallStack => Type -> Type -> Bool
    Type -> Type -> Bool
    `tcEqType` Type
    k2
      = CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSHomo CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs1 Type
    ps_xi1 Type
    xi2 Type
    ps_xi2

      | Bool
    otherwise
      = CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> Type
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSHetero CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs1 Type
    ps_xi1 Type
    k1 Type
    xi2 Type
    ps_xi2 Type
    k2

      where
        k1 :: Type
    k1 = CanEqLHS -> Type
    canEqLHSKind CanEqLHS
    lhs1
        k2 :: Type
    k2 = HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    xi2

    {-
    Note [Kind Equality Orientation]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    While in theory [W] x ~ y and [W] y ~ x ought to give us the same behaviour, in
    practice it does not.  See Note [Fundeps with instances, and equality
    orientation] where this is discussed at length.  As a rule of thumb: we keep
    the newest unification variables on the left of the equality.  See also
    Note [Improvement orientation].

    In particular, `canEqCanLHSHetero` produces the following constraint equalities

    [X] (xi1 :: ki1) ~ (xi2 :: ki2)
      -->  [X] kco :: ki1 ~ ki2
           [X] co : xi1 :: ki1 ~ (xi2 |> sym kco) :: ki1

    Note that the types in the LHS of the new constraints are the ones that were on the LHS of
    the original constraint.

    --- Historical note ---
    We prevously used to flip the kco to avoid using a sym in the cast

    [X] (xi1 :: ki1) ~ (xi2 :: ki2)
      -->  [X] kco :: ki2 ~ ki1
           [X] co : xi1 :: ki1 ~ (xi2 |> kco) :: ki1

    But this sent solver in an infinite loop (see #19415).
    -- End of historical note --
    -}

    canEqCanLHSHetero :: CtEvidence         -- :: (xi1 :: ki1) ~ (xi2 :: ki2)
                                            --    (or reversed if SwapFlag=IsSwapped)
                      -> EqRel -> SwapFlag
                      -> CanEqLHS -> TcType -- xi1
                      -> TcKind             -- ki1
                      -> TcType -> TcType   -- xi2
                      -> TcKind             -- ki2
                      -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSHetero :: CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> Type
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSHetero CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs1 Type
    ps_xi1 Type
    ki1 Type
    xi2 Type
    ps_xi2 Type
    ki2
    -- See Note [Equalities with incompatible kinds]
    -- See Note [Kind Equality Orientation]

    -- NB: preserve left-to-right orientation!! See wrinkle (W2) in
    -- Note [Fundeps with instances, and equality orientation] in GHC.Tc.Solver.Dict
    --    NotSwapped:
    --        ev      :: (lhs1:ki1) ~r# (xi2:ki2)
    --        kind_co :: k11 ~# ki2               -- Same orientation as ev
    --        type_ev :: lhs1 ~r# (xi2 |> sym kind_co)
    --    Swapped
    --        ev      :: (xi2:ki2) ~r# (lhs1:ki1)
    --        kind_co :: ki2 ~# ki1               -- Same orientation as ev
    --        type_ev :: (xi2 |> kind_co) ~r# lhs1

      = do { (kind_co, rewriters, unifs_happened) <- TcS (TcCoercion, RewriterSet, Bool)
    mk_kind_eq   -- :: ki1 ~N ki2
           ; if unifs_happened
                  -- Unifications happened, so start again to do the zonking
                  -- Otherwise we might put something in the inert set that isn't inert
             then startAgainWith (mkNonCanonical ev)
             else
        do { let lhs_redn = Role -> Type -> Reduction
    mkReflRedn Role
    role Type
    ps_xi1
                 rhs_redn = Role -> Type -> TcCoercion -> Reduction
    mkGReflRightRedn Role
    role Type
    xi2 TcCoercion
    mb_sym_kind_co
                 mb_sym_kind_co = case SwapFlag
    swapped of
                                    SwapFlag
    NotSwapped -> TcCoercion -> TcCoercion
    mkSymCo TcCoercion
    kind_co
                                    SwapFlag
    IsSwapped  -> TcCoercion
    kind_co

           ; traceTcS "Hetero equality gives rise to kind equality"
               (ppr swapped $$
                ppr kind_co <+> dcolon <+> sep [ ppr ki1, text "~#", ppr ki2 ])
           ; type_ev <- rewriteEqEvidence rewriters ev swapped lhs_redn rhs_redn

           ; let new_xi2 = Type -> TcCoercion -> Type
    mkCastTy Type
    ps_xi2 TcCoercion
    mb_sym_kind_co
           ; canEqCanLHSHomo type_ev eq_rel NotSwapped lhs1 ps_xi1 new_xi2 new_xi2 }}

      where
        mk_kind_eq :: TcS (CoercionN, RewriterSet, Bool)
        -- Returned kind_co has kind (k1 ~ k2) if NotSwapped, (k2 ~ k1) if Swapped
        -- Returned Bool = True if unifications happened, so we should retry
        mk_kind_eq :: TcS (TcCoercion, RewriterSet, Bool)
    mk_kind_eq = case CtEvidence
    ev of
          CtGiven { ctev_evar :: CtEvidence -> TyVar
    ctev_evar = TyVar
    evar }
            -> do { let kind_co :: TcCoercion
    kind_co  = TcCoercion -> TcCoercion
    mkKindCo (TyVar -> TcCoercion
    mkCoVarCo TyVar
    evar)
                        pred_ty :: Type
    pred_ty  = SwapFlag -> (Type -> Type -> Type) -> Type -> Type -> Type
    forall a b. SwapFlag -> (a -> a -> b) -> a -> a -> b
    unSwap SwapFlag
    swapped (Type -> Type -> Type -> Type
    mkNomPrimEqPred Type
    liftedTypeKind) Type
    ki1 Type
    ki2
                        kind_loc :: CtLoc
    kind_loc = Type -> Type -> CtLoc -> CtLoc
    mkKindEqLoc Type
    xi1 Type
    xi2 (CtEvidence -> CtLoc
    ctev_loc CtEvidence
    ev)
                  ; kind_ev <- CtLoc -> (Type, EvTerm) -> TcS CtEvidence
    newGivenEvVar CtLoc
    kind_loc (Type
    pred_ty, TcCoercion -> EvTerm
    evCoercion TcCoercion
    kind_co)
                  ; emitWorkNC [kind_ev]
                  ; return (ctEvCoercion kind_ev, emptyRewriterSet, False) }

          CtWanted {}
            -> do { (kind_co, cts, unifs) <- CtEvidence
    -> Role
    -> (UnifyEnv -> TcM TcCoercion)
    -> TcS (TcCoercion, Cts, [TyVar])
    forall a.
    CtEvidence -> Role -> (UnifyEnv -> TcM a) -> TcS (a, Cts, [TyVar])
    wrapUnifierTcS CtEvidence
    ev Role
    Nominal ((UnifyEnv -> TcM TcCoercion) -> TcS (TcCoercion, Cts, [TyVar]))
    -> (UnifyEnv -> TcM TcCoercion) -> TcS (TcCoercion, Cts, [TyVar])
    forall a b. (a -> b) -> a -> b
    $ \UnifyEnv
    uenv ->
                                             let uenv' :: UnifyEnv
    uenv' = UnifyEnv -> (CtLoc -> CtLoc) -> UnifyEnv
    updUEnvLoc UnifyEnv
    uenv (Type -> Type -> CtLoc -> CtLoc
    mkKindEqLoc Type
    xi1 Type
    xi2)
                                             in SwapFlag
    -> (Type -> Type -> TcM TcCoercion)
    -> Type
    -> Type
    -> TcM TcCoercion
    forall a b. SwapFlag -> (a -> a -> b) -> a -> a -> b
    unSwap SwapFlag
    swapped (UnifyEnv -> Type -> Type -> TcM TcCoercion
    uType UnifyEnv
    uenv') Type
    ki1 Type
    ki2
                  ; return (kind_co, rewriterSetFromCts cts, not (null unifs)) }

        xi1 :: Type
    xi1  = CanEqLHS -> Type
    canEqLHSType CanEqLHS
    lhs1
        role :: Role
    role = EqRel -> Role
    eqRelRole EqRel
    eq_rel

    canEqCanLHSHomo :: CtEvidence          -- lhs ~ rhs
                                           -- or, if swapped: rhs ~ lhs
                    -> EqRel -> SwapFlag
                    -> CanEqLHS -> TcType  -- lhs, pretty lhs
                    -> TcType   -> TcType  -- rhs, pretty rhs
                    -> TcS (StopOrContinue (Either IrredCt EqCt))
    -- Guaranteed that typeKind lhs == typeKind rhs
    canEqCanLHSHomo :: CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> Type
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSHomo CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs1 Type
    ps_xi1 Type
    xi2 Type
    ps_xi2
      | (Type
    xi2', MCoercion
    mco) <- Type -> (Type, MCoercion)
    split_cast_ty Type
    xi2
      , Just CanEqLHS
    lhs2 <- Type -> Maybe CanEqLHS
    canEqLHS_maybe Type
    xi2'
      = CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> CanEqLHS
    -> Type
    -> MCoercion
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHS2 CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs1 Type
    ps_xi1 CanEqLHS
    lhs2 (Type
    ps_xi2 Type -> MCoercion -> Type
    `mkCastTyMCo` MCoercion -> MCoercion
    mkSymMCo MCoercion
    mco) MCoercion
    mco

      | Bool
    otherwise
      = CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSFinish CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs1 Type
    ps_xi2

      where
        split_cast_ty :: Type -> (Type, MCoercion)
    split_cast_ty (CastTy Type
    ty TcCoercion
    co) = (Type
    ty, TcCoercion -> MCoercion
    MCo TcCoercion
    co)
        split_cast_ty Type
    other          = (Type
    other, MCoercion
    MRefl)

    -- This function deals with the case that both LHS and RHS are potential
    -- CanEqLHSs.
    canEqCanLHS2 :: CtEvidence              -- lhs ~ (rhs |> mco)
                                            -- or, if swapped: (rhs |> mco) ~ lhs
                 -> EqRel -> SwapFlag
                 -> CanEqLHS                -- lhs (or, if swapped, rhs)
                 -> TcType                  -- pretty lhs
                 -> CanEqLHS                -- rhs
                 -> TcType                  -- pretty rhs
                 -> MCoercion               -- :: kind(rhs) ~N kind(lhs)
                 -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHS2 :: CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> CanEqLHS
    -> Type
    -> MCoercion
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHS2 CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs1 Type
    ps_xi1 CanEqLHS
    lhs2 Type
    ps_xi2 MCoercion
    mco
      | CanEqLHS
    lhs1 CanEqLHS -> CanEqLHS -> Bool
    `eqCanEqLHS` CanEqLHS
    lhs2
        -- It must be the case that mco is reflexive
      = CtEvidence
    -> EqRel -> Type -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall a. CtEvidence -> EqRel -> Type -> TcS (StopOrContinue a)
    canEqReflexive CtEvidence
    ev EqRel
    eq_rel Type
    lhs1_ty

      | TyVarLHS TyVar
    tv1 <- CanEqLHS
    lhs1
      , TyVarLHS TyVar
    tv2 <- CanEqLHS
    lhs2
      = -- See Note [TyVar/TyVar orientation] in GHC.Tc.Utils.Unify
        do { String -> SDoc -> TcS ()
    traceTcS String
    "canEqLHS2 swapOver" (TyVar -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyVar
    tv1 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ TyVar -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyVar
    tv2 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ SwapFlag -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr SwapFlag
    swapped)
           ; if Bool -> TyVar -> TyVar -> Bool
    swapOverTyVars (CtEvidence -> Bool
    isGiven CtEvidence
    ev) TyVar
    tv1 TyVar
    tv2
             then TcS (StopOrContinue (Either IrredCt EqCt))
    finish_with_swapping
             else TcS (StopOrContinue (Either IrredCt EqCt))
    finish_without_swapping }

      | TyVarLHS {} <- CanEqLHS
    lhs1
      , TyFamLHS {} <- CanEqLHS
    lhs2
      = if Bool
    put_tyvar_on_lhs
        then TcS (StopOrContinue (Either IrredCt EqCt))
    finish_without_swapping
        else TcS (StopOrContinue (Either IrredCt EqCt))
    finish_with_swapping

      | TyFamLHS {} <- CanEqLHS
    lhs1
      , TyVarLHS {} <- CanEqLHS
    lhs2
      = if Bool
    put_tyvar_on_lhs
        then TcS (StopOrContinue (Either IrredCt EqCt))
    finish_with_swapping
        else TcS (StopOrContinue (Either IrredCt EqCt))
    finish_without_swapping

      | TyFamLHS TyCon
    fun_tc1 [Type]
    fun_args1 <- CanEqLHS
    lhs1
      , TyFamLHS TyCon
    fun_tc2 [Type]
    fun_args2 <- CanEqLHS
    lhs2
      -- See Note [Decomposing type family applications]
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "canEqCanLHS2 two type families" (CanEqLHS -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CanEqLHS
    lhs1 SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ CanEqLHS -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CanEqLHS
    lhs2)

           ; unifications_done <- CtEvidence
    -> EqRel
    -> TyCon
    -> [Type]
    -> TyCon
    -> [Type]
    -> MCoercion
    -> TcS Bool
    tryFamFamInjectivity CtEvidence
    ev EqRel
    eq_rel
                                       TyCon
    fun_tc1 [Type]
    fun_args1 TyCon
    fun_tc2 [Type]
    fun_args2 MCoercion
    mco
           ; if unifications_done
             then -- Go round again, since the unifications affect lhs/rhs
                  startAgainWith (mkNonCanonical ev)
             else
        do { tclvl <- getTcLevel
           ; let tvs1 = [Type] -> TyCoVarSet
    tyCoVarsOfTypes [Type]
    fun_args1
                 tvs2 = [Type] -> TyCoVarSet
    tyCoVarsOfTypes [Type]
    fun_args2

                 -- See Note [Orienting TyFamLHS/TyFamLHS]
                 swap_for_size = [Type] -> Int
    typesSize [Type]
    fun_args2 Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > [Type] -> Int
    typesSize [Type]
    fun_args1

                 -- See Note [Orienting TyFamLHS/TyFamLHS]
                 meta_tv_lhs = (TyVar -> Bool) -> TyCoVarSet -> Bool
    anyVarSet (TcLevel -> TyVar -> Bool
    isTouchableMetaTyVar TcLevel
    tclvl) TyCoVarSet
    tvs1
                 meta_tv_rhs = (TyVar -> Bool) -> TyCoVarSet -> Bool
    anyVarSet (TcLevel -> TyVar -> Bool
    isTouchableMetaTyVar TcLevel
    tclvl) TyCoVarSet
    tvs2
                 swap_for_rewriting = Bool
    meta_tv_rhs Bool -> Bool -> Bool
    && Bool -> Bool
    not Bool
    meta_tv_lhs
                                      -- See Note [Put touchable variables on the left]
                                      -- This second check is just to avoid unfruitful swapping

             -- It's important that we don't flip-flop (#T24134)
             -- So swap_for_rewriting "wins", and we only try swap_for_size
             -- if swap_for_rewriting doesn't care either way
           ; if swap_for_rewriting || (meta_tv_lhs == meta_tv_rhs && swap_for_size)
             then finish_with_swapping
             else finish_without_swapping } }
      where
        sym_mco :: MCoercion
    sym_mco = MCoercion -> MCoercion
    mkSymMCo MCoercion
    mco
        role :: Role
    role    = EqRel -> Role
    eqRelRole EqRel
    eq_rel
        lhs1_ty :: Type
    lhs1_ty  = CanEqLHS -> Type
    canEqLHSType CanEqLHS
    lhs1
        lhs2_ty :: Type
    lhs2_ty  = CanEqLHS -> Type
    canEqLHSType CanEqLHS
    lhs2

        finish_without_swapping :: TcS (StopOrContinue (Either IrredCt EqCt))
    finish_without_swapping
          = CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSFinish CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs1 (Type
    ps_xi2 Type -> MCoercion -> Type
    `mkCastTyMCo` MCoercion
    mco)

        -- Swapping. We have   ev : lhs1 ~ lhs2 |> co
        -- We swap to      new_ev : lhs2 ~ lhs1 |> sym co
        --                     ev = grefl1 ; sym new_ev ; grefl2
        --      where grefl1 : lhs1 ~ lhs1 |> sym co
        --            grefl2 : lhs2 ~ lhs2 |> co
        finish_with_swapping :: TcS (StopOrContinue (Either IrredCt EqCt))
    finish_with_swapping
          = do { let lhs1_redn :: Reduction
    lhs1_redn = Role -> Type -> MCoercion -> Reduction
    mkGReflRightMRedn Role
    role Type
    lhs1_ty MCoercion
    sym_mco
                     lhs2_redn :: Reduction
    lhs2_redn = Role -> Type -> MCoercion -> Reduction
    mkGReflLeftMRedn  Role
    role Type
    lhs2_ty MCoercion
    mco
               ; new_ev <-RewriterSet
    -> CtEvidence
    -> SwapFlag
    -> Reduction
    -> Reduction
    -> TcS CtEvidence
    rewriteEqEvidence RewriterSet
    emptyRewriterSet CtEvidence
    ev SwapFlag
    swapped Reduction
    lhs1_redn Reduction
    lhs2_redn
               ; canEqCanLHSFinish new_ev eq_rel IsSwapped lhs2 (ps_xi1 `mkCastTyMCo` sym_mco) }

        put_tyvar_on_lhs :: Bool
    put_tyvar_on_lhs = CtEvidence -> Bool
    isWanted CtEvidence
    ev Bool -> Bool -> Bool
    && EqRel
    eq_rel EqRel -> EqRel -> Bool
    forall a. Eq a => a -> a -> Bool
    == EqRel
    NomEq
        -- See Note [Orienting TyVarLHS/TyFamLHS]
        -- Same conditions as for canEqCanLHSFinish_try_unification
        -- which we are setting ourselves up for here

    {- Note [Orienting TyVarLHS/TyFamLHS]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    What if one side is a TyVarLHS and the other is a TyFamLHS, (a ~ F tys)?
    Which to put on the left?  Answer:

    * If there is no chance of unifying, put the type family on the left,
      (F tys ~ a), because it's generally better to rewrite away function
      calls.  See `put_tyvar_on_lhs` in canEqCanLHS2; and
      Note [Orienting TyVarLHS/TyFamLHS]

    * But if there /is/ a chance of unifying, put the tyvar on the left,
      (a ~ F tys), as this may be our only shot to unify. Again see
      `put_tyvar_on_lhs`.

    * But if we /fail/ to unify then flip back to (F tys ~ a) because it's
      generally better to rewrite away function calls. See the call to
      `swapAndFinish` in `canEqCanLHSFinish_try_unification`

      It's important to flip back. Consider
        [W] F alpha ~ alpha
        [W] F alpha ~ beta
        [W] G alpha beta ~ Int   ( where we have type instance G a a = a )
      If we end up with a stuck alpha ~ F alpha, we won't be able to solve this.
      Test case: indexed-types/should_compile/CEqCanOccursCheck

    Note [Orienting TyFamLHS/TyFamLHS]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    If we have a TyFamLHS on both sides, we choose how to orient it.

    * swap_for_size.  If we have
          S a ~ F (G (H (Maybe a)))
      then we swap so that we tend to rewrite the bigger type (F (G (H (Maybe a))))
      into the smaller one (S a).  This same test tends to avoid occurs-check
      errors.  E.g.
          S g ~ F (G (S g))
      Here (S g) occurs on the RHS, so this is not canonical.  But if we swap it
      around, it /is/ canonical
          F (G (S g)) ~ S g

    * swap_for_rewriting: put touchable meta-tyvars on the left:
      see Note [Put touchable variables on the left]
    -}

    -- The RHS here is either not CanEqLHS, or it's one that we
    -- want to rewrite the LHS to (as per e.g. swapOverTyVars)
    canEqCanLHSFinish, canEqCanLHSFinish_try_unification,
                       canEqCanLHSFinish_no_unification
        :: CtEvidence           -- (lhs ~ rhs) or if swapped (rhs ~ lhs)
        -> EqRel -> SwapFlag
        -> CanEqLHS             -- lhs
        -> TcType               -- rhs
        -> TcS (StopOrContinue (Either IrredCt EqCt))
        -- RHS is fully rewritten, but with type synonyms
        --   preserved as much as possible
        -- Guaranteed preconditions that
        --    (TyEq:K)  handled in canEqCanLHSHomo
        --    (TyEq:N)  checked in can_eq_nc
        --    (TyEq:TV) handled in canEqCanLHS2

    ---------------------------
    canEqCanLHSFinish :: CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSFinish CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs Type
    rhs
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "canEqCanLHSFinish" (SDoc -> TcS ()) -> SDoc -> TcS ()
    forall a b. (a -> b) -> a -> b
    $
             [SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "ev:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> CtEvidence -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CtEvidence
    ev
                  , String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "swapped:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> SwapFlag -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr SwapFlag
    swapped
                  , String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "lhs:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> CanEqLHS -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CanEqLHS
    lhs
                  , String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "rhs:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    rhs ]

             -- Assertion: (TyEq:K) is already satisfied
           ; Bool -> TcS ()
    forall (m :: * -> *). (HasCallStack, Applicative m) => Bool -> m ()
    massert (CanEqLHS -> Type
    canEqLHSKind CanEqLHS
    lhs Type -> Type -> Bool
    `eqType` HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    rhs)

             -- Assertion: (TyEq:N) is already satisfied (if applicable)
           ; TcS Bool -> SDoc -> TcS ()
    forall (m :: * -> *).
    (HasCallStack, Monad m) =>
    m Bool -> SDoc -> m ()
    assertPprM TcS Bool
    ty_eq_N_OK (SDoc -> TcS ()) -> SDoc -> TcS ()
    forall a b. (a -> b) -> a -> b
    $
               [SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "CanEqCanLHSFinish: (TyEq:N) not satisfied"
                    , String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "rhs:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    rhs ]

           ; CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSFinish_try_unification CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs Type
    rhs }

      where
        -- This is about (TyEq:N): check that we don't have a saturated application
        -- of a newtype TyCon at the top level of the RHS, if the constructor
        -- of the newtype is in scope.
        ty_eq_N_OK :: TcS Bool
        ty_eq_N_OK :: TcS Bool
    ty_eq_N_OK
          | EqRel
    ReprEq <- EqRel
    eq_rel
          , Just (TyCon
    tc, [Type]
    tc_args) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
    Type -> Maybe (TyCon, [Type])
    splitTyConApp_maybe Type
    rhs
          , Just DataCon
    con <- TyCon -> Maybe DataCon
    newTyConDataCon_maybe TyCon
    tc
          -- #22310: only a problem if the newtype TyCon is saturated.
          , [Type]
    tc_args [Type] -> Int -> Bool
    forall a. [a] -> Int -> Bool
    `lengthAtLeast` TyCon -> Int
    tyConArity TyCon
    tc
          -- #21010: only a problem if the newtype constructor is in scope.
          = do { rdr_env <- TcS GlobalRdrEnv
    getGlobalRdrEnvTcS
               ; let con_in_scope = Maybe GlobalRdrElt -> Bool
    forall a. Maybe a -> Bool
    isJust (Maybe GlobalRdrElt -> Bool) -> Maybe GlobalRdrElt -> Bool
    forall a b. (a -> b) -> a -> b
    $ GlobalRdrEnv -> Name -> Maybe GlobalRdrElt
    forall info.
    Outputable info =>
    GlobalRdrEnvX info -> Name -> Maybe (GlobalRdrEltX info)
    lookupGRE_Name GlobalRdrEnv
    rdr_env (DataCon -> Name
    dataConName DataCon
    con)
               ; return $ not con_in_scope }
          | Bool
    otherwise
          = Bool -> TcS Bool
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    True

    -----------------------
    canEqCanLHSFinish_try_unification :: CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSFinish_try_unification CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs Type
    rhs
      -- Try unification; for Wanted, Nominal equalities with a meta-tyvar on the LHS
      | CtEvidence -> Bool
    isWanted CtEvidence
    ev      -- See Note [Do not unify Givens]
      , EqRel
    NomEq <- EqRel
    eq_rel  -- See Note [Do not unify representational equalities]
      , TyVarLHS TyVar
    tv <- CanEqLHS
    lhs
      = do { given_eq_lvl <- TcS TcLevel
    getInnermostGivenEqLevel
           ; if not (touchabilityAndShapeTest given_eq_lvl tv rhs)
             then if | Just can_rhs <- canTyFamEqLHS_maybe rhs
                     -> swapAndFinish ev eq_rel swapped (mkTyVarTy tv) can_rhs
                        -- See Note [Orienting TyVarLHS/TyFamLHS]

                     | otherwise
                     -> canEqCanLHSFinish_no_unification ev eq_rel swapped lhs rhs
             else

        -- We have a touchable unification variable on the left
        do { check_result <- checkTouchableTyVarEq ev tv rhs
           ; case check_result of {
                PuFail CheckTyEqResult
    reason
                  | Just CanEqLHS
    can_rhs <- Type -> Maybe CanEqLHS
    canTyFamEqLHS_maybe Type
    rhs
                  -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> Type
    -> CanEqLHS
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall unused.
    CtEvidence
    -> EqRel
    -> SwapFlag
    -> Type
    -> CanEqLHS
    -> TcS (StopOrContinue (Either unused EqCt))
    swapAndFinish CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped (TyVar -> Type
    mkTyVarTy TyVar
    tv) CanEqLHS
    can_rhs
                    -- Swap back: see Note [Orienting TyVarLHS/TyFamLHS]

                  | CheckTyEqResult
    reason CheckTyEqResult -> CheckTyEqResult -> Bool
    `cterHasOnlyProblems` CheckTyEqResult
    do_not_prevent_rewriting
                  -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSFinish_no_unification CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs Type
    rhs

                  | Bool
    otherwise
                  -> CheckTyEqResult
    -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall unused.
    CheckTyEqResult
    -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt unused))
    tryIrredInstead CheckTyEqResult
    reason CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs Type
    rhs ;

                PuOK Bag ()
    _ Reduction
    rhs_redn ->

        -- Success: we can solve by unification
        do { -- In the common case where rhs_redn is Refl, we don't need to rewrite
             -- the evidence, even if swapped=IsSwapped.   Suppose the original was
             --     [W] co : Int ~ alpha
             -- We unify alpha := Int, and set co := <Int>.  No need to
             -- swap to   co = sym co'
             --           co' = <Int>
             new_ev <- if TcCoercion -> Bool
    isReflCo (Reduction -> TcCoercion
    reductionCoercion Reduction
    rhs_redn)
                       then CtEvidence -> TcS CtEvidence
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return CtEvidence
    ev
                       else RewriterSet
    -> CtEvidence
    -> SwapFlag
    -> Reduction
    -> Reduction
    -> TcS CtEvidence
    rewriteEqEvidence RewriterSet
    emptyRewriterSet CtEvidence
    ev SwapFlag
    swapped
                                (Role -> Type -> Reduction
    mkReflRedn Role
    Nominal (TyVar -> Type
    mkTyVarTy TyVar
    tv)) Reduction
    rhs_redn

           ; let tv_ty     = TyVar -> Type
    mkTyVarTy TyVar
    tv
                 final_rhs = Reduction -> Type
    reductionReducedType Reduction
    rhs_redn

           ; traceTcS "Sneaky unification:" $
             vcat [text "Unifies:" <+> ppr tv <+> text ":=" <+> ppr final_rhs,
                   text "Coercion:" <+> pprEq tv_ty final_rhs,
                   text "Left Kind is:" <+> ppr (typeKind tv_ty),
                   text "Right Kind is:" <+> ppr (typeKind final_rhs) ]

           -- Update the unification variable itself
           ; unifyTyVar tv final_rhs

           -- Provide Refl evidence for the constraint
           -- Ignore 'swapped' because it's Refl!
           ; setEvBindIfWanted new_ev True $
             evCoercion (mkNomReflCo final_rhs)

           -- Kick out any constraints that can now be rewritten
           ; kickOutAfterUnification [tv]

           ; return (Stop new_ev (text "Solved by unification")) }}}}

      -- Otherwise unification is off the table
      | Bool
    otherwise
      = CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSFinish_no_unification CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs Type
    rhs

      where
        -- Some problems prevent /unification/ but not /rewriting/
        -- Skolem-escape: if we have [W] alpha[2] ~ Maybe b[3]
        --    we can't unify (skolem-escape); but it /is/ canonical,
        --    and hence we /can/ use it for rewriting
        -- Concrete-ness:  alpha[conc] ~ b[sk]
        --    We can use it to rewrite; we still have to solve the original
        do_not_prevent_rewriting :: CheckTyEqResult
        do_not_prevent_rewriting :: CheckTyEqResult
    do_not_prevent_rewriting = CheckTyEqProblem -> CheckTyEqResult
    cteProblem CheckTyEqProblem
    cteSkolemEscape CheckTyEqResult -> CheckTyEqResult -> CheckTyEqResult
    forall a. Semigroup a => a -> a -> a
    S.<>
                                   CheckTyEqProblem -> CheckTyEqResult
    cteProblem CheckTyEqProblem
    cteConcrete

    ---------------------------
    -- Unification is off the table
    canEqCanLHSFinish_no_unification :: CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    canEqCanLHSFinish_no_unification CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs Type
    rhs
      = do { -- Do checkTypeEq to guarantee (TyEq:OC), (TyEq:F)
             -- Must do the occurs check even on tyvar/tyvar equalities,
             -- in case have  x ~ (y :: ..x...); this is #12593.
           ; check_result <- CtEvidence
    -> EqRel -> CanEqLHS -> Type -> TcS (PuResult () Reduction)
    checkTypeEq CtEvidence
    ev EqRel
    eq_rel CanEqLHS
    lhs Type
    rhs

           ; let lhs_ty = CanEqLHS -> Type
    canEqLHSType CanEqLHS
    lhs
           ; case check_result of
                PuFail CheckTyEqResult
    reason

                  -- If we had F a ~ G (F a), which gives an occurs check,
                  -- then swap it to G (F a) ~ F a, which does not
                  -- However `swap_for_size` above will orient it with (G (F a)) on
                  -- the left anwyway.  `swap_for_rewriting` "wins", but that doesn't
                  -- matter: in the occurs check case swap_for_rewriting will be moot.
                  -- TL;DR: the next four lines of code are redundant
                  -- I'm leaving them here in case they become relevant again
    --              | TyFamLHS {} <- lhs
    --              , Just can_rhs <- canTyFamEqLHS_maybe rhs
    --              , reason `cterHasOnlyProblem` cteSolubleOccurs
    --              -> swapAndFinish ev eq_rel swapped lhs_ty can_rhs
    --              | otherwise

                  -> CheckTyEqResult
    -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt EqCt))
    forall unused.
    CheckTyEqResult
    -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt unused))
    tryIrredInstead CheckTyEqResult
    reason CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs Type
    rhs

                PuOK Bag ()
    _ Reduction
    rhs_redn
                  -> do { new_ev <- RewriterSet
    -> CtEvidence
    -> SwapFlag
    -> Reduction
    -> Reduction
    -> TcS CtEvidence
    rewriteEqEvidence RewriterSet
    emptyRewriterSet CtEvidence
    ev SwapFlag
    swapped
                                       (Role -> Type -> Reduction
    mkReflRedn (EqRel -> Role
    eqRelRole EqRel
    eq_rel) Type
    lhs_ty)
                                       Reduction
    rhs_redn

                        -- Important: even if the coercion is Refl,
                        --   * new_ev has reductionReducedType on the RHS
                        --   * eq_rhs is set to reductionReducedType
                        -- See Note [Forgetful synonyms in checkTyConApp] in GHC.Tc.Utils.Unify
                        ; continueWith $ Right $
                          EqCt { eq_ev  = new_ev, eq_eq_rel = eq_rel
                               , eq_lhs = lhs
                               , eq_rhs = reductionReducedType rhs_redn } } }

    ----------------------
    swapAndFinish :: CtEvidence -> EqRel -> SwapFlag
                  -> TcType -> CanEqLHS      -- ty ~ F tys
                  -> TcS (StopOrContinue (Either unused EqCt))
    -- We have an equality alpha ~ F tys, that we can't unify e.g because 'tys'
    -- mentions alpha, it would not be a canonical constraint as-is.
    -- We want to flip it to (F tys ~ a), whereupon it is canonical
    swapAndFinish :: forall unused.
    CtEvidence
    -> EqRel
    -> SwapFlag
    -> Type
    -> CanEqLHS
    -> TcS (StopOrContinue (Either unused EqCt))
    swapAndFinish CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped Type
    lhs_ty CanEqLHS
    can_rhs
      = do { let role :: Role
    role = EqRel -> Role
    eqRelRole EqRel
    eq_rel
           ; new_ev <- RewriterSet
    -> CtEvidence
    -> SwapFlag
    -> Reduction
    -> Reduction
    -> TcS CtEvidence
    rewriteEqEvidence RewriterSet
    emptyRewriterSet CtEvidence
    ev (SwapFlag -> SwapFlag
    flipSwap SwapFlag
    swapped)
                           (Role -> Type -> Reduction
    mkReflRedn Role
    role (CanEqLHS -> Type
    canEqLHSType CanEqLHS
    can_rhs))
                           (Role -> Type -> Reduction
    mkReflRedn Role
    role Type
    lhs_ty)
           ; continueWith $ Right $
             EqCt { eq_ev  = new_ev, eq_eq_rel = eq_rel
                  , eq_lhs = can_rhs, eq_rhs = lhs_ty } }

    ----------------------
    tryIrredInstead :: CheckTyEqResult -> CtEvidence
                    -> EqRel -> SwapFlag -> CanEqLHS -> TcType
                    -> TcS (StopOrContinue (Either IrredCt unused))
    -- We have a non-canonical equality
    -- We still swap it if 'swapped' says so, so that it is oriented
    -- in the direction that the error-reporting machinery
    -- expects it; e.g.  (m ~ t m) rather than (t m ~ m)
    -- This is not very important, and only affects error reporting.
    tryIrredInstead :: forall unused.
    CheckTyEqResult
    -> CtEvidence
    -> EqRel
    -> SwapFlag
    -> CanEqLHS
    -> Type
    -> TcS (StopOrContinue (Either IrredCt unused))
    tryIrredInstead CheckTyEqResult
    reason CtEvidence
    ev EqRel
    eq_rel SwapFlag
    swapped CanEqLHS
    lhs Type
    rhs
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "cantMakeCanonical" (CheckTyEqResult -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CheckTyEqResult
    reason SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ CanEqLHS -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CanEqLHS
    lhs SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    rhs)
           ; let role :: Role
    role = EqRel -> Role
    eqRelRole EqRel
    eq_rel
           ; new_ev <- RewriterSet
    -> CtEvidence
    -> SwapFlag
    -> Reduction
    -> Reduction
    -> TcS CtEvidence
    rewriteEqEvidence RewriterSet
    emptyRewriterSet CtEvidence
    ev SwapFlag
    swapped
                           (Role -> Type -> Reduction
    mkReflRedn Role
    role (CanEqLHS -> Type
    canEqLHSType CanEqLHS
    lhs))
                           (Role -> Type -> Reduction
    mkReflRedn Role
    role Type
    rhs)
           ; finishCanWithIrred (NonCanonicalReason reason) new_ev }

    finishCanWithIrred :: CtIrredReason -> CtEvidence
                       -> TcS (StopOrContinue (Either IrredCt a))
    finishCanWithIrred :: forall a.
    CtIrredReason
    -> CtEvidence -> TcS (StopOrContinue (Either IrredCt a))
    finishCanWithIrred CtIrredReason
    reason CtEvidence
    ev
      = do { -- Abort fast if we have any insoluble Wanted constraints,
             -- and the TcS abort-if-insoluble flag is on.
             Bool -> TcS () -> TcS ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when (CtIrredReason -> Bool
    isInsolubleReason CtIrredReason
    reason) TcS ()
    tryEarlyAbortTcS

           ; Either IrredCt a -> TcS (StopOrContinue (Either IrredCt a))
    forall a. a -> TcS (StopOrContinue a)
    continueWith (Either IrredCt a -> TcS (StopOrContinue (Either IrredCt a)))
    -> Either IrredCt a -> TcS (StopOrContinue (Either IrredCt a))
    forall a b. (a -> b) -> a -> b
    $ IrredCt -> Either IrredCt a
    forall a b. a -> Either a b
    Left (IrredCt -> Either IrredCt a) -> IrredCt -> Either IrredCt a
    forall a b. (a -> b) -> a -> b
    $ IrredCt { ir_ev :: CtEvidence
    ir_ev = CtEvidence
    ev, ir_reason :: CtIrredReason
    ir_reason = CtIrredReason
    reason } }

    -----------------------
    -- | Solve a reflexive equality constraint
    canEqReflexive :: CtEvidence    -- ty ~ ty
                   -> EqRel
                   -> TcType        -- ty
                   -> TcS (StopOrContinue a)   -- always Stop
    canEqReflexive :: forall a. CtEvidence -> EqRel -> Type -> TcS (StopOrContinue a)
    canEqReflexive CtEvidence
    ev EqRel
    eq_rel Type
    ty
      = do { CtEvidence -> Bool -> EvTerm -> TcS ()
    setEvBindIfWanted CtEvidence
    ev Bool
    True (EvTerm -> TcS ()) -> EvTerm -> TcS ()
    forall a b. (a -> b) -> a -> b
    $
             TcCoercion -> EvTerm
    evCoercion (Role -> Type -> TcCoercion
    mkReflCo (EqRel -> Role
    eqRelRole EqRel
    eq_rel) Type
    ty)
           ; CtEvidence -> String -> TcS (StopOrContinue a)
    forall a. CtEvidence -> String -> TcS (StopOrContinue a)
    stopWith CtEvidence
    ev String
    "Solved by reflexivity" }

    {- Note [Equalities with incompatible kinds]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    What do we do when we have an equality

      (tv :: k1) ~ (rhs :: k2)

    where k1 and k2 differ? Easy: we create a coercion that relates k1 and
    k2 and use this to cast. To wit, from

      [X] (tv :: k1) ~ (rhs :: k2)

    (where [X] is [G] or [W]), we go to

      [X] co :: k1 ~ k2
      [X] (tv :: k1) ~ ((rhs |> sym co) :: k1)

    Wrinkles:

    (EIK1) When X is W, the new type-level wanted is effectively rewritten by the
         kind-level one. We thus include the kind-level wanted in the RewriterSet
         for the type-level one. See Note [Wanteds rewrite Wanteds] in GHC.Tc.Types.Constraint.
         This is done in canEqCanLHSHetero.

    (EIK2) Suppose we have [W] (a::Type) ~ (b::Type->Type). The above rewrite will produce
            [W] w  : a ~ (b |> kw)
            [W] kw : Type ~ (Type->Type)

         But we do /not/ want to regard `w` as canonical, and use it for rewriting
         other constraints: `kw` is insoluble, and replacing something of kind
         `Type` with something of kind `Type->Type` (even wrapped in an insouluble
         cast) does not help, and doing so turns out to lead to much worse error
         messages.  (In particular, if 'a' is a unification variable, we might
         unify, losing the tracking info that it depends on solving `kw`.)

         Conclusion: if a RHS contains a coercion hole arising from fixing a hetero-kinded
         equality, treat the equality (`w` in this case) as non-canonical, so that
           * It will not be used for unification
           * It will not be used for rewriting
         Instead, it lands in the inert_irreds in the inert set, awaiting solution of
         that `kw`.

         (EIK2a) We must later indeed unify if/when the kind-level wanted, `kw` gets
         solved. This is done in kickOutAfterFillingCoercionHole, which kicks out
         all equalities whose RHS mentions the filled-in coercion hole.  Note that
         it looks for type family equalities, too, because of the use of unifyTest
         in canEqTyVarFunEq.

         (EIK2b) What if the RHS mentions /other/ coercion holes?  How can that happen?  The
         main way is like this. Assume F :: forall k. k -> Type
            [W] kw : k  ~ Type
            [W] w  : a ~ F k t
         We can rewrite `w` with `kw` like this:
            [W] w' : a ~ F Type (t |> kw)
         The cast on the second argument of `F` is necessary to keep the appliation well-kinded.
         There is nothing special here; no reason not treat w' as canonical, and use it for
         rewriting. Indeed tests JuanLopez only typechecks if we do.  So we'd like to treat
         this kind of equality as canonical.

         Hence the ch_hetero_kind field in CoercionHole: it is True of constraints
         created by `canEqCanLHSHetero` to fix up hetero-kinded equalities; and False otherwise:

         * An equality constraint is non-canonical if it mentions a hetero-kind
           CoercionHole on the RHS.  See the `hasCoercionHoleCo` test in GHC.Tc.Utils.checkCo.

         * Hetero-kind CoercionHoles are created when the parent's CtOrigin is
           KindEqOrigin: see GHC.Tc.Utils.TcMType.newCoercionHole and friends.  We
           set this origin, via `mkKindLoc`, in `mk_kind_eq` in `canEqCanLHSHetero`.

    (EIK3) Suppose we have [W] (a :: k1) ~ (rhs :: k2). We duly follow the
         algorithm detailed here, producing [W] co :: k1 ~ k2, and adding
         [W] (a :: k1) ~ ((rhs |> sym co) :: k1) to the irreducibles. Some time
         later, we solve co, and fill in co's coercion hole. This kicks out
         the irreducible as described in (2).

         But now, during canonicalization, we see the cast and remove it, in
         canEqCast. By the time we get into canEqCanLHS, the equality is
         heterogeneous again, and the process repeats.

         To avoid this, we don't strip casts off a type if the other type in the
         equality is a CanEqLHS (the scenario above can happen with a type
         family, too. testcase: typecheck/should_compile/T13822).

         And this is an improvement regardless: because tyvars can, generally,
         unify with casted types, there's no reason to go through the work of
         stripping off the cast when the cast appears opposite a tyvar. This is
         implemented in the cast case of can_eq_nc.

    Historical note:

    We used to do this via emitting a Derived kind equality and then parking
    the heterogeneous equality as irreducible. But this new approach is much
    more direct. And it doesn't produce duplicate Deriveds (as the old one did).

    Note [Type synonyms and canonicalization]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    We treat type synonym applications as xi types, that is, they do not
    count as type function applications.  However, we do need to be a bit
    careful with type synonyms: like type functions they may not be
    generative or injective.  However, unlike type functions, they are
    parametric, so there is no problem in expanding them whenever we see
    them, since we do not need to know anything about their arguments in
    order to expand them; this is what justifies not having to treat them
    as specially as type function applications.  The thing that causes
    some subtleties is that we prefer to leave type synonym applications
    *unexpanded* whenever possible, in order to generate better error
    messages.

    If we encounter an equality constraint with type synonym applications
    on both sides, or a type synonym application on one side and some sort
    of type application on the other, we simply must expand out the type
    synonyms in order to continue decomposing the equality constraint into
    primitive equality constraints.  For example, suppose we have

      type F a = [Int]

    and we encounter the equality

      F a ~ [b]

    In order to continue we must expand F a into [Int], giving us the
    equality

      [Int] ~ [b]

    which we can then decompose into the more primitive equality
    constraint

      Int ~ b.

    However, if we encounter an equality constraint with a type synonym
    application on one side and a variable on the other side, we should
    NOT (necessarily) expand the type synonym, since for the purpose of
    good error messages we want to leave type synonyms unexpanded as much
    as possible.  Hence the ps_xi1, ps_xi2 argument passed to canEqCanLHS.

    Note [Type equality cycles]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider this situation (from indexed-types/should_compile/GivenLoop):

      instance C (Maybe b)
      *[G] a ~ Maybe (F a)
      [W] C a

    or (typecheck/should_compile/T19682b):

      instance C (a -> b)
      *[W] alpha ~ (Arg alpha -> Res alpha)
      [W] C alpha

    or (typecheck/should_compile/T21515):

      type family Code a
      *[G] Code a ~ '[ '[ Head (Head (Code a)) ] ]
      [W] Code a ~ '[ '[ alpha ] ]

    In order to solve the final Wanted, we must use the starred constraint
    for rewriting. But note that all starred constraints have occurs-check failures,
    and so we can't straightforwardly add these to the inert set and
    use them for rewriting. (NB: A rigid type constructor is at the
    top of all RHSs, preventing reorienting in canEqTyVarFunEq in the tyvar
    cases.)

    The key idea is to replace the outermost type family applications in the RHS of
    the starred constraints with a fresh variable, which we'll call a cycle-breaker
    variable, or cbv. Then, relate the cbv back with the original type family
    application via new equality constraints. Our situations thus become:

      instance C (Maybe b)
      [G] a ~ Maybe cbv
      [G] F a ~ cbv
      [W] C a

    or

      instance C (a -> b)
      [W] alpha ~ (cbv1 -> cbv2)
      [W] Arg alpha ~ cbv1
      [W] Res alpha ~ cbv2
      [W] C alpha

    or

      [G] Code a ~ '[ '[ cbv ] ]
      [G] Head (Head (Code a)) ~ cbv
      [W] Code a ~ '[ '[ alpha ] ]

    This transformation (creating the new types and emitting new equality
    constraints) is done by the `FamAppBreaker` field of `TEFA_Break`, which
    in turn lives in the `tef_fam_app` field of `TyEqFlags`.  And that in
    turn controls the behaviour of the workhorse: GHC.Tc.Utils.Unify.checkTyEqRhs.

    The details depend on whether we're working with a Given or a Wanted.

    Given
    -----
    We emit a new Given, [G] F a ~ cbv, equating the type family application
    to our new cbv. This is actually done by `break_given` in
    `GHC.Tc.Solver.Monad.checkTypeEq`.

    Note its orientation: The type family ends up on the left; see
    Note [Orienting TyFamLHS/TyFamLHS]. No special treatment for
    CycleBreakerTvs is necessary. This scenario is now easily soluble, by using
    the first Given to rewrite the Wanted, which can now be solved.

    (The first Given actually also rewrites the second one, giving
    [G] F (Maybe cbv) ~ cbv, but this causes no trouble.)

    Of course, we don't want our fresh variables leaking into e.g. error
    messages.  So we fill in the metavariables with their original type family
    applications after we're done running the solver (in nestImplicTcS and
    runTcSWithEvBinds).  This is done by `restoreTyVarCycles`, which uses the
    `inert_cycle_breakers` field in InertSet, which contains the pairings
    invented in `break_given`.

    That is, we transform
      [G] g : lhs ~ ...(F lhs)...
    to
      [G] (Refl lhs) : F lhs ~ cbv      -- CEqCan
      [G] g          : lhs ~ ...cbv...  -- CEqCan

    Note that
    * `cbv` is a fresh cycle breaker variable.
    * `cbv` is a is a meta-tyvar, but it is completely untouchable.
    * We track the cycle-breaker variables in inert_cycle_breakers in InertSet
    * We eventually fill in the cycle-breakers, with `cbv := F lhs`.
      No one else fills in CycleBreakerTvs!
    * The evidence for the new `F lhs ~ cbv` constraint is Refl, because we know
      this fill-in is ultimately going to happen.
    * In `inert_cycle_breakers`, we remember the (cbv, F lhs) pair; that is, we
      remember the /original/ type.  The [G] F lhs ~ cbv constraint may be rewritten
      by other givens (eg if we have another [G] lhs ~ (b,c)), but at the end we
      still fill in with cbv := F lhs
    * This fill-in is done when solving is complete, by restoreTyVarCycles
      in nestImplicTcS and runTcSWithEvBinds.

    Wanted
    ------
    First, we do not cycle-break unless the LHS is a unifiable type variable
    See Note [Don't cycle-break Wanteds when not unifying] in GHC.Tc.Solver.Monad.

    OK, so suppose the LHS is a unifiable type variable.  The fresh cycle-breaker
    variables here must actually be normal, touchable metavariables. That is, they
    are TauTvs. Nothing at all unusual. Repeating the example from above, we have

      *[W] alpha ~ (Arg alpha -> Res alpha)

    and we turn this into

      *[W] alpha ~ (cbv1 -> cbv2)
      [W] Arg alpha ~ cbv1
      [W] Res alpha ~ cbv2

    where cbv1 and cbv2 are fresh TauTvs.  This is actually done by `break_wanted`
    in `GHC.Tc.Solver.Monad.checkTouchableTyVarEq`.

    Why TauTvs? See [Why TauTvs] below.

    Critically, we emit the two new constraints (the last two above)
    directly instead of calling wrapUnifierTcS. (Otherwise, we'd end up
    unifying cbv1 and cbv2 immediately, achieving nothing.)  Next, we
    unify alpha := cbv1 -> cbv2, having eliminated the occurs check. This
    unification happens immediately following a successful call to
    checkTouchableTyVarEq, in canEqCanLHSFinish_try_unification.

    Now, we're here (including further context from our original example,
    from the top of the Note):

      instance C (a -> b)
      [W] Arg (cbv1 -> cbv2) ~ cbv1
      [W] Res (cbv1 -> cbv2) ~ cbv2
      [W] C (cbv1 -> cbv2)

    The first two W constraints reduce to reflexivity and are discarded,
    and the last is easily soluble.

    [Why TauTvs]:
    Let's look at another example (typecheck/should_compile/T19682) where we need
    to unify the cbvs:

      class    (AllEqF xs ys, SameShapeAs xs ys) => AllEq xs ys
      instance (AllEqF xs ys, SameShapeAs xs ys) => AllEq xs ys

      type family SameShapeAs xs ys :: Constraint where
        SameShapeAs '[] ys      = (ys ~ '[])
        SameShapeAs (x : xs) ys = (ys ~ (Head ys : Tail ys))

      type family AllEqF xs ys :: Constraint where
        AllEqF '[]      '[]      = ()
        AllEqF (x : xs) (y : ys) = (x ~ y, AllEq xs ys)

      [W] alpha ~ (Head alpha : Tail alpha)
      [W] AllEqF '[Bool] alpha

    Without the logic detailed in this Note, we're stuck here, as AllEqF cannot
    reduce and alpha cannot unify. Let's instead apply our cycle-breaker approach,
    just as described above. We thus invent cbv1 and cbv2 and unify
    alpha := cbv1 -> cbv2, yielding (after zonking)

      [W] Head (cbv1 : cbv2) ~ cbv1
      [W] Tail (cbv1 : cbv2) ~ cbv2
      [W] AllEqF '[Bool] (cbv1 : cbv2)

    The first two W constraints simplify to reflexivity and are discarded.
    But the last reduces:

      [W] Bool ~ cbv1
      [W] AllEq '[] cbv2

    The first of these is solved by unification: cbv1 := Bool. The second
    is solved by the instance for AllEq to become

      [W] AllEqF '[] cbv2
      [W] SameShapeAs '[] cbv2

    While the first of these is stuck, the second makes progress, to lead to

      [W] AllEqF '[] cbv2
      [W] cbv2 ~ '[]

    This second constraint is solved by unification: cbv2 := '[]. We now
    have

      [W] AllEqF '[] '[]

    which reduces to

      [W] ()

    which is trivially satisfiable. Hooray!

    Note that we need to unify the cbvs here; if we did not, there would be
    no way to solve those constraints. That's why the cycle-breakers are
    ordinary TauTvs.

    How all this is implemented
    ---------------------------
    We implement all this via the `TEFA_Break` constructor of `TyEqFamApp`,
    itself stored in the `tef_fam_app` field of `TyEqFlags`, which controls
    the behaviour of `GHC.Tc.Utils.Unify.checkTyEqRhs`.  The `TEFA_Break`
    stuff happens when `checkTyEqRhs` encounters a family application.

    We try the cycle-breaking trick:
    * For Wanteds, when there is a touchable unification variable on the left
    * For Givens, regardless of the LHS

    EXCEPT that, in both cases, as `GHC.Tc.Solver.Monad.mkTEFA_Break` shows, we
    don't use this trick:

    * When the constraint we are looking at was itself created by cycle-breaking;
      see Detail (7) below.

    * For representational equalities, as there is no concrete use case where it is
      helpful (unlike for nominal equalities).

      Furthermore, because function applications can be CanEqLHSs, but newtype
      applications cannot, the disparities between the cases are enough that it
      would be effortful to expand the idea to representational equalities. A quick
      attempt, with
          data family N a b
          f :: (Coercible a (N a b), Coercible (N a b) b) => a -> b
          f = coerce
      failed with "Could not match 'b' with 'b'." Further work is held off
      until when we have a concrete incentive to explore this dark corner.

    More details:

     (1) We don't look under foralls, at all, in `checkTyEqRhs`.  There might be
         a cyclic occurrence underneath, in a case like
              [G] lhs ~ forall b. ... lhs ....
         but it doesn't matter because we will classify the constraint as Irred,
         so it will not be used for rewriting.

         Earlier versions required an extra, post-breaking, check.  Skipping this
         check causes typecheck/should_fail/GivenForallLoop and polykinds/T18451 to
         loop.  But now it is all simpler, with no need for a second check.

     (2) Historical Note: our goal here is to avoid loops in rewriting. We can thus
         skip looking in coercions, as we don't rewrite in coercions in the
         algorithm in GHC.Solver.Rewrite.  This doesn't seem relevant any more.
         We cycle break to make the constraint canonical.

     (3) As we cycle-break as described in this Note, we can build ill-kinded
         types. For example, if we have Proxy (F a) b, where (b :: F a), then
         replacing this with Proxy cbv b is ill-kinded. However, we will later
         set cbv := F a, and so the zonked type will be well-kinded again.
         The temporary ill-kinded type hurts no one, and avoiding this would
         be quite painfully difficult.

         Specifically, this detail does not contravene the Purely Kinded Type Invariant
         (Note [The Purely Kinded Type Invariant (PKTI)] in GHC.Tc.Gen.HsType).
         The PKTI says that we can call typeKind on any type, without failure.
         It would be violated if we, say, replaced a kind (a -> b) with a kind c,
         because an arrow kind might be consulted in piResultTys. Here, we are
         replacing one opaque type like (F a b c) with another, cbv (opaque in
         that we never assume anything about its structure, like that it has a
         result type or a RuntimeRep argument).

     (4) The evidence for the produced Givens is all just reflexive, because we
         will eventually set the cycle-breaker variable to be the type family, and
         then, after the zonk, all will be well. See also the notes at the end of
         the Given section of this Note.

     (5) The implementation in `checkTyEqRhs` is efficient because it only replaces
         a type family application with a type variable, if that particular
         appplication is implicated in the occurs check.  For example:
             [W] alpha ~ Maybe (F alpha, G beta)
         We'll end up calling GHC.Tc.Utils.Unify.checkFamApp
           * On `F alpha`, which fail and calls the cycle-breaker in TEFA_Break
           * On `G beta`, which succeeds no problem.

         However, we make no attempt to detect cases like a ~ (F a, F a) and use the
         same tyvar to replace F a. The constraint solver will common them up later!
         (Cf. Note [Flattening type-family applications when matching instances] in
         GHC.Core.Unify, which goes to this extra effort.) However, this is really
         a very small corner case.  The investment to craft a clever, performant
         solution seems unworthwhile.

     (6) We often get the predicate associated with a constraint from its evidence
         with ctPred. We thus must not only make sure the generated CEqCan's fields
         have the updated RHS type (that is, the one produced by replacing type
         family applications with fresh variables), but we must also update the
         evidence itself. This is done by the call to rewriteEqEvidence in
         canEqCanLHSFinish.

     (7) We don't wish to apply this magic on the equalities created
         by this very same process. Consider this, from
         typecheck/should_compile/ContextStack2:

           type instance TF (a, b) = (TF a, TF b)
           t :: (a ~ TF (a, Int)) => ...

           [G] a ~ TF (a, Int)

         The RHS reduces, so we get

           [G] a ~ (TF a, TF Int)

         We then break cycles, to get

           [G] g1 :: a ~ (cbv1, cbv2)
           [G] g2 :: TF a ~ cbv1
           [G] g3 :: TF Int ~ cbv2

         g1 gets added to the inert set, as written. But then g2 becomes
         the work item. g1 rewrites g2 to become

           [G] TF (cbv1, cbv2) ~ cbv1

         which then uses the type instance to become

           [G] (TF cbv1, TF cbv2) ~ cbv1

         which looks remarkably like the Given we started with. If left unchecked,
         this will end up breaking cycles again, looping ad infinitum (and
         resulting in a context-stack reduction error, not an outright loop). The
         solution is easy: don't break cycles on an equality generated by breaking
         cycles. Instead, we mark this final Given as a CIrredCan with a
         NonCanonicalReason with the soluble occurs-check bit set (only).

         We track these equalities by giving them a special CtOrigin,
         CycleBreakerOrigin. This works for both Givens and Wanteds, as we need the
         logic in the W case for e.g. typecheck/should_fail/T17139.  Because this
         logic needs to work for Wanteds, too, we cannot simply look for a
         CycleBreakerTv on the left: Wanteds don't use them.

    **********************************************************************
    *                                                                    *
                       Rewriting evidence
    *                                                                    *
    **********************************************************************
    -}

    rewriteEqEvidence :: RewriterSet        -- New rewriters
                                            -- See GHC.Tc.Types.Constraint
                                            -- Note [Wanteds rewrite Wanteds]
                      -> CtEvidence         -- Old evidence :: olhs ~ orhs (not swapped)
                                            --              or orhs ~ olhs (swapped)
                      -> SwapFlag
                      -> Reduction          -- lhs_co :: olhs ~ nlhs
                      -> Reduction          -- rhs_co :: orhs ~ nrhs
                      -> TcS CtEvidence     -- Of type nlhs ~ nrhs
    -- With reductions (Reduction lhs_co nlhs) (Reduction rhs_co nrhs),
    -- rewriteEqEvidence yields, for a given equality (Given g olhs orhs):
    -- If not swapped
    --      g1 : nlhs ~ nrhs = sym lhs_co ; g ; rhs_co
    -- If swapped
    --      g1 : nlhs ~ nrhs = sym lhs_co ; Sym g ; rhs_co
    --
    -- For a wanted equality (Wanted w), we do the dual thing:
    -- New  w1 : nlhs ~ nrhs
    -- If not swapped
    --      w : olhs ~ orhs = lhs_co ; w1 ; sym rhs_co
    -- If swapped
    --      w : orhs ~ olhs = rhs_co ; sym w1 ; sym lhs_co
    --
    -- It's all a form of rewriteEvidence, specialised for equalities
    rewriteEqEvidence :: RewriterSet
    -> CtEvidence
    -> SwapFlag
    -> Reduction
    -> Reduction
    -> TcS CtEvidence
    rewriteEqEvidence RewriterSet
    new_rewriters CtEvidence
    old_ev SwapFlag
    swapped (Reduction TcCoercion
    lhs_co Type
    nlhs) (Reduction TcCoercion
    rhs_co Type
    nrhs)
      | SwapFlag
    NotSwapped <- SwapFlag
    swapped
      , TcCoercion -> Bool
    isReflCo TcCoercion
    lhs_co      -- See Note [Rewriting with Refl]
      , TcCoercion -> Bool
    isReflCo TcCoercion
    rhs_co
      = CtEvidence -> TcS CtEvidence
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (HasDebugCallStack => CtEvidence -> Type -> CtEvidence
    CtEvidence -> Type -> CtEvidence
    setCtEvPredType CtEvidence
    old_ev Type
    new_pred)

      | CtGiven { ctev_evar :: CtEvidence -> TyVar
    ctev_evar = TyVar
    old_evar } <- CtEvidence
    old_ev
      = do { let new_tm :: EvTerm
    new_tm = TcCoercion -> EvTerm
    evCoercion ( TcCoercion -> TcCoercion
    mkSymCo TcCoercion
    lhs_co
                                      TcCoercion -> TcCoercion -> TcCoercion
    `mkTransCo` SwapFlag -> TcCoercion -> TcCoercion
    maybeSymCo SwapFlag
    swapped (TyVar -> TcCoercion
    mkCoVarCo TyVar
    old_evar)
                                      TcCoercion -> TcCoercion -> TcCoercion
    `mkTransCo` TcCoercion
    rhs_co)
           ; CtLoc -> (Type, EvTerm) -> TcS CtEvidence
    newGivenEvVar CtLoc
    loc (Type
    new_pred, EvTerm
    new_tm) }

      | CtWanted { ctev_dest :: CtEvidence -> TcEvDest
    ctev_dest = TcEvDest
    dest
                 , ctev_rewriters :: CtEvidence -> RewriterSet
    ctev_rewriters = RewriterSet
    rewriters } <- CtEvidence
    old_ev
      , let rewriters' :: RewriterSet
    rewriters' = RewriterSet
    rewriters RewriterSet -> RewriterSet -> RewriterSet
    forall a. Semigroup a => a -> a -> a
    S.<> RewriterSet
    new_rewriters
      = do { (new_ev, hole_co) <- CtLoc
    -> RewriterSet
    -> Role
    -> Type
    -> Type
    -> TcS (CtEvidence, TcCoercion)
    newWantedEq CtLoc
    loc RewriterSet
    rewriters' (CtEvidence -> Role
    ctEvRole CtEvidence
    old_ev) Type
    nlhs Type
    nrhs
           ; let co = SwapFlag -> TcCoercion -> TcCoercion
    maybeSymCo SwapFlag
    swapped (TcCoercion -> TcCoercion) -> TcCoercion -> TcCoercion
    forall a b. (a -> b) -> a -> b
    $
                      TcCoercion
    lhs_co TcCoercion -> TcCoercion -> TcCoercion
    `mkTransCo` TcCoercion
    hole_co TcCoercion -> TcCoercion -> TcCoercion
    `mkTransCo` TcCoercion -> TcCoercion
    mkSymCo TcCoercion
    rhs_co
           ; setWantedEq dest co
           ; traceTcS "rewriteEqEvidence" (vcat [ ppr old_ev
                                                , ppr nlhs
                                                , ppr nrhs
                                                , ppr co
                                                , ppr new_rewriters ])
           ; return new_ev }

      where
        new_pred :: Type
    new_pred = CtEvidence -> Type -> Type -> Type
    mkTcEqPredLikeEv CtEvidence
    old_ev Type
    nlhs Type
    nrhs
        loc :: CtLoc
    loc      = CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    old_ev

    {-
    **********************************************************************
    *                                                                    *
                       tryInertEqs
    *                                                                    *
    **********************************************************************

    Note [Combining equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose we have
       Inert:     g1 :: a ~ t
       Work item: g2 :: a ~ t

    Then we can simply solve g2 from g1, thus g2 := g1.  Easy!
    But it's not so simple:

    * If t is a type variable, the equalties might be oriented differently:
          e.g. (g1 :: a~b) and (g2 :: b~a)
      So we look both ways round.  Hence the SwapFlag result to
      inertsCanDischarge.

    * We can only do g2 := g1 if g1 can discharge g2; that depends on
      (a) the role and (b) the flavour.  E.g. a representational equality
      cannot discharge a nominal one; a Wanted cannot discharge a Given.
      The predicate is eqCanRewriteFR.

    * Visibility. Suppose  S :: forall k. k -> Type, and consider unifying
          S @Type (a::Type)  ~   S @(Type->Type) (b::Type->Type)
      From the first argument we get (Type ~ Type->Type); from the second
      argument we get (a ~ b) which in turn gives (Type ~ Type->Type).
      See typecheck/should_fail/T16204c.

      That first argument is invisible in the source program (aside from
      visible type application), so we'd much prefer to get the error from
      the second. We track visibility in the uo_visible field of a TypeEqOrigin.
      We use this to prioritise visible errors (see GHC.Tc.Errors.tryReporters,
      the partition on isVisibleOrigin).

      So when combining two otherwise-identical equalites, we want to
      keep the visible one, and discharge the invisible one.  Hence the
      call to strictly_more_visible.
    -}

    tryInertEqs :: EqCt -> SolverStage ()
    tryInertEqs :: EqCt -> SolverStage ()
    tryInertEqs work_item :: EqCt
    work_item@(EqCt { eq_ev :: EqCt -> CtEvidence
    eq_ev = CtEvidence
    ev, eq_eq_rel :: EqCt -> EqRel
    eq_eq_rel = EqRel
    eq_rel })
      = TcS (StopOrContinue ()) -> SolverStage ()
    forall a. TcS (StopOrContinue a) -> SolverStage a
    Stage (TcS (StopOrContinue ()) -> SolverStage ())
    -> TcS (StopOrContinue ()) -> SolverStage ()
    forall a b. (a -> b) -> a -> b
    $
        do { inerts <- TcS InertCans
    getInertCans
           ; if | Just (ev_i, swapped) <- inertsCanDischarge inerts work_item
                -> do { setEvBindIfWanted ev True $
                        evCoercion (maybeSymCo swapped $
                                    downgradeRole (eqRelRole eq_rel)
                                                  (ctEvRole ev_i)
                                                  (ctEvCoercion ev_i))
                      ; stopWith ev "Solved from inert" }

                | otherwise
                -> continueWith () }

    inertsCanDischarge :: InertCans -> EqCt
                       -> Maybe ( CtEvidence  -- The evidence for the inert
                                , SwapFlag )  -- Whether we need mkSymCo
    inertsCanDischarge :: InertCans -> EqCt -> Maybe (CtEvidence, SwapFlag)
    inertsCanDischarge InertCans
    inerts (EqCt { eq_lhs :: EqCt -> CanEqLHS
    eq_lhs = CanEqLHS
    lhs_w, eq_rhs :: EqCt -> Type
    eq_rhs = Type
    rhs_w
                                    , eq_ev :: EqCt -> CtEvidence
    eq_ev = CtEvidence
    ev_w, eq_eq_rel :: EqCt -> EqRel
    eq_eq_rel = EqRel
    eq_rel })
      | (CtEvidence
    ev_i : [CtEvidence]
    _) <- [ CtEvidence
    ev_i | EqCt { eq_ev :: EqCt -> CtEvidence
    eq_ev = CtEvidence
    ev_i, eq_rhs :: EqCt -> Type
    eq_rhs = Type
    rhs_i
                                    , eq_eq_rel :: EqCt -> EqRel
    eq_eq_rel = EqRel
    eq_rel }
                                      <- InertCans -> CanEqLHS -> [EqCt]
    findEq InertCans
    inerts CanEqLHS
    lhs_w
                             , Type
    rhs_i HasDebugCallStack => Type -> Type -> Bool
    Type -> Type -> Bool
    `tcEqType` Type
    rhs_w
                             , CtEvidence -> EqRel -> Bool
    inert_beats_wanted CtEvidence
    ev_i EqRel
    eq_rel ]
      =  -- Inert:     a ~ ty
         -- Work item: a ~ ty
        (CtEvidence, SwapFlag) -> Maybe (CtEvidence, SwapFlag)
    forall a. a -> Maybe a
    Just (CtEvidence
    ev_i, SwapFlag
    NotSwapped)

      | Just CanEqLHS
    rhs_lhs <- Type -> Maybe CanEqLHS
    canEqLHS_maybe Type
    rhs_w
      , (CtEvidence
    ev_i : [CtEvidence]
    _) <- [ CtEvidence
    ev_i | EqCt { eq_ev :: EqCt -> CtEvidence
    eq_ev = CtEvidence
    ev_i, eq_rhs :: EqCt -> Type
    eq_rhs = Type
    rhs_i
                                    , eq_eq_rel :: EqCt -> EqRel
    eq_eq_rel = EqRel
    eq_rel }
                                 <- InertCans -> CanEqLHS -> [EqCt]
    findEq InertCans
    inerts CanEqLHS
    rhs_lhs
                             , Type
    rhs_i HasDebugCallStack => Type -> Type -> Bool
    Type -> Type -> Bool
    `tcEqType` CanEqLHS -> Type
    canEqLHSType CanEqLHS
    lhs_w
                             , CtEvidence -> EqRel -> Bool
    inert_beats_wanted CtEvidence
    ev_i EqRel
    eq_rel ]
      =  -- Inert:     a ~ b
         -- Work item: b ~ a
         (CtEvidence, SwapFlag) -> Maybe (CtEvidence, SwapFlag)
    forall a. a -> Maybe a
    Just (CtEvidence
    ev_i, SwapFlag
    IsSwapped)

      where
        loc_w :: CtLoc
    loc_w  = CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    ev_w
        flav_w :: CtFlavour
    flav_w = CtEvidence -> CtFlavour
    ctEvFlavour CtEvidence
    ev_w
        fr_w :: CtFlavourRole
    fr_w   = (CtFlavour
    flav_w, EqRel
    eq_rel)

        inert_beats_wanted :: CtEvidence -> EqRel -> Bool
    inert_beats_wanted CtEvidence
    ev_i EqRel
    eq_rel
          = -- eqCanRewriteFR:        see second bullet of Note [Combining equalities]
            -- strictly_more_visible: see last bullet of Note [Combining equalities]
            CtFlavourRole
    fr_i CtFlavourRole -> CtFlavourRole -> Bool
    `eqCanRewriteFR` CtFlavourRole
    fr_w
            Bool -> Bool -> Bool
    && Bool -> Bool
    not ((CtLoc
    loc_w CtLoc -> CtLoc -> Bool
    `strictly_more_visible` CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    ev_i)
                     Bool -> Bool -> Bool
    && (CtFlavourRole
    fr_w CtFlavourRole -> CtFlavourRole -> Bool
    `eqCanRewriteFR` CtFlavourRole
    fr_i))
          where
            fr_i :: CtFlavourRole
    fr_i = (CtEvidence -> CtFlavour
    ctEvFlavour CtEvidence
    ev_i, EqRel
    eq_rel)

        -- See Note [Combining equalities], final bullet
        strictly_more_visible :: CtLoc -> CtLoc -> Bool
    strictly_more_visible CtLoc
    loc1 CtLoc
    loc2
           = Bool -> Bool
    not (CtOrigin -> Bool
    isVisibleOrigin (CtLoc -> CtOrigin
    ctLocOrigin CtLoc
    loc2)) Bool -> Bool -> Bool
    &&
             CtOrigin -> Bool
    isVisibleOrigin (CtLoc -> CtOrigin
    ctLocOrigin CtLoc
    loc1)

    inertsCanDischarge InertCans
    _ EqCt
    _ = Maybe (CtEvidence, SwapFlag)
    forall a. Maybe a
    Nothing

    {- Note [Do not unify Givens]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider this GADT match
       data T a where
          T1 :: T Int
          ...

       f x = case x of
               T1 -> True
               ...

    So we get f :: T alpha[1] -> beta[1]
              x :: T alpha[1]
    and from the T1 branch we get the implication
       forall[2] (alpha[1] ~ Int) => beta[1] ~ Bool

    Now, clearly we don't want to unify alpha:=Int!  Yet at the moment we
    process [G] alpha[1] ~ Int, we don't have any given-equalities in the
    inert set, and hence there are no given equalities to make alpha untouchable.

    NB: if it were alpha[2] ~ Int, this argument wouldn't hold.  But that
    never happens: invariant (GivenInv) in Note [TcLevel invariants]
    in GHC.Tc.Utils.TcType.

    Simple solution: never unify in Givens!

    Note [Do not unify representational equalities]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider   [W] alpha ~R# b
    where alpha is touchable. Should we unify alpha := b?

    Certainly not!  Unifying forces alpha and be to be the same; but they
    only need to be representationally equal types.

    For example, we might have another constraint [W] alpha ~# N b
    where
      newtype N b = MkN b
    and we want to get alpha := N b.

    See also #15144, which was caused by unifying a representational
    equality.

    Note [Solve by unification]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    If we solve
       alpha[n] ~ ty
    by unification, there are two cases to consider

    * TouchableSameLevel: if the ambient level is 'n', then
      we can simply update alpha := ty, and do nothing else

    * TouchableOuterLevel free_metas n: if the ambient level is greater than
      'n' (the level of alpha), in addition to setting alpha := ty we must
      do two other things:

      1. Promote all the free meta-vars of 'ty' to level n.  After all,
         alpha[n] is at level n, and so if we set, say,
              alpha[n] := Maybe beta[m],
         we must ensure that when unifying beta we do skolem-escape checks
         etc relevant to level n.  Simple way to do that: promote beta to
         level n.

      2. Set the Unification Level Flag to record that a level-n unification has
         taken place. See Note [The Unification Level Flag] in GHC.Tc.Solver.Monad

    NB: TouchableSameLevel is just an optimisation for TouchableOuterLevel. Promotion
    would be a no-op, and setting the unification flag unnecessarily would just
    make the solver iterate more often.  (We don't need to iterate when unifying
    at the ambient level because of the kick-out mechanism.)
    -}

    {-********************************************************************
    *                                                                    *
              Final wrap-up for equalities
    *                                                                    *
    ********************************************************************-}

    {- Note [Looking up primitive equalities in quantified constraints]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    For equalities (a ~# b) look up (a ~ b), and then do a superclass
    selection. This avoids having to support quantified constraints whose
    kind is not Constraint, such as (forall a. F a ~# b)

    See
     * Note [Evidence for quantified constraints] in GHC.Core.Predicate
     * Note [Equality superclasses in quantified constraints]
       in GHC.Tc.Solver.Dict
    -}

    --------------------
    tryQCsIrredEqCt :: IrredCt -> SolverStage ()
    tryQCsIrredEqCt :: IrredCt -> SolverStage ()
    tryQCsIrredEqCt irred :: IrredCt
    irred@(IrredCt { ir_ev :: IrredCt -> CtEvidence
    ir_ev = CtEvidence
    ev })
      | EqPred EqRel
    eq_rel Type
    t1 Type
    t2 <- Type -> Pred
    classifyPredType (CtEvidence -> Type
    ctEvPred CtEvidence
    ev)
      = Ct -> EqRel -> Type -> Type -> SolverStage ()
    lookup_eq_in_qcis (IrredCt -> Ct
    CIrredCan IrredCt
    irred) EqRel
    eq_rel Type
    t1 Type
    t2

      | Bool
    otherwise  -- All the calls come from in this module, where we deal only with
                   -- equalities, so ctEvPred ev) must be an equality. Indeed, we could
                   -- pass eq_rel, t1, t2 as arguments, to avoid this can't-happen case,
                   -- but it's not a hot path, and this is simple and robust
      = String -> SDoc -> SolverStage ()
    forall a. HasCallStack => String -> SDoc -> a
    pprPanic String
    "solveIrredEquality" (CtEvidence -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr CtEvidence
    ev)

    --------------------
    tryQCsEqCt :: EqCt -> SolverStage ()
    tryQCsEqCt :: EqCt -> SolverStage ()
    tryQCsEqCt work_item :: EqCt
    work_item@(EqCt { eq_lhs :: EqCt -> CanEqLHS
    eq_lhs = CanEqLHS
    lhs, eq_rhs :: EqCt -> Type
    eq_rhs = Type
    rhs, eq_eq_rel :: EqCt -> EqRel
    eq_eq_rel = EqRel
    eq_rel })
      = Ct -> EqRel -> Type -> Type -> SolverStage ()
    lookup_eq_in_qcis (EqCt -> Ct
    CEqCan EqCt
    work_item) EqRel
    eq_rel (CanEqLHS -> Type
    canEqLHSType CanEqLHS
    lhs) Type
    rhs

    --------------------
    lookup_eq_in_qcis :: Ct -> EqRel -> TcType -> TcType -> SolverStage ()
    -- The "final QCI check" checks to see if we have
    --    [W] t1 ~# t2
    -- and a Given quantified contraint like (forall a b. blah => a ~ b)
    -- Why?  See Note [Looking up primitive equalities in quantified constraints]
    -- See also GHC.Tc.Solver.Dict
    -- Note [Equality superclasses in quantified constraints]
    lookup_eq_in_qcis :: Ct -> EqRel -> Type -> Type -> SolverStage ()
    lookup_eq_in_qcis Ct
    work_ct EqRel
    eq_rel Type
    lhs Type
    rhs
      = TcS (StopOrContinue ()) -> SolverStage ()
    forall a. TcS (StopOrContinue a) -> SolverStage a
    Stage (TcS (StopOrContinue ()) -> SolverStage ())
    -> TcS (StopOrContinue ()) -> SolverStage ()
    forall a b. (a -> b) -> a -> b
    $
        do { ev_binds_var <- TcS EvBindsVar
    getTcEvBindsVar
           ; ics <- getInertCans
           ; if isWanted ev                       -- Never look up Givens in quantified constraints
             && not (null (inert_insts ics))      -- Shortcut common case
             && not (isCoEvBindsVar ev_binds_var) -- See Note [Instances in no-evidence implications]
             then try_for_qci
             else continueWith () }
      where
        ev :: CtEvidence
    ev  = Ct -> CtEvidence
    ctEvidence Ct
    work_ct
        loc :: CtLoc
    loc = CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    ev
        role :: Role
    role = EqRel -> Role
    eqRelRole EqRel
    eq_rel

        try_for_qci :: TcS (StopOrContinue ())
    try_for_qci  -- First try looking for (lhs ~ rhs)
           | Just (Class
    cls, [Type]
    tys) <- EqRel -> Type -> Type -> Maybe (Class, [Type])
    boxEqPred EqRel
    eq_rel Type
    lhs Type
    rhs
           = do { res <- Type -> CtLoc -> TcS ClsInstResult
    matchLocalInst (Class -> [Type] -> Type
    mkClassPred Class
    cls [Type]
    tys) CtLoc
    loc
                ; traceTcS "lookup_irred_in_qcis:1" (ppr (mkClassPred cls tys))
                ; case res of
                    OneInst { cir_mk_ev :: ClsInstResult -> [EvExpr] -> EvTerm
    cir_mk_ev = [EvExpr] -> EvTerm
    mk_ev }
                      -> CtEvidence -> ClsInstResult -> TcS (StopOrContinue ())
    forall a. CtEvidence -> ClsInstResult -> TcS (StopOrContinue a)
    chooseInstance CtEvidence
    ev (ClsInstResult
    res { cir_mk_ev = mk_eq_ev cls tys mk_ev })
                    ClsInstResult
    _ -> TcS (StopOrContinue ())
    try_swapping }
           | Bool
    otherwise
           = () -> TcS (StopOrContinue ())
    forall a. a -> TcS (StopOrContinue a)
    continueWith ()

        try_swapping :: TcS (StopOrContinue ())
    try_swapping  -- Now try looking for (rhs ~ lhs)  (see #23333)
           | Just (Class
    cls, [Type]
    tys) <- EqRel -> Type -> Type -> Maybe (Class, [Type])
    boxEqPred EqRel
    eq_rel Type
    rhs Type
    lhs
           = do { res <- Type -> CtLoc -> TcS ClsInstResult
    matchLocalInst (Class -> [Type] -> Type
    mkClassPred Class
    cls [Type]
    tys) CtLoc
    loc
                ; traceTcS "lookup_irred_in_qcis:2" (ppr (mkClassPred cls tys))
                ; case res of
                    OneInst { cir_mk_ev :: ClsInstResult -> [EvExpr] -> EvTerm
    cir_mk_ev = [EvExpr] -> EvTerm
    mk_ev }
                      -> do { ev' <- RewriterSet
    -> CtEvidence
    -> SwapFlag
    -> Reduction
    -> Reduction
    -> TcS CtEvidence
    rewriteEqEvidence RewriterSet
    emptyRewriterSet CtEvidence
    ev SwapFlag
    IsSwapped
                                          (Role -> Type -> Reduction
    mkReflRedn Role
    role Type
    rhs) (Role -> Type -> Reduction
    mkReflRedn Role
    role Type
    lhs)
                            ; chooseInstance ev' (res { cir_mk_ev = mk_eq_ev cls tys mk_ev }) }
                    ClsInstResult
    _ -> do { String -> SDoc -> TcS ()
    traceTcS String
    "lookup_irred_in_qcis:3" (Ct -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Ct
    work_ct)
                            ; () -> TcS (StopOrContinue ())
    forall a. a -> TcS (StopOrContinue a)
    continueWith () }}
           | Bool
    otherwise
           = () -> TcS (StopOrContinue ())
    forall a. a -> TcS (StopOrContinue a)
    continueWith ()

        mk_eq_ev :: Class -> [Type] -> ([EvExpr] -> EvTerm) -> [EvExpr] -> EvTerm
    mk_eq_ev Class
    cls [Type]
    tys [EvExpr] -> EvTerm
    mk_ev [EvExpr]
    evs
          | TyVar
    sc_id : [TyVar]
    rest <- Class -> [TyVar]
    classSCSelIds Class
    cls  -- Just one superclass for this
          = Bool -> EvTerm -> EvTerm
    forall a. HasCallStack => Bool -> a -> a
    assert ([TyVar] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [TyVar]
    rest) (EvTerm -> EvTerm) -> EvTerm -> EvTerm
    forall a b. (a -> b) -> a -> b
    $ case ([EvExpr] -> EvTerm
    mk_ev [EvExpr]
    evs) of
              EvExpr EvExpr
    e -> EvExpr -> EvTerm
    EvExpr (TyVar -> EvExpr
    forall b. TyVar -> Expr b
    Var TyVar
    sc_id EvExpr -> [Type] -> EvExpr
    forall b. Expr b -> [Type] -> Expr b
    `mkTyApps` [Type]
    tys EvExpr -> EvExpr -> EvExpr
    forall b. Expr b -> Expr b -> Expr b
    `App` EvExpr
    e)
              EvTerm
    ev       -> String -> SDoc -> EvTerm
    forall a. HasCallStack => String -> SDoc -> a
    pprPanic String
    "mk_eq_ev" (EvTerm -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr EvTerm
    ev)
          | Bool
    otherwise = String -> SDoc -> EvTerm
    forall a. HasCallStack => String -> SDoc -> a
    pprPanic String
    "finishEqCt" (Ct -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Ct
    work_ct)

    {- Note [Instances in no-evidence implications]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In #15290 we had
      [G] forall p q. Coercible p q => Coercible (m p) (m q))   -- Quantified
      [W] forall <no-ev> a. m (Int, IntStateT m a)
                              ~R#
                            m (Int, StateT Int m a)

    The Given is an ordinary quantified constraint; the Wanted is an implication
    equality that arises from
      [W] (forall a. t1) ~R# (forall a. t2)

    But because the (t1 ~R# t2) is solved "inside a type" (under that forall a)
    we can't generate any term evidence.  So we can't actually use that
    lovely quantified constraint.  Alas!

    This test arranges to ignore the instance-based solution under these
    (rare) circumstances.   It's sad, but I  really don't see what else we can do.
    -}

    {-
    **********************************************************************
    *                                                                    *
        Functional dependencies for type families
    *                                                                    *
    **********************************************************************

    Note [Reverse order of fundep equations]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider this scenario (from dependent/should_fail/T13135_simple):

      type Sig :: Type -> Type
      data Sig a = SigFun a (Sig a)

      type SmartFun :: forall (t :: Type). Sig t -> Type
      type family SmartFun sig = r | r -> sig where
        SmartFun @Type (SigFun @Type a sig) = a -> SmartFun @Type sig

      [W] SmartFun @kappa sigma ~ (Int -> Bool)

    The injectivity of SmartFun allows us to produce two new equalities:

      [W] w1 :: Type ~ kappa
      [W] w2 :: SigFun @Type Int beta ~ sigma

    for some fresh (beta :: SigType). The second Wanted here is actually
    heterogeneous: the LHS has type Sig Type while the RHS has type Sig kappa.
    Of course, if we solve the first wanted first, the second becomes homogeneous.

    When looking for injectivity-inspired equalities, we work left-to-right,
    producing the two equalities in the order written above. However, these
    equalities are then passed into wrapUnifierTcS, which will fail, adding these
    to the work list. However, crucially, the work list operates like a *stack*.
    So, because we add w1 and then w2, we process w2 first. This is silly: solving
    w1 would unlock w2. So we make sure to add equalities to the work
    list in left-to-right order, which requires a few key calls to 'reverse'.

    This treatment is also used for class-based functional dependencies, although
    we do not have a program yet known to exhibit a loop there. It just seems
    like the right thing to do.

    When this was originally conceived, it was necessary to avoid a loop in T13135.
    That loop is now avoided by continuing with the kind equality (not the type
    equality) in canEqCanLHSHetero (see Note [Equalities with incompatible kinds]).
    However, the idea of working left-to-right still seems worthwhile, and so the calls
    to 'reverse' remain.

    Note [Improvement orientation]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    See also Note [Fundeps with instances, and equality orientation], which describes
    the Exact Same Problem, with the same solution, but for functional dependencies.

    A very delicate point is the orientation of equalities
    arising from injectivity improvement (#12522).  Suppose we have
      type family F x = t | t -> x
      type instance F (a, Int) = (Int, G a)
    where G is injective; and wanted constraints

      [W] F (alpha, beta) ~ (Int, <some type>)

    The injectivity will give rise to constraints

      [W] gamma1 ~ alpha
      [W] Int ~ beta

    The fresh unification variable gamma1 comes from the fact that we
    can only do "partial improvement" here; see Section 5.2 of
    "Injective type families for Haskell" (HS'15).

    Now, it's very important to orient the equations this way round,
    so that the fresh unification variable will be eliminated in
    favour of alpha.  If we instead had
       [W] alpha ~ gamma1
    then we would unify alpha := gamma1; and kick out the wanted
    constraint.  But when we substitute it back in, it'd look like
       [W] F (gamma1, beta) ~ fuv
    and exactly the same thing would happen again!  Infinite loop.

    This all seems fragile, and it might seem more robust to avoid
    introducing gamma1 in the first place, in the case where the
    actual argument (alpha, beta) partly matches the improvement
    template.  But that's a bit tricky, esp when we remember that the
    kinds much match too; so it's easier to let the normal machinery
    handle it.  Instead we are careful to orient the new
    equality with the template on the left.  Delicate, but it works.

    -}

    tryFamFamInjectivity :: CtEvidence -> EqRel
                         -> TyCon -> [TcType] -> TyCon -> [TcType] -> MCoercion
                         -> TcS Bool  -- True <=> some unification happened
    tryFamFamInjectivity :: CtEvidence
    -> EqRel
    -> TyCon
    -> [Type]
    -> TyCon
    -> [Type]
    -> MCoercion
    -> TcS Bool
    tryFamFamInjectivity CtEvidence
    ev EqRel
    eq_rel TyCon
    fun_tc1 [Type]
    fun_args1 TyCon
    fun_tc2 [Type]
    fun_args2 MCoercion
    mco
      | EqRel
    ReprEq <- EqRel
    eq_rel
      = Bool -> TcS Bool
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False   -- Injectivity applies only for Nominal equalities
      | TyCon
    fun_tc1 TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    /= TyCon
    fun_tc2
      = Bool -> TcS Bool
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False   -- If the families don't match, stop.
      | CtEvidence -> Bool
    isGiven CtEvidence
    ev
      = Bool -> TcS Bool
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False   -- See Note [No Given/Given fundeps] in GHC.Tc.Solver.Dict

      -- So this is a [W] (F tys1 ~N# F tys2)

      -- Is F an injective type family
      | Injective [Bool]
    inj <- TyCon -> Injectivity
    tyConInjectivityInfo TyCon
    fun_tc1
      = CtEvidence -> Role -> (UnifyEnv -> TcM ()) -> TcS Bool
    unifyFunDeps CtEvidence
    ev Role
    Nominal ((UnifyEnv -> TcM ()) -> TcS Bool)
    -> (UnifyEnv -> TcM ()) -> TcS Bool
    forall a b. (a -> b) -> a -> b
    $ \UnifyEnv
    uenv ->
        UnifyEnv -> [TypeEqn] -> TcM ()
    uPairsTcM UnifyEnv
    uenv [ Type -> Type -> TypeEqn
    forall a. a -> a -> Pair a
    Pair Type
    ty1 Type
    ty2
                       | (Type
    ty1,Type
    ty2,Bool
    True) <- [Type] -> [Type] -> [Bool] -> [(Type, Type, Bool)]
    forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
    zip3 [Type]
    fun_args1 [Type]
    fun_args2 [Bool]
    inj ]

        -- Built-in synonym families don't have an entry point for this
        -- use case. So, we just use sfInteractInert and pass two equal
        -- RHSs. We *could* add another entry point, but then there would
        -- be a burden to make sure the new entry point and existing ones
        -- were internally consistent. This is slightly distasteful, but
        -- it works well in practice and localises the problem.  Ugh.
      | Just BuiltInSynFamily
    ops <- TyCon -> Maybe BuiltInSynFamily
    isBuiltInSynFamTyCon_maybe TyCon
    fun_tc1
      = let tc_kind :: Type
    tc_kind = TyCon -> Type
    tyConKind TyCon
    fun_tc1
            ki1 :: Type
    ki1 = HasDebugCallStack => Type -> [Type] -> Type
    Type -> [Type] -> Type
    piResultTys Type
    tc_kind [Type]
    fun_args1
            ki2 :: Type
    ki2 | MCoercion
    MRefl <- MCoercion
    mco
                = Type
    ki1   -- just a small optimisation
                | Bool
    otherwise
                = HasDebugCallStack => Type -> [Type] -> Type
    Type -> [Type] -> Type
    piResultTys Type
    tc_kind [Type]
    fun_args2

            fake_rhs1 :: Type
    fake_rhs1 = Type -> Type
    anyTypeOfKind Type
    ki1
            fake_rhs2 :: Type
    fake_rhs2 = Type -> Type
    anyTypeOfKind Type
    ki2

            eqs :: [TypeEqn]
            eqs :: [TypeEqn]
    eqs = BuiltInSynFamily -> [Type] -> Type -> [Type] -> Type -> [TypeEqn]
    sfInteractInert BuiltInSynFamily
    ops [Type]
    fun_args1 Type
    fake_rhs1 [Type]
    fun_args2 Type
    fake_rhs2
        in
        CtEvidence -> Role -> (UnifyEnv -> TcM ()) -> TcS Bool
    unifyFunDeps CtEvidence
    ev Role
    Nominal ((UnifyEnv -> TcM ()) -> TcS Bool)
    -> (UnifyEnv -> TcM ()) -> TcS Bool
    forall a b. (a -> b) -> a -> b
    $ \UnifyEnv
    uenv ->
        UnifyEnv -> [TypeEqn] -> TcM ()
    uPairsTcM UnifyEnv
    uenv [TypeEqn]
    eqs

      | Bool
    otherwise  -- ordinary, non-injective type family
      = Bool -> TcS Bool
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False

    --------------------
    tryFunDeps :: EqCt -> SolverStage ()
    tryFunDeps :: EqCt -> SolverStage ()
    tryFunDeps work_item :: EqCt
    work_item@(EqCt { eq_lhs :: EqCt -> CanEqLHS
    eq_lhs = CanEqLHS
    lhs, eq_ev :: EqCt -> CtEvidence
    eq_ev = CtEvidence
    ev })
      = TcS (StopOrContinue ()) -> SolverStage ()
    forall a. TcS (StopOrContinue a) -> SolverStage a
    Stage (TcS (StopOrContinue ()) -> SolverStage ())
    -> TcS (StopOrContinue ()) -> SolverStage ()
    forall a b. (a -> b) -> a -> b
    $
        case CanEqLHS
    lhs of
           TyFamLHS TyCon
    tc [Type]
    args -> do { inerts <- TcS InertCans
    getInertCans
                                  ; imp1 <- improveLocalFunEqs inerts tc args work_item
                                  ; imp2 <- improveTopFunEqs tc args work_item
                                  ; if (imp1 || imp2)
                                    then startAgainWith (mkNonCanonical ev)
                                    else continueWith () }
           TyVarLHS {} -> () -> TcS (StopOrContinue ())
    forall a. a -> TcS (StopOrContinue a)
    continueWith ()

    --------------------
    improveTopFunEqs :: TyCon -> [TcType] -> EqCt -> TcS Bool
    -- See Note [FunDep and implicit parameter reactions]
    improveTopFunEqs :: TyCon -> [Type] -> EqCt -> TcS Bool
    improveTopFunEqs TyCon
    fam_tc [Type]
    args (EqCt { eq_ev :: EqCt -> CtEvidence
    eq_ev = CtEvidence
    ev, eq_rhs :: EqCt -> Type
    eq_rhs = Type
    rhs })
      | CtEvidence -> Bool
    isGiven CtEvidence
    ev
      = Bool -> TcS Bool
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False  -- See Note [No Given/Given fundeps]

      | Bool
    otherwise
      = do { fam_envs <- TcS (FamInstEnv, FamInstEnv)
    getFamInstEnvs
           ; eqns <- improve_top_fun_eqs fam_envs fam_tc args rhs
           ; traceTcS "improveTopFunEqs" (vcat [ text "lhs:" <+> ppr fam_tc <+> ppr args
                                               , text "rhs:" <+> ppr rhs
                                               , text "eqns:" <+> ppr eqns ])
           ; unifyFunDeps ev Nominal $ \UnifyEnv
    uenv ->
             UnifyEnv -> [TypeEqn] -> TcM ()
    uPairsTcM (UnifyEnv -> UnifyEnv
    bump_depth UnifyEnv
    uenv) ([TypeEqn] -> [TypeEqn]
    forall a. [a] -> [a]
    reverse [TypeEqn]
    eqns) }
             -- Missing that `reverse` causes T13135 and T13135_simple to loop.
             -- See Note [Reverse order of fundep equations]

      where
        bump_depth :: UnifyEnv -> UnifyEnv
    bump_depth UnifyEnv
    env = UnifyEnv
    env { u_loc = bumpCtLocDepth (u_loc env) }
            -- ToDo: this location is wrong; it should be FunDepOrigin2
            -- See #14778

    improve_top_fun_eqs :: FamInstEnvs
                        -> TyCon -> [TcType] -> TcType
                        -> TcS [TypeEqn]
    improve_top_fun_eqs :: (FamInstEnv, FamInstEnv)
    -> TyCon -> [Type] -> Type -> TcS [TypeEqn]
    improve_top_fun_eqs (FamInstEnv, FamInstEnv)
    fam_envs TyCon
    fam_tc [Type]
    args Type
    rhs_ty
      | Just BuiltInSynFamily
    ops <- TyCon -> Maybe BuiltInSynFamily
    isBuiltInSynFamTyCon_maybe TyCon
    fam_tc
      = [TypeEqn] -> TcS [TypeEqn]
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (BuiltInSynFamily -> [Type] -> Type -> [TypeEqn]
    sfInteractTop BuiltInSynFamily
    ops [Type]
    args Type
    rhs_ty)

      -- see Note [Type inference for type families with injectivity]
      | TyCon -> Bool
    isOpenTypeFamilyTyCon TyCon
    fam_tc
      , Injective [Bool]
    injective_args <- TyCon -> Injectivity
    tyConInjectivityInfo TyCon
    fam_tc
      , let fam_insts :: [FamInst]
    fam_insts = (FamInstEnv, FamInstEnv) -> TyCon -> [FamInst]
    lookupFamInstEnvByTyCon (FamInstEnv, FamInstEnv)
    fam_envs TyCon
    fam_tc
      = -- it is possible to have several compatible equations in an open type
        -- family but we only want to derive equalities from one such equation.
        do { let improvs :: [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    improvs = [FamInst]
    -> (FamInst -> [TyVar])
    -> (FamInst -> [Type])
    -> (FamInst -> Type)
    -> (FamInst -> Maybe CoAxBranch)
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    forall a.
    [a]
    -> (a -> [TyVar])
    -> (a -> [Type])
    -> (a -> Type)
    -> (a -> Maybe CoAxBranch)
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    buildImprovementData [FamInst]
    fam_insts
                               FamInst -> [TyVar]
    fi_tvs FamInst -> [Type]
    fi_tys FamInst -> Type
    fi_rhs (Maybe CoAxBranch -> FamInst -> Maybe CoAxBranch
    forall a b. a -> b -> a
    const Maybe CoAxBranch
    forall a. Maybe a
    Nothing)

           ; String -> SDoc -> TcS ()
    traceTcS String
    "improve_top_fun_eqs2" ([([Type], Subst, [TyVar], Maybe CoAxBranch)] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    improvs)
           ; (([Type], Subst, [TyVar], Maybe CoAxBranch) -> TcS [TypeEqn])
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)] -> TcS [TypeEqn]
    forall (m :: * -> *) (f :: * -> *) a b.
    (Monad m, Traversable f) =>
    (a -> m [b]) -> f a -> m [b]
    concatMapM ([Bool]
    -> ([Type], Subst, [TyVar], Maybe CoAxBranch) -> TcS [TypeEqn]
    injImproveEqns [Bool]
    injective_args) ([([Type], Subst, [TyVar], Maybe CoAxBranch)] -> TcS [TypeEqn])
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)] -> TcS [TypeEqn]
    forall a b. (a -> b) -> a -> b
    $
             Int
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    forall a. Int -> [a] -> [a]
    take Int
    1 [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    improvs }

      | Just CoAxiom Branched
    ax <- TyCon -> Maybe (CoAxiom Branched)
    isClosedSynFamilyTyConWithAxiom_maybe TyCon
    fam_tc
      , Injective [Bool]
    injective_args <- TyCon -> Injectivity
    tyConInjectivityInfo TyCon
    fam_tc
      = (([Type], Subst, [TyVar], Maybe CoAxBranch) -> TcS [TypeEqn])
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)] -> TcS [TypeEqn]
    forall (m :: * -> *) (f :: * -> *) a b.
    (Monad m, Traversable f) =>
    (a -> m [b]) -> f a -> m [b]
    concatMapM ([Bool]
    -> ([Type], Subst, [TyVar], Maybe CoAxBranch) -> TcS [TypeEqn]
    injImproveEqns [Bool]
    injective_args) ([([Type], Subst, [TyVar], Maybe CoAxBranch)] -> TcS [TypeEqn])
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)] -> TcS [TypeEqn]
    forall a b. (a -> b) -> a -> b
    $
        [CoAxBranch]
    -> (CoAxBranch -> [TyVar])
    -> (CoAxBranch -> [Type])
    -> (CoAxBranch -> Type)
    -> (CoAxBranch -> Maybe CoAxBranch)
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    forall a.
    [a]
    -> (a -> [TyVar])
    -> (a -> [Type])
    -> (a -> Type)
    -> (a -> Maybe CoAxBranch)
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    buildImprovementData (Branches Branched -> [CoAxBranch]
    forall (br :: BranchFlag). Branches br -> [CoAxBranch]
    fromBranches (CoAxiom Branched -> Branches Branched
    forall (br :: BranchFlag). CoAxiom br -> Branches br
    co_ax_branches CoAxiom Branched
    ax))
                             CoAxBranch -> [TyVar]
    cab_tvs CoAxBranch -> [Type]
    cab_lhs CoAxBranch -> Type
    cab_rhs CoAxBranch -> Maybe CoAxBranch
    forall a. a -> Maybe a
    Just

      | Bool
    otherwise
      = [TypeEqn] -> TcS [TypeEqn]
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return []

      where
          in_scope :: InScopeSet
    in_scope = TyCoVarSet -> InScopeSet
    mkInScopeSet (Type -> TyCoVarSet
    tyCoVarsOfType Type
    rhs_ty)

          buildImprovementData
              :: [a]                     -- axioms for a TF (FamInst or CoAxBranch)
              -> (a -> [TyVar])          -- get bound tyvars of an axiom
              -> (a -> [Type])           -- get LHS of an axiom
              -> (a -> Type)             -- get RHS of an axiom
              -> (a -> Maybe CoAxBranch) -- Just => apartness check required
              -> [( [Type], Subst, [TyVar], Maybe CoAxBranch )]
                 -- Result:
                 -- ( [arguments of a matching axiom]
                 -- , RHS-unifying substitution
                 -- , axiom variables without substitution
                 -- , Maybe matching axiom [Nothing - open TF, Just - closed TF ] )
          buildImprovementData :: forall a.
    [a]
    -> (a -> [TyVar])
    -> (a -> [Type])
    -> (a -> Type)
    -> (a -> Maybe CoAxBranch)
    -> [([Type], Subst, [TyVar], Maybe CoAxBranch)]
    buildImprovementData [a]
    axioms a -> [TyVar]
    axiomTVs a -> [Type]
    axiomLHS a -> Type
    axiomRHS a -> Maybe CoAxBranch
    wrap =
              [ ([Type]
    ax_args, Subst
    subst, [TyVar]
    unsubstTvs, a -> Maybe CoAxBranch
    wrap a
    axiom)
              | a
    axiom <- [a]
    axioms
              , let ax_args :: [Type]
    ax_args = a -> [Type]
    axiomLHS a
    axiom
                    ax_rhs :: Type
    ax_rhs  = a -> Type
    axiomRHS a
    axiom
                    ax_tvs :: [TyVar]
    ax_tvs  = a -> [TyVar]
    axiomTVs a
    axiom
                    in_scope1 :: InScopeSet
    in_scope1 = InScopeSet
    in_scope InScopeSet -> [TyVar] -> InScopeSet
    `extendInScopeSetList` [TyVar]
    ax_tvs
              , Just Subst
    subst <- [Bool -> InScopeSet -> Type -> Type -> Maybe Subst
    tcUnifyTyWithTFs Bool
    False InScopeSet
    in_scope1 Type
    ax_rhs Type
    rhs_ty]
              , let notInSubst :: TyVar -> Bool
    notInSubst TyVar
    tv = Bool -> Bool
    not (TyVar
    tv TyVar -> VarEnv Type -> Bool
    forall a. TyVar -> VarEnv a -> Bool
    `elemVarEnv` Subst -> VarEnv Type
    getTvSubstEnv Subst
    subst)
                    unsubstTvs :: [TyVar]
    unsubstTvs    = (TyVar -> Bool) -> [TyVar] -> [TyVar]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (TyVar -> Bool
    notInSubst (TyVar -> Bool) -> (TyVar -> Bool) -> TyVar -> Bool
    forall (f :: * -> *). Applicative f => f Bool -> f Bool -> f Bool
    <&&> TyVar -> Bool
    isTyVar) [TyVar]
    ax_tvs ]
                       -- The order of unsubstTvs is important; it must be
                       -- in telescope order e.g. (k:*) (a:k)

          injImproveEqns :: [Bool]
                         -> ([Type], Subst, [TyCoVar], Maybe CoAxBranch)
                         -> TcS [TypeEqn]
          injImproveEqns :: [Bool]
    -> ([Type], Subst, [TyVar], Maybe CoAxBranch) -> TcS [TypeEqn]
    injImproveEqns [Bool]
    inj_args ([Type]
    ax_args, Subst
    subst, [TyVar]
    unsubstTvs, Maybe CoAxBranch
    cabr)
            = do { subst1 <- Subst -> [TyVar] -> TcS Subst
    instFlexiX Subst
    subst [TyVar]
    unsubstTvs
                      -- If the current substitution bind [k -> *], and
                      -- one of the un-substituted tyvars is (a::k), we'd better
                      -- be sure to apply the current substitution to a's kind.
                      -- Hence instFlexiX.   #13135 was an example.

                 ; return [ Pair (substTy subst1 ax_arg) arg
                            -- NB: the ax_arg part is on the left
                            -- see Note [Improvement orientation]
                          | case cabr of
                              Just CoAxBranch
    cabr' -> [Type] -> CoAxBranch -> Bool
    apartnessCheck (HasDebugCallStack => Subst -> [Type] -> [Type]
    Subst -> [Type] -> [Type]
    substTys Subst
    subst1 [Type]
    ax_args) CoAxBranch
    cabr'
                              Maybe CoAxBranch
    _          -> Bool
    True
                          , (ax_arg, arg, True) <- zip3 ax_args args inj_args ] }

    improveLocalFunEqs :: InertCans -> TyCon -> [TcType] -> EqCt -> TcS Bool
    -- Generate improvement equalities, by comparing
    -- the current work item with inert CFunEqs
    -- E.g.   x + y ~ z,   x + y' ~ z   =>   [W] y ~ y'
    --
    -- See Note [FunDep and implicit parameter reactions]
    improveLocalFunEqs :: InertCans -> TyCon -> [Type] -> EqCt -> TcS Bool
    improveLocalFunEqs InertCans
    inerts TyCon
    fam_tc [Type]
    args (EqCt { eq_ev :: EqCt -> CtEvidence
    eq_ev = CtEvidence
    work_ev, eq_rhs :: EqCt -> Type
    eq_rhs = Type
    rhs })
      | [FunDepEqn (CtLoc, RewriterSet)] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [FunDepEqn (CtLoc, RewriterSet)]
    improvement_eqns
      = Bool -> TcS Bool
    forall a. a -> TcS a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False
      | Bool
    otherwise
      = do { String -> SDoc -> TcS ()
    traceTcS String
    "interactFunEq improvements: " (SDoc -> TcS ()) -> SDoc -> TcS ()
    forall a b. (a -> b) -> a -> b
    $
                       [SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "Eqns:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> [FunDepEqn (CtLoc, RewriterSet)] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [FunDepEqn (CtLoc, RewriterSet)]
    improvement_eqns
                            , String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "Candidates:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> [EqCt] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [EqCt]
    funeqs_for_tc
                            , String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "Inert eqs:" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> InertEqs -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr (InertCans -> InertEqs
    inert_eqs InertCans
    inerts) ]
           ; CtEvidence -> [FunDepEqn (CtLoc, RewriterSet)] -> TcS Bool
    emitFunDepWanteds CtEvidence
    work_ev [FunDepEqn (CtLoc, RewriterSet)]
    improvement_eqns }
      where
        funeqs :: InertFunEqs
    funeqs        = InertCans -> InertFunEqs
    inert_funeqs InertCans
    inerts
        funeqs_for_tc :: [EqCt]
        funeqs_for_tc :: [EqCt]
    funeqs_for_tc = [ EqCt
    funeq_ct | [EqCt]
    equal_ct_list <- InertFunEqs -> TyCon -> [[EqCt]]
    forall a. FunEqMap a -> TyCon -> [a]
    findFunEqsByTyCon InertFunEqs
    funeqs TyCon
    fam_tc
                                   , EqCt
    funeq_ct <- [EqCt]
    equal_ct_list
                                   , EqRel
    NomEq EqRel -> EqRel -> Bool
    forall a. Eq a => a -> a -> Bool
    == EqCt -> EqRel
    eq_eq_rel EqCt
    funeq_ct ]
                                      -- representational equalities don't interact
                                      -- with type family dependencies
        work_loc :: CtLoc
    work_loc      = CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    work_ev
        work_pred :: Type
    work_pred     = CtEvidence -> Type
    ctEvPred CtEvidence
    work_ev
        fam_inj_info :: Injectivity
    fam_inj_info  = TyCon -> Injectivity
    tyConInjectivityInfo TyCon
    fam_tc

        --------------------
        improvement_eqns :: [FunDepEqn (CtLoc, RewriterSet)]
        improvement_eqns :: [FunDepEqn (CtLoc, RewriterSet)]
    improvement_eqns
          | Just BuiltInSynFamily
    ops <- TyCon -> Maybe BuiltInSynFamily
    isBuiltInSynFamTyCon_maybe TyCon
    fam_tc
          =    -- Try built-in families, notably for arithmethic
            (EqCt -> [FunDepEqn (CtLoc, RewriterSet)])
    -> [EqCt] -> [FunDepEqn (CtLoc, RewriterSet)]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap (BuiltInSynFamily
    -> Type -> EqCt -> [FunDepEqn (CtLoc, RewriterSet)]
    do_one_built_in BuiltInSynFamily
    ops Type
    rhs) [EqCt]
    funeqs_for_tc

          | Injective [Bool]
    injective_args <- Injectivity
    fam_inj_info
          =    -- Try improvement from type families with injectivity annotations
            (EqCt -> [FunDepEqn (CtLoc, RewriterSet)])
    -> [EqCt] -> [FunDepEqn (CtLoc, RewriterSet)]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap ([Bool] -> Type -> EqCt -> [FunDepEqn (CtLoc, RewriterSet)]
    do_one_injective [Bool]
    injective_args Type
    rhs) [EqCt]
    funeqs_for_tc

          | Bool
    otherwise
          = []

        --------------------
        do_one_built_in :: BuiltInSynFamily
    -> Type -> EqCt -> [FunDepEqn (CtLoc, RewriterSet)]
    do_one_built_in BuiltInSynFamily
    ops Type
    rhs (EqCt { eq_lhs :: EqCt -> CanEqLHS
    eq_lhs = TyFamLHS TyCon
    _ [Type]
    iargs, eq_rhs :: EqCt -> Type
    eq_rhs = Type
    irhs, eq_ev :: EqCt -> CtEvidence
    eq_ev = CtEvidence
    inert_ev })
          | Bool -> Bool
    not (CtEvidence -> Bool
    isGiven CtEvidence
    inert_ev Bool -> Bool -> Bool
    && CtEvidence -> Bool
    isGiven CtEvidence
    work_ev)  -- See Note [No Given/Given fundeps]
          = CtEvidence -> [TypeEqn] -> [FunDepEqn (CtLoc, RewriterSet)]
    mk_fd_eqns CtEvidence
    inert_ev (BuiltInSynFamily -> [Type] -> Type -> [Type] -> Type -> [TypeEqn]
    sfInteractInert BuiltInSynFamily
    ops [Type]
    args Type
    rhs [Type]
    iargs Type
    irhs)

          | Bool
    otherwise
          = []

        do_one_built_in BuiltInSynFamily
    _ Type
    _ EqCt
    _ = String -> SDoc -> [FunDepEqn (CtLoc, RewriterSet)]
    forall a. HasCallStack => String -> SDoc -> a
    pprPanic String
    "interactFunEq 1" (TyCon -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyCon
    fam_tc) -- TyVarLHS

        --------------------
        -- See Note [Type inference for type families with injectivity]
        do_one_injective :: [Bool] -> Type -> EqCt -> [FunDepEqn (CtLoc, RewriterSet)]
    do_one_injective [Bool]
    inj_args Type
    rhs (EqCt { eq_lhs :: EqCt -> CanEqLHS
    eq_lhs = TyFamLHS TyCon
    _ [Type]
    inert_args
                                            , eq_rhs :: EqCt -> Type
    eq_rhs = Type
    irhs, eq_ev :: EqCt -> CtEvidence
    eq_ev = CtEvidence
    inert_ev })
          | Bool -> Bool
    not (CtEvidence -> Bool
    isGiven CtEvidence
    inert_ev Bool -> Bool -> Bool
    && CtEvidence -> Bool
    isGiven CtEvidence
    work_ev) -- See Note [No Given/Given fundeps]
          , Type
    rhs HasDebugCallStack => Type -> Type -> Bool
    Type -> Type -> Bool
    `tcEqType` Type
    irhs
          = CtEvidence -> [TypeEqn] -> [FunDepEqn (CtLoc, RewriterSet)]
    mk_fd_eqns CtEvidence
    inert_ev ([TypeEqn] -> [FunDepEqn (CtLoc, RewriterSet)])
    -> [TypeEqn] -> [FunDepEqn (CtLoc, RewriterSet)]
    forall a b. (a -> b) -> a -> b
    $ [ Type -> Type -> TypeEqn
    forall a. a -> a -> Pair a
    Pair Type
    arg Type
    iarg
                                  | (Type
    arg, Type
    iarg, Bool
    True) <- [Type] -> [Type] -> [Bool] -> [(Type, Type, Bool)]
    forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
    zip3 [Type]
    args [Type]
    inert_args [Bool]
    inj_args ]
          | Bool
    otherwise
          = []

        do_one_injective [Bool]
    _ Type
    _ EqCt
    _ = String -> SDoc -> [FunDepEqn (CtLoc, RewriterSet)]
    forall a. HasCallStack => String -> SDoc -> a
    pprPanic String
    "interactFunEq 2" (TyCon -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyCon
    fam_tc)  -- TyVarLHS

        --------------------
        mk_fd_eqns :: CtEvidence -> [TypeEqn] -> [FunDepEqn (CtLoc, RewriterSet)]
        mk_fd_eqns :: CtEvidence -> [TypeEqn] -> [FunDepEqn (CtLoc, RewriterSet)]
    mk_fd_eqns CtEvidence
    inert_ev [TypeEqn]
    eqns
          | [TypeEqn] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [TypeEqn]
    eqns  = []
          | Bool
    otherwise  = [ FDEqn { fd_qtvs :: [TyVar]
    fd_qtvs = [], fd_eqs :: [TypeEqn]
    fd_eqs = [TypeEqn]
    eqns
                                 , fd_pred1 :: Type
    fd_pred1 = Type
    work_pred
                                 , fd_pred2 :: Type
    fd_pred2 = Type
    inert_pred
                                 , fd_loc :: (CtLoc, RewriterSet)
    fd_loc   = (CtLoc
    loc, RewriterSet
    inert_rewriters) } ]
          where
            initial_loc :: CtLoc
    initial_loc  -- start with the location of the Wanted involved
              | CtEvidence -> Bool
    isGiven CtEvidence
    work_ev = CtLoc
    inert_loc
              | Bool
    otherwise       = CtLoc
    work_loc
            eqn_orig :: CtOrigin
    eqn_orig        = Type
    -> CtOrigin
    -> RealSrcSpan
    -> Type
    -> CtOrigin
    -> RealSrcSpan
    -> CtOrigin
    InjTFOrigin1 Type
    work_pred (CtLoc -> CtOrigin
    ctLocOrigin CtLoc
    work_loc) (CtLoc -> RealSrcSpan
    ctLocSpan CtLoc
    work_loc)
                                           Type
    inert_pred (CtLoc -> CtOrigin
    ctLocOrigin CtLoc
    inert_loc) (CtLoc -> RealSrcSpan
    ctLocSpan CtLoc
    inert_loc)
            eqn_loc :: CtLoc
    eqn_loc         = CtLoc -> CtOrigin -> CtLoc
    setCtLocOrigin CtLoc
    initial_loc CtOrigin
    eqn_orig
            inert_pred :: Type
    inert_pred      = CtEvidence -> Type
    ctEvPred CtEvidence
    inert_ev
            inert_loc :: CtLoc
    inert_loc       = CtEvidence -> CtLoc
    ctEvLoc CtEvidence
    inert_ev
            inert_rewriters :: RewriterSet
    inert_rewriters = CtEvidence -> RewriterSet
    ctEvRewriters CtEvidence
    inert_ev
            loc :: CtLoc
    loc = CtLoc
    eqn_loc { ctl_depth = ctl_depth inert_loc `maxSubGoalDepth`
                                        ctl_depth work_loc }

    {- Note [Type inference for type families with injectivity]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose we have a type family with an injectivity annotation:
        type family F a b = r | r -> b

    Then if we have an equality like F s1 t1 ~ F s2 t2,
    we can use the injectivity to get a new Wanted constraint on
    the injective argument
      [W] t1 ~ t2

    That in turn can help GHC solve constraints that would otherwise require
    guessing.  For example, consider the ambiguity check for
       f :: F Int b -> Int
    We get the constraint
       [W] F Int b ~ F Int beta
    where beta is a unification variable.  Injectivity lets us pick beta ~ b.

    Injectivity information is also used at the call sites. For example:
       g = f True
    gives rise to
       [W] F Int b ~ Bool
    from which we can derive b.  This requires looking at the defining equations of
    a type family, ie. finding equation with a matching RHS (Bool in this example)
    and inferring values of type variables (b in this example) from the LHS patterns
    of the matching equation.  For closed type families we have to perform
    additional apartness check for the selected equation to check that the selected
    is guaranteed to fire for given LHS arguments.

    These new constraints are Wanted constraints, but we will not use the evidence.
    We could go further and offer evidence from decomposing injective type-function
    applications, but that would require new evidence forms, and an extension to
    FC, so we don't do that right now (Dec 14).

    We generate these Wanteds in three places, depending on how we notice the
    injectivity.

    1. When we have a [W] F tys1 ~ F tys2. This is handled in canEqCanLHS2, and
    described in Note [Decomposing type family applications] in GHC.Tc.Solver.Equality

    2. When we have [W] F tys1 ~ T and [W] F tys2 ~ T. Note that neither of these
    constraints rewrites the other, as they have different LHSs. This is done
    in improveLocalFunEqs, called during the interactWithInertsStage.

    3. When we have [W] F tys ~ T and an equation for F that looks like F tys' = T.
    This is done in improve_top_fun_eqs, called from the top-level reactions stage.

    See also Note [Injective type families] in GHC.Core.TyCon

    Note [Cache-caused loops]
    ~~~~~~~~~~~~~~~~~~~~~~~~~
    It is very dangerous to cache a rewritten wanted family equation as 'solved' in our
    solved cache (which is the default behaviour or xCtEvidence), because the interaction
    may not be contributing towards a solution. Here is an example:

    Initial inert set:
      [W] g1 : F a ~ beta1
    Work item:
      [W] g2 : F a ~ beta2
    The work item will react with the inert yielding the _same_ inert set plus:
        (i)   Will set g2 := g1 `cast` g3
        (ii)  Will add to our solved cache that [S] g2 : F a ~ beta2
        (iii) Will emit [W] g3 : beta1 ~ beta2
    Now, the g3 work item will be spontaneously solved to [G] g3 : beta1 ~ beta2
    and then it will react the item in the inert ([W] g1 : F a ~ beta1). So it
    will set
          g1 := g ; sym g3
    and what is g? Well it would ideally be a new goal of type (F a ~ beta2) but
    remember that we have this in our solved cache, and it is ... g2! In short we
    created the evidence loop:

            g2 := g1 ; g3
            g3 := refl
            g1 := g2 ; sym g3

    To avoid this situation we do not cache as solved any workitems (or inert)
    which did not really made a 'step' towards proving some goal. Solved's are
    just an optimization so we don't lose anything in terms of completeness of
    solving.
    -}