• No results found

Big-step evaluation semantics

In document Space cost analysis using sized types (Page 178-183)

6.2 Operational semantics for expressions

6.2.5 Big-step evaluation semantics

We will now define an instrumented big-step evaluation relation that abstracts in- dividual reduction steps but records stack and heap usage.1 The objective is to be

able to reason about the evaluation costs compositionally, i.e. define the stack and heap costs of an expression in terms of the costs of sub-expressions. This will pave the way for the source-level analysis for stack and heap costs in Section 6.4.

The big-step judgements have the form

H, E`e⇓ru, H0, δ, γ

meaning that ereduces to uunder environmentE, heapH and region r; the final heap isH0 andδandγare, respectively, the relative stack and heap metrics used in the evaluation ofe.

First, we remark that the relative heap for a reduction sequenceσ,→→σ0is simply the difference|heap(σ0)| − |heap(σ)|because no deallocation occurs during expression reduction.

For the stack metric, however, we need to keep track of the maximum depth in all intermediate states. We do so using an instrumented version of the transitive reduction relation: σ ,→→r

M σ

0 means that M is the maximum stack depth in the

reduction sequence fromσ toσ0. This relation is formally defined by two inductive rules: M =|stack(σ)| σ,→→r M σ σ,→r σ0 σ0,→→r M0 σ 00 M = max(|stack(σ)|, M0) σ,→→r M σ 00 Using,→→r

M we now define the big-step evaluation relation.

1This can be seen as the formalisation of a simple profiler that records maximum usage for a

Definition 6.3 (Big-step evaluation semantics) The big-step evaluation rela- tion is defined by H, E`e⇓ru, H0, δ, γ def ⇐⇒ ∀C ∀S heval(e) :C, E, S, Hi,→→r M hC, E, u:S, H 0i ∧δ=M− |S| ∧γ=|H0| − |H|.

Two remarks should be made regarding this definition. First, note that the stack size of the final state is 1 +|S|; thereforeM ≥1 +|S|which implies thatδ≥1.

Second, the universal quantification in the code C and stack S makes big-step evaluation independent of context. This allows us to derive structural rules for big step evaluation with stack and heap costs (Table 6.2).

Lemma 6.4 The rules of Table 6.2 are admissible.

Proof: The proofs are straightforward but tedious. We prove the rules for the let expression, function application and case expression in detail.

Let-expression: the hypotheses of rule (6.17) are

H0, E`e1⇓ru1, H1, δ1, γ1 (6.22)

H1, E[x7→u1]`e2⇓ru2, H2, δ2, γ2 (6.23)

and we want to conclude

H0, E`letx=e1 ine2⇓ru2, H2,max(δ1,1 +δ2), γ1+γ2 (6.24)

LetCandS be arbitrary; the reduction sequence is as follow: heval(letx=e1 ine2) :C, E, S, H0i

r

,→ heval(e1) :bind(x) :eval(e2) :sret(1, E) :C, E, S, H0i

r

,→→

M1 hbind(x) :eval(e2) :sret(1, E) :C, E, u1:S, H1i r ,→ heval(e2) :sret(1, E) :C, E[x7→u1], u1:S, H1i r ,→→ M2 hsret(1, E) :C, E[x7→u1], u2:u1:S, H2i r ,→ hC, E, u2:S, H2i

Designate by δthe relative stack metric for the above evaluation. By definition of r

,→→

M, we knowM1≥ |u1:S|andM2≥ |u2:u1:S|, and therefore

δ= max{|S|, M1,|u1:S|, M2,|u2:u1:S|,|u2:S|} − |S|

H, E`n ⇓rn, H,1,0 (6.15) H, E`x⇓rE(x), H,1,0 (6.16) H0, E`e1⇓ru1, H1, δ1, γ1 H1, E[x7→u1]`e2⇓ru2, H2, δ2, γ2 H0, E `letx=e1 ine2⇓ru2, H2,max(δ1,1 +δ2), γ1+γ2 (6.17) Hn−i, E`ei⇓rui, Hn+1−i, δi, γi (1≤i≤n) Hn, [x17→u1, . . . , xn7→un]`e0⇓ru0, Hn+1, δ0, γ0 δ= max{n+ 1−i+δi}ni=1 γ= Pn i=1γi H0, E`f (e1, . . . , en)⇓ru0, Hn+1,max(δ, n+ 1 +δ0), γ+γ0 if (f (x1, . . . , xn) =e0)∈P (6.18) (∀i) Hn−i, E`ei⇓rui, Hn+1−i, δi, γi ui atr a /∈dom(Hn) aatr δ= max{n−i+δi}ni=1 γ= Pn i=1γi H0, E`c(e1, . . . , en)⇓ra, Hn[a7→ hc, u1, . . . , uni], δ, γ+ 1 +n (6.19) H0, E`e⇓ra, H1, δ1, γ1 H1(a) =hc, u1, . . . , uni (c(x1, . . . , xn)→e0)∈alts H1, E[x17→u1, . . . , xn7→un]`e0⇓ru0, H2, δ2, γ2

H0, E`caseeofalts⇓ru0, H2,max(δ1, n+δ2), γ1+γ2

(6.20)

H, E`e⇓rn, H0, δ, γ

H, E`succe⇓rn+ 1, H0, δ, γ

(6.21)

By hypotheses (6.22) and (6.23), we obtain relations between the maximum and relative stack depths for subexpressionse1 ande2:

δ1=M1− |S|

δ2=M2− |u1:S|=M2−(1 +|S|)

⇐⇒ 1 +δ2=M2− |S|

Replacing the above equalities in (6.25) we getδ= max(δ1,1 +δ2), thus establishing

the relative stack result. For the relative heap result, we remark thatγ1=|H1|−|H0|

andγ2=|H2| − |H1|and therefore

γ=|H2| − |H0|=|H1| − |H0| | {z } γ1 +|H2| − |H1| | {z } γ2 =γ1+γ2 .

Function application: the hypotheses of rule (6.18) are

Hn−i, E`ei⇓rui, Hn+1−i, δi, γi (1≤i≤n) (6.26)

Hn,[x17→u1, . . . , xn7→un]`e0 ⇓ru0, Hn+1, δ0, γ0 (6.27)

and we want to conclude

H0, E`f (e1, . . . , en)⇓ru0, Hn+1,max(δ, n+ 1 +δ0), γ+γ0 (6.28)

whereδ= max{n+ 1−i+δi}ni=1 andγ= Pn

i=1γi. LetC andS be arbitrary; the

reduction sequence for the application is as follows: heval(f (e1, . . . , en) :C), E, S, H0i

r

,→ heval(en) :. . .:eval(e1) :fbind(~x) :eval(e0) :ret(n) : [], E,hC, Ei:S, H0i

r

,→→ Mn

heval(en−1) :. . .:eval(e1) :fbind(~x) :eval(e0) :ret(n) : [], E,

un:hC, Ei:S, H1i .. . r ,→→ M2

heval(e1) :fbind(~x) :eval(e0) :ret(n) : [], E, u2:. . .:un :hC, Ei:S, Hn−1i

r

,→→ M1

hfbind(~x) :eval(e0) :ret(n) : [], E, u1:. . .:un:hC, Ei:S, Hni r ,→ heval(e0) :ret(n) : [],[~x7→~u], u1:. . .:un:hC, Ei:S, Hni r ,→→ M0 hret(n) : [], [~x7→~u], u 0:u 1:. . .:un:hC, Ei:S, Hn+1i r ,→ hC, E, u0:S, Hn+1i

The relative stack metric for the application is

δ= max{M1, M2, . . . , Mn, M0} − |S|

Using hypotheses (6.26) and (6.27), we obtain:

δi=Mi− |ui+1:. . .:un:hC, Ei:S|=Mi−(n+ 1−i+|S|) (6.30)

δ0 =M0− |u1:. . .:un :hC, Ei:S|=M0−(n+ 1 +|S|) (6.31) Isolatingδi andδ0 in the above equations yields

Mi− |S|=δi+n+ 1−i (6.32)

M0− |S|=δ0+n+ 1 (6.33) Replacing the above equations in (6.29) we get the stack result:

δ= max(max{δi+n+ 1−i}ni=1, δ0+n+ 1) (6.34)

The heap result is immediate because |Hn+1| − |H0|=|Hn+1| − |Hn| | {z } γ0 +|Hn| − |Hn−1| | {z } γn +· · ·+|H1| − |H0| | {z } γ1 =γ0+ n X i=1 γi

Case expression: the hypotheses of rule (6.20) are

H0, E`e⇓ra, H1, δ1, γ1 (6.35)

H1(a) =hc, u1, . . . , uni (6.36) (c(x1, . . . , xn)→e0)∈alts (6.37)

H1, E[x17→u1, . . . , xn7→un]`e0⇓ru0, H2, δ2, γ2 (6.38)

and the conclusion is

H0, E`caseeofalts⇓ru0, H2,max(δ1, n+δ2), γ1+γ2 (6.39)

Starting from an arbitrary codeC and stackS, the reduction of the case expression is: heval(caseeofalts) :C, E, S, H0i r ,→ heval(e) :select(alts) :C, E, S, H0i r ,→→ M1 hselect(alts) :C, E, a:S, H1i r

,→ hbind(x1, . . . , xn) :eval(e0) :sret(n, E) :C, E, u1:. . .:un:S, H1i

r ,→ heval(e0) :sret(n, E) :C, E[x17→u1, . . . , xn7→un], u1:. . .:un:S, H1i r ,→→ M2 hsret(n, E) :C, E[x17→u1, . . . , xn7→un], u0:u1:. . .:un:S, H2i r ,→ hC, E, u0:S, H2i

Using hypothesis (6.35) and (6.38) we obtain:

δ1=M1− |S|

δ2=M2− |u1:. . .:un:S|=M2−(|S|+n)

⇐⇒ n+δ2=M2− |S|

The relative stack metricδ for the case expression is then

δ= max{|S|,|S|+ 1,|S|+n, M1, M2} − |S|

= max{1, n, M1− |S|, M2− |S|}

= max{1, n, δ1, n+δ2}

= max(δ1, n+δ2)

sinceδ1≥1 and δ2≥1 (see the remark following Definition 6.3). The relative heap

result is again immediate because

γ=|H2| − |H0|= (|H2| − |H1| | {z } γ2 ) + (|H1| − |H0| | {z } γ1 ) =γ1+γ2

The proofs for the rules (6.15) (6.16) (6.21) for constants, variables and the primitive successor follow directly from the single-step reduction; we therefore omit them. The proof for the constructor application rule (6.19) is similar to the proof of rule (6.18) for function application. This concludes the proof of Lemma 6.4. Whenever we do not care about the stack and heap metrics we will omit them from the reduction relation, writingH, E`e⇓ru, H0 instead of∃δ∃γ H, E`e⇓r

u, H0, δ, γ. We will also introduce a notation for a sequence of big-step evaluations, each producing a result in a distinct region. This notation will be used for the evaluation of the right-hand of a box rule:

H1, E`(e1, . . . , en)⇓(r1,...,rn)(u1, . . . , un), Hn+1

def ⇐⇒

Hi, E`ei⇓ri ui, Hi+1∧ui atri (1≤i≤n) (6.40)

Note that the threading of the heap fromH1toHn+1in the above definition fixes the

order of evaluation frome1 to en. Since the output regionsr1, . . . , rn are distinct from each other and from the input wire regions, no real data dependency exists between the evaluations.

In document Space cost analysis using sized types (Page 178-183)