-
Notifications
You must be signed in to change notification settings - Fork 427
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: BitVec.toInt_abs #6154
Open
bollu
wants to merge
9
commits into
leanprover:master
Choose a base branch
from
opencompl:toInt_abs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: BitVec.toInt_abs #6154
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR adds a theorem that describes `(-x).toInt` as a case-split: - If `x = intMin w`, then `(-x)` overflows and is equal to `intMin w = x = -x`. and thus `(-x).toInt = x.toInt`. - otherwise, `(-x).toInt = - (x.toInt)`. This is a building block to prove `BitVec.toInt_abs`, from leanprover#5787
This PR implements `BitVec.toInt_abs`. The absolute value of `x : BitVec w` is naively a case split on the sign of `x`. However, recall that when `x = intMin w`, `-x = x`. Thus, the full value of `abs x` is computed by the case split: - If `x : BitVec w` is `intMin`, then its absolute value is also `intMin w`, and thus `toInt` will equal `intMin.toInt`. - Otherwise, if `x` is negative, then `x.abs.toInt = (-x).toInt`. - If `x` is positive, then it is equal to `x.abs.toInt = x.toInt`. ```lean theorem toInt_abs {x : BitVec w} : x.abs.toInt = if x = intMin w then (intMin w).toInt else if x.msb then -x.toInt else x.toInt ``` We also provide a variant of `toInt_abs` that hides the case split for `x` being positive or negative by using `natAbs`. ```lean theorem toInt_abs_eq_natAbs {x : BitVec w} : x.abs.toInt = if x = intMin w then (intMin w).toInt else x.toInt.natAbs ``` Supercedes leanprover#5787
github-actions
bot
added
the
toolchain-available
A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
label
Nov 21, 2024
Mathlib CI status (docs):
|
Co-authored-by: Tobias Grosser <github@grosser.es>
Co-authored-by: Tobias Grosser <github@grosser.es>
Can you drop the mention of 'stacked pr' in the commit message? |
@tobiasgrosser done |
tobiasgrosser
approved these changes
Nov 21, 2024
awaiting-review |
changelog-library |
kim-em
reviewed
Nov 21, 2024
kim-em
reviewed
Nov 21, 2024
kim-em
reviewed
Nov 21, 2024
It doesn't need to be in this PR, but I would like to also have
(didn't check the RHS carefully, but something like that ought to be true). |
Co-authored-by: Tobias Grosser <github@grosser.es>
kim-em
reviewed
Nov 26, 2024
Otherwise looking good now. Switch back to |
awaiting-review |
github-actions
bot
added
awaiting-review
Waiting for someone to review the PR
and removed
awaiting-author
Waiting for PR author to address issues
labels
Nov 26, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
awaiting-review
Waiting for someone to review the PR
changelog-library
Library
toolchain-available
A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements
BitVec.toInt_abs
.The absolute value of
x : BitVec w
is naively a case split on the sign ofx
.However, recall that when
x = intMin w
,-x = x
.Thus, the full value of
abs x
is computed by the case split:x : BitVec w
isintMin
, then its absolute value is alsointMin w
, andthus
toInt
will equalintMin.toInt
.x
is negative, thenx.abs.toInt = (-x).toInt
.x
is nonnegative, thenx.abs.toInt = x.toInt
.We also provide a variant of
toInt_abs
thathides the case split for
x
being positive or negative by usingnatAbs
.Supercedes #5787