-- Mathlib/LinearAlgebra/Dimension/DivisionRing.lean
theorem linearIndependent_of_top_le_span_of_card_eq_finrank {ι : Type*} [Fintype ι] {b : ι → V}
    (spans : ⊤ ≤ span K (Set.range b)) (card_eq : Fintype.card ι = finrank K V) :
    LinearIndependent K b :=
  linearIndependent_iff'.mpr fun s g dependent i i_mem_s => by
    classical
    by_contra gx_ne_zero
    -- We'll derive a contradiction by showing `b '' (univ \ {i})` of cardinality `n - 1`
    -- spans a vector space of dimension `n`.
    refine not_le_of_gt (span_lt_top_of_card_lt_finrank
      (show (b '' (Set.univ \ {i})).toFinset.card < finrank K V from ?_)) ?_
    · calc
        (b '' (Set.univ \ {i})).toFinset.card = ((Set.univ \ {i}).toFinset.image b).card

Proof state:
case neg
K : Type u
V : Type v
inst✝³ : DivisionRing K
inst✝² : AddCommGroup V
inst✝¹ : Module K V
ι : Type u_2
inst✝ : Fintype ι
b : ι → V
spans : ⊤ ≤ span K (range b)
card_eq : Fintype.card ι = finrank K V
s : Finset ι
g : ι → K
dependent : ∑ i ∈ s, g i • b i = 0
i : ι
i_mem_s : i ∈ s
gx_ne_zero : ¬g i = 0
j : ι
j_eq : ¬j = i
⊢ b j ∈ ↑(span K (b '' (Set.univ \ {i})))

Tactic:
refine subset_span ⟨j, (Set.mem_diff _).mpr ⟨Set.mem_univ _, ?_⟩, rfl⟩

Result:
case neg
K : Type u
V : Type v
inst✝³ : DivisionRing K
inst✝² : AddCommGroup V
inst✝¹ : Module K V
ι : Type u_2
inst✝ : Fintype ι
b : ι → V
spans : ⊤ ≤ span K (range b)
card_eq : Fintype.card ι = finrank K V
s : Finset ι
g : ι → K
dependent : ∑ i ∈ s, g i • b i = 0
i : ι
i_mem_s : i ∈ s
gx_ne_zero : ¬g i = 0
j : ι
j_eq : ¬j = i
⊢ j ∉ {i}

Proof state:
case h
K : Type u
V : Type v
inst✝³ : DivisionRing K
inst✝² : AddCommGroup V
inst✝¹ : Module K V
ι : Type u_2
inst✝ : Fintype ι
b : ι → V
spans : ⊤ ≤ span K (range b)
card_eq : Fintype.card ι = finrank K V
s : Finset ι
g : ι → K
dependent : ∑ i ∈ s, g i • b i = 0
i : ι
i_mem_s : i ∈ s
gx_ne_zero : ¬g i = 0
j : ι
j_eq : j = i
⊢ b i + (g i)⁻¹ • ∑ j ∈ s.erase i, g j • b j = 0

Tactic:
calc
  (b i + (g i)⁻¹ • (s.erase i).sum fun j => g j • b j) =
      (g i)⁻¹ • (g i • b i + (s.erase i).sum fun j => g j • b j) := by
    rw [smul_add, ← mul_smul, inv_mul_cancel₀ gx_ne_zero, one_smul]
  _ = (g i)⁻¹ • (0 : V) := congr_arg _ ?_
  _ = 0 := smul_zero _

Result:
case h
K : Type u
V : Type v
inst✝³ : DivisionRing K
inst✝² : AddCommGroup V
inst✝¹ : Module K V
ι : Type u_2
inst✝ : Fintype ι
b : ι → V
spans : ⊤ ≤ span K (range b)
card_eq : Fintype.card ι = finrank K V
s : Finset ι
g : ι → K
dependent : ∑ i ∈ s, g i • b i = 0
i : ι
i_mem_s : i ∈ s
gx_ne_zero : ¬g i = 0
j : ι
j_eq : j = i
⊢ g i • b i + ∑ j ∈ s.erase i, g j • b j = 0

Proof state:
K : Type u
V : Type v
inst✝³ : DivisionRing K
inst✝² : AddCommGroup V
inst✝¹ : Module K V
ι : Type u_2
inst✝ : Fintype ι
b : ι → V
spans : ⊤ ≤ span K (range b)
card_eq : Fintype.card ι = finrank K V
s : Finset ι
g : ι → K
dependent : ∑ i ∈ s, g i • b i = 0
i : ι
i_mem_s : i ∈ s
⊢ g i = 0

Tactic:
by_contra gx_ne_zero

Result:
K : Type u
V : Type v
inst✝³ : DivisionRing K
inst✝² : AddCommGroup V
inst✝¹ : Module K V
ι : Type u_2
inst✝ : Fintype ι
b : ι → V
spans : ⊤ ≤ span K (range b)
card_eq : Fintype.card ι = finrank K V
s : Finset ι
g : ι → K
dependent : ∑ i ∈ s, g i • b i = 0
i : ι
i_mem_s : i ∈ s
gx_ne_zero : ¬g i = 0
⊢ False

Proof state:
case pos.intro
K : Type u
V : Type v
inst✝³ : DivisionRing K
inst✝² : AddCommGroup V
inst✝¹ : Module K V
ι : Type u_2
inst✝ : Fintype ι
b : ι → V
spans : ⊤ ≤ span K (range b)
card_eq : Fintype.card ι = finrank K V
s : Finset ι
g : ι → K
dependent : ∑ i ∈ s, g i • b i = 0
i : ι
i_mem_s : i ∈ s
gx_ne_zero : ¬g i = 0
j : ι
j_eq : j = i
k : ι
hk : k ∈ s.erase i
k_ne_i : k ≠ i
right✝ : k ∈ s
⊢ k ∈ Set.univ \ {i}

Tactic:
simp_all only [Set.mem_univ, Set.mem_diff, Set.mem_singleton_iff, and_self, not_false_eq_true]

Result:
no goals

Proof state:
case h
K : Type u
V : Type v
inst✝³ : DivisionRing K
inst✝² : AddCommGroup V
inst✝¹ : Module K V
ι : Type u_2
inst✝ : Fintype ι
b : ι → V
spans : ⊤ ≤ span K (range b)
card_eq : Fintype.card ι = finrank K V
s : Finset ι
g : ι → K
dependent : ∑ i ∈ s, g i • b i = 0
i : ι
i_mem_s : i ∈ s
gx_ne_zero : ¬g i = 0
j : ι
j_eq : j = i
⊢ g i • b i + ∑ j ∈ s.erase i, g j • b j = 0

Tactic:
rwa [← Finset.insert_erase i_mem_s, Finset.sum_insert (Finset.not_mem_erase _ _)] at dependent

Result:
no goals