src/HOL/NumberTheory/Fib.thy
 author haftmann Wed Sep 26 20:27:55 2007 +0200 (2007-09-26) changeset 24728 e2b3a1065676 parent 24573 5bbdc9b60648 child 25222 78943ac46f6d permissions -rw-r--r--
moved Finite_Set before Datatype
1 (*  ID:         \$Id\$
2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
3     Copyright   1997  University of Cambridge
4 *)
6 header {* The Fibonacci function *}
8 theory Fib imports Primes begin
10 text {*
11   Fibonacci numbers: proofs of laws taken from:
12   R. L. Graham, D. E. Knuth, O. Patashnik.  Concrete Mathematics.
15   \bigskip
16 *}
18 fun fib :: "nat \<Rightarrow> nat"
19 where
20          "fib 0 = 0"
21 |        "fib (Suc 0) = 1"
22 | fib_2: "fib (Suc (Suc n)) = fib n + fib (Suc n)"
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 *}
31 text{*We disable @{text fib.fib_2fib_2} for simplification ...*}
32 declare fib_2 [simp del]
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_2)
38 text {* \medskip Concrete Mathematics, page 280 *}
40 lemma fib_add: "fib (Suc (n + k)) = fib (Suc k) * fib (Suc n) + fib k * fib n"
41 proof (induct n rule: fib.induct)
42   case 1 show ?case by simp
43 next
44   case 2 show ?case  by (simp add: fib_2)
45 next
46   case 3 thus ?case by (simp add: fib_2 add_mult_distrib2)
47 qed
49 lemma fib_Suc_neq_0: "fib (Suc n) \<noteq> 0"
50   apply (induct n rule: fib.induct)
51     apply (simp_all add: fib_2)
52   done
54 lemma fib_Suc_gr_0: "0 < fib (Suc n)"
55   by (insert fib_Suc_neq_0 [of n], simp)
57 lemma fib_gr_0: "0 < n ==> 0 < fib n"
58   by (case_tac n, auto simp add: fib_Suc_gr_0)
61 text {*
62   \medskip Concrete Mathematics, page 278: Cassini's identity.  The proof is
63   much easier using integers, not natural numbers!
64 *}
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 proof(induct n rule: fib.induct)
71   case 1 thus ?case by (simp add: fib_2)
72 next
73   case 2 thus ?case by (simp add: fib_2 mod_Suc)
74 next
75   case (3 x)
76   have "Suc 0 \<noteq> x mod 2 \<longrightarrow> x mod 2 = 0" by presburger
77   with "3.hyps" show ?case by (simp add: fib_2 add_mult_distrib add_mult_distrib2)
78 qed
80 text{*We now obtain a version for the natural numbers via the coercion
81    function @{term int}.*}
82 theorem fib_Cassini:
83  "fib (Suc (Suc n)) * fib n =
84   (if n mod 2 = 0 then fib (Suc n) * fib (Suc n) - 1
85    else fib (Suc n) * fib (Suc n) + 1)"
86   apply (rule int_int_eq [THEN iffD1])
87   apply (simp add: fib_Cassini_int)
88   apply (subst zdiff_int [symmetric])
89    apply (insert fib_Suc_gr_0 [of n], simp_all)
90   done
93 text {* \medskip Toward Law 6.111 of Concrete Mathematics *}
95 lemma gcd_fib_Suc_eq_1: "gcd (fib n, fib (Suc n)) = Suc 0"
96   apply (induct n rule: fib.induct)
97     prefer 3
98     apply (simp add: gcd_commute fib_Suc3)
99    apply (simp_all add: fib_2)
100   done
102 lemma gcd_fib_add: "gcd (fib m, fib (n + m)) = gcd (fib m, fib n)"
103   apply (simp add: gcd_commute [of "fib m"])
104   apply (case_tac m)
105    apply simp
107   apply (simp add: add_commute gcd_non_0 [OF fib_Suc_gr_0])
108   apply (simp add: gcd_non_0 [OF fib_Suc_gr_0, symmetric])
109   apply (simp add: gcd_fib_Suc_eq_1 gcd_mult_cancel)
110   done
112 lemma gcd_fib_diff: "m \<le> n ==> gcd (fib m, fib (n - m)) = gcd (fib m, fib n)"
113   by (simp add: gcd_fib_add [symmetric, of _ "n-m"])
115 lemma gcd_fib_mod: "0 < m ==> gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
116 proof (induct n rule: less_induct)
117   case (less n)
118   from less.prems have pos_m: "0 < m" .
119   show "gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
120   proof (cases "m < n")
121     case True note m_n = True
122     then have m_n': "m \<le> n" by auto
123     with pos_m have pos_n: "0 < n" by auto
124     with pos_m m_n have diff: "n - m < n" by auto
125     have "gcd (fib m, fib (n mod m)) = gcd (fib m, fib ((n - m) mod m))"
126     by (simp add: mod_if [of n]) (insert m_n, auto)
127     also have "\<dots> = gcd (fib m, fib (n - m))" by (simp add: less.hyps diff pos_m)
128     also have "\<dots> = gcd (fib m, fib n)" by (simp add: gcd_fib_diff m_n')
129     finally show "gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)" .
130   next
131     case False then show "gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
132     by (cases "m = n") auto
133   qed
134 qed
136 lemma fib_gcd: "fib (gcd (m, n)) = gcd (fib m, fib n)"  -- {* Law 6.111 *}
137   apply (induct m n rule: gcd_induct)
138   apply (simp_all add: gcd_non_0 gcd_commute gcd_fib_mod)
139   done
141 theorem fib_mult_eq_setsum:
142     "fib (Suc n) * fib n = (\<Sum>k \<in> {..n}. fib k * fib k)"
143   apply (induct n rule: fib.induct)
144     apply (auto simp add: atMost_Suc fib_2)