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 reverse definition, getLsbD_reverse, getMsbD_reverse, reverse_append, reverse_replicate and Nat.mod_sub_eq_sub_mod #6476

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7acc914
prelim def
luisacicolini Dec 12, 2024
46a61c9
added relevant theorems
luisacicolini Dec 12, 2024
280ac8d
simpler def?
luisacicolini Dec 12, 2024
de0a2d6
reverse definition
luisacicolini Dec 28, 2024
e838c6a
Merge branch 'leanprover:master' into bitvec-reverse
luisacicolini Dec 28, 2024
01482af
chore: update reverse def
luisacicolini Dec 28, 2024
28fcb69
chore: better reverse definition
luisacicolini Dec 28, 2024
ee2a398
chore: close proof
luisacicolini Dec 30, 2024
77feac5
Merge branch 'leanprover:master' into bitvec-reverse
luisacicolini Dec 30, 2024
edf2a8c
chore: fix reverse
luisacicolini Dec 31, 2024
972f8cd
Merge branch 'bitvec-reverse' of github.com:opencompl/lean4 into bitv…
luisacicolini Dec 31, 2024
034bf23
chore: add docstring for reverse
luisacicolini Dec 31, 2024
cfdf9be
chore: add docstring for reverse
luisacicolini Dec 31, 2024
f51ba58
Update src/Init/Data/BitVec/Basic.lean
luisacicolini Jan 2, 2025
b47c36a
Update src/Init/Data/BitVec/Basic.lean
luisacicolini Jan 2, 2025
c20d086
feat: add msb_replicate
luisacicolini Jan 2, 2025
d62f75b
Merge branch 'leanprover:master' into bitvec-reverse
luisacicolini Jan 2, 2025
a8ff8e2
chore: fix build
luisacicolini Jan 2, 2025
42585f1
chore: remove useless hypotheses from proof
luisacicolini Jan 6, 2025
4ba0785
better proof strategy
kim-em Jan 8, 2025
052880f
chore: proof of reverse_cast
luisacicolini Jan 8, 2025
6fa32db
chore: temporary heavy proof
luisacicolini Jan 8, 2025
54a9697
chore: clean proof
luisacicolini Jan 8, 2025
38a1294
chore: remove nonterm simp
luisacicolini Jan 8, 2025
8efdadf
chore: fix simp only
luisacicolini Jan 8, 2025
37c1002
chore: move deprecateed theorems to fix build
luisacicolini Jan 8, 2025
b9049df
feat: replicate_append_self proof
luisacicolini Jan 10, 2025
38fb6eb
chore: an ugly and aggressive proof to start with, before induction
luisacicolini Jan 10, 2025
e696663
feat: cons append
luisacicolini Jan 10, 2025
63e1fb4
chore: fix cons_append for consistency with list behavior
luisacicolini Jan 11, 2025
7b39cc2
chore: induction proof done!
luisacicolini Jan 12, 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
7 changes: 7 additions & 0 deletions src/Init/Data/BitVec/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -669,4 +669,11 @@ def ofBoolListLE : (bs : List Bool) → BitVec bs.length
| [] => 0#0
| b :: bs => concat (ofBoolListLE bs) b

/- ### reverse -/

/-- Reverse of a Bitvec. We treat BitVec as lists of bools. -/
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
def reverse : (w : Nat) → BitVec w → BitVec w
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
| 0, x => x
| w + 1, x => concat (reverse w (x.truncate w)) (x.msb)

end BitVec
80 changes: 80 additions & 0 deletions src/Init/Data/BitVec/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3502,6 +3502,86 @@ theorem toInt_abs_eq_natAbs_of_ne_intMin {x : BitVec w} (hx : x ≠ intMin w) :
x.abs.toInt = x.toInt.natAbs := by
simp [toInt_abs_eq_natAbs, hx]

/-! ### Reverse -/

theorem getLsbD_reverse {i : Nat} {x : BitVec w} :
(x.reverse).getLsbD i = x.getMsbD i := by
induction w generalizing i
case zero => simp
case succ n ih =>
simp only [reverse, truncate_eq_setWidth, getLsbD_concat]
rcases i with rfl | i
· rfl
· simp only [Nat.add_one_ne_zero, ↓reduceIte, Nat.add_one_sub_one, ih]
rw [getMsbD_setWidth]
simp only [show n - (n + 1) = 0 by omega, Nat.zero_le, decide_true, Bool.true_and]
congr; omega

theorem getMsbD_reverse {i : Nat} {x : BitVec w} :
(x.reverse).getMsbD i = x.getLsbD i := by
simp only [getMsbD_eq_getLsbD, getLsbD_reverse]
by_cases hi : i < w
· simp only [hi, decide_true, show w - 1 - i < w by omega, Bool.true_and]
congr; omega
· simp [hi, show i ≥ w by omega]

luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
theorem reverse_append {x : BitVec w} {y : BitVec v} (h : v + w = w + v) :
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
(x ++ y).reverse = (y.reverse ++ x.reverse).cast h := by
ext i h
simp only [getLsbD_append, getLsbD_reverse]
by_cases hi : i < v
· by_cases hw : w ≤ i
· simp [getMsbD_append, getLsbD_cast, getLsbD_append, getLsbD_reverse, hw]
· simp [getMsbD_append, getLsbD_cast, getLsbD_append, getLsbD_reverse, hw, show i < w by omega]
· by_cases hw : w ≤ i
· simp [getMsbD_append, getLsbD_cast, getLsbD_append, hw, show ¬ i < w by omega, getLsbD_reverse]
· simp [getMsbD_append, getLsbD_cast, getLsbD_append, hw, show i < w by omega, getLsbD_reverse]

theorem Nat.mod_sub_eq_sub_mod {w n i : Nat} (hwn : i < w * n) (hn : 0 < n):
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
(w * n - 1 - i) % w = w - 1 - i % w := by
induction n
case zero => omega
case succ n ih =>
simp_all only [Nat.mul_add, Nat.mul_one, Nat.zero_lt_succ]
by_cases h : i < w * n
· simp only [show w * n + w - 1 - i = w + (w * n - 1 - i) by omega, Nat.add_mod_left]
rw [ih (by omega)]
suffices ¬ n = 0 by omega
intros hcontra
subst hcontra
simp at h
· rw [Nat.mod_eq_of_lt]
· have := Nat.mod_add_div i w
have hiw : i / w = n := by
apply Nat.div_eq_of_lt_le
· rw [Nat.mul_comm]
omega
· rw [Nat.add_mul]
simp only [Nat.one_mul]
rw [Nat.mul_comm]
omega
rw [hiw] at this
conv =>
lhs
rw [← this]
omega
· omega

theorem reverse_replicate {n : Nat} {x : BitVec w} :
(x.replicate n).reverse = (x.reverse).replicate n := by
ext i h
simp [getLsbD_reverse, getMsbD_eq_getLsbD, getLsbD_replicate]
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
rcases n with rfl | n
· simp
· by_cases hw : 0 < w
· simp [show (w * (n + 1) - 1 - i < w * (n + 1)) by omega,
luisacicolini marked this conversation as resolved.
Show resolved Hide resolved
show i % w < w by simp [Nat.mod_lt (x := i) (y := w) hw]]
congr
let m := (n + 1)
have hn: 0 < n + 1 := by omega
rw [Nat.mod_sub_eq_sub_mod (n := n + 1) h hn]
<;> omega
· simp [show w = 0 by omega]

/-! ### Decidable quantifiers -/

Expand Down
Loading