Title: SKETCH: Programming With Partial Programs
1SKETCH Programming With Partial Programs
Armando Solar-Lezama, Liviu Tancau, Rastislav
Bodik, Sanjit Seshia UC Berkeley Vijay
Saraswat IBM
Motivation
Example Population Count
- Programmers often face a difficult tradeoff
between simplicity and efficiency - Programmers would like to write clean and
readable code, but such code usually has poor
performance - On the other hand, efficient code is difficult to
write and maintain - With sketching, the programmer can write the
clean and readable version of the program, and
then provide only an outline of the efficient
implementation
1) Write a clean reference implementation For
this example we want to compute the number of
ones in an input word. For this task, its easy
to write a simple specification for it, but its
going to be very inefficient.
2) Think of a clever optimization trick We can
solve this in a divide-and-conquer fashion,
computing all the sums at each level in parallel.
3-4) Write a sketch using the sketching
constructs The implementation idea can be
described as a sketch. Each hole in the sketch
will be filled out with the correct code. The
implements keyword denotes functional equivalence
int popSketched (bitW x) implements pop
loop (??) x (x ??) ((x ??) ??)
return x
int pop (bitW x) int count 0 for
(int i 0 i return count
high
Optimal implementation
Sketch
5) Compiler solves for the values of the holes
6) The resolved sketch is compiled into C code
unsigned popSketched (unsigned x) x (x
0x5555) ((x 1) 0x5555) x (x 0x3333)
((x 2) 0x3333) x (x 0x0077) ((x
8) 0x0077) x (x 0x000F) ((x 4)
0x000F) return x
int popSketched (bitW x) implements pop
loop (4) x (x 0x5555, 0x3333, 0x0077,
0x000F ) ((x 1,2, 8, 4) 0x5555,
0x3333, 0x0077, 0x000F ) return x
Holes inside loops can have multiple values, a
different one for each iteration of the loop
sketch compiler
Efficiency
Trivial implementation
low
hard
easy
Development difficulty
Programming with the SKETCH language
Example Merge Sort
This sketch for Merge Sort shows that some holes
can be arbitrary expressions, rather than just
constants.
1) Write a clean reference implementation. 2)
Think of a clever optimization trick 3) Write a
sketch using ?? construct. 4) Indicate functional
equivalence. 5) Compiler tries to assign values
to ?? in order to satisfy the equivalence. 6) If
successful ? C code
intN mergeSort (intN input) if( N1 )
return input0 return merge(
mergeSort ( input0N/2 ),
mergeSort ( inputN/2 N/2 ) )
System Architecture
Solver Algorithm
- The sketch synthesis problem is an instance of
2QBF -
- Counter-example driven solver
- Reduces 2QBF to a series of SAT problems
- I
- x random()
- do
- I I ? x
- c synthesizeForSomeInputs(I)
- if c nil then exit(buggy sketch'')
- x verifyForAllInputs(c)
- while x ! nil
- return c
E
A
- synthesizeForSomeInputs(I)
- Find a set of controls c such that the sketch
produces the same output as the spec for all
inputs in I - c establishes partial equivalence
c in 0,1k . ? x in 0,1m . F(x)
S(x, c)
- verifyForAllInputs(c)
- Given a control c, find an input x for which the
sketch output differs from the spec output - x is a counterexample of equivalence