src/HOL/ex/NatSum.ML
author paulson
Mon, 08 May 2000 18:20:04 +0200
changeset 8837 9ee87bdcba05
parent 8836 32fe62227ff0
child 8842 b90d653bd089
permissions -rw-r--r--
yet another example
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.
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    10
*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    11
8493
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    12
(*The sum of the first n odd numbers equals n squared.*)
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    13
Goal "sum (%i. Suc(i+i)) n = n*n";
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    14
by (induct_tac "n" 1);
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    15
by Auto_tac;
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    16
qed "sum_of_odds";
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    17
8493
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    18
Addsimps [add_mult_distrib, add_mult_distrib2];
60c2f892b1d9 re-ordered the theorems
paulson
parents: 8415
diff changeset
    19
8770
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    20
(*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
    21
Goal "#2 * sum id (Suc n) = n*Suc(n)";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    22
by (induct_tac "n" 1);
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    23
by Auto_tac;
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    24
qed "sum_of_naturals";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    25
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    26
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
    27
by (induct_tac "n" 1);
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    28
by Auto_tac;
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    29
qed "sum_of_squares";
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    30
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    31
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
    32
by (induct_tac "n" 1);
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    33
by Auto_tac;
bfab4d4b7516 new, but still slow, proofs using binary numerals
paulson
parents: 8493
diff changeset
    34
qed "sum_of_cubes";
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    35
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    36
(** Sum of fourth powers requires lemmas **)
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    37
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    38
Goal "[| #1 <= (j::nat); k <= m |] ==> k <= j*m";
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    39
by (dtac mult_le_mono 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    40
by Auto_tac;
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    41
qed "mult_le_1_mono";
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    42
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    43
Addsimps [diff_mult_distrib, diff_mult_distrib2];
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    44
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    45
(*Thanks to Sloane's On-Line Encyclopedia of Integer Sequences,
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    46
  http://www.research.att.com/~njas/sequences/*)
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    47
Goal "#30 * sum (%i. i*i*i*i) (Suc n) = \
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    48
\     n * Suc(n) * Suc(#2*n) * (#3*n*n + #3*n - #1)";
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    49
by (induct_tac "n" 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    50
by (Simp_tac 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    51
(*Automating this inequality proof would make the proof trivial*)
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    52
by (subgoal_tac "n <= #10 * (n * (n * n)) + (#15 * (n * (n * (n * n))) + \
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    53
\                     #6 * (n * (n * (n * (n * n)))))" 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    54
(*In simplifying we want only the outer Suc argument to be unfolded.
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    55
  Thus the result matches the induction hypothesis (also with Suc). *)
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    56
by (asm_simp_tac (simpset() delsimps [sum_Suc]
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    57
                            addsimps [inst "n" "Suc ?m" sum_Suc]) 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    58
by (rtac ([mult_le_1_mono, le_add1] MRS le_trans) 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    59
by (rtac le_cube 2);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    60
by (Simp_tac 1);
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    61
qed "sum_of_fourth_powers";
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    62
8837
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    63
(** Alternative proof, avoiding the need for inequality reasoning **)
8836
32fe62227ff0 new example
paulson
parents: 8780
diff changeset
    64
8837
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    65
Addsimps [zmult_int RS sym, zadd_zmult_distrib, zadd_zmult_distrib2, 
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    66
	  zdiff_zmult_distrib, zdiff_zmult_distrib2];
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    67
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    68
Goal "#30 * int (sum (%i. i*i*i*i) (Suc n)) = \
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    69
\     int n * int(Suc n) * int (Suc(#2*n)) * (int (#3*n*n) + int (#3*n) - #1)";
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    70
by (induct_tac "n" 1);
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    71
by (Simp_tac 1);
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    72
by (asm_simp_tac (simpset() delsimps [sum_Suc]
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    73
                            addsimps [inst "n" "Suc ?m" sum_Suc]) 1);
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    74
qed "int_sum_of_fourth_powers";
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    75
9ee87bdcba05 yet another example
paulson
parents: 8836
diff changeset
    76