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

Add property checks for BinNat #1176

Merged
merged 6 commits into from Mar 22, 2024
Merged

Conversation

johnynek
Copy link
Owner

add the same tests for BinNat as Nat.

def div2(b: BinNat) -> BinNat:
match b:
case Zero: Zero
case Odd(n): n
case Even(n): prev(n)
Copy link
Owner Author

Choose a reason for hiding this comment

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

this was a bug! yay for property checks!

diff = sub_BinNat(left, right)
match diff:
case Zero:
match cmp_BinNat(left, right):
Copy link
Owner Author

Choose a reason for hiding this comment

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

this seems bad to do internally, seems like we should be able to do this once outside the loop... should think some more.

@codecov-commenter
Copy link

codecov-commenter commented Mar 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.58%. Comparing base (04a0e87) to head (d32020f).
Report is 14 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1176      +/-   ##
==========================================
+ Coverage   91.32%   91.58%   +0.26%     
==========================================
  Files          96       96              
  Lines       11846    12010     +164     
  Branches     2675     2778     +103     
==========================================
+ Hits        10818    10999     +181     
+ Misses       1028     1011      -17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

case Odd(right):
# (2n + 2) - (2m + 1)
# if n >= m: 2(n - m) + 1
# if (2n + 2) > (2m + 1), then n > m
Copy link
Owner Author

Choose a reason for hiding this comment

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

this is wrong... if (2n + 2) > (2m + 1) then n >= m. So, that means the invariant must actually be left >= right. So that means we only know that (2n + 2) >= (2m + 1), but that's okay, because we know an odd number can't equal an even number, so we recover n >= m.

# (2n + 2) - (2m + 2) = 2(n - m)
times2(loop(left, right))
case Zero:
# this is last because it is actually unreachable
Copy link
Owner Author

Choose a reason for hiding this comment

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

this isn't true. The invariant is left >= right

@@ -97,11 +142,51 @@ def times2(b: BinNat) -> BinNat:
#2(2(n + 1)) = 2((2n + 1) + 1)
Even(Odd(n))

def sub_BinNat(left: BinNat, right: BinNat) -> BinNat:
# invariant: left > right
Copy link
Owner Author

Choose a reason for hiding this comment

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

see below: invariant: left >= right, so the comment below is wrong, we can return Zero.

@johnynek johnynek merged commit 5191197 into main Mar 22, 2024
9 checks passed
@johnynek johnynek deleted the oscar/20240317_add_bin_nat_props branch March 22, 2024 04:02
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.

None yet

2 participants