• Keine Ergebnisse gefunden

InferringLowerBoundsforRuntimeComplexity Aachen

N/A
N/A
Protected

Academic year: 2021

Aktie "InferringLowerBoundsforRuntimeComplexity Aachen"

Copied!
34
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Aachen

Department of Computer Science

Technical Report

Inferring Lower Bounds for Runtime Complexity

Florian Frohn, J¨ urgen Giesl, Jera Hensel, Cornelius Ascher- mann, Thomas Str¨ oder

ISSN 0935–3232 · Aachener Informatik-Berichte · AIB-2015-15 RWTH Aachen · Department of Computer Science · April 2015 (revised version)

(2)

http://aib.informatik.rwth-aachen.de/

(3)

Florian Frohn, Jürgen Giesl, Jera Hensel, Cornelius Aschermann, and Thomas Ströder

LuFG Informatik 2, RWTH Aachen University, Germany

{florian.frohn,giesl,hensel,cornelius,stroeder}@informatik.rwth-aachen.de

Abstract

We present the first approach to deduce lower bounds for innermost runtime complexity of term rewrite systems (TRSs) automatically. Inferring lower runtime bounds is useful to detect bugs and to complement existing techniques that compute upper complexity bounds. The key idea of our approach is to generate suitable families of rewrite sequences of a TRS and to find a relation between the length of such a rewrite sequence and the size of the first term in the sequence. We implemented our approach in the toolAProVE and evaluated it by extensive experiments.

1998 ACM Subject Classification F.1.3 - Complexity Measures and Classes, F.4.2 - Grammars and Other Rewriting Systems, I.2.3 Deduction and Theorem Proving

Keywords and phrases Term Rewriting, Runtime Complexity, Lower Bounds, Induction

1 Introduction

There exist numerous methods to inferupper bounds for the runtime complexity of TRSs [3, 11, 13, 16, 20]. We present the first automatic technique to inferlower bounds for the innermost1runtime complexity of TRSs. Runtime complexity[11] refers to the “worst” cases in terms of evaluation length and our goal is to find lower bounds for these cases. While upper complexity bounds help to prove the absence of bugs that worsen the performance of programs, lower bounds can be used to find such bugs. Moreover, in combination with methods to deduce upper bounds, our approach can prove tight complexity results. In addition to asymptoticlower bounds, in many cases our technique can even computeconcrete bounds.

As an example, consider the following TRS Rqs for quicksort. The auxiliary function low(x,xs) returns those elements from the listxs that are smaller thanx(andhigh works analogously). To ease readability, we use infix notation for the function symbols≤and++.

IExample 1 (TRSRqs for Quicksort).

qs(nil) nil (1)

qs(cons(x,xs)) qs(low(x,xs))++cons(x,qs(high(x,xs))) (2) low(x,nil) nil

low(x,cons(y, ys)) ifLow(xy, x,cons(y, ys))

ifLow(true, x,cons(y, ys)) low(x, ys) zerox true

ifLow(false, x,cons(y, ys)) cons(y,low(x, ys)) succ(x)zero false

high(x,nil) nil succ(x)succ(y) xy

high(x,cons(y, ys)) ifHigh(xy, x,cons(y, ys)) nil++ys ys (3) ifHigh(true, x,cons(y, ys)) cons(y,high(x, ys)) cons(x, xs)++ys cons(x, xs++ys) ifHigh(false, x,cons(y, ys)) high(x, ys)

Supported by the DFG grant GI 274/6-1.

1 We considerinnermostrewriting, since TRSs resulting from the translation of programs usually have to be evaluated with an innermost strategy (e.g., [9, 17]). Obviously, lower bounds for innermost reductions are also lower bounds for full reductions (i.e., our approach can also be used for full rewriting).

(4)

For anyn∈N, letγList(n) be the term

ntimes

z }| {

cons(zero, . . . ,cons(zero,nil). . .), i.e., the list of lengthnwhere all elements have the valuezero(we also use the notation “consn(zero,nil)”). To find lower bounds, we automatically generaterewrite lemmasthat describe families of rewrite sequences. For example, our technique infers the following rewrite lemma automatically.

qs(γList(n)) →i 3n

2+2n+1

γList(n) (4)

This rewrite lemma means that for eachn∈N, there is an innermost rewrite sequence of length 3n2+ 2n+ 1 that reduces qs(consn(zero,nil)) to consn(zero,nil). From this rewrite lemma, our technique then concludes that the innermost runtime ofRqsis at least quadratic.

While most methods to infer upper bounds are adaptions of termination techniques, the approach in this paper is related to our technique to prove non-termination of TRSs [7].

Both techniques generate “meta-rules” representing infinitely many rewrite sequences. How- ever, therewrite lemmas in the current paper are more general than the meta-rules in [7], as they can be parameterized byseveral variablesn1, . . . , nmof typeN.

In Sect. 2 we show how to automatically speculate conjectures that may result in suitable rewrite lemmas. Sect. 3 explains how these conjectures can be verified automatically by induction. From these induction proofs, one can deduce information on the lengths of the rewrite sequences represented by a rewrite lemma, cf. Sect. 4. Thus, the use of induction to infer lower runtime bounds represents a novel application for automated inductive theorem proving. This complements our earlier work on using inductive theorem proving for termination analysis [8]. Finally, Sect. 5 shows how rewrite lemmas can be used to infer lower bounds for the innermost runtime complexity of a TRS.

Sect. 6 discusses an improvement of our approach by pre-processing the TRS before the analysis and Sect. 7 extends our approach to handle rewrite lemmas with arbitrary unknown right-hand sides. We implemented our technique in the toolAProVE [10] and demonstrate its power by an extensive experimental evaluation in Sect. 8. All proofs can be found in the appendix.

2 Speculating Conjectures

We now show how to speculate conjectures (whose validity must be proved afterwards in Sect.

3). See, e.g., [5] for the basics of rewriting, where we only consider finite TRSs. T(Σ,V) is the set of all terms over a (finite) signature Σ and a set of variablesV andT(Σ) =T(Σ,∅) is the set of ground terms. Thearityof a symbolf ∈Σ is denoted by arΣ(f). As usual, thedefined symbolsof a TRSRare Σdef(R) ={root(`)|`r∈ R }and theconstructorsΣcon(R) are all other function symbols inR. Thus, Σdef(Rqs) ={qs,low,ifLow,high,ifHigh,++,≤}and Σcon(Rqs) ={nil,cons,zero,succ,true,false}.

Our approach is based on rewrite lemmas containinggenerator functionssuch asγList

for types likeList. Hence, in the first step of our approach we compute suitable types for the TRSRto be analyzed. While ordinary TRSs are defined over untyped signatures Σ, Def. 2 shows how to extend such signatures by (monomorphic) types (see, e.g., [8, 13, 21]).

IDefinition 2(Typing). Let Σ be an (untyped) signature. A many-sorted signature Σ0 is a typed variant of Σ if it contains the same function symbols as Σ, with the same arities. So f ∈Σ with arΣ(f) =kifff ∈Σ0 wheref’s type has the formτ1×. . .×τkτ. Similarly, a typed variantV0 of the set of variables V contains the same variables asV, but now every variable has a typeτ. We always assume that for every typeτ, V0 contains infinitely many

(5)

variables of typeτ. Given Σ0 andV0,t∈ T(Σ,V) is awell-typed term of typeτ iff t∈ V0 is a variable of typeτ or

t =f(t1, . . . , tk) withk ≥0, where each ti is a well-typed term of type τi, and where f ∈Σ0 has the typeτ1×. . .×τkτ.

We only permit typed variants Σ0where there exist well-typed ground terms of typesτ1, . . . , τk over Σ0, whenever somef ∈Σ0 has type τ1×. . .×τkτ.2

A TRSRover Σ andV iswell typed w.r.t. Σ0 andV0 iff for all`r∈ R, we have that

`andrare well typed and that they have the same type.3

For any TRS R, one can use a standard type inference algorithm to compute a typed variant Σ0 such thatRis well typed. Of course, a trivial solution is to use a many-sorted signature with just one sort (then every term and every TRS are trivially well typed). But to make our approach more powerful, it is advantageous to use the most general typed variant whereRis well typed. Here, the set of terms is decomposed into as many types as possible.

Then fewer terms are well typed and more useful rewrite lemmas can be generated.

To make Rqs from Ex. 1 well typed, we obtain a typed variant of its signature with the typesNats,Bool, andList. Here, the function symbols have the following types:

nil:List qs:ListList

cons:Nats×ListList ++:List×ListList

zero:Nats ≤:Nats×NatsBool

succ:NatsNats low,high:Nats×ListList

true,false:Bool ifLow,ifHigh:Bool×Nats×ListList A typeτdepends on a typeτ0 (denotedτ wdepτ0) iffτ =τ0 or if there is ac∈Σ0con(R) of typeτ1×. . .×τkτ whereτiwdepτ0 for some 1≤ik. To ease the presentation, we do not allow mutually recursive types (i.e., ifτ wdepτ0andτ0wdepτ, thenτ0 =τ). To speculate conjectures, we now introduce generator functionsγτ. For anyn∈N,γτ(n) is a term from T(Σ0con(R)) where a recursive constructor of typeτ is nestedntimes. A constructorc : τ1× . . .×τkτ isrecursiveiffτi=τ for some 1≤ik. So for the typeNats above, we have γNats(0) =zeroandγNats(n+ 1) =succ(γNats(n)). If a constructor has a non-recursive argu- ment of typeτ0, then γτ instantiates this argument byγτ0(0). So forList, we getγList(0) = nilandγList(n+ 1) =cons(zero, γList(n)). If a constructor has several recursive arguments, then several generator functions are possible. So for a type Tree with the constructors leaf:Treeandnode:Tree×TreeTree, we haveγTree(0) =leaf, but eitherγTree(n+1) = node(γTree(n),leaf) orγTree(n+ 1) =node(leaf, γTree(n)). Similarly, if a type has several non-recursive or recursive constructors, then several different generator functions can be constructed by considering all combinations of non-recursive and recursive constructors.

To ease the presentation, we only consider generator functions forsimply structuredtypes τ. Such types have exactly two constructors c, d∈Σ0con(R), wherecis not recursive, dhas exactly one argument of typeτ, and each argument typeτ0 6=τ ofcordis simply structured, too. The presented approach can easily be extended to more complex types by applying suitable heuristics to choose one of the possible generator functions.

IDefinition 3 (Generator Functions and Equations). LetRbe a TRS that is well typed w.r.t.

Σ0 andV0. We extend the set of types by a fresh typeN. For every typeτ6=N, letγτ be a freshgenerator function symbolof typeN→τ. The setGR consists of the followinggenerator

2 This is not a restriction, as one can simply add new constants to Σ and Σ0.

3 W.l.o.g., here one may rename the variables in every rule. Then it is not a problem if the variablexis used with typeτ1in one rule and with typeτ2in another rule.

(6)

equationsfor every simply structured typeτ with the constructorsc : τ1×. . .×τkτ and d : ρ1×. . .×ρbτ, whereρj =τ. We writeG instead ofGRifRis clear from the context.

γτ(0) = c(γτ1(0), . . . , γτk(0))

γτ(n+ 1) = d(γρ1(0), . . . , γρj−1(0), γτ(n), γρj+1(0), . . . , γρb(0))

We extendwdep to Σdef(R) by definingf wdephifff =hor if there is a rulef(. . .)→r and a symbolg intwithgwdeph. When speculating conjectures, we take the dependencies between defined symbols into account. Iff wdepg andg6wdepf, then we first generate a rewrite lemma forg. This lemma can be used when generating a lemma forf afterwards.

Forf ∈Σ0def(R) of typeτ1×. . .×τkτwith simply structured typesτ1, . . . , τk, our goal is to speculate a conjecture of the formfτ1(s1), . . . , γτk(sk)) →i t, where the s1, . . . , sk

are polynomials over variablesn1, . . . , nm of typeN. Moreover, t is a term built from Σ, arithmetic expressions, generator functions, and n1, . . . , nm. As usual, a rewrite step is innermost (denoted si R twhere we omit the index Rif it is clear from the context) if the reduced subterm ofsdoes not have redexes as proper subterms. From the speculated conjecture, we afterwards infer a rewrite lemma fτ1(s1), . . . , γτk(sk)) →i rt(n1,...,nm) t, wherert :Nm→Ndescribes theruntimeof the lemma. To speculate a conjecture, we first generate sample conjectures that describe the effect of applying f to specific arguments.

To this end, we narrowfτ1(n1), . . . , γτk(nk)) wheren1, . . . , nk∈ V using the rules of the TRS and the lemmas we have proven so far, taking also the generator equations and integer arithmetic into account.

For any proven rewrite lemmasi rt(...)t, let the setL contain the rulest. Moreover, letAbe the infinite set of all valid equalities in the theory ofNwith addition and multiplication.

Thensnarrowstot(“s (R∪L)/(G∪A)t” or just “s t” ifR,L,Gare clear from the context) iff there exist a terms0, a substitutionσthat maps variables of typeNto arithmetic expressions, a positionπ, and a variable-renamed rule`r∈ R ∪ Lsuch thatG∪As0σ,s0|πσ=`σ, ands0[r]πσ=t. Although checkingsσG∪As0σ(i.e.,G ∪ A |==s0σ) is undecidable in general, the required narrowing can usually be performed automatically using SMT solvers.

IExample 4 (Narrowing). In Ex. 1 we haveqswdeplowandqswdephigh. If the lemmas low(γNats(0), γList(n)) →i 3n+1 γList(0) (5) high(γNats(0), γList(n)) →i 3n+1 γList(n) (6) were already proved, then the following narrowing tree can be generated to find sample conjectures forqs. The arrows are annotated with the rules and the substitutions used for variables of typeN. To save space, some arrows correspond toseveral narrowing steps.

qs(γList(n))

nil Rule (1)

[n/0]

cons(zero,qs(γList(n0)))

cons(zero,nil) Rule (1)

[n0/0]

cons(zero,cons(zero,qs(γList(n00))))

cons(zero,cons(zero,nil)) Rule (1)

[n00/0]

. . .

Rule (2), Lemmas (5) and (6), Rules (1) and (3) [n0/n00+ 1]

Rule (2), Rewrite Lemmas (5) and (6), Rules (1) and (3) [n/n0+ 1]

(7)

The goal is to get representative rewrite sequences, but not to cover all reductions. So we stop constructing the tree after some steps and choose suitable narrowings heuristically.

After constructing a narrowing tree for f, we collect sample points (t, σ, d). Here, t results from a -normal formq reached in a path of the tree by normalizingqw.r.t. the generator equations G applied from right to left. So terms fromT(Σ,V) are rewritten to generator symbols with arithmetic expressions as arguments. Moreover,σis the substitution for variables of typeN, anddis the number of applications of recursive f-rules on the path (the recursion depth). A rulef(. . .)→risrecursiveiffrcontains a symbolg withgwdepf.

IExample 5 (Sample Points). In Ex. 4, we obtain the following set of sample points:4 S = {(γList(0),[n/0],0), (γList(1),[n/1],1), (γList(2),[n/2],2)} (7) The sequence fromqs(γList(n)) tonildoes not use recursive qs-rules. So its recursion depth is 0 and the -normal form nil rewrites toγList(0) when applying G from right to left.

The sequence fromqs(γList(n)) to cons(zero,nil) (resp. cons(zero,cons(zero,nil))) uses the recursiveqs-rule (2) once (resp. twice), i.e., it has recursion depth 1 (resp. 2). Moreover, these -normal forms rewrite toγList(1) (resp.γList(2)) when usingG from right to left.

A sample point (t, σ, d) for a narrowing tree with the roots=f(. . .) represents thesample conjecture i t, which stands for a reduction withdapplications of recursivef-rules. So fors=qs(γList(n)), the sample points in (7) represent the sample conjecturesqs(γList(0))→i γList(0),qs(γList(1))→i γList(1),qs(γList(2))→i γList(2). Now the goal is to speculate a general conjecture from these sample conjectures (whose validity must be proved afterwards).

In general, we search for a maximal subset of sample conjectures that are suitable for generalization. More precisely, if s is the root of the narrowing tree, then we take a maximal subset Smax of sample points such that for all (t, σ, d),(t0, σ0, d0)∈ Smax, the sample conjecturesi tand0i t0 are identical up to the occurring natural numbers and the variable names. For instance, qs(γList(0)) →i γList(0), qs(γList(1)) →i γList(1), andqs(γList(2))→i γList(2) are indeed identical up to the numbers in these sample con- jectures. To obtain a general conjecture, we replace all numbers in the sample conjec- tures by polynomials. So in our example, we want to speculate a conjecture of the form qs(γList(polleft)) →i γList(polright). Here, polleft and polright are polynomials in one vari- ablen(theinduction variable of the conjecture) that stands for the recursion depth. This facilitates a proof of the resulting conjecture by induction onn.

So in general, in any sample conjecture i t that correspond to a sample point (t, σ, d)∈Smax, we replace the natural numbers in andtby polynomials. For any termq,

let pos(q) be the set of its positions and Πq

N={π∈pos(q)|q|π∈N}. Then for eachπ∈Π

N

(resp. π∈ΠtN) with (t, σ, d)∈Smax, we search for a polynomialpolleftπ (resp.polrightπ ). To this end, for every sample point (t, σ, d)∈Smax, we generate the constraints

“polleftπ (d) = sσ|π” for everyπ∈ΠN and “polrightπ (d) = t|π” for everyπ∈ΠtN. (8) Here, polleftπ andpolrightπ are polynomials with abstract coefficients. So if one searches for polynomials of degreee, then the polynomials have the formc0+c1·n+c2·n2+. . .+ce·ne and the constraints in (8) are linear diophantine equations over the unknown coefficients ci∈N.5 These equations can easily be solved automatically. Finally, the desired generalized

4 We always simplify arithmetic expressions in terms and substitutions, e.g., the substitution [n/0 + 1] in the second sample point is simplified to [n/1].

5 Note that in the constraints (8),nis instantiated by an actual numberd. Thus, ifpolleftπ =c0+c1·n+ c2·n2+. . .+ce·ne, thenpolleftπ (d) is alinear polynomial over the unknownsc0, . . . , ce.

(8)

speculated conjecture is obtained from i t by replacing sσ|π with polleftπ for every π∈ΠN and by replacingt|π withpolrightπ for every π∈ΠtN.

IExample 6 (Speculating Conjectures). In Ex. 4, we narroweds=qs(γList(n)) andSmax is the setS in (7). For each (t, σ, d)∈Smax, we have Π

N ={1.1}and Πt

N={1}. So from the sample conjectureqs(γList(0))→i γList(0), where the recursion depth isd=0, we obtain the constraintspolleft1.1(d)=polleft1.1(0)=qs(γList(0))|1.1=0 andpolright1 (d)=polright1 (0)=γList(0)|1= 0. Similarly, from the two other sample conjectures we getpolleft1.1(1) =polright1 (1) = 1 and polleft1.1(2)=polright1 (2)=2. When usingpolleft1.1=c0+c1·n+c2·n2andpolright1 =d0+d1·n+d2·n2 with the abstract coefficientsc0, . . . , c2, d0, . . . , d2, the solutionc0=c2=d0=d2=0,c1=d1=1 (i.e.,polleft1.1=nandpolright1 =n) is easily found automatically. So the resulting conjecture is

qs(γList(polleft1.1))→i γList(polright1 ), i.e.,qs(γList(n))→i γList(n).

If Smax contains sample points with e different recursion depths, then we generate polynomials of at most degreee−1 satisfying the constraints (8) (these polynomials are determined uniquely). Ex. 7 shows how to speculate conjectures withseveral variables.

IExample 7(Conjecture With Several Variables). The following TRS combines half and plus.

hp(zero, y) → y hp(succ(succ(x)), y) → succ(hp(x, y))

Narrowing s = hp(γNats(n1), γNats(n2)) yields the sample points (γNats(n2),[n1/0],0), (γNats(n2+ 1),[n1/2],1), (γNats(n2+ 2),[n1/4],2), and (γNats(n2+ 3),[n1/6],3). For the last three sample points (t, σ, d), the only number inis at position 1.1 and the polynomial polleft1.1 = 2·nsatisfies the constraintpolleft1.1(d) =sσ|1.1. Moreover, the only number intis at position 1.2 and the polynomialpolright1.2 =nsatisfiespolright1.2 =t|1.2. Thus, we speculate the conjecturehp(γNats(2·n), γNats(n2))→i γNats(n2+n) with the induction variablen.

3 Proving Rewrite Lemmas

If the proof of a speculated conjecture succeeds, then we have found arewrite lemma.

IDefinition 8(Rewrite Lemmas). LetRbe a TRS that is well typed w.r.t. Σ0 andV0. For any termq, letqG/Abeq’s normal form w.r.t.GR, where the generator equations are applied from left to right andA-equivalent (sub)terms are considered to be equal. Moreover, let si tbe a conjecture withV(s) ={n1, . . . , nm} 6=∅, wheren= (n1, . . . , nm) are pairwise different variables of typeN,sis well typed, root(s)∈Σdef(R), andshas no defined symbol from Σdef(R) below the root. Letrt :Nm→N. ThensrtR(n)t is arewrite lemmaforRiff sσ↓G/Ai rtR(nσ) G/Afor allσ:V(s)→N, i.e.,G/Acan be reduced toG/Ain exact- lyrt(n1σ, . . . , nmσ) innermostR-steps. We omit the indexRif it is clear from the context.

So the conjectureqs(γList(n))→i γList(n) gives rise to a rewrite lemma, sinceσ(n) =b∈ Nimpliesqs(γList(b))↓G/A=qs(consb(zero,nil))→i 3b

2+2b+1

consb(zero,nil) =γList(b)↓G/A. To prove rewrite lemmas, essentially we use rewriting with→i (R∪L)/(G∪A).6 However, this would allow us to prove lemmas that do not correspond toinnermost rewriting withR, ifR contains rules with overlapping left-hand sides. ConsiderR={g(zero)→zero,f(g(x))→ zero}. We have f(g(γNats(n))) →i (R∪L)/(G∪A) zero, but for the instantiation [n/0], this would not be an innermost reduction. To avoid this, we use the following relation *i R

i (R∪L)/(G∪A): We haves*i R tiff there exist a terms0, a substitutionσ, a positionπ, and a

6 Here, we definei (R∪L)/(G∪A) to be the relationG∪A (i R∪ →L)◦ ≡G∪A. An adaption of our approach to runtime complexity of full rewriting is obtained by considering(R∪L)/(G∪A) instead.

(9)

rule`r∈ R ∪ Lsuch thatsG∪As0,s0|π=ands0[rσ]πG∪At. Moreover, if`r∈ R, then there must not be any proper non-variable subtermqof`σ, a (variable-renamed) rule

`0r0∈ R, and a substitutionσ0 such that `0σ0G∪A0. Nowf(g(γNats(n)))6*i Rzero, because the subtermg(γNats(n)) unifies with the left-hand sideg(zero) modulo G ∪ A.

When proving a conjecture si t by induction, in the step case we try to reduce s[n/n+ 1] tot[n/n+ 1], where one may use the rule IH: stas induction hypothesis. Here, the variables in IH may not be instantiated. The reason for not allowing instantiations of the non-induction variables fromV(s)\ {n} is that such induction proofs are particularly suitable for inferring runtimes of rewrite lemmas, cf. Sect. 4.

Thus, for any rule IH: `r, lets7→IHtiff there exist a terms0 and a positionπsuch thatsG∪As0,s0|π =`ands0[r]πG∪A t. Let*i (R,IH)=*i R∪ 7→IH. Moreover,*i R(resp.

*i (R,IH)) denotes the transitive-reflexive closure of*i R (resp.*i (R,IH)), where in addition s*i Rs0 ands*i (R,IH)s0 also hold ifsG∪As0. Thm. 9 shows which rewrite sequences are needed to prove a conjecturesi tby induction on its induction variable n.

ITheorem 9(Proving Rewrite Lemmas). LetR,s,tbe as in Def. 8,n∈ V(s) ={n1, . . . , nm}, andn= (n1, . . . , nm). Ifs[n/0] *i R t[n/0]ands[n/n+ 1] *i (R,IH) t[n/n+ 1], whereIHis the rulest, then there is an rt :Nm→N such thatsi rt(n)t is a rewrite lemma for R.

IExample 10(Proof of Rewrite Lemma). Assume that we have already proved the rewrite lemmas (5) and (6). To prove the conjectureqs(γList(n))→i γList(n), in the induction base we showqs(γList(0))*i RγList(0) and in the induction step, we obtainqs(γList(n+ 1))*i R nil++cons(zero,qs(γList(n)))7→IHnil++cons(zero, γList(n))*i RγList(n+ 1). Thus, there is a rewrite lemmaqs(γList(n))→i rt(n)γList(n). Sect. 4 will clarify how to find the functionrt.

4 Inferring Bounds for Rewrite Lemmas

Now we show how to infer the functionrt for a rewrite lemmasi rt(n)tfrom its proof. Ifnn was the induction variable and the induction hypothesis was appliedih times in the induction step, then we get the following recurrence equations forrt whereenisnwithout the variablen:

rt(n[n/0]) =ib(en) and rt(n[n/n+ 1]) =ih·rt(n) +is(n) (9) Here,ib(en) is the length of the reductions[n/0]G/Ai R t[n/0]G/A, which must exist due to the induction base. The addendis(n) is the length ofs[n/n+ 1]↓G/Ai R t[n/n+ 1]↓G/A, but without those subsequences that are covered by the induction hypothesis IH. Since the non-induction variables were not instantiated in IH,rt(n) is the runtime for each application of IH. To computeibandis, for each previous rewrite lemmas0i rt

0(n0)

t0 that was used in the proof of si rt(n)t, we assume thatrt0 is known. Thus,rt0 can be used to infer the number of rewrite steps represented by that previous lemma. To avoid treating rules and rewrite lemmas separately, in Def. 11 we regard each rulest∈ Ras a rewrite lemmasi 1t.

IDefinition 11 (ih,ib,is). Let si rt(n)t be a rewrite lemma with an induction proof as in Thm. 9. More precisely, let u1 *i R . . . *i R ub+1 be the rewrite sequence s[n/0] *i R t[n/0] for the induction base and let v1 *i (R,IH) . . .*i (R,IH) vk+1 be the rewrite sequence s[n/n+ 1] *i (R,IH) t[n/n+ 1] for the induction step, where IH: stis appliedih times.

Forj ∈ {1, . . . , b}, let`ji rtj(yj)rj andσj be the rewrite lemma and substitution used to reduceuj touj+1. Similarly forj∈ {1, . . . , k}, let pjrt0j(zj)qj andθj be the lemma and substitution used to reducevj tovj+1. Then we define:

(10)

ib(en) = X

j∈{1,...,b} rtj(yjσj) and is(n) = X

j∈{1,...,k}, pj→qj6=IH rt0j(zjθj) By solving the recurrence equations (9), we can now computert explicitly.

ITheorem 12(Explicit Runtime of Rewrite Lemmas). Letsi rt(n)tbe a rewrite lemma, where ih,ib, andis are as in Def. 11. Then we obtainrt(n) =ihn·ib(en) + Pn−1

i=0 ihn−1−i·is(n[n/i]).

IExample 13 (Computingrt). Reconsiderqs(γList(n))→i rt(n) γList(n) from Ex. 10. The proof of the induction base isqs(γList(0))≡G qs(nil)→i Rqs nil≡G γList(0). Hence,ib =rt1= 1.

The proof of the induction step is as follows. Here, we use that the runtime of both previously proved lemmas (5) and (6) is 3n+ 1. Note that the non-overlap condition required by the relation*i Rqs is clearly satisfied in all steps with→i Rqs in the proof.

qs(γList(n+ 1)) ≡G qs(cons(γNats(0), γList(n))) →i Rqs rt01= 1

qs(low(γNats(0), γList(n))) ++ cons(γNats(0),qs(high(. . .))) →L rt02(n) = 3n+ 1 qs(γList(0)) ++ cons(γNats(0),qs(high(γNats(0), γList(n)))) →L rt03(n) = 3n+ 1

qs(γList(0)) ++ cons(γNats(0),qs(γList(n))) ≡G

qs(nil) ++ cons(zero,qs(γList(n))) →i Rqs rt04= 1 nil ++ cons(zero,qs(γList(n))) 7→IH rt05(n) =rt(n)

nil ++ cons(zero, γList(n)) →i Rqs rt06= 1 cons(zero, γList(n)) ≡G γList(n+ 1)

Hence,is(n) = P

j∈{1,...,6}, pj→qj6=IH rt0j(zjθj) = rt01+rt02(n) +rt03(n) +rt04+rt06

= 1 + (3n+ 1) + (3n+ 1) + 1 + 1 = 6n+ 5.

In our example, we haveih= 1. So Thm. 12 impliesrt(n) =ib+Pn−1

i=0 is(i) = 1+Pn−1 i=0(6i+5)

= 3n2+ 2n+ 1. Thus, we get the rewrite lemma (4): qs(γList(n))→i 3n

2+2n+1

γList(n).

To compute asymptotic bounds for the complexity of a TRS afterwards, we have to infer asymptotic bounds for the runtime of rewrite lemmas. Based on Thm. 12, such bounds can be automatically obtained from the induction proofs of the lemmas. To ease the formulation of bounds forrt :Nm→N, we define the unary functionrtN:N→NasrtN(n) =rt(n, . . . , n).

If the induction hypothesis was not used in the proof of a rewrite lemma (i.e.,ih = 0), then we havert(n[n/0]) =ib(en) andrt(n[n/n+ 1]) =is(n). Thus, ifib andis are polynomials of degreedib anddis, respectively, then we obtainrtN(n)∈Ω(nmax{dib,dis}).

Ifih= 1, then Thm. 12 impliesrt(n) =ib(n) +e Pn−1

i=0 is(n[n/i]). Again, letib andis be polynomials of degreedibanddis, respectively. Thenis(n) =t0+t1n+t2n2+. . .+tdisndis, where thetj are polynomials of degree at mostdisj containing variables fromen. Hence,rt(n) =

ib(en) +

n−1

P

i=0

(t0+t1i+t2i2+. . .+tdisidis) =ib(en) +t0·

n−1

P

i=0

i0+t1·

n−1

P

i=0

i1+t2·

n−1

P

i=0

i2+. . .+tdis·

n−1

P

i=0

idis.

By Faulhaber’s formula [14], for anye∈N,Pn−1

i=0 ieis a polynomial over the variablenof de- greee+ 1. For example ife= 1, thenPn−1

i=0 i1= n·(n−1)2 has degree 2. By taking also the de- greedib ofib into account,rt has degree max{dib, dis+ 1}, i.e.,rtN(n)∈Ω(nmax{dib,dis+1}).

Finally we consider the case where the induction hypothesis was used several times, i.e., ih >1. By construction we always have is(n) ≥1 (since the induction step cannot only consist of applying the induction hypothesis). Thus, Thm. 12 impliesrt(n)≥Pn−1

i=0 ihn−1−i= Pn−1

j=0ihj= ihih−1n−1. SortN(n)∈Ω(ihn), i.e., the runtime of the rewrite lemma is exponential.

ITheorem 14(Asymptotic Runtime of Rewrite Lemmas). Letsi rt(n)t be a rewrite lemma withih,ib, and is as in Def. 11. Moreover, letib andis be polynomials of degreedib anddis.

(11)

If ih = 0, thenrtN(n)∈Ω(nmax{dib,dis}).

If ih = 1, thenrtN(n)∈Ω(nmax{dib,dis+1}).

If ih >1, thenrtN(n)∈Ω(ihn).

IExample 15(Exponential Runtime).Consider the TRSRexpwith the rulesf(succ(x),succ(x))

→f(f(x, x),f(x, x)) andf(zero,zero)→zero. Our approach speculates and proves the rewrite lemmaf(γNats(n), γNats(n))→i rt(n)zero. For the induction base, we havef(γNats(0), γNats(0))

Gf(zero,zero)→i Rexpzeroand thusib= 1. The induction step is proved as follows:

f(γNats(n+ 1), γNats(n+ 1)) G f(succ(γNats(n)),succ(γNats(n))) i Rexp rt01= 1 f(f(γNats(n), γNats(n)),f(γNats(n), γNats(n))) 7→2IH

f(zero,zero) i Rexp rt04= 1 zero

Thus, ih = 2 and is(n) is the constant 2 for all n ∈ N. Hence, by Thm. 14 we have rt(n)∈Ω(2n). Indeed, Thm. 12 impliesrt(n) = 2n+Pn−1

i=0 2n−1−i·2 = 2n+1+ 2n−2.

5 Inferring Bounds for TRSs

We now use rewrite lemmas to infer lower bounds for the innermost runtime complexityircR

of a TRSR. To define ircR, thederivation heightof a termtw.r.t. a relation→is the length of the longest→-sequence starting witht, i.e., dh(t,→) = sup{m| ∃t0∈ T(Σ,V), t→mt0}, cf. [12]. Here, for anyM ⊆N∪ {ω}, supM is the least upper bound ofM and sup∅= 0.

Since we only regard finite TRSs, dh(t,→i R) =ω ifftstarts an infinite sequence of→i R-steps.

So as in [16], dh treats terminating and non-terminating terms in a uniform way.

When analyzing the complexity of programs, one is interested in evaluations of basic termsf(t1, . . . , tk) where a defined symbolf ∈Σdef(R) is applied to data objectst1, . . . , tk∈ T(Σcon(R),V). Theinnermost runtime complexity function ircR corresponds to the usual notion of “complexity” for programs. It maps anyn∈Nto the length of the longest sequence of→i R-steps starting with a basic termtwith|t| ≤n. Here, the sizeof a term is|x|= 1 for x∈ V and|f(t1, . . . , tk)|= 1 +|t1|+. . .+|tk|, andTB is the set of all basic terms.

I Definition 16 (Innermost Runtime Complexity ircR [11]). For a TRS R, its innermost runtime complexity function ircR:N→N∪{ω}is ircR(n) = sup{dh(t,→i R)|t∈ TB,|t| ≤n}.

In Sect. 4 we computed the lengthrt(n) of the rewrite sequences represented by a rewrite lemma si rt(n) t, where V(s) = n. However, ircR is defined w.r.t. the size of the start term of a rewrite sequence. Thus, to obtain a lower bound for ircR from rt(n), for any σ:V(s)→None has to take the relation betweenand the size of the start term G/A into account. Note that our approach in Sect. 2 only speculates lemmas wheres has the form fτ1(s1), . . . , γτk(sk)). Here, f ∈ Σdef(R), s1, . . . , sk are polynomials over n, and τ1, . . . , τk are simply structured types. For anyτi, letdτi :ρ1× · · · ×ρbτ beτi’s recursive constructor. Then for anyn∈N, Def. 3 implies|γτi(n)↓G/A|=szτi(n) forszτi:N→Nwith

szτi(n) =|γτi(0)↓G/A|+n· 1 +|γρ1(0)↓G/A|+· · ·+|γρb(0)↓G/A| − |γτi(0)↓G/A| . The reason is thatγτi(n)↓G/A containsn occurrences ofdτi and of eachγρ1(0)↓G/A, . . . , γρb(0) ↓G/A except γτi(0) ↓G/A, and just one occurrence of γτi(0) ↓G/A. For instance,

Nats(n)↓G/A| isszNats(n) =|γNats(0)↓G/A|+n·(1 +|γNats(0)↓G/A| − |γNats(0)↓G/A|) =

|zero|+n= 1 +nand|γList(n)↓G/A|isszList(n) =|γList(0)↓G/A|+n·(1 +|γNats(0)↓G/A|) =

|nil|+n·(1 +|zero|) = 1 +2. Consequently, the size ofsG/A=fτ1(s1), . . . , γτk(sk))↓G/A withV(s) =nis given by the following function sz:Nm→N:

(12)

sz(n) = 1 +szτ1(s1) +· · ·+szτk(sk)

For instance, the term qs(γList(n))↓G/A= qs(consn(zero,nil)) has the size sz(n) = 1 + szList(n) = 2n+ 2. Since|γτ(0)↓G/A|is a constant for each typeτ,sz is a polynomial whose degree is given by the maximal degree of the polynomialss1, . . . , sk.

So the rewrite lemma (4) forqs states that there are terms of sizesz(n) = 2n+ 2 with reductions of lengthrt(n) = 3n2+ 2n+ 1. To determine a lower bound for ircRqs, we construct an inverse functionsz−1with (sz◦sz−1)(n) =n. In our example wheresz(n) = 2n+2, we have sz−1(n) = n−22 ifnis even. So there are terms of sizesz(sz−1(n)) =nwith reductions of length rt(sz−1(n)) =rt(n−22 ) = 34n2−2n+ 2. Since multivariate polynomialssz(n1, . . . , nm) cannot be inverted, we invert the unary functionszN:N→NwithszN(n) =sz(n, . . . , n) instead.

Of course, invertingszNfails ifszNis not injective. However, the conjectures speculated in Sect. 2 only contain polynomials with natural coefficients. Then,szNis always strictly monoto- nically increasing. So we only proceed if there is asz−1N : img(szN)→Nwhere (szN◦sz−1N )(n) = nholds for alln∈img(szN) ={n∈N| ∃v∈N.szN(v) =n}. To extendsz−1N to a function onN, for any (total) functionh:M →NwithM ⊆N, we define bhc(n) :N→Nby:

bhc(n) =h( max{n0 |n0M, n0n}), ifn≥min(M) and bhc(n) = 0, otherwise Using this notation, the following theorem states how we can derive lower bounds for ircR. ITheorem 17(Explicit Lower Bounds forircR).Letsi rt(n1,...,nm)tbe a rewrite lemma forR, letsz:Nm→Nbe a function such that sz(b1, . . . , bm)is the size ofs[n1/b1, . . . , nm/bm]↓G/A for allb1, . . . , bm∈N, and letszN’s inverse function sz−1N exist. Then rtN◦ bsz−1N cis a lower bound forircR, i.e.,(rtN◦ bsz−1N c)(n)≤ircR(n)holds for all n∈Nwithn≥min(img(szN)).

So for the rewrite lemma (4) forqswhereszN(n) = 2n+ 2, we havebsz−1N c(n) =bn−22 c ≥

n−3

2 and ircRqs(n)≥rt(bsz−1N c(n))≥rt(n−32 ) =34n272n+194 for alln≥2.

However, even if sz−1N exists, finding resp. approximating sz−1N automatically can be non-trivial in general. Therefore, we now show how to obtain an asymptotic lower bound for ircR directly from a rewrite lemmafτ1(s1), . . . , γτk(sk))→i rt(n)twithout constructing sz−1N . As mentioned, ifeis the maximal degree of the polynomialss1, . . . , sk, thensz is also a polynomial of degreeeand thus,szN(n)∈ O(ne). Moreover, from the induction proof of the rewrite lemma we obtain an asymptotic lower bound forrtN, cf. Thm. 14. Using these bounds, Lemma 18 can be used to infer an asymptotic lower bound for ircR directly.

ILemma 18 (Asymptotic Bounds for Function Composition). Let rtN,szN : N→N where szN∈ O(ne)for somee≥1 and whereszNis strictly monotonically increasing.

If rtN(n)∈Ω(nd)with d≥0, then(rtN◦ bsz−1N c)(n)∈Ω(nde).

If rtN(n)∈Ω(bn)withb≥1, then(rtN◦ bsz−1N c)(n)∈Ω(ben).

So for the rewrite lemma qs(γList(n))→i rt(n)γList(n) where rtN=rt andszN=sz, we only need the asymptotic boundssz(n)∈ O(n) andrt(n)∈Ω(n2), to conclude ircRqs(n)∈ Ω(n21) = Ω(n2), i.e., to prove that the quicksort TRS has at least quadratic complexity.

So while Thm. 17 explains how to find concrete lower bounds for ircR(ifszNcan be inver- ted), the following theorem summarizes our results on asymptotic lower bounds for ircR. To this end, we combine Thm. 14 on the inference of asymptotic bounds forrt with Lemma 18.

ITheorem 19 (Asymptotic Lower Bounds forircR). Letsi rt(n)t be a rewrite lemma for R and let sz : Nm → N be a function such that sz(b1, . . . , bm) is the size of s[n1/b1, . . . , nm/bm]↓G/A for allb1, . . . , bm∈N, whereszN(n)∈ O(ne)for somee≥1 andszNis strictly monotonically increasing. Furthermore, letih,ib, andis be defined as in Def. 11.

1. Ifih= 0andib andis are polynomials of degreedib anddis, thenircR(n)∈Ω(n

max{dib,dis}

e ).

Referenzen

ÄHNLICHE DOKUMENTE

We consider a generalization of the Prize Collecting Steiner Tree Problem on a graph with special redundancy requirements on a subset of the customer nodes suitable to model a

In [11], the author shows how to extend Lee-Brickell’s and Stern’s algorithms to codes over F q. The website [12] lists the work factor of this algorithm against several parameters.

Pos- sibly the most important novel results of this paper are the following: (1) A derivation of a lower bound for the Jaccard distance of sets represented using Bloom filters, (2)

joint work with Florian Frohn, Jera Hensel, Cornelius Aschermann, and Thomas

The in- stantiation, rewriting, and forward instantiation processors are analogous to the narrowing processor (i.e., they adapt the techniques of instantiating, rewriting, and

Techniques for automated innermost termination analysis of term rewriting are very powerful and have been successfully used to analyze termination of programs in many

new approach: direct adaption of DP framework (CADE ’11) modular combination of different techniques. automated and more powerful than

We present an automated approach to prove termination of Java Bytecode (JBC) programs by automatically transforming them to term rewrite systems (TRSs).. In this way, the