I'm learning the ropes of QuickCheck >= 2.6 but I don't understand what a shrink is. From looking at the type signature shrink looks more like expand! Please illuminate me :)
相关问题
- Understanding do notation for simple Reader monad:
- Making Custom Instances of PersistBackend
- Haskell: What is the differrence between `Num [a]
- applying a list to an entered function to check fo
- Haskell split a list into two by a pivot value
相关文章
- Is it possible to write pattern-matched functions
- Haskell underscore vs. explicit variable
- Top-level expression evaluation at compile time
- Stuck in the State Monad
- foldr vs foldr1 usage in Haskell
- List of checkboxes with digestive-functors
- How does this list comprehension over the inits of
- Replacing => in place of -> in function type signa
A single
shrink
is a stepwise reduction in complexity of someArbitrary
test case (an "immediate shrink"). This might be something like2 -> 1
or1:[] -> []
. For more complex types there may be multiple ways to "incrementally shrink" a type so you specify all of them in a list.For example, trees might be shrunk by removing any one leaf, thus if there are
n
leaves then shrink produces a list of lengthn
.When QuickCheck finds an input that violates a property, it will first try to find smaller inputs that also violate the property, in order to give the developer a better message about the nature of the failure.
What it means to be „small“ of course depends on the datatype in question; to QuickCheck it is anything that comes out from from the
shrink
function.It is best explained in a QuickCheck session:
So here QuickCheck was able to give the smallest counter-example, but judging from the comments, it first had a larger list in mind and then reduced it using
shrink
. To have a closer look at what is happening, we useverboseCheck
:QuickCheck tries a few lists for which the proposition holds, and then finds
[-1,-2,5,4,2]
. Now it reduces the list, by trying sublists of it. You can convince yourself in GHCi thatshrink [-1,-2,5,4,2] == [[],[5,4,2],[-1,-2,2],...
and the second entry is the first that still fails the test. QuickCheck then continues with that and shrinks further:shrink [5,4,2] == [[],[4,2],[5,2],...
, and furthershrink [5,2] [[],[2],[5],...
. Lastly it tries to shrink[5]
further, but none ofshrink [5] == [[],[0],[3],[4]]
fail the proposition, so the final count-example is[5]
.