DEVELOPMENT EDITION · 769442725406
Verification Manual
On this page

Diagnose and repair an overflow

An increment can fail even when its input has type Natural. A natural number may already be the largest representable integer. This worked diagnosis uses a precondition to state which inputs the operation accepts, then checks that its caller meets that condition.

The complete example projects contain the files used below. Run commands from the indicated project directory with veradic on your executable search path and the documented proof dependencies installed.

The intended operation

The operation accepts a Natural and returns the next Natural. Its postcondition says exactly how the answer relates to the input. Here are the relevant declarations and bodies from the before projects; these excerpts need the surrounding package/module files in the complete example project.

Vada — bounded.vads and bounded.vadb:

function Increment (Value : Natural) return Natural
  with Post => Increment'Result = Value + 1;

function Increment (Value : Natural) return Natural is
begin
   return Value + 1;
end Increment;

Varc — inside package Bounded in bounded.va:

pub fn increment(value: Natural) -> Natural
    ensures result == value + 1
{
    return value + 1;
}

Vibe — bounded.vb:

from verada import Natural, ensures, result


def increment(value: Natural) -> Natural:
    ensures(result == value + 1)
    return value + 1

The annotation constrains the input and result; it does not make arithmetic unbounded. Natural ranges from zero to Natural'Last in Vada, Natural::Last in Varc, and Natural.last in Vibe. Its upper bound is the upper bound of Verada's Integer type. Use the named bound rather than baking a machine-dependent maximum into the contract. Vibe's annotation also does not turn this operation into unrestricted Python integer arithmetic.

Ask for a proof

Choose one directory:

Syntax Working directory from the repository root
Vada docs/manuals/examples/first-proof/vada/before
Varc docs/manuals/examples/first-proof/varc/before
Vibe docs/manuals/examples/first-proof/vibe/before

Run:

veradic prove --project verada.toml

The command fails with a nonzero exit status and an overflow diagnostic, B4101, for the addition. At the largest input, the mathematical answer is one greater than the largest representable integer. Changing the result subtype alone would not fix that addition. The expression Value + 1 in the postcondition also needs to be defined over the accepted inputs.

A counterexample is useful evidence, but read its qualification. When Verada says that replaying the values reaches the check and fails, execution of its reference model confirmed the failure. If replay runs through instead, the solver's state may expose an insufficient loop invariant or callee contract. An unknown result, B5115, means that the budget did not establish either success or failure; it is not a confirmed overflow. This example has a real boundary failure: substitute the largest Natural directly to understand it.

The main program in the project passes 41 and would produce 42. That safe call does not prove the public operation correct for every input allowed by its interface. Verada reasons about the operation using its parameter types and contracts.

State the missing boundary condition

The after projects add a precondition. The body and promised relation between input and output remain the same.

Vada — the declaration in bounded.vads:

function Increment (Value : Natural) return Natural
  with Pre  => Value < Natural'Last,
       Post => Increment'Result = Value + 1;

Varc — the declaration and body in bounded.va:

pub fn increment(value: Natural) -> Natural
    requires value < Natural::Last
    ensures result == value + 1
{
    return value + 1;
}

Vibe — bounded.vb:

from verada import Natural, ensures, requires, result


def increment(value: Natural) -> Natural:
    requires(value < Natural.last)
    ensures(result == value + 1)
    return value + 1

The precondition gives the prover room for the addition. Because value is nonnegative and strictly below the maximum, value + 1 is representable and belongs to Natural. The returned expression then satisfies the postcondition.

A precondition transfers a responsibility to every caller. It is appropriate here only if the API is meant to reject the largest input. If the application must accept that input, choose a different specification, such as an optional answer, a wider result with safe arithmetic, or a saturating increment. Do not silently exclude required cases to obtain a proof.

Check the repair and the caller

Change to the matching after directory and run:

veradic prove --project verada.toml
veradic run --project verada.toml

The proof succeeds. The program prints:

answer: 42

The example's caller passes 41, so it also discharges the new precondition. A caller that passes the largest Natural cannot prove the call; that is a precondition failure, B5101, at the call rather than an accepted overflow inside increment.

For a native executable, still in the after directory, run:

veradic build --project verada.toml -o increment-demo
./increment-demo

It prints the same result. build proves before it compiles, and run in a project uses the proofs prove just put in the project's proof cache, so both executables leave out every check proved here. --advisory turns unproved checks into warnings and keeps them as run-time checks; --no-prove skips proof and keeps every check at run time. Neither option establishes the proof shown here. check performs legality and flow analysis and does not discharge overflow obligations. prove --no-prove still proves.

What this result establishes

For each input satisfying the repaired interface, the implementation proves the specified addition and contract checks under Verada's proof model. This does not establish that the chosen interface meets every application requirement, that arbitrary foreign callers obey it, or that resource exhaustion cannot occur. A proof also relies on the compiler, proof encoding, solver, runtime/backend and the contracts of any trusted dependencies.

This example uses executable scalar checks. Runtime checking cannot be assumed to cover arbitrary ghost-only contracts or every resource/progress property; see the mode and runtime limits. Continue with the lesson, B4101, or bounded vectors.