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