• Keine Ergebnisse gefunden

5.2 Control-flow Refinement of a Cost Relation

5.2.4 Refined Chains

thensp(T) = t(ctd(a : b),[sp(T1),· · ·,sp(Tm)]). Conditions (1) and (2) hold withctd:C(x : y) = ϕ0ϕ1,b1,· · ·,bj−1,C(x1:y1),bj+1,· · ·,bnCRS0. The same assignmentσsatisfies condition (3) and we have thatsp(Ti)∈¹CRS0|biσºc for 1≤i<m andi6= j andsp(Tj)∈¹CRS0|C(x1 :y1)σºc

(thanks to the proof for finite evaluations above), andsp(Tm)∈¹CRS0|bmσºω because of the co-induction hypothesis. The additional condition that allbi in the refined CE have to be evaluated is also satisfied.

The case wherecis not recursive is analogous to the latter wherecis tail-divergent.

CE case distinction is precise

Point (a) basically proves that spCRS|C(a : bc) ⊆ ¹CRS0|C(a : bc and spCRS|C(a : bω) ⊆

¹CRS0|C(a:b)ºωfor everyCand every a:b. In order to prove precision, it is enough to show the other direction, that is,spCRS|C(a: bc)⊇¹CRS0|C(a: bc andspCRS|C(a: bω)⊇¹CRS0|C(a: bω. This, in turn, is equivalent to showing that any evaluation T ∈¹CRS0|C(a:b)ºis the result applyingsp to an evaluationT0∈¹CRS|C(a:b)º.

In this case, it is easy to see that any evaluationT ∈¹CRS0|C(a:b)ºmatches one of the cases obtained by applying sp in the previous proof. In particular, the additional requirements on the evaluations of refined CEs prevent us from having finite evaluations that contain partial CEs and from having infinite evaluations that diverge before their last call.

This result is useful later on and thus we establish it in a lemma.

Lemma 5.16. LetCRSandCRS0be the cost relation systems before and after CE case distinction. For anyC and any parametersa:b, we have:

spCRS|C(a:bc) =¹CRS0|C(a:bc

spCRS|C(a:b)ºω) =¹CRS0|C(a:b)ºω

always non-terminating, we can apply unreachable code elimination (Definition4.13) to CE2and obtain a tail-divergent CE

2:tail(l,m) ={l≥1,l0=l−1,mx,x<0},inf(x)

Once this simplification has been performed, CE2is no longer recursive and the chains of CRtailcan be clearly divided into terminating and non-terminating.

Terminating: (3)+(1) (1)

Non-terminating: (3)+(2) (2) (3)ω

Chains(3)+(2)and(2)are non-terminating because they end in a tail-divergent CE.

If some partially refined cost equations are non-tail recursive, that is, a CE with calls after the recursive callc:C(x :y) =ϕa,b1,· · ·,bi1,C(xi: yi),ϕb,bi,· · ·,bn, there can still be chains that might terminate or not. If cis tail divergent, it has a non-terminating call bn after the recursive call C(xi : yi)has been completely evaluated. Therefore,cdoes not have to appear at the end of the chain and can be part of an iterative phaseph:= (c1∨ · · · ∨cm)+. In such a case, the termination ofphwill depend on whether CEc is ever evaluated and we have to distinguish cases.

Example 5.18. Consider a non tail recursive variant of the cost relation from Example5.17:

1:nonTail(l,m) ={l=0}, 0

2:nonTail(l,m) ={l≥1,l0=l−1,mx,x<0},nonTail(l0,m),inf(x) 3:nonTail(l,m) ={l≥1,l0=l−1,mx,x≥0},nonTail(l0,m)

In this case, CE2cannot be simplified further and the chains ofnonTailare:

(2∨3)+(1) (1) (2∨3)ω

While chains(1)and (2∨3)ω are respectively terminating and non-terminating, chain(2∨3)+(1) can terminate or not depending on whether CE3is evaluated in the phase(2∨3)+.

In order to generate chains over refined cost equations, the refinement distinguishes cases for phases and for chains.

Definition 5.19 (Phase Case Distinction). Let ph := (c1 ∨ · · · ∨cm)+ be an iterative phase, such that c1,· · ·,ci for0≤imare tail-divergent, the following phases are generated:

1. Acomplete phasephc:= (cci+1∨ · · · ∨ccm)+ where the phase is completely evaluated. Not all CEs can be tail-divergent (i<m).

2. A partial phasephp := (c1p∨ · · · ∨cpm)+ where the recursive calls diverge but not within the phase.

All the bi after the recursive calls are not evaluated.

3. Atail-divergent phasephtd:= (c1p∨ · · · ∨cmpctd1 ∨ · · · ∨ctdicci+1∨ · · · ∨ccm)+ where a tail-divergent CE is evaluated at some point in the phase. The evaluation diverges within the phase. The phase must contain at least one tail-divergent CE (i≥1).

If none of the CE in the phase is tail-divergent (i=0), only cases (1) and (2) are generated. If on the contrary, all CE in the phase are tail-divergent (i=m) only cases (2) and (3) are generated.

Non-iterative phases ph:= (c) are a particular case of the above. If cis not tail-divergent, a partial phase php:= (cp)and a complete phase phc:= (cc) are generated. Ifcis tail-divergent, a partial phase php:= (cp)and tail-divergent phasephtd:= (ctd) are generated.

Finally, a divergent phaseph:= (c1∨ · · · ∨cm)ωgeneratesphd := (c1p∨ · · · ∨cmp)ω.

5.2. Control-flow Refinement of a Cost Relation 59

The pattern of tail-divergent phases is not precise, in fact it can match any of the other patterns (complete and partial phases). A more precise pattern would be:

(c1p∨ · · · ∨cpm)(ctd1 ∨ · · · ∨ctdi )(cci+1∨ · · · ∨ccm)

However, keeping the simpler pattern does not affect the soundness of the analysis and it allows us to maintain a uniform format for the phases.

Example 5.20. Continuing with Example5.18, we generate the following phases for CRnonTail:

Complete Partial Tail-divergent Divergent (3c)+ (2p∨3p)+ (2td∨3c∨2p∨3p)+ (2p∨3p)ω (1c)

Finally, chains can also be refined into terminating and non-terminating chains.

Definition 5.21 (Refined Chains). Let ch be a chain, the functions term and nterm receive a chain and return its terminating and non-terminating refinements. A terminating chain is written chc and a non-terminating chain is writtenchω.

term(ch) :=§ phc ifch=ph

phc·chc0 ifch=ph·ch0andchc0=term(ch0)

nterm(ch) :=





{phd} ifch=phphis divergent

{phtd} ifch=phph= (c)

{phtd·chc0|chc0=term(ch0)}

∪{php·chω0|chω0nterm(ch0)} ifch=ph·ch0

The functiontermreturns a single terminating chain whereasntermreturns a (possibly empty) set of non-terminating chains. Note though thattermis not defined for all chains. For instance, it is not defined for chains whose last phase is divergent.

Example 5.22. The resulting chains generated from the phases in Example5.20are the following1: Terminating chains: (3c)+(1c) (1c)

Non-terminating chains: (2td∨3c∨2p∨3p)+(1c) (2p∨3p)ω

The evaluations of refined chains are defined as the evaluations of chains (Definition5.8) but we only define finite evaluations for terminating chains and infinite evaluations for non-terminating chains:

Definition 5.23 (Refined Chain Evaluations). Let chc and chω be respectively a terminating and non-terminating chain of CRCanda:b some parameters, their evaluations are:

¹CRS|C[chc](a:bc:=

T ∈¹CRS|C(a:b)ºc label(pathsC(T))⊆ Lchc

¹CRS|C[chω](a:bω:=

T ∈¹CRS|C(a:b)ºω label(pathsC(T))⊆ Lchω

Thanks to this case distinction, terminating and non-terminating evaluations are separated. This is important to guarantee Claim5.1for the remaining cost relations.

In the common case where everything is terminating (all divergent phases have been discarded and there are no tail-divergent CEs), the case distinction generates only a terminating chain for each original

1 The chain(2p3p)ωcan be proved terminating and be discarded.

chain. On the other hand, if some CEs are tail-divergent, the case distinction generates different non-terminating chains depending on where non-termination originates.

Example 5.24. Consider a chainch:=ph1·ph2·ph3·ph4 whereph1 andph3 contain tail-divergent CEs andph4 is a base case. The generated non-terminating chains are:

chω1:=ph1p·ph2p·phtd3 ·ph4c chω2:=phtd1 ·ph2c·phc3·ph4c

However, ifph4was divergent, the only refined chain would bechω:=ph1p·ph2p·ph3p·phd4.

The following theorem is an updated version of Theorem 5.9(Chains Completeness) for the refined chains.

Theorem 5.25 (Refined Chains Completeness). Let CRS0 be the cost relation system after the CE case distinction (Definition5.13). Letchc1,· · ·,chcn andchω1,· · ·,chωm be the terminating and non-terminating refined chains ofC. We have that for all parametersa:b:

¹CRS0|C(a:bc=

n

[

i=1

¹CRS0|C[chci](a:bc (5.3)

¹CRS0|C(a:b)ºω= [m

i=1¹CRS0|C[chωi](a:b)ºω (5.4) Proof of Theorem5.25. The direction⊇ holds by Definition5.23. The other direction ⊆can be proved based on Lemma 5.16 (sp(¹CRS|C(a : b)º) = ¹CRS0|C(a : b)º) and based on Theorem 5.9 (Chain completeness before the case distinction).

Consider Equation 5.3. Let T0 ∈ ¹CRS0|C(a : bc, we know that there is a T ∈ ¹CRS|C(a : bc

such that sp(T) = T0 (Lemma 5.16). For T there is a chain ch such that T ∈ ¹CRS|C[ch](a : b)ºc

(Theorem 5.9). It is enough to prove that every p0pathsC(sp(T)) matches term(ch) = chc. Because T is finite, sp maps every c to cc. Additionally, we know that T and any of its paths ppathsC(T) do not contain any node with a tail-divergent CE. Such paths match the original chain label(p) ∈ Lch

and chc = term(ch) corresponds to ch in which every CE c has been substituted by cc and the tail-divergent CEs have been excluded. Therefore, for everyp0pathsc(sp(T))we havelabel(p0)∈ Lchc and sp(T) =T0∈¹CRS0|C[chc](a:bc.

Consider Equation 5.4. Let T0∈¹CRS0|C(a:bω, we know that there is a T ∈¹CRS|C(a:bω such thatsp(T) =T0. ForT there is a chainchsuch thatT ∈¹CRS|C[ch](a:bω. Therefore, it is enough to prove that allp0pathsC(sp(T))match a chain innterm(ch). We prove it by induction on the length of the chain (number of phases).

In the base casech=ph, there are two possibilities.

• Ifphis divergent,phd is well-defined and so ischω=phd. In addition, any sub-evaluation T00of a CE in the phase is infinite and thereforesp(T00)mapscto cp and for any path p0pathsC(T0)we havelabel(p0)∈ Lchω.

• If the phase is non-recursiveph= (c),label(sp(T)) =ctd(chas to be tail divergent soT is infinite).

Therefore, we have thatphtd = (cc) and chω =phtd are well-defined. This phase contains all the possible cases of the CEs inphsolabel(p)∈ Lchω.

Let us consider now the inductive case. The chain has the formch=ph·ch0. Every pathppathsC(T) and can be split into two parts p=p1·p2 where p2∈ Lch0 where p1 is common for all the paths p. The corresponding path inT0isp0=p01·p20. Let T2 be the evaluation whose root starts with the first node of p2 i.e. p2pathsC(T2). The evaluation T2 might be finite or infinite.

5.2. Control-flow Refinement of a Cost Relation 61

• If T2 is finite, then T2 ∈ ¹CRS|C[ch0](a : bc and sp(T2) ∈ ¹CRS0|C[chc0](a : bc (this is the finite case which we have already proved). Because T is infinite, there must be a call to a non-terminating chain so there must be aciphthat is tail-divergent. This means that the phasephtd is well-defined and p01 matchesphtd (phtd contains both the complete, the tail-divergent, and the partial versions of the CEs inph),chω=phtd·chc0is defined, and every pathp0 matcheschω.

• If T2 is infinite, then T2 ∈ ¹CRS|C[ch0](a : bω and there is a chain chω0 such that sp(T2) ∈

¹CRS0|Cr[chω0](x y)ºωby induction hypothesis and p20 matcheschω0. Moreover, we have that every node inp1 is mapped tocp(because they contain an infinite evaluation onC) and thus p10 matches php. The chainchω=php·chω0is defined and everyp0matches it.