Chapter 1 · Foundations
Hello, Lean
Step 1 of 17 · Concept
Lean checks what you claim
Lean is a programming language and a proof checker. You write a claim, such as 2 + 2 = 4, and a proof of it.
Lean checks the proof: it accepts the claim, or it says why not.
In this lesson you will:
- check your first Lean file,
- state facts and have Lean prove them,
- read what is left to prove.
Step 2 of 17 · Demo
Run your first file
A Lean file holds lines Lean reads in order. This one asks for a value, then states a fact and proves it. Run needs a larger screen than a phone's; on a phone you can read every step.
Press Run. Line 1 prints a value. Line 2 prints nothing at all.
Step 3 of 17 · Concept
What Run did
Run hands the whole file to Lean, which checks it from the top.
| line | what Lean did |
|---|---|
#eval 2 + 3 | worked out the value and printed 5 |
theorem two_plus_two ... | checked the proof and accepted it |
A file Lean accepts prints only what #eval asks for.
Step 4 of 17 · Quiz
What does silence mean?
After Run, this line printed nothing. What does that mean?
theorem two_plus_two : 2 + 2 = 4 := by rfl
Step 5 of 17 · Concept
A theorem: a name, a statement, a proof
A theorem line has three parts: a name, a statement after :, and a proof after :=.
by starts a proof made of tactics: steps that tell Lean how to prove it. rfl is a tactic. It
proves an equation whose two sides compute to the same value.
A one-tactic proof may stay on one line. Otherwise by ends the line, then one tactic per line, each indented two
spaces:
theorem two_plus_two : 2 + 2 = 4 := by
rfl
Step 6 of 17 · Concept
example and comments
example is a theorem without a name. Use it for a fact that nothing will use again:
example : 3 + 3 = 6 := by rfl
-- starts a comment: Lean skips the rest of the line. The exercises give their tasks in comments, one theorem
per line: its name, a colon, its statement.
-- ten_plus_five : 10 + 5 = 15
theorem ten_plus_five : 10 + 5 = 15 := by rfl
Step 7 of 17 · Concept
The goal panel shows what is left
After Run, put the cursor on rfl. The goal panel shows what is left to prove, before the tactic and after it.
- Before
rfl:⊢ 2 + 2 = 4. Read⊢as prove. - After
rfl: No goals. The proof is complete.
Step 8 of 17 · Quiz
Read the goal
You run this file and put the cursor on rfl. What does the goal panel show before rfl?
theorem t : 3 * 4 = 12 := by rfl
Step 9 of 17 · Try it
Try it: a false claim
When this theorem runs, Lean refuses it, naming line 1. The message begins:
error: Tactic `rfl` failed: The left-hand side
2 + 2
is not definitionally equal to the right-hand side
5
Lesson 4 reads such messages.
Press Run and read the refusal. Then change the 5 so that the claim is true.
rfl failed because the two sides do not compute to the same value. 2 + 2 is 4: change the 5.
Step 10 of 17 · Concept
sorry is a promise, not a proof
sorry stands in for a proof you have not written. Lean accepts the file, with a warning:
theorem later : 3 + 3 = 6 := by sorry
warning: declaration uses `sorry`
The file is still accepted. But the theorem is not proved, so a block whose file says sorry does not pass.
Every exercise starts with a sorry where you work, the next one included.
Step 11 of 17 · Exercise
Your turn: three facts
Each comment gives a theorem's name and statement. The first theorem is started for you, with a sorry where its
proof goes.
- Replace
sorrywithrfl. - Write the other two theorems, one line each, with the names and statements the comments give.
Every theorem needs its name: the check looks for three_fours and seven_eights. A line with no name, such as theorem : 3 * 4 = 12, gives:
error: unexpected token ':'; expected identifier
Copy the first theorem's shape: theorem, the name, :, the statement, then := by rfl.
theorem three_fours : 3 * 4 = 12 := by rfl
Step 12 of 17 · Concept
decide answers yes-or-no questions
3 < 5 is not an equation, so rfl cannot prove it. decide is a tactic for a yes-or-no fact about given
numbers. It works out whether the fact is true; if it is, the proof is complete.
| statement | a tactic that proves it |
|---|---|
2 + 2 = 4, an equation | rfl or decide |
3 < 5, a comparison | decide |
#eval asks the same question for a value: #eval 3 < 5 shows true.
Step 13 of 17 · Demo
See decide work
decide proves 3 < 5, and #eval shows the comparison's value.
Press Run. Line 1 prints true. Line 2 is accepted, so it prints nothing.
Step 14 of 17 · Quiz
Which one passes?
Each option is a whole file. Which one passes the check?
Step 15 of 17 · Check yourself
Check yourself: three facts and a value
No shape this time.
Write and prove the three theorems the comments give. Then print 6 * 7 with #eval: Run shows 42.
One line per theorem: its name, its statement, its proof. Choose each tactic by the statement: an equation, or a comparison.
Step 16 of 17 · Challenge · optional
Challenge: not equal
Optional.
Write and prove the theorem the comment gives. ≠ means is not equal to; type it as \ne.
Is 2 + 2 ≠ 5 a yes-or-no fact about given numbers? Then decide can answer it.
Type \ne and a space: the editor turns it into ≠. In plain ASCII, Not (2 + 2 = 5) says the same.
The theorem's line ends with := by, and the tactic goes on the next line, indented.
Step 17 of 17 · Recap
Recap
- Run checks the file from the top; an accepted file prints only what
#evalasks for. theorem name : statement := by rfl:rflproves an equation whose sides compute alike.decideproves a yes-or-no fact about given numbers, such as3 < 5.- The goal panel shows what is left to prove, after
⊢. sorryleaves a proof unfinished: a warning, and the block does not pass.
Next: lesson 2 looks inside Lean: what reads your file, and what checks a proof.
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
#eval 2 + 3
theorem two_plus_two : 2 + 2 = 4 := by rfl
theorem two_and_two : 2 + 2 = 5 := by rfl
-- ten_plus_five : 10 + 5 = 15
-- three_fours : 3 * 4 = 12
-- seven_eights : 7 * 8 = 56
theorem ten_plus_five : 10 + 5 = 15 := by
sorry
#eval 3 < 5
theorem small : 3 < 5 := by decide
-- six_sevens : 6 * 7 = 42
-- twelve_thirty : 12 + 30 = 42
-- forty_less : 40 < 42
-- and print 6 * 7 with #eval
-- not_five : 2 + 2 ≠ 5