01-03

Chapter 01 · States, steps, behaviors

Behaviors are infinite

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

00Recall

Two cards from 01-02 (steps versus actions, and why fewer states are reachable than exist) and one from 01-01 (what a state is). States and steps are the alphabet. Today you write the words.

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.

01-02-b from Steps and the state graph

Why can a system reach fewer states than the number of combinations of its variables' values?

Show answer

The state graph holds only the states reachable from an initial state by allowed steps. Some combinations are good snapshots that no sequence of steps leads to.

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.

Skip recall Recall is never required.

01Commit

One print job on an office printer that jams. The job waits in the queue, then prints. Printing ends in one of two ways: the job finishes, or the paper jams. When it jams, someone pulls out the crumpled sheet and the job goes back in the queue. Once the job is done, nothing more can happen to it.

ActionAllowed when the job isIt becomes
Startqueuedprinting
Finishprintingdone
Jamprintingjammed
Clearjammedqueued

A behavior is a sequence of states: a first state, a second, a third, and so on. A behavior of the job starts queued, and each of its changes is allowed by the table.

Commit a

The job starts queued. How many different behaviors does it have?

  1. Finitely many: one for each route through the table that never repeats a state.
  2. Two: either it finishes, or it jams.
  3. A large but finite number: every route through the table that ends at done.
  4. Infinitely many.

Answer Infinitely many.

Commit b

In some behavior, the job reaches done. What is the next state of that behavior?

  1. There is none. The behavior ends at done.
  2. Done again, and again after that, forever.
  3. There is none, and since nothing is allowed, this history is not a behavior of the job at all.

Answer Done again, and again after that, forever.

02Lab

Lab · State Graph ExplorerWhat you’re looking at: every state one print job can reach and every step between them. The job starts queued. No arrow leaves done.

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

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

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

Text view: every state and step
States and their successors
StatestatusSteps (action → state)
initial 1"queued"Start → 2
2"printing"Finish → 3; Jam → 4
3"done"
4"jammed"Clear → 1
  1. Find a loop: a route that leaves a box and comes back to it. Which actions does it use?
  2. Use the builder to show a path from the start to done. Then, on paper, write a different route to done that is longer.
  3. Look at done. No arrow leaves it. Before you read on, say what you think the job's history does after it gets there.

Reveal for commit a

Infinitely many, from a graph of only 4 states. The job can finish without a jam, or after one jam, or after two, and each of those is a different behavior. It can also jam, be cleared, start, and jam again, round and round the loop, without ever finishing. Nothing in the table forbids that. A finite graph, infinitely many histories.

Reveal for commit b

Done, then done, then done, forever. Every behavior is an infinite sequence of states. When nothing more happens to the job, its history does not stop; its state stops changing. Each step from done to done is a step that changes nothing. Such a step has a name: a stuttering step.

03Reason

If you picked "the behavior ends at done", you described a run the way a log file shows it: the log stops when the program stops. That is a fine way to look at programs. It is not how the course looks at systems, and chapters 09 and 10 are built on the difference.

A behavior is an infinite sequence of states. Each consecutive pair is a step. A spec picks out the behaviors of its system: the ones that start right and take only steps it allows. So what do we do with a system that finishes? We say that after its last real change, it stays where it is, forever.

Think of a security camera pointed at an empty room. The recording does not end when the last person leaves. Frames keep coming, and every frame shows the same empty room. Nothing happens, and the camera faithfully records nothing happening. A finished print job is that room.

Write something before openingwhy go to the trouble? Why not let some behaviors be finite?

Because then every idea would need two versions. Some systems never stop: a traffic light, a web server, a heartbeat. Others stop: a print job, a database migration, a program. If behaviors could be finite, every claim about "always" or "eventually" would need a special case for the end. With every behavior infinite, there is no end to worry about. Stopping is one ordinary way to go on forever.

There is a consequence you may not expect. Nothing about this is special to done: the course's specs allow a step that changes nothing from every state. So queued, queued, printing, done, done, … is also a behavior of the print job: the snapshot was taken twice before anything changed. That looks like a waste now. It is what lets a detailed spec implement a simpler one, matching it step for step once you allow for steps that change nothing. Chapter 04 starts that idea and chapter 11 finishes it.

Now go back to the first question. The graph has 4 states and a handful of arrows. The behaviors are infinitely many: stuttering alone gives that (wait in the queue for one step, or two, or a thousand, before printing), and the jam loop adds histories that never finish. The state graph is not the set of behaviors. The graph is a map. A behavior is one journey across it that never stops: at each step it follows an arrow or stays where it is.

Write something before openingan infinite journey on a map with only a few places must do something over and over. What?

It must visit some state again and again: with only a few places to be and infinitely many steps to take, some place comes up infinitely often. A behavior that finishes sits at done forever. One that never finishes either goes round the jam loop forever, or stops in queued, printing or jammed and stutters there forever. The spec allows that too, because nothing in it forces a step.

That shape has a name. A lasso is a behavior written as a stem followed by a loop that repeats forever. "Queued, printing, done, then done forever" is a lasso: the stem is queued and printing, the loop is done stuttering in place. "Queued, printing, jammed, then back to queued, forever" is a lasso whose loop is the whole circle. "Queued, printing, then printing forever" is a lasso too, of a job stuck on the printer.

Write something before openingis every behavior of every system a lasso?

No, not even for the print job. Go round the jam loop forever, but stutter at queued once on the first trip, twice on the second, three times on the third, and so on. That behavior is allowed, and no stretch of it repeats forever, so it has no stem-and-loop description. Lassos are the eventually periodic behaviors: a finite stem, then a finite loop repeated forever. TLC can print one as a finite trace, and chapter 09 shows you how it does.

TLC itself treats done in its own way. Ask it to check the print job with its default settings, and it complains:

04In your editor

The TLA+ copy of the job is chapters/01-states-steps-behaviors/specs/PrintJob.tla, with two configurations: PrintJob.cfg checks every state with the deadlock report turned off, and PrintJob.deadlock.cfg leaves it on. Run:

shell
./tla check 01-03

The block for PrintJob.cfg reads:

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

The deadlock block is the one quoted above. The block for CancellablePrintJob contains the Core's answers, so do the Core first. Notice that the deadlock report's trace has its states numbered from one, ending at done. It shows how TLC got there. It is not the whole behavior: that goes on, at done, forever.

05Practice

Warm-up: a worked example, then your turn. Here is the behavior "jams once, then finishes", written in full: queued, printing, jammed, queued, printing, done, done, done, and done forever.

Now write the behavior "jams every time and never finishes" as a lasso: a stem, then a loop.

Write something before openingthe stem and the loop.

The stem is empty: the job starts in the loop. The loop is queued, printing, jammed, and then back to queued, forever. If you wrote the stem as "queued" and the loop as "printing, jammed, queued", that describes the same behavior. A behavior can have several lasso descriptions.

Core. Give the printer a Cancel button. In the builder, add "cancelled" to the values of status, and add an action Cancel that is allowed when the job is queued or jammed (status = "queued" \/ status = "jammed", where \/ is read "or") and sets the status to "cancelled". Before you look, answer four questions. How many states? How many arrows? Which states have no arrow out? Can a job still jam forever? Then open the fold to compare.

Write something before openingyour four answers.

TLC's copy of the changed job reached 5 distinct states, with 7 generated. As 01-02 showed, "generated" is the start plus one for each arrow TLC followed, so the graph has one arrow fewer than that number. Count yours: Start out of queued, Finish and Jam out of printing, Clear out of jammed, and Cancel out of both queued and jammed. Neither number tells you whether Cancel's condition is right: a Cancel allowed from queued or printing gives the same two. Check which arrows Cancel adds: one out of queued, one out of jammed. The one new state is cancelled. Done and cancelled now have no arrow out, and every behavior that reaches either one stutters there forever. And yes, a job can still jam forever. Cancel is allowed while the job is jammed; nothing makes anyone press it. A table of allowed steps says what may happen, never what must.

06Explain it

Open NOTES.md and answer the three prompts under 01-03. Compare your answers with "01-03 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, explain in your own words why the print job has infinitely many behaviors but only a few states.

After you writeCheck your explanation

A good answer: "The print job has only a few states, but a behavior is an infinite sequence of them, and a step may change nothing. Stuttering alone gives infinitely many behaviors: the job can wait in the queue for one step, or two, or a thousand, before printing. The jam loop adds histories that jam any number of times, or forever. A finished job's behavior repeats done forever with stuttering steps. The jam loop depends on Clear sending the job back to the queue; if a jam ended the job, no history could jam more than once, though stuttering would still give infinitely many behaviors. Nothing here says a jammed job is ever cleared: the table only allows it."

A weak answer: "Behaviors are infinite because the job can jam forever." That is one infinite behavior. It misses the point that every behavior is infinite, including the ones that finish.

Done?