Module 5: Reading Real Lean·Lesson 14 of 16·5 min read

Finding the Lemma You Need in Mathlib: Names, exact? and Loogle

Here is a problem. You are in the middle of a proof and you need the fact that a * b = b * a for real numbers. You know it is true. Someone has surely proved it already. But what is it called, and where is it? Guessing names at random wastes hours, and proving it yourself again wastes more.

This lesson is about that problem. It covers what Mathlib is, how its lemma names are built so you can often guess them, and the search tools that find a lemma when guessing fails.

What Mathlib is

Lean itself ships with a core library: natural numbers, lists, strings, and the tactics you have used so far. Mathlib is a separate, much larger library built by a community of contributors. It covers algebra, analysis, number theory, topology, combinatorics and more, along with many extra tactics.

You use it by adding it to a project and writing import Mathlib at the top of a file. The web editor at live.lean-lang.org has Mathlib available, so you can try it without installing anything. In Mathlib files you will also see ℕ for natural numbers and ℝ for real numbers.

General lemmas, not one per type

The first surprise is that add_comm is not about natural numbers. It is about any type whose addition is commutative. The same lemma works for ℕ, ℤ, ℝ and many others.

lean
import Mathlib

-- General lemmas work for any type with the right structure
example (a b : ℕ) : a + b = b + a := add_comm a b
example (x y : ℝ) : x * y = y * x := mul_comm x y

#check @add_comm
#check @Nat.succ_le_iff
#check @Nat.Prime.two_le

-- Dot notation: hp has type Nat.Prime p, so hp.two_le means Nat.Prime.two_le hp
example (p : ℕ) (hp : p.Prime) : 2 ≤ p := hp.two_le

The statement of add_comm is {G : Type u_1} [AddCommMagma G] (a b : G) : a + b = b + a. The part in square brackets says "this works for any G that has a commutative addition". Lesson 15 explains how to read those brackets.

Names describe the statement

Mathlib names follow written conventions, so a name usually tells you what the theorem says. Theorem names use snake_case. Types and structures use UpperCamelCase. Symbols are turned into words: + is add, * is mul, ≤ is le, < is lt, 0 is zero, ∣ is dvd.

Some examples, read out loud:

  • add_comm: addition is commutative.
  • mul_zero: something times zero, which is zero.
  • Nat.succ_le_iff: about natural numbers, "successor is less or equal" is the same as something else. Its statement is m.succ ≤ n ↔ m < n.
  • Nat.Prime.two_le: about a prime natural number, two is less or equal to it.

The word of separates hypotheses from the conclusion. A lemma named like x_of_y usually means "if y, then x". The conclusion comes first in the name. The word iff means the statement is an ↔.

Once you know this, you can often guess. Want "a plus zero equals a"? Try add_zero. Want "multiplication is associative"? Try mul_assoc. Type the guess in #check and see if Lean knows it.

exact? and apply?: ask Lean

When guessing fails, let Lean search. The tactic exact? looks through every lemma available in the current file and tries to find one that closes the goal. It exists in core Lean, so it works with or without Mathlib.

lean
-- exact? searches the library for a lemma that closes the goal
example (a b : Nat) : a + b = b + a := by
  exact?

example (n : Nat) (h : 0 < n) : 1 ≤ n := by
  exact?

Lean answers with a suggestion. For the first goal it prints Try this: exact Nat.add_comm a b. For the second it prints Try this: exact Nat.one_le_of_lt h. In an editor you can click the suggestion to replace exact? with it. Do that, because exact? reruns the whole search every time the file is checked.

apply? is the looser version. It looks for lemmas whose conclusion matches your goal, even if they leave new goals to prove.

lean
-- apply? suggests lemmas whose conclusion matches the goal
example (a b c : Nat) (h : a ≤ b) : a + c ≤ b + c := by
  apply?

Here it suggests exact Nat.add_le_add_right h c, which closes the goal completely. For harder goals, apply? can also suggest a lemma that only gets you part of the way, leaving new goals for you to prove.

These searches can be slow with all of Mathlib loaded, and they can only find lemmas that are imported. If they fail, your goal may need to be broken into smaller steps first.

lean
import Mathlib

example (x : ℝ) (h : 0 ≤ x) : Real.sqrt x * Real.sqrt x = x := by
  exact?

Animation: Lemma names spell out their statements, and exact? suggests Nat.add_comm a b for a + b = b + a.

The Mathlib docs search

The Mathlib documentation site lists every declaration with its full statement, the file it lives in, and a link to the source. The search box at the top searches by name. It is the place to go when you half remember a name, or when you want to see every lemma in a namespace like Nat.Prime.

Reading the docs page for a lemma also shows its exact hypotheses. That matters, because two lemmas with similar names can need different assumptions.

Loogle: search by shape

Sometimes you do not know any part of the name, only what the statement looks like. Loogle searches Mathlib by the shape of a statement. You can search by:

  • a name or part of one, such as Nat.succ_le_iff;
  • a list of constants that must appear, such as List.length, List.reverse, which finds List.length_reverse;
  • a pattern with placeholders written ?a, ?b, such as (?a * ?b) * ?c = ?a * (?b * ?c), which finds associativity lemmas like Nat.mul_assoc.

The placeholder form is the powerful one. You write the statement you want with unknowns, and Loogle finds lemmas that match it.

A search routine

When you need a fact, a simple order works well. First, guess the name from the conventions and #check it. Second, try exact? in place. Third, search Loogle by shape or the docs by name. Only after all three fail should you prove the fact yourself.

Try this

On live.lean-lang.org, with import Mathlib at the top, guess the name of the lemma that says a * 1 = a and check it.

lean
import Mathlib

#check mul_one
example (x : ℝ) : x * 1 = x := mul_one x

Then search Loogle for ?a * 1 = ?a and compare what it finds with your guess.

Sources

Try it yourselfEasy

Find the library lemma instead of proving it again

Each goal is already a lemma in Lean's library. Close each one with `exact` and the right lemma name. If you are not sure of a name, write `exact?` and Lean will search for you; then replace it with the name it suggests. These lemmas are in core Lean, so no Mathlib import is needed.

example (a b : Nat) : a + b = b + a := by
  sorry

example (a b c : Nat) : a * (b * c) = a * b * c := by
  sorry

example (xs ys : List Nat) : (xs ++ ys).length = xs.length + ys.length := 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. By Mathlib naming conventions, what does the name mul_zero describe?

2. What does exact? do?

3. What is Loogle especially good for?