src/HOL/NumberTheory/Fib.thy
changeset 11049 7eef34adb852
parent 10147 178deaacb244
child 11377 0f16ad464c62
     1.1 --- a/src/HOL/NumberTheory/Fib.thy	Sat Feb 03 17:43:34 2001 +0100
     1.2 +++ b/src/HOL/NumberTheory/Fib.thy	Sun Feb 04 19:31:13 2001 +0100
     1.3 @@ -1,17 +1,125 @@
     1.4 -(*  Title:      ex/Fib
     1.5 +(*  Title:      HOL/NumberTheory/Fib.thy
     1.6      ID:         $Id$
     1.7      Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     1.8      Copyright   1997  University of Cambridge
     1.9 -
    1.10 -The Fibonacci function.  Demonstrates the use of recdef.
    1.11  *)
    1.12  
    1.13 -Fib = Primes +
    1.14 +header {* The Fibonacci function *}
    1.15 +
    1.16 +theory Fib = Primes:
    1.17 +
    1.18 +text {*
    1.19 +  Fibonacci numbers: proofs of laws taken from:
    1.20 +  R. L. Graham, D. E. Knuth, O. Patashnik.  Concrete Mathematics.
    1.21 +  (Addison-Wesley, 1989)
    1.22 +
    1.23 +  \bigskip
    1.24 +*}
    1.25 +
    1.26 +consts fib :: "nat => nat"
    1.27 +recdef fib  less_than
    1.28 +  zero: "fib 0 = 0"
    1.29 +  one:  "fib 1 = 1"
    1.30 +  Suc_Suc: "fib (Suc (Suc x)) = fib x + fib (Suc x)"
    1.31 +
    1.32 +text {*
    1.33 +  \medskip The difficulty in these proofs is to ensure that the
    1.34 +  induction hypotheses are applied before the definition of @{term
    1.35 +  fib}.  Towards this end, the @{term fib} equations are not declared
    1.36 +  to the Simplifier and are applied very selectively at first.
    1.37 +*}
    1.38 +
    1.39 +declare fib.Suc_Suc [simp del]
    1.40 +
    1.41 +lemma fib_Suc3: "fib (Suc (Suc (Suc n))) = fib (Suc n) + fib (Suc (Suc n))"
    1.42 +  apply (rule fib.Suc_Suc)
    1.43 +  done
    1.44 +
    1.45 +
    1.46 +text {* \medskip Concrete Mathematics, page 280 *}
    1.47 +
    1.48 +lemma fib_add: "fib (Suc (n + k)) = fib (Suc k) * fib (Suc n) + fib k * fib n"
    1.49 +  apply (induct n rule: fib.induct)
    1.50 +    prefer 3
    1.51 +    txt {* simplify the LHS just enough to apply the induction hypotheses *}
    1.52 +    apply (simp add: fib.Suc_Suc [of "Suc (m + n)", standard])
    1.53 +    apply (simp_all (no_asm_simp) add: fib.Suc_Suc add_mult_distrib add_mult_distrib2)
    1.54 +    done
    1.55 +
    1.56 +lemma fib_Suc_neq_0 [simp]: "fib (Suc n) \<noteq> 0"
    1.57 +  apply (induct n rule: fib.induct)
    1.58 +    apply (simp_all add: fib.Suc_Suc)
    1.59 +  done
    1.60 +
    1.61 +lemma [simp]: "0 < fib (Suc n)"
    1.62 +  apply (simp add: neq0_conv [symmetric])
    1.63 +  done
    1.64 +
    1.65 +lemma fib_gr_0: "0 < n ==> 0 < fib n"
    1.66 +  apply (rule not0_implies_Suc [THEN exE])
    1.67 +   apply auto
    1.68 +  done
    1.69 +
    1.70  
    1.71 -consts fib  :: "nat => nat"
    1.72 -recdef fib "less_than"
    1.73 -  zero    "fib 0 = 0"
    1.74 -  one     "fib 1 = 1"
    1.75 -  Suc_Suc "fib (Suc (Suc x)) = fib x + fib (Suc x)"
    1.76 +text {*
    1.77 +  \medskip Concrete Mathematics, page 278: Cassini's identity.  It is
    1.78 +  much easier to prove using integers!
    1.79 +*}
    1.80 +
    1.81 +lemma fib_Cassini: "int (fib (Suc (Suc n)) * fib n) =
    1.82 +  (if n mod #2 = 0 then int (fib (Suc n) * fib (Suc n)) - #1
    1.83 +   else int (fib (Suc n) * fib (Suc n)) + #1)"
    1.84 +  apply (induct n rule: fib.induct)
    1.85 +    apply (simp add: fib.Suc_Suc)
    1.86 +   apply (simp add: fib.Suc_Suc mod_Suc)
    1.87 +  apply (simp add: fib.Suc_Suc
    1.88 +    add_mult_distrib add_mult_distrib2 mod_Suc zmult_int [symmetric] zmult_ac)
    1.89 +  done
    1.90 +
    1.91 +
    1.92 +text {* \medskip Towards Law 6.111 of Concrete Mathematics *}
    1.93 +
    1.94 +lemma gcd_fib_Suc_eq_1: "gcd (fib n, fib (Suc n)) = 1"
    1.95 +  apply (induct n rule: fib.induct)
    1.96 +    prefer 3
    1.97 +    apply (simp add: gcd_commute fib_Suc3)
    1.98 +   apply (simp_all add: fib.Suc_Suc)
    1.99 +  done
   1.100 +
   1.101 +lemma gcd_fib_add: "gcd (fib m, fib (n + m)) = gcd (fib m, fib n)"
   1.102 +  apply (simp (no_asm) add: gcd_commute [of "fib m"])
   1.103 +  apply (case_tac "m = 0")
   1.104 +   apply simp
   1.105 +  apply (clarify dest!: not0_implies_Suc)
   1.106 +  apply (simp add: fib_add)
   1.107 +  apply (simp add: add_commute gcd_non_0)
   1.108 +  apply (simp add: gcd_non_0 [symmetric])
   1.109 +  apply (simp add: gcd_fib_Suc_eq_1 gcd_mult_cancel)
   1.110 +  done
   1.111 +
   1.112 +lemma gcd_fib_diff: "m \<le> n ==> gcd (fib m, fib (n - m)) = gcd (fib m, fib n)"
   1.113 +  apply (rule gcd_fib_add [symmetric, THEN trans])
   1.114 +  apply simp
   1.115 +  done
   1.116 +
   1.117 +lemma gcd_fib_mod: "0 < m ==> gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
   1.118 +  apply (induct n rule: nat_less_induct)
   1.119 +  apply (subst mod_if)
   1.120 +  apply (simp add: gcd_fib_diff mod_geq not_less_iff_le diff_less)
   1.121 +  done
   1.122 +
   1.123 +lemma fib_gcd: "fib (gcd (m, n)) = gcd (fib m, fib n)"  -- {* Law 6.111 *}
   1.124 +  apply (induct m n rule: gcd_induct)
   1.125 +   apply simp
   1.126 +  apply (simp add: gcd_non_0)
   1.127 +  apply (simp add: gcd_commute gcd_fib_mod)
   1.128 +  done
   1.129 +
   1.130 +lemma fib_mult_eq_setsum:
   1.131 +    "fib (Suc n) * fib n = setsum (\<lambda>k. fib k * fib k) (atMost n)"
   1.132 +  apply (induct n rule: fib.induct)
   1.133 +    apply (auto simp add: atMost_Suc fib.Suc_Suc)
   1.134 +  apply (simp add: add_mult_distrib add_mult_distrib2)
   1.135 +  done
   1.136  
   1.137  end