Skip to content

Commit 260746d

Browse files
authored
Merge pull request #1200 from lean-ja/Seasawher/issue1199
`qify` のコード例が不適切
2 parents 1e84178 + 11da526 commit 260746d

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

LeanByExample/Reference/Tactic/Qify.lean

+13-30
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
`qify` は自然数や整数に関する命題を有理数に関する命題にキャストします。
33
-/
44
import Mathlib.Tactic.GCongr
5+
import Mathlib.Tactic.Linarith
56
import Mathlib.Tactic.Qify -- `qify` を使うために必要
6-
import Mathlib.Tactic.Ring
77

8-
variable (a b c x y z : ℕ)
9-
10-
example (h : x ≥ 1) : 2 * x ≥ 2 := by
8+
example (x : Nat) (h : x ≥ 1) : 2 * x ≥ 2 := by
119
-- 自然数から有理数にキャストする
1210
qify at h ⊢
1311

@@ -19,33 +17,18 @@ example (h : x ≥ 1) : 2 * x ≥ 2 := by
1917
(2 : ℚ) = 2 * 1 := by simp
2018
_ ≤ 2 * (x : ℚ) := by gcongr
2119

22-
/- より自然な例として、次の例も挙げておきます。-/
23-
24-
/-- `0` から `n` までの自然数の総和 -/
25-
def sum (n : Nat) : Nat :=
26-
match n with
27-
| 0 => 0
28-
| n + 1 => (n + 1) + sum n
20+
/- ## 用途
2921
30-
@[simp]
31-
theorem sum_zero : sum 0 = 0 := by rfl
32-
33-
/-- 和を計算する多項式。割り算を含むので返り値が `Rat` になっている -/
34-
def sumPoly (n : Nat) : Rat := n * (n + 1) / 2
22+
自然数や整数は体ではないので、体を想定したタクティクが使えないことがあります。
23+
`qify` で有理数にキャストすることで、そのようなタクティクが使えるようになり、証明が楽になることがあります。
24+
-/
3525

36-
/-- `sumPoly` が `sum` と同じ漸化式を満たす -/
37-
theorem sumPoly_succ {n : Nat} : sumPoly (n + 1) = (n + 1) + sumPoly n := by
38-
simp [sumPoly]
39-
ring
26+
example (n m : Nat) (x : Rat) (hx : x > 0) (h : n * m = x) : m > 0 := by
27+
-- 最初は `nlinarith` が使えない
28+
fail_if_success nlinarith
4029

41-
@[simp]
42-
theorem sumPoly_zero : sumPoly 0 = 0 := by
43-
simp [sumPoly]
30+
-- 有理数にキャストする
31+
qify
4432

45-
/-- `sumPoly` の値は常に自然数で、`sum` と一致する -/
46-
theorem sum_eq_sumPoly {n : Nat} : sum n = sumPoly n := by
47-
induction n with
48-
| zero => simp
49-
| succ n ih =>
50-
rw [sum, sumPoly_succ, ← ih]
51-
qify
33+
-- `nlinarith` が使えるようになった!
34+
nlinarith

0 commit comments

Comments
 (0)