02-04
Chapter 02 · Actions are formulas
Just enough logic
00Recall
Two cards from 02-03 (what \/ does inside an action, and why each disjunct must settle its variables) and one from 02-02 on unmentioned variables.
02-03-a from Or means "may"
What does \/ do inside an action?
Show answer
It lists the ways a step may go. A pair of states satisfies the action if it satisfies any disjunct, so when several are true the spec allows each, and TLC explores every one. Nothing is chosen.
02-03-b from Or means "may"
Why does each disjunct of an action need to settle every variable that its shared part does not?
Show answer
Each disjunct is a complete description of one way the step can go. A disjunct that leaves a variable without a value allows anything for it in the logic, and TLC stops on that branch with "not completely specified".
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.
Skip recall Recall is never required.
01Commit
specs/CheckFirstClock.tla is the hour clock with one extra conjunct in Tick, written first:
Tick is defined as
and hourprime not equal to hour
and hourprime = IF hour = 12 THEN 1 ELSE hour + 1The first line says the hand must move. Every tick moves it anyway, so in the logic this is the same clock as HourClock.
Commit a
TLC checks CheckFirstClock with the same cfg as HourClock. What does it report?
- The same result as HourClock: the extra conjunct is true of every tick, so it changes nothing at all.
- A deadlock: hour' # hour cannot be true before hour' has a value, so no tick is ever possible.
- An error at the first tick: TLC tries to evaluate hour' before anything has given it a value.
Answer An error at the first tick: TLC tries to evaluate hour' before anything has given it a value.
- If you picked A
- As logic, yes: and does not care about order, and the conjunct holds on every tick. TLC, though, reads the conjuncts left to right, and the first one needs a value that only the second one supplies.
- If you picked B
- A deadlock would be a fact about the system: a reachable state with no next state. This is a fact about TLC: it cannot evaluate the first conjunct at all, so it reports an error, not a property of the clock.
specs/GappyClock.tla writes Tick as an implication, read "implies", and forgets one case:
Tick is defined as
and hour not equal to 12 implies hourprime = hour + 1It starts at 1.
Commit b
GappyClock starts at 1 and ticks with hour # 12 => hour' = hour + 1. What does TLC report?
- A deadlock at 12: the implication cannot fire there, so the clock stops.
- It ticks up to 12, then stops with an error: nothing gives hour' a value there.
- No violation: at 12 the clock stays where it is.
Answer It ticks up to 12, then stops with an error: nothing gives hour' a value there.
- If you picked A
- You read => as "if hour is not 12, do the tick". An implication does nothing. At 12 its left side is false, which makes the whole implication true, for every possible new hour. That is not a stopped clock. It is a clock that may jump to any value at all, staying put included.
- If you picked C
- Nothing in the formula says the hour stays. At 12 the implication is true whatever hour' is, so in the logic the hour may become anything. TLC will not guess "unchanged" for you.
02Lab
Run the clock with the filter first, then the same two conjuncts in the other order:
./tla check chapters/02-actions-are-formulas/specs/CheckFirstClock.cfg
./tla check chapters/02-actions-are-formulas/specs/CheckSecondClock.cfg02-actions-are-formulas/specs/CheckFirstClock02-actions-are-formulas/specs/CheckFirstClock CheckFirstClock.cfg
parse ok
model check: TLC error: TLC threw an unexpected exception.
chapters/02-actions-are-formulas/specs/CheckFirstClock.tla:22:8: TLC threw an unexpected exception.
chapters/02-actions-are-formulas/specs/CheckFirstClock.tla:22: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 = 1202-actions-are-formulas/specs/CheckSecondClock02-actions-are-formulas/specs/CheckSecondClock CheckSecondClock.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
The filter-first clock stops at the first tick with a TLC error, and the raw log names the identifier hour, as the mirrored clock's did in 02-01. The summary's "unexpected exception" is the same wrapper you met there. Swap the two conjuncts and TLC finds no violation in 12 states, the same as HourClock. One formula, two orders, two verdicts.
Now the clock with a missing case:
./tla check chapters/02-actions-are-formulas/specs/GappyClock.cfg02-actions-are-formulas/specs/GappyClock02-actions-are-formulas/specs/GappyClock GappyClock.cfg
parse ok
model check: TLC error: Successor state is not completely specified by the next-state action. The following variable is not defined: hour.
expected outcome met: error not completely specified
trace (changed variables only; ./tla trace --full shows all):
1 Init hour = 1
2 Tick hour: 1 -> 2
3 Tick hour: 2 -> 3
4 Tick hour: 3 -> 4
5 Tick hour: 4 -> 5
6 Tick hour: 5 -> 6
7 Tick hour: 6 -> 7
8 Tick hour: 7 -> 8
9 Tick hour: 8 -> 9
10 Tick hour: 9 -> 10
11 Tick hour: 10 -> 11
12 Tick hour: 11 -> 12
13 Tick hour: 12 -> nullReveal for commit b
TLC ticks from 1 up to 12. Then it stops: "not completely specified", with hour shown as null in the last state of a trace of 13 states. At 12 the implication allowed every new hour, and TLC refused to pick one, exactly as it refused the forgetful alarm in 02-02.
03Reason
If you picked "a deadlock at 12", you read => as a guarded command: "if the hour is not 12, tick". That reading feels natural and it is backwards. To see why, stop reading formulas as things that run, and read them as filters.
Picture every candidate as a card: a state, when the formula is a claim about states, or a pair of states, when it is an action. A formula is a sieve. A card passes or it does not. Each connective builds a sieve from smaller ones:
| Written | Read | Passes a card when |
|---|---|---|
P /\ Q | "P and Q" | both pass it |
P \/ Q | "P or Q" | at least one passes it |
~P | "not P" | P rejects it |
P => Q | "P implies Q" | P rejects it, or Q passes it |
\E x \in S : P | "there exists x in S such that P" | P passes it for at least one x in S |
\A x \in S : P | "for all x in S, P" | P passes it for every x in S |
Two consequences follow. Adding a conjunct can only narrow the sieve; adding a disjunct can only widen it. And P => Q applies Q only to the cards that pass P: every card that fails P walks straight through.
Write something before openingGappyClock's Tick, at hour = 12. Which pairs pass it?
Every pair whose old hour is 12, whatever the new hour is: 12 to 1, 12 to 7, 12 to 99, 12 to a string. The left side hour # 12 rejects the card, so the implication passes it. A guard would have rejected those cards; an implication waves them through.
That is why implications are the natural shape for conditional requirements and the wrong shape for actions. A requirement says "whenever P, Q": RingsAtSeven == alarm = "ringing" => hour = 7 in 02-02, SafeCrossing in 02-03. Read it as a query: is there any reachable state where the alarm rings and the hour is not 7? States where the alarm is quiet are not part of the question, and the implication lets them pass. An action written as an implication lets through every step where its left side is false. specs/ImpliedClock.tla covers both cases, hour = 12 => hour' = 1 and hour # 12 => hour' = hour + 1, and TLC accepts it with 12 states. The pair of implications says what an IF ... THEN ... ELSE says.
Write something before openingDialLock's TypeOK says each wheel shows 0, 1 or 2 and the shackle is open or shut. How many states pass that filter, and how many can the lock reach?
Three digits on each of three wheels, times two shackle positions: 54 states pass TypeOK. The lock reaches only 28 of them, because the shackle opens only at the code and no wheel can turn while it is open. A filter tells you which states are acceptable, not which ones happen. That is why a passing TypeOK says so little, a point chapter 03's AI item makes with a lift.
Quantifiers are the same connectives, stretched over a set. For a finite S, \E x \in S : P is a long \/, one disjunct per element of S, and \A x \in S : P is a long /\. DialLock's TypeOK uses one: \A w \in {a, b, c} : w \in 0..2, "every wheel shows a digit". specs/DialLockExists.tla writes each turn as \E click \in {1, 2} : a' = (a + click) % 3 instead of two disjuncts, and TLC finds the same 28 states as DialLock.
Write something before openingrewrite TypeOK's \A w \in {a, b, c} : w \in 0..2 as a conjunction. Then: which states pass \E w \in {a, b, c} : w = 2?
a \in 0..2 /\ b \in 0..2 /\ c \in 0..2: one conjunct per wheel, with w replaced by each in turn. The \E version unrolls to a = 2 \/ b = 2 \/ c = 2, so it passes every state in which at least one wheel shows 2, whatever the others show and whether the lock is open or shut. Read a quantifier as a query over a set: \A asks whether every element passes, \E whether any one does.
Now the TLC half, which you have met twice already. TLC does not hold every candidate pair up to the sieve, because there are infinitely many. It reads an action left to right, looking inside \/, \E, IF and => until it reaches a formula about x'. There, x' = e, x' \in S or UNCHANGED x, with x' still lacking a value, produces candidates: one value, or one per element of S. Anything else that reads x', such as x' > 0 or e = x', can only test a candidate, so it must come after something has produced one. That is why ImpliedClock runs: TLC evaluates hour = 12, and only where it is true does it reach hour' = 1.
Write something before openingspecs/RangeFirstClock.tla puts hour' \in 1..12 before the equation. Will TLC accept it?
Yes. hour' \in 1..12 is a producer: TLC tries each of the twelve values in turn, and the equation after it keeps the one that fits. TLC finds no violation in 12 states. Membership produces; comparison tests.
Notice that the rule is about TLC, not about meaning. Two people can write the same action in two orders and mean exactly the same thing; only one of them will see a verdict. So here is the working rule for this course: in every action, give each primed variable its value with x' = e, x' \in S or UNCHANGED x before any line that tests it. Write conditional requirements ("whenever P, Q") as implications; write actions as guards and values. When TLC says "undefined" about a variable you declared, look for a test that came before its producer.
04In your editor
The specs are in chapters/02-actions-are-formulas/specs/. One command checks this lesson's exercises and every spec it names:
./tla check 02-04The clock with the filter in the right place prints:
02-actions-are-formulas/specs/CheckSecondClock02-actions-are-formulas/specs/CheckSecondClock CheckSecondClock.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)05Practice
Warm-up. A scoreboard whose filter comes first. Put it second.
Core. Write the dial lock's requirement as an implication, and watch three broken locks fail it.
Stretch. A car park with a barrier and a "full" sign: three actions from prose, nothing to copy.
02-04-ex1Warm-up5 min
02-04-ex1 · Filter second
Tier: warm-up. Time: about 5 minutes.
Point in Scoreboard.tla is two conjuncts: the new score is never above 5, and the new score is one more than the old. As a formula, it is right. TLC cannot compute it.
- Run
./tla check 02-04-ex1. The summary's "unexpected exception" is the wrapper from 02-01. In your terminal, open the file on theraw log:line and find the sentence that names an identifier. Which conjunct was TLC evaluating when it stopped? - Read the two conjuncts as filters on candidate next states. Which one produces a candidate, and which one only accepts or rejects one?
- Fill gap g1: keep both conjuncts, in the order TLC needs. Delete both
\* GAPmarkers. - Run the check again. Then delete the
score' <= 5line and run it once more. What does the second conjunct do at score 5, and what happens without it? Put the line back.
The cfg also checks OnePointAtATime, a claim the course wrote about steps: the score goes up one point at a time or is cleared to 0. You only read its verdict.
Hints: HINTS.md, under "02-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/02-actions-are-formulas/exercises/04-ex1-filter-second/; 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-04-ex1 prints "goal met: pass".
./tla check 02-04-ex1
Repo chapters/02-actions-are-formulas/exercises/04-ex1-filter-second/
Hints
Hint 1
Read the two conjuncts as filters. One of them can produce a value for score'; the other can only accept or reject a value that already exists.
Hint 2
TLC works through the conjuncts from left to right. The conjunct that produces score' has to come before any conjunct that tests it.
Hint 3 (nearly the answer)
Swap the two lines inside the gap, keep both of them, and delete both \* GAP markers. The equation goes first; the bound goes second.
02-04-ex2Core8 min
02-04-ex2 · Only at the code
Tier: core. Time: about 8 minutes.
DialLock.tla is the lock from lesson 02-03, with one requirement still to write: whenever the lock is open, the wheels show the code, 2-0-1. The placeholder is TRUE, a filter that lets every state through.
- Run
./tla check 02-04-ex2. Five blocks appear. The first is the lesson's lock. The other four are the course's broken locks inMCDialLock.tla: a lock forced open at every wrong combination, then three locks with one worn wheel each, which also open when the other two wheels are right. Read the TLC warning in the first block. What does TLC think of a requirement that is nothing butTRUE? - Fill gap g1 in
DialLock.tla: defineOpenOnlyAtCodeas a filter on states. Decide which connective says "whenever the lock is open", and which one joins the three wheel conditions. Delete the\* GAPmarker. - Run the check again. Your requirement must pass in the lesson's lock, reject every open state of the forced lock (so that block passes), and fail in each worn lock.
Before you run step 3, predict what happens if you join the wheel conditions with \/ instead of /\. Then try it. Which locks let it through, and why?
Afterwards, in NOTES.md: in words, your right-hand side says every wheel shows its digit, and the \/ version says some wheel does. Which of \A and \E matches each? Lesson 02-04 unrolls both.
Leave MCDialLock.tla, its cfgs and DialLock.cfg as they are: they are the course's test of your requirement, not part of your answer.
Hints: HINTS.md, under "02-04-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/02-actions-are-formulas/exercises/04-ex2-only-at-the-code/; open it after you have a passing answer of your own.
You are done only when all five blocks meet their goals: "goal met: pass" for DialLock.cfg and MCDialLock.forced.cfg, the line below for each of the three MCDialLock.worn*.cfg, and "goal not met" nowhere. A partial answer can print the line below for one worn lock and still miss the others. The starter already ends "5 of 5 checks met their expected outcome", because all five starter outcomes are the expected ones, so read the "goal met" lines, not the count.
Done when: ./tla check 02-04-ex2 prints "goal met: violation OpenOnlyAtCode".
./tla check 02-04-ex2
Repo chapters/02-actions-are-formulas/exercises/04-ex2-only-at-the-code/
Hints
Hint 1
The requirement is "whenever the lock is open, ...". Which connective reads "whenever P, Q"?
Hint 2
Use =>. Its left side is the condition under which the requirement applies; its right side says all three wheels show their digits of the code. "All three" needs /\, not \/.
Hint 3 (nearly the answer)
Write an implication whose left side is open and whose right side is a parenthesised conjunction of three equations, one per wheel: 2, 0 and 1. Then check that it fails in all three worn locks and passes in the forced lock.
02-04-ex3Stretch25 min · mastery path
02-04-ex3 · Car park
Tier: stretch. Time: about 25 minutes. Off the core clock: this is on the mastery path.
Three actions, written from prose, with nothing to copy. CarPark.tla gives you the variables, TypeOK, Init, Next, the requirements and a witness. The three action bodies are FALSE.
The car park:
- Arrive. A car pulls up at the entrance. If the sign says "free", the barrier lifts. Nothing else changes.
- Enter. With the barrier up, the car drives in: one more car inside, the barrier comes down, and the sign says "full" exactly when the car park now holds three cars, "free" otherwise.
- Leave. A car leaves by the exit, which has no barrier. One car fewer, and the sign says "free". The entry barrier stays as it is.
The requirements, already in the module: a "full" sign means three cars inside, a "free" sign means there is room, and the barrier is never up when there is no room. The cfg also checks two claims the course wrote: OneCarAtATime, about steps (the count changes by at most one car, a car comes in only through a raised barrier, and a car leaving does not move the entry barrier), and CanLeave (whenever a car is inside, one can leave).
- Run
./tla check 02-04-ex3. With every actionFALSE, what does TLC report, and in which state? - Write the three actions, one gap at a time, as bulleted
/\lists. For each one, name the guards first, then give every variable exactly one line: its new value, orUNCHANGED. Delete each gap's\* GAPmarker as you finish it. - Run the check after each action. When TLC reports a violation, read the trace's last two states and ask which action let that step through.
Enter needs a value for the sign that depends on the new number of cars. IF ... THEN ... ELSE ... is one way; two disjuncts, one for "now full" and one for "still room", is another. Try both if you have time: TLC should find the same states.
Hints: HINTS.md, under "02-04-ex3 hint 1" to "hint 3". The solution is in solutions/chapters/02-actions-are-formulas/exercises/04-ex3-car-park/; 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-04-ex3 prints "goal met: pass".
./tla check 02-04-ex3
Repo chapters/02-actions-are-formulas/exercises/04-ex3-car-park/
Hints
Hint 1
Do one action at a time and run the check after each. For each action, list the guards from the prose first, then write one line per variable: its new value or UNCHANGED.
Hint 2
Arrive changes only the barrier. Enter changes all three variables. Leave changes two and leaves the barrier alone. When you write the new sign in Enter, remember that a value on the right of = must be computed from old values.
Hint 3 (nearly the answer)
In Enter, the sign's new value depends on whether the old count plus one equals three; the README names two ways to write that. Leave needs the guard that there is a car to leave, and sets the sign to "free".
Review (optional, mastery path). A teammate's junction lights, 02-review, pass every check they ran. Their pull request states a rule: a road may get green only while the other road has red. That rule is about steps. The state claim it implies is "at least one road shows red": read that as a filter on states, then find a state in this graph that the filter rejects, and the arrow that leads into it. The task is in chapters/02-actions-are-formulas/review/REVIEW.md.
TLC found no violation
This is a teaching view. Real checking happens with TLC on your machine.
Text view: every state and step
| State | ew | ns | Steps (action → state) |
|---|---|---|---|
| initial 1 | "red" | "red" | NSGo → 2; EWGo → 3 |
| 2 | "red" | "green" | NSSlow → 4; EWGo → 5 |
| 3 | "green" | "red" | EWSlow → 6 |
| 4 | "red" | "yellow" | NSStop → 1; EWGo → 7 |
| 5 | "green" | "green" | NSSlow → 7; EWSlow → 8 |
| 6 | "yellow" | "red" | EWStop → 1 |
| 7 | "green" | "yellow" | NSStop → 3; EWSlow → 9 |
| 8 | "yellow" | "green" | NSSlow → 9; EWStop → 2 |
| 9 | "yellow" | "yellow" | NSStop → 6; EWStop → 4 |
AI item (optional, mastery path). 02-ai1 gives you eight short specs in the style of an assistant's output (provenance: curated, hand-written), with a confident message that they all run. Six carry this chapter's mistakes or their cousins: = read as assignment, a missing frame, an operator nobody defined. Read them first, then let SANY and TLC settle it. The task is in chapters/02-actions-are-formulas/ai/1-spot-the-hallucination/README.md.
06Explain it
Open NOTES.md and answer the three prompts under 02-04. Write about the requirement you wrote 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-04 check your explanation" in HINTS.md.
After you writeCheck your explanation
Good answer. "My OpenOnlyAtCode is an implication from open to all three wheels at their digits of the code, the three wheel conditions joined by 'and'. It filters states: every shut-lock state passes, and an open state passes only at the code. It holds in the lesson's lock, fails in each worn lock, and rejects every open state of the forced lock, so it is neither too weak nor too strong for these locks. It assumes the code is fixed at 2-0-1; a lock whose code can change needs a variable for the code. TLC checked locks with three digits per wheel, not ten."
Weak answer. "I wrote that the lock is open at the code, and it passed."
Why it is weak. "The lock is open at the code" is a different claim (it reads like the implication turned around, from the code to an open lock), and passing the lesson's lock alone says nothing: TRUE passes it too. The worn and forced locks are the evidence.
Done?