02-03
Chapter 02 · Actions are formulas
Or means "may"
00Recall
Two cards from 02-02 (what an action says about a variable it does not mention, and how TLC reacts) and one from 02-01 on primed variables.
02-02-a from UNCHANGED and the frame
An action never mentions the variable alarm. In the logic, what may alarm be after a step of that action?
Show answer
Anything at all. An action constrains only what it says, so with no conjunct about alarm', every value makes it true. UNCHANGED alarm, which means alarm' = alarm, is how you say it stays.
02-02-b from UNCHANGED and the frame
What does TLC do with an action that leaves a variable without a value, and how do you spot the variable?
Show answer
It stops with "Successor state is not completely specified" and names the variable. In the trace's last state that variable is shown as null. TLC never tries every value.
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
specs/TrafficLight.tla is a light at a pedestrian crossing: the cars' light (red, green or yellow) and a walk signal that is on (walk = TRUE) or off. It is the TLA+ twin of the traffic light you finished in the state-graph builder in lesson 01-04: the same five actions, the same guards, the same reachable states. Each row of your builder table became a named action, a guard became a conjunct without a prime, an update became a conjunct with one, and "unchanged" became UNCHANGED. Its next-state action is the list of the five joined by \/, read "or":
Next is defined as
or Go
or Slow
or Stop
or Walk
or EndWalkIn the starting state the cars have red and the walk signal is off. Go (the cars get green) needs exactly that, and so does Walk (the pedestrians are told to walk). Both can take a step from there.
Commit a
In the state with the cars on red and the walk signal off, both Go and Walk can happen. What does the spec say happens next?
- TLC picks one of the two at random, so each run explores one of them.
- Go, because it comes first in Next, as in an if/else-if chain.
- Either one. Both are allowed steps, and TLC explores both next states.
Answer Either one. Both are allowed steps, and TLC explores both next states.
- If you picked A
- A random pick would make TLC a tester, sampling one future per run. In its default mode, the one this course uses until chapter 12, TLC is not one. It computes every next state the formula allows, so one run covers both.
- If you picked B
- That is how an if/else-if chain reads, and it is a different program. A disjunction has no order: a step is allowed when any disjunct is true of it.
specs/DialLock.tla is chapter 01's dial lock (three wheels showing 0, 1 or 2, code 2-0-1) with two changes: a wheel turns one click up or down, not only forward, and the wheels are jammed while the shackle is open. You may turn them only while the lock is shut, and once it is open you may shut it again.
Commit b
How many distinct states will TLC find for DialLock, starting with every wheel at 0 and the lock shut?
Your answer is a whole number.
Answer 28
- Exactly right
- Right: every combination of the wheels with the lock shut, plus one more.
- Within 10 %
- Within 10 %. If you were one short, you counted every combination of the wheels and forgot the state in which the lock is open.
- Under by more than half
- Under by more than half. You may have added the wheels' digits instead of multiplying: each wheel's setting combines with every setting of the other two.
- More than double
- More than double. You probably counted ways of getting to a state, such as different orders of turning the wheels. TLC counts each distinct state once, however many paths lead to it.
- Anything else
- If you doubled the number of wheel combinations, you counted an open lock at every combination. That was chapter 01's lock, whose wheels turn while it is open. Here they are jammed, so only one open state is reachable. Otherwise, count the combinations with the lock shut first, then ask where the lock can be open.
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 | walk | light | Steps (action → state) |
|---|---|---|---|
| initial 1 | FALSE | "red" | Go → 2; Walk → 3 |
| 2 | FALSE | "green" | Slow → 4 |
| 3 | TRUE | "red" | EndWalk → 1 |
| 4 | FALSE | "yellow" | Stop → 1 |
./tla check chapters/02-actions-are-formulas/specs/TrafficLight.cfg02-actions-are-formulas/specs/TrafficLight02-actions-are-formulas/specs/TrafficLight TrafficLight.cfg
parse ok
model check: no violation found in 4 distinct states (6 generated, depth 3)
expected outcome met: pass
coverage: all 5 actions fired
witness Walking reached (trace of 2 states)
witness Amber reached (trace of 3 states)Reveal for commit a
Either one. The starting state has two arrows, one labelled Go and one labelled Walk, and TLC followed both. Every one of the five actions fired, and the crossing reaches 4 states. Nobody chose; the spec allows both futures, and TLC kept both.
Then the lock. This lesson's graph is the crossing, so for the lock, read the count:
./tla check chapters/02-actions-are-formulas/specs/DialLock.cfg02-actions-are-formulas/specs/DialLock02-actions-are-formulas/specs/DialLock DialLock.cfg
parse ok
model check: no violation found in 28 distinct states (165 generated, depth 4)
expected outcome met: pass
coverage: all 5 actions fired
witness Opened reached (trace of 4 states)Reveal for commit b
TLC found 28 distinct states. Every combination of the wheels is reachable with the lock shut, and exactly one state has the lock open: the code, with the shackle up.
03Reason
If you picked "Go, because it comes first", you read Next the way you read an if/else-if chain: try the first branch, and stop there if it fits. If you picked "at random", you read it as a scheduler that rolls a die. Both pictures have TLA+ deciding something. It decides nothing.
Use pairs again. A pair of states is a step of A \/ B when it is a step of A, or of B, or of both. Judge three candidate steps from the starting state:
| New state | Go | Walk | Next |
|---|---|---|---|
| green, walk off | true | false | true |
| red, walk on | false | true | true |
| yellow, walk off | false | false | false |
Two different new states make Next true, so the spec allows both. That is nondeterminism: from one state, more than one next state is allowed. Read \/ in an action as "may": the cars may get green next, or the pedestrians may be told to walk next. The spec does not say which, because the real crossing does not either. A sensor, a button, a timer somewhere decides, and you chose to leave those out.
Why would you want a spec that refuses to choose? Because your claims must hold whichever way the world goes. SafeCrossing, "the walk signal is on only while the cars have red", is checked on every branch. A test runs one branch per run. TLC walks all of them.
This is also how you model a world that is not on your side. A network may deliver a message or lose it; a disk may write a block or fail; a user may press the button twice. You do not know which, so you write \/ and let TLC try every combination that the spec allows, in this finite model. The Core exercise does this for a lossy link, and chapter 08 builds a whole network out of it.
Write something before openingis nondeterminism the same as randomness?
No. Randomness comes with odds: a fair coin lands heads about half the time. Nondeterminism has no odds at all. It says only which next states are possible. A spec in which a message "may be lost" allows the behavior where every message is lost, however unlikely that seems, and TLC will find that behavior if it breaks a claim. When you need "eventually it gets through", you state it as a liveness property (chapter 09) and assume fairness to make it hold (chapter 10).
Write something before openingin the lab, how many arrows leave the state "green, walk off"? And the state "red, walk on"? Why fewer than from the start?
One each. From "green, walk off" only Slow can take a step; from "red, walk on" only EndWalk can, because Go needs the walk signal off and Walk needs it off too. The guards are what cut the choices down. In this crossing, nondeterminism lives exactly where more than one guard is true at once; a disjunction inside one action, next, is another source.
A disjunction can also sit inside one action. DialLock turns a wheel one click either way:
TurnA is defined as
and notopen
and or aprime = (a + 1) % 3
or aprime = (a + 2) % 3
and UNCHANGED tuple of b, c, open end tuple~ is read "not", and % is the remainder after division, so (a + 2) % 3 is one click down. Read the structure from the outside in: a shut lock, then one of two new values for a, then the other three variables unchanged. The guard and the frame are shared, so they sit outside the \/. If the two ways differed in what they leave alone, each disjunct would need its own frame, because each disjunct is a complete description of one way the step can go.
Write something before openingwhy exactly one reachable state with the lock open?
Open needs the wheels at 2, 0 and 1, and while the lock is open no wheel can turn: every Turn action starts with ~open. So the only open state is the code itself with the shackle up. The rest are the shut lock at every combination of three wheels with three digits each.
A worked example that is not in the specs. Init can be a disjunction too. Suppose the crossing could power up showing red or flashing yellow:
Init == \/ /\ light = "red" /\ walk = FALSE
\/ /\ light = "yellow" /\ walk = FALSEThen there are two initial states, and every claim must hold in the behaviors that start from either. Same rule: "or" means "may", and one TLC run checks the claims from both starts.
04In your editor
The specs are chapters/02-actions-are-formulas/specs/TrafficLight.tla and DialLock.tla. One command checks this lesson's exercises and every model it uses:
./tla check 02-03The crossing on its own prints:
02-actions-are-formulas/specs/TrafficLight02-actions-are-formulas/specs/TrafficLight TrafficLight.cfg
parse ok
model check: no violation found in 4 distinct states (6 generated, depth 3)
expected outcome met: pass
coverage: all 5 actions fired
witness Walking reached (trace of 2 states)
witness Amber reached (trace of 3 states)05Practice
Warm-up. A robot vacuum that can only roll forward, and gets stuck. Give Roll a second disjunct, with its own guard.
Core. A network link that may deliver a message or lose it. Finish the loss disjunct, frame and all.
02-03-ex1Warm-up5 min
02-03-ex1 · Back down the hall
Tier: warm-up. Time: about 5 minutes.
A robot vacuum rolls along a hallway of four tiles. Its one action, Roll, is a disjunction, but so far only one disjunct says anything: the robot may roll forward. The other is FALSE, which no step satisfies.
- Predict: what does TLC report, and in which state?
- Run
./tla check 02-03-ex1and read the first block,Hallway.cfg. Compare it with your prediction. The second block,Hallway.rollback.cfg, is the course's check that the robot may roll back one tile from every tile except 0; it fails on tile 1, which tells you the same story from another side. - Fill gap g1 with a second disjunct: when the robot is not on tile 0, it may roll back one tile. Give the disjunct its own guard, the way the first one has. Delete the
\* GAPmarker. - Run the check again. Then try your disjunct without its guard. Which claim fails, and on which tile?
Hallway.cfg also checks OneTileAtATime, a claim the course wrote about steps: every step moves the robot by one tile. Chapter 04 explains how such claims work; here you only read its verdict.
Hints: HINTS.md, under "02-03-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/02-actions-are-formulas/exercises/03-ex1-back-down-the-hall/; 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". The starter already ends "2 of 2 checks met their expected outcome", because both starter outcomes are the expected ones, so read the "goal met" lines, not the count.
Done when: ./tla check 02-03-ex1 prints "goal met: pass".
./tla check 02-03-ex1
Repo chapters/02-actions-are-formulas/exercises/03-ex1-back-down-the-hall/
Hints
Hint 1
A deadlock is a state where no step is possible. Which tile is the robot on in the last state, and which disjunct of Roll could take a step from there? The second block fails on tile 1 for a related reason.
Hint 2
The second disjunct has the same shape as the first: a guard that says when this way is possible, then the new tile. Moving back is possible everywhere except on one tile.
Hint 3 (nearly the answer)
Replace FALSE with a disjunct made of two conjuncts: the tile is above 0, and the new tile is the old tile minus one. Indent it like the first disjunct.
02-03-ex2Core8 min
02-03-ex2 · Lost in the post
Tier: core. Time: about 8 minutes.
A sender pushes three messages over a link that may deliver each one or lose it. Send has a disjunction inside it. The first disjunct, delivery, is complete. The second, loss, has been started and not finished.
- Run
./tla check 02-03-ex2. Read the error. Which variable does TLC name, and which branch ofSendwas it computing? The trace's last step tells you. - Finish the loss disjunct in gap g1. It needs a line for each variable it changes and a frame for each one it leaves alone.
sentis already handled outside the disjunction. Delete the\* GAPmarker. - Run the check again. You need both witnesses: a batch in which every message arrives and one in which every message is lost.
Then look at the initial state. How many different states can follow it now, and how many could follow it when Send could only deliver? Write both numbers and the reason in NOTES.md: what did the \/ add?
Hints: HINTS.md, under "02-03-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/02-actions-are-formulas/exercises/03-ex2-lost-in-the-post/; 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-03-ex2 prints "goal met: pass".
./tla check 02-03-ex2
Repo chapters/02-actions-are-formulas/exercises/03-ex2-lost-in-the-post/
Hints
Hint 1
The error names delivered, and the trace shows the branch that lost the message. That branch gives lost' a value. What does it say about delivered'?
Hint 2
Look at the delivery disjunct: one change and one UNCHANGED. The loss disjunct is its mirror image.
Hint 3 (nearly the answer)
Make the loss disjunct a two-line conjunction, indented like the delivery one: lost' is one more than lost, and delivered is unchanged.
Review (optional, mastery path). A teammate's junction lights, 02-review, are this lesson's idea with one guard missing. Read chapters/02-actions-are-formulas/review/REVIEW.md; the graph of their spec is in lesson 02-04, under PRACTICE.
06Explain it
Open NOTES.md and answer the three prompts under 02-03. Write about the lossy link 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: where in your own system does the outside world make a choice your code cannot, such as a lost message or a retry, and does anything you have written down list both outcomes?
Compare your answers with "02-03 check your explanation" in HINTS.md.
After you writeCheck your explanation
Good answer. "Send now says the network may deliver or lose each message: two disjuncts, each with one change and a frame for the counter it does not touch. Both are allowed, so TLC explores both, and the initial state has two successors instead of one. Accounted holds in every reachable state, both actions fire, and both witnesses are reached, so batches with every message delivered and with every message lost both happen. It assumes the network never duplicates a message; with duplication Accounted would be the wrong claim. TLC checked batches of three messages only."
Weak answer. "The \/ means TLC picks delivery or loss."
Why it is weak. "Picks" is the misconception the lesson is about. TLC does not choose a branch; it follows both. The answer also says nothing about frames, witnesses or limits.
Done?