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.(getMSbD, msb)_replicate, replicate_append_replicate_eq and support theorems #6326

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

luisacicolini
Copy link
Contributor

@luisacicolini luisacicolini commented Dec 6, 2024

This PR adds BitVec.(getMsbD, msb)_replicate and replicate_append_replicate_eq theorems. An additional private theorem mod_sub_eq_sub_mod is added to prove getMsbD_replicate. The proving strategy basically reduces getMsbD to getLsbD and tackles each case separately.

Co-authored with @bollu.

@luisacicolini luisacicolini changed the title feat: add BitVec.(getMSbD, msb)_replicate and necessary theorems feat: add BitVec.(getMSbD, msb)_replicate, replicate_append_replicate_eq and support theorems Dec 6, 2024
@tobiasgrosser
Copy link
Contributor

@alexkeizer, can you do a round of reviews?

@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 Dec 6, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Dec 6, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Dec 6, 2024
@leanprover-community-bot leanprover-community-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Dec 6, 2024
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Dec 6, 2024

Mathlib CI status (docs):

src/Init/Data/BitVec/Lemmas.lean Outdated Show resolved Hide resolved
src/Init/Data/BitVec/Lemmas.lean Outdated Show resolved Hide resolved
src/Init/Data/BitVec/Lemmas.lean Outdated Show resolved Hide resolved
src/Init/Data/BitVec/Lemmas.lean Outdated Show resolved Hide resolved
src/Init/Data/BitVec/Lemmas.lean Outdated Show resolved Hide resolved
src/Init/Data/BitVec/Lemmas.lean Outdated Show resolved Hide resolved
Comment on lines 3268 to 3269
by_cases h₀ : 0 < w
· by_cases h₁ : i < w * n <;> by_cases h₂ : n = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the proof will be nicer if you use cases w and cases n rather than by_cases.

(cases is also morally the right thing to use, since by_cases invokes classical reasoning, which is wholly unnecessary here, but that is mostly my opinion on aesthetics that is not shared by the community as a whole)

src/Init/Data/BitVec/Lemmas.lean Outdated Show resolved Hide resolved
src/Init/Data/BitVec/Lemmas.lean Show resolved Hide resolved
@luisacicolini
Copy link
Contributor Author

thanks @alexkeizer for the review. I refactored the proof with cases and fixed all non terminal simps.

leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Dec 9, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Dec 9, 2024
@kim-em kim-em self-assigned this Dec 10, 2024
Comment on lines +3262 to +3264
theorem getMsbD_replicate {n w : Nat} (x : BitVec w) :
(x.replicate n).getMsbD i
= (decide (i < w * n) && x.getMsbD (i % w)) := by
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be much simpler if you introduced BitVec.reverse first, and prove this via
rw [← getLsbD_reverse, reverse_replicate, getLsbD_replicate, getLsbD_reverse]

Could you have a go at that? It should avoid all the index munging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @kim-em! Am currently working on the reverse on a different branch to avoiding adding too much stuff here (I am trying to follow the same design strategies as List, treating the bitvec as a list of bools).

Comment on lines +3231 to +3232
theorem mod_sub_eq_sub_mod {w n i : Nat} (hwn : i < w * n) (hn : 0 < n) :
(w * n - 1 - i) % w = w - 1 - i % w := by
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This proof can probably be shortened! But let's see if we can avoid it entirely.

@kim-em kim-em added the awaiting-author Waiting for PR author to address issues label Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-author Waiting for PR author to address issues builds-mathlib CI has verified that Mathlib builds against this PR 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