
<div class="callout callout-note"><p>This tutorial builds on <a href="/docs/prove/overview/">What formal verification is</a>. Each step adds one idea and one short example. You can run every example with <code>vary prove</code>.</p></div>

## Step 1: let the prover look for bugs

You do not have to write any proof to get value from the prover. Add an `out` block that says what a function should return, then run it in bounded mode. Bounded mode runs the function symbolically but only follows each loop or recursive call a fixed number of times (8 by default). It cannot prove anything, but it needs no extra annotations, and it finds counterexamples.

```vary
def abs_diff(a: Int, b: Int) -> Int {
    out(r) {
        r >= 0
    }
    if a > b {
        return a - b
    }
    return b - a
}
```

The promise is that the result is never negative. It looks right, and every ordinary test passes. Run it:

```text
$ vary prove --bounded abs_diff.vary
abs_diff  overflow in `a - b`   REFUTED
  a = 0
  b = -9223372036854775808
```

A Vary `Int` is a 64-bit number that wraps around when it overflows. If `b` is the smallest possible value, `a - b` wraps to a negative number and breaks the promise. The prover prints a `test` block you can paste into your test file to reproduce it.

To fix it, say what inputs the function accepts by adding bounds to an `in` block. [Worked examples](/docs/prove/examples/) shows the fixed version.

## Step 2: prove a loop with an invariant

A function with no loops is usually proven automatically. A loop is harder, because the prover cannot step through a loop whose length depends on the input. You tell it what stays true on every pass. That fact is called a loop invariant.

```vary
def count_up(n: Int) -> Int {
    in {
        n >= 0
        n < 1000000
    }
    out(r) {
        r == n
    }
    mut i = 0
    while i < n {
        invariant {
            0 <= i
            i <= n
        }
        decreases n - i
        i = i + 1
    }
    return i
}
```

Read the loop like this. The `invariant` says "at the top of every pass, `i` is between 0 and `n`". When the loop stops, `i < n` is false, so `i >= n`. Combined with the invariant `i <= n`, that gives `i == n`, which is the promise in the `out` block.

The `decreases` line shows the loop finishes. It names a number (`n - i`) that gets smaller on every pass and can't go below zero. Both lines also run as checks while your program runs.

```text
$ vary prove count_up.vary
7 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported
```

If the invariant is too weak to prove the promise, the result is unknown, and the report often shows which fact is missing. Strengthen the invariant and run again. A `for` loop over `range` always terminates, so it only needs an `invariant`.

## Step 3: recursion works the same way

A recursive function needs a `decreases` measure for the same reason a loop does. Put it after the contract blocks.

```vary
def gcd(a: Int, b: Int) -> Int {
    in {
        a >= 0
        b >= 0
    }
    out(r) {
        r >= 0
    }
    decreases b
    if b == 0 {
        return a
    }
    return gcd(b, a % b)
}
```

Each call passes `a % b` as the new second argument, which is smaller than `b`, so `b` shrinks toward zero. Without `decreases`, the `out` promise can still be proven, and termination is reported unknown.

## Step 4: say something about every item in a list

To talk about a whole list, use `forall` (every item) or `exists` (at least one). This function says a list is sorted: for every position, the item is not greater than the one after it.

```vary
pure def is_sorted(xs: List[Int]) -> Bool {
    return forall i in range(0, len(xs) - 1): xs[i] <= xs[i + 1]
}
```

You can use `is_sorted(xs)` in an `in` or `out` block, and it also runs as a normal check at run time. Statements about every item are the hardest thing for a solver. If a proof comes back unknown, try `--solver z3` (see [Solvers](/docs/prove/solvers/)), or add a hint, as described in [Advanced proof techniques](/docs/prove/advanced/).

## Step 5: classes, and what a function is allowed to change

A class can have an `invariant`: something true for every object whenever no method is running. A function that changes fields lists them in a `modifies` clause. The prover checks that nothing else changes, and callers rely on that.

```vary
class Account(opening: Int) {
    invariant {
        self.balance >= 0
    }

    mut balance: Int = opening
}

def transfer(src: Account, dst: Account, amount: Int) {
    in {
        src != dst
        amount > 0
        amount <= src.balance
        amount < 1000000000
        dst.balance < 1000000000
    }
    post {
        dst.balance == old(dst.balance) + amount
    }
    modifies src.balance, dst.balance
    src.balance = src.balance - amount
    dst.balance = dst.balance + amount
}
```

Three things here are worth noticing. `old(dst.balance)` means the value when the function started, so the `post` block can compare before and after. And `src != dst` is in the `in` block because two parameters of the same class might be the very same object. If they were, the transfer would not do what the `post` block says, so the prover makes you rule it out. The two bounds of one billion are there for the same reason as in step 1: without them, `dst.balance + amount` could overflow, and the prover reports exactly that as a refuted result.

## Where to go from here

| If you want to | Read |
|---|---|
| Practise each step with checked answers | [Formal verification course](/courses/vary-prove/) |
| Prove a fact the solver can't find | [Advanced proof techniques](/docs/prove/advanced/) |
| Check whether a type or construct is supported | [Supported language features](/docs/prove/features/) |
| Understand the results and the two modes | [How vary prove works](/docs/prove/how-it-works/) |
| Run the prover in CI or the editor | [Toolchain integration](/docs/prove/toolchain/) |
