11 · The Play Within the Package
How can the usher trust a receipt from the other side of the counter?
Viola is waiting for an answer about seat 3. The usher gives Puck the seating chart and asks for a receipt for that seat. Can the usher use the reply without doing the lookup himself? Puck starts to lean through the window to explain. His ruff has other ideas.
Puck writes a promise for every request. The receipt repeats the requested seat number. It says the seat is available exactly when that seat is free on the chart the usher handed over. Verada checks the lookup against the promise, and the usher can rely on it when he answers the customer.
Bring with you. The receipt from chapter 10 and the before-and-after promises from chapter 4.
The window was measured to fit a receipt. Puck was measured separately.
The counter and the work behind it
Seating groups the chart type, the receipt type, and the lookup function under one name. Vada and Varc call this a package. Vibe calls it a module. The public interface is what other code can use, including the function's promise about its result. In this example, the main program plays the usher. It's the client that calls the function and uses the receipt.
The promise about the returned receipt is a postcondition. It makes one claim about each field. Verada checks both against the function body. At the call site, the client can use both facts in its own assertions.
Follow the facts to the client
The client starts with the familiar mixed chart and asks for seat 3. Before reading the function body, find the two facts that let it assert that the slip says seat 3 is available. Which one follows from the seat argument? Which one needs the chart as well?
Example 11-1 · Check the promise at the counter
Copy your example project. Read the postcondition and then the assertions in the client. Read the body afterward to see how it earns the two promises.
Vada · Ada · Example 11-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;
type Receipt is record
Requested : Seat_Number;
Available : Boolean;
end record;
function To_Offset (Seat : Seat_Number) return Row_Offset is
(Integer (Seat) - 1);
function Check_Request (Taken : Seat_Row; Seat : Seat_Number) return Receipt
with Post => Check_Request'Result.Requested = Seat
and Check_Request'Result.Available = (not Taken (To_Offset (Seat)));
end Seating;package body Seating is
function Check_Request (Taken : Seat_Row; Seat : Seat_Number) return Receipt is
begin
return (Requested => Seat, Available => not Taken (To_Offset (Seat)));
end Check_Request;
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];
Slip : constant Receipt := Check_Request (Taken, 3);
begin
pragma Assert (Slip.Requested = 3);
pragma Assert (Slip.Available);
Ada.Text_IO.Put_Line ("Seat" & Seat_Number'Image (Slip.Requested) & " is available.");
end Main;
Varc · C-family · Example 11-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 struct Receipt {
pub requested: Seat_Number,
pub available: Boolean,
}
pub fn to_offset(seat: Seat_Number) -> Row_Offset = Integer(seat) - 1;
pub fn check_request(taken: Seat_Row, seat: Seat_Number) -> Receipt
ensures result.requested == seat
ensures result.available == !taken[to_offset(seat)]
{
return (requested: seat, available: !taken[to_offset(seat)]);
}
}import Ada.Text_IO;
import Seating; use Seating;
fn Main() {
let taken: Seat_Row = [True, True, False, True, False, True, False, True];
let slip: Receipt = check_request(taken, 3);
assert slip.requested == 3;
assert slip.available;
Ada.Text_IO.Put_Line("Seat" ++ Seat_Number::Image(slip.requested) ++ " is available.");
}
Vibe · Python · Example 11-1
seating.vb · Link to this version
from dataclasses import dataclass
from verada import Annotated, Array, Interval, RangeInt, ensures, result
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 to_offset(seat: Seat_Number) -> Row_Offset:
return int(seat) - 1
def check_request(taken: Seat_Row, seat: Seat_Number) -> Receipt:
ensures(result.requested == seat)
ensures(result.available == (not taken[to_offset(seat)]))
slot = to_offset(seat)
return Receipt(requested=seat, available=not taken[slot])from seating import Receipt, Seat_Row, check_request
def main() -> None:
taken = Seat_Row([True, True, False, True, False, True, False, True])
slip: Receipt = check_request(taken, 3)
assert slip.requested == 3
assert slip.available
print("Seat", int(slip.requested), "is available.")
if __name__ == "__main__":
main()Syntax note. Vada puts the public declaration and Post in seating.vads and the function body in seating.vadb. Varc puts both in seating.va, using pub to expose the declarations and ensures for the promises. Vibe imports the types and function from seating.vb, where ensures(...) starts the function. The source layout differs, and the client's two checks use the same facts in all three routes.
Run veradic prove and then veradic run. The assertion about the seat number uses the first promise. The assertion about availability uses the second promise and the free slot at offset 2. The program prints the usher's line.
Seat 3 is available.
Your cue
- Try Olivia's request for seat 8. Change the seat number in the call and in the assertion about the receipt. Expect a false availability field and print
Seat 8 is taken.Prove and run. - Restore the client. Remove the availability part of the postcondition while keeping the requested-seat part and the body as they were. In Vada, keep the semicolon at the end of the shortened postcondition. Which assertion loses the fact it needs?
- Restore the full postcondition. In the expression that fills the receipt in the body, copy the taken flag directly into the availability field. Run
veradic prove. Where does the failure move?
Prompter's notes
Seat 8 lies at offset 7, which is taken. Both parts of the promise still apply.
With the shorter promise, the client's availability assertion fails to prove (B5105). The lookup still computes the intended answer. The client needs the missing promise to use that answer in its proof.
With the full promise and the reversed decision, the function fails to establish its postcondition (B5102). Repair the expression that fills the receipt, then prove the whole project again.
The usher tells Viola that seat 3 is free. “I'll take it,” she says. Now Puck needs to put that booking on the chart.
← Previous: The Cast of Characters · Contents · Next: To Have and to Hold →