Skip to content

Commit 83d7426

Browse files
committed
refactor(Data/Int/Bitwise): use <<< and >>> notation (#6789)
The lemma names have changed to use `shiftLeft` and `shiftRight` to match the new `Nat` names.
1 parent 47db6c4 commit 83d7426

File tree

2 files changed

+51
-47
lines changed

2 files changed

+51
-47
lines changed

Mathlib/Data/Int/Bitwise.lean

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -365,96 +365,99 @@ theorem testBit_lnot : ∀ n k, testBit (lnot n) k = not (testBit n k)
365365
#align int.test_bit_lnot Int.testBit_lnot
366366

367367
@[simp]
368-
theorem shiftl_neg (m n : ℤ) : shiftl m (-n) = shiftr m n :=
368+
theorem shiftLeft_neg (m n : ℤ) : m <<< (-n) = m >>> n :=
369369
rfl
370-
#align int.shiftl_neg Int.shiftl_neg
370+
#align int.shiftl_neg Int.shiftLeft_neg
371371

372372
@[simp]
373-
theorem shiftr_neg (m n : ℤ) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg]
374-
#align int.shiftr_neg Int.shiftr_neg
373+
theorem shiftRight_neg (m n : ℤ) : m >>> (-n) = m <<< n := by rw [← shiftLeft_neg, neg_neg]
374+
#align int.shiftr_neg Int.shiftRight_neg
375375

376376
-- Porting note: what's the correct new name?
377377
@[simp]
378-
theorem shiftl_coe_nat (m n : ℕ) : shiftl m n = m <<< n :=
379-
by simp [shiftl]
380-
#align int.shiftl_coe_nat Int.shiftl_coe_nat
378+
theorem shiftLeft_coe_nat (m n : ℕ) : (m : ℤ) <<< (n : ℤ) = ↑(m <<< n) :=
379+
by simp [instShiftLeftInt, HShiftLeft.hShiftLeft]
380+
#align int.shiftl_coe_nat Int.shiftLeft_coe_nat
381381

382382
-- Porting note: what's the correct new name?
383383
@[simp]
384-
theorem shiftr_coe_nat (m n : ℕ) : shiftr m n = m >>> n := by cases n <;> rfl
385-
#align int.shiftr_coe_nat Int.shiftr_coe_nat
384+
theorem shiftRight_coe_nat (m n : ℕ) : (m : ℤ) >>> (n : ℤ) = m >>> n := by cases n <;> rfl
385+
#align int.shiftr_coe_nat Int.shiftRight_coe_nat
386386

387387
@[simp]
388-
theorem shiftl_negSucc (m n : ℕ) : shiftl -[m+1] n = -[Nat.shiftLeft' true m n+1] :=
388+
theorem shiftLeft_negSucc (m n : ℕ) : -[m+1] <<< (n : ℤ) = -[Nat.shiftLeft' true m n+1] :=
389389
rfl
390-
#align int.shiftl_neg_succ Int.shiftl_negSucc
390+
#align int.shiftl_neg_succ Int.shiftLeft_negSucc
391391

392392
@[simp]
393-
theorem shiftr_negSucc (m n : ℕ) : shiftr -[m+1] n = -[m >>> n+1] := by cases n <;> rfl
394-
#align int.shiftr_neg_succ Int.shiftr_negSucc
393+
theorem shiftRight_negSucc (m n : ℕ) : -[m+1] >>> (n : ℤ) = -[m >>> n+1] := by cases n <;> rfl
394+
#align int.shiftr_neg_succ Int.shiftRight_negSucc
395395

396-
theorem shiftr_add : ∀ (m : ℤ) (n k : ℕ), shiftr m (n + k) = shiftr (shiftr m n) k
396+
theorem shiftRight_add : ∀ (m : ℤ) (n k : ℕ), m >>> (n + k : ℤ) = (m >>> (n : ℤ)) >>> (k : ℤ)
397397
| (m : ℕ), n, k => by
398-
rw [shiftr_coe_nat, shiftr_coe_nat, ← Int.ofNat_add, shiftr_coe_nat, Nat.shiftRight_add]
398+
rw [shiftRight_coe_nat, shiftRight_coe_nat, ← Int.ofNat_add, shiftRight_coe_nat,
399+
Nat.shiftRight_add]
399400
| -[m+1], n, k => by
400-
rw [shiftr_negSucc, shiftr_negSucc, ← Int.ofNat_add, shiftr_negSucc, Nat.shiftRight_add]
401-
#align int.shiftr_add Int.shiftr_add
401+
rw [shiftRight_negSucc, shiftRight_negSucc, ← Int.ofNat_add, shiftRight_negSucc,
402+
Nat.shiftRight_add]
403+
#align int.shiftr_add Int.shiftRight_add
402404

403405
/-! ### bitwise ops -/
404406

405407
attribute [local simp] Int.zero_div
406408

407-
theorem shiftl_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), shiftl m (n + k) = shiftl (shiftl m n) k
409+
theorem shiftLeft_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), m <<< (n + k) = (m <<< (n : ℤ)) <<< k
408410
| (m : ℕ), n, (k : ℕ) =>
409411
congr_arg ofNat (by simp [Nat.pow_add, mul_assoc])
410412
| -[m+1], n, (k : ℕ) => congr_arg negSucc (Nat.shiftLeft'_add _ _ _ _)
411413
| (m : ℕ), n, -[k+1] =>
412-
subNatNat_elim n k.succ (fun n k i => shiftl (↑m) i = (Nat.shiftLeft' false m n) >>> k)
414+
subNatNat_elim n k.succ (fun n k i => (↑m) <<< i = (Nat.shiftLeft' false m n) >>> k)
413415
(fun (i n : ℕ) =>
414416
by dsimp; simp [- Nat.shiftLeft_eq, ← Nat.shiftLeft_sub _ , add_tsub_cancel_left])
415-
fun i n =>
416-
by dsimp; simp [- Nat.shiftLeft_eq, Nat.shiftLeft_zero, Nat.shiftRight_add,
417-
← Nat.shiftLeft_sub, shiftl]
417+
fun i n => by
418+
dsimp
419+
simp [- Nat.shiftLeft_eq, Nat.shiftLeft_zero, Nat.shiftRight_add, ← Nat.shiftLeft_sub]
420+
rfl
418421
| -[m+1], n, -[k+1] =>
419422
subNatNat_elim n k.succ
420-
(fun n k i => shiftl -[m+1] i = -[(Nat.shiftLeft' true m n) >>> k+1])
423+
(fun n k i => -[m+1] <<< i = -[(Nat.shiftLeft' true m n) >>> k+1])
421424
(fun i n =>
422425
congr_arg negSucc <| by
423426
rw [← Nat.shiftLeft'_sub, add_tsub_cancel_left]; apply Nat.le_add_right)
424427
fun i n =>
425428
congr_arg negSucc <| by rw [add_assoc, Nat.shiftRight_add, ← Nat.shiftLeft'_sub, tsub_self]
426429
<;> rfl
427-
#align int.shiftl_add Int.shiftl_add
430+
#align int.shiftl_add Int.shiftLeft_add
428431

429-
theorem shiftl_sub (m : ℤ) (n : ℕ) (k : ℤ) : shiftl m (n - k) = shiftr (shiftl m n) k :=
430-
shiftl_add _ _ _
431-
#align int.shiftl_sub Int.shiftl_sub
432+
theorem shiftLeft_sub (m : ℤ) (n : ℕ) (k : ℤ) : m <<< (n - k) = (m <<< (n : ℤ)) >>> k :=
433+
shiftLeft_add _ _ _
434+
#align int.shiftl_sub Int.shiftLeft_sub
432435

433-
theorem shiftl_eq_mul_pow : ∀ (m : ℤ) (n : ℕ), shiftl m n = m * ↑(2 ^ n)
436+
theorem shiftLeft_eq_mul_pow : ∀ (m : ℤ) (n : ℕ), m <<< (n : ℤ) = m * ↑(2 ^ n)
434437
| (m : ℕ), _ => congr_arg ((↑) : ℕ → ℤ) (by simp)
435438
| -[_+1], _ => @congr_arg ℕ ℤ _ _ (fun i => -i) (Nat.shiftLeft'_tt_eq_mul_pow _ _)
436-
#align int.shiftl_eq_mul_pow Int.shiftl_eq_mul_pow
439+
#align int.shiftl_eq_mul_pow Int.shiftLeft_eq_mul_pow
437440

438-
theorem shiftr_eq_div_pow : ∀ (m : ℤ) (n : ℕ), shiftr m n = m / ↑(2 ^ n)
439-
| (m : ℕ), n => by rw [shiftr_coe_nat, Nat.shiftRight_eq_div_pow _ _]; simp
441+
theorem shiftRight_eq_div_pow : ∀ (m : ℤ) (n : ℕ), m >>> (n : ℤ) = m / ↑(2 ^ n)
442+
| (m : ℕ), n => by rw [shiftRight_coe_nat, Nat.shiftRight_eq_div_pow _ _]; simp
440443
| -[m+1], n => by
441-
rw [shiftr_negSucc, negSucc_ediv, Nat.shiftRight_eq_div_pow]; rfl
444+
rw [shiftRight_negSucc, negSucc_ediv, Nat.shiftRight_eq_div_pow]; rfl
442445
exact ofNat_lt_ofNat_of_lt (pow_pos (by decide) _)
443-
#align int.shiftr_eq_div_pow Int.shiftr_eq_div_pow
446+
#align int.shiftr_eq_div_pow Int.shiftRight_eq_div_pow
444447

445-
theorem one_shiftl (n : ℕ) : shiftl 1 n = (2 ^ n : ℕ) :=
448+
theorem one_shiftLeft (n : ℕ) : 1 <<< (n : ℤ) = (2 ^ n : ℕ) :=
446449
congr_arg ((↑) : ℕ → ℤ) (by simp)
447-
#align int.one_shiftl Int.one_shiftl
450+
#align int.one_shiftl Int.one_shiftLeft
448451

449452
@[simp]
450-
theorem zero_shiftl : ∀ n : ℤ, shiftl 0 n = 0
453+
theorem zero_shiftLeft : ∀ n : ℤ, 0 <<< n = 0
451454
| (n : ℕ) => congr_arg ((↑) : ℕ → ℤ) (by simp)
452455
| -[_+1] => congr_arg ((↑) : ℕ → ℤ) (by simp)
453-
#align int.zero_shiftl Int.zero_shiftl
456+
#align int.zero_shiftl Int.zero_shiftLeft
454457

455458
@[simp]
456-
theorem zero_shiftr (n) : shiftr 0 n = 0 :=
457-
zero_shiftl _
458-
#align int.zero_shiftr Int.zero_shiftr
459+
theorem zero_shiftRight (n : ℤ) : 0 >>> n = 0 :=
460+
zero_shiftLeft _
461+
#align int.zero_shiftr Int.zero_shiftRight
459462

460463
end Int

Mathlib/Init/Data/Int/Bitwise.lean

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,20 @@ def lxor' : ℤ → ℤ → ℤ
101101
| -[m +1], -[n +1] => Nat.lxor' m n
102102
#align int.lxor Int.lxor'
103103

104-
/-- `shiftl m n` produces an integer whose binary representation
104+
/-- `m <<< n` produces an integer whose binary representation
105105
is obtained by left-shifting the binary representation of `m` by `n` places -/
106-
def shiftl : ℤ → ℤ → ℤ
106+
instance : ShiftLeft ℤ where
107+
shiftLeft
107108
| (m : ℕ), (n : ℕ) => Nat.shiftLeft' false m n
108109
| (m : ℕ), -[n +1] => m >>> (Nat.succ n)
109110
| -[m +1], (n : ℕ) => -[Nat.shiftLeft' true m n +1]
110111
| -[m +1], -[n +1] => -[m >>> (Nat.succ n) +1]
111-
#align int.shiftl Int.shiftl
112+
#align int.shiftl ShiftLeft.shiftLeft
112113

113-
/-- `shiftr m n` produces an integer whose binary representation
114+
/-- `m >>> n` produces an integer whose binary representation
114115
is obtained by right-shifting the binary representation of `m` by `n` places -/
115-
def shiftr (m n : ℤ) : ℤ :=
116-
shiftl m (-n)
117-
#align int.shiftr Int.shiftr
116+
instance : ShiftRight ℤ where
117+
shiftRight m n := m <<< (-n)
118+
#align int.shiftr ShiftRight.shiftRight
118119

119120
end Int

0 commit comments

Comments
 (0)