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