12.03.2014 Views

# SAT and Termination

SAT and Termination

### Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong><br />

Nao Hirokawa<br />

Japan Advanced Institute of Science <strong>and</strong> Technology<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 1/41

given 9 × 9-grid like<br />

Sudoku Puzzle<br />

1 8 7<br />

3 2<br />

7<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

fill out numbers from 1 to 9 that each number appears exactly once in<br />

each {row, column, 3 × 3-subgrid}<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 2/41

given 9 × 9-grid like<br />

Sudoku Puzzle<br />

1 8 7<br />

3 2<br />

7<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

fill out numbers from 1 to 9 that each number appears exactly once in<br />

each {row, column, 3 × 3-subgrid}<br />

how to solve?<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 2/41

given 9 × 9-grid like<br />

Sudoku Puzzle<br />

1 8 7<br />

3 2<br />

7<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

fill out numbers from 1 to 9 that each number appears exactly once in<br />

each {row, column, 3 × 3-subgrid}<br />

how to solve? — difficult?<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 2/41

given 9 × 9-grid like<br />

Sudoku Puzzle<br />

1 8 7<br />

3 2<br />

7<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

fill out numbers from 1 to 9 that each number appears exactly once in<br />

each {row, column, 3 × 3-subgrid}<br />

how to solve? — difficult? — NP-complete<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 2/41

x 11<br />

Solving Sudoku<br />

1 8 7<br />

x 21 x 22 x 23 3 2<br />

x 31 7 x 33<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 3/41

Solving Sudoku<br />

Lemma<br />

x 33 = 3<br />

Proof<br />

x 11<br />

1 8 7<br />

x 21 x 22 x 23 3 2<br />

x 31 7 x 33<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

{x 11 , x 21 , x 22 , x 23 , x 31 , x 33 } = {2, 3, 4, 5, 6, 9} by subgrid constraint<br />

{x 11 , x 21 , x 22 , x 23 , x 31 } = {2, 4, 5, 6, 9} by row & column constraint<br />

how about x 26 ?<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 3/41

Solving Sudoku<br />

1 8 7<br />

x 21 x 22 x 23 3 x 25 x 26 2 x 28 x 29<br />

7 3<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 4/41

Solving Sudoku<br />

Lemma<br />

x 26 = 7<br />

1 8 7<br />

x 21 x 22 x 23 3 x 25 x 26 2 x 28 x 29<br />

7 3<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 4/41

Solving Sudoku<br />

Lemma<br />

x 26 = 7<br />

1 8 7<br />

x 21 x 22 x 23 3 x 25 x 26 2 x 28 x 29<br />

7 3<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

Proof<br />

{x 21 , x 22 , x 23 , x 25 , x 26 , x 28 , x 29 } = {1, 4, 5, 6, 7, 8, 9}<br />

by row constraint<br />

7 ∉ {x 21 , x 22 , x 23 , x 25 , x 28 , x 29 } by column & subgrid constraint<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 4/41

Overview<br />

<strong>SAT</strong><br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 5/41

Overview<br />

<strong>SAT</strong><br />

encoding techniques<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 5/41

Overview<br />

<strong>SAT</strong><br />

encoding techniques<br />

termination analysis<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 5/41

<strong>SAT</strong><br />

Definition<br />

syntax<br />

l ::= x | ¬x<br />

C ::= l 1 ∨ · · · ∨ l n<br />

φ ::= C 1 ∧ · · · ∧ C n<br />

literal<br />

clause<br />

CNF<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 6/41

<strong>SAT</strong><br />

Definition<br />

syntax<br />

l ::= x | ¬x<br />

C ::= l 1 ∨ · · · ∨ l n<br />

φ ::= C 1 ∧ · · · ∧ C n<br />

literal<br />

clause<br />

CNF<br />

<strong>SAT</strong> problem is decision problem of scheme:<br />

instance:<br />

CNF φ<br />

question: is φ satisfiable ?<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 6/41

<strong>SAT</strong><br />

Definition<br />

syntax<br />

l ::= x | ¬x<br />

C ::= l 1 ∨ · · · ∨ l n<br />

φ ::= C 1 ∧ · · · ∧ C n<br />

literal<br />

clause<br />

CNF<br />

<strong>SAT</strong> problem is decision problem of scheme:<br />

instance:<br />

CNF φ<br />

question: is φ satisfiable ?<br />

Theorem<br />

Cook <strong>and</strong> Levin<br />

<strong>SAT</strong> is NP-complete<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 6/41

Quiz<br />

is next CNF φ satisfiable?<br />

⎧<br />

∧<br />

⎪⎨<br />

⎪⎩<br />

x 1 ∨ ¬x 2 ,<br />

¬x 1 ∨ x 2 ∨ x 3 ,<br />

¬x 2 ∨ x 3 ,<br />

¬x 3<br />

⎫⎪ ⎬<br />

⎪ ⎭<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 7/41

Quiz<br />

is next CNF φ satisfiable?<br />

⎧<br />

∧<br />

⎪⎨<br />

⎪⎩<br />

x 1 ∨ ¬x 2 ,<br />

¬x 1 ∨ x 2 ∨ x 3 ,<br />

¬x 2 ∨ x 3 ,<br />

¬x 3<br />

⎫⎪ ⎬<br />

⎪ ⎭<br />

φ is satisfiable<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 7/41

Quiz<br />

is next CNF φ satisfiable?<br />

⎧<br />

∧<br />

⎪⎨<br />

⎪⎩<br />

x 1 ∨ ¬x 2 ,<br />

¬x 1 ∨ x 2 ∨ x 3 ,<br />

¬x 2 ∨ x 3 ,<br />

¬x 3<br />

⎫⎪ ⎬<br />

⎪ ⎭<br />

φ is satisfiable because of next satisfiable assignment<br />

x 1 ↦→ F x 2 ↦→ F x 3 ↦→ F<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 7/41

Quiz<br />

is next CNF φ satisfiable?<br />

⎧<br />

∧<br />

⎪⎨<br />

⎪⎩<br />

x 1 ∨ ¬x 2 ,<br />

¬x 1 ∨ ¬x 2 ∨ x 3 ,<br />

¬x 2 ∨ x 3 ,<br />

¬x 3<br />

⎫⎪ ⎬<br />

⎪ ⎭<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 8/41

Quiz<br />

is next CNF φ satisfiable?<br />

⎧<br />

∧<br />

⎪⎨<br />

⎪⎩<br />

x 1 ∨ ¬x 2 ,<br />

¬x 1 ∨ ¬x 2 ∨ x 3 ,<br />

¬x 2 ∨ x 3 ,<br />

¬x 3<br />

⎫⎪ ⎬<br />

⎪ ⎭<br />

φ is unsatisfiable.<br />

why?<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 8/41

Quiz<br />

is next CNF φ satisfiable?<br />

⎧<br />

∧<br />

⎪⎨<br />

⎪⎩<br />

x 1 ∨ ¬x 2 ,<br />

¬x 1 ∨ ¬x 2 ∨ x 3 ,<br />

¬x 2 ∨ x 3 ,<br />

¬x 3<br />

⎫⎪ ⎬<br />

⎪ ⎭<br />

φ is unsatisfiable.<br />

why?<br />

because<br />

exhaustive search shows that any assignment is unsatisfiable, or<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 8/41

Quiz<br />

is next CNF φ satisfiable?<br />

⎧<br />

∧<br />

⎪⎨<br />

⎪⎩<br />

x 1 ∨ ¬x 2 ,<br />

¬x 1 ∨ ¬x 2 ∨ x 3 ,<br />

¬x 2 ∨ x 3 ,<br />

¬x 3<br />

⎫⎪ ⎬<br />

⎪ ⎭<br />

φ is unsatisfiable.<br />

why?<br />

because<br />

exhaustive search shows that any assignment is unsatisfiable, or<br />

deduction derives φ is ⊥<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 8/41

<strong>SAT</strong> Solver <strong>and</strong> DIMACS Format<br />

is ∧ ⎧⎪ ⎨<br />

⎪ ⎩<br />

x 1 ∨ ¬x 2 ,<br />

¬x 1 ∨ x 2 ∨ ¬x 3 ,<br />

x 2 ∨ ¬x 3 ,<br />

¬x 3<br />

⎫⎪ ⎬<br />

⎪ ⎭<br />

satisfiable ?<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 9/41

<strong>SAT</strong> Solver <strong>and</strong> DIMACS Format<br />

is ∧ ⎧⎪ ⎨<br />

⎪ ⎩<br />

x 1 ∨ ¬x 2 ,<br />

¬x 1 ∨ x 2 ∨ ¬x 3 ,<br />

x 2 ∨ ¬x 3 ,<br />

¬x 3<br />

⎫⎪ ⎬<br />

⎪ ⎭<br />

satisfiable ?<br />

\$ cat a.cnf<br />

p cnf 3 #variables<br />

4 #clauses<br />

1 -2 0<br />

-1 2 -3 0<br />

3 -2 0<br />

-3 0<br />

\$ minisat a.cnf a.ans<br />

\$ cat a.ans<br />

<strong>SAT</strong><br />

1 -2 -3 0<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 9/41

Sudoku Constraints<br />

1 8 7<br />

3 2<br />

7<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

constraints<br />

each cell is number from 1 to 9<br />

each number appears at most once in each row<br />

each number appears at most once in each column<br />

each number appears at most once in each 3 × 3 subgrid<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 10/41

Sudoku Constraints<br />

1 8 7<br />

3 2<br />

7<br />

7 1<br />

6 4<br />

3<br />

4 5 3<br />

2 8<br />

6<br />

constraints<br />

each cell is number from 1 to 9<br />

each number appears at most once in each row<br />

each number appears at most once in each column<br />

each number appears at most once in each 3 × 3 subgrid<br />

☞ use s 111 , . . . , s 999 with s ijk = T ⇐⇒ x ij = k<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 10/41

Sudoku Constraints in Boolean<br />

each cell is number from 1 to 9<br />

9∧ 9∧<br />

(s ij1 ∨ · · · ∨ s ij9 )<br />

i=1 j=1<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 11/41

Sudoku Constraints in Boolean<br />

each cell is number from 1 to 9<br />

9∧ 9∧<br />

(s ij1 ∨ · · · ∨ s ij9 )<br />

i=1 j=1<br />

each number appears at most once in each row<br />

9∧ 9∧ 9∧ 9∧<br />

(¬s ijk ∨ ¬s i ′ jk)<br />

j=1 k=1 i=1 i ′ =i+1<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 11/41

Sudoku Constraints in Boolean<br />

each cell is number from 1 to 9<br />

9∧ 9∧<br />

(s ij1 ∨ · · · ∨ s ij9 )<br />

i=1 j=1<br />

each number appears at most once in each row<br />

9∧ 9∧ 9∧ 9∧<br />

(¬s ijk ∨ ¬s i ′ jk)<br />

j=1 k=1 i=1 i ′ =i+1<br />

each number appears at most once in each column<br />

9∧ 9∧ 9∧ 9∧<br />

(¬s ijk ∨ ¬s ij ′ k)<br />

i=1 k=1 j=1 j ′ =j+1<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 11/41

Sudoku Constraints in Boolean<br />

each cell is number from 1 to 9<br />

9∧ 9∧<br />

(s ij1 ∨ · · · ∨ s ij9 )<br />

i=1 j=1<br />

each number appears at most once in each row<br />

9∧ 9∧ 9∧ 9∧<br />

(¬s ijk ∨ ¬s i ′ jk)<br />

j=1 k=1 i=1 i ′ =i+1<br />

each number appears at most once in each column<br />

9∧ 9∧ 9∧ 9∧<br />

(¬s ijk ∨ ¬s ij ′ k)<br />

i=1 k=1 j=1 j ′ =j+1<br />

each number appears at most once in each 3 × 3 subgrid<br />

∧ ∧ 9∧<br />

¬s ijk ∨ ¬s i ′ j ′ k<br />

G:subgrid (i,j)≠(i ′ ,j ′ ) k=1<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 11/41

Uniqueness of Solution<br />

QUESTION<br />

how to check whether there is exactly one solution<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 12/41

Uniqueness of Solution<br />

QUESTION<br />

how to check whether there is exactly one solution<br />

SOLUTION<br />

check whether there is no other solution<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 12/41

Uniqueness of Solution<br />

QUESTION<br />

how to check whether there is exactly one solution<br />

SOLUTION<br />

check whether there is no other solution<br />

Theorem<br />

let φ be CNF encoding, <strong>and</strong> α its satisfiable assignment<br />

solution is unique if <strong>and</strong> only if<br />

⎛<br />

φ ∧ ⎝ ( ∨<br />

is satisfiable<br />

α(x)=T<br />

¬x ) ∨ ( ∨<br />

α(x)=F<br />

x ) ⎞<br />

⎠<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 12/41

Encoding Techniques<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 13/41

<strong>SAT</strong> Encoding<br />

modern <strong>SAT</strong> solvers are extremely fast<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 14/41

<strong>SAT</strong> Encoding<br />

modern <strong>SAT</strong> solvers are extremely fast<br />

how to translate problem to CNF?<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 14/41

Quiz<br />

find equivalent CNFs<br />

(x ∧ y) ∨ ¬(u ∧ v)<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 15/41

Quiz<br />

find equivalent CNFs<br />

(x ∧ y) ∨ ¬(u ∧ v)<br />

∨ n<br />

i=1 (x i ∧ y i )<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 15/41

Quiz<br />

find equivalent CNFs<br />

(x ∧ y) ∨ ¬(u ∧ v)<br />

∨ n<br />

i=1 (x i ∧ y i )<br />

how to avoid exponential blow up?<br />

☞ Tseitin conversion<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 15/41

Tseitin’s transformation<br />

x ↔ (y ∧ z) = (¬x ∨ y) ∧ (¬x ∨ z) ∧ (¬y ∨ ¬z ∨ x)<br />

x ↔ (y ∨ z) = (x ∨ ¬y) ∧ (x ∨ ¬z) ∧ (y ∨ z ∨ ¬x)<br />

CNF<br />

CNF<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 16/41

Tseitin’s transformation<br />

x ↔ (y ∧ z) = (¬x ∨ y) ∧ (¬x ∨ z) ∧ (¬y ∨ ¬z ∨ x)<br />

x ↔ (y ∨ z) = (x ∨ ¬y) ∧ (x ∨ ¬z) ∧ (y ∨ z ∨ ¬x)<br />

CNF<br />

CNF<br />

⇔<br />

⇔<br />

(x ∧ ¬y)<br />

⎧<br />

∨ (¬x ∧ y)<br />

⎪⎨ z = u ∨ v<br />

z where u = x ∧ ¬y<br />

⎪⎩<br />

⎧<br />

v = ¬x ∧ y<br />

⎫<br />

z ∧ ∧ ⎨ z ↔ (u ∨ v), ⎬<br />

u ↔ (x ∧ ¬y),<br />

⎩<br />

⎭<br />

v ↔ (¬x ∧ y)<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 16/41

Tseitin’s transformation<br />

x ↔ (y ∧ z) = (¬x ∨ y) ∧ (¬x ∨ z) ∧ (¬y ∨ ¬z ∨ x)<br />

x ↔ (y ∨ z) = (x ∨ ¬y) ∧ (x ∨ ¬z) ∧ (y ∨ z ∨ ¬x)<br />

CNF<br />

CNF<br />

⇔<br />

⇔<br />

(x ∧ ¬y)<br />

⎧<br />

∨ (¬x ∧ y)<br />

⎪⎨ z = u ∨ v<br />

z where u = x ∧ ¬y<br />

⎪⎩<br />

⎧<br />

v = ¬x ∧ y<br />

⎫<br />

z ∧ ∧ ⎨ z ↔ (u ∨ v), ⎬<br />

u ↔ (x ∧ ¬y),<br />

⎩<br />

⎭<br />

v ↔ (¬x ∧ y)<br />

NOTE<br />

given formula of size n, converted formula is of size O(n)<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 16/41

Arithmetic<br />

⃗x k = (a k , . . . , a 1 ) is binary representation of x < 2 k<br />

Definition<br />

bit encoding<br />

⃗x k = ⃗y k =<br />

⃗x k > ⃗y k =<br />

k∧<br />

(x k ↔ y k )<br />

i=1<br />

⃗x k + ⃗y k = (c k , s k , . . . , s 1 )<br />

where<br />

{<br />

x 1 ∧ y 1 if k = 1<br />

(x k ∧ ¬y k ) ∨ ( )<br />

(x k ↔ y k ) ∧ ⃗x k−1 > ⃗y k−1 if k > 1<br />

c 0 = ⊥ c i = (x i ∧ y i ) ∨ (x i ∧ c i−1 ) ∨ (y i ∧ c i−1 ) for i 1<br />

s i = x i ⊕ y i ⊕ c i−1 for i 1<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 17/41

More...<br />

many encodable mathematical objects:<br />

finite sets<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 18/41

More...<br />

many encodable mathematical objects:<br />

finite sets<br />

order > on finite set<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 18/41

More...<br />

many encodable mathematical objects:<br />

finite sets<br />

order > on finite set<br />

graphs<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 18/41

More...<br />

many encodable mathematical objects:<br />

finite sets<br />

order > on finite set<br />

graphs<br />

...<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 18/41

DPLL Algorithm<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 19/41

Implementation<br />

many <strong>SAT</strong> solvers use DPLL algorithm<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 20/41

Implementation<br />

many <strong>SAT</strong> solvers use DPLL algorithm<br />

decision, BCP, conflict analysis, clause learning<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 20/41

Implementation<br />

many <strong>SAT</strong> solvers use DPLL algorithm<br />

decision, BCP, conflict analysis, clause learning<br />

two-watched literal (for BCP)<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 20/41

Boolean Constraint Propagation<br />

current assignment:<br />

current decision: z ↦→ T<br />

x ↦→ T, y ↦→ F<br />

¬x ∨ z ∨ b ∨ c<br />

¬x ∨ y ∨ ¬z ∨ ¬w<br />

w ∨ y ∨ a<br />

¬a ∨ z ∨ b ∨ y<br />

¬x ∨ c ∨ d ∨ e<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 21/41

Boolean Constraint Propagation<br />

current assignment:<br />

current decision: z ↦→ T<br />

x ↦→ T, y ↦→ F<br />

¬x ∨ z ∨ b ∨ c ✓<br />

¬x ∨ y ∨ ¬z ∨ ¬w<br />

w ∨ y ∨ a<br />

¬a ∨ z ∨ b ∨ y<br />

¬x ∨ c ∨ d ∨ e<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 21/41

Boolean Constraint Propagation<br />

current assignment:<br />

current decision: z ↦→ T<br />

x ↦→ T, y ↦→ F<br />

¬x ∨ z ∨ b ∨ c ✓<br />

¬x ∨ y ∨ ¬z ∨ ¬w w ↦→ F<br />

w ∨ y ∨ a<br />

¬a ∨ z ∨ b ∨ y<br />

¬x ∨ c ∨ d ∨ e<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 21/41

Boolean Constraint Propagation<br />

current assignment:<br />

current decision: z ↦→ T<br />

x ↦→ T, y ↦→ F<br />

¬x ∨ z ∨ b ∨ c ✓<br />

¬x ∨ y ∨ ¬z ∨ ¬w w ↦→ F<br />

w ∨ y ∨ a<br />

−<br />

¬a ∨ z ∨ b ∨ y −<br />

¬x ∨ c ∨ d ∨ e −<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 21/41

Two Watch Literals<br />

current assignment:<br />

current decision: z ↦→ T<br />

x ↦→ T, y ↦→ F<br />

¬x ∨ z ∨ b ∨ c<br />

¬x ∨ y ∨ ¬z ∨ ¬w<br />

w ∨ y ∨ a<br />

¬a ∨ z ∨ b ∨ y<br />

¬x ∨ c ∨ d ∨ e<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 22/41

Conflict-Directed Backtracking<br />

current decision: x 1 ↦→ T @6 (T is assigned to x 1 at 6th decision)<br />

x 10 ↦→ F @3<br />

x 2 ↦→ T @6<br />

x 5 ↦→ T @6<br />

x 1 ↦→ T @6<br />

x 4 ↦→ T @6<br />

⊥<br />

x 3 ↦→ T @6<br />

x 5 ↦→ F @6<br />

x 9 ↦→ F @1<br />

x 11 ↦→ F @3<br />

if x 1 ≠ ⊥, back to level 3<br />

learned clause: ¬x 1 ∨ x 9 ∨ x 10 ∨ x 11<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 23/41

Implementation II<br />

many <strong>SAT</strong> solvers use DPLL algorithm<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 24/41

Implementation II<br />

many <strong>SAT</strong> solvers use DPLL algorithm<br />

restart rather than backtrack<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 24/41

Implementation II<br />

many <strong>SAT</strong> solvers use DPLL algorithm<br />

restart rather than backtrack<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 24/41

Implementation II<br />

many <strong>SAT</strong> solvers use DPLL algorithm<br />

restart rather than backtrack<br />

glue clauses (generalisation of unit clauses)<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 24/41

<strong>Termination</strong><br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 25/41

Term Rewriting<br />

pair of terms l → r is rewrite rule if l is non-variable <strong>and</strong><br />

Var(r) ⊆ Var(l)<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 26/41

Term Rewriting<br />

pair of terms l → r is rewrite rule if l is non-variable <strong>and</strong><br />

Var(r) ⊆ Var(l)<br />

term rewrite system (TRS) is set of rewrite rules<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 26/41

Term Rewriting<br />

pair of terms l → r is rewrite rule if l is non-variable <strong>and</strong><br />

Var(r) ⊆ Var(l)<br />

term rewrite system (TRS) is set of rewrite rules<br />

(rewrite relation) s → R t if ∃l → r ∈ R, context C, substitution σ :<br />

s = C[lσ] ∧ t = C[rσ]<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 26/41

Example<br />

TRS R<br />

Term Rewriting<br />

pair of terms l → r is rewrite rule if l is non-variable <strong>and</strong><br />

Var(r) ⊆ Var(l)<br />

term rewrite system (TRS) is set of rewrite rules<br />

(rewrite relation) s → R t if ∃l → r ∈ R, context C, substitution σ :<br />

s = C[lσ] ∧ t = C[rσ]<br />

rewriting<br />

x + 0 → x x + s(y) → s(x + y)<br />

x × 0 → 0<br />

x × s(y) → x × y + x<br />

s(0) × s(0) → R s(0) × 0 + s(0)<br />

→ R 0 + s(0)<br />

→ R s(0 + 0)<br />

→ R s(0)<br />

normal form<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 26/41

APPLICATIONS<br />

verification for functional programming<br />

theorem proving<br />

code optimization in compilers<br />

symbolic computation in mathematics<br />

...<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 27/41

Implementation of <strong>Termination</strong> Tools<br />

TRS<br />

constraint<br />

solution<br />

proof<br />

encode<br />

decode<br />

CNF<br />

<strong>SAT</strong> solver<br />

assignment<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 28/41

Precedence <strong>Termination</strong><br />

precedence > is strict order on function symbols<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 29/41

Precedence <strong>Termination</strong><br />

precedence > is strict order on function symbols<br />

Definition<br />

l > prec r if Var(l) ⊇ Var(r), l = f(. . .), <strong>and</strong> f > g for all functions g in r<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 29/41

Precedence <strong>Termination</strong><br />

precedence > is strict order on function symbols<br />

Definition<br />

l > prec r if Var(l) ⊇ Var(r), l = f(. . .), <strong>and</strong> f > g for all functions g in r<br />

Theorem<br />

finite TRS R is terminating if R ⊆ > prec for some precedence ><br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 29/41

Encoding of Precedence <strong>Termination</strong> Problem<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 30/41

Encoding of Precedence <strong>Termination</strong> Problem<br />

Corollary<br />

assume f > g st<strong>and</strong>s for propositional variable.<br />

finite TRS R is terminating if O ∧ I ∧ T , where<br />

O =<br />

∧<br />

(l > prec r)<br />

l→r∈R<br />

I = ∧<br />

¬(f > f)<br />

T =<br />

f∈F<br />

∧<br />

f,g,h∈F<br />

((f > g) ∧ (g > h) → (f > h))<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 30/41

Encoding of Precedence <strong>Termination</strong> Problem<br />

Corollary<br />

assume f > g st<strong>and</strong>s for propositional variable.<br />

finite TRS R is terminating if O ∧ I ∧ T , where<br />

O =<br />

∧<br />

(l > prec r)<br />

l→r∈R<br />

I = ∧<br />

¬(f > f)<br />

T =<br />

f∈F<br />

∧<br />

f,g,h∈F<br />

((f > g) ∧ (g > h) → (f > h))<br />

NOTE<br />

size of T is O(n 3 ), where n = |F| (number of function symbols)<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 30/41

Example<br />

prove termination of TRS<br />

not(x) → if(x, false, true)<br />

<strong>and</strong>(x, y) → if(x, y, false)<br />

or(x, y) → if(x, true, y)<br />

equiv(x, y) → if(x, y, not(y))<br />

if(true, x, y) → x<br />

if(false, x, y) → y<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 31/41

⎫<br />

⎧⎪<br />

O = ∧ not(x) > prec if(x, false, true), if(true, x, y) > prec x,<br />

⎨ ⎪⎬<br />

<strong>and</strong>(x, y) > prec if(x, y, false), if(false, x, y) > prec y,<br />

⎪ or(x, y) > prec if(x, true, y),<br />

⎩ ⎪⎭<br />

equiv(x, y) > prec if(x, y, not(y))<br />

⎛<br />

⎞<br />

not > if ∧ not > false ∧ not > true ∧<br />

= ⎜ <strong>and</strong> > if ∧ <strong>and</strong> > false ∧<br />

⎟<br />

⎝ or > if ∧ or > true ∧<br />

⎠<br />

equiv > if ∧ equiv > not<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 32/41

⎫<br />

⎧⎪<br />

O = ∧ not(x) > prec if(x, false, true), if(true, x, y) > prec x,<br />

⎨ ⎪⎬<br />

<strong>and</strong>(x, y) > prec if(x, y, false), if(false, x, y) > prec y,<br />

⎪ or(x, y) > prec if(x, true, y),<br />

⎩ ⎪⎭<br />

equiv(x, y) > prec if(x, y, not(y))<br />

⎛<br />

⎞<br />

not > if ∧ not > false ∧ not > true ∧<br />

= ⎜ <strong>and</strong> > if ∧ <strong>and</strong> > false ∧<br />

⎟<br />

⎝ or > if ∧ or > true ∧<br />

⎠<br />

equiv > if ∧ equiv > not<br />

O ∧ I ∧ T is satisfiable:<br />

hence TRS is terminating<br />

<strong>and</strong> > or > equiv > not > false > true > if<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 32/41

Example<br />

prove termination of TRS R<br />

x + 0 → x x + s(y) → s(x + y)<br />

x × 0 → 0<br />

x × s(y) → x × y + x<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 33/41

Example<br />

prove termination of TRS R<br />

x + 0 → x x + s(y) → s(x + y)<br />

x × 0 → 0<br />

x × s(y) → x × y + x<br />

precedence termination does not hold:<br />

is unsatisfiable<br />

R ⊆ > prec<br />

⇔ s(x) + y > prec s(x + y) ∧ · · ·<br />

⇔ + > s ∧ + > + ∧ · · ·<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 33/41

Lexicographic Path Order<br />

Definition given precedence ><br />

s > lpo t if s = f(s 1 , . . . , s m ), <strong>and</strong> either t ∈ Var(s) or t = g(t 1 , . . . , t n )<br />

<strong>and</strong><br />

s i > lpo t or s i = t for all 1 i m,<br />

f > g <strong>and</strong> s > lpo t i for all 1 i n, or<br />

f = g <strong>and</strong> there is 1 i n with<br />

s 1 = t 1 , . . . , s i−1 = t i−1 , s i > lpo t i , <strong>and</strong> s > lpo t i+1 , . . . s > lpo t n<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 34/41

Lexicographic Path Order<br />

Definition given precedence ><br />

s > lpo t if s = f(s 1 , . . . , s m ), <strong>and</strong> either t ∈ Var(s) or t = g(t 1 , . . . , t n )<br />

<strong>and</strong><br />

s i > lpo t or s i = t for all 1 i m,<br />

f > g <strong>and</strong> s > lpo t i for all 1 i n, or<br />

f = g <strong>and</strong> there is 1 i n with<br />

s 1 = t 1 , . . . , s i−1 = t i−1 , s i > lpo t i , <strong>and</strong> s > lpo t i+1 , . . . s > lpo t n<br />

Theorem Kamin <strong>and</strong> Levy, 1980<br />

finite TRS R is terminating if R ⊆ > lpo for some precedence ><br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 34/41

Lexicographic Path Order<br />

Definition given precedence ><br />

s > lpo t if s = f(s 1 , . . . , s m ), <strong>and</strong> either t ∈ Var(s) or t = g(t 1 , . . . , t n )<br />

<strong>and</strong><br />

s i > lpo t or s i = t for all 1 i m,<br />

f > g <strong>and</strong> s > lpo t i for all 1 i n, or<br />

f = g <strong>and</strong> there is 1 i n with<br />

s 1 = t 1 , . . . , s i−1 = t i−1 , s i > lpo t i , <strong>and</strong> s > lpo t i+1 , . . . s > lpo t n<br />

Theorem Kamin <strong>and</strong> Levy, 1980<br />

finite TRS R is terminating if R ⊆ > lpo for some precedence ><br />

<strong>SAT</strong> encoding is similar to precedence termination<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 34/41

Example<br />

TRS R<br />

x + 0 → x x + s(y) → s(x + y)<br />

x × 0 → 0<br />

x × s(y) → x × y + x<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 35/41

Example<br />

TRS R<br />

x + 0 → x x + s(y) → s(x + y)<br />

x × 0 → 0<br />

x × s(y) → x × y + x<br />

<strong>SAT</strong> solver finds precedence that fulfils R ⊆ > lpo :<br />

hence R is terminating<br />

× > + > s > 0<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 35/41

Optimization<br />

Annov, Codish, <strong>and</strong> Stuckey, RTA 2006<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 36/41

Optimization<br />

Annov, Codish, <strong>and</strong> Stuckey, RTA 2006<br />

FACT<br />

transitivity constraint is O(n 3 )<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 36/41

Optimization<br />

Annov, Codish, <strong>and</strong> Stuckey, RTA 2006<br />

FACT<br />

transitivity constraint is O(n 3 )<br />

Lemma<br />

two statements are equivalent<br />

R ⊆ > lpo for some precedence ><br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 36/41

Optimization<br />

Annov, Codish, <strong>and</strong> Stuckey, RTA 2006<br />

FACT<br />

transitivity constraint is O(n 3 )<br />

Lemma<br />

two statements are equivalent<br />

R ⊆ > lpo for some precedence ><br />

R ⊆ > lpo for some total precedence ><br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 36/41

Optimization<br />

Annov, Codish, <strong>and</strong> Stuckey, RTA 2006<br />

FACT<br />

transitivity constraint is O(n 3 )<br />

Lemma<br />

two statements are equivalent<br />

R ⊆ > lpo for some precedence ><br />

R ⊆ > lpo for some total precedence ><br />

IDEA<br />

total precedence > can be represented by weight assignment<br />

each function f is variable over {0, . . . , n}; use bit-encoding<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 36/41

Optimization<br />

Annov, Codish, <strong>and</strong> Stuckey, RTA 2006<br />

FACT<br />

transitivity constraint is O(n 3 )<br />

Lemma<br />

two statements are equivalent<br />

R ⊆ > lpo for some precedence ><br />

R ⊆ > lpo for some total precedence ><br />

IDEA<br />

total precedence > can be represented by weight assignment<br />

each function f is variable over {0, . . . , n}; use bit-encoding<br />

size of constraint is O(N log n), where N is size of TRS<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 36/41

Knuth-Bendix Orders<br />

Definition<br />

weight assignment (w 0 , w f , w g , . . .) is tuple of real numbers where<br />

f, g, . . . ∈ F<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 37/41

Knuth-Bendix Orders<br />

Definition<br />

weight assignment (w 0 , w f , w g , . . .) is tuple of real numbers where<br />

f, g, . . . ∈ F<br />

weight of term t is<br />

{<br />

w 0<br />

if t is variable<br />

w(t) =<br />

w f + w(t 1 ) + · · · + w(t n ) if t = f(t 1 , . . . , t n )<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 37/41

Knuth-Bendix Orders<br />

Definition<br />

weight assignment (w 0 , w f , w g , . . .) is tuple of real numbers where<br />

f, g, . . . ∈ F<br />

weight of term t is<br />

{<br />

w 0<br />

if t is variable<br />

w(t) =<br />

w f + w(t 1 ) + · · · + w(t n ) if t = f(t 1 , . . . , t n )<br />

weight assignment is admissible for precedence > if<br />

w f > 0 or f g<br />

for all unary functions f <strong>and</strong> all functions g<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 37/41

Definition Knuth-Bendix order; given weight w <strong>and</strong> precedence ><br />

s > kbo t if |s| x |t| x for all variables x <strong>and</strong> either<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 38/41

Definition Knuth-Bendix order; given weight w <strong>and</strong> precedence ><br />

s > kbo t if |s| x |t| x for all variables x <strong>and</strong> either<br />

w(s) > w(t), or<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 38/41

Definition Knuth-Bendix order; given weight w <strong>and</strong> precedence ><br />

s > kbo t if |s| x |t| x for all variables x <strong>and</strong> either<br />

w(s) > w(t), or<br />

w(s) = w(t) <strong>and</strong><br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 38/41

Definition Knuth-Bendix order; given weight w <strong>and</strong> precedence ><br />

s > kbo t if |s| x |t| x for all variables x <strong>and</strong> either<br />

w(s) > w(t), or<br />

w(s) = w(t) <strong>and</strong><br />

s = f n (t) <strong>and</strong> t is variable for some unary f <strong>and</strong> n 1; or<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 38/41

Definition Knuth-Bendix order; given weight w <strong>and</strong> precedence ><br />

s > kbo t if |s| x |t| x for all variables x <strong>and</strong> either<br />

w(s) > w(t), or<br />

w(s) = w(t) <strong>and</strong><br />

s = f n (t) <strong>and</strong> t is variable for some unary f <strong>and</strong> n 1; or<br />

s = f(s 1, . . . , s i−1, s i, . . . , s n), t = f(s 1, . . . , s i−1, t i, . . . , t n), <strong>and</strong><br />

s i > kbo t i; or<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 38/41

Definition Knuth-Bendix order; given weight w <strong>and</strong> precedence ><br />

s > kbo t if |s| x |t| x for all variables x <strong>and</strong> either<br />

w(s) > w(t), or<br />

w(s) = w(t) <strong>and</strong><br />

s = f n (t) <strong>and</strong> t is variable for some unary f <strong>and</strong> n 1; or<br />

s = f(s 1, . . . , s i−1, s i, . . . , s n), t = f(s 1, . . . , s i−1, t i, . . . , t n), <strong>and</strong><br />

s i > kbo t i; or<br />

s = f(s 1, . . . , s n), t = g(t 1, . . . , t m), <strong>and</strong> f > g<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 38/41

Definition Knuth-Bendix order; given weight w <strong>and</strong> precedence ><br />

s > kbo t if |s| x |t| x for all variables x <strong>and</strong> either<br />

w(s) > w(t), or<br />

w(s) = w(t) <strong>and</strong><br />

s = f n (t) <strong>and</strong> t is variable for some unary f <strong>and</strong> n 1; or<br />

s = f(s 1, . . . , s i−1, s i, . . . , s n), t = f(s 1, . . . , s i−1, t i, . . . , t n), <strong>and</strong><br />

s i > kbo t i; or<br />

s = f(s 1, . . . , s n), t = g(t 1, . . . , t m), <strong>and</strong> f > g<br />

Theorem Knuth <strong>and</strong> Bendix, 1970; Dershowitz, 1979<br />

finite TRS R is terminating if R ⊆ > kbo for some weight<br />

(w 0 , w f , w g , . . .) over R <strong>and</strong> precedence ><br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 38/41

Theorem Hirokawa, Zankl, Middeldorp, JAR 2010<br />

next statements are equivalent<br />

R ⊆ > kbo for some weight w over R <strong>and</strong> precedence ><br />

R ⊆ > kbo for some weight w over {0, 1, . . . , 2 2N } <strong>and</strong> precedence ><br />

here N is size of R<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 39/41

Theorem Hirokawa, Zankl, Middeldorp, JAR 2010<br />

next statements are equivalent<br />

R ⊆ > kbo for some weight w over R <strong>and</strong> precedence ><br />

R ⊆ > kbo for some weight w over {0, 1, . . . , 2 2N } <strong>and</strong> precedence ><br />

here N is size of R<br />

NOTE<br />

large weights (> 15) are hardly required<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 39/41

<strong>Termination</strong> Tools<br />

termination tools (AProVE, Matchbox, µ-Term, TTT2, VMTL, ...)<br />

use <strong>SAT</strong>/SMT solvers<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 40/41

<strong>Termination</strong> Tools<br />

termination tools (AProVE, Matchbox, µ-Term, TTT2, VMTL, ...)<br />

use <strong>SAT</strong>/SMT solvers<br />

to increase power, termination tools employ transformations<br />

dependency pair method by Arts <strong>and</strong> Giesl, TCS 2000<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 40/41

<strong>Termination</strong> Tools<br />

termination tools (AProVE, Matchbox, µ-Term, TTT2, VMTL, ...)<br />

use <strong>SAT</strong>/SMT solvers<br />

to increase power, termination tools employ transformations<br />

dependency pair method by Arts <strong>and</strong> Giesl, TCS 2000<br />

complexity analysers (AProVE, CaT, TCT, ...) use same way<br />

POP* by Avanzini <strong>and</strong> Moser (2008)<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 40/41

Summary<br />

<strong>SAT</strong> solver <strong>and</strong> basic encoding techniques<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 41/41

Summary<br />

<strong>SAT</strong> solver <strong>and</strong> basic encoding techniques<br />

termination analysis<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 41/41

Summary<br />

<strong>SAT</strong> solver <strong>and</strong> basic encoding techniques<br />

termination analysis<br />

often <strong>SAT</strong> encoding approach outperforms dedicated algorithm<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 41/41

Summary<br />

<strong>SAT</strong> solver <strong>and</strong> basic encoding techniques<br />

termination analysis<br />

often <strong>SAT</strong> encoding approach outperforms dedicated algorithm<br />

thank you for your kind attention!<br />

<strong>SAT</strong> <strong>and</strong> <strong>Termination</strong> 41/41