SAT and SMT solvers Ayrat Khalimov (based on Georg Hofferek‘s slides) AKDV 2014
Motivation Institute for Applied Information Processing and Communications 2 SAT solvers: They rocketed the model checking First-Order Theories Very expressive Efficient SMT Solvers But: What are they? How do solvers work?
Outline Institute for Applied Information Processing and Communications 3 Propositional SAT solver DPLL algorithm Predicate Logic (aka. First-Order Logic) Syntax Semantics First Order Logic First-Order Theories SMT solver Eager Encoding Lazy Encoding DPLL(T)
Scope of Solvers propositional logic SAT solvers first order logic theory of equality difference logic Theorem provers SMT solvers linear integer arithmetic … theory of arrays
Notation propositional variables e.g., a, b, c, d, … literal is a variable or its negation e.g., a, b, … partial assignment A is a conjunction of literals e.g., A = a d clause is a disjunction of literals e.g., c = a b is a CNF formula (i.e. conjunction of clauses): e.g., = (a b d) c [A] is with all variables set according to A e.g., [A] = (FALSE b TRUE) c = b c
SAT Solver Formula in CNF Satisfiable (+ model) Unsatisfiable (+ refutation proof)
DPLL Algorithm Due to Davis, Putnam, Loveland, Logemann two papers: 1960, 1962 Basis for all modern SAT solvers
CNF as a Set of Clauses
Idea of DPLL-based SAT Solvers Recursively search an A: [A] is TRUE Proves satisfiable “A” is a satisfying model No such A exists is unsatisfiable
Setting Literals
Truth Value of a CNF At least one clause is empty: FALSE Clause set empty: TRUE Otherwise: Unassigned Literals left
DPLL Algorithm // sat( , A)=TRUE iff [A] is satisfiable // sat( , true)=TRUE iff is satisfiable sat( , A){ if( [A] = true) return TRUE; if( [A] = false) return FALSE; // Some unassigned variables left l = pick unassigned variable; AT = A l; if(sat( , AT)) return TRUE; AF = A l; if(sat( , AF)) return TRUE; return FALSE; }
DPLL Example Formula to check: ( a b) ( b c) ( c a) 1.sat(( a b) ( b c) ( c a), true) 2.sat( ( a b) ( b c) ( c a), a) 3.sat( ( a b) ( b c) ( c a), a b) 4.sat( ( a b) ( b c) ( c a), a b c) unsat 5.sat( ( a b) ( b c) ( c a), a b c) unsat 6.sat( ( a b) ( b c) ( c a), a b) unsat 7.sat( ( a b) ( b c) ( c a), a) 8.sat(( a b) ( b c) ( c a), a b) 9.sat(( a b) ( b c) ( c a), a b c) sat
Boolean Constraint Propagation (BCP) Unit clause: a clause with a single unassigned literal Examples: (a) ( b) Unit Clause exists set its literal Very simple but very important heuristic!
DPLL with BCP sat( , A){ while(unit clause occurs){ // l is only unassigned literal in // unit clause; A = A l; } if( [A] = true) return TRUE; if( [A] = false) return FALSE; l = pick unassigned variable; AT = A l; if(sat( , AT)) return TRUE; AF = A l; if(sat( , AF)) return TRUE; return FALSE; }
Example Formula to check: ( a b) ( b c) ( c a) 1.sat(( a b) ( b c) ( c a), true) 2.sat( ( a b) ( b c) ( c a), a) 3.[BCP]: sat( ( a b) ( b c) ( c a), a b) 4.[BCP]: sat( ( a b) ( b c) ( c a), a b c) unsat 5.sat( ( a b) ( b c) ( c a), a) 6.sat( ( a b) ( b c) ( c a), a b) 7.sat(( a b) ( b c) ( c a), a b c) sat
Can we do better? sat( , A){ while(unit clause occurs){ // l is only unassigned literal in // unit clause; A = A l; } if( [A] = true) return TRUE; if( [A] = false) return FALSE; l = pick unassigned variable; AT = A l; if(sat( , AT)) return TRUE; AF = A l; if(sat( , AF)) return TRUE; return FALSE; }
Pure Literals Pure literal: Literal for unassigned variable The variable appears in one phase only Pure literals true them
DPLL with BCP and Pure Literals sat( , A){ while(unit clause occurs){ // BCP let l be only unassigned literal in c; A = A l; } while(pure literal l exists){ // Pure literals A = A l; } if( [A] = true) return TRUE; if( [A] = false) return FALSE; l = pick a literal that does not occur in A; AT = A l; if(sat( , AT)) return TRUE; AL = A l; if(sat( , AL)) return TRUE; return FALSE; }
Example Formula to check: ( a b) ( b c) ( c a) 1.sat(( a b) ( b c) ( c a), true) [ a pure] 2.sat( ( a b) ( b c) ( c a), a) [ b pure] 3.sat( ( a b) ( b c) ( c a), a b) sat
Can we do better? Institute for Applied Information Processing and Communications 21 sat( , A){ while(unit clause l occurs) A = A l; while(pure literal l exists) A = A l; if( [A] = true) return TRUE; if( [A] = false) return FALSE; l = pick a literal that does not occur in A; AT = A l; if(sat( , AT)) return TRUE; AL = A l; if(sat( , AL)) return TRUE; return FALSE; }
Whenever we get the conflict analyze it add clauses to avoid in future Institute for Applied Information Processing and Communications 22 Learning: informal
Learning 1.(a c) 2.(b c) 3.( a b c) 4.( a b) 5.( a b) 6.(a b) 7.(a b) cc aa UNSAT
Learning 1.(a c) 2.(b c) 3.( a b c) 4.( a b) 5.( a b) 6.(a b) 7.(a b) cc aa UNSAT a The problem is with a: no need to set c=true! aa UNSAT a Without learning
Learning 1.(a c) 2.(b c) 3.( a b c) 4.( a b) 5.( a b) 6.(a b) 7.(a b) cc aa UNSAT aa false 7 We learn: a bb 6
Learning & Backtracking 1.(a c) 2.(b c) 3.( a b c) 4.( a b) 5.( a b) 6.(a b) 7.(a b) 8.a cc aa UNSAT Jump back to level 0 is smart LEVEL 0 LEVEL 1 LEVEL 2 aa false 7 We learn: a bb 6
Learning & Backtracking 1.(a c) 2.(b c) 3.( a b c) 4.( a b) 5.( a b) 6.(a b) 7.(a b) 8.a cc aa UNSAT a Jump back to level 0 is smart LEVEL 0 LEVEL 1 LEVEL 2
Learning & Backtracking 1.(a c) 2.(b c) 3.( a b c) 4.( a b) 5.( a b) 6.(a b) 7.(a b) 8.a cc aa UNSAT a bb 4 false 5 LEVEL 0 LEVEL 1 LEVEL 2
Learning & Backtracking 1.(a c) 2.(b c) 3.( a b c) 4.( a b) 5.( a b) 6.(a b) 7.(a b) 8.a cc aa UNSAT a bb 4 false 5 UNSAT We learn: UNSAT, because no decision was necessary LEVEL 0 LEVEL 1 LEVEL 2
Backtrack Level Three important possibilities 1.Backtrack as usual 2.Restart for every learned clause 3.Go to the earliest level in which the conflict clause is a unit clause Option 3 often performs better
Can we do better? (learning is not shown) 31 sat( , A){ while(unit clause l occurs) A = A l; while(pure literal l exists) A = A l; if( [A] = true) return TRUE; if( [A] = false) return FALSE; l = pick a literal that does not occur in A; AT = A l; if(sat( , AT)) return TRUE; AF = A l; if(sat( , AF)) return TRUE; return FALSE; } how to pick literals?
Institute for Applied Information Processing and Communications 32 Source: Armin Biere’s slides: Effect of picking heuristics on SAT solver performance
Can we do better? -- Special cases Institute for Applied Information Processing and Communications 33 Horn clauses can be solved in polynomial time Cut width algorithm
source:
Syntax of Predicate Logic Two sorts: Objects Numbers Strings Elements of sets … Truth values IsEven(42) “Terms” “Formulas”
From Terms to Formulas Term Formula Predicate
FOL formulae: informal definition quantifiers over variables unary predicates: binary, etc. functions can FO formulae quantify over functions/predicates? can FO formulae have free (non-quantified) variables? * can FO formulae have ‘uninterpreted’ functions? * can FO formula has infinite number of atoms?
Syntax of Predicate Logic Variables x, y, z, … Functions f, g, h, … (arity > 0) constants (arity = 0) Predicates ℙ P, Q, R, … (with arity > 0) Terms and Formulae defined next
Terms
Formulae
True and False FO formulae
Semantics of Predicate Logic Inductive Definition
Semantics of Predicate Logic
Institute for Applied Information Processing and Communications 46 Examples
Satisfiable FO formulae
Valid FO formulae
Some facts about our world Gödel proved that every valid FO formula has a finite proof. Church-Turing proved that no algorithm exists that can decide if FO formula is invalid proof deduction algorithm FO formula may never terminate if valid if invalid
Notion of “Theory” Application Domain Structures & Objects Predicates & Functions Arithmetic Numbers (Integers, Rationals, Reals) Computer Programs Arrays,Bitvectors Array-Read, Array-Write, …
Definition of a Theory
Model View We check satisfiability and validity only wrt models that satisfy axioms “Satisfiability modulo (=‘with respect to’) theories” All possible Models Models satisfying all axioms
Green: Models Satisfying all Axioms Violet: Models Satisfying Formula in Question
Green: Models Satisfying all Axioms Violet: Models Satisfying Formula in Question
Theory Formulas vs. FO Formulas equivalid equisatisfiable
Fragment of a Theory
Scope of Solvers propositional logic SAT solvers first order logic theory of equality difference logic Theorem provers SMT solvers linear integer arithmetic … theory of arrays
Deciding Satisfiability (quantifier free theory): main methods 1. Eager Encoding Equisatisfiable propositional formula one fat SAT call 2. Lazy Encoding Theory Solver Conjunctive Fragment Blocking Clauses numerous SAT calls 3. DPLL (T)
Axiom Schema: Template for (infinite number of) axioms
Two-Stage Eager Encoding equisatisfiable propositional formula equisatisfiable propositional formula Ackermann’s Reduction Graph-based Reduction SAT Solver
63
Non-Polar Equality Graph Node per variable Edge per (dis)equality Make it chordal No chord-free cycles (size > 3) a b c d e f g
SAT Solver
66
Summary: Eager Encoding equisatisfiable propositional formula equisatisfiable propositional formula Ackermann’s Reduction Graph-based Reduction SAT Solver
Lazy Encoding SAT Solver Theory Solver Assignment of Literals Blocking Clause SAT UNSAT
Congruence-Closure Algorithm
71
Lazy Encoding SAT Solver Theory Solver Assignment of Literals Blocking Clause SAT UNSAT
DPLL(T) Decide Start full assignment SAT BCP/PL partial assignment Analyze Conflict conflict UNSAT Learn & Backtrack Theory Solver Add Clauses partial assignment theory propagation / conflict partial assignment
Scope of Solvers propositional logic SAT solvers first order logic theory of equality difference logic Theorem provers SMT solvers linear integer arithmetic … theory of arrays
Summary 75
Self-check: learning targets Institute for Applied Information Processing and Communications 76 Explain Satisfiability Modulo Theories Describe Theory of Uninterpreted Functions and Equality Explain and use Ackermann’s Reduction Graph-based Reduction Congruence Closure DPLL DPLL(T)
History of satisfiability: pdfhttp://gauss.ececs.uc.edu/SAT/articles/FAIA pdf SAT basics: Conflict Driven Clause Learning: Armin Biere’s slides: SAT game artois.fr/~roussel/satgame/satgame.php?level=1&lang=enghttp:// artois.fr/~roussel/satgame/satgame.php?level=1&lang=eng Logic and Computability classes by Georg echenbarkeit/ echenbarkeit/ Institute for Applied Information Processing and Communications 77 some reading