Skip to content

feat: add binary search shrinking for numeric types#60

Open
alok wants to merge 1 commit intoleanprover-community:mainfrom
alok:binary-search-shrink
Open

feat: add binary search shrinking for numeric types#60
alok wants to merge 1 commit intoleanprover-community:mainfrom
alok:binary-search-shrink

Conversation

@alok
Copy link

@alok alok commented Dec 27, 2025

Adds shrinkToMinimal for numeric types using binary search.

Problem: Nat.shrink only tries n/2, n/4, ... so for test n ≥ 50 from 4096, it finds 64.

Solution: Binary search finds true minimal (50).

Nat.shrink (iterative): 4096 → 64
Nat.shrinkToMinimal:    4096 → 50

Adds: Nat.binarySearch, *.shrinkToMinimal for Nat, Int, Fin, UInt8/16/32/64

Termination proven via termination_by hi - lo.

Nat.shrinkToMinimal finds the true minimal failing value via binary
search, improving on Nat.shrink which only tries n/2, n/4, ...

Example: for test `n ≥ 50`, starting from 4096:
- Nat.shrink finds 64 (smallest power of 2 ≥ 50)
- Nat.shrinkToMinimal finds 50 (true minimal)

Adds shrinkToMinimal for: Nat, Int, Fin, UInt8/16/32/64
@tom93
Copy link
Contributor

tom93 commented Feb 25, 2026

Hi, I came across this PR while browsing, I have a comment about this PR and a broader comment about Plausible's current integer shrinking:

  • This code in this PR does not appear to have integration with the Shrinkable instances, so it would not get used with Plausible.Testable.check, am I correct? It would be nice to to have minimal integer shrinking everywhere (see the next comment).

  • In the current Shrinkable implementation, the candidates for n are [n/2, n/4, n/8, ..., 0]. This doesn't make sense to me, because if n/2 fails there is no point trying n/4 (assuming the proposition is monotonic). QuickCheck's shrinkIntegral uses the candidates [0, n-n/2, n-n/4, n-n/8, ..., n-1], which gives minimal shrinking; why not use that?

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.

2 participants