09-04

Chapter 09 · Eventually

Doing nothing is legal

  • 43 min core
  • Essentials track
  • Skills: intent, counterexamples

00Recall

One card from 09-03 (what PROPERTY P checks when P has no temporal operator), one from chapter 01 (why a behavior never ends), and one from 03-05 (what CHECK_DEADLOCK FALSE changes). Chapter 01's card is today's lesson in one line, if you read it carefully.

09-03-a from Why TLC shows you a lasso

PROPERTY P, where P is a state predicate with no temporal operator. What does TLC check?

Show answer

Only that P holds in the initial states. TLC warns about it. INVARIANT P, or PROPERTY []P, checks every reachable state.

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

A new spec: specs/BoundedBuffer.tla. A producer puts a batch of Items = 3 items into a buffer that holds Cap = 2; a consumer takes them out. Done means every item has been taken. Its Spec is the shape from chapter 04 and nothing more:

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

In every state where the batch is not done, some step is possible: if the buffer holds an item the consumer can take it, and if it is empty the producer can put one in.

Commit a

BoundedBuffer.nofair.cfg checks EventuallyDone == <>Done against Spec == Init /\ [][Next]_vars, with no other conjunct. What does TLC report?

  1. No violation: until Done, some step is always possible, so the batch gets through.
  2. A violation: a trace that gets partway, then stutters for ever.
  3. A deadlock: the buffer gets stuck before Done.

Answer A violation: a trace that gets partway, then stutters for ever.

The same module also defines a second spec, with one more conjunct:

TLA+
FairSpec is defined as Spec and WF_vars(Next)

Commit b

BoundedBuffer.live.cfg checks the same EventuallyDone against FairSpec. What does TLC report?

  1. No violation: the extra conjunct rules out stopping while a real step, one that changes something, is possible.
  2. The same violation: fairness is about sharing time between processes, and here there is nothing to share.
  3. A new failure: once Done, the buffer cannot take a step, and the conjunct demands steps.

Answer No violation: the extra conjunct rules out stopping while a real step, one that changes something, is possible.

02Lab

shell
./tla check chapters/09-eventually/specs/BoundedBuffer.nofair.cfg
./tla check chapters/09-eventually/specs/BoundedBuffer.live.cfg

The run without fairness:

Expected output 09-eventually/specs/BoundedBuffer.nofair
09-eventually/specs/BoundedBuffer.nofair  BoundedBuffer.nofair.cfg  (Items=3, Cap=2)
parse ok
model check: temporal property EventuallyDone violated; trace of 6 states, then stuttering forever; 9 distinct states
expected outcome met: property EventuallyDone
trace (changed variables only; ./tla trace --full shows all):
  1  Init  buf = 0, made = 0, taken = 0
  2  Put   buf: 0 -> 1, made: 0 -> 1
  3  Put   buf: 1 -> 2, made: 1 -> 2
  4  Take  buf: 2 -> 1, taken: 0 -> 1
  5  Put   buf: 1 -> 2, made: 2 -> 3
  6  Take  buf: 2 -> 1, taken: 1 -> 2
     (stutter)  nothing changes from here on, forever

Reveal for commit a

A violation: ./tla prints a trace of 6 states whose last line is (stutter) nothing changes from here on, forever; its summary line says then stuttering forever. The batch is not done in the last state, and a step is possible there (in the run above, an item is still in the buffer and the consumer could take it). The spec does not require anyone to take it.

Reveal for commit b

No violation, in 9 distinct states, with both witnesses reached. The only difference between the two runs is WF_vars(Next). The model's states are the same; what changed is which infinite behaviors count.

03Reason

If you said the batch must get through because a step is always possible, you read the spec as a program that runs. It is not one. It is a formula, and the formula has a loophole that you have been using since chapter 04.

[][Next]_vars says: every step is a Next step or a step that leaves vars unchanged. Chapter 04 called those stuttering steps and explained why a spec must allow them. Now look at what they allow.

Write something before openingwrite down a behavior of BoundedBuffer's Spec in which no item is ever taken.

The initial state, then the initial state again, and again, for ever. Every step leaves vars unchanged, so every step is allowed by [][Next]_vars. It is a perfectly legal behavior of Spec: nothing wrong ever happens, because nothing ever happens.

That is the whole lesson in one behavior: doing nothing is legal. A spec of the form Init /\ [][Next]_vars allows every behavior to stop, at any point, for ever. It satisfies a liveness property only if every behavior that stops for good satisfies it, including the one that never leaves its initial state. <>Done fails because the behavior may stop before Done; a P ~> Q fails whenever a behavior can stop after a P with no Q yet.

TLC found exactly such a behavior. It did not find a bug in the buffer. It found a gap in what you told it about the world.

So liveness needs an assumption, written into Spec, about what the world does. WF_vars(Next) is the plainest one: the system does not stop while it can still take a real step. The lights, lighthouse and lift of this chapter have been carrying it since 09-01. With it, a behavior may still stutter for ever, but only in a state where nothing real can happen: here, at Done, where the Finished disjunct changes nothing.

Write something before openingis "the system does not stop while it can take a step" true of a real buffer?

It depends on what is underneath. If the consumer is a thread that the operating system schedules, it is a reasonable assumption about the scheduler. If the consumer is a person who may go home, it is false, and a spec that assumes it will pass checks the real system fails. An assumption is a claim about the world. You state it so that a reviewer can disagree with it.

When a liveness check fails, look at the end of the trace first. A loop means the system keeps busy and still never gets there: a design bug, like the nearest-first lift, or an environment the requirement has to assume away, like lesson 09-05's network. A stutter means the behavior was allowed to stop. If a real step is possible in the last state, as here, an assumption is missing. If none is, the system stopped for good, stuck or at an end the spec allows, and lesson 09-05 tells those apart.

Write something before openingFairSpec passes. Does that mean the producer and the consumer each get their turn?

No. WF_vars(Next) is about Next as a whole: while some real step is possible, some real step happens. It says nothing about which one. In this buffer that is enough, because every Put adds to made and every Take adds to taken, both are bounded by Items, and so only finitely many real steps can happen before Done, whoever takes them. When the buffer is full only the consumer can move, and when it is empty only the producer can. Now imagine a producer that could keep working for ever on its own. Fairness on the whole of Next would be satisfied by the producer alone, and the consumer could starve. Fairness on Next is a promise about the system as a whole, never about each of its parts. Chapter 10 opens with exactly that scheduler.

04In your editor

The buffer is chapters/09-eventually/specs/BoundedBuffer.tla, with Spec and FairSpec side by side. Check this lesson's exercises with:

shell
./tla check 09-04

The run with fairness:

Output 09-eventually/specs/BoundedBuffer.live
09-eventually/specs/BoundedBuffer.live  BoundedBuffer.live.cfg  (Items=3, Cap=2)
parse ok
model check: no violation found in 9 distinct states (12 generated, depth 7)
expected outcome met: pass
coverage: all 3 actions fired
witness BufferFull reached (trace of 3 states)
witness Done reached (trace of 7 states)

05Practice

Warm-up. A kettle that is switched on never has to boil. Read the stuttering trace, then give Spec the assumption it is missing.

Core. A courier with two parcels. Write the assumption the depot manager makes about a courier who still has work, and the property "every parcel is delivered". The course checks your property against a courier with no assumption at all, and against one who delivers a single parcel and keeps the rest; it must fail on both.

Before the Core, write in one sentence what your assumption says about the courier. If you cannot defend that sentence to the courier's manager, the assumption is wrong, however green the check.

09-04-ex1Warm-up5 min

09-04-ex1 · The kettle that never boils

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

An electric kettle. Switch it on and it heats one step at a time until it boils, then clicks off and cools. The liveness property says that once it is switched on, it boils. TLC refutes it.

  1. Run ./tla check 09-04-ex1. Read the trace and the TLC warning: line under the verdict. How does the trace end? In its last state, which actions are enabled?
  2. Read Spec in gap g1. Compare it with FairSpec in lesson 09-04.
  3. Give Spec the assumption it is missing. Write it as fairness, as FairSpec does, not as the property itself: a Spec that assumes OnLeadsToBoil passes any check of OnLeadsToBoil and tells you nothing.
  4. Run the check again. Kettle.live.cfg should pass, with every action firing and the witness reached. Kettle.cfg, the safety cfg, passes before and after: fairness never changes which states are reachable.

TLC's warning gave you the answer this time. It does not always appear: lesson 09-04's run of the buffer has no such warning. Learn to read the stutter, not the warning.

Hints: HINTS.md, under "09-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/04-ex1-the-kettle-that-never-boils/; open it after you have a passing answer of your own.

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

./tla check 09-04-ex1

Repo chapters/09-eventually/exercises/04-ex1-the-kettle-that-never-boils/

Hints

Hint 1

In the trace's last state the kettle is on and not yet boiling. Which action is enabled there? Why does the spec allow it not to happen?

Hint 2

Spec is Init /\ [][Next]_vars and nothing else. Lesson 09-04 added one conjunct to the buffer's spec to rule out stopping while a real step is possible.

Hint 3 (nearly the answer)

FairSpec in BoundedBuffer.tla has the conjunct you need. Which part of it names the variables, and does that part match the subscript of your box?

09-04-ex2Core8 min

09-04-ex2 · Every parcel arrives

Tier: core. Time: about 8 minutes.

A courier loads two parcels from the depot into a van, one at a time, and delivers them in any order. The depot manager wants every parcel delivered, and assumes something about a courier who still has work to do. You write both.

  1. Run ./tla check 09-04-ex2. All three cfgs pass, because the property is TRUE.
  2. Fill gap g2, EveryParcelDelivered: for every parcel, a temporal formula. Run the check: Courier.live.cfg should now fail. Read how its trace ends.
  3. Fill gap g1: Spec, with the assumption. Write it as fairness, not as the property you are checking: a Spec that assumes "every parcel arrives" makes every check of it pass and tells you nothing. Write a comment above it saying, in words the depot manager would accept, what it assumes.
  4. Run the check. Courier.live.cfg must pass. MCCourier.cfg runs the same courier with no assumption at all, and MCCourier.careless.cfg a courier who loads both parcels, delivers one and then never delivers again, resting with the other in the van; your property must fail on both.

MCCourier.tla is the course's check. Leave it as it is.

All three cfgs must meet their goals: "goal met: pass" for Courier.live.cfg, and "goal met: property EveryParcelDelivered" for MCCourier.cfg and MCCourier.careless.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-04-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/04-ex2-every-parcel-arrives/; open it after you have a passing answer of your own.

Done when: ./tla check 09-04-ex2 prints "goal met: property EveryParcelDelivered".

./tla check 09-04-ex2

Repo chapters/09-eventually/exercises/04-ex2-every-parcel-arrives/

Hints

Hint 1

Write the property first and run it: the courier with no assumption stops for good with a parcel undelivered. That stop is what the assumption must rule out.

Hint 2

"Every parcel is delivered" quantifies over Parcels and says, of each one, that it is delivered at some point. The assumption is the one from lesson 09-04.

Hint 3 (nearly the answer)

EveryParcelDelivered needs a quantifier over parcels and one temporal operator from 09-02's table. If the careless courier passes, your formula is about some parcel, or about loading. Spec gets the same extra conjunct as FairSpec in BoundedBuffer.

06Explain it

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

After you writeCheck your explanation

A good answer: "EveryParcelDelivered says each parcel is delivered at some point. Without an assumption, TLC refuted it with a courier that stops for ever with a parcel in the van, because stopping is always legal. WF_vars(Next) assumes a courier with work left keeps working; with it the property holds for two parcels. If couriers can take the van home with parcels inside, the assumption is false and the pass means nothing about them."

A weak answer: "I added fairness and the property passed." It does not say what the fairness assumes about the courier, or what TLC showed without it.

Done?