12-01
00Recall
One card from 03-04 on generated versus distinct states, one from chapter 09 on a liveness trace that ends in stuttering, and one from chapter 10 on what weak fairness takes away from a hostile scheduler. This lesson opens TLC up and shows where each of those three comes from.
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.
09-04-b from Doing nothing is legal
A liveness check's summary line ends "then stuttering forever". What do you look at first, and what can it mean?
Show answer
Whether a real step is possible in the last state. If one is, the spec is missing an assumption, such as WF_vars(Next). If none is, the system stopped for good: stuck in a deadlock, or at an end the spec allows before the property was met.
10-01-b from Be the scheduler
What does WF_vars(Next) take away from a hostile scheduler, and what does it leave?
Show answer
It takes away stopping while some step is possible. It leaves favouritism: the scheduler can keep choosing other processes' steps for ever.
Skip recall Recall is never required.
01Commit
This chapter's running example is two-phase commit, specs/TwoPhaseCommit.tla. One coordinator, a set of participants, one transaction. Each participant votes yes or gives up; the coordinator commits only with a yes from everyone, and may abort at any time before that. The requirement Consistent says no participant commits while another aborts. The liveness property Termination says every participant eventually learns the outcome.
The folder has two cfgs for the same model with three participants. TwoPhaseCommit.n3.cfg checks TypeOK and Consistent against Spec. TwoPhaseCommit.live.cfg checks Termination against FairSpec, which is Spec plus weak fairness on Next.
Commit a
The safety cfg and the liveness cfg of TwoPhaseCommit use the same three participants. Compared with the safety run, how many distinct states will the liveness run report?
- The same number.
- More: a liveness check also has to store the loops it searches.
- Fewer: fairness rules out the behaviors that stall, so their states go too.
Answer The same number.
- If you picked B
- It does search loops, and it does use more memory for that. But "distinct states" counts the states of the system, and the two runs reach the same ones. The extra work is done on top of the same graph.
- If you picked C
- Fairness never removes a state. It rules out infinite behaviors that stall forever, and every state on them is still reachable by some fair behavior. Chapter 10 met this with FairLock.
Commit b
TLC does not store each state it has seen. It stores a fingerprint, a 64-bit hash of the state. What can go wrong because of that?
- Nothing: a hash of the whole state identifies it exactly, so two different states never share one.
- Two different states may share a fingerprint; TLC then never explores one of them and may miss a violation.
- Two different states may share a fingerprint; TLC may then report a violation in a state no behavior reaches.
Answer Two different states may share a fingerprint; TLC then never explores one of them and may miss a violation.
- If you picked A
- Many different states map to each hash value, so two reachable states can collide; the odds grow with the number of states found and the number generated. For the models in this course they are tiny but never zero, so TLC prints an estimate rather than ignoring them.
- If you picked C
- A collision makes TLC believe it has already seen a state it has not. The effect is a state it never explores, so the risk runs the other way: a violation missed, never one invented.
02Lab
./tla check chapters/12-state-space-and-evidence/specs/TwoPhaseCommit.n3.cfg
./tla check chapters/12-state-space-and-evidence/specs/TwoPhaseCommit.live.cfg12-state-space-and-evidence/specs/TwoPhaseCommit.n312-state-space-and-evidence/specs/TwoPhaseCommit.n3 TwoPhaseCommit.n3.cfg (Participants={p1, p2, p3})
parse ok
model check: no violation found in 99 distinct states (297 generated, depth 8)
expected outcome met: pass
coverage: all 7 actions fired
witness AllCommitted reached (trace of 8 states)12-state-space-and-evidence/specs/TwoPhaseCommit.live12-state-space-and-evidence/specs/TwoPhaseCommit.live TwoPhaseCommit.live.cfg (Participants={p1, p2, p3})
parse ok
model check: no violation found in 99 distinct states (297 generated, depth 8)
expected outcome met: pass
coverage: all 7 actions fired
witness AllCommitted reached (trace of 8 states)Reveal for commit a
The same: 99 distinct states in the safety run and 99 in the liveness run, with the same generated count and the same depth. Both runs build the same state graph. The liveness run then does a second job on that graph, and the summary line does not count it.
Reveal for commit b
Two states can collide. TLC keeps one fingerprint per distinct state; if a new state's fingerprint is already in the set, TLC treats the state as seen and never explores it. When a breadth-first run finishes without finding an error, the end of its raw log estimates the probability that this happened. A run that stops at a violation, and a simulation run, print no estimate. For this course's models the estimate is tiny, but it is never zero, and a good claim says the search was "fingerprint-based".
03Reason
If you picked "more", you had the right instinct about cost and the wrong idea about where the cost lands. Let us build TLC from its parts.
TLC has two data structures. The first is a set of fingerprints: one short, fixed-size hash for each distinct state it has found. The second is a queue of states it has found but not yet explored. It starts by computing every initial state, checking the invariants in each, and putting them on the queue. Then it repeats one move until the queue is empty: take the state at the front, compute every successor allowed by Next, and for each successor, compute its fingerprint and look it up in the set. If it is new, TLC adds it to the set, checks the invariants in the state, and puts the state at the back of the queue. A known one is dropped unchecked. Remember that order; 12-03 depends on it.
That loop is breadth-first search. Every state one step from the start is explored before any state two steps away.
Write something before openingwhy does breadth-first search with one worker always print a shortest counterexample to an invariant?
Because the states come off the queue in order of distance from the start. The first state that breaks an invariant is found while TLC is expanding the level immediately before it, so no state that breaks it can be closer. TLC also records, for each new state, the fingerprint of the state it was found from, so it can walk back to the start and rebuild the trace by re-running the steps. With several workers the levels overlap, and the course runs one worker for exactly this reason.
Now the counts make sense. "States generated" is every successor TLC computed, the known ones included. "Distinct states" is the size of the fingerprint set. "Depth" is the number of levels. A fingerprint is much smaller than a state, so TLC can remember millions; the queue holds whole states, and TLC moves it to disk when it grows.
An invariant fits this loop perfectly: it is a claim about one state, so TLC checks it the moment it computes the state, and stops at the first failure.
Termination does not fit. It is a claim about infinite behaviors, and chapter 09 showed what refutes one: a lasso, a path that ends in a loop of states in which the property never comes true. You cannot see a loop from one state. TLC needs the graph's edges as well as its nodes.
Write something before openingwhy can't the liveness check stop at the first bad state, the way the invariant check does?
There is no bad state. Every state on a stuttering lasso of the starter in 12-01-ex1 is a perfectly good state; what is wrong is staying there for ever. Whether some fair behavior stays for ever depends on which states loop back to which, and on which actions are enabled around the loop. That is a property of the whole graph.
So a liveness check has two phases. The first is the same breadth-first search, building the same graph, which is why the numbers in the summary line agree; this time TLC also records the edges. Then it combines the graph with, when the property needs one, a small automaton for the negated property, called a tableau, and searches the combination for a fair loop that never satisfies the property. The summary line leaves that second phase out. Your raw log shows where it starts: a line beginning "Checking temporal properties for the complete state space".
Write something before openingif the numbers are the same, what did the liveness check pay for?
Memory for the edges, and time for the second phase, which grows with the graph times the tableau. On a model with millions of states, a liveness check can take hours, or run out of memory, where the safety check of the same model finishes. The summary counts states; the cost of liveness is in what TLC does with them afterwards.
That cost is the practical reason for the rule you have followed since chapter 09: safety and liveness go in separate cfgs. The safety cfg can be fast. It can use the reductions of 12-02 and 12-03, and it reports a broken invariant without waiting for a loop search. The liveness cfg is slower, uses fewer reductions, and carries the fairness the property needs. Fairness belongs there only: in the safety cfg it changes no invariant result, only the reader's idea of what was assumed.
The worked example is the pair you ran. TLC found no violation of TypeOK or Consistent in the model with three participants, with every action firing and AllCommitted reached; and it found no violation of Termination in the same model under weak fairness on Next.
04In your editor
Check this lesson's exercises with:
./tla check 12-01Then open the raw log of the liveness run and find the line that starts its second phase.
05Practice
Warm-up. The liveness cfg of TwoPhaseCommit fails, because the spec it checks has no fairness. Add the fairness, and compare the safety run's numbers with the liveness run's.
Core. The task board needs a liveness property: every task in progress is eventually finished or dropped. You write the property and pick the specification its cfg checks, and the course's grader checks that your property fails without fairness.
12-01-ex1Warm-up5 min
12-01-ex1 · Fair enough
Tier: warm-up. Time: about 5 minutes.
Two of the folder's cfgs check the same model of TwoPhaseCommit with three participants: TwoPhaseCommit.safety.cfg checks invariants, and TwoPhaseCommit.live.cfg checks Termination, "every participant eventually learns the outcome".
- Before you run anything, write two predictions in
NOTES.md: will the two runs report the same number of distinct states, and what will the liveness run say? - Run
./tla check 12-01-ex1. Read the liveness run's trace. It ends in a loop: which step repeats, and is anything in the spec forbidding it? - Fill gap g1 in
TwoPhaseCommit.tla:FairSpecisSpecplus the assumption that a step that stays possible eventually happens. - Run the check again. Compare both runs' distinct and generated counts with your prediction.
Only FairSpec changes. Leave Spec alone: the safety check uses it, and invariants need no fairness.
Hints: HINTS.md, under "12-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/12-state-space-and-evidence/exercises/01-ex1-fair-enough/; 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.
The safety cfg has no goal and keeps printing "expected outcome met: pass". The liveness cfg is the one that changes. Two more cfgs are the course's checks of your FairSpec. TwoPhaseCommit.grader.cfg asks whether the coordinator eventually decides, which a FairSpec that assumes <>AllDecided fails. TwoPhaseCommit.abort.cfg expects EventuallyCommits to fail, because a fair transaction may still abort; a FairSpec that forces a commit, or allows no behavior at all, makes it hold.
The liveness and grader cfgs must each print the line below, and the abort cfg "goal met: property EventuallyCommits".
Done when: ./tla check 12-01-ex1 prints "goal met: pass".
./tla check 12-01-ex1
Repo chapters/12-state-space-and-evidence/exercises/01-ex1-fair-enough/
Hints
Hint 1
Read the lasso's last line. The trace ends in a state where some step is still possible, and then nothing happens for ever. What in Spec says that a possible step must eventually happen?
Hint 2
Nothing does: Init /\ [][Next]_vars allows stopping anywhere. Chapter 10's weak fairness, WF_vars(A), says that an action that stays enabled eventually takes a step.
Hint 3 (nearly the answer)
FairSpec is Spec conjoined with weak fairness on the whole next-state relation, with vars as the subscript. One conjunct, written with WF_.
12-01-ex2Core8 min
12-01-ex2 · No task left behind
Tier: core. Time: about 8 minutes.
The team's board, with three workers and three tasks. The safety check, MCTaskBoard.cfg, already passes with symmetry. You write the liveness half: the property, and the specification its cfg checks.
- Run
./tla check 12-01-ex2. Three cfgs run. The liveness cfg and the course'sTaskBoard.grader.cfgboth pass, becauseNoTaskStuckis[]TRUEfor now, which nothing can violate. - Fill gap g1 in
TaskBoard.tla: every task that is in progress eventually stops being in progress.P ~> Qis read "P leads to Q": wheneverPholds,Qholds then or later. - Run again with
SPECIFICATION Spec, and read the lasso. Then fill gap g1 inTaskBoard.live.cfgwith the specification that rules that lasso out.TaskBoard.tlaalready defines it. - Run again. Your property must pass in
TaskBoard.live.cfgand fail inTaskBoard.grader.cfg, which checks it without fairness. Leave the grader cfg as it is.
Do not add SYMMETRY to the liveness cfg. The safety cfg may keep it; 12-03 shows why the liveness one may not.
Hints: HINTS.md, under "12-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/12-state-space-and-evidence/exercises/01-ex2-no-task-left-behind/.
All three cfgs must meet their outcomes: "expected outcome met: pass" for MCTaskBoard.cfg, which has no goal, "goal met: property NoTaskStuck" for TaskBoard.grader.cfg, and "goal met: pass" for TaskBoard.live.cfg.
Done when: ./tla check 12-01-ex2 prints "goal met: pass".
./tla check 12-01-ex2
Repo chapters/12-state-space-and-evidence/exercises/01-ex2-no-task-left-behind/
Hints
Hint 1
P ~> Q holds when every state satisfying P is followed, then or later, by one satisfying Q. Say it for one task, then for all of them.
Hint 2
The property quantifies over Tasks: "a task in progress" leads to "that task not in progress". Against Spec, the lasso stutters while a worker holds a task. TaskBoard.tla already defines a specification that rules that out.
Hint 3 (nearly the answer)
Write \A t \in Tasks : followed by a leads-to between two statements about status[t], one with = and one with #, both mentioning "doing". In the liveness cfg, point SPECIFICATION at the spec with fairness on Finish.
06Explain it
Open NOTES.md and answer the three prompts under 12-01, 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 "12-01 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "NoTaskStuck says every task in progress later leaves that state, and the live cfg checks it against FairSpec, which has weak fairness on each Finish(w, t). It relies on workers who hold a task eventually acting; a worker who can sit on a task for ever breaks it, which is why the grader, without fairness, fails. TLC checked one board of three workers and three tasks, without symmetry, and did not check deadlines, priorities or anything about how long a task takes."
A weak answer: "I added ~> and FairSpec and both cfgs did what they should." It describes the edit, not why it is right, and names no assumption.
Done?