Much Ado About Verada

A cheerful guide to programs that keep their promises.

3 · To Branch, or Not to Branch

How does a decision make the next operation safe?

Bring with you: a changing sold count, a fixed capacity, and finite Natural values.

Last time we added one to the count. At a full house, that sells nine tickets for eight seats. Puck proposes a very narrow ninth customer. The box office proposes a condition.

An if statement chooses which instructions run. Ours checks for room before changing the count. Its condition is a Boolean, a value that is either true or false. Each answer sends the program down a different path. Only the chosen path runs.

Puck guards a theatre door beside eight seats, all with their tickets sold. The sign admits another customer only when sold is less than capacity.

To seat, or not to seat: that is a capacity question.

Ask before adding

Predict first: this theatre starts full. Which message appears? Does the count change?

Example 3-1 · A full house

Replace the single file from the last chapter, or copy the matching example project into a fresh folder.

Vada · Ada · Example 3-1

main.vadb · Link to this version

with Ada.Text_IO;

procedure Main is
   Capacity : constant Natural := 8;
   Sold : Natural := 8;
begin
   if Sold < Capacity then
      Sold := Sold + 1;
      Ada.Text_IO.Put_Line ("Admitted.");
   else
      Ada.Text_IO.Put_Line ("Full house.");
   end if;
   pragma Assert (Sold <= Capacity);
end Main;
Varc · C-family · Example 3-1

main.va · Link to this version

import Ada.Text_IO;

fn Main() {
    let capacity: Natural = 8;
    var sold: Natural = 8;
    if sold < capacity {
        sold = sold + 1;
        Ada.Text_IO.Put_Line("Admitted.");
    } else {
        Ada.Text_IO.Put_Line("Full house.");
    }
    assert sold <= capacity;
}
Vibe · Python · Example 3-1

main.vb · Link to this version

from verada import Natural


def main() -> None:
    capacity: Natural = 8
    sold: Natural = 8
    if sold < capacity:
        sold = sold + 1
        print("Admitted.")
    else:
        print("Full house.")
    assert sold <= capacity


if __name__ == "__main__":
    main()

Syntax note: Vada closes the choice with end if;, Varc with braces, and Vibe with indentation. The final assertion is pragma Assert, assert …;, or assert … respectively. In all three it names a condition Verada must prove at that point. Vibe's assertion is part of Verada's proof work, not merely a Python statement we hope to encounter during testing.

Run it as in chapter 2: pass your filename to veradic run for a single file, or prove, then run, inside the copied project folder. It prints:

Full house.

The condition compares 8 tickets sold with a capacity of 8 seats. Since sold < capacity is false, we take the else path, print the refusal, and leave sold at 8.

Comedy and tragedy masks: two possible entrances.

A path carries information

Now suppose seven tickets have been sold. The condition is true, so we may add one. The addition happens only on a path where we know sold < capacity.

For whole numbers, being less than eight means being at most seven. Adding one therefore leaves the result at most eight. The decision supplies exactly the fact the following operation needs. It is useful to read the code as if there is room, sell one ticket.

Verada reasons about the paths too. A fact learned from a condition can justify arithmetic or a contract call inside that path. We do not need to repeat the condition as a comment and hope somebody remembers it.

Puck holds a lantern over the next few steps.

Say what must still be true

The assertion after the choice states:

The number sold is no greater than capacity.

An assertion is a condition to establish where it appears. Here it lets us name the important property and ask Verada to check it after either path.

On the admitted path, the guard makes the increment safe for the house. On the full-house path in this complete program, sold started at eight and stayed there. Both paths lead to the asserted fact.

There is a subtle limit: an else path only tells us that sold < capacity was false. That means sold >= capacity, not necessarily sold == capacity. Our fixed initial value makes this program's other path safe. If we allowed a starting count of nine, refusing another customer would not repair an already overbooked theatre. The assertion would fail. A decision needs appropriate starting facts as well as sensible actions.

The smallest bad change

Change only the guard from “less than” to “less than or equal”: Sold <= Capacity in Vada or sold <= capacity in Varc/Vibe. Leave the initial count at eight.

Before running it, ask what happens on that path. Eight satisfies the changed guard, so we sell ticket nine. The final assertion is false.

Now prove it: veradic prove, adding your filename for a single file. prove reports a proof failure, B5105: the assertion does not hold, and running the program would reach it and fail. In a project, a run working from the proof cache would not stop to prove the change. It keeps the assertion as a run-time check and catches the problem only when the program gets there, with ticket nine already sold. That is why we prove first. This is a broken property with a concrete reason, not an invitation to remove the assertion. Restore the strict less-than guard.

If a different proof cannot be completed, it does not automatically follow that the program is wrong: the prover may need another fact or more help. Here we can trace the actual failing execution ourselves, from eight to nine.

A ninth chair descends on a rope from above the stage.

Puck has submitted a furniture-based counterproposal.

Your cue

  1. Start with seven sold and keep the correct guard. Predict the message and final count.
  2. Try the changed <= guard with eight sold. Explain the failing assertion without using the phrase “the compiler doesn't like it.”
  3. Restore <, then start with nine sold. Why is the guard insufficient to establish the final assertion?
Prompter's notes · hints and answers

7 sold, correct guard. The program prints Admitted. and ends at 8 sold.

8 sold, changed guard. The full house passes <=. Another sale brings the count to 9 sold, which violates the 8-seat limit.

9 sold, correct guard. The program refuses the next sale. The count stays at 9 sold, so the existing overbooking still fails the assertion.

You can now follow both sides of a choice, use its condition as evidence, and distinguish preventing a new error from repairing invalid starting data. Next, Puck wants to use the admission rule at the theatre door as well as at the box office. He'll need a clear agreement about who checks for room.

← Previous: A Name and a Number · Contents · Next: Measure for Measure →