Algorithm Search⌘K

Techniqueadvanced

Deductive program synthesis

Also called Manna-Waldinger synthesis.

Extracts a program from a constructive proof that for every input there is an output meeting the specification.

Deductive program synthesis builds a program from a proof. The specification says that for every input xx with P(x)P(x) there is an output zz with Q(x,z)Q(x, z). A theorem prover proves this statement in a constructive way, and each step of the proof also builds a part of the output: the term that the proof gives for zz is the program. Case splits in the proof become conditionals, and proofs by induction become recursive calls. So the program meets its specification by construction. Green (1969) extracted answers from resolution proofs, and Manna and Waldinger (1971, 1980) gave the general method with the deductive tableau.

In plain words

To find a program, prove that the answer exists, and prove it in a way that says how to find it. Say you must prove that every pair of numbers has a larger-or-equal member. One proof goes: "if the first is at most the second, take the second; else take the first." That sentence is already a program.

The method writes such proofs with strict rules, so a machine can find them and read off the program. Splitting into cases gives an "if". Proving a claim for 0, and for n from the claim for n minus 1, gives a function that calls itself on n minus 1.

The program needs no testing against its specification, because the proof is a proof of exactly that. The hard part is to find the proof.

When it applies

Use it when the specification is a logical formula, not a set of examples, and when the background facts about the data types can be written as axioms. The output is a program with a proof of correctness. The method fits small programs on numbers, lists and trees, and the derivation of algorithms from formal definitions.

It needs a constructive proof. A proof by contradiction can show that some zz exists without naming it, and then there is no program. A case split on a property that no program can decide gives no executable conditional. So the rules may split cases only on primitive conditions, which the target language can compute, and the axioms may use in programs only primitive functions.

For specifications given by examples, use programming by example. For a known program shape with unknown details, use program sketching.

The template

Step 1: the specification. Write it as ∀x (P(x)→∃z Q(x,z))\forall x\, (P(x) \rightarrow \exists z\, Q(x, z)). Mark the function and predicate symbols that the target language can compute.

Step 2: the theory. Write axioms for the data types and for the primitive functions: for example the order of the integers, the equations for addition, and an induction principle for the natural numbers or for lists.

Step 3: prove with output terms. Every formula in the proof carries an output term, which may contain variables of the proof. The basic rules are:

  • Resolution. From clauses C∨LC \vee L and D∨¬L′D \vee \neg L', where LL and L′L' have a most general unifier σ\sigma, infer (C∨D)σ(C \vee D)\sigma. This is the rule of Robinson resolution. A proof shows that a set of clauses is contradictory by deriving the empty clause.
  • The answer literal. Green (1969) negates the goal ∃z Q(a,z)\exists z\, Q(a, z), which gives the clause ¬Q(a,z)\neg Q(a, z), and adds a literal: ¬Q(a,z)∨Ans(z)\neg Q(a, z) \vee \mathrm{Ans}(z). The input aa is a new constant, as in Skolemization. Resolution applies the unifiers to Ans too. When the derived clause is Ans(t)\mathrm{Ans}(t) alone, the term tt is an answer; see answer substitution.
  • Conditional outputs. If one clause states "pp holds, or t1t_1 is an answer" and another "pp fails, or t2t_2 is an answer", resolution on the primitive condition pp gives the answer if p then t2 else t1\mathbf{if}\ p\ \mathbf{then}\ t_2\ \mathbf{else}\ t_1. Manna and Waldinger (1980) build this into the rule, so the derived row carries the conditional term.
  • Induction. To prove the goal for xx, assume it for all yy smaller than xx in a well-founded relation. Each use of this hypothesis with an output f(y)f(y) becomes a recursive call of the program ff that is being built. Well-foundedness makes the recursion stop.

Step 4: read off the program. The output term of the final step is the body of the program, with the input as its parameter.

Step 5: correctness. The program meets the specification by soundness of the rules: each rule preserves the meaning "the output term is a correct zz for the inputs where the row holds". Termination follows from the well-founded relation of the induction.

Classic examples

The maximum by resolution with an answer literal. The theory has a primitive order Le\mathrm{Le} and a predicate Max(u,v,w)\mathrm{Max}(u, v, w), "w is a larger-or-equal member of u and v":

  1. Le(u,v)∨Le(v,u)\mathrm{Le}(u, v) \vee \mathrm{Le}(v, u)
  2. ¬Le(u,v)∨Max(u,v,v)\neg \mathrm{Le}(u, v) \vee \mathrm{Max}(u, v, v)
  3. ¬Le(v,u)∨Max(u,v,u)\neg \mathrm{Le}(v, u) \vee \mathrm{Max}(u, v, u)

The goal is ∃z Max(a,b,z)\exists z\, \mathrm{Max}(a, b, z) for input constants aa and bb. Negate it and add the answer literal:

  1. ¬Max(a,b,z)∨Ans(z)\neg \mathrm{Max}(a, b, z) \vee \mathrm{Ans}(z)

The proof:

  1. From 4 and 2 with {u↦a,v↦b,z↦b}\{u \mapsto a, v \mapsto b, z \mapsto b\}: ¬Le(a,b)∨Ans(b)\neg \mathrm{Le}(a, b) \vee \mathrm{Ans}(b).
  2. From 4 and 3 with {u↦a,v↦b,z↦a}\{u \mapsto a, v \mapsto b, z \mapsto a\}: ¬Le(b,a)∨Ans(a)\neg \mathrm{Le}(b, a) \vee \mathrm{Ans}(a).
  3. From 5 and 1 with {u↦a,v↦b}\{u \mapsto a, v \mapsto b\}: Le(b,a)∨Ans(b)\mathrm{Le}(b, a) \vee \mathrm{Ans}(b).
  4. From 7 and 6 on the primitive literal Le(b,a)\mathrm{Le}(b, a): the clause Ans(b)∨Ans(a)\mathrm{Ans}(b) \vee \mathrm{Ans}(a), which is the answer if Le(b,a) then a else b\mathbf{if}\ \mathrm{Le}(b, a)\ \mathbf{then}\ a\ \mathbf{else}\ b.

Clause 6 says "if Le(b,a)\mathrm{Le}(b, a), then aa is an answer", and clause 7 says "if not, then bb is". The program is max⁡(a,b)=if b≤a then a else b\max(a, b) = \mathbf{if}\ b \le a\ \mathbf{then}\ a\ \mathbf{else}\ b.

A query instead of a program. With the facts Parent(a,b)\mathrm{Parent}(a, b) and Parent(b,c)\mathrm{Parent}(b, c), the rule ¬Parent(x,y)∨¬Parent(y,z)∨Grand(x,z)\neg \mathrm{Parent}(x, y) \vee \neg \mathrm{Parent}(y, z) \vee \mathrm{Grand}(x, z) and the goal clause ¬Grand(a,w)∨Ans(w)\neg \mathrm{Grand}(a, w) \vee \mathrm{Ans}(w), three resolution steps give ¬Parent(a,y)∨¬Parent(y,w)∨Ans(w)\neg \mathrm{Parent}(a, y) \vee \neg \mathrm{Parent}(y, w) \vee \mathrm{Ans}(w), then ¬Parent(b,w)∨Ans(w)\neg \mathrm{Parent}(b, w) \vee \mathrm{Ans}(w), then Ans(c)\mathrm{Ans}(c). The answer is a constant, because the goal has no input. Logic programming answers queries in this way.

Recursion from induction. Specification: for every natural number nn there is zz with 2z=n(n+1)2z = n(n + 1). Prove it by induction on nn.

  • Base. For n=0n = 0, take z=0z = 0, since 2⋅0=0⋅12 \cdot 0 = 0 \cdot 1.
  • Step. For n>0n > 0, the hypothesis for n−1n - 1 gives z′z' with 2z′=(n−1)n2z' = (n - 1)n, with the output f(n−1)f(n - 1). Take z=z′+nz = z' + n: then 2z=(n−1)n+2n=n(n+1)2z = (n - 1)n + 2n = n(n + 1).

The case split on n=0n = 0 is primitive, so it becomes a conditional, and the use of the hypothesis becomes a call:

f(n)=if n=0 then 0 else f(n−1)+n.f(n) = \mathbf{if}\ n = 0\ \mathbf{then}\ 0\ \mathbf{else}\ f(n - 1) + n.

A different proof gives a different program. A proof that uses the axiom "n(n+1)n(n + 1) is even" and a primitive halving function gives f(n)=n(n+1)/2f(n) = n(n + 1)/2 with no recursion.

Variations

  • The deductive tableau. Manna and Waldinger (1980) write the proof as a table of assertions and goals, each with an output column. Rules for resolution, for equality, and for induction act on rows and combine their outputs.
  • Proofs as programs in type theory. In a constructive type theory a proof of ∀x ∃z Q(x,z)\forall x\, \exists z\, Q(x, z) is itself a function that returns zz together with a proof of Q(x,z)Q(x, z). Bates and Constable (1985) extracted programs in this way from Nuprl proofs, and proof assistants such as Coq extract programs from proofs today. This is the Curry-Howard correspondence used for synthesis, often with dependent types.
  • Transformational synthesis. Burstall and Darlington (1977) start from a clear but slow set of equations and rewrite them with the rules fold and unfold into a fast recursive program. Each rule keeps the meaning, so the result is correct. The refinement calculus is a related method for imperative programs.
  • Synthesis with a solver. Modern tools often mix the two worlds: a proof rule splits the goal, and a solver or a search fills the small parts.

Pitfalls

  • Classical proofs give no program. "Either the program halts or it does not, and in each case the claim holds" is a valid classical proof, but the case split cannot be computed. Restrict case splits to primitive conditions.
  • Non-primitive symbols in the answer. If the answer term uses a function that the axioms only describe, such as "the zz with Q(x,z)Q(x, z)", the program cannot run. Only primitive symbols may appear in output terms.
  • The proof decides the program. Two proofs of the same theorem can give programs of very different cost. The prover has no notion of efficiency unless the method adds one.
  • The search is hard. Proving the existence theorem is as hard as theorem proving in general, which is undecidable for first-order logic. Practical systems need guidance by the user or strong heuristics for induction.
  • The specification must be complete. A program that meets a weak specification is correct only in that weak sense. A sort that only asks "the output is ordered" can return the empty list.
  • Induction needs a well-founded order. An induction on an order with infinite descending chains gives a recursion that may not stop.

Exercises

  1. In the maximum example, resolve 5 with 1 first on the other literal: unify Le(a,b)\mathrm{Le}(a, b) with Le(v,u)\mathrm{Le}(v, u). What clause do you get, and how does the proof finish? Answer. The unifier {v↦a,u↦b}\{v \mapsto a, u \mapsto b\} gives Le(b,a)∨Ans(b)\mathrm{Le}(b, a) \vee \mathrm{Ans}(b) again, since clause 1 is symmetric, and step 8 is the same.
  2. Derive a recursive program for ∃z z=2n\exists z\, z = 2^n from the axioms 20=12^0 = 1 and 2n=2⋅2n−12^{n} = 2 \cdot 2^{n-1} for n>0n > 0. Answer. Induction on nn gives f(n)=if n=0 then 1 else 2⋅f(n−1)f(n) = \mathbf{if}\ n = 0\ \mathbf{then}\ 1\ \mathbf{else}\ 2 \cdot f(n - 1).
  3. Why is the goal clause ¬Max(a,b,z)∨Ans(z)\neg \mathrm{Max}(a, b, z) \vee \mathrm{Ans}(z) and not ¬Max(x,y,z)∨Ans(z)\neg \mathrm{Max}(x, y, z) \vee \mathrm{Ans}(z) with variables xx and yy? Answer. The goal is ∀x ∀y ∃z Max(x,y,z)\forall x\, \forall y\, \exists z\, \mathrm{Max}(x, y, z). Its negation is ∃x ∃y ∀z ¬Max(x,y,z)\exists x\, \exists y\, \forall z\, \neg \mathrm{Max}(x, y, z), and Skolemization replaces xx and yy by constants. With variables, resolution could pick special inputs, and the answer would hold only for them.
  4. A proof of ∀n ∃z z2≤n<(z+1)2\forall n\, \exists z\, z^2 \le n < (z + 1)^2 by induction on nn with the step "if (z′+1)2≤n(z' + 1)^2 \le n then z=z′+1z = z' + 1 else z=z′z = z'" gives which program? Answer. f(n)=if n=0 then 0 else (if (f(n−1)+1)2≤n then f(n−1)+1 else f(n−1))f(n) = \mathbf{if}\ n = 0\ \mathbf{then}\ 0\ \mathbf{else}\ (\mathbf{if}\ (f(n - 1) + 1)^2 \le n\ \mathbf{then}\ f(n - 1) + 1\ \mathbf{else}\ f(n - 1)), an integer square root in O(n)O(n) steps.

References

  1. Cordell Green, Application of theorem proving to problem solving, Proceedings of the International Joint Conference on Artificial Intelligence (IJCAI 1969), Washington, D.C., pp. 219-239, the answer literal in resolution proofs, and the extraction of programs with conditionals and recursion from proofs
  2. Zohar Manna and Richard Waldinger, Toward automatic program synthesis, Communications of the ACM 14(3), 1971, pp. 151-165, conditionals from case analysis and recursion from induction in the proof of an existence theorem
  3. Zohar Manna and Richard Waldinger, A deductive approach to program synthesis, ACM Transactions on Programming Languages and Systems 2(1), 1980, pp. 90-121, the deductive tableau with output columns, the resolution rule with conditional outputs, and the induction rule that yields recursive calls
  4. Joseph L. Bates and Robert L. Constable, Proofs as programs, ACM Transactions on Programming Languages and Systems 7(1), 1985, pp. 113-136, the extraction of programs from constructive proofs in the Nuprl type theory
  5. Rod M. Burstall and John Darlington, A transformation system for developing recursive programs, Journal of the ACM 24(1), 1977, pp. 44-67, the fold and unfold rules for deriving programs from equations
  6. Sumit Gulwani, Oleksandr Polozov and Rishabh Singh, Program Synthesis, Foundations and Trends in Programming Languages 4(1-2), 2017, the overview of deductive synthesis in the history of the field

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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises