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 can be used to answer questions about program behavior i j n j i After exec Before exec Loop counter i The problem : given a loop, compute a formula that represents its behavior. k while (i < n): i := i + 2 j := j + 1 k i i j k j k n n i n i valid?
• Is i Loop summarization The problem : given a loop, compute a formula that represents its behavior. j loop n j Summary can be used to answer questions about program behavior i j n j i After exec Before exec Loop counter i valid? while (i < n): i := i + 2 j := j + 1 i ′ = i + 2 k j ′ = j + k ∧ ∃ k ∈ N . n ′ = n ∧ i ′ ≥ n ∧ ( k ≥ 1 ⇒ i ′ ≤ n + 1) ∧
Loop summarization while (i < n): i := i + 2 j := j + 1 Before exec After exec Summary can be used to answer questions about program behavior Loop counter The problem : given a loop, compute a formula that represents its behavior. i = j = 0 ∧ n > 0 ∧ i ′ = i + 2 k j ′ = j + k ∧ ∃ k ∈ N . n ′ = n ∧ i ′ ≥ n ∧ ( k ≥ 1 ⇒ i ′ ≤ n + 1) ∧ ∧¬ (2 j ′ = i ′ ) • Is { i = j = 0 ∧ n > 0 } loop { 2 j = i } valid?
• In the paper: affine & solvable polynomial loops Today: Linear loops while ( * ): non-deterministic [Rodríguez-Carbonell & Kapur, ISAAC 2004]. x := A x A ∈ Q n × n
Today: Linear loops while ( * ): non-deterministic [Rodríguez-Carbonell & Kapur, ISAAC 2004]. x := A x A ∈ Q n × n • In the paper: affine & solvable polynomial loops
• Practical applications • Any loop can be approximated by a linear loop [KBCR POPL’18] • Summary for the approximation gives invariants for the loop Why linear loops? • Natural problem
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
k hi Approximating general loops [KBCR POPL’18] x ticks hi lo y z s s v v k kz x z y k y z z k ticks ticks k hi lo x y binary-search(A,target) : while (*): 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 x x y z x y z ticks lo hi mid target A lo
k hi Approximating general loops [KBCR POPL’18] y y lo hi ticks x z A x s target mid hi lo ticks z z s binary-search(A,target) : z lo hi k ticks ticks k z v k y y kz x x k v y x lo else if A[mid] < target: while (*): x y z else : hi = mid-1 lo = mid+1 Not a linear transformation return mid if A[mid] == target: lo = 1, hi = size(A), ticks = 0 mid = lo + (hi-lo)/2 ticks++; while (lo <= hi): 1 0 1 1 := 0 0 2 0 0 1
k hi Approximating general loops [KBCR POPL’18] v z binary-search(A,target) : ticks lo hi mid target A x y z s s k v x x x kz y k y z z k ticks ticks k hi lo y lo hi = mid-1 else if A[mid] < target: x y z else : while (*): lo = mid+1 return mid if A[mid] == target: mid = lo + (hi-lo)/2 ticks++; while (lo <= hi): lo = 1, hi = size(A), ticks = 0 Not a linear transformation ⇐ ∼ ⇒ x = ticks ∧ hi − lo ≤ y ∧ z = 1 1 0 1 1 := 0 0 2 0 0 1
k hi Approximating general loops [KBCR POPL’18] x lo hi ticks x z y A z target mid hi lo ticks z y s x z lo hi k ticks ticks k z v k y y kz x x k binary-search(A,target) : y lo y 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 z � s ′ v ′ 1 0 1 � 1 := 0 0 2 0 0 1
k hi Approximating general loops [KBCR POPL’18] hi z ticks lo hi mid target A x y z x ticks lo binary-search(A,target) : y z s s v v k ticks ticks k hi lo y x lo z 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 (*): y x x ′ = x + kz 1 0 1 ∧ y ′ = (1/2) k y 1 ∃ k ∈ N . := 0 0 ∧ z ′ = z 2 0 0 1
Approximating general loops [KBCR POPL’18] x y lo hi ticks x z y A s target mid hi lo ticks binary-search(A,target) : z z s y lo = mid+1 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: else : v hi = mid-1 Not a linear transformation while (*): v x y z x ( ticks ′ = ticks + k ) ∃ k ∈ N . ∧ hi ′ − lo ′ ≤ (1/2) k ( hi − lo ) x ′ = x + kz 1 0 1 ∧ y ′ = (1/2) k y 1 ∃ k ∈ N . := 0 0 ∧ z ′ = z 2 0 0 1
Hasn’t this problem already been solved? n k d n Algebraic numbers Camille Jordan while (*): x A x k x A k x Given a square matrix A ∈ Q n × n , can compute A k symbolically Entries of A k are exponential polynomials: 1 k d 1 + · · · + a n λ k a 1 λ k
Hasn’t this problem already been solved? n k d n Algebraic numbers Camille Jordan while (*): Given a square matrix A ∈ Q n × n , can compute A k symbolically Entries of A k are exponential polynomials: 1 k d 1 + · · · + a n λ k a 1 λ k ∃ k ∈ N . x ′ = A k x x := A x
No. Skolem’s problem (variant): Given an exponential-polynomial f over the alge- Decidability of Skolem’s problem is unknown! Thoraf Skolem Essential problem: algebraic numbers. braic numbers, does there exists some n ∈ N such that f ( k ) = 0 ?
No. Skolem’s problem (variant): Given an exponential-polynomial f over the alge- Decidability of Skolem’s problem is unknown! Thoraf Skolem Essential problem: algebraic numbers. braic numbers, does there exists some n ∈ N such that f ( k ) = 0 ?
Outline Starting point of this work: avoid algebraic numbers 2 All matrices have best periodic-rational approximations. 3 Exponential-polynomial arithmetic over is decidable. 1 Periodic rational matrices have closed forms over Q . • Computable in polytime
Outline Starting point of this work: avoid algebraic numbers 2 All matrices have best periodic-rational approximations. 3 Exponential-polynomial arithmetic over is decidable. 1 Periodic rational matrices have closed forms over Q . • Computable in polytime
Outline Starting point of this work: avoid algebraic numbers 2 All matrices have best periodic-rational approximations. 1 Periodic rational matrices have closed forms over Q . • Computable in polytime 3 Exponential-polynomial arithmetic over Q is decidable.
Closed forms for linear loops
A k can be • [Boigelot PhD thesis ’99]: A generates a finite monoid power p such that A p has rational eigenvalues. • A periodic rational k p A i x Known: p A p x i mod p k i x k A k x k can express closed form as Common generalization: A matrix A is periodic rational if there is some expressed in Presburger arithmetic. Rational eigenvalues • Eigenvalues of A are rational ⇒ A k can be expressed in exponential-polynomial arithmetic over Q .
power p such that A p has rational eigenvalues. • A periodic rational k p A i x Known: k A p x i mod p k i p x A k x k can express closed form as Common generalization: A matrix A is periodic rational if there is some expressed in Presburger arithmetic. Rational eigenvalues • Eigenvalues of A are rational ⇒ A k can be expressed in exponential-polynomial arithmetic over Q . • [Boigelot PhD thesis ’99]: A generates a finite monoid ⇒ A k can be
• A periodic rational k p A i x Known: k A p x i mod p k i p x A k x k can express closed form as expressed in Presburger arithmetic. Rational eigenvalues • Eigenvalues of A are rational ⇒ A k can be expressed in exponential-polynomial arithmetic over Q . • [Boigelot PhD thesis ’99]: A generates a finite monoid ⇒ A k can be Common generalization: A matrix A is periodic rational if there is some power p such that A p has rational eigenvalues.
Known: expressed in Presburger arithmetic. Rational eigenvalues • Eigenvalues of A are rational ⇒ A k can be expressed in exponential-polynomial arithmetic over Q . • [Boigelot PhD thesis ’99]: A generates a finite monoid ⇒ A k can be Common generalization: A matrix A is periodic rational if there is some power p such that A p has rational eigenvalues. • A periodic rational ⇒ can express closed form as ( ) p − 1 ( ) ∨ ∃ k ∈ N . x ′ = A k x k ≡ i mod p ∧ x ′ = ( A p ) ⌊ k / p ⌋ A i x ≡ ∃ k ∈ N . i =0
Recommend
More recommend