Conao3 Notes

leanassay: mutation testing for Lean 4 theorems, to measure how much they pin down the implementation

I built leanassay, a mutation testing tool for Lean 4 theorems that measures theorem quality: it runs broken versions of your functions against your theorems. The idea comes from mizchi's Japanese article おい、 Mutation Test をやれ。テスト品質を、測るために ("Hey, run mutation tests, to measure test quality"), which says that before you trust coverage raised by AI, you should make mutants by rewriting the code and check whether the tests fail.

The same holds for proofs in Lean. If a broken implementation satisfies the same theorem, the theorem says nothing about the implementation, even when the proof passes. Now that AI writes proofs for us, I think the question to ask is how much a theorem pins down the implementation, more than whether the proof passes. In a project that mapped Julia to Lean, 128 of 130 "proved" bounds checks only assumed that the index was in range.

Lean can run both theorems and implementations, so I brought mutation testing into Lean: the tool breaks the functions that a theorem mentions and runs the theorem to see whether it still holds. I tried it on the theorems I had proved myself for lsp-touchstone, and every theorem still held for a state machine that broke the spec and for a conversion with the wrong UTF-16 width.

I also ran it on six of my tools that turn Lean functions into Rust, and in the functions I audited it found 56 holes in two of the tools, so I added theorems that close all 56. Two holes remain, in leanwisp's fields, which I left out of the audit.

The tool, leanassay, is at conao3/lean-leanassay under the Apache-2.0 license. The results in this post come from Lean 4.35.0 and leanassay at commit ef054d7, whose Lean code is the same as in 0.2.0.

A broken implementation that passes the proofs

lsp-touchstone is a tool that checks whether LSP messages follow the spec. It describes in Lean how progress notifications move the state, and before I ran leanassay it had six proved theorems.

def step : TokenState → ProgressKind → Option TokenState
  | .created, .begin => some .begun
  | .begun, .report => some .begun
  | .begun, .finish => some .ended
  | _, _ => none

LSP progress goes through begin, some report messages, and end, in that order and only once. In the code, .finish is the LSP end, and I call each line that starts with | an arm. Let's add one arm that takes begin in the begun state and stays in begun.

| .begun, .begin => some .begun

With this arm added, the six theorems still pass with the original proofs. I had not written any theorem that forbids a second begin, and lsp-touchstone uses this step to detect violations, so with this arm it would miss a second begin.

Every theorem is true and every proof passes, so the problem is in the theorems I did not write. You cannot find those by rereading the proofs, however many times you do.

What leanassay does

leanassay makes broken versions of the functions that appear in a theorem, and runs the theorem with each version. It does not use the proof: it evaluates the theorem's premises and conclusion on concrete inputs, so if it finds an input where the theorem fails, the theorem has killed that mutation, and if the theorem holds on every input, there is a hole.

mizchi's article frames unit tests as the quality check for the implementation and mutation tests as the quality check for the test code, and leanassay puts theorems in place of the tests to measure how much your proved theorems guarantee about the implementation.

It breaks functions in two ways.

  • Replace the whole function with a wrong one, such as a function that always returns default or none, one that returns its argument, or one that returns only the first element of a list argument.
  • Change the body a little: swap a comparison or arithmetic operator, shift a number literal by one, swap take and drop, replace the result of a match arm with another arm's result, add an arm for a combination of constructors that no arm covers, or swap the branches of an if.

If a broken version returns the same value as the original on every input it tried, leanassay treats it as not broken and leaves it out of the report, and it reports as holes only the versions whose output changed but still passed the theorems.

In the terms of mizchi's article, a version that a theorem kills is Killed, a hole is Survived, and a version left out of the report is an equivalent mutant.

The inputs come from the generators of plausible, a Lean library that tests properties on random inputs. The seed is fixed, so repeated runs give the same result. When an argument type has few values, leanassay tries every combination, and for a theorem whose premise holds for few inputs, it tries more inputs until it has enough.

Usage

Add leanassay to lakefile.toml and pass the function you want to audit to #assay.

import Assay
import LspTouchstone.Progress

#assay proved.step

leanassay collects the theorems that mention the function, and the theorems that mention functions that call it. When a theorem's conclusion is joined with ∧, it gives each part its own column, and it prints a table of which theorems killed each broken version.

img

The figure shows four rows from the output table, with shorter names for the mutations.

✗ means it found an input where the theorem fails, · means the theorem held on the inputs it tried, and ? means it could not check because it found almost no inputs that satisfy the theorem's premise in the broken version. At least one theorem killed 17 of the 19 mutations, and the two that add an arm taking begin in begun remained as holes. For each hole it prints a warning with an input where the output differs from the original, and for this hole it reports that the output differs for the begun state with begin.

With set_option assay.failOnHole true it reports holes as errors, so if you include this file in the build, lake build fails while any hole remains.

Results on lsp-touchstone

I ran it on the three parts of lsp-touchstone that have proofs.

Part Mutations Killed Holes
Progress state transitions 19 17 2
Position unit conversion 72 43 15
Document range replacement 12 12 0

Of the other 14 in the position unit conversion, 12 could not be built because the types did not match, 1 returned the same values as the original on the inputs it tried, and 1 could not be checked because too few inputs met the premise.

The position unit conversion uses unitWidth to decide how many units one character takes in UTF-8, UTF-16, and UTF-32; for example, 😀 counts as 4 in UTF-8, 2 in UTF-16, and 1 in UTF-32. But when I changed this width to any other value of at least 1, all four theorems still held. The theorems only required the width to be at least 1, and did not say which characters count as 2 in UTF-16. leanassay reported as holes a version that changes the comparison at the UTF-16 width-2 boundary from < to ≤, and a version whose UTF-16 width is always 1.

The other hole was about positions that point inside a character. The theorem only said "if it judges that a position points inside a character, the position is inside a character", and there was no converse theorem saying "a position inside a character is always judged to be inside a character". Because of this, a version that judged positions inside a character after the second to be character boundaries or line ends passed every theorem.

Based on these results, I added three theorems to lsp-touchstone.

  • In the begun state, it accepts only report and end.
  • A position inside a character is always judged to be inside a character.
  • The width of one character equals Lean's Char.utf8Size in UTF-8; in UTF-16, only characters of 4 bytes in UTF-8 have width 2; in UTF-32, it is 1.

With these theorems, none of the 19 progress mutations or the 72 position mutations remain as holes. The commit that adds the theorems and the audit results from before and after are in the repositories.

Results on my other tools

Besides lsp-touchstone, I have six tools that use Lean functions turned into Rust with leanfran. I put #assay on the functions that Rust calls, and made make check run the audit.

Tool Functions Mutations Killed Holes
leanwisp (Lean formatter) 24 155 140 0
leansail (Lean language server) 7 420 385 0
leankeel (Lean linter) 7 40 40 0
leanwake (finds Lean declarations that got slower) 3 10 10 0
ritsuryo (statutory periods and tax amounts) 5 14 10 0
ritsulint (linter for statutes) 17 203 137 0
Total 63 842 722 0

The mutations that were not killed are versions that could not be built because the types did not match, and versions that returned the same values as the original on the inputs tried.

I also left some functions out of the audit. I left out leanwisp's key, which builds cache keys, because fields, the function it builds on, still has two holes.

I left out leanwisp's four constants for the exit codes and the minimum and maximum line width as well, since a theorem about one of them would restate its value.

ritsuryo's describe, which returns the list of commands, has no theorems. In ritsulint, analyze, which finds the provisions each read-as clause refers to, and relativeRefs, which finds references by position, have no theorems either, and I check the output of these three with tests.

Before the audit, leanwisp had 17 holes and leansail had 39, and no theorem mentioned 7 of the audited functions in ritsulint, 5 in ritsuryo, 3 in leanwisp, and 1 in leankeel.

Most holes were in theorems that stated only an upper bound or only one direction. For example, leanwisp's softblockIndent computes the indent width with min indent.toNat (base + 2), and its theorem only said that the result exceeds neither value, so a version that always returns 0 and a version that turns + into - both passed. After I added a theorem that the result equals one of the two values, the hole went away.

In leansail, the language server's state transition function transition had 39 holes. The original theorem was an implication, "if it moves to running, the previous state is one of these", and only 3 of 63 inputs met the premise, so many broken versions went unchecked. I rewrote it as an ↔ and added an ↔ theorem for each target state, and all 39 holes went away.

Each hole was a missing theorem, and I did not have to change any function definition. mizchi's article recommends using mutation testing as a tool for refactoring rather than running all mutants in CI every time, but I made each tool check in make check that there are no holes, so that I notice when a theorem is removed or weakened. For example, if I remove initializing_iff_initialize, which I added to leansail, 29 holes appear and the build fails.

How it differs from tools that check whether the proof breaks

Lean already has a tool that breaks functions to measure how strong theorems are. Mutate.lean changes a function body and checks whether the proofs in the file stop passing, and Microsoft CCF used it to look for holes in its Lean model.

But when you judge by whether the proof breaks, the result depends on how the proof is written. If I write by hand the version that judges a position inside a character to be a character boundary, simp closes the goal in one branch of the case split and the tactics after it have nothing left to do, so the proof of resolve_splits fails. By that measure the version counts as killed, and you do not see that the converse theorem is missing. leanassay runs the theorem's statement itself, so it finds that this version satisfies the theorem, and it also works on theorems whose proof is still sorry.

For the progress state transitions, Mutate.lean makes only two well-typed mutations, and it found no holes. The holes that leanassay found came from the mutation that adds an arm, and Mutate.lean does not make that kind of mutation.

What it cannot check

leanassay can only check theorems that it can run. The premise and conclusion must be decidable propositions and the argument types need plausible generators, so it reports theorems about real numbers or infinite sets as theorems it cannot run.

It tries a sample of inputs, so when it finds no hole, that only means it found none on the inputs it tried. A version that it treats as not broken and leaves out of the report also only returned the same values as the original on the inputs it tried.