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 complete queue property checks #1171

Merged
merged 2 commits into from Mar 17, 2024
Merged

Conversation

johnynek
Copy link
Owner

trying to exercise property checks by adding more checks for Queue. Definitely it can be tricky to implement functions in Bosatsu due to the totality requirements. For instance, implementing Rand::one_of was a bit tricky. You have to find the right recursion strategy. I was trying to make sampling O(log N), so I needed to make a tree. To do that, I used BinNat to be able to recur on half the size of the items, rather than the naive approach.

I also found a nice trick.

Define:

external def todo(ignore: x) -> forall a. a

then we can consume args and magically produce things by doing todo((foo, bar)) and run type-check. Then it fails at runtime since we don't actually implement todo. We could use this strategy except possibly make that function only exist in predef when running type-check (which doesn't output anything).

case Zero: acc
case Succ(n):
# (n + 1) / 2 = n/2 if n is even, else n/2 + 1
if is_even(n): loop(n, acc)
Copy link
Owner Author

Choose a reason for hiding this comment

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

we know that every other item is even. This algorithm is quadratic, but we can make it O(N).

@@ -48,7 +50,7 @@ def fold_Queue(Queue(f, b): Queue[a], init: b, fold_fn: (b, a) -> b) -> b:
b.reverse().foldLeft(front, fold_fn)

def reverse_Queue(Queue(f, b): Queue[a]) -> Queue[a]:
Queue(b.reverse(), f.reverse())
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 bug was detected by property checks! Yay!

rev_rand = smaller.map_Rand(reverse_Queue)
one_of(pop_rand, [push_rand, rev_rand])

rand_Queue_Int: Rand[Queue[Int]] = rand_Queue_depth(int_to_Nat(50))
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 building a pretty giant tree perhaps... definitely the checks take a while to run.

@codecov-commenter
Copy link

codecov-commenter commented Mar 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.48%. Comparing base (04a0e87) to head (0cb4658).
Report is 10 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    #1171      +/-   ##
==========================================
+ Coverage   91.32%   91.48%   +0.16%     
==========================================
  Files          96       96              
  Lines       11846    12010     +164     
  Branches     2675     2778     +103     
==========================================
+ Hits        10818    10987     +169     
+ Misses       1028     1023       -5     

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

@johnynek johnynek merged commit 39adf19 into main Mar 17, 2024
9 checks passed
@johnynek johnynek deleted the oscar/20240316_more_queue_tests branch March 17, 2024 16:58
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