Algorithm Search⌘K

Techniqueadvanced

Program sketching

Also called sketch-based synthesis.

The programmer writes a program with holes and the synthesizer fills the holes with constants or expressions so the assertions hold.

Program sketching is a way to write a program in two parts. The programmer writes a sketch: a program with holes, written ??, where the details are unknown. A synthesizer fills the holes so that the program meets its specification, for example the assertions in the code or equality with a slow reference program, for all inputs. The programmer gives the structure, which is hard for a machine to guess. The machine finds the constants, indices and small expressions, which are hard for a person to get right. Solar-Lezama and others built the method into the Sketch language (2005 to 2008). For a sketch with bounded inputs and loops, the question is ∃c ∀x φ(x,c)\exists c\, \forall x\, \varphi(x, c), and Sketch answers it with counterexample-guided inductive synthesis and a SAT solver.

In plain words

Say you know that a trick with "and" and "plus" can clear the lowest set bit of a number, but you forget the exact constant. You write the program with a blank where the constant goes, and you state what the result must be. The tool tries to fill the blank.

It does not try all inputs for every possible constant. It picks a constant that works on a few inputs, then looks for an input where that constant fails. That input joins the list, and the tool picks a new constant. When no failing input is left, the blank is filled.

Holes can also stand for a choice between a few expressions, so you can say "one of these operators goes here" and let the tool decide.

When it applies

Use it when you know the shape of the solution but not its details. Typical cases are bit tricks, the constants and masks of a fast routine, the index arithmetic of a loop, and the order of a few statements. The specification is a set of assertions, or a simple reference program that the fast one must match.

The method needs a bounded problem. The inputs must have a fixed width, such as 8 or 32 bits, loops must be unrolled to a fixed depth, and recursion must be inlined to a fixed depth. Then the program and its assertions become a finite formula. Without bounds, even a sketch with no holes asks whether a program meets its assertions, and that is undecidable by Rice's theorem.

It does not fit when the structure itself is unknown. Then use a search over a whole grammar, as in syntax-guided synthesis, or a search from examples.

The template

Step 1: the specification. Write a reference program spec(x), or assertions inside the code. The sketch must give the same result as spec on every input, or no assertion may fail.

Step 2: the sketch. Write the program with holes. An integer hole ?? stands for a constant of a fixed number of bits. A choice {| e1 | e2 | e3 |} stands for one of the listed expressions. A generator is a function whose body has holes, and each call of it gets its own copies of the holes. All holes together form the control vector cc.

Step 3: the bounds. Fix the width of the inputs and of the holes, the number of times each loop is unrolled, and the depth of inlining. An assertion that fails after the last unrolling counts as a failure, so the bound does not hide a bug inside the bound.

Step 4: the formula. Run the sketch on a symbolic input xx and symbolic holes cc, as in symbolic execution, and translate each operation into a Boolean circuit by bit-blasting. The result is a formula φ(x,c)\varphi(x, c) that is true when the assertions hold on input xx with holes cc.

Step 5: the search. Solve ∃c ∀x φ(x,c)\exists c\, \forall x\, \varphi(x, c) by CEGIS. Keep a small set EE of inputs. A SAT call finds a cc with φ(x,c)\varphi(x, c) for all x∈Ex \in E. A second SAT call looks for an xx with ¬φ(x,c)\neg \varphi(x, c). If there is one, add it to EE and repeat. If there is none, cc is correct for all bounded inputs. If the first call fails, no completion exists within the bounds. The loop stops because the space of cc is finite; the proof is on the CEGIS page.

Step 6: the program. Put the values of cc into the sketch. The result is an ordinary program with no holes. Check it outside the bounds with a verifier or with tests.

How hard is it. For a sketch where holes and inputs are bits and φ\varphi is a polynomial-size circuit, the question "is there a completion?" is complete for Σ2P\Sigma_2^P, the second level of the polynomial hierarchy.

Proof. It is in Σ2P\Sigma_2^P: the certificate is cc, of polynomial size, and checking ∀x φ(x,c)\forall x\, \varphi(x, c) is a co-NP question. It is Σ2P\Sigma_2^P-hard: take any true or false quantified Boolean formula ∃c ∀x ψ(c,x)\exists c\, \forall x\, \psi(c, x). The sketch "assert ψ(??,x)\psi(??, x)" with input xx and one hole bit for each variable of cc has a completion exactly when the formula is true. Deciding such formulas is Σ2P\Sigma_2^P-complete (Stockmeyer and Wrathall, 1976). □\square

So no polynomial algorithm is expected, and CEGIS is a way to split the hard query into many SAT calls that are often easy.

Classic examples

Clear the lowest set bit. On 8-bit words, the specification is spec(x) = x & (x - 1), with arithmetic modulo 256. The sketch is

int8 f(int8 x) implements spec { return x & (x + ??); }

with an 8-bit hole. Of the 256 values, only ??=255?? = 255, which is −1-1 modulo 256, gives spec on all inputs. Run CEGIS, where the synthesizer returns the least constant that fits the inputs in EE and the verifier returns the least input where the constant fails:

round candidate counterexample
1 0 1
2 1 2
3 3 4
4 7 8
5 to 8 15, 31, 63, 127 16, 32, 64, 128
9 255 none

The constant 2j−12^j - 1 fits every input whose lowest set bit is below bit jj, and fails on 2j2^j. So the loop needs 8 counterexamples out of 256 inputs, one for each bit, and stops after 9 rounds.

A choice of operators. In the sketch return x OP1 (x OP2 ??); each of OP1 and OP2 is a choice among the five operators and, or, exclusive or, plus and minus. It has 5⋅5⋅256=64005 \cdot 5 \cdot 256 = 6400 completions. Two of them are correct: x & (x + 255) and x & (x - 1). With candidates in a fixed order, CEGIS again stops after 9 rounds; its counterexamples are 3, 2, 4, 8, 16, 32, 64 and 128. So 8 inputs out of 256 decide among 6,400 candidates.

Loops with index holes. A sketch of an in-place array reversal can leave the loop bound and the index of the swap partner as holes, for example for i < n / ?? : swap(a[i], a[n - ?? - i]). With 4-bit holes, nn bounded by 8 and the loop unrolled up to 8 times, the divisor 2 and the offset 1 are the only completion that reverses every array, and an index out of range counts as a failed assertion. The bound on nn matters: the formula says nothing about longer arrays.

Bit-level kernels. The first sketching work (Solar-Lezama and others 2005) targeted bit-stream programs, where a short reference program says what each output bit is, and a fast version works on whole machine words with masks and shifts. The 2006 paper extended this to finite programs in a C-like language.

Variations

  • Generators. A generator is a function with holes that the synthesizer expands at each call. A recursive generator describes a whole grammar of expressions up to a depth, so a sketch can contain a small syntax-guided synthesis problem.
  • Minimization. Ask for the completion with the least value of a cost written in the sketch, such as the number of operations. The solver repeats the search with a bound on the cost that drops each time.
  • Solver-aided languages. Rosette (Torlak and Bodík 2013) adds holes and synthesis queries to a host language. It evaluates the program symbolically and sends the formula to an SMT solver, so holes can also range over integers and other theories.
  • Concurrent programs. The inputs include the choices of the scheduler, so the formula holds for all thread interleavings up to the bound.
  • Hole-free sketches. With no holes, the second query of CEGIS alone is bounded model checking of the assertions.

Pitfalls

  • Bounds are not proofs. A completion correct for all 8-bit inputs and for loops of up to 8 rounds may fail on 32-bit inputs or on longer loops. Check the result outside the bounds.
  • Holes are tied to the width. Solve the first example with 4-bit words and the answer is ??=15?? = 15, which is −1-1 at 4 bits. Copied into 8-bit code it is wrong: on x=16x = 16 it gives 16&31=1616 \mathbin{\&} 31 = 16, and the right result is 0. Write the constant as −1-1 or solve at the final width.
  • A failed search says little. If no completion exists, the solver reports only that. The cause can be a wrong structure, a hole that is too narrow, or a bound that is too small. Change one thing at a time.
  • Weak specifications. Assertions that check only part of the behaviour give programs that pass them and do the wrong thing. A reference program is a stronger specification than a few assertions.
  • Too many holes. The formula grows with the bit width, the number of holes, and the unrolling depth. A sketch with many wide holes can make each SAT call slow, even if CEGIS needs few rounds.
  • Not the same as satisfiability. Finding cc and xx with φ(x,c)\varphi(x, c) is one SAT call, but it shows only that the completion works on one input. The question is for all inputs.

Exercises

  1. In the first example, why is ??=254?? = 254 wrong? Answer. On x=1x = 1, x+254=255x + 254 = 255 and 1&255=11 \mathbin{\&} 255 = 1, but spec(1) =0= 0.
  2. Give a sketch with one 8-bit hole for spec(x) = x & -x, the lowest set bit, using ¬x\neg x (bitwise not) and ++. Answer. x & (~x + ??), with ??=1?? = 1, since −x=¬x+1-x = \neg x + 1 modulo 256.
  3. A sketch has hh hole bits and nn input bits. Give a bound on the number of CEGIS rounds. Answer. Each round removes at least one candidate and adds a new input, so there are at most min⁡(2h,2n)+1\min(2^h, 2^n) + 1 rounds.
  4. Show that a sketch whose assertions hold for no input has no completion, and that a sketch whose holes do not occur in the assertions has a completion exactly when the program is correct for all inputs. Answer. In the first case ∀x φ(x,c)\forall x\, \varphi(x, c) is false for every cc. In the second case φ\varphi does not depend on cc, so ∃c ∀x φ(x)\exists c\, \forall x\, \varphi(x) is the same as ∀x φ(x)\forall x\, \varphi(x), which is verification.

References

  1. Armando Solar-Lezama, Rodric Rabbah, Rastislav Bodík and Kemal Ebcioğlu, Programming by sketching for bit-streaming programs, Programming Language Design and Implementation (PLDI 2005), pp. 281-294, the first form of sketching, for bit-level stream programs in StreamBit
  2. Armando Solar-Lezama, Liviu Tancau, Rastislav Bodík, Sanjit Seshia and Vijay Saraswat, Combinatorial sketching for finite programs, Architectural Support for Programming Languages and Operating Systems (ASPLOS 2006), pp. 404-415, the sketch language with integer holes, the reduction of a finite sketch to an exists-forall problem, and the counterexample-guided loop with a SAT solver
  3. Armando Solar-Lezama, Program synthesis by sketching, PhD thesis, University of California, Berkeley, 2008, technical report UCB/EECS-2008-177, the Sketch language, generators, the semantics of holes, the encoding into Boolean formulas and CEGIS
  4. Armando Solar-Lezama, Program sketching, International Journal on Software Tools for Technology Transfer 15(5-6), 2013, pp. 475-495, an overview of the language, of the solver and of its use
  5. Emina Torlak and Rastislav Bodík, Growing solver-aided languages with Rosette, Onward! 2013, pp. 135-152, holes and synthesis queries inside a host language with symbolic evaluation
  6. Sumit Gulwani, Oleksandr Polozov and Rishabh Singh, Program Synthesis, Foundations and Trends in Programming Languages 4(1-2), 2017, the parts on constraint-based search and on sketching
  7. Larry J. Stockmeyer, The polynomial-time hierarchy, Theoretical Computer Science 3(1), 1976, pp. 1-22, the levels of the hierarchy and their complete problems
  8. Celia Wrathall, Complete sets and the polynomial-time hierarchy, Theoretical Computer Science 3(1), 1976, pp. 23-33, the completeness of quantified Boolean formulas with k alternations for the k-th level

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples