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.

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).

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.)

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).

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?

Quantifier order

The homework file gives two definitions of “the sequence is bounded”:

def BoundedBad (f : ℕ → ℝ) : Prop := ∀ n, ∃ M, |f n| ≤ M
 
def Bounded (f : ℕ → ℝ) : Prop := ∃ M, ∀ n, |f n| ≤ M
Problem 5 (Every sequence is BoundedBad).

Prove BoundedBad f for every f (HW02.boundedBad_all). What went wrong in the definition?

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).

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.

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).

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'.