macro_rules! verus_proof_monotone_fold {
(
acc = $accty:ty, item = $itemty:ty
$(, captures = |$($cap:ident : $capty:ty),* $(,)?|)?
$(, proof = |$pstate:ident, $px:ident $(, $pafter:ident)?| { $($proof_body:tt)* })?
, __target = { $(move)? |$tacc:ident $(: $taccty:ty)?, $titem:ident $(: $titemty:ty)?| $target_body:expr }
, __captures = [$($fv:ident),* $(,)?] $(,)?
) => { ... };
(
$($rest:tt)*
) => { ... };
}Expand description
Fulfills a monotone = ... proof parameter for an aggregation closure
(fold / reduce, shape |acc, item| with acc: &mut A) with a Verus-checked
proof of monotonicity.
What monotonicity means for an aggregation: applying the closure to any
accumulator value and any item must leave the accumulator greater than or equal
to its previous value, with respect to the accumulator type’s PartialOrd. This is
the order that Monotonic singletons are observed through (e.g. by
Singleton::threshold_greater_or_equal), so a monotone aggregation yields a
singleton whose value only grows over time, making threshold detection deterministic.
Users never write the proof obligation (so they cannot get it wrong): this macro
receives the quoted closure itself from q! (as a trailing __target = { ... }
argument) and generates a Verus function that takes a ghost snapshot of an arbitrary
accumulator, symbolically executes the actual closure body on it with an arbitrary
item, and asserts that the snapshot compares less than or equal to the final
accumulator. Verifying this obligation also proves the closure body panic-free (e.g.
no arithmetic overflow). Users only declare the accumulator and item types:
stream.fold(
q!(|| 0u32),
q!(
|max, x| { if x > *max { *max = x; } },
monotone = verus_proof_monotone_fold!(acc = u32, item = u32)
)
);The comparison is expressed through Verus’s specification of PartialOrd
(vstd::std_specs::cmp::PartialOrdSpec): the obligation requires that the
accumulator type’s PartialOrd implementation is known to follow its specification
(obeys_partial_cmp_spec()), and that the specification orders the old value at or
below the new one. This holds automatically for primitive integers and tuples of
them; your own accumulator types must implement PartialOrdSpecImpl inside a
verus! block.
If the closure captures (read-only) variables from its environment, re-declare them
(with their runtime types) in a captures = |...| clause; the obligation is then
universally quantified over the capture values. q! also passes the closure’s capture
list (as a trailing __captures = [...] argument), and the macro checks at compile
time that every capture is declared.
An optional proof = |state, x| { ... } (or proof = |state, x, result| { ... })
clause supplies a proof script to help the SMT solver, with ghost bindings for the
initial accumulator, the item, and (optionally) the final accumulator (e.g.
proof = |s, x| { assert(s <= (s | x)) by (bit_vector); }). The script is checked by
Verus and cannot weaken the obligation (though, as anywhere in Verus, assume is an
explicit soundness escape hatch).
The proof is gated on cfg(verus_keep_ghost), so it only exists when the crate is
compiled by the Verus driver (cargo verus verify). Under normal compilation the
macro just produces a VerusMonotoneProof marker, so the types go through the
ordinary monotone = ... mechanism with zero overhead.