Lectures onType Theory
Chapter 179
Chapter 179Optional

Verified Compilation of Probabilistic Programs

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

The source program emix = IF Random(Bernoulli 1/3)THEN Random(UniformReal 0,1)ELSE Random(UniformReal 1,2) denotes a measure on R. An inference backend does not consume measures; it consumes densities, and it will accept the expression fmix(x)=13[0x1]+23[1x2] and evaluate it at whatever points its algorithm visits: 1/3 at x=1/2, 2/3 at x=3/2, 0 at x=1. Producing fmix from emix is a compilation, and its output is plausible. Plausible is not enough: an inference run reports numbers whatever density it is given, and a wrong density yields a wrong posterior silently.

What must be proved is an equation between two measures: the measure obtained by integrating the generated density against a fixed reference measure equals the measure denoted by the source program. Stating that equation requires a measure semantics for the source language, a notion of density with its side conditions, and a compiler whose output is a syntactic object rather than a mathematical function. The chapter builds the three, proves the soundness of the abstract compiler in its representative cases, records the exact statements of the machine-checked soundness lemmas, and keeps the executable refinement and the simplifier strictly separate from them.

Convention 179.1 — Imported interface

This chapter uses PMK-map and PMK-bind of convention 172.27: measurable spaces and maps, subprobability measures, Dirac measures, pushforward, theorem 172.14, products and Tonelli, kernels with bind, lemma 172.19, and the monad laws theorem 172.20. The density interface below is introduced here, where the source-to-target problem forces it; it is not part of PMK. No quasi-Borel structure and no conditioning result is used.

Densities, and what a compiler must prove

Definition 179.2 — Density

Let N be a measure on a measurable space X and let f:X[0,] be measurable. The measure fN is (fN)(A)=AfdN. Write hasDensity(M,N,f) for the conjunction of four statements: M=fN; M is a subprobability measure; f is measurable; and f0 on X.

Lemma 179.3 — f N is a measure, and densities compose with mixtures

For measurable f,g:X[0,] and a measure N: fN is a measure; (f+g)N=fN+gN; and (cf)N=c(fN) for c[0,).

Proof of Lemma 179.3 — f N is a measure, and densities compose with mixtures

Proof. For countable additivity of fN, let (An) be disjoint; then 1nAnf=n1Anf pointwise, and monotone convergence applied to the partial sums (convention 172.12) gives AnfdN=nAnfdN; also fdN=0. The two equations are additivity and homogeneity of the integral, applied inside A. ◻

Lemma 179.4 — Densities are unique almost everywhere

Let N be a σ-finite measure and let f,g be densities of the same measure M with respect to N. Then f=g outside an N-null set.

Proof of Lemma 179.4 — Densities are unique almost everywhere

Proof. Let A={f>g}, measurable. On a set BA of finite N-measure, B(fg)dN=M(B)M(B)=0 with a nonnegative integrand, so N(B{f>g})=0; writing A as a countable union of such B by σ-finiteness gives N(A)=0. Exchanging f and g gives N({g>f})=0. ◻

Remark 179.5 — Why uniqueness matters to a compiler

Lemma 179.4 says that a compiler may not be judged by pointwise comparison against a hand-written density: two correct outputs may differ on a null set, and a fixture that checks values at finitely many points is checking one representative. The acceptance test in this chapter’s project therefore compares a generated syntax tree against a frozen expected tree, and evaluates both at named points; neither comparison is the soundness theorem, which is an equation between measures.

The source language and its measure semantics

Convention 179.6 — The PDF source-language card

Types are t::=UNITBOOLINTREALt×t; each type t carries a stock measure stock(t): counting measure on UNIT,BOOL,INT, Lebesgue measure on REAL, and the product measure on a product type. Expressions are e::=ValvVarxe,eopeLET e IN eRandom dst eIF e THEN e ELSE eFail t, with built-in distributions dst among Bernoulli, UniformInt, UniformReal, Gaussian, and others; each has a parameter type, a result type, and a density densdst(p,y) with respect to the stock measure of its result type. An expression is deterministic when it contains neither Random nor Fail. Typing Γe:t is the evident system. The semantics [[e]]σ of a well-typed e in a state σ is a subprobability measure on stock(t), defined by [[Valv]]σ=δv,[[Varx]]σ=δσ(x),[[Failt]]σ=0,[[LET e1 IN e2]]σ=[[e1]]σ=λv.[[e2]]vσ,[[Random dst e]]σ=[[e]]σ=λp.densdst(p,)stock(t), [[IF b THEN e1 ELSE e2]]σ=[[b]]σ=λv.(if v then [[e1]]σ else [[e2]]σ), all binds being those of definition 172.16. This is the frozen source calculus; the compiler below is claimed correct for it and for nothing else.

Example 179.7 — The mixture, semantically

For emix, the scrutinee denotes 13δtt+23δff and the two branches denote the uniform measures on [0,1] and on [1,2]. By the IF clause and example 172.26, [[emix]]=13Unif[0,1]+23Unif[1,2], a probability measure. By lemma 179.3 it has the density fmix of the chapter opening with respect to Lebesgue measure, since Unif[a,b] has density [axb]/(ba) and both intervals have length 1. The compiler must produce a syntactic expression denoting that function, and the soundness theorem must certify the displayed equation rather than the plausibility of the formula.

The abstract compiler

Definition 179.8 — Density judgment

The abstract compiler derives Δdef, read: in the density context Δ=(V,V,Γ,δ), whose random variables are V, whose parameters are V, whose typing is Γ and whose accumulated density is δ, the expression e has density f, a function of the parameter state and the result value. Write bp(Δ) for the total mass accumulated by Δ and s for the parameter type of dst. Four of the rules carry the argument:

ΔdFailtλρx.0
HD-Fail
countable(type(v))
ΔdValvλρx.bp(Δ)(ρ)[x=v]
HD-Val
e deterministicFV(e)V
ΔdRandom dst eλρx.bp(Δ)(ρ)densdst([[e]]ρ,x)
HD-RandDet
Δdef
ΔdRandom dst eλρy.f(ρ)(x)densdst(x,y)dstock(s)(x)
HD-Rand
(,VV,Γ,λρ.1)dbf(V,V,Γ,λρ.δ(ρ)f(ρ)(tt))de1g1(V,V,Γ,λρ.δ(ρ)f(ρ)(ff))de2g2
ΔdIF b THEN e1 ELSE e2λρx.g1(ρ)(x)+g2(ρ)(x)
HD-If

The rules for variables, pairs, LET, and the deterministic operations are those of the source, and the judgment is nondeterministic: an expression may have zero, one, or several derivations.

Lemma 179.9 — Two representative soundness cases

Let N=stock(t).

  1. Branching. If [[ei]]=giN for i=1,2 and the scrutinee denotes pδtt+(1p)δff, and if g1 already carries the factor p and g2 the factor 1p, then [[IF b THEN e1 ELSE e2]]=(g1+g2)N.

  2. Sampling with a random parameter. If [[e]]=fstock(s) and dst has density densdst with respect to N, then [[Random dst e]]=hN with h(y)=f(x)densdst(x,y)dstock(s)(x).

Proof of Lemma 179.9 — Two representative soundness cases

Proof. (i) By the IF clause of convention 179.6 and example 172.26, the left-hand side is p[[e1]]+(1p)[[e2]], which by hypothesis is g1N+g2N, and lemma 179.3 rewrites this as (g1+g2)N.

(ii) Write D(x)=Adensdst(x,y)dN(y) and λ=stock(s). For measurable A, [[Random dst e]](A)=convention179.6D(x)d[[e]](x)=hypothesisonef(x)D(x)dλ(x)=TonelliA(f(x)densdst(x,y)dλ(x))dN(y)=(hN)(A), the second step by definition 179.2 and the three-stage ascent of theorem 172.14 applied to D, and the exchange by Tonelli (convention 172.12), whose σ-finiteness hypothesis holds because stock measures are σ-finite and the integrand is nonnegative and jointly measurable. ◻

Convention 179.10 — Imported soundness lemmas

The following are the machine-checked statements of the Archive of Formal Proofs entry accompanying Eberl–Hölzl–Nipkow, imported at exactly these signatures.

Abstract soundness, expr_has_density_sound: if (,,Γ,λρ.1)def, Γe:t, and e has no free variables, then hasDensity([[e]]σ,stock(t),f(λx.undefined)). It is proved from a generalized auxiliary lemma expr_has_density_sound_aux, stated for an arbitrary density context satisfying a well-formedness predicate, by induction on the derivation of definition 179.8.

Concrete soundness, expr_has_density_cexpr_sound: if ([],[],Γ,1)cef, Γe:t, and e is ground, then hasDensity([[e]]σ,stock(t),λx.csem(xσ)(f)), and moreover f is a well-typed target expression of type REAL with FV(f){0}.

Final result, expr_compiles_to_sound: writing e:tcf for the conjunction of well-typedness, groundness, and compilation, it states [[e]]σ=(λx.csem(xσ)(f))stock(t),xuniverse(t). csem(xσ)(f)0, together with Γe:t, tΓcf:REAL and FV(f){0}. This is the chapter’s endpoint and its sole principal system card. Lemma 179.9 proves two of the induction cases at the book’s signature; the import supplies the remaining cases and the refinement.

From the abstract compiler to an executable one

The judgment of definition 179.8 produces a mathematical function. An implementation must produce a syntax tree, and the passage between them is a separate development with its own invariant.

Definition 179.11 — Target expressions and the refinement invariant

Target expressions cexpr are built from variables of de Bruijn index, constants, arithmetic and comparison operations, an indicator b, a conditional IFc  THEN  ELSE , and a bounded integral cet; they are typed by Γcf:t and evaluated by csem(σ)(f). The concrete compiler derives (V,V,Γ,δ)cef with the same rule shapes as definition 179.8, replacing every mathematical operation by its syntactic counterpart. The refinement invariant relating the two is: for every derivation of the concrete judgment there is a derivation of the abstract judgment with fabs(ρ)(x)=csem(xρ)(fcon), and the concrete result is well-typed of type REAL with free variables among the single result index.

Remark 179.12 — The simplifier is outside the theorem

The expression produced by the concrete compiler for example 179.7 is large: it contains integrals over the Boolean parameter, indicator products, and unevaluated constant subexpressions. Simplifying it to fmix is performed by a separate, separately verified simplifier, and no part of convention 179.10 is a statement about the simplified expression. A fixture that compares only the simplified output therefore tests the composition of two components; the project below compares the unsimplified output against a frozen expected tree first, and only then the simplified one.

Example 179.13 — The paper's program, compiled and simplified

Let ebhat= LET x=Random(UniformReal 0,1) INLET y=Random(Bernoulli x) INIF y THEN x+1 ELSE x. Its compiled density, after simplification, is fbhat(x)=[1x2](x1)+[0x1](1x), with fbhat(1/2)=1/2, fbhat(3/2)=1/2, and fbhat(1)=0. The value at 3/2 is the density of x+1 at 3/2, that is the density of x=1/2 weighted by the probability x=1/2 of taking the tt branch; the value at 1/2 is the density of x=1/2 weighted by the probability 1x=1/2 of the ff branch. Both weights are the Bernoulli density evaluated at the sampled x, which is what HD-RandDet inserts.

Remark 179.14 — Comparison card

SlicStan is recorded as a comparison card and as a source of examples. Its shredding transformation, its density semantics, and its conditional-independence results are theorems about a different source language with a different semantics, and none of them is imported here. A translation between the two would require its own semantics-preservation theorem relating the measure semantics of convention 179.6 to SlicStan’s; no such theorem is constructed in this book, so no statement crosses between the two cards.

Exercise 179.1

★☆☆ Verify from definition 179.2 that fmix is a density for the measure of example 179.7, by computing (fmixLeb)([0,3/2]) and comparing it with the measure of that interval.

Exercise 179.2

★☆☆ Compute [[Fail REAL]] and check that HD-Fail produces a density for it. State which clause of hasDensity would fail if the rule produced a positive constant.

Exercise 179.3

★★☆ In the proof of lemma 179.9(ii), replace the stock measure of the parameter type by a measure that is not σ-finite and exhibit a failure of the exchange of integrals. State the hypothesis of convention 172.12 that is violated.

Suggested first pass.

None of these problems is a prerequisite for a later chapter. Begin with exercise 179.4, then exercise 179.5, then the practical project exercise 179.8.

Exercise 179.4

★☆☆ Compute by hand the density of LET x=Random(UniformReal 0,1) IN x+1 and check it against HD-RandDet and the LET rule.

Exercise 179.5

★★☆ Write out lemma 179.9(i) for emix, displaying the two scaled branch densities produced by HD-If and their sum.

Exercise 179.6

★★☆ Derive fbhat of example 179.13 by hand from the rules of definition 179.8, and verify its three named values.

Exercise 179.7

★★☆ Exhibit two syntactically different target expressions that both satisfy convention 179.10 for emix, and identify the null set on which they differ. Explain what this implies for a checker that compares generated trees.

Exercise 179.8

★★★ Practical project.density-compiler-fixture-runner Implement the abstract compiler of definition 179.8 for the fragment containing Val, Var, LET, IF, addition of a constant, and Random at Bernoulli and UniformReal, producing a target expression as a syntax tree; then implement, as a separate pass, the simplifier that evaluates constant subexpressions and collapses indicator products.

The invariant to maintain is that the compiler pass never simplifies and the simplifier never compiles: the two outputs are recorded separately, and every numeric constant in both is a reduced rational.

The concrete result is the compiled and simplified density for two programs. The acceptance test is decidable and exact. For the mixture program emix of the chapter opening, the unsimplified tree must equal the frozen expected tree recorded with the project, and the simplified tree must equal fmix, evaluating to 1/3 at x=1/2, 2/3 at x=3/2, and 0 at x=1. For ebhat of example 179.13, the simplified tree must equal fbhat, evaluating to 1/2 at x=1/2 and at x=3/2, and 0 at x=1. Normalization is never attributed to convention 179.10: the runner prints which comparison used the unsimplified tree and which used the simplified one. A SlicStan example may be displayed for contrast and is not part of the test.

Search the book

Type to search the local edition.