src/HOL/Isar_examples/Summation.thy
changeset 7761 7fab9592384f
parent 7748 5b9c45b21782
child 7800 8ee919e42174
equal deleted inserted replaced
7760:43f8d28dbc6e 7761:7fab9592384f
     9 
     9 
    10 header {* Summing natural numbers *};
    10 header {* Summing natural numbers *};
    11 
    11 
    12 theory Summation = Main:;
    12 theory Summation = Main:;
    13 
    13 
       
    14 
    14 subsection {* A summation operator *};
    15 subsection {* A summation operator *};
    15 
    16 
    16 consts
    17 consts
    17   sum   :: "[nat => nat, nat] => nat";
    18   sum   :: "[nat => nat, nat] => nat";
    18 
    19 
    19 primrec
    20 primrec
    20   "sum f 0 = 0"
    21   "sum f 0 = 0"
    21   "sum f (Suc n) = f n + sum f n";
    22   "sum f (Suc n) = f n + sum f n";
    22 
    23 
    23 syntax
    24 syntax
    24   "_SUM" :: "idt => nat => nat => nat"       ("SUM _ < _. _" [0, 0, 10] 10);
    25   "_SUM" :: "idt => nat => nat => nat"  ("SUM _ < _. _" [0, 0, 10] 10);
    25 translations
    26 translations
    26   "SUM i < k. b" == "sum (%i. b) k";
    27   "SUM i < k. b" == "sum (%i. b) k";
    27 
    28 
    28 
    29 
    29 subsection {* Summation laws *};
    30 subsection {* Summation laws *};
    30 
    31 
    31 syntax				(* FIXME binary arithmetic does not yet work here *)
    32 (* FIXME binary arithmetic does not yet work here *)
       
    33 
       
    34 syntax
    32   "3" :: nat  ("3")
    35   "3" :: nat  ("3")
    33   "4" :: nat  ("4")
    36   "4" :: nat  ("4")
    34   "6" :: nat  ("6");
    37   "6" :: nat  ("6");
    35 
    38 
    36 translations
    39 translations
    63   also; assume "?S n = n^2";
    66   also; assume "?S n = n^2";
    64   also; have "... + 2 * n + 1 = (n + 1)^2"; by simp;
    67   also; have "... + 2 * n + 1 = (n + 1)^2"; by simp;
    65   finally; show "?P (Suc n)"; by simp;
    68   finally; show "?P (Suc n)"; by simp;
    66 qed;
    69 qed;
    67 
    70 
    68 theorem sum_of_squares: "6 * (SUM i < n + 1. i^2) = n * (n + 1) * (2 * n + 1)"
    71 theorem sum_of_squares:
       
    72   "6 * (SUM i < n + 1. i^2) = n * (n + 1) * (2 * n + 1)"
    69   (is "?P n" is "?S n = _");
    73   (is "?P n" is "?S n = _");
    70 proof (induct n);
    74 proof (induct n);
    71   show "?P 0"; by simp;
    75   show "?P 0"; by simp;
    72 
    76 
    73   fix n;
    77   fix n;
    74   have "?S (n + 1) = ?S n + 6 * (n + 1)^2"; by simp;
    78   have "?S (n + 1) = ?S n + 6 * (n + 1)^2"; by simp;
    75   also; assume "?S n = n * (n + 1) * (2 * n + 1)";
    79   also; assume "?S n = n * (n + 1) * (2 * n + 1)";
    76   also; have "... + 6 * (n + 1)^2 = (n + 1) * (n + 2) * (2 * (n + 1) + 1)"; by simp;
    80   also; have "... + 6 * (n + 1)^2 = (n + 1) * (n + 2) * (2 * (n + 1) + 1)";
       
    81     by simp;
    77   finally; show "?P (Suc n)"; by simp;
    82   finally; show "?P (Suc n)"; by simp;
    78 qed;
    83 qed;
    79 
    84 
    80 theorem sum_of_cubes: "4 * (SUM i < n + 1. i^3) = (n * (n + 1))^2"
    85 theorem sum_of_cubes: "4 * (SUM i < n + 1. i^3) = (n * (n + 1))^2"
    81   (is "?P n" is "?S n = _");
    86   (is "?P n" is "?S n = _");
    83   show "?P 0"; by simp;
    88   show "?P 0"; by simp;
    84 
    89 
    85   fix n;
    90   fix n;
    86   have "?S (n + 1) = ?S n + 4 * (n + 1)^3"; by simp;
    91   have "?S (n + 1) = ?S n + 4 * (n + 1)^3"; by simp;
    87   also; assume "?S n = (n * (n + 1))^2";
    92   also; assume "?S n = (n * (n + 1))^2";
    88   also; have "... + 4 * (n + 1)^3 = ((n + 1) * ((n + 1) + 1))^2"; by simp;
    93   also; have "... + 4 * (n + 1)^3 = ((n + 1) * ((n + 1) + 1))^2";
       
    94     by simp;
    89   finally; show "?P (Suc n)"; by simp;
    95   finally; show "?P (Suc n)"; by simp;
    90 qed;
    96 qed;
    91 
    97 
    92 end;
    98 end;