-- Mathlib/Algebra/ContinuedFractions/Determinant.lean
theorem determinant_aux (hyp : n = 0 ∨ ¬(↑s : GenContFract K).TerminatedAt (n - 1)) :
    ((↑s : GenContFract K).contsAux n).a * ((↑s : GenContFract K).contsAux (n + 1)).b -
      ((↑s : GenContFract K).contsAux n).b * ((↑s : GenContFract K).contsAux (n + 1)).a =
        (-1) ^ n

Proof state:
case succ
K : Type u_1
inst✝ : Field K
s : SimpContFract K
n✝ n : ℕ
hyp : n + 1 = 0 ∨ ¬(↑s).TerminatedAt (n + 1 - 1)
g : GenContFract K := ↑s
conts : Pair K := g.contsAux (n + 2)
pred_conts : Pair K := g.contsAux (n + 1)
pred_conts_eq : pred_conts = g.contsAux (n + 1)
ppred_conts : Pair K := g.contsAux n
IH : n = 0 ∨ ¬(↑s).TerminatedAt (n - 1) → ppred_conts.a * pred_conts.b - ppred_conts.b * pred_conts.a = (-1) ^ n
ppred_conts_eq : ppred_conts = g.contsAux n
pA : K := pred_conts.a
pB : K := pred_conts.b
ppA : K := ppred_conts.a
⊢ pred_conts.a * ((↑s).contsAux (n + 1 + 1)).b - pred_conts.b * ((↑s).contsAux (n + 1 + 1)).a = (-1) ^ (n + 1)

Tactic:
let ppB := ppred_conts.b

Result:
case succ
K : Type u_1
inst✝ : Field K
s : SimpContFract K
n✝ n : ℕ
hyp : n + 1 = 0 ∨ ¬(↑s).TerminatedAt (n + 1 - 1)
g : GenContFract K := ↑s
conts : Pair K := g.contsAux (n + 2)
pred_conts : Pair K := g.contsAux (n + 1)
pred_conts_eq : pred_conts = g.contsAux (n + 1)
ppred_conts : Pair K := g.contsAux n
IH : n = 0 ∨ ¬(↑s).TerminatedAt (n - 1) → ppred_conts.a * pred_conts.b - ppred_conts.b * pred_conts.a = (-1) ^ n
ppred_conts_eq : ppred_conts = g.contsAux n
pA : K := pred_conts.a
pB : K := pred_conts.b
ppA : K := ppred_conts.a
ppB : K := ppred_conts.b
⊢ pred_conts.a * ((↑s).contsAux (n + 1 + 1)).b - pred_conts.b * ((↑s).contsAux (n + 1 + 1)).a = (-1) ^ (n + 1)

Proof state:
K : Type u_1
inst✝ : Field K
s : SimpContFract K
n✝ n : ℕ
hyp : n + 1 = 0 ∨ ¬(↑s).TerminatedAt (n + 1 - 1)
g : GenContFract K := ↑s
conts : Pair K := g.contsAux (n + 2)
pred_conts : Pair K := g.contsAux (n + 1)
pred_conts_eq : pred_conts = g.contsAux (n + 1)
ppred_conts : Pair K := g.contsAux n
IH : n = 0 ∨ ¬(↑s).TerminatedAt (n - 1) → ppred_conts.a * pred_conts.b - ppred_conts.b * pred_conts.a = (-1) ^ n
ppred_conts_eq : ppred_conts = g.contsAux n
pA : K := pred_conts.a
pB : K := pred_conts.b
ppA : K := ppred_conts.a
ppB : K := ppred_conts.b
not_terminated_at_n : ¬g.TerminatedAt n
gp : Pair K
s_nth_eq : g.s.get? n = some gp
this : pA * (ppB + gp.b * pB) - pB * (ppA + gp.b * pA) = (-1) ^ (n + 1)
⊢ pA * conts.b - pB * conts.a = (-1) ^ (n + 1)

Tactic:
simp only [conts, contsAux_recurrence s_nth_eq ppred_conts_eq pred_conts_eq]

Result:
K : Type u_1
inst✝ : Field K
s : SimpContFract K
n✝ n : ℕ
hyp : n + 1 = 0 ∨ ¬(↑s).TerminatedAt (n + 1 - 1)
g : GenContFract K := ↑s
conts : Pair K := g.contsAux (n + 2)
pred_conts : Pair K := g.contsAux (n + 1)
pred_conts_eq : pred_conts = g.contsAux (n + 1)
ppred_conts : Pair K := g.contsAux n
IH : n = 0 ∨ ¬(↑s).TerminatedAt (n - 1) → ppred_conts.a * pred_conts.b - ppred_conts.b * pred_conts.a = (-1) ^ n
ppred_conts_eq : ppred_conts = g.contsAux n
pA : K := pred_conts.a
pB : K := pred_conts.b
ppA : K := ppred_conts.a
ppB : K := ppred_conts.b
not_terminated_at_n : ¬g.TerminatedAt n
gp : Pair K
s_nth_eq : g.s.get? n = some gp
this : pA * (ppB + gp.b * pB) - pB * (ppA + gp.b * pA) = (-1) ^ (n + 1)
⊢ pA * (gp.b * (g.contsAux (n + 1)).b + gp.a * (g.contsAux n).b) -
      pB * (gp.b * (g.contsAux (n + 1)).a + gp.a * (g.contsAux n).a) =
    (-1) ^ (n + 1)

Proof state:
case succ
K : Type u_1
inst✝ : Field K
s : SimpContFract K
n✝ n : ℕ
hyp : n + 1 = 0 ∨ ¬(↑s).TerminatedAt (n + 1 - 1)
g : GenContFract K := ↑s
conts : Pair K := g.contsAux (n + 2)
pred_conts : Pair K := g.contsAux (n + 1)
pred_conts_eq : pred_conts = g.contsAux (n + 1)
ppred_conts : Pair K := g.contsAux n
IH : n = 0 ∨ ¬(↑s).TerminatedAt (n - 1) → ppred_conts.a * pred_conts.b - ppred_conts.b * pred_conts.a = (-1) ^ n
ppred_conts_eq : ppred_conts = g.contsAux n
pA : K := pred_conts.a
pB : K := pred_conts.b
ppA : K := ppred_conts.a
ppB : K := ppred_conts.b
⊢ pA * conts.b - pB * conts.a = (-1) ^ (n + 1)

Tactic:
have not_terminated_at_n : ¬TerminatedAt g n := Or.resolve_left hyp n.succ_ne_zero

Result:
case succ
K : Type u_1
inst✝ : Field K
s : SimpContFract K
n✝ n : ℕ
hyp : n + 1 = 0 ∨ ¬(↑s).TerminatedAt (n + 1 - 1)
g : GenContFract K := ↑s
conts : Pair K := g.contsAux (n + 2)
pred_conts : Pair K := g.contsAux (n + 1)
pred_conts_eq : pred_conts = g.contsAux (n + 1)
ppred_conts : Pair K := g.contsAux n
IH : n = 0 ∨ ¬(↑s).TerminatedAt (n - 1) → ppred_conts.a * pred_conts.b - ppred_conts.b * pred_conts.a = (-1) ^ n
ppred_conts_eq : ppred_conts = g.contsAux n
pA : K := pred_conts.a
pB : K := pred_conts.b
ppA : K := ppred_conts.a
ppB : K := ppred_conts.b
not_terminated_at_n : ¬g.TerminatedAt n
⊢ pA * conts.b - pB * conts.a = (-1) ^ (n + 1)

Proof state:
K : Type u_1
inst✝ : Field K
s : SimpContFract K
n : ℕ
hyp : n = 0 ∨ ¬(↑s).TerminatedAt (n - 1)
⊢ ((↑s).contsAux n).a * ((↑s).contsAux (n + 1)).b - ((↑s).contsAux n).b * ((↑s).contsAux (n + 1)).a = (-1) ^ n

Tactic:
induction n with
| zero => simp [contsAux]
| succ n IH =>
  let g := (↑s : GenContFract K)
  let conts := contsAux g (n + 2)
  set pred_conts := contsAux g (n + 1) with pred_conts_eq
  set ppred_conts := contsAux g n with ppred_conts_eq
  let pA := pred_conts.a
  let pB := pred_conts.b
  let ppA := ppred_conts.a
  let ppB := ppred_conts.b
  change pA * conts.b - pB * conts.a = (-1) ^ (n + 1)
  have not_terminated_at_n : ¬TerminatedAt g n := Or.resolve_left hyp n.succ_ne_zero
  obtain ⟨gp, s_nth_eq⟩ : ∃ gp, g.s.get? n = some gp :=
    Option.ne_none_iff_exists'.1 not_terminated_at_n
  suffices pA * (ppB + gp.b * pB) - pB * (ppA + gp.b * pA) = (-1) ^ (n + 1) by
    simp only [conts, contsAux_recurrence s_nth_eq ppred_conts_eq pred_conts_eq]
    have gp_a_eq_one : gp.a = 1 := s.property _ _ (partNum_eq_s_a s_nth_eq)
    rw [gp_a_eq_one, this.symm]
    ring
  suffices pA * ppB - pB * ppA = (-1) ^ (n + 1) by calc
    pA * (ppB + gp.b * pB) - pB * (ppA + gp.b * pA) =
        pA * ppB + pA * gp.b * pB - pB * ppA - pB * gp.b * pA := by ring
    _ = pA * ppB - pB * ppA := by ring
    _ = (-1) ^ (n + 1) := by assumption
  suffices ppA * pB - ppB * pA = (-1) ^ n by
    have pow_succ_n : (-1 : K) ^ (n + 1) = -1 * (-1) ^ n := pow_succ' (-1) n
    rw [pow_succ_n, ← this]
    ring
  exact IH <| Or.inr <| mt (terminated_stable <| n.sub_le 1) not_terminated_at_n

Result:
no goals

Proof state:
case succ
K : Type u_1
inst✝ : Field K
s : SimpContFract K
n✝ n : ℕ
IH :
  n = 0 ∨ ¬(↑s).TerminatedAt (n - 1) →
    ((↑s).contsAux n).a * ((↑s).contsAux (n + 1)).b - ((↑s).contsAux n).b * ((↑s).contsAux (n + 1)).a = (-1) ^ n
hyp : n + 1 = 0 ∨ ¬(↑s).TerminatedAt (n + 1 - 1)
g : GenContFract K := ↑s
conts : Pair K := g.contsAux (n + 2)
⊢ ((↑s).contsAux (n + 1)).a * ((↑s).contsAux (n + 1 + 1)).b -
      ((↑s).contsAux (n + 1)).b * ((↑s).contsAux (n + 1 + 1)).a =
    (-1) ^ (n + 1)

Tactic:
set pred_conts := contsAux g (n + 1) with pred_conts_eq

Result:
case succ
K : Type u_1
inst✝ : Field K
s : SimpContFract K
n✝ n : ℕ
hyp : n + 1 = 0 ∨ ¬(↑s).TerminatedAt (n + 1 - 1)
g : GenContFract K := ↑s
conts : Pair K := g.contsAux (n + 2)
pred_conts : Pair K := g.contsAux (n + 1)
IH :
  n = 0 ∨ ¬(↑s).TerminatedAt (n - 1) →
    ((↑s).contsAux n).a * pred_conts.b - ((↑s).contsAux n).b * pred_conts.a = (-1) ^ n
pred_conts_eq : pred_conts = g.contsAux (n + 1)
⊢ pred_conts.a * ((↑s).contsAux (n + 1 + 1)).b - pred_conts.b * ((↑s).contsAux (n + 1 + 1)).a = (-1) ^ (n + 1)