DEVELOPMENT EDITION · 769442725406
Programmer Guide
On this page

Exercises and worked solutions

Work in a copy of an example directory, so the original stays as a known-good version. Predict the result of each change before you run prove, then compare.

Each exercise has a point to make about reasoning. A command that passes without the intended property is not a solution.

1. Send the largest Natural to Increment

Task. In the repaired first-proof project, replace 41 with the largest Natural. Predict whether this is a body overflow or a caller contract failure.

Solution. The repaired function accepts only values below the maximum. The call breaks that precondition and fails with B5101; the function's own proof still stands. Pass a permitted value, or change the function and its contract if the maximum must be accepted.

2. Reserve zero items

Task. In the stockroom's first reservation, change the amount from 5 to 0. What should the Boolean result and total be?

Solution. Zero is a valid Quantity and never more than a bin holds, so the reservation succeeds and changes nothing. After the restock the total is 66, so the assertions that the total is 61 now fail to prove; change them to 66, and the program prints total: 66. The request for 99 is still declined. Returning false for zero would break the outcome relation in the contract.

If the business requires a positive request, narrow the API explicitly. That is a domain change, not an overflow repair.

3. Deliver more than the remaining capacity

Task. Change the initial restock amount from 6 to 91. Is 91 an invalid Quantity, and where should proof fail?

Solution. 91 is a valid Quantity, but the bin starts with 10 and has room for 90, so the call fails with B5101 in every syntax. Repair it with an amount that fits, a check before the call, or a restock that reports a capacity outcome. Deleting the precondition only moves the failure into the body, where the addition no longer fits a Quantity.

4. Remove the unchanged-bin guarantee

Task. Remove the quantified frame relation from an update contract, keeping only the selected bin's new value. Can a modular caller still justify the exact total?

Solution. No. The contract no longer says what happened to the other bins, so the main program's assertions that the reservation succeeds and that the total is 61 fail (B5105), even though the body leaves those bins alone. The note says the prover's values describe "a state no run reaches": the contract is too weak, not the code wrong. Restore the frame. Assuming the total after the call would only hide the missing guarantee.

5. Add a fourth bin

Task. Extend the stockroom index domain and initializer. Identify every observation that must change.

Solution. Change the Bin range and add a fourth initial quantity. Then update the written-out total, the Vibe range(3) in the contracts and the display loop, and the expected totals and output. Vada and Varc loops over Bin follow the new range by themselves; the three-term total does not.

The lesson: iterate over the declared domain, and when the number of elements grows, define the total as a reduction with a contract rather than writing out every term.

6. Introduce an invalid display index

Task. Iterate once beyond the shelf's last valid index.

Solution. Index 3 is outside the shelf, and prove reports B4103, an index check. Extending the loop does not extend the array; change the data model and the loop together.

7. Widen after an overflowing operation

Task. An Integer addition can exceed Integer's base range. Would assigning its result to a wider variable repair it?

Solution. No. The addition is computed in its operands' type before the assignment, and a wider destination does not widen it after the fact. Convert an operand to the wider type before adding, or choose an algorithm whose intermediate values fit. Use a modular type only if wrapping around is the required result.

8. Distinguish remainder grouping

Task. Predict Vada -7 mod 3 and (-7) mod 3. Compare Varc (-7) % 3 and (-7) %% 3.

Solution. In Vada the minus sign applies to the whole term, so -7 mod 3 is -(7 mod 3), which is −1, while (-7) mod 3 is 2. In Varc, % is the remainder, −1, and %% the modulus, 2. Write the parentheses: the sign is not part of the literal.

9. Keep an element view while changing its container

Task. Suppose a Vibe local names a mutable element and the code also resizes or replaces its source container before the view ends. What must you decide?

Solution. Decide whether you need that element itself, an independent copy of its value, or the element at the same index looked up again after the change. A view cannot outlive or conflict with its container, so Verada rejects the program as written: end the view before changing the container, or take a copy. See sharing.

10. Write the invariant before the loop body

Task. You are summing a bounded prefix. Explain why total <= 100*i alone cannot prove that you summed the correct elements.

Solution. It bounds the total but says nothing about which numbers were added; many wrong totals satisfy it. Relate total to the elements already processed, and keep the bound too, since it proves the additions safe.

The invariant describes the point where it is written. Placed after adding element i, it must describe one more element than at the start of the iteration.

11. Give a recursive call a progress argument

Task. A recursive function calls itself with an unchanged measure. Can a stronger postcondition establish termination?

Solution. No. A postcondition says what holds if the function returns; it cannot make a recursion that repeats the same argument forever return. Give the function a variant that each recursive call decreases, or change the algorithm. A finite parameter range alone does not prove termination.

12. Reorder filter and take-while

Task. On [2, 1, 3], predict the sum of take_while(x => x > 1).filter(x => x != 1), and of filter(x => x != 1).take_while(x => x > 1).

Solution. Stages apply in the order written. The first stops at 1 and sums 2. The second drops 1 first, never meets an element that stops it, and sums 2 + 3 = 5. Reordering stages changes the meaning, so choose the order the requirement describes.

13. Trust an imported implementation

Task. A C function's declared postcondition says it returns a nonnegative value. Verada proves a client using that fact. What still needs validation?

Solution. That the C function really meets the declaration: its result, its effects, its types and calling convention, its ownership of memory and its errors. Verada proves the client assuming the contract; it never reads the C. veradic trust lists the contract as trusted. Review the C code and test the program as built.

14. Review a successful run

Task. veradic run on the first-proof before project prints answer: 42. Has the increment been proved safe?

Solution. No. run uses the proof cache and keeps unproved checks in the program as run-time checks. Before any prove it warns that they are unproved (B8012); once prove has run, it names the overflow that fails. The call with 41 is safe, but the function still accepts the one input that overflows. Add the precondition, or change the function, and prove again.

Finish an extension

Choose one stockroom change and finish it completely: state the requirement, change the contract and the representation, check, prove, run and build the result, and keep a failing or boundary case that shows the intended rejection. A short note explaining those four steps says more than a count of proved checks.