12-05

Chapter 12 · Taming the state space, choosing your evidence

Reachable versus inductive

  • 45 min core
  • Core track
  • Skills: evidence, intent

00Recall

One card from 12-04 on the small-scope hypothesis, one from chapter 09 on what makes a property a safety property, and one from 03-05 on what a passing TypeOK lets you say. This lesson is the first step from "no violation in this model" towards "no violation in any model".

12-04-b from Simulation and honest claims

What does the small-scope hypothesis claim, and what does it not?

Show answer

That many bugs show up in small instances, so small models are the right place to start. It is an empirical observation, not a proof: a bug that needs four of something cannot appear in a model with three.

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.

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

specs/PetersonInductive.tla is Peterson's mutual exclusion for two processes, written with a program counter: raise your flag, yield the turn, wait until the other is not interested or the turn comes back, enter. PetersonInductive.cfg checks MutualExclusion from Init and finds no violation in 20 distinct states.

specs/MCPetersonInductive.tla defines WeakInit == TypeOK /\ MutualExclusion: every state with the right types in which the two processes are not both critical, whether or not Peterson ever reaches it. MCPetersonInductive.weak.cfg uses WeakInit as the initial states, lets TLC take steps, and checks MutualExclusion again.

Commit a

What does ./tla report for MCPetersonInductive.weak.cfg?

  1. No violation: MutualExclusion holds in every reachable state, so no step can break it.
  2. A violation after one step.
  3. A violation, but only after several steps, from a state Peterson reaches.

Answer A violation after one step.

Inv adds two facts to MutualExclusion: a flag is up exactly when its process has left "idle" (FlagsHonest), and if one process is critical while the other waits, the turn belongs to the critical one (TurnDecides). MCPetersonInductive.strong.cfg starts from StrongInit == TypeOK /\ Inv and checks Inv. It passes.

Commit b

MCPetersonInductive.strong.cfg starts from every typed state in which Inv holds and checks Inv. How many distinct states will it report?

Your answer is a whole number.

Answer 28

02Lab

shell
./tla check chapters/12-state-space-and-evidence/specs/MCPetersonInductive.weak.cfg
./tla check chapters/12-state-space-and-evidence/specs/MCPetersonInductive.strong.cfg
./tla check chapters/12-state-space-and-evidence/specs/MCPetersonInductive.sim.cfg
Expected output 12-state-space-and-evidence/specs/MCPetersonInductive.weak
12-state-space-and-evidence/specs/MCPetersonInductive.weak  MCPetersonInductive.weak.cfg
parse ok
model check: invariant MutualExclusion violated; trace has 2 states (1 steps); 121 distinct states
expected outcome met: violation MutualExclusion
trace (changed variables only; ./tla trace --full shows all):
  1  Init   pc = [p1 |-> "waiting", p2 |-> "critical"], flag = [p1 |-> FALSE, p2 |-> FALSE], turn = "p1"
  2  Enter  pc: [p1 |-> "waiting", p2 |-> "critical"] -> [p1 |-> "critical", p2 |-> "critical"]
Expected output 12-state-space-and-evidence/specs/MCPetersonInductive.strong
12-state-space-and-evidence/specs/MCPetersonInductive.strong  MCPetersonInductive.strong.cfg
parse ok
model check: no violation found in 28 distinct states (80 generated, depth 1)
expected outcome met: pass
coverage: all 4 actions fired
witness Contended reached (trace of 1 states)

Reveal for commit a

A violation of MutualExclusion in a trace of 2 states: one starting state, one step. The starting state is in WeakInit, so it has the right types and only one process critical. Peterson never reaches it.

Reveal for commit b

The strong check reports 28 distinct states at depth 1: every successor of a state in StrongInit is already in StrongInit. That is more than the 20 reachable states. Inv describes a set that contains the reachable states and is closed under steps.

03Reason

If you picked "no violation", you assumed that an invariant keeps itself true. It is a natural assumption, and it is false.

Write something before openingwhich run is lying?

Neither. They answer different questions. The first asks "is MutualExclusion true in every state Peterson reaches?" Yes. The second asks "is MutualExclusion alone enough to keep itself true after one more step, from any typed state?" No. The trace starts in a state Peterson never reaches. Every shortest trace does: its first state satisfies MutualExclusion and breaks FlagsHonest or TurnDecides, and the reachable states satisfy both.

Why care, if TLC already checked every reachable state? Three reasons.

First, induction does not depend on how many states are reachable. If Init => Inv and Inv /\ Next => Inv' hold, a two-line argument gives Inv in every state of every behavior, for any number of steps. A proof assistant can check that argument for every value of the constants (12-06). TLC's breadth-first search can only check one model.

Second, the strengthening is the explanation. FlagsHonest and TurnDecides are why Peterson works. Finding them is how you come to understand a protocol rather than trust it.

Third, the one-step check has no depth: one step from each starting state. Its cost is the number of typed states, which can be far larger than the reachable set.

Write something before openingthe weak check's starting state has one process critical. What else must be true of it for one step to make both critical?

The other process must be able to enter. Enter(p) needs pc[p] = "waiting" and either the other's flag down or the turn equal to p. So the waiting process sees the critical one's flag down, which FlagsHonest forbids, or it sees the turn as its own while the other is critical, which TurnDecides forbids. Each conjunct of Inv closes one way in.

Count the strong check's states once; the count shows what an inductive invariant is. FlagsHonest ties each flag to its program counter, so a state is fixed by the two program counters and the turn.

Four program counters per process give 4 × 4 = 16 pairs; MutualExclusion removes the one pair with both critical, leaving 15, and each pair comes with either value of turn: 30. TurnDecides removes the two states where one process is critical, the other waits, and the turn belongs to the waiter. That leaves 28, the fact in the reveal, which includes states Peterson never visits from Init. Which ones they are does not matter: no step leads from any of them to a state Inv forbids, and that is all induction asks.

How do you find an inductive invariant? Run the weak check, read the counterexample to induction, ask what is true of every reachable state but false of its first state, add that as a conjunct, and repeat. Each round, the trace tells you exactly which unreachable state was still allowed. The Warm-up takes one round, and the Core takes one good idea. Stop when the one-step check passes and the ordinary check from Init still passes too: the one-step check never tests Init => Inv, so only the check from Init catches an Inv that is false in a reachable state.

Write something before openingInv == TRUE holds initially, and no step can break it. Why is it no help?

Because induction gives you exactly what Inv implies, and TRUE implies nothing. A useful strengthening has three obligations. Inv must hold in every initial state: the ordinary check from Init catches an Inv that is too strong. Every step from a state satisfying Inv must keep it: the one-step check catches an Inv that is too weak. And Inv must imply the requirement, which is why MutualExclusion is one of its conjuncts. TRUE passes the first two obligations and fails the third. MutualExclusion alone passes the first and the third, and the weak check showed you it fails the second.

When the typed states are too many to enumerate, there are two standard patterns. The first keeps TLC and uses simulation, with an initial predicate that draws typed states at random. MCPetersonInductive.sim.cfg does that with SampledWeakInit and finds a violation too, from a sample of starting states instead of all of them. A random walk need not take the shortest way, so its trace can be longer than one step. Silence from it means what silence from any simulation means.

The second uses Apalache, a symbolic model checker. It does not enumerate states; it asks a solver whether any state satisfying the initial predicate has a step to a state breaking the invariant. PetersonInductive.tla already carries the @type comments Apalache needs, and IndInv == TypeOK /\ Inv. The two commands, which check Init => IndInv and then one step from any state satisfying IndInv, are:

shell
apalache-mc check --init=Init --inv=IndInv --length=0 PetersonInductive.tla
apalache-mc check --init=IndInv --inv=IndInv --length=1 PetersonInductive.tla

Apalache is an optional tool and the course's checks do not run it. Output not shown: run it yourself, or see the optional-tools workflow.

04In your editor

Check this lesson's exercises with:

shell
./tla check 12-05

For each conjunct you add, write in a comment which counterexample to induction it rules out.

05Practice

Warm-up. A spin lock's MutualExclusion is not inductive. One conjunct about the lock word fixes it.

Core. A token ring. "At most one holder" is not inductive, because the token also travels by message. Find the invariant that counts every place the token can be.

Stretch. TwoPhaseCommit's Consistent, by induction: write the inductive invariant and the cfg that checks it.

12-05-ex1Warm-up5 min

12-05-ex1 · The lock that remembers

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

SpinLock.tla has two workers and one lock word. MutualExclusion holds in every reachable state, and SpinLock.cfg agrees. MCSpinLock.cfg asks the 12-05 question: from every typed state where Inv holds, does every step keep it?

  1. Run ./tla check 12-05-ex1. The ordinary check passes. The induction check finds a violation after one step. Read the first state of the trace: is it a state the lock can reach from Init?
  2. Fill gap g1 in SpinLock.tla: add one conjunct to Inv that every reachable state satisfies and that state does not.
  3. Run again. Both cfgs must pass: the induction check shows Inv is inductive, and the ordinary check shows it allows every state the lock really reaches.

Leave MCSpinLock.tla and both cfgs as they are.

Hints: HINTS.md, under "12-05-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/12-state-space-and-evidence/exercises/05-ex1-the-lock-that-remembers/.

Both cfgs must meet their goals, MCSpinLock.cfg and SpinLock.cfg: the line below appears once for each.

Done when: ./tla check 12-05-ex1 prints "goal met: pass".

./tla check 12-05-ex1

Repo chapters/12-state-space-and-evidence/exercises/05-ex1-the-lock-that-remembers/

Hints

Hint 1

Read the first state of the induction check's trace. One worker is inside. What does the lock word say?

Hint 2

In every reachable state, a worker inside is named by the lock word, because Acquire writes the name when it lets the worker in.

Hint 3 (nearly the answer)

Add a conjunct that quantifies over Workers: being inside implies that lock equals that worker.

12-05-ex2Core8 min

12-05-ex2 · One token

Tier: core. Time: about 8 minutes.

TokenRing.tla passes one token round a ring of three nodes by message. Only the holder may write to the shared log, and OneHolder says at most one node holds the token. It holds in every reachable state. It is not inductive.

  1. Run ./tla check 12-05-ex2 and read the induction check's trace. Where was the token in its first state?
  2. Fill gap g1 in TokenRing.tla: an Inv that implies OneHolder and that no step can break. Ask where else, besides a holder, the token can be.
  3. Run again. Both cfgs must pass.

Two wrong answers to try in a copy, so that you see what catches them: Inv == OneHolder /\ Cardinality(net) <= 1 (still not inductive), and Inv == OneHolder /\ net = {} (false as soon as the first token is sent).

Hints: HINTS.md, under "12-05-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/12-state-space-and-evidence/exercises/05-ex2-one-token/.

Both cfgs must meet their goals, MCTokenRing.cfg and TokenRing.cfg: the line below appears once for each.

Done when: ./tla check 12-05-ex2 prints "goal met: pass".

./tla check 12-05-ex2

Repo chapters/12-state-space-and-evidence/exercises/05-ex2-one-token/

Hints

Hint 1

In the trace's first state, one node holds the token and a message carries a token to another. OneHolder is happy with that. Is the ring?

Hint 2

The token is in exactly one place, a holder or a message, never both and never two messages. OneHolder counts only holders.

Hint 3 (nearly the answer)

Count the token in every place it can be: the nodes that hold it, and the messages that carry it. Inv says something about that count, besides OneHolder. Cardinality counts a set.

12-05-ex3Stretch40 min · mastery path

12-05-ex3 · Commit by induction

Tier: stretch. Time: about 40 minutes.

Off the core clock: this is on the mastery path. Blank page, cfg included.

TwoPhaseCommit.tla is the chapter's spec, unchanged. Consistent holds in every reachable state with three participants. Your job is an inductive invariant IndInv that implies it, and the cfg that checks one step from every typed state satisfying it.

  1. Run ./tla check 12-05-ex3. Your cfg is empty for now, and TLC stops: nothing says where behaviors start, which is the whole question of an induction check. The course's MCTwoPhaseCommit.grader.cfg starts from IndInit == TypeOK /\ IndInv, and with IndInv == TRUE an initial state already breaks Consistent.
  2. Write your cfg in gap g1 of MCTwoPhaseCommit.cfg: the induction check, with a comment on deadlock checking. Its witness StartsCommitted is a committed transaction among the starting states, which only a check that starts from IndInit reaches.
  3. Write IndInv in gap g1 of MCTwoPhaseCommit.tla. Start with Consistent alone, read the counterexample to induction, and add one conjunct per round. Each conjunct should relate a variable to the message that explains its value.
  4. Stop when all three cfgs pass. MCTwoPhaseCommit.reach.cfg checks that IndInv allows every reachable state.

Then compare the distinct count of the induction check with that of specs/TwoPhaseCommit.n3.cfg, and explain the difference or the lack of one.

Hints: HINTS.md, under "12-05-ex3 hint 1" to "hint 3". The solution is in solutions/chapters/12-state-space-and-evidence/exercises/05-ex3-commit-by-induction/.

All three cfgs must meet their goals: the line below appears once for each.

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

./tla check 12-05-ex3

Repo chapters/12-state-space-and-evidence/exercises/05-ex3-commit-by-induction/

Hints

Hint 1

Start with IndInv == Consistent and read the counterexample to induction. Its first state has a combination of messages and participant states the protocol could never produce.

Hint 2

Relate each variable to its cause: a decision message to the coordinator's decision, a commit to every yes vote, each participant's state to the message that explains it. Your cfg is the 12-05 induction check with deadlock checking off.

Hint 3 (nearly the answer)

The reference uses seven conjuncts besides Consistent. Two say a decision message exists exactly when the coordinator has decided; one says a commit needs every yes vote; the rest relate "committed", "ready", a sent yes vote and "aborted" to messages.

06Explain it

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

After you writeCheck your explanation

A good answer: "Inv says the number of holders plus the number of token messages in flight is one, so no step can copy the token. It relies on Pass and Receive each moving exactly one token; a duplicating network would break it. TLC checked one step from every typed state of a three-node ring, plus the ordinary check from Init; it did not check larger rings or a lossy network."

A weak answer: "I added conjuncts until the induction check passed." It does not say why the invariant is true, and it never mentions the check from Init, which catches an Inv that passes the one-step check but is false in a reachable state.

Done?