Vary Course

Formal Verification

Eleven runnable lessons on vary prove: proofs, counterexamples, loop invariants, recursion, lists, classes, how proofs fit with tests, and when to use Z3.

See a proof

Prove your first function

Run the prover on a function with an in and out contract and read the summary line.

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
}

Run: vary prove clamp.vary

Expected output:

1 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported

Let the prover find a bug

Run a function that looks correct and read the counterexample.

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

Run: vary prove abs_diff.vary

Expected output:

1 proven, 2 refuted, 0 unknown, 0 bound-reached, 0 unsupported

Fix it by stating the inputs

Add bounds to the in block and prove the same function.

def abs_diff(a: Int, b: Int) -> Int {
    in {
        a > -1000000000
        a < 1000000000
        b > -1000000000
        b < 1000000000
    }
    out(r) {
        r >= 0
    }
    if a > b {
        return a - b
    }
    return b - a
}

Run: vary prove abs_diff.vary

Expected output:

3 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported

Turn a counterexample into a test

Paste the prover's counterexample into a test block and watch it fail under vary test.

def abs_diff(a: Int, b: Int) -> Int {
    if a > b {
        return a - b
    }
    return b - a
}

test "abs_diff counterexample" {
    let a = 0
    let b = -9223372036854775807 - 1
    let r = abs_diff(a, b)
    observe r >= 0
}

Run: vary test abs_diff.vary

Expected output:

Results: 0 passed, 1 failed

Loops and recursion

Prove a loop with an invariant

Add an invariant and a decreases measure so the prover can reason about every iteration.

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
}

Run: vary prove count_up.vary

Expected output:

7 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported

See what a weak invariant looks like

Remove half of the invariant and read the unknown result.

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

Run: vary prove count_up.vary

Expected output:

5 proven, 0 refuted, 1 unknown, 0 bound-reached, 0 unsupported

Prove a recursive function

Use a decreases measure to show a recursive function terminates.

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

Run: vary prove gcd.vary

Expected output:

6 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported

Lists, classes and tests

Say something about every item in a list

Use forall in a pure function and rely on it in a contract.

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

def smallest(xs: List[Int]) -> Int {
    in {
        len(xs) > 0
        is_sorted(xs)
    }
    out(r) {
        r == xs[0]
    }
    return xs[0]
}

Run: vary prove sorted.vary

Expected output:

9 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported

Prove a class and what a function may change

Check a class invariant and a modifies clause on a money transfer.

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
}

Run: vary prove account.vary

Expected output:

7 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported

Contracts are still checked when the code runs

Run tests against a proven function, including a call that breaks its precondition.

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
}

test "clamp keeps a value inside the range" {
    observe clamp(5, 0, 10) == 5
    observe clamp(-3, 0, 10) == 0
    observe clamp(99, 0, 10) == 10
}

test "clamp rejects a reversed range" {
    observe throws { clamp(1, 5, 2) }
}

Run: vary test clamp.vary

Expected output:

Results: 2 passed, 0 failed

When the solver matters

Decide a nonlinear goal with Z3

Prove a claim about a product of two variables, a goal the default solver leaves unknown.

def spread(a: Int, b: Int) -> Int {
    in {
        a > -1000
        a < 1000
        b > -1000
        b < 1000
    }
    out(r) {
        r >= 2 * a * b
    }
    return a * a + b * b
}

Run: vary prove --solver z3 spread.vary

Expected output:

6 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported