03-01

Chapter 03 · Meet TLC

One command

  • 42 min core
  • Essentials track
  • Skills: intent, evidence

00Recall

Three cards from earlier chapters: what a primed variable means, what \/ (read "or") does inside an action, and why a behavior never ends. Rate each one honestly. If you work in the terminal, the same cards are in drills/CARDS.md.

02-01-a from Primes

In an action, what does a primed variable such as hour' stand for?

Show answer

The variable's value in the new state of a step. The unprimed hour is its value in the old state. The action is a true-or-false claim about that pair of states, not an instruction.

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.

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.

Skip recall Recall is never required.

01Commit

Here is this chapter's puzzle. You have a small jug that holds 3 cups (Small = 3) and a big jug that holds 7 (Big = 7). Neither has markings. You have a tap and a sink. A move is one of three things: fill a jug to the brim, empty it, or pour one jug into the other until the first is empty or the second is full. A recipe needs exactly 5 cups.

The whole puzzle, written in TLA+, is specs/KitchenMeasures.tla. Its configuration file asks TLC to check two claims in every state it can reach: TypeOK (each jug holds a whole number of cups that fits in it) and EmptyOrFull (after every move, at least one jug is empty or full).

Commit a

You run ./tla check on KitchenMeasures.cfg, which asks only for TypeOK and EmptyOrFull. What does TLC report?

  1. No violation, and some counts of the states it explored.
  2. The shortest way to get 5 cups, since that is what the puzzle is about.
  3. Nothing: it runs forever, because behaviors are infinite.

Answer No violation, and some counts of the states it explored.

Now a slip of the keyboard. A teammate's copy, specs/TypoKitchen.tla, is identical except that one line of Next says FilBig instead of FillBig.

Commit b

Inside Next, a teammate typed FilBig instead of FillBig. Nothing called FilBig is defined. Who complains, and when?

  1. SANY, before TLC explores a single state.
  2. TLC, which reports a violation when it reaches the bad line.
  3. Nobody. Next gains an option that is never true, and TLC quietly skips it.

Answer SANY, before TLC explores a single state.

02Lab

From the repository root, run the check on the working spec:

shell
./tla check chapters/03-meet-tlc/specs/KitchenMeasures.cfg
Expected output 03-meet-tlc/specs/KitchenMeasures
03-meet-tlc/specs/KitchenMeasures  KitchenMeasures.cfg  (Small=3, Big=7)
parse ok
model check: no violation found in 20 distinct states (75 generated, depth 10)
expected outcome met: pass
coverage: all 6 actions fired
witness FiveCups reached (trace of 9 states)

Reveal for commit a

No violation. TLC found 20 distinct states and checked both claims in each one. The two extra lines come from ./tla, not from TLC: every action fired at least once, and a state with five cups is reachable. Lesson 03-03 explains why the course insists on both.

Now run the teammate's copy:

shell
./tla check chapters/03-meet-tlc/specs/TypoKitchen.cfg
Expected output 03-meet-tlc/specs/TypoKitchen
03-meet-tlc/specs/TypoKitchen  TypoKitchen.cfg  (Small=3, Big=7)
parse error: Unknown operator: `FilBig'.
  chapters/03-meet-tlc/specs/TypoKitchen.tla:67:8: Unknown operator: `FilBig'.
expected outcome met: parse-error FilBig

Reveal for commit b

SANY rejects the module with "Unknown operator", and gives the line and column of the slip. TLC never starts. The verdict line says "expected outcome met" because this cfg was written to show a parse error: its header says @expect: parse-error FilBig.

03Reason

If you picked the shortest recipe, you gave TLC credit for knowing what you want. It does not. If you picked "runs forever", you mixed up behaviors and states. Both mistakes come from one picture of TLC as a program that runs your system. It is something stranger, and to see it we need to pull apart the one command you typed.

./tla check runs two programs on two inputs. The inputs are the module and the cfg; the programs are SANY, then TLC. Take the pieces in the order they meet your text.

Write something before openingthe module never says Small = 3. So where does the 3 come from?

From the cfg. KitchenMeasures.tla declares Small and Big as CONSTANTS: names with no value. The module describes the puzzle for every pair of jug sizes at once. The cfg line CONSTANTS Small = 3 Big = 7 picks one pair, and TLC checks only that one.

SANY is the parser and semantic checker. It reads the module and checks that it is well formed: every name is defined, every operator gets the right number of arguments, and every expression sits at a level that makes sense (you cannot prime something that is already primed, for instance). SANY never computes a state. That is why the typo is caught before anything runs: FilBig is not a false claim, it is a word with no meaning.

The cfg is the half of the question the module leaves open. Here it is, minus the header comments:

cfg
SPECIFICATION Spec
CONSTANTS Small = 3  Big = 7
INVARIANT TypeOK
INVARIANT EmptyOrFull

Line by line: which formula is the system, what the constants are, and which claims to check. An invariant is a claim about a single state that should be true in every state the system can reach. INVARIANT is the cfg's way of asking "check this one too".

A module can also say what it needs from the cfg. ASSUME states a fact about the constants, and TLC checks it before it computes a single state. This chapter's thermostat, which 03-04 and 03-05 use, says ASSUME Band \in 1..Target: the band is a whole number from 1 up to the target (\in is read "is an element of"). specs/Thermostat.assume.cfg gives it Band = 0, and TLC stops at once instead of checking a thermostat with no band:

Output 03-meet-tlc/specs/Thermostat.assume
03-meet-tlc/specs/Thermostat.assume  Thermostat.assume.cfg  (Target=20, Band=0)
parse ok
model check: TLC error: Assumption line 19, col 8 to line 19, col 25 of module Thermostat is false.
  chapters/03-meet-tlc/specs/Thermostat.tla:19:8: Assumption line 19, col 8 to line 19, col 25 of module Thermostat is false.
expected outcome met: error Assumption

No cfg line asks for that check. TLC checks every ASSUME in the module, always.

TLC is the model checker. It computes every initial state, then every state one step away, then every state one step from those, and so on, until no step leads anywhere new. In each new state it evaluates every invariant the cfg names.

Write something before openinga behavior is an infinite sequence of states. How can TLC check every behavior in a finite time?

Every state of every behavior is a reachable state, and there are only finitely many reachable states here. If an invariant holds in each of them, it holds in every state of every behavior, however long. TLC never walks a behavior to its end. It walks the graph of states, which is finite.

One more line in the cfg deserves a second look: SPECIFICATION Spec. The module defines

TLA+
Spec is defined as Init and always[Next]_vars

Read it as "start in a state where Init holds, and then always ([]) take a Next step or a step that changes none of vars". That is the shape every spec in this course will have. Chapter 04 unpacks it piece by piece. For now, notice that you could also name the two halves directly:

cfg
INIT Init
NEXT Next

specs/KitchenMeasures.initnext.cfg does exactly that, and TLC reports the same 20 distinct states. The course writes SPECIFICATION Spec anyway, because later chapters add to Spec (fairness, in chapter 10) and the cfg must point at the whole formula, not its parts.

04In your editor

The spec is chapters/03-meet-tlc/specs/KitchenMeasures.tla. One command checks every exercise of this lesson:

shell
./tla check 03-01

The passing spec on its own prints:

Output 03-meet-tlc/specs/KitchenMeasures
03-meet-tlc/specs/KitchenMeasures  KitchenMeasures.cfg  (Small=3, Big=7)
parse ok
model check: no violation found in 20 distinct states (75 generated, depth 10)
expected outcome met: pass
coverage: all 6 actions fired
witness FiveCups reached (trace of 9 states)

Read it one line at a time. The first line names what was checked and under which constants. "parse ok" is SANY's verdict. "model check" is TLC's, in one sentence with its counts. "expected outcome met" compares that verdict with what the cfg said would happen. The coverage and witness lines are ./tla's own checks, explained in 03-03. In your terminal one more line follows, raw log: and a path under .tla-out/. ./tla always keeps TLC's raw log: when a summary surprises you, the raw log is the evidence.

Where does "expected" come from? Every cfg in this course opens with a few comment lines that ./tla reads and TLC ignores:

cfg
\* @expect: pass
\* @witness: FiveCups
\* @coverage: all

@expect is the outcome the author predicted before running: pass, a named violation, a parse error, and so on. A check whose outcome nobody predicted is a check nobody can be surprised by, so the course writes the prediction down. When you meet a starter cfg in an exercise, it also has a @goal line: the outcome once you have filled the gaps. ./tla compares the run with @expect while gaps remain and with @goal after.

When a check does not do what you expected, read in this order: the first line that is not "ok" in spirit (a parse error, an unexpected verdict), then the trace if there is one, then the raw log. Resist the urge to edit until you can say in one sentence what TLC saw.

05Practice

Warm-up. The cfg has lost its constants. TLC refuses to start; give it what it needs.

Core. One of the six moves is missing, and one line of the output says which. Write it.

03-01-ex1Warm-up5 min

03-01-ex1 · Give it constants

Tier: warm-up. Time: about 5 minutes.

KitchenMeasures.tla is the spec from the lesson, unchanged. Its cfg has lost the line that gives the constants their values, so TLC does not know how big the jugs are.

  1. Run ./tla check 03-01-ex1 and read the error. Which program reported it: SANY or TLC? How can you tell?
  2. Fill gap g1 in KitchenMeasures.cfg: a 3-cup small jug and a 7-cup big jug. Delete the \* GAP line.
  3. Run the check again. Look at the two lines ./tla adds after the verdict. Some other jug sizes would pass too, so check the constants named on the first line.

The module declares Small and Big and never says how big they are. That is on purpose: the same module describes every pair of jugs, and the cfg chooses one.

Hints: HINTS.md, under "03-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/03-meet-tlc/exercises/01-ex1-give-it-constants/; 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 03-01-ex1 prints "goal met: pass".

./tla check 03-01-ex1

Repo chapters/03-meet-tlc/exercises/01-ex1-give-it-constants/

Hints

Hint 1

The error comes from TLC, not SANY: the module parsed fine. Read which constant TLC names, then look for where the cfg should have given it a value.

Hint 2

CONSTANTS in the module declares names. CONSTANTS in the cfg assigns values. The cfg line has the form CONSTANTS Name = value Name = value.

Hint 3 (nearly the answer)

Replace the \* GAP line with one CONSTANTS line that sets Small to the small jug's size and Big to the big jug's size, from the gap's comment.

03-01-ex2Core8 min

03-01-ex2 · Pour it back

Tier: core. Time: about 8 minutes.

This copy of KitchenMeasures.tla has five of its six moves. PourBigIntoSmall is a placeholder that can never happen, so TLC finds no violation and the action never fires.

  1. Run ./tla check 03-01-ex2. It runs several cfgs; read the first block, for KitchenMeasures.cfg. Its starter state line names the outcome the cfg's author expected, coverage-gap PourBigIntoSmall. Which action is missing? (The other blocks, the course's own tests, stop on a violation until the gap is filled.)
  2. Fill gap g1: pour the big jug into the small one until the big jug is empty or the small one is full. PourSmallIntoBig, a few lines above, is a worked example of the opposite direction.
  3. Run the check. KitchenMeasures.cfg checks TypeOK and EmptyOrFull, every action must fire, and the witness FiveCups must be reached.

A pour that moves too much can break TypeOK, and a pour that stops early can break EmptyOrFull; both are worth trying on purpose once. Those invariants miss a pour that moves nothing, or one that pours only when everything fits, because every state of this model is reachable without PourBigIntoSmall. So the check also runs the course's own test, which compares your pour, step by step, with PourSmallIntoBig read with the jugs swapped. MCKitchenMeasures.cfg runs it with jugs of 3 and 7 cups, and MCKitchenMeasures.other.cfg with 4 and 9, so sizes written as numbers instead of Small and Big fail. Leave the MC files as they are.

Hints: HINTS.md, under "03-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/03-meet-tlc/exercises/01-ex2-pour-it-back/; open it after you have a passing answer of your own.

Every cfg must meet its goal, "goal met: pass". 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 03-01-ex2 prints "goal met: pass".

./tla check 03-01-ex2

Repo chapters/03-meet-tlc/exercises/01-ex2-pour-it-back/

Hints

Hint 1

Find the line in ./tla's output that names an action. Then compare that action with its mirror image, PourSmallIntoBig.

Hint 2

The action needs a guard (there is water to pour and room to receive it) and a new value for both jugs. The amount that moves is the smaller of what the big jug holds and the room left in the small one.

Hint 3 (nearly the answer)

Copy the structure of PourSmallIntoBig and swap the roles of the jugs everywhere: in the two guards, and in the amount that moves, which is a Min of what the source holds and the room left in the target.

06Explain it

Open NOTES.md and answer the three prompts under 03-01. Write about the spec you finished in the Core exercise.

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

Compare your answers with "03-01 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "My PourBigIntoSmall moves the smaller of what the big jug holds and the room left in the small one, so one jug always ends empty or full. It relies on the jugs having no markings; if you could pour by eye, the move would need a different amount. TLC checked TypeOK and EmptyOrFull in every reachable state of the model with a 3-cup and a 7-cup jug, plus coverage and the FiveCups witness; it checked nothing about other jug sizes."

A weak answer: "I copied the other pour and swapped the jugs, and TLC passed." It describes what was typed, not why the action is right, and says nothing about what the pass covers.

Done?