Much Ado About Verada

A cheerful guide to programs that keep their promises.

5 · A Seat by Any Other Name

How can the same number do two different jobs?

The box office can count tickets sold and work out how many seats remain. Now a customer asks for seat three. Puck writes 3 on two slips. One goes in the ledger, where it means three seats remain. The other goes on a ticket, where it means the seat marked three. He then files them both under “Three.” In fairness, the filing system is very fast.

To send the usher to the right seat, the program has to tell the two threes apart. We'll name both kinds of number and teach Verada which one belongs on a ticket.

Two slips each show the number three. One is labelled Seats left and the other Seat number. Puck stands between them.

The handwriting is identical. The usher still needs to know which slip to take.

Start with zero

The theatre has eight seats. The count of free seats runs from 0 through 8. The number on a seat runs from 1 through 8. Zero is a useful count. Anyone holding a ticket for seat zero will need an unusually imaginative usher.

Give the count a name, Seat_Count, and a constraint of 0 through 8. This is a subtype. It keeps the identity of its underlying integer type and limits the values that fit. Give seat numbers their own integer type, Seat_Number. The new type has its own identity as well as its own bounds. This lets Verada tell the two jobs apart even when the numbers overlap.

Try the two slips

Copy one example project into a working directory. For this experiment, start with a full house. The ledger says 0 seats left. The customer's request is still for seat 3. Before running it, decide whether 3 fits the seat-number type. What does that check tell you about availability?

Example 5-1 · Two slips marked three

Vada · Ada · Example 5-1

main.vadb · Link to this version

with Ada.Text_IO;

procedure Main is
   subtype Seat_Count is Natural range 0 .. 8;
   type Seat_Number is range 1 .. 8;

   Remaining : constant Seat_Count := 0;
   Request   : constant Integer := 3;
   Chosen    : constant Seat_Number := Seat_Number (Request);
begin
   Ada.Text_IO.Put_Line ("Seats left:" & Natural'Image (Remaining));
   Ada.Text_IO.Put_Line ("Seat requested:" & Seat_Number'Image (Chosen));
end Main;
Varc · C-family · Example 5-1

main.va · Link to this version

import Ada.Text_IO;

fn Main() {
    subtype Seat_Count = Natural range 0...8;
    type Seat_Number = range 1...8;

    let remaining: Seat_Count = 0;
    let request: Integer = 3;
    let chosen: Seat_Number = Seat_Number(request);
    Ada.Text_IO.Put_Line("Seats left:" ++ Natural::Image(remaining));
    Ada.Text_IO.Put_Line("Seat requested:" ++ Seat_Number::Image(chosen));
}
Vibe · Python · Example 5-1

main.vb · Link to this version

from verada import Annotated, Interval, RangeInt

type Seat_Count = Annotated[int, Interval(ge=0, le=8)]


class Seat_Number(RangeInt, ge=1, le=8):
    pass


def main() -> None:
    remaining: Seat_Count = 0
    request: int = 3
    chosen: Seat_Number = Seat_Number(request)
    print("Seats left:", remaining)
    print("Seat requested:", int(chosen))


if __name__ == "__main__":
    main()

Syntax note. The declarations name the same two concepts. Vada and Varc use a constrained Natural subtype. Vibe gives its int alias bounds with Annotated and Interval. Vibe's RangeInt declaration introduces the distinct integer type. Read the short class declaration as a range with its own name and identity. We'll give records and their fields a chapter of their own later.

In your project directory, prove, then run. The program prints these two lines.

Seats left: 0
Seat requested: 3

Seat 3 is a valid label even in a full theatre. We'll record the occupancy of each seat in chapter 7.

A ticket marked zero lies beside the faint chalk outline of a chair.

Cross the boundary on purpose

Request starts as an ordinary integer. The expression Seat_Number(Request) is a conversion. It asks for a value of the new type and creates a check that the value fits. In the example, three fits. Change the request to zero and run veradic prove. The same conversion now fails its range check with B4102. Try nine and see the other end of the same bound. Restore the request to three before the next exercise.

There is a second question to ask when two numbers meet. Are they values of the type the operation expects? A range check concerns the value. A type check concerns the identity of that value's type. In the second exercise, you'll compare a count and a seat number that both hold three. Verada reports B2001 because the operands have distinct integer types.

Two tickets marked three are tied with different coloured ribbons.

Puck files the slips in two drawers. He labels one drawer “Other Three.”

Your cue

  1. Predict the result of converting a request of 0, 8, or 9. Try the values one at a time.
  2. Put the request back to 3 and set Remaining or remaining to 3. Before the output, add pragma Assert (Chosen = Remaining); in Vada, assert chosen == remaining; in Varc, or assert chosen == remaining in Vibe. Which check reports the problem?
  3. A clerk wants to check whether seat three is occupied. Which number should the clerk hand over? What information does the clerk still need?
Prompter's notes

Eight fits Seat_Number. Zero and nine fail its range check. Zero still fits Seat_Count. The assertion compares a seat number with a count and fails the type check, even with 3 in both variables. The clerk needs the seat number and a chart that records which seats are taken.

The seat numbers are ready for the tickets. At the next rehearsal, Puck wants to print a strip of them for the ushers without writing the same instruction eight times.

← Previous: Measure for Measure · Contents · Next: Once More Unto the Loop →