Sometimes a fact is true but the solver can't discover it alone, such as "if a list is sorted, every item before position m is below a value that item m is below". You state that fact as a lemma. A lemma looks like a function with no return value, and the prover proves it like a function. Then you use it by calling it as a statement, which hands the fact to the solver at that point.
Proving a lemma about "every position up to m" usually takes induction: assume it for m - 1 and show it for m. In a lemma, induction is a recursive call with a decreases measure.
pure def is_sorted(xs: List[Int]) -> Bool {
return forall i in range(0, len(xs) - 1): xs[i] <= xs[i + 1]
}
lemma sorted_prefix_lt(xs: List[Int], m: Int, t: Int) {
in {
is_sorted(xs)
0 <= m
m < len(xs)
xs[m] < t
}
post {
forall i in range(0, m + 1): xs[i] < t
}
decreases m
if m > 0 {
sorted_prefix_lt(xs, m - 1, t)
}
}
Lemmas cost nothing at run time. They are removed from the compiled program together with every call to them, including calls from modules that import them. A lemma is still type-checked and proven.
A lemma that no function calls does nothing, and vary check reports it (rule VCC006).
Sometimes the proof needs a value your program doesn't, such as a step counter or a copy of an earlier state. Declare it with ghost let or ghost mut.
def count_to(n: Int) -> Int {
in {
n >= 0
n < 1000000
}
out(r) {
r == n
}
ghost mut steps = 0
mut i = 0
while i < n {
invariant {
i <= n
steps == i
}
decreases n - i
i = i + 1
steps = steps + 1
}
return i
}
Ghost variables are erased from the compiled program, together with any invariant line or assert that mentions them. The type checker rejects a ghost value that flows into code that runs, such as a return, a condition, or a normal variable. Ghost values may feed other ghost declarations, lemma calls, assert, and loop annotations.
xs[i] = v, xs.append(v) and xs.pop() are supported on a list that nothing else can see. That means a local list created in the function (with a list literal, list(...), sorted(...), map(...), filter(...) or a comprehension), or a list field of an object that starts empty or fresh.
A list is shared, and stays read-only in the proof, as soon as it is stored under a second name, passed to a function, captured by a lambda, or returned from a field. To change a parameter, copy it first with list(xs).
Loops that change a list need an invariant that says what is true of it, including len(xs). A bounded stack with len(self.items) <= self.capacity as a class invariant is proven across push and pop. An insertion sort is proven to return a list of the same length in order, with --solver z3 or --solver cvc5. The bundled solver leaves one quantified step undecided.
When a forall makes a proof slow or undecided, name the term that should make the solver use the statement:
forall i in range(0, len(xs)) {:trigger xs[i]}: xs[i] > 0
A forall can also range over a list directly (forall x in xs: x > 0). A trigger has no effect when the program runs. A timeout note names the quantifier that was being used, which tells you where a trigger or a lemma would help.
A loop proof cuts the loop at its invariant, so a counterexample can describe a loop state that the real program reaches only after billions of iterations. An example is an overflow in i * (i + 1) once i passes three billion.
For a simple counting loop (while i < n, i = i + 1, no break or return) whose invariant fixes every other variable the loop assigns from the counter, vary prove jumps straight to that state and replays from there. The failure is then reported as refuted with concrete inputs. This needs --solver z3 or --solver cvc5 when the invariant involves products.
You often don't need to write modifies or decreases. When a method has no modifies clause, the prover works out which fields its body writes. When a loop or recursion has no decreases, it infers a measure for the common counting and structural cases.
These show up as modifies (inferred) and decreases (inferred) in the output, in the JSON report under inferred, and as hints in the editor. An inferred clause is checked exactly like a written one, so a wrong guess gives unknown, never proven. To keep one, run vary check --fix (rule VCC008), which writes the clause into your source.
When an index or division fails because a field of self is out of range, the report can suggest a class invariant. It only suggests one after proving it holds in the constructor and in every method.