synthesis with the sketch system d ay 2 armando solar-lezama
TRANSCRIPT
Synthesis with the Sketch System
DAY 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?
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
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}
Insert your favorite checker here
CEGIS
{𝒊𝒏𝒊}
∃𝒄 𝒔 .𝒕 .𝑪𝒐𝒓𝒓𝒆𝒄𝒕 (𝑷 𝒄 , 𝒊𝒏𝒊) ∃ 𝒊𝒏𝒔 .𝒕 .¬𝑪𝒐𝒓𝒓𝒆𝒄𝒕 (𝑷𝒄 , 𝒊𝒏𝒊)
Synthesize Check𝒄
𝒊𝒏
•Constraints for each follow from semantics
Loops handled through simple unrolling
Insert your favorite checker here
CEGIS
{𝒊𝒏𝒊}
∃𝒄 𝒔 .𝒕 .𝑪𝒐𝒓𝒓𝒆𝒄𝒕 (𝑷 𝒄 , 𝒊𝒏𝒊)
Synthesize Check𝒄
𝒊𝒏
CEGIS in Detail
𝑸 (𝒄 ,𝒊𝒏)+
+
+
b
+
+
++
a c d
A
+
+
+ ++
A A A
{𝒊𝒏𝒊}
SynthesizeCheck𝒄
𝒊𝒏
CEGIS in Detail
𝑸𝟎(𝒄 , 𝒊𝒏)𝑸𝟎 (𝒄 , 𝒊𝒏 )⇒𝑸𝟏(𝒄 , 𝒊𝒏)𝑸𝟏(𝒄 , 𝒊𝒏)𝑸𝟏 (𝒄 , 𝒊𝒏)⇒𝑸𝟐(𝒄 , 𝒊𝒏)𝑸𝟐(𝒄 , 𝒊𝒏)
+
+
+
b
+
+
++
a c d
A
+
+
+ ++
A A A
{𝒊𝒏𝒊}
SynthesizeCheck𝒄
𝒊𝒏
Bits
2
3
45
GENERATORS
User defined generators
•Mechanism to define sets of code fragments
•They look like functions But with a few caveats
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);}
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
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;}
Karatsuba Multiplication
•Recursive grade-school multiplication
𝑥=𝑥2∗𝑝𝑛2+𝑥1 𝑦=𝑦2∗𝑝
𝑛2 +𝑦1
𝑥∗ 𝑦=𝑎+𝑏∗𝑝𝑛2+𝑐 ∗𝑝
𝑛2+𝑑∗𝑝𝑛
Karatsuba Multiplication
•Smarter Karatsuba Multiplication
𝑥=𝑥1∗𝑝𝑛2+𝑥2 𝑦=𝑦1∗𝑝
𝑛2+𝑦2
𝑥∗ 𝑦=…