decision procedures for automating termination...

28
Decision Procedures for Automating Termination Proofs Ruzica Piskac, EPFL Thomas Wies, IST Austria

Upload: others

Post on 10-Apr-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Decision Procedures for Automating Termination Proofs

Ruzica Piskac, EPFLThomas Wies, IST Austria

Page 2: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Proving Program Termination

Ranking function into the natural numbers:

Construction of global ranking functions is difficult (to automate)!

assume x > 0

while x ¸ 0 do

y := 1

while y < x do

y := 2y

end

x := x – 1

end

Page 3: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Automating Termination Proofs

Proof techniques based on local ranking functions - Size-change principle [Lee, Jones, Ben-Amram 2001]- Transition invariants [Podelski, Rybalchenko 2004]

Idea- decompose program into simpler ones- prove each simple program terminating independently

Use decision procedures for well-founded domains to automate these tasks Terminator [Cook, Podelski, Rybalchenko 2006]

Page 4: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Proving Program Termination

Ranking function into a lexicographically ordered set:

Decomposition into linear ranking functions is not always possible!

assume x > 0

while x ¸ 0 do

y := 1

while y < x do

y := 2y

end

x := x – 1

end

We need decision procedures for more powerful well-founded orderings.This talk: decision procedures for multiset orderings

Page 5: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Counting Leaves in a Tree

prog CountLeaves(root : Tree) : int =

var S : Stack[Tree] = root

var c : int = 0

do

y := head(S)

if leaf (y) then

S := tail(S)

c := c + 1

else S := left(y) ¢ right(y) ¢ tail(S)until S = ²

return c

a

b c

d d d

e e

e

e e e e

S : a

Page 6: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Counting Leaves in a Tree

prog CountLeaves(root : Tree) : int =

var S : Stack[Tree] = root

var c : int = 0

do

y := head(S)

if leaf (y) then

S := tail(S)

c := c + 1

else S := left(y) ¢ right(y) ¢ tail(S)until S = ²

return c

a

b c

d d d

e e

e

e e e e

S : e ¢ e ¢ c

Ranking function for loop:

Consider S as a multiset of trees with subtree ordering.

Page 7: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Multisets

1

2

3

4

2

2

3

X

base set

multiplicity

Operations are defined point-wise:finite multisets

Page 8: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Multiset Orderings

23

1

2

3

4

2

2

3

X

1

2

3

5

51

Y

Extend ordering ¹ on base set to ordering ¹m on multisets

Page 9: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

4

2

2

3

X

Multiset Orderings

23

1

2

3

5

51

Y

X n Y Y nX

¹m well-founded iff ¹ well-founded [Dershowitz, Manna 1979]

Extend ordering ¹ on base set to ordering ¹m on multisets

Page 10: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Counting Leaves in a Tree

a

b c

d d d

e e

e

e e e e

Termination Condition for Loop:extension of subtree relationto Multisets

Valid!Is satisfiability of multiset ordering constraints decidable?

Page 11: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Main Results

1. If T0 is decidable then so is its multiset extension.

2. If T0 is decidable in NP then so is its multiset extension.

3. Decision procedure is easily implementable using off-the-shelf SMT solvers.

Let T0 be a base theory of a preordered set.

Examples for T0

− theory of all preordered sets− theory of linear integer arithmetic− theory of a term algebra (trees) with subterm relation

Page 12: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Decision Procedure through an Example

X0 = (X nY )]Y 0 ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

Unsatisfiable!

X

Y

X’

Y ’

Page 13: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Step 1: Flattening

X0 = (X nY )]Y 0 ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

Introduce fresh variables for all non-variable subterms

X0 = X1 ]Y 0 ^ X1 = X nY ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

1

Page 14: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Step 2: Reduction

X0 = (X nY )]Y 0 ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

Replace multiset operations by their pointwise definitions

(8x: X 0(x) = X1(x) + Y 0(x)) ^(8x: X1(x) = maxfX(x)¡ Y (x); 0g) ^(8x: Y (x) · X(x)) ^(9y:Y 0(y) 6= Y (y)) ^(8y0: Y (y0) < Y 0(y0)! 9y: Y 0(y) < Y (y) ^ y0 ¹ y) ^((8x:X 0(x) = X(x)) _(9x0: X(x0) < X 0(x0) ^ 8x: X 0(x) < X 0(x)! :(x0 ¹ x)))

X0 = X1 ]Y 0 ^ X1 = X nY ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

1

2

Page 15: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Step 3: Skolemization

X0 = (X nY )]Y 0 ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

Skolemize all existential quantifiers

(8x: X 0(x) = X1(x) + Y 0(x)) ^(8x: X1(x) = maxfX(x)¡ Y (x); 0g) ^(8x: Y (x) · X(x)) ^(9y:Y 0(y) 6= Y (y)) ^(8y0: Y (y0) < Y 0(y0)! 9y: Y 0(y) < Y (y) ^ y0 ¹ y) ^((8x:X 0(x) = X(x)) _(9x0: X(x0) < X 0(x0) ^ 8x: X 0(x) < X 0(x)! :(x0 ¹ x)))

X0 = X1 ]Y 0 ^ X1 = X nY ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

1

2,3

Page 16: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Step 3: Skolemization

X0 = (X nY )]Y 0 ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

Skolemize all existential quantifiers

(8x: X 0(x) = X1(x) + Y 0(x)) ^(8x: X1(x) = maxfX(x)¡ Y (x); 0g) ^(8x: Y (x) · X(x)) ^(9y:Y 0(y) 6= Y (y)) ^(8y0: Y (y0) < Y 0(y0)! 9y: Y 0(y) < Y (y) ^ y0 ¹ y) ^( (8x:X 0(x) = X(x)) _( 9x0: X(x0) < X 0(x0) ^ 8x: X 0(x) < X 0(x)! :(x0 ¹ x)))

X0 = X1 ]Y 0 ^ X1 = X nY ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

1

2,3

(8x: X 0(x) = X1(x) + Y 0(x)) ^(8x: X1(x) = maxfX(x)¡ Y (x); 0g) ^(8x: Y (x) · X(x)) ^(Y 0(c1) 6= Y (c1)) ^(8y0: Y (y0) < Y 0(y0)! Y 0(w(y0)) < Y (w(y0)) ^ y0 ¹ w(y0)) ^((8x:X 0(x) = X(x)) _(X(c2) < X 0(c2) ^ 8x: X 0(x) < X 0(x)! :(c2 ¹ x)))

witness function

Page 17: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Step 4: Strengthening

X0 = (X nY )]Y 0 ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

Add additional axioms constraining the witness functions

(8x: X 0(x) = X1(x) + Y 0(x)) ^(8x: X1(x) = maxfX(x)¡ Y (x); 0g) ^(8x: Y (x) · X(x)) ^(9y:Y 0(y) 6= Y (y)) ^(8y0: Y (y0) < Y 0(y0)! 9y: Y 0(y) < Y (y) ^ y0 ¹ y) ^( (8x:X 0(x) = X(x)) _( 9x0: X(x0) < X 0(x0) ^ 8x: X 0(x) < X 0(x)! :(x0 ¹ x)))

X0 = X1 ]Y 0 ^ X1 = X nY ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

1

2,3,4

(8x: X 0(x) = X1(x) + Y 0(x)) ^(8x: X1(x) = maxfX(x)¡ Y (x); 0g) ^(8x: Y (x) · X(x)) ^(Y 0(c1) 6= Y (c1)) ^(8y0: Y (y0) < Y 0(y0)! Y 0(w(y0)) < Y (w(y0)) ^ y0 ¹ w(y0)) ^((8x:X 0(x) = X(x)) _(X(c2) < X 0(c2) ^ 8x: X 0(x) < X 0(x)! :(c2 ¹ x)))

^ F (Y; Y 0; w)

Page 18: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Y 0(c1) 6= Y (c1) ^X 0(c1) = X(c1) ^X 0(c1) = X(c1)¡ Y (c1) + Y 0(c1)_

X 0(c2) = X(c2)¡ Y (c2) + Y 0(c2) ^X(c2) < X 0(c2) ^ Y (c2) ¸ Y 0(c2) _

X 0(w(c2)) = X(w(c2))¡ Y (w(c2)) + Y 0(w(c2)) ^Y 0(w(c2)) < Y (w(c2)) ^X 0(w(c2)) ¸ X(w(c2)) _

c2 ¹ w(c2) ^ :(c2 ¹ w(c2))

(8x: X 0(x) = X1(x) + Y 0(x)) ^(8x: X1(x) = maxfX(x)¡ Y (x); 0g) ^(8x: Y (x) · X(x)) ^(Y 0(c1) 6= Y (c1)) ^(8y0: Y (y0) < Y 0(y0)! Y 0(w(y0)) < Y (w(y0)) ^ y0 ¹ w(y0)) ^((8x:X 0(x) = X(x)) _(X(c2) < X 0(c2) ^ 8x: X 0(x) < X 0(x)! :(c2 ¹ x)))

^ F (Y; Y 0; w)

Step 5: Instantiation

X0 = (X nY )]Y 0 ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

Instantiate universal quantifiers with ground terms

X0 = X1 ]Y 0 ^ X1 = X nY ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

1

Instantiate with c1; c2; w(c1); w(c2)

2,3,4

Page 19: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Step 5: Instantiation

X0 = (X nY )]Y 0 ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

Instantiate universal quantifiers with ground terms

X0 = X1 ]Y 0 ^ X1 = X nY ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

1

2,3,4,

5

Y 0(c1) 6= Y (c1) ^X 0(c1) = X(c1) ^X 0(c1) = X(c1)¡ Y (c1) + Y 0(c1) _

X 0(c2) = X(c2)¡ Y (c2) + Y 0(c2) ^X(c2) < X 0(c2) ^ Y (c2) ¸ Y 0(c2) _

X 0(w(c2)) = X(w(c2))¡ Y (w(c2)) + Y 0(w(c2)) ^Y 0(w(c2)) < Y (w(c2)) ^X 0(w(c2)) ¸ X(w(c2)) _

c2 ¹ w(c2) ^ :(c2 ¹ w(c2))

Page 20: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Step 6: Check Satisfiability

X0 = (X nY )]Y 0 ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

Call decision procedure for base theory + LIA + EUF

X0 = X1 ]Y 0 ^ X1 = X nY ^ Y µX ^ Y 0 Ám Y ^ :(X0 Ám X)

1

2,3,4,

5

Y 0(c1) 6= Y (c1) ^X 0(c1) = X(c1) ^X 0(c1) = X(c1)¡ Y (c1) + Y 0(c1)_

X 0(c2) = X(c2)¡ Y (c2) + Y 0(c2) ^X(c2) < X 0(c2) ^ Y (c2) ¸ Y 0(c2) _

X 0(w(c2)) = X(w(c2))¡ Y (w(c2)) + Y 0(w(c2)) ^Y 0(w(c2)) < Y (w(c2)) ^X 0(w(c2)) ¸ X(w(c2)) _

c2 ¹ w(c2) ^ :(c2 ¹ w(c2))

Unsatisfiable!

Unsatisfiable!

Unsatisfiable!

Unsatisfiable!

Unsatisfiable!

Page 21: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

model of F

base set

values of base setground terms in F

XN

Completeness of Finite Instantiation

Functions representing multisets are sort restricted

w

but witness functions are not!

F: multiset ordering constraint after Skolemization

Additional axioms bound witness functions!

finite

Page 22: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Axioms for Witness Functions

X Y

X n Y Y nX

w

only map to max. elements!

Axioms are designed specifically to guarantee NP complexity bound.

Page 23: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

ti

Completeness of InstantiationF : formula obtained after strengthening, M : model of Fn : number of witness functions in F

tm  tm-1  …  t0 of length m > n in Mt1 = w0(t0) t2 = w

1(t1) … tm = w

m-1(t

m-1)

tjtj+1

ti+1

X n Y Y nX

Á

ÁÁ

w

w

Strict chains can have at most length n!

then for some w, i, j : 0 · i < j < m and w = wi = wj

Page 24: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

model of F

base set

values of base setground terms of F

XN

Completeness of Finite Instantiation

F: multiset ordering constraint

Instantiate quantifiers with terms constructed from ground terms of F by applying each witness function at most once.

Size of the instantiated formula is exponential in number of witness functions!

w

finitepolynomial

Page 25: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Complexity

F : formula obtained after strengthening, M : model of Fn : number of witness functions, m : number of ground terms in F

tk  tk-1 …  t0 : maximal strict chain of witness terms in Mt1 = w0(t0) t2 = w1(t1) … tk = wk-1(tk-1)

tjtj+1

t0

Á

Á

wj

wj

If the base theory is decidable in NP then so is its multiset extension

Guess polynomially many strict chains. Then instantiate!

Page 26: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

POSSUMMultisets over Preordered Sets

top-level formulas:

F ::= A j F ^ F j :F

A ::= M = M j M µ M j M ¹m M j Aelem j K = K j K · K j F 8

M ::= X j ; j ftKg j M \M j M [M j M ]M j M nM j set(M)

K ::= k j C j K + K j C ¢Krestricted quanti¯ed formulas:

F 8 ::= 8x : elem:F 8 j 8x : elem:Fin

Fin ::= Ain j Fin ^ Fin j :Fin

Ain ::= tin · tin j tin=tin j ein ¹ ein j ein=eintin ::= X(ein) j C j tin + tin j C ¢ tinein ::= x j t

terminals:

X - multiset ; k - integer variable; C - integer constant

t - ground §elem-term of sort elem; x - variable of sort elem

Aelem - ground §elem-atom

If the base theory is decidable in NP then so is its POSSUM extension

Page 27: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Related Work

• Dershowitz, Manna 1979: Multiset orderings for termination proofs• Zarba 2002: Multisets + linear integer arithmetic is decidable in NP• Piskac, Kuncak 2008: Multisets + cardinality constraints are decidable

in NP• Kuncak, Piskac, Suter 2010: Sets over total orders + cardinality

constraints are decidable in NP• Nieuwenhuis 1993: Lexicographic path orderings decidable in NP• Narendran, Rusinowitch, Verma 1998: Recursive path orderings

decidable in NP• Baader, Nipkow 1998: Term Rewriting and All That• Zhang, Sipma, Manna 2005: Knuth-Bendix orderings decidable in NP• Bradley, Manna, Sipma 2006: What’s decidable about arrays?• Sofronie-Stokkermans 2005: Local theory extensions• Ihlemann, Jacobs, Sofronie-Stokkermans 2008: Psi-local theory

extensions

Page 28: Decision Procedures for Automating Termination Proofsresources.mpi-inf.mpg.de/.../deduction10/slides/ruzica-piskac.pdf · Decision Procedures for Automating Termination Proofs Ruzica

Conclusion

New logic for reasoning about

multisets over preordered sets (POSSUM)• interesting applications: automating termination proofs

• parameterized by base theory

• decidable if base theory is decidable

• good complexity (NP complete for many base theories)

• implementation of decision procedure with off-the-shelf SMT-solvers possible