Theorem: Nat.Partrec.Code.evaln_complete
File: Mathlib/Computability/PartrecCode.lean

tactic: rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
state_before: c : Code
n x : ℕ
h : x ∈ eval c n
⊢ ∃ k, x ∈ evaln k c n

tactic: induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
state_before: c : Code
n x : ℕ
h : x ∈ eval c n
⊢ ∃ k, x ∈ evaln (k + 1) c n

tactic: iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
state_before: case zero
n x : ℕ
h : x = 0
⊢ (∃ x, n ≤ x) ∧ 0 = x

case succ
n x : ℕ
h : x = Nat.succ n
⊢ (∃ x, n ≤ x) ∧ Nat.succ n = x

case left
n x : ℕ
h : x = (unpair n).fst
⊢ (∃ x, n ≤ x) ∧ (unpair n).fst = x

case right
n x : ℕ
h : x = (unpair n).snd
⊢ (∃ x, n ≤ x) ∧ (unpair n).snd = x

case pair
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h : ∃ a, a ∈ eval a✝¹ n ∧ ∃ a_1, a_1 ∈ eval a✝ n ∧ Nat.pair a a_1 = x
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) a✝¹ n = some a ∧ ∃ a_1, evaln (k + 1) a✝ n = some a_1 ∧ Nat.pair a a_1 = x

case comp
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h : ∃ a, a ∈ eval a✝ n ∧ x ∈ eval a✝¹ a
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) a✝ n = some a ∧ evaln (k + 1) a✝¹ a = some x

case prec
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h :
  x ∈
    Nat.rec (eval a✝¹ (unpair n).fst)
      (fun y IH => Part.bind IH fun i => eval a✝ (Nat.pair (unpair n).fst (Nat.pair y i))) (unpair n).snd
⊢ ∃ k,
    n ≤ k ∧
      Nat.rec (evaln (k + 1) a✝¹ (unpair n).fst)
          (fun n_1 n_ih =>
            Option.bind (evaln k (prec a✝¹ a✝) (Nat.pair (unpair n).fst n_1)) fun i =>
              evaln (k + 1) a✝ (Nat.pair (unpair n).fst (Nat.pair n_1 i)))
          (unpair n).snd =
        some x

case rfind'
a✝ : Code
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h :
  ∃ a,
    (0 ∈ eval a✝ (Nat.pair (unpair n).fst (a + (unpair n).snd)) ∧
        ∀ {m : ℕ}, m < a → ∃ a, a ∈ eval a✝ (Nat.pair (unpair n).fst (m + (unpair n).snd)) ∧ ¬a = 0) ∧
      a + (unpair n).snd = x
⊢ ∃ k,
    n ≤ k ∧
      ∃ a,
        evaln (k + 1) a✝ n = some a ∧
          (if a = 0 then some (unpair n).snd else evaln k (rfind' a✝) (Nat.pair (unpair n).fst ((unpair n).snd + 1))) =
            some x

tactic: case pair cf cg hf hg =>
  rcases h with ⟨x, hx, y, hy, rfl⟩
  rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
  refine' ⟨max k₁ k₂, _⟩
  refine'
    ⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
      evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
      evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
state_before: case pair
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h : ∃ a, a ∈ eval a✝¹ n ∧ ∃ a_1, a_1 ∈ eval a✝ n ∧ Nat.pair a a_1 = x
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) a✝¹ n = some a ∧ ∃ a_1, evaln (k + 1) a✝ n = some a_1 ∧ Nat.pair a a_1 = x

case comp
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h : ∃ a, a ∈ eval a✝ n ∧ x ∈ eval a✝¹ a
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) a✝ n = some a ∧ evaln (k + 1) a✝¹ a = some x

case prec
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h :
  x ∈
    Nat.rec (eval a✝¹ (unpair n).fst)
      (fun y IH => Part.bind IH fun i => eval a✝ (Nat.pair (unpair n).fst (Nat.pair y i))) (unpair n).snd
⊢ ∃ k,
    n ≤ k ∧
      Nat.rec (evaln (k + 1) a✝¹ (unpair n).fst)
          (fun n_1 n_ih =>
            Option.bind (evaln k (prec a✝¹ a✝) (Nat.pair (unpair n).fst n_1)) fun i =>
              evaln (k + 1) a✝ (Nat.pair (unpair n).fst (Nat.pair n_1 i)))
          (unpair n).snd =
        some x

case rfind'
a✝ : Code
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h :
  ∃ a,
    (0 ∈ eval a✝ (Nat.pair (unpair n).fst (a + (unpair n).snd)) ∧
        ∀ {m : ℕ}, m < a → ∃ a, a ∈ eval a✝ (Nat.pair (unpair n).fst (m + (unpair n).snd)) ∧ ¬a = 0) ∧
      a + (unpair n).snd = x
⊢ ∃ k,
    n ≤ k ∧
      ∃ a,
        evaln (k + 1) a✝ n = some a ∧
          (if a = 0 then some (unpair n).snd else evaln k (rfind' a✝) (Nat.pair (unpair n).fst ((unpair n).snd + 1))) =
            some x

tactic: case comp cf cg hf hg =>
  rcases h with ⟨y, hy, hx⟩
  rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
  refine' ⟨max k₁ k₂, _⟩
  exact
    ⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
      evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
      evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
state_before: case comp
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h : ∃ a, a ∈ eval a✝ n ∧ x ∈ eval a✝¹ a
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) a✝ n = some a ∧ evaln (k + 1) a✝¹ a = some x

case prec
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h :
  x ∈
    Nat.rec (eval a✝¹ (unpair n).fst)
      (fun y IH => Part.bind IH fun i => eval a✝ (Nat.pair (unpair n).fst (Nat.pair y i))) (unpair n).snd
⊢ ∃ k,
    n ≤ k ∧
      Nat.rec (evaln (k + 1) a✝¹ (unpair n).fst)
          (fun n_1 n_ih =>
            Option.bind (evaln k (prec a✝¹ a✝) (Nat.pair (unpair n).fst n_1)) fun i =>
              evaln (k + 1) a✝ (Nat.pair (unpair n).fst (Nat.pair n_1 i)))
          (unpair n).snd =
        some x

case rfind'
a✝ : Code
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h :
  ∃ a,
    (0 ∈ eval a✝ (Nat.pair (unpair n).fst (a + (unpair n).snd)) ∧
        ∀ {m : ℕ}, m < a → ∃ a, a ∈ eval a✝ (Nat.pair (unpair n).fst (m + (unpair n).snd)) ∧ ¬a = 0) ∧
      a + (unpair n).snd = x
⊢ ∃ k,
    n ≤ k ∧
      ∃ a,
        evaln (k + 1) a✝ n = some a ∧
          (if a = 0 then some (unpair n).snd else evaln k (rfind' a✝) (Nat.pair (unpair n).fst ((unpair n).snd + 1))) =
            some x

tactic: exact ⟨k + 1, h⟩
state_before: case intro
c : Code
n x : ℕ
h✝ : x ∈ eval c n
k : ℕ
h : x ∈ evaln (k + 1) c n
⊢ ∃ k, x ∈ evaln k c n

tactic: exact ⟨⟨_, le_rfl⟩, h.symm⟩
state_before: case right
n x : ℕ
h : x = (unpair n).snd
⊢ (∃ x, n ≤ x) ∧ (unpair n).snd = x

case pair
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h : ∃ a, a ∈ eval a✝¹ n ∧ ∃ a_1, a_1 ∈ eval a✝ n ∧ Nat.pair a a_1 = x
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) a✝¹ n = some a ∧ ∃ a_1, evaln (k + 1) a✝ n = some a_1 ∧ Nat.pair a a_1 = x

case comp
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h : ∃ a, a ∈ eval a✝ n ∧ x ∈ eval a✝¹ a
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) a✝ n = some a ∧ evaln (k + 1) a✝¹ a = some x

case prec
a✝¹ a✝ : Code
a_ih✝¹ : ∀ {n x : ℕ}, x ∈ eval a✝¹ n → ∃ k, x ∈ evaln (k + 1) a✝¹ n
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h :
  x ∈
    Nat.rec (eval a✝¹ (unpair n).fst)
      (fun y IH => Part.bind IH fun i => eval a✝ (Nat.pair (unpair n).fst (Nat.pair y i))) (unpair n).snd
⊢ ∃ k,
    n ≤ k ∧
      Nat.rec (evaln (k + 1) a✝¹ (unpair n).fst)
          (fun n_1 n_ih =>
            Option.bind (evaln k (prec a✝¹ a✝) (Nat.pair (unpair n).fst n_1)) fun i =>
              evaln (k + 1) a✝ (Nat.pair (unpair n).fst (Nat.pair n_1 i)))
          (unpair n).snd =
        some x

case rfind'
a✝ : Code
a_ih✝ : ∀ {n x : ℕ}, x ∈ eval a✝ n → ∃ k, x ∈ evaln (k + 1) a✝ n
n x : ℕ
h :
  ∃ a,
    (0 ∈ eval a✝ (Nat.pair (unpair n).fst (a + (unpair n).snd)) ∧
        ∀ {m : ℕ}, m < a → ∃ a, a ∈ eval a✝ (Nat.pair (unpair n).fst (m + (unpair n).snd)) ∧ ¬a = 0) ∧
      a + (unpair n).snd = x
⊢ ∃ k,
    n ≤ k ∧
      ∃ a,
        evaln (k + 1) a✝ n = some a ∧
          (if a = 0 then some (unpair n).snd else evaln k (rfind' a✝) (Nat.pair (unpair n).fst ((unpair n).snd + 1))) =
            some x

tactic: rcases h with ⟨x, hx, y, hy, rfl⟩
state_before: cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x : ℕ
h : ∃ a, a ∈ eval cf n ∧ ∃ a_1, a_1 ∈ eval cg n ∧ Nat.pair a a_1 = x
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) cf n = some a ∧ ∃ a_1, evaln (k + 1) cg n = some a_1 ∧ Nat.pair a a_1 = x

tactic: rcases hf hx with ⟨k₁, hk₁⟩
state_before: case intro.intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x : ℕ
hx : x ∈ eval cf n
y : ℕ
hy : y ∈ eval cg n
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) cf n = some a ∧ ∃ a_1, evaln (k + 1) cg n = some a_1 ∧ Nat.pair a a_1 = Nat.pair x y

tactic: rcases hg hy with ⟨k₂, hk₂⟩
state_before: case intro.intro.intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x : ℕ
hx : x ∈ eval cf n
y : ℕ
hy : y ∈ eval cg n
k₁ : ℕ
hk₁ : x ∈ evaln (k₁ + 1) cf n
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) cf n = some a ∧ ∃ a_1, evaln (k + 1) cg n = some a_1 ∧ Nat.pair a a_1 = Nat.pair x y

tactic: refine' ⟨max k₁ k₂, _⟩
state_before: case intro.intro.intro.intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x : ℕ
hx : x ∈ eval cf n
y : ℕ
hy : y ∈ eval cg n
k₁ : ℕ
hk₁ : x ∈ evaln (k₁ + 1) cf n
k₂ : ℕ
hk₂ : y ∈ evaln (k₂ + 1) cg n
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) cf n = some a ∧ ∃ a_1, evaln (k + 1) cg n = some a_1 ∧ Nat.pair a a_1 = Nat.pair x y

tactic: refine'
  ⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
    evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
    evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
state_before: case intro.intro.intro.intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x : ℕ
hx : x ∈ eval cf n
y : ℕ
hy : y ∈ eval cg n
k₁ : ℕ
hk₁ : x ∈ evaln (k₁ + 1) cf n
k₂ : ℕ
hk₂ : y ∈ evaln (k₂ + 1) cg n
⊢ n ≤ max k₁ k₂ ∧
    ∃ a,
      evaln (max k₁ k₂ + 1) cf n = some a ∧ ∃ a_1, evaln (max k₁ k₂ + 1) cg n = some a_1 ∧ Nat.pair a a_1 = Nat.pair x y

tactic: rcases h with ⟨y, hy, hx⟩
state_before: cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x : ℕ
h : ∃ a, a ∈ eval cg n ∧ x ∈ eval cf a
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) cg n = some a ∧ evaln (k + 1) cf a = some x

tactic: rcases hg hy with ⟨k₁, hk₁⟩
state_before: case intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x y : ℕ
hy : y ∈ eval cg n
hx : x ∈ eval cf y
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) cg n = some a ∧ evaln (k + 1) cf a = some x

tactic: rcases hf hx with ⟨k₂, hk₂⟩
state_before: case intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x y : ℕ
hy : y ∈ eval cg n
hx : x ∈ eval cf y
k₁ : ℕ
hk₁ : y ∈ evaln (k₁ + 1) cg n
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) cg n = some a ∧ evaln (k + 1) cf a = some x

tactic: refine' ⟨max k₁ k₂, _⟩
state_before: case intro.intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x y : ℕ
hy : y ∈ eval cg n
hx : x ∈ eval cf y
k₁ : ℕ
hk₁ : y ∈ evaln (k₁ + 1) cg n
k₂ : ℕ
hk₂ : x ∈ evaln (k₂ + 1) cf y
⊢ ∃ k, n ≤ k ∧ ∃ a, evaln (k + 1) cg n = some a ∧ evaln (k + 1) cf a = some x

tactic: exact
  ⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
    evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
    evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
state_before: case intro.intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x y : ℕ
hy : y ∈ eval cg n
hx : x ∈ eval cf y
k₁ : ℕ
hk₁ : y ∈ evaln (k₁ + 1) cg n
k₂ : ℕ
hk₂ : x ∈ evaln (k₂ + 1) cf y
⊢ n ≤ max k₁ k₂ ∧ ∃ a, evaln (max k₁ k₂ + 1) cg n = some a ∧ evaln (max k₁ k₂ + 1) cf a = some x

tactic: revert h
state_before: cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x : ℕ
h :
  x ∈
    Nat.rec (eval cf (unpair n).fst)
      (fun y IH => Part.bind IH fun i => eval cg (Nat.pair (unpair n).fst (Nat.pair y i))) (unpair n).snd
⊢ ∃ k,
    n ≤ k ∧
      Nat.rec (evaln (k + 1) cf (unpair n).fst)
          (fun n_1 n_ih =>
            Option.bind (evaln k (prec cf cg) (Nat.pair (unpair n).fst n_1)) fun i =>
              evaln (k + 1) cg (Nat.pair (unpair n).fst (Nat.pair n_1 i)))
          (unpair n).snd =
        some x

tactic: generalize n.unpair.1 = n₁
state_before: cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x : ℕ
⊢ x ∈
      Nat.rec (eval cf (unpair n).fst)
        (fun y IH => Part.bind IH fun i => eval cg (Nat.pair (unpair n).fst (Nat.pair y i))) (unpair n).snd →
    ∃ k,
      n ≤ k ∧
        Nat.rec (evaln (k + 1) cf (unpair n).fst)
            (fun n_1 n_ih =>
              Option.bind (evaln k (prec cf cg) (Nat.pair (unpair n).fst n_1)) fun i =>
                evaln (k + 1) cg (Nat.pair (unpair n).fst (Nat.pair n_1 i)))
            (unpair n).snd =
          some x

tactic: generalize n.unpair.2 = n₂
state_before: cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x n₁ : ℕ
⊢ x ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) (unpair n).snd →
    ∃ k,
      n ≤ k ∧
        Nat.rec (evaln (k + 1) cf n₁)
            (fun n n_ih =>
              Option.bind (evaln k (prec cf cg) (Nat.pair n₁ n)) fun i => evaln (k + 1) cg (Nat.pair n₁ (Nat.pair n i)))
            (unpair n).snd =
          some x

tactic: induction' n₂ with m IH generalizing x n <;> simp
state_before: cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n x n₁ n₂ : ℕ
⊢ x ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) n₂ →
    ∃ k,
      n ≤ k ∧
        Nat.rec (evaln (k + 1) cf n₁)
            (fun n n_ih =>
              Option.bind (evaln k (prec cf cg) (Nat.pair n₁ n)) fun i => evaln (k + 1) cg (Nat.pair n₁ (Nat.pair n i)))
            n₂ =
          some x

tactic: intro h
state_before: case zero
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n✝ x✝ n₁ n x : ℕ
⊢ x ∈ eval cf n₁ → ∃ k, n ≤ k ∧ evaln (k + 1) cf n₁ = some x

tactic: rcases hf h with ⟨k, hk⟩
state_before: case zero
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n✝ x✝ n₁ n x : ℕ
h : x ∈ eval cf n₁
⊢ ∃ k, n ≤ k ∧ evaln (k + 1) cf n₁ = some x

tactic: exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
state_before: case zero.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n✝ x✝ n₁ n x : ℕ
h : x ∈ eval cf n₁
k : ℕ
hk : x ∈ evaln (k + 1) cf n₁
⊢ ∃ k, n ≤ k ∧ evaln (k + 1) cf n₁ = some x

tactic: intro y hy hx
state_before: case succ
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n✝ x✝ n₁ m : ℕ
IH :
  ∀ {n x : ℕ},
    x ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m →
      ∃ k,
        n ≤ k ∧
          Nat.rec (evaln (k + 1) cf n₁)
              (fun n n_ih =>
                Option.bind (evaln k (prec cf cg) (Nat.pair n₁ n)) fun i =>
                  evaln (k + 1) cg (Nat.pair n₁ (Nat.pair n i)))
              m =
            some x
n x : ℕ
⊢ ∀ (x_1 : ℕ),
    x_1 ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m →
      x ∈ eval cg (Nat.pair n₁ (Nat.pair m x_1)) →
        ∃ k,
          n ≤ k ∧
            ∃ a, evaln k (prec cf cg) (Nat.pair n₁ m) = some a ∧ evaln (k + 1) cg (Nat.pair n₁ (Nat.pair m a)) = some x

tactic: rcases IH hy with ⟨k₁, nk₁, hk₁⟩
state_before: case succ
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n✝ x✝ n₁ m : ℕ
IH :
  ∀ {n x : ℕ},
    x ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m →
      ∃ k,
        n ≤ k ∧
          Nat.rec (evaln (k + 1) cf n₁)
              (fun n n_ih =>
                Option.bind (evaln k (prec cf cg) (Nat.pair n₁ n)) fun i =>
                  evaln (k + 1) cg (Nat.pair n₁ (Nat.pair n i)))
              m =
            some x
n x y : ℕ
hy : y ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m
hx : x ∈ eval cg (Nat.pair n₁ (Nat.pair m y))
⊢ ∃ k,
    n ≤ k ∧ ∃ a, evaln k (prec cf cg) (Nat.pair n₁ m) = some a ∧ evaln (k + 1) cg (Nat.pair n₁ (Nat.pair m a)) = some x

tactic: rcases hg hx with ⟨k₂, hk₂⟩
state_before: case succ.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n✝ x✝ n₁ m : ℕ
IH :
  ∀ {n x : ℕ},
    x ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m →
      ∃ k,
        n ≤ k ∧
          Nat.rec (evaln (k + 1) cf n₁)
              (fun n n_ih =>
                Option.bind (evaln k (prec cf cg) (Nat.pair n₁ n)) fun i =>
                  evaln (k + 1) cg (Nat.pair n₁ (Nat.pair n i)))
              m =
            some x
n x y : ℕ
hy : y ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m
hx : x ∈ eval cg (Nat.pair n₁ (Nat.pair m y))
k₁ : ℕ
nk₁ : ?m.1303232 ≤ k₁
hk₁ :
  Nat.rec (evaln (k₁ + 1) cf n₁)
      (fun n n_ih =>
        Option.bind (evaln k₁ (prec cf cg) (Nat.pair n₁ n)) fun i => evaln (k₁ + 1) cg (Nat.pair n₁ (Nat.pair n i)))
      m =
    some y
⊢ ∃ k,
    n ≤ k ∧ ∃ a, evaln k (prec cf cg) (Nat.pair n₁ m) = some a ∧ evaln (k + 1) cg (Nat.pair n₁ (Nat.pair m a)) = some x

tactic: refine'
  ⟨(max k₁ k₂).succ,
    Nat.le_succ_of_le <| le_max_of_le_left <|
      le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
    evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
    evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
state_before: case succ.intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n✝ x✝ n₁ m : ℕ
IH :
  ∀ {n x : ℕ},
    x ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m →
      ∃ k,
        n ≤ k ∧
          Nat.rec (evaln (k + 1) cf n₁)
              (fun n n_ih =>
                Option.bind (evaln k (prec cf cg) (Nat.pair n₁ n)) fun i =>
                  evaln (k + 1) cg (Nat.pair n₁ (Nat.pair n i)))
              m =
            some x
n x y : ℕ
hy : y ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m
hx : x ∈ eval cg (Nat.pair n₁ (Nat.pair m y))
k₁ : ℕ
nk₁ : ?m.1303232 ≤ k₁
hk₁ :
  Nat.rec (evaln (k₁ + 1) cf n₁)
      (fun n n_ih =>
        Option.bind (evaln k₁ (prec cf cg) (Nat.pair n₁ n)) fun i => evaln (k₁ + 1) cg (Nat.pair n₁ (Nat.pair n i)))
      m =
    some y
k₂ : ℕ
hk₂ : x ∈ evaln (k₂ + 1) cg (Nat.pair n₁ (Nat.pair m y))
⊢ ∃ k,
    n ≤ k ∧ ∃ a, evaln k (prec cf cg) (Nat.pair n₁ m) = some a ∧ evaln (k + 1) cg (Nat.pair n₁ (Nat.pair m a)) = some x

tactic: simp [evaln, Bind.bind]
state_before: case succ.intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n✝ x✝ n₁ m : ℕ
IH :
  ∀ {n x : ℕ},
    x ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m →
      ∃ k,
        n ≤ k ∧
          Nat.rec (evaln (k + 1) cf n₁)
              (fun n n_ih =>
                Option.bind (evaln k (prec cf cg) (Nat.pair n₁ n)) fun i =>
                  evaln (k + 1) cg (Nat.pair n₁ (Nat.pair n i)))
              m =
            some x
n x y : ℕ
hy : y ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m
hx : x ∈ eval cg (Nat.pair n₁ (Nat.pair m y))
k₁ : ℕ
nk₁ : max n (Nat.pair n₁ m) ≤ k₁
hk₁ :
  Nat.rec (evaln (k₁ + 1) cf n₁)
      (fun n n_ih =>
        Option.bind (evaln k₁ (prec cf cg) (Nat.pair n₁ n)) fun i => evaln (k₁ + 1) cg (Nat.pair n₁ (Nat.pair n i)))
      m =
    some y
k₂ : ℕ
hk₂ : x ∈ evaln (k₂ + 1) cg (Nat.pair n₁ (Nat.pair m y))
⊢ y ∈ evaln (Nat.succ k₁) (prec cf cg) (Nat.pair n₁ m)

tactic: exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
state_before: case succ.intro.intro.intro
cf cg : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
hg : ∀ {n x : ℕ}, x ∈ eval cg n → ∃ k, x ∈ evaln (k + 1) cg n
n✝ x✝ n₁ m : ℕ
IH :
  ∀ {n x : ℕ},
    x ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m →
      ∃ k,
        n ≤ k ∧
          Nat.rec (evaln (k + 1) cf n₁)
              (fun n n_ih =>
                Option.bind (evaln k (prec cf cg) (Nat.pair n₁ n)) fun i =>
                  evaln (k + 1) cg (Nat.pair n₁ (Nat.pair n i)))
              m =
            some x
n x y : ℕ
hy : y ∈ Nat.rec (eval cf n₁) (fun y IH => Part.bind IH fun i => eval cg (Nat.pair n₁ (Nat.pair y i))) m
hx : x ∈ eval cg (Nat.pair n₁ (Nat.pair m y))
k₁ : ℕ
nk₁ : max n (Nat.pair n₁ m) ≤ k₁
hk₁ :
  Nat.rec (evaln (k₁ + 1) cf n₁)
      (fun n n_ih =>
        Option.bind (evaln k₁ (prec cf cg) (Nat.pair n₁ n)) fun i => evaln (k₁ + 1) cg (Nat.pair n₁ (Nat.pair n i)))
      m =
    some y
k₂ : ℕ
hk₂ : x ∈ evaln (k₂ + 1) cg (Nat.pair n₁ (Nat.pair m y))
⊢ Nat.pair n₁ m ≤ k₁ ∧
    Nat.rec (evaln (k₁ + 1) cf n₁)
        (fun n n_ih =>
          Option.bind (evaln k₁ (prec cf cg) (Nat.pair n₁ n)) fun i => evaln (k₁ + 1) cg (Nat.pair n₁ (Nat.pair n i)))
        m =
      some y

tactic: rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
state_before: cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n x : ℕ
h :
  ∃ a,
    (0 ∈ eval cf (Nat.pair (unpair n).fst (a + (unpair n).snd)) ∧
        ∀ {m : ℕ}, m < a → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + (unpair n).snd)) ∧ ¬a = 0) ∧
      a + (unpair n).snd = x
⊢ ∃ k,
    n ≤ k ∧
      ∃ a,
        evaln (k + 1) cf n = some a ∧
          (if a = 0 then some (unpair n).snd else evaln k (rfind' cf) (Nat.pair (unpair n).fst ((unpair n).snd + 1))) =
            some x

tactic: suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
  simpa [evaln, Bind.bind]
state_before: case intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y + (unpair n).snd))
hy₂ : ∀ {m : ℕ}, m < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + (unpair n).snd)) ∧ ¬a = 0
⊢ ∃ k,
    n ≤ k ∧
      ∃ a,
        evaln (k + 1) cf n = some a ∧
          (if a = 0 then some (unpair n).snd else evaln k (rfind' cf) (Nat.pair (unpair n).fst ((unpair n).snd + 1))) =
            some (y + (unpair n).snd)

tactic: revert hy₁ hy₂
state_before: case intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y + (unpair n).snd))
hy₂ : ∀ {m : ℕ}, m < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + (unpair n).snd)) ∧ ¬a = 0
⊢ ∃ k, y + (unpair n).snd ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst (unpair n).snd)

tactic: generalize n.unpair.2 = m
state_before: case intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y : ℕ
⊢ 0 ∈ eval cf (Nat.pair (unpair n).fst (y + (unpair n).snd)) →
    (∀ {m : ℕ}, m < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + (unpair n).snd)) ∧ ¬a = 0) →
      ∃ k, y + (unpair n).snd ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst (unpair n).snd)

tactic: intro hy₁ hy₂
state_before: case intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y m : ℕ
⊢ 0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
    (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
      ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)

tactic: induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
state_before: case intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
⊢ ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)

tactic: simpa [evaln, Bind.bind]
state_before: cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y + (unpair n).snd))
hy₂ : ∀ {m : ℕ}, m < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + (unpair n).snd)) ∧ ¬a = 0
this : ∃ k, y + (unpair n).snd ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst (unpair n).snd)
⊢ ∃ k,
    n ≤ k ∧
      ∃ a,
        evaln (k + 1) cf n = some a ∧
          (if a = 0 then some (unpair n).snd else evaln k (rfind' cf) (Nat.pair (unpair n).fst ((unpair n).snd + 1))) =
            some (y + (unpair n).snd)

tactic: simp at hy₁
state_before: case intro.intro.intro.zero
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.zero + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.zero → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
⊢ ∃ k,
    Nat.pair (unpair n).fst m ≤ k ∧
      ∃ a,
        evaln (k + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
          (if a = 0 then pure m else evaln k (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) = some m

tactic: rcases hf hy₁ with ⟨k, hk⟩
state_before: case intro.intro.intro.zero
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
m : ℕ
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.zero → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst m)
⊢ ∃ k,
    Nat.pair (unpair n).fst m ≤ k ∧
      ∃ a,
        evaln (k + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
          (if a = 0 then pure m else evaln k (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) = some m

tactic: exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
state_before: case intro.intro.intro.zero.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
m : ℕ
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.zero → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst m)
k : ℕ
hk : 0 ∈ evaln (k + 1) cf (Nat.pair (unpair n).fst m)
⊢ ∃ k,
    Nat.pair (unpair n).fst m ≤ k ∧
      ∃ a,
        evaln (k + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
          (if a = 0 then pure m else evaln k (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) = some m

tactic: simp
state_before: cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
m : ℕ
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.zero → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst m)
k : ℕ
hk : 0 ∈ evaln (k + 1) cf (Nat.pair (unpair n).fst m)
⊢ (if 0 = 0 then pure m else evaln k (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) = some m

tactic: rfl
state_before: cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
m : ℕ
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.zero → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst m)
k : ℕ
hk : 0 ∈ evaln (k + 1) cf (Nat.pair (unpair n).fst m)
⊢ pure m = some m

tactic: rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
state_before: case intro.intro.intro.succ
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
⊢ ∃ k,
    Nat.pair (unpair n).fst m ≤ k ∧
      ∃ a,
        evaln (k + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
          (if a = 0 then pure m else evaln k (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) = some (Nat.succ y + m)

tactic: rcases hf ha with ⟨k₁, hk₁⟩
state_before: case intro.intro.intro.succ.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
⊢ ∃ k,
    Nat.pair (unpair n).fst m ≤ k ∧
      ∃ a,
        evaln (k + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
          (if a = 0 then pure m else evaln k (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) = some (Nat.succ y + m)

tactic: rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
    fun {i} hi => by
    simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
      hy₂ (Nat.succ_lt_succ hi) with
  ⟨k₂, hk₂⟩
state_before: case intro.intro.intro.succ.intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
k₁ : ℕ
hk₁ : a ∈ evaln (k₁ + 1) cf (Nat.pair (unpair n).fst (0 + m))
⊢ ∃ k,
    Nat.pair (unpair n).fst m ≤ k ∧
      ∃ a,
        evaln (k + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
          (if a = 0 then pure m else evaln k (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) = some (Nat.succ y + m)

tactic: use (max k₁ k₂).succ
state_before: case intro.intro.intro.succ.intro.intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
k₁ : ℕ
hk₁ : a ∈ evaln (k₁ + 1) cf (Nat.pair (unpair n).fst (0 + m))
k₂ : ℕ
hk₂ : y + Nat.succ m ∈ evaln (k₂ + 1) (rfind' cf) (Nat.pair (unpair n).fst (Nat.succ m))
⊢ ∃ k,
    Nat.pair (unpair n).fst m ≤ k ∧
      ∃ a,
        evaln (k + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
          (if a = 0 then pure m else evaln k (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) = some (Nat.succ y + m)

tactic: rw [zero_add] at hk₁
state_before: case intro.intro.intro.succ.intro.intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
k₁ : ℕ
hk₁ : a ∈ evaln (k₁ + 1) cf (Nat.pair (unpair n).fst (0 + m))
k₂ : ℕ
hk₂ : y + Nat.succ m ∈ evaln (k₂ + 1) (rfind' cf) (Nat.pair (unpair n).fst (Nat.succ m))
⊢ Nat.pair (unpair n).fst m ≤ Nat.succ (max k₁ k₂) ∧
    ∃ a,
      evaln (Nat.succ (max k₁ k₂) + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
        (if a = 0 then pure m else evaln (Nat.succ (max k₁ k₂)) (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) =
          some (Nat.succ y + m)

tactic: use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
state_before: case intro.intro.intro.succ.intro.intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
k₁ : ℕ
hk₁ : a ∈ evaln (k₁ + 1) cf (Nat.pair (unpair n).fst m)
k₂ : ℕ
hk₂ : y + Nat.succ m ∈ evaln (k₂ + 1) (rfind' cf) (Nat.pair (unpair n).fst (Nat.succ m))
⊢ Nat.pair (unpair n).fst m ≤ Nat.succ (max k₁ k₂) ∧
    ∃ a,
      evaln (Nat.succ (max k₁ k₂) + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
        (if a = 0 then pure m else evaln (Nat.succ (max k₁ k₂)) (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) =
          some (Nat.succ y + m)

tactic: use a
state_before: case intro.intro.intro.succ.intro.intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
k₁ : ℕ
hk₁ : a ∈ evaln (k₁ + 1) cf (Nat.pair (unpair n).fst m)
k₂ : ℕ
hk₂ : y + Nat.succ m ∈ evaln (k₂ + 1) (rfind' cf) (Nat.pair (unpair n).fst (Nat.succ m))
⊢ ∃ a,
    evaln (Nat.succ (max k₁ k₂) + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
      (if a = 0 then pure m else evaln (Nat.succ (max k₁ k₂)) (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) =
        some (Nat.succ y + m)

tactic: use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
state_before: case intro.intro.intro.succ.intro.intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
k₁ : ℕ
hk₁ : a ∈ evaln (k₁ + 1) cf (Nat.pair (unpair n).fst m)
k₂ : ℕ
hk₂ : y + Nat.succ m ∈ evaln (k₂ + 1) (rfind' cf) (Nat.pair (unpair n).fst (Nat.succ m))
⊢ evaln (Nat.succ (max k₁ k₂) + 1) cf (Nat.pair (unpair n).fst m) = some a ∧
    (if a = 0 then pure m else evaln (Nat.succ (max k₁ k₂)) (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) =
      some (Nat.succ y + m)

tactic: simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
  evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂
state_before: case intro.intro.intro.succ.intro.intro.intro.intro
cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
k₁ : ℕ
hk₁ : a ∈ evaln (k₁ + 1) cf (Nat.pair (unpair n).fst m)
k₂ : ℕ
hk₂ : y + Nat.succ m ∈ evaln (k₂ + 1) (rfind' cf) (Nat.pair (unpair n).fst (Nat.succ m))
⊢ (if a = 0 then pure m else evaln (Nat.succ (max k₁ k₂)) (rfind' cf) (Nat.pair (unpair n).fst (m + 1))) =
    some (Nat.succ y + m)

tactic: simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁
state_before: cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
k₁ : ℕ
hk₁ : a ∈ evaln (k₁ + 1) cf (Nat.pair (unpair n).fst (0 + m))
⊢ 0 ∈ eval cf (Nat.pair (unpair n).fst (y + Nat.succ m))

tactic: simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
  hy₂ (Nat.succ_lt_succ hi)
state_before: cf : Code
hf : ∀ {n x : ℕ}, x ∈ eval cf n → ∃ k, x ∈ evaln (k + 1) cf n
n y✝ m✝ : ℕ
hy₁✝ : 0 ∈ eval cf (Nat.pair (unpair n).fst (y✝ + m✝))
hy₂✝ : ∀ {m : ℕ}, m < y✝ → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m + m✝)) ∧ ¬a = 0
y : ℕ
IH :
  ∀ (m : ℕ),
    0 ∈ eval cf (Nat.pair (unpair n).fst (y + m)) →
      (∀ {m_1 : ℕ}, m_1 < y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0) →
        ∃ k, y + m ∈ evaln (k + 1) (rfind' cf) (Nat.pair (unpair n).fst m)
m : ℕ
hy₁ : 0 ∈ eval cf (Nat.pair (unpair n).fst (Nat.succ y + m))
hy₂ : ∀ {m_1 : ℕ}, m_1 < Nat.succ y → ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (m_1 + m)) ∧ ¬a = 0
a : ℕ
ha : a ∈ eval cf (Nat.pair (unpair n).fst (0 + m))
a0 : ¬a = 0
k₁ : ℕ
hk₁ : a ∈ evaln (k₁ + 1) cf (Nat.pair (unpair n).fst (0 + m))
i : ℕ
hi : i < y
⊢ ∃ a, a ∈ eval cf (Nat.pair (unpair n).fst (i + Nat.succ m)) ∧ ¬a = 0