|
Nikolaj Bjørner
Microsoft Research
nbjorner@microsoft.com |
An Introduction to Satisfiability Modulo Theories and Z3
Theory Solvers
SAT and SMT Algorithms
Optimization with MaxSAT
Quantifier Reasoning
An Introduction to Satisfiability Modulo Theories and Z3
Tie, Shirt = Bools('Tie Shirt')
s = Solver()
s.add(Or(Tie, Shirt), Or(Not(Tie), Shirt), Or(Not(Tie), Not(Shirt)))
print s.check()
print s.model()I = IntSort()
f = Function('f', I, I)
x, y, z = Ints('x y z')
A = Array('A',I,I)
fml = Implies(x + 2 == y, f(Select(Store(A, x, 3), y - 2)) == f(y - x + 1))
s = Solver()
s.add(Not(fml))
print s.check()
Is formula satisfiable under theory ?
SMT solvers use specialized algorithms for .
|
|
|
Sorts -
- signature of sorted constant, functions
Predicates are functions with range
Terms and predicates are built from functions over
Connectives and Quantifiers
Slide is by Joao Marques-Silva
sat unsat
model (clausal) proof
correction set core
local min correction set local min core
min correction set min core
Theory Solvers
(declare-sort A)
(declare-fun f (A) A)
(declare-const x A)
(assert (= (f (f x)) x))
(assert (= (f (f (f x))) x))
(check-sat)
(assert (not (= (f x) x)))
(check-sat)Variants of union-find are effective to decide equality.
Congruence rule
A satisfiable version:
(set-logic QF_IDL) ; optional in Z3
(declare-fun t11 () Int)
(declare-fun t12 () Int)
(declare-fun t21 () Int)
(declare-fun t22 () Int)
(declare-fun t31 () Int)
(declare-fun t32 () Int)
(assert (and (>= t11 0) (>= t12 (+ t11 2)) (<= (+ t12 1) 8)))
(assert (and (>= t21 0) (>= t22 (+ t21 3)) (<= (+ t22 1) 8)))
(assert (and (>= t31 0) (>= t32 (+ t31 2)) (<= (+ t32 3) 8)))
(assert (or (>= t11 (+ t21 3)) (>= t21 (+ t11 2))))
(assert (or (>= t11 (+ t31 2)) (>= t31 (+ t11 2))))
(assert (or (>= t21 (+ t31 2)) (>= t31 (+ t21 3))))
(assert (or (>= t12 (+ t22 1)) (>= t22 (+ t12 1))))
(assert (or (>= t12 (+ t32 3)) (>= t32 (+ t12 1))))
(assert (or (>= t22 (+ t32 3)) (>= t32 (+ t22 1))))
(check-sat)
(get-model) ; display the model | Logic | Fragment | Solver | Example |
|---|---|---|---|
| LRA | Linear Real Arithmetic | Dual Simplex | |
| LIA | Linear Integer Arithmetic | CutSat | |
| LIRA | Mixed Real/Integer | Cuts + Branch | |
| IDL | Integer Difference Logic | Floyd-Warshall | |
| RDL | Real Difference Logic | Bellman-Ford | |
| UTVPI | Unit two-variable per inequality | ||
| NRA | Polynomial Real Arithmetic | Model based CAD | |
NIA - There is no decision procedure for integer polynomial constraints
Other theories that admit custom solvers:
Bi-linear real arithmetic
Non-unit two-variable per inequality
At most one unit positive variable per inequality (Horn)
(set-logic QF_IDL) ; optional in Z3
(declare-fun t11 () Int)
(declare-fun t12 () Int)
(declare-fun t21 () Int)
(declare-fun t22 () Int)
(declare-fun t31 () Int)
(declare-fun t32 () Int)
(assert (and (>= t11 0) ...))
(assert (and (>= t21 0) ...))
(assert (and (>= t31 0) (>= t32 (+ t31 2)) (<= (+ t32 3) 8)))
(assert (or ... (>= t21 (+ t11 2))))
(assert (or (>= t21 (+ t31 2)) ...))
(check-sat)
(get-model) ; display the model Solve difference logic using graph Bellman-Ford network flow algorithm. Negative cycle unsat.
General form and
Only bounds (e.g., ) are asserted during search.
- are basic (dependent)
- are non-basic
- are basic (dependent)
- are non-basic
Initial values:
Bounds :
, make non-basic.
- are basic (dependent)
- are non-basic
(define-sort A () (Array Int Int))
(declare-fun x () Int)
(declare-fun y () Int)
(declare-fun a1 () A)
(push)
(assert (= (select a1 x) x))
(assert (= (store a1 x y) a1))
(check-sat)
(get-model)
(assert (not (= x y)))
(check-sat)
(pop)
(define-fun all1_array () A ((as const A) 1))
(simplify (select all1_array x))
(define-sort IntSet () (Array Int Bool))
(declare-fun a () IntSet)
(declare-fun b () IntSet)
(declare-fun c () IntSet)
(push) ; illustrate map
(assert (not (= ((_ map and) a b) ((_ map not) ((_ map or) ((_ map not) b) ((_ map not) a))))))
(check-sat)
(pop) A = Array(Index, Elem) # array sort
a[i] # index array 'a' at index 'i'
# Select(a, i)
Store(a, i, v) # update array 'a' with value 'v' at index 'i'
# = lambda j: If(i == j, v, a[j])
Const(v, A) # constant array
# = lambda j: v
Map(f, a) # map function 'f' on values of 'a'
# = lambda j: f(a[j])
Ext(a, b) # Extensionality
# Implies(a[Ext(a, b)] == b[Ext(a, b)], a == b)Example Store(a, i, v)
Store(a, i, v) and b[j],
Store(a, i, v)[j] == If(i == j, v, a[j])
Assert Store(a, i, v)[i] == v
Example Store(a, i, v)
Store(a, i, v) and b[j],
Store(a, i, v)[j] == If(i == j, v, a[j])
Assert Store(a, i, v)[i] == v
Assert .
Store axioms.
Use current partial interpretation to guide axiom instantiation.
Rough sketch:
Also important to limit set of pairs.
(define-fun is-power-of-two ((x (_ BitVec 4))) Bool
(= #x0 (bvand x (bvsub x #x1))))
(declare-const a (_ BitVec 4))
(assert
(not (= (is-power-of-two a)
(or (= a #x0)
(= a #x1)
(= a #x2)
(= a #x4)
(= a #x8)))))
(check-sat)
(declare-fun X () (_ FloatingPoint 11 53))
(assert (fp.isNormal X))
(assert (not (fp.isSubnormal X)))
(assert (not (fp.isZero X)))
(assert (not (fp.isInfinite X)))
(assert (not (fp.isNaN X)))
(assert (not (fp.isNegative X)))
(assert (fp.isPositive X))
(check-sat)
(get-model)(declare-const a String)
(declare-const b String)
(declare-const c String)
(assert (str.prefixof b a))
(assert (str.suffixof c a))
(assert (= (str.len a) (+ (str.len b) (str.len c))))
(assert (not (= a (str.++ b c))))
(check-sat) (declare-datatypes () ((Tree Empty (Node (left Tree) (data Int) (right Tree)))))
(declare-const t Tree)
(assert (not (= t Empty)))
(check-sat)
(get-model)
sat
(model
(define-fun t () Tree
(Node Empty 0 Empty))
)
; Code written by TheoryGuru
(declare-fun v1 () Real)
(declare-fun v2 () Real)
(declare-fun v3 () Real)
(declare-fun v4 () Real)
(declare-fun v5 () Real)
(declare-fun v6 () Real)
(declare-fun v7 () Real)
(declare-fun v8 () Real)
(declare-fun v9 () Real)
(declare-fun v10 () Real)
(declare-fun v11 () Real)
(declare-fun v12 () Real)
; define true assumption
(assert (and
(> (* 2 v11 v6 v9) (+ (* v12 (^ v6 2)) (* v8 (^ v9 2))))
(> (+ (* 2 v10 v11 v4 v6) (* v12 v5 (^ v6 2)) (* -2 v12 v4 v6 v7) (* v12 (^ v4 2) v8) (* -2 v11 v5 v6 v9) (* 2 v11 v4 v7 v9) (* 2 v10 v6 v7 v9) (* v5 v8 (^ v9 2))) (+ (* (^ v11 2) (^ v4 2)) (* (^ v10 2) (^ v6 2)) (* 2 v10 v4 v8 v9) (* (^ v7 2) (^ v9 2))))
(> v1 0)
(> v2 0)
(> v3 0)
(> v4 0)
(> v6 0)
(> v9 0)
(= (+ (* v1 v11) (* v3 v7) (* v2 v8)) 0)
(= (+ (* v1 v12) (* v11 v2) (* v10 v3)) 0)
(= (+ (* v1 v10) (* v3 v5) (* v2 v7)) 0)
))
; define negative of the hypothesis
(assert (not (>= (* v5 v8) (^ v7 2))))
(check-sat)
(exit)Strings: Decidability and efficiency.
Certificates: Modular, Clausal
New, non-disjoint, theories
SAT and SMT Algorithms