    -- (c) The University of Glasgow 2006

    {-# LANGUAGE ScopedTypeVariables, PatternSynonyms, MultiWayIf #-}

    {-# LANGUAGE DeriveFunctor #-}

    module GHC.Core.Unify (
            tcMatchTy, tcMatchTyKi,
            tcMatchTys, tcMatchTyKis,
            tcMatchTyX, tcMatchTysX, tcMatchTyKisX,
            tcMatchTyX_BM, ruleMatchTyKiX,

            -- Side-effect free unification
            tcUnifyTy, tcUnifyTys, tcUnifyFunDeps, tcUnifyDebugger,
            tcUnifyTysFG, tcUnifyTyForInjectivity,
            BindTvFun, BindFamFun, BindFlag(..),
            matchBindTv, alwaysBindTv, alwaysBindFam, dontCareBindFam,
            UnifyResult, UnifyResultM(..), MaybeApartReason(..),
            typesCantMatch, typesAreApart,

            -- Matching a type against a lifted type (coercion)
            liftCoMatch
       ) where

    import GHC.Prelude

    import GHC.Types.Var
    import GHC.Types.Var.Env
    import GHC.Types.Var.Set
    import GHC.Builtin.Names( tYPETyConKey, cONSTRAINTTyConKey )
    import GHC.Core.Type     hiding ( getTvSubstEnv )
    import GHC.Core.Coercion hiding ( getCvSubstEnv )
    import GHC.Core.Predicate( scopedSort )
    import GHC.Core.TyCon
    import GHC.Core.Predicate( CanEqLHS(..), canEqLHS_maybe )
    import GHC.Core.TyCon.Env
    import GHC.Core.TyCo.Rep
    import GHC.Core.TyCo.Compare ( eqType, tcEqType, tcEqTyConAppArgs )
    import GHC.Core.TyCo.FVs     ( tyCoVarsOfCoList, tyCoFVsOfTypes )
    import GHC.Core.TyCo.Subst   ( mkTvSubst )
    import GHC.Core.Map.Type
    import GHC.Core.Multiplicity

    import GHC.Utils.FV( FV, fvVarList )
    import GHC.Utils.Misc
    import GHC.Utils.Outputable
    import GHC.Types.Basic( SwapFlag(..) )
    import GHC.Types.Unique.FM
    import GHC.Exts( oneShot )
    import GHC.Utils.Panic

    import GHC.Data.Pair
    import GHC.Data.TrieMap
    import GHC.Data.Maybe( orElse )

    import Control.Monad
    import qualified Data.Semigroup as S
    import GHC.Builtin.Types.Prim (fUNTyCon)

    {- Note [The Core unifier]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~
    This module contains the (pure) unifier two types.  It is subtle in a number
    of ways.  Here we summarise, but see Note [Specification of unification].

    (CU1) It creates a substition only for "bindable" or "template" type variables.
      These are identified by a `um_bind_tv_fun` function passed down in the `UMEnv`
      environment.

    (CU2) We want to match in the presence of foralls;
            e.g     (forall a. t1) ~ (forall b. t2)
       That is what the `um_rn_env :: RnEnv2` field of `UMEnv` is for; it does the
       alpha-renaming that makes it as if `a` and `b` were the same variable.
       Initialising the `RnEnv2`, so that it can generate a fresh binder when
       necessary, entails knowing the free variables of both types.

       Of course, we must be careful not to bind a template type variable to a
       locally bound variable.  E.g.
            (forall a. x) ~ (forall b. b)
       where `x` is the template type variable.  Then we do not want to
       bind `x` to a/b!  See `mentionsForAllBoundTyVarsL/R`.

    (CU3) We want to take special care for type families.
      See the big Note [Apartness and type families]

    (CU4) Rather than returning just "unifiable" or "not-unifiable" we do "fine-grained"
      unification (hence "fg" or "FG" in this module) returning three possiblities,
      captured in `UnifyResult`:
        - Unifiable subst : certainly unifiable with this type substitution
        - SurelyApart     : cannot be unifiable, regardless of how type familes reduce
        - MaybeApart      : neither of the above
      See Note [Unification result].

      Four reasons for MaybeApart (see `MaybeApartReason`).  The first two are the
      big ones!
        * MARTypeFamily:
             Family reduction might make the two types equal
                 Maybe (F Int) ~ Maybe Bool
             See Note [Apartness and type families]
        * MARInfinite (occurs check):
             See Note [Infinitary substitutions]
        * MARTypeVsConstraint:
             See Note [Type and Constraint are not apart] in GHC.Builtin.Types.Prim
        * MARCast (obscure):
             See (KCU2) in Note [Kind coercions in Unify]

    (CU5) We need to take care with kinds.  See Note [tcMatchTy vs tcMatchTyKi]

    (CU6) The "unifier" can also do /matching/, governed by `um_unif :: AmIUnifying`.
       When matching, the LHS and RHS namespaces are unrelated. In particular, the
       bindable type variable can occur (unrelatedly) in the RHS.  E.g.
            match  (a,Maybe a) ~  ([a], Maybe [a])
       We get the substitution [a :-> [a]], without confusing the
       LHS `a` with the RHS `a`.  The substitition is "one-shot", and should not be
       iterated.

    Note [Infinitary substitutions]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Do the types (x, x) and ([y], y) unify? The answer is seemingly "no" --
    no substitution to finite types makes these match. This is the famous
    "occurs check".

    But, a substitution to *infinite* types can unify these two types:
      [x |-> [[...]]], y |-> [[[...]]] ].

    Why do we care? Consider these two type family instances:

      type instance F x x   = Int
      type instance F [y] y = Bool

    If we also have

      type instance Looper = [Looper]

    then the instances potentially overlap -- they are not "apart". So we must
    distinguish failure-to-unify from definitely-apart. The solution is to use
    unification over infinite terms. This is possible (see [1] for lots of gory
    details), but a full algorithm is a little more powerful than we need. Instead,
    we make a conservative approximation and just omit the occurs check.

      [1]: http://research.microsoft.com/en-us/um/people/simonpj/papers/ext-f/axioms-extended.pdf

    tcUnifyTys considers an occurs-check problem as the same as general unification
    failure.

    See also #8162.

    It's worth noting that unification in the presence of infinite types is not
    complete. This means that, sometimes, a closed type family does not reduce
    when it should. See test case indexed-types/should_fail/Overlap15 for an
    example.

    Note [tcMatchTy vs tcMatchTyKi]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    This module offers two variants of matching: with kinds and without.
    The TyKi variant takes two types, of potentially different kinds,
    and matches them. Along the way, it necessarily also matches their
    kinds. The Ty variant instead assumes that the kinds are already
    eqType and so skips matching up the kinds.

    How do you choose between them?

    1. If you know that the kinds of the two types are eqType, use
       the Ty variant. It is more efficient, as it does less work.

    2. If the kinds of variables in the template type might mention type families,
       use the Ty variant (and do other work to make sure the kinds
       work out). These pure unification functions do a straightforward
       syntactic unification and do no complex reasoning about type
       families. Note that the types of the variables in instances can indeed
       mention type families, so instance lookup must use the Ty variant.

       (Nothing goes terribly wrong -- no panics -- if there might be type
       families in kinds in the TyKi variant. You just might get match
       failure even though a reducing a type family would lead to success.)

    3. Otherwise, if you're sure that the variable kinds do not mention
       type families and you're not already sure that the kind of the template
       equals the kind of the target, then use the TyKi version.

    Note [Unification result]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    See `UnifyResult` and `UnifyResultM`.  When unifying t1 ~ t2, we return
    * Unifiable s, if s is a substitution such that s(t1) is syntactically the
      same as s(t2), modulo type-synonym expansion.
    * SurelyApart, if there is no substitution s such that s(t1) = s(t2),
      where "=" includes type-family reductions.
    * MaybeApart mar s, when we aren't sure. `mar` is a MaybeApartReason.

    Examples
    * [a] ~ Maybe b: SurelyApart, because [] and Maybe can't unify

    * [(a,Int)] ~ [(Bool,b)]:  Unifiable

    * [F Int] ~ [Bool]: MaybeApart MARTypeFamily, because F Int might reduce to Bool
                        (the unifier does not try this)

    * a ~ Maybe a: MaybeApart MARInfinite. Not Unifiable clearly, but not SurelyApart
        either; consider
           a := Loop
           where  type family Loop where Loop = Maybe Loop

    Wrinkle (UR1): see `combineMAR`
       There is the possibility that two types are MaybeApart for *both* reasons:

       * (a, F Int) ~ (Maybe a, Bool)

       What reason should we use? The *only* consumer of the reason is described
       in Note [Infinitary substitution in lookup] in GHC.Core.InstEnv. The goal
       there is identify which instances might match a target later (but don't
       match now) -- except that we want to ignore the possibility of infinitary
       substitutions. So let's examine a concrete scenario:

         class C a b c
         instance C a (Maybe a) Bool
         -- other instances, including one that will actually match
         [W] C b b (F Int)

       Do we want the instance as a future possibility? No. The only way that
       instance can match is in the presence of an infinite type (infinitely nested
       Maybes). We thus say that `MARInfinite` takes precedence, so that InstEnv treats
       this case as an infinitary substitution case; the fact that a type family is
       involved is only incidental. We thus define `combineMAR` to prefer
       `MARInfinite`.

    Note [Apartness and type families]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider this:

      type family F a b where
        F Int Bool = Char
        F a   b    = Double
      type family G a         -- open, no instances

    How do we reduce (F (G Float) (G Float))? The first equation clearly doesn't
    match immediately while the second equation does. But, before reducing, we must
    make sure that the target can never become (F Int Bool). Well, no matter what G
    Float becomes, it certainly won't become *both* Int and Bool, so indeed we're
    safe reducing (F (G Float) (G Float)) to Double.

    So we must say that the argument list
         (G Float) (G Float)   is SurelyApart from   Int Bool

    This is necessary not only to get more reductions (which we might be willing to
    give up on), but for /substitutivity/. If we have (F x x), we can see that (F x x)
    can reduce to Double. So, it had better be the case that (F blah blah) can
    reduce to Double, no matter what (blah) is!

    To achieve this, `go` in `uVarOrFam` does this;

    * We maintain /two/ substitutions, not just one:
         * um_tv_env: the regular substitution, mapping TyVar :-> Type
         * um_fam_env: maps (TyCon,[Type]) :-> Type, where the LHS is a type-fam application
      In effect, these constitute one substitution mapping
         CanEqLHS :-> Types

    * When we attempt to unify (G Float) ~ Int, we return MaybeApart..
      but we /also/ add a "family substitution" [G Float :-> Int],
      to `um_fam_env`. See the `BindMe` case of `go` in `uVarOrFam`.

    * When we later encounter (G Float) ~ Bool, we apply the family substitution,
      very much as we apply the conventional [tyvar :-> type] substitution
      when we encounter a type variable.  See the `lookupFamEnv` in `go` in
      `uVarOrFam`.

      So (G Float ~ Bool) becomes (Int ~ Bool) which is SurelyApart.  Bingo.

    Wrinkles

    (ATF0) Once we encounter a type-family application, we only ever return
                 MaybeApart   or   SurelyApart
      but never `Unifiable`.  Accordingly, we only return a TyCoVar substitution
      from `tcUnifyTys` and friends; we don't return a type-family substitution as
      well.  (We could imagine doing so, though.)

    (ATF1) Exactly the same mechanism is used in class-instance checking.
        If we have
            instance C (Maybe b)
            instance {-# OVERLAPPING #-} C (Maybe Bool)
            [W] C (Maybe (F a))
        we want to know that the second instance might match later, when we know more about `a`.
        The function `GHC.Core.InstEnv.instEnvMatchesAndUnifiers` uses `tcUnifyTysFG` to
        account for type families in the type being matched.

    (ATF2) A very similar check is made in `GHC.Tc.Utils.Unify.mightEqualLater`, which
      again uses `tcUnifyTysFG` to account for the possibility of type families.  See
      Note [What might equal later?] in GHC.Tc.Utils.Unify, esp example (10).

    (ATF3) What about foralls?   For example, supppose we are unifying
               (forall a. F a) -> (forall a. F a)
       against some other type. Those two (F a) types are unrelated, bound by
       different foralls; we cannot extend the um_fam_env with a binding [F a :-> blah]

       So to keep things simple, the entire family-substitution machinery is used
       only if there are no enclosing foralls (see the `under_forall` check in
       `uSatFamApp`).  That's fine, because the apartness business is used only for
       reducing type-family applications, and class instances, and their arguments
       can't have foralls anyway.

       The bottom line is that we won't discover that
           (forall a. (a, F Int, F Int))
       is surely apart from
           (forall a. (a, Int, Bool))
       but that doesn't matter.  Fixing this would be possible, but would require
       quite a bit of head-scratching.

    (ATF4) The family substitution only has /saturated/ family applications in
       its domain. Consider the following concrete example from #16995:

         type family Param :: Type -> Type   -- arity 0

         type family LookupParam (a :: Type) :: Type where
           LookupParam (f Char) = Bool
           LookupParam x        = Int

         foo :: LookupParam (Param ())
         foo = 42

       In order for `foo` to typecheck, `LookupParam (Param ())` must reduce to
       `Int`.  So    (f Char) ~ (Param ())   must be SurelyApart.  Remember, since
       `Param` is a nullary type family, it is over-saturated in (Param ()).
       This unification will only be SurelyApart if we decompose the outer AppTy
       separately, to then give (() ~ Char).

       Not only does this allow more programs to be accepted, it's also important
       for correctness. Not doing this was the root cause of the Core Lint error
       in #16995.

    (ATF5) Consider
              instance (Generic1 f, Ord (Rep1 f a))
                    => Ord (Generically1 f a) where ...
                  -- The "..." gives rise to [W] Ord (Generically1 f a)
       where Rep1 is a type family.

       We must use the instance decl (recursively) to simplify the [W] constraint;
       we do /not/ want to worry that the `[G] Ord (Rep1 f a)` might be an
       alternative path.  So `noMatchableGivenDicts` must return False;
       so `mightMatchLater` must return False; so when um_bind_fam_fun returns
       `DontBindMe`, the unifier must return `SurelyApart`, not `MaybeApart`.  See
       `go` in `uVarOrFam`

       This looks a bit sketchy, because they aren't SurelyApart, but see
       Note [What might equal later?] in GHC.Tc.Utils.Unify, esp "Red Herring".

       If we are under a forall, we return `MaybeApart`; that seems more conservative,
       and class constraints are on tau-types so it doesn't matter.

    (ATF6) When /matching/ can we ever have a type-family application on the LHS, in
       the template?  You might think not, because type-class-instance and
       type-family-instance heads can't include type families.  E.g.
                instance C (F a) where ...  -- Illegal

       But you'd be wrong: even when matching, we can see type families in the LHS template:
       * In `checkValidClass`, in `check_dm` we check that the default method has the
          right type, using matching, both ways.  And that type may have type-family
          applications in it. Examples in test CoOpt_Singletons and T26457.

       * In the specialiser: see the call to `tcMatchTy` in
         `GHC.Core.Opt.Specialise.beats_or_same`

       * With -fpolymorphic-specialisation, we might get a specialiation rule like
             RULE forall a (d :: Eq (Maybe (F a))) .
                     f @(Maybe (F a)) d = ...
         See #25965.

       * A user-written RULE could conceivably have a type-family application
         in the template.  It might not be a good rule, but I don't think we currently
         check for this.

        In all these cases we are only interested in finding a substitution /for
        type variables/ that makes the match work.  So we simply want to recurse into
        the arguments of the type family.  E.g.
           Template:   forall a.  Maybe (F a)
           Target:     Maybe (F Int)
        We want to succeed with substitution [a :-> Int].  See (ATF9).

        Conclusion: where we enter via `tcMatchTy`, `tcMatchTys`, `tc_match_tys`,
        etc, we always end up in `tc_match_tys_x`.  There we invoke the unifier
        but we do not distinguish between `SurelyApart` and `MaybeApart`. So in
        these cases we can set `um_bind_fam_fun` to `neverBindFam`.

    (ATF7) There is one other, very special case of matching where we /do/ want to
       bind type families in `um_fam_env`, namely in GHC.Tc.Solver.Equality, the call
       to `tcUnifyTyForInjectivity False` in `improve_injective_wanted_top`.
       Consider
       of a match. Consider
          type family G6 a = r | r -> a
          type instance G6 [a]  = [G a]
          type instance G6 Bool = Int
       and suppose we have a Wanted constraint
          [W] G6 alpha ~ [Int]
       According to Section 5.2 of "Injective type families for Haskell", we /match/
       the RHS each of type instance with [Int].  So we try
            Template: [G a]    Target: [Int]
       and we want to succeed with MaybeApart, so that we can generate the improvement
       constraint
            [W] alpha ~ [beta]
       where beta is fresh.  We do this by binding [G a :-> Int]

    (ATF8) The treatment of type families is governed by
             um_bind_fam_fun :: BindFamFun
      in UMEnv, where
             type BindFamFun = TyCon -> [Type] -> Type -> BindFlag
      There are some simple BindFamFun functions provided:
         alwaysBindFam    do the clever stuff above
         neverBindFam     treat type families as SurelyApart
         dontCareBindFam  type families shouldn't exist at all
      This function only affects the difference between the results MaybeApart and
      SurelyApart; it never does not affect whether or not we return Unifiable.

    (ATF9) Decomposition.  Consider unifying
              F a  ~  F Int
      when `um_bind_fam_fun` says DontBindMe.  There is a unifying substitition [a :-> Int],
      and we want to find it, returning Unifiable. Why?
        - Remember, this is the Core unifier -- we are not doing type inference
        - When we have two equal types, like  F a ~ F a, it is ridiculous to say that they
          are MaybeApart.  Example: the two-way tcMatchTy in `checkValidClass` and #26457.

      (ATF9-1) But consider unifying
              F Int ~ F Bool
        Although Int and Bool are SurelyApart, we must return MaybeApart for the outer
        unification.  Hence the use of `don'tBeSoSure` in `go_fam_fam`; it leaves Unifiable
        alone, but weakens `SurelyApart` to `MaybeApart`.

      (ATF9-2) We want this decomposition to occur even under a forall (this was #26457).
        E.g.    (forall a. F Int) -> Int  ~   (forall a. F Int) ~ Int

    (ATF10) Injectivity.  Consider (AFT9) where F is known to be injective.  Then if we
      are unifying
              F Int ~ F Bool
      we /can/ say SurelyApart.  See the inj/noninj stuff in `go_fam_fam`.

    (ATF11) Consider unifying
              [F Int, F Int, F Bool]  ~  [F Bool, Char, Double]
      We find (F Int ~ F Bool), so we can decompose.  But we /also/ want to remember
      the substitution [F Int :-> F Bool].  Then from (F Int ~ Char) we get the
      substitution [F Bool :-> Char].  And that flat-out contradicts (F Bool ~ Double)
      so we should get SurelyApart.

      Key point: when decomposing (F tys1 ~ F tys2), we should /also/ extend the
      type-family substitution.

      (ATF11-1) All this cleverness only matters when unifying, not when matching

    (ATF12) There is a horrid exception for the injectivity check. See (UR1) in
      in Note [Specification of unification].

    (ATF13) We have to be careful about the occurs check.
      See Note [The occurs check in the Core unifier]

    SIDE NOTE.  The paper "Closed type families with overlapping equations"
    http://research.microsoft.com/en-us/um/people/simonpj/papers/ext-f/axioms-extended.pdf
    tries to achieve the same effect with a standard yes/no unifier, by "flattening"
    the types (replacing each type-family application with a fresh type variable)
    and then unifying.  But that does not work well. Consider (#25657)

        type MyEq :: k -> k -> Bool
        type family MyEq a b where
           MyEq a a = 'True
           MyEq _ _ = 'False

        type Var :: forall {k}. Tag -> k
        type family Var tag = a | a -> tag

    Then, because Var is injective, we want
         MyEq (Var A) (Var B) --> False
         MyEq (Var A) (Var A) --> True

    But if we flattten the types (Var A) and (Var B) we'll just get fresh type variables,
    and all is lost.  But with the current algorithm we have that
        a a   ~    (Var A) (Var B)
    is SurelyApart, so the first equation definitely doesn't match and we can try the
    second, which does.  END OF SIDE NOTE.

    Note [Shortcomings of the apartness test]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Note [Apartness and type families] is very clever.

    But it still has shortcomings (#26358).  Consider unifying
        [F a, F Int, Int]  ~  [Bool, Char, a]
    Working left to right you might think we would build the mapping
      F a   :-> Bool
      F Int :-> Char
    Now we discover that `a` unifies with `Int`. So really these two lists are Apart
    because F Int can't be both Bool and Char.

    Just the same applies when adding a type-family binding to um_fam_env:
      [F (G Float), F Int, G Float] ~ [Bool, Char, Iont]
    Again these are Apart, because (G Float = Int),
    and (F Int) can't be both Bool and Char

    But achieving this is very tricky! Perhaps whenever we unify a type variable,
    or a type family, we should run it over the domain and (maybe range) of the
    type-family mapping too?  Sigh.

    For now we make no such attempt.
    * The um_fam_env has only /un-substituted/ types.
    * We look up only /un-substituted/ types in um_fam_env

    This may make us say MaybeApart when we could say SurelyApart, but it has no
    effect on the correctness of unification: if we return Unifiable, it really is
    Unifiable.

    This is all quite subtle. suppose we have:
        um_tv_env:   c :-> b
        um_fam_env   F b :-> a
    and we are trying to add a :-> F c. We will call lookupFamEnv on (F, [c]), which will
    fail because b and c are not equal. So we go ahead and add a :-> F c as a new tyvar eq,
    getting:
        um_tv_env:   a :-> F c, c :-> b
        um_fam_env   F b :-> a

    Does that loop, like this:
       a --> F c --> F b --> a?
    No, because we do not substitute (F c) to (F b) and then look up in um_fam_env;
    we look up only un-substituted types.
    -}

    {- *********************************************************************
    *                                                                      *
                    Binding decisions
    *                                                                      *
    ********************************************************************* -}

    data BindFlag
      = BindMe      -- ^ A bindable type variable

      | DontBindMe  -- ^ Do not bind this type variable is /apart/
                    -- See also Note [Super skolems: binding when looking up instances]
                    -- in GHC.Core.InstEnv.
      deriving BindFlag -> BindFlag -> Bool
    (BindFlag -> BindFlag -> Bool)
    -> (BindFlag -> BindFlag -> Bool) -> Eq BindFlag
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: BindFlag -> BindFlag -> Bool
    == :: BindFlag -> BindFlag -> Bool
    $c/= :: BindFlag -> BindFlag -> Bool
    /= :: BindFlag -> BindFlag -> Bool
    Eq

    -- | Some unification functions are parameterised by a 'BindTvFun', which
    -- says whether or not to allow a certain unification to take place.
    -- A 'BindTvFun' takes the 'TyVar' involved along with the 'Type' it will
    -- potentially be bound to.
    --
    -- It is possible for the variable to actually be a coercion variable
    -- (Note [Matching coercion variables]), but only when one-way matching.
    -- In this case, the 'Type' will be a 'CoercionTy'.
    type BindTvFun = TyCoVar -> Type -> BindFlag

    -- | BindFamFun is similiar to BindTvFun, but deals with a saturated
    -- type-family application.  See Note [Apartness and type families].
    type BindFamFun = TyCon -> [Type] -> Type -> BindFlag

    -- | Allow binding only for any variable in the set. Variables may
    -- be bound to any type.
    -- Used when doing simple matching; e.g. can we find a substitution
    --
    -- @
    -- S = [a :-> t1, b :-> t2] such that
    --     S( Maybe (a, b->Int )  =   Maybe (Bool, Char -> Int)
    -- @
    matchBindTv :: TyCoVarSet -> BindTvFun
    matchBindTv :: TyCoVarSet -> BindTvFun
    matchBindTv TyCoVarSet
    tvs Var
    tv Type
    _ty
      | Var
    tv Var -> TyCoVarSet -> Bool
    `elemVarSet` TyCoVarSet
    tvs = BindFlag
    BindMe
      | Bool
    otherwise           = BindFlag
    DontBindMe

    -- | Allow the binding of any variable to any type
    alwaysBindTv :: BindTvFun
    alwaysBindTv :: BindTvFun
    alwaysBindTv Var
    _tv Type
    _ty = BindFlag
    BindMe

    -- | Allow the binding of a type-family application to any type
    alwaysBindFam :: BindFamFun
    -- See (ATF8) in Note [Apartness and type families]
    alwaysBindFam :: BindFamFun
    alwaysBindFam TyCon
    _tc [Type]
    _args Type
    _rhs = BindFlag
    BindMe

    dontCareBindFam :: HasCallStack => BindFamFun
    -- See (ATF8) in Note [Apartness and type families]
    dontCareBindFam :: HasCallStack => BindFamFun
    dontCareBindFam TyCon
    tc [Type]
    args Type
    rhs
      = String -> SDoc -> BindFlag
    forall a. HasCallStack => String -> SDoc -> a
    pprPanic String
    "dontCareBindFam" (SDoc -> BindFlag) -> SDoc -> BindFlag
    forall a b. (a -> b) -> a -> b
    $
        [SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ TyCon -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyCon
    tc SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> [Type] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [Type]
    args, 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 ]

    -- | Don't allow the binding of a type-family application at all
    neverBindFam :: BindFamFun
    -- See (ATF8) in Note [Apartness and type families]
    neverBindFam :: BindFamFun
    neverBindFam TyCon
    _tc [Type]
    _args Type
    _rhs = BindFlag
    DontBindMe

    {- *********************************************************************
    *                                                                      *
                    Various wrappers for matching
    *                                                                      *
    ********************************************************************* -}

    -- | @tcMatchTy t1 t2@ produces a substitution (over fvs(t1))
    -- @s@ such that @s(t1)@ equals @t2@.
    -- The returned substitution might bind coercion variables,
    -- if the variable is an argument to a GADT constructor.
    --
    -- Precondition: typeKind ty1 `eqType` typeKind ty2
    --
    -- We don't pass in a set of "template variables" to be bound
    -- by the match, because tcMatchTy (and similar functions) are
    -- always used on top-level types, so we can bind any of the
    -- free variables of the LHS.
    -- See also Note [tcMatchTy vs tcMatchTyKi]
    tcMatchTy :: HasDebugCallStack => Type -> Type -> Maybe Subst
    tcMatchTy :: HasDebugCallStack => Type -> Type -> Maybe Subst
    tcMatchTy Type
    ty1 Type
    ty2 = [Type] -> [Type] -> Maybe Subst
    HasDebugCallStack => [Type] -> [Type] -> Maybe Subst
    tcMatchTys [Type
    ty1] [Type
    ty2]

    tcMatchTyX_BM :: HasDebugCallStack
                  => BindTvFun -> Subst
                  -> Type -> Type -> Maybe Subst
    tcMatchTyX_BM :: HasDebugCallStack =>
    BindTvFun -> Subst -> Type -> Type -> Maybe Subst
    tcMatchTyX_BM BindTvFun
    bind_tv Subst
    subst Type
    ty1 Type
    ty2
      = HasDebugCallStack =>
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys_x BindTvFun
    bind_tv Bool
    False Subst
    subst [Type
    ty1] [Type
    ty2]

    -- | Like 'tcMatchTy', but allows the kinds of the types to differ,
    -- and thus matches them as well.
    -- See also Note [tcMatchTy vs tcMatchTyKi]
    tcMatchTyKi :: HasDebugCallStack => Type -> Type -> Maybe Subst
    tcMatchTyKi :: HasDebugCallStack => Type -> Type -> Maybe Subst
    tcMatchTyKi Type
    ty1 Type
    ty2
      = HasDebugCallStack =>
    BindTvFun -> Bool -> [Type] -> [Type] -> Maybe Subst
    BindTvFun -> Bool -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys BindTvFun
    alwaysBindTv Bool
    True [Type
    ty1] [Type
    ty2]

    -- | This is similar to 'tcMatchTy', but extends a substitution
    -- See also Note [tcMatchTy vs tcMatchTyKi]
    tcMatchTyX :: HasDebugCallStack
               => Subst               -- ^ Substitution to extend
               -> Type                -- ^ Template
               -> Type                -- ^ Target
               -> Maybe Subst
    tcMatchTyX :: HasDebugCallStack => Subst -> Type -> Type -> Maybe Subst
    tcMatchTyX Subst
    subst Type
    ty1 Type
    ty2
      = HasDebugCallStack =>
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys_x BindTvFun
    alwaysBindTv Bool
    False Subst
    subst [Type
    ty1] [Type
    ty2]

    -- | Like 'tcMatchTy' but over a list of types.
    -- See also Note [tcMatchTy vs tcMatchTyKi]
    tcMatchTys :: HasDebugCallStack
               => [Type]         -- ^ Template
               -> [Type]         -- ^ Target
               -> Maybe Subst    -- ^ One-shot; in principle the template
                                 -- variables could be free in the target
                                 -- See (CU6) in Note [The Core unifier]
    tcMatchTys :: HasDebugCallStack => [Type] -> [Type] -> Maybe Subst
    tcMatchTys [Type]
    tys1 [Type]
    tys2
      = HasDebugCallStack =>
    BindTvFun -> Bool -> [Type] -> [Type] -> Maybe Subst
    BindTvFun -> Bool -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys BindTvFun
    alwaysBindTv Bool
    False [Type]
    tys1 [Type]
    tys2

    -- | Like 'tcMatchTyKi' but over a list of types.
    -- See also Note [tcMatchTy vs tcMatchTyKi]
    tcMatchTyKis :: HasDebugCallStack
                 => [Type]         -- ^ Template
                 -> [Type]         -- ^ Target
                 -> Maybe Subst    -- ^ One-shot substitution
                                   -- See (CU6) in Note [The Core unifier]
    tcMatchTyKis :: HasDebugCallStack => [Type] -> [Type] -> Maybe Subst
    tcMatchTyKis [Type]
    tys1 [Type]
    tys2
      = HasDebugCallStack =>
    BindTvFun -> Bool -> [Type] -> [Type] -> Maybe Subst
    BindTvFun -> Bool -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys BindTvFun
    alwaysBindTv Bool
    True [Type]
    tys1 [Type]
    tys2

    -- | Like 'tcMatchTys', but extending a substitution
    -- See also Note [tcMatchTy vs tcMatchTyKi]
    tcMatchTysX :: HasDebugCallStack
                => Subst          -- ^ Substitution to extend
                -> [Type]         -- ^ Template
                -> [Type]         -- ^ Target
                -> Maybe Subst    -- ^ One-shot substitution
    tcMatchTysX :: HasDebugCallStack => Subst -> [Type] -> [Type] -> Maybe Subst
    tcMatchTysX Subst
    subst [Type]
    tys1 [Type]
    tys2
      = HasDebugCallStack =>
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys_x BindTvFun
    alwaysBindTv Bool
    False Subst
    subst [Type]
    tys1 [Type]
    tys2

    -- | Like 'tcMatchTyKis', but extending a substitution
    -- See also Note [tcMatchTy vs tcMatchTyKi]
    tcMatchTyKisX :: HasDebugCallStack
                  => Subst        -- ^ Substitution to extend
                  -> [Type]       -- ^ Template
                  -> [Type]       -- ^ Target
                  -> Maybe Subst  -- ^ One-shot substitution
    tcMatchTyKisX :: HasDebugCallStack => Subst -> [Type] -> [Type] -> Maybe Subst
    tcMatchTyKisX Subst
    subst [Type]
    tys1 [Type]
    tys2
      = HasDebugCallStack =>
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys_x BindTvFun
    alwaysBindTv Bool
    True Subst
    subst [Type]
    tys1 [Type]
    tys2

    -- | Same as tc_match_tys_x, but starts with an empty substitution
    tc_match_tys :: HasDebugCallStack
                 => BindTvFun
                 -> Bool          -- ^ match kinds?
                 -> [Type]
                 -> [Type]
                 -> Maybe Subst
    tc_match_tys :: HasDebugCallStack =>
    BindTvFun -> Bool -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys BindTvFun
    bind_me Bool
    match_kis [Type]
    tys1 [Type]
    tys2
      = HasDebugCallStack =>
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys_x BindTvFun
    bind_me Bool
    match_kis (InScopeSet -> Subst
    mkEmptySubst InScopeSet
    in_scope) [Type]
    tys1 [Type]
    tys2
      where
        in_scope :: InScopeSet
    in_scope = TyCoVarSet -> InScopeSet
    mkInScopeSet ([Type] -> TyCoVarSet
    tyCoVarsOfTypes [Type]
    tys1 TyCoVarSet -> TyCoVarSet -> TyCoVarSet
    `unionVarSet` [Type] -> TyCoVarSet
    tyCoVarsOfTypes [Type]
    tys2)

    -- | Worker for 'tcMatchTysX' and 'tcMatchTyKisX'
    tc_match_tys_x :: HasDebugCallStack
                   => BindTvFun
                   -> Bool          -- ^ match kinds?
                   -> Subst
                   -> [Type]
                   -> [Type]
                   -> Maybe Subst
    tc_match_tys_x :: HasDebugCallStack =>
    BindTvFun -> Bool -> Subst -> [Type] -> [Type] -> Maybe Subst
    tc_match_tys_x BindTvFun
    bind_tv Bool
    match_kis (Subst InScopeSet
    in_scope IdSubstEnv
    id_env TvSubstEnv
    tv_env CvSubstEnv
    cv_env) [Type]
    tys1 [Type]
    tys2
      = case BindFamFun
    -> BindTvFun
    -> Bool
    -> Bool
    -> Bool
    -> MultiplicityFlag
    -> RnEnv2
    -> TvSubstEnv
    -> CvSubstEnv
    -> [Type]
    -> [Type]
    -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    tc_unify_tys BindFamFun
    neverBindFam  -- (ATF7) in Note [Apartness and type families]
                          BindTvFun
    bind_tv
                          Bool
    False  -- Matching, not unifying
                          Bool
    False  -- Not an injectivity check
                          Bool
    match_kis
                          MultiplicityFlag
    RespectMultiplicities
                          (InScopeSet -> RnEnv2
    mkRnEnv2 InScopeSet
    in_scope) TvSubstEnv
    tv_env CvSubstEnv
    cv_env [Type]
    tys1 [Type]
    tys2 of
          Unifiable (TvSubstEnv
    tv_env', CvSubstEnv
    cv_env')
            -> Subst -> Maybe Subst
    forall a. a -> Maybe a
    Just (Subst -> Maybe Subst) -> Subst -> Maybe Subst
    forall a b. (a -> b) -> a -> b
    $ InScopeSet -> IdSubstEnv -> TvSubstEnv -> CvSubstEnv -> Subst
    Subst InScopeSet
    in_scope IdSubstEnv
    id_env TvSubstEnv
    tv_env' CvSubstEnv
    cv_env'
          UnifyResultM (TvSubstEnv, CvSubstEnv)
    _ -> Maybe Subst
    forall a. Maybe a
    Nothing

    -- | This one is called from the expression matcher,
    -- which already has a MatchEnv in hand
    ruleMatchTyKiX
      :: TyCoVarSet          -- ^ template variables
      -> RnEnv2
      -> TvSubstEnv          -- ^ type substitution to extend
      -> Type                -- ^ Template
      -> Type                -- ^ Target
      -> Maybe TvSubstEnv
    ruleMatchTyKiX :: TyCoVarSet
    -> RnEnv2 -> TvSubstEnv -> Type -> Type -> Maybe TvSubstEnv
    ruleMatchTyKiX TyCoVarSet
    tmpl_tvs RnEnv2
    rn_env TvSubstEnv
    tenv Type
    tmpl Type
    target
    -- See Note [Kind coercions in Unify]
      = case BindFamFun
    -> BindTvFun
    -> Bool
    -> Bool
    -> Bool
    -> MultiplicityFlag
    -> RnEnv2
    -> TvSubstEnv
    -> CvSubstEnv
    -> [Type]
    -> [Type]
    -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    tc_unify_tys BindFamFun
    neverBindFam (TyCoVarSet -> BindTvFun
    matchBindTv TyCoVarSet
    tmpl_tvs)
          -- neverBindFam: a type family probably shouldn't appear
          -- on the LHS of a RULE, although we don't currently prevent it.
          -- But even if it did, (ATF8) in Note [Apartness and type families]
          -- says it doesn't matter becuase here we only care about Unifiable.
          -- So neverBindFam is efficient, and sufficient.
                          Bool
    False    -- Matching, not unifying
                          Bool
    False    -- No doing an injectivity check
                          Bool
    True     -- Match the kinds
                          MultiplicityFlag
    IgnoreMultiplicities
                            -- See Note [Rewrite rules ignore multiplicities in FunTy]
                          RnEnv2
    rn_env TvSubstEnv
    tenv CvSubstEnv
    emptyCvSubstEnv [Type
    tmpl] [Type
    target] of
          Unifiable (TvSubstEnv
    tenv', CvSubstEnv
    _) -> TvSubstEnv -> Maybe TvSubstEnv
    forall a. a -> Maybe a
    Just TvSubstEnv
    tenv'
          UnifyResultM (TvSubstEnv, CvSubstEnv)
    _                    -> Maybe TvSubstEnv
    forall a. Maybe a
    Nothing

    {-
    ************************************************************************
    *                                                                      *
                    GADTs
    *                                                                      *
    ************************************************************************

    Note [Pruning dead case alternatives]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider        data T a where
                       T1 :: T Int
                       T2 :: T a

                    newtype X = MkX Int
                    newtype Y = MkY Char

                    type family F a
                    type instance F Bool = Int

    Now consider    case x of { T1 -> e1; T2 -> e2 }

    The question before the house is this: if I know something about the type
    of x, can I prune away the T1 alternative?

    Suppose x::T Char.  It's impossible to construct a (T Char) using T1,
            Answer = YES we can prune the T1 branch (clearly)

    Suppose x::T (F a), where 'a' is in scope.  Then 'a' might be instantiated
    to 'Bool', in which case x::T Int, so
            ANSWER = NO (clearly)

    We see here that we want precisely the apartness check implemented within
    tcUnifyTysFG. So that's what we do! Two types cannot match if they are surely
    apart. Note that since we are simply dropping dead code, a conservative test
    suffices.
    -}

    -- | Given a list of pairs of types, are any two members of a pair surely
    -- apart, even after arbitrary type function evaluation and substitution?
    typesCantMatch :: [(Type,Type)] -> Bool
    -- See Note [Pruning dead case alternatives]
    typesCantMatch :: [(Type, Type)] -> Bool
    typesCantMatch [(Type, Type)]
    prs = ((Type, Type) -> Bool) -> [(Type, Type)] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any ((Type -> Type -> Bool) -> (Type, Type) -> Bool
    forall a b c. (a -> b -> c) -> (a, b) -> c
    uncurry Type -> Type -> Bool
    typesAreApart) [(Type, Type)]
    prs

    typesAreApart :: Type -> Type -> Bool
    typesAreApart :: Type -> Type -> Bool
    typesAreApart Type
    t1 Type
    t2 = case BindFamFun -> BindTvFun -> [Type] -> [Type] -> UnifyResult
    tcUnifyTysFG BindFamFun
    alwaysBindFam BindTvFun
    alwaysBindTv [Type
    t1] [Type
    t2] of
                            UnifyResult
    SurelyApart -> Bool
    True
                            UnifyResult
    _           -> Bool
    False
    {-
    ************************************************************************
    *                                                                      *
                 Various wrappers for unification
    *                                                                      *
    ********************************************************************* -}

    -- | Simple unification of two types; all type variables are bindable
    -- Precondition: the kinds are already equal
    tcUnifyTy :: Type -> Type       -- All tyvars are bindable
              -> Maybe Subst
                           -- A regular one-shot (idempotent) substitution
    tcUnifyTy :: Type -> Type -> Maybe Subst
    tcUnifyTy Type
    t1 Type
    t2 = BindTvFun -> [Type] -> [Type] -> Maybe Subst
    tcUnifyTys BindTvFun
    alwaysBindTv [Type
    t1] [Type
    t2]

    tcUnifyDebugger :: Type -> Type -> Maybe Subst
    tcUnifyDebugger :: Type -> Type -> Maybe Subst
    tcUnifyDebugger Type
    t1 Type
    t2
      = case Bool -> BindFamFun -> BindTvFun -> [Type] -> [Type] -> UnifyResult
    tc_unify_tys_fg
                 Bool
    True            -- Unify kinds
                 BindFamFun
    neverBindFam    -- Does not affect Unifiable, so pick max efficient
                                 -- See (ATF8) in Note [Apartness and type families]
                 BindTvFun
    alwaysBindTv
                 [Type
    t1] [Type
    t2] of
          Unifiable Subst
    subst -> Subst -> Maybe Subst
    forall a. a -> Maybe a
    Just Subst
    subst
          UnifyResult
    _               -> Maybe Subst
    forall a. Maybe a
    Nothing

    -- | Like 'tcUnifyTys' but also unifies the kinds
    tcUnifyFunDeps :: TyCoVarSet
                   -> [Type] -> [Type]
                   -> Maybe Subst
    tcUnifyFunDeps :: TyCoVarSet -> [Type] -> [Type] -> Maybe Subst
    tcUnifyFunDeps TyCoVarSet
    qtvs [Type]
    tys1 [Type]
    tys2
      = case Bool -> BindFamFun -> BindTvFun -> [Type] -> [Type] -> UnifyResult
    tc_unify_tys_fg
                 Bool
    True               -- Unify kinds
                 HasCallStack => BindFamFun
    BindFamFun
    dontCareBindFam    -- Class-instance heads never mention type families
                 (TyCoVarSet -> BindTvFun
    matchBindTv TyCoVarSet
    qtvs)
                 [Type]
    tys1 [Type]
    tys2 of
          Unifiable Subst
    subst -> Subst -> Maybe Subst
    forall a. a -> Maybe a
    Just Subst
    subst
          UnifyResult
    _               -> Maybe Subst
    forall a. Maybe a
    Nothing

    -- | Unify or match a type-family RHS with a type (possibly another type-family RHS)
    -- Precondition: kinds are the same
    tcUnifyTyForInjectivity
        :: AmIUnifying  -- ^ True <=> do two-way unification;
                        --   False <=> do one-way matching.
                        --   See end of sec 5.2 from the paper
        -> InScopeSet     -- Should include the free tyvars of both Type args
        -> Type -> Type   -- Types to unify
        -> Maybe Subst
    -- This algorithm is an implementation of the "Algorithm U" presented in
    -- the paper "Injective type families for Haskell", Figures 2 and 3.
    -- The code is incorporated with the standard unifier for convenience, but
    -- its operation should match the specification in the paper.
    tcUnifyTyForInjectivity :: Bool -> InScopeSet -> Type -> Type -> Maybe Subst
    tcUnifyTyForInjectivity Bool
    unif InScopeSet
    in_scope Type
    t1 Type
    t2
      = case BindFamFun
    -> BindTvFun
    -> Bool
    -> Bool
    -> Bool
    -> MultiplicityFlag
    -> RnEnv2
    -> TvSubstEnv
    -> CvSubstEnv
    -> [Type]
    -> [Type]
    -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    tc_unify_tys BindFamFun
    alwaysBindFam BindTvFun
    alwaysBindTv
                           Bool
    unif   -- Am I unifying?
                           Bool
    True   -- Do injectivity checks
                           Bool
    False  -- Don't check outermost kinds
                           MultiplicityFlag
    RespectMultiplicities
                           RnEnv2
    rn_env TvSubstEnv
    emptyTvSubstEnv CvSubstEnv
    emptyCvSubstEnv
                           [Type
    t1] [Type
    t2] of
          Unifiable          (TvSubstEnv
    tv_subst, CvSubstEnv
    _cv_subst) -> Subst -> Maybe Subst
    forall a. a -> Maybe a
    Just (Subst -> Maybe Subst) -> Subst -> Maybe Subst
    forall a b. (a -> b) -> a -> b
    $ TvSubstEnv -> Subst
    maybe_fix TvSubstEnv
    tv_subst
          MaybeApart MaybeApartReason
    _reason (TvSubstEnv
    tv_subst, CvSubstEnv
    _cv_subst) -> Subst -> Maybe Subst
    forall a. a -> Maybe a
    Just (Subst -> Maybe Subst) -> Subst -> Maybe Subst
    forall a b. (a -> b) -> a -> b
    $ TvSubstEnv -> Subst
    maybe_fix TvSubstEnv
    tv_subst
                     -- We want to *succeed* in questionable cases.
                     -- This is a pre-unification algorithm.
          UnifyResultM (TvSubstEnv, CvSubstEnv)
    SurelyApart      -> Maybe Subst
    forall a. Maybe a
    Nothing
      where
        rn_env :: RnEnv2
    rn_env   = InScopeSet -> RnEnv2
    mkRnEnv2 InScopeSet
    in_scope

        maybe_fix :: TvSubstEnv -> Subst
    maybe_fix | Bool
    unif      = InScopeSet -> TvSubstEnv -> Subst
    niFixSubst InScopeSet
    in_scope
                  | Bool
    otherwise = InScopeSet -> TvSubstEnv -> Subst
    mkTvSubst InScopeSet
    in_scope -- when matching, don't confuse
                                                   -- domain with range

    -----------------
    tcUnifyTys :: BindTvFun
               -> [Type] -> [Type]
               -> Maybe Subst
                                    -- ^ A regular one-shot (idempotent) substitution
                                    -- that unifies the erased types. See comments
                                    -- for 'tcUnifyTysFG'

    -- The two types may have common type variables, and indeed do so in the
    -- second call to tcUnifyTys in GHC.Tc.Instance.FunDeps.checkClsFD
    tcUnifyTys :: BindTvFun -> [Type] -> [Type] -> Maybe Subst
    tcUnifyTys BindTvFun
    bind_fn [Type]
    tys1 [Type]
    tys2
      = case BindFamFun -> BindTvFun -> [Type] -> [Type] -> UnifyResult
    tcUnifyTysFG BindFamFun
    neverBindFam BindTvFun
    bind_fn [Type]
    tys1 [Type]
    tys2 of
          Unifiable Subst
    result -> Subst -> Maybe Subst
    forall a. a -> Maybe a
    Just Subst
    result
          UnifyResult
    _                -> Maybe Subst
    forall a. Maybe a
    Nothing

    -- | (tcUnifyTysFG bind_fam bind_tv tys1 tys2) does "fine-grain" unification
    -- of tys1 and tys2, under the control of `bind_fam` and `bind_tv`.
    -- This version requires that the kinds of the types are the same,
    -- if you unify left-to-right.
    -- See Note [The Core unifier]
    tcUnifyTysFG :: BindFamFun -> BindTvFun
                 -> [Type] -> [Type]
                 -> UnifyResult
    tcUnifyTysFG :: BindFamFun -> BindTvFun -> [Type] -> [Type] -> UnifyResult
    tcUnifyTysFG BindFamFun
    bind_fam BindTvFun
    bind_tv [Type]
    tys1 [Type]
    tys2
      = Bool -> BindFamFun -> BindTvFun -> [Type] -> [Type] -> UnifyResult
    tc_unify_tys_fg Bool
    False BindFamFun
    bind_fam BindTvFun
    bind_tv [Type]
    tys1 [Type]
    tys2

    tc_unify_tys_fg :: Bool
                    -> BindFamFun -> BindTvFun
                    -> [Type] -> [Type]
                    -> UnifyResult
    tc_unify_tys_fg :: Bool -> BindFamFun -> BindTvFun -> [Type] -> [Type] -> UnifyResult
    tc_unify_tys_fg Bool
    match_kis BindFamFun
    bind_fam BindTvFun
    bind_tv [Type]
    tys1 [Type]
    tys2
      = do { (tv_env, _) <- BindFamFun
    -> BindTvFun
    -> Bool
    -> Bool
    -> Bool
    -> MultiplicityFlag
    -> RnEnv2
    -> TvSubstEnv
    -> CvSubstEnv
    -> [Type]
    -> [Type]
    -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    tc_unify_tys BindFamFun
    bind_fam BindTvFun
    bind_tv
                                      Bool
    True       -- Unifying
                                      Bool
    False      -- Not doing an injectivity check
                                      Bool
    match_kis  -- Match outer kinds
                                      MultiplicityFlag
    RespectMultiplicities RnEnv2
    rn_env
                                      TvSubstEnv
    emptyTvSubstEnv CvSubstEnv
    emptyCvSubstEnv
                                      [Type]
    tys1 [Type]
    tys2
           ; return $ niFixSubst in_scope tv_env }
      where
        in_scope :: InScopeSet
    in_scope = TyCoVarSet -> InScopeSet
    mkInScopeSet (TyCoVarSet -> InScopeSet) -> TyCoVarSet -> InScopeSet
    forall a b. (a -> b) -> a -> b
    $ [Type] -> TyCoVarSet
    tyCoVarsOfTypes [Type]
    tys1 TyCoVarSet -> TyCoVarSet -> TyCoVarSet
    `unionVarSet` [Type] -> TyCoVarSet
    tyCoVarsOfTypes [Type]
    tys2
        rn_env :: RnEnv2
    rn_env   = InScopeSet -> RnEnv2
    mkRnEnv2 InScopeSet
    in_scope

    -- | This function is actually the one to call the unifier -- a little
    -- too general for outside clients, though.
    tc_unify_tys :: BindFamFun -> BindTvFun
                 -> AmIUnifying -- ^ True <=> unify; False <=> match
                 -> Bool        -- ^ True <=> doing an injectivity check
                 -> Bool        -- ^ True <=> treat the kinds as well
                 -> MultiplicityFlag -- ^ see Note [Rewrite rules ignore multiplicities in FunTy] in GHC.Core.Unify
                 -> RnEnv2
                 -> TvSubstEnv  -- ^ substitution to extend
                 -> CvSubstEnv
                 -> [Type] -> [Type]
                 -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    -- NB: It's tempting to ASSERT here that, if we're not matching kinds, then
    -- the kinds of the types should be the same. However, this doesn't work,
    -- as the types may be a dependent telescope, where later types have kinds
    -- that mention variables occurring earlier in the list of types. Here's an
    -- example (from typecheck/should_fail/T12709):
    --   template: [rep :: RuntimeRep,       a :: TYPE rep]
    --   target:   [LiftedRep :: RuntimeRep, Int :: TYPE LiftedRep]
    -- We can see that matching the first pair will make the kinds of the second
    -- pair equal. Yet, we still don't need a separate pass to unify the kinds
    -- of these types, so it's appropriate to use the Ty variant of unification.
    -- See also Note [tcMatchTy vs tcMatchTyKi].
    tc_unify_tys :: BindFamFun
    -> BindTvFun
    -> Bool
    -> Bool
    -> Bool
    -> MultiplicityFlag
    -> RnEnv2
    -> TvSubstEnv
    -> CvSubstEnv
    -> [Type]
    -> [Type]
    -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    tc_unify_tys BindFamFun
    bind_fam BindTvFun
    bind_tv Bool
    unif Bool
    inj_check Bool
    match_kis MultiplicityFlag
    match_mults RnEnv2
    rn_env TvSubstEnv
    tv_env CvSubstEnv
    cv_env [Type]
    tys1 [Type]
    tys2
      = TvSubstEnv
    -> CvSubstEnv -> UM () -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    initUM TvSubstEnv
    tv_env CvSubstEnv
    cv_env (UM () -> UnifyResultM (TvSubstEnv, CvSubstEnv))
    -> UM () -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    forall a b. (a -> b) -> a -> b
    $
        do { Bool -> UM () -> UM ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when Bool
    match_kis (UM () -> UM ()) -> UM () -> UM ()
    forall a b. (a -> b) -> a -> b
    $
             UMEnv -> [Type] -> [Type] -> UM ()
    unify_tys UMEnv
    env [Type]
    kis1 [Type]
    kis2
           ; UMEnv -> [Type] -> [Type] -> UM ()
    unify_tys UMEnv
    env [Type]
    tys1 [Type]
    tys2 }
      where
        env :: UMEnv
    env = UMEnv { um_bind_tv_fun :: BindTvFun
    um_bind_tv_fun  = BindTvFun
    bind_tv
                    , um_bind_fam_fun :: BindFamFun
    um_bind_fam_fun = BindFamFun
    bind_fam
                    , um_foralls :: TyCoVarSet
    um_foralls      = TyCoVarSet
    emptyVarSet
                    , um_unif :: Bool
    um_unif         = Bool
    unif
                    , um_inj_tf :: Bool
    um_inj_tf       = Bool
    inj_check
                    , um_arr_mult :: MultiplicityFlag
    um_arr_mult     = MultiplicityFlag
    match_mults
                    , um_rn_env :: RnEnv2
    um_rn_env       = RnEnv2
    rn_env }

        kis1 :: [Type]
    kis1 = (Type -> Type) -> [Type] -> [Type]
    forall a b. (a -> b) -> [a] -> [b]
    map HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind [Type]
    tys1
        kis2 :: [Type]
    kis2 = (Type -> Type) -> [Type] -> [Type]
    forall a b. (a -> b) -> [a] -> [b]
    map HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind [Type]
    tys2

    {- *********************************************************************
    *                                                                      *
                    UnifyResult, MaybeApart etc
    *                                                                      *
    ********************************************************************* -}

    {- Note [Rewrite rules ignore multiplicities in FunTy]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider the following (higher-order) rule:

    m :: Bool -> Bool -> Bool
    {-# RULES "m" forall f. m (f True) = f #-}

    let x = m ((,) @Bool @Bool True True)

    The rewrite rule expects an `f :: Bool -> Bool`, but `(,) @Bool @Bool True ::
    Bool %1 -> Bool` is linear (see Note [Data constructors are linear by default]
    in GHC.Core.Multiplicity) Should the rule match? Yes! According to the
    principles laid out in Note [Linting linearity] in GHC.Core.Lint, optimisation
    shouldn't be constrained by linearity.

    However, when matching the template variable `f` to `(,) True`, we do check that
    their types unify (see Note [Matching variable types] in GHC.Core.Rules). So
    when unifying types for the sake of rule-matching, the unification algorithm
    must be able to ignore multiplicities altogether.

    How is this done?
      (1) The `um_arr_mult` field of `UMEnv` recordsw when we are doing rule-matching,
          and hence want to ignore multiplicities.
      (2) The field is set to True in by `ruleMatchTyKiX`.
      (3) It is consulted when matching `FunTy` in `unify_ty`.

    Wrinkle in (3). In `unify_tc_app`, in `unify_ty`, `FunTy` is handled as if it
    was a regular type constructor. In this case, and when the types being unified
    are *function* arrows, but not constraint arrows, then the first argument is a
    multiplicity.

    We select this situation by comparing the type constructor with fUNTyCon. In
    this case, and this case only, we can safely drop the first argument (using the
    tail function) and unify the rest.

    Note [The substitution in MaybeApart]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The constructor MaybeApart carries data with it, typically a TvSubstEnv. Why?
    Because consider unifying these:

    (a, a, Int) ~ (b, [b], Bool)

    If we go left-to-right, we start with [a |-> b]. Then, on the middle terms, we
    apply the subst we have so far and discover that we need [b |-> [b]]. Because
    this fails the occurs check, we say that the types are MaybeApart (see above
    Note [Infinitary substitutions]). But, we can't stop there! Because if we
    continue, we discover that Int is SurelyApart from Bool, and therefore the
    types are apart. This has practical consequences for the ability for closed
    type family applications to reduce. See test case
    indexed-types/should_compile/Overlap14.
    -}

    -- This type does double-duty. It is used in the UM (unifier monad) and to
    -- return the final result. See Note [Unification result]
    type UnifyResult = UnifyResultM Subst

    -- | See Note [Unification result]
    data UnifyResultM a = Unifiable a        -- the subst that unifies the types
                        | MaybeApart MaybeApartReason
                                     a       -- the subst has as much as we know
                                             -- it must be part of a most general unifier
                                             -- See Note [The substitution in MaybeApart]
                        | SurelyApart
                        deriving (forall a b. (a -> b) -> UnifyResultM a -> UnifyResultM b)
    -> (forall a b. a -> UnifyResultM b -> UnifyResultM a)
    -> Functor UnifyResultM
    forall a b. a -> UnifyResultM b -> UnifyResultM a
    forall a b. (a -> b) -> UnifyResultM a -> UnifyResultM b
    forall (f :: * -> *).
    (forall a b. (a -> b) -> f a -> f b)
    -> (forall a b. a -> f b -> f a) -> Functor f
    $cfmap :: forall a b. (a -> b) -> UnifyResultM a -> UnifyResultM b
    fmap :: forall a b. (a -> b) -> UnifyResultM a -> UnifyResultM b
    $c<$ :: forall a b. a -> UnifyResultM b -> UnifyResultM a
    <$ :: forall a b. a -> UnifyResultM b -> UnifyResultM a
    Functor

    -- | Why are two types 'MaybeApart'? 'MARInfinite' takes precedence:
    -- This is used (only) in Note [Infinitary substitution in lookup] in GHC.Core.InstEnv
    -- As of Feb 2022, we never differentiate between MARTypeFamily and MARTypeVsConstraint;
    -- it's really only MARInfinite that's interesting here.
    data MaybeApartReason
      = MARTypeFamily   -- ^ matching e.g. F Int ~? Bool

      | MARInfinite     -- ^ matching e.g. a ~? Maybe a

      | MARTypeVsConstraint  -- ^ matching Type ~? Constraint or the arrow types
        -- See Note [Type and Constraint are not apart] in GHC.Builtin.Types.Prim

      | MARCast         -- ^ Very obscure.
        -- See (KCU2) in Note [Kind coercions in Unify]

    combineMAR :: MaybeApartReason -> MaybeApartReason -> MaybeApartReason
    -- See (UR1) in Note [Unification result] for why MARInfinite wins
    combineMAR :: MaybeApartReason -> MaybeApartReason -> MaybeApartReason
    combineMAR MaybeApartReason
    MARInfinite         MaybeApartReason
    _ = MaybeApartReason
    MARInfinite   -- MARInfinite wins
    combineMAR MaybeApartReason
    MARTypeFamily       MaybeApartReason
    r = MaybeApartReason
    r             -- Otherwise it doesn't really matter
    combineMAR MaybeApartReason
    MARTypeVsConstraint MaybeApartReason
    r = MaybeApartReason
    r
    combineMAR MaybeApartReason
    MARCast             MaybeApartReason
    r = MaybeApartReason
    r

    instance Outputable MaybeApartReason where
      ppr :: MaybeApartReason -> SDoc
    ppr MaybeApartReason
    MARTypeFamily       = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "MARTypeFamily"
      ppr MaybeApartReason
    MARInfinite         = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "MARInfinite"
      ppr MaybeApartReason
    MARTypeVsConstraint = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "MARTypeVsConstraint"
      ppr MaybeApartReason
    MARCast             = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "MARCast"

    instance Semigroup MaybeApartReason where
      <> :: MaybeApartReason -> MaybeApartReason -> MaybeApartReason
    (<>) = MaybeApartReason -> MaybeApartReason -> MaybeApartReason
    combineMAR

    instance Applicative UnifyResultM where
      pure :: forall a. a -> UnifyResultM a
    pure  = a -> UnifyResultM a
    forall a. a -> UnifyResultM a
    Unifiable
      <*> :: forall a b.
    UnifyResultM (a -> b) -> UnifyResultM a -> UnifyResultM b
    (<*>) = UnifyResultM (a -> b) -> UnifyResultM a -> UnifyResultM b
    forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
    ap

    instance Monad UnifyResultM where
      UnifyResultM a
    SurelyApart  >>= :: forall a b.
    UnifyResultM a -> (a -> UnifyResultM b) -> UnifyResultM b
    >>= a -> UnifyResultM b
    _ = UnifyResultM b
    forall a. UnifyResultM a
    SurelyApart
      MaybeApart MaybeApartReason
    r1 a
    x >>= a -> UnifyResultM b
    f = case a -> UnifyResultM b
    f a
    x of
                                Unifiable b
    y     -> MaybeApartReason -> b -> UnifyResultM b
    forall a. MaybeApartReason -> a -> UnifyResultM a
    MaybeApart MaybeApartReason
    r1 b
    y
                                MaybeApart MaybeApartReason
    r2 b
    y -> MaybeApartReason -> b -> UnifyResultM b
    forall a. MaybeApartReason -> a -> UnifyResultM a
    MaybeApart (MaybeApartReason
    r1 MaybeApartReason -> MaybeApartReason -> MaybeApartReason
    forall a. Semigroup a => a -> a -> a
    S.<> MaybeApartReason
    r2) b
    y
                                UnifyResultM b
    SurelyApart     -> UnifyResultM b
    forall a. UnifyResultM a
    SurelyApart
      Unifiable a
    x  >>= a -> UnifyResultM b
    f = a -> UnifyResultM b
    f a
    x

    instance Outputable a => Outputable (UnifyResultM a) where
      ppr :: UnifyResultM a -> SDoc
    ppr UnifyResultM a
    SurelyApart      = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "SurelyApart"
      ppr (Unifiable a
    x)    = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "Unifiable" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> a -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr a
    x
      ppr (MaybeApart MaybeApartReason
    r a
    x) = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "MaybeApart" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> MaybeApartReason -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr MaybeApartReason
    r SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> a -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr a
    x

    {-
    ************************************************************************
    *                                                                      *
                    Non-idempotent substitution
    *                                                                      *
    ************************************************************************

    Note [Non-idempotent substitution]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    During unification we use a TvSubstEnv/CvSubstEnv pair that is
      (a) non-idempotent
      (b) loop-free; ie repeatedly applying it yields a fixed point

    Note [Finding the substitution fixpoint]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Finding the fixpoint of a non-idempotent substitution arising from a
    unification is much trickier than it looks, because of kinds.  Consider
       T k (H k (f:k)) ~ T * (g:*)
    If we unify, we get the substitution
       [ k -> *
       , g -> H k (f:k) ]
    To make it idempotent we don't want to get just
       [ k -> *
       , g -> H * (f:k) ]
    We also want to substitute inside f's kind, to get
       [ k -> *
       , g -> H k (f:*) ]
    If we don't do this, we may apply the substitution to something,
    and get an ill-formed type, i.e. one where typeKind will fail.
    This happened, for example, in #9106.

    It gets worse.  In #14164 we wanted to take the fixpoint of
    this substitution
       [ xs_asV :-> F a_aY6 (z_aY7 :: a_aY6)
                            (rest_aWF :: G a_aY6 (z_aY7 :: a_aY6))
       , a_aY6  :-> a_aXQ ]

    We have to apply the substitution for a_aY6 two levels deep inside
    the invocation of F!  We don't have a function that recursively
    applies substitutions inside the kinds of variable occurrences (and
    probably rightly so).

    So, we work as follows:

     1. Start with the current substitution (which we are
        trying to fixpoint
           [ xs :-> F a (z :: a) (rest :: G a (z :: a))
           , a  :-> b ]

     2. Take all the free vars of the range of the substitution:
           {a, z, rest, b}
        NB: the free variable finder closes over
        the kinds of variable occurrences

     3. If none are in the domain of the substitution, stop.
        We have found a fixpoint.

     4. Remove the variables that are bound by the substitution, leaving
           {z, rest, b}

     5. Do a topo-sort to put them in dependency order:
           [ b :: *, z :: a, rest :: G a z ]

     6. Apply the substitution left-to-right to the kinds of these
        tyvars, extending it each time with a new binding, so we
        finish up with
           [ xs   :-> ..as before..
           , a    :-> b
           , b    :-> b    :: *
           , z    :-> z    :: b
           , rest :-> rest :: G b (z :: b) ]
        Note that rest now has the right kind

     7. Apply this extended substitution (once) to the range of
        the /original/ substitution.  (Note that we do the
        extended substitution would go on forever if you tried
        to find its fixpoint, because it maps z to z.)

     8. And go back to step 1

    In Step 6 we use the free vars from Step 2 as the initial
    in-scope set, because all of those variables appear in the
    range of the substitution, so they must all be in the in-scope
    set.  But NB that the type substitution engine does not look up
    variables in the in-scope set; it is used only to ensure no
    shadowing.
    -}

    niFixSubst :: InScopeSet -> TvSubstEnv -> Subst
    -- Find the idempotent fixed point of the non-idempotent substitution
    -- This is surprisingly tricky:
    --   see Note [Finding the substitution fixpoint]
    -- ToDo: use laziness instead of iteration?
    niFixSubst :: InScopeSet -> TvSubstEnv -> Subst
    niFixSubst InScopeSet
    in_scope TvSubstEnv
    tenv
      | Bool
    not_fixpoint = InScopeSet -> TvSubstEnv -> Subst
    niFixSubst InScopeSet
    in_scope ((Type -> Type) -> TvSubstEnv -> TvSubstEnv
    forall a b. (a -> b) -> VarEnv a -> VarEnv b
    mapVarEnv (HasDebugCallStack => Subst -> Type -> Type
    Subst -> Type -> Type
    substTy Subst
    subst) TvSubstEnv
    tenv)
      | Bool
    otherwise    = Subst
    subst
      where
        range_fvs :: FV
        range_fvs :: FV
    range_fvs = [Type] -> FV
    tyCoFVsOfTypes (TvSubstEnv -> [Type]
    forall {k} (key :: k) elt. UniqFM key elt -> [elt]
    nonDetEltsUFM TvSubstEnv
    tenv)
              -- It's OK to use nonDetEltsUFM here because the
              -- order of range_fvs, range_tvs is immaterial

        range_tvs :: [TyVar]
        range_tvs :: [Var]
    range_tvs = FV -> [Var]
    fvVarList FV
    range_fvs

        not_fixpoint :: Bool
    not_fixpoint  = (Var -> Bool) -> [Var] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any Var -> Bool
    in_domain [Var]
    range_tvs
        in_domain :: Var -> Bool
    in_domain Var
    tv  = Var
    tv Var -> TvSubstEnv -> Bool
    forall a. Var -> VarEnv a -> Bool
    `elemVarEnv` TvSubstEnv
    tenv

        free_tvs :: [Var]
    free_tvs = [Var] -> [Var]
    scopedSort ((Var -> Bool) -> [Var] -> [Var]
    forall a. (a -> Bool) -> [a] -> [a]
    filterOut Var -> Bool
    in_domain [Var]
    range_tvs)

        -- See Note [Finding the substitution fixpoint], Step 6
        subst :: Subst
    subst = (Subst -> Var -> Subst) -> Subst -> [Var] -> Subst
    forall b a. (b -> a -> b) -> b -> [a] -> b
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    foldl' Subst -> Var -> Subst
    add_free_tv
                      (InScopeSet -> TvSubstEnv -> Subst
    mkTvSubst InScopeSet
    in_scope TvSubstEnv
    tenv)
                      [Var]
    free_tvs

        add_free_tv :: Subst -> TyVar -> Subst
        add_free_tv :: Subst -> Var -> Subst
    add_free_tv Subst
    subst Var
    tv
          = Subst -> Var -> Type -> Subst
    extendTvSubst Subst
    subst Var
    tv (Var -> Type
    mkTyVarTy Var
    tv')
         where
            tv' :: Var
    tv' = (Type -> Type) -> Var -> Var
    updateTyVarKind (HasDebugCallStack => Subst -> Type -> Type
    Subst -> Type -> Type
    substTy Subst
    subst) Var
    tv

    {-
    ************************************************************************
    *                                                                      *
                    unify_ty: the main workhorse
    *                                                                      *
    ************************************************************************

    Note [Specification of unification]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The pure unifier, unify_ty, defined in this module, tries to work out
    a substitution to make two types say True to eqType. NB: eqType is
    itself not purely syntactic; it accounts for CastTys;
    see Note [Non-trivial definitional equality] in GHC.Core.TyCo.Rep

    Unlike the "impure unifiers" in the typechecker (the eager unifier in
    GHC.Tc.Utils.Unify, and the constraint solver itself in GHC.Tc.Solver.Equality),
    the pure unifier does /not/ work up to ~.

    The algorithm implemented here is rather delicate, and we depend on it
    to uphold certain properties. This is a summary of these required
    properties.

    Notation:
     θ,φ  substitutions
     ξ    type-function-free types
     τ,σ  other types
     τ♭   type τ, flattened

     ≡    eqType

    (U1) Soundness.
         If (unify τ₁ τ₂) = Unifiable θ, then θ(τ₁) ≡ θ(τ₂).
         θ is a most general unifier for τ₁ and τ₂.

    (U2) Completeness.
         If (unify ξ₁ ξ₂) = SurelyApart,
         then there exists no substitution θ such that θ(ξ₁) ≡ θ(ξ₂).

    These two properties are stated as Property 11 in the "Closed Type Families"
    paper (POPL'14). Below, this paper is called [CTF].

    (U3) Apartness under substitution.
         If (unify ξ τ♭) = SurelyApart, then (unify ξ θ(τ)♭) = SurelyApart,
         for any θ. (Property 12 from [CTF])

    (U4) Apart types do not unify.
         If (unify ξ τ♭) = SurelyApart, then there exists no θ
         such that θ(ξ) = θ(τ). (Property 13 from [CTF])

    THEOREM. Completeness w.r.t ~
        If (unify τ₁♭ τ₂♭) = SurelyApart,
        then there exists no proof that (τ₁ ~ τ₂).

    PROOF. See appendix of [CTF].

    The unification algorithm is used for type family injectivity, as described
    in the "Injective Type Families" paper (Haskell'15), called [ITF]. When run
    in this mode, it has the following properties.

    (I1) If (unify σ τ) = SurelyApart, then σ and τ are not unifiable, even
         after arbitrary type family reductions.

    (I2) If (unify σ τ) = MaybeApart θ, and if some
         φ exists such that φ(σ) ~ φ(τ), then φ extends θ.

    Furthermore, the RULES matching algorithm requires this property,
    but only when using this algorithm for matching:

    (M1) If (match σ τ) succeeds with θ, then all matchable tyvars
         in σ are bound in θ.

         Property M1 means that we must extend the substitution with,
         say (a ↦ a) when appropriate during matching.
         See also Note [Self-substitution when unifying or matching].

    (M2) Completeness of matching.
         If θ(σ) = τ, then (match σ τ) = Unifiable φ,
         where θ is an extension of φ.

    Wrinkle (SI1): um_inj_tf:
        Sadly, property M2 and I2 conflict. Consider

        type family F1 a b where
          F1 Int    Bool   = Char
          F1 Double String = Char

        Consider now two matching problems:

        P1. match (F1 a Bool) (F1 Int Bool)
        P2. match (F1 a Bool) (F1 Double String)

        In case P1, we must find (a ↦ Int) to satisfy M2.  In case P2, we must /not/
        find (a ↦ Double), in order to satisfy I2. (Note that the correct mapping for
        I2 is (a ↦ Int). There is no way to discover this, but we mustn't map a to
        anything else!)

        We thus must parameterize the algorithm over whether it's being used
        for an injectivity check (refrain from looking at non-injective arguments
        to type families) or not (do indeed look at those arguments).  This is
        implemented  by the um_inj_tf field of UMEnv.

        (It's all a question of whether or not to include equation (7) from Fig. 2
        of [ITF].)

        This extra parameter is a bit fiddly, perhaps, but seemingly less so than
        having two separate, almost-identical algorithms.

    Note [Self-substitution when unifying or matching]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    What happens when we are unifying or matching two identical type variables?
         a ~ a

    * When /unifying/, just succeed, without binding [a :-> a] in the substitution,
      else we'd get an infinite substitution.  We need to make this check before
      we do the occurs check, of course.

    * When /matching/, and `a` is a bindable variable from the template, we /do/
      want to extend the substitution.  Remember, a successful match should map all
      the template variables (except ones that disappear when expanding synonyms),

      But when `a` is /not/ a bindable variable (perhaps it is a globally-in-scope
      skolem) we want to treat it like a constant `Int ~ Int` and succeed.

      Notice: no occurs check!  It's fine to match (a ~ Maybe a), because the
      template vars of the template come from a different name space to the free
      vars of the target.

      Note that this arrangement was provoked by a real failure, where the same
      unique ended up in the template as in the target. (It was a rule firing when
      compiling Data.List.NonEmpty.)

    * What about matching a /non-bindable/ variable?  For example:
          template-vars   : {a}
          matching problem: (forall b. b -> a) ~ (forall c. c -> Int)
      We want to emerge with the substitution [a :-> Int]
      But on the way we will encounter (b ~ b), when we match the bits before the
      arrow under the forall, having renamed `c` to `b`.  This match should just
      succeed, just like (Int ~ Int), without extending the substitution.

      It's important to do this for /non-bindable/ variables, not just for
      forall-bound ones.  In an associated type
             instance C (Maybe a) where {  type F (Maybe a) = Int }
      `checkConsistentFamInst` matches (Maybe a) from the header against (Maybe a)
      from the type-family instance, with `a` marked as non-bindable.

    Note [Matching coercion variables]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider this:

       type family F a

       data G a where
         MkG :: F a ~ Bool => G a

       type family Foo (x :: G a) :: F a
       type instance Foo MkG = False

    We would like that to be accepted. For that to work, we need to introduce
    a coercion variable on the left and then use it on the right. Accordingly,
    at use sites of Foo, we need to be able to use matching to figure out the
    value for the coercion. (See the desugared version:

       axFoo :: [a :: *, c :: F a ~ Bool]. Foo (MkG c) = False |> (sym c)

    ) We never want this action to happen during *unification* though, when
    all bets are off.

    Note [Kind coercions in Unify]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    We wish to match/unify while ignoring casts. But, we can't just ignore
    them completely, or we'll end up with ill-kinded substitutions. For example,
    say we're matching `a` with `ty |> co`. If we just drop the cast, we'll
    return [a |-> ty], but `a` and `ty` might have different kinds. We can't
    just match/unify their kinds, either, because this might gratuitously
    fail. After all, `co` is the witness that the kinds are the same -- they
    may look nothing alike.

    So, we pass a kind coercion `kco` to the main `unify_ty`. This coercion witnesses
    the equality between the substed kind of the left-hand type and the substed
    kind of the right-hand type. Note that we do not unify kinds at the leaves
    (as we did previously).

    Hence: (UKINV) Unification Kind Invariant
    * In the call
         unify_ty ty1 ty2 kco
      it must be that
         subst(kco) :: subst(kind(ty1)) ~N subst(kind(ty2))
      where `subst` is the ambient substitution in the UM monad
    * In the call
         unify_tys tys1 tys2
      (which has no kco), after we unify any prefix of tys1,tys2, the kinds of the
      head of the remaining tys1,tys2 are identical after substitution.  This
      implies, for example, that the kinds of the head of tys1,tys2 are identical
      after substitution.

    Preserving (UKINV) takes a bit of work, governed by the `match_kis` flag in
    `tc_unify_tys`:

    * When we're working with type applications (either TyConApp or AppTy) we
      need to worry about establishing (UKINV), as the kinds of the function
      & arguments aren't (necessarily) included in the kind of the result.
      When unifying two TyConApps, this is easy, because the two TyCons are
      the same. Their kinds are thus the same. As long as we unify left-to-right,
      we'll be sure to unify types' kinds before the types themselves. (For example,
      think about Proxy :: forall k. k -> *. Unifying the first args matches up
      the kinds of the second args.)

    * For AppTy, we must unify the kinds of the functions, but once these are
      unified, we can continue unifying arguments without worrying further about
      kinds.

    * The interface to this module includes both "...Ty" functions and
      "...TyKi" functions. The former assume that (UKINV) is already
      established, either because the kinds are the same or because the
      list of types being passed in are the well-typed arguments to some
      type constructor (see two paragraphs above). The latter take a separate
      pre-pass over the kinds to establish (UKINV). Sometimes, it's important
      not to take the second pass, as it caused #12442.

    Wrinkles

    (KCU1) We ensure that the `kco` argument never mentions variables in the
      domain of either RnEnvL or RnEnvR.  Why?

      * `kco` is used only to build the final well-kinded substitution
             a :-> ty |> kco
        The range of the substitution never mentions forall-bound variables,
        so `kco` cannot either.

      * `kco` mixes up types from both left and right arguments of
        `unify_ty`, which have different renamings in the RnEnv2.

      The easiest thing is to insist that `kco` does not need renaming with
      the RnEnv2; it mentions no forall-bound variables.

      To achieve this we do a `mentionsForAllBoundTyVars` test in the
      `CastTy` cases of `unify_ty`.

    (KCU2) Suppose we are unifying
                (forall a. x |> (...F a b...) ~ (forall a. y)
      We can't bind y :-> x |> (...F a b...), becuase of that free `a`.

      But if we later learn that b=Int, and F a Int = Bool,
      that free `a` might disappear, so we could unify with
          y :-> x |> (...Bool...)

      Conclusion: if there is a free forall-bound variable in a cast,
      return MaybeApart, with a MaybeApartReason of MARCast.

    (KCU3) We thought, at one point, that this was all unnecessary: why should
        casts be in types in the first place? But they are sometimes. In
        dependent/should_compile/KindEqualities2, we see, for example the
        constraint Num (Int |> (blah ; sym blah)).  We naturally want to find
        a dictionary for that constraint, which requires dealing with
        coercions in this manner.

    Note [Matching in the presence of casts (1)]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    When matching, it is crucial that no variables from the template
    end up in the range of the matching substitution (obviously!).
    When unifying, that's not a constraint; instead we take the fixpoint
    of the substitution at the end.

    So what should we do with this, when matching?
       unify_ty (tmpl |> co) tgt kco

    Previously, wrongly, we pushed 'co' in the (horrid) accumulating
    'kco' argument like this:
       unify_ty (tmpl |> co) tgt kco
         = unify_ty tmpl tgt (kco ; co)

    But that is obviously wrong because 'co' (from the template) ends
    up in 'kco', which in turn ends up in the range of the substitution.

    This all came up in #13910.  Because we match tycon arguments
    left-to-right, the ambient substitution will already have a matching
    substitution for any kinds; so there is an easy fix: just apply
    the substitution-so-far to the coercion from the LHS.

    Note that

    * When matching, the first arg of unify_ty is always the template;
      we never swap round.

    * The above argument is distressingly indirect. We seek a
      better way.

    * One better way is to ensure that type patterns (the template
      in the matching process) have no casts.  See #14119.

    Note [Matching in the presence of casts (2)]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    There is another wrinkle (#17395).  Suppose (T :: forall k. k -> Type)
    and we are matching
       tcMatchTy (T k (a::k))  (T j (b::j))

    Then we'll match k :-> j, as expected. But then in unify_tys
    we invoke
       unify_tys env (a::k) (b::j) (Refl j)

    Although we have unified k and j, it's very important that we put
    (Refl j), /not/ (Refl k) as the fourth argument to unify_tys.
    If we put (Refl k) we'd end up with the substitution
      a :-> b |> Refl k
    which is bogus because one of the template variables, k,
    appears in the range of the substitution.  Eek.

    Similar care is needed in unify_ty_app.

    Note [Polykinded tycon applications]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose  T :: forall k. Type -> K
    and we are unifying
      ty1:  T @Type         Int       :: Type
      ty2:  T @(Type->Type) Int Int   :: Type

    These two TyConApps have the same TyCon at the front but they
    (legitimately) have different numbers of arguments.  They
    are surelyApart, so we can report that without looking any
    further (see #15704).

    Note [Unifying type applications]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Unifying type applications is quite subtle, as we found
    in #23134 and #22647, when type families are involved.

    Suppose
       type family F a :: Type -> Type
       type family G k :: k = r | r -> k

    and consider these examples:

    * F Int ~ F Char, where F is injective
      Since F is injective, we can reduce this to Int ~ Char,
      therefore SurelyApart.

    * F Int ~ F Char, where F is not injective
      Without injectivity, return MaybeApart.

    * G Type ~ G (Type -> Type) Int
      Even though G is injective and the arguments to G are different,
      we cannot deduce apartness because the RHS is oversaturated.
      For example, G might be defined as
        G Type = Maybe Int
        G (Type -> Type) = Maybe
      So we return MaybeApart.

    * F Int Bool ~ F Int Char       -- SurelyApart (since Bool is apart from Char)
      F Int Bool ~ Maybe a          -- MaybeApart
      F Int Bool ~ a b              -- MaybeApart
      F Int Bool ~ Char -> Bool     -- MaybeApart
      An oversaturated type family can match an application,
      whether it's a TyConApp, AppTy or FunTy. Decompose.

    * F Int ~ a b
      We cannot decompose a saturated, or under-saturated
      type family application. We return MaybeApart.

    To handle all those conditions, unify_ty goes through
    the following checks in sequence, where Fn is a type family
    of arity n:

    * (C1) Fn x_1 ... x_n ~ Fn y_1 .. y_n
      A saturated application.
      Here we can unify arguments in which Fn is injective.
    * (C2) Fn x_1 ... x_n ~ anything, anything ~ Fn x_1 ... x_n
      A saturated type family can match anything - we return MaybeApart.
    * (C3) Fn x_1 ... x_m ~ a b, a b ~ Fn x_1 ... x_m where m > n
      An oversaturated type family can be decomposed.
    * (C4) Fn x_1 ... x_m ~ anything, anything ~ Fn x_1 ... x_m, where m > n
      If we couldn't decompose in the previous step, we return SurelyApart.

    Afterwards, the rest of the code doesn't have to worry about type families.

    Note [Unifying type synonyms]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider the task of unifying two 'Type's of the form

      TyConApp tc [] ~ TyConApp tc []

    where `tc` is a type synonym. A naive way to perform this comparison these
    would first expand the synonym and then compare the resulting expansions.

    However, this is obviously wasteful and the RHS of `tc` may be large; it is
    much better to rather compare the TyCons directly. Consequently, before
    expanding type synonyms in type comparisons we first look for a nullary
    TyConApp and simply compare the TyCons if we find one.

    Of course, if we find that the TyCons are *not* equal then we still need to
    perform the expansion as their RHSs may still be unifiable.  E.g
        type T = S (a->a)
        type S a = [a]
    and consider
        T Int ~ S (Int -> Int)

    We can't decompose non-nullary synonyms.  E.g.
        type R a = F a    -- Where F is a type family
    and consider
        R (a->a) ~ R Int
    We can't conclude that  (a->) ~ Int.  (There is a currently-missed opportunity
    here; if we knew that R was /injective/, perhaps we could decompose.)

    We perform the nullary-type-synonym optimisation in a number of places:

     * GHC.Core.Unify.unify_ty
     * GHC.Tc.Solver.Equality.can_eq_nc'
     * GHC.Tc.Utils.Unify.uType

    This optimisation is especially helpful for the ubiquitous GHC.Types.Type,
    since GHC prefers to use the type synonym over @TYPE 'LiftedRep@ applications
    whenever possible. See Note [Using synonyms to compress types] in
    GHC.Core.Type for details.

    c.f. Note [Comparing type synonyms] in GHC.Core.TyCo.Compare
    -}

    -------------- unify_ty: the main workhorse -----------

    type AmIUnifying = Bool   -- True  <=> Unifying
                              -- False <=> Matching

    type InType      = Type       -- Before applying the RnEnv2
    type OutCoercion = Coercion   -- After applying the RnEnv2

    unify_ty :: UMEnv
             -> InType -> InType  -- Types to be unified
             -> OutCoercion       -- A nominal coercion between their kinds
                                  -- OutCoercion: the RnEnv has already been applied
                                  -- When matching, the coercion is in "target space",
                                  --   not "template space"
                                  -- See Note [Kind coercions in Unify]
             -> UM ()
    -- Precondition: see (Unification Kind Invariant)
    --
    -- See Note [Specification of unification]
    -- Respects newtypes, PredTypes
    -- See Note [Computing equality on types] in GHC.Core.Type
    unify_ty :: UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    _env (TyConApp TyCon
    tc1 []) (TyConApp TyCon
    tc2 []) OutCoercion
    _kco
      -- See Note [Unifying type synonyms]
      | TyCon
    tc1 TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyCon
    tc2
      = () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()

    unify_ty UMEnv
    env Type
    ty1 Type
    ty2 OutCoercion
    kco
        -- Now handle the cases we can "look through": synonyms and casts.
      | Just Type
    ty1' <- Type -> Maybe Type
    coreView Type
    ty1 = UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    env Type
    ty1' Type
    ty2 OutCoercion
    kco
      | Just Type
    ty2' <- Type -> Maybe Type
    coreView Type
    ty2 = UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    env Type
    ty1 Type
    ty2' OutCoercion
    kco

    unify_ty UMEnv
    env (CastTy Type
    ty1 OutCoercion
    co1) Type
    ty2 OutCoercion
    kco
      | UMEnv -> TyCoVarSet -> Bool
    mentionsForAllBoundTyVarsL UMEnv
    env (OutCoercion -> TyCoVarSet
    tyCoVarsOfCo OutCoercion
    co1)
        -- See (KCU1) in Note [Kind coercions in Unify]
      = MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    MARCast  -- See (KCU2)

      | UMEnv -> Bool
    um_unif UMEnv
    env
      = UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    env Type
    ty1 Type
    ty2 (OutCoercion
    co1 HasDebugCallStack => OutCoercion -> OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion -> OutCoercion
    `mkTransCo` OutCoercion
    kco)

      | Bool
    otherwise -- We are matching, not unifying
      = do { subst <- UMEnv -> UM Subst
    getSubst UMEnv
    env
           ; let co' = HasDebugCallStack => Subst -> OutCoercion -> OutCoercion
    Subst -> OutCoercion -> OutCoercion
    substCo Subst
    subst OutCoercion
    co1
             -- We match left-to-right, so the free template vars of the
             -- coercion should already have been matched.
             -- See Note [Matching in the presence of casts (1)]
             -- NB: co1 does not mention forall-bound vars, so no need to rename
           ; unify_ty env ty1 ty2 (co' `mkTransCo` kco) }

    unify_ty UMEnv
    env Type
    ty1 (CastTy Type
    ty2 OutCoercion
    co2) OutCoercion
    kco
      | UMEnv -> TyCoVarSet -> Bool
    mentionsForAllBoundTyVarsR UMEnv
    env (OutCoercion -> TyCoVarSet
    tyCoVarsOfCo OutCoercion
    co2)
        -- See (KCU1) in Note [Kind coercions in Unify]
      = MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    MARCast  -- See (KCU2)
      | Bool
    otherwise
      = UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    env Type
    ty1 Type
    ty2 (OutCoercion
    kco HasDebugCallStack => OutCoercion -> OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion -> OutCoercion
    `mkTransCo` OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    co2)
        -- NB: co2 does not mention forall-bound variables

    -- Applications need a bit of care!
    -- They can match FunTy and TyConApp, so use splitAppTy_maybe
    unify_ty UMEnv
    env (AppTy Type
    ty1a Type
    ty1b) Type
    ty2 OutCoercion
    _kco
      | Just (Type
    ty2a, Type
    ty2b) <- Type -> Maybe (Type, Type)
    tcSplitAppTyNoView_maybe Type
    ty2
      = UMEnv -> Type -> [Type] -> Type -> [Type] -> UM ()
    unify_ty_app UMEnv
    env Type
    ty1a [Type
    ty1b] Type
    ty2a [Type
    ty2b]

    unify_ty UMEnv
    env Type
    ty1 (AppTy Type
    ty2a Type
    ty2b) OutCoercion
    _kco
      | Just (Type
    ty1a, Type
    ty1b) <- Type -> Maybe (Type, Type)
    tcSplitAppTyNoView_maybe Type
    ty1
      = UMEnv -> Type -> [Type] -> Type -> [Type] -> UM ()
    unify_ty_app UMEnv
    env Type
    ty1a [Type
    ty1b] Type
    ty2a [Type
    ty2b]

    unify_ty UMEnv
    _ (LitTy TyLit
    x) (LitTy TyLit
    y) OutCoercion
    _kco | TyLit
    x TyLit -> TyLit -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyLit
    y = () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()

    unify_ty UMEnv
    env (ForAllTy (Bndr Var
    tv1 ForAllTyFlag
    _) Type
    ty1) (ForAllTy (Bndr Var
    tv2 ForAllTyFlag
    _) Type
    ty2) OutCoercion
    kco
      -- ToDo: See Note [Unifying coercion-foralls]
      = do { UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    env (Var -> Type
    varType Var
    tv1) (Var -> Type
    varType Var
    tv2) (Type -> OutCoercion
    mkNomReflCo Type
    liftedTypeKind)
           ; let env' :: UMEnv
    env' = UMEnv -> Var -> Var -> UMEnv
    umRnBndr2 UMEnv
    env Var
    tv1 Var
    tv2
           ; UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    env' Type
    ty1 Type
    ty2 OutCoercion
    kco }

    -- See Note [Matching coercion variables]
    unify_ty UMEnv
    env (CoercionTy OutCoercion
    co1) (CoercionTy OutCoercion
    co2) OutCoercion
    kco
      = do { c_subst <- UM CvSubstEnv
    getCvSubstEnv
           ; case co1 of
               CoVarCo Var
    cv
                 | Bool -> Bool
    not (UMEnv -> Bool
    um_unif UMEnv
    env)
                 , Bool -> Bool
    not (Var
    cv Var -> CvSubstEnv -> Bool
    forall a. Var -> VarEnv a -> Bool
    `elemVarEnv` CvSubstEnv
    c_subst)   -- Not forall-bound
                 , let (OutCoercion
    _mult_co, OutCoercion
    co_l, OutCoercion
    co_r) = HasDebugCallStack =>
    OutCoercion -> (OutCoercion, OutCoercion, OutCoercion)
    OutCoercion -> (OutCoercion, OutCoercion, OutCoercion)
    decomposeFunCo OutCoercion
    kco
                         -- Because the coercion is used in a type, it should be safe to
                         -- ignore the multiplicity coercion, _mult_co
                          -- cv :: t1 ~ t2
                          -- co2 :: s1 ~ s2
                          -- co_l :: t1 ~ s1
                          -- co_r :: t2 ~ s2
                       rhs_co :: OutCoercion
    rhs_co = OutCoercion
    co_l HasDebugCallStack => OutCoercion -> OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion -> OutCoercion
    `mkTransCo` OutCoercion
    co2 HasDebugCallStack => OutCoercion -> OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion -> OutCoercion
    `mkTransCo` OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    co_r
                 , BindFlag
    BindMe <- UMEnv -> BindTvFun
    um_bind_tv_fun UMEnv
    env Var
    cv (OutCoercion -> Type
    CoercionTy OutCoercion
    rhs_co)
                 -> if UMEnv -> TyCoVarSet -> Bool
    mentionsForAllBoundTyVarsR UMEnv
    env (OutCoercion -> TyCoVarSet
    tyCoVarsOfCo OutCoercion
    co2)
                    then UM ()
    forall a. UM a
    surelyApart
                    else Var -> OutCoercion -> UM ()
    extendCvEnv Var
    cv OutCoercion
    rhs_co

               OutCoercion
    _ -> () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return () }

    unify_ty UMEnv
    env (TyVarTy Var
    tv1) Type
    ty2 OutCoercion
    kco
      = UMEnv -> CanEqLHS -> Type -> OutCoercion -> UM ()
    uVarOrFam UMEnv
    env (Var -> CanEqLHS
    TyVarLHS Var
    tv1) Type
    ty2 OutCoercion
    kco

    unify_ty UMEnv
    env Type
    ty1 (TyVarTy Var
    tv2) OutCoercion
    kco
      | UMEnv -> Bool
    um_unif UMEnv
    env  -- If unifying, can swap args; but not when matching
      = UMEnv -> CanEqLHS -> Type -> OutCoercion -> UM ()
    uVarOrFam (UMEnv -> UMEnv
    umSwapRn UMEnv
    env) (Var -> CanEqLHS
    TyVarLHS Var
    tv2) Type
    ty1 (OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    kco)

    -- Deal with TyConApps
    unify_ty UMEnv
    env Type
    ty1 Type
    ty2 OutCoercion
    kco
      -- Handle non-oversaturated type families first
      -- See Note [Unifying type applications]
      | Just (TyCon
    tc,[Type]
    tys) <- Maybe (TyCon, [Type])
    mb_sat_fam_app1
      = UMEnv -> CanEqLHS -> Type -> OutCoercion -> UM ()
    uVarOrFam UMEnv
    env (TyCon -> [Type] -> CanEqLHS
    TyFamLHS TyCon
    tc [Type]
    tys) Type
    ty2 OutCoercion
    kco

      | UMEnv -> Bool
    um_unif UMEnv
    env
      , Just (TyCon
    tc,[Type]
    tys) <- Maybe (TyCon, [Type])
    mb_sat_fam_app2
      = UMEnv -> CanEqLHS -> Type -> OutCoercion -> UM ()
    uVarOrFam (UMEnv -> UMEnv
    umSwapRn UMEnv
    env) (TyCon -> [Type] -> CanEqLHS
    TyFamLHS TyCon
    tc [Type]
    tys) Type
    ty1 (OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    kco)

      -- Handle oversaturated type families. Suppose we have
      --     (F a b) ~ (c d)    where F has arity 1
      -- We definitely want to decompose that type application! (#22647)
      --
      -- If there is no application, an oversaturated type family can only
      -- match a type variable or a saturated type family,
      -- both of which we handled earlier. So we can say surelyApart.
      | Just (TyCon
    tc1, [Type]
    _) <- Maybe (TyCon, [Type])
    mb_tc_app1
      , TyCon -> Bool
    isTypeFamilyTyCon TyCon
    tc1
      = if | 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
           -> UMEnv -> Type -> [Type] -> Type -> [Type] -> UM ()
    unify_ty_app UMEnv
    env Type
    ty1a [Type
    ty1b] Type
    ty2a [Type
    ty2b]            -- (C3)
           | Bool
    otherwise -> UM ()
    forall a. UM a
    surelyApart                             -- (C4)

      | Just (TyCon
    tc2, [Type]
    _) <- Maybe (TyCon, [Type])
    mb_tc_app2
      , TyCon -> Bool
    isTypeFamilyTyCon TyCon
    tc2
      = if | 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
           -> UMEnv -> Type -> [Type] -> Type -> [Type] -> UM ()
    unify_ty_app UMEnv
    env Type
    ty1a [Type
    ty1b] Type
    ty2a [Type
    ty2b]            -- (C3)
           | Bool
    otherwise -> UM ()
    forall a. UM a
    surelyApart                             -- (C4)

      -- At this point, neither tc1 nor tc2 can be a type family.
      | Just (TyCon
    tc1, [Type]
    tys1) <- Maybe (TyCon, [Type])
    mb_tc_app1
      , Just (TyCon
    tc2, [Type]
    tys2) <- Maybe (TyCon, [Type])
    mb_tc_app2
      , TyCon
    tc1 TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyCon
    tc2
      = do { Bool -> SDoc -> UM ()
    forall (m :: * -> *).
    (HasCallStack, Applicative m) =>
    Bool -> SDoc -> m ()
    massertPpr (TyCon -> Role -> Bool
    isInjectiveTyCon TyCon
    tc1 Role
    Nominal) (TyCon -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyCon
    tc1)
           ; UMEnv -> TyCon -> [Type] -> [Type] -> UM ()
    unify_tc_app UMEnv
    env TyCon
    tc1 [Type]
    tys1 [Type]
    tys2
           }

      -- TYPE and CONSTRAINT are not Apart
      -- See Note [Type and Constraint are not apart] in GHC.Builtin.Types.Prim
      -- NB: at this point we know that the two TyCons do not match
      | Just (TyCon
    tc1,[Type]
    _) <- Maybe (TyCon, [Type])
    mb_tc_app1, let u1 :: Unique
    u1 = TyCon -> Unique
    tyConUnique TyCon
    tc1
      , Just (TyCon
    tc2,[Type]
    _) <- Maybe (TyCon, [Type])
    mb_tc_app2, let u2 :: Unique
    u2 = TyCon -> Unique
    tyConUnique TyCon
    tc2
      , (Unique
    u1 Unique -> Unique -> Bool
    forall a. Eq a => a -> a -> Bool
    == Unique
    tYPETyConKey Bool -> Bool -> Bool
    && Unique
    u2 Unique -> Unique -> Bool
    forall a. Eq a => a -> a -> Bool
    == Unique
    cONSTRAINTTyConKey) Bool -> Bool -> Bool
    ||
        (Unique
    u2 Unique -> Unique -> Bool
    forall a. Eq a => a -> a -> Bool
    == Unique
    tYPETyConKey Bool -> Bool -> Bool
    && Unique
    u1 Unique -> Unique -> Bool
    forall a. Eq a => a -> a -> Bool
    == Unique
    cONSTRAINTTyConKey)
      = MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    MARTypeVsConstraint
        -- We don't bother to look inside; wrinkle (W3) in GHC.Builtin.Types.Prim
        -- Note [Type and Constraint are not apart]

      -- The arrow types are not Apart
      -- See Note [Type and Constraint are not apart] in GHC.Builtin.Types.Prim
      --     wrinkle (W2)
      -- NB1: at this point we know that the two TyCons do not match
      -- NB2: In the common FunTy/FunTy case you might wonder if we want to go via
      --      splitTyConApp_maybe.  But yes we do: we need to look at those implied
      --      kind argument in order to satisfy (Unification Kind Invariant)
      | FunTy {} <- Type
    ty1
      , FunTy {} <- Type
    ty2
      = MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    MARTypeVsConstraint
        -- We don't bother to look inside; wrinkle (W3) in GHC.Builtin.Types.Prim
        -- Note [Type and Constraint are not apart]

      where
        mb_tc_app1 :: Maybe (TyCon, [Type])
    mb_tc_app1 = HasDebugCallStack => Type -> Maybe (TyCon, [Type])
    Type -> Maybe (TyCon, [Type])
    splitTyConApp_maybe Type
    ty1
        mb_tc_app2 :: Maybe (TyCon, [Type])
    mb_tc_app2 = HasDebugCallStack => Type -> Maybe (TyCon, [Type])
    Type -> Maybe (TyCon, [Type])
    splitTyConApp_maybe Type
    ty2
        mb_sat_fam_app1 :: Maybe (TyCon, [Type])
    mb_sat_fam_app1 = Type -> Maybe (TyCon, [Type])
    isSatTyFamApp Type
    ty1
        mb_sat_fam_app2 :: Maybe (TyCon, [Type])
    mb_sat_fam_app2 = Type -> Maybe (TyCon, [Type])
    isSatTyFamApp Type
    ty2

    unify_ty UMEnv
    _ Type
    _ Type
    _ OutCoercion
    _ = UM ()
    forall a. UM a
    surelyApart

    -----------------------------
    unify_tc_app :: UMEnv -> TyCon -> [Type] -> [Type] -> UM ()
    -- Mainly just unifies the argument types;
    -- but with a special case for fUNTyCon
    unify_tc_app :: UMEnv -> TyCon -> [Type] -> [Type] -> UM ()
    unify_tc_app UMEnv
    env TyCon
    tc [Type]
    tys1 [Type]
    tys2
      | TyCon
    tc TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyCon
    fUNTyCon
      , MultiplicityFlag
    IgnoreMultiplicities <- UMEnv -> MultiplicityFlag
    um_arr_mult UMEnv
    env
      , (Type
    _mult1 : [Type]
    no_mult_tys1) <- [Type]
    tys1
      , (Type
    _mult2 : [Type]
    no_mult_tys2) <- [Type]
    tys2
      = -- We're comparing function arrow types here (not constraint arrow
        -- types!), and they have at least one argument, which is the arrow's
        -- multiplicity annotation. The flag `um_arr_mult` instructs us to
        -- ignore multiplicities in this very case. This is a little tricky: see
        -- point (3) in Note [Rewrite rules ignore multiplicities in FunTy].
         UMEnv -> [Type] -> [Type] -> UM ()
    unify_tys UMEnv
    env [Type]
    no_mult_tys1 [Type]
    no_mult_tys2

      | Bool
    otherwise
      = UMEnv -> [Type] -> [Type] -> UM ()
    unify_tys UMEnv
    env [Type]
    tys1 [Type]
    tys2

    -----------------------------
    unify_ty_app :: UMEnv -> Type -> [Type] -> Type -> [Type] -> UM ()
    -- Deal with (t1 t1args) ~ (t2 t2args)
    -- where   length t1args = length t2args
    unify_ty_app :: UMEnv -> Type -> [Type] -> Type -> [Type] -> UM ()
    unify_ty_app UMEnv
    env Type
    ty1 [Type]
    ty1args Type
    ty2 [Type]
    ty2args
      | Just (Type
    ty1', Type
    ty1a) <- HasDebugCallStack => Type -> Maybe (Type, Type)
    Type -> Maybe (Type, Type)
    splitAppTyNoView_maybe Type
    ty1
      , Just (Type
    ty2', Type
    ty2a) <- HasDebugCallStack => Type -> Maybe (Type, Type)
    Type -> Maybe (Type, Type)
    splitAppTyNoView_maybe Type
    ty2
      = UMEnv -> Type -> [Type] -> Type -> [Type] -> UM ()
    unify_ty_app UMEnv
    env Type
    ty1' (Type
    ty1a Type -> [Type] -> [Type]
    forall a. a -> [a] -> [a]
    : [Type]
    ty1args) Type
    ty2' (Type
    ty2a Type -> [Type] -> [Type]
    forall a. a -> [a] -> [a]
    : [Type]
    ty2args)

      | Bool
    otherwise
      = do { let ki1 :: Type
    ki1 = HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    ty1
                 ki2 :: Type
    ki2 = HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    ty2
               -- See Note [Kind coercions in Unify]
           ; UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty  UMEnv
    env Type
    ki1 Type
    ki2 (Type -> OutCoercion
    mkNomReflCo Type
    liftedTypeKind)
           ; UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty  UMEnv
    env Type
    ty1 Type
    ty2 (Type -> OutCoercion
    mkNomReflCo Type
    ki2)
                     -- Very important: 'ki2' not 'ki1'
                     -- See Note [Matching in the presence of casts (2)]
           ; UMEnv -> [Type] -> [Type] -> UM ()
    unify_tys UMEnv
    env [Type]
    ty1args [Type]
    ty2args }

    -----------------------------
    unify_tys :: UMEnv -> [Type] -> [Type] -> UM ()
    -- Precondition: see (Unification Kind Invariant)
    unify_tys :: UMEnv -> [Type] -> [Type] -> UM ()
    unify_tys UMEnv
    env [Type]
    orig_xs [Type]
    orig_ys
      = [Type] -> [Type] -> UM ()
    go [Type]
    orig_xs [Type]
    orig_ys
      where
        go :: [Type] -> [Type] -> UM ()
    go []     []     = () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
        go (Type
    x:[Type]
    xs) (Type
    y:[Type]
    ys)
          -- See Note [Kind coercions in Unify]
          = do { UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    env Type
    x Type
    y (Type -> OutCoercion
    mkNomReflCo (Type -> OutCoercion) -> Type -> OutCoercion
    forall a b. (a -> b) -> a -> b
    $ HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    y)
                     -- Very important: 'y' not 'x'
                     -- See Note [Matching in the presence of casts (2)]
               ; [Type] -> [Type] -> UM ()
    go [Type]
    xs [Type]
    ys }
        go [Type]
    _ [Type]
    _ = UM ()
    forall a. UM a
    surelyApart
          -- Possibly different saturations of a polykinded tycon
          -- See Note [Polykinded tycon applications]

    ---------------------------------
    uVarOrFam :: UMEnv -> CanEqLHS -> InType -> OutCoercion -> UM ()
    -- Invariants: (a) If ty1 is a TyFamLHS, then ty2 is NOT a TyVarTy
    --             (b) both args have had coreView already applied
    -- Why saturated?  See (ATF4) in Note [Apartness and type families]
    uVarOrFam :: UMEnv -> CanEqLHS -> Type -> OutCoercion -> UM ()
    uVarOrFam UMEnv
    env CanEqLHS
    ty1 Type
    ty2 OutCoercion
    kco
      = do { substs <- UM UMState
    getSubstEnvs
    --       ; pprTrace "uVarOrFam" (vcat
    --           [ text "ty1" <+> ppr ty1
    --           , text "ty2" <+> ppr ty2
    --           , text "tv_env" <+> ppr (um_tv_env substs)
    --           , text "fam_env" <+> ppr (um_fam_env substs) ]) $
           ; go NotSwapped substs ty1 ty2 kco }
      where
        foralld_tvs :: TyCoVarSet
    foralld_tvs  = UMEnv -> TyCoVarSet
    um_foralls UMEnv
    env
        under_forall :: Bool
    under_forall = Bool -> Bool
    not (TyCoVarSet -> Bool
    isEmptyVarSet TyCoVarSet
    foralld_tvs)

        -- `go` takes two bites at the cherry; if the first one fails
        -- it swaps the arguments and tries again; and then it fails.
        -- The SwapFlag argument tells `go` whether it is on the first
        -- bite (NotSwapped) or the second (IsSwapped).
        -- E.g.    a ~ F p q
        --         Starts with: go a (F p q)
        --         if `a` not bindable, swap to: go (F p q) a

        -----------------------------
        -- LHS is a type variable
        -- The sequence of tests is very similar to go_tv
        go :: SwapFlag -> UMState -> CanEqLHS -> InType -> OutCoercion -> UM ()
        go :: SwapFlag -> UMState -> CanEqLHS -> Type -> OutCoercion -> UM ()
    go SwapFlag
    swapped UMState
    substs lhs :: CanEqLHS
    lhs@(TyVarLHS Var
    tv1) Type
    ty2 OutCoercion
    kco
          | Just Type
    ty1' <- TvSubstEnv -> Var -> Maybe Type
    forall a. VarEnv a -> Var -> Maybe a
    lookupVarEnv (UMState -> TvSubstEnv
    um_tv_env UMState
    substs) Var
    tv1'
          = -- We already have a substitution for tv1
            if | UMEnv -> Bool
    um_unif UMEnv
    env                          -> UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    env Type
    ty1' Type
    ty2 OutCoercion
    kco
               | (Type
    ty1' Type -> OutCoercion -> Type
    `mkCastTy` OutCoercion
    kco) HasDebugCallStack => Type -> Type -> Bool
    Type -> Type -> Bool
    `tcEqType` Type
    ty2 -> () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
               | Bool
    otherwise                            -> UM ()
    forall a. UM a
    surelyApart
               -- Unifying: recurse into unify_ty
               -- Matching: we /don't/ want to just recurse here, because the range of
               --    the subst is the target type, not the template type. So, just check
               --    for normal type equality.
               -- NB: it's important to use `tcEqType` instead of `eqType` here,
               -- otherwise we might not reject a substitution
               -- which unifies `Type` with `Constraint`, e.g.
               -- a call to tc_unify_tys with arguments
               --
               --   tys1 = [k,k]
               --   tys2 = [Type, Constraint]
               --
               -- See test cases: T11715b, T20521.

          -- If we are matching or unifying a ~ a, take care
          -- See Note [Self-substitution when unifying or matching]
          | TyVarTy Var
    tv2 <- Type
    ty2
          , let tv2' :: Var
    tv2' = UMEnv -> Var -> Var
    umRnOccR UMEnv
    env Var
    tv2
          , Var
    tv1' Var -> Var -> Bool
    forall a. Eq a => a -> a -> Bool
    == Var
    tv2'
          = if | UMEnv -> Bool
    um_unif UMEnv
    env     -> () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
               | Bool
    tv1_is_bindable -> Var -> Type -> UM ()
    extendTvEnv Var
    tv1' Type
    ty2
               | Bool
    otherwise       -> () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()

          | Bool
    tv1_is_bindable
          , Bool -> Bool
    not (UMEnv -> TyCoVarSet -> Bool
    mentionsForAllBoundTyVarsR UMEnv
    env TyCoVarSet
    ty2_fvs)
                -- ty2_fvs: kco does not mention forall-bound vars
          , Bool -> Bool
    not Bool
    occurs_check
          = -- No occurs check, nor skolem-escape; just bind the tv
            -- We don't need to rename `rhs` because it mentions no forall-bound vars
            Var -> Type -> UM ()
    extendTvEnv Var
    tv1' Type
    rhs     -- Bind tv1:=rhs and continue

          -- When unifying, try swapping:
          -- e.g.   a    ~ F p q       with `a` not bindable: we might succeed with go_fam
          -- e.g.   a    ~ beta        with `a` not bindable: we might be able to bind `beta`
          -- e.g.   beta ~ F beta Int  occurs check; but MaybeApart after swapping
          | UMEnv -> Bool
    um_unif UMEnv
    env
          , SwapFlag
    NotSwapped <- SwapFlag
    swapped  -- If we have swapped already, don't do so again
          , Just CanEqLHS
    lhs2 <- Type -> Maybe CanEqLHS
    canEqLHS_maybe Type
    ty2
          = SwapFlag -> UMState -> CanEqLHS -> Type -> OutCoercion -> UM ()
    go SwapFlag
    IsSwapped UMState
    substs CanEqLHS
    lhs2 (Var -> Type
    mkTyVarTy Var
    tv1) (OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    kco)

          | Bool
    occurs_check = MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    MARInfinite   -- Occurs check
          | Bool
    otherwise    = UM ()
    forall a. UM a
    surelyApart

          where
            tv1' :: Var
    tv1'            = UMEnv -> Var -> Var
    umRnOccL UMEnv
    env Var
    tv1
            ty2_fvs :: TyCoVarSet
    ty2_fvs         = Type -> TyCoVarSet
    tyCoVarsOfType Type
    ty2
            rhs :: Type
    rhs             = Type
    ty2 Type -> OutCoercion -> Type
    `mkCastTy` OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    kco
            tv1_is_bindable :: Bool
    tv1_is_bindable | Bool -> Bool
    not (Var
    tv1' Var -> TyCoVarSet -> Bool
    `elemVarSet` TyCoVarSet
    foralld_tvs)
                              -- tv1' is not forall-bound, but tv1 can still differ
                              -- from tv1; see Note [Cloning the template binders]
                              -- in GHC.Core.Rules.  So give tv1' to um_bind_tv_fun.
                            , BindFlag
    BindMe <- UMEnv -> BindTvFun
    um_bind_tv_fun UMEnv
    env Var
    tv1' Type
    rhs
                            = Bool
    True
                            | Bool
    otherwise
                            = Bool
    False

            occurs_check :: Bool
    occurs_check = UMEnv -> Bool
    um_unif UMEnv
    env Bool -> Bool -> Bool
    && UMState -> TyCoVarSet -> CanEqLHS -> Type -> Bool
    uOccursCheck UMState
    substs TyCoVarSet
    foralld_tvs CanEqLHS
    lhs Type
    rhs
              -- Occurs check, only when unifying
              -- see Note [Infinitary substitutions]
              -- Make sure you include `kco` in rhs #14846

        -----------------------------
        -- LHS is a saturated type-family application
        -- Invariant: ty2 is not a TyVarTy
        go SwapFlag
    swapped UMState
    substs lhs :: CanEqLHS
    lhs@(TyFamLHS TyCon
    tc1 [Type]
    tys1) Type
    ty2 OutCoercion
    kco
          -- Check if we have an existing substitution for the LHS; if so, recurse
          -- But not under a forall; see (ATF3) in Note [Apartness and type families]
          -- Hence the RnEnv2 is empty
          | Bool -> Bool
    not Bool
    under_forall
          , Just Type
    ty1' <- FamSubstEnv -> TyCon -> [Type] -> Maybe Type
    lookupFamEnv (UMState -> FamSubstEnv
    um_fam_env UMState
    substs) TyCon
    tc1 [Type]
    tys1
          = if | UMEnv -> Bool
    um_unif UMEnv
    env                          -> UMEnv -> Type -> Type -> OutCoercion -> UM ()
    unify_ty UMEnv
    env Type
    ty1' Type
    ty2 OutCoercion
    kco
               -- Below here we are matching
               -- The return () case deals with:
               --    Template:   (F a)..(F a)
               --    Target:     (F b)..(F b)
               -- This should match! With [a :-> b]
               | (Type
    ty1' Type -> OutCoercion -> Type
    `mkCastTy` OutCoercion
    kco) HasDebugCallStack => Type -> Type -> Bool
    Type -> Type -> Bool
    `tcEqType` Type
    ty2 -> () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
               | Bool
    otherwise                            -> MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    MARTypeFamily

          -- Check for equality  F tys1 ~ F tys2
          -- Very important that this can happen under a forall, so that we
          -- successfully match  (forall a. F a) ~ (forall b. F b)  See (ATF9-2)
          | Just (TyCon
    tc2, [Type]
    tys2) <- Type -> Maybe (TyCon, [Type])
    isSatTyFamApp Type
    ty2
          , TyCon
    tc1 TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyCon
    tc2
          = UMState -> TyCon -> [Type] -> [Type] -> OutCoercion -> UM ()
    go_fam_fam UMState
    substs TyCon
    tc1 [Type]
    tys1 [Type]
    tys2 OutCoercion
    kco

          -- If we are under a forall, just give up
          -- see (ATF3) and (ATF5) in Note [Apartness and type families]
          | Bool
    under_forall
          = MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    MARTypeFamily

          -- Now check if we can bind the (F tys) to the RHS
          -- Again, not under a forall; see (ATF3)
          -- This can happen even when matching: see (ATF7)
          | BindFlag
    BindMe <- UMEnv -> BindFamFun
    um_bind_fam_fun UMEnv
    env TyCon
    tc1 [Type]
    tys1 Type
    rhs
          = if UMState -> TyCoVarSet -> CanEqLHS -> Type -> Bool
    uOccursCheck UMState
    substs TyCoVarSet
    emptyVarSet CanEqLHS
    lhs Type
    rhs
            then MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    MARInfinite
            else do { TyCon -> [Type] -> Type -> UM ()
    extendFamEnv TyCon
    tc1 [Type]
    tys1 Type
    rhs
                         -- We don't substitute tys1 before extending
                         -- See Note [Shortcomings of the apartness test]
                    ; MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    MARTypeFamily }

          -- Swap in case of (F a b) ~ (G c d e)
          -- Maybe um_bind_fam_fun is False of (F a b) but true of (G c d e)
          -- NB: a type family can appear on the template when matching
          --     see (ATF6) in Note [Apartness and type families]
          -- (Only worth doing this if we are not under a forall.)
          | UMEnv -> Bool
    um_unif UMEnv
    env
          , SwapFlag
    NotSwapped <- SwapFlag
    swapped
          , Just CanEqLHS
    lhs2 <- Type -> Maybe CanEqLHS
    canEqLHS_maybe Type
    ty2
          = SwapFlag -> UMState -> CanEqLHS -> Type -> OutCoercion -> UM ()
    go SwapFlag
    IsSwapped UMState
    substs CanEqLHS
    lhs2 (TyCon -> [Type] -> Type
    mkTyConApp TyCon
    tc1 [Type]
    tys1) (OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    kco)

          | Bool
    otherwise   -- See (ATF5) in Note [Apartness and type families]
          = UM ()
    forall a. UM a
    surelyApart

          where
            rhs :: Type
    rhs = Type
    ty2 Type -> OutCoercion -> Type
    `mkCastTy` OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    kco

        -----------------------------
        -- go_fam_fam: LHS and RHS are both saturated type-family applications,
        --             for the same type-family F
        go_fam_fam :: UMState -> TyCon -> [Type] -> [Type] -> OutCoercion -> UM ()
    go_fam_fam UMState
    substs TyCon
    tc [Type]
    tys1 [Type]
    tys2 OutCoercion
    kco
           -- Decompose (F tys1 ~ F tys2): (ATF9)
           -- Use injectivity information of F: (ATF10)
           -- But first bind the type-fam if poss: (ATF11)
          = do { UM ()
    bind_fam_if_poss                 -- (ATF11)
               ; UMEnv -> [Type] -> [Type] -> UM ()
    unify_tys UMEnv
    env [Type]
    inj_tys1 [Type]
    inj_tys2  -- (ATF10)
               ; Bool -> UM () -> UM ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    unless (UMEnv -> Bool
    um_inj_tf UMEnv
    env) (UM () -> UM ()) -> UM () -> UM ()
    forall a b. (a -> b) -> a -> b
    $         -- (ATF12)
                 MaybeApartReason -> UM () -> UM ()
    don'tBeSoSure MaybeApartReason
    MARTypeFamily (UM () -> UM ()) -> UM () -> UM ()
    forall a b. (a -> b) -> a -> b
    $    -- (ATF9-1)
                 UMEnv -> [Type] -> [Type] -> UM ()
    unify_tys UMEnv
    env [Type]
    noninj_tys1 [Type]
    noninj_tys2 }
         where
           inj :: [Bool]
    inj = case TyCon -> Injectivity
    tyConInjectivityInfo TyCon
    tc of
                    Injectivity
    NotInjective -> Bool -> [Bool]
    forall a. a -> [a]
    repeat Bool
    False
                    Injective [Bool]
    bs -> [Bool]
    bs

           ([Type]
    inj_tys1, [Type]
    noninj_tys1) = [Bool] -> [Type] -> ([Type], [Type])
    forall a. [Bool] -> [a] -> ([a], [a])
    partitionByList [Bool]
    inj [Type]
    tys1
           ([Type]
    inj_tys2, [Type]
    noninj_tys2) = [Bool] -> [Type] -> ([Type], [Type])
    forall a. [Bool] -> [a] -> ([a], [a])
    partitionByList [Bool]
    inj [Type]
    tys2

           bind_fam_if_poss :: UM ()
    bind_fam_if_poss
             | Bool -> Bool
    not (UMEnv -> Bool
    um_unif UMEnv
    env)  -- Not when matching (ATF11-1)
             = () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
             | Bool
    under_forall       -- Not under a forall (ATF3)
             = () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
             | BindFlag
    BindMe <- UMEnv -> BindFamFun
    um_bind_fam_fun UMEnv
    env TyCon
    tc [Type]
    tys1 Type
    rhs1
             = Bool -> UM () -> UM ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    unless (UMState -> TyCoVarSet -> CanEqLHS -> Type -> Bool
    uOccursCheck UMState
    substs TyCoVarSet
    emptyVarSet (TyCon -> [Type] -> CanEqLHS
    TyFamLHS TyCon
    tc [Type]
    tys1) Type
    rhs1) (UM () -> UM ()) -> UM () -> UM ()
    forall a b. (a -> b) -> a -> b
    $
               TyCon -> [Type] -> Type -> UM ()
    extendFamEnv TyCon
    tc [Type]
    tys1 Type
    rhs1
             -- At this point um_unif=True, so we can unify either way
             | BindFlag
    BindMe <- UMEnv -> BindFamFun
    um_bind_fam_fun UMEnv
    env TyCon
    tc [Type]
    tys2 Type
    rhs2
             = Bool -> UM () -> UM ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    unless (UMState -> TyCoVarSet -> CanEqLHS -> Type -> Bool
    uOccursCheck UMState
    substs TyCoVarSet
    emptyVarSet (TyCon -> [Type] -> CanEqLHS
    TyFamLHS TyCon
    tc [Type]
    tys2) Type
    rhs2) (UM () -> UM ()) -> UM () -> UM ()
    forall a b. (a -> b) -> a -> b
    $
               TyCon -> [Type] -> Type -> UM ()
    extendFamEnv TyCon
    tc [Type]
    tys2 Type
    rhs2
             | Bool
    otherwise
             = () -> UM ()
    forall a. a -> UM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()

           rhs1 :: Type
    rhs1 = TyCon -> [Type] -> Type
    mkTyConApp TyCon
    tc [Type]
    tys2 Type -> OutCoercion -> Type
    `mkCastTy` OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    kco
           rhs2 :: Type
    rhs2 = TyCon -> [Type] -> Type
    mkTyConApp TyCon
    tc [Type]
    tys1 Type -> OutCoercion -> Type
    `mkCastTy` OutCoercion
    kco

    uOccursCheck :: UMState
                 -> TyVarSet -- Bound by enclosing foralls; see (OCU1)
                 -> CanEqLHS -> Type   -- Can we unify (lhs := ty)?
                 -> Bool
    -- See Note [The occurs check in the Core unifier] and (ATF13)
    uOccursCheck :: UMState -> TyCoVarSet -> CanEqLHS -> Type -> Bool
    uOccursCheck (UMState { um_tv_env :: UMState -> TvSubstEnv
    um_tv_env = TvSubstEnv
    tv_env, um_fam_env :: UMState -> FamSubstEnv
    um_fam_env = FamSubstEnv
    fam_env }) TyCoVarSet
    bvs CanEqLHS
    lhs Type
    ty
      = TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    ty
      where
        go :: TyCoVarSet   -- Bound by enclosing foralls; see (OCU1)
           -> Type -> Bool
        go :: TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    ty | Just Type
    ty' <- Type -> Maybe Type
    coreView Type
    ty = TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    ty'
        go TyCoVarSet
    bvs (TyVarTy Var
    tv) | Just Type
    ty' <- TvSubstEnv -> Var -> Maybe Type
    forall a. VarEnv a -> Var -> Maybe a
    lookupVarEnv TvSubstEnv
    tv_env Var
    tv
                            = TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    ty'
                            | TyVarLHS Var
    tv' <- CanEqLHS
    lhs, Var
    tvVar -> Var -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Var
    tv'
                            = Bool
    True
                            | Bool
    otherwise
                            = TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs (Var -> Type
    tyVarKind Var
    tv)
        go TyCoVarSet
    bvs (AppTy Type
    ty1 Type
    ty2)           = TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    ty1 Bool -> Bool -> Bool
    || TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    ty2
        go TyCoVarSet
    _   (LitTy {})                = Bool
    False
        go TyCoVarSet
    bvs (FunTy FunTyFlag
    _ Type
    w Type
    arg Type
    res)       = TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    w Bool -> Bool -> Bool
    || TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    arg Bool -> Bool -> Bool
    || TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    res
        go TyCoVarSet
    bvs (TyConApp TyCon
    tc [Type]
    tys)         = TyCoVarSet -> TyCon -> [Type] -> Bool
    go_tc TyCoVarSet
    bvs TyCon
    tc [Type]
    tys

        go TyCoVarSet
    bvs (ForAllTy (Bndr Var
    tv ForAllTyFlag
    _) Type
    ty)
          = TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs (Var -> Type
    tyVarKind Var
    tv) Bool -> Bool -> Bool
    ||
            (case CanEqLHS
    lhs of
               TyVarLHS Var
    tv' | Var
    tvVar -> Var -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Var
    tv'   -> Bool
    False  -- Shadowing
                            | Bool
    otherwise -> TyCoVarSet -> Type -> Bool
    go (TyCoVarSet
    bvs TyCoVarSet -> Var -> TyCoVarSet
    `extendVarSet` Var
    tv) Type
    ty
               TyFamLHS {} -> Bool
    False)  -- Lookups don't happen under a forall

        go TyCoVarSet
    bvs (CastTy Type
    ty  OutCoercion
    _co) = TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    ty  -- ToDo: should we worry about `co`?
        go TyCoVarSet
    _   (CoercionTy OutCoercion
    _co) = Bool
    False      -- ToDo: should we worry about `co`?

        go_tc :: TyCoVarSet -> TyCon -> [Type] -> Bool
    go_tc TyCoVarSet
    bvs TyCon
    tc [Type]
    tys
          | TyCoVarSet -> Bool
    isEmptyVarSet TyCoVarSet
    bvs   -- Never look up in um_fam_env under a forall (ATF3)
          , TyCon -> Bool
    isTypeFamilyTyCon TyCon
    tc
          , Just Type
    ty' <- FamSubstEnv -> TyCon -> [Type] -> Maybe Type
    lookupFamEnv FamSubstEnv
    fam_env TyCon
    tc (Int -> [Type] -> [Type]
    forall a. Int -> [a] -> [a]
    take Int
    arity [Type]
    tys)
                 -- NB: we look up /un-substituted/ types;
                 -- See Note [Shortcomings of the apartness test]
          = TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs Type
    ty' Bool -> Bool -> Bool
    || (Type -> Bool) -> [Type] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any (TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs) (Int -> [Type] -> [Type]
    forall a. Int -> [a] -> [a]
    drop Int
    arity [Type]
    tys)

          | TyFamLHS TyCon
    tc' [Type]
    tys' <- CanEqLHS
    lhs
          , TyCon
    tc TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyCon
    tc'
          , [Type]
    tys [Type] -> Int -> Bool
    forall a. [a] -> Int -> Bool
    `lengthAtLeast` Int
    arity  -- Saturated, or over-saturated
          , [Type] -> [Type] -> Bool
    tcEqTyConAppArgs [Type]
    tys [Type]
    tys'
          = Bool
    True

          | Bool
    otherwise
          = (Type -> Bool) -> [Type] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any (TyCoVarSet -> Type -> Bool
    go TyCoVarSet
    bvs) [Type]
    tys
          where
            arity :: Int
    arity = TyCon -> Int
    tyConArity TyCon
    tc

    {- Note [The occurs check in the Core unifier]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The unifier applies both substitutions (um_tv_env and um_fam_env) as it goes,
    so we'll get an infinite loop if we have, for example
        um_tv_env:   a :-> F b      -- (1)
        um_fam_env   F b :-> a      -- (2)

    So (uOccursCheck substs lhs ty) returns True iff extending `substs` with `lhs :-> ty`
    could lead to a loop. That is, could there by a type `s` such that
      applySubsts( (substs + lhs:->ty), s ) is infinite

    It's vital that we do both at once: we might have (1) already and add (2);
    or we might have (2) already and add (1).

    A very similar task is done by GHC.Tc.Utils.Unify.checkTyEqRhs.

    (OCU1) We keep track of the forall-bound variables because the um_fam_env is inactive
      under a forall; indeed it is /unsound/ to consult it because we may have a binding
      (F a :-> Int), and then unify (forall a. ...(F a)...) with something.  We don't
      want to map that (F a) to Int!

    (OCU2) Performance. Consider unifying
             [a, b] ~ [big-ty, (a,a,a)]
      We'll unify a:=big-ty.  Then we'll attempt b:=(a,a,a), but must do an occurs check.
      So we'll walk over big-ty, looking for `b`.  And then again, and again, once for
      each occurrence of `a`.  A similar thing happens for
             [a, (b,b,b)] ~ [big-ty, (a,a,a)]
      albeit a bit less obviously.

      Potentially we could use a cache to record checks we have already done;
      but I have not attempted that yet.  Precisely similar remarks would apply
      to GHC.Tc.Utils.Unify.checkTyEqRhs

    Note [Unifying coercion-foralls]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose we try to unify (forall cv. t1) ~ (forall cv. t2).
    See Note [ForAllTy] in GHC.Core.TyCo.Rep.

    The problem with coercion variables is that coercion abstraction is not erased:
    the `kco` shouldn't propagate from outside the ForAllTy to inside. Instead, I think
    the correct new `kco` for the recursive call is `mkNomReflCo liftedTypeKind` (but I'm
    a little worried it might be Constraint sometimes).

    This potential problem has been there a long time, and I'm going to let
    sleeping dogs lie for now.
    -}

    {-
    ************************************************************************
    *                                                                      *
                    Unification monad
    *                                                                      *
    ************************************************************************
    -}

    data UMEnv
      = UMEnv { UMEnv -> Bool
    um_unif :: AmIUnifying

              , UMEnv -> Bool
    um_inj_tf :: Bool
                -- Checking for injectivity?
                -- See (SI1) in Note [Specification of unification]

              , UMEnv -> MultiplicityFlag
    um_arr_mult :: MultiplicityFlag
                -- Whether to unify multiplicity arguments when unifying arrows.
                -- See Note [Rewrite rules ignore multiplicities in FunTy]

              , UMEnv -> RnEnv2
    um_rn_env :: RnEnv2
                -- Renaming InTyVars to OutTyVars; this eliminates shadowing, and
                -- lines up matching foralls on the left and right
                -- See (CU2) in Note [The Core unifier]

              , UMEnv -> TyCoVarSet
    um_foralls :: TyVarSet
                -- OutTyVars bound by a forall in this unification;
                -- Do not bind these in the substitution!
                -- See the function tvBindFlag

              , UMEnv -> BindTvFun
    um_bind_tv_fun :: BindTvFun
                -- User-supplied BindFlag function, for variables not in um_foralls
                -- See (CU1) in Note [The Core unifier]

              , UMEnv -> BindFamFun
    um_bind_fam_fun :: BindFamFun
                -- Similar to um_bind_tv_fun, but for type-family applications
                -- See (ATF8) in Note [Apartness and type families]
              }

    type FamSubstEnv = TyConEnv (ListMap TypeMap Type)
      -- Map a TyCon and a list of types to a type
      -- Domain of FamSubstEnv is exactly-saturated type-family
      -- applications (F t1...tn)

    lookupFamEnv :: FamSubstEnv -> TyCon -> [Type] -> Maybe Type
    lookupFamEnv :: FamSubstEnv -> TyCon -> [Type] -> Maybe Type
    lookupFamEnv FamSubstEnv
    env TyCon
    tc [Type]
    tys
      = do { tys_map <- FamSubstEnv -> TyCon -> Maybe (ListMap TypeMap Type)
    forall a. TyConEnv a -> TyCon -> Maybe a
    lookupTyConEnv FamSubstEnv
    env TyCon
    tc
           ; lookupTM tys tys_map }

    data UMState = UMState
                       { UMState -> TvSubstEnv
    um_tv_env   :: TvSubstEnv
                       , UMState -> CvSubstEnv
    um_cv_env   :: CvSubstEnv
                       , UMState -> FamSubstEnv
    um_fam_env  :: FamSubstEnv }
      -- um_tv_env, um_cv_env, um_fam_env are all "global" substitutions;
      -- that is, neither their domains nor their ranges mention any variables
      -- in um_foralls; i.e. variables bound by foralls inside the types being unified

      -- When /matching/ um_fam_env is usually empty; but not quite always.
      -- See (ATF7) of Note [Apartness and type families]

    newtype UM a
      = UM' { forall a. UM a -> UMState -> UnifyResultM (UMState, a)
    unUM :: UMState -> UnifyResultM (UMState, a) }
        -- See Note [The one-shot state monad trick] in GHC.Utils.Monad

    pattern UM :: (UMState -> UnifyResultM (UMState, a)) -> UM a
    -- See Note [The one-shot state monad trick] in GHC.Utils.Monad
    pattern $mUM :: forall {r} {a}.
    UM a
    -> ((UMState -> UnifyResultM (UMState, a)) -> r)
    -> ((# #) -> r)
    -> r
    $bUM :: forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM m <- UM' m
      where
        UM UMState -> UnifyResultM (UMState, a)
    m = (UMState -> UnifyResultM (UMState, a)) -> UM a
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM' ((UMState -> UnifyResultM (UMState, a))
    -> UMState -> UnifyResultM (UMState, a)
    forall a b. (a -> b) -> a -> b
    oneShot UMState -> UnifyResultM (UMState, a)
    m)
    {-# COMPLETE UM #-}

    instance Functor UM where
      fmap :: forall a b. (a -> b) -> UM a -> UM b
    fmap a -> b
    f (UM UMState -> UnifyResultM (UMState, a)
    m) = (UMState -> UnifyResultM (UMState, b)) -> UM b
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM (\UMState
    s -> ((UMState, a) -> (UMState, b))
    -> UnifyResultM (UMState, a) -> UnifyResultM (UMState, b)
    forall a b. (a -> b) -> UnifyResultM a -> UnifyResultM b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (\(UMState
    s', a
    v) -> (UMState
    s', a -> b
    f a
    v)) (UMState -> UnifyResultM (UMState, a)
    m UMState
    s))

    instance Applicative UM where
          pure :: forall a. a -> UM a
    pure a
    a = (UMState -> UnifyResultM (UMState, a)) -> UM a
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM (\UMState
    s -> (UMState, a) -> UnifyResultM (UMState, a)
    forall a. a -> UnifyResultM a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (UMState
    s, a
    a))
          <*> :: forall a b. UM (a -> b) -> UM a -> UM b
    (<*>)  = UM (a -> b) -> UM a -> UM b
    forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
    ap

    instance Monad UM where
      {-# INLINE (>>=) #-}
      -- See Note [INLINE pragmas and (>>)] in GHC.Utils.Monad
      UM a
    m >>= :: forall a b. UM a -> (a -> UM b) -> UM b
    >>= a -> UM b
    k  = (UMState -> UnifyResultM (UMState, b)) -> UM b
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM (\UMState
    state ->
                      do { (state', v) <- UM a -> UMState -> UnifyResultM (UMState, a)
    forall a. UM a -> UMState -> UnifyResultM (UMState, a)
    unUM UM a
    m UMState
    state
                         ; unUM (k v) state' })

    instance MonadFail UM where
        fail :: forall a. HasCallStack => String -> UM a
    fail String
    _   = (UMState -> UnifyResultM (UMState, a)) -> UM a
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM (\UMState
    _ -> UnifyResultM (UMState, a)
    forall a. UnifyResultM a
    SurelyApart) -- failed pattern match

    initUM :: TvSubstEnv  -- subst to extend
           -> CvSubstEnv
           -> UM ()
           -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    initUM :: TvSubstEnv
    -> CvSubstEnv -> UM () -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    initUM TvSubstEnv
    subst_env CvSubstEnv
    cv_subst_env UM ()
    um
      = case UM () -> UMState -> UnifyResultM (UMState, ())
    forall a. UM a -> UMState -> UnifyResultM (UMState, a)
    unUM UM ()
    um UMState
    state of
          Unifiable (UMState
    state, ()
    _)    -> (TvSubstEnv, CvSubstEnv) -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    forall a. a -> UnifyResultM a
    Unifiable (UMState -> (TvSubstEnv, CvSubstEnv)
    get UMState
    state)
          MaybeApart MaybeApartReason
    r (UMState
    state, ()
    _) -> MaybeApartReason
    -> (TvSubstEnv, CvSubstEnv)
    -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    forall a. MaybeApartReason -> a -> UnifyResultM a
    MaybeApart MaybeApartReason
    r (UMState -> (TvSubstEnv, CvSubstEnv)
    get UMState
    state)
          UnifyResultM (UMState, ())
    SurelyApart             -> UnifyResultM (TvSubstEnv, CvSubstEnv)
    forall a. UnifyResultM a
    SurelyApart
      where
        state :: UMState
    state = UMState { um_tv_env :: TvSubstEnv
    um_tv_env = TvSubstEnv
    subst_env
                        , um_cv_env :: CvSubstEnv
    um_cv_env = CvSubstEnv
    cv_subst_env
                        , um_fam_env :: FamSubstEnv
    um_fam_env = FamSubstEnv
    forall a. TyConEnv a
    emptyTyConEnv }
        get :: UMState -> (TvSubstEnv, CvSubstEnv)
    get (UMState { um_tv_env :: UMState -> TvSubstEnv
    um_tv_env = TvSubstEnv
    tv_env, um_cv_env :: UMState -> CvSubstEnv
    um_cv_env = CvSubstEnv
    cv_env }) = (TvSubstEnv
    tv_env, CvSubstEnv
    cv_env)

    getTvSubstEnv :: UM TvSubstEnv
    getTvSubstEnv :: UM TvSubstEnv
    getTvSubstEnv = (UMState -> UnifyResultM (UMState, TvSubstEnv)) -> UM TvSubstEnv
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM ((UMState -> UnifyResultM (UMState, TvSubstEnv)) -> UM TvSubstEnv)
    -> (UMState -> UnifyResultM (UMState, TvSubstEnv)) -> UM TvSubstEnv
    forall a b. (a -> b) -> a -> b
    $ \UMState
    state -> (UMState, TvSubstEnv) -> UnifyResultM (UMState, TvSubstEnv)
    forall a. a -> UnifyResultM a
    Unifiable (UMState
    state, UMState -> TvSubstEnv
    um_tv_env UMState
    state)

    getCvSubstEnv :: UM CvSubstEnv
    getCvSubstEnv :: UM CvSubstEnv
    getCvSubstEnv = (UMState -> UnifyResultM (UMState, CvSubstEnv)) -> UM CvSubstEnv
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM ((UMState -> UnifyResultM (UMState, CvSubstEnv)) -> UM CvSubstEnv)
    -> (UMState -> UnifyResultM (UMState, CvSubstEnv)) -> UM CvSubstEnv
    forall a b. (a -> b) -> a -> b
    $ \UMState
    state -> (UMState, CvSubstEnv) -> UnifyResultM (UMState, CvSubstEnv)
    forall a. a -> UnifyResultM a
    Unifiable (UMState
    state, UMState -> CvSubstEnv
    um_cv_env UMState
    state)

    getSubstEnvs :: UM UMState
    getSubstEnvs :: UM UMState
    getSubstEnvs = (UMState -> UnifyResultM (UMState, UMState)) -> UM UMState
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM ((UMState -> UnifyResultM (UMState, UMState)) -> UM UMState)
    -> (UMState -> UnifyResultM (UMState, UMState)) -> UM UMState
    forall a b. (a -> b) -> a -> b
    $ \UMState
    state -> (UMState, UMState) -> UnifyResultM (UMState, UMState)
    forall a. a -> UnifyResultM a
    Unifiable (UMState
    state, UMState
    state)

    getSubst :: UMEnv -> UM Subst
    getSubst :: UMEnv -> UM Subst
    getSubst UMEnv
    env = do { tv_env <- UM TvSubstEnv
    getTvSubstEnv
                      ; cv_env <- getCvSubstEnv
                      ; let in_scope = RnEnv2 -> InScopeSet
    rnInScopeSet (UMEnv -> RnEnv2
    um_rn_env UMEnv
    env)
                      ; return (mkTCvSubst in_scope tv_env cv_env) }

    extendTvEnv :: TyVar -> Type -> UM ()
    extendTvEnv :: Var -> Type -> UM ()
    extendTvEnv Var
    tv Type
    ty = (UMState -> UnifyResultM (UMState, ())) -> UM ()
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM ((UMState -> UnifyResultM (UMState, ())) -> UM ())
    -> (UMState -> UnifyResultM (UMState, ())) -> UM ()
    forall a b. (a -> b) -> a -> b
    $ \UMState
    state ->
      (UMState, ()) -> UnifyResultM (UMState, ())
    forall a. a -> UnifyResultM a
    Unifiable (UMState
    state { um_tv_env = extendVarEnv (um_tv_env state) tv ty }, ())

    extendCvEnv :: CoVar -> Coercion -> UM ()
    extendCvEnv :: Var -> OutCoercion -> UM ()
    extendCvEnv Var
    cv OutCoercion
    co = (UMState -> UnifyResultM (UMState, ())) -> UM ()
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM ((UMState -> UnifyResultM (UMState, ())) -> UM ())
    -> (UMState -> UnifyResultM (UMState, ())) -> UM ()
    forall a b. (a -> b) -> a -> b
    $ \UMState
    state ->
      (UMState, ()) -> UnifyResultM (UMState, ())
    forall a. a -> UnifyResultM a
    Unifiable (UMState
    state { um_cv_env = extendVarEnv (um_cv_env state) cv co }, ())

    extendFamEnv :: TyCon -> [Type] -> Type -> UM ()
    extendFamEnv :: TyCon -> [Type] -> Type -> UM ()
    extendFamEnv TyCon
    tc [Type]
    tys Type
    ty = (UMState -> UnifyResultM (UMState, ())) -> UM ()
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM ((UMState -> UnifyResultM (UMState, ())) -> UM ())
    -> (UMState -> UnifyResultM (UMState, ())) -> UM ()
    forall a b. (a -> b) -> a -> b
    $ \UMState
    state ->
      (UMState, ()) -> UnifyResultM (UMState, ())
    forall a. a -> UnifyResultM a
    Unifiable (UMState
    state { um_fam_env = extend (um_fam_env state) tc }, ())
      where
        extend :: FamSubstEnv -> TyCon -> FamSubstEnv
        extend :: FamSubstEnv -> TyCon -> FamSubstEnv
    extend = (Maybe (ListMap TypeMap Type) -> Maybe (ListMap TypeMap Type))
    -> FamSubstEnv -> TyCon -> FamSubstEnv
    forall a. (Maybe a -> Maybe a) -> TyConEnv a -> TyCon -> TyConEnv a
    alterTyConEnv Maybe (ListMap TypeMap Type) -> Maybe (ListMap TypeMap Type)
    alter_tm

        alter_tm :: Maybe (ListMap TypeMap Type) -> Maybe (ListMap TypeMap Type)
        alter_tm :: Maybe (ListMap TypeMap Type) -> Maybe (ListMap TypeMap Type)
    alter_tm Maybe (ListMap TypeMap Type)
    m_elt = ListMap TypeMap Type -> Maybe (ListMap TypeMap Type)
    forall a. a -> Maybe a
    Just (Key (ListMap TypeMap)
    -> XT Type -> ListMap TypeMap Type -> ListMap TypeMap Type
    forall b.
    Key (ListMap TypeMap)
    -> XT b -> ListMap TypeMap b -> ListMap TypeMap b
    forall (m :: * -> *) b. TrieMap m => Key m -> XT b -> m b -> m b
    alterTM [Type]
    Key (ListMap TypeMap)
    tys (\Maybe Type
    _ -> Type -> Maybe Type
    forall a. a -> Maybe a
    Just Type
    ty) (Maybe (ListMap TypeMap Type)
    m_elt Maybe (ListMap TypeMap Type)
    -> ListMap TypeMap Type -> ListMap TypeMap Type
    forall a. Maybe a -> a -> a
    `orElse` ListMap TypeMap Type
    forall a. ListMap TypeMap a
    forall (m :: * -> *) a. TrieMap m => m a
    emptyTM))

    umRnBndr2 :: UMEnv -> TyCoVar -> TyCoVar -> UMEnv
    umRnBndr2 :: UMEnv -> Var -> Var -> UMEnv
    umRnBndr2 UMEnv
    env Var
    v1 Var
    v2
      = UMEnv
    env { um_rn_env = rn_env', um_foralls = um_foralls env `extendVarSet` v' }
      where
        (RnEnv2
    rn_env', Var
    v') = RnEnv2 -> Var -> Var -> (RnEnv2, Var)
    rnBndr2_var (UMEnv -> RnEnv2
    um_rn_env UMEnv
    env) Var
    v1 Var
    v2

    mentionsForAllBoundTyVarsL, mentionsForAllBoundTyVarsR :: UMEnv -> VarSet -> Bool
    -- See (CU2) in Note [The Core unifier]
    mentionsForAllBoundTyVarsL :: UMEnv -> TyCoVarSet -> Bool
    mentionsForAllBoundTyVarsL = (RnEnv2 -> Var -> Bool) -> UMEnv -> TyCoVarSet -> Bool
    mentions_forall_bound_tvs RnEnv2 -> Var -> Bool
    inRnEnvL
    mentionsForAllBoundTyVarsR :: UMEnv -> TyCoVarSet -> Bool
    mentionsForAllBoundTyVarsR = (RnEnv2 -> Var -> Bool) -> UMEnv -> TyCoVarSet -> Bool
    mentions_forall_bound_tvs RnEnv2 -> Var -> Bool
    inRnEnvR

    mentions_forall_bound_tvs :: (RnEnv2 -> TyVar -> Bool) -> UMEnv -> VarSet -> Bool
    mentions_forall_bound_tvs :: (RnEnv2 -> Var -> Bool) -> UMEnv -> TyCoVarSet -> Bool
    mentions_forall_bound_tvs RnEnv2 -> Var -> Bool
    in_rn_env UMEnv
    env TyCoVarSet
    varset
      | TyCoVarSet -> Bool
    isEmptyVarSet (UMEnv -> TyCoVarSet
    um_foralls UMEnv
    env)               = Bool
    False
      | (Var -> Bool) -> TyCoVarSet -> Bool
    anyVarSet (RnEnv2 -> Var -> Bool
    in_rn_env (UMEnv -> RnEnv2
    um_rn_env UMEnv
    env)) TyCoVarSet
    varset = Bool
    True
      | Bool
    otherwise                                    = Bool
    False
        -- NB: That isEmptyVarSet guard is a critical optimization;
        -- it means we don't have to calculate the free vars of
        -- the type, often saving quite a bit of allocation.

    -- | Converts any SurelyApart to a MaybeApart
    don'tBeSoSure :: MaybeApartReason -> UM () -> UM ()
    don'tBeSoSure :: MaybeApartReason -> UM () -> UM ()
    don'tBeSoSure MaybeApartReason
    r UM ()
    um = (UMState -> UnifyResultM (UMState, ())) -> UM ()
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM ((UMState -> UnifyResultM (UMState, ())) -> UM ())
    -> (UMState -> UnifyResultM (UMState, ())) -> UM ()
    forall a b. (a -> b) -> a -> b
    $ \ UMState
    state ->
      case UM () -> UMState -> UnifyResultM (UMState, ())
    forall a. UM a -> UMState -> UnifyResultM (UMState, a)
    unUM UM ()
    um UMState
    state of
        UnifyResultM (UMState, ())
    SurelyApart -> MaybeApartReason -> (UMState, ()) -> UnifyResultM (UMState, ())
    forall a. MaybeApartReason -> a -> UnifyResultM a
    MaybeApart MaybeApartReason
    r (UMState
    state, ())
        UnifyResultM (UMState, ())
    other       -> UnifyResultM (UMState, ())
    other

    umRnOccL :: UMEnv -> TyVar -> TyVar
    umRnOccL :: UMEnv -> Var -> Var
    umRnOccL UMEnv
    env Var
    v = RnEnv2 -> Var -> Var
    rnOccL (UMEnv -> RnEnv2
    um_rn_env UMEnv
    env) Var
    v

    umRnOccR :: UMEnv -> TyVar -> TyVar
    umRnOccR :: UMEnv -> Var -> Var
    umRnOccR UMEnv
    env Var
    v = RnEnv2 -> Var -> Var
    rnOccR (UMEnv -> RnEnv2
    um_rn_env UMEnv
    env) Var
    v

    umSwapRn :: UMEnv -> UMEnv
    umSwapRn :: UMEnv -> UMEnv
    umSwapRn UMEnv
    env = UMEnv
    env { um_rn_env = rnSwap (um_rn_env env) }

    maybeApart :: MaybeApartReason -> UM ()
    maybeApart :: MaybeApartReason -> UM ()
    maybeApart MaybeApartReason
    r = (UMState -> UnifyResultM (UMState, ())) -> UM ()
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM (\UMState
    state -> MaybeApartReason -> (UMState, ()) -> UnifyResultM (UMState, ())
    forall a. MaybeApartReason -> a -> UnifyResultM a
    MaybeApart MaybeApartReason
    r (UMState
    state, ()))

    surelyApart :: UM a
    surelyApart :: forall a. UM a
    surelyApart = (UMState -> UnifyResultM (UMState, a)) -> UM a
    forall a. (UMState -> UnifyResultM (UMState, a)) -> UM a
    UM (\UMState
    _ -> UnifyResultM (UMState, a)
    forall a. UnifyResultM a
    SurelyApart)

    {-
    %************************************************************************
    %*                                                                      *
                Matching a (lifted) type against a coercion
    %*                                                                      *
    %************************************************************************

    This section defines essentially an inverse to liftCoSubst. It is defined
    here to avoid a dependency from Coercion on this module.

    -}

    data MatchEnv = ME { MatchEnv -> TyCoVarSet
    me_tmpls :: TyVarSet
                       , MatchEnv -> RnEnv2
    me_env   :: RnEnv2 }

    -- | 'liftCoMatch' is sort of inverse to 'liftCoSubst'.  In particular, if
    --   @liftCoMatch vars ty co == Just s@, then @liftCoSubst s ty == co@,
    --   where @==@ there means that the result of 'liftCoSubst' has the same
    --   type as the original co; but may be different under the hood.
    --   That is, it matches a type against a coercion of the same
    --   "shape", and returns a lifting substitution which could have been
    --   used to produce the given coercion from the given type.
    --   Note that this function is incomplete -- it might return Nothing
    --   when there does indeed exist a possible lifting context.
    --
    -- This function is incomplete in that it doesn't respect the equality
    -- in `eqType`. That is, it's possible that this will succeed for t1 and
    -- fail for t2, even when t1 `eqType` t2. That's because it depends on
    -- there being a very similar structure between the type and the coercion.
    -- This incompleteness shouldn't be all that surprising, especially because
    -- it depends on the structure of the coercion, which is a silly thing to do.
    --
    -- The lifting context produced doesn't have to be exacting in the roles
    -- of the mappings. This is because any use of the lifting context will
    -- also require a desired role. Thus, this algorithm prefers mapping to
    -- nominal coercions where it can do so.
    liftCoMatch :: TyCoVarSet -> Type -> Coercion -> Maybe LiftingContext
    liftCoMatch :: TyCoVarSet -> Type -> OutCoercion -> Maybe LiftingContext
    liftCoMatch TyCoVarSet
    tmpls Type
    ty OutCoercion
    co
      = do { cenv1 <- MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    forall a. VarEnv a
    emptyVarEnv Type
    ki OutCoercion
    ki_co OutCoercion
    ki_ki_co OutCoercion
    ki_ki_co
           ; cenv2 <- ty_co_match menv cenv1       ty co
                                  (mkNomReflCo co_lkind) (mkNomReflCo co_rkind)
           ; return (LC (mkEmptySubst in_scope) cenv2) }
      where
        menv :: MatchEnv
    menv     = ME { me_tmpls :: TyCoVarSet
    me_tmpls = TyCoVarSet
    tmpls, me_env :: RnEnv2
    me_env = InScopeSet -> RnEnv2
    mkRnEnv2 InScopeSet
    in_scope }
        in_scope :: InScopeSet
    in_scope = TyCoVarSet -> InScopeSet
    mkInScopeSet (TyCoVarSet
    tmpls TyCoVarSet -> TyCoVarSet -> TyCoVarSet
    `unionVarSet` OutCoercion -> TyCoVarSet
    tyCoVarsOfCo OutCoercion
    co)
        -- Like tcMatchTy, assume all the interesting variables
        -- in ty are in tmpls

        ki :: Type
    ki       = HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    ty
        ki_co :: OutCoercion
    ki_co    = HasDebugCallStack => OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion
    promoteCoercion OutCoercion
    co
        ki_ki_co :: OutCoercion
    ki_ki_co = Type -> OutCoercion
    mkNomReflCo Type
    liftedTypeKind

        Pair Type
    co_lkind Type
    co_rkind = HasDebugCallStack => OutCoercion -> Pair Type
    OutCoercion -> Pair Type
    coercionKind OutCoercion
    ki_co

    -- | 'ty_co_match' does all the actual work for 'liftCoMatch'.
    ty_co_match :: MatchEnv   -- ^ ambient helpful info
                -> LiftCoEnv  -- ^ incoming subst
                -> Type       -- ^ ty, type to match
                -> Coercion   -- ^ co :: lty ~r rty, coercion to match against
                -> Coercion   -- ^ :: kind(lsubst(ty)) ~N kind(lty)
                -> Coercion   -- ^ :: kind(rsubst(ty)) ~N kind(rty)
                -> Maybe LiftCoEnv
       -- ^ Just env ==> liftCoSubst Nominal env ty == co, modulo roles.
       -- Also: Just env ==> lsubst(ty) == lty and rsubst(ty) == rty,
       -- where lsubst = lcSubstLeft(env) and rsubst = lcSubstRight(env)
    ty_co_match :: MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty OutCoercion
    co OutCoercion
    lkco OutCoercion
    rkco
      | Just Type
    ty' <- Type -> Maybe Type
    coreView Type
    ty = MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty' OutCoercion
    co OutCoercion
    lkco OutCoercion
    rkco

      -- handle Refl case:
      | Type -> TyCoVarSet
    tyCoVarsOfType Type
    ty TyCoVarSet -> CvSubstEnv -> Bool
    forall a. TyCoVarSet -> VarEnv a -> Bool
    `isNotInDomainOf` CvSubstEnv
    subst
      , Just (Type
    ty', Role
    _) <- OutCoercion -> Maybe (Type, Role)
    isReflCo_maybe OutCoercion
    co
      , Type
    ty HasCallStack => Type -> Type -> Bool
    Type -> Type -> Bool
    `eqType` Type
    ty'
        -- Why `eqType` and not `tcEqType`? Because this function is only used
        -- during coercion optimisation, after type-checking has finished.
      = CvSubstEnv -> Maybe CvSubstEnv
    forall a. a -> Maybe a
    Just CvSubstEnv
    subst

      where
        isNotInDomainOf :: VarSet -> VarEnv a -> Bool
        isNotInDomainOf :: forall a. TyCoVarSet -> VarEnv a -> Bool
    isNotInDomainOf TyCoVarSet
    set VarEnv a
    env
          = (Var -> Bool) -> TyCoVarSet -> Bool
    noneSet (\Var
    v -> Var -> VarEnv a -> Bool
    forall a. Var -> VarEnv a -> Bool
    elemVarEnv Var
    v VarEnv a
    env) TyCoVarSet
    set

        noneSet :: (Var -> Bool) -> VarSet -> Bool
        noneSet :: (Var -> Bool) -> TyCoVarSet -> Bool
    noneSet Var -> Bool
    f = (Var -> Bool) -> TyCoVarSet -> Bool
    allVarSet (Bool -> Bool
    not (Bool -> Bool) -> (Var -> Bool) -> Var -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Var -> Bool
    f)

    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty OutCoercion
    co OutCoercion
    lkco OutCoercion
    rkco
      | CastTy Type
    ty' OutCoercion
    co' <- Type
    ty
         -- See Note [Matching in the presence of casts (1)]
      = let empty_subst :: Subst
    empty_subst  = InScopeSet -> Subst
    mkEmptySubst (RnEnv2 -> InScopeSet
    rnInScopeSet (MatchEnv -> RnEnv2
    me_env MatchEnv
    menv))
            substed_co_l :: OutCoercion
    substed_co_l = HasDebugCallStack => Subst -> OutCoercion -> OutCoercion
    Subst -> OutCoercion -> OutCoercion
    substCo (Subst -> CvSubstEnv -> Subst
    liftEnvSubstLeft Subst
    empty_subst CvSubstEnv
    subst)  OutCoercion
    co'
            substed_co_r :: OutCoercion
    substed_co_r = HasDebugCallStack => Subst -> OutCoercion -> OutCoercion
    Subst -> OutCoercion -> OutCoercion
    substCo (Subst -> CvSubstEnv -> Subst
    liftEnvSubstRight Subst
    empty_subst CvSubstEnv
    subst) OutCoercion
    co'
        in
        MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty' OutCoercion
    co (OutCoercion
    substed_co_l HasDebugCallStack => OutCoercion -> OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion -> OutCoercion
    `mkTransCo` OutCoercion
    lkco)
                                      (OutCoercion
    substed_co_r HasDebugCallStack => OutCoercion -> OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion -> OutCoercion
    `mkTransCo` OutCoercion
    rkco)

      | SymCo OutCoercion
    co' <- OutCoercion
    co
      = CvSubstEnv -> CvSubstEnv
    swapLiftCoEnv (CvSubstEnv -> CvSubstEnv) -> Maybe CvSubstEnv -> Maybe CvSubstEnv
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv (CvSubstEnv -> CvSubstEnv
    swapLiftCoEnv CvSubstEnv
    subst) Type
    ty OutCoercion
    co' OutCoercion
    rkco OutCoercion
    lkco

      -- Match a type variable against a non-refl coercion
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst (TyVarTy Var
    tv1) OutCoercion
    co OutCoercion
    lkco OutCoercion
    rkco
      | Just OutCoercion
    co1' <- CvSubstEnv -> Var -> Maybe OutCoercion
    forall a. VarEnv a -> Var -> Maybe a
    lookupVarEnv CvSubstEnv
    subst Var
    tv1' -- tv1' is already bound to co1
      = if RnEnv2 -> OutCoercion -> OutCoercion -> Bool
    eqCoercionX (RnEnv2 -> RnEnv2
    nukeRnEnvL RnEnv2
    rn_env) OutCoercion
    co1' OutCoercion
    co
        then CvSubstEnv -> Maybe CvSubstEnv
    forall a. a -> Maybe a
    Just CvSubstEnv
    subst
        else Maybe CvSubstEnv
    forall a. Maybe a
    Nothing       -- no match since tv1 matches two different coercions

      | Var
    tv1' Var -> TyCoVarSet -> Bool
    `elemVarSet` MatchEnv -> TyCoVarSet
    me_tmpls MatchEnv
    menv           -- tv1' is a template var
      = if (Var -> Bool) -> [Var] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any (RnEnv2 -> Var -> Bool
    inRnEnvR RnEnv2
    rn_env) (OutCoercion -> [Var]
    tyCoVarsOfCoList OutCoercion
    co)
        then Maybe CvSubstEnv
    forall a. Maybe a
    Nothing      -- occurs check failed
        else CvSubstEnv -> Maybe CvSubstEnv
    forall a. a -> Maybe a
    Just (CvSubstEnv -> Maybe CvSubstEnv) -> CvSubstEnv -> Maybe CvSubstEnv
    forall a b. (a -> b) -> a -> b
    $ CvSubstEnv -> Var -> OutCoercion -> CvSubstEnv
    forall a. VarEnv a -> Var -> a -> VarEnv a
    extendVarEnv CvSubstEnv
    subst Var
    tv1' (OutCoercion -> CvSubstEnv) -> OutCoercion -> CvSubstEnv
    forall a b. (a -> b) -> a -> b
    $
                    OutCoercion -> OutCoercion -> OutCoercion -> OutCoercion
    castCoercionKind OutCoercion
    co (OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    lkco) (OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    rkco)

      | Bool
    otherwise
      = Maybe CvSubstEnv
    forall a. Maybe a
    Nothing

      where
        rn_env :: RnEnv2
    rn_env = MatchEnv -> RnEnv2
    me_env MatchEnv
    menv
        tv1' :: Var
    tv1' = RnEnv2 -> Var -> Var
    rnOccL RnEnv2
    rn_env Var
    tv1

      -- just look through SubCo's. We don't really care about roles here.
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty (SubCo OutCoercion
    co) OutCoercion
    lkco OutCoercion
    rkco
      = MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty OutCoercion
    co OutCoercion
    lkco OutCoercion
    rkco

    ty_co_match MatchEnv
    menv CvSubstEnv
    subst (AppTy Type
    ty1a Type
    ty1b) OutCoercion
    co OutCoercion
    _lkco OutCoercion
    _rkco
      | Just (OutCoercion
    co2, OutCoercion
    arg2) <- OutCoercion -> Maybe (OutCoercion, OutCoercion)
    splitAppCo_maybe OutCoercion
    co     -- c.f. Unify.match on AppTy
      = MatchEnv
    -> CvSubstEnv
    -> Type
    -> [Type]
    -> OutCoercion
    -> [OutCoercion]
    -> Maybe CvSubstEnv
    ty_co_match_app MatchEnv
    menv CvSubstEnv
    subst Type
    ty1a [Type
    ty1b] OutCoercion
    co2 [OutCoercion
    arg2]
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty1 (AppCo OutCoercion
    co2 OutCoercion
    arg2) OutCoercion
    _lkco OutCoercion
    _rkco
      | Just (Type
    ty1a, Type
    ty1b) <- HasDebugCallStack => Type -> Maybe (Type, Type)
    Type -> Maybe (Type, Type)
    splitAppTyNoView_maybe Type
    ty1
           -- yes, the one from Type, not TcType; this is for coercion optimization
      = MatchEnv
    -> CvSubstEnv
    -> Type
    -> [Type]
    -> OutCoercion
    -> [OutCoercion]
    -> Maybe CvSubstEnv
    ty_co_match_app MatchEnv
    menv CvSubstEnv
    subst Type
    ty1a [Type
    ty1b] OutCoercion
    co2 [OutCoercion
    arg2]

    ty_co_match MatchEnv
    menv CvSubstEnv
    subst (TyConApp TyCon
    tc1 [Type]
    tys) (TyConAppCo Role
    _ TyCon
    tc2 [OutCoercion]
    cos) OutCoercion
    _lkco OutCoercion
    _rkco
      = MatchEnv
    -> CvSubstEnv
    -> TyCon
    -> [Type]
    -> TyCon
    -> [OutCoercion]
    -> Maybe CvSubstEnv
    ty_co_match_tc MatchEnv
    menv CvSubstEnv
    subst TyCon
    tc1 [Type]
    tys TyCon
    tc2 [OutCoercion]
    cos

    ty_co_match MatchEnv
    menv CvSubstEnv
    subst (FunTy { ft_mult :: Type -> Type
    ft_mult = Type
    w, ft_arg :: Type -> Type
    ft_arg = Type
    ty1, ft_res :: Type -> Type
    ft_res = Type
    ty2 })
                (FunCo { fco_mult :: OutCoercion -> OutCoercion
    fco_mult = OutCoercion
    co_w, fco_arg :: OutCoercion -> OutCoercion
    fco_arg = OutCoercion
    co1, fco_res :: OutCoercion -> OutCoercion
    fco_res = OutCoercion
    co2 }) OutCoercion
    _lkco OutCoercion
    _rkco
      = MatchEnv
    -> CvSubstEnv -> [Type] -> [OutCoercion] -> Maybe CvSubstEnv
    ty_co_match_args MatchEnv
    menv CvSubstEnv
    subst [Type
    w,    Type
    rep1,    Type
    rep2,    Type
    ty1, Type
    ty2]
                                    [OutCoercion
    co_w, OutCoercion
    co1_rep, OutCoercion
    co2_rep, OutCoercion
    co1, OutCoercion
    co2]
      where
         rep1 :: Type
    rep1    = HasDebugCallStack => Type -> Type
    Type -> Type
    getRuntimeRep Type
    ty1
         rep2 :: Type
    rep2    = HasDebugCallStack => Type -> Type
    Type -> Type
    getRuntimeRep Type
    ty2
         co1_rep :: OutCoercion
    co1_rep = HasDebugCallStack => OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion
    mkRuntimeRepCo OutCoercion
    co1
         co2_rep :: OutCoercion
    co2_rep = HasDebugCallStack => OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion
    mkRuntimeRepCo OutCoercion
    co2
        -- NB: we include the RuntimeRep arguments in the matching;
        --     not doing so caused #21205.

    ty_co_match MatchEnv
    menv CvSubstEnv
    subst (ForAllTy (Bndr Var
    tv1 ForAllTyFlag
    vis1t) Type
    ty1)
                           (ForAllCo Var
    tv2 ForAllTyFlag
    vis1c ForAllTyFlag
    vis2c OutCoercion
    kind_co2 OutCoercion
    co2)
                           OutCoercion
    lkco OutCoercion
    rkco
      | Var -> Bool
    isTyVar Var
    tv1 Bool -> Bool -> Bool
    && Var -> Bool
    isTyVar Var
    tv2
      , ForAllTyFlag
    vis1t ForAllTyFlag -> ForAllTyFlag -> Bool
    forall a. Eq a => a -> a -> Bool
    == ForAllTyFlag
    vis1c Bool -> Bool -> Bool
    && ForAllTyFlag
    vis1c ForAllTyFlag -> ForAllTyFlag -> Bool
    forall a. Eq a => a -> a -> Bool
    == ForAllTyFlag
    vis2c -- Is this necessary?
          -- Is this visibility check necessary?  @rae says: yes, I think the
          -- check is necessary, if we're caring about visibility (and we are).
          -- But ty_co_match is a dark and not important corner.
      = do { subst1 <- MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst (Var -> Type
    tyVarKind Var
    tv1) OutCoercion
    kind_co2
                                   OutCoercion
    ki_ki_co OutCoercion
    ki_ki_co
           ; let rn_env0 = MatchEnv -> RnEnv2
    me_env MatchEnv
    menv
                 rn_env1 = RnEnv2 -> Var -> Var -> RnEnv2
    rnBndr2 RnEnv2
    rn_env0 Var
    tv1 Var
    tv2
                 menv'   = MatchEnv
    menv { me_env = rn_env1 }
           ; ty_co_match menv' subst1 ty1 co2 lkco rkco }
      where
        ki_ki_co :: OutCoercion
    ki_ki_co = Type -> OutCoercion
    mkNomReflCo Type
    liftedTypeKind

    -- ty_co_match menv subst (ForAllTy (Bndr cv1 _) ty1)
    --                        (ForAllCo cv2 kind_co2 co2)
    --                        lkco rkco
    --   | isCoVar cv1 && isCoVar cv2
    --   We seems not to have enough information for this case
    --   1. Given:
    --        cv1      :: (s1 :: k1) ~r (s2 :: k2)
    --        kind_co2 :: (s1' ~ s2') ~N (t1 ~ t2)
    --        eta1      = mkSelCo (SelTyCon 2 role) (downgradeRole r Nominal kind_co2)
    --                 :: s1' ~ t1
    --        eta2      = mkSelCo (SelTyCon 3 role) (downgradeRole r Nominal kind_co2)
    --                 :: s2' ~ t2
    --      Wanted:
    --        subst1 <- ty_co_match menv subst  s1 eta1 kco1 kco2
    --        subst2 <- ty_co_match menv subst1 s2 eta2 kco3 kco4
    --      Question: How do we get kcoi?
    --   2. Given:
    --        lkco :: <*>    -- See Note [Weird typing rule for ForAllTy] in GHC.Core.TyCo.Rep
    --        rkco :: <*>
    --      Wanted:
    --        ty_co_match menv' subst2 ty1 co2 lkco' rkco'
    --      Question: How do we get lkco' and rkco'?

    ty_co_match MatchEnv
    _ CvSubstEnv
    subst (CoercionTy {}) OutCoercion
    _ OutCoercion
    _ OutCoercion
    _
      = CvSubstEnv -> Maybe CvSubstEnv
    forall a. a -> Maybe a
    Just CvSubstEnv
    subst -- don't inspect coercions

    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty (GRefl Role
    r Type
    t (MCo OutCoercion
    co)) OutCoercion
    lkco OutCoercion
    rkco
      =  MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty (Role -> Type -> MCoercion -> OutCoercion
    GRefl Role
    r Type
    t MCoercion
    MRefl) OutCoercion
    lkco (OutCoercion
    rkco HasDebugCallStack => OutCoercion -> OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion -> OutCoercion
    `mkTransCo` OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    co)

    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty OutCoercion
    co1 OutCoercion
    lkco OutCoercion
    rkco
      | Just (CastTy Type
    t OutCoercion
    co, Role
    r) <- OutCoercion -> Maybe (Type, Role)
    isReflCo_maybe OutCoercion
    co1
      -- In @pushRefl@, pushing reflexive coercion inside CastTy will give us
      -- t |> co ~ t ; <t> ; t ~ t |> co
      -- But transitive coercions are not helpful. Therefore we deal
      -- with it here: we do recursion on the smaller reflexive coercion,
      -- while propagating the correct kind coercions.
      = let kco' :: OutCoercion
    kco' = OutCoercion -> OutCoercion
    mkSymCo OutCoercion
    co
        in MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty (Role -> Type -> OutCoercion
    mkReflCo Role
    r Type
    t) (OutCoercion
    lkco HasDebugCallStack => OutCoercion -> OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion -> OutCoercion
    `mkTransCo` OutCoercion
    kco')
                                                    (OutCoercion
    rkco HasDebugCallStack => OutCoercion -> OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion -> OutCoercion
    `mkTransCo` OutCoercion
    kco')

    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty OutCoercion
    co OutCoercion
    lkco OutCoercion
    rkco
      | Just OutCoercion
    co' <- OutCoercion -> Maybe OutCoercion
    pushRefl OutCoercion
    co = MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty OutCoercion
    co' OutCoercion
    lkco OutCoercion
    rkco
      | Bool
    otherwise               = Maybe CvSubstEnv
    forall a. Maybe a
    Nothing

    ty_co_match_tc :: MatchEnv -> LiftCoEnv
                   -> TyCon -> [Type]
                   -> TyCon -> [Coercion]
                   -> Maybe LiftCoEnv
    ty_co_match_tc :: MatchEnv
    -> CvSubstEnv
    -> TyCon
    -> [Type]
    -> TyCon
    -> [OutCoercion]
    -> Maybe CvSubstEnv
    ty_co_match_tc MatchEnv
    menv CvSubstEnv
    subst TyCon
    tc1 [Type]
    tys1 TyCon
    tc2 [OutCoercion]
    cos2
      = do { Bool -> Maybe ()
    forall (f :: * -> *). Alternative f => Bool -> f ()
    guard (TyCon
    tc1 TyCon -> TyCon -> Bool
    forall a. Eq a => a -> a -> Bool
    == TyCon
    tc2)
           ; MatchEnv
    -> CvSubstEnv -> [Type] -> [OutCoercion] -> Maybe CvSubstEnv
    ty_co_match_args MatchEnv
    menv CvSubstEnv
    subst [Type]
    tys1 [OutCoercion]
    cos2 }

    ty_co_match_app :: MatchEnv -> LiftCoEnv
                    -> Type -> [Type] -> Coercion -> [Coercion]
                    -> Maybe LiftCoEnv
    ty_co_match_app :: MatchEnv
    -> CvSubstEnv
    -> Type
    -> [Type]
    -> OutCoercion
    -> [OutCoercion]
    -> Maybe CvSubstEnv
    ty_co_match_app MatchEnv
    menv CvSubstEnv
    subst Type
    ty1 [Type]
    ty1args OutCoercion
    co2 [OutCoercion]
    co2args
      | Just (Type
    ty1', Type
    ty1a) <- HasDebugCallStack => Type -> Maybe (Type, Type)
    Type -> Maybe (Type, Type)
    splitAppTyNoView_maybe Type
    ty1
      , Just (OutCoercion
    co2', OutCoercion
    co2a) <- OutCoercion -> Maybe (OutCoercion, OutCoercion)
    splitAppCo_maybe OutCoercion
    co2
      = MatchEnv
    -> CvSubstEnv
    -> Type
    -> [Type]
    -> OutCoercion
    -> [OutCoercion]
    -> Maybe CvSubstEnv
    ty_co_match_app MatchEnv
    menv CvSubstEnv
    subst Type
    ty1' (Type
    ty1a Type -> [Type] -> [Type]
    forall a. a -> [a] -> [a]
    : [Type]
    ty1args) OutCoercion
    co2' (OutCoercion
    co2a OutCoercion -> [OutCoercion] -> [OutCoercion]
    forall a. a -> [a] -> [a]
    : [OutCoercion]
    co2args)

      | Bool
    otherwise
      = do { subst1 <- MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ki1 OutCoercion
    ki2 OutCoercion
    ki_ki_co OutCoercion
    ki_ki_co
           ; let Pair lkco rkco = mkNomReflCo <$> coercionKind ki2
           ; subst2 <- ty_co_match menv subst1 ty1 co2 lkco rkco
           ; ty_co_match_args menv subst2 ty1args co2args }
      where
        ki1 :: Type
    ki1 = HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    ty1
        ki2 :: OutCoercion
    ki2 = HasDebugCallStack => OutCoercion -> OutCoercion
    OutCoercion -> OutCoercion
    promoteCoercion OutCoercion
    co2
        ki_ki_co :: OutCoercion
    ki_ki_co = Type -> OutCoercion
    mkNomReflCo Type
    liftedTypeKind

    ty_co_match_args :: MatchEnv -> LiftCoEnv -> [Type] -> [Coercion]
                     -> Maybe LiftCoEnv
    ty_co_match_args :: MatchEnv
    -> CvSubstEnv -> [Type] -> [OutCoercion] -> Maybe CvSubstEnv
    ty_co_match_args MatchEnv
    menv CvSubstEnv
    subst (Type
    ty:[Type]
    tys) (OutCoercion
    arg:[OutCoercion]
    args)
      = do { let Pair Type
    lty Type
    rty = HasDebugCallStack => OutCoercion -> Pair Type
    OutCoercion -> Pair Type
    coercionKind OutCoercion
    arg
                 lkco :: OutCoercion
    lkco = Type -> OutCoercion
    mkNomReflCo (HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    lty)
                 rkco :: OutCoercion
    rkco = Type -> OutCoercion
    mkNomReflCo (HasDebugCallStack => Type -> Type
    Type -> Type
    typeKind Type
    rty)
           ; subst' <- MatchEnv
    -> CvSubstEnv
    -> Type
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> Maybe CvSubstEnv
    ty_co_match MatchEnv
    menv CvSubstEnv
    subst Type
    ty OutCoercion
    arg OutCoercion
    lkco OutCoercion
    rkco
           ; ty_co_match_args menv subst' tys args }
    ty_co_match_args MatchEnv
    _    CvSubstEnv
    subst []       [] = CvSubstEnv -> Maybe CvSubstEnv
    forall a. a -> Maybe a
    Just CvSubstEnv
    subst
    ty_co_match_args MatchEnv
    _    CvSubstEnv
    _     [Type]
    _        [OutCoercion]
    _  = Maybe CvSubstEnv
    forall a. Maybe a
    Nothing

    pushRefl :: Coercion -> Maybe Coercion
    pushRefl :: OutCoercion -> Maybe OutCoercion
    pushRefl OutCoercion
    co =
      case (OutCoercion -> Maybe (Type, Role)
    isReflCo_maybe OutCoercion
    co) of
        Just (AppTy Type
    ty1 Type
    ty2, Role
    Nominal)
          -> OutCoercion -> Maybe OutCoercion
    forall a. a -> Maybe a
    Just (OutCoercion -> OutCoercion -> OutCoercion
    AppCo (Role -> Type -> OutCoercion
    mkReflCo Role
    Nominal Type
    ty1) (Type -> OutCoercion
    mkNomReflCo Type
    ty2))
        Just (FunTy FunTyFlag
    af Type
    w Type
    ty1 Type
    ty2, Role
    r)
          ->  OutCoercion -> Maybe OutCoercion
    forall a. a -> Maybe a
    Just (Role
    -> FunTyFlag
    -> FunTyFlag
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    -> OutCoercion
    FunCo Role
    r FunTyFlag
    af FunTyFlag
    af (Role -> Type -> OutCoercion
    mkReflCo Role
    r Type
    w) (Role -> Type -> OutCoercion
    mkReflCo Role
    r Type
    ty1) (Role -> Type -> OutCoercion
    mkReflCo Role
    r Type
    ty2))
        Just (TyConApp TyCon
    tc [Type]
    tys, Role
    r)
          -> OutCoercion -> Maybe OutCoercion
    forall a. a -> Maybe a
    Just (Role -> TyCon -> [OutCoercion] -> OutCoercion
    TyConAppCo Role
    r TyCon
    tc ((Role -> Type -> OutCoercion) -> [Role] -> [Type] -> [OutCoercion]
    forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
    zipWith Role -> Type -> OutCoercion
    mkReflCo (Role -> TyCon -> [Role]
    tyConRoleListX Role
    r TyCon
    tc) [Type]
    tys))
        Just (ForAllTy (Bndr Var
    tv ForAllTyFlag
    vis) Type
    ty, Role
    r)
          -> OutCoercion -> Maybe OutCoercion
    forall a. a -> Maybe a
    Just (ForAllCo { fco_tcv :: Var
    fco_tcv = Var
    tv, fco_visL :: ForAllTyFlag
    fco_visL = ForAllTyFlag
    vis, fco_visR :: ForAllTyFlag
    fco_visR = ForAllTyFlag
    vis
                            , fco_kind :: OutCoercion
    fco_kind = Type -> OutCoercion
    mkNomReflCo (Var -> Type
    varType Var
    tv)
                            , fco_body :: OutCoercion
    fco_body = Role -> Type -> OutCoercion
    mkReflCo Role
    r Type
    ty })
        Maybe (Type, Role)
    _ -> Maybe OutCoercion
    forall a. Maybe a
    Nothing