The counter in TLA+
Follow a model kept the counter’s model in the test, in Java. That works until the model is a design several people read, or more than one test follows: then it belongs in a language made for designs, checked by a tool of its own. TLA+ is one. Its model checker, TLC, explores every state the spec allows, and proptastic follows the code through that graph, checking after every step that the step is one the spec allows.
This page follows the same BatchedCounter, bug and all, against a spec of a few lines.
The spec
Section titled “The spec”models/tla/Counter.tla, where models go:
EXTENDS Naturals
CONSTANT MaxVARIABLE i
Init == i = 0
Increment == i < Max /\ i' = i + 1
Reset == i' = 0
Next == Increment \/ Resetiis the count.Initstarts it at 0.Incrementadds one, whileiis belowMax: TLC explores a finite graph, so the spec stops counting somewhere.Resetclears it.Nextis either step.
TLC checks it by the config of its own name, models/tla/Counter.cfg, which says how far to count:
CONSTANT Max = 8INIT InitNEXT NextDump the graph
Section titled “Dump the graph”The test doesn’t run TLC. It follows the graph TLC dumps from the spec: every state the spec
allows, and every step between them, each named by its action. For the counter, that’s i from 0
to Max, an Increment from each i below Max to the next, and a Reset from each back to 0:
i = 0 ──Increment──▶ i = 1 ──Increment──▶ i = 2 ──▶ … ──Increment──▶ i = 8 ▲ │ └──────────────────────── Reset, from any i ─────────────────────────┘After each step, proptastic looks in that graph for an edge from where the counter was, named by
the step’s action, to where the counter is now. Reading a graph is quick and the same on every
machine, so the tests need neither TLC nor the time it takes, and a change to the spec shows up as
a change to a file you commit. The build dumps it, once for each change to the spec, to
models/tla/Counter.dot:
./gradlew proptasticTlc./gradlew check runs proptasticTlcCheck, which fails until the committed graph is the one TLC
dumps from the spec. See Build plugins.
./mvnw proptastic:tlc./mvnw verify runs proptastic:tlc-check, which fails until the committed graph is the one TLC
dumps from the spec. See Build plugins.
Follow it
Section titled “Follow it”The test takes the counter’s two steps, and spec("Counter", …) hooks them to that graph: it reads
models/tla/Counter.dot, found by the spec’s name. Its other two arguments say how to see the
counter in the spec’s terms:
stategives the spec’s variables from the counter: itsiis the counter’sget().withinMaxsays whether the counter is still inside the graph, which TLC explored only up toMax = 8, the config’s value. Keep the two the same.
And each step’s name is an action’s: increment is Increment, and reset is Reset.
static Steps<BatchedCounter> counting() { return steps(BatchedCounter::new) .step("increment", BatchedCounter::increment) .step("reset", BatchedCounter::reset);}
/** The spec: its variable i is the counter's get(), checked up to Max. */static Spec<BatchedCounter> counter() { return spec("Counter", CounterSpecTest::state, CounterSpecTest::withinMax);}
static Map<String, Long> state(BatchedCounter counter) { return Map.of("i", counter.get());}
static boolean withinMax(BatchedCounter counter) { return counter.get() <= 8;}@Test@Tag("failing")void theCounterFollowsItsSpec() { forAll(counting()).follows(counter());}There’s no model in the test now: after each step, the counter’s i must be one the matching
action reaches in the graph from the i before it.
Read the failure
Section titled “Read the failure”does not follow spec Counter sha256:989bea44 (TLA+)step 4 is not one spec Counter allows: increment() allowed i = 4 did i = 0 differs i 4 → 0
theCounterFollowsItsSpec failed after 7 tries (seed 0x5eed) shrunk steps: 1. increment() 2. increment() 3. increment() 4. increment() pin it: .example( step("increment"), step("increment"), step("increment"), step("increment")) // typed pins come from @StepBuilder rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'CounterSpecTest.theCounterFollowsItsSpec'The same bug, named at the step: after the fourth increment the spec’s Increment allows only
i = 4, and the counter did i = 0. The shrunk run is the same four increments as before, and
there’s no model in the test to get wrong.
Fix it, and read the pass
Section titled “Fix it, and read the pass”With the two lines swapped, as in the quickstart, the counter follows the spec:
followed spec Counter sha256:989bea44 on 100 runs (TLC: 10 states, 15 of 19 edges reached) · actions Increment 310 · Reset 348 · left the model 0% how sure: 0 of 100 runs failed, so under 3% of runs drawn this way fail it (95% confidence) why: https://docs.proptastic.io/concepts/how-sure-is-a-pass/- TLC: states and edges is the size of the graph, and how much of it the runs reached.
- actions counts the steps of each kind. Every action must come up, or the pass fails.
- left the model is the share of steps taken past what TLC checked, here increments past
Max. The rest of such a run is counted, not checked, unless the spec is followed by TLC traces, below.
Choose the bound
Section titled “Choose the bound”Max bounds the graph TLC explores, and every increment past it leaves the model. Runs average
eight steps, so with Max = 8 most stay inside; with Max = 3 most would leave, and the pass
warns when more than half of the changing steps do. Either raise the constant (the graph grows by
one state for each, and past 10,000 states the tests follow the spec by
TLC traces instead) or keep the step inside it with a precondition:
.step("increment", BatchedCounter::increment).when(counter -> counter.get() < 8).
Follow it past the graph
Section titled “Follow it past the graph”A graph is worth committing up to about 10,000 states, 3 MB of .dot. Most real specs pass that
at constants worth testing: the counter at Max = 1000000 would be a million states and 300 MB.
Past 10,000, the build writes no graph, and says why. Here the same spec, named BigCounter, is
checked by a config that counts to a million:
CONSTANT Max = 1000000INIT InitNEXT Nextproptastic: models/tla/BigCounter.tla has more than 10,000 states, too many for a graph worth committing, so none is written: a test follows it by TLC tracesWith no graph beside the spec, spec(…) follows it by TLC traces. Each run’s steps are kept as
it goes. Once the runs are done, every step goes to TLC in one job for the property, at the
constants the config sets, and TLC judges each: is it the spec’s step for that action, from the
state before it? Each distinct step goes once, however many runs took it, so the job stays small:
/** The same spec, counting to a million: no graph, so TLC judges the steps. */static Spec<BatchedCounter> bigCounter() { Predicate<BatchedCounter> withinMax = counter -> counter.get() <= 1_000_000; return spec("BigCounter", CounterSpecTest::state, withinMax);}
@Test@Tag("failing")void theCounterFollowsItsSpecPastTheGraph() { forAll(counting()).follows(bigCounter());}The batching bug fails as it did, with the same message, at the same step:
does not follow spec BigCounter sha256:685ac333 (TLA+)step 4 is not one spec BigCounter allows: increment() allowed i = 4 did i = 0 differs i 4 → 0
theCounterFollowsItsSpecPastTheGraph failed after 1 try (seed 0x5eed) shrunk steps: 1. increment() 2. increment() 3. increment() 4. increment() pin it: .example( step("increment"), step("increment"), step("increment"), step("increment")) // typed pins come from @StepBuilder rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'CounterSpecTest.theCounterFollowsItsSpecPastTheGraph'When TLC finds a step it doesn’t allow, the runs are run again, each step judged as it’s taken, so
the first run that took it fails there and shrinks as before. A step a shrink reaches that TLC
hasn’t judged goes to TLC as its run ends. The allowed line takes one more TLC run, made once,
for the failure that’s shown. Fixed, the pass says which way the spec was followed:
followed spec BigCounter sha256:685ac333 on 100 runs (TLC traces: 673 steps, 16 distinct) · actions Increment 322 · Reset 351 · left the model 0% how sure: 0 of 100 runs failed, so under 3% of runs drawn this way fail it (95% confidence) why: https://docs.proptastic.io/concepts/how-sure-is-a-pass/- TLC traces: steps and distinct count the steps the runs took, and how many of them TLC judged: the counter’s runs keep taking the same few steps.
- It costs a TLC job a property, about 0.7 s, most of it starting TLC’s JVM, where reading a graph costs milliseconds. A graph stays the faster way while one fits.
The two ways check different things, so the pass names its way. Followed by its graph, TLC checked every behaviour of the spec within its constants, and every step every run took was a step in that graph. Followed by TLC traces, every step every run took is one the spec allows, at the constants the config sets, however large; TLC explores nothing else, and checks the config’s invariants only on the state a failure reached. Either way the runs are drawn, so the pass says how sure it is; neither covers sequences no run took, or liveness.
TLC judges a step from the whole state, so spec(…)’s state must give every variable the spec
declares, and a spec with one the code doesn’t show is refused. TLC’s integers are 32 bits: a state
with a value past them has left the model, as a state outside the constants has. TLC traces need
TLC where the tests run, which the build plugins set as PROPTASTIC_TLC; without it the pass says
the steps weren’t checked.
- The counter in Lean: the same counter, its model a Lean function with a theorem about it.
- Follow a model: a spec with arguments, invariants and a failure that names which one broke.