src/HOL/NumberTheory/Fib.thy
author paulson
Fri Mar 05 11:43:55 2004 +0100 (2004-03-05)
changeset 14434 5f14c1207499
parent 11868 56db9f3a6b3e
child 15003 6145dd7538d7
permissions -rw-r--r--
patch to NumberTheory problems caused by Parity
     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: "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   apply (subgoal_tac "x mod 2 < 2", arith)
    78   apply simp
    79   done
    80 
    81 
    82 text {* \medskip Towards Law 6.111 of Concrete Mathematics *}
    83 
    84 lemma gcd_fib_Suc_eq_1: "gcd (fib n, fib (Suc n)) = Suc 0"
    85   apply (induct n rule: fib.induct)
    86     prefer 3
    87     apply (simp add: gcd_commute fib_Suc3)
    88    apply (simp_all add: fib.Suc_Suc)
    89   done
    90 
    91 lemma gcd_fib_add: "gcd (fib m, fib (n + m)) = gcd (fib m, fib n)"
    92   apply (simp (no_asm) add: gcd_commute [of "fib m"])
    93   apply (case_tac "m = 0")
    94    apply simp
    95   apply (clarify dest!: not0_implies_Suc)
    96   apply (simp add: fib_add)
    97   apply (simp add: add_commute gcd_non_0)
    98   apply (simp add: gcd_non_0 [symmetric])
    99   apply (simp add: gcd_fib_Suc_eq_1 gcd_mult_cancel)
   100   done
   101 
   102 lemma gcd_fib_diff: "m \<le> n ==> gcd (fib m, fib (n - m)) = gcd (fib m, fib n)"
   103   apply (rule gcd_fib_add [symmetric, THEN trans])
   104   apply simp
   105   done
   106 
   107 lemma gcd_fib_mod: "0 < m ==> gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
   108   apply (induct n rule: nat_less_induct)
   109   apply (subst mod_if)
   110   apply (simp add: gcd_fib_diff mod_geq not_less_iff_le diff_less)
   111   done
   112 
   113 lemma fib_gcd: "fib (gcd (m, n)) = gcd (fib m, fib n)"  -- {* Law 6.111 *}
   114   apply (induct m n rule: gcd_induct)
   115    apply simp
   116   apply (simp add: gcd_non_0)
   117   apply (simp add: gcd_commute gcd_fib_mod)
   118   done
   119 
   120 lemma fib_mult_eq_setsum:
   121     "fib (Suc n) * fib n = (\<Sum>k \<in> {..n}. fib k * fib k)"
   122   apply (induct n rule: fib.induct)
   123     apply (auto simp add: atMost_Suc fib.Suc_Suc)
   124   apply (simp add: add_mult_distrib add_mult_distrib2)
   125   done
   126 
   127 end