src/HOL/Isar_examples/Summation.thy
author haftmann
Tue, 12 May 2009 21:17:47 +0200
changeset 31129 d2cead76fca2
parent 18193 54419506df9e
child 31758 3edd5f813f01
permissions -rw-r--r--
split Predicate_Compile examples into separate theory
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Isar_examples/Summation.thy
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
     4
*)
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
     5
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
     6
header {* Summing natural numbers *}
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
     7
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
     8
theory Summation
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
     9
imports Main
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
    10
begin
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
    11
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    12
text_raw {*
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    13
 \footnote{This example is somewhat reminiscent of the
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    14
 \url{http://isabelle.in.tum.de/library/HOL/ex/NatSum.html}, which is
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    15
 discussed in \cite{isabelle-ref} in the context of permutative
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    16
 rewrite rules and ordered rewriting.}
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    17
*}
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    18
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    19
text {*
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    20
 Subsequently, we prove some summation laws of natural numbers
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    21
 (including odds, squares, and cubes).  These examples demonstrate how
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    22
 plain natural deduction (including induction) may be combined with
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    23
 calculational proof.
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    24
*}
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    25
7761
7fab9592384f improved presentation;
wenzelm
parents: 7748
diff changeset
    26
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    27
subsection {* Summation laws *}
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
    28
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    29
text {*
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    30
 The sum of natural numbers $0 + \cdots + n$ equals $n \times (n +
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    31
 1)/2$.  Avoiding formal reasoning about division we prove this
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    32
 equation multiplied by $2$.
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    33
*}
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7761
diff changeset
    34
8ee919e42174 improved presentation;
wenzelm
parents: 7761
diff changeset
    35
theorem sum_of_naturals:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
    36
  "2 * (\<Sum>i::nat=0..n. i) = n * (n + 1)"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    37
  (is "?P n" is "?S n = _")
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    38
proof (induct n)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    39
  show "?P 0" by simp
10146
wenzelm
parents: 10007
diff changeset
    40
next
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
    41
  fix n have "?S (n + 1) = ?S n + 2 * (n + 1)" by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    42
  also assume "?S n = n * (n + 1)"
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
    43
  also have "... + 2 * (n + 1) = (n + 1) * (n + 2)" by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    44
  finally show "?P (Suc n)" by simp
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    45
qed
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
    46
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    47
text {*
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    48
 The above proof is a typical instance of mathematical induction.  The
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    49
 main statement is viewed as some $\var{P} \ap n$ that is split by the
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    50
 induction method into base case $\var{P} \ap 0$, and step case
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    51
 $\var{P} \ap n \Impl \var{P} \ap (\idt{Suc} \ap n)$ for arbitrary $n$.
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    52
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    53
 The step case is established by a short calculation in forward
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    54
 manner.  Starting from the left-hand side $\var{S} \ap (n + 1)$ of
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    55
 the thesis, the final result is achieved by transformations involving
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    56
 basic arithmetic reasoning (using the Simplifier).  The main point is
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    57
 where the induction hypothesis $\var{S} \ap n = n \times (n + 1)$ is
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    58
 introduced in order to replace a certain subterm.  So the
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    59
 ``transitivity'' rule involved here is actual \emph{substitution}.
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    60
 Also note how the occurrence of ``\dots'' in the subsequent step
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    61
 documents the position where the right-hand side of the hypothesis
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    62
 got filled in.
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    63
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    64
 \medskip A further notable point here is integration of calculations
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    65
 with plain natural deduction.  This works so well in Isar for two
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    66
 reasons.
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    67
 \begin{enumerate}
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    68
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    69
 \item Facts involved in \isakeyword{also}~/ \isakeyword{finally}
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    70
 calculational chains may be just anything.  There is nothing special
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    71
 about \isakeyword{have}, so the natural deduction element
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    72
 \isakeyword{assume} works just as well.
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    73
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    74
 \item There are two \emph{separate} primitives for building natural
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    75
 deduction contexts: \isakeyword{fix}~$x$ and \isakeyword{assume}~$A$.
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    76
 Thus it is possible to start reasoning with some new ``arbitrary, but
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    77
 fixed'' elements before bringing in the actual assumption.  In
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    78
 contrast, natural deduction is occasionally formalized with basic
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    79
 context elements of the form $x:A$ instead.
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    80
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    81
 \end{enumerate}
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    82
*}
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    83
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    84
text {*
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    85
 \medskip We derive further summation laws for odds, squares, and
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    86
 cubes as follows.  The basic technique of induction plus calculation
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
    87
 is the same as before.
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    88
*}
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
    89
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7761
diff changeset
    90
theorem sum_of_odds:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
    91
  "(\<Sum>i::nat=0..<n. 2 * i + 1) = n^Suc (Suc 0)"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    92
  (is "?P n" is "?S n = _")
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    93
proof (induct n)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    94
  show "?P 0" by simp
10146
wenzelm
parents: 10007
diff changeset
    95
next
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
    96
  fix n have "?S (n + 1) = ?S n + 2 * n + 1" by simp
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 10672
diff changeset
    97
  also assume "?S n = n^Suc (Suc 0)"
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
    98
  also have "... + 2 * n + 1 = (n + 1)^Suc (Suc 0)" by simp
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
    99
  finally show "?P (Suc n)" by simp
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   100
qed
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
   101
8814
0a5edcbe0695 adapted to new arithmetic simprocs;
wenzelm
parents: 8659
diff changeset
   102
text {*
0a5edcbe0695 adapted to new arithmetic simprocs;
wenzelm
parents: 8659
diff changeset
   103
 Subsequently we require some additional tweaking of Isabelle built-in
0a5edcbe0695 adapted to new arithmetic simprocs;
wenzelm
parents: 8659
diff changeset
   104
 arithmetic simplifications, such as bringing in distributivity by
0a5edcbe0695 adapted to new arithmetic simprocs;
wenzelm
parents: 8659
diff changeset
   105
 hand.
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   106
*}
8814
0a5edcbe0695 adapted to new arithmetic simprocs;
wenzelm
parents: 8659
diff changeset
   107
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   108
lemmas distrib = add_mult_distrib add_mult_distrib2
8814
0a5edcbe0695 adapted to new arithmetic simprocs;
wenzelm
parents: 8659
diff changeset
   109
7761
7fab9592384f improved presentation;
wenzelm
parents: 7748
diff changeset
   110
theorem sum_of_squares:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
   111
  "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
   112
  (is "?P n" is "?S n = _")
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   113
proof (induct n)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   114
  show "?P 0" by simp
10146
wenzelm
parents: 10007
diff changeset
   115
next
18193
54419506df9e tuned document;
wenzelm
parents: 15912
diff changeset
   116
  fix n have "?S (n + 1) = ?S n + 6 * (n + 1)^Suc (Suc 0)"
54419506df9e tuned document;
wenzelm
parents: 15912
diff changeset
   117
    by (simp add: distrib)
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   118
  also assume "?S n = n * (n + 1) * (2 * n + 1)"
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   119
  also have "... + 6 * (n + 1)^Suc (Suc 0) =
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   120
    (n + 1) * (n + 2) * (2 * (n + 1) + 1)" by (simp add: distrib)
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   121
  finally show "?P (Suc n)" by simp
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   122
qed
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
   123
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7761
diff changeset
   124
theorem sum_of_cubes:
15561
045a07ac35a7 another reorganization of setsums and intervals
nipkow
parents: 15043
diff changeset
   125
  "4 * (\<Sum>i::nat=0..n. i^3) = (n * (n + 1))^Suc (Suc 0)"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   126
  (is "?P n" is "?S n = _")
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   127
proof (induct n)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   128
  show "?P 0" by (simp add: power_eq_if)
10146
wenzelm
parents: 10007
diff changeset
   129
next
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   130
  fix n have "?S (n + 1) = ?S n + 4 * (n + 1)^3"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   131
    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
   132
  also assume "?S n = (n * (n + 1))^Suc (Suc 0)"
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   133
  also have "... + 4 * (n + 1)^3 = ((n + 1) * ((n + 1) + 1))^Suc (Suc 0)"
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   134
    by (simp add: power_eq_if distrib)
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   135
  finally show "?P (Suc n)" by simp
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   136
qed
7443
e5356e73f57a renamed NatSum to Summation;
wenzelm
parents:
diff changeset
   137
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   138
text {*
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   139
 Comparing these examples with the tactic script version
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   140
 \url{http://isabelle.in.tum.de/library/HOL/ex/NatSum.html}, we note
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
   141
 an important difference of how induction vs.\ simplification is
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   142
 applied.  While \cite[\S10]{isabelle-ref} advises for these examples
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   143
 that ``induction should not be applied until the goal is in the
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   144
 simplest form'' this would be a very bad idea in our setting.
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   145
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   146
 Simplification normalizes all arithmetic expressions involved,
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
   147
 producing huge intermediate goals.  With applying induction
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
   148
 afterwards, the Isar proof text would have to reflect the emerging
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
   149
 configuration by appropriate sub-proofs.  This would result in badly
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
   150
 structured, low-level technical reasoning, without any good idea of
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
   151
 the actual point.
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   152
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   153
 \medskip As a general rule of good proof style, automatic methods
7982
d534b897ce39 improved presentation;
wenzelm
parents: 7968
diff changeset
   154
 such as $\idt{simp}$ or $\idt{auto}$ should normally be never used as
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   155
 initial proof methods, but only as terminal ones, solving certain
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   156
 goals completely.
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   157
*}
7968
964b65b4e433 improved presentation;
wenzelm
parents: 7869
diff changeset
   158
10007
64bf7da1994a isar-strip-terminators;
wenzelm
parents: 9659
diff changeset
   159
end