09-03

Chapter 09 · Eventually

Why TLC shows you a lasso

  • 44 min core
  • Essentials track
  • Skills: counterexamples, evidence

00Recall

One card from 09-02 (where a predicate must hold on a lasso), one from 09-01 (bounded response is safety), and one from 03-05 (a pass covers only what the cfg names). The last one returns today in a sharper form.

09-02-a from Operators on lassos

On a lasso (a stem, then a loop), where must P hold for []<>P, and where for <>[]P?

Show answer

[]<>P: in at least one state of the loop. <>[]P: in every state of the loop. The stem does not matter to either.

09-01-b from The prefix judge

"Every request is answered within three changes of state." Safety or liveness?

Show answer

Safety. The deadline makes a violation finite: three changes of state with no answer. Without the deadline it would be liveness.

03-05-a from TypeOK, and the check you didn't configure

The cfg names only INVARIANT TypeOK, and TLC finds no violation. May you say the thermostat keeps the room comfortable?

Show answer

No, for two reasons. TLC never checked Comfortable, because the cfg did not name it. And even a cfg that names it gives "no violation found in this model": one finite model, not a proof.

Skip recall Recall is never required.

01Commit

A new spec: specs/Elevator.tla. One lift car on floors 1 to Floors, with a call button on each floor. Calls wait in a queue and the car serves the oldest one first, one floor per step. The safety cfg, Elevator.cfg, checks invariants; the liveness cfg, Elevator.live.cfg, checks EveryCallAnswered, which says that every waiting floor stops waiting, sooner or later.

A teammate wrote their own liveness cfg, Elevator.trap.cfg, before they had read 09-02. Its only check is PROPERTY AllAnswered, where:

TLA+
AllAnswered is defined as queue = tuple of end tuple

Commit a

TLC runs Elevator.trap.cfg, whose only check is PROPERTY AllAnswered, with AllAnswered == queue = << >>. What does it report?

  1. A violation: the first call puts a floor in the queue.
  2. An error: a state predicate is not allowed after PROPERTY.
  3. No violation, plus a warning that the formula was checked in the initial state only.

Answer No violation, plus a warning that the formula was checked in the initial state only.

A second teammate changed one line, in specs/NearestElevator.tla: the car heads for the nearest waiting floor instead of the oldest call, "because that is what real lifts do". The safety cfg still passes.

Commit b

NearestElevator.live.cfg checks EveryCallAnswered on the lift that heads for the nearest waiting floor. What does TLC print?

  1. A trace that ends in a state where the top floor has waited too long.
  2. A stem, then a loop in which the top floor waits in every state.
  3. No violation: every action fires and the car never stops moving.

Answer A stem, then a loop in which the top floor waits in every state.

02Lab

Four runs. The teammate's cfg, the nearest-first lift, the same lift judged by a formula you will meet in REASON, and the real lift:

shell
./tla check chapters/09-eventually/specs/Elevator.trap.cfg
./tla check chapters/09-eventually/specs/NearestElevator.live.cfg
./tla check chapters/09-eventually/specs/NearestElevator.nobox.cfg
./tla check chapters/09-eventually/specs/Elevator.live.cfg

The nearest-first lift:

Expected output 09-eventually/specs/NearestElevator.live
09-eventually/specs/NearestElevator.live  NearestElevator.live.cfg  (Floors=3)
parse ok
model check: temporal property EveryCallAnswered violated; lasso of 7 states, looping back to state 2; 36 distinct states
expected outcome met: property EveryCallAnswered
trace (changed variables only; ./tla trace --full shows all):
  1  Init    floor = 1, queue = <<>>
  2  Call    queue: <<>> -> <<3>>
  3  Call    queue: <<3>> -> <<3, 2>>
  4  Move    floor: 1 -> 2
  5  Arrive  queue: <<3, 2>> -> <<3>>
  6  Call    queue: <<3>> -> <<3, 1>>
  7  Move    floor: 2 -> 1
     Arrive  back to state 2

Reveal for commit a

No violation, in 34 distinct states, every action firing and both witnesses reached. In between sits a TLC warning: line saying that AllAnswered "is a state-level formula" used as a PROPERTY, and that such formulas "are only checked in the initial state". Nothing about the elevator was checked after its first state.

Reveal for commit b

A lasso of 7 states. The stem puts the top floor in the queue; the loop has the car go between the bottom two floors, answering new calls there, while the top floor waits in every state. A separate cfg, NearestElevator.lower.cfg, checks the same property for every floor but the top one and finds no violation: in this model, the top floor is the one that starves. REASON says why it is the top one.

03Reason

If you expected a trace that ends, you were thinking of invariants, where the bad state is the end of the story. A liveness violation has no bad state. It has a bad forever, and TLC has to show you a forever on a finite screen.

Write something before openingTLC's state graph for this lift is finite. Why must any infinite behavior in it revisit a state?

Count. The graph has a fixed number of distinct states, and an infinite behavior visits a state at every position. After as many steps as there are states, some state has come up twice. That is the pigeonhole principle.

A revisited state alone does not give a counterexample: the first cycle a behavior closes may contain the good thing, and then repeating it satisfies the property. So TLC pairs each state with a node of a tableau, a small graph built from the negated property, and searches the product for a reachable cycle that breaks the property and respects the fairness in Spec. Such a cycle exists whenever any violation does. That is why a finite model that breaks a liveness property always has a counterexample shaped like a lasso: a stem from an initial state to the cycle, then the cycle repeated. TLC prints the stem and one turn of the loop. The back to state line is the knot.

Read a lasso from the loop backwards. A loop of real steps is the part that happens for ever, so start there. In the nearest-first lift, the loop is two floors trading calls; the stem only explains how the top floor got into the queue. Ask of the loop: what keeps the good thing from happening here? Target heads for the nearest waiting floor. At the bottom floor, the middle floor is nearer than the top one. At the middle floor, the bottom and top floors are equally near, and CHOOSE settles a tie the same way every time it meets it; TLC's choice is the bottom floor. So on every turn of the loop the top floor is either farther away or loses the tie. Which floor loses depends on how CHOOSE settles ties, which the spec does not pin down. What the spec does settle is that nothing in Target stops one floor from losing for ever.

Check the knot yourself, because it is the one line of the trace that shows no values. The last printed state has the car at the bottom floor, with the top floor and the bottom floor waiting. The closing step is Arrive: the car answers the bottom floor, which leaves only the top floor in the queue and the car where it was. That is exactly the state the back to state line names, every variable equal. Checking it by hand makes sure you read the loop TLC meant.

The split. From this chapter on, safety and liveness go in separate cfgs: <Module>.cfg for invariants, with deadlock checking on, and <Module>.live.cfg for temporal properties, where the spec has any. Here they are Elevator.cfg and Elevator.live.cfg. Three reasons:

  • A temporal formula after INVARIANT is an error: TLC says it "is not a state predicate". A state predicate after PROPERTY is not an error, and that is worse.
  • A liveness check searches the state graph for bad cycles, which is more work than checking invariants state by state. A safety check can stop at the first bad state. Kept apart, the cheap check stays cheap.
  • A liveness verdict depends on the fairness assumption in Spec; a safety verdict does not, because fairness never changes which states can be reached (for weak or strong fairness on actions of Next, the only kind this course writes). Two cfgs keep the difference visible. Chapter 10 makes it precise.

That last line is the trap with no warning. NearestElevator.tla defines TopAnsweredNoBox == Waiting(Floors) => <>~Waiting(Floors). It is temporal, so TLC does not warn. It is about the first state, where nobody waits, so it is true of every behavior. NearestElevator.nobox.cfg checks it on the lift that starves the top floor and reports no violation. Put a [] in front, or write ~>, and the same lift fails.

04In your editor

The lifts are chapters/09-eventually/specs/Elevator.tla and NearestElevator.tla, each with a .cfg and a .live.cfg. Check this lesson's exercises with:

shell
./tla check 09-03

The real lift's liveness run:

Output 09-eventually/specs/Elevator.live
09-eventually/specs/Elevator.live  Elevator.live.cfg  (Floors=3)
parse ok
model check: no violation found in 34 distinct states (58 generated, depth 9)
expected outcome met: pass
coverage: all 3 actions fired
witness CarAtTop reached (trace of 4 states)
witness TopWaitsBehindAnother reached (trace of 3 states)

05Practice

Warm-up. A teammate put a liveness formula in the safety cfg. Read TLC's error, and move the check to where it belongs.

Core. A garage door's requirement was written as pressed => <>(door = "open"). It passes, with no warning, on the real door and on a door whose motor stalls. Write it so that it passes on the real door, and on one where nobody has to press, and fails on two broken ones.

Before you start the Core, write down what pressed => <>(door = "open") claims about the first state of a behavior. Then look at Init.

09-03-ex1Warm-up5 min

09-03-ex1 · Not an invariant

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

A pop-up toaster with a safety property, NoHeatWhenUp, and a liveness property, ToastPops. A teammate put all three checks in one cfg.

  1. Run ./tla check 09-03-ex1. Toaster.cfg stops with a TLC error. Read it: what does TLC say ToastPops is not?
  2. Open Toaster.live.cfg. Which keyword does it use for ToastPops?
  3. Fix gap g1 in Toaster.cfg, so that the safety cfg holds only what an invariant can be.
  4. Run the check again. Both cfgs should pass, with every action firing and the witness reached.

Then answer for yourself: if TLC had accepted INVARIANT ToastPops, what would it have had to check in each state, and why can no single state answer that?

Toaster.cfg must meet its goal, "goal met: pass". Toaster.live.cfg has no gap and keeps passing. 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.

Hints: HINTS.md, under "09-03-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/03-ex1-not-an-invariant/; open it after you have a passing answer of your own.

Done when: ./tla check 09-03-ex1 prints "goal met: pass".

./tla check 09-03-ex1

Repo chapters/09-eventually/exercises/03-ex1-not-an-invariant/

Hints

Hint 1

TLC says ToastPops "is not a state predicate". Look at how ToastPops is defined. Which operator in it is not about a single state?

Hint 2

An invariant is evaluated in one state at a time. A leads-to is about what comes later in a behavior. The liveness cfg already checks ToastPops the right way.

Hint 3 (nearly the answer)

Toaster.live.cfg already checks ToastPops with the keyword that judges whole behaviors. What is left for the safety cfg to do with it?

09-03-ex2Core8 min

09-03-ex2 · Press means open

Tier: core. Time: about 8 minutes.

A garage door with a remote. The requirement: "every press of the remote opens the door". A teammate wrote it as

output
OpensWhenPressed == pressed => <>(door = "open")

TLC accepts it with no warning, and it passes. It also passes on a door whose motor stalls and never opens it.

  1. Run ./tla check 09-03-ex2. Four cfgs run. GarageDoor.live.cfg checks the real door. MCGarageDoor.jammed.cfg checks a door whose motor starts and stalls, so the door drops back shut, and MCWornGarageDoor.cfg a door whose motor runs once and never again. MCGarageDoor.idle.cfg checks the real door in a house where nobody has to press the remote. All four pass.
  2. Write down what the teammate's formula claims about the first state of a behavior. Then read Init.
  3. Rewrite OpensWhenPressed in gap g1 so that it says what the requirement says: every press, not only one in the first state, and an open door, not a door that starts to move.
  4. Run the check. The real door must pass, and so must the idle one; both broken doors must fail.

The other doors are the course's checks, in MCGarageDoor.tla and MCWornGarageDoor.tla. Leave them as they are.

All four cfgs must meet their goals: "goal met: pass" for GarageDoor.live.cfg and MCGarageDoor.idle.cfg, and "goal met: property OpensWhenPressed" for MCGarageDoor.jammed.cfg and MCWornGarageDoor.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.

Hints: HINTS.md, under "09-03-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/03-ex2-press-means-open/; open it after you have a passing answer of your own.

Done when: ./tla check 09-03-ex2 prints "goal met: property OpensWhenPressed".

./tla check 09-03-ex2

Repo chapters/09-eventually/exercises/03-ex2-press-means-open/

Hints

Hint 1

In the first state of every behavior, the door is closed and pressed is FALSE. What does FALSE => anything evaluate to?

Hint 2

The teammate's formula is about the first state because nothing stands in front of the =>. The requirement is about every state in which the remote has been pressed.

Hint 3 (nearly the answer)

The teammate's implication is right about one state; the requirement wants it about every state. 09-02's table has an operator that says exactly that. The door's goal state is "open", not merely moving: the jammed door must fail, and the idle door, where nobody has to press, must pass.

06Explain it

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

After you writeCheck your explanation

A good answer: "The teammate's pressed => <>(door = "open") is a claim about the first state, where nothing is pressed, so it held for every door, including the jammed one, and TLC did not warn. pressed ~> (door = "open") is the same claim made about every state; it passes on the real door and on the idle one, and fails on both broken ones. It relies on the fairness assumption in Spec. TLC checked one door with no time and no safety beam."

A weak answer: "The formula was wrong, so I fixed it." It does not say what the wrong formula actually claimed, or why TLC gave no warning.

Done?