4. Proofs with structure, II
In the course of Chapter 2, we studied the
logical symbols
This chapter finishes the work started in Chapter 2.
We learn the grammar of the remaining logical symbols:
4.1. “For all” and implication
4.1.1. Example
Problem
Let

Fig. 4.1 The parabola
Specifying that a formula or predicate is meant to be true for all values of a variable ∀
.
To use a hypothesis with a universal quantifier, you may want to “specialize” its use to
one particular variable. For example, in the solution below, we use the special case of the
hypothesis in which
Solution
In Lean, perform this specialization using the apply
tactic.
example {a : ℝ} (h : ∀ x, a ≤ x ^ 2 - 2 * x) : a ≤ -1 :=
calc
a ≤ 1 ^ 2 - 2 * 1 := by apply h
_ = -1 := by numbers
4.1.2. Example
Problem
Let
Solution
Since
For the Lean proof, we recall that the bound from Example 3.2.7 is in Lean as
Nat.le_of_dvd
, and the bound from Example 3.2.8 is in Lean as
Nat.pos_of_dvd_of_pos
.
example {n : ℕ} (hn : ∀ m, n ∣ m) : n = 1 := by
have h1 : n ∣ 1 := by apply hn
have h2 : 0 < 1 := by numbers
apply le_antisymm
· apply Nat.le_of_dvd h2 h1
· apply Nat.pos_of_dvd_of_pos h1 h2
4.1.3. Example
Problem
Let
Solution
Consider the real number
Case 1:
Case 2:
example {a b : ℝ} (h : ∀ x, x ≥ a ∨ x ≤ b) : a ≤ b := by
sorry
4.1.4. Example
Problem
Let
Consider the hypothesis in this problem that
is greater than or equal to any real number whose square is at most 2.
Implicitly, there is a universal quantification (“any real number”), and also an implication, so a more pedantic version of this hypothesis would be
for all real numbers
, if then .
We can specialize such a hypothesis to any particular choice of
Solution
Since
Since
Therefore
In Lean, an implication is expressed using the symbol →
. The tactic apply
works for
hypotheses featuring implications. In the proof below, the goal state before apply hb2
is
a b: ℝ
ha1 : a ^ 2 ≤ 2
hb1 : b ^ 2 ≤ 2
ha2 : ∀ (y : ℝ), y ^ 2 ≤ 2 → y ≤ a
hb2 : ∀ (y : ℝ), y ^ 2 ≤ 2 → y ≤ b
⊢ a ≤ b
and the goal state after it is
a b : ℝ
ha1 : a ^ 2 ≤ 2
hb1 : b ^ 2 ≤ 2
ha2 : ∀ (y : ℝ), y ^ 2 ≤ 2 → y ≤ a
hb2 : ∀ (y : ℝ), y ^ 2 ≤ 2 → y ≤ b
⊢ a ^ 2 ≤ 2
The hypothesis ∀ (y : ℝ), y ^ 2 ≤ 2 → y ≤ b
has been applied to the goal a ≤ b
, leaving
the hopefully-easier goal a ^ 2 ≤ 2
: proving the antecedent of the implication.
Fill in the second part of the proof.
example {a b : ℝ} (ha1 : a ^ 2 ≤ 2) (hb1 : b ^ 2 ≤ 2) (ha2 : ∀ y, y ^ 2 ≤ 2 → y ≤ a)
(hb2 : ∀ y, y ^ 2 ≤ 2 → y ≤ b) :
a = b := by
apply le_antisymm
· apply hb2
apply ha1
· sorry
4.1.5. Example
Problem
Show that there exists a real number
Notice that in this problem a universally quantified statement appears in the goal:
“for every real number
Solution
We show that -1 has this property. Indeed, let
We solve the problem by formally introducing a particular, arbitrary real number intro
. Before the use of this tactic, the goal state is
⊢ ∀ (x : ℝ), -1 ≤ x ^ 2 - 2 * x
After the use of this tactic, the goal state is
x : ℝ
⊢ -1 ≤ x ^ 2 - 2 * x
example : ∃ b : ℝ, ∀ x : ℝ, b ≤ x ^ 2 - 2 * x := by
use -1
intro x
calc
-1 ≤ -1 + (x - 1) ^ 2 := by extra
_ = x ^ 2 - 2 * x := by ring
4.1.6. Example
Problem
Show that there exists a real number
Here the goal contains universal quantification over
Solution
We show that -3 has this property. Indeed, let
So
In Lean, the introduction of both the variables intro
tactic. To deduce from
lemma abs_le_of_sq_le_sq' (h : x ^ 2 ≤ y ^ 2) (hy : 0 ≤ y) : -y ≤ x ∧ x ≤ y :=
which we previously saw in Example 2.4.2.
example : ∃ c : ℝ, ∀ x y, x ^ 2 + y ^ 2 ≤ 4 → x + y ≥ c := by
sorry
4.1.7. Example
Definition
A property is true for all sufficiently large integers
Likewise for rational numbers, real numbers, ….
Problem
Show that for all sufficiently large integers
In the solution that follows, “For all
Solution
For all
I have provided the notation forall_sufficiently_large
to express this and similar problems in
Lean.
example : forall_sufficiently_large n : ℤ, n ^ 3 ≥ 4 * n ^ 2 + 7 := by
dsimp
use 5
intro n hn
calc
n ^ 3 = n * n ^ 2 := by ring
_ ≥ 5 * n ^ 2 := by rel [hn]
_ = 4 * n ^ 2 + n ^ 2 := by ring
_ ≥ 4 * n ^ 2 + 5 ^ 2 := by rel [hn]
_ = 4 * n ^ 2 + 7 + 18 := by ring
_ ≥ 4 * n ^ 2 + 7 := by extra
4.1.8. Example
Definition
A natural number
def Prime (p : ℕ) : Prop :=
2 ≤ p ∧ ∀ m : ℕ, m ∣ p → m = 1 ∨ m = p
Problem
Show that 2 is prime.
Solution
Clearly
One new technique used in this solution is to observe from numeric bounds on a natural number (like
interval_cases
for this kind of
argument. It also works for integers, but it doesn’t work for rational numbers or real numbers –
why?
example : Prime 2 := by
constructor
· numbers -- show `2 ≤ 2`
intro m hmp
have hp : 0 < 2 := by numbers
have hmp_le : m ≤ 2 := Nat.le_of_dvd hp hmp
have h1m : 1 ≤ m := Nat.pos_of_dvd_of_pos hmp hp
interval_cases m
· left
numbers -- show `1 = 1`
· right
numbers -- show `2 = 2`
This lemma is available for future use in Lean under the name prime_two
.
4.1.9. Example
You might be wondering how to prove that a natural number
Problem
Show that 6 is not prime.
Solution
We will prove this test carefully in Example 4.5.7. For now, feel free to
use it. In Lean the lemma name is not_prime
.
example : ¬ Prime 6 := by
apply not_prime 2 3
· numbers -- show `2 ≠ 1`
· numbers -- show `2 ≠ 6`
· numbers -- show `6 = 2 * 3`
4.1.10. Exercises
Let
be a rational number and suppose that for all rational numbers , . Show that .example {a : ℚ} (h : ∀ b : ℚ, a ≥ -3 + 4 * b - b ^ 2) : a ≥ 1 := sorry
Let
be an integer and suppose that every integer between 1 and 5 is a factor of . Show that 15 is a factor of . (You may need to review Section 3.5.)example {n : ℤ} (hn : ∀ m, 1 ≤ m → m ≤ 5 → m ∣ n) : 15 ∣ n := by sorry
Show that there exists a natural number
such that every natural number is at least .example : ∃ n : ℕ, ∀ m : ℕ, n ≤ m := by sorry
Show that there exists a real number
, such that for all real numbers , there exists a real number , such that .example : ∃ a : ℝ, ∀ b : ℝ, ∃ c : ℝ, a + b < c := by sorry
Show that for all sufficiently large real numbers
, .example : forall_sufficiently_large x : ℝ, x ^ 3 + 3 * x ≥ 7 * x ^ 2 + 12 := by sorry
Show that 45 is not prime.
You may use the Lean lemma
not_prime
, as in Example 4.1.9.example : ¬(Prime 45) := by sorry
Footnotes
- 1
That is,
is maximal in the set of real numbers whose square is at most 2.
4.2. “If and only if”
4.2.1. Example
Problem
Let
The phrase “if and only if” means exactly what it sounds like. In this problem we have to show
(1) if
Solution
First, suppose that
Conversely, suppose that
In handwritten work, it is quite common to annotate the two directions with the symbols
Solution
This is recommended for homework, tests, writing on the blackboard, etc. In more formal writing (such as this book) we omit such symbols and instead use words like “First” and “Conversely” to signal the different parts of the proof.
In Lean, an “if and only if” is stated with the bi-implication symbol ↔
. Since, under the hood,
an “if and only if” is an “and” statement, we use the same tactic as for “and” goals:
constructor
.
example {a : ℚ} : 3 * a + 1 ≤ 7 ↔ a ≤ 2 := by
constructor
· intro h
calc a = ((3 * a + 1) - 1) / 3 := by ring
_ ≤ (7 - 1) / 3 := by rel [h]
_ = 2 := by numbers
· intro h
calc 3 * a + 1 ≤ 3 * 2 + 1 := by rel [h]
_ = 7 := by numbers
4.2.2. Example
Let’s modify Example 3.5.1 to be an if-and-only-if problem. Now there are two things to prove, one of which we did before.
Problem
Let
Solution
Suppose that
so
Conversely, suppose that
so
example {n : ℤ} : 8 ∣ 5 * n ↔ 8 ∣ n := by
constructor
· intro hn
obtain ⟨a, ha⟩ := hn
use -3 * a + 2 * n
calc
n = -3 * (5 * n) + 16 * n := by ring
_ = -3 * (8 * a) + 16 * n := by rw [ha]
_ = 8 * (-3 * a + 2 * n) := by ring
· intro hn
obtain ⟨a, ha⟩ := hn
use 5 * a
calc 5 * n = 5 * (8 * a) := by rw [ha]
_ = 8 * (5 * a) := by ring
4.2.3. Example
Problem
Show that an integer
Solution
First, suppose that
Conversely, suppose that
We name this example Int.odd_iff_modEq
for future use.
theorem odd_iff_modEq (n : ℤ) : Odd n ↔ n ≡ 1 [ZMOD 2] := by
constructor
· intro h
obtain ⟨k, hk⟩ := h
dsimp [Int.ModEq]
dsimp [(· ∣ ·)]
use k
addarith [hk]
· sorry
4.2.4. Example
Now do the same to characterise evenness.
Problem
Show that an integer
theorem even_iff_modEq (n : ℤ) : Even n ↔ n ≡ 0 [ZMOD 2] := by
constructor
· intro h
obtain ⟨k, hk⟩ := h
dsimp [Int.ModEq]
dsimp [(· ∣ ·)]
use k
addarith [hk]
· sorry
4.2.5. Example
The high school concept of “solving” equations represents an “if and only if” problem: to solve an equation, you state a list of numbers and prove that they satisfy the equation and no other numbers do.
Problem
Let
Solution
First, suppose that
so either
Conversely, if
and if
example {x : ℝ} : x ^ 2 + x - 6 = 0 ↔ x = -3 ∨ x = 2 := by
sorry
4.2.6. Example
Problem
Let
Solution
First, suppose that
so
Conversely, if
and if
example {a : ℤ} : a ^ 2 - 5 * a + 5 ≤ -1 ↔ a = 2 ∨ a = 3 := by
sorry
4.2.7. Example
Some library lemmas have the form of an “if and only if”. This is convenient because they take the place of two ordinary lemmas, one for each direction.
Problem
Let
Solution
We have
so either
In this problem we need to transform the fact
theorem eq_zero_or_eq_zero_of_mul_eq_zero {a b : ℤ} (h : a * b = 0) : a = 0 ∨ b = 0 :=
resulting in a proof setup like this:
example {n : ℤ} (hn : n ^ 2 - 10 * n + 24 = 0) : Even n := by
have hn1 :=
calc (n - 4) * (n - 6) = n ^ 2 - 10 * n + 24 := by ring
_ = 0 := hn
have hn2 := eq_zero_or_eq_zero_of_mul_eq_zero hn1
sorry
(Exercise: finish off the proof.)
But there is also a library lemma in ↔
form, which combines eq_zero_or_eq_zero_of_mul_eq_zero
with the converse (other direction) of that statement:
theorem mul_eq_zero {a b : ℤ} : a * b = 0 ↔ a = 0 ∨ b = 0 :=
We can use an ↔
lemma in Lean with the rw
tactic; it converts a hypothesis (or the goal) in
the form of the left-hand side of the ↔
to one with the form of the right-hand side.
example {n : ℤ} (hn : n ^ 2 - 10 * n + 24 = 0) : Even n := by
have hn1 :=
calc (n - 4) * (n - 6) = n ^ 2 - 10 * n + 24 := by ring
_ = 0 := hn
rw [mul_eq_zero] at hn1 -- `hn1 : n - 4 = 0 ∨ n - 6 = 0`
sorry
4.2.8. Example
Above, in Example 4.2.3, we proved that an integer is odd if and only if it
is congruent to 1 modulo 2, recording this under the name Int.odd_iff_modEq
. This is now also
a convenient “if and only if” library lemma which we can use to solve problems about parity using
modulo arithmetic. As an example, let’s re-do the problem from
Example 3.1.5.
Problem
Prove that if the integers
Solution
We will prove that if
example {x y : ℤ} (hx : Odd x) (hy : Odd y) : Odd (x + y + 1) := by
rw [Int.odd_iff_modEq] at *
calc x + y + 1 ≡ 1 + 1 + 1 [ZMOD 2] := by rel [hx, hy]
_ = 2 * 1 + 1 := by ring
_ ≡ 1 [ZMOD 2] := by extra
4.2.9. Example
Another way we can use the characterization of parity in terms of modular arithmetic is to prove the theorem from Example 3.1.9 whose proof we skipped.
Theorem
Every integer is either even or odd.
Proof
Let
If
If
Write this in Lean using the lemmas Int.odd_iff_modEq
and Int.even_iff_modEq
from
Example 4.2.3 and Example 4.2.4, together with the
tactic mod_cases
. I have written the beginning.
example (n : ℤ) : Even n ∨ Odd n := by
mod_cases hn : n % 2
· left
rw [Int.even_iff_modEq]
apply hn
· sorry
4.2.10. Exercises
Let
be a real number. Show that if and only if .example {x : ℝ} : 2 * x - 1 = 11 ↔ x = 6 := by sorry
Let
be an integer. Show that 63 is a factor of if and only if both 7 and 9 are.example {n : ℤ} : 63 ∣ n ↔ 7 ∣ n ∧ 9 ∣ n := by sorry
Let
and be integers. Show that is a multiple of if and only if .theorem dvd_iff_modEq {a n : ℤ} : n ∣ a ↔ a ≡ 0 [ZMOD n] := by sorry
Let
and be integers and suppose that . Show that .Note that this appeared already as an exercise in Section 3.2. But now, using the lemma
Int.dvd_iff_modEq
proved in the previous exercise, this kind of problem is much easier.example {a b : ℤ} (hab : a ∣ b) : a ∣ 2 * b ^ 3 - b ^ 2 + 3 * b := by sorry
Let
be a natural number. Show that , if and only if is 0, 1 or 2.example {k : ℕ} : k ^ 2 ≤ 6 ↔ k = 0 ∨ k = 1 ∨ k = 2 := by sorry
4.3. “There exists a unique”
4.3.1. Example
Problem
Show that there exists a unique real number
Solution
We will show that 2 is the unique real number with this property.
First, we show that 2 has this property. Indeed,
Now, let
example : ∃! a : ℝ, 3 * a + 1 = 7 := by
use 2
dsimp
constructor
· numbers
intro y hy
calc
y = (3 * y + 1 - 1) / 3 := by ring
_ = (7 - 1) / 3 := by rw [hy]
_ = 2 := by numbers
4.3.2. Example
Problem
Show that there exists a unique rational number
Solution
We will show that 2 is the unique rational number with this property.
Firstly, if
Now, let
Since 1 is between 1 and 3,
So
Also
In Lean, the result of Example 2.1.7 is available as the lemma sq_le_sq'
.
example : ∃! x : ℚ, ∀ a, a ≥ 1 → a ≤ 3 → (a - x) ^ 2 ≤ 1 := by
sorry
4.3.3. Example
Problem
Let
More colloquially: the only rational number with a unique square root is 0.
Solution
We first show that
and since
It follows that
So
example {x : ℚ} (hx : ∃! a : ℚ, a ^ 2 = x) : x = 0 := by
obtain ⟨a, ha1, ha2⟩ := hx
have h1 : -a = a
· apply ha2
calc
(-a) ^ 2 = a ^ 2 := by ring
_ = x := ha1
have h2 :=
calc
a = (a - -a) / 2 := by ring
_ = (a - a) / 2 := by rw [h1]
_ = 0 := by ring
calc
x = a ^ 2 := by rw [ha1]
_ = 0 ^ 2 := by rw [h2]
_ = 0 := by ring
4.3.4. Example
The following is an important theorem about the integers, which we will prove later in the book, in the exercises to Section 6.6.
Theorem (the Division Algorithm)
Let
This lemma is what allows us to do case divisions according to congruence class modulo mod_cases
). But it’s actually a bit more powerful, since the “uniqueness” part
of the statement provides extra information.
In the Lean library it is available in the following form:
lemma Int.existsUnique_modEq_lt (a b : ℤ) (h : 0 < b) :
∃! r : ℤ, 0 ≤ r ∧ r < b ∧ a ≡ r [ZMOD b] :=
To understand this theorem better, let’s prove just one of the infinitely many cases of this theorem.
Problem
Show that there exists a unique integer
Solution
We will show that the unique integer with this property is 4.
First, we show that 4 has this property. It is true that
Now, let
We have,
so since
and so since
Therefore
example : ∃! r : ℤ, 0 ≤ r ∧ r < 5 ∧ 14 ≡ r [ZMOD 5] := by
use 4
dsimp
constructor
· constructor
· numbers
constructor
· numbers
use 2
numbers
intro r hr
obtain ⟨hr1, hr2, q, hr3⟩ := hr
have :=
calc
5 * 1 < 14 - r := by addarith [hr2]
_ = 5 * q := by rw [hr3]
cancel 5 at this
have :=
calc
5 * q = 14 - r := by rw [hr3]
_ < 5 * 3 := by addarith [hr1]
cancel 5 at this
interval_cases q
addarith [hr3]
4.3.5. Exercises
Show that there exists a unique rational number
, such that .example : ∃! x : ℚ, 4 * x - 3 = 9 := by sorry
Show that there exists a unique natural number
, such that for all natural numbers , we have .example : ∃! n : ℕ, ∀ a, n ≤ a := by sorry
Show that there exists a unique integer
, such that and .example : ∃! r : ℤ, 0 ≤ r ∧ r < 3 ∧ 11 ≡ r [ZMOD 3] := by sorry
4.4. Contradictory hypotheses
4.4.1. Example
Sometimes, we encounter a situation with two contradictory hypotheses. At this point, there is no need to prove anything more. Two contradictory hypotheses mean that the situation we had hypothesized can’t actually happen.
It is quite common to encounter this when giving a proof by cases. You might reduce the problem to some list of cases, prove your goal in some of those cases, and prove that the other cases are impossible.
Here is an example of this kind of reasoning. I have written the solution very pedantically, to make the point clearer.
Lemma
Let
We have used this fact many times before – it is one of the facts which underlie the cancel
tactic.
Proof
We consider two cases, depending on whether or not
Case 1 (
and so it is false that
Case 2 (
In Lean, the tactic contradiction
concludes a (part of a) proof by pointing out two
contradictory hypotheses. In the Lean translation of this example, notice that right before it’s
used, the goal state is
y x : ℝ
h : 0 < x * y
hx : 0 ≤ x
hneg : y ≤ 0
this : ¬0 < x * y
⊢ 0 < y
which contains the contradictory hypotheses h : 0 < x * y
and this : ¬0 < x * y
. (Remember
that ¬
is the logical symbol for “not”. If you don’t name a hypothesis, Lean labels it
this
.)
example {y : ℝ} (x : ℝ) (h : 0 < x * y) (hx : 0 ≤ x) : 0 < y := by
obtain hneg | hpos : y ≤ 0 ∨ 0 < y := le_or_lt y 0
· -- the case `y ≤ 0`
have : ¬0 < x * y
· apply not_lt_of_ge
calc
0 = x * 0 := by ring
_ ≥ x * y := by rel [hneg]
contradiction
· -- the case `0 < y`
apply hpos
4.4.2. Example
One very common way to get a contradiction is by proving that the hypotheses imply some “obviously
false” numeric fact, whose falseness can be checked with numbers
.
Problem
Let
Solution
We have,
But clearly it’s false that
You can write this proof up in Lean directly using the contradiction
tactic, as in the previous
examples:
example {t : ℤ} (h2 : t < 3) (h : t - 1 = 6) : t = 13 := by
have H :=
calc
7 = t := by addarith [h]
_ < 3 := h2
have : ¬(7 : ℤ) < 3 := by numbers
contradiction
But the pattern is also sufficiently common that there is a shorthand in Lean. If H
is a
hypothesis whose negation can be proved by numbers
, then writing numbers at H
will
close the goal.
example {t : ℤ} (h2 : t < 3) (h : t - 1 = 6) : t = 13 := by
have H :=
calc
7 = t := by addarith [h]
_ < 3 := h2
numbers at H -- this is a contradiction!
4.4.3. Example
Problem
Show that if
Solution
We consider cases according to the residue of
contradiction.
Notice that in the above proof we did some extra work to get
For the purposes of this book, we will treat as “obviously true/false” only congruences
The mathematical justification for this is the uniqueness lemma Int.existsUnique_modEq_lt
,
discussed in Example 4.3.4.
example (n : ℤ) (hn : n ^ 2 + n + 1 ≡ 1 [ZMOD 3]) :
n ≡ 0 [ZMOD 3] ∨ n ≡ 2 [ZMOD 3] := by
mod_cases h : n % 3
· -- case 1: `n ≡ 0 [ZMOD 3]`
left
apply h
· -- case 2: `n ≡ 1 [ZMOD 3]`
have H :=
calc 0 ≡ 0 + 3 * 1 [ZMOD 3] := by extra
_ = 1 ^ 2 + 1 + 1 := by numbers
_ ≡ n ^ 2 + n + 1 [ZMOD 3] := by rel [h]
_ ≡ 1 [ZMOD 3] := hn
numbers at H -- contradiction!
· -- case 3: `n ≡ 2 [ZMOD 3]`
right
apply h
4.4.4. Example
We defined prime numbers in Example 4.1.8, and proved that 2 was prime. Let’s now prove a slight reformulation of the definition, which will be convenient in showing that other numbers are prime.
Lemma
Let
Proof
Since we are given that
Since
Case 1 (
Case 2 (
Case 2(i) (
Case 2(ii) (
I’ve filled in about half of this proof in Lean; fill in the rest, including the final contradiction. If you need to look up the Lean names for the size bounds on factors, they were proved in Example 3.2.7 and Example 3.2.8.
example {p : ℕ} (hp : 2 ≤ p) (H : ∀ m : ℕ, 1 < m → m < p → ¬m ∣ p) : Prime p := by
constructor
· apply hp -- show that `2 ≤ p`
intro m hmp
have hp' : 0 < p := by extra
have h1m : 1 ≤ m := Nat.pos_of_dvd_of_pos hmp hp'
obtain hm | hm_left : 1 = m ∨ 1 < m := eq_or_lt_of_le h1m
· -- the case `m = 1`
left
addarith [hm]
-- the case `1 < m`
sorry
We record this for future use under the Lean name prime_test
.
Here’s an example of how this prime-testing lemma is used.
Problem
Show that 5 is prime.
Solution
Clearly
Case 1 (
Case 2 (
Case 3 (
example : Prime 5 := by
apply prime_test
· numbers
intro m hm_left hm_right
apply Nat.not_dvd_of_exists_lt_and_lt
interval_cases m
· use 2
constructor <;> numbers
· use 1
constructor <;> numbers
· use 1
constructor <;> numbers
Here constructor <;> numbers
is a shorthand for
constructor
· numbers
· numbers
More generally, <;>
connects two tactics, performing the second one on every goal created by the
first one.
4.4.5. Example
Here’s a harder example, with many cases.
Problem
Let
Three numbers satisfying this equation are called a Pythagorean triple, since by Pythagoras’
theorem this means that they form the three sides of a right-angled triangle. The natural numbers
3, 4, 5 satisfy this equation:
Solution
Either
Either
Case 1 (
We have that
This implies that
Case 2 (
We have that
so
so
Write this proof up in Lean. It will be long; my version is 27 lines.
example {a b c : ℕ} (ha : 0 < a) (hb : 0 < b) (hc : 0 < c)
(h_pyth : a ^ 2 + b ^ 2 = c ^ 2) : 3 ≤ a := by
sorry
4.4.6. Exercises
Let
and be real numbers, with nonnegative, and let be a positive natural number. Prove that if then .We have used this fact before; it’s another of the lemmas which underlie the
cancel
tactic.example {x y : ℝ} (n : ℕ) (hx : 0 ≤ x) (hn : 0 < n) (h : y ^ n ≤ x ^ n) : y ≤ x := by sorry
Show that if
then or .example (n : ℤ) (hn : n ^ 2 ≡ 4 [ZMOD 5]) : n ≡ 2 [ZMOD 5] ∨ n ≡ 3 [ZMOD 5] := by sorry
Show that 7 is prime.
example : Prime 7 := by sorry
Give a different proof of a problem from the exercises to Section 2.1: Let
be a rational number whose square is 4, and which is greater than 1. Show that .Instead of using the
cancel
tactic, give a direct proof using the ideas of this section: break into two cases, and then rule out one of them. (You may find it convenient to deduce a numeric contradiction.)example {x : ℚ} (h1 : x ^ 2 = 4) (h2 : 1 < x) : x = 2 := by have h3 := calc (x + 2) * (x - 2) = x ^ 2 + 2 * x - 2 * x - 4 := by ring _ = 0 := by addarith [h1] rw [mul_eq_zero] at h3 sorry
Show that a prime number is either 2 or odd.
example (p : ℕ) (h : Prime p) : p = 2 ∨ Odd p := by sorry
4.5. Proof by contradiction
4.5.1. Example
Problem
Show that it is not true that for all real numbers
Solution
Suppose that for all real numbers
example : ¬ (∀ x : ℝ, x ^ 2 ≥ x) := by
intro h
have : 0.5 ^ 2 ≥ 0.5 := h 0.5
numbers at this
4.5.2. Example
Problem
Show that 13 is not a multiple of 3.
In the past we have established non-divisibility facts using the theorem
Nat.not_dvd_of_exists_lt_and_lt
. But now we finally have the tools to do this from first
principles.
Solution
Suppose that 13 were a multiple of 3. Then there would exist a natural number
Case 1,
contradiction.
Case 2,
contradiction.
example : ¬ 3 ∣ 13 := by
intro H
obtain ⟨k, hk⟩ := H
obtain h4 | h5 := le_or_succ_le k 4
· have h :=
calc 13 = 3 * k := hk
_ ≤ 3 * 4 := by rel [h4]
numbers at h
· sorry
4.5.3. Example
Problem
Let
Solution
Suppose that
contradiction.
example {x y : ℝ} (h : x + y = 0) : ¬(x > 0 ∧ y > 0) := by
intro h
obtain ⟨hx, hy⟩ := h
have H :=
calc 0 = x + y := by rw [h]
_ > 0 := by extra
numbers at H
4.5.4. Example
Problem
Show that there does not exist a natural number
(Compare this with Example 2.3.2.)
Solution
Suppose that some integer
Case 1,
contradiction.
Case 2,
contradiction.
example : ¬ (∃ n : ℕ, n ^ 2 = 2) := by
sorry
4.5.5. Example
Lemma
Show that an integer
Proof
First, let
contradiction.
Now, suppose that
We record this for future use in Lean problems under the name Int.even_iff_not_odd
.
example (n : ℤ) : Int.Even n ↔ ¬ Int.Odd n := by
constructor
· intro h1 h2
rw [Int.even_iff_modEq] at h1
rw [Int.odd_iff_modEq] at h2
have h :=
calc 0 ≡ n [ZMOD 2] := by rel [h1]
_ ≡ 1 [ZMOD 2] := by rel [h2]
numbers at h -- contradiction!
· intro h
obtain h1 | h2 := Int.even_or_odd n
· apply h1
· contradiction
Now repeat the process to characterise “not-even.”
Lemma
Show that an integer
example (n : ℤ) : Int.Odd n ↔ ¬ Int.Even n := by
sorry
4.5.6. Example
Problem
Let
Solution
Suppose that
If
contradiction.
If
contradiction.
Finally, if
contradiction.
example (n : ℤ) : ¬(n ^ 2 ≡ 2 [ZMOD 3]) := by
intro h
mod_cases hn : n % 3
· have h :=
calc (0:ℤ) = 0 ^ 2 := by numbers
_ ≡ n ^ 2 [ZMOD 3] := by rel [hn]
_ ≡ 2 [ZMOD 3] := by rel [h]
numbers at h -- contradiction!
· sorry
· sorry
4.5.7. Example
We can now pay a couple of debts. First, there is this theorem, first mentioned in Example 4.1.9:
Theorem
Let
Proof
example {p : ℕ} (k l : ℕ) (hk1 : k ≠ 1) (hkp : k ≠ p) (hkl : p = k * l) :
¬(Prime p) := by
have hk : k ∣ p
· use l
apply hkl
intro h
obtain ⟨h2, hfact⟩ := h
have : k = 1 ∨ k = p := hfact k hk
obtain hk1' | hkp' := this
· contradiction
· contradiction
4.5.8. Example
Secondly, there is this theorem, first mentioned in Example 3.2.6:
Theorem
Let
This is the lemma we have invoked in Lean as Int.not_dvd_of_exists_lt_and_lt
.
Proof
Suppose for the sake of contradiction that
We first note that
Now let us reason from the two known inequalities separately. We first observe that
and so (since
We next observe that
and so (since
These two facts contradict each other, so
example (a b : ℤ) (h : ∃ q, b * q < a ∧ a < b * (q + 1)) : ¬b ∣ a := by
intro H
obtain ⟨k, hk⟩ := H
obtain ⟨q, hq₁, hq₂⟩ := h
have hb :=
calc 0 = a - a := by ring
_ < b * (q + 1) - b * q := by rel [hq₁, hq₂]
_ = b := by ring
have h1 :=
calc b * k = a := by rw [hk]
_ < b * (q + 1) := hq₂
cancel b at h1
sorry
4.5.9. Example
We also establish a test for primality which will be more efficient than the test from Example 4.4.4.
Theorem
Let
(Notice that in the Example 4.4.4 test we had to check that every number up to
Proof
By the prime test from Example 4.4.4, it suffices to show that every natural
number
So suppose that
We claim that
We also claim that
Since we have established that
example {p : ℕ} (hp : 2 ≤ p) (T : ℕ) (hTp : p < T ^ 2)
(H : ∀ (m : ℕ), 1 < m → m < T → ¬ (m ∣ p)) :
Prime p := by
apply prime_test hp
intro m hm1 hmp
obtain hmT | hmT := lt_or_le m T
· apply H m hm1 hmT
intro h_div
obtain ⟨l, hl⟩ := h_div
have : l ∣ p
· sorry
have hl1 :=
calc m * 1 = m := by ring
_ < p := hmp
_ = m * l := hl
cancel m at hl1
have hl2 : l < T
· sorry
have : ¬ l ∣ p := H l hl1 hl2
contradiction
We record this for future use under the Lean name better_prime_test
.
Here’s an example of how this prime-testing lemma is used. I have left some of the later cases for you to check.
Problem
Show that 79 is prime.
Solution
Clearly
Case 1 (
Case 2 (
Case 3 (
(etc. for 5, 6, 7 and 8)
example : Prime 79 := by
apply better_prime_test (T := 9)
· numbers
· numbers
intro m hm1 hm2
apply Nat.not_dvd_of_exists_lt_and_lt
interval_cases m
· use 39
constructor <;> numbers
· use 26
constructor <;> numbers
· use 19
constructor <;> numbers
· sorry
· sorry
· sorry
· sorry
4.5.10. Exercises
Show that there does not exist a real number
, such that and .example : ¬ (∃ t : ℝ, t ≤ 4 ∧ t ≥ 5) := by sorry
Show that there does not exist a real number
, such that and .example : ¬ (∃ a : ℝ, a ^ 2 ≤ 8 ∧ a ^ 3 ≥ 30) := by sorry
Show that 7 is not even.
example : ¬ Int.Even 7 := by sorry
Let
be an integer satisfying . Show that cannot be both even and a solution to .example {n : ℤ} (hn : n + 3 = 7) : ¬ (Int.Even n ∧ n ^ 2 = 10) := by sorry
Let
be a real number satisfying . Show that cannot be either less than or equal to -3, or greater than or equal to 3.example {x : ℝ} (hx : x ^ 2 < 9) : ¬ (x ≤ -3 ∨ x ≥ 3) := by sorry
Show that there does not exist a natural number
, such that every natural number greater than is even.example : ¬ (∃ N : ℕ, ∀ k > N, Nat.Even k) := by sorry
Let
be an integer. Show that .example (n : ℤ) : ¬(n ^ 2 ≡ 2 [ZMOD 4]) := by sorry
Show that 1 is not prime.
We record this lemma for future use under the name
not_prime_one
.example : ¬ Prime 1 := by sorry
Show that 97 is prime.
example : Prime 97 := by sorry