-- Lecture Notes for Nov 3, 2026 import Mathlib set_option linter.style.header false set_option linter.unusedVariables false -- ---------------------------------------------------- -- * Finding things in Mathlib -- Mathlib has about 200,000 theorems. Nobody knows all -- of Mathlib, so the relevant skill is -- _finding things in it_. -- -- There are at least five techniques for doing so: -- -- 1. Guess the name (naming conventions). -- 2. Ask Lean to search (`exact?`, `apply?`, `rw?`). -- 3. Search by _shape_ (Loogle). -- 4. Search by _words_ (LeanSearch, the docs). -- 5. Read the source. -- ---------------------------------------------------- -- * 1. Guessing the name -- We already discussed this last time. Names are -- systematic descriptions of the statement, and -- are often at least somewhat predicable. -- Here are a few more conventions worth knowing: -- -- _cancel an inverse cancels add_sub_cancel -- _self same argument twice mul_self_nonneg -- _left _right which argument mul_le_mul_left -- _assoc associativity -- _comm commutativity -- _def unfolds a definition Set.subset_def -- _iff an iff version le_iff_lt_or_eq -- _eq an equation -- _ne ≠ -- _pos _neg 0 < x, x < 0 -- _nonneg 0 ≤ x -- _nonpos x ≤ 0 -- _lt_ < -- _le_ ≤ -- -- Namespaces matter too. A lemma about `Nat` that is -- specific to `Nat` lives in the `Nat` namespace: -- `Nat.succ_le_of_lt`. A lemma that holds in any -- ordered field is at top level: `div_le_one`. So: if -- your guess fails at top level, try the namespace of -- the type you are working with, and vice versa. #check @Nat.succ_le_of_lt #check @div_le_one -- ---------------------------------------------------- -- * 2. Letting Lean search -- There are several tactics that let Lean search a -- relevant lemma for you. -- `exact?` searches for a single lemma that closes the -- goal, possibly after `apply`ing it to hypotheses in -- the context. Prints the result for you to paste in. example (a b : ℕ) : a + b = b + a := by exact? --> Try this: exact Nat.add_comm a b -- IMPORTANT: always replace `exact?` by its answer. -- `exact?` takes seconds to run, and its answer may -- change when the library changes. Replacing it by -- its answer makes the file faster and more robust. -- `apply?` is like `exact?`, but also offers lemmas -- that leave subgoals. More hits, more noise. example (a b : ℝ) (h : a ≤ b) : a - b ≤ 0 := by apply? --> Try this: exact tsub_nonpos.mpr h -- NOTE: that answer is worth a pause. `tsub` is -- _truncated_ subtraction, and the lemma is stated in -- a generality that also covers ℕ. It is correct here, -- but a human would write `sub_nonpos.mpr h`. Moral: -- read what the search tools give you; do not paste -- blindly. example (a b : ℝ) (h : a ≤ b) : a - b ≤ 0 := sub_nonpos.mpr h -- `rw?` lists the rewrites that apply to the goal, -- with a preview of the result. It is excellent for -- exploration when you do not know what is available. example (n : ℕ) : n + 0 = n := by rw? --> Try this: rw [Nat.add_eq_left] -- no goals -- `simp?` runs `simp`, then tells you which lemmas -- fired, as a `simp only [...]` call you can paste in. -- Use this to turn a fragile `simp` into a robust -- `simp only`. example (a : ℕ) : a + 0 + 0 = a := by simp? --> Try this: simp only [Nat.add_zero] -- `hint` runs several standard tactics in parallel and -- reports which ones make progress. A good first move -- when you are stuck. example (x : ℝ) (h : 0 < x) : x ≠ 0 := by hint --> Try these: ... positivity ... -- ------------------------------------------------- -- ** The limits of search -- These tactics look for _one_ lemma. They will not -- find a three-step argument. If `exact?` fails, that -- is useful information too: your goal is probably not -- a single library lemma, and you need to decompose it -- first. -- ---------------------------------------------------- -- * 3. Searching by shape: Loogle -- https://loogle.lean-lang.org/ -- -- Loogle searches for _statements of a given shape_, -- which is useful when you can describe the lemma but -- not name it. Query syntax: -- -- |- _ + _ = _ + _ goal shape (use `_` for holes) -- Nat.succ, Nat.le mentions both of these -- "comm" name contains "comm" -- (_ : Nat) → _ by type -- ⊢ _ ∣ _ the conclusion is a divisibility -- -- Queries can be combined with commas: -- -- Real.sqrt, |- _ ≤ _ -- List, "append", |- _ = _ -- -- In class, we searched for the triangle inequality by looking -- for lemmas about the absolute value of a sum: -- -- |_ + _| -- -- We found `abs_add_le`: #check abs_add_le -- ---------------------------------------------------- -- * 4. Searching by words: LeanSearch -- When you cannot even describe the shape, describe -- the mathematics in English: -- -- • LeanSearch https://leansearch.net/ -- -- This is AI search over Mathlib; you type "the sum of -- two even numbers is even" and get candidate lemmas. -- They are often right, but sometimes wrong - verify -- with `#check`. -- ---------------------------------------------------- -- * 5. Reading the source -- A good place to start is the Mathlib documentation: -- -- • https://leanprover-community.github.io/mathlib4_docs/ -- -- The documentation site has, at the top of every -- page, a search box that does plain name/substring -- search. Also useful: each declaration page links to -- its source, and lists the file it lives in. -- -- Mathlib also has a human-written overview of what it -- contains, organized by mathematical area: -- https://leanprover-community.github.io/mathlib-overview.html -- You can also read the source code directly. Often -- this is the fastest method to find something. -- ** Go to definition -- Ctrl-click (or F12) on any identifier jumps to its -- definition in the Mathlib source. Then -- _look at the neighbours_: lemmas in Mathlib are -- grouped, so the lemma you actually want is usually -- within twenty lines of the one you found. -- -- For example, `sub_nonneg` is surrounded by -- `sub_nonpos`, `sub_pos`, `sub_neg`, and so on. #check @sub_pos #check @sub_nonpos #check @sub_neg -- ---------------------------------------------------- -- * A theorem in group theory -- In Lecture 12 we built `Monoid'` as a type class by -- hand. Lean has many type classes for algebraic -- concepts, and they are organized hierarchically: -- every theorem about monoids automatically works for -- groups, and so on. -- -- Semigroup → Monoid → Group → CommGroup -- -- Semiring → Ring → CommRing → Field -- -- Also: Module, Algebra, TopologicalSpace, -- MeasurableSpace, and so on. section GroupTheory -- To work in an arbitrary group, write a `variable` -- with an instance argument. -- -- You can read this as: "Let `G` be an arbitrary -- group". variable {G : Type*} [Group G] -- Let's prove a general fact about groups. We want to -- show that a group where every element has order 2 is -- abelian. -- Let's start by stating the theorem. example (h : ∀ x : G, x * x = 1) (a b : G) : a * b = b * a := sorry -- Next, we prove it by hand. This is an important -- step. It's much easier to formalize a theorem if we -- have at least a general idea how to prove it. -- -- The key lemma is: if a² = 1, then a⁻¹ = a. -- -- After this: ab = (ab)⁻¹ = b⁻¹a⁻¹ = ba. -- We use Loogle to find some relevant lemmas. We need -- the lemma that says `(ab)⁻¹ = b⁻¹a⁻¹`. #check @mul_inv_rev -- The next step is to write a skeleton proof. We just -- list the key lemmas and steps, and do not yet worry -- about filling in each detail. example (h : ∀ x : G, x * x = 1) (a b : G) : a * b = b * a := by have key : ∀ x : G, x⁻¹ = x := by sorry calc a * b _ = (a * b)⁻¹ := by sorry _ = b⁻¹ * a⁻¹ := by sorry _ = b * a := by sorry -- Once the skeleton stands, we can dispose of each -- `sorry` one by one. example (h : ∀ x : G, x * x = 1) (a b : G) : a * b = b * a := by have key : ∀ x : G, x⁻¹ = x := by intro x calc x⁻¹ _ = x⁻¹ * (x * x) := by simp [h] _ = x := by simp calc a * b _ = (a * b)⁻¹ := by rw [key] _ = b⁻¹ * a⁻¹ := by exact? _ = b * a := by repeat rw [key] end GroupTheory -- ---------------------------------------------------- -- * The sum of the first `n` odd numbers. -- Let's prove this theorem: -- -- The sum of the first n odd numbers is n^2. -- First step: State it! We use `Finset.range n`, -- which is the set `{0, 1, …, n-1}`. example (n : ℕ) : ∑ i ∈ Finset.range n, (2*i + 1) = n^2 := by sorry -- Second step: write scaffolding for the proof. The way -- to prove this theorem is by induction. example (n : ℕ) : ∑ i ∈ Finset.range n, (2*i + 1) = n^2 := by induction n with | zero => sorry | succ k ih => sorry -- Flesh it out! The base case is trivial and can be -- solved by `rfl`. The induction step requires us to -- find a lemma about how the sum of `n+1` things -- is the sum of `n` things plus one more thing. -- We found this lemma by Loogling `Finset.range (n + 1)`. example (n : ℕ) : ∑ i ∈ Finset.range n, (2*i + 1) = n^2 := by induction n with | zero => rfl | succ k ih => rw [Finset.sum_range_succ] rw [ih] ring -- Done! -- ---------------------------------------------------- -- * Another quick example. -- Let's prove that if `n` is even then so is `n^2`. -- It turns out there is a relevant lemma in Mathlib, -- and `apply?` finds it. example (n : ℕ) (h : Even n) : Even (n^2) := by apply? --> Try this: [apply] refine (Nat.even_pow' ?_).mpr h -- Flesh it out: example (n : ℕ) (h : Even n) : Even (n^2) := by refine (Nat.even_pow' ?_).mpr h -- Remaining subgoal: 2 ≠ 0. norm_num -- The lemma that `apply?` found is slightly overkill. -- Why did we have to prove `2 ≠ 0`? The reason is that -- the lemma more generally states that `Even n` iff -- `Even (n ^ k)`, which is only true for `k ≠ 0`. #check Nat.even_pow' --> Nat.even_pow' {m n : ℕ} (h : n ≠ 0) : Even (m ^ n) ↔ Even m