This post is part of a series of posts on implementing a minimal version of QuickCheck from scratch. The source code is available on GitHub.
In this post I’ll be porting the function from QuickCheck that shrinks numbers.
When reporting back the actual input that caused a function to fail, QuickCheck first cleans the noise by reporting back the counterexample. — This is called shrinking. See how it works here.
All the shrinking functions have the same signature:
So in F# it can look like this:
QuickCheck shrinks towards smaller numeric values with the following generic function:
(View on Hackage)
Let’s try this one out first:
Here’s the equivalent one in F#:
I’m going to try this one with the same values as the ones used in Haskell:
Shrinking not only integers, but floats, decimals, and so on
Here is again the
shrinkNumber number function that was ported from Haskell:
0, the F# compiler infers that this function works with integers.
Fortunately, there’s a way to make this polymorphic with compile-time generics:
Here’s the end result:
And now the type has become generic:
Let’s try this one out: