Problemcore
Programming by example
Also called PBE, inductive program synthesis.
Find a program in a domain-specific language that is consistent with a few given input-output examples and generalizes well.
Programming by example (PBE) is program synthesis where the specification is a short list of inputs together with the wanted outputs. The user does not write a formula, so the method suits people who do not program. The hard part is not to find a program that fits the examples, but to find the one that the user means. This page states the problem, why it is underspecified, what is decidable, and the main families of methods. The algorithms have their own pages: bottom-up enumerative synthesis, version space algebras, string transformation synthesis and type-directed synthesis.
In plain words
You show the tool a few cases. For example, "Alan Turing" becomes "A. Turing". The tool must find a small program that does the same to every row in the column.
Many programs agree with one or two examples. One program takes the first letter, adds a dot and a space, and copies the last name. Another program always writes the text "A. " and then copies the last name. Both give the right answer on the example, but only the first one is right on "Grace Hopper".
So a tool needs two things. It needs a fast way to find the programs that fit the examples, and it needs a rule to choose among them. Good tools prefer short and general programs, show the result on the other rows, and ask the user about rows where the candidates disagree.
Definition
Input.
- A domain-specific language : a grammar of programs and a semantics .
- A finite list of examples with and .
- A ranking function , for example minus the size of the program.
Output. A program that is consistent with , that is for , with the highest rank among the consistent programs. If no program of is consistent, the answer is "none".
The set of all consistent programs is the version space of in :
Adding an example can only make the version space smaller: . Version space algebras store in a compact form and compute this intersection.
The definition asks for consistency, but the real goal is generalization: the program should also give the intended output on inputs that the user did not show. Generalization cannot be checked from the examples alone. This is the same issue as overfitting in machine learning.
Variants
- Input-output examples. The basic form above.
- Programming by demonstration. The user shows the steps, not only the result, for example the edits in a text editor. Each step is an example for one part of the program (Lau, Wolfman, Domingos and Weld 2003).
- Interactive PBE. The tool finds an input on which two consistent programs disagree, a distinguishing input, and asks the user for the right output.
- Negative examples. Some pairs say that the output on must not be .
- Recursive programs from examples. Summers (1977) built recursive LISP functions from examples. Escher (Albarghouthi, Gulwani and Kincaid 2013) and Myth (Osera and Zdancewic 2015) need examples for the recursive calls as well, so that a call can be evaluated from the example table.
- Noisy examples. Some examples may be wrong, and the goal is a program that fits most of them.
- The PBE track of SyGuS. A syntax-guided synthesis problem whose specification is a conjunction of examples.
Examples
Strings. Take a language with constant strings, substrings between positions, and concatenation (see string transformation synthesis). From the one example "Alan Turing" "A. Turing", three consistent programs are:
| program | on "Grace Hopper" |
|---|---|
| first letter, then ". ", then the text after the space | "G. Hopper" |
| the constant "A. ", then the text after the space | "A. Hopper" |
| the constant "A. Turing" | "A. Turing" |
A rank that counts constant strings as expensive picks the first program. A second example, "Grace Hopper" "G. Hopper", removes the other two from the version space.
Integers. Over the grammar of the program synthesis page, with , , , , , , and ite on , a script that enumerates programs by size gives these results:
| examples | smallest consistent programs |
|---|---|
| , | and , size 3 |
| add | four programs of size 6: , , , |
| add and | only the two programs for the maximum |
The input is a distinguishing input for and the maximum. An interactive tool would ask the user for it.
Known algorithms
| method | idea | notes |
|---|---|---|
| bottom-up enumeration with observational equivalence | build programs by size, keep one per vector of outputs on the example inputs | finds a smallest program; cost exponential in its size |
| top-down deduction with version space algebras | invert each operator: from the output of a node, compute the outputs its arguments must have | FlashFill (Gulwani 2011), FlashMeta witness functions (Polozov and Gulwani 2015); returns all consistent programs at once |
| string transformation synthesis | a graph over output positions, with all atoms that produce each piece | the FlashFill algorithm |
| type-directed synthesis | refine the examples through pattern matching and recursion on well-typed terms | Myth (Osera and Zdancewic 2015) |
| deduction with combinators | split list examples over map, filter and fold, and enumerate the small bodies | lambda-squared (Feser, Chaudhuri and Dillig 2015) |
| recursive synthesis with goal graphs | bottom-up search, with a conditional that joins programs correct on parts of the examples | Escher (Albarghouthi, Gulwani and Kincaid 2013) |
| constraint solving | encode a bounded program and the examples as one formula | program sketching |
| stochastic search | mutate programs, with a fitness that counts correct examples | genetic programming |
Ranking. The usual rule is Occam's razor: prefer the shortest program, or prefer programs with few constants. Singh and Gulwani (2015) learn the ranking function from a set of tasks with known intended programs.
Bounds
Theorem 1 (a rich language makes consistency trivial). If has equality tests on inputs, conditionals and a constant for each output, and the example inputs are distinct, then a consistent program always exists: the table "if then else if then ..." of size times the size of the constants.
So for a general-purpose language, the question is never whether a program exists, but which one generalizes. This is why PBE uses small domain-specific languages and ranking.
Theorem 2 (undecidable for grammars). Consistency with a single example is undecidable for languages given by a context-free grammar, even when every program terminates.
Proof. Reduce Hilbert's tenth problem over the natural numbers (Hilbert's tenth problem). Given a polynomial with integer coefficients, take the grammar
where is the fixed text of the polynomial. Each program picks numerals , ignores its input, terminates, and outputs . Every tuple of natural numbers arises. With the one example , a consistent program exists if and only if has a root in .
Theorem 3 (decidable cases). If every program of terminates on the example inputs, the set of consistent programs is recursively enumerable, and enumeration by size finds a smallest consistent program when one exists. If is finite, or if only programs up to a size bound are allowed, the problem is decidable.
Proof. Each check runs a program on inputs and terminates. Enumerate the programs by size and check each one. With a finite space the enumeration ends.
Cost. The number of programs grows exponentially with the size. In the integer grammar above there are 1460 programs of size at most 6 and 2,247,860 of size at most 10. Observational equivalence and version space algebras reduce the work, but the worst case stays exponential in the size of the answer.
Reductions
- PBE to syntax-guided synthesis. The examples form a formula ; see syntax-guided synthesis.
- PBE to version space intersection. Build for each example and intersect them.
- Hilbert's tenth problem to PBE. As in Theorem 2.
- Concept learning to PBE. A boolean program learned from labelled examples is a classifier, and the version space is Mitchell's version space of consistent hypotheses (see version space algebra).
Applications
- FlashFill in Microsoft Excel (2013) completes a column of strings from examples.
- Text editors that repeat an edit shown once (SMARTedit, Lau et al. 2003).
- Data wrangling: extract fields from logs or web pages, and reshape tables, from a few marked examples.
- Teaching tools that build small recursive functions on lists from examples.
- Spreadsheet and shell commands that the user describes by the result they want.
References
- Sumit Gulwani, Automating string processing in spreadsheets using input-output examples, Principles of Programming Languages (POPL 2011), pp. 317-330, the FlashFill language, synthesis with version space algebras, ranking, and the interaction with the user
- Oleksandr Polozov and Sumit Gulwani, FlashMeta, a framework for inductive program synthesis, Object-Oriented Programming, Systems, Languages and Applications (OOPSLA 2015), pp. 107-126, top-down synthesis with witness functions that split the examples of an operator into examples for its arguments
- Tessa Lau, Steven A. Wolfman, Pedro Domingos and Daniel S. Weld, Programming by demonstration using version space algebra, Machine Learning 53(1-2), 2003, pp. 111-156, programming by demonstration in a text editor (SMARTedit), with version spaces composed by union and join
- Phillip D. Summers, A methodology for LISP program construction from examples, Journal of the ACM 24(1), 1977, pp. 161-175, recursive LISP programs built from a few input-output examples by finding recurrences in traces
- Rishabh Singh and Sumit Gulwani, Predicting a correct program in programming by example, Computer Aided Verification (CAV 2015), pp. 398-414, a ranking function learned from data to pick the intended program among those consistent with the examples
- Sumit Gulwani, Oleksandr Polozov and Rishabh Singh, Program Synthesis, Foundations and Trends in Programming Languages 4(1-2), 2017, chapter 7 (programming by examples, including ranking and interaction) and chapter 4 (enumerative search)
- John K. Feser, Swarat Chaudhuri and Isil Dillig, Synthesizing data structure transformations from input-output examples, Programming Language Design and Implementation (PLDI 2015), pp. 229-239, the lambda-squared tool, which combines deduction over higher-order combinators with enumeration of small terms
- Aws Albarghouthi, Sumit Gulwani and Zachary Kincaid, Recursive program synthesis, Computer Aided Verification (CAV 2013), pp. 934-950, the Escher tool, which synthesizes recursive programs from examples by bottom-up search with goal graphs
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 H27VZ7 Edit this page History Report an error Markdown