Module 3: Propositions as Types·Lesson 9 of 16·4 min read

For All, There Exists, and Equals: Lean's Three Workhorse Statements

Here is a problem. You want to tell Lean "adding zero to any number gives that number back." Not just for 5, or for 100, but for every natural number at once. You also want to say things like "some number doubled is 10" without listing every number. And underneath both, you need a way to say two things are equal.

Those three ideas are "for all", "there exists", and equality. This lesson shows how each one becomes something you can build and check in Lean.

For all is a function

In the last lessons you saw that a proof of p → q is a function: give it a proof of p and it hands back a proof of q. A "for all" statement works the same way. A proof of ∀ n : Nat, n + 0 = n is a function that takes any number n and returns a proof that n + 0 = n.

The difference is that the type of the result depends on the input. Feed it 5 and you get a proof of 5 + 0 = 5. Feed it 9 and you get a proof of 9 + 0 = 9. That is why Theorem Proving in Lean calls these dependent functions.

lean
-- A "for all" statement: a function that takes any n and returns a proof
theorem add_zero_all : ∀ n : Nat, n + 0 = n :=
  fun n => Nat.add_zero n

#check add_zero_all
#check add_zero_all 5

The second #check shows add_zero_all 5 : 5 + 0 = 5. Applying the proof to a number is how you use a "for all" fact. Notice that Lean prints the theorem as add_zero_all (n : Nat) : n + 0 = n. Writing (n : Nat) before the colon and writing ∀ n : Nat, after it mean the same thing.

Using a for all proof

Since the proof is a function, you use it by calling it. Here is a fact from the core library, wrapped as our own theorem and then applied to 41.

lean
-- Using a ∀ proof: apply it to a value
theorem le_succ_all : ∀ n : Nat, n ≤ n + 1 :=
  fun n => Nat.le_succ n

example : 41 ≤ 42 := le_succ_all 41

-- Using an ∃ proof: take it apart with match
example (h : ∃ n : Nat, n + n = 10) : ∃ m : Nat, m + m + 2 = 12 :=
  match h with
  | ⟨n, hn⟩ => ⟨n, by rw [hn]⟩

The second example in that block uses "there exists", which is next. The rw inside it is a tactic covered properly in lesson 11. For now, read it as "replace n + n with 10".

There exists needs a witness

To prove "there is a number with some property", you must actually name one. That number is called the witness. Then you also give a proof that the witness has the property.

In Lean you package these two things with angle brackets: ⟨witness, proof⟩. You type the brackets as \< and \> in most editors.

lean
-- "There exists" needs a witness and a proof about that witness
theorem exists_double : ∃ n : Nat, n + n = 10 :=
  ⟨5, rfl⟩

example : ∃ x : Nat, x > 3 ∧ x < 5 :=
  ⟨4, by decide, by decide⟩

In the first one the witness is 5 and the proof is rfl, because 5 + 5 computes to 10. In the second, the property is an "and", so after the witness come two proofs, one for each half. The anonymous constructor flattens them, so ⟨4, p1, p2⟩ is accepted.

Going the other way, when you are given an ∃ fact, you can open it up with match as in the earlier block. You get the hidden witness n and the proof hn about it, and you can use both. What you cannot do is ask which number it was in a way that computes, because the proof only promises that one exists.

Animation: A for all proof turns any n into a proof about n. An exists proof packs a witness, 5, with a proof about it.

Equality, rfl and decide

Equality in Lean is a type too. For any two values a and b of the same type, a = b is a proposition, which Lean calls Eq a b. The simplest proof of equality is rfl, short for reflexivity. It says "these two sides are the same thing".

The key detail is that rfl works when both sides compute to the same value. Lean is allowed to run definitions to check this. For small, concrete facts there is also the decide tactic. It works on statements that have a yes or no checking procedure, like comparing two small numbers, and it runs that procedure.

lean
example : 2 + 3 = 5 := rfl
example : "Lean" ++ "4" = "Lean4" := by decide
example : [1, 2] ++ [3] = [1, 2, 3] := rfl
example : 7 * 6 = 42 := by decide
example : 10 < 100 := by decide
-- Eq is symmetric and transitive
example (a b c : Nat) (h1 : a = b) (h2 : b = c) : a = c :=
  Eq.trans h1 h2
example (a b : Nat) (h : a = b) : b = a :=
  Eq.symm h

The last two examples show that you can also build new equalities from old ones. Eq.symm flips an equation around. Eq.trans chains a = b and b = c into a = c.

When rfl says no

If the two sides do not compute to the same value, rfl fails. This is Lean doing its job.

lean
-- rfl only works when both sides compute to the same value
example : 2 + 2 = 5 := rfl

Lean reports a type mismatch: rfl has a type of the form ?m = ?m, both sides equal, but the goal is 2 + 2 = 5. The same message appears for true statements that need real reasoning, such as 0 + n = n for an unknown n. Lesson 12 explains why that one needs induction.

Try this

Open live.lean-lang.org and prove these two lines. For the first, pick the right witness. For the second, write a function that uses the library lemma Nat.zero_mul.

lean
example : ∃ n : Nat, n * n = 49 := ⟨7, rfl⟩
example : ∀ n : Nat, 0 * n = 0 := fun n => Nat.zero_mul n

Then change 49 to 50 and see which error you get, and why no witness can fix it.

Sources

Try it yourselfMedium

Use a for-all fact and give a witness

The first statement says there is a number whose double is 14: give the witness. The second uses a for-all hypothesis on a specific number. The third is about every number, so start by introducing it.

example : ∃ n : Nat, 2 * n = 14 := by
  sorry

example (f : Nat → Nat) (h : ∀ n, f n = n + 1) : f 4 = 5 := by
  sorry

example : ∀ n : Nat, n + 1 > n := by
  sorry
Open in the Lean web editor

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.

Saved in this browser. Pass the quiz while signed in to count it for the certificate.

Check what you learned

Answer all 3 questions. You need at least 2 right to pass.

1. In Lean, what is a proof of ∀ n : Nat, n + 0 = n?

2. How do you prove ∃ n : Nat, n + n = 10 directly?

3. Why does example : 2 + 2 = 5 := rfl fail?