Skip to main content

verus_proof_order_preserving_map

Macro verus_proof_order_preserving_map 

Source
macro_rules! verus_proof_order_preserving_map {
    (
        input = $inty:ty, output = $outty:ty
        $(, captures = |$($cap:ident : $capty:ty),* $(,)?|)?
        $(, proof = |$px:ident, $py:ident $(, $pfx:ident, $pfy:ident)?| { $($proof_body:tt)* })?
        , __target = { $(move)? |$tin:ident $(: $tinty:ty)?| $target_body:expr }
        , __captures = [$($fv:ident),* $(,)?] $(,)?
    ) => { ... };
    (
        $($rest:tt)*
    ) => { ... };
}
Expand description

Fulfills an order_preserving = ... proof parameter for a singleton map closure (shape |value| -> out, as taken by Singleton::map) with a Verus-checked proof of order preservation.

What order preservation means for a map closure: for any two inputs x <= y (with respect to the input type’s PartialOrd), the outputs must satisfy f(x) <= f(y) (with respect to the output type’s PartialOrd). A Monotonic singleton only ever grows, so mapping it through an order-preserving function yields a singleton that also only grows, which lets the result stay Monotonic (e.g. for a deterministic Singleton::threshold_greater_or_equal).

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, assuming two arbitrary inputs with x <= y, symbolically executes the actual closure body on each and asserts that the outputs compare less than or equal. Verifying this obligation also proves the closure body panic-free (e.g. no arithmetic overflow). Users only declare the input and output types:

ⓘ
let doubled = sum.map(q!(
    |v| v.saturating_mul(2),
    order_preserving = verus_proof_order_preserving_map!(input = u32, output = u32)
));

The comparisons are expressed through Verus’s specification of PartialOrd (vstd::std_specs::cmp::PartialOrdSpec): the obligation requires that both the input and output types’ PartialOrd implementations are known to follow their specification (obeys_partial_cmp_spec()). This holds automatically for primitive integers and tuples of them; your own 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 = |x, y| { ... } (or proof = |x, y, fx, fy| { ... }) clause supplies a proof script to help the SMT solver, with ghost bindings for the two inputs and (optionally) the two outputs. 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 VerusOrderPreservingProof marker, so the types go through the ordinary order_preserving = ... mechanism with zero overhead.