Theory Transcendental

Up to index of Isabelle/HOL

theory Transcendental
imports Fact Series NthRoot

(*  Title       : Transcendental.thy
    Author      : Jacques D. Fleuriot
    Copyright   : 1998,1999 University of Cambridge
                  1999,2001 University of Edinburgh
    Conversion to Isar and new proofs by Lawrence C Paulson, 2004
*)

header{*Power Series, Transcendental Functions etc.*}

theory Transcendental
imports Fact Series Deriv NthRoot
begin

subsection {* Properties of Power Series *}

lemma lemma_realpow_diff:
  fixes y :: "'a::recpower"
  shows "p ≤ n ==> y ^ (Suc n - p) = (y ^ (n - p)) * y"
proof -
  assume "p ≤ n"
  hence "Suc n - p = Suc (n - p)" by (rule Suc_diff_le)
  thus ?thesis by (simp add: power_commutes)
qed

lemma lemma_realpow_diff_sumr:
  fixes y :: "'a::{recpower,comm_semiring_0}" shows
     "(∑p=0..<Suc n. (x ^ p) * y ^ (Suc n - p)) =  
      y * (∑p=0..<Suc n. (x ^ p) * y ^ (n - p))"
by (simp add: setsum_right_distrib lemma_realpow_diff mult_ac
         del: setsum_op_ivl_Suc cong: strong_setsum_cong)

lemma lemma_realpow_diff_sumr2:
  fixes y :: "'a::{recpower,comm_ring}" shows
     "x ^ (Suc n) - y ^ (Suc n) =  
      (x - y) * (∑p=0..<Suc n. (x ^ p) * y ^ (n - p))"
apply (induct n, simp)
apply (simp del: setsum_op_ivl_Suc)
apply (subst setsum_op_ivl_Suc)
apply (subst lemma_realpow_diff_sumr)
apply (simp add: right_distrib del: setsum_op_ivl_Suc)
apply (subst mult_left_commute [where a="x - y"])
apply (erule subst)
apply (simp add: algebra_simps)
done

lemma lemma_realpow_rev_sumr:
     "(∑p=0..<Suc n. (x ^ p) * (y ^ (n - p))) =  
      (∑p=0..<Suc n. (x ^ (n - p)) * (y ^ p))"
apply (rule setsum_reindex_cong [where f="λi. n - i"])
apply (rule inj_onI, simp)
apply auto
apply (rule_tac x="n - x" in image_eqI, simp, simp)
done

text{*Power series has a `circle` of convergence, i.e. if it sums for @{term
x}, then it sums absolutely for @{term z} with @{term "¦z¦ < ¦x¦"}.*}

lemma powser_insidea:
  fixes x z :: "'a::{real_normed_field,banach,recpower}"
  assumes 1: "summable (λn. f n * x ^ n)"
  assumes 2: "norm z < norm x"
  shows "summable (λn. norm (f n * z ^ n))"
proof -
  from 2 have x_neq_0: "x ≠ 0" by clarsimp
  from 1 have "(λn. f n * x ^ n) ----> 0"
    by (rule summable_LIMSEQ_zero)
  hence "convergent (λn. f n * x ^ n)"
    by (rule convergentI)
  hence "Cauchy (λn. f n * x ^ n)"
    by (simp add: Cauchy_convergent_iff)
  hence "Bseq (λn. f n * x ^ n)"
    by (rule Cauchy_Bseq)
  then obtain K where 3: "0 < K" and 4: "∀n. norm (f n * x ^ n) ≤ K"
    by (simp add: Bseq_def, safe)
  have "∃N. ∀n≥N. norm (norm (f n * z ^ n)) ≤
                   K * norm (z ^ n) * inverse (norm (x ^ n))"
  proof (intro exI allI impI)
    fix n::nat assume "0 ≤ n"
    have "norm (norm (f n * z ^ n)) * norm (x ^ n) =
          norm (f n * x ^ n) * norm (z ^ n)"
      by (simp add: norm_mult abs_mult)
    also have "… ≤ K * norm (z ^ n)"
      by (simp only: mult_right_mono 4 norm_ge_zero)
    also have "… = K * norm (z ^ n) * (inverse (norm (x ^ n)) * norm (x ^ n))"
      by (simp add: x_neq_0)
    also have "… = K * norm (z ^ n) * inverse (norm (x ^ n)) * norm (x ^ n)"
      by (simp only: mult_assoc)
    finally show "norm (norm (f n * z ^ n)) ≤
                  K * norm (z ^ n) * inverse (norm (x ^ n))"
      by (simp add: mult_le_cancel_right x_neq_0)
  qed
  moreover have "summable (λn. K * norm (z ^ n) * inverse (norm (x ^ n)))"
  proof -
    from 2 have "norm (norm (z * inverse x)) < 1"
      using x_neq_0
      by (simp add: nonzero_norm_divide divide_inverse [symmetric])
    hence "summable (λn. norm (z * inverse x) ^ n)"
      by (rule summable_geometric)
    hence "summable (λn. K * norm (z * inverse x) ^ n)"
      by (rule summable_mult)
    thus "summable (λn. K * norm (z ^ n) * inverse (norm (x ^ n)))"
      using x_neq_0
      by (simp add: norm_mult nonzero_norm_inverse power_mult_distrib
                    power_inverse norm_power mult_assoc)
  qed
  ultimately show "summable (λn. norm (f n * z ^ n))"
    by (rule summable_comparison_test)
qed

lemma powser_inside:
  fixes f :: "nat => 'a::{real_normed_field,banach,recpower}" shows
     "[| summable (%n. f(n) * (x ^ n)); norm z < norm x |]  
      ==> summable (%n. f(n) * (z ^ n))"
by (rule powser_insidea [THEN summable_norm_cancel])

lemma sum_split_even_odd: fixes f :: "nat => real" shows
  "(∑ i = 0 ..< 2 * n. if even i then f i else g i) = 
   (∑ i = 0 ..< n. f (2 * i)) + (∑ i = 0 ..< n. g (2 * i + 1))"
proof (induct n)
  case (Suc n)
  have "(∑ i = 0 ..< 2 * Suc n. if even i then f i else g i) = 
        (∑ i = 0 ..< n. f (2 * i)) + (∑ i = 0 ..< n. g (2 * i + 1)) + (f (2 * n) + g (2 * n + 1))"
    using Suc.hyps unfolding One_nat_def by auto
  also have "… = (∑ i = 0 ..< Suc n. f (2 * i)) + (∑ i = 0 ..< Suc n. g (2 * i + 1))" by auto
  finally show ?case .
qed auto

lemma sums_if': fixes g :: "nat => real" assumes "g sums x"
  shows "(λ n. if even n then 0 else g ((n - 1) div 2)) sums x"
  unfolding sums_def
proof (rule LIMSEQ_I)
  fix r :: real assume "0 < r"
  from `g sums x`[unfolded sums_def, THEN LIMSEQ_D, OF this]
  obtain no where no_eq: "!! n. n ≥ no ==> (norm (setsum g { 0..<n } - x) < r)" by blast

  let ?SUM = "λ m. ∑ i = 0 ..< m. if even i then 0 else g ((i - 1) div 2)"
  { fix m assume "m ≥ 2 * no" hence "m div 2 ≥ no" by auto
    have sum_eq: "?SUM (2 * (m div 2)) = setsum g { 0 ..< m div 2 }" 
      using sum_split_even_odd by auto
    hence "(norm (?SUM (2 * (m div 2)) - x) < r)" using no_eq unfolding sum_eq using `m div 2 ≥ no` by auto
    moreover
    have "?SUM (2 * (m div 2)) = ?SUM m"
    proof (cases "even m")
      case True show ?thesis unfolding even_nat_div_two_times_two[OF True, unfolded numeral_2_eq_2[symmetric]] ..
    next
      case False hence "even (Suc m)" by auto
      from even_nat_div_two_times_two[OF this, unfolded numeral_2_eq_2[symmetric]] odd_nat_plus_one_div_two[OF False, unfolded numeral_2_eq_2[symmetric]]
      have eq: "Suc (2 * (m div 2)) = m" by auto
      hence "even (2 * (m div 2))" using `odd m` by auto
      have "?SUM m = ?SUM (Suc (2 * (m div 2)))" unfolding eq ..
      also have "… = ?SUM (2 * (m div 2))" using `even (2 * (m div 2))` by auto
      finally show ?thesis by auto
    qed
    ultimately have "(norm (?SUM m - x) < r)" by auto
  }
  thus "∃ no. ∀ m ≥ no. norm (?SUM m - x) < r" by blast
qed

lemma sums_if: fixes g :: "nat => real" assumes "g sums x" and "f sums y"
  shows "(λ n. if even n then f (n div 2) else g ((n - 1) div 2)) sums (x + y)"
proof -
  let ?s = "λ n. if even n then 0 else f ((n - 1) div 2)"
  { fix B T E have "(if B then (0 :: real) else E) + (if B then T else 0) = (if B then T else E)"
      by (cases B) auto } note if_sum = this
  have g_sums: "(λ n. if even n then 0 else g ((n - 1) div 2)) sums x" using sums_if'[OF `g sums x`] .
  { 
    have "?s 0 = 0" by auto
    have Suc_m1: "!! n. Suc n - 1 = n" by auto
    { fix B T E have "(if ¬ B then T else E) = (if B then E else T)" by auto } note if_eq = this

    have "?s sums y" using sums_if'[OF `f sums y`] .
    from this[unfolded sums_def, THEN LIMSEQ_Suc] 
    have "(λ n. if even n then f (n div 2) else 0) sums y"
      unfolding sums_def setsum_shift_lb_Suc0_0_upt[where f="?s", OF `?s 0 = 0`, symmetric]
                image_Suc_atLeastLessThan[symmetric] setsum_reindex[OF inj_Suc, unfolded comp_def]
                even_nat_Suc Suc_m1 if_eq .
  } from sums_add[OF g_sums this]
  show ?thesis unfolding if_sum .
qed

subsection {* Alternating series test / Leibniz formula *}

lemma sums_alternating_upper_lower:
  fixes a :: "nat => real"
  assumes mono: "!!n. a (Suc n) ≤ a n" and a_pos: "!!n. 0 ≤ a n" and "a ----> 0"
  shows "∃l. ((∀n. (∑i=0..<2*n. -1^i*a i) ≤ l) ∧ (λ n. ∑i=0..<2*n. -1^i*a i) ----> l) ∧ 
             ((∀n. l ≤ (∑i=0..<2*n + 1. -1^i*a i)) ∧ (λ n. ∑i=0..<2*n + 1. -1^i*a i) ----> l)"
  (is "∃l. ((∀n. ?f n ≤ l) ∧ _) ∧ ((∀n. l ≤ ?g n) ∧ _)")
proof -
  have fg_diff: "!!n. ?f n - ?g n = - a (2 * n)" unfolding One_nat_def by auto

  have "∀ n. ?f n ≤ ?f (Suc n)"
  proof fix n show "?f n ≤ ?f (Suc n)" using mono[of "2*n"] by auto qed
  moreover
  have "∀ n. ?g (Suc n) ≤ ?g n"
  proof fix n show "?g (Suc n) ≤ ?g n" using mono[of "Suc (2*n)"]
    unfolding One_nat_def by auto qed
  moreover
  have "∀ n. ?f n ≤ ?g n" 
  proof fix n show "?f n ≤ ?g n" using fg_diff a_pos
    unfolding One_nat_def by auto qed
  moreover
  have "(λ n. ?f n - ?g n) ----> 0" unfolding fg_diff
  proof (rule LIMSEQ_I)
    fix r :: real assume "0 < r"
    with `a ----> 0`[THEN LIMSEQ_D] 
    obtain N where "!! n. n ≥ N ==> norm (a n - 0) < r" by auto
    hence "∀ n ≥ N. norm (- a (2 * n) - 0) < r" by auto
    thus "∃ N. ∀ n ≥ N. norm (- a (2 * n) - 0) < r" by auto
  qed
  ultimately
  show ?thesis by (rule lemma_nest_unique)
qed 

lemma summable_Leibniz': fixes a :: "nat => real"
  assumes a_zero: "a ----> 0" and a_pos: "!! n. 0 ≤ a n"
  and a_monotone: "!! n. a (Suc n) ≤ a n"
  shows summable: "summable (λ n. (-1)^n * a n)"
  and "!!n. (∑i=0..<2*n. (-1)^i*a i) ≤ (∑i. (-1)^i*a i)"
  and "(λn. ∑i=0..<2*n. (-1)^i*a i) ----> (∑i. (-1)^i*a i)"
  and "!!n. (∑i. (-1)^i*a i) ≤ (∑i=0..<2*n+1. (-1)^i*a i)"
  and "(λn. ∑i=0..<2*n+1. (-1)^i*a i) ----> (∑i. (-1)^i*a i)"
proof -
  let "?S n" = "(-1)^n * a n"
  let "?P n" = "∑i=0..<n. ?S i"
  let "?f n" = "?P (2 * n)"
  let "?g n" = "?P (2 * n + 1)"
  obtain l :: real where below_l: "∀ n. ?f n ≤ l" and "?f ----> l" and above_l: "∀ n. l ≤ ?g n" and "?g ----> l"
    using sums_alternating_upper_lower[OF a_monotone a_pos a_zero] by blast
  
  let ?Sa = "λ m. ∑ n = 0..<m. ?S n"
  have "?Sa ----> l"
  proof (rule LIMSEQ_I)
    fix r :: real assume "0 < r"

    with `?f ----> l`[THEN LIMSEQ_D] 
    obtain f_no where f: "!! n. n ≥ f_no ==> norm (?f n - l) < r" by auto

    from `0 < r` `?g ----> l`[THEN LIMSEQ_D] 
    obtain g_no where g: "!! n. n ≥ g_no ==> norm (?g n - l) < r" by auto

    { fix n :: nat
      assume "n ≥ (max (2 * f_no) (2 * g_no))" hence "n ≥ 2 * f_no" and "n ≥ 2 * g_no" by auto
      have "norm (?Sa n - l) < r"
      proof (cases "even n")
        case True from even_nat_div_two_times_two[OF this]
        have n_eq: "2 * (n div 2) = n" unfolding numeral_2_eq_2[symmetric] by auto
        with `n ≥ 2 * f_no` have "n div 2 ≥ f_no" by auto
        from f[OF this]
        show ?thesis unfolding n_eq atLeastLessThanSuc_atLeastAtMost .
      next
        case False hence "even (n - 1)" using even_num_iff odd_pos by auto 
        from even_nat_div_two_times_two[OF this]
        have n_eq: "2 * ((n - 1) div 2) = n - 1" unfolding numeral_2_eq_2[symmetric] by auto
        hence range_eq: "n - 1 + 1 = n" using odd_pos[OF False] by auto

        from n_eq `n ≥ 2 * g_no` have "(n - 1) div 2 ≥ g_no" by auto
        from g[OF this]
        show ?thesis unfolding n_eq atLeastLessThanSuc_atLeastAtMost range_eq .
      qed
    }
    thus "∃ no. ∀ n ≥ no. norm (?Sa n - l) < r" by blast
  qed
  hence sums_l: "(λi. (-1)^i * a i) sums l" unfolding sums_def atLeastLessThanSuc_atLeastAtMost[symmetric] .
  thus "summable ?S" using summable_def by auto

  have "l = suminf ?S" using sums_unique[OF sums_l] .

  { fix n show "suminf ?S ≤ ?g n" unfolding sums_unique[OF sums_l, symmetric] using above_l by auto }
  { fix n show "?f n ≤ suminf ?S" unfolding sums_unique[OF sums_l, symmetric] using below_l by auto }
  show "?g ----> suminf ?S" using `?g ----> l` `l = suminf ?S` by auto
  show "?f ----> suminf ?S" using `?f ----> l` `l = suminf ?S` by auto
qed

theorem summable_Leibniz: fixes a :: "nat => real"
  assumes a_zero: "a ----> 0" and "monoseq a"
  shows "summable (λ n. (-1)^n * a n)" (is "?summable")
  and "0 < a 0 --> (∀n. (∑i. -1^i*a i) ∈ { ∑i=0..<2*n. -1^i * a i .. ∑i=0..<2*n+1. -1^i * a i})" (is "?pos")
  and "a 0 < 0 --> (∀n. (∑i. -1^i*a i) ∈ { ∑i=0..<2*n+1. -1^i * a i .. ∑i=0..<2*n. -1^i * a i})" (is "?neg")
  and "(λn. ∑i=0..<2*n. -1^i*a i) ----> (∑i. -1^i*a i)" (is "?f")
  and "(λn. ∑i=0..<2*n+1. -1^i*a i) ----> (∑i. -1^i*a i)" (is "?g")
proof -
  have "?summable ∧ ?pos ∧ ?neg ∧ ?f ∧ ?g"
  proof (cases "(∀ n. 0 ≤ a n) ∧ (∀m. ∀n≥m. a n ≤ a m)")
    case True
    hence ord: "!!n m. m ≤ n ==> a n ≤ a m" and ge0: "!! n. 0 ≤ a n" by auto
    { fix n have "a (Suc n) ≤ a n" using ord[where n="Suc n" and m=n] by auto }
    note leibniz = summable_Leibniz'[OF `a ----> 0` ge0] and mono = this
    from leibniz[OF mono]
    show ?thesis using `0 ≤ a 0` by auto
  next
    let ?a = "λ n. - a n"
    case False
    with monoseq_le[OF `monoseq a` `a ----> 0`]
    have "(∀ n. a n ≤ 0) ∧ (∀m. ∀n≥m. a m ≤ a n)" by auto
    hence ord: "!!n m. m ≤ n ==> ?a n ≤ ?a m" and ge0: "!! n. 0 ≤ ?a n" by auto
    { fix n have "?a (Suc n) ≤ ?a n" using ord[where n="Suc n" and m=n] by auto }
    note monotone = this
    note leibniz = summable_Leibniz'[OF _ ge0, of "λx. x", OF LIMSEQ_minus[OF `a ----> 0`, unfolded minus_zero] monotone]
    have "summable (λ n. (-1)^n * ?a n)" using leibniz(1) by auto
    then obtain l where "(λ n. (-1)^n * ?a n) sums l" unfolding summable_def by auto
    from this[THEN sums_minus]
    have "(λ n. (-1)^n * a n) sums -l" by auto
    hence ?summable unfolding summable_def by auto
    moreover
    have "!! a b :: real. ¦ - a - - b ¦ = ¦a - b¦" unfolding minus_diff_minus by auto
    
    from suminf_minus[OF leibniz(1), unfolded mult_minus_right minus_minus]
    have move_minus: "(∑n. - (-1 ^ n * a n)) = - (∑n. -1 ^ n * a n)" by auto

    have ?pos using `0 ≤ ?a 0` by auto
    moreover have ?neg using leibniz(2,4) unfolding mult_minus_right setsum_negf move_minus neg_le_iff_le by auto
    moreover have ?f and ?g using leibniz(3,5)[unfolded mult_minus_right setsum_negf move_minus, THEN LIMSEQ_minus_cancel] by auto
    ultimately show ?thesis by auto
  qed
  from this[THEN conjunct1] this[THEN conjunct2, THEN conjunct1] this[THEN conjunct2, THEN conjunct2, THEN conjunct1] this[THEN conjunct2, THEN conjunct2, THEN conjunct2, THEN conjunct1]
       this[THEN conjunct2, THEN conjunct2, THEN conjunct2, THEN conjunct2]
  show ?summable and ?pos and ?neg and ?f and ?g .
qed

subsection {* Term-by-Term Differentiability of Power Series *}

definition
  diffs :: "(nat => 'a::ring_1) => nat => 'a" where
  "diffs c = (%n. of_nat (Suc n) * c(Suc n))"

text{*Lemma about distributing negation over it*}
lemma diffs_minus: "diffs (%n. - c n) = (%n. - diffs c n)"
by (simp add: diffs_def)

lemma sums_Suc_imp:
  assumes f: "f 0 = 0"
  shows "(λn. f (Suc n)) sums s ==> (λn. f n) sums s"
unfolding sums_def
apply (rule LIMSEQ_imp_Suc)
apply (subst setsum_shift_lb_Suc0_0_upt [where f=f, OF f, symmetric])
apply (simp only: setsum_shift_bounds_Suc_ivl)
done

lemma diffs_equiv:
     "summable (%n. (diffs c)(n) * (x ^ n)) ==>  
      (%n. of_nat n * c(n) * (x ^ (n - Suc 0))) sums  
         (∑n. (diffs c)(n) * (x ^ n))"
unfolding diffs_def
apply (drule summable_sums)
apply (rule sums_Suc_imp, simp_all)
done

lemma lemma_termdiff1:
  fixes z :: "'a :: {recpower,comm_ring}" shows
  "(∑p=0..<m. (((z + h) ^ (m - p)) * (z ^ p)) - (z ^ m)) =  
   (∑p=0..<m. (z ^ p) * (((z + h) ^ (m - p)) - (z ^ (m - p))))"
by(auto simp add: algebra_simps power_add [symmetric] cong: strong_setsum_cong)

lemma sumr_diff_mult_const2:
  "setsum f {0..<n} - of_nat n * (r::'a::ring_1) = (∑i = 0..<n. f i - r)"
by (simp add: setsum_subtractf)

lemma lemma_termdiff2:
  fixes h :: "'a :: {recpower,field}"
  assumes h: "h ≠ 0" shows
  "((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0) =
   h * (∑p=0..< n - Suc 0. ∑q=0..< n - Suc 0 - p.
        (z + h) ^ q * z ^ (n - 2 - q))" (is "?lhs = ?rhs")
apply (subgoal_tac "h * ?lhs = h * ?rhs", simp add: h)
apply (simp add: right_diff_distrib diff_divide_distrib h)
apply (simp add: mult_assoc [symmetric])
apply (cases "n", simp)
apply (simp add: lemma_realpow_diff_sumr2 h
                 right_diff_distrib [symmetric] mult_assoc
            del: power_Suc setsum_op_ivl_Suc of_nat_Suc)
apply (subst lemma_realpow_rev_sumr)
apply (subst sumr_diff_mult_const2)
apply simp
apply (simp only: lemma_termdiff1 setsum_right_distrib)
apply (rule setsum_cong [OF refl])
apply (simp add: diff_minus [symmetric] less_iff_Suc_add)
apply (clarify)
apply (simp add: setsum_right_distrib lemma_realpow_diff_sumr2 mult_ac
            del: setsum_op_ivl_Suc power_Suc)
apply (subst mult_assoc [symmetric], subst power_add [symmetric])
apply (simp add: mult_ac)
done

lemma real_setsum_nat_ivl_bounded2:
  fixes K :: "'a::ordered_semidom"
  assumes f: "!!p::nat. p < n ==> f p ≤ K"
  assumes K: "0 ≤ K"
  shows "setsum f {0..<n-k} ≤ of_nat n * K"
apply (rule order_trans [OF setsum_mono])
apply (rule f, simp)
apply (simp add: mult_right_mono K)
done

lemma lemma_termdiff3:
  fixes h z :: "'a::{real_normed_field,recpower}"
  assumes 1: "h ≠ 0"
  assumes 2: "norm z ≤ K"
  assumes 3: "norm (z + h) ≤ K"
  shows "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0))
          ≤ of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
proof -
  have "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0)) =
        norm (∑p = 0..<n - Suc 0. ∑q = 0..<n - Suc 0 - p.
          (z + h) ^ q * z ^ (n - 2 - q)) * norm h"
    apply (subst lemma_termdiff2 [OF 1])
    apply (subst norm_mult)
    apply (rule mult_commute)
    done
  also have "… ≤ of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2)) * norm h"
  proof (rule mult_right_mono [OF _ norm_ge_zero])
    from norm_ge_zero 2 have K: "0 ≤ K" by (rule order_trans)
    have le_Kn: "!!i j n. i + j = n ==> norm ((z + h) ^ i * z ^ j) ≤ K ^ n"
      apply (erule subst)
      apply (simp only: norm_mult norm_power power_add)
      apply (intro mult_mono power_mono 2 3 norm_ge_zero zero_le_power K)
      done
    show "norm (∑p = 0..<n - Suc 0. ∑q = 0..<n - Suc 0 - p.
              (z + h) ^ q * z ^ (n - 2 - q))
          ≤ of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2))"
      apply (intro
         order_trans [OF norm_setsum]
         real_setsum_nat_ivl_bounded2
         mult_nonneg_nonneg
         zero_le_imp_of_nat
         zero_le_power K)
      apply (rule le_Kn, simp)
      done
  qed
  also have "… = of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
    by (simp only: mult_assoc)
  finally show ?thesis .
qed

lemma lemma_termdiff4:
  fixes f :: "'a::{real_normed_field,recpower} =>
              'b::real_normed_vector"
  assumes k: "0 < (k::real)"
  assumes le: "!!h. [|h ≠ 0; norm h < k|] ==> norm (f h) ≤ K * norm h"
  shows "f -- 0 --> 0"
unfolding LIM_def diff_0_right
proof (safe)
  let ?h = "of_real (k / 2)::'a"
  have "?h ≠ 0" and "norm ?h < k" using k by simp_all
  hence "norm (f ?h) ≤ K * norm ?h" by (rule le)
  hence "0 ≤ K * norm ?h" by (rule order_trans [OF norm_ge_zero])
  hence zero_le_K: "0 ≤ K" using k by (simp add: zero_le_mult_iff)

  fix r::real assume r: "0 < r"
  show "∃s. 0 < s ∧ (∀x. x ≠ 0 ∧ norm x < s --> norm (f x) < r)"
  proof (cases)
    assume "K = 0"
    with k r le have "0 < k ∧ (∀x. x ≠ 0 ∧ norm x < k --> norm (f x) < r)"
      by simp
    thus "∃s. 0 < s ∧ (∀x. x ≠ 0 ∧ norm x < s --> norm (f x) < r)" ..
  next
    assume K_neq_zero: "K ≠ 0"
    with zero_le_K have K: "0 < K" by simp
    show "∃s. 0 < s ∧ (∀x. x ≠ 0 ∧ norm x < s --> norm (f x) < r)"
    proof (rule exI, safe)
      from k r K show "0 < min k (r * inverse K / 2)"
        by (simp add: mult_pos_pos positive_imp_inverse_positive)
    next
      fix x::'a
      assume x1: "x ≠ 0" and x2: "norm x < min k (r * inverse K / 2)"
      from x2 have x3: "norm x < k" and x4: "norm x < r * inverse K / 2"
        by simp_all
      from x1 x3 le have "norm (f x) ≤ K * norm x" by simp
      also from x4 K have "K * norm x < K * (r * inverse K / 2)"
        by (rule mult_strict_left_mono)
      also have "… = r / 2"
        using K_neq_zero by simp
      also have "r / 2 < r"
        using r by simp
      finally show "norm (f x) < r" .
    qed
  qed
qed

lemma lemma_termdiff5:
  fixes g :: "'a::{recpower,real_normed_field} =>
              nat => 'b::banach"
  assumes k: "0 < (k::real)"
  assumes f: "summable f"
  assumes le: "!!h n. [|h ≠ 0; norm h < k|] ==> norm (g h n) ≤ f n * norm h"
  shows "(λh. suminf (g h)) -- 0 --> 0"
proof (rule lemma_termdiff4 [OF k])
  fix h::'a assume "h ≠ 0" and "norm h < k"
  hence A: "∀n. norm (g h n) ≤ f n * norm h"
    by (simp add: le)
  hence "∃N. ∀n≥N. norm (norm (g h n)) ≤ f n * norm h"
    by simp
  moreover from f have B: "summable (λn. f n * norm h)"
    by (rule summable_mult2)
  ultimately have C: "summable (λn. norm (g h n))"
    by (rule summable_comparison_test)
  hence "norm (suminf (g h)) ≤ (∑n. norm (g h n))"
    by (rule summable_norm)
  also from A C B have "(∑n. norm (g h n)) ≤ (∑n. f n * norm h)"
    by (rule summable_le)
  also from f have "(∑n. f n * norm h) = suminf f * norm h"
    by (rule suminf_mult2 [symmetric])
  finally show "norm (suminf (g h)) ≤ suminf f * norm h" .
qed


text{* FIXME: Long proofs*}

lemma termdiffs_aux:
  fixes x :: "'a::{recpower,real_normed_field,banach}"
  assumes 1: "summable (λn. diffs (diffs c) n * K ^ n)"
  assumes 2: "norm x < norm K"
  shows "(λh. ∑n. c n * (((x + h) ^ n - x ^ n) / h
             - of_nat n * x ^ (n - Suc 0))) -- 0 --> 0"
proof -
  from dense [OF 2]
  obtain r where r1: "norm x < r" and r2: "r < norm K" by fast
  from norm_ge_zero r1 have r: "0 < r"
    by (rule order_le_less_trans)
  hence r_neq_0: "r ≠ 0" by simp
  show ?thesis
  proof (rule lemma_termdiff5)
    show "0 < r - norm x" using r1 by simp
  next
    from r r2 have "norm (of_real r::'a) < norm K"
      by simp
    with 1 have "summable (λn. norm (diffs (diffs c) n * (of_real r ^ n)))"
      by (rule powser_insidea)
    hence "summable (λn. diffs (diffs (λn. norm (c n))) n * r ^ n)"
      using r
      by (simp add: diffs_def norm_mult norm_power del: of_nat_Suc)
    hence "summable (λn. of_nat n * diffs (λn. norm (c n)) n * r ^ (n - Suc 0))"
      by (rule diffs_equiv [THEN sums_summable])
    also have "(λn. of_nat n * diffs (λn. norm (c n)) n * r ^ (n - Suc 0))
      = (λn. diffs (%m. of_nat (m - Suc 0) * norm (c m) * inverse r) n * (r ^ n))"
      apply (rule ext)
      apply (simp add: diffs_def)
      apply (case_tac n, simp_all add: r_neq_0)
      done
    finally have "summable 
      (λn. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0))"
      by (rule diffs_equiv [THEN sums_summable])
    also have
      "(λn. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) *
           r ^ (n - Suc 0)) =
       (λn. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))"
      apply (rule ext)
      apply (case_tac "n", simp)
      apply (case_tac "nat", simp)
      apply (simp add: r_neq_0)
      done
    finally show
      "summable (λn. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))" .
  next
    fix h::'a and n::nat
    assume h: "h ≠ 0"
    assume "norm h < r - norm x"
    hence "norm x + norm h < r" by simp
    with norm_triangle_ineq have xh: "norm (x + h) < r"
      by (rule order_le_less_trans)
    show "norm (c n * (((x + h) ^ n - x ^ n) / h - of_nat n * x ^ (n - Suc 0)))
          ≤ norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2) * norm h"
      apply (simp only: norm_mult mult_assoc)
      apply (rule mult_left_mono [OF _ norm_ge_zero])
      apply (simp (no_asm) add: mult_assoc [symmetric])
      apply (rule lemma_termdiff3)
      apply (rule h)
      apply (rule r1 [THEN order_less_imp_le])
      apply (rule xh [THEN order_less_imp_le])
      done
  qed
qed

lemma termdiffs:
  fixes K x :: "'a::{recpower,real_normed_field,banach}"
  assumes 1: "summable (λn. c n * K ^ n)"
  assumes 2: "summable (λn. (diffs c) n * K ^ n)"
  assumes 3: "summable (λn. (diffs (diffs c)) n * K ^ n)"
  assumes 4: "norm x < norm K"
  shows "DERIV (λx. ∑n. c n * x ^ n) x :> (∑n. (diffs c) n * x ^ n)"
unfolding deriv_def
proof (rule LIM_zero_cancel)
  show "(λh. (suminf (λn. c n * (x + h) ^ n) - suminf (λn. c n * x ^ n)) / h
            - suminf (λn. diffs c n * x ^ n)) -- 0 --> 0"
  proof (rule LIM_equal2)
    show "0 < norm K - norm x" using 4 by (simp add: less_diff_eq)
  next
    fix h :: 'a
    assume "h ≠ 0"
    assume "norm (h - 0) < norm K - norm x"
    hence "norm x + norm h < norm K" by simp
    hence 5: "norm (x + h) < norm K"
      by (rule norm_triangle_ineq [THEN order_le_less_trans])
    have A: "summable (λn. c n * x ^ n)"
      by (rule powser_inside [OF 1 4])
    have B: "summable (λn. c n * (x + h) ^ n)"
      by (rule powser_inside [OF 1 5])
    have C: "summable (λn. diffs c n * x ^ n)"
      by (rule powser_inside [OF 2 4])
    show "((∑n. c n * (x + h) ^ n) - (∑n. c n * x ^ n)) / h
             - (∑n. diffs c n * x ^ n) = 
          (∑n. c n * (((x + h) ^ n - x ^ n) / h - of_nat n * x ^ (n - Suc 0)))"
      apply (subst sums_unique [OF diffs_equiv [OF C]])
      apply (subst suminf_diff [OF B A])
      apply (subst suminf_divide [symmetric])
      apply (rule summable_diff [OF B A])
      apply (subst suminf_diff)
      apply (rule summable_divide)
      apply (rule summable_diff [OF B A])
      apply (rule sums_summable [OF diffs_equiv [OF C]])
      apply (rule arg_cong [where f="suminf"], rule ext)
      apply (simp add: algebra_simps)
      done
  next
    show "(λh. ∑n. c n * (((x + h) ^ n - x ^ n) / h -
               of_nat n * x ^ (n - Suc 0))) -- 0 --> 0"
        by (rule termdiffs_aux [OF 3 4])
  qed
qed


subsection{* Some properties of factorials *}

lemma real_of_nat_fact_not_zero [simp]: "real (fact n) ≠ 0"
by auto

lemma real_of_nat_fact_gt_zero [simp]: "0 < real(fact n)"
by auto

lemma real_of_nat_fact_ge_zero [simp]: "0 ≤ real(fact n)"
by simp

lemma inv_real_of_nat_fact_gt_zero [simp]: "0 < inverse (real (fact n))"
by (auto simp add: positive_imp_inverse_positive)

lemma inv_real_of_nat_fact_ge_zero [simp]: "0 ≤ inverse (real (fact n))"
by (auto intro: order_less_imp_le)

subsection {* Derivability of power series *}

lemma DERIV_series': fixes f :: "real => nat => real"
  assumes DERIV_f: "!! n. DERIV (λ x. f x n) x0 :> (f' x0 n)"
  and allf_summable: "!! x. x ∈ {a <..< b} ==> summable (f x)" and x0_in_I: "x0 ∈ {a <..< b}"
  and "summable (f' x0)"
  and "summable L" and L_def: "!! n x y. [| x ∈ { a <..< b} ; y ∈ { a <..< b} |] ==> ¦ f x n - f y n ¦ ≤ L n * ¦ x - y ¦"
  shows "DERIV (λ x. suminf (f x)) x0 :> (suminf (f' x0))"
  unfolding deriv_def
proof (rule LIM_I)
  fix r :: real assume "0 < r" hence "0 < r/3" by auto

  obtain N_L where N_L: "!! n. N_L ≤ n ==> ¦ ∑ i. L (i + n) ¦ < r/3" 
    using suminf_exist_split[OF `0 < r/3` `summable L`] by auto

  obtain N_f' where N_f': "!! n. N_f' ≤ n ==> ¦ ∑ i. f' x0 (i + n) ¦ < r/3" 
    using suminf_exist_split[OF `0 < r/3` `summable (f' x0)`] by auto

  let ?N = "Suc (max N_L N_f')"
  have "¦ ∑ i. f' x0 (i + ?N) ¦ < r/3" (is "?f'_part < r/3") and
    L_estimate: "¦ ∑ i. L (i + ?N) ¦ < r/3" using N_L[of "?N"] and N_f' [of "?N"] by auto

  let "?diff i x" = "(f (x0 + x) i - f x0 i) / x"

  let ?r = "r / (3 * real ?N)"
  have "0 < 3 * real ?N" by auto
  from divide_pos_pos[OF `0 < r` this]
  have "0 < ?r" .

  let "?s n" = "SOME s. 0 < s ∧ (∀ x. x ≠ 0 ∧ ¦ x ¦ < s --> ¦ ?diff n x - f' x0 n ¦ < ?r)"
  def S'  "Min (?s ` { 0 ..< ?N })"

  have "0 < S'" unfolding S'_def
  proof (rule iffD2[OF Min_gr_iff])
    show "∀ x ∈ (?s ` { 0 ..< ?N }). 0 < x"
    proof (rule ballI)
      fix x assume "x ∈ ?s ` {0..<?N}"
      then obtain n where "x = ?s n" and "n ∈ {0..<?N}" using image_iff[THEN iffD1] by blast
      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def] 
      obtain s where s_bound: "0 < s ∧ (∀x. x ≠ 0 ∧ ¦x¦ < s --> ¦?diff n x - f' x0 n¦ < ?r)" by auto
      have "0 < ?s n" by (rule someI2[where a=s], auto simp add: s_bound)
      thus "0 < x" unfolding `x = ?s n` .
    qed
  qed auto

  def S  "min (min (x0 - a) (b - x0)) S'"
  hence "0 < S" and S_a: "S ≤ x0 - a" and S_b: "S ≤ b - x0" and "S ≤ S'" using x0_in_I and `0 < S'`
    by auto

  { fix x assume "x ≠ 0" and "¦ x ¦ < S"
    hence x_in_I: "x0 + x ∈ { a <..< b }" using S_a S_b by auto
    
    note diff_smbl = summable_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
    note div_smbl = summable_divide[OF diff_smbl]
    note all_smbl = summable_diff[OF div_smbl `summable (f' x0)`]
    note ign = summable_ignore_initial_segment[where k="?N"]
    note diff_shft_smbl = summable_diff[OF ign[OF allf_summable[OF x_in_I]] ign[OF allf_summable[OF x0_in_I]]]
    note div_shft_smbl = summable_divide[OF diff_shft_smbl]
    note all_shft_smbl = summable_diff[OF div_smbl ign[OF `summable (f' x0)`]]

    { fix n
      have "¦ ?diff (n + ?N) x ¦ ≤ L (n + ?N) * ¦ (x0 + x) - x0 ¦ / ¦ x ¦" 
        using divide_right_mono[OF L_def[OF x_in_I x0_in_I] abs_ge_zero] unfolding abs_divide .
      hence "¦ ( ¦ ?diff (n + ?N) x ¦) ¦ ≤ L (n + ?N)" using `x ≠ 0` by auto
    } note L_ge = summable_le2[OF allI[OF this] ign[OF `summable L`]]
    from order_trans[OF summable_rabs[OF conjunct1[OF L_ge]] L_ge[THEN conjunct2]]
    have "¦ ∑ i. ?diff (i + ?N) x ¦ ≤ (∑ i. L (i + ?N))" .
    hence "¦ ∑ i. ?diff (i + ?N) x ¦ ≤ r / 3" (is "?L_part ≤ r/3") using L_estimate by auto

    have "¦∑n ∈ { 0 ..< ?N}. ?diff n x - f' x0 n ¦ ≤ (∑n ∈ { 0 ..< ?N}. ¦?diff n x - f' x0 n ¦)" ..
    also have "… < (∑n ∈ { 0 ..< ?N}. ?r)"
    proof (rule setsum_strict_mono)
      fix n assume "n ∈ { 0 ..< ?N}"
      have "¦ x ¦ < S" using `¦ x ¦ < S` .
      also have "S ≤ S'" using `S ≤ S'` .
      also have "S' ≤ ?s n" unfolding S'_def 
      proof (rule Min_le_iff[THEN iffD2])
        have "?s n ∈ (?s ` {0..<?N}) ∧ ?s n ≤ ?s n" using `n ∈ { 0 ..< ?N}` by auto
        thus "∃ a ∈ (?s ` {0..<?N}). a ≤ ?s n" by blast
      qed auto
      finally have "¦ x ¦ < ?s n" .

      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def diff_0_right, unfolded some_eq_ex[symmetric], THEN conjunct2]
      have "∀x. x ≠ 0 ∧ ¦x¦ < ?s n --> ¦?diff n x - f' x0 n¦ < ?r" .
      with `x ≠ 0` and `¦x¦ < ?s n`
      show "¦?diff n x - f' x0 n¦ < ?r" by blast
    qed auto
    also have "… = of_nat (card {0 ..< ?N}) * ?r" by (rule setsum_constant)
    also have "… = real ?N * ?r" unfolding real_eq_of_nat by auto
    also have "… = r/3" by auto
    finally have "¦∑n ∈ { 0 ..< ?N}. ?diff n x - f' x0 n ¦ < r / 3" (is "?diff_part < r / 3") .

    from suminf_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
    have "¦ (suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0) ¦ = 
                    ¦ ∑n. ?diff n x - f' x0 n ¦" unfolding suminf_diff[OF div_smbl `summable (f' x0)`, symmetric] using suminf_divide[OF diff_smbl, symmetric] by auto
    also have "… ≤ ?diff_part + ¦ (∑n. ?diff (n + ?N) x) - (∑ n. f' x0 (n + ?N)) ¦" unfolding suminf_split_initial_segment[OF all_smbl, where k="?N"] unfolding suminf_diff[OF div_shft_smbl ign[OF `summable (f' x0)`]] by (rule abs_triangle_ineq)
    also have "… ≤ ?diff_part + ?L_part + ?f'_part" using abs_triangle_ineq4 by auto
    also have "… < r /3 + r/3 + r/3" 
      using `?diff_part < r/3` `?L_part ≤ r/3` and `?f'_part < r/3` by auto
    finally have "¦ (suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0) ¦ < r"
      by auto
  } thus "∃ s > 0. ∀ x. x ≠ 0 ∧ norm (x - 0) < s --> 
      norm (((∑n. f (x0 + x) n) - (∑n. f x0 n)) / x - (∑n. f' x0 n)) < r" using `0 < S`
    unfolding real_norm_def diff_0_right by blast
qed

lemma DERIV_power_series': fixes f :: "nat => real"
  assumes converges: "!! x. x ∈ {-R <..< R} ==> summable (λ n. f n * real (Suc n) * x^n)"
  and x0_in_I: "x0 ∈ {-R <..< R}" and "0 < R"
  shows "DERIV (λ x. (∑ n. f n * x^(Suc n))) x0 :> (∑ n. f n * real (Suc n) * x0^n)"
  (is "DERIV (λ x. (suminf (?f x))) x0 :> (suminf (?f' x0))")
proof -
  { fix R' assume "0 < R'" and "R' < R" and "-R' < x0" and "x0 < R'"
    hence "x0 ∈ {-R' <..< R'}" and "R' ∈ {-R <..< R}" and "x0 ∈ {-R <..< R}" by auto
    have "DERIV (λ x. (suminf (?f x))) x0 :> (suminf (?f' x0))"
    proof (rule DERIV_series')
      show "summable (λ n. ¦f n * real (Suc n) * R'^n¦)"
      proof -
        have "(R' + R) / 2 < R" and "0 < (R' + R) / 2" using `0 < R'` `0 < R` `R' < R` by auto
        hence in_Rball: "(R' + R) / 2 ∈ {-R <..< R}" using `R' < R` by auto
        have "norm R' < norm ((R' + R) / 2)" using `0 < R'` `0 < R` `R' < R` by auto
        from powser_insidea[OF converges[OF in_Rball] this] show ?thesis by auto
      qed
      { fix n x y assume "x ∈ {-R' <..< R'}" and "y ∈ {-R' <..< R'}"
        show "¦?f x n - ?f y n¦ ≤ ¦f n * real (Suc n) * R'^n¦ * ¦x-y¦"
        proof -
          have "¦f n * x ^ (Suc n) - f n * y ^ (Suc n)¦ = (¦f n¦ * ¦x-y¦) * ¦∑p = 0..<Suc n. x ^ p * y ^ (n - p)¦" 
            unfolding right_diff_distrib[symmetric] lemma_realpow_diff_sumr2 abs_mult by auto
          also have "… ≤ (¦f n¦ * ¦x-y¦) * (¦real (Suc n)¦ * ¦R' ^ n¦)" 
          proof (rule mult_left_mono)
            have "¦∑p = 0..<Suc n. x ^ p * y ^ (n - p)¦ ≤ (∑p = 0..<Suc n. ¦x ^ p * y ^ (n - p)¦)" by (rule setsum_abs)
            also have "… ≤ (∑p = 0..<Suc n. R' ^ n)"
            proof (rule setsum_mono)
              fix p assume "p ∈ {0..<Suc n}" hence "p ≤ n" by auto
              { fix n fix x :: real assume "x ∈ {-R'<..<R'}"
               hence "¦x¦ ≤ R'"  by auto
               hence "¦x^n¦ ≤ R'^n" unfolding power_abs by (rule power_mono, auto)
              } from mult_mono[OF this[OF `x ∈ {-R'<..<R'}`, of p] this[OF `y ∈ {-R'<..<R'}`, of "n-p"]] `0 < R'`
              have "¦x^p * y^(n-p)¦ ≤ R'^p * R'^(n-p)" unfolding abs_mult by auto
              thus "¦x^p * y^(n-p)¦ ≤ R'^n" unfolding power_add[symmetric] using `p ≤ n` by auto
            qed
            also have "… = real (Suc n) * R' ^ n" unfolding setsum_constant card_atLeastLessThan real_of_nat_def by auto
            finally show "¦∑p = 0..<Suc n. x ^ p * y ^ (n - p)¦ ≤ ¦real (Suc n)¦ * ¦R' ^ n¦" unfolding abs_real_of_nat_cancel abs_of_nonneg[OF zero_le_power[OF less_imp_le[OF `0 < R'`]]] .
            show "0 ≤ ¦f n¦ * ¦x - y¦" unfolding abs_mult[symmetric] by auto
          qed
          also have "… = ¦f n * real (Suc n) * R' ^ n¦ * ¦x - y¦" unfolding abs_mult real_mult_assoc[symmetric] by algebra
          finally show ?thesis .
        qed }
      { fix n
        from DERIV_pow[of "Suc n" x0, THEN DERIV_cmult[where c="f n"]]
        show "DERIV (λ x. ?f x n) x0 :> (?f' x0 n)" unfolding real_mult_assoc by auto }
      { fix x assume "x ∈ {-R' <..< R'}" hence "R' ∈ {-R <..< R}" and "norm x < norm R'" using assms `R' < R` by auto
        have "summable (λ n. f n * x^n)"
        proof (rule summable_le2[THEN conjunct1, OF _ powser_insidea[OF converges[OF `R' ∈ {-R <..< R}`] `norm x < norm R'`]], rule allI)
          fix n
          have le: "¦f n¦ * 1 ≤ ¦f n¦ * real (Suc n)" by (rule mult_left_mono, auto)
          show "¦f n * x ^ n¦ ≤ norm (f n * real (Suc n) * x ^ n)" unfolding real_norm_def abs_mult
            by (rule mult_right_mono, auto simp add: le[unfolded mult_1_right])
        qed
        from this[THEN summable_mult2[where c=x], unfolded real_mult_assoc, unfolded real_mult_commute]
        show "summable (?f x)" by auto }
      show "summable (?f' x0)" using converges[OF `x0 ∈ {-R <..< R}`] .
      show "x0 ∈ {-R' <..< R'}" using `x0 ∈ {-R' <..< R'}` .
    qed
  } note for_subinterval = this
  let ?R = "(R + ¦x0¦) / 2"
  have "¦x0¦ < ?R" using assms by auto
  hence "- ?R < x0"
  proof (cases "x0 < 0")
    case True
    hence "- x0 < ?R" using `¦x0¦ < ?R` by auto
    thus ?thesis unfolding neg_less_iff_less[symmetric, of "- x0"] by auto
  next
    case False
    have "- ?R < 0" using assms by auto
    also have "… ≤ x0" using False by auto 
    finally show ?thesis .
  qed
  hence "0 < ?R" "?R < R" "- ?R < x0" and "x0 < ?R" using assms by auto
  from for_subinterval[OF this]
  show ?thesis .
qed

subsection {* Exponential Function *}

definition
  exp :: "'a => 'a::{recpower,real_normed_field,banach}" where
  "exp x = (∑n. x ^ n /R real (fact n))"

lemma summable_exp_generic:
  fixes x :: "'a::{real_normed_algebra_1,recpower,banach}"
  defines S_def: "S ≡ λn. x ^ n /R real (fact n)"
  shows "summable S"
proof -
  have S_Suc: "!!n. S (Suc n) = (x * S n) /R real (Suc n)"
    unfolding S_def by (simp del: mult_Suc)
  obtain r :: real where r0: "0 < r" and r1: "r < 1"
    using dense [OF zero_less_one] by fast
  obtain N :: nat where N: "norm x < real N * r"
    using reals_Archimedean3 [OF r0] by fast
  from r1 show ?thesis
  proof (rule ratio_test [rule_format])
    fix n :: nat
    assume n: "N ≤ n"
    have "norm x ≤ real N * r"
      using N by (rule order_less_imp_le)
    also have "real N * r ≤ real (Suc n) * r"
      using r0 n by (simp add: mult_right_mono)
    finally have "norm x * norm (S n) ≤ real (Suc n) * r * norm (S n)"
      using norm_ge_zero by (rule mult_right_mono)
    hence "norm (x * S n) ≤ real (Suc n) * r * norm (S n)"
      by (rule order_trans [OF norm_mult_ineq])
    hence "norm (x * S n) / real (Suc n) ≤ r * norm (S n)"
      by (simp add: pos_divide_le_eq mult_ac)
    thus "norm (S (Suc n)) ≤ r * norm (S n)"
      by (simp add: S_Suc norm_scaleR inverse_eq_divide)
  qed
qed

lemma summable_norm_exp:
  fixes x :: "'a::{real_normed_algebra_1,recpower,banach}"
  shows "summable (λn. norm (x ^ n /R real (fact n)))"
proof (rule summable_norm_comparison_test [OF exI, rule_format])
  show "summable (λn. norm x ^ n /R real (fact n))"
    by (rule summable_exp_generic)
next
  fix n show "norm (x ^ n /R real (fact n)) ≤ norm x ^ n /R real (fact n)"
    by (simp add: norm_scaleR norm_power_ineq)
qed

lemma summable_exp: "summable (%n. inverse (real (fact n)) * x ^ n)"
by (insert summable_exp_generic [where x=x], simp)

lemma exp_converges: "(λn. x ^ n /R real (fact n)) sums exp x"
unfolding exp_def by (rule summable_exp_generic [THEN summable_sums])


lemma exp_fdiffs: 
      "diffs (%n. inverse(real (fact n))) = (%n. inverse(real (fact n)))"
by (simp add: diffs_def mult_assoc [symmetric] real_of_nat_def of_nat_mult
         del: mult_Suc of_nat_Suc)

lemma diffs_of_real: "diffs (λn. of_real (f n)) = (λn. of_real (diffs f n))"
by (simp add: diffs_def)

lemma lemma_exp_ext: "exp = (λx. ∑n. x ^ n /R real (fact n))"
by (auto intro!: ext simp add: exp_def)

lemma DERIV_exp [simp]: "DERIV exp x :> exp(x)"
apply (simp add: exp_def)
apply (subst lemma_exp_ext)
apply (subgoal_tac "DERIV (λu. ∑n. of_real (inverse (real (fact n))) * u ^ n) x :> (∑n. diffs (λn. of_real (inverse (real (fact n)))) n * x ^ n)")
apply (rule_tac [2] K = "of_real (1 + norm x)" in termdiffs)
apply (simp_all only: diffs_of_real scaleR_conv_of_real exp_fdiffs)
apply (rule exp_converges [THEN sums_summable, unfolded scaleR_conv_of_real])+
apply (simp del: of_real_add)
done

lemma isCont_exp [simp]: "isCont exp x"
by (rule DERIV_exp [THEN DERIV_isCont])


subsubsection {* Properties of the Exponential Function *}

lemma powser_zero:
  fixes f :: "nat => 'a::{real_normed_algebra_1,recpower}"
  shows "(∑n. f n * 0 ^ n) = f 0"
proof -
  have "(∑n = 0..<1. f n * 0 ^ n) = (∑n. f n * 0 ^ n)"
    by (rule sums_unique [OF series_zero], simp add: power_0_left)
  thus ?thesis unfolding One_nat_def by simp
qed

lemma exp_zero [simp]: "exp 0 = 1"
unfolding exp_def by (simp add: scaleR_conv_of_real powser_zero)

lemma setsum_cl_ivl_Suc2:
  "(∑i=m..Suc n. f i) = (if Suc n < m then 0 else f m + (∑i=m..n. f (Suc i)))"
by (simp add: setsum_head_Suc setsum_shift_bounds_cl_Suc_ivl
         del: setsum_cl_ivl_Suc)

lemma exp_series_add:
  fixes x y :: "'a::{real_field,recpower}"
  defines S_def: "S ≡ λx n. x ^ n /R real (fact n)"
  shows "S (x + y) n = (∑i=0..n. S x i * S y (n - i))"
proof (induct n)
  case 0
  show ?case
    unfolding S_def by simp
next
  case (Suc n)
  have S_Suc: "!!x n. S x (Suc n) = (x * S x n) /R real (Suc n)"
    unfolding S_def by (simp del: mult_Suc)
  hence times_S: "!!x n. x * S x n = real (Suc n) *R S x (Suc n)"
    by simp

  have "real (Suc n) *R S (x + y) (Suc n) = (x + y) * S (x + y) n"
    by (simp only: times_S)
  also have "… = (x + y) * (∑i=0..n. S x i * S y (n-i))"
    by (simp only: Suc)
  also have "… = x * (∑i=0..n. S x i * S y (n-i))
                + y * (∑i=0..n. S x i * S y (n-i))"
    by (rule left_distrib)
  also have "… = (∑i=0..n. (x * S x i) * S y (n-i))
                + (∑i=0..n. S x i * (y * S y (n-i)))"
    by (simp only: setsum_right_distrib mult_ac)
  also have "… = (∑i=0..n. real (Suc i) *R (S x (Suc i) * S y (n-i)))
                + (∑i=0..n. real (Suc n-i) *R (S x i * S y (Suc n-i)))"
    by (simp add: times_S Suc_diff_le)
  also have "(∑i=0..n. real (Suc i) *R (S x (Suc i) * S y (n-i))) =
             (∑i=0..Suc n. real i *R (S x i * S y (Suc n-i)))"
    by (subst setsum_cl_ivl_Suc2, simp)
  also have "(∑i=0..n. real (Suc n-i) *R (S x i * S y (Suc n-i))) =
             (∑i=0..Suc n. real (Suc n-i) *R (S x i * S y (Suc n-i)))"
    by (subst setsum_cl_ivl_Suc, simp)
  also have "(∑i=0..Suc n. real i *R (S x i * S y (Suc n-i))) +
             (∑i=0..Suc n. real (Suc n-i) *R (S x i * S y (Suc n-i))) =
             (∑i=0..Suc n. real (Suc n) *R (S x i * S y (Suc n-i)))"
    by (simp only: setsum_addf [symmetric] scaleR_left_distrib [symmetric]
              real_of_nat_add [symmetric], simp)
  also have "… = real (Suc n) *R (∑i=0..Suc n. S x i * S y (Suc n-i))"
    by (simp only: scaleR_right.setsum)
  finally show
    "S (x + y) (Suc n) = (∑i=0..Suc n. S x i * S y (Suc n - i))"
    by (simp add: scaleR_cancel_left del: setsum_cl_ivl_Suc)
qed

lemma exp_add: "exp (x + y) = exp x * exp y"
unfolding exp_def
by (simp only: Cauchy_product summable_norm_exp exp_series_add)

lemma mult_exp_exp: "exp x * exp y = exp (x + y)"
by (rule exp_add [symmetric])

lemma exp_of_real: "exp (of_real x) = of_real (exp x)"
unfolding exp_def
apply (subst of_real.suminf)
apply (rule summable_exp_generic)
apply (simp add: scaleR_conv_of_real)
done

lemma exp_not_eq_zero [simp]: "exp x ≠ 0"
proof
  have "exp x * exp (- x) = 1" by (simp add: mult_exp_exp)
  also assume "exp x = 0"
  finally show "False" by simp
qed

lemma exp_minus: "exp (- x) = inverse (exp x)"
by (rule inverse_unique [symmetric], simp add: mult_exp_exp)

lemma exp_diff: "exp (x - y) = exp x / exp y"
  unfolding diff_minus divide_inverse
  by (simp add: exp_add exp_minus)


subsubsection {* Properties of the Exponential Function on Reals *}

text {* Comparisons of @{term "exp x"} with zero. *}

text{*Proof: because every exponential can be seen as a square.*}
lemma exp_ge_zero [simp]: "0 ≤ exp (x::real)"
proof -
  have "0 ≤ exp (x/2) * exp (x/2)" by simp
  thus ?thesis by (simp add: exp_add [symmetric])
qed

lemma exp_gt_zero [simp]: "0 < exp (x::real)"
by (simp add: order_less_le)

lemma not_exp_less_zero [simp]: "¬ exp (x::real) < 0"
by (simp add: not_less)

lemma not_exp_le_zero [simp]: "¬ exp (x::real) ≤ 0"
by (simp add: not_le)

lemma abs_exp_cancel [simp]: "¦exp x::real¦ = exp x"
by simp

lemma exp_real_of_nat_mult: "exp(real n * x) = exp(x) ^ n"
apply (induct "n")
apply (auto simp add: real_of_nat_Suc right_distrib exp_add mult_commute)
done

text {* Strict monotonicity of exponential. *}

lemma exp_ge_add_one_self_aux: "0 ≤ (x::real) ==> (1 + x) ≤ exp(x)"
apply (drule order_le_imp_less_or_eq, auto)
apply (simp add: exp_def)
apply (rule real_le_trans)
apply (rule_tac [2] n = 2 and f = "(%n. inverse (real (fact n)) * x ^ n)" in series_pos_le)
apply (auto intro: summable_exp simp add: numeral_2_eq_2 zero_le_mult_iff)
done

lemma exp_gt_one: "0 < (x::real) ==> 1 < exp x"
proof -
  assume x: "0 < x"
  hence "1 < 1 + x" by simp
  also from x have "1 + x ≤ exp x"
    by (simp add: exp_ge_add_one_self_aux)
  finally show ?thesis .
qed

lemma exp_less_mono:
  fixes x y :: real
  assumes "x < y" shows "exp x < exp y"
proof -
  from `x < y` have "0 < y - x" by simp
  hence "1 < exp (y - x)" by (rule exp_gt_one)
  hence "1 < exp y / exp x" by (simp only: exp_diff)
  thus "exp x < exp y" by simp
qed

lemma exp_less_cancel: "exp (x::real) < exp y ==> x < y"
apply (simp add: linorder_not_le [symmetric])
apply (auto simp add: order_le_less exp_less_mono)
done

lemma exp_less_cancel_iff [iff]: "exp (x::real) < exp y <-> x < y"
by (auto intro: exp_less_mono exp_less_cancel)

lemma exp_le_cancel_iff [iff]: "exp (x::real) ≤ exp y <-> x ≤ y"
by (auto simp add: linorder_not_less [symmetric])

lemma exp_inj_iff [iff]: "exp (x::real) = exp y <-> x = y"
by (simp add: order_eq_iff)

text {* Comparisons of @{term "exp x"} with one. *}

lemma one_less_exp_iff [simp]: "1 < exp (x::real) <-> 0 < x"
  using exp_less_cancel_iff [where x=0 and y=x] by simp

lemma exp_less_one_iff [simp]: "exp (x::real) < 1 <-> x < 0"
  using exp_less_cancel_iff [where x=x and y=0] by simp

lemma one_le_exp_iff [simp]: "1 ≤ exp (x::real) <-> 0 ≤ x"
  using exp_le_cancel_iff [where x=0 and y=x] by simp

lemma exp_le_one_iff [simp]: "exp (x::real) ≤ 1 <-> x ≤ 0"
  using exp_le_cancel_iff [where x=x and y=0] by simp

lemma exp_eq_one_iff [simp]: "exp (x::real) = 1 <-> x = 0"
  using exp_inj_iff [where x=x and y=0] by simp

lemma lemma_exp_total: "1 ≤ y ==> ∃x. 0 ≤ x & x ≤ y - 1 & exp(x::real) = y"
apply (rule IVT)
apply (auto intro: isCont_exp simp add: le_diff_eq)
apply (subgoal_tac "1 + (y - 1) ≤ exp (y - 1)") 
apply simp
apply (rule exp_ge_add_one_self_aux, simp)
done

lemma exp_total: "0 < (y::real) ==> ∃x. exp x = y"
apply (rule_tac x = 1 and y = y in linorder_cases)
apply (drule order_less_imp_le [THEN lemma_exp_total])
apply (rule_tac [2] x = 0 in exI)
apply (frule_tac [3] real_inverse_gt_one)
apply (drule_tac [4] order_less_imp_le [THEN lemma_exp_total], auto)
apply (rule_tac x = "-x" in exI)
apply (simp add: exp_minus)
done


subsection {* Natural Logarithm *}

definition
  ln :: "real => real" where
  "ln x = (THE u. exp u = x)"

lemma ln_exp [simp]: "ln (exp x) = x"
by (simp add: ln_def)

lemma exp_ln [simp]: "0 < x ==> exp (ln x) = x"
by (auto dest: exp_total)

lemma exp_ln_iff [simp]: "exp (ln x) = x <-> 0 < x"
apply (rule iffI)
apply (erule subst, rule exp_gt_zero)
apply (erule exp_ln)
done

lemma ln_unique: "exp y = x ==> ln x = y"
by (erule subst, rule ln_exp)

lemma ln_one [simp]: "ln 1 = 0"
by (rule ln_unique, simp)

lemma ln_mult: "[|0 < x; 0 < y|] ==> ln (x * y) = ln x + ln y"
by (rule ln_unique, simp add: exp_add)

lemma ln_inverse: "0 < x ==> ln (inverse x) = - ln x"
by (rule ln_unique, simp add: exp_minus)

lemma ln_div: "[|0 < x; 0 < y|] ==> ln (x / y) = ln x - ln y"
by (rule ln_unique, simp add: exp_diff)

lemma ln_realpow: "0 < x ==> ln (x ^ n) = real n * ln x"
by (rule ln_unique, simp add: exp_real_of_nat_mult)

lemma ln_less_cancel_iff [simp]: "[|0 < x; 0 < y|] ==> ln x < ln y <-> x < y"
by (subst exp_less_cancel_iff [symmetric], simp)

lemma ln_le_cancel_iff [simp]: "[|0 < x; 0 < y|] ==> ln x ≤ ln y <-> x ≤ y"
by (simp add: linorder_not_less [symmetric])

lemma ln_inj_iff [simp]: "[|0 < x; 0 < y|] ==> ln x = ln y <-> x = y"
by (simp add: order_eq_iff)

lemma ln_add_one_self_le_self [simp]: "0 ≤ x ==> ln (1 + x) ≤ x"
apply (rule exp_le_cancel_iff [THEN iffD1])
apply (simp add: exp_ge_add_one_self_aux)
done

lemma ln_less_self [simp]: "0 < x ==> ln x < x"
by (rule order_less_le_trans [where y="ln (1 + x)"]) simp_all

lemma ln_ge_zero [simp]:
  assumes x: "1 ≤ x" shows "0 ≤ ln x"
proof -
  have "0 < x" using x by arith
  hence "exp 0 ≤ exp (ln x)"
    by (simp add: x)
  thus ?thesis by (simp only: exp_le_cancel_iff)
qed

lemma ln_ge_zero_imp_ge_one:
  assumes ln: "0 ≤ ln x" 
      and x:  "0 < x"
  shows "1 ≤ x"
proof -
  from ln have "ln 1 ≤ ln x" by simp
  thus ?thesis by (simp add: x del: ln_one) 
qed

lemma ln_ge_zero_iff [simp]: "0 < x ==> (0 ≤ ln x) = (1 ≤ x)"
by (blast intro: ln_ge_zero ln_ge_zero_imp_ge_one)

lemma ln_less_zero_iff [simp]: "0 < x ==> (ln x < 0) = (x < 1)"
by (insert ln_ge_zero_iff [of x], arith)

lemma ln_gt_zero:
  assumes x: "1 < x" shows "0 < ln x"
proof -
  have "0 < x" using x by arith
  hence "exp 0 < exp (ln x)" by (simp add: x)
  thus ?thesis  by (simp only: exp_less_cancel_iff)
qed

lemma ln_gt_zero_imp_gt_one:
  assumes ln: "0 < ln x" 
      and x:  "0 < x"
  shows "1 < x"
proof -
  from ln have "ln 1 < ln x" by simp
  thus ?thesis by (simp add: x del: ln_one) 
qed

lemma ln_gt_zero_iff [simp]: "0 < x ==> (0 < ln x) = (1 < x)"
by (blast intro: ln_gt_zero ln_gt_zero_imp_gt_one)

lemma ln_eq_zero_iff [simp]: "0 < x ==> (ln x = 0) = (x = 1)"
by (insert ln_less_zero_iff [of x] ln_gt_zero_iff [of x], arith)

lemma ln_less_zero: "[| 0 < x; x < 1 |] ==> ln x < 0"
by simp

lemma exp_ln_eq: "exp u = x ==> ln x = u"
by auto

lemma isCont_ln: "0 < x ==> isCont ln x"
apply (subgoal_tac "isCont ln (exp (ln x))", simp)
apply (rule isCont_inverse_function [where f=exp], simp_all)
done

lemma DERIV_ln: "0 < x ==> DERIV ln x :> inverse x"
apply (rule DERIV_inverse_function [where f=exp and a=0 and b="x+1"])
apply (erule lemma_DERIV_subst [OF DERIV_exp exp_ln])
apply (simp_all add: abs_if isCont_ln)
done

lemma ln_series: assumes "0 < x" and "x < 2"
  shows "ln x = (∑ n. (-1)^n * (1 / real (n + 1)) * (x - 1)^(Suc n))" (is "ln x = suminf (?f (x - 1))")
proof -
  let "?f' x n" = "(-1)^n * (x - 1)^n"

  have "ln x - suminf (?f (x - 1)) = ln 1 - suminf (?f (1 - 1))"
  proof (rule DERIV_isconst3[where x=x])
    fix x :: real assume "x ∈ {0 <..< 2}" hence "0 < x" and "x < 2" by auto
    have "norm (1 - x) < 1" using `0 < x` and `x < 2` by auto
    have "1 / x = 1 / (1 - (1 - x))" by auto
    also have "… = (∑ n. (1 - x)^n)" using geometric_sums[OF `norm (1 - x) < 1`] by (rule sums_unique)
    also have "… = suminf (?f' x)" unfolding power_mult_distrib[symmetric] by (rule arg_cong[where f=suminf], rule arg_cong[where f="op ^"], auto)
    finally have "DERIV ln x :> suminf (?f' x)" using DERIV_ln[OF `0 < x`] unfolding real_divide_def by auto
    moreover
    have repos: "!! h x :: real. h - 1 + x = h + x - 1" by auto
    have "DERIV (λx. suminf (?f x)) (x - 1) :> (∑n. (-1)^n * (1 / real (n + 1)) * real (Suc n) * (x - 1) ^ n)"
    proof (rule DERIV_power_series')
      show "x - 1 ∈ {- 1<..<1}" and "(0 :: real) < 1" using `0 < x` `x < 2` by auto
      { fix x :: real assume "x ∈ {- 1<..<1}" hence "norm (-x) < 1" by auto
        show "summable (λn. -1 ^ n * (1 / real (n + 1)) * real (Suc n) * x ^ n)"
          unfolding One_nat_def
          by (auto simp del: power_mult_distrib simp add: power_mult_distrib[symmetric] summable_geometric[OF `norm (-x) < 1`])
      }
    qed
    hence "DERIV (λx. suminf (?f x)) (x - 1) :> suminf (?f' x)" unfolding One_nat_def by auto
    hence "DERIV (λx. suminf (?f (x - 1))) x :> suminf (?f' x)" unfolding DERIV_iff repos .
    ultimately have "DERIV (λx. ln x - suminf (?f (x - 1))) x :> (suminf (?f' x) - suminf (?f' x))"
      by (rule DERIV_diff)
    thus "DERIV (λx. ln x - suminf (?f (x - 1))) x :> 0" by auto
  qed (auto simp add: assms)
  thus ?thesis by (auto simp add: suminf_zero)
qed

subsection {* Sine and Cosine *}

definition
  sin :: "real => real" where
  "sin x = (∑n. (if even(n) then 0 else
             (-1 ^ ((n - Suc 0) div 2))/(real (fact n))) * x ^ n)"
 
definition
  cos :: "real => real" where
  "cos x = (∑n. (if even(n) then (-1 ^ (n div 2))/(real (fact n)) 
                            else 0) * x ^ n)"

lemma summable_sin: 
     "summable (%n.  
           (if even n then 0  
           else -1 ^ ((n - Suc 0) div 2)/(real (fact n))) *  
                x ^ n)"
apply (rule_tac g = "(%n. inverse (real (fact n)) * ¦x¦ ^ n)" in summable_comparison_test)
apply (rule_tac [2] summable_exp)
apply (rule_tac x = 0 in exI)
apply (auto simp add: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
done

lemma summable_cos: 
      "summable (%n.  
           (if even n then  
           -1 ^ (n div 2)/(real (fact n)) else 0) * x ^ n)"
apply (rule_tac g = "(%n. inverse (real (fact n)) * ¦x¦ ^ n)" in summable_comparison_test)
apply (rule_tac [2] summable_exp)
apply (rule_tac x = 0 in exI)
apply (auto simp add: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
done

lemma lemma_STAR_sin:
     "(if even n then 0  
       else -1 ^ ((n - Suc 0) div 2)/(real (fact n))) * 0 ^ n = 0"
by (induct "n", auto)

lemma lemma_STAR_cos:
     "0 < n -->  
      -1 ^ (n div 2)/(real (fact n)) * 0 ^ n = 0"
by (induct "n", auto)

lemma lemma_STAR_cos1:
     "0 < n -->  
      (-1) ^ (n div 2)/(real (fact n)) * 0 ^ n = 0"
by (induct "n", auto)

lemma lemma_STAR_cos2:
  "(∑n=1..<n. if even n then -1 ^ (n div 2)/(real (fact n)) *  0 ^ n 
                         else 0) = 0"
apply (induct "n")
apply (case_tac [2] "n", auto)
done

lemma sin_converges: 
      "(%n. (if even n then 0  
            else -1 ^ ((n - Suc 0) div 2)/(real (fact n))) *  
                 x ^ n) sums sin(x)"
unfolding sin_def by (rule summable_sin [THEN summable_sums])

lemma cos_converges: 
      "(%n. (if even n then  
           -1 ^ (n div 2)/(real (fact n))  
           else 0) * x ^ n) sums cos(x)"
unfolding cos_def by (rule summable_cos [THEN summable_sums])

lemma sin_fdiffs: 
      "diffs(%n. if even n then 0  
           else -1 ^ ((n - Suc 0) div 2)/(real (fact n)))  
       = (%n. if even n then  
                 -1 ^ (n div 2)/(real (fact n))  
              else 0)"
by (auto intro!: ext 
         simp add: diffs_def divide_inverse real_of_nat_def of_nat_mult
         simp del: mult_Suc of_nat_Suc)

lemma sin_fdiffs2: 
       "diffs(%n. if even n then 0  
           else -1 ^ ((n - Suc 0) div 2)/(real (fact n))) n  
       = (if even n then  
                 -1 ^ (n div 2)/(real (fact n))  
              else 0)"
by (simp only: sin_fdiffs)

lemma cos_fdiffs: 
      "diffs(%n. if even n then  
                 -1 ^ (n div 2)/(real (fact n)) else 0)  
       = (%n. - (if even n then 0  
           else -1 ^ ((n - Suc 0)div 2)/(real (fact n))))"
by (auto intro!: ext 
         simp add: diffs_def divide_inverse odd_Suc_mult_two_ex real_of_nat_def of_nat_mult
         simp del: mult_Suc of_nat_Suc)


lemma cos_fdiffs2: 
      "diffs(%n. if even n then  
                 -1 ^ (n div 2)/(real (fact n)) else 0) n 
       = - (if even n then 0  
           else -1 ^ ((n - Suc 0)div 2)/(real (fact n)))"
by (simp only: cos_fdiffs)

text{*Now at last we can get the derivatives of exp, sin and cos*}

lemma lemma_sin_minus:
     "- sin x = (∑n. - ((if even n then 0 
                  else -1 ^ ((n - Suc 0) div 2)/(real (fact n))) * x ^ n))"
by (auto intro!: sums_unique sums_minus sin_converges)

lemma lemma_sin_ext:
     "sin = (%x. ∑n. 
                   (if even n then 0  
                       else -1 ^ ((n - Suc 0) div 2)/(real (fact n))) *  
                   x ^ n)"
by (auto intro!: ext simp add: sin_def)

lemma lemma_cos_ext:
     "cos = (%x. ∑n. 
                   (if even n then -1 ^ (n div 2)/(real (fact n)) else 0) *
                   x ^ n)"
by (auto intro!: ext simp add: cos_def)

lemma DERIV_sin [simp]: "DERIV sin x :> cos(x)"
apply (simp add: cos_def)
apply (subst lemma_sin_ext)
apply (auto simp add: sin_fdiffs2 [symmetric])
apply (rule_tac K = "1 + ¦x¦" in termdiffs)
apply (auto intro: sin_converges cos_converges sums_summable intro!: sums_minus [THEN sums_summable] simp add: cos_fdiffs sin_fdiffs)
done

lemma DERIV_cos [simp]: "DERIV cos x :> -sin(x)"
apply (subst lemma_cos_ext)
apply (auto simp add: lemma_sin_minus cos_fdiffs2 [symmetric] minus_mult_left)
apply (rule_tac K = "1 + ¦x¦" in termdiffs)
apply (auto intro: sin_converges cos_converges sums_summable intro!: sums_minus [THEN sums_summable] simp add: cos_fdiffs sin_fdiffs diffs_minus)
done

lemma isCont_sin [simp]: "isCont sin x"
by (rule DERIV_sin [THEN DERIV_isCont])

lemma isCont_cos [simp]: "isCont cos x"
by (rule DERIV_cos [THEN DERIV_isCont])


subsection {* Properties of Sine and Cosine *}

lemma sin_zero [simp]: "sin 0 = 0"
unfolding sin_def by (simp add: powser_zero)

lemma cos_zero [simp]: "cos 0 = 1"
unfolding cos_def by (simp add: powser_zero)

lemma DERIV_sin_sin_mult [simp]:
     "DERIV (%x. sin(x)*sin(x)) x :> cos(x) * sin(x) + cos(x) * sin(x)"
by (rule DERIV_mult, auto)

lemma DERIV_sin_sin_mult2 [simp]:
     "DERIV (%x. sin(x)*sin(x)) x :> 2 * cos(x) * sin(x)"
apply (cut_tac x = x in DERIV_sin_sin_mult)
apply (auto simp add: mult_assoc)
done

lemma DERIV_sin_realpow2 [simp]:
     "DERIV (%x. (sin x)²) x :> cos(x) * sin(x) + cos(x) * sin(x)"
by (auto simp add: numeral_2_eq_2 real_mult_assoc [symmetric])

lemma DERIV_sin_realpow2a [simp]:
     "DERIV (%x. (sin x)²) x :> 2 * cos(x) * sin(x)"
by (auto simp add: numeral_2_eq_2)

lemma DERIV_cos_cos_mult [simp]:
     "DERIV (%x. cos(x)*cos(x)) x :> -sin(x) * cos(x) + -sin(x) * cos(x)"
by (rule DERIV_mult, auto)

lemma DERIV_cos_cos_mult2 [simp]:
     "DERIV (%x. cos(x)*cos(x)) x :> -2 * cos(x) * sin(x)"
apply (cut_tac x = x in DERIV_cos_cos_mult)
apply (auto simp add: mult_ac)
done

lemma DERIV_cos_realpow2 [simp]:
     "DERIV (%x. (cos x)²) x :> -sin(x) * cos(x) + -sin(x) * cos(x)"
by (auto simp add: numeral_2_eq_2 real_mult_assoc [symmetric])

lemma DERIV_cos_realpow2a [simp]:
     "DERIV (%x. (cos x)²) x :> -2 * cos(x) * sin(x)"
by (auto simp add: numeral_2_eq_2)

lemma lemma_DERIV_subst: "[| DERIV f x :> D; D = E |] ==> DERIV f x :> E"
by auto

lemma DERIV_cos_realpow2b: "DERIV (%x. (cos x)²) x :> -(2 * cos(x) * sin(x))"
apply (rule lemma_DERIV_subst)
apply (rule DERIV_cos_realpow2a, auto)
done

(* most useful *)
lemma DERIV_cos_cos_mult3 [simp]:
     "DERIV (%x. cos(x)*cos(x)) x :> -(2 * cos(x) * sin(x))"
apply (rule lemma_DERIV_subst)
apply (rule DERIV_cos_cos_mult2, auto)
done

lemma DERIV_sin_circle_all: 
     "∀x. DERIV (%x. (sin x)² + (cos x)²) x :>  
             (2*cos(x)*sin(x) - 2*cos(x)*sin(x))"
apply (simp only: diff_minus, safe)
apply (rule DERIV_add) 
apply (auto simp add: numeral_2_eq_2)
done

lemma DERIV_sin_circle_all_zero [simp]:
     "∀x. DERIV (%x. (sin x)² + (cos x)²) x :> 0"
by (cut_tac DERIV_sin_circle_all, auto)

lemma sin_cos_squared_add [simp]: "((sin x)²) + ((cos x)²) = 1"
apply (cut_tac x = x and y = 0 in DERIV_sin_circle_all_zero [THEN DERIV_isconst_all])
apply (auto simp add: numeral_2_eq_2)
done

lemma sin_cos_squared_add2 [simp]: "((cos x)²) + ((sin x)²) = 1"
apply (subst add_commute)
apply (rule sin_cos_squared_add)
done

lemma sin_cos_squared_add3 [simp]: "cos x * cos x + sin x * sin x = 1"
apply (cut_tac x = x in sin_cos_squared_add2)
apply (simp add: power2_eq_square)
done

lemma sin_squared_eq: "(sin x)² = 1 - (cos x)²"
apply (rule_tac a1 = "(cos x)²" in add_right_cancel [THEN iffD1])
apply simp
done

lemma cos_squared_eq: "(cos x)² = 1 - (sin x)²"
apply (rule_tac a1 = "(sin x)²" in add_right_cancel [THEN iffD1])
apply simp
done

lemma abs_sin_le_one [simp]: "¦sin x¦ ≤ 1"
by (rule power2_le_imp_le, simp_all add: sin_squared_eq)

lemma sin_ge_minus_one [simp]: "-1 ≤ sin x"
apply (insert abs_sin_le_one [of x]) 
apply (simp add: abs_le_iff del: abs_sin_le_one) 
done

lemma sin_le_one [simp]: "sin x ≤ 1"
apply (insert abs_sin_le_one [of x]) 
apply (simp add: abs_le_iff del: abs_sin_le_one) 
done

lemma abs_cos_le_one [simp]: "¦cos x¦ ≤ 1"
by (rule power2_le_imp_le, simp_all add: cos_squared_eq)

lemma cos_ge_minus_one [simp]: "-1 ≤ cos x"
apply (insert abs_cos_le_one [of x]) 
apply (simp add: abs_le_iff del: abs_cos_le_one) 
done

lemma cos_le_one [simp]: "cos x ≤ 1"
apply (insert abs_cos_le_one [of x]) 
apply (simp add: abs_le_iff del: abs_cos_le_one)
done

lemma DERIV_fun_pow: "DERIV g x :> m ==>  
      DERIV (%x. (g x) ^ n) x :> real n * (g x) ^ (n - 1) * m"
unfolding One_nat_def
apply (rule lemma_DERIV_subst)
apply (rule_tac f = "(%x. x ^ n)" in DERIV_chain2)
apply (rule DERIV_pow, auto)
done

lemma DERIV_fun_exp:
     "DERIV g x :> m ==> DERIV (%x. exp(g x)) x :> exp(g x) * m"
apply (rule lemma_DERIV_subst)
apply (rule_tac f = exp in DERIV_chain2)
apply (rule DERIV_exp, auto)
done

lemma DERIV_fun_sin:
     "DERIV g x :> m ==> DERIV (%x. sin(g x)) x :> cos(g x) * m"
apply (rule lemma_DERIV_subst)
apply (rule_tac f = sin in DERIV_chain2)
apply (rule DERIV_sin, auto)
done

lemma DERIV_fun_cos:
     "DERIV g x :> m ==> DERIV (%x. cos(g x)) x :> -sin(g x) * m"
apply (rule lemma_DERIV_subst)
apply (rule_tac f = cos in DERIV_chain2)
apply (rule DERIV_cos, auto)
done

lemmas DERIV_intros = DERIV_ident DERIV_const DERIV_cos DERIV_cmult 
                    DERIV_sin  DERIV_exp  DERIV_inverse DERIV_pow 
                    DERIV_add  DERIV_diff  DERIV_mult  DERIV_minus 
                    DERIV_inverse_fun DERIV_quotient DERIV_fun_pow 
                    DERIV_fun_exp DERIV_fun_sin DERIV_fun_cos 

(* lemma *)
lemma lemma_DERIV_sin_cos_add:
     "∀x.  
         DERIV (%x. (sin (x + y) - (sin x * cos y + cos x * sin y)) ^ 2 +  
               (cos (x + y) - (cos x * cos y - sin x * sin y)) ^ 2) x :> 0"
apply (safe, rule lemma_DERIV_subst)
apply (best intro!: DERIV_intros intro: DERIV_chain2) 
  --{*replaces the old @{text DERIV_tac}*}
apply (auto simp add: algebra_simps)
done

lemma sin_cos_add [simp]:
     "(sin (x + y) - (sin x * cos y + cos x * sin y)) ^ 2 +  
      (cos (x + y) - (cos x * cos y - sin x * sin y)) ^ 2 = 0"
apply (cut_tac y = 0 and x = x and y7 = y 
       in lemma_DERIV_sin_cos_add [THEN DERIV_isconst_all])
apply (auto simp add: numeral_2_eq_2)
done

lemma sin_add: "sin (x + y) = sin x * cos y + cos x * sin y"
apply (cut_tac x = x and y = y in sin_cos_add)
apply (simp del: sin_cos_add)
done

lemma cos_add: "cos (x + y) = cos x * cos y - sin x * sin y"
apply (cut_tac x = x and y = y in sin_cos_add)
apply (simp del: sin_cos_add)
done

lemma lemma_DERIV_sin_cos_minus:
    "∀x. DERIV (%x. (sin(-x) + (sin x)) ^ 2 + (cos(-x) - (cos x)) ^ 2) x :> 0"
apply (safe, rule lemma_DERIV_subst)
apply (best intro!: DERIV_intros intro: DERIV_chain2)
apply (simp add: algebra_simps)
done

lemma sin_cos_minus: 
    "(sin(-x) + (sin x)) ^ 2 + (cos(-x) - (cos x)) ^ 2 = 0"
apply (cut_tac y = 0 and x = x 
       in lemma_DERIV_sin_cos_minus [THEN DERIV_isconst_all])
apply simp
done

lemma sin_minus [simp]: "sin (-x) = -sin(x)"
  using sin_cos_minus [where x=x] by simp

lemma cos_minus [simp]: "cos (-x) = cos(x)"
  using sin_cos_minus [where x=x] by simp

lemma sin_diff: "sin (x - y) = sin x * cos y - cos x * sin y"
by (simp add: diff_minus sin_add)

lemma sin_diff2: "sin (x - y) = cos y * sin x - sin y * cos x"
by (simp add: sin_diff mult_commute)

lemma cos_diff: "cos (x - y) = cos x * cos y + sin x * sin y"
by (simp add: diff_minus cos_add)

lemma cos_diff2: "cos (x - y) = cos y * cos x + sin y * sin x"
by (simp add: cos_diff mult_commute)

lemma sin_double [simp]: "sin(2 * x) = 2* sin x * cos x"
  using sin_add [where x=x and y=x] by simp

lemma cos_double: "cos(2* x) = ((cos x)²) - ((sin x)²)"
  using cos_add [where x=x and y=x]
  by (simp add: power2_eq_square)


subsection {* The Constant Pi *}

definition
  pi :: "real" where
  "pi = 2 * (THE x. 0 ≤ (x::real) & x ≤ 2 & cos x = 0)"

text{*Show that there's a least positive @{term x} with @{term "cos(x) = 0"}; 
   hence define pi.*}

lemma sin_paired:
     "(%n. -1 ^ n /(real (fact (2 * n + 1))) * x ^ (2 * n + 1)) 
      sums  sin x"
proof -
  have "(λn. ∑k = n * 2..<n * 2 + 2.
            (if even k then 0
             else -1 ^ ((k - Suc 0) div 2) / real (fact k)) *
            x ^ k) 
        sums sin x"
    unfolding sin_def
    by (rule sin_converges [THEN sums_summable, THEN sums_group], simp) 
  thus ?thesis unfolding One_nat_def by (simp add: mult_ac)
qed

text {* FIXME: This is a long, ugly proof! *}
lemma sin_gt_zero: "[|0 < x; x < 2 |] ==> 0 < sin x"
apply (subgoal_tac 
       "(λn. ∑k = n * 2..<n * 2 + 2.
              -1 ^ k / real (fact (2 * k + 1)) * x ^ (2 * k + 1)) 
     sums (∑n. -1 ^ n / real (fact (2 * n + 1)) * x ^ (2 * n + 1))")
 prefer 2
 apply (rule sin_paired [THEN sums_summable, THEN sums_group], simp) 
apply (rotate_tac 2)
apply (drule sin_paired [THEN sums_unique, THEN ssubst])
unfolding One_nat_def
apply (auto simp del: fact_Suc)
apply (frule sums_unique)
apply (auto simp del: fact_Suc)
apply (rule_tac n1 = 0 in series_pos_less [THEN [2] order_le_less_trans])
apply (auto simp del: fact_Suc)
apply (erule sums_summable)
apply (case_tac "m=0")
apply (simp (no_asm_simp))
apply (subgoal_tac "6 * (x * (x * x) / real (Suc (Suc (Suc (Suc (Suc (Suc 0))))))) < 6 * x") 
apply (simp only: mult_less_cancel_left, simp)  
apply (simp (no_asm_simp) add: numeral_2_eq_2 [symmetric] mult_assoc [symmetric])
apply (subgoal_tac "x*x < 2*3", simp) 
apply (rule mult_strict_mono)
apply (auto simp add: real_0_less_add_iff real_of_nat_Suc simp del: fact_Suc)
apply (subst fact_Suc)
apply (subst fact_Suc)
apply (subst fact_Suc)
apply (subst fact_Suc)
apply (subst real_of_nat_mult)
apply (subst real_of_nat_mult)
apply (subst real_of_nat_mult)
apply (subst real_of_nat_mult)
apply (simp (no_asm) add: divide_inverse del: fact_Suc)
apply (auto simp add: mult_assoc [symmetric] simp del: fact_Suc)
apply (rule_tac c="real (Suc (Suc (4*m)))" in mult_less_imp_less_right) 
apply (auto simp add: mult_assoc simp del: fact_Suc)
apply (rule_tac c="real (Suc (Suc (Suc (4*m))))" in mult_less_imp_less_right) 
apply (auto simp add: mult_assoc mult_less_cancel_left simp del: fact_Suc)
apply (subgoal_tac "x * (x * x ^ (4*m)) = (x ^ (4*m)) * (x * x)") 
apply (erule ssubst)+
apply (auto simp del: fact_Suc)
apply (subgoal_tac "0 < x ^ (4 * m) ")
 prefer 2 apply (simp only: zero_less_power) 
apply (simp (no_asm_simp) add: mult_less_cancel_left)
apply (rule mult_strict_mono)
apply (simp_all (no_asm_simp))
done

lemma sin_gt_zero1: "[|0 < x; x < 2 |] ==> 0 < sin x"
by (auto intro: sin_gt_zero)

lemma cos_double_less_one: "[| 0 < x; x < 2 |] ==> cos (2 * x) < 1"
apply (cut_tac x = x in sin_gt_zero1)
apply (auto simp add: cos_squared_eq cos_double)
done

lemma cos_paired:
     "(%n. -1 ^ n /(real (fact (2 * n))) * x ^ (2 * n)) sums cos x"
proof -
  have "(λn. ∑k = n * 2..<n * 2 + 2.
            (if even k then -1 ^ (k div 2) / real (fact k) else 0) *
            x ^ k) 
        sums cos x"
    unfolding cos_def
    by (rule cos_converges [THEN sums_summable, THEN sums_group], simp) 
  thus ?thesis by (simp add: mult_ac)
qed

lemma fact_lemma: "real (n::nat) * 4 = real (4 * n)"
by simp

lemma cos_two_less_zero [simp]: "cos (2) < 0"
apply (cut_tac x = 2 in cos_paired)
apply (drule sums_minus)
apply (rule neg_less_iff_less [THEN iffD1]) 
apply (frule sums_unique, auto)
apply (rule_tac y =
 "∑n=0..< Suc(Suc(Suc 0)). - (-1 ^ n / (real(fact (2*n))) * 2 ^ (2*n))"
       in order_less_trans)
apply (simp (no_asm) add: fact_num_eq_if realpow_num_eq_if del: fact_Suc)
apply (simp (no_asm) add: mult_assoc del: setsum_op_ivl_Suc)
apply (rule sumr_pos_lt_pair)
apply (erule sums_summable, safe)
unfolding One_nat_def
apply (simp (no_asm) add: divide_inverse real_0_less_add_iff mult_assoc [symmetric] 
            del: fact_Suc)
apply (rule real_mult_inverse_cancel2)
apply (rule real_of_nat_fact_gt_zero)+
apply (simp (no_asm) add: mult_assoc [symmetric] del: fact_Suc)
apply (subst fact_lemma) 
apply (subst fact_Suc [of "Suc (Suc (Suc (Suc (Suc (Suc (Suc (4 * d)))))))"])
apply (simp only: real_of_nat_mult)
apply (rule mult_strict_mono, force)
  apply (rule_tac [3] real_of_nat_ge_zero)
 prefer 2 apply force
apply (rule real_of_nat_less_iff [THEN iffD2])
apply (rule fact_less_mono, auto)
done

lemmas cos_two_neq_zero [simp] = cos_two_less_zero [THEN less_imp_neq]
lemmas cos_two_le_zero [simp] = cos_two_less_zero [THEN order_less_imp_le]

lemma cos_is_zero: "EX! x. 0 ≤ x & x ≤ 2 & cos x = 0"
apply (subgoal_tac "∃x. 0 ≤ x & x ≤ 2 & cos x = 0")
apply (rule_tac [2] IVT2)
apply (auto intro: DERIV_isCont DERIV_cos)
apply (cut_tac x = xa and y = y in linorder_less_linear)
apply (rule ccontr)
apply (subgoal_tac " (∀x. cos differentiable x) & (∀x. isCont cos x) ")
apply (auto intro: DERIV_cos DERIV_isCont simp add: differentiable_def)
apply (drule_tac f = cos in Rolle)
apply (drule_tac [5] f = cos in Rolle)
apply (auto dest!: DERIV_cos [THEN DERIV_unique] simp add: differentiable_def)
apply (drule_tac y1 = xa in order_le_less_trans [THEN sin_gt_zero])
apply (assumption, rule_tac y=y in order_less_le_trans, simp_all) 
apply (drule_tac y1 = y in order_le_less_trans [THEN sin_gt_zero], assumption, simp_all) 
done
    
lemma pi_half: "pi/2 = (THE x. 0 ≤ x & x ≤ 2 & cos x = 0)"
by (simp add: pi_def)

lemma cos_pi_half [simp]: "cos (pi / 2) = 0"
by (simp add: pi_half cos_is_zero [THEN theI'])

lemma pi_half_gt_zero [simp]: "0 < pi / 2"
apply (rule order_le_neq_trans)
apply (simp add: pi_half cos_is_zero [THEN theI'])
apply (rule notI, drule arg_cong [where f=cos], simp)
done

lemmas pi_half_neq_zero [simp] = pi_half_gt_zero [THEN less_imp_neq, symmetric]
lemmas pi_half_ge_zero [simp] = pi_half_gt_zero [THEN order_less_imp_le]

lemma pi_half_less_two [simp]: "pi / 2 < 2"
apply (rule order_le_neq_trans)
apply (simp add: pi_half cos_is_zero [THEN theI'])
apply (rule notI, drule arg_cong [where f=cos], simp)
done

lemmas pi_half_neq_two [simp] = pi_half_less_two [THEN less_imp_neq]
lemmas pi_half_le_two [simp] =  pi_half_less_two [THEN order_less_imp_le]

lemma pi_gt_zero [simp]: "0 < pi"
by (insert pi_half_gt_zero, simp)

lemma pi_ge_zero [simp]: "0 ≤ pi"
by (rule pi_gt_zero [THEN order_less_imp_le])

lemma pi_neq_zero [simp]: "pi ≠ 0"
by (rule pi_gt_zero [THEN less_imp_neq, THEN not_sym])

lemma pi_not_less_zero [simp]: "¬ pi < 0"
by (simp add: linorder_not_less)

lemma minus_pi_half_less_zero: "-(pi/2) < 0"
by simp

lemma m2pi_less_pi: "- (2 * pi) < pi"
proof -
  have "- (2 * pi) < 0" and "0 < pi" by auto
  from order_less_trans[OF this] show ?thesis .
qed

lemma sin_pi_half [simp]: "sin(pi/2) = 1"
apply (cut_tac x = "pi/2" in sin_cos_squared_add2)
apply (cut_tac sin_gt_zero [OF pi_half_gt_zero pi_half_less_two])
apply (simp add: power2_eq_square)
done

lemma cos_pi [simp]: "cos pi = -1"
by (cut_tac x = "pi/2" and y = "pi/2" in cos_add, simp)

lemma sin_pi [simp]: "sin pi = 0"
by (cut_tac x = "pi/2" and y = "pi/2" in sin_add, simp)

lemma sin_cos_eq: "sin x = cos (pi/2 - x)"
by (simp add: diff_minus cos_add)
declare sin_cos_eq [symmetric, simp]

lemma minus_sin_cos_eq: "-sin x = cos (x + pi/2)"
by (simp add: cos_add)
declare minus_sin_cos_eq [symmetric, simp]

lemma cos_sin_eq: "cos x = sin (pi/2 - x)"
by (simp add: diff_minus sin_add)
declare cos_sin_eq [symmetric, simp]

lemma sin_periodic_pi [simp]: "sin (x + pi) = - sin x"
by (simp add: sin_add)

lemma sin_periodic_pi2 [simp]: "sin (pi + x) = - sin x"
by (simp add: sin_add)

lemma cos_periodic_pi [simp]: "cos (x + pi) = - cos x"
by (simp add: cos_add)

lemma sin_periodic [simp]: "sin (x + 2*pi) = sin x"
by (simp add: sin_add cos_double)

lemma cos_periodic [simp]: "cos (x + 2*pi) = cos x"
by (simp add: cos_add cos_double)

lemma cos_npi [simp]: "cos (real n * pi) = -1 ^ n"
apply (induct "n")
apply (auto simp add: real_of_nat_Suc left_distrib)
done

lemma cos_npi2 [simp]: "cos (pi * real n) = -1 ^ n"
proof -
  have "cos (pi * real n) = cos (real n * pi)" by (simp only: mult_commute)
  also have "... = -1 ^ n" by (rule cos_npi) 
  finally show ?thesis .
qed

lemma sin_npi [simp]: "sin (real (n::nat) * pi) = 0"
apply (induct "n")
apply (auto simp add: real_of_nat_Suc left_distrib)
done

lemma sin_npi2 [simp]: "sin (pi * real (n::nat)) = 0"
by (simp add: mult_commute [of pi]) 

lemma cos_two_pi [simp]: "cos (2 * pi) = 1"
by (simp add: cos_double)

lemma sin_two_pi [simp]: "sin (2 * pi) = 0"
by simp

lemma sin_gt_zero2: "[| 0 < x; x < pi/2 |] ==> 0 < sin x"
apply (rule sin_gt_zero, assumption)
apply (rule order_less_trans, assumption)
apply (rule pi_half_less_two)
done

lemma sin_less_zero: 
  assumes lb: "- pi/2 < x" and "x < 0" shows "sin x < 0"
proof -
  have "0 < sin (- x)" using prems by (simp only: sin_gt_zero2) 
  thus ?thesis by simp
qed

lemma pi_less_4: "pi < 4"
by (cut_tac pi_half_less_two, auto)

lemma cos_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < cos x"
apply (cut_tac pi_less_4)
apply (cut_tac f = cos and a = 0 and b = x and y = 0 in IVT2_objl, safe, simp_all)
apply (cut_tac cos_is_zero, safe)
apply (rename_tac y z)
apply (drule_tac x = y in spec)
apply (drule_tac x = "pi/2" in spec, simp) 
done

lemma cos_gt_zero_pi: "[| -(pi/2) < x; x < pi/2 |] ==> 0 < cos x"
apply (rule_tac x = x and y = 0 in linorder_cases)
apply (rule cos_minus [THEN subst])
apply (rule cos_gt_zero)
apply (auto intro: cos_gt_zero)
done
 
lemma cos_ge_zero: "[| -(pi/2) ≤ x; x ≤ pi/2 |] ==> 0 ≤ cos x"
apply (auto simp add: order_le_less cos_gt_zero_pi)
apply (subgoal_tac "x = pi/2", auto) 
done

lemma sin_gt_zero_pi: "[| 0 < x; x < pi  |] ==> 0 < sin x"
apply (subst sin_cos_eq)
apply (rotate_tac 1)
apply (drule real_sum_of_halves [THEN ssubst])
apply (auto intro!: cos_gt_zero_pi simp del: sin_cos_eq [symmetric])
done


lemma pi_ge_two: "2 ≤ pi"
proof (rule ccontr)
  assume "¬ 2 ≤ pi" hence "pi < 2" by auto
  have "∃y > pi. y < 2 ∧ y < 2 * pi"
  proof (cases "2 < 2 * pi")
    case True with dense[OF `pi < 2`] show ?thesis by auto
  next
    case False have "pi < 2 * pi" by auto
    from dense[OF this] and False show ?thesis by auto
  qed
  then obtain y where "pi < y" and "y < 2" and "y < 2 * pi" by blast
  hence "0 < sin y" using sin_gt_zero by auto
  moreover 
  have "sin y < 0" using sin_gt_zero_pi[of "y - pi"] `pi < y` and `y < 2 * pi` sin_periodic_pi[of "y - pi"] by auto
  ultimately show False by auto
qed

lemma sin_ge_zero: "[| 0 ≤ x; x ≤ pi |] ==> 0 ≤ sin x"
by (auto simp add: order_le_less sin_gt_zero_pi)

lemma cos_total: "[| -1 ≤ y; y ≤ 1 |] ==> EX! x. 0 ≤ x & x ≤ pi & (cos x = y)"
apply (subgoal_tac "∃x. 0 ≤ x & x ≤ pi & cos x = y")
apply (rule_tac [2] IVT2)
apply (auto intro: order_less_imp_le DERIV_isCont DERIV_cos)
apply (cut_tac x = xa and y = y in linorder_less_linear)
apply (rule ccontr, auto)
apply (drule_tac f = cos in Rolle)
apply (drule_tac [5] f = cos in Rolle)
apply (auto intro: order_less_imp_le DERIV_isCont DERIV_cos
            dest!: DERIV_cos [THEN DERIV_unique] 
            simp add: differentiable_def)
apply (auto dest: sin_gt_zero_pi [OF order_le_less_trans order_less_le_trans])
done

lemma sin_total:
     "[| -1 ≤ y; y ≤ 1 |] ==> EX! x. -(pi/2) ≤ x & x ≤ pi/2 & (sin x = y)"
apply (rule ccontr)
apply (subgoal_tac "∀x. (- (pi/2) ≤ x & x ≤ pi/2 & (sin x = y)) = (0 ≤ (x + pi/2) & (x + pi/2) ≤ pi & (cos (x + pi/2) = -y))")
apply (erule contrapos_np)
apply (simp del: minus_sin_cos_eq [symmetric])
apply (cut_tac y="-y" in cos_total, simp) apply simp 
apply (erule ex1E)
apply (rule_tac a = "x - (pi/2)" in ex1I)
apply (simp (no_asm) add: add_assoc)
apply (rotate_tac 3)
apply (drule_tac x = "xa + pi/2" in spec, safe, simp_all) 
done

lemma reals_Archimedean4:
     "[| 0 < y; 0 ≤ x |] ==> ∃n. real n * y ≤ x & x < real (Suc n) * y"
apply (auto dest!: reals_Archimedean3)
apply (drule_tac x = x in spec, clarify) 
apply (subgoal_tac "x < real(LEAST m::nat. x < real m * y) * y")
 prefer 2 apply (erule LeastI) 
apply (case_tac "LEAST m::nat. x < real m * y", simp) 
apply (subgoal_tac "~ x < real nat * y")
 prefer 2 apply (rule not_less_Least, simp, force)  
done

(* Pre Isabelle99-2 proof was simpler- numerals arithmetic 
   now causes some unwanted re-arrangements of literals!   *)
lemma cos_zero_lemma:
     "[| 0 ≤ x; cos x = 0 |] ==>  
      ∃n::nat. ~even n & x = real n * (pi/2)"
apply (drule pi_gt_zero [THEN reals_Archimedean4], safe)
apply (subgoal_tac "0 ≤ x - real n * pi & 
                    (x - real n * pi) ≤ pi & (cos (x - real n * pi) = 0) ")
apply (auto simp add: algebra_simps real_of_nat_Suc)
 prefer 2 apply (simp add: cos_diff)
apply (simp add: cos_diff)
apply (subgoal_tac "EX! x. 0 ≤ x & x ≤ pi & cos x = 0")
apply (rule_tac [2] cos_total, safe)
apply (drule_tac x = "x - real n * pi" in spec)
apply (drule_tac x = "pi/2" in spec)
apply (simp add: cos_diff)
apply (rule_tac x = "Suc (2 * n)" in exI)
apply (simp add: real_of_nat_Suc algebra_simps, auto)
done

lemma sin_zero_lemma:
     "[| 0 ≤ x; sin x = 0 |] ==>  
      ∃n::nat. even n & x = real n * (pi/2)"
apply (subgoal_tac "∃n::nat. ~ even n & x + pi/2 = real n * (pi/2) ")
 apply (clarify, rule_tac x = "n - 1" in exI)
 apply (force simp add: odd_Suc_mult_two_ex real_of_nat_Suc left_distrib)
apply (rule cos_zero_lemma)
apply (simp_all add: add_increasing)  
done


lemma cos_zero_iff:
     "(cos x = 0) =  
      ((∃n::nat. ~even n & (x = real n * (pi/2))) |    
       (∃n::nat. ~even n & (x = -(real n * (pi/2)))))"
apply (rule iffI)
apply (cut_tac linorder_linear [of 0 x], safe)
apply (drule cos_zero_lemma, assumption+)
apply (cut_tac x="-x" in cos_zero_lemma, simp, simp) 
apply (force simp add: minus_equation_iff [of x]) 
apply (auto simp only: odd_Suc_mult_two_ex real_of_nat_Suc left_distrib) 
apply (auto simp add: cos_add)
done

(* ditto: but to a lesser extent *)
lemma sin_zero_iff:
     "(sin x = 0) =  
      ((∃n::nat. even n & (x = real n * (pi/2))) |    
       (∃n::nat. even n & (x = -(real n * (pi/2)))))"
apply (rule iffI)
apply (cut_tac linorder_linear [of 0 x], safe)
apply (drule sin_zero_lemma, assumption+)
apply (cut_tac x="-x" in sin_zero_lemma, simp, simp, safe)
apply (force simp add: minus_equation_iff [of x]) 
apply (auto simp add: even_mult_two_ex)
done

lemma cos_monotone_0_pi: assumes "0 ≤ y" and "y < x" and "x ≤ pi"
  shows "cos x < cos y"
proof -
  have "- (x - y) < 0" by (auto!)

  from MVT2[OF `y < x` DERIV_cos[THEN impI, THEN allI]]
  obtain z where "y < z" and "z < x" and cos_diff: "cos x - cos y = (x - y) * - sin z" by auto
  hence "0 < z" and "z < pi" by (auto!)
  hence "0 < sin z" using sin_gt_zero_pi by auto
  hence "cos x - cos y < 0" unfolding cos_diff minus_mult_commute[symmetric] using `- (x - y) < 0` by (rule mult_pos_neg2)
  thus ?thesis by auto
qed

lemma cos_monotone_0_pi': assumes "0 ≤ y" and "y ≤ x" and "x ≤ pi" shows "cos x ≤ cos y"
proof (cases "y < x")
  case True show ?thesis using cos_monotone_0_pi[OF `0 ≤ y` True `x ≤ pi`] by auto
next
  case False hence "y = x" using `y ≤ x` by auto
  thus ?thesis by auto
qed

lemma cos_monotone_minus_pi_0: assumes "-pi ≤ y" and "y < x" and "x ≤ 0"
  shows "cos y < cos x"
proof -
  have "0 ≤ -x" and "-x < -y" and "-y ≤ pi" by (auto!)
  from cos_monotone_0_pi[OF this]
  show ?thesis unfolding cos_minus .
qed

lemma cos_monotone_minus_pi_0': assumes "-pi ≤ y" and "y ≤ x" and "x ≤ 0" shows "cos y ≤ cos x"
proof (cases "y < x")
  case True show ?thesis using cos_monotone_minus_pi_0[OF `-pi ≤ y` True `x ≤ 0`] by auto
next
  case False hence "y = x" using `y ≤ x` by auto
  thus ?thesis by auto
qed

lemma sin_monotone_2pi': assumes "- (pi / 2) ≤ y" and "y ≤ x" and "x ≤ pi / 2" shows "sin y ≤ sin x"
proof -
  have "0 ≤ y + pi / 2" and "y + pi / 2 ≤ x + pi / 2" and "x + pi /2 ≤ pi" using pi_ge_two by (auto!)
  from cos_monotone_0_pi'[OF this] show ?thesis unfolding minus_sin_cos_eq[symmetric] by auto
qed

subsection {* Tangent *}

definition
  tan :: "real => real" where
  "tan x = (sin x)/(cos x)"

lemma tan_zero [simp]: "tan 0 = 0"
by (simp add: tan_def)

lemma tan_pi [simp]: "tan pi = 0"
by (simp add: tan_def)

lemma tan_npi [simp]: "tan (real (n::nat) * pi) = 0"
by (simp add: tan_def)

lemma tan_minus [simp]: "tan (-x) = - tan x"
by (simp add: tan_def minus_mult_left)

lemma tan_periodic [simp]: "tan (x + 2*pi) = tan x"
by (simp add: tan_def)

lemma lemma_tan_add1: 
      "[| cos x ≠ 0; cos y ≠ 0 |]  
        ==> 1 - tan(x)*tan(y) = cos (x + y)/(cos x * cos y)"
apply (simp add: tan_def divide_inverse)
apply (auto simp del: inverse_mult_distrib 
            simp add: inverse_mult_distrib [symmetric] mult_ac)
apply (rule_tac c1 = "cos x * cos y" in real_mult_right_cancel [THEN subst])
apply (auto simp del: inverse_mult_distrib 
            simp add: mult_assoc left_diff_distrib cos_add)
done

lemma add_tan_eq: 
      "[| cos x ≠ 0; cos y ≠ 0 |]  
       ==> tan x + tan y = sin(x + y)/(cos x * cos y)"
apply (simp add: tan_def)
apply (rule_tac c1 = "cos x * cos y" in real_mult_right_cancel [THEN subst])
apply (auto simp add: mult_assoc left_distrib)
apply (simp add: sin_add)
done

lemma tan_add:
     "[| cos x ≠ 0; cos y ≠ 0; cos (x + y) ≠ 0 |]  
      ==> tan(x + y) = (tan(x) + tan(y))/(1 - tan(x) * tan(y))"
apply (simp (no_asm_simp) add: add_tan_eq lemma_tan_add1)
apply (simp add: tan_def)
done

lemma tan_double:
     "[| cos x ≠ 0; cos (2 * x) ≠ 0 |]  
      ==> tan (2 * x) = (2 * tan x)/(1 - (tan(x) ^ 2))"
apply (insert tan_add [of x x]) 
apply (simp add: mult_2 [symmetric])  
apply (auto simp add: numeral_2_eq_2)
done

lemma tan_gt_zero: "[| 0 < x; x < pi/2 |] ==> 0 < tan x"
by (simp add: tan_def zero_less_divide_iff sin_gt_zero2 cos_gt_zero_pi) 

lemma tan_less_zero: 
  assumes lb: "- pi/2 < x" and "x < 0" shows "tan x < 0"
proof -
  have "0 < tan (- x)" using prems by (simp only: tan_gt_zero) 
  thus ?thesis by simp
qed

lemma tan_half: fixes x :: real assumes "- (pi / 2) < x" and "x < pi / 2"
  shows "tan x = sin (2 * x) / (cos (2 * x) + 1)"
proof -
  from cos_gt_zero_pi[OF `- (pi / 2) < x` `x < pi / 2`]
  have "cos x ≠ 0" by auto

  have minus_cos_2x: "!!X. X - cos (2*x) = X - (cos x) ^ 2 + (sin x) ^ 2" unfolding cos_double by algebra

  have "tan x = (tan x + tan x) / 2" by auto
  also have "… = sin (x + x) / (cos x * cos x) / 2" unfolding add_tan_eq[OF `cos x ≠ 0` `cos x ≠ 0`] ..
  also have "… = sin (2 * x) / ((cos x) ^ 2 + (cos x) ^ 2 + cos (2*x) - cos (2*x))" unfolding divide_divide_eq_left numeral_2_eq_2 by auto
  also have "… = sin (2 * x) / ((cos x) ^ 2 + cos (2*x) + (sin x)^2)" unfolding minus_cos_2x by auto
  also have "… = sin (2 * x) / (cos (2*x) + 1)" by auto
  finally show ?thesis .
qed

lemma lemma_DERIV_tan:
     "cos x ≠ 0 ==> DERIV (%x. sin(x)/cos(x)) x :> inverse((cos x)²)"
apply (rule lemma_DERIV_subst)
apply (best intro!: DERIV_intros intro: DERIV_chain2) 
apply (auto simp add: divide_inverse numeral_2_eq_2)
done

lemma DERIV_tan [simp]: "cos x ≠ 0 ==> DERIV tan x :> inverse((cos x)²)"
by (auto dest: lemma_DERIV_tan simp add: tan_def [symmetric])

lemma isCont_tan [simp]: "cos x ≠ 0 ==> isCont tan x"
by (rule DERIV_tan [THEN DERIV_isCont])

lemma LIM_cos_div_sin [simp]: "(%x. cos(x)/sin(x)) -- pi/2 --> 0"
apply (subgoal_tac "(λx. cos x * inverse (sin x)) -- pi * inverse 2 --> 0*1")
apply (simp add: divide_inverse [symmetric])
apply (rule LIM_mult)
apply (rule_tac [2] inverse_1 [THEN subst])
apply (rule_tac [2] LIM_inverse)
apply (simp_all add: divide_inverse [symmetric]) 
apply (simp_all only: isCont_def [symmetric] cos_pi_half [symmetric] sin_pi_half [symmetric]) 
apply (blast intro!: DERIV_isCont DERIV_sin DERIV_cos)+
done

lemma lemma_tan_total: "0 < y ==> ∃x. 0 < x & x < pi/2 & y < tan x"
apply (cut_tac LIM_cos_div_sin)
apply (simp only: LIM_def)
apply (drule_tac x = "inverse y" in spec, safe, force)
apply (drule_tac ?d1.0 = s in pi_half_gt_zero [THEN [2] real_lbound_gt_zero], safe)
apply (rule_tac x = "(pi/2) - e" in exI)
apply (simp (no_asm_simp))
apply (drule_tac x = "(pi/2) - e" in spec)
apply (auto simp add: tan_def)
apply (rule inverse_less_iff_less [THEN iffD1])
apply (auto simp add: divide_inverse)
apply (rule real_mult_order) 
apply (subgoal_tac [3] "0 < sin e & 0 < cos e")
apply (auto intro: cos_gt_zero sin_gt_zero2 simp add: mult_commute) 
done

lemma tan_total_pos: "0 ≤ y ==> ∃x. 0 ≤ x & x < pi/2 & tan x = y"
apply (frule order_le_imp_less_or_eq, safe)
 prefer 2 apply force
apply (drule lemma_tan_total, safe)
apply (cut_tac f = tan and a = 0 and b = x and y = y in IVT_objl)
apply (auto intro!: DERIV_tan [THEN DERIV_isCont])
apply (drule_tac y = xa in order_le_imp_less_or_eq)
apply (auto dest: cos_gt_zero)
done

lemma lemma_tan_total1: "∃x. -(pi/2) < x & x < (pi/2) & tan x = y"
apply (cut_tac linorder_linear [of 0 y], safe)
apply (drule tan_total_pos)
apply (cut_tac [2] y="-y" in tan_total_pos, safe)
apply (rule_tac [3] x = "-x" in exI)
apply (auto intro!: exI)
done

lemma tan_total: "EX! x. -(pi/2) < x & x < (pi/2) & tan x = y"
apply (cut_tac y = y in lemma_tan_total1, auto)
apply (cut_tac x = xa and y = y in linorder_less_linear, auto)
apply (subgoal_tac [2] "∃z. y < z & z < xa & DERIV tan z :> 0")
apply (subgoal_tac "∃z. xa < z & z < y & DERIV tan z :> 0")
apply (rule_tac [4] Rolle)
apply (rule_tac [2] Rolle)
apply (auto intro!: DERIV_tan DERIV_isCont exI 
            simp add: differentiable_def)
txt{*Now, simulate TRYALL*}
apply (rule_tac [!] DERIV_tan asm_rl)
apply (auto dest!: DERIV_unique [OF _ DERIV_tan]
            simp add: cos_gt_zero_pi [THEN less_imp_neq, THEN not_sym]) 
done

lemma tan_monotone: assumes "- (pi / 2) < y" and "y < x" and "x < pi / 2"
  shows "tan y < tan x"
proof -
  have "∀ x'. y ≤ x' ∧ x' ≤ x --> DERIV tan x' :> inverse (cos x'^2)"
  proof (rule allI, rule impI)
    fix x' :: real assume "y ≤ x' ∧ x' ≤ x"
    hence "-(pi/2) < x'" and "x' < pi/2" by (auto!)
    from cos_gt_zero_pi[OF this]
    have "cos x' ≠ 0" by auto
    thus "DERIV tan x' :> inverse (cos x'^2)" by (rule DERIV_tan)
  qed
  from MVT2[OF `y < x` this] 
  obtain z where "y < z" and "z < x" and tan_diff: "tan x - tan y = (x - y) * inverse ((cos z)²)" by auto
  hence "- (pi / 2) < z" and "z < pi / 2" by (auto!)
  hence "0 < cos z" using cos_gt_zero_pi by auto
  hence inv_pos: "0 < inverse ((cos z)²)" by auto
  have "0 < x - y" using `y < x` by auto
  from real_mult_order[OF this inv_pos]
  have "0 < tan x - tan y" unfolding tan_diff by auto
  thus ?thesis by auto
qed

lemma tan_monotone': assumes "- (pi / 2) < y" and "y < pi / 2" and "- (pi / 2) < x" and "x < pi / 2"
  shows "(y < x) = (tan y < tan x)"
proof
  assume "y < x" thus "tan y < tan x" using tan_monotone and `- (pi / 2) < y` and `x < pi / 2` by auto
next
  assume "tan y < tan x"
  show "y < x"
  proof (rule ccontr)
    assume "¬ y < x" hence "x ≤ y" by auto
    hence "tan x ≤ tan y" 
    proof (cases "x = y")
      case True thus ?thesis by auto
    next
      case False hence "x < y" using `x ≤ y` by auto
      from tan_monotone[OF `- (pi/2) < x` this `y < pi / 2`] show ?thesis by auto
    qed
    thus False using `tan y < tan x` by auto
  qed
qed

lemma tan_inverse: "1 / (tan y) = tan (pi / 2 - y)" unfolding tan_def sin_cos_eq[of y] cos_sin_eq[of y] by auto

lemma tan_periodic_pi[simp]: "tan (x + pi) = tan x" 
  by (simp add: tan_def)

lemma tan_periodic_nat[simp]: fixes n :: nat shows "tan (x + real n * pi) = tan x" 
proof (induct n arbitrary: x)
  case (Suc n)
  have split_pi_off: "x + real (Suc n) * pi = (x + real n * pi) + pi" unfolding Suc_plus1 real_of_nat_add real_of_one real_add_mult_distrib by auto
  show ?case unfolding split_pi_off using Suc by auto
qed auto

lemma tan_periodic_int[simp]: fixes i :: int shows "tan (x + real i * pi) = tan x"
proof (cases "0 ≤ i")
  case True hence i_nat: "real i = real (nat i)" by auto
  show ?thesis unfolding i_nat by auto
next
  case False hence i_nat: "real i = - real (nat (-i))" by auto
  have "tan x = tan (x + real i * pi - real i * pi)" by auto
  also have "… = tan (x + real i * pi)" unfolding i_nat mult_minus_left diff_minus_eq_add by (rule tan_periodic_nat)
  finally show ?thesis by auto
qed

lemma tan_periodic_n[simp]: "tan (x + number_of n * pi) = tan x"
  using tan_periodic_int[of _ "number_of n" ] unfolding real_number_of .

subsection {* Inverse Trigonometric Functions *}

definition
  arcsin :: "real => real" where
  "arcsin y = (THE x. -(pi/2) ≤ x & x ≤ pi/2 & sin x = y)"

definition
  arccos :: "real => real" where
  "arccos y = (THE x. 0 ≤ x & x ≤ pi & cos x = y)"

definition     
  arctan :: "real => real" where
  "arctan y = (THE x. -(pi/2) < x & x < pi/2 & tan x = y)"

lemma arcsin:
     "[| -1 ≤ y; y ≤ 1 |]  
      ==> -(pi/2) ≤ arcsin y &  
           arcsin y ≤ pi/2 & sin(arcsin y) = y"
unfolding arcsin_def by (rule theI' [OF sin_total])

lemma arcsin_pi:
     "[| -1 ≤ y; y ≤ 1 |]  
      ==> -(pi/2) ≤ arcsin y & arcsin y ≤ pi & sin(arcsin y) = y"
apply (drule (1) arcsin)
apply (force intro: order_trans)
done

lemma sin_arcsin [simp]: "[| -1 ≤ y; y ≤ 1 |] ==> sin(arcsin y) = y"
by (blast dest: arcsin)
      
lemma arcsin_bounded:
     "[| -1 ≤ y; y ≤ 1 |] ==> -(pi/2) ≤ arcsin y & arcsin y ≤ pi/2"
by (blast dest: arcsin)

lemma arcsin_lbound: "[| -1 ≤ y; y ≤ 1 |] ==> -(pi/2) ≤ arcsin y"
by (blast dest: arcsin)

lemma arcsin_ubound: "[| -1 ≤ y; y ≤ 1 |] ==> arcsin y ≤ pi/2"
by (blast dest: arcsin)

lemma arcsin_lt_bounded:
     "[| -1 < y; y < 1 |] ==> -(pi/2) < arcsin y & arcsin y < pi/2"
apply (frule order_less_imp_le)
apply (frule_tac y = y in order_less_imp_le)
apply (frule arcsin_bounded)
apply (safe, simp)
apply (drule_tac y = "arcsin y" in order_le_imp_less_or_eq)
apply (drule_tac [2] y = "pi/2" in order_le_imp_less_or_eq, safe)
apply (drule_tac [!] f = sin in arg_cong, auto)
done

lemma arcsin_sin: "[|-(pi/2) ≤ x; x ≤ pi/2 |] ==> arcsin(sin x) = x"
apply (unfold arcsin_def)
apply (rule the1_equality)
apply (rule sin_total, auto)
done

lemma arccos:
     "[| -1 ≤ y; y ≤ 1 |]  
      ==> 0 ≤ arccos y & arccos y ≤ pi & cos(arccos y) = y"
unfolding arccos_def by (rule theI' [OF cos_total])

lemma cos_arccos [simp]: "[| -1 ≤ y; y ≤ 1 |] ==> cos(arccos y) = y"
by (blast dest: arccos)
      
lemma arccos_bounded: "[| -1 ≤ y; y ≤ 1 |] ==> 0 ≤ arccos y & arccos y ≤ pi"
by (blast dest: arccos)

lemma arccos_lbound: "[| -1 ≤ y; y ≤ 1 |] ==> 0 ≤ arccos y"
by (blast dest: arccos)

lemma arccos_ubound: "[| -1 ≤ y; y ≤ 1 |] ==> arccos y ≤ pi"
by (blast dest: arccos)

lemma arccos_lt_bounded:
     "[| -1 < y; y < 1 |]  
      ==> 0 < arccos y & arccos y < pi"
apply (frule order_less_imp_le)
apply (frule_tac y = y in order_less_imp_le)
apply (frule arccos_bounded, auto)
apply (drule_tac y = "arccos y" in order_le_imp_less_or_eq)
apply (drule_tac [2] y = pi in order_le_imp_less_or_eq, auto)
apply (drule_tac [!] f = cos in arg_cong, auto)
done

lemma arccos_cos: "[|0 ≤ x; x ≤ pi |] ==> arccos(cos x) = x"
apply (simp add: arccos_def)
apply (auto intro!: the1_equality cos_total)
done

lemma arccos_cos2: "[|x ≤ 0; -pi ≤ x |] ==> arccos(cos x) = -x"
apply (simp add: arccos_def)
apply (auto intro!: the1_equality cos_total)
done

lemma cos_arcsin: "[|-1 ≤ x; x ≤ 1|] ==> cos (arcsin x) = sqrt (1 - x²)"
apply (subgoal_tac "x² ≤ 1")
apply (rule power2_eq_imp_eq)
apply (simp add: cos_squared_eq)
apply (rule cos_ge_zero)
apply (erule (1) arcsin_lbound)
apply (erule (1) arcsin_ubound)
apply simp
apply (subgoal_tac "¦x¦² ≤ 1²", simp)
apply (rule power_mono, simp, simp)
done

lemma sin_arccos: "[|-1 ≤ x; x ≤ 1|] ==> sin (arccos x) = sqrt (1 - x²)"
apply (subgoal_tac "x² ≤ 1")
apply (rule power2_eq_imp_eq)
apply (simp add: sin_squared_eq)
apply (rule sin_ge_zero)
apply (erule (1) arccos_lbound)
apply (erule (1) arccos_ubound)
apply simp
apply (subgoal_tac "¦x¦² ≤ 1²", simp)
apply (rule power_mono, simp, simp)
done

lemma arctan [simp]:
     "- (pi/2) < arctan y  & arctan y < pi/2 & tan (arctan y) = y"
unfolding arctan_def by (rule theI' [OF tan_total])

lemma tan_arctan: "tan(arctan y) = y"
by auto

lemma arctan_bounded: "- (pi/2) < arctan y  & arctan y < pi/2"
by (auto simp only: arctan)

lemma arctan_lbound: "- (pi/2) < arctan y"
by auto

lemma arctan_ubound: "arctan y < pi/2"
by (auto simp only: arctan)

lemma arctan_tan: 
      "[|-(pi/2) < x; x < pi/2 |] ==> arctan(tan x) = x"
apply (unfold arctan_def)
apply (rule the1_equality)
apply (rule tan_total, auto)
done

lemma arctan_zero_zero [simp]: "arctan 0 = 0"
by (insert arctan_tan [of 0], simp)

lemma cos_arctan_not_zero [simp]: "cos(arctan x) ≠ 0"
apply (auto simp add: cos_zero_iff)
apply (case_tac "n")
apply (case_tac [3] "n")
apply (cut_tac [2] y = x in arctan_ubound)
apply (cut_tac [4] y = x in arctan_lbound) 
apply (auto simp add: real_of_nat_Suc left_distrib mult_less_0_iff)
done

lemma tan_sec: "cos x ≠ 0 ==> 1 + tan(x) ^ 2 = inverse(cos x) ^ 2"
apply (rule power_inverse [THEN subst])
apply (rule_tac c1 = "(cos x)²" in real_mult_right_cancel [THEN iffD1])
apply (auto dest: field_power_not_zero
        simp add: power_mult_distrib left_distrib power_divide tan_def 
                  mult_assoc power_inverse [symmetric])
done

lemma isCont_inverse_function2:
  fixes f g :: "real => real" shows
  "[|a < x; x < b;
    ∀z. a ≤ z ∧ z ≤ b --> g (f z) = z;
    ∀z. a ≤ z ∧ z ≤ b --> isCont f z|]
   ==> isCont g (f x)"
apply (rule isCont_inverse_function
       [where f=f and d="min (x - a) (b - x)"])
apply (simp_all add: abs_le_iff)
done

lemma isCont_arcsin: "[|-1 < x; x < 1|] ==> isCont arcsin x"
apply (subgoal_tac "isCont arcsin (sin (arcsin x))", simp)
apply (rule isCont_inverse_function2 [where f=sin])
apply (erule (1) arcsin_lt_bounded [THEN conjunct1])
apply (erule (1) arcsin_lt_bounded [THEN conjunct2])
apply (fast intro: arcsin_sin, simp)
done

lemma isCont_arccos: "[|-1 < x; x < 1|] ==> isCont arccos x"
apply (subgoal_tac "isCont arccos (cos (arccos x))", simp)
apply (rule isCont_inverse_function2 [where f=cos])
apply (erule (1) arccos_lt_bounded [THEN conjunct1])
apply (erule (1) arccos_lt_bounded [THEN conjunct2])
apply (fast intro: arccos_cos, simp)
done

lemma isCont_arctan: "isCont arctan x"
apply (rule arctan_lbound [of x, THEN dense, THEN exE], clarify)
apply (rule arctan_ubound [of x, THEN dense, THEN exE], clarify)
apply (subgoal_tac "isCont arctan (tan (arctan x))", simp)
apply (erule (1) isCont_inverse_function2 [where f=tan])
apply (clarify, rule arctan_tan)
apply (erule (1) order_less_le_trans)
apply (erule (1) order_le_less_trans)
apply (clarify, rule isCont_tan)
apply (rule less_imp_neq [symmetric])
apply (rule cos_gt_zero_pi)
apply (erule (1) order_less_le_trans)
apply (erule (1) order_le_less_trans)
done

lemma DERIV_arcsin:
  "[|-1 < x; x < 1|] ==> DERIV arcsin x :> inverse (sqrt (1 - x²))"
apply (rule DERIV_inverse_function [where f=sin and a="-1" and b="1"])
apply (rule lemma_DERIV_subst [OF DERIV_sin])
apply (simp add: cos_arcsin)
apply (subgoal_tac "¦x¦² < 1²", simp)
apply (rule power_strict_mono, simp, simp, simp)
apply assumption
apply assumption
apply simp
apply (erule (1) isCont_arcsin)
done

lemma DERIV_arccos:
  "[|-1 < x; x < 1|] ==> DERIV arccos x :> inverse (- sqrt (1 - x²))"
apply (rule DERIV_inverse_function [where f=cos and a="-1" and b="1"])
apply (rule lemma_DERIV_subst [OF DERIV_cos])
apply (simp add: sin_arccos)
apply (subgoal_tac "¦x¦² < 1²", simp)
apply (rule power_strict_mono, simp, simp, simp)
apply assumption
apply assumption
apply simp
apply (erule (1) isCont_arccos)
done

lemma DERIV_arctan: "DERIV arctan x :> inverse (1 + x²)"
apply (rule DERIV_inverse_function [where f=tan and a="x - 1" and b="x + 1"])
apply (rule lemma_DERIV_subst [OF DERIV_tan])
apply (rule cos_arctan_not_zero)
apply (simp add: power_inverse tan_sec [symmetric])
apply (subgoal_tac "0 < 1 + x²", simp)
apply (simp add: add_pos_nonneg)
apply (simp, simp, simp, rule isCont_arctan)
done

subsection {* More Theorems about Sin and Cos *}

lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
proof -
  let ?c = "cos (pi / 4)" and ?s = "sin (pi / 4)"
  have nonneg: "0 ≤ ?c"
    by (rule cos_ge_zero, rule order_trans [where y=0], simp_all)
  have "0 = cos (pi / 4 + pi / 4)"
    by simp
  also have "cos (pi / 4 + pi / 4) = ?c² - ?s²"
    by (simp only: cos_add power2_eq_square)
  also have "… = 2 * ?c² - 1"
    by (simp add: sin_squared_eq)
  finally have "?c² = (sqrt 2 / 2)²"
    by (simp add: power_divide)
  thus ?thesis
    using nonneg by (rule power2_eq_imp_eq) simp
qed

lemma cos_30: "cos (pi / 6) = sqrt 3 / 2"
proof -
  let ?c = "cos (pi / 6)" and ?s = "sin (pi / 6)"
  have pos_c: "0 < ?c"
    by (rule cos_gt_zero, simp, simp)
  have "0 = cos (pi / 6 + pi / 6 + pi / 6)"
    by simp
  also have "… = (?c * ?c - ?s * ?s) * ?c - (?s * ?c + ?c * ?s) * ?s"
    by (simp only: cos_add sin_add)
  also have "… = ?c * (?c² - 3 * ?s²)"
    by (simp add: algebra_simps power2_eq_square)
  finally have "?c² = (sqrt 3 / 2)²"
    using pos_c by (simp add: sin_squared_eq power_divide)
  thus ?thesis
    using pos_c [THEN order_less_imp_le]
    by (rule power2_eq_imp_eq) simp
qed

lemma sin_45: "sin (pi / 4) = sqrt 2 / 2"
proof -
  have "sin (pi / 4) = cos (pi / 2 - pi / 4)" by (rule sin_cos_eq)
  also have "pi / 2 - pi / 4 = pi / 4" by simp
  also have "cos (pi / 4) = sqrt 2 / 2" by (rule cos_45)
  finally show ?thesis .
qed

lemma sin_60: "sin (pi / 3) = sqrt 3 / 2"
proof -
  have "sin (pi / 3) = cos (pi / 2 - pi / 3)" by (rule sin_cos_eq)
  also have "pi / 2 - pi / 3 = pi / 6" by simp
  also have "cos (pi / 6) = sqrt 3 / 2" by (rule cos_30)
  finally show ?thesis .
qed

lemma cos_60: "cos (pi / 3) = 1 / 2"
apply (rule power2_eq_imp_eq)
apply (simp add: cos_squared_eq sin_60 power_divide)
apply (rule cos_ge_zero, rule order_trans [where y=0], simp_all)
done

lemma sin_30: "sin (pi / 6) = 1 / 2"
proof -
  have "sin (pi / 6) = cos (pi / 2 - pi / 6)" by (rule sin_cos_eq)
  also have "pi / 2 - pi / 6 = pi / 3" by simp
  also have "cos (pi / 3) = 1 / 2" by (rule cos_60)
  finally show ?thesis .
qed

lemma tan_30: "tan (pi / 6) = 1 / sqrt 3"
unfolding tan_def by (simp add: sin_30 cos_30)

lemma tan_45: "tan (pi / 4) = 1"
unfolding tan_def by (simp add: sin_45 cos_45)

lemma tan_60: "tan (pi / 3) = sqrt 3"
unfolding tan_def by (simp add: sin_60 cos_60)

text{*NEEDED??*}
lemma [simp]:
     "sin (x + 1 / 2 * real (Suc m) * pi) =  
      cos (x + 1 / 2 * real  (m) * pi)"
by (simp only: cos_add sin_add real_of_nat_Suc left_distrib right_distrib, auto)

text{*NEEDED??*}
lemma [simp]:
     "sin (x + real (Suc m) * pi / 2) =  
      cos (x + real (m) * pi / 2)"
by (simp only: cos_add sin_add real_of_nat_Suc add_divide_distrib left_distrib, auto)

lemma DERIV_sin_add [simp]: "DERIV (%x. sin (x + k)) xa :> cos (xa + k)"
apply (rule lemma_DERIV_subst)
apply (rule_tac f = sin and g = "%x. x + k" in DERIV_chain2)
apply (best intro!: DERIV_intros intro: DERIV_chain2)+
apply (simp (no_asm))
done

lemma sin_cos_npi [simp]: "sin (real (Suc (2 * n)) * pi / 2) = (-1) ^ n"
proof -
  have "sin ((real n + 1/2) * pi) = cos (real n * pi)"
    by (auto simp add: algebra_simps sin_add)
  thus ?thesis
    by (simp add: real_of_nat_Suc left_distrib add_divide_distrib 
                  mult_commute [of pi])
qed

lemma cos_2npi [simp]: "cos (2 * real (n::nat) * pi) = 1"
by (simp add: cos_double mult_assoc power_add [symmetric] numeral_2_eq_2)

lemma cos_3over2_pi [simp]: "cos (3 / 2 * pi) = 0"
apply (subgoal_tac "cos (pi + pi/2) = 0", simp)
apply (subst cos_add, simp)
done

lemma sin_2npi [simp]: "sin (2 * real (n::nat) * pi) = 0"
by (auto simp add: mult_assoc)

lemma sin_3over2_pi [simp]: "sin (3 / 2 * pi) = - 1"
apply (subgoal_tac "sin (pi + pi/2) = - 1", simp)
apply (subst sin_add, simp)
done

(*NEEDED??*)
lemma [simp]:
     "cos(x + 1 / 2 * real(Suc m) * pi) = -sin (x + 1 / 2 * real m * pi)"
apply (simp only: cos_add sin_add real_of_nat_Suc right_distrib left_distrib minus_mult_right, auto)
done

(*NEEDED??*)
lemma [simp]: "cos (x + real(Suc m) * pi / 2) = -sin (x + real m * pi / 2)"
by (simp only: cos_add sin_add real_of_nat_Suc left_distrib add_divide_distrib, auto)

lemma cos_pi_eq_zero [simp]: "cos (pi * real (Suc (2 * m)) / 2) = 0"
by (simp only: cos_add sin_add real_of_nat_Suc left_distrib right_distrib add_divide_distrib, auto)

lemma DERIV_cos_add [simp]: "DERIV (%x. cos (x + k)) xa :> - sin (xa + k)"
apply (rule lemma_DERIV_subst)
apply (rule_tac f = cos and g = "%x. x + k" in DERIV_chain2)
apply (best intro!: DERIV_intros intro: DERIV_chain2)+
apply (simp (no_asm))
done

lemma sin_zero_abs_cos_one: "sin x = 0 ==> ¦cos x¦ = 1"
by (auto simp add: sin_zero_iff even_mult_two_ex)

lemma cos_one_sin_zero: "cos x = 1 ==> sin x = 0"
by (cut_tac x = x in sin_cos_squared_add3, auto)

subsection {* Machins formula *}

lemma tan_total_pi4: assumes "¦x¦ < 1"
  shows "∃ z. - (pi / 4) < z ∧ z < pi / 4 ∧ tan z = x"
proof -
  obtain z where "- (pi / 2) < z" and "z < pi / 2" and "tan z = x" using tan_total by blast
  have "tan (pi / 4) = 1" and "tan (- (pi / 4)) = - 1" using tan_45 tan_minus by auto
  have "z ≠ pi / 4" 
  proof (rule ccontr)
    assume "¬ (z ≠ pi / 4)" hence "z = pi / 4" by auto
    have "tan z = 1" unfolding `z = pi / 4` `tan (pi / 4) = 1` ..
    thus False unfolding `tan z = x` using `¦x¦ < 1` by auto
  qed
  have "z ≠ - (pi / 4)"
  proof (rule ccontr)
    assume "¬ (z ≠ - (pi / 4))" hence "z = - (pi / 4)" by auto
    have "tan z = - 1" unfolding `z = - (pi / 4)` `tan (- (pi / 4)) = - 1` ..
    thus False unfolding `tan z = x` using `¦x¦ < 1` by auto
  qed

  have "z < pi / 4"
  proof (rule ccontr)
    assume "¬ (z < pi / 4)" hence "pi / 4 < z" using `z ≠ pi / 4` by auto
    have "- (pi / 2) < pi / 4" using m2pi_less_pi by auto
    from tan_monotone[OF this `pi / 4 < z` `z < pi / 2`] 
    have "1 < x" unfolding `tan z = x` `tan (pi / 4) = 1` .
    thus False using `¦x¦ < 1` by auto
  qed
  moreover 
  have "-(pi / 4) < z"
  proof (rule ccontr)
    assume "¬ (-(pi / 4) < z)" hence "z < - (pi / 4)" using `z ≠ - (pi / 4)` by auto
    have "-(pi / 4) < pi / 2" using m2pi_less_pi by auto
    from tan_monotone[OF `-(pi / 2) < z` `z < -(pi / 4)` this]
    have "x < - 1" unfolding `tan z = x` `tan (-(pi / 4)) = - 1` .
    thus False using `¦x¦ < 1` by auto
  qed
  ultimately show ?thesis using `tan z = x` by auto
qed

lemma arctan_add: assumes "¦x¦ ≤ 1" and "¦y¦ < 1"
  shows "arctan x + arctan y = arctan ((x + y) / (1 - x * y))"
proof -
  obtain y' where "-(pi/4) < y'" and "y' < pi/4" and "tan y' = y" using tan_total_pi4[OF `¦y¦ < 1`] by blast

  have "pi / 4 < pi / 2" by auto

  have "∃ x'. -(pi/4) ≤ x' ∧ x' ≤ pi/4 ∧ tan x' = x"
  proof (cases "¦x¦ < 1")
    case True from tan_total_pi4[OF this] obtain x' where "-(pi/4) < x'" and "x' < pi/4" and "tan x' = x" by blast
    hence "-(pi/4) ≤ x'" and "x' ≤ pi/4" and "tan x' = x" by auto
    thus ?thesis by auto
  next
    case False
    show ?thesis
    proof (cases "x = 1")
      case True hence "tan (pi/4) = x" using tan_45 by auto
      moreover 
      have "- pi ≤ pi" unfolding minus_le_self_iff by auto
      hence "-(pi/4) ≤ pi/4" and "pi/4 ≤ pi/4" by auto
      ultimately show ?thesis by blast
    next
      case False hence "x = -1" using `¬ ¦x¦ < 1` and `¦x¦ ≤ 1` by auto
      hence "tan (-(pi/4)) = x" using tan_45 tan_minus by auto
      moreover 
      have "- pi ≤ pi" unfolding minus_le_self_iff by auto
      hence "-(pi/4) ≤ pi/4" and "-(pi/4) ≤ -(pi/4)" by auto
      ultimately show ?thesis by blast
    qed
  qed
  then obtain x' where "-(pi/4) ≤ x'" and "x' ≤ pi/4" and "tan x' = x" by blast
  hence "-(pi/2) < x'" and "x' < pi/2" using order_le_less_trans[OF `x' ≤ pi/4` `pi / 4 < pi / 2`] by auto

  have "cos x' ≠ 0" using cos_gt_zero_pi[THEN less_imp_neq] and `-(pi/2) < x'` and `x' < pi/2` by auto
  moreover have "cos y' ≠ 0" using cos_gt_zero_pi[THEN less_imp_neq] and `-(pi/4) < y'` and `y' < pi/4` by auto
  ultimately have "cos x' * cos y' ≠ 0" by auto

  have divide_nonzero_divide: "!! A B C :: real. C ≠ 0 ==> (A / C) / (B / C) = A / B" by auto
  have divide_mult_commute: "!! A B C D :: real. A * B / (C * D) = (A / C) * (B / D)" by auto

  have "tan (x' + y') = sin (x' + y') / (cos x' * cos y' - sin x' * sin y')" unfolding tan_def cos_add ..
  also have "… = (tan x' + tan y') / ((cos x' * cos y' - sin x' * sin y') / (cos x' * cos y'))" unfolding add_tan_eq[OF `cos x' ≠ 0` `cos y' ≠ 0`] divide_nonzero_divide[OF `cos x' * cos y' ≠ 0`] ..
  also have "… = (tan x' + tan y') / (1 - tan x' * tan y')" unfolding tan_def diff_divide_distrib divide_self[OF `cos x' * cos y' ≠ 0`] unfolding divide_mult_commute ..
  finally have tan_eq: "tan (x' + y') = (x + y) / (1 - x * y)" unfolding `tan y' = y` `tan x' = x` .

  have "arctan (tan (x' + y')) = x' + y'" using `-(pi/4) < y'` `-(pi/4) ≤ x'` `y' < pi/4` and `x' ≤ pi/4` by (auto intro!: arctan_tan)
  moreover have "arctan (tan (x')) = x'" using `-(pi/2) < x'` and `x' < pi/2` by (auto intro!: arctan_tan)
  moreover have "arctan (tan (y')) = y'" using `-(pi/4) < y'` and `y' < pi/4` by (auto intro!: arctan_tan)
  ultimately have "arctan x + arctan y = arctan (tan (x' + y'))" unfolding `tan y' = y` [symmetric] `tan x' = x`[symmetric] by auto
  thus "arctan x + arctan y = arctan ((x + y) / (1 - x * y))" unfolding tan_eq .
qed

lemma arctan1_eq_pi4: "arctan 1 = pi / 4" unfolding tan_45[symmetric] by (rule arctan_tan, auto simp add: m2pi_less_pi)

theorem machin: "pi / 4 = 4 * arctan (1/5) - arctan (1 / 239)"
proof -
  have "¦1 / 5¦ < (1 :: real)" by auto
  from arctan_add[OF less_imp_le[OF this] this]
  have "2 * arctan (1 / 5) = arctan (5 / 12)" by auto
  moreover
  have "¦5 / 12¦ < (1 :: real)" by auto
  from arctan_add[OF less_imp_le[OF this] this]
  have "2 * arctan (5 / 12) = arctan (120 / 119)" by auto
  moreover 
  have "¦1¦ ≤ (1::real)" and "¦1 / 239¦ < (1::real)" by auto
  from arctan_add[OF this]
  have "arctan 1 + arctan (1 / 239) = arctan (120 / 119)" by auto
  ultimately have "arctan 1 + arctan (1 / 239) = 4 * arctan (1 / 5)" by auto
  thus ?thesis unfolding arctan1_eq_pi4 by algebra
qed
subsection {* Introducing the arcus tangens power series *}

lemma monoseq_arctan_series: fixes x :: real
  assumes "¦x¦ ≤ 1" shows "monoseq (λ n. 1 / real (n*2+1) * x^(n*2+1))" (is "monoseq ?a")
proof (cases "x = 0") case True thus ?thesis unfolding monoseq_def One_nat_def by auto
next
  case False
  have "norm x ≤ 1" and "x ≤ 1" and "-1 ≤ x" using assms by auto
  show "monoseq ?a"
  proof -
    { fix n fix x :: real assume "0 ≤ x" and "x ≤ 1"
      have "1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) ≤ 1 / real (Suc (n * 2)) * x ^ Suc (n * 2)"
      proof (rule mult_mono)
        show "1 / real (Suc (Suc n * 2)) ≤ 1 / real (Suc (n * 2))" by (rule frac_le) simp_all
        show "0 ≤ 1 / real (Suc (n * 2))" by auto
        show "x ^ Suc (Suc n * 2) ≤ x ^ Suc (n * 2)" by (rule power_decreasing) (simp_all add: `0 ≤ x` `x ≤ 1`)
        show "0 ≤ x ^ Suc (Suc n * 2)" by (rule zero_le_power) (simp add: `0 ≤ x`)
      qed
    } note mono = this
    
    show ?thesis
    proof (cases "0 ≤ x")
      case True from mono[OF this `x ≤ 1`, THEN allI]
      show ?thesis unfolding Suc_plus1[symmetric] by (rule mono_SucI2)
    next
      case False hence "0 ≤ -x" and "-x ≤ 1" using `-1 ≤ x` by auto
      from mono[OF this]
      have "!!n. 1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) ≥ 1 / real (Suc (n * 2)) * x ^ Suc (n * 2)" using `0 ≤ -x` by auto
      thus ?thesis unfolding Suc_plus1[symmetric] by (rule mono_SucI1[OF allI])
    qed
  qed
qed

lemma zeroseq_arctan_series: fixes x :: real
  assumes "¦x¦ ≤ 1" shows "(λ n. 1 / real (n*2+1) * x^(n*2+1)) ----> 0" (is "?a ----> 0")
proof (cases "x = 0") case True thus ?thesis unfolding One_nat_def by (auto simp add: LIMSEQ_const)
next
  case False
  have "norm x ≤ 1" and "x ≤ 1" and "-1 ≤ x" using assms by auto
  show "?a ----> 0"
  proof (cases "¦x¦ < 1")
    case True hence "norm x < 1" by auto
    from LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_power_zero[OF `norm x < 1`, THEN LIMSEQ_Suc]]
    have "(λn. 1 / real (n + 1) * x ^ (n + 1)) ----> 0"
      unfolding inverse_eq_divide Suc_plus1 by simp
    then show ?thesis using pos2 by (rule LIMSEQ_linear)
  next
    case False hence "x = -1 ∨ x = 1" using `¦x¦ ≤ 1` by auto
    hence n_eq: "!! n. x ^ (n * 2 + 1) = x" unfolding One_nat_def by auto
    from LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat[THEN LIMSEQ_linear, OF pos2, unfolded inverse_eq_divide] LIMSEQ_const[of x]]
    show ?thesis unfolding n_eq Suc_plus1 by auto
  qed
qed

lemma summable_arctan_series: fixes x :: real and n :: nat
  assumes "¦x¦ ≤ 1" shows "summable (λ k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))" (is "summable (?c x)")
  by (rule summable_Leibniz(1), rule zeroseq_arctan_series[OF assms], rule monoseq_arctan_series[OF assms])

lemma less_one_imp_sqr_less_one: fixes x :: real assumes "¦x¦ < 1" shows "x^2 < 1"
proof -
  from mult_mono1[OF less_imp_le[OF `¦x¦ < 1`] abs_ge_zero[of x]]
  have "¦ x^2 ¦ < 1" using `¦ x ¦ < 1` unfolding numeral_2_eq_2 power_Suc2 by auto
  thus ?thesis using zero_le_power2 by auto
qed 

lemma DERIV_arctan_series: assumes "¦ x ¦ < 1"
  shows "DERIV (λ x'. ∑ k. (-1)^k * (1 / real (k*2+1) * x' ^ (k*2+1))) x :> (∑ k. (-1)^k * x^(k*2))" (is "DERIV ?arctan _ :> ?Int")
proof -
  let "?f n" = "if even n then (-1)^(n div 2) * 1 / real (Suc n) else 0"

  { fix n :: nat assume "even n" hence "2 * (n div 2) = n" by presburger } note n_even=this
  have if_eq: "!! n x'. ?f n * real (Suc n) * x'^n = (if even n then (-1)^(n div 2) * x'^(2 * (n div 2)) else 0)" using n_even by auto

  { fix x :: real assume "¦x¦ < 1" hence "x^2 < 1" by (rule less_one_imp_sqr_less_one)
    have "summable (λ n. -1 ^ n * (x^2) ^n)"
      by (rule summable_Leibniz(1), auto intro!: LIMSEQ_realpow_zero monoseq_realpow `x^2 < 1` order_less_imp_le[OF `x^2 < 1`])
    hence "summable (λ n. -1 ^ n * x^(2*n))" unfolding power_mult .
  } note summable_Integral = this

  { fix f :: "nat => real"
    have "!! x. f sums x = (λ n. if even n then f (n div 2) else 0) sums x"
    proof
      fix x :: real assume "f sums x" 
      from sums_if[OF sums_zero this]
      show "(λ n. if even n then f (n div 2) else 0) sums x" by auto
    next
      fix x :: real assume "(λ n. if even n then f (n div 2) else 0) sums x"
      from LIMSEQ_linear[OF this[unfolded sums_def] pos2, unfolded sum_split_even_odd[unfolded mult_commute]]
      show "f sums x" unfolding sums_def by auto
    qed
    hence "op sums f = op sums (λ n. if even n then f (n div 2) else 0)" ..
  } note sums_even = this

  have Int_eq: "(∑ n. ?f n * real (Suc n) * x^n) = ?Int" unfolding if_eq mult_commute[of _ 2] suminf_def sums_even[of "λ n. -1 ^ n * x ^ (2 * n)", symmetric]
    by auto

  { fix x :: real
    have if_eq': "!! n. (if even n then -1 ^ (n div 2) * 1 / real (Suc n) else 0) * x ^ Suc n = 
      (if even n then -1 ^ (n div 2) * (1 / real (Suc (2 * (n div 2))) * x ^ Suc (2 * (n div 2))) else 0)"
      using n_even by auto
    have idx_eq: "!! n. n * 2 + 1 = Suc (2 * n)" by auto 
    have "(∑ n. ?f n * x^(Suc n)) = ?arctan x" unfolding if_eq' idx_eq suminf_def sums_even[of "λ n. -1 ^ n * (1 / real (Suc (2 * n)) * x ^ Suc (2 * n))", symmetric]
      by auto
  } note arctan_eq = this

  have "DERIV (λ x. ∑ n. ?f n * x^(Suc n)) x :> (∑ n. ?f n * real (Suc n) * x^n)"
  proof (rule DERIV_power_series')
    show "x ∈ {- 1 <..< 1}" using `¦ x ¦ < 1` by auto
    { fix x' :: real assume x'_bounds: "x' ∈ {- 1 <..< 1}"
      hence "¦x'¦ < 1" by auto

      let ?S = "∑ n. (-1)^n * x'^(2 * n)"
      show "summable (λ n. ?f n * real (Suc n) * x'^n)" unfolding if_eq
        by (rule sums_summable[where l="0 + ?S"], rule sums_if, rule sums_zero, rule summable_sums, rule summable_Integral[OF `¦x'¦ < 1`])
    }
  qed auto
  thus ?thesis unfolding Int_eq arctan_eq .
qed

lemma arctan_series: assumes "¦ x ¦ ≤ 1"
  shows "arctan x = (∑ k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))" (is "_ = suminf (λ n. ?c x n)")
proof -
  let "?c' x n" = "(-1)^n * x^(n*2)"

  { fix r x :: real assume "0 < r" and "r < 1" and "¦ x ¦ < r"
    have "¦x¦ < 1" using `r < 1` and `¦x¦ < r` by auto
    from DERIV_arctan_series[OF this]
    have "DERIV (λ x. suminf (?c x)) x :> (suminf (?c' x))" .
  } note DERIV_arctan_suminf = this

  { fix x :: real assume "¦x¦ ≤ 1" note summable_Leibniz[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]] }
  note arctan_series_borders = this

  { fix x :: real assume "¦x¦ < 1" have "arctan x = (∑ k. ?c x k)"
  proof -
    obtain r where "¦x¦ < r" and "r < 1" using dense[OF `¦x¦ < 1`] by blast
    hence "0 < r" and "-r < x" and "x < r" by auto

    have suminf_eq_arctan_bounded: "!! x a b. [| -r < a ; b < r ; a < b ; a ≤ x ; x ≤ b |] ==> suminf (?c x) - arctan x = suminf (?c a) - arctan a"
    proof -
      fix x a b assume "-r < a" and "b < r" and "a < b" and "a ≤ x" and "x ≤ b"
      hence "¦x¦ < r" by auto
      show "suminf (?c x) - arctan x = suminf (?c a) - arctan a"
      proof (rule DERIV_isconst2[of "a" "b"])
        show "a < b" and "a ≤ x" and "x ≤ b" using `a < b` `a ≤ x` `x ≤ b` by auto
        have "∀ x. -r < x ∧ x < r --> DERIV (λ x. suminf (?c x) - arctan x) x :> 0"
        proof (rule allI, rule impI)
          fix x assume "-r < x ∧ x < r" hence "¦x¦ < r" by auto
          hence "¦x¦ < 1" using `r < 1` by auto
          have "¦ - (x^2) ¦ < 1" using less_one_imp_sqr_less_one[OF `¦x¦ < 1`] by auto
          hence "(λ n. (- (x^2)) ^ n) sums (1 / (1 - (- (x^2))))" unfolding real_norm_def[symmetric] by (rule geometric_sums)
          hence "(?c' x) sums (1 / (1 - (- (x^2))))" unfolding power_mult_distrib[symmetric] power_mult nat_mult_commute[of _ 2] by auto
          hence suminf_c'_eq_geom: "inverse (1 + x^2) = suminf (?c' x)" using sums_unique unfolding inverse_eq_divide by auto
          have "DERIV (λ x. suminf (?c x)) x :> (inverse (1 + x^2))" unfolding suminf_c'_eq_geom
            by (rule DERIV_arctan_suminf[OF `0 < r` `r < 1` `¦x¦ < r`])
          from DERIV_add_minus[OF this DERIV_arctan]
          show "DERIV (λ x. suminf (?c x) - arctan x) x :> 0" unfolding diff_minus by auto
        qed
        hence DERIV_in_rball: "∀ y. a ≤ y ∧ y ≤ b --> DERIV (λ x. suminf (?c x) - arctan x) y :> 0" using `-r < a` `b < r` by auto
        thus "∀ y. a < y ∧ y < b --> DERIV (λ x. suminf (?c x) - arctan x) y :> 0" using `¦x¦ < r` by auto
        show "∀ y. a ≤ y ∧ y ≤ b --> isCont (λ x. suminf (?c x) - arctan x) y" using DERIV_in_rball DERIV_isCont by auto
      qed
    qed
    
    have suminf_arctan_zero: "suminf (?c 0) - arctan 0 = 0"
      unfolding Suc_plus1[symmetric] power_Suc2 mult_zero_right arctan_zero_zero suminf_zero by auto
    
    have "suminf (?c x) - arctan x = 0"
    proof (cases "x = 0")
      case True thus ?thesis using suminf_arctan_zero by auto
    next
      case False hence "0 < ¦x¦" and "- ¦x¦ < ¦x¦" by auto
      have "suminf (?c (-¦x¦)) - arctan (-¦x¦) = suminf (?c 0) - arctan 0"
        by (rule suminf_eq_arctan_bounded[where x="0" and a="-¦x¦" and b="¦x¦", symmetric], auto simp add: `¦x¦ < r` `-¦x¦ < ¦x¦`)
      moreover
      have "suminf (?c x) - arctan x = suminf (?c (-¦x¦)) - arctan (-¦x¦)"
        by (rule suminf_eq_arctan_bounded[where x="x" and a="-¦x¦" and b="¦x¦"], auto simp add: `¦x¦ < r` `-¦x¦ < ¦x¦`)
      ultimately 
      show ?thesis using suminf_arctan_zero by auto
    qed
    thus ?thesis by auto
  qed } note when_less_one = this

  show "arctan x = suminf (λ n. ?c x n)"
  proof (cases "¦x¦ < 1")
    case True thus ?thesis by (rule when_less_one)
  next case False hence "¦x¦ = 1" using `¦x¦ ≤ 1` by auto
    let "?a x n" = "¦1 / real (n*2+1) * x^(n*2+1)¦"
    let "?diff x n" = "¦ arctan x - (∑ i = 0..<n. ?c x i)¦"
    { fix n :: nat
      have "0 < (1 :: real)" by auto
      moreover
      { fix x :: real assume "0 < x" and "x < 1" hence "¦x¦ ≤ 1" and "¦x¦ < 1" by auto
        from `0 < x` have "0 < 1 / real (0 * 2 + (1::nat)) * x ^ (0 * 2 + 1)" by auto
        note bounds = mp[OF arctan_series_borders(2)[OF `¦x¦ ≤ 1`] this, unfolded when_less_one[OF `¦x¦ < 1`, symmetric], THEN spec]
        have "0 < 1 / real (n*2+1) * x^(n*2+1)" by (rule mult_pos_pos, auto simp only: zero_less_power[OF `0 < x`], auto)
        hence a_pos: "?a x n = 1 / real (n*2+1) * x^(n*2+1)" by (rule abs_of_pos)
        have "?diff x n ≤ ?a x n"
        proof (cases "even n")
          case True hence sgn_pos: "(-1)^n = (1::real)" by auto
          from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto
          from bounds[of m, unfolded this atLeastAtMost_iff]
          have "¦arctan x - (∑i = 0..<n. (?c x i))¦ ≤ (∑i = 0..<n + 1. (?c x i)) - (∑i = 0..<n. (?c x i))" by auto
          also have "… = ?c x n" unfolding One_nat_def by auto
          also have "… = ?a x n" unfolding sgn_pos a_pos by auto
          finally show ?thesis .
        next
          case False hence sgn_neg: "(-1)^n = (-1::real)" by auto
          from `odd n` obtain m where m_def: "2 * m + 1 = n" unfolding odd_Suc_mult_two_ex by auto
          hence m_plus: "2 * (m + 1) = n + 1" by auto
          from bounds[of "m + 1", unfolded this atLeastAtMost_iff, THEN conjunct1] bounds[of m, unfolded m_def atLeastAtMost_iff, THEN conjunct2]
          have "¦arctan x - (∑i = 0..<n. (?c x i))¦ ≤ (∑i = 0..<n. (?c x i)) - (∑i = 0..<n+1. (?c x i))" by auto
          also have "… = - ?c x n" unfolding One_nat_def by auto
          also have "… = ?a x n" unfolding sgn_neg a_pos by auto
          finally show ?thesis .
        qed
        hence "0 ≤ ?a x n - ?diff x n" by auto
      }
      hence "∀ x ∈ { 0 <..< 1 }. 0 ≤ ?a x n - ?diff x n" by auto
      moreover have "!!x. isCont (λ x. ?a x n - ?diff x n) x"
        unfolding real_diff_def divide_inverse
        by (auto intro!: isCont_add isCont_rabs isCont_ident isCont_minus isCont_arctan isCont_inverse isCont_mult isCont_power isCont_const isCont_setsum)
      ultimately have "0 ≤ ?a 1 n - ?diff 1 n" by (rule LIM_less_bound)
      hence "?diff 1 n ≤ ?a 1 n" by auto
    }
    have "?a 1 ----> 0"
      unfolding LIMSEQ_rabs_zero power_one divide_inverse One_nat_def
      by (auto intro!: LIMSEQ_mult LIMSEQ_linear LIMSEQ_inverse_real_of_nat)
    have "?diff 1 ----> 0"
    proof (rule LIMSEQ_I)
      fix r :: real assume "0 < r"
      obtain N :: nat where N_I: "!! n. N ≤ n ==> ?a 1 n < r" using LIMSEQ_D[OF `?a 1 ----> 0` `0 < r`] by auto
      { fix n assume "N ≤ n" from `?diff 1 n ≤ ?a 1 n` N_I[OF this]
        have "norm (?diff 1 n - 0) < r" by auto }
      thus "∃ N. ∀ n ≥ N. norm (?diff 1 n - 0) < r" by blast
    qed
    from this[unfolded LIMSEQ_rabs_zero real_diff_def add_commute[of "arctan 1"], THEN LIMSEQ_add_const, of "- arctan 1", THEN LIMSEQ_minus]
    have "(?c 1) sums (arctan 1)" unfolding sums_def by auto
    hence "arctan 1 = (∑ i. ?c 1 i)" by (rule sums_unique)
    
    show ?thesis
    proof (cases "x = 1", simp add: `arctan 1 = (∑ i. ?c 1 i)`)
      assume "x ≠ 1" hence "x = -1" using `¦x¦ = 1` by auto
      
      have "- (pi / 2) < 0" using pi_gt_zero by auto
      have "- (2 * pi) < 0" using pi_gt_zero by auto
      
      have c_minus_minus: "!! i. ?c (- 1) i = - ?c 1 i" unfolding One_nat_def by auto
    
      have "arctan (- 1) = arctan (tan (-(pi / 4)))" unfolding tan_45 tan_minus ..
      also have "… = - (pi / 4)" by (rule arctan_tan, auto simp add: order_less_trans[OF `- (pi / 2) < 0` pi_gt_zero])
      also have "… = - (arctan (tan (pi / 4)))" unfolding neg_equal_iff_equal by (rule arctan_tan[symmetric], auto simp add: order_less_trans[OF `- (2 * pi) < 0` pi_gt_zero])
      also have "… = - (arctan 1)" unfolding tan_45 ..
      also have "… = - (∑ i. ?c 1 i)" using `arctan 1 = (∑ i. ?c 1 i)` by auto
      also have "… = (∑ i. ?c (- 1) i)" using suminf_minus[OF sums_summable[OF `(?c 1) sums (arctan 1)`]] unfolding c_minus_minus by auto
      finally show ?thesis using `x = -1` by auto
    qed
  qed
qed

lemma arctan_half: fixes x :: real
  shows "arctan x = 2 * arctan (x / (1 + sqrt(1 + x^2)))"
proof -
  obtain y where low: "- (pi / 2) < y" and high: "y < pi / 2" and y_eq: "tan y = x" using tan_total by blast
  hence low2: "- (pi / 2) < y / 2" and high2: "y / 2 < pi / 2" by auto

  have divide_nonzero_divide: "!! A B C :: real. C ≠ 0 ==> A / B = (A / C) / (B / C)" by auto
  
  have "0 < cos y" using cos_gt_zero_pi[OF low high] .
  hence "cos y ≠ 0" and cos_sqrt: "sqrt ((cos y) ^ 2) = cos y" by auto

  have "1 + (tan y)^2 = 1 + sin y^2 / cos y^2" unfolding tan_def power_divide ..
  also have "… = cos y^2 / cos y^2 + sin y^2 / cos y^2" using `cos y ≠ 0` by auto
  also have "… = 1 / cos y^2" unfolding add_divide_distrib[symmetric] sin_cos_squared_add2 ..
  finally have "1 + (tan y)^2 = 1 / cos y^2" .

  have "sin y / (cos y + 1) = tan y / ((cos y + 1) / cos y)" unfolding tan_def divide_nonzero_divide[OF `cos y ≠ 0`, symmetric] ..
  also have "… = tan y / (1 + 1 / cos y)" using `cos y ≠ 0` unfolding add_divide_distrib by auto
  also have "… = tan y / (1 + 1 / sqrt(cos y^2))" unfolding cos_sqrt ..
  also have "… = tan y / (1 + sqrt(1 / cos y^2))" unfolding real_sqrt_divide by auto
  finally have eq: "sin y / (cos y + 1) = tan y / (1 + sqrt(1 + (tan y)^2))" unfolding `1 + (tan y)^2 = 1 / cos y^2` .

  have "arctan x = y" using arctan_tan low high y_eq by auto
  also have "… = 2 * (arctan (tan (y/2)))" using arctan_tan[OF low2 high2] by auto
  also have "… = 2 * (arctan (sin y / (cos y + 1)))" unfolding tan_half[OF low2 high2] by auto
  finally show ?thesis unfolding eq `tan y = x` .
qed

lemma arctan_monotone: assumes "x < y"
  shows "arctan x < arctan y"
proof -
  obtain z where "-(pi / 2) < z" and "z < pi / 2" and "tan z = x" using tan_total by blast
  obtain w where "-(pi / 2) < w" and "w < pi / 2" and "tan w = y" using tan_total by blast
  have "z < w" unfolding tan_monotone'[OF `-(pi / 2) < z` `z < pi / 2` `-(pi / 2) < w` `w < pi / 2`] `tan z = x` `tan w = y` using `x < y` .
  thus ?thesis
    unfolding `tan z = x`[symmetric] arctan_tan[OF `-(pi / 2) < z` `z < pi / 2`]
    unfolding `tan w = y`[symmetric] arctan_tan[OF `-(pi / 2) < w` `w < pi / 2`] .
qed

lemma arctan_monotone': assumes "x ≤ y" shows "arctan x ≤ arctan y"
proof (cases "x = y") 
  case False hence "x < y" using `x ≤ y` by auto from arctan_monotone[OF this] show ?thesis by auto
qed auto

lemma arctan_minus: "arctan (- x) = - arctan x" 
proof -
  obtain y where "- (pi / 2) < y" and "y < pi / 2" and "tan y = x" using tan_total by blast
  thus ?thesis unfolding `tan y = x`[symmetric] tan_minus[symmetric] using arctan_tan[of y] arctan_tan[of "-y"] by auto 
qed

lemma arctan_inverse: assumes "x ≠ 0" shows "arctan (1 / x) = sgn x * pi / 2 - arctan x"
proof -
  obtain y where "- (pi / 2) < y" and "y < pi / 2" and "tan y = x" using tan_total by blast
  hence "y = arctan x" unfolding `tan y = x`[symmetric] using arctan_tan by auto

  { fix y x :: real assume "0 < y" and "y < pi /2" and "y = arctan x" and "tan y = x" hence "- (pi / 2) < y" by auto
    have "tan y > 0" using tan_monotone'[OF _ _ `- (pi / 2) < y` `y < pi / 2`, of 0] tan_zero `0 < y` by auto
    hence "x > 0" using `tan y = x` by auto

    have "- (pi / 2) < pi / 2 - y" using `y > 0` `y < pi / 2` by auto
    moreover have "pi / 2 - y < pi / 2" using `y > 0` `y < pi / 2` by auto
    ultimately have "arctan (1 / x) = pi / 2 - y" unfolding `tan y = x`[symmetric] tan_inverse using arctan_tan by auto
    hence "arctan (1 / x) = sgn x * pi / 2 - arctan x" unfolding `y = arctan x` real_sgn_pos[OF `x > 0`] by auto
  } note pos_y = this

  show ?thesis
  proof (cases "y > 0")
    case True from pos_y[OF this `y < pi / 2` `y = arctan x` `tan y = x`] show ?thesis .
  next
    case False hence "y ≤ 0" by auto
    moreover have "y ≠ 0" 
    proof (rule ccontr)
      assume "¬ y ≠ 0" hence "y = 0" by auto
      have "x = 0" unfolding `tan y = x`[symmetric] `y = 0` tan_zero ..
      thus False using `x ≠ 0` by auto
    qed
    ultimately have "y < 0" by auto
    hence "0 < - y" and "-y < pi / 2" using `- (pi / 2) < y` by auto
    moreover have "-y = arctan (-x)" unfolding arctan_minus `y = arctan x` ..
    moreover have "tan (-y) = -x" unfolding tan_minus `tan y = x` ..
    ultimately have "arctan (1 / -x) = sgn (-x) * pi / 2 - arctan (-x)" using pos_y by blast
    hence "arctan (- (1 / x)) = - (sgn x * pi / 2 - arctan x)" unfolding arctan_minus[of x] divide_minus_right sgn_minus by auto
    thus ?thesis unfolding arctan_minus neg_equal_iff_equal .
  qed
qed

theorem pi_series: "pi / 4 = (∑ k. (-1)^k * 1 / real (k*2+1))" (is "_ = ?SUM")
proof -
  have "pi / 4 = arctan 1" using arctan1_eq_pi4 by auto
  also have "… = ?SUM" using arctan_series[of 1] by auto
  finally show ?thesis by auto
qed

subsection {* Existence of Polar Coordinates *}

lemma cos_x_y_le_one: "¦x / sqrt (x² + y²)¦ ≤ 1"
apply (rule power2_le_imp_le [OF _ zero_le_one])
apply (simp add: abs_divide power_divide divide_le_eq not_sum_power2_lt_zero)
done

lemma cos_arccos_abs: "¦y¦ ≤ 1 ==> cos (arccos y) = y"
by (simp add: abs_le_iff)

lemma sin_arccos_abs: "¦y¦ ≤ 1 ==> sin (arccos y) = sqrt (1 - y²)"
by (simp add: sin_arccos abs_le_iff)

lemmas cos_arccos_lemma1 = cos_arccos_abs [OF cos_x_y_le_one]

lemmas sin_arccos_lemma1 = sin_arccos_abs [OF cos_x_y_le_one]

lemma polar_ex1:
     "0 < y ==> ∃r a. x = r * cos a & y = r * sin a"
apply (rule_tac x = "sqrt (x² + y²)" in exI)
apply (rule_tac x = "arccos (x / sqrt (x² + y²))" in exI)
apply (simp add: cos_arccos_lemma1)
apply (simp add: sin_arccos_lemma1)
apply (simp add: power_divide)
apply (simp add: real_sqrt_mult [symmetric])
apply (simp add: right_diff_distrib)
done

lemma polar_ex2:
     "y < 0 ==> ∃r a. x = r * cos a & y = r * sin a"
apply (insert polar_ex1 [where x=x and y="-y"], simp, clarify)
apply (rule_tac x = r in exI)
apply (rule_tac x = "-a" in exI, simp)
done

lemma polar_Ex: "∃r a. x = r * cos a & y = r * sin a"
apply (rule_tac x=0 and y=y in linorder_cases)
apply (erule polar_ex1)
apply (rule_tac x=x in exI, rule_tac x=0 in exI, simp)
apply (erule polar_ex2)
done

end