05-05
Chapter 05 · Just enough set theory
Constants and model values
00Recall
One card from 05-04 on what CHOOSE explores, one from 03-05 on what a TypeOK-only check says, and one from 03-01 on what a cfg tells TLC. Today both halves of a model can lie: the module, and the cfg that gives its constants their values.
05-04-a from CHOOSE is deterministic
A spec models "a delivery of some size from 1 to 3" as CHOOSE n \in 1..3 : TRUE. What does TLC explore?
Show answer
Deliveries of one size only, the same every time: with constant arguments, CHOOSE denotes one fixed value. A choice the world makes needs \E n \in 1..3.
03-05-a from TypeOK, and the check you didn't configure
The cfg names only INVARIANT TypeOK, and TLC finds no violation. May you say the thermostat keeps the room comfortable?
Show answer
No, for two reasons. TLC never checked Comfortable, because the cfg did not name it. And even a cfg that names it gives "no violation found in this model": one finite model, not a proof.
03-01-b from One command
Name three things a cfg tells TLC that the module does not.
Show answer
The values of the constants, which formula is the system (SPECIFICATION, or INIT and NEXT), and which invariants to check.
Skip recall Recall is never required.
01Commit
Back to the seating chart from 05-02. A teammate changed one line of Cancel, "because 0 means empty", and saved it as specs/SentinelChart.tla:
\* Whoever holds seat s gives it back. The teammate's version writes 0.
Cancel(s) is defined as
and seat[s] not equal to Free
and seatprime = [seat EXCEPT ![s] = 0]TypeOK is unchanged: seat \in [Seats -> Guests \cup {Free}], and the cfg names it.
Commit a
TLC checks SentinelChart, with TypeOK and AtMostOneSeat named in the cfg. What will it report?
- SANY rejects the module: 0 is not a guest.
- A violation of TypeOK, after a booking and a cancel.
- A TLC error: a number cannot be stored in a chart of guests.
Answer A violation of TypeOK, after a booking and a cancel.
- If you picked A
- SANY checks that names are defined and used with the right number of arguments. It has no idea what kind of value a seat should hold, because TLA+ has no types to tell it.
- If you picked C
- Nothing in TLA+ stops a function from holding 0 next to guests. TLC stores the new chart without complaint. Only an invariant you wrote says it is wrong.
Now a mistake in the cfg rather than the module. The real SeatChart cfg says Free = Free. Here is specs/SeatChart.samefree.cfg:
CONSTANTS Seats = {aisle, middle, window} Guests = {g1, g2, g3} Free = g1Commit b
TLC checks SeatChart with Free = g1. What will it report?
- An error: a cfg cannot give one constant the value of another.
- A violation of AtMostOneSeat, in the initial state.
- No violation. Free and g1 have different names, so the box office works as before.
Answer A violation of AtMostOneSeat, in the initial state.
- If you picked A
- The cfg may give a constant any value it can write, including a model value it has already named. TLC accepts the line and builds the model.
- If you picked C
- A name is not a value. With Free = g1, "free" and "held by g1" are the same value. Look at what that makes the initial state.
02Lab
The teammate's Cancel, with and without TypeOK named, then the cfg mistake:
./tla check chapters/05-just-enough-set-theory/specs/SentinelChart.cfg
./tla check chapters/05-just-enough-set-theory/specs/SentinelChart.notype.cfg
./tla check chapters/05-just-enough-set-theory/specs/SeatChart.samefree.cfg05-just-enough-set-theory/specs/SentinelChart05-just-enough-set-theory/specs/SentinelChart SentinelChart.cfg (Seats={aisle, middle, window}, Guests={g1, g2, g3}, Free=Free)
parse ok
model check: invariant TypeOK violated; trace has 3 states (2 steps); 15 distinct states
expected outcome met: violation TypeOK
trace (changed variables only; ./tla trace --full shows all):
1 Init seat = (aisle :> Free @@ middle :> Free @@ window :> Free)
2 Book seat: (aisle :> Free @@ middle :> Free @@ window :> Free) -> (aisle :> g1 @@ middle :> Free @@ window :> Free)
3 Cancel seat: (aisle :> g1 @@ middle :> Free @@ window :> Free) -> (aisle :> 0 @@ middle :> Free @@ window :> Free)Reveal for commit a
TypeOK is violated after a trace of 3 states: a booking, then a cancel that writes 0. Without TypeOK in the cfg, TLC finds no violation at all, in 86 distinct states, more than the 34 of the real chart.
05-just-enough-set-theory/specs/SeatChart.samefree05-just-enough-set-theory/specs/SeatChart.samefree SeatChart.samefree.cfg (Seats={aisle, middle, window}, Guests={g1, g2, g3}, Free=g1)
parse ok
model check: invariant AtMostOneSeat violated; an initial state violates it
expected outcome met: violation AtMostOneSeat
trace (changed variables only; ./tla trace --full shows all):
1 Init seat = (aisle :> g1 @@ middle :> g1 @@ window :> g1)Reveal for commit b
AtMostOneSeat is violated by the initial state. With Free = g1, "every seat is free" is "every seat belongs to g1", so guest g1 holds all three seats before anyone has booked.
03Reason
If you picked an answer where SANY or TLC refuses the 0, you expected types. TLA+ has none.
So the teammate's 0 is a legal value, and the run without TypeOK passes. Its extra states are charts with a 0 in them. Read Book's guard, seat[s] = Free, against a seat holding 0: it is false, so a cancelled seat can never be sold again. Nothing failed; the box office quietly shrank. The witness suffers too: a seat holding 0 is not Free, so HouseFull counts it as sold, and it now also holds in states where seats hold 0 and nobody sits in them. The predicate no longer means what its name says. TypeOK turned all of that into a violation, at the step that wrote the 0.
Why did TLC not stop at seat[s] # Free with an error, when seat[s] was 0? Because of what Free is. The cfg line Free = Free makes it a model value: a value TLC invents, equal to itself and unequal to every other value.
Model values are the course's default for sets of things that have no arithmetic: seats, guests, bins, keys. Seats = {aisle, middle, window} gives three distinct values that TLC cannot add, order or confuse with a string. A spec over them can only compare them for equality, which is exactly what a seat is for. Chapter 12 shows one more benefit, symmetry, and its warning.
Compare the alternatives. Numbers, Seats = 1..3, invite arithmetic the system does not have: nothing stops an action from writing s + 1 and inventing a fourth seat. Strings, {"aisle", "middle", "window"}, are safe from arithmetic, but an action can spell them too, and a misspelt "aisel" in an action is a new value rather than an error. The module cannot spell a model value at all: it reaches the seats only through the constant Seats, and SANY rejects a misspelt constant name. The cfg is another matter. Every bare name on the right of its CONSTANTS line is a model value, so a typo there makes a new value without complaint, exactly like a misspelt string; that is one more job for ASSUME. TLC prints a model value bare, as aisle, so you can tell it from the string "aisle" in a trace.
Write something before openingthe module says CONSTANTS Seats, Guests, Free. Which of the cfg's values were wrong in the second commit, and could TLC have known?
Free = g1. Each value on its own is fine, and TLC cannot know what you meant by the names. The module did have an expectation about how Free relates to the guests, but it lived only in a comment. TLC reads formulas, not comments.
A constant is a name the module declares and the cfg defines. Change the values and you have a different model; the module is the same. That makes the cfg half of every result, and a cfg can be wrong in ways the module never sees. The cure was in 03-01: ASSUME, a formula about the constants that TLC checks before it computes a single state. The Warm-up asks you to write the one the box office needs.
Write something before openingan ASSUME and an invariant are both formulas TLC checks. What is the difference?
An ASSUME must mention only constants, so it is true or false of the model as a whole, and it is part of the module: TLC checks it under every cfg that uses the module, before any state exists. An invariant is checked only when a cfg names it, and then in every reachable state. One that mentions only constants has the same value in every state, and TLC warns that it is a constant-level formula and suggests an ASSUME (the 05-01 Core's starter prints that warning). An ASSUME that fails means "this cfg does not describe the system the module is about"; an invariant that fails means "the system can reach a bad state".
Put the two halves together and the habit of this chapter is complete. Choose representations that make bad states unwritable: a function, not a set of pairs. Write TypeOK and name it in the cfg, because nothing else checks kinds of values. Use model values for things without arithmetic. And state what the constants must satisfy with ASSUME, so that the cfg is checked as well as the module.
04In your editor
The specs are in chapters/05-just-enough-set-theory/specs/. Check this lesson's exercises with:
./tla check 05-05The run without TypeOK, which finds nothing wrong:
05-just-enough-set-theory/specs/SentinelChart.notype05-just-enough-set-theory/specs/SentinelChart.notype SentinelChart.notype.cfg (Seats={aisle, middle, window}, Guests={g1, g2, g3}, Free=Free)
parse ok
model check: no violation found in 86 distinct states (319 generated, depth 7)
expected outcome met: pass
coverage: all 2 actions fired
witness HouseFull reached (trace of 4 states)Before you trust a cfg, read its CONSTANTS line as if a stranger wrote it. Is each set the size the module's comments promise? Is any model value used for two constants? Then ask what the module assumes about its constants, and whether an ASSUME says so.
Read the cfg's own notation slowly the first few times. Free = Free reads "the constant Free is a fresh model value, also named Free". Seats = {aisle, middle, window} makes three fresh model values in one line and gives the constant the set of them. Cap = 3 gives an ordinary number. The right-hand sides are values, not formulas: a cfg cannot say "some set of three seats", only which one.
Look for three shapes of assumption. Two constants that must not overlap. A set that must not be empty, because a quantifier over {} makes every \A true and every \E false (05-01). A number in a range, such as MaxDelivery \in 1..Cap for the storeroom of 05-04: a delivery bigger than a bin could never be put away, so a correct guard would never let it happen, and no check would tell you that a size you configured is dead. Write each one as an ASSUME next to the CONSTANTS it constrains, and a wrong cfg costs you one error message instead of a misleading pass. TLC's message names a failed assumption by its line and column in the module, not by what it means, so put a comment above each ASSUME saying, in words, what a cfg that breaks it got wrong.
05Practice
Warm-up. A teammate's cfg gives Free the same model value as a guest. Write the ASSUME that stops TLC before it explores a single state.
Core. A teammate's storeroom marks empty bins with the string "empty", and its TypeOK checks only that every bin has an entry. Make TypeOK say what a bin holds, and keep numbers in the bins.
05-05-ex1Warm-up5 min
05-05-ex1 · Assume the constants
Tier: warm-up. Time: about 5 minutes.
The seating chart with a teammate's cfg that sets Free = g1. The module's comments say what it expects of its constants, but no formula checks it.
- Run
./tla check 05-05-ex1. With the teammate's cfg,AtMostOneSeatfails in the initial state, and the same happens withSeatChart.lastguest.cfg, a second teammate's cfg that setsFree = g3. With the course's cfg,SeatChart.grader.cfg, everything passes. - Fill gap g1 in
SeatChart.tla: anASSUMEthat turns the expectation in the comments into a formula about the constants. - Run the check.
The goal for both teammates' cfgs is an error: your ASSUME should stop TLC before it explores a single state, whichever guest Free was aliased to. The goal for the course's cfg is a pass: with the right constants your ASSUME holds, so it does not forbid a correct model. Leave the cfgs as they are; the mistakes in them are the point.
Hints: HINTS.md, under "05-05-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/05-ex1-assume-the-constants/; open it after you have a passing answer of your own.
All three cfgs must meet their goals, each in its own block of output: "goal met: error Assumption" for SeatChart.cfg and SeatChart.lastguest.cfg, and "goal met: pass" for SeatChart.grader.cfg. Read each block's goal line, not only the last line.
Done when: ./tla check 05-05-ex1 prints "goal met: error Assumption".
./tla check 05-05-ex1
Repo chapters/05-just-enough-set-theory/exercises/05-ex1-assume-the-constants/
Hints
Hint 1
The module's comment on Free says what the constants must satisfy. Turn that comment into a formula.
Hint 2
An ASSUME mentions only constants. The relation you need is "is not an element of".
Hint 3 (nearly the answer)
In place of TRUE: the constant that marks a free seat, then the TLA+ operator read "is not an element of", then the set of guests.
05-05-ex2Core8 min
05-05-ex2 · No sentinels
Tier: core. Time: about 8 minutes.
A teammate's storeroom. When a bin runs out, Ship writes the string "empty" into it, "for the restock report". The cfg names TypeOK, but the teammate's TypeOK checks only that every bin has an entry.
- Run
./tla check 05-05-ex2. TLC stops with an error insideReceive: "The first argument of < should be an integer".Receivedid nothing wrong. Find the step that put the string there. - Change
TypeOK(gap g1) so that it says what kind of value a bin holds: a whole number of units, from 0 toCap. - Change
Ship(gap g2) so that only numbers go intoqty. The report,EmptyBins, already derives the empty bins from the numbers. - Run the check until it passes with both witnesses. Delete both
\* GAPmarkers when you are done.
The check also runs four course cfgs. Three test your TypeOK: MCStoreroom.cfg runs the teammate's Ship; its goal is an error inside TypeOK, not a violation, and hint 3 says why. MCStoreroom.overfull.cfg and MCStoreroom.underflow.cfg each run a storeroom with one off-by-one guard, so a bin reaches Cap + 1 or -1, and your TypeOK must be violated in both. MCStoreroom.step.cfg checks that every step of your Spec moves one bin by one unit. Leave the MC files as they are.
Hints: HINTS.md, under "05-05-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/05-ex2-no-sentinels/; 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 five cfgs must meet their goals, each in its own block of output: "goal met: pass" for Storeroom.cfg and MCStoreroom.step.cfg, "goal met: error Evaluating invariant TypeOK failed" for MCStoreroom.cfg, and "goal met: violation TypeOK" for the other two. Read each block's goal line, not only the last line.
Done when: ./tla check 05-05-ex2 prints "goal met: pass".
./tla check 05-05-ex2
Repo chapters/05-just-enough-set-theory/exercises/05-ex2-no-sentinels/
Hints
Hint 1
The error is in Receive, but Receive only compares. Look at the trace's last step: which action wrote a value that is not a number? Then ask why the teammate's TypeOK did not object to it.
Hint 2
DOMAIN qty = Bins says every bin has an entry, and a string is an entry. A TypeOK that says what a bin holds is a function set: every bin maps to a number from 0 to Cap. In Ship, subtract one, always.
Hint 3 (nearly the answer)
TypeOK says qty is in the set of functions from Bins to 0..Cap. Ship loses the IF. On the course's MCStoreroom.cfg, TLC then stops inside your TypeOK with "Evaluating invariant TypeOK failed.": asked whether the string "empty" is in 0..Cap, it will not guess, so it reports an error rather than a violation. That error is the goal there.
05-mxMixed20 min · mastery path
05-mx · Key cabinet
Tier: mixed. Time: about 20 minutes. Off the core clock: this is on the mastery path.
The key cabinet at a site office. Staff take a key from its hook and hang it back later, and nobody carries two keys. A teammate's model passes: no invariant fails, ./tla reports every action fired (it counts steps that change nothing), the witness is reached, and the action property HolderReturns holds. Their cfg ends with one line they added "because TLC kept complaining".
- Run
./tla check 05-mx. The teammate's cfg passes. The course's cfg,KeyCabinet.grader.cfg, has the same checks without that last line, plusReturnHangsKey, and reports something else. Read its trace. - Decide whether the teammate's line is a reason about the cabinet. Fill gap g1 in
KeyCabinet.cfgaccordingly. - Fill gap g1 in
KeyCabinet.tla: only the person carrying keykcan hang it back. - Run the check until both cfgs pass.
Leave KeyCabinet.grader.cfg as it is. The grader cannot see your decision about the teammate's line: once Return is right, KeyCabinet.cfg passes with the line or without it, and only the course's cfg checks the cabinet with deadlock checking on. Decide by the reason, not by the result.
Hints: HINTS.md, under "05-mx hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/mx-key-cabinet/; 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.
Both cfgs must meet their goals, each in its own block of output: "goal met: pass" for KeyCabinet.cfg and for KeyCabinet.grader.cfg. Read each block's goal line, not only the last line.
Done when: ./tla check 05-mx prints "goal met: pass".
./tla check 05-mx
Repo chapters/05-just-enough-set-theory/exercises/mx-key-cabinet/
Hints
Hint 1
Read the course cfg's trace to the end. In its last state, which actions could still fire?
Hint 2
The teammate's last cfg line turns off a check rather than fixing anything. A cabinet where every key is out and nobody can return one is stuck, and nothing about that is the design. Then read Return's guard aloud.
Hint 3 (nearly the answer)
Delete the teammate's line from KeyCabinet.cfg. In Return, the guard should say the key is held by p, not that it is on its hook.
06Explain it
Open NOTES.md and answer the three prompts under 05-05, about the Core exercise.
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-05 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "TypeOK says every bin holds a number from 0 to Cap; the teammate's version only said every bin had an entry, which a string satisfies. Now TLC stops inside TypeOK, at the step that wrote "empty", rather than later in Receive. Ship now always subtracts one, and the report derives empty bins from the numbers, so no value of another kind enters qty. It assumes nothing else writes to qty. TLC checked two bins with Cap = 2; it says nothing about larger stores."
A weak answer: "TLA+ had a type error, and I fixed the type." TLA+ has no types; the answer misses that TypeOK is an invariant someone has to write, and that a weak one passes silently.
Done?