Skip to content

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 ​

lean
#eval 2 + 2

You should see:

text
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:

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

You should see: no errors, and no output at all. In Lean, a passing proof is silent.

Read it word by word:

What it saysWhat it means
theoremwhat follows is a theorem
two_plus_twoits name, you choose it
: 2 + 2 = 4after the colon comes the proposition, i.e. the statement you claim holds
:= bythe proof follows, built step by step with "tactics"
rflone tactic: evaluate both sides; the results are exactly the same, so they are equal

Confirm that Lean has remembered it:

lean
#check two_plus_two

You should see:

text
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_two has no errors, and #check shows its proposition

Step 3: Try to prove something false ​

Change 4 to 5 and see what Lean says:

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

You should see: a red squiggly line appears under rfl, and the error message contains this sentence:

text
is not definitionally equal to the right-hand side

It 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:

lean
example : 2 + 2 = 5 := by
  decide
text
Tactic `decide` proved that the proposition
2 + 2 = 5
is false

Here example works just like theorem, except it has no name, which is convenient for quick experiments.

  • I saw Lean reject 2 + 2 = 5 with 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 名字 : 命题 := by structure
  • I know that Lean outputs nothing when a proof passes
  • I know that rfl proves "both sides evaluate to the same thing"
  • I know that example is the unnamed version of theorem

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.

gaodong.aiAboutPrivacyContact

Independent tutorials, not affiliated with any product mentioned. Product names and trademarks belong to their owners. Check official pages for prices and policies.