-- Lecture Notes for Nov 5, 2026 import Mathlib set_option linter.style.header false set_option linter.unusedVariables false -- ---------------------------------------------------- -- * Stating theorems well -- Lean will check theorems, but Lean cannot check -- whether a theorem actually says what you wanted it -- to say. There are a couple of common pitfalls you -- should avoid: -- (a) The wrong type -- This theorem looks weird, but it is actually valid, -- and `omega` happily proves it: example (a b : ℕ) (h : a < b) : a - b = 0 := by omega -- The reason it looks strange is because Lean uses -- truncated subtraction on the natural numbers. Maybe -- you meant a theorem about the integers instead? example (a b : ℤ) (h : a < b) : a - b < 0 := by omega -- (b) Hypotheses that are too strong -- Lean happily proves that `2 + 2 = 5`, given strong -- enough hypotheses. When reading a theorem, -- especially one written by someone else, make sure to -- check the hypotheses carefully, to make sure no -- monster are hiding under the bed. example (n : ℕ) (h : n < n) : 2 + 2 = 5 := by omega -- (c) The wrong quantifier order, or a missing -- quantifier -- There is a big difference between the following: -- -- • ∀ ε > 0, ∃ δ > 0, ∀ x, .... -- -- • ∃ δ > 0, ∀ ε > 0, ∀ x, .... -- ---------------------------------------------------- -- * Convergence of sequences -- Let's do some real math now. We'll start with -- calculus. Let's prove some theorems about -- convergence of sequences. -- What is a sequence? It's just a function from `ℕ.` -- For example, `s : ℕ → ℝ` is a sequence of real -- numbers. The `n`th element in the sequence is -- written `s n` (and not `sₙ` as in everyday -- notation). -- Let's start with a definition. `ConvergesTo s a` -- means the sequence `s` converges to the limit `a`. def ConvergesTo (s : ℕ → ℝ) (a : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |s n - a| < ε -- Our first theorem about sequences is a warm-up -- exercise: constant sequences converge. In class, we -- first wrote `by sorry`, then completed the proof -- step by step. theorem convergesTo_const (a : ℝ) : ConvergesTo (fun n ↦ a) a := by intro ε εpos -- Take an arbitrary `ε > 0`. use 0 -- Fix `N = 0`. intro n n_ge_N -- Consider arbitrary `n > N`. simpa -- The rest of the proof amounts to showing `|a - a| < ε`. -- `simpa` is like `simp`, but can also use assumptions. -- It is needed here because we need the assumption `ε > 0`. -- Our second theorem is another warm-up exercise: -- If `s` converges to `a` then `s + b` converges to `a + b`. theorem convergesTo_add_const (s : ℕ → ℝ) (a : ℝ) (b : ℝ) : ConvergesTo s a → ConvergesTo (fun n ↦ s n + b) (a + b) := by intro hyp ε εpos specialize hyp ε εpos obtain ⟨N, hypN⟩ := hyp use N intro n n_ge_N specialize hypN n n_ge_N simpa -- Our third theorem: the sum of convergent sequences converges. -- -- We start by stating the theorem we want to prove. theorem convergestTo_add_v0 (s t : ℕ → ℝ) (a b : ℝ) : ConvergesTo s a → ConvergesTo t b → ConvergesTo (s + t) (a + b) := by sorry -- It helps to first plan the proof. We sketch a -- pencil-and-paper proof. -- -- • Assume `s` converges to `a` and `t` convergest to `b`. -- • To show `s + t` converges to `a + b`, consider an -- arbitrary `ε > 0`. -- • By convergence of `s`, there is some `N1` such that -- for all `n ≥ N1`, `|s n - a| < ε / 2`. -- • By convergence of `t`, there is some `N2` such that -- for all `n ≥ N2`, `|t n - b| < ε / 2`. -- • Let `N = max N1 N2`, and consider any `n ≥ N`. We must -- show `|(s + t) n - (a + b)| < ε`. -- • It follows from our choice of `N` and the triangle -- inequality. -- Now that we have a plan for the proof, let's put it -- in Lean. theorem convergesTo_add (s t : ℕ → ℝ) (a b : ℝ) : ConvergesTo s a → ConvergesTo t b → ConvergesTo (s + t) (a + b) := by intro hyp1 hyp2 ε εpos specialize hyp1 (ε/2) (by linarith) specialize hyp2 (ε/2) (by linarith) obtain ⟨N1, hyp1⟩ := hyp1 obtain ⟨N2, hyp2⟩ := hyp2 use max N1 N2 intro n n_ge_max have n_ge_N1 : n ≥ N1 := by omega have n_ge_N2 : n ≥ N2 := by omega specialize hyp1 n n_ge_N1 specialize hyp2 n n_ge_N2 have step : |s n - a| + |t n - b| < ε := by linarith calc |(s + t) n - (a + b)| _ = |(s n - a) + (t n - b)| := by congr simp linarith _ ≤ |s n - a| + |t n - b| := by apply abs_add_le _ < ε := by exact step -- ---------------------------------------------------- -- * Using AI -- Proving the last theorem took us almost an hour in -- class. Writing out the pencil-and-paper proof took -- maybe 6 minutes. The usual rule of thumb is that -- doing a tactics proof in Lean takes about 10 times -- longer than doing a pencil-and-paper proof. -- Today, we can use AI to speed up the process. -- One good way to work with AI is to plan the main -- proof steps with `sorry`s, then ask AI to -- fill in the details. -- We state the theorem as before, with `sorry` as the proof: theorem convergesTo_add_claude_v0 (s t : ℕ → ℝ) (a b : ℝ) : ConvergesTo s a → ConvergesTo t b → ConvergesTo (s + t) (a + b) := by sorry -- • Save the file with Ctrl-S. -- -- • Start the AI agent (Claude Code) with Alt-K. -- -- • Prompt: "Can you please fill in the `sorry` on line 158. -- Do it as quickly as possible." -- -- Claude completed the proof for us. It did so by -- editing our file directly. Claude also ran Lean to -- check that the proof compiled (this took a few -- minutes), and wrote the following response: -- -- "I replaced the `sorry` on line 158 of -- [Lecture18.lean] with a proof, and the file now -- compiles with no errors. -- -- It's the standard ε/2 argument: -- -- • Use the convergence of `s` and of `t` with ε/2 each, which gives thresholds N₁ and N₂. -- -- • Take `max N₁ N₂` as the threshold. -- -- • Rewrite `s n + t n - (a + b)` as `(s n - a) + (t n - b)`. -- -- • Finish with the triangle inequality (`abs_add_le`) and `linarith`." -- -- For the readability of these lecture notes, I have -- copied Claude's proof below, rather than directly -- updating `v0` above: theorem convergesTo_add_claude_v1 (s t : ℕ → ℝ) (a b : ℝ) : ConvergesTo s a → ConvergesTo t b → ConvergesTo (s + t) (a + b) := by intro hs ht ε εpos obtain ⟨N₁, hN₁⟩ := hs (ε / 2) (by linarith) obtain ⟨N₂, hN₂⟩ := ht (ε / 2) (by linarith) use max N₁ N₂ intro n hn specialize hN₁ n (le_of_max_le_left hn) specialize hN₂ n (le_of_max_le_right hn) rw [Pi.add_apply, show s n + t n - (a + b) = (s n - a) + (t n - b) by ring] calc |(s n - a) + (t n - b)| ≤ |s n - a| + |t n - b| := abs_add_le _ _ _ < ε := by linarith -- We looked at this proof and found it pretty -- readable, especially in conjunction with Claude's -- explanation. Still, the last 3 lines of the proof -- look a bit inscrutable. -- -- • `Pi.add_apply` is a theorem saying that -- `(s + t) n = s n + t n`. In our own proof, we used -- `rfl` to show this (it is the definition of `s + t` -- for functions). -- -- • Claude's `show ... by ring` inside the `rw` tactic -- is actually a pretty neat trick. It lets you -- rewrite `s n + t n - (a + b)` to `(s n - a) + (t n - b)` -- without first proving it as a separate lemma. -- If we don't like the proof, we can just ask Claude -- to change it. -- Prompt: "Thank you. Can you rewrite the last part of -- the proof, from line 64, to do one step at a time -- instead of a gigantic rewrite. Thanks!" -- Claude responded with a brief report on how it -- rewrote the proof. It rewrote the last few lines as -- follows: theorem convergesTo_add_claude_v2 (s t : ℕ → ℝ) (a b : ℝ) : ConvergesTo s a → ConvergesTo t b → ConvergesTo (s + t) (a + b) := by intro hs ht ε εpos obtain ⟨N₁, hN₁⟩ := hs (ε / 2) (by linarith) obtain ⟨N₂, hN₂⟩ := ht (ε / 2) (by linarith) use max N₁ N₂ intro n hn specialize hN₁ n (le_of_max_le_left hn) specialize hN₂ n (le_of_max_le_right hn) rw [Pi.add_apply] have eq : s n + t n - (a + b) = (s n - a) + (t n - b) := by ring rw [eq] have tri : |(s n - a) + (t n - b)| ≤ |s n - a| + |t n - b| := abs_add_le _ _ linarith