Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add BitVec.[(getMsbD, msb)_extractLsb', (getLsbD, getMsbD, msb)_extractLsb] #6792

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Changes from 9 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6430f5c
feat: dumbest statements I could think of
luisacicolini Jan 23, 2025
4f2605e
simplification
luisacicolini Jan 23, 2025
30d6f28
chore: simplify dumb statements
luisacicolini Jan 24, 2025
98c4afb
progress
luisacicolini Jan 24, 2025
20a6ca8
chore: proofs done
luisacicolini Jan 27, 2025
84fed64
chore: proofs done
luisacicolini Jan 27, 2025
0b9c377
chore: update names
luisacicolini Jan 29, 2025
58b6f1a
chore: update names
luisacicolini Jan 29, 2025
619a901
chore: simplify msb_extractLsb
luisacicolini Jan 29, 2025
8309077
chore: undo
luisacicolini Jan 29, 2025
da80f4d
chore: simplify msb_extractLsb
luisacicolini Jan 29, 2025
be7f0b8
chore: format cases
luisacicolini Jan 30, 2025
ce00480
chore: format
luisacicolini Jan 30, 2025
38fe8a8
Update src/Init/Data/BitVec/Lemmas.lean
bollu Jan 30, 2025
cb775b7
chore: fix comments
luisacicolini Feb 3, 2025
243d6af
chore: remove unwanted chnge
luisacicolini Feb 3, 2025
4c91668
chore: new statements and new poroofs
luisacicolini Feb 3, 2025
5954098
chore: unwanted change fix
luisacicolini Feb 3, 2025
912bdbe
chore: remove useless newline
luisacicolini Feb 3, 2025
2b51a16
chore: format getMsbD_extractLsb_prime
luisacicolini Feb 5, 2025
f3d2463
chore: verbose comment
luisacicolini Feb 5, 2025
11e3baf
chore: fix getMsbD_extractLsb proof
luisacicolini Feb 5, 2025
070ff21
chore: format
luisacicolini Feb 5, 2025
b1c04d1
chore: remove unwanted space mod
luisacicolini Feb 5, 2025
b42dfad
chore: formatting
luisacicolini Feb 6, 2025
fd5578b
chore: format
luisacicolini Feb 7, 2025
d283c3b
chore: format
luisacicolini Feb 7, 2025
2db6461
chore: fix build
luisacicolini Feb 7, 2025
47cb161
chore: formatting
luisacicolini Feb 7, 2025
2145d73
chore: formatting
luisacicolini Feb 7, 2025
d31a7d7
chore: formatting
luisacicolini Feb 7, 2025
5182507
chore: format
luisacicolini Feb 7, 2025
e46bc38
chore: format
luisacicolini Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/Init/Data/BitVec/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,23 @@ protected theorem extractLsb_ofNat (x n : Nat) (hi lo : Nat) :
(extractLsb' start len x).getLsbD i = (i < len && x.getLsbD (start+i)) := by
simp [getLsbD, Nat.lt_succ]

@[simp] theorem getMsbD_extractLsb' {start len : Nat} {x : BitVec w} {i : Nat} :
(extractLsb' start len x).getMsbD i =
(decide (i < len) &&
(decide (start + (len - 1 - i) < w) && x.getMsbD (w - 1 - (start + (len - 1 - i))))) := by
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
rcases len with rfl | len
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
· simp
· have h : (len - i < len + 1) := by omega
rw [getMsbD_eq_getLsbD, getLsbD_extractLsb', getLsbD_eq_getMsbD]
simp [h]

@[simp] theorem msb_extractLsb' {start len : Nat} {x : BitVec w} :
(extractLsb' start len x).msb =
(decide (0 < len) && x.getLsbD (start + len - 1)) := by
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
rcases len with rfl | len
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
· simp [BitVec.extractLsb', BitVec.msb]
· simp [BitVec.msb, BitVec.getMsbD_eq_getLsbD, BitVec.getLsbD_extractLsb', ← Nat.add_assoc]

@[simp] theorem getElem_extract {hi lo : Nat} {x : BitVec n} {i : Nat} (h : i < hi - lo + 1) :
(extractLsb hi lo x)[i] = getLsbD x (lo+i) := by
simp [getElem_eq_testBit_toNat, getLsbD, h]
Expand All @@ -789,6 +806,16 @@ protected theorem extractLsb_ofNat (x n : Nat) (hi lo : Nat) :
getLsbD (extractLsb hi lo x) i = (i ≤ (hi-lo) && getLsbD x (lo+i)) := by
simp [getLsbD, Nat.lt_succ]

@[simp] theorem getMsbD_extractLsb {hi lo : Nat} {x : BitVec w} {i : Nat} :
(extractLsb hi lo x).getMsbD i =
(decide (i < hi - lo + 1) && (decide (lo + (hi - lo - i) < w) && x.getMsbD (w - 1 - (lo + (hi - lo - i))))) := by
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
rw [BitVec.extractLsb, BitVec.getMsbD_extractLsb', Nat.add_sub_cancel]

@[simp] theorem msb_extractLsb {hi lo : Nat} {x : BitVec n} :
(extractLsb hi lo x).msb = (decide (0 < hi - lo + 1) &&
(decide (lo + (hi - lo) < n) && x.getMsbD (n - 1 - (lo + (hi - lo))))) := by
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
simp [show hi - lo ≤ hi - lo by omega, BitVec.msb, BitVec.getMsbD_extractLsb, Nat.sub_zero]

theorem extractLsb'_eq_extractLsb {w : Nat} (x : BitVec w) (start len : Nat) (h : len > 0) :
x.extractLsb' start len = (x.extractLsb (len - 1 + start) start).cast (by omega) := by
apply eq_of_toNat_eq
Expand Down Expand Up @@ -3352,7 +3379,7 @@ theorem shiftLeft_eq_mul_twoPow (x : BitVec w) (n : Nat) :
simp [getLsbD_shiftLeft, Fin.is_lt, decide_true, Bool.true_and, mul_twoPow_eq_shiftLeft]

/-- 2^i * 2^j = 2^(i + j) with bitvectors as well -/
theorem twoPow_mul_twoPow_eq {w : Nat} (i j : Nat) : twoPow w i * twoPow w j = twoPow w (i + j) := by
theorem twoPow_mul_twoPow_eq {w : Nat} (i j : Nat) : twoPow w i * twoPow w j = twoPow w (i + j) := by
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
bollu marked this conversation as resolved.
Show resolved Hide resolved
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
apply BitVec.eq_of_toNat_eq
simp only [toNat_mul, toNat_twoPow]
rw [← Nat.mul_mod, Nat.pow_add]
Expand Down
Loading