Skip to content

Commit 5945875

Browse files
feat: add Bitvec.[(toInt, toFin)_twoPow, toNat_twoPow_of_le, toNat_twoPow_of_lt, toNat_twoPow_eq_ite] (leanprover#7225)
This PR contains `BitVec.(toInt, toFin)_twoPow` theorems, completing the API for `BitVec.*_twoPow`. It also expands the `toNat_twoPow` API with `toNat_twoPow_of_le`, `toNat_twoPow_of_lt`, as well as `toNat_twoPow_eq_if` and moves `msb_twoPow` up, as it is used in the `toInt_msb` proof. --------- Co-authored-by: Henrik Böving <[email protected]>
1 parent 6df6011 commit 5945875

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

src/Init/Data/BitVec/Lemmas.lean

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,6 +4122,22 @@ theorem toNat_twoPow (w : Nat) (i : Nat) : (twoPow w i).toNat = 2^i % 2^w := by
41224122
have h1 : 1 < 2 ^ (w + 1) := Nat.one_lt_two_pow (by omega)
41234123
rw [Nat.mod_eq_of_lt h1, Nat.shiftLeft_eq, Nat.one_mul]
41244124

4125+
theorem toNat_twoPow_of_le {i w : Nat} (h : w ≤ i) : (twoPow w i).toNat = 0 := by
4126+
rw [toNat_twoPow]
4127+
apply Nat.mod_eq_zero_of_dvd
4128+
exact Nat.pow_dvd_pow_iff_le_right'.mpr h
4129+
4130+
theorem toNat_twoPow_of_lt {i w : Nat} (h : i < w) : (twoPow w i).toNat = 2^i := by
4131+
rw [toNat_twoPow]
4132+
apply Nat.mod_eq_of_lt
4133+
apply Nat.pow_lt_pow_of_lt (by omega) (by omega)
4134+
4135+
theorem toNat_twoPow_eq_ite {i w : Nat} : (twoPow w i).toNat = if i < w then 2^i else 0 := by
4136+
by_cases h : i < w
4137+
· simp only [h, toNat_twoPow_of_lt, if_true]
4138+
· simp only [h, if_false]
4139+
rw [toNat_twoPow_of_le (by omega)]
4140+
41254141
@[simp]
41264142
theorem getLsbD_twoPow (i j : Nat) : (twoPow w i).getLsbD j = ((i < w) && (i = j)) := by
41274143
rcases w with rfl | w
@@ -4140,6 +4156,33 @@ theorem getLsbD_twoPow (i j : Nat) : (twoPow w i).getLsbD j = ((i < w) && (i = j
41404156
simp at hi
41414157
simp_all
41424158

4159+
@[simp]
4160+
theorem msb_twoPow {i w: Nat} :
4161+
(twoPow w i).msb = (decide (i < w) && decide (i = w - 1)) := by
4162+
simp only [BitVec.msb, getMsbD_eq_getLsbD, Nat.sub_zero, getLsbD_twoPow,
4163+
Bool.and_iff_right_iff_imp, Bool.and_eq_true, decide_eq_true_eq, and_imp]
4164+
intros
4165+
omega
4166+
4167+
theorem toInt_twoPow {w i : Nat} :
4168+
(BitVec.twoPow w i).toInt = if w ≤ i then 0
4169+
else if i + 1 = w then (-(2^i : Nat) : Int) else 2^i := by
4170+
simp only [BitVec.toInt_eq_msb_cond, toNat_twoPow_eq_ite]
4171+
rcases w with _ | w
4172+
· simp
4173+
· by_cases h : i = w
4174+
· simp [h, show ¬ (w + 1 ≤ w) by omega]
4175+
omega
4176+
· by_cases h' : w + 1 ≤ i
4177+
· simp [h', show ¬ i < w + 1 by omega]
4178+
· simp [h, h', show i < w + 1 by omega, Int.natCast_pow]
4179+
4180+
theorem toFin_twoPow {w i : Nat} :
4181+
(BitVec.twoPow w i).toFin = Fin.ofNat' (2^w) (2^i) := by
4182+
rcases w with rfl | w
4183+
· simp [BitVec.twoPow, BitVec.toFin, toFin_shiftLeft, Fin.fin_one_eq_zero]
4184+
· simp [BitVec.twoPow, BitVec.toFin, toFin_shiftLeft, Nat.shiftLeft_eq]
4185+
41434186
@[simp]
41444187
theorem getElem_twoPow {i j : Nat} (h : j < w) : (twoPow w i)[j] = decide (j = i) := by
41454188
rw [←getLsbD_eq_getElem, getLsbD_twoPow]
@@ -4153,14 +4196,6 @@ theorem getMsbD_twoPow {i j w: Nat} :
41534196
by_cases h₀ : i < w <;> by_cases h₁ : j < w <;>
41544197
simp [h₀, h₁] <;> omega
41554198

4156-
@[simp]
4157-
theorem msb_twoPow {i w: Nat} :
4158-
(twoPow w i).msb = (decide (i < w) && decide (i = w - 1)) := by
4159-
simp only [BitVec.msb, getMsbD_eq_getLsbD, Nat.sub_zero, getLsbD_twoPow,
4160-
Bool.and_iff_right_iff_imp, Bool.and_eq_true, decide_eq_true_eq, and_imp]
4161-
intros
4162-
omega
4163-
41644199
theorem and_twoPow (x : BitVec w) (i : Nat) :
41654200
x &&& (twoPow w i) = if x.getLsbD i then twoPow w i else 0#w := by
41664201
ext j h

0 commit comments

Comments
 (0)