Proof 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.

For each lesson, copy the code into the filename shown in the terminal, run the command next to the lesson, then paste the summary line (or the test results line) into the answer box and validate it. Your score is saved in this browser, so you can leave and come back later on the same device.

Section 1

See a proof

Run the prover, read its answers, and fix a real bug.

clamp.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 }

Lesson 1 / clamp.vary

Prove your first function

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

The in block says what the function needs, and the out block says what it promises. vary prove checks the promise for every input the in block allows, without running the function. The last line counts the results: one obligation here, and it is proven.

Run vary prove clamp.vary
Expected output
1 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported
abs_diff.vary
def abs_diff(a: Int, b: Int) -> Int { out(r) { r >= 0 } if a > b { return a - b } return b - a }

Lesson 2 / abs_diff.vary

Let the prover find a bug

Run a function that looks correct and read the counterexample.

abs_diff promises a result that is never negative, and every ordinary test of it passes. The prover finds that a - b overflows a 64-bit Int when b is the smallest possible value. A refuted result prints the inputs and a ready-to-run test block. The summary line counts two refuted overflows, one for each subtraction.

Run vary prove abs_diff.vary
Expected output
1 proven, 2 refuted, 0 unknown, 0 bound-reached, 0 unsupported
abs_diff.vary
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 }

Lesson 3 / abs_diff.vary

Fix it by stating the inputs

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

The bug is not in the arithmetic. The function was never told which inputs it accepts. Bounding both parameters in the in block rules out the overflow, and every obligation is now proven. The contract is both the fix and the documentation.

Run vary prove abs_diff.vary
Expected output
3 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported
abs_diff.vary
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 }

Lesson 4 / abs_diff.vary

Turn a counterexample into a test

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

A refuted result is not a hint, it is a real failure. The Reproduce block the prover prints is a normal Vary test. Running it with vary test fails, which gives you a regression test to keep after you fix the bug. This file has no contract, so there is nothing to prove, only the test to run.

Run vary test abs_diff.vary
Expected output
Results: 0 passed, 1 failed

Section 2

Loops and recursion

Give the prover the facts it cannot find by itself.

count_up.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 }

Lesson 5 / count_up.vary

Prove a loop with an invariant

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

The prover cannot step through a loop whose length depends on the input, so you tell it what stays true on every pass. This invariant says i stays between 0 and n. When the loop ends, i >= n, which with i <= n gives i == n, the promise in the out block. The decreases measure shows the loop finishes.

Run vary prove count_up.vary
Expected output
7 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported
count_up.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 } decreases n - i i = i + 1 } return i }

Lesson 6 / count_up.vary

See what a weak invariant looks like

Remove half of the invariant and read the unknown result.

Here the invariant only says 0 <= i. That is true, but it is not strong enough to give r == n when the loop ends, so the prover cannot decide and reports unknown. It never guesses. The note shows an input where it could not decide and tells you to strengthen the invariant.

Run vary prove count_up.vary
Expected output
5 proven, 0 refuted, 1 unknown, 0 bound-reached, 0 unsupported
gcd.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) }

Lesson 7 / gcd.vary

Prove a recursive function

Use a decreases measure to show a recursive function terminates.

Recursion needs a measure for the same reason a loop does. Each call to gcd passes a % b as the new second argument, which is smaller than b, so b shrinks toward zero. The decreases b line states that, and the prover checks it along with the out promise.

Run vary prove gcd.vary
Expected output
6 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported

Section 3

Lists, classes and tests

Prove properties of collections and objects, then check that contracts still run.

sorted.vary
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] }

Lesson 8 / sorted.vary

Say something about every item in a list

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

forall states a fact about every item in a list. is_sorted is a pure function, so contracts can call it. smallest requires a sorted, non-empty list and promises to return the first item, and the prover shows the index xs[0] is always in range.

Run vary prove sorted.vary
Expected output
9 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported
account.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 }

Lesson 9 / account.vary

Prove a class and what a function may change

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

A class invariant holds for every object whenever no method is running. modifies lists the fields a function may change, and the prover checks nothing else changes. old(dst.balance) is the value when the function started. src != dst is required because two parameters of one class could be the same object.

Run vary prove account.vary
Expected output
7 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported
clamp.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 } 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) } }

Lesson 10 / clamp.vary

Contracts are still checked when the code runs

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

A proof does not replace tests. The same in and out blocks run as checks whenever the program runs, so calling clamp with a reversed range throws. observe throws asserts that. Proofs and tests answer different questions, and each catches what the other misses.

Run vary test clamp.vary
Expected output
Results: 2 passed, 0 failed

Section 4

When the solver matters

Use the solver that ships with Vary for what the default solver cannot decide.

spread.vary
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 }

Lesson 11 / spread.vary

Decide a nonlinear goal with Z3

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

This promise needs the fact that (a - b) * (a - b) is never negative, which is reasoning about products of two variables. The default solver, SMTInterpol, can only approximate products, so plain vary prove reports one unknown for it, and says that Z3 may decide it. Z3 ships with Vary, so --solver z3 works offline: all six obligations are proven. A solver can turn an unknown into a proven or a refuted, never the other way.

Run vary prove --solver z3 spread.vary
Expected output
6 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported

Course score

0/11 lessons complete

Validate each lesson output to finish this course.

Next course

Install and Start Via Server Prepare an Ubuntu host, confirm Vary and Via are installed, install Via prerequisites, initialize the control plane, and verify the Via systemd services. Continue