This problem set practises steps 2 to 4 of the recipe in
Lecture 5: define, sanity-check, and state faithfully. The
first two parts hand you statements that look right and are not. In each, you first prove
that the statement is wrong (or that it is true for a silly reason), then prove a repaired
version.
In the last part you write a definition yourself and check it before trusting it:
Problem 7, Problem 8, Problem 9.
How to work on it. Open the lean/ folder in VS Code with the Lean 4 extension, then open
LeanFV/Homework/HW02.lean. Each problem names its declaration in that file, such as
HW02.sub_add_bad. Replace each sorry with a proof, or in Problem 7 with a definition.
Before you open a solution, try to say in one sentence why the bad statement is bad: that
sentence is the real answer, and the Lean proof only confirms it.
The file starts with import Mathlib, and so do the solutions below.
import Mathlib
Junk values
Problem 1 (Subtraction that does not undo).
On paper, . Someone formalizes this as
∀ a b : ℕ, a - b + b = a. Prove that this Lean statement is false
(HW02.sub_add_bad).
Hint
In ℕ, a - b is 0 whenever b > a (a junk value). Pick the smallest counterexample,
specialise the hypothesis to it, and let omega see the contradiction.
Solution
theorem sub_add_bad : ¬ ∀ a b : ℕ, a - b + b = a := by intro h have h01 := h 0 1 omega
With , : 0 - 1 = 0 in ℕ, so the left side is 1 and the right side is
0. The paper statement silently assumed , or that we were in .
Problem 2 (Repair it).
Prove the repaired statement a - b + b = a under the hypothesis b ≤ a
(HW02.sub_add_of_le). Before opening the file, write down your own repair. Is adding a
hypothesis the only fix? (Think about which type the numbers could live in instead.)
Solution
theorem sub_add_of_le (a b : ℕ) (h : b ≤ a) : a - b + b = a := by omega
omega decides linear arithmetic over ℕ and ℤ, including truncated subtraction. The
other repair is to state the identity in ℤ, where it holds with no hypothesis. Which repair
is faithful depends on what the paper meant.
Problem 3 (A logarithm law).
The law is formalized as
∀ x y : ℝ, Real.log (x * y) = Real.log x + Real.log y. Prove that this is false
(HW02.log_mul_bad).
Hint
Mathlib defines Real.log 0 = 0 (Lecture 5). Try and , and use
Real.log_pos : 1 < x → 0 < Real.log x.
Solution
theorem log_mul_bad : ¬ ∀ x y : ℝ, Real.log (x * y) = Real.log x + Real.log y := by intro h have h02 := h 0 2 simp at h02 have hpos : 0 < Real.log 2 := Real.log_pos (by norm_num) linarith
After simp, the hypothesis reads 0 = Real.log 2: the left side is log 0 = 0 and the
right side is 0 + log 2. Since , linarith finds the contradiction.
Problem 4 (The faithful version).
Prove the law for positive reals (HW02.log_mul_of_pos). Then look up Mathlib’s own
Real.log_mul with #check. Its hypotheses are weaker than yours. Why is it still true?
Real.log_mul only asks for x ≠ 0 and y ≠ 0, and hx.ne' turns 0 < x into x ≠ 0.
It holds for negative numbers too, because Mathlib defines Real.log x as the logarithm of
when (another junk value, recorded in Real.log_abs). So
Real.log (-2) = Real.log 2. A statement about positive reals is still the faithful one
if that is what the paper meant; the lesson is to know what the library’s version says.
Quantifier order
The homework file gives two definitions of “the sequence is bounded”:
def BoundedBad (f : ℕ → ℝ) : Prop := ∀ n, ∃ M, |f n| ≤ Mdef Bounded (f : ℕ → ℝ) : Prop := ∃ M, ∀ n, |f n| ≤ M
Problem 5 (Every sequence is BoundedBad).
Prove BoundedBad f for everyf (HW02.boundedBad_all). What went wrong in the
definition?
Solution
theorem boundedBad_all (f : ℕ → ℝ) : BoundedBad f := by intro n exact ⟨|f n|, le_refl _⟩
In BoundedBad, the bound M is chosen aftern, so it may depend on n, and
M = |f n| always works. The definition is true of every sequence, so it carries no
information. This is the quantifier-order error of Lecture 5, and a sanity check catches it
at once: a definition of “bounded” must fail for some sequence.
Problem 6 (The identity sequence is not Bounded).
Run that sanity check on the repaired definition: prove that is not Bounded
(HW02.not_bounded_id).
Hint
Given a bound M, exists_nat_gt M gives a natural number n with M < n. For |↑n|,
use abs_of_nonneg and Nat.cast_nonneg.
Solution
theorem not_bounded_id : ¬ Bounded (fun n : ℕ => (n : ℝ)) := by rintro ⟨M, hM⟩ obtain ⟨n, hn⟩ := exists_nat_gt M have h := hM n rw [abs_of_nonneg (Nat.cast_nonneg n)] at h linarith
h says and hn says . linarith combines them.
Your own definition
Problem 7 (Define the maximum of a list).
In HW02.IsMaxOf, replace sorry with a definition of “m is the maximum of the list
xs”, for m : ℚ and xs : List ℚ. The two problems after this one test your definition;
if they cannot be proved, change the definition, not the tests.
Hint
A first attempt is often ∀ x ∈ xs, x ≤ m. Check it against the second half of
Problem 8 before you keep it.
Solution
def IsMaxOf (m : ℚ) (xs : List ℚ) : Prop := m ∈ xs ∧ ∀ x ∈ xs, x ≤ m
The maximum must be an element of the list, and at least as large as every element. The
first attempt in the hint only says that is an upper bound, so it would accept as
the maximum of .
Problem 8 (Sanity checks).
Prove IsMaxOf 4 [1, 4, 2] ∧ ¬ IsMaxOf 5 [1, 4, 2] (HW02.isMaxOf_checks), and that the
empty list has no maximum, ¬ IsMaxOf m [] (HW02.not_isMaxOf_nil).
Hint
Membership in a list of rationals and comparisons of rationals are decidable. After
unfold IsMaxOf, try decide.
Solution
theorem isMaxOf_checks : IsMaxOf 4 [1, 4, 2] ∧ ¬ IsMaxOf 5 [1, 4, 2] := by unfold IsMaxOf decidetheorem not_isMaxOf_nil (m : ℚ) : ¬ IsMaxOf m [] := by rintro ⟨hm, -⟩ simp at hm
decide works here because ℚ is built from integers, so equality and ≤ on ℚ have
algorithms. Over ℝ or ℂ they do not (Lecture 7). The check on the empty list is the one
a sloppy definition fails. With only “upper bound”, every m would be a maximum of []
(vacuous truth). With m = xs.foldr max 0, a definition through a function, 0 would be
the maximum of [] (a junk value).
Problem 9 (The maximum is unique).
State and prove that a list has at most one maximum (HW02.IsMaxOf.unique): if m and
m' are both maxima of xs, then m = m'.
Solution
theorem IsMaxOf.unique {m m' : ℚ} {xs : List ℚ} (h : IsMaxOf m xs) (h' : IsMaxOf m' xs) : m = m' := le_antisymm (h'.2 m h.1) (h.2 m' h'.1)
h.1 : m ∈ xs, so h'.2 m h.1 : m ≤ m'; symmetrically m' ≤ m. This is a first theorem
that uses both halves of the definition, which is another check: if one half were
missing, this proof would fail. Because the name starts with IsMaxOf., you can call it as
h.unique h' (dot notation, as in Mathlib).