Satisfiability Modulo Theories and Z3 Nikolaj Bjrner Microsoft - - PowerPoint PPT Presentation

satisfiability modulo theories and z3
SMART_READER_LITE
LIVE PREVIEW

Satisfiability Modulo Theories and Z3 Nikolaj Bjrner Microsoft - - PowerPoint PPT Presentation

Satisfiability Modulo Theories and Z3 Nikolaj Bjrner Microsoft Research ReRISE Winter School, Linz, Austria February 3, 2014 SMT : Basic Architecture Theory SAT SMT Solvers Equality + UF Arithmetic Case Analysis Bit-vectors Nikolaj is


slide-1
SLIDE 1

Satisfiability Modulo Theories and Z3

Nikolaj Bjørner Microsoft Research

ReRISE Winter School, Linz, Austria February 3, 2014

slide-2
SLIDE 2

SAT Theory Solvers SMT

SMT : Basic Architecture

Equality + UF Arithmetic Bit-vectors …

Case Analysis

Nikolaj is Sober ∨ Nikolaj is Somber ∨ (Nikolaj is Drunk ∧ Nikolaj is Happy) Theory[Alcohol]: Sober ⊗ Drunk Theory[Moodswings]: Somber ⊗ Happy

slide-3
SLIDE 3

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

slide-4
SLIDE 4

Part 1

I. Satisfiability Modulo Theories in a nutshell II. SMT solving in a nutshell

  • III. SMT by example
slide-5
SLIDE 5

Takeaways:

  • Modern SMT solvers are a often good fit for

program analysis tools.

– Handle domains found in programs directly.

  • The selected examples are intended to show

instances where sub-tasks are reduced to SMT/Z3.

slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8

– Backed by Proof Plumbers

Leonardo de Moura, Nikolaj Bjørner, Christoph Wintersteiger Not all is hopeless

slide-9
SLIDE 9

Background Reading: SMT

September 2011

slide-10
SLIDE 10

Program Verification Auditing Type Safety Over- Approximation Under- Approximation Testing Analysis Synthesis

SAGE

HAVOC

SLAyer

BEK

Some Microsoft Tools based on

slide-11
SLIDE 11
slide-12
SLIDE 12

SMT IN A NUTSHELL

slide-13
SLIDE 13

Is formula  satisfiable modulo theory T ?

SMT solvers have specialized algorithms for T

Satisfiability Modulo Theories (SMT)

slide-14
SLIDE 14

Arithmetic Array Theory Uninterpreted Functions

𝑡𝑓𝑚𝑓𝑑𝑢(𝑡𝑢𝑝𝑠𝑓 𝑏, 𝑗, 𝑤 , 𝑗) = 𝑤 𝑗 ≠ 𝑘 ⇒ 𝑡𝑓𝑚𝑓𝑑𝑢(𝑡𝑢𝑝𝑠𝑓 𝑏, 𝑗, 𝑤 , 𝑘) = 𝑡𝑓𝑚𝑓𝑑𝑢(𝑏, 𝑘)

𝑦 + 2 = 𝑧 ⇒ 𝑔 𝑡𝑓𝑚𝑓𝑑𝑢 𝑡𝑢𝑝𝑠𝑓 𝑏, 𝑦, 3 , 𝑧 − 2 = 𝑔(𝑧 − 𝑦 + 1)

Satisfiability Modulo Theories (SMT)

slide-15
SLIDE 15

SMT SOLVING IN A NUTSHELL

Job Shop Scheduling

slide-16
SLIDE 16

Job Shop Scheduling

Machines Jobs P = NP? Laundry

𝜂 𝑡 = 0 ⇒ 𝑡 = 1 2 + 𝑗𝑠

Tasks

slide-17
SLIDE 17

Constraints:

Precedence: between two tasks of the same

job

Resource: Machines execute at most one job

at a time

4 1 3 2 𝑡𝑢𝑏𝑠𝑢2,2. . 𝑓𝑜𝑒2,2 ∩ 𝑡𝑢𝑏𝑠𝑢4,2. . 𝑓𝑜𝑒4,2 = ∅

Job Shop Scheduling

slide-18
SLIDE 18

Constraints: Encoding:

Precedence:

𝑢2,3 - start time of job 2 on mach 3 𝑒2,3 - duration of job 2 on mach 3 𝑢2,3 + 𝑒2,3 ≤ 𝑢2,4

Resource:

4 1 3 2 𝑡𝑢𝑏𝑠𝑢2,2. . 𝑓𝑜𝑒2,2 ∩ 𝑡𝑢𝑏𝑠𝑢4,2. . 𝑓𝑜𝑒4,2 = ∅

𝑢2,2 + 𝑒2,2 ≤ 𝑢4,2 ∨ 𝑢4,2 + d4,2 ≤ 𝑢2,2

Not convex

Job Shop Scheduling

slide-19
SLIDE 19

Job Shop Scheduling

slide-20
SLIDE 20

Job Shop Scheduling

case split case split Efficient solvers:

  • Floyd-Warshal algorithm
  • Ford-Fulkerson algorithm

𝑨 − 𝑨 = 5 – 2 – 3 – 2 = −2 < 0

slide-21
SLIDE 21

THEORIES

slide-22
SLIDE 22

Theories

Uninterpreted functions

slide-23
SLIDE 23

Uninterpreted functions Arithmetic (linear)

Theories

slide-24
SLIDE 24

Uninterpreted functions Arithmetic (linear) Bit-vectors

Theories

slide-25
SLIDE 25

Uninterpreted functions Arithmetic (linear) Bit-vectors Algebraic data-types

Theories

slide-26
SLIDE 26

Uninterpreted functions Arithmetic (linear) Bit-vectors Algebraic data-types Arrays

Theories

slide-27
SLIDE 27

Uninterpreted functions Arithmetic (linear) Bit-vectors Algebraic data-types Arrays Polynomial Arithmetic

Theories

slide-28
SLIDE 28

QUANTIFIERS

slide-29
SLIDE 29

Equality-Matching

(∀𝑦 𝑔 𝑕 𝑑, 𝑦 = 𝑦) ∧ 𝑏 = 𝑕 𝑐, 𝑐 ∧ 𝑐 = 𝑑 ∧ 𝑔 𝑏 ≠ 𝑑 ∧ ∀𝑦 𝑔 𝑕 𝑑, 𝑦 = 𝑦 → 𝑔 𝑕 𝑑, 𝑐 = 𝑐

𝑕 𝑑, 𝑦 matches 𝑕 𝑐, 𝑐 with substitution 𝑦 ↦ 𝑐 modulo 𝑐 = 𝑑

[de Moura, B. CADE 2007]

𝑞(∀ …) ∧ 𝑏 = 𝑕 𝑐, 𝑐 ∧ 𝑐 = 𝑑 ∧ 𝑔 𝑏 ≠ 𝑑 ∧ 𝑞(∀𝑦 ...) → 𝑔 𝑕 𝑑, 𝑐 = 𝑐

slide-30
SLIDE 30

Quantifier Elimination

[B. IJCAR 2010] Presburger Arithmetic, Algebraic Data-types, Quadratic polynomials SMT integration to prune branches

slide-31
SLIDE 31

MBQI: Model based Quantifier Instantiation

[de Moura, Ge. CAV 2008] [Bonachnia, Lynch, de Moura CADE 2009] [de Moura, B. IJCAR 2010]

slide-32
SLIDE 32

Horn Clauses

[Hoder, B. SAT 2012]

∀𝒀. 𝒀 > 𝟐𝟏𝟏  mc(𝒀, 𝒀 − 𝟐𝟏) ∀𝒀, 𝒁, 𝑺. 𝒀 ≤ 𝟐𝟏𝟏  mc(𝒀 + 𝟐𝟐, 𝒁)  mc(𝒁, 𝑺)  mc(𝒀, 𝑺) ∀𝒀, 𝑺. mc(𝒀, 𝑺) ∧ 𝒀 ≤ 𝟐𝟏𝟐 → 𝑺 = 𝟘𝟐

Solver finds solution for mc mc(x) = x-10 if x > 100 mc(x) = mc(mc(x+11)) if x  100 assert (mc(x)  91)

slide-33
SLIDE 33

MODELS, PROOFS, CORES & SIMPLIFICATION

slide-34
SLIDE 34

Logical Formula

Sat/Model

Models

slide-35
SLIDE 35

Proofs

Logical Formula

Unsat/Proof

slide-36
SLIDE 36

Simplification

Simplify

Logical Formula

slide-37
SLIDE 37

Cores

Logical Formula

  • Unsat. Core
slide-38
SLIDE 38

TACTICS, SOLVERS

slide-39
SLIDE 39

Tactics

Composi

  • siti

tion

  • n of tacti

ctics: cs:

  • (then t s)
  • (par-then t s) applies t to the input goal and s to every subgoal produced by t in parallel.
  • (or-else t s)
  • (par-or t s) applies t and s in parallel until one of them succeed.
  • (repeat t)
  • (repeat t n)
  • (try-for t ms)
  • (using-params t params) Apply the given tactic using the given parameters.
slide-40
SLIDE 40

Solvers

  • Tactics take goals and reduce to sub-goals
  • Solvers take tactics and serve as logical contexts.
  • push
  • add
  • check
  • model, core, proof
  • pop
slide-41
SLIDE 41

APIS C

C++

python

OCaml

.NET Java

slide-42
SLIDE 42

SMT SOLVING

slide-43
SLIDE 43

SAT Theory Solvers SMT

SMT : Basic Architecture

Equality + UF Arithmetic Bit-vectors …

Case Analysis

slide-44
SLIDE 44

SAT + Theory solvers

Basic Idea

x  0, y = x + 1, (y > 2  y < 1) p1, p2, (p3  p4) Abstract (aka “naming” atoms) p1  (x  0), p2  (y = x + 1), p3  (y > 2), p4  (y < 1)

slide-45
SLIDE 45

SAT + Theory solvers

Basic Idea

x  0, y = x + 1, (y > 2  y < 1) p1, p2, (p3  p4) Abstract (aka “naming” atoms) p1  (x  0), p2  (y = x + 1), p3  (y > 2), p4  (y < 1) SAT Solver

slide-46
SLIDE 46

SAT + Theory solvers

Basic Idea

x  0, y = x + 1, (y > 2  y < 1) p1, p2, (p3  p4) Abstract (aka “naming” atoms) p1  (x  0), p2  (y = x + 1), p3  (y > 2), p4  (y < 1) SAT Solver Assignment p1, p2, p3, p4

slide-47
SLIDE 47

SAT + Theory solvers

Basic Idea

x  0, y = x + 1, (y > 2  y < 1) p1, p2, (p3  p4) Abstract (aka “naming” atoms) p1  (x  0), p2  (y = x + 1), p3  (y > 2), p4  (y < 1) SAT Solver Assignment p1, p2, p3, p4 x  0, y = x + 1,

  • (y > 2), y < 1
slide-48
SLIDE 48

SAT + Theory solvers

Basic Idea

x  0, y = x + 1, (y > 2  y < 1) p1, p2, (p3  p4) Abstract (aka “naming” atoms) p1  (x  0), p2  (y = x + 1), p3  (y > 2), p4  (y < 1) SAT Solver Assignment p1, p2, p3, p4 x  0, y = x + 1,

  • (y > 2), y < 1

Theory Solver Unsatisfiable x  0, y = x + 1, y < 1

slide-49
SLIDE 49

SAT + Theory solvers

Basic Idea

x  0, y = x + 1, (y > 2  y < 1) p1, p2, (p3  p4) Abstract (aka “naming” atoms) p1  (x  0), p2  (y = x + 1), p3  (y > 2), p4  (y < 1) SAT Solver Assignment p1, p2, p3, p4 x  0, y = x + 1,

  • (y > 2), y < 1

Theory Solver Unsatisfiable x  0, y = x + 1, y < 1 New Lemma

  • p1p2p4
slide-50
SLIDE 50

SAT + Theory solvers

Theory Solver Unsatisfiable x  0, y = x + 1, y < 1 New Lemma

  • p1p2p4

AKA Theory conflict

slide-51
SLIDE 51

SAT/SMT SOLVING USING DPLL(T)/CDCL

slide-52
SLIDE 52

Proofs

Conflict Clauses

Models

literal assignments

Backjump Propagate

Mile High: Modern SAT/SMT search

slide-53
SLIDE 53

Core Engine in Z3: Modern DPLL/CDCL

Initialize 𝜗| 𝐺 𝐺 𝑗𝑡 𝑏 𝑡𝑓𝑢 𝑝𝑔 𝑑𝑚𝑏𝑣𝑡𝑓𝑡 Decide 𝑁 𝐺 ⟹ 𝑁, ℓ 𝐺 ℓ 𝑗𝑡 𝑣𝑜𝑏𝑡𝑡𝑗𝑕𝑜𝑓𝑒 Propagate 𝑁 𝐺, 𝐷 ∨ ℓ ⟹ 𝑁, ℓ𝐷∨ℓ 𝐺, 𝐷 ∨ ℓ 𝐷 𝑗𝑡 𝑔𝑏𝑚𝑡𝑓 𝑣𝑜𝑒𝑓𝑠 𝑁 Sat 𝑁 |𝐺 ⟹ 𝑁 𝐺 𝑢𝑠𝑣𝑓 𝑣𝑜𝑒𝑓𝑠 𝑁 Conflict 𝑁 𝐺, 𝐷 ⟹ 𝑁 𝐺, 𝐷 | 𝐷 𝐷 𝑗𝑡 𝑔𝑏𝑚𝑡𝑓 𝑣𝑜𝑒𝑓𝑠 𝑁 Learn 𝑁 𝐺 | 𝐷 ⟹ 𝑁 𝐺, 𝐷 | 𝐷 Unsat 𝑁 𝐺 ∅ ⟹ 𝑉𝑜𝑡𝑏𝑢 Backjump 𝑁𝑁′ 𝐺 | 𝐷 ∨ ℓ ⟹ 𝑁ℓ𝐷∨ℓ 𝐺 𝐷 ⊆ 𝑁, ¬ℓ ∈ 𝑁′ Resolve 𝑁 𝐺 | 𝐷′ ∨ ¬ℓ ⟹ 𝑁 𝐺 | 𝐷′ ∨ 𝐷 ℓ𝐷∨ℓ ∈ 𝑁 Forget 𝑁 𝐺, 𝐷 ⟹ 𝑁 𝐺 𝐷 is a learned clause Restart 𝑁 𝐺 ⟹ 𝜗 𝐺 [Nieuwenhuis, Oliveras, Tinelli J.ACM 06] customized

slide-54
SLIDE 54

DPLL(T) solver interaction

slide-55
SLIDE 55

Summary

Z3 supports several theories

– Using a default combination – Providing custom tactics for special combinations

Z3 is more than sat/unsat

– Models, proofs, unsat cores, – simplification, quantifier elimination are tactics

Prototype with python/smt-lib2

– Implement using smt-lib2/programmatic API