On this page
Write and prove a bounded increment
In this lesson you will check, prove, run, and build a small application in your preferred Verada syntax. You will see why an operation can pass the type checker and still fail proof, then repair its interface and check its caller.
You need veradic, Z3, and a C compiler for the final native build; the previous page shows how to check them. Commands below run from the root of the Verada source tree. If you copy an example directory elsewhere, run the commands inside it with --project verada.toml instead.
Choose your syntax
Each project contains a small Bounded package or module and a main program. The main passes 41 to an increment operation and prints its answer. Start with the before project:
| Syntax | Project file | Files |
|---|---|---|
| Vada | vada/before/verada.toml | Ada package specification and body |
| Varc | varc/before/verada.toml | One package file |
| Vibe | vibe/before/verada.toml | One typed Python module |
Use the row for your chosen syntax in each step. You can complete the lesson without reading the other two. All files are included under examples/first-proof.
Check the program
Run one of these commands:
veradic check --project docs/manuals/examples/first-proof/vada/before/verada.toml
veradic check --project docs/manuals/examples/first-proof/varc/before/verada.toml
veradic check --project docs/manuals/examples/first-proof/vibe/before/verada.tomlYour command succeeds and prints nothing. check verifies syntax, names, types, and the flow of data and ownership. It does not ask whether the arithmetic is safe.
Ask whether the increment is safe
Run the matching command:
veradic prove --project docs/manuals/examples/first-proof/vada/before/verada.toml
veradic prove --project docs/manuals/examples/first-proof/varc/before/verada.toml
veradic prove --project docs/manuals/examples/first-proof/vibe/before/verada.tomlThe proof fails with B4101, an overflow check. In Vada the report reads:
error[B4101]: overflow check fails
--> docs/manuals/examples/first-proof/vada/before/bounded.vadb:4:14
|
4 | return Value + 1;
| ^^^^^^^^^ might overflow `Integer` (-9223372036854775808 .. 9223372036854775807)
|
= note: replaying these values on the program reaches this check and it fails
= counterexample: fails when Value = Integer'Last
= failing inputs: exactly Value = Integer'Last
= help: a precondition `Value <= 9223372036854775806` on `Bounded.Increment` excludes exactly the values that fail, and is proved to make this check pass
1 error emitted
proved 4 of 5 checks in 2 subprograms (1 statically, 3 by the solver)
The function promises to return the next Natural, and its input may be any Natural. Natural is the subtype of Integer from 0 to Integer'Last, so for the largest input the sum does not fit. Verada found that input, ran the program on it to confirm that the check really fails (the replaying note), and computed a precondition that excludes exactly the failing inputs.
The main program only passes 41, which is safe. But the function's interface allows every Natural, and a proof of the function covers every input its interface allows, not just the calls that happen to exist today.
Give callers a precise boundary
For this application, define the increment only for inputs below the maximum, which is the precondition Verada suggested. A precondition states what every caller must establish; a postcondition states what the operation promises when it returns. The after projects add this precondition and keep the body and the postcondition.
Vada, the complete bounded.vads:
package Bounded is
function Increment (Value : Natural) return Natural
with Pre => Value < Natural'Last,
Post => Increment'Result = Value + 1;
end Bounded;The body in bounded.vadb returns Value + 1. Natural'Last names the largest value. Increment'Result names the function result in its postcondition.
Varc, the complete bounded.va:
package Bounded {
pub fn increment(value: Natural) -> Natural
requires value < Natural::Last
ensures result == value + 1
{
return value + 1;
}
}
pub makes the function part of the package's public interface. requires and ensures state the precondition and postcondition. Natural::Last names the upper bound.
Vibe, the complete bounded.vb:
from verada import Natural, ensures, requires, result
def increment(value: Natural) -> Natural:
requires(value < Natural.last)
ensures(result == value + 1)
return value + 1The parameter and result have subtype Natural. requires and ensures, imported from verada, declare the precondition and postcondition, and Natural.last is the upper bound. Unlike CPython's int, Natural is bounded: that bound is what the proof checks against.
With the precondition, adding one stays within Natural and the postcondition describes the returned value. The caller passing 41 can prove that it meets the new requirement.
In words: a caller must establish the precondition before making the call. The implementation assumes that precondition, then must establish its postcondition and the checks inside its body.
Prove and run the repaired project
Choose the matching after project:
veradic prove --project docs/manuals/examples/first-proof/vada/after/verada.toml
veradic run --project docs/manuals/examples/first-proof/vada/after/verada.tomlFor Varc, replace vada with varc; for Vibe, replace it with vibe. Proof succeeds. The program's output is:
answer: 42
run compiles the program in memory and runs it, using the proofs prove has just recorded in the project's proof cache, so it prints only the program's output. Had you run it before proving, it would have warned (B8012) that the unproved checks stay in the program as run-time checks.
Build an executable
Run the corresponding command:
veradic build --project docs/manuals/examples/first-proof/vada/after/verada.toml -o increment-demo
./increment-demoAgain, substitute varc or vibe for your syntax. build proves the program first and refuses to build one that does not prove; the native program prints the same line.
If the solver cannot start, install Z3 or pass --z3 PATH. If linking fails, install a C compiler or pass --cc PATH.
Try a different caller
In a copy of the after project, replace the argument 41 with the largest Natural: Natural'Last in Vada, Natural::Last in Varc, or Natural.last in Vibe (in Vibe, also add from verada import Natural to main.vb). Prove again. The call now fails with B5101: the caller does not meet the precondition. The function is still proved; it is the call that must change.
A precondition is appropriate only if the application can accept the restriction. If the operation must handle every input, revise its specification and implementation together; for example, a saturating increment could return the maximum unchanged. Excluding inputs just to make a proof pass changes what the application promises.
Continue with everyday programming, or look deeper at integer ranges and diagnosing an overflow.