src/HOL/Deriv.thy
author blanchet
Sun May 04 18:14:58 2014 +0200 (2014-05-04)
changeset 56846 9df717fef2bb
parent 56541 0e3abadbef39
child 57418 6ab1c7cb0b8d
permissions -rw-r--r--
renamed 'xxx_size' to 'size_xxx' for old datatype package
     1 (*  Title       : Deriv.thy
     2     Author      : Jacques D. Fleuriot
     3     Copyright   : 1998  University of Cambridge
     4     Author      : Brian Huffman
     5     Conversion to Isar and new proofs by Lawrence C Paulson, 2004
     6     GMVT by Benjamin Porter, 2005
     7 *)
     8 
     9 header{* Differentiation *}
    10 
    11 theory Deriv
    12 imports Limits
    13 begin
    14 
    15 subsection {* Frechet derivative *}
    16 
    17 definition
    18   has_derivative :: "('a::real_normed_vector \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a filter \<Rightarrow>  bool"
    19   (infix "(has'_derivative)" 50)
    20 where
    21   "(f has_derivative f') F \<longleftrightarrow>
    22     (bounded_linear f' \<and>
    23      ((\<lambda>y. ((f y - f (Lim F (\<lambda>x. x))) - f' (y - Lim F (\<lambda>x. x))) /\<^sub>R norm (y - Lim F (\<lambda>x. x))) ---> 0) F)"
    24 
    25 text {*
    26   Usually the filter @{term F} is @{term "at x within s"}.  @{term "(f has_derivative D)
    27   (at x within s)"} means: @{term D} is the derivative of function @{term f} at point @{term x}
    28   within the set @{term s}. Where @{term s} is used to express left or right sided derivatives. In
    29   most cases @{term s} is either a variable or @{term UNIV}.
    30 *}
    31 
    32 lemma has_derivative_eq_rhs: "(f has_derivative f') F \<Longrightarrow> f' = g' \<Longrightarrow> (f has_derivative g') F"
    33   by simp
    34 
    35 definition 
    36   has_field_derivative :: "('a::real_normed_field \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a filter \<Rightarrow> bool"
    37   (infix "(has'_field'_derivative)" 50)
    38 where
    39   "(f has_field_derivative D) F \<longleftrightarrow> (f has_derivative op * D) F"
    40 
    41 lemma DERIV_cong: "(f has_field_derivative X) F \<Longrightarrow> X = Y \<Longrightarrow> (f has_field_derivative Y) F"
    42   by simp
    43 
    44 definition
    45   has_vector_derivative :: "(real \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'b \<Rightarrow> real filter \<Rightarrow> bool"
    46   (infix "has'_vector'_derivative" 50)
    47 where
    48   "(f has_vector_derivative f') net \<longleftrightarrow> (f has_derivative (\<lambda>x. x *\<^sub>R f')) net"
    49 
    50 lemma has_vector_derivative_eq_rhs: "(f has_vector_derivative X) F \<Longrightarrow> X = Y \<Longrightarrow> (f has_vector_derivative Y) F"
    51   by simp
    52 
    53 ML {*
    54 
    55 structure Derivative_Intros = Named_Thms
    56 (
    57   val name = @{binding derivative_intros}
    58   val description = "structural introduction rules for derivatives"
    59 )
    60 
    61 *}
    62 
    63 setup {*
    64   let
    65     val eq_thms = [@{thm has_derivative_eq_rhs}, @{thm DERIV_cong}, @{thm has_vector_derivative_eq_rhs}]
    66     fun eq_rule thm = get_first (try (fn eq_thm => eq_thm OF [thm])) eq_thms
    67   in
    68     Derivative_Intros.setup #>
    69     Global_Theory.add_thms_dynamic
    70       (@{binding derivative_eq_intros}, map_filter eq_rule o Derivative_Intros.get o Context.proof_of)
    71   end;
    72 *}
    73 
    74 text {*
    75   The following syntax is only used as a legacy syntax.
    76 *}
    77 abbreviation (input)
    78   FDERIV :: "('a::real_normed_vector \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a \<Rightarrow>  ('a \<Rightarrow> 'b) \<Rightarrow> bool"
    79   ("(FDERIV (_)/ (_)/ :> (_))" [1000, 1000, 60] 60)
    80 where
    81   "FDERIV f x :> f' \<equiv> (f has_derivative f') (at x)"
    82 
    83 lemma has_derivative_bounded_linear: "(f has_derivative f') F \<Longrightarrow> bounded_linear f'"
    84   by (simp add: has_derivative_def)
    85 
    86 lemma has_derivative_linear: "(f has_derivative f') F \<Longrightarrow> linear f'"
    87   using bounded_linear.linear[OF has_derivative_bounded_linear] .
    88 
    89 lemma has_derivative_ident[derivative_intros, simp]: "((\<lambda>x. x) has_derivative (\<lambda>x. x)) F"
    90   by (simp add: has_derivative_def tendsto_const)
    91 
    92 lemma has_derivative_const[derivative_intros, simp]: "((\<lambda>x. c) has_derivative (\<lambda>x. 0)) F"
    93   by (simp add: has_derivative_def tendsto_const)
    94 
    95 lemma (in bounded_linear) bounded_linear: "bounded_linear f" ..
    96 
    97 lemma (in bounded_linear) has_derivative:
    98   "(g has_derivative g') F \<Longrightarrow> ((\<lambda>x. f (g x)) has_derivative (\<lambda>x. f (g' x))) F"
    99   using assms unfolding has_derivative_def
   100   apply safe
   101   apply (erule bounded_linear_compose [OF bounded_linear])
   102   apply (drule tendsto)
   103   apply (simp add: scaleR diff add zero)
   104   done
   105 
   106 lemmas has_derivative_scaleR_right [derivative_intros] =
   107   bounded_linear.has_derivative [OF bounded_linear_scaleR_right]
   108 
   109 lemmas has_derivative_scaleR_left [derivative_intros] =
   110   bounded_linear.has_derivative [OF bounded_linear_scaleR_left]
   111 
   112 lemmas has_derivative_mult_right [derivative_intros] =
   113   bounded_linear.has_derivative [OF bounded_linear_mult_right]
   114 
   115 lemmas has_derivative_mult_left [derivative_intros] =
   116   bounded_linear.has_derivative [OF bounded_linear_mult_left]
   117 
   118 lemma has_derivative_add[simp, derivative_intros]:
   119   assumes f: "(f has_derivative f') F" and g: "(g has_derivative g') F"
   120   shows "((\<lambda>x. f x + g x) has_derivative (\<lambda>x. f' x + g' x)) F"
   121   unfolding has_derivative_def
   122 proof safe
   123   let ?x = "Lim F (\<lambda>x. x)"
   124   let ?D = "\<lambda>f f' y. ((f y - f ?x) - f' (y - ?x)) /\<^sub>R norm (y - ?x)"
   125   have "((\<lambda>x. ?D f f' x + ?D g g' x) ---> (0 + 0)) F"
   126     using f g by (intro tendsto_add) (auto simp: has_derivative_def)
   127   then show "(?D (\<lambda>x. f x + g x) (\<lambda>x. f' x + g' x) ---> 0) F"
   128     by (simp add: field_simps scaleR_add_right scaleR_diff_right)
   129 qed (blast intro: bounded_linear_add f g has_derivative_bounded_linear)
   130 
   131 lemma has_derivative_setsum[simp, derivative_intros]:
   132   assumes f: "\<And>i. i \<in> I \<Longrightarrow> (f i has_derivative f' i) F"
   133   shows "((\<lambda>x. \<Sum>i\<in>I. f i x) has_derivative (\<lambda>x. \<Sum>i\<in>I. f' i x)) F"
   134 proof cases
   135   assume "finite I" from this f show ?thesis
   136     by induct (simp_all add: f)
   137 qed simp
   138 
   139 lemma has_derivative_minus[simp, derivative_intros]: "(f has_derivative f') F \<Longrightarrow> ((\<lambda>x. - f x) has_derivative (\<lambda>x. - f' x)) F"
   140   using has_derivative_scaleR_right[of f f' F "-1"] by simp
   141 
   142 lemma has_derivative_diff[simp, derivative_intros]:
   143   "(f has_derivative f') F \<Longrightarrow> (g has_derivative g') F \<Longrightarrow> ((\<lambda>x. f x - g x) has_derivative (\<lambda>x. f' x - g' x)) F"
   144   by (simp only: diff_conv_add_uminus has_derivative_add has_derivative_minus)
   145 
   146 lemma has_derivative_at_within:
   147   "(f has_derivative f') (at x within s) \<longleftrightarrow>
   148     (bounded_linear f' \<and> ((\<lambda>y. ((f y - f x) - f' (y - x)) /\<^sub>R norm (y - x)) ---> 0) (at x within s))"
   149   by (cases "at x within s = bot") (simp_all add: has_derivative_def Lim_ident_at)
   150 
   151 lemma has_derivative_iff_norm:
   152   "(f has_derivative f') (at x within s) \<longleftrightarrow>
   153     (bounded_linear f' \<and> ((\<lambda>y. norm ((f y - f x) - f' (y - x)) / norm (y - x)) ---> 0) (at x within s))"
   154   using tendsto_norm_zero_iff[of _ "at x within s", where 'b="'b", symmetric]
   155   by (simp add: has_derivative_at_within divide_inverse ac_simps)
   156 
   157 lemma has_derivative_at:
   158   "(f has_derivative D) (at x) \<longleftrightarrow> (bounded_linear D \<and> (\<lambda>h. norm (f (x + h) - f x - D h) / norm h) -- 0 --> 0)"
   159   unfolding has_derivative_iff_norm LIM_offset_zero_iff[of _ _ x] by simp
   160 
   161 lemma field_has_derivative_at:
   162   fixes x :: "'a::real_normed_field"
   163   shows "(f has_derivative op * D) (at x) \<longleftrightarrow> (\<lambda>h. (f (x + h) - f x) / h) -- 0 --> D"
   164   apply (unfold has_derivative_at)
   165   apply (simp add: bounded_linear_mult_right)
   166   apply (simp cong: LIM_cong add: nonzero_norm_divide [symmetric])
   167   apply (subst diff_divide_distrib)
   168   apply (subst times_divide_eq_left [symmetric])
   169   apply (simp cong: LIM_cong)
   170   apply (simp add: tendsto_norm_zero_iff LIM_zero_iff)
   171   done
   172 
   173 lemma has_derivativeI:
   174   "bounded_linear f' \<Longrightarrow> ((\<lambda>y. ((f y - f x) - f' (y - x)) /\<^sub>R norm (y - x)) ---> 0) (at x within s) \<Longrightarrow>
   175   (f has_derivative f') (at x within s)"
   176   by (simp add: has_derivative_at_within)
   177 
   178 lemma has_derivativeI_sandwich:
   179   assumes e: "0 < e" and bounded: "bounded_linear f'"
   180     and sandwich: "(\<And>y. y \<in> s \<Longrightarrow> y \<noteq> x \<Longrightarrow> dist y x < e \<Longrightarrow> norm ((f y - f x) - f' (y - x)) / norm (y - x) \<le> H y)"
   181     and "(H ---> 0) (at x within s)"
   182   shows "(f has_derivative f') (at x within s)"
   183   unfolding has_derivative_iff_norm
   184 proof safe
   185   show "((\<lambda>y. norm (f y - f x - f' (y - x)) / norm (y - x)) ---> 0) (at x within s)"
   186   proof (rule tendsto_sandwich[where f="\<lambda>x. 0"])
   187     show "(H ---> 0) (at x within s)" by fact
   188     show "eventually (\<lambda>n. norm (f n - f x - f' (n - x)) / norm (n - x) \<le> H n) (at x within s)"
   189       unfolding eventually_at using e sandwich by auto
   190   qed (auto simp: le_divide_eq tendsto_const)
   191 qed fact
   192 
   193 lemma has_derivative_subset: "(f has_derivative f') (at x within s) \<Longrightarrow> t \<subseteq> s \<Longrightarrow> (f has_derivative f') (at x within t)"
   194   by (auto simp add: has_derivative_iff_norm intro: tendsto_within_subset)
   195 
   196 lemmas has_derivative_within_subset = has_derivative_subset 
   197 
   198 
   199 subsection {* Continuity *}
   200 
   201 lemma has_derivative_continuous:
   202   assumes f: "(f has_derivative f') (at x within s)"
   203   shows "continuous (at x within s) f"
   204 proof -
   205   from f interpret F: bounded_linear f' by (rule has_derivative_bounded_linear)
   206   note F.tendsto[tendsto_intros]
   207   let ?L = "\<lambda>f. (f ---> 0) (at x within s)"
   208   have "?L (\<lambda>y. norm ((f y - f x) - f' (y - x)) / norm (y - x))"
   209     using f unfolding has_derivative_iff_norm by blast
   210   then have "?L (\<lambda>y. norm ((f y - f x) - f' (y - x)) / norm (y - x) * norm (y - x))" (is ?m)
   211     by (rule tendsto_mult_zero) (auto intro!: tendsto_eq_intros)
   212   also have "?m \<longleftrightarrow> ?L (\<lambda>y. norm ((f y - f x) - f' (y - x)))"
   213     by (intro filterlim_cong) (simp_all add: eventually_at_filter)
   214   finally have "?L (\<lambda>y. (f y - f x) - f' (y - x))"
   215     by (rule tendsto_norm_zero_cancel)
   216   then have "?L (\<lambda>y. ((f y - f x) - f' (y - x)) + f' (y - x))"
   217     by (rule tendsto_eq_intros) (auto intro!: tendsto_eq_intros simp: F.zero)
   218   then have "?L (\<lambda>y. f y - f x)"
   219     by simp
   220   from tendsto_add[OF this tendsto_const, of "f x"] show ?thesis
   221     by (simp add: continuous_within)
   222 qed
   223 
   224 subsection {* Composition *}
   225 
   226 lemma tendsto_at_iff_tendsto_nhds_within: "f x = y \<Longrightarrow> (f ---> y) (at x within s) \<longleftrightarrow> (f ---> y) (inf (nhds x) (principal s))"
   227   unfolding tendsto_def eventually_inf_principal eventually_at_filter
   228   by (intro ext all_cong imp_cong) (auto elim!: eventually_elim1)
   229 
   230 lemma has_derivative_in_compose:
   231   assumes f: "(f has_derivative f') (at x within s)"
   232   assumes g: "(g has_derivative g') (at (f x) within (f`s))"
   233   shows "((\<lambda>x. g (f x)) has_derivative (\<lambda>x. g' (f' x))) (at x within s)"
   234 proof -
   235   from f interpret F: bounded_linear f' by (rule has_derivative_bounded_linear)
   236   from g interpret G: bounded_linear g' by (rule has_derivative_bounded_linear)
   237   from F.bounded obtain kF where kF: "\<And>x. norm (f' x) \<le> norm x * kF" by fast
   238   from G.bounded obtain kG where kG: "\<And>x. norm (g' x) \<le> norm x * kG" by fast
   239   note G.tendsto[tendsto_intros]
   240 
   241   let ?L = "\<lambda>f. (f ---> 0) (at x within s)"
   242   let ?D = "\<lambda>f f' x y. (f y - f x) - f' (y - x)"
   243   let ?N = "\<lambda>f f' x y. norm (?D f f' x y) / norm (y - x)"
   244   let ?gf = "\<lambda>x. g (f x)" and ?gf' = "\<lambda>x. g' (f' x)"
   245   def Nf \<equiv> "?N f f' x"
   246   def Ng \<equiv> "\<lambda>y. ?N g g' (f x) (f y)"
   247 
   248   show ?thesis
   249   proof (rule has_derivativeI_sandwich[of 1])
   250     show "bounded_linear (\<lambda>x. g' (f' x))"
   251       using f g by (blast intro: bounded_linear_compose has_derivative_bounded_linear)
   252   next
   253     fix y::'a assume neq: "y \<noteq> x"
   254     have "?N ?gf ?gf' x y = norm (g' (?D f f' x y) + ?D g g' (f x) (f y)) / norm (y - x)"
   255       by (simp add: G.diff G.add field_simps)
   256     also have "\<dots> \<le> norm (g' (?D f f' x y)) / norm (y - x) + Ng y * (norm (f y - f x) / norm (y - x))"
   257       by (simp add: add_divide_distrib[symmetric] divide_right_mono norm_triangle_ineq G.zero Ng_def)
   258     also have "\<dots> \<le> Nf y * kG + Ng y * (Nf y + kF)"
   259     proof (intro add_mono mult_left_mono)
   260       have "norm (f y - f x) = norm (?D f f' x y + f' (y - x))"
   261         by simp
   262       also have "\<dots> \<le> norm (?D f f' x y) + norm (f' (y - x))"
   263         by (rule norm_triangle_ineq)
   264       also have "\<dots> \<le> norm (?D f f' x y) + norm (y - x) * kF"
   265         using kF by (intro add_mono) simp
   266       finally show "norm (f y - f x) / norm (y - x) \<le> Nf y + kF"
   267         by (simp add: neq Nf_def field_simps)
   268     qed (insert kG, simp_all add: Ng_def Nf_def neq zero_le_divide_iff field_simps)
   269     finally show "?N ?gf ?gf' x y \<le> Nf y * kG + Ng y * (Nf y + kF)" .
   270   next
   271     have [tendsto_intros]: "?L Nf"
   272       using f unfolding has_derivative_iff_norm Nf_def ..
   273     from f have "(f ---> f x) (at x within s)"
   274       by (blast intro: has_derivative_continuous continuous_within[THEN iffD1])
   275     then have f': "LIM x at x within s. f x :> inf (nhds (f x)) (principal (f`s))"
   276       unfolding filterlim_def
   277       by (simp add: eventually_filtermap eventually_at_filter le_principal)
   278 
   279     have "((?N g  g' (f x)) ---> 0) (at (f x) within f`s)"
   280       using g unfolding has_derivative_iff_norm ..
   281     then have g': "((?N g  g' (f x)) ---> 0) (inf (nhds (f x)) (principal (f`s)))"
   282       by (rule tendsto_at_iff_tendsto_nhds_within[THEN iffD1, rotated]) simp
   283 
   284     have [tendsto_intros]: "?L Ng"
   285       unfolding Ng_def by (rule filterlim_compose[OF g' f'])
   286     show "((\<lambda>y. Nf y * kG + Ng y * (Nf y + kF)) ---> 0) (at x within s)"
   287       by (intro tendsto_eq_intros) auto
   288   qed simp
   289 qed
   290 
   291 lemma has_derivative_compose:
   292   "(f has_derivative f') (at x within s) \<Longrightarrow> (g has_derivative g') (at (f x)) \<Longrightarrow>
   293   ((\<lambda>x. g (f x)) has_derivative (\<lambda>x. g' (f' x))) (at x within s)"
   294   by (blast intro: has_derivative_in_compose has_derivative_subset)
   295 
   296 lemma (in bounded_bilinear) FDERIV:
   297   assumes f: "(f has_derivative f') (at x within s)" and g: "(g has_derivative g') (at x within s)"
   298   shows "((\<lambda>x. f x ** g x) has_derivative (\<lambda>h. f x ** g' h + f' h ** g x)) (at x within s)"
   299 proof -
   300   from bounded_linear.bounded [OF has_derivative_bounded_linear [OF f]]
   301   obtain KF where norm_F: "\<And>x. norm (f' x) \<le> norm x * KF" by fast
   302 
   303   from pos_bounded obtain K where K: "0 < K" and norm_prod:
   304     "\<And>a b. norm (a ** b) \<le> norm a * norm b * K" by fast
   305   let ?D = "\<lambda>f f' y. f y - f x - f' (y - x)"
   306   let ?N = "\<lambda>f f' y. norm (?D f f' y) / norm (y - x)"
   307   def Ng =="?N g g'" and Nf =="?N f f'"
   308 
   309   let ?fun1 = "\<lambda>y. norm (f y ** g y - f x ** g x - (f x ** g' (y - x) + f' (y - x) ** g x)) / norm (y - x)"
   310   let ?fun2 = "\<lambda>y. norm (f x) * Ng y * K + Nf y * norm (g y) * K + KF * norm (g y - g x) * K"
   311   let ?F = "at x within s"
   312 
   313   show ?thesis
   314   proof (rule has_derivativeI_sandwich[of 1])
   315     show "bounded_linear (\<lambda>h. f x ** g' h + f' h ** g x)"
   316       by (intro bounded_linear_add
   317         bounded_linear_compose [OF bounded_linear_right] bounded_linear_compose [OF bounded_linear_left]
   318         has_derivative_bounded_linear [OF g] has_derivative_bounded_linear [OF f])
   319   next
   320     from g have "(g ---> g x) ?F"
   321       by (intro continuous_within[THEN iffD1] has_derivative_continuous)
   322     moreover from f g have "(Nf ---> 0) ?F" "(Ng ---> 0) ?F"
   323       by (simp_all add: has_derivative_iff_norm Ng_def Nf_def)
   324     ultimately have "(?fun2 ---> norm (f x) * 0 * K + 0 * norm (g x) * K + KF * norm (0::'b) * K) ?F"
   325       by (intro tendsto_intros) (simp_all add: LIM_zero_iff)
   326     then show "(?fun2 ---> 0) ?F"
   327       by simp
   328   next
   329     fix y::'d assume "y \<noteq> x"
   330     have "?fun1 y = norm (f x ** ?D g g' y + ?D f f' y ** g y + f' (y - x) ** (g y - g x)) / norm (y - x)"
   331       by (simp add: diff_left diff_right add_left add_right field_simps)
   332     also have "\<dots> \<le> (norm (f x) * norm (?D g g' y) * K + norm (?D f f' y) * norm (g y) * K +
   333         norm (y - x) * KF * norm (g y - g x) * K) / norm (y - x)"
   334       by (intro divide_right_mono mult_mono'
   335                 order_trans [OF norm_triangle_ineq add_mono]
   336                 order_trans [OF norm_prod mult_right_mono]
   337                 mult_nonneg_nonneg order_refl norm_ge_zero norm_F
   338                 K [THEN order_less_imp_le])
   339     also have "\<dots> = ?fun2 y"
   340       by (simp add: add_divide_distrib Ng_def Nf_def)
   341     finally show "?fun1 y \<le> ?fun2 y" .
   342   qed simp
   343 qed
   344 
   345 lemmas has_derivative_mult[simp, derivative_intros] = bounded_bilinear.FDERIV[OF bounded_bilinear_mult]
   346 lemmas has_derivative_scaleR[simp, derivative_intros] = bounded_bilinear.FDERIV[OF bounded_bilinear_scaleR]
   347 
   348 lemma has_derivative_setprod[simp, derivative_intros]:
   349   fixes f :: "'i \<Rightarrow> 'a :: real_normed_vector \<Rightarrow> 'b :: real_normed_field"
   350   assumes f: "\<And>i. i \<in> I \<Longrightarrow> (f i has_derivative f' i) (at x within s)"
   351   shows "((\<lambda>x. \<Prod>i\<in>I. f i x) has_derivative (\<lambda>y. \<Sum>i\<in>I. f' i y * (\<Prod>j\<in>I - {i}. f j x))) (at x within s)"
   352 proof cases
   353   assume "finite I" from this f show ?thesis
   354   proof induct
   355     case (insert i I)
   356     let ?P = "\<lambda>y. f i x * (\<Sum>i\<in>I. f' i y * (\<Prod>j\<in>I - {i}. f j x)) + (f' i y) * (\<Prod>i\<in>I. f i x)"
   357     have "((\<lambda>x. f i x * (\<Prod>i\<in>I. f i x)) has_derivative ?P) (at x within s)"
   358       using insert by (intro has_derivative_mult) auto
   359     also have "?P = (\<lambda>y. \<Sum>i'\<in>insert i I. f' i' y * (\<Prod>j\<in>insert i I - {i'}. f j x))"
   360       using insert(1,2) by (auto simp add: setsum_right_distrib insert_Diff_if intro!: ext setsum_cong)
   361     finally show ?case
   362       using insert by simp
   363   qed simp  
   364 qed simp
   365 
   366 lemma has_derivative_power[simp, derivative_intros]:
   367   fixes f :: "'a :: real_normed_vector \<Rightarrow> 'b :: real_normed_field"
   368   assumes f: "(f has_derivative f') (at x within s)"
   369   shows "((\<lambda>x. f x^n) has_derivative (\<lambda>y. of_nat n * f' y * f x^(n - 1))) (at x within s)"
   370   using has_derivative_setprod[OF f, of "{..< n}"] by (simp add: setprod_constant ac_simps)
   371 
   372 lemma has_derivative_inverse':
   373   fixes x :: "'a::real_normed_div_algebra"
   374   assumes x: "x \<noteq> 0"
   375   shows "(inverse has_derivative (\<lambda>h. - (inverse x * h * inverse x))) (at x within s)"
   376         (is "(?inv has_derivative ?f) _")
   377 proof (rule has_derivativeI_sandwich)
   378   show "bounded_linear (\<lambda>h. - (?inv x * h * ?inv x))"
   379     apply (rule bounded_linear_minus)
   380     apply (rule bounded_linear_mult_const)
   381     apply (rule bounded_linear_const_mult)
   382     apply (rule bounded_linear_ident)
   383     done
   384 next
   385   show "0 < norm x" using x by simp
   386 next
   387   show "((\<lambda>y. norm (?inv y - ?inv x) * norm (?inv x)) ---> 0) (at x within s)"
   388     apply (rule tendsto_mult_left_zero)
   389     apply (rule tendsto_norm_zero)
   390     apply (rule LIM_zero)
   391     apply (rule tendsto_inverse)
   392     apply (rule tendsto_ident_at)
   393     apply (rule x)
   394     done
   395 next
   396   fix y::'a assume h: "y \<noteq> x" "dist y x < norm x"
   397   then have "y \<noteq> 0"
   398     by (auto simp: norm_conv_dist dist_commute)
   399   have "norm (?inv y - ?inv x - ?f (y -x)) / norm (y - x) = norm ((?inv y - ?inv x) * (y - x) * ?inv x) / norm (y - x)"
   400     apply (subst inverse_diff_inverse [OF `y \<noteq> 0` x])
   401     apply (subst minus_diff_minus)
   402     apply (subst norm_minus_cancel)
   403     apply (simp add: left_diff_distrib)
   404     done
   405   also have "\<dots> \<le> norm (?inv y - ?inv x) * norm (y - x) * norm (?inv x) / norm (y - x)"
   406     apply (rule divide_right_mono [OF _ norm_ge_zero])
   407     apply (rule order_trans [OF norm_mult_ineq])
   408     apply (rule mult_right_mono [OF _ norm_ge_zero])
   409     apply (rule norm_mult_ineq)
   410     done
   411   also have "\<dots> = norm (?inv y - ?inv x) * norm (?inv x)"
   412     by simp
   413   finally show "norm (?inv y - ?inv x - ?f (y -x)) / norm (y - x) \<le>
   414       norm (?inv y - ?inv x) * norm (?inv x)" .
   415 qed
   416 
   417 lemma has_derivative_inverse[simp, derivative_intros]:
   418   fixes f :: "_ \<Rightarrow> 'a::real_normed_div_algebra"
   419   assumes x:  "f x \<noteq> 0" and f: "(f has_derivative f') (at x within s)"
   420   shows "((\<lambda>x. inverse (f x)) has_derivative (\<lambda>h. - (inverse (f x) * f' h * inverse (f x)))) (at x within s)"
   421   using has_derivative_compose[OF f has_derivative_inverse', OF x] .
   422 
   423 lemma has_derivative_divide[simp, derivative_intros]:
   424   fixes f :: "_ \<Rightarrow> 'a::real_normed_div_algebra"
   425   assumes f: "(f has_derivative f') (at x within s)" and g: "(g has_derivative g') (at x within s)" 
   426   assumes x: "g x \<noteq> 0"
   427   shows "((\<lambda>x. f x / g x) has_derivative
   428                 (\<lambda>h. - f x * (inverse (g x) * g' h * inverse (g x)) + f' h / g x)) (at x within s)"
   429   using has_derivative_mult[OF f has_derivative_inverse[OF x g]]
   430   by (simp add: field_simps)
   431 
   432 text{*Conventional form requires mult-AC laws. Types real and complex only.*}
   433 
   434 lemma has_derivative_divide'[derivative_intros]: 
   435   fixes f :: "_ \<Rightarrow> 'a::real_normed_field"
   436   assumes f: "(f has_derivative f') (at x within s)" and g: "(g has_derivative g') (at x within s)" and x: "g x \<noteq> 0"
   437   shows "((\<lambda>x. f x / g x) has_derivative (\<lambda>h. (f' h * g x - f x * g' h) / (g x * g x))) (at x within s)"
   438 proof -
   439   { fix h
   440     have "f' h / g x - f x * (inverse (g x) * g' h * inverse (g x)) =
   441           (f' h * g x - f x * g' h) / (g x * g x)"
   442       by (simp add: field_simps x)
   443    }
   444   then show ?thesis
   445     using has_derivative_divide [OF f g] x
   446     by simp
   447 qed
   448 
   449 subsection {* Uniqueness *}
   450 
   451 text {*
   452 
   453 This can not generally shown for @{const has_derivative}, as we need to approach the point from
   454 all directions. There is a proof in @{text Multivariate_Analysis} for @{text euclidean_space}.
   455 
   456 *}
   457 
   458 lemma has_derivative_zero_unique:
   459   assumes "((\<lambda>x. 0) has_derivative F) (at x)" shows "F = (\<lambda>h. 0)"
   460 proof -
   461   interpret F: bounded_linear F
   462     using assms by (rule has_derivative_bounded_linear)
   463   let ?r = "\<lambda>h. norm (F h) / norm h"
   464   have *: "?r -- 0 --> 0"
   465     using assms unfolding has_derivative_at by simp
   466   show "F = (\<lambda>h. 0)"
   467   proof
   468     fix h show "F h = 0"
   469     proof (rule ccontr)
   470       assume **: "F h \<noteq> 0"
   471       hence h: "h \<noteq> 0" by (clarsimp simp add: F.zero)
   472       with ** have "0 < ?r h" by simp
   473       from LIM_D [OF * this] obtain s where s: "0 < s"
   474         and r: "\<And>x. x \<noteq> 0 \<Longrightarrow> norm x < s \<Longrightarrow> ?r x < ?r h" by auto
   475       from dense [OF s] obtain t where t: "0 < t \<and> t < s" ..
   476       let ?x = "scaleR (t / norm h) h"
   477       have "?x \<noteq> 0" and "norm ?x < s" using t h by simp_all
   478       hence "?r ?x < ?r h" by (rule r)
   479       thus "False" using t h by (simp add: F.scaleR)
   480     qed
   481   qed
   482 qed
   483 
   484 lemma has_derivative_unique:
   485   assumes "(f has_derivative F) (at x)" and "(f has_derivative F') (at x)" shows "F = F'"
   486 proof -
   487   have "((\<lambda>x. 0) has_derivative (\<lambda>h. F h - F' h)) (at x)"
   488     using has_derivative_diff [OF assms] by simp
   489   hence "(\<lambda>h. F h - F' h) = (\<lambda>h. 0)"
   490     by (rule has_derivative_zero_unique)
   491   thus "F = F'"
   492     unfolding fun_eq_iff right_minus_eq .
   493 qed
   494 
   495 subsection {* Differentiability predicate *}
   496 
   497 definition
   498   differentiable :: "('a::real_normed_vector \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   499   (infix "differentiable" 50)
   500 where
   501   "f differentiable F \<longleftrightarrow> (\<exists>D. (f has_derivative D) F)"
   502 
   503 lemma differentiable_subset: "f differentiable (at x within s) \<Longrightarrow> t \<subseteq> s \<Longrightarrow> f differentiable (at x within t)"
   504   unfolding differentiable_def by (blast intro: has_derivative_subset)
   505 
   506 lemmas differentiable_within_subset = differentiable_subset
   507 
   508 lemma differentiable_ident [simp, derivative_intros]: "(\<lambda>x. x) differentiable F"
   509   unfolding differentiable_def by (blast intro: has_derivative_ident)
   510 
   511 lemma differentiable_const [simp, derivative_intros]: "(\<lambda>z. a) differentiable F"
   512   unfolding differentiable_def by (blast intro: has_derivative_const)
   513 
   514 lemma differentiable_in_compose:
   515   "f differentiable (at (g x) within (g`s)) \<Longrightarrow> g differentiable (at x within s) \<Longrightarrow> (\<lambda>x. f (g x)) differentiable (at x within s)"
   516   unfolding differentiable_def by (blast intro: has_derivative_in_compose)
   517 
   518 lemma differentiable_compose:
   519   "f differentiable (at (g x)) \<Longrightarrow> g differentiable (at x within s) \<Longrightarrow> (\<lambda>x. f (g x)) differentiable (at x within s)"
   520   by (blast intro: differentiable_in_compose differentiable_subset)
   521 
   522 lemma differentiable_sum [simp, derivative_intros]:
   523   "f differentiable F \<Longrightarrow> g differentiable F \<Longrightarrow> (\<lambda>x. f x + g x) differentiable F"
   524   unfolding differentiable_def by (blast intro: has_derivative_add)
   525 
   526 lemma differentiable_minus [simp, derivative_intros]:
   527   "f differentiable F \<Longrightarrow> (\<lambda>x. - f x) differentiable F"
   528   unfolding differentiable_def by (blast intro: has_derivative_minus)
   529 
   530 lemma differentiable_diff [simp, derivative_intros]:
   531   "f differentiable F \<Longrightarrow> g differentiable F \<Longrightarrow> (\<lambda>x. f x - g x) differentiable F"
   532   unfolding differentiable_def by (blast intro: has_derivative_diff)
   533 
   534 lemma differentiable_mult [simp, derivative_intros]:
   535   fixes f g :: "'a :: real_normed_vector \<Rightarrow> 'b :: real_normed_algebra"
   536   shows "f differentiable (at x within s) \<Longrightarrow> g differentiable (at x within s) \<Longrightarrow> (\<lambda>x. f x * g x) differentiable (at x within s)"
   537   unfolding differentiable_def by (blast intro: has_derivative_mult)
   538 
   539 lemma differentiable_inverse [simp, derivative_intros]:
   540   fixes f :: "'a :: real_normed_vector \<Rightarrow> 'b :: real_normed_field"
   541   shows "f differentiable (at x within s) \<Longrightarrow> f x \<noteq> 0 \<Longrightarrow> (\<lambda>x. inverse (f x)) differentiable (at x within s)"
   542   unfolding differentiable_def by (blast intro: has_derivative_inverse)
   543 
   544 lemma differentiable_divide [simp, derivative_intros]:
   545   fixes f g :: "'a :: real_normed_vector \<Rightarrow> 'b :: real_normed_field"
   546   shows "f differentiable (at x within s) \<Longrightarrow> g differentiable (at x within s) \<Longrightarrow> g x \<noteq> 0 \<Longrightarrow> (\<lambda>x. f x / g x) differentiable (at x within s)"
   547   unfolding divide_inverse using assms by simp
   548 
   549 lemma differentiable_power [simp, derivative_intros]:
   550   fixes f g :: "'a :: real_normed_vector \<Rightarrow> 'b :: real_normed_field"
   551   shows "f differentiable (at x within s) \<Longrightarrow> (\<lambda>x. f x ^ n) differentiable (at x within s)"
   552   unfolding differentiable_def by (blast intro: has_derivative_power)
   553 
   554 lemma differentiable_scaleR [simp, derivative_intros]:
   555   "f differentiable (at x within s) \<Longrightarrow> g differentiable (at x within s) \<Longrightarrow> (\<lambda>x. f x *\<^sub>R g x) differentiable (at x within s)"
   556   unfolding differentiable_def by (blast intro: has_derivative_scaleR)
   557 
   558 lemma has_derivative_imp_has_field_derivative:
   559   "(f has_derivative D) F \<Longrightarrow> (\<And>x. x * D' = D x) \<Longrightarrow> (f has_field_derivative D') F"
   560   unfolding has_field_derivative_def 
   561   by (rule has_derivative_eq_rhs[of f D]) (simp_all add: fun_eq_iff mult_commute)
   562 
   563 lemma has_field_derivative_imp_has_derivative: "(f has_field_derivative D) F \<Longrightarrow> (f has_derivative op * D) F"
   564   by (simp add: has_field_derivative_def)
   565 
   566 lemma DERIV_subset: 
   567   "(f has_field_derivative f') (at x within s) \<Longrightarrow> t \<subseteq> s 
   568    \<Longrightarrow> (f has_field_derivative f') (at x within t)"
   569   by (simp add: has_field_derivative_def has_derivative_within_subset)
   570 
   571 abbreviation (input)
   572   DERIV :: "('a::real_normed_field \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
   573   ("(DERIV (_)/ (_)/ :> (_))" [1000, 1000, 60] 60)
   574 where
   575   "DERIV f x :> D \<equiv> (f has_field_derivative D) (at x)"
   576 
   577 abbreviation 
   578   has_real_derivative :: "(real \<Rightarrow> real) \<Rightarrow> real \<Rightarrow> real filter \<Rightarrow> bool"
   579   (infix "(has'_real'_derivative)" 50)
   580 where
   581   "(f has_real_derivative D) F \<equiv> (f has_field_derivative D) F"
   582 
   583 lemma real_differentiable_def:
   584   "f differentiable at x within s \<longleftrightarrow> (\<exists>D. (f has_real_derivative D) (at x within s))"
   585 proof safe
   586   assume "f differentiable at x within s"
   587   then obtain f' where *: "(f has_derivative f') (at x within s)"
   588     unfolding differentiable_def by auto
   589   then obtain c where "f' = (op * c)"
   590     by (metis real_bounded_linear has_derivative_bounded_linear mult_commute fun_eq_iff)
   591   with * show "\<exists>D. (f has_real_derivative D) (at x within s)"
   592     unfolding has_field_derivative_def by auto
   593 qed (auto simp: differentiable_def has_field_derivative_def)
   594 
   595 lemma real_differentiableE [elim?]:
   596   assumes f: "f differentiable (at x within s)" obtains df where "(f has_real_derivative df) (at x within s)"
   597   using assms by (auto simp: real_differentiable_def)
   598 
   599 lemma differentiableD: "f differentiable (at x within s) \<Longrightarrow> \<exists>D. (f has_real_derivative D) (at x within s)"
   600   by (auto elim: real_differentiableE)
   601 
   602 lemma differentiableI: "(f has_real_derivative D) (at x within s) \<Longrightarrow> f differentiable (at x within s)"
   603   by (force simp add: real_differentiable_def)
   604 
   605 lemma DERIV_def: "DERIV f x :> D \<longleftrightarrow> (\<lambda>h. (f (x + h) - f x) / h) -- 0 --> D"
   606   apply (simp add: has_field_derivative_def has_derivative_at bounded_linear_mult_right LIM_zero_iff[symmetric, of _ D])
   607   apply (subst (2) tendsto_norm_zero_iff[symmetric])
   608   apply (rule filterlim_cong)
   609   apply (simp_all add: eventually_at_filter field_simps nonzero_norm_divide)
   610   done
   611 
   612 lemma mult_commute_abs: "(\<lambda>x. x * c) = op * (c::'a::ab_semigroup_mult)"
   613   by (simp add: fun_eq_iff mult_commute)
   614 
   615 subsection {* Derivatives *}
   616 
   617 lemma DERIV_D: "DERIV f x :> D \<Longrightarrow> (\<lambda>h. (f (x + h) - f x) / h) -- 0 --> D"
   618   by (simp add: DERIV_def)
   619 
   620 lemma DERIV_const [simp, derivative_intros]: "((\<lambda>x. k) has_field_derivative 0) F"
   621   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_const]) auto
   622 
   623 lemma DERIV_ident [simp, derivative_intros]: "((\<lambda>x. x) has_field_derivative 1) F"
   624   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_ident]) auto
   625 
   626 lemma field_differentiable_add[derivative_intros]:
   627   "(f has_field_derivative f') F \<Longrightarrow> (g has_field_derivative g') F \<Longrightarrow> 
   628     ((\<lambda>z. f z + g z) has_field_derivative f' + g') F"
   629   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_add])
   630      (auto simp: has_field_derivative_def field_simps mult_commute_abs)
   631 
   632 corollary DERIV_add:
   633   "(f has_field_derivative D) (at x within s) \<Longrightarrow> (g has_field_derivative E) (at x within s) \<Longrightarrow>
   634   ((\<lambda>x. f x + g x) has_field_derivative D + E) (at x within s)"
   635   by (rule field_differentiable_add)
   636 
   637 lemma field_differentiable_minus[derivative_intros]:
   638   "(f has_field_derivative f') F \<Longrightarrow> ((\<lambda>z. - (f z)) has_field_derivative -f') F"
   639   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_minus])
   640      (auto simp: has_field_derivative_def field_simps mult_commute_abs)
   641 
   642 corollary DERIV_minus: "(f has_field_derivative D) (at x within s) \<Longrightarrow> ((\<lambda>x. - f x) has_field_derivative -D) (at x within s)"
   643   by (rule field_differentiable_minus)
   644 
   645 lemma field_differentiable_diff[derivative_intros]:
   646   "(f has_field_derivative f') F \<Longrightarrow> (g has_field_derivative g') F \<Longrightarrow> ((\<lambda>z. f z - g z) has_field_derivative f' - g') F"
   647   by (simp only: assms diff_conv_add_uminus field_differentiable_add field_differentiable_minus)
   648 
   649 corollary DERIV_diff:
   650   "(f has_field_derivative D) (at x within s) \<Longrightarrow> (g has_field_derivative E) (at x within s) \<Longrightarrow>
   651   ((\<lambda>x. f x - g x) has_field_derivative D - E) (at x within s)"
   652   by (rule field_differentiable_diff)
   653 
   654 lemma DERIV_continuous: "(f has_field_derivative D) (at x within s) \<Longrightarrow> continuous (at x within s) f"
   655   by (drule has_derivative_continuous[OF has_field_derivative_imp_has_derivative]) simp
   656 
   657 corollary DERIV_isCont: "DERIV f x :> D \<Longrightarrow> isCont f x"
   658   by (rule DERIV_continuous)
   659 
   660 lemma DERIV_continuous_on:
   661   "(\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative D) (at x)) \<Longrightarrow> continuous_on s f"
   662   by (metis DERIV_continuous continuous_at_imp_continuous_on)
   663 
   664 lemma DERIV_mult':
   665   "(f has_field_derivative D) (at x within s) \<Longrightarrow> (g has_field_derivative E) (at x within s) \<Longrightarrow>
   666   ((\<lambda>x. f x * g x) has_field_derivative f x * E + D * g x) (at x within s)"
   667   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_mult])
   668      (auto simp: field_simps mult_commute_abs dest: has_field_derivative_imp_has_derivative)
   669 
   670 lemma DERIV_mult[derivative_intros]:
   671   "(f has_field_derivative Da) (at x within s) \<Longrightarrow> (g has_field_derivative Db) (at x within s) \<Longrightarrow>
   672   ((\<lambda>x. f x * g x) has_field_derivative Da * g x + Db * f x) (at x within s)"
   673   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_mult])
   674      (auto simp: field_simps dest: has_field_derivative_imp_has_derivative)
   675 
   676 text {* Derivative of linear multiplication *}
   677 
   678 lemma DERIV_cmult:
   679   "(f has_field_derivative D) (at x within s) ==> ((\<lambda>x. c * f x) has_field_derivative c * D) (at x within s)"
   680   by (drule DERIV_mult' [OF DERIV_const], simp)
   681 
   682 lemma DERIV_cmult_right:
   683   "(f has_field_derivative D) (at x within s) ==> ((\<lambda>x. f x * c) has_field_derivative D * c) (at x within s)"
   684   using DERIV_cmult by (force simp add: mult_ac)
   685 
   686 lemma DERIV_cmult_Id [simp]: "(op * c has_field_derivative c) (at x within s)"
   687   by (cut_tac c = c and x = x in DERIV_ident [THEN DERIV_cmult], simp)
   688 
   689 lemma DERIV_cdivide:
   690   "(f has_field_derivative D) (at x within s) \<Longrightarrow> ((\<lambda>x. f x / c) has_field_derivative D / c) (at x within s)"
   691   using DERIV_cmult_right[of f D x s "1 / c"] by simp
   692 
   693 lemma DERIV_unique:
   694   "DERIV f x :> D \<Longrightarrow> DERIV f x :> E \<Longrightarrow> D = E"
   695   unfolding DERIV_def by (rule LIM_unique) 
   696 
   697 lemma DERIV_setsum[derivative_intros]:
   698   "(\<And> n. n \<in> S \<Longrightarrow> ((\<lambda>x. f x n) has_field_derivative (f' x n)) F) \<Longrightarrow> 
   699     ((\<lambda>x. setsum (f x) S) has_field_derivative setsum (f' x) S) F"
   700   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_setsum])
   701      (auto simp: setsum_right_distrib mult_commute_abs dest: has_field_derivative_imp_has_derivative)
   702 
   703 lemma DERIV_inverse'[derivative_intros]:
   704   "(f has_field_derivative D) (at x within s) \<Longrightarrow> f x \<noteq> 0 \<Longrightarrow>
   705   ((\<lambda>x. inverse (f x)) has_field_derivative - (inverse (f x) * D * inverse (f x))) (at x within s)"
   706   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_inverse])
   707      (auto dest: has_field_derivative_imp_has_derivative)
   708 
   709 text {* Power of @{text "-1"} *}
   710 
   711 lemma DERIV_inverse:
   712   "x \<noteq> 0 \<Longrightarrow> ((\<lambda>x. inverse(x)) has_field_derivative - (inverse x ^ Suc (Suc 0))) (at x within s)"
   713   by (drule DERIV_inverse' [OF DERIV_ident]) simp
   714 
   715 text {* Derivative of inverse *}
   716 
   717 lemma DERIV_inverse_fun:
   718   "(f has_field_derivative d) (at x within s) \<Longrightarrow> f x \<noteq> 0 \<Longrightarrow>
   719   ((\<lambda>x. inverse (f x)) has_field_derivative (- (d * inverse(f x ^ Suc (Suc 0))))) (at x within s)"
   720   by (drule (1) DERIV_inverse') (simp add: mult_ac nonzero_inverse_mult_distrib)
   721 
   722 text {* Derivative of quotient *}
   723 
   724 lemma DERIV_divide[derivative_intros]:
   725   "(f has_field_derivative D) (at x within s) \<Longrightarrow>
   726   (g has_field_derivative E) (at x within s) \<Longrightarrow> g x \<noteq> 0 \<Longrightarrow>
   727   ((\<lambda>x. f x / g x) has_field_derivative (D * g x - f x * E) / (g x * g x)) (at x within s)"
   728   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_divide])
   729      (auto dest: has_field_derivative_imp_has_derivative simp: field_simps)
   730 
   731 lemma DERIV_quotient:
   732   "(f has_field_derivative d) (at x within s) \<Longrightarrow>
   733   (g has_field_derivative e) (at x within s)\<Longrightarrow> g x \<noteq> 0 \<Longrightarrow> 
   734   ((\<lambda>y. f y / g y) has_field_derivative (d * g x - (e * f x)) / (g x ^ Suc (Suc 0))) (at x within s)"
   735   by (drule (2) DERIV_divide) (simp add: mult_commute)
   736 
   737 lemma DERIV_power_Suc:
   738   "(f has_field_derivative D) (at x within s) \<Longrightarrow>
   739   ((\<lambda>x. f x ^ Suc n) has_field_derivative (1 + of_nat n) * (D * f x ^ n)) (at x within s)"
   740   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_power])
   741      (auto simp: has_field_derivative_def)
   742 
   743 lemma DERIV_power[derivative_intros]:
   744   "(f has_field_derivative D) (at x within s) \<Longrightarrow>
   745   ((\<lambda>x. f x ^ n) has_field_derivative of_nat n * (D * f x ^ (n - Suc 0))) (at x within s)"
   746   by (rule has_derivative_imp_has_field_derivative[OF has_derivative_power])
   747      (auto simp: has_field_derivative_def)
   748 
   749 lemma DERIV_pow: "((\<lambda>x. x ^ n) has_field_derivative real n * (x ^ (n - Suc 0))) (at x within s)"
   750   apply (cut_tac DERIV_power [OF DERIV_ident])
   751   apply (simp add: real_of_nat_def)
   752   done
   753 
   754 lemma DERIV_chain': "(f has_field_derivative D) (at x within s) \<Longrightarrow> DERIV g (f x) :> E \<Longrightarrow> 
   755   ((\<lambda>x. g (f x)) has_field_derivative E * D) (at x within s)"
   756   using has_derivative_compose[of f "op * D" x s g "op * E"]
   757   unfolding has_field_derivative_def mult_commute_abs ac_simps .
   758 
   759 corollary DERIV_chain2: "DERIV f (g x) :> Da \<Longrightarrow> (g has_field_derivative Db) (at x within s) \<Longrightarrow>
   760   ((\<lambda>x. f (g x)) has_field_derivative Da * Db) (at x within s)"
   761   by (rule DERIV_chain')
   762 
   763 text {* Standard version *}
   764 
   765 lemma DERIV_chain:
   766   "DERIV f (g x) :> Da \<Longrightarrow> (g has_field_derivative Db) (at x within s) \<Longrightarrow> 
   767   (f o g has_field_derivative Da * Db) (at x within s)"
   768   by (drule (1) DERIV_chain', simp add: o_def mult_commute)
   769 
   770 lemma DERIV_image_chain: 
   771   "(f has_field_derivative Da) (at (g x) within (g ` s)) \<Longrightarrow> (g has_field_derivative Db) (at x within s) \<Longrightarrow>
   772   (f o g has_field_derivative Da * Db) (at x within s)"
   773   using has_derivative_in_compose [of g "op * Db" x s f "op * Da "]
   774   by (simp add: has_field_derivative_def o_def mult_commute_abs ac_simps)
   775 
   776 (*These two are from HOL Light: HAS_COMPLEX_DERIVATIVE_CHAIN*)
   777 lemma DERIV_chain_s:
   778   assumes "(\<And>x. x \<in> s \<Longrightarrow> DERIV g x :> g'(x))"
   779       and "DERIV f x :> f'" 
   780       and "f x \<in> s"
   781     shows "DERIV (\<lambda>x. g(f x)) x :> f' * g'(f x)"
   782   by (metis (full_types) DERIV_chain' mult_commute assms)
   783 
   784 lemma DERIV_chain3: (*HAS_COMPLEX_DERIVATIVE_CHAIN_UNIV*)
   785   assumes "(\<And>x. DERIV g x :> g'(x))"
   786       and "DERIV f x :> f'" 
   787     shows "DERIV (\<lambda>x. g(f x)) x :> f' * g'(f x)"
   788   by (metis UNIV_I DERIV_chain_s [of UNIV] assms)
   789 
   790 declare
   791   DERIV_power[where 'a=real, unfolded real_of_nat_def[symmetric], derivative_intros]
   792 
   793 text{*Alternative definition for differentiability*}
   794 
   795 lemma DERIV_LIM_iff:
   796   fixes f :: "'a::{real_normed_vector,inverse} \<Rightarrow> 'a" shows
   797      "((%h. (f(a + h) - f(a)) / h) -- 0 --> D) =
   798       ((%x. (f(x)-f(a)) / (x-a)) -- a --> D)"
   799 apply (rule iffI)
   800 apply (drule_tac k="- a" in LIM_offset)
   801 apply simp
   802 apply (drule_tac k="a" in LIM_offset)
   803 apply (simp add: add_commute)
   804 done
   805 
   806 lemma DERIV_iff2: "(DERIV f x :> D) \<longleftrightarrow> (\<lambda>z. (f z - f x) / (z - x)) --x --> D"
   807   by (simp add: DERIV_def DERIV_LIM_iff)
   808 
   809 lemma DERIV_cong_ev: "x = y \<Longrightarrow> eventually (\<lambda>x. f x = g x) (nhds x) \<Longrightarrow> u = v \<Longrightarrow>
   810     DERIV f x :> u \<longleftrightarrow> DERIV g y :> v"
   811   unfolding DERIV_iff2
   812 proof (rule filterlim_cong)
   813   assume *: "eventually (\<lambda>x. f x = g x) (nhds x)"
   814   moreover from * have "f x = g x" by (auto simp: eventually_nhds)
   815   moreover assume "x = y" "u = v"
   816   ultimately show "eventually (\<lambda>xa. (f xa - f x) / (xa - x) = (g xa - g y) / (xa - y)) (at x)"
   817     by (auto simp: eventually_at_filter elim: eventually_elim1)
   818 qed simp_all
   819 
   820 lemma DERIV_shift:
   821   "(DERIV f (x + z) :> y) \<longleftrightarrow> (DERIV (\<lambda>x. f (x + z)) x :> y)"
   822   by (simp add: DERIV_def field_simps)
   823 
   824 lemma DERIV_mirror:
   825   "(DERIV f (- x) :> y) \<longleftrightarrow> (DERIV (\<lambda>x. f (- x::real) :: real) x :> - y)"
   826   by (simp add: DERIV_def filterlim_at_split filterlim_at_left_to_right
   827                 tendsto_minus_cancel_left field_simps conj_commute)
   828 
   829 text {* Caratheodory formulation of derivative at a point *}
   830 
   831 lemma CARAT_DERIV: (*FIXME: SUPERSEDED BY THE ONE IN Deriv.thy. But still used by NSA/HDeriv.thy*)
   832   "(DERIV f x :> l) \<longleftrightarrow> (\<exists>g. (\<forall>z. f z - f x = g z * (z - x)) \<and> isCont g x \<and> g x = l)"
   833       (is "?lhs = ?rhs")
   834 proof
   835   assume der: "DERIV f x :> l"
   836   show "\<exists>g. (\<forall>z. f z - f x = g z * (z-x)) \<and> isCont g x \<and> g x = l"
   837   proof (intro exI conjI)
   838     let ?g = "(%z. if z = x then l else (f z - f x) / (z-x))"
   839     show "\<forall>z. f z - f x = ?g z * (z-x)" by simp
   840     show "isCont ?g x" using der
   841       by (simp add: isCont_iff DERIV_def cong: LIM_equal [rule_format])
   842     show "?g x = l" by simp
   843   qed
   844 next
   845   assume "?rhs"
   846   then obtain g where
   847     "(\<forall>z. f z - f x = g z * (z-x))" and "isCont g x" and "g x = l" by blast
   848   thus "(DERIV f x :> l)"
   849      by (auto simp add: isCont_iff DERIV_def cong: LIM_cong)
   850 qed
   851 
   852 text {*
   853  Let's do the standard proof, though theorem
   854  @{text "LIM_mult2"} follows from a NS proof
   855 *}
   856 
   857 subsection {* Local extrema *}
   858 
   859 text{*If @{term "0 < f'(x)"} then @{term x} is Locally Strictly Increasing At The Right*}
   860 
   861 lemma DERIV_pos_inc_right:
   862   fixes f :: "real => real"
   863   assumes der: "DERIV f x :> l"
   864       and l:   "0 < l"
   865   shows "\<exists>d > 0. \<forall>h > 0. h < d --> f(x) < f(x + h)"
   866 proof -
   867   from l der [THEN DERIV_D, THEN LIM_D [where r = "l"]]
   868   have "\<exists>s > 0. (\<forall>z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < l)"
   869     by simp
   870   then obtain s
   871         where s:   "0 < s"
   872           and all: "!!z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < l"
   873     by auto
   874   thus ?thesis
   875   proof (intro exI conjI strip)
   876     show "0<s" using s .
   877     fix h::real
   878     assume "0 < h" "h < s"
   879     with all [of h] show "f x < f (x+h)"
   880     proof (simp add: abs_if pos_less_divide_eq split add: split_if_asm)
   881       assume "~ (f (x+h) - f x) / h < l" and h: "0 < h"
   882       with l
   883       have "0 < (f (x+h) - f x) / h" by arith
   884       thus "f x < f (x+h)"
   885   by (simp add: pos_less_divide_eq h)
   886     qed
   887   qed
   888 qed
   889 
   890 lemma DERIV_neg_dec_left:
   891   fixes f :: "real => real"
   892   assumes der: "DERIV f x :> l"
   893       and l:   "l < 0"
   894   shows "\<exists>d > 0. \<forall>h > 0. h < d --> f(x) < f(x-h)"
   895 proof -
   896   from l der [THEN DERIV_D, THEN LIM_D [where r = "-l"]]
   897   have "\<exists>s > 0. (\<forall>z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < -l)"
   898     by simp
   899   then obtain s
   900         where s:   "0 < s"
   901           and all: "!!z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < -l"
   902     by auto
   903   thus ?thesis
   904   proof (intro exI conjI strip)
   905     show "0<s" using s .
   906     fix h::real
   907     assume "0 < h" "h < s"
   908     with all [of "-h"] show "f x < f (x-h)"
   909     proof (simp add: abs_if pos_less_divide_eq split add: split_if_asm)
   910       assume " - ((f (x-h) - f x) / h) < l" and h: "0 < h"
   911       with l
   912       have "0 < (f (x-h) - f x) / h" by arith
   913       thus "f x < f (x-h)"
   914   by (simp add: pos_less_divide_eq h)
   915     qed
   916   qed
   917 qed
   918 
   919 lemma DERIV_pos_inc_left:
   920   fixes f :: "real => real"
   921   shows "DERIV f x :> l \<Longrightarrow> 0 < l \<Longrightarrow> \<exists>d > 0. \<forall>h > 0. h < d --> f(x - h) < f(x)"
   922   apply (rule DERIV_neg_dec_left [of "%x. - f x" "-l" x, simplified])
   923   apply (auto simp add: DERIV_minus)
   924   done
   925 
   926 lemma DERIV_neg_dec_right:
   927   fixes f :: "real => real"
   928   shows "DERIV f x :> l \<Longrightarrow> l < 0 \<Longrightarrow> \<exists>d > 0. \<forall>h > 0. h < d --> f(x) > f(x + h)"
   929   apply (rule DERIV_pos_inc_right [of "%x. - f x" "-l" x, simplified])
   930   apply (auto simp add: DERIV_minus)
   931   done
   932 
   933 lemma DERIV_local_max:
   934   fixes f :: "real => real"
   935   assumes der: "DERIV f x :> l"
   936       and d:   "0 < d"
   937       and le:  "\<forall>y. \<bar>x-y\<bar> < d --> f(y) \<le> f(x)"
   938   shows "l = 0"
   939 proof (cases rule: linorder_cases [of l 0])
   940   case equal thus ?thesis .
   941 next
   942   case less
   943   from DERIV_neg_dec_left [OF der less]
   944   obtain d' where d': "0 < d'"
   945              and lt: "\<forall>h > 0. h < d' \<longrightarrow> f x < f (x-h)" by blast
   946   from real_lbound_gt_zero [OF d d']
   947   obtain e where "0 < e \<and> e < d \<and> e < d'" ..
   948   with lt le [THEN spec [where x="x-e"]]
   949   show ?thesis by (auto simp add: abs_if)
   950 next
   951   case greater
   952   from DERIV_pos_inc_right [OF der greater]
   953   obtain d' where d': "0 < d'"
   954              and lt: "\<forall>h > 0. h < d' \<longrightarrow> f x < f (x + h)" by blast
   955   from real_lbound_gt_zero [OF d d']
   956   obtain e where "0 < e \<and> e < d \<and> e < d'" ..
   957   with lt le [THEN spec [where x="x+e"]]
   958   show ?thesis by (auto simp add: abs_if)
   959 qed
   960 
   961 
   962 text{*Similar theorem for a local minimum*}
   963 lemma DERIV_local_min:
   964   fixes f :: "real => real"
   965   shows "[| DERIV f x :> l; 0 < d; \<forall>y. \<bar>x-y\<bar> < d --> f(x) \<le> f(y) |] ==> l = 0"
   966 by (drule DERIV_minus [THEN DERIV_local_max], auto)
   967 
   968 
   969 text{*In particular, if a function is locally flat*}
   970 lemma DERIV_local_const:
   971   fixes f :: "real => real"
   972   shows "[| DERIV f x :> l; 0 < d; \<forall>y. \<bar>x-y\<bar> < d --> f(x) = f(y) |] ==> l = 0"
   973 by (auto dest!: DERIV_local_max)
   974 
   975 
   976 subsection {* Rolle's Theorem *}
   977 
   978 text{*Lemma about introducing open ball in open interval*}
   979 lemma lemma_interval_lt:
   980      "[| a < x;  x < b |]
   981       ==> \<exists>d::real. 0 < d & (\<forall>y. \<bar>x-y\<bar> < d --> a < y & y < b)"
   982 
   983 apply (simp add: abs_less_iff)
   984 apply (insert linorder_linear [of "x-a" "b-x"], safe)
   985 apply (rule_tac x = "x-a" in exI)
   986 apply (rule_tac [2] x = "b-x" in exI, auto)
   987 done
   988 
   989 lemma lemma_interval: "[| a < x;  x < b |] ==>
   990         \<exists>d::real. 0 < d &  (\<forall>y. \<bar>x-y\<bar> < d --> a \<le> y & y \<le> b)"
   991 apply (drule lemma_interval_lt, auto)
   992 apply force
   993 done
   994 
   995 text{*Rolle's Theorem.
   996    If @{term f} is defined and continuous on the closed interval
   997    @{text "[a,b]"} and differentiable on the open interval @{text "(a,b)"},
   998    and @{term "f(a) = f(b)"},
   999    then there exists @{text "x0 \<in> (a,b)"} such that @{term "f'(x0) = 0"}*}
  1000 theorem Rolle:
  1001   assumes lt: "a < b"
  1002       and eq: "f(a) = f(b)"
  1003       and con: "\<forall>x. a \<le> x & x \<le> b --> isCont f x"
  1004       and dif [rule_format]: "\<forall>x. a < x & x < b --> f differentiable (at x)"
  1005   shows "\<exists>z::real. a < z & z < b & DERIV f z :> 0"
  1006 proof -
  1007   have le: "a \<le> b" using lt by simp
  1008   from isCont_eq_Ub [OF le con]
  1009   obtain x where x_max: "\<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> f z \<le> f x"
  1010              and alex: "a \<le> x" and xleb: "x \<le> b"
  1011     by blast
  1012   from isCont_eq_Lb [OF le con]
  1013   obtain x' where x'_min: "\<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> f x' \<le> f z"
  1014               and alex': "a \<le> x'" and x'leb: "x' \<le> b"
  1015     by blast
  1016   show ?thesis
  1017   proof cases
  1018     assume axb: "a < x & x < b"
  1019         --{*@{term f} attains its maximum within the interval*}
  1020     hence ax: "a<x" and xb: "x<b" by arith + 
  1021     from lemma_interval [OF ax xb]
  1022     obtain d where d: "0<d" and bound: "\<forall>y. \<bar>x-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
  1023       by blast
  1024     hence bound': "\<forall>y. \<bar>x-y\<bar> < d \<longrightarrow> f y \<le> f x" using x_max
  1025       by blast
  1026     from differentiableD [OF dif [OF axb]]
  1027     obtain l where der: "DERIV f x :> l" ..
  1028     have "l=0" by (rule DERIV_local_max [OF der d bound'])
  1029         --{*the derivative at a local maximum is zero*}
  1030     thus ?thesis using ax xb der by auto
  1031   next
  1032     assume notaxb: "~ (a < x & x < b)"
  1033     hence xeqab: "x=a | x=b" using alex xleb by arith
  1034     hence fb_eq_fx: "f b = f x" by (auto simp add: eq)
  1035     show ?thesis
  1036     proof cases
  1037       assume ax'b: "a < x' & x' < b"
  1038         --{*@{term f} attains its minimum within the interval*}
  1039       hence ax': "a<x'" and x'b: "x'<b" by arith+ 
  1040       from lemma_interval [OF ax' x'b]
  1041       obtain d where d: "0<d" and bound: "\<forall>y. \<bar>x'-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
  1042   by blast
  1043       hence bound': "\<forall>y. \<bar>x'-y\<bar> < d \<longrightarrow> f x' \<le> f y" using x'_min
  1044   by blast
  1045       from differentiableD [OF dif [OF ax'b]]
  1046       obtain l where der: "DERIV f x' :> l" ..
  1047       have "l=0" by (rule DERIV_local_min [OF der d bound'])
  1048         --{*the derivative at a local minimum is zero*}
  1049       thus ?thesis using ax' x'b der by auto
  1050     next
  1051       assume notax'b: "~ (a < x' & x' < b)"
  1052         --{*@{term f} is constant througout the interval*}
  1053       hence x'eqab: "x'=a | x'=b" using alex' x'leb by arith
  1054       hence fb_eq_fx': "f b = f x'" by (auto simp add: eq)
  1055       from dense [OF lt]
  1056       obtain r where ar: "a < r" and rb: "r < b" by blast
  1057       from lemma_interval [OF ar rb]
  1058       obtain d where d: "0<d" and bound: "\<forall>y. \<bar>r-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
  1059   by blast
  1060       have eq_fb: "\<forall>z. a \<le> z --> z \<le> b --> f z = f b"
  1061       proof (clarify)
  1062         fix z::real
  1063         assume az: "a \<le> z" and zb: "z \<le> b"
  1064         show "f z = f b"
  1065         proof (rule order_antisym)
  1066           show "f z \<le> f b" by (simp add: fb_eq_fx x_max az zb)
  1067           show "f b \<le> f z" by (simp add: fb_eq_fx' x'_min az zb)
  1068         qed
  1069       qed
  1070       have bound': "\<forall>y. \<bar>r-y\<bar> < d \<longrightarrow> f r = f y"
  1071       proof (intro strip)
  1072         fix y::real
  1073         assume lt: "\<bar>r-y\<bar> < d"
  1074         hence "f y = f b" by (simp add: eq_fb bound)
  1075         thus "f r = f y" by (simp add: eq_fb ar rb order_less_imp_le)
  1076       qed
  1077       from differentiableD [OF dif [OF conjI [OF ar rb]]]
  1078       obtain l where der: "DERIV f r :> l" ..
  1079       have "l=0" by (rule DERIV_local_const [OF der d bound'])
  1080         --{*the derivative of a constant function is zero*}
  1081       thus ?thesis using ar rb der by auto
  1082     qed
  1083   qed
  1084 qed
  1085 
  1086 
  1087 subsection{*Mean Value Theorem*}
  1088 
  1089 lemma lemma_MVT:
  1090      "f a - (f b - f a)/(b-a) * a = f b - (f b - f a)/(b-a) * (b::real)"
  1091   by (cases "a = b") (simp_all add: field_simps)
  1092 
  1093 theorem MVT:
  1094   assumes lt:  "a < b"
  1095       and con: "\<forall>x. a \<le> x & x \<le> b --> isCont f x"
  1096       and dif [rule_format]: "\<forall>x. a < x & x < b --> f differentiable (at x)"
  1097   shows "\<exists>l z::real. a < z & z < b & DERIV f z :> l &
  1098                    (f(b) - f(a) = (b-a) * l)"
  1099 proof -
  1100   let ?F = "%x. f x - ((f b - f a) / (b-a)) * x"
  1101   have contF: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont ?F x"
  1102     using con by (fast intro: continuous_intros)
  1103   have difF: "\<forall>x. a < x \<and> x < b \<longrightarrow> ?F differentiable (at x)"
  1104   proof (clarify)
  1105     fix x::real
  1106     assume ax: "a < x" and xb: "x < b"
  1107     from differentiableD [OF dif [OF conjI [OF ax xb]]]
  1108     obtain l where der: "DERIV f x :> l" ..
  1109     show "?F differentiable (at x)"
  1110       by (rule differentiableI [where D = "l - (f b - f a)/(b-a)"],
  1111           blast intro: DERIV_diff DERIV_cmult_Id der)
  1112   qed
  1113   from Rolle [where f = ?F, OF lt lemma_MVT contF difF]
  1114   obtain z where az: "a < z" and zb: "z < b" and der: "DERIV ?F z :> 0"
  1115     by blast
  1116   have "DERIV (%x. ((f b - f a)/(b-a)) * x) z :> (f b - f a)/(b-a)"
  1117     by (rule DERIV_cmult_Id)
  1118   hence derF: "DERIV (\<lambda>x. ?F x + (f b - f a) / (b - a) * x) z
  1119                    :> 0 + (f b - f a) / (b - a)"
  1120     by (rule DERIV_add [OF der])
  1121   show ?thesis
  1122   proof (intro exI conjI)
  1123     show "a < z" using az .
  1124     show "z < b" using zb .
  1125     show "f b - f a = (b - a) * ((f b - f a)/(b-a))" by (simp)
  1126     show "DERIV f z :> ((f b - f a)/(b-a))"  using derF by simp
  1127   qed
  1128 qed
  1129 
  1130 lemma MVT2:
  1131      "[| a < b; \<forall>x. a \<le> x & x \<le> b --> DERIV f x :> f'(x) |]
  1132       ==> \<exists>z::real. a < z & z < b & (f b - f a = (b - a) * f'(z))"
  1133 apply (drule MVT)
  1134 apply (blast intro: DERIV_isCont)
  1135 apply (force dest: order_less_imp_le simp add: real_differentiable_def)
  1136 apply (blast dest: DERIV_unique order_less_imp_le)
  1137 done
  1138 
  1139 
  1140 text{*A function is constant if its derivative is 0 over an interval.*}
  1141 
  1142 lemma DERIV_isconst_end:
  1143   fixes f :: "real => real"
  1144   shows "[| a < b;
  1145          \<forall>x. a \<le> x & x \<le> b --> isCont f x;
  1146          \<forall>x. a < x & x < b --> DERIV f x :> 0 |]
  1147         ==> f b = f a"
  1148 apply (drule MVT, assumption)
  1149 apply (blast intro: differentiableI)
  1150 apply (auto dest!: DERIV_unique simp add: diff_eq_eq)
  1151 done
  1152 
  1153 lemma DERIV_isconst1:
  1154   fixes f :: "real => real"
  1155   shows "[| a < b;
  1156          \<forall>x. a \<le> x & x \<le> b --> isCont f x;
  1157          \<forall>x. a < x & x < b --> DERIV f x :> 0 |]
  1158         ==> \<forall>x. a \<le> x & x \<le> b --> f x = f a"
  1159 apply safe
  1160 apply (drule_tac x = a in order_le_imp_less_or_eq, safe)
  1161 apply (drule_tac b = x in DERIV_isconst_end, auto)
  1162 done
  1163 
  1164 lemma DERIV_isconst2:
  1165   fixes f :: "real => real"
  1166   shows "[| a < b;
  1167          \<forall>x. a \<le> x & x \<le> b --> isCont f x;
  1168          \<forall>x. a < x & x < b --> DERIV f x :> 0;
  1169          a \<le> x; x \<le> b |]
  1170         ==> f x = f a"
  1171 apply (blast dest: DERIV_isconst1)
  1172 done
  1173 
  1174 lemma DERIV_isconst3: fixes a b x y :: real
  1175   assumes "a < b" and "x \<in> {a <..< b}" and "y \<in> {a <..< b}"
  1176   assumes derivable: "\<And>x. x \<in> {a <..< b} \<Longrightarrow> DERIV f x :> 0"
  1177   shows "f x = f y"
  1178 proof (cases "x = y")
  1179   case False
  1180   let ?a = "min x y"
  1181   let ?b = "max x y"
  1182   
  1183   have "\<forall>z. ?a \<le> z \<and> z \<le> ?b \<longrightarrow> DERIV f z :> 0"
  1184   proof (rule allI, rule impI)
  1185     fix z :: real assume "?a \<le> z \<and> z \<le> ?b"
  1186     hence "a < z" and "z < b" using `x \<in> {a <..< b}` and `y \<in> {a <..< b}` by auto
  1187     hence "z \<in> {a<..<b}" by auto
  1188     thus "DERIV f z :> 0" by (rule derivable)
  1189   qed
  1190   hence isCont: "\<forall>z. ?a \<le> z \<and> z \<le> ?b \<longrightarrow> isCont f z"
  1191     and DERIV: "\<forall>z. ?a < z \<and> z < ?b \<longrightarrow> DERIV f z :> 0" using DERIV_isCont by auto
  1192 
  1193   have "?a < ?b" using `x \<noteq> y` by auto
  1194   from DERIV_isconst2[OF this isCont DERIV, of x] and DERIV_isconst2[OF this isCont DERIV, of y]
  1195   show ?thesis by auto
  1196 qed auto
  1197 
  1198 lemma DERIV_isconst_all:
  1199   fixes f :: "real => real"
  1200   shows "\<forall>x. DERIV f x :> 0 ==> f(x) = f(y)"
  1201 apply (rule linorder_cases [of x y])
  1202 apply (blast intro: sym DERIV_isCont DERIV_isconst_end)+
  1203 done
  1204 
  1205 lemma DERIV_const_ratio_const:
  1206   fixes f :: "real => real"
  1207   shows "[|a \<noteq> b; \<forall>x. DERIV f x :> k |] ==> (f(b) - f(a)) = (b-a) * k"
  1208 apply (rule linorder_cases [of a b], auto)
  1209 apply (drule_tac [!] f = f in MVT)
  1210 apply (auto dest: DERIV_isCont DERIV_unique simp add: real_differentiable_def)
  1211 apply (auto dest: DERIV_unique simp add: ring_distribs)
  1212 done
  1213 
  1214 lemma DERIV_const_ratio_const2:
  1215   fixes f :: "real => real"
  1216   shows "[|a \<noteq> b; \<forall>x. DERIV f x :> k |] ==> (f(b) - f(a))/(b-a) = k"
  1217 apply (rule_tac c1 = "b-a" in mult_right_cancel [THEN iffD1])
  1218 apply (auto dest!: DERIV_const_ratio_const simp add: mult_assoc)
  1219 done
  1220 
  1221 lemma real_average_minus_first [simp]: "((a + b) /2 - a) = (b-a)/(2::real)"
  1222 by (simp)
  1223 
  1224 lemma real_average_minus_second [simp]: "((b + a)/2 - a) = (b-a)/(2::real)"
  1225 by (simp)
  1226 
  1227 text{*Gallileo's "trick": average velocity = av. of end velocities*}
  1228 
  1229 lemma DERIV_const_average:
  1230   fixes v :: "real => real"
  1231   assumes neq: "a \<noteq> (b::real)"
  1232       and der: "\<forall>x. DERIV v x :> k"
  1233   shows "v ((a + b)/2) = (v a + v b)/2"
  1234 proof (cases rule: linorder_cases [of a b])
  1235   case equal with neq show ?thesis by simp
  1236 next
  1237   case less
  1238   have "(v b - v a) / (b - a) = k"
  1239     by (rule DERIV_const_ratio_const2 [OF neq der])
  1240   hence "(b-a) * ((v b - v a) / (b-a)) = (b-a) * k" by simp
  1241   moreover have "(v ((a + b) / 2) - v a) / ((a + b) / 2 - a) = k"
  1242     by (rule DERIV_const_ratio_const2 [OF _ der], simp add: neq)
  1243   ultimately show ?thesis using neq by force
  1244 next
  1245   case greater
  1246   have "(v b - v a) / (b - a) = k"
  1247     by (rule DERIV_const_ratio_const2 [OF neq der])
  1248   hence "(b-a) * ((v b - v a) / (b-a)) = (b-a) * k" by simp
  1249   moreover have " (v ((b + a) / 2) - v a) / ((b + a) / 2 - a) = k"
  1250     by (rule DERIV_const_ratio_const2 [OF _ der], simp add: neq)
  1251   ultimately show ?thesis using neq by (force simp add: add_commute)
  1252 qed
  1253 
  1254 (* A function with positive derivative is increasing. 
  1255    A simple proof using the MVT, by Jeremy Avigad. And variants.
  1256 *)
  1257 lemma DERIV_pos_imp_increasing_open:
  1258   fixes a::real and b::real and f::"real => real"
  1259   assumes "a < b" and "\<And>x. a < x \<Longrightarrow> x < b \<Longrightarrow> (EX y. DERIV f x :> y & y > 0)"
  1260       and con: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> isCont f x"
  1261   shows "f a < f b"
  1262 proof (rule ccontr)
  1263   assume f: "~ f a < f b"
  1264   have "EX l z. a < z & z < b & DERIV f z :> l
  1265       & f b - f a = (b - a) * l"
  1266     apply (rule MVT)
  1267       using assms Deriv.differentiableI
  1268       apply force+
  1269     done
  1270   then obtain l z where z: "a < z" "z < b" "DERIV f z :> l"
  1271       and "f b - f a = (b - a) * l"
  1272     by auto
  1273   with assms f have "~(l > 0)"
  1274     by (metis linorder_not_le mult_le_0_iff diff_le_0_iff_le)
  1275   with assms z show False
  1276     by (metis DERIV_unique)
  1277 qed
  1278 
  1279 lemma DERIV_pos_imp_increasing:
  1280   fixes a::real and b::real and f::"real => real"
  1281   assumes "a < b" and "\<forall>x. a \<le> x & x \<le> b --> (EX y. DERIV f x :> y & y > 0)"
  1282   shows "f a < f b"
  1283 by (metis DERIV_pos_imp_increasing_open [of a b f] assms DERIV_continuous less_imp_le)
  1284 
  1285 lemma DERIV_nonneg_imp_nondecreasing:
  1286   fixes a::real and b::real and f::"real => real"
  1287   assumes "a \<le> b" and
  1288     "\<forall>x. a \<le> x & x \<le> b --> (\<exists>y. DERIV f x :> y & y \<ge> 0)"
  1289   shows "f a \<le> f b"
  1290 proof (rule ccontr, cases "a = b")
  1291   assume "~ f a \<le> f b" and "a = b"
  1292   then show False by auto
  1293 next
  1294   assume A: "~ f a \<le> f b"
  1295   assume B: "a ~= b"
  1296   with assms have "EX l z. a < z & z < b & DERIV f z :> l
  1297       & f b - f a = (b - a) * l"
  1298     apply -
  1299     apply (rule MVT)
  1300       apply auto
  1301       apply (metis DERIV_isCont)
  1302      apply (metis differentiableI less_le)
  1303     done
  1304   then obtain l z where z: "a < z" "z < b" "DERIV f z :> l"
  1305       and C: "f b - f a = (b - a) * l"
  1306     by auto
  1307   with A have "a < b" "f b < f a" by auto
  1308   with C have "\<not> l \<ge> 0" by (auto simp add: not_le algebra_simps)
  1309     (metis A add_le_cancel_right assms(1) less_eq_real_def mult_right_mono add_left_mono linear order_refl)
  1310   with assms z show False
  1311     by (metis DERIV_unique order_less_imp_le)
  1312 qed
  1313 
  1314 lemma DERIV_neg_imp_decreasing_open:
  1315   fixes a::real and b::real and f::"real => real"
  1316   assumes "a < b" and "\<And>x. a < x \<Longrightarrow> x < b \<Longrightarrow> (EX y. DERIV f x :> y & y < 0)"
  1317       and con: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> isCont f x"
  1318   shows "f a > f b"
  1319 proof -
  1320   have "(%x. -f x) a < (%x. -f x) b"
  1321     apply (rule DERIV_pos_imp_increasing_open [of a b "%x. -f x"])
  1322     using assms
  1323     apply auto
  1324     apply (metis field_differentiable_minus neg_0_less_iff_less)
  1325     done
  1326   thus ?thesis
  1327     by simp
  1328 qed
  1329 
  1330 lemma DERIV_neg_imp_decreasing:
  1331   fixes a::real and b::real and f::"real => real"
  1332   assumes "a < b" and
  1333     "\<forall>x. a \<le> x & x \<le> b --> (\<exists>y. DERIV f x :> y & y < 0)"
  1334   shows "f a > f b"
  1335 by (metis DERIV_neg_imp_decreasing_open [of a b f] assms DERIV_continuous less_imp_le)
  1336 
  1337 lemma DERIV_nonpos_imp_nonincreasing:
  1338   fixes a::real and b::real and f::"real => real"
  1339   assumes "a \<le> b" and
  1340     "\<forall>x. a \<le> x & x \<le> b --> (\<exists>y. DERIV f x :> y & y \<le> 0)"
  1341   shows "f a \<ge> f b"
  1342 proof -
  1343   have "(%x. -f x) a \<le> (%x. -f x) b"
  1344     apply (rule DERIV_nonneg_imp_nondecreasing [of a b "%x. -f x"])
  1345     using assms
  1346     apply auto
  1347     apply (metis DERIV_minus neg_0_le_iff_le)
  1348     done
  1349   thus ?thesis
  1350     by simp
  1351 qed
  1352 
  1353 lemma DERIV_pos_imp_increasing_at_bot:
  1354   fixes f :: "real => real"
  1355   assumes "\<And>x. x \<le> b \<Longrightarrow> (EX y. DERIV f x :> y & y > 0)"
  1356       and lim: "(f ---> flim) at_bot"
  1357   shows "flim < f b"
  1358 proof -
  1359   have "flim \<le> f (b - 1)"
  1360     apply (rule tendsto_ge_const [OF _ lim])
  1361     apply (auto simp: trivial_limit_at_bot_linorder eventually_at_bot_linorder)
  1362     apply (rule_tac x="b - 2" in exI)
  1363     apply (force intro: order.strict_implies_order DERIV_pos_imp_increasing [where f=f] assms)
  1364     done
  1365   also have "... < f b"
  1366     by (force intro: DERIV_pos_imp_increasing [where f=f] assms)
  1367   finally show ?thesis .
  1368 qed
  1369 
  1370 lemma DERIV_neg_imp_decreasing_at_top:
  1371   fixes f :: "real => real"
  1372   assumes der: "\<And>x. x \<ge> b \<Longrightarrow> (EX y. DERIV f x :> y & y < 0)"
  1373       and lim: "(f ---> flim) at_top"
  1374   shows "flim < f b"
  1375   apply (rule DERIV_pos_imp_increasing_at_bot [where f = "\<lambda>i. f (-i)" and b = "-b", simplified])
  1376   apply (metis DERIV_mirror der le_minus_iff neg_0_less_iff_less)
  1377   apply (metis filterlim_at_top_mirror lim)
  1378   done
  1379 
  1380 text {* Derivative of inverse function *}
  1381 
  1382 lemma DERIV_inverse_function:
  1383   fixes f g :: "real \<Rightarrow> real"
  1384   assumes der: "DERIV f (g x) :> D"
  1385   assumes neq: "D \<noteq> 0"
  1386   assumes a: "a < x" and b: "x < b"
  1387   assumes inj: "\<forall>y. a < y \<and> y < b \<longrightarrow> f (g y) = y"
  1388   assumes cont: "isCont g x"
  1389   shows "DERIV g x :> inverse D"
  1390 unfolding DERIV_iff2
  1391 proof (rule LIM_equal2)
  1392   show "0 < min (x - a) (b - x)"
  1393     using a b by arith 
  1394 next
  1395   fix y
  1396   assume "norm (y - x) < min (x - a) (b - x)"
  1397   hence "a < y" and "y < b" 
  1398     by (simp_all add: abs_less_iff)
  1399   thus "(g y - g x) / (y - x) =
  1400         inverse ((f (g y) - x) / (g y - g x))"
  1401     by (simp add: inj)
  1402 next
  1403   have "(\<lambda>z. (f z - f (g x)) / (z - g x)) -- g x --> D"
  1404     by (rule der [unfolded DERIV_iff2])
  1405   hence 1: "(\<lambda>z. (f z - x) / (z - g x)) -- g x --> D"
  1406     using inj a b by simp
  1407   have 2: "\<exists>d>0. \<forall>y. y \<noteq> x \<and> norm (y - x) < d \<longrightarrow> g y \<noteq> g x"
  1408   proof (rule exI, safe)
  1409     show "0 < min (x - a) (b - x)"
  1410       using a b by simp
  1411   next
  1412     fix y
  1413     assume "norm (y - x) < min (x - a) (b - x)"
  1414     hence y: "a < y" "y < b"
  1415       by (simp_all add: abs_less_iff)
  1416     assume "g y = g x"
  1417     hence "f (g y) = f (g x)" by simp
  1418     hence "y = x" using inj y a b by simp
  1419     also assume "y \<noteq> x"
  1420     finally show False by simp
  1421   qed
  1422   have "(\<lambda>y. (f (g y) - x) / (g y - g x)) -- x --> D"
  1423     using cont 1 2 by (rule isCont_LIM_compose2)
  1424   thus "(\<lambda>y. inverse ((f (g y) - x) / (g y - g x)))
  1425         -- x --> inverse D"
  1426     using neq by (rule tendsto_inverse)
  1427 qed
  1428 
  1429 subsection {* Generalized Mean Value Theorem *}
  1430 
  1431 theorem GMVT:
  1432   fixes a b :: real
  1433   assumes alb: "a < b"
  1434     and fc: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x"
  1435     and fd: "\<forall>x. a < x \<and> x < b \<longrightarrow> f differentiable (at x)"
  1436     and gc: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont g x"
  1437     and gd: "\<forall>x. a < x \<and> x < b \<longrightarrow> g differentiable (at x)"
  1438   shows "\<exists>g'c f'c c.
  1439     DERIV g c :> g'c \<and> DERIV f c :> f'c \<and> a < c \<and> c < b \<and> ((f b - f a) * g'c) = ((g b - g a) * f'c)"
  1440 proof -
  1441   let ?h = "\<lambda>x. (f b - f a)*(g x) - (g b - g a)*(f x)"
  1442   from assms have "a < b" by simp
  1443   moreover have "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont ?h x"
  1444     using fc gc by simp
  1445   moreover have "\<forall>x. a < x \<and> x < b \<longrightarrow> ?h differentiable (at x)"
  1446     using fd gd by simp
  1447   ultimately have "\<exists>l z. a < z \<and> z < b \<and> DERIV ?h z :> l \<and> ?h b - ?h a = (b - a) * l" by (rule MVT)
  1448   then obtain l where ldef: "\<exists>z. a < z \<and> z < b \<and> DERIV ?h z :> l \<and> ?h b - ?h a = (b - a) * l" ..
  1449   then obtain c where cdef: "a < c \<and> c < b \<and> DERIV ?h c :> l \<and> ?h b - ?h a = (b - a) * l" ..
  1450 
  1451   from cdef have cint: "a < c \<and> c < b" by auto
  1452   with gd have "g differentiable (at c)" by simp
  1453   hence "\<exists>D. DERIV g c :> D" by (rule differentiableD)
  1454   then obtain g'c where g'cdef: "DERIV g c :> g'c" ..
  1455 
  1456   from cdef have "a < c \<and> c < b" by auto
  1457   with fd have "f differentiable (at c)" by simp
  1458   hence "\<exists>D. DERIV f c :> D" by (rule differentiableD)
  1459   then obtain f'c where f'cdef: "DERIV f c :> f'c" ..
  1460 
  1461   from cdef have "DERIV ?h c :> l" by auto
  1462   moreover have "DERIV ?h c :>  g'c * (f b - f a) - f'c * (g b - g a)"
  1463     using g'cdef f'cdef by (auto intro!: derivative_eq_intros)
  1464   ultimately have leq: "l =  g'c * (f b - f a) - f'c * (g b - g a)" by (rule DERIV_unique)
  1465 
  1466   {
  1467     from cdef have "?h b - ?h a = (b - a) * l" by auto
  1468     also from leq have "\<dots> = (b - a) * (g'c * (f b - f a) - f'c * (g b - g a))" by simp
  1469     finally have "?h b - ?h a = (b - a) * (g'c * (f b - f a) - f'c * (g b - g a))" by simp
  1470   }
  1471   moreover
  1472   {
  1473     have "?h b - ?h a =
  1474          ((f b)*(g b) - (f a)*(g b) - (g b)*(f b) + (g a)*(f b)) -
  1475           ((f b)*(g a) - (f a)*(g a) - (g b)*(f a) + (g a)*(f a))"
  1476       by (simp add: algebra_simps)
  1477     hence "?h b - ?h a = 0" by auto
  1478   }
  1479   ultimately have "(b - a) * (g'c * (f b - f a) - f'c * (g b - g a)) = 0" by auto
  1480   with alb have "g'c * (f b - f a) - f'c * (g b - g a) = 0" by simp
  1481   hence "g'c * (f b - f a) = f'c * (g b - g a)" by simp
  1482   hence "(f b - f a) * g'c = (g b - g a) * f'c" by (simp add: mult_ac)
  1483 
  1484   with g'cdef f'cdef cint show ?thesis by auto
  1485 qed
  1486 
  1487 lemma GMVT':
  1488   fixes f g :: "real \<Rightarrow> real"
  1489   assumes "a < b"
  1490   assumes isCont_f: "\<And>z. a \<le> z \<Longrightarrow> z \<le> b \<Longrightarrow> isCont f z"
  1491   assumes isCont_g: "\<And>z. a \<le> z \<Longrightarrow> z \<le> b \<Longrightarrow> isCont g z"
  1492   assumes DERIV_g: "\<And>z. a < z \<Longrightarrow> z < b \<Longrightarrow> DERIV g z :> (g' z)"
  1493   assumes DERIV_f: "\<And>z. a < z \<Longrightarrow> z < b \<Longrightarrow> DERIV f z :> (f' z)"
  1494   shows "\<exists>c. a < c \<and> c < b \<and> (f b - f a) * g' c = (g b - g a) * f' c"
  1495 proof -
  1496   have "\<exists>g'c f'c c. DERIV g c :> g'c \<and> DERIV f c :> f'c \<and>
  1497     a < c \<and> c < b \<and> (f b - f a) * g'c = (g b - g a) * f'c"
  1498     using assms by (intro GMVT) (force simp: real_differentiable_def)+
  1499   then obtain c where "a < c" "c < b" "(f b - f a) * g' c = (g b - g a) * f' c"
  1500     using DERIV_f DERIV_g by (force dest: DERIV_unique)
  1501   then show ?thesis
  1502     by auto
  1503 qed
  1504 
  1505 
  1506 subsection {* L'Hopitals rule *}
  1507 
  1508 lemma isCont_If_ge:
  1509   fixes a :: "'a :: linorder_topology"
  1510   shows "continuous (at_left a) g \<Longrightarrow> (f ---> g a) (at_right a) \<Longrightarrow> isCont (\<lambda>x. if x \<le> a then g x else f x) a"
  1511   unfolding isCont_def continuous_within
  1512   apply (intro filterlim_split_at)
  1513   apply (subst filterlim_cong[OF refl refl, where g=g])
  1514   apply (simp_all add: eventually_at_filter less_le)
  1515   apply (subst filterlim_cong[OF refl refl, where g=f])
  1516   apply (simp_all add: eventually_at_filter less_le)
  1517   done
  1518 
  1519 lemma lhopital_right_0:
  1520   fixes f0 g0 :: "real \<Rightarrow> real"
  1521   assumes f_0: "(f0 ---> 0) (at_right 0)"
  1522   assumes g_0: "(g0 ---> 0) (at_right 0)"
  1523   assumes ev:
  1524     "eventually (\<lambda>x. g0 x \<noteq> 0) (at_right 0)"
  1525     "eventually (\<lambda>x. g' x \<noteq> 0) (at_right 0)"
  1526     "eventually (\<lambda>x. DERIV f0 x :> f' x) (at_right 0)"
  1527     "eventually (\<lambda>x. DERIV g0 x :> g' x) (at_right 0)"
  1528   assumes lim: "((\<lambda> x. (f' x / g' x)) ---> x) (at_right 0)"
  1529   shows "((\<lambda> x. f0 x / g0 x) ---> x) (at_right 0)"
  1530 proof -
  1531   def f \<equiv> "\<lambda>x. if x \<le> 0 then 0 else f0 x"
  1532   then have "f 0 = 0" by simp
  1533 
  1534   def g \<equiv> "\<lambda>x. if x \<le> 0 then 0 else g0 x"
  1535   then have "g 0 = 0" by simp
  1536 
  1537   have "eventually (\<lambda>x. g0 x \<noteq> 0 \<and> g' x \<noteq> 0 \<and>
  1538       DERIV f0 x :> (f' x) \<and> DERIV g0 x :> (g' x)) (at_right 0)"
  1539     using ev by eventually_elim auto
  1540   then obtain a where [arith]: "0 < a"
  1541     and g0_neq_0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> g0 x \<noteq> 0"
  1542     and g'_neq_0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> g' x \<noteq> 0"
  1543     and f0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> DERIV f0 x :> (f' x)"
  1544     and g0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> DERIV g0 x :> (g' x)"
  1545     unfolding eventually_at by (auto simp: dist_real_def)
  1546 
  1547   have g_neq_0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> g x \<noteq> 0"
  1548     using g0_neq_0 by (simp add: g_def)
  1549 
  1550   { fix x assume x: "0 < x" "x < a" then have "DERIV f x :> (f' x)"
  1551       by (intro DERIV_cong_ev[THEN iffD1, OF _ _ _ f0[OF x]])
  1552          (auto simp: f_def eventually_nhds_metric dist_real_def intro!: exI[of _ x]) }
  1553   note f = this
  1554 
  1555   { fix x assume x: "0 < x" "x < a" then have "DERIV g x :> (g' x)"
  1556       by (intro DERIV_cong_ev[THEN iffD1, OF _ _ _ g0[OF x]])
  1557          (auto simp: g_def eventually_nhds_metric dist_real_def intro!: exI[of _ x]) }
  1558   note g = this
  1559 
  1560   have "isCont f 0"
  1561     unfolding f_def by (intro isCont_If_ge f_0 continuous_const)
  1562 
  1563   have "isCont g 0"
  1564     unfolding g_def by (intro isCont_If_ge g_0 continuous_const)
  1565 
  1566   have "\<exists>\<zeta>. \<forall>x\<in>{0 <..< a}. 0 < \<zeta> x \<and> \<zeta> x < x \<and> f x / g x = f' (\<zeta> x) / g' (\<zeta> x)"
  1567   proof (rule bchoice, rule)
  1568     fix x assume "x \<in> {0 <..< a}"
  1569     then have x[arith]: "0 < x" "x < a" by auto
  1570     with g'_neq_0 g_neq_0 `g 0 = 0` have g': "\<And>x. 0 < x \<Longrightarrow> x < a  \<Longrightarrow> 0 \<noteq> g' x" "g 0 \<noteq> g x"
  1571       by auto
  1572     have "\<And>x. 0 \<le> x \<Longrightarrow> x < a \<Longrightarrow> isCont f x"
  1573       using `isCont f 0` f by (auto intro: DERIV_isCont simp: le_less)
  1574     moreover have "\<And>x. 0 \<le> x \<Longrightarrow> x < a \<Longrightarrow> isCont g x"
  1575       using `isCont g 0` g by (auto intro: DERIV_isCont simp: le_less)
  1576     ultimately have "\<exists>c. 0 < c \<and> c < x \<and> (f x - f 0) * g' c = (g x - g 0) * f' c"
  1577       using f g `x < a` by (intro GMVT') auto
  1578     then obtain c where *: "0 < c" "c < x" "(f x - f 0) * g' c = (g x - g 0) * f' c"
  1579       by blast
  1580     moreover
  1581     from * g'(1)[of c] g'(2) have "(f x - f 0)  / (g x - g 0) = f' c / g' c"
  1582       by (simp add: field_simps)
  1583     ultimately show "\<exists>y. 0 < y \<and> y < x \<and> f x / g x = f' y / g' y"
  1584       using `f 0 = 0` `g 0 = 0` by (auto intro!: exI[of _ c])
  1585   qed
  1586   then obtain \<zeta> where "\<forall>x\<in>{0 <..< a}. 0 < \<zeta> x \<and> \<zeta> x < x \<and> f x / g x = f' (\<zeta> x) / g' (\<zeta> x)" ..
  1587   then have \<zeta>: "eventually (\<lambda>x. 0 < \<zeta> x \<and> \<zeta> x < x \<and> f x / g x = f' (\<zeta> x) / g' (\<zeta> x)) (at_right 0)"
  1588     unfolding eventually_at by (intro exI[of _ a]) (auto simp: dist_real_def)
  1589   moreover
  1590   from \<zeta> have "eventually (\<lambda>x. norm (\<zeta> x) \<le> x) (at_right 0)"
  1591     by eventually_elim auto
  1592   then have "((\<lambda>x. norm (\<zeta> x)) ---> 0) (at_right 0)"
  1593     by (rule_tac real_tendsto_sandwich[where f="\<lambda>x. 0" and h="\<lambda>x. x"])
  1594        (auto intro: tendsto_const tendsto_ident_at)
  1595   then have "(\<zeta> ---> 0) (at_right 0)"
  1596     by (rule tendsto_norm_zero_cancel)
  1597   with \<zeta> have "filterlim \<zeta> (at_right 0) (at_right 0)"
  1598     by (auto elim!: eventually_elim1 simp: filterlim_at)
  1599   from this lim have "((\<lambda>t. f' (\<zeta> t) / g' (\<zeta> t)) ---> x) (at_right 0)"
  1600     by (rule_tac filterlim_compose[of _ _ _ \<zeta>])
  1601   ultimately have "((\<lambda>t. f t / g t) ---> x) (at_right 0)" (is ?P)
  1602     by (rule_tac filterlim_cong[THEN iffD1, OF refl refl])
  1603        (auto elim: eventually_elim1)
  1604   also have "?P \<longleftrightarrow> ?thesis"
  1605     by (rule filterlim_cong) (auto simp: f_def g_def eventually_at_filter)
  1606   finally show ?thesis .
  1607 qed
  1608 
  1609 lemma lhopital_right:
  1610   "((f::real \<Rightarrow> real) ---> 0) (at_right x) \<Longrightarrow> (g ---> 0) (at_right x) \<Longrightarrow>
  1611     eventually (\<lambda>x. g x \<noteq> 0) (at_right x) \<Longrightarrow>
  1612     eventually (\<lambda>x. g' x \<noteq> 0) (at_right x) \<Longrightarrow>
  1613     eventually (\<lambda>x. DERIV f x :> f' x) (at_right x) \<Longrightarrow>
  1614     eventually (\<lambda>x. DERIV g x :> g' x) (at_right x) \<Longrightarrow>
  1615     ((\<lambda> x. (f' x / g' x)) ---> y) (at_right x) \<Longrightarrow>
  1616   ((\<lambda> x. f x / g x) ---> y) (at_right x)"
  1617   unfolding eventually_at_right_to_0[of _ x] filterlim_at_right_to_0[of _ _ x] DERIV_shift
  1618   by (rule lhopital_right_0)
  1619 
  1620 lemma lhopital_left:
  1621   "((f::real \<Rightarrow> real) ---> 0) (at_left x) \<Longrightarrow> (g ---> 0) (at_left x) \<Longrightarrow>
  1622     eventually (\<lambda>x. g x \<noteq> 0) (at_left x) \<Longrightarrow>
  1623     eventually (\<lambda>x. g' x \<noteq> 0) (at_left x) \<Longrightarrow>
  1624     eventually (\<lambda>x. DERIV f x :> f' x) (at_left x) \<Longrightarrow>
  1625     eventually (\<lambda>x. DERIV g x :> g' x) (at_left x) \<Longrightarrow>
  1626     ((\<lambda> x. (f' x / g' x)) ---> y) (at_left x) \<Longrightarrow>
  1627   ((\<lambda> x. f x / g x) ---> y) (at_left x)"
  1628   unfolding eventually_at_left_to_right filterlim_at_left_to_right DERIV_mirror
  1629   by (rule lhopital_right[where f'="\<lambda>x. - f' (- x)"]) (auto simp: DERIV_mirror)
  1630 
  1631 lemma lhopital:
  1632   "((f::real \<Rightarrow> real) ---> 0) (at x) \<Longrightarrow> (g ---> 0) (at x) \<Longrightarrow>
  1633     eventually (\<lambda>x. g x \<noteq> 0) (at x) \<Longrightarrow>
  1634     eventually (\<lambda>x. g' x \<noteq> 0) (at x) \<Longrightarrow>
  1635     eventually (\<lambda>x. DERIV f x :> f' x) (at x) \<Longrightarrow>
  1636     eventually (\<lambda>x. DERIV g x :> g' x) (at x) \<Longrightarrow>
  1637     ((\<lambda> x. (f' x / g' x)) ---> y) (at x) \<Longrightarrow>
  1638   ((\<lambda> x. f x / g x) ---> y) (at x)"
  1639   unfolding eventually_at_split filterlim_at_split
  1640   by (auto intro!: lhopital_right[of f x g g' f'] lhopital_left[of f x g g' f'])
  1641 
  1642 lemma lhopital_right_0_at_top:
  1643   fixes f g :: "real \<Rightarrow> real"
  1644   assumes g_0: "LIM x at_right 0. g x :> at_top"
  1645   assumes ev:
  1646     "eventually (\<lambda>x. g' x \<noteq> 0) (at_right 0)"
  1647     "eventually (\<lambda>x. DERIV f x :> f' x) (at_right 0)"
  1648     "eventually (\<lambda>x. DERIV g x :> g' x) (at_right 0)"
  1649   assumes lim: "((\<lambda> x. (f' x / g' x)) ---> x) (at_right 0)"
  1650   shows "((\<lambda> x. f x / g x) ---> x) (at_right 0)"
  1651   unfolding tendsto_iff
  1652 proof safe
  1653   fix e :: real assume "0 < e"
  1654 
  1655   with lim[unfolded tendsto_iff, rule_format, of "e / 4"]
  1656   have "eventually (\<lambda>t. dist (f' t / g' t) x < e / 4) (at_right 0)" by simp
  1657   from eventually_conj[OF eventually_conj[OF ev(1) ev(2)] eventually_conj[OF ev(3) this]]
  1658   obtain a where [arith]: "0 < a"
  1659     and g'_neq_0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> g' x \<noteq> 0"
  1660     and f0: "\<And>x. 0 < x \<Longrightarrow> x \<le> a \<Longrightarrow> DERIV f x :> (f' x)"
  1661     and g0: "\<And>x. 0 < x \<Longrightarrow> x \<le> a \<Longrightarrow> DERIV g x :> (g' x)"
  1662     and Df: "\<And>t. 0 < t \<Longrightarrow> t < a \<Longrightarrow> dist (f' t / g' t) x < e / 4"
  1663     unfolding eventually_at_le by (auto simp: dist_real_def)
  1664     
  1665 
  1666   from Df have
  1667     "eventually (\<lambda>t. t < a) (at_right 0)" "eventually (\<lambda>t::real. 0 < t) (at_right 0)"
  1668     unfolding eventually_at by (auto intro!: exI[of _ a] simp: dist_real_def)
  1669 
  1670   moreover
  1671   have "eventually (\<lambda>t. 0 < g t) (at_right 0)" "eventually (\<lambda>t. g a < g t) (at_right 0)"
  1672     using g_0 by (auto elim: eventually_elim1 simp: filterlim_at_top_dense)
  1673 
  1674   moreover
  1675   have inv_g: "((\<lambda>x. inverse (g x)) ---> 0) (at_right 0)"
  1676     using tendsto_inverse_0 filterlim_mono[OF g_0 at_top_le_at_infinity order_refl]
  1677     by (rule filterlim_compose)
  1678   then have "((\<lambda>x. norm (1 - g a * inverse (g x))) ---> norm (1 - g a * 0)) (at_right 0)"
  1679     by (intro tendsto_intros)
  1680   then have "((\<lambda>x. norm (1 - g a / g x)) ---> 1) (at_right 0)"
  1681     by (simp add: inverse_eq_divide)
  1682   from this[unfolded tendsto_iff, rule_format, of 1]
  1683   have "eventually (\<lambda>x. norm (1 - g a / g x) < 2) (at_right 0)"
  1684     by (auto elim!: eventually_elim1 simp: dist_real_def)
  1685 
  1686   moreover
  1687   from inv_g have "((\<lambda>t. norm ((f a - x * g a) * inverse (g t))) ---> norm ((f a - x * g a) * 0)) (at_right 0)"
  1688     by (intro tendsto_intros)
  1689   then have "((\<lambda>t. norm (f a - x * g a) / norm (g t)) ---> 0) (at_right 0)"
  1690     by (simp add: inverse_eq_divide)
  1691   from this[unfolded tendsto_iff, rule_format, of "e / 2"] `0 < e`
  1692   have "eventually (\<lambda>t. norm (f a - x * g a) / norm (g t) < e / 2) (at_right 0)"
  1693     by (auto simp: dist_real_def)
  1694 
  1695   ultimately show "eventually (\<lambda>t. dist (f t / g t) x < e) (at_right 0)"
  1696   proof eventually_elim
  1697     fix t assume t[arith]: "0 < t" "t < a" "g a < g t" "0 < g t"
  1698     assume ineq: "norm (1 - g a / g t) < 2" "norm (f a - x * g a) / norm (g t) < e / 2"
  1699 
  1700     have "\<exists>y. t < y \<and> y < a \<and> (g a - g t) * f' y = (f a - f t) * g' y"
  1701       using f0 g0 t(1,2) by (intro GMVT') (force intro!: DERIV_isCont)+
  1702     then obtain y where [arith]: "t < y" "y < a"
  1703       and D_eq0: "(g a - g t) * f' y = (f a - f t) * g' y"
  1704       by blast
  1705     from D_eq0 have D_eq: "(f t - f a) / (g t - g a) = f' y / g' y"
  1706       using `g a < g t` g'_neq_0[of y] by (auto simp add: field_simps)
  1707 
  1708     have *: "f t / g t - x = ((f t - f a) / (g t - g a) - x) * (1 - g a / g t) + (f a - x * g a) / g t"
  1709       by (simp add: field_simps)
  1710     have "norm (f t / g t - x) \<le>
  1711         norm (((f t - f a) / (g t - g a) - x) * (1 - g a / g t)) + norm ((f a - x * g a) / g t)"
  1712       unfolding * by (rule norm_triangle_ineq)
  1713     also have "\<dots> = dist (f' y / g' y) x * norm (1 - g a / g t) + norm (f a - x * g a) / norm (g t)"
  1714       by (simp add: abs_mult D_eq dist_real_def)
  1715     also have "\<dots> < (e / 4) * 2 + e / 2"
  1716       using ineq Df[of y] `0 < e` by (intro add_le_less_mono mult_mono) auto
  1717     finally show "dist (f t / g t) x < e"
  1718       by (simp add: dist_real_def)
  1719   qed
  1720 qed
  1721 
  1722 lemma lhopital_right_at_top:
  1723   "LIM x at_right x. (g::real \<Rightarrow> real) x :> at_top \<Longrightarrow>
  1724     eventually (\<lambda>x. g' x \<noteq> 0) (at_right x) \<Longrightarrow>
  1725     eventually (\<lambda>x. DERIV f x :> f' x) (at_right x) \<Longrightarrow>
  1726     eventually (\<lambda>x. DERIV g x :> g' x) (at_right x) \<Longrightarrow>
  1727     ((\<lambda> x. (f' x / g' x)) ---> y) (at_right x) \<Longrightarrow>
  1728     ((\<lambda> x. f x / g x) ---> y) (at_right x)"
  1729   unfolding eventually_at_right_to_0[of _ x] filterlim_at_right_to_0[of _ _ x] DERIV_shift
  1730   by (rule lhopital_right_0_at_top)
  1731 
  1732 lemma lhopital_left_at_top:
  1733   "LIM x at_left x. (g::real \<Rightarrow> real) x :> at_top \<Longrightarrow>
  1734     eventually (\<lambda>x. g' x \<noteq> 0) (at_left x) \<Longrightarrow>
  1735     eventually (\<lambda>x. DERIV f x :> f' x) (at_left x) \<Longrightarrow>
  1736     eventually (\<lambda>x. DERIV g x :> g' x) (at_left x) \<Longrightarrow>
  1737     ((\<lambda> x. (f' x / g' x)) ---> y) (at_left x) \<Longrightarrow>
  1738     ((\<lambda> x. f x / g x) ---> y) (at_left x)"
  1739   unfolding eventually_at_left_to_right filterlim_at_left_to_right DERIV_mirror
  1740   by (rule lhopital_right_at_top[where f'="\<lambda>x. - f' (- x)"]) (auto simp: DERIV_mirror)
  1741 
  1742 lemma lhopital_at_top:
  1743   "LIM x at x. (g::real \<Rightarrow> real) x :> at_top \<Longrightarrow>
  1744     eventually (\<lambda>x. g' x \<noteq> 0) (at x) \<Longrightarrow>
  1745     eventually (\<lambda>x. DERIV f x :> f' x) (at x) \<Longrightarrow>
  1746     eventually (\<lambda>x. DERIV g x :> g' x) (at x) \<Longrightarrow>
  1747     ((\<lambda> x. (f' x / g' x)) ---> y) (at x) \<Longrightarrow>
  1748     ((\<lambda> x. f x / g x) ---> y) (at x)"
  1749   unfolding eventually_at_split filterlim_at_split
  1750   by (auto intro!: lhopital_right_at_top[of g x g' f f'] lhopital_left_at_top[of g x g' f f'])
  1751 
  1752 lemma lhospital_at_top_at_top:
  1753   fixes f g :: "real \<Rightarrow> real"
  1754   assumes g_0: "LIM x at_top. g x :> at_top"
  1755   assumes g': "eventually (\<lambda>x. g' x \<noteq> 0) at_top"
  1756   assumes Df: "eventually (\<lambda>x. DERIV f x :> f' x) at_top"
  1757   assumes Dg: "eventually (\<lambda>x. DERIV g x :> g' x) at_top"
  1758   assumes lim: "((\<lambda> x. (f' x / g' x)) ---> x) at_top"
  1759   shows "((\<lambda> x. f x / g x) ---> x) at_top"
  1760   unfolding filterlim_at_top_to_right
  1761 proof (rule lhopital_right_0_at_top)
  1762   let ?F = "\<lambda>x. f (inverse x)"
  1763   let ?G = "\<lambda>x. g (inverse x)"
  1764   let ?R = "at_right (0::real)"
  1765   let ?D = "\<lambda>f' x. f' (inverse x) * - (inverse x ^ Suc (Suc 0))"
  1766 
  1767   show "LIM x ?R. ?G x :> at_top"
  1768     using g_0 unfolding filterlim_at_top_to_right .
  1769 
  1770   show "eventually (\<lambda>x. DERIV ?G x  :> ?D g' x) ?R"
  1771     unfolding eventually_at_right_to_top
  1772     using Dg eventually_ge_at_top[where c="1::real"]
  1773     apply eventually_elim
  1774     apply (rule DERIV_cong)
  1775     apply (rule DERIV_chain'[where f=inverse])
  1776     apply (auto intro!:  DERIV_inverse)
  1777     done
  1778 
  1779   show "eventually (\<lambda>x. DERIV ?F x  :> ?D f' x) ?R"
  1780     unfolding eventually_at_right_to_top
  1781     using Df eventually_ge_at_top[where c="1::real"]
  1782     apply eventually_elim
  1783     apply (rule DERIV_cong)
  1784     apply (rule DERIV_chain'[where f=inverse])
  1785     apply (auto intro!:  DERIV_inverse)
  1786     done
  1787 
  1788   show "eventually (\<lambda>x. ?D g' x \<noteq> 0) ?R"
  1789     unfolding eventually_at_right_to_top
  1790     using g' eventually_ge_at_top[where c="1::real"]
  1791     by eventually_elim auto
  1792     
  1793   show "((\<lambda>x. ?D f' x / ?D g' x) ---> x) ?R"
  1794     unfolding filterlim_at_right_to_top
  1795     apply (intro filterlim_cong[THEN iffD2, OF refl refl _ lim])
  1796     using eventually_ge_at_top[where c="1::real"]
  1797     by eventually_elim simp
  1798 qed
  1799 
  1800 end