Problemcore
Program synthesis
Also called automatic programming.
Given a specification, such as a formula, examples or a type, find a program in a given language that meets it.
Program synthesis asks for a program that meets a specification. The specification may be a logical formula, a list of input-output examples, a type, a program with holes, or a temporal formula over infinite runs. This page states the general problem, its decidable and undecidable cases, and the main families of methods. The close pages each fix one kind of specification: programming by example uses examples, syntax-guided synthesis uses a formula and a grammar, and reactive synthesis builds systems that run forever against an environment.
In plain words
Normally a person writes a program and a tool checks it. Synthesis turns this around. The person says what the program must do, and the tool writes the program.
The description can take many forms. It can be a precise rule, a few examples of inputs and the right outputs, the type of the function, or a program with some parts left blank. The tool also needs a list of the building blocks it may use.
The search space is huge, because the number of possible programs grows very fast with their length. So tools limit the language, search the smallest programs first, and throw away programs that behave like ones they have already seen. In general no tool can always succeed, because synthesis can encode questions that no algorithm can answer. With a finite set of candidate programs and a decidable check, the problem is decidable.
Definition
Input.
- A program space : a set of programs, usually the terms of a context-free grammar, with a semantics that maps an input to an output or is undefined.
- A specification : a relation between inputs and allowed outputs. It is given by a formula , by a finite list of pairs, by a type, or in another finite form.
Output. A program such that for every input , is defined and holds. If no such program exists, the specification is unrealizable in , and an exact solver says so.
Optimal synthesis. With a cost , such as the number of nodes of the syntax tree, find a program of least cost among those that meet .
Example-based specification. When is a finite list of pairs , a program meets it if for each . Inputs outside the list are free. This is programming by example.
Reactive specification. For a system that reads an input letter and writes an output letter at each step, forever, and are infinite words, and the program must be causal: the output at step may depend only on the inputs up to step . This is reactive synthesis. It is not the same as satisfiability of the specification, because the program must meet for every input sequence that the environment chooses.
Variants
- Logical specification with a grammar (syntax-guided synthesis). A formula in a decidable theory, and a grammar for the body of .
- Examples (programming by example). Few examples, a domain-specific language, and a ranking function that picks one program from the many that fit.
- Sketches (program sketching). A program with holes for constants or expressions; the solver fills the holes.
- Deductive synthesis (deductive program synthesis). Prove that for each input an output exists, and read the program from the proof.
- Types (type-directed synthesis). The goal type, often with examples or refinement predicates, guides a search over well-typed terms only.
- Reactive systems (reactive synthesis). A temporal logic formula over input and output signals; the answer is a finite-state transducer.
- Repair (automated program repair). The specification is a test suite, and the search space is the set of small edits to a given program.
- Superoptimization (superoptimization). The specification is an existing program, and the cost is speed or length.
- Invariants (loop invariant synthesis). The "program" is a predicate that makes a proof go through.
Examples
The numbers below come from a short script that enumerates all programs of a grammar by size and runs them.
A grammar and its size. Take the grammar
and let the size of a program be the number of nodes of its syntax tree. The number of integer programs of sizes is . There are programs of size at most and of size at most . Each step of two in size multiplies the count by about 30, so enumeration is exponential in the size of the answer.
A logical specification. Let be over the integers. The smallest programs that meet it on all inputs in have size : and . Both meet on all integers. Without ite the specification is unrealizable in this grammar. Every program then denotes a polynomial with integer coefficients. If , then equals for every , so as polynomials, but . This is a contradiction.
Examples underspecify the intent. Give only the examples and . The smallest programs that fit are and , of size . They fit the examples but are not the maximum. Add . Now no program of size at most fits, and four programs of size fit: the two correct ones, and . The examples and remove the last two. So a synthesizer from examples needs more examples, or a ranking function that prefers some programs, or a question to the user.
Known algorithms
| method | idea | page |
|---|---|---|
| bottom-up enumeration | build programs by size from smaller ones, and keep one program per output vector on the examples | bottom-up enumerative synthesis |
| top-down deduction | split the examples of a goal into sub-goals for the arguments of an operator, and store all answers in a shared graph | version space algebra, string transformation synthesis |
| counterexample-guided search | a synthesizer for finitely many inputs and a verifier for all inputs, in a loop | CEGIS |
| constraint solving | encode the holes or the shape of a bounded program as variables, and solve with a SAT or SMT solver | program sketching |
| theorem proving | prove constructively, and extract the program from the proof | deductive program synthesis |
| type-guided search | build only well-typed terms, and split the examples over pattern matches and conditionals | type-directed synthesis |
| stochastic search | mutate programs and keep the better ones by a cost on the tests | genetic programming |
| games | turn a temporal specification into a game against the environment, and read a strategy | reactive synthesis |
| complete functional synthesis | for linear integer arithmetic, compile into a program that computes a whenever one exists (Kuncak et al. 2010) |
Bounds
The hypotheses matter. A semantic check of one candidate program is already undecidable for a Turing-complete language, by Rice's theorem. So the decidable cases restrict either the language or the specification.
Theorem 1 (undecidable in general). Realizability is undecidable even when every candidate is a numeral and each check of a candidate is decidable.
Proof. Fix a deterministic Turing machine . Let be the numerals , each read as a constant function. Let say " halts on the empty input within steps". For one candidate , the check runs for steps, so it is decidable. A numeral meets if and only if halts. So an algorithm for realizability would decide the halting problem.
The syntax-guided synthesis page proves the same for a small arithmetic grammar, from Hilbert's tenth problem.
Theorem 2 (finite program spaces). If is finite and the check " meets " is decidable for each , then realizability and optimal synthesis are decidable.
Proof. Check each program, and return a cheapest one that passes.
Theorem 3 (semi-decision). If can be enumerated in order of cost and the check is decidable, then enumeration finds a cheapest program whenever one exists, and runs forever on unrealizable specifications.
Proof. Each program of cost appears after finitely many programs, all of cost at most . The first program that passes has the least cost.
With finitely many examples and a language whose programs all terminate, the check is decidable, so Theorem 3 applies to programming by example. The cost is the problem: the counts in the examples above grow exponentially with the size.
Known decidable cases with special methods.
- Linear integer arithmetic: every specification in Presburger arithmetic can be compiled into a program that returns some with whenever one exists (Kuncak, Mayer, Piskac and Suter 2010). This follows from quantifier elimination for Presburger arithmetic.
- Church's problem for specifications in S1S, the monadic second-order logic of one successor, is decidable, and when a solution exists, a finite-state one exists (Büchi and Landweber 1969).
- Reactive synthesis from LTL is 2EXPTIME-complete (Pnueli-Rosner theorem).
- Sketches whose holes and inputs range over finite domains: decidable, by Theorem 2. CEGIS stops on finite candidate spaces.
Reductions
- Examples to logic. A list of examples is the formula with no quantifiers.
- Bounded synthesis to one exists-forall query. Encode a program of size at most by selector variables , and ask . Over Boolean inputs this is a 2QBF formula.
- Synthesis to proof search. A constructive proof of contains a program; see deductive program synthesis and the Curry-Howard correspondence.
- Reactive synthesis to games. An LTL specification becomes a parity game between the environment and the system.
- From the halting problem. As in Theorem 1.
Applications
- FlashFill in Microsoft Excel (2013) fills a column of strings from one or two examples (string transformation synthesis).
- Superoptimizers find the shortest instruction sequence equal to a given one.
- Verifiers synthesize loop invariants and ranking functions.
- Repair tools search for small edits that make the failing tests pass.
- Controllers for hardware and robots are synthesized from temporal specifications.
- Code completion in typed languages searches for terms of the expected type.
References
- Sumit Gulwani, Oleksandr Polozov and Rishabh Singh, Program Synthesis, Foundations and Trends in Programming Languages 4(1-2), 2017, chapter 1 (the three dimensions of a synthesizer, user intent, program space and search technique), chapters 4 to 6 (enumerative search, constraint solving and stochastic search) and chapter 7 (programming by examples)
- Alonzo Church, Applications of recursive arithmetic to the problem of circuit synthesis, Summaries of the Summer Institute of Symbolic Logic, vol. I, Cornell University, Ithaca, 1957, pp. 3-50, the first statement of the synthesis problem, for circuits that map input streams to output streams (Church's problem)
- J. Richard Büchi and Lawrence H. Landweber, Solving sequential conditions by finite-state strategies, Transactions of the American Mathematical Society 138, 1969, pp. 295-311, the solution of Church's problem for specifications in S1S, with finite-state strategies
- Zohar Manna and Richard Waldinger, A deductive approach to program synthesis, ACM Transactions on Programming Languages and Systems 2(1), 1980, pp. 90-121, synthesis as theorem proving, with the program read from the proof
- Amir Pnueli and Roni Rosner, On the synthesis of a reactive module, Principles of Programming Languages (POPL 1989), pp. 179-190, synthesis of reactive modules from LTL specifications in doubly exponential time
- Rajeev Alur, Rastislav Bodík, Garvit Juniwal, Milo M. K. Martin, Mukund Raghothaman, Sanjit A. Seshia, Rishabh Singh, Armando Solar-Lezama, Emina Torlak and Abhishek Udupa, Syntax-guided synthesis, Formal Methods in Computer-Aided Design (FMCAD 2013), pp. 1-8, a specification in a logical theory together with a grammar that limits the programs
- Viktor Kuncak, Mikaël Mayer, Ruzica Piskac and Philippe Suter, Complete functional synthesis, Programming Language Design and Implementation (PLDI 2010), pp. 316-329, synthesis procedures for specifications in linear arithmetic and sets, which produce a program whenever one exists
Where this leads
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag YH7B5E Edit this page History Report an error Markdown