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 with there is an output with . 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 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 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 . 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 and , where and have a most general unifier , infer . 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 , which gives the clause , and adds a literal: . The input is a new constant, as in Skolemization. Resolution applies the unifiers to Ans too. When the derived clause is alone, the term is an answer; see answer substitution.
- Conditional outputs. If one clause states " holds, or is an answer" and another " fails, or is an answer", resolution on the primitive condition gives the answer . Manna and Waldinger (1980) build this into the rule, so the derived row carries the conditional term.
- Induction. To prove the goal for , assume it for all smaller than in a well-founded relation. Each use of this hypothesis with an output becomes a recursive call of the program 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 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 and a predicate , "w is a larger-or-equal member of u and v":
The goal is for input constants and . Negate it and add the answer literal:
The proof:
- From 4 and 2 with : .
- From 4 and 3 with : .
- From 5 and 1 with : .
- From 7 and 6 on the primitive literal : the clause , which is the answer .
Clause 6 says "if , then is an answer", and clause 7 says "if not, then is". The program is .
A query instead of a program. With the facts and , the rule and the goal clause , three resolution steps give , then , then . 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 there is with . Prove it by induction on .
- Base. For , take , since .
- Step. For , the hypothesis for gives with , with the output . Take : then .
The case split on is primitive, so it becomes a conditional, and the use of the hypothesis becomes a call:
A different proof gives a different program. A proof that uses the axiom " is even" and a primitive halving function gives 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 is itself a function that returns together with a proof of . 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 with ", 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
- In the maximum example, resolve 5 with 1 first on the other literal: unify with . What clause do you get, and how does the proof finish? Answer. The unifier gives again, since clause 1 is symmetric, and step 8 is the same.
- Derive a recursive program for from the axioms and for . Answer. Induction on gives .
- Why is the goal clause and not with variables and ? Answer. The goal is . Its negation is , and Skolemization replaces and by constants. With variables, resolution could pick special inputs, and the answer would hold only for them.
- A proof of by induction on with the step "if then else " gives which program? Answer. , an integer square root in steps.
References
- 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
- 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
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag EYDYMW Edit this page History Report an error Markdown