Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

15
Synthesis with the Sketch System DAY 2 Armando Solar-Lezama

Transcript of Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

Page 1: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

Synthesis with the Sketch System

DAY 2

Armando Solar-Lezama

Page 2: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

The challenge of synthesis

•For functions, the environment controls the inputs i.e. whatever we synthesize must work for all inputs

•Modeled with a doubly quantified constraint

What does it mean to quantify over programs?

Page 3: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

Quantifying over programs

•Synthesis as curve fitting we want a function that satisfies some properties

•It’s hard to do curve fitting with arbitrary curves Instead, people use parameterized families of curves Quantify over parameters instead of over functions

•A sketch is just a way of describing these families

Page 4: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

4

Insight

Sketches are not arbitrary constraint systems They express the high level structure of a program

A small number of inputs can be enough focus on corner cases

This is an inductive synthesis problem !

∃𝑐 ∀ 𝑖𝑛∈𝐸𝑄(𝑖𝑛 ,𝑐 )where E = {x1, x2, …, xk}

Page 5: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

Insert your favorite checker here

CEGIS

{𝒊𝒏𝒊}

∃𝒄 𝒔 .𝒕 .𝑪𝒐𝒓𝒓𝒆𝒄𝒕 (𝑷 𝒄 , 𝒊𝒏𝒊) ∃ 𝒊𝒏𝒔 .𝒕 .¬𝑪𝒐𝒓𝒓𝒆𝒄𝒕 (𝑷𝒄 , 𝒊𝒏𝒊)

Synthesize Check𝒄

𝒊𝒏

Page 6: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

•Constraints for each follow from semantics

Loops handled through simple unrolling

Insert your favorite checker here

CEGIS

{𝒊𝒏𝒊}

∃𝒄 𝒔 .𝒕 .𝑪𝒐𝒓𝒓𝒆𝒄𝒕 (𝑷 𝒄 , 𝒊𝒏𝒊)

Synthesize Check𝒄

𝒊𝒏

Page 7: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

CEGIS in Detail

𝑸 (𝒄 ,𝒊𝒏)+

+

+

b

+

+

++

a c d

A

+

+

+ ++

A A A

{𝒊𝒏𝒊}

SynthesizeCheck𝒄

𝒊𝒏

Page 8: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

CEGIS in Detail

𝑸𝟎(𝒄 , 𝒊𝒏)𝑸𝟎 (𝒄 , 𝒊𝒏 )⇒𝑸𝟏(𝒄 , 𝒊𝒏)𝑸𝟏(𝒄 , 𝒊𝒏)𝑸𝟏 (𝒄 , 𝒊𝒏)⇒𝑸𝟐(𝒄 , 𝒊𝒏)𝑸𝟐(𝒄 , 𝒊𝒏)

+

+

+

b

+

+

++

a c d

A

+

+

+ ++

A A A

{𝒊𝒏𝒊}

SynthesizeCheck𝒄

𝒊𝒏

Bits

2

3

45

Page 9: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

GENERATORS

Page 10: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

User defined generators

•Mechanism to define sets of code fragments

•They look like functions But with a few caveats

Page 11: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

Key features of generators

•Different dynamic invocations • different code

•Recursive generators = grammar of expressionsgenerator bit[W] gen(bit[W] x, int bnd){ assert bnd > 0; if(??) return x; if(??) return ??; if(??) return ~gen(x, bnd-1); if(??){ return {| gen(x, bnd-1) (+ | & | ^) gen(x, bnd-1) |}; }}

bit[W] isolate0sk (bit[W] x) implements isolate0 { return gen(x, 3);}

Page 12: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

bit[W] reverseSketch(bit[W] in) implements reverse {

bit[W] t = in; int s = 1; repeat(??){ bit[W] tmp1 = (t << s); bit[W] tmp2 = (t >> s); t = tmp1 {|} tmp2; s = s*??; } return t;}

Gens + Closures = Extensible Language•Redefining Repeat

Page 13: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

Gens + Closures = Extensible Language•Redefining Repeat

generator void rep(int n, fun f){ if(n>0){ f(); rep(n-1, f); } }

bit[W] reverseSketch(bit[W] in) implements reverse {

bit[W] t = in; int s = 1; generator void tmp(){ bit[W] tmp1 = (t << s); bit[W] tmp2 = (t >> s); t = tmp1 {|} tmp2; s = s*??; } rep(??, tmp); return t;}

Page 14: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

Karatsuba Multiplication

•Recursive grade-school multiplication

𝑥=𝑥2∗𝑝𝑛2+𝑥1 𝑦=𝑦2∗𝑝

𝑛2 +𝑦1

𝑥∗ 𝑦=𝑎+𝑏∗𝑝𝑛2+𝑐 ∗𝑝

𝑛2+𝑑∗𝑝𝑛

Page 15: Synthesis with the Sketch System D AY 2 Armando Solar-Lezama.

Karatsuba Multiplication

•Smarter Karatsuba Multiplication

𝑥=𝑥1∗𝑝𝑛2+𝑥2 𝑦=𝑦1∗𝑝

𝑛2+𝑦2

𝑥∗ 𝑦=…