Sakura Lang

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)
}

Open in playground

Save this as main.sak, then run:

sakura verify main.sak

It proves two things, called obligations:

  • the call clamp(12, 0, 10) meets ordered, and
  • clamp meets bounded whenever it returns, assuming ordered held.

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

sakura verify gives each obligation one of four results; only a proof can remove a check.sakura verify: contract and arithmetic obligations go to pinned Z3 workers. Then proved or disproved or unknown or unsupported. proved: holds under the recorded assumptions; a build may drop an eligible check. This path ends here. disproved: a counterexample exists; the run exits 1. This path ends here. unknown: the solver could not decide; the check stays. This path ends here. unsupported: outside the implemented fragment; the check stays.sakura verifycontract andarithmeticobligations go topinned Z3 workersprovedholds under therecordedassumptions; abuild may drop aneligible checkdisproveda counterexampleexists; the runexits 1unknownthe solver couldnot decide; thecheck staysunsupportedoutside theimplementedfragment; thecheck stays

The exit code is 0 for unknown and unsupported too: automation that needs every proof reads the JSON Lines records.

sakura verify gives each obligation one of four results; only a proof can remove a check.

Each obligation gets one of four results:

  • proved: it holds under the recorded assumptions. Warning W7002 (and path_feasible: false) flags a proof whose assumptions can never be true, so it proves nothing useful.
  • disproved: the solver found a counterexample. Check its replay field 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.sak

Here --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.sak

A 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.

Edit on GitHub

Last updated

On this page