(Un)Solvable Loop Analysis

Automatically generating invariants, key to computer-aided analysis of probabilistic and deterministic programs and compiler optimisation, is a challenging open problem. Whilst the problem is in general undecidable, the goal is settled for restricted classes of loops. For the class of solvable loops, introduced by Kapur and Rodr´ıguez-Carbonell in 2004, one can automatically compute invariants from closed-form solutions of recurrence equations that model the loop behaviour. In this paper we establish a technique for invariant synthesis for loops that are not solvable, termed unsolvable loops. Our approach automatically partitions the program variables and identiﬁes the so-called defective variables that characterise unsolvability. Herein we consider the following two applications. First, we present a novel technique that automatically synthesises polynomials from defective monomials, that admit closed-form solutions and thus lead to polynomial loop invariants. Second, given an unsolvable loop, we synthesise solvable loops with the following property: the invariant polynomials of the solvable loops are all invariants of the given unsolvable loop. Our implementation and experiments demonstrate both the feasibility and applicability of our approach to both deterministic and probabilistic programs.


Introduction 1.Background and Motivation
With substantial progress in computer-aided program analysis and automated reasoning, several techniques have emerged to automatically synthesise loop invariants, thus advancing a central challenge in the computer-aided verification of programs with loops.In this paper, we address the problem of automatically generating loop invariants in the presence of polynomial arithmetic, which is still unsolved.This problem remains unsolved even when we restrict consideration to loops that are non-nested, without conditionals, and/or without exit conditions.Our work improves the state of the art under such and similar considerations.
Loop invariants, in the sequel simply invariants, are properties that hold before and after every iteration of a loop.As such, invariants provide the key inductive arguments for automating the verification of programs; for example, proving correctness of deterministic loops [2][3][4][5][6] and correctness of hybrid and probabilistic loops [7][8][9], or data flow analysis and compiler optimisation [10].One challenging aspect in invariant synthesis is the derivation of polynomial invariants for arithmetic loops.Such invariants are defined by polynomial relations P (x 1 , . . ., x k ) = 0 among the program variables x 1 , . . ., x k .While deriving polynomial invariants is, in general, undecidable [11], efficient invariant synthesis techniques emerge when considering restricted classes of polynomial arithmetic in so-called solvable loops [2], such as loops with (blocks of) affine assignments [3][4][5][6].
A common approach for constructing polynomial invariants, first pioneered in [12,13], is to (i) map a loop to a system of recurrence equations modelling the behaviour of program variables; (ii) derive closed-forms for program variables by solving the recurrences; and (iii) compute polynomial invariants by eliminating the loop counter n from the closed-forms.The central components in this setting follow.In step (i) a recurrence operator is employed to map loops to recurrences, which leads to closedforms for the program variables as exponential polynomials in step (ii); that is, each program variable is written as a finite sum of the form j P j (n)λ n j parameterised by the nth loop iteration for polynomials P j and algebraic numbers λ j .From the theory of algebraic recurrences, this is the case if and only if the behaviour of each variable z ← 0 while ⋆ do z ← 1 − z x ← 2x + y 2 + z y ← 2y − y 2 + 2z end while Closed-form of x + y: x(n) + y(n) = 2 n (x(0) + y(0) + 2) − (−1) n /2 − 3/2 (a) The program P .
Fig. 1: Two running examples with unsolvable recurrence operators.Nevertheless, P admits a closed-form for combinations of variables and P SC admits a polynomial invariant.Herein we use ⋆ (rather than a loop guard or true) as loop termination is not our focus.For the avoidance of doubt, in this paper we consider standard mathematical arithmetic (e.g.mathematical integers) rather than machine floating-point and finite precision arithmetic.obeys a linear recurrence equation with constant coefficients [14,15].Exploiting this result, the class of recurrence operators that can be linearised are called solvable [2].Intuitively, a loop with a recurrence operator is solvable only if the non-linear dependencies in the resulting system of polynomial recurrences are acyclic (see Section 3).However, even simple loops may fall outside the category of solvable operators, but still admit polynomial invariants and closed-forms for combinations of variables.This phenomenon is illustrated in Figure 1 whose recurrence operators are not solvable (i.e.unsolvable).In general, the main obstacle in the setting of unsolvable recurrence operators is the absence of "well-behaved" closed-forms for the resulting recurrences.

Related Work
To the best of our knowledge, the study of invariant synthesis from the viewpoint of recurrence operators is mostly limited to the setting of solvable operators (or minor generalisations thereof).In [2,16] the authors introduce solvable loops and mappings to model loops with (blocks of) affine assignments and propose solutions for steps (i)-(iii) for this class of loops: all polynomial invariants are derived by first solving linear recurrence equations and then eliminating variables based on Gröbner basis computation.These results have further been generalised in [3,6] to handle more generic recurrences; in particular, deriving arbitrary exponential polynomials as closed-forms of loop variables and allowing restricted multiplication among recursively updated loop variables.The authors of [5,17] generalise the setting: they consider more complex programs and devise abstract (wedge) domains to map the invariant generation problem to the problem of solving C-finite recurrences.(We give further details of this class of recurrences in Section 2).All the aforementioned approaches are mainly restricted to C-finite recurrences for which closed-forms always exist, thus yielding loop invariants.In [9,18] the authors establish techniques to apply invariant synthesis techniques developed for deterministic loops to probabilistic programs.Instead of devising recurrences describing the precise value of variables in step (i), their approach produces C-finite recurrences describing (higher) moments of program variables, yielding moment-based invariants after step (iii).
Pushing the boundaries in analyzing unsolvable loops is addressed in [5,19].The approach of [5] extracts C-finite recurrences over linear combinations of loops variables from unsolvable loops.For example, the method presented in [5] can also synthesise the closed-forms identified by our work for Figure 1a.However, unlike [5], our work is not limited to linear combinations (we can extract C-finite recurrences over polynomial relations in the loop variables).As such, the technique of [5] cannot synthesise the polynomial loop invariant in Figure 1b, whereas our work can.A further related approach to our work is given in [19], yet in the setting of loop termination.However, our work is not restricted to solvable loops that are triangular, but can handle mutual dependencies among (unsolvable) loop variables, as evidenced in Figure 1.
Related work in the literature introduces techniques from the theory of martingales in order to synthesise invariants in the setting of probabilistic programs [20].Therein, the programming model is represented by a class of loop programs where all updates are linear and the synthesised invariants are given by linear templates.By contrast, our method allows us to handle polynomial arithmetic; in particular, we automatically generate invariants given by monomials in the program variables.On the other hand, the approach of [20] can also synthesise supermartingales whereas our work is restricted to invariants defined by equalities.

Our Contributions
In this paper we consider the sister problems of invariant generation and solvable loop synthesis in the setting of unsolvable recurrence operators.We introduce the notions of effective and defective program variables where, figuratively speaking, the defective variables are those "responsible" for unsolvability.Our main contributions are summarised below.
1. Crucial for our synthesis technique is our novel characterisation of unsolvable recurrence operators in terms of defective variables (Theorem 13).Our approach complements existing techniques in loop analysis, by extending these methods to the setting of 'unsolvable' loops.2. On the one hand, defective variables do not generally admit closed-forms.On the other hand, some polynomial combinations of such variables are well-behaved (see e.g., Figure 1).We show how to compute the set of defective variables in polynomial time (Algorithm 1). 3. We introduce a new technique to synthesise valid linear relations in defective monomials such that these relations admit closed-forms, from which polynomial loop invariants follow (Section 5). 4. Given an unsolvable loop, we introduce an algorithmic approach (Algorithm 2) that synthesises a solvable loop with the following property: every polynomial invariant of the solvable loop is also an invariant of the given unsolvable loop (Section 6). 5. We generalise our work to the analysis of probabilistic program loops ( Section 7) and showcase further applications of unsolvable operators in such programs (Section 8).6.We provide a fully automated approach in the tool Polar1 .For evaluating our work, we compiled an extensive lists of challenging loops from the literature, including applications of mathematical and physical modelling.Our experiments demonstrate the feasibility of invariant synthesis for 'unsolvable' loops and the applicability of our approach to deterministic loops, probabilistic models, and biological systems (Section 9).

Beyond Invariant Generation
We believe our work can provide new solutions towards compiler optimisation challenges.Scalar evolution2 is a technique to detect general induction variables.Scalar evolution and general induction variables are used for a multitude of compiler optimisations, for example inside the LLVM toolchain [21].On a high-level, general induction variables are loop variables that satisfy linear recurrences.As we show in our work, defective variables do not satisfy linear recurrences in general; hence, scalar evolution optimisations cannot be applied upon them.However, some linear combinations of defective monomials do satisfy linear recurrences, which opens avenues where we can apply scalar evolution techniques over such monomials.In particular, our work automatically computes polynomial combinations of some defective loop variables, which potentially enlarges the class of loops that, for example, LLVM can optimise.

Structure and Summary of Results
The rest of this paper is organised as follows.We briefly recall preliminary material in Section 2. Section 3 abstracts from concrete recurrence-based approaches to invariant synthesis via recurrence operators.Section 4 introduces effective and defective variables, presents Algorithm 1 that computes the set of defective program variables in polynomial time, and characterises unsolvable loops in terms of defective variables (Theorem 13).In Section 5 we present our new technique that synthesises linear relations in defective monomials that admit well-behaved closed-forms.In Section 6 we introduce Algorithm 2 to synthesise solvable loops.That is, given an unsolvable loop, Algorithm 2 outputs a solvable loop, if it exists such that each polynomial invariant of the solvable loop is also an invariant of the unsolvable loop.In Section 7 we detail the necessary changes to the algorithms in Sections 5 and 6 for probabilistic programs.We illustrate our approach with several case-studies in Section 8, and describe a fullyautomated tool support of our work in Section 9. We also report on accompanying experimental evaluation in Sections 8-9, and conclude the paper in Section 10.

Notation
Throughout this paper, we write N, Q, and R to respectively denote the sets of natural, rational, and real numbers.We write Q, the algebraic closure of Q, to denote the field of algebraic numbers.We write R[x 1 , . . ., x k ] and Q[x 1 , . . ., x k ] for the polynomial rings of all polynomials P (x 1 , . . ., x k ) in k variables x 1 , . . ., x k with coefficients in R and Q, respectively (with k ∈ N and k = 0).A monomial is a monic polynomial with a single term.For a program P, Vars(P) denotes the set of program variables.We adopt the following syntax in our examples.Sequential assignments in while loops are listed on separate lines (as demonstrated in Figure 1).In programs where simultaneous assignments are performed, we employ vector notation (as demonstrated by the assignments to the variables x and y in program P MC in Example 4).
We refer to a directed graph with G, whose edge and vertex (node) sets are respectively denoted via A(G) and V (G).We endow each element of A(G) with a label according to a labelling function L. A path in G is a finite sequence of contiguous edges of G, whereas a cycle in G is a path whose initial and terminal vertices coincide.A graph that contains no cycles is acyclic.In a graph G, if there exists a path from vertex u to vertex v, then we say that v is reachable from vertex u and say that u is a predecessor of v.

C-finite recurrences
We recall relevant results on (algebraic) recurrences and refer to [14,15] for further details.A sequence in Q is a function u : N → Q, shortly written also as u(n) ∞ n=0 or simply just u(n) n .A recurrence for a sequence u(n) n is an equation u(n+ℓ) = Rec(u(n+ℓ−1), . . ., u(n+1), u(n), n), for some function Rec : R ℓ+1 → R. The number ℓ ∈ N is the order of the recurrence.
A special class of recurrences we consider are the linear recurrences with constant coefficients, in short C-finite recurrences.A C-finite recurrence for a sequence u(n) n is an equation of the form where a 0 , . . ., a ℓ−1 ∈ Q are constants and a 0 = 0.A sequence u(n) n satisfying a C-finite recurrence (1) is a C-finite sequence and is uniquely determined by its initial values u 0 = u(0), . . ., u ℓ−1 = u(ℓ−1).The characteristic polynomial associated with the C-finite recurrence relation (1) is The terms of a C-finite sequence can be written in a closed-form as exponential polynomials, depending only on n and the initial values of the sequence.That is, if and λ 1 , . . ., λ r are the roots of the associated characteristic polynomial.Importantly, closed-forms of (systems of) C-finite sequences always exist and are computable [14,15].

Invariants
A loop invariant is a loop property that holds before and after each loop iteration [22].
In this paper, we are interested in polynomial invariants, the class of invariants given by Boolean combinations of polynomial equations among loop variables.There is a minor caveat to our characterisation of (polynomial) loop invariants.We assume that a (polynomial) invariant consists of a finite number of initial values together with a closed-form expression of a monomial in the loop variables.Thus the closed-form of a loop invariant must eventually hold after a (computable) finite number of loop iterations.Let us illustrate this caveat with the following loop: x ← 0 while ⋆ do x ← 1 end while Here the loop admits the polynomial invariant given by the initial value x(0) = 1 of x and the closed-form x(n) = 1.For each n ≥ 1, we denote by x(n) the value of loop variable x at loop iteration n.Herein, we synthesise invariants that satisfy inhomogeneous first-order recurrence relations and it is straightforward to show that each associated closed-form holds for n ≥ 1.

Polynomial Invariants and Invariant Ideals
A polynomial ideal is a subset I ⊆ Q[x 1 , . . ., x k ] with the following properties: I contains 0; I is closed under addition; and if P ∈ Q[x 1 , . . ., x k ] and Q ∈ I, then P Q ∈ I.For a set of polynomials S ⊆ Q[x 1 , . . ., x k ], one can define the ideal generated by S by I(S) := {s 1 q 1 + • • • + s ℓ q ℓ | s i ∈ S, q i ∈ Q[x 1 , . . ., x k ], ℓ ∈ N}.Let P be a program as before.For x j ∈ Vars(P), let x j (n) n denote the sequence whose nth term is given by the value of x j in the nth loop iteration.The set of polynomial invariants of P form an ideal, the invariant ideal of P [16].If for each program variable x j the sequence x j (n) n is C-finite, then a basis for the invariant ideal can be computed as follows.Let f j (n) be the exponential polynomial closed-form of variable x j .The exponential terms λ n 1 , . . ., λ n s in each of the f j (n) are replaced by fresh symbols, yielding the polynomials g j (n).Next, with techniques from [23], the set R of all polynomial relations among λ n 1 , . . ., λ n s (that hold for each n ∈ N) is computed.Then we express the polynomial relations in terms of the fresh constants, so that we can interpret R as a set of polynomials.Thus is precisely the invariant ideal of P. Finally, we can compute a finite basis for the invariant ideal with techniques from Gröbner bases and elimination theory [23].
3 From Loops to Recurrences

Recurrence Operators
Modelling properties of loop variables by algebraic recurrences and solving the resulting recurrences is an established approach in program analysis.Multiple works [3,5,6,17,24] associate a loop variable x with a sequence x(n) n whose nth term is given by the value of x in the nth loop iteration.These works are primarily concerned with the problem of representing such sequences via recurrence equations whose closed-forms can be computed automatically, as in the case of C-finite sequences.A closely connected question to this line of research focuses on identifying classes of loops that can be modelled by solvable recurrences, as advocated in [2].To this end, overapproximation methods for general loops are proposed in [5,17] such that solvable recurrences can be obtained from (over-approximated) loops.
In order to formalise the above and similar efforts in associating loop variables with recurrences, herein we introduce the concept of a recurrence operator, and the characterisation of both solvable and unsolvable operators.Intuitively, a recurrence operator maps program variables to recurrence equations describing some properties of the variables; for instance, the exact values at the nth loop iteration [2,3,17] or statistical moments in probabilistic loops [9].
constitutes a polynomial first-order system of recurrences.We call R linear if R[x] is linear for all x ∈ Vars(P).
One can extend the operator R to R[Vars(P)].Then, with a slight abuse of notation, for P (x 1 , . . ., x j ) ∈ R[Vars(P)] we define R(P ) by For a program P with recurrence operator R and a monomial over program variables M := x∈Vars(P) x αx , we denote by M (n) the product of sequences x∈Vars(P) x αx (n).Given a polynomial P over program variables, P (n) is defined by replacing every monomial M in P by M (n).For a set T of polynomials over program variables let Example 2. Consider the program P SC in Figure 1b.One can employ a recurrence operator R in order to capture the values of the program variables in the nth iteration.For v ∈ Vars(P SC ), R[v] is obtained by bottom-up substitution in the polynomial updates starting with v.As a result, we obtain the following system of recurrences: Similarly, for the program P of Figure 1a, we obtain the following system of recurrences:

Solvable Operators
Systems of linear recurrences with constant coefficients admit computable closed-form solutions as exponential polynomials [14,15].This property holds for a larger class of recurrences with polynomial updates, which leads to the notion of solvability introduced in [2].We adjust the notion of solvability to our setting by using recurrence operators.In the following definition, we make a slight abuse of notation and order the program variables so that we can transform program variables by a matrix operator.
Definition 3 (Solvable Operators [2,4]).The recurrence operator R is solvable if there exists a partition of Vars n ; that is, for some matrices M j and polynomials P j .A recurrence operator that is not solvable is said to be unsolvable.
This definition captures the notion of solvability in [2] (see the discussion in [4]).
We conclude this section by emphasising the use of (solvable) recurrence operators beyond deterministic loops, in particular relating its use to probabilistic program loops.As evidenced in [9], recurrence operators model statistical moments of program variables by essentially focusing on solvable recurrence operators extended with an expectation operator E( • ) to derive closed-forms of (higher) moments of program variables, as illustrated below.
Example 4. Consider the probabilistic program P MC of [25,26] modelling a non-linear Markov chain, where Bernoulli(p) refers to a Bernoulli distribution with parameter p.Here the updates to the program variables x and y occur simultaneously. while We can construct recurrence equations, in terms of the expectation operator E( • ), for this program as follows:

Defective Variables
To the best of our knowledge, existing approaches in loop analysis and invariant synthesis are restricted to solvable recurrence operators.In this section, we establish a new characterisation of unsolvable recurrence operators.Our characterisation pinpoints the program variables responsible for unsolvability, the defective variables (see Definition 7).Moreover, we provide a polynomial time algorithm to compute the set of defective variables (Algorithm 1), in order to exploit our new characterisation for synthesising invariants in the presence of unsolvable operators in Section 5.
For simplicity, we limit the discussion in this section to deterministic programs.We note however that the results presented herein can also be applied to probabilistic programs.The details of the necessary changes in this respect are given in Section 7. In what follows, we write M n (P) to denote the set of non-trivial monomials in Vars(P) evaluated at the nth loop iteration so that We next introduce the notions of variable dependency and dependency graph, needed to further characterise defective variables.
Definition 5 (Variable Dependency).Let P be a loop with recurrence operator R and x, y ∈ Vars(P).We say x depends on y if y appears in a monomial in R[x] with non-zero coefficient.Moreover, x depends linearly on y if all monomials with non-zero coefficients in R[x] containing y are linear.Analogously, x depends non-linearly on y if there is a non-linear monomial with non-zero coefficient in R[x] containing y. Furthermore, we consider the transitive closure for variable dependency.If z depends on y and y depends on x, then z depends on x and, if in addition, one of these two dependencies is non-linear, then z depends non-linearly on x.We otherwise say the dependency is linear.
For each program with polynomial updates, we further define a dependency graph with respect to a recurrence operator.Definition 6 (Dependency Graph).Let P be a program with recurrence operator R. The dependency graph of P with respect to R is the labelled directed graph G = (Vars(P), A, L) with vertex set Vars(P), edge set A := {(x, y) | x, y ∈ Vars(P) ∧ x depends on y}, and a function L : A → {L, N } that assigns a unique label to each edge such that L(x, y) = L if x depends linearly on y, and N if x depends non-linearly on y.
In our approach, we partition the variables Vars(P) of the program P into two sets: effective-and defective variables, denoted by E(P) and D(P) respectively.Our partition builds on the definition of the dependency graph of P, as follows.
Definition 7 (Effective-Defective).A variable x ∈ Vars(P) is effective if: 1. x appears in no directed cycle with at least one edge with an N label, and 2.
x cannot reach a vertex of an aforementioned cycle (as in 1).A variable is defective if it is not effective.
Example 8. From the recurrence equations of Example 2 for the program P SC (see Figure 1b), one obtains the dependencies between the program variables of P SC : the program variable w depends linearly on both x and y, whilst x and y depend nonlinearly on each other and on w.By definition, the partition into effective and defective variables is E(P SC ) = ∅ and D(P SC ) = {w, x, y}.
Similarly, we can construct the dependency graph for the program P from Figure 1a, as illustrated in Figure 2. We derive that E(P ) = {z} and D(P ) = {x, y}.We give the following straightforward corollary of Definition 7.
Corollary 9. Given any effective variable x ∈ E(P), the recurrence relation R[x] is a polynomial in effective variables.
The concept of effective, and, especially, defective variables allows us to establish a new characterisation of programs with unsolvable recurrence operators: a recurrence operator is unsolvable if and only if there exists a defective variable (as stated in Theorem 13 and automated in Algorithm 1).We formalise and prove this results via the following three lemmas.
Lemma 10.Let P be a program with recurrence operator R. If D(P) is non-empty, so that there is at least one defective variable, then R is unsolvable.
Proof.Let x ∈ Vars(P) be a defective variable and G = (Vars(P), A, L) the dependency graph of P with respect to a recurrence operator R. Following Definition 7, there exists a cycle C such that x is a vertex visited by or can reach said cycle and, in addition, there is an edge in C labelled by N .
Assume, for a contradiction, that R is solvable.Then there exists a partition W 1 , . . ., W k of Vars n (P) as described in Definition 3.Moreover, since C is a cycle, there exists j ∈ {1, . . ., k} such that each variable visited by C lies in W j .Let (w, y) ∈ C be an edge labelled with N .Since w depends on y non-linearly, and R[w] = M j • W ⊤ j + P j (W 1 , . . ., W j−1 ) (by Definition 3), it is clear that y(n) ∈ W ℓ for some ℓ = j.We also have that y(n) ∈ W j since C visits y.Thus we arrive at a contradiction as W 1 , . . ., W k is a partition of Vars n (P).Hence R is unsolvable.
Given a program P whose variables are all effective, it is immediate that a pair of distinct mutually dependent variables are necessarily linearly dependent and, similarly, a self-dependent variable is necessarily linearly dependent on itself.Consider the following binary relation ∼ on program variables: x ∼ y ⇐⇒ x = y ∨ (x depends on y ∧ y depends on x).
Thus, any two mutually dependent variables are related by ∼.Under the assumption that all variables of a program P are effective, it is easily seen that ∼ defines an equivalence relation on Vars(P).The partition of the equivalence classes Π of Vars(P) under ∼ admits the following notion of dependence between equivalence classes: for π, π ∈ Π we say that π depends on π if there exist variables x ∈ π and y ∈ π such that variable x depends on variable y.Lemma 11.Suppose that all variables of a program P are effective.Consider the graph G with vertex set given by the set of equivalence classes Π and edge set Proof.From the definition of G, it is clear that the graph is directed and has no selfloops.Now assume, for a contradiction, that G contains a cycle.Since the relation ∼ is transitive, there exists a cycle C in G of length two.Moreover, the variables in a given equivalence class are mutually dependent.Thus the elements of the two classes in C are equivalent under the relation ∼, which contradicts the partition into distinct equivalence classes.Therefore the graph G is acyclic, as required.
Lemma 12. Let P be a program with recurrence operator R. If each of the program variables of P is effective then R is solvable.
Proof.By Lemma 11, the associated graph G = (Π, A ′ ) on the equivalence classes of Vars(P) is directed and acyclic.Thus there exists a topological ordering of Π = {π 1 , . . ., π |Π| } such that for every (π i , π j ) ∈ A ′ we have i > j.Thus if x ∈ π i then x does not depend on any variables in class π j for j > i.Moreover, for each π i ∈ Π, if x, y ∈ π i then x cannot depend on y non-linearly because every variable is effective (and all the variables in π i are mutually dependent).Thus Π evaluated at loop iteration n partitions Vars n (P) and satisfies the criteria in Definition 3. We thus conclude that R is solvable.Let G = (Vars(P), A, L) be the dependency graph of P with respect to R. In Algorithm 1 we provide a polynomial time algorithm that constructs both E(P) and D(P) given a program and a recurrence operator.We use the initialism "DFS" for the depth-first search procedure.Algorithm 1 terminates in polynomial time as both the construction of the dependency graph and depth-first search exhibit polynomial time complexity.The procedure searches for cycles in the dependency graph with at least one non-linear edge (labelled by N ).All variables that reach such cycles are, by definition, defective.
In what follows, we focus on programs with unsolvable recurrence operators, or equivalently by Theorem 13, the case where D(P) = ∅.The characterisation of unsolvable operators in terms of defective variables and our polynomial algorithm to construct the set of defective variables is the foundation for our approach synthesising invariants in the presence of unsolvable recurrence operators in Section 5.
Remark 14.The recurrence operator R[x] for an effective variable x will admit a closed-form solution for every initial value x 0 .For the avoidance of doubt, the same cannot be said for the recurrence operator of a defective variable.However, it is possible that a set of initial values will lead to a closed-form expression as a C-finite sequence: consider a loop with defective variable x and update x ← x 2 and initialisation x 0 ← 0 or x 0 ← ±1.

Synthesising Invariants
In this section we propose a new technique to synthesise invariants for programs with unsolvable recurrence operators.The approach is based on our new characterisation of unsolvable operators in terms of defective monomials (Section 4).
For the remainder of this section we fix a program P with an unsolvable recurrence operator R, or equivalently with D(P) = ∅.We start by extending the notions of effective and defective from program variables to monomials of program variables.Let E be the set of effective monomials given by The complement, the defective monomials, is given by D(P) := M(P) \ E(P).The difficulty with defective variables is that in general they do not admit closed-forms.However, linear combinations of defective monomials may allow for closed-forms as illustrated in previous examples.The main idea of our technique for invariant synthesis in the presence of defective variables is to find such polynomials.We fix a candidate polynomial called S(n) based on an arbitrary degree d ∈ N: where the coefficients c W ∈ R are unknown real constants.We use D n (P) ↾ d to indicate the set of defective monomials of degree at most d.
On the one hand, all variables in S(n) are defective; however, S(n) may admit a closed-form.This occurs if S(n) obeys a "well-behaved" recurrence equation; that is to say, an inhomogeneous recurrence equation where the inhomogeneous component is given by a linear combination of effective monomials.In such instances the recurrence takes the form S(n+1) = κS(n) + where the coefficients c M are unknown.Thus an intermediate step towards our goal of synthesising invariants is to determine whether there are constants c M , c W , κ ∈ R that satisfy the above equations.If such constants exist then we come to our final step: solving a first-order inhomogeneous recurrence relation.There are standard methods available to solve first-order inhomogeneous recurrences of the form S(n+1) = κS(n)+ h(n), where h(n) is the closed-form of M∈En(P) c M M , see e.g., [15].We note h(n) is computable and an exponential polynomial since it is determined by a linear sum of effective monomials.Thus S(n) n is a C-finite sequence.c M M = 0, yielding a polynomial in Vars n (P).Thus all the coefficients in the above formula are necessarily zero as the polynomial is identically zero.Therefore all solutions to the unknowns c M , c W , and κ are computed by solving a (quadratic) system of equations.
Example 17.Consider the following illustration of our invariant synthesis procedure.Recall program P from Figure 1a: From Algorithm 1 we obtain E(P ) = {z} and D(P ) = {x, y}.Because D(P ) = ∅, we deduce using Theorem 13 that the associated operator R is unsolvable.Consider the candidate S(n) = ax(n) + by(n) with unknowns a, b ∈ R. The recurrence for S(n) given by R is We next express S(n+1) in terms of an inhomogeneous recurrence equation (cf.equation (3)).When we substitute for S(n), we obtain where the coefficients in the inhomogeneous component are unknown.We then combine the preceding two equations (for brevity we suppress the loop counter n in the program variables x, y, z) and derive Thus we have a polynomial in the program variables that is identically zero.Therefore, all the coefficients in the above equation are necessarily zero.We then solve the resulting system of quadratic equations, which leads to the non-trivial solution a = b, κ = 2, d = 3a, and c = −3a.We substitute this solution back into the recurrence for R[S] and find Here, we have used the closed-form solution z(n) = 1/2 − (−1) n /2 of the effective variable z.We can compute the solution of this inhomogeneous first-order recurrence equation.In the case that a = 1, we have Therefore, the following identity holds for each n ∈ N: and so we have synthesised the closed-form of x + y for program P of Figure 1a.

Solution Space of Invariants for Unsolvable Operators
Given a program and a recurrence operator, our invariant synthesis technique is relative-complete with respect to the degree d of the candidate S(n).This means, for a fixed degree d ∈ N, our approach is in theory able to compute all polynomials of defective variables with maximum degree d that satisfy a "well-behaved" recurrence; that is, a first-order recurrence equation of the form (3).This holds because of our reduction of the problem to a system of quadratic equations for which all solutions are computable.Our technique can also rule out the existence of well-behaved polynomials of defective variables of degree at most d if the resulting system has no (non-trivial) solutions.
Let P be a program with program variables Vars(P) = {x 1 , . . ., x k }.The set of polynomials P with P (x 1 (n), . . ., x k (n))=0 for all n ∈ N form an ideal, the invariant ideal of P. The requirement of closed-forms is the main obstacle for computing a basis for the invariant ideal in the presence of defective variables.Our work introduces a method that includes defective variables in the computation of invariant ideals, via the following steps of deriving the polynomial invariant ideal of an unsolvable loop: • For every effective variable x i , let f i (n) be its closed-form and assume h(n) is the closed-form for some candidate S given by a polynomial in defective variables.• Let λ n 1 , . . ., λ n s be the exponential terms in all f i (n) and h(n).Replace the exponential terms in all f i (n) as well as h(n) by fresh constants to construct the polynomials g i (n) and l(n) respectively.
• Next, construct the set R of polynomial relations among all exponential terms, as explained in Section 2.Then, the ideal contains precisely all polynomial relations among program variables implied by the equations } in the theory of polynomial arithmetic.• A finite basis for this ideal is computed using techniques from Gröbner bases and elimination theory.This step is similar to the case of the invariant ideal for solvable loops, see e.g., [2,3].
In conclusion, we infer a finite representation of the ideal of polynomial invariants for loops with unsolvable recurrence operators.

Synthesising Solvable Loops from Unsolvable Loops
In previous sections, we introduced a new technique to compute invariants for unsolvable loops; that is, loops containing defective variables.An orthogonal challenge is to synthesise a solvable loop from an unsolvable loop that preserves or over-approximates given specifications.
In this section we establish, with Algorithm 2, a new method to synthesise a solvable loop P ′ from an unsolvable loop P. The solvable loop P ′ over-approximates the behaviour of P in the sense that every polynomial invariant of P ′ is an invariant of P.Moreover, we show the invariants among effective variables of P and P ′ coincide.
The following example illustrates the main idea leading to Algorithm 2.
Example 18. Example 17 showed how to synthesise the polynomial S = x + y of defective variables x and y for the loop in Figure 1a such that S admits a closed-form.In this case, the polynomial of program variables S satisfies the linear inhomogeneous recurrence S(n + 1) = 2S(n) − 3z(n) + 3. We can use this recurrence to construct a solvable loop from the unsolvable from Figure 1a that captures the dynamics of the only effective variable z as well as S: Our algorithmic approach, as given in Algorithm 2, synthesises the solvable loop from the unsolvable loop on the left.Such a synthesis step is implemented within our tool (Section 9), allowing us to synthesize the above solvable loop in about 1 second.

Algorithm 2 Solvable Loop Synthesis
Input: Unsolvable loop P with recurrence operator R, degree d ∈ N Output: Solvable loop P ′ 1: Compute E(P) and D(P) using Algorithm 1.
3: Solve for coefficients in S(n+1) = κS(n) + M∈En(P) c M M (as in Section 5 (3)).The inputs for Algorithm 2 are an unsolvable loop P with recurrence operator R and a fixed degree d ∈ N; the algorithm outputs a solvable loop P ′ , if it exist.Briefly, the algorithm invokes the invariant synthesis procedure from Section 2 (for degree d) and constructs the loop P ′ from P by removing all the defective variables and then introducing a new variable s that models an invariant among defective monomials, if such an invariant exists.The recurrence operator R ′ associated with the synthesised loop P ′ is the canonical recurrence operator: R ′ maps every program variable to its assignment.
Proof.Using our characterisation of solvable and unsolvable loops in terms of effective and defective variables (Theorem 13), we show that all variables in P ′ are effective.The variables of P ′ consist of the effective variables of P as well as the fresh variable s: Vars(P ′ ) = E(P)∪{s}, or Vars(P ′ ) = E(P) if no invariant among defective monomials with degree d exists.
First, every x ∈ E(P) necessarily remains effective for the synthesised program P ′ : in the dependency graph of P ′ , the variable x cannot occur in a cycle containing s, because s is a fresh variable and hence cannot appear in the recurrence R[y] for any y ∈ E(P) (Algorithm 2 line 15).Hence, if z ∈ E(P) ∩ D(P ′ ), then there must exist a cycle in the dependency graph of P ′ with a non-linear edge (x, y) (i.e., L(x, y) = N ) such that every vertex in the cycle is in E(P).Consequently, this cycle is also present in the dependency graph of the original program P. This means that not all variables in E(P) are effective, which is a contradiction.
Second, the variable s is effective.Because s is a fresh variable, the only incoming edge of s in the dependency graph of P ′ is a linear self-loop (Algorithm 2 line 18).Hence s cannot occur in a cycle with a non-linear edge.Moreover, all outgoing edges point to effective variables.Thus s ∈ E(P ′ ) is effective.
With the next two lemmas, we prove that the loop P ′ synthesised by Algorithm 2 over-approximates the invariants of the unsolvable loop P. Let Inv(P) and Inv(P ′ ) denote the invariant ideals of P and P ′ respectively.The next lemma establishes that the synthesised loop P ′ is complete with respect to invariants among effective variables.
Lemma 20 (Completeness with respect to Effective Variables).The invariant ideals of P and P ′ coincide when restricted on the effective variables of P. That is, Proof.By the construction of P ′ , every x ∈ E(P) is also a variable of P ′ .To distinguish the variable x in P from the variable x in P ′ we refer to the latter by x ′ .We will show that for every x ∈ E(P) the sequences x(n) n and x ′ (n) n coincide.If this is the case, the polynomial invariants for the programs P and P ′ among the variables E(P) necessarily coincide as well.
Let R and R ′ be the recurrence operators associated to P and P ′ , respectively.For every x ∈ E(P) we have R[x] = R ′ [x ′ ] and x 0 = x ′ 0 by the construction of P ′ .Furthermore, by Corollary 9, defective variables cannot occur in R[x] for any x ∈ E(P).Moreover, the fresh variable s cannot occur in R ′ We note that when an invariant among defective monomials of degree d does not exist, the variables of the synthesised loop P ′ are precisely the effective variables of P. In this case, Lemma 20 fully characterises the relationship between the invariants of P and P ′ .
In the following, let us consider the complementary case, namely when an invariant among the defective monomials of P exists.Hence, the synthesised loop P ′ contains the additional fresh variable s modelling the behaviour of this invariant.With the next lemma, we confirm that the synthesised loop P ′ is indeed a sound over-approximation of the unsolvable loop P. We show that every invariant of P ′ is also an invariant of P. The program variable s ∈ Vars(P ′ ) introduced by Algorithm 2 is however not a program variable of P; nevertheless, s models the polynomial S of defective variables in P. Hence, to compare the invariant ideals of P and P ′ , we need to "substitute" s by the polynomial of defective variables it models.This can be done by adding the equation s = S (s − S = 0) to the invariant ideal of P ′ and restricting the resulting ideal to Vars(P): Lemma 21 (Over-Approximation).Let J be the ideal in (4) constructed from the invariant ideal of P ′ by replacing the program variable s by the polynomial of defective variables it models.Then, J ⊆ Inv(P).
Proof.As argued in the proof of Lemma 20, for every x ∈ E(P), the sequences corresponding to the variable x in both P and P ′ coincide; that is, x(n (where the notation indicates that S is substituted for s).Now, Q is a polynomial relation among the sequences induced by the variables in Vars(P ′ ).Because x(n) n ≡ x ′ (n) n for every x ∈ E(P) and s(n) n ≡ S(n) n , the polynomial Q{s → S} represents a relation among the sequences induced by the variables in Vars(P).Hence we have Q ∈ Inv(P).
Remark 22.Our loop synthesis procedure given in Algorithm 2 computes a single invariant among defective monomials (if such an invariant exists) of an unsolvable loop P. The results in this section naturally generalise to multiple invariants among defective monomials, as follows: for every invariant I, add a fresh variable s I modelling the behaviour of I to the synthesised program P ′ .Note that with each additional invariant I added, the dynamics of the synthesised loop P ′ more closely resembles that of the unsolvable loop P.
7 Adjustments for Unsolvable Operators in Probabilistic Programs

Defective Variables in Probabilistic Loop Models
The works [9,27] defined recurrence operators for probabilistic loops.Specifically, a recurrence operator is defined for loops with polynomial assignments, probabilistic choice, and drawing from common probability distributions with constant parameters.
Recurrences for deterministic loops model the precise values of program variables.
For probabilistic loops, this approach is not viable, due to the stochastic nature of the program variables.Thus a recurrence operator for a probabilistic loop models (higher) moments of program variables.As illustrated in Example 4, the recurrences of a probabilistic loop are taken over expected values of program variable monomials.
The authors of [9,27] explicitly excluded the case of circular non-linear dependencies to guarantee computability.However, in contrast to our notions in Sections 3, they defined variable dependence not on the level of recurrences but on the level of assignments in the loop body.To use the notions of effective and defective variables for probabilistic loops, we follow the same approach and base the dependency graph on assignments rather then recurrences.We illustrate the necessity of this adaptation in the following example.
Example 23.Consider the following probabilistic loop and associated set of first-order recurrence relations in terms of the expectation E( • ): It is straightforward to see that variable y is defective from the deterministic update y ← 4y(1 − y) with its characteristic non-linear self-dependence.Moreover, y appears in the probabilistic assignment of x.However, due to the particular form of the assignment, the recurrence of E(x n ) does not contain y.Nevertheless, y appears in the recurrence of E(x 2 n ).This phenomenon is specific to the probabilistic setting.For deterministic loops, it is always the case that if the values of a program variable w do not depend on defective variables, then neither do the values of any power of w.
In light of the phenomenon exhibited in Example 23, we adapt our notion of variable dependency, for probabilistic loops.Without loss of generality, we assume that every program variable has exactly one assignment in the loop body.Let P be a probabilistic loop and x, y ∈ Vars(P).We say x depends on y, if y appears in the assignment of x.Additionally, the dependency is linear if all occurrences of y in the assignment of x are linear, else the dependency is non-linear.Further, we consider the transitive closure of variable dependency analogous to deterministic loops and Definition 5.
With variable dependency thus defined, the dependency graph and the notions of effective and defective variables follow immediately.Analogous to our characterisation of unsolvable recurrence operators in terms of defective variables for deterministic loops, all (higher) moments of effective variables of probabilistic loops can be described by a system of linear recurrences [9,27].For defective variables this property will generally fail For instance, in Example 23, the variable x is now classified as defective and E(x 2 n ) cannot be modelled by linear recurrences for some initial values.The only necessary change to the invariant synthesis algorithm from Section 5 is as follows: instead of program variable monomials, we consider expected values of program variable monomials.Now, our invariant synthesis technique from Section 5 can also be applied to probabilistic loops to synthesise combinations of expected values of defective monomials that do satisfy a linear recurrence.

Synthesising Solvable Probabilistic Loops
In Algorithm 2 we introduced a procedure, utilising our new invariant synthesis technique from Section 5, to over-approximate an unsolvable loop by a solvable loop.The inputs to Algorithm 2 are an unsolvable loop with a recurrence operator and a natural number specifying a fixed degree.As mentioned, our invariant synthesis procedure is also applicable to probabilistic loops using the recurrence operator modelling moments of program variables introduced in [9,27].Hence, Algorithm 2 can also be used to synthesise solvable loops from unsolvable probabilistic loops.In the probabilistic case, however, the invariants computed by our approach are over moments of program variables.Therefore, the invariant ideal of probabilistic loops describes polynomial relations among a given set of moments of program variables, such as the expected values.Consequently, the loop synthesised by Algorithm 2 for a given probabilistic loop will be deterministic and model the dynamics of moments of program variables of the probabilistic loop.
Example 24.Recall the program P MC of Example 4.An invariant synthesised by our approach in Section 5 with degree 6 n (x 0 − y 0 ).Hence the solvable loop synthesised by Algorithm 2 for P MC with input degree 1 is where f is the fresh variable introduced by Algorithm 2 modelling E(x n − y n ).Our approach from Algorithm 2 synthesises this solvable loop from P MC , using less than 0.5 second within our implementation (Section 9).

Applications of Unsolvable Operators towards Invariant Generation
Our approach automatically generates invariants for programs with defective variables (Section 5), and pushes the boundaries of both theory and practice of invariant generation: we introduce and incorporate defective variable analysis into the state-of-the-art methodology for reasoning about solvable loops, complementing thus existing methods, see e.g., [2,3,5,6], in the area.As such, the class of unsolvable loops that can be handled by our work extends (aforementioned) existing approaches on polynomial invariant generation.The experimental results of our approach (see Section 9) demonstrate the efficiency and scalability of our work in deriving invariants for unsolvable loops.Since our approach to loops via recurrences is generic, we can deal with emerging applications of programming paradigms such as: transitions systems and statistical moments in probabilistic programs; and reasoning about biological systems.We showcase these applications in this section and also exemplify the limitations of our work.
In the sequel, we write E(t) to refer to the expected value of an expression t, and denote by E(t n ) (or E(t(n))) the expected value of t at loop iteration n.
Example 25 (Moments of Probabilistic Programs [25]).As mentioned in Example 24, E(x n −y n ) = 5 n 6 n (x 0 −y 0 ) is an invariant for the program P MC introduced in Example 4. Closed-form solutions for higher order expressions are also available; for example,

E((x
refers to the dth moment of x(n) − y(n).While the work in [25] uses martingale theory to synthesise the above invariant (of degree 1), our approach automatically generates such invariants over higher-order moments (see Table 2).We note to this end that the defective variables in P MC are precisely x and y as can be seen from their mutual non-linear interdependence.Namely, we have D(P MC ) = {x, y} and E(P MC ) = {s}.
Example 26 (non-lin-markov-2).We give a second example of a non-linear Markov chain.We analyse the moments of this probabilistic program in the next section.
x, y ← 0, 1 while ⋆ do  [28]).A model for the decision-making process of swarming bees choosing one nest-site from a selection of two is introduced in [28] and further studied in [29,30].Previous works have computed probability distributions for this model [30].The (unsolvable) loop is a discrete-time model with five classes of bees (each represented by a program variable).The coefficient ∆ is the length of the timestep in the model and the remaining coefficients parameterise the rates of change.All coefficients here are symbolic.
Normal(475, 5) Uniform(350, 400) Uniform(100, 150) Normal( We note that the model in [30] uses truncated Normal distributions, as [30] is limited to finite supports for the program variables, which is not the case with our work. In the loop above, each of the variables exhibits non-linear self-dependence, and so the variables are partitioned into D(P) = {x, y 1 , y 2 , z 1 , x 2 } and E(P) = ∅.While the recurrence operator of the loop above is unsolvable, our approach infers polynomial loop invariants using defective variable reasoning (Section 5).Namely, we generate the following closed-form solutions over expected values of program variables: One can interpret such invariants in terms of the biological assumptions in the model.Take, for example, the fact that E(x(n This invariant is in line with the assumption in the model that the total population of the swarm is constant.In fact, our invariants reflect the behaviour of the system in the original continuous-time model proposed in [28], because our approach is able to process all coefficients (most importantly ∆) as symbolic constants.
Example 28 (Probabilistic Transition Systems [25]).Consider the following probabilistic loop modelling a probabilistic transition system from [25]: while ⋆ do a b ← Normal(0, 1) Normal(0, 1) x y ← x + axy y + bxy end while While [25] uses martingale theory to synthesise a degree one invariant of the form aE(x k ) + bE(y k ) = aE(x 0 ) + bE(y 0 ), our work automatically generates invariants over higher-order moments involving the defective variables x and y, as presented in Table 2.
The next example demonstrates an unsolvable loop whose recurrence operator cannot (yet) be handled by our work.
Example 29 (Trigonometric Updates).As our approach is limited to polynomial updates of the program variables, the loop below cannot be handled by our work: while ⋆ do x y ← cos(x) sin(x) end while Note the trigonometric functions are transcendental, from which it follows that one cannot generally obtain closed-form solutions for the program variables.Nevertheless, this program does admit polynomial invariants in the program variables; for example, x 2 + y 2 = 1.Although our definition of a defective variables does not apply here, we could say the variable x here is somehow defective: while the exact value of sin(x) cannot be computed, it could be approximated using power series.Extending our work with more general notions of defective variables is an interesting line for future work.
Examples 30-33 (below) are custom-made benchmarks.We have tailored these benchmarks to demonstrate the flexibility and applicability of our method to the current state of the art.Our experimental analysis is delayed to Section 9.

Example 30 (squares+).
s, x, y, z ← 0, 2, 1, 0 while ⋆ do x, y ← 1, 1 while ⋆ do z ← Normal(0, 1) The following set of examples are taken from the literature on the theory of trace maps.Arguably the most famous example is the classical Fibonacci Trace Map f : R 3 → R 3 given by f (x, y, z) = (y, z, 2yz − x) (Example 34 below); said map has garnered the attention of researchers in fields as diverse as invariant analysis, representation theory, geometry, and mathematical physics (cf. the survey papers [31,32]).From a computational viewpoint, trace maps arise from substitution rules on matrices (see, again, the aforementioned survey papers).Given two matrices A, B ∈ SL(2, R) Example 37 (markov-triples-toggle).A while loop that generates an infinite sequence of nodes on the Markov tree (the walk alternates between 'upper' and 'lower' branches).
Example 38 (markov-triples-random).A while loop that simulates a Bernoulli walk on the Markov tree.
x,y,z = 1,1,2 while ⋆ do end if end while For this benchmark, our technique generates both closed-forms and invariants in (higher) moments of program variables such as E(x The next two benchmarks concern a special class of polynomial automorphisms, the Yagzhev maps, (sometimes cubic-homogeneous maps) introduced (independently) by Yagzhev [34] and Bass, Connell, and Wright [35] in the context of the Jacobian conjecture.
Recall that Yagzhev maps f : C n → C n take the form f (x) = x − g(x) such that det f ′ (x) = 1 for all x, and g : C n → C n is a homogeneous polynomial mapping of degree 3.
De Bondt exhibited a Yagzhev mapping in 10 dimensions that has no linear invariants (providing a counterexample to the "linear dependence conjecture" for the class of maps) [36].Zampieri demonstrated that De Bondt's example has quadratic and cubic invariants [37] (see Example 39 for such a Yagzhev map in 9 dimensions).Work by Santos Freire, Gorni, and Zampieri [38] exhibited a Yagzhev mapping in 11 dimensions that has neither linear invariants nor quadratic invariants (see Example 40).
Example 39 (yagzhev9 [37]). while For this example, our work generates an invariant quadratic homogeneous polynomial in six variables and three symbolic constants, which we can interpret in terms of determinants (as given below): x 5 (0) x 6 (0) .
Our computation confirms previous work by the authors of [37].Those authors demonstrated that this example has no linear invariants, but does admit the above quadratic invariant.
Example 40 (yagzhev11 [38]).x 10 + (x end while For this example, our approach generates an invariant cubic homogeneous polynomial, which we can interpret as the determinant of a matrix in the program variables: . For the avoidance of doubt, the above polynomial was previously found by the authors of [38].Indeed, our implementation confirms previous results: there are neither linear nor quadratic invariants for this example. Example 41 (nagata [39]).Let us now consider the classical Nagata automorphism introduced by Nagata [39, pg.41] (see also [40]). while end while For the Nagata Automorphism, it is easy to see that the variable z is effective and so contributes a linear invariant.When used to search for quadratic closed-forms, our method generates the polynomial where a, b, and c are symbolic constants.Here we note that x(n)z(n) and y(n) 2 are defective monomials.Our computation confirms the invariants and closed-forms established in [39].

Experiments
In this section, we report on our implementation towards fully automating the analysis of unsolvable loops and describe our experimental setting and results.

Implementation
Algorithm 1, our method for synthesising invariants involving defective variables, and Algorithm 2, for the synthesis of solvable loops from unsolvable loops are implemented in the Polar tool3 .We use python3 and the sympy package [41] for symbolic manipulations of algebraic expressions.

Benchmark Selection
While previous works [2,4,5,9,16,42] consider invariant synthesis, their techniques are only applicable in a restricted setting: the analysed loops are, for the most part, solvable; or, for unsolvable loops, the search for polynomial invariants is templatedriven or employs heuristics.In contrast, the work herein complements and extends the techniques presented for solvable loops in [2,4,5,9,16,42].Indeed, our automated approach turns the problem of polynomial invariant synthesis into a decidable problem for a larger class of unsolvable loops.
While solvable loops can clearly be analysed by our work, the main benefit of our work comes with handling unsolvable loops by translating them into solvable ones.For this reason, in our experimentation we are not interested in examples of solvable loops and so only focus on unsolvable loop benchmarks.There is therefore no sensible baseline that we can compare against, as state-of-the-art techniques cannot routinely synthesise invariants for unsolvable loops in the generality we present.
In our work we present a set of 23 examples of unsolvable loops, as listed in Table 1 4 .Common to all 23 benchmarks from Table 1 is the exhibition of circular non-linear dependencies within the variable assignments.We display features of our benchmarks in Table 1 (for example, column 3 of Table 1 counts the number of defective variables for each benchmark).
Three examples from Table 1 are challenging benchmarks taken from the invariant generation literature [25,26,29,30]

Experimental Setup
We evaluate our approach in Polar on the examples from Table 1.All our experiments were performed on a machine with a 1.80GHz Intel i7 processor and 16 GB of RAM.

Evaluation Setting
The landscape of benchmarks in the invariant synthesis literature for solvable loops can appear complex with: high numbers of variables, high degrees in polynomial updates, and multiple update options.However, we do not intend to compete on these metrics for solvable loops.The power of our invariant synthesis technique lies in its ability to handle 'unsolvable' loop programs: those with cyclic inter-dependencies and non-linear self-dependencies in the loop body.Regarding Algorithm 2, specifying our method for synthesising solvable from unsolvable loops, the main complexity lies in constructing an invariant involving defective variables.Once, such an invariant has been found, the remaining complexity of Algorithm 2 is linear in the number of program variables.Hence, the experimental results for our invariant synthesis technique also show the feasibility of our new method synthesising solvable from unsolvable loops.While the benchmarks of Table 1 may be considered simple, the fact that previous works cannot systematically handle such simple models crystallises that even simple loops can be unsolvable, limiting the applicability of state-of-the-art methods, as illustrated in the example below.

Experimental Results
Our experiments using Polar to synthesise invariants are summarised in Table 2, using the examples of Table 1.Patterns in Table 2 show that, if time considerations are the limiting factor, then the greatest impact cannot be attributed to the number

Experimental Summary
Our experiments illustrate the feasibility of synthesising invariants and solvable loops using our approach for programs with unsolvable recurrence operators from various domains such as biological systems, probabilistic loops, and classical programs (see Section 5).This further motivates the theoretical characterisation of unsolvable operators in terms of defective variables (Section 4).

Conclusion
We establish a new technique that synthesises invariants for loops with unsolvable recurrence operators and show its applicability for deterministic and probabilistic programs.Our work is further extended to translate unsolvable loops into solvable ones, by ensuring that the polynomial loop invariants of the solvable loop are invariants of the given unsolvable loop.Our work is based on a new characterisation of unsolvable loops in terms of effective and defective variables: the presence of defective variables is equivalent to unsolvability.In order to generate invariants, we provide an algorithm to isolate the defective program variables and a new method to compute polynomial combinations of defective variables admitting exponential polynomial closed-forms.The implementation of our approach in the tool Polar and our experimental evaluation demonstrate the usefulness of our alternative characterisation of unsolvable loops and the applicability of our invariant synthesis technique to systems from various domains.

Definition 1 (
Recurrence Operator).A recurrence operator R maps the program variables Vars(P) to the polynomial ring R[Vars n (P)].The set of equations

Fig. 2 :
Fig. 2: The dependency graphs for P SC and P from Figure 1.

Algorithm 1
Construct E(P) and D(P) from program P with operator R.

Remark 16 .
Observe that the sum on the right-hand side of equation (3) is finite, since all but finitely many of the coefficients c M are zero.Further, the coefficient c M of monomial M is non-zero only if M appears in R[S].Going further, in equation (3) we express S(n+1) in terms of a polynomial in Vars n (P) with unknown coefficients c M , c W , and κ.An alternative expression for S(n+1) in Vars n (P) is given by the recurrence operator S(n+1) = R[S].Taken in combination, we arrive at the following formula R[S] − κS(n) − M∈En(P) [x ′ ] by the construction of P ′ .Hence the two systems of first-order recurrences {x(n+1) = R[x] | x ∈ E(P)} and {x ′ (n+1) = R ′ [x ′ ] | x ∈ E(P)} together with the initial values {x 0 | E(P)} and {x ′ 0 | E(P)} induce the same sequences x(n) n and x ′ (n) n for all x ∈ E(P).

Examples 37 -
the group of 2 × 2 matrices with unit determinant), consider the following substitution rule on strings of matrices: A → B and B → AB.The classical Fibonacci Trace Map is determined by the action of this substitution on the traces of the matrices; i.e., f tr(A), tr(B), tr(AB) = tr(B), tr(AB), − tr(A) + 2 tr(B) tr(AB) .Further examples of trace maps (Example 35 and Example 36 below) are constructed from similar substitution rules on strings of matrices.For Examples 34-36, our work generates the cubic polynomial invariantx 2 + y 2 + z 2 − 2xyz = x 2 0 + y 2 0 + z 2 0 − 2x 0 y 0 z 0 ,from the well-studied class of Fricke-Vogt invariants as well as higher-degree polynomial invariants.Example 35 (fib2).A Generalised Fibonacci Trace Map while ⋆ do   38 are while loops that generate Markov triples [33, Chapter II.3]; that is, at every iteration each loop variable takes an integer value that appears in a Diophantine solution of the Markov equation x 2 + y 2 + z 2 = 3xyz.
Together, Lemmas 10-12 yield a new characterisation of unsolvable operators.Theorem 13 (Defective Characterisation).Let P be a program with recurrence operator R, then R is unsolvable if and only if D(P) is non-empty.
we have Vars(P ′ ) = E(P) ∪ {s}.The fresh variable s in P ′ models the polynomial S ∈ Q[Vars(P)].Let s(n) n be the sequence induced by the program variable s in P ′ and, likewise, S(n) n the sequence induced by the polynomial S ∈ Q[Vars(P)].Then, by the construction of P ′ , we have s(n ; full automation in analysing these examples was not yet possible.These examples are listed as non-lin-markov-1, pts, and bees in Table 1, respectively corresponding to Example 4 (and hence Example 25), Example 28, and Example 27 from Section 8. Eight further benchmarks, as described in Examples 34-41, are drawn from the theoretical physics and pure mathematics literature (references are given in Section 8).The remaining 12 examples of Table 1 are self-constructed benchmarks to highlight the key ingredients of our work in synthesising invariants associated with unsolvable recurrence operators.

Table 1 :
Example 42.Consider the question: does the unsolvable loop program deg-9 in Table 1 (i.e.Example 33) possess a cubic invariant?The program variables for deg-9 are x, y, and z.The variables x and y are defective.Using Polar, we derive that the Features of the benchmarks.Var = Total number of loop variables; Def = Number of defective variables; Term = Total number of terms in assignments; Deg = Maximum degree in assignments; Cand-7 = Number of monomials in candidate with degree 7; Eqn-7 = Size of the system of equations associated with a candidate of degree 7; -= Timeout (60 seconds).cubic, non-trivial polynomial p(x n , y n , z n ) given by 12(ay n + by 2 n + cy 3 n + dx n + ex n y n + f x n y 2 n ) − (3a + 24b + 117c + 2d + 17e + 26f )x 2