Skip to content

Commit 813c10a

Browse files
tobiasgrossereric-wieseralexkeizerChrisHughes24
committed
chore: remove Nat.bitwise' (#7451)
Building upon the proof that `Nat.bitwise` and `Nat.bitwise'` are equal (from #7410), this PR completely removes `bitwise'` and changes all uses to `bitwise` instead. In particular, `land'/lor'/lxor'` are replaced with the `bitwise`-based equivalent operations in core, which have overriden optimized implementations in the compiler. Co-authored-by: Eric Wieser <wieser.eric@gmail.com> Co-authored-by: Alex Keizer <alex@keizer.dev> Co-authored-by: Chris Hughes <chrishughes24@gmail.com>
1 parent 7891de9 commit 813c10a

File tree

7 files changed

+302
-376
lines changed

7 files changed

+302
-376
lines changed

Mathlib/Data/Bool/Basic.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ theorem or_not_self : ∀ x, (x || !x) = true := by decide
259259
theorem not_or_self : ∀ x, (!x || x) = true := by decide
260260
#align bool.bnot_bor_self Bool.not_or_self
261261

262+
theorem bne_eq_xor : bne = xor := by funext a b; revert a b; decide
263+
262264
theorem xor_comm : ∀ a b, xor a b = xor b a := by decide
263265
#align bool.bxor_comm Bool.xor_comm
264266

Mathlib/Data/Int/Bitwise.lean

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import Mathlib.Init.Data.Int.Bitwise
2222

2323
namespace Int
2424

25+
-- In the `Int` namespace, `xor` will inconveniently resolve to `Int.xor`.
26+
/-- `bxor` denotes the `xor` function, i.e. the exclusive-or function, on type `Bool`. -/
27+
local notation "bxor" => _root_.xor
28+
2529
/-! ### bitwise ops -/
2630

2731
@[simp]
@@ -44,8 +48,8 @@ theorem bodd_coe (n : ℕ) : Int.bodd n = Nat.bodd n :=
4448
#align int.bodd_coe Int.bodd_coe
4549

4650
@[simp]
47-
theorem bodd_subNatNat (m n : ℕ) : bodd (subNatNat m n) = xor m.bodd n.bodd := by
48-
apply subNatNat_elim m n fun m n i => bodd i = xor m.bodd n.bodd <;>
51+
theorem bodd_subNatNat (m n : ℕ) : bodd (subNatNat m n) = bxor m.bodd n.bodd := by
52+
apply subNatNat_elim m n fun m n i => bodd i = bxor m.bodd n.bodd <;>
4953
intros i j <;>
5054
simp only [Int.bodd, Int.bodd_coe, Nat.bodd_add] <;>
5155
cases Nat.bodd i <;> simp
@@ -72,7 +76,7 @@ theorem bodd_neg (n : ℤ) : bodd (-n) = bodd n := by
7276
#align int.bodd_neg Int.bodd_neg
7377

7478
@[simp]
75-
theorem bodd_add (m n : ℤ) : bodd (m + n) = xor (bodd m) (bodd n) := by
79+
theorem bodd_add (m n : ℤ) : bodd (m + n) = bxor (bodd m) (bodd n) := by
7680
cases' m with m m <;>
7781
cases' n with n n <;>
7882
simp only [ofNat_eq_coe, ofNat_add_negSucc, negSucc_add_ofNat,
@@ -221,13 +225,12 @@ theorem testBit_succ (m b) : ∀ n, testBit (bit b n) (Nat.succ m) = testBit n m
221225
theorem bitwise_or : bitwise or = lor := by
222226
funext m n
223227
cases' m with m m <;> cases' n with n n <;> try {rfl}
224-
<;> simp only [bitwise, natBitwise, Bool.not_false, Bool.or_true, cond_true, lor, Nat.ldiff',
225-
negSucc.injEq, Bool.true_or, Nat.land']
226-
· rw [Nat.bitwise'_swap, Function.swap]
228+
<;> simp only [bitwise, natBitwise, Bool.not_false, Bool.or_true, cond_true, lor, Nat.ldiff,
229+
negSucc.injEq, Bool.true_or, Nat.land]
230+
· rw [Nat.bitwise_swap, Function.swap]
227231
congr
228232
funext x y
229233
cases x <;> cases y <;> rfl
230-
rfl
231234
· congr
232235
funext x y
233236
cases x <;> cases y <;> rfl
@@ -241,45 +244,43 @@ theorem bitwise_and : bitwise and = land := by
241244
funext m n
242245
cases' m with m m <;> cases' n with n n <;> try {rfl}
243246
<;> simp only [bitwise, natBitwise, Bool.not_false, Bool.or_true,
244-
cond_false, cond_true, lor, Nat.ldiff', Bool.and_true, negSucc.injEq,
245-
Bool.and_false, Nat.land']
246-
· rw [Nat.bitwise'_swap, Function.swap]
247+
cond_false, cond_true, lor, Nat.ldiff, Bool.and_true, negSucc.injEq,
248+
Bool.and_false, Nat.land]
249+
· rw [Nat.bitwise_swap, Function.swap]
247250
congr
248251
funext x y
249252
cases x <;> cases y <;> rfl
250-
rfl
251253
· congr
252254
funext x y
253255
cases x <;> cases y <;> rfl
254256
#align int.bitwise_and Int.bitwise_and
255257

256258
--Porting note : Was `bitwise_tac` in mathlib
257-
theorem bitwise_diff : (bitwise fun a b => a && not b) = ldiff' := by
259+
theorem bitwise_diff : (bitwise fun a b => a && not b) = ldiff := by
258260
funext m n
259261
cases' m with m m <;> cases' n with n n <;> try {rfl}
260262
<;> simp only [bitwise, natBitwise, Bool.not_false, Bool.or_true,
261-
cond_false, cond_true, lor, Nat.ldiff', Bool.and_true, negSucc.injEq,
262-
Bool.and_false, Nat.land', Bool.not_true, ldiff', Nat.lor']
263+
cond_false, cond_true, lor, Nat.ldiff, Bool.and_true, negSucc.injEq,
264+
Bool.and_false, Nat.land, Bool.not_true, ldiff, Nat.lor]
263265
· congr
264266
funext x y
265267
cases x <;> cases y <;> rfl
266268
· congr
267269
funext x y
268270
cases x <;> cases y <;> rfl
269-
· rw [Nat.bitwise'_swap, Function.swap]
271+
· rw [Nat.bitwise_swap, Function.swap]
270272
congr
271273
funext x y
272274
cases x <;> cases y <;> rfl
273-
rfl
274275
#align int.bitwise_diff Int.bitwise_diff
275276

276277
--Porting note : Was `bitwise_tac` in mathlib
277-
theorem bitwise_xor : bitwise xor = lxor' := by
278+
theorem bitwise_xor : bitwise bxor = xor := by
278279
funext m n
279280
cases' m with m m <;> cases' n with n n <;> try {rfl}
280-
<;> simp only [bitwise, natBitwise, Bool.not_false, Bool.or_true,
281-
cond_false, cond_true, lor, Nat.ldiff', Bool.and_true, negSucc.injEq, Bool.false_xor,
282-
Bool.true_xor, Bool.and_false, Nat.land', Bool.not_true, ldiff', Nat.lor', lxor', Nat.lxor']
281+
<;> simp only [bitwise, natBitwise, Bool.not_false, Bool.or_true, Bool.bne_eq_xor,
282+
cond_false, cond_true, lor, Nat.ldiff, Bool.and_true, negSucc.injEq, Bool.false_xor,
283+
Bool.true_xor, Bool.and_false, Nat.land, Bool.not_true, ldiff, Nat.lor, xor, Nat.xor]
283284
· congr
284285
funext x y
285286
cases x <;> cases y <;> rfl
@@ -314,12 +315,12 @@ theorem land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n)
314315
#align int.land_bit Int.land_bit
315316

316317
@[simp]
317-
theorem ldiff_bit (a m b n) : ldiff' (bit a m) (bit b n) = bit (a && not b) (ldiff' m n) := by
318+
theorem ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && not b) (ldiff m n) := by
318319
rw [← bitwise_diff, bitwise_bit]
319320
#align int.ldiff_bit Int.ldiff_bit
320321

321322
@[simp]
322-
theorem lxor_bit (a m b n) : lxor' (bit a m) (bit b n) = bit (xor a b) (lxor' m n) := by
323+
theorem lxor_bit (a m b n) : xor (bit a m) (bit b n) = bit (bxor a b) (xor m n) := by
323324
rw [← bitwise_xor, bitwise_bit]
324325
#align int.lxor_bit Int.lxor_bit
325326

@@ -350,12 +351,12 @@ theorem testBit_land (m n k) : testBit (land m n) k = (testBit m k && testBit n
350351
#align int.test_bit_land Int.testBit_land
351352

352353
@[simp]
353-
theorem testBit_ldiff (m n k) : testBit (ldiff' m n) k = (testBit m k && not (testBit n k)) := by
354+
theorem testBit_ldiff (m n k) : testBit (ldiff m n) k = (testBit m k && not (testBit n k)) := by
354355
rw [← bitwise_diff, testBit_bitwise]
355356
#align int.test_bit_ldiff Int.testBit_ldiff
356357

357358
@[simp]
358-
theorem testBit_lxor (m n k) : testBit (lxor' m n) k = xor (testBit m k) (testBit n k) := by
359+
theorem testBit_lxor (m n k) : testBit (xor m n) k = bxor (testBit m k) (testBit n k) := by
359360
rw [← bitwise_xor, testBit_bitwise]
360361
#align int.test_bit_lxor Int.testBit_lxor
361362

0 commit comments

Comments
 (0)