Top Banner
SMT solvers in Program Analysis and Verification Nikolaj Bjørner Microsoft Research Lecture 3
76

Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

SMT solvers in Program Analysis and Verification

Nikolaj BjørnerMicrosoft Research

Lecture 3

Page 2: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Overview of the lectures

Day Topics Lab

1 Overview of SMT and applications. SAT solving, Z3

Encoding combinatorial problems with Z3

2 Congruence closure Program exploration with Pex

3 A solver for arithmetic. Encoding arithmetic problems

4 Theory combination. Arrays (part 1)

Arrays

5 Arrays, (part 2) and quantifiers

Build a theory solver on top of Z3

Page 3: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Summary of Day 3

A solver for Arithmetic

Lab: Explore PexRush hour and bounded model checking

Page 4: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

References

Slides for Lecture 5 contains a comprehensive list of references

Default pointer, with pointers to pointers: http://research.microsoft.com/projects/z3

Page 5: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Solvers for linear

arithmetic

Page 6: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Linear arithmetic?Find x, y such that:

For reals:Solution:

For integers:No solution

1 3 6 2x y

1, 03

x y

Page 7: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Linear arithmeticThe set of terms TLA and atoms ALA :

t TLA ::= r x x F r Int/Rational | t + t’ t, t’ TLA

Shorthand: x instead of 1 x

a ALA ::= t t’ t, t’ TLA

| t < t’ | t = t’

Page 8: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Difference LogicConstraints are of the form

x – y 4 y – z 7

Example unsatisfiable constraints:x1 - x2 -3, x2 - x3 1, x3 - x4 -2, x4 - x1 3

Proof: 0 = (x1 - x2)+(x2 - x3)+(x3 - x4)+(x4 - x1)= -3 + 1 – 2 + 3 = -1

Page 9: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Difference Logic

Graph interpretation:

Variables are nodes.Atoms x – y c are weighted edges A set of literals is satisfiable iff there is no negative cycle:

where C := c1 + c2 + c3 + c4 < 0. A negative cycle implies a contradiction 0 C < 0.

31 2 4

1 2 3 4 1

cc c c

x x x x x

c

x y

Page 10: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Difference logicHow to find negative cycles?

Bellman-Ford style algorithm O(nm), where

n – # vertices, m – # edges.

Floyd-Warshall O(n3), works OK when m n2for : cost[x,y] := c, else cost[x,y] =

for x V: for y V: for z V: cost[x,y] := min(cost[x,y], cost[x,z] + cost[z,y])

Check that x . cost[x,x] 0

c

x y

Page 11: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Difference Logic – strict inequalities

What about (x – y c) (y – x < -c)?

x, y integers: (y – x < -c) (y – x -c-1) x,y reals: Use infinitesimals .

(y – x < -c) (y – x -c- )Formally, constants are pairs c,c’

with interpretation c + c’

Sample negative cycle:

c

x y

3 3

x y x

Page 12: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

General Linear Arithmetic

Not all linear arithmetic uses only two variables per inequality

Page 13: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Two approaches to arithmeticFourier-Motzkin:

Quantifier elimination procedurex (t ax t’ bx cx t’’) ct at’ ct’ bt’’

Polynomial for difference logic.Generally: exponential space, doubly exponential time.

Simplex:Worst-case exponential, butTime-tried practical efficiency.Linear space

Page 14: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Fast Linear arithmetic

Simplex general formPre-processing stepAlgorithm based on Dual SimplexEfficient backtrackingEfficient Theory propagation

The following material is from:Dutetre & de Moura CAV 2006

Page 15: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Fast Linear arithmetic: General Form

General form: Ax = s, lj xj , sj uj Example:

Only bounds (e.g., s1 0 ) are asserted during search.

1 2

1 2 1 2

0 ( 2 2 6) ( 2 2 4)

( 2 )

( 0 ( 2 6) ( 2 4))

x x y x y x y x y

s x y s x y

x s s s s

Page 16: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Fast Linear arithmetic - Search

Tableau Ax = s is built duringpre-processingBounds are asserted and un-asserted during search

1 2

1 2 1 2

0 ( 2 2 6) ( 2 2 4)

( 2 )

( 0 ( 2 6) ( 2 4))

x x y x y x y x y

s x y s x y

x s s s s

Page 17: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Fast Linear arithmetic - Search

Tableau Ax = s is built duringpre-processingBounds are asserted and un-asserted during search

Backtracking should be cheap – and it is:

Let (x1) = 0, (x2) = -1, (s2) = 0 satisfies x1 4 , x2 -1, -3 s2 then satisfies x1 4 , x2 -1

Page 18: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Fast Linear arithmetic – Dual Simplex

Tableau Ax = s from pre-processing. Initial assignment , where (xi) = 0, (si) = 0 satisfies tableau.

What do we do when bounds are asserted?

We pivot.

Page 19: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Fast Linear arithmetic – Dual Simplex

Terminology:Ax = sThe s variables are basic. The x variables are non-basic.

Invariant on Ax = s, :For non-basic variables: lj (xj) uj The role of pivoting: also satisfy bounds on non-basic variables.

Page 20: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Fast Linear arithmetic – Dual Simplex

New bound xj u is asserted.Set (xj) min((xj), u)Check each row with xj if bounds on non-basic variables are satisfied.

If not, pivot and update

Page 21: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAsserting

Assignment EquationsBounds

0

0

0

0

0

x

y

s

u

v

2

s x y

u x y

v x y

, 0

( 1 2),( 2 0

1

),( 2 1)

x

y v v

s

v v u

1s

Page 22: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAsserting assignment does not satisfy bounds

Assignment EquationsBounds 0

0

0

0

0

y

v

s

x

u

2

s x y

u x y

v x y

, 0

( 1 2),( 2 0

1

),( 2 1)

x

y v v

s

v v u

1s

1s

Page 23: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAsserting pivot s and x (s is a dependent variable)

Assignment EquationsBounds 0

0

0

0

0

y

v

s

x

u

2

y

u x y

v x y

s x

, 0

( 1 2),( 2 0

1

),( 2 1)

x

y v v

s

v v u

1s

1s

Page 24: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAsserting pivot s and x (s is a dependent variable)

Assignment EquationsBounds 0

0

0

0

0

y

v

s

x

u

2

y

u x y

v x y

x s

, 0

( 1 2),( 2 0

1

),( 2 1)

x

y v v

s

v v u

1s

1s

Page 25: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAsserting pivot s and x (s is a dependent variable)

Assignment EquationsBounds 0

0

0

0

0

y

v

s

x

u

2

x s

s y

s

y

u

v y

, 0

( 1 2),( 2 0

1

),( 2 1)

x

y v v

s

v v u

1s

1s

Page 26: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAsserting update dependent variable assignment

Assignment EquationsBounds 0

0

0

0

1

y

v

s

x

u

2

x s

s y

s

y

u

v y

, 0

( 1 2),( 2 0

1

),( 2 1)

x

y v v

s

v v u

1s

1s

Page 27: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAsserting update dependent variable assignment

Assignment EquationsBounds

0

1

1

1

1

x

s

v

y

u

2

x s

s y

s

y

u

v y

, 0

( 1 2),( 2 0

1

),( 2 1)

x

y v v

s

v v u

1s

1s

Page 28: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAsserting

Assignment EquationsBounds

1

0

1

1

1

x

y

s

u

v

2

x s y

u s y

v s y

,

( 1 2),(

1 0

2 0),( 2 1)y v v u

s x

v v

0x

1

0

s

x

Page 29: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAsserting assignment satisfies new bound

Assignment EquationsBounds

1

0

1

1

1

x

y

s

u

v

2

x s y

u s y

v s y

( 1 2),( 2 0

1

),

0

( 2 )

,

1

s x

y v v v v u

0x

1

0

s

x

Page 30: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleCase split

Assignment EquationsBounds

1

0

1

1

1

x

y

s

u

v

2

x s y

u s y

v s y

1, 0

( 2),( 2 0)1 ,( 2 1)

s x

v vy v v u

( 1)y

1

1

0

y

s

x

Page 31: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleCase split bounds do not satisfy assignment

Assignment EquationsBounds

0

1

1

1

1

x

s

v

y

u

2

x s y

u s y

v s y

( 2),( 2 0),

1,

21 )

0

( 1v v

s

y v v u

x

( 1)y

1

1

0

y

s

x

Page 32: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleCase split update assignment

Assignment EquationsBounds

1

1

1

1

1

x

s

y

u

v

2

x s y

u s y

v s y

( 2),( 2 0),

1,

21 )

0

( 1v v

s

y v v u

x

( 1)y

1

1

0

y

s

x

Page 33: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleCase split update dependent assignment

Assignment EquationsBounds

1

1

2

1 2

x

y

u

v

s

2

x s y

u s y

v s y

1, 0

( 2),( 2 0)1 ,( 2 1)

s x

v vy v v u

( 1)y

1

1

0

y

s

x

Page 34: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation

Assignment EquationsBounds

1

1

2

1 2

x

y

u

v

s

2

x s y

u s y

v s y

( 2),( 2 0),

1,

21 )

0

( 1v v

s

y v v u

x

1

1

0

y

s

x

Page 35: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation pivot x and s (x is a dependent variable)

Assignment EquationsBounds 1

1

2

1 2

x

y

s

u

v

2

y

u s y

v s

x s

y

1, 0

( 2),( 2 0)1 ,( 2 1)

s x

v vy v v u

0

1

1y

x

s

Page 36: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation pivot x and s (x is a dependent variable)

Assignment EquationsBounds 1

1

2

1 2

x

y

s

u

v

2

y

u s y

v s

x s

y

( 2),( 2 0),

1,

21 )

0

( 1v v

s

y v v u

x

0

1

1y

x

s

Page 37: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation pivot x and s (x is a dependent variable)

Assignment EquationsBounds 1

1

2

1 2

x

y

s

u

v

2

y

u

y

x

s

v s

s

y

( 2),( 2 0),

1,

21 )

0

( 1v v

s

y v v u

x

0

1

1y

x

s

Page 38: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation pivot x and s (x is a dependent variable)

Assignment EquationsBounds 1

1

2

1 2

x

y

s

u

v

2

y

u

x

y

v

s

x

x y

( 2),( 2 0),

1,

21 )

0

( 1v v

s

y v v u

x

0

1

1y

x

s

Page 39: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation update assignment

Assignment EquationsBounds

1

0

1

2 2

1

x

y

s

u

v

2

y

u

x

y

v

s

x

x y

1, 0

( 2),( 2 0)1 ,( 2 1)

s x

v vy v v u

0

1

1y

x

s

Page 40: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleTheory propagation:

Assignment EquationsBounds

1

0

1

2 2

1

x

y

s

u

v

2

y

u

x

y

v

s

x

x y

1, 0

( 2),( 2 0)1 ,( 2 1)

s x

v vy v v u

1

2

1

0

s

x

y

u

0, 1 2x y u

Page 41: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleTheory propagation:

Assignment EquationsBounds

0

1

1

2 2

1

x

y

s

u

v

2

s x y

u x y

v x y

1, 0

( 2),( 2 0)1 , 2 1( )

s x

v vy uv v

1

0

1

2

s

x

y

u

2 ( 1)u u

Page 42: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBoolean propagation:

Assignment EquationsBounds

2

s x y

u x y

v x y

( ),( 2 0),( 2 1)

1,

21

0

vy

s

v

x

uvv

1

0

1

2

s

x

y

u

( 1) 2y v

0

1

1

2 2

1

x

y

s

u

v

Page 43: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleTheory propagation:

Assignment EquationsBounds

0

1

1

2 2

1

x

y

s

u

v

2

s x y

u x y

v x y

1 2

1, 0

2( ),( 2 0),( )1v u

s

y v

x

vv

1

0

1

2

s

x

y

u

2 ( 2)v v

Page 44: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleConflict

Assignment EquationsBounds

0

1

1

2 2

1

x

y

s

u

v

2

s x y

u x y

v x y

1 2

1, 0

2( ),( 2 0),( )1v u

s

y v

x

vv

1

0

1

2

s

x

y

u

Page 45: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBacktrack

Assignment EquationsBounds

0

1

1

2 2

1

x

y

s

u

v

2

s x y

u x y

v x y

( 2),( 2 0),

1,

21 )

0

( 1v v

s

y v v u

x

1

1

0

y

s

x

Page 46: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAssert y 1 assignment does not satisfy bound

Assignment EquationsBounds

0

1

1

2 2

1

x

y

s

u

v

2

s x y

u x y

v x y

( 2),( 2 0),

1,

21 )

0

( 1v v

s

y v v u

x

1

1

0

y

s

x

Page 47: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAssert y 1 update assignment

Assignment EquationsBounds

1

1

0

2

1

y

s

u

x

v

2

s x y

u x y

v x y

( 2),( 2 0),

1,

21 )

0

( 1v v

s

y v v u

x

1

1

0

y

s

x

Page 48: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleTheory propagation

Assignment EquationsBounds

0

1

1

2

1

x

y

s

u

v

2

s x y

u x y

v x y

0, 1 1x y v

1

1

1

0

s

x

y

v

( 2),( 0),( 11 2

0

2

1,

)v v

s

v uy v

x

Page 49: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBoolean propagation

Assignment EquationsBounds

0

1

1

2

1

x

y

s

u

v

2

s x y

u x y

v x y

( 2) 0v v

1

0

1

0

s

x

y

v

( 2),( ),

1, 0

1 0 ( 22 1)v v

s

v u

x

y v

Page 50: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleAssignment does not satisfy bounds

Assignment EquationsBounds

0

1

1

2

1

x

y

s

u

v

2

s x y

u x y

v x y

1

0

1

0

s

x

y

v

( 2),( ),

1, 0

1 0 2(2 1)v v

s

v u

x

y v

Page 51: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExamplePivot v and x ( v is a dependent variable)

Assignment EquationsBounds

0

1

1

2

1

x

y

s

u

v

2

v x

s x y

u x y

y

1

0

1

0

s

x

y

v

( 2),( 0),( 11 2

0

2

1,

)v v

s

v uy v

x

Page 52: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExamplePivot v and x ( v is a dependent variable)

Assignment EquationsBounds

0

1

1

2

1

x

y

s

u

v

2

3

s v y

u v y

x v y

1

0

1

0

s

x

y

v

( 2),( 0),( 11 2

0

2

1,

)v v

s

v uy v

x

Page 53: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleUpdate assignment to v

Assignment EquationsBounds

0

1

1

2

0

x

y

s

v

u

2

3

s v y

u v y

x v y

1

0

1

0

s

x

y

v

( 2),( 0),( 11 2

0

2

1,

)v v

s

v uy v

x

Page 54: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleUpdate all other assignments

Assignment EquationsBounds

1

1

2

3

0

x

s

y

v

u

2

3

s v y

u v y

x v y

1

0

1

0

s

x

y

v

( 2),( 0),( 11 2

0

2

1,

)v v

s

v uy v

x

Page 55: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBoolean propagation

Assignment EquationsBounds

1

3

1

2

0

x

y

s

v

u

2

3

s v y

u v y

x v y

1

0

1

0

1

s

x

y

v

u

( 2),( 0),

1, 0

1 1(2 2 )v v

s

vy uv

x

( 2) 1v u

Page 56: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation assignment to u does not satisfy bounds.

Assignment EquationsBounds

1

3

1

2

0

x

y

s

v

u

2

3

s v y

u v y

x v y

( 2),( 0),

1, 0

1 1(2 2 )v v

s

vy uv

x

1

0

1

0

1

s

x

y

v

u

Page 57: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation pivot u and v (u is a dependent variable)

Assignment EquationsBounds

1

3

1

2

0

x

y

s

v

u

2

3v u y

s v y

x v y

( 2),( 0),

1, 0

1 1(2 2 )v v

s

vy uv

x

1

0

1

0

1

s

x

y

v

u

Page 58: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation pivot u and v (u is a dependent variable)

Assignment EquationsBounds

1

3

1

2

0

x

y

s

v

u

2

3v

u y

x u

u

s

y

y

( 2),( 0),

1, 0

1 1(2 2 )v v

s

vy uv

x

1

0

1

0

1

s

x

y

v

u

Page 59: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation update assignment

Assignment EquationsBounds

1

1

2

1

0

u

x

y

s

v

2

3v

u y

x u

u

s

y

y

( 2),( 0),

1, 0

1 1(2 2 )v v

s

vy uv

x

1

0

1

0

1

s

x

y

v

u

Page 60: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation update assignment on dependent variables

Assignment EquationsBounds 1

1

0

1

2

x

y

s

v

u

2

3v

u y

x u

u

s

y

y

( 2),( 0),

1, 0

1 1(2 2 )v v

s

vy uv

x

1

0

1

0

1

s

x

y

v

u

Page 61: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation bounds on s are violated

Assignment EquationsBounds

1

1

1

2

0

s

x

y

u

v

3

2

s u y

v u y

x u y

( 2),( 0),

1, 0

1 1(2 2 )v v

s

vy uv

x

1

0

1

0

1

s

x

y

v

u

Page 62: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation pivot s and y (s is a dependent variable)

Assignment EquationsBounds 1

1

1

2

0

s

x

y

u

v

3

2

v

u s

u

x u

y

y

y

( 2),( 0),

1, 0

1 1(2 2 )v v

s

vy uv

x

1

0

1

0

1

s

x

y

v

u

Page 63: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleBound violation update value of s

Assignment EquationsBounds

1

2

1

0

1

y

u

v

s

x

3

2

v

u s

u

x u

y

y

y

( 2),( ),

1, 0

1 0 2(2 1)v v

s

v

x

y v u

1

0

1

0

1

s

x

y

v

u

Page 64: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExamplePropagate pivot to other rows

Assignment EquationsBounds

2 3

2

v u s

x u

y u s

s

( 2),( ),

1, 0

1 0 2(2 1)v v

s

v

x

y v u

1

0

1

0

1

s

x

y

v

u

1

2

1

0

1

y

u

v

s

x

Page 65: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExamplePropagate assignment to dependent variables

Assignment EquationsBounds

2 3

2

v u s

x u

y u s

s

( 2),( ),

1, 0

1 0 2(2 1)v v

s

v

x

y v u

1

0

1

0

1

s

x

y

v

u

3

2

1

5

1

y

u

v

s

x

Page 66: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

ExampleTableau is feasible, constraints are satisfied

Assignment EquationsBounds

2 3

2

y u s

v u s

x u s

( 2),( ),

1, 0

1 0 2(2 1)v v

s

v

x

y v u

1

0

1

0

1

s

x

y

v

u

3

2

1

1

5

x

y

s

u

v

Page 67: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Fast Linear arithmetic – recap

New bound xj u is asserted.Set (xj) min((xj), u)Check each row with xj if bounds on non-basic variables are satisfied.Tableau is infeasible if some row:

s = 3x1 + 4x2 - 5x3

Either ls > 3ux1 + 4ux2 - 5lx3,Or us < 3lx1 + 4lx2 - 5ux3

A tableau may still be feasible even if on non-basic variable is not satisfied.

Page 68: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Fast Linear arithmetic – recap

New bound xj u is asserted.Set (xj) min((xj), u)A tableau may still be feasible even if on non-basic variable is not satisfied.

Restore feasible tableau: PivotingExchange basic and non-basic variables in row where basic variable can be fixed.Substitute new basic variable everywhere elseUpdate assignment

Page 69: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Integer Linear Arithmetic

GCD test

Gomory Cut

Branch and Bound

Page 70: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Non-linear arithmeticMostly encountered in SMT applications of Hybrid systems.Decision problem is doubly exponential.Tools:

CAD: Cylindric Algebraic DecompositionGröbner Basis computationSymbolic solutions (using non-standard numerals)

Page 71: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Precise explanations

Page 72: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Precise explanations

Page 73: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

TakeawaysChoice of solver (and when to apply it) depends on:

Problem characteristics:Difference logic

Dense difference logic – Floyd Warshall.

Full linear arithmetic

Solver must work in the context of:Backtracking search engineProducing succinct explanations

Page 74: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Application:Spec#/Boogie

http://research.microsoft.com/specsharp

Page 75: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

Spec# Approach for a Verifying Compiler

Source Language

C# + goodies = Spec#Specifications

method contracts,invariants,field and type annotations.

Program Logic:

Dijkstra’s weakest preconditions.Automatic Verification

type checking,verification condition generation (VCG),automatic theorem proving (SMT)

Spec# (annotated C#)

Boogie PL

Spec# Compiler

VC Generator

Formulas

Automatic Theorem Prover

Page 76: Nikolaj Bjørner Microsoft Research Lecture 3. DayTopicsLab 1Overview of SMT and applications. SAT solving, Z3 Encoding combinatorial problems with Z3.

A short Demo

Spec#