03-05

Chapter 03 · Meet TLC

TypeOK, and the check you didn't configure

  • 45 min core
  • Essentials track
  • Milestone 2
  • Skills: intent, evidence

00Recall

One card on generated versus distinct states, one on why an invariant's trace stops at the bad state, and one from chapter 01 on why a behavior never ends. That last one matters today: a system that stops must still have a next state.

03-04-a from Reading the numbers

TLC reports more states generated than distinct states. Did it count something twice by mistake?

Show answer

No. Generated counts every successor it computed, including repeats; distinct counts each state once.

03-02-a from Your first counterexample

Why does a TLC trace for an invariant violation stop at the bad state instead of showing a whole infinite behavior?

Show answer

Once a state breaks an invariant, nothing later can repair it, so the path to that state is all the evidence needed.

01-03-a from Behaviors are infinite

A print job reaches done, and nothing more can happen to it. What is the next state of its behavior?

Show answer

Done again, and again forever. Every behavior is infinite, so a system that stops still has a next state: the same one. The step from done to done changes nothing and is called a stuttering step.

Skip recall Recall is never required.

01Commit

Back to specs/Thermostat.tla, which you counted in 03-04. A room, a heater, and a controller that keeps the room within Band = 2 degrees of Target = 20. The room warms one degree per step while the heater is on and cools one degree while it is off; the controller switches the heater at the edges of the band. The requirement is Comfortable: the temperature stays inside the band.

A teammate's copy, specs/DriftingThermostat.tla, has an off-by-one: the room can warm one degree past the top of the band. Its cfg lists a single invariant:

cfg
INVARIANT TypeOK

Commit a

DriftingThermostat.cfg names only INVARIANT TypeOK. The spec defines Comfortable, which is false once the room drifts. What does TLC report?

  1. A violation of Comfortable.
  2. No violation, so TLC has shown that this thermostat keeps the room comfortable.
  3. A violation of TypeOK, since the room is too warm.
  4. No violation.

Answer No violation.

A second teammate's copy, specs/StuckThermostat.tla, makes TLC report a deadlock: a reachable state in which no Next step is possible, so only stuttering steps, which change nothing, remain. The teammate's fix was to add one line to the cfg:

cfg
CHECK_DEADLOCK FALSE

Commit b

With CHECK_DEADLOCK FALSE added, what does the run of StuckThermostat say?

  1. No violation, after fewer states than the working thermostat has.
  2. Still a deadlock: the flag changes only how TLC prints it.
  3. No violation, so the deadlock was a false alarm all along.

Answer No violation, after fewer states than the working thermostat has.

02Lab

The drifting thermostat, first with the teammate's cfg, then with the requirement named:

shell
./tla check chapters/03-meet-tlc/specs/DriftingThermostat.cfg
./tla check chapters/03-meet-tlc/specs/DriftingThermostat.comfort.cfg
Expected output 03-meet-tlc/specs/DriftingThermostat.comfort
03-meet-tlc/specs/DriftingThermostat.comfort  DriftingThermostat.comfort.cfg  (Target=20, Band=2)
parse ok
model check: invariant Comfortable violated; trace has 9 states (8 steps); 9 distinct states
expected outcome met: violation Comfortable
trace (changed variables only; ./tla trace --full shows all):
   1  Init      temp = 20, heater = "off"
   2  Cool      temp: 20 -> 19
   3  Cool      temp: 19 -> 18
   4  SwitchOn  heater: "off" -> "on"
   5  Warm      temp: 18 -> 19
   6  Warm      temp: 19 -> 20
   7  Warm      temp: 20 -> 21
   8  Warm      temp: 21 -> 22
   9  Warm      temp: 22 -> 23

Reveal for commit a

With only TypeOK named, TLC finds no violation in 12 distinct states. Name Comfortable as well and it is violated, with a trace of 9 states that ends one degree above the band.

The stuck thermostat, before and after the teammate's fix:

shell
./tla check chapters/03-meet-tlc/specs/StuckThermostat.cfg
./tla check chapters/03-meet-tlc/specs/StuckThermostat.masked.cfg
Expected output 03-meet-tlc/specs/StuckThermostat
03-meet-tlc/specs/StuckThermostat  StuckThermostat.cfg  (Target=20, Band=2)
parse ok
model check: deadlock reached; trace has 3 states (2 steps); 3 distinct states
expected outcome met: deadlock
trace (changed variables only; ./tla trace --full shows all):
  1  Init  temp = 20, heater = "off"
  2  Cool  temp: 20 -> 19
  3  Cool  temp: 19 -> 18

Reveal for commit b

Before the fix, TLC reports a deadlock after a trace of 3 states: the room cools to the bottom of the band, and nothing can happen next. After it, no violation, in 3 distinct states. The working thermostat has 10. The line expected outcome met: coverage-gap SwitchOn says the cfg's author expected SwitchOn never to fire, and it did not. Read the spec and you can see that Warm and SwitchOff cannot fire either: both need the heater on.

03Reason

If you expected a Comfortable violation, you assumed TLC checks what the spec defines. It checks what the cfg names, plus a few things it always checks. Here is the whole picture in three rows:

WhenWhat TLC checks
Always, with no cfg lineevery ASSUME (03-01); deadlock, unless the cfg says CHECK_DEADLOCK FALSE; and every expression it evaluates, stopping with an error on one it cannot evaluate
Only if the cfg names iteach INVARIANT and each PROPERTY
Nevereverything else the module defines, however important its name sounds

The gap between the second row and the third has a name in this course: the check you didn't configure. The requirement exists in the module, and nobody asked TLC to check it.

Start with TypeOK:

TLA+
TypeOK is defined as
    and temp in Nat
    and heater in {"on", "off"}

TLA+ has no types (lesson 05-05 comes back to what that means). TypeOK is an ordinary invariant that says what kind of value each variable holds, and the course always lists it first. It earns its place by catching a whole family of slips early: a misspelt string, a counter that goes negative, a variable set to the wrong kind of thing. TLC reports the violation in the state where the bad value first appears, not later, where it finally breaks something. In the Warm-up exercise, a teammate's thermostat deadlocks after a trace of 13 states. With TypeOK written, TLC stops after 9, on the step that wrote the bad value, and the steps in between are the bug travelling.

What TypeOK does not do is state the requirement. "The room stays comfortable" needs its own invariant, and that invariant needs its own INVARIANT line.

Write something before openingStuckThermostat's run with CHECK_DEADLOCK FALSE explored fewer states than the working thermostat. Where did the others go?

They were never reached. The room cools to the bottom of the band and stops, because SwitchOn waits for a temperature that Cool never allows. With deadlock checking off, TLC treats that dead end as fine, so the run "passes" having seen only the cooling half of the system.

Stopping can be intended. A batch job that has processed its last item has nothing left to do, and a spec of it should say so. Then turning the check off is honest, and the comment says why. It is also blunt: the flag silences every dead end, not only the intended one, so a batch job that also stalls halfway would pass. A sharper fix keeps the check on and adds a disjunct to Next that lets the finished state stutter, such as Done == finished /\ UNCHANGED vars. The teammate's comment would have to say "TLC kept complaining", which is not a reason.

When a run surprises you, the standard module TLC has debugging aids. The course keeps them in a wrapper, specs/MCThermostat.tla, so the model itself stays clean:

TLA+
Shallow is defined as TLCGet("level") < 4

Evaluated in an invariant, TLCGet("level") is the number of states on the path from an initial state to the current one, counting both ends, so an initial state is at level 1. (While TLC evaluates Init itself, it is zero.) specs/MCThermostat.cfg checks Shallow as an invariant.

Write something before openingwhere does Shallow stop TLC, and what will the trace show?

At the first state four levels deep, which is three steps from the start. Like any violated invariant, it comes with the path to that state, so you see what the system does in its first few steps without reading the raw log:

Output 03-meet-tlc/specs/MCThermostat
03-meet-tlc/specs/MCThermostat  MCThermostat.cfg  (Target=20, Band=2)
parse ok
model check: invariant Shallow violated; trace has 4 states (3 steps); 4 distinct states
expected outcome met: violation Shallow
trace (changed variables only; ./tla trace --full shows all):
  1  Init      temp = 20, heater = "off"
  2  Cool      temp: 20 -> 19
  3  Cool      temp: 19 -> 18
  4  SwitchOn  heater: "off" -> "on"

Two more aids sit in the same wrapper. ShowState, built on PrintT, prints each distinct state once, when TLC first checks it, and never fails (MCThermostat.print.cfg). AlarmBelowTarget, built on Assert, stops TLC with an error quoting its message when its condition is false (MCThermostat.assert.cfg). ./tla's summary never shows the printed states. For the failed Assert it shows TLC's one-line error, which names the invariant but not your message, then the verdict (which repeats the message only because the cfg's @expect quotes it) and the trace. The printed states, and TLC's full report with the message, are in the raw log whose path it prints. CONCEPTS.md sums up all three.

Write something before openingthe working Thermostat passes with Comfortable named. Can you now say the thermostat is correct?

No. You can say: TLC found no violation of TypeOK or Comfortable in the model with Target = 20 and Band = 2, in which every action fired and both witnesses were reached. That sentence is longer, and every word of it is true.

04In your editor

The specs are in chapters/03-meet-tlc/specs/. Check this lesson's exercises with:

shell
./tla check 03-05

The working thermostat, with both invariants, every action, and both witnesses:

Output 03-meet-tlc/specs/Thermostat
03-meet-tlc/specs/Thermostat  Thermostat.cfg  (Target=20, Band=2)
parse ok
model check: no violation found in 10 distinct states (11 generated, depth 10)
expected outcome met: pass
coverage: all 4 actions fired
witness HeaterRuns reached (trace of 4 states)
witness WarmedToTop reached (trace of 8 states)

When a run surprises you, reach for the cheapest aid first. Read the trace, all of it, with ./tla trace --full. If a value looks wrong, check that TypeOK would have caught it, and tighten TypeOK if not. If you cannot tell what the system does at all, stop it early with a TLCGet("level") invariant and read the first few steps. Use PrintT last: it prints in the order TLC explores, which is rarely the order you think in.

Before you trust any pass in this course, ask the three questions of this chapter. Did the cfg name the requirement? Did every action fire, and was every witness reached? And if deadlock checking is off, does the comment give a reason that is about the system rather than about TLC?

05Practice

Warm-up. A teammate's thermostat deadlocks and they cannot see why. Write TypeOK, and TLC points at the real cause.

Core. A teammate's cfg forgot the requirement, and their spec has a bug that the requirement would catch. Configure the check, then fix the spec.

Stretch. A microwave oven, from a blank skeleton, including the cfg.

03-05-ex1Warm-up5 min

03-05-ex1 · Type the thermostat

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

A teammate's thermostat deadlocks. TLC prints a trace that ends with the room at the bottom of the band and nothing able to happen next. The teammate has stared at Cool and SwitchOn, the two actions that should fire there, and cannot see anything wrong with either.

  1. Run ./tla check 03-05-ex1 and read the block for Thermostat.cfg. Then read the last state of its trace with every variable, not only the ones that changed. ./tla trace --full replays the last trace a check wrote, and this check runs two cfgs, so run the teammate's cfg on its own first:

sh ./tla check chapters/03-meet-tlc/exercises/05-ex1-type-the-thermostat/Thermostat.cfg ./tla trace --full

  1. Fill gap g1 with a real TypeOK: temp is a natural number, and heater is one of the two strings "on" and "off".
  2. Run the check again and compare the two traces. Where does each one stop? Which action does the new trace end on, and what is wrong in it?
  3. Why did the room keep cooling for a while after the bad value appeared, instead of getting stuck at once? Read the guard of Cool.

Your goal is the TypeOK violation, not the fix. Any TypeOK that the bad value breaks would meet that goal, even one with no temp conjunct, so the check also runs MCThermostat.cfg, the course's own test: it pairs temperatures in and out of type with heater values in and out of type, and your TypeOK must hold in exactly the pairs step 2 describes. Leave the two MC files as they are. Write the fix in your notes; then try it in a copy if you like.

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

Both cfgs must meet their goals: "goal met: violation TypeOK" for Thermostat.cfg, and "goal met: pass" for MCThermostat.cfg.

Done when: ./tla check 03-05-ex1 prints "goal met: violation TypeOK".

./tla check 03-05-ex1

Repo chapters/03-meet-tlc/exercises/05-ex1-type-the-thermostat/

Hints

Hint 1

Read the last state of the deadlock trace, all of it. Is every value one the spec's actions know how to handle?

Hint 2

TypeOK is a conjunction with one conjunct per variable. The heater's conjunct uses \in with a set of two strings.

Hint 3 (nearly the answer)

Once TypeOK is real, TLC stops at the state where a bad value first appears. Compare the string that action writes with the strings TypeOK allows, then read Cool's guard to see why the room kept cooling afterwards.

03-05-ex2Core8 min

03-05-ex2 · Configure the check

Tier: core. Time: about 8 minutes.

A teammate's thermostat passes with their cfg, Thermostat.cfg. That cfg names TypeOK and nothing else.

  1. Run ./tla check 03-05-ex2. It checks two cfgs. The teammate's passes: open it and write down what it checked. The second, Thermostat.grader.cfg, is the course's check of your work, and it already fails. Leave that file alone.
  2. Fill gap g1 in Thermostat.cfg so that TLC also checks the requirement, Comfortable. Run the check: read the counterexample.
  3. Fill gap g2 in Thermostat.tla: the room cools only while it is above the bottom of the comfort band. Run the check again.

Both gaps must be filled. If you fix only the spec, the teammate's cfg still never names the requirement; if you fix only the cfg, you have a red check and a bug. ./tla cannot tell whether you filled g1: deleting its \* GAP line without adding an INVARIANT line also passes, and the grader checks only the spec. Check g1 yourself.

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

Both cfgs must print "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-05-ex2 prints "goal met: pass".

./tla check 03-05-ex2

Repo chapters/03-meet-tlc/exercises/05-ex2-configure-the-check/

Hints

Hint 1

Start with the cfg. Which invariant does the module define for the requirement, and is it named in the cfg?

Hint 2

Once Comfortable is checked, the trace ends one degree outside the band. Which action took it there, and what guard should have stopped it?

Hint 3 (nearly the answer)

In the cfg, add an INVARIANT line for the requirement. In Cool, one comparison lets the room take one step too many; make it stop at the bottom of the band. Thermostat.grader.cfg passes once the spec is right.

03-05-ex3Stretch40 min · mastery path

03-05-ex3 · Microwave door

Tier: stretch. Time: about 40 minutes.

A blank page, almost. Microwave.tla names the variables, the actions and two witnesses; every body is a placeholder. You write the rest, including the cfg.

The oven:

  • The door is open or closed. Opening it cuts the power at once; any time left stays on the timer. Closing it changes nothing else.
  • While nothing is heating, you can set the timer to any whole number of ticks from 1 to MaxTime.
  • Start turns the element on, but only with the door closed and time left.
  • While heating, each tick takes one off the timer, and the tick that reaches zero also turns the element off.
  • The requirement, DoorInterlock: the element is never on while the door is open.

Steps:

  1. Run ./tla check 03-05-ex3. Read the starter error in the block for Microwave.cfg: what does TLC say about an Init that is FALSE?
  2. Fill gaps g1 to g8 in Microwave.tla, then gap g1 in Microwave.cfg: the spec, MaxTime = 3, and every invariant worth checking.
  3. Run the check until it passes with full coverage and both witnesses.
  4. Break DoorInterlock on purpose: delete one guard and confirm TLC catches it. Then, from the repository root, run ./tla mutate chapters/03-meet-tlc/exercises/05-ex3-microwave-door/Microwave.tla and read the survivors. For each one, decide whether it is a gap in your properties or a change that does not matter.

Hints: HINTS.md, under "03-05-ex3 hint 1" to "hint 3". The solution is in solutions/chapters/03-meet-tlc/exercises/05-ex3-microwave-door/; 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.

Your cfg checks only the invariants you name, so the check also runs cfgs the course wrote. MCMicrowave.cfg runs your Spec and checks that the oven starts closed, idle and unset; that the element is never on while the door is open, nor with the timer at zero; and that a tick that is not the last one takes one off and keeps the element on. MCMicrowave.interlock.cfg checks your DoorInterlock in every state with time on the timer: it must be false exactly when the element is on and the door is open. MCMicrowaveType.cfg checks your TypeOK on values in and out of type. None of them checks every line of the oven list (that opening the door keeps the time, that closing it changes nothing else, or that the timer is set only while nothing is heating), so step 4 still matters. Nor can ./tla see which invariants your own Microwave.cfg names: deleting its \* GAP lines without adding an INVARIANT line also passes, so check that part of step 2 yourself. Leave the MC files as they are.

Every cfg must meet its goal, "goal met: pass".

Done when: ./tla check 03-05-ex3 prints "goal met: pass".

./tla check 03-05-ex3

Repo chapters/03-meet-tlc/exercises/05-ex3-microwave-door/

Hints

Hint 1

Write TypeOK and Init first, and make every action body FALSE for now. The run then reports a deadlock in the initial state, which is expected until an action exists. Add one action at a time and run after each.

Hint 2

Every action must give a value to all three variables, either directly or with UNCHANGED. OpenDoor changes two of them; SetTimer chooses its new value with \in.

Hint 3 (nearly the answer)

DoorInterlock relates two variables and says something only while the element is on. In Tick, the new value of heating can be written in terms of the new value of timer. The cfg needs the behavior spec, the constant, and an INVARIANT line for each invariant you wrote.

06Explain it

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

After you writeCheck your explanation

A good answer: "The cfg named only TypeOK, which a temperature below the band still satisfies, so the drift went unnoticed; naming Comfortable exposed it, and a strict guard on Cool fixed it. The fix assumes the controller reacts before the room cools further. TLC found no violation of TypeOK or Comfortable with Target = 20 and Band = 2, with every action firing and the witness reached; it says nothing about real time, heat flow or a sensor that misreads."

A weak answer: "TLC now passes, so the thermostat is correct." It confuses a passing check with correctness and does not say which invariants or constants the pass covers.

07Your system

Milestone 2: variables, TypeOK and one invariant, checked by TLC.

If you have not yet set up your own-system folder, run ./tla my-system init. It copies my-system-template/ to my-system/, which Git ignores. Keep that work in a private repository; never in a public fork. Strip identifiers from anything you share, and do not paste proprietary designs, customer data or credentials into a third-party AI tool without your organisation's approval.

  1. In my-system/, write a module for the system you described at milestone 1. Give it two or three VARIABLES, no more. Pick the ones a bug would show up in.
  2. Write TypeOK, then one invariant that states something your system must never do, in the words of your own requirement.
  3. Write Init, two or three named actions and Next, then a cfg with small constants, INVARIANT TypeOK, your invariant, and the header lines @expect, @witness and @coverage.
  4. Run ./tla check my-system/<YourModule>.cfg. Before you trust a pass, break the invariant on purpose once, and name one witness.

Tick milestone 2 in my-system/MILESTONES.md when the check passes with a witness reached.

Done?