Synthesis using Variable Elimination

Similar documents
Implicit Programming

Software Construction using Executable Constraints

Automating Construction of Provably Correct Software Viktor Kuncak

Functional Synthesis for Linear Arithmetic and Sets

Complete Functional Synthesis

Your Wish is my Command

doi: / Software Synthesis Procedures By Viktor Kuncak, Mikaël Mayer, Ruzica Piskac, and Philippe Suter

Solving Computations. Viktor Kuncak. PhD MIT,

On Interactive Synthesis of Code Snippets

Synthesizing Computable Functions from Relations. Viktor Kuncak. EPFL Laboratory for Automated Reasoning and Analysis

Action Language Verifier, Extended

Laboratory for Automated Reasoning and Analysis

Ruzica Piskac Yale University

Deductive Methods, Bounded Model Checking

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

DISCRETE MATHEMATICS

Numerical Computations and Formal Methods

LTCS Report. Concept Descriptions with Set Constraints and Cardinality Constraints. Franz Baader. LTCS-Report 17-02

Induction and Semantics in Dafny

Finite Model Generation for Isabelle/HOL Using a SAT Solver

Solving Linear Constraints over Real. German Vitaliy Jr. Researcher Glushkov Institute of Cybernetic NAS Ukraine

6. Hoare Logic and Weakest Preconditions

CS 267: Automated Verification. Lecture 13: Bounded Model Checking. Instructor: Tevfik Bultan

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

CSE 20 DISCRETE MATH. Winter

Case by Case. Chapter 3

Program Verification using Templates over Predicate Abstraction. Saurabh Srivastava and Sumit Gulwani

Discrete Optimization. Lecture Notes 2

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Runtime Checking for Program Verification Systems

Lecture 6: Arithmetic and Threshold Circuits

Reducing Clocks in Timed Automata while Preserving Bisimulation

3.7 Denotational Semantics

Data types for mcrl2

Cuts from Proofs: A Complete and Practical Technique for Solving Linear Inequalities over Integers

Programming Languages Third Edition

Lecture Notes on Ints

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

In Our Last Exciting Episode

Small Formulas for Large Programs: On-line Constraint Simplification In Scalable Static Analysis

CS 456 (Fall 2018) Scribe Notes: 2

Verification Overview Testing Theory and Principles Testing in Practice. Verification. Miaoqing Huang University of Arkansas 1 / 80

THEORY OF COMPUTATION

CS671 Parallel Programming in the Many-Core Era

CS 11 Haskell track: lecture 1

CSCI 3155: Homework Assignment 3

Softwaretechnik. Program verification. Software Engineering Albert-Ludwigs-University Freiburg. June 30, 2011

Proofs and Counterexamples for Java Programs

Type Theory meets Effects. Greg Morrisett

Verifying Liveness Properties of ML Programs

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

PLDI 2016 Tutorial Automata-Based String Analysis

Decision Procedures in the Theory of Bit-Vectors

What is a Computer Algorithm?

LOGIC AND DISCRETE MATHEMATICS

V Advanced Data Structures

Towards a Software Model Checker for ML. Naoki Kobayashi Tohoku University

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Absolute C++ Walter Savitch

Inductive Synthesis. Neil Immerman. joint work with Shachar Itzhaky, Sumit Gulwani, and Mooly Sagiv

Algorithmic Semi-algebraic Geometry and its applications. Saugata Basu School of Mathematics & College of Computing Georgia Institute of Technology.

Metaprogramming assignment 3

Programming with Contracts. Juan Pablo Galeotti, Alessandra Gorla Saarland University, Germany

Programming Languages Lecture 15: Recursive Types & Subtyping

CS446: Machine Learning Fall Problem Set 4. Handed Out: October 17, 2013 Due: October 31 th, w T x i w

CSE 20 DISCRETE MATH. Fall

Hoare Logic: Proving Programs Correct

Counting multiplicity over infinite alphabets

Formalization of Incremental Simplex Algorithm by Stepwise Refinement

CSE 331 Midterm Exam 2/13/12

Diagonalization. The cardinality of a finite set is easy to grasp: {1,3,4} = 3. But what about infinite sets?

V Advanced Data Structures

CMSC 330: Organization of Programming Languages. Operational Semantics

An Introduction to Programming and Proving in Agda (incomplete draft)

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Testing and Debugging

Exercise 1: Graph coloring (10 points)

The Pointer Assertion Logic Engine

Appendix Set Notation and Concepts

Functional Programming

Arithmetic and Bitwise Operations on Binary Data

Boolean Representations and Combinatorial Equivalence

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

! Addition! Multiplication! Bigger Example - RSA cryptography

Lectures 20, 21: Axiomatic Semantics

CSE wi Midterm Exam 2/8/18. Name UW ID #

Bounded Model Checking Of C Programs: CBMC Tool Overview

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

Softwaretechnik. Program verification. Albert-Ludwigs-Universität Freiburg. June 28, Softwaretechnik June 28, / 24

CS 320: Concepts of Programming Languages

Integrating verification in programming languages

More Lambda Calculus and Intro to Type Systems

Term Algebras with Length Function and Bounded Quantifier Elimination

Transcription:

Synthesis using Variable Elimination Viktor Kuncak EPF Lausanne http://lara.epfl.ch/w/impro

Implicit Programming at All Levels Opportunities for implicit programming in Development within an IDE InSynth tool Compilation Comfusy and RegSy tools Execution Scala^Z3, UDITA, Kaplan

constraint (relation): P(a,x) between inputs and outputs a. pre(a) P(a,f(a)) precondition: pre(a) function: x = f(a) from inputs to outputs

Compilation in Comfusy def secondstotime(totalseconds: Int) : (Int, Int, Int) = choose((h: Int, m: Int, s: Int) ( h * 3600 + m * 60 + s == totalseconds && h 0 && m 0 && m < 60 && s 0 && s < 60 )) def secondstotime(totalseconds: Int) : (Int, Int, Int) = val t1 =? val t2 = val t3 = val t4 = (t1, t3, t4) Mikaël Mayer Ruzica Piskac Philippe Suter, PLDI 10

Todays Approach:

Equations Use one equation to reduce the number of variables by one: choose((h: Int, m: Int, s: Int) ( h * 3600 + m * 60 + s == totalseconds && h 0 && m 0 && m < 60 && s 0 && s < 60 ))

One equation where no coefficient is 1 choose((x,y) => 10 x + 14 y == a ) Find one possible pair x,y, as a function of a.

Constructive (Extended) Euclid s Algorithm: gcd(10,14) 10 14 10 4 6 4 2 4 2 2 2 0 10 x + 14 y == 10 x + 14 y == a

Result of Synthesis for one Equation choose((x,y) => 10 x + 14 y == a ) assert ( ) val x = val y = check the solution: Change in requirements! Customer also wants: x < y does our solution work for e.g. a=2

Idea: Eliminate Variables choose((x,y) => 10 x + 14 y == a && x < y) Cannot easily eliminate one of the x,y. Eliminate both x,y, get one free - z x = 3/2 a + c 1 z y = -a + c 2 z c 1 =? c 2 =? Find general form, characterize all solutions, parametric solution

Idea: Eliminate Variables choose((x,y) => 10 x + 14 y == a && x < y) x = 3/2 a + c 1 z y = -a + c 2 z 10 x 0 + 14 y 0 + (10 c 1 + 14 c 2 ) == a 10 c 1 + 14 c 2 == 0 c 1 =? c 2 =?

choose((x,y) => 10 x + 14 y == a && x < y) x = 3/2 a + 5 z y = -a - 7 z

I forgot to say, we also wish: 0 < x choose((x,y) => 10 x + 14 y == a && 0 < x && x < y)

Compilation in Comfusy def secondstotime(totalseconds: Int) : (Int, Int, Int) = choose((h: Int, m: Int, s: Int) ( h * 3600 + m * 60 + s == totalseconds && h 0 && m 0 && m < 60 && s 0 && s < 60 )) def secondstotime(totalseconds: Int) : (Int, Int, Int) = val t1 =? val t2 = val t3 = val t4 = (t1, t3, t4) Mikaël Mayer Ruzica Piskac Philippe Suter, PLDI 10

Synthesis Procedure on Example process every equality: take an equality E i, compute a parametric description of the solution set and insert those values in the rest of formula 15 Z totalsecon ds s m h, 0 0 60 1 0 3600 0 1 CODE: <further code will come here> val h = lambda val m = mu val s = (-3600) * lambda + (-60) * mu + totalseconds

Synthesis Procedure by Example process every equality: take an equality E i, compute a parametric description of the solution set and insert those values in the rest of formula 16 Z totalsecon ds s m h, 0 0 60 1 0 3600 0 1 Formula (remain specification): 0 λ, 0 μ, μ 59, 0 totalseconds 3600λ - 60μ, totalseconds 3600λ - 60μ 59

Processing Inequalities process output variables one by one 0 λ, 0 μ, μ 59, 0 totalseconds 3600λ - 60μ, totalseconds 3600λ - 60μ 59 expressing constraints as bounds on μ 0 λ, 0 μ, μ 59, μ (totalseconds 3600λ)/60, (totalseconds 3600λ 59)/60 μ Code: val mu = min(59, (totalseconds -3600* lambda) div 60)

Fourier-Motzkin-Style Elimination 0 λ, 0 μ, μ 59, μ (totalseconds 3600λ)/60, (totalseconds 3600λ 59)/60 μ combine each lower and upper bound 0 λ, 0 59, 0 (totalseconds 3600λ)/60, (totalseconds 3600λ 59)/60 (totalseconds 3600λ)/60, (totalseconds 3600λ 59)/60 59 basic simplifications 0 λ, 60λ totalseconds /60, (totalseconds 59)/60 59 60λ Code: val lambda = totalseconds div 3600 Preconditions: 0 totalseconds

Compilation in Comfusy def secondstotime(totalseconds: Int) : (Int, Int, Int) = choose((h: Int, m: Int, s: Int) ( h * 3600 + m * 60 + s == totalseconds && h 0 && m 0 && m < 60 && s 0 && s < 60 )) def secondstotime(totalseconds: Int) : (Int, Int, Int) = val t1 = totalseconds div 3600 val t2 = totalseconds -3600 * t1 val t3 = t2 div 60 val t4 = totalseconds - 3600 * t1-60 * t3 (t1, t3, t4) Mikaël Mayer Ruzica Piskac Philippe Suter, PLDI 10

Handling of Inequalities Solve for one by one variable: separate inequalities depending on polarity of x: A i α i x β j x B j define values a = max i A i /α i and b = min j B j / β j if b is defined, return x = b else return x = a further continue with the conjunction of all formulas A i /α i B j / β j 20

2y b 3x + a 2x a 4y + b 2y b a 3x 2x 4y + a + b 4y 2b 2a 6x 12y + 3a + 3b (4y 2b 2a) / 6 x (12y + 3a + 3b) / 6 (4y 2b 2a) / 6 (12y + 3a + 3b) / 6 two extra variables: (4y 2b 2a) / 6 l 12y + 3a + 3b = 6 l + k 0 k 5 4y 2b 2a 6 * l 12y + 3a + 3b = 6 l + k 0 k 5 pre: 6 3a + 3b k 4y 2b 2a 12y + 3a + 3b k 5b 5a +k 8y y = (k 5a 5b)/8 21

Generated Code Contains Loops val (x1, y1) = choose(x: Int, y: Int => 2*y b =< 3*x + a && 2*x a =< 4*y + b) val kfound = false for k = 0 to 5 do { val v1 = 3 * a + 3 * b k if (v1 mod 6 == 0) { val alpha = ((k 5 * a 5 * b)/8).ceiling val l = (v1 / 6) + 2 * alpha val y = alpha val kfound = true break } } if (kfound) val x = ((4 * y + a + b)/2).floor else throw new Exception( No solution exists ) 22

NP-Hard Constructs Divisibility combined with inequalities: corresponding to big disjunction in q.e., we will generate a for loop with constant bounds (could be expanded if we wish) Disjunctions Synthesis of a formula computes program and exact precondition of when output exists Given disjunctive normal form, use preconditions to generate if-then-else expressions (try one by one)

Synthesis for Disjunctions

General Form of Synthesized Functions for Presburger Arithmetic choose x such that F(x,a) x = t(a) Result t(a) is expressed in terms of +, -, C*, /C, if Need arithmetic for solving equations Need conditionals for disjunctions in input formula divisibility and inequalities (find a witness meeting bounds and divisibility by constants) t(a) = if P 1 (a) t 1 (a) elseif elseif P n (a) t n (a) else error( No solution exists for input,a)

Methodology QE Synthesis Each quantifier elimination trick we found corresponds to a synthesis trick Find the corresponding terms Key techniques: one point rule immediately gives a term change variables, using a computable function strengthen formula while preserving realizability recursively eliminate variables one-by one Example use transform formula into disjunction strengthen each disjunct using equality apply one-point rule

Synthesis for non-linear arithmetic def decomposeoffset(offset: Int, dimension: Int) : (Int, Int) = choose((x: Int, y: Int) ( offset == x + dimension * y && 0 x && x < dimension )) The predicate becomes linear at run-time Synthesized program must do case analysis on the sign of the input variables Some coefficients are computed at run-time

Generated Code May Contain Loops val (x1, y1) = choose(x: Int, y: Int => 2*y b =< 3*x + a && 2*x a =< 4*y + b) val kfound = false for k = 0 to 5 do { val v1 = 3 * a + 3 * b k if (v1 mod 6 == 0) { val alpha = ((k 5 * a 5 * b)/8).ceiling val l = (v1 / 6) + 2 * alpha val y = alpha val kfound = true break } } if (kfound) val x = ((4 * y + a + b)/2).floor else throw new Exception( No solution exists )

Basic Compile-Time Analysis

Compile-time warnings def secondstotime(totalseconds: Int) : (Int, Int, Int) = choose((h: Int, m: Int, s: Int) ( h * 3600 + m * 60 + s == totalseconds && h 0 && h < 24 && m 0 && m < 60 && s 0 && s < 60 )) Warning: Synthesis predicate is not satisfiable for variable assignment: totalseconds = 86400

Compile-time warnings def secondstotime(totalseconds: Int) : (Int, Int, Int) = choose((h: Int, m: Int, s: Int) ( h * 3600 + m * 60 + s == totalseconds && h 0 && m 0 && m 60 && s 0 && s < 60 )) Warning: Synthesis predicate has multiple solutions for variable assignment: totalseconds = 60 Solution 1: h = 0, m = 0, s = 60 Solution 2: h = 0, m = 1, s = 0

Handling Data Structures

Synthesis for sets def splitbalanced[t](s: Set[T]) : (Set[T], Set[T]) = choose((a: Set[T], b: Set[T]) ( a union b == s && a intersect b == empty && a.size b.size 1 && b.size a.size 1 )) def splitbalanced[t](s: Set[T]) : (Set[T], Set[T]) = val k = ((s.size + 1)/2).floor val t1 = k val t2 = s.size k val s1 = take(t1, s) val s2 = take(t2, s minus s1) (s1, s2) s a b

From Data Structures to Numbers Observation: Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size && a union b == bigset && a intersect b == empty a b bigset 34

From Data Structures to Numbers Observation: Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size && a union b == bigset && a intersect b == empty a b bigset 35

From Data Structures to Numbers Observation: Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size && a union b == bigset && a intersect b == empty a b bigset 36

From Data Structures to Numbers Observation: Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size && a union b == bigset && a intersect b == empty a b New specification: ka = kb bigset 37

From Data Structures to Numbers Observation: Reasoning about collections reduces to reasoning about linear integer arithmetic! a.size == b.size && a union b == bigset && a intersect b == empty a b New specification: ka = kb && ka +kb = bigset bigset 38

Experience with Comfusy Works well for examples we encountered Needed: synthesis for more expressive logics, to handle more examples seems ideal for domain-specific languages Efficient for conjunctions of equations (could be made polynomial) Extends to synthesis with parametric coefficients Extends to logics that reduce to Presburger arithmetic (implemented for BAPA)

Comfusy for Arithmetic Limitations of Comfusy for arithmetic: Naïve handling of disjunctions Blowup in elimination, divisibility constraints Complexity of running synthesized code (from QE): doubly exponential in formula size Not tested on modular arithmetic, or on synthesis with optimization objectives Arbitrary-precision arithmetic with multiple operations generates time-inefficient code Cannot do bitwise operations (not in PA)

Arithmetic Synthesis using Automata

RegSy Synthesis for regular specifications over unbounded domains J. Hamza, B. Jobstmann, V. Kuncak FMCAD 2010

Automata-Based Synthesis Disjunctions not a problem Result not depends on the syntax of input formula Complexity of running synthesized code: ALWAYS LINEAR IN THE NUMBER OF BITS Modular arithmetic and bitwise operators: synthesize bit tricks for unbounded number of bits, uniformly without skeletons Supports quantified constraints including optimization constraints

Expressiveness of Spec Language Non-negative integer constants and variables Boolean operators Linear arithmetic operator Bitwise operators Quantifiers over numbers and bit positions (,,) (, &,!) (, c x) PAbit = Presburger arithmetic with bitwise operators WS1S= weak monadic second-order logic of one successor

010001101010101010101010101101111111110101010101100000000010101010100011010101 Basic Idea View integers as finite (unbounded) bit-streams (binary representation starting with LSB) Specification in WS1S (PAbit) Synthesis approach: Step 1: Compile specification to automaton over combined input/output alphabet (automaton specifying relation) Step 2: Use automaton to generate efficient function from inputs to outputs realizing relation

Inefficient but Correct Method Run over a deterministic automaton over inputs and outputs Becomes run of a non-deterministic if we look only at outputs Simulate all branches until the end of input Successful branches indicate outputs that work

Our Approach: Precompute without losing backward information WS1S spec Synthesis: 1. Det. automaton for spec over joint alphabet 2. Project, determinize, extract lookup table Mona Synthesized program: Automaton + lookup table Input word Execution: 1. Run A on input w and record trace 2. Use table to run backwards and output Output word

Experiments No Example MONA (ms) Syn (ms) A A 512b 1024b 2048b 4096b 1 addition 318 132 4 9 509 995 1967 3978 2 approx 719 670 27 35 470 932 1821 3641 3 company 8291 1306 58 177 608 1312 2391 4930 4 parity 346 108 4 5 336 670 1310 2572 5 mod-6 341 242 23 27 460 917 1765 3567 6 3-weightsmin 26963 640 22 13 438 875 1688 3391 7 4-weights 2707 1537 55 19 458 903 1781 3605 8 smooth-4b 51578 1950 1781 955 637 1271 2505 4942 9 smooth-f-2b 569 331 73 67 531 989 1990 3905 10 smooth-b-2b 569 1241 73 342 169 347 628 1304 Linear scaling observed in length of input 11 6-3n+1 834 1007 233 79 556 953 1882 4022 In 3 seconds solve constraint, minimizing the output; Inputs and outputs are of order 2 4000

Thought: A quantifier not to forget Automatically synthesized and only automatically tested code is great But probably even less likely to be correct Synthesizing programs that are guaranteed to be correct by construction will remain important.

Implicit Programming at All Levels Opportunities for implicit programming in Development within an IDE InSynth tool Compilation Comfusy and RegSy tools Execution Scala^Z3 and UDITA tools requirements def f(x : Int) = { choose y st... } iload_0 iconst_1 call Z3 42

Example all : SequenceInputStream = tool

Code Synthesis inside IDE Find all visible symbols Create clauses: - encode in FOL - assign weights Program point Max steps Resolution algorithm with weights Code snippets

Curry-Howard Correspondence def map[a,b](x : List[T], f : A -> B) : List[B] = {...}

joint work with: Tihomir Gvero, Ruzica Piskac InSynth - Interactive Synthesis of Code Snippets def map[a,b](f:a => B, l:list[a]): List[B] = {... } def stringconcat(lst : List[String]): String = {... }... def printints(intlist:list[int], prn: Int => String): String = Returned value: stringconcat(map (prn, intlist)) Is there a term of given type in given environment? Monorphic: decidable. Polymorphic: undecidable

Features Parametric polymorphism Branching expressions with multiple arguments (not just chains of calls) Mining API usage frequencies but can still synthesize previously unseen code Distance to program point heuristics Filtering based on test-cases

Wrapping Up

Synthesis and Verification Some functionality is best synthesized from specs other: better implemented, verified (and put into library) Currently, no choice must always implement specifications and verification are viewed as overhead Goal: make specifications intrinsic part of programs, with clear benefits to programmers they run! Example: write an assertion, not how to establish it This will help both verifiability: document, not reverse-engineer the invariants productivity: avoid writing messy implementation

Conclusion: Implicit Programming Development within an IDE InSynth tool theorem proving as code completion Compilation Comfusy : decision procedure synthesis procedure Scala implementation for integer arithmetic, BAPA RegSy : solving WS1S constraints Execution UDITA: Java + choice as test generation language Scala^Z3 : invoking Z3 Kaplan: constraint programming in Scala http://lara.epfl.ch/w/impro