TL;DR
Program synthesis is the task of building a program automatically from a description of what it should do. The description can be a few input-output examples, a formal specification or a request in plain language. The system searches a space of candidate programs and returns one that passes a check against the description. It matters because writing correct code by hand is slow, and a search that is checked against the same tests the user cares about can produce code that is correct by construction for those tests.
Builds on: Abstract Syntax Tree, Tree Search, Language Model · Leads to: Code as Policies
Where it's used
Spreadsheet formulas. Flash Fill, a feature of Microsoft Excel from 2013, watches a few cells the user has filled in by hand. Typing "Lovelace, A." next to "Ada Lovelace" is enough for it to fill the rest of the column with the same pattern. The program behind the fill is synthesised from those examples, and it is only accepted if it reproduces every example the user gave.
Code assistants and programming contests. A large language model proposes many candidate functions for a task, and a filter runs each one against example tests and keeps the ones that pass. DeepMind's AlphaCode (2022) used this generate-then-filter pattern on competitive programming problems. The Codex paper (2021) measured a model trained on public code by running its generated functions against unit tests, not by reading them.
Robot control. In Code as Policies (2022), a language model is prompted with a few examples and writes short Python programs that call perception and motion functions. A spoken command such as "stack the blocks" becomes a program that the robot then runs. The synthesised program is the robot's policy, and it can be read, edited and re-run.
How it works
Take a tiny task. The specification is two examples: input 1 must give output 2, and input 3 must give output 6. The search space is every expression built from the variable , the constants 1 and 2, and the operators and .
- Write the specification. The examples are the spec: and . Check: a candidate is correct only if it matches both pairs.
- Define the space of programs. A program is a leaf (, 1 or 2) or an operator with two sub-programs. Check: and are in the space. is not, because 5 is not a leaf.
- Enumerate by size. The size of a program is its number of nodes. List all programs of size 1, then size 3, then size 5, and so on. Check: the size-1 programs are , and .
- Run each candidate on the examples. At input 1, the leaf gives 1, not 2. The leaf 1 gives 1. The leaf 2 gives 2 at input 1, but gives 2 at input 3, not 6. All three fail. Check: nothing of size 1 passes, so the search moves on.
- Keep the first match. At size 3, gives 2 at input 1 and 6 at input 3, so it passes both examples and the search stops. Check: also passes both examples. It is the same function as (both give everywhere), so the choice between them does not change the behaviour, but the examples alone cannot tell the two programs apart from the function they compute.
flowchart TD
S[Examples as the spec] --> E[Enumerate programs of size n]
E --> T{Does a program match every example?}
T -- yes --> R[Return it]
T -- no --> N[Try the next program]
N --> E2{Any programs left at size n?}
E2 -- yes --> E
E2 -- no --> G[Grow to size n plus 2]
G --> E
The loop is a Tree Search in which each program is a node. The search is only feasible because the programs are small and the test is cheap.
The math
Step 1 in symbols: the specification as a test
Define the check to be 1 when for all , and 0 otherwise. A program satisfies the specification exactly when . Equivalently, is the product over the examples of the indicator that each example matches:
Step 2 in symbols: the grammar
Take the grammar . Every binary operator adds one node for itself, so a program with an operator at the root and two children has size , where and are the sizes of the children. The sizes of programs are therefore .
Step 3 in symbols: enumeration by size
Let be the set of programs of size . Then , which has 3 elements. For size 3, a program is an operator applied to two leaves, so , from 2 operators, 3 choices for the left leaf and 3 for the right.
Step 4 and 5 in symbols: the search rule
The search returns the first program in the enumeration order that satisfies the check:
Evaluating can stop at the first failed example, because one mismatch already makes the product 0. The number of programs of size grows roughly exponentially in , so this search is practical only for small programs. Pruning, which discards whole groups of programs that cannot match, is what makes larger searches possible.
Worked examples
Specification: and .
- Size 1, leaf : , which is not 2. Reject.
- Size 1, leaf : , which is not 2. Reject.
- Size 1, leaf : and . Both match.
- The search returns the program .
- Interpretation: the search tested three programs, and the answer is a constant. The examples only ask for a constant, so the smallest program is enough.
Specification: , and .
- Size 1: gives , not 4. Reject. The leaves and give constants, which fail at input 1 or input 2. Reject.
- Size 3: the search tests the 18 programs in turn. The sums such as give , which is not 1. Reject.
- Continue through the products and sums. For example gives . Reject.
- The program gives , and . All three match.
- Interpretation: the search found the square with 3 nodes, and it is the only program of size 3 with these outputs. The check did all the work, and nothing in the search needed to know that the answer is a square.
Real string tools use a richer space, with operators such as "take the last word" and "take the first letter". Here is one such program.
Specification: "Ada Lovelace" becomes "Lovelace, A." and "Alan Turing" becomes "Turing, A.".
- Split the input on spaces. "Ada Lovelace" gives the words "Ada" and "Lovelace".
- Take the last word, "Lovelace".
- Take the first letter of the first word, "A".
- Join the pieces in order: the last word, then ", ", then the letter, then ".". The result is "Lovelace, A.".
- Check the second example in the same way. "Alan Turing" gives "Turing" and "A", so the output is "Turing, A.", which matches.
- Interpretation: the program is correct on both examples and reads like the rule a person would state. It also generalises to other names with the same shape.
Try it: apply the same program to "Grace Hopper". Answer: "Hopper, G.". The last word is "Hopper", the first letter of "Grace" is "G", and the joined result is "Hopper, G.".
Common mistakes and tradeoffs
It is tempting to think that a program which matches the examples is correct. In fact the examples only test the program on the inputs given. A program can fit and and still be wrong at , for instance a program that returns on those two inputs and something else elsewhere. Good synthesis keeps a separate set of held-out tests, or asks for more examples when several programs fit with different behaviour.
It is tempting to think that a large language model writes correct code, so no search is needed. In fact the model is a source of candidate programs. Its output is only trusted after a check, which is why the code-assistant pipeline runs tests on every candidate. The check is what turns a plausible guess into a verified answer.
It is tempting to think that a bigger search space always gives better programs. In fact the number of programs grows exponentially with size, so a larger grammar makes the search slower and more likely to return a long, odd program that fits the examples by accident.
When to use it. Use program synthesis when the specification can be checked cheaply, such as examples, tests or a formal property, and when the program is short enough for the search to reach. Enumerative search is exact and easy to explain. Genetic programming trades that exactness for a different way of exploring the space. Neural models trained on code, and In-Context Learning from a few examples in the prompt, handle vague descriptions well but need sampling and filtering to be reliable.
When not to use it. When the specification is hard to write down, synthesis has no check to rely on. Then the cost of finding a program is not the bottleneck, and the cost of agreeing on what the program should do is.
Alternatives. Formal Verification proves that a given program meets a property, rather than searching for one. Type checking catches a class of errors without running the program.
History
In 1971, Zohar Manna and Richard Waldinger proposed deriving a program from a proof that its specification can be met, one of the first serious attempts at automatic programming. In the mid-1970s, Phillip Summers showed that small LISP programs could be built from examples of their input and output. The field was slow for decades, because the search space grew faster than the computers of the time could cover.
In 2011, Sumit Gulwani published a method for synthesising string-transforming programs from examples, which became the basis of Flash Fill in Excel. It worked because the search space was small and designed around the operations people actually use on text. In 2013, Alur and colleagues proposed syntax-guided synthesis, which states the grammar and the correctness condition explicitly, so that a solver can be reused across problems.
Large language models changed the picture from 2021. Codex was measured by running generated functions against unit tests, and AlphaCode (2022) generated many candidate solutions and filtered them with example tests. Code as Policies (2022) applied the same idea to robot control, where the generated program is run on the robot.
Recap
- A program synthesis problem has three parts: a space of programs defined by a grammar, a specification such as examples, and a check that the program meets it.
- The simplest search enumerates programs in order of size and returns the first one that passes the check, as a Tree Search over programs.
- The examples do not force a unique program, and the number of programs grows exponentially, so practical systems prune the space and add held-out tests.
- Language models make the candidate generation broad and vague descriptions possible, but the check still decides what is correct.
Read next: Code as Policies applies synthesis to a robot, and Symbolic Regression uses the same search to find equations from data.