Write you some QuickCheck - Shrinking lists
10 April 2016
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 the previous post I’ve ported the function from QuickCheck that shrinks numbers. In this post I’ll be doing the same for lists.
QuickCheck shrinks lists by dropping elements from them, given a shrinking function for individual elements.
Shrinking a list of integers
To shrink a list of integers, say
[1,2,3] we need
- a function that shrinks integers (shrinker)
- a function that shrinks lists, applying for each element the shrinker
In Haskell QuickCheck 2.8.2, it looks like this:
Doing similar in F#
A possible shrinking function for lists in F# can be based on the following Haskell code snippet, taken from QuickCheck 2.8.2:
I’m going to try this one with the same values as the ones used in Haskell:
Not exactly the same results as in Haskell, but similar. That’s because the
shrinkList function in F# is based on a small part of the
shrinkList function in Haskell.
For the record, here’s the complete
shrinkList from QuickCheck 2.8.2:
Hopefully, the next post(s) will become more interesting where I’ll be gluing together some generators, shrinkers, and properties.