Chapter 3: Inductive Synthesis & Programming by Example (PBE)
Programming by Example (PBE) is the most direct form of synthesis: a user gives input-output pairs and the system infers a program. It is also the most proven in industry. Flash Fill, which shipped in Microsoft Excel 2013, lets a spreadsheet user type one or two cleaned-up values and fills the rest of the column with a synthesized string program. The user never sees the program; they see a column that is right.
This chapter moves from the constant-only language of Chapter 2 to grammars with variables and shows four ways to search them: whole-string primitives, version spaces, evolutionary search, and a version space algebra in the style of Flash Fill. The first three examples are small on purpose and each stops at a clear limit. The fourth removes those limits and is the longest program in this part of the book.
All code for this chapter is in source-code/03-inductive-synthesis-pbe/. string-reverse comes from source-code/utils/string.rkt. Each file keeps demos in (module+ main ...) and checks in (module+ test ...).
String Transformations with Whole-String Primitives
01-pbe-string-transform.rkt starts with the smallest useful DSL: four whole-string operations, plus an expression evaluator for concat, take, and drop that later examples build on.
1 (require "../utils/string.rkt")
2
3 (struct string-op (name func) #:transparent)
4 (define +operations+
5 (list (string-op "reverse" string-reverse)
6 (string-op "to-upper" string-upcase)
7 (string-op "to-lower" string-downcase)
8 (string-op "trim" string-trim)))
9
10 (define (eval-string-expr expr input)
11 (match expr
12 [(? string? s) s]
13 ['input input]
14 [`(concat ,args ...) (apply string-append (map (lambda (a) (eval-string-expr a input)) args))]
15 [`(take ,s ,n) (substring (eval-string-expr s input) 0 n)]
16 [`(drop ,s ,n) (substring (eval-string-expr s input) n)]
17 [_ (raise-argument-error 'eval-string-expr "string expr" expr)]))
18
19 (define (learn-transform examples)
20 (for/or ([op +operations+])
21 (and (for/and ([ex examples]) (equal? ((string-op-func op) (car ex)) (cadr ex))) op)))
learn-transform tries each primitive and returns the first one that maps every input to its output. On ("john smith" "smith, john") it returns #f, and the demo says why:
1 No single primitive fits name formatting.
2 This DSL has no split/concat on space; a richer DSL would
3 learn (concat (second (split input " ")) ", " (first ...)).
That failure is the point of the example. A synthesizer can only find programs its DSL can express, and a DSL of whole-string operations cannot pull a word out of a string. The last section of this chapter adds substrings and positions and learns this exact task.
Version Spaces
A version space is the set of every program in the DSL that fits the examples seen so far. Each new example can only shrink it. 02-version-space.rkt holds the set in a vs struct and shows two ways to shrink it: filter by one example at a time, or filter by each example on its own and intersect the results.
1 (require "../utils/string.rkt")
2 (struct vs (desc candidates) #:transparent)
3
4 (define (vs-update example old-vs)
5 (match-define (list input output) example)
6 (vs (vs-desc old-vs)
7 (filter (lambda (c) (equal? (apply-operation c input) output))
8 (vs-candidates old-vs))))
9
10 (define (vs-intersect vs1 vs2)
11 (vs (vs-desc vs1) (filter (lambda (c) (member c (vs-candidates vs2))) (vs-candidates vs1))))
The demo filters hello -> olleh down to (reverse) and HELLO -> hello down to (to-lower):
1 Initial candidates: 4
2 After 'hello' -> 'olleh': 1 candidates (reverse)
3 After 'HELLO' -> 'hello': 1 candidates (to-lower)
4 Intersection of the two filtered spaces: ()
5 No single primitive is both reverse and to-lower.
6 Sequential filter (hello->olleh then HELLO->hello): ()
Both routes give the same empty answer, and that is the key property: filtering in sequence and intersecting give the same result. Flash Fill depends on it, because it lets the system build one version space per example and combine them afterward.
This version stores the candidates as a plain list. With four candidates that costs nothing. With a billion it is not possible, and the Flash Fill section shows how to store a billion programs in a few hundred edges.
Stochastic and Evolutionary Search
03-genetic-programming.rkt leaves strings for arithmetic over a variable x, so that f(x) = 2x is learnable, and searches by evolution rather than by enumeration. Mutation swaps a random subtree for a fresh random one. Crossover grafts a subtree of one parent into the other.
1 (define (eval-expr expr env)
2 (match expr
3 [(? number? n) n]
4 [(? symbol? s) (hash-ref env s s)]
5 [`(+ ,a ,b) (+ (eval-expr a env) (eval-expr b env))]
6 [`(* ,a ,b) (* (eval-expr a env) (eval-expr b env))]
7 [`(- ,a ,b) (- (eval-expr a env) (eval-expr b env))]))
8
9 (define (mutate prog) (replace-random-subtree prog (random-program 1)))
10 (define (crossover p1 p2)
11 (if (and (list? p1) (list? p2))
12 (replace-random-subtree p1 (random-ref (subtrees p2)))
13 (random-ref (list p1 p2))))
14
15 (define (evolve population examples generations)
16 (let loop ([gen 0] [pop population])
17 (if (>= gen generations) (argmax (lambda (p) (fitness p examples)) pop)
18 (let* ([scored (sort pop > #:key (lambda (p) (fitness p examples)))]
19 [parents (take scored (max 2 (quotient (length scored) 2)))])
20 (loop (+ gen 1) (append parents (for/list ([_ parents]) (mutate (crossover (random-ref parents) (random-ref parents))))))))))
fitness evaluates a program with (hash 'x input) and scores it by negative error, so 0 is perfect. evolve keeps the top half of the population as parents and breeds the same number of children. A fixed (random-seed 3) at the top of the file makes the run repeat exactly:
1 Gen 0: Best fitness = 0 prog (- (* 3 x) (* 1 x))
2 Gen 1: Best fitness = 0 prog (- (* 3 x) (* 1 x))
3 ...
4 Best program after 5 generations: (- (* 3 x) (* 1 x))
5 Check: (0 2 4 6)
The winner, 3x - x, is already in the random population at generation 0, and elitist selection keeps it unchanged. That is worth a pause. With only 20 random depth-2 programs over {+, *, -} and leaves that include x, a correct program often turns up at once, and evolution’s job is to keep it rather than to find it. Evolution earns its cost on larger spaces, where no random sample is likely to contain the answer and partial credit from fitness guides the search.
Flash Fill in a Toy DSL
04-flash-fill-demo.rkt frames the task as Flash Fill does, with a column of examples, but keeps the whole-string DSL of the first example. It learns HELLO -> hello as lower, and it states that phone formatting (1234567890 -> 123-456-7890) and name swapping are out of reach:
1 Note: This toy DSL has only whole-string ops, so phone
2 formatting cannot be learned. Real FlashFill uses substring
3 extraction with regex and position logic.
The demo’s closing list names what is missing: substring extraction, positions, and pattern matching. The next section builds all three.
The Flash Fill Algorithm: A Version Space Algebra
Gulwani’s 2011 paper “Automating String Processing in Spreadsheets Using Input-Output Examples” describes the algorithm behind Flash Fill. It has three parts:
- A DSL in which a program joins a list of pieces, and each piece is either a constant string or a substring of the input that lies between two positions.
- A data structure that holds every program in that DSL consistent with an example, without listing them one by one.
- An intersection operation on that data structure, plus a ranking function that picks one program from what is left.
05-flash-fill-vsa.rkt builds a small version of each part.
The DSL
A program is (concat atom ...). An atom is (const "s") or (substr p1 p2). Positions say where a substring starts and ends:
(cpos k): a fixed offset.k >= 0counts from the start;k < 0counts from the end, so(cpos -1)is the end of the string.(before c n): the index of then-th occurrence of the characterc.(after c n): the index just past then-th occurrence ofc.
As with cpos, a negative n counts from the end, so (after #\space -1) means “just past the last space”. The separators are space, comma, hyphen, @, period, and slash.
1 (define (eval-pos p s)
2 (define len (string-length s))
3 (match p
4 [`(cpos ,k) (let ([i (if (>= k 0) k (+ len 1 k))]) (and (<= 0 i len) i))]
5 [`(before ,c ,n) (nth-occurrence s c n)]
6 [`(after ,c ,n) (let ([i (nth-occurrence s c n)]) (and i (+ i 1)))]))
The real Flash Fill DSL is richer in two ways. Its positions match regular-expression tokens (for example, “the start of the second run of digits”), and a top-level conditional picks between programs when the inputs come in different formats. The structure, though, is the same, and so is the algorithm.
One example as a DAG
Take the example "john smith" -> "smith, john". Number the gaps between the output’s characters 0 to 11. Every program in the DSL that prints this output splits it into pieces, so every program is a path from node 0 to node 11, and each step of the path prints one slice of the output.
That observation gives the data structure. Build a directed acyclic graph (DAG) with a node for each output position and an edge i -> j for every slice. On each edge, store every atom that prints that slice: the constant, plus one substr atom for each place the slice occurs in the input.
The second trick makes the structure small. A substr atom does not store one start position and one end position. It stores the set of every position expression that evaluates to the right index:
1 (define (positions-for s a)
2 (define len (string-length s))
3 (append
4 (list `(cpos ,a) `(cpos ,(- a len 1)))
5 (for*/list ([c separators]
6 [occ (in-value (occurrences s c))]
7 [i occ]
8 [k (in-value (length occ))]
9 [n (in-value (+ 1 (index-of occ i)))]
10 [p (list `(before ,c ,n) `(before ,c ,(- n k 1))
11 `(after ,c ,n) `(after ,c ,(- n k 1)))]
12 #:when (eqv? (eval-pos p s) a))
13 p)))
For index 5 of "john smith", positions-for returns (cpos 5), (cpos -6), (after #\space 1), and (after #\space -1). A substr atom with four start positions and two end positions stands for eight programs but costs six list entries. Across a whole DAG these products multiply, which is why the word “algebra” fits: the structure stores sums of products of sets, and the program count falls out by arithmetic.
1 (define (example-dag in out)
2 (define m (string-length out))
3 (define pos-cache (for/vector ([a (in-range (+ 1 (string-length in)))]) (positions-for in a)))
4 (dag 0 m
5 (for/hash ([i (in-range m)])
6 (values i
7 (for/list ([j (in-range (+ i 1) (+ m 1))])
8 (define piece (substring out i j))
9 (cons j
10 (cons `(const ,piece)
11 (for/list ([a (substring-starts in piece)])
12 `(substr ,(vector-ref pos-cache a)
13 ,(vector-ref pos-cache (+ a (- j i))))))))))))
count-programs counts paths by dynamic programming. Each edge contributes one program per constant and |P1| x |P2| per substr atom:
1 (define (count-programs d)
2 (define memo (make-hash))
3 (let count ([node (dag-start d)])
4 (cond [(equal? node (dag-end d)) 1]
5 [else
6 (hash-ref! memo node
7 (lambda ()
8 (for/sum ([e (hash-ref (dag-edges d) node '())])
9 (* (for/sum ([a (cdr e)]) (atom-count a))
10 (count (car e))))))])))
For the name example the answer is 1,211,394,080 programs, held in a DAG of 66 edges.

Intersection
A second example, "ada lovelace" -> "lovelace, ada", has its own DAG. The programs that fit both examples are the paths that exist in both DAGs at once. dag-intersect builds that set with a product construction: node (u . v) pairs a node from each DAG, and an edge (u . v) -> (u2 . v2) carries the pairwise meets of the atoms on u -> u2 and v -> v2.
1 (define (atom-meet a1 a2)
2 (match* (a1 a2)
3 [(`(const ,s1) `(const ,s2)) (and (string=? s1 s2) a1)]
4 [(`(substr ,p1 ,p2) `(substr ,q1 ,q2))
5 (define r1 (set-meet p1 q1))
6 (define r2 (set-meet p2 q2))
7 (and (pair? r1) (pair? r2) `(substr ,r1 ,r2))]
8 [(_ _) #f]))
Two constants meet only if they are the same string. Two substrings meet by intersecting their position sets. The meet drops every expression that worked for one example by accident. (cpos 5) finds the start of "smith" but not the start of "lovelace", so it vanishes; (after #\space 1) works for both, so it stays. dag-intersect only builds nodes it can reach from the start, so the product stays far smaller than the full cross product of the two DAGs.
The version space falls from 1.2 billion programs to 7,056, and every survivor is a general rule.
Ranking
Even 7,056 programs is too many to show a user, so Flash Fill ranks them and returns the best one. best-program finds the cheapest path through the DAG. A substring costs 3, and a constant costs 2 plus 2 per character, so the ranker explains output with input text whenever it can and keeps constants short. Within a position set, it prefers the start and end of the string, then separator positions, then fixed offsets.
1 (define (atom-cost a)
2 (match a
3 [`(const ,s) (+ 2 (* 2 (string-length s)))]
4 [`(substr ,_ ,_) 3]))
These weights are a choice, and the demo shows that the choice matters. Here is the run:
1 === Swap first and last name ===
2 After 1 example: 1211394080 programs in the version space
3 best: (concat (substr (after #\space 1) (cpos -1)) (const ", ") (substr (cpos 0) (before #\space 1)))
4 "alan turing" -> "turing, alan"
5 After 2 examples: 7056 programs in the version space
6 best: (concat (substr (after #\space 1) (cpos -1)) (const ", ") (substr (cpos 0) (before #\space 1)))
7 "alan turing" -> "turing, alan"
The ranker picks the general program from a single example, because it prefers separator positions. Swap in a ranker that prefers fixed offsets and the picture changes:
1 === Why the second example matters ===
2 One example, offset-loving ranker: (concat (substr (cpos 5) (cpos -1)) (const ", ") (substr (cpos 0) (cpos 4)))
3 on "grace hopper" -> " hopper, grac"
4 Two examples, offset-loving ranker: (concat (substr (after #\space 1) (cpos -1)) (const ", ") (substr (cpos 0) (before #\space 1)))
5 on "grace hopper" -> "hopper, grace"
With one example, the offset program is just as consistent as the separator program, and only the ranker’s taste picks between them. With two examples of different lengths, intersection removes the offset programs, and even the bad ranker has no wrong choice left. Ranking and intersection do the same job from two sides: ranking guesses what the user meant, and each new example cuts down how much guessing is needed.
The same code learns tasks that defeated the toy DSL:
1 === Format a phone number ===
2 After 1 example: 4978022400 programs in the version space
3 best: (concat (substr (cpos 0) (cpos 3)) (const "-") (substr (cpos 3) (cpos 6)) (const "-") (substr (cpos 6) (cpos -1)))
4 "8005550199" -> "800-555-0199"
5
6 === User name from an email address ===
7 After 1 example: 798964 programs in the version space
8 best: (concat (substr (after #\. 1) (before #\@ 1)))
9 "ada.lovelace@analytical.uk" -> "lovelace"
10 After 2 examples: 64 programs in the version space
The phone task shows the cost weights at work. A single constant "-123-" would also print the middle of the output, but it costs more than a substring between two hyphens, so the ranker reads the digits from the input and the program generalizes.
The last task finds the DSL’s edge:
1 === Initials (beyond this DSL) ===
2 After 1 example: 72 programs in the version space
3 best: (concat (const "g.h."))
4 "alan turing" -> "g.h."
5 After 2 examples: 0 programs in the version space
Initials need “the character just after the first space”, a position one past a separator. This DSL has no such position, so from one example the best it can do is a constant that does not generalize, and a second example empties the version space. An empty version space is useful output: it tells the user, or the DSL designer, that no program in the language fits. Flash Fill’s regex-token positions express “the first character of the second word” directly, which is one reason its DSL is larger than this one.
Why this is the right shape
The version space algebra gives PBE three properties that the earlier examples lacked:
- Scale. The DAG grows with the square of the output length, while the number of programs it holds grows exponentially. The system never enumerates programs.
- Exact answers to “what else fits?” The count tells you how ambiguous the examples still are. A system can use that to ask the user for the one example that would cut the space most.
- Composition by intersection. Each example builds its own DAG, so examples can arrive in any order, and adding one is a single product construction.
Polozov and Gulwani’s FlashMeta framework (now PROSE) generalizes this design: given any DSL with suitable inverse semantics for its operators, it derives the version space construction and intersection automatically.
Running the Examples
1 cd source-code/03-inductive-synthesis-pbe
2 racket 01-pbe-string-transform.rkt # whole-string DSL and its limit
3 racket 02-version-space.rkt # sequential filter vs intersect
4 racket 03-genetic-programming.rkt # subtree mutate/crossover, with x
5 racket 04-flash-fill-demo.rkt # Flash Fill framing, toy DSL
6 racket 05-flash-fill-vsa.rkt # version space algebra DAGs
7
8 # tests (no demo output)
9 raco test .
Wrap Up
PBE asks the user for the cheapest kind of specification there is, a few examples, and pays for it with ambiguity. This chapter showed the four tools that manage that trade. A DSL bounds what can be learned; the first and fourth examples found its edges. A version space holds every program that fits, and intersection shrinks it as examples arrive. Evolution trades completeness for reach in spaces too big to enumerate. And a version space algebra holds billions of consistent programs in a small DAG, so a ranker can pick the most plausible one and a second example can remove the rest.
The next chapter keeps the same eval-expr pattern but turns from examples to specifications: proof extraction, constraint solving, and CEGIS, which adds a verifier that finds the most useful next example on its own.