-- Mathlib/RingTheory/Flat/FaithfullyFlat/Basic.lean
lemma rTensor_reflects_exact [fl : FaithfullyFlat R M]
    (ex : Function.Exact (l12.rTensor M) (l23.rTensor M)) :
    Function.Exact l12 l23 := LinearMap.exact_iff.2 <| by
  have complex : LinearMap.range l12 ≤ LinearMap.ker l23 := range_le_ker_of_exact_rTensor R M _ _ ex
  -- By the previous lemma we have that range l12 ≤ ker l23 and hence the quotient
  -- H := ker l23 ⧸ range l12 makes sense.
  -- Hence our goal ker l23 = range l12 follows from the claim that H = 0.
  let H := LinearMap.ker l23 ⧸ LinearMap.range (Submodule.inclusion complex)
  suffices triv_coh : Subsingleton H by
    rw [Submodule.subsingleton_quotient_iff_eq_top, Submodule.range_inclusion,
      Submodule.comap_subtype_eq_top] at triv_coh
    exact le_antisymm triv_coh complex

  -- Since `M` is faithfully flat, we need only to show that `H ⊗ M` is trivial.
  suffices Subsingleton (H ⊗[R] M) from rTensor_reflects_triviality R M H
  let e : H ⊗[R] M ≃ₗ[R] _ := TensorProduct.quotientTensorEquiv _ _
  -- Note that `H ⊗ M` is isomorphic to `ker l12 ⊗ M ⧸ range ((range l12 ⊗ M) -> (ker l23 ⊗ M))`.
  -- So the problem is reduced to proving surjectivity of `range l12 ⊗ M → ker l23 ⊗ M`.
  rw [e.toEquiv.subsingleton_congr, Submodule.subsingleton_quotient_iff_eq_top,
    LinearMap.range_eq_top]
  intro x
  induction x using TensorProduct.induction_on with
  | zero => exact ⟨0, by simp⟩
  -- let `x ⊗ m` be an element in `ker l23 ⊗ M`, then `x ⊗ m` is in the kernel of `l23 ⊗ 𝟙M`.
  -- Since `N1 ⊗ M -l12 ⊗ M-> N2 ⊗ M -l23 ⊗ M-> N3 ⊗ M` is exact, we have that `x ⊗ m` is in
  -- the range of `l12 ⊗ 𝟙M`, i.e. `x ⊗ m = (l12 ⊗ 𝟙M) y` for some `y ∈ N1 ⊗ M` as elements of
  -- `N2 ⊗ M`. We need to prove that `x ⊗ m = (l12 ⊗ 𝟙M) y` still holds in `(ker l23) ⊗ M`.
  -- This is okay because `M` is flat and `ker l23 -> N2` is injective.
  | tmul x m =>
    rcases x with ⟨x, (hx : l23 x = 0)⟩
    have mem : x ⊗ₜ[R] m ∈ LinearMap.ker (l23.rTensor M)

Proof state:
case tmul.mk.intro
R : Type u
M : Type v
inst✝⁸ : CommRing R
inst✝⁷ : AddCommGroup M
inst✝⁶ : Module R M
N1 : Type u_1
inst✝⁵ : AddCommGroup N1
inst✝⁴ : Module R N1
N2 : Type u_2
inst✝³ : AddCommGroup N2
inst✝² : Module R N2
N3 : Type u_3
inst✝¹ : AddCommGroup N3
inst✝ : Module R N3
l12 : N1 →ₗ[R] N2
l23 : N2 →ₗ[R] N3
fl : FaithfullyFlat R M
ex : Function.Exact ⇑(LinearMap.rTensor M l12) ⇑(LinearMap.rTensor M l23)
complex : LinearMap.range l12 ≤ LinearMap.ker l23
H : Type u_2 := ↥(LinearMap.ker l23) ⧸ LinearMap.range (Submodule.inclusion complex)
e : H ⊗[R] M ≃ₗ[R]
  ↥(LinearMap.ker l23) ⊗[R] M ⧸
    LinearMap.range (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) :=
  quotientTensorEquiv M (LinearMap.range (Submodule.inclusion complex))
m : M
x : N2
hx : l23 x = 0
y : N1 ⊗[R] M
hy : (LinearMap.rTensor M l12) y = x ⊗ₜ[R] m
⊢ ∃ a, (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) a = ⟨x, hx⟩ ⊗ₜ[R] m

Tactic:
refine ⟨LinearMap.rTensor M (LinearMap.rangeRestrict _ ∘ₗ LinearMap.rangeRestrict l12) y,
  Module.Flat.rTensor_preserves_injective_linearMap (LinearMap.ker l23).subtype
  Subtype.val_injective ?_⟩

Result:
case tmul.mk.intro
R : Type u
M : Type v
inst✝⁸ : CommRing R
inst✝⁷ : AddCommGroup M
inst✝⁶ : Module R M
N1 : Type u_1
inst✝⁵ : AddCommGroup N1
inst✝⁴ : Module R N1
N2 : Type u_2
inst✝³ : AddCommGroup N2
inst✝² : Module R N2
N3 : Type u_3
inst✝¹ : AddCommGroup N3
inst✝ : Module R N3
l12 : N1 →ₗ[R] N2
l23 : N2 →ₗ[R] N3
fl : FaithfullyFlat R M
ex : Function.Exact ⇑(LinearMap.rTensor M l12) ⇑(LinearMap.rTensor M l23)
complex : LinearMap.range l12 ≤ LinearMap.ker l23
H : Type u_2 := ↥(LinearMap.ker l23) ⧸ LinearMap.range (Submodule.inclusion complex)
e : H ⊗[R] M ≃ₗ[R]
  ↥(LinearMap.ker l23) ⊗[R] M ⧸
    LinearMap.range (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) :=
  quotientTensorEquiv M (LinearMap.range (Submodule.inclusion complex))
m : M
x : N2
hx : l23 x = 0
y : N1 ⊗[R] M
hy : (LinearMap.rTensor M l12) y = x ⊗ₜ[R] m
⊢ (LinearMap.rTensor M (LinearMap.ker l23).subtype)
      ((map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id)
        ((LinearMap.rTensor M ((Submodule.inclusion complex).rangeRestrict ∘ₗ l12.rangeRestrict)) y)) =
    (LinearMap.rTensor M (LinearMap.ker l23).subtype) (⟨x, hx⟩ ⊗ₜ[R] m)

Proof state:
case tmul
R : Type u
M : Type v
inst✝⁸ : CommRing R
inst✝⁷ : AddCommGroup M
inst✝⁶ : Module R M
N1 : Type u_1
inst✝⁵ : AddCommGroup N1
inst✝⁴ : Module R N1
N2 : Type u_2
inst✝³ : AddCommGroup N2
inst✝² : Module R N2
N3 : Type u_3
inst✝¹ : AddCommGroup N3
inst✝ : Module R N3
l12 : N1 →ₗ[R] N2
l23 : N2 →ₗ[R] N3
fl : FaithfullyFlat R M
ex : Function.Exact ⇑(LinearMap.rTensor M l12) ⇑(LinearMap.rTensor M l23)
complex : LinearMap.range l12 ≤ LinearMap.ker l23
H : Type u_2 := ↥(LinearMap.ker l23) ⧸ LinearMap.range (Submodule.inclusion complex)
e : H ⊗[R] M ≃ₗ[R]
  ↥(LinearMap.ker l23) ⊗[R] M ⧸
    LinearMap.range (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) :=
  quotientTensorEquiv M (LinearMap.range (Submodule.inclusion complex))
x : ↥(LinearMap.ker l23)
m : M
⊢ ∃ a, (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) a = x ⊗ₜ[R] m

Tactic:
rcases x with ⟨x, (hx : l23 x = 0)⟩

Result:
case tmul.mk
R : Type u
M : Type v
inst✝⁸ : CommRing R
inst✝⁷ : AddCommGroup M
inst✝⁶ : Module R M
N1 : Type u_1
inst✝⁵ : AddCommGroup N1
inst✝⁴ : Module R N1
N2 : Type u_2
inst✝³ : AddCommGroup N2
inst✝² : Module R N2
N3 : Type u_3
inst✝¹ : AddCommGroup N3
inst✝ : Module R N3
l12 : N1 →ₗ[R] N2
l23 : N2 →ₗ[R] N3
fl : FaithfullyFlat R M
ex : Function.Exact ⇑(LinearMap.rTensor M l12) ⇑(LinearMap.rTensor M l23)
complex : LinearMap.range l12 ≤ LinearMap.ker l23
H : Type u_2 := ↥(LinearMap.ker l23) ⧸ LinearMap.range (Submodule.inclusion complex)
e : H ⊗[R] M ≃ₗ[R]
  ↥(LinearMap.ker l23) ⊗[R] M ⧸
    LinearMap.range (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) :=
  quotientTensorEquiv M (LinearMap.range (Submodule.inclusion complex))
m : M
x : N2
hx : l23 x = 0
⊢ ∃ a, (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) a = ⟨x, hx⟩ ⊗ₜ[R] m

Proof state:
case tmul.mk.intro
R : Type u
M : Type v
inst✝⁸ : CommRing R
inst✝⁷ : AddCommGroup M
inst✝⁶ : Module R M
N1 : Type u_1
inst✝⁵ : AddCommGroup N1
inst✝⁴ : Module R N1
N2 : Type u_2
inst✝³ : AddCommGroup N2
inst✝² : Module R N2
N3 : Type u_3
inst✝¹ : AddCommGroup N3
inst✝ : Module R N3
l12 : N1 →ₗ[R] N2
l23 : N2 →ₗ[R] N3
fl : FaithfullyFlat R M
ex : Function.Exact ⇑(LinearMap.rTensor M l12) ⇑(LinearMap.rTensor M l23)
complex : LinearMap.range l12 ≤ LinearMap.ker l23
H : Type u_2 := ↥(LinearMap.ker l23) ⧸ LinearMap.range (Submodule.inclusion complex)
e : H ⊗[R] M ≃ₗ[R]
  ↥(LinearMap.ker l23) ⊗[R] M ⧸
    LinearMap.range (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) :=
  quotientTensorEquiv M (LinearMap.range (Submodule.inclusion complex))
m : M
x : N2
hx : l23 x = 0
y : N1 ⊗[R] M
hy : (LinearMap.rTensor M l12) y = x ⊗ₜ[R] m
⊢ (LinearMap.rTensor M ((LinearMap.ker l23).subtype ∘ₗ (LinearMap.range (Submodule.inclusion complex)).subtype))
      ((LinearMap.rTensor M
          (LinearMap.codRestrict (LinearMap.range (Submodule.inclusion complex))
            (Submodule.inclusion complex ∘ₗ l12.rangeRestrict) ⋯))
        y) =
    (LinearMap.rTensor M l12) y

Tactic:
rw [← LinearMap.comp_apply, ← LinearMap.rTensor_comp, LinearMap.comp_assoc,
  LinearMap.subtype_comp_codRestrict, ← LinearMap.comp_assoc, Submodule.subtype_comp_inclusion,
  LinearMap.subtype_comp_codRestrict]

Result:
no goals