The counter in Lean
A model is only worth following if it’s right. A model in Java or TLA+ is right because it’s small enough to read. A model in Lean can also carry proofs: theorems about it, for every input, that Lean’s compiler checks. proptastic follows the code against the model, and the pass names what the model proved.
This page follows the same BatchedCounter from Follow a model,
bug and all, against a model of a few lines with one theorem.
The model
Section titled “The model”models/lean/Counter.lean, in the Lake project where models go:
/-- What a test does to the counter. -/inductive Op where | increment | reset
/-- One operation on the count. -/def step (i : Nat) : Op → Nat | .increment => i + 1 | .reset => 0
/-- The count after each operation, from zero. -/def counts (ops : List Op) : List Nat := (ops.scanl step 0).tail
/-- A reset clears the count, whatever came before it. -/theorem reset_clears (ops : List Op) : (ops ++ [Op.reset]).foldl step 0 = 0 := by simp [List.foldl_append, step]Opis what a test does to the counter.stepis the model: an increment adds one, a reset clears it.countsis the count after each operation of a run, from zero.reset_clearsis a theorem: whatever came before, a reset leaves 0.lake buildfails if the proof doesn’t hold.
The program
Section titled “The program”A Lean model runs as a program proptastic starts, speaking the model protocol.
Proptastic.serve, a small library in the same project, speaks it: the program names its outcomes
and its theorems, and answers each try.
/-- An operation as Java names it: `Op.INCREMENT` is `"INCREMENT"`. -/def Op.ofJson? : Json → Except String Op | .str "INCREMENT" => .ok .increment | .str "RESET" => .ok .reset | other => .error s!"no operation {other.compress}"
/-- One try's operations in, the count after each out. -/def answer (values : Array Json) : Except String Proptastic.Answer := do let ops ← ((← (values.getD 0 Json.null).getArr?).toList.mapM Op.ofJson?) let outcome := match ops.getLast? with | none => "untouched" | some .increment => "counting" | some .reset => "cleared" return ⟨outcome, Lean.toJson (counts ops)⟩
def main : IO Unit := Proptastic.serve "Counter" [⟨"untouched", "no operations"⟩, ⟨"counting", "the last operation is an increment"⟩, ⟨"cleared", "the last operation is a reset"⟩] [``reset_clears] answerEach try is a whole run of operations, a list, so the model sees the run at once and answers the
count after every step. Java names an enum’s constants in capitals and Lean its constructors in
lower case, so the model reads Java’s names, "INCREMENT", and keeps its own.
Start a model of your own from the template: the files of this one,
served at /lean-model/, lakefile.toml, lean-toolchain, Proptastic.lean, Counter.lean and
CounterMain.lean.
Build it
Section titled “Build it”Lean comes with elan, which installs the Lean that
models/lean/lean-toolchain names, the first time it runs. The build plugins run lake build in
models/lean before the tests:
./gradlew test runs proptasticLakeBuild first, once for each change to the Lean sources. See
Build plugins.
./mvnw test runs proptastic:lake-build first, once for each change to the Lean sources. See
Build plugins.
Without Lean installed, the build says how to install it and goes on, and a property that follows the model fails, naming how to build it: it never passes or skips on a model that isn’t there.
Follow it
Section titled “Follow it”The test draws a list of operations, runs it on a new counter, and answers the count after each:
/** What a test does to the counter, as the Lean model's {@code Op} reads it. */enum Op { INCREMENT, RESET}
/** The count after each operation, as the counter says it. */static List<Long> counts(List<Op> ops) { var counter = new BatchedCounter(); var counts = new ArrayList<Long>(); for (var op : ops) { if (op == Op.INCREMENT) { counter.increment(); } else { counter.reset(); } counts.add(counter.get()); } return counts;}@Test@Tag("failing")void theCounterFollowsItsLeanModel() { var ops = lists(any(Op.class)); forAll(ops).follows(lean("Counter"), CounterLeanTest::counts);}lean("Counter") runs the program Lake built for the model, counter. The model’s answer and the
code’s are compared as JSON, so [1, 2, 3, 4] from Lean and List.of(1L, 2L, 3L, 4L) from Java
are the same answer.
Read the failure
Section titled “Read the failure”does not follow model Counter 0.1.0 (Lean)model Counter said [1, 2, 3, 4], code said [1, 2, 3, 0] (outcome counting)
theCounterFollowsItsLeanModel failed after 3 tries (seed 0x5eed) shrunk counterexample: ops = [Op.INCREMENT, Op.INCREMENT, Op.INCREMENT, Op.INCREMENT] pin it: .example(List.of(Op.INCREMENT, Op.INCREMENT, Op.INCREMENT, Op.INCREMENT)) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'CounterLeanTest.theCounterFollowsItsLeanModel'The same four increments: after the fourth, the model counts 4 and the code 0.
Fix it, and read the pass
Section titled “Fix it, and read the pass”With the two lines swapped, the counter follows the model:
followed model Counter 0.1.0 on 100 tries (Lean; proved reset_clears) · outcomes untouched 12 · counting 55 · cleared 33 how sure: 0 of 100 tries failed, so under 3% of inputs drawn this way fail it (95% confidence) why: https://docs.proptastic.io/concepts/how-sure-is-a-pass/proved reset_clears is the theorem Lean checked when it built the model; a pass says it beside
the runs that matched, and the version names which build of the model they matched.
- The counter in TLA+: the same counter, its model a TLA+ spec that every step is checked against.
- From design to code: a design chosen with TLC, its calculation proved in Lean, and the code following both.