src/HOL/ex/NatSum.ML
author paulson
Fri, 12 May 2000 15:20:46 +0200
changeset 8869 9ba7ef8a765b
parent 8842 b90d653bd089
child 8932 c1d0f7495714
permissions -rw-r--r--
new simprules needed because of new subtraction rewriting
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];
8869
9ba7ef8a765b new simprules needed because of new subtraction rewriting
paulson
parents: 8842
diff changeset
    16
Addsimps [diff_mult_distrib, diff_mult_distrib2];
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    17
8493
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    18
(*The sum of the first n odd numbers equals n squared.*)
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    19
Goal "sum (%i. Suc(i+i)) n = n*n";
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    20
by (induct_tac "n" 1);
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    21
by Auto_tac;
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    22
qed "sum_of_odds";
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    23
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    24
(*The sum of the first n odd squares*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    25
Goal "#3 * sum (%i. Suc(i+i)*Suc(i+i)) n = n * (#4*n*n - #1)";
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    26
by (induct_tac "n" 1);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    27
(*This removes the -#1 from the inductive step*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    28
by (case_tac "n" 2);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    29
by Auto_tac;
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    30
qed "sum_of_odd_squares";
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    31
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    32
(*The sum of the first n odd cubes*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    33
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
    34
by (induct_tac "n" 1);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    35
(*This removes the -#1 from the inductive step*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    36
by (case_tac "n" 2);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    37
by Auto_tac;
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    38
qed "sum_of_odd_cubes";
8493
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    39
8770
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    40
(*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
    41
Goal "#2 * sum id (Suc n) = n*Suc(n)";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    42
by (induct_tac "n" 1);
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    43
by Auto_tac;
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    44
qed "sum_of_naturals";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    45
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    46
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
    47
by (induct_tac "n" 1);
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    48
by Auto_tac;
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    49
qed "sum_of_squares";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    50
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    51
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
    52
by (induct_tac "n" 1);
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    53
by Auto_tac;
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    54
qed "sum_of_cubes";
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    55
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    56
(** Sum of fourth powers: two versions **)
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    57
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    58
Goal "#30 * sum (%i. i*i*i*i) (Suc n) = \
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    59
\     n * Suc(n) * Suc(#2*n) * (#3*n*n + #3*n - #1)";
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    60
by (induct_tac "n" 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    61
by (Simp_tac 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    62
(*In simplifying we want only the outer Suc argument to be unfolded.
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    63
  Thus the result matches the induction hypothesis (also with Suc). *)
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    64
by (asm_simp_tac (simpset() delsimps [sum_Suc]
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    65
                            addsimps [inst "n" "Suc ?m" sum_Suc]) 1);
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    66
(*Eliminates the subtraction*)
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    67
by (case_tac "n" 1);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    68
by (ALLGOALS Asm_simp_tac);
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    69
qed "sum_of_fourth_powers";
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    70
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    71
(*Alternative proof.  The change of variables n |-> m-#1 eliminates the tricky
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    72
  rewriting of Suc (Suc n).  Because it involves much more subtraction, we
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    73
  switch to the integers. *)
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    74
8837
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    75
Addsimps [zmult_int RS sym, zadd_zmult_distrib, zadd_zmult_distrib2, 
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    76
	  zdiff_zmult_distrib, zdiff_zmult_distrib2];
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    77
8842
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    78
Goal "#30 * int (sum (%i. i*i*i*i) m) = \
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    79
\         int m * (int m - #1) * (int (#2*m) - #1) * \
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    80
\         (int (#3*m*m) - int(#3*m) - #1)";
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    81
by (induct_tac "m" 1);
b90d653bd089 more examples
paulson
parents: 8837
diff changeset
    82
by (ALLGOALS Asm_simp_tac);
8837
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    83
qed "int_sum_of_fourth_powers";