04-02
Chapter 04 · Stuttering and the shape of a spec
Stuttering steps
00Recall
One card from 04-01 (unpacking [Next]_vars), one from chapter 02 (what an action says about a variable it never mentions), and one from chapter 01 (why every behavior is infinite). Keep the chapter 02 card in mind. It is the whole of today's lesson, seen from the other side.
04-01-a from The shape of every spec
Unpack [Next]_vars, and say which steps it allows.
Show answer
Next \/ (vars' = vars): every Next step, plus every step that leaves all the variables in vars unchanged (a stuttering step).
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.
01-03-b from Behaviors are infinite
Why does TLA+ make every behavior infinite, even for a system that finishes?
Show answer
So that there is one shape for everything. Stopping is a final state repeated forever, and claims about "always" and "eventually" need no special case for the end.
Skip recall Recall is never required.
01Commit
Two clocks, one question: is this a valid hour clock?
The first is specs/GluedClock.tla. Someone glued its hour hand at 12. It has the same variable hr and the same Init as HourClock, and its only action is
Stay is defined as
and UNCHANGED hrTo ask the question, the module brings in HourClock's spec under a prefix, and names it:
Hour is defined as INSTANCE HourClock
HourSpec is defined as Hour!SpecHour!Spec is HourClock's Spec, with its hr read as this module's hr. The cfg lists PROPERTY HourSpec, which asks TLC: does every behavior of the glued clock satisfy the hour clock's spec?
Commit a
TLC checks PROPERTY HourSpec on GluedClock. What does it report?
- A violation: an hour clock must move on, and this hand never does.
- A deadlock: the hand can never move, so nothing can happen next.
- No violation.
Answer No violation.
- If you picked A
- Look for the word "must" in HourClock.tla. Init says where the hand starts. The box says which steps are allowed. Neither says that any step has to happen.
- If you picked B
- TLC reports a deadlock when a state has no Next step at all. Stay is a Next step: it leads from the state back to itself. And deadlock is TLC's own check on Next, not a verdict on HourSpec.
The second is the chapter's main spec, specs/HourMinuteClock.tla. It has hr and min. Its TickMinute moves the minute hand and leaves hr alone; its TickHour fires at minute 59, sets min back to 0 and moves the hour hand on. The cfg HourMinuteClock.hourspec.cfg checks the same PROPERTY HourSpec.
Commit b
TLC checks PROPERTY HourSpec on HourMinuteClock. What does it report?
- No violation.
- A violation on the first minute tick. A stuttering step of the hour clock changes nothing, and that step changes min.
- An error: HourClock has one variable and this module has two, so the specs cannot be compared.
Answer No violation.
- If you picked B
- "Changes nothing" was too big a promise. Unpack HourClock's box and ask which variables it constrains.
- If you picked C
- INSTANCE matches HourClock's variable hr with this module's hr, by name. HourSpec is then an ordinary formula about this module's behaviors, and min is a variable it never mentions.
02Lab
./tla check chapters/04-stuttering/specs/GluedClock.cfg
./tla check chapters/04-stuttering/specs/HourMinuteClock.hourspec.cfg04-stuttering/specs/HourMinuteClock.hourspec04-stuttering/specs/HourMinuteClock.hourspec HourMinuteClock.hourspec.cfg
parse ok
model check: no violation found in 720 distinct states (721 generated, depth 720)
expected outcome met: pass
coverage: all 2 actions fired
witness AlmostNoon reached (trace of 720 states)Reveal for commit a
No violation, in 1 distinct state. A glued hand is a valid hour clock. Its only behavior starts at 12 and stutters for ever, and HourClock's spec allows exactly that: the first state satisfies Init, and every step leaves hr unchanged. The cfg names no witness, and its header says why: this model reaches nothing, on purpose.
Reveal for commit b
No violation, in 720 distinct states, with both actions firing. Every TickHour step is an Advance step of the hour clock. Every TickMinute step leaves hr where it was, so to the hour clock it is a stuttering step, even though min changed.
03Reason
If you expected a violation on the first minute tick, you gave stuttering a bigger meaning than it has. "A step that changes nothing" sounds as if the whole world freezes for a moment. Let us see what the formula says.
HourClock's box is [Advance]_vars with vars == << hr >>. Unpacked:
Advance \/ (<< hr >>' = << hr >>)The second disjunct mentions one variable, hr. Recall the card from chapter 02: an action says nothing about a variable it does not mention. In the logic, such a variable may take any value at all. So the stuttering disjunct of the hour clock allows any step in which hr keeps its value, and says nothing about min, or the temperature in the room, or anything else in the universe.
Write something before openinga minute tick takes min from 7 to 8 and leaves hr at 3. Which disjunct of the hour clock's box allows it?
The second. hr is the same before and after, so << hr >>' = << hr >> is true of the step. Advance is false of it: it wants hr' = 4.
So "stuttering" is always relative to a subscript. A stuttering step of [Next]_v is a step that leaves the variables in v unchanged. Variables outside v may do anything. A minute tick is a stuttering step of the hour clock. It is not a stuttering step of the minute clock, whose subscript is << hr, min >>.
Why would anyone design a logic this way? Picture the alternative. If a stuttering step had to freeze every variable in the universe, the hour clock's spec would be a claim about the whole world: nothing else may ever change between two hours. No real system satisfies that, because something else always changes. Every spec would have to list every variable of every other part of the system, and you could never write a spec of one part and later put it inside a bigger one. Stuttering relative to a subscript lets a spec talk about its own variables and leave everyone else alone, and that is what lets specs compose.
The glued clock shows the cost. A formula of this shape says which steps are allowed. It never says that a step must happen. A clock that never moves, a server that never answers, a queue that never delivers: each one satisfies a spec of this shape, as long as it starts right and does nothing wrong. Everything in this chapter is a safety claim, "nothing bad happens", and after a correct start, doing nothing is always safe. That is not a flaw in the hour clock. It is a choice about what the spec promises, and later chapters add the other kind of promise on purpose, in a separate formula, so you can see exactly which assumption buys it.
Write something before openingthen how will you ever say "the clock keeps going"?
With a different kind of formula, one that talks about the whole infinite behavior: "eventually the hand moves", and fairness, which says that an action that stays possible, or keeps becoming possible, must eventually take a step that changes something. That is Part III, chapters 09 and 10. A spec's box will never say it.
How did TLC check HourSpec at all? Look at INSTANCE once more. Hour == INSTANCE HourClock reads HourClock.tla and makes its definitions available under the prefix Hour!. Its variable hr is matched with the variable of the same name in this module. Hour!Spec is then a formula about this module's behaviors. PROPERTY HourSpec asks TLC to check that every behavior of Spec satisfies it. In logic, that is the implication Spec => HourSpec (read "Spec implies HourSpec"). Chapter 11 gives this a name, refinement, and lets the two specs have different variables. Here they share one, and that is all you need.
One more consequence before you practise. When you write an action, the stuttering steps of the whole spec are already there. Writing Wait == UNCHANGED side in a ferry spec does not add "the ferry may wait": [Next]_vars allowed waiting already. Worse, it says nothing about the ferry's other variable, so TLC cannot tell what happens to it. That is the Warm-up.
04In your editor
The specs are chapters/04-stuttering/specs/GluedClock.tla and HourMinuteClock.tla. Check this lesson's exercises with:
./tla check 04-02The glued clock, checked against the hour clock's spec:
04-stuttering/specs/GluedClock04-stuttering/specs/GluedClock GluedClock.cfg
parse ok
model check: no violation found in 1 distinct states (2 generated, depth 1)
expected outcome met: pass
coverage: all 1 actions firedThen check the minute clock against nothing but its own spec, ./tla check chapters/04-stuttering/specs/HourMinuteClock.cfg, and compare it with the HourMinuteClock.hourspec.cfg run from the LAB. They report the same states, because adding a PROPERTY line never changes the graph: it adds a question TLC asks of every step.
Read HourMinuteClock.tla to the end. Below the clock itself are the properties the next two lessons check, each with a comment. None of them is part of the clock. They are questions you can ask of it.
05Practice
Warm-up. A teammate gave the ferry a Wait action that says only that the ferry stays on its bank. TLC refuses to run it. Make it a real stuttering step, then ask whether it was needed at all.
Core. A clock radio's mute button does something to the clock that it should not. Find it with the hour clock's spec, and fix the button.
04-02-ex1Warm-up5 min
04-02-ex1 · Wait at the dock
Tier: warm-up. Time: about 5 minutes.
The ferry again. A teammate added an action Wait, "so the ferry may stay at the dock", and wrote it as one line: UNCHANGED side.
- Run
./tla check 04-02-ex1and read the error. Which variable does TLC say is not defined, and in which action? - Fill gap g1: make
Waita step in which nothing about the ferry changes. - Run the check again. The run also checks
MCFerryWait.cfg, which the course wrote: a ferry that starts in every stateTypeOKallows and takesWaitsteps and nothing else. If nothing about the ferry changes in aWaitstep, no step of that model changes anything, and that is what it checks. Leave the twoMCFerryWaitfiles as they are. Compare the distinct count ofFerry.cfgwith the solved ferry of 04-01-ex1 in the solutions folder. Then answer for yourself: did the ferry needWaitat all?
UNCHANGED side is a stuttering step for the subscript side. The ferry's subscript is vars.
Hints: HINTS.md, under "04-02-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/04-stuttering/exercises/02-ex1-wait-at-the-dock/; 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: "goal met: pass" for each.
Done when: ./tla check 04-02-ex1 prints "goal met: pass".
./tla check 04-02-ex1
Repo chapters/04-stuttering/exercises/02-ex1-wait-at-the-dock/
Hints
Hint 1
Read TLC's error to the end: it names the action and the variable that action leaves without a value.
Hint 2
UNCHANGED side says what happens to side and nothing about cars. A step that changes nothing about the ferry must say so for both variables.
Hint 3 (nearly the answer)
UNCHANGED takes a tuple, and the module already defines the tuple of all the ferry's variables. MCFerryWait.cfg starts the ferry in every state it can be in, and fails if a Wait step changes anything there. Then compare the distinct count with the 04-01-ex1 solution: [Next]_vars allowed these steps all along.
04-02-ex2Core8 min
04-02-ex2 · Mute the radio
Tier: core. Time: about 8 minutes.
A clock radio has an hour display and a volume knob. The question the cfg asks is the one from the lesson: is the display a valid hour clock? ClockRadio.tla brings in HourClock and checks HourSteps, the step half of the hour clock's spec: every step is an hour clock step or leaves hr alone.
- Run
./tla check 04-02-ex2and read the trace ofClockRadio.cfg, the first block, to its last step. Which action broke the hour clock's rule? - The radio could mute in its very first state too, and TLC did not complain there. Why not? Look at the value of
hrthere. - Fill gap g1: the mute button silences the radio and is not a clock button.
- Run the check again. The cfg's
@coverageline names all four actions, so deletingMutefromNextis not a fix. The second block isClockRadio.mute.cfg, which the course wrote: it checks the button's own rules at the foot of the module:MuteKeepsHour;MuteGoesSilent, that everyMutestep leaves the radio silent; andMuteSilences, that a playing radio can always be muted. Leave it as it is. Why is the hour clock's spec alone not enough? Ask whatHourStepswould say about aMutethat moved the hour on by one.
The volume actions say UNCHANGED hr. To the hour clock they are stuttering steps, whatever they do to the volume. That is the lesson.
Hints: HINTS.md, under "04-02-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/04-stuttering/exercises/02-ex2-mute-the-radio/; 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: "goal met: pass" for each.
Done when: ./tla check 04-02-ex2 prints "goal met: pass".
./tla check 04-02-ex2
Repo chapters/04-stuttering/exercises/02-ex2-mute-the-radio/
Hints
Hint 1
The trace ends with a Mute step. Compare hr before and after it, and ask what the hour clock's step rule allows.
Hint 2
The first state already has hr = 12, so a Mute there leaves hr unchanged: a stuttering step for the hour clock. The problem appears only once the clock has moved on.
Hint 3 (nearly the answer)
A radio button should leave the clock exactly as it was. Replace the line that sets hr' with the same kind of conjunct that Louder and Quieter use for hr. A Mute that moved the hour on by one would satisfy HourSteps, and ClockRadio.mute.cfg is there to catch it.
06Explain it
Open NOTES.md and answer the three prompts under 04-02, 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 "04-02 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "Mute now leaves hr unchanged, so to the hour clock every mute is a stuttering step, whatever it does to the volume. The hour clock's spec constrains only hr; the volume is outside its subscript. TLC found that every step of the radio is an hour clock step or leaves hr alone, with all four actions firing and LoudAtEleven reached, in a model with Loudest = 3. It did not check that the hour ever advances, or anything about minutes."
A weak answer: "Mute was changing the hour, so I stopped it." It is true, but it does not say why changing the volume is allowed and changing hr is not, which is the point.
Done?