04-03
Chapter 04 · Stuttering and the shape of a spec
Why the subscript
00Recall
One card from 04-02 (stuttering is relative to a subscript), one from 04-01 (the angle form), and one from chapter 02 (what a primed variable stands for). Today the subscript stops being decoration.
04-02-a from Stuttering steps
A step of the minute clock takes min from 7 to 8 and leaves hr unchanged. Is it a stuttering step?
Show answer
For the hour clock's spec, subscript hr, yes: hr is unchanged. For the minute clock's spec, subscript << hr, min >>, no: min changed. Stuttering is always relative to a subscript.
04-01-b from The shape of every spec
What does <<A>>_v mean, and why is <<Rest>>_vars false of every step when Rest is UNCHANGED vars?
Show answer
A /\ (v' # v), an A step that really changes v. A Rest step never changes vars, so no step is both.
02-01-a from Primes
In an action, what does a primed variable such as hour' stand for?
Show answer
The variable's value in the new state of a step. The unprimed hour is its value in the old state. The action is a true-or-false claim about that pair of states, not an instruction.
Skip recall Recall is never required.
01Commit
If stuttering steps cause so much thought, why not drop them? A teammate does exactly that. specs/BareClock.tla is HourClock with the subscript taken off the box:
Spec is defined as Init and alwaysNext"Every step is a Next step. No stuttering, no surprises."
Commit a
You run ./tla check on BareClock.cfg. What happens?
- TLC checks a stricter spec, in which the hand moves at every step.
- TLC checks it exactly like HourClock, since its graph comes from Next anyway.
- SANY rejects the module before TLC starts.
Answer SANY rejects the module before TLC starts.
- If you picked A
- That stricter spec is what your teammate meant. Ask whether TLA lets you say it: which kinds of formula may follow []?
- If you picked B
- TLC's graph does come from Next. But TLC never gets that far: the module has to be well formed first.
Now two claims about HourMinuteClock. Both use the hour clock's step, written for this module:
HourNext is defined as hrprime = IF hr = 12 THEN 1 ELSE hr + 1They differ only in their subscripts:
WatchesBoth is defined as always[HourNext]_tuple of hr, min end tuple
WatchesHour is defined as always[HourNext]_hrA formula of the form [][A]_v that is not the whole spec, but a claim about it, is an action property. It goes in the cfg on a PROPERTY line.
Commit b
TLC checks WatchesBoth and WatchesHour on HourMinuteClock, one cfg each. What does it report?
- Both hold: minute ticks do not change the hour, so they are stuttering steps for both.
- WatchesHour holds. WatchesBoth is violated on the first minute tick.
- Both are violated: a minute tick is not an HourNext step.
Answer WatchesHour holds. WatchesBoth is violated on the first minute tick.
- If you picked A
- A stuttering step leaves the subscript's variables unchanged. List the variables in each subscript, then ask what a minute tick does to each one.
- If you picked C
- A minute tick is not an HourNext step, true. But the box has a second disjunct. For one of the two subscripts, the minute tick satisfies it.
02Lab
./tla check chapters/04-stuttering/specs/BareClock.cfg
./tla check chapters/04-stuttering/specs/HourMinuteClock.both.cfg
./tla check chapters/04-stuttering/specs/HourMinuteClock.hour.cfg04-stuttering/specs/BareClock04-stuttering/specs/BareClock BareClock.cfg
parse error: [] followed by action not of form [A]_v.
chapters/04-stuttering/specs/BareClock.tla:24:17: [] followed by action not of form [A]_v.
expected outcome met: parse-error not of formReveal for commit a
SANY rejects it: "[] followed by action not of form [A]_v." TLC never starts. The only way TLA lets you put an action under [] is the square form, [A]_v, which always allows a step that leaves v alone. There is no way to write "every step is a Next step" in TLA at all.
04-stuttering/specs/HourMinuteClock.both04-stuttering/specs/HourMinuteClock.both HourMinuteClock.both.cfg
parse ok
model check: action property WatchesBoth violated; trace has 2 states (1 steps); 2 distinct states
expected outcome met: property WatchesBoth
trace (changed variables only; ./tla trace --full shows all):
1 Init hr = 12, min = 0
2 TickMinute min: 0 -> 1Reveal for commit b
WatchesHour holds in all 720 states. WatchesBoth is violated with a trace of 2 states: the first state, and one minute tick. The tick changes min, which is in WatchesBoth's subscript, and it is not an HourNext step. So neither disjunct holds.
03Reason
If you picked "both hold", you read stuttering as "steps that do not change the variable I care about". It is "steps that do not change the variables in the subscript". WatchesBoth cares about min because its author put min in the subscript.
Start with the parse error, because it explains everything else. Formulas in TLA come in levels. A state predicate is about one state. An action is about a pair of states. A temporal formula is about a whole behavior. [] takes a temporal formula, and also a state predicate, which it reads as a claim about the first state of each suffix. What it does not take is a bare action. The one exception is the square form: [][A]_v is allowed, and only that.
Write something before openingwhy would a logic forbid "every step is a Next step"? It sounds like the most natural claim there is.
Because no system that sits inside a bigger one can satisfy it. Put the hour clock inside the minute clock and fifty-nine of every sixty steps are minute ticks, which are not hour steps. If []Next were allowed, the hour clock's spec would be false of the minute clock, and of every system that does anything besides move an hour hand. By allowing only [A]_v, and by having no temporal operator that means "in the next state" (a primed variable may appear only inside an action), TLA makes every formula stuttering invariant: adding or removing steps that leave its variables alone never changes whether it is true. Lesson 04-04 puts that to work.
This is also why chapter 03 never mentioned stuttering. Invariants are about single states, and a stuttering step repeats a state, so no invariant can tell the difference. The subscript starts to matter the moment a formula talks about steps.
So every box carries a subscript, and the subscript is a choice you make. It answers one question: which variables is this formula about? Steps that leave those variables alone are none of its business. Steps that change them must satisfy the action.
Now the two properties make sense. WatchesHour is about hr. A minute tick leaves hr alone, so WatchesHour lets it through, and an hour tick satisfies HourNext. WatchesBoth is about both hands. A minute tick changes min, so WatchesBoth judges it, and it fails HourNext. The two formulas share an action and disagree about the world.
Write something before openinga requirement says "the clock's time, hour and minute together, only ever moves forward". Someone writes it with the subscript hr. What slips past?
Every step that leaves hr alone, whatever it does to min. A minute hand that ran back from 40 to 10 within the hour would be a stuttering step for that property, and it would pass. That is a subscript that is too narrow: it leaves out a variable the requirement is about. WatchesBoth is the opposite mistake, too wide: it watches a variable the requirement is not about, so legitimate steps of another part of the clock break it. The right subscript is the list of variables the requirement talks about, no more and no fewer.
An action property is a different kind of check from an invariant. An invariant is a state predicate, true or false of one state; TLC evaluates it in every reachable state. An action property is about steps; TLC evaluates its action on every step of the state graph. When one fails, the trace ends with the bad step, so the last two states are the evidence. And it goes on a PROPERTY line. Under INVARIANT, TLC refuses it:
04-stuttering/specs/HourMinuteClock.asinvariant04-stuttering/specs/HourMinuteClock.asinvariant HourMinuteClock.asinvariant.cfg
parse ok
model check: TLC error: The invariant WatchesHour is not a state predicate (one with no primes or temporal operators).
expected outcome met: error not a state predicateNotice what changed between the two runs and what did not. The spec and the graph are the same; only the question differs. The counts differ only because TLC stops at the first violation: the failing run reports the states it reached before the bad step. That is the general pattern for a property: the spec says what the system may do, and each property is a separate claim about those behaviors, checked one cfg at a time.
Action properties are how you write requirements about change: "the balance changes only by a deposit or a withdrawal", "the lap display changes only when Lap is pressed", "the channel changes only when someone presses a channel button". An invariant cannot say any of those, because a single state does not know how it was reached. The gate in this chapter asks for one.
04In your editor
The properties are at the bottom of chapters/04-stuttering/specs/HourMinuteClock.tla. Check this lesson's exercises with:
./tla check 04-03The property that holds:
04-stuttering/specs/HourMinuteClock.hour04-stuttering/specs/HourMinuteClock.hour HourMinuteClock.hour.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)Open HourMinuteClock.both.cfg and HourMinuteClock.hour.cfg side by side. They differ in one line. A third cfg, HourMinuteClock.min.cfg, checks WatchesMinute == [][HourNext]_min: the hour clock's step, with the minute hand as its subscript. Write down what you expect TLC to say, then run it:
./tla check chapters/04-stuttering/specs/HourMinuteClock.min.cfgWrite something before openingwas your prediction right, and is WatchesMinute a property of the hour hand at all?
TLC reports "action property WatchesMinute violated" with a trace of 2 states: the first state and one minute tick. That tick changes min, the variable in the subscript, and it is not an HourNext step. The subscript is not narrower than hr; it is aimed at the wrong variable. The property watches the minute hand and asks it to move like an hour hand, and it says nothing about hr on the steps that leave min alone.
05Practice
Warm-up. The harbour master's rule for the ferry is written with a subscript that watches too much. Fix what it watches.
Core. A product manager's rule about a television's channel, as an action property, from a blank line. The course's checks make sure it is neither too strict nor too weak.
Stretch. A drawbridge from a skeleton, including the cfg: an invariant and an action property that say different things.
04-03-ex1Warm-up5 min
04-03-ex1 · Only by crossing
Tier: warm-up. Time: about 5 minutes.
The harbour master's rule for the ferry: it changes banks only by crossing. Boarding and unloading are none of the rule's business. Ferry.tla states it as an action property, SideRule, with the right action and the wrong subscript.
- Run
./tla check 04-03-ex1and read the trace ofFerry.cfg. Which step breaksSideRule, and does it break the harbour master's rule? - Fill gap g1: keep
Crossin the box, and change only what the property watches. - Run the check again. The run also checks three ferries the course wrote, each with one extra step. In
MCFerry.cfgthe empty ferry drifts to the other bank; inMCFerry.late.cfga late car drives aboard as the ferry pulls away. Both change banks without aCrossstep, so yourSideRulemust fail on both. InMCFerry.onebyone.cfgcars drive off one at a time, which never moves the ferry, so yourSideRulemust pass there. A rule that watches nothing would not fail on the first two, and a rule that forbids every step outsideNextwould fail on the third. Leave theMCFerryfiles as they are.
Hints: HINTS.md, under "04-03-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/04-stuttering/exercises/03-ex1-only-by-crossing/; 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 Ferry.cfg and MCFerry.onebyone.cfg, and "goal met: property SideRule" for MCFerry.cfg and MCFerry.late.cfg.
Done when: ./tla check 04-03-ex1 prints "goal met: pass".
./tla check 04-03-ex1
Repo chapters/04-stuttering/exercises/03-ex1-only-by-crossing/
Hints
Hint 1
The trace's last step is a Board. The harbour master does not care about boarding. Which variable did that step change, and is that variable in SideRule's subscript?
Hint 2
The subscript is the list of variables a property is about. The harbour master's rule is about one variable only.
Hint 3 (nearly the answer)
Replace vars with the variable that says which bank the ferry is at. Cross stays in the box. MCFerry.cfg then fails, as it should, on the drifting ferry.
04-03-ex2Core8 min
04-03-ex2 · Hands off the channel
Tier: core. Time: about 8 minutes.
A television and its remote. The product manager's rule: the channel changes only when someone presses a channel button. Television.tla has every action; ChannelRule is a placeholder.
- Run
./tla check 04-03-ex2. All five cfgs pass, and nothing warns you. TLC printed a warning for a constant invariant in chapter 03. Is there one here? - Fill gap g1: write
ChannelRuleas an action property. Decide which action goes in the box and which variable goes in the subscript. - Run the check again.
Television.cfgmust pass: your rule may not forbid anything the real television does. The course wrote four more televisions, each with one extra step. InMCTelevision.cfg,MCTelevision.volume.cfgandMCTelevision.guide.cfga firmware bug changes the channel without a channel button, so your rule must fail on all three. InMCTelevision.sleep.cfga sleep timer switches the television off and turns the volume down, which leaves the channel alone, so your rule must pass. Leave theMCTelevisionfiles as they are. - Try one wrong answer on purpose, say with
varsas the subscript, and read which cfg catches it.
Hints: HINTS.md, under "04-03-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/04-stuttering/exercises/03-ex2-hands-off-the-channel/; 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 five cfgs must meet their goals: "goal met: pass" for Television.cfg and MCTelevision.sleep.cfg, and "goal met: property ChannelRule" for the other three.
Done when: ./tla check 04-03-ex2 prints "goal met: pass".
./tla check 04-03-ex2
Repo chapters/04-stuttering/exercises/03-ex2-hands-off-the-channel/
Hints
Hint 1
A property of the form [][A]_v has two choices in it: the action A that may change things, and the variables v it watches. Write the requirement as "v changes only by A".
Hint 2
Two buttons change the channel, and the rule names both. A step that keeps the channel where it was has nothing to answer for, whatever it does to power or volume.
Hint 3 (nearly the answer)
The subscript lists only the variables whose changes the product manager restricts. With vars as the subscript, Television.cfg fails on the first press of the power button. A box that says only "power and volume stay put" fails MCTelevision.guide.cfg, where a guide button jumps two channels.
04-03-ex3Stretch40 min · mastery path
04-03-ex3 · Drawbridge
Tier: stretch. Time: about 40 minutes.
A blank page, almost. Drawbridge.tla names the variables, the actions, the requirements and a witness; every body is a placeholder. You write the rest, including Spec and the cfg.
The bridge:
- The road bridge is down (cars cross) or up (boats pass). Road gates on both banks are open or closed.
- Boats arrive one at a time and wait, at any time there is room: up to
MaxBoatsof them. - The gates may close at any time they are open. They may open only while the bridge is down.
- The bridge lifts only with the gates closed and a boat waiting. Under a lifted bridge, boats pass one at a time. The bridge comes down on its own step: nothing else happens in that step.
RoadSafe, an invariant: whenever the bridge is up, the gates are closed.BridgeBehindGates, an action property: the bridge moves only while the gates are closed, before the move and after it.
Steps:
- Run
./tla check 04-03-ex3. Read the starter error: what does TLC say about aSpecthat isFALSE? - Fill gaps g1 to g11 in
Drawbridge.tla, then gap g12 inDrawbridge.cfg: the spec,MaxBoats = 2, both invariants and the action property. - Run the check until it passes with every action firing and the witness reached.
Drawbridge.cfgalso checks the box in yourSpec(its header says@subscript: all): if TLC reports that the subscript leaves out a variable, the goal is not met, because the box then lets that variable change at any time. TLC reads the box only fromSPECIFICATION Spec, so a cfg that namesINITandNEXTinstead does not meet the goal either. Write the subscript as a tuple of the variables, as lesson 04-03 does: a variable inside a record or a function in the subscript counts as left out. The run also checks six cfgs the course wrote. Five run yourInitandNextwith one extra step. InMCDrawbridge.cfgandMCDrawbridge.raise.cfga hurried operator lowers the bridge and opens the gates in one step (LowerAndOpen), or closes the gates and lifts the bridge in one step: yourBridgeBehindGatesmust fail on both. InMCDrawbridge.open.cfgthe bridge lifts with the gates open, and yourRoadSafemust fail. InMCDrawbridge.squeeze.cfga boat joins a full queue, and yourTypeOKmust fail. InMCDrawbridge.turnback.cfga waiting boat turns back, which breaks no rule, soTypeOK,RoadSafeandBridgeBehindGatesmust all hold. The sixth,MCDrawbridge.rules.cfg, adds no step: it checks yourInitand actions against the rules above, one formula per rule, and must pass. Leave theMCDrawbridgefiles as they are. - Now the question this exercise is about. Would
RoadSafecatchLowerAndOpen? Predict, then find out: in a copy, addLowerAndOpento your ownNextand run your own cfg. Why can the invariant not see it?
Hints: HINTS.md, under "04-03-ex3 hint 1" to "hint 3". The solution is in solutions/chapters/04-stuttering/exercises/03-ex3-drawbridge/; open it after you have a passing answer of your own.
If TLC finds no violation but an action never fires, a witness is not reached or the subscript of your box leaves out a variable, ./tla prints "goal not met" and exits with code 1.
All seven cfgs must meet their goals: "goal met: pass" for Drawbridge.cfg, MCDrawbridge.turnback.cfg and MCDrawbridge.rules.cfg, "goal met: property BridgeBehindGates" for MCDrawbridge.cfg and MCDrawbridge.raise.cfg, "goal met: violation RoadSafe" for MCDrawbridge.open.cfg, and "goal met: violation TypeOK" for MCDrawbridge.squeeze.cfg.
Done when: ./tla check 04-03-ex3 prints "goal met: pass".
./tla check 04-03-ex3
Repo chapters/04-stuttering/exercises/03-ex3-drawbridge/
Hints
Hint 1
Write TypeOK and Init first, make Spec the usual shape, and leave every action FALSE. Get a run with one state, then add one action at a time.
Hint 2
Every action must give a value to all three variables, directly or with UNCHANGED. Lower changes one variable and nothing else, even though opening the gates would save time.
Hint 3 (nearly the answer)
RoadSafe is an implication between two state conditions. BridgeBehindGates is a box whose action talks about the gates in both states of the step, before and after, and whose subscript is the variable whose moves you are restricting. MCDrawbridge.cfg checks it on an operator who hurries. The cfg needs SPECIFICATION, the constant, two INVARIANT lines and one PROPERTY line.
06Explain it
Open NOTES.md and answer the three prompts under 04-03, 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-03 check your explanation" in HINTS.md.
(Optional, for your own system.) In my-system/, name one variable that only one action may change, and write that rule as an action property with that variable as its subscript. Add it on a PROPERTY line and run your cfg.
After you writeCheck your explanation
A good answer: "ChannelRule says the channel changes only by ChannelUp or ChannelDown; its subscript is channel, so power and volume steps are stuttering steps for it. It passes on the real television and on the one with a sleep timer, and fails on the three with firmware bugs, where the channel jumps without a channel button. It assumes the two channel buttons are the only legitimate way to change the channel; a 'last channel' button would need adding. TLC checked models with three and four channels. Because the rule is written with the spec's own ChannelUp and ChannelDown, a bug inside those actions would pass it, and it does not require that anyone ever presses a button."
A weak answer: "I wrote [][ChannelUp \/ ChannelDown]_channel and all five cfgs met their goals." It names the formula without explaining the subscript or what the course's televisions tested.
Done?