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