Why Mathematicians Let a Computer Check Their Proofs: Meet Lean

5 min read

The problem: who checks the checker?

A long math proof can run to hundreds of pages. A referee reads it, nods, and signs off. But referees are human, and humans skim, get tired and trust steps that look familiar. Every so often a published proof turns out to have a gap that nobody noticed for years.

Software has the same problem. Your code passes the tests you wrote, but tests only cover the cases you thought of. What you really want is a way to say "this is true for every input" and have something refuse to agree unless every single step holds.

That is the problem a proof assistant solves.

The idea: a program that only accepts valid steps

A proof assistant is a program where you write a mathematical statement and a proof of it in a precise language. The program then checks the proof, step by step, and either accepts it or tells you exactly which step does not follow.

It does not guess and it does not "mostly agree". If one step is missing, the whole proof is rejected. If the program accepts it, every step has been verified by a machine that never gets tired.

Lean is one of these proof assistants. It is also a full programming language, so the same tool lets you write ordinary programs and prove things about them. This course uses Lean 4, the current version.

Here is what it looks like

Here is about the smallest proof you can write in Lean. It claims that 2 + 2 equals 4 and gives a proof:

lean
theorem two_plus_two : 2 + 2 = 4 := rfl

#check two_plus_two

The word theorem starts a named claim. After the colon comes the statement, 2 + 2 = 4. After := comes the proof, here just rfl, which roughly means "both sides compute to the same thing". The #check line asks Lean to show what two_plus_two is, and Lean answers:

lean
-- two_plus_two : 2 + 2 = 4

Now watch what happens when the claim is false:

lean
theorem two_plus_two_wrong : 2 + 2 = 5 := rfl

Lean refuses. Part of its message reads:

lean
-- error: Not a definitional equality: the left-hand side
--   2 + 2
-- is not definitionally equal to the right-hand side
--   5

That red error is the whole point. You cannot sneak a false statement past it, no matter how confident you sound.

The trust story: a small kernel

You might ask: why should I trust Lean itself? Lean is a big program, and big programs have bugs.

The answer is a design choice. Lean has a lot of helpful machinery for writing proofs: automation, notation, search tools. But all of that machinery produces a low level proof term in the end, and that term is handed to a small core called the kernel. The kernel is the only part that decides whether a proof is accepted.

Because the kernel is small, it is far easier to inspect and trust than the whole system. The fancy parts can have bugs, and the worst they can do is fail to produce a proof. They cannot make the kernel accept something false. So when you trust a Lean proof, you are mostly trusting that small core plus the exact statement you wrote down.

That last part matters. Lean checks that your proof proves your statement. It cannot check that your statement says what you meant. Reading statements carefully is a skill this course comes back to later.

Animation: Tactics and automation only build a proof term. The small kernel decides. Whether the statement means what you wanted is still your job.

Why mathematicians and AI labs care

Mathematicians use Lean to make sure long or subtle arguments have no hidden holes. A whole community maintains Mathlib, a large shared library of formalized mathematics covering algebra, analysis, topology and much more. When a result is in Mathlib, anyone can build on it knowing it has been machine checked.

AI researchers care for a related reason. A model that writes proofs can be wrong in convincing ways. If the model writes its proof in Lean, the checker gives a clear yes or no answer that does not depend on anyone reading the output. That makes Lean a useful judge for training and testing systems that do math.

In my opinion, this is also the best reason for a programmer to learn Lean: it changes "I think this is right" into "the machine agrees this is right".

What this course covers

The plan is simple. First you install Lean and learn to talk to it with #eval and #check. Then you learn types, functions and data, because Lean proofs are built from the same pieces as Lean programs. After that comes the key idea, that a proof is just a value of a special type, and then tactics, induction and Mathlib.

You do not need any math background beyond school algebra. If you can write a small function in Python or JavaScript, you can follow along.

Try this

Open https://live.lean-lang.org in your browser, delete what is there, and paste:

lean
theorem my_first : 3 + 4 = 7 := rfl

No error means Lean accepted your proof. Now change 7 to 8 and watch the error appear. Change it back and it disappears.

Sources