How to work on it. Open the lean/ folder (not the course root) in VS Code with the Lean 4
extension, then open LeanFV/Homework/HW01.lean. Each problem below names its theorem in that
file, such as HW01.and_swap. Replace the sorry with a proof, watching the goal in the
infoview as you go. The file is finished when it shows no sorry warnings. You can also work in
a notebook: start it with leanmagic.setup("import LeanFV.Homework.HW01") and copy a statement
into a %%lean cell.
The file imports nothing, so you have core Lean only. That covers every tactic you need: write
exists 3 (or ⟨3, ?_⟩ with refine) where Lecture 3 used Mathlib’s use 3. omega,
decide, simp, grind and exact? are all available. Each solution below is collapsed; open
it after you have a proof of your own, or after you have been stuck for a while. Many proofs are
correct, and yours need not match.
Propositional logic
Problem 1 (Chain two implications).
Prove (p → q) → (q → r) → p → r (HW01.imp_chain).
Solution
theorem imp_chain (p q r : Prop) : (p → q) → (q → r) → p → r := by intro hpq hqr hp exact hqr (hpq hp)
After the three intros the goal is r. hpq hp : q, and hqr turns that into r.
Problem 2 (Swap a conjunction).
Prove p ∧ q → q ∧ p (HW01.and_swap). Take the hypothesis apart instead of using .1
and .2.
Hint
obtain ⟨hp, hq⟩ := h turns h : p ∧ q into two hypotheses.
Solution
theorem and_swap (p q : Prop) : p ∧ q → q ∧ p := by intro h obtain ⟨hp, hq⟩ := h exact ⟨hq, hp⟩
One rcases pattern can take the ∧ apart and split the ∨ at the same time. Then left
or right chooses the side of the goal.
Solution
theorem and_or_split (p q r : Prop) : p ∧ (q ∨ r) → (p ∧ q) ∨ (p ∧ r) := by intro h rcases h with ⟨hp, hq | hr⟩ · left exact ⟨hp, hq⟩ · right exact ⟨hp, hr⟩
Problem 4 (A De Morgan law).
Prove ¬(p ∨ q) → ¬p ∧ ¬q (HW01.not_or_split).
Hint
¬p means p → False, so intro hp works on a goal ¬p. You then need False, and h
gives you False from any proof of p ∨ q.
Solution
theorem not_or_split (p q : Prop) : ¬(p ∨ q) → ¬p ∧ ¬q := by intro h constructor · intro hp exact h (Or.inl hp) · intro hq exact h (Or.inr hq)
The same proof as a term, which you can now read off the tactic proof:
example (p q : Prop) : ¬(p ∨ q) → ¬p ∧ ¬q := fun h => ⟨fun hp => h (Or.inl hp), fun hq => h (Or.inr hq)⟩
Quantifiers
Problem 5 (Split a universal conjunction).
For predicates P Q : α → Prop, prove
(∀ x, P x ∧ Q x) → (∀ x, P x) ∧ (∀ x, Q x) (HW01.forall_and_split).
Solution
theorem forall_and_split {α : Type} (P Q : α → Prop) : (∀ x, P x ∧ Q x) → (∀ x, P x) ∧ (∀ x, Q x) := by intro h constructor · intro x exact (h x).1 · intro x exact (h x).2
h is a function: h x : P x ∧ Q x for each x, and .1 projects out P x.
Problem 6 (An even number above n).
Prove that above every natural number there is an even one:
∀ n : Nat, ∃ m, n < m ∧ m % 2 = 0 (HW01.exists_even_above).
Hint
The witness has to depend on n. After exists, what is left is linear arithmetic with
% 2, which omega handles.
Solution
theorem exists_even_above : ∀ n : Nat, ∃ m, n < m ∧ m % 2 = 0 := by intro n exists 2 * n + 2 omega
After exists 2 * n + 2 the goal is n < 2 * n + 2 ∧ (2 * n + 2) % 2 = 0. If you run
#print axioms on this theorem in the solution file, it lists propext,
Classical.choice and Quot.sound: the proof that omega builds uses classical logic.
These are Lean’s three standard axioms, so the proof is fine; the imp-chain proof, by
contrast, uses no axioms at all.
Equations
Problem 7 (Rewrite, then compute).
From h1 : a = b + 2 and h2 : b = 2 * c, prove a + c = 3 * c + 2
(HW01.rewrite_chain). Use rw for the substitution and something else for the
arithmetic.
Solution
theorem rewrite_chain (a b c : Nat) (h1 : a = b + 2) (h2 : b = 2 * c) : a + c = 3 * c + 2 := by rw [h1, h2] omega
After the rewrites the goal is 2 * c + 2 + c = 3 * c + 2. The order matters:
rw [h2, h1] fails, because the goal contains no b until h1 has replaced a. (omega
alone also proves the original goal, since h1 and h2 are linear.)
A first induction
Problem 8 (Sum of odd numbers).
The homework file defines the sum of the first n odd numbers:
def sumOdd : Nat → Nat | 0 => 0 | n + 1 => sumOdd n + (2 * n + 1)
Prove sumOdd n = n * n (HW01.sum_odd).
Hint
Use induction n with and two cases. In the succ case, simp only [sumOdd, ih] leaves
k * k + (2 * k + 1) = (k + 1) * (k + 1). omega treats k * k and (k + 1) * (k + 1) as
unrelated unknowns, so multiply out first with Nat.mul_add and Nat.add_mul.
Solution
theorem sum_odd (n : Nat) : sumOdd n = n * n := by induction n with | zero => rfl | succ k ih => simp only [sumOdd, ih, Nat.mul_add, Nat.add_mul] omega
grind does the algebra on its own, because it normalises commutative-ring expressions:
example (n : Nat) : sumOdd n = n * n := by induction n with | zero => rfl | succ k ih => simp only [sumOdd, ih] grind