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: show an equivalence between bitvectors and Fin w -> Bool #8775

Closed
wants to merge 22 commits into from

Conversation

alexkeizer
Copy link
Collaborator


Open in Gitpod

@alexkeizer alexkeizer added the WIP Work in progress label Dec 1, 2023
@alexkeizer
Copy link
Collaborator Author

Proving the equivalence for the big-endian case is turning out to require a decent number of extra intermediate results, so I figure it's easier to move that to another PR and stub it out for now. Thus, I'm marking this PR ready for review!

@alexkeizer alexkeizer added awaiting-review awaiting-CI and removed WIP Work in progress labels Dec 4, 2023
@alexkeizer alexkeizer marked this pull request as ready for review December 4, 2023 13:13
Comment on lines +77 to +83
@[simp]
theorem ofLEFn_getLsb' (x : BitVec w) : ofLEFn (x.getLsb') = x := by
simp [← coe_symm_finFunctionEquivAux_eq_ofLEFn, ← coe_finFunctionEquivAux_eq_getLsb']

@[simp]
theorem getLsb'_ofLEFn (f : Fin w → Bool) : getLsb' (ofLEFn f) = f := by
simp [← coe_symm_finFunctionEquivAux_eq_ofLEFn, ← coe_finFunctionEquivAux_eq_getLsb']
Copy link
Member

Choose a reason for hiding this comment

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

Aren't these trivial to prove by induction on w, without invoking the above?

Copy link
Collaborator Author

@alexkeizer alexkeizer Dec 4, 2023

Choose a reason for hiding this comment

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

We would need to do induction on x, or at the least, have the result concat (x >>> 1 |>.truncate n) x.lsb = x, for x : BitVec (n+1), but once we do have that I do agree it would be cleaner to just prove it directly.

@@ -140,4 +140,14 @@ def toLEList (x : BitVec w) : List Bool :=
def toBEList (x : BitVec w) : List Bool :=
List.ofFn x.getMsb'

/-- Create a bitvector from a function that maps index `i` to the `i`-th least significant bit -/
def ofLEFn {w} (f : Fin w → Bool) : BitVec w :=
Copy link
Member

Choose a reason for hiding this comment

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

Do you think it makes more sense to implement this by taking the bitwise or of bif f i then 1 else 0 <<< i using multiset.fold or similar?
If nothing else, it would be nice to prove that those are equal

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I considered this, it might be faster since there are then less data-dependencies, but it does complicate proofs.
In particular, we might want to upstream ofLEFn to Std at some point, so I would prefer not to rely to much on Mathlib-specific APIs.

For posterity: the following is an alternative def of ofLEFn that would work.

List.finRange w
    |>.map (fun i =>
        shiftLeftZeroExtend (ofBool (f i)) i.val |>.zeroExtend' (Nat.add_comm _ _ ▸ i.prop)
      )
    |>.foldr (· ||| ·) 0#_

@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. label Dec 15, 2023
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. label Dec 16, 2023
@alexkeizer alexkeizer added WIP Work in progress awaiting-author A reviewer has asked the author a question or requested changes and removed awaiting-review labels Dec 27, 2023
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. label Mar 1, 2024
@alexkeizer alexkeizer closed this May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-author A reviewer has asked the author a question or requested changes merge-conflict The PR has a merge conflict with master, and needs manual merging. WIP Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants