Theorem: Nat.exists_most_significant_bit
File: Mathlib/Data/Nat/Bitwise.lean

tactic: induction' n using Nat.binaryRec with b n hn
state_before: n : ℕ
h : n ≠ 0
⊢ ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false

tactic: by_cases h' : n = 0
state_before: case f
n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
⊢ ∃ i, testBit (bit b n) i = true ∧ ∀ (j : ℕ), i < j → testBit (bit b n) j = false

tactic: exact False.elim (h rfl)
state_before: case z
n : ℕ
h✝ : n ≠ 0
h : 0 ≠ 0
⊢ ∃ i, testBit 0 i = true ∧ ∀ (j : ℕ), i < j → testBit 0 j = false

tactic: subst h'
state_before: case pos
n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
h' : n = 0
⊢ ∃ i, testBit (bit b n) i = true ∧ ∀ (j : ℕ), i < j → testBit (bit b n) j = false

tactic: rw [show b = true by
    revert h
    cases b <;> simp]
state_before: case pos
n : ℕ
h✝ : n ≠ 0
b : Bool
hn : 0 ≠ 0 → ∃ i, testBit 0 i = true ∧ ∀ (j : ℕ), i < j → testBit 0 j = false
h : bit b 0 ≠ 0
⊢ ∃ i, testBit (bit b 0) i = true ∧ ∀ (j : ℕ), i < j → testBit (bit b 0) j = false

tactic: refine' ⟨0, ⟨by rw [testBit_zero], fun j hj => _⟩⟩
state_before: case pos
n : ℕ
h✝ : n ≠ 0
b : Bool
hn : 0 ≠ 0 → ∃ i, testBit 0 i = true ∧ ∀ (j : ℕ), i < j → testBit 0 j = false
h : bit b 0 ≠ 0
⊢ ∃ i, testBit (bit true 0) i = true ∧ ∀ (j : ℕ), i < j → testBit (bit true 0) j = false

tactic: obtain ⟨j', rfl⟩ := exists_eq_succ_of_ne_zero (ne_of_gt hj)
state_before: case pos
n : ℕ
h✝ : n ≠ 0
b : Bool
hn : 0 ≠ 0 → ∃ i, testBit 0 i = true ∧ ∀ (j : ℕ), i < j → testBit 0 j = false
h : bit b 0 ≠ 0
j : ℕ
hj : 0 < j
⊢ testBit (bit true 0) j = false

tactic: rw [testBit_succ, zero_testBit]
state_before: case pos.intro
n : ℕ
h✝ : n ≠ 0
b : Bool
hn : 0 ≠ 0 → ∃ i, testBit 0 i = true ∧ ∀ (j : ℕ), i < j → testBit 0 j = false
h : bit b 0 ≠ 0
j' : ℕ
hj : 0 < succ j'
⊢ testBit (bit true 0) (succ j') = false

tactic: revert h
state_before: n : ℕ
h✝ : n ≠ 0
b : Bool
hn : 0 ≠ 0 → ∃ i, testBit 0 i = true ∧ ∀ (j : ℕ), i < j → testBit 0 j = false
h : bit b 0 ≠ 0
⊢ b = true

tactic: cases b <;> simp
state_before: n : ℕ
h : n ≠ 0
b : Bool
hn : 0 ≠ 0 → ∃ i, testBit 0 i = true ∧ ∀ (j : ℕ), i < j → testBit 0 j = false
⊢ bit b 0 ≠ 0 → b = true

tactic: rw [testBit_zero]
state_before: n : ℕ
h✝ : n ≠ 0
b : Bool
hn : 0 ≠ 0 → ∃ i, testBit 0 i = true ∧ ∀ (j : ℕ), i < j → testBit 0 j = false
h : bit b 0 ≠ 0
⊢ testBit (bit true 0) 0 = true

tactic: obtain ⟨k, ⟨hk, hk'⟩⟩ := hn h'
state_before: case neg
n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
h' : ¬n = 0
⊢ ∃ i, testBit (bit b n) i = true ∧ ∀ (j : ℕ), i < j → testBit (bit b n) j = false

tactic: refine' ⟨k + 1, ⟨by rw [testBit_succ, hk], fun j hj => _⟩⟩
state_before: case neg.intro.intro
n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
h' : ¬n = 0
k : ℕ
hk : testBit n k = true
hk' : ∀ (j : ℕ), k < j → testBit n j = false
⊢ ∃ i, testBit (bit b n) i = true ∧ ∀ (j : ℕ), i < j → testBit (bit b n) j = false

tactic: obtain ⟨j', rfl⟩ := exists_eq_succ_of_ne_zero (show j ≠ 0 by intro x; subst x; simp at hj)
state_before: case neg.intro.intro
n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
h' : ¬n = 0
k : ℕ
hk : testBit n k = true
hk' : ∀ (j : ℕ), k < j → testBit n j = false
j : ℕ
hj : k + 1 < j
⊢ testBit (bit b n) j = false

tactic: exact (testBit_succ _ _ _).trans (hk' _ (lt_of_succ_lt_succ hj))
state_before: case neg.intro.intro.intro
n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
h' : ¬n = 0
k : ℕ
hk : testBit n k = true
hk' : ∀ (j : ℕ), k < j → testBit n j = false
j' : ℕ
hj : k + 1 < succ j'
⊢ testBit (bit b n) (succ j') = false

tactic: rw [testBit_succ, hk]
state_before: n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
h' : ¬n = 0
k : ℕ
hk : testBit n k = true
hk' : ∀ (j : ℕ), k < j → testBit n j = false
⊢ testBit (bit b n) (k + 1) = true

tactic: intro x
state_before: n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
h' : ¬n = 0
k : ℕ
hk : testBit n k = true
hk' : ∀ (j : ℕ), k < j → testBit n j = false
j : ℕ
hj : k + 1 < j
⊢ j ≠ 0

tactic: subst x
state_before: n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
h' : ¬n = 0
k : ℕ
hk : testBit n k = true
hk' : ∀ (j : ℕ), k < j → testBit n j = false
j : ℕ
hj : k + 1 < j
x : j = 0
⊢ False

tactic: simp at hj
state_before: n✝ : ℕ
h✝ : n✝ ≠ 0
b : Bool
n : ℕ
hn : n ≠ 0 → ∃ i, testBit n i = true ∧ ∀ (j : ℕ), i < j → testBit n j = false
h : bit b n ≠ 0
h' : ¬n = 0
k : ℕ
hk : testBit n k = true
hk' : ∀ (j : ℕ), k < j → testBit n j = false
hj : k + 1 < 0
⊢ False