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