Skip to content

Commit 37f9063

Browse files
feat: add BitVec.neg_neg (#4977)
.. as well as neg_neq_iff_neq_neg. --------- Co-authored-by: Henrik Böving <hargonix@gmail.com>
1 parent 8364c3e commit 37f9063

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Init/Data/BitVec/Lemmas.lean

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ theorem toNat_zero (n : Nat) : (0#n).toNat = 0 := by trivial
162162
@[simp] theorem toNat_mod_cancel (x : BitVec n) : x.toNat % (2^n) = x.toNat :=
163163
Nat.mod_eq_of_lt x.isLt
164164

165+
@[simp] theorem sub_toNat_mod_cancel {x : BitVec w} (h : ¬ x = 0#w) :
166+
(2 ^ w - x.toNat) % 2 ^ w = 2 ^ w - x.toNat := by
167+
simp only [toNat_eq, toNat_ofNat, Nat.zero_mod] at h
168+
rw [Nat.mod_eq_of_lt (by omega)]
169+
170+
@[simp] theorem sub_sub_toNat_cancel {x : BitVec w} :
171+
2 ^ w - (2 ^ w - x.toNat) = x.toNat := by
172+
simp [Nat.sub_sub_eq_min, Nat.min_eq_right]
173+
omega
174+
165175
private theorem lt_two_pow_of_le {x m n : Nat} (lt : x < 2 ^ m) (le : m ≤ n) : x < 2 ^ n :=
166176
Nat.lt_of_lt_of_le lt (Nat.pow_le_pow_of_le_right (by trivial : 0 < 2) le)
167177

@@ -300,8 +310,7 @@ theorem truncate_eq_zeroExtend {v : Nat} {x : BitVec w} :
300310

301311
@[simp, bv_toNat] theorem toNat_zeroExtend' {m n : Nat} (p : m ≤ n) (x : BitVec m) :
302312
(zeroExtend' p x).toNat = x.toNat := by
303-
unfold zeroExtend'
304-
simp [p, x.isLt, Nat.mod_eq_of_lt]
313+
simp [zeroExtend']
305314

306315
@[bv_toNat] theorem toNat_zeroExtend (i : Nat) (x : BitVec n) :
307316
BitVec.toNat (zeroExtend i x) = x.toNat % 2^i := by
@@ -1264,6 +1273,19 @@ theorem neg_eq_not_add (x : BitVec w) : -x = ~~~x + 1 := by
12641273
have hx : x.toNat < 2^w := x.isLt
12651274
rw [Nat.sub_sub, Nat.add_comm 1 x.toNat, ← Nat.sub_sub, Nat.sub_add_cancel (by omega)]
12661275

1276+
@[simp]
1277+
theorem neg_neg {x : BitVec w} : - - x = x := by
1278+
by_cases h : x = 0#w
1279+
· simp [h]
1280+
· simp [bv_toNat, h]
1281+
1282+
theorem neg_ne_iff_ne_neg {x y : BitVec w} : -x ≠ y ↔ x ≠ -y := by
1283+
constructor
1284+
all_goals
1285+
intro h h'
1286+
subst h'
1287+
simp at h
1288+
12671289
/-! ### mul -/
12681290

12691291
theorem mul_def {n} {x y : BitVec n} : x * y = (ofFin <| x.toFin * y.toFin) := by rfl

0 commit comments

Comments
 (0)