01-04
Chapter 01 · States, steps, behaviors
A spec is a filter
00Recall
Two cards from 01-03 on why a behavior never ends, and one from 01-02 on steps versus actions. This lesson puts states, steps and behaviors together.
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.
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.
01-02-a from Steps and the state graph
What is a step, and how is it different from an action?
Show answer
A step is a pair of states, before and after: one arrow in the state graph. An action is a rule that allows a set of steps, so one action can appear as many arrows.
Skip recall Recall is never required.
01Commit
A traffic light at a pedestrian crossing has two variables. light is the car light: red, green or yellow. walk is the pedestrian signal: on or off. The light starts red, with walk off. Write a state as light/walk, so the start is red/off.
| Action | Allowed when | What changes |
|---|---|---|
| Go | light is red and walk is off | light becomes green |
| Slow | light is green | light becomes yellow |
| Stop | light is yellow | light becomes red |
| Walk | light is red and walk is off | walk comes on |
| EndWalk | walk is on | walk goes off |
Commit a
Each behavior below starts as shown and goes on in the same pattern forever. Which one does the traffic light allow?
- red/off, red/on, green/on, yellow/on, red/on, green/on, …
- red/on, red/off, green/off, yellow/off, red/off, green/off, …
- red/off, green/off, yellow/off, green/off, yellow/off, green/off, …
- red/off, red/on, red/off, green/off, yellow/off, red/off, …
Answer red/off, red/on, red/off, green/off, yellow/off, red/off, …
- If you picked A
- The step from
red/ontogreen/onmakes the change Go describes, but Go is allowed only when walk is off. Check when an action may happen, not only what it does. - If you picked B
- Every step after the first state is fine. The first state is not: the light starts at
red/off. A behavior has to start right as well as move right. - If you picked C
- From yellow, the only action is Stop, to red. Nothing in the table goes from yellow to green. A spec allows the steps its actions describe, plus steps that change nothing, and nothing else; it does not need to forbid the rest.
- If you picked D
- Right. Walk, EndWalk, Go, Slow, Stop, and round again: every step is in the table, and it starts where the light starts.
Commit b
Does the traffic light allow a behavior in which the walk signal never comes on?
- No. Walk is one of its actions, so it has to happen at some point.
- Yes. Walk is allowed, but nothing requires it.
Answer Yes. Walk is allowed, but nothing requires it.
- If you picked A
- An action says a step may happen when its condition holds. It never says the step must. A light that cycles red, green, yellow forever, with nobody ever crossing, takes only allowed steps.
- If you picked B
- Right. Each action is permission, not obligation.
02Lab
Builder model TrafficLightStarter. The interactive builder loads with JavaScript; without it, the graph it builds is below.
The graph the builder computes (open it after your prediction)
This is a teaching view. Real checking happens with TLC on your machine.
Text view: every state and step
| State | light | walk | Steps (action → state) |
|---|---|---|---|
| initial 1 | "red" | FALSE | Go → 2 |
| 2 | "green" | FALSE | Slow → 3 |
| 3 | "yellow" | FALSE |
The builder opens with Go and Slow already written; read them against the table. In the builder, walk on and off are written TRUE and FALSE, and colours are words in quotation marks, such as "red".
- Look at the graph as it is. Where does the light end up, and what does its behavior do once it gets there? Answer in the first fold below.
- Add Stop, Walk and EndWalk, each with its condition and its change from the table. Every variable an action does not change must stay unchanged.
- When all five actions are in, compare the builder's counts with TLC's for its copy of the finished light: 4 states and 6 generated, which is the start plus one per step, so the builder should show one step fewer. If the builder warns that an action does not mention a variable, fix that first. If your counts differ, open the hints below one at a time.
- Walk the behavior you picked in the first question along your graph: does it start at the start, and does every step follow an arrow (or stay put)? The reveal does the same for all four.
Write something before openingwhere the light ends up, and what happens after.
With only Go and Slow, the light goes red, green, yellow, and no arrow leaves yellow. If it takes a step whenever it can, its behavior is red, green, then yellow forever: a light frozen on yellow. (It may also stutter earlier, at red or green; nothing forces a step.) TLC, asked about the same half-built light, reports yellow as a deadlock:
01-states-steps-behaviors/specs/TrafficLightStarter01-states-steps-behaviors/specs/TrafficLightStarter TrafficLightStarter.cfg
parse ok
model check: deadlock reached; trace has 3 states (2 steps); 3 distinct states
expected outcome met: deadlock
trace (changed variables only; ./tla trace --full shows all):
1 Init light = "red", walk = FALSE
2 Go light: "red" -> "green"
3 Slow light: "green" -> "yellow"Write something before openingwhat your graph shows so far. Builder hint 1.
Build one action at a time, and look at the graph after each. With Stop added, the light should go round in a circle and no new state should appear: Stop leads to red, which was already there. If a state appears that the table never mentions, the last action you added is the culprit.
Write something before openingwhich action you suspect. Builder hint 2.
Every action needs a value or "unchanged" for both variables: Stop, Go and Slow leave walk unchanged, and Walk and EndWalk leave light unchanged. In the builder, a variable an action does not mention can become any value in its list, and the builder warns you, because TLC would stop with an error on such a model.
Write something before openingyour conditions for Walk and EndWalk. Builder hint 3.
Walk has the same condition as Go: the light is red and walk is off. It sets walk to TRUE. EndWalk's condition mentions only walk, and it sets walk back to FALSE. If your count is too high, check that Go still requires walk to be off.
Reveal for commit a
Only the last one. The first breaks at red/on to green/on: Go needs walk off. The second breaks at its very first state, because the light starts at red/off. The third breaks at yellow/off to green/off: no action goes from yellow to green. The fourth starts at the start, and every step is one of Walk, EndWalk, Go, Slow and Stop, round and round.
Reveal for commit b
Yes. Red, green, yellow, red, green, yellow, and so on, with walk off in every state, takes only allowed steps: Go, Slow and Stop. The spec permits Walk; it never demands it. That is not a flaw in this light. It is what a list of allowed steps can and cannot say.
03Reason
If you picked the first behavior, you checked what Go does and not when it may happen. If you picked the second, you checked every step and forgot the first state. If you picked the third, you assumed that anything the table does not forbid is allowed. Each wrong answer skips one part of a test, and the test has exactly two parts.
Here is the idea. Imagine every infinite sequence of states you could ever write down for the traffic light: sensible ones, and absurd ones that start green or flicker between red and yellow. Each of them is a behavior. Pour all of them into a sieve. A spec is a filter: it lets through the behaviors the system may have, and holds back all the others. The sieve has two meshes.
- The first state. It must be a state the spec allows to start in. Here, only
red/off. - Every step. Each pair of consecutive states must be a step one of the actions allows, or a step that changes nothing.
Write something before openingwhich mesh caught each of the three wrong behaviors?
The second behavior was caught by the first mesh: it starts at red/on. The first and third were caught by the second mesh, at one bad step each. One of them broke a condition (Go when walk is on); the other made a change no action describes (yellow to green).
Notice something about the table. It never says "pedestrians must not walk while the cars have green". Yet the conditions keep the two apart: Walk happens only on red, and Go only once walk is off. Both are needed. Without Walk's, walk could come on during green; without Go's, green could come on during a walk. You did not write the rule; the conditions produced it, and the course had TLC check it in specs/TrafficLight.safe.cfg (chapter 03 teaches you to write such checks). The Core deletes one condition and lets the dangerous behaviors through: not a wrong action, but a filter with a hole in it.
Write something before openingthe light sits at red/off and stays there forever. Does the filter let that behavior through?
Yes. Its first state is the start, and every step changes nothing, which the second mesh allows (01-03 showed that steps that change nothing are always allowed). So the traffic light's spec allows a light that never turns green, and a print job that never prints. A filter on steps can say what must not happen. It cannot say that something must happen eventually. Chapters 09 and 10 add that.
The graph and the filter meet in the rule you used in the lab: a behavior is allowed exactly when it starts at an initial state and every step follows an arrow or stays put. So a finite graph describes an infinite set of behaviors. TLC works on the graph; your claims will be about behaviors.
04In your editor
The TLA+ copies are in chapters/01-states-steps-behaviors/specs/: TrafficLightStarter.tla for the half-built light and TrafficLight.tla for the finished one. Chapter 02 reads a copy of TrafficLight.tla next to the builder's table. To run the checks whose numbers this lesson quotes:
./tla check 01-04The finished light's block reads:
01-states-steps-behaviors/specs/TrafficLight01-states-steps-behaviors/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)The witness lines say TLC reached a state with walk on and one with the light yellow. The other blocks belong to the half-built light, the Core and the Stretch; do the Core and the Stretch before you read theirs. The "nobody walks on green" check is separate: ./tla check chapters/01-states-steps-behaviors/specs/TrafficLight.safe.cfg.
05Practice
Warm-up: a worked example, then your turn. Is red/off, red/on, red/off, red/on, … (pedestrians cross again and again, the cars never move) allowed? First mesh: it starts at red/off, the start. Second mesh: each step is either Walk (red/off to red/on, allowed since the light is red and walk is off) or EndWalk (red/on to red/off, allowed since walk is on). Allowed, and very annoying for the drivers.
Now judge this one, mesh by mesh: red/off, green/off, yellow/off, red/off, red/on, green/on, ….
Write something before openingyour verdict and the step that decides it.
Not allowed. The first mesh passes, and so do the first four steps (Go, Slow, Stop, Walk). The step from red/on to green/on fails: Go needs walk off. One bad step anywhere is enough to hold a behavior back.
Core. Make a careless light. In the builder, delete "walk is off" from Go's condition, so that Go is allowed whenever the light is red. Before you look, predict two numbers: the states the light reaches now, and the arrows between them. Also write down the shortest path from the start to a state where the cars have green while walk is on: the beginning of a dangerous behavior. Then open the fold to compare.
Write something before openingwhat got through the hole?
TLC's copy of the careless light reached 6 distinct states, with 11 generated: the start plus one per arrow. Every combination of light and walk is now reachable. The shortest path to danger is red/off, red/on, green/on: someone is told to walk, then the cars are given green. Check your arrows as well as your states. Deleting Walk's "light is red" instead also reaches every combination, but TLC's copy of that light generated 12, so only the arrow count tells the two edits apart. One deleted condition let in a whole family of behaviors.
Stretch: build the dial lock from nothing. Clear the builder and build 01-01's dial lock from its description: three wheels from 0 to 2, a shackle that is "open" or "closed", and five actions. Don't look at the shipped model until you are done. Three hints are in HINTS.md, under "01-04 stretch hint 1" to 3. Then judge this behavior, where each state is the three wheels followed by c for closed or o for open: 000c, 100c, 200c, 201c, 201o, 202o, 202c, 202o, and then 202o forever. Is it allowed? If not, which mesh catches it, and at which step?
Write something before openingyour verdict, and the mesh and step that decide it.
TLC's copy of the lock reached 54 states with 191 generated, so your builder should show as many states and one step fewer. A mistyped code gives the same two numbers, so check also that exactly one box has an Open step out: 201c. The behavior is not allowed. It starts at 000c, and the next six steps are TurnA, TurnA, TurnC, Open, TurnC and Close. The step from 202c to 202o fails the second mesh: Open needs the wheels at 2-0-1. Yet 202o is reachable; the lock was there two steps earlier. The filter holds back the opening step, not the state it ends in.
06Explain it
Open NOTES.md and answer the three prompts under 01-04. Compare your answers with "01-04 check your explanation" in HINTS.md.
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:Write about the traffic light you finished in the lab: why its conditions keep cars and pedestrians apart, which conditions do the work, and what each one prevents.
After you writeCheck your explanation
A good answer: "My traffic light allows a behavior when it starts at red/off and every step is one of the five actions or changes nothing. Cars and pedestrians never overlap because both conditions do their part. Walk's 'light is red' stops walk coming on during green or yellow; Go's 'walk is off' stops green coming on during a walk. The careless light in the Core showed that Go's condition is needed; removing Walk's would break it too, though I did not build that one. The builder and the runs I saw checked only which states are reachable and TypeOK; the careless light passed those runs too. Nothing I ran checked 'nobody walks on green' as a property, and nothing says the light ever changes at all."
A weak answer: "Go needs walk off, so the cars never get green while people walk." It names one condition and forgets that the other one is needed too, and it does not say what was and was not checked.
07Your system
Milestone 1: the state and steps of a system you work on, in a paragraph.
Run ./tla my-system init. It copies my-system-template/ to my-system/, which Git ignores. Keep that work in a private repository made with "Use this template", never in a public fork. Strip the names of your employer, customers and colleagues, and don't paste the table into third-party AI tools without your organisation's approval.
- Pick one small part of a system you know, something with a few moving parts: a job queue, a login flow, a cache, a feature flag rollout.
- In
my-system/TEMPLATE.md, under "State", list the variables a snapshot needs and the values each can take. Ask yourself the 01-01 question: is there a program counter hiding in it? - Under "Steps", list the actions, and for each one say when it is allowed and what it changes, the way the traffic light's table does.
- Below the steps, write one sentence about a behavior your table allows that you hope never happens. That sentence is the seed of milestone 2.
If your variables have small sets of values, you can type the table into the builder. It runs in your browser and sends your table nowhere; a reload clears it.
Tick milestone 1 in my-system/MILESTONES.md when the paragraph is written.
Done?