src/HOL/Deriv.thy
 author hoelzl Fri Mar 22 10:41:43 2013 +0100 (2013-03-22) changeset 51481 ef949192e5d6 parent 51480 3793c3a11378 child 51526 155263089e7b permissions -rw-r--r--
move continuous_on_inv to HOL image (simplifies isCont_inverse_function)
```     1 (*  Title       : Deriv.thy
```
```     2     Author      : Jacques D. Fleuriot
```
```     3     Copyright   : 1998  University of Cambridge
```
```     4     Conversion to Isar and new proofs by Lawrence C Paulson, 2004
```
```     5     GMVT by Benjamin Porter, 2005
```
```     6 *)
```
```     7
```
```     8 header{* Differentiation *}
```
```     9
```
```    10 theory Deriv
```
```    11 imports Lim
```
```    12 begin
```
```    13
```
```    14 text{*Standard Definitions*}
```
```    15
```
```    16 definition
```
```    17   deriv :: "['a::real_normed_field \<Rightarrow> 'a, 'a, 'a] \<Rightarrow> bool"
```
```    18     --{*Differentiation: D is derivative of function f at x*}
```
```    19           ("(DERIV (_)/ (_)/ :> (_))" [1000, 1000, 60] 60) where
```
```    20   "DERIV f x :> D = ((%h. (f(x + h) - f x) / h) -- 0 --> D)"
```
```    21
```
```    22 subsection {* Derivatives *}
```
```    23
```
```    24 lemma DERIV_iff: "(DERIV f x :> D) = ((%h. (f(x + h) - f(x))/h) -- 0 --> D)"
```
```    25 by (simp add: deriv_def)
```
```    26
```
```    27 lemma DERIV_D: "DERIV f x :> D ==> (%h. (f(x + h) - f(x))/h) -- 0 --> D"
```
```    28 by (simp add: deriv_def)
```
```    29
```
```    30 lemma DERIV_const [simp]: "DERIV (\<lambda>x. k) x :> 0"
```
```    31   by (simp add: deriv_def tendsto_const)
```
```    32
```
```    33 lemma DERIV_ident [simp]: "DERIV (\<lambda>x. x) x :> 1"
```
```    34   by (simp add: deriv_def tendsto_const cong: LIM_cong)
```
```    35
```
```    36 lemma DERIV_add:
```
```    37   "\<lbrakk>DERIV f x :> D; DERIV g x :> E\<rbrakk> \<Longrightarrow> DERIV (\<lambda>x. f x + g x) x :> D + E"
```
```    38   by (simp only: deriv_def add_diff_add add_divide_distrib tendsto_add)
```
```    39
```
```    40 lemma DERIV_minus:
```
```    41   "DERIV f x :> D \<Longrightarrow> DERIV (\<lambda>x. - f x) x :> - D"
```
```    42   by (simp only: deriv_def minus_diff_minus divide_minus_left tendsto_minus)
```
```    43
```
```    44 lemma DERIV_diff:
```
```    45   "\<lbrakk>DERIV f x :> D; DERIV g x :> E\<rbrakk> \<Longrightarrow> DERIV (\<lambda>x. f x - g x) x :> D - E"
```
```    46 by (simp only: diff_minus DERIV_add DERIV_minus)
```
```    47
```
```    48 lemma DERIV_add_minus:
```
```    49   "\<lbrakk>DERIV f x :> D; DERIV g x :> E\<rbrakk> \<Longrightarrow> DERIV (\<lambda>x. f x + - g x) x :> D + - E"
```
```    50 by (simp only: DERIV_add DERIV_minus)
```
```    51
```
```    52 lemma DERIV_isCont: "DERIV f x :> D \<Longrightarrow> isCont f x"
```
```    53 proof (unfold isCont_iff)
```
```    54   assume "DERIV f x :> D"
```
```    55   hence "(\<lambda>h. (f(x+h) - f(x)) / h) -- 0 --> D"
```
```    56     by (rule DERIV_D)
```
```    57   hence "(\<lambda>h. (f(x+h) - f(x)) / h * h) -- 0 --> D * 0"
```
```    58     by (intro tendsto_mult tendsto_ident_at)
```
```    59   hence "(\<lambda>h. (f(x+h) - f(x)) * (h / h)) -- 0 --> 0"
```
```    60     by simp
```
```    61   hence "(\<lambda>h. f(x+h) - f(x)) -- 0 --> 0"
```
```    62     by (simp cong: LIM_cong)
```
```    63   thus "(\<lambda>h. f(x+h)) -- 0 --> f(x)"
```
```    64     by (simp add: LIM_def dist_norm)
```
```    65 qed
```
```    66
```
```    67 lemma DERIV_mult_lemma:
```
```    68   fixes a b c d :: "'a::real_field"
```
```    69   shows "(a * b - c * d) / h = a * ((b - d) / h) + ((a - c) / h) * d"
```
```    70   by (simp add: field_simps diff_divide_distrib)
```
```    71
```
```    72 lemma DERIV_mult':
```
```    73   assumes f: "DERIV f x :> D"
```
```    74   assumes g: "DERIV g x :> E"
```
```    75   shows "DERIV (\<lambda>x. f x * g x) x :> f x * E + D * g x"
```
```    76 proof (unfold deriv_def)
```
```    77   from f have "isCont f x"
```
```    78     by (rule DERIV_isCont)
```
```    79   hence "(\<lambda>h. f(x+h)) -- 0 --> f x"
```
```    80     by (simp only: isCont_iff)
```
```    81   hence "(\<lambda>h. f(x+h) * ((g(x+h) - g x) / h) +
```
```    82               ((f(x+h) - f x) / h) * g x)
```
```    83           -- 0 --> f x * E + D * g x"
```
```    84     by (intro tendsto_intros DERIV_D f g)
```
```    85   thus "(\<lambda>h. (f(x+h) * g(x+h) - f x * g x) / h)
```
```    86          -- 0 --> f x * E + D * g x"
```
```    87     by (simp only: DERIV_mult_lemma)
```
```    88 qed
```
```    89
```
```    90 lemma DERIV_mult:
```
```    91     "DERIV f x :> Da \<Longrightarrow> DERIV g x :> Db \<Longrightarrow> DERIV (\<lambda>x. f x * g x) x :> Da * g x + Db * f x"
```
```    92   by (drule (1) DERIV_mult', simp only: mult_commute add_commute)
```
```    93
```
```    94 lemma DERIV_unique:
```
```    95     "DERIV f x :> D \<Longrightarrow> DERIV f x :> E \<Longrightarrow> D = E"
```
```    96   unfolding deriv_def by (rule LIM_unique)
```
```    97
```
```    98 text{*Differentiation of finite sum*}
```
```    99
```
```   100 lemma DERIV_setsum:
```
```   101   assumes "finite S"
```
```   102   and "\<And> n. n \<in> S \<Longrightarrow> DERIV (%x. f x n) x :> (f' x n)"
```
```   103   shows "DERIV (%x. setsum (f x) S) x :> setsum (f' x) S"
```
```   104   using assms by induct (auto intro!: DERIV_add)
```
```   105
```
```   106 lemma DERIV_sumr [rule_format (no_asm)]:
```
```   107      "(\<forall>r. m \<le> r & r < (m + n) --> DERIV (%x. f r x) x :> (f' r x))
```
```   108       --> DERIV (%x. \<Sum>n=m..<n::nat. f n x :: real) x :> (\<Sum>r=m..<n. f' r x)"
```
```   109   by (auto intro: DERIV_setsum)
```
```   110
```
```   111 text{*Alternative definition for differentiability*}
```
```   112
```
```   113 lemma DERIV_LIM_iff:
```
```   114   fixes f :: "'a::{real_normed_vector,inverse} \<Rightarrow> 'a" shows
```
```   115      "((%h. (f(a + h) - f(a)) / h) -- 0 --> D) =
```
```   116       ((%x. (f(x)-f(a)) / (x-a)) -- a --> D)"
```
```   117 apply (rule iffI)
```
```   118 apply (drule_tac k="- a" in LIM_offset)
```
```   119 apply (simp add: diff_minus)
```
```   120 apply (drule_tac k="a" in LIM_offset)
```
```   121 apply (simp add: add_commute)
```
```   122 done
```
```   123
```
```   124 lemma DERIV_iff2: "(DERIV f x :> D) = ((%z. (f(z) - f(x)) / (z-x)) -- x --> D)"
```
```   125 by (simp add: deriv_def diff_minus [symmetric] DERIV_LIM_iff)
```
```   126
```
```   127 lemma DERIV_inverse_lemma:
```
```   128   "\<lbrakk>a \<noteq> 0; b \<noteq> (0::'a::real_normed_field)\<rbrakk>
```
```   129    \<Longrightarrow> (inverse a - inverse b) / h
```
```   130      = - (inverse a * ((a - b) / h) * inverse b)"
```
```   131 by (simp add: inverse_diff_inverse)
```
```   132
```
```   133 lemma DERIV_inverse':
```
```   134   assumes der: "DERIV f x :> D"
```
```   135   assumes neq: "f x \<noteq> 0"
```
```   136   shows "DERIV (\<lambda>x. inverse (f x)) x :> - (inverse (f x) * D * inverse (f x))"
```
```   137     (is "DERIV _ _ :> ?E")
```
```   138 proof (unfold DERIV_iff2)
```
```   139   from der have lim_f: "f -- x --> f x"
```
```   140     by (rule DERIV_isCont [unfolded isCont_def])
```
```   141
```
```   142   from neq have "0 < norm (f x)" by simp
```
```   143   with LIM_D [OF lim_f] obtain s
```
```   144     where s: "0 < s"
```
```   145     and less_fx: "\<And>z. \<lbrakk>z \<noteq> x; norm (z - x) < s\<rbrakk>
```
```   146                   \<Longrightarrow> norm (f z - f x) < norm (f x)"
```
```   147     by fast
```
```   148
```
```   149   show "(\<lambda>z. (inverse (f z) - inverse (f x)) / (z - x)) -- x --> ?E"
```
```   150   proof (rule LIM_equal2 [OF s])
```
```   151     fix z
```
```   152     assume "z \<noteq> x" "norm (z - x) < s"
```
```   153     hence "norm (f z - f x) < norm (f x)" by (rule less_fx)
```
```   154     hence "f z \<noteq> 0" by auto
```
```   155     thus "(inverse (f z) - inverse (f x)) / (z - x) =
```
```   156           - (inverse (f z) * ((f z - f x) / (z - x)) * inverse (f x))"
```
```   157       using neq by (rule DERIV_inverse_lemma)
```
```   158   next
```
```   159     from der have "(\<lambda>z. (f z - f x) / (z - x)) -- x --> D"
```
```   160       by (unfold DERIV_iff2)
```
```   161     thus "(\<lambda>z. - (inverse (f z) * ((f z - f x) / (z - x)) * inverse (f x)))
```
```   162           -- x --> ?E"
```
```   163       by (intro tendsto_intros lim_f neq)
```
```   164   qed
```
```   165 qed
```
```   166
```
```   167 lemma DERIV_divide:
```
```   168   "\<lbrakk>DERIV f x :> D; DERIV g x :> E; g x \<noteq> 0\<rbrakk>
```
```   169    \<Longrightarrow> DERIV (\<lambda>x. f x / g x) x :> (D * g x - f x * E) / (g x * g x)"
```
```   170 apply (subgoal_tac "f x * - (inverse (g x) * E * inverse (g x)) +
```
```   171           D * inverse (g x) = (D * g x - f x * E) / (g x * g x)")
```
```   172 apply (erule subst)
```
```   173 apply (unfold divide_inverse)
```
```   174 apply (erule DERIV_mult')
```
```   175 apply (erule (1) DERIV_inverse')
```
```   176 apply (simp add: ring_distribs nonzero_inverse_mult_distrib)
```
```   177 done
```
```   178
```
```   179 lemma DERIV_power_Suc:
```
```   180   fixes f :: "'a \<Rightarrow> 'a::{real_normed_field}"
```
```   181   assumes f: "DERIV f x :> D"
```
```   182   shows "DERIV (\<lambda>x. f x ^ Suc n) x :> (1 + of_nat n) * (D * f x ^ n)"
```
```   183 proof (induct n)
```
```   184 case 0
```
```   185   show ?case by (simp add: f)
```
```   186 case (Suc k)
```
```   187   from DERIV_mult' [OF f Suc] show ?case
```
```   188     apply (simp only: of_nat_Suc ring_distribs mult_1_left)
```
```   189     apply (simp only: power_Suc algebra_simps)
```
```   190     done
```
```   191 qed
```
```   192
```
```   193 lemma DERIV_power:
```
```   194   fixes f :: "'a \<Rightarrow> 'a::{real_normed_field}"
```
```   195   assumes f: "DERIV f x :> D"
```
```   196   shows "DERIV (\<lambda>x. f x ^ n) x :> of_nat n * (D * f x ^ (n - Suc 0))"
```
```   197 by (cases "n", simp, simp add: DERIV_power_Suc f del: power_Suc)
```
```   198
```
```   199 text {* Caratheodory formulation of derivative at a point *}
```
```   200
```
```   201 lemma CARAT_DERIV:
```
```   202      "(DERIV f x :> l) =
```
```   203       (\<exists>g. (\<forall>z. f z - f x = g z * (z-x)) & isCont g x & g x = l)"
```
```   204       (is "?lhs = ?rhs")
```
```   205 proof
```
```   206   assume der: "DERIV f x :> l"
```
```   207   show "\<exists>g. (\<forall>z. f z - f x = g z * (z-x)) \<and> isCont g x \<and> g x = l"
```
```   208   proof (intro exI conjI)
```
```   209     let ?g = "(%z. if z = x then l else (f z - f x) / (z-x))"
```
```   210     show "\<forall>z. f z - f x = ?g z * (z-x)" by simp
```
```   211     show "isCont ?g x" using der
```
```   212       by (simp add: isCont_iff DERIV_iff diff_minus
```
```   213                cong: LIM_equal [rule_format])
```
```   214     show "?g x = l" by simp
```
```   215   qed
```
```   216 next
```
```   217   assume "?rhs"
```
```   218   then obtain g where
```
```   219     "(\<forall>z. f z - f x = g z * (z-x))" and "isCont g x" and "g x = l" by blast
```
```   220   thus "(DERIV f x :> l)"
```
```   221      by (auto simp add: isCont_iff DERIV_iff cong: LIM_cong)
```
```   222 qed
```
```   223
```
```   224 lemma DERIV_chain':
```
```   225   assumes f: "DERIV f x :> D"
```
```   226   assumes g: "DERIV g (f x) :> E"
```
```   227   shows "DERIV (\<lambda>x. g (f x)) x :> E * D"
```
```   228 proof (unfold DERIV_iff2)
```
```   229   obtain d where d: "\<forall>y. g y - g (f x) = d y * (y - f x)"
```
```   230     and cont_d: "isCont d (f x)" and dfx: "d (f x) = E"
```
```   231     using CARAT_DERIV [THEN iffD1, OF g] by fast
```
```   232   from f have "f -- x --> f x"
```
```   233     by (rule DERIV_isCont [unfolded isCont_def])
```
```   234   with cont_d have "(\<lambda>z. d (f z)) -- x --> d (f x)"
```
```   235     by (rule isCont_tendsto_compose)
```
```   236   hence "(\<lambda>z. d (f z) * ((f z - f x) / (z - x)))
```
```   237           -- x --> d (f x) * D"
```
```   238     by (rule tendsto_mult [OF _ f [unfolded DERIV_iff2]])
```
```   239   thus "(\<lambda>z. (g (f z) - g (f x)) / (z - x)) -- x --> E * D"
```
```   240     by (simp add: d dfx)
```
```   241 qed
```
```   242
```
```   243 text {*
```
```   244  Let's do the standard proof, though theorem
```
```   245  @{text "LIM_mult2"} follows from a NS proof
```
```   246 *}
```
```   247
```
```   248 lemma DERIV_cmult:
```
```   249       "DERIV f x :> D ==> DERIV (%x. c * f x) x :> c*D"
```
```   250 by (drule DERIV_mult' [OF DERIV_const], simp)
```
```   251
```
```   252 lemma DERIV_cdivide: "DERIV f x :> D ==> DERIV (%x. f x / c) x :> D / c"
```
```   253   apply (subgoal_tac "DERIV (%x. (1 / c) * f x) x :> (1 / c) * D", force)
```
```   254   apply (erule DERIV_cmult)
```
```   255   done
```
```   256
```
```   257 text {* Standard version *}
```
```   258 lemma DERIV_chain: "[| DERIV f (g x) :> Da; DERIV g x :> Db |] ==> DERIV (f o g) x :> Da * Db"
```
```   259 by (drule (1) DERIV_chain', simp add: o_def mult_commute)
```
```   260
```
```   261 lemma DERIV_chain2: "[| DERIV f (g x) :> Da; DERIV g x :> Db |] ==> DERIV (%x. f (g x)) x :> Da * Db"
```
```   262 by (auto dest: DERIV_chain simp add: o_def)
```
```   263
```
```   264 text {* Derivative of linear multiplication *}
```
```   265 lemma DERIV_cmult_Id [simp]: "DERIV (op * c) x :> c"
```
```   266 by (cut_tac c = c and x = x in DERIV_ident [THEN DERIV_cmult], simp)
```
```   267
```
```   268 lemma DERIV_pow: "DERIV (%x. x ^ n) x :> real n * (x ^ (n - Suc 0))"
```
```   269 apply (cut_tac DERIV_power [OF DERIV_ident])
```
```   270 apply (simp add: real_of_nat_def)
```
```   271 done
```
```   272
```
```   273 text {* Power of @{text "-1"} *}
```
```   274
```
```   275 lemma DERIV_inverse:
```
```   276   fixes x :: "'a::{real_normed_field}"
```
```   277   shows "x \<noteq> 0 ==> DERIV (%x. inverse(x)) x :> (-(inverse x ^ Suc (Suc 0)))"
```
```   278 by (drule DERIV_inverse' [OF DERIV_ident]) simp
```
```   279
```
```   280 text {* Derivative of inverse *}
```
```   281 lemma DERIV_inverse_fun:
```
```   282   fixes x :: "'a::{real_normed_field}"
```
```   283   shows "[| DERIV f x :> d; f(x) \<noteq> 0 |]
```
```   284       ==> DERIV (%x. inverse(f x)) x :> (- (d * inverse(f(x) ^ Suc (Suc 0))))"
```
```   285 by (drule (1) DERIV_inverse') (simp add: mult_ac nonzero_inverse_mult_distrib)
```
```   286
```
```   287 text {* Derivative of quotient *}
```
```   288 lemma DERIV_quotient:
```
```   289   fixes x :: "'a::{real_normed_field}"
```
```   290   shows "[| DERIV f x :> d; DERIV g x :> e; g(x) \<noteq> 0 |]
```
```   291        ==> DERIV (%y. f(y) / (g y)) x :> (d*g(x) - (e*f(x))) / (g(x) ^ Suc (Suc 0))"
```
```   292 by (drule (2) DERIV_divide) (simp add: mult_commute)
```
```   293
```
```   294 text {* @{text "DERIV_intros"} *}
```
```   295 ML {*
```
```   296 structure Deriv_Intros = Named_Thms
```
```   297 (
```
```   298   val name = @{binding DERIV_intros}
```
```   299   val description = "DERIV introduction rules"
```
```   300 )
```
```   301 *}
```
```   302
```
```   303 setup Deriv_Intros.setup
```
```   304
```
```   305 lemma DERIV_cong: "\<lbrakk> DERIV f x :> X ; X = Y \<rbrakk> \<Longrightarrow> DERIV f x :> Y"
```
```   306   by simp
```
```   307
```
```   308 declare
```
```   309   DERIV_const[THEN DERIV_cong, DERIV_intros]
```
```   310   DERIV_ident[THEN DERIV_cong, DERIV_intros]
```
```   311   DERIV_add[THEN DERIV_cong, DERIV_intros]
```
```   312   DERIV_minus[THEN DERIV_cong, DERIV_intros]
```
```   313   DERIV_mult[THEN DERIV_cong, DERIV_intros]
```
```   314   DERIV_diff[THEN DERIV_cong, DERIV_intros]
```
```   315   DERIV_inverse'[THEN DERIV_cong, DERIV_intros]
```
```   316   DERIV_divide[THEN DERIV_cong, DERIV_intros]
```
```   317   DERIV_power[where 'a=real, THEN DERIV_cong,
```
```   318               unfolded real_of_nat_def[symmetric], DERIV_intros]
```
```   319   DERIV_setsum[THEN DERIV_cong, DERIV_intros]
```
```   320
```
```   321
```
```   322 subsection {* Differentiability predicate *}
```
```   323
```
```   324 definition
```
```   325   differentiable :: "['a::real_normed_field \<Rightarrow> 'a, 'a] \<Rightarrow> bool"
```
```   326     (infixl "differentiable" 60) where
```
```   327   "f differentiable x = (\<exists>D. DERIV f x :> D)"
```
```   328
```
```   329 lemma differentiableE [elim?]:
```
```   330   assumes "f differentiable x"
```
```   331   obtains df where "DERIV f x :> df"
```
```   332   using assms unfolding differentiable_def ..
```
```   333
```
```   334 lemma differentiableD: "f differentiable x ==> \<exists>D. DERIV f x :> D"
```
```   335 by (simp add: differentiable_def)
```
```   336
```
```   337 lemma differentiableI: "DERIV f x :> D ==> f differentiable x"
```
```   338 by (force simp add: differentiable_def)
```
```   339
```
```   340 lemma differentiable_ident [simp]: "(\<lambda>x. x) differentiable x"
```
```   341   by (rule DERIV_ident [THEN differentiableI])
```
```   342
```
```   343 lemma differentiable_const [simp]: "(\<lambda>z. a) differentiable x"
```
```   344   by (rule DERIV_const [THEN differentiableI])
```
```   345
```
```   346 lemma differentiable_compose:
```
```   347   assumes f: "f differentiable (g x)"
```
```   348   assumes g: "g differentiable x"
```
```   349   shows "(\<lambda>x. f (g x)) differentiable x"
```
```   350 proof -
```
```   351   from `f differentiable (g x)` obtain df where "DERIV f (g x) :> df" ..
```
```   352   moreover
```
```   353   from `g differentiable x` obtain dg where "DERIV g x :> dg" ..
```
```   354   ultimately
```
```   355   have "DERIV (\<lambda>x. f (g x)) x :> df * dg" by (rule DERIV_chain2)
```
```   356   thus ?thesis by (rule differentiableI)
```
```   357 qed
```
```   358
```
```   359 lemma differentiable_sum [simp]:
```
```   360   assumes "f differentiable x"
```
```   361   and "g differentiable x"
```
```   362   shows "(\<lambda>x. f x + g x) differentiable x"
```
```   363 proof -
```
```   364   from `f differentiable x` obtain df where "DERIV f x :> df" ..
```
```   365   moreover
```
```   366   from `g differentiable x` obtain dg where "DERIV g x :> dg" ..
```
```   367   ultimately
```
```   368   have "DERIV (\<lambda>x. f x + g x) x :> df + dg" by (rule DERIV_add)
```
```   369   thus ?thesis by (rule differentiableI)
```
```   370 qed
```
```   371
```
```   372 lemma differentiable_minus [simp]:
```
```   373   assumes "f differentiable x"
```
```   374   shows "(\<lambda>x. - f x) differentiable x"
```
```   375 proof -
```
```   376   from `f differentiable x` obtain df where "DERIV f x :> df" ..
```
```   377   hence "DERIV (\<lambda>x. - f x) x :> - df" by (rule DERIV_minus)
```
```   378   thus ?thesis by (rule differentiableI)
```
```   379 qed
```
```   380
```
```   381 lemma differentiable_diff [simp]:
```
```   382   assumes "f differentiable x"
```
```   383   assumes "g differentiable x"
```
```   384   shows "(\<lambda>x. f x - g x) differentiable x"
```
```   385   unfolding diff_minus using assms by simp
```
```   386
```
```   387 lemma differentiable_mult [simp]:
```
```   388   assumes "f differentiable x"
```
```   389   assumes "g differentiable x"
```
```   390   shows "(\<lambda>x. f x * g x) differentiable x"
```
```   391 proof -
```
```   392   from `f differentiable x` obtain df where "DERIV f x :> df" ..
```
```   393   moreover
```
```   394   from `g differentiable x` obtain dg where "DERIV g x :> dg" ..
```
```   395   ultimately
```
```   396   have "DERIV (\<lambda>x. f x * g x) x :> df * g x + dg * f x" by (rule DERIV_mult)
```
```   397   thus ?thesis by (rule differentiableI)
```
```   398 qed
```
```   399
```
```   400 lemma differentiable_inverse [simp]:
```
```   401   assumes "f differentiable x" and "f x \<noteq> 0"
```
```   402   shows "(\<lambda>x. inverse (f x)) differentiable x"
```
```   403 proof -
```
```   404   from `f differentiable x` obtain df where "DERIV f x :> df" ..
```
```   405   hence "DERIV (\<lambda>x. inverse (f x)) x :> - (inverse (f x) * df * inverse (f x))"
```
```   406     using `f x \<noteq> 0` by (rule DERIV_inverse')
```
```   407   thus ?thesis by (rule differentiableI)
```
```   408 qed
```
```   409
```
```   410 lemma differentiable_divide [simp]:
```
```   411   assumes "f differentiable x"
```
```   412   assumes "g differentiable x" and "g x \<noteq> 0"
```
```   413   shows "(\<lambda>x. f x / g x) differentiable x"
```
```   414   unfolding divide_inverse using assms by simp
```
```   415
```
```   416 lemma differentiable_power [simp]:
```
```   417   fixes f :: "'a::{real_normed_field} \<Rightarrow> 'a"
```
```   418   assumes "f differentiable x"
```
```   419   shows "(\<lambda>x. f x ^ n) differentiable x"
```
```   420   apply (induct n)
```
```   421   apply simp
```
```   422   apply (simp add: assms)
```
```   423   done
```
```   424
```
```   425
```
```   426 subsection {* Nested Intervals and Bisection *}
```
```   427
```
```   428 lemma nested_sequence_unique:
```
```   429   assumes "\<forall>n. f n \<le> f (Suc n)" "\<forall>n. g (Suc n) \<le> g n" "\<forall>n. f n \<le> g n" "(\<lambda>n. f n - g n) ----> 0"
```
```   430   shows "\<exists>l::real. ((\<forall>n. f n \<le> l) \<and> f ----> l) \<and> ((\<forall>n. l \<le> g n) \<and> g ----> l)"
```
```   431 proof -
```
```   432   have "incseq f" unfolding incseq_Suc_iff by fact
```
```   433   have "decseq g" unfolding decseq_Suc_iff by fact
```
```   434
```
```   435   { fix n
```
```   436     from `decseq g` have "g n \<le> g 0" by (rule decseqD) simp
```
```   437     with `\<forall>n. f n \<le> g n`[THEN spec, of n] have "f n \<le> g 0" by auto }
```
```   438   then obtain u where "f ----> u" "\<forall>i. f i \<le> u"
```
```   439     using incseq_convergent[OF `incseq f`] by auto
```
```   440   moreover
```
```   441   { fix n
```
```   442     from `incseq f` have "f 0 \<le> f n" by (rule incseqD) simp
```
```   443     with `\<forall>n. f n \<le> g n`[THEN spec, of n] have "f 0 \<le> g n" by simp }
```
```   444   then obtain l where "g ----> l" "\<forall>i. l \<le> g i"
```
```   445     using decseq_convergent[OF `decseq g`] by auto
```
```   446   moreover note LIMSEQ_unique[OF assms(4) tendsto_diff[OF `f ----> u` `g ----> l`]]
```
```   447   ultimately show ?thesis by auto
```
```   448 qed
```
```   449
```
```   450 lemma Bolzano[consumes 1, case_names trans local]:
```
```   451   fixes P :: "real \<Rightarrow> real \<Rightarrow> bool"
```
```   452   assumes [arith]: "a \<le> b"
```
```   453   assumes trans: "\<And>a b c. \<lbrakk>P a b; P b c; a \<le> b; b \<le> c\<rbrakk> \<Longrightarrow> P a c"
```
```   454   assumes local: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> \<exists>d>0. \<forall>a b. a \<le> x \<and> x \<le> b \<and> b - a < d \<longrightarrow> P a b"
```
```   455   shows "P a b"
```
```   456 proof -
```
```   457   def bisect \<equiv> "nat_rec (a, b) (\<lambda>n (x, y). if P x ((x+y) / 2) then ((x+y)/2, y) else (x, (x+y)/2))"
```
```   458   def l \<equiv> "\<lambda>n. fst (bisect n)" and u \<equiv> "\<lambda>n. snd (bisect n)"
```
```   459   have l[simp]: "l 0 = a" "\<And>n. l (Suc n) = (if P (l n) ((l n + u n) / 2) then (l n + u n) / 2 else l n)"
```
```   460     and u[simp]: "u 0 = b" "\<And>n. u (Suc n) = (if P (l n) ((l n + u n) / 2) then u n else (l n + u n) / 2)"
```
```   461     by (simp_all add: l_def u_def bisect_def split: prod.split)
```
```   462
```
```   463   { fix n have "l n \<le> u n" by (induct n) auto } note this[simp]
```
```   464
```
```   465   have "\<exists>x. ((\<forall>n. l n \<le> x) \<and> l ----> x) \<and> ((\<forall>n. x \<le> u n) \<and> u ----> x)"
```
```   466   proof (safe intro!: nested_sequence_unique)
```
```   467     fix n show "l n \<le> l (Suc n)" "u (Suc n) \<le> u n" by (induct n) auto
```
```   468   next
```
```   469     { fix n have "l n - u n = (a - b) / 2^n" by (induct n) (auto simp: field_simps) }
```
```   470     then show "(\<lambda>n. l n - u n) ----> 0" by (simp add: LIMSEQ_divide_realpow_zero)
```
```   471   qed fact
```
```   472   then obtain x where x: "\<And>n. l n \<le> x" "\<And>n. x \<le> u n" and "l ----> x" "u ----> x" by auto
```
```   473   obtain d where "0 < d" and d: "\<And>a b. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> b - a < d \<Longrightarrow> P a b"
```
```   474     using `l 0 \<le> x` `x \<le> u 0` local[of x] by auto
```
```   475
```
```   476   show "P a b"
```
```   477   proof (rule ccontr)
```
```   478     assume "\<not> P a b"
```
```   479     { fix n have "\<not> P (l n) (u n)"
```
```   480       proof (induct n)
```
```   481         case (Suc n) with trans[of "l n" "(l n + u n) / 2" "u n"] show ?case by auto
```
```   482       qed (simp add: `\<not> P a b`) }
```
```   483     moreover
```
```   484     { have "eventually (\<lambda>n. x - d / 2 < l n) sequentially"
```
```   485         using `0 < d` `l ----> x` by (intro order_tendstoD[of _ x]) auto
```
```   486       moreover have "eventually (\<lambda>n. u n < x + d / 2) sequentially"
```
```   487         using `0 < d` `u ----> x` by (intro order_tendstoD[of _ x]) auto
```
```   488       ultimately have "eventually (\<lambda>n. P (l n) (u n)) sequentially"
```
```   489       proof eventually_elim
```
```   490         fix n assume "x - d / 2 < l n" "u n < x + d / 2"
```
```   491         from add_strict_mono[OF this] have "u n - l n < d" by simp
```
```   492         with x show "P (l n) (u n)" by (rule d)
```
```   493       qed }
```
```   494     ultimately show False by simp
```
```   495   qed
```
```   496 qed
```
```   497
```
```   498 (*HOL style here: object-level formulations*)
```
```   499 lemma IVT_objl: "(f(a::real) \<le> (y::real) & y \<le> f(b) & a \<le> b &
```
```   500       (\<forall>x. a \<le> x & x \<le> b --> isCont f x))
```
```   501       --> (\<exists>x. a \<le> x & x \<le> b & f(x) = y)"
```
```   502 apply (blast intro: IVT)
```
```   503 done
```
```   504
```
```   505 lemma IVT2_objl: "(f(b::real) \<le> (y::real) & y \<le> f(a) & a \<le> b &
```
```   506       (\<forall>x. a \<le> x & x \<le> b --> isCont f x))
```
```   507       --> (\<exists>x. a \<le> x & x \<le> b & f(x) = y)"
```
```   508 apply (blast intro: IVT2)
```
```   509 done
```
```   510
```
```   511
```
```   512 lemma compact_Icc[simp, intro]: "compact {a .. b::real}"
```
```   513 proof (cases "a \<le> b", rule compactI)
```
```   514   fix C assume C: "a \<le> b" "\<forall>t\<in>C. open t" "{a..b} \<subseteq> \<Union>C"
```
```   515   def T == "{a .. b}"
```
```   516   from C(1,3) show "\<exists>C'\<subseteq>C. finite C' \<and> {a..b} \<subseteq> \<Union>C'"
```
```   517   proof (induct rule: Bolzano)
```
```   518     case (trans a b c)
```
```   519     then have *: "{a .. c} = {a .. b} \<union> {b .. c}" by auto
```
```   520     from trans obtain C1 C2 where "C1\<subseteq>C \<and> finite C1 \<and> {a..b} \<subseteq> \<Union>C1" "C2\<subseteq>C \<and> finite C2 \<and> {b..c} \<subseteq> \<Union>C2"
```
```   521       by (auto simp: *)
```
```   522     with trans show ?case
```
```   523       unfolding * by (intro exI[of _ "C1 \<union> C2"]) auto
```
```   524   next
```
```   525     case (local x)
```
```   526     then have "x \<in> \<Union>C" using C by auto
```
```   527     with C(2) obtain c where "x \<in> c" "open c" "c \<in> C" by auto
```
```   528     then obtain e where "0 < e" "{x - e <..< x + e} \<subseteq> c"
```
```   529       by (auto simp: open_real_def dist_real_def subset_eq Ball_def abs_less_iff)
```
```   530     with `c \<in> C` show ?case
```
```   531       by (safe intro!: exI[of _ "e/2"] exI[of _ "{c}"]) auto
```
```   532   qed
```
```   533 qed simp
```
```   534
```
```   535 subsection {* Boundedness of continuous functions *}
```
```   536
```
```   537 text{*By bisection, function continuous on closed interval is bounded above*}
```
```   538
```
```   539 lemma isCont_eq_Ub:
```
```   540   fixes f :: "real \<Rightarrow> 'a::linorder_topology"
```
```   541   shows "a \<le> b \<Longrightarrow> \<forall>x::real. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x \<Longrightarrow>
```
```   542     \<exists>M. (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M) \<and> (\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = M)"
```
```   543   using continuous_attains_sup[of "{a .. b}" f]
```
```   544   apply (simp add: continuous_at_imp_continuous_on Ball_def)
```
```   545   apply safe
```
```   546   apply (rule_tac x="f x" in exI)
```
```   547   apply auto
```
```   548   done
```
```   549
```
```   550 lemma isCont_eq_Lb:
```
```   551   fixes f :: "real \<Rightarrow> 'a::linorder_topology"
```
```   552   shows "a \<le> b \<Longrightarrow> \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x \<Longrightarrow>
```
```   553     \<exists>M. (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> M \<le> f x) \<and> (\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = M)"
```
```   554   using continuous_attains_inf[of "{a .. b}" f]
```
```   555   apply (simp add: continuous_at_imp_continuous_on Ball_def)
```
```   556   apply safe
```
```   557   apply (rule_tac x="f x" in exI)
```
```   558   apply auto
```
```   559   done
```
```   560
```
```   561 lemma isCont_bounded:
```
```   562   fixes f :: "real \<Rightarrow> 'a::linorder_topology"
```
```   563   shows "a \<le> b \<Longrightarrow> \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x \<Longrightarrow> \<exists>M. \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M"
```
```   564   using isCont_eq_Ub[of a b f] by auto
```
```   565
```
```   566 lemma isCont_has_Ub:
```
```   567   fixes f :: "real \<Rightarrow> 'a::linorder_topology"
```
```   568   shows "a \<le> b \<Longrightarrow> \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x \<Longrightarrow>
```
```   569     \<exists>M. (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M) \<and> (\<forall>N. N < M \<longrightarrow> (\<exists>x. a \<le> x \<and> x \<le> b \<and> N < f x))"
```
```   570   using isCont_eq_Ub[of a b f] by auto
```
```   571
```
```   572 text{*Refine the above to existence of least upper bound*}
```
```   573
```
```   574 lemma lemma_reals_complete: "((\<exists>x. x \<in> S) & (\<exists>y. isUb UNIV S (y::real))) -->
```
```   575       (\<exists>t. isLub UNIV S t)"
```
```   576 by (blast intro: reals_complete)
```
```   577
```
```   578
```
```   579 text{*Another version.*}
```
```   580
```
```   581 lemma isCont_Lb_Ub: "[|a \<le> b; \<forall>x. a \<le> x & x \<le> b --> isCont f x |]
```
```   582       ==> \<exists>L M::real. (\<forall>x::real. a \<le> x & x \<le> b --> L \<le> f(x) & f(x) \<le> M) &
```
```   583           (\<forall>y. L \<le> y & y \<le> M --> (\<exists>x. a \<le> x & x \<le> b & (f(x) = y)))"
```
```   584 apply (frule isCont_eq_Lb)
```
```   585 apply (frule_tac [2] isCont_eq_Ub)
```
```   586 apply (assumption+, safe)
```
```   587 apply (rule_tac x = "f x" in exI)
```
```   588 apply (rule_tac x = "f xa" in exI, simp, safe)
```
```   589 apply (cut_tac x = x and y = xa in linorder_linear, safe)
```
```   590 apply (cut_tac f = f and a = x and b = xa and y = y in IVT_objl)
```
```   591 apply (cut_tac [2] f = f and a = xa and b = x and y = y in IVT2_objl, safe)
```
```   592 apply (rule_tac [2] x = xb in exI)
```
```   593 apply (rule_tac [4] x = xb in exI, simp_all)
```
```   594 done
```
```   595
```
```   596
```
```   597 subsection {* Local extrema *}
```
```   598
```
```   599 text{*If @{term "0 < f'(x)"} then @{term x} is Locally Strictly Increasing At The Right*}
```
```   600
```
```   601 lemma DERIV_pos_inc_right:
```
```   602   fixes f :: "real => real"
```
```   603   assumes der: "DERIV f x :> l"
```
```   604       and l:   "0 < l"
```
```   605   shows "\<exists>d > 0. \<forall>h > 0. h < d --> f(x) < f(x + h)"
```
```   606 proof -
```
```   607   from l der [THEN DERIV_D, THEN LIM_D [where r = "l"]]
```
```   608   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)"
```
```   609     by (simp add: diff_minus)
```
```   610   then obtain s
```
```   611         where s:   "0 < s"
```
```   612           and all: "!!z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < l"
```
```   613     by auto
```
```   614   thus ?thesis
```
```   615   proof (intro exI conjI strip)
```
```   616     show "0<s" using s .
```
```   617     fix h::real
```
```   618     assume "0 < h" "h < s"
```
```   619     with all [of h] show "f x < f (x+h)"
```
```   620     proof (simp add: abs_if pos_less_divide_eq diff_minus [symmetric]
```
```   621     split add: split_if_asm)
```
```   622       assume "~ (f (x+h) - f x) / h < l" and h: "0 < h"
```
```   623       with l
```
```   624       have "0 < (f (x+h) - f x) / h" by arith
```
```   625       thus "f x < f (x+h)"
```
```   626   by (simp add: pos_less_divide_eq h)
```
```   627     qed
```
```   628   qed
```
```   629 qed
```
```   630
```
```   631 lemma DERIV_neg_dec_left:
```
```   632   fixes f :: "real => real"
```
```   633   assumes der: "DERIV f x :> l"
```
```   634       and l:   "l < 0"
```
```   635   shows "\<exists>d > 0. \<forall>h > 0. h < d --> f(x) < f(x-h)"
```
```   636 proof -
```
```   637   from l der [THEN DERIV_D, THEN LIM_D [where r = "-l"]]
```
```   638   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)"
```
```   639     by (simp add: diff_minus)
```
```   640   then obtain s
```
```   641         where s:   "0 < s"
```
```   642           and all: "!!z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < -l"
```
```   643     by auto
```
```   644   thus ?thesis
```
```   645   proof (intro exI conjI strip)
```
```   646     show "0<s" using s .
```
```   647     fix h::real
```
```   648     assume "0 < h" "h < s"
```
```   649     with all [of "-h"] show "f x < f (x-h)"
```
```   650     proof (simp add: abs_if pos_less_divide_eq diff_minus [symmetric]
```
```   651     split add: split_if_asm)
```
```   652       assume " - ((f (x-h) - f x) / h) < l" and h: "0 < h"
```
```   653       with l
```
```   654       have "0 < (f (x-h) - f x) / h" by arith
```
```   655       thus "f x < f (x-h)"
```
```   656   by (simp add: pos_less_divide_eq h)
```
```   657     qed
```
```   658   qed
```
```   659 qed
```
```   660
```
```   661
```
```   662 lemma DERIV_pos_inc_left:
```
```   663   fixes f :: "real => real"
```
```   664   shows "DERIV f x :> l \<Longrightarrow> 0 < l \<Longrightarrow> \<exists>d > 0. \<forall>h > 0. h < d --> f(x - h) < f(x)"
```
```   665   apply (rule DERIV_neg_dec_left [of "%x. - f x" x "-l", simplified])
```
```   666   apply (auto simp add: DERIV_minus)
```
```   667   done
```
```   668
```
```   669 lemma DERIV_neg_dec_right:
```
```   670   fixes f :: "real => real"
```
```   671   shows "DERIV f x :> l \<Longrightarrow> l < 0 \<Longrightarrow> \<exists>d > 0. \<forall>h > 0. h < d --> f(x) > f(x + h)"
```
```   672   apply (rule DERIV_pos_inc_right [of "%x. - f x" x "-l", simplified])
```
```   673   apply (auto simp add: DERIV_minus)
```
```   674   done
```
```   675
```
```   676 lemma DERIV_local_max:
```
```   677   fixes f :: "real => real"
```
```   678   assumes der: "DERIV f x :> l"
```
```   679       and d:   "0 < d"
```
```   680       and le:  "\<forall>y. \<bar>x-y\<bar> < d --> f(y) \<le> f(x)"
```
```   681   shows "l = 0"
```
```   682 proof (cases rule: linorder_cases [of l 0])
```
```   683   case equal thus ?thesis .
```
```   684 next
```
```   685   case less
```
```   686   from DERIV_neg_dec_left [OF der less]
```
```   687   obtain d' where d': "0 < d'"
```
```   688              and lt: "\<forall>h > 0. h < d' \<longrightarrow> f x < f (x-h)" by blast
```
```   689   from real_lbound_gt_zero [OF d d']
```
```   690   obtain e where "0 < e \<and> e < d \<and> e < d'" ..
```
```   691   with lt le [THEN spec [where x="x-e"]]
```
```   692   show ?thesis by (auto simp add: abs_if)
```
```   693 next
```
```   694   case greater
```
```   695   from DERIV_pos_inc_right [OF der greater]
```
```   696   obtain d' where d': "0 < d'"
```
```   697              and lt: "\<forall>h > 0. h < d' \<longrightarrow> f x < f (x + h)" by blast
```
```   698   from real_lbound_gt_zero [OF d d']
```
```   699   obtain e where "0 < e \<and> e < d \<and> e < d'" ..
```
```   700   with lt le [THEN spec [where x="x+e"]]
```
```   701   show ?thesis by (auto simp add: abs_if)
```
```   702 qed
```
```   703
```
```   704
```
```   705 text{*Similar theorem for a local minimum*}
```
```   706 lemma DERIV_local_min:
```
```   707   fixes f :: "real => real"
```
```   708   shows "[| DERIV f x :> l; 0 < d; \<forall>y. \<bar>x-y\<bar> < d --> f(x) \<le> f(y) |] ==> l = 0"
```
```   709 by (drule DERIV_minus [THEN DERIV_local_max], auto)
```
```   710
```
```   711
```
```   712 text{*In particular, if a function is locally flat*}
```
```   713 lemma DERIV_local_const:
```
```   714   fixes f :: "real => real"
```
```   715   shows "[| DERIV f x :> l; 0 < d; \<forall>y. \<bar>x-y\<bar> < d --> f(x) = f(y) |] ==> l = 0"
```
```   716 by (auto dest!: DERIV_local_max)
```
```   717
```
```   718
```
```   719 subsection {* Rolle's Theorem *}
```
```   720
```
```   721 text{*Lemma about introducing open ball in open interval*}
```
```   722 lemma lemma_interval_lt:
```
```   723      "[| a < x;  x < b |]
```
```   724       ==> \<exists>d::real. 0 < d & (\<forall>y. \<bar>x-y\<bar> < d --> a < y & y < b)"
```
```   725
```
```   726 apply (simp add: abs_less_iff)
```
```   727 apply (insert linorder_linear [of "x-a" "b-x"], safe)
```
```   728 apply (rule_tac x = "x-a" in exI)
```
```   729 apply (rule_tac [2] x = "b-x" in exI, auto)
```
```   730 done
```
```   731
```
```   732 lemma lemma_interval: "[| a < x;  x < b |] ==>
```
```   733         \<exists>d::real. 0 < d &  (\<forall>y. \<bar>x-y\<bar> < d --> a \<le> y & y \<le> b)"
```
```   734 apply (drule lemma_interval_lt, auto)
```
```   735 apply force
```
```   736 done
```
```   737
```
```   738 text{*Rolle's Theorem.
```
```   739    If @{term f} is defined and continuous on the closed interval
```
```   740    @{text "[a,b]"} and differentiable on the open interval @{text "(a,b)"},
```
```   741    and @{term "f(a) = f(b)"},
```
```   742    then there exists @{text "x0 \<in> (a,b)"} such that @{term "f'(x0) = 0"}*}
```
```   743 theorem Rolle:
```
```   744   assumes lt: "a < b"
```
```   745       and eq: "f(a) = f(b)"
```
```   746       and con: "\<forall>x. a \<le> x & x \<le> b --> isCont f x"
```
```   747       and dif [rule_format]: "\<forall>x. a < x & x < b --> f differentiable x"
```
```   748   shows "\<exists>z::real. a < z & z < b & DERIV f z :> 0"
```
```   749 proof -
```
```   750   have le: "a \<le> b" using lt by simp
```
```   751   from isCont_eq_Ub [OF le con]
```
```   752   obtain x where x_max: "\<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> f z \<le> f x"
```
```   753              and alex: "a \<le> x" and xleb: "x \<le> b"
```
```   754     by blast
```
```   755   from isCont_eq_Lb [OF le con]
```
```   756   obtain x' where x'_min: "\<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> f x' \<le> f z"
```
```   757               and alex': "a \<le> x'" and x'leb: "x' \<le> b"
```
```   758     by blast
```
```   759   show ?thesis
```
```   760   proof cases
```
```   761     assume axb: "a < x & x < b"
```
```   762         --{*@{term f} attains its maximum within the interval*}
```
```   763     hence ax: "a<x" and xb: "x<b" by arith +
```
```   764     from lemma_interval [OF ax xb]
```
```   765     obtain d where d: "0<d" and bound: "\<forall>y. \<bar>x-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
```
```   766       by blast
```
```   767     hence bound': "\<forall>y. \<bar>x-y\<bar> < d \<longrightarrow> f y \<le> f x" using x_max
```
```   768       by blast
```
```   769     from differentiableD [OF dif [OF axb]]
```
```   770     obtain l where der: "DERIV f x :> l" ..
```
```   771     have "l=0" by (rule DERIV_local_max [OF der d bound'])
```
```   772         --{*the derivative at a local maximum is zero*}
```
```   773     thus ?thesis using ax xb der by auto
```
```   774   next
```
```   775     assume notaxb: "~ (a < x & x < b)"
```
```   776     hence xeqab: "x=a | x=b" using alex xleb by arith
```
```   777     hence fb_eq_fx: "f b = f x" by (auto simp add: eq)
```
```   778     show ?thesis
```
```   779     proof cases
```
```   780       assume ax'b: "a < x' & x' < b"
```
```   781         --{*@{term f} attains its minimum within the interval*}
```
```   782       hence ax': "a<x'" and x'b: "x'<b" by arith+
```
```   783       from lemma_interval [OF ax' x'b]
```
```   784       obtain d where d: "0<d" and bound: "\<forall>y. \<bar>x'-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
```
```   785   by blast
```
```   786       hence bound': "\<forall>y. \<bar>x'-y\<bar> < d \<longrightarrow> f x' \<le> f y" using x'_min
```
```   787   by blast
```
```   788       from differentiableD [OF dif [OF ax'b]]
```
```   789       obtain l where der: "DERIV f x' :> l" ..
```
```   790       have "l=0" by (rule DERIV_local_min [OF der d bound'])
```
```   791         --{*the derivative at a local minimum is zero*}
```
```   792       thus ?thesis using ax' x'b der by auto
```
```   793     next
```
```   794       assume notax'b: "~ (a < x' & x' < b)"
```
```   795         --{*@{term f} is constant througout the interval*}
```
```   796       hence x'eqab: "x'=a | x'=b" using alex' x'leb by arith
```
```   797       hence fb_eq_fx': "f b = f x'" by (auto simp add: eq)
```
```   798       from dense [OF lt]
```
```   799       obtain r where ar: "a < r" and rb: "r < b" by blast
```
```   800       from lemma_interval [OF ar rb]
```
```   801       obtain d where d: "0<d" and bound: "\<forall>y. \<bar>r-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
```
```   802   by blast
```
```   803       have eq_fb: "\<forall>z. a \<le> z --> z \<le> b --> f z = f b"
```
```   804       proof (clarify)
```
```   805         fix z::real
```
```   806         assume az: "a \<le> z" and zb: "z \<le> b"
```
```   807         show "f z = f b"
```
```   808         proof (rule order_antisym)
```
```   809           show "f z \<le> f b" by (simp add: fb_eq_fx x_max az zb)
```
```   810           show "f b \<le> f z" by (simp add: fb_eq_fx' x'_min az zb)
```
```   811         qed
```
```   812       qed
```
```   813       have bound': "\<forall>y. \<bar>r-y\<bar> < d \<longrightarrow> f r = f y"
```
```   814       proof (intro strip)
```
```   815         fix y::real
```
```   816         assume lt: "\<bar>r-y\<bar> < d"
```
```   817         hence "f y = f b" by (simp add: eq_fb bound)
```
```   818         thus "f r = f y" by (simp add: eq_fb ar rb order_less_imp_le)
```
```   819       qed
```
```   820       from differentiableD [OF dif [OF conjI [OF ar rb]]]
```
```   821       obtain l where der: "DERIV f r :> l" ..
```
```   822       have "l=0" by (rule DERIV_local_const [OF der d bound'])
```
```   823         --{*the derivative of a constant function is zero*}
```
```   824       thus ?thesis using ar rb der by auto
```
```   825     qed
```
```   826   qed
```
```   827 qed
```
```   828
```
```   829
```
```   830 subsection{*Mean Value Theorem*}
```
```   831
```
```   832 lemma lemma_MVT:
```
```   833      "f a - (f b - f a)/(b-a) * a = f b - (f b - f a)/(b-a) * (b::real)"
```
```   834   by (cases "a = b") (simp_all add: field_simps)
```
```   835
```
```   836 theorem MVT:
```
```   837   assumes lt:  "a < b"
```
```   838       and con: "\<forall>x. a \<le> x & x \<le> b --> isCont f x"
```
```   839       and dif [rule_format]: "\<forall>x. a < x & x < b --> f differentiable x"
```
```   840   shows "\<exists>l z::real. a < z & z < b & DERIV f z :> l &
```
```   841                    (f(b) - f(a) = (b-a) * l)"
```
```   842 proof -
```
```   843   let ?F = "%x. f x - ((f b - f a) / (b-a)) * x"
```
```   844   have contF: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont ?F x"
```
```   845     using con by (fast intro: isCont_intros)
```
```   846   have difF: "\<forall>x. a < x \<and> x < b \<longrightarrow> ?F differentiable x"
```
```   847   proof (clarify)
```
```   848     fix x::real
```
```   849     assume ax: "a < x" and xb: "x < b"
```
```   850     from differentiableD [OF dif [OF conjI [OF ax xb]]]
```
```   851     obtain l where der: "DERIV f x :> l" ..
```
```   852     show "?F differentiable x"
```
```   853       by (rule differentiableI [where D = "l - (f b - f a)/(b-a)"],
```
```   854           blast intro: DERIV_diff DERIV_cmult_Id der)
```
```   855   qed
```
```   856   from Rolle [where f = ?F, OF lt lemma_MVT contF difF]
```
```   857   obtain z where az: "a < z" and zb: "z < b" and der: "DERIV ?F z :> 0"
```
```   858     by blast
```
```   859   have "DERIV (%x. ((f b - f a)/(b-a)) * x) z :> (f b - f a)/(b-a)"
```
```   860     by (rule DERIV_cmult_Id)
```
```   861   hence derF: "DERIV (\<lambda>x. ?F x + (f b - f a) / (b - a) * x) z
```
```   862                    :> 0 + (f b - f a) / (b - a)"
```
```   863     by (rule DERIV_add [OF der])
```
```   864   show ?thesis
```
```   865   proof (intro exI conjI)
```
```   866     show "a < z" using az .
```
```   867     show "z < b" using zb .
```
```   868     show "f b - f a = (b - a) * ((f b - f a)/(b-a))" by (simp)
```
```   869     show "DERIV f z :> ((f b - f a)/(b-a))"  using derF by simp
```
```   870   qed
```
```   871 qed
```
```   872
```
```   873 lemma MVT2:
```
```   874      "[| a < b; \<forall>x. a \<le> x & x \<le> b --> DERIV f x :> f'(x) |]
```
```   875       ==> \<exists>z::real. a < z & z < b & (f b - f a = (b - a) * f'(z))"
```
```   876 apply (drule MVT)
```
```   877 apply (blast intro: DERIV_isCont)
```
```   878 apply (force dest: order_less_imp_le simp add: differentiable_def)
```
```   879 apply (blast dest: DERIV_unique order_less_imp_le)
```
```   880 done
```
```   881
```
```   882
```
```   883 text{*A function is constant if its derivative is 0 over an interval.*}
```
```   884
```
```   885 lemma DERIV_isconst_end:
```
```   886   fixes f :: "real => real"
```
```   887   shows "[| a < b;
```
```   888          \<forall>x. a \<le> x & x \<le> b --> isCont f x;
```
```   889          \<forall>x. a < x & x < b --> DERIV f x :> 0 |]
```
```   890         ==> f b = f a"
```
```   891 apply (drule MVT, assumption)
```
```   892 apply (blast intro: differentiableI)
```
```   893 apply (auto dest!: DERIV_unique simp add: diff_eq_eq)
```
```   894 done
```
```   895
```
```   896 lemma DERIV_isconst1:
```
```   897   fixes f :: "real => real"
```
```   898   shows "[| a < b;
```
```   899          \<forall>x. a \<le> x & x \<le> b --> isCont f x;
```
```   900          \<forall>x. a < x & x < b --> DERIV f x :> 0 |]
```
```   901         ==> \<forall>x. a \<le> x & x \<le> b --> f x = f a"
```
```   902 apply safe
```
```   903 apply (drule_tac x = a in order_le_imp_less_or_eq, safe)
```
```   904 apply (drule_tac b = x in DERIV_isconst_end, auto)
```
```   905 done
```
```   906
```
```   907 lemma DERIV_isconst2:
```
```   908   fixes f :: "real => real"
```
```   909   shows "[| a < b;
```
```   910          \<forall>x. a \<le> x & x \<le> b --> isCont f x;
```
```   911          \<forall>x. a < x & x < b --> DERIV f x :> 0;
```
```   912          a \<le> x; x \<le> b |]
```
```   913         ==> f x = f a"
```
```   914 apply (blast dest: DERIV_isconst1)
```
```   915 done
```
```   916
```
```   917 lemma DERIV_isconst3: fixes a b x y :: real
```
```   918   assumes "a < b" and "x \<in> {a <..< b}" and "y \<in> {a <..< b}"
```
```   919   assumes derivable: "\<And>x. x \<in> {a <..< b} \<Longrightarrow> DERIV f x :> 0"
```
```   920   shows "f x = f y"
```
```   921 proof (cases "x = y")
```
```   922   case False
```
```   923   let ?a = "min x y"
```
```   924   let ?b = "max x y"
```
```   925
```
```   926   have "\<forall>z. ?a \<le> z \<and> z \<le> ?b \<longrightarrow> DERIV f z :> 0"
```
```   927   proof (rule allI, rule impI)
```
```   928     fix z :: real assume "?a \<le> z \<and> z \<le> ?b"
```
```   929     hence "a < z" and "z < b" using `x \<in> {a <..< b}` and `y \<in> {a <..< b}` by auto
```
```   930     hence "z \<in> {a<..<b}" by auto
```
```   931     thus "DERIV f z :> 0" by (rule derivable)
```
```   932   qed
```
```   933   hence isCont: "\<forall>z. ?a \<le> z \<and> z \<le> ?b \<longrightarrow> isCont f z"
```
```   934     and DERIV: "\<forall>z. ?a < z \<and> z < ?b \<longrightarrow> DERIV f z :> 0" using DERIV_isCont by auto
```
```   935
```
```   936   have "?a < ?b" using `x \<noteq> y` by auto
```
```   937   from DERIV_isconst2[OF this isCont DERIV, of x] and DERIV_isconst2[OF this isCont DERIV, of y]
```
```   938   show ?thesis by auto
```
```   939 qed auto
```
```   940
```
```   941 lemma DERIV_isconst_all:
```
```   942   fixes f :: "real => real"
```
```   943   shows "\<forall>x. DERIV f x :> 0 ==> f(x) = f(y)"
```
```   944 apply (rule linorder_cases [of x y])
```
```   945 apply (blast intro: sym DERIV_isCont DERIV_isconst_end)+
```
```   946 done
```
```   947
```
```   948 lemma DERIV_const_ratio_const:
```
```   949   fixes f :: "real => real"
```
```   950   shows "[|a \<noteq> b; \<forall>x. DERIV f x :> k |] ==> (f(b) - f(a)) = (b-a) * k"
```
```   951 apply (rule linorder_cases [of a b], auto)
```
```   952 apply (drule_tac [!] f = f in MVT)
```
```   953 apply (auto dest: DERIV_isCont DERIV_unique simp add: differentiable_def)
```
```   954 apply (auto dest: DERIV_unique simp add: ring_distribs diff_minus)
```
```   955 done
```
```   956
```
```   957 lemma DERIV_const_ratio_const2:
```
```   958   fixes f :: "real => real"
```
```   959   shows "[|a \<noteq> b; \<forall>x. DERIV f x :> k |] ==> (f(b) - f(a))/(b-a) = k"
```
```   960 apply (rule_tac c1 = "b-a" in real_mult_right_cancel [THEN iffD1])
```
```   961 apply (auto dest!: DERIV_const_ratio_const simp add: mult_assoc)
```
```   962 done
```
```   963
```
```   964 lemma real_average_minus_first [simp]: "((a + b) /2 - a) = (b-a)/(2::real)"
```
```   965 by (simp)
```
```   966
```
```   967 lemma real_average_minus_second [simp]: "((b + a)/2 - a) = (b-a)/(2::real)"
```
```   968 by (simp)
```
```   969
```
```   970 text{*Gallileo's "trick": average velocity = av. of end velocities*}
```
```   971
```
```   972 lemma DERIV_const_average:
```
```   973   fixes v :: "real => real"
```
```   974   assumes neq: "a \<noteq> (b::real)"
```
```   975       and der: "\<forall>x. DERIV v x :> k"
```
```   976   shows "v ((a + b)/2) = (v a + v b)/2"
```
```   977 proof (cases rule: linorder_cases [of a b])
```
```   978   case equal with neq show ?thesis by simp
```
```   979 next
```
```   980   case less
```
```   981   have "(v b - v a) / (b - a) = k"
```
```   982     by (rule DERIV_const_ratio_const2 [OF neq der])
```
```   983   hence "(b-a) * ((v b - v a) / (b-a)) = (b-a) * k" by simp
```
```   984   moreover have "(v ((a + b) / 2) - v a) / ((a + b) / 2 - a) = k"
```
```   985     by (rule DERIV_const_ratio_const2 [OF _ der], simp add: neq)
```
```   986   ultimately show ?thesis using neq by force
```
```   987 next
```
```   988   case greater
```
```   989   have "(v b - v a) / (b - a) = k"
```
```   990     by (rule DERIV_const_ratio_const2 [OF neq der])
```
```   991   hence "(b-a) * ((v b - v a) / (b-a)) = (b-a) * k" by simp
```
```   992   moreover have " (v ((b + a) / 2) - v a) / ((b + a) / 2 - a) = k"
```
```   993     by (rule DERIV_const_ratio_const2 [OF _ der], simp add: neq)
```
```   994   ultimately show ?thesis using neq by (force simp add: add_commute)
```
```   995 qed
```
```   996
```
```   997 (* A function with positive derivative is increasing.
```
```   998    A simple proof using the MVT, by Jeremy Avigad. And variants.
```
```   999 *)
```
```  1000 lemma DERIV_pos_imp_increasing:
```
```  1001   fixes a::real and b::real and f::"real => real"
```
```  1002   assumes "a < b" and "\<forall>x. a \<le> x & x \<le> b --> (EX y. DERIV f x :> y & y > 0)"
```
```  1003   shows "f a < f b"
```
```  1004 proof (rule ccontr)
```
```  1005   assume f: "~ f a < f b"
```
```  1006   have "EX l z. a < z & z < b & DERIV f z :> l
```
```  1007       & f b - f a = (b - a) * l"
```
```  1008     apply (rule MVT)
```
```  1009       using assms
```
```  1010       apply auto
```
```  1011       apply (metis DERIV_isCont)
```
```  1012      apply (metis differentiableI less_le)
```
```  1013     done
```
```  1014   then obtain l z where z: "a < z" "z < b" "DERIV f z :> l"
```
```  1015       and "f b - f a = (b - a) * l"
```
```  1016     by auto
```
```  1017   with assms f have "~(l > 0)"
```
```  1018     by (metis linorder_not_le mult_le_0_iff diff_le_0_iff_le)
```
```  1019   with assms z show False
```
```  1020     by (metis DERIV_unique less_le)
```
```  1021 qed
```
```  1022
```
```  1023 lemma DERIV_nonneg_imp_nondecreasing:
```
```  1024   fixes a::real and b::real and f::"real => real"
```
```  1025   assumes "a \<le> b" and
```
```  1026     "\<forall>x. a \<le> x & x \<le> b --> (\<exists>y. DERIV f x :> y & y \<ge> 0)"
```
```  1027   shows "f a \<le> f b"
```
```  1028 proof (rule ccontr, cases "a = b")
```
```  1029   assume "~ f a \<le> f b" and "a = b"
```
```  1030   then show False by auto
```
```  1031 next
```
```  1032   assume A: "~ f a \<le> f b"
```
```  1033   assume B: "a ~= b"
```
```  1034   with assms have "EX l z. a < z & z < b & DERIV f z :> l
```
```  1035       & f b - f a = (b - a) * l"
```
```  1036     apply -
```
```  1037     apply (rule MVT)
```
```  1038       apply auto
```
```  1039       apply (metis DERIV_isCont)
```
```  1040      apply (metis differentiableI less_le)
```
```  1041     done
```
```  1042   then obtain l z where z: "a < z" "z < b" "DERIV f z :> l"
```
```  1043       and C: "f b - f a = (b - a) * l"
```
```  1044     by auto
```
```  1045   with A have "a < b" "f b < f a" by auto
```
```  1046   with C have "\<not> l \<ge> 0" by (auto simp add: not_le algebra_simps)
```
```  1047     (metis A add_le_cancel_right assms(1) less_eq_real_def mult_right_mono add_left_mono linear order_refl)
```
```  1048   with assms z show False
```
```  1049     by (metis DERIV_unique order_less_imp_le)
```
```  1050 qed
```
```  1051
```
```  1052 lemma DERIV_neg_imp_decreasing:
```
```  1053   fixes a::real and b::real and f::"real => real"
```
```  1054   assumes "a < b" and
```
```  1055     "\<forall>x. a \<le> x & x \<le> b --> (\<exists>y. DERIV f x :> y & y < 0)"
```
```  1056   shows "f a > f b"
```
```  1057 proof -
```
```  1058   have "(%x. -f x) a < (%x. -f x) b"
```
```  1059     apply (rule DERIV_pos_imp_increasing [of a b "%x. -f x"])
```
```  1060     using assms
```
```  1061     apply auto
```
```  1062     apply (metis DERIV_minus neg_0_less_iff_less)
```
```  1063     done
```
```  1064   thus ?thesis
```
```  1065     by simp
```
```  1066 qed
```
```  1067
```
```  1068 lemma DERIV_nonpos_imp_nonincreasing:
```
```  1069   fixes a::real and b::real and f::"real => real"
```
```  1070   assumes "a \<le> b" and
```
```  1071     "\<forall>x. a \<le> x & x \<le> b --> (\<exists>y. DERIV f x :> y & y \<le> 0)"
```
```  1072   shows "f a \<ge> f b"
```
```  1073 proof -
```
```  1074   have "(%x. -f x) a \<le> (%x. -f x) b"
```
```  1075     apply (rule DERIV_nonneg_imp_nondecreasing [of a b "%x. -f x"])
```
```  1076     using assms
```
```  1077     apply auto
```
```  1078     apply (metis DERIV_minus neg_0_le_iff_le)
```
```  1079     done
```
```  1080   thus ?thesis
```
```  1081     by simp
```
```  1082 qed
```
```  1083
```
```  1084 text{*Continuity of inverse function*}
```
```  1085
```
```  1086 lemma isCont_inverse_function:
```
```  1087   fixes f g :: "real \<Rightarrow> real"
```
```  1088   assumes d: "0 < d"
```
```  1089       and inj: "\<forall>z. \<bar>z-x\<bar> \<le> d \<longrightarrow> g (f z) = z"
```
```  1090       and cont: "\<forall>z. \<bar>z-x\<bar> \<le> d \<longrightarrow> isCont f z"
```
```  1091   shows "isCont g (f x)"
```
```  1092 proof -
```
```  1093   let ?A = "f (x - d)" and ?B = "f (x + d)" and ?D = "{x - d..x + d}"
```
```  1094
```
```  1095   have f: "continuous_on ?D f"
```
```  1096     using cont by (intro continuous_at_imp_continuous_on ballI) auto
```
```  1097   then have g: "continuous_on (f`?D) g"
```
```  1098     using inj by (intro continuous_on_inv) auto
```
```  1099
```
```  1100   from d f have "{min ?A ?B <..< max ?A ?B} \<subseteq> f ` ?D"
```
```  1101     by (intro connected_contains_Ioo connected_continuous_image) (auto split: split_min split_max)
```
```  1102   with g have "continuous_on {min ?A ?B <..< max ?A ?B} g"
```
```  1103     by (rule continuous_on_subset)
```
```  1104   moreover
```
```  1105   have "(?A < f x \<and> f x < ?B) \<or> (?B < f x \<and> f x < ?A)"
```
```  1106     using d inj by (intro continuous_inj_imp_mono[OF _ _ f] inj_on_imageI2[of g, OF inj_onI]) auto
```
```  1107   then have "f x \<in> {min ?A ?B <..< max ?A ?B}"
```
```  1108     by auto
```
```  1109   ultimately
```
```  1110   show ?thesis
```
```  1111     by (simp add: continuous_on_eq_continuous_at)
```
```  1112 qed
```
```  1113
```
```  1114 lemma isCont_inverse_function2:
```
```  1115   fixes f g :: "real \<Rightarrow> real" shows
```
```  1116   "\<lbrakk>a < x; x < b;
```
```  1117     \<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> g (f z) = z;
```
```  1118     \<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> isCont f z\<rbrakk>
```
```  1119    \<Longrightarrow> isCont g (f x)"
```
```  1120 apply (rule isCont_inverse_function
```
```  1121        [where f=f and d="min (x - a) (b - x)"])
```
```  1122 apply (simp_all add: abs_le_iff)
```
```  1123 done
```
```  1124
```
```  1125 text {* Derivative of inverse function *}
```
```  1126
```
```  1127 lemma DERIV_inverse_function:
```
```  1128   fixes f g :: "real \<Rightarrow> real"
```
```  1129   assumes der: "DERIV f (g x) :> D"
```
```  1130   assumes neq: "D \<noteq> 0"
```
```  1131   assumes a: "a < x" and b: "x < b"
```
```  1132   assumes inj: "\<forall>y. a < y \<and> y < b \<longrightarrow> f (g y) = y"
```
```  1133   assumes cont: "isCont g x"
```
```  1134   shows "DERIV g x :> inverse D"
```
```  1135 unfolding DERIV_iff2
```
```  1136 proof (rule LIM_equal2)
```
```  1137   show "0 < min (x - a) (b - x)"
```
```  1138     using a b by arith
```
```  1139 next
```
```  1140   fix y
```
```  1141   assume "norm (y - x) < min (x - a) (b - x)"
```
```  1142   hence "a < y" and "y < b"
```
```  1143     by (simp_all add: abs_less_iff)
```
```  1144   thus "(g y - g x) / (y - x) =
```
```  1145         inverse ((f (g y) - x) / (g y - g x))"
```
```  1146     by (simp add: inj)
```
```  1147 next
```
```  1148   have "(\<lambda>z. (f z - f (g x)) / (z - g x)) -- g x --> D"
```
```  1149     by (rule der [unfolded DERIV_iff2])
```
```  1150   hence 1: "(\<lambda>z. (f z - x) / (z - g x)) -- g x --> D"
```
```  1151     using inj a b by simp
```
```  1152   have 2: "\<exists>d>0. \<forall>y. y \<noteq> x \<and> norm (y - x) < d \<longrightarrow> g y \<noteq> g x"
```
```  1153   proof (safe intro!: exI)
```
```  1154     show "0 < min (x - a) (b - x)"
```
```  1155       using a b by simp
```
```  1156   next
```
```  1157     fix y
```
```  1158     assume "norm (y - x) < min (x - a) (b - x)"
```
```  1159     hence y: "a < y" "y < b"
```
```  1160       by (simp_all add: abs_less_iff)
```
```  1161     assume "g y = g x"
```
```  1162     hence "f (g y) = f (g x)" by simp
```
```  1163     hence "y = x" using inj y a b by simp
```
```  1164     also assume "y \<noteq> x"
```
```  1165     finally show False by simp
```
```  1166   qed
```
```  1167   have "(\<lambda>y. (f (g y) - x) / (g y - g x)) -- x --> D"
```
```  1168     using cont 1 2 by (rule isCont_LIM_compose2)
```
```  1169   thus "(\<lambda>y. inverse ((f (g y) - x) / (g y - g x)))
```
```  1170         -- x --> inverse D"
```
```  1171     using neq by (rule tendsto_inverse)
```
```  1172 qed
```
```  1173
```
```  1174 subsection {* Generalized Mean Value Theorem *}
```
```  1175
```
```  1176 theorem GMVT:
```
```  1177   fixes a b :: real
```
```  1178   assumes alb: "a < b"
```
```  1179     and fc: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x"
```
```  1180     and fd: "\<forall>x. a < x \<and> x < b \<longrightarrow> f differentiable x"
```
```  1181     and gc: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont g x"
```
```  1182     and gd: "\<forall>x. a < x \<and> x < b \<longrightarrow> g differentiable x"
```
```  1183   shows "\<exists>g'c f'c c. 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)"
```
```  1184 proof -
```
```  1185   let ?h = "\<lambda>x. (f b - f a)*(g x) - (g b - g a)*(f x)"
```
```  1186   from assms have "a < b" by simp
```
```  1187   moreover have "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont ?h x"
```
```  1188     using fc gc by simp
```
```  1189   moreover have "\<forall>x. a < x \<and> x < b \<longrightarrow> ?h differentiable x"
```
```  1190     using fd gd by simp
```
```  1191   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)
```
```  1192   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" ..
```
```  1193   then obtain c where cdef: "a < c \<and> c < b \<and> DERIV ?h c :> l \<and> ?h b - ?h a = (b - a) * l" ..
```
```  1194
```
```  1195   from cdef have cint: "a < c \<and> c < b" by auto
```
```  1196   with gd have "g differentiable c" by simp
```
```  1197   hence "\<exists>D. DERIV g c :> D" by (rule differentiableD)
```
```  1198   then obtain g'c where g'cdef: "DERIV g c :> g'c" ..
```
```  1199
```
```  1200   from cdef have "a < c \<and> c < b" by auto
```
```  1201   with fd have "f differentiable c" by simp
```
```  1202   hence "\<exists>D. DERIV f c :> D" by (rule differentiableD)
```
```  1203   then obtain f'c where f'cdef: "DERIV f c :> f'c" ..
```
```  1204
```
```  1205   from cdef have "DERIV ?h c :> l" by auto
```
```  1206   moreover have "DERIV ?h c :>  g'c * (f b - f a) - f'c * (g b - g a)"
```
```  1207     using g'cdef f'cdef by (auto intro!: DERIV_intros)
```
```  1208   ultimately have leq: "l =  g'c * (f b - f a) - f'c * (g b - g a)" by (rule DERIV_unique)
```
```  1209
```
```  1210   {
```
```  1211     from cdef have "?h b - ?h a = (b - a) * l" by auto
```
```  1212     also with leq have "\<dots> = (b - a) * (g'c * (f b - f a) - f'c * (g b - g a))" by simp
```
```  1213     finally have "?h b - ?h a = (b - a) * (g'c * (f b - f a) - f'c * (g b - g a))" by simp
```
```  1214   }
```
```  1215   moreover
```
```  1216   {
```
```  1217     have "?h b - ?h a =
```
```  1218          ((f b)*(g b) - (f a)*(g b) - (g b)*(f b) + (g a)*(f b)) -
```
```  1219           ((f b)*(g a) - (f a)*(g a) - (g b)*(f a) + (g a)*(f a))"
```
```  1220       by (simp add: algebra_simps)
```
```  1221     hence "?h b - ?h a = 0" by auto
```
```  1222   }
```
```  1223   ultimately have "(b - a) * (g'c * (f b - f a) - f'c * (g b - g a)) = 0" by auto
```
```  1224   with alb have "g'c * (f b - f a) - f'c * (g b - g a) = 0" by simp
```
```  1225   hence "g'c * (f b - f a) = f'c * (g b - g a)" by simp
```
```  1226   hence "(f b - f a) * g'c = (g b - g a) * f'c" by (simp add: mult_ac)
```
```  1227
```
```  1228   with g'cdef f'cdef cint show ?thesis by auto
```
```  1229 qed
```
```  1230
```
```  1231
```
```  1232 subsection {* Theorems about Limits *}
```
```  1233
```
```  1234 (* need to rename second isCont_inverse *)
```
```  1235
```
```  1236 lemma isCont_inv_fun:
```
```  1237   fixes f g :: "real \<Rightarrow> real"
```
```  1238   shows "[| 0 < d; \<forall>z. \<bar>z - x\<bar> \<le> d --> g(f(z)) = z;
```
```  1239          \<forall>z. \<bar>z - x\<bar> \<le> d --> isCont f z |]
```
```  1240       ==> isCont g (f x)"
```
```  1241 by (rule isCont_inverse_function)
```
```  1242
```
```  1243 text{*Bartle/Sherbert: Introduction to Real Analysis, Theorem 4.2.9, p. 110*}
```
```  1244 lemma LIM_fun_gt_zero:
```
```  1245      "[| f -- c --> (l::real); 0 < l |]
```
```  1246          ==> \<exists>r. 0 < r & (\<forall>x::real. x \<noteq> c & \<bar>c - x\<bar> < r --> 0 < f x)"
```
```  1247 apply (drule (1) LIM_D, clarify)
```
```  1248 apply (rule_tac x = s in exI)
```
```  1249 apply (simp add: abs_less_iff)
```
```  1250 done
```
```  1251
```
```  1252 lemma LIM_fun_less_zero:
```
```  1253      "[| f -- c --> (l::real); l < 0 |]
```
```  1254       ==> \<exists>r. 0 < r & (\<forall>x::real. x \<noteq> c & \<bar>c - x\<bar> < r --> f x < 0)"
```
```  1255 apply (drule LIM_D [where r="-l"], simp, clarify)
```
```  1256 apply (rule_tac x = s in exI)
```
```  1257 apply (simp add: abs_less_iff)
```
```  1258 done
```
```  1259
```
```  1260 lemma LIM_fun_not_zero:
```
```  1261      "[| f -- c --> (l::real); l \<noteq> 0 |]
```
```  1262       ==> \<exists>r. 0 < r & (\<forall>x::real. x \<noteq> c & \<bar>c - x\<bar> < r --> f x \<noteq> 0)"
```
```  1263 apply (rule linorder_cases [of l 0])
```
```  1264 apply (drule (1) LIM_fun_less_zero, force)
```
```  1265 apply simp
```
```  1266 apply (drule (1) LIM_fun_gt_zero, force)
```
```  1267 done
```
```  1268
```
```  1269 lemma GMVT':
```
```  1270   fixes f g :: "real \<Rightarrow> real"
```
```  1271   assumes "a < b"
```
```  1272   assumes isCont_f: "\<And>z. a \<le> z \<Longrightarrow> z \<le> b \<Longrightarrow> isCont f z"
```
```  1273   assumes isCont_g: "\<And>z. a \<le> z \<Longrightarrow> z \<le> b \<Longrightarrow> isCont g z"
```
```  1274   assumes DERIV_g: "\<And>z. a < z \<Longrightarrow> z < b \<Longrightarrow> DERIV g z :> (g' z)"
```
```  1275   assumes DERIV_f: "\<And>z. a < z \<Longrightarrow> z < b \<Longrightarrow> DERIV f z :> (f' z)"
```
```  1276   shows "\<exists>c. a < c \<and> c < b \<and> (f b - f a) * g' c = (g b - g a) * f' c"
```
```  1277 proof -
```
```  1278   have "\<exists>g'c f'c c. DERIV g c :> g'c \<and> DERIV f c :> f'c \<and>
```
```  1279     a < c \<and> c < b \<and> (f b - f a) * g'c = (g b - g a) * f'c"
```
```  1280     using assms by (intro GMVT) (force simp: differentiable_def)+
```
```  1281   then obtain c where "a < c" "c < b" "(f b - f a) * g' c = (g b - g a) * f' c"
```
```  1282     using DERIV_f DERIV_g by (force dest: DERIV_unique)
```
```  1283   then show ?thesis
```
```  1284     by auto
```
```  1285 qed
```
```  1286
```
```  1287 lemma DERIV_cong_ev: "x = y \<Longrightarrow> eventually (\<lambda>x. f x = g x) (nhds x) \<Longrightarrow> u = v \<Longrightarrow>
```
```  1288     DERIV f x :> u \<longleftrightarrow> DERIV g y :> v"
```
```  1289   unfolding DERIV_iff2
```
```  1290 proof (rule filterlim_cong)
```
```  1291   assume "eventually (\<lambda>x. f x = g x) (nhds x)"
```
```  1292   moreover then have "f x = g x" by (auto simp: eventually_nhds)
```
```  1293   moreover assume "x = y" "u = v"
```
```  1294   ultimately show "eventually (\<lambda>xa. (f xa - f x) / (xa - x) = (g xa - g y) / (xa - y)) (at x)"
```
```  1295     by (auto simp: eventually_within at_def elim: eventually_elim1)
```
```  1296 qed simp_all
```
```  1297
```
```  1298 lemma DERIV_shift:
```
```  1299   "(DERIV f (x + z) :> y) \<longleftrightarrow> (DERIV (\<lambda>x. f (x + z)) x :> y)"
```
```  1300   by (simp add: DERIV_iff field_simps)
```
```  1301
```
```  1302 lemma DERIV_mirror:
```
```  1303   "(DERIV f (- x) :> y) \<longleftrightarrow> (DERIV (\<lambda>x. f (- x::real) :: real) x :> - y)"
```
```  1304   by (simp add: deriv_def filterlim_at_split filterlim_at_left_to_right
```
```  1305                 tendsto_minus_cancel_left field_simps conj_commute)
```
```  1306
```
```  1307 lemma lhopital_right_0:
```
```  1308   fixes f0 g0 :: "real \<Rightarrow> real"
```
```  1309   assumes f_0: "(f0 ---> 0) (at_right 0)"
```
```  1310   assumes g_0: "(g0 ---> 0) (at_right 0)"
```
```  1311   assumes ev:
```
```  1312     "eventually (\<lambda>x. g0 x \<noteq> 0) (at_right 0)"
```
```  1313     "eventually (\<lambda>x. g' x \<noteq> 0) (at_right 0)"
```
```  1314     "eventually (\<lambda>x. DERIV f0 x :> f' x) (at_right 0)"
```
```  1315     "eventually (\<lambda>x. DERIV g0 x :> g' x) (at_right 0)"
```
```  1316   assumes lim: "((\<lambda> x. (f' x / g' x)) ---> x) (at_right 0)"
```
```  1317   shows "((\<lambda> x. f0 x / g0 x) ---> x) (at_right 0)"
```
```  1318 proof -
```
```  1319   def f \<equiv> "\<lambda>x. if x \<le> 0 then 0 else f0 x"
```
```  1320   then have "f 0 = 0" by simp
```
```  1321
```
```  1322   def g \<equiv> "\<lambda>x. if x \<le> 0 then 0 else g0 x"
```
```  1323   then have "g 0 = 0" by simp
```
```  1324
```
```  1325   have "eventually (\<lambda>x. g0 x \<noteq> 0 \<and> g' x \<noteq> 0 \<and>
```
```  1326       DERIV f0 x :> (f' x) \<and> DERIV g0 x :> (g' x)) (at_right 0)"
```
```  1327     using ev by eventually_elim auto
```
```  1328   then obtain a where [arith]: "0 < a"
```
```  1329     and g0_neq_0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> g0 x \<noteq> 0"
```
```  1330     and g'_neq_0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> g' x \<noteq> 0"
```
```  1331     and f0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> DERIV f0 x :> (f' x)"
```
```  1332     and g0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> DERIV g0 x :> (g' x)"
```
```  1333     unfolding eventually_within eventually_at by (auto simp: dist_real_def)
```
```  1334
```
```  1335   have g_neq_0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> g x \<noteq> 0"
```
```  1336     using g0_neq_0 by (simp add: g_def)
```
```  1337
```
```  1338   { fix x assume x: "0 < x" "x < a" then have "DERIV f x :> (f' x)"
```
```  1339       by (intro DERIV_cong_ev[THEN iffD1, OF _ _ _ f0[OF x]])
```
```  1340          (auto simp: f_def eventually_nhds_metric dist_real_def intro!: exI[of _ x]) }
```
```  1341   note f = this
```
```  1342
```
```  1343   { fix x assume x: "0 < x" "x < a" then have "DERIV g x :> (g' x)"
```
```  1344       by (intro DERIV_cong_ev[THEN iffD1, OF _ _ _ g0[OF x]])
```
```  1345          (auto simp: g_def eventually_nhds_metric dist_real_def intro!: exI[of _ x]) }
```
```  1346   note g = this
```
```  1347
```
```  1348   have "isCont f 0"
```
```  1349     using tendsto_const[of "0::real" "at 0"] f_0
```
```  1350     unfolding isCont_def f_def
```
```  1351     by (intro filterlim_split_at_real)
```
```  1352        (auto elim: eventually_elim1
```
```  1353              simp add: filterlim_def le_filter_def eventually_within eventually_filtermap)
```
```  1354
```
```  1355   have "isCont g 0"
```
```  1356     using tendsto_const[of "0::real" "at 0"] g_0
```
```  1357     unfolding isCont_def g_def
```
```  1358     by (intro filterlim_split_at_real)
```
```  1359        (auto elim: eventually_elim1
```
```  1360              simp add: filterlim_def le_filter_def eventually_within eventually_filtermap)
```
```  1361
```
```  1362   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)"
```
```  1363   proof (rule bchoice, rule)
```
```  1364     fix x assume "x \<in> {0 <..< a}"
```
```  1365     then have x[arith]: "0 < x" "x < a" by auto
```
```  1366     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"
```
```  1367       by auto
```
```  1368     have "\<And>x. 0 \<le> x \<Longrightarrow> x < a \<Longrightarrow> isCont f x"
```
```  1369       using `isCont f 0` f by (auto intro: DERIV_isCont simp: le_less)
```
```  1370     moreover have "\<And>x. 0 \<le> x \<Longrightarrow> x < a \<Longrightarrow> isCont g x"
```
```  1371       using `isCont g 0` g by (auto intro: DERIV_isCont simp: le_less)
```
```  1372     ultimately have "\<exists>c. 0 < c \<and> c < x \<and> (f x - f 0) * g' c = (g x - g 0) * f' c"
```
```  1373       using f g `x < a` by (intro GMVT') auto
```
```  1374     then guess c ..
```
```  1375     moreover
```
```  1376     with g'(1)[of c] g'(2) have "(f x - f 0)  / (g x - g 0) = f' c / g' c"
```
```  1377       by (simp add: field_simps)
```
```  1378     ultimately show "\<exists>y. 0 < y \<and> y < x \<and> f x / g x = f' y / g' y"
```
```  1379       using `f 0 = 0` `g 0 = 0` by (auto intro!: exI[of _ c])
```
```  1380   qed
```
```  1381   then guess \<zeta> ..
```
```  1382   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)"
```
```  1383     unfolding eventually_within eventually_at by (intro exI[of _ a]) (auto simp: dist_real_def)
```
```  1384   moreover
```
```  1385   from \<zeta> have "eventually (\<lambda>x. norm (\<zeta> x) \<le> x) (at_right 0)"
```
```  1386     by eventually_elim auto
```
```  1387   then have "((\<lambda>x. norm (\<zeta> x)) ---> 0) (at_right 0)"
```
```  1388     by (rule_tac real_tendsto_sandwich[where f="\<lambda>x. 0" and h="\<lambda>x. x"])
```
```  1389        (auto intro: tendsto_const tendsto_ident_at_within)
```
```  1390   then have "(\<zeta> ---> 0) (at_right 0)"
```
```  1391     by (rule tendsto_norm_zero_cancel)
```
```  1392   with \<zeta> have "filterlim \<zeta> (at_right 0) (at_right 0)"
```
```  1393     by (auto elim!: eventually_elim1 simp: filterlim_within filterlim_at)
```
```  1394   from this lim have "((\<lambda>t. f' (\<zeta> t) / g' (\<zeta> t)) ---> x) (at_right 0)"
```
```  1395     by (rule_tac filterlim_compose[of _ _ _ \<zeta>])
```
```  1396   ultimately have "((\<lambda>t. f t / g t) ---> x) (at_right 0)" (is ?P)
```
```  1397     by (rule_tac filterlim_cong[THEN iffD1, OF refl refl])
```
```  1398        (auto elim: eventually_elim1)
```
```  1399   also have "?P \<longleftrightarrow> ?thesis"
```
```  1400     by (rule filterlim_cong) (auto simp: f_def g_def eventually_within)
```
```  1401   finally show ?thesis .
```
```  1402 qed
```
```  1403
```
```  1404 lemma lhopital_right:
```
```  1405   "((f::real \<Rightarrow> real) ---> 0) (at_right x) \<Longrightarrow> (g ---> 0) (at_right x) \<Longrightarrow>
```
```  1406     eventually (\<lambda>x. g x \<noteq> 0) (at_right x) \<Longrightarrow>
```
```  1407     eventually (\<lambda>x. g' x \<noteq> 0) (at_right x) \<Longrightarrow>
```
```  1408     eventually (\<lambda>x. DERIV f x :> f' x) (at_right x) \<Longrightarrow>
```
```  1409     eventually (\<lambda>x. DERIV g x :> g' x) (at_right x) \<Longrightarrow>
```
```  1410     ((\<lambda> x. (f' x / g' x)) ---> y) (at_right x) \<Longrightarrow>
```
```  1411   ((\<lambda> x. f x / g x) ---> y) (at_right x)"
```
```  1412   unfolding eventually_at_right_to_0[of _ x] filterlim_at_right_to_0[of _ _ x] DERIV_shift
```
```  1413   by (rule lhopital_right_0)
```
```  1414
```
```  1415 lemma lhopital_left:
```
```  1416   "((f::real \<Rightarrow> real) ---> 0) (at_left x) \<Longrightarrow> (g ---> 0) (at_left x) \<Longrightarrow>
```
```  1417     eventually (\<lambda>x. g x \<noteq> 0) (at_left x) \<Longrightarrow>
```
```  1418     eventually (\<lambda>x. g' x \<noteq> 0) (at_left x) \<Longrightarrow>
```
```  1419     eventually (\<lambda>x. DERIV f x :> f' x) (at_left x) \<Longrightarrow>
```
```  1420     eventually (\<lambda>x. DERIV g x :> g' x) (at_left x) \<Longrightarrow>
```
```  1421     ((\<lambda> x. (f' x / g' x)) ---> y) (at_left x) \<Longrightarrow>
```
```  1422   ((\<lambda> x. f x / g x) ---> y) (at_left x)"
```
```  1423   unfolding eventually_at_left_to_right filterlim_at_left_to_right DERIV_mirror
```
```  1424   by (rule lhopital_right[where f'="\<lambda>x. - f' (- x)"]) (auto simp: DERIV_mirror)
```
```  1425
```
```  1426 lemma lhopital:
```
```  1427   "((f::real \<Rightarrow> real) ---> 0) (at x) \<Longrightarrow> (g ---> 0) (at x) \<Longrightarrow>
```
```  1428     eventually (\<lambda>x. g x \<noteq> 0) (at x) \<Longrightarrow>
```
```  1429     eventually (\<lambda>x. g' x \<noteq> 0) (at x) \<Longrightarrow>
```
```  1430     eventually (\<lambda>x. DERIV f x :> f' x) (at x) \<Longrightarrow>
```
```  1431     eventually (\<lambda>x. DERIV g x :> g' x) (at x) \<Longrightarrow>
```
```  1432     ((\<lambda> x. (f' x / g' x)) ---> y) (at x) \<Longrightarrow>
```
```  1433   ((\<lambda> x. f x / g x) ---> y) (at x)"
```
```  1434   unfolding eventually_at_split filterlim_at_split
```
```  1435   by (auto intro!: lhopital_right[of f x g g' f'] lhopital_left[of f x g g' f'])
```
```  1436
```
```  1437 lemma lhopital_right_0_at_top:
```
```  1438   fixes f g :: "real \<Rightarrow> real"
```
```  1439   assumes g_0: "LIM x at_right 0. g x :> at_top"
```
```  1440   assumes ev:
```
```  1441     "eventually (\<lambda>x. g' x \<noteq> 0) (at_right 0)"
```
```  1442     "eventually (\<lambda>x. DERIV f x :> f' x) (at_right 0)"
```
```  1443     "eventually (\<lambda>x. DERIV g x :> g' x) (at_right 0)"
```
```  1444   assumes lim: "((\<lambda> x. (f' x / g' x)) ---> x) (at_right 0)"
```
```  1445   shows "((\<lambda> x. f x / g x) ---> x) (at_right 0)"
```
```  1446   unfolding tendsto_iff
```
```  1447 proof safe
```
```  1448   fix e :: real assume "0 < e"
```
```  1449
```
```  1450   with lim[unfolded tendsto_iff, rule_format, of "e / 4"]
```
```  1451   have "eventually (\<lambda>t. dist (f' t / g' t) x < e / 4) (at_right 0)" by simp
```
```  1452   from eventually_conj[OF eventually_conj[OF ev(1) ev(2)] eventually_conj[OF ev(3) this]]
```
```  1453   obtain a where [arith]: "0 < a"
```
```  1454     and g'_neq_0: "\<And>x. 0 < x \<Longrightarrow> x < a \<Longrightarrow> g' x \<noteq> 0"
```
```  1455     and f0: "\<And>x. 0 < x \<Longrightarrow> x \<le> a \<Longrightarrow> DERIV f x :> (f' x)"
```
```  1456     and g0: "\<And>x. 0 < x \<Longrightarrow> x \<le> a \<Longrightarrow> DERIV g x :> (g' x)"
```
```  1457     and Df: "\<And>t. 0 < t \<Longrightarrow> t < a \<Longrightarrow> dist (f' t / g' t) x < e / 4"
```
```  1458     unfolding eventually_within_le by (auto simp: dist_real_def)
```
```  1459
```
```  1460   from Df have
```
```  1461     "eventually (\<lambda>t. t < a) (at_right 0)" "eventually (\<lambda>t::real. 0 < t) (at_right 0)"
```
```  1462     unfolding eventually_within eventually_at by (auto intro!: exI[of _ a] simp: dist_real_def)
```
```  1463
```
```  1464   moreover
```
```  1465   have "eventually (\<lambda>t. 0 < g t) (at_right 0)" "eventually (\<lambda>t. g a < g t) (at_right 0)"
```
```  1466     using g_0 by (auto elim: eventually_elim1 simp: filterlim_at_top_dense)
```
```  1467
```
```  1468   moreover
```
```  1469   have inv_g: "((\<lambda>x. inverse (g x)) ---> 0) (at_right 0)"
```
```  1470     using tendsto_inverse_0 filterlim_mono[OF g_0 at_top_le_at_infinity order_refl]
```
```  1471     by (rule filterlim_compose)
```
```  1472   then have "((\<lambda>x. norm (1 - g a * inverse (g x))) ---> norm (1 - g a * 0)) (at_right 0)"
```
```  1473     by (intro tendsto_intros)
```
```  1474   then have "((\<lambda>x. norm (1 - g a / g x)) ---> 1) (at_right 0)"
```
```  1475     by (simp add: inverse_eq_divide)
```
```  1476   from this[unfolded tendsto_iff, rule_format, of 1]
```
```  1477   have "eventually (\<lambda>x. norm (1 - g a / g x) < 2) (at_right 0)"
```
```  1478     by (auto elim!: eventually_elim1 simp: dist_real_def)
```
```  1479
```
```  1480   moreover
```
```  1481   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)"
```
```  1482     by (intro tendsto_intros)
```
```  1483   then have "((\<lambda>t. norm (f a - x * g a) / norm (g t)) ---> 0) (at_right 0)"
```
```  1484     by (simp add: inverse_eq_divide)
```
```  1485   from this[unfolded tendsto_iff, rule_format, of "e / 2"] `0 < e`
```
```  1486   have "eventually (\<lambda>t. norm (f a - x * g a) / norm (g t) < e / 2) (at_right 0)"
```
```  1487     by (auto simp: dist_real_def)
```
```  1488
```
```  1489   ultimately show "eventually (\<lambda>t. dist (f t / g t) x < e) (at_right 0)"
```
```  1490   proof eventually_elim
```
```  1491     fix t assume t[arith]: "0 < t" "t < a" "g a < g t" "0 < g t"
```
```  1492     assume ineq: "norm (1 - g a / g t) < 2" "norm (f a - x * g a) / norm (g t) < e / 2"
```
```  1493
```
```  1494     have "\<exists>y. t < y \<and> y < a \<and> (g a - g t) * f' y = (f a - f t) * g' y"
```
```  1495       using f0 g0 t(1,2) by (intro GMVT') (force intro!: DERIV_isCont)+
```
```  1496     then guess y ..
```
```  1497     from this
```
```  1498     have [arith]: "t < y" "y < a" and D_eq: "(f t - f a) / (g t - g a) = f' y / g' y"
```
```  1499       using `g a < g t` g'_neq_0[of y] by (auto simp add: field_simps)
```
```  1500
```
```  1501     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"
```
```  1502       by (simp add: field_simps)
```
```  1503     have "norm (f t / g t - x) \<le>
```
```  1504         norm (((f t - f a) / (g t - g a) - x) * (1 - g a / g t)) + norm ((f a - x * g a) / g t)"
```
```  1505       unfolding * by (rule norm_triangle_ineq)
```
```  1506     also have "\<dots> = dist (f' y / g' y) x * norm (1 - g a / g t) + norm (f a - x * g a) / norm (g t)"
```
```  1507       by (simp add: abs_mult D_eq dist_real_def)
```
```  1508     also have "\<dots> < (e / 4) * 2 + e / 2"
```
```  1509       using ineq Df[of y] `0 < e` by (intro add_le_less_mono mult_mono) auto
```
```  1510     finally show "dist (f t / g t) x < e"
```
```  1511       by (simp add: dist_real_def)
```
```  1512   qed
```
```  1513 qed
```
```  1514
```
```  1515 lemma lhopital_right_at_top:
```
```  1516   "LIM x at_right x. (g::real \<Rightarrow> real) x :> at_top \<Longrightarrow>
```
```  1517     eventually (\<lambda>x. g' x \<noteq> 0) (at_right x) \<Longrightarrow>
```
```  1518     eventually (\<lambda>x. DERIV f x :> f' x) (at_right x) \<Longrightarrow>
```
```  1519     eventually (\<lambda>x. DERIV g x :> g' x) (at_right x) \<Longrightarrow>
```
```  1520     ((\<lambda> x. (f' x / g' x)) ---> y) (at_right x) \<Longrightarrow>
```
```  1521     ((\<lambda> x. f x / g x) ---> y) (at_right x)"
```
```  1522   unfolding eventually_at_right_to_0[of _ x] filterlim_at_right_to_0[of _ _ x] DERIV_shift
```
```  1523   by (rule lhopital_right_0_at_top)
```
```  1524
```
```  1525 lemma lhopital_left_at_top:
```
```  1526   "LIM x at_left x. (g::real \<Rightarrow> real) x :> at_top \<Longrightarrow>
```
```  1527     eventually (\<lambda>x. g' x \<noteq> 0) (at_left x) \<Longrightarrow>
```
```  1528     eventually (\<lambda>x. DERIV f x :> f' x) (at_left x) \<Longrightarrow>
```
```  1529     eventually (\<lambda>x. DERIV g x :> g' x) (at_left x) \<Longrightarrow>
```
```  1530     ((\<lambda> x. (f' x / g' x)) ---> y) (at_left x) \<Longrightarrow>
```
```  1531     ((\<lambda> x. f x / g x) ---> y) (at_left x)"
```
```  1532   unfolding eventually_at_left_to_right filterlim_at_left_to_right DERIV_mirror
```
```  1533   by (rule lhopital_right_at_top[where f'="\<lambda>x. - f' (- x)"]) (auto simp: DERIV_mirror)
```
```  1534
```
```  1535 lemma lhopital_at_top:
```
```  1536   "LIM x at x. (g::real \<Rightarrow> real) x :> at_top \<Longrightarrow>
```
```  1537     eventually (\<lambda>x. g' x \<noteq> 0) (at x) \<Longrightarrow>
```
```  1538     eventually (\<lambda>x. DERIV f x :> f' x) (at x) \<Longrightarrow>
```
```  1539     eventually (\<lambda>x. DERIV g x :> g' x) (at x) \<Longrightarrow>
```
```  1540     ((\<lambda> x. (f' x / g' x)) ---> y) (at x) \<Longrightarrow>
```
```  1541     ((\<lambda> x. f x / g x) ---> y) (at x)"
```
```  1542   unfolding eventually_at_split filterlim_at_split
```
```  1543   by (auto intro!: lhopital_right_at_top[of g x g' f f'] lhopital_left_at_top[of g x g' f f'])
```
```  1544
```
```  1545 lemma lhospital_at_top_at_top:
```
```  1546   fixes f g :: "real \<Rightarrow> real"
```
```  1547   assumes g_0: "LIM x at_top. g x :> at_top"
```
```  1548   assumes g': "eventually (\<lambda>x. g' x \<noteq> 0) at_top"
```
```  1549   assumes Df: "eventually (\<lambda>x. DERIV f x :> f' x) at_top"
```
```  1550   assumes Dg: "eventually (\<lambda>x. DERIV g x :> g' x) at_top"
```
```  1551   assumes lim: "((\<lambda> x. (f' x / g' x)) ---> x) at_top"
```
```  1552   shows "((\<lambda> x. f x / g x) ---> x) at_top"
```
```  1553   unfolding filterlim_at_top_to_right
```
```  1554 proof (rule lhopital_right_0_at_top)
```
```  1555   let ?F = "\<lambda>x. f (inverse x)"
```
```  1556   let ?G = "\<lambda>x. g (inverse x)"
```
```  1557   let ?R = "at_right (0::real)"
```
```  1558   let ?D = "\<lambda>f' x. f' (inverse x) * - (inverse x ^ Suc (Suc 0))"
```
```  1559
```
```  1560   show "LIM x ?R. ?G x :> at_top"
```
```  1561     using g_0 unfolding filterlim_at_top_to_right .
```
```  1562
```
```  1563   show "eventually (\<lambda>x. DERIV ?G x  :> ?D g' x) ?R"
```
```  1564     unfolding eventually_at_right_to_top
```
```  1565     using Dg eventually_ge_at_top[where c="1::real"]
```
```  1566     apply eventually_elim
```
```  1567     apply (rule DERIV_cong)
```
```  1568     apply (rule DERIV_chain'[where f=inverse])
```
```  1569     apply (auto intro!:  DERIV_inverse)
```
```  1570     done
```
```  1571
```
```  1572   show "eventually (\<lambda>x. DERIV ?F x  :> ?D f' x) ?R"
```
```  1573     unfolding eventually_at_right_to_top
```
```  1574     using Df eventually_ge_at_top[where c="1::real"]
```
```  1575     apply eventually_elim
```
```  1576     apply (rule DERIV_cong)
```
```  1577     apply (rule DERIV_chain'[where f=inverse])
```
```  1578     apply (auto intro!:  DERIV_inverse)
```
```  1579     done
```
```  1580
```
```  1581   show "eventually (\<lambda>x. ?D g' x \<noteq> 0) ?R"
```
```  1582     unfolding eventually_at_right_to_top
```
```  1583     using g' eventually_ge_at_top[where c="1::real"]
```
```  1584     by eventually_elim auto
```
```  1585
```
```  1586   show "((\<lambda>x. ?D f' x / ?D g' x) ---> x) ?R"
```
```  1587     unfolding filterlim_at_right_to_top
```
```  1588     apply (intro filterlim_cong[THEN iffD2, OF refl refl _ lim])
```
```  1589     using eventually_ge_at_top[where c="1::real"]
```
```  1590     by eventually_elim simp
```
```  1591 qed
```
```  1592
```
```  1593 end
```