9 · Is There Room in the House?
When can a search safely answer yes or no?
Puck walked the whole row to get a count for the clerk's sign. A customer at the door has a quicker question. “Is there a seat left? Any seat will do.” Puck, who has just learned to count all eight, is visibly disappointed.
He can walk the chart again, looking for a free seat. This time he needs to decide when he's seen enough to answer.
Bring with you. An array of occupancy flags and a checkpoint at the start of each visit.
One seat can settle it
For this rehearsal, put the only free seat at the very end of the row. Puck looks at the first seven and finds them all taken. What can he honestly tell the customer at this point? What would let him answer yes? What would let him answer no?
The moth has a seat and a strong opinion about sample size.
A free seat is enough for yes. It is a witness, one particular seat that makes “there is a free seat” true. On this walk, no takes a visit to every seat, with every one found taken. On our first row, seat 8 supplies the witness for yes.
Give the answer a promise
Chapter 8 asked the count to fit a range. Here we can say exactly what a Boolean answer means. The postcondition says that the result equals “some slot is free.” When some slot is free, the answer must be true. When every slot is taken, it must be false.
A quantifier turns a question about one slot into a question about a whole range. Some asks whether at least one member passes the test. Every asks whether each member passes it. We'll use some over the whole row for the promise, and every over the inspected prefix for the checkpoint.
At the start of a visit, every slot before the current offset is taken. At offset 0 there are no earlier slots. The claim about every earlier slot holds because there is no earlier free slot to contradict it. At offset 7 it covers the first seven seats. The eighth seat still gets its own look.
Example 9-1 · Search for any free seat
Copy your example project. Look for the promise above the work in Has_Free_Seat or has_free_seat, then find the two ways the function returns. The report uses the same search on two charts.
Vada · Ada · Example 9-1
main.vadb · Link to this version
with Ada.Text_IO;
procedure Main is
subtype Row_Offset is Natural range 0 .. 7;
type Seat_Row is array (Row_Offset) of Boolean;
function Has_Free_Seat (Taken : Seat_Row) return Boolean
with Post =>
Has_Free_Seat'Result =
(for some Slot in Row_Offset => not Taken (Slot))
is
begin
for Offset in Row_Offset loop
pragma Loop_Invariant
(for all Seen in 0 .. Offset - 1 => Taken (Seen));
if not Taken (Offset) then
return True;
end if;
end loop;
return False;
end Has_Free_Seat;
procedure Report (Taken : Seat_Row) is
begin
if Has_Free_Seat (Taken) then
Ada.Text_IO.Put_Line ("There is a free seat.");
else
Ada.Text_IO.Put_Line ("Full house.");
end if;
end Report;
begin
Report ([True, True, True, True, True, True, True, False]);
Report ([True, True, True, True, True, True, True, True]);
end Main;
Varc · C-family · Example 9-1
main.va · Link to this version
import Ada.Text_IO;
fn Main() {
subtype Row_Offset = Natural range 0...7;
type Seat_Row = [Row_Offset]Boolean;
fn has_free_seat(taken: Seat_Row) -> Boolean
ensures result == (exists slot in Row_Offset => !taken[slot])
{
for offset in Row_Offset {
invariant forall seen in 0...offset - 1 => taken[seen];
if !taken[offset] {
return True;
}
}
return False;
}
fn report(taken: Seat_Row) {
if has_free_seat(taken) {
Ada.Text_IO.Put_Line("There is a free seat.");
} else {
Ada.Text_IO.Put_Line("Full house.");
}
}
report([True, True, True, True, True, True, True, False]);
report([True, True, True, True, True, True, True, True]);
}
Vibe · Python · Example 9-1
main.vb · Link to this version
from verada import Array, ensures, invariant, result
class Seat_Row(Array[bool], size=8):
pass
def has_free_seat(taken: Seat_Row) -> bool:
ensures(result == any(not taken[slot] for slot in range(8)))
for offset in range(8):
invariant(all(taken[seen] for seen in range(offset)))
if not taken[offset]:
return True
return False
def report(taken: Seat_Row) -> None:
if has_free_seat(taken):
print("There is a free seat.")
else:
print("Full house.")
def main() -> None:
report(Seat_Row([True, True, True, True, True, True, True, False]))
report(Seat_Row([True, True, True, True, True, True, True, True]))
if __name__ == "__main__":
main()Syntax note. Vada writes for some and for all. Varc writes exists and forall. Vibe writes any(… for …) and all(… for …). Vada and Varc give the prefix an inclusive last offset of offset - 1. Vibe's range(offset) stops before the current offset. All three give an empty prefix on the first visit.
Two ways out
- Find a free slot. The slot we just read is a witness for the postcondition. Return true right there.
- See a taken slot. Add this slot to the prefix of taken seats. If another visit follows, the checkpoint is true again. If the loop finishes, the prefix has grown to the whole row, so return false.
Run veradic prove and then veradic run. The first chart has one free seat at the end. The second chart is full.
There is a free seat.
Full house.
Eight. The moth prepared a longer speech but was given a time limit.
Your cue
- In the first chart, mark seat 8 taken and seat 1 free. Predict the first answer and how many visits the search needs. Compare with the original chart.
- Restore the original charts. Shorten only the search loop to visit offsets 0 through 6. Use
0 .. 6in Vada,0...6in Varc, orrange(7)in Vibe. Leave the postcondition over all eight slots. Runveradic prove. On which chart would the shortened search give the wrong answer? - Which return could still be justified on a shortened walk, and what would justify it?
Prompter's notes
With seat 1 free, the search answers yes after one visit. With only seat 8 free, it answers yes after eight visits. The full chart takes eight visits and gives no.
The shortened walk returns false on the chart whose only free seat is seat 8. Verada reports B5102 because the result may violate the postcondition. Follow the original first chart by hand and the missed witness is right there. Restore the eight-slot loop after trying the edit.
An early true still has a witness whenever the search finds a free slot. An early false would need evidence that the skipped slots are taken as well.
“Any seat” was easy to answer with one bit. The next two customers ask for seats 3 and 8 and would like their answers in writing.
← Previous: A Midsummer Night's Proof · Contents · Next: The Cast of Characters →