��Program Synthesis�A Tutorial
Yingfei Xiong
Peking University
ISSTA Summer School 2019
Can grandmas program?
2
Level of
Abstraction
What is the next?
Haskell (1990), Prolog (1972)
Java
C
Assembly
Why cannot?
3
Program
Compile
Executable
Program Synthesis saves grandmas
4
Specification
Synthesize
Program
“One of the most central problems in the theory of programming.”
----Amir Pneuli
Turing Award Recipient
“The fundamental way to improve software productivity.”
----Jiafu Xu
Founder of Software Research in China
History of Program Synthesis
5
1957
Before 2000
After 2000
Application – Data Wrangling
6
Application – Superoptimization
7
i=round(i);
a = 6755399441055744.0;
i=(i+a)-a;
Application – Reducing Duplicated Programming
8
Application – Program Repair
9
Synthesize an expression to replace the buggy one
Application – Testing
10
Synthesize a unit test to cover a path
Application – Analysis
11
Synthesize a strategy for a class of problems
SMT Solver
Apply Tactic 1
If formula is long
Apply Tactic 2
Else
Apply Tactic 3
Strategies
Defining Program Synthesis
12
Classic Synthesis
Program Optimization
Program Estimation
Test Generation
Superoptimization
Program Repair
This Lecture
13
Classic Synthesis
Program Estimation
SyGuS: Syntax-Guided Synthesis
14
Example: max
15
SyGuS format: Synth-Lib
16
(set-logic LIA)
(synth-fun max2 ((x Int) (y Int)) Int
((Start Int (x y
(+ Start Start)
(ite StartBool Start Start)))……))
(declare-var x Int)
(declare-var y Int)
(constraint (>= (max2 x y) x))
……
(check-synth)
Program Synthesis as a Search Problem
17
Generate
Verify
Q1: How to generate the next program to be verified?
Q2: How to verify the correctness?
a program
correct
incorrect
Q1: How to verify correctness?
18
Fast
Slow
Can we combine
the two?
CEGIS: Counter-Example Guided Inductive Synthesis
19
Generate
Verify
Program
Counter-
Example
No Counter-
Example
Q2: How to generate the next program to be verified?
20
Top-Down Enumeration
21
Bottom-Up Enumeration
22
Optimization
23
Pruning
24
Ite BoolExpr x x
(declare-fun boolExpr () Int)
(declare-fun max2 ((x Int) (y Int)) Int
(ite boolExpr x x))
max2(1,2)=2
(assert (= (max2 1 2) 2))
(check-sat)
Needs to balance between the benefit and the cost.
Equivalence reduction: How to determine equivalence?
25
How to generate the next program to be verified?
26
Representation-based
27
FlashMeta: Basic Idea
28
FlashMeta: Single Test
29
Intersection of grammars
30
FlashMeta: Multiple Tests
31
FlashMeta: Discussion
32
How to generate the next program to be verified?
33
Component-Based Program Synthesis
34
+
-
if
x
y
+
-
o1
o2
o3
o4
o5
o6
o7
i11
i12
i21
i22
i31
i32
i41
i42
i51
i53
i52
1
2
3
4
5
6
7
8
9
Components
Connection
Points
Generate constraints
35
This Lecture
36
Classic Synthesis
Program Estimation
Program Estimation
37
Program Estimation as an Search Problem
38
Generate
Estimate
Program
Probability
Enough?
Program with the highest probability
Learning to synthesis (L2S)
Example: Condition Completion
40
Space of Conditions
Q3: Estimating the Probability
41
Decomposing Generation
42
E
E
>12
hours
E
E
>12
E
Expand E with E -> E “> 12”
Expand E with
E -> “hours”
Probability of the program
43
E
E
>12
…;if(
) throw new ArgException();
Training models
44
Feature Engineering
45
E
E
>12
…;if(
) throw new ArgException();
Can we use a different expansion order?
46
E
E
>12
hours
hours
E
hours
The order may greatly affect the performance of L2S.
E
E
>12
hours
E
E
>12
E
Annotations
From Grammar to Rewriting Rules
Grammar | Top-down Rules | Bottom-up Rules |
| | |
| | |
| | |
�
Creation Rules | |
| // starting from the root // starting from a middle node // starting from a leaf |
Ending Rule | |
Example
E
E
>12
hours
E
>12
E
>12
hours
hours
E
E
>12
hours
Unambiguity
50
Q4: How to find the most probable program?
51
>12
hours
>0
value
0.6 * 0.2
= 0.12
0.3 * 0.8
= 0.24
Use Metaheuristic Search
52
Applications
53
Repairing Conditional Expressions
Missing boundary checks
- if (hours >= 24)
+ if (hours > 24)
withinOneDay=true;
Conditions too weak or too strong
lcm = Math.abs(a+b);
+ if (lcm == Integer.MIN_Value)
+ throw new ArithmeticException();
L2S Configuration
55
Results
56
Also repaired 8 unique bugs that have never been repaired by any approach.
Benchmark: Defects4J
Generating Code from Natural Language Expression
57
L2S Configuration
58
A CNN-based Network Architecture
59
Results
60
Benchmark: HearthStone
Newest Results
61
Future Learning
62
Reference
63