% synthesis
# problem
Find a program: for every input it produces the same output as
the spec (reference implementation) does
∃prog, ∀in, prog(in) = spec(in)
# finite programs
prog is composed of small snippets,
connected using a set of boolean, control variables
spec: x * 16
prog: if (b) then (x << c) else (x >> c)
problem: ∃b, ∀x, prog(x) = spec(x)
# solving
```python
from z3 import *
x = BitVec("x", 32)
c = BitVec("c", 32)
b = Bool("b")
e = If(b, x << c, x >> c)
solve(Exists([b, c], ForAll([x], x * 16 == e))
```
output: (x << 4)
```
[c!0 = 4, b!1 = True]
```
Z3 works for simple examples;
in general you need to do more: e.g.,
counter-example driven synthesis, two-player game
take 507 if you want to learn more
# demos
[sketch](https://bitbucket.org/gatoatigrado/sketch-frontend/wiki/Home)
(and [web interface](http://sketch1.csail.mit.edu/demo/pages/sketchexp)):
[non-overflow average](sk/avg.sk): result from Hacker's Delight
[timing-safe string comparison](sk/cmp.sk)