09-01
The prefix judge
00Recall
One card from 03-02 (why an invariant's trace stops at the bad state), one from chapter 01 (why a behavior never ends), and one from 03-05 (what CHECK_DEADLOCK FALSE changes). Hold the first two side by side today: a trace that stops, and a behavior that does not.
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.
01-03-a from Behaviors are infinite
A print job reaches done, and nothing more can happen to it. What is the next state of its behavior?
Show answer
Done again, and again forever. Every behavior is infinite, so a system that stops still has a next state: the same one. The step from done to done changes nothing and is called a stuttering step.
03-05-b from TypeOK, and the check you didn't configure
What does CHECK_DEADLOCK FALSE change about the system?
Show answer
Nothing. It turns off TLC's deadlock report; any stuck state is still there.
Skip recall Recall is never required.
01Commit
Here is a log from a server, one line per state. A request arrives in the first state. Five more states follow, and none of them contains a response.
state 1: request received
state 2: worker busy
state 3: worker busy
state 4: cache refreshed
state 5: worker busy
state 6: worker busyCommit a
A request arrives, then five more states pass with no response. Can a finite log like this show that the server will never respond to the request?
- No. No finite log can show "never responds".
- Yes. Five quiet states after a request is long enough to call it.
- Yes, if the log shows the worker idle in every one of those states.
Answer No. No finite log can show "never responds".
- If you picked B
- "Long enough" is a timeout, and the claim never mentioned one. Whatever number you pick, the claim was about never, not about that number.
- If you picked C
- Idle in those states says nothing about the states after them, and "never" is a claim about all of those. When TLC refutes a claim like this, it prints something that is not a finite log at all. That is the LAB.
The teammate who owns the crossing at the end of the street sent three requirements for its traffic light.
Commit b
Which of these requirements can never be refuted by any finite stretch of the light's behavior?
- The light is never green both ways at once.
- A waiting direction gets a green within three changes of the light.
- East-west gets a green light again and again, forever.
Answer East-west gets a green light again and again, forever.
- If you picked A
- A crash between two green lights happens in one particular state. Show the states up to it and the requirement is refuted, whatever comes after. That is the signature of a safety property.
- If you picked B
- The deadline makes it refutable: three changes without a green is a finite stretch you can point at. A requirement with a bound is a safety property, however much it sounds like "eventually".
02Lab
specs/StuckLight.tla is a teammate's copy of specs/TrafficLight.tla whose behavior differs in one line. Run its safety cfg, then its liveness cfg:
./tla check chapters/09-eventually/specs/StuckLight.cfg
./tla check chapters/09-eventually/specs/StuckLight.live.cfgThe second run:
09-eventually/specs/StuckLight.live09-eventually/specs/StuckLight.live StuckLight.live.cfg
parse ok
model check: temporal property EWKeepsGettingGreen violated; lasso of 3 states, looping back to state 1; 3 distinct states
expected outcome met: property EWKeepsGettingGreen
trace (changed variables only; ./tla trace --full shows all):
1 Init light = [NS |-> "red", EW |-> "red"], turn = "NS"
2 Go light: [NS |-> "red", EW |-> "red"] -> [NS |-> "green", EW |-> "red"]
3 Slow light: [NS |-> "green", EW |-> "red"] -> [NS |-> "yellow", EW |-> "red"]
Stop("NS") back to state 1EWKeepsGettingGreen violated · loop
This is a teaching view. Real checking happens with TLC on your machine.
Trace
- State 1 Initial state light = [NS |-> "red", EW |-> "red"] turn = "NS"
- State 2 Go("NS") light [NS |-> "red", EW |-> "red"] becomes [NS |-> "green", EW |-> "red"]
- State 3 Slow("NS") light [NS |-> "green", EW |-> "red"] becomes [NS |-> "yellow", EW |-> "red"]
- Loop back to state 1
Text view: every state and step
| State | light | turn | Steps (action → state) |
|---|---|---|---|
| initial 1 | [NS |-> "red", EW |-> "red"] | "NS" | Go("NS") → 2 |
| 2 | [NS |-> "green", EW |-> "red"] | "NS" | Slow("NS") → 3 |
| 3 | [NS |-> "yellow", EW |-> "red"] | "NS" | Stop("NS") → 1 |
Read the last line of the trace twice. It does not name a new state. It points back.
Reveal for commit a
No. The log is a finite prefix of a behavior that has no end, and any finite prefix can be continued with a response in the very next state. A finite log can refute "the server never responds" (show one response). It can never refute "the server eventually responds". TLC is in the same position, which is why the stuck light's counterexample ends in back to state 1 instead of a last state.
Reveal for commit b
East-west "again and again, forever". No finite stretch can refute it, because east-west's next green might come one step after the stretch ends. The other two are safety properties. Each one is refuted by a finite stretch you can point at: the state with two greens, or three changes with no green. The deadline is what makes the second one safety.
03Reason
If you picked "five quiet states is long enough", you did what every on-call engineer does at 3 a.m.: you replaced "never" with a timeout. Timeouts are useful. They are also a different requirement.
Here is a way to make the difference sharp. Imagine a judge who is shown only the first k states of a behavior, any k you like, and must decide whether the behavior breaks a requirement. Call it the prefix judge.
Write something before openingthe requirement is "never green both ways". When can the prefix judge convict?
As soon as the prefix contains a state with two greens. Nothing after that state can undo it: the crash already happened. And if the behavior really does break the requirement, there is such a state at some finite position, so some prefix is long enough to convict.
That is a safety property: every behavior that breaks it has a finite prefix that already breaks it, whatever comes next. "Something bad happens" is something you can point at. Every invariant you wrote in chapters 03 to 08 is a safety property, and that is why TLC's traces for them stop: 03-02 showed that the trace ends at the first bad state, because the prefix up to it is all the evidence there is.
Write something before openingthe requirement is "east-west gets a green, again and again". When can the prefix judge convict?
Never. Take any prefix, as long as you like, with no east-west green in it. It might be the opening of a behavior in which east-west goes green in the very next state. The judge cannot rule that out, so the judge cannot convict.
That is a liveness property: every finite prefix can be continued into a behavior that satisfies it. "Something good eventually happens" can always still happen. The two definitions are precise, and they split cleanly:
Alpern and Schneider showed in 1985 that every property is the intersection of one safety property and one liveness property; the paper is under "Read more" in CONCEPTS.md. "Every request gets exactly one answer" is "no request ever gets a second answer" (safety) and "every request gets an answer eventually" (liveness).
Now look at the stuck light again, with the prefix judge's eyes. Its trace has 3 states. The working light, specs/TrafficLight.tla, has exactly one behavior apart from stuttering steps, because in every state exactly one real step is possible, and an assumption in its Spec (see the callout below) rules out freezing. (Stuttering steps repeat a state, and by the frame-rate test of 04-04 no property in this chapter can tell them apart.) Here is its start, up to east-west's first green, produced by breaking it on purpose (03-03):
- State 1 Initial state light = [NS |-> "red", EW |-> "red"] turn = "NS"
- State 2 Go light [NS |-> "red", EW |-> "red"] becomes [NS |-> "green", EW |-> "red"]
- State 3 Slow light [NS |-> "green", EW |-> "red"] becomes [NS |-> "yellow", EW |-> "red"]
- State 4 Stop light [NS |-> "yellow", EW |-> "red"] becomes [NS |-> "red", EW |-> "red"] turn "NS" becomes "EW"
- State 5 Go light [NS |-> "red", EW |-> "red"] becomes [NS |-> "red", EW |-> "green"]
Its first three states are the stuck light's three states. No finite prefix of the stuck light's behavior convicts it: continue any prefix with a light that hands over the turn, and the result gives east-west a green again and again. Only the whole infinite behavior refutes it, so TLC prints the whole behavior in the only form that fits on a page: a finite path, then a step back to an earlier state. Go round the loop forever and east-west is red forever. Lesson 09-03 is about that shape.
One more consequence of the definitions. "A waiting direction gets a green within three changes" sounds like liveness, but it has a deadline, and a deadline turns "eventually" into something you can point at: three changes with no green. Bounded response is safety. Unbounded response is liveness. When a design document says "promptly", ask which one it means, because only one of them can be checked as an invariant, once the spec counts the changes since the request.
04In your editor
The two lights are chapters/09-eventually/specs/TrafficLight.tla and chapters/09-eventually/specs/StuckLight.tla. Check this lesson's exercises with:
./tla check 09-01The stuck light's safety run, which passes:
09-eventually/specs/StuckLight09-eventually/specs/StuckLight StuckLight.cfg
parse ok
model check: no violation found in 3 distinct states (4 generated, depth 3)
expected outcome met: pass
coverage: all 3 actions fired
witness NSGreen reached (trace of 2 states)05Practice
Warm-up. Fix the stuck light, so that its liveness cfg passes as well as its safety cfg.
Core. A help desk manager's requirement mixes a safety half and a liveness half. The liveness half is written for you; write the safety half, then the deadline a product owner offers in place of "sooner or later". Then let TLC show you which desks keep which promise.
Before you write the Core, decide for each of the three formulas: if it were broken, could you show the breakage with a finite trace?
09-01-ex1Warm-up5 min
09-01-ex1 · Hand over the turn
Tier: warm-up. Time: about 5 minutes.
This is the teammate's traffic light from lesson 09-01. TrafficLight.cfg checks the safety properties, including GreenOnlyFromAllRed about steps, and passes. TrafficLight.live.cfg checks that each direction gets a green light again and again, and fails.
- Run
./tla check 09-01-ex1. Read the liveness trace from its last line backwards: which state does the loop go back to, and which direction never appears green in it? - Read
Stopin gap g1. The comment says what it should do. What does it do? - Fix the one placeholder line, so that a direction that stops hands the turn to the other one.
- Run the check again. Both cfgs should pass, with every action firing and the witnesses each cfg names reached.
Before you fix it, write down why the safety cfg passed with the bug in. Which kind of property is "never green both ways", and which kind is "east-west gets a green again and again"?
Both cfgs must meet the same goal, "goal met: pass": TrafficLight.cfg and TrafficLight.live.cfg. 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.
Hints: HINTS.md, under "09-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/01-ex1-hand-over-the-turn/; open it after you have a passing answer of your own.
Done when: ./tla check 09-01-ex1 prints "goal met: pass".
./tla check 09-01-ex1
Repo chapters/09-eventually/exercises/01-ex1-hand-over-the-turn/
Hints
Hint 1
The loop goes back to the all-red state with the turn still at north-south. Which variable decides who goes green next, and which action should change it?
Hint 2
Stop(d) is the only action that runs when a direction gives up the crossing. Right now it leaves turn where it was.
Hint 3 (nearly the answer)
The placeholder line is about turn'. Which direction should be allowed to go green next, and is there an operator in the module that names it?
09-01-ex2Core8 min
09-01-ex2 · Split the requirement
Tier: core. Time: about 8 minutes.
HelpDesk.tla models a customer asking up to Max questions and a desk answering them. The desk manager's requirement is one sentence: "every question gets an answer, and nobody gets an answer to a question they did not ask." It has a safety half and a liveness half. The liveness half, EveryQuestionAnswered, is written for you, with an operator lesson 09-02 takes apart; this exercise is about which half is which, not about how to write liveness.
A product owner wants something TLC can check as an invariant, and offers a deadline instead of "sooner or later": "every question is answered before the customer asks the next one."
- Run
./tla check 09-01-ex2. Read the block forMCHelpDesk.silent.cfg: which half fails, and what does the desk do in its last state? - Fill gap g1,
NoAnswerUnasked, the safety half: a state predicate overaskedandanswered. - Fill gap g2,
AnsweredBeforeNext, the product owner's deadline. Ask the prefix judge's question first: if it were broken, could you point at one state? Then write it as a state predicate. - Run the check. Six cfgs run.
HelpDesk.cfgchecks the safety half on the real desk andHelpDesk.live.cfgthe liveness half; both must pass.MCHelpDesk.echo.cfgruns a desk that answers the first question twice; your safety half must fail there.HelpDesk.deadline.cfgchecks the deadline on the real desk, andMCHelpDesk.impatient.cfgon a desk of three questions whose customer waits for the first answer only and then asks freely; your deadline must fail on both.MCHelpDesk.silent.cfgruns a desk whose customer waits for each answer before asking again, and which may stop answering; both of your formulas must hold there, so that only the liveness half fails.
Before that second run, predict the verdict of HelpDesk.deadline.cfg. The real desk meets the manager's requirement. Does it meet the product owner's? And does the silent desk, which meets the deadline, meet the manager's?
MCHelpDesk.tla is the course's check. Leave it as it is.
All six cfgs must meet their goals: "goal met: pass" for HelpDesk.cfg and HelpDesk.live.cfg, "goal met: violation AnsweredBeforeNext" for HelpDesk.deadline.cfg and MCHelpDesk.impatient.cfg, "goal met: violation NoAnswerUnasked" for MCHelpDesk.echo.cfg, and "goal met: property EveryQuestionAnswered" for MCHelpDesk.silent.cfg. 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.
Hints: HINTS.md, under "09-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/01-ex2-split-the-requirement/; open it after you have a passing answer of your own.
Done when: ./tla check 09-01-ex2 prints "goal met: violation AnsweredBeforeNext".
./tla check 09-01-ex2
Repo chapters/09-eventually/exercises/01-ex2-split-the-requirement/
Hints
Hint 1
Ask of each formula: if it were broken, could you point at one state? "An answer nobody asked for" is visible in a single state, as a relation between the two counts. So is "a question still open when the next one is asked": a state with too many questions waiting. "Every question gets an answer" is not visible in any one state, which is why it is written for you and checked under PROPERTY, in HelpDesk.live.cfg.
Hint 2
Both of your formulas compare answered with asked. The safety half says which count may never be ahead. The deadline says how far behind the answers may fall: when the customer asks question k + 1, question k already has its answer.
Hint 3 (nearly the answer)
Safety: one comparison between the two variables, the right way round. Deadline: a bound on asked - answered. If the echo desk passes, your safety half allows a second answer to one question. If HelpDesk.deadline.cfg passes, your bound lets two questions wait at once. If MCHelpDesk.impatient.cfg passes, your deadline covers only one question, the first or the last. If the silent desk reports an invariant instead of the liveness half, one of your formulas forbids a state in which one question is waiting, which the deadline allows.
06Explain it
Open NOTES.md and answer the three prompts under 09-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 "09-01 check your explanation" in HINTS.md. Then, under the optional "A system at my job with the same shape", write one requirement from a system you work on that has a deadline, and one that says only "eventually". Which could a finite log refute?
After you writeCheck your explanation
A good answer: "NoAnswerUnasked says answers never outnumber questions; it is safety, because a desk that breaks it does so in one visible state, as the echo desk does with its second answer. AnsweredBeforeNext is safety too: the deadline makes a violation finite, two questions waiting at once, and TLC shows the real desk reaching that state in a short trace. EveryQuestionAnswered is liveness, because no finite stretch can show that an answer will never come. The two requirements are different, not stronger and weaker: the real desk meets the manager's and misses the deadline, and the silent desk meets the deadline, because its customer never asks again, and leaves the question unanswered for good. Only the liveness half depends on the fairness conjunct in Spec. TLC checked desks of two and three questions; it says nothing about how long anyone waits in time."
A weak answer: "I wrote the two formulas and all six cfgs met their goals." It does not say which formula is which kind, why, or what the silent desk shows about a deadline counted in questions.
Done?