def abs_diff(a: Int, b: Int) -> Int {
out(r) {
r >= 0
}
if a > b {
return a - b
}
return b - a
}
The function looks right and any ordinary test passes. vary prove finds that a - b can overflow a 64-bit Int.
abs_diff overflow in `a - b` REFUTED
a = 0
b = -9223372036854775808
Reproduce:
test "abs_diff counterexample" {
let a = 0
let b = -9223372036854775807 - 1
let r = abs_diff(a, b)
observe r >= 0
}
1 proven, 2 refuted, 0 unknown, 0 bound-reached, 0 unsupported
The printed test block fails under vary test, so the bug is reproducible without the prover. The fix is to say what inputs the function accepts.
def abs_diff_bounded(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
}
3 proven, 0 refuted, 0 unknown, 0 bound-reached, 0 unsupported
Least common multiple is a // gcd(a, b) * b. Nobody writes a test for it at zero.
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)
}
def lcm(a: Int, b: Int) -> Int {
in {
a >= 0
b >= 0
a < 1000000
b < 1000000
}
out(r) {
r >= 0
}
return a // gcd(a, b) * b
}
lcm division by zero in `a // gcd(a, b)` REFUTED
a = 0
b = 0
gcd(0, 0) is 0, so lcm(0, 0) divides by zero. The precondition allowed it. The standard library had this exact bug until vary prove was pointed at it. The fix is either to require a > 0 or to return 0 when both inputs are zero. The same run reports three obligations as unknown, because a // gcd(a, b) divides by a value that is not a constant. See Solvers for how --solver z3 settles those.
def discount_cents(price: Int, percent_off: Int) -> Int {
in {
price >= 0
price < 1000000000
}
out(r) {
r >= 0 and r <= price
}
return price - price * percent_off // 100
}
discount_cents out { (r >= 0) and (r <= price) } REFUTED
price = 2
percent_off = -100
A negative discount raises the price above the original, and a discount over 100 percent makes it negative. Both break the promise in the out block. The contract was right and the callers were free to pass bad values. Add percent_off >= 0 and percent_off <= 100 to the in block and the result is clean:
3 proven, 0 refuted, 2 unknown (bundled solver)
5 proven, 0 refuted, 0 unknown (with --solver z3)
The two unknowns are the overflow checks for price * percent_off. The bundled solver does not decide a product of two variables, and Z3 does.
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
}
The invariant says that at the top of every iteration i is between 0 and n. When the loop ends, i < n is false, so i >= n. Together with i <= n that gives i == n, which is the out contract. The decreases n - i measure shrinks every iteration and stays at or above zero, so the loop terminates. The prover checks each of these steps, and both annotations also run as checks at run time.
The same command works on a whole module or project. It prints what it proved, what it refuted, and which opaque calls it took on trust. Writing your first proofs walks through loops, lists and classes, and Supported language features lists what the prover understands. Toolchain integration shows how to keep a baseline in CI.