How to improve your Quickcheck implementation with this one weird trick

When asked what’s genuinely novel in Hypothesis as opposed to mere optimisation, I usually cite three things:

  1. Strategies carry simplification and serialization with them but still compose monadically.
  2. The data generation can hit corners of the search space that standard Quickcheck can not, and adapt dynamically to requirements.
  3. The simplification is much more sophisticated than the standard Quickcheck interface and can adapt to the structure of the problem much better.

It took me a surprisingly long time to notice that all three of these rely on essentially the same tweak to the standard API.

The tweak is this: Introduce an intermediate representation.

  1. By introducing an intermediate template type which is then reified to the final result, you can simplify a mapped object by simplifying the argument.
  2. Instead of generating a random value, you first generate a random parameter and then draw a conditional value given that parameter. As well as producing more interesting results this way (because of how you compose parameters for e.g. lists) this also lets you reuse parameters which are more likely to give you a useful result.
  3. Instead of having a single simplify function, you have a list of simplifiers which you apply in turn.

Each of these have taken what was a single step and broken it up into two steps which you can naturally compose to give the original. We then don’t compose these in the obvious way, but instead change things around just enough to give something strictly more powerful while still mostly retaining the original characteristics.

I’m not sure what the broader significance of this is yet, but it seems likely that this approach has other areas to which you could apply it.

This entry was posted in Hypothesis, Python, Uncategorized on by .

One thought on “How to improve your Quickcheck implementation with this one weird trick

  1. Richard Bradley

    “All problems in computer science can be solved by another level of indirection… Except for the problem of too many layers of indirection.”

Comments are closed.