Delft University of Technology - Department of Software Technology

Computational Logic
and Satisfiability
IN4077
Applications
Marijn J.H. Heule
September 9, 2008
1
Delft University of Technology
Delft University of Technology
Overview
•
Equivalence checking
• Hardware and software optimization
•
Bounded model checking
• Hardware and software verification
•
Arithmetic operations
• Factorization, term rewriting
•
Graph coloring
• Sudoku, timetabling
September 9, 2008
Delft University of Technology
2
Equivalence checking introduction
Given two formulae, are they equivalent?
Applications:
•
Hardware and software optimization
•
Software to FPGA conversion
September 9, 2008
Delft University of Technology
3
Equivalence checking example
original C code
if(!a && !b) h();
else if(!a) g();
else f();
September 9, 2008
Delft University of Technology
4
Equivalence checking example
original C code
if(!a && !b) h();
else if(!a) g();
else f();
⇓
if(!a) {
if(!b) h();
else g(); }
else f();
September 9, 2008
Delft University of Technology
4
Equivalence checking example
original C code
if(!a && !b) h();
else if(!a) g();
else f();
⇓
if(!a) {
if(!b) h();
else g(); }
else f();
September 9, 2008
Delft University of Technology
⇒
if(a) f();
else {
if(!b) h();
else g(); }
4
Equivalence checking example
original C code
optimized C code
if(a) f();
else if(b) g();
else h();
if(!a && !b) h();
else if(!a) g();
else f();
⇓
if(!a) {
if(!b) h();
else g(); }
else f();
September 9, 2008
Delft University of Technology
⇑
⇒
if(a) f();
else {
if(!b) h();
else g(); }
4
Equivalence checking example
original C code
optimized C code
if(a) f();
else if(b) g();
else h();
if(!a && !b) h();
else if(!a) g();
else f();
⇓
if(!a) {
if(!b) h();
else g(); }
else f();
⇑
⇒
if(a) f();
else {
if(!b) h();
else g(); }
How to check that these two versions are equivalent?
September 9, 2008
Delft University of Technology
4
Equivalence checking encoding (1)
1. represent procedures as independent Boolean variables
original C code
:=
if ¬a ∧ ¬b then h
else if ¬a then g
else f
September 9, 2008
Delft University of Technology
optimized C code
:=
if a then f
else if b then g
else h
5
Equivalence checking encoding (1)
1. represent procedures as independent Boolean variables
original C code
:=
if ¬a ∧ ¬b then h
else if ¬a then g
else f
optimized C code
:=
if a then f
else if b then g
else h
2. compile if-then-else into Conjunctive Normal Form
compile(if x then y else z) ≡ (¬x ∨ y) ∧ (x ∨ z)
September 9, 2008
Delft University of Technology
5
Equivalence checking encoding (1)
1. represent procedures as independent Boolean variables
original C code
:=
if ¬a ∧ ¬b then h
else if ¬a then g
else f
optimized C code
:=
if a then f
else if b then g
else h
2. compile if-then-else into Conjunctive Normal Form
compile(if x then y else z) ≡ (¬x ∨ y) ∧ (x ∨ z)
3. check equivalence of Boolean formulae
compile(original C code) ⇔ compile(optimized C code)
September 9, 2008
Delft University of Technology
5
Equivalence checking encoding (2)
compile(original C code):
if ¬a ∧ ¬b then h else if ¬a then g else f
≡
(¬(¬a ∧ ¬b) ∨ h) ∨ ((¬a ∧ ¬b) ∨ (if ¬a then g else f )) ≡
(a ∨ b ∨ h) ∨ ((¬a ∧ ¬b) ∨ ((a ∨ g) ∧ (¬a ∨ f ))
September 9, 2008
Delft University of Technology
6
Equivalence checking encoding (2)
compile(original C code):
if ¬a ∧ ¬b then h else if ¬a then g else f
≡
(¬(¬a ∧ ¬b) ∨ h) ∨ ((¬a ∧ ¬b) ∨ (if ¬a then g else f )) ≡
(a ∨ b ∨ h) ∨ ((¬a ∧ ¬b) ∨ ((a ∨ g) ∧ (¬a ∨ f ))
compile(optimized C code):
if a then f else if b then g else h ≡
(¬a ∨ f ) ∧ (a ∨ (if b then g else h)) ≡
(¬a ∨ f ) ∧ (a ∨ ((¬b ∨ g) ∧ (b ∨ h))
September 9, 2008
Delft University of Technology
6
Equivalence checking encoding (2)
compile(original C code):
if ¬a ∧ ¬b then h else if ¬a then g else f
≡
(¬(¬a ∧ ¬b) ∨ h) ∨ ((¬a ∧ ¬b) ∨ (if ¬a then g else f )) ≡
(a ∨ b ∨ h) ∨ ((¬a ∧ ¬b) ∨ ((a ∨ g) ∧ (¬a ∨ f ))
compile(optimized C code):
if a then f else if b then g else h ≡
(¬a ∨ f ) ∧ (a ∨ (if b then g else h)) ≡
(¬a ∨ f ) ∧ (a ∨ ((¬b ∨ g) ∧ (b ∨ h))
(a∨b∨h)∨((¬a∧¬b)∨((a∨g)∧(¬a∨f )) ⇔ (¬a∨f )∧(a∨((¬b∨g)∧(b∨h))
September 9, 2008
Delft University of Technology
6
Checking (in)equivalence
Reformulate it as a satisfiability (SAT) problem:
Is there an assignment to a, b, f , g, and h, which results in
different evaluations of the compiled codes?
September 9, 2008
Delft University of Technology
7
Checking (in)equivalence
Reformulate it as a satisfiability (SAT) problem:
Is there an assignment to a, b, f , g, and h, which results in
different evaluations of the compiled codes?
or equivalently:
Is the Boolean formula
compile(original C code) < compile(optimized C code)
satisfiable?
Such an assignment would provide a counterexample
September 9, 2008
Delft University of Technology
7
Checking (in)equivalence
Reformulate it as a satisfiability (SAT) problem:
Is there an assignment to a, b, f , g, and h, which results in
different evaluations of the compiled codes?
or equivalently:
Is the Boolean formula
compile(original C code) < compile(optimized C code)
satisfiable?
Such an assignment would provide a counterexample
Note: by concentrating on counterexamples we moved
from Co-NP to NP (not really important for applications)
September 9, 2008
Delft University of Technology
7
Bounded Model Checking (BMC)
Given a property p: (e.g. signal a = signal b)
September 9, 2008
Delft University of Technology
8
Bounded Model Checking (BMC)
Given a property p: (e.g. signal a = signal b)
Is there a state reachable in k cycles, which satisfies ¬p ?
p
p
p
p
¬p
p
S0
S1
S2
S3
Sk−1
Sk
September 9, 2008
Delft University of Technology
8
Bounded Model Checking (BMC)
Given a property p: (e.g. signal a = signal b)
Is there a state reachable in k cycles, which satisfies ¬p ?
p
p
p
p
¬p
p
S0
S1
S2
S3
Sk−1
Sk
Turing award 2007 for Edmund M. Clarke
September 9, 2008
Delft University of Technology
8
BMC Encoding (1)
The reachable states in k steps are captured by:
I(S0 ) ∧ T (S0 , S1 ) ∧ · · · ∧ T (Sk−1 , Sk )
The property p fails in one of the k steps by:
¬P (S0 ) ∨ ¬P (S1 ) ∨ · · · ∨ ¬P (Sk )
September 9, 2008
Delft University of Technology
9
BMC Encoding (2)
The safety property p is valid up to step k
if and only if F(k) is unsatisfiable:
F(k) = I(S0 ) ∧
k−1
^
T (Si , Si+1 )) ∧
i=0
k
_
¬P (Si )
i=0
p
p
p
p
¬p
p
S0
S1
S2
S3
Sk−1
Sk
September 9, 2008
Delft University of Technology
10
Bounded model checking Example
Two bit counter
00
11
Initial state I:
Transition T :
01
10
Property P :
September 9, 2008
Delft University of Technology
l0 = 0, r0 = 0
li+1 = li ⊕ ri ,
ri+1 = ¬ri
¬li ∨ ¬ri
11
Bounded model checking Example
Two bit counter
00
11
Initial state I:
Transition T :
01
10
Property P :

F(2) = (¬l0 ∧¬r0 )∧
l0 = 0, r0 = 0
li+1 = li ⊕ ri ,
ri+1 = ¬ri
¬li ∨ ¬ri
l1 = l0 ⊕ r0 ∧ r1 = ¬r0 ∧
l2 = l1 ⊕ r1 ∧ r2 = ¬r1
September 9, 2008
Delft University of Technology


(¬l0 ∨ ¬r0 ) ∧



∧ (¬l1 ∨ ¬r1 ) ∧ 


(¬l2 ∨ ¬r2 )
11
Bounded model checking Example
Two bit counter
00
11
Initial state I:
Transition T :
01
10
Property P :

F(2) = (¬l0 ∧¬r0 )∧
l0 = 0, r0 = 0
li+1 = li ⊕ ri ,
ri+1 = ¬ri
¬li ∨ ¬ri
l1 = l0 ⊕ r0 ∧ r1 = ¬r0 ∧
l2 = l1 ⊕ r1 ∧ r2 = ¬r1


(¬l0 ∨ ¬r0 ) ∧



∧ (¬l1 ∨ ¬r1 ) ∧ 


(¬l2 ∨ ¬r2 )
For k = 2, F (k) is unsatisfiable; for k = 3 it is satisfiable
September 9, 2008
Delft University of Technology
11
Arithmetic operations: Introduction
How to encode arithmetic operations into SAT?
September 9, 2008
Delft University of Technology
12
Arithmetic operations: Introduction
How to encode arithmetic operations into SAT?
Efficient encoding using electronic circuits
September 9, 2008
Delft University of Technology
12
Arithmetic operations: Introduction
How to encode arithmetic operations into SAT?
Efficient encoding using electronic circuits
Applications:
•
factorization (not competitive)
•
term rewriting
September 9, 2008
Delft University of Technology
12
4x4 Multiplier circuit
September 9, 2008
Delft University of Technology
13
Multiplier encoding
1. Multiplication mi,j = xi × yj
(mi,j ∨ ¬xi ∨ ¬yj ) ∧ (¬mi,j ∨ xi ) ∧ (¬mi,j ∨ yj )
September 9, 2008
Delft University of Technology
14
Multiplier encoding
1. Multiplication mi,j = xi × yj
(mi,j ∨ ¬xi ∨ ¬yj ) ∧ (¬mi,j ∨ xi ) ∧ (¬mi,j ∨ yj )
2. Carry out cout = 1 if and only if pin + mi,j + cin > 1
(cout ∨ ¬pin ∨ ¬mi,j ) ∧ (cout ∨ ¬pin ∨ ¬cin ) ∧ (cout ∨ ¬mi,j ∨ ¬cin )
September 9, 2008
Delft University of Technology
14
Multiplier encoding
1. Multiplication mi,j = xi × yj
(mi,j ∨ ¬xi ∨ ¬yj ) ∧ (¬mi,j ∨ xi ) ∧ (¬mi,j ∨ yj )
2. Carry out cout = 1 if and only if pin + mi,j + cin > 1
(cout ∨ ¬pin ∨ ¬mi,j ) ∧ (cout ∨ ¬pin ∨ ¬cin ) ∧ (cout ∨ ¬mi,j ∨ ¬cin )
3. Parity out pout of variables pin , mi,j and cin
(pout ∨ ¬pin ∨ ¬mi,j ∨ ¬cin ) ∧
(¬pout ∨ pin ∨ ¬mi,j ∨ ¬cin ) ∧
(¬pout ∨ ¬pin ∨ mi,j ∨ ¬cin ) ∧
(¬pout ∨ ¬pin ∨ ¬mi,j ∨ cin ) ∧
September 9, 2008
Delft University of Technology
(pout ∨ pin ∨ mi,j ∨ ¬cin ) ∧
(pout ∨ pin ∨ ¬mi,j ∨ cin ) ∧
(pout ∨ ¬pin ∨ mi,j ∨ cin ) ∧
(¬pout ∨ pin ∨ mi,j ∨ cin )
14
Arithmetic operations: Is 27 prime?
x3 x2 x1 x0
x3 y0 x2 y0 x1 y0 x0 y0
x3 y1 x2 y1 x1 y1 x0 y1
x3 y2 x2 y2 x1 y2 x0 y2
x3 y3 x2 y3 x1 y3 x0 y3
0
0
1
September 9, 2008
Delft University of Technology
1
0
1
y0
y1
y2
y3
1
15
Arithmetic operations: Is 27 prime?
x3 x2 x1 x0
x3 y0 x2 y0 x1 y0 x0 y0
x3 y1 x2 y1 x1 y1 x0 y1
x3 y2 x2 y2 x1 y2 x0 y2
x3 y3 x2 y3 x1 y3 x0 y3
0
0
1
1
0
1
y0
y1
y2
y3
1
Prime: (x1 ∨ x2 ∨ x3 ) ∧ (y1 ∨ y2 ∨ y3 )
September 9, 2008
Delft University of Technology
15
Arithmetic operations: Is 27 prime?
x3 x2 x1 x0
x3 y0 x2 y0 x1 y0 x0 y0
x3 y1 x2 y1 x1 y1 x0 y1
x3 y2 x2 y2 x1 y2 x0 y2
x3 y3 x2 y3 x1 y3 x0 y3
0
0
1
1
0
1
y0
y1
y2
y3
1
Prime: (x1 ∨ x2 ∨ x3 ) ∧ (y1 ∨ y2 ∨ y3 )
September 9, 2008
Delft University of Technology
15
Arithmetic operations: Is 29 prime?
x3 x2 x1 x0
x3 y0 x2 y0 x1 y0 x0 y0
x3 y1 x2 y1 x1 y1 x0 y1
x3 y2 x2 y2 x1 y2 x0 y2
x3 y3 x2 y3 x1 y3 x0 y3
0
0
1
1
1
0
y0
y1
y2
y3
1
Prime: (x1 ∨ x2 ∨ x3 ) ∧ (y1 ∨ y2 ∨ y3 )
September 9, 2008
Delft University of Technology
16
Arithmetic operations: Is 29 prime?
x3 x2 x1 x0
x3 y0 x2 y0 x1 y0 x0 y0
x3 y1 x2 y1 x1 y1 x0 y1
x3 y2 x2 y2 x1 y2 x0 y2
x3 y3 x2 y3 x1 y3 x0 y3
0
0
1
1
1
0
y0
y1
y2
y3
1
Prime: (x1 ∨ x2 ∨ x3 ) ∧ (y1 ∨ y2 ∨ y3 )
September 9, 2008
Delft University of Technology
16
Arithmetic operations: Term rewriting
Given a set of rewriting rules,
will rewriting always terminate?
September 9, 2008
Delft University of Technology
17
Arithmetic operations: Term rewriting
Given a set of rewriting rules,
will rewriting always terminate?
Example set of rules:
aa → bc
bb → ca
cc → ab
September 9, 2008
Delft University of Technology
17
Arithmetic operations: Term rewriting
Given a set of rewriting rules,
will rewriting always terminate?
Example set of rules:
aa → bc
bb → ca
cc → ab
Strongest rewriting solvers use SAT (e.g. aprove)
Example solved by Hofbauer, Waldmann (2006)
September 9, 2008
Delft University of Technology
17
Combinatorial problems
Encoding is critical when dealing with hard
combinatorial problems
Problems are (relatively) small but very hard
The most compact representation is not
necessarily the best performing
Mostly based on graph coloring encoding
September 9, 2008
Delft University of Technology
18
Graph coloring
Given a graph G(V, E), can the vertices be
colored with k colors such that of each
(v, w) ∈ E, v and w are colored differently.
Problem: Many symmetries!!!
September 9, 2008
Delft University of Technology
19
Graph coloring encoding
Variables
xi,j
Clauses
Range
Meaning
i ∈ {1, . . . , c}
node j has color i
j ∈ {1, . . . , |V |}
Range
x1,j ∨ x2,j ∨ · · · ∨ xc,j j ∈ {1, . . . , |V |}
Meaning
j is colored
¬xs,j ∨ ¬xt,j
s ∈ {1, . . . , c − 1}
t ∈ {s + 1, . . . , c}
j has at most
one color
¬xi,v ∨ ¬xi,w
(v, w) ∈ E
v and w have a
different color
???
???
breaking symmetry
September 9, 2008
Delft University of Technology
20
Sudoku
September 9, 2008
Delft University of Technology
21
4x4 Sudoku clauses
2
1
2
1
4
September 9, 2008
Delft University of Technology
22
4x4 Sudoku clauses
2
∀i, n : xi,1,n ∨xi,2,n ∨xi,3,n ∨xi,4,n
1
2
1
4
September 9, 2008
Delft University of Technology
22
4x4 Sudoku clauses
2
1
2
1
4
September 9, 2008
Delft University of Technology
∀i, n : xi,1,n ∨xi,2,n ∨xi,3,n ∨xi,4,n

¬x1,1,n ∨ ¬x1,2,n 



¬x1,1,n ∨ ¬x1,3,n 




¬x1,1,n ∨ ¬x1,4,n 

∀n : ¬x1,1,n ∨ ¬x2,1,n


¬x1,1,n ∨ ¬x3,1,n 




¬x1,1,n ∨ ¬x4,1,n 



¬x1,1,n ∨ ¬x2,2,n
22
4x4 Sudoku clauses
2
1
2
1
4
September 9, 2008
Delft University of Technology
∀i, n : xi,1,n ∨xi,2,n ∨xi,3,n ∨xi,4,n

¬x1,1,n ∨ ¬x1,2,n 



¬x1,1,n ∨ ¬x1,3,n 




¬x1,1,n ∨ ¬x1,4,n 

∀n : ¬x1,1,n ∨ ¬x2,1,n × 16


¬x1,1,n ∨ ¬x3,1,n 




¬x1,1,n ∨ ¬x4,1,n 



¬x1,1,n ∨ ¬x2,2,n
22
Timetables (1)
Suitable for SAT solving
September 9, 2008
Delft University of Technology
23
Timetables (2)
Not suitable for SAT solving
September 9, 2008
Delft University of Technology
24
Computational Logic
and Satisfiability
IN4077
Applications
Marijn J.H. Heule
September 9, 2008
25
Delft University of Technology
Delft University of Technology