This problem set practises Lecture 11 and
Notebook 11. Part A asks for two macros (Problem 1,
Problem 2). Part B asks for an elaborator that reads the goal and searches the
hypotheses (Problem 3). Part C goes back to proofs: you prove three facts about
binary trees and tag them for simp (Problem 4, Problem 5), so that the
last proof (Problem 6) needs no induction at all.
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/HW05.lean. Each problem below names its declarations
in that file. The file starts with these lines:
import Leanopen Lean Elab Tactic Meta
import Lean loads core Lean together with its metaprogramming library: TacticM, Expr,
getMainGoal and the rest of Lecture 11. The open line lets you write isDefEq instead of
Lean.Meta.isDefEq. The file does not import Mathlib.
Exercises that are programs. In Parts A and B the exercise is a tactic, not a proof, and a
tactic has no sorry to replace. So the file gives each tactic a placeholder: a body that
runs sorry. Here is the idea on a made-up tactic:
The example builds, with the warning declaration uses `sorry` , because the placeholder
proved nothing. Each tactic in the file is followed by test theorems written this way. Replace
the placeholder with your tactic and leave the tests alone. When your tactic is right, the
warnings on its tests disappear. When it is wrong, the tests show errors, and the errors are
your feedback. A test with a sorry warning has not passed. To be sure, run #print axioms
on a test: the list must not contain sorryAx.
Each solution below is collapsed. Open it after you have a tactic or proof of your own, or
after you have been stuck for a while. Many answers are correct, and yours need not match.
Part A: macros
Problem 1 (A first-match arithmetic tactic).
Write a macro arith_close that tries rfl, then decide, then omega, and stops at the
first one that closes the goal. The tests are HW05.arith_test_pow (2 ^ 10 = 1024),
HW05.arith_test_square (∀ n, n < 10 → n * n ≠ 2) and HW05.arith_test_linear
(a + 1 ≤ b from h : a < b).
Hint
first | t₁ | t₂ | t₃ runs t₁; if it fails, it runs t₂ on the original goal; and so on.
A macro’s right-hand side is a tactic quotation `(tactic| …).
Solution
macro "arith_close" : tactic => `(tactic| first | rfl | decide | omega)theorem arith_test_pow : 2 ^ 10 = 1024 := by arith_closetheorem arith_test_square : ∀ n, n < 10 → n * n ≠ 2 := by arith_closetheorem arith_test_linear (a b : Nat) (h : a < b) : a + 1 ≤ b := by arith_close
Each test needs a different alternative. rfl proves 2 ^ 10 = 1024 by computing both
sides. decide proves the second test by checking n = 0, 1, …, 9 one at a time. omega
cannot, because n * n is not linear. The third test has unknowns a and b, so neither
computation works, and omega does it.
When all three fail, first reports the error of the last alternative it tried. Here
n ≤ n * n is true, but decide cannot evaluate it with n unknown, and omega
treats n * n as an unrelated number:
example (n : Nat) : n ≤ n * n := by arith_close
Lean reports omega’s error, “omega could not prove the goal”, with a possible
counterexample. It names a := ↑n and b := ↑n * ↑n and allows a - b ≥ 1: to omega,
n * n is just another unknown, which might be smaller than n. So the order inside
first also decides which error message you see.
Problem 2 (An extensible splitting tactic).
The file declares a tactic split_close with two rules:
Replace the placeholder rule below them with two rules of your own, so that split_close
also splits a goal with constructor and introduces a hypothesis with intro, then
calls itself on every goal that is left. The tests are HW05.split_test_and,
HW05.split_test_imp, HW05.split_test_iff and HW05.split_test_mixed.
Hint
A rule may use split_close on its own right-hand side. t <;> s runs s on every goal
that t produces; (t; s) runs t and then s. Remember that the newest rule is tried
first, and that Lean moves on to an older rule when a newer one fails.
Solution
macro_rules | `(tactic| split_close) => `(tactic| (constructor <;> split_close))macro_rules | `(tactic| split_close) => `(tactic| (intro; split_close))theorem split_test_and (p q : Prop) (hp : p) (hq : q) : (p ∧ q) ∧ (q ∧ p) := by split_closetheorem split_test_imp (p q : Prop) : p → q → q ∧ p := by split_closetheorem split_test_iff (p q : Prop) (hp : p) (hq : q) : p ↔ q := by split_closetheorem split_test_mixed (a : Nat) (p : Prop) : p → (a + 0 = a ∧ True) ∧ p := by split_close
One constructor rule covers ∧, ↔ and True, because each is a structure with a
single constructor (And.intro, Iff.intro, True.intro). On p ↔ q it leaves
p → q and q → p, and the intro rule takes over. A rule that does not apply simply
fails, and Lean tries the next one: intro fails on p, then constructor fails, then
rfl, and assumption finds hp. On these tests every step that succeeds leaves
simpler goals, so the recursion stops. That is not guaranteed on every goal: split_close
is a quick tool, not a decision procedure.
Part B: an elaborator
Problem 3 (Assumption up to symmetry).
Write an elaborator symm_assumption. On a goal a = b, it closes the goal with a
hypothesis h : a = b if there is one, or with h.symm if there is a hypothesis
h : b = a. If the goal is not an equation, it fails with a message that says so. If no
hypothesis fits, it fails with a message that shows the goal. The tests are
HW05.symm_test_flip, HW05.symm_test_arith, HW05.symm_test_skip (which has a
hypothesis that must be skipped) and HW05.symm_test_direct.
Hint
Start from my_assumption in Lecture 11. You need three new pieces:
Expr.eq? : Expr → Option (Expr × Expr × Expr) returns some (type, lhs, rhs) when its
argument is an equation; mkEq rhs lhs builds the Expr of rhs = lhs; and mkEqSymm h
builds the Expr of Eq.symm h. Here is a frame to fill in. The sorry at the end marks
the loop you write:
elab "symm_assumption" : tactic => do let g ← getMainGoal g.withContext do let goal ← instantiateMVars (← g.getType) let some (_, lhs, rhs) := goal.eq? | throwError "symm_assumption: not an equation{indentExpr goal}" -- loop over `← getLCtx` here, as in `my_assumption` sorry
Solution
elab "symm_assumption" : tactic => do let g ← getMainGoal g.withContext do let goal ← instantiateMVars (← g.getType) let some (_, lhs, rhs) := goal.eq? | throwError "symm_assumption: not an equation{indentExpr goal}" let flipped ← mkEq rhs lhs for d in ← getLCtx do if d.isImplementationDetail then continue if ← isDefEq d.type goal then g.assign d.toExpr replaceMainGoal [] return if ← isDefEq d.type flipped then g.assign (← mkEqSymm d.toExpr) replaceMainGoal [] return throwError "symm_assumption: no hypothesis fits{indentExpr goal}"theorem symm_test_flip (a b : Nat) (h : b = a) : a = b := by symm_assumptiontheorem symm_test_arith (x : Nat) (h : 3 = x + 1) : x + 1 = 3 := by symm_assumptiontheorem symm_test_skip (a b c : Nat) (_h1 : a = c) (h2 : b = a) : a = b := by symm_assumptiontheorem symm_test_direct (a : Nat) (h : a = 2) : a = 2 := by symm_assumption#print symm_test_skip
#print shows the term the tactic wrote, fun a b c _h1 h2 ↦ Eq.symm h2: the loop
passed over _h1, whose type a = c matches neither a = b nor b = a. Both error
messages work too:
example (a b : Nat) (h : a ≤ b) : a = b := by symm_assumption
fails with “symm_assumption: no hypothesis fits” followed by the goal a = b, and
example (p : Prop) (hp : p) : p := by symm_assumption
fails with “symm_assumption: not an equation” followed by p. The tactic never builds a
term the kernel could reject: it assigns d.toExpr only after isDefEq has said the
types agree, and mkEqSymm builds a well-typed Eq.symm application by construction.
Part C: a simp set for binary trees
The file defines binary trees with a number at each node, their size, and their mirror
image. The four equations that define size and mirror are already tagged @[simp]:
inductive BinTree where | leaf : BinTree | node : BinTree → Nat → BinTree → BinTreedef BinTree.size : BinTree → Nat | leaf => 0 | node l _ r => l.size + 1 + r.sizedef BinTree.mirror : BinTree → BinTree | leaf => leaf | node l x r => node r.mirror x l.mirror@[simp] theorem BinTree.size_leaf : BinTree.leaf.size = 0 := rfl@[simp] theorem BinTree.size_node (l r : BinTree) (x : Nat) : (BinTree.node l x r).size = l.size + 1 + r.size := rfl@[simp] theorem BinTree.mirror_leaf : BinTree.leaf.mirror = BinTree.leaf := rfl@[simp] theorem BinTree.mirror_node (l r : BinTree) (x : Nat) : (BinTree.node l x r).mirror = BinTree.node r.mirror x l.mirror := rfl
Problem 4 (Mirroring keeps the size).
Prove t.mirror.size = t.size by induction on t (HW05.BinTree.size_mirror). The
statement is tagged @[simp], so later proofs can use it.
Hint
induction t with gives a leaf case and a node l x r ihl ihr case. In the node case,
simp [ihl, ihr] uses the tagged equations and the two induction hypotheses. Look at what
it leaves.
Solution
@[simp] theorem BinTree.size_mirror (t : BinTree) : t.mirror.size = t.size := by induction t with | leaf => simp | node l x r ihl ihr => simp [ihl, ihr] omega
After simp [ihl, ihr] the goal is r.size + 1 + l.size = l.size + 1 + r.size. simp
does not reorder sums, so you need omega (or Nat.add_comm) for the last step.
Problem 5 (Mirroring twice).
Prove t.mirror.mirror = t (HW05.BinTree.mirror_mirror). It is tagged @[simp] as well.
Solution
@[simp] theorem BinTree.mirror_mirror (t : BinTree) : t.mirror.mirror = t := by induction t with | leaf => simp | node l x r ihl ihr => simp [ihl, ihr]
The direction of a simp lemma matters. Read left to right, this one makes terms smaller,
which is what simp needs. The same fact stated as t = t.mirror.mirror would be a bad
simp lemma: its left side matches every tree, and each rewrite makes the term bigger.
Problem 6 (Let simp do it).
Prove (BinTree.node t 5 t.mirror).mirror.size = 2 * t.size + 1 without induction
(HW05.mirror_test). Then replace your simp with the simp only call that simp?
suggests.
Hint
Four simp lemmas apply, one after another: mirror_node, mirror_mirror, size_node and
size_mirror. What is left is arithmetic.
Solution
theorem mirror_test (t : BinTree) : (BinTree.node t 5 t.mirror).mirror.size = 2 * t.size + 1 := by simp omega
simp leaves t.size + 1 + t.size = 2 * t.size + 1, and omega finishes. simp?
reports the lemmas it used:
example (t : BinTree) : (BinTree.node t 5 t.mirror).mirror.size = 2 * t.size + 1 := by simp? omega
After Try this:, Lean suggests simp only [BinTree.mirror_node, BinTree.mirror_mirror, BinTree.size_node, BinTree.size_mirror], the four lemmas of the hint. Two of them are
yours from Problem 4 and Problem 5. While those two still end in sorry,
this proof builds without a warning of its own, yet it rests on unproved lemmas:
#print axioms mirror_test would list sorryAx. In the solution file it lists only
standard axioms:
#print axioms mirror_test
The answer is 'mirror_test' depends on axioms: [propext, Quot.sound], two of the three
standard axioms.