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

bitadd is equivalent to BitVector addition #358

Closed
wants to merge 10 commits into from

Conversation

mhk119
Copy link

@mhk119 mhk119 commented Nov 9, 2023

In this PR, we prove a bit-blasting theorem for addition. This depends on #314. I copied the changes in #314 and pasted them manually, but once that gets merged, the number of changes will decrease. The only real additions happen in Std.Data.BitVec.Bitblast.

· simp [mod_one, ofBits]
· simp [bitwise_add, Nat.mod_eq_of_lt ofBits_lt]

theorem BV_add {x y : BitVec w} : bitwise_add (x.toNat) y.toNat w = (x + y).toNat := by
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
theorem BV_add {x y : BitVec w} : bitwise_add (x.toNat) y.toNat w = (x + y).toNat := by
theorem bitwise_add_toNat {x y : BitVec w} : bitwise_add x.toNat y.toNat w = (x + y).toNat := by

| i + 1 => (x.testBit i && y.testBit i) || ((x.testBit i ^^ y.testBit i) && bitwise_carry x y i)

/-- Bitwise addition implemented via a ripple carry adder. -/
@[simp] def bitwise_add (x y i : Nat) :=
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you sure you want @[simp] here? Usually it is a bad idea to just let definitions fall apart anytime someone mentions simp nearby!

Copy link
Author

@mhk119 mhk119 Nov 10, 2023

Choose a reason for hiding this comment

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

I see, in every use case when I want to unfold the definition I want to go directly to unfolding ofBits too (i.e. the intermediate state isn't useful). Perhaps, there's another way to automatically unfold ofBits too whenever I want to unfold without doing simp only [bitadd, ofBits]?

Comment on lines +14 to +15
/-! ### Preliminaries -/

Copy link
Collaborator

Choose a reason for hiding this comment

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

Most of this section should be moved to earlier files.

mhk119 and others added 7 commits November 9, 2023 17:15
Co-authored-by: Scott Morrison <scott@tqft.net>
Co-authored-by: Scott Morrison <scott@tqft.net>
Co-authored-by: Scott Morrison <scott@tqft.net>
Co-authored-by: Scott Morrison <scott@tqft.net>
Comment on lines +68 to +70
def ofBits (f : Nat → Bool) (z : Nat) : Nat → Nat
| 0 => z
| i + 1 => ofBits f (z.bit (f i)) i
Copy link
Member

@eric-wieser eric-wieser Nov 12, 2023

Choose a reason for hiding this comment

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

This has undergone a fair amount of review in Mathlib, but it seems the review there has been discarded and we've reverted back to the original version. The suggestion there was to use

Suggested change
def ofBits (f : Nat → Bool) (z : Nat) : Nat → Nat
| 0 => z
| i + 1 => ofBits f (z.bit (f i)) i
def ofBits {z : Nat} (f : Fin z → Bool) : Nat := sorry

@mhk119 mhk119 changed the title bitwise_addition is equivalent to BitVector addition bitadd is equivalent to BitVector addition Nov 13, 2023
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author. label Nov 17, 2023
@joehendrix
Copy link
Contributor

I'm going to close this. The author's work was appreciated and adapted into #366.

@joehendrix joehendrix closed this Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants