Chapter
07
PlusCal: labels are atomicity
Part II · Safety and the language
Lessons
About
The same algorithm, with labels in three places. Two ticket sellers, one seat left. Which version can sell the seat twice?
PlusCal is a small language of processes and assignments that a translator turns into TLA+. It writes the program counter, the frame and Next for you, so you can model the code you have without the bookkeeping of chapter 06. The one thing it asks of you is the one thing that matters in concurrent code: where a process can be interrupted. That is what a label says.
The goals: read a translation, and know what each line of it came from; place labels where the real system can be interrupted, and predict what moving one does; use processes, await, either and with; and model a bounded producer and consumer.
Before you start
- Chapter 06: program counters by hand, atomicity, mutual exclusion, and deadlock as TLC reports it.
- Chapter 03's witness rule and chapter 05's functions and sets.
- Time: the chapter page shows the estimate, generated from the lessons.
What else is here
specs/: the models the lessons run.review/(07-review): a teammate reorders a release pipeline's two writes and calls them independent.ai/1-coarse-labels/(07-ai1): an AI translates an English mutex description into PlusCal, and its labels are too coarse.ai/2-lock-free-ring/(07-ai2): an AI models a lock-free ring buffer in PlusCal. Decide, with evidence, whether its claims hold.gate/(07-gate): a bounded job pool with two workers;gate/twin/(07-gate-twin): a print spooler. The skeletons supply the interface; you write the algorithm,TypeOKandSafety.
Run it
From the repository root:
./tla check 07-01
./tla check 07-review
./tla check --gate 07./tla check translates PlusCal before every run and keeps the translation in each file up to date. Never edit the translation by hand.
Skills
abstraction (choosing what is one step), counterexamples (reading them).
Elsewhere
- Web version: https://indicogni.com/learn/tla-plus/07-pluscal/
- Recaps:
CONCEPTS.md. Hints and explanation checks:HINTS.md. Predictions without answers:PREDICT.md. - Solutions:
solutions/chapters/07-pluscal/, including the gate references and their mutants. - Peterson's algorithm is from G. L. Peterson, "Myths about the mutual exclusion problem", Information Processing Letters 12(3), 1981. PlusCal is described in L. Lamport, "The PlusCal Algorithm Language", ICTAC 2009, and in the PlusCal manual on Lamport's TLA+ site.
Beyond the lessons
- 07-review Review a teammate’s spec ·
chapters/07-pluscal/review/·./tla check 07-review - 07-ai1 AI item ·
chapters/07-pluscal/ai/1-coarse-labels/·./tla check 07-ai1 - 07-ai2 AI item ·
chapters/07-pluscal/ai/2-lock-free-ring/·./tla check 07-ai2 - 07-gate Gate ·
./tla check --gate 07Hints
Hint 1
Write the submitter first and leave each worker as
await FALSEfor now. Run your cfg: the submitter alone should fill the pool and stop. WriteTypeOKearly, from the translation'sVARIABLESline, as in07-mx: while it isTRUEit checks nothing.Hint 2
When must a worker stop for good, and what must be true before it can take a job? Write both conditions in words before you write any PlusCal. Then ask, for each of the worker's two steps, which variables it changes.
Hint 3 (nearly the answer; opening it marks a pass as "passed with help")
Safetyhas two parts. The first involvespoolandCapacity. The second involvessubmitted,pool,jobanddone. Pick one job that has been handed in and ask how many of those places may hold it at the same moment.jobis a function with one entry per worker, so "in a worker's hands" is a question about every worker. - 07-gate-twin Gate twin, another domain ·
./tla check --gate 07 --twinHints
Hint 1
Write the spooler first and leave each printer as
await FALSEfor now. Run your cfg: the spooler alone should fill the tray and stop. WriteTypeOKearly, from the translation'sVARIABLESline, as in07-mx: while it isTRUEit checks nothing.Hint 2
When must a printer stop for good, and what must be true before it can grab a document? Write both conditions in words before you write any PlusCal. Then ask, for each of the printer's two steps, which variables it changes.
Hint 3 (nearly the answer; opening it marks a pass as "passed with help")
Safetyhas two parts. The first involvestrayandSlots. The second involvesspooled,tray,docandprinted. Pick one document that has been spooled and ask how many of those places may hold it at the same moment.docis a function with one entry per printer, so "in a printer" is a question about every printer.