For checking and comparing
The answer key.
Each file contains the warm-up and stretch solution for one lesson. Other correct proofs are welcome.
01 · Meet your new calculator
def firstTotal : Nat := 3 + 4
#eval firstTotal
def secondTotal : Nat := (3 + 4) * 2
#eval secondTotal
02 · Everything has a type
def aNumber : Nat := 42
#check aNumber
def aMessage : String := "hello"
#check aMessage
03 · Make a function
def triple (n : Nat) : Nat := 3 * n
#eval triple 4
def addThenDouble (n : Nat) : Nat := 2 * (n + 1)
#eval addThenDouble 4
04 · Words and collections
def greeting : String := "Hello" ++ " Lean"
#eval greeting
def trail : List Nat := [1, 2] ++ [3, 4]
#eval trail
05 · Make a decision
def isOdd (n : Nat) : Bool := n % 2 == 1
#eval isOdd 7
def numberLabel (n : Nat) : String := if n % 2 == 0 then "even" else "odd"
#eval numberLabel 8
06 · Your first proof
example : 3 + 3 = 6 := by
rfl
example : (2 + 3) * 4 = 20 := by
rfl
07 · Prove a difference
example : (3 : Nat) ≠ 4 := by
decide
example : (7 : Nat) < 3 * 3 := by
decide
08 · Give a proof a name
theorem three_plus_three : 3 + 3 = 6 := by
rfl
example : 3 + 3 = 6 := by
exact three_plus_three
theorem four_squared : 4 * 4 = 16 := by
rfl
example : 4 * 4 = 16 := by
exact four_squared
09 · Swap equal things
example (a b : Nat) (h : a = b) : a * 2 = b * 2 := by
rw [h]
example (a b c : Nat) (h₁ : a = b) (h₂ : b = c) : a = c := by
rw [h₁, h₂]
10 · Use what you know
example (n : Nat) (h : n = 8) : n = 8 := by
exact h
example (P Q : Prop) (hP : P) (h : P → Q) : Q := by
exact h hP
11 · If this, then that
example (P Q : Prop) : P → Q → P := by
intro hP _hQ
exact hP
example (P Q R : Prop) (f : P → Q) (g : Q → R) : P → R := by
intro h
exact g (f h)
12 · Prove both halves
example (P : Prop) (h : P) : P ∧ P := by
constructor
· exact h
· exact h
example (P Q R : Prop) (hP : P) (hQ : Q) (hR : R) : P ∧ (Q ∧ R) := by
exact ⟨hP, hQ, hR⟩
13 · Unpack an and
example (P Q : Prop) (h : P ∧ Q) : P := by
exact h.left
example (P Q R : Prop) (h : P ∧ (Q ∧ R)) : R := by
exact h.right.right
14 · Choose a side
example (P Q : Prop) (hQ : Q) : P ∨ Q := by
right
exact hQ
example (P Q R : Prop) (h : R) : P ∨ (Q ∨ R) := by
right
right
exact h
15 · Follow both possibilities
example (P : Prop) (h : P ∨ P) : P := by
cases h with
| inl hP => exact hP
| inr hP => exact hP
example (P Q R : Prop) (h : P ∨ Q) (f : P → R) (g : Q → R) : R := by
cases h with
| inl hP => exact f hP
| inr hQ => exact g hQ
16 · What not means
example (P : Prop) (h : False) : P := by
exact False.elim h
example (P Q : Prop) (f : P → Q) (hn : ¬ Q) : ¬ P := by
intro hP
exact hn (f hP)
17 · Proof by contradiction
example (P : Prop) (h : ¬ ¬ P) : P := by
exact Classical.byContradiction h
example (P Q : Prop) (h : ¬ (P ∨ Q)) : ¬ P := by
intro hP
exact h (Or.inl hP)
18 · A two-way promise
example (P : Prop) : P ↔ P := by
constructor
· intro h
exact h
· intro h
exact h
example (P : Prop) : (P ∧ P) ↔ P := by
constructor
· intro h
exact h.left
· intro h
exact ⟨h, h⟩
19 · For every input
example : ∀ n : Nat, n + 0 = n := by
intro n
rfl
example (P : Nat → Prop) (h : ∀ n, P n) : P 2 ∧ P 5 := by
exact ⟨h 2, h 5⟩
20 · Find one witness
example : ∃ n : Nat, n * 2 = 10 := by
exact ⟨5, rfl⟩
example : ∃ n : Nat, n + 1 = 4 ∧ n * 2 = 6 := by
exact ⟨3, rfl, rfl⟩
21 · Numbers from zero and steps
example : Nat.succ (Nat.succ Nat.zero) = 2 := by
rfl
example (n : Nat) : Nat.succ (Nat.succ n) = n + 2 := by
rfl
22 · Your first induction
example (n : Nat) : n + 0 = n := by
rfl
example (n : Nat) : 0 + (n + 1) = n + 1 := by
induction n with
| zero => rfl
| succ n ih =>
change Nat.succ (0 + (n + 1)) = Nat.succ (n + 1)
rw [ih]
23 · Addition in either order
example (n : Nat) : n + 1 = 1 + n := by
exact Nat.add_comm n 1
example (a b c : Nat) : a + (b + c) = b + (a + c) := by
rw [← Nat.add_assoc, Nat.add_comm a b, Nat.add_assoc]
24 · Add a whole staircase
example : 2 * (0 + 1 + 2 + 3 + 4) = 4 * (4 + 1) := by
decide
example (n s : Nat) (h : 2 * s = n * (n + 1)) : 2 * (s + (n + 1)) = (n + 1) * (n + 2) := by
simp only [Nat.mul_add, Nat.add_mul, Nat.mul_one, Nat.one_mul] at h ⊢
omega
25 · Let omega handle the arithmetic
example (n : Nat) (h : n + 2 ≤ 10) : n ≤ 8 := by
omega
example (a b : Nat) (h₁ : a ≤ b) (h₂ : b ≤ a) : a = b := by
omega
26 · A library of mathematics
module
import Mathlib.Tactic.Ring
import Mathlib.Basic.Real.Basic
example (x : ℝ) : (x + 1)^2 = x^2 + 2*x + 1 := by
ring
example (x y : ℝ) : (x + y) * (x - y) = x^2 - y^2 := by
ring
27 · Numbers, checked
module
import Mathlib.Tactic.NormNum
example : (7 : ℤ) * 8 = 56 := by
norm_num
example : (2 : ℚ) / 3 + 1 / 6 = 5 / 6 := by
norm_num
28 · Follow the inequalities
module
import Mathlib.Tactic.Linarith
example (x : ℚ) (h : x + 3 ≤ 8) : x ≤ 5 := by
linarith
example (x y : ℚ) (h₁ : 2*x + y = 8) (h₂ : y = 2) : x = 3 := by
linarith
29 · Write a chain of reasons
module
import Mathlib.Basic.Real.Basic
example (a b : ℝ) (h : a = b) : a + 2 = b + 2 := by
calc
a + 2 = b + 2 := by rw [h]
example (a b c : ℝ) (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c := by
calc
a ≤ b := h₁
_ ≤ c := h₂
30 · A square is never negative
module
import Mathlib.Basic.Real.Basic
import Mathlib.Tactic.Linarith
example (a : ℝ) : 0 ≤ a^2 := by
exact sq_nonneg a
example (a b : ℝ) : 4*a*b ≤ (a + b)^2 := by
have h := sq_nonneg (a - b)
nlinarith
31 · Functions that call themselves
def fact : Nat → Nat
| 0 => 1
| n + 1 => (n + 1) * fact n
example : fact 4 = 24 := by
rfl
def fib : Nat → Nat
| 0 => 0
| 1 => 1
| n + 2 => fib n + fib (n + 1)
example : fib 7 = 13 := by
rfl
32 · Induction on a list
example (ys : List Nat) : ([] ++ ys).length = ys.length := by
rfl
example (x : α) (xs : List α) : (x :: xs).length = xs.length + 1 := by
rfl
33 · Keep related data together
structure Spot where
x : Int
y : Int
example : (Spot.mk 2 3).x = 2 := by
rfl
structure Spot2 where
x : Int
y : Int
def shift (p : Spot2) : Spot2 := ⟨p.x + 1, p.y⟩
example (p : Spot2) : (shift p).y = p.y := by
rfl
34 · Make your own type
inductive Light where
| red | green
deriving DecidableEq
example : Light.red ≠ Light.green := by
decide
inductive Switch where
| off | on
def flipSwitch : Switch → Switch
| .off => .on
| .on => .off
example (s : Switch) : flipSwitch (flipSwitch s) = s := by
cases s <;> rfl
35 · Different inputs stay different
example (a b : Nat) (h : a + 2 = b + 2) : a = b := by
omega
example (a b : Nat) (h : 2*a = 2*b) : a = b := by
omega
36 · Even plus even
module
import Mathlib.Algebra.Ring.Parity
example : Even (12 : ℕ) := by
exact ⟨6, rfl⟩
example (n : ℕ) : Even (n + n) := by
exact ⟨n, rfl⟩
37 · Odd squared stays odd
module
import Mathlib.Algebra.Ring.Parity
import Mathlib.Tactic.Ring
example : Odd (9 : ℕ) := by
exact ⟨4, rfl⟩
example (k : ℕ) : Odd (2*k + 1) := by
exact ⟨k, rfl⟩
38 · Neighbors make an even product
module
import Mathlib.Data.Nat.Basic
example : 2 ∣ (6 : ℕ) * 7 := by
exact ⟨21, rfl⟩
example (n : ℕ) (h : 2 ∣ n) : 2 ∣ n * (n + 1) := by
exact Nat.dvd_mul_right_of_dvd h (n + 1)
39 · Primes never run out
module
import Mathlib.Data.Nat.Prime.Infinite
example : Nat.Prime 7 := by
decide
example (bound : ℕ) : ∃ p, bound + 1 ≤ p ∧ Nat.Prime p := by
exact Nat.exists_infinite_primes (bound + 1)
40 · Capstone: reverse, then reverse again
def rev : List Nat → List Nat
| [] => []
| x :: xs => rev xs ++ [x]
example : rev [1, 2, 3] = [3, 2, 1] := by
rfl
def rev2 : List α → List α
| [] => []
| x :: xs => rev2 xs ++ [x]
example (x : α) : rev2 [x] = [x] := by
rfl
41 · Reason from a missing badge
example (P Q : Prop) (rule : P → Q) (hp : P) : Q := by
exact rule hp
example (P Q R : Prop) (f : P → Q) (g : Q → R) (hr : ¬ R) : ¬ P := by
intro hp
exact hr (g (f hp))
42 · Rule out one possibility
example (P Q : Prop) (options : P ∨ Q) (hq : ¬ Q) : P := by
cases options with
| inl p => exact p
| inr q => exact False.elim (hq q)
example (P Q R : Prop) (options : P ∨ (Q ∨ R)) (hp : ¬ P) (hq : ¬ Q) : R := by
cases options with
| inl p => exact False.elim (hp p)
| inr rest =>
cases rest with
| inl q => exact False.elim (hq q)
| inr r => exact r
43 · Keep a clue in both cases
example (P Q R : Prop) (p : P) (r : R) : (P ∧ Q) ∨ (P ∧ R) := by
exact Or.inr ⟨p, r⟩
example (P Q R : Prop) (h : (P ∧ Q) ∨ (P ∧ R)) : P ∧ (Q ∨ R) := by
cases h with
| inl pq => exact ⟨pq.1, Or.inl pq.2⟩
| inr pr => exact ⟨pr.1, Or.inr pr.2⟩
44 · Connect two two-way rules
example (P Q : Prop) (a : P ↔ Q) (q : Q) : P := by
exact a.mpr q
example (P Q R : Prop) (a : Q ↔ P) (b : Q ↔ R) : P ↔ R := by
constructor
· intro p
exact b.mp (a.mpr p)
· intro r
exact a.mp (b.mpr r)
45 · Test a claim with one witness
example (Pass : Nat → Prop) (fails : ¬ Pass 7) : ¬ (∀ n, Pass n) := by
intro allPass
exact fails (allPass 7)
example (P Q : Nat → Prop) (clue : ∃ n, P n ∧ ¬ Q n) : ¬ (∀ n, P n → Q n) := by
intro rule
obtain ⟨n, p, nq⟩ := clue
exact nq (rule n p)
46 · Flip a switch twice
example : (!true) = false := by
rfl
example (b : Bool) : (!(!(!b))) = (!b) := by
cases b <;> rfl
47 · Swap a pair and swap back
example (x : Nat) (y : Bool) : (x, y).swap = (y, x) := by
rfl
example (p : α × β) : (p.swap.swap.swap) = p.swap := by
cases p
rfl
48 · Change the values, keep the count
example : ([1, 2, 3].map (fun n => n + 10)).length = 3 := by
rfl
example (f : α → β) (g : β → γ) (xs : List α) : ((xs.map f).map g).length = xs.length := by
rw [List.length_map, List.length_map]
49 · Combine two passes through a list
example : ([1, 2].map (fun n => n + 1)).map (fun n => n * 2) = [4, 6] := by
rfl
example (f : α → β) (xs ys : List α) : (xs ++ ys).map f = xs.map f ++ ys.map f := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [List.cons_append, List.map_cons, ih]
50 · Capstone: combine two scoreboards
def points : List Nat → Nat
| [] => 0
| x :: xs => x + points xs
example : points [2, 0, 5] = 7 := by
rfl
example (xs : List Nat) (bonus : Nat) : points (xs ++ [bonus]) = points xs + bonus := by
induction xs with
| nil => simp [points]
| cons x xs ih => simp only [List.cons_append, points, ih, Nat.add_assoc]