author | haftmann |
Mon, 29 Sep 2008 12:31:58 +0200 | |
changeset 28401 | d5f39173444c |
parent 27556 | 292098f2efdf |
child 31758 | 3edd5f813f01 |
permissions | -rw-r--r-- |
8051 | 1 |
(* Title: HOL/Isar_examples/Fibonacci.thy |
2 |
ID: $Id$ |
|
3 |
Author: Gertrud Bauer |
|
4 |
Copyright 1999 Technische Universitaet Muenchen |
|
5 |
||
6 |
The Fibonacci function. Demonstrates the use of recdef. Original |
|
7 |
tactic script by Lawrence C Paulson. |
|
8 |
||
9 |
Fibonacci numbers: proofs of laws taken from |
|
10 |
||
11 |
R. L. Graham, D. E. Knuth, O. Patashnik. |
|
12 |
Concrete Mathematics. |
|
13 |
(Addison-Wesley, 1989) |
|
14 |
*) |
|
15 |
||
10007 | 16 |
header {* Fib and Gcd commute *} |
8051 | 17 |
|
27366 | 18 |
theory Fibonacci |
19 |
imports Primes |
|
20 |
begin |
|
8051 | 21 |
|
22 |
text_raw {* |
|
23 |
\footnote{Isar version by Gertrud Bauer. Original tactic script by |
|
8052 | 24 |
Larry Paulson. A few proofs of laws taken from |
8051 | 25 |
\cite{Concrete-Math}.} |
10007 | 26 |
*} |
8051 | 27 |
|
28 |
||
10007 | 29 |
subsection {* Fibonacci numbers *} |
8051 | 30 |
|
27366 | 31 |
fun fib :: "nat \<Rightarrow> nat" where |
18153 | 32 |
"fib 0 = 0" |
27366 | 33 |
| "fib (Suc 0) = 1" |
34 |
| "fib (Suc (Suc x)) = fib x + fib (Suc x)" |
|
8051 | 35 |
|
10007 | 36 |
lemma [simp]: "0 < fib (Suc n)" |
18153 | 37 |
by (induct n rule: fib.induct) simp_all |
8051 | 38 |
|
39 |
||
10007 | 40 |
text {* Alternative induction rule. *} |
8051 | 41 |
|
8304 | 42 |
theorem fib_induct: |
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
43 |
"P 0 ==> P 1 ==> (!!n. P (n + 1) ==> P n ==> P (n + 2)) ==> P (n::nat)" |
18153 | 44 |
by (induct rule: fib.induct) simp_all |
8051 | 45 |
|
46 |
||
10007 | 47 |
subsection {* Fib and gcd commute *} |
8051 | 48 |
|
10007 | 49 |
text {* A few laws taken from \cite{Concrete-Math}. *} |
8051 | 50 |
|
9659 | 51 |
lemma fib_add: |
8051 | 52 |
"fib (n + k + 1) = fib (k + 1) * fib (n + 1) + fib k * fib n" |
9659 | 53 |
(is "?P n") |
10007 | 54 |
-- {* see \cite[page 280]{Concrete-Math} *} |
11809 | 55 |
proof (induct n rule: fib_induct) |
10007 | 56 |
show "?P 0" by simp |
57 |
show "?P 1" by simp |
|
58 |
fix n |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
59 |
have "fib (n + 2 + k + 1) |
10007 | 60 |
= fib (n + k + 1) + fib (n + 1 + k + 1)" by simp |
61 |
also assume "fib (n + k + 1) |
|
8051 | 62 |
= fib (k + 1) * fib (n + 1) + fib k * fib n" |
10007 | 63 |
(is " _ = ?R1") |
64 |
also assume "fib (n + 1 + k + 1) |
|
8051 | 65 |
= fib (k + 1) * fib (n + 1 + 1) + fib k * fib (n + 1)" |
10007 | 66 |
(is " _ = ?R2") |
67 |
also have "?R1 + ?R2 |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
68 |
= fib (k + 1) * fib (n + 2 + 1) + fib k * fib (n + 2)" |
10007 | 69 |
by (simp add: add_mult_distrib2) |
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
70 |
finally show "?P (n + 2)" . |
10007 | 71 |
qed |
8051 | 72 |
|
27556 | 73 |
lemma gcd_fib_Suc_eq_1: "gcd (fib n) (fib (n + 1)) = 1" (is "?P n") |
11809 | 74 |
proof (induct n rule: fib_induct) |
10007 | 75 |
show "?P 0" by simp |
76 |
show "?P 1" by simp |
|
77 |
fix n |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
78 |
have "fib (n + 2 + 1) = fib (n + 1) + fib (n + 2)" |
10007 | 79 |
by simp |
27556 | 80 |
also have "gcd (fib (n + 2)) ... = gcd (fib (n + 2)) (fib (n + 1))" |
10007 | 81 |
by (simp only: gcd_add2') |
27556 | 82 |
also have "... = gcd (fib (n + 1)) (fib (n + 1 + 1))" |
10007 | 83 |
by (simp add: gcd_commute) |
84 |
also assume "... = 1" |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
85 |
finally show "?P (n + 2)" . |
10007 | 86 |
qed |
8051 | 87 |
|
27556 | 88 |
lemma gcd_mult_add: "0 < n ==> gcd (n * k + m) n = gcd m n" |
10007 | 89 |
proof - |
90 |
assume "0 < n" |
|
27556 | 91 |
then have "gcd (n * k + m) n = gcd n (m mod n)" |
10007 | 92 |
by (simp add: gcd_non_0 add_commute) |
27556 | 93 |
also from `0 < n` have "... = gcd m n" by (simp add: gcd_non_0) |
10007 | 94 |
finally show ?thesis . |
95 |
qed |
|
8051 | 96 |
|
27556 | 97 |
lemma gcd_fib_add: "gcd (fib m) (fib (n + m)) = gcd (fib m) (fib n)" |
10007 | 98 |
proof (cases m) |
18153 | 99 |
case 0 |
100 |
then show ?thesis by simp |
|
10007 | 101 |
next |
18153 | 102 |
case (Suc k) |
27556 | 103 |
then have "gcd (fib m) (fib (n + m)) = gcd (fib (n + k + 1)) (fib (k + 1))" |
10007 | 104 |
by (simp add: gcd_commute) |
105 |
also have "fib (n + k + 1) |
|
106 |
= fib (k + 1) * fib (n + 1) + fib k * fib n" |
|
107 |
by (rule fib_add) |
|
27556 | 108 |
also have "gcd ... (fib (k + 1)) = gcd (fib k * fib n) (fib (k + 1))" |
10007 | 109 |
by (simp add: gcd_mult_add) |
27556 | 110 |
also have "... = gcd (fib n) (fib (k + 1))" |
10007 | 111 |
by (simp only: gcd_fib_Suc_eq_1 gcd_mult_cancel) |
27556 | 112 |
also have "... = gcd (fib m) (fib n)" |
18153 | 113 |
using Suc by (simp add: gcd_commute) |
10007 | 114 |
finally show ?thesis . |
115 |
qed |
|
8051 | 116 |
|
9659 | 117 |
lemma gcd_fib_diff: |
18153 | 118 |
assumes "m <= n" |
27556 | 119 |
shows "gcd (fib m) (fib (n - m)) = gcd (fib m) (fib n)" |
10007 | 120 |
proof - |
27556 | 121 |
have "gcd (fib m) (fib (n - m)) = gcd (fib m) (fib (n - m + m))" |
10007 | 122 |
by (simp add: gcd_fib_add) |
18153 | 123 |
also from `m <= n` have "n - m + m = n" by simp |
10007 | 124 |
finally show ?thesis . |
125 |
qed |
|
8051 | 126 |
|
9659 | 127 |
lemma gcd_fib_mod: |
18241 | 128 |
assumes "0 < m" |
27556 | 129 |
shows "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)" |
18153 | 130 |
proof (induct n rule: nat_less_induct) |
131 |
case (1 n) note hyp = this |
|
132 |
show ?case |
|
133 |
proof - |
|
134 |
have "n mod m = (if n < m then n else (n - m) mod m)" |
|
135 |
by (rule mod_if) |
|
27556 | 136 |
also have "gcd (fib m) (fib ...) = gcd (fib m) (fib n)" |
18153 | 137 |
proof (cases "n < m") |
138 |
case True then show ?thesis by simp |
|
139 |
next |
|
140 |
case False then have "m <= n" by simp |
|
18241 | 141 |
from `0 < m` and False have "n - m < n" by simp |
27556 | 142 |
with hyp have "gcd (fib m) (fib ((n - m) mod m)) |
143 |
= gcd (fib m) (fib (n - m))" by simp |
|
144 |
also have "... = gcd (fib m) (fib n)" |
|
18153 | 145 |
using `m <= n` by (rule gcd_fib_diff) |
27556 | 146 |
finally have "gcd (fib m) (fib ((n - m) mod m)) = |
147 |
gcd (fib m) (fib n)" . |
|
18153 | 148 |
with False show ?thesis by simp |
10408 | 149 |
qed |
18153 | 150 |
finally show ?thesis . |
10007 | 151 |
qed |
152 |
qed |
|
8051 | 153 |
|
154 |
||
27556 | 155 |
theorem fib_gcd: "fib (gcd m n) = gcd (fib m) (fib n)" (is "?P m n") |
11809 | 156 |
proof (induct m n rule: gcd_induct) |
27556 | 157 |
fix m show "fib (gcd m 0) = gcd (fib m) (fib 0)" by simp |
10007 | 158 |
fix n :: nat assume n: "0 < n" |
27556 | 159 |
then have "gcd m n = gcd n (m mod n)" by (rule gcd_non_0) |
160 |
also assume hyp: "fib ... = gcd (fib n) (fib (m mod n))" |
|
161 |
also from n have "... = gcd (fib n) (fib m)" by (rule gcd_fib_mod) |
|
162 |
also have "... = gcd (fib m) (fib n)" by (rule gcd_commute) |
|
163 |
finally show "fib (gcd m n) = gcd (fib m) (fib n)" . |
|
10007 | 164 |
qed |
8051 | 165 |
|
10007 | 166 |
end |