src/HOL/Old_Number_Theory/Fib.thy
 author wenzelm Sun Nov 02 18:21:45 2014 +0100 (2014-11-02) changeset 58889 5b7a9633cfa8 parent 57512 cc97b347b301 child 61382 efac889fccbc permissions -rw-r--r--
```     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 {* The Fibonacci function *}
```
```     7
```
```     8 theory Fib
```
```     9 imports Primes
```
```    10 begin
```
```    11
```
```    12 text {*
```
```    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 *}
```
```    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 {*
```
```    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 *}
```
```    32
```
```    33 text{*We disable @{text fib.fib_2fib_2} for simplification ...*}
```
```    34 declare fib_2 [simp del]
```
```    35
```
```    36 text{*...then prove a version that has a more restrictive pattern.*}
```
```    37 lemma fib_Suc3: "fib (Suc (Suc (Suc n))) = fib (Suc n) + fib (Suc (Suc n))"
```
```    38   by (rule fib_2)
```
```    39
```
```    40 text {* \medskip Concrete Mathematics, page 280 *}
```
```    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 {*
```
```    64   \medskip Concrete Mathematics, page 278: Cassini's identity.  The proof is
```
```    65   much easier using integers, not natural numbers!
```
```    66 *}
```
```    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{*We now obtain a version for the natural numbers via the coercion
```
```    83    function @{term int}.*}
```
```    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 int_int_eq [THEN iffD1])
```
```    89   apply (simp add: fib_Cassini_int)
```
```    90   apply (subst of_nat_diff)
```
```    91    apply (insert fib_Suc_gr_0 [of n], simp_all)
```
```    92   done
```
```    93
```
```    94
```
```    95 text {* \medskip Toward Law 6.111 of Concrete Mathematics *}
```
```    96
```
```    97 lemma gcd_fib_Suc_eq_1: "gcd (fib n) (fib (Suc n)) = Suc 0"
```
```    98   apply (induct n rule: fib.induct)
```
```    99     prefer 3
```
```   100     apply (simp add: gcd_commute fib_Suc3)
```
```   101    apply (simp_all add: fib_2)
```
```   102   done
```
```   103
```
```   104 lemma gcd_fib_add: "gcd (fib m) (fib (n + m)) = gcd (fib m) (fib n)"
```
```   105   apply (simp add: gcd_commute [of "fib m"])
```
```   106   apply (case_tac m)
```
```   107    apply simp
```
```   108   apply (simp add: fib_add)
```
```   109   apply (simp add: add.commute gcd_non_0 [OF fib_Suc_gr_0])
```
```   110   apply (simp add: gcd_non_0 [OF fib_Suc_gr_0, symmetric])
```
```   111   apply (simp add: gcd_fib_Suc_eq_1 gcd_mult_cancel)
```
```   112   done
```
```   113
```
```   114 lemma gcd_fib_diff: "m \<le> n ==> gcd (fib m) (fib (n - m)) = gcd (fib m) (fib n)"
```
```   115   by (simp add: gcd_fib_add [symmetric, of _ "n-m"])
```
```   116
```
```   117 lemma gcd_fib_mod: "0 < m ==> gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)"
```
```   118 proof (induct n rule: less_induct)
```
```   119   case (less n)
```
```   120   from less.prems have pos_m: "0 < m" .
```
```   121   show "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)"
```
```   122   proof (cases "m < n")
```
```   123     case True note m_n = True
```
```   124     then have m_n': "m \<le> n" by auto
```
```   125     with pos_m have pos_n: "0 < n" by auto
```
```   126     with pos_m m_n have diff: "n - m < n" by auto
```
```   127     have "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib ((n - m) mod m))"
```
```   128     by (simp add: mod_if [of n]) (insert m_n, auto)
```
```   129     also have "\<dots> = gcd (fib m) (fib (n - m))" by (simp add: less.hyps diff pos_m)
```
```   130     also have "\<dots> = gcd (fib m) (fib n)" by (simp add: gcd_fib_diff m_n')
```
```   131     finally show "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)" .
```
```   132   next
```
```   133     case False then show "gcd (fib m) (fib (n mod m)) = gcd (fib m) (fib n)"
```
```   134     by (cases "m = n") auto
```
```   135   qed
```
```   136 qed
```
```   137
```
```   138 lemma fib_gcd: "fib (gcd m n) = gcd (fib m) (fib n)"  -- {* Law 6.111 *}
```
```   139   apply (induct m n rule: gcd_induct)
```
```   140   apply (simp_all add: gcd_non_0 gcd_commute gcd_fib_mod)
```
```   141   done
```
```   142
```
```   143 theorem fib_mult_eq_setsum:
```
```   144     "fib (Suc n) * fib n = (\<Sum>k \<in> {..n}. fib k * fib k)"
```
```   145   apply (induct n rule: fib.induct)
```
```   146     apply (auto simp add: atMost_Suc fib_2)
```
```   147   apply (simp add: add_mult_distrib add_mult_distrib2)
```
```   148   done
```
```   149
```
```   150 end
```