04-04
Chapter 04 · Stuttering and the shape of a spec
The frame-rate test and the shadow
00Recall
One card from 04-03 (why []Next is rejected), one from 04-02 (why a glued clock is a valid hour clock), and one from 03-03 (how a witness check works). The last one is a reminder that a pass needs evidence; this lesson ends with a pass that needs it badly.
04-03-a from Why the subscript
Why does SANY reject Spec == Init /\ []Next?
Show answer
[] cannot be applied to a bare action. The only action form allowed under [] is [A]_v, which always permits steps that leave v unchanged. That keeps every TLA formula stuttering invariant.
04-02-b from Stuttering steps
A clock whose hand never moves satisfies Init /\ [][Advance]_hr when Init holds in its first state. Why?
Show answer
Every step of its behavior leaves hr unchanged, and the box allows every such step. A formula of this shape says which steps are allowed, never that a step must happen.
03-03-b from Break it on purpose
How does ./tla check that the witness W is reachable?
Show answer
It checks ~W as an invariant in a wrapper module and expects a violation; the trace shows how W is reached.
Skip recall Recall is never required.
01Commit
A teammate wants to say "the hour hand moves at every step". They know [] needs the square form now, so they write
HourAlwaysMoves is defined as always[hrprime not equal to hr]_hrand check it on HourMinuteClock, where most steps are minute ticks that leave hr alone.
Commit a
TLC checks PROPERTY HourAlwaysMoves on HourMinuteClock. What does it report?
- No violation.
- A violation on the first minute tick, which leaves hr where it was.
- A violation on the first TickHour, which changes min as well as hr.
Answer No violation.
- If you picked B
- Unpack the box. A step that leaves hr where it was satisfies one of its two disjuncts. Which one?
- If you picked C
- The subscript is hr, so the property never looks at min. A TickHour step changes hr, and hr' # hr is true of it.
specs/TravelClock.tla is the minute clock with two time-zone buttons. ZoneForward moves the hour hand one hour on; ZoneBack moves it one hour back. Neither touches the minute hand. TravelClock.cfg checks HourSteps, the step half of the hour clock's spec: every step is an hour clock step, or leaves hr alone. With both buttons the check fails, and TLC prints the shortest trace it can find.
Commit b
How many states will the trace of TravelClock.cfg have, counting the first state?
Your answer is a whole number.
Answer 2
- Exactly right
- Right. ZoneBack is possible in the very first state, and a step from 12 back to 11 is no hour clock step.
- Within 10 %
- Close. Find the first state in which a bad step is possible, and count the states up to the end of that step.
- Under by more than half
- Under. A trace always starts with the first state, and a violated action property needs at least one step after it.
- More than double
- Over. The buttons do not wait for the minute hand, and TLC prints a shortest trace. Which button press breaks the rule, and how soon can it happen?
- Anything else
- If you said 1, you counted the bad step instead of the states: a trace of one step has two states. If you said 3 or 4, ask whether anything has to happen before the hand can go back.
02Lab
./tla check chapters/04-stuttering/specs/HourMinuteClock.moves.cfg
./tla check chapters/04-stuttering/specs/TravelClock.cfg04-stuttering/specs/TravelClock04-stuttering/specs/TravelClock TravelClock.cfg
parse ok
model check: action property HourSteps violated; trace has 2 states (1 steps); 4 distinct states
expected outcome met: property HourSteps
trace (changed variables only; ./tla trace --full shows all):
1 Init hr = 12, min = 0
2 ZoneBack hr: 12 -> 11Reveal for commit a
No violation, in 720 states. The property says "every step changes hr, or leaves hr unchanged". Every step there is does one or the other, so the property is true of every behavior of every spec. It says nothing at all, and the run looks exactly like a pass that means something.
Reveal for commit b
2 states: the first state, and one ZoneBack step that moves the hand from 12 back to 11. No hour clock does that, and the back button is there from the start. Nothing about the minute hand had to happen first: HourSteps does not know the minute hand exists.
03Reason
If you expected HourAlwaysMoves to fail, you assumed TLA could count steps. It cannot, and that is a feature. To see why, pretend you are filming the minute clock.
Film it at one frame per minute and watch only the hour hand. It stands still for fifty-nine frames, then moves. Film it at one frame per second: it stands still for thousands of frames, then moves. Film it at one frame every time anything in the room changes, a fly, a cloud, and it stands still for even more. The story of the hour hand is the same in every film: 12, then 1, then 2. Only the number of repeated frames changes.
A TLA formula is a judge who cannot tell these films apart. Adding or removing repeated frames, steps that leave the formula's variables alone, never changes its verdict. That is stuttering invariance. Two things make it hold: every box allows the steps that leave its subscript alone, so a few more or a few fewer of them cannot matter, and TLA has no temporal operator that means "in the next state": a primed variable may appear only inside an action, and an action may sit under [] only as [A]_v. So no formula can point at the frame after this one. Call it the frame-rate test. Every TLA formula passes it, by construction.
Write something before opening"the hour hand moves at every step" fails the frame-rate test. Why?
Film the same clock at a faster rate and the hour hand no longer moves at every frame. The claim would be true of one film and false of another, though the clock is the same. TLA has no way to write it: [] refuses the bare action (04-03), and the square form forgives the extra frames, which is how HourAlwaysMoves came to mean nothing.
It is worth being clear about what the teammate got. Not an error, not a warning: a pass, with full coverage and a reached witness, because the witness and coverage are facts about the spec, and the spec is fine. The only thing wrong is the property, and a property that cannot fail looks exactly like one that holds. When you write a property, break the spec on purpose once and watch the property catch it.
So some sentences cannot be written as TLA formulas: "at every step", "within three steps", "exactly once every sixty steps". All of them count frames, and frames are not time. A step in a spec is one change, not one tick of a real clock. When a requirement really is about time, you model time: a variable that counts minutes, as min does. Then "the hour hand moves every sixty minutes" becomes a statement about min and hr, and the frame rate stops mattering.
Now the payoff. Take any behavior of the minute clock and keep only the hour hand in each state. Call what is left the shadow of the behavior: 12, 12, 12, …, 12, 1, 1, 1, …. Each shadow is a behavior of the hour clock with stuttering steps inserted: at least fifty-nine between one hour and the next, and, in a behavior that stops ticking, infinitely many at the end. Because the hour clock's spec passes the frame-rate test, the extra frames do not matter, and every shadow satisfies HourSpec. That is what TLC confirmed in 04-02, when it checked PROPERTY HourSpec on HourMinuteClock.
This is the precise version of "the minute clock is still an hour clock": Spec => HourSpec, every behavior of the detailed spec has a shadow that the simple spec allows. Chapter 11 calls it refinement, lets the shadow be computed rather than read off a shared variable, and makes it the subject of the whole chapter. Without stuttering invariance it could not work at all: the detailed spec usually takes steps that the simple spec has no counterpart for, and only steps the simple spec forgives make room for them.
Write something before openingTravelClock.cfg checks HourSteps, not HourSpec. What is HourSteps, and why is that enough?
HourSteps == [][Hour!Next]_hr, the step half of HourSpec. HourSpec is Hour!Init /\ [][Hour!Next]_Hour!vars, and Hour!vars is << hr >>, so its box is the same as [][Hour!Next]_hr. The first half only says the hand starts at 12, which TravelClock's Init already says. Checking the step half on its own gives a verdict that names it. Check the whole HourSpec instead, and TLC names the violated part by its position in HourClock.tla, not by a name.
Write something before openingtake the travel clock with only the forward button (EastSpec, checked by TravelClock.east.cfg against HourSpec). Is its hour hand still a valid hour clock?
Yes. TLC finds no violation in 720 distinct states, with all three actions firing and the witness reached. Every ZoneForward step moves hr forward one hour, which is an hour clock step, even when it happens in the middle of an hour. The hour clock's spec mentions only hr. It does not know minutes exist, so it cannot object to an hour that arrives early.
The travel clock shows both edges of the idea. With the back button, the shadow contains a step from 12 to 11, which the hour clock forbids: TLC finds it in one step. With only the forward button, the hour hand sometimes jumps in the middle of an hour, and the check passes anyway. A shadow check checks the shadow. If you want "the hour hand moves only when the minute hand passes 59", that is a property about both hands, and you must write it.
04In your editor
The specs are chapters/04-stuttering/specs/HourMinuteClock.tla and TravelClock.tla. Check this lesson's exercises with:
./tla check 04-04The forward-only travel clock from the last fold, passing:
04-stuttering/specs/TravelClock.east04-stuttering/specs/TravelClock.east TravelClock.east.cfg
parse ok
model check: no violation found in 720 distinct states (1441 generated, depth 71)
expected outcome met: pass
coverage: all 3 actions fired
witness AlmostNoon reached (trace of 71 states)Read the counts as well as the verdict. The distinct count is the same as the minute clock's, so the forward button reaches no new combination of hands; it only reaches them sooner. Its depth is 71, against 720 for the clock without buttons: the forward button reaches every hour long before the minute hand would. Read TravelClock.tla: EastSpec is a second behavior formula in the same module, with its own next-state action, and TravelClock.east.cfg names it in SPECIFICATION.
05Practice
Warm-up. A clock with an "am/pm" window flips the window at the wrong hour. Fix the hand's action so that the window changes only between 11 and 12, and the hour hand stays an hour clock.
Core. Add a second hand to the minute clock. Every hand on its own must still behave like a clock hand, and the minute hand must move only as the second hand passes 59.
04-04-ex1Warm-up5 min
04-04-ex1 · AM and PM
Tier: warm-up. Time: about 5 minutes.
An hour clock with a small window that shows "am" or "pm". It starts at midnight: 12, "am". The window should change when the hand goes from 11 to 12, and at no other time. The cfg checks three action properties the course wrote: HourSteps (the hand alone is a valid hour clock), HalfRule (the window changes only when the hand goes from 11 to 12) and HalfFlips (and then it does change, at noon and at midnight).
- Run
./tla check 04-04-ex1and read the trace. Which property failed, and on which step? - Fill gap g1 in
AmPmClock.tla: write the new value ofhalf. Leave the line forhr'as it is. - Run the check again. The witness
ElevenPmneeds a whole day: if it is not reached, the window never changes.
Whatever you do to half, HourSteps cannot see it. The window is outside the hour clock's subscript.
Hints: HINTS.md, under "04-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/04-stuttering/exercises/04-ex1-am-and-pm/; 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-04-ex1 prints "goal met: pass".
./tla check 04-04-ex1
Repo chapters/04-stuttering/exercises/04-ex1-am-and-pm/
Hints
Hint 1
The window changed on the step from hr = 12 to hr = 1. HalfRule allows it to change only on the step from hr = 11 to hr = 12.
Hint 2
The new value of half depends on the old value of hr: on one particular hour it flips, and on every other hour it stays as it was.
Hint 3 (nearly the answer)
Write half' with IF on the old hr: in one branch the flip the starter already has, in the other the old value of half. Do not read hr' in that line; write every new value from old ones. A window that only ever sets "pm" fails HalfFlips: at midnight it must flip back.
04-04-ex2Core8 min
04-04-ex2 · A second hand
Tier: core. Time: about 8 minutes.
Add a second hand to the minute clock. SecondClock.tla has the three variables, the course's checks, and a finished TickMinute to read first. You write TickSecond, and you update TickHour, which still behaves as if seconds did not exist.
The cfg checks five action properties the course wrote: each hand on its own still steps like a clock hand (HourSteps, MinuteSteps, SecondSteps), the minute hand moves only as the second hand passes 59 (MinuteOnTheMinute), and when it passes 59 the minute hand does move (SecondsCarry).
- Run
./tla check 04-04-ex2. Why can nothing happen in the very first state? - Fill gap g1: the second hand moves on one second, and the other two hands stay where they are. Run the check. TLC now stops with an error in
TickHour. Read which variable it names. - Fill gap g2: at the end of an hour, all three hands move. Use
TickMinuteas your model. - Run the check until it passes. A clock with a second hand has a lot of states, so give it a few seconds. If an action property fails, the trace can run to thousands of lines of
TickSecond: the evidence is the last two states, the bad step.
Before you open the solution, say in one sentence why adding a whole new hand did not break the hour clock's property: what does a TickSecond step look like to HourSteps?
Hints: HINTS.md, under "04-04-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/04-stuttering/exercises/04-ex2-a-second-hand/; 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-04-ex2 prints "goal met: pass".
./tla check 04-04-ex2
Repo chapters/04-stuttering/exercises/04-ex2-a-second-hand/
Hints
Hint 1
TickMinute waits for sec = 59, and TickHour waits for min = 59. In the first state neither holds, and TickSecond is still FALSE.
Hint 2
TickSecond has a guard, a new value for sec, and UNCHANGED for the two hands it does not move. After g1, read TLC's error: TickHour does not say what happens to sec.
Hint 3 (nearly the answer)
TickHour needs the same two extra conjuncts that TickMinute has: a guard on the second hand, and its new value. Without the guard, the hour hand can move as soon as minute 59 begins, and MinuteOnTheMinute notices. A TickSecond that wraps past 59 by itself fails SecondsCarry: the minute hand has to move when the second hand comes round.
06Explain it
Open NOTES.md and answer the three prompts under 04-04, 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-04 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "TickSecond moves only sec, and TickHour now waits for both other hands to reach 59 and resets sec. A second tick leaves hr and min alone, so it is a stuttering step for the subscripts of HourSteps and MinuteSteps, and adding a hand did not break them, for the same reason a minute tick is invisible to the hour clock. The guards matter: without sec = 59 in TickHour, the hour could move early, which MinuteOnTheMinute catches and HourSteps alone would not, since each early move is still one hour forward. TLC explored one model starting at 12:00:00; a step is one move of a hand, not a second of real time."
A weak answer: "I copied TickMinute and it passed." It does not say why the coarser properties still hold, or which check would catch a wrong guard.
Done?