Skip to content

Commit 23a202b

Browse files
alexkeizerkim-em
andauthored
feat: characterize BitVec.toInt in terms of BitVec.msb (leanprover#4200)
This PR extracts `msb_eq_false_iff_two_mul_lt` and `msb_eq_true_iff_two_mul_ge` from leanprover#4179, and uses them to prove a theorem that characterizes `BitVec.toInt` in terms of `BitVec.msb`. This lemma will be useful to prove a bit-blasting theorem for `BitVec.slt` and `BitVec.sle`. Also cleans up an existing proof (`toInt_eq_toNat_cond `), which turns out to be provable by `rfl`. --------- Co-authored-by: Kim Morrison <scott@tqft.net>
1 parent ff37e5d commit 23a202b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/Init/Data/BitVec/Lemmas.lean

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Init.Data.Bool
99
import Init.Data.BitVec.Basic
1010
import Init.Data.Fin.Lemmas
1111
import Init.Data.Nat.Lemmas
12+
import Init.Data.Nat.Mod
1213

1314
namespace BitVec
1415

@@ -222,9 +223,21 @@ theorem toInt_eq_toNat_cond (i : BitVec n) :
222223
if 2*i.toNat < 2^n then
223224
(i.toNat : Int)
224225
else
225-
(i.toNat : Int) - (2^n : Nat) := by
226-
unfold BitVec.toInt
227-
split <;> omega
226+
(i.toNat : Int) - (2^n : Nat) :=
227+
rfl
228+
229+
theorem msb_eq_false_iff_two_mul_lt (x : BitVec w) : x.msb = false2 * x.toNat < 2^w := by
230+
cases w <;> simp [Nat.pow_succ, Nat.mul_comm _ 2, msb_eq_decide]
231+
232+
theorem msb_eq_true_iff_two_mul_ge (x : BitVec w) : x.msb = true2 * x.toNat ≥ 2^w := by
233+
simp [← Bool.ne_false_iff, msb_eq_false_iff_two_mul_lt]
234+
235+
/-- Characterize `x.toInt` in terms of `x.msb`. -/
236+
theorem toInt_eq_msb_cond (x : BitVec w) :
237+
x.toInt = if x.msb then (x.toNat : Int) - (2^w : Nat) else (x.toNat : Int) := by
238+
simp only [BitVec.toInt, ← msb_eq_false_iff_two_mul_lt]
239+
cases x.msb <;> rfl
240+
228241

229242
theorem toInt_eq_toNat_bmod (x : BitVec n) : x.toInt = Int.bmod x.toNat (2^n) := by
230243
simp only [toInt_eq_toNat_cond]

0 commit comments

Comments
 (0)