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

chore: prove that subtraction on BitStreams corresponds to subtraction on BitVectors #559

Merged
merged 22 commits into from
Aug 28, 2024

Conversation

AtticusKuhn
Copy link
Collaborator

@AtticusKuhn AtticusKuhn commented Aug 22, 2024

This PR proves the key theorem

theorem ofBitVec_sub : ofBitVec (x - y) ≈ʷ (ofBitVec x) - (ofBitVec y)  := by
  calc
  _ ≈ʷ ofBitVec (x + - y) := by rw [BitVec.sub_add_neg]
  _ ≈ʷ ofBitVec x + ofBitVec (-y) := ofBitVec_add
  _ ≈ʷ ofBitVec x + - ofBitVec y := add_congr equal_up_to_refl ofBitVec_neg
  _ ≈ʷ ofBitVec x - ofBitVec y := by rw [sub_add_neg]

Which shows that subtraction on BitStreams corresponds to subtraction on BitVectors.

In doing so, this PR removes the last "sorry" from the file BitStream.lean.

Note that this PR is a continuation of PR
#554

Copy link

Alive Statistics: 64 / 93 (29 failed)

Copy link

Alive Statistics: 64 / 93 (29 failed)

4 similar comments
Copy link

Alive Statistics: 64 / 93 (29 failed)

Copy link

Alive Statistics: 64 / 93 (29 failed)

Copy link

Alive Statistics: 64 / 93 (29 failed)

Copy link

Alive Statistics: 64 / 93 (29 failed)

@tobiasgrosser
Copy link
Collaborator

@AtticusKuhn, what a success! 🎉 Turning my broken hack into such a beautiful proof.

I left many comments, but this is one of the most polished PRs I started reviewing. In retrospect, the proof seems straightforward, which is remarkable given the complex proof states one can reach. You broke it down into a couple of well-structured semantic pieces.

Copy link

Alive Statistics: 64 / 93 (29 failed)

Copy link

Alive Statistics: 64 / 93 (29 failed)

1 similar comment
Copy link

Alive Statistics: 64 / 93 (29 failed)

Copy link
Collaborator

@tobiasgrosser tobiasgrosser left a comment

Choose a reason for hiding this comment

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

I am done. This looks perfect to me. @alexkeizer @bollu, feedback would be appreciated.

Copy link

Alive Statistics: 64 / 93 (29 failed)

1 similar comment
Copy link

Alive Statistics: 64 / 93 (29 failed)

@AtticusKuhn
Copy link
Collaborator Author

AtticusKuhn commented Aug 22, 2024

I am done.

Thank you for your guidance. Your feedback is very helpful.

@tobiasgrosser tobiasgrosser changed the title Chore: Prove that subtraction on BitStreams correponds to subtraction on BitVectors, remove last sorry from BitStream.lean chore: Prove that subtraction on BitStreams corresponds to subtraction on BitVectors Aug 23, 2024
@tobiasgrosser tobiasgrosser changed the title chore: Prove that subtraction on BitStreams corresponds to subtraction on BitVectors chore: prove that subtraction on BitStreams corresponds to subtraction on BitVectors Aug 23, 2024
Copy link

Alive Statistics: 64 / 93 (29 failed)

@AtticusKuhn
Copy link
Collaborator Author

If no one objects, then I am going to merge this PR because there has been no activity on it for three days.

Copy link

Alive Statistics: 64 / 93 (29 failed)

@AtticusKuhn AtticusKuhn added this pull request to the merge queue Aug 28, 2024
Merged via the queue into main with commit 1d0c5c9 Aug 28, 2024
2 checks passed
@AtticusKuhn AtticusKuhn deleted the chore/prove_subtraction_2 branch August 28, 2024 02:00
Copy link
Collaborator

@alexkeizer alexkeizer left a comment

Choose a reason for hiding this comment

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

Sorry for the late review! The proof looks nice, thanks for pushing it through

let carry : Bool := match i with
| 0 => false
| i + 1 => a.subCarries? b i
(!a i && b i || !xor (a i) (b i) && carry)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I find it hard to follow operator precedence here, with the xor. Some more parens would be appreciated!

Comment on lines +362 to +364
theorem subAux_inductive_lemma (i : Nat) :
a.subAux b i = ⟨(a.addAux b.neg i).1, subCarries? a b i⟩ := by
induction' i with i ih
Copy link
Collaborator

Choose a reason for hiding this comment

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

We generally prefer rather mechanical names, based on a theorems type, over names based on intended usage.
This could, e.g., be called subAux_eq_addAux_neg

Comment on lines +455 to +461
theorem ofBitVec_sub : ofBitVec (x - y) ≈ʷ (ofBitVec x) - (ofBitVec y) := by
calc
_ ≈ʷ ofBitVec (x + -y) := by rw [BitVec.sub_eq_add_neg]
_ ≈ʷ ofBitVec x + ofBitVec (-y) := ofBitVec_add
_ ≈ʷ ofBitVec x + -(ofBitVec y) := add_congr equal_up_to_refl ofBitVec_neg
_ ≈ʷ ofBitVec x - ofBitVec y := by rw [sub_eq_add_neg]

Copy link
Collaborator

Choose a reason for hiding this comment

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

Gorgeous proof!

<;> simp

@[simp]
theorem not_eq_and {a b : Bool} : ((!b) == (a && b)) = (!a && b) := by
Copy link
Collaborator

Choose a reason for hiding this comment

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

You've got an extra whitespace here, just before the :=!

AtticusKuhn added a commit that referenced this pull request Sep 2, 2024
…n on BitVectors (#559)

This PR proves the key theorem

```lean
theorem ofBitVec_sub : ofBitVec (x - y) ≈ʷ (ofBitVec x) - (ofBitVec y)  := by
  calc
  _ ≈ʷ ofBitVec (x + - y) := by rw [BitVec.sub_add_neg]
  _ ≈ʷ ofBitVec x + ofBitVec (-y) := ofBitVec_add
  _ ≈ʷ ofBitVec x + - ofBitVec y := add_congr equal_up_to_refl ofBitVec_neg
  _ ≈ʷ ofBitVec x - ofBitVec y := by rw [sub_add_neg]
```

Which shows that subtraction on BitStreams corresponds to subtraction on
BitVectors.

In doing so, this PR removes the last "sorry" from the file
BitStream.lean.


Note that this PR is a continuation of PR
#554

---------

Co-authored-by: Atticus Kuhn <atticusmkuhn@atticsmkuhn.com>
Co-authored-by: Tobias Grosser <tobias@grosser.es>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants