04-01
Chapter 04 · Stuttering and the shape of a spec
The shape of every spec
00Recall
One card from 03-01 (what a cfg tells TLC that the module does not), one from chapter 02 (what \/ does inside an action), and one from chapter 01 (why a behavior never ends). The last one carries this whole chapter: a clock that stops still has a next state. It is the same state again.
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.
02-03-a from Or means "may"
What does \/ do inside an action?
Show answer
It lists the ways a step may go. A pair of states satisfies the action if it satisfies any disjunct, so when several are true the spec allows each, and TLC explores every one. Nothing is chosen.
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.
Skip recall Recall is never required.
01Commit
This chapter's spec is the smallest one in the course. specs/HourClock.tla is the hour hand of a clock, and nothing else. One variable, hr, from 1 to 12. It starts at 12, and its one action moves it on an hour:
Advance is defined as
and hrprime = IF hr = 12 THEN 1 ELSE hr + 1The whole system is one line, the shape you met in 03-01:
Spec is defined as Init and always[Next]_varsHere vars is << hr >> and Next is Advance. A behavior is an infinite sequence of states, so write one down as the values of hr, in order.
Commit a
Does Spec allow the behavior 12, 12, 1, 2, 2, 2, 3, 4, ... in which the hand sometimes stays put?
- Yes. A step that leaves hr where it was is allowed between moves.
- No. Next says the hand moves, and Spec says every step is a Next step.
- Only in TLA, the logic. TLA+, the language TLC reads, does not allow it.
Answer Yes. A step that leaves hr where it was is allowed between moves.
- If you picked B
- Read the box again: [Next]_vars, not Next. The subscript adds a second way to take a step. Pull the formula apart below and find it.
- If you picked C
- TLA+ is TLA plus the mathematics to write states and actions in. It does not change what [][Next]_vars means.
A teammate has made a copy, specs/RestingClock.tla. It is identical except for one more action in Next, "so the clock is allowed to wait between hours":
Rest is defined as
and UNCHANGED hrHourClock has 12 distinct states, one per hour.
Commit b
How many distinct states will TLC report for RestingClock?
Your answer is a whole number.
Answer 12
- Exactly right
- Right. Rest adds steps, but every one of them lands on the state it started from.
- Within 10 %
- Close, but a state is only a value for every variable, and Rest adds no new value of hr.
- Under by more than half
- Under. Rest takes nothing away: every Advance step is still there.
- More than double
- More than double. A state is the values of the variables, not the path that reached them or the action that produced them.
- Anything else
- If you said twice as many, you counted a resting hand at 3 as a different state from a moving hand at 3. A state is only the values of the variables, and hr is 3 in both.
02Lab
Run the hour clock, then the teammate's copy:
./tla check chapters/04-stuttering/specs/HourClock.cfg
./tla check chapters/04-stuttering/specs/RestingClock.cfg04-stuttering/specs/RestingClock04-stuttering/specs/RestingClock RestingClock.cfg
parse ok
model check: no violation found in 12 distinct states (25 generated, depth 12)
expected outcome met: pass
coverage: all 2 actions fired
witness AlmostNoon reached (trace of 12 states)Reveal for commit a
Yes. Every step of that behavior is either an Advance step or a step where hr stays the same, and [Next]_vars allows both. The repeated 12 and the repeated 2 are stuttering steps: steps that change none of the variables in the subscript. No cfg line was needed to allow them. They are in Spec itself.
Reveal for commit b
12 distinct states, the same as HourClock. Look at the other number: TLC generated 25 states for RestingClock and 13 for HourClock. The extra ones are the Rest steps. Each lands on the state it started from, which TLC has already seen.
03Reason
If you said no, you read [][Next]_vars as "every step is a Next step". It is an easy reading, because the box looks like a loop and Next looks like its body. It is neither. It is a formula, and we can take it apart one symbol at a time.
Start with the outside. Spec is true or false of a whole behavior. Init is a state predicate: TLA reads it as a claim about the first state. /\ (read "and") joins two claims about the same behavior. That leaves [][Next]_vars.
[] is read "always". []F says that F holds now and at every point from here on. What is F here? It is [Next]_vars, read "square Next sub vars". That is an action: a claim about a pair of states, the step from one state to the next. It is short for
Next \/ (vars' = vars)"A Next step, or a step after which vars has the value it had before." vars' is read "vars prime", the value in the next state. Since vars is the tuple << hr >>, the tuple is unchanged exactly when hr is. Put back together, Spec says: the first state satisfies Init, and every step either is a Next step or leaves every variable in vars alone.
Try it on a behavior that begins 12, 1, 1, 2. The first state has hr = 12, so Init holds. The step from 12 to 1 satisfies Advance, the first disjunct. The step from 1 to 1 fails Advance, which wants 2, and satisfies the second disjunct. The step from 1 to 2 is an Advance again. Every step passes one of the two tests, so far, and the behavior is allowed as long as every later step does the same. Now try 12, 3. Advance wants 1, and hr changed, so neither disjunct holds. One bad step, and the whole behavior is out: [] means every step, not most of them.
Write something before openingwhere in the formula is a step from noon to noon allowed?
In the second disjunct of [Next]_vars. vars' = vars is true of that step, so [Next]_vars is true of it, whatever Next says. Advance itself is false of that step: it demands hr' = 1.
Now the teammate's Rest. It is UNCHANGED hr, which is hr' = hr. Every Rest step is a step that leaves vars alone, and [Next]_vars allowed those already. So
[Advance \/ Rest]_vars and [Advance]_varsare the same action, and the two specs allow exactly the same behaviors. The teammate added a name, not a behavior. TLC agrees in its own way: same distinct states, more generated ones.
The subscript also has a mirror image. <<A>>_v, read "angle A sub v", is short for
A /\ (v' # v)"An A step that really changes v." # is read "is not equal to". Where the square form forgives a step that changes nothing, the angle form insists on change. For the teammate's action, <<Rest>>_vars is false of every step there is: no step both leaves hr alone and changes it. That is the precise way to say "Rest never does anything". You will need the angle form in chapter 10, where fairness talks about steps that must eventually happen. A step that changes nothing will not count as one.
Write something before openingis <<Advance>>_vars true of the step from noon to one o'clock? Of a step that stays at three o'clock?
Of the first, yes: it is an Advance step, and hr changed. Of the second, no: Advance wants hr' = 4, and hr did not change, so both conjuncts fail. [Advance]_vars is true of both steps. The square form lets the repeat through, and the angle form does not.
Write something before opening./tla printed "coverage: all 2 actions fired" for RestingClock. Did Rest do anything?
Nothing visible. ./tla counts an action as fired when TLC generated at least one state from it, even a state it had already seen. Every Rest step leads back to where it started. The coverage line is true, and it does not mean that Rest matters.
One more pair of names, because you will meet both, and the third choice in the first prediction mixed them up.
That is why stuttering, which chapter 01 named, never changed a verdict in chapter 03, though the steps were always in Spec. Every check there was an invariant, and to an invariant a step that goes nowhere new is invisible. Next lesson asks what they leave unchanged, and the answer is less than you think.
04In your editor
The specs are in chapters/04-stuttering/specs/. Check this lesson's exercises with:
./tla check 04-01The hour clock on its own:
04-stuttering/specs/HourClock04-stuttering/specs/HourClock HourClock.cfg
parse ok
model check: no violation found in 12 distinct states (13 generated, depth 12)
expected outcome met: pass
coverage: all 1 actions fired
witness AlmostNoon reached (trace of 12 states)The depth is 12: the hand needs that many states, counting the first, to visit every hour once. No stuttering step appears in that count, and none needs to. A behavior may repeat any state as often as it likes, but a repeat is never a new state, so the reachable set, the depth and every invariant verdict are the same as if stuttering did not exist. Stuttering matters when you compare specs with each other, which is where the next three lessons go, and when you ask whether something must eventually happen, which is chapter 09. Open HourClock.tla and find each piece of the formula in the text: Init, Next, vars, the box and the subscript. Then read RestingClock.tla and say out loud why Rest is a name and not a behavior.
05Practice
Warm-up. A ferry spec with every part except Spec. TLC refuses to start without the box; write it.
Core. An egg timer rings once and then has nothing left to do, and TLC calls that a deadlock. Write the one action that says the stop is intended, and only after the bell.
04-01-ex1Warm-up5 min
04-01-ex1 · Give it a box
Tier: warm-up. Time: about 5 minutes.
A car ferry: cars board one at a time, the ferry crosses with at least one car aboard, and the cars aboard drive off together. Ferry.tla has the variables, TypeOK, Init, the three actions and Next. It has no behavior formula yet: Spec is only Init.
- Run
./tla check 04-01-ex1and read TLC's error. It blames "the configuration file". The cfg does saySPECIFICATION Spec. So where did TLC look for the next-state relation, and what did it find there? - Fill gap g1 in
Ferry.tla: start inInit, then every step is aNextstep or leaves every variable unchanged. The lesson'sHourClock.tlahas the same shape. - Run the check again. This time
./tlaalso checks thatBoard,CrossandUnloadeach fire, so a box that leaves one out fails, and that the witnessFullSouth, a full deck on the south bank, is reached. If aTLC warning:line appears, read the whole warning in the raw log that./tlanames: a box with the wrong subscript still passes this check, and that warning is the only sign.
Hints: HINTS.md, under "04-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/04-stuttering/exercises/01-ex1-give-it-a-box/; 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 04-01-ex1 prints "goal met: pass".
./tla check 04-01-ex1
Repo chapters/04-stuttering/exercises/01-ex1-give-it-a-box/
Hints
Hint 1
TLC reads the steps of a system out of the box in Spec. This Spec has no box, so nothing in it says how to leave the first state, and TLC refuses to start.
Hint 2
The shape is the same in every spec of this course: Init, "and", "always", then the next-state action in square brackets with a subscript. The subscript is the tuple of every variable, which the module already defines.
Hint 3 (nearly the answer)
Copy the shape of Spec from specs/HourClock.tla. Nothing else in the ferry changes, and delete the \* GAP marker.
04-01-ex2Core8 min
04-01-ex2 · Ring, then rest
Tier: core. Time: about 8 minutes.
A wind-up egg timer counts down from Start and rings once at zero. Then it has nothing left to do, and TLC reports a deadlock: a reachable state with no Next step. Here the stop is the design, not a bug. Chapter 03 said that when stopping is intended, the spec should say so. You will say it with one action.
- Run
./tla check 04-01-ex2and read the deadlock trace to its last state. Is anything wrong with the timer in that state? - Fill gap g1 in
EggTimer.tla:Doneis a step that changes nothing, and it is possible only after the bell has rung. Keep theUNCHANGED varsline. - Run the check again. The run also checks three cfgs the course wrote. Two are a broken timer plus your
Done:MCEggTimer.cfg, whose dial jams one tick before zero, andMCEggTimer.silent.cfg, whose dial reaches zero while the bell is broken. Neither timer rings, so each stop is a real bug, and TLC must still report the deadlock there. ADonethat lets the timer stutter before the bell has rung would hide it. A third,MCEggTimer.bell.cfg, runs your own timer and checks in every state thatDoneis possible only once the bell has rung. Leave theMCEggTimerfiles as they are. - Before you look at the solution, write one sentence: which behaviors does
Specallow withDonethat it did not allow without it? (The lesson'sRestis the clue.)
Deadlock checking stays on in every cfg. Turning it off would also hide the jammed dial.
Hints: HINTS.md, under "04-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/04-stuttering/exercises/01-ex2-ring-and-rest/; 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: "goal met: pass" for EggTimer.cfg and MCEggTimer.bell.cfg, and "goal met: deadlock" for MCEggTimer.cfg and MCEggTimer.silent.cfg.
Done when: ./tla check 04-01-ex2 prints "goal met: pass".
./tla check 04-01-ex2
Repo chapters/04-stuttering/exercises/01-ex2-ring-and-rest/
Hints
Hint 1
The deadlock trace ends with the bell rung and the dial at zero. That is the timer's finished state, and in that state no action can fire. Which steps should still be possible there?
Hint 2
Done already says UNCHANGED vars. What it lacks is a guard: the condition under which a step that changes nothing is part of the design. The gap's comment says when.
Hint 3 (nearly the answer)
Replace FALSE with a guard on the variable that records the bell. A guard on nothing, or on TRUE, would let every state stutter, and MCEggTimer.cfg would no longer find the jammed dial. A guard on the dial instead of the bell fails MCEggTimer.silent.cfg, where the dial reaches zero and the bell never rings.
06Explain it
Open NOTES.md and answer the three prompts under 04-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 "04-01 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "Done lets the timer take steps that change nothing, but only once the bell has rung. Those steps were already allowed by [Next]_vars, so Spec allows the same behaviors as before; what changes is that TLC no longer calls the finished state a deadlock. It relies on the guard: without it, a timer that jams before ringing would stutter too, and MCEggTimer.cfg shows TLC still catches that. TLC checked one model with Start = 3; it says nothing about how long a tick takes, or that the timer ever reaches zero in a real kitchen."
A weak answer: "I added Done so TLC stops complaining about a deadlock." It describes the symptom it silenced, not what Done means, and it would accept a Done that hides every deadlock.
Done?