First Proof
After finishing this section, you will have written a theorem that Lean accepts, and you will understand every word of it.
Open the Test.lean from the previous section, or the online version live.lean-lang.org, and type in the code below piece by piece.
What you will be able to do
- Distinguish between "having Lean compute" and "having Lean prove"
- Write the
theorem 名字 : 命题 := by 证明structure - Understand what Lean says when it rejects a false statement
Step 1: Have Lean compute
#eval 2 + 2You should see:
4#eval means "evaluate this expression." It runs a program and gives you the result.
- I saw 4
Step 2: Have Lean prove
Write "2 + 2 equals 4" as a theorem:
theorem two_plus_two : 2 + 2 = 4 := by
rflYou should see: no errors, and no output at all. In Lean, a passing proof is silent.
Read it word by word:
| What it says | What it means |
|---|---|
theorem | what follows is a theorem |
two_plus_two | its name, you choose it |
: 2 + 2 = 4 | after the colon comes the proposition, i.e. the statement you claim holds |
:= by | the proof follows, built step by step with "tactics" |
rfl | one tactic: evaluate both sides; the results are exactly the same, so they are equal |
Confirm that Lean has remembered it:
#check two_plus_twoYou should see:
two_plus_two : 2 + 2 = 4#check asks "what is this thing?" The answer: two_plus_two is a proof of the proposition 2 + 2 = 4.
- My
two_plus_twohas no errors, and#checkshows its proposition
Step 3: Try to prove something false
Change 4 to 5 and see what Lean says:
theorem two_plus_two_wrong : 2 + 2 = 5 := by
rflYou should see: a red squiggly line appears under rfl, and the error message contains this sentence:
is not definitionally equal to the right-hand sideIt means: what the left side evaluates to is not the same as the right side. Lean is not convinced, this "theorem" does not hold, and it cannot be used afterwards.
A different tactic makes no difference. decide directly evaluates the proposition to true or false:
example : 2 + 2 = 5 := by
decideTactic `decide` proved that the proposition
2 + 2 = 5
is falseHere example works just like theorem, except it has no name, which is convenient for quick experiments.
- I saw Lean reject
2 + 2 = 5with my own eyes
Quiz timeWhat's the difference between `#eval 2 + 2` and `theorem two_plus_two : 2 + 2 = 4 := by rfl`?
Checkpoint
- I can write the
theorem 名字 : 命题 := bystructure - I know that Lean outputs nothing when a proof passes
- I know that
rflproves "both sides evaluate to the same thing" - I know that
exampleis the unnamed version oftheorem
Didn't work?
Nothing happens after typing, and there is no red line. Check whether the file name ends in .lean, and whether the toolchain is still installing in the background.
theorem line reports "already declared." Names cannot be repeated within the same file. Use a different name, or use example.
Next Section
2 + 2 = 4 can be proved just by computing. Most propositions cannot, for example "if p holds then q follows; p now holds; therefore q holds." Next section begins the study of real reasoning.