Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Nat.[shiftLeft_or_distrib, shiftLeft_xor_distrib, shiftLeft_and_distrib, testBit_mul_two_pow, testBit_mul_two_pow_gt, testBit_mul_two_pow_le, bitwise_mul_two_pow, shiftLeft_bitwise_distrib] #6630

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Init/Data/Nat/Bitwise/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@ theorem testBit_add (x i n : Nat) : testBit x (i + n) = testBit (x / 2 ^ n) i :=
rw [← Nat.add_assoc, testBit_add_one, ih (x / 2),
Nat.pow_succ, Nat.div_div_eq_div_mul, Nat.mul_comm]

theorem testBit_mul_two_pow (x i n : Nat) :
testBit (x * 2 ^ n) i = if n ≤ i then testBit x (i - n) else false := by
simp only [testBit, one_and_eq_mod_two, mod_two_bne_zero, Bool.if_false_right]
by_cases hni : n ≤ i
· simp only [hni, decide_true, Bool.true_and]
congr 2
let j := i - n
have hj : (x * 2 ^ n) >>> i = (x * 2 ^ n) >>> (j + n) := by simp only [j]; rw [Nat.sub_add_cancel]; omega
have hj' : x >>> (i - n) = x >>> j := by simp only [j]
rw [hj, hj', ← shiftLeft_eq, Nat.add_comm, shiftRight_add, shiftLeft_shiftRight]
· simp only [hni, decide_false, Bool.false_and, beq_eq_false_iff_ne, ne_eq]
simp only [Nat.not_le] at hni
rw [← shiftLeft_eq]
let k := n - i
have hk : x <<< n >>> i = x <<< (k + i) >>> i := by simp only [k]; rw [Nat.sub_add_cancel]; omega
rw [hk, Nat.shiftLeft_add, Nat.shiftLeft_shiftRight, shiftLeft_eq]
have hk' : 0 < k := by omega
have hx : 2 * (x * 2 ^ k / 2) = x * 2 ^ k := by
rw [Nat.mul_comm, Nat.div_mul_cancel]
suffices hs : 2 * (x * 2 ^ (k - 1)) = x * 2 ^ k by
exact ⟨_, hs.symm⟩
let j := k - 1
have hj : 2 ^ (k - 1) = 2 ^ j := by simp only [j]
have hj' : 2 ^ k = 2 ^ (j + 1) := by simp only [j]; rw [Nat.sub_add_cancel]; omega
rw [hj, hj']
simp only [Nat.pow_add, Nat.pow_one, j, k]
rw [Nat.mul_comm, Nat.mul_assoc]
omega
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved

theorem testBit_div_two (x i : Nat) : testBit (x / 2) i = testBit x (i + 1) := by
simp

Expand Down Expand Up @@ -452,6 +481,15 @@ theorem bitwise_lt_two_pow (left : x < 2^n) (right : y < 2^n) : (Nat.bitwise f x
case neg =>
apply Nat.add_lt_add <;> exact hyp1

theorem bitwise_mul_two_pow (of_false_false : f false false = false := by rfl) :
(bitwise f x y) * 2 ^ n = bitwise f (x * 2 ^ n) (y * 2 ^ n) := by
apply Nat.eq_of_testBit_eq
simp only [testBit_mul_two_pow, testBit_bitwise of_false_false, Bool.if_false_right]
intro i
by_cases hn : n ≤ i
· simp [hn]
· simp [hn, of_false_false]
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved

theorem bitwise_div_two_pow (of_false_false : f false false = false := by rfl) :
(bitwise f x y) / 2 ^ n = bitwise f (x / 2 ^ n) (y / 2 ^ n) := by
apply Nat.eq_of_testBit_eq
Expand Down Expand Up @@ -711,6 +749,13 @@ theorem mul_add_lt_is_or {b : Nat} (b_lt : b < 2^i) (a : Nat) : 2^i * a + b = 2^
rw [mod_two_eq_one_iff_testBit_zero, testBit_shiftLeft]
simp

theorem shiftLeft_bitwise_distrib {a b : Nat} (of_false_false : f false false = false := by rfl) :
(bitwise f a b) <<< i = bitwise f (a <<< i) (b <<< i) := by
simp [shiftLeft_eq, bitwise_mul_two_pow of_false_false]

theorem shiftLeft_or_distrib {a b : Nat} : (a ||| b) <<< i = a <<< i ||| b <<< i :=
shiftLeft_bitwise_distrib

luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
@[simp] theorem decide_shiftRight_mod_two_eq_one :
decide (x >>> i % 2 = 1) = x.testBit i := by
simp only [testBit, one_and_eq_mod_two, mod_two_bne_zero]
Expand Down
Loading