DEV Community

Cover image for TurboLean compiler proof coverage
Madison Hope Steiner
Madison Hope Steiner

Posted on Originally published at mh0pe.github.io on

TurboLean compiler proof coverage

A compiler can translate a function without having a proof that the translation preserves its behavior. I wanted to know which functions the proof actually covered.

If a compiler gives me an executable, what would make me comfortable saying it preserves the program I wrote? A test can tell me the two programs agreed on the inputs I tried. A proof can tell me something more, but only about the translation it describes and under the conditions it names. I want both answers. I don't want either to stand in for the other.

That distinction shaped my work in the TurboLean fork. The project translates Lean programs through several representations before they run. I worked on one crossing in that chain: from Lean's LCNF representation to TurboLean's lower-level LNIR. The question was not how much of the compiler I could call verified. It was whether I could point to the exact translation, say what the theorem promises, and show where a particular function falls.

Make the theorem follow the code that runs.

The first problem was an implementation problem. It's easy to have a lowerer you can reason about and another version that the compiler actually calls. I removed the second, unproved route in my fork so the bridge and the proof could refer to one lowerer. That is what the first drawing follows: one translation, with the result it produces kept separate from what we can prove about that result.

I also split the evaluator for the incoming representation away from the lowerer. They still share definitions for the language they talk about, but the evaluator can describe how the source program runs without translating it first. That gave me a useful comparison in tests. If both sides go through the same translation just to tell me what the source means, it is much harder to know what that test has checked.

The split is not the theorem. It gives the theorem and the tests a cleaner object to talk about. And having one path is not an argument that every input to it is covered. The next question is what the theorem needs to know about the function it sees.

A function brings its callees with it.

Take a function that calls a helper, which in turn calls another helper. The first function might look simple. Its translation can still depend on what happens at the end of that chain. I changed the coverage check to follow those calls before counting the original function as proved.

In the fork's local-call measurement, the whole reachable chain has to be accounted for. The declarations have to lower, the relevant names have to agree, and the call sites have to use arguments the theorem handles. This check is for non-recursive calls. An unaccounted-for callee or a cycle keeps the caller out of the proved count. Where the function did lower, it can still be recorded as translated without proof. That is information, not a verdict that the generated program is wrong.

This matters because a proof with a condition does not fail to be useful when the condition is narrow. My job was to stop the reporting code from erasing the condition. I wanted a reader to be able to explain why an entry moved into the proved column, and why the next entry didn't.

Stop the claim where the theorem stops.

The local-call theorem is a one-way result. If lowering succeeds, the environments and context agree, and the source evaluator successfully runs the function with the given fuel bound, the target evaluator returns the same value and final modeled heap. Fuel bounds the evaluation in the model. It isn't a measure of time. If source evaluation fails, or never finishes, this theorem doesn't tell me what the target does.

In the separate recursion work, I fixed a case that made the heap comparison matter. Both modeled runs returned zero. One freed an object and the other left it live. A test that checked only the return value would have missed it. That fix closed a specific gap, not the full recursive lowering result. And neither result proves the native binary correct. Code generation after LNIR, the rules for calling native functions, and what runs on the machine are outside the comparison.

There is a related mistake in code review. A theorem's statement can stay the same while its proof body changes. Some importing modules can observe that body during checking. In the fork I kept the edit classified as proof-only, but had the verifier recheck dependents when there were importers. That is more work than rechecking only the edited file. It is also a cost a reviewer can see and explain.

Put the denominator beside the claim.

The receipt changed how I would talk about coverage. In the pinned run, 880 of 902 declarations in the targeted C1 tests counted as proved. In the pinned Batteries library, 53 of 10,658 did. Neither number is a correction to the other. One set exercises the constructs the work was targeting. The other makes it harder to mistake that success for coverage of a larger body of Lean code.

The most useful result was a number that went down. On the same Batteries input set, the previous count was 59. When I fixed the classifier to see a helper call after a reference-count or set operation, ten of those entries no longer qualified as call-free. Applying the local-call rule admitted four. The result was 53. The intermediate 49 is a reclassification of the recorded facts, not another run. If I had optimized for the headline number, I would have missed the correction that made it worth publishing.

I want the receipt to travel with the result, so it records the measured source revision, Lean 4.35.0, the inputs, and digests of code used to produce the classification. It also records translated but unproved declarations separately from rejected ones. Compiler-generated auxiliaries were excluded from these denominators. A label that says a declaration was rejected is about this measurement path, not a claim that Lean cannot compile it.

The roadmap pull request I opened was closed as superseded. The maintainer credited the work and carried pieces into narrower changes. Later upstream work on bounded recursion is a separate result with its own conditions. The numbers shown here remain the measured fork snapshot, not measurements of upstream today.

Read every category in the pinned receipt

  1. Targeted C1 tests: 880 proved, 0 translated without proof, 22 rejected, out of 902 declarations.
  2. Std/Init + C2: 48 proved, 65 translated without proof, 1,692 rejected, out of 1,805 declarations.
  3. Pinned Batteries: 53 proved, 1,066 translated without proof, 9,539 rejected, out of 10,658 declarations.

The rejected-but-not-lowered count in the source is part of rejected, not a fourth category.

What I take from it

Before I repeat a claim about a verified compiler, I want to know which path it takes, which behaviors the theorem compares, and what the count leaves out. If I can't trace those answers back to the work, what exactly am I asking someone else to trust?

Code and further reading

Read the illustrated essay on mh0pe.github.io

Top comments (0)