Skip to content

Commit 56e7c23

Browse files
tobiasgrosserJovanGerb
authored andcommitted
feat: add BitVec.[toNat|toInt|toFin|getLsbD|getMsbD|getElem|msb]_fill (leanprover#6177)
This PR implements `BitVec.*_fill`. We also add `toInt_allOnes` and `toFin_allOnes` as the former is needed here. This completes the allOnes API.
1 parent b95f31b commit 56e7c23

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/Init/Data/BitVec/Lemmas.lean

+61
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,19 @@ theorem extractLsb'_eq_extractLsb {w : Nat} (x : BitVec w) (start len : Nat) (h
785785
unfold allOnes
786786
simp
787787

788+
@[simp] theorem toInt_allOnes : (allOnes w).toInt = if 0 < w then -1 else 0 := by
789+
norm_cast
790+
by_cases h : w = 0
791+
· subst h
792+
simp
793+
· have : 1 < 2 ^ w := by simp [h]
794+
simp [BitVec.toInt]
795+
omega
796+
797+
@[simp] theorem toFin_allOnes : (allOnes w).toFin = Fin.ofNat' (2^w) (2^w - 1) := by
798+
ext
799+
simp
800+
788801
@[simp] theorem getLsbD_allOnes : (allOnes v).getLsbD i = decide (i < v) := by
789802
simp [allOnes]
790803

@@ -2382,6 +2395,54 @@ theorem not_neg (x : BitVec w) : ~~~(-x) = x + -1#w := by
23822395
show (_ - x.toNat) % _ = _ by rw [Nat.mod_eq_of_lt (by omega)]]
23832396
omega
23842397

2398+
/-! ### fill -/
2399+
2400+
@[simp]
2401+
theorem getLsbD_fill {w i : Nat} {v : Bool} :
2402+
(fill w v).getLsbD i = (v && decide (i < w)) := by
2403+
by_cases h : v
2404+
<;> simp [h, BitVec.fill, BitVec.negOne_eq_allOnes]
2405+
2406+
@[simp]
2407+
theorem getMsbD_fill {w i : Nat} {v : Bool} :
2408+
(fill w v).getMsbD i = (v && decide (i < w)) := by
2409+
by_cases h : v
2410+
<;> simp [h, BitVec.fill, BitVec.negOne_eq_allOnes]
2411+
2412+
@[simp]
2413+
theorem getElem_fill {w i : Nat} {v : Bool} (h : i < w) :
2414+
(fill w v)[i] = v := by
2415+
by_cases h : v
2416+
<;> simp [h, BitVec.fill, BitVec.negOne_eq_allOnes]
2417+
2418+
@[simp]
2419+
theorem msb_fill {w : Nat} {v : Bool} :
2420+
(fill w v).msb = (v && decide (0 < w)) := by
2421+
simp [BitVec.msb]
2422+
2423+
theorem fill_eq {w : Nat} {v : Bool} : fill w v = if v = true then allOnes w else 0#w := by
2424+
by_cases h : v <;> (simp only [h] ; ext ; simp)
2425+
2426+
@[simp]
2427+
theorem fill_true {w : Nat} : fill w true = allOnes w := by
2428+
simp [fill_eq]
2429+
2430+
@[simp]
2431+
theorem fill_false {w : Nat} : fill w false = 0#w := by
2432+
simp [fill_eq]
2433+
2434+
@[simp] theorem fill_toNat {w : Nat} {v : Bool} :
2435+
(fill w v).toNat = if v = true then 2^w - 1 else 0 := by
2436+
by_cases h : v <;> simp [h]
2437+
2438+
@[simp] theorem fill_toInt {w : Nat} {v : Bool} :
2439+
(fill w v).toInt = if v = true && 0 < w then -1 else 0 := by
2440+
by_cases h : v <;> simp [h]
2441+
2442+
@[simp] theorem fill_toFin {w : Nat} {v : Bool} :
2443+
(fill w v).toFin = if v = true then (allOnes w).toFin else Fin.ofNat' (2 ^ w) 0 := by
2444+
by_cases h : v <;> simp [h]
2445+
23852446
/-! ### mul -/
23862447

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

0 commit comments

Comments
 (0)