Saturday, November 23, 2024

17 - Using Our Own Lemma

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 n, show that

n27


Maths

Intuitively, we can see that small n leads to an n2 less than 7, and large n results in n2 larger than 7. 

So a proof strategy is to split the natural number sequence into two, so that all the smaller numbers result in n2<7, and all the larger numbers in n2>7. If every possible choice for n leads to either n2<7 or n2>7, then clearly n27.

Let's write this out in small steps as preparation for a Lean proof.

n27proof objective(1)nmm+1nour lemma, for m,nN(2)n23nlemma (1) with m=2case n2using (2)n24<7(3)n27lemma a<babcase n3using (2)n29>7(4)n27lemma a>babn7conclusion of both cases

We use our lemma (1) to split the natural numbers into two sets, so that for any nN we have either n2 or 3n

This is a disjunction of two cases, n2 and n3

  • The first case n2 means n24, which also means n2<7. The conclusion n27 seem obvious, but we do need to justify it. We can use a tiny lemma a<bab.
  • The second case n3 means n29, which also means n2>7. Using another small lemma a>bab, we conclude n27.

Both cases lead to n27, completing the proof.


Code

The following Lean program proves n27 for any natural number n using our lemma. 


-- 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^27  := by
  have h := Nat.le_or_succ_le n 2
  obtain ha | hb := h
  · apply ne_of_lt
    calc
      n^22^2 := by rel [ha]
      _ < 7 := by norm_num
  · apply ne_of_gt
    calc
      n^23^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 n27. Let's break it down.

  • 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 n310, for any natural number n.