• Keine Ergebnisse gefunden

This section contains an alternative algorithm to compute cost structures of chains that can also be ap-plied to chains with multiple recursion. The main idea is to extend the algorithm for phases (Section6.5) to include the cost of the chain or chains that appear after the given phase.

We consider a general algorithm for chains that start with multiple phases first. Non-multiple iterative phases represent a particular case of this general approach. Letch=ph·CHandph=M(c1∨ · · · ∨cn)+/ω or ph=M(c1∨ · · · ∨cn)+, we start from valid cost structures 〈Eci,ICci,FCci(x x0)〉 for each ci and valid cost structures〈Ech0,ICch0,FCch0(x y)〉for eachch0CH.

Remark6.42. Let T ∈ ¹C[chf c be an arbitrary evaluation. Eachci is evaluated#ci:=|CEinst(T,ci)|

times (see Definition6.7) and〈Ecij,ICcij,FCcij(acija0c

ij)〉represents the cost structure instance of the j-th CE evaluation ofci for 1≤ j ≤#ci. Similarly,#ch0=|maxCH(T,ch0)|is the number of maximal evalu-ation trees of chain ch0 in T (see Theorem 6.8) and 〈Ech0j,ICch0j,FCch0j(ach0jbch0j)〉 is the cost structure instance of the j-th maximal evaluation tree ofch0for1≤ j≤#ch0. The following cost structure can be evaluated toCost(T).

* n

P

i=1

#ci

P

j=1

Ecij+ P

ch0∈CH

#ch0

P

j=1

Ech0j,

n

S

i=1

#ci

S

j=1

(ICcij)∪ S

ch0∈CH

#ch0

S

j=1

(ICch0j)

 ,

n

S

i=1

#ci

S

j=1

FCcij(acija0c

ij)

S

ch0∈CH

#ch0

S

j=1

FCch0j(ach0jbch0j)

 +

This remark is a direct result of Theorem6.8and Definitions 6.7and6.6. As in the case for phases, this cost structure has to be transformed to remove the sums over unknowns. For the main cost expressions and non-final constraints, this can be done using the same approach as with phases (see Sections 6.5 and6.5.1). Sum variables can also be defined for the intermediate variables of the chainsch0CHand the transformations are also valid for the constraints originated inch0. Algorithm2now receives a tuple that also containsICch0 for eachch0CHand the returned constraint set isICchinstead ofICph. The rest is not modified.

The shape of Algorithm3also remains largely the same. In addition to the final constraint sets of the CEs of the phase, it receives the final constraint sets of eachch0CHand generates the corresponding pending sets Psumsch0 and Pmsch0. The algorithm has access to the chains insideCH and its summaries so they can be used by the strategies. Finally, the returned constraint sets are the constraint sets of the complete chainch, not only of the phaseph.

Next, the strategies for phases are adapted to deal with constraints in these new pending sets and to generate constraints that depend only on the initial input and output initial values (xsys) of the chain.

6.7. Chains with Multiple Recursion 99

Table 6.3.: Classification conditions for Inductive Sum Strategy in multiple chains: Acephor ach0C H named B can be classified into a class with respect to a candidate cd(x y) if its condition is satisfied.

Class Condition when./is≤ Condition when./is≥

Cnt (P

iv0./kl0k)∈PsumsB∧ kl0k./cd(x y)

#calls(B)

P

k=1

cd(xkyk)≥0 Dc 0≤dcB(x)≤cd(x y)

#calls(B)

P

k=1

cd(xkyk) dcB(x)≥cd(x y)−

#calls(B)

P

k=1

cd(xkyk) Ic icB(x)≥

#calls(B)

P

k=1

cd(xkyk)−cd(x y) 0≤icB(x)≤

#calls(B)

P

k=1

cd(xkyk)−cd(x y)

Inductive Sum Strategy for Multiple Chains

This strategy follows the same scheme of the original Inductive Sum strategy and is valid for chains of the form ch = ph · CH where ph = M(c1 ∨ · · ·cn)+, that is, it is not applicable for phases that might diverge. In this case, the CEs in the phase can have multiple recursive calls ci:C(x :y) =ϕci,b0,C(x1:y1),b1,C(x2:y2),· · ·,C(xn:yn),bn. The function #calls(ci) denotes the number of recursive calls of ci and C(xk : yk) is the k-th recursive call for 1 ≤ k ≤ #calls(i). For chains ch0CH, we define #calls(ch0) := 0 and ϕch0 := summary(ch0) so cost equations ciph and chainsch0CHcan be treated uniformly.

Given a constraintP

iv./klk ∈PsumsAwhereAis aciphor a chainch0CH, the strategy generates a candidatecd(x y)such that:

ϕA⇒ klk./cd(x y)−

#calls(A)

X

k=1

cd(xkyk)≥0

This is a direct generalization of the condition used for linear phases. Note that if Ais a chain ch0,

#calls(A)is zero and the condition isϕA⇒(klk./cd(x y)≥0).

Once a candidate has been generated, the CEsceph and the chainsch0CHhave to be classified according to their behavior with respect to the candidate. This strategy has the same classes as before but the conditions have been generalized for CEs with multiple recursive calls and for chains.

LetBbe acephor ach0CH, Table6.3contains the classes and their conditions. The classesCnt,Dc andIc also define the expressionscntB := P

smiv0, ivdc

B:=kdcB(x)k and ivic

B :=kicB(x)k respectively.

Note that whenBis a chain, the expressioncd(x y)−P#calls(B)

k=1 cd(xkyk)simply corresponds tocd(x y).

Theorem 6.43. If everycephand everych0CHis classified intoCnt,IcorDcwith respect to a candidate cd(x y), the following constraints are valid:

X

B∈Cnt

cntB./ivcd+ivcd+X

B∈Ic

smivicB−X

B∈Dc

smivdcB ivcd+./kcd(xsys)k ivcd!./k−cd(xsys)k Finally, the strategy adds the constraintsivic

B ./ kicB(x)k andivdc

B ./ kdcB(x)k to the corresponding pending setsPsumsBfor eachBIcandBDc.

Table 6.4.:Classification conditions for Inductive Sum Strategy wit Resets in multiple chains: A ceph or ach0C HnamedBcan be classified into a class with respect to a candidatecd(x y)if its condition is satisfied.

Class Condition

CntR (P

iv0≤ kl0k)∈PsumsB∧ kl0k ≤ kcd(x)k −#calls(B)P

k=1

kcd(xk)k

DcR 0≤ kcd(x)k −#calls(B)P

k=1

kcd(xk)k IcR icrB(x)≥

#calls(B)

P

k=1

kcd(xk)k − kcd(x)k

Inductive Sum Strategy with Resets for Multiple Chains

As in the linear case, Inductive Sum Strategy with Resets can be applied to chains with multiple phases that might diverge, that is, chains of the formch:=ph·CHwhereph:=M(c1∨· · ·cn)+/ωand it generates only upper bound constraints.

The candidates are generated using the condition of class CntR(see Table 6.4) and only depend on the input variables cd(x). The strategy considers the classes CntR, DcR, and IcR from Table 6.4. In the case of multiple recursion, the conditions from the Inductive Sum Strategy cannot be reused. The conditions for this strategy have to always consider the positive part of the candidates (kcd(x)k) not only forCntRbut also forDcRandIcR. This is necessary for the soundness of the strategy. Given these conditions, resets become a special case of IcR and thus no distinct class Rstis considered. It is also worth noting that the condition for the DcRis trivial for chains ch0CH because the right-hand side kcd(x)k−P#calls(ch0)

k=1 kcd(xk)k=kcd(x)kis always positive. Consequently, the strategy can always classify chains intoDcRwhere they are ignored.

Theorem 6.44. If every cephand everych0CHis classified into CntR,DcR, andIcRwith respect to a candidatecd(x), the following constraints are valid:

X

BCntR

cntrBivcd+ X

BIcR

smivicrB ivcd ≤ kcd(xs)k

For each BIcR, the strategy adds the constraintivicrB ≤ kicr(x)ktoPsumsB.

Basic Product Strategy

The basic product strategy does not require any changes, it can be directly applied to constraints from pending sets of chains.

Max-Min Strategy for Multiple Chains

This strategy requires no changes in the candidate generation and in the constraint generation. Theo-rems6.33and6.34are also valid for constraintsiv≥ klk ∈Pmsch0 andiv≤ klk ∈Pmsch0 and for phases with multiple recursion. However, the classification differs. In this strategy, the chains ch0CH do not need to be classified and the classification conditions are given in Table6.5. Instead of considering the sum of the values of the candidate in all the recursive calls, these conditions consider these values independently.

6.7. Chains with Multiple Recursion 101

Example 6.45. Let us consider Program4(in Page11) and its chainM(4c∨5c)+{(3c)}. Its cost relations are in Figure 5.4 and they have been strengthened in Example 5.66 (in Page 74). For simplicity, we omit the superscript of the CEs and we assume the cost structures of its components have been already computed. The cost structure of(3)is empty, and the cost structures for4and5are〈iv1+1,;,{iv1=t1}〉

and 〈1,;,;〉 respectively (t1= l1is the cost of the call to append[(2)+(1)]). The simplified constraint sets are:

CE/Chain Constraint set

4 {t =1+t1+t2,t2≥0,t1≥1,t =l}

5 {t =1+t1+t2,t1≥0,t2≥0,l=t2+1,t1=0} (3) {t =l=0}

The initial pending sets arePsums4 ={ivi t

4 ≤1,ivi t

4 ≥1,iv1 ≤ kt1k,iv1 ≥ kt1k}andPsums5 ={ivi t

5 ≤ 1,ivi t

5≥1}. The cost structure computation is as follows:

1. Consider ivi t

4 ≤ 1∈ Psums4. The algorithm applies the Inductive Sum Strategy and generates a candidate t. The classification isCnt:={4,5}and Dc:={(3)} withdc(3):=0 and the generated constraints aresmivi t

4+smivi t

5ivcd+ivcd,ivcd+≤ ktk, andivcd≥ k−tkwhich can be simplified tosmivi t

4+smivi t

5≤ ktk.

2. The processing ofivi t4≥1 is symmetric and its generated (and simplified) constraint issmivi t4+ smivi t

5≥ ktk.

3. The Inductive Sum Strategy fails to generate a candidate foriv1≤ kt1k. Instead, the Basic Product Strategy can be applied and it generates smiv1smivi t4· dive1. The variable smivi t4 has already been bounded soivi t

4≤1does not need to be added to the pending sets. The constraintiv1≤ kt1k is added toPms4.

4. Consider iv1 ≤ kt1k ∈ Pms4. The algorithm applies the Max-Min strategy and it generates the candidate t (t ≥ t1). Both CEs 4 and 5 can be classified in Dc and the generated constraint is dive1t.

5. Finally, foriv1≥ kt1k ∈Psums4no non-trivial constraint can be obtained.

The resulting cost structure is:

smiv1+smivi t

4+smivi t

5, {smiv1smivi t

4· dive1}, {dive1t,smivi t

4+smivi t

5=ktk}

which represents an upper bound(1+tt and a lower bound oft. Note that the lower bound is precise for the given cost model. It corresponds to the case where the input argumentt is a degenerate tree and appendis always called withl1=Nil.

Linear Phases as a Special Case

The approach for chains with multiple recursion can also be applied for chains ch:=ph·ch0. It corre-sponds to having a setCHwith only one elementch0(see Corollary6.9).

The only particularity is that in such a chain, we know that#ch0=1and consequently for intermediate variables fromch, we havesmiv=dive=bivc. Therefore, theBasic Product strategycan be simplified for those cases. LetP

iv≤ klk ∈Psumsch0, the modified strategy generates the constraintP

smiv≤ dive0and addsiv0≤ klk toPmsch0 whereiv0is a fresh intermediate variable. Similarly, letP

iv≥ klk ∈Psumsch0, it generates the constraintP

smiv≥ bivc0and addsiv0≥ klktoPmsch0.

Table 6.5.:Classification conditions for Max-Min strategy in multiple chains: Acecan be classified into the classesDc,IcorRstwith respect to a candidatecd(x)if its conditions are satisfied.

Class Condition when./is≤ Condition when./is≥ Dc

#calls(ce)

V

k=1

0≤dcB(x)≤cd(x)−cd(xk) #calls(cV e)

k=1

dcB(x)≥cd(x)−cd(xk) Ic

#calls(ce)

V

k=1

icB(x)≥cd(xk)−cd(x)

#calls(ce)

V

k=1

0≤icB(x)≤cd(xk)−cd(x) Rst

#calls(ce)

V

k=1

cd(xk)./krstB(x)k

Example 6.46. This approach obtains bounds for Program16. Consider the chain (3)+(2.1). The cost structures for CE 3 and chain (2.1) are 〈1,;,;〉 and 〈iv1,;,{iv1 = kbk}〉 respectively. The main cost expression is smivi t

3+smiv1 and the initial pending sets are Psums(2.1) = {iv1 ≤ kbk,iv1 ≥ kbk} and Psums3={ivi t3≤1,ivi t3≥1}. The cost structure computation is as follows:

1. First, the constraints ivi t3 ≤1and ivi t3 ≥1can be processed by the Inductive Sum Strategy. The generated (simplified) constraints aresmivi t

3≤ kakandsmivi t

3≥ kak.

2. Next, the constraintiv1 ≤ kbk ∈Psums(2.1) is selected. The Inductive Sum strategy generates the candidate b. The classification is Cnt := {(2.1)} and Ic := {3} with ic3 := kak. The generated (simplified) constraints aresmiv1iv2+smivic

3andiv2≤ kbk. The constraintivic

3≤ kakis added to the pending setPsums3. The treatment ofiv1≥ kbk ∈Psums(2.1)is symmetric.

3. The constraint ivic

3 ≤ kak ∈ Psums3 can be processed using the Basic Product strategy, which generatessmivic

3smivi t

3· diveic3, followed by the Max-Min strategy which generatesdiveic3≤ kak.

This is enough to obtain an upper bound. The cost structure at this point is:

* smivi t

3+smiv1,

§ smiv1=iv2+smivic

3

smivic

3smivi t

3· diveic3 ª

,

smivi t

3=kak, iv2=kbk, diveic3≤ kak

 +

and the corresponding upper bound iskak+kbk+ (kak2). 4. Alternatively, the constraintsivic

3≤ kakandivic

3≥ kakcan also be processed using the Triangular Sum strategy withq=1. The resulting (simplified) cost structure is:

* smivi t

3+smiv1,









smiv1=iv2+smivic

3

smivic

3=ivp1+12ivp212ivi ts ivp1=ivini·ivi ts

ivp2=smivi t

3·ivi ts ivi ts=smivi t

3







 ,

smivi t

3=kak, iv2=kbk, ivini=kak

 +

which represents the precise cost (upper and lower bound)kak+kbk+ (kak2/2+kak/2)

This approach can obtain bounds for programs where the approach based on composing the cost structures of the individual phases fails. This is because using this approach, the strategies for Sum and Max variables are used to infer non-linear size relations whereas the compositional approach relies on linear summaries that cannot capture such size relations. That is precisely the case with Program16.

However, this alternative does not solve all the limitations of the analysis with respect to non-linear size relations and it presents other disadvantages. Namely, this approach is less modular. Instead of

6.7. Chains with Multiple Recursion 103

computing a cost structure for the phase once, a computation has to be done for each chain that starts with such phase. In addition, it is harder to generate good candidates for the Inductive Sum strategy and obtain expressions that depend on the output as well. That is, it is harder to obtain amortized bounds using this approach.

A more detailed account of the limitations of the current analysis with respect to non-linear size relations is given in Chapter10.