src/HOL/NumberTheory/Fib.thy
author wenzelm
Sun Feb 04 19:31:13 2001 +0100 (2001-02-04)
changeset 11049 7eef34adb852
parent 10147 178deaacb244
child 11377 0f16ad464c62
permissions -rw-r--r--
HOL-NumberTheory: converted to new-style format and proper document setup;
     1 (*  Title:      HOL/NumberTheory/Fib.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1997  University of Cambridge
     5 *)
     6 
     7 header {* The Fibonacci function *}
     8 
     9 theory Fib = Primes:
    10 
    11 text {*
    12   Fibonacci numbers: proofs of laws taken from:
    13   R. L. Graham, D. E. Knuth, O. Patashnik.  Concrete Mathematics.
    14   (Addison-Wesley, 1989)
    15 
    16   \bigskip
    17 *}
    18 
    19 consts fib :: "nat => nat"
    20 recdef fib  less_than
    21   zero: "fib 0 = 0"
    22   one:  "fib 1 = 1"
    23   Suc_Suc: "fib (Suc (Suc x)) = fib x + fib (Suc x)"
    24 
    25 text {*
    26   \medskip The difficulty in these proofs is to ensure that the
    27   induction hypotheses are applied before the definition of @{term
    28   fib}.  Towards this end, the @{term fib} equations are not declared
    29   to the Simplifier and are applied very selectively at first.
    30 *}
    31 
    32 declare fib.Suc_Suc [simp del]
    33 
    34 lemma fib_Suc3: "fib (Suc (Suc (Suc n))) = fib (Suc n) + fib (Suc (Suc n))"
    35   apply (rule fib.Suc_Suc)
    36   done
    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.Suc_Suc [of "Suc (m + n)", standard])
    46     apply (simp_all (no_asm_simp) add: fib.Suc_Suc add_mult_distrib add_mult_distrib2)
    47     done
    48 
    49 lemma fib_Suc_neq_0 [simp]: "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 [simp]: "0 < fib (Suc n)"
    55   apply (simp add: neq0_conv [symmetric])
    56   done
    57 
    58 lemma fib_gr_0: "0 < n ==> 0 < fib n"
    59   apply (rule not0_implies_Suc [THEN exE])
    60    apply auto
    61   done
    62 
    63 
    64 text {*
    65   \medskip Concrete Mathematics, page 278: Cassini's identity.  It is
    66   much easier to prove using integers!
    67 *}
    68 
    69 lemma fib_Cassini: "int (fib (Suc (Suc n)) * fib n) =
    70   (if n mod #2 = 0 then int (fib (Suc n) * fib (Suc n)) - #1
    71    else int (fib (Suc n) * fib (Suc n)) + #1)"
    72   apply (induct n rule: fib.induct)
    73     apply (simp add: fib.Suc_Suc)
    74    apply (simp add: fib.Suc_Suc mod_Suc)
    75   apply (simp add: fib.Suc_Suc
    76     add_mult_distrib add_mult_distrib2 mod_Suc zmult_int [symmetric] zmult_ac)
    77   done
    78 
    79 
    80 text {* \medskip Towards Law 6.111 of Concrete Mathematics *}
    81 
    82 lemma gcd_fib_Suc_eq_1: "gcd (fib n, fib (Suc n)) = 1"
    83   apply (induct n rule: fib.induct)
    84     prefer 3
    85     apply (simp add: gcd_commute fib_Suc3)
    86    apply (simp_all add: fib.Suc_Suc)
    87   done
    88 
    89 lemma gcd_fib_add: "gcd (fib m, fib (n + m)) = gcd (fib m, fib n)"
    90   apply (simp (no_asm) add: gcd_commute [of "fib m"])
    91   apply (case_tac "m = 0")
    92    apply simp
    93   apply (clarify dest!: not0_implies_Suc)
    94   apply (simp add: fib_add)
    95   apply (simp add: add_commute gcd_non_0)
    96   apply (simp add: gcd_non_0 [symmetric])
    97   apply (simp add: gcd_fib_Suc_eq_1 gcd_mult_cancel)
    98   done
    99 
   100 lemma gcd_fib_diff: "m \<le> n ==> gcd (fib m, fib (n - m)) = gcd (fib m, fib n)"
   101   apply (rule gcd_fib_add [symmetric, THEN trans])
   102   apply simp
   103   done
   104 
   105 lemma gcd_fib_mod: "0 < m ==> gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
   106   apply (induct n rule: nat_less_induct)
   107   apply (subst mod_if)
   108   apply (simp add: gcd_fib_diff mod_geq not_less_iff_le diff_less)
   109   done
   110 
   111 lemma fib_gcd: "fib (gcd (m, n)) = gcd (fib m, fib n)"  -- {* Law 6.111 *}
   112   apply (induct m n rule: gcd_induct)
   113    apply simp
   114   apply (simp add: gcd_non_0)
   115   apply (simp add: gcd_commute gcd_fib_mod)
   116   done
   117 
   118 lemma fib_mult_eq_setsum:
   119     "fib (Suc n) * fib n = setsum (\<lambda>k. fib k * fib k) (atMost n)"
   120   apply (induct n rule: fib.induct)
   121     apply (auto simp add: atMost_Suc fib.Suc_Suc)
   122   apply (simp add: add_mult_distrib add_mult_distrib2)
   123   done
   124 
   125 end