Skip to content

Commit 21fd553

Browse files
committed
chore: cleanup in Nat bitwise files (#29929)
1 parent 725b6fa commit 21fd553

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

Mathlib/Computability/PartrecCode.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
162162
have IH := encode_ofNatCode m
163163
have IH1 := encode_ofNatCode m.unpair.1
164164
have IH2 := encode_ofNatCode m.unpair.2
165-
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
165+
conv_rhs => rw [← Nat.bit_bodd_div2 n, ← Nat.bit_bodd_div2 n.div2]
166166
simp only [ofNatCode.eq_5]
167167
cases n.bodd <;> cases n.div2.bodd <;>
168168
simp [m, encodeCode, IH, IH1, IH2, Nat.bit_val]

Mathlib/Data/Nat/Bits.lean

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ lemma div2_succ (n : ℕ) : div2 (n + 1) = cond (bodd n) (succ (div2 n)) (div2 n
9090
simp only [bodd, boddDiv2, div2]
9191
rcases boddDiv2 n with ⟨_ |_, _⟩ <;> simp
9292

93-
attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc
93+
attribute [local simp] Nat.add_comm Nat.mul_comm
9494

9595
lemma bodd_add_div2 : ∀ n, (bodd n).toNat + 2 * div2 n = n
9696
| 0 => rfl
@@ -106,12 +106,19 @@ lemma div2_val (n) : div2 n = n / 2 := by
106106
(Nat.add_left_cancel (Eq.trans ?_ (Nat.mod_add_div n 2).symm))
107107
rw [mod_two_of_bodd, bodd_add_div2]
108108

109-
lemma bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n :=
109+
@[simp, grind =]
110+
lemma bit_bodd_div2 (n : Nat) : bit (bodd n) (div2 n) = n :=
110111
(bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _
111112

112-
lemma bit_zero : bit false 0 = 0 :=
113+
@[deprecated (since := "2025-09-24")]
114+
alias bit_decomp := bit_bodd_div2
115+
116+
lemma bit_false_zero : bit false 0 = 0 :=
113117
rfl
114118

119+
@[deprecated (since := "2025-09-24")]
120+
alias bit_zero := bit_false_zero
121+
115122
/-- `shiftLeft' b m n` performs a left shift of `m` `n` times
116123
and adds the bit `b` as the least significant bit each time.
117124
Returns the corresponding natural number -/
@@ -156,12 +163,14 @@ def ldiff : ℕ → ℕ → ℕ :=
156163

157164
/-! bitwise ops -/
158165

166+
@[simp, grind =]
159167
lemma bodd_bit (b n) : bodd (bit b n) = b := by
160168
rw [bit_val]
161169
simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false,
162170
Bool.not_true, Bool.and_false, Bool.xor_false]
163171
cases b <;> cases bodd n <;> rfl
164172

173+
@[simp, grind =]
165174
lemma div2_bit (b n) : div2 (bit b n) = n := by
166175
rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add]
167176
<;> cases b
@@ -197,7 +206,7 @@ lemma testBit_bit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by
197206
/-! ### `boddDiv2_eq` and `bodd` -/
198207

199208

200-
@[simp]
209+
@[simp, grind =]
201210
theorem boddDiv2_eq (n : ℕ) : boddDiv2 n = (bodd n, div2 n) := rfl
202211

203212
@[simp]

Mathlib/Data/Nat/Bitwise.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ lemma bitwise_of_ne_zero {n m : Nat} (hn : n ≠ 0) (hm : m ≠ 0) :
6868

6969
theorem binaryRec_of_ne_zero {C : Nat → Sort*} (z : C 0) (f : ∀ b n, C n → C (bit b n)) {n}
7070
(h : n ≠ 0) :
71-
binaryRec z f n = bit_decomp n ▸ f (bodd n) (div2 n) (binaryRec z f (div2 n)) := by
71+
binaryRec z f n = bit_bodd_div2 n ▸ f (bodd n) (div2 n) (binaryRec z f (div2 n)) := by
7272
cases n using bitCasesOn with
7373
| bit b n =>
7474
rw [binaryRec_eq _ _ (by right; simpa [bit_eq_zero_iff] using h)]
@@ -189,7 +189,7 @@ theorem testBit_eq_inth (n i : ℕ) : n.testBit i = n.bits.getI i := by
189189
bodd_eq_bits_head, List.getI_zero_eq_headI]
190190
cases List.headI (bits n) <;> rfl
191191
| succ i ih =>
192-
conv_lhs => rw [← bit_decomp n]
192+
conv_lhs => rw [← bit_bodd_div2 n]
193193
rw [testBit_bit_succ, ih n.div2, div2_bits_eq_tail]
194194
cases n.bits <;> simp
195195

Mathlib/Logic/Denumerable.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ instance option : Denumerable (Option α) :=
119119
/-- If `α` and `β` are denumerable, then so is their sum. -/
120120
instance sum : Denumerable (α ⊕ β) :=
121121
fun n => by
122-
suffices ∃ a ∈ @decodeSum α β _ _ n, encodeSum a = bit (bodd n) (div2 n) by simpa [bit_decomp]
122+
suffices ∃ a ∈ @decodeSum α β _ _ n, encodeSum a = bit (bodd n) (div2 n) by
123+
simpa [bit_bodd_div2]
123124
simp only [decodeSum, boddDiv2_eq, decode_eq_ofNat, Option.map_some,
124125
Option.mem_def, Sum.exists]
125126
cases bodd n <;> simp [bit, encodeSum, Nat.two_mul]⟩

Mathlib/Logic/Equiv/Nat.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ variable {α : Type*}
2727
def boolProdNatEquivNat : Bool × ℕ ≃ ℕ where
2828
toFun := uncurry bit
2929
invFun := boddDiv2
30-
left_inv := fun ⟨b, n⟩ => by simp only [bodd_bit, div2_bit, uncurry_apply_pair, boddDiv2_eq]
31-
right_inv n := by simp only [bit_decomp, boddDiv2_eq, uncurry_apply_pair]
30+
left_inv := fun ⟨b, n⟩ => by simp
31+
right_inv n := by simp
3232

3333
/-- An equivalence between `ℕ ⊕ ℕ` and `ℕ`, by mapping `(Sum.inl x)` to `2 * x` and `(Sum.inr x)` to
3434
`2 * x + 1`.

0 commit comments

Comments
 (0)