14 · The Box Office Opens
How do the receipt, the tally, and the booking rules work together?
Puck unlocks the box office. At rehearsal, the usher checked a receipt, a booking, and a cancellation in separate scenes. Tonight the requests come one after another. The clerk needs to answer each one using the chart left by the last. The customers have declined to queue in chapter order.
The usher checks seat 1. Then Viola asks whether seat 3 is free. These two lookups read the chart. Next come the three requests from chapter 12. Viola books seat 3, Olivia asks to book the same seat, and Viola cancels. The clerk keeps a count of free seats for the sign above the window.
Bring with you. The tally from chapter 8, the receipt from chapter 11, and the updates from chapter 12.
Every request gets an answer, even 'no'. Puck considers putting the queue on the playbill.
One chart through the evening
Start with seats 3, 5, and 7 free. Mark the chart on paper as the three requests arrive. Count the free seats at the start and after each request. When Olivia asks for seat 3, which flag should the clerk read? What should the sign say after that request?
The main program joins three pieces we've already built.
- Read a receipt.
Check_Requestgives the requested seat and its availability on the chart supplied to the call. - Record a decision.
ReserveandCancelcan change the requested seat. Their contracts say what happens to that seat and preserve all the other flags. - Update the sign.
Count_Freewalks the current chart and returns a count in the range 0 through 8.
The script then switches to a separate chart with all eight seats taken. The final request, for seat 8, uses that chart. Seat 8 is a valid request in a full house.
Example 14-1 · An evening at the box office
Copy your whole example project. The panel shows the script in the main file. The accompanying seating files bring the lookup, the tally, and both updates together. Start with the assertions immediately after each call. They check the results this particular evening should produce.
Vada · Ada · Example 14-1
main.vadb · Link to this version
with Ada.Text_IO; use Ada.Text_IO;
with Seating; use Seating;
procedure Main is
Taken : Seat_Row :=
[True, True, False, True, False, True, False, True];
Full : Seat_Row :=
[True, True, True, True, True, True, True, True];
First : constant Receipt := Check_Request (Taken, 1);
Slip : constant Receipt := Check_Request (Taken, 3);
Accepted, Released : Boolean;
begin
pragma Assert (First.Requested = 1 and not First.Available);
pragma Assert (Slip.Requested = 3 and Slip.Available);
Put_Line ("Seat 1 is taken.");
Put_Line ("Seat 3 is available.");
Put_Line ("Free seats:" & Seat_Count'Image (Count_Free (Taken)));
Reserve (Taken, 3, Accepted);
pragma Assert (Accepted and Taken (2) and Taken (3) and not Taken (4));
Put_Line ("Seat 3 reserved.");
Put_Line ("Free seats:" & Seat_Count'Image (Count_Free (Taken)));
Reserve (Taken, 3, Accepted);
pragma Assert (not Accepted and Taken (2) and Taken (3) and not Taken (4));
Put_Line ("Seat 3 already taken.");
Put_Line ("Free seats:" & Seat_Count'Image (Count_Free (Taken)));
Cancel (Taken, 3, Released);
pragma Assert (Released and not Taken (2) and Taken (3) and not Taken (4));
Put_Line ("Seat 3 released.");
Put_Line ("Free seats:" & Seat_Count'Image (Count_Free (Taken)));
Reserve (Full, 8, Accepted);
pragma Assert (not Accepted);
pragma Assert (for all Slot in Row_Offset => Full (Slot));
Put_Line ("Full house: seat 8 refused.");
end Main;
Varc · C-family · Example 14-1
main.va · Link to this version
import Ada.Text_IO; use Ada.Text_IO;
import Seating; use Seating;
fn Main() {
var taken: Seat_Row = [True, True, False, True, False, True, False, True];
var full: Seat_Row = [True, True, True, True, True, True, True, True];
let first: Receipt = check_request(taken, 1);
let slip: Receipt = check_request(taken, 3);
var accepted, released: Boolean;
assert first.requested == 1 && !first.available;
assert slip.requested == 3 && slip.available;
Put_Line("Seat 1 is taken.");
Put_Line("Seat 3 is available.");
Put_Line("Free seats:" ++ Seat_Count::Image(count_free(taken)));
reserve(&taken, 3, &accepted);
assert accepted && taken[2] && taken[3] && !taken[4];
Put_Line("Seat 3 reserved.");
Put_Line("Free seats:" ++ Seat_Count::Image(count_free(taken)));
reserve(&taken, 3, &accepted);
assert !accepted && taken[2] && taken[3] && !taken[4];
Put_Line("Seat 3 already taken.");
Put_Line("Free seats:" ++ Seat_Count::Image(count_free(taken)));
cancel(&taken, 3, &released);
assert released && !taken[2] && taken[3] && !taken[4];
Put_Line("Seat 3 released.");
Put_Line("Free seats:" ++ Seat_Count::Image(count_free(taken)));
reserve(&full, 8, &accepted);
assert !accepted;
assert forall slot in Row_Offset => full[slot];
Put_Line("Full house: seat 8 refused.");
}
Vibe · Python · Example 14-1
main.vb · Link to this version
from seating import Receipt, Seat_Row, cancel, check_request, count_free, reserve
def main() -> None:
taken = Seat_Row([True, True, False, True, False, True, False, True])
full = Seat_Row([True, True, True, True, True, True, True, True])
first: Receipt = check_request(taken, 1)
slip: Receipt = check_request(taken, 3)
assert first.requested == 1 and not first.available
assert slip.requested == 3 and slip.available
print("Seat 1 is taken.")
print("Seat 3 is available.")
print("Free seats:", count_free(taken))
accepted: bool = reserve(taken, 3)
assert accepted and taken[2] and taken[3] and not taken[4]
print("Seat 3 reserved.")
print("Free seats:", count_free(taken))
accepted = reserve(taken, 3)
assert not accepted and taken[2] and taken[3] and not taken[4]
print("Seat 3 already taken.")
print("Free seats:", count_free(taken))
released: bool = cancel(taken, 3)
assert released and not taken[2] and taken[3] and not taken[4]
print("Seat 3 released.")
print("Free seats:", count_free(taken))
accepted = reserve(full, 8)
assert not accepted
assert all(full[slot] for slot in range(8))
print("Full house: seat 8 refused.")
if __name__ == "__main__":
main()Syntax note. All three routes pass the same chart to the receipt lookup and the tally. The update calls use the writable forms from chapter 12. In the assertions, offset 2 checks seat 3, offset 3 checks seat 4, and offset 4 checks seat 5.
Run veradic prove, then veradic run. Compare the free-seat lines with your paper chart.
Seat 1 is taken.
Seat 3 is available.
Free seats: 3
Seat 3 reserved.
Free seats: 2
Seat 3 already taken.
Free seats: 2
Seat 3 released.
Free seats: 3
Full house: seat 8 refused.
The run sheet shows the free seats after each step. The transcript prints the first tally after both lookups. Chapter 8's invariant proves the tally stays within bounds for any row. The run checks the exact totals at four points in this evening.
The moth has a press pass. Its review covers every seat.
Your cue
- Trace seat 3 and seat 5 through the three requests. At which calls do the free-seat totals change, and which seat accounts for each change?
- On the separate chart used for the last request, make seat 8 the only free seat. Change the final status assertion to expect acceptance and the last line to
Last seat: seat 8 reserved.Keep the assertion that every slot is taken after the request. Prove and run. - Start with the original chart. The director wants a block of three seats: seat 3 (free), seat 4 (taken), and seat 5 (free). The director wants the whole block or none. What happens if Puck books in order and stops at the first refusal? Write the guarantees for the group operation.
Prompter's notes
The first reservation takes seat 3 and reduces the tally from 3 to 2. The repeat request leaves the tally at 2. Cancellation frees seat 3, bringing the tally back to 3. Seat 5 stays free through all three calls.
With only seat 8 free, reserving it succeeds and the second chart becomes full. The check of every slot still holds.
Seat 3 is free and seat 4 is taken. Separate calls would book seat 3 before refusing seat 4. A group success should book all three seats and preserve the rest. A group refusal should preserve the whole entry chart, including seat 3.
The manager reviews the evening and pins up a new sign. “One ticket per customer.”
← Previous: Exit, Pursued by a Counterexample · Contents · Next: A Play Is Only as Good as Its Script →