This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

11institutetext: Technische Universität Wien
Institut für Informationssysteme 184
Favoritenstraße 9-11
Vienna A-1040, Austria
11email: ahumenbe@forsyte.at
11email: maximilian@mjaroschek.com
11email: lkovacs@forsyte.at

Invariant Generation for Multi-Path Loops with Polynomial Assignments

Andreas Humenberger    Maximilian Jaroschek    Laura Kovács All authors are supported by the ERC Starting Grant 2014 SYMCAR 639270. Furthermore, we acknowledge funding from the Wallenberg Academy Fellowship 2014 TheProSE, the Swedish VR grant GenPro D0497701, and the Austrian FWF research project RiSE S11409-N23. We also acknowledge support from the FWF project W1255-N23.
Abstract

Program analysis requires the generation of program properties expressing conditions to hold at intermediate program locations. When it comes to programs with loops, these properties are typically expressed as loop invariants. In this paper we study a class of multi-path program loops with numeric variables, in particular nested loops with conditionals, where assignments to program variables are polynomial expressions over program variables. We call this class of loops extended P-solvable and introduce an algorithm for generating all polynomial invariants of such loops. By an iterative procedure employing Gröbner basis computation, our approach computes the polynomial ideal of the polynomial invariants of each program path and combines these ideals sequentially until a fixed point is reached. This fixed point represents the polynomial ideal of all polynomial invariants of the given extended P-solvable loop. We prove termination of our method and show that the maximal number of iterations for reaching the fixed point depends linearly on the number of program variables and the number of inner loops. In particular, for a loop with mm program variables and rr conditional branches we prove an upper bound of mrm\cdot r iterations. We implemented our approach in the Aligator software package. Furthermore, we evaluated it on 18 programs with polynomial arithmetic and compared it to existing methods in invariant generation. The results show the efficiency of our approach.

1 Introduction

Reasoning about programs with loops requires loop invariants expressing properties that hold before and after every loop iteration. The difficulty of generating such properties automatically comes from the use of non-linear arithmetic, unbounded data structures, complex control flow, just to name few of the reasons. In this paper we focus on multi-path loops with numeric variables and polynomial arithmetic and introduce an automated approach inferring all loop invariants as polynomial equalities among program variables. For doing so, we identify a class of multi-path loops with nested conditionals, where assignments to program variables are polynomial expressions over program variables. Based on our previous work [4], we call this class of loops extended P-solvable. Compared to [4] where only single-path programs with polynomial arithmetic were treated, in this paper we generalize the notion of extended P-solvable loops to multi-path loops; single-path loops being thus a special case of our method.

For the class of extended P-solvable loops, we introduce an automated approach computing all polynomial invariants. Our work exploits the results of [17, 9] showing that the set of polynomial invariants forms a polynomial ideal, called the polynomial invariant ideal. Hence, the task of generating all polynomial invariants reduces to the problem of generating a basis of the polynomial invariant ideal. Following this observation, given an extended P-solvable loop with nested conditionals, we proceed as follows: we (i) turn the multi-path loop into a sequence of single-path loops, (ii) generate the polynomial invariant ideal of each single-path loop and (iii) combine these ideals iteratively until the polynomial invariant ideal of the multi-path loop is derived.

A crucial property of extended P-solvable loops is that the single-path loops corresponding to one path of the multi-path loop are also extended P-solvable. For generating the polynomial invariant ideal of extended P-solvable single-path loops, we model loops by a system of algebraic recurrences, compute the closed forms of these recurrences by symbolic computation as described in [4] and compute the Gröbner basis of the polynomial invariant ideal from the system of closed forms. When combining the polynomial invariant ideals of each extended P-solvable single-path loop, we prove that the “composition” maintains the properties of extended P-solvable loops. Further, by exploiting the algebraic structures of the polynomial invariant ideals of extended P-solvable loops, we prove that the process of iteratively combining the polynomial invariant ideals of each extended P-solvable single-path loop is finite. That is, a fixed point is reached in a finite number of steps. We prove that this fixed point is the polynomial invariant ideal of the extended P-solvable loop with nested conditionals. We also show that reaching the fixed point depends linearly on the number of program variables and the number of inner loops. In particular, for a loop with mm program variables and rr inner loops (paths) we prove an upper bound of mrm\cdot r iterations. The termination proof of our method implies the completeness of our approach: for an extended P-solvable loop with nested conditionals, our method computes all its polynomial invariants. This result generalizes and corrects the result of [10] on programs for more restricted arithmetic than extended P-solvable loops. Our class of programs extends the programming model of [10] with richer arithmetic and our invariant generation procedure also applies to [10]. As such, our proof of termination also yields a termination proof for [10].

We implemented our approach in the open source Mathematica package Aligator and evaluated our method on 18 challenging examples. When compared to state-of-the-art tools in invariant generation, Aligator performed much better in 14 examples out of 18.

The paper is organized as follows: We start by giving the necessary details about our programming model in Section 2.1 and provide background about polynomial rings and ideals in Section 2.2. In Section 3.1 we recall the notion of extended P-solvable loops from [4]. The lemmas and propositions of Section 3.2 will then help us to prove termination of our invariant generation procedure in Section 3.3. Finally, Section 4 describes our implementation in Aligator, together with an experimental evaluation of our approach.

Related Work. Generation of non-linear loop invariants has been addressed in previous research. We discuss here some of the most related works that we are aware of.

The methods of [11, 18] compute polynomial equality invariants by fixing an a priori bound on the degree of the polynomials. Using this bound, a template invariant of fixed degree is constructed. Properties of polynomial invariants, e.g. inductiveness, are used to generate constraints over the unknown coefficients of the template coefficients and these constraints are then solved in linear or polynomial algebra. An a priori fixed polynomial degree is also used in [16, 2]. Unlike these approaches, in our work we do not fix the degree of polynomial invariants but generate all polynomial invariants (and not just invariants up to a fixed degree). Our restrictions come in the programming model, namely treating only loops with nested conditionals and polynomial arithmetic. For such programs, our approach is complete.

Another line of research uses abstract interpretation in conjunction with recurrence solving and/or polynomial algebra. The work of [17] generates all polynomial invariants of so-called simple loops with nested conditionals. The approach combines abstract interpretation with polynomial ideal theory. Our model of extended P-solvable loops is much more general than simple loops, for example we allow multiplication with the loop counter and treat algebraic, and not only rational, numbers in closed form solutions. Abstract interpretation is also used in [3, 12, 7] to infer non-linear invariants. The programming model of these works handle loops whose assignments induce linear recurrences with constant coefficients. Extended P-solvable loops can however yield more complex recurrence equations. In particular, when comparing our work to [7], we note that the recurrence equations of program variables in [7] correspond to a subclass of linear recurrences with constant coefficients: namely, recurrences whose closed form representations do not include non-rational algebraic numbers. Our work treats the entire class of linear recurrences with constant coefficients and even handles programs whose arithmetic operations induce a class of linear recurrences with polynomial coefficients in the loop counter. While the non-linear arithmetic of our work is more general than the one in [7], we note that the programming model of [7] can handle programs that are more complex than the ones treated in our work, in particular due to the presence of nested loops and function/procedure calls. Further, the invariant generation approach of  [7] is property-guided: invariants are generated in order to prove the safety assertion of the program. Contrarily to this, we generate all invariants of the program and not only the ones implying the safety assertion.

Solving recurrences and computing polynomial invariant ideals from a system of closed form solution is also described in [9]. Our work builds upon the results of [9] but generalizes [9] to extended P-solvable loops. Moreover, we also prove that our invariant generation procedure terminates. Our termination result generalizes [10] by handling programs with more complex polynomial arithmetic. Furthermore, instead of computing the invariant ideals of all permutations of a given set of inner loops and extending this set until a polynomial ideal as a fixed point is reached, we generate the polynomial invariant ideal of just one permutation iteratively until we reach the fixed point. As a result we have to perform less Gröbner basis computations in the process of invariant generation.

A data-driven approach to invariant generation is given in [20], where concrete program executions are used to generate invariant candidates. Machine learning is then used to infer polynomial invariants from the candidate ones. In our work we do not use invariant candidates. While the program flow in our programming model is more restricted then [20], to the best of our knowledge, none of the above cited methods can fully handle the polynomial arithmetic of extended P-solvable loops.

2 Preliminaries

2.1 Programming Model and Invariants

Let 𝕂\mathbb{K} be a computable field of characteristic zero. This means that addition and multiplication can be carried out algorithmically, that there exists an algorithm to test if an element in 𝕂\mathbb{K} is zero, and that the field of rational numbers \mathbb{Q} is a subfield of 𝕂\mathbb{K}. For variables x1,,xnx_{1},\dots,x_{n}, the ring of multivariate polynomials over 𝕂\mathbb{K} is denoted by 𝕂[x1,,xn]\mathbb{K}[x_{1},\dots,x_{n}], or, if the number of variables is clear from (or irrelevant in) the context, by 𝕂[x]\mathbb{K}[\vec{x}]. Correspondingly, 𝕂(x1,,xm)\mathbb{K}(x_{1},\dots,x_{m}) or 𝕂(x)\mathbb{K}(\vec{x}) denotes the field of rational functions over 𝕂\mathbb{K} in x1,,xmx_{1},\dots,x_{m}. If every polynomial in 𝕂[x]\mathbb{K}[x] with a degree 1\geq 1 has at least one root in 𝕂\mathbb{K}, then 𝕂\mathbb{K} is called algebraically closed. An example for such a field is ¯\overline{\mathbb{Q}}, the field of algebraic numbers. In contrast, the field of complex numbers \mathbb{C} is algebraically closed, but not computable, and \mathbb{Q} is computable, but not algebraically closed. We suppose that 𝕂\mathbb{K} is always algebraically closed. This is not necessary for our theory, as we only need the existence of roots for certain polynomials, which is achieved by choosing 𝕂\mathbb{K} to be an appropriate algebraic extension field of \mathbb{Q}. It does, however, greatly simplify the statement of our results.

In our framework, we consider a program BB to be a loop of the form

while … do
   BB^{\prime}
end while
(1)

where BB^{\prime} is a program block that is either the empty block ϵ\epsilon, an assignment vi=f(v1,,vm)v_{i}=f(v_{1},\dots,v_{m}) for a rational function f𝕂(x1,,xm)f\in\mathbb{K}(x_{1},\dots,x_{m}) and program variables v1,,vmv_{1},\dots,v_{m}, or has one of the composite forms

sequential    inner loop    conditional
while … do if … then
   B1;B2B_{1};B_{2}    B1B_{1}    B1B_{1} else B2B_{2}
end while end if

for some program blocks B1B_{1} and B2B_{2} and the usual semantics. We omit conditions for the loop and if statements, as the problem of computing all polynomial invariants is undecidable when taking affine equality tests into account [11]. Consequently, we regard loops as non-deterministic programs in which each block of consecutive assignments can be executed arbitrarily often. More precisely, grouping consecutive assignments into blocks B1,,BrB_{1},\dots,B_{r}, any execution path of BB can be written in the form

B1n1;B2n2;;Brnr;B1nr+1;B2nr+2;B_{1}^{n_{1}};B_{2}^{n_{2}};\dots;B_{r}^{n_{r}};B_{1}^{n_{r+1}};B_{2}^{n_{r+2}};\dots

for a sequence (ni)i(n_{i})_{i\in\mathbb{N}} of non-negative integers with finitely many non-zero elements. To that effect, we interpret any given program (1) as the set of its execution paths, written as

B=(B1;B2;;Br).B=(B_{1}^{*};B_{2}^{*};\dots;B_{r}^{*})^{*}.

We adapt the well-established Hoare triple notation

{P}B{Q},\{P\}B\{Q\}, (2)

for program specifications, where PP and QQ are logical formulas, called the pre- and postcondition respectively, and BB is a program. In this paper we focus on partial correctness of programs, that is a Hoare triple (2) is correct if every terminating computation of BB which starts in a state satisfying PP terminates in a state that satisfies QQ.

In this paper we are concerned with computing polynomial invariants for a considerable subset of loops of the form (1). These invariants are algebraic dependencies among the loop variables that hold after any number of loop iterations.

Definition 1

A polynomial p𝕂[x1,,xm]p\in\mathbb{K}[x_{1},\dots,x_{m}] is a polynomial loop invariant for a loop B=B1;;BrB=B_{1}^{*};\dots;B_{r}^{*} in the program variables v1,,vmv_{1},\dots,v_{m} with initial values v1(0),,vm(0)v_{1}(0),\dots,v_{m}(0), if for every sequence (ni)i(n_{i})_{i\in\mathbb{N}} of non-negative integers with finitely many non-zero elements, the Hoare triple

{p(v1,,vm)=0i=0mvi=vi(0)}\displaystyle\{p(v_{1},\dots,v_{m})=0\wedge\bigwedge_{i=0}^{m}v_{i}=v_{i}(0)\}
B1n1;B2n2,Brnr;B1nr+1;\displaystyle\quad B_{1}^{n_{1}};B_{2}^{n_{2}}\dots,B_{r}^{n_{r}};B_{1}^{n_{r+1}};\dots
{p(v1,,vm)=0}\displaystyle\{p(v_{1},\dots,v_{m})=0\}

is correct.

2.2 Polynomial Rings and Ideals

Polynomial invariants are algebraic dependencies among the values of the variables at each loop iteration. Obviously, non-trivial dependencies do not always exist.

Definition 2

Let 𝕃/𝕂\mathbb{L}\mathbin{/}\mathbb{K} be a field extension. Then a1,,an𝕃{a_{1},\dots,a_{n}\in\mathbb{L}} are algebraically dependent over 𝕂\mathbb{K} if there exists a p𝕂[x1,,xn]{0}p\in\mathbb{K}[x_{1},\dots,x_{n}]\setminus\{0\} such that p(a1,,an)=0p(a_{1},\dots,a_{n})=0. Otherwise they are called algebraically independent.

In [8, 17], it is observed that the set of all polynomial loop invariants for a given loop forms an ideal. It is this fact that facilitates all of our subsequent reasoning.

Definition 3

A subset \mathcal{I} of a commutative ring RR is called an ideal, written R\mathcal{I}\vartriangleleft R, if it satisfies the following three properties:

  1. 1.

    00\in\mathcal{I}.

  2. 2.

    For all a,ba,b\in\mathcal{I}: a+ba+b\in\mathcal{I}.

  3. 3.

    For all aa\in\mathcal{I} and bRb\in R: aba\cdot b\in\mathcal{I}.

Definition 4

Let R\mathcal{I}\vartriangleleft R. Then \mathcal{I} is called

  • proper if it is not equal to RR,

  • prime if aba\cdot b\in\mathcal{I} implies aa\in\mathcal{I} or bb\in\mathcal{I}, and

  • radical if ana^{n}\in\mathcal{I} implies aa\in\mathcal{I}.

The height hg()\operatorname{hg}(\mathcal{I})\in\mathbb{N} of a prime ideal \mathcal{I} is equal to nn if nn is the maximal length of all possible chains of prime ideals 02n=\mathcal{I}_{0}\subset\mathcal{I}_{2}\subset\dots\subset\mathcal{I}_{n}=\mathcal{I}.

Example 1

The set of even integers 22\mathbb{Z} is an ideal of \mathbb{Z}. In general nn\mathbb{Z} for a fixed integer nn is an ideal of \mathbb{Z}. It is prime if and only if nn is a prime number.

Polynomial ideals can informally be interpreted as the set of all consequences when it is known that certain polynomial equations hold. In fact, if we have given a set PP of polynomials of which we know that they serve as algebraic dependencies among the variables of a given loop, the ideal generated by PP then contains all the polynomials that consequently have to be polynomial invariants as well.

Definition 5

A subset BB\subseteq\mathcal{I} of an ideal R\mathcal{I}\vartriangleleft R is called a basis for \mathcal{I} if

=B:={a0b0++ambmm,a0,,amR,b0,bmB}.\mathcal{I}=\langle B\rangle:=\{a_{0}b_{0}+\dots+a_{m}b_{m}\mid m\in\mathbb{N},a_{0},\dots,a_{m}\in R,b_{0}\dots,b_{m}\in B\}.

We say that BB generates \mathcal{I}.

A basis for a given ideal in a ring does not necessarily have to be finite. However, a key result in commutative algebra makes sure that in our setting we only have to consider finitely generated ideals.

Theorem 2.1 (Hilbert’s Basis Theorem – Special case)

Every ideal in 𝕂[x]\mathbb{K}[\vec{x}] has a finite basis.

Subsequently, whenever we say we are given an ideal \mathcal{I}, we mean that we have given a finite basis of \mathcal{I}.

There is usually more than one basis for a given ideal and some are more useful for certain purposes than others. In his seminal PhD thesis [1], Buchberger introduced the notion of Gröbner bases for polynomial ideals and an algorithm to compute them. While, for reasons of brevity, we will not formally define these bases, it is important to note that with their help, central questions concerning polynomial ideals can be answered algorithmically.

Theorem 2.2

Let p𝕂[x1,,xn]p\in\mathbb{K}[x_{1},\dots,x_{n}] and ,𝒥𝕂[x1,,xn]\mathcal{I},\mathcal{J}\vartriangleleft\mathbb{K}[x_{1},\dots,x_{n}]. There exist algorithms to decide the following problems.

  1. 1.

    Decide if pp is an element of \mathcal{I}.

  2. 2.

    Compute a basis of +𝒥\mathcal{I}+\mathcal{J}.

  3. 3.

    Compute a basis of 𝒥\mathcal{I}\cap\mathcal{J}.

  4. 4.

    For {x~1,x~m}{x1,,xn}\{\tilde{x}_{1},\dots\tilde{x}_{m}\}\subseteq\{x_{1},\dots,x_{n}\}, compute a basis of 𝕂[x~1,,x~m]\mathcal{I}\cap\mathbb{K}[\tilde{x}_{1},\dots,\tilde{x}_{m}].

  5. 5.

    Let q𝕂[x]q\in\mathbb{K}[\vec{x}]. Compute a basis for

    :q:={q𝕂[x]n:qnp}.\mathcal{I}\mathbin{:}\langle q\rangle^{\infty}:=\{q\in\mathbb{K}[\vec{x}]\mid\exists n\in\mathbb{N}:q^{n}p\in\mathcal{I}\}.

    The ideal :q\mathcal{I}\mathbin{:}\langle q\rangle^{\infty}\kern-1.0pt is called the saturation of \mathcal{I} with respect to qq.

We will use Gröbner bases to compute the ideal of all algebraic relations among given rational functions. For this, we use the polynomials qiyipiq_{i}y_{i}-p_{i} to model the equations yi=qi/piy_{i}=q_{i}/p_{i} by multiplying the equation with the denominator. In order to model the fact that the denominator is not identically zero, and therefore allowing us to divide by it again, we use the saturation with respect to the least common multiple of all denominators. To see why this is necessary, consider y1=y2=x1x2y_{1}=y_{2}=\frac{x_{1}}{x_{2}}. An algebraic relation among y1y_{1} and y2y_{2} is y1y2y_{1}-y_{2}, but with the polynomials x2y1x1x_{2}y_{1}-x_{1} and x2y2x1x_{2}y_{2}-x_{1}, we only can derive x2(y1y2)x_{2}(y_{1}-y_{2}). We have to divide by x2x_{2}.

Theorem 2.3

Let r1,,rm𝕂(x)r_{1},\dots,r_{m}\in\mathbb{K}(\vec{x}) and let the numerator of rir_{i}\kern-2.0pt be given by pi𝕂[x]p_{i}\in\mathbb{K}[\vec{x}] and the denominator by qi𝕂[x]q_{i}\in\mathbb{K}[\vec{x}]. The ideal of all polynomials pp in 𝕂[y]\mathbb{K}[\vec{y}] with p(r1,,rm)=0p(r_{1},\dots,r_{m})=0 is given by

(i=1mqiyipi):lcm(q1,,qm)𝕂[y],\biggl{(}\smash{\sum_{i=1}^{m}}\langle q_{i}y_{i}-p_{i}\rangle\biggr{)}\mathbin{:}\langle\operatorname{lcm}(q_{1},\dots,q_{m})\rangle^{\infty}\cap\mathbb{K}[\vec{y}],

where lcm()\operatorname{lcm}(\dots) denotes the least common multiple.

Proof

Write d:=lcm(q1,,qm)d:=\operatorname{lcm}(q_{1},\dots,q_{m}). The theorem can be easily verified from the fact that, for any given pp with p(r1,,rm)=0p(r_{1},\dots,r_{m})=0, there exists a kk\in\mathbb{N} such that dkp(r1,,rm)=0d^{k}p(r_{1},\dots,r_{m})=0 is an algebraic relation for p1,,pmp_{1},\dots,p_{m} (by clearing denominators in the equation p(r1,,rm)=0).p(r_{1},\dots,r_{m})=0).

A polynomial ideal 𝕂[x]\mathcal{I}\vartriangleleft\mathbb{K}[\vec{x}] gives rise to a set of points in 𝕂n\mathbb{K}^{n} for which all polynomials in \mathcal{I} vanish simultaneously. This set is called a variety.

Definition 6

Let 𝕂[x1,,xn]\mathcal{I}\vartriangleleft\mathbb{K}[x_{1},\dots,x_{n}] be an ideal. The set

V()={(a1,,an)𝕂np(a1,,an)=0for allp},V(\mathcal{I})=\{(a_{1},\dots,a_{n})\in\mathbb{K}^{n}\mid p(a_{1},\dots,a_{n})=0~\text{for all}~p\in\mathcal{I}\},

is the variety defined by \mathcal{I}.

Varieties are one of the central objects of study in algebraic geometry. Certain geometric shapes like points, lines, circles or balls can be described by prime ideals and come with an intuitive notion of a dimension, e.g. points have dimension zero, lines and circles have dimension one and balls have dimension two. The notion of the Krull dimension of a ring formalizes this intuition when being applied to the quotient ring 𝕂[x]/\mathbb{K}[\vec{x}]/\mathcal{I}. In this paper, we will use the Krull dimension to provide an upper bound for the number of necessary iterations of our algorithm.

Definition 7

The Krull dimension of a commutative ring RR is the supremum of the lengths of all chains 01\mathcal{I}_{0}\subset\mathcal{I}_{1}\subset\dots of prime ideals.

Theorem 2.4

The Krull dimension of 𝕂[x1,,xn]\mathbb{K}[x_{1},\dots,x_{n}] is equal to nn.

3 Extended P-Solvable Loops

In [4] the class of P-solvable loops [9] was extended to so-called extended P-solvable loops. So far, this class captures loops with assignments only, i.e. loops without any nesting of conditionals and loops. In Section 3.3 we close this gap by introducing a new approach for computing invariants of multi-path loops which generalizes the algorithm proposed in [10]. Before dealing with multi-path loops, we recall the notion of extended P-solvable loops in Section 3.1 and showcase the invariant ideal computation.

3.1 Loops with assignments only

In this section, we restrain ourselves to loops whose bodies are comprised of rational function assignments only. This means that we restrict the valid composite forms in a program of the form (1) to sequential compositions and, for the moment, exclude inner loops and conditional branches. We therefore consider a loop L=B1L=B_{1}^{*} where B1B_{1} is a single block containing only variable assignments.

Each variable viv_{i} in a given loop of the form (1) gives rise to a sequence (vi(n))n(v_{i}(n))_{n\in\mathbb{N}}, where nn is the number of loop iterations. The class of eligible loops is then defined based on the form of these sequences. Let r(x)n¯r(x)^{\underline{n}} denote the falling factorial defined as i=0n1r(xi){\prod_{i=0}^{n-1}r(x-i)} for any r𝕂(x)r\in\mathbb{K}(x) and nn\in\mathbb{N}.

Definition 8

A loop with assignments only is called extended P-solvable if each of its recursively changed variables determines a sequence of the form

vi(n)=jpi,j(n,θ1n,,θkn)((n+ζ1)n¯)j1((n+ζ)n¯)jv_{i}(n)=\sum_{j\in\mathbb{Z}^{\ell}}p_{i,j}(n,\theta_{1}^{n},\dots,\theta_{k}^{n})((n+\zeta_{1})^{\underline{n}})^{j_{1}}\cdots((n+\zeta_{\ell})^{\underline{n}})^{j_{\ell}} (3)

where k,k,\ell\in\mathbb{N}, the pi,jp_{i,j} are polynomials in 𝕂(x)[y1,,yk]\mathbb{K}(x)[y_{1},\dots,y_{k}], not identically zero for finitely many jj\in\mathbb{Z}^{\ell}, the θi\theta_{i} are elements of 𝕂\mathbb{K} and the ζi\zeta_{i} are elements of 𝕂\mathbb{K}\setminus\mathbb{Z}^{-} with θiθj\theta_{i}\neq\theta_{j} and ζiζj\zeta_{i}-\zeta_{j}\notin\mathbb{Z} for iji\neq j.

Definition 8 extends the class of P-solvable loops in the sense that each sequence induced by an extended P-solvable loop is the sum of a finitely many hypergeometric sequences. This comprises C-finite sequences as well as hypergeometric sequences and sums and Hadamard products of C-finite and hypergeometric sequences. In contrast, P-solvable loops induce C-finite sequences only. For details on C-finite and hypergeometric sequences we refer to [5].

Every sequence of the form (3) can be written as

vj(1)=rj(v(0),θ,(n+ζ)n¯,n)\smash{v^{(1)}_{j}}\kern-2.0pt=\smash{r_{j}(\vec{v}^{(0)}}\kern-2.0pt,\vec{\theta},(n+\vec{\zeta})^{\underline{n}},n)

where rj=pi/qir_{j}=p_{i}/q_{i} is a rational function, and v(0)v^{(0)} and v(1)v^{(1)} denote the values of vv before and after the execution of the loop. Let I(θ,ζ)𝕂[y0,,yk+]I(\vec{\theta},\vec{\zeta})\vartriangleleft\mathbb{K}[y_{0},\dots,y_{k+\ell}] be the ideal of all algebraic dependencies in the variables y0,,yk+y_{0},\dots,y_{k+\ell} between the sequence (n)n(n)_{n\in\mathbb{N}}, the exponential sequences θ1n,,θkn\theta_{1}^{n},\dots,\theta_{k}^{n} and the sequences (n+ζ1)n¯,,(n+ζ)n¯(n+\zeta_{1})^{\underline{n}},\dots,(n+\zeta_{\ell})^{\underline{n}}. Note that it was shown in [4] that this ideal is the same as the extension of the ideal I(θ)𝕂[y0,,yk]I(\vec{\theta})\vartriangleleft\mathbb{K}[y_{0},\dots,y_{k}] of all algebraic dependencies between the θn\theta^{n}\kern-2.0pt in 𝕂[y0,,yk]\mathbb{K}[y_{0},\dots,y_{k}] to 𝕂[y0,,yk+]\mathbb{K}[y_{0},\dots,y_{k+\ell}], as the factorial sequences (n+ζi)n¯(n+\zeta_{i})^{\underline{n}} are algebraically independent from the exponential sequences θin\theta_{i}^{n}. Now the following proposition states how the invariant ideal of an extended P-solvable loop can be computed.

Proposition 1 ([4])

For an extended P-solvable loop with program variables v1,,vmv_{1},\dots,v_{m} the invariant ideal is given by

((j=1mqj(v(0),y)vj(1)pj(v(0),y)):lcm(q1,,qm)+I(θ,ζ))𝕂[v(1),v(0)].\Biggl{(}\Biggl{(}\sum_{j=1}^{m}\bigl{\langle}\smash{q_{j}(\vec{v}^{(0)}\kern-2.0pt,\vec{y})v^{(1)}_{j}}\kern-2.0pt-\smash{p_{j}(\vec{v}^{(0)}}\kern-2.0pt,\vec{y})\bigr{\rangle}\Biggr{)}\mathbin{:}\langle\operatorname{lcm}(q_{1},\dots,q_{m})\rangle^{\infty}+I(\vec{\theta},\vec{\zeta})\Biggr{)}\cap\mathbb{K}[\vec{v}^{(1)}\kern-2.0pt,\vec{v}^{(0)}].
Example 2

Consider the following loop with relevant program variables a,ba,b and cc.

while true do
   a:=2(n+1)(n+32)aa\mathrel{:=}2\cdot(n+1)(n+\frac{3}{2})\cdot a
   b:=4(n+1)bb\mathrel{:=}4\cdot(n+1)\cdot b
   c:=12(n+32)cc\mathrel{:=}\frac{1}{2}\cdot(n+\frac{3}{2})\cdot c
   n:=n+1n\mathrel{:=}n+1
end while

The extracted recurrence relations admit the following system of closed form solutions:

an\displaystyle a_{n} =2na0(n)n¯(n+12)n¯,\displaystyle=2^{n}\cdot a_{0}\cdot(n)^{\underline{n}}\cdot(n+\frac{1}{2})^{\underline{n}},
bn\displaystyle b_{n} =4nb0(n)n¯,\displaystyle=4^{n}\cdot b_{0}\cdot(n)^{\underline{n}},
cn\displaystyle c_{n} =2nc0(n+12)n¯.\displaystyle=2^{-n}\cdot c_{0}\cdot(n+\frac{1}{2})^{\underline{n}}.

Since every closed form solution is of the form (3) we have an extended P-solvable loop, and we can apply Proposition 1 to compute the invariant ideal:

(+I(θ,ζ))𝕂[a(1),b(1),c(1),a(0),b(0),c(0)]=b(1)c(1)a(0)a(1)b(0)c(0),\displaystyle(\mathcal{I}+I(\vec{\theta},\vec{\zeta}))\cap\mathbb{K}[a^{(1)}\kern-2.0pt,b^{(1)}\kern-2.0pt,c^{(1)}\kern-2.0pt,a^{(0)}\kern-2.0pt,b^{(0)}\kern-2.0pt,c^{(0)}]=\langle b^{(1)}\kern-2.0pt\cdot c^{(1)}\kern-2.0pt\cdot a^{(0)}\kern-2.0pt-a^{(1)}\kern-2.0pt\cdot b^{(0)}\kern-2.0pt\cdot c^{(0)}\rangle,

where

\displaystyle\mathcal{I} =\displaystyle\;=\; a(1)y1a(0)z1z2,b(1)y2b(0)z1,c(1)y3c(0)z2,\displaystyle\langle a^{(1)}\kern-2.0pt-y_{1}\cdot a^{(0)}\kern-2.0pt\cdot z_{1}z_{2},b^{(1)}\kern-2.0pt-y_{2}\cdot b^{(0)}\kern-2.0pt\cdot z_{1},c^{(1)}\kern-2.0pt-y_{3}\cdot c^{(0)}\kern-2.0pt\cdot z_{2}\rangle,
I(θ,ζ)\displaystyle I(\vec{\theta},\vec{\zeta}) =\displaystyle\;=\; y12y2,y1y31,y2y3y1.\displaystyle\langle y_{1}^{2}-y_{2},y_{1}y_{3}-1,y_{2}y_{3}-y_{1}\rangle.

The ideal I(θ,ζ)I(\vec{\theta},\vec{\zeta}) in variables y1,y2,y3y_{1},y_{2},y_{3} is the set of all algebraic dependencies among 2n,4n2^{n},4^{n} and 2n2^{-n}, and \mathcal{I} is generated by the closed form solutions where exponential and factorial sequences are replaced by variables y1,y2,y3y_{1},y_{2},y_{3} and z1,z2z_{1},z_{2}.

3.2 Algebraic Dependencies of Composed Rational Functions with Side Conditions

In this section we give the prerequisites for proving termination of the invariant generation method for multi-path loops (Section 3.3). The results of this section will allow us to proof termination by applying Theorem 2.4.

Let v(i)=v1(i),,vm(i)\vec{v}^{(i)}=v_{1}^{(i)},\dots,v_{m}^{(i)} and y(i)=y1(i),,y(i)\vec{y}^{(i)}=y_{1}^{(i)},\dots,y_{\ell}^{(i)} for ii\in\mathbb{N}. We model the situation in which the value of the jjth loop variable after the execution of the iith block in (1) is given by a rational function in the y(i)\vec{y}^{(i)} (which, for us, will be the exponential and factorial sequences as well as the loop counter) and the ‘old’ variable values v(i1)\vec{v}^{(i-1)} and is assigned to vj(i)v_{j}^{(i)}. Set 0=j=1mvj(1)vj(0){\mathcal{I}_{0}=\sum_{j=1}^{m}\langle v^{(1)}_{j}\kern-2.0pt-v^{(0)}_{j}\rangle} and let Ii𝕂[y(i)]{I_{i}\vartriangleleft\mathbb{K}[\vec{y}^{(i)}]} for ii\in\mathbb{N}^{*}. Furthermore, let qj(i),pj(i)𝕂[v(i),y(i)]q^{(i)}_{j},p^{(i)}_{j}\kern-2.0pt\in\mathbb{K}[\vec{v}^{(i)}\kern-2.0pt,\vec{y}^{(i)}] such that for fixed ii there exists a yV(Ii)\vec{y}\in V(I_{i}) with pj(i)(v(i),y)/qj(i)(v(i),y)=vj(i){p_{j}^{(i)}(\vec{v}^{(i)}\kern-2.0pt,\vec{y})/q_{j}^{(i)}(\vec{v}^{(i)}\kern-2.0pt,\vec{y})=\vec{v}^{(i)}_{j}\kern-2.0pt} for all jj and with di:=lcm(q1(i),,qm(i))d_{i}:=\operatorname{lcm}(q^{(i)}_{1},\dots,q^{(i)}_{m}) we have diIid_{i}\notin I_{i} and di(vi,y)=1d_{i}(\vec{v_{i}},\vec{y})=1. Set

Ji=j=1mqj(i)(v(i),y(i))vj(i+1)pj(i)(v(i),y(i)).J_{i}=\sum_{j=1}^{m}\langle q^{(i)}_{j}(\vec{v}^{(i)}\kern-2.0pt,\vec{y}^{(i)})v^{(i+1)}_{j}\kern-2.0pt-p^{(i)}_{j}(\vec{v}^{(i)}\kern-2.0pt,\vec{y}^{(i)})\rangle.
Remark 1

The requirement for the existence of a point y\vec{y} in V(Ii)V(I_{i}) such that pj(i)(v(i),y)/qj(i)(v(i),y)=vj(i){p_{j}^{(i)}(\vec{v}^{(i)}\kern-2.0pt,\vec{y})/q_{j}^{(i)}(\vec{v}^{(i)}\kern-2.0pt,\vec{y})=\vec{v}^{(i)}_{j}\kern-2.0pt} for all jj and di(vi,y)=1d_{i}(\vec{v_{i}},\vec{y})=1 is always fulfilled in our context, as it is a formalization of the fact that the execution of a loop LL^{*} also allows that it is executed zero times, meaning the values of the program variables do not change.

In order to develop some intuition about the following, consider a list of consecutive loops L1;L2;L3;L_{1};L_{2};L_{3};\dots where each of them is extended P-solvable. Intuitively, the ideals IiI_{i} then correspond to the ideal of algebraic dependencies among the exponential and factorial sequences occurring in LiL_{i}, whereas JiJ_{i} stands for the ideal generated by the closed form solutions of LiL_{i}. Moreover, the variables vj(i+1)v_{j}^{(i+1)} correspond to the values of the loop variables after the execution of the loop LiL_{i}. The following iterative computation then allows us to generate the invariant ideal for L1;L2;L3;L_{1};L_{2};L_{3};\dots

i:=((Ji+i1+Ii):di)𝕂[v(i+1),v(0)]\mathcal{I}_{i}:=((J_{i}+\mathcal{I}_{i-1}+I_{i})\mathbin{:}\langle d_{i}\rangle^{\infty})\cap\mathbb{K}[\vec{v}^{(i+1)}\kern-2.0pt,\vec{v}^{(0)}]

Now the remaining part of this section is devoted to proving properties of the ideals i\mathcal{I}_{i} which will help us to show that there exists an index kk such that k=k\mathcal{I}_{k}=\mathcal{I}_{k^{\prime}} for all k>kk^{\prime}>k for a list of consecutive loops L1;;Lr;L1;;Lr;L_{1};\dots;L_{r};L_{1};\dots;L_{r};\dots with rr\in\mathbb{N}.

First note that the ideal i\mathcal{I}_{i} can be rewritten as

i={p\displaystyle\mathcal{I}_{i}=\{p 𝕂[v(i+1),v(0)]qi1,k:\displaystyle\in\smash{\mathbb{K}[\vec{v}^{(i+1)}\kern-2.0pt,\vec{v}^{(0)}]}\mid\exists q\in\mathcal{I}_{i-1},k\in\mathbb{N}:
qdikp(r1(i)(v(i),y(i)),,rm(i)(v(i),y(i)),v(0))(𝐦𝐨𝐝Ii)}.\displaystyle q\equiv\smash{d_{i}^{k}p(r^{(i)}_{1}(\vec{v}^{(i)}\kern-2.0pt,\vec{y}^{(i)}),\dots,r^{(i)}_{m}(\vec{v}^{(i)}\kern-2.0pt,\vec{y}^{(i)}),\vec{v}^{(0)})}\;\;(\operatorname{\mathbf{mod}}\;I_{i})\}. (4)

If IiI_{i} is radical, an equation 𝐦𝐨𝐝Ii\mathbf{mod}\;I_{i} is, informally speaking, the same as substituting y\vec{y} with values from V(Ii)V(I_{i}), so (3.2) translates to

i={p\displaystyle\mathcal{I}_{i}=\{p 𝕂[v(i+1),v(0)]qi1,k:\displaystyle\in\smash{\mathbb{K}[\vec{v}^{(i+1)}\kern-2.0pt,\vec{v}^{(0)}]}\mid\exists q\in\mathcal{I}_{i-1},k\in\mathbb{N}:
yV(Ii):q=dikp(r1(i)(v(i),y),,rm(i)(v(i),y),v(0))}.\displaystyle\forall\vec{y}\in V(I_{i}):q=\smash{d_{i}^{k}p(r^{(i)}_{1}(\vec{v}^{(i)}\kern-2.0pt,\vec{y}),\dots,r^{(i)}_{m}(\vec{v}^{(i)}\kern-2.0pt,\vec{y}),\vec{v}^{(0)})}\}. (5)

We now get the following subset relation between two consecutively computed ideals i\mathcal{I}_{i}.

Lemma 1

If IiI_{i} is radical, then ii1|v(i1)v(i)\mathcal{I}_{i}\subseteq\mathcal{I}_{i-1}|_{\vec{v}^{(i-1)}\leftarrow\vec{v}^{(i)}}.

Proof

Let pip\in\mathcal{I}_{i}. We have to show that there is an ri2r\in\mathcal{I}_{i-2} and a kk\in\mathbb{N} such that

rdi1kp(r1(i1)(v(i1),y(i1)),,rm(i1)(v(i1),y(i1)),v(0))(𝐦𝐨𝐝Ii1).r\equiv d_{i-1}^{k}\smash{p(r_{1}^{(i-1)}(\vec{v}^{(i-1)}\kern-2.0pt,\vec{y}^{(i-1)}),\dots,r^{(i-1)}_{m}(\vec{v}^{(i-1)}\kern-2.0pt,\vec{y}^{(i-1)}),\vec{v}^{(0)})}\;\;(\operatorname{\mathbf{mod}}\;I_{i-1}).

Since IiI_{i} is radical, there is a qi1q\in\mathcal{I}_{i-1}, a zz\in\mathbb{N}, and a yV(Ii)\vec{y}\in V(I_{i}) with

q=dizp(r1(i)(v(i),y),,rm(i)(v(i),y),v(0))=p(v(i),v(0)).q=d_{i}^{z}\smash{p(r^{(i)}_{1}(\vec{v}^{(i)}\kern-2.0pt,\vec{y}),\dots,r^{(i)}_{m}(\vec{v}^{(i)}\kern-2.0pt,\vec{y}),\vec{v}^{(0)})=p(\vec{v}^{(i)}\kern-2.0pt,\vec{v}^{(0)})}.

Then, by Equation (3.2) for i1\mathcal{I}_{i-1}, there is an ri2r\in\mathcal{I}_{i-2} with the desired property.

For prime ideals, we get an additional property:

Lemma 2

If i1\mathcal{I}_{i-1} and IiI_{i} are prime, then so is i\mathcal{I}_{i}.

Proof

Let abia\cdot b\in\mathcal{I}_{i} and denote by a|ra|_{r} and b|rb|_{r} the rational functions where each vj(i+1)v_{j}^{(i+1)} is substituted by rj(i)r^{(i)}_{j} in a,ba,b respectively. Then there is a qi1q\in\mathcal{I}_{i-1} and a k=k1+k2k=k_{1}+k_{2}\in\mathbb{N} with dik1a|r,dik2b|r𝕂[v(i+1),v(0)]d_{i}^{k_{1}}a|_{r},d_{i}^{k_{2}}b|_{r}\in\mathbb{K}[\vec{v}^{(i+1)}\kern-2.0pt,\vec{v}^{(0)}]

qdik(ab)|rdik1a|rdik2b|r(𝐦𝐨𝐝Ii)q\equiv d_{i}^{k}(a\cdot b)|_{r}\equiv d_{i}^{k_{1}}a|_{r}\cdot d_{i}^{k_{2}}b|_{r}\;\;(\operatorname{\mathbf{mod}}\;I_{i})

If dika|rd_{i}^{k}a|_{r} is zero modulo IiI_{i}, then aa is an element of i\mathcal{I}_{i}, as 0i10\in\mathcal{I}_{i-1}. The same argument holds for bb. Suppose that dik1a|r,dik2b|r0(𝐦𝐨𝐝Ii)d_{i}^{k_{1}}a|_{r},d_{i}^{k_{2}}b|_{r}\not\equiv 0\;(\operatorname{\mathbf{mod}}\;I_{i}). Then, since IiI_{i} is prime, 𝕂[y(i)]/Ii\mathbb{K}[\vec{y}^{(i)}]/I_{i} is an integral domain, and so it follows that q0(𝐦𝐨𝐝Ii)q\not\equiv 0\;(\operatorname{\mathbf{mod}}\;I_{i}). Now, because i1\mathcal{I}_{i-1} is prime, it follows without loss of generality that dik1a|ri1d_{i}^{k_{1}}a|_{r}\in\mathcal{I}_{i-1}, from which we get aia\in\mathcal{I}_{i}.

We now use Lemmas 1 and 2 to give details about the minimal decomposition of i\mathcal{I}_{i}.

Proposition 2

For fixed i0i_{0}\in\mathbb{N}, let all IiI_{i}, 0ii00\leq i\leq i_{0} be radical and let i0=k=0nPk\mathcal{I}_{i_{0}}=\smash{\bigcap_{k=0}^{n}}P_{k} be the minimal decomposition of i0\mathcal{I}_{i_{0}}. Then

  1. 1.

    for each kk there exist prime ideals Ik,1,Ik,2,I_{k,1},I_{k,2},\dots such that PkP_{k} is equal to a k,i0\mathcal{I}_{k,{i_{0}}} constructed as above with J1,,Ji0J_{1},\dots,J_{i_{0}} and Ik,1,,Ik,i0I_{k,1},\dots,I_{k,{i_{0}}}.

  2. 2.

    if Ii0+1I_{i_{0}+1} is radical and i0+1=j=0nPj\mathcal{I}_{{i_{0}}+1}=\smash{\bigcap_{j=0}^{n^{\prime}}}P^{\prime}_{j} is the minimal decomposition of i+1,\mathcal{I}_{i+1}, then, for each PjP^{\prime}_{j} there exists a PkP_{k} such that PjPk|v(i0)v(i0+1)P^{\prime}_{j}\subseteq P_{k}|_{\vec{v}^{(i_{0})}\leftarrow\vec{v}^{(i_{0}+1)}}.

Proof

We prove 1. by induction. For i0=0i_{0}=0, there is nothing to show. Now assume the claim holds for some i0i_{0}\in\mathbb{N} and let Ii0+1=j=0wQjI_{i_{0}+1}=\smash{\bigcap_{j=0}^{w}}Q_{j} be the minimal decomposition of Ii0+1I_{i_{0}+1}. With this we get

i0+1\displaystyle\mathcal{I}_{i_{0}+1} =(Ji0+1+i0+Ii0+1):di0+1𝕂[v(i0+1),v(0)]\displaystyle=(J_{i_{0}+1}+\mathcal{I}_{i_{0}}+I_{i_{0}+1})\mathbin{:}\langle d_{i_{0}+1}\rangle^{\infty}\cap\mathbb{K}[\vec{v}^{(i_{0}+1)}\kern-2.0pt,\vec{v}^{(0)}]
=(k=0nJi0+1+Pk+j=0wQj):di0+1𝕂[v(i+1),v(0)]\displaystyle=\left(\bigcap_{k=0}^{n}J_{i_{0}+1}+P_{k}+\bigcap_{j=0}^{w}Q_{j}\right)\mathbin{:}\langle d_{i_{0}+1}\rangle^{\infty}\cap\mathbb{K}[\vec{v}^{(i+1)}\kern-2.0pt,\vec{v}^{(0)}]
=(k=0nj=0w(Ji0+1+Pk+Qj):di0+1𝕂[v(i0+1),v(0)]I~k,j).\displaystyle=\raisebox{-1.5pt}{$\Biggl{(}$}\bigcap_{k=0}^{n}\bigcap_{j=0}^{w}\underbrace{(J_{i_{0}+1}+P_{k}+Q_{j})\mathbin{:}\langle d_{i_{0}+1}\rangle^{\infty}\cap\mathbb{K}[\vec{v}^{(i_{0}+1)}\kern-2.0pt,\vec{v}^{(0)}]}_{\tilde{I}_{k,j}}\raisebox{-1.5pt}{$\Biggr{)}$}.

By the induction hypothesis, each PkP_{k} admits a construction as above, and thus so does I~k,j\tilde{I}_{k,j}. By Lemma 2, I~k,j\tilde{I}_{k,j} is prime. This shows 1. The second claim then follows from the fact that the prime ideals in the minimal decomposition of i0+1\mathcal{I}_{i_{0}+1} are obtained from the PkP_{k} via Ji0+1J_{i_{0}+1} and QjQ_{j}. Since the QjQ_{j} are prime, they are also radical, and the claim follows from Lemma 1.

3.3 Loops with conditional branches

In this section, we extend the results of Section 3.1 to loops with conditional branches. Without loss of generality, we define our algorithm for a loop of the form

whiledoL1;L2;;Lrend while\textbf{while}\ \dots\ \textbf{do}\ L_{1};L_{2};\dots;L_{r}\ \textbf{end~while}

where Li=BiL_{i}=B_{i}^{*} and BiB_{i} is a block containing variable assignments only.

Let I(θi,ζi)I(\vec{\theta}_{i},\vec{\zeta}_{i}) denote the ideal of all algebraic dependencies as described in Section 3.1 for a inner loop LiL_{i}. As every inner loop provides its own loop counter, we have that the exponential and factorial sequences of distinct inner loops are algebraically independent. Therefore I(θ,ζ):=i=0rI(θi,ζi)I(\vec{\theta},\vec{\zeta}):=\sum_{i=0}^{r}I(\vec{\theta}_{i},\vec{\zeta}_{i}) denotes the set of all algebraic dependencies between exponential and factorial sequences among the inner loops L1,,LrL_{1},\dots,L_{r}.

Consider loop bodies B1,,BrB_{1},\dots,B_{r} with common loop variables v1,,vmv_{1},\dots,v_{m}. Suppose the closed form of vjv_{j} in the iith loop body is given by a rational function in m+k++1m+k+\ell+1 variables:

vj(i+1)=rj(i)(v(i),θn,(n+ζ)n¯,n),\smash{v^{(i+1)}_{j}}\kern-2.0pt=\smash{r^{(i)}_{j}(\vec{v}^{(i)}}\kern-2.0pt,\vec{\theta}^{n},(n+\vec{\zeta})^{\underline{n}},n),

where vj(i)\smash{v^{(i)}_{j}}\kern-2.0pt and vj(i+1)\smash{v^{(i+1)}_{j}}\kern-2.0pt are variables for the value of vjv_{j} before and after the execution of the loop body. Then we can compute the ideal of all polynomial invariants of the non-deterministic program (B1;B2;;Br)(B_{1}^{*};B_{2}^{*};\dots;B_{r}^{*})^{*} with Algorithm 1.

Algorithm 1 Invariant generation via fixed point computation
1:Loop bodies B1,,BrB_{1},\dots,B_{r} as described.
2:The ideal of all polynomial invariants of (B1;B2;;Br)(B_{1}^{*};B_{2}^{*};\dots;B_{r}^{*})^{*}.  
3:Compute I:=I(θ,ζ)I:=I(\vec{\theta},\vec{\zeta}) as described above
4:old={0}\mathcal{I}_{old}=\{0\}, new=j=1mvj(1)vi(0)\mathcal{I}_{new}=\smash{\sum_{j=1}^{m}\langle v^{(1)}_{j}-v^{(0)}_{i}\rangle}, j=0j=0
5:WHILE old|v((j1)r+1)v(jr+1)new\mathcal{I}_{old}|_{\vec{v}^{((j-1)\cdot r+1)}\leftarrow\vec{v}^{(j\cdot r+1)}}\neq\mathcal{I}_{new} AND new{0}\mathcal{I}_{new}\neq\{0\} DO
6:  oldnew\mathcal{I}_{old}\leftarrow\mathcal{I}_{new}, jj+1j\leftarrow j+1
7:  FOR i=1,,ri=1,\dots,r DO
8:   new(Jij+old+I)𝕂[v(ij+1),v(0)]\mathcal{I}_{new}\leftarrow(J_{i\cdot j}+\mathcal{I}_{old}+I)\cap\mathbb{K}[\vec{v}^{(i\cdot j+1)},\vec{v}^{(0)}]   
9:RETURN new\mathcal{I}_{new}
Lemma 3

I(θ,ζ)I(\vec{\theta},\vec{\zeta}) is a radical ideal.

Proof

The elements of I(θ)I(\vec{\theta}) represent C-finite sequences, i.e. sequences of the form

f1(n)θ1n++fknθkn,f_{1}(n)\theta_{1}^{n}+\dots+f_{k}^{n}\theta_{k}^{n},

for univariate polynomials f1,,fk𝕂[y0]f_{1},\dots,f_{k}\in\mathbb{K}[y_{0}] and pairwise distinct θ1,,θk𝕂\theta_{1},\dots,\theta_{k}\in\mathbb{K}. The claim is then proven by the fact that the Hadamard-product a2(n,a(0))a^{2}(n,a(0)) of a C-finite sequence a(n,a(0))a(n,a(0)) with itself is zero if and only if a(n,a(0))a(n,a(0)) is zero, and I(θ,ζ)I(\vec{\theta},\vec{\zeta}) is the extension of I(θ)I(\vec{\theta}) to 𝕂[y0,,yk+]\mathbb{K}[y_{0},\dots,y_{k+\ell}].

Theorem 3.1

Algorithm 1 is correct and terminates.

Proof

The algorithm iteratively computes the ideals 1,2,\mathcal{I}_{1},\mathcal{I}_{2},\dots as in Section 3.2, so we will refer to IoldI_{old} and InewI_{new} as i\mathcal{I}_{i} and i+1\mathcal{I}_{i+1}.

Termination: 0\mathcal{I}_{0} is a prime ideal of height mm. Suppose after an execution of the outer loop, the condition i|v(i)v(i+1)i+1\mathcal{I}_{i}|_{\vec{v}^{(i)}\leftarrow\vec{v}^{(i+1)}}\neq\mathcal{I}_{i+1} holds. As I(θ,ζ)I(\vec{\theta},\vec{\zeta}) is radical by Lemma 3, we then get i+1i|v(i)v(i+1)\mathcal{I}_{i+1}\subset\mathcal{I}_{i}|_{\vec{v}^{(i)}\leftarrow\vec{v}^{(i+1)}} by Lemma 1. Thus there is a p𝕂[v(i+1),v(0)]{p\in\mathbb{K}[\vec{v}^{(i+1)}\kern-2.0pt,\vec{v}^{(0)}]} with pi|v(i)v(i+1)p\in\mathcal{I}_{i}|_{\vec{v}^{(i)}\leftarrow\vec{v}^{(i+1)}} and pi+1p\notin\mathcal{I}_{i+1}. Then, by Proposition 2, all prime ideals PkP_{k} in the minimal decomposition of i+1\mathcal{I}_{i+1} are have to be subsets of the prime ideals in the minimal decomposition of i|v(i)v(i+1)\mathcal{I}_{i}|_{\vec{v}^{(i)}\leftarrow\vec{v}^{(i+1)}}, where at least one of the subset relations is proper. Since pi+1p\notin\mathcal{I}_{i+1}, the height of at least one PkP_{k} has to be reduced. The height of each prime ideal is bounded by the height of 0\mathcal{I}_{0}.

Correctness: Let ii\in\mathbb{N} be fixed and denote by I(B;i)𝕂[v(i+1),v(0)]I(B;i)\vartriangleleft\mathbb{K}[\vec{v}^{(i+1)},\vec{v}^{(0)}] the ideal of all polynomial invariants for the non-deterministic program

(B1;;Br)i/r;B1;;Bi𝐫𝐞𝐦r.(B_{1}^{*};\dots;B_{r}^{*})^{\nicefrac{{i}}{{r}}};B_{1}^{*};\dots;B_{i\;\mathrel{\operatorname{\mathbf{rem}}}\;r}^{*}.

It suffices to show that i\mathcal{I}_{i} is equal to I(B;i)I(B;i). In fact, after i0i_{0} iterations with i0=i0+1=i0+2=\mathcal{I}_{i_{0}}=\mathcal{I}_{i_{0}+1}=\mathcal{I}_{i_{0}+2}=\dots, it follows that i0\mathcal{I}_{i_{0}} is the ideal of polynomial invariants for (B1;;Br).(B_{1}^{*};\dots;B_{r}^{*})^{*}\kern-2.0pt. Let pI(B;i)p\in I(B;i). The value of the program variable vjv_{j} in the program B1;;Bi𝐫𝐞𝐦rB_{1}^{*};\dots;B_{i\;\mathrel{\operatorname{\mathbf{rem}}}\;r}^{*} is given as the value of a composition of the closed forms of each BkB_{k}:

vj=pj(i)(p(i1)((p(1)(v(0),sn1),),sni1),sni),v_{j}=\smash{p^{(i)}_{j}\bigg{(}p^{(i-1)}\Big{(}\dots\big{(}p^{(1)}(\vec{v}^{(0)}\kern-2.0pt,\vec{s}_{n_{1}}),\dots\big{)},\vec{s}_{n_{i-1}}\Big{)},\vec{s}_{n_{i}}\bigg{)}},

with sn=n,θn,(n+ζ)n¯\vec{s}_{n}=n,\vec{\theta}^{n},(n+\vec{\zeta})^{\underline{n}} and n1,,nin_{1},\dots,n_{i}\in\mathbb{N}. The correctness then follows from the fact that that i\mathcal{I}_{i} is the ideal of all such compositions under the side condition that (θn,(n+ζ)n¯,n)V(I(θ,ζ))(\vec{\theta}^{n},(n+\vec{\zeta})^{\underline{n}},n)\in V(I(\vec{\theta},\vec{\zeta})) for any nn\in\mathbb{N}.

Revisiting the subset relations of the prime ideals in the minimal decomposition of 0,1,\mathcal{I}_{0},\mathcal{I}_{1},\dots gives an upper bound for the necessary number of iterations in the algorithm.

Corollary 1

Algorithm 1 terminates after at most mm iterations of the while-loop at line 5.

Proof

Suppose the algorithm terminates after k0k_{0} iterations of the outer loop. We look at the ideals rk\mathcal{I}_{r\cdot k}, k{0,,k0}k\in\{0,\dots,k_{0}\}. For a prime ideal PP in the minimal decomposition of any r(k+1)\mathcal{I}_{r\cdot(k+1)}, there is a prime ideal QQ in the minimal decomposition of rk\mathcal{I}_{r\cdot k} such that PQP\subseteq Q. If P=QP=Q, then PP is a prime ideal in the minimal decomposition of each r(k)\mathcal{I}_{r\cdot(k^{\prime})}, k>kk^{\prime}>k. This holds because there are only rr many JiJ_{i}. So if QQ does not get replaced by smaller prime ideals in rk+1,rk+2,r(k+1)\mathcal{I}_{r\cdot k+1},\mathcal{I}_{r\cdot k+2}\dots,\mathcal{I}_{r\cdot(k+1)}, it has to be part of the minimal decomposition for any subsequent i\mathcal{I}_{i}. From this it follows that, for each kk, there is a prime ideal PkP_{k} in the minimal decomposition in rk\mathcal{I}_{r\cdot k}, such that P0P1Pk0P_{0}\supset P_{1}\supset\dots\supset P_{k_{0}} is a chain of proper superset relations, which then proves the claim since the height of P0=0P_{0}=\mathcal{I}_{0} is mm.

Example 3

Consider a multi-path loop LL

whiledoL1;L2end while\textbf{while}\ \dots\ \textbf{do}~L_{1};L_{2}~\textbf{end~while}

containing the following nested loops L1L_{1} and L2L_{2} and the corresponding closed form solutions:

while … do
   a:=aba\mathrel{:=}a-b
   p:=pqp\mathrel{:=}p-q
   r:=rsr\mathrel{:=}r-s
end while
an=a0nb0a_{n}=a_{0}-nb_{0}
pn=p0nq0p_{n}=p_{0}-nq_{0}
rn=r0ns0r_{n}=r_{0}-ns_{0}
while … do
   b:=bab\mathrel{:=}b-a
   q:=qpq\mathrel{:=}q-p
   s:=srs\mathrel{:=}s-r
end while
bm=b0ma0b_{m}=b_{0}-ma_{0}
qm=q0mp0q_{m}=q_{0}-mp_{0}
sm=s0mr0s_{m}=s_{0}-mr_{0}

For simplicity we chose inner loops without algebraic dependencies, i.e. II at line 3 will be the zero ideal and we therefore neglect it in the following computation. Moreover, we write aia_{i} instead of a(i)a^{(i)}. We start with

0=a1a0,b1b0,p1p0,q1q0,r1r0,s1s0\mathcal{I}_{0}=\langle a_{1}-a_{0},b_{1}-b_{0},p_{1}-p_{0},q_{1}-q_{0},r_{1}-r_{0},s_{1}-s_{0}\rangle

followed by the first loop iteration:

1\displaystyle\mathcal{I}_{1} =(J1+0)𝕂[a0,b0,p0,q0,r0,s0,a2,b2,p2,q2,r2,s2]\displaystyle=(J_{1}+\mathcal{I}_{0})\cap\mathbb{K}[a_{0},b_{0},p_{0},q_{0},r_{0},s_{0},a_{2},b_{2},p_{2},q_{2},r_{2},s_{2}]
=b0b2,q0q2,s0s2,p0s2+p2s2+q2r0q2r2,\displaystyle=\langle b_{0}-b_{2},q_{0}-q_{2},s_{0}-s_{2},-p_{0}s_{2}+p_{2}s_{2}+q_{2}r_{0}-q_{2}r_{2},
a0s2a2s2b2r0+b2r2,a0q2a2q2b2p0+b2p2\displaystyle\qquad a_{0}s_{2}-a_{2}s_{2}-b_{2}r_{0}+b_{2}r_{2},a_{0}q_{2}-a_{2}q_{2}-b_{2}p_{0}+b_{2}p_{2}\rangle

where

J1=a2a1+b1n,p2p1+q1n,r2r1+s1n,b2b1,q2q1,s2s1J_{1}=\langle a_{2}-a_{1}+b_{1}n,p_{2}-p_{1}+q_{1}n,r_{2}-r_{1}+s_{1}n,b_{2}-b_{1},q_{2}-q_{1},s_{2}-s_{1}\rangle

The following ideal 2\mathcal{I}_{2} is then the invariant ideal for the first iteration of the outer loop LL.

2\displaystyle\mathcal{I}_{2} =(J2+1)𝕂[a0,b0,p0,q0,r0,s0,a3,b3,p3,q3,r3,s3]\displaystyle=(J_{2}+\mathcal{I}_{1})\cap\mathbb{K}[a_{0},b_{0},p_{0},q_{0},r_{0},s_{0},a_{3},b_{3},p_{3},q_{3},r_{3},s_{3}]
=p0r3s0+p3r3s3+p3r0s0p3r0s3q3r32+q3r0r3,\displaystyle=\langle-p_{0}r_{3}s_{0}+p_{3}r_{3}s_{3}+p_{3}r_{0}s_{0}-p_{3}r_{0}s_{3}-q_{3}r_{3}^{2}+q_{3}r_{0}r_{3},
p3s0+p3s3+q0r3q3r3,p0s0+p3s3+q0r0q3r3,\displaystyle\qquad-p_{3}s_{0}+p_{3}s_{3}+q_{0}r_{3}-q_{3}r_{3},-p_{0}s_{0}+p_{3}s_{3}+q_{0}r_{0}-q_{3}r_{3},
a3s0a3s3b0r3+b3r3,a0q0a3q3b0p0+b3p3,\displaystyle\qquad a_{3}s_{0}-a_{3}s_{3}-b_{0}r_{3}+b_{3}r_{3},a_{0}q_{0}-a_{3}q_{3}-b_{0}p_{0}+b_{3}p_{3},
a3p0s3a3p3s3a3q3r0+a3q3r3b0p3r0+b3p3r0+b0p0r3b3p0r3,\displaystyle\qquad a_{3}p_{0}s_{3}-a_{3}p_{3}s_{3}-a_{3}q_{3}r_{0}+a_{3}q_{3}r_{3}-b_{0}p_{3}r_{0}+b_{3}p_{3}r_{0}+b_{0}p_{0}r_{3}-b_{3}p_{0}r_{3},
a3q0a3q3b0p3+b3p3,a0s0a3s3b0r0+b3r3,\displaystyle\qquad a_{3}q_{0}-a_{3}q_{3}-b_{0}p_{3}+b_{3}p_{3},a_{0}s_{0}-a_{3}s_{3}-b_{0}r_{0}+b_{3}r_{3},
a0p3s3+a3p3s3+a0q3r3a3q3r3+b0p3r0b0p0r3,\displaystyle\qquad-a_{0}p_{3}s_{3}+a_{3}p_{3}s_{3}+a_{0}q_{3}r_{3}-a_{3}q_{3}r_{3}+b_{0}p_{3}r_{0}-b_{0}p_{0}r_{3},
a3b0r0+a3b3r3+a0b0r3a0b3r3a32s3+a0a3s3,\displaystyle\qquad-a_{3}b_{0}r_{0}+a_{3}b_{3}r_{3}+a_{0}b_{0}r_{3}-a_{0}b_{3}r_{3}-a_{3}^{2}s_{3}+a_{0}a_{3}s_{3},
a3b0p0+a3b3p3+a0b0p3a0b3p3a32q3+a0a3q3\displaystyle\qquad-a_{3}b_{0}p_{0}+a_{3}b_{3}p_{3}+a_{0}b_{0}p_{3}-a_{0}b_{3}p_{3}-a_{3}^{2}q_{3}+a_{0}a_{3}q_{3}\rangle

where

J2=b3b2+a2m,q3q2+p2m,s3s2+r2m,a3a2,p3p2,r3r2J_{2}=\langle b_{3}-b_{2}+a_{2}m,q_{3}-q_{2}+p_{2}m,s_{3}-s_{2}+r_{2}m,a_{3}-a_{2},p_{3}-p_{2},r_{3}-r_{2}\rangle

By continuing this computation we get the following ideals 4\mathcal{I}_{4} and 6\mathcal{I}_{6} which are the invariant ideals after two and three iterations of the outer loop LL respectively.

4\displaystyle\mathcal{I}_{4} =p0s0p5s5r0q0+r5q5,\displaystyle=\langle p_{0}s_{0}-p_{5}s_{5}-r_{0}q_{0}+r_{5}q_{5},
b5p5b0p0+a0q0a5q5,\displaystyle\qquad b_{5}p_{5}-b_{0}p_{0}+a_{0}q_{0}-a_{5}q_{5},
b5r5b0r0+a0s0a5s5,\displaystyle\qquad b_{5}r_{5}-b_{0}r_{0}+a_{0}s_{0}-a_{5}s_{5},
b5(p5s0+r5q0)+b0(p5s5r5q5)+a5(s5q0+s0q5),\displaystyle\qquad b_{5}(-p_{5}s_{0}+r_{5}q_{0})+b_{0}(p_{5}s_{5}-r_{5}q_{5})+a_{5}(-s_{5}q_{0}+s_{0}q_{5}),
b5(p5r0+p0r5)+a5(p0s5+r0q5)+a0(p5s5r5q5),\displaystyle\qquad b_{5}(-p_{5}r_{0}+p_{0}r_{5})+a_{5}(-p_{0}s_{5}+r_{0}q_{5})+a_{0}(p_{5}s_{5}-r_{5}q_{5}),
b0p0(p5s5+r5q5)+b5(p52s5p0r5q0+p5(r0q0r5q5))+\displaystyle\qquad b_{0}p_{0}(-p_{5}s_{5}+r_{5}q_{5})+b_{5}(p_{5}^{2}s_{5}-p_{0}r_{5}q_{0}+p_{5}(r_{0}q_{0}-r_{5}q_{5}))+{}
a5(p0s5q0+q5(p5s5r0q0+r5q5))\displaystyle\qquad\qquad a_{5}(p_{0}s_{5}q_{0}+q_{5}(-p_{5}s_{5}-r_{0}q_{0}+r_{5}q_{5}))\rangle
6\displaystyle\mathcal{I}_{6} =p0s0p7s7r0q0+r7q7,\displaystyle=\langle p_{0}s_{0}-p_{7}s_{7}-r_{0}q_{0}+r_{7}q_{7},
b7p7b0p0+a0q0a7q7,\displaystyle\qquad b_{7}p_{7}-b_{0}p_{0}+a_{0}q_{0}-a_{7}q_{7},
b7r7b0r0+a0s0a7s7,\displaystyle\qquad b_{7}r_{7}-b_{0}r_{0}+a_{0}s_{0}-a_{7}s_{7},
b7(p7s0+r7q0)+b0(p7s7r7q7)+a7(s7q0+s0q7),\displaystyle\qquad b_{7}(-p_{7}s_{0}+r_{7}q_{0})+b_{0}(p_{7}s_{7}-r_{7}q_{7})+a_{7}(-s_{7}q_{0}+s_{0}q_{7}),
b7(p7r0+p0r7)+a7(p0s7+r0q7)+a0(p7s7r7q7),\displaystyle\qquad b_{7}(-p_{7}r_{0}+p_{0}r_{7})+a_{7}(-p_{0}s_{7}+r_{0}q_{7})+a_{0}(p_{7}s_{7}-r_{7}q_{7}),
b0p0(p7s7+r7q7)+b7(p72s7p0r7q0+p7(r0q0r7q7))+\displaystyle\qquad b_{0}p_{0}(-p_{7}s_{7}+r_{7}q_{7})+b_{7}(p_{7}^{2}s_{7}-p_{0}r_{7}q_{0}+p_{7}(r_{0}q_{0}-r_{7}q_{7}))+{}
a7(p0s7q0+q7(p7s7r0q0+r7q7))\displaystyle\qquad\qquad a_{7}(p_{0}s_{7}q_{0}+q_{7}(-p_{7}s_{7}-r_{0}q_{0}+r_{7}q_{7}))\rangle

Note that we now reached the fixed point as 6=4|v(5)v(7)\mathcal{I}_{6}=\mathcal{I}_{4}|_{\vec{v}^{(5)}\leftarrow\vec{v}^{(7)}}.

Corollary 1 provides a bound on the number of iterations in Algorithm 1. Therefore, we know at which stage we have to reach the fixed point of the computation at the latest, viz. after computing rm\mathcal{I}_{r\cdot m}. This fact allows us to construct a new algorithm which computes the ideal rm\mathcal{I}_{r\cdot m} directly instead of doing a fixed point computation. The benefit of Algorithm 2 is that we have to perform only one Gröbner basis computation in the end, although the new algorithm might performs more iterations than Algorithm 1.

Algorithm 2 Invariant generation without fixed point computation
1:Loop bodies B1,,BrB_{1},\dots,B_{r} as described.
2:The ideal of all polynomial invariants of (B1;B2;;Br)(B_{1}^{*};B_{2}^{*};\dots;B_{r}^{*})^{*}.  
3:Compute I:=I(θ,ζ)I:=I(\vec{\theta},\vec{\zeta}) as described above
4:new=j=1mvj(1)vi(0)+I\mathcal{I}_{new}=\smash{\sum_{j=1}^{m}\langle v^{(1)}_{j}-v^{(0)}_{i}\rangle}+I
5:FOR j=1,,mj=1,\dots,m DO
6:  FOR i=1,,ri=1,\dots,r DO
7:   new(Jij+new)\mathcal{I}_{new}\leftarrow(J_{i\cdot j}+\mathcal{I}_{new})   
8:RETURN new𝕂[v(mr+1),v(0)]\mathcal{I}_{new}\cap\mathbb{K}[\vec{v}^{(m\cdot r+1)},\vec{v}^{(0)}]

The proof of termination of the invariant generation method of [10] assumes that the ideal of algebraic dependencies is prime. In general, this does not hold. Consider the following loop and its closed forms with exponential sequences 2n2^{n} and (2)n(-2)^{n}:

while … do
   x:=2xx\mathrel{:=}2x
   y:=2yy\mathrel{:=}-2y
end while
x(n)=2nx(0)x(n)=2^{n}\cdot x(0)
y(n)=(2)ny(0)y(n)=(-2)^{n}\cdot y(0)

The ideal of algebraic dependencies among the before-mentioned exponential sequences is given by a2b2\langle a^{2}-b^{2}\rangle which is obviously not prime. As a consequence, the termination proof of [10] is incorrect. This paper closes this gap by providing a new algorithm and a corresponding termination proof.

4 Implementation and Experiments

We implemented our method in the Mathematica package Aligator 111Aligator requires the Mathematica packages Hyper [14], Dependencies [6] and FastZeil [13], where the latter two are part of the compilation package ErgoSum [15].. Aligator is open source and available at:

https://ahumenberger.github.io/aligator/

Comparison of generated invariants.

Based on the examples in Figure 1 we show that our technique can infer invariants which cannot be found by other state-of-the-art approaches. Our observations indicate that our method is superior to existing approaches if the loop under consideration has some mathematical meaning like division or factorization algorithms as depicted in Figure 1, whereas the approach of [7] has advantages when it comes to programs with complex flow.

The techniques of [2] and [7] were implemented in tools called Fastind222Available at http://www.irisa.fr/celtique/ext/polyinv/ and Duet333Available at https://github.com/zkincaid/duet respectively. Unlike Aligator and Fastind, Duet is not a pure inference engine for polynomial invariants, instead it tries to prove user-specified safety assertions. In order to check which invariants can be generated by Duet, we therefore asserted the invariants computed by Aligator and checked if Duet can prove them.

while aba\neq b do
   if a>ba>b then
    a:=aba\mathrel{:=}a-b
    p:=pqp\mathrel{:=}p-q
    r:=rsr\mathrel{:=}r-s
   else
    b:=bab\mathrel{:=}b-a
    q:=qpq\mathrel{:=}q-p
    s:=srs\mathrel{:=}s-r
   end if
end while
(a)
while r0r\neq 0 do
   if r>0r>0 then
    r:=rvr\mathrel{:=}r-v
    v:=v+2v\mathrel{:=}v+2
   else
    r:=r+ur\mathrel{:=}r+u
    u:=u+2u\mathrel{:=}u+2
   end if
end while
(b)
while dEd\geq E do
   if P<a+bP<a+b then
    b:=b/2b\mathrel{:=}b/2
    d:=d/2d\mathrel{:=}d/2
   else
    a:=a+ba\mathrel{:=}a+b
    y:=y+d/2y\mathrel{:=}y+d/2
    b:=b/2b\mathrel{:=}b/2
    d:=d/2d\mathrel{:=}d/2
   end if
end while
(c)
Figure 1: Three examples: (a) Extended Euclidean algorithm, (b) a variant of Fermat’s factorization algorithm and (c) Wensley’s algorithm for real division.

Let us consider the loop depicted in Figure 1(a). Since we treat conditional branches as inner loops, we have that the invariants for this loop are the same as for the loop in Example 3. By instantiating the generated invariants with the following initial values on the left we get the following polynomial invariants on the right:

a0\displaystyle\hskip 50.00008pta_{0} x\displaystyle\mapsto x\qquad
b0\displaystyle b_{0} y\displaystyle\mapsto y\qquad
p0\displaystyle p_{0} 1\displaystyle\mapsto 1\qquad
q0\displaystyle q_{0} 0\displaystyle\mapsto 0\qquad
r0\displaystyle r_{0} 0\displaystyle\mapsto 0\qquad
s0\displaystyle s_{0} 1\displaystyle\mapsto 1
1+qrps\displaystyle 1+qr-ps (I1I_{1})
bpaqy\displaystyle bp-aq-y (I2I_{2})
bras+x\displaystyle br-as+x (I3I_{3})
bp+aqqry+psy\displaystyle-bp+aq-qry+psy (I4I_{4})
brasqrx+psx\displaystyle br-as-qrx+psx (I5I_{5})
(qrps)(bp+aq+y)\displaystyle(qr-ps)(-bp+aq+y) (I6I_{6})

Note that (I4I_{4})-(I6I_{6}) are just linear combinations of (I1)(I_{1})-(I3)(I_{3}). However, Fastind was able to infer (I1)(I_{1})-(I3)(I_{3}), whereas Duet was only able to prove (I2)(I_{2}), (I5)(I_{5}) and (I6)(I_{6}).

Other examples where Aligator is superior in terms of the number of inferred invariants are given by the loops in Figures 1(b) and 1(c). For Fermat’s algorithm (Figure 1(b)) and the following initial values, Aligator found one invariant, which was also found by Fastind. However, Duet was not able to prove it.

u0\displaystyle\hskip 40.00006ptu_{0} 2R+1\displaystyle\mapsto 2R+1
v0\displaystyle v_{0} 1\displaystyle\mapsto 1
r0\displaystyle r_{0} RRN\displaystyle\mapsto RR-N
4N4r2u+u2+2vv2\displaystyle-4N-4r-2u+u^{2}+2v-v^{2} (I7I_{7})

In case of Wensley’s algorithm (Figure 1(c)) Aligator was able to identify the following three invariants. Fastind inferred the first two invariants, whereas Duet could not prove any of them.

a0\displaystyle\hskip 50.00008pta_{0} 0\displaystyle\mapsto 0
b0\displaystyle b_{0} Q/2\displaystyle\mapsto Q/2
d0\displaystyle d_{0} 1\displaystyle\mapsto 1
y0\displaystyle y_{0} 0\displaystyle\mapsto 0
2bdQ\displaystyle 2b-dQ (I8I_{8})
ad2by\displaystyle ad-2by (I9I_{9})
aQy\displaystyle a-Qy (I10I_{10})

Benchmarks and Evaluation.

For the experimental evaluation of our approach, we used the following set of examples: (i) 18 programs taken from [2]; (ii) 4 new programs of extended P-solvable loops that were created by us. All examples are available at the repository of Aligator.

Our experiments were performed on a machine with a 2.9 GHz Intel Core i5 and 16 GB LPDDR3 RAM; for each example, a timeout of 300300 seconds was set. When using Aligator, the Gröbner basis of the invariant ideal computed by Aligator was non-empty for each example; that is, for each example we were able to find non-trivial invariants.

We evaluated Aligator against Fastind. As Duet is not a pure inference engine for polynomial invariants, we did not include it in the following evaluation. When compared to [2], we note that we do not fix the degree of the polynomial invariants to be generated. Moreover, our method is complete. That is, whenever Aligator terminates, the basis of the polynomial invariant ideal is inferred; any other polynomial invariant is a linear combination of the basis polynomials.

Table 1: Experimental evaluation of Aligator.
(a)
Single-path Aligator Fastind
cohencu 0.0720.072 0.0430.043
freire1 0.0160.016 0.0410.041
freire2 0.0620.062 0.0480.048
petter1 0.0150.015 0.0400.040
petter2 0.0260.026 0.0420.042
petter3 0.0350.035 0.0510.051
petter4 0.0420.042 0.1040.104
petter5 0.0530.053 0.2610.261
petter20 48.29048.290 9.8169.816
petter22 247.820247.820 9.8829.882
petter23 TOTO 9.8539.853
(b)
Multi-path #b\#b #v\#v #i\#i Al1 Al2 Fastind
divbin 22 33 22 0.1340.134 45.94845.948 0.0450.045
euclidex 22 66 33 0.4330.433 TOTO 0.0490.049
fermat 22 33 22 0.0450.045 0.0600.060 0.0430.043
knuth 44 55 22 55.791~55.791 TOTO 1.0251.025
lcm 22 44 33 0.0510.051 87.752~87.752 0.0430.043
mannadiv 22 33 22 0.0220.022 0.0250.025 0.0480.048
wensley 22 44 22 0.1240.124 41.85141.851 errerr
extpsolv2 22 33 22 0.1920.192 TOTO errerr
extpsolv3 33 33 22 0.2950.295 TOTO errerr
extpsolv4 44 33 22 0.3650.365 TOTO errerr
extpsolv10 1010 33 22 0.9510.951 TOTO errerr
#b,#v\#b,\#v \dots number of branches, variables
#i\#i \dots number of iterations until fixed point reached
Al1 \dots Aligator with Algorithm 1 (timeout 300s300s)
Al2 \dots Aligator with Algorithm 2 (timeout 100s100s)
Fastind \dots OCaml version of the tool in [2]444Testing the Maple implementation was not possible due to constraints regarding the Maple version.
TO,errTO,err \dots timeout, error

Table 1(a) summarizes our experimental results on single-path loops, whereas Table 1(b) reports on the results from multi-path programs. The first column of each table lists the name of the benchmark. The second and third columns of Table 1(a) report, on the timing results of Aligator and Fastind, respectively. In Table 1(b), the second column lists the number of branches (paths) of the multi-path loop, whereas the third column gives the number of variables used in the program. The fourth column reports on the number of iterations until the fixed point is reached by Aligator, and hence terminates. The fifth and sixth columns, labeled Al1 and Al2, show the performance of Aligator when using Algorithm 1 or Algorithm 2, respectively. The last column of Table 1(b) lists the results obtained by Fastind. In both tables, timeouts are denoted by TOTO, whereas errors, due to the fact that the tool cannot be evaluated on the respective example, are given as errerr.

The results reported in Tables 1(a) and 1(b) show the efficiency of Aligator: in 14 out of 18 examples, Aligator performed significantly better than FastInd. For the examples petter20, petter22 and petter23, the time-consuming part in Aligator comes from recurrence solving (computing the closed form of the recurrence), and not from the Gröbner basis computation. We intend to improve this part of Aligator in the future. The examples extpsolv2, extpsolv3, extpsolv4 and extpsolv10 are extended P-solvable loops with respectively 2, 3, 4, and 10 nested conditional branches. The polynomial arithmetic of these examples is not supported by Fastind. The results of Aligator on these examples indicate that extended P-solvable loops do not increase the complexity of computing the invariant ideal.

We also compared the performance of Aligator with Algorithm 1 against Algorithm 2. As shown in columns 5 and 6 of Table 1(b), Algorithm 2 is not as efficient as Algorithm 1, even though Algorithm 2 uses only a single Gröbner basis computation. We conjecture that this is due to the increased number of variables in the polynomial system which influences the Gröbner basis computation. We therefore conclude that several small Gröbner basis computations (with fewer variables) perform better than a single large one.

5 Conclusions

We proposed a new algorithm for computing the ideal of all polynomial invariants for the class of extended P-solvable multi-path loops. The new approach computes the invariant ideal for a non-deterministic program (L1;;Lr)(L_{1};\dots;L_{r})^{*} where the LiL_{i} are single-path loops. As a consequence, the proposed method can handle loops containing (i) an arbitrary nesting of conditionals, as these conditional branches can be transformed into a sequence of single-path loops by introducing flags, and (ii) one level of nested single-path loops.

Our method computes the ideals 1,2,\mathcal{I}_{1},\mathcal{I}_{2},\dots until a fixed point is reached where i\mathcal{I}_{i} denotes the invariant ideal of (L1;;Lr)i(L_{1};\dots;L_{r})^{i}. This fixed point is then a basis for the ideal containing all polynomial invariants for the extended P-solvable loop. We showed that this fixed point computation is guaranteed to terminate which implies the completeness of our method. Furthermore, we gave a bound on the number of iterations we have to perform to reach the fixed point. The proven bound is given by mm iterations where mm is the number of loop variables.

We showed that our method can generate invariants which cannot be inferred by other state-of-the-art techniques. In addition, we showcased the efficiency of our approach by comparing our Mathematica package Aligator with state-of-the-art tools in invariant generation.

Future research directions include the incorporation of the loop condition into our method. So far we operate on an abstraction of the loop where we ignore the loop condition and treat the loop as a non-deterministic program. By doing so we might loose valuable information about the control flow of the program. By employing ΠΣ\mathrm{\Pi\Sigma^{*}}-theory [19] it might be possible to extend our work also to loops containing arbitrary nesting of inner loops, which reflects another focus for further research.

Acknowledgments. We want to thank the anonymous reviewers for their helpful comments and remarks.

References

  • [1] Buchberger, B.: An Algorithm for Finding the Basis Elements of the Residue Class Ring of a Zero Dimensional Polynomial Ideal. J. Symbolic Computation 41(3-4), 475–511 (2006)
  • [2] Cachera, D., Jensen, T.P., Jobin, A., Kirchner, F.: Inference of Polynomial Invariants for Imperative Programs: A Farewell to Gröbner Bases. In: Miné, A., Schmidt, D. (eds.) Static Analysis - 19th International Symposium, SAS 2012, Deauville, France, September 11-13, 2012. Proceedings. Lecture Notes in Computer Science, vol. 7460, pp. 58–74. Springer (2012)
  • [3] Farzan, A., Kincaid, Z.: Compositional recurrence analysis. In: Proc. of FMCAD. pp. 57–64. FMCAD Inc, Austin, TX (2015)
  • [4] Humenberger, A., Jaroschek, M., Kovács, L.: Automated Generation of Non-Linear Loop Invariants Utilizing Hypergeometric Sequences. In: Proceedings of the 2017 ACM on International Symposium on Symbolic and Algebraic Computation. pp. 221–228. ISSAC ’17, ACM, New York, NY, USA (2017)
  • [5] Kauers, M., Paule, P.: The Concrete Tetrahedron. Text and Monographs in Symbolic Computation, Springer Wien, 1st edn. (2011)
  • [6] Kauers, M., Zimmermann, B.: Computing the algebraic relations of C-finite sequences and multisequences. Journal of Symbolic Computation 43(11), 787 – 803 (2008)
  • [7] Kincaid, Z., Cyphert, J., Breck, J., Reps, T.: Non-Linear Reasoning For Invariant Synthesis. In: POPL (2018), to appear
  • [8] Kovács, L.: Automated Invariant Generation by Algebraic Techniques for Imperative Program Verification in Theorema. Ph.D. thesis, RISC, Johannes Kepler University Linz (October 2007)
  • [9] Kovács, L.: Reasoning Algebraically About P-Solvable Loops. In: Tools and Algorithms for the Construction and Analysis of Systems, 14th International Conference, TACAS 2008, Held as Part of the Joint European Conferences on Theory and Practice of Software, ETAPS 2008, Budapest, Hungary, March 29-April 6, 2008. Proceedings. pp. 249–264 (2008)
  • [10] Kovács, L.: A Complete Invariant Generation Approach for P-solvable Loops. In: Perspectives of Systems Informatics, 7th International Andrei Ershov Memorial Conference, PSI 2009, Novosibirsk, Russia, June 15-19, 2009. Revised Papers. pp. 242–256 (2009)
  • [11] Müller-Olm, M., Seidl, H.: A Note on Karr’s Algorithm. In: Automata, Languages and Programming: 31st International Colloquium, ICALP 2004, Turku, Finland, July 12-16, 2004. Proceedings. pp. 1016–1028 (2004)
  • [12] de Oliveira, S., Bensalem, S., Prevosto, V.: Polynomial invariants by linear algebra. In: Artho, C., Legay, A., Peled, D. (eds.) Proc. of ATVA. pp. 479–494. Springer (2016)
  • [13] Paule, P., Schorn, M.: A Mathematica Version of Zeilberger’s Algorithm for Proving Binomial Coefficient Identities. Journal of Symbolic Computation 20, 673 – 698 (1995)
  • [14] Petkovšek, M.: Mathematic package hyper (1998), http://www.fmf.uni-lj.si/~petkovsek/
  • [15] Research Institute for Symbolic Computation.: Mathematic Package ErgoSum (2016), http://www.risc.jku.at/research/combinat/software/ergosum/
  • [16] Rodriguez-Carbonell, E., Kapur, D.: Automatic Generation of Polynomial Invariants of Bounded Degree using Abstract Interpretation. J. Science of Computer Programming 64(1), 54–75 (2007)
  • [17] Rodríguez-Carbonell, E., Kapur, D.: Generating all polynomial invariants in simple loops. Journal of Symbolic Computation 42(4), 443 – 476 (2007)
  • [18] Sankaranarayanan, S., Sipma, H.B., Manna, Z.: Non-linear loop invariant generation using gröbner bases. In: Proc. of POPL. pp. 318–329. ACM, New York, NY, USA (2004)
  • [19] Schneider, C.: Summation theory ii: Characterizations of rπσr\pi\sigma-extensions and algorithmic aspects. J. Symb. Comput. 80(3), 616–664 (2017), arXiv:1603.04285 [cs.SC]
  • [20] Sharma, R., Gupta, S., Hariharan, B., Aiken, A., Liang, P., Nori, A.V.: A Data Driven Approach for Algebraic Loop Invariants. In: Felleisen, M., Gardner, P. (eds.) Programming Languages and Systems - 22nd European Symposium on Programming, ESOP 2013, Held as Part of the European Joint Conferences on Theory and Practice of Software, ETAPS 2013, Rome, Italy, March 16-24, 2013. Proceedings. Lecture Notes in Computer Science, vol. 7792, pp. 574–592. Springer (2013)