|
| 1 | +import Mathlib.Data.Nat.Basic |
| 2 | + |
| 3 | +/- |
| 4 | +## |
| 5 | +## I. defining what an "even" number is |
| 6 | +## |
| 7 | +-/ |
| 8 | + |
| 9 | +-- we define a new `inductive` type called `Even` |
| 10 | +-- an inductive type allows us to define a data type by specifying its constructors |
| 11 | +-- here, we say that a number `n` is `Even` if: |
| 12 | +-- 1. `Even.zero` : 0 is even |
| 13 | +-- 2. `Even.add_two` : if `k` is even, then `k + 2` is also even |
| 14 | +inductive Even : Nat → Prop |
| 15 | + | zero : Even 0 |
| 16 | + | add_two {k : Nat} (hk : Even k) : Even (k + 2) |
| 17 | + |
| 18 | + |
| 19 | +/- |
| 20 | +## |
| 21 | +## II. proving a simple property about even numbers |
| 22 | +## |
| 23 | +-/ |
| 24 | + |
| 25 | +-- our goal is to prove the following theorem: |
| 26 | +-- if a number `n` is even, then `n + 2` is also even |
| 27 | +theorem even_add_two_is_even {n : Nat} (hn : Even n) : Even (n + 2) := by |
| 28 | + -- we have `hn : Even n` in our context |
| 29 | + -- our goal is `Even (n + 2)` |
| 30 | + -- since `Even` is an inductive type, we can use `induction` on `hn`. |
| 31 | + -- this means we'll prove the statement for each constructor of `Even` |
| 32 | + induction hn with |
| 33 | + | zero => |
| 34 | + -- case 1: `n` is `0`. |
| 35 | + -- our goal is now `Even (0 + 2)`, which simplifies to `Even 2` |
| 36 | + -- we can prove this directly using the `Even.add_two` constructor |
| 37 | + -- we need to show that `Even 0` holds, which is true by `Even.zero` |
| 38 | + apply Even.add_two |
| 39 | + apply Even.zero |
| 40 | + | add_two k hk ih => |
| 41 | + -- case 2: `n` is of the form `k + 2`, where `hk : Even k` |
| 42 | + -- and `ih` is our inductive hypothesis: `Even (k + 2)` implies `Even ((k + 2) + 2)` |
| 43 | + -- the inductive hypothesis `ih` is actually `Even (k + 2)` implies `Even (k + 2 + 2)` |
| 44 | + -- `ih` will be a proof of our goal for `k` so `ih : Even (k + 2)` |
| 45 | + -- our goal is `Even ((k + 2) + 2)` |
| 46 | + -- we can use `Even.add_two` again |
| 47 | + -- to apply `Even.add_two`, we need a proof that `Even (k + 2)` holds (our inductive hypothesis `ih`) |
| 48 | + apply Even.add_two |
| 49 | + assumption -- this tactic tries to find a proof of the current goal among the hypotheses |
| 50 | + -- in this case, `ih` is exactly `Even (k + 2)` |
| 51 | + |
| 52 | + |
| 53 | +/- |
| 54 | +## |
| 55 | +## III. using the theorem |
| 56 | +## |
| 57 | +-/ |
| 58 | + |
| 59 | +example : Even 4 := by |
| 60 | + -- we know 0 is even |
| 61 | + have h0 : Even 0 := Even.zero |
| 62 | + -- then 2 is even (0 + 2) |
| 63 | + have h2 : Even 2 := Even.add_two h0 |
| 64 | + -- then 4 is even (2 + 2) |
| 65 | + exact Even.add_two h2 |
| 66 | + |
| 67 | + |
| 68 | +-- another way to prove `Even 4` using our theorem `even_add_two_is_even` |
| 69 | +example : Even 4 := by |
| 70 | + -- we know `Even 0`. |
| 71 | + have h0 : Even 0 := Even.zero |
| 72 | + -- apply `even_add_two_is_even` to `h0` to get `Even (0 + 2)`, i.e., `Even 2` |
| 73 | + have h2 : Even 2 := even_add_two_is_even h0 |
| 74 | + -- apply `even_add_two_is_even` to `h2` to get `Even (2 + 2)`, i.e., `Even 4` |
| 75 | + exact even_add_two_is_even h2 |
| 76 | + |
| 77 | + |
| 78 | +/- |
| 79 | +## |
| 80 | +## IV. a slightly more complex definition and proof |
| 81 | +## |
| 82 | +-/ |
| 83 | + |
| 84 | +-- let's define addition for natural numbers |
| 85 | +-- let's assume `add_zero` and `add_succ` are axioms for now |
| 86 | +-- let's define what it means for a number to be "odd": |
| 87 | +-- a number `n` is `Odd` if `n = k + 1` for some even `k` |
| 88 | +def Odd (n : Nat) : Prop := ∃ k, Even k ∧ n = k + 1 |
| 89 | + |
| 90 | +-- prove that if `n` is odd, then `n + 2` is also odd |
| 91 | +theorem odd_add_two_is_odd {n : Nat} (hn : Odd n) : Odd (n + 2) := by |
| 92 | + -- our hypothesis `hn` is `Odd n`, which means `∃ k, wven k ∧ n = k + 1` |
| 93 | + -- we can `rcases` this hypothesis to get `k` and its properties |
| 94 | + rcases hn with ⟨k, hk_even, hk_n⟩ |
| 95 | + -- `k : Nat` |
| 96 | + -- `hk_even : Even k` |
| 97 | + -- `hk_n : n = k + 1` |
| 98 | + |
| 99 | + -- our goal is `Odd (n + 2)` |
| 100 | + -- by definition, this means `∃ m, Even m ∧ (n + 2) = m + 1` |
| 101 | + |
| 102 | + -- let's substitute `n = k + 1` into the goal |
| 103 | + -- the goal becomes `Odd ((k + 1) + 2)`, which simplifies to `Odd (k + 3)` |
| 104 | + -- we need to find an `m` such that `Even m` and `k + 3 = m + 1` |
| 105 | + -- if we choose `m = k + 2`, then `k + 3 = (k + 2) + 1` is true |
| 106 | + -- so we need to show `Even (k + 2)` |
| 107 | + |
| 108 | + -- we know `hk_even : Even k` |
| 109 | + -- we can use our previously proven theorem `even_add_two_is_even` to show `Even (k + 2)` |
| 110 | + have hk_plus_2_even : Even (k + 2) := even_add_two_is_even hk_even |
| 111 | + |
| 112 | + -- now we have `hk_plus_2_even : Even (k + 2)` |
| 113 | + -- we want to show `∃ m, Even m ∧ (n + 2) = m + 1` |
| 114 | + -- let `m := k + 2` |
| 115 | + -- we use `exists.intro` to provide the witness `k + 2` |
| 116 | + exists (k + 2) |
| 117 | + -- now our goal is `Even (k + 2) ∧ (n + 2) = (k + 2) + 1` |
| 118 | + -- we can `split` the goal into two subgoals |
| 119 | + constructor |
| 120 | + -- subgoal 1: `Even (k + 2)` |
| 121 | + exact hk_plus_2_even |
| 122 | + -- subgoal 2: `(n + 2) = (k + 2) + 1` |
| 123 | + -- we know `n = k + 1`, lets rewrite: |
| 124 | + rw [hk_n] |
| 125 | + -- goal becomes `(k + 1) + 2 = (k + 2) + 1` |
| 126 | + -- this is true by associativity and commutativity of addition |
| 127 | + -- lean's simplifier `simp` can usually handle such arithmetic equalities |
| 128 | + simp |
0 commit comments