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