Unary Probabilistic Hoare Logic - Proof Rules #
This file provides proof rules for the unary probabilistic Hoare logic.
Main theorems #
pHoare_ret- Return rule: pure values satisfy postconditions derivable from preconditionpHoare_fail- Fail rule: fail satisfies any postcondition (vacuously)pHoare_sample- Sample rule: if Q holds for all sampled values, the judgment holdspHoare_get- Get rule: reading from a locationpHoare_set- Set rule: writing to a locationpHoare_bind- Bind rule: sequential compositionpHoare_assert- Assert rule: assertion with continuation
Implementation notes #
The key proof technique is extracting information from support membership.
For SDistr.pure x, the only element with nonzero probability is x itself.
We use SDistr.mem_support_pure_iff which states b ∈ (pure a).support ↔ a = b
without requiring DecidableEq.
For SDistr.bind, the challenge is that if (d.bind f)(some y) ≠ 0 then
there must exist some x with d(some x) ≠ 0 and (f x)(some y) ≠ 0.
This requires analyzing the sum ∑ x, d(x) * (f x)(y) which is nonzero
only if at least one term is nonzero.
Return rules #
Return rule: pure values satisfy any postcondition derivable from precondition.
For SPComp.pure a, the only outcome is (a, h₀) where h₀ is the initial heap.
So we need Q a h₀ which follows from the hypothesis.
Fail rule #
Fail satisfies any postcondition (vacuously, since no outcomes exist).
SPComp.fail returns SDistr.fail which assigns probability 0 to all
some outcomes, so the support is empty and the universal quantification
is vacuously true.
Sample rule #
Sample rule: if Q holds for all possible sampled values, the judgment holds.
SPComp.sample α draws uniformly from α and does not change the heap.
So any outcome (a, h') in the support satisfies h' = h₀, and we need
Q a h₀ for all a, which the hypothesis provides.
State rules #
Get rule: reading from a location.
SPComp.get l returns SDistr.pure (h₀.get l, h₀), so the only outcome
is (h₀.get l, h₀).
Set rule: writing to a location.
SPComp.set l v returns SDistr.pure ((), h₀.set l v), so the only outcome
is ((), h₀.set l v).
Bind rule #
Bind rule: sequential composition.
This is the most important structural rule. To prove pHoare P (c.bind f) Q,
we need an intermediate postcondition Mid such that:
cestablishesMidfromP- For each intermediate result
a,f aestablishesQfromMid a
The key proof obligation is: if (c h₀).bind (fun (a, h₁) => f a h₁) assigns
nonzero probability to (b, h'), then there exists an intermediate (a, h₁)
with nonzero probability in c h₀ such that f a h₁ assigns nonzero
probability to (b, h'). This follows from the fact that a sum of nonneg
terms is nonzero iff at least one term is nonzero.
Assert rule #
Assert rule: if precondition implies the assertion, continue.
SPComp.assert p is if p then pure () else fail. When p holds
(which it does, given P implies p), it reduces to pure ().
Derived rules #
Frame rule: pHoare is preserved under conjunction with a frame condition.
If c preserves R (i.e., R holds before and after), and we have
pHoare P c Q, then we can add R to both pre- and postcondition.
Existential rule: if for some witness the judgment holds, the judgment holds.