07-01

Chapter 07 · PlusCal: labels are atomicity

Read the translation first

  • 45 min core
  • Core track
  • Skills: abstraction, counterexamples

00Recall

One card from chapter 03 on what CHECK_DEADLOCK FALSE changes, one from chapter 01 on why a behavior never ends, and one from 06-05 on deadlock as TLC reports it.

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.

06-05-a from Deadlock as TLC reports it

Is deadlock part of what a TLA+ spec says?

Show answer

No. Init /\ [][Next]_vars allows stuttering in every state, so no behavior is ever stuck. Deadlock is a check TLC adds: a reachable state where Next itself is not enabled. Two specs with the same behaviors can differ in whether TLC reports one, and it reports finished runs and jammed systems alike.

Skip recall Recall is never required.

01Commit

In chapter 06 you wrote program counters by hand. specs/BankTransferByHand.tla is one more of those: two transfers, t1 and t2, each move Amount = 3 from Alice to Bob in three steps (read Alice's balance, write it back minus the amount, credit Bob), and nothing else. There is no step for "everyone has finished".

specs/BankTransferPlusCal.tla says the same thing in PlusCal, a small language of processes, assignments and labels that a translator turns into TLA+:

TLA+
process transfer in Transfers
variables seen = 0;
begin
  Read:   seen := alice;
  Debit:  alice := seen - Amount;
  Credit: bob := bob + Amount;
end process;

Both cfgs have the same lines: INVARIANT TypeOK, and deadlock checking left on. For the hand-written spec, TLC reports a deadlock once both transfers are done. For the PlusCal one it does not: chapter 06 promised that the translator adds a step for that moment, and this lesson reads it.

specs/BankTransferCapped.tla is the PlusCal version with one change. Bob's account is a prepaid card that holds at most Cap = 5, so a credit waits until it fits:

TLA+
  Credit: await bob + Amount less than or equal to Cap;
          bob := bob + Amount;

await P is read "wait until P". With Amount = 3, the first credit fits and the second never will. The cfg lines are the same as before, plus the cap, and the translator writes its extra step into this module too.

Commit a

BankTransferCapped is translated like BankTransferPlusCal, so its Next has the extra step, and deadlock checking is on. What does TLC report?

  1. No violation: the translator's extra step gives the end of the run a successor.
  2. A deadlock: one transfer finishes, and the other waits at Credit for good.
  3. No violation: a transfer waiting at an await is still running, so the state is not stuck.

Answer A deadlock: one transfer finishes, and the other waits at Credit for good.

Commit b

The uncapped PlusCal version passes. Compare its distinct states with the hand-written spec run with deadlock checking off (BankTransferByHand.ends.cfg).

  1. More: the extra step at the end adds a state.
  2. The same number.
  3. Fewer: the translator merges steps that cannot interfere.

Answer The same number.

02Lab

Run all three, hand-written first:

shell
./tla check chapters/07-pluscal/specs/BankTransferByHand.cfg
./tla check chapters/07-pluscal/specs/BankTransferPlusCal.cfg
./tla check chapters/07-pluscal/specs/BankTransferCapped.cfg
Expected output 07-pluscal/specs/BankTransferByHand
07-pluscal/specs/BankTransferByHand  BankTransferByHand.cfg  (Transfers={t1, t2}, Start=10, Amount=3)
parse ok
model check: deadlock reached; trace has 7 states (6 steps); 28 distinct states
expected outcome met: deadlock
trace (changed variables only; ./tla trace --full shows all):
  1  Init    pc = (t1 :> "Read" @@ t2 :> "Read"), alice = 10, bob = 0, seen = (t1 :> 0 @@ t2 :> 0)
  2  Read    pc: (t1 :> "Read" @@ t2 :> "Read") -> (t1 :> "Debit" @@ t2 :> "Read"), seen: (t1 :> 0 @@ t2 :> 0) -> (t1 :> 10 @@ t2 :> 0)
  3  Debit   pc: (t1 :> "Debit" @@ t2 :> "Read") -> (t1 :> "Credit" @@ t2 :> "Read"), alice: 10 -> 7
  4  Credit  pc: (t1 :> "Credit" @@ t2 :> "Read") -> (t1 :> "Done" @@ t2 :> "Read"), bob: 0 -> 3
  5  Read    pc: (t1 :> "Done" @@ t2 :> "Read") -> (t1 :> "Done" @@ t2 :> "Debit"), seen: (t1 :> 10 @@ t2 :> 0) -> (t1 :> 10 @@ t2 :> 7)
  6  Debit   pc: (t1 :> "Done" @@ t2 :> "Debit") -> (t1 :> "Done" @@ t2 :> "Credit"), alice: 7 -> 4
  7  Credit  pc: (t1 :> "Done" @@ t2 :> "Credit") -> (t1 :> "Done" @@ t2 :> "Done"), bob: 3 -> 6
Expected output 07-pluscal/specs/BankTransferPlusCal
07-pluscal/specs/BankTransferPlusCal  BankTransferPlusCal.cfg  (Transfers={t1, t2}, Start=10, Amount=3)
PlusCal translated (translation up to date)
parse ok
model check: no violation found in 28 distinct states (42 generated, depth 7)
expected outcome met: pass
coverage: all 4 actions fired
witness BothDone reached (trace of 7 states)
Expected output 07-pluscal/specs/BankTransferCapped
07-pluscal/specs/BankTransferCapped  BankTransferCapped.cfg  (Transfers={t1, t2}, Start=10, Amount=3, Cap=5)
PlusCal translated (translation up to date)
parse ok
model check: deadlock reached; trace has 6 states (5 steps); 25 distinct states
expected outcome met: deadlock
trace (changed variables only; ./tla trace --full shows all):
  1  Init    pc = (t1 :> "Read" @@ t2 :> "Read"), alice = 10, bob = 0, seen = (t1 :> 0 @@ t2 :> 0)
  2  Read    pc: (t1 :> "Read" @@ t2 :> "Read") -> (t1 :> "Debit" @@ t2 :> "Read"), seen: (t1 :> 0 @@ t2 :> 0) -> (t1 :> 10 @@ t2 :> 0)
  3  Debit   pc: (t1 :> "Debit" @@ t2 :> "Read") -> (t1 :> "Credit" @@ t2 :> "Read"), alice: 10 -> 7
  4  Credit  pc: (t1 :> "Credit" @@ t2 :> "Read") -> (t1 :> "Done" @@ t2 :> "Read"), bob: 0 -> 3
  5  Read    pc: (t1 :> "Done" @@ t2 :> "Read") -> (t1 :> "Done" @@ t2 :> "Debit"), seen: (t1 :> 10 @@ t2 :> 0) -> (t1 :> 10 @@ t2 :> 7)
  6  Debit   pc: (t1 :> "Done" @@ t2 :> "Debit") -> (t1 :> "Done" @@ t2 :> "Credit"), alice: 7 -> 4

The PlusCal runs start with a line the first does not have: ./tla ran the PlusCal translator before SANY. Open specs/BankTransferPlusCal.tla and scroll past the algorithm. The block between \* BEGIN TRANSLATION and \* END TRANSLATION is the translator's output, and it is what TLC checked.

Reveal for commit a

A deadlock, after a trace of 6 states. In the last state one transfer is "Done" and the other is at Credit, where bob + Amount <= Cap is false for good: only a credit changes bob, and the only credit left is the one waiting. The uncapped PlusCal version finds no violation in 28 distinct states, and the hand-written one deadlocks after a trace of 7 states, with both transfers done. The difference between those three endings is one disjunct of Next, called Terminating, and its guard.

Reveal for commit b

The same number: 28 distinct states by hand with deadlock checking off, 28 in PlusCal. The generated counts differ (39 and 42), because TLC also computes Terminating's step out of each of the three final states (they differ in what each transfer read), and each such step lands where it started.

03Reason

If you picked "no violation", you remembered chapter 06's promise, that the translator adds a step for the end of the run, and stretched it one state too far. The step exists, and it is more careful than that. Before reading it, put the translation next to the hand-written action:

TLA+
Read(self) is defined as and pc[self] = "Read"
              and seenprime = [seen EXCEPT ![self] = alice]
              and pcprime = [pc EXCEPT ![self] = "Debit"]
              and UNCHANGED tuple of alice, bob end tuple
TLA+
Read(t) is defined as
    and pc[t] = "Read"
    and seenprime = [seen EXCEPT ![t] = alice]
    and pcprime = [pc EXCEPT ![t] = "Debit"]
    and UNCHANGED tuple of alice, bob end tuple

Same guard, same updates, same frame. The translator did the bookkeeping you did by hand in chapter 06: a variable pc with one entry per process, one operator per label, a guard pc[self] = "Read" that says where the process is, an update that moves it to the next label, and an UNCHANGED list for everything the step leaves alone. The process-local variable seen became a function from processes to values, seen = [self \in Transfers |-> 0], because each transfer has its own copy. When a process runs past its last statement, its pc becomes "Done", a label nobody wrote.

Write something before openingthe translation has one action per label. Where does self come from, and what is it in TLC's trace?

process transfer \in Transfers declares one process per element of Transfers. The translator writes transfer(self) == Read(self) \/ Debit(self) \/ Credit(self) and puts \E self \in Transfers : transfer(self) into Next. So self is the process's own name: in the trace, t1 or t2, exactly where your hand-written spec had t.

The translation starts a run the way your hand-written Init did:

TLA+
Init is defined as (* Global variables *)
        and alice = Start
        and bob = 0
        (* Process transfer *)
        and seen = [self in Transfers maps to 0]
        and pc = [self in ProcSet maps to "Read"]

ProcSet is the set of every process name; here it is Transfers alone. Every process starts at its first label, and seen starts with one entry per transfer. The translator writes one action per label, and adds two things to the model that no label asked for: the label "Done" and the disjunct below. Everything else in the block you could have written yourself, and in chapter 06 you did.

Then the one line you did not write:

TLA+
(* Allow infinite stuttering to prevent deadlock on termination. *)
Terminating is defined as and for all self in ProcSet: pc[self] = "Done"

The rest of the definition, in the file, is /\ UNCHANGED vars. Once every process is done, Terminating is a step that changes nothing, and it is a disjunct of Next. TLC's deadlock check asks one question of each state: is there any Next step out of it? In the final state of the hand-written spec there is none, so TLC reports a deadlock. In the uncapped translation, Terminating is that step.

Now the capped run. Here is its Credit, translated:

TLA+
Credit(self) is defined as and pc[self] = "Credit"
                and bob + Amount less than or equal to Cap
                and bobprime = bob + Amount
                and pcprime = [pc EXCEPT ![self] = "Done"]
                and UNCHANGED tuple of alice, seen end tuple

The await became one more conjunct (07-03 has the rest of that story). While it is false, Credit is not a step at all. In the capped run's last state, one transfer is "Done", the other is at "Credit" with one Amount already on Bob's card, and no Read, Debit or Credit is possible. Terminating is not possible either: its guard asks every process, and one of them answers "Credit". No step at all, so TLC reports a deadlock.

You can see why the guard matters by imagining it gone. Suppose the translator added a do-nothing step to every state, unguarded. No state would ever lack a successor, and TLC's deadlock check would never fire again, for any PlusCal model you ever wrote: it would be CHECK_DEADLOCK FALSE in disguise. With the guard, a transfer stuck halfway, like the capped one, is not "Done", so Terminating is not enabled there and TLC still reports the deadlock. The guard limits the exemption to exactly the state you meant: everybody finished.

Write something before openingbelow Spec, the translator also wrote a definition called Termination. Your cfg does not mention it. Did TLC check it?

No. TLC checks what the cfg names: here INVARIANT TypeOK, plus deadlock. Termination starts with <>, read "eventually": it says every run eventually reaches a state where every process is done. That is a claim about whole behaviors, not about one state at a time, and checking it needs a PROPERTY line. For it to hold, the spec also needs fairness, as chapters 09 and 10 show. A definition in a module is a sentence nobody has asked TLC to test yet.

Write something before openingif the two specs are the same, why does the course make you read the translation at all?

Because the translation is the spec. TLC never sees your algorithm; it sees the TLA+ below it. When a trace surprises you, the action names in it are label names, and the variables are the translation's, pc included. And when the translator does something you did not intend (07-02 has an example), the only place you can see it is the translation.

Now check something that matters. BankTransferPlusCal.money.cfg adds MoneyConserved: once both transfers are done, Alice and Bob hold Start = 10 between them.

shell
./tla check chapters/07-pluscal/specs/BankTransferPlusCal.money.cfg

TLC finds the lost update from chapter 06, with a trace of 7 states. Read it: both transfers read Alice's balance before either writes it back, so one debit vanishes while Bob is still credited twice. Same bug, less typing.

04In your editor

The specs are in chapters/07-pluscal/specs/. Check this lesson's exercises with:

shell
./tla check 07-01

./tla check translates the algorithm before every run. When you run it on a starter or on your own file, it rewrites the translation block in that file, so the TLA+ below the algorithm always matches the algorithm above it. Never edit between \* BEGIN TRANSLATION and \* END TRANSLATION: your edit would be overwritten on the next run. If the translator rejects the algorithm, ./tla stops with its message before SANY runs. When the block already matched, the run's first line says PlusCal translated (translation up to date).

The PlusCal transfer, with every action and the witness:

Output 07-pluscal/specs/BankTransferPlusCal
07-pluscal/specs/BankTransferPlusCal  BankTransferPlusCal.cfg  (Transfers={t1, t2}, Start=10, Amount=3)
PlusCal translated (translation up to date)
parse ok
model check: no violation found in 28 distinct states (42 generated, depth 7)
expected outcome met: pass
coverage: all 4 actions fired
witness BothDone reached (trace of 7 states)

05Practice

Warm-up. Two petty-cash clerks, written by hand and in PlusCal. Write the PlusCal step that matches the hand-written one exactly. The course checks it with an action property: every step your algorithm takes must be a step the hand-written spec allows.

Core. An expense claim that is never paid, although its algorithm reads like the story and its translation has Terminating. Predict what TLC says, read the translation to find out why, and fix the clerk.

07-01-ex1Warm-up5 min

07-01-ex1 · Write the step

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

Two clerks pay a bill each from one petty-cash tin. PettyCashByHand.tla is the hand-written spec, with a program counter, as in chapter 06. PettyCash.tla is the same system in PlusCal, with one label left empty.

  1. Run ./tla check 07-01-ex1. TLC reports that the action property StepsMatchHand is violated. Read the trace: the last step is a Pay step. What did it change, and what does Pay(c) in PettyCashByHand.tla change?
  2. Fill gap g1 in PettyCash.tla: write the statements of the Pay label so that the step does exactly what Pay(c) does. Delete the skip and its \* GAP marker.
  3. Run the check again. Then scroll to the translation block and compare the translator's Pay(self) with the hand-written Pay(c), line by line.

StepsMatchHand is the course's check, defined below the translation: every step your algorithm takes must be a step the hand-written spec allows, or a step that changes nothing. Leave it and PettyCashByHand.tla as they are. ./tla check rewrites the translation for you; do not edit it by hand.

Hints: HINTS.md, under "07-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/07-pluscal/exercises/01-ex1-write-the-step/; open it after you have a passing answer of your own.

If TLC finds no violation but an action never fires or the witness BothPaid is not reached, ./tla prints "goal not met" and exits with code 1.

Done when: ./tla check 07-01-ex1 prints "goal met: pass".

./tla check 07-01-ex1

Repo chapters/07-pluscal/exercises/01-ex1-write-the-step/

Hints

Hint 1

Open PettyCashByHand.tla and find Pay(c). List the variables it gives a new value, and what each new value is.

Hint 2

Inside the process, seen is the clerk's own copy: the translator turns it into seen[self] for you. One label can hold several assignments, and they all happen in the same step.

Hint 3 (nearly the answer)

Replace the skip with two assignments under the Pay label: one for cash, computed from what the clerk counted and the bill, and one that adds a payment to paid.

07-01-ex2Core8 min

07-01-ex2 · Send before you wait

Tier: core. Time: about 8 minutes.

An expense claim needs every manager's approval before it is paid. In ExpenseApproval.tla, a clerk sends the claim, waits for the approvals and pays; each manager waits for the claim and approves it. The algorithm reads like the story. The cfg keeps deadlock checking on, and the translation has Terminating.

  1. Before you run anything, write down two predictions. First: what are the translation's ProcSet and the initial value of pc, with one clerk named "clerk" and managers m1 and m2? Second: does TLC report a deadlock, given that Terminating is there?
  2. Run ./tla check 07-01-ex2. Then open the translation and check both predictions against ProcSet, Init and the action Submit.
  3. Read the trace's last state, and for each process find the label it is at and the condition that label waits for. Who could make that condition true, and why can't they?
  4. Fill gap g1: fix the clerk so that the claim goes out, the clerk waits for every approval, and only then pays. Keep the labels Submit (the send) and Pay (the payment); you may add a label. Delete the \* GAP markers. Change only the clerk: the managers, the variables' initial values and the cfg stay as they are.
  5. Run the check again.

The cfg also checks three requirements, defined below the translation: the claim is paid only once every manager has approved it, a manager approves only a claim that was sent, and the claim goes out in the clerk's Submit step. A "fix" that deletes a wait, or starts with the claim already sent, fails one of them.

Hints: HINTS.md, under "07-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/07-pluscal/exercises/01-ex2-send-before-you-wait/; open it after you have a passing answer of your own.

If TLC finds no violation but an action never fires or the witness Paid is not reached, ./tla prints "goal not met" and exits with code 1.

Done when: ./tla check 07-01-ex2 prints "goal met: pass".

./tla check 07-01-ex2

Repo chapters/07-pluscal/exercises/01-ex2-send-before-you-wait/

Hints

Hint 1

The trace has a single state, so nobody can take even a first step. Find each process's label in Init, then find that label's action in the translation and read every conjunct it has, not only the first line of the algorithm.

Hint 2

The statements under one label are one action, and an action happens all at once or not at all. If one conjunct is false, none of the label's assignments happen, including the ones written above the await.

Hint 3 (nearly the answer)

The send and the wait need to be separate steps, so that the send can happen while the wait is still false. Give the await a label of its own, between Submit and Pay, or move it under Pay, above the payment.

06Explain it

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

After you writeCheck your explanation

A good answer: "In the starter, Submit's translation is sent' = TRUE /\ approvals = Managers: one action, so the claim could go out only after every approval, and the managers approve only a claim that is out. Every process waited for another, and Terminating could not help, because it needs every process to be "Done". I gave the await its own label, so the send is a step of its own; then the managers approve, the clerk pays, everyone reaches "Done", and Terminating gives that last state its successor. It relies on the send being a call that does not wait for an answer. TLC checked one clerk and two managers who always approve, and nothing about a manager who rejects or never replies, which would leave the clerk waiting for good."

A weak answer: "I split the label and the deadlock went away." It does not say why one label could not work (the await guards the whole step), and making a deadlock report go away is also what CHECK_DEADLOCK FALSE does.

Done?