Skip to content

Commit c23c0c8

Browse files
committed
feat: add BitVec.[toNat|toInt|toFin|getLsbD|getMsbD|getElem|msb]_fill
1 parent 5145030 commit c23c0c8

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/Init/Data/BitVec/Lemmas.lean

+62
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,15 @@ theorem extractLsb'_eq_extractLsb {w : Nat} (x : BitVec w) (start len : Nat) (h
756756
unfold allOnes
757757
simp
758758

759+
@[simp] theorem toInt_allOnes : (allOnes w).toInt = if 0 < w then -1 else 0 := by
760+
norm_cast
761+
by_cases h : w = 0
762+
· subst h
763+
simp
764+
· have : 1 < 2 ^ w := by simp [h]
765+
simp [BitVec.toInt]
766+
omega
767+
759768
@[simp] theorem getLsbD_allOnes : (allOnes v).getLsbD i = decide (i < v) := by
760769
simp [allOnes]
761770

@@ -2215,6 +2224,59 @@ theorem not_neg (x : BitVec w) : ~~~(-x) = x + -1#w := by
22152224
show (_ - x.toNat) % _ = _ by rw [Nat.mod_eq_of_lt (by omega)]]
22162225
omega
22172226

2227+
/-! ### fill -/
2228+
2229+
@[simp]
2230+
theorem getLsbD_fill {w i : Nat} {v : Bool} :
2231+
(fill w v).getLsbD i = (v && i < w) := by
2232+
simp [BitVec.fill]
2233+
by_cases h : v
2234+
<;> simp [h, BitVec.negOne_eq_allOnes]
2235+
2236+
@[simp]
2237+
theorem getMsbD_fill {w i : Nat} {v : Bool} :
2238+
(fill w v).getMsbD i = (v && i < w) := by
2239+
simp [BitVec.fill]
2240+
by_cases h : v
2241+
<;> simp [h, BitVec.negOne_eq_allOnes]
2242+
2243+
@[simp]
2244+
theorem getElem_fill {w i : Nat} {v : Bool} (h : i < w) :
2245+
(fill w v)[i] = v := by
2246+
simp [BitVec.fill]
2247+
by_cases h : v
2248+
<;> simp [h, BitVec.negOne_eq_allOnes]
2249+
2250+
@[simp]
2251+
theorem msb_fill {w : Nat} {v : Bool} :
2252+
(fill w v).msb = (v && 0 < w) := by
2253+
simp [BitVec.msb]
2254+
2255+
theorem fill_eq {w : Nat} {v : Bool} : fill w v = if v then allOnes w else 0#w := by
2256+
by_cases h : v <;> (simp [h] ; ext ; simp)
2257+
2258+
@[simp]
2259+
theorem fill_eq_allOnes {w : Nat} : fill w true = allOnes w := by
2260+
simp [fill_eq]
2261+
2262+
@[simp]
2263+
theorem fill_eq_zero {w : Nat} : fill w false = 0#w := by
2264+
simp [fill_eq]
2265+
2266+
@[simp] theorem fill_toNat {w : Nat} {v : Bool} :
2267+
(fill w v).toNat = if v then 2^w - 1 else 0 := by
2268+
by_cases h : v <;> simp [h]
2269+
2270+
@[simp] theorem fill_toInt {w : Nat} {v : Bool} :
2271+
(fill w v).toInt = if v then (allOnes w).toInt else 0 := by
2272+
simp
2273+
by_cases h : v <;> simp [h]
2274+
2275+
@[simp] theorem fill_toFin {w : Nat} {v : Bool} :
2276+
(fill w v).toFin = if v then (allOnes w).toFin else (0#w).toFin := by
2277+
simp
2278+
by_cases h : v <;> simp [h]
2279+
22182280
/-! ### mul -/
22192281

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

0 commit comments

Comments
 (0)