Skip to content

Commit 4cff110

Browse files
committed
feat: add BitVec.[zero_ushiftRight|zero_sshiftRight|zero_mul] and clean up BVDecide
- Fix names shiftLeft_zero_eq -> shiftLeft_zero ushiftRight_zero_eq -> ushiftRight_zero - Remove duplicate prefixes BitVec.mul_zero -> mul_zero BitVec.mul_add -> mul_add - Adapt BVDecide/Normalize/BitVec by reusing the following functions zero_add | add_zero and_self mul_zero | zero_mul shiftLeft_zero | zero_shiftLeft sshiftRight_zero | zero_sshiftRight ushiftRight_zero | zero_ushiftRight
1 parent 8c7f748 commit 4cff110

File tree

2 files changed

+37
-54
lines changed

2 files changed

+37
-54
lines changed

src/Init/Data/BitVec/Lemmas.lean

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ theorem not_eq_comm {x y : BitVec w} : ~~~ x = y ↔ x = ~~~ y := by
10621062
BitVec.toFin (x <<< n) = Fin.ofNat' (2^w) (x.toNat <<< n) := rfl
10631063

10641064
@[simp]
1065-
theorem shiftLeft_zero_eq (x : BitVec w) : x <<< 0 = x := by
1065+
theorem shiftLeft_zero (x : BitVec w) : x <<< 0 = x := by
10661066
apply eq_of_toNat_eq
10671067
simp
10681068

@@ -1232,7 +1232,11 @@ theorem ushiftRight_or_distrib (x y : BitVec w) (n : Nat) :
12321232
simp
12331233

12341234
@[simp]
1235-
theorem ushiftRight_zero_eq (x : BitVec w) : x >>> 0 = x := by
1235+
theorem ushiftRight_zero (x : BitVec w) : x >>> 0 = x := by
1236+
simp [bv_toNat]
1237+
1238+
@[simp]
1239+
theorem zero_ushiftRight {n : Nat} : 0#w >>> n = 0#w := by
12361240
simp [bv_toNat]
12371241

12381242
/--
@@ -1387,6 +1391,10 @@ theorem msb_sshiftRight {n : Nat} {x : BitVec w} :
13871391
ext i
13881392
simp [getLsbD_sshiftRight]
13891393

1394+
@[simp] theorem zero_sshiftRight {n : Nat} : (0#w).sshiftRight n = 0#w := by
1395+
ext i
1396+
simp [getLsbD_sshiftRight]
1397+
13901398
theorem sshiftRight_add {x : BitVec w} {m n : Nat} :
13911399
x.sshiftRight (m + n) = (x.sshiftRight m).sshiftRight n := by
13921400
ext i
@@ -2153,18 +2161,23 @@ instance : Std.LawfulCommIdentity (fun (x y : BitVec w) => x * y) (1#w) where
21532161
right_id := BitVec.mul_one
21542162

21552163
@[simp]
2156-
theorem BitVec.mul_zero {x : BitVec w} : x * 0#w = 0#w := by
2164+
theorem mul_zero {x : BitVec w} : x * 0#w = 0#w := by
21572165
apply eq_of_toNat_eq
21582166
simp [toNat_mul]
21592167

2160-
theorem BitVec.mul_add {x y z : BitVec w} :
2168+
@[simp]
2169+
theorem zero_mul {x : BitVec w} : 0#w * x = 0#w := by
2170+
apply eq_of_toNat_eq
2171+
simp [toNat_mul]
2172+
2173+
theorem mul_add {x y z : BitVec w} :
21612174
x * (y + z) = x * y + x * z := by
21622175
apply eq_of_toNat_eq
21632176
simp only [toNat_mul, toNat_add, Nat.add_mod_mod, Nat.mod_add_mod]
21642177
rw [Nat.mul_mod, Nat.mod_mod (y.toNat + z.toNat),
21652178
← Nat.mul_mod, Nat.mul_add]
21662179

2167-
theorem mul_succ {x y : BitVec w} : x * (y + 1#w) = x * y + x := by simp [BitVec.mul_add]
2180+
theorem mul_succ {x y : BitVec w} : x * (y + 1#w) = x * y + x := by simp [mul_add]
21682181
theorem succ_mul {x y : BitVec w} : (x + 1#w) * y = x * y + y := by simp [BitVec.mul_comm, BitVec.mul_add]
21692182

21702183
theorem mul_two {x : BitVec w} : x * 2#w = x + x := by
@@ -3179,4 +3192,10 @@ abbrev and_one_eq_zeroExtend_ofBool_getLsbD := @and_one_eq_setWidth_ofBool_getLs
31793192
@[deprecated msb_sshiftRight (since := "2024-10-03")]
31803193
abbrev sshiftRight_msb_eq_msb := @msb_sshiftRight
31813194

3195+
@[deprecated shiftLeft_zero (since := "2024-10-27")]
3196+
abbrev shiftLeft_zero_eq := @shiftLeft_zero
3197+
3198+
@[deprecated ushiftRight_zero (since := "2024-10-27")]
3199+
abbrev ushiftRight_zero_eq := @ushiftRight_zero
3200+
31823201
end BitVec

src/Std/Tactic/BVDecide/Normalize/BitVec.lean

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,8 @@ attribute [bv_normalize] BitVec.not_not
104104

105105
end Constant
106106

107-
@[bv_normalize]
108-
theorem BitVec.zero_and (a : BitVec w) : 0#w &&& a = 0#w := by
109-
ext
110-
simp
111-
112-
@[bv_normalize]
113-
theorem BitVec.and_zero (a : BitVec w) : a &&& 0#w = 0#w := by
114-
ext
115-
simp
107+
attribute [bv_normalize] BitVec.zero_and
108+
attribute [bv_normalize] BitVec.and_zero
116109

117110
-- Used in simproc because of - normalization
118111
theorem BitVec.ones_and (a : BitVec w) : (-1#w) &&& a = a := by
@@ -124,10 +117,7 @@ theorem BitVec.and_ones (a : BitVec w) : a &&& (-1#w) = a := by
124117
ext
125118
simp [BitVec.negOne_eq_allOnes]
126119

127-
@[bv_normalize]
128-
theorem BitVec.and_self (a : BitVec w) : a &&& a = a := by
129-
ext
130-
simp
120+
attribute [bv_normalize] BitVec.and_self
131121

132122
@[bv_normalize]
133123
theorem BitVec.and_contra (a : BitVec w) : a &&& ~~~a = 0#w := by
@@ -202,56 +192,30 @@ theorem BitVec.add_const_right (a b c : BitVec w) : a + (b + c) = (a + c) + b :=
202192
theorem BitVec.add_const_left' (a b c : BitVec w) : (a + b) + c = (a + c) + b := by ac_rfl
203193
theorem BitVec.add_const_right' (a b c : BitVec w) : (a + b) + c = (b + c) + a := by ac_rfl
204194

205-
@[bv_normalize]
206-
theorem BitVec.zero_sshiftRight : BitVec.sshiftRight 0#w a = 0#w := by
207-
ext
208-
simp [BitVec.getLsbD_sshiftRight]
209-
210-
@[bv_normalize]
211-
theorem BitVec.sshiftRight_zero : BitVec.sshiftRight a 0 = a := by
212-
ext
213-
simp [BitVec.getLsbD_sshiftRight]
214-
215-
@[bv_normalize]
216-
theorem BitVec.mul_zero (a : BitVec w) : a * 0#w = 0#w := by
217-
simp [bv_toNat]
195+
attribute [bv_normalize] BitVec.mul_zero
196+
attribute [bv_normalize] BitVec.zero_mul
218197

219-
@[bv_normalize]
220-
theorem BitVec.zero_mul (a : BitVec w) : 0#w * a = 0#w := by
221-
simp [bv_toNat]
198+
attribute [bv_normalize] BitVec.shiftLeft_zero
199+
attribute [bv_normalize] BitVec.zero_shiftLeft
222200

223201
@[bv_normalize]
224-
theorem BitVec.shiftLeft_zero (n : BitVec w) : n <<< 0#w' = n := by
202+
theorem BitVec.shiftLeft_zero' (n : BitVec w) : n <<< 0#w' = n := by
225203
ext i
226204
simp only [(· <<< ·)]
227205
simp
228206

229-
@[bv_normalize]
230-
theorem BitVec.shiftLeft_zero' (n : BitVec w) : n <<< 0 = n := by
231-
ext i
232-
simp
233-
234-
@[bv_normalize]
235-
theorem BitVec.zero_shiftLeft (n : Nat) : 0#w <<< n = 0#w := by
236-
ext
237-
simp
207+
attribute [bv_normalize] BitVec.zero_sshiftRight
208+
attribute [bv_normalize] BitVec.sshiftRight_zero
238209

239-
@[bv_normalize]
240-
theorem BitVec.zero_shiftRight (n : Nat) : 0#w >>> n = 0#w := by
241-
ext
242-
simp
210+
attribute [bv_normalize] BitVec.zero_ushiftRight
211+
attribute [bv_normalize] BitVec.ushiftRight_zero
243212

244213
@[bv_normalize]
245-
theorem BitVec.shiftRight_zero (n : BitVec w) : n >>> 0#w' = n := by
214+
theorem BitVec.ushiftRight_zero' (n : BitVec w) : n >>> 0#w' = n := by
246215
ext i
247216
simp only [(· >>> ·)]
248217
simp
249218

250-
@[bv_normalize]
251-
theorem BitVec.shiftRight_zero' (n : BitVec w) : n >>> 0 = n := by
252-
ext i
253-
simp
254-
255219
theorem BitVec.zero_lt_iff_zero_neq (a : BitVec w) : (0#w < a) ↔ (a ≠ 0#w) := by
256220
constructor <;>
257221
simp_all only [BitVec.lt_def, BitVec.toNat_ofNat, Nat.zero_mod, ne_eq, BitVec.toNat_eq] <;>

0 commit comments

Comments
 (0)