Much Ado About Verada

A cheerful guide to programs that keep their promises.

10 · The Cast of Characters

How do I keep a request and its decision together?

The last customer was happy with any free seat. Now Viola asks whether seat 3 is available, and Olivia asks about seat 8. Both want written answers. Puck looks them up on the same chart and writes each seat number and each answer on a separate slip.

One slip says “3.” Another says “Already taken.” A gust of wind brings in two more slips. Which answer goes with which seat? Puck would like to file a complaint against the concept of separate pieces of paper.

Bring with you. A seat number, its offset, and an occupancy flag read from the chart.

A slip with named boxes

Give the receipt two boxes. One holds the requested seat number. The other holds the availability decision. A record groups named fields into one value. The type Receipt describes the boxes, and each receipt fills them in for one request.

Viola's reply pairs seat 3 with Available. Olivia's reply pairs seat 8 with Taken. The customer names label the two replies. Each receipt contains the seat number and decision.

Seat and answer share one slip now. The wind has been reassigned.

The array holds the same kind of value in every slot, an occupancy flag. The receipt holds two different kinds of values. In the printer, slip.requested supplies the seat number and slip.available supplies the flag that chooses the message.

Puck holds a rubber stamp of considerable authority.

Follow a request to its receipt

Give Check_Request or check_request the chart and one Seat_Number. It uses the subtraction from chapter 7 to find the slot. The availability field is true exactly when the taken flag in that slot is false. The function fills in both fields and returns the receipt. The printer reads the receipt and prints the decision.

The receipt records availability on the chart you supply. Later we'll make a booking that changes the chart. Follow each request in the picture to its slot on the mixed row. Which flag gives Viola her answer? Which flag gives Olivia hers?

Example 10-1 · Make and read a receipt

Copy your example project. Find the two field declarations, the expression that fills them, and the two field reads in the printer.

Vada · Ada · Example 10-1

main.vadb · Link to this version

with Ada.Text_IO;

procedure Main is
   type Seat_Number is range 1 .. 8;
   subtype Row_Offset is Natural range 0 .. 7;
   type Seat_Row is array (Row_Offset) of Boolean;

   type Receipt is record
      Requested : Seat_Number;
      Available : Boolean;
   end record;

   function Check_Request (Taken : Seat_Row; Seat : Seat_Number)
     return Receipt
   is
      Offset : constant Row_Offset := Integer (Seat) - 1;
   begin
      return (Requested => Seat, Available => not Taken (Offset));
   end Check_Request;

   procedure Print_Receipt (Slip : Receipt) is
   begin
      Ada.Text_IO.Put_Line ("Seat:" & Seat_Number'Image (Slip.Requested));
      if Slip.Available then
         Ada.Text_IO.Put_Line ("Available.");
      else
         Ada.Text_IO.Put_Line ("Already taken.");
      end if;
   end Print_Receipt;

   Taken : constant Seat_Row :=
     [True, True, False, True, False, True, False, True];
begin
   Print_Receipt (Check_Request (Taken, 3));
   Print_Receipt (Check_Request (Taken, 8));
end Main;
Varc · C-family · Example 10-1

main.va · Link to this version

import Ada.Text_IO;

fn Main() {
    type Seat_Number = range 1...8;
    subtype Row_Offset = Natural range 0...7;
    type Seat_Row = [Row_Offset]Boolean;

    struct Receipt {
        requested: Seat_Number,
        available: Boolean,
    }

    fn check_request(taken: Seat_Row, seat: Seat_Number) -> Receipt {
        let offset: Row_Offset = Integer(seat) - 1;
        return (requested: seat, available: !taken[offset]);
    }

    fn print_receipt(slip: Receipt) {
        Ada.Text_IO.Put_Line("Seat:" ++ Seat_Number::Image(slip.requested));
        if slip.available {
            Ada.Text_IO.Put_Line("Available.");
        } else {
            Ada.Text_IO.Put_Line("Already taken.");
        }
    }

    let taken: Seat_Row = [True, True, False, True, False, True, False, True];
    print_receipt(check_request(taken, 3));
    print_receipt(check_request(taken, 8));
}
Vibe · Python · Example 10-1

seating.vb · Link to this version

from dataclasses import dataclass
from verada import Annotated, Array, Interval, RangeInt


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


type Row_Offset = Annotated[int, Interval(ge=0, le=7)]


class Seat_Row(Array[bool], size=8):
    pass


@dataclass
class Receipt:
    requested: Seat_Number
    available: bool


def check_request(taken: Seat_Row, seat: Seat_Number) -> Receipt:
    offset: Row_Offset = int(seat) - 1
    return Receipt(requested=seat, available=not taken[offset])

main.vb

from seating import Receipt, Seat_Row, check_request


def print_receipt(slip: Receipt) -> None:
    print("Seat:", int(slip.requested))
    if slip.available:
        print("Available.")
    else:
        print("Already taken.")


def main() -> None:
    taken = Seat_Row([True, True, False, True, False, True, False, True])
    print_receipt(check_request(taken, 3))
    print_receipt(check_request(taken, 8))


if __name__ == "__main__":
    main()

Syntax note. Vada declares a record and fills named fields with =>. Varc calls the declaration a struct and uses field: value inside parentheses. Vibe uses a @dataclass with typed fields and fills them with keyword arguments such as requested=seat. All three use a dot to read a named field. In the Vibe project, seating.vb holds the receipt and lookup, and main.vb prints the slips.

Run veradic prove and then veradic run. The two receipts come out in request order.

Seat: 3
Available.
Seat: 8
Already taken.

A taken seat is a valid request

Seat 8 is a valid Seat_Number. The array lookup succeeds, and a receipt with a false availability field is an ordinary result. Seat 9 falls outside Seat_Number at the call, before the lookup. A good type lets the function assume a real seat number and keeps the decision field free to answer the customer's question.

Two paper receipts sit together in a tray.

Your cue

  1. Change Viola's request from seat 3 to seat 1 and Olivia's from seat 8 to seat 7. Predict each reply. Check which field gives the printer each piece of information.
  2. Restore the original requests. Change Olivia's request from seat 8 to seat 9. Run veradic prove. At what boundary does the bad request fail?
  3. Suppose Puck puts the same Boolean on both receipts. Would the record type alone detect a wrong answer? What would you want the function to promise about that field?
Prompter's notes

Viola's reply for seat 1 says Already taken. Olivia's reply for seat 7 says Available. The requested field gives the printer the number. The available field chooses the message.

The argument 9 fails the range check for the seat parameter (B4102). Restore the request for seat 8 after trying it.

The two Boolean values are both allowed in the field. To show that the decision is correct, ask for a postcondition linking it to the flag in the requested slot. The next chapter will put that promise where the caller can see it.

The receipt for seat 3 can now travel to the usher. Before he answers Viola, he wants to know that the answer on the slip agrees with the seating chart.

← Previous: Is There Room in the House? · Contents · Next: The Play Within the Package →