src/HOL/NumberTheory/Fib.thy
author nipkow
Wed Aug 18 11:09:40 2004 +0200 (2004-08-18)
changeset 15140 322485b816ac
parent 15003 6145dd7538d7
child 15439 71c0f98e31f1
permissions -rw-r--r--
import -> imports
     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 (Suc 0) = Suc 0"
    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:
    70  "int (fib (Suc (Suc n)) * fib n) =
    71   (if n mod 2 = 0 then int (fib (Suc n) * fib (Suc n)) - 1
    72    else int (fib (Suc n) * fib (Suc n)) + 1)"
    73   apply (induct n rule: fib.induct)
    74     apply (simp add: fib.Suc_Suc)
    75    apply (simp add: fib.Suc_Suc mod_Suc)
    76   apply (simp add: fib.Suc_Suc
    77     add_mult_distrib add_mult_distrib2 mod_Suc zmult_int [symmetric] zmult_ac)
    78   apply (subgoal_tac "x mod 2 < 2", arith)
    79   apply simp
    80   done
    81 
    82 
    83 text {* \medskip Towards Law 6.111 of Concrete Mathematics *}
    84 
    85 lemma gcd_fib_Suc_eq_1: "gcd (fib n, fib (Suc n)) = Suc 0"
    86   apply (induct n rule: fib.induct)
    87     prefer 3
    88     apply (simp add: gcd_commute fib_Suc3)
    89    apply (simp_all add: fib.Suc_Suc)
    90   done
    91 
    92 lemma gcd_fib_add: "gcd (fib m, fib (n + m)) = gcd (fib m, fib n)"
    93   apply (simp (no_asm) add: gcd_commute [of "fib m"])
    94   apply (case_tac "m = 0")
    95    apply simp
    96   apply (clarify dest!: not0_implies_Suc)
    97   apply (simp add: fib_add)
    98   apply (simp add: add_commute gcd_non_0)
    99   apply (simp add: gcd_non_0 [symmetric])
   100   apply (simp add: gcd_fib_Suc_eq_1 gcd_mult_cancel)
   101   done
   102 
   103 lemma gcd_fib_diff: "m \<le> n ==> gcd (fib m, fib (n - m)) = gcd (fib m, fib n)"
   104   apply (rule gcd_fib_add [symmetric, THEN trans])
   105   apply simp
   106   done
   107 
   108 lemma gcd_fib_mod: "0 < m ==> gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
   109   apply (induct n rule: nat_less_induct)
   110   apply (subst mod_if)
   111   apply (simp add: gcd_fib_diff mod_geq not_less_iff_le diff_less)
   112   done
   113 
   114 lemma fib_gcd: "fib (gcd (m, n)) = gcd (fib m, fib n)"  -- {* Law 6.111 *}
   115   apply (induct m n rule: gcd_induct)
   116    apply simp
   117   apply (simp add: gcd_non_0)
   118   apply (simp add: gcd_commute gcd_fib_mod)
   119   done
   120 
   121 lemma fib_mult_eq_setsum:
   122     "fib (Suc n) * fib n = (\<Sum>k \<in> {..n}. fib k * fib k)"
   123   apply (induct n rule: fib.induct)
   124     apply (auto simp add: atMost_Suc fib.Suc_Suc)
   125   apply (simp add: add_mult_distrib add_mult_distrib2)
   126   done
   127 
   128 end