A Proof Is Just a Value: Propositions as Types in Lean
The problem: where do proofs live?
So far Lean has looked like a programming language with a strict type checker. You wrote functions, defined data and evaluated expressions. But this course promises proofs a computer can check, and it is fair to ask: where do proofs fit into a language of types and terms?
The answer is the most important idea in Lean, and it is surprisingly simple. A statement is a type. A proof of the statement is a term of that type. Checking a proof is just type checking. You already know how that works.
Statements are types in Prop
Ask Lean for the type of an equation:
lean#check 2 + 2 = 4 #check 2 + 2 = 5 #check Prop #check (fun (n : Nat) => n + n = 2 * n)
Lean prints:
lean-- 2 + 2 = 4 : Prop -- 2 + 2 = 5 : Prop -- Prop : Type -- fun n => n + n = 2 * n : Nat → Prop
Prop is the type of propositions, meaning statements that could be true or false. Notice that 2 + 2 = 5 is a perfectly good Prop. Writing a statement down does not claim it is true. It only says "here is something one might try to prove".
The last line is a function that takes a number and returns a statement about it. Its type is Nat → Prop. Functions like this describe properties, such as "n is even", and they show up everywhere in real theorems.
A proof is a term of that type
If 2 + 2 = 4 is a type, then a proof of it is a value of that type. Lean gives you rfl as a basic way to build proofs of equations where both sides compute to the same thing:
leantheorem four : 2 + 2 = 4 := rfl example : 3 * 3 = 9 := rfl def answer : Nat := 42 #check four #check answer theorem alwaysTrue : True := True.intro
Lean prints:
lean-- four : 2 + 2 = 4 -- answer : Nat
Compare the two lines. answer : Nat says answer is a value of type Nat. four : 2 + 2 = 4 says four is a value of type 2 + 2 = 4, which is exactly what it means to be a proof of that statement. Same syntax, same checker.
The last line proves True, the statement that is trivially true. Its only proof is True.intro. A proposition with no possible proof, like 2 + 2 = 5, is simply a type with no values.
Animation: Both statements are Props. Only one has a term, rfl, and sorry is a placeholder, not a proof.
theorem, def and example
You have now seen three keywords.
def defines a value you want to compute with, like a number or a function. theorem does the same job for proofs: it names a term whose type is a Prop. Lean treats theorems a little differently because it does not care which proof you used, only that one exists, so you would never #eval a theorem. Using theorem also tells readers "this is a fact", not data.
example is a theorem with no name. It is handy for checking something once without cluttering the file. In the snippet above, example : 3 * 3 = 9 := rfl asks Lean to confirm the statement, and since there is no error, it did.
Proofs can be inputs too
Because proofs are terms, functions can take them as arguments. Here is a theorem that says "if p is true, then p is true":
leantheorem keep (p : Prop) (hp : p) : p := hp #check keep #check keep (2 + 2 = 4) rfl
Lean prints:
lean-- keep (p : Prop) (hp : p) : p -- keep (2 + 2 = 4) rfl : 2 + 2 = 4
keep takes a proposition p and a proof hp of p, and returns that proof. The second line uses it: give it the statement 2 + 2 = 4 and the proof rfl, and you get back a proof of 2 + 2 = 4. By convention, names like hp mean "hypothesis about p". This idea, that using a fact is just passing a proof to a function, drives the next lesson.
A wrong proof is a type error
Try to prove a statement with something that is not a proof:
leanexample : 2 + 2 = 4 := "four"
Lean rejects it with:
lean-- error: Type mismatch -- "four" -- has type -- String -- of sort `Type` but is expected to have type -- 2 + 2 = 4 -- of sort `Prop`
This is the same error pattern from lesson three: what you gave, its type, what was expected. Lean does not need a separate "proof checker" on top of the type checker. They are the same thing.
sorry: a placeholder, not a proof
Sometimes you want to state a theorem now and prove it later. Lean lets you write sorry in place of a proof. This snippet shows it on purpose:
leantheorem later : 10 * 10 = 100 := sorry
Lean accepts the file but warns:
lean-- warning: declaration uses `sorry`
sorry tells Lean "trust me for now". It is useful while you work, but a file containing sorry proves nothing. Always treat that warning as unfinished work. A later lesson shows how to check whether a theorem secretly depends on sorry.
Try this
Paste into https://live.lean-lang.org:
leanexample : 5 * 5 = 25 := rfl theorem ten : 6 + 4 = 10 := rfl #check ten
Then change the example to claim 5 * 5 = 24 and read the error. Finally replace rfl with sorry and look for the warning.
Sources
Write proofs as plain terms
A proof of a proposition is a term of that type. Fill in each sorry with a term, no tactics: use the hypotheses you are given and the constructor `And.intro`.
example (p : Prop) (hp : p) : p := sorry example (p q : Prop) (hp : p) (hq : q) : p ∧ q := sorry example (p q : Prop) (hpq : p → q) (hp : p) : q := sorry
The official Lean editor runs in your browser and checks the file on Lean's server. Replace every sorry until no errors or warnings are left, then mark it solved.
The worked solution unlocks after your first attempt.
Check what you learned
Answer all 3 questions. You need at least 2 right to pass.
1. What is the type of 2 + 2 = 5?
2. What does example do?
3. What does a sorry in a file mean?