SAT-based Program Synthesis - GitHub Pages

202
SAT-based Program Synthesis Ruben Martins SAT/SMT/AR Summer School 2019 July 6, 2019

Transcript of SAT-based Program Synthesis - GitHub Pages

Page 1: SAT-based Program Synthesis - GitHub Pages

SAT-based Program SynthesisRuben Martins

SAT/SMT/AR Summer School 2019July 6, 2019

Page 2: SAT-based Program Synthesis - GitHub Pages

What is Program Synthesis?

∃ P. ∀ x. φ(x, P(x)) • Find a program P that for all inputs x meets the specification φ

Specifications φ Program P

Page 4: SAT-based Program Synthesis - GitHub Pages

[email protected]

[email protected]@itwareinc.com

[email protected]@contoso.com

[email protected]

First NameNancyAndrew

JanMariya

AlexanderAmr

Programming by Examples

• Flash Fill (Excel 2013 feature): • Automating string processing in spreadsheets using input-output

examples. POPL 2011

Page 5: SAT-based Program Synthesis - GitHub Pages

Name Month Rate1 Rate2Aira 1 12 23Aira 2 18 73Ben 1 53 19Ben 2 22 87Cat 1 22 87Cat 2 67 43

Name avg1 avg2Aira 15.0 48Ben 37.5 53Cat 44.5 65

Programming by Examples

• Can we find a program that automatically transforms tables given input-output examples?

Page 6: SAT-based Program Synthesis - GitHub Pages

R program: TBL_15=group_by(p8_input1,`Name`)TBL_7=summarise(TBL_15,avg2=mean(`Rate2`))TBL_3=inner_join(TBL_7,p8_input1)TBL_1=group_by(TBL_3,`Name`,`avg2`)morpheus=summarise(TBL_1,avg1=mean(`Rate1`))morpheus=select(morpheus,1,3,2)

Programming by Examples

• Component-based synthesis of table consolidation and transformation tasks from examples. PLDI 2017

Name Month Rate1 Rate2Aira 1 12 23Aira 2 18 73Ben 1 53 19Ben 2 22 87Cat 1 22 87Cat 2 67 43

Name avg1 avg2Aira 15.0 48Ben 37.5 53Cat 44.5 65

Page 7: SAT-based Program Synthesis - GitHub Pages

Programming by Examples

public static int getDayFromString(String date, String pat) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pat);

LocalDate localdate = LocalDate.parse(date, dtf); int day = localdate.getDayOfMonth();

return day; }

public static boolean test() { return (getDayFromString("2013/06/13", "yyyy/MM/dd") == 13); }

• Can we find a sequence of API calls using java.time in Java 8 to get the day from a Date in string format?

Page 8: SAT-based Program Synthesis - GitHub Pages

Programming by Examples

public static int getDayFromString(String date, String pat) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pat);

LocalDate localdate = LocalDate.parse(date, dtf); int day = localdate.getDayOfMonth();

return day; }

public static boolean test() { return (getDayFromString("2013/06/13", "yyyy/MM/dd") == 13); }

• Can we find a sequence of API calls using java.time in Java 8 to get the day from a Date in string format?

• Component-based synthesis for complex APIs. POPL 2017

Page 9: SAT-based Program Synthesis - GitHub Pages

Who can Program Synthesis help?

Page 10: SAT-based Program Synthesis - GitHub Pages

Who can Program Synthesis help?• Are we trying to replace programmers? No!

• We want to make programmers life easier • Automating tedious and repetitive tasks

Page 11: SAT-based Program Synthesis - GitHub Pages

Who can Program Synthesis help?

• 99% of computer users cannot program! • They struggle with simple repetitive tasks • Help non-CS people to automate their daily

tasks

• Are we trying to replace programmers? No! • We want to make programmers life easier • Automating tedious and repetitive tasks

Page 12: SAT-based Program Synthesis - GitHub Pages

How do Program Synthesizers Work?

Enumerative Search Constraint SolvingStochastic Search

Page 13: SAT-based Program Synthesis - GitHub Pages

How do Program Synthesizers Work?

Enumerative Search Constraint SolvingStochastic Search

• Combinatorial search for all possible programs

Page 14: SAT-based Program Synthesis - GitHub Pages

How do Program Synthesizers Work?

Enumerative Search Constraint SolvingStochastic Search

• Build a statistical models using large corpora • Guide the search using statistical models

Page 15: SAT-based Program Synthesis - GitHub Pages

How do Program Synthesizers Work?

Enumerative Search Constraint SolvingStochastic Search

• Encode the synthesis problem to SAT/SMT • Prune infeasible incomplete programs with logical deduction

Page 16: SAT-based Program Synthesis - GitHub Pages

OutlineExamples of Synthesizers

Synthesis of Java programs

Conflict-driven Synthesis

Page 17: SAT-based Program Synthesis - GitHub Pages

OutlineExamples of Synthesizers

Synthesis of Java programs

Conflict-driven Synthesis

Page 18: SAT-based Program Synthesis - GitHub Pages
Page 19: SAT-based Program Synthesis - GitHub Pages

Demo PROSEhttps://microsoft.github.io/prose/

Page 20: SAT-based Program Synthesis - GitHub Pages

Microsoft PROSE

Requires witness functions for pruning the search space

Program synthesis in a real-world scenario

Provides a ranking for the most likely solutions

Very efficient for string manipulations

Hard to extend to other domains

Page 21: SAT-based Program Synthesis - GitHub Pages

Specification SyntacticRestrictions

Synthesized Program

Synthesizer

• Combine: • Human expert insights • Constraint solving

• Benefit from progress in SAT / SMT • Extends SMT-LIB to SYNTH-LIB

Syntax-Guided Synthesis (SyGuS)

• Syntax-Guided Synthesis. FMCAD 2013

Page 22: SAT-based Program Synthesis - GitHub Pages

Syntax-Guided Synthesis (SyGuS)Specification Syntactic

Restrictions

Synthesized Program

Synthesizer

• Inputs to SyGuS: • Background theory • Function to be synthesized • Specification • Context-free grammar

Page 23: SAT-based Program Synthesis - GitHub Pages

(set-logic LIA) (synth-fun max2 ((x Int) (y Int)) Int ((Start Int (x y 0 1 (+ Start Start) (- Start Start) (ite StartBool Start Start))) (StartBool Bool ((and StartBool StartBool)

(or StartBool StartBool) (not StartBool) (<= Start Start)))) (declare-var x Int) (declare-var y Int) (constraint (>= (max2 x y) x)) (constraint (>= (max2 x y) y)) (constraint (or (= x (max2 x y)) (= y (max2 x y)))) (check-synth)

Background theory

Page 24: SAT-based Program Synthesis - GitHub Pages

(set-logic LIA) (synth-fun max2 ((x Int) (y Int)) Int ((Start Int (x y 0 1 (+ Start Start) (- Start Start) (ite StartBool Start Start))) (StartBool Bool ((and StartBool StartBool)

(or StartBool StartBool) (not StartBool) (<= Start Start)))) (declare-var x Int) (declare-var y Int) (constraint (>= (max2 x y) x)) (constraint (>= (max2 x y) y)) (constraint (or (= x (max2 x y)) (= y (max2 x y)))) (check-synth)

Background theoryFunction

Page 25: SAT-based Program Synthesis - GitHub Pages

(set-logic LIA) (synth-fun max2 ((x Int) (y Int)) Int ((Start Int (x y 0 1 (+ Start Start) (- Start Start) (ite StartBool Start Start))) (StartBool Bool ((and StartBool StartBool)

(or StartBool StartBool) (not StartBool) (<= Start Start)))) (declare-var x Int) (declare-var y Int) (constraint (>= (max2 x y) x)) (constraint (>= (max2 x y) y)) (constraint (or (= x (max2 x y)) (= y (max2 x y)))) (check-synth)

Background theoryFunction

Grammar

Page 26: SAT-based Program Synthesis - GitHub Pages

(set-logic LIA) (synth-fun max2 ((x Int) (y Int)) Int ((Start Int (x y 0 1 (+ Start Start) (- Start Start) (ite StartBool Start Start))) (StartBool Bool ((and StartBool StartBool)

(or StartBool StartBool) (not StartBool) (<= Start Start)))) (declare-var x Int) (declare-var y Int) (constraint (>= (max2 x y) x)) (constraint (>= (max2 x y) y)) (constraint (or (= x (max2 x y)) (= y (max2 x y)))) (check-synth)

Background theoryFunction

Grammar

Specification

Page 27: SAT-based Program Synthesis - GitHub Pages

(set-logic LIA) (synth-fun max2 ((x Int) (y Int)) Int ((Start Int (x y 0 1 (+ Start Start) (- Start Start) (ite StartBool Start Start))) (StartBool Bool ((and StartBool StartBool)

(or StartBool StartBool) (not StartBool) (<= Start Start)))) (declare-var x Int) (declare-var y Int) (constraint (>= (max2 x y) x)) (constraint (>= (max2 x y) y)) (constraint (or (= x (max2 x y)) (= y (max2 x y)))) (check-synth)

Background theoryFunction

Grammar

Specification

Execute

Page 28: SAT-based Program Synthesis - GitHub Pages

Demo SyGushttp://sygus.seas.upenn.edu/

Page 29: SAT-based Program Synthesis - GitHub Pages

Syntax-Guided Synthesis

Theories are restricted to SMT-LIB

Input is not specific to any synthesis problem

Leverages the advances in SAT / SMT solving

Specifications allows for human insights

Does not scale for large grammars

Page 30: SAT-based Program Synthesis - GitHub Pages

Program Synthesis as Sketchingharness void doubleSketch(int x){ int t = x * ??; assert t == x + x; }

• Sketch of the program: • Partial program with holes (“??”)

• Synthesizer finds values to the holes that satisfies the specifications • Uses SAT / SMT technology to find the missing values

• Programming by sketching for bit-streaming programs. PLDI 2005

Page 31: SAT-based Program Synthesis - GitHub Pages

Demo Sketchhttps://people.csail.mit.edu/asolar/

Page 32: SAT-based Program Synthesis - GitHub Pages

Sketching

Holes must be expressed by SMT theories

Synthesis real-world code (C, Java)

Leverages the advances in SAT / SMT solving

Specifications allows for human insights

Writing the sketch may be as hard as writing the program

Page 33: SAT-based Program Synthesis - GitHub Pages

Synthesis of Java programs

Programmers spend a lot of effort learning APIs!

Send HTTP requestCompute GCD

Rotate an image

• Component-based synthesis for complex APIs. POPL 2017

Page 34: SAT-based Program Synthesis - GitHub Pages

public static int getDayFromString(String date, String pat) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pat);

LocalDate localdate = LocalDate.parse(date, dtf); int day = localdate.getDayOfMonth();

return day; }

public static boolean test() { return (getDayFromString("2013/06/13", "yyyy/MM/dd") == 13); }

Synthesizing Programs with APIs

Page 35: SAT-based Program Synthesis - GitHub Pages

public static boolean test() { return (getDayFromString("2013/06/13", "yyyy/MM/dd") == 13); }

Synthesizing Programs with APIspublic static int getDayFromString(String date, String pat) {

DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pat); LocalDate localdate = LocalDate.parse(date, dtf); int day = localdate.getDayOfMonth();

return day; }

Page 36: SAT-based Program Synthesis - GitHub Pages

Demo SyPethttps://utopia-group.github.io/sypet/

Page 37: SAT-based Program Synthesis - GitHub Pages

SyPet

Does not work well for libraries with few types

Works for real-world code

Scales for large libraries

Can handle any Java library

Does not support loops or conditionals

Page 38: SAT-based Program Synthesis - GitHub Pages

OutlineIntroduction to Syntax-Guided Synthesis (SyGus)

Synthesis of Java code

Conflict-driven Synthesis

Page 39: SAT-based Program Synthesis - GitHub Pages

public static boolean test() { return (getDayFromString("2013/06/13", "yyyy/MM/dd") == 13); }

Synthesizing Programs with APIspublic static int getDayFromString(String date, String pat) {

DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pat); LocalDate localdate = LocalDate.parse(date, dtf); int day = localdate.getDayOfMonth();

return day; }

Page 40: SAT-based Program Synthesis - GitHub Pages

How to Find the Correct Program?

Page 41: SAT-based Program Synthesis - GitHub Pages

How to Find the Correct Program?Use Petri net reachability analysis to look for

well-typed programs of the desired type

Page 42: SAT-based Program Synthesis - GitHub Pages

How to Find the Correct Program?

• Model relationships between APIs using Petri nets • Use type signature of desired method to mark initial and

target configurations • Perform reachability analysis to find valid sequences of

method calls

Use Petri net reachability analysis to look for well-typed programs of the desired type

Page 43: SAT-based Program Synthesis - GitHub Pages

Petri Nets in a Nutshell1 1

1 1

12

P1 T1 P2T3

P3

T2

Page 44: SAT-based Program Synthesis - GitHub Pages

Petri Nets in a Nutshell

• Petri net is a generalized graph with two kinds of nodes: places and transitions

1 1

1 1

12

P1 T1 P2T3

P3

T2

Page 45: SAT-based Program Synthesis - GitHub Pages

Petri Nets in a Nutshell

• Petri net is a generalized graph with two kinds of nodes: places and transitions

• Each place contains zero or more tokens; edges are labeled with a number of tokens

1 1

1 1

12

P1 T1 P2T3

P3

T2

Page 46: SAT-based Program Synthesis - GitHub Pages

• A transition T can fire if, for each incoming edge (p,T) with label n, place p contains at least n tokens

• Firing a transition T consumes (resp. produces) the indicated number of tokens at the source (resp. target) nodes

1 1

1 1

12

P1 T1 P2T3

P3

T2

Petri Nets in a Nutshell

Page 47: SAT-based Program Synthesis - GitHub Pages

• A transition T can fire if, for each incoming edge (p,T) with label n, place p contains at least n tokens

• Firing a transition T consumes (resp. produces) the indicated number of tokens at the source (resp. target) nodes

1 1

1 1

12

P1 T1 P2T3

P3

T2

Petri Nets in a Nutshell

Page 48: SAT-based Program Synthesis - GitHub Pages

Reachability Problem in Petri Nets

• Reachability problem: Given a Petri net with initial marking M and a target marking M’, is it possible to obtain M’ by firing a sequence of transitions?

1 1

1 1

12

P1 T1 P2T3

P3

T2

Page 49: SAT-based Program Synthesis - GitHub Pages

• Example: Consider marking M’ : [P1→0,P2→0,P3→1].

1 1

1 1

12

P1 T1 P2T3

P3

T2

Reachability Problem in Petri Nets

Page 50: SAT-based Program Synthesis - GitHub Pages

• Example: Consider marking M’ : [P1→0,P2→0,P3→1].

1 1

1 1

12

P1 T1 P2T3

P3

T2

Reachability Problem in Petri Nets

• This marking is reachable, and accepting run is T1,T1,T2.

Page 51: SAT-based Program Synthesis - GitHub Pages

• Example: Consider marking M’ : [P1→0,P2→0,P3→1].

1 1

1 1

12

P1 T1 P2T3

P3

T2

Reachability Problem in Petri Nets

• This marking is reachable, and accepting run is T1,T1,T2.

Page 52: SAT-based Program Synthesis - GitHub Pages

• Example: Consider marking M’ : [P1→0,P2→0,P3→1].

1 1

1 1

12

P1 T1 P2T3

P3

T2

Reachability Problem in Petri Nets

• This marking is reachable, and accepting run is T1,T1,T2.

Page 53: SAT-based Program Synthesis - GitHub Pages

• Example: Consider marking M’ : [P1→0,P2→0,P3→1].

1 1

1 1

12

P1 T1 P2T3

P3

T2

Reachability Problem in Petri Nets

• This marking is reachable, and accepting run is T1,T1,T2.

Page 54: SAT-based Program Synthesis - GitHub Pages

SyPet Architecture

Construct Petri net

APIs

Reachability analysis

Candidate Sketch

Sketch Completion

Candidate Program

CheckCandidate

Backtrack

Init/target markings

Signature

Page 55: SAT-based Program Synthesis - GitHub Pages

Construct Petri net

APIs

Reachability analysis

Candidate Sketch

Sketch Completion

Candidate Program

CheckCandidate

Backtrack

Init/target markings

Signature

SyPet Architecture

Page 56: SAT-based Program Synthesis - GitHub Pages

Petri Net Constructionclass CPt { CPt(Int x, Int y, Color c); Int getX(); void setColor(Color c); ...}

Int

Color

CPt void

Page 57: SAT-based Program Synthesis - GitHub Pages

Petri Net Constructionclass CPt { CPt(Int x, Int y, Color c); Int getX(); void setColor(Color c); ...}

Int

Color

CPt void

CPt(..)

12

1

Page 58: SAT-based Program Synthesis - GitHub Pages

Petri Net Constructionclass CPt { CPt(Int x, Int y, Color c); Int getX(); void setColor(Color c); ...}

Int

Color

CPt void

CPt(..)

12

1

11

getX

Page 59: SAT-based Program Synthesis - GitHub Pages

Petri Net Constructionclass CPt { CPt(Int x, Int y, Color c); Int getX(); void setColor(Color c); ...}

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX

Page 60: SAT-based Program Synthesis - GitHub Pages

Clone Transitions

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX

Page 61: SAT-based Program Synthesis - GitHub Pages

Clone Transitions• Our construction so far views objects as“resources”– every

method “consumes” and “produces” objects

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX

Page 62: SAT-based Program Synthesis - GitHub Pages

Clone Transitions• Our construction so far views objects as“resources”– every

method “consumes” and “produces” objects• But in conventional languages, we can reuse objects!

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX

Page 63: SAT-based Program Synthesis - GitHub Pages

Clone Transitions• Our construction so far views objects as“resources”– every

method “consumes” and “produces” objects• But in conventional languages, we can reuse objects! • Therefore, augment Petri net model with clone transitions

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX

Page 64: SAT-based Program Synthesis - GitHub Pages

Clone Transitions• Our construction so far views objects as“resources”– every

method “consumes” and “produces” objects• But in conventional languages, we can reuse objects! • Therefore, augment Petri net model with clone transitions

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX

1

2

Page 65: SAT-based Program Synthesis - GitHub Pages

Clone Transitions• Our construction so far views objects as“resources”– every

method “consumes” and “produces” objects• But in conventional languages, we can reuse objects! • Therefore, augment Petri net model with clone transitions

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

Page 66: SAT-based Program Synthesis - GitHub Pages

Clone Transitions• Our construction so far views objects as“resources”– every

method “consumes” and “produces” objects• But in conventional languages, we can reuse objects! • Therefore, augment Petri net model with clone transitions

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2 1

2

Page 67: SAT-based Program Synthesis - GitHub Pages

Clone Transitions• Our construction so far views objects as“resources”– every

method “consumes” and “produces” objects• But in conventional languages, we can reuse objects! • Therefore, augment Petri net model with clone transitions

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Page 68: SAT-based Program Synthesis - GitHub Pages

SyPet Architecture

Construct Petri net

APIs

Reachability analysis

Candidate Sketch

Sketch Completion

Candidate Program

CheckCandidate

Backtrack

Init/target markings

Signature

Page 69: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Page 70: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Page 71: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Page 72: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Page 73: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Page 74: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Target marking:

Page 75: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Target marking:Cpt = 1

Page 76: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Target marking:Cpt = 1void = *

Page 77: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Target marking:Cpt = 1void = *int = 0

Page 78: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Target marking:Cpt = 1void = *int = 0color = 0

Page 79: SAT-based Program Synthesis - GitHub Pages

Initial and Target Markings

CPt shift (CPt p, Int shiftX, Int shiftY)

Int

Color

CPt void

CPt(..)

12

1

1

setColor

1

1

11

getX1

2

1

2

1

2 1

2

Use signature to determine initial and target markings of Petri net

Target marking:Cpt = 1void = *int = 0color = 0

All args must be used!

Page 80: SAT-based Program Synthesis - GitHub Pages

Building a Petri Netclass Point { Point(); int getX(); int getY(); void setX(int); void setY(int); }

class MyPoint { MyPoint(int x, int y); int getX(); int getY();}

Page 81: SAT-based Program Synthesis - GitHub Pages

Building a Petri Netclass Point { Point(); int getX(); int getY(); void setX(int); void setY(int); }

class MyPoint { MyPoint(int x, int y); int getX(); int getY();}

• What are the places (i.e., types)? • What are the transitions (i.e., methods)?

Page 82: SAT-based Program Synthesis - GitHub Pages

Building a Petri Netclass Point { Point(); int getX(); int getY(); void setX(int); void setY(int); }

Page 83: SAT-based Program Synthesis - GitHub Pages

Building a Petri Net

IntPoint

voidclass Point { Point(); int getX(); int getY(); void setX(int); void setY(int); }

Page 84: SAT-based Program Synthesis - GitHub Pages

Building a Petri Net

IntPoint

void

11

getX1

Point()

1

1setX

1

1

class Point { Point(); int getX(); int getY(); void setX(int); void setY(int); }

Page 85: SAT-based Program Synthesis - GitHub Pages

Building a Petri Net

IntPoint

void

11

getX

11getY

1

Point()

1

1setX

1

setY

1

1

1

1

class Point { Point(); int getX(); int getY(); void setX(int); void setY(int); }

Page 86: SAT-based Program Synthesis - GitHub Pages

Building a Petri Net

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

2

1

21

2

class Point { Point(); int getX(); int getY(); void setX(int); void setY(int); }

Page 87: SAT-based Program Synthesis - GitHub Pages

Building a Petri Net

class MyPoint { MyPoint(int x, int y); int getX(); int getY();}

Page 88: SAT-based Program Synthesis - GitHub Pages

Building a Petri Net

IntMP

MyPoint(int,int)

12

1

2

1

2

class MyPoint { MyPoint(int x, int y); int getX(); int getY();}

Page 89: SAT-based Program Synthesis - GitHub Pages

Building a Petri Net

IntMP

11mp.getX

11mp.getY

MyPoint(int,int)

12

1

2

1

2

class MyPoint { MyPoint(int x, int y); int getX(); int getY();}

Page 90: SAT-based Program Synthesis - GitHub Pages

Building a Petri Net

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2

1

2 class Point { Point(); int getX(); int getY(); void setX(int); void setY(int); }

Page 91: SAT-based Program Synthesis - GitHub Pages

Building a Petri Net

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1class MyPoint { MyPoint(int x, int y); int getX(); int getY();}

Page 92: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1Point convert(Mypoint pt)

• Synthesize this function:

What is the Initial Marking?

Page 93: SAT-based Program Synthesis - GitHub Pages

What is the Initial Marking?

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1Point convert(Mypoint pt)

• Synthesize this function:

Initial marking: <MP = 1, void = 1, Int = 0, Point = 0>

Page 94: SAT-based Program Synthesis - GitHub Pages

What is the Final Marking?

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1Point convert(Mypoint pt)

• Synthesize this function:

Page 95: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1Point convert(Mypoint pt)

• Synthesize this function:

Final marking: <MP = 0, void = *, Int = 0, Point = 1>

What is the Final Marking?

Page 96: SAT-based Program Synthesis - GitHub Pages

SyPet Architecture

Construct Petri net

APIs

Reachability analysis

Candidate Sketch

Sketch Completion

Candidate Program

CheckCandidate

Backtrack

Init/target markings

Signature

Page 97: SAT-based Program Synthesis - GitHub Pages

Reachability Analysis

• Need to perform reachability analysis to identify accepting runs of the Petri net

• Reachability analysis of Petri nets can be encoded to SAT: • Find a reachable path of size k • Enumerate all reachable paths

All accepting runs of Petri net correspond to method call sequences with desired type signature!

Page 98: SAT-based Program Synthesis - GitHub Pages

Reachable Paths

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

Page 99: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) mp.getX2) Point()3) Clone-Point4) setX

Not all Reachable Paths are a Solution!

Page 100: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) mp.getX2) Point()3) Clone-Point4) setX

Not all Reachable Paths are a Solution!

Page 101: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) mp.getX2) Point()3) Clone-Point4) setX

Not all Reachable Paths are a Solution!

Page 102: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) mp.getX2) Point()3) Clone-Point4) setX

Not all Reachable Paths are a Solution!

Page 103: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) mp.getX2) Point()3) Clone-Point4) setX

Not all Reachable Paths are a Solution!

Page 104: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) mp.getX2) Point()3) Clone-Point4) setX

Not all Reachable Paths are a Solution!

Page 105: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) mp.getX2) Point()3) Clone-Point4) setX

Not all Reachable Paths are a Solution!

Page 106: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) mp.getX2) Point()3) Clone-Point4) setX

Not all Reachable Paths are a Solution!

Page 107: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) getX7) getY

Reachable Path that Corresponds to a Solution

Page 108: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) getX7) getY

Reachable Path that Corresponds to a Solution

Page 109: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) getX7) getY

Reachable Path that Corresponds to a Solution

Page 110: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) getX7) getY

Reachable Path that Corresponds to a Solution

Page 111: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) getX7) getY

Reachable Path that Corresponds to a Solution

Page 112: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) getX7) getY

Reachable Path that Corresponds to a Solution

Page 113: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) getX7) getY

Reachable Path that Corresponds to a Solution

Page 114: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) getX7) getY

Reachable Path that Corresponds to a Solution

Page 115: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) getX7) getY

Reachable Path that Corresponds to a Solution

Page 116: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) setX7) getY

Reachable Path that Corresponds to a Solution

Page 117: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) setX7) Clone-Point7) getY

Reachable Path that Corresponds to a Solution

Page 118: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1 Point convert(Mypoint pt)

• Synthesize this function:

1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) setX7) Clone-Point8) setY

Reachable Path that Corresponds to a Solution

Page 119: SAT-based Program Synthesis - GitHub Pages

Why a Petri Net and not a Graph?

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1

Page 120: SAT-based Program Synthesis - GitHub Pages

IntPoint

void

11

getX

11getY

1

1

Point()

1

1setX

1

setY

1

1

1

1

21

2MP

1

mp.getX

1mp.getY

MyPoint(int,int)

2

1

1

1

2

1

2

1• Graphs do not support:

• All incoming edges to a transition are part of the path

• Resource consumption

Why a Petri Net and not a Graph?

Page 121: SAT-based Program Synthesis - GitHub Pages

Accepting Run as Program Sketch

Construct Petri net

APIs

Reachability analysis

Candidate Sketch

Sketch Completion

Candidate Program

CheckCandidate

Backtrack

Init/target markings

Signature

Page 122: SAT-based Program Synthesis - GitHub Pages

Accepting Run as Program Sketch Accepting run of Petri net corresponds to program sketch because it does not specify arguments of each method call.

Construct Petri net

APIs

Reachability analysis

Candidate Sketch

Sketch Completion

Candidate Program

CheckCandidate

Backtrack

Init/target markings

Signature

Page 123: SAT-based Program Synthesis - GitHub Pages

Sketch Completion 1) Clone-MP2) mp.getX3) mp.getY4) Point()5) Clone-Point6) setX7) Clone-Point8) setY

• Remove the Clone transitions

Point convert(Mypoint pt){ int x = pt.getX(); int y = pt.getY(); Point p = new Point(); p.setX(x); p.setY(y); return p;

}

Page 124: SAT-based Program Synthesis - GitHub Pages

2) mp.getX3) mp.getY4) Point()6) setX8) setY

Point convert(Mypoint pt){ int x = pt.getX(); int y = pt.getY(); Point p = new Point(); p.setX(x); p.setY(y); return p;

}• What is the code with holes?

Sketch Completion

Page 125: SAT-based Program Synthesis - GitHub Pages

2) mp.getX3) mp.getY4) Point()6) setX8) setY

Point convert(Mypoint pt){ int x = #1.getX(); int y = #2.getY(); Point p = new Point(); p.setX(#3); p.setY(#4); return #5;

}• What is the code with holes?

Sketch Completion

• Find the arguments that should be used in each hole such that the program type checks

Page 126: SAT-based Program Synthesis - GitHub Pages

2) mp.getX3) mp.getY4) Point()6) setX8) setY

• What is the code with holes?

Point convert(Mypoint pt){ int x = pt.getX(); int y = pt.getY(); Point p = new Point(); p.setX(#3); p.setY(#4); return p;

}

Sketch Completion

• Find the arguments that should be used in each hole such that the program type checks

Page 127: SAT-based Program Synthesis - GitHub Pages

2) mp.getX3) mp.getY4) Point()6) setX8) setY

• What is the code with holes?

Point convert(Mypoint pt){ int x = pt.getX(); int y = pt.getY(); Point p = new Point(); p.setX(y); p.setY(x); return p;

}

Sketch Completion

• Find the arguments that should be used in each hole such that the program type checks

Page 128: SAT-based Program Synthesis - GitHub Pages

2) mp.getX3) mp.getY4) Point()6) setX8) setY

• What is the code with holes?

Point convert(Mypoint pt){ int x = pt.getX(); int y = pt.getY(); Point p = new Point(); p.setX(x); p.setY(y); return p;

}

Sketch Completion

• Find the arguments that should be used in each hole such that the program type checks

Page 129: SAT-based Program Synthesis - GitHub Pages

Construct Petri net

APIs

Checking the Candidate

Reachability analysis

Candidate Sketch

Sketch Completion

Candidate Program

CheckCandidate

Backtrack

Init/target markings

Signature

Page 130: SAT-based Program Synthesis - GitHub Pages

TestingPoint convert(Mypoint pt){ int x = pt.getX(); int y = pt.getY(); Point p = new Point(); p.setX(x); p.setY(y); return p;

}

• Test case to check the conversion:

Page 131: SAT-based Program Synthesis - GitHub Pages

Point convert(Mypoint pt){ int x = pt.getX(); int y = pt.getY(); Point p = new Point(); p.setX(x); p.setY(y); return p;

}

bool test(){

MyPoint mp = new MyPoint(1,2); Point p = convert(mp); return (p.getX() == 1 && p.getY() == 2);

}

• Test case to check the conversion:

Testing

Page 132: SAT-based Program Synthesis - GitHub Pages

For more information

https://utopia-group.github.io/sypet/

Page 133: SAT-based Program Synthesis - GitHub Pages

OutlineIntroduction to Syntax-Guided Synthesis (SyGus)

Synthesis of Java code

Conflict-driven Synthesis

Page 134: SAT-based Program Synthesis - GitHub Pages

How do Program Synthesizers Work?

Enumerative Search Constraint SolvingStochastic Search

Page 135: SAT-based Program Synthesis - GitHub Pages

How do Program Synthesizers Work?

Can we learn from past mistakes?

Enumerative Search Constraint SolvingStochastic Search

Page 136: SAT-based Program Synthesis - GitHub Pages

Learning from Mistakes

•λx.map(x,…)

This program will not satisfy the input-output specification since map preserves the size of the list!

• Input: x= [1,2,3] • Output: y= [1,2]

Page 137: SAT-based Program Synthesis - GitHub Pages

Learning from Mistakes

•λx.map(x,…)

This program will not satisfy the input-output specification since map preserves the size of the list!

•λx.f(x,…)

If f preserves or increases the size of the list then f will also be infeasible!

• Input: x= [1,2,3] • Output: y= [1,2]

Learning

Page 138: SAT-based Program Synthesis - GitHub Pages

Learning from Mistakes

•λx.map(x,…)

This program will not satisfy the input-output specification since map preserves the size of the list!

•λx.f(x,…)

If f preserves or increases the size of the list then f will also be infeasible!

• Input: x= [1,2,3] • Output: y= [1,2]

Learning

Can we learn from past mistakes?If λx.map(x,…) in an infeasible program then we can rule out many other erroneous programs such as λx.reverse(x) or λx.sort(x)

Page 139: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

ConflictLearning

No conflict

Current decision

From SAT solvers to Synthesis

SATLearn from

Conflict

Pick a variable

UNSAT

Page 140: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

ConflictLearning

No conflict

Partial Program

Conflict-Driven Synthesis

Synthesized Program Learn from

Conflict

Pick a component

No Solution

• Program Synthesis using Conflict-Driven Learning. PLDI’18

Page 141: SAT-based Program Synthesis - GitHub Pages

Running Example• Compute the scores of the best

k teams of a soccer league

Page 142: SAT-based Program Synthesis - GitHub Pages

• computeKsum:: List -> Int -> Int• computeKSum x1 x2 =• Inputs:

• x1 = [49, 62, 82, 54, 76]• x2 = 2

• Output:• 158 (82 + 76)

Running Example• Compute the scores of the best

k teams of a soccer league

Page 143: SAT-based Program Synthesis - GitHub Pages

Sample Domain Specific Language

N->0 | … | 10 | X | last(L) | head(L) |

sum(L) | maximum(L) | minimum(L)

L-> take(L,N) | filter(L,T) | sort(L) | reverse(L) | X

T-> geqz | leqz | eqz

• computeKsum:: List -> Int -> Int• computeKSum ( [49, 62, 82, 54, 76] , 2) = 158

Page 144: SAT-based Program Synthesis - GitHub Pages

• computeKsum:: List -> Int -> Int• computeKSum x1 x2 =

- - sort x1 in ascending orderL1 <- sort x1- - L2 is x1 in descending orderL2 <- reverse L1- - Take L2’s first x2 entriesL3 <- take L2 x2- - Compute sum of all elements in L3sum L3

Complete Programs

Page 145: SAT-based Program Synthesis - GitHub Pages

• computeKsum:: List -> Int -> Int• computeKSum x1 x2 =

- - sort x1 in ascending orderL1 <- sort x1- - L2 is x1 in descending orderL2 <- reverse L1- - Take L2’s first x2 entriesL3 <- take L2 x2- - Compute sum of all elements in L3sum L3

Complete ProgramsN0 sum

take

reverse x2

sort

x1

N1

N2 N3

N4

N5

• Program:• Abstract Syntax

Trees (ASTs)

Page 146: SAT-based Program Synthesis - GitHub Pages

• computeKsum:: List -> Int -> Int• computeKSum x1 x2 =

- - sort x1 in ascending orderL1 <- sort x1- - L2 is x1 in descending orderL2 <- reverse L1- - Take L2’s first x2 entriesL3 <- take L2 x2- - Compute sum of all elements in L3sum L3

Partial ProgramsN0 sum

take

reverse ?

?

N1

N2 N3

N4

• Partial Program:• Abstract Syntax

Trees (ASTs)

• Some nodes are unknown

Page 147: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

ConflictLearning

No conflict

Partial program

Learn from Conflict

Conflict-Driven Synthesis

Synthesized Program

Page 148: SAT-based Program Synthesis - GitHub Pages

Guiding the Search• Goal: Choose a component to each node of the program

Page 149: SAT-based Program Synthesis - GitHub Pages

Guiding the Search

• Machine learning (ML): • N-grams • Neural Networks

• Learning from large corpora

• Goal: Choose a component to each node of the program

Page 150: SAT-based Program Synthesis - GitHub Pages

N0 ?

• computeKsum:: List -> Int -> Int• Inputs: [49, 62, 82, 54, 76] , 2 • Output: 158

Guiding the Search

• Machine learning (ML): • N-grams • Neural Networks

• Learning from large corpora

• Goal: Choose a component to each node of the program

Page 151: SAT-based Program Synthesis - GitHub Pages

N0 sum

• computeKsum:: List -> Int -> Int• Inputs: [49, 62, 82, 54, 76] , 2 • Output: 158

?N1

Guiding the Search

• Machine learning (ML): • N-grams • Neural Networks

• Learning from large corpora

• Goal: Choose a component to each node of the program

Page 152: SAT-based Program Synthesis - GitHub Pages

N0 sum

• computeKsum:: List -> Int -> Int• Inputs: [49, 62, 82, 54, 76] , 2 • Output: 158

?N1

Guiding the Search

We can only check if the program is correct once we have a complete program!

• Machine learning (ML): • N-grams • Neural Networks

• Learning from large corpora

• Goal: Choose a component to each node of the program

Page 153: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

ConflictLearning

No conflict

Partial program

Conflict-Driven Synthesis

Synthesized Program Learn from

Conflict

Page 154: SAT-based Program Synthesis - GitHub Pages

Imprecise Specifications• Goal: Prune the search space with imprecise specifications

Page 155: SAT-based Program Synthesis - GitHub Pages

Imprecise Specifications• Precisely describing a component can be challenging

• Goal: Prune the search space with imprecise specifications

Page 156: SAT-based Program Synthesis - GitHub Pages

Imprecise Specifications• Precisely describing a component can be challenging• Use simple properties that over-approximate the

behavior of a component: • List properties: size, maximum, etc.

• Goal: Prune the search space with imprecise specifications

Page 157: SAT-based Program Synthesis - GitHub Pages

Imprecise Specifications

L-> filter(L,T) y.size <= x1.size y.max <= x1.max

L -> take(L,N) y.size <= x1.size ; y.size = x2 y.max <= x1.max

N -> head(L) y <= x1.max

• Precisely describing a component can be challenging• Use simple properties that over-approximate the

behavior of a component: • List properties: size, maximum, etc.

• Goal: Prune the search space with imprecise specifications

Page 158: SAT-based Program Synthesis - GitHub Pages

Pruning the Search Space

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

• x1 = [49, 62, 82, 54, 76] • y = 158N0 ->

(head)

N1 -> (take)

N2 -> (filter)N4 -> (x1)

Page 159: SAT-based Program Synthesis - GitHub Pages

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

N0 -> (head) y <= n1.max

N1 -> (take)

n1.max <= n2.max n1.size <= n2.size n1.size = n3

N2 -> (filter)

n2.size <= n4.size n2.max <= n4.max

N4 -> (x1) n4 = x1

• x1 = [49, 62, 82, 54, 76] • y = 158

Pruning the Search Space

Page 160: SAT-based Program Synthesis - GitHub Pages

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

N0 -> (head)

y <= n1.max(158 <= 82)

N1 -> (take)

n1.max <= n2.max n1.size <= n2.size n1.size = n3

N2 -> (filter)

n2.size <= n4.size n2.max <= n4.max

N4 -> (x1) n4 = x1

• x1 = [49, 62, 82, 54, 76]• y = 158

Pruning the Search Space

Page 161: SAT-based Program Synthesis - GitHub Pages

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

• x1 = [49, 62, 82, 54, 76]• y = 158

A partial program represents many complete programs!

Pruning the Search Space

Page 162: SAT-based Program Synthesis - GitHub Pages

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

• x1 = [49, 62, 82, 54, 76]• y = 158

We are only pruning one partial program!

A partial program represents many complete programs!

Pruning the Search Space

Page 163: SAT-based Program Synthesis - GitHub Pages

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

• x1 = [49, 62, 82, 54, 76]• y = 158

Can we prune equivalent infeasible partial programs?

We are only pruning one partial program!

A partial program represents many complete programs!

Pruning the Search Space

Page 164: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

ConflictLearning

No conflict

Partial program

Conflict-Driven Synthesis

Synthesized Program Learn from

Conflict

Page 165: SAT-based Program Synthesis - GitHub Pages

Learning from Mistakes• Goal: : Learn equivalent infeasible partial programs

Page 166: SAT-based Program Synthesis - GitHub Pages

Learning from Mistakes

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

Equivalent modulo conflict:• Two components X and X’ are

equivalent modulo conflict at node N if replacing X with X’ leads to the same conflict

• Goal: : Learn equivalent infeasible partial programs

Page 167: SAT-based Program Synthesis - GitHub Pages

Learning from Mistakes

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

Equivalent modulo conflict:• Two components X and X’ are

equivalent modulo conflict at node N if replacing X with X’ leads to the same conflict

How to detect equivalent modulo conflict components?

• Goal: : Learn equivalent infeasible partial programs

Page 168: SAT-based Program Synthesis - GitHub Pages

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

How to detect equivalent modulo conflict components?N0 -> (head) y <= n1.max

N1 -> (take)

n1.max <= n2.max n1.size < n2.size n1.size = n3

N2 -> (filter)

n2.size < n4.size n2.max <= n4.max

N4 -> (x1) n4 = x1

Learning from Mistakes

Page 169: SAT-based Program Synthesis - GitHub Pages

N0 head

filter ?

x1

N1

N2 N3

?N4 N5

N0 -> (head) y <= n1.max

N1 -> (take)

n1.max <= n2.max n1.size < n2.size n1.size = n3

N2 -> (filter)

n2.size < n4.size n2.max <= n4.max

N4 -> (x1) n4 = x1

take

Learning from MistakesHow to detect equivalent modulo conflict components?

Page 170: SAT-based Program Synthesis - GitHub Pages

Take n1.max <= n2.max Sort n1.max = n2.max

Reverse n1.max = n2.max

Filter n1.max <= n2.max

If the specification of X’ implies X then X ≡ X’: • take ≡ sort ≡ reverse ≡ filter

N0 head

filter ?

x1

N1

N2 N3

?N4 N5

take

Learning from MistakesHow to detect equivalent modulo conflict components?

Page 171: SAT-based Program Synthesis - GitHub Pages

Take n1.max <= n2.maxSort n1.max = n2.max

Reverse n1.max = n2.max

Filter n1.max <= n2.max

If the specification of X’ implies X then X ≡ X’: • take ≡ sort ≡ reverse ≡ filter

N0 head

filter

x1

N1

N2

?N4 N5

sort

Learning from MistakesHow to detect equivalent modulo conflict components?

Page 172: SAT-based Program Synthesis - GitHub Pages

Take n1.max <= n2.maxSort n1.max = n2.max

Reverse n1.max = n2.max

Filter n1.max <= n2.max

If the specification of X’ implies X then X ≡ X’: • take ≡ sort ≡ reverse ≡ filter

N0 head

filter

x1

N1

N2

?N4 N5

reverse

Learning from MistakesHow to detect equivalent modulo conflict components?

Page 173: SAT-based Program Synthesis - GitHub Pages

Take n1.max <= n2.maxSort n1.max = n2.max

Reverse n1.max = n2.max

Filter n1.max <= n2.max

If the specification of X’ implies X then X ≡ X’: • take ≡ sort ≡ reverse ≡ filter

N0 head

filter

x1

N1

N2

?N4 N5

filter

Learning from MistakesHow to detect equivalent modulo conflict components?

?N3

Page 174: SAT-based Program Synthesis - GitHub Pages

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

Using modulo conflict components:• We can learn a lemma that allow

us to rule out 63 other partial programs!

• Learning allows the synthesis algorithm to avoid similar mistakes in the future!

Learning from MistakesHow to detect equivalent modulo conflict components?

Page 175: SAT-based Program Synthesis - GitHub Pages

Conflict-Driven Synthesis

Page 176: SAT-based Program Synthesis - GitHub Pages

Conflict-Driven Synthesis

Search

N0 ?

Page 177: SAT-based Program Synthesis - GitHub Pages

Search

Partial program

Conflict-Driven SynthesisN0 head

N1 ?

Page 178: SAT-based Program Synthesis - GitHub Pages

Search

Partial program

Conflict-Driven SynthesisN0 head

N1 ?

Check Conflict

Page 179: SAT-based Program Synthesis - GitHub Pages

Search

Partial program

Conflict-Driven Synthesis

Check Conflict

No conflict

N0 head

N1 ?

Page 180: SAT-based Program Synthesis - GitHub Pages

Search

Partial program

Conflict-Driven Synthesis

Check Conflict

No conflict

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

Page 181: SAT-based Program Synthesis - GitHub Pages

Search

Partial program

Conflict-Driven Synthesis

Check Conflict

No conflict

N0 head

take

filter ?

x1

N1

N2 N3

?N4 N5

Conflict

Learn from Conflict

Page 182: SAT-based Program Synthesis - GitHub Pages

Search

Partial program

Conflict-Driven Synthesis

Check Conflict

No conflict

N0 head

filter ?

x1

N1

N2 N3

?N4 N5

Conflict

Learn from Conflict Learn:

• take ≡ sort ≡ reverse ≡ filter

take

Learning

Page 183: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

Conflict

No conflict

Partial program

Learn from Conflict

Conflict-Driven Synthesis

Learning

Page 184: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

Conflict

No conflict

Partial program

No solutionLearn from

Conflict

Conflict-Driven Synthesis

Learning

Page 185: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

Conflict

No conflict

Partial program

No solutionLearn from

Conflict

Conflict-Driven Synthesis

Synthesized Program

N0 sum

take

reverse x2

sort

x1

N1

N2 N3

N4

N5

Learning

Page 186: SAT-based Program Synthesis - GitHub Pages

Experimental EvaluationDeepCoder (Microsoft Research): • List manipulation synthesizer • Uses deep learning to guide the search • We reimplemented DeepCoder statistical model in

our Conflict-Driven Synthesis Framework

Benchmarks:• 100 challenging benchmarks described in

DeepCoder’s paper

Page 187: SAT-based Program Synthesis - GitHub Pages

Neo vs DeepCoder

Page 188: SAT-based Program Synthesis - GitHub Pages

Perc

enta

ge o

f Sol

ved

Benc

hmar

ks

0

20

40

60

80

Time (seconds)

10 60 110 160 210 260 300

Enumeration DeepCoder (Machine Learning)ML + Deduction Neo (ML + Deduction + Logical Learning)

Neo vs DeepCoder

Page 189: SAT-based Program Synthesis - GitHub Pages

Perc

enta

ge o

f Sol

ved

Benc

hmar

ks

0

20

40

60

80

Time (seconds)

10 60 110 160 210 260 300

Enumeration DeepCoder (Machine Learning)ML + Deduction Neo (ML + Deduction + Logical Learning)

Neo vs DeepCoder

Page 190: SAT-based Program Synthesis - GitHub Pages

Perc

enta

ge o

f Sol

ved

Benc

hmar

ks

0

20

40

60

80

Time (seconds)

10 60 110 160 210 260 300

Enumeration DeepCoder (Machine Learning)ML + Deduction Neo (ML + Deduction + Logical Learning)

Neo vs DeepCoder

Page 191: SAT-based Program Synthesis - GitHub Pages

Perc

enta

ge o

f Sol

ved

Benc

hmar

ks

0

20

40

60

80

Time (seconds)

10 60 110 160 210 260 300

Enumeration DeepCoder (Machine Learning)ML + Deduction Neo (ML + Deduction + Logical Learning)

Neo vs DeepCoder

Page 192: SAT-based Program Synthesis - GitHub Pages

Perc

enta

ge o

f Sol

ved

Benc

hmar

ks

0

20

40

60

80

Time (seconds)

10 60 110 160 210 260 300

Enumeration DeepCoder (Machine Learning)ML + Deduction Neo (ML + Deduction + Logical Learning)

Neo vs DeepCoder

Page 193: SAT-based Program Synthesis - GitHub Pages

Perc

enta

ge o

f Sol

ved

Benc

hmar

ks

0

20

40

60

80

Time (seconds)

10 60 110 160 210 260 300

Enumeration DeepCoder (Machine Learning)ML + Deduction Neo (ML + Deduction + Logical Learning)

Neo vs DeepCoder

Good news:• Speedup over 15x for some benchmarks • Problems that took 45 minutes can now be

solved in 3 minutes!

Page 194: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

ConflictLearning

No conflict

Partial program

Synthesized Program Learn from

Conflict

Neo: Conflict-Driven Synthesis

Page 195: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

ConflictLearning

No conflict

Partial program

Synthesized Program Learn from

Conflict

Neo: Conflict-Driven SynthesisMachine Learning

Page 196: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

ConflictLearning

No conflict

Partial program

Synthesized Program Learn from

Conflict

Neo: Conflict-Driven SynthesisMachine Learning

Logic Deduction

Page 197: SAT-based Program Synthesis - GitHub Pages

Search Check Conflict

ConflictLearning

No conflict

Partial program

Synthesized Program Learn from

Conflict

Neo: Conflict-Driven SynthesisMachine Learning

Logic Deduction

Learning

Page 198: SAT-based Program Synthesis - GitHub Pages

Scalability Through Learning

Page 199: SAT-based Program Synthesis - GitHub Pages

Scalability of Program Synthesis• SAT Solving:

•<1996: SAT solving was seen as intractable! •1996-now: Conflict-Driven Clause Learning SAT solvers revolutionized the field!

Page 200: SAT-based Program Synthesis - GitHub Pages

• Program Synthesis:•First step towards learning from mistakes

• SAT Solving:•<1996: SAT solving was seen as intractable! •1996-now: Conflict-Driven Clause Learning SAT solvers revolutionized the field!

Scalability of Program Synthesis

Page 201: SAT-based Program Synthesis - GitHub Pages

• Program Synthesis:•First step towards learning from mistakes

• SAT Solving:•<1996: SAT solving was seen as intractable! •1996-now: Conflict-Driven Clause Learning SAT solvers revolutionized the field!

•Can learning push the boundaries of program synthesis?

Scalability of Program Synthesis

Page 202: SAT-based Program Synthesis - GitHub Pages

Applications of Program Synthesis

Data Science Databases Program Repair

Security Computer-Aided Education

And many others!