05-03
Chapter 05 · Just enough set theory
Records, sequences, SUBSET
00Recall
One card from 05-02 on counting the functions in [S -> T], one from 05-01 on the guard that asked the wrong question, and one from 03-02 on why an invariant's trace stops at the bad state. Today's trace is short, and every value in it is a set, a record or a sequence.
05-02-a from Functions are total maps
How many functions does [S -> T] contain, for finite sets S and T?
Show answer
|T| to the power |S|: one independent choice of value from T for each element of S.
05-01-b from Sets and quantifiers as queries
The teammate's guard asked whether the pair of s and g was missing from bookings. Why did TLC find a double booking?
Show answer
The guard asks whether this guest has this seat, not whether anyone does. A second guest passes it for a seat that is already sold.
03-02-a from Your first counterexample
Why does a TLC trace for an invariant violation stop at the bad state instead of showing a whole infinite behavior?
Show answer
Once a state breaks an invariant, nothing later can repair it, so the path to that state is all the evidence needed.
Skip recall Recall is never required.
01Commit
A shared document. specs/Permissions.tla gives each user a set of rights, drawn from Rights == {"read", "write"}. The owner starts with both; guests start with none. Anyone can pass on rights they hold:
\* User u passes the rights R on to user v.
Share(u, v, R) is defined as
and u not equal to v
and R not equal to {}
and R subset of or equal to access[u]
and accessprime = [access EXCEPT ![v] = @ union R]
and logprime = Record([from maps to u, to maps to v, rights maps to R])\subseteq is read "is a subset of". In Next, R ranges over SUBSET Rights, read "the set of all subsets of Rights". Inside EXCEPT, @ stands for the old value, access[v]. The product's one rule is WriteNeedsRead: nobody can write a document they cannot read.
Commit a
TLC checks WriteNeedsRead on Permissions, with an owner and two guests. What will it report?
- No violation: the owner holds both rights, so anything the owner shares includes read.
- A violation: some guest can write but not read.
- An error: SUBSET Rights is a set of sets, which TLC cannot range over.
Answer A violation: some guest can write but not read.
- If you picked A
- List the subsets of the owner's rights. One of them contains write and not read, and nothing in Share rules it out.
- If you picked C
- SUBSET of a small finite set is a small finite set, and TLC enumerates it like any other. Its elements happen to be sets; that is fine.
Every share also goes into an audit log. Each entry is a record, a value with named fields, and TypeOK says the log is a sequence of them:
\* The set of every possible log record. Each one has three fields.
Entry is defined as [from: Users, to: Users, rights: SUBSET Rights]Commit b
A teammate writes Entry with |-> where the colons are: [from |-> Users, to |-> Users, rights |-> SUBSET Rights]. What does TLC report?
- Nothing changes: the two forms mean the same thing.
- An error, as soon as the first record joins the log.
- A violation of TypeOK, with a trace.
Answer An error, as soon as the first record joins the log.
- If you picked A
- With |-> you get one record whose fields hold sets. With : you get the set of every record whose fields come from those sets. TypeOK needs the second.
- If you picked C
- A violation needs TLC to decide that a log entry is not in the set. With |->, there is no set of records to look in, only one record, and TLC cannot treat a record as a set to test membership in.
02Lab
./tla check chapters/05-just-enough-set-theory/specs/Permissions.cfg05-just-enough-set-theory/specs/Permissions05-just-enough-set-theory/specs/Permissions Permissions.cfg (Owner=owner, Guests={g1, g2}, MaxLog=2)
parse ok
model check: invariant WriteNeedsRead violated; trace has 2 states (1 steps); 4 distinct states
expected outcome met: violation WriteNeedsRead
trace (changed variables only; ./tla trace --full shows all):
1 Init access = (owner :> {"read", "write"} @@ g1 :> {} @@ g2 :> {}), log = <<>>
2 Share access: (owner :> {"read", "write"} @@ g1 :> {} @@ g2 :> {}) -> (owner :> {"read", "write"} @@ g1 :> {"write"} @@ g2 :> {}), log: <<>> -> <<[from |-> owner, to |-> g1, rights |-> {"write"}]>>Reveal for commit a
WriteNeedsRead is violated after a trace of 2 states: the owner shares write, alone, with a guest. The log's one record says so.
Reveal for commit b
An error. The Warm-up starts from exactly that line, and its first run stops with this verdict: error. TLC's message is "Attempted to check if element: ... is in the record: ...": it was asked whether a record is an element of another record.
03Reason
If you picked "no violation", you pictured the subsets of {"read", "write"} as "read" and "read and write". Write them all out: {}, {"read"}, {"write"}, and {"read", "write"}. SUBSET S contains every subset, including the empty one and each element on its own. Share rules out {} and nothing else, so {"write"} is a legal share:
- State 1 Initial state access = (owner :> {"read", "write"} @@ g1 :> {} @@ g2 :> {}) log = <<>>
- State 2 Share access (owner :> {"read", "write"} @@ g1 :> {} @@ g2 :> {}) becomes (owner :> {"read", "write"} @@ g1 :> {"write"} @@ g2 :> {}) log <<>> becomes <<[from |-> owner, to |-> g1, rights |-> {"write"}]>>
SUBSET is what makes the model honest. A real sharing dialog has a checkbox per right, and a user can tick any combination. \E R \in SUBSET Rights is that dialog: TLC tries every combination of ticks. The bug is not in SUBSET; it is that the requirement was never applied to the combinations.
SUBSET grows the way [S -> T] does in 05-02, and for the same reason. Picking a subset of Rights is deciding, for each right, in or out: one independent yes-or-no per element. So SUBSET S has 2 to the power |S| elements. Two rights give the four you wrote out; ten rights would give over a thousand ways to tick the boxes, on every single share. Keep the sets under SUBSET small, and say in a comment why that size is enough.
Notice the two ways to say "R is some of the rights". R \in SUBSET Rights reads "R is an element of the set of subsets"; R \subseteq Rights reads "R is a subset of Rights". They mean the same thing. R \in Rights means something else entirely: R is one right, a string, not a set of them. That one-symbol slip is easy to type, and a reviewer reading fast will not see it. Read every \in aloud and ask whether the thing on its left is one element or a whole set.
Write something before openingthe trace shows the owner sharing write with a guest. Could a guest have caused the first violation instead?
No. Guests start with no rights, and Share requires R \subseteq access[u], so the first share of all must come from the owner. TLC searches breadth-first, and with one worker, as ./tla runs it, the first violation it reports has a shortest trace. Every shortest violation is one share: the owner giving write alone to some guest. Which guest TLC names is its own tie-break.
Now read the log value. [from |-> owner, to |-> g1, rights |-> {"write"}] is a record. |-> is read "maps to", and r.rights is read "r dot rights". A record and its set have two different spellings for a reason:
And the log itself is a sequence, written << >> when empty. The standard module Sequences gives Append(s, e), Len(s) and Tail(s) (all but the first element); s[i] is the element at position i, counting from 1. A sequence is a function too: its domain is 1..Len(s). That is why log[i].rights works, one application after another. CONCEPTS lists the rest of the module.
Seq(S) being infinite matters to TLC. TypeOK may say log \in Seq(Entry), because TLC can test whether one given sequence is a member. But TLC cannot list the members, so Init cannot start from log \in Seq(Entry), and a log that only ever grows gives an infinite model. That is why Record keeps only the newest MaxLog entries:
\* Append e to the log, dropping the oldest record when the log is full.
Record(e) is defined as
IF Len(log) < MaxLog THEN Append(log, e) ELSE Append(Tail(log), e)Write something before openingis bounding the log a bug in the model?
It is an abstraction, and you must be able to say what it costs. The model keeps the two newest shares, so it can check claims about the recent log, but not a claim such as "every share ever made is still in the log". A real audit log that kept only two entries would be a bug; a model that keeps two to stay finite is a choice, and MaxLog's comment has to say why two is enough for the claims being checked.
Back to the bug. The fix is a filter: of all the subsets the dialog offers, allow only those that keep the rule. Set comprehension from 05-01 writes filters, for example the subsets of Rights that contain read:
{R \in SUBSET Rights : "read" \in R}The Core asks you for the right filter, which also has to look at what the guest already holds.
04In your editor
The spec is chapters/05-just-enough-set-theory/specs/Permissions.tla. Check this lesson's exercises with:
./tla check 05-03The violating run, with the record in the log:
05-just-enough-set-theory/specs/Permissions05-just-enough-set-theory/specs/Permissions Permissions.cfg (Owner=owner, Guests={g1, g2}, MaxLog=2)
parse ok
model check: invariant WriteNeedsRead violated; trace has 2 states (1 steps); 4 distinct states
expected outcome met: violation WriteNeedsRead
trace (changed variables only; ./tla trace --full shows all):
1 Init access = (owner :> {"read", "write"} @@ g1 :> {} @@ g2 :> {}), log = <<>>
2 Share access: (owner :> {"read", "write"} @@ g1 :> {} @@ g2 :> {}) -> (owner :> {"read", "write"} @@ g1 :> {"write"} @@ g2 :> {}), log: <<>> -> <<[from |-> owner, to |-> g1, rights |-> {"write"}]>>Before you pick a shape for new data, ask three questions. Does order matter? Then it is a sequence, like the log or a queue. Does every key need exactly one value? Then it is a function, like the seating chart. Is it a collection where only membership matters? Then it is a set, like a user's rights. A record is the right shape for one thing with a few named parts, such as one log entry. The 05-01 double booking came from a shape that allowed a state the box office cannot be in.
TLC may print a record's fields in a different order from the one you wrote. When a log entry looks wrong, read it field by field, and remember that rights holds a set: {"write"} and "write" are different values.
The \in versus \subseteq slip from REASON also turns up in AI output, written fluently and passing every check: ai/1-in-vs-subseteq (05-ai1) is the same mistake in an assistant's feature-flag model, with a confident message beside it. Try it after the Core.
05Practice
Warm-up. Write Entry as the set of every possible log record, so that TypeOK can say what the log holds.
Core. Change Share so that it refuses any combination that would leave the receiver able to write but not read. Four witnesses must still be reached: a guest who writes, a guest who only reads, write alone shared with someone who already reads, and both rights shared at once with a guest who has none.
Stretch. A boarding queue as a sequence, from a skeleton, including the cfg.
05-03-ex1Warm-up5 min
05-03-ex1 · A set of records
Tier: warm-up. Time: about 5 minutes.
TypeOK says the audit log is a sequence of entries: log \in Seq(Entry). The starter writes Entry with |->, which makes one record, not a set of them.
- Run
./tla check 05-03-ex1. TLC stops with an error the first time a share adds a record to the log. Your terminal shows only "Evaluating invariant TypeOK failed."; open the file on itsraw log:line and read the message there: what did TLC try to look inside? - Fill gap g1:
Entryis the set of every record with fieldsfrom,toandrights, wherefromandtoare users andrightsis a set of rights. - Run the check.
This exercise checks only TypeOK. The model still has the sharing bug from the lesson; the Core exercise deals with it.
Hints: HINTS.md, under "05-03-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/03-ex1-a-set-of-records/; 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-03-ex1 prints "goal met: pass".
./tla check 05-03-ex1
Repo chapters/05-just-enough-set-theory/exercises/03-ex1-a-set-of-records/
Hints
Hint 1
TLC's message says it tried to look inside a record as if it were a set. Seq(Entry) needs Entry to be a set.
Hint 2
[f |-> S] is one record. [f : S] is the set of all records whose field f holds an element of S.
Hint 3 (nearly the answer)
Keep the starter's field names and sets of values. Only the symbol between each field and its set changes: which of the two spellings builds a set of records?
05-03-ex2Core8 min
05-03-ex2 · Share safely
Tier: core. Time: about 8 minutes.
The sharing model from the lesson, with the product's rule WriteNeedsRead named in the cfg. Share lets a user pass on any nonempty set of rights they hold, and TLC finds a guest who can write but not read.
- Run
./tla check 05-03-ex2and read the record in the log. - Change
Share(gap g1) so that no share leavesvable to write but not read. Think about whatvholds after the share, not only aboutR. Delete the\* GAPmarker on its first line when you are done. - Run the check until it passes with all four witnesses: a guest who can write, a guest who can only read, a log entry where write alone went to someone who could already read, and a first share that gives a guest both rights at once.
Stopping the violation is easy: forbid sharing write, demand that every share of write carries read with it, or demand that the receiver already reads. Each refuses shares the product allows, and each fails a witness.
Hints: HINTS.md, under "05-03-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/03-ex2-share-safely/; 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-03-ex2 prints "goal met: pass".
./tla check 05-03-ex2
Repo chapters/05-just-enough-set-theory/exercises/03-ex2-share-safely/
Hints
Hint 1
Read the record in the log: which rights did the guest receive, and what did they hold before? The rule is about what v holds after the share. Then look at the last two witnesses: which shares must still be allowed?
Hint 2
After the share, v holds access[v] \cup R. The share is fine unless that set contains write and not read.
Hint 3 (nearly the answer)
Add one guard: if write is in R, then read must be in R or already in access[v]. Use =>, and \in on the union.
05-03-ex3Stretch40 min · mastery path
05-03-ex3 · Boarding queue
Tier: stretch. Time: about 40 minutes.
A blank page, almost: BoardingQueue.tla names the variables, the actions and two witnesses, and you write every body and the cfg.
The gate:
- Passengers join a single queue at the back. A passenger who is already queueing, or has already boarded, cannot join again. The queue holds at most
MaxQueuepeople. - The agent boards whoever is at the front: they leave the queue and are on board.
- Requirements: nobody appears in the queue twice (
NoDuplicates), and nobody who has boarded is still in the queue (BoardedLeftQueue). - Once every passenger has boarded, nothing more happens. Decide what the cfg should say about that, and say why in a comment.
Steps:
- Run
./tla check 05-03-ex3. Read the starter error: TLC says the right side of\inis not enumerable. Why can it testqueue \in Seq(Passengers)inTypeOKbut not start from it inInit? - Fill gaps g1 to g6 in
BoardingQueue.tla, then gap g1 inBoardingQueue.cfg: the spec,Passengers = {p1, p2, p3},MaxQueue = 2, and every invariant worth checking. - Run the check until it passes with full coverage and both witnesses.
- Break
NoDuplicateson purpose: remove one guard fromJoinand confirm TLC catches it. Then, from the repository root, run./tla mutate chapters/05-just-enough-set-theory/exercises/03-ex3-boarding-queue/BoardingQueue.tlaand read the survivors. A mutant that drops a whole action fromNextcannot break a safety invariant, so those survivors are expected; coverage and the witnesses catch a missing action.
{queue[i] : i \in 1..Len(queue)} is the set of passengers in the queue; Head(s) is the first element and Tail(s) all but the first. Two programming habits bite here: positions count from 1, so queue[0] is outside the domain 1..Len(queue), and Head(<< >>) has no defined value, so taking from the queue needs a guard that it is not empty.
The check also runs three course cfgs; leave the MC files as they are. MCBoardingQueue.cfg and MCBoardingQueue.sticky.cfg each run a gate with one bug, a double join and a Board that leaves the passenger queued: your NoDuplicates must fail on the first, your BoardedLeftQueue on the second. MCBoardingQueue.stuck.cfg checks that your Spec stops only once everyone has boarded, and boards from the front in joining order. Nothing checks which invariants your own cfg names, so name every one you wrote.
Hints: HINTS.md, under "05-03-ex3 hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/03-ex3-boarding-queue/; 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 four cfgs must meet their goals, each in its own block of output: "goal met: pass" for BoardingQueue.cfg and MCBoardingQueue.stuck.cfg, "goal met: violation NoDuplicates" for MCBoardingQueue.cfg and "goal met: violation BoardedLeftQueue" for MCBoardingQueue.sticky.cfg. Read each block's goal line, not only the last line.
Done when: ./tla check 05-03-ex3 prints "goal met: pass".
./tla check 05-03-ex3
Repo chapters/05-just-enough-set-theory/exercises/03-ex3-boarding-queue/
Hints
Hint 1
Init names a specific starting queue, not the set of all queues. Which queue does the gate open with? For the cfg, look at the witness comment in the module.
Hint 2
The passengers in the queue are {queue[i] : i \in 1..Len(queue)}. Join appends; Board uses Head and Tail. Both invariants quantify over the positions 1..Len(queue).
Hint 3 (nearly the answer)
Join(p) needs three guards (not queueing, not boarded, room) and leaves boarded unchanged. Board needs a nonempty queue. NoDuplicates compares two different positions; BoardedLeftQueue checks every position against boarded. The cfg names three invariants and turns deadlock checking off, with a comment saying why stopping is the design here and which stop the switch would also hide.
06Explain it
Open NOTES.md and answer the three prompts under 05-03, 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-03 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "My guard refuses a share when write is in R and read is in neither R nor what v already holds, so v never ends up with write alone. All four witnesses are reached, so the fix still allows sharing write, sharing read on its own, topping up a reader with write alone, and giving both rights at once to a guest who has none. It assumes rights only arrive through Share; an admin action that grants write directly would need the same check. TLC checked one owner, two guests, two rights and a log of the newest two shares."
A weak answer: "I stopped people sharing write." That fix would fail the GuestWrites witness, and the answer says nothing about what the model covers.
Done?