|
| 1 | +# Lean 4: Commands and Tactic Proofs |
| 2 | + |
| 3 | +This notebook demonstrates Lean 4's dual nature as both a programming language and a proof assistant. We define data types and functions (commands), then prove properties about them (tactics). |
| 4 | + |
| 5 | +## Defining a binary tree |
| 6 | + |
| 7 | +An inductive type for binary trees with values at the leaves: |
| 8 | + |
| 9 | +```lean4 |
| 10 | +inductive Tree (α : Type) where |
| 11 | + | leaf : α → Tree α |
| 12 | + | node : Tree α → Tree α → Tree α |
| 13 | +deriving Repr |
| 14 | +``` |
| 15 | + |
| 16 | +## Computing with trees |
| 17 | + |
| 18 | +Recursive functions over our tree type — size counts leaves, depth measures the longest path: |
| 19 | + |
| 20 | +```lean4 |
| 21 | +def Tree.size : Tree α → Nat |
| 22 | + | .leaf _ => 1 |
| 23 | + | .node l r => l.size + r.size |
| 24 | +
|
| 25 | +def Tree.depth : Tree α → Nat |
| 26 | + | .leaf _ => 0 |
| 27 | + | .node l r => 1 + max l.depth r.depth |
| 28 | +
|
| 29 | +def Tree.map (f : α → β) : Tree α → Tree β |
| 30 | + | .leaf x => .leaf (f x) |
| 31 | + | .node l r => .node (l.map f) (r.map f) |
| 32 | +
|
| 33 | +-- Test with a small tree |
| 34 | +def exTree := Tree.node (Tree.node (Tree.leaf 1) (Tree.leaf 2)) (Tree.leaf 3) |
| 35 | +
|
| 36 | +#eval exTree.size -- 3 |
| 37 | +#eval exTree.depth -- 2 |
| 38 | +``` |
| 39 | + |
| 40 | +## Proving size is always positive |
| 41 | + |
| 42 | +Every tree has at least one leaf, so size is always at least 1: |
| 43 | + |
| 44 | +```lean4 |
| 45 | +theorem Tree.size_pos (t : Tree α) : t.size ≥ 1 := by |
| 46 | + induction t with |
| 47 | + | leaf _ => simp [Tree.size] |
| 48 | + | node l r ihl ihr => simp [Tree.size]; omega |
| 49 | +``` |
| 50 | + |
| 51 | +## Map preserves structure |
| 52 | + |
| 53 | +Mapping a function over a tree doesn't change its shape: |
| 54 | + |
| 55 | +```lean4 |
| 56 | +theorem Tree.size_map (f : α → β) (t : Tree α) : (t.map f).size = t.size := by |
| 57 | + induction t with |
| 58 | + | leaf _ => simp [Tree.map, Tree.size] |
| 59 | + | node l r ihl ihr => simp [Tree.map, Tree.size, ihl, ihr] |
| 60 | +
|
| 61 | +theorem Tree.depth_map (f : α → β) (t : Tree α) : (t.map f).depth = t.depth := by |
| 62 | + induction t with |
| 63 | + | leaf _ => simp [Tree.map, Tree.depth] |
| 64 | + | node l r ihl ihr => simp [Tree.map, Tree.depth, ihl, ihr] |
| 65 | +``` |
| 66 | + |
| 67 | +## Depth bounds size |
| 68 | + |
| 69 | +The depth of a tree is always less than its size (since depth counts edges on the longest path, while size counts all leaves): |
| 70 | + |
| 71 | +```lean4 |
| 72 | +theorem Tree.depth_lt_size (t : Tree α) : t.depth < t.size := by |
| 73 | + induction t with |
| 74 | + | leaf _ => simp [Tree.size, Tree.depth] |
| 75 | + | node l r ihl ihr => |
| 76 | + simp [Tree.size, Tree.depth] |
| 77 | + omega |
| 78 | +``` |
| 79 | + |
| 80 | +## A verified lookup function |
| 81 | + |
| 82 | +Define a mirroring operation and prove it is involutive (applying it twice gives back the original): |
| 83 | + |
| 84 | +```lean4 |
| 85 | +def Tree.mirror : Tree α → Tree α |
| 86 | + | .leaf x => .leaf x |
| 87 | + | .node l r => .node r.mirror l.mirror |
| 88 | +
|
| 89 | +theorem Tree.mirror_mirror (t : Tree α) : t.mirror.mirror = t := by |
| 90 | + induction t with |
| 91 | + | leaf _ => simp [Tree.mirror] |
| 92 | + | node l r ihl ihr => simp [Tree.mirror, ihl, ihr] |
| 93 | +``` |
| 94 | + |
| 95 | +## Composing proofs |
| 96 | + |
| 97 | +We can combine earlier results. Mirror preserves size because it just swaps children: |
| 98 | + |
| 99 | +```lean4 |
| 100 | +theorem Tree.size_mirror (t : Tree α) : t.mirror.size = t.size := by |
| 101 | + induction t with |
| 102 | + | leaf _ => simp [Tree.mirror, Tree.size] |
| 103 | + | node l r ihl ihr => |
| 104 | + simp [Tree.mirror, Tree.size, ihl, ihr] |
| 105 | + omega |
| 106 | +``` |
0 commit comments