src/HOL/NumberTheory/Fib.thy
 author paulson Fri Mar 25 16:20:57 2005 +0100 (2005-03-25) changeset 15628 9f912f8fd2df parent 15439 71c0f98e31f1 child 16417 9bc16273c2d4 permissions -rw-r--r--
tidied up
```     1 (*  ID:         \$Id\$
```
```     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
```
```     3     Copyright   1997  University of Cambridge
```
```     4 *)
```
```     5
```
```     6 header {* The Fibonacci function *}
```
```     7
```
```     8 theory Fib = Primes:
```
```     9
```
```    10 text {*
```
```    11   Fibonacci numbers: proofs of laws taken from:
```
```    12   R. L. Graham, D. E. Knuth, O. Patashnik.  Concrete Mathematics.
```
```    13   (Addison-Wesley, 1989)
```
```    14
```
```    15   \bigskip
```
```    16 *}
```
```    17
```
```    18 consts fib :: "nat => nat"
```
```    19 recdef fib  "measure (\<lambda>x. x)"
```
```    20    zero:    "fib 0  = 0"
```
```    21    one:     "fib (Suc 0) = Suc 0"
```
```    22    Suc_Suc: "fib (Suc (Suc x)) = fib x + fib (Suc x)"
```
```    23
```
```    24 text {*
```
```    25   \medskip The difficulty in these proofs is to ensure that the
```
```    26   induction hypotheses are applied before the definition of @{term
```
```    27   fib}.  Towards this end, the @{term fib} equations are not declared
```
```    28   to the Simplifier and are applied very selectively at first.
```
```    29 *}
```
```    30
```
```    31 text{*We disable @{text fib.Suc_Suc} for simplification ...*}
```
```    32 declare fib.Suc_Suc [simp del]
```
```    33
```
```    34 text{*...then prove a version that has a more restrictive pattern.*}
```
```    35 lemma fib_Suc3: "fib (Suc (Suc (Suc n))) = fib (Suc n) + fib (Suc (Suc n))"
```
```    36   by (rule fib.Suc_Suc)
```
```    37
```
```    38
```
```    39 text {* \medskip Concrete Mathematics, page 280 *}
```
```    40
```
```    41 lemma fib_add: "fib (Suc (n + k)) = fib (Suc k) * fib (Suc n) + fib k * fib n"
```
```    42   apply (induct n rule: fib.induct)
```
```    43     prefer 3
```
```    44     txt {* simplify the LHS just enough to apply the induction hypotheses *}
```
```    45     apply (simp add: fib_Suc3)
```
```    46     apply (simp_all add: fib.Suc_Suc add_mult_distrib2)
```
```    47     done
```
```    48
```
```    49 lemma fib_Suc_neq_0: "fib (Suc n) \<noteq> 0"
```
```    50   apply (induct n rule: fib.induct)
```
```    51     apply (simp_all add: fib.Suc_Suc)
```
```    52   done
```
```    53
```
```    54 lemma fib_Suc_gr_0: "0 < fib (Suc n)"
```
```    55   by (insert fib_Suc_neq_0 [of n], simp)
```
```    56
```
```    57 lemma fib_gr_0: "0 < n ==> 0 < fib n"
```
```    58   by (case_tac n, auto simp add: fib_Suc_gr_0)
```
```    59
```
```    60
```
```    61 text {*
```
```    62   \medskip Concrete Mathematics, page 278: Cassini's identity.  The proof is
```
```    63   much easier using integers, not natural numbers!
```
```    64 *}
```
```    65
```
```    66 lemma fib_Cassini_int:
```
```    67  "int (fib (Suc (Suc n)) * fib n) =
```
```    68   (if n mod 2 = 0 then int (fib (Suc n) * fib (Suc n)) - 1
```
```    69    else int (fib (Suc n) * fib (Suc n)) + 1)"
```
```    70   apply (induct n rule: fib.induct)
```
```    71     apply (simp add: fib.Suc_Suc)
```
```    72    apply (simp add: fib.Suc_Suc mod_Suc)
```
```    73   apply (simp add: fib.Suc_Suc add_mult_distrib add_mult_distrib2
```
```    74                    mod_Suc zmult_int [symmetric])
```
```    75   apply presburger
```
```    76   done
```
```    77
```
```    78 text{*We now obtain a version for the natural numbers via the coercion
```
```    79    function @{term int}.*}
```
```    80 theorem fib_Cassini:
```
```    81  "fib (Suc (Suc n)) * fib n =
```
```    82   (if n mod 2 = 0 then fib (Suc n) * fib (Suc n) - 1
```
```    83    else fib (Suc n) * fib (Suc n) + 1)"
```
```    84   apply (rule int_int_eq [THEN iffD1])
```
```    85   apply (simp add: fib_Cassini_int)
```
```    86   apply (subst zdiff_int [symmetric])
```
```    87    apply (insert fib_Suc_gr_0 [of n], simp_all)
```
```    88   done
```
```    89
```
```    90
```
```    91 text {* \medskip Toward Law 6.111 of Concrete Mathematics *}
```
```    92
```
```    93 lemma gcd_fib_Suc_eq_1: "gcd (fib n, fib (Suc n)) = Suc 0"
```
```    94   apply (induct n rule: fib.induct)
```
```    95     prefer 3
```
```    96     apply (simp add: gcd_commute fib_Suc3)
```
```    97    apply (simp_all add: fib.Suc_Suc)
```
```    98   done
```
```    99
```
```   100 lemma gcd_fib_add: "gcd (fib m, fib (n + m)) = gcd (fib m, fib n)"
```
```   101   apply (simp add: gcd_commute [of "fib m"])
```
```   102   apply (case_tac m)
```
```   103    apply simp
```
```   104   apply (simp add: fib_add)
```
```   105   apply (simp add: add_commute gcd_non_0 [OF fib_Suc_gr_0])
```
```   106   apply (simp add: gcd_non_0 [OF fib_Suc_gr_0, symmetric])
```
```   107   apply (simp add: gcd_fib_Suc_eq_1 gcd_mult_cancel)
```
```   108   done
```
```   109
```
```   110 lemma gcd_fib_diff: "m \<le> n ==> gcd (fib m, fib (n - m)) = gcd (fib m, fib n)"
```
```   111   by (simp add: gcd_fib_add [symmetric, of _ "n-m"])
```
```   112
```
```   113 lemma gcd_fib_mod: "0 < m ==> gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
```
```   114   apply (induct n rule: nat_less_induct)
```
```   115   apply (simp add: mod_if gcd_fib_diff mod_geq)
```
```   116   done
```
```   117
```
```   118 lemma fib_gcd: "fib (gcd (m, n)) = gcd (fib m, fib n)"  -- {* Law 6.111 *}
```
```   119   apply (induct m n rule: gcd_induct)
```
```   120   apply (simp_all add: gcd_non_0 gcd_commute gcd_fib_mod)
```
```   121   done
```
```   122
```
```   123 theorem fib_mult_eq_setsum:
```
```   124     "fib (Suc n) * fib n = (\<Sum>k \<in> {..n}. fib k * fib k)"
```
```   125   apply (induct n rule: fib.induct)
```
```   126     apply (auto simp add: atMost_Suc fib.Suc_Suc)
```
```   127   apply (simp add: add_mult_distrib add_mult_distrib2)
```
```   128   done
```
```   129
```
```   130 end
```