-
Notifications
You must be signed in to change notification settings - Fork 459
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', extractLsb), getMsbD_extractLsb'_eq_getLsbD
#6792
base: master
Are you sure you want to change the base?
Conversation
Mathlib CI status (docs):
|
changelog-library |
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 API :) I request a couple of minor syntactic changes, and one simplification of a theorem statement.
Co-authored-by: Siddharth <[email protected]>
Co-authored-by: Siddharth <[email protected]>
git push
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.
Some style comments
Co-authored-by: Alex Keizer <[email protected]>
awaiting-review |
I've now changed the statements to remove the parenthesization in |
awaiting-review |
@[simp] theorem getMsbD_extractLsb {hi lo : Nat} {x : BitVec w} {i : Nat} : | ||
(extractLsb hi lo x).getMsbD i | ||
= if lo ≤ hi then decide (i ≤ hi - lo) && (decide (hi - i < w) && x.getMsbD (w - 1 - (hi - i))) | ||
else decide (i = 0) && (decide (lo < w) && x.getMsbD (w - 1 - lo)) := 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.
I have the same concern about the legibility of the RHS here. I wonder if there isn't a simpler RHS?
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 am missing what makes the rhs simpler I guess. The previous version was:
= (decide (i ≤ hi - lo)
&& (decide (lo + (hi - lo - i) < w)
&& x.getMsbD (w - 1 - (lo + (hi - lo - i))))
would this be simpler? Consider that simplifying the parenthesization means introducing the hypoyhesis on lo \le hi
.
The way I see it, the current RHS is simpler, as it highlights the main characteristic of the theorem, being that we can indeed extract (at least) one bit even if hi < lo
. But I am very confused here.
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.
Happy to write the comment anyways - once we reach consensus on a sensible RHS
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 proof of this theorem must start with
rw [getMsbD_eq_getLsbD]
rw [getLsbD_extractLsb]
rw [getLsbD_eq_getMsbD]
and then simplify the inequalities.
After those rewrites, we get
(decide (i < hi - lo + 1) &&
(decide (hi - lo + 1 - 1 - i < hi - lo + 1) &&
(decide (lo + (hi - lo + 1 - 1 - i) < w) && x.getMsbD (w - 1 - (lo + (hi - lo + 1 - 1 - i))))))
The second inequality is automatic and can be dropped.
In the third inequality, + 1 - 1 should be dropped.
The first inequality implies that the -i
in the 3rd inequality is not truncating.
Moreover, if hi - lo
is truncating, then i
is 0.
Thus, given the 1st inequality, lo + (hi - lo + 1 - 1 - i) = (max hi lo) - i
and so the 3rd inequality becomes (max hi lo) - i < w
.
Finally, one needs to simplify w - 1 - (lo + (hi - lo + 1 - 1 - i))
using the first two inequalities. But given the above this is just w - 1 - (max (hi lo) - i)
.
Can you make the proof follow that argument?
(And apologies, I haven't checked this argument beyond what is written here, but even if it is not quite right hopefully you can see the method to derive the appropriate RHS.)
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.
Here's the correct proof style:
@[boolToPropSimps] theorem and_eq_decide (a b : Bool) : (a && b) = decide (a = true ∧ b = true) := by sorry
attribute [boolToPropSimps] decide_eq_true_iff
@[simp] theorem getMsbD_extractLsb {hi lo : Nat} {x : BitVec w} {i : Nat} :
(extractLsb hi lo x).getMsbD i =
(decide (i < hi - lo + 1) &&
(decide ((max hi lo) - i < w) &&
x.getMsbD (w - 1 - ((max hi lo) - i)))) := by
rw [getMsbD_eq_getLsbD]
rw [getLsbD_extractLsb]
rw [getLsbD_eq_getMsbD]
simp only [boolToPropSimps]
constructor
· rintro ⟨h₁, h₂, h₃, h₄⟩
have p : w - 1 - (lo + (hi - lo + 1 - 1 - i)) = w - 1 - (max hi lo - i) := by omega
rw [p] at h₄
simp [h₄]
omega
· rintro ⟨h₁, h₂, h₃⟩
have p : w - 1 - (lo + (hi - lo + 1 - 1 - i)) = w - 1 - (max hi lo - i) := by omega
rw [← p] at h₃
rw [h₃]
simp
omega
Perhaps you can all take a look at this, and
- work out what we need to add to
boolToPropSimps
- digest this proof style, and see where else it can be applied?
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.
(This proof style will eventually be fully automated by grind
, but not quite yet. We need the full linear arithmetic module.)
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.
That's extremely useful @kim-em, thanks a lot! The strategy is clear now
Why is
missing? It seems this is clearly logically prior to the lemmas you're adding here. |
This PR adds theorems
BitVec.(getMsbD, msb)_(extractLsb', extractLsb), getMsbD_extractLsb'_eq_getLsbD
.