Theorem: GeneralizedContinuedFraction.of_correctness_of_nth_stream_eq_none
File: Mathlib/Algebra/ContinuedFractions/Computation/CorrectnessTerminating.lean

tactic: contradiction
state_before: case zero
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n : ℕ
inst✝ : FloorRing K
nth_stream_eq_none : IntFractPair.stream v Nat.zero = none
⊢ v = convergents (of v) (Nat.zero - 1)

tactic: let g := of v
state_before: case succ
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n✝ : ℕ
inst✝ : FloorRing K
n : ℕ
IH : IntFractPair.stream v n = none → v = convergents (of v) (n - 1)
nth_stream_eq_none : IntFractPair.stream v (Nat.succ n) = none
⊢ v = convergents (of v) (Nat.succ n - 1)

tactic: change v = g.convergents n
state_before: case succ
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n✝ : ℕ
inst✝ : FloorRing K
n : ℕ
IH : IntFractPair.stream v n = none → v = convergents (of v) (n - 1)
nth_stream_eq_none : IntFractPair.stream v (Nat.succ n) = none
g : GeneralizedContinuedFraction K := of v
⊢ v = convergents (of v) (Nat.succ n - 1)

tactic: have :
  IntFractPair.stream v n = none ∨ ∃ ifp, IntFractPair.stream v n = some ifp ∧ ifp.fr = 0 :=
  IntFractPair.succ_nth_stream_eq_none_iff.1 nth_stream_eq_none
state_before: case succ
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n✝ : ℕ
inst✝ : FloorRing K
n : ℕ
IH : IntFractPair.stream v n = none → v = convergents (of v) (n - 1)
nth_stream_eq_none : IntFractPair.stream v (Nat.succ n) = none
g : GeneralizedContinuedFraction K := of v
⊢ v = convergents g n

tactic: rcases this with (⟨nth_stream_eq_none⟩ | ⟨ifp_n, nth_stream_eq, nth_stream_fr_eq_zero⟩)
state_before: case succ
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n✝ : ℕ
inst✝ : FloorRing K
n : ℕ
IH : IntFractPair.stream v n = none → v = convergents (of v) (n - 1)
nth_stream_eq_none : IntFractPair.stream v (Nat.succ n) = none
g : GeneralizedContinuedFraction K := of v
this : IntFractPair.stream v n = none ∨ ∃ ifp, IntFractPair.stream v n = some ifp ∧ ifp.fr = 0
⊢ v = convergents g n

tactic: cases' n with n'
state_before: case succ.inl
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n✝ : ℕ
inst✝ : FloorRing K
n : ℕ
IH : IntFractPair.stream v n = none → v = convergents (of v) (n - 1)
nth_stream_eq_none✝ : IntFractPair.stream v (Nat.succ n) = none
g : GeneralizedContinuedFraction K := of v
nth_stream_eq_none : IntFractPair.stream v n = none
⊢ v = convergents g n

tactic: contradiction
state_before: case succ.inl.zero
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n : ℕ
inst✝ : FloorRing K
g : GeneralizedContinuedFraction K := of v
IH : IntFractPair.stream v Nat.zero = none → v = convergents (of v) (Nat.zero - 1)
nth_stream_eq_none✝ : IntFractPair.stream v (Nat.succ Nat.zero) = none
nth_stream_eq_none : IntFractPair.stream v Nat.zero = none
⊢ v = convergents g Nat.zero

tactic: have : g.TerminatedAt n' :=
  of_terminatedAt_n_iff_succ_nth_intFractPair_stream_eq_none.2
    nth_stream_eq_none
state_before: case succ.inl.succ
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n : ℕ
inst✝ : FloorRing K
g : GeneralizedContinuedFraction K := of v
n' : ℕ
IH : IntFractPair.stream v (Nat.succ n') = none → v = convergents (of v) (Nat.succ n' - 1)
nth_stream_eq_none✝ : IntFractPair.stream v (Nat.succ (Nat.succ n')) = none
nth_stream_eq_none : IntFractPair.stream v (Nat.succ n') = none
⊢ v = convergents g (Nat.succ n')

tactic: have : g.convergents (n' + 1) = g.convergents n' :=
  convergents_stable_of_terminated n'.le_succ this
state_before: case succ.inl.succ
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n : ℕ
inst✝ : FloorRing K
g : GeneralizedContinuedFraction K := of v
n' : ℕ
IH : IntFractPair.stream v (Nat.succ n') = none → v = convergents (of v) (Nat.succ n' - 1)
nth_stream_eq_none✝ : IntFractPair.stream v (Nat.succ (Nat.succ n')) = none
nth_stream_eq_none : IntFractPair.stream v (Nat.succ n') = none
this : TerminatedAt g n'
⊢ v = convergents g (Nat.succ n')

tactic: rw [this]
state_before: case succ.inl.succ
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n : ℕ
inst✝ : FloorRing K
g : GeneralizedContinuedFraction K := of v
n' : ℕ
IH : IntFractPair.stream v (Nat.succ n') = none → v = convergents (of v) (Nat.succ n' - 1)
nth_stream_eq_none✝ : IntFractPair.stream v (Nat.succ (Nat.succ n')) = none
nth_stream_eq_none : IntFractPair.stream v (Nat.succ n') = none
this✝ : TerminatedAt g n'
this : convergents g (n' + 1) = convergents g n'
⊢ v = convergents g (Nat.succ n')

tactic: exact IH nth_stream_eq_none
state_before: case succ.inl.succ
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n : ℕ
inst✝ : FloorRing K
g : GeneralizedContinuedFraction K := of v
n' : ℕ
IH : IntFractPair.stream v (Nat.succ n') = none → v = convergents (of v) (Nat.succ n' - 1)
nth_stream_eq_none✝ : IntFractPair.stream v (Nat.succ (Nat.succ n')) = none
nth_stream_eq_none : IntFractPair.stream v (Nat.succ n') = none
this✝ : TerminatedAt g n'
this : convergents g (n' + 1) = convergents g n'
⊢ v = convergents g n'

tactic: simpa [nth_stream_fr_eq_zero, compExactValue] using
  compExactValue_correctness_of_stream_eq_some nth_stream_eq
state_before: case succ.inr.intro.intro
K : Type u_1
inst✝¹ : LinearOrderedField K
v : K
n✝ : ℕ
inst✝ : FloorRing K
n : ℕ
IH : IntFractPair.stream v n = none → v = convergents (of v) (n - 1)
nth_stream_eq_none : IntFractPair.stream v (Nat.succ n) = none
g : GeneralizedContinuedFraction K := of v
ifp_n : IntFractPair K
nth_stream_eq : IntFractPair.stream v n = some ifp_n
nth_stream_fr_eq_zero : ifp_n.fr = 0
⊢ v = convergents g n