src/HOL/Isar_Examples/Summation.thy
author wenzelm
Sat, 26 Dec 2015 15:44:14 +0100
changeset 61932 2e48182cc82c
parent 61541 846c72206207
child 63583 a39baba12732
permissions -rw-r--r--
misc tuning and modernization;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33026
8f35633c4922 modernized session Isar_Examples;
wenzelm
parents: 31758
diff changeset
     1
(*  Title:      HOL/Isar_Examples/Summation.thy
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
     2
    Author:     Makarius
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
     3
*)
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
     4
58882
6e2010ab8bd9 modernized header;
wenzelm
parents: 58614
diff changeset
     5
section \<open>Summing natural numbers\<close>
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
     6
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
     7
theory Summation
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
     8
imports Main
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
     9
begin
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
    10
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    11
text \<open>
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    12
  Subsequently, we prove some summation laws of natural numbers (including
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    13
  odds, squares, and cubes). These examples demonstrate how plain natural
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    14
  deduction (including induction) may be combined with calculational proof.
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    15
\<close>
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    16
7761
7fab9592384f improved presentation;
wenzelm
parents: 7748
diff changeset
    17
58614
7338eb25226c more cartouches;
wenzelm
parents: 55640
diff changeset
    18
subsection \<open>Summation laws\<close>
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
    19
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    20
text \<open>
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    21
  The sum of natural numbers \<open>0 + \<cdots> + n\<close> equals \<open>n \<times> (n + 1)/2\<close>. Avoiding
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    22
  formal reasoning about division we prove this equation multiplied by \<open>2\<close>.
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    23
\<close>
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7761
diff changeset
    24
8ee919e42174 improved presentation;
wenzelm
parents: 7761
diff changeset
    25
theorem sum_of_naturals:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
    26
  "2 * (\<Sum>i::nat=0..n. i) = n * (n + 1)"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    27
  (is "?P n" is "?S n = _")
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    28
proof (induct n)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    29
  show "?P 0" by simp
10146
wenzelm
parents: 10007
diff changeset
    30
next
55640
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    31
  fix n have "?S (n + 1) = ?S n + 2 * (n + 1)"
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    32
    by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    33
  also assume "?S n = n * (n + 1)"
55640
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    34
  also have "\<dots> + 2 * (n + 1) = (n + 1) * (n + 2)"
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    35
    by simp
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    36
  finally show "?P (Suc n)"
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    37
    by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    38
qed
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
    39
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    40
text \<open>
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    41
  The above proof is a typical instance of mathematical induction. The main
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    42
  statement is viewed as some \<open>?P n\<close> that is split by the induction method
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    43
  into base case \<open>?P 0\<close>, and step case \<open>?P n \<Longrightarrow> ?P (Suc n)\<close> for arbitrary \<open>n\<close>.
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    44
61541
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    45
  The step case is established by a short calculation in forward manner.
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    46
  Starting from the left-hand side \<open>?S (n + 1)\<close> of the thesis, the final
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    47
  result is achieved by transformations involving basic arithmetic reasoning
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    48
  (using the Simplifier). The main point is where the induction hypothesis \<open>?S
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    49
  n = n \<times> (n + 1)\<close> is introduced in order to replace a certain subterm. So the
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    50
  ``transitivity'' rule involved here is actual \<^emph>\<open>substitution\<close>. Also note how
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    51
  the occurrence of ``\dots'' in the subsequent step documents the position
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    52
  where the right-hand side of the hypothesis got filled in.
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    53
61541
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    54
  \<^medskip>
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    55
  A further notable point here is integration of calculations with plain
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    56
  natural deduction. This works so well in Isar for two reasons.
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    57
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    58
    \<^enum> Facts involved in \<^theory_text>\<open>also\<close>~/ \<^theory_text>\<open>finally\<close> calculational chains may be just
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    59
    anything. There is nothing special about \<^theory_text>\<open>have\<close>, so the natural deduction
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    60
    element \<^theory_text>\<open>assume\<close> works just as well.
61541
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    61
  
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    62
    \<^enum> There are two \<^emph>\<open>separate\<close> primitives for building natural deduction
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    63
    contexts: \<^theory_text>\<open>fix x\<close> and \<^theory_text>\<open>assume A\<close>. Thus it is possible to start reasoning
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    64
    with some new ``arbitrary, but fixed'' elements before bringing in the
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    65
    actual assumption. In contrast, natural deduction is occasionally
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    66
    formalized with basic context elements of the form \<open>x:A\<close> instead.
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    67
61541
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    68
  \<^medskip>
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
    69
  We derive further summation laws for odds, squares, and cubes as follows.
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    70
  The basic technique of induction plus calculation is the same as before.
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    71
\<close>
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    72
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7761
diff changeset
    73
theorem sum_of_odds:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
    74
  "(\<Sum>i::nat=0..<n. 2 * i + 1) = n^Suc (Suc 0)"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    75
  (is "?P n" is "?S n = _")
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    76
proof (induct n)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    77
  show "?P 0" by simp
10146
wenzelm
parents: 10007
diff changeset
    78
next
55640
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    79
  fix n
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    80
  have "?S (n + 1) = ?S n + 2 * n + 1"
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    81
    by simp
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 10672
diff changeset
    82
  also assume "?S n = n^Suc (Suc 0)"
55640
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    83
  also have "\<dots> + 2 * n + 1 = (n + 1)^Suc (Suc 0)"
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    84
    by simp
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    85
  finally show "?P (Suc n)"
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
    86
    by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    87
qed
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
    88
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    89
text \<open>
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    90
  Subsequently we require some additional tweaking of Isabelle built-in
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    91
  arithmetic simplifications, such as bringing in distributivity by hand.
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
    92
\<close>
8814
0a5edcbe0695 adapted to new arithmetic simprocs;
wenzelm
parents: 8659
diff changeset
    93
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    94
lemmas distrib = add_mult_distrib add_mult_distrib2
8814
0a5edcbe0695 adapted to new arithmetic simprocs;
wenzelm
parents: 8659
diff changeset
    95
7761
7fab9592384f improved presentation;
wenzelm
parents: 7748
diff changeset
    96
theorem sum_of_squares:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
    97
  "6 * (\<Sum>i::nat=0..n. i^Suc (Suc 0)) = n * (n + 1) * (2 * n + 1)"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    98
  (is "?P n" is "?S n = _")
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    99
proof (induct n)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   100
  show "?P 0" by simp
10146
wenzelm
parents: 10007
diff changeset
   101
next
55640
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   102
  fix n
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   103
  have "?S (n + 1) = ?S n + 6 * (n + 1)^Suc (Suc 0)"
18193
54419506df9e tuned document;
wenzelm
parents: 15912
diff changeset
   104
    by (simp add: distrib)
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   105
  also assume "?S n = n * (n + 1) * (2 * n + 1)"
55640
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   106
  also have "\<dots> + 6 * (n + 1)^Suc (Suc 0) =
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   107
      (n + 1) * (n + 2) * (2 * (n + 1) + 1)"
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   108
    by (simp add: distrib)
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   109
  finally show "?P (Suc n)"
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   110
    by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   111
qed
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
   112
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7761
diff changeset
   113
theorem sum_of_cubes:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
   114
  "4 * (\<Sum>i::nat=0..n. i^3) = (n * (n + 1))^Suc (Suc 0)"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   115
  (is "?P n" is "?S n = _")
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   116
proof (induct n)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   117
  show "?P 0" by (simp add: power_eq_if)
10146
wenzelm
parents: 10007
diff changeset
   118
next
55640
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   119
  fix n
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   120
  have "?S (n + 1) = ?S n + 4 * (n + 1)^3"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   121
    by (simp add: power_eq_if distrib)
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 10672
diff changeset
   122
  also assume "?S n = (n * (n + 1))^Suc (Suc 0)"
55640
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   123
  also have "\<dots> + 4 * (n + 1)^3 = ((n + 1) * ((n + 1) + 1))^Suc (Suc 0)"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   124
    by (simp add: power_eq_if distrib)
55640
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   125
  finally show "?P (Suc n)"
abc140f21caa tuned proofs;
wenzelm
parents: 50086
diff changeset
   126
    by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   127
qed
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
   128
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   129
text \<open>
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   130
  Note that in contrast to older traditions of tactical proof scripts, the
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   131
  structured proof applies induction on the original, unsimplified statement.
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   132
  This allows to state the induction cases robustly and conveniently.
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   133
  Simplification (or other automated) methods are then applied in terminal
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   134
  position to solve certain sub-problems completely.
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   135
61541
846c72206207 tuned document;
wenzelm
parents: 58882
diff changeset
   136
  As a general rule of good proof style, automatic methods such as \<open>simp\<close> or
61932
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   137
  \<open>auto\<close> should normally be never used as initial proof methods with a nested
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   138
  sub-proof to address the automatically produced situation, but only as
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   139
  terminal ones to solve sub-problems.
2e48182cc82c misc tuning and modernization;
wenzelm
parents: 61541
diff changeset
   140
\<close>
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   141
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   142
end