ScalaZ3 Integrating SMT and Programming

Slides:



Advertisements
Similar presentations
Satisfiability Modulo Computable Functions Philippe Suter, Ali Sinan Köksal, and Viktor Kuncak ÉCOLE POLYTECHNIQUE FÉDÉRALE DE LAUSANNE, SWITZERLAND Photo:
Advertisements

1. Scala 2. Traffic DSL in Scala 3. Comparison AToM3 4. Conclusion & Future work.
Satisfiability Modulo Theories and Network Verification Nikolaj Bjørner Microsoft Research Formal Methods and Networks Summer School Ithaca, June
Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
Using SMT solvers for program analysis Shaz Qadeer Research in Software Engineering Microsoft Research.
Linear real and integer arithmetic. Fixed-size bit-vectors Uninterpreted functions Extensional arrays Quantifiers Model generation Several input formats.
Satisfiability Modulo Theories (An introduction)
Verification of Functional Programs in Scala Philippe Suter (joint work w/ Ali Sinan Köksal and Viktor Kuncak) ÉCOLE POLYTECHNIQUE FÉDÉRALE DE LAUSANNE,
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Functional Design and Programming Lecture 9: Lexical analysis and parsing.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Nikolaj Bjørner Leonardo de Moura Nikolai Tillmann Microsoft Research August 11’th 2008.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
Distributed Programming in Scala with APGAS Philippe Suter, Olivier Tardieu, Josh Milthorpe IBM Research Picture by Simon Greig.
F# Shiva Srivastava David He Peter Bingel. Overview F# (pronounced "F sharp") is a functional and object oriented programming language for the Microsoft.NET.
Computer Science 210 Computer Organization Introduction to C.
Owl Andrew Olowude Ben Klingher Belai Lencho Kenneth Cheng Martin Li A language for finite automata Team 20 _________ /_ ___ \ \ \ \__/\___/ / \_\/______/
UNIVERSITI TENAGA NASIONAL “Generates Professionals” CHAPTER 4 : Part 2 INTRODUCTION TO SOFTWARE DEVELOPMENT: PROGRAMMING & LANGUAGES.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
A brief introduction to 11ACM 朱旻申. Outline What is Groovy ? Differences between Groovy & Java About closure & Dagger Methods.
By: Saurabh Dixit.  Groovy server pages  Taglibs  Validators in grails.
Scala Overview Brandon Clopton CSCI 431. Scala fuses object-oriented and functional programming in a statically typed programming language. It is aimed.
Exceptions Handling Exceptionally Sticky Problems.
Synthesis, Analysis, and Verification Lecture 01c Lectures: Viktor Kuncak Exercises and Labs: Eva Darulová Giuliano Losa About Synthesis General Background.
- Neeraj Chandra.  It’s language written by by Martin Odersky at EPFL  It’s language written by by Martin Odersky at EPFL (École Polytechnique Fédérale.
CS 363 Comparative Programming Languages Semantics.
Logistics, Exercises and Demos Everyone should register for the course ‘SAV’ in whatAFunCourse Please obtain “The Calculus of Computation”
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
1 JTransformer Framework Tobias Windeln Institut für Informatik III Rheinische Friedrich-Wilhelms-Universität Bonn.
Chapters 2 & 3. .NET Software development model that allows applications created in disparate programming languages to communicate Universal data access.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
More on Type Checking. Conversion and Coercion Int C; A = C;
Basic Introduction to Lisp
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
C is a high level language (HLL)
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Phillip Trelford Blog:
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Open Source Compiler Construction (for the JVM)
Lesson 06: Functions Class Participation: Class Chat:
Computer Science 210 Computer Organization
Satisfiability Modulo Theories
Chapter 3 Context-Free Grammar and Parsing
Lambda Expressions By Val Feldsher.
Moonzoo Kim CS Dept. KAIST
Moonzoo Kim CS Dept. KAIST
Rich Model Toolkit An Infrastructure for Reliable Computer Systems
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Computer Science 210 Computer Organization
مفاهیم بهره وري.
Easy Ada tooling with Libadalang Raphaël Amiard.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Easy Ada tooling with Libadalang Raphaël Amiard
Syntax-Directed Translation
Lesson 06: Functions Class Chat: Attendance: Participation
CSC1018F: Intermediate Python
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CSE 341 Lecture 11 b closures; scoping rules
Rudra Dutta CSC Spring 2007, Section 001
Rich Model Toolkit – An Infrastructure for Reliable Computer Systems
Lecture 2 - Names & Functions
Presentation transcript:

ScalaZ3 Integrating SMT and Programming Ali Sinan Köksal, Viktor Kuncak, Philippe Suter École Polytechnique Fédérale de Lausanne

Efficient SMT solver from Microsoft Research Supports many theories through DPLL(T) and Nelson-Oppen combination SMT-LIB standard input format, as well as C, .NET, OCaml, and Python bindings “Scalable programming language” Blending of functional and object-oriented programming Runs on the Java Virtual Machine (and .NET) Rich type system (generics, type classes, implicit conversions, etc.) Now used by over 100’000 developers (incl. Twitter, UBS, LinkedIn) - Z3 just won most of the SMT-COMP divisions

~$ ./demo I’m going to start by showing how it feels to use the system One part of Scala^Z3 mirrors the functions in the C API. We will come back to that later, for now I will focus on some of the things that we added as part of the integration.

C and Scala side-by-side #include "z3.h" … Z3_config cfg = Z3_mk_config(); Z3_set_param_value(cfg, "MODEL", "true"); Z3_context z3 = Z3_mk_context(cfg); Z3_sort intSort = Z3_mk_int_sort(z3); Z3_func_decl f = Z3_mk_func_decl(z3, Z3_mk_string_symbol(z3,"f"), 1, &intSort, intSort); Z3_ast x = Z3_mk_const(z3, Z3_mk_string_symbol(z3,"x"), intSort); Z3_ast y = Z3_mk_const(z3, Z3_mk_string_symbol(z3,"y"), intSort); Z3_ast ineq = Z3_mk_not(z3, Z3_mk_eq(z3, x, Z3_mk_app(z3, f, 1, &y))); Z3_assert_cnstr(z3, ineq); Z3_model m; if(Z3_check_and_get_model(z3, &m)) { printf("%s", Z3_model_to_string(z3, m)); } import z3.scala._ … val cfg = new Z3Config cfg.setParamValue("MODEL", "true") val z3 = new Z3Context(cfg) val intSort : Z3Sort = z3.mkIntSort val f : Z3FuncDecl = z3.mkFuncDecl( z3.mkStringSymbol("f"), List(intSort), intSort) val x : Z3AST = z3.mkConst( z3.mkStringSymbol("x"), intSort) val y : Z3AST = z3.mkConst( z3.mkStringSymbol("y"), intSort) val ineq : Z3AST = z3.mkNot( z3.mkEq(x, z3.mkApp(f, y))) z3.assertCnstr(ineq) z3.checkAndGetModel match { case (Some(true), m) ⇒ println(m) case _ ⇒ ; } One thing this tool is: a mapping of the C functions into Scala. Almost one-to-one correspondence; uses object-oriented style and syntax standards differ

def choose[A,B](p: (Val[A],Val[B])⇒Tree[BoolSort]) : (A,B) def find[A,B](p: (Val[A],Val[B])⇒Tree[BoolSort]) : Option[(A,B)] def findAll[A,B](p: (Val[A],Val[B])⇒Tree[BoolSort]) : Iterator[(A,B)]

Anatomy of an Inline Invocation import z3.scala._ import dsl._ ... choose((y: Val[Int⇒Int], x: Val[Int], y: Val[Int]) ⇒ !(x === f(y))) imports and Val[_]s only manifestations of the library Desired return types (actual Scala types). Domain specific language of formulas resembles Scala expressions. Returned values are Scala values (including functions).

for Comprehensions Can you find positive x, y, z such that 2x + 3y ≤ 40, xz = 3y2, and y is prime? for((x,y) ← findAll((x: Val[Int], y: Val[Int]) ⇒ x > 0 && y > x && x * 2 + y * 3 <= 40); if(isPrime(y));   z ← findAll((z: Val[Int]) ⇒ z * x === 3 * y * y))     yield (x, y, z) Generators: sequences, computed eagerly or lazily. Filter: arbitrary boolean expression. Returned expression. (1,2,12), (1,3,27), (1,5,75), (1,7,147), (1,11,363), (3,11,121), (3,5,25), (3,7,49)

Implementation Aspects

Automatic conversions between the two kinds. Bottom Top IntSort BoolSort … SetSort Basic representation: class Z3AST(ptr : Long) Tree hierarchy as part of the DSL: abstract class Tree[+T >: Bottom <: Top] { ... def <(other : Tree[_ <: IntSort]) : Tree[BoolSort] = ... } implicit def ast2Tree(ast : Z3AST) : Tree[Bottom] implicit def tree2AST(tree : Tree[_]) : Z3AST Two kinds of trees: the basic kind, and the typed kind. Conversions between the two is automatic. The typed kind of trees will catch some compile-time errors. Automatic conversions between the two kinds. Soft typing for the DSL Trees.

DSL in Action import z3.scala._ … val cfg = new Z3Config cfg.setParamValue("MODEL", "true") val z3 = new Z3Context(cfg) val intSort : Z3Sort = z3.mkIntSort val f : Z3FuncDecl = z3.mkFuncDecl( z3.mkStringSymbol("f"), List(intSort), intSort) val x : Z3AST = z3.mkConst( z3.mkStringSymbol("x"), intSort) val y : Z3AST = z3.mkConst( z3.mkStringSymbol("y"), intSort) val ineq : Z3AST = z3.mkNot( z3.mkEq(x, z3.mkApp(f, y))) import z3.scala._ … val z3 = new Z3Context("MODEL“ -> true) val intSort = z3.mkIntSort val f = z3.mkFuncDecl( "f", intSort, intSort) val x = z3.mkConst("x", intSort) val y = z3.mkConst("y", intSort) val ineq : Z3AST = !(x === f(y)) One thing this tool is: a mapping of the C functions into Scala. Almost one-to-one correspondence; uses object-oriented style and syntax standards differ

Z3 Sorts Int BV[32] Bool Array[A,B] … Scala Types Int Boolean Set[A] A⇒B … Different function calls to create constants and to retrieve the models: Users should be able to ignore the underlying representation: ctx.getBool(m.eval(tree)) ctx.getNumeralInt(m.eval(tree)) m.getArrayValue(tree)... m.evalAs[Boolean](tree) m.evalAs[Int](tree) m.evalAs[Int⇒Int](tree)

def evalAs[T : Evaluator](tree : Z3AST) : T def evalAs[T](tree : Z3AST) : T def evalAs[T](tree : Z3AST)(implicit e : Evaluator[T]) : T @implicitNotFound(“No known model evaluator for type ${T}.”) trait Evaluator[T] { def eval(model : Z3Model, tree : Z3AST) : T } implicit object IntEvaluator extends Evaluator[Int] { def eval(model : Z3Model, tree : Z3AST) : Int = ... } implicit def lift2Set[T : Evaluator] = new Evaluator[Set[T]] { def eval(model : Z3Model, tree : Z3AST) : Set[T] = ... } In fact, the signature is slightly more complicated. That : Evaluator reads as “evalAs is defined for any type T such that T has an Evaluator” It’s short for the long form with implicit So what is an evaluator? It’s something we define as part of the library: simply an interface parametrized by a type. The interface says “I know how to extract a value of type T from a Z3Model” Now what we do is that we define evaluators for the types we know how to handle. The implicit keywords mean that we don-t actually need to pass the argument: the Scala compiler fills them in by scanning for an implicit value of the right type. Hence, when we write, it automatically is expanded into … we injected type-dependent code, but the user doesn’t have to know! Now, I could define Evaluators for Ints, Bools, etc. What about type constructors? We can handle them too! Now, instead of one Evaluator, we provide a rule to build an Evaluator from another one. This reallys reads as Prolog: if T has an evaluator, then Set[T] has one too, given as follows. m.evalAs[Int](t) m.evalAs[Int](t)(IntEvaluator) m.evalAs[Set[Set[Int]]](t) m.evalAs[...](t)(lift2Set(lift2Set(IntEvaluator)))

Procedural Attachments val z3 = new Z3Context(“MODEL” -> true) val stringTheory = new ProceduralAttachment[String](z3) { val concat = function((s1,s2) ⇒ s1 + s2)   val substr = predicate((s1,s2) ⇒ s2.contains(s1))   val evenLength = predicate(_.length % 2 == 0) } import stringTheory._ val s = z3.mkConst(“s”, stringTheory.sort) z3.assertCnstr(s === “world” || s === “moon”) z3.assertCnstr(evenLength(s)) z3.check > Some(true) z3.assertCnstr(substr(concat(“hello”, s), “low”)) > Some(false) So far, we’ve seen how to communicate mostly from Scala to Z3.

Applications MUNCH: Decision procedure for multisets and sets R. Piskac, V. Kuncak, IJCAR 2010 Z3 extension for sets with cardinality constraints P. Suter, R. Steiger, V. Kuncak, VMCAI 2011 Leon: Verifier for functional programs P. Suter, A.S. Köksal, V. Kuncak, SAS 2011, http://lara.epfl.ch/leon/ Kaplan: Constraint programming in Scala Ali Sinan Köksal’s Master Thesis Other users at ETH Zürich, KU Leuven

(Tested on Windows and Linux, on 32 and 64 bit architectures.) Availability http://lara.epfl.ch/w/ScalaZ3 https://github.com/psuter/ScalaZ3 - Note that it’s probably also a good starting point for a Java wrapper. (Tested on Windows and Linux, on 32 and 64 bit architectures.)

Thank you.