Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming...

55
Introduction to SAT, Predicate Logic and DPLL solving Lecture 1 Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson

Transcript of Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming...

Page 1: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Introduction to SAT, Predicate Logic and DPLL solving

Lecture 1Nikolaj Bjørner Microsoft ResearchDTU Winter course January 2nd 2012Organized by Flemming Nielson & Hanne Riis Nielson

Page 2: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Plan

General overview of propositional and predicate logic

Refresher on SAT and modern DPLL

Page 3: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Takeaways :

The syntax and semantics of propositional and predicate logic.

Algorithmic principles of modern SAT solvers:DPLL (actually DLL) algorithmConflict Directed Clause Learning (CDCL)Two-watch literal indexing

Page 4: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

What is logic?

Logic is the art and science of effective reasoning.How can we draw general and reliable conclusions from a collection of facts?Formal logic: Precise, syntactic characterizations of well-formed expressions and valid deductions.Formal logic makes it possible to calculate consequences at the symbolic level.

Computers can be used to automate such symbolic calculations.

Page 5: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

What is logic?

Logic studies the relationship between language, meaning, and (proof) method. A logic consists of a language in which (well-formed) sentences are expressed. A semantic that distinguishes the valid sentences from the refutable ones.A proof system for constructing arguments justifying valid sentences.Examples of logics include propositional logic, equational logic, first-order logic, higher-order logic, and modal logics.

Page 6: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

What is logical language?

A language consists of logical symbols whose interpretations are fixed, and non-logical ones whose interpretations vary. These symbols are combined together to form well-formed formulas.In propositional logic PL, the connectives , , and have a fixed interpretation, whereas the constants p, q, r may be interpreted at will.

Page 7: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

SAT

Theory

Solvers

SMT

SMT : Basic Architecture

Equality + UFArithmeticBit-vectors…

Case Analysis

Page 8: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Propositional Logic

Formulas: := p | 1 2 | 1 2 | 1 | 1 2

Examples:p q q pp q (p q)

We say p and q are propositional variables.

Page 9: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Interpretation

An interpretation M assigns values {true, false} to propositional variables.

Let F and G range over PL formulas.

Page 10: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Satisfiability & ValidityA formula is satisfiable if it has an interpretation that makes it logically true. In this case, we say the interpretation is a model.A formula is unsatisfiable if it does not have any model.A formula is valid if it is logically true in any interpretation.A propositional formula is valid if and only if its negation is unsatisfiable.

Page 11: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Satisfiability & Validity: examplesp q q p

p q q

p q (p q)

Page 12: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Satisfiability & Validity: examplesp q q p VALID

p q q SATISFIABLE

p q (p q) UNSATISFIABLE

Page 13: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Equivalence

We say two formulas F and G are equivalent if and only if they evaluate to the same value (true or false) in every interpretation

Page 14: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Equisatisfiable

We say formulas A and B are equisatisfiable if and only if A is satisfiable if and only if B is.

During this tutorial, we will describe transformations that preserve equivalence and equisatisfiability.

Page 15: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

Page 16: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

NNF?(p q) (q (r p))

Page 17: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

NNF? NO(p q) (q (r p))

Page 18: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

NNF? NO(p q) (q (r p))

Page 19: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

NNF? NO(p q) (q (r p))(p q) (q (r p))

Page 20: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

NNF? NO(p q) (q (r p))(p q) (q (r p))(p q) (q (r p))

Page 21: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

CNF?((p s) (q r)) (q p s) (r s)

Page 22: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

CNF? NO((p s) (q r)) (q p s) (r s)

Page 23: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

CNF? NO((p s) (q r)) (q p s) (r s)

Distributivity 1. A(BC) (AB)(AC)2. A(BC) (AB)(AC)

Page 24: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

CNF? NO((p s) (q r)) (q p s) (r s)((p s) q)) ((p s) r)) (q p s) (r s)

Distributivity 1. A(BC) (AB)(AC)2. A(BC) (AB)(AC)

Page 25: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

CNF? NO((p s) (q r)) (q p s) (r s)((p s) q)) ((p s) r)) (q p s) (r s)(p q) (s q) ((p s) r)) (q p s) (r s)

Distributivity 1. A(BC) (AB)(AC)2. A(BC) (AB)(AC)

Page 26: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

CNF? NO((p s) (q r)) (q p s) (r s)((p s) q)) ((p s) r)) (q p s) (r s)(p q) (s q) ((p s) r)) (q p s) (r s)(p q) (s q) (p r) (s r) (q p s) (r s)

Page 27: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

DNF? p (p q) (q r)

Page 28: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

DNF? NO, actually this formula is in CNF p (p q) (q r)

Page 29: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

DNF? NO, actually this formula is in CNF p (p q) (q r)

Distributivity 1. A(BC) (AB)(AC)2. A(BC) (AB)(AC)

Page 30: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

DNF? NO, actually this formula is in CNF p (p q) (q r)((p p) (p q)) (q r)

Distributivity 1. A(BC) (AB)(AC)2. A(BC) (AB)(AC)

Page 31: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

DNF? NO, actually this formula is in CNF p (p q) (q r)((p p) (p q)) (q r)(p q) (q r)

Distributivity 1. A(BC) (AB)(AC)2. A(BC) (AB)(AC)Other Rules1. AA 2. A A

Page 32: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Normal Forms

DNF? NO, actually this formula is in CNF p (p q) (q r)((p p) (p q)) (q r)(p q) (q r)((p q) q) ((p q) r)

Distributivity 1. A(BC) (AB)(AC)2. A(BC) (AB)(AC)Other Rules1. AA 2. A A

Page 33: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Efficient CNF Translation

Page 34: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

CNF translation (example)

Page 35: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Resolution

Page 36: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Resolution (example)

Page 37: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Unit & Input Resolution

Page 38: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

DPLL

DPLL

Page 39: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Pure LiteralsA literal is pure if only occurs positively or negatively.

Page 40: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Pure LiteralsA literal is pure if only occurs positively or negatively.

Page 41: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

DPLL (as a procedure)

Page 42: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

DPLL

M | F

Partial model Set of clauses

Page 43: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

DPLL

Guessing

p, q | p q, q r

p | p q, q r

Page 44: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

DPLL

Deducing

p, s| p q, p s

p | p q, p s

Page 45: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

DPLL

Backtracking

p, s| p q, s q, p q

p, s, q | p q, s q, p q

Page 46: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Modern DPLL

Efficient indexing (two-watch literal)Non-chronological backtracking (backjumping)Lemma learning

Page 47: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Modern DPLL in a nutshell

Initialize

Decide

Propagate

Conflict

Resolve

Learn

Backjump

Unsat

Sat Restart

Adapted and modified from [Nieuwenhuis, Oliveras, Tinelli J.ACM 06]

Page 48: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

CDCL – Conflict Directed Clause Learning

Lemma learning

t, p, q, s | t p q, q s, p s |p s

t, p, q, s | t p q, q s, p s

t, p, q, s | t p q, q s, p s |p q

t, p, q, s | t p q, q s, p s |p t

Page 49: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Two-watch literals

• Indexing is key to efficient theorem proving.• An index is a dictionary that is tuned to search.

• The literal two-watch scheme is the main index in modern SAT solvers.• Track two literals per clause, such that:• Track literals that are unassigned or assigned to true.• If one of the tracked literals is assigned to false, search for

non-tracked literal that is either unassigned or true.• If there is no other unassigned or true literal to select, then

the other watched literal can be assigned to true.• Unit propagation or conflict detection.

Page 50: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Two-watch literalsWatch literals for Propagate and Conflict

Naïve: For every literal l maintain map:Watch(l) = {C1 … Cm} where l Ci

If l is assigned to true, check each Cj Watch(l) for Conflict or Propagate

But most of the time, some other literal in Cj is either:Unassigned (not yet assigned)Assigned to true.

Page 51: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Two watch literalsInsight:

No need to include clause C in every set Watch(l) where l C.

It suffices to include C in at most 2 such sets.

Maintain invariant:If some literal l in C unassigned, or assigned to true, then C belongs to the Watch(l’) of some literal that is unassigned or true.

Page 52: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Modern DPLL - implementation

Page 53: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Two watch literalsMaintain 2-watch invariant:

Set l to true (l to false). For each C Watch(l)

If all literals in C are assigned to false, then BackjumpElse, if all but one literal in C is assigned to false, then PropagateElse, if the other literal in l’ C where C Watch(l’) is assigned to true, then do nothing.Else, some other literal l’ is true or unassigned, and not watched. Set Watch(l’) Watch(l’) { C }, set Watch(l) Watch(l) \ { C }.

Page 54: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Phase cachingHeuristic: Phase caching

Remember the last truth value assigned to propositional atom.If using rule Decide, then re-use the old assignment.

Why should this be good (in practice)?Dependencies follow clusters.Truth values in a cluster are dependent.Truth values between clusters are independent.Decide is mainly used when jumping between clusters.

Page 55: Nikolaj Bjørner Microsoft Research DTU Winter course January 2 nd 2012 Organized by Flemming Nielson & Hanne Riis Nielson.

Modern DPLL - tuningTune between different heuristics:

Restart frequencyWhy is restarting good?

Phase to assign to decision variableWhich variable to split on

Use simulated annealing based on activity in conflictsFeedback factor from phase changes

Which lemmas to learnNot necessarily uniqueMinimize lemmasSub-sumption

Blocked clause eliminationCache binary propagations