09-02
Operators on lassos
00Recall
One card from 09-01 (safety and liveness, precisely), one from chapter 01 (why a behavior never ends), and one from 03-03 (what a pass in zero states shows). Today's operators all talk about infinite behaviors; keep the second card in view.
09-01-a from The prefix judge
What makes a property a safety property, and what makes it a liveness property?
Show answer
Safety: every violating behavior has a finite prefix that already violates it, whatever comes next. Liveness: every finite prefix can still be continued into a behavior that satisfies it.
01-03-b from Behaviors are infinite
Why does TLA+ make every behavior infinite, even for a system that finishes?
Show answer
So that there is one shape for everything. Stopping is a final state repeated forever, and claims about "always" and "eventually" need no special case for the end.
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.
Skip recall Recall is never required.
01Commit
A new spec: specs/Lighthouse.tla. The lamp is switched on, warms up, and then the beam turns round the compass for ever: north, east, south, west, north again. Nothing else ever happens, and its Spec carries the same WF_vars(Next) assumption as the traffic light, so, apart from stuttering steps, the spec has exactly one behavior. On says the lamp is on; North says the beam points north.
Two operators to read before you predict. []P is read "always P": P holds in every state of the behavior. <>P is read "eventually P": P holds in some state of it.
Commit a
TLC checks SettlesOnNorth == <>[](On /\ North), read "eventually always on and north". What does it report?
- No violation: the beam comes back to north for ever, so it is eventually always there.
- A violation, with a trace that loops: the beam never stays on north.
- A violation, with a trace that ends in the first state where the beam has left north.
Answer A violation, with a trace that loops: the beam never stays on north.
- If you picked A
- "Comes back for ever" is []<>, read "always eventually": again and again. <>[] asks for more: from some point on, north in every state. A beam that keeps turning never gives you that point.
- If you picked C
- No finite trace can refute <>[]: after any finite stretch, the beam might settle on north for good in the next state. TLC has to show a whole behavior in which it never settles, and that takes a loop.
Commit b
TLC checks AlwaysOn == []On as a PROPERTY. What does it report?
- A violation in the very first state: the lamp starts off.
- No violation: once warm, the lamp stays on for ever, which is what "always" means.
- A violation, with a trace that loops, since temporal properties fail with loops.
Answer A violation in the very first state: the lamp starts off.
- If you picked B
- "Always" means every state, the first ones included. "Stays on from some point on" is <>[]On, which holds here. The two differ only in the stem, and that is where the lamp is off.
- If you picked C
- []On is a safety property: a single state with the lamp off refutes it, whatever comes after. A temporal operator does not make a property liveness; what it claims does.
02Lab
Six properties, each checked in its own cfg against the same lighthouse. The two you predicted:
./tla check chapters/09-eventually/specs/Lighthouse.settles-on-north.cfg
./tla check chapters/09-eventually/specs/Lighthouse.always-on.cfg09-eventually/specs/Lighthouse.settles-on-north09-eventually/specs/Lighthouse.settles-on-north Lighthouse.settles-on-north.cfg
parse ok
model check: temporal property SettlesOnNorth violated; lasso of 6 states, looping back to state 3; 6 distinct states
expected outcome met: property SettlesOnNorth
trace (changed variables only; ./tla trace --full shows all):
1 Init lamp = "off", beam = 1
2 SwitchOn lamp: "off" -> "warming"
3 WarmUp lamp: "warming" -> "on"
4 Turn beam: 1 -> 2
5 Turn beam: 2 -> 3
6 Turn beam: 3 -> 4
Turn back to state 309-eventually/specs/Lighthouse.always-on09-eventually/specs/Lighthouse.always-on Lighthouse.always-on.cfg
parse ok
model check: invariant On violated; an initial state violates it
expected outcome met: violation On
trace (changed variables only; ./tla trace --full shows all):
1 Init lamp = "off", beam = 1SettlesOnNorth violated · loop
This is a teaching view. Real checking happens with TLC on your machine.
Trace
- State 1 Initial state lamp = "off" beam = 1
- State 2 SwitchOn lamp "off" becomes "warming"
- State 3 WarmUp lamp "warming" becomes "on"
- State 4 Turn beam 1 becomes 2
- State 5 Turn beam 2 becomes 3
- State 6 Turn beam 3 becomes 4
- Loop back to state 3
Text view: every state and step
| State | lamp | beam | Steps (action → state) |
|---|---|---|---|
| initial 1 | "off" | 1 | SwitchOn → 2 |
| 2 | "warming" | 1 | WarmUp → 3 |
| 3 | "on" | 1 | Turn → 4 |
| 4 | "on" | 2 | Turn → 5 |
| 5 | "on" | 3 | Turn → 6 |
| 6 | "on" | 4 | Turn → 3 |
The other four, eventually-on, north-again-and-again, stays-on and east-leads-to-north, run the same way. Predict each verdict before you run it, using the table in REASON.
Reveal for commit a
A violation, shown as a lasso of 6 states that loops back to an earlier one. The loop is the sweep: north, east, south, west, and back. On /\ North is true in one state of the loop and false in the other three, so it is true again and again but never for good.
Reveal for commit b
A violation in the first state: ./tla reports a trace of 1 state. Notice the wording: TLC's own line is Invariant On is violated by the initial state, and ./tla reports invariant On violated. TLC turned your PROPERTY []On into the invariant On, which is exactly what []P means for a state predicate P, and checked it the way it checks every invariant, state by state.
03Reason
If you said <>[] holds because north keeps coming back, you swapped two words, an easy swap to make. The cure is a picture.
Every liveness counterexample TLC prints is a lasso: a stem of states, then a loop of states that repeats for ever. A stutter is a loop of one unchanged state. The lighthouse's one behavior is a lasso too. Here it is, as TLC printed it for SettlesOnNorth:
- State 1 Initial state lamp = "off" beam = 1
- State 2 SwitchOn lamp "off" becomes "warming"
- State 3 WarmUp lamp "warming" becomes "on"
- State 4 Turn beam 1 becomes 2
- State 5 Turn beam 2 becomes 3
- State 6 Turn beam 3 becomes 4
- Loop Turn back to state 3
The stem is the switch-on and the warm-up. The loop is the sweep, and "back to state" closes it. Now every operator becomes a question about where in the lasso a predicate holds.
| Formula | Read | True on a lasso when |
|---|---|---|
P | P | P holds in the first state |
[]P | always P | P holds in every state, stem and loop |
<>P | eventually P | P holds in some state, stem or loop |
[]<>P | always eventually P | P holds in at least one state of the loop |
<>[]P | eventually always P | P holds in every state of the loop |
P ~> Q | P leads to Q | every P state has a Q state at or after it |
Write something before openingwhy does the stem not matter for []<>P and <>[]P?
The stem is visited once. The loop is visited for ever. "Again and again" can only be satisfied by states that come round again, and those are the loop's. "From some point on" lets you choose the point, and the best choice is the start of the loop: everything before it is forgiven.
The first row deserves a second look. A state predicate on its own, such as On, is a claim about a behavior too, but only about its first state. Hold on to that; it is the trap in lesson 09-03.
P ~> Q is the operator for requirements like "every request is answered". It is shorthand for [](P => <>Q): always, if P holds now, Q holds now or later. On a lasso, a P in the loop needs a Q in the loop, because the P comes round again; a P in the stem can be answered anywhere later.
Now the whole table for the lighthouse, with the outcome of each of the six cfgs. pass means TLC found no violation; property means a temporal property was refuted, with a lasso; violation means an invariant was refuted.
| Property | Formula | Outcome |
|---|---|---|
AlwaysOn | []On | violation |
EventuallyOn | <>On | pass |
NorthAgainAndAgain | []<>(On /\ North) | pass |
SettlesOnNorth | <>[](On /\ North) | property |
StaysOn | <>[]On | pass |
EastLeadsToNorth | (On /\ East) ~> (On /\ North) | pass |
Read each row against the lasso until the verdict feels unsurprising. East is in the loop, and north follows it in the loop, so east leads to north. The lamp is on throughout the loop, so it stays on. It is off in the stem, so it is not always on.
Write something before openinga lamp warms up, sweeps round a few times, then fails and stays dark for ever. Draw its lasso. Which of the six rows still hold?
The stem now holds everything interesting: the warm-up and every sweep. The loop is a single dark state that repeats. EventuallyOn still holds, because the lamp was on somewhere in the stem. AlwaysOn, StaysOn, NorthAgainAndAgain and SettlesOnNorth all fail: each asks something of the loop, and the loop is dark. EastLeadsToNorth depends on where the lamp dies. If its last lit state faces east, that east has no north after it, and the property fails. If it dies right after facing north, every east was followed by a north, and the property holds. Of the six rows, ~> is the one that cares about the order of the states in the stem, not only about which states are in it.
04In your editor
The lighthouse is chapters/09-eventually/specs/Lighthouse.tla; its six cfgs sit next to it. Check this lesson's exercises with:
./tla check 09-02The run of NorthAgainAndAgain, which passes:
09-eventually/specs/Lighthouse.north-again-and-again09-eventually/specs/Lighthouse.north-again-and-again Lighthouse.north-again-and-again.cfg
parse ok
model check: no violation found in 6 distinct states (7 generated, depth 6)
expected outcome met: pass
coverage: all 3 actions fired
witness FacingWest reached (trace of 6 states)05Practice
Warm-up. A harbour master's requirement was written with the operators the wrong way round. Change the operators, not the predicate, until it passes, and until a jammed beam the course supplies fails it.
Core. A dishwasher program, and two lines from its product sheet. Write each one as a temporal formula, so that both pass on the real program, and each fails on at least one of the broken programs the course supplies.
Before the Core, draw each broken program as a lasso on paper and mark where your formula's predicate holds. The table above does the rest. Afterwards, reread the help desk's liveness half from 09-01-ex2, \A k \in 1..Max : (asked >= k) ~> (answered >= k): a \A in front of ~> says "leads to" of every question at once.
09-02-ex1Warm-up5 min
09-02-ex1 · North, again and again
Tier: warm-up. Time: about 5 minutes.
The lighthouse from lesson 09-02, with the harbour master's requirement: "ships must see the beam point north again and again, for as long as the light runs." Someone wrote it as <>[](On /\ North), and TLC refutes it.
- Run
./tla check 09-02-ex1. Read the lasso: where is the stem, where is the loop, and in how many states of the loop does the beam point north? - Read the requirement again, then the table in lesson 09-02. Which row says "again and again"?
- Change the operators in gap g1. Leave the state predicate
On /\ Northas it is. - Run the check again.
Lighthouse.live.cfgmust pass.MCLighthouse.jammed.cfgruns a beam that points north once and jams at east,MCLighthouse.dark.cfga lens that turns for ever with the lamp never lit, andMCLighthouse.blink.cfga lamp that goes off whenever the beam faces north; your property must fail on all three.
A property that is true once is not true again and again, and a beam nobody can see is not a beam. The three broken lighthouses are how the course tells these apart.
All four cfgs must meet their goals: "goal met: pass" for Lighthouse.live.cfg, and "goal met: property SweepsNorth" for MCLighthouse.jammed.cfg, MCLighthouse.dark.cfg and MCLighthouse.blink.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-02-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/02-ex1-north-again-and-again/; open it after you have a passing answer of your own.
Done when: ./tla check 09-02-ex1 prints "goal met: pass".
./tla check 09-02-ex1
Repo chapters/09-eventually/exercises/02-ex1-north-again-and-again/
Hints
Hint 1
Find north in the lasso. Is it in the stem, the loop, or both? In how many states of the loop?
Hint 2
The table in lesson 09-02 has one row for "again and again" and one for "from some point on, always". The starter uses the second.
Hint 3 (nearly the answer)
Swap the two operators in front of the state predicate. If the jammed beam passes, you have a formula that is true once, not again and again. If the dark lighthouse passes, you changed the predicate.
09-02-ex2Core8 min
09-02-ex2 · The dishwasher program
Tier: core. Time: about 8 minutes.
Dishwasher.tla is one run of a dishwasher program: fill, wash, drain, dry, done, and then the machine rests. Two lines from the product sheet need to become temporal formulas.
Finishes: "the program comes to rest in the done stage, and stays there for good."WashLeadsToDry: "whenever the dishes are being washed, they are dried at some later point."
- Run
./tla check 09-02-ex2. Everything passes, because both properties areTRUE. - Draw the program's one behavior as a lasso. Where is the loop?
- Fill gaps g1 and g2.
- Run the check. Five cfgs run.
Dishwasher.live.cfgmust pass. Two broken programs testFinishes, which must fail on both:MCDishwasher.redry.cfgreaches "done", goes back to "dry", and back to "done", for ever;MCDishwasher.never.cfgwashes, drains and washes again, for ever, and never reaches "done". Two testWashLeadsToDry, which must fail on both:MCDishwasher.nodry.cfggoes from "wash" straight to "done", andMCRinseDishwasher.cfgdries after its first wash, then washes a second time and stops wet.
If Finishes passes on the re-drying program, your formula says "done again and again", not "done for good". If WashLeadsToDry passes on the rinsing program, it says "some wash is followed by a dry", not "every wash is". Look again at 09-02's table.
All five cfgs must meet their goals: "goal met: pass" for Dishwasher.live.cfg, "goal met: property Finishes" for MCDishwasher.redry.cfg and MCDishwasher.never.cfg, and "goal met: property WashLeadsToDry" for MCDishwasher.nodry.cfg and MCRinseDishwasher.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-02-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/02-ex2-dishwasher-program/; open it after you have a passing answer of your own.
Done when: ./tla check 09-02-ex2 prints "goal met: property Finishes".
./tla check 09-02-ex2
Repo chapters/09-eventually/exercises/02-ex2-dishwasher-program/
Hints
Hint 1
Draw the real program as a lasso. Its loop has a single state. Then draw the re-drying program: its loop has two states, and only one of them is "done". Then the rinsing program: it washes twice. Which wash has a "dry" after it?
Hint 2
"Comes to rest in done, for good" is "from some point on, always done". "Whenever washing, later drying" is the shape P ~> Q.
Hint 3 (nearly the answer)
Finishes puts two operators in front of stage = "done"; which order is "eventually always"? WashLeadsToDry relates stage = "wash" and stage = "dry" with the leads-to operator.
06Explain it
Open NOTES.md and answer the three prompts under 09-02, 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 "09-02 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "Finishes is <>[] on 'done', because the program's loop is a single 'done' state and the requirement is that it stays there; the re-drying program's loop alternates "dry" and "done", so it fails, where []<> would wrongly pass. WashLeadsToDry is a leads-to, which fails on the program that never dries and on the one that washes a second time without drying, where "some wash is followed by a dry" would wrongly pass. Both depend on the fairness in Spec, which forbids the program stopping half-way. TLC checked one five-stage program; the model has no time, so 'later' has no deadline."
A weak answer: "I used <>[] and ~> and it passed." It names the operators without saying why each fits its requirement, or what the re-drying program shows.
Done?