| 1 | --- |
| 2 | tags: technical, details, python, alternatives |
| 3 | date: 2016-12-08 9:00 |
| 4 | title: Compositional shrinking |
| 5 | author: drmaciver |
| 6 | --- |
| 7 | |
| 8 | In [my last article about shrinking](../integrated-shrinking/), |
| 9 | I discussed the problems with basing shrinking on the type of the values |
| 10 | to be shrunk. |
| 11 | |
| 12 | In writing it though I forgot that there was a halfway house which is |
| 13 | also somewhat bad (but significantly less so) that you see in a couple |
| 14 | of implementations. |
| 15 | |
| 16 | This is when the shrinking is not type based, but still follows the |
| 17 | classic shrinking API that takes a value and returns a lazy list of |
| 18 | shrinks of that value. Examples of libraries that do this are |
| 19 | [theft](https://github.com/silentbicycle/theft) and |
| 20 | [QuickTheories](https://github.com/NCR-CoDE/QuickTheories). |
| 21 | |
| 22 | This works reasonably well and solves the major problems with type |
| 23 | directed shrinking, but it's still somewhat fragile and importantly |
| 24 | does not compose nearly as well as the approaches that Hypothesis |
| 25 | or test.check take. |
| 26 | |
| 27 | Ideally, as well as not being based on the types of the values being |
| 28 | generated, shrinking should not be based on the actual values generated |
| 29 | at all. |
| 30 | |
| 31 | This may seem counter-intuitive, but it actually works pretty well. |
| 32 | |
| 33 | <!--more--> |
| 34 | |
| 35 | Rather than going into implementation details just yet, lets start |
| 36 | with why this is important. |
| 37 | |
| 38 | Consider the example from the last post: |
| 39 | |
| 40 | |
| 41 | ```python |
| 42 | from hypothesis import given |
| 43 | from hypothesis.strategies import integers |
| 44 | |
| 45 | even_numbers = integers().map(lambda x: x * 2) |
| 46 | |
| 47 | |
| 48 | @given(even_numbers) |
| 49 | def test_even_numbers_are_even(n): |
| 50 | assert n % 2 == 0 |
| 51 | ``` |
| 52 | |
| 53 | We took a strategy and composed it with a function mapping over |
| 54 | the values that that strategy produced to get a new strategy. |
| 55 | |
| 56 | Suppose the Hypothesis strategy implementation looked something |
| 57 | like the following: |
| 58 | |
| 59 | ```python |
| 60 | class SearchStrategy: |
| 61 | def generate(self, random): |
| 62 | raise NotImplementedErro() |
| 63 | |
| 64 | def shrink(self, value): |
| 65 | return () |
| 66 | ``` |
| 67 | |
| 68 | i.e. we can generate a value and we can shrink a value that we've |
| 69 | previously generated. By default we don't know how to generate values |
| 70 | (subclasses have to implement that) and we can't shrink anything, |
| 71 | which subclasses are able to fix if they want or leave as is if |
| 72 | they're fine with that. |
| 73 | |
| 74 | (This is in fact how a very early implementation of it looked) |
| 75 | |
| 76 | This is essentially the approach taken by theft or QuickTheories, |
| 77 | and the problem with it is that under this implementation the |
| 78 | 'map' function we used above is impossible to define in a way |
| 79 | that preserves shrinking: In order to shrink a generated value, |
| 80 | you need some way to invert the function you're composing with |
| 81 | (which is in general impossible even if your language somehow |
| 82 | exposed the facilities to do it, which it almost certainly |
| 83 | doesn't) so you could take the generated value, map it back |
| 84 | to the value that produced it, shrink that and then compose |
| 85 | with the mapping function. |
| 86 | |
| 87 | Hypothesis and test.check both support even more complicated |
| 88 | composition of strategies (Hypothesis somewhat better than |
| 89 | test.check - both support the same operations, but Hypothesis's |
| 90 | underlying implementation works somewhat better for more |
| 91 | complicated compositions), but even the simplest of compositions |
| 92 | fails if you need to be able to shrink arbitrary values. |
| 93 | |
| 94 | The key idea for fixing this is as follows: In order to shrink |
| 95 | *outputs* it almost always suffices to shrink *inputs*. Although |
| 96 | in theory you can get functions where simpler input leads to more |
| 97 | complicated output, in practice this seems to be rare enough |
| 98 | that it's OK to just shrug and accept more complicated test |
| 99 | output in those cases. |
| 100 | |
| 101 | Given that, the way to shrink the output of a mapped strategy |
| 102 | is to just shrink the value generated from the first strategy |
| 103 | and feed it to the mapping function. |
| 104 | |
| 105 | Which means that you need an API that can support that sort |
| 106 | of shrinking. |
| 107 | |
| 108 | The way this works in test.check is that instead of generating |
| 109 | a single value it generates an entire (lazy) tree of values |
| 110 | with shrinks for them. See [Reid Draper's article on the |
| 111 | subject](http://reiddraper.com/writing-simple-check/) for slightly |
| 112 | more detail. |
| 113 | |
| 114 | This supports mapping fairly easily: We just apply the mapping |
| 115 | function to the rose tree - both the initial generated value, |
| 116 | and all the shrunk child values. |
| 117 | |
| 118 | Hypothesis's implementation is more complicated so will have to |
| 119 | wait for another article, but the key idea behind it is that |
| 120 | Hypothesis takes the "Shrinking outputs can be done by shrinking |
| 121 | inputs" idea to its logical conclusion and has a single unified |
| 122 | intermediate representation that *all* generation is based off. |
| 123 | Strategies can provide hints about possibly useful shrinks to |
| 124 | perform on that representation, but otherwise have very little |
| 125 | control over the shrinking process at all. This supports mapping |
| 126 | even more easily, because a strategy is just a function which |
| 127 | takes an IR object and returns a value, so the mapped strategy |
| 128 | just does the same thing and applies the mapping function. |
| 129 | |
| 130 | Obviously I think Hypothesis's implementation is better, but |
| 131 | test.check's implementation is entirely respectable too and |
| 132 | is probably easier to copy right now if you're implementing |
| 133 | a property based testing system from scratch. |
| 134 | |
| 135 | But I do think that whichever one you start from it's important |
| 136 | to take away the key idea: You can shrink outputs by shrinking |
| 137 | inputs, and strategies should compose in a way that preserves |
| 138 | shrinking. |
| 139 | |
| 140 | The result is significantly more convenient to use because it |
| 141 | means that users will rarely or never have to write their own |
| 142 | shrinking functions, and there are fewer possible places for |
| 143 | shrinking and generation to get out of sync. |