01-02

Chapter 01 · States, steps, behaviors

Steps and the state graph

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

00Recall

Two cards from 01-01 (what a state is, and the program counter) and one from chapter 00 (a state versus an interleaving). Today the states start to move.

01-01-a from A state is a snapshot

What is a state?

Show answer

A value for every variable of the model, all at one instant. Two states are the same only if every variable has the same value in both.

01-01-b from A state is a snapshot

A program reaches two different lines with exactly the same data. Is that one state or two?

Show answer

Two. Where the program is, its program counter, is one more variable, and it differs. What can happen next differs too.

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.

Skip recall Recall is never required.

01Commit

Meet a cheaper lock. It has two wheels, and each shows only 0 or 1. The code is 1-0. It starts at 0-0 with the shackle closed. There are four kinds of thing that can happen:

ActionAllowed whenWhat changes
TurnAthe shackle is closedwheel a flips between 0 and 1
TurnBthe shackle is closedwheel b flips between 0 and 1
Openthe shackle is closed and the wheels show 1-0the shackle opens
Closethe shackle is openthe shackle closes

The one difference from the dial lock: these wheels jam while the shackle is open.

Commit a

Starting from 0-0 with the shackle closed, and using only the four actions, how many different states can the tiny lock reach?

Your answer is a whole number.

Answer 5

Commit b

The state graph draws each state as a box and each possible change as an arrow. How does the action TurnA appear in the tiny lock's graph?

  1. As one arrow labelled TurnA, from the start to the state with wheel a flipped.
  2. As several arrows labelled TurnA, one out of every state where the shackle is closed.
  3. As one box labelled TurnA, standing for the state the lock is in after TurnA.

Answer As several arrows labelled TurnA, one out of every state where the shackle is closed.

02Lab

Before you open the builder, draw the graph by hand. Use a pencil; you will want to move things.

  1. Draw a box for the start: a 0, b 0, closed.
  2. For each of the four actions, ask: is it allowed in this box? If yes, work out the state it leads to. If you have not drawn that state yet, draw a new box. Draw an arrow from the old box to the new one, labelled with the action.
  3. Finish every box on one row before you start the next row. Stop when a row adds no new box.

Put the start on the top row, the boxes one arrow away from it on the second row, and so on. When you are done, count your boxes and compare with your prediction. Then open the builder.

Lab · State Graph ExplorerWhat you’re looking at: every state the tiny lock can reach from 0-0 with the shackle closed, one row per distance from the start. Each arrow is one step, labelled with the action that makes it.

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

The graph the builder computes (open it after your prediction)12345

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

Text view: every state and step
States and their successors
StateabshackleSteps (action → state)
initial 100"closed"TurnA → 2; TurnB → 3
210"closed"TurnA → 1; TurnB → 4; Open → 5
301"closed"TurnA → 4; TurnB → 1
411"closed"TurnA → 3; TurnB → 2
510"open"Close → 2

Compare the builder's graph with yours, box by box and arrow by arrow. The builder places each box on the row of its shortest distance from the start, which is how you were told to draw.

Reveal for commit a

TLC, run on a TLA+ copy of the tiny lock, counted 5 states, and so does the builder: the four settings of the wheels with the shackle closed, plus 1-0 with the shackle open. The other combinations are open locks with the wheels at 0-0, 0-1 or 1-1. Each is a perfectly good snapshot, but no sequence of actions leads to any of them: the shackle opens only at 1-0, and then the wheels jam.

Reveal for commit b

Every closed box has an arrow labelled TurnA leaving it, and each one ends at the state with wheel a flipped and everything else the same. The open box has none, because the wheels jam. So the single rule TurnA appears as several arrows, each joining two particular states.

03Reason

If you said 8, you multiplied the possibilities, which is the right way to count snapshots and the wrong way to count what a system can do. 01-01's lock hid that difference, because every combination of its wheels and shackle could happen. This lock does not.

Start with the arrows. A step is a pair of states: the one before and the one after. An arrow from box 0-0 closed to box 1-0 closed is one step. Nothing in it says how long the change took or what it felt like. It says: here is a snapshot, and here is the very next snapshot.

An action is something different: a rule that says which steps are allowed. TurnA says "when the shackle is closed, a step may flip wheel a and leave everything else alone". That rule allows a step out of each closed state. One action, many steps. Chapter 02 writes actions as formulas that are true or false of a pair of states, and the pair is exactly this: before and after.

Now the boxes. The graph you drew contains only the states you could reach: start at an initial state and follow arrows. That is a much stronger condition than "is a sensible snapshot".

Write something before openingthe open lock with the wheels at 0-0 is not in your graph. Is it a state?

Yes. It gives a value to every variable, and every value is allowed. It is a state the tiny lock cannot reach from its start. Start the lock somewhere else, say already open at 0-0, and it would be in the graph. Reachability depends on where you start and on the rules, not only on which snapshots make sense.

So the state graph of a system is two things together: the states reachable from its initial states, and the steps between them. You built it by hand with a simple procedure: row by row, try every action in every box of the row, and put what is new on the next row. That procedure has a name, breadth-first search, because it finishes a whole row before starting the next.

TLC also reports a depth: the number of rows in your drawing, counting the start's row. For the tiny lock it is 3. Mind the difference between rows and arrows. A path that visits k states takes k − 1 steps, in the same way that a fence of k posts has k − 1 gaps. The course always says which one it means.

Write something before openingwhich states have the most arrows leaving them, and why does the open state have only one?

The closed state at 1-0 has three: TurnA, TurnB and Open are all allowed there. The other closed states have two. The open state has only Close, because the wheels jam. The number of arrows out of a box is the number of different changes that can happen next, and that is where a system's choices live.

A box with two arrows out means the lock does not decide what happens next: either wheel might turn. In 00-02 the two transfers took turns in every possible order, and that was the same thing: many arrows out of one state. Chapter 02 calls it nondeterminism and shows how a spec says "either of these may happen".

04In your editor

The TLA+ copy of the tiny lock is chapters/01-states-steps-behaviors/specs/TinyLock.tla. Chapter 02 reads specs like it. To check it and the Core variant below, run:

shell
./tla check 01-02

The tiny lock's block reads:

Output 01-states-steps-behaviors/specs/TinyLock
01-states-steps-behaviors/specs/TinyLock  TinyLock.cfg
parse ok
model check: no violation found in 5 distinct states (11 generated, depth 3)
expected outcome met: pass
coverage: all 4 actions fired
witness Opened reached (trace of 3 states)

In the model check line, "distinct states" is the number of boxes and "depth" is the number of rows. The middle number, "generated", counts every arrow TLC followed plus the start, including arrows into boxes it had already drawn. Lesson 03-04 takes those numbers apart. The second block, for the Core variant, contains its answers, so do the Core first.

05Practice

Warm-up: a worked example, then your turn. From the closed state at 1-0, three actions are allowed. TurnA leads to 0-0 closed. TurnB leads to 1-1 closed. Open leads to 1-0 open. Close is not allowed, because the shackle is already closed.

Now list every next state from 1-0 with the shackle open, and say which action leads to each.

Write something before openingthe list.

Only one: Close leads to 1-0 closed. TurnA and TurnB are not allowed, because the wheels jam while the shackle is open, and Open is not allowed, because the shackle is already open.

Core. Unjam the wheels. In the builder, delete the condition "the shackle is closed" from TurnA and from TurnB, so that the wheels turn whether the shackle is open or not. Before you look at the graph, predict two numbers: the states it reaches, and the number of rows. Then open the fold to compare.

Write something before openingwhat changed, and why?

TLC's copy of the changed lock reached 8 states with a depth of 5, and the builder should agree. Every combination is now reachable: open at 1-0, then turn the wheels to anything. The open states that were missing appear. The graph also gets deeper, because the farthest of them is reached by opening first and then turning: those extra rows are the turns after the open. One deleted condition changed what the lock can do, and nothing about the snapshots themselves changed.

06Explain it

Open NOTES.md and answer the three prompts under 01-02. Compare your answers with "01-02 check your explanation" in HINTS.md.

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, write about the graph you drew by hand: why it has exactly the boxes it has.

After you writeCheck your explanation

A good answer: "My graph has every state reachable from 0-0 closed, found row by row by trying every action in every state until a row added nothing new. The open states with the wheels away from 1-0 are missing, because the shackle opens only at 1-0 and the wheels then jam. If the wheels could turn while the shackle is open, those states would appear, as the Core showed. TLC and the builder counted the states of this one model; neither says anything about a real lock or about which order a person tries combinations in."

A weak answer: "There are eight possible states but only some are used." It does not say which states are missing or why, and "used" hides the real idea, which is reachable by allowed steps from the start.

Done?