Static verification
Prove contracts before the code runs, and tell proofs apart from runtime checks.
Contracts are checked while the code runs. sakura verify goes one step further: it tries to prove, before the code runs, that a contract can never be broken. It uses the Z3 solver, which you install separately (the pinned version is 5.3.1).
fn clamp(x: Int, lo: Int, hi: Int) -> Int
requires ordered: lo <= hi
ensures bounded: lo <= result && result <= hi
{
if x < lo { lo } else if x > hi { hi } else { x }
}
pub fn main() -> Unit {
let _value = clamp(12, 0, 10)
}Save this as main.sak, then run:
sakura verify main.sakIt proves two things, called obligations:
- the call
clamp(12, 0, 10)meetsordered, and clampmeetsboundedwhenever it returns, assumingorderedheld.
Each obligation is proved on its own. A proof covers that one rule, not the whole program.
Read the result, not just the exit code
The exit code is 0 for unknown and unsupported too: automation that needs every proof reads the JSON Lines records.
Each obligation gets one of four results:
proved: it holds under the recorded assumptions. WarningW7002(andpath_feasible: false) flags a proof whose assumptions can never be true, so it proves nothing useful.disproved: the solver found a counterexample. Check itsreplayfield to see whether the counterexample was confirmed by actually running it, or is only a candidate.unknown: the solver could not decide within its limits, or failed.unsupported: the rule uses something the verifier does not handle yet.
sakura verify exits 1 if any obligation is disproved, and 0 otherwise, including when some are unknown or unsupported. Setup and compile errors fail too. If your automation needs every rule proved, read the --json output instead of the exit code. Note that a postcondition can be proved while a separate overflow check in the same function fails; one proof does not imply the other.
Integer ranges
By default, verify checks integers in JavaScript's safe-integer range. --target wasm checks signed 64-bit integers instead:
sakura verify --json main.sak
sakura verify --target wasm main.sakHere --target only picks the integer rules for the proof. To run on WebAssembly, use sakura run --target wasm.
What changes in a build
sakura build --manifest main.sakA build can drop some runtime checks that verify proved: eligible ensures checks and where checks other than at entry. It drops a check only when a matching, authenticated proof record exists; a missing or stale record keeps the check. Preconditions (requires) and T.from checks always stay, and the rule's own computation still runs. run, test and LIR output keep every check. A WebAssembly build drops the checks sakura verify --target wasm proved, as a JavaScript build does for the default profile. Simple range-based arithmetic optimizations do not need the solver.
Not supported yet
Verifying loops, recursion, termination, rules over whole collections, and Float or String reasoning is planned. Proof certificates and verify fn, proof and ghost syntax are not implemented. The verification reference covers solver limits, JSON fields, cache authentication and manifest statuses.
Next
Testing: unit tests, property tests and replay.
Last updated