03-04
Reading the numbers
00Recall
One card on vacuous passes from 03-03, one from chapter 02 on what an action says about a variable it does not mention, and one from 00-03 on counting the steps of a trace. Keep that last one in mind: this lesson leans on it.
03-03-a from Break it on purpose
TLC reports no violation and zero distinct states. What has it shown about your invariants?
Show answer
Nothing. With no reachable states, every invariant holds vacuously.
02-02-a from UNCHANGED and the frame
An action never mentions the variable alarm. In the logic, what may alarm be after a step of that action?
Show answer
Anything at all. An action constrains only what it says, so with no conjunct about alarm', every value makes it true. UNCHANGED alarm, which means alarm' = alarm, is how you say it stays.
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
Every TLC run ends with a few numbers. This lesson is about why you should predict them first.
Take the passing check from 03-01: KitchenMeasures.cfg, with Small = 3 and Big = 7, checking TypeOK and EmptyOrFull. Each jug holds a whole number of cups, from empty to full.
Commit a
TLC explores KitchenMeasures.cfg and nothing fails. How many distinct states will it report?
Your answer is a whole number.
Answer 20
- Exactly right
- Right. You counted the states the moves can reach, not the ones the jugs could hold in principle.
- Within 10 %
- Within 10 %. Recount the states where one jug is partly full: the other must then be empty or full.
- Under by more than half
- Under by more than half. You probably ruled out odd or partial amounts. Pours leave all sorts of amounts behind; the trace in 03-02 passes through several.
- More than double
- More than double. You probably counted the same pair of amounts once for every way of reaching it. TLC counts it once.
- Anything else
- If you multiplied the sizes of the two ranges, you counted every pair TypeOK allows. Moves never reach most pairs with both jugs partly full.
Commit b
The same run reports a depth: the number of states on a shortest path from the start to the reachable state farthest from it. What depth will it report?
Your answer is a whole number.
Answer 10
- Exactly right
- Right. Some pair of amounts is farther from the start than five cups, and TLC counts the path to it in states.
- Within 10 %
- Within 10 %. If you were one under, you counted moves; TLC counts depth in states, so an initial state is at depth one.
- Under by more than half
- Under by more than half. You probably measured the distance to one state you had in mind, such as a full jug. Depth is about the farthest reachable state, whichever it is.
- More than double
- More than double. You probably counted a long route that visits states more than once. Depth follows a shortest path to each state, and no state appears twice on one.
- Anything else
- Compare with the five-cup recipe from 03-02. If your answer is its length, you measured the distance to five cups; depth measures the distance to the farthest state, which need not be the one with five cups.
02Lab
./tla check chapters/03-meet-tlc/specs/KitchenMeasures.cfg03-meet-tlc/specs/KitchenMeasures03-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)The summary line holds three numbers. The raw log, .tla-out/03-meet-tlc__specs__KitchenMeasures.log, holds TLC's own wording of them: "states generated", "distinct states found", and "the depth of the complete state graph search".
Reveal for commit a
TLC found 20 distinct states. The reasoning section bounds that number with EmptyOrFull, and warns that the bound is not always reached.
Reveal for commit b
The depth is 10. TLC counts it in states, with an initial state at depth one, so the farthest state needs one move fewer than that to reach. The five-cup recipe is not the longest shortest path.
03Reason
If you multiplied the two ranges, you counted the states TypeOK allows. That is a box drawn around the reachable states, not the states themselves. If you counted more than that, you probably counted routes.
Start with distinct states. A state is one value for every variable; here, one pair of amounts. TLC stores each pair once, however many routes lead to it.
You can bound the count with the invariant you already trust. EmptyOrFull says one jug is always empty or full, so count the states it allows: those with the small jug empty or full, plus those with the small jug partly full and the big one empty or full.
With the small jug at 0 or 3, the big jug can hold any of 0 to 7 cups: 2 × 8 = 16 states. With the small jug at 1 or 2, the big jug must be at 0 or 7: 2 × 2 = 4 more. So at most 20 states are reachable.
TLC reports 20, so with these jugs every state EmptyOrFull allows is reachable. That is a finding, not a given: an invariant says which states are allowed, not which ones the moves reach. With the Warm-up's jugs, the same kind of bound overshoots. When your count and TLC's disagree, one of you has misunderstood the spec. Sometimes it is you, and the disagreement teaches you something about the system. Sometimes it is the spec, and you have found a bug before any invariant failed.
Next, states generated: 75. That is bigger than the distinct count, and it is not double counting by mistake.
Write something before openingwhat does the gap between generated and distinct tell you about the jugs?
Each state has several moves out of it, and many lead back to states TLC already knows: refilling, emptying, pouring back and forth. A large gap means a densely connected graph.
Then depth: 10. For every reachable state there is a shortest path to it from an initial state. Depth is the length of the longest of those shortest paths, counted in states.
Write something before openingthe 5-cup recipe in 03-02 was the shortest possible. Why is the depth bigger than its trace?
The depth belongs to the farthest state, and the farthest state is not the one with 5 cups. The recipe's trace has 9 states; some other pair of amounts needs a longer shortest path. Depth bounds every invariant or deadlock trace TLC prints for this model: no shortest path is longer.
Last, states versus steps, because it is easy to get wrong. A trace of k states has k − 1 steps: the first state comes before any move. The 03-02 trace had 9 states and 8 steps. The course always says which one it means.
Write something before openingif you reorder the disjuncts of Next, can the failing NeverFive run report a different distinct count? A different trace length?
The trace length cannot change: it is the shortest distance from the start to a five-cup state, a fact about the graph. The distinct count can, because TLC stops in the middle of a level, and how much of that level it has explored depends on the order in which it generates successors. specs/ReorderedKitchen.tla lists the six disjuncts of Next in reverse order. Checked against NeverFive, the original stops after 18 distinct states with a trace of 9 states; the reordered spec stops after 17 distinct states with a trace of 9 states. A failing run's counts record how far TLC got, which is one more reason the course quotes them only from generated facts.
The raw log also reports "states left on queue", found but not yet explored (none, after a complete run), and the estimated chance that two distinct states shared a fingerprint, which would make TLC skip one. At this size that chance is negligible.
Try the habit once more on the other spec of this chapter, specs/Thermostat.tla, before you run it. The heater is on or off, and Comfortable says the room stays within Band = 2 degrees of Target = 20.
That is five temperatures and two heater positions, so at most 5 × 2 = 10 states. Are they all reachable? The room starts at the target with the heater off, cools to the bottom of the band, switches on, warms to the top, switches off, and cools again, passing every temperature both ways.
TLC reports 10 distinct states and a depth of 10. The depth is worth a second look: the whole graph is one long loop, so the farthest state is a long way round.
Put together, the numbers are a cheap vacuity check that TLC gives you for free. A model with zero distinct states (03-03) is empty. A model with far fewer states than you predicted is probably stuck somewhere. A model with far more is probably letting something vary that you meant to pin down. Predict first, every time, and treat a surprise as a finding.
04In your editor
Check this lesson's exercises with:
./tla check 03-04Before each run, write your predicted distinct count and depth in NOTES.md. Afterwards, write down what TLC said and, if they differ, why.
05Practice
Warm-up. Change the jugs to a 4-cup and a 6-cup jug. Predict the distinct count, and whether five cups is still possible, before you run.
Core. Add a move that tips one cup out of the big jug. Predict what happens to the count and to EmptyOrFull.
Write every prediction down before you run, even the ones you are sure of. A prediction you only thought about is easy to revise afterwards without noticing.
03-04-ex1Warm-up5 min
03-04-ex1 · Other jugs
Tier: warm-up. Time: about 5 minutes.
Same spec, different kitchen: a 4-cup small jug and a 6-cup big jug.
Before you change anything, write three predictions in NOTES.md:
- Is 5 cups still possible?
- How many distinct states will TLC report?
- What depth will it report?
Then:
- Fill gap g1 in
KitchenMeasures.cfgwith the new sizes. - Run
./tla check 03-04-ex1and compare TLC's numbers with yours. Some other sizes would pass too, so check the constants named on the first line. - In 03-03-ex2, a passing
NeverFivewas a bug report. Why is a passingNeverFivefine here? Name the evidence in the output that does not depend onNeverFiveand shows that the model is not stuck.
Hints: HINTS.md, under "03-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/03-meet-tlc/exercises/04-ex1-other-jugs/; 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-04-ex1 prints "goal met: pass".
./tla check 03-04-ex1
Repo chapters/03-meet-tlc/exercises/04-ex1-other-jugs/
Hints
Hint 1
Write your predictions first. The gap itself is one line: the new sizes from the gap's comment.
Hint 2
Look at what every move does to the amounts. Can a fill, an empty or a pour with a 4-cup and a 6-cup jug ever leave an odd number of cups in either jug? What does that say about 5, and about how many amounts each jug can hold?
Hint 3 (nearly the answer)
Replace the placeholder line with a CONSTANTS line setting Small to 4 and Big to 6, without the \* GAP marker. NeverFive should now pass, and the witness BothFull should be reached.
03-04-ex2Core8 min
03-04-ex2 · Spill a cup
Tier: core. Time: about 8 minutes.
The lesson's model leaves spills out on purpose. This exercise puts one back: SpillOne tips exactly one cup out of the big jug.
Before you write it, predict in NOTES.md:
- Will
EmptyOrFullstill hold? - If not, how many states long is the shortest trace that breaks it?
- If nothing stopped TLC, would the spec with
SpillOnereach more pairs of amounts than the lesson's spec, or the same ones? (The real run stops at the first violation, so its distinct count only says how far it got.)
Then:
- Fill gap g1:
SpillOneneeds water in the big jug, takes one cup out of it, and leaves the small jug alone. - Run
./tla check 03-04-ex2. Compare the first block's trace and counts with your predictions.
Any spill that breaks EmptyOrFull meets the first block's goal, including one from the wrong jug or one of two cups. So the check also runs MCKitchenMeasures.cfg, the course's own test: it leaves out EmptyOrFull, explores every reachable state, and checks in each that SpillOne takes exactly one cup from the big jug whenever it holds any. Because nothing stops it early, its distinct count answers your third prediction. Leave the two MC files as they are.
If your prediction of the trace was too short, find the state you expected to break the invariant and check EmptyOrFull in it by hand.
Hints: HINTS.md, under "03-04-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/03-meet-tlc/exercises/04-ex2-spill-a-cup/; 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.
Done when: ./tla check 03-04-ex2 prints "goal met: violation EmptyOrFull".
./tla check 03-04-ex2
Repo chapters/03-meet-tlc/exercises/04-ex2-spill-a-cup/
Hints
Hint 1
SpillOne has a guard and changes one variable. Which one, and when is the move possible?
Hint 2
Every action must say what happens to every variable. The small jug does not change.
Hint 3 (nearly the answer)
Three conjuncts: a guard on big, a new value for big written from its old value, and UNCHANGED for the other jug. Then predict: in which state does a spill first leave both jugs partly full?
06Explain it
Open NOTES.md and answer the three prompts under 03-04, about the Core exercise.
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-04 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "SpillOne takes a cup from the big jug whenever it has one. I predicted the trace would end at the first spill, but a spill cannot break EmptyOrFull while the small jug is empty or full; the first violation needs the small jug partly full as well, which is why the trace is longer. The counts describe one model, not the time it takes, and a spill is a modelling choice: real kitchens also spill from the small jug."
A weak answer: "TLC found more states and a violation." It repeats the output without explaining the trace's length or comparing with the prediction.
Done?