src/HOL/ex/NatSum.ML
author paulson
Tue, 09 May 2000 11:29:13 +0200
changeset 8842 b90d653bd089
parent 8837 9ee87bdcba05
child 8869 9ba7ef8a765b
permissions -rw-r--r--
more examples
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1805
10494d0241cd Explicitly included add_mult_distrib & add_mult_distrib2
paulson
parents: 1783
diff changeset
     1
(*  Title:      HOL/ex/NatSum.ML
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     2
    ID:         $Id$
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1266
diff changeset
     3
    Author:     Tobias Nipkow
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     4
    Copyright   1994 TU Muenchen
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     5
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
     6
Summing natural numbers, squares, cubes, etc.
8415
paulson
parents: 8356
diff changeset
     7
paulson
parents: 8356
diff changeset
     8
Originally demonstrated permutative rewriting, but add_ac is no longer needed
paulson
parents: 8356
diff changeset
     9
  thanks to new simprocs.
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    10
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    11
Thanks to Sloane's On-Line Encyclopedia of Integer Sequences,
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    12
  http://www.research.att.com/~njas/sequences/
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    13
*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    14
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    15
Addsimps [add_mult_distrib, add_mult_distrib2];
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    16
8493
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    17
(*The sum of the first n odd numbers equals n squared.*)
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    18
Goal "sum (%i. Suc(i+i)) n = n*n";
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    19
by (induct_tac "n" 1);
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    20
by Auto_tac;
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    21
qed "sum_of_odds";
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    22
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    23
(*The sum of the first n odd squares*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    24
Goal "#3 * sum (%i. Suc(i+i)*Suc(i+i)) n = n * (#4*n*n - #1)";
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    25
by (induct_tac "n" 1);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    26
(*This removes the -#1 from the inductive step*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    27
by (case_tac "n" 2);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    28
by Auto_tac;
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    29
qed "sum_of_odd_squares";
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    30
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    31
(*The sum of the first n odd cubes*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    32
Goal "sum (%i. Suc(i+i)*Suc(i+i)*Suc(i+i)) n = n * n * (#2*n*n - #1)";
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    33
by (induct_tac "n" 1);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    34
(*This removes the -#1 from the inductive step*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    35
by (case_tac "n" 2);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    36
by Auto_tac;
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    37
qed "sum_of_odd_cubes";
8493
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    38
8770
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    39
(*The sum of the first n positive integers equals n(n+1)/2.*)
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    40
Goal "#2 * sum id (Suc n) = n*Suc(n)";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    41
by (induct_tac "n" 1);
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    42
by Auto_tac;
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    43
qed "sum_of_naturals";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    44
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    45
Goal "#6 * sum (%i. i*i) (Suc n) = n * Suc(n) * Suc(#2*n)";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    46
by (induct_tac "n" 1);
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    47
by Auto_tac;
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    48
qed "sum_of_squares";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    49
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    50
Goal "#4 * sum (%i. i*i*i) (Suc n) = n * n * Suc(n) * Suc(n)";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    51
by (induct_tac "n" 1);
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    52
by Auto_tac;
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    53
qed "sum_of_cubes";
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    54
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    55
(** Sum of fourth powers: two versions **)
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    56
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    57
Goal "#30 * sum (%i. i*i*i*i) (Suc n) = \
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    58
\     n * Suc(n) * Suc(#2*n) * (#3*n*n + #3*n - #1)";
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    59
by (induct_tac "n" 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    60
by (Simp_tac 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    61
(*In simplifying we want only the outer Suc argument to be unfolded.
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    62
  Thus the result matches the induction hypothesis (also with Suc). *)
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    63
by (asm_simp_tac (simpset() delsimps [sum_Suc]
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    64
                            addsimps [inst "n" "Suc ?m" sum_Suc]) 1);
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    65
(*Eliminates the subtraction*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    66
by (case_tac "n" 1);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    67
by (ALLGOALS Asm_simp_tac);
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    68
qed "sum_of_fourth_powers";
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    69
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    70
(*Alternative proof.  The change of variables n |-> m-#1 eliminates the tricky
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    71
  rewriting of Suc (Suc n).  Because it involves much more subtraction, we
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    72
  switch to the integers. *)
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    73
8837
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    74
Addsimps [zmult_int RS sym, zadd_zmult_distrib, zadd_zmult_distrib2, 
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    75
	  zdiff_zmult_distrib, zdiff_zmult_distrib2];
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    76
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    77
Goal "#30 * int (sum (%i. i*i*i*i) m) = \
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    78
\         int m * (int m - #1) * (int (#2*m) - #1) * \
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    79
\         (int (#3*m*m) - int(#3*m) - #1)";
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    80
by (induct_tac "m" 1);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    81
by (ALLGOALS Asm_simp_tac);
8837
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    82
qed "int_sum_of_fourth_powers";