7 · All the World's an Array
How do I find a particular seat on the chart?
“Five sold,” says the ledger. Seat eight asks for a second opinion.
Puck can print a ticket for each seat, and his ledger tells him how many tickets he's sold. A customer asks whether seat eight is still free. To answer, Puck needs a chart that keeps track of which seats are taken, with a way to go from a ticket number to its place on the chart.
Give the theatre an array, a fixed row of eight values for this example. Each value is a Boolean. True means the seat is taken, and False means it is free. The row stores the eight answers in seat order. Now a question about one seat can go straight to its answer.
Puck numbers the seats for customers and the floor for the program. The floor is taking it well.
Two sets of labels
The audience sees seat numbers 1 through 8. In our array, the index selects a slot, and we have chosen slots 0 through 7. An offset tells us how far a slot lies from the first one. For this array, the offset is also the index. Seat 1 is zero steps from the start, so it lives at offset 0. Seat 8 is seven steps from the start, so it lives at offset 7.
Put that subtraction in a small function, To_Offset or to_offset. Its input is a Seat_Number and its result has the range 0 through 7. The expression converts the seat number to an ordinary integer and subtracts one. Because the caller supplies a seat number from 1 through 8, the subtraction always yields a result from 0 through 7. Each lookup uses that result to select a slot.
Read one seat, then walk the row
Copy your whole example project into a working directory. Seating gives the row and the conversion their names. The program first looks up seat 8. In the picture, follow the highlighted column from seat 8 to offset 7 and read its flag. Then use the loop from the last chapter to predict which free seats the program will list.
Example 7-1 · One lookup and a walk
Vada · Ada · Example 7-1
seating.vads · Link to this version
package Seating 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;
function To_Offset (Seat : Seat_Number) return Row_Offset is
(Integer (Seat) - 1);
end Seating;with Ada.Text_IO;
with Seating; use Seating;
procedure Main is
Taken : constant Seat_Row :=
[True, True, False, True, False, True, False, True];
Requested : constant Seat_Number := 8;
begin
if Taken (To_Offset (Requested)) then
Ada.Text_IO.Put_Line ("Requested seat is taken.");
else
Ada.Text_IO.Put_Line ("Requested seat is free.");
end if;
for Seat in Seat_Number loop
if not Taken (To_Offset (Seat)) then
Ada.Text_IO.Put_Line ("Free seat:" & Seat_Number'Image (Seat));
end if;
end loop;
end Main;
Varc · C-family · Example 7-1
seating.va · Link to this version
package Seating {
pub type Seat_Number = range 1...8;
pub subtype Row_Offset = Natural range 0...7;
pub type Seat_Row = [Row_Offset]Boolean;
pub fn to_offset(seat: Seat_Number) -> Row_Offset = Integer(seat) - 1;
}import Ada.Text_IO;
import Seating; use Seating;
fn Main() {
let taken: Seat_Row = [True, True, False, True, False, True, False, True];
let requested: Seat_Number = 8;
if taken[to_offset(requested)] {
Ada.Text_IO.Put_Line("Requested seat is taken.");
} else {
Ada.Text_IO.Put_Line("Requested seat is free.");
}
for seat in Seat_Number {
if !taken[to_offset(seat)] {
Ada.Text_IO.Put_Line("Free seat:" ++ Seat_Number::Image(seat));
}
}
}
Vibe · Python · Example 7-1
seating.vb · Link to this version
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
def to_offset(seat: Seat_Number) -> Row_Offset:
return int(seat) - 1from seating import Seat_Number, Seat_Row, to_offset
def main() -> None:
taken = Seat_Row([True, True, False, True, False, True, False, True])
requested: Seat_Number = 8
if taken[to_offset(requested)]:
print("Requested seat is taken.")
else:
print("Requested seat is free.")
for label in range(1, 9):
seat = Seat_Number(label)
if not taken[to_offset(seat)]:
print("Free seat:", label)
if __name__ == "__main__":
main()Syntax note. Vada declares the array over Row_Offset and uses parentheses for a lookup. Varc declares the same index range through Row_Offset and uses square brackets. Vibe's Array[bool] with size=8 gives eight slots starting at zero and uses square brackets as well. These examples choose the same mapping in all three syntaxes. Vada and Varc can loop through Seat_Number directly. Vibe visits the labels in range(1, 9) and converts each label to a seat number at the call boundary.
In your project directory, prove, then run. Seat 8 is taken, and the loop prints the public labels on the free seats.
Requested seat is taken.
Free seat: 3
Free seat: 5
Free seat: 7
The loop visits the seats in order. Its condition reads one slot and prints the label when that slot is free. The index tells the program where to look. The label tells the customer where to sit.
Puck's prompt · an optional REPL experiment
A REPL lets you try an expression or statement at a prompt. Puck predicts that seats 1 and 8 map to offsets 0 and 7. He can type each call and see what it returns. Start in your example directory and enter each input on its own line.
- Vada. Start
veradic repland enter:with Seating. Then enterSeating.To_Offset (1)andSeating.To_Offset (8). - Varc. Start
veradic repl --syntax varcand enter:with Seating. Then enterSeating.to_offset(1)andSeating.to_offset(8). - Vibe. Start
veradic repl --syntax vibeand enterfrom seating import to_offset. Then enterto_offset(1)andto_offset(8).
The REPL prints offset 0 for seat 1 and offset 7 for seat 8. Type :quit to leave. The REPL uses advisory proof by default, so it can warn about an unproved check and continue with runtime checks active. Use veradic prove on the whole project to check the conversion and lookups under strict proof. For more session commands, see the REPL reference.
The REPL answers every line at once. Puck asks it to hold.
Your cue
- Try two lookups on the same chart. Set
Requestedorrequestedto seat1for the first run and seat3for the next. Predict each status line. Will either lookup change the free-seat list? - Restore the request to
8. In the seating package or module, remove the- 1from the conversion function and runveradic prove. Which legal seat number would now produce a value outsideRow_Offset? - Explain why a function that always returns offset zero could satisfy the result range while sending every customer to the first seat. What would the endpoint experiment show?
Prompter's notes
Seat 1 is taken. Seat 3 is free. Both requests leave the row as it was, so the free labels are still 3, 5, and 7. With the subtraction removed, seat 8 would return offset 8. The function's result range ends at 7, so the range check fails with B4102. Restore the subtraction after the experiment.
Zero is a valid offset, and a constant-zero function would return it for both seat 1 and seat 8. Trying the last seat would reveal the wrong mapping. The result range establishes that the slot exists. The conversion expression establishes which slot we choose.
Puck can now look up a seat and call out the free ones. The clerk wants a number for the sign above the window. Next, Puck will get that number from the same chart.
← Previous: Once More Unto the Loop · Contents · Next: A Midsummer Night's Proof →