Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Constraint-based Search

Similar presentations


Presentation on theme: "Lecture 7 Constraint-based Search"β€” Presentation transcript:

1 Lecture 7 Constraint-based Search
Xiaokang Qiu

2 Constraint-based search
Key idea1: Search as β€œcurve fitting” β€œcurve” is a parameterized family of functions 𝐻 = 𝑃 𝑐 π‘βˆˆπΆ} Key idea 2: Define a language to describe parameterized programs Key idea 3: β€œSolve” instead of search

3 Synthesis with constraints
Overview of the Sketch language Turning synthesis problems into constraints Efficient constraint solving

4 Language Design Strategy
Extend base language with one construct Constant hole: ?? Synthesizer replaces ?? with a constant High-level constructs defined in terms of ?? int bar (int x) { int t = x * ??; assert t == x + x; return t; } int bar (int x) { int t = x * 2; assert t == x + x; return t; }

5 Integer Generator Sets of Expressions
Expressions with ?? == sets of expressions linear expressions x*?? + y*?? + ?? polynomials x*x*?? + x*?? + ?? sets of variables ?? ? x : y

6 Example: Registerless Swap
Swap two words without an extra temporary int W = 32; void swap(ref bit[W] x, ref bit[W] y){ if(??){ x = x ^ y;}else{ y = x ^ y; } } harness void main(bit[W] x, bit[W] y){ bit[W] tx = x; bit[W] ty = y; swap(x, y); assert x==ty && y == tx;

7 From simple to complex holes
We need to compose ?? to form complex holes Borrow ideas from generative programming Define generators to produce families of functions Use partial evaluation aggressively

8 Generators Look like a function Key feature:
but are partially evaluated into their calling context Key feature: Different invocations οƒ  Different code Can recursively define arbitrary families of programs

9 Sample Generator /** * Generate the set of all bit-vector expressions
* involving +, &, xor and bitwise negation (~). * the bnd param limits the size of the generated expression. */ generator 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) |}; }

10 Example: Least Significant Zero Bit
generator 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){ return gen(x, 3);

11 High order generators /* * Generate code from f n times */
generator void rep(int n, fun f){ if(n>0){ f(); rep(n-1, f); }

12 Closures + High Order Generators
generator void rep(int n, fun f){ if(n>0){ f(); rep(n-1, f); } bit[16] reverseSketch(bit[16] in) { bit[16] t = in; int s = 1; generator void tmp(){ bit[16] m = ??; t = ((t << s)&m )| ((t >> s)&(~m)); s = s*??; } rep(??, tmp); return t;

13 Syntactic Sugar {| RegExp |}
RegExp supports choice β€˜|’ and optional β€˜?’ can be used arbitrarily within an expression to select operands {| (x | y | z) + 1 |} to select operators {| x (+ | -) y |} to select fields {| n(.prev | .next)? |} to select arguments {| foo( x | y, z) |} Set must respect the type system all expressions in the set must type-check all must be of the same type

14 repeat Avoid copying and pasting repeat(n){ s}  s;s;…s;
each of the n copies may resolve to a distinct stmt n can be a hole too. n

15 Example: Reversing bits
pragma options "--bnd-cbits 3 "; int W = 32; bit[W] reverseSketch(bit[W] in) { bit[W] t = in; int s = 1; int r = ??; repeat(??){ bit[W] tmp1 = (t << s); bit[W] tmp2 = (t >> s); t = tmp1 {|} tmp2; // Syntactic sugar for m=??, (tmp1&m | tmp2&~m). s = s*r; } return t;

16 Framing the synthesis problem
Goal: Find a function from holes to values Easy in the absence of generators Finite set of holes so function is just a table bit[W] isolateSk (bit[W] x) implements isolate0 { return !(x + ??1) & (x + ??2) ; }

17 Framing the synthesis problem
Goal: Find a function from holes to values Easy in the absence of generators Finite set of holes so function is just a table bit[W] isolateSk (bit[W] x) implements isolate0 { return !(x + πœ™(??1)) & (x + πœ™(??2)) ; }

18 Framing the synthesis problem
Generators need something more generator bit[W] gen(bit[W] x, int bnd){ assert bnd > 0; if(??1) return x; if(??2) return ??5; if(??3) return ~geng1(x, bnd-1); if(??4){ ... } bit[W] isolate0sk (bit[W] x) implements isolate0 { return geng0(x, 3);

19 Framing the synthesis problem
Generators need something more generator bit[W] gen(bit[W] x, int bnd){ assert bnd > 0; if(πœ™(??1)) return x; if(πœ™(??2)) return πœ™(??5); if(πœ™(??3)) return ~geng1(x, bnd-1); if(πœ™(??4)){ ... } bit[W] isolate0sk (bit[W] x) implements isolate0 { return geng0(x, 3);

20 Framing the synthesis problem
Generators need something more The value of the holes depends on the context generator bit[W] gen(context 𝜏 , bit[W] x, int bnd){ assert bnd > 0; if(πœ™(𝜏,??1)) return x; if(πœ™(𝜏,??2)) return πœ™(𝜏,??5); if(πœ™(𝜏,??3)) return ~geng1(𝝉 β‹… π’ˆ 𝟏 , x, bnd-1); if(πœ™(𝜏,??4)){ ... } bit[W] isolate0sk (bit[W] x) implements isolate0 { return geng0( π’ˆ 𝟎 , x, 3);

21 Framing the synthesis problem
generator bit[W] gen(context 𝜏 , bit[W] x, int bnd){ assert bnd > 0; if(πœ™(𝜏,??1)) return x; if(πœ™(𝜏,??2)) return πœ™(𝜏,??5); if(πœ™(𝜏,??3)) return ~geng1(𝝉 β‹… π’ˆ 𝟏 , x, bnd-1); if(πœ™(𝜏,??4)){ return {| geng2(𝝉 β‹… π’ˆ 𝟐 , x, bnd-1) (+ | & | ^) geng3(𝝉 β‹… π’ˆ πŸ‘ , x, bnd-1) |}; } bit[W] isolate0sk (bit[W] x) implements isolate0 { return geng0( π’ˆ 𝟎 , x, 3); πœ™( 𝑔 0 ,??k) πœ™( 𝑔 0 𝑔 1 ,??k) πœ™( 𝑔 0 𝑔 2 ,??k) πœ™( 𝑔 0 𝑔 1 𝑔 2 ,??k) πœ™( 𝑔 0 𝑔 1 𝑔 3 ,??k) πœ™( 𝑔 0 𝑔 1 𝑔 2 𝑔 1 ,??k) ... Potentially unbounded set of unknowns We can bound the depth of recursion That means again πœ™ is just a table

22 The inductive synthesis problem
βˆƒπ‘ βˆ€π‘–π‘›βˆˆπΈ 𝑄(𝑖𝑛, 𝑐) where E = {x1, x2, …, xk}

23 Overall Strategy Symbolic Execution Simplification Encoding Solver
Sketch with harnesses Symbolic Execution Simplification Encoding Solver

24 Building Constraints

25 A sketch as a constraint system
Ο†(??2)*x + Ο†(??3) Ο†(??4)*x x > Ο†(??1) Ο†(??2)*(x+1) + Ο†(??3) Ο†(??4)*(x+1) x+1 > Ο†(??1) x>=4 x<3 mux x*x - = 1 or >= and int lin(int x){ if(x > Ο†(??1)) return Ο†(??2)*x + Ο†(??3); else return Ο†(??4)*x; } void main(int x){ int t1 = lin(x); int t2 = lin(x+1); if(x<4) assert t1 >= x*x; if(x>=3) assert t2-t1 == 1;

26 int popSketched (bit[W] x) implements pop { repeat(??) { x = (x & ??)
>> & + int popSketched (bit[W] x) implements pop { repeat(??) { x = (x & ??) + ((x >> ??) & ??); } return x; mux x & >> & + mux x & >> & + mux x

27 Ex : Population count. 0010 0110 οƒ  3 int pop (bit[W] x) {
one 1 + int pop (bit[W] x) { int count = 0; for (int i = 0; i < W; i++) { if (x[i]) count++; } return count; mux count + mux count + mux count + mux count

28 Simplification

29 Structural Hashing * * * * + + + + h0 h1 h2 h0 h1 h2 a b h0 h1 h2 a b

30 Structural hashing + rewriting
b < ^ + h0 a ^ < + h0 < ^ h0 a < + ^ h0 a < ^ + h0 < ^ h0 < 𝑋+𝑏<𝑏 →𝑋<0 π‘‹βˆ§π‘‹β†’π‘‹

31 What about Arithmetic? 1) Bit-blast 2) Unary encoding 3) SMT


Download ppt "Lecture 7 Constraint-based Search"

Similar presentations


Ads by Google