Lectures onType Theory
Chapter 176
Chapter 176Optional

Probabilistic Program Logics

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

Let bad1 be the program bcoin(1/4); return b, where tt means failure, and let bad2 run two independent copies and report whether either failed. Exact enumeration gives Pr[bad1]=14,Pr[bad2]=1(34)2=716. Enumeration also gives the answer for three copies, and for four, and for a program whose two copies are drawn from a continuous distribution it gives nothing at all: the state space is uncountable, and the four cases become an integral.

What survives all these changes is an inequality and the way it was obtained: the probability of a disjunction is at most the sum of the probabilities, so Pr[bad2]Pr[bad1]+Pr[bad1]=1/2. That step used nothing about the value 1/4, nothing about the number of copies, and nothing about the type of the sampled value. A logic is what remains when such a step is separated from the program it was applied to: judgments that record a bound, rules that compose bounds, and a soundness theorem tying the rules to the measures of chapter 174.

The obstruction that organizes the chapter is that a denotational equality between whole programs, which is what the preceding chapters prove, does not compose with a bound. Knowing [[e1]]=[[e2]] says nothing about Pr[e1 fails], and knowing a bound for a subprogram says nothing about the whole unless there is a rule for the construct that joins them. The chapter builds those rules for a fixed language, proves them sound against the quasi-Borel semantics, and then applies them to three programs whose side conditions are displayed at the point of use.

Convention 176.1 — Imported interface

This chapter uses the quasi-Borel category and its probability monad from chapter 174proposition 174.8, proposition 174.10, theorem 174.16, proposition 174.17 — and, through it, the integration and kernel facts PMK-map and PMK-bind of convention 172.27. It does not use the inference transformations of chapter 175.

From one bound to a judgment

Definition 176.2 — The programming fragment

Fix the call-by-value fragment HPProg with types τ::=12Rτ×τττM[τ], where M[τ] is the type of distributions over τ; terms include variables, abstraction, application, pairing, projections, real constants, coin(p) for rational p[0,1], returne, and xe1;e2. A term of type M[τ] denotes an element of P[[τ]] in the sense of definition 174.12, and a term of a first-order type denotes a point.

Definition 176.3 — Assertions

Assertions φ are built from equalities and inequalities between enriched expressions, which extend the terms of definition 176.2 by two families: for d of type M[τ] and a predicate p over τ, Prxd[p(x)] of type R, and for h of type τR, Exd[h(x)] of type R. Assertions are closed under , ¬, and quantification over a type. Their meaning is the evident predicate on the denotation of the context, with Prxd[p(x)]=1pd[[d]],Exd[h(x)]=[[h]]d[[d]], both integrals in the sense of definition 174.12.

Definition 176.4 — The three judgments

ΓΨPLφ asserts that φ holds in every environment satisfying the assumptions Ψ. ΓΨUPLe:τφ asserts the same for φ with a distinguished variable r bound to the value of e. ΓΨRPLe1:τ1e2:τ2φ asserts it for φ with two distinguished variables r1,r2 bound to the values of e1 and e2.

The unary and relational judgments are notation for the assertion judgment, not separate logics; the following equivalence is the precise form of that claim and is the reason a single soundness theorem suffices.

Proposition 176.5 — Interderivability

ΓΨUPLe:τφ is derivable if and only if ΓΨPLφ[e/r] is derivable, and ΓΨRPLe1:τ1e2:τ2φ is derivable if and only if ΓΨPLφ[e1/r1][e2/r2] is.

Proof of Proposition 176.5 — Interderivability

Proof. Each unary rule is, by construction, the assertion rule obtained by substituting the subject expression for r; the derivations are in bijection, and the substitution is meaningful because definition 176.3 allows enriched expressions inside assertions. The relational case substitutes both subjects. This is Sato et al.’s Theorem 6.1 and its relational counterpart Theorem 6.2, at their signatures. ◻

Rules for the fragment, and their soundness

Definition 176.6 — Probabilistic rules

The rules below are stated for the fragment of definition 176.2; every premise is a judgment of definition 176.4 and every side condition is displayed. In Choice, ep abbreviates the biased branch (bcoin(p); if b then e1 else e0).

ΓΨPLφ[δe/r]
ΓΨUPLreturne:M[τ]φ
Ret
p[0,1]Q
ΓΨUPLcoin(p):M[2]Prxr[x]=p
Coin
ΓΨUPLe1:M[τ1]φ1Γ,x:τ1Ψ,φ1[x/r]UPLe2:M[τ2]φ2
ΓΨUPL(xe1;e2):M[τ2]Pryr[q(y)]supxPrye2[q(y)]
Bind
ΓΨUPLe0:M[τ]Prxr[q(x)]a0ΓΨUPLe1:M[τ]Prxr[q(x)]a1
ΓΨUPLep:M[τ]Prxr[q(x)]pa1+(1p)a0
Choice
ΓΨUPLe:M[τ]Prxr[q1(x)]a1ΓΨUPLe:M[τ]Prxr[q2(x)]a2
ΓΨUPLe:M[τ]Prxr[q1(x)q2(x)]a1+a2
Union
ΓΨUPLe:τφΓ,r:τΨPLφφ
ΓΨUPLe:τφ
Weaken

Theorem 176.7 — Soundness for the fragment

Every rule of definition 176.6 is sound for the interpretation of definition 176.3: if the denotations of the premises hold in an environment satisfying Ψ, so does the denotation of the conclusion.

Proof of Theorem 176.7 — Soundness for the fragment

Proof. Fix an environment satisfying Ψ and write μ=[[e]] for the relevant subject.

Ret. [[returne]]=δ[[e]] by definition 174.14, which is what the premise assumes about r.

Coin. Prxcoin(p)[x]=1{tt}d(pδtt+(1p)δff)=p, by lemma 172.13.

Choice. The subject denotes p[[e1]]+(1p)[[e0]] by the bind clause and lemma 172.15; integrating 1q against a finite mixture is the corresponding combination of the integrals, by example 172.26. The premises bound the two integrals by a1 and a0, and the combination is monotone in each, so the conclusion follows.

Union. Pointwise 1q1q21q1+1q2, so by monotonicity and additivity of the integral (lemma 172.13, convention 172.12) Pr[q1q2]Pr[q1]+Pr[q2]a1+a2. The two premises are about the same subject e, which is what makes the sum meaningful; nothing about independence is used or needed.

Bind. By lemma 172.19, Pry(xe1;e2)[q(y)]=Prye2[q(y)]d[[e1]](x), and an integral of a function bounded by c against a subprobability measure is at most c; here c is the displayed supremum, which is finite because probabilities are bounded by 1. The second premise is what makes Prye2[q(y)] a function of x satisfying the assumption φ1[x/r] at [[e1]]-almost every x.

Weaken. Immediate from the interpretation of implication as inclusion of predicates. ◻

Example 176.8 — The opening bound, derived

Write bad2=(b1coin(1/4); b2coin(1/4); return(b1b2)). By Coin, Pr[bi]=1/4 for each copy; by Bind and Weaken each copy contributes Prxr[qi(x)]1/4, where qi is the predicate that the i-th draw is tt; by Union, Prxr[q1q2]1/2. The derived bound is 1/2 and the exact value, by enumeration, is 7/16. The gap is the price of a rule that never mentions independence: the same derivation applies verbatim when the two draws are dependent, and there 7/16 would be false.

Convention 176.9 — The Sato et al. semantic core, imported

The general soundness theorem is imported at exactly this signature. Formulas are interpreted as objects of Pred(QBS), the category of predicates on quasi-Borel spaces, obtained as the change of base of the predicate fibration along the forgetful functor QBSSet; a well-formed formula Γφ denotes a subset of [[Γ]], with conjunction as intersection, negation as complement, and universal quantification as an intersection over the interpretation of the bound type. Sato et al.’s Theorem 7.4: if ΓΨPLφ is derivable then ψΨ[[Γψ]][[Γφ]], equivalently the identity on [[Γ]] is a morphism [[ΓψΨψ]][[Γφ]] in Pred(QBS). Their Corollary 7.5 and Corollary 7.6 derive the unary and relational statements from it through the interderivability of proposition 176.5. Their proof of the axioms uses the isomorphism M1[0,], the commutativity of the monad — proved here as proposition 174.17 — the agreement of the monadic integral with Lebesgue integration, and the fact that measurable maps between standard Borel spaces are exactly the quasi-Borel morphisms between them. Theorem 176.7 is the fragment instance proved locally; the import supplies the general case, including the higher-order rules not displayed in definition 176.6.

Exercise 176.1

★☆☆ Derive Pr[bad1]1/4 with Choice from the two trivial bounds 0 and 1, and compute the resulting arithmetic.

Exercise 176.2

★☆☆ Exhibit a program for which the Union bound is attained exactly, and one for which it exceeds the true probability by 1/16, as in example 176.8.

Exercise 176.3

★★☆ In Bind, replace the supremum over x by the value at one particular x and exhibit a two-point example where the resulting rule is unsound. State which step of the proof of theorem 176.7 fails.

Three applications, with their side conditions

Slicing a probabilistic program

Proposition 176.10 — Removing an unused draw

Let Γe1:M[τ1] and Γe2:M[τ2], and let Γ,y:τ2e:M[σ] with x not free in e. Then [[xe1; ye2; e]]=[[ye2; e]] whenever [[e1]] is a probability measure, that is, of total mass 1.

Proof of Proposition 176.10 — Removing an unused draw

Proof. By proposition 174.17 the two binds may be exchanged, so the left-hand side equals [[ye2; xe1; e]]. Since x is not free in e, the inner bind is [[e1]]=λx.[[e]] with a constant integrand, whose value at a measurable set A is [[e]](A)[[e1]](whole space)=[[e]](A) by lemma 172.13 and the mass hypothesis. ◻

Remark 176.11 — The hypothesis is not decorative

If e1 contains a score, its denotation has mass different from 1 and the conclusion is false by exactly that factor: with e1=score(2), the left-hand side is twice the right-hand side. Slicing is therefore a statement about the sublanguage without scoring, or about programs whose sliced part is proved to have unit mass.

A sample-size bound for importance sampling

Lemma 176.12 — Markov and Chebyshev

Let μ be a probability measure on X and f:X[0,] measurable. Then μ({fa})1afdμ for every a>0. Consequently, if g:XR has mean m=gdμ and variance σ2=(gm)2dμ<, then μ({|gm|ε})σ2/ε2 for every ε>0.

Proof of Lemma 176.12 — Markov and Chebyshev

Proof. Pointwise a1{fa}f, so integrating and using monotonicity and homogeneity (lemma 172.13, convention 172.12) gives aμ({fa})fdμ. For the second claim apply the first to f=(gm)2 and a=ε2, noting {|gm|ε}={(gm)2ε2}. ◻

Proposition 176.13 — Monte Carlo sample size

Let d be a probability measure, let h be measurable with μ=Exd[h(x)] and σ2=Varxd[h(x)]<, and let mcn be the program that draws x1,,xn independently from d and returns 1nih(xi). Then for every ε>0, Prvmcn[|vμ|ε]σ2nε2, so nσ2/(δε2) gives the bound δ.

Proof of Proposition 176.13 — Monte Carlo sample size

Proof. The denotation of mcn is the pushforward of the n-fold product dn along λx.1nih(xi), by lemma 172.22 and proposition 174.17, which permits the n draws to be performed in any order. Its mean is μ, by linearity of the integral and Tonelli, and its variance is σ2/n: expanding (1ni(h(xi)μ))2ddn by linearity gives n diagonal terms σ2/n2 and n(n1) off-diagonal terms, each of which factors by Tonelli into 1n2((hμ)dd)2=0. Now apply lemma 176.12 with g the identity on the pushed-forward measure. ◻

Example 176.14 — The bound as a judgment

In the notation of definition 176.4, the proposition is the judgment (Exd[1]=1), (σ2=Varxd[h(x)]), (μ=Exd[h(x)]), (ε>0) UPL mcn:M[R]Prvr[|vμ|ε]σ2nε2. The four assumptions are exactly the hypotheses used in the proof: total mass one, finite variance, the value of the mean, and positivity of ε. Dropping the finiteness of σ2 makes the right-hand side meaningless; dropping the mass condition makes the mean computation false.

Convergence of a mean-learning program

Convention 176.15 — Imported application

The third reconstructed application is Sato et al.’s analysis of Gaussian mean learning, imported at its signature. Let the prior on the unknown mean be normal with mean m0 and variance v0, let each observation be normal about the unknown mean with known variance v, and let learnn be the program returning the posterior after n observations. Their development derives, in UPL, that the posterior mean after n observations is the stated weighted average of m0 and the empirical mean, that the posterior variance is (1/v0+n/v)1, and hence that the posterior variance converges to 0 at rate v/n; the stability statement bounds the change in the posterior under a perturbation of one observation by a quantity proportional to v0/(v0n+v). The hypotheses are that all variances are strictly positive, that the observations are conditionally independent given the mean, and that the prior is proper. Nothing in this chapter proves these; the Lipschitz analysis of generalized value iteration that the source also presents is comparison material and owns no statement here.

Remark 176.16 — The mechanization boundary

Two Isabelle developments accompany this material and their coverage is not the same as the paper’s. The Quasi-Borel Spaces entry of the Archive of Formal Proofs mechanizes quasi-Borel spaces, their products, coproducts and exponentials, and the probability monad with its laws — the content of convention 174.19 — and it does not by itself formalize conditioning. The S-Finite Measure Monad entry adds s-finite kernels and a monad supporting scoring, a richer measure interface, and still does not by itself prove every rule of definition 176.6 or the general soundness statement of convention 176.9. When a rule of the logic is said to be checked, the claim is about a named lemma of one of these entries; no claim of machine-checked status is made for the applications of section 176.3. Lilac, a separate logic for independence and conditional probability, is a comparison card: none of its rules or metatheorems is attributed to the logic of this chapter.

Exercise 176.4

★☆☆ Give the two-line calculation showing that proposition 176.10 fails for e1=score(2), and give the exact factor by which the two sides differ.

Exercise 176.5

★★☆ Exhibit a distribution and an ε for which the Chebyshev bound of lemma 176.12 is attained with equality, and one for which it overestimates by a factor of 100.

Exercise 176.6

★★☆ Carry out in full the variance computation in the proof of proposition 176.13 for n=2, displaying the diagonal and off-diagonal terms and the use of Tonelli.

Suggested first pass.

None of these problems is a prerequisite for a later chapter. Begin with exercise 176.7, then exercise 176.8, then the practical project exercise 176.11.

Exercise 176.7

★☆☆ Enumerate bad2 exactly, confirm Pr[bad2]=7/16, and compare it with the derived bound 1/2 of example 176.8.

Exercise 176.8

★★☆ Write out the Bind case of theorem 176.7 for the special case where e1 is coin(p), replacing the integral by a two-term sum, and identify the exact inequality that the supremum supplies.

Exercise 176.9

★★☆ Using proposition 176.5, translate the slicing statement of proposition 176.10 into an RPL judgment, and state the assumptions Ψ it requires.

Exercise 176.10

★★☆ Generalize example 176.8 to k independent copies: derive the bound k/4 by Union, compute the exact probability 1(3/4)k, and determine the least k for which the derived bound exceeds 1 while the exact probability does not.

Exercise 176.11

★★★ Practical project.finite-bound-certificate-checker Build a certificate checker for the finite fragment. A certificate is one of ret, bind(c1,c2), choice(p,c0,c1), weaken(q,q,c), and union(c1,c2), where every probability is a reduced rational; the checker computes the claimed bound bottom-up according to definition 176.6 and compares it with the requested bound using integer arithmetic only. A second component enumerates the program exactly and reports its true probability.

The invariant to maintain is that no floating-point number appears anywhere and that a certificate is accepted only when every side condition of the corresponding rule is checked, including the requirement in Union that both premises concern the same subject program.

The concrete result is the pair of verdicts and the exact probability for the two programs of the chapter opening. The acceptance test is decidable and exact: the checker must accept Pr[bad1]1/4 by choice; accept Pr[bad2]1/2 by union; reject the same bad2 certificate at the bound 1/4, reporting the derived bound 1/2 and the requested bound 1/4; and the enumerator must report Pr[bad2]=7/16 exactly. Successful checking is the oracle. The certificate language is a finite executable shadow of the logic of definition 176.6; its general soundness is theorem 176.7, which the checker does not prove.

Search the book

Type to search the local edition.