# Program synthesis

In computer science, program synthesis is the task to automatically construct a program that satisfies a given high-level specification[1]. In contrast to other automatic programming techniques, the specifications are usually non-algorithmic statements of an appropriate logical calculus.[2] Often, program synthesis employs techniques from formal verification.

## Origin

Already during the Summer Institute of Symbolic Logic at Cornell University in 1957 Alonzo Church defined the problem to synthesize a circuit from mathematical requirements[3]. Even though the work only refers to circuits and not programs, the work is considered to be one of the earliest descriptions of program synthesis and some researchers refer to program synthesis as "Church's Problem". In the 1960s a similar idea, to build an "automatic programmer", was explored by researchers in artificial intelligence[citation needed].

Since then, various research communities considered the problem of program synthesis. Notable works include the automata-theoretic approach by Büchi and Landweber[4], and the works by Manna and Waldinger discussed below. Even the development of modern high-level programming languages can be understood as a form of program synthesis.

## Current Developments

The last decade has seen a surge of practical interest in the idea of program synthesis in the formal verification community and related fields. Armando Solar-Lezama showed that it is possible to encode program synthesis problems in Boolean logic and use algorithms for the Boolean satisfiability problem to automatically find programs[5]. In 2013, a unified framework for program synthesis problems was proposed[6] and since 2014 there is a yearly program synthesis competition, comparing the different algorithms for program synthesis in a competitive event (www.sygus.org). Still, the available algorithms are only able to synthesize small programs.

## The framework of Manna and Waldinger

The framework of Manna and Waldinger[7] starts from a user-given first-order specification formula. For that formula, a proof is constructed, thereby also synthesizing a functional program from unifying substitutions. Proof rules include non-clausal resolution, logical transformations, splitting of conjunctive assertions and of disjunctive goals, and structural induction. Murray has shown these rules to be complete for first-order logic.[8] In 1986, Manna and Waldinger added generalized E-resolution and paramodulation rules to handle also equality;[9] later, these rules turned out to be incomplete (but nevertheless sound).[10]

The framework has been designed to enhance human readability of intermediate formulas: contrary to classical resolution, it does not require clausal normal form, but allows one to reason with formulas of arbitrary structure and containing any junctors. Programs obtained by this approach are guaranteed to satisfy the specification formula started from; in this sense they are correct by construction. Only a minimalist, yet Turing-complete functional programming language, consisting of conditional, recursion, and arithmetic and other operators is supported.

As a toy example, a functional program to compute the maximum ${\displaystyle M}$ of two numbers ${\displaystyle x}$ and ${\displaystyle y}$ can be derived as follows.[citation needed]

Example synthesis of maximum function
Nr Assertions Goals Program Origin
1 ${\displaystyle A=A}$ Axiom
2 ${\displaystyle A\leq A}$ Axiom
3 ${\displaystyle A\leq B\lor B\leq A}$ Axiom
10 ${\displaystyle x\leq M\land y\leq M\land (x=M\lor y=M)}$ M Specification
11 ${\displaystyle (x\leq M\land y\leq M\land x=M)\lor (x\leq M\land y\leq M\land y=M)}$ M Distr(10)
12 ${\displaystyle x\leq M\land y\leq M\land x=M}$ M Split(11)
13 ${\displaystyle x\leq M\land y\leq M\land y=M}$ M Split(11)
14 ${\displaystyle x\leq x\land y\leq x}$ x Resolve(12,1)
15 ${\displaystyle y\leq x}$ x Resolve(14,2)
16 ${\displaystyle \lnot (x\leq y)}$ x Resolve(15,3)
17 ${\displaystyle x\leq y\land y\leq y}$ y Resolve(13,1)
18 ${\displaystyle x\leq y}$ y Resolve(17,2)
19 ${\displaystyle {\text{true}}}$ x<y ? y : x Resolve(18,16)

Starting from the requirement description "The maximum is larger or equal than any given number, and is one of the given numbers", the first-order formula ${\displaystyle \forall X\forall Y\exists M:X\leq M\land Y\leq M\land (X=M\lor Y=M)}$ is obtained as its formal translation. This formula is to be proved. By reverse Skolemization,[11] the specification in line 10 is obtained, an upper- and lower-case letter denoting a variable and a Skolem constant, respectively. After applying the distributive law in line 11, the proof goal is a disjunction, and hence can be split into two cases, viz. lines 12 and 13. Turning to the first case, resolving line 12 with the axiom in line 1 leads to instantiation of the program variable ${\displaystyle M}$ in line 14. Intuitively, the last conjunct of line 12 prescribes the value that ${\displaystyle M}$ must take in this case. Formally, the non-clausal resolution rule shown in line 57 below is applied to lines 12 and 1, with

• ${\displaystyle p}$ being the common instance ${\displaystyle x=x}$ of ${\displaystyle A=A}$ and ${\displaystyle x=M}$, obtained by syntactically unifying the latter formulas,
• ${\displaystyle F[}$${\displaystyle p}$${\displaystyle ]}$ being ${\displaystyle {\text{true}}\land }$${\displaystyle x=x}$, obtained from instantiated line 1 (appropriately padded to make the conext ${\displaystyle F[\cdot ]}$ around ${\displaystyle p}$ visible), and
• ${\displaystyle G[}$${\displaystyle p}$${\displaystyle ]}$ being ${\displaystyle x\leq x\land y\leq x\land }$${\displaystyle x=x}$, obtained from instantiated line 12,

yielding ${\displaystyle \lnot (}$${\displaystyle {\text{true}}\land }$${\displaystyle {\text{false}}}$${\displaystyle )\land (}$${\displaystyle x\leq x\land y\leq x\land }$${\displaystyle {\text{true}}}$${\displaystyle )}$, which simplifies to ${\displaystyle x\leq x\land y\leq x}$. In a similar way, line 14 yields line 15 and then line 16 by resolution. Also, the second case, ${\displaystyle x\leq M\land y\leq M\land y=M}$ in line 13, is handled similarly. In a last step, both cases (i.e. lines 16 and 18) are joined, using the resolution rule from line 58; to make that rule applicable, the preparatory step 15→16 was needed. Since both line 16 and 18 comes with a program term, a conditional expression results in the program column. Since the goal formula ${\displaystyle {\text{true}}}$ has been derived, the proof is done, and the program column of the "${\displaystyle {\text{true}}}$" line contains the program.

Non-clausal resolution rules (unifying substitutions not shown)
Nr Assertions Goals Program Origin
51 ${\displaystyle E[p]}$
52 ${\displaystyle F[p]}$
53 ${\displaystyle G[p]}$ s
54 ${\displaystyle H[p]}$ t
55 ${\displaystyle E[{\text{true}}]\lor F[{\text{false}}]}$ Resolve(51,52)
56 ${\displaystyle \lnot F[{\text{true}}]\land G[{\text{false}}]}$ s Resolve(52,53)
57 ${\displaystyle \lnot F[{\text{false}}]\land G[{\text{true}}]}$ s Resolve(53,52)
58 ${\displaystyle G[{\text{true}}]\land H[{\text{false}}]}$ p ? s : t Resolve(53,54)

## References

1. ^ Sumit Gulwani, Oleksandr Polozov, Rishabh Singh (Jul 2017). "Program Synthesis". Foundations and Trends® in Programming Languages. 4: 1–119. doi:10.1561/2500000010.
2. ^ Basin, D.; Deville, Y.; Flener, P.; Hamfelt, A.; Fischer Nilsson, J. (2004). "Synthesis of programs in computational logic". Program Development in Computational Logic. CiteSeerX .
3. ^ Alonzo Church (1957). "Applications of recursive arithmetic to the problem of circuit synthesis". Summaries of the Summer Institute of Symbolic Logic. 1: 3–50.
4. ^ Richard Büchi, Lawrence Landweber (Apr 1969). "Solving Sequential Conditions by Finite-State Strategies". Transactions of the American Mathematical Society. 138: 295–311. doi:10.2307/1994916.
5. ^ Solar-Lezama, Armando (2008). Program synthesis by sketching (Ph.D.). University of California, Berkeley.
6. ^ Alur, Rajeev; al., et (2013). "Syntax-guided Synthesis". Proceedings of Formal Methods in Computer-Aided Design. IEEE. p. 8.
7. ^ Zohar Manna, Richard Waldinger (Jan 1980). "A Deductive Approach to Program Synthesis". ACM Transactions on Programming Languages and Systems. 2: 90–121. doi:10.1145/357084.357090. A preprint appearead in Dec 1978 as SRI Technical Note 177
8. ^ Manna, Waldinger (1980), p.103, referring to: Neil V. Murray (Feb 1979). A Proof Procedure for Quantifier-Free Non-Clausal First Order Logic (Technical report). Syracuse Univ. 2-79.
9. ^ Zohar Manna, Richard Waldinger (Jan 1986). "Special Relations in Automated Deduction". Journal of the ACM: 1–59. doi:10.1145/4904.4905.
10. ^ Zohar Manna, Richard Waldinger (1992). "The Special-Relations Rules are Incomplete". Proc. CADE 11. LNCS. 607. Springer. pp. 492–506.
11. ^ While ordinary Skolemization preserves satisfiability, reverse Skolemization, i.e. replacing universally quantified variables by functions, preserves validity.
• Zohar Manna, Richard Waldinger (1975). "Knowledge and Reasoning in Program Synthesis". Artificial Intelligence. 6: 175–208. doi:10.1016/0004-3702(75)90008-9.
• Jonathan Traugott (1986). "Deductive Synthesis of Sorting Programs". Proceedings of the International Conference on Automated Deduction. LNCS. 230. Springer. pp. 641–660.