4. Sqrt 2 is irrational

Today we will teach Lean that \(\sqrt{2}\) is irrational. Let us start by reviewing some concepts we encountered yesterday.

4.1. Implicit arguments

Consider the following theorem which says that the smallest non-trivial factor of a natural number greater than 1 is a prime number.

Nat.minFac_prime : n ≠ 1 → (Nat.minFac n).Prime

It needs only one argument, namely a term of type n ≠ 1. But we have not told Lean what n is! That’s because if we pass a term, say hp : 2 ≠ 1 to Nat.minFac_prime then from hp Lean can infer that n = 2. n is called an implicit argument. An argument is made implicit by using curly brackets { and } instead of the usual ( and ) while defining the theorem.

theorem Nat.minFac_prime {n : ℕ} (hne1 : n ≠ 1) : (Nat.minFac n).Prime := ...

Sometimes the notation is ambiguous and Lean is unable to infer the implicit arguments. In such a case, you can force all the arguments to become explicit by putting an @ symbol in front of the theorem. For example,

@Nat.minFac_prime : (n : ℕ) → n ≠ 1 → (Nat.minFac n).Prime

Use this sparingly as this makes the proof very hard to read and debug.

4.2. The two haves

We have seen two slightly different variants of the have tactic.

have hq := ...
have hq : ...

In the first case, we are defining hq to be the term on the right hand side. In the second case, we are saying that we do not know what the term hq is but we know it’s type.

Let’s consider the example of Nat.minFac_prime again. Suppose we want to conclude that the smallest factor of 10 is a prime. We will need a term of type (Nat.minFac 10).Prime. If this is the target, we can use apply Nat.minFac_prime. If not, we need a proof of 10 ≠ 1 to provide as input to Nat.minFac_prime. For this we’ll use

have ten_ne_one : 10 ≠ 1

which will open up a goal with target 10 ≠ 1. If on the other hand, you have another hypothesis, say f : P →  (10 ≠ 1) and a term hp : P, then

have ten_ne_one := f hp

will immediately create a term of type 10 ≠ 1. More generally, remember that

  1. “:=” stands for definition. x := ... means that x is defined to be the right hand side.

  2. “:” is a way of specifying type. x : ... means that the type of x is the right hand side.

  3. “=” is only ever used in propositions and has nothing to do with terms or types.

4.3. Sqrt(2) is irrational

We will show that there do not exist non-zero natural numbers m and n such that

2 * m ^ 2 = n ^ 2  -- (*)

The crux of the proof is very easy. You simply have to start with the assumption that m and n are coprime without any loss of generality and derive a contradiction. But proving that without loss of generality is a valid argument requires quite a bit of effort. This proof is broken down into several parts. The first two parts prove (*) assuming that m and n are coprime. The rest of the parts prove the without loss of generality part.

For this problem, you’ll need the following definitions.

  • m.gcd n : ℕ is the gcd of m and n.

  • m.Coprime n is defined to be the proposition m.gcd n = 1.

The descriptions of the library theorems you’ll need are included as comments. Have fun!

4.3.1. Lemmas for proving (*) assuming m and n are coprime.

-- Nat.Prime.dvd_of_dvd_pow : p.Prime → p ∣ m ^ n → p ∣ m
lemma two_dvd_of_two_dvd_sq {k : ℕ}
    (hk : 2 ∣ k ^ 2) :
    2 ∣ k := by
  sorry

-- to switch the target from ``P = Q`` to ``Q = P``,
-- use the tactic ``symm``
lemma division_lemma_n {m n : ℕ}
    (hmn : 2 * m ^ 2 = n ^ 2) :
    2 ∣ n := by
  sorry

lemma div_2 {m n : ℕ} (hnm : 2 * m = 2 * n) : (m = n) := by
  linarith

lemma division_lemma_m {m n : ℕ}
    (hmn : 2 * m ^ 2 = n ^ 2) :
    2 ∣ m := by
  sorry

4.3.2. Prove (*) assuming m and n are coprime.

-- If ``1 < d``, ``d ∣ m``, and ``d ∣ n``, then ``d ∣ Nat.gcd m n`` (via
-- ``Nat.dvd_gcd``), and since ``m.Coprime n`` means ``Nat.gcd m n = 1``,
-- this contradicts ``1 < d``.

theorem sqrt2_irrational' :
    ¬ ∃ (m n : ℕ), 2 * m ^ 2 = n ^ 2 ∧ m.Coprime n := by
  intro hex
  obtain ⟨m, n, hmn, h_cop⟩ := hex
  -- obtain lets you destructure the hypothesis ``hex``
  -- you get the brackets by typing ``\langle`` and ``\rangle``
  sorry

4.3.3. Lemmas for proving (*) assuming m ≠ 0

lemma ne_zero_ge_zero {n : ℕ}
    (hne : n ≠ 0) :
    (0 < n) := by
  contrapose! hne
  sorry

-- Nat.pow_pos : 0 < p → 0 < p ^ n  (n is inferred automatically)
lemma ge_zero_sq_ge_zero {n : ℕ} (hne : 0 < n) : (0 < n ^ 2) := by
  sorry

lemma cancellation_lemma {k m n : ℕ}
    (hk_pos : 0 < k ^ 2)
    (hmn : 2 * (m * k) ^ 2 = (n * k) ^ 2) :
    2 * m ^ 2 = n ^ 2 := by
  apply Nat.eq_of_mul_eq_mul_right hk_pos
  calc 2 * m ^ 2 * k ^ 2 = 2 * (m * k) ^ 2 := by ring
    _ = (n * k) ^ 2 := hmn
    _ = n ^ 2 * k ^ 2 := by ring

4.3.4. Prove (*) assuming m ≠ 0

-- Nat.gcd_pos_of_pos_left : ∀ (n : ℕ), 0 < m → 0 < Nat.gcd m n
-- Nat.gcd_pos_of_pos_right : ∀ (m : ℕ), 0 < n → 0 < Nat.gcd m n
-- Nat.exists_coprime : ∀ (m n : ℕ), ∃ m' n', m'.Coprime n' ∧ m = m' * m.gcd n ∧ n = n' * m.gcd n

theorem wlog_coprime :
    (∃ (m n : ℕ), 2 * m ^ 2 = n ^ 2 ∧ m ≠ 0) →
    (∃ (m' n' : ℕ), 2 * m' ^ 2 = n' ^ 2 ∧ m'.Coprime n') := by
  intro hex
  obtain ⟨m, n, hmn, hme0⟩ := hex
  set k := m.gcd n with hk
  -- might be useful to declutter
  -- you can replace all the ``m.gcd n`` with ``k`` using ``rw [← hk]`` if needed
  sorry

theorem sqrt2_irrational'' :
    ¬ ∃ (m n : ℕ), 2 * m ^ 2 = n ^ 2 ∧ m ≠ 0 := by
  sorry

4.4. Solutions

Worked solutions to all of today’s exercises, in the order they appear above, are in Day4.lean.