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