DEVELOPMENT EDITION · 769442725406
Programmer Guide
On this page

Reasoning about contracts, loops and models

Proof is easier when each boundary in the program states the facts the next layer needs. Start with a useful specification, then make the implementation establish it. Solver options come last, if at all.

Three questions

  1. Is this a legal Verada program: well typed, initialized, with valid data flow and ownership? check answers this.
  2. Is every run-time check safe for every permitted execution? prove answers this.
  3. Do the contracts say what the program is meant to do, and does the code meet them? prove answers the second half; only you can answer the first.

A program can pass the first question and fail the second, or prove a contract that is too weak to express what you actually needed. The first proof showed an overflow; the stockroom adds behavior: a reservation's outcome, its exact change to the stock, and that the other bins stay unchanged.

State what the caller needs

A caller can use the subtypes of parameters and results and the public contracts. In the stockroom:

Each fact has a job. The bound proves arithmetic safe, the outcome relation lets the caller interpret the result, and the frame relation lets the caller keep what it knew about the other bins.

If a caller cannot prove something after a call, look at the callee's contract. The body may be right while the contract says too little. Adding an assumption at the call site only hides that.

Refer to the entry state

Items'Old in Vada, old(items) in Varc and Vibe, is the value of items when the operation was entered. The stockroom indexes it: Items'Old (Position) or old(items)[position]. Inside a quantifier, take old of the whole value and index it with the quantified variable, as the stockroom does, because the quantified variable does not exist on entry.

Write a loop invariant

A loop invariant states what is true each time the loop reaches it. Verada proves that it holds the first time, and that one more iteration preserves it; after the loop it is all Verada knows about what the loop did.

Suppose a loop adds up quantities that are each at most 100. A bound such as total <= 100 * i at the start of iteration i proves that the additions don't overflow, but says nothing about which numbers were added. To prove that the result is the sum, the invariant must also relate total to the elements already visited.

  1. Describe the part already processed and what the accumulator means for it.
  2. Add the bounds needed to make the next operation safe.
  3. At the exit, combine the invariant with the reason the loop stopped.

Vada writes pragma Loop_Invariant (...), Varc invariant ...; and Vibe invariant(...), as a statement in the loop body. Its position matters: before or after the update describes a different state. The loop example shows a counting loop that runs correctly but does not prove until it gets its invariant, in all three syntaxes; loop invariants and termination explains it step by step.

When the prover can find a missing invariant, it suggests it: a failed check inside a loop may carry a help: line with an invariant that is proved and makes the check pass.

Explain why it terminates

A for loop over a range always ends. A while loop or a recursive call may need a variant: a value that decreases (or increases) on every iteration or call and cannot do so forever.

For Euclid's algorithm, the remainder is smaller than the previous divisor. For binary search, the remaining interval shrinks. For recursion over a structure, the part left to process shrinks.

Functions must terminate. Procedures must terminate when they are marked to, or when a context that must terminate calls them. Don't turn a function into a procedure just to avoid writing a variant unless the operation really may run forever. The syntax for variants is in contracts.

Use models when the representation gets in the way

A model is a mathematical view of data for use in contracts: a sequence of elements, a set of keys, a map from keys to values. A vector's capacity and storage can change while its model, the sequence of its elements, stays the same.

Ghost code exists only for proof; it cannot affect what the program computes. A container's model is the right way to say "append adds this element at the end" without describing pointers. Use the library's models and contracts before inventing your own; see ghost models and lemmas.

Read a failed proof

The note under each failed check tells you what kind of failure it is:

Diagnose proof results goes through each case.

Improve a proof without changing the requirements

Split an operation at a natural boundary and give the parts contracts. Name repeated expressions with an expression function. State the frame and range facts that matter. Add an assertion for an intermediate fact; the prover proves it and then uses it.

Prefer these to assumptions. pragma Assume is trusted, not proved, and veradic trust lists it, together with imported C contracts and any other premise a proof relies on. Operating the prover covers budgets, statistics and certificates.

Review the specification

Ask someone to explain, from the public contracts alone, what each operation accepts, changes and leaves alone. If they can't, the specification is not finished, however green the proof is.

The Verification Manual develops these methods further, and the exercises give you contracts to change and callers to fix.