02-01
00Recall
Three cards from earlier chapters: why a behavior never ends, what separates a state from an interleaving, and the difference between a TLC error and a violation. You will meet several TLC errors in this chapter, so the last one matters. If you work in the terminal, the same cards are in drills/CARDS.md.
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.
00-02-a from Every interleaving
What is the difference between a state and an interleaving?
Show answer
A state is a snapshot of every variable's value. An interleaving is one order of all the steps of a run; running it passes through a sequence of states. Many interleavings can pass through the same state.
00-03-b from Your first TLC run
What is the difference between a TLC error and a violation?
Show answer
A violation means TLC found a reachable state where your invariant is false, and it shows a trace to it (so does a deadlock, a reachable state where no step is possible). An error means TLC could not evaluate, parse or configure something, so there is no verdict.
Skip recall Recall is never required.
01Commit
In chapter 01 you described systems with a table of guards and updates. From here on you write them in TLA+. This is the line that makes a clock tick in specs/HourClock.tla, a clock face that shows only the hour:
and hourprime = IF hour = 12 THEN 1 ELSE hour + 1The tick mark after hour is a prime. hour' is read "hour prime". Strip away the wrap from 12 to 1, and the heart of the line is hour' = hour + 1.
Commit a
In TLA+, what is hour' = hour + 1?
- An instruction, run once per step: compute hour + 1 from the current value, then store the result in hour.
- A claim about a pair of states, an old one and a new one: true when the new hour is one more than the old.
- A contradiction, false in every state: no number equals itself plus one, so nothing can ever satisfy it.
Answer A claim about a pair of states, an old one and a new one: true when the new hour is one more than the old.
- If you picked A
- In C, Python or Java, = with a variable on the left means "store". TLA+ has no store. The formula does not do anything; it describes which steps count as a tick.
- If you picked C
- You read hour' as hour. They are different values: hour is the hour now, hour' is the hour after the step. 3 = 3 + 1 is false, but "the new hour is 4 and the old one was 3" is true.
Now the same equation, mirrored. specs/MirroredClock.tla is HourClock with one line changed:
and (IF hour = 12 THEN 1 ELSE hour + 1) = hourprimeIn arithmetic, a = b and b = a say the same thing.
Commit b
TLC checks MirroredClock with the same cfg as HourClock. What happens?
- The same result as HourClock: a = b means b = a, so it is the same clock.
- SANY rejects the module: a primed variable must be on the left of =.
- The module parses. TLC stops with an error at the first step it tries to compute.
Answer The module parses. TLC stops with an error at the first step it tries to compute.
- If you picked A
- As mathematics, you are right: it is the same clock. But TLC does not solve equations. It computes next states with a simple rule about where the primed variable stands, and this line breaks the rule.
- If you picked B
- If = were assignment, the mirror image would be a syntax error. It is not: the module is well-formed TLA+ and SANY accepts it. The trouble comes later, when TLC tries to compute with it.
02Lab
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 | Steps (action → state) |
|---|---|---|
| initial 1 | 12 | Tick → 2 |
| 2 | 1 | Tick → 3 |
| 3 | 2 | Tick → 4 |
| 4 | 3 | Tick → 5 |
| 5 | 4 | Tick → 6 |
| 6 | 5 | Tick → 7 |
| 7 | 6 | Tick → 8 |
| 8 | 7 | Tick → 9 |
| 9 | 8 | Tick → 10 |
| 10 | 9 | Tick → 11 |
| 11 | 10 | Tick → 12 |
| 12 | 11 | Tick → 1 |
Click a state to see its value, then follow its arrow. Each arrow is one pair of states: an old hour and a new hour. Now run the clock in the terminal:
./tla check chapters/02-actions-are-formulas/specs/HourClock.cfg02-actions-are-formulas/specs/HourClock02-actions-are-formulas/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 ElevenOClock reached (trace of 12 states)Reveal for commit a
A claim about a pair of states. The graph above is that claim drawn out: TLC found 12 states, and each arrow joins an old state to a new state for which the formula is true. Nothing is stored anywhere. TLC asked "which new states make this true?" and drew an arrow to each.
Then the mirror image:
./tla check chapters/02-actions-are-formulas/specs/MirroredClock.cfg02-actions-are-formulas/specs/MirroredClock02-actions-are-formulas/specs/MirroredClock MirroredClock.cfg
parse ok
model check: TLC error: TLC threw an unexpected exception.
chapters/02-actions-are-formulas/specs/MirroredClock.tla:20:46: TLC threw an unexpected exception.
chapters/02-actions-are-formulas/specs/MirroredClock.tla:20:5: TLC threw an unexpected exception.
expected outcome met: error either undefined or not an operator
trace (changed variables only; ./tla trace --full shows all):
1 Init hour = 12Reveal for commit b
SANY said "parse ok". Then TLC computed the initial state, tried the first tick, and stopped with an error. In your terminal, open the file on the raw log: line: TLC says "the identifier hour is either undefined or not an operator", and points at hour' on the right of the equation. The summary line in your terminal says "TLC threw an unexpected exception". That is how TLC reports an error it hit while evaluating a formula, not a crash: the reason is always in the raw log. Same formula, different verdict. The rest of this lesson explains why both facts are true at once.
03Reason
If you picked "an instruction", you read = the way C, Python or Java use it: the thing on the left receives the value on the right. That habit will cost you in TLA+, so take it apart slowly. Habits from code can also put := or != into a spec, whether a person or a language model wrote it. TLA+ formulas give neither a meaning: := is a spare operator symbol that no standard module defines, and != is not TLA+ at all (not-equal is #). 02-ai1 has both.
Start from chapter 01. A state gives every variable a value. In HourClock there is one variable, so a state is one number: hour = 3, say. A step is a pair of states, an old one and a new one. To write a claim about a step, you need names for the variable's value in both states. TLA+ uses hour for the old state and hour' for the new one.
So hour' = hour + 1 is not a command. It is an equation between two numbers, and you cannot say whether it is true until somebody hands you a pair of states. Hand it one:
| Old state | New state | hour' = hour + 1 |
|---|---|---|
hour = 3 | hour = 4 | true |
hour = 3 | hour = 5 | false |
hour = 3 | hour = 3 | false |
hour = 12 | hour = 1 | false |
The last row is why HourClock writes IF hour = 12 THEN 1 ELSE hour + 1: without the IF, the step from 12 to 1 would not count as a tick. Formulas that mention both hour and hour' are the ones this course calls actions. Hand one a pair of states and it says yes or no, so each action picks out a set of pairs: the steps it allows.
Write something before openingis the pair "old hour = 12, new hour = 13" a step of Tick? Does it matter that 13 is not on any clock face?
It is not a step of Tick: with hour = 12, the IF gives 1, and 13 is not 1. The clock face plays no part in the answer. The action alone decides which pairs are steps. If Tick had said hour' = hour + 1 with no IF, the pair 12 to 13 would be a step, and the spec would describe a clock that runs off its own face. Claims such as TypeOK only report on it afterwards.
Now look at the lab again. Init picks the states a behavior may start in; here, hour = 12. Next is the action that says which steps the system may take. The graph is what you get by starting from Init and adding an arrow for every pair that makes Next true, again and again, until no new state appears. Nothing in it was "executed". It is the set of pairs, drawn.
That settles the meaning. It does not settle what TLC does, and the mirrored clock shows the two can part ways. TLC cannot test every possible new state against the formula: there are infinitely many values hour' could have. So TLC uses a shortcut. It reads the action from left to right, and when it meets hour' = e with hour' still lacking a value, it does not test the equation. It computes e and gives hour' that value. That is the one place where TLC treats = a little like assignment, and it works only with the primed variable alone on the left.
Write something before openingthe module declares hour. Why does TLC call it undefined?
TLC was not evaluating hour; it was evaluating hour', the new state's value, which no conjunct had given yet. The message names the variable without its prime. Read "undefined" here as "has no value in the state TLC is building".
So both of your instincts were half right. The mathematician who said "same clock" was right about the meaning. The programmer who said "the prime goes on the left" was right about TLC. Keep both: write the meaning you intend, in the order TLC can compute. Lesson 02-04 comes back to this with filters.
A worked example of reading pairs. Take the action hour' = hour, with no IF and no + 1. Which pairs is it true of? Every pair where the hour does not change: 3 to 3, 12 to 12. It describes a step in which nothing happens. It is a valid action, and chapter 04 shows that every TLA+ spec allows such steps on purpose.
04In your editor
The spec is chapters/02-actions-are-formulas/specs/HourClock.tla. One command checks this lesson's exercises and the specs it uses:
./tla check 02-01The working clock on its own prints:
02-actions-are-formulas/specs/HourClock02-actions-are-formulas/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 ElevenOClock reached (trace of 12 states)You do not write cfg files in this chapter; chapter 03 teaches them. Each exercise comes with one. Read its verdict line and, for errors, the raw log.
05Practice
Warm-up. An egg timer whose countdown is written the mirrored way. Turn it around.
Core. Two cups, three sugar lumps, and a swap written like a programmer's swap. Find out what it does to the lumps, then fix it.
02-01-ex1Warm-up5 min
02-01-ex1 · Turn it around
Tier: warm-up. Time: about 5 minutes.
EggTimer.tla is a kitchen timer. Its Countdown action says, correctly, that the new value of left is one less than the old value. It says it as left - 1 = left', the mirror image of the way HourClock writes Tick.
- Predict before you run: will TLC accept the action, report a violation, or stop with an error? Write your answer down.
- Run
./tla check 02-01-ex1. The summary says "TLC error: TLC threw an unexpected exception.", the wrapper from lesson 02-01, reveal b. In your terminal, open the file named on theraw log:line and find the sentence that names an identifier. Which identifier, and why does TLC call it undefined when the module plainly declares it? - Fill gap g1: write the same equation with
left'on the left of=. Delete the\* GAPmarker. - Run the check again.
The cfg also checks OneMinuteAtATime, a claim the course wrote about steps: the timer only counts down by one minute, or winds back to 3 once it has run out. You only read its verdict.
You changed the order of one equation and nothing else. Logically the action is the same. Only TLC's way of computing it changed.
Hints: HINTS.md, under "02-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/02-actions-are-formulas/exercises/01-ex1-turn-it-around/; 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-01-ex1 prints "goal met: pass".
./tla check 02-01-ex1
Repo chapters/02-actions-are-formulas/exercises/01-ex1-turn-it-around/
Hints
Hint 1
The module parsed: SANY said "parse ok". So the trouble is in how TLC computes the next state, not in the text. Look at where left' stands in the equation.
Hint 2
TLC gives a primed variable its value only from left' = e with left' alone on the left, from left' \in S, or from UNCHANGED left. Anything else that reads left' is treated as a test of a value that must already exist.
Hint 3 (nearly the answer)
Keep the same two sides of the equation and swap them, so that left' is on the left and the old value minus one is on the right. Compare with left' = 3 in Rewind.
02-01-ex2Core8 min
02-01-ex2 · Swap the cups
Tier: core. Time: about 8 minutes.
Two cups stand on a tray and share three sugar lumps. Swap should exchange the cups' places. Whoever wrote it thought like a programmer swapping two variables: first the left gets the right's value, then the right gets the left's.
- Read
SwapinTeaCups.tlaand predict: does TLC report a violation ofNoneLost("there are always three lumps"), and if so, how many lumps does the bad state hold? - Run
./tla check 02-01-ex2and read the first block's trace,TeaCups.cfg. Find the step where the lumps changed in number. Which primed variable did the second line ofSwapread, and what value did that have? - Fill gap g1 so that each new value is the other position's old value. Delete both
\* GAPmarkers. - Run the check again. Then, as an experiment, swap the order of your two lines and run it once more. Does the result change? Why not, this time?
The witness AllLeft matters: lumps only ever move to the right, so getting all three into the left-hand cup needs at least one swap. The cfg also checks SwapsOrMoves, a claim the course wrote about steps: every step either drops one lump to the right or really swaps the cups. Chapter 04 explains how such claims work; here you only read its verdict. The second block, TeaCups.four.cfg, runs the same cups with four lumps: an answer that leans on the number 3 instead of the old values fails there. It also checks CanSwap: whenever the cups hold different numbers of lumps, a step can really swap them. A guard the comment does not ask for, such as swapping only when the right cup holds more, fails it. The starter fails it in the initial state.
Hints: HINTS.md, under "02-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/02-actions-are-formulas/exercises/01-ex2-swap-the-cups/; 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-01-ex2 prints "goal met: pass".
./tla check 02-01-ex2
Repo chapters/02-actions-are-formulas/exercises/01-ex2-swap-the-cups/
Hints
Hint 1
In the first block's trace, the cups hold four lumps after Swap. Ask which value the second line of Swap copied: the right cup's old value, or something else?
Hint 2
Inside one action, every unprimed variable is its value in the old state, and the old state does not change while you read the action. You never need a temporary variable to remember an old value.
Hint 3 (nearly the answer)
Each new value is the other position's old value: the new left is the old right, and the new right is the old left. Neither line should mention a primed variable on its right-hand side.
06Explain it
Open NOTES.md and answer the three prompts under 02-01. Write about the cups you fixed in 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 "02-01 check your explanation" in HINTS.md.
After you writeCheck your explanation
Good answer. "My Swap gives each cup's new value from the other cup's old value, and nothing on a right-hand side is primed. It is correct because an action is one claim about a pair of states, so both lines read the same old state; TLC finds no violation of NoneLost, the course's step check SwapsOrMoves or its CanSwap, both actions fire, and AllLeft is reached, which needs at least one swap. It breaks if the second line copies left' instead of left, as the starter's did: both cups then hold the right cup's old count. TLC checked a tray of three lumps starting at one and two, and one of four starting at one and three; it did not check other totals or other starting splits."
Weak answer. "I fixed the swap and TLC passed."
Why it is weak. It names no reason the swap is right, no assumption, and no limit of the check. "TLC passed" could describe the starter with its invariant deleted.
Done?