10.07.2015 Views

A New Look at the Automatic Synthesis of Linear Ranking Functions$

A New Look at the Automatic Synthesis of Linear Ranking Functions$

A New Look at the Automatic Synthesis of Linear Ranking Functions$

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>the</strong>refore, ranking functions on <strong>the</strong> n<strong>at</strong>urals are sufficient, for instance, whenmodeling <strong>the</strong> input <strong>of</strong> values for commonly available built-in d<strong>at</strong>a types. However,<strong>the</strong> use <strong>of</strong> more general well-founded orderings can simplify <strong>the</strong> search fora ranking function (see, e.g., [3, Example 1.2]). Moreover, such a generaliz<strong>at</strong>ionis mand<strong>at</strong>ory when dealing with unbounded nondeterminism [2] (see also [4,Section 10]).The termin<strong>at</strong>ion <strong>of</strong> a set <strong>of</strong> comput<strong>at</strong>ions and <strong>the</strong> existence <strong>of</strong> a rankingfunction for such a set are thus completely equivalent. On <strong>the</strong> one hand, thismeans th<strong>at</strong> trying to prove th<strong>at</strong> a ranking function exists is, <strong>at</strong> least in principle,not less powerful than any o<strong>the</strong>r method we may use to prove termin<strong>at</strong>ion.On <strong>the</strong> o<strong>the</strong>r hand, undecidability <strong>of</strong> <strong>the</strong> termin<strong>at</strong>ion problem implies th<strong>at</strong> <strong>the</strong>existence <strong>of</strong> a ranking function is also undecidable. An obvious way to prove<strong>the</strong> existence <strong>of</strong> a ranking function is to syn<strong>the</strong>size one from <strong>the</strong> program textand a description <strong>of</strong> <strong>the</strong> initial st<strong>at</strong>es: because <strong>of</strong> undecidability, <strong>the</strong>re exists noalgorithm th<strong>at</strong> can do th<strong>at</strong> in general.The use <strong>of</strong> ranking functions as a tool to reason about termin<strong>at</strong>ion can betraced back to <strong>the</strong> seminal work <strong>of</strong> R. W. Floyd in [5], where <strong>the</strong>y are introducedunder <strong>the</strong> name <strong>of</strong> W -functions. Since <strong>the</strong>n, several vari<strong>at</strong>ions <strong>of</strong> <strong>the</strong> methodhave been proposed so as to extend its applicability from <strong>the</strong> realm <strong>of</strong> classicalsequential programs to more general constructs (e.g., concurrency). In particular,in [4], seven different ‘à la Floyd’ induction principles for nondeterministictransition systems are formally shown to be sound, semantically complete andequivalent. For instance, it is shown th<strong>at</strong> it is sufficient to consider a single,global ranking function, instead <strong>of</strong> a different ranking function for each programcontrol point, as originally proposed in [5]; and th<strong>at</strong> <strong>the</strong> decrease <strong>of</strong> sucha global ranking function need not be verified <strong>at</strong> all program control points, butit is enough to consider a minimal set <strong>of</strong> loop cut-points; moreover, when tryingto prove properties th<strong>at</strong> only depend on <strong>the</strong> current st<strong>at</strong>e <strong>of</strong> <strong>the</strong> system (e.g.,termin<strong>at</strong>ion <strong>of</strong> a deterministic program), it is always possible to find a rankingfunction depending on <strong>the</strong> current st<strong>at</strong>e only, i.e., independent <strong>of</strong> <strong>the</strong> initialst<strong>at</strong>e <strong>of</strong> <strong>the</strong> system. Note th<strong>at</strong> <strong>the</strong>se results have been implicitly exploited in<strong>the</strong> examples above so as to simplify <strong>the</strong> present<strong>at</strong>ion <strong>of</strong> <strong>the</strong> method.In this paper we present, in very general terms so as to encompass any programmingparadigm, <strong>the</strong> approach to termin<strong>at</strong>ion analysis based on <strong>the</strong> explicitsearch <strong>of</strong> ranking functions. We <strong>the</strong>n restrict <strong>at</strong>tention to linear ranking functionsobtained from linear approxim<strong>at</strong>ions <strong>of</strong> <strong>the</strong> program’s semantics. For thisrestriction, we present and fully justify two methods to prove <strong>the</strong> existence <strong>of</strong> linearranking functions: one, based on work d<strong>at</strong>ed 1991 by Sohn and Van Gelder,th<strong>at</strong> is almost unknown outside <strong>the</strong> field <strong>of</strong> logic programming even though, aswe demonstr<strong>at</strong>e in <strong>the</strong> present paper, it is completely general; <strong>the</strong> o<strong>the</strong>r, dueto Podelski and Rybalchenko, d<strong>at</strong>ed 2004, was proved correct by <strong>the</strong> authorsbut <strong>the</strong> reasons why it works were never presented. We <strong>the</strong>n provide a pro<strong>of</strong> <strong>of</strong>equivalence <strong>of</strong> <strong>the</strong> two methods, thus providing an independent assessment <strong>of</strong><strong>the</strong>ir correctness and rel<strong>at</strong>ive completeness. We also compare <strong>the</strong>ir <strong>the</strong>oreticalcomplexity and practical efficiency on three rel<strong>at</strong>ed problems:4


1. proving th<strong>at</strong> one linear ranking function exists;2. exhibiting one such function;3. computing <strong>the</strong> space <strong>of</strong> all linear ranking functions.The experimental evalu<strong>at</strong>ion is based on <strong>the</strong> implement<strong>at</strong>ion <strong>of</strong> <strong>the</strong> two methodsprovided by <strong>the</strong> Parma Polyhedra Library [6], a free s<strong>of</strong>tware library <strong>of</strong>numerical abstraction targeted <strong>at</strong> s<strong>of</strong>tware/hardware analysis and verific<strong>at</strong>ion.These implement<strong>at</strong>ions are, to <strong>the</strong> best <strong>of</strong> our knowledge, <strong>the</strong> first ones th<strong>at</strong> arebeing made available, in source form, to <strong>the</strong> community. For this reason, <strong>the</strong>implement<strong>at</strong>ions should be regarded as complementary to <strong>the</strong> present paper in<strong>the</strong> common aim <strong>of</strong> making <strong>the</strong> autom<strong>at</strong>ic syn<strong>the</strong>sis <strong>of</strong> linear ranking functionsknown outside programming language barriers, understandable and accessible.The plan <strong>of</strong> <strong>the</strong> paper is as follows: Section 2 recalls preliminary notionsand introduces <strong>the</strong> not<strong>at</strong>ion used throughout <strong>the</strong> paper; Section 3 introduces<strong>the</strong> problem <strong>of</strong> autom<strong>at</strong>ic termin<strong>at</strong>ion analysis <strong>of</strong> individual loops and its solutiontechnique based on <strong>the</strong> syn<strong>the</strong>sis <strong>of</strong> ranking functions; Section 4 presentsa simple generaliz<strong>at</strong>ion <strong>of</strong> <strong>the</strong> approach <strong>of</strong> [7] th<strong>at</strong> is generally applicable totermin<strong>at</strong>ion analysis <strong>of</strong> any language; Section 5 shows and fully justifies <strong>the</strong>approach <strong>of</strong> [8]; Section 6 proves <strong>the</strong> two methods are equivalent and compares<strong>the</strong>m from <strong>the</strong> point <strong>of</strong> view <strong>of</strong> comput<strong>at</strong>ional complexity; Section 7 presents <strong>the</strong>implement<strong>at</strong>ion <strong>of</strong> <strong>the</strong> two approaches <strong>of</strong>fered by <strong>the</strong> Parma Polyhedra Libraryand <strong>the</strong> corresponding experimental evalu<strong>at</strong>ion, providing a comparison <strong>of</strong> <strong>the</strong>irpractical efficiency; Section 8 concludes.Readers who are interested in a more detailed exposition <strong>of</strong> <strong>the</strong> <strong>the</strong>mestre<strong>at</strong>ed in this paper are referred to its technical report version [3].2. PreliminariesSet Theory. The set <strong>of</strong> all finite sequences <strong>of</strong> elements <strong>of</strong> S is denoted by S ∗ .The empty sequence is denoted by ε and <strong>the</strong> length <strong>of</strong> a sequence w is denotedby |w|. The set <strong>of</strong> non-neg<strong>at</strong>ive integers, r<strong>at</strong>ionals and reals are denoted by N,Q + and R + , respectively.<strong>Linear</strong> Algebra. For each i ∈ {1, . . . , n}, v i denotes <strong>the</strong> i-th component <strong>of</strong> <strong>the</strong>real (column) vector v = 〈v 1 , . . . , v n 〉 ∈ R n . A vector v ∈ R n can also beinterpreted as a m<strong>at</strong>rix in R n×1 and manipul<strong>at</strong>ed accordingly with <strong>the</strong> usualdefinitions for addition, multiplic<strong>at</strong>ion (both by a scalar and by ano<strong>the</strong>r m<strong>at</strong>rix),and transposition, which is denoted by v T , so th<strong>at</strong> 〈v 1 , . . . , v n 〉 = (v 1 , . . . , v n ) T .If v ∈ R n and w ∈ R m , we will write 〈v, w〉 to denote <strong>the</strong> column vector in R n+mobtained by “conc<strong>at</strong>en<strong>at</strong>ing” v and w, so th<strong>at</strong> 〈v, w〉 = 〈v 1 , . . . , v n , w 1 , . . . , w m 〉.The scalar product <strong>of</strong> v, w ∈ R n is <strong>the</strong> real number v T w = ∑ ni=1 v iw i . Theidentity m<strong>at</strong>rix in R n×n is denoted by I n . We write 0 to denote a m<strong>at</strong>rixin R n×m having all <strong>of</strong> its components equal to zero; <strong>the</strong> dimensions n and mwill be clear from context. We sometimes tre<strong>at</strong> scalars as vectors in R 1 orm<strong>at</strong>rices in R 1×1 . For any rel<strong>at</strong>ional oper<strong>at</strong>or ⊲⊳ ∈ {}, we writev ⊲⊳ w to denote <strong>the</strong> conjunctive proposition ∧ ni=1 (v i ⊲⊳ w i ). Moreover, v ≠ w5


will denote <strong>the</strong> proposition ¬(v = w). We will sometimes use <strong>the</strong> convenientnot<strong>at</strong>ion a ⊲⊳ 1 b ⊲⊳ 2 c to denote <strong>the</strong> conjunction a ⊲⊳ 1 b ∧ b ⊲⊳ 2 c and wewill not distinguish conjunctions <strong>of</strong> propositions from sets <strong>of</strong> propositions. Thesame not<strong>at</strong>ion applies to vectors defined over o<strong>the</strong>r numeric fields and, for <strong>the</strong>supported oper<strong>at</strong>ions, to vectors defined over numeric sets such as N and Q + .First-Order Logic. Let L be a first-order language with variables in X. We willroutinely confuse a tuple <strong>of</strong> variables with <strong>the</strong> set <strong>of</strong> its components. So, if φ isan L-formula, we will write φ[¯x] to denote φ itself, yet emphasizing th<strong>at</strong> <strong>the</strong> set<strong>of</strong> free variables in φ is included in ¯x. Let ¯x, ȳ ∈ X ∗ be <strong>of</strong> <strong>the</strong> same length andlet φ be a L-formula: <strong>the</strong>n φ[ȳ/¯x] denotes <strong>the</strong> formula obtained by simultaneousrenaming <strong>of</strong> each free occurrence in φ <strong>of</strong> a variable in ¯x with <strong>the</strong> correspondingvariable in ȳ, possibly renaming bound variable occurrences as needed to avoidvariable capture. A formula with no free variable occurrences is termed closedor called a sentence. The universal closure <strong>of</strong> a formula φ is denoted by ∀(φ). Ifφ is a closed L-formula and A is an L-structure, we write A |= φ if φ is s<strong>at</strong>isfiedin A. A set T <strong>of</strong> closed L-formulas is called an L-<strong>the</strong>ory. We write A |= T ifA |= φ for each φ ∈ T . If φ is a closed L-formula and T is an L-<strong>the</strong>ory, we writeT |= φ if, for each L-structure A, A |= T implies A |= φ. In this case we sayth<strong>at</strong> φ is a logical consequence <strong>of</strong> T .3. Termin<strong>at</strong>ion Analysis <strong>of</strong> Individual LoopswhereWe will start by restricting our <strong>at</strong>tention to individual loops <strong>of</strong> <strong>the</strong> form{ I } while B do C (2)• I is a loop invariant th<strong>at</strong> a previous analysis phase has determined to holdjust before any evalu<strong>at</strong>ion <strong>of</strong> B;• B is a Boolean guard expressing <strong>the</strong> condition on <strong>the</strong> st<strong>at</strong>e upon whichiter<strong>at</strong>ion continues;• C is a command th<strong>at</strong>, in <strong>the</strong> context set by (2), is known to always termin<strong>at</strong>e.Notice th<strong>at</strong>, for maximum generality, we do not impose any syntactic restrictionon I, B and C and will only observe <strong>the</strong>ir interaction with <strong>the</strong> program st<strong>at</strong>e: Iand B express conditions on <strong>the</strong> st<strong>at</strong>e, and C is seen as a st<strong>at</strong>e transformer, th<strong>at</strong>is, a condition constraining <strong>the</strong> program st<strong>at</strong>es th<strong>at</strong> correspond to its initial andfinal st<strong>at</strong>es. We assume th<strong>at</strong> such conditions are expressed in a fragment <strong>of</strong> somefirst-order language L th<strong>at</strong> is closed under finite conjunction and implic<strong>at</strong>ion(indeed a limited form <strong>of</strong> implic<strong>at</strong>ion is <strong>of</strong>ten enough). We assume fur<strong>the</strong>r th<strong>at</strong><strong>the</strong> meaning <strong>of</strong> <strong>the</strong> sentences in L is given by some <strong>the</strong>ory T for which we aregiven a sound inference procedure denoted by ‘⊢’, th<strong>at</strong> is, for each sentenceφ ∈ L, if T ⊢ φ <strong>the</strong>n T |= φ. Finally, we fix an L-structure D such th<strong>at</strong> D |= T ,6


which captures <strong>the</strong> domain over which comput<strong>at</strong>ion and program reasoningtake place. Let ¯x be <strong>the</strong> tuple <strong>of</strong> variables containing (among possible o<strong>the</strong>rs)all <strong>the</strong> free variables <strong>of</strong> (2). The effect <strong>of</strong> C within <strong>the</strong> loop can be captured bystipul<strong>at</strong>ing th<strong>at</strong> ¯x characterizes <strong>the</strong> st<strong>at</strong>e before execution <strong>of</strong> C, introducing <strong>at</strong>uple <strong>of</strong> new variables ¯x ′ th<strong>at</strong> characterizes <strong>the</strong> st<strong>at</strong>e after C’s execution, andby imposing restrictions on <strong>the</strong> combined tuple ¯x¯x ′ . Our last assumption is th<strong>at</strong>we are given formulas <strong>of</strong> L th<strong>at</strong> correctly express <strong>the</strong> semantics <strong>of</strong> I, B, and C:let us call <strong>the</strong>se formulas φ I , φ B and φ C , respectively. With <strong>the</strong>se definitionsand assumptions, <strong>the</strong> semantics <strong>of</strong> loop (2) is correctly approxim<strong>at</strong>ed as follows:1. whenever <strong>the</strong> loop guard B is evalu<strong>at</strong>ed, φ I [¯x] holds;2. if φ I [¯x] ∧ φ B [¯x] is inconsistent, iter<strong>at</strong>ion <strong>of</strong> <strong>the</strong> loop termin<strong>at</strong>es;3. just before execution <strong>of</strong> C, φ I [¯x] ∧ φ B [¯x] holds;4. just after execution <strong>of</strong> C, φ I [¯x] ∧ φ B [¯x] ∧ φ C [¯x¯x ′ ] holds.It is worth observing th<strong>at</strong> <strong>the</strong> presence <strong>of</strong> <strong>the</strong> externally-gener<strong>at</strong>ed invariantI is not restrictive: on <strong>the</strong> one hand, φ I [¯x] can simply be <strong>the</strong> “true” formula,when nothing better is available; on <strong>the</strong> o<strong>the</strong>r hand, non trivial invariants areusually a decisive factor for <strong>the</strong> precision <strong>of</strong> termin<strong>at</strong>ion analysis. As observed in[9], <strong>the</strong> requirement th<strong>at</strong> I must hold before any evalu<strong>at</strong>ion <strong>of</strong> B can be relaxedby allowing I not to hold finitely many times. 2 The same kind <strong>of</strong> approxim<strong>at</strong>ioncan be applied to φ I , φ B and φ C by only requesting th<strong>at</strong> <strong>the</strong>y eventually hold.We would like to stress th<strong>at</strong>, <strong>at</strong> this stage, we have not lost generality.While <strong>the</strong> formaliz<strong>at</strong>ion <strong>of</strong> basic iter<strong>at</strong>ion units in terms <strong>of</strong> while loops has anunmistakable imper<strong>at</strong>ive flavor, it is general enough to capture iter<strong>at</strong>ion in o<strong>the</strong>rprogramming paradigms [3].The approach to termin<strong>at</strong>ion analysis based on ranking functions requiresth<strong>at</strong>:1. a set O and a binary rel<strong>at</strong>ion ≺ ⊆ O × O are selected so th<strong>at</strong> O is wellfoundedwith respect to ‘≺’;2. a term δ[ȳ] <strong>of</strong> L is found such th<strong>at</strong>T ⊢ ∀( (φI[¯x] ∧ φ B [¯x] ∧ φ C [¯x¯x ′ ] ) → ω ( δ[¯x ′ /ȳ], δ[¯x/ȳ] )) , (3)where <strong>the</strong> interpret<strong>at</strong>ion <strong>of</strong> ω over D corresponds to ‘≺’; <strong>the</strong> functionassoci<strong>at</strong>ed to δ in D is called ranking function for <strong>the</strong> loop (2).Termin<strong>at</strong>ion <strong>of</strong> (2) follows by <strong>the</strong> correctness <strong>of</strong> φ I , φ B , φ C and ‘⊢’, and bywell-foundedness <strong>of</strong> O with respect to ‘≺’. To see this, suppose, towards acontradiction, th<strong>at</strong> loop (2) does not termin<strong>at</strong>e. The mentioned soundness conditionswould imply <strong>the</strong> existence <strong>of</strong> an infinite sequence <strong>of</strong> elements <strong>of</strong> Oo 0 ≻ o 1 ≻ o 2 ≻ · · · (4)2 Such an invariant is called tail invariant in [9].7


Let U ⊆ O be <strong>the</strong> (nonempty) set <strong>of</strong> elements in <strong>the</strong> sequence. Since O iswell founded with respect to ‘≺’, <strong>the</strong>re exists j ∈ N such th<strong>at</strong>, for each i ∈ N,o i ⊀ o j . But this is impossible, as, for each j ∈ N, o j+1 ≺ o j . This means th<strong>at</strong><strong>the</strong> infinite chain (4) cannot exist and loop (2) termin<strong>at</strong>es.This general view <strong>of</strong> <strong>the</strong> ranking functions approach to termin<strong>at</strong>ion analysisallows us to compare <strong>the</strong> methods in <strong>the</strong> liter<strong>at</strong>ure on a common ground andfocusing on wh<strong>at</strong>, besides mere present<strong>at</strong>ion artifacts, really distinguishes <strong>the</strong>mfrom one ano<strong>the</strong>r. Real differences have to do with:• <strong>the</strong> choice <strong>of</strong> <strong>the</strong> well-founded ordering (O, ≺);• <strong>the</strong> class <strong>of</strong> functions in which <strong>the</strong> method “searches” for <strong>the</strong> rankingfunctions;• <strong>the</strong> choice <strong>of</strong> <strong>the</strong> first-order languae L, <strong>the</strong> domain D and <strong>the</strong>ory T ; thishas to accommod<strong>at</strong>e <strong>the</strong> programming formalism <strong>at</strong> hand, <strong>the</strong> semanticcharacteriz<strong>at</strong>ion upon which termin<strong>at</strong>ion reasoning has to be based, <strong>the</strong>axiom<strong>at</strong>iz<strong>at</strong>ion <strong>of</strong> (O, ≺), and <strong>the</strong> represent<strong>at</strong>ion <strong>of</strong> ranking functions;• <strong>the</strong> class <strong>of</strong> algorithms th<strong>at</strong> <strong>the</strong> method uses to conduct such a search.We now briefly review <strong>the</strong>se aspects.The most n<strong>at</strong>ural well-founded ordering is, <strong>of</strong> course, (N, 0, by < ɛ := { }(h, k) ∈ S 2 ∣+ h + ɛ ≤ k ,where ɛ ∈ S + and S + = Q + or S + = R + , respectively. Of course, this issimply a m<strong>at</strong>ter <strong>of</strong> convenience: a ranking function f with codomain (R + , < ɛ )can always be converted into a ranking function g with codomain (N,


fur<strong>the</strong>r generalized by Mesnard and Serebrenik [17, 18] to obtain affine functions<strong>of</strong> <strong>the</strong> form f(x 1 , . . . , x n ) = µ 0 + ∑ ni=1 µ ix i , where µ i ∈ Z and x i take values inQ or R, for i = 0, . . . , n. Use <strong>of</strong> <strong>the</strong> method <strong>of</strong> Podelski and Rybalchenko [8]was presented in [19] and is a component <strong>of</strong> Termin<strong>at</strong>or, a termin<strong>at</strong>ion prover<strong>of</strong> C systems code [20]. Nguyen and De Schreye [21] proposed, in <strong>the</strong> context <strong>of</strong>logic programming and following a thread <strong>of</strong> work in termin<strong>at</strong>ion <strong>of</strong> term rewritesystems th<strong>at</strong> can be traced back to [22], to use polynomial ranking functions.These are <strong>of</strong> <strong>the</strong> basic form f(x 1 , . . . , x n ) = µ 0 + ∑ mj=1 µ ∏ nj i=1 xkij i where µ 0 ∈ Zand, for i = 1, . . . , n and j = 1, . . . , m, µ j ∈ Z, k ij ∈ N and <strong>the</strong> variable x itakes values in Z [23]. Several fur<strong>the</strong>r restriction are usually imposed: first adomain A ⊆ N is selected; <strong>the</strong>n it is demanded th<strong>at</strong>, for each x 1 , . . . , x n ∈ A,f(x 1 , . . . , x n ) ∈ A and th<strong>at</strong> f is strictly monotone over A on all its arguments.The set <strong>of</strong> all such polynomials is itself well-founded with respect to ‘< A ’: f < Ag if and only if, for each x 1 , . . . , x n ∈ A, f(x 1 , . . . , x n ) < g(x 1 , . . . , x n ). Thecondition <strong>of</strong> strict monotonicity, namely, for each x 1 , . . . , x n ∈ A, each i =1, . . . , n, and each y, z ∈ A with y < z, f(x 1 , . . . , x i−1 , y, x i+1 , . . . , x n )


function. This is <strong>the</strong> case, for instance, <strong>of</strong> [13], where gener<strong>at</strong>ion consists in <strong>the</strong>instanti<strong>at</strong>ion <strong>of</strong> templ<strong>at</strong>e functions and testing employs an algorithm based on avariant <strong>of</strong> Farkas’ Lemma. Non-linear constraints gener<strong>at</strong>ed by <strong>the</strong> method describedin [24] are handled by first resorting to semidefinite programming solversand <strong>the</strong>n valid<strong>at</strong>ing <strong>the</strong> obtained results by using some o<strong>the</strong>r tools, since <strong>the</strong>sesolvers are typically based on interior point algorithms and hence may incur intounsafe rounding errors. Note th<strong>at</strong>, in principle, <strong>the</strong> very same observ<strong>at</strong>ion wouldapply to <strong>the</strong> case <strong>of</strong> linear constraints, if <strong>the</strong> corresponding linear programmingproblem is solved using an interior point method or even a flo<strong>at</strong>ing-point basedimplement<strong>at</strong>ion <strong>of</strong> <strong>the</strong> simplex algorithm; however, <strong>the</strong>re exist implement<strong>at</strong>ions<strong>of</strong> <strong>the</strong> simplex algorithm based on exact arithmetic, so th<strong>at</strong> linear programmingproblems can be numerically solved incurring no rounding errors <strong>at</strong> all and witha comput<strong>at</strong>ional overhead th<strong>at</strong> is <strong>of</strong>ten acceptable. 34. The Approach <strong>of</strong> Sohn and Van Gelder, GeneralizedAs far as we know, <strong>the</strong> first approach to <strong>the</strong> autom<strong>at</strong>ic syn<strong>the</strong>sis <strong>of</strong> rankingfunctions is due to Kirack Sohn and Allen Van Gelder [7, 10]. Even thoughSohn and Van Gelder’s work concerned termin<strong>at</strong>ion <strong>of</strong> logic programs, we willshow th<strong>at</strong> <strong>the</strong> key ideas <strong>of</strong> <strong>the</strong>ir approach can be applied, with only r<strong>at</strong>hersimple modific<strong>at</strong>ions, to <strong>the</strong> syn<strong>the</strong>sis <strong>of</strong> ranking functions for any programmingparadigm, thus going beyond wh<strong>at</strong> subsequent authors acknowledged.4.1. <strong>Ranking</strong> Functions for Binary, Directly Recursive CLP(N) ProgramsWe refer <strong>the</strong> interested reader to [3] for a more complete reconstruction <strong>of</strong><strong>the</strong> approach by Sohn and Van Gelder. For <strong>the</strong> o<strong>the</strong>r readers, suffices it to sayth<strong>at</strong> termin<strong>at</strong>ion analysis <strong>of</strong> logic programs can be mapped onto termin<strong>at</strong>ionanalysis <strong>of</strong> CLP(N) programs, <strong>the</strong> termin<strong>at</strong>ion <strong>of</strong> which implies <strong>the</strong> termin<strong>at</strong>ion<strong>of</strong> <strong>the</strong> original program. 4 In turn, <strong>the</strong> termin<strong>at</strong>ion analysis <strong>of</strong> general CLP(N)programs can be approxim<strong>at</strong>ed by termin<strong>at</strong>ion analysis <strong>of</strong> directly recursive, binaryCLP(N) clauses. In order to show how ranking functions can be computedfrom <strong>the</strong>se, we deal first with a single clausep(¯x) :− c[¯x, ¯x ′ ], p(¯x ′ ), (5)where p is a predic<strong>at</strong>e symbol, ¯x and ¯x ′ are disjoint n-tuples <strong>of</strong> variables, andc[¯x, ¯x ′ ] is a linear constraint involving variables in ¯x ∪ ¯x ′ . 5 The meaning <strong>of</strong> sucha clause is th<strong>at</strong>, if p is called on some tuple <strong>of</strong> integers ¯x, <strong>the</strong>n <strong>the</strong>re are twocases:3 In contrast, an exact solver for non-linear constraints would probably require a trulysymbolic comput<strong>at</strong>ion, incurring a much more significant comput<strong>at</strong>ional overhead.4 See, e.g., [26] for an introduction to CLP —constraint logic programming— languages,and [16] for <strong>the</strong> mapping <strong>of</strong> logic programs to CLP(N) programs.5 As usual, we abuse not<strong>at</strong>ion by confusing a tuple with <strong>the</strong> set <strong>of</strong> its elements.10


• c[¯x, ¯x ′ ] is uns<strong>at</strong>isfiable (i.e., <strong>the</strong>re does not exist a tuple <strong>of</strong> integers ¯x ′ th<strong>at</strong>,toge<strong>the</strong>r with ¯x, s<strong>at</strong>isfies it), in which case <strong>the</strong> comput<strong>at</strong>ion will fail, andthus termin<strong>at</strong>e;• <strong>the</strong>re exists ¯x ′ such th<strong>at</strong> c[¯x, ¯x ′ ] holds, in which case <strong>the</strong> comput<strong>at</strong>ionproceeds with <strong>the</strong> (recursive) calls p(¯x ′ ), for each ¯x ′ such th<strong>at</strong> c[¯x, ¯x ′ ].The question is now to see whe<strong>the</strong>r th<strong>at</strong> recursive procedure is termin<strong>at</strong>ing,th<strong>at</strong> is whe<strong>the</strong>r, for each ¯x ∈ N n , <strong>the</strong> call p(¯x) will only give rise to chains <strong>of</strong>recursive calls <strong>of</strong> finite length. The approach <strong>of</strong> Sohn and Van Gelder allows tosyn<strong>the</strong>size a function f p : N n → N such th<strong>at</strong>∀¯x, ¯x ′ ∈ N n : c[¯x, ¯x ′ ] =⇒ f p (¯x) > f p (¯x ′ ). (6)This means th<strong>at</strong> <strong>the</strong> measure induced by f p strictly decreases when passing froma call <strong>of</strong> p to its recursive call. Since <strong>the</strong> n<strong>at</strong>urals are well founded with respectto ‘


a witness for termin<strong>at</strong>ion <strong>of</strong> (5), but gives us no indic<strong>at</strong>ion about where to lookfor such a tuple <strong>of</strong> n<strong>at</strong>urals.A first step forward consists in considering <strong>the</strong> relax<strong>at</strong>ion <strong>of</strong> (9) obtained byreplacing <strong>the</strong> integrality constraints x, x ′ ∈ N n with x, x ′ ∈ Q n +. This amountsto trading precision for efficiency. In fact, since any feasible solution <strong>of</strong> (9) isalso feasible for <strong>the</strong> relaxed problem, if <strong>the</strong> optimum solution <strong>of</strong> <strong>the</strong> l<strong>at</strong>ter has acost gre<strong>at</strong>er than or equal to 1, <strong>the</strong>n ei<strong>the</strong>r (9) is unfeasible or ˆθ ≥ 1. However,we may have ˆθ ≥ 1 even if <strong>the</strong> optimum <strong>of</strong> <strong>the</strong> relax<strong>at</strong>ion is less than 1. 6On <strong>the</strong> o<strong>the</strong>r hand, <strong>the</strong> relaxed problem is a linear problem: so by giving upcompleteness we have passed from an NP-complete problem to a problem inP for which we have, in addition, quite efficient algorithms. 7 Fur<strong>the</strong>rmore, weobserve th<strong>at</strong> although <strong>the</strong> parameters µ are n<strong>at</strong>urals in (8), this condition canbe relaxed as well: if µ ∈ Q n + gives a relaxed problem with optimum gre<strong>at</strong>erthan 1, <strong>the</strong>n we can multiply this vector by a positive n<strong>at</strong>ural so as to obtaina tuple <strong>of</strong> n<strong>at</strong>urals s<strong>at</strong>isfying (8). The relax<strong>at</strong>ion can now be written using <strong>the</strong>standard linear programming (LP) not<strong>at</strong>ion:minimize 〈µ, −µ〉 T 〈x, x ′ 〉subject toA c 〈x, x ′ 〉 ≥ b c〈x, x ′ 〉 ≥ 0.(10)We still do not know how to determine <strong>the</strong> vector <strong>of</strong> parameters µ so th<strong>at</strong><strong>the</strong> optimum <strong>of</strong> (10) is <strong>at</strong> least 1, but here comes one <strong>of</strong> <strong>the</strong> brilliant ideas <strong>of</strong>Sohn and Van Gelder: passing to <strong>the</strong> dual. It is a classical result <strong>of</strong> LP <strong>the</strong>oryth<strong>at</strong> every LP problem can be converted into an equivalent dual problem. Thedual <strong>of</strong> (10) ismaximize b T c ysubject toA T c y ≤ 〈µ, −µ〉y ≥ 0,(11)where y is an m-column vector <strong>of</strong> (dual) unknowns. Duality <strong>the</strong>ory ensures th<strong>at</strong>if both (10) and (11) have bounded feasible solutions, <strong>the</strong>n both <strong>of</strong> <strong>the</strong>m haveoptimal solutions and <strong>the</strong>se solutions have <strong>the</strong> same cost. More formally, forevery choice <strong>of</strong> <strong>the</strong> parameters µ ∈ Q n +, if 〈ˆx, ˆx ′ 〉 ∈ Q 2n is an optimal solutionfor (10) and ŷ ∈ Q m is an optimal solution for (11), <strong>the</strong>n 〈µ, −µ〉 T 〈ˆx, ˆx ′ 〉 =b T c ŷ. Moreover, if one <strong>of</strong> (10) and (11) is unfeasible, <strong>the</strong>n <strong>the</strong> o<strong>the</strong>r is ei<strong>the</strong>runbounded or unfeasible. In contrast, if one <strong>of</strong> (10) and (11) is unbounded,<strong>the</strong>n <strong>the</strong> o<strong>the</strong>r is definitely unfeasible.Thus, thanks to duality <strong>the</strong>ory, <strong>the</strong> LP problems (10) and (11) are equivalentfor our purposes and we can consider any one <strong>of</strong> <strong>the</strong>m. Suppose we analyze <strong>the</strong>dual problem (11):6 Let us consider <strong>the</strong> clause: p(x) :− 2x ≥ 2x ′ + 1, p(x ′ ) with µ = 1. The optimiz<strong>at</strong>ion over<strong>the</strong> integers leads to ˆθ = 1, whereas <strong>the</strong> optimiz<strong>at</strong>ion for <strong>the</strong> relax<strong>at</strong>ion has ˆθ = 1 2 .7 We denote by P <strong>the</strong> class <strong>of</strong> problems solvable in weakly polynomial time. For a formaldefinition <strong>of</strong> P and <strong>the</strong> notion <strong>of</strong> NP-completeness we refer <strong>the</strong> reader to, e.g., [28].12


• If (11) is unfeasible <strong>the</strong>n ei<strong>the</strong>r (10) is unfeasible, which implies trivialtermin<strong>at</strong>ion <strong>of</strong> (5), or (10) is unbounded, in which case —since we areworking on relax<strong>at</strong>ions— nothing can be concluded about whe<strong>the</strong>r µ definesa ranking function for (5).• If (11) is feasible and unbounded <strong>the</strong>n (10) is unfeasible and (5) triviallytermin<strong>at</strong>es.• If (11) is feasible and bounded, <strong>the</strong>n we have proved termin<strong>at</strong>ion (µ inducesa ranking function) if <strong>the</strong> cost <strong>of</strong> <strong>the</strong> optimal solution is <strong>at</strong> least 1(actually, any positive r<strong>at</strong>ional could be used instead <strong>of</strong> 1). The analysisis inconclusive o<strong>the</strong>rwise.The crucial point is th<strong>at</strong>, in (11), <strong>the</strong> parameters µ occur linearly, whereasin (10) <strong>the</strong>y are multiplied by 〈x, x ′ 〉. So we can tre<strong>at</strong> µ as a vector <strong>of</strong> variablesand transform (11) into <strong>the</strong> new LP problem in m + n variablesmaximize 〈b c , 0〉 T 〈y, µ〉( )−Isubject to A T nc 〈y, µ〉 ≤ 0I n〈y, µ〉 ≥ 0.(12)The requirement th<strong>at</strong>, in order to guarantee termin<strong>at</strong>ion <strong>of</strong> (5), <strong>the</strong> optimalsolutions <strong>of</strong> (10) and (11) should not be less than 1 can now be captured byincorpor<strong>at</strong>ing b T c y ≥ 1 into <strong>the</strong> constraints <strong>of</strong> (12), yieldingThere are several possibilities:maximize 〈b c , 0〉 T 〈y, µ〉⎛⎞−I n(subject to ⎝ AT cI n⎠ 0〈y, µ〉 ≤−1)−b T c 0〈y, µ〉 ≥ 0.(13)1. If (13) is unfeasible, <strong>the</strong>n:(a) if (12) is unfeasible, <strong>the</strong>n, for each µ ∈ Q n +, (11) is unfeasible and:i. if (10) is unfeasible, <strong>the</strong>n (5) trivially termin<strong>at</strong>es;ii. o<strong>the</strong>rwise (10) is unbounded and we can conclude nothing about<strong>the</strong> termin<strong>at</strong>ion <strong>of</strong> (5).(b) If (12) is feasible, <strong>the</strong>n it is bounded by a r<strong>at</strong>ional number q < 1.Thus, for each ˇµ ∈ Q n + extracted from a feasible solution 〈ˇy, ˇµ〉 ∈Q+ m+n <strong>of</strong> (12), <strong>the</strong> corresponding LP problem (11) is also feasible,bounded, and its optimum q ′ ∈ Q is such th<strong>at</strong> q ′ ≤ q < 1. Moreover,we must have q ′ ≤ 0. In fact, if q ′ > 0, problem (10) instanti<strong>at</strong>ed overˇµ ′ := ˇµ/q ′ would have an optimal solution <strong>of</strong> cost 1; <strong>the</strong> same wouldhold for <strong>the</strong> corresponding dual (11), but this would contradict <strong>the</strong>hypo<strong>the</strong>sis th<strong>at</strong> (12) is bounded by q < 1. Hence q ′ ≤ 0. Since byduality <strong>the</strong> optimum <strong>of</strong> problem (10) is q ′ , <strong>the</strong> analysis is inconclusive.13


2. If (13) is feasible, let 〈ˇy, ˇµ〉 ∈ Q m+n be any <strong>of</strong> its feasible solutions.Choosing ˇµ for <strong>the</strong> values <strong>of</strong> <strong>the</strong> parameters, (11) is feasible. There aretwo fur<strong>the</strong>r possibilities:(a) ei<strong>the</strong>r (11) is unbounded, so (5) trivially termin<strong>at</strong>es;(b) or it is bounded by a r<strong>at</strong>ional q ≥ 1 and <strong>the</strong> same holds for itsdual (10).In both cases, ˇµ, possibly multiplied by a positive n<strong>at</strong>ural in order to geta tuple <strong>of</strong> n<strong>at</strong>urals, defines, via (7), a ranking function for (5).The above case analysis boils down to <strong>the</strong> following algorithm:1. Use <strong>the</strong> simplex algorithm to determine <strong>the</strong> feasibility <strong>of</strong> (13), ignoring<strong>the</strong> objective function. If it is feasible, <strong>the</strong>n any feasible solution inducesa linear ranking function for (5); exit with success.2. If (13) is unfeasible, <strong>the</strong>n try to determine <strong>the</strong> feasibility <strong>of</strong> (9) (e.g.,by using <strong>the</strong> simplex algorithm again to test whe<strong>the</strong>r <strong>the</strong> relax<strong>at</strong>ion (10)is feasible). If (9) is unfeasible <strong>the</strong>n (5) trivially termin<strong>at</strong>es; exit withsuccess.3. Exit with failure (<strong>the</strong> analysis is inconclusive).An example should serve to better clarify <strong>the</strong> methodology we have employed.Example 4.1. In <strong>the</strong> CLP(N) programp(x 1 , x 2 ) :− x 1 ≤ 1 ∧ x 2 = 0,p(x 1 , x 2 ) :− x 1 ≥ 2 ∧ 2x ′ 1 + 1 ≥ x 1 ∧ 2x ′ 1 ≤ x 1 ∧ x ′ 2 + 1 = x 2 , p(x ′ 1, x ′ 2),p(x 1 , x 2 ) is equivalent tox 2 ={⌊log 2 (x 1 )⌋, if x 1 ≠ 0;0, o<strong>the</strong>rwise.The relaxed optimiz<strong>at</strong>ion problem in LP not<strong>at</strong>ion (10) is 8minimize 〈µ 1 , µ 2 , −µ 1 , −µ 2 〉 T 〈x 1 , x 2 , x ′ 1, x ′ 2〉⎛⎞ ⎛ ⎞1 0 0 0 ⎛ ⎞ 2−1 0 2 0x 1subject to⎜ 1 0 −2 0⎜x 2⎟⎟ ⎝⎝ 0 1 0 −1⎠x ′ ⎠ ≥ −1⎜ 0⎟1 ⎝x0 −1 0 1′ 1 ⎠2−1〈x 1 , x 2 , x ′ 1, x ′ 2〉 ≥ 0,8 We will tacitly replace an equality in <strong>the</strong> form α = β by <strong>the</strong> equivalent pair <strong>of</strong> inequalitiesα ≥ β and −α ≥ −β whenever <strong>the</strong> substitution is necessary to fit our framework.14


and <strong>the</strong> dual optimiz<strong>at</strong>ion problem (11) ismaximize 〈2, −1, 0, 1, −1〉 T 〈y 1 , y 2 , y 3 , y 4 , y 5 〉⎛ ⎞⎛⎞ y1 −1 1 0 0 1⎛ ⎞subject to ⎜0 0 0 1 −1y 2µ 1⎟⎝0 2 −2 0 0 ⎠ ⎜y 3⎟⎝y 0 0 0 −1 1 4⎠ ≤ ⎜ µ 2⎟⎝−µ 1⎠−µy 25〈y 1 , y 2 , y 3 , y 4 , y 5 〉 ≥ 0.Incorpor<strong>at</strong>ion <strong>of</strong> <strong>the</strong> unknown coefficients <strong>of</strong> µ among <strong>the</strong> problem variablesfinally yields as <strong>the</strong> transformed problem (13):maximize 〈2, −1, 0, 1, −1, 0, 0〉 T 〈y 1 , y 2 , y 3 , y 4 , y 5 , µ 1 , µ 2 〉⎛ ⎞⎛⎞ y 1 ⎛ ⎞1 −1 1 0 0 −1 0y 200 0 0 1 −1 0 −1y 30subject to⎜ 0 2 −2 0 0 1 0⎟y 4≤⎜ 0⎟⎝ 0 0 0 −1 1 0 1 ⎠⎜y 5⎝⎟ 0 ⎠−2 1 0 −1 1 0 0 ⎝µ 1⎠ −1µ 2〈y 1 , y 2 , y 3 , y 4 , y 5 , µ 1 , µ 2 〉 ≥ 0(14)This problem is feasible so this CLP(N) program termin<strong>at</strong>es. Projecting <strong>the</strong>constraints <strong>of</strong> (14) onto µ we obtain, in addition, <strong>the</strong> knowledge th<strong>at</strong> every µwith µ 1 + µ 2 ≥ 1 gives a ranking function. In o<strong>the</strong>r words, µ 1 x 1 + µ 2 x 2 is aranking function if <strong>the</strong> non-neg<strong>at</strong>ive numbers µ 1 and µ 2 s<strong>at</strong>isfy µ 1 + µ 2 ≥ 1.The following result illustr<strong>at</strong>es <strong>the</strong> strength <strong>of</strong> <strong>the</strong> method:Theorem 4.2. Let C be <strong>the</strong> binary CLP(Q + ) clause p(¯x) :− c[¯x, ¯x ′ ], p(¯x ′ ),where p is an n-ary predic<strong>at</strong>e and c[¯x, ¯x ′ ] is a linear s<strong>at</strong>isfiable constraint. Letplrf(C) be <strong>the</strong> set <strong>of</strong> positive linear ranking functions for C and svg(C) be <strong>the</strong>set <strong>of</strong> solutions <strong>of</strong> (13) projected onto µ, th<strong>at</strong> is,{n∑plrf(C) := µ ∈ Q n + ∣ ∀¯x, ¯x′ ∈ Q n + : c[¯x, ¯x ′ ] =⇒ µ i x i −i=1svg(C) := { ˇµ ∈ Q n ∣+ 〈ˇy, ˇµ〉 is a solution <strong>of</strong> (13) } .Then plrf(C) = svg(C).n∑i=1}µ i x ′ i ≥ 1 ,Pro<strong>of</strong>. As c[¯x, ¯x ′ ] is s<strong>at</strong>isfiable, problem (10) is feasible. We prove each inclusionsepar<strong>at</strong>ely.svg(C) ⊆ plrf(C). Assume th<strong>at</strong> (13) is feasible and let 〈ˇy, ˇµ〉 be a solution<strong>of</strong> (13). For this choice <strong>of</strong> ˇµ, <strong>the</strong> corresponding LP problems (10) and (11) arebounded by q ≥ 1 (case 2b <strong>of</strong> <strong>the</strong> discussion above). So ˇµ ∈ plrf(C).15


plrf(C) ⊆ svg(C). Let us pick µ ∈ plrf(C). For this choice, <strong>the</strong> correspondingLP problem (10) is bounded by r ≥ 1, so is its dual (11). Let ŷ be an optimalsolution for (11). Thus 〈ŷ, µ〉 is a feasible solution <strong>of</strong> (12) and (13). Henceµ ∈ svg(C).As an immedi<strong>at</strong>e consequence, <strong>the</strong> question “does a given binary recursiveclause with linear constraint admit a positive linear mapping?” can be solvedin weakly polynomial time.Corollary 4.3. Let C be <strong>the</strong> binary CLP(Q + ) clause p(¯x) :− c[¯x, ¯x ′ ], p(¯x ′ ),where c[¯x, ¯x ′ ] is a linear s<strong>at</strong>isfiable constraint. The decision problem plrf(C) = ∅is in P.Pro<strong>of</strong>. By Theorem 4.2 <strong>the</strong> problems plrf(C) = ∅ and svg(C) = ∅ are equivalent.So, if (13) is feasible <strong>the</strong>n <strong>the</strong> answer is no: as c[¯x, ¯x ′ ] is s<strong>at</strong>isfiable, weare in case (2)(b). O<strong>the</strong>rwise, again because <strong>of</strong> <strong>the</strong> s<strong>at</strong>isfiability <strong>of</strong> c[¯x, ¯x ′ ], ei<strong>the</strong>r(10) is unbounded (case (1)(a)ii. or it is bounded by q ′ < 0 (case (1)(b)).In both cases, <strong>the</strong> answer is yes. Finally, testing <strong>the</strong> s<strong>at</strong>isfiability <strong>of</strong> a linearsystem, as well as computing one <strong>of</strong> its solutions —and thus computing oneconcrete linear ranking function—, is in P (see, e.g., [28]).For <strong>the</strong> case where we have more than one directly recursive binary CLP(N)clauses, C 1 , . . . , C n , <strong>the</strong> set <strong>of</strong> global positive linear ranking functions, i.e., th<strong>at</strong>ensure termin<strong>at</strong>ion whichever clause is selected <strong>at</strong> each comput<strong>at</strong>ion step, isgiven by ⋂ ni=1 svg(C i). This can be computed by taking <strong>the</strong> conjunction <strong>of</strong> <strong>the</strong>constraints obtained, for each clause, from <strong>the</strong> projection <strong>of</strong> <strong>the</strong> constraints <strong>of</strong><strong>the</strong> corresponding linear problem (13) onto µ.To summarize, <strong>the</strong> main contribution <strong>of</strong> Sohn and Van Gelder lies in <strong>the</strong>irencoding <strong>of</strong> <strong>the</strong> ranking function search problem into linear programming and<strong>the</strong>ir use <strong>of</strong> <strong>the</strong> duality <strong>the</strong>orem. As we will see, this idea is amenable to ageneraliz<strong>at</strong>ion th<strong>at</strong> makes it widely applicable to any programming paradigm,not just (constraint) logic programming.4.2. The Generaliz<strong>at</strong>ion by Mesnard and SerebrenikFred Mesnard and Alexandre Serebrenik have generalized <strong>the</strong> method <strong>of</strong>Sohn and Van Gelder from <strong>the</strong> analysis <strong>of</strong> logic programs to <strong>the</strong> analysis <strong>of</strong>CLP(Q) and CLP(R) programs in [17, 18]. In <strong>the</strong> following, for present<strong>at</strong>ionpurposes and without loss <strong>of</strong> generality, we consider <strong>the</strong> case <strong>of</strong> r<strong>at</strong>ional-valuedvariables. They use a class <strong>of</strong> affine ranking functions <strong>of</strong> <strong>the</strong> formf p (y 1 , . . . , y n ) = µ 0 +n∑µ i y i , (15)where µ i ∈ Q, for i = 0, . . . , n. Allowing for r<strong>at</strong>ional-valued coefficients µ i andvariables y i (both <strong>the</strong> µ i ’s and <strong>the</strong> y i ’s were n<strong>at</strong>urals in [7]) implies th<strong>at</strong> (15)i=116


andThus∀¯x, ¯x ′ ∈ Q n : c[¯x, ¯x ′ ] =⇒so th<strong>at</strong> ˇµ ∈ lrf(C).∀¯x, ¯x ′ ∈ Q n : c[¯x, ¯x ′ ] =⇒ µ 0 +n∑µ i x i −i=1n∑µ i x i ≥ 0.i=1n∑µ i x ′ i ≥ 1 ∧ µ 0 +i=1n∑µ i x i ≥ 0,lrf(C) ⊆ ms(C). Let us pick ˜µ ∈ lrf(C). For this choice, <strong>the</strong> corresponding LPproblem (18) are bounded by 1 and 0, and so are <strong>the</strong>ir duals (19). Let ŷ be anoptimal solution for (19) l . Thus 〈ŷ, µ〉 is a feasible solution <strong>of</strong> (20) l . Similarly,let ẑ be an optimal solution for (19) r . Thus 〈ẑ, ˜µ〉 is a feasible solution <strong>of</strong> (20) r .Hence ˜µ ∈ ms(C).Moreover, even for <strong>the</strong> case <strong>of</strong> <strong>the</strong> linear fragment <strong>of</strong> CLP(Q) —and CLP(R)—checking for <strong>the</strong> existence <strong>of</strong> a linear ranking function is a weakly polynomialproblem.Corollary 4.5. Let C be <strong>the</strong> binary CLP(Q) clause p(¯x) :− c[¯x, ¯x ′ ], p(¯x ′ ), wherec[¯x, ¯x ′ ] is a linear s<strong>at</strong>isfiable constraint. The decision problem lrf(C) = ∅ is in P.A space <strong>of</strong> ranking functions can be obtained (<strong>at</strong> a comput<strong>at</strong>ional price th<strong>at</strong>is no longer polynomial) by projecting <strong>the</strong> constraints <strong>of</strong> (20) onto ˜µ. Any ˜µs<strong>at</strong>isfying all <strong>the</strong> projected constraints corresponds to one ranking function th<strong>at</strong>,subject to c[¯x, ¯x ′ ], is bounded from below by 0 and th<strong>at</strong> decreases by <strong>at</strong> least 1<strong>at</strong> each iter<strong>at</strong>ion. From <strong>the</strong>se “normalized” ranking functions, <strong>the</strong> opposite <strong>of</strong><strong>the</strong> transform<strong>at</strong>ion outlined in Section 3 allows to recover all affine rankingfunctions: <strong>the</strong>se are induced by <strong>the</strong> set <strong>of</strong> parameters{〈h, kµ〉∣ ∣ 〈µ 0 , µ〉 ∈ lrf(C), h ∈ Q, k ∈ Q + \ {0} } . (21)i=14.3. Applic<strong>at</strong>ion to <strong>the</strong> Analysis <strong>of</strong> Imper<strong>at</strong>ive While LoopsThe generaliz<strong>at</strong>ion <strong>of</strong> Mesnard and Serebrenik can be used, almost unchanged,to analyze <strong>the</strong> termin<strong>at</strong>ion behavior <strong>of</strong> imper<strong>at</strong>ive while loops withinteger- or r<strong>at</strong>ional-valued variables. Consider a loop <strong>of</strong> <strong>the</strong> form (2), i.e.,{ I } while B do C where I is known to hold before any evalu<strong>at</strong>ion <strong>of</strong> B and Cis known to always termin<strong>at</strong>e in th<strong>at</strong> loop. Termin<strong>at</strong>ion analysis is conductedas follows:1. Variables are duplic<strong>at</strong>ed: if ¯x are <strong>the</strong> n variables <strong>of</strong> <strong>the</strong> original loop, weintroduce a new tuple <strong>of</strong> variables ¯x ′ .19


2. An analyzer based on convex polyhedra [29] is used to analyze <strong>the</strong> followingprogram:{I}x ′ 1 := x 1 ; . . . ; x ′ n := x n ;if B[¯x ′ /¯x] <strong>the</strong>nC[¯x ′ /¯x]⋆(22)Let <strong>the</strong> invariant obtained for <strong>the</strong> program point marked with ‘⋆’ bec[¯x, ¯x ′ ]; this is a finite conjunction <strong>of</strong> linear constraints.3. The method <strong>of</strong> Mesnard and Serebrenik is now applied to <strong>the</strong> CLP(Q)clause p(¯x) :− c[¯x, ¯x ′ ], p(¯x ′ ): if termin<strong>at</strong>ion can be established for th<strong>at</strong>clause, <strong>the</strong>n <strong>the</strong> while loop we started with will termin<strong>at</strong>e.Notice how <strong>the</strong> clause p(¯x) :− c[¯x, ¯x ′ ], p(¯x ′ ) approxim<strong>at</strong>es <strong>the</strong> termin<strong>at</strong>ionbehavior <strong>of</strong> <strong>the</strong> loop: if we interpret <strong>the</strong> predic<strong>at</strong>e p applied to ¯x as “<strong>the</strong> loopguard is evalu<strong>at</strong>ed on values ¯x,” <strong>the</strong>n <strong>the</strong> clause can be read as “if <strong>the</strong> loopguard is evalu<strong>at</strong>ed on values ¯x, and c[¯x, ¯x ′ ] holds, <strong>the</strong>n <strong>the</strong> loop guard will beevalu<strong>at</strong>ed again on values ¯x ′ .”We illustr<strong>at</strong>e <strong>the</strong> overall methodology with an example.Example 4.6. The following program, where x 1 and y take values in Z, computesand stores in x 2 <strong>the</strong> integer base-2 logarithm <strong>of</strong> x 1 if x 1 > 0, 0 o<strong>the</strong>rwise:x 2 := 0;{x 1 ≥ 0 ∧ x 2 ≥ 0}while x 1 ≥ 2 dox 1 := x 1 div 2;x 2 := x 2 + 1where <strong>the</strong> loop invariant {x 1 ≥ 0∧x 2 ≥ 0} has been obtained by st<strong>at</strong>ic analysis.After <strong>the</strong> duplic<strong>at</strong>ion <strong>of</strong> variables, we submit to <strong>the</strong> analyzer <strong>the</strong> program{x 1 ≥ 0 ∧ x 2 ≥ 0}x ′ 1 := x 1 ; x ′ 2 := x 2 ;if x ′ 1 ≥ 2 <strong>the</strong>nx ′ 1 := x ′ 1 div 2;x ′ 2 := x ′ 2 + 1and we obtain, for program point ‘⋆’, <strong>the</strong> invariant⋆x 1 ≥ 2 ∧ 2x ′ 1 + 1 ≥ x 1 ∧ 2x ′ 1 ≤ x 1 ∧ x ′ 2 = x 2 + 1 ∧ x ′ 2 ≥ 1.Applying <strong>the</strong> method <strong>of</strong> Mesnard and Serebrenik we obtain th<strong>at</strong>, for eachµ 0 , µ 1 , µ 2 ∈ Q such th<strong>at</strong> µ 1 − µ 2 ≥ 1, µ 2 ≥ 0, and µ 0 + 2µ 1 ≥ 0, <strong>the</strong> function20


f(x 1 , x 2 ) := µ 0 + µ 1 x 1 + µ 2 x 2 is a ranking function for <strong>the</strong> given while loop.It is interesting to observe th<strong>at</strong> <strong>the</strong> first constraint guarantees strict decrease(<strong>at</strong> least 1), <strong>the</strong> addition <strong>of</strong> <strong>the</strong> second constraint guarantees boundedness frombelow, while <strong>the</strong> fur<strong>the</strong>r addition <strong>of</strong> <strong>the</strong> third constraint ensures nonneg<strong>at</strong>ivity,i.e., th<strong>at</strong> 0 is a lower bound.4.4. Applic<strong>at</strong>ion to Conditional Termin<strong>at</strong>ion AnalysisAn important observ<strong>at</strong>ion is th<strong>at</strong> <strong>the</strong> method <strong>of</strong> Mesnard and Serebrenik isimmedi<strong>at</strong>ely applicable in conditional termin<strong>at</strong>ion analysis. This is <strong>the</strong> problem<strong>of</strong> (autom<strong>at</strong>ically) inferring <strong>the</strong> preconditions under which code th<strong>at</strong> does notuniversally termin<strong>at</strong>e (i.e., <strong>the</strong>re are inputs for which it does loop forever) isguaranteed to termin<strong>at</strong>e. This problem has been recently studied in [30], wherepreconditions are inferred under which functions th<strong>at</strong> are ei<strong>the</strong>r decreasing orbounded become proper ranking functions. The two systems in (20), projectedonto ˜µ, exactly define <strong>the</strong> space <strong>of</strong> non-neg<strong>at</strong>ive candid<strong>at</strong>e ranking functionsand <strong>the</strong> space <strong>of</strong> decreasing candid<strong>at</strong>e ranking functions, respectively. Whilethis is subject for future research, we believe th<strong>at</strong> <strong>the</strong> availability <strong>of</strong> <strong>the</strong>se twospaces allows to improve <strong>the</strong> techniques presented in [30].5. The Approach <strong>of</strong> Podelski and RybalchenkoAndreas Podelski and Andrey Rybalchenko [8] introduce a method for findinglinear ranking functions for a particular class <strong>of</strong> unnested while loops th<strong>at</strong>,with <strong>the</strong> help <strong>of</strong> a preliminary analysis phase, is indeed completely general.Consider a while loop <strong>of</strong> <strong>the</strong> form{I}while B doC⋆(23)in which variables x 1 , . . . , x n occur. Suppose we have determined (e.g., by ad<strong>at</strong>a-flow analysis based on convex polyhedra) th<strong>at</strong> <strong>the</strong> invariantn∑g k,i x i ≤ b k , for k = 1, . . . , r, (24)i=1holds <strong>at</strong> <strong>the</strong> program point marked with ‘’, while <strong>the</strong> invariantn∑a ′ k,ix ′ i ≤i=1n∑a k,i x i + b k , for k = r + 1, . . . , r + s, (25)i=121


holds <strong>at</strong> <strong>the</strong> program point marked with ‘⋆’, where unprimed variables represent<strong>the</strong> values before <strong>the</strong> upd<strong>at</strong>e and primed variables represent <strong>the</strong> values after <strong>the</strong>upd<strong>at</strong>e, and all <strong>the</strong> coefficients and variables are assumed to take values in Q. 11The inequalities in (24) can be expressed in <strong>the</strong> form (25) by just defininga ′ k,i := 0 and a k,i := −g k,i for i = 1, . . . , n and k = 1, . . . , r. The conjunction<strong>of</strong> (24) and (25) can now be st<strong>at</strong>ed in m<strong>at</strong>rix form as( ) ( )A A′ xx ′ ≤ b, (26)where <strong>the</strong> m<strong>at</strong>rix ( A A ′) is obtained by juxtaposition <strong>of</strong> <strong>the</strong> two (r + s) × nm<strong>at</strong>rices A := (−a k,i ) and A ′ := (a ′ k,i ), b := 〈b 1, b 2 , . . . , b r+s 〉 and, as explainedin Section 2, 〈x, x ′ 〉 is obtained by juxtaposing <strong>the</strong> vectors x := 〈x 1 , x 2 , . . . , x n 〉and x ′ := 〈x ′ 1, x ′ 2, . . . , x ′ n〉.Podelski and Rybalchenko have proved th<strong>at</strong> (23) is guaranteed to termin<strong>at</strong>eon all possible inputs if <strong>the</strong>re exist two (r+s)-dimensional non-neg<strong>at</strong>ive r<strong>at</strong>ionalvectors λ 1 and λ 2 such th<strong>at</strong>: 12 λ T 1 A ′ = 0, (27a)(λ T 1 − λ T 2 )A = 0, (27b)λ T 2 (A + A ′ ) = 0,λ T 2 b < 0.(27c)(27d)Note th<strong>at</strong> we have ei<strong>the</strong>r zero or infinitely many solutions, since if <strong>the</strong> pair <strong>of</strong>vectors λ 1 and λ 2 s<strong>at</strong>isfies <strong>the</strong> constraints, <strong>the</strong>n <strong>the</strong> pair kλ 1 and kλ 2 s<strong>at</strong>isfies<strong>the</strong>m as well, for any k ∈ Q + \ {0}. Podelski and Rybalchenko proved also <strong>the</strong>following completeness result: if <strong>the</strong> iter<strong>at</strong>ions <strong>of</strong> (23) are completely characterizedby conditions (24) and (25) —in which case <strong>the</strong>y call it a “simple linearloop”— <strong>the</strong>n λ 1 , λ 2 ∈ Q r+s+ s<strong>at</strong>isfying conditions (27a)–(27d) exist if and onlyif <strong>the</strong> program termin<strong>at</strong>es for all inputs.5.1. Gener<strong>at</strong>ion <strong>of</strong> <strong>Ranking</strong> FunctionsFor each pair <strong>of</strong> vectors λ 1 and λ 2 s<strong>at</strong>isfying <strong>the</strong> conditions (27a)–(27d), alinear ranking function for <strong>the</strong> considered program can be obtained asf(x) := λ T 2 A ′ x. (28)11 In [8] variables are said to have integer domain, but this restriction seems unnecessaryand, in fact, it is not present in [31].12 For an informal justific<strong>at</strong>ion <strong>of</strong> <strong>the</strong>se equ<strong>at</strong>ions, see Section 5.2; a more detailed explan<strong>at</strong>ionis available in [3].22


In [8] a slightly more complex form is proposed, namely:⎧⎪⎨λ T 2 A ′ x, if <strong>the</strong>re exists x ′ such th<strong>at</strong>g(x) :=⎪⎩(λ T 2 − λ T 1 )b, o<strong>the</strong>rwise,(AA ′ ) ( xx ′ )≤ b ,(29)but <strong>the</strong> extra provisions are actually necessary only if one is interested into an“extended ranking function” th<strong>at</strong> is strictly decreasing also on <strong>the</strong> very lastiter<strong>at</strong>ion <strong>of</strong> <strong>the</strong> loop, th<strong>at</strong> is, when <strong>the</strong> effect <strong>of</strong> <strong>the</strong> command C is such th<strong>at</strong> xwould viol<strong>at</strong>e <strong>the</strong> loop guard B <strong>at</strong> <strong>the</strong> following iter<strong>at</strong>ion. As this more complexdefinition does not seem to provide any additional benefit, we disregard it andconsider only <strong>the</strong> linear ranking function (28).Example 5.1. Consider again <strong>the</strong> program <strong>of</strong> Example 4.6. The invariants in<strong>the</strong> forms dict<strong>at</strong>ed by (24) and (25) are given by <strong>the</strong> systems {−x 1 ≤ −2, −x ′ 2 ≤−1} and {2x ′ 1 ≤ x 1 , −2x ′ 1 −1 ≤ −x 1 , −x ′ 2 ≤ −x 2 −1, x ′ 2 ≤ x 2 +1}, respectively.These can be expressed in <strong>the</strong> m<strong>at</strong>rix form (26) by letting⎛ ⎞ ⎛ ⎞ ⎛ ⎞−1 00 0−2−1 02 00A :=1 0⎜ 0 1⎟, A ′ :=−2 0⎜ 0 −1⎟, b =1⎜−1⎟.⎜⎝0 −10 0⎟⎠⎜⎝0 10 −1Two non-neg<strong>at</strong>ive r<strong>at</strong>ional vectors solving <strong>the</strong> system (27) are, for instance,λ 1 = 〈2, 0, 0, 0, 0, 0〉 T and λ 2 = 〈1, 1, 0, 0, 0, 0〉 T .5.2. Justific<strong>at</strong>ion <strong>of</strong> <strong>the</strong> ApproachA reader <strong>of</strong> [8] wonders where <strong>the</strong> method <strong>of</strong> Podelski and Rybalchenkocomes from. In fact, <strong>the</strong> paper does not give an intuition about why conditions(27a)–(27d) imply termin<strong>at</strong>ion <strong>of</strong> (23). Those conditions can be mapped into astreng<strong>the</strong>ning, tailored to <strong>the</strong> linear case, <strong>of</strong> <strong>the</strong> well known Floyd termin<strong>at</strong>ionverific<strong>at</strong>ion conditions, 13 but such a higher level view needs to be extracted,with some effort, from <strong>the</strong> details <strong>of</strong> <strong>the</strong> pro<strong>of</strong> <strong>of</strong> [8, Theorem 1]. The rel<strong>at</strong>ivecompleteness <strong>of</strong> <strong>the</strong> approach is <strong>the</strong>n proved in [8, Theorem 2] by exploiting <strong>the</strong>affine form <strong>of</strong> Farkas’ Lemma, showing th<strong>at</strong> such a streng<strong>the</strong>ning is unconsequentialfor <strong>the</strong> case <strong>of</strong> linear ranking functions and simple linear loops.The intuitive reading hidden in <strong>the</strong> pro<strong>of</strong> details is made explicit in [24,Section 6.2], where P<strong>at</strong>rick Cousot hints th<strong>at</strong> <strong>the</strong> method by Podelski and Rybalchenkocan be derived from <strong>the</strong> Floyd termin<strong>at</strong>ion verific<strong>at</strong>ion conditionsby applic<strong>at</strong>ion <strong>of</strong> Lagrangian relax<strong>at</strong>ion. The interested reader can find moredetails on this connection in <strong>the</strong> technical report version <strong>of</strong> this paper [3].⎟⎠⎜⎝1−1⎟⎠13 A. Rybalchenko, personal communic<strong>at</strong>ion, 2011.23


5.3. An Altern<strong>at</strong>ive Implement<strong>at</strong>ion ApproachAs long as <strong>the</strong> distinction between invariants (24) and (25) is retained, <strong>the</strong>method <strong>of</strong> Podelski and Rybalchenko can be implemented following an altern<strong>at</strong>iveapproach. The linear invariants (26) are more precisely described by( ) ( ) ( )AB 0 x bBx ′ ≤(30)b CA CA ′ Cwhere A B ∈ Q r×n , A C ∈ Q s×n , A ′ C ∈ Qs×n , b B ∈ Q r , b C ∈ Q s . As shownin <strong>the</strong> technical report version <strong>of</strong> this paper [3, Section 5.3], <strong>the</strong> existence <strong>of</strong> alinear ranking function for <strong>the</strong> system (30) is equivalent to <strong>the</strong> existence <strong>of</strong> threevectors v 1 ∈ Q r +, v 2 ∈ Q r +, v 3 ∈ Q s + such th<strong>at</strong>(v 1 − v 2 ) T A B − v T 3 A C = 0, (31a)v T 2 A B + v T 3 (A C + A ′ C) = 0,v 2 b B + v 3 b C < 0.(31b)(31c)As already noted, <strong>the</strong> two vectors <strong>of</strong> <strong>the</strong> original Podelski and Rybalchenkomethod can be reconstructed as λ 1 = 〈v 1 , 0〉 and λ 2 = 〈v 2 , v 3 〉.Note th<strong>at</strong> <strong>the</strong> same approach is still valid when starting from <strong>the</strong> singlem<strong>at</strong>rix form (26) in full generality, i.e., when we can’t assume th<strong>at</strong> <strong>the</strong> distinctionbetween invariants (24) and (25) has been retained or th<strong>at</strong> invariants are listedin <strong>the</strong> order we used to build <strong>the</strong> m<strong>at</strong>rix form (26): it is enough to applya straightforward permut<strong>at</strong>ion to (26) to rearrange it in <strong>the</strong> form (30). Inth<strong>at</strong> case, due to <strong>the</strong> permut<strong>at</strong>ion involved, we would solve a different linearprogramming problem; however, we still obtain <strong>the</strong> same space <strong>of</strong> linear rankingfunctions we would have obtained by applying <strong>the</strong> original method starting from<strong>the</strong> m<strong>at</strong>rix form (26), as we prove using <strong>the</strong> following lemma.Lemma 5.2. Let S be <strong>the</strong> space <strong>of</strong> linear ranking functions obtained by applying<strong>the</strong> method <strong>of</strong> Podelski and Rybalchenko to ( A A ′) 〈x, x ′ 〉 ≤ b, i.e.,S := { 〈λ T 2 A ′ , λ T 1 b〉 ∈ Q n+1 ∣ ∣ 〈λ1 , λ 2 〉 is a solution <strong>of</strong> (27) } ,and let P ∈ Q (r+s)×(r+s) be a permut<strong>at</strong>ion m<strong>at</strong>rix. 14 Then <strong>the</strong> applic<strong>at</strong>ion <strong>of</strong><strong>the</strong> method <strong>of</strong> Podelski and Rybalchenko to P ( A A ′) 〈x, x ′ 〉 ≤ P b yields <strong>the</strong>same space <strong>of</strong> linear ranking functions S.Pro<strong>of</strong>. The system (27) corresponding to P ( Aη T 1 P A ′ = 0,A ′) 〈x, x ′ 〉 ≤ P b becomes(32a)(η T 1 − η T 2 )P A = 0, (32b)η T 2 P (A + A ′ ) = 0,η T 2 P b < 0,(32c)(32d)14 We recall th<strong>at</strong> a k-dimensional permut<strong>at</strong>ion m<strong>at</strong>rix is a square m<strong>at</strong>rix obtained by apermut<strong>at</strong>ion <strong>of</strong> <strong>the</strong> rows or columns <strong>of</strong> <strong>the</strong> k-dimensional identity m<strong>at</strong>rix.24


to be solved for <strong>the</strong> two (r + s)-dimensional non-neg<strong>at</strong>ive r<strong>at</strong>ional vectors η 1and η 2 .Now, 〈λ 1 , λ 2 〉 is a solution <strong>of</strong> (27) if and only if 〈λ 1 , λ 2 〉P −1 is a solution<strong>of</strong> (32): on one side, if 〈λ 1 , λ 2 〉 is a solution <strong>of</strong> (27) <strong>the</strong>n 〈η 1 , η 2 〉 defined as〈η 1 , η 2 〉 := 〈λ 1 , λ 2 〉P −1 is a solution <strong>of</strong> (32); on <strong>the</strong> o<strong>the</strong>r side, if 〈η 1 , η 2 〉 isa solution <strong>of</strong> (32) <strong>the</strong>n 〈λ 1 , λ 2 〉 defined as 〈λ 1 , λ 2 〉 := 〈η 1 , η 2 〉P is a solution<strong>of</strong> (27) and <strong>the</strong> desired property can be verified by right-multiplying by P −1both solutions.The space <strong>of</strong> linear ranking functions for <strong>the</strong> permuted system isS P = { 〈η T 2 P A ′ , η T 1 P b〉 ∈ Q n+1 ∣ ∣ 〈η1 , η 2 〉 is a solution <strong>of</strong> (32) }= { 〈λ T 2 P −1 P A ′ , λ T 1 P −1 P b〉 ∈ Q n+1 ∣ ∣ 〈λ 1 , λ 2 〉 is a solution <strong>of</strong> (27) }= S,and thus it is unaltered with respect to <strong>the</strong> space <strong>of</strong> linear ranking functions Scorresponding to <strong>the</strong> non-permuted system.Since <strong>the</strong> system (30) is obtained by applying a suitable permut<strong>at</strong>ion to (26),a straightforward applic<strong>at</strong>ion <strong>of</strong> this lemma proves th<strong>at</strong> <strong>the</strong> space <strong>of</strong> linear rankingfunctions obtained is <strong>the</strong> same in both cases.Moreover, as λ T 2 A ′ = v T 3 A ′ C and λ 1b = v 1 b B , we can express <strong>the</strong> space <strong>of</strong>linear ranking functions asS := { 〈v T 3 A ′ C, v 1 b B 〉 ∈ Q n+1 ∣ ∣ 〈v1 , v 2 , v 3 〉 is a solution <strong>of</strong> (31) } .6. Comparison <strong>of</strong> <strong>the</strong> Two MethodsIn this section we compare <strong>the</strong> method by Mesnard and Serebrenik with<strong>the</strong> method by Podelski and Rybalchenko: we first prove th<strong>at</strong> <strong>the</strong>y have <strong>the</strong>same “inferential power”, <strong>the</strong>n we compare <strong>the</strong>ir worst-case complexities, <strong>the</strong>nwe experimentally evalu<strong>at</strong>e <strong>the</strong>m on a represent<strong>at</strong>ive set <strong>of</strong> benchmarks.6.1. Equivalence <strong>of</strong> <strong>the</strong> Two MethodsWe will now show th<strong>at</strong> <strong>the</strong> method proposed in [8] is equivalent to <strong>the</strong> onegiven in [32] on <strong>the</strong> class <strong>of</strong> simple linear loops, i.e., th<strong>at</strong> if one <strong>of</strong> <strong>the</strong> twomethods can prove termin<strong>at</strong>ion <strong>of</strong> a given simple linear loop, <strong>the</strong>n <strong>the</strong> o<strong>the</strong>r onecan do <strong>the</strong> same. This is an expected result since both methods claim to becomplete on <strong>the</strong> class <strong>of</strong> programs considered.It is worth noting th<strong>at</strong> a completeness result was already st<strong>at</strong>ed in [16,Theorem 5.1] for <strong>the</strong> case <strong>of</strong> single predic<strong>at</strong>e CLP(Q + ) procedures, which canbe seen to be a close variant <strong>of</strong> <strong>the</strong> binary, directly recursive CLP(Q + ) programsconsidered in Theorem 4.2 and Corollary 4.3. Probably due to <strong>the</strong> programmingparadigm mism<strong>at</strong>ch, Podelski and Rybalchenko [8] fail to recognize <strong>the</strong> actualstrength and generality <strong>of</strong> <strong>the</strong> mentioned result, <strong>the</strong>reby claiming originality for<strong>the</strong>ir completeness result.25


Theorem 6.1. Let C be <strong>the</strong> binary CLP(Q) clause p(¯x) :− c[¯x, ¯x ′ ], p(¯x ′ ), wherep is an n-ary predic<strong>at</strong>e and c[¯x, ¯x ′ ] is a linear s<strong>at</strong>isfiable constraint. Let pr(C)and ̂ms(C) be <strong>the</strong> spaces <strong>of</strong> linear ranking functions for C obtained through <strong>the</strong>method <strong>of</strong> Podelski and Rybalchenko and through <strong>the</strong> method <strong>of</strong> Mesnard andSerebrenik, respectively, th<strong>at</strong> is,pr(C) := { 〈λ T 2 A ′ , λ T 1 b〉 ∈ Q ∣ n+1 〈λ1 , λ 2 〉 is a solution <strong>of</strong> (27) } ,{∣ }∣∣∣∣̂ms(C) := k ˜µ ∈ Q n+1 〈y, µ〉 and 〈z, ˜µ〉 are solutions <strong>of</strong> (20),.k ∈ Q + \ {0}A ′) 〈x, x ′ 〉 ≤ b or to A c 〈x, x ′ 〉 ≥ b c , respec-where c[¯x, ¯x ′ ] is equivalent to ( Atively. Then pr(C) = ̂ms(C).Pro<strong>of</strong>. We will, as customary, prove <strong>the</strong> two inclusions pr(C) ⊆ ̂ms(C) andpr(C) ⊇ ̂ms(C).pr(C) ⊆ ̂ms(C). Suppose th<strong>at</strong> <strong>the</strong>re exist two non-neg<strong>at</strong>ive r<strong>at</strong>ional vectorsλ 1 and λ 2 s<strong>at</strong>isfying (27), i.e., λ T 1 A ′ = (λ T 1 − λ T 2 )A = λ T 2 (A + A ′ ) = 0 andλ T 2 b < 0. We need to show th<strong>at</strong> 〈λ T 2 A ′ , λ T 1 b〉 ∈ ̂ms(C), which is equivalentto proving th<strong>at</strong> <strong>the</strong>re exists a positive coefficient (th<strong>at</strong> we can denote with 1 kwithout loss <strong>of</strong> generality) 1 k ∈ Q + \ {0} such th<strong>at</strong> 〈 1 k λT 2 A ′ , 1 k λT 1 b〉 ∈ ms(C), or,by Theorem 4.4, th<strong>at</strong> 〈 1 k λT 2 A ′ , 1 k λT 1 b〉 ∈ lrf(C), which is in turn equivalent, bydefinition, to λ T 2 A ′ x − λ T 2 A ′ x ′ ≥ k and λ T 1 b + λ T 2 A ′ x ≥ 0. We have( ) ( )A A′ xx ′ ≤ b =⇒ Ax + A ′ x ′ ≤ b=⇒ − Ax ≥ A ′ x ′ − b=⇒ − λ T 2 Ax ≥ λ T 2 A ′ x ′ − λ T 2 b [by λ 2 ≥ 0]=⇒ λ T 2 A ′ x ≥ λ T 2 A ′ x ′ − λ T 2 b [by (27c)]and <strong>the</strong> former property is s<strong>at</strong>isfied if we choose k = −λ T 2 b, which is nonneg<strong>at</strong>iveby rel<strong>at</strong>ion (27d). For <strong>the</strong> l<strong>at</strong>ter property, we haveAx + A ′ x ′ ≤ b =⇒ λ T 1 Ax + λ T 1 A ′ x ′ ≤ λ T 1 band both properties are thus proved.as λ T 1 is non-neg<strong>at</strong>ive=⇒ λ T 1 Ax ≤ λ T 1 b [by (27a)]=⇒ λ T 2 Ax ≤ λ T 1 b [by (27b)]=⇒ − λ T 2 A ′ x ≤ λ T 1 b [by (27c)]pr(C) ⊇ ̂ms(C). In order to prove <strong>the</strong> inverse containment, we will need torecall <strong>the</strong> affine form <strong>of</strong> Farkas’ Lemma (see [28]).Lemma 6.2 (Affine form <strong>of</strong> Farkas’ lemma). Let P be a nonempty polyhedrondefined by <strong>the</strong> inequalities Cx + d ≥ 0. Then an affine function f(x) isnon-neg<strong>at</strong>ive everywhere in P if and only if it is a positive affine combin<strong>at</strong>ion<strong>of</strong> <strong>the</strong> columns <strong>of</strong> Cx + d: f(x) = λ 0 + λ T (Cx + d) with λ 0 ≥ 0, λ ≥ 0.26


Let ˜µ ∈ ̂ms(C). Then <strong>the</strong>re exists h ∈ Q + \ {0} such th<strong>at</strong> h˜µ ∈ lrf(C)describes a linear ranking function f for C.The inequalities ( A A ′) 〈x, x ′ 〉 ≤ b define a polyhedron; according to <strong>the</strong>affine form <strong>of</strong> Farkas’ lemma, a function is non-neg<strong>at</strong>ive on this polyhedron,i.e., throughout <strong>the</strong> loop, if and only if it is a positive affine combin<strong>at</strong>ion <strong>of</strong> <strong>the</strong>column vectors ( A A ′) 〈x, x ′ 〉 ≤ b. In particular this holds for <strong>the</strong> rankingfunction f and its two properties: f(x) ≥ 0 and f(x) − f(x ′ ) ≥ 1.Hence <strong>the</strong>re exist two non-neg<strong>at</strong>ive r<strong>at</strong>ional vectors λ 1 and λ 2 and two nonneg<strong>at</strong>ivenumbers λ 0,1 and λ 0,2 such th<strong>at</strong>( (f(x) = λ 0,1 + λ ) T 1 − A A′〈x, x ′ 〉 + b )andf(x) − f(x ′ ( () − 1 = λ 0,2 + λ ) T 2 − A A′〈x, x ′ 〉 + b ) .Replacing f(x) by hµx + hµ 0 , we get two equalities —one for <strong>the</strong> partcontaining variables and one for <strong>the</strong> remaining part— for each expression. Aftersimplific<strong>at</strong>ion we obtain <strong>the</strong> following equalities:(−λ ) T 1 A A′〈x, x ′ 〉 = hµx (33a)(−λ ) T 2 A A′〈x, x ′ 〉 = hµx − hµx ′ (33b)−λ T 2 b = 1 + λ 0,2(33c)From (33a) and (33b) we obtain λ T 1 A = −hµ T , λ T 1 A ′ = 0, λ T 2 A = −hµ Tand λ T 2 A ′ = hµ T . We can rewrite it as 0 = λ T 1 A ′ = (λ T 1 − λ T 2 )A = λ T 2 (A + A ′ ).From (33c) we deduce λ T 2 b < 0.The four conditions (27) to prove termin<strong>at</strong>ion by [8] are thus s<strong>at</strong>isfied.The combin<strong>at</strong>ion <strong>of</strong> Theorems 4.4 and 6.1 gives:Theorem 6.3. Let C be <strong>the</strong> binary CLP(Q) clause p(¯x) :− c[¯x, ¯x ′ ], p(¯x ′ ), wherep is an n-ary predic<strong>at</strong>e and c[¯x, ¯x ′ ] is a linear s<strong>at</strong>isfiable constraint. Let ̂lrf(C) be<strong>the</strong> set <strong>of</strong> (positive multiples <strong>of</strong>) linear ranking functions for C, ̂ms(C) be <strong>the</strong> set<strong>of</strong> (positive multiples <strong>of</strong>) solutions <strong>of</strong> <strong>the</strong> Mesnard and Serebrenik system (20)projected onto µ and pr(C) be <strong>the</strong> set <strong>of</strong> <strong>the</strong> ranking function coefficients obtainedthrough <strong>the</strong> method <strong>of</strong> Podelski and Rybalchenko, th<strong>at</strong> is,⎧∣ ⎫∣∣∣∣∣∣∣∣ ∀¯x, ¯x ′ ∈ Q n : c[¯x, ¯x ′ ] =⇒⎪⎨∑ n̂lrf(C) := k ˜µ ∈ Q n+1 i=1 µ ix i − ∑ ni=1 µ ix ′ i ≥ 1 ⎪⎬∧ µ 0 + ∑ ni=1 ⎪⎩µ ,ix i ≥ 0,⎪⎭k ∈ Q + \ {0}{∣ }∣∣∣∣̂ms(C) := k ˜µ ∈ Q n+1 〈y, µ〉 and 〈z, ˜µ〉 are solutions <strong>of</strong> (20),,k ∈ Q + \ {0}pr(C) := { 〈λ T 2 A ′ , λ T 1 b〉 ∈ Q n+1 ∣ ∣ 〈λ 1 , λ 2 〉 is a solution <strong>of</strong> (27) } ,where c[¯x, ¯x ′ ] is equivalent to ( AA ′) 〈x, x ′ 〉 ≤ b or to A c 〈x, x ′ 〉 ≥ b c , respectively.Then ̂lrf(C) = ̂ms(C) = pr(C).27


6.2. Worst-Case Complexity Using <strong>the</strong> Simplex AlgorithmThe comput<strong>at</strong>ionally most expensive component in both methods is <strong>the</strong> resolution<strong>of</strong> a linear optimiz<strong>at</strong>ion problem th<strong>at</strong> can always be expressed in <strong>the</strong>standard formminimize c T xsubject toAx = bx ≥ 0by applying well known transform<strong>at</strong>ions: inequalities and unconstrained (i.e.,not subject to lower or upper bounds) variables can be replaced and <strong>the</strong> resultingequivalent problem in standard form has one more variable for each inequalityor unconstrained variable appearing in <strong>the</strong> original problem.The most common way to solve this linear optimiz<strong>at</strong>ion problems involvesusing <strong>the</strong> simplex algorithm [33], an iter<strong>at</strong>ive algorithm th<strong>at</strong> requires ( )e+uepivoting steps in <strong>the</strong> worst-case scenario, where e and u denote <strong>the</strong> number <strong>of</strong>equalities in A and unknowns in x respectively.For a simple linear loop <strong>of</strong> m inequalities over n variables, Podelski and Rybalchenkorequire to solve a linear problem in standard form having 3n equalitiesover 2m variables (<strong>the</strong> opposite <strong>of</strong> <strong>the</strong> expression appearing in (27d) canbe ( used as <strong>the</strong> quantity to be minimized); this gives a worst-case complexity <strong>of</strong>3n+2m)3n pivoting steps, corresponding, by Stirling’s formula, to an exponentialcomplexity <strong>of</strong> exponent 3n + 2m approxim<strong>at</strong>ely.If <strong>the</strong> altern<strong>at</strong>ive formaliz<strong>at</strong>ion <strong>of</strong> <strong>the</strong> Podelski and Rybalchenko methodis adopted for <strong>the</strong> same loop, <strong>the</strong>n we will have <strong>the</strong> same m constraints asabove for <strong>the</strong> ‘⋆’ invariant, while <strong>the</strong> ‘’ invariant will be described by o<strong>the</strong>r lconstraints. If redundant constraints are removed, we will have l ≤ m. Hence,<strong>the</strong> altern<strong>at</strong>ive approach will result in a linear programming problem having 2nequalities over m + 2l variables. Hence, <strong>the</strong> worst-case number <strong>of</strong> pivoting stepswill be an exponential <strong>of</strong> exponent approxim<strong>at</strong>ely 2n + m + 2l.For <strong>the</strong> same simple linear loop, Serebrenik and Mesnard require <strong>the</strong> resolution<strong>of</strong> two linear problems, th<strong>at</strong> can be rewritten to contain 2n equalitiesover m + n variables (with n unconstrained variables) and 2n + 1 equalities over(m + 2) + (n + 1) variables (with n + 1 unconstrained variables), respectively.They can <strong>the</strong>n be merged to gener<strong>at</strong>e a single linear problem <strong>of</strong> 4n + 1 equalitiesover m + (m + 2) + (n + 1) variables, n + 1 <strong>of</strong> which unconstrained, andan extra inequality replacing one <strong>of</strong> <strong>the</strong> two objective functions. In <strong>the</strong> end, weget a linear problem in standard form with 4n + 2 equalities over 2m + 2n + 5variables. This means a worst-case complexity <strong>of</strong> ( )6n+2m+74n+2 pivoting steps andan exponential complexity <strong>of</strong> exponent 6n + 2m approxim<strong>at</strong>ely.So <strong>the</strong> method proposed by Podelski and Rybalchenko has, in general, alower worst-case complexity than <strong>the</strong> one proposed by Mesnard and Serebrenik,if <strong>the</strong> single linear problem approach is chosen. The comparison <strong>of</strong> <strong>the</strong> two altern<strong>at</strong>iveimplement<strong>at</strong>ion approaches for <strong>the</strong> Podelski and Rybalchenko methoddepends on <strong>the</strong> rel<strong>at</strong>ions between quantities n, m and l. On <strong>the</strong> one hand, if lis significantly smaller than m, <strong>the</strong>n <strong>the</strong> altern<strong>at</strong>ive approach could result in anefficiency improvement. On <strong>the</strong> o<strong>the</strong>r hand, if <strong>the</strong> number <strong>of</strong> constraints is much28


higher than <strong>the</strong> number <strong>of</strong> variables, <strong>the</strong>n <strong>the</strong> original implement<strong>at</strong>ion approachshould be preferred. Note th<strong>at</strong> <strong>the</strong> need for two loop invariants instead <strong>of</strong> a singleone should not be seen as a big practical problem: in fact, most analysisframeworks will provide <strong>the</strong> ‘’ invariant as <strong>the</strong> original input to <strong>the</strong> termin<strong>at</strong>ionanalysis tool, which will <strong>the</strong>n use it to compute <strong>the</strong> ‘⋆’ invariant (via <strong>the</strong>abstract execution <strong>of</strong> a single iter<strong>at</strong>ion <strong>of</strong> <strong>the</strong> loop); th<strong>at</strong> is, <strong>the</strong> comput<strong>at</strong>ionalcost for <strong>the</strong> ‘’ invariant is implicitly paid anyway.It is well known, though, th<strong>at</strong> <strong>the</strong> worst-case scenario for <strong>the</strong> simplex algorithmis extremely uncommon in practice. An average complexity analysis and,more recently, a smoo<strong>the</strong>d complexity analysis [34] have been carried out on <strong>the</strong>simplex algorithm and showed why it usually takes polynomial time. Besides<strong>the</strong> <strong>the</strong>oretical studies, several experimental evalu<strong>at</strong>ions <strong>of</strong> implement<strong>at</strong>ions <strong>of</strong><strong>the</strong> simplex algorithm reported th<strong>at</strong> <strong>the</strong> average number <strong>of</strong> pivoting steps seemsto grow linearly with <strong>the</strong> sum e + u <strong>of</strong> <strong>the</strong> number <strong>of</strong> equalities and unknowns<strong>of</strong> <strong>the</strong> problem. Therefore, for a more inform<strong>at</strong>ive and meaningful comparison,<strong>the</strong> next section presents an experimental evalu<strong>at</strong>ion <strong>of</strong> <strong>the</strong> methods on arepresent<strong>at</strong>ive set <strong>of</strong> while loops.7. Implement<strong>at</strong>ion and Experimental Evalu<strong>at</strong>ionThe Parma Polyhedra Library (PPL) is a free s<strong>of</strong>tware, pr<strong>of</strong>essional libraryfor <strong>the</strong> handling <strong>of</strong> numeric approxim<strong>at</strong>ions targeted <strong>at</strong> st<strong>at</strong>ic analysis andcomputer-aided verific<strong>at</strong>ion <strong>of</strong> hardware and s<strong>of</strong>tware systems [6, 35]. The PPL,which fe<strong>at</strong>ures several unique innov<strong>at</strong>ions [36, 37, 38, 39, 40], is employed bynumerous projects in this field, most notably by GCC, <strong>the</strong> GNU Compiler Collection,probably <strong>the</strong> most widely used suite <strong>of</strong> compilers. 15As an integral part <strong>of</strong> <strong>the</strong> overall project to which <strong>the</strong> present paper belongs—whose aim is to make <strong>the</strong> technology <strong>of</strong> <strong>the</strong> autom<strong>at</strong>ic syn<strong>the</strong>sis <strong>of</strong> linear rankingfunctions thoroughly explained and generally available—, we have extended<strong>the</strong> PPL with all <strong>the</strong> methods discussed in <strong>the</strong> present paper. Previously, onlya r<strong>at</strong>her limited demo version <strong>of</strong> RankFinder was available, only in x86/Linuxbinary form<strong>at</strong>, implementing <strong>the</strong> method by Podelski and Rybalchenko. 16 Incontrast, <strong>the</strong> PPL implement<strong>at</strong>ion is completely general and available, both insource and binary form<strong>at</strong>s, with high-level interfaces to C, C ++ , Java, OCamland six different Prolog systems.For each <strong>of</strong> <strong>the</strong> methods —Mesnard and Serebrenik (MS) or Podelski andRybalchenko (PR)—, for each <strong>of</strong> <strong>the</strong> two possibilities to encode <strong>the</strong> input —ei<strong>the</strong>r <strong>the</strong> single ⋆ invariant <strong>of</strong> (22) in Section 4.3, or <strong>the</strong> two and ⋆ invariants<strong>of</strong> (23) in Section 5—, for each numerical abstractions supported by <strong>the</strong>PPL —including (not necessarily closed) convex polyhedra, bounded-differenceshapes and octagonal shapes—, <strong>the</strong> PPL provides three distinct functionalitiesto investig<strong>at</strong>e termin<strong>at</strong>ion <strong>of</strong> <strong>the</strong> loop being analyzed:15 See "http://bugseng.com/products/ppl for more inform<strong>at</strong>ion.16 See http://www7.in.tum.de/~rybal/rankfinder/, last checked on August 18th, 2011.29


1. a Boolean termin<strong>at</strong>ion test;2. a Boolean termin<strong>at</strong>ion test th<strong>at</strong>, in addition, returns <strong>the</strong> coefficients <strong>of</strong>one (not fur<strong>the</strong>r specified) affine ranking function;3. a function returning a convex polyhedron th<strong>at</strong> encodes <strong>the</strong> space <strong>of</strong> allaffine ranking functions.In addition, using <strong>the</strong> MS method and for each input method, <strong>the</strong> PPL provides4. a function returning two convex polyhedra th<strong>at</strong> encode <strong>the</strong> space <strong>of</strong> all decreasingfunctions (also known as quasi-ranking functions) and all boundedfunctions, respectively, for use in conditional termin<strong>at</strong>ion analysis.We have evalu<strong>at</strong>ed <strong>the</strong> performance <strong>of</strong> <strong>the</strong> new algorithms implemented in<strong>the</strong> PPL using <strong>the</strong> termin<strong>at</strong>ion analyzer built into Julia, a st<strong>at</strong>e-<strong>of</strong>-<strong>the</strong>-art analyzerfor Java bytecode [41]. We have thus taken several Java programs in <strong>the</strong>Julia test suite and, using Julia, we have extracted <strong>the</strong> constraint systems th<strong>at</strong>characterize <strong>the</strong> loops in <strong>the</strong> program th<strong>at</strong> Julia cannot quickly resolve withsyntax-based heuristics. This extraction phase allowed us to measure <strong>the</strong> performance<strong>of</strong> <strong>the</strong> methods described in <strong>the</strong> present paper, factoring out <strong>the</strong> timespent by Julia in all <strong>the</strong> analyses (nullness, sharing, p<strong>at</strong>h-length, unfolding, . . . )th<strong>at</strong> allow to obtain such constraint systems.We first tested <strong>the</strong> performance (and correctness) <strong>of</strong> <strong>the</strong> new PPL implement<strong>at</strong>ionwith <strong>the</strong> implement<strong>at</strong>ion <strong>of</strong> <strong>the</strong> MS method, based on CLP(Q), previouslyused by Julia and with <strong>the</strong> implement<strong>at</strong>ion <strong>of</strong> PR, still based on CLP(Q), providedby <strong>the</strong> demo version <strong>of</strong> RankFinder. The reason we did this comparisonis th<strong>at</strong>, while we know th<strong>at</strong> <strong>the</strong> infinite precision implement<strong>at</strong>ion <strong>of</strong> <strong>the</strong> simplexalgorithm available in <strong>the</strong> PPL performs better than its direct competitors [6,Section 4, Table 3], 17 we know <strong>the</strong>re is much room for improvement: it couldhave been <strong>the</strong> case th<strong>at</strong> <strong>the</strong> constraint solver employed in modern CLP systemsmade our implement<strong>at</strong>ion useless. The result was quite s<strong>at</strong>isfactory: <strong>the</strong> PPLimplement<strong>at</strong>ion is one to two orders <strong>of</strong> magnitude faster over <strong>the</strong> consideredbenchmark suite.The benchmark programs are: CaffeineMark, from Pendragon S<strong>of</strong>twareCorpor<strong>at</strong>ion, measures <strong>the</strong> speed <strong>of</strong> Java; JLex is a lexical analyzer gener<strong>at</strong>ordeveloped by Elliot Berk and C. Scott Ananian; JavaCC is a parser gener<strong>at</strong>orfrom Sun Microsystems; Java_CUP is a parser gener<strong>at</strong>or developed by ScottHudson, Frank Flannery and C. Scott Ananian; Jess is a rule engine writtenby Ernest Friedman-Hill; Kitten is a didactic compiler for a simple imper<strong>at</strong>iveobject-oriented language written by Fausto Spoto; NQueens is a solver <strong>of</strong><strong>the</strong> n-queens problem which includes a library for binary decision diagrams;Raytracer is a ray-tracing program; Termin<strong>at</strong>ion is a JAR file containing all17 I.e., Cassowary (http://www.cs.washington.edu/research/constraints/cassowary/)and Wallaroo (http://sourceforge.net/projects/wallaroo/). While GLPK, <strong>the</strong> GNU <strong>Linear</strong>Programming Toolkit (http://www.gnu.org/s<strong>of</strong>tware/glpk/) includes a solver th<strong>at</strong> istermed “exact,” it still depends critically on flo<strong>at</strong>ing point comput<strong>at</strong>ions; moreover, it has notyet been made available in <strong>the</strong> public interface.30


Table 1: Benchmarks used in <strong>the</strong> experimental evalu<strong>at</strong>ionbenchmark loops n n σ n m m σ mCaffeineMark 151 [1,9] 6.0 1.3 [2,26] 17. 3.8JLex 467 [1,14] 7.2 2.5 [2,45] 17. 6.7JavaCC 136 [1,14] 8.6 4.1 [1,45] 22. 12.Java_CUP 29 [2,14] 8.3 4.3 [5,45] 23. 13.Jess 151 [1,9] 6.0 1.3 [2,26] 17. 3.8Kitten 1484 [1,15] 11. 3.6 [2,45] 29. 10.NQueens 359 [1,14] 6.3 3.6 [2,45] 17. 10.Raytracer 8 [2,9] 4.5 2.7 [5,26] 11. 7.8Termin<strong>at</strong>ion 121 [1,9] 4.2 3.5 [2,27] 12. 9.9Table 2: MS vs PR: CPU time in secondsterm. test one r. f. all r. f.benchmark MS PR MS PR MS PRCaffeineMark 0.42 0.26 0.43 0.25 0.31 0.34JLex 1.62 0.83 1.64 0.84 1.17 1.14JavaCC 0.86 0.43 0.87 0.45 0.67 0.65Java_CUP 0.35 0.14 0.35 0.14 0.29 0.22Jess 0.42 0.26 0.43 0.26 0.29 0.34Kitten 11.8 6.87 11.9 6.84 8.41 10.2NQueens 1.43 0.76 1.44 0.74 0.99 1.03Raytracer 0.04 0.03 0.04 0.03 0.03 0.03Termin<strong>at</strong>ion 0.25 0.15 0.25 0.15 0.18 0.21<strong>the</strong> programs <strong>of</strong> [41, Figure 16]. In Table 1 we report, for each benchmark, <strong>the</strong>number <strong>of</strong> loops for which termin<strong>at</strong>ion was investig<strong>at</strong>ed, <strong>the</strong> interval, mean andstandard devi<strong>at</strong>ions —with two significant figures— <strong>of</strong> <strong>the</strong> quantities n (number<strong>of</strong> variables) and m (number <strong>of</strong> constraints) th<strong>at</strong> characterize those loops.The results <strong>of</strong> <strong>the</strong> CPU-time comparison between <strong>the</strong> MS and PR methodsare reported in Table 2. Measurements took place on a GNU/Linux systemequipped with an Intel Core 2 Quad CPU Q9400 <strong>at</strong> 2.66 GHz and 8 Gbytes <strong>of</strong>main memory; a single core was used and <strong>the</strong> maximum resident set size over<strong>the</strong> entire set <strong>of</strong> tests was slightly above 53 Mbytes. From <strong>the</strong>se we can concludeth<strong>at</strong> <strong>the</strong> difference in performance between <strong>the</strong> two methods is r<strong>at</strong>her limited.The PR method is more efficient on <strong>the</strong> problem <strong>of</strong> semi-deciding termin<strong>at</strong>ion,with or without <strong>the</strong> comput<strong>at</strong>ion <strong>of</strong> a witness ranking function, while <strong>the</strong> MSmethod is superior on <strong>the</strong> problem <strong>of</strong> computing <strong>the</strong> space <strong>of</strong> all affine rankingfunctions.We also present, in Table 3, <strong>the</strong> precision results. For each benchmark,along with <strong>the</strong> total number <strong>of</strong> loops, we have <strong>the</strong> number <strong>of</strong> loops for whichtermin<strong>at</strong>ion is decided positively, ei<strong>the</strong>r with <strong>the</strong> MS or <strong>the</strong> PR method (column‘term’); <strong>the</strong> remaining loops are divided, using <strong>the</strong> MS method, between thoseth<strong>at</strong> admit a linear decreasing function (column ‘w/ d.f.’) and those who do31


Table 3: Precision results and applic<strong>at</strong>ion to conditional termin<strong>at</strong>ionbenchmark loops term w/ d.f. w/o d.f.CaffeineMark 151 149 0 2JLex 467 453 3 11JavaCC 136 120 4 12Java_CUP 29 27 0 2Jess 151 149 0 2Kitten 1484 1454 3 27NQueens 359 271 4 84Raytracer 8 6 0 2Termin<strong>at</strong>ion 121 119 0 2not (column ‘w/o d.f.’). It can be seen th<strong>at</strong> <strong>the</strong> percentage <strong>of</strong> loops for whichtermin<strong>at</strong>ion is decided positively ranges from 75% to 99%, depending on <strong>the</strong>benchmark. This means th<strong>at</strong> we are conducting <strong>the</strong> experimental evalu<strong>at</strong>ionwith a termin<strong>at</strong>ion analyzer, Julia, whose analysis algorithms —though certainlyimprovable— very <strong>of</strong>ten provide enough inform<strong>at</strong>ion for termin<strong>at</strong>ion analysis.This is crucial for <strong>the</strong> meaningfulness <strong>of</strong> <strong>the</strong> experimental evalu<strong>at</strong>ion presentedin this section.8. Conclusions<strong>Linear</strong> ranking functions play a crucial role in termin<strong>at</strong>ion analysis, as <strong>the</strong>termin<strong>at</strong>ion <strong>of</strong> many programs can be decided by <strong>the</strong> existence on one suchfunction. In this paper we have addressed <strong>the</strong> topic <strong>of</strong> <strong>the</strong> autom<strong>at</strong>ic syn<strong>the</strong>sis<strong>of</strong> linear ranking functions with <strong>the</strong> aim <strong>of</strong> clarifying its origins, thoroughlyexplaining <strong>the</strong> underlying <strong>the</strong>ory, and presenting new, efficient implement<strong>at</strong>ionsth<strong>at</strong> are being made available to <strong>the</strong> general public.In particular, we have introduced, in general terms independent from anyprogramming paradigm, <strong>the</strong> problem <strong>of</strong> autom<strong>at</strong>ic termin<strong>at</strong>ion analysis <strong>of</strong> individualloops —to which more general control flows can be reconducted— andits solution technique based on <strong>the</strong> syn<strong>the</strong>sis <strong>of</strong> ranking functions.We have <strong>the</strong>n presented and generalized a technique originally due to Sohnand Van Gelder, th<strong>at</strong> was virtually unknown outside <strong>the</strong> logic programming fielddespite its general applicability and its rel<strong>at</strong>ive completeness (given a linearconstraint system approxim<strong>at</strong>ing <strong>the</strong> behavior <strong>of</strong> a loop, if a linear rankingfunction exists for th<strong>at</strong> system, <strong>the</strong>n <strong>the</strong> method will find it). This method, dueto its ability to characterize <strong>the</strong> spaces <strong>of</strong> all <strong>the</strong> linear decreasing functions andall <strong>the</strong> linear bounded functions, is also immedi<strong>at</strong>ely applicable to conditionaltermin<strong>at</strong>ion analysis; this <strong>the</strong>me is an excellent candid<strong>at</strong>e for future work.We have also presented and, for <strong>the</strong> first time, fully justified, a more recenttechnique by Podelski and Rybalchenko. For this we also present an altern<strong>at</strong>iveformul<strong>at</strong>ion th<strong>at</strong> can lead to efficiency improvements.32


We have compared <strong>the</strong> two methods, first proving <strong>the</strong>ir equivalence —thus obtaining an independent confirm<strong>at</strong>ion on <strong>the</strong>ir correctness and rel<strong>at</strong>ivecompleteness— and <strong>the</strong>n studying <strong>the</strong>ir worst-case complexity.Finally, we have presented <strong>the</strong> implement<strong>at</strong>ion <strong>of</strong> all <strong>the</strong> techniques describedin <strong>the</strong> paper recently included in <strong>the</strong> Parma Polyhedra Library, along with anexperimental evalu<strong>at</strong>ion covering both <strong>the</strong> efficiency and <strong>the</strong> precision <strong>of</strong> <strong>the</strong>analysis.AcknowledgmentsWe would like to express our gr<strong>at</strong>itude to Amir Ben-Amram, David Merch<strong>at</strong>,Andrey Rybalchenko, Alessandro Zaccagnini and <strong>the</strong> anonymous reviewers for<strong>the</strong>ir useful comments and suggestions. The connection between <strong>the</strong> MS methodand conditional termin<strong>at</strong>ion analysis was indic<strong>at</strong>ed to us during a discussionwith Samir Genaim.References[1] J. C. Lagarias, The 3x+1 problem and its generaliz<strong>at</strong>ions, American M<strong>at</strong>hem<strong>at</strong>icalMonthly 92 (1985) 3–23.[2] E. W. Dijkstra, A Discipline <strong>of</strong> Programming, Prentice Hall, 1976.[3] R. Bagnara, F. Mesnard, A. Pescetti, E. Zaffanella, The autom<strong>at</strong>ic syn<strong>the</strong>sis<strong>of</strong> linear ranking functions: The complete unabridged version,arXiv:cs.PL/1004.0944v2, available from http://arxiv.org/. (2012).[4] P. Cousot, R. Cousot, ‘A la Floyd’ induction principles for proving inevitabilityproperties <strong>of</strong> programs, in: M. Niv<strong>at</strong>, J. C. Reynolds (Eds.),Algebraic Methods in Semantics, Cambridge University Press, 1985, pp.277–312.[5] R. W. Floyd, Assigning meanings to programs, in: J. T. Schwartz (Ed.),M<strong>at</strong>hem<strong>at</strong>ical Aspects <strong>of</strong> Computer Science, Vol. 19 <strong>of</strong> Proceedings <strong>of</strong> Symposiain Applied M<strong>at</strong>hem<strong>at</strong>ics, American M<strong>at</strong>hem<strong>at</strong>ical Society, ProvidenceRI, <strong>New</strong> York City, USA, 1967, pp. 19–32.[6] R. Bagnara, P. M. Hill, E. Zaffanella, The Parma Polyhedra Library: Towarda complete set <strong>of</strong> numerical abstractions for <strong>the</strong> analysis and verific<strong>at</strong>ion<strong>of</strong> hardware and s<strong>of</strong>tware systems, Science <strong>of</strong> Computer Programming72 (1–2) (2008) 3–21.[7] K. Sohn, A. Van Gelder, Termin<strong>at</strong>ion detection in logic programs usingargument sizes (extended abstract), in: Proceedings <strong>of</strong> <strong>the</strong> Tenth ACMSIGACT-SIGMOD-SIGART Symposium on Principles <strong>of</strong> D<strong>at</strong>abase Systems,ACM, Associ<strong>at</strong>ion for Computing Machinery, Denver, Colorado,United St<strong>at</strong>es, 1991, pp. 216–226.33


[8] A. Podelski, A. Rybalchenko, A complete method for <strong>the</strong> syn<strong>the</strong>sis <strong>of</strong> linearranking functions, in: B. Steffen, G. Levi (Eds.), Verific<strong>at</strong>ion, ModelChecking and Abstract Interpret<strong>at</strong>ion: Proceedings <strong>of</strong> <strong>the</strong> 5th Intern<strong>at</strong>ionalConference, VMCAI 2004, Vol. 2937 <strong>of</strong> Lecture Notes in Computer Science,Springer-Verlag, Berlin, Venice, Italy, 2004, pp. 239–251.[9] M. A. Colón, H. B. Sipma, Practical methods for proving program termin<strong>at</strong>ion,in: E. Brinksma, K. G. Larsen (Eds.), Computer Aided Verific<strong>at</strong>ion:Proceedings <strong>of</strong> <strong>the</strong> 14th Intern<strong>at</strong>ional Conference (CAV 2002), Vol. 2404 <strong>of</strong>Lecture Notes in Computer Science, Springer-Verlag, Berlin, Copenhagen,Denmark, 2002, pp. 442–454.[10] K. Sohn, Autom<strong>at</strong>ed termin<strong>at</strong>ion analysis for logic programs, Ph.D. <strong>the</strong>sis,University <strong>of</strong> California Santa Cruz, Santa Cruz, CA, USA (1993).[11] J. Fischer, Termin<strong>at</strong>ion analysis for Mercury using convex constraints, Honoursreport, Department <strong>of</strong> Computer Science and S<strong>of</strong>tware Engineering,The University <strong>of</strong> Melbourne, Melbourne, Australia (Aug. 2002).[12] C. Speirs, Z. Somogyi, H. Søndergaard, Termin<strong>at</strong>ion analysis for Mercury,in: P. Van Hentenryck (Ed.), St<strong>at</strong>ic Analysis: Proceedings <strong>of</strong> <strong>the</strong> 4th Intern<strong>at</strong>ionalSymposium, Vol. 1302 <strong>of</strong> Lecture Notes in Computer Science,Springer-Verlag, Berlin, Paris, France, 1997, pp. 157–171.[13] A. R. Bradley, Z. Manna, H. B. Sipma, <strong>Linear</strong> ranking with reachability,in: Computer Aided Verific<strong>at</strong>ion: Proceedings <strong>of</strong> <strong>the</strong> 15th Intern<strong>at</strong>ionalConference, Vol. 3576 <strong>of</strong> Lecture Notes in Computer Science, Springer-Verlag, Berlin, Edinburgh, Scotland, UK, 2005, pp. 491–504.[14] K. Verschaetse, D. De Schreye, Deriving termin<strong>at</strong>ion pro<strong>of</strong>s for logic programs,using abstract procedures, in: K. Furukawa (Ed.), Proceedings <strong>of</strong><strong>the</strong> 8th Intern<strong>at</strong>ional Conference on Logic Programming, The MIT Press,Paris, France, 1991, pp. 301–315.[15] M. Codish, C. Taboch, A semantic basis for <strong>the</strong> termin<strong>at</strong>ion analysis <strong>of</strong>logic programs, Journal <strong>of</strong> Logic Programming 41 (1) (1999) 103–123.[16] F. Mesnard, Inferring left-termin<strong>at</strong>ing classes <strong>of</strong> queries for constraint logicprograms by means <strong>of</strong> approxim<strong>at</strong>ions, in: M. J. Maher (Ed.), Logic Programming:Proceedings <strong>of</strong> <strong>the</strong> Joint Intern<strong>at</strong>ional Conference and Symposiumon Logic Programming, MIT Press Series in Logic Programming, TheMIT Press, Bonn, Germany, 1996, pp. 7–21.[17] F. Mesnard, A. Serebrenik, A polynomial-time decidable class <strong>of</strong> termin<strong>at</strong>ingbinary constraint logic programs, Tech. Rep. 05-11, Université de laRéunion (2005).[18] F. Mesnard, A. Serebrenik, Recurrence with affine level mappings is P-timedecidable for CLP(R), Theory and Practice <strong>of</strong> Logic Programming 8 (1)(2008) 111–119.34


[19] A. Podelski, A. Rybalchenko, Transition invariants, in: Logic in ComputerScience, Proceedings <strong>of</strong> <strong>the</strong> 19th IEEE Symposium, LICS 2004, IEEE ComputerSociety, Turku, Finland, 2004, pp. 32–41.[20] B. Cook, A. Podelski, A. Rybalchenko, Abstraction refinement for termin<strong>at</strong>ion,in: C. Hankin, I. Siveroni (Eds.), St<strong>at</strong>ic Analysis: Proceedings <strong>of</strong><strong>the</strong> 12th Intern<strong>at</strong>ional Symposium, Vol. 3672 <strong>of</strong> Lecture Notes in ComputerScience, Springer-Verlag, Berlin, London, UK, 2005, pp. 87–101.[21] M. T. Nguyen, D. De Schreye, Polynomial interpret<strong>at</strong>ions as a basis fortermin<strong>at</strong>ion analysis <strong>of</strong> logic programs, in: M. Gabbrielli, G. Gupta (Eds.),Proceedings <strong>of</strong> <strong>the</strong> 21st Intern<strong>at</strong>ional Conference on Logic Programming,no. 3668 in Lecture Notes in Computer Science, Springer-Verlag, Berlin,Sitges, Spain, 2005, pp. 311–325.[22] D. S. Lankford, A finite termin<strong>at</strong>ion algorithm, Internal memo, SouthwesternUniversity, Georgetown, TX, USA (1976).[23] H. Zantema, Termin<strong>at</strong>ion <strong>of</strong> term rewriting, Tech. Rep. UU-CS-2000-04, Department <strong>of</strong> Computer Science, Universiteit Utrecht, Utrecht, TheNe<strong>the</strong>rlands (2000).[24] P. Cousot, Proving program invariance and termin<strong>at</strong>ion by parametricabstraction, lagrangian relax<strong>at</strong>ion and semidefinite programming, in:R. Cousot (Ed.), Verific<strong>at</strong>ion, Model Checking and Abstract Interpret<strong>at</strong>ion:Proceedings <strong>of</strong> <strong>the</strong> 6th Intern<strong>at</strong>ional Conference (VMCAI 2005), Vol.3385 <strong>of</strong> Lecture Notes in Computer Science, Springer-Verlag, Berlin, Paris,France, 2005, pp. 1–24.[25] M. A. Colón, H. B. Sipma, Syn<strong>the</strong>sis <strong>of</strong> linear ranking functions, in: T. Margaria,W. Yi (Eds.), Tools and Algorithms for Construction and Analysis <strong>of</strong>Systems, 7th Intern<strong>at</strong>ional Conference, TACAS 2001, Vol. 2031 <strong>of</strong> LectureNotes in Computer Science, Springer-Verlag, Berlin, Genova, Italy, 2001,pp. 67–81.[26] J. Jaffar, M. J. Maher, Constraint logic programming: A survey, Journal<strong>of</strong> Logic Programming 19&20 (1994) 503–582.[27] M. R. Garey, D. S. Johnson, Computers and Intractability; A Guide to <strong>the</strong>Theory <strong>of</strong> NP-Completeness, W. H. Freeman & Co., <strong>New</strong> York, NY, 1990.[28] A. Schrijver, Theory <strong>of</strong> linear and integer programming, Wiley, Chichester,<strong>New</strong> York, 1986.[29] P. Cousot, N. Halbwachs, Autom<strong>at</strong>ic discovery <strong>of</strong> linear restraints amongvariables <strong>of</strong> a program, in: Conference Record <strong>of</strong> <strong>the</strong> Fifth Annual ACMSymposium on Principles <strong>of</strong> Programming Languages, ACM Press, Tucson,Arizona, 1978, pp. 84–96.35


[30] B. Cook, S. Gulwani, T. Lev-Ami, A. Rybalchenko, M. Sagiv, Provingconditional termin<strong>at</strong>ion, in: A. Gupta, S. Malik (Eds.), Computer AidedVerific<strong>at</strong>ion: Proceedings <strong>of</strong> <strong>the</strong> 20th Intern<strong>at</strong>ional Conference (CAV 2008),Vol. 5123 <strong>of</strong> Lecture Notes in Computer Science, Springer-Verlag, Berlin,Princeton, NJ, USA, 2008, pp. 328–340.[31] A. Rybalchenko, Temporal verific<strong>at</strong>ion with transition invariants, Ph.D.<strong>the</strong>sis, Universität des Saarlandes, Saarbrücken, Germany (2004).[32] A. Serebrenik, F. Mesnard, On termin<strong>at</strong>ion <strong>of</strong> binary CLP programs, in:S. Etalle (Ed.), Logic Based Program Syn<strong>the</strong>sis and Transform<strong>at</strong>ion: 14thIntern<strong>at</strong>ional Symposium, Revised Selected Papers, no. 3573 in LectureNotes in Computer Science, Springer-Verlag, Berlin, Verona, Italy, 2005,pp. 231–244.[33] C. H. Papadimitriou, K. Steiglitz, Combin<strong>at</strong>orial Optimiz<strong>at</strong>ion: Algorithmsand Complexity, Prentice Hall, Englewood Cliffs, NJ, USA, 1982.[34] D. A. Spielman, S.-H. Teng, Smoo<strong>the</strong>d analysis: Why <strong>the</strong> simplex algorithmusually takes polynomial time, Journal <strong>of</strong> <strong>the</strong> ACM 51 (2004) 385–463.[35] R. Bagnara, P. M. Hill, E. Zaffanella, Applic<strong>at</strong>ions <strong>of</strong> polyhedral comput<strong>at</strong>ionsto <strong>the</strong> analysis and verific<strong>at</strong>ion <strong>of</strong> hardware and s<strong>of</strong>tware systems,Theoretical Computer Science 410 (46) (2009) 4672–4691.[36] R. Bagnara, P. M. Hill, E. Ricci, E. Zaffanella, Precise widening oper<strong>at</strong>orsfor convex polyhedra, Science <strong>of</strong> Computer Programming 58 (1–2) (2005)28–56.[37] R. Bagnara, P. M. Hill, E. Zaffanella, Not necessarily closed convex polyhedraand <strong>the</strong> double description method, Formal Aspects <strong>of</strong> Computing17 (2) (2005) 222–257.[38] R. Bagnara, P. M. Hill, E. Zaffanella, Widening oper<strong>at</strong>ors for powersetdomains, S<strong>of</strong>tware Tools for Technology Transfer 8 (4/5) (2006) 449–466.[39] R. Bagnara, P. M. Hill, E. Zaffanella, Weakly-rel<strong>at</strong>ional shapes for numericabstractions: Improved algorithms and pro<strong>of</strong>s <strong>of</strong> correctness, FormalMethods in System Design 35 (3) (2009) 279–323.[40] R. Bagnara, P. M. Hill, E. Zaffanella, Exact join detection for convex polyhedraand o<strong>the</strong>r numerical abstractions, Comput<strong>at</strong>ional Geometry: Theoryand Applic<strong>at</strong>ions 43 (5) (2010) 453–473.[41] F. Spoto, F. Mesnard, É. Payet, A termin<strong>at</strong>ion analyzer for Java bytecodebased on p<strong>at</strong>h-length, ACM Transactions on Programming Languages andSystems 32 (3).36

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!