In the last post we created our own lemma.
Here we'll make use of that lemma to prove a disequality.
Task
For any natural number
Maths
Intuitively, we can see that small n leads to an
So a proof strategy is to split the natural number sequence into two, so that all the smaller numbers result in
Let's write this out in small steps as preparation for a Lean proof.
We use our lemma (1) to split the natural numbers into two sets, so that for any
This is a disjunction of two cases,
- The first case
means , which also means . The conclusion seem obvious, but we do need to justify it. We can use a tiny lemma . - The second case
means , which also means . Using another small lemma , we conclude .
Both cases lead to
Code
The following Lean program proves
-- 17 - Using Our Own Lemma
import Mathlib.Tactic
---
lemma Nat.le_or_succ_le (a b : ℕ): a ≤ b ∨ b + 1 ≤ a := by
rw [Nat.add_one_le_iff]
exact le_or_lt a b
---
example {n : ℕ} : n^2 ≠ 7 := by
have h := Nat.le_or_succ_le n 2
obtain ha | hb := h
· apply ne_of_lt
calc
n^2 ≤ 2^2 := by rel [ha]
_ < 7 := by norm_num
· apply ne_of_gt
calc
n^2 ≥ 3^2 := by rel [hb]
_ > 7 := by norm_num
The code is separated into two sections by comment dashes.
The first section is a copy of the lemma we developed in the last chapter, Nat.le_or_succ_le.
The second section is the Lean proof that
- Our lemma Nat.le_or_succ_le with parameters n and 2, gives us n ≤ 2 ∨ 2 + 1 ≤ n. We capture this as hypothesis h using have.
- That hypothesis h is a disjunction, so we use obtain to split it into two cases, n ≤ 2 and 2 + 1 ≤ n.
- The first case starts by changing the goal from n^2 ≠ 7 to n^2 < 7 by applying a Mathlib lemma ne_of_lt. Its definition confirms that if we can prove n^2 < 7 then we've proven n^2 ≠ 7:
lemma ne_of_lt (h : a < b) : a %
% b :=
We then use a calc section to show n^2 ≤ 2^2 from the current case n ≤ 2 , then 2^2 < 7, all of which resolves the goal n^2 < 7.
- The second case is similar. It starts by changing the goal from n^2 ≠ 7 to n^2 > 7 by applying a Mathlib lemma ne_of_gt. Its definition confirms that if we can prove n^2 > 7 then we've proven n^2 ≠ 7:
lemma ne_of_gt (h : b < a) : a %
% b :=
Again, we use a calc section to show n^2 ≥ 3^2 from the current case 2 + 1 ≤ n , then 3^2 > 7, all of which resolves the goal n^2 > 7.
Libraries of Lemmas
If our lemma Nat.le_or_succ_le is used in several proofs, then having a copy accompanying each can become a little untidy, and make it more difficult to maintain.
In the wider world of software, re-usable code is packaged into libraries. We can do the same with Lean lemmas and theorems. Appendix B explains briefly how to do this.
Easy Exercise
Write a Lean proof to show that