src/HOL/Number_Theory/Fib.thy
author wenzelm
Sat Sep 10 23:27:32 2011 +0200 (2011-09-10)
changeset 44872 a98ef45122f3
parent 41959 b460124855b8
child 53077 a1b3784f8129
permissions -rw-r--r--
misc tuning;
wenzelm@41959
     1
(*  Title:      HOL/Number_Theory/Fib.thy
wenzelm@41959
     2
    Author:     Lawrence C. Paulson
wenzelm@41959
     3
    Author:     Jeremy Avigad
nipkow@31719
     4
nipkow@31719
     5
Defines the fibonacci function.
nipkow@31719
     6
nipkow@31719
     7
The original "Fib" is due to Lawrence C. Paulson, and was adapted by
nipkow@31719
     8
Jeremy Avigad.
nipkow@31719
     9
*)
nipkow@31719
    10
nipkow@31719
    11
header {* Fib *}
nipkow@31719
    12
nipkow@31719
    13
theory Fib
nipkow@31719
    14
imports Binomial
nipkow@31719
    15
begin
nipkow@31719
    16
nipkow@31719
    17
nipkow@31719
    18
subsection {* Main definitions *}
nipkow@31719
    19
nipkow@31719
    20
class fib =
wenzelm@44872
    21
  fixes fib :: "'a \<Rightarrow> 'a"
nipkow@31719
    22
nipkow@31719
    23
nipkow@31719
    24
(* definition for the natural numbers *)
nipkow@31719
    25
nipkow@31719
    26
instantiation nat :: fib
wenzelm@44872
    27
begin
nipkow@31719
    28
wenzelm@44872
    29
fun fib_nat :: "nat \<Rightarrow> nat"
nipkow@31719
    30
where
nipkow@31719
    31
  "fib_nat n =
nipkow@31719
    32
   (if n = 0 then 0 else
nipkow@31719
    33
   (if n = 1 then 1 else
nipkow@31719
    34
     fib (n - 1) + fib (n - 2)))"
nipkow@31719
    35
wenzelm@44872
    36
instance ..
nipkow@31719
    37
nipkow@31719
    38
end
nipkow@31719
    39
nipkow@31719
    40
(* definition for the integers *)
nipkow@31719
    41
nipkow@31719
    42
instantiation int :: fib
wenzelm@44872
    43
begin
nipkow@31719
    44
wenzelm@44872
    45
definition fib_int :: "int \<Rightarrow> int"
wenzelm@44872
    46
  where "fib_int n = (if n >= 0 then int (fib (nat n)) else 0)"
nipkow@31719
    47
wenzelm@44872
    48
instance ..
nipkow@31719
    49
nipkow@31719
    50
end
nipkow@31719
    51
nipkow@31719
    52
nipkow@31719
    53
subsection {* Set up Transfer *}
nipkow@31719
    54
nipkow@31719
    55
lemma transfer_nat_int_fib:
nipkow@31719
    56
  "(x::int) >= 0 \<Longrightarrow> fib (nat x) = nat (fib x)"
nipkow@31719
    57
  unfolding fib_int_def by auto
nipkow@31719
    58
nipkow@31719
    59
lemma transfer_nat_int_fib_closure:
nipkow@31719
    60
  "n >= (0::int) \<Longrightarrow> fib n >= 0"
nipkow@31719
    61
  by (auto simp add: fib_int_def)
nipkow@31719
    62
wenzelm@44872
    63
declare transfer_morphism_nat_int[transfer add return:
nipkow@31719
    64
    transfer_nat_int_fib transfer_nat_int_fib_closure]
nipkow@31719
    65
wenzelm@44872
    66
lemma transfer_int_nat_fib: "fib (int n) = int (fib n)"
nipkow@31719
    67
  unfolding fib_int_def by auto
nipkow@31719
    68
wenzelm@44872
    69
lemma transfer_int_nat_fib_closure: "is_nat n \<Longrightarrow> fib n >= 0"
nipkow@31719
    70
  unfolding fib_int_def by auto
nipkow@31719
    71
wenzelm@44872
    72
declare transfer_morphism_int_nat[transfer add return:
nipkow@31719
    73
    transfer_int_nat_fib transfer_int_nat_fib_closure]
nipkow@31719
    74
nipkow@31719
    75
nipkow@31719
    76
subsection {* Fibonacci numbers *}
nipkow@31719
    77
nipkow@31952
    78
lemma fib_0_nat [simp]: "fib (0::nat) = 0"
nipkow@31719
    79
  by simp
nipkow@31719
    80
nipkow@31952
    81
lemma fib_0_int [simp]: "fib (0::int) = 0"
nipkow@31719
    82
  unfolding fib_int_def by simp
nipkow@31719
    83
nipkow@31952
    84
lemma fib_1_nat [simp]: "fib (1::nat) = 1"
nipkow@31719
    85
  by simp
nipkow@31719
    86
nipkow@31952
    87
lemma fib_Suc_0_nat [simp]: "fib (Suc 0) = Suc 0"
nipkow@31719
    88
  by simp
nipkow@31719
    89
nipkow@31952
    90
lemma fib_1_int [simp]: "fib (1::int) = 1"
nipkow@31719
    91
  unfolding fib_int_def by simp
nipkow@31719
    92
nipkow@31952
    93
lemma fib_reduce_nat: "(n::nat) >= 2 \<Longrightarrow> fib n = fib (n - 1) + fib (n - 2)"
nipkow@31719
    94
  by simp
nipkow@31719
    95
nipkow@31719
    96
declare fib_nat.simps [simp del]
nipkow@31719
    97
nipkow@31952
    98
lemma fib_reduce_int: "(n::int) >= 2 \<Longrightarrow> fib n = fib (n - 1) + fib (n - 2)"
nipkow@31719
    99
  unfolding fib_int_def
nipkow@31952
   100
  by (auto simp add: fib_reduce_nat nat_diff_distrib)
nipkow@31719
   101
nipkow@31952
   102
lemma fib_neg_int [simp]: "(n::int) < 0 \<Longrightarrow> fib n = 0"
nipkow@31719
   103
  unfolding fib_int_def by auto
nipkow@31719
   104
nipkow@31952
   105
lemma fib_2_nat [simp]: "fib (2::nat) = 1"
nipkow@31952
   106
  by (subst fib_reduce_nat, auto)
nipkow@31719
   107
nipkow@31952
   108
lemma fib_2_int [simp]: "fib (2::int) = 1"
nipkow@31952
   109
  by (subst fib_reduce_int, auto)
nipkow@31719
   110
nipkow@31952
   111
lemma fib_plus_2_nat: "fib ((n::nat) + 2) = fib (n + 1) + fib n"
nipkow@31952
   112
  by (subst fib_reduce_nat, auto simp add: One_nat_def)
nipkow@31719
   113
(* the need for One_nat_def is due to the natdiff_cancel_numerals
nipkow@31719
   114
   procedure *)
nipkow@31719
   115
wenzelm@44872
   116
lemma fib_induct_nat: "P (0::nat) \<Longrightarrow> P (1::nat) \<Longrightarrow>
nipkow@31719
   117
    (!!n. P n \<Longrightarrow> P (n + 1) \<Longrightarrow> P (n + 2)) \<Longrightarrow> P n"
nipkow@31719
   118
  apply (atomize, induct n rule: nat_less_induct)
nipkow@31719
   119
  apply auto
nipkow@31719
   120
  apply (case_tac "n = 0", force)
nipkow@31719
   121
  apply (case_tac "n = 1", force)
nipkow@31719
   122
  apply (subgoal_tac "n >= 2")
nipkow@31719
   123
  apply (frule_tac x = "n - 1" in spec)
nipkow@31719
   124
  apply (drule_tac x = "n - 2" in spec)
nipkow@31719
   125
  apply (drule_tac x = "n - 2" in spec)
nipkow@31719
   126
  apply auto
nipkow@31719
   127
  apply (auto simp add: One_nat_def) (* again, natdiff_cancel *)
nipkow@31719
   128
done
nipkow@31719
   129
wenzelm@44872
   130
lemma fib_add_nat: "fib ((n::nat) + k + 1) = fib (k + 1) * fib (n + 1) +
nipkow@31719
   131
    fib k * fib n"
nipkow@31952
   132
  apply (induct n rule: fib_induct_nat)
nipkow@31719
   133
  apply auto
nipkow@31952
   134
  apply (subst fib_reduce_nat)
haftmann@36350
   135
  apply (auto simp add: field_simps)
nipkow@31952
   136
  apply (subst (1 3 5) fib_reduce_nat)
haftmann@36350
   137
  apply (auto simp add: field_simps Suc_eq_plus1)
nipkow@31719
   138
(* hmmm. Why doesn't "n + (1 + (1 + k))" simplify to "n + k + 2"? *)
nipkow@31719
   139
  apply (subgoal_tac "n + (k + 2) = n + (1 + (1 + k))")
nipkow@31719
   140
  apply (erule ssubst) back back
wenzelm@44872
   141
  apply (erule ssubst) back
nipkow@31719
   142
  apply auto
nipkow@31719
   143
done
nipkow@31719
   144
wenzelm@44872
   145
lemma fib_add'_nat: "fib (n + Suc k) =
wenzelm@44872
   146
    fib (Suc k) * fib (Suc n) + fib k * fib n"
nipkow@31952
   147
  using fib_add_nat by (auto simp add: One_nat_def)
nipkow@31719
   148
nipkow@31719
   149
nipkow@31719
   150
(* transfer from nats to ints *)
wenzelm@44872
   151
lemma fib_add_int: "(n::int) >= 0 \<Longrightarrow> k >= 0 \<Longrightarrow>
wenzelm@44872
   152
    fib (n + k + 1) = fib (k + 1) * fib (n + 1) +  fib k * fib n "
nipkow@31952
   153
  by (rule fib_add_nat [transferred])
nipkow@31719
   154
nipkow@31952
   155
lemma fib_neq_0_nat: "(n::nat) > 0 \<Longrightarrow> fib n ~= 0"
nipkow@31952
   156
  apply (induct n rule: fib_induct_nat)
nipkow@31952
   157
  apply (auto simp add: fib_plus_2_nat)
wenzelm@44872
   158
  done
nipkow@31719
   159
nipkow@31952
   160
lemma fib_gr_0_nat: "(n::nat) > 0 \<Longrightarrow> fib n > 0"
nipkow@31952
   161
  by (frule fib_neq_0_nat, simp)
nipkow@31719
   162
nipkow@31952
   163
lemma fib_gr_0_int: "(n::int) > 0 \<Longrightarrow> fib n > 0"
nipkow@31952
   164
  unfolding fib_int_def by (simp add: fib_gr_0_nat)
nipkow@31719
   165
nipkow@31719
   166
text {*
nipkow@31719
   167
  \medskip Concrete Mathematics, page 278: Cassini's identity.  The proof is
nipkow@31719
   168
  much easier using integers, not natural numbers!
nipkow@31719
   169
*}
nipkow@31719
   170
wenzelm@44872
   171
lemma fib_Cassini_aux_int: "fib (int n + 2) * fib (int n) -
nipkow@31719
   172
    (fib (int n + 1))^2 = (-1)^(n + 1)"
nipkow@31719
   173
  apply (induct n)
wenzelm@44872
   174
  apply (auto simp add: field_simps power2_eq_square fib_reduce_int power_add)
wenzelm@44872
   175
  done
nipkow@31719
   176
wenzelm@44872
   177
lemma fib_Cassini_int: "n >= 0 \<Longrightarrow> fib (n + 2) * fib n -
nipkow@31719
   178
    (fib (n + 1))^2 = (-1)^(nat n + 1)"
nipkow@31952
   179
  by (insert fib_Cassini_aux_int [of "nat n"], auto)
nipkow@31719
   180
nipkow@31719
   181
(*
wenzelm@44872
   182
lemma fib_Cassini'_int: "n >= 0 \<Longrightarrow> fib (n + 2) * fib n =
nipkow@31719
   183
    (fib (n + 1))^2 + (-1)^(nat n + 1)"
wenzelm@44872
   184
  by (frule fib_Cassini_int, simp)
nipkow@31719
   185
*)
nipkow@31719
   186
nipkow@31952
   187
lemma fib_Cassini'_int: "n >= 0 \<Longrightarrow> fib ((n::int) + 2) * fib n =
nipkow@31719
   188
  (if even n then tsub ((fib (n + 1))^2) 1
nipkow@31719
   189
   else (fib (n + 1))^2 + 1)"
nipkow@31952
   190
  apply (frule fib_Cassini_int, auto simp add: pos_int_even_equiv_nat_even)
nipkow@31719
   191
  apply (subst tsub_eq)
nipkow@31952
   192
  apply (insert fib_gr_0_int [of "n + 1"], force)
nipkow@31719
   193
  apply auto
wenzelm@44872
   194
  done
nipkow@31719
   195
nipkow@31952
   196
lemma fib_Cassini_nat: "fib ((n::nat) + 2) * fib n =
wenzelm@44872
   197
    (if even n then (fib (n + 1))^2 - 1
wenzelm@44872
   198
     else (fib (n + 1))^2 + 1)"
nipkow@31952
   199
  by (rule fib_Cassini'_int [transferred, of n], auto)
nipkow@31719
   200
nipkow@31719
   201
nipkow@31719
   202
text {* \medskip Toward Law 6.111 of Concrete Mathematics *}
nipkow@31719
   203
nipkow@31952
   204
lemma coprime_fib_plus_1_nat: "coprime (fib (n::nat)) (fib (n + 1))"
nipkow@31952
   205
  apply (induct n rule: fib_induct_nat)
nipkow@31719
   206
  apply auto
nipkow@31952
   207
  apply (subst (2) fib_reduce_nat)
nipkow@31792
   208
  apply (auto simp add: Suc_eq_plus1) (* again, natdiff_cancel *)
nipkow@31719
   209
  apply (subst add_commute, auto)
haftmann@36350
   210
  apply (subst gcd_commute_nat, auto simp add: field_simps)
wenzelm@44872
   211
  done
nipkow@31719
   212
nipkow@31952
   213
lemma coprime_fib_Suc_nat: "coprime (fib n) (fib (Suc n))"
nipkow@31952
   214
  using coprime_fib_plus_1_nat by (simp add: One_nat_def)
nipkow@31719
   215
wenzelm@44872
   216
lemma coprime_fib_plus_1_int: "n >= 0 \<Longrightarrow> coprime (fib (n::int)) (fib (n + 1))"
nipkow@31952
   217
  by (erule coprime_fib_plus_1_nat [transferred])
nipkow@31719
   218
nipkow@31952
   219
lemma gcd_fib_add_nat: "gcd (fib (m::nat)) (fib (n + m)) = gcd (fib m) (fib n)"
nipkow@31952
   220
  apply (simp add: gcd_commute_nat [of "fib m"])
nipkow@31952
   221
  apply (rule cases_nat [of _ m])
nipkow@31719
   222
  apply simp
nipkow@31719
   223
  apply (subst add_assoc [symmetric])
nipkow@31952
   224
  apply (simp add: fib_add_nat)
nipkow@31952
   225
  apply (subst gcd_commute_nat)
nipkow@31719
   226
  apply (subst mult_commute)
nipkow@31952
   227
  apply (subst gcd_add_mult_nat)
nipkow@31952
   228
  apply (subst gcd_commute_nat)
nipkow@31952
   229
  apply (rule gcd_mult_cancel_nat)
nipkow@31952
   230
  apply (rule coprime_fib_plus_1_nat)
wenzelm@44872
   231
  done
nipkow@31719
   232
wenzelm@44872
   233
lemma gcd_fib_add_int [rule_format]: "m >= 0 \<Longrightarrow> n >= 0 \<Longrightarrow>
nipkow@31719
   234
    gcd (fib (m::int)) (fib (n + m)) = gcd (fib m) (fib n)"
nipkow@31952
   235
  by (erule gcd_fib_add_nat [transferred])
nipkow@31719
   236
wenzelm@44872
   237
lemma gcd_fib_diff_nat: "(m::nat) \<le> n \<Longrightarrow>
nipkow@31719
   238
    gcd (fib m) (fib (n - m)) = gcd (fib m) (fib n)"
nipkow@31952
   239
  by (simp add: gcd_fib_add_nat [symmetric, of _ "n-m"])
nipkow@31719
   240
wenzelm@44872
   241
lemma gcd_fib_diff_int: "0 <= (m::int) \<Longrightarrow> m \<le> n \<Longrightarrow>
nipkow@31719
   242
    gcd (fib m) (fib (n - m)) = gcd (fib m) (fib n)"
nipkow@31952
   243
  by (simp add: gcd_fib_add_int [symmetric, of _ "n-m"])
nipkow@31719
   244
wenzelm@44872
   245
lemma gcd_fib_mod_nat: "0 < (m::nat) \<Longrightarrow>
nipkow@31719
   246
    gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)"
nipkow@31719
   247
proof (induct n rule: less_induct)
nipkow@31719
   248
  case (less n)
nipkow@31719
   249
  from less.prems have pos_m: "0 < m" .
nipkow@31719
   250
  show "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)"
nipkow@31719
   251
  proof (cases "m < n")
wenzelm@44872
   252
    case True
wenzelm@44872
   253
    then have "m \<le> n" by auto
nipkow@31719
   254
    with pos_m have pos_n: "0 < n" by auto
wenzelm@44872
   255
    with pos_m `m < n` have diff: "n - m < n" by auto
nipkow@31719
   256
    have "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib ((n - m) mod m))"
wenzelm@44872
   257
      by (simp add: mod_if [of n]) (insert `m < n`, auto)
wenzelm@44872
   258
    also have "\<dots> = gcd (fib m)  (fib (n - m))"
nipkow@31719
   259
      by (simp add: less.hyps diff pos_m)
wenzelm@44872
   260
    also have "\<dots> = gcd (fib m) (fib n)"
wenzelm@44872
   261
      by (simp add: gcd_fib_diff_nat `m \<le> n`)
nipkow@31719
   262
    finally show "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)" .
nipkow@31719
   263
  next
wenzelm@44872
   264
    case False
wenzelm@44872
   265
    then show "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)"
wenzelm@44872
   266
      by (cases "m = n") auto
nipkow@31719
   267
  qed
nipkow@31719
   268
qed
nipkow@31719
   269
wenzelm@44872
   270
lemma gcd_fib_mod_int:
nipkow@31719
   271
  assumes "0 < (m::int)" and "0 <= n"
nipkow@31719
   272
  shows "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)"
nipkow@31952
   273
  apply (rule gcd_fib_mod_nat [transferred])
wenzelm@41541
   274
  using assms apply auto
wenzelm@41541
   275
  done
nipkow@31719
   276
wenzelm@44872
   277
lemma fib_gcd_nat: "fib (gcd (m::nat) n) = gcd (fib m) (fib n)"
nipkow@31719
   278
    -- {* Law 6.111 *}
nipkow@31952
   279
  apply (induct m n rule: gcd_nat_induct)
nipkow@31952
   280
  apply (simp_all add: gcd_non_0_nat gcd_commute_nat gcd_fib_mod_nat)
wenzelm@41541
   281
  done
nipkow@31719
   282
nipkow@31952
   283
lemma fib_gcd_int: "m >= 0 \<Longrightarrow> n >= 0 \<Longrightarrow>
nipkow@31719
   284
    fib (gcd (m::int) n) = gcd (fib m) (fib n)"
nipkow@31952
   285
  by (erule fib_gcd_nat [transferred])
nipkow@31719
   286
wenzelm@44872
   287
lemma atMost_plus_one_nat: "{..(k::nat) + 1} = insert (k + 1) {..k}"
nipkow@31719
   288
  by auto
nipkow@31719
   289
nipkow@31952
   290
theorem fib_mult_eq_setsum_nat:
nipkow@31719
   291
    "fib ((n::nat) + 1) * fib n = (\<Sum>k \<in> {..n}. fib k * fib k)"
nipkow@31719
   292
  apply (induct n)
haftmann@36350
   293
  apply (auto simp add: atMost_plus_one_nat fib_plus_2_nat field_simps)
wenzelm@41541
   294
  done
nipkow@31719
   295
nipkow@31952
   296
theorem fib_mult_eq_setsum'_nat:
nipkow@31719
   297
    "fib (Suc n) * fib n = (\<Sum>k \<in> {..n}. fib k * fib k)"
nipkow@31952
   298
  using fib_mult_eq_setsum_nat by (simp add: One_nat_def)
nipkow@31719
   299
nipkow@31952
   300
theorem fib_mult_eq_setsum_int [rule_format]:
nipkow@31719
   301
    "n >= 0 \<Longrightarrow> fib ((n::int) + 1) * fib n = (\<Sum>k \<in> {0..n}. fib k * fib k)"
nipkow@31952
   302
  by (erule fib_mult_eq_setsum_nat [transferred])
nipkow@31719
   303
nipkow@31719
   304
end