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 BitVec.(shiftLeft_add_distrib, shiftLeft_ushiftRight) #5478

Merged
merged 12 commits into from
Sep 26, 2024
28 changes: 28 additions & 0 deletions src/Init/Data/BitVec/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,25 @@ theorem shiftRight_add {w : Nat} (x : BitVec w) (n m : Nat) :
ext i
simp [Nat.add_assoc n m i]

@[simp]
theorem shiftLeft_ushiftRight {x : BitVec w} {n : Nat}:
Copy link
Contributor

@bollu bollu Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
theorem shiftLeft_ushiftRight {x : BitVec w} {n : Nat}:
theorem shiftLeft_ushiftRight {x : BitVec w} {n : Nat} :

x >>> n <<< n = x &&& BitVec.allOnes w <<< n := by
tobiasgrosser marked this conversation as resolved.
Show resolved Hide resolved
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
induction n generalizing x
case zero =>
ext; simp
case succ n ih =>
rw [BitVec.shiftLeft_add, Nat.add_comm, BitVec.shiftRight_add, ih,
Nat.add_comm, BitVec.shiftLeft_add, BitVec.shiftLeft_and_distrib]
ext i
have hi₁ (_: 0 < i.val) : 1 + (i.val - 1) = i := by
rw [Nat.add_comm, Nat.sub_add_cancel]
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
omega
by_cases hw : w = 0
· simp [hw]
by_cases hi₂ : i.val = 0
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
· simp [hi₂]
· simp [Nat.lt_one_iff, hi₂, hi₁ (show 0 < i.val by omega)]
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved

@[deprecated shiftRight_add (since := "2024-06-02")]
theorem shiftRight_shiftRight {w : Nat} (x : BitVec w) (n m : Nat) :
(x >>> n) >>> m = x >>> (n + m) := by
Expand Down Expand Up @@ -1737,6 +1756,15 @@ theorem ofInt_add {n} (x y : Int) : BitVec.ofInt n (x + y) =
apply eq_of_toInt_eq
simp

@[simp]
theorem shiftLeft_add_distrib {x y : BitVec w} {n : Nat} :
(x + y) <<< n = x <<< n + y <<< n := by
induction n
case zero =>
simp
case succ n ih =>
simp [ih, toNat_eq, Nat.shiftLeft_eq_mul_pow, ←Nat.add_mul]
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved

/-! ### sub/neg -/

theorem sub_def {n} (x y : BitVec n) : x - y = .ofNat n ((2^n - y.toNat) + x.toNat) := by rfl
Expand Down
2 changes: 2 additions & 0 deletions src/Init/Data/Nat/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,8 @@ theorem lt_succ_of_lt (h : a < b) : a < succ b := le_succ_of_le h

theorem lt_add_one_of_lt (h : a < b) : a < b + 1 := le_succ_of_le h

theorem lt_one_iff : n < 1 ↔ n = 0 := Nat.lt_succ_iff.trans <| by rw [le_zero_eq]

theorem succ_pred_eq_of_ne_zero : ∀ {n}, n ≠ 0 → succ (pred n) = n
| _+1, _ => rfl

Expand Down
5 changes: 5 additions & 0 deletions src/Init/Data/Nat/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,9 @@ theorem add_mod (a b n : Nat) : (a + b) % n = ((a % n) + (b % n)) % n := by
| zero => simp_all
| succ k => omega

@[simp] theorem mod_mul_mod {a b c : Nat} : (a % c * b) % c = a * b % c := by
rw [mul_mod, mod_mod, ← mul_mod]

/-! ### pow -/

theorem pow_succ' {m n : Nat} : m ^ n.succ = m * m ^ n := by
Expand Down Expand Up @@ -916,6 +919,8 @@ theorem mul_add_mod (m x y : Nat) : (m * x + y) % m = y % m := by
· exact (m % 0).div_zero
· case succ n => exact Nat.div_eq_of_lt (m.mod_lt n.succ_pos)

theorem shiftLeft_eq_mul_pow (m) : ∀ n, m <<< n = m * 2 ^ n := shiftLeft_eq _
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved

/-! ### Decidability of predicates -/

instance decidableBallLT :
Expand Down
Loading