03-02

Chapter 03 · Meet TLC

Your first counterexample

  • 44 min core
  • Essentials track
  • Lab: Trace Theater
  • Skills: intent, counterexamples

00Recall

Two cards from 03-01 (what SANY does, what a cfg adds) and one from chapter 02 on reading /\ and \/ as filters.

03-01-a from One command

What does SANY check, and what does it never do?

Show answer

It checks that the module is well formed, with every name defined and used correctly. It never computes a state.

03-01-b from One command

Name three things a cfg tells TLC that the module does not.

Show answer

The values of the constants, which formula is the system (SPECIFICATION, or INIT and NEXT), and which invariants to check.

02-04-a from Just enough logic

Read /\ and \/ as filters on candidate states or steps. What does each do?

Show answer

P /\ Q lets a candidate through only if both pass it, so adding a conjunct can only narrow the set. P \/ Q lets it through if either passes it, so adding a disjunct can only widen it. P => Q lets through every candidate that P rejects.

Skip recall Recall is never required.

01Commit

Back to the jugs: a 3-cup jug (Small = 3), a 7-cup jug (Big = 7), and a recipe that needs exactly 5 cups. Only the big jug can hold 5. So write down the pessimist's claim and let TLC judge it:

TLA+
NeverFive is defined as big not equal to 5

# is read "is not equal to". specs/KitchenMeasures.neverfive.cfg checks NeverFive as an invariant, in every reachable state.

Commit a

TLC checks NeverFive as an invariant. What will it report?

  1. No violation: 5 cups is impossible with a 3 and a 7.
  2. A violation, with a trace.
  3. An error: an invariant cannot mention a specific number like 5.

Answer A violation, with a trace.

Commit b

If 5 cups is reachable, the trace is a recipe. How many moves (steps, not states) does it take?

Your answer is a whole number.

Answer 8

02Lab

Lab · Trace TheaterWhat you’re looking at: the trace TLC printed when NeverFive failed, one card per state, changed variables only. Step through it with the arrow keys.

NeverFive violated

● 12● 345● 67● 89● 1011● 1213● 1415● 1617● 18

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

Trace

  1. State 1 Initial state big = 0 small = 0
  2. State 2 FillBig big 0 becomes 7
  3. State 3 PourBigIntoSmall big 7 becomes 4 small 0 becomes 3
  4. State 4 EmptySmall small 3 becomes 0
  5. State 5 PourBigIntoSmall big 4 becomes 1 small 0 becomes 3
  6. State 6 EmptySmall small 3 becomes 0
  7. State 7 PourBigIntoSmall big 1 becomes 0 small 0 becomes 1
  8. State 8 FillBig big 0 becomes 7
  9. State 9 PourBigIntoSmall big 7 becomes 5 small 1 becomes 3
Text view: every state and step
States and their successors
StatesmallbigSteps (action → state)
initial 100FillSmall → 2; FillBig → 3
230FillBig → 4; EmptySmall → 1; PourSmallIntoBig → 5
307FillSmall → 4; EmptyBig → 1; PourBigIntoSmall → 6
437EmptySmall → 3; EmptyBig → 2
503FillSmall → 7; FillBig → 3; EmptyBig → 1; PourBigIntoSmall → 2
634FillBig → 4; EmptySmall → 8; EmptyBig → 2; PourSmallIntoBig → 3
733FillBig → 4; EmptySmall → 5; EmptyBig → 2; PourSmallIntoBig → 9
804FillSmall → 6; FillBig → 3; EmptyBig → 1; PourBigIntoSmall → 10
906FillSmall → 11; FillBig → 3; EmptyBig → 1; PourBigIntoSmall → 7
1031FillBig → 4; EmptySmall → 12; EmptyBig → 2; PourSmallIntoBig → 8
1136FillBig → 4; EmptySmall → 9; EmptyBig → 2; PourSmallIntoBig → 13
1201FillSmall → 10; FillBig → 3; EmptyBig → 1; PourBigIntoSmall → 14
1327FillSmall → 4; EmptySmall → 3; EmptyBig → 15; PourBigIntoSmall → 11
1410FillSmall → 2; FillBig → 16; EmptySmall → 1; PourSmallIntoBig → 12
1520FillSmall → 2; FillBig → 13; EmptySmall → 1; PourSmallIntoBig → 17
1617FillSmall → 4; EmptySmall → 3; EmptyBig → 14; PourBigIntoSmall → 18
1702
1835

Before you step through it, cover the trace with your hand and try the puzzle yourself for two minutes. Then step through TLC's answer one card at a time and, at each card, ask which move you would have made instead. The places where your instinct and the trace disagree are the places where a shortest path does something unobvious, such as pouring water away to make room.

Or in the terminal:

shell
./tla check chapters/03-meet-tlc/specs/KitchenMeasures.neverfive.cfg
Expected output 03-meet-tlc/specs/KitchenMeasures.neverfive
03-meet-tlc/specs/KitchenMeasures.neverfive  KitchenMeasures.neverfive.cfg  (Small=3, Big=7)
parse ok
model check: invariant NeverFive violated; trace has 9 states (8 steps); 18 distinct states
expected outcome met: violation NeverFive
trace (changed variables only; ./tla trace --full shows all):
   1  Init              small = 0, big = 0
   2  FillBig           big: 0 -> 7
   3  PourBigIntoSmall  small: 0 -> 3, big: 7 -> 4
   4  EmptySmall        small: 3 -> 0
   5  PourBigIntoSmall  small: 0 -> 3, big: 4 -> 1
   6  EmptySmall        small: 3 -> 0
   7  PourBigIntoSmall  small: 0 -> 1, big: 1 -> 0
   8  FillBig           big: 0 -> 7
   9  PourBigIntoSmall  small: 1 -> 3, big: 7 -> 5

Reveal for commit a

TLC reports that NeverFive is violated and prints how: a trace of 9 states. The last state has five cups in the big jug. The pessimist was wrong.

Reveal for commit b

The trace has 8 steps, one per move. It has one more state than steps, because the first state is where you start, before any move.

03Reason

If you picked "no violation", you ran the numbers in your head, found it hard, and concluded it was impossible. TLC has no head. It enumerates.

Here is the trace again, as ./tla prints it, with only the variables that changed:

  1. State 1 Initial state small = 0 big = 0
  2. State 2 FillBig big 0 becomes 7
  3. State 3 PourBigIntoSmall small 0 becomes 3 big 7 becomes 4
  4. State 4 EmptySmall small 3 becomes 0
  5. State 5 PourBigIntoSmall small 0 becomes 3 big 4 becomes 1
  6. State 6 EmptySmall small 3 becomes 0
  7. State 7 PourBigIntoSmall small 0 becomes 1 big 1 becomes 0
  8. State 8 FillBig big 0 becomes 7
  9. State 9 PourBigIntoSmall small 1 becomes 3 big 7 becomes 5

A counterexample is a behavior, or the start of one, that shows a claim is false. For an invariant, the start is enough: once a state breaks the claim, nothing that happens later can repair it. So TLC prints the path from an initial state to the first bad state it found, and stops.

Read it as a recipe. Fill the big jug, pour off what the small jug takes, empty the small jug, and repeat until a single cup is left in the big one. Park that cup in the small jug, fill the big jug again, and top up the small one. What stays behind in the big jug is the answer. Every line names the action that produced the state, so the trace is also a list of which parts of your spec fired, in order.

Write something before openingthe trace shows only the variables that changed. When could that hide something you need?

When the variable you care about is one that did not change. Here both jugs change often, so nothing hides. In a spec with ten variables, the one that stayed put while everything else moved can be the whole story: the lock nobody released, the message nobody sent. ./tla trace --full prints every variable in every state.

Write something before openingTLC found this recipe. Is there a shorter one?

No. TLC searches breadth-first: first every state one move from the start, then every state two moves away, and so on. It checks the invariant in each state as it finds it. The first state with five cups that it meets is therefore at the smallest possible number of moves. Any shorter recipe would have ended in a state TLC had already checked, and none of those held five cups.

That fold depends on one detail of how the course runs TLC. ./tla always passes -workers 1: one thread does all the exploring, taking states strictly in the order it found them.

Write something before openingthis run reports fewer distinct states than the passing run in 03-01. Why?

TLC stops at the first violation. This run found 18 distinct states before it stopped; the passing run, which had nothing to stop it, found all 20. A failing run's numbers describe how far TLC got, not the size of the model.

Compare the other invariant from 03-01, EmptyOrFull:

TLA+
EmptyOrFull is defined as
    or small in {0, Small}
    or big in {0, Big}

\in is read "is an element of". This one holds everywhere, and for a reason you can say out loud: every move ends by filling, emptying, or pouring until a jug is empty or full. The two invariants show the two jobs an invariant can do. EmptyOrFull states something the design must keep true; a violation would be a bug. NeverFive states something you suspect is false, so that TLC will show you how. The next lesson turns that second job into a rule.

So "counterexample" does not mean "bug". It means "a behavior that contradicts the claim you wrote". Whether that is bad news depends on why you wrote the claim. Before every run, say which job your invariant is doing. You will read the result differently, and you will notice when a result does not fit either job.

04In your editor

The failing cfg is chapters/03-meet-tlc/specs/KitchenMeasures.neverfive.cfg. Check this lesson's exercises with:

shell
./tla check 03-02

The failing run prints its verdict, then the trace:

Output 03-meet-tlc/specs/KitchenMeasures.neverfive
03-meet-tlc/specs/KitchenMeasures.neverfive  KitchenMeasures.neverfive.cfg  (Small=3, Big=7)
parse ok
model check: invariant NeverFive violated; trace has 9 states (8 steps); 18 distinct states
expected outcome met: violation NeverFive
trace (changed variables only; ./tla trace --full shows all):
   1  Init              small = 0, big = 0
   2  FillBig           big: 0 -> 7
   3  PourBigIntoSmall  small: 0 -> 3, big: 7 -> 4
   4  EmptySmall        small: 3 -> 0
   5  PourBigIntoSmall  small: 0 -> 3, big: 4 -> 1
   6  EmptySmall        small: 3 -> 0
   7  PourBigIntoSmall  small: 0 -> 1, big: 1 -> 0
   8  FillBig           big: 0 -> 7
   9  PourBigIntoSmall  small: 1 -> 3, big: 7 -> 5

After any check that printed a trace, two more commands help. ./tla trace --full prints the last trace again with every variable in every state, not only the changed ones. ./tla trace --json writes .tla-out/trace-theater.json, which you can open in the Trace Theater lab on the course site, by file picker or by dragging it onto the lab. The file stays on your machine: the lab parses it in your browser and uploads nothing.

If your own spec's trace is long, read it backwards. Start at the bad state and ask which variable makes the claim false. Then walk up until you find the step that set that variable. That step's action is where to look first.

05Practice

Warm-up. Ask TLC a new question: can the big jug hold exactly 4 cups? Write the claim and read the recipe.

Core. A teammate replaced both pours with "one cup at a time" and says nothing important changed. TypeOK agrees. Write the invariant that disagrees.

In both exercises, the starter passes. That is not a mistake in the exercise: a claim of TRUE holds everywhere, and TLC says so (it even warns you that the claim mentions no variables). Your answer turns a check that cannot fail into one that can.

03-02-ex1Warm-up5 min

03-02-ex1 · Never four

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

Can the big jug hold exactly 4 cups? Ask TLC the way the lesson asked about 5.

  1. Run ./tla check 03-02-ex1. The starter passes. Read the TLC warning it prints: what does TLC think of a claim that is nothing but TRUE?
  2. Fill gap g1 in KitchenMeasures.tla: claim that the big jug never holds exactly 4 cups.
  3. Run the check and read the trace as a recipe. Before you run it, write down how many moves you think the shortest recipe takes.

The cfg already names INVARIANT NeverFour. If you change only the module and the result does not change, check that you deleted the \* GAP marker. The check also runs two tests the course wrote. In KitchenMeasures.grader.cfg, a kitchen where four cups can never happen, your claim must pass. MCKitchenMeasures.cfg goes through every pair of amounts TypeOK allows, and your claim must be false in exactly the pairs with 4 cups in the big jug, so a claim about another amount fails there. Leave those three files as they are.

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

All three cfgs must meet their goals: "goal met: violation NeverFour" for KitchenMeasures.cfg, and "goal met: pass" for KitchenMeasures.grader.cfg and MCKitchenMeasures.cfg. 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-02-ex1 prints "goal met: violation NeverFour".

./tla check 03-02-ex1

Repo chapters/03-meet-tlc/exercises/02-ex1-never-four/

Hints

Hint 1

What would TLC need to find to refute "the big jug never holds 4 cups"? Write the claim so that exactly that state breaks it.

Hint 2

# is read "is not equal to". The claim is about the big jug only, because the small jug cannot hold 4 cups.

Hint 3 (nearly the answer)

NeverFour has the same shape as NeverFive: one variable, one operator, one number. Replace the TRUE and delete its \* GAP marker.

03-02-ex2Core8 min

03-02-ex2 · One-cup pours

Tier: core. Time: about 8 minutes.

A teammate rewrote both pours to move one cup at a time: "Same amounts reachable, simpler actions." TypeOK passes and the witness FiveCups is reached, so the review looks green.

But the teammate's jugs have no markings either. Pouring one cup at a time means pouring by eye.

  1. Run ./tla check 03-02-ex2. It passes.
  2. Fill gap g1: EmptyOrFull says that after every move, at least one jug is empty or full. Write it as a state predicate.
  3. Run the check. Read the counterexample: which move broke it, and what would you write in the review?

You are not fixing the teammate's pours here. Your goal is the counterexample that shows the change is not harmless. The check also runs MCKitchenMeasures.cfg, the course's own test of your claim: the same jugs with the lesson's real pours, where EmptyOrFull must hold. A claim that fails there is not the requirement. Leave those two MC files as they are.

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

Both cfgs must meet their goals: "goal met: violation EmptyOrFull" for KitchenMeasures.cfg, and "goal met: pass" for MCKitchenMeasures.cfg. 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-02-ex2 prints "goal met: violation EmptyOrFull".

./tla check 03-02-ex2

Repo chapters/03-meet-tlc/exercises/02-ex2-one-cup-pours/

Hints

Hint 1

Ask what an unmarked jug lets you know. After a real pour, how do you know how much water is where?

Hint 2

The claim is a disjunction: the small jug is at one of its two ends, or the big jug is at one of its two ends. \in is read "is an element of".

Hint 3 (nearly the answer)

Write EmptyOrFull as two disjuncts, one per jug, each saying the jug is at one of its two ends. Each disjunct uses \in and a set of two values, one of them a constant.

06Explain it

Open NOTES.md and answer the three prompts under 03-02, about 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-02 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "EmptyOrFull says one jug is always empty or full, which is true for real pours because a pour stops only at an empty source or a full target. The teammate's one-cup pour breaks it on the first pour that leaves water in both jugs, and TLC's shortest trace shows that within a few moves. If the jugs had markings, one-cup pours would be legitimate and the invariant would be the wrong requirement. TLC checked one model and stopped at the first violation; it did not explore the rest of the teammate's model."

A weak answer: "TLC found a counterexample, so the teammate is wrong." It does not say what the invariant means or why it matters, and "wrong" depends on the assumption about markings.

Done?