r/3Blue1Brown • • 7d ago

What does a compiled Lean proof actually certify?

What do you think about that?

63 Upvotes

15 comments sorted by

38

u/SufficientGreek 7d ago

6

u/DifficultAvocado6185 7d ago

Thanks 🫶 Kevin Buzzard's post is a great read on exactly this, I went through it tonight. Good to see the statement fidelity question taken seriously by someone at that level.

24

u/zolk333 7d ago

I'll try not to muddy the waters too much, but a compiled lean proof doesn't really certify anything. The fact that it compiled (rather, type-checked) is the certificate.

For example: let's say you want to prove that there are infinitely many prime numbers, i.e. that for any natural number there is a prime number that's larger than it. This statement can be translated into a Lean type. Your proof then would be a program that can take in any number, and return a prime that's larger. Lean checks that the program actually satisfies the type: it really can work for any input number, the result really is larger than the input, it really is a prime, etc. If the checks fail, the program doesn't compile.

So in the end, your problem statement is turned into a type, your proof into a program, the compiler makes sure the program satisfies the type, and returns a compiled program. But this compiled program, by itself, is nothing more than a box that takes in a number and returns another one. The compilation process is what made sure the compiled program is correct.

(Actually, many proving techniques don't really compile into anything tangible, but that's another rabbit hole)

3

u/DifficultAvocado6185 7d ago

Nice explanation and the rabbit hole at the end is actually relevant here. In Lean proofs of propositions are erased at compile time, and once classical axioms like Classical.choice are involved the , (RH proof uses it, per Sutherland's check) theres no algorithm to extract at all. So the type-check really is the whole certificate. Which brings it back to the statement the certificate tells you the proof inhabits the type but someone still has to read the type and confirm it says what we mean. Thanks for laying it out so clearly ✨️

1

u/JoJoModding 6d ago

Proofs of propositions are not really erased at compile time, that's misinfo.

Lean has the feature where you can write some code in Lean (say a tic tac toe game) and then prove something about it (like that the game only says "you won" when they have 3 in a row). When you then run the game, the proof is indeed ignored since it is not needed for running the game.

But when compiling the proof of a mathematical theorem, you are not running any program. The compiler output is the entire proof, just in a very low-level format. And all axioms (that were actually used) will show up in this and can easily be found.

3

u/cloudsandclouds 6d ago

No, they really are erased at compile time! The issue is that “compile” in lean specifically means compile in the sense of “turn into executable code” which is a separate thing in Lean from “turn source files into type theory terms” (i.e. elaboration).

So it’s not like the executable tic tac toe game has the proof and ignores it, the pipeline that turns type theory terms into executable or interpretable code has indeed erased it (and erased types as well). This resulting executable code is just completely unrelated to the type theory terms being typechecked by the kernel (except for coming from the type theory terms as a starting point).

1

u/JoJoModding 6d ago

What I was trying to say is that the Olean files contain the whole proof and erase nothing. I think we agree on that

12

u/cloudsandclouds 6d ago

This is true for some typecheckers but…not really for Lean, as it happens 😅

In particular, proofs in lean are not actually programs you can run. They may use the law of excluded middle and be noncomputable. They are data called Lean expressions.

You can actually export this data into an *.ndjson file, if you like. So the proof is not a box, it is some particular term of Lean’s type theory, built out of expression constructors (function application, lambda/foralls, let’s, named constants, and a handful of other simple ways to make terms).

Then this proof can be typechecked by various Lean kernels. But they do not actually compile this into a program or anything. They just return a verdict as to whether all the types and expression structures and name references worked out!

2

u/zolk333 6d ago

I have to be honest, I'm not this versed in Lean, but from their own documentation it seems like what you (plural) call compilation is what Lean internally calls elaboration. I'm guessing the community adopted the term for proof-only applications?

3

u/cloudsandclouds 6d ago

Correct that elaboration is exactly the relevant term here! :) (Notice that I carefully avoided using “compile” to refer to anything besides creating executable code! 😁)

But, note that elaboration is not actually about proof-only applications. Elaboration is the process of turning anything, including both theorems and definitions intended to be used as programs and anything else, from source syntax to terms of the type theory. These type theory expressions then are sent down two different pipelines independently: you can send them to the kernel to check they’re type correct (this applies equally to both proofs and non-proofs), and independently you can send them to the compiler to become a different sort of thing called LCNF (Lean Compiler Normal Form, but not “of expressions”; it’s just a different sort of thing), and then after that, technically a step to Lean IR.

This is what a computer can execute or lean can interpret, and types and proofs are in fact completely erased by this point, because there is no way to run them. You can then either keep going and turn Lean IR into C code, then native machine code; or you can just read it and execute the instructions line by line, i.e. interpret it.

Anyway, you can see that in the compiler branch, we have eventually completely left the world of type theory 🙃 (except I suppose for the fact that lean’s compiler is implemented in lean itself, and that proofs and types are kept around just long enough in the pipeline to let the compiler do optimizations before being obliterated…) And in lean, it’s specifically turning expressions into executable code that gets the name “compilation.”

3

u/JoJoModding 6d ago

But note that it would be enough to distribute the compiled output to say that you have proven it. And checking if the compiled output is a correct proof is often a lot faster than checking the Lean source code, since the compiled output is in a format that makes the checking a lot easier.

3

u/Upbeat_Assist2680 6d ago

People use the phrase, compiled a bit informally and just assume that the type checker is run as part of the compilation process. I use the analogy myself when I'm describing what the type checker does and how it can certify approve. 

It's not a bad one, and rarely are you going to be in a situation with the distinction is going to matter.

0

u/LeonhardEulerZ2 2d ago

I am no expert, but I agree that a compiled Lean proof doesn't certify anything. For me the RH is still unproven. Better check the human "proof" 840 pages long, but I wouldn't try even if I were a professional mathematician with 50 year wxperience.

7

u/Tejasisamazing 5d ago edited 5d ago

If you are wondering why or how type checking to validate proofs actually works, here is a nice way to look at it

In other programming languages, like java, if you have a function which is supposed to return something of type int, then the program would not even compile if you tried to return some object of type bool, or string, or tried to return nothing. Before even running the program, the type checker can check if you are outputting a correct object of the type int or not.

Now, suppose we are able to create a situation, where the types themselves (like int, bool) represents statements like 2+2=4, and objects of that type are proofs of that statement, then

if you have a function of type (2+2=4), your program wouldn't be able to compile until you can return an object of the type 2+2=4, ie, a proof of 2+2=4. Which means you'll have to make the proof object somehow, ie prove the statement.

So you can atleast see that if we are able to create such a situation, then the type checker can actually validate your proof. If you have written an incorrect proof, then it wouldn't match the type, and fail at type checking.

Lets see more concretely how this works We start with certain axioms, ie statement which we say are true without proof. What this corresponds to in lean, is the axiom keyword, where you specify a type (like the statement 2+1=3), and it automatically gives you an object/proof of that type, without having to prove it. Thats what we mean by axioms.

Then, using these proven statements, we can then prove other statements, ie, give object of type of other statements, which can then further be used to give objects of other types, and so on

Here is an example suppose we start with axioms: 2=1+1, 3=2+1, 4=3+1 this means, we now have objects of each of these types. Ill label them st1, st2, st3 (standing for statement 1, 2 and 3). Assume that the = and + symbols have been defined seperately.

And I wish to prove 2+2=4, so my type is (2+2=4), and i wish to produce an object of this type

I denote it like theorem twoplustwoeq4: (2+2=4) := .....

the (2+2=4) denotes the type, and in the ....., ill write a proof/create an object of type 2+2=4, and return it.

You can think of theorem as a function, which takes in nothing, and returns that object of type 2+2=4.

so i start with the object of 4=3+1, and i apply st2 to simplify it, and i get an object of type 4=2+1+1. Then I notice the 1+1, and i tell lean to apply st1 on the section (1+1), and i get an object of type 4=2+2. (These assumed associativity, so the definition of + itself must have a theorem that associativity works)

almost proven, but the object of 4=2+2 is not object of 2+2=4, they are different things!

Luckily, the way = is defined is that it is a symmetric relation, ie, the type = gives us a function Sym, which, given a object of type a=b, returns an object of type b=a. (so notation: theorem Sym (h:a=b) : (b=a) := .....

the (h:a=b) indicates taking in input an object of type a=b, and storing it in the variable h to be used in the proof)

So we apply Sym on object of 4=2+2, and get an object of 2+2=4, and we can return it! This program will pass type checking, and hence its a valid proof.

Notice how at any point of time, you didn't have any other way of returning an object of type 2+2=4, aside from actually constructing it, or defining it as an axiom

Thats how lean can actually truly verify proofs.

2

u/_maple_panda 5d ago

This finally made it clear for me, appreciate the explanation and example