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 Lean
 
open 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:

macro "placeholder" : tactic => `(tactic| sorry)
 
example : 1 + 1 = 2 := by placeholder

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

Problem 2 (An extensible splitting tactic).

The file declares a tactic split_close with two rules:

syntax "split_close" : tactic
macro_rules | `(tactic| split_close) => `(tactic| assumption)
macro_rules | `(tactic| split_close) => `(tactic| rfl)

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.

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.

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 → BinTree
 
def BinTree.size : BinTree → Nat
  | leaf => 0
  | node l _ r => l.size + 1 + r.size
 
def 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.

Problem 5 (Mirroring twice).

Prove t.mirror.mirror = t (HW05.BinTree.mirror_mirror). It is tagged @[simp] as well.

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.

Cheatsheet cards for this homework: writing your own tactic and simp and simp lemmas.

Back to Lecture 11 · Notebook 11