SMT and Z3 Nikolaj BjΓΈrner Microsoft Research ReRISE Winter School, Linz, Austria February 5, 2014
Plan Mon An invitation to SMT with Z3 Tue Equalities and Theory Combination Wed Theories: Arithmetic, Arrays, Data types Thu Quantifiers and Theories Fri Programming Z3: Interfacing and Solving
Quiz Show: A difference logic graph without negative cycles has a model. Give a procedure for extracting a model. True or false: A formula over difference logic has a model over reals iff it has a model over integers? Give an efficient algorithm to extract models for UTVPI over integers. Encode lambda Calculus into πππ, πΏ, π πππ (without π½ ).
Plan β’ Arithmetic β’ Arrays and friends β’ Data types [Introduction]
What Theories? Overall aim: Rich Theories (and logics) with Efficient Decision Procedures ASP Auth MSOL Sequences XDucers Queues DL homomor Optimiz BAPA Orders Objects HOL MultiSets phisms ation Floats f* * Strings Reg. Exprs. NRA NIA SAT Arrays Bit-Vectors Alg. DT EUF LRA LIA
Be afraid!
Linear Real Arithmetic β’ Many approaches β Graph-based for difference logic: a β b ο£ 3 β Fourier-Motzkin elimination: β Standard Simplex β General Form Simplex β GDPLL [McMillan], Unate Resolution [Coton], Conflict Resolution [Korovin et.al.]
Difference Logic: a β b ο£ 5 Very useful in practice! Most arithmetical constraints in software verification/analysis are in this fragment. x := x + 1 x 1 = x 0 + 1 x 1 - x 0 ο£ 1, x 0 - x 1 ο£ - 1
Job shop scheduling
Difference Logic Chasing negative cycles! Algorithms based on Bellman-Ford (O(mn)).
Unit Two Variables Per Inequality π¦ + π§ β€ 5 β§ βπ¦ + π§ β€ β4 β§ π§ + π§ β₯ 1
Unit Two Variables Per Inequality π¦ + π§ β€ 5 β§ βπ¦ + π§ β€ β4 β§ 2π§ β₯ 1 2π§ β€ 1 β§ 2π§ β₯ 1
Unit Two Variables Per Inequality π¦ + π§ β€ 5 β§ βπ¦ + π§ β€ β4 β§ 2π§ β₯ 1 2π§ β€ 1 β§ 2π§ β₯ 1 π§ β€ 0 β§ π§ β₯ 1
Unit Two Variables Per Inequality: UTVPI Reduce to Difference Logic: β’ For every variable π¦ introduce fresh variables π¦ + , π¦ β β’ Meaning: 2π¦ β π¦ + β π¦ β β’ Rewrite constraints as follows: β π¦ + β π§ + β€ π β’ π¦ β π§ β€ π π§ β β π¦ β β€ π β’
UTVPI β π¦ + β π§ + β€ π β’ π¦ β π§ β€ π π§ β β π¦ β β€ π β π¦ + β π¦ β β€ 2π β’ π¦ β€ π β π¦ + β π§ β β€ π β’ π¦ + π§ β€ π π§ + β π¦ β β€ π β’ π¦ + π§ β€ π β chalkboard
UTVPI π¦ + π§ β€ 5 β§ βπ¦ + π§ β€ β4 β§ 2π§ β₯ 1 π¦ + β π§ β β€ 5 β§ π§ + β π¦ β β€ 5 β§ βπ¦ + + π§ + β€ β4 β§ π¦ β β π§ β β€ β4 β§ π§ β β π§ + β€ 1
UTVPI β’ Solve for π¦ + and π¦ β β’ π(π¦) β (π(π¦ + ) β π(π¦ β ))/2 β’ Nothing can go wrongβ¦ 2π§ β€ 1 β§ 2π§ β₯ 1
UTVPI β’ π(π¦) β (π(π¦ + ) β π(π¦ β ))/2 β’ Nothing can go wrongβ¦ as if β’ What if: β π¦ is an integer β π(π¦ + ) is odd and β π(π¦ β ) is even β’ Thm : Parity can be fixed iff there is no tight loop forcing the wrong parity
UTVPI π¦ β β π§ + β€ 5 π§ + β π¨ β β€ β6 π¦ β β π¦ + β€ β3 π¨ β β π¦ + β β€ β2 π¦ + β π¦ β β€ 3 π¦ + β π€ + β€ 3 π€ + β π¦ β β€ 0
General Form
From Definitions to a Tableau s 1 οΊ x + y, s 2 οΊ x + 2y
From Definitions to a Tableau s 1 οΊ x + y, s 2 οΊ x + 2y s 1 = x + y, s 2 = x + 2y
From Definitions to a Tableau s 1 οΊ x + y, s 2 οΊ x + 2y s 1 = x + y, s 2 = x + 2y s 1 - x - y = 0 s 2 - x - 2y = 0
From Definitions to a Tableau s 1 οΊ x + y, s 2 οΊ x + 2y s 1 = x + y, s 2 = x + 2y s 1 , s 2 are basic (dependent) s 1 - x - y = 0 x,y are non-basic s 2 - x - 2y = 0
Pivoting A way to swap a basic with a non-basic variable! It is just equational reasoning. Key invariant: a basic variable occurs in only one equation. Example: swap s 1 and y s 1 - x - y = 0 s 2 - x - 2y = 0
Pivoting A way to swap a basic with a non-basic variable! It is just equational reasoning. Key invariant: a basic variable occurs in only one equation. Example: swap s 1 and y s 1 - x - y = 0 s 2 - x - 2y = 0 -s 1 + x + y = 0 s 2 - x - 2y = 0
Pivoting A way to swap a basic with a non-basic variable! It is just equational reasoning. Key invariant: a basic variable occurs in only one equation. Example: swap s 1 and y s 1 - x - y = 0 s 2 - x - 2y = 0 -s 1 + x + y = 0 s 2 - x - 2y = 0 -s 1 + x + y = 0 s 2 - 2s 1 + x = 0
Pivoting A way to swap a basic with a non-basic variable! It is just equational reasoning. Key invariant: a basic variable occurs in only one equation. Example: swap s 1 and y s 1 - x - y = 0 It is just substituting s 2 - x - 2y = 0 equals by equals. -s 1 + x + y = 0 s 2 - x - 2y = 0 -s 1 + x + y = 0 s 2 - 2s 1 + x = 0
Definition: Pivoting An assignment (model) is a mapping from variables to values A way to swap a basic with a non-basic variable! It is just equational reasoning. Key invariant: a basic variable occurs in only one equation. Example: swap s 1 and y s 1 - x - y = 0 It is just substituting s 2 - x - 2y = 0 equals by equals. -s 1 + x + y = 0 Key Property: s 2 - x - 2y = 0 If an assignment satisfies the equations before a pivoting -s 1 + x + y = 0 step, then it will also satisfy s 2 - 2s 1 + x = 0 them after!
Definition: Pivoting An assignment (model) is a mapping from variables to values A way to swap a basic with a non-basic variable! It is just equational reasoning. Key invariant: a basic variable occurs in only one equation. Example: swap s 2 and y s 1 - x - y = 0 It is just substituting s 2 - x - 2y = 0 equals by equals. Example: -s 1 + x + y = 0 M(x) = 1 Key Property: s 2 - x - 2y = 0 M(y) = 1 If an assignment satisfies the M(s 1 ) = 2 equations before a pivoting M(s 2 ) = 3 -s 1 + x + y = 0 step, then it will also satisfy s 2 - 2s 1 + x = 0 them after!
Equations + Bounds + Assignment
βRepairing Modelsβ If the assignment of a non-basic variable does not satisfy a bound, then fix it and propagate the change to all dependent variables. a = c β d a = c β d b = c + d b = c + d M(a) = 0 M(a) = 1 M(b) = 0 M(b) = 1 M(c) = 0 M(c) = 1 M(d) = 0 M(d) = 0 1 ο£ c 1 ο£ c
βRepairing Modelsβ If the assignment of a non-basic variable does not satisfy a bound, then fix it and propagate the change to all dependent variables. Of course, we may introduce new βproblemsβ. a = c β d a = c β d b = c + d b = c + d M(a) = 0 M(a) = 1 M(b) = 0 M(b) = 1 M(c) = 0 M(c) = 1 M(d) = 0 M(d) = 0 1 ο£ c 1 ο£ c a ο£ 0 a ο£ 0
βRepairing Modelsβ If the assignment of a basic variable does not satisfy a bound, then pivot it, fix it, and propagate the change to its new dependent variables. a = c β d c = a + d c = a + d b = c + d b = a + 2d b = a + 2d M(a) = 0 M(a) = 0 M(a) = 1 M(b) = 0 M(b) = 0 M(b) = 1 M(c) = 0 M(c) = 0 M(c) = 1 M(d) = 0 M(d) = 0 M(d) = 0 1 ο£ a 1 ο£ a 1 ο£ a
βRepairing Modelsβ Sometimes, a model cannot be repaired. It is pointless to pivot. The value of M(a) is too big. We can reduce it by: a = b β c - reducing M(b) a ο£ 0, 1 ο£ b, c ο£ 0 not possible b is at lower bound - increasing M(c) M(a) = 1 not possible c is at upper bound M(b) = 1 M(c) = 0
βRepairing Modelsβ Extracting proof from failed repair attempts is easy. s 1 οΊ a + d, s 2 οΊ c + d a = s 1 β s 2 + c a ο£ 0, 1 ο£ s 1 , s 2 ο£ 0, 0 ο£ c M(a) = 1 M(s 1 ) = 1 M(s 2 ) = 0 M(c) = 0
βRepairing Modelsβ Extracting proof from failed repair attempts is easy. s 1 οΊ a + d, s 2 οΊ c + d a = s 1 β s 2 + c a ο£ 0, 1 ο£ s 1 , s 2 ο£ 0, 0 ο£ c M(a) = 1 M(s 1 ) = 1 M(s 2 ) = 0 M(c) = 0 { a ο£ 0, 1 ο£ s 1 , s 2 ο£ 0, 0 ο£ c } is inconsistent
βRepairing Modelsβ Extracting proof from failed repair attempts is easy. s 1 οΊ a + d, s 2 οΊ c + d a = s 1 β s 2 + c a ο£ 0, 1 ο£ s 1 , s 2 ο£ 0, 0 ο£ c M(a) = 1 M(s 1 ) = 1 M(s 2 ) = 0 M(c) = 0 { a ο£ 0, 1 ο£ s 1 , s 2 ο£ 0, 0 ο£ c } is inconsistent { a ο£ 0, 1 ο£ a + d, c + d ο£ 0, 0 ο£ c } is inconsistent
What are arrays? β’ Applicative stores: ο½ ( , , )[ ] write a i v i v οΉ ο ο½ ( , , )[ ] [ ] i j write a i v j a j β’ Or, special combinator: ο½ ο¬ ο½ ( , , ) . ( , , [ ]) write a i v j ite i j v a j
What are arrays? β’ Special combinator: ο½ ο¬ ο½ ( , , ) . ( , , [ ]) write a i v j ite i j v a j β’ Existential fragment is decidable by reduction to congruence closure using finite set of instances. β’ Models for arrays are finite maps with default values.
What else are arrays? β’ Special combinators: ο½ ο¬ ο½ ( , , ) . ( , , [ ]) write a i v j ite i j v a j ο½ ο¬ ( ) . K v j v ο½ ο¬ ( , ) . ( [ ], [ ]) map a b j f a j b j f β’ Result : Existential fragment is decidable and in NP by reduction to congruence closure using finite set of instances.
Recommend
More recommend