src/HOL/NumberTheory/Fib.thy
author wenzelm
Fri Mar 28 19:43:54 2008 +0100 (2008-03-28)
changeset 26462 dac4e2bce00d
parent 25361 1aa441e48496
child 27556 292098f2efdf
permissions -rw-r--r--
avoid rebinding of existing facts;
paulson@15628
     1
(*  ID:         $Id$
paulson@9944
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@9944
     3
    Copyright   1997  University of Cambridge
paulson@9944
     4
*)
paulson@9944
     5
wenzelm@11049
     6
header {* The Fibonacci function *}
wenzelm@11049
     7
haftmann@16417
     8
theory Fib imports Primes begin
wenzelm@11049
     9
wenzelm@11049
    10
text {*
wenzelm@11049
    11
  Fibonacci numbers: proofs of laws taken from:
wenzelm@11049
    12
  R. L. Graham, D. E. Knuth, O. Patashnik.  Concrete Mathematics.
wenzelm@11049
    13
  (Addison-Wesley, 1989)
wenzelm@11049
    14
wenzelm@11049
    15
  \bigskip
wenzelm@11049
    16
*}
wenzelm@11049
    17
paulson@24549
    18
fun fib :: "nat \<Rightarrow> nat"
paulson@24549
    19
where
paulson@24573
    20
         "fib 0 = 0"
paulson@24573
    21
|        "fib (Suc 0) = 1"
paulson@24549
    22
| fib_2: "fib (Suc (Suc n)) = fib n + fib (Suc n)"
wenzelm@11049
    23
wenzelm@11049
    24
text {*
wenzelm@11049
    25
  \medskip The difficulty in these proofs is to ensure that the
wenzelm@11049
    26
  induction hypotheses are applied before the definition of @{term
wenzelm@11049
    27
  fib}.  Towards this end, the @{term fib} equations are not declared
wenzelm@11049
    28
  to the Simplifier and are applied very selectively at first.
wenzelm@11049
    29
*}
wenzelm@11049
    30
paulson@24549
    31
text{*We disable @{text fib.fib_2fib_2} for simplification ...*}
paulson@24549
    32
declare fib_2 [simp del]
wenzelm@11049
    33
paulson@15628
    34
text{*...then prove a version that has a more restrictive pattern.*}
wenzelm@11049
    35
lemma fib_Suc3: "fib (Suc (Suc (Suc n))) = fib (Suc n) + fib (Suc (Suc n))"
paulson@24549
    36
  by (rule fib_2)
wenzelm@11049
    37
wenzelm@11049
    38
text {* \medskip Concrete Mathematics, page 280 *}
wenzelm@11049
    39
wenzelm@11049
    40
lemma fib_add: "fib (Suc (n + k)) = fib (Suc k) * fib (Suc n) + fib k * fib n"
paulson@24573
    41
proof (induct n rule: fib.induct)
paulson@24573
    42
  case 1 show ?case by simp
paulson@24573
    43
next
paulson@24573
    44
  case 2 show ?case  by (simp add: fib_2)
paulson@24573
    45
next
krauss@25361
    46
  case 3 thus ?case by (simp add: fib_2 add_mult_distrib2)
paulson@24573
    47
qed
wenzelm@11049
    48
paulson@15628
    49
lemma fib_Suc_neq_0: "fib (Suc n) \<noteq> 0"
wenzelm@11049
    50
  apply (induct n rule: fib.induct)
paulson@24549
    51
    apply (simp_all add: fib_2)
wenzelm@11049
    52
  done
wenzelm@11049
    53
paulson@15628
    54
lemma fib_Suc_gr_0: "0 < fib (Suc n)"
paulson@15628
    55
  by (insert fib_Suc_neq_0 [of n], simp)  
wenzelm@11049
    56
wenzelm@11049
    57
lemma fib_gr_0: "0 < n ==> 0 < fib n"
paulson@15628
    58
  by (case_tac n, auto simp add: fib_Suc_gr_0) 
wenzelm@11049
    59
paulson@9944
    60
wenzelm@11049
    61
text {*
paulson@15628
    62
  \medskip Concrete Mathematics, page 278: Cassini's identity.  The proof is
paulson@15628
    63
  much easier using integers, not natural numbers!
wenzelm@11049
    64
*}
wenzelm@11049
    65
paulson@15628
    66
lemma fib_Cassini_int:
paulson@15003
    67
 "int (fib (Suc (Suc n)) * fib n) =
paulson@11868
    68
  (if n mod 2 = 0 then int (fib (Suc n) * fib (Suc n)) - 1
paulson@11868
    69
   else int (fib (Suc n) * fib (Suc n)) + 1)"
chaieb@23315
    70
proof(induct n rule: fib.induct)
paulson@24549
    71
  case 1 thus ?case by (simp add: fib_2)
chaieb@23315
    72
next
paulson@24549
    73
  case 2 thus ?case by (simp add: fib_2 mod_Suc)
chaieb@23315
    74
next 
krauss@25361
    75
  case (3 x) 
paulson@24573
    76
  have "Suc 0 \<noteq> x mod 2 \<longrightarrow> x mod 2 = 0" by presburger
krauss@25361
    77
  with "3.hyps" show ?case by (simp add: fib.simps add_mult_distrib add_mult_distrib2)
chaieb@23315
    78
qed
paulson@15628
    79
paulson@15628
    80
text{*We now obtain a version for the natural numbers via the coercion 
paulson@15628
    81
   function @{term int}.*}
paulson@15628
    82
theorem fib_Cassini:
paulson@15628
    83
 "fib (Suc (Suc n)) * fib n =
paulson@15628
    84
  (if n mod 2 = 0 then fib (Suc n) * fib (Suc n) - 1
paulson@15628
    85
   else fib (Suc n) * fib (Suc n) + 1)"
paulson@15628
    86
  apply (rule int_int_eq [THEN iffD1]) 
huffman@23431
    87
  apply (simp add: fib_Cassini_int)
paulson@15628
    88
  apply (subst zdiff_int [symmetric]) 
paulson@15628
    89
   apply (insert fib_Suc_gr_0 [of n], simp_all)
wenzelm@11049
    90
  done
wenzelm@11049
    91
wenzelm@11049
    92
paulson@15628
    93
text {* \medskip Toward Law 6.111 of Concrete Mathematics *}
wenzelm@11049
    94
wenzelm@11701
    95
lemma gcd_fib_Suc_eq_1: "gcd (fib n, fib (Suc n)) = Suc 0"
wenzelm@11049
    96
  apply (induct n rule: fib.induct)
wenzelm@11049
    97
    prefer 3
wenzelm@11049
    98
    apply (simp add: gcd_commute fib_Suc3)
paulson@24549
    99
   apply (simp_all add: fib_2)
wenzelm@11049
   100
  done
wenzelm@11049
   101
wenzelm@11049
   102
lemma gcd_fib_add: "gcd (fib m, fib (n + m)) = gcd (fib m, fib n)"
paulson@15628
   103
  apply (simp add: gcd_commute [of "fib m"])
paulson@15628
   104
  apply (case_tac m)
paulson@15628
   105
   apply simp 
wenzelm@11049
   106
  apply (simp add: fib_add)
paulson@15628
   107
  apply (simp add: add_commute gcd_non_0 [OF fib_Suc_gr_0])
paulson@15628
   108
  apply (simp add: gcd_non_0 [OF fib_Suc_gr_0, symmetric])
wenzelm@11049
   109
  apply (simp add: gcd_fib_Suc_eq_1 gcd_mult_cancel)
wenzelm@11049
   110
  done
wenzelm@11049
   111
wenzelm@11049
   112
lemma gcd_fib_diff: "m \<le> n ==> gcd (fib m, fib (n - m)) = gcd (fib m, fib n)"
paulson@15628
   113
  by (simp add: gcd_fib_add [symmetric, of _ "n-m"]) 
wenzelm@11049
   114
wenzelm@11049
   115
lemma gcd_fib_mod: "0 < m ==> gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
haftmann@23688
   116
proof (induct n rule: less_induct)
haftmann@23688
   117
  case (less n)
haftmann@23688
   118
  from less.prems have pos_m: "0 < m" .
haftmann@23688
   119
  show "gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
haftmann@23688
   120
  proof (cases "m < n")
haftmann@23688
   121
    case True note m_n = True
haftmann@23688
   122
    then have m_n': "m \<le> n" by auto
haftmann@23688
   123
    with pos_m have pos_n: "0 < n" by auto
haftmann@23688
   124
    with pos_m m_n have diff: "n - m < n" by auto
haftmann@23688
   125
    have "gcd (fib m, fib (n mod m)) = gcd (fib m, fib ((n - m) mod m))"
haftmann@23688
   126
    by (simp add: mod_if [of n]) (insert m_n, auto)
haftmann@23688
   127
    also have "\<dots> = gcd (fib m, fib (n - m))" by (simp add: less.hyps diff pos_m)
haftmann@23688
   128
    also have "\<dots> = gcd (fib m, fib n)" by (simp add: gcd_fib_diff m_n')
haftmann@23688
   129
    finally show "gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)" .
haftmann@23688
   130
  next
haftmann@23688
   131
    case False then show "gcd (fib m, fib (n mod m)) = gcd (fib m, fib n)"
haftmann@23688
   132
    by (cases "m = n") auto
haftmann@23688
   133
  qed
haftmann@23688
   134
qed
wenzelm@11049
   135
wenzelm@11049
   136
lemma fib_gcd: "fib (gcd (m, n)) = gcd (fib m, fib n)"  -- {* Law 6.111 *}
wenzelm@11049
   137
  apply (induct m n rule: gcd_induct)
paulson@15628
   138
  apply (simp_all add: gcd_non_0 gcd_commute gcd_fib_mod)
wenzelm@11049
   139
  done
wenzelm@11049
   140
paulson@15628
   141
theorem fib_mult_eq_setsum:
wenzelm@11786
   142
    "fib (Suc n) * fib n = (\<Sum>k \<in> {..n}. fib k * fib k)"
wenzelm@11049
   143
  apply (induct n rule: fib.induct)
paulson@24549
   144
    apply (auto simp add: atMost_Suc fib_2)
wenzelm@11049
   145
  apply (simp add: add_mult_distrib add_mult_distrib2)
wenzelm@11049
   146
  done
paulson@9944
   147
paulson@9944
   148
end