• No results found

2.2 Automatic Amortized Analysis

3.3.3 Partial Big-Step Operational Semantics

A general shortcoming of classic big-step operational semantics is that it does not provide judgments for evaluations that diverge. This is problematic if one intends to prove statements for all computations (divergent and convergent) that do not go wrong.

A straightforward remedy is to use a small-step semantics to describe computations. But in the context of resource analysis, the use of big-step rules seems to be more favorable. Firstly, big-step rules can more directly axiomatize the resource behavior of compiled code on specific machines. Secondly, it allows for shorter and less syntactic proofs.

Another classic approach [CC92, Ler06] is to add divergence rules to the operational semantics that are interpreted coinductively. But then one loses the ability to prove state- ments by induction on the evaluation which is crucial for the proof of the soundness theorems of the analysis systems (see Chapters 4, 5, and 6). It should also be possible to work with a coinductive definition in the style of Cousot or Leroy [CC92, Ler06]. How- ever, coinductive semantics leans itself less well to formulating and proving semantic soundness theorems of the form “if the program is well-typed and the operational semantics says X then Y holds”. For example, in Leroy’s Lemmas 17-22 [Ler06] the coinductive definition appears in the conclusion rather than as a premise.

That is why I use a novel approach to the problem here by defining abig-step semantics for partial evaluationsthat directly corresponds to the rules of the big-step semantics in Figures 3.2 and 3.3. The rules in Figures 3.5 and 3.6 define a judgment of the form

V,H`e |q

whereV is a stack,His a heap,q∈Q+0, andeis an expression. The meaning is that there

is a partial evaluation ofewith the initial stackV and the initial heapHthat consumes

qresources. Here,qis the watermark of the resource usage. We do not have to keep track of the restituted resources since partial evaluations are composed of complete evaluations only.

Since there might be negative constantsK, the partial evaluation rules have conclu- sions of the formV,H`e |max(q, 0) to ensure non-negative values. For simplicity, I just writeV,H`e |qinstead ofV,H`e |max(q, 0) in each conclusion of the rules in Figures 3.5 and 3.6.

Note that the rule P:ZEROis essential for the partiality of the semantics. It can be applied at any point to stop the evaluation and thus yields to a non-deterministic evaluation judgment. I explain the other rules with three representative examples.

The rule P:VARcan be understood as follows. To partially evaluate a variable, you can only do one evaluation step, namely evaluating the variable thereby producing the costKvarifKvar>0 and zero cost otherwise.

The rule P:LET1 can be read as follows. If there is a partial evaluation ofe1that needs qresources then you can partially evaluateletx=e1ine2by starting the evaluation of the let expression which costsK1let≥0 or reimbursesK1let<0 resources. Then you can partially evaluatee1, deriving a partial evaluation of the let expression that produces the watermarkK1let+q.

Another way to partially evaluate the let expressionletx=e1ine2is to use the rule P:LET2. There we completely evaluatee1measuring the resource consumption

V,H`e |0(P:ZERO) V,H`() |Kunit (P:CONSTU) b∈{True,False} V,H`b |Kbool (P:CONSTB) n∈Z V,H`n |Kint (P:CONSTI) x∈dom(V) V,H`x |Kvar (P:VAR) V(x)=v [yf 7→v],H`ef |q V,H`f(x) |K1app+q (P:APP) x1,x2∈dom(V) V,H`x1op x2 |Kop (P:BINOP) V,H`e1 |q V,H`letx=e1ine2 |K1let+q (P:LET1) V,H`e1 v1,H1|(q,q0) V[x7→v1],H1`e2 |p K1let·(q,q0)·K let 2 ·(p, 0)=(r,r0) V,H`letx=e1ine2 |r (P:LET2) V(x)=True V,H`et |q

V,H`ifxthenet elseef |K1conT+q

(P:CONDT )

V(x)=False V,H`ef |q

V,H`ifxthenet elseef |K1conF+q

(P:CONDF) V,H`nil |Knil(P:NIL) x1,x2∈dom(V) V,H`(x1,x2) |Kpair (P:PAIR) xh,xt∈dom(V) V,H`cons(xh,xt) |Kcons (P:CONS) V(x)=(v1,v2) V[x17→v1,x27→v2],H`e |q

V,H`matchxwith(x1,x2)→e |K1matP+q (P:MATP)

V(x)=NULL V,H`e1 |q V,H`matchxwith  nil→e1   cons(xh,xt)→e2 |K matN 1 +q (P:MATNIL) V(x)=` H(`)=(vh,vt) V[xh7→vh,xt7→vt],H`e2 |q V,H`matchxwith  nil→e1   cons(xh,xt)→e2 |K matC 1 +q (P:MATCONS)

V,H`leaf |Kleaf(P:LEAF)

x0,x1,x2∈dom(V)

V,H`node(x0,x1,x2) |Knode

(P:NODE)

V(x)=NULL V,H`e1 |q

V,H`matchxwith|leaf→e1|node(x0,x1,x2)→e2 |K1matTL+q (P:MATLEAF)

V(x)=`

H(`)=(v0,v1,v2) V[x07→v0,x17→v1,x27→v2],H`e2 |q

V,H`matchxwith|leaf→e1|node(x0,x1,x2)→e2 |K1matTN+q (P:MATNODE)

Figure 3.6: Partial big-step operational semantics (2 of 2).

evaluations and obtain a partial evaluation for the let expression that usesrresources where (r,r0)=K1let·(q,q0)·K2let·(p, 0).

Theorem 3.3.5 proves that if an expression converges in a given environment then the resource-usage watermark of the evaluation is an upper bound for the resource usage of every partial evaluation of the expression in that environment.

Theorem 3.3.5 IfV,H`e v,H0|(q,q0) andV,H`e |pthenpq.

PROOF By induction on the derivationDof the judgmentV,H`e v,H0|(q,q0). To prove theinduction basisletD consist of one step. Theneis a constantc, a variable

x, a binary operation x1op x2, a pair (x1,x2), the constantnil,leaf,cons(x1,x2), or

node(x1,x2,x3). Letebe for instance a variablex. Then by definition of E:VARit follows thatV,H`e v,H0|(Kvar, 0) orV,H`e v,H0|(0,−Kvar). Thusq=max(0,Kvar). The only P-rules that apply toxare P:VARand P:ZERO. Thus it follows that ifV,H`e

|pthen thenp=max(0,Kvar). The other cases are similar.

For the induction step assume that|D| >1. Theneis a pattern match, a function application, a conditional, or a let expression. For instance, lete be the expression

letx=e1ine2. Then it follows from rule E:LETthatV,H`e1 v1,H1|(q1,q10),V[x7→

v1],H1`e2 v2,H2|(q2,q20) and

(q,q0)=K1let·(q1,q01)·K2let·(q2,q20)·K3let (3.5)

By induction we conclude

ifV,H`e1 |p1thenp1q1 (3.6)

ifV[x7→v1],H1`e2 |p2thenp2≤q2 (3.7)

Now letV,H`e |p. Then this judgment was derived via the rules P:LET1 or P:LET2. In the first case it follows by definition thatp=max(p1+K1let, 0) for somep1andp1≤q1

IfV,H`e |pwas derived by P:LET2 then it follows that (p,p0)=K1let·(q1,q10)·

K2let·(p2, 0) for somep0,p2. We conclude from (3.7) thatp2≤q2and hence from Propo-

sition 3.3.1 and (3.5)pq. The other cases are similar to the case P:LET1. ■ Theorem 3.3.9 states that, in a well-formed environment, every well-typed expression either diverges or evaluates to a value of the stated type. To this end we instantiate the resource constants in the rules to count the number of evaluation steps.

Proposition 3.3.6 Let the resource constants be instantiated byKx =1,K1x =1 and

Kmx =0 for allxand allm>1. LetV,H`e v,H0|(q,q0) and let the derivation of the judgment havensteps. Thenq=nandq0=0.

PROOF By induction on the derivationDofV,H`e v,H0|(q,q0).

IfDconsists of only one step (|D| =1) theneis a constantc, a variablex, a binary operationx1op x2, a pair (x1,x2), the constantnil,leaf,cons(x1,x2), ornode(x1,x2,x3).

In each case,q=1 andq0=0 follows immediately from the respective evaluation rule. Now let|D| >1. Theneis a pattern match, a function application, a conditional, or a let expression. For instance, letebe the expressionletx=e1ine2. Then it follows from rule E:LETthatV,H`e1 v1,H1|(q1,q10),V[x7→v1],H1`e2 v2,H2|(q2,q02) and

(q,q0)=1·(q1,q01)·0·(q2,q20)·0=(1+q1,q01)·(q2,q20)

Letn1be the evaluation steps needed bye1and letn2be the number of evaluation steps needed bye2. By induction it follows thatq1=n1,q2=n2andq10 =q20 =0. Thus

q=n1+n2+1=n.

The other cases are similar.

The following lemma shows that if there is a complete evaluation that usesnsteps then there are partial evaluations that usei steps for 0≤in. It is used in the proof of Theorem 3.3.9 withi=n.

Lemma 3.3.7 Let the resource constants be instantiated byKx=1,K1x=1 andKmx =0 for allxand allm>1. IfV,H`e v,H0|(n, 0) thenV,H`e |ifor every 0≤in. PROOF By induction on the derivationD ofV,H`e v,H0|(n, 0). The proof is very

similar to the proof of Theorem 3.3.5. ■

Lemma 3.3.8 proves that you can always make one partial evaluation step for a well- typed expression in a well-formed environment. It is used in the induction basis of the proof of Theorem 3.3.9.

Lemma 3.3.8 Let the resource constants be instantiated byKx=1,K1x=1 andKmx =0 for allxand allm>1. IfΣ;Γ`e:A,HÍV :ΓthenV,H`e |1.

PROOF By case distinction one. The proof is straightforward so I only demonstrate two characteristic cases.

Letefor instance be a variablex. Then it follows fromΣ;Γ`x:AandHÍV:Γthat

xV. ThusV,H`x |1 by (P:VAR).

Lete now be a conditionalifxthenet elseef. Then it follows fromΣ;Γ`e: A

andHÍV :ΓthatV(x)∈{True,False}. Furthermore, we deriveV,H `et |0 and

V,H`ef |0 with the rule P:ZERO. Thus we can use either P:CONDT or P:CONDF to

deriveV,H`e |1.

Theorem 3.3.9 Let the resource constants be instantiated byKx=1,K1x=1 andKix=0 for allxand alli>1. IfΣ;Γ`e:AandHÍV :ΓthenV,H`e v,H0|(n, 0) for an

n∈NorV,H`e |mfor everym∈N. PROOF We show by induction onnthat if

Σ;Γ`e:A,V,H`e |nandHÍV :Γ (3.8)

thenV,H`e v,H0|(n, 0) orV,H`e |n+1. Then Theorem 3.3.9 follows since

V,H`e |0 for everyV,Hande.

Induction basisn=0: We use Lemma 3.3.8 to conclude from the well-formedness of the environment (3.8) thatV,H`e |1.

Induction stepn >0: Assume (3.8). If e is a constant c, a variablex, a binary operationx1 op x2, a pair (x1,x2), the constantnil, orcons(x1,x2). Thenn=1 and we deriveV,H`e v,H0|(1, 0) immediately from the corresponding evaluation rule.

Ifeis a pattern match, a function application, a conditional, or a let expression then we use the induction hypothesis. Since the other cases are similar, we provide the argument only for the case whereeis a let expressionletx=e1ine2. ThenV,H`e |n

was derived via P:LET1 or P:LET2. In the case of P:LET1 it follows thatV,H`e1 |n−1.

By the induction hypothesis we conclude that eitherV,H`e1 |norV,H`e1 v1,H1|(n−1, 0). In the first case we can use P:LET1 to deriveV,H`e |n+1. In the second case it follows from Theorem 3.3.4 thatHV :ΓandHv1:Aand thus H1ÍV[x7→v1]:Γ,x:A. We then apply Lemma 3.3.8 to obtainV[x7→v1],H1`e2 |1. Therefore we can apply P:LET2 to deriveV,H`e |n+1.

Assume now thatewas derived by the use of P:LET2. Then it is true thatV,H`

e1 v1,H1|(n1, 0) andV[x7→v1],H1`e2 |n2for somen1,n2withn1+n2+1=n. From Theorem 3.3.4 it follows that H1ÍV[x7→v1]:Γ,x:A. Therefore we can apply the induction hypothesis to infer thatV[x7→v1],H1`e2 v2,H2|(n2, 0) orV[x7→ v1],H1`e2 |n2+1. In the first case we apply E:LETand deriveV,H`e v2,H2| (n, 0). In the second case we apply P:LET2 and deriveV,H`e |n+1.

Cost-Free Metric

The type inference algorithm makes use of thecost-freeresource metric. This is the metric in which all constantsKthat appear in the rules are instantiated to zero. I use it in

Chapters 5 and 6 to define a resource-polymorphic recursion that uses cost-free function types to pass potential from the argument to the result. The following proposition can be proved analogous to Proposition 3.3.6.

Proposition 3.3.10 Let all resource constantsKbe instantiated byK=0. IfV,H`e v,H0|(q,q0) thenq=q0=0. IfV,H`e |qthenq=0.

Keynote address at the ACM Symposium on Applied Computing (1999)

4

Linear Potential

Hofmann and Jost introduced linear automated amortized analysis in 2003 to analyze the heap-space consumption of first-order functional programs. As I am writing this thesis, their work [HJ03] has been cited more then 200 times1and has been devel- oped further in several directions. Linear amortized analysis has been applied to ana- lyze object-oriented programs [HJ06, HR09], to compute bounds for generic resources [JLH+09, Cam09], to analyze polymorphic and higher-order programs [JHLH10], and to analyze Java-like bytecode by means of separation logic [Atk10].

In this chapter I present a linear amortized analysis system for generic resources, following [JLH+09]. It is the basis of the polynomial analysis systems that I develop in the following two chapters and introduces many concepts that are used there. An informal introduction to linear amortized analysis can be found in Section 2.2.1.

The chapter is organized as follows. In Section 4.1, I define linear resource-annotated data types and the potential functions that the annotations represent. I then, in Sec- tion 4.2, introduce type judgments that constitute resource bounds together with type rules to derive the judgments for RAML programs. In Section 4.3, I prove the soundness of the type system. It states that derived type judgments constitute correct bounds. Section 4.4 explains how the type analysis can be automated through an inference of the type derivations. Finally, Section 4.5 demonstrates the analysis on several example programs.

4.1 Resource Annotations

The first step in the design of an automatic amortized analysis is to choose a set of potential functions. In this chapter, I use potential functions that are linear in the size of the data in the memory.

1according to Google Scholar

To represent the linear potential functions in the type system, types of inductive data structures are annotated with non-negative rational numbers2q∈Q+0. The following

EBNF grammar defines the(linear) resource-annotated data typesof RAML.

A::=unit|bool|int|Lq(A)|Tq(A)|(A,A)

LetAlinbe the set of linear resource-annotated data types. LetA∈Alinbe an annotated data type. As in Section 3.2, I write‚Aƒfor the set of semantic values of typeA. For instance,‚Lq(int)ƒis the set of (finite) lists of integers. Similarly, we extend all other definitions—such asHÍv7→a:A andHÍv:A—for simple data types to resource- annotated data types by ignoring the resource annotations.

LetA∈Alinby a resource-annotated data type and leta∈ ‚Aƒ. ThepotentialΦ(a:A) ofa under typeAis defined as follows. Recall from Section 3.2 that elems(t) are the elements of the treet∈ ‚T(A)ƒin pre-order.

Φ(a:A)=0 ifA∈{unit,int,bool}

Φ(a:(A1,A2))=Φ(a1:A1)+Φ(a2:A2) ifa=(a1,a2) Φ(`:Lq(B))=q·n+ X i=1,...,n Φ(ai:B) if`=[a1, . . . ,an] Φ(t:Tq(B))=q·n+ X i=1,...,n Φ(ai:B) if elems(t)=[a1, . . . ,an]

LetA∈Alin, letH be a heap, and letvValbe a value such thatHÍv7→a:A. The potentialΦH(v:A) ofvunder typeAinHis then defined asΦH(v:A)=Φ(a:A).

In the following I will sometimes explain an idea by talking about the potential

Φ(x:A) of a variablexwith respect to an annotated typeA. In such a case I mean in fact the potentialΦH(V(x):A) with respect to a stackV and a heapHthat I do not want to describe precisely.

Lemma 4.1.1 states some facts about the potential of a value without referring to the corresponding semantic value. These facts can also be used to define the potential functionΦ.

Lemma 4.1.1 LetA∈Alin, let Hbe a heap and letvValbe a value withHÍv:A. Then the following is true.

1. ΦH(v:A)=0 ifv=NULLor ifA∈{int,unit,bool} 2. ΦH((v1,v2):(A1,A2))=ΦH(v1:A1)+ΦH(v2:A2)

3. ΦH(`:Lq(B))=qH(v1:B)+ΦH(`0:Lq(B)) ifH(`)=(v1,`0).

4. ΦH(`:Tq(B))=qH(v1:B)+ΦH(`1:Tq(B))+ΦH(`2:Tq(B)) ifH(`)=(v1,`1,`2) 2The use of rational rather than natural numbers in the potential annotations leads to more precise

PROOF 1. SinceHÍv:A, we haveHÍv7→[] :L(A0) ,HÍv7→leaf:T(A0) ,HÍv7→

n:int,HÍv7→() :unit, orHÍv7→a:boolfora∈{True,False}. Then the claim

follows from the definition ofΦ.

2. It follows from definition thatHÍv7→(a1,a2) : (A1,A2),H Ív17→a1:A1, and

HÍv27→a2:A2. The claim is thus a direct consequence of the definition ofΦ. 3. From rule V:CONSwe conclude thatHÍv17→a1:B,HÍ`07→[a2, . . . ,an] :L(B)

andHÍ`7→[a1, . . . ,an] :L(B). ThenΦH(`:Lq(B))=qn+P1≤inΦ(ai:B)=(q+

Φ(a1:B))+(q(n−1)P

2≤inΦ(ai:B))=qH(v1:B)+ΦH(`0:Lq(B).

4. The proof is similar to the list case. In addition, one has to use the fact that elems(tree(a,t1,t2))=[a,a1, . . . ,am,b1, . . . ,bm] where elems(t1)=[a1, . . . ,am] and

elems(tf)=[b1, . . . ,bm]. ■

For instance, we haveΦ([b1, . . . ,bn] :Lq(bool)=q·nfor a list [b1, . . . ,bn] of Booleans. Similarly, we have for a list of lists of Booleans thatΦ([[b11, . . . ,b1,m1], . . . , [bn1, . . . ,bnmn]] : Lq(Lp(bool))=q·n+p·(m1+ · · · +mn). Note that potential functions incorporate the length of each individual inner data structure. This is an important property that enables the precise analysis of nested data structures.

The Subtyping Relation

Intuitively, it is true that a resource-annotated data typeAis a subtype of a resource- annotated data typeBif and only ifAandBhave the same set‚Aƒof semantic values, and for every valuea∈ ‚Aƒthe potentialΦ(a:A) is greater or equal than the potential of

φ(a:B). More formal, we define<: to be the smallest relation such that the following is