Closed Forms for Numerical Loops Zachary Kincaid 1 Jason Breck 2 John - - PowerPoint PPT Presentation

closed forms for numerical loops
SMART_READER_LITE
LIVE PREVIEW

Closed Forms for Numerical Loops Zachary Kincaid 1 Jason Breck 2 John - - PowerPoint PPT Presentation

January 16, 2019 Closed Forms for Numerical Loops Zachary Kincaid 1 Jason Breck 2 John Cyphert 2 Thomas Reps 2 , 3 1 Princeton University 2 University of Wisconsin-Madison 3 GrammaTech, Inc Is i Loop summarization n j loop n j Summary


slide-1
SLIDE 1

Closed Forms for Numerical Loops

Zachary Kincaid1 Jason Breck2 John Cyphert2 Thomas Reps2,3

1Princeton University 2University of Wisconsin-Madison 3GrammaTech, Inc

January 16, 2019

slide-2
SLIDE 2

Loop summarization

The problem: given a loop, compute a formula that represents its behavior. while(i < n): i := i + 2 j := j + 1 k i i k j j k n n i n k i n Loop counter Before exec After exec i j n j i Summary can be used to answer questions about program behavior

  • Is i

j n loop j i valid?

slide-3
SLIDE 3

Loop summarization

The problem: given a loop, compute a formula that represents its behavior. while(i < n): i := i + 2 j := j + 1 ∃k ∈ N.     i′ = i + 2k ∧ j′ = j + k ∧ n′ = n ∧ i′ ≥ n ∧ (k ≥ 1 ⇒ i′ ≤ n + 1)     Loop counter Before exec After exec i j n j i Summary can be used to answer questions about program behavior

  • Is i

j n loop j i valid?

slide-4
SLIDE 4

Loop summarization

The problem: given a loop, compute a formula that represents its behavior. while(i < n): i := i + 2 j := j + 1 ∃k ∈ N.     i′ = i + 2k ∧ j′ = j + k ∧ n′ = n ∧ i′ ≥ n ∧ (k ≥ 1 ⇒ i′ ≤ n + 1)     Loop counter Before exec After exec i = j = 0 ∧ n > 0∧ ∧¬(2j′ = i′) Summary can be used to answer questions about program behavior

  • Is {i = j = 0 ∧ n > 0}loop{2j = i} valid?
slide-5
SLIDE 5

Today: Linear loops while ( * ): x := Ax non-deterministic A ∈ Qn×n

  • In the paper: affine & solvable polynomial loops

[Rodríguez-Carbonell & Kapur, ISAAC 2004].

slide-6
SLIDE 6

Today: Linear loops while ( * ): x := Ax non-deterministic A ∈ Qn×n

  • In the paper: affine & solvable polynomial loops

[Rodríguez-Carbonell & Kapur, ISAAC 2004].

slide-7
SLIDE 7

Why linear loops?

  • Natural problem
  • Practical applications
  • Any loop can be approximated by a linear loop [KBCR POPL’18]
  • Summary for the approximation gives invariants for the loop
slide-8
SLIDE 8

Why linear loops?

  • Natural problem
  • Practical applications
  • Any loop can be approximated by a linear loop [KBCR POPL’18]
  • Summary for the approximation gives invariants for the loop
slide-9
SLIDE 9

Approximating general loops [KBCR POPL’18]

binary-search(A,target): lo = 1, hi = size(A), ticks = 0 while (lo <= hi): ticks++; mid = lo + (hi-lo)/2 if A[mid] == target: return mid else if A[mid] < target: lo = mid+1 else : hi = mid-1 Not a linear transformation while (*): x y z x y z ticks lo hi mid target A x y z x ticks hi lo y z s s v v k x x kz y

ky

z z k ticks ticks k hi lo

k hi

lo

slide-10
SLIDE 10

Approximating general loops [KBCR POPL’18]

binary-search(A,target): lo = 1, hi = size(A), ticks = 0 while (lo <= hi): ticks++; mid = lo + (hi-lo)/2 if A[mid] == target: return mid else if A[mid] < target: lo = mid+1 else : hi = mid-1 Not a linear transformation while (*):   x y z   :=    1 1 1 2 1      x y z   ticks lo hi mid target A x y z x ticks hi lo y z s s v v k x x kz y

ky

z z k ticks ticks k hi lo

k hi

lo

slide-11
SLIDE 11

Approximating general loops [KBCR POPL’18]

binary-search(A,target): lo = 1, hi = size(A), ticks = 0 while (lo <= hi): ticks++; mid = lo + (hi-lo)/2 if A[mid] == target: return mid else if A[mid] < target: lo = mid+1 else : hi = mid-1 Not a linear transformation while (*):   x y z   :=    1 1 1 2 1      x y z           ticks lo hi mid target A         ∼   x y z   ⇐ ⇒ x = ticks ∧ hi − lo ≤ y ∧ z = 1 s s v v k x x kz y

ky

z z k ticks ticks k hi lo

k hi

lo

slide-12
SLIDE 12

Approximating general loops [KBCR POPL’18]

binary-search(A,target): lo = 1, hi = size(A), ticks = 0 while (lo <= hi): ticks++; mid = lo + (hi-lo)/2 if A[mid] == target: return mid else if A[mid] < target: lo = mid+1 else : hi = mid-1 Not a linear transformation while (*):   x y z   :=    1 1 1 2 1      x y z   ticks lo hi mid target A x y z x ticks hi lo y z s s′ v v′

  • k

x x kz y

ky

z z k ticks ticks k hi lo

k hi

lo

slide-13
SLIDE 13

Approximating general loops [KBCR POPL’18]

binary-search(A,target): lo = 1, hi = size(A), ticks = 0 while (lo <= hi): ticks++; mid = lo + (hi-lo)/2 if A[mid] == target: return mid else if A[mid] < target: lo = mid+1 else : hi = mid-1 Not a linear transformation while (*):   x y z   :=    1 1 1 2 1      x y z   ticks lo hi mid target A x y z x ticks hi lo y z s s v v ∃k ∈ N.   x′ = x + kz ∧y′ = (1/2)ky ∧z′ = z   k ticks ticks k hi lo

k hi

lo

slide-14
SLIDE 14

Approximating general loops [KBCR POPL’18]

binary-search(A,target): lo = 1, hi = size(A), ticks = 0 while (lo <= hi): ticks++; mid = lo + (hi-lo)/2 if A[mid] == target: return mid else if A[mid] < target: lo = mid+1 else : hi = mid-1 Not a linear transformation while (*):   x y z   :=    1 1 1 2 1      x y z   ticks lo hi mid target A x y z x ticks hi lo y z s s v v ∃k ∈ N.   x′ = x + kz ∧y′ = (1/2)ky ∧z′ = z   ∃k ∈ N. ( ticks′ = ticks + k ∧hi′ − lo′ ≤ (1/2)k(hi − lo) )

slide-15
SLIDE 15

Hasn’t this problem already been solved?

Given a square matrix A ∈ Qn×n, can compute Ak symbolically Entries of Ak are exponential polynomials: a1λk

1kd1 + · · · + anλk nkdn

Algebraic numbers Camille Jordan while(*): x Ax k x Akx

slide-16
SLIDE 16

Hasn’t this problem already been solved?

Given a square matrix A ∈ Qn×n, can compute Ak symbolically Entries of Ak are exponential polynomials: a1λk

1kd1 + · · · + anλk nkdn

Algebraic numbers Camille Jordan while(*): x := Ax ∃k ∈ N.x′ = Akx

slide-17
SLIDE 17

No.

Skolem’s problem (variant): Given an exponential-polynomial f over the alge- braic numbers, does there exists some n ∈ N such that f(k) = 0? Decidability of Skolem’s problem is unknown! Thoraf Skolem Essential problem: algebraic numbers.

slide-18
SLIDE 18

No.

Skolem’s problem (variant): Given an exponential-polynomial f over the alge- braic numbers, does there exists some n ∈ N such that f(k) = 0? Decidability of Skolem’s problem is unknown! Thoraf Skolem Essential problem: algebraic numbers.

slide-19
SLIDE 19

Outline

Starting point of this work: avoid algebraic numbers

1 Periodic rational matrices have closed forms over Q.

  • Computable in polytime

2 All matrices have best periodic-rational approximations. 3 Exponential-polynomial arithmetic over

is decidable.

slide-20
SLIDE 20

Outline

Starting point of this work: avoid algebraic numbers

1 Periodic rational matrices have closed forms over Q.

  • Computable in polytime

2 All matrices have best periodic-rational approximations. 3 Exponential-polynomial arithmetic over

is decidable.

slide-21
SLIDE 21

Outline

Starting point of this work: avoid algebraic numbers

1 Periodic rational matrices have closed forms over Q.

  • Computable in polytime

2 All matrices have best periodic-rational approximations. 3 Exponential-polynomial arithmetic over Q is decidable.

slide-22
SLIDE 22

Closed forms for linear loops

slide-23
SLIDE 23

Known:

  • Eigenvalues of A are rational ⇒ Ak can be expressed in

exponential-polynomial arithmetic over Q.

  • [Boigelot PhD thesis ’99]: A generates a finite monoid

Ak can be expressed in Presburger arithmetic. Common generalization: A matrix A is periodic rational if there is some power p such that Ap has rational eigenvalues.

  • A periodic rational

can express closed form as k x Akx k

p i

k i mod p x Ap

k p Aix

Rational eigenvalues

slide-24
SLIDE 24

Known:

  • Eigenvalues of A are rational ⇒ Ak can be expressed in

exponential-polynomial arithmetic over Q.

  • [Boigelot PhD thesis ’99]: A generates a finite monoid ⇒ Ak can be

expressed in Presburger arithmetic. Common generalization: A matrix A is periodic rational if there is some power p such that Ap has rational eigenvalues.

  • A periodic rational

can express closed form as k x Akx k

p i

k i mod p x Ap

k p Aix

Rational eigenvalues

slide-25
SLIDE 25

Known:

  • Eigenvalues of A are rational ⇒ Ak can be expressed in

exponential-polynomial arithmetic over Q.

  • [Boigelot PhD thesis ’99]: A generates a finite monoid ⇒ Ak can be

expressed in Presburger arithmetic. Common generalization: A matrix A is periodic rational if there is some power p such that Ap has rational eigenvalues.

  • A periodic rational

can express closed form as k x Akx k

p i

k i mod p x Ap

k p Aix

Rational eigenvalues

slide-26
SLIDE 26

Known:

  • Eigenvalues of A are rational ⇒ Ak can be expressed in

exponential-polynomial arithmetic over Q.

  • [Boigelot PhD thesis ’99]: A generates a finite monoid ⇒ Ak can be

expressed in Presburger arithmetic. Common generalization: A matrix A is periodic rational if there is some power p such that Ap has rational eigenvalues.

  • A periodic rational ⇒ can express closed form as

( ∃k ∈ N.x′ = Akx ) ≡ ( ∃k ∈ N.

p−1

i=0

k ≡ i mod p ∧ x′ = (Ap)⌊k/p⌋Aix ) Rational eigenvalues

slide-27
SLIDE 27
  • Problem: Rational period of a matrix might be exponential in its size
  • Expressing closed form takes exponential space!
  • Solution: periodic rational spectral decomposition
slide-28
SLIDE 28
  • Problem: Rational period of a matrix might be exponential in its size
  • Expressing closed form takes exponential space!
  • Solution: periodic rational spectral decomposition
slide-29
SLIDE 29

Periodic rational spectral decomposition (PRSD)

Let A ∈ Qn×n be a square rational matrix. A periodic rational spectral decomposition of A is a set of triples {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩} ⊂ N × Q × Qn such that

  • for each i, vi is a generalized eigenvector of Api, with eigenvalue λi.
  • v

vm is linearly independent

  • Informally:

v vm is maximal

slide-30
SLIDE 30

Periodic rational spectral decomposition (PRSD)

Let A ∈ Qn×n be a square rational matrix. A periodic rational spectral decomposition of A is a set of triples {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩} ⊂ N × Q × Qn such that

  • for each i, vi is a generalized eigenvector of Api, with eigenvalue λi.
  • {v1, ..., vm} is linearly independent
  • Informally:

v vm is maximal

slide-31
SLIDE 31

Periodic rational spectral decomposition (PRSD)

Let A ∈ Qn×n be a square rational matrix. A periodic rational spectral decomposition of A is a set of triples {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩} ⊂ N × Q × Qn such that

  • for each i, vi is a generalized eigenvector of Api, with eigenvalue λi.
  • {v1, ..., vm} is linearly independent
  • Informally: {v1, ..., vm} is maximal
slide-32
SLIDE 32

Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.

  • (

x′ = Akx ) takes exponential space, but

  • for any i,

vT

i x

vT

i Akx

can be computed in polytime

  • Intuition: break up period.

Each vi is an easy-to-compute projection

A is periodic rational State-space can be recovered from projections x Akx

m i

vT

i x

vT

i Akx

slide-33
SLIDE 33

Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.

  • (

x′ = Akx ) takes exponential space, but

  • for any i,

( vT

i x′ = vT i Akx

) can be computed in polytime

  • Intuition: break up period.

Each vi is an easy-to-compute projection

A is periodic rational State-space can be recovered from projections x Akx

m i

vT

i x

vT

i Akx

slide-34
SLIDE 34

Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.

  • (

x′ = Akx ) takes exponential space, but

  • for any i,

( vT

i x′ = vT i Akx

) can be computed in polytime

  • Intuition: break up period.

Each vi is an easy-to-compute projection

A is periodic rational ⇐ ⇒ State-space can be recovered from projections ( x′ = Akx ) ≡ ( m ∧

i=1

vT

i x′ = vT i Akx

)

slide-35
SLIDE 35

Approximating linear loops

slide-36
SLIDE 36

Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.

  • Set V =

[ v1 v2 ... vm ]T.

  • There exists a unique B

m m with VA

BV.

  • B is periodic rational
  • B simulates A, and V is a simulation:

a a b b V V A B B is the best periodic-rational approximation of A

slide-37
SLIDE 37

Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.

  • Set V =

[ v1 v2 ... vm ]T.

  • There exists a unique B ∈ Qm×m with VA = BV.
  • B is periodic rational
  • B simulates A, and V is a simulation:

a a b b V V A B B is the best periodic-rational approximation of A

slide-38
SLIDE 38

Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.

  • Set V =

[ v1 v2 ... vm ]T.

  • There exists a unique B ∈ Qm×m with VA = BV.
  • B is periodic rational
  • B simulates A, and V is a simulation:

a a′ b b′ V V A B B is the best periodic-rational approximation of A

slide-39
SLIDE 39

Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.

  • Set V =

[ v1 v2 ... vm ]T.

  • There exists a unique B ∈ Qm×m with VA = BV.
  • B is periodic rational
  • B simulates A, and V is a simulation:

a a′ b b′ V V A B B is the best periodic-rational approximation of A

slide-40
SLIDE 40

Invariant generation pipeline

General loop Linear loop

[KBCR POPL’18]

Periodic rational linear loop

This work

Closed form Invariants

slide-41
SLIDE 41

Invariant generation pipeline

General loop Linear loop

[KBCR POPL’18]

Periodic rational linear loop

This work

Closed form Invariants

slide-42
SLIDE 42

Invariant generation pipeline

General loop Linear loop

[KBCR POPL’18]

Periodic rational linear loop

This work

Closed form Invariants

slide-43
SLIDE 43

Invariant generation pipeline

General loop Linear loop

[KBCR POPL’18]

Periodic rational linear loop

This work

Closed form Invariants

slide-44
SLIDE 44

Reasoning about non-linear arithmetic

slide-45
SLIDE 45

Exponential-polynomial arithmetic is decidable

Two steps:

1 Eliminate all symbols except the loop counter (i.e., program variables)

  • Key idea: terms are linear over the ring of exponential-polynomials.
  • (2kk3 − 3kk2 + 140 · 3k)x + (4kk)y + (2k)z
  • Eliminate symbols using linear q.e. [Loos & Weispfennning ’93]

2 Find a bound for the loop counter

  • Key idea: exponential-polynomials are eventually dominated by the

term with largest base (and largest degree)

  • E.g.,

kk kk k is eventully negative

slide-46
SLIDE 46

Exponential-polynomial arithmetic is decidable

Two steps:

1 Eliminate all symbols except the loop counter (i.e., program variables)

  • Key idea: terms are linear over the ring of exponential-polynomials.
  • (2kk3 − 3kk2 + 140 · 3k)x + (4kk)y + (2k)z
  • Eliminate symbols using linear q.e. [Loos & Weispfennning ’93]

2 Find a bound for the loop counter

  • Key idea: exponential-polynomials are eventually dominated by the

term with largest base (and largest degree)

  • E.g., 2kk3−3kk2 + 140 · 3k is eventully negative
slide-47
SLIDE 47

Consequences

Suppose A is periodic rational. The following problems are decidable:

  • Is {P}{while(∗) : x := Ax}{Q} valid?
  • Does x

v while C do x Ax terminate? Linear rational arithmetic Constant vector

slide-48
SLIDE 48

Consequences

Suppose A is periodic rational. The following problems are decidable:

  • Is {P}{while(∗) : x := Ax}{Q} valid?
  • Does (x := v; while(C) do x := Ax) terminate?

Linear rational arithmetic Constant vector

slide-49
SLIDE 49

Experiments

slide-50
SLIDE 50

Suite of 101 microbenchmarks from C4B, HOLA, and literature: # safe Time(s) 10k

7.5k 2.5k 00

101

25 76

KCBR’18 PRSD UAutomizer SeaHorn

slide-51
SLIDE 51

Contributions:

1 Periodic rational linear loops have closed forms over Q.

  • Polytime computation of the summary

2 Every matrix has a best periodic-rational approximation. 3 Exponential-polynomial arithmetic over

is decidable.

slide-52
SLIDE 52

Contributions:

1 Periodic rational linear loops have closed forms over Q.

  • Polytime computation of the summary

2 Every matrix has a best periodic-rational approximation. 3 Exponential-polynomial arithmetic over

is decidable.

slide-53
SLIDE 53

Contributions:

1 Periodic rational linear loops have closed forms over Q.

  • Polytime computation of the summary

2 Every matrix has a best periodic-rational approximation. 3 Exponential-polynomial arithmetic over Q is decidable.