# Shrinking failing input using a SAT solver

Advance warning: This is mostly me writing up a negative to neutral result. The technique described in this post works OK, but it doesn’t seem to work better than a well optimized delta debugging and as such does not currently justify its complexity. It also doesn’t work very well when you start from large examples, but that’s fortunately not a huge problem using the ideas I outlined in my last post because you can work with smaller sequences until the example is small enough to be feasible when approached bytewise.

My hope was that it would give a system that was much better at finding longer intervals to delete (delta debugging only guarantees that you can not delete any byte. Guaranteeing that you cannot delete any interval provably requires a quadratic number of tests to check, but I was hoping that this would be able to exploit structure found in the string to find more intervals that pure delta debugging would have missed.

I’ve got some ideas I want to pursue to extend it that might make it worth it, but this post just describes the current state of work in progress. Also I might end up incorporating it into my shrinker anyway and hope that I can improve its working with more cleverness later (I already have a prototype of doing this).

Anyway, disclaimers over, on to the idea.

We are trying to take a string $$S$$ and a predicate $$f$$. We are trying to construct a string $$T$$ of minimal length such that $$f(T)$$ is true.

This builds on my previous idea of using regular language induction to aid shrinking. The version I described in that post appears to be too slow to make work.

The core idea is still that we are looking for a DFA with fewer than $$|S|$$ nodes such that every accepted string satisfies $$f$$. If we can find such a DFA then we can shrink $$S$$ because the shortest path to an accepted node must have no more than the number of nodes, which is fewer than $$|S|$$ by assumption.

The idea is to try to manufacture a DFA from $$S$$ using a SAT solver (Disclaimer: I actually used the naive algorithm that they aimed to replace because I didn’t quite understand their work and wanted to prototype something quickly) to colour the nodes in a manner consistent with the transitions observed in $$S$$. (i.e. if we colour the indices $$i$$ and $$j$$ the same and $$S[i] = S[j]$$ then we colour $$i + 1$$ and $$j + 1$$ the same).

We maintain a set of pairs of indices which we know do not correspond to the same state in the DFA. We initialize it to $$\{(0, |S|\}$$ at the start and will add to it as we go.

We now produce a colouring consistent with that set and the transition requirement.

If that colouring gives a unique colour to every index, stop. There is no DFA with fewer than $$|S|$$ nodes that matches $$S$$ and.

Otherwise, produce the quotient DFA and look at the minimal path to an accepting node. If it satisfies $$f$$, we have shrunk the string. Start again from the beginning with the new string (this usually doesn’t work, but it’s worth checking).

If that didn’t work, we now produce either a strictly smaller string satisfying $$f$$ or we increase the size of our set of known inconsistencies as follows:

For each colour, we look at the smallest and the largest index in it, $$i, j$$. If deleting the interval $$[i, j)$$ from the  string produces a string satisfying $$f$$ then we have shrunk the string. Start again from the beginning with the new string. Otherwise, we have proved that the subsequences of $$S$$ up to $$i$$ and $$j$$ respectively must live in distinct states (because following the suffix of $$S$$ starting at $$j$$ produces different results). Add $$(i, j)$$ to the set of inconsistencies. Do this for each colour (starting from the ones which produce the largest gap so we maximize how much we shrink by), and then if none of them produced a shrink then try colouring again.

Eventually we’ll either hit the point where we need all $$|S| + 1$$ colours to colour the indices, or we’ll have shrunk the string.

Note: I think this doesn’t actually guarantee even 1-minimality like delta debugging does. The problem is that you might have an index that is not equivalent to its successor index even though you can delete the character there. I’m not currently sure if this algorithm can witness that possibility or not.

Anyway, I’ve been running the example I used to prototype structureshrink (the C++ hello world) with this as the list minimization algorithm. It seems to do alright, but it’s nothing to write home about, but it’s been an interesting experiment and I’m glad to have got the excuse to play with minisat.

This entry was posted in programming, Python on by .