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: BitVec.toFin/ToInt BitVec.ushiftRight #6238

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

bollu
Copy link
Contributor

@bollu bollu commented Nov 27, 2024

This PR adds theorems characterizing the value of the unsigned shift right of a bitvector in terms of its 2s complement interpretation as an integer.
Unsigned shift right by at least one bit makes the value of the bitvector less than or equal to 2^(w-1),
makes the interpretation of the bitvector Int and Nat agree.
In the case when n = 0, then the shift right value equals the integer interpretation.

theorem toInt_ushiftRight_eq_ite {x : BitVec w} {n : Nat} :
  (x >>> n).toInt = if n = 0 then x.toInt else x.toNat >>> n
theorem toFin_uShiftRight {x : BitVec w} {n : Nat} :
  (x >>> n).toFin = x.toFin / (Fin.ofNat' (2^w) (2^n))

@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Nov 27, 2024
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Nov 27, 2024

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 5982a6d2303d629d3c879afe4d00dd8b9fc6bcc6 --onto 9a17919ef11c2dba824498229633b8333a0b53d9. (2024-11-27 15:32:57)
  • ✅ Mathlib branch lean-pr-testing-6238 has successfully built against this PR. (2024-11-28 15:30:07) View Log
  • ✅ Mathlib branch lean-pr-testing-6238 has successfully built against this PR. (2024-11-29 21:35:46) View Log

@kim-em
Copy link
Collaborator

kim-em commented Nov 28, 2024

Otherwise looking good.

Unsigned shift right by at least one bit makes the value of the bitvector less than or equal to `2^(w-1)`,
makes the interpretation of the bitvector `Int` and `Nat` agree.
In the case when `n = 0`, then the shift right value equals the integer interpretation.

```lean
theorem toInt_ushiftRight_eq_ite {x : BitVec w} {n : Nat} :
    (x >>> n).toInt = if n = 0 then x.toInt else x.toNat >>> n
```

---

```lean
theorem toFin_uShiftRight {x : BitVec w} {n : Nat} :
    (x >>> n).toFin = x.toFin / (Fin.ofNat' (2^w) (2^n))
```

Co-authored-by: Harun Khan <[email protected]>
@bollu bollu marked this pull request as ready for review November 28, 2024 14:24
@bollu
Copy link
Contributor Author

bollu commented Nov 28, 2024

awaiting-review

@github-actions github-actions bot added the awaiting-review Waiting for someone to review the PR label Nov 28, 2024
@bollu bollu changed the title feat: BitVec.toFin BitVec.ushiftRight feat: BitVec.toFin/ToInt(BitVec.ushiftRight) Nov 28, 2024
@bollu bollu changed the title feat: BitVec.toFin/ToInt(BitVec.ushiftRight) feat: BitVec.toFin/ToInt BitVec.ushiftRight Nov 28, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Nov 28, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Nov 28, 2024
@leanprover-community-bot leanprover-community-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Nov 28, 2024
@tobiasgrosser
Copy link
Contributor

changelog-library

@github-actions github-actions bot added the changelog-library Library label Nov 29, 2024
@tobiasgrosser
Copy link
Contributor

@bollu, you want to update the PR message to start with This PR ....

leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Nov 29, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Nov 29, 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 builds-mathlib CI has verified that Mathlib builds against this PR changelog-library Library toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants