05-02

Chapter 05 · Just enough set theory

Functions are total maps

  • 45 min core
  • Core track
  • Lab: State Space Lab
  • Skills: abstraction, intent

00Recall

One card from 05-01 on quantifiers as queries, one from 03-04 on distinct versus generated states, and one from chapter 02 on what an action says about a variable it does not mention. That last one decides today's Warm-up.

05-01-a from Sets and quantifiers as queries

In GuardedSeatBooking, what does the guard ~\E h \in Guests : (the pair of s and h) \in bookings ask, in words?

Show answer

Whether no guest holds seat s. The \E part is a query over the set of pairs, true as soon as one guest h is paired with s; the ~ negates it.

03-04-a from Reading the numbers

TLC reports more states generated than distinct states. Did it count something twice by mistake?

Show answer

No. Generated counts every successor it computed, including repeats; distinct counts each state once.

02-02-a from UNCHANGED and the frame

An action never mentions the variable alarm. In the logic, what may alarm be after a step of that action?

Show answer

Anything at all. An action constrains only what it says, so with no conjunct about alarm', every value makes it true. UNCHANGED alarm, which means alarm' = alarm, is how you say it stays.

Skip recall Recall is never required.

01Commit

The last lesson ended with a question: if a seat may have at most one guest, what shape should the data have? Here is the box office again, specs/SeatChart.tla, with the seats stored as a function:

TLA+
TypeOK is defined as seat in [Seats to Guests union {Free}]

Init is defined as seat = [s in Seats maps to Free]

seat[s] is read "seat of s": the guest in seat s, or Free, a special value that is not a guest (05-05 says exactly what it is). [Seats -> T] is read "the set of functions from Seats to T". [s \in Seats |-> Free] is read "the function that maps every s in Seats to Free". So TypeOK says: seat is one of the functions from the seats to the guests-or-free.

The cfg uses three seats (aisle, middle, window) and three guests (g1, g2, g3).

Commit a

How many functions are there in [Seats -> Guests \cup {Free}], with three seats and three guests?

Your answer is a whole number.

Answer 64

The box office adds two rules, written into Book: the seat must be free, and the guest may hold only one seat at a time.

Commit b

TLC runs SeatChart from the empty house with those two rules. How many distinct states will it report?

Your answer is a whole number.

Answer 34

02Lab

Lab · State Space LabWhat you’re looking at: every seating chart TLC reached from the empty house, one node per chart, with Book and Cancel steps as edges. Count before you read the reveal.

TLC found no violation

12345678910111213141516171819202122232425262728293031323334

This is a teaching view. Real checking happens with TLC on your machine.

Text view: every state and step
States and their successors
StateseatSteps (action → state)
initial 1(aisle :> Free @@ middle :> Free @@ window :> Free)Book(aisle,g1) → 2; Book(middle,g1) → 3; Book(window,g1) → 4; Book(aisle,g2) → 5; Book(middle,g2) → 6; Book(window,g2) → 7; Book(aisle,g3) → 8; Book(middle,g3) → 9; Book(window,g3) → 10
2(aisle :> g1 @@ middle :> Free @@ window :> Free)Book(middle,g2) → 11; Book(window,g2) → 12; Book(middle,g3) → 13; Book(window,g3) → 14; Cancel(aisle) → 1
3(aisle :> Free @@ middle :> g1 @@ window :> Free)Book(aisle,g2) → 15; Book(window,g2) → 16; Book(aisle,g3) → 17; Book(window,g3) → 18; Cancel(middle) → 1
4(aisle :> Free @@ middle :> Free @@ window :> g1)Book(aisle,g2) → 19; Book(middle,g2) → 20; Book(aisle,g3) → 21; Book(middle,g3) → 22; Cancel(window) → 1
5(aisle :> g2 @@ middle :> Free @@ window :> Free)Book(middle,g1) → 15; Book(window,g1) → 19; Book(middle,g3) → 23; Book(window,g3) → 24; Cancel(aisle) → 1
6(aisle :> Free @@ middle :> g2 @@ window :> Free)Book(aisle,g1) → 11; Book(window,g1) → 20; Book(aisle,g3) → 25; Book(window,g3) → 26; Cancel(middle) → 1
7(aisle :> Free @@ middle :> Free @@ window :> g2)Book(aisle,g1) → 12; Book(middle,g1) → 16; Book(aisle,g3) → 27; Book(middle,g3) → 28; Cancel(window) → 1
8(aisle :> g3 @@ middle :> Free @@ window :> Free)Book(middle,g1) → 17; Book(window,g1) → 21; Book(middle,g2) → 25; Book(window,g2) → 27; Cancel(aisle) → 1
9(aisle :> Free @@ middle :> g3 @@ window :> Free)Book(aisle,g1) → 13; Book(window,g1) → 22; Book(aisle,g2) → 23; Book(window,g2) → 28; Cancel(middle) → 1
10(aisle :> Free @@ middle :> Free @@ window :> g3)Book(aisle,g1) → 14; Book(middle,g1) → 18; Book(aisle,g2) → 24; Book(middle,g2) → 26; Cancel(window) → 1
11(aisle :> g1 @@ middle :> g2 @@ window :> Free)Book(window,g3) → 29; Cancel(aisle) → 6; Cancel(middle) → 2
12(aisle :> g1 @@ middle :> Free @@ window :> g2)Book(middle,g3) → 30; Cancel(aisle) → 7; Cancel(window) → 2
13(aisle :> g1 @@ middle :> g3 @@ window :> Free)Book(window,g2) → 30; Cancel(aisle) → 9; Cancel(middle) → 2
14(aisle :> g1 @@ middle :> Free @@ window :> g3)Book(middle,g2) → 29; Cancel(aisle) → 10; Cancel(window) → 2
15(aisle :> g2 @@ middle :> g1 @@ window :> Free)Book(window,g3) → 31; Cancel(aisle) → 3; Cancel(middle) → 5
16(aisle :> Free @@ middle :> g1 @@ window :> g2)Book(aisle,g3) → 32; Cancel(middle) → 7; Cancel(window) → 3
17(aisle :> g3 @@ middle :> g1 @@ window :> Free)Book(window,g2) → 32; Cancel(aisle) → 3; Cancel(middle) → 8
18(aisle :> Free @@ middle :> g1 @@ window :> g3)Book(aisle,g2) → 31; Cancel(middle) → 10; Cancel(window) → 3
19(aisle :> g2 @@ middle :> Free @@ window :> g1)Book(middle,g3) → 33; Cancel(aisle) → 4; Cancel(window) → 5
20(aisle :> Free @@ middle :> g2 @@ window :> g1)Book(aisle,g3) → 34; Cancel(middle) → 4; Cancel(window) → 6
21(aisle :> g3 @@ middle :> Free @@ window :> g1)Book(middle,g2) → 34; Cancel(aisle) → 4; Cancel(window) → 8
22(aisle :> Free @@ middle :> g3 @@ window :> g1)Book(aisle,g2) → 33; Cancel(middle) → 4; Cancel(window) → 9
23(aisle :> g2 @@ middle :> g3 @@ window :> Free)Book(window,g1) → 33; Cancel(aisle) → 9; Cancel(middle) → 5
24(aisle :> g2 @@ middle :> Free @@ window :> g3)Book(middle,g1) → 31; Cancel(aisle) → 10; Cancel(window) → 5
25(aisle :> g3 @@ middle :> g2 @@ window :> Free)Book(window,g1) → 34; Cancel(aisle) → 6; Cancel(middle) → 8
26(aisle :> Free @@ middle :> g2 @@ window :> g3)Book(aisle,g1) → 29; Cancel(middle) → 10; Cancel(window) → 6
27(aisle :> g3 @@ middle :> Free @@ window :> g2)Book(middle,g1) → 32; Cancel(aisle) → 7; Cancel(window) → 8
28(aisle :> Free @@ middle :> g3 @@ window :> g2)Book(aisle,g1) → 30; Cancel(middle) → 7; Cancel(window) → 9
29(aisle :> g1 @@ middle :> g2 @@ window :> g3)Cancel(aisle) → 26; Cancel(middle) → 14; Cancel(window) → 11
30(aisle :> g1 @@ middle :> g3 @@ window :> g2)Cancel(aisle) → 28; Cancel(middle) → 12; Cancel(window) → 13
31(aisle :> g2 @@ middle :> g1 @@ window :> g3)Cancel(aisle) → 18; Cancel(middle) → 24; Cancel(window) → 15
32(aisle :> g3 @@ middle :> g1 @@ window :> g2)Cancel(aisle) → 16; Cancel(middle) → 27; Cancel(window) → 17
33(aisle :> g2 @@ middle :> g3 @@ window :> g1)Cancel(aisle) → 22; Cancel(middle) → 19; Cancel(window) → 23
34(aisle :> g3 @@ middle :> g2 @@ window :> g1)Cancel(aisle) → 20; Cancel(middle) → 21; Cancel(window) → 25

Or in the terminal. The first command counts the charts TypeOK allows, with a wrapper whose initial states are all of them at once; the second runs the box office from the empty house:

shell
./tla check chapters/05-just-enough-set-theory/specs/MCSeatChart.cfg
./tla check chapters/05-just-enough-set-theory/specs/SeatChart.cfg
Expected output 05-just-enough-set-theory/specs/SeatChart
05-just-enough-set-theory/specs/SeatChart  SeatChart.cfg  (Seats={aisle, middle, window}, Guests={g1, g2, g3}, Free=Free)
parse ok
model check: no violation found in 34 distinct states (127 generated, depth 4)
expected outcome met: pass
coverage: all 2 actions fired
witness HouseFull reached (trace of 4 states)

Reveal for commit a

64. MCSeatChart.tla starts TLC from every function in [Seats -> Guests \cup {Free}] at once, so its distinct count is the size of that set.

Reveal for commit b

34, with no violation of TypeOK or AtMostOneSeat, both actions firing, and HouseFull reached. The other charts allowed by TypeOK give some guest two or three seats, and Book never makes one.

03Reason

If you said 12, you counted arrows from a seat to a value, but a chart is not a set of arrows: it is one arrow per seat, chosen all at once. If you said 27, you had the right method and left out Free, which a seat can hold like any guest.

Choose the aisle seat's value: four options. For each, choose the middle seat's: four again. Then the window's. That is 4 × 4 × 4 = 64, the count in reveal a. In general there are |T| to the power |S| functions from S to T, which is why [S -> T] grows so fast: each seat you add to the cfg multiplies the ceiling by |T|, and each guest you add raises the number being multiplied.

"Total" is the word that fixes the double booking. A dictionary in a program can be missing a key; a TLA+ function cannot. And a function cannot give one seat two values: seat[s] is one thing. The bad state from 05-01, one seat with two guests, is not a state you can write down any more. Compare the guards: the set of pairs needed a quantified query to ask "is the seat taken?", and the function asks seat[s] = Free. The representation does the work the query used to do.

What the representation does not do is the other rule. A guest holding two seats is still a perfectly good function, so Book needs a guard for it, and AtMostOneSeat checks it:

TLA+
\* The seats a guest holds, as a set: a query over the chart.
SeatsOf(g) is defined as {s in Seats : seat[s] = g}

\* No guest holds two seats.
AtMostOneSeat is defined as for all g in Guests : Cardinality(SeatsOf(g)) less than or equal to 1

SeatsOf(g) looks like seat[s], but it is a different thing. seat is a function: a value, stored in a variable, with a domain you can ask for. SeatsOf is an operator: a definition with a parameter, which TLC expands wherever it is used. You cannot store an operator in a variable or ask for its domain. Square brackets apply a function; round brackets apply an operator.

Two arrows, two meanings, and they are easy to swap. [Seats -> T], with the plain arrow, is a set: every chart there is. [s \in Seats |-> Free], with the bar, is one function: the empty house. So TypeOK says seat \in [Seats -> ...], membership in the set, and Init says seat = [s \in Seats |-> Free], equality with one chart. Write Init == seat \in [Seats -> Guests \cup {Free}] by mistake and the box office opens with every chart at once, including charts where one guest already holds all three seats. SANY has nothing to object to, because it is a well-formed spec. It is a different spec. MCSeatChart.tla is that spec on purpose, as a counting device.

That is the gap between the two counts: 64 charts are functions, and 34 of them are reachable: the gap between TypeOK and the actions from 05-01, now with functions.

TLC prints a function as a list of pairs joined together. Here is the break-it-on-purpose check from 03-03, claiming the house never sells out (SeatChart.neverfull.cfg):

  1. State 1 Initial state seat = (aisle :> Free @@ middle :> Free @@ window :> Free)
  2. State 2 Book seat (aisle :> Free @@ middle :> Free @@ window :> Free) becomes (aisle :> g1 @@ middle :> Free @@ window :> Free)
  3. State 3 Book seat (aisle :> g1 @@ middle :> Free @@ window :> Free) becomes (aisle :> g1 @@ middle :> g2 @@ window :> Free)
  4. State 4 Book seat (aisle :> g1 @@ middle :> g2 @@ window :> Free) becomes (aisle :> g1 @@ middle :> g2 @@ window :> g3)

:> is read "maps to" and @@ is read "joined with". Three bookings, and the house is full.

Now the new update. [seat EXCEPT ![s] = g] is read "seat, except that s now maps to g": a new function, equal to seat everywhere but at s.

Write something before openingwhy not write the update as seat'[s] = g, the way you would assign to an array?

Because an action is a formula about two states, not a command. seat'[s] = g says one thing about the new chart: its value at s is g. It says nothing about the other seats, so in the mathematics they could become anything. TLC refuses before it gets that far. TLC works out a successor by finding a value for each primed variable, and it meets seat'[s] before anything has given seat' a value, so there is nothing to look inside. Your terminal says only "TLC threw an unexpected exception."; the raw log adds "In evaluation, the identifier seat is either undefined or not an operator." Read "undefined" as "seat' has no value yet", not as "you never declared seat". The Warm-up starts from exactly that line.

04In your editor

The specs are in chapters/05-just-enough-set-theory/specs/. Check this lesson's exercises with:

shell
./tla check 05-02

The wrapper that counts every chart TypeOK allows:

Output 05-just-enough-set-theory/specs/MCSeatChart
05-just-enough-set-theory/specs/MCSeatChart  MCSeatChart.cfg  (Seats={aisle, middle, window}, Guests={g1, g2, g3}, Free=Free)
parse ok
model check: no violation found in 64 distinct states (289 generated, depth 1)
expected outcome met: pass
coverage: all 2 actions fired

Three habits help with functions in traces and cfgs:

  • Read a long function value seat by seat, not as a line. Better, compare two consecutive states and find the one seat that changed: an action built with EXCEPT changes exactly the entries it names, so a step that changes two seats points at an action that names two, or at a mistake.
  • Equal functions print equally. If two states in a trace look the same, they are the same chart, and the step between them changed some other variable or nothing at all.
  • When TypeOK uses [S -> T], work out |T| to the power |S| before you add a constant to the cfg. That number is a ceiling on what TLC may have to store.

One more piece of EXCEPT you will meet in the next two lessons. Inside the brackets, @ stands for the old value at the point being changed: [qty EXCEPT ![b] = @ + 1] is read "qty, except that b now maps to its old value plus one". It saves writing qty[b] twice, and it makes a step that adds one unit look like one.

Some data needs two keys: a chart per show, a light per floor and room. TLA+ has no special functions of two arguments. A function whose domain is a set of pairs does the job, and the notation lets you leave out the pair's brackets. An office's lights, with Floors and Rooms as constants:

TLA+
\* One switch for every pair of a floor and a room.
TypeOK is defined as light in [Floors cross Rooms to {"on", "off"}]
Init   is defined as light = [x in Floors cross Rooms maps to "off"]

\* light[f, r] means light[<<f, r>>]: one entry, at one pair.
Flip(f, r) is defined as
    lightprime = [light EXCEPT ![f, r] = IF @ = "on" THEN "off" ELSE "on"]

light[f, r] is read "light of f, r". In [x \in Floors \X Rooms |-> "off"], x is a whole pair, and x[1] and x[2] are its two parts, because a pair is a sequence of length 2 (05-03). EXCEPT ![f, r] changes the one entry at that pair, and @ is its old value. The Core exercise moves the seating chart to this shape.

05Practice

Warm-up. Book is written the way an array assignment would be, and TLC stops on it. Rewrite the update with EXCEPT.

Core. The box office sells two shows. Finish a teammate's change that turns the chart into a function of a show and a seat.

05-02-ex1Warm-up5 min

05-02-ex1 · EXCEPT, not assignment

Tier: warm-up. Time: about 5 minutes.

The seating chart from the lesson, with Book's update written the way you would assign to an array: seat'[s] = g.

  1. Run ./tla check 05-02-ex1. TLC stops with "model check: TLC error: TLC threw an unexpected exception." That line says only that something went wrong. Open the file named on the raw log: line in your terminal and find the cause: "In evaluation, the identifier seat is either undefined or not an operator." The first line under the summary points at seat'[s]. What had TLC not worked out yet when it got there?
  2. Fill gap g1: the new chart is the old chart with seat s now holding g. Write it with EXCEPT.
  3. Run the check. It should pass with both actions firing and HouseFull reached.

Cancel, a few lines below, already updates the chart the right way.

Hints: HINTS.md, under "05-02-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/02-ex1-except-not-assignment/; open it after you have a passing answer of your own.

If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Done when: ./tla check 05-02-ex1 prints "goal met: pass".

./tla check 05-02-ex1

Repo chapters/05-just-enough-set-theory/exercises/02-ex1-except-not-assignment/

Hints

Hint 1

TLC's error points at seat'[s]. When TLC evaluates Book, it has not yet decided what seat' is, so it cannot look inside it.

Hint 2

An action must say what the whole new value of seat is. Cancel, below, does that for freeing a seat.

Hint 3 (nearly the answer)

Write seat' equal to an EXCEPT expression on seat that changes the entry for s to g, the same shape as Cancel with g in place of Free.

05-02-ex2Core8 min

05-02-ex2 · Two shows

Tier: core. Time: about 8 minutes.

The box office now sells two performances, matinee and evening, with the same seats and guests. The chart becomes one function of two arguments: seat[sh, s] is the guest in seat s at show sh, or Free. The rule becomes: a guest may hold one seat per show. A teammate started the change. Cancel, Next, the invariant OneSeatPerShow, the action property NoResale (a sold seat never passes to another guest) and the two witnesses already use the new shape; TypeOK, Init and Book still describe one show.

  1. Run ./tla check 05-02-ex2. TLC stops with "Attempted to apply function:", and the raw log: line in your terminal names the file with the rest: the argument was <<matinee, aisle>>, "which is not in the domain of the function". The chart's domain is still Seats. Who asked it about a pair? The terminal points at Init, because the raw log says the error came "While working on the initial state": TLC checks the invariants on every initial state. The last lines of the raw log's list of nested positions are the expression that asked.
  2. Read Cancel: it is your model for the new shape. seat[sh, s] means seat[<<sh, s>>], and EXCEPT ![sh, s] changes the one entry at that pair.
  3. Change TypeOK (gap g1), Init (g2) and Book (g3) so that the chart's domain is Shows \X Seats. 05-02's IN YOUR EDITOR shows [x \in A \X B |-> e].
  4. Run the check until it passes with both witnesses: both shows full, and some guest with a seat at each show.

The check also runs two course tests of your TypeOK, and it must be violated in both: MCSeatChart.cfg starts from the teammate's old chart, a function of the seats alone, and MCSeatChart.range.cfg from a chart over Shows \X Seats with a seat's name in every entry. Leave the MC files as they are.

Hints: HINTS.md, under "05-02-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/02-ex2-two-shows/; open it after you have a passing answer of your own.

If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

All three cfgs must meet their goals, each in its own block of output: "goal met: pass" for SeatChart.cfg and "goal met: violation TypeOK" for both MCSeatChart cfgs. Read each block's goal line, not only the last line.

Done when: ./tla check 05-02-ex2 prints "goal met: pass".

./tla check 05-02-ex2

Repo chapters/05-just-enough-set-theory/exercises/02-ex2-two-shows/

Hints

Hint 1

The starter's TypeOK and Init still describe one chart over Seats, but OneSeatPerShow asks it about <<matinee, aisle>>. Every place that builds or reads the chart has to agree on the new domain, Shows \X Seats. Cancel already does.

Hint 2

TypeOK is a function set whose domain is the set of pairs. Init builds one function over that set with |->, binding one name to a whole pair. In Book, every seat[s] becomes seat[sh, s], and the update names one pair, as in Cancel.

Hint 3 (nearly the answer)

All three gaps use the domain Shows \X Seats: a function set in TypeOK, a function built with |-> in Init. Book(sh, s, g) has the same two guards as before, each now asking about show sh, and an update shaped like Cancel's.

06Explain it

Open NOTES.md and answer the three prompts under 05-02, about the Core exercise.

output
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:

Compare your answers with "05-02 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "The chart is now one function over pairs, seat \in [Shows \X Seats -> Guests \cup {Free}], so seat[sh, s] is one value and a seat at one show cannot hold two guests. Book(sh, s, g) checks the seat is free and g holds no seat at that show, so the rule is per show, and GuestAtBoth confirms a guest can sit at both shows. It assumes a seat at one show is independent of the same seat at the other; a rule such as "a guest keeps the same seat for both shows" would need a new guard and a new invariant. TLC checked two shows, two seats and two guests, from the empty house."

A weak answer: "I changed every seat[s] to seat[sh, s]." It describes the edit, not why the new shape is right, and names no assumption or bound.

Done?