Basic Lean proofs

The most basic Lean proofs, simplest first. Each one compiles as written.

1. Arithmetic: Lean computes both sides

theorem two_plus_two : 2 + 2 = 4 := rfl

2. Something true: trivial proves True

theorem it_is_true : True := trivial

3. Using a fact you're given: if you have p, you have p

theorem same (p : Prop) (hp : p) : p := hp

4. "If p then q", and p, gives q

theorem modus_ponens (p q : Prop) (hp : p) (hpq : p → q) : q := hpq hp

5. Socrates

theorem socrates_mortal (Person : Type) (Human Mortal : Person → Prop)
    (socrates : Person)
    (all_humans_mortal : ∀ x, Human x → Mortal x)
    (socrates_human : Human socrates) :
    Mortal socrates :=
  all_humans_mortal socrates socrates_human

6. A wrong claim, which Lean refuses

theorem wrong : 2 + 2 = 5 := rfl   -- error: Lean rejects this

The pattern is the same every time: name, then claim, then :=, then the proof. Lean checks that the proof really proves the claim.

What rfl is doing

2, 2 and 4 are number literals, and with no type given Lean treats them as natural numbers (Nat).

rfl makes Lean evaluate both sides. It computes 2 + 2, gets 4, sees that the right side is also 4, and accepts the proof. rfl itself only says "a thing equals itself"; the work is Lean reducing both sides to the same value first. That's why 2 + 2 = 5 fails: the left side computes to 4, and 4 isn't 5.

Lean's checker has built-in fast arithmetic for number literals, so this stays quick even with huge numbers:

theorem big : 123456789 * 987654321 = 121932631112635269 := rfl

Under the hood a natural number is defined as zero or "one more than" another number. If Lean really counted up one step at a time, large numbers would be far too slow, so the fast arithmetic is a shortcut it is trusted to take.