
<div class="callout callout-note"><p>This page assumes no background. If you can write a Vary function with an <code>in</code> and <code>out</code> block, you can use <code>vary prove</code>.</p></div>

## 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:

```vary
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:

```text
$ 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.

| Answer | What it means |
|---|---|
| Proven | The promise holds for every allowed input |
| Refuted | Here is an input that breaks it, with a ready-to-run `test` block so you can see the bug |
| Unknown | The 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](/docs/prove/how-it-works/).

## Proofs and tests answer different questions

| | Tests | Formal verification |
|---|---|---|
| What it checks | The inputs you wrote down | Every input the `in` block allows |
| When it finds a problem | A failing input | A failing input, called a counterexample |
| When it finds nothing | No bug seen so far | No bug exists, for the promise you wrote |
| Cost | Cheap to write and fast to run | You 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

| Word | Plain meaning |
|---|---|
| Contract | The `in` and `out` blocks on a function: what it needs and what it promises |
| Specification | The contract, seen as the definition of "correct" for that function |
| Precondition | What must be true when the function is called. The caller's job. Written as `in { ... }` |
| Postcondition | What is true when the function returns. The function's job. Written as `out(r) { ... }` |
| Invariant | Something 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 |
| Counterexample | An input that breaks a promise |
| Solver | The 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 says | You get |
|---|---|
| It can never be false | Proven |
| It can be false, here are values | Refuted, with those values |
| It can't tell, or it ran out of time | Unknown 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.

| Construct | Why it is hard | What you write |
|---|---|---|
| Loops | The prover cannot step through a loop whose length depends on the input | An `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 handle | A `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](/docs/prove/examples/) shows both.

## What a proof does and does not tell you

A proof holds as far as its assumptions hold.

| Assumption | In Vary |
|---|---|
| The solver and the prover are correct | They 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 language | `Int` 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 says | Library calls and AI calls are treated as opaque. The report lists every such assumption, so nothing is hidden |
| The contract says what you meant | Only 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.

| Benefit | Cost |
|---|---|
| Covers every allowed input, including rare ones | You have to write a precise contract, and a bad one proves the wrong thing |
| Finds overflow and boundary errors | Loops and list properties need invariants or lemmas |
| Counterexamples are concrete and reproducible | Unknown results happen, and can be hard to diagnose |
| Contracts double as exact documentation | Small edits can break a proof |
| Proven checks can be removed from production | Only part of the language is modelled. The rest is reported unsupported |

## Where to go next

| Page | What it covers |
|---|---|
| [How vary prove works](/docs/prove/how-it-works/) | The result statuses, the two modes, and what is checked |
| [Worked examples](/docs/prove/examples/) | Real bugs found by proof, and how each was fixed |
| [Writing your first proofs](/docs/prove/first-proofs/) | A step-by-step tutorial: loops, recursion, lists, classes |
| [Formal verification course](/courses/vary-prove/) | Eleven runnable lessons with checked answers |
| [Advanced proof techniques](/docs/prove/advanced/) | Lemmas, ghost variables, and what to do when a proof is stuck |
| [Supported language features](/docs/prove/features/) | Reference: what the prover understands, by type and construct |
| [Solvers](/docs/prove/solvers/) | The bundled solver, Z3, and cvc5 |
| [Toolchain integration](/docs/prove/toolchain/) | Editor, CI, mutation testing, and Lean export |
| [Other verification tools](/docs/prove/comparison/) | How Vary relates to Dafny, SPARK and others, and where it is weaker |
