01-01

Chapter 01 · States, steps, behaviors

A state is a snapshot

  • 35 min core
  • Essentials track
  • Lab: State Graph Explorer
  • Skills: abstraction

00Recall

Three cards from chapter 00: the difference between a state and an interleaving, what has to happen for the transfer race to lose money, and how many steps a trace of numbered states has. The first one is this lesson's starting point.

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-01-a from Predict the balance

In transfer(), what must happen for the final balance to be wrong?

Show answer

Both transfers read the balance before either one writes it back. Then both compute from the same old value, and the second write erases the first.

00-03-a from Your first TLC run

A TLC trace lists states numbered 1 to k. How many steps does it contain?

Show answer

k - 1. The first state is where the run starts, before any step, like the first post of a fence.

Skip recall Recall is never required.

01Commit

This chapter has no TLA+ in it. It builds the four ideas the language is made of, using things you can hold in your hand. The first is a luggage lock.

The lock has three wheels. Each wheel shows a digit from 0 to 9, so in the course's notation Digits = 10. It also has a shackle, the metal loop that holds the bag shut. The shackle is either open or closed. It pulls open only when the wheels show the code, 2-0-1. You can push it closed whenever you like. The wheels turn whether the shackle is open or closed. When you buy the lock, it reads 0-0-0 with the shackle closed.

Commit a

Three wheels from 0 to 9, a shackle that is open or closed, and wheels that turn whether the shackle is open or not. How many different states can this lock be in?

Your answer is a whole number.

Answer 2000

Commit b

The lock is open. Is "open" a state of this lock?

  1. Yes. Open is one special state, the one the lock enters when the wheels show the code.
  2. No. Open is an event, the moment the shackle pops; afterwards the lock is in an ordinary state.
  3. Neither. Open is a condition, true of every state with the shackle up, whatever the wheels show.

Answer Neither. Open is a condition, true of every state with the shackle up, whatever the wheels show.

02Lab

Lab · State Graph ExplorerWhat you’re looking at: the dial lock with three digits a wheel, 0 to 2, instead of ten, so that every state fits on screen. Each box is one state. Click a box to see its value for every variable.

Builder model DialLock. The interactive builder loads with JavaScript; without it, the graph it builds is below.

The graph the builder computes (open it after your prediction)states per BFS level, level 1 on the left

This is a teaching view. Real checking happens with TLC on your machine.

Text view: every state and step
States and their successors
StateabcshackleSteps (action → state)
initial 1000"closed"TurnA → 2; TurnB → 3; TurnC → 4
2100"closed"TurnA → 5; TurnB → 6; TurnC → 7
3010"closed"TurnA → 6; TurnB → 8; TurnC → 9
4001"closed"TurnA → 7; TurnB → 9; TurnC → 10
5200"closed"TurnA → 1; TurnB → 11; TurnC → 12
6110"closed"TurnA → 11; TurnB → 13; TurnC → 14
7101"closed"TurnA → 12; TurnB → 14; TurnC → 15
8020"closed"TurnA → 13; TurnB → 1; TurnC → 16
9011"closed"TurnA → 14; TurnB → 16; TurnC → 17
10002"closed"TurnA → 15; TurnB → 17; TurnC → 1
11210"closed"TurnA → 3; TurnB → 18; TurnC → 19
12201"closed"TurnA → 4; TurnB → 19; TurnC → 20; Open → 21
13120"closed"TurnA → 18; TurnB → 2; TurnC → 22
14111"closed"TurnA → 19; TurnB → 22; TurnC → 23
15102"closed"TurnA → 20; TurnB → 23; TurnC → 2
16021"closed"TurnA → 22; TurnB → 4; TurnC → 24
17012"closed"TurnA → 23; TurnB → 24; TurnC → 3
18220"closed"TurnA → 8; TurnB → 5; TurnC → 25
19211"closed"TurnA → 9; TurnB → 25; TurnC → 26
20202"closed"TurnA → 10; TurnB → 26; TurnC → 5
21201"open"TurnA → 27; TurnB → 28; TurnC → 29; Close → 12
22121"closed"TurnA → 25; TurnB → 7; TurnC → 30
23112"closed"TurnA → 26; TurnB → 30; TurnC → 6
24022"closed"TurnA → 30; TurnB → 10; TurnC → 8
25221"closed"TurnA → 16; TurnB → 12; TurnC → 31
26212"closed"TurnA → 17; TurnB → 31; TurnC → 11
27001"open"TurnA → 32; TurnB → 33; TurnC → 34; Close → 4
28211"open"TurnA → 33; TurnB → 35; TurnC → 36; Close → 19
29202"open"TurnA → 34; TurnB → 36; TurnC → 37; Close → 20
30122"closed"TurnA → 31; TurnB → 15; TurnC → 13
31222"closed"TurnA → 24; TurnB → 20; TurnC → 18
32101"open"TurnA → 21; TurnB → 38; TurnC → 39; Close → 7
33011"open"TurnA → 38; TurnB → 40; TurnC → 41; Close → 9
34002"open"TurnA → 39; TurnB → 41; TurnC → 42; Close → 10
35221"open"TurnA → 40; TurnB → 21; TurnC → 43; Close → 25
36212"open"TurnA → 41; TurnB → 43; TurnC → 44; Close → 26
37200"open"TurnA → 42; TurnB → 44; TurnC → 21; Close → 5
38111"open"TurnA → 28; TurnB → 45; TurnC → 46; Close → 14
39102"open"TurnA → 29; TurnB → 46; TurnC → 47; Close → 15
40021"open"TurnA → 45; TurnB → 27; TurnC → 48; Close → 16
41012"open"TurnA → 46; TurnB → 48; TurnC → 49; Close → 17
42000"open"TurnA → 47; TurnB → 49; TurnC → 27; Close → 1
43222"open"TurnA → 48; TurnB → 29; TurnC → 50; Close → 31
44210"open"TurnA → 49; TurnB → 50; TurnC → 28; Close → 11
45121"open"TurnA → 35; TurnB → 32; TurnC → 51; Close → 22
46112"open"TurnA → 36; TurnB → 51; TurnC → 52; Close → 23
47100"open"TurnA → 37; TurnB → 52; TurnC → 32; Close → 2
48022"open"TurnA → 51; TurnB → 34; TurnC → 53; Close → 24
49010"open"TurnA → 52; TurnB → 53; TurnC → 33; Close → 3
50220"open"TurnA → 53; TurnB → 37; TurnC → 35; Close → 18
51122"open"TurnA → 43; TurnB → 39; TurnC → 54; Close → 30
52110"open"TurnA → 44; TurnB → 54; TurnC → 38; Close → 6
53020"open"TurnA → 54; TurnB → 42; TurnC → 40; Close → 8
54120"open"TurnA → 50; TurnB → 47; TurnC → 45; Close → 13

This is the builder. On the left is the lock written as a table: four variables, each with the values it can take, and five actions, each saying when it may happen and what it changes. On the right is every state those rules can reach from the start. The shackle's two values are written "open" and "closed", with quotation marks, because they are words rather than numbers. You will write a table from scratch in 01-04; in this lesson you only read this one and, in the Core, add to it.

  1. Click the box at the top. That is the start. Its table has four rows, one per variable: a, b and c for the wheels, and shackle.
  2. Find a box where the shackle is open and the wheels do not show 2-0-1. How did the lock get there?
  3. Read the number of states the builder reports. TLC, run on a TLA+ copy of this smaller lock, counted 54. The two should agree.

Reveal for commit a

TLC, run on a TLA+ copy of the lock with Digits = 10, counted 2,000 states. You can see where the number comes from: each wheel has 10 positions and the shackle has 2, and a state picks one of each, so there are 10 × 10 × 10 × 2 combinations. The count and the product agree, which means every combination can actually happen: open the lock, then turn the wheels to anything you like. The builder's smaller lock works the same way, with 3 × 3 × 3 × 2 combinations and 54 states.

Reveal for commit b

"Open" is a condition that a state either meets or does not: the shackle is up. Every state with shackle set to "open" meets it, one for each setting of the wheels. In the builder you found such a state with the wheels off the code: the lock was opened at 2-0-1, and then someone turned a wheel. Pulling the shackle is a different kind of thing. It is a change, from a state where the shackle is closed to one where it is open, and lesson 01-02 calls it a step.

03Reason

If you said 1000, you described the lock by the part you find interesting, the wheels, and dropped the part that seemed too obvious to count. The shackle is not decoration. A lock at 2-0-1 with its shackle closed is in a different situation from a lock at 2-0-1 with its shackle open: you can pull one bag open and not the other.

So here is the first idea of the course, stated carefully. A state is a value for every variable, all at the same instant. Not the variables you care about at the moment. Every one of them. Think of a photograph taken with the shutter open for no time at all. It catches the wheels and the shackle together, whether or not you were looking at the shackle.

Two snapshots are the same state exactly when every variable has the same value in both. Change one wheel by one click and you have a different state, even though the lock "means" the same thing to you.

Who decides what the variables are? You do.

Write something before openingthe code 2-0-1 is part of the lock. Is it part of the state?

Not in this model. The code never changes, so it is part of the rules: it appears in the condition for opening, not in any snapshot. Now imagine a lock with a button that lets you set a new code. The code can change, so it has to become a variable, and every state must say what the code currently is. Same object, different model. The Core below builds that lock and asks you how many states it has.

The same goes for everything you leave out. The model has no colour, no bag, no thumb on the wheel, no clock. Each of those is a decision: either it never changes, or you have decided it does not matter for the question you are asking. Choosing the variables is choosing what your model can talk about. Choose too few and you cannot state the bug you are hunting. Choose too many and the number of states explodes. This course calls that skill abstraction, and you will practise it in every chapter.

Programs have states too, and they hide a variable that is easy to forget. Here is a short one:

output
count = 0
while count < 2:          # line 2
    count = count + 1     # line 3
print(count)              # line 4
Write something before openingthe program has count equal to 2 at line 2, right before the loop test fails, and again at line 4. Same state or not?

Different states. The data is the same, but the program is somewhere else, and what can happen next is different: at line 2 the test runs, at line 4 the program prints. Where the program is, its program counter, is part of the snapshot.

There is nothing special about a program counter. It is one more variable, often called pc, whose values are places in the code. Write it down, and the program's states are pairs: pc and count. Leave it out, and two moments that behave differently look identical, which is the forgotten-shackle mistake again. The lock, on the other hand, has no program counter at all. Nobody missed it. A model needs a pc only when "where we are in a procedure" matters, and chapter 07 shows a language that adds one for you.

The definition of a state also settles the second question. "Open" fixes one variable, shackle, and leaves the wheels free, so it is a condition on states that many states meet, not a state.

04In your editor

The counts on this page come from TLC, run on TLA+ copies of the locks in chapters/01-states-steps-behaviors/specs/: DialLock.tla for the lock on this page, and RecodableDialLock.tla for the Core's lock. You do not need to read them yet; chapter 02 writes a close variant of the dial lock and reads it line by line. If you set up the tools in 00-03, run this from the repository root:

shell
./tla check 01-01

It checks both sizes of the lock, then the Core's lock. That last block contains the Core's answer, so do the Core before you read it. The ten-digit lock's block reads:

Output 01-states-steps-behaviors/specs/DialLock.real
01-states-steps-behaviors/specs/DialLock.real  DialLock.real.cfg  (Digits=10)
parse ok
model check: no violation found in 2000 distinct states (7002 generated, depth 32)
expected outcome met: pass
coverage: all 5 actions fired
witness OpenOffCode reached (trace of 6 states)

Read it as one long sentence: TLC found no violation of TypeOK, the only invariant DialLock.real.cfg asks for, in the model with Digits = 10, in which every action fired and the witness was reached. That is all it was asked. TypeOK says only that each wheel shows one of its digits and the shackle is open or closed, so the pass is no surprise. The number of distinct states is the count you predicted. The coverage line says every one of the five actions happened at least once, and the witness line says TLC reached an open lock with the wheels off the code. Lesson 03-03 explains why the course always asks for those two lines.

05Practice

Warm-up: a worked example, then your turn. The lock shows 2-0-1 and someone has pulled the shackle. Its state is a = 2, b = 0, c = 1, shackle = "open". Four variables, four values.

Now someone turns wheel B one click. Write the new state, all four values.

Write something before openingthe four values.

a = 2, b = 1, c = 1, shackle = "open". Only b changed, but you still write all four: a state is every variable, not the one that moved. The lock is still open, in a different state.

Core. Give the builder's lock a code button. Many luggage locks have one: with the shackle open, you set the wheels to a new code and press the button, and from then on the lock opens at that code. The code can now change, so it has to be part of the state.

  1. Add three variables, ka, kb and kc, each with the values 0..2, starting at 2, 0 and 1. Together they are the current code.
  2. Check that each of the five existing actions leaves the three new variables unchanged. The builder fills that in when you add a variable.
  3. Add an action SetCode, allowed when the shackle is open, that sets ka to a, kb to b and kc to c, and leaves the wheels and the shackle unchanged.
  4. Change Open's condition so that it compares the wheels with ka, kb and kc instead of with 2, 0 and 1.

Before you look at the builder's count, write down how many states you expect, and why. Then open the fold to compare. The graph is now too big to draw, so the builder shows a table instead.

Write something before openingwhat did you expect, and why?

TLC, run on a TLA+ copy of this lock, counted 1,458 states. That is 27 × 27 × 2: every setting of the wheels, times every code, times the two shackle positions. Every combination can happen: open the lock, turn the wheels to any code, press the button, turn the wheels to anything else, and close it. If you expected 54, you kept the code in the rules, where it was before the button existed. The count cannot tell you whether you did steps 3 and 4: a SetCode allowed while the shackle is closed, or an Open that still compares with 2-0-1, reaches the same number. Check steps instead. The closed state with wheels 0-0-0 and code 0-0-0 must have an Open step out; the closed state with wheels 2-0-1 and code 0-0-0 must not; and no closed state may have a SetCode step out.

06Explain it

Open NOTES.md in this chapter's folder and answer the three prompts under 01-01. If the file is missing, ./tla notes 01 writes it. Then compare your answers with "01-01 check your explanation" in HINTS.md. You grade yourself.

output
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:

For the first prompt, explain your count of the Core lock's states and why it is right.

After you writeCheck your explanation

A good answer: "Once the code can change, it is part of the state, so the Core lock has a state for every setting of the wheels, every code and both shackle positions. Every combination can happen: open the lock, set the wheels to any code and press the button, turn the wheels to anything, and close it. That relies on the wheels turning while the shackle is open and on SetCode copying any setting of the wheels; a lock whose wheels jam while open would reach far fewer. TLC counted states of a model with forward-clicking wheels. The count does not show that SetCode's condition is right: a button that worked only while the shackle is closed would reach the same states. It checked nothing about a real lock, and nothing about whether the code button is a good idea."

A weak answer: "Three wheels, three code digits and a shackle, so multiply." It multiplies without asking whether every combination can be reached, which is the question 01-02 is about.

Done?