Formal verification

How vary prove works

From contract to proof

vary prove does not run your function on real numbers. It treats the parameters (x, lo and hi in the clamp example) as unknown placeholders and follows each path through the code, recording what must be true on that path: the in conditions, the if conditions taken, and the assignments made. This technique is called symbolic execution.

At each place where something could go wrong it creates a proof obligation, which just means "a thing the prover has to show is true". For clamp in the overview, each of the three return paths yields a statement like "if lo <= hi and x < lo, then lo >= lo and lo <= hi". Each obligation is sent to an SMT solver as the question "can this be false?". No is a proof. Yes is a counterexample.

Calls are checked against the callee's contract, never its body. This is called modular verification. Changing a function body re-proves only that function and anything that relied on its contract, which keeps proofs scalable and cacheable.

What is always checked

ObligationWhen
Postconditions (out, post)Every function that declares them
Loop invariants and decreasesEvery loop and recursive call that declares them
Runtime failures such as division by zeroAlways, in every function. This includes a missing dict key, !! on None, an index out of range, assert, and unwrap on a possible Err
Arithmetic overflowIn functions that declare a contract. Wrapping is valid Vary behaviour, so a function with no contract is not flagged
Callee preconditionsAt every call site
Class invariantsAt construction and at the end of every method. They are assumed at method entry
modifies framesEvery write to a field

The result statuses

StatusMeaning
provenHolds for all inputs that satisfy the preconditions
refutedThe solver found inputs that break it, and Vary confirmed them by running the function
unknownThe solver could not decide, or a loop invariant was too weak
timeoutThe solver ran out of time
unsupportedThe function uses a construct the prover does not model, and the report names it
bound-reachedBounded mode stopped unrolling a loop
proven-externalA Lean proof you supplied was checked

If the prover itself fails on a function, that function is reported unsupported with an internal: note that carries the error. The other functions in the file are still checked.

When a result is unknown and the solver offered inputs, the report may say unconfirmed candidate. The inputs did not fail when Vary ran the function, so they are not a bug. They are a hint about which fact is missing, usually a loop invariant or a callee contract that is too weak.

Two modes

ModeCommandWhat it does
Bounded checkingvary prove --boundedUnrolls loops and recursion a fixed number of times (8 by default) and looks for a counterexample. It needs no annotations and proves nothing, so it is a bug finder, like a very thorough test generator. Start here
Deductive proofvary proveReasons about all iterations using loop invariants and decreases. This is the mode that can say proven

Bounded mode is what found the overflow in the first worked example. Deductive mode is what turns a clean bounded run into a real proof.

Counterexamples are Vary values

A refuted obligation prints concrete inputs as Vary literals and, where possible, a generated test block that reproduces the failure. Before the prover reports refuted, it replays the counterexample on the real semantics of the program. If the solver was wrong, or the model was too loose, the status becomes unknown with an unconfirmed candidate note instead of a false alarm.

Faithful JVM semantics

Vary Int is a 64-bit wrapping long. The prover models it as an unbounded mathematical integer and adds an overflow obligation at every operation that could wrap. If every overflow obligation is proven, the simple model is exact. Division with // truncates toward zero and % takes the sign of the dividend, unlike the division in the solver's own language, so the encoder never uses the solver's operators directly.

Design rules

RuleWhat it means for you
One specification languageThe contract you test at run time is the contract you prove. There is no second language to learn
ModularA call is checked against the callee's contract. Edits re-prove only what they affect
Sound for the supported part, explicit about the restA construct the prover cannot model is reported unsupported by name. It is never reported proven
Trusted boundaries are namedBuiltins, runtime functions, JVM interop, spawn and AI library calls are opaque, and the axioms a proof used are listed in the report
Nothing to installThe default solver is pure Java and ships with Vary. Z3 and cvc5 are optional
Counterexamples are replayedRefuted means a real failure, not a solver artifact

What it does with code it cannot model

Real code calls the standard library, parses strings, and talks to services. vary prove treats any value or call it has no model for as opaque. A call with no body and no contract returns a fresh value of its declared type, and unless the callee is pure it is assumed to change anything it was passed. The report lists it as opaque:NAME, so you can see exactly what a proof took on trust. For a few built-ins the standard library states what the runtime guarantees (ord returns a value from 0 to 65535); those are written as extern def contracts in the standard library, the prover uses them like a function contract, and a test runs every one against the runtime. The proof of the rest of the function still goes through.

This is why vary prove is useful on existing code with no changes. A function that mixes an HTTP call with arithmetic still gets its arithmetic checked.

← What formal verification is
Writing your first proofs →