Chapter

07

PlusCal: labels are atomicity

Part II · Safety and the language

  • 5 lessons
  • 3 h 45 min of core lessons
  • Skills: abstraction, counterexamples

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, TypeOK and Safety.

Run it

From the repository root:

shell
./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 07

    Hints

    Hint 1

    Write the submitter first and leave each worker as await FALSE for now. Run your cfg: the submitter alone should fill the pool and stop. Write TypeOK early, from the translation's VARIABLES line, as in 07-mx: while it is TRUE it 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")

    Safety has two parts. The first involves pool and Capacity. The second involves submitted, pool, job and done. Pick one job that has been handed in and ask how many of those places may hold it at the same moment. job is 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 --twin

    Hints

    Hint 1

    Write the spooler first and leave each printer as await FALSE for now. Run your cfg: the spooler alone should fill the tray and stop. Write TypeOK early, from the translation's VARIABLES line, as in 07-mx: while it is TRUE it 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")

    Safety has two parts. The first involves tray and Slots. The second involves spooled, tray, doc and printed. Pick one document that has been spooled and ask how many of those places may hold it at the same moment. doc is a function with one entry per printer, so "in a printer" is a question about every printer.