02-02
Chapter 02 · Actions are formulas
UNCHANGED and the frame
00Recall
Two cards from 02-01 (what a primed variable stands for, and why TLC wants it on the left) and one from chapter 01 on why a behavior never ends.
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.
02-01-b from Primes
hour' = e and e = hour' are the same formula. Why does TLC accept only the first?
Show answer
TLC reads an action left to right and gives hour' a value only when it reaches hour' = e, hour' \in S or UNCHANGED hour with hour' still unset. Any earlier use of hour', such as e = hour', or a test like hour' > 0 written first, is evaluated, finds no value, and stops TLC with an error. In the logic the order does not matter.
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
specs/AlarmClock.tla adds an alarm to the hour clock. The alarm is "off", "on" (set for 7) or "ringing". It may start ringing while the hand is at 7 and the alarm is on, and then it rings until someone stops it. Nothing forces it to ring: the hand may also move on silently. This is its Tick:
Tick is defined as
and alarm not equal to "ringing"
and hourprime = IF hour = 12 THEN 1 ELSE hour + 1
and UNCHANGED alarm# is read "is not equal to". specs/ForgetfulAlarm.tla is the same clock with the last line of Tick deleted. Nothing else changed.
Commit a
In ForgetfulAlarm, Tick no longer mentions alarm. As a formula, what does it allow the alarm to be after a tick?
- Whatever it was before. An action leaves alone every variable it does not mention, as a program would.
- Anything at all. No conjunct says anything about alarm', so every value makes the action true.
- Nothing: an action that does not mention every variable is not a well-formed formula.
Answer Anything at all. No conjunct says anything about alarm', so every value makes the action true.
- If you picked A
- That is how a program works: a statement that does not touch a variable leaves it alone. An action is not a list of changes. It is a claim about two states, and it claims nothing about the new alarm.
- If you picked C
- An action may mention as few variables as it likes. It is still a formula, true or false of each pair of states. What a missing variable changes is which pairs make it true.
Commit b
TLC checks ForgetfulAlarm against the same two claims as AlarmClock: values in range, and the alarm rings only at 7. What does it report?
- No violation: the alarm stays as it was, as in AlarmClock.
- It tries every value for the alarm, and reports that the alarm can ring at the wrong hour.
- It stops with an error at the first tick it computes.
Answer It stops with an error at the first tick it computes.
- If you picked A
- TLC does not fill in what you left out. If it guessed "unchanged" for you, it would be checking a spec you did not write.
- If you picked B
- That is what the formula means, so it is a reasonable guess. But "every value" is not a finite list TLC can try: it includes numbers, strings, sets and anything else. TLC refuses instead of picking a list for you.
02Lab
Run the forgetful clock first:
./tla check chapters/02-actions-are-formulas/specs/ForgetfulAlarm.cfg02-actions-are-formulas/specs/ForgetfulAlarm02-actions-are-formulas/specs/ForgetfulAlarm ForgetfulAlarm.cfg
parse ok
model check: TLC error: Successor state is not completely specified by action Tick of the next-state relation. The following variable is not defined: alarm.
expected outcome met: error not completely specified
trace (changed variables only; ./tla trace --full shows all):
1 Init hour = 12, alarm = "off"
2 Tick hour: 12 -> 1, alarm: "off" -> nullReveal for commit b
TLC stops at the first Tick it computes: "Successor state is not completely specified by action Tick". It names the variable, alarm, and the trace shows the new alarm as null, TLC's marker for "no value". It did not assume the alarm stayed put, and it did not try every value.
TLC refused to show you what the formula means, so here is the closest picture it can draw. specs/LooseAlarm.tla replaces UNCHANGED alarm in Tick with "the new alarm is any of its three values": alarm' \in {"off", "on", "ringing"}, read "alarm prime is an element of" that set. Everything else is AlarmClock, whose graph has the ringing state only at the hour the alarm is set for.
TLC found no violation
This is a teaching view. Real checking happens with TLC on your machine.
Text view: every state and step
| State | hour | alarm | Steps (action → state) |
|---|---|---|---|
| initial 1 | 12 | "off" | Tick → 2; Tick → 3; Tick → 4; Toggle → 5 |
| 2 | 1 | "off" | Tick → 6; Tick → 7; Tick → 8; Toggle → 3 |
| 3 | 1 | "on" | Tick → 6; Tick → 7; Tick → 8; Toggle → 2 |
| 4 | 1 | "ringing" | Stop → 3 |
| 5 | 12 | "on" | Tick → 2; Tick → 3; Tick → 4; Toggle → 1 |
| 6 | 2 | "off" | Tick → 9; Tick → 10; Tick → 11; Toggle → 7 |
| 7 | 2 | "on" | Tick → 9; Tick → 10; Tick → 11; Toggle → 6 |
| 8 | 2 | "ringing" | Stop → 7 |
| 9 | 3 | "off" | Tick → 12; Tick → 13; Tick → 14; Toggle → 10 |
| 10 | 3 | "on" | Tick → 12; Tick → 13; Tick → 14; Toggle → 9 |
| 11 | 3 | "ringing" | Stop → 10 |
| 12 | 4 | "off" | Tick → 15; Tick → 16; Tick → 17; Toggle → 13 |
| 13 | 4 | "on" | Tick → 15; Tick → 16; Tick → 17; Toggle → 12 |
| 14 | 4 | "ringing" | Stop → 13 |
| 15 | 5 | "off" | Tick → 18; Tick → 19; Tick → 20; Toggle → 16 |
| 16 | 5 | "on" | Tick → 18; Tick → 19; Tick → 20; Toggle → 15 |
| 17 | 5 | "ringing" | Stop → 16 |
| 18 | 6 | "off" | Tick → 21; Tick → 22; Tick → 23; Toggle → 19 |
| 19 | 6 | "on" | Tick → 21; Tick → 22; Tick → 23; Toggle → 18 |
| 20 | 6 | "ringing" | Stop → 19 |
| 21 | 7 | "off" | Tick → 24; Tick → 25; Tick → 26; Toggle → 22 |
| 22 | 7 | "on" | Tick → 24; Tick → 25; Tick → 26; Toggle → 21; Ring → 23 |
| 23 | 7 | "ringing" | Stop → 22 |
| 24 | 8 | "off" | Tick → 27; Tick → 28; Tick → 29; Toggle → 25 |
| 25 | 8 | "on" | Tick → 27; Tick → 28; Tick → 29; Toggle → 24 |
| 26 | 8 | "ringing" | Stop → 25 |
| 27 | 9 | "off" | Tick → 30; Tick → 31; Tick → 32; Toggle → 28 |
| 28 | 9 | "on" | Tick → 30; Tick → 31; Tick → 32; Toggle → 27 |
| 29 | 9 | "ringing" | Stop → 28 |
| 30 | 10 | "off" | Tick → 33; Tick → 34; Tick → 35; Toggle → 31 |
| 31 | 10 | "on" | Tick → 33; Tick → 34; Tick → 35; Toggle → 30 |
| 32 | 10 | "ringing" | Stop → 31 |
| 33 | 11 | "off" | Tick → 1; Tick → 5; Tick → 36; Toggle → 34 |
| 34 | 11 | "on" | Tick → 1; Tick → 5; Tick → 36; Toggle → 33 |
| 35 | 11 | "ringing" | Stop → 34 |
| 36 | 12 | "ringing" | Stop → 5 |
Reveal for commit a
Anything at all. In the loose clock, every tick may leave the alarm off, set it, or start it ringing, so each state where the alarm is not ringing has three Tick arrows instead of one. TLC reached 36 states against 25 for the real clock, and the extra ones include an alarm ringing at hours other than 7. The math allows even more than the loose clock shows: values that are not alarm settings at all.
Now check the loose clock against the requirement:
./tla check chapters/02-actions-are-formulas/specs/LooseAlarm.cfg02-actions-are-formulas/specs/LooseAlarm02-actions-are-formulas/specs/LooseAlarm LooseAlarm.cfg
parse ok
model check: invariant RingsAtSeven violated; trace has 2 states (1 steps); 4 distinct states
expected outcome met: violation RingsAtSeven
trace (changed variables only; ./tla trace --full shows all):
1 Init hour = 12, alarm = "off"
2 Tick hour: 12 -> 1, alarm: "off" -> "ringing"03Reason
If you picked "whatever it was before", you carried over a rule from code: a line of code that does not touch a variable leaves it alone. In a program that is true, because a program is a list of changes and everything not on the list stays put. An action is not a list of changes. It is a claim about a pair of states, and it constrains only what it says.
Check that with pairs, as in 02-01. Here is the forgetful Tick judged on three steps out of the state where the hour is twelve:
| Old state | New state | Forgetful Tick |
|---|---|---|
hour = 12, alarm = "off" | hour = 1, alarm = "off" | true |
hour = 12, alarm = "off" | hour = 1, alarm = "ringing" | true |
hour = 12, alarm = "off" | hour = 2, alarm = "off" | false |
The second row is the bug, stated as mathematics: a tick past 12 that starts the alarm ringing at 1 is a step this spec allows. Nothing in the formula rules it out.
Write something before openingwhat would you have to add to the forgetful Tick to make the second row false and keep the first row true?
A conjunct that ties the new alarm to the old one: alarm' = alarm. That is exactly what UNCHANGED alarm means. For several variables, UNCHANGED << b, c, open >> says each of them keeps its value, as DialLock's actions do in 02-03.
The conjuncts that say which variables an action leaves alone are called its frame condition, or its frame for short. An action that changes one variable still has to say, for each of the others, that it keeps its value. In return, every action states its whole effect on the world. Nothing happens off the page.
Write something before openinga language could have made "unchanged" the default for anything an action does not mention. Why might TLA+ refuse to?
Because an action is a formula, and a formula means what it says and nothing more. A default would be a hidden conjunct. It would also get in the way later. In chapter 06, two processes share one spec, and Next is "the first process takes a step, or the second one does". Each process's action must say that it leaves the other process's variables alone, and a reader can check that by looking at the action. With a hidden default, what the step does not touch would be the one fact about it you cannot see.
Write something before openingwhy does TLC refuse, instead of trying every value the way the math says?
Because "every value" has no end. TLC can only try the elements of a finite set, and nothing in the formula says which finite set you meant. It could guess the values TypeOK allows, but then it would be checking a spec you did not write and hiding your mistake. Refusing is the honest move.
Notice what kind of help that refusal is. It is not a check of your design. It is TLC's machinery hitting a limit, and the limit happens to fall exactly on the mistake this lesson is about. Write the loose version on purpose, with alarm' \in {"off", "on", "ringing"}, and TLC runs it without a word of complaint. Then only a claim about the system, such as RingsAtSeven, stands between you and an alarm that rings at 1.
Write something before openingthe loose clock's violation trace is as short as a trace can be while still taking a step. Why does TLC stop so soon?
The very first tick from 12 may set the alarm to "ringing", and in that state the hour is 1, so RingsAtSeven is false. An invariant fails in the first state where it is false, and TLC stops there: a trace of 2 states, 1 step.
A worked example with more variables. DialLock, in the next lesson, has four variables: three wheels and the shackle. Its TurnA turns the first wheel, so it gives a' a value and adds UNCHANGED << b, c, open >> for the rest. Delete c from that tuple and you have the forgetful clock's mistake again: in the logic, the third wheel could spin to anything every time you touch the first.
04In your editor
The specs are in chapters/02-actions-are-formulas/specs/: AlarmClock.tla, ForgetfulAlarm.tla and LooseAlarm.tla. One command checks this lesson's exercises and the specs it uses:
./tla check 02-02The real alarm clock on its own prints:
02-actions-are-formulas/specs/AlarmClock02-actions-are-formulas/specs/AlarmClock AlarmClock.cfg
parse ok
model check: no violation found in 25 distinct states (51 generated, depth 13)
expected outcome met: pass
coverage: all 4 actions fired
witness Ringing reached (trace of 10 states)When TLC says "not completely specified", read the variable it names, then the last line of the trace: the null shows which variable no conjunct gave a value. Then find the action (the message names it when Next is a disjunction of named actions; with a single action it says "the next-state action") and ask, for that variable, which line was supposed to say what happens to it.
05Practice
Warm-up. A front door whose Open forgets the deadbolt. TLC refuses; give it the missing line.
Core. A kettle with three variables and no way to pour. Write Pour with its guards and its frame.
02-02-ex1Warm-up5 min
02-02-ex1 · Mind the lock
Tier: warm-up. Time: about 5 minutes.
FrontDoor.tla is a door with a deadbolt. Three of its four actions say what happens to both variables. Open says what happens to the door and nothing about the bolt.
- Before you run: in the logic, what may the bolt do when the door opens? And what will TLC do?
- Run
./tla check 02-02-ex1. Read the error and the trace. What value does./tlashow forboltin the last state? - Fill gap g1 so that
Openleaves the bolt as it was. Delete the\* GAPmarker. - Run the check again.
Suppose TLC had not refused, and had let the bolt be anything. Which of the cfg's two claims would have failed, and in which state? Write one sentence in NOTES.md.
Hints: HINTS.md, under "02-02-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/02-actions-are-formulas/exercises/02-ex1-mind-the-lock/; 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 02-02-ex1 prints "goal met: pass".
./tla check 02-02-ex1
Repo chapters/02-actions-are-formulas/exercises/02-ex1-mind-the-lock/
Hints
Hint 1
Read the error: TLC names the variable it has no value for. Then read the last line of the trace, where that variable is null.
Hint 2
Opening a door does nothing to its bolt. TLA+ has a keyword for "this variable keeps its value"; the other actions in the file use it.
Hint 3 (nearly the answer)
Replace the TRUE placeholder with one UNCHANGED line naming the bolt, the way Close names it.
02-02-ex2Core8 min
02-02-ex2 · Pour a cup
Tier: core. Time: about 8 minutes.
Kettle.tla has three variables and four actions. Pour is still a placeholder, FALSE: an action that no pair of states satisfies.
- Run
./tla check 02-02-ex2. In the first block,Kettle.cfg, TLC reports a deadlock: a state with no possible next step. Read the last state of the trace. What would a person do next in that kitchen, and why can the spec not do it? - Write
Pourin gap g1 from its comment: pour out one cup, only while the kettle is switched off. Use bulleted/\lines, one conjunct per line. Before you run it, check your action against this question: for each of the three variables, which line says what its new value is? - Run the check again. Delete the
\* GAPmarker once your answer works.
The cfg also checks PourOneCup, a claim the course wrote about steps: the water goes down only one cup at a time, only while the kettle is switched off, and with the power and the temperature left as they were. If TLC reports it violated, read the last step of the trace. The second block, Kettle.pour.cfg, checks CanPour: whenever the kettle is off and holds water, a step can pour out one cup. A guard the comment does not ask for, such as pouring only hot water, fails it. The starter fails it after the first Fill.
If you see "not completely specified", read which variable TLC names. If you see a violation of NeverDry, read the trace: what was the kettle doing when the last cup went?
Hints: HINTS.md, under "02-02-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/02-actions-are-formulas/exercises/02-ex2-pour-a-cup/; 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.
You are done only when both blocks print the line below and neither prints "goal not met".
Done when: ./tla check 02-02-ex2 prints "goal met: pass".
./tla check 02-02-ex2
Repo chapters/02-actions-are-formulas/exercises/02-ex2-pour-a-cup/
Hints
Hint 1
Start from the comment above Pour. It names one condition outright (the kettle is switched off) and implies a second (there must be a cup to pour), and it names one change. Everything else in the kettle stays as it is.
Hint 2
Write the guards first (the kettle is off; there is a cup to pour), then the change to water, then one UNCHANGED line with a tuple << ... >> of the other two variables. If TLC says "not completely specified", read which variables it names.
Hint 3 (nearly the answer)
Pour has four conjuncts, shaped like SwitchOn: two guards (power is "off", water is above zero), water' one less than water, and UNCHANGED of the two variables it does not change, power and temp. A guard on power reads its old value; it does not say what power' is.
06Explain it
Open NOTES.md and answer the three prompts under 02-02. Write about the kettle you finished in the Core exercise.
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:If you answer the optional fourth prompt in NOTES.md, try this question: which fields does a handler in your own system leave alone, and where is that written down?
Compare your answers with "02-02 check your explanation" in HINTS.md.
After you writeCheck your explanation
Good answer. "Pour needs the kettle off and at least one cup in it, takes a cup out, and leaves power and temp unchanged. Each variable gets exactly one line fixing its new value, so TLC can compute every next state; TLC finds no violation of TypeOK, NeverDry, the course's step check PourOneCup or its CanPour, every action fires, and PouredItAll is reached. If the guard on power were missing, you could pour the last cup from a heating kettle and NeverDry would fail. TLC checked a two-cup kettle with two temperatures; it did not model spills, temperatures in between, or the mains."
Weak answer. "I added UNCHANGED so TLC would stop complaining."
Why it is weak. It treats the frame as a way to silence TLC rather than as a statement about the kettle. It does not say why those variables stay the same, or what the guards are for.
Done?