-
Notifications
You must be signed in to change notification settings - Fork 259
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
Conversation
alexkeizer
commented
Dec 1, 2023
9edb151
to
f36e959
Compare
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! |
@[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'] |
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.
Aren't these trivial to prove by induction on w
, without invoking the above?
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.
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 := |
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.
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
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 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#_
This reverts commit d4df1a0.