-- 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 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
⊢ g k • b k ∈ span K (b '' (Set.univ \ {i}))

Tactic:
refine smul_mem _ _ (subset_span ⟨k, ?_, rfl⟩)

Result:
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}

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
gx_ne_zero : ¬g i = 0
⊢ (b '' (Set.univ \ {i})).toFinset.card = (Finset.image b (Set.univ \ {i}).toFinset).card

Tactic:
rw [Set.toFinset_card, Fintype.card_ofFinset]

Result:
no goals

Proof state:
case refine_1
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
⊢ (b '' (Set.univ \ {i})).toFinset.card < finrank K V

Tactic:
calc
  (b '' (Set.univ \ {i})).toFinset.card = ((Set.univ \ {i}).toFinset.image b).card := by
    rw [Set.toFinset_card, Fintype.card_ofFinset]
  _ ≤ (Set.univ \ {i}).toFinset.card := Finset.card_image_le
  _ = (Finset.univ.erase i).card := (congr_arg Finset.card (Finset.ext (by simp [and_comm])))
  _ < Finset.univ.card := Finset.card_erase_lt_of_mem (Finset.mem_univ i)
  _ = finrank K V := card_eq

Result:
no goals

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
⊢ j ∉ {i}

Tactic:
exact mt Set.mem_singleton_iff.mp j_eq

Result:
no goals

Proof state:
case refine_2.intro.refl
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 : ι
⊢ b j ∈ ↑(span K (b '' (Set.univ \ {i})))

Tactic:
by_cases j_eq : j = i

Result:
case pos
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})))

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})))

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
gx_ne_zero : ¬g i = 0
j : ι
j_eq : j = i
⊢ b i + (g i)⁻¹ • ∑ j ∈ s.erase i, g j • b j = (g i)⁻¹ • (g i • b i + ∑ j ∈ s.erase i, g j • b j)

Tactic:
rw [smul_add, ← mul_smul, inv_mul_cancel₀ gx_ne_zero, one_smul]

Result:
no goals

Proof state:
case refine_2
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
⊢ range b ⊆ ↑(span K (b '' (Set.univ \ {i})))

Tactic:
rintro _ ⟨j, rfl, rfl⟩

Result:
case refine_2.intro.refl
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 : ι
⊢ b j ∈ ↑(span K (b '' (Set.univ \ {i})))

Proof state:
case pos
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:
rw [j_eq, SetLike.mem_coe, show b i = -((g i)⁻¹ • (s.erase i).sum fun j => g j • b j) from _]

Result:
case pos
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)⁻¹ • ∑ j ∈ s.erase i, g j • b j) ∈ span K (b '' (Set.univ \ {i}))

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)