|
| 1 | +/-! |
| 2 | + # Simp lemmas can match numerals and constant offsets |
| 3 | +-/ |
| 4 | + |
| 5 | +def MyProp (_ : Nat) : Prop := True |
| 6 | + |
| 7 | +variable (n : Nat) |
| 8 | + |
| 9 | +theorem myProp_zero : MyProp 0 = True := rfl |
| 10 | +theorem myProp_add_one : MyProp (n + 1) = True := rfl |
| 11 | +theorem myProp_ofNat : MyProp (OfNat.ofNat n) = True := rfl |
| 12 | +theorem myProp_ofNat_add_one : MyProp (OfNat.ofNat (n + 1)) = True := rfl |
| 13 | + |
| 14 | +/-- Lemmas about a specific numeral match that numeral. -/ |
| 15 | +example : MyProp 0 := by |
| 16 | + dsimp only [myProp_zero] |
| 17 | + |
| 18 | +/-- Lemmas about all numerals match a specific numeral. |
| 19 | +
|
| 20 | + (Regression test for https://github.com/leanprover/lean4/issues/2867)-/ |
| 21 | +example : MyProp 0 := by |
| 22 | + dsimp only [myProp_ofNat] |
| 23 | + |
| 24 | +/-- Lemmas about any natural number plus a constant offset match their left-hand side. -/ |
| 25 | +example : MyProp (n + 1) := by |
| 26 | + dsimp only [myProp_add_one] |
| 27 | + |
| 28 | +/-- Lemmas about any natural number plus a constant offset match a larger offset. -/ |
| 29 | +example : MyProp (n + 2) := by |
| 30 | + dsimp only [myProp_add_one] |
| 31 | + |
| 32 | +/-- Lemmas about any natural number plus a constant offset match the constant offset. -/ |
| 33 | +example : MyProp 1 := by |
| 34 | + dsimp only [myProp_add_one] |
| 35 | + |
| 36 | +/-- Lemmas about any natural number plus a constant offset match constants exceeding the offset. -/ |
| 37 | +example : MyProp 2 := by |
| 38 | + dsimp only [myProp_add_one] |
| 39 | + |
| 40 | +/-- Lemmas about numerals that express a lower-bound |
| 41 | + through a constant offset match their left-hand side.-/ |
| 42 | +example : MyProp (OfNat.ofNat (n + 1)) := by |
| 43 | + dsimp only [myProp_ofNat_add_one] |
| 44 | + |
| 45 | +/-- Lemmas about numerals that express a lower-bound |
| 46 | + through a constant offset match a larger offset.-/ |
| 47 | +example : MyProp (OfNat.ofNat (n + 2)) := by |
| 48 | + dsimp only [myProp_ofNat_add_one] |
| 49 | + |
| 50 | +/-- Lemmas about numerals that express a lower-bound |
| 51 | + through a constant offset match the constant offset.-/ |
| 52 | +example : MyProp 1 := by |
| 53 | + dsimp only [myProp_ofNat_add_one] |
| 54 | + |
| 55 | +/-- Lemmas about numerals that express a lower-bound |
| 56 | + through a constant offset match numerals exceeding the constant offset.-/ |
| 57 | +example : MyProp 2 := by |
| 58 | + dsimp only [myProp_ofNat_add_one] |
| 59 | + |
| 60 | +/-- Lemmas about `0` match `Nat.zero` since `Nat.zero = 0` is in the default simp set.-/ |
| 61 | +example : MyProp Nat.zero := by |
| 62 | + dsimp [myProp_zero] |
| 63 | + |
| 64 | +/-- Lemmas about `0` match `nat_lit 0` since `nat_lit 0 = 0` is in the default simp set.-/ |
| 65 | +example : MyProp (nat_lit 0) := by |
| 66 | + dsimp [myProp_zero] |
| 67 | + |
| 68 | +/-- Lemmas about any natural number plus a constant offset match their LHS in terms of `Nat.succ` |
| 69 | + since `Nat.succ n = n + 1` is in the default simp set. -/ |
| 70 | +example : MyProp (Nat.succ n) := by |
| 71 | + dsimp [myProp_add_one] |
| 72 | + |
| 73 | +/-- If a general lemma about numerals is specialized to a particular one, it still applies. |
| 74 | +
|
| 75 | + This is a tricky case because it results in nested `OfNat.ofNat`. |
| 76 | + For example, `(myProp_ofNat 0 : MyProp (OfNat.ofNat (OfNat.ofNat (nat_lit 0)))`. |
| 77 | +
|
| 78 | + This currently works because we're passing an expression instead of a declaration name, |
| 79 | + so `simp` considers it a local hypothesis and bypasses precise discrimination tree matching. |
| 80 | + -/ |
| 81 | +example : MyProp 0 := by |
| 82 | + dsimp only [myProp_ofNat 0] |
| 83 | + |
| 84 | +/-- If a general lemma about natural numbers plus a constant offset is specialized |
| 85 | + to a constant base, it still applies. |
| 86 | +
|
| 87 | + This is a tricky case because it results in a non-simp-normal lemma statement. |
| 88 | + For example, `(myProp_add_one 0 : MyProp (0 + 1))` can be simplified to `MyProp 1`.-/ |
| 89 | +example : MyProp 1 := by |
| 90 | + dsimp only [myProp_add_one 0] |
| 91 | + |
| 92 | +/-- If a general lemma about numerals that expresses a lower-bound |
| 93 | + through a constant offset is specialized to a constant base, it still applies.-/ |
| 94 | +example : MyProp 1 := by |
| 95 | + dsimp only [myProp_ofNat_add_one 0] |
| 96 | + |
| 97 | +class MyClass (n : Nat) : Prop where |
| 98 | + |
| 99 | +instance myClass_succ [MyClass n] : MyClass (n.succ) := ⟨⟩ |
| 100 | + |
| 101 | +section BaseZero |
| 102 | + |
| 103 | +local instance myClass_nat_zero : MyClass (Nat.zero) := ⟨⟩ |
| 104 | + |
| 105 | +/-- A base instance for `P Nat.zero` and a step instance for `P n → P (n.succ)` can combine to reach any |
| 106 | + sequence of `Nat.zero...succ.succ`. |
| 107 | +
|
| 108 | + Because the kernel has special handling for unifying `Nat.succ`, |
| 109 | + this requires that they can also combine to reach any numeral. |
| 110 | +
|
| 111 | + For example, to reach `P (Nat.zero.succ.succ)`, |
| 112 | + the first recursive step contains the unification problem `P (Nat.zero.succ.succ) =?= P (?n.succ)`, |
| 113 | + which the kernel solves by setting `?n := 1`, *not* `?n := Nat.zero.succ`. |
| 114 | + Thereafter, we need to be able to reach the numeral `1`. -/ |
| 115 | +example : MyClass (Nat.zero.succ.succ) := |
| 116 | + inferInstance |
| 117 | + |
| 118 | +#check show Nat.zero.succ.succ = Nat.succ _ from rfl -- Nat.zero.succ.succ = Nat.succ 1 |
| 119 | + |
| 120 | +example : MyClass 1 := |
| 121 | + inferInstance |
| 122 | + |
| 123 | +example : MyClass 0 := |
| 124 | + inferInstance |
| 125 | + |
| 126 | +end BaseZero |
| 127 | + |
| 128 | +section BaseOne |
| 129 | + |
| 130 | +local instance myClass_succ_nat_zero : MyClass (Nat.zero.succ) := ⟨⟩ |
| 131 | + |
| 132 | +/-- A base instance for `P Nat.zero.succ` |
| 133 | + and a step instance for `P n → P (n.succ)` can combine |
| 134 | + to reach any sequence of `Nat.zero.succ...succ.succ`. |
| 135 | +
|
| 136 | + Because the kernel has special handling for unifying `Nat.succ`, |
| 137 | + this requires that they can also combine to reach any numeral greater than `0`.-/ |
| 138 | +example : MyClass (Nat.zero.succ.succ) := |
| 139 | + inferInstance |
| 140 | + |
| 141 | +example : MyClass 1 := |
| 142 | + inferInstance |
| 143 | + |
| 144 | +end BaseOne |
0 commit comments