Alpha. Vary is under active development and not ready for production use. Syntax, APIs, performance, and behaviour may change between releases.

What formal verification is

Start with the problem

When you write a function, you want to know it works. The usual way to find out is to run it on a few inputs and check the answers. That is a test, and tests are worth writing.

The trouble is how few inputs a test covers. Suppose a function takes one whole number. A computer's whole number can be any of about 18 quintillion values. A test suite might try five of them. The function can still be wrong for the other 18 quintillion, and bugs like to hide in the odd cases nobody thought to try: zero, a negative number, a huge number, two values that are equal.

Formal verification is a different way of checking. Instead of running the function on chosen inputs, a tool reasons about the code and answers the question for every input at once. It then tells you one of two things: this function always does what it promises, or here is a specific input where it doesn't.

First, say what the function promises

A tool can only check a promise you have written down. In Vary you write it as a contract, which is two blocks on a function. The in block says what must be true when the function is called. The out block says what will be true when it returns.

Here is a function that keeps a number inside a range:

def clamp(x: Int, lo: Int, hi: Int) -> Int {
    in {
        lo <= hi
    }
    out(r) {
        r >= lo and r <= hi
    }
    if x < lo {
        return lo
    }
    if x > hi {
        return hi
    }
    return x
}

Read the contract out loud. "If the low end is not above the high end, the result r is never below lo and never above hi." You already write contracts like this in Vary, and they already run as checks while your program runs. Formal verification reuses them, so there is no second language to learn.

Then ask the prover to check it

Running the prover is one command:

$ vary prove clamp.vary
1 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported

"Proven" means the out promise holds for every input that satisfies the in block. Three tests could only tell you about three inputs.

What you can get back

Suppose the function were wrong. The prover would not just say so. It would give you an input that breaks the promise, so you can see the bug.

AnswerWhat it means
ProvenThe promise holds for every allowed input
RefutedHere is an input that breaks it, with a ready-to-run test block so you can see the bug
UnknownThe prover could not decide. It says so instead of guessing

Those are the three you will see most. The full list of results is on How vary prove works.

Proofs and tests answer different questions

TestsFormal verification
What it checksThe inputs you wrote downEvery input the in block allows
When it finds a problemA failing inputA failing input, called a counterexample
When it finds nothingNo bug seen so farNo bug exists, for the promise you wrote
CostCheap to write and fast to runYou must write the promise precisely, and it can be slow or undecided

You still need tests. A proof is only about the promise you wrote, so it can't catch a function that keeps a promise nobody should have made. Tests catch the cases you didn't think to write down. A proof catches the inputs you didn't think to try. Each finds what the other misses.

Words you will meet

WordPlain meaning
ContractThe in and out blocks on a function: what it needs and what it promises
SpecificationThe contract, seen as the definition of "correct" for that function
PreconditionWhat must be true when the function is called. The caller's job. Written as in { ... }
PostconditionWhat is true when the function returns. The function's job. Written as out(r) { ... }
InvariantSomething that stays true. A loop invariant is true at the top of every pass through a loop. A class invariant is true for an object whenever no method is running on it
CounterexampleAn input that breaks a promise
SolverThe program that does the logical work of finding a proof or a counterexample

A program is correct only relative to its specification. If the contract is wrong, a proof of it proves the wrong thing. Writing the contract is the part only you can do.

How a proof is found

You do not need this to use the tool, but it helps to know what is happening.

The prover does not run clamp on every input. It follows each path through the code once, using placeholders (x, lo, hi) in place of real numbers. For each path it builds a statement: "if lo <= hi and x < lo, then the result lo is between lo and hi." Then it asks a program called an SMT solver whether that statement can ever be false.

If the solver says no, the statement always holds and you get proven. If it says yes, it hands back values that make it false, and you get a counterexample. If it can't tell, you get unknown.

Solver saysYou get
It can never be falseProven
It can be false, here are valuesRefuted, with those values
It can't tell, or it ran out of timeUnknown or timeout

The prover is built to be sound. That means it would rather say "I don't know" than claim a proof that isn't real. An unknown result is annoying. A false proof would be dangerous, so the tool never takes that risk.

Where you need to help

Straight-line code is usually proven automatically. Two things need a hint from you.

ConstructWhy it is hardWhat you write
LoopsThe prover cannot step through a loop whose length depends on the inputAn invariant: a fact that is true before the loop, stays true on every pass, and is strong enough to give the out promise once the loop ends
"For every item in a list"Statements about every element are the hardest thing for a solver to handleA forall in the contract, sometimes with a hint, or a lemma (a helper fact proven separately)

Finding the right loop invariant is the hardest part of verification. If that sounds like too much, start with vary prove --bounded. It looks for bugs by trying loops up to a fixed number of passes, needs no extra annotations, and still gives you counterexamples. Worked examples shows both.

What a proof does and does not tell you

A proof holds as far as its assumptions hold.

AssumptionIn Vary
The solver and the prover are correctThey are software and can have bugs. Before reporting a bug, vary prove runs your function on the counterexample to confirm it is real
The model matches the languageInt is a 64-bit integer that wraps on overflow, like Java's long. The prover checks for overflow instead of ignoring it
Code the prover can't see into behaves as its contract saysLibrary calls and AI calls are treated as opaque. The report lists every such assumption, so nothing is hidden
The contract says what you meantOnly you can check this

When it is worth the effort

Verify the small, important core and test everything else. The function that handles money, the parser that must never accept bad input, the routine that sorts: these usually pay back. A whole application rarely does.

BenefitCost
Covers every allowed input, including rare onesYou have to write a precise contract, and a bad one proves the wrong thing
Finds overflow and boundary errorsLoops and list properties need invariants or lemmas
Counterexamples are concrete and reproducibleUnknown results happen, and can be hard to diagnose
Contracts double as exact documentationSmall edits can break a proof
Proven checks can be removed from productionOnly part of the language is modelled. The rest is reported unsupported

Where to go next

PageWhat it covers
How vary prove worksThe result statuses, the two modes, and what is checked
Worked examplesReal bugs found by proof, and how each was fixed
Writing your first proofsA step-by-step tutorial: loops, recursion, lists, classes
Formal verification courseEleven runnable lessons with checked answers
Advanced proof techniquesLemmas, ghost variables, and what to do when a proof is stuck
Supported language featuresReference: what the prover understands, by type and construct
SolversThe bundled solver, Z3, and cvc5
Toolchain integrationEditor, CI, mutation testing, and Lean export
Other verification toolsHow Vary relates to Dafny, SPARK and others, and where it is weaker