Optional Stopping Theorem
For the analysis of loops, the induction proof rule allows obtaining upper bounds on the wp
-semantics through superinvariants.
Under certain additional conditions, subinvariants I
can also provide valid lower bounds on the wp
-semantics (I <= wp[C](f)
).
The Optional Stopping Theorem identifies conditions ensuring uniform integrability of the subinvariant as one such constraint.
Intuitively, the proof rule requires that the loop executes finitely many iterations in expectation (e.g. that the loop is PAST) and that one can find a constant that bounds the expected change of the subinvariant by one loop iteration.
Caesar supports the "Optional Stopping Theorem for Weakest Preexpectation Reasoning" by Hark et al. as a built-in encoding.1 You can find the extended version of the paper on arxiv. The paper was published at POPL 2020.
Formal Theorem
Consider a loop while G { Body }
whose Body
is AST.
The loop's used and modified variables, except the ones declared within the loop, are referred to as vars
.
Give
I
, a function assigning a value to every state,PAST_I
, a function assigning a value to every state,c
,f
, a function assigning a value to every state,
such that all the following conditions are fulfilled:
-
I
is awp
-subinvariant ofwhile G { Body }
with respect tof
,HeyVL Encoding
@wp
proc I_wp_subinvariant(init_vars: ...) -> (vars: ...)
pre I(init_vars)
post f(vars)
{
vars = init_vars // set current state to input values
@unroll(1, I(vars))
while G {
Body
}
} -
while G { Body }
performs finitely many loop iterations in expectation:PAST_I
is awp
-superinvariant2 ofwhile G { Body; tick 1 }
with respect toPAST_I
,HeyVL Encoding
@wp
coproc PAST_I_ert_superinvariant(init_vars: ...) -> (vars: ...)
pre PAST_I(init_vars)
post PAST_I(vars)
{
vars = init_vars // set current state to input values
if G {
Body
tick 1
} else {}
} -
I
harmonizes withf
: , i.e.I
andf
are equal on states that do not fulfill the loop guardG
,HeyVL Encoding
proc I_harmonizes_with_post(vars: ...) -> ()
pre ?(!G(vars))
post ?(I(vars) == f(vars))
{} -
The expected value of
I
after one loop iteration is finite,3HeyVL Encoding
@wp
coproc I_iter_finite(init_vars: ...) -> (vars: ...)
pre 0
post I(vars)
{
vars = init_vars // set current state to input values
validate // maps finite expectation to 0, ∞ to ∞
if G {
Body
} else {}
} -
I
is conditionally difference bounded byc
: the absolute change ofI
by one loop iteration is at mostc
.HeyVL Encoding
@wp
coproc I_conditionally_difference_bounded_by_c(init_vars: ...) -> (vars: ...)
pre c
post ite(I(init_vars) <= I(vars), I(vars) - I(init_vars), I(init_vars) - I(vars)) // |I(vars) - I(init_vars)|
{
vars = init_vars // set current state to input values
Body
}
Then, I <= wp[while G { Body }](f)
holds.
Usage
By applying the @ost
annotation to a loop, Caesar will check the above requirements for a given invariant, PAST invariant, constant and post-expectation.
Below is the encoding of Example 39 from the paper.
It is a geometric loop program, which increments a counter b
with probability or stops (setting a = false
).
The HeyVL program uses the @ost
annotation that proves a lower bound of b + [a]
on the expected value of b
.
proc ost_geo_loop_example(init_a: Bool, init_b: UInt, init_k: UInt) -> (a: Bool, b: UInt, k: UInt)
pre init_b + [init_a]
post b
{
a = init_a
b = init_b
k = init_k
@ost(
/* invariant */ b + [a],
/* past_invariant */ 2 * [a],
/* cdb */ 1,
/* post */ b
)
while a {
var prob_choice: Bool = flip(1 / 2)
if prob_choice {
a = false
} else {
b = b + 1
}
k = k + 1
}
}
Inputs
You can see all four parameters are passed to the @ost
annotation in sequence:
invariant
: The quantitative subinvariant which should lower bound thewp
-semantics. Must harmonize withpost
.past_invariant
: The quantitative invariant used to proof PAST.cdb
: A constant of typeUReal
specifying the conditional difference bound for theinvariant
.post
: The post-expectation after the loop.
All parameters must be finite, i.e. cannot evaluate to .
Currently, this is only checked by Caesar for past_invariant
and cdb
.
Footnotes
-
We present the conditions from the paper's Theorem 37 (b), as used by the built-in encoding to ensure uniform integrability of the subinvariant. Currently, using the paper's alternative conditions (a) or (c) requires a manual encoding. ↩
-
Our
PAST_I
condition effectively encodes anert
-superinvariant, whereert
is a modified expected runtime calculus which counts the number of loop iterations ofwhile G { Body }
and does not count any nested loop iterations inBody
. ↩ -
In the paper, the condition to check is . With the additional constraint that
I
harmonizes withf
, we instead check the simpler but equivalent condition that holds. ↩