-
Notifications
You must be signed in to change notification settings - Fork 447
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
base: master
Are you sure you want to change the base?
Conversation
Mathlib CI status (docs):
|
Co-authored-by: Alex Keizer <[email protected]>
Co-authored-by: Alex Keizer <[email protected]>
@luisacicolini, please add the |
Thanks a lot @kim-em :) |
changelog-library |
I suspect, but won't ask for this PR, that proving things about |
And please remember to comment |
Had to add an extra |
awaiting-review |
theorem cons_append (x : BitVec w₁) (y : BitVec w₂) (a : Bool) : | ||
(cons a x) ++ y = (cons a (x ++ y)).cast (by omega) := by | ||
ext i h | ||
simp only [cons, getLsbD_append, getLsbD_cast, getLsbD_ofBool, cast_cast] | ||
by_cases h₀ : i < w₁ + w₂ | ||
· simp [h₀] | ||
by_cases h₁ : i < w₂ | ||
· simp [h₁] | ||
· simp [h₁, show i - w₂ - w₁ = 0 by omega] | ||
omega | ||
· simp [show ¬i < w₂ by omega, show i - w₂ - w₁ = 0 by omega, h₀, show i - (w₁ + w₂) = 0 by omega] | ||
omega |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry to keep asking for more and more changes here, but I think there's opportunity to fill in obvious missing lemmas and get easier proofs.
Could this be by:
theorem cons_append (x : BitVec w₁) (y : BitVec w₂) (a : Bool) :
(cons a x) ++ y = (cons a (x ++ y)).cast (by omega) := by
apply eq_of_toNat_eq
simp only [toNat_append, toNat_cons, toNat_cast]
rw [Nat.shiftLeft_add, Nat.shiftLeft_or_distrib]
The lemma Nat.shiftLeft_or_distrib
is missing and would need to be written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here is that switching to Nat as soon as possible means you don't need to handle all in index inequalities. It's great that omega
can solve them, but hopefully we can just avoid them to begin with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the advice @kim-em :) I'm working on the proof of Nat.shiftLeft_or_distrib
but it seems to me like the best way to prove it is to add shiftLeft_bitwise_distrib
, bitwise_mul_two_pow
, and potentially a few more theorems (basically do what the proof of shiftRight_and_distrib
does). If this sounds sensible I'm happy to add the necessary theorems, but maybe it's better to do it in another PR, since this is quite big already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently working here on this to avoid having a massive PR!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now in #6630 :)
@@ -3217,6 +3250,33 @@ theorem getElem_replicate {n w : Nat} (x : BitVec w) (h : i < w * n) : | |||
simp only [← getLsbD_eq_getElem, getLsbD_replicate] | |||
by_cases h' : w = 0 <;> simp [h'] <;> omega | |||
|
|||
theorem append_assoc {x₁ : BitVec w₁} {x₂ : BitVec w₂} {x₃ : BitVec w₃} : | |||
(x₁ ++ x₂)++ x₃ = (x₁ ++ (x₂ ++ x₃)).cast (by omega) := by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(x₁ ++ x₂)++ x₃ = (x₁ ++ (x₂ ++ x₃)).cast (by omega) := by | |
(x₁ ++ x₂) ++ x₃ = (x₁ ++ (x₂ ++ x₃)).cast (by omega) := by |
This PR defines
reverse
for bitvectors and implements a first subset of theorems (getLsbD_reverse, getMsbD_reverse, reverse_append, reverse_replicate
) and an additional theorem necessary for one of the proofs (Nat.mod_sub_eq_sub_mod
).The main objective is to simplify the proofs in #6326. I sadly could not find a way to avoid adding
Nat.mod_sub_eq_sub_mod
, any suggestion in this direction would be greatly helpful.