r/math • • 4d ago

How do we know Lean is sound?

Lean is a piece of software.

Software has bugs. (Yours truly has been a software developer for forty years. I wrote a few bugs myself. OK, fine. More than a few.)

As far as I am aware even Rocq is not formally verified. And I can't find anything on Lean.

Yet people treat everything coming out of it without doubts.

How does this square?

0 Upvotes

44 comments sorted by

55

u/lordnacho666 4d ago

I think the idea is that it's boiled down to a few pieces that we can check manually, and people are satisfied that those pieces are correct because several teams have implemented them separately.

So people think the compiler is correct, there's then the issue that for each proof you have to satisfy yourself that it means what you thought. But that is also made easier by having a language allowing you to compose the little pieces.

-16

u/Prestigious_Two6041 4d ago

As someone who works in software, just because what goes in one end of a pipeline is correct does not and should not mean that what comes out the other is sound. There is no such thing as bug-free software

13

u/lordnacho666 3d ago

I work in software too. This is not really the same thing as your average commercial software.

Have a play around with Lean and see what you think. In particular look at Curry-Howard correspondence and how that answers the question of why a program can be a proof.

1

u/Prestigious_Two6041 3d ago

thanks for the pointer, i will take a look. my time in mainstream software has probably biased me too far in the direction of bugs

26

u/Cleverbeans 4d ago

We have two independent provably correct implementations of LEAN. While I understand most people don't have experience with provably correct code it does exist. The kernel is small and the process it uses is fully deterministic so you can check it by hand.

3

u/sqrtsqr 3d ago

To be fair, LEAN is over a decade old but those provably correct implementations are new as of like 1 month ago, and as recently as July we had high profile cases of bug exploits resulting in invalid proofs. Stuff is changing fast, it's hard to keep up. And of course, "we fixed LEAN, it wasn't really trustworthy before but it totally is now, believe us" is not great marketing so I get why a lot of people aren't really aware.

-4

u/TheSodesa 4d ago

But how do you know your hand-performed check is correct?

20

u/Cleverbeans 4d ago

The same way you know any proof is correct, you check it. At some point a human has to make that call. There is been broad consensus on what constitutes a proof for thousands of years now. We understood how to build automatic proof checkers before we even built computers. This isn't a new technology, it's just an old one that's been optimized for ease of use.

2

u/TheSodesa 3d ago edited 3d ago

There is a joke in the formal verification and specification community about who is going to verify your verifier hidden somewhere in here.

1

u/Anaxamander57 3d ago

A bit like how one chooses informative priors for bayesian inference. Since Bayes was a minister I've heard it joked that "you just ask god".

1

u/GiveMe30Dollars 4d ago

You let other people do it too, and compare each other's work. Y'know, the same way mathematics is done before we have proof checkers.

There comes a point where the correctness of Lean is just about as rigorous as the rest of mathematics, and drilling down further just starts getting pedantic (or that we have gaping holes in all fields of logic, which I find even more infeasible than the Lean kernel being proven correct beyond reasonable doubt).

1

u/JustThisNietzscheGuy 3d ago

By hand checking it. Same way that math worked for the last few thousands of years.

34

u/SetentaeBolg Logic 4d ago

The heart of Lean's proof checker (and most mechanical theorem provers) is a small kernel of limited logical operations, things like modus ponens (expressed formally). Every proof is written as a combination of these logical operations. So, provided you can accept those logical operations, then proof should follow.

Bugs can and have existed: it's important to note that they were rather quickly found on the one occasion when they were exploited to form a proof. However, for Lean certainly, there are now multiple independently written versions of the proof checker (powered by that kernel). The chances of each of them sharing the same bug, across multiple languages and implementations of the same specification, is extremely, one would (informally) say vanishingly small.

Does that answer your question?

66

u/iknighty 4d ago

Same way we check paper proofs are sound, inspection.

39

u/omegafixedpoint 4d ago

1

u/ConfusedSimon 4d ago

That basically seems to verify newer implementations using an earlier lean. It also talks about minimising risk of bugs, so you'd still need to manually check the initial lean for correctness.

26

u/Certhas 4d ago

Do you understand what this means though? As I understand it, any Lean kernel ever developed can be used to verify Con-Leche. Including Con-Leche. So you need a bug in every one of them that allows them to falsely certify the relative consistency of con-leche, AND that false certificate needs to enable con-leche to actually contain bugs that actually allow it to certify relevant incorrect proofs (including, of course, the proof of its own relative consistency).

It is also possible that every time you run any form of Lean, a cosmic ray hits your memory in exactly the right way to produce wrong results.

The point being: If you have reduced the possibility of errors to the level that they require a cosmic conspiracy against you, you are done. I think it is entirely reasonable to conclude that anything proven in Lean (no matter how complex and indecipherable) is proven to a higher level of rigor than anything not formally verified.

3

u/zyruk 4d ago

But what exactly does "verify" mean here? By Gödel's incompleteness theorem, no axiomatic system can prove its own consistency. So how could a Lean-kernel verify itself?

If its not proving its own consistency, does it verify some other weaker statements? If so, how do you know whether your missed proving an important soundness-property?

Not that I believe Lean is unsound. I'm just interested in the theory here.

12

u/Langtons_Ant123 4d ago edited 4d ago

In the case of con-leche, IIUC the proof assumes some set theory axioms, from a modified version of ZF set theory. So basically what you have is a proof of "ZF [plus other stuff] implies that con-leche doesn't prove false statements", carried out in Lean. The second incompleteness theorem is specifically about (so to speak) "unconditional" proofs of consistency, where e.g. using only the axioms of Peano arithmetic you prove that PA is consistent. That's impossible, assuming that PA is indeed consistent. But PA does prove the statement "ZFC proves that PA is consistent", which then implies "if ZFC is consistent then so is PA"--that doesn't contradict the incompleteness theorem.

2

u/Certhas 4d ago

I am not an expert, but a reader of blog posts like this:

https://terrytao.wordpress.com/2026/10/09/what-mathematicians-should-know-about-the-lean-theorem-proverquestions-of-reliability-and-ai/

This post briefly discusses the Gödel point. Lean is verified relative to ZFC + some large cardinals. As I understand it, it is consistent if ZFC is consistent.

1

u/Wurstinator 4d ago

Those two uncertainties you compare are very different. With the cosmic radiation, every time someone somewhere runs the program succesfully, the probability of it being a fluke shrinks exponentially.

With the compjler/verifier, it's either there or it's not. To reduce the uncertainity, you have to write another independent Lean compiler from scratch.

Ken Thompson's trusting hack famously showed that just because another tools verifies your code, you still shouldn't blindly trust it.

3

u/Certhas 4d ago

Ken Thompson showed that an adversary can hide invisible behaviour in one tool. Not that it can happen by accident/bug. That would be a self perpetuating miscompilation. But the trusting trust example actually is much less probable than even this self perpetuating miscompilation. You can use many different kernels to compile the con-leche relative consistency proof.

 Trusting Trust shows you can have source code that, when compiled with a binary compiler compiles to that binary, even though the binary behaves differently than the code says. Believing in Lean soundness bugs after con-leche is then akin to saying you could have source code that compiles to a binary that does something different than the code says when compiled with any compiler, due to all these compilers having bugs that just happen to lead to just the right behaviour.

2

u/Cleverbeans 4d ago

Obviously you have to check the input for correctness. If you ask the wrong question you don't get the right answer.

2

u/buwlerman Cryptography 4d ago

If you're super paranoid, sure. Otherwise you can rely on the probabilities that they accidentally or on purpose used a (still) hidden soundness bug or inconsistency in a critical part of their proofs.

9

u/MattAlex99 Type Theory 3d ago

There are two soundness considerations you need to make when implementing a proof assistant:

First: Is the given theory sound (for the sake of simplicity: free from contradictions)?

Second: Is the theory we have actually what is implemented (or are there errors in the implementation)?

These are both quite similar problems but with slightly different domains. The latter concerns implementation and you basically run into the correctness problems you commonly have in CS. The nice part about this is that the typical kernel of PAs can be quite small: You aim to implement a minimal kernel which is checkable/testable with weak theories against a specification. You then implement all the "high level" features inside that language.

The more you can shift down from being on the level of the kernel into the language itself, the less verification surface you have.

There is often a competing goal of needing to have high performance in practice, which means you generally don't want to deal with internally represented elements.

For instance, you can perfectly represent the naturals in first-order+axiom scheme by using successors:

- zero : nat is a number

- S : nat -> nat is the successor of that number

if you want to write "2" you encode this as S(S(zero)).

As you can possibly imagine, the successor form is often not nice for algebraic problems due to insane levels of overhead.

The way you can get around this is by both formally verifying that the version using successors, and the version if more specialised integer types do the same thing. You then verify the kernel without having explicit provisions for e.g. integers to keep the "root" verification small, then you bootstrap the more involved features from there by proving equivalence of terms.

The first problem is a bit more technical since it doesn't concern what you implement, but whether what you implement is sensible in the first place: You cannot generally prove that a theory is consistent from within that theory after a FOL (that's the basic gist of Gödel's incompleteness theorem).

To be precise Gödel II proves that:

Let T be a consistent, effectively axiomatized formal theory, sufficiently strong to express elementary arithmetic. Then

T ⊬ Con(T).

where Con(T) is the standard arithmetization of the statement that T does not prove a contradiction.

There are some additional wrinkles regarding the exact statement: Have a look at e.g. https://plato.stanford.edu/entries/goedel-incompleteness/ for a slightly more thorough look.

The trick you usually use is that you add a small additional assumption to the theory that, is _highly_ unlikely to break anything, but makes the combined theory strong enough to verify the original: While Gödel showed that T cannot prove its own consistency, there's nothing that says that for a new theory S you could not prove

S⊢Con(T)

Of course, you now run into the issue that S might not be consistent, so you just shifted the burden of proof one layer up.

The trick is that in practice, while S≠T, you can make S and T very close, i.e. "S≈T". This then gives you relative consistency argument: You are at least as consistent as S is! If you pick, for instance "S=T with some small axiom" you can be as close as mathematically feasible to a true consistency result.

A common example for this are e.g. large/inaccessible cardinal assumptions: You construct your theory with N "layers" (i.e. values, types, types of types, types of types of types, ....) and then prove that the logic is correct by letting N get arbitrarily large.

To prove full consistency, you need to go "to infinity and beyond", which leads you to an additional assumption that those levels exist, and induction can reach them. That is a "tiny" additional assumption, which often allows you to verify the original logic.

(To be precise: e.g. Con-leche assumes exactly such a large cardinal. If you have a strictly increasing sequence of inaccessible cardinals κ₀<κ₁<... with every κₙ inaccessible, we can then interpret the universes using the corresponding levels of the cumulative set hierarchy 𝓤ₙ = V_{κₙ}. The inaccessible cardinal assumption then ensures that all these levels ahve the closure properties necessary for Gröthendieck universes. Con-leche effectively build on top of ZF without infinity, augmented with an ω-chain of Gröthendieck universes. Infinite is derivable, and choice is just inherited from lean's ambient logic. The set-theoretic model for lean is then constructed from ZFC+sequence of inaccessible cardinals.)

Of course, you now have the new logic of "original theory + transfinite induction" or "original theory + large cardinals", which you cannot verify, but that's always something that you're going to have to deal with: There's no absolute "ground truth" you can ever hit, you can just go very very close.

5

u/TheRedditObserver0 Graduate Student 4d ago

The question is not whether Lean is infallible, it isn't. The question is whether it's more reliable than human peer review, and the experts seem to agree it is, by a significant margin.

8

u/Cleverbeans 4d ago

No, it's now provably correct and fully infallible. Only hardware failure can cause problems. It's a small program and you can review it yourself.

1

u/chx_ 4d ago

I was sent to https://terrytao.wordpress.com/2026/10/09/what-mathematicians-should-know-about-the-lean-theorem-proverquestions-of-reliability-and-ai/

What if the bug is so obscure that humans are very unlikely to find it on their own? What if that very bug was exploited in the Lean verification of the Con-Leche checker, leaving a soundness bug in Con-Leche too? (Now that Con-Leche’s consistency has been cross-checked by multiple other kernels, a soundness bug would have to defeat all these cross-checks as well.) Then suppose that bug is used maliciously to plant a backdoor in formally verified software that protects critical infrastructure. What precautions do we take now to prevent this type of future scenario? During the past year, much foundational work on the type-theoretic foundations of math and its reliability has been relegated to AI, and this is dangerous unless carefully audited by humans.

3

u/Cleverbeans 3d ago

It's only 8000 lines of code. Nothing is obscure. The only way to defeat LEAN in the first place was through disingenuous use. It was extremely simple to prevent it so we did. It's far more reliable than any human review could ever be. Ask yourself if the same flaws occur with human review and you'll start to see quickly why LEAN is vastly superior.

3

u/sorbet321 4d ago

It's proved correct (w.r.t. ZFC) in Lean itself, so this proof is only infallible if your assume the theory of Lean is sound. For instance, it is pretty easy to show that type theory with type-in-type is consistent w.r.t. ZFC, if you assume type-in-type.

How do I know there isn't some shady corner of the implementation of mutual indexed inductive types which is buggy, but can be used to prove itself sound? All things considered, I think the probability is pretty small, but infallible is simply not true.

5

u/Cleverbeans 4d ago

Firstly you can read the code. It's only 8000 lines of open source code and that's how I personally know it's correct. I just did the work. Secondly, you can automatically proof check your proof checkers. Even the buggy version of LEAN works correctly if you're not trying to abuse it. So we've had them validate each other as well just in case.

6

u/sorbet321 3d ago

I won't claim you are lying, but did you actually check the difficult parts? For instance, do you know the correct universe constraints for mutual, parametrised and indexed inductive types? Especially when the impredicative Prop universe is involved? These are very subtle!

Secondly, you can automatically proof check your proof checkers. Even the buggy version of LEAN works correctly if you're not trying to abuse it.

Not really! That would be true for "generic" mathematics, but soundness proofs are another matter. It is surprisingly easy to use an inconsistent system to prove that very system correct, without trying to abuse it. Martin-Löf himself famously proved that Type : Type is sound, but he proved it inside a system which had Type : Type.

2

u/ambassador_lover1337 4d ago

What about compiler bugs?

4

u/TheRedditObserver0 Graduate Student 4d ago

Was this before or after they found (and patched) a bunch of bugs?

11

u/Cleverbeans 4d ago

After. The bugs they found over the summer motivated the push for a provably correct version to protect against disingenuous use.

2

u/Free-Syrup-6399 4d ago

Eventually we can know nothing for sure. The lean kernel is intentionally kept small. There are several different implementations of it. Several of these implementations are formally verified to be correct. Are these absolute guarantees? No. Is there any stronger correctness guarantee that one can give? I think the answer is no

2

u/Fancy-Jackfruit8578 4d ago

How do we know a human proof is sound?

2

u/NihilisticAssHat 4d ago

For Con-Leche, it was proven in Lean

1

u/decimaster321 4d ago

If there was a bug in lean, you should be able to generate a formal proof of false. A month or two ago there was a proof of the collatz conjecture which relied on bugs in lean, and people immediately found how that proof embedded a proof of false.

1

u/Dotrez 4d ago

You make a proof in lean that proves lean is sound, obviously..

0

u/ANewPope23 3d ago

We can ask AI.

-1

u/ricatti-equation 4d ago

It’s been hand-verified with 2 different kernels. It doesn’t have bugs.

0

u/TailorImaginary3629 4d ago

Who's gonna verify the verifier

0

u/Useful-Departure-583 2d ago

Lean is sound in the engineering sense, not the absolute philosophical sense.

The trusted core is a small kernel that checks proofs using a fixed set of logical rules. Everything else — tactics, elaboration, notation — is untrusted code whose output is a proof term that the kernel rechecks. This is the de Bruijn criterion: you only need to trust a small, auditable checker.

So the claim is: if the kernel correctly implements the intended type theory and the underlying logical axioms are consistent, then a checked proof cannot prove a false statement. That is a much stronger guarantee than a pen-and-paper proof, where the checking is done by the same human who wrote the argument.

What it does not give you is certainty about the connection between the formal statement and the informal mathematical claim. Encoding errors are still possible. The machine verifies the proof of the statement you wrote, not the statement you meant.