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