|
7443
|
1 |
(* Title: HOL/Isar_examples/Summation.thy
|
|
|
2 |
ID: $Id$
|
|
|
3 |
Author: Markus Wenzel
|
|
|
4 |
|
|
|
5 |
Summing natural numbers, squares and cubes (see HOL/ex/NatSum for the
|
|
|
6 |
original scripts). Demonstrates mathematical induction together with
|
|
|
7 |
calculational proof.
|
|
|
8 |
*)
|
|
|
9 |
|
|
7748
|
10 |
header {* Summing natural numbers *};
|
|
7443
|
11 |
|
|
|
12 |
theory Summation = Main:;
|
|
|
13 |
|
|
7748
|
14 |
subsection {* A summation operator *};
|
|
7443
|
15 |
|
|
|
16 |
consts
|
|
|
17 |
sum :: "[nat => nat, nat] => nat";
|
|
|
18 |
|
|
|
19 |
primrec
|
|
|
20 |
"sum f 0 = 0"
|
|
|
21 |
"sum f (Suc n) = f n + sum f n";
|
|
|
22 |
|
|
|
23 |
syntax
|
|
|
24 |
"_SUM" :: "idt => nat => nat => nat" ("SUM _ < _. _" [0, 0, 10] 10);
|
|
|
25 |
translations
|
|
|
26 |
"SUM i < k. b" == "sum (%i. b) k";
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
subsection {* Summation laws *};
|
|
|
30 |
|
|
7748
|
31 |
syntax (* FIXME binary arithmetic does not yet work here *)
|
|
7443
|
32 |
"3" :: nat ("3")
|
|
|
33 |
"4" :: nat ("4")
|
|
|
34 |
"6" :: nat ("6");
|
|
|
35 |
|
|
|
36 |
translations
|
|
|
37 |
"3" == "Suc 2"
|
|
|
38 |
"4" == "Suc 3"
|
|
|
39 |
"6" == "Suc (Suc 4)";
|
|
|
40 |
|
|
|
41 |
theorems [simp] = add_mult_distrib add_mult_distrib2 mult_ac;
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
theorem sum_of_naturals: "2 * (SUM i < n + 1. i) = n * (n + 1)"
|
|
7480
|
45 |
(is "?P n" is "?S n = _");
|
|
7443
|
46 |
proof (induct n);
|
|
7480
|
47 |
show "?P 0"; by simp;
|
|
7443
|
48 |
|
|
|
49 |
fix n;
|
|
7480
|
50 |
have "?S (n + 1) = ?S n + 2 * (n + 1)"; by simp;
|
|
|
51 |
also; assume "?S n = n * (n + 1)";
|
|
7443
|
52 |
also; have "... + 2 * (n + 1) = (n + 1) * (n + 2)"; by simp;
|
|
7480
|
53 |
finally; show "?P (Suc n)"; by simp;
|
|
7443
|
54 |
qed;
|
|
|
55 |
|
|
|
56 |
theorem sum_of_odds: "(SUM i < n. 2 * i + 1) = n^2"
|
|
7480
|
57 |
(is "?P n" is "?S n = _");
|
|
7443
|
58 |
proof (induct n);
|
|
7480
|
59 |
show "?P 0"; by simp;
|
|
7443
|
60 |
|
|
|
61 |
fix n;
|
|
7480
|
62 |
have "?S (n + 1) = ?S n + 2 * n + 1"; by simp;
|
|
|
63 |
also; assume "?S n = n^2";
|
|
7443
|
64 |
also; have "... + 2 * n + 1 = (n + 1)^2"; by simp;
|
|
7480
|
65 |
finally; show "?P (Suc n)"; by simp;
|
|
7443
|
66 |
qed;
|
|
|
67 |
|
|
|
68 |
theorem sum_of_squares: "6 * (SUM i < n + 1. i^2) = n * (n + 1) * (2 * n + 1)"
|
|
7480
|
69 |
(is "?P n" is "?S n = _");
|
|
7443
|
70 |
proof (induct n);
|
|
7480
|
71 |
show "?P 0"; by simp;
|
|
7443
|
72 |
|
|
|
73 |
fix n;
|
|
7480
|
74 |
have "?S (n + 1) = ?S n + 6 * (n + 1)^2"; by simp;
|
|
|
75 |
also; assume "?S n = n * (n + 1) * (2 * n + 1)";
|
|
7443
|
76 |
also; have "... + 6 * (n + 1)^2 = (n + 1) * (n + 2) * (2 * (n + 1) + 1)"; by simp;
|
|
7480
|
77 |
finally; show "?P (Suc n)"; by simp;
|
|
7443
|
78 |
qed;
|
|
|
79 |
|
|
|
80 |
theorem sum_of_cubes: "4 * (SUM i < n + 1. i^3) = (n * (n + 1))^2"
|
|
7480
|
81 |
(is "?P n" is "?S n = _");
|
|
7443
|
82 |
proof (induct n);
|
|
7480
|
83 |
show "?P 0"; by simp;
|
|
7443
|
84 |
|
|
|
85 |
fix n;
|
|
7480
|
86 |
have "?S (n + 1) = ?S n + 4 * (n + 1)^3"; by simp;
|
|
|
87 |
also; assume "?S n = (n * (n + 1))^2";
|
|
7443
|
88 |
also; have "... + 4 * (n + 1)^3 = ((n + 1) * ((n + 1) + 1))^2"; by simp;
|
|
7480
|
89 |
finally; show "?P (Suc n)"; by simp;
|
|
7443
|
90 |
qed;
|
|
|
91 |
|
|
|
92 |
end;
|