Abstract
Shrinking is what a property-based testing library does after it finds a failing input: it searches for the smallest input that still fails, so the bug is easy to read. Choice-sequence shrinking, the model behind Hypothesis (MacIver and Donaldson, ECOOP 2020), doesn’t shrink the value at all. It records every random choice the generator made, shrinks that list of numbers, and replays it through the same generator.
”Smaller” means shortlex: a shorter record is smaller, and between two of the same length the first differing choice decides. Because every candidate goes back through the generator, every shrunk value is one the generator could have made.
Cue
- What does the shrinker shrink? → The record of random choices, not the generated value
- What does “smaller” mean? → Shortlex: fewer choices first, then lexicographically smaller
- Why replay through the generator? → Every candidate is a value the generator can make, so its invariants hold for free
- Why does it handle dependent draws? → A later draw (the elements) is replayed after an earlier one (the length), so they stay consistent
- Why does it always stop? → Shortlex is a well-order on finite records: every accepted step is strictly smaller
- What else must a candidate do besides fail? → Fail as the same bug, or the shrinker slides onto a different, simpler one
The idea
A generator never sees a random number generator directly. It asks a choice source for bounded choices:
- “a length between 0 and 10” → 3
- ”an int between 0 and 1000” → 416, then 309, then 276
The source records them: . The value is just what those choices produce.
When the property fails, the library keeps the record. Shrinking proposes smaller records, replays each through the same generator and property, and keeps one only if:
where, for records and :
The search repeats until no proposal is accepted.
Replay, not reconstruct
A shrunk record can run out, or hold a value above a new bound. The source then draws the minimum (0) past the end and clamps a value to its bound. It records what it actually drew, and that record, not the proposal, is what’s compared and kept.
The passes
Each pass proposes a kind of smaller record:
| Pass | Proposal | What it does to the value |
|---|---|---|
| Delete chunks | Remove 8, 4, 2, then 1 consecutive choices | Drops list elements, branches, whole sub-values |
| Delete and lower | Remove one choice, and lower an earlier one by 1 | Removes an element and its list’s length together |
| Lower one choice | Binary search for the smallest value that still fails | , a length |
| Redistribute | for a later | Moves weight from an early element to a later one |
The last two matter together. Lowering one choice at a time gets stuck when the bug depends on a sum: no single element can drop without the property passing. Redistributing moves the weight to the end, and then the earlier choices can fall to 0.
Real shrinkers have more passes, such as sorting runs of choices, minimising pairs that must stay equal, and trying common values like 1 and . These four show the mechanism.
Worked example
Property: a list of ints in sums to at most 1000. The length is drawn first (0 to 10), then each element.
The first failing input found, seed 42:
| Record | Value | |
|---|---|---|
| Found | ||
| Deleting and lowering only | (stuck) | |
| With redistribution | ||
| With delete-and-lower |
- Without redistribution, it stops at three elements. Each one is needed for the sum to pass 1000.
- is the shortlex minimum. Two elements are the fewest that can exceed 1000, since one is capped at 1000. Among two-element records, beats at the first differing choice.
- Every seed ends there. Seeds 1, 7, 99, 2026 and 123456 each found a different failing list, and each shrank to exactly . The ordering, not the luck of the search, decides the answer. That’s what makes a shrunk counterexample stable enough to paste into a test.
Implementation (Java)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import java.util.function.Predicate;
/** Choice-sequence shrinking: record every choice, shrink the record, replay it. */
public class Shrink {
/** Hands out bounded choices, replaying a prefix first, and records what it handed out. */
static final class Choices {
private final long[] prefix;
private final Random random;
private final List<Long> drawn = new ArrayList<>();
Choices(long[] prefix, Random random) {
this.prefix = prefix;
this.random = random;
}
/** A choice in [0, max]. Replayed if the prefix has one, random otherwise (0 when shrinking). */
long draw(long max) {
int i = drawn.size();
long value;
if (i < prefix.length) {
value = Math.min(prefix[i], max); // a shrunk value may exceed a new bound: clamp it
} else {
value = random == null ? 0 : (long) (random.nextDouble() * (max + 1));
}
drawn.add(value);
return value;
}
long[] record() {
return drawn.stream().mapToLong(Long::longValue).toArray();
}
}
/** Shortlex: shorter is smaller; at equal length, the first differing choice decides. */
static boolean smaller(long[] a, long[] b) {
if (a.length != b.length) return a.length < b.length;
return Arrays.compare(a, b) < 0;
}
/** Replays a candidate; returns what it actually drew if the property still fails, else null. */
static <T> long[] stillFails(long[] candidate, Function<Choices, T> gen, Predicate<T> property) {
Choices c = new Choices(candidate, null);
T value = gen.apply(c);
return property.test(value) ? null : c.record();
}
static <T> long[] shrink(long[] failing, Function<Choices, T> gen, Predicate<T> property) {
long[] best = failing;
boolean improved = true;
while (improved) {
improved = false;
// Pass 1: delete chunks of choices, big chunks first.
for (int k = 8; k >= 1; k /= 2) {
for (int i = 0; i + k <= best.length; i++) {
long[] candidate = new long[best.length - k];
System.arraycopy(best, 0, candidate, 0, i);
System.arraycopy(best, i + k, candidate, i, best.length - i - k);
long[] got = stillFails(candidate, gen, property);
if (got != null && smaller(got, best)) {
best = got;
improved = true;
i--; // the same position now holds the next choice
}
}
}
// Pass 1b: delete one choice and lower an earlier one by 1, such as a list's length.
for (int i = 1; i < best.length; i++) {
for (int j = 0; j < i; j++) {
if (best[j] == 0) continue;
long[] candidate = new long[best.length - 1];
System.arraycopy(best, 0, candidate, 0, i);
System.arraycopy(best, i + 1, candidate, i, best.length - i - 1);
candidate[j]--;
long[] got = stillFails(candidate, gen, property);
if (got != null && smaller(got, best)) {
best = got;
improved = true;
break;
}
}
}
// Pass 2: lower each choice as far as it will go, by binary search.
for (int i = 0; i < best.length; i++) {
long lo = 0, hi = best[i];
while (lo < hi) {
long mid = lo + (hi - lo) / 2;
long[] candidate = best.clone();
candidate[i] = mid;
long[] got = stillFails(candidate, gen, property);
if (got != null && smaller(got, best)) {
best = got;
improved = true;
if (i >= best.length) break; // lowering it shortened the record
hi = best[i];
} else {
lo = mid + 1;
}
}
}
// Pass 3: move value from an earlier choice to a later one: (a, b) -> (a - d, b + d).
for (int i = 0; i < best.length; i++) {
for (int j = i + 1; j < best.length; j++) {
long lo = 1, hi = best[i];
while (lo <= hi) {
long d = hi - (hi - lo) / 2; // the largest move first, then smaller ones
long[] candidate = best.clone();
candidate[i] -= d;
candidate[j] += d;
long[] got = stillFails(candidate, gen, property);
if (got != null && smaller(got, best)) {
best = got;
improved = true;
break;
}
hi = d - 1;
}
if (i >= best.length) break;
}
}
}
return best;
}
/** A list: its length is drawn first, then each element. The elements depend on the length. */
static List<Long> listOfInts(Choices c) {
int n = (int) c.draw(10);
List<Long> xs = new ArrayList<>();
for (int i = 0; i < n; i++) xs.add(c.draw(1000));
return xs;
}
public static void main(String[] args) {
Predicate<List<Long>> sumAtMost1000 = xs -> xs.stream().mapToLong(Long::longValue).sum() <= 1000;
Random random = new Random(42);
for (int run = 0; run < 1000; run++) {
Choices c = new Choices(new long[0], random);
List<Long> xs = listOfInts(c);
if (!sumAtMost1000.test(xs)) {
long[] found = c.record();
System.out.println("found: " + Arrays.toString(found) + " -> " + xs);
long[] shrunk = shrink(found, Shrink::listOfInts, sumAtMost1000);
List<Long> value = listOfInts(new Choices(shrunk, null));
System.out.println("shrunk: " + Arrays.toString(shrunk) + " -> " + value);
return;
}
}
System.out.println("no failure in 1000 runs");
}
}Notes on the Java:
- One
draw(max)is the whole interface. The list generator never knows it’s being shrunk. Any generator built fromdrawgets shrinking for free, including records, trees and strings. stillFailsreturns what was drawn, not the candidate. Clamping and running past the end both change the record, and only the real one is comparable.- Every pass checks
smaller(got, best). That single rule is why the loop terminates and why the answer is canonical. - It’s a teaching version. The bug check is “still fails”. A real one, like proptastic’s, also requires the same bug: the same exception type, and the same place or message shape. Without that, shrinking a property with two bugs slides from the hard one to the easy one.
Complexity
Each proposal costs one run of the generator and the property, so cost counts property runs, not choices. Chunk deletion is proposals per round for a record of choices, and binary search is per choice for values up to . Redistribution is the expensive pass, , which is why real shrinkers run it last and stop early. Rounds repeat until nothing is accepted. In the worked example, the six seeds took 184 to 341 property runs each to shrink records of 5 to 9 choices to .
Compared with other models
| Type-based (QuickCheck) | Integrated (Hedgehog, proptest’s value trees) | Choice sequences (Hypothesis, proptastic) | |
|---|---|---|---|
| What shrinks | The value, by a shrinker per type | A tree of shrinks built alongside the value | The record of choices |
| Per-type work | A shrinker for every type | None, but built into each generator | None |
| Generator invariants | Can be broken by the shrinker | Kept | Kept, by replay |
Dependent draws (flatMap) | Manual | Weak: inner shrinks are lost on bind | Natural: replay regenerates them |
| Driven by a fuzzer’s bytes | No | No | Yes: the bytes are the choices |
The last row is why proptastic chose this model: a coverage-guided fuzzer (libFuzzer, AFL, Jazzer) can feed the same property its byte buffer as the choice source, and shrinking and the saved corpus work unchanged.
Strengths and blind spots
| ✅ No shrinker per type | Anything built from draw shrinks. |
| ✅ Valid by construction | A shrunk value is one the generator could make. |
| ✅ Canonical | Shortlex makes the answer independent of the seed and the path. |
| ✅ Fuzzer-ready | Bytes from a fuzzer are a choice record. |
| ❌ Shrinks the record, not the meaning | A generator that maps small choices to big values (a hash, say) shrinks badly. Draw sizes and indices as choices, not derived numbers. |
| ❌ Cost is property runs | A slow property makes shrinking slow. Cap it with a budget. |
| ❌ Needs the same-bug rule | Without it, two bugs in one property blur into the simpler one. |
Related
- Proptastic shrinking model: why proptastic picked this model, and the proof in its spike
- property-based testing
- MacIver & Donaldson, Test-Case Reduction via Test-Case Generation: Insights from the Hypothesis Reducer (ECOOP 2020)
- Claessen & Hughes, QuickCheck: A Lightweight Tool for Random Testing of Haskell Programs (ICFP 2000)
- Zeller & Hildebrandt, Simplifying and Isolating Failure-Inducing Input (IEEE TSE, 2002): delta debugging, the ancestor of chunk deletion