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 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
j n loop j i valid?
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
j n loop j i valid?
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 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 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 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 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
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
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
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 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′
x x kz y
ky
z z k ticks ticks k hi lo
k hi
lo
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
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
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
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
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
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 Outline
Starting point of this work: avoid algebraic numbers
1 Periodic rational matrices have closed forms over Q.
2 All matrices have best periodic-rational approximations. 3 Exponential-polynomial arithmetic over
is decidable.
SLIDE 20 Outline
Starting point of this work: avoid algebraic numbers
1 Periodic rational matrices have closed forms over Q.
2 All matrices have best periodic-rational approximations. 3 Exponential-polynomial arithmetic over
is decidable.
SLIDE 21 Outline
Starting point of this work: avoid algebraic numbers
1 Periodic rational matrices have closed forms over Q.
2 All matrices have best periodic-rational approximations. 3 Exponential-polynomial arithmetic over Q is decidable.
SLIDE 22
Closed forms for linear loops
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.
can express closed form as k x Akx k
p i
k i mod p x Ap
k p Aix
Rational eigenvalues
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.
can express closed form as k x Akx k
p i
k i mod p x Ap
k p Aix
Rational eigenvalues
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.
can express closed form as k x Akx k
p i
k i mod p x Ap
k p Aix
Rational eigenvalues
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
- 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
- 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 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
v vm is maximal
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 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 Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.
x′ = Akx ) takes exponential space, but
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 Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.
x′ = Akx ) takes exponential space, but
( 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 Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.
x′ = Akx ) takes exponential space, but
( 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
Approximating linear loops
SLIDE 36 Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.
[ v1 v2 ... vm ]T.
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 Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.
[ 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 Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.
[ 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 Let A be a matrix with PRSD {⟨p1, λ1, v1⟩, ..., ⟨pm, λm, vm⟩}.
[ 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
Invariant generation pipeline
General loop Linear loop
[KBCR POPL’18]
Periodic rational linear loop
This work
Closed form Invariants
SLIDE 41
Invariant generation pipeline
General loop Linear loop
[KBCR POPL’18]
Periodic rational linear loop
This work
Closed form Invariants
SLIDE 42
Invariant generation pipeline
General loop Linear loop
[KBCR POPL’18]
Periodic rational linear loop
This work
Closed form Invariants
SLIDE 43
Invariant generation pipeline
General loop Linear loop
[KBCR POPL’18]
Periodic rational linear loop
This work
Closed form Invariants
SLIDE 44
Reasoning about non-linear arithmetic
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)
kk kk k is eventully negative
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 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 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
Experiments
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 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 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 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.