-- 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:
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)
⊢ LinearMap.ker l23 = LinearMap.range l12

Tactic:
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

Result:
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)
⊢ Subsingleton H

Proof state:
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) ⊗[R] M
⊢ ∃ a, (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) a = x

Tactic:
induction x using TensorProduct.induction_on with
| zero => exact ⟨0, by simp⟩
| tmul x m =>
  rcases x with ⟨x, (hx : l23 x = 0)⟩
  have mem : x ⊗ₜ[R] m ∈ LinearMap.ker (l23.rTensor M) := by simp [hx]
  rw [LinearMap.exact_iff.1 ex] at mem
  obtain ⟨y, hy⟩ := mem
  refine ⟨LinearMap.rTensor M (LinearMap.rangeRestrict _ ∘ₗ LinearMap.rangeRestrict l12) y,
    Module.Flat.rTensor_preserves_injective_linearMap (LinearMap.ker l23).subtype
    Subtype.val_injective ?_⟩
  simp only [LinearMap.comp_codRestrict, LinearMap.rTensor_tmul, Submodule.coe_subtype, ← hy]
  rw [← LinearMap.comp_apply]
  erw [← LinearMap.rTensor_comp]
  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]
| add x y hx hy =>
  obtain ⟨x, rfl⟩ := hx; obtain ⟨y, rfl⟩ := hy
  exact ⟨x + y, by simp⟩

Result:
no goals

Proof state:
case add.intro.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))
x y : ↥(LinearMap.range (Submodule.inclusion complex)) ⊗[R] M
⊢ ∃ a,
    (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) a =
      (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) x +
        (map (LinearMap.range (Submodule.inclusion complex)).subtype LinearMap.id) y

Tactic:
exact ⟨x + y, by simp⟩

Result:
no goals