Lectures onType Theory
Chapter 177
Chapter 177Optional

Expected Cost and Probabilistic Resource Analysis

Prerequisites. Direct starred prerequisites: chapter 57. No later core chapter depends on this route.

Walk down a list, and at each element flip a fair coin: on heads stop and return tt; on tails pay one unit and continue. In the notation of the system card below this is bernoulli lst= match lst with []ffhd::tlflip 12 {HttTtick1; bernoulli tl}. On a list of length n, the worst run pays n: every flip may come up tails. The expected payment is different. Element k is reached only if the first k1 flips were tails, which has probability 2(k1), and it is charged only if flip k is also tails; so E[cost]=k=1n2k=12n<1. The bound 1 holds for every n and is approached as n grows. A worst-case analysis reports n; the truth is bounded by a constant.

The gap is not repaired by replacing each branch cost with its numerical expectation. In bernoulli the tails branch pays 1 and then pays whatever the recursive call pays, so “the expected cost of this statement” is not a number available before the analysis of the rest of the program. What is available is a quantity assigned to the data: a potential that the program carries and spends. The chapter’s obstruction is therefore to make potential flow through a distribution over successor states rather than through a single successor, and to keep divergence visible in the soundness statement, since a probabilistic program may diverge with positive probability and still have finite expected cost.

Convention 177.1 — The pRaML system card

The calculus is Wang–Kahn–Hoffmann’s linear pRaML core, frozen here. Types are τ::=1boollist(A)prob{qH;qT}arr(A;B),A,B::=τ,q  (qQ0), so a potential-annotated type is a type paired with a nonnegative rational constant, and a list type list(τ,p) carries p units of potential per element. Expressions are variables, triv, nil, cons, list matching, let, application, fun, share, tick{q}, the probability value prob{p} for rational p[0,1], the probabilistic branch flip{e1;e2}(p), and the branch on a stored probability flipS(x;e1,e2). The potential of a value is Φ(v:τ,q)=q+Φ(v:τ),Φ([v1,,vn]:list(A))=iΦ(vi:A),Φ(prob(p):prob{qH;qT})=qHp+qT(1p), and Φ(V:Γ)=xdom(Γ)Φ(V(x):Γ(x)) for an environment V with V:Γ. The typing judgment Γ;qe:A reads: in context Γ with q additional units of constant potential, e has potential-annotated type A. This is not the discrete pPCF of chapter 171, and no theorem of that chapter is imported here.

Convention 177.2 — Imported interface

From chapter 172 this chapter uses PMK-bind and PMK-lim: subprobability measures, bind, and the limit facts proposition 172.24, proposition 172.25, together with example 172.26 for finite rational mixtures. All distributions below are over a countable set of pairs, so every integral is a sum. It uses no quasi-Borel structure, no program logic, and no inference transformation.

Potential that flows through a distribution

The deterministic potential method assigns to each state S a nonnegative number Φ(S) and requires, for each operation o with successor state S, (1)Φ(S)  cost(S,S)+Φ(S). Summing (1) along an execution telescopes: the initial potential bounds the total cost. When o produces a distribution over successors, the only formula that keeps the telescoping property is obtained by taking the expectation on the right: (2)Φ(S)  ESo(S)[cost(S,S)+Φ(S)]=ESo(S)[cost(S,S)]+ESo(S)[Φ(S)].

Lemma 177.3 — Two steps compose

Suppose (2) holds for o at S, and for o at every S in the support of o(S). Then Φ(S)  ESo(S),So(S)[cost(S,S)+cost(S,S)]+ESo(S),So(S)[Φ(S)].

Proof of Lemma 177.3 — Two steps compose

Proof. Substitute the hypothesis for o into the second summand of (2) and use linearity of expectation over the finite mixture, which is example 172.26: ES[Φ(S)]ES[ES[cost(S,S)]+ES[Φ(S)]], and the iterated expectation of a nonnegative quantity over the composite is the expectation over the product, by lemma 172.19. ◻

The typing rules realize (2) syntactically. Two of them carry the whole probabilistic content.

Definition 177.4 — The probabilistic rules

Write Γ(p×Γ1,(1p)×Γ2) for the sharing relation that splits the per-element potential of every binding in Γ into the two branches with the displayed weights, so that (3)Φ(V:Γ)=pΦ(V:Γ1)+(1p)Φ(V:Γ2). Then

Γ(p×Γ1,(1p)×Γ2)q=pq1+(1p)q2Γ1;q1e1:AΓ2;q2e2:A
Γ;qflip{e1;e2}(p):A
L:Flip
;qtick{q}:1,0
L:Tick
q=pqH+(1p)qT
;qprob{p}:prob{qH;qT},0
L:Prob
Γ;q+qHe1:AΓ;q+qTe2:A
Γ,x:prob{qH;qT};qflipS(x;e1,e2):A
L:FlipS

The equation q=pq1+(1p)q2 is (2) for the constant part of the potential and the sharing relation is (2) for the part carried by the data. Neither is an average of costs: the costs q1,q2 are themselves the potentials required by the branches, determined by the analysis of those branches.

Example 177.5 — The exact bound for bernoulli

Derive the typing bernoulli:list(τ,0),1bool,0, where the list carries no potential per element and one constant unit is available per call. At the branch, the heads side returns tt and needs q1=0; the tails side pays one tick and then calls bernoulli again, which needs one unit, so q2=1+1=2. The rule L:Flip requires q=120+122=1, the constant available. The derived bound on the expected cost is therefore 1, independently of the length of the list; by the opening calculation the true expected cost is 12n, so the bound is attained in the limit.

Example 177.6 — A bound that mixes constants and data

Let rdwalk consume a list of probabilities, paying one tick per iteration, and on heads at probability p push two new probabilities 1/5,2/5 onto the list while on tails pop the head. The derivable typing is rdwalk:list(prob{5;0}),11,0, whose potential on the argument [p1,,pn] is Φ([p1,,pn])=n+1in5pi, one unit per element for the tick and 5pi for the expected future work that the i-th flip may create. On the argument [1/5,2/5] this is 2+515+525=2+1+2=5. The program may fail to terminate — the head branch lengthens the list — and the bound is finite anyway; section 177.3 is where that combination is made legitimate.

Exercise 177.1

★☆☆ Compute the expected cost of bernoulli on lists of length 1,2,3 by enumerating the runs, and compare each with the derived bound 1.

Exercise 177.2

★★☆ Replace L:Flip by the rule that requires qmax(q1,q2) and show that it is sound but derives the bound 2 for bernoulli. Then replace it by qmin(q1,q2) and exhibit a program for which the resulting system derives a false bound.

Exercise 177.3

★★☆ State the sharing relation explicitly for Γ=(x:list(1,p)) and verify Φ(V:Γ)=pΦ(V:Γ1)+(1p)Φ(V:Γ2) on a list of three elements.

Two cost semantics

A soundness proof must compare a typing derivation with the aggregate behavior of all runs. The trace semantics describes one run at a time, which is the wrong shape for that comparison; the following failed attempt shows what has to be repaired.

Definition 177.7 — Trace semantics

The judgment V;σepvq says that under environment V, with σ the finite sequence of coin outcomes consumed, e evaluates to v with net cost q, and that the outcome sequence σ has probability p. Set [[e]]V(v,q)={pσV;σepσvq}, a subprobability distribution over pairs, since infinite traces contribute nothing.

Remark 177.8 — A naive distribution semantics, and why it fails

One would like an unindexed judgment Veμ with the rules Vtrivδ(,0),Ve1μ1Ve2μ2Vflip{e1;e2}(p)pμ1+(1p)μ2. The rules are true statements about terminating programs, but as an inductive definition they define nothing for a diverging one: a program whose recursive call is itself has no finite derivation, so the relation assigns it no distribution at all, not even the zero distribution. Since the whole point is to bound the expected cost of programs that may diverge, the definition must instead be approximated from below. The repair is an index: Venμ unfolds recursion at most n times and assigns the zero distribution — later, the distribution concentrated on a dummy value — to anything deeper.

Definition 177.9 — Indexed distribution semantics

The judgment Venμ assigns to each expression a distribution over Val×Q0, by the rules Vtick{q}nδ(,q),Vprob{p}nδ(prob(p),0),Ve1nμ1Ve2nμ2VflipS(x;e1,e2)n+1pμ1+(1p)μ2 with V(x)=prob(p), together with the evident rules for the other constructs, in which the let rule adds the costs of the two stages and the application rule decrements the index. At index 0 every expression receives the zero subdistribution. The two semantics agree in the limit: [[e]]V=supnμn where Venμn, and this supremum exists by proposition 172.24, because the μn increase pointwise.

Theorem 177.10 — Soundness for terminating mass

If Γ;qe:A and V:Γ, then Φ(V:Γ)+q  (v,q)[[e]]V(v,q)(Φ(v:A)+q).

Proof of Theorem 177.10 — Soundness for terminating mass

Proof. Since [[e]]V is the supremum of the μn and all summands are nonnegative, proposition 172.24 reduces the claim to: for every n, if Venμ then Φ(V:Γ)+q(v,q)μ(v,q)(Φ(v:A)+q). Induct on n, with an inner induction on the typing derivation.

Index zero. μ=0 and the right-hand side is 0Φ+q.

L:Tick. μ=δ(,q) and the right-hand side is Φ(:1,0)+q=q, which is the left-hand side because the context is empty.

L:Flip. Here μ=pμ1+(1p)μ2 with Veinμi. The inner induction hypothesis at the two premises gives Φ(V:Γi)+qiμi(v,q)(Φ(v:A)+q). Multiplying the first by p, the second by 1p, and adding, p(Φ(V:Γ1)+q1)+(1p)(Φ(V:Γ2)+q2)=(3)Φ(V:Γ)+q, while the right-hand sides combine to μ(v,q)(Φ(v:A)+q) by linearity of a finite mixture (example 172.26).

L:Let. With Γ1;qe1:τ,p and Γ2,x:τ;pe2:B, the semantics gives μ=(v1,q1)μ1(v1,q1)μ(v1,q1) shifted by the cost q1. Apply the induction hypothesis to e1, then to each e2 under the extended environment, and use lemma 177.3 with the roles of o and o played by the two stages; the additive shift of the cost is exactly the first summand there.

Application. The index decreases, so the outer induction hypothesis applies to the body under the environment extended by the argument, and L:Fun supplies the typing of that body in that context.

The remaining rules — L:Var, L:Unit, L:Nil, L:Cons, L:MatL, L:Share, L:Prob, L:FlipS, and the structural rules L:Sub, L:Sup, L:Weak, L:Relax — are formal copies of the deterministic cases with the same potential bookkeeping, because their semantics is a Dirac distribution or a rearrangement of one; each is obtained from the displayed L:Let case by deleting the mixture. ◻

Remark 177.11 — What thm:cost-soundness-basic does not say

The distribution [[e]]V of definition 177.9 ignores infinite traces: they contribute no mass. So the inequality bounds the expected cost conditioned on the terminating part, weighted by its probability, and says nothing about a program that diverges with positive probability. In particular it does not entail that a well-typed program terminates almost surely. Making divergence visible requires changing the object being approximated, which is the next section.

Divergence made visible

Definition 177.12 — Partial evaluation and its order

Extend the distributions of definition 177.9 to full probability distributions over (Val{})×(Q0{}), where is a dummy value recording an unfinished evaluation; the base rule becomes Ve0δ(,0), and the let rule propagates with the cost accumulated so far. Define μ1μ2 when v,q: μ1(v,q)μ2(v,q),andq: μ1((Val{})×[0,q])  μ2((Val{})×[0,q]). On finished values this is the pointwise order; on the dummy value it points the other way, because as evaluation proceeds an unfinished run accumulates cost and its mass migrates to larger costs.

Convention 177.13 — Imported order-theoretic lemmas

Three statements of Wang–Kahn–Hoffmann are imported at exactly these signatures.

Their Lemma 5.4: is a partial order on these distributions, and every -increasing sequence μ1μ2 has a least upper bound, written nμn.

Their Lemma 5.5: if Venμ1 and Vemμ2 with nm, then μ1μ2; consequently [[e]]V=nμn is defined and describes all executions, terminating and not.

Their Lemma 5.6: let h(μ)=qμ(,q)q+(v,q):vμ(v,q)(Φ(v:A)+q). If μ1μ2 and h(μn)M for all n, then h(nμn)M.

The third is the load-bearing one: the functional h is not known to be continuous for , and the imported statement is the weaker preservation of an upper bound, which is what the soundness proof needs.

Theorem 177.14 — Improved soundness

If Γ;qe:A and V:Γ, then Φ(V:Γ)+q  q[[e]]V(,q)q+(v,q):v[[e]]V(v,q)(Φ(v:A)+q).

Proof of Theorem 177.14 — Improved soundness

Proof. Write M=Φ(V:Γ)+q and let h be the functional of convention 177.13. By Lemma 5.5 the distributions μn with Venμn form a -increasing sequence with supremum [[e]]V, and by Lemma 5.6 it suffices to prove h(μn)M for every n. That is the same induction as in theorem 177.10, on n with an inner induction on the typing derivation, with one new case: at index 0 the distribution is δ(,0), for which h(δ(,0))=0M; and in the let case the dummy value carries the cost accumulated by the first stage, which the first summand of h charges exactly once. All other cases are unchanged, because they do not produce . ◻

Corollary 177.15 — Almost sure termination, under the tick hypothesis

Let Γ;qe:A and V:Γ, and suppose e is instrumented with ticks that charge at least one unit for each evaluation step. Then [[e]]V(,q)=0 for every qQ0{}; that is, e terminates with probability one.

Proof of Corollary 177.15 — Almost sure termination, under the tick hypothesis

Proof. For finite q: a nonterminating run performs unboundedly many steps, each charged at least one unit, so its accumulated cost exceeds every finite q; hence the limit distribution assigns no mass to (,q). For q=: by theorem 177.14 the first summand [[e]]V(,) is bounded by Φ(V:Γ)+q, which is finite; a finite bound on a product with forces the probability to be 0. ◻

Remark 177.16 — The hypothesis of cor:cost-ast is not removable

Without the instrumentation, a well-typed program may diverge with positive probability and still satisfy theorem 177.14: if the diverging runs accrue no cost, the first summand is 0 and the inequality holds vacuously for them. The program rdwalk of example 177.6 is the instructive case in the other direction: it does charge one tick per iteration, so corollary 177.15 applies and it terminates almost surely, even though the head branch lengthens the list. A finite expected cost is therefore not, by itself, a termination statement; it becomes one exactly when the cost counts steps.

Remark 177.17 — Comparison cards

Two nearby analyses are recorded as separate system cards and supply no theorem here. Avanzini–Moser–Schaper analyze expected cost for an imperative language by a modular transformation into a deterministic problem; their programs, cost model, and soundness statement are not those of convention 177.1. Wang et al.’s nondeterministic analysis admits signed costs, which invalidates the monotonicity used in definition 177.12 and therefore requires a different limit argument. Neither is obtained from the other by instantiation, and no bound derived in one system is asserted in the other.

Exercise 177.4

★★☆ Verify that of definition 177.12 is antisymmetric on distributions supported on {}×{0,1}, and exhibit two distributions that are incomparable.

Exercise 177.5

★★☆ Give a well-typed program that diverges with probability 1/2 and has a finite derived bound, compute both, and check theorem 177.14 on it term by term.

Exercise 177.6

★★☆ Give a program that terminates with probability one and has infinite expected cost, and state which hypothesis of theorem 177.14 prevents the type system from deriving a finite bound for it.

Suggested first pass.

None of these problems is a prerequisite for a later chapter. Begin with exercise 177.7, then exercise 177.8, then the practical project exercise 177.11.

Exercise 177.7

★☆☆ Write the complete typing derivation of example 177.5, displaying the instance of L:Flip and the arithmetic 1=120+122.

Exercise 177.8

★★☆ Write out the L:Flip case of theorem 177.10 in full for a concrete two-branch program, displaying the sharing equation and the mixture computation.

Exercise 177.9

★★☆ Verify Φ([1/5,2/5])=5 for the typing of example 177.6, and compute the expected cost of the first two iterations exactly, checking that it does not exceed 5.

Exercise 177.10

★★☆ Take the naive rules of remark 177.8 and a program whose body calls itself unconditionally. Show that no distribution μ satisfies the rules for it, and show that the indexed judgment of definition 177.9 assigns it the zero distribution at every index.

Exercise 177.11

★★★ Practical project.praml-linear-constraint-generator Build a linear constraint generator and solver for the fragment of convention 177.1 restricted to lists, tick, stored probabilities, flip, flipS, let, and recursive functions of one argument. The generator walks a typing derivation skeleton and emits one linear constraint per rule — the sharing equations, the mixture equation q=pq1+(1p)q2 of L:Flip, and the tick equation — over rational unknowns; the solver minimizes the constant potential subject to the constraints, in exact rational arithmetic.

The invariant to maintain is that every emitted constraint is linear with rational coefficients and that no potential unknown is ever assigned a negative value; the solver must report infeasibility rather than returning a bound when the constraints admit no nonnegative solution.

The concrete result is a derived bound for each of the two chapter programs. The acceptance test is decidable and exact: the tool must infer the bound 1 for bernoulli at input type list(τ,0), and the bound 5 for rdwalk at the argument [1/5,2/5], matching n+5ipi of example 177.6; it must report infeasibility for bernoulli when the available constant potential is fixed at 1/2; and an instrumented finite trace enumerator must confirm the expected costs 12n for n=1,2,3 as exact rationals. This checker is not the authors’ unpublished working tree, and it is not the public RaML release, which is the closest published implementation companion and includes the advertised probabilistic examples; agreement of a derived bound with either is not part of the acceptance test.

Search the book

Type to search the local edition.