Lectures onType Theory
Chapter 178
Chapter 178Optional

Error Credits and Approximate Higher-Order Reasoning

Prerequisites. Direct starred prerequisites: chapter 45 and chapter 163. No later core chapter depends on this route.

Consider the program e  let n=rand3 in if n1 then tt else e,e  let k=rand1 in if n+k2 then tt elif n+k=3 then ff else Ω, where randN draws uniformly from {0,,N} and Ω diverges. Enumerating the tree: n{0,1} with probability 1/2 returns tt; n=2 with probability 1/4 splits into tt and ff, each of probability 1/8; n=3 with probability 1/4 splits into ff and divergence, each 1/8. So Pr[e returns tt]=58,Pr[e returns ff]=14,Pr[e diverges]=18.

Now try to prove the bound 1/4 by a rule that charges a fixed error at each sampling point. The first draw has four outcomes, two of them leading away from tt, so a branch-insensitive rule charges 1/2; the second draw has two outcomes, one of them bad, so it charges another 1/2; the total is 1, which asserts nothing. The loss is exactly the information that the error required by the continuation depends on the value drawn: after n1 no error is needed at all, and after n{2,3} an error of 1/2 is needed. Averaging those four values gives 1/4, the true bound.

What must therefore be added to a program logic is a quantity that can be split, spent, and made to depend on an intermediate value — in short, an owned resource. This chapter takes error probability to be such a resource, proves the rules that govern it, and states the adequacy theorem that connects a derivation to the probabilities enumerated above. Because the resource is owned rather than global, the rules compose across function calls and stored higher-order code, which is what a fixed per-call budget cannot do.

Convention 178.1 — The Eris system card

The language is λrefrand: an untyped call-by-value λ-calculus with pairs, sums, recursion, a higher-order store with allocation, dereference and assignment, and the sampling primitive randN for NN, whose reduction chooses uniformly among {0,,N}. Its operational semantics is a step-indexed distribution over configurations; Prexece[φ] denotes the probability that e terminates in a value satisfying the meta-level predicate φ, and a configuration with no reduction and no value is stuck.

The logic is an Iris-style higher-order separation logic over that language: assertions form a step-indexed BI algebra with the separating conjunction , the persistence and later modalities, ghost ownership ownγ(a) over a resource algebra, invariants, and the fancy update; a Hoare triple {P}e{φ} is defined from a weakest precondition WPe{φ} whose sampling clause is a graded lifting modality carrying an error budget. The new assertion is the error credit E(ε) for ε[0,1].

From chapter 45 this chapter imports the higher-order ghost-state interface: resource algebras, the ownership assertion, the frame-preserving update, and the invariant rules. From chapter 163 it imports the guarded, step-indexed setting in which the weakest precondition is defined by guarded recursion. From chapter 172 it uses PMK-bind and PMK-lim: bind of subprobability kernels, and the limit facts proposition 172.24, proposition 172.25 used to define the execution distribution as a supremum of finite unfoldings. No result of chapter 176 or chapter 177 is imported, and none is strengthened by anything proved here.

Error as an owned resource

Definition 178.2 — The credit interface

The assertion E(ε), for ε[0,1], satisfies E(ε1)E(ε2)⊣⊢E(ε1+ε2),E(ε1)(ε2<ε1)IE(ε2),E(1)IFalse.

The three laws are what make the resource behave like error probability. Splitting is additivity of the error budget; weakening discards budget that a proof does not need; and owning a full unit of error is a contradiction, because a claim holding with probability at least 0 says nothing, so any branch that would need a full credit may be discarded outright.

Definition 178.3 — The sampling rules

1N+1i=0NE2(i)=ε1
{E(ε1)} randN {n. E(E2(n))}
ht-rand-exp
{E(length(xs)/(N+1))} randN {n. nxs}
ht-rand-err

together with the standard structural rules

{P}e{Q}
{PR}e{QR}
ht-frame
{P}e{v.Q}v. {Q}K[v]{R}
{P}K[e]{R}
ht-bind

where E2:{0,,N}[0,1] and K is an evaluation context.

The first rule is the one the opening calculation demanded: the credit required before the draw is the average of the credits required after it, so the budget may be distributed unevenly across outcomes. The second is derived, and its derivation shows the third law of definition 178.2 doing work.

Proof of Lemma 178.4 — Avoiding a list of outcomes

Proof. Apply ht-rand-exp with E2(i)=1 if ixs and E2(i)=0 otherwise. Its side condition computes 1N+1iE2(i)=length(xs)/(N+1), which is the precondition of ht-rand-err. The postcondition is n. E(E2(n)). In the case nxs the postcondition is E(1), which entails False by the third law and hence entails nxs; in the case nxs the postcondition already gives nxs. Combining the two cases yields {E(length(xs)/(N+1))} randN {n. nxs}. ◻

Example 178.5 — The opening bound, derived

Prove {E(1/4)} e {φ} with φ the postcondition “returns tt”. First, for the subprogram e under the assumption n=2 or n=3, prove {(n=3n=2)E(1/2)} e {φ}: the draw rand1 has two outcomes, exactly one of which leads away from tt — for n=2 it is k=1, for n=3 it is k=0 — so lemma 178.4 with a singleton xs spends 1/2 and rules that outcome out.

Second, for e itself, take E2(i)={0i<2,1/2i{2,3},14i=03E2(i)=14(0+0+12+12)=14, so ht-rand-exp gives {E(1/4)} rand3 {n. E(E2(n))}, and ht-bind composes it with the two continuations: for n1 the continuation returns tt with no credit, and for n{2,3} it is the triple for e just proved. The derived bound 1/4 equals the enumerated probability Pr[e returns ff] exactly; the divergence probability 1/8 is not charged, which is what makes this a partial-correctness statement.

Exercise 178.1

★☆☆ Using definition 178.2, prove E(1/2)IE(1/4)E(1/4) and E(1/3)E(1/3)E(1/3)IFalse.

Exercise 178.2

★★☆ Recompute example 178.5 with the branch-insensitive rule that charges length(xs)/(N+1) at every draw with the same xs for all outcomes, and show that the derived bound is 1. Identify the exact place where the value dependence is lost.

Exercise 178.3

★★☆ State and derive the analogue of lemma 178.4 in which outcomes in xs need not be excluded but require credit 1/2 each, and compute the resulting precondition for N=3 and |xs|=2.

The finite fragment, with a proof

The adequacy theorem of section 178.3 is a statement about the full language with a higher-order store. For the fragment in which the opening example lives — finitely branching sampling, no store, no recursion except the constant Ω — the same statement can be proved directly, and the proof shows which property of the credit rules carries the bound.

Definition 178.6 — Finite credit derivations

Let the finite fragment have expressions Ω, values v, randN, and letn=e1ine2. Its execution distribution exece over values, defined by the finite tree of draws, assigns mass to values and leaves the divergence mass unassigned. A credit derivation of εe:φ is generated by

φ(v)
0v:φ
F-Val
0Ω:φ
F-Loop
εe:φεε1
εe:φ
F-Weak
(εie2[i/n]:φ)0iNε=1N+1iεi
εletn=randNine2:φ
F-Bind
1e:φ
F-Fail

Theorem 178.7 — Adequacy for the finite fragment

If εe:φ then Prexece[¬φ]ε, where the probability is of terminating in a value violating φ.

Proof of Theorem 178.7 — Adequacy for the finite fragment

Proof. Induction on the derivation.

F-Val. execv=δv and φ(v) holds, so the probability of ¬φ is 0.

F-Loop. execΩ assigns no mass to any value, so the probability is 0.

F-Weak, F-Fail. Monotone in ε, and every probability is at most 1.

F-Bind. The execution distribution of the sampling let is the uniform mixture 1N+1iexece2[i/n], by the sampling rule of the operational semantics and example 172.26. Hence Prexece[¬φ]=mixture1N+1iPrexece2[i/n][¬φ]=IH,monotonicity1N+1iεi=ε. ◻

Example 178.8 — The checker's two verdicts

The derivation of example 178.5, transcribed into definition 178.6, uses F-Bind at the outer draw with ε0=ε1=0 and ε2=ε3=1/2, giving ε=1/4; at each inner draw it uses F-Bind with one branch of cost 0 and one of cost 1, giving 1/2. By theorem 178.7 the bound 1/4 is sound. It is also least: the enumerated probability of returning ff is exactly 1/4, so no ε<1/4 can be derivable for this e and φ without contradicting the theorem.

The model and the adequacy theorem

Definition 178.9 — The credit resource algebra

Let the carrier be R0 with addition as composition and no unit element beyond 0; the authoritative construction of chapter 45 then provides elements εtot and ε, with validity of εtotε requiring εεtot. Define E(ε)=ownγerr(ε), for a fixed ghost name allocated once.

Proof of Lemma 178.10 — The interface laws hold in the model

Proof. Splitting is the equation ε1ε2=ε1+ε2 in the resource algebra, which is the additivity of composition, together with the rule that ownership of a composite is the separating conjunction of the ownerships. Weakening is a frame-preserving update from ε1 to ε2 for ε2<ε1: any frame f compatible with the former, meaning ε1+fεtot, is compatible with the latter. For the third law, the weakest precondition is defined so that the authoritative element never exceeds 1; owning 1 together with the authoritative element forces εtot1 and leaves no room for the invariant’s own share, so the assertion is inconsistent. ◻

Convention 178.11 — Imported adequacy

The following are imported at exactly these signatures.

Aguirre et al.’s Theorem 6, adequacy for partial Eris: if {E(ε)}e{φ} then Prexece[¬φ]ε, and moreover the probability that e gets stuck is at most ε, so e is safe with probability at least 1ε. Neither bound is 0: a diverging trace satisfies every partial specification, and ε bounds the probability of terminating outside φ.

Their Theorem 7, adequacy for total Eris: if E(ε)eφ in the total logic then Prexec(e,σ)[φ]1ε for every initial state σ.

Their Theorem 8: let ε0; if for all ε>ε the total triple E(ε)eφ is derivable, then Prexec(e,σ)[φ]1ε. The quantifier over ε is in the meta-logic, and the passage from the family of triples to the single conclusion is a continuity argument there, not a rule of the object logic.

Theorem 178.7 is the finite instance proved locally; the imports supply the statements for the full language with higher-order store.

Example 178.12 — Partial and total budgets differ

For the opening program, the partial bound is εp=1/4, charging only the terminating runs that return ff. The total bound is εt=3/8, charging in addition the divergent branch of probability 1/8, since a total triple asserts termination with probability at least 1εt. Indeed Pr[e returns tt]=5/8=13/8. The two numbers are different statements about the same program, and neither follows from the other: εp cannot be strengthened to 0 merely because the program is almost surely correct on its terminating runs, and εt cannot be lowered to εp without proving termination.

Remark 178.13 — Mechanization

A Coq development accompanies the source and is pinned in the reference library; it contains the resource algebra of definition 178.9, the weakest precondition with its graded lifting, the rules of definition 178.3, and the three theorems of convention 178.11. Replaying it establishes that those statements have machine-checked proofs against the displayed definitions; it does not establish anything about definition 178.6, which is this book’s own fragment, nor does it substitute for theorem 178.7. When a rule of definition 178.3 is said to be checked, the claim is about a named lemma of that development.

Remark 178.14 — Boundary

Error credits bound the probability of an incorrect outcome. They are not expected-cost potential: the credit algebra of definition 178.9 is additive in a quantity bounded by 1, while the potential of chapter 177 is an unbounded nonnegative quantity paying for ticks, and the two soundness statements bound different things — a probability there, an expectation here. A comparison between them is a translation to be proved, not an identification; no rule of one system is imported into the other in this book. Likewise, Theorems 7 and 8 of convention 178.11 are a terminal extension: nothing in chapter 176 or chapter 177 is strengthened by them.

Exercise 178.4

★☆☆ Check the frame-preserving update in lemma 178.10 explicitly for ε1=1/2, ε2=1/4, and a frame f=1/4.

Exercise 178.5

★★☆ Give a program that diverges with probability 1 and prove {E(0)}e{φ} for every φ in the finite fragment, using definition 178.6. State which clause of theorem 178.7 makes this sound and why the corresponding total statement fails.

Exercise 178.6

★★☆ Prove that 1/4 is the least derivable ε for the opening program and postcondition, by combining theorem 178.7 with the exact enumeration.

Suggested first pass.

None of these problems is a prerequisite for a later chapter. Begin with exercise 178.7, then exercise 178.8, then the practical project exercise 178.11.

Exercise 178.7

★☆☆ Draw the complete execution tree of the opening program with every edge labelled by its probability, and verify the three numbers 5/8, 1/4, 1/8.

Exercise 178.8

★★☆ Write out the F-Bind case of theorem 178.7 for N=1, replacing the mixture identity by an explicit two-term computation, and identify where the induction hypothesis is applied.

Exercise 178.9

★★☆ Show that F-Bind with the average replaced by the maximum is sound but derives 1/2 for the opening program, and that with the average replaced by the minimum it is unsound, by exhibiting a program and a false bound.

Exercise 178.10

★★☆ Formulate a total-correctness variant of definition 178.6 in which F-Loop requires ε=1, prove the corresponding adequacy statement Prexece[φ]1ε, and derive 3/8 for the opening program.

Exercise 178.11

★★★ Practical project.error-credit-arithmetic-checker Build a certificate checker for the finite credit arithmetic of definition 178.6. A certificate is a tree of the five rules; the checker recomputes each rule’s side condition in exact rational arithmetic and reports either the derived bound or the first violated side condition. A second component enumerates the program tree exactly, reporting the three probabilities of returning a value satisfying the postcondition, returning one that violates it, and diverging.

The invariant to maintain is that every credit is a reduced rational in [0,1], that F-Bind recomputes the average rather than accepting a supplied value, and that the enumerator’s three probabilities sum to exactly 1.

The concrete result is the pair of verdicts for the program of the chapter opening. The acceptance test is decidable and exact: the checker must accept the partial certificate at ε=1/4; it must reject every strictly smaller rational bound for the same certificate, reporting the recomputed average 1/4; it must accept the total certificate of exercise 178.10 at ε=3/8 and reject it at 1/4; and the enumerator must report exactly 5/8, 1/4, 1/8. Replaying the pinned Coq development is separate mechanization evidence for convention 178.11; the arithmetic checker establishes theorem 178.7 for the certificates it accepts and does not prove adequacy for the full language.

Search the book

Type to search the local edition.