src/HOL/Deriv.thy
changeset 28952 15a4b2cf8c34
parent 27668 6eb20b2cecf8
child 29166 c23b2d108612
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Deriv.thy	Wed Dec 03 15:58:44 2008 +0100
     1.3 @@ -0,0 +1,1725 @@
     1.4 +(*  Title       : Deriv.thy
     1.5 +    ID          : $Id$
     1.6 +    Author      : Jacques D. Fleuriot
     1.7 +    Copyright   : 1998  University of Cambridge
     1.8 +    Conversion to Isar and new proofs by Lawrence C Paulson, 2004
     1.9 +    GMVT by Benjamin Porter, 2005
    1.10 +*)
    1.11 +
    1.12 +header{* Differentiation *}
    1.13 +
    1.14 +theory Deriv
    1.15 +imports Lim Univ_Poly
    1.16 +begin
    1.17 +
    1.18 +text{*Standard Definitions*}
    1.19 +
    1.20 +definition
    1.21 +  deriv :: "['a::real_normed_field \<Rightarrow> 'a, 'a, 'a] \<Rightarrow> bool"
    1.22 +    --{*Differentiation: D is derivative of function f at x*}
    1.23 +          ("(DERIV (_)/ (_)/ :> (_))" [1000, 1000, 60] 60) where
    1.24 +  "DERIV f x :> D = ((%h. (f(x + h) - f x) / h) -- 0 --> D)"
    1.25 +
    1.26 +definition
    1.27 +  differentiable :: "['a::real_normed_field \<Rightarrow> 'a, 'a] \<Rightarrow> bool"
    1.28 +    (infixl "differentiable" 60) where
    1.29 +  "f differentiable x = (\<exists>D. DERIV f x :> D)"
    1.30 +
    1.31 +
    1.32 +consts
    1.33 +  Bolzano_bisect :: "[real*real=>bool, real, real, nat] => (real*real)"
    1.34 +primrec
    1.35 +  "Bolzano_bisect P a b 0 = (a,b)"
    1.36 +  "Bolzano_bisect P a b (Suc n) =
    1.37 +      (let (x,y) = Bolzano_bisect P a b n
    1.38 +       in if P(x, (x+y)/2) then ((x+y)/2, y)
    1.39 +                            else (x, (x+y)/2))"
    1.40 +
    1.41 +
    1.42 +subsection {* Derivatives *}
    1.43 +
    1.44 +lemma DERIV_iff: "(DERIV f x :> D) = ((%h. (f(x + h) - f(x))/h) -- 0 --> D)"
    1.45 +by (simp add: deriv_def)
    1.46 +
    1.47 +lemma DERIV_D: "DERIV f x :> D ==> (%h. (f(x + h) - f(x))/h) -- 0 --> D"
    1.48 +by (simp add: deriv_def)
    1.49 +
    1.50 +lemma DERIV_const [simp]: "DERIV (\<lambda>x. k) x :> 0"
    1.51 +by (simp add: deriv_def)
    1.52 +
    1.53 +lemma DERIV_ident [simp]: "DERIV (\<lambda>x. x) x :> 1"
    1.54 +by (simp add: deriv_def cong: LIM_cong)
    1.55 +
    1.56 +lemma add_diff_add:
    1.57 +  fixes a b c d :: "'a::ab_group_add"
    1.58 +  shows "(a + c) - (b + d) = (a - b) + (c - d)"
    1.59 +by simp
    1.60 +
    1.61 +lemma DERIV_add:
    1.62 +  "\<lbrakk>DERIV f x :> D; DERIV g x :> E\<rbrakk> \<Longrightarrow> DERIV (\<lambda>x. f x + g x) x :> D + E"
    1.63 +by (simp only: deriv_def add_diff_add add_divide_distrib LIM_add)
    1.64 +
    1.65 +lemma DERIV_minus:
    1.66 +  "DERIV f x :> D \<Longrightarrow> DERIV (\<lambda>x. - f x) x :> - D"
    1.67 +by (simp only: deriv_def minus_diff_minus divide_minus_left LIM_minus)
    1.68 +
    1.69 +lemma DERIV_diff:
    1.70 +  "\<lbrakk>DERIV f x :> D; DERIV g x :> E\<rbrakk> \<Longrightarrow> DERIV (\<lambda>x. f x - g x) x :> D - E"
    1.71 +by (simp only: diff_def DERIV_add DERIV_minus)
    1.72 +
    1.73 +lemma DERIV_add_minus:
    1.74 +  "\<lbrakk>DERIV f x :> D; DERIV g x :> E\<rbrakk> \<Longrightarrow> DERIV (\<lambda>x. f x + - g x) x :> D + - E"
    1.75 +by (simp only: DERIV_add DERIV_minus)
    1.76 +
    1.77 +lemma DERIV_isCont: "DERIV f x :> D \<Longrightarrow> isCont f x"
    1.78 +proof (unfold isCont_iff)
    1.79 +  assume "DERIV f x :> D"
    1.80 +  hence "(\<lambda>h. (f(x+h) - f(x)) / h) -- 0 --> D"
    1.81 +    by (rule DERIV_D)
    1.82 +  hence "(\<lambda>h. (f(x+h) - f(x)) / h * h) -- 0 --> D * 0"
    1.83 +    by (intro LIM_mult LIM_ident)
    1.84 +  hence "(\<lambda>h. (f(x+h) - f(x)) * (h / h)) -- 0 --> 0"
    1.85 +    by simp
    1.86 +  hence "(\<lambda>h. f(x+h) - f(x)) -- 0 --> 0"
    1.87 +    by (simp cong: LIM_cong)
    1.88 +  thus "(\<lambda>h. f(x+h)) -- 0 --> f(x)"
    1.89 +    by (simp add: LIM_def)
    1.90 +qed
    1.91 +
    1.92 +lemma DERIV_mult_lemma:
    1.93 +  fixes a b c d :: "'a::real_field"
    1.94 +  shows "(a * b - c * d) / h = a * ((b - d) / h) + ((a - c) / h) * d"
    1.95 +by (simp add: diff_minus add_divide_distrib [symmetric] ring_distribs)
    1.96 +
    1.97 +lemma DERIV_mult':
    1.98 +  assumes f: "DERIV f x :> D"
    1.99 +  assumes g: "DERIV g x :> E"
   1.100 +  shows "DERIV (\<lambda>x. f x * g x) x :> f x * E + D * g x"
   1.101 +proof (unfold deriv_def)
   1.102 +  from f have "isCont f x"
   1.103 +    by (rule DERIV_isCont)
   1.104 +  hence "(\<lambda>h. f(x+h)) -- 0 --> f x"
   1.105 +    by (simp only: isCont_iff)
   1.106 +  hence "(\<lambda>h. f(x+h) * ((g(x+h) - g x) / h) +
   1.107 +              ((f(x+h) - f x) / h) * g x)
   1.108 +          -- 0 --> f x * E + D * g x"
   1.109 +    by (intro LIM_add LIM_mult LIM_const DERIV_D f g)
   1.110 +  thus "(\<lambda>h. (f(x+h) * g(x+h) - f x * g x) / h)
   1.111 +         -- 0 --> f x * E + D * g x"
   1.112 +    by (simp only: DERIV_mult_lemma)
   1.113 +qed
   1.114 +
   1.115 +lemma DERIV_mult:
   1.116 +     "[| DERIV f x :> Da; DERIV g x :> Db |]
   1.117 +      ==> DERIV (%x. f x * g x) x :> (Da * g(x)) + (Db * f(x))"
   1.118 +by (drule (1) DERIV_mult', simp only: mult_commute add_commute)
   1.119 +
   1.120 +lemma DERIV_unique:
   1.121 +      "[| DERIV f x :> D; DERIV f x :> E |] ==> D = E"
   1.122 +apply (simp add: deriv_def)
   1.123 +apply (blast intro: LIM_unique)
   1.124 +done
   1.125 +
   1.126 +text{*Differentiation of finite sum*}
   1.127 +
   1.128 +lemma DERIV_sumr [rule_format (no_asm)]:
   1.129 +     "(\<forall>r. m \<le> r & r < (m + n) --> DERIV (%x. f r x) x :> (f' r x))
   1.130 +      --> DERIV (%x. \<Sum>n=m..<n::nat. f n x :: real) x :> (\<Sum>r=m..<n. f' r x)"
   1.131 +apply (induct "n")
   1.132 +apply (auto intro: DERIV_add)
   1.133 +done
   1.134 +
   1.135 +text{*Alternative definition for differentiability*}
   1.136 +
   1.137 +lemma DERIV_LIM_iff:
   1.138 +     "((%h. (f(a + h) - f(a)) / h) -- 0 --> D) =
   1.139 +      ((%x. (f(x)-f(a)) / (x-a)) -- a --> D)"
   1.140 +apply (rule iffI)
   1.141 +apply (drule_tac k="- a" in LIM_offset)
   1.142 +apply (simp add: diff_minus)
   1.143 +apply (drule_tac k="a" in LIM_offset)
   1.144 +apply (simp add: add_commute)
   1.145 +done
   1.146 +
   1.147 +lemma DERIV_iff2: "(DERIV f x :> D) = ((%z. (f(z) - f(x)) / (z-x)) -- x --> D)"
   1.148 +by (simp add: deriv_def diff_minus [symmetric] DERIV_LIM_iff)
   1.149 +
   1.150 +lemma inverse_diff_inverse:
   1.151 +  "\<lbrakk>(a::'a::division_ring) \<noteq> 0; b \<noteq> 0\<rbrakk>
   1.152 +   \<Longrightarrow> inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
   1.153 +by (simp add: ring_simps)
   1.154 +
   1.155 +lemma DERIV_inverse_lemma:
   1.156 +  "\<lbrakk>a \<noteq> 0; b \<noteq> (0::'a::real_normed_field)\<rbrakk>
   1.157 +   \<Longrightarrow> (inverse a - inverse b) / h
   1.158 +     = - (inverse a * ((a - b) / h) * inverse b)"
   1.159 +by (simp add: inverse_diff_inverse)
   1.160 +
   1.161 +lemma DERIV_inverse':
   1.162 +  assumes der: "DERIV f x :> D"
   1.163 +  assumes neq: "f x \<noteq> 0"
   1.164 +  shows "DERIV (\<lambda>x. inverse (f x)) x :> - (inverse (f x) * D * inverse (f x))"
   1.165 +    (is "DERIV _ _ :> ?E")
   1.166 +proof (unfold DERIV_iff2)
   1.167 +  from der have lim_f: "f -- x --> f x"
   1.168 +    by (rule DERIV_isCont [unfolded isCont_def])
   1.169 +
   1.170 +  from neq have "0 < norm (f x)" by simp
   1.171 +  with LIM_D [OF lim_f] obtain s
   1.172 +    where s: "0 < s"
   1.173 +    and less_fx: "\<And>z. \<lbrakk>z \<noteq> x; norm (z - x) < s\<rbrakk>
   1.174 +                  \<Longrightarrow> norm (f z - f x) < norm (f x)"
   1.175 +    by fast
   1.176 +
   1.177 +  show "(\<lambda>z. (inverse (f z) - inverse (f x)) / (z - x)) -- x --> ?E"
   1.178 +  proof (rule LIM_equal2 [OF s])
   1.179 +    fix z
   1.180 +    assume "z \<noteq> x" "norm (z - x) < s"
   1.181 +    hence "norm (f z - f x) < norm (f x)" by (rule less_fx)
   1.182 +    hence "f z \<noteq> 0" by auto
   1.183 +    thus "(inverse (f z) - inverse (f x)) / (z - x) =
   1.184 +          - (inverse (f z) * ((f z - f x) / (z - x)) * inverse (f x))"
   1.185 +      using neq by (rule DERIV_inverse_lemma)
   1.186 +  next
   1.187 +    from der have "(\<lambda>z. (f z - f x) / (z - x)) -- x --> D"
   1.188 +      by (unfold DERIV_iff2)
   1.189 +    thus "(\<lambda>z. - (inverse (f z) * ((f z - f x) / (z - x)) * inverse (f x)))
   1.190 +          -- x --> ?E"
   1.191 +      by (intro LIM_mult LIM_inverse LIM_minus LIM_const lim_f neq)
   1.192 +  qed
   1.193 +qed
   1.194 +
   1.195 +lemma DERIV_divide:
   1.196 +  "\<lbrakk>DERIV f x :> D; DERIV g x :> E; g x \<noteq> 0\<rbrakk>
   1.197 +   \<Longrightarrow> DERIV (\<lambda>x. f x / g x) x :> (D * g x - f x * E) / (g x * g x)"
   1.198 +apply (subgoal_tac "f x * - (inverse (g x) * E * inverse (g x)) +
   1.199 +          D * inverse (g x) = (D * g x - f x * E) / (g x * g x)")
   1.200 +apply (erule subst)
   1.201 +apply (unfold divide_inverse)
   1.202 +apply (erule DERIV_mult')
   1.203 +apply (erule (1) DERIV_inverse')
   1.204 +apply (simp add: ring_distribs nonzero_inverse_mult_distrib)
   1.205 +apply (simp add: mult_ac)
   1.206 +done
   1.207 +
   1.208 +lemma DERIV_power_Suc:
   1.209 +  fixes f :: "'a \<Rightarrow> 'a::{real_normed_field,recpower}"
   1.210 +  assumes f: "DERIV f x :> D"
   1.211 +  shows "DERIV (\<lambda>x. f x ^ Suc n) x :> (1 + of_nat n) * (D * f x ^ n)"
   1.212 +proof (induct n)
   1.213 +case 0
   1.214 +  show ?case by (simp add: power_Suc f)
   1.215 +case (Suc k)
   1.216 +  from DERIV_mult' [OF f Suc] show ?case
   1.217 +    apply (simp only: of_nat_Suc ring_distribs mult_1_left)
   1.218 +    apply (simp only: power_Suc right_distrib mult_ac add_ac)
   1.219 +    done
   1.220 +qed
   1.221 +
   1.222 +lemma DERIV_power:
   1.223 +  fixes f :: "'a \<Rightarrow> 'a::{real_normed_field,recpower}"
   1.224 +  assumes f: "DERIV f x :> D"
   1.225 +  shows "DERIV (\<lambda>x. f x ^ n) x :> of_nat n * (D * f x ^ (n - Suc 0))"
   1.226 +by (cases "n", simp, simp add: DERIV_power_Suc f)
   1.227 +
   1.228 +
   1.229 +(* ------------------------------------------------------------------------ *)
   1.230 +(* Caratheodory formulation of derivative at a point: standard proof        *)
   1.231 +(* ------------------------------------------------------------------------ *)
   1.232 +
   1.233 +lemma CARAT_DERIV:
   1.234 +     "(DERIV f x :> l) =
   1.235 +      (\<exists>g. (\<forall>z. f z - f x = g z * (z-x)) & isCont g x & g x = l)"
   1.236 +      (is "?lhs = ?rhs")
   1.237 +proof
   1.238 +  assume der: "DERIV f x :> l"
   1.239 +  show "\<exists>g. (\<forall>z. f z - f x = g z * (z-x)) \<and> isCont g x \<and> g x = l"
   1.240 +  proof (intro exI conjI)
   1.241 +    let ?g = "(%z. if z = x then l else (f z - f x) / (z-x))"
   1.242 +    show "\<forall>z. f z - f x = ?g z * (z-x)" by simp
   1.243 +    show "isCont ?g x" using der
   1.244 +      by (simp add: isCont_iff DERIV_iff diff_minus
   1.245 +               cong: LIM_equal [rule_format])
   1.246 +    show "?g x = l" by simp
   1.247 +  qed
   1.248 +next
   1.249 +  assume "?rhs"
   1.250 +  then obtain g where
   1.251 +    "(\<forall>z. f z - f x = g z * (z-x))" and "isCont g x" and "g x = l" by blast
   1.252 +  thus "(DERIV f x :> l)"
   1.253 +     by (auto simp add: isCont_iff DERIV_iff cong: LIM_cong)
   1.254 +qed
   1.255 +
   1.256 +lemma DERIV_chain':
   1.257 +  assumes f: "DERIV f x :> D"
   1.258 +  assumes g: "DERIV g (f x) :> E"
   1.259 +  shows "DERIV (\<lambda>x. g (f x)) x :> E * D"
   1.260 +proof (unfold DERIV_iff2)
   1.261 +  obtain d where d: "\<forall>y. g y - g (f x) = d y * (y - f x)"
   1.262 +    and cont_d: "isCont d (f x)" and dfx: "d (f x) = E"
   1.263 +    using CARAT_DERIV [THEN iffD1, OF g] by fast
   1.264 +  from f have "f -- x --> f x"
   1.265 +    by (rule DERIV_isCont [unfolded isCont_def])
   1.266 +  with cont_d have "(\<lambda>z. d (f z)) -- x --> d (f x)"
   1.267 +    by (rule isCont_LIM_compose)
   1.268 +  hence "(\<lambda>z. d (f z) * ((f z - f x) / (z - x)))
   1.269 +          -- x --> d (f x) * D"
   1.270 +    by (rule LIM_mult [OF _ f [unfolded DERIV_iff2]])
   1.271 +  thus "(\<lambda>z. (g (f z) - g (f x)) / (z - x)) -- x --> E * D"
   1.272 +    by (simp add: d dfx real_scaleR_def)
   1.273 +qed
   1.274 +
   1.275 +(* let's do the standard proof though theorem *)
   1.276 +(* LIM_mult2 follows from a NS proof          *)
   1.277 +
   1.278 +lemma DERIV_cmult:
   1.279 +      "DERIV f x :> D ==> DERIV (%x. c * f x) x :> c*D"
   1.280 +by (drule DERIV_mult' [OF DERIV_const], simp)
   1.281 +
   1.282 +(* standard version *)
   1.283 +lemma DERIV_chain: "[| DERIV f (g x) :> Da; DERIV g x :> Db |] ==> DERIV (f o g) x :> Da * Db"
   1.284 +by (drule (1) DERIV_chain', simp add: o_def real_scaleR_def mult_commute)
   1.285 +
   1.286 +lemma DERIV_chain2: "[| DERIV f (g x) :> Da; DERIV g x :> Db |] ==> DERIV (%x. f (g x)) x :> Da * Db"
   1.287 +by (auto dest: DERIV_chain simp add: o_def)
   1.288 +
   1.289 +(*derivative of linear multiplication*)
   1.290 +lemma DERIV_cmult_Id [simp]: "DERIV (op * c) x :> c"
   1.291 +by (cut_tac c = c and x = x in DERIV_ident [THEN DERIV_cmult], simp)
   1.292 +
   1.293 +lemma DERIV_pow: "DERIV (%x. x ^ n) x :> real n * (x ^ (n - Suc 0))"
   1.294 +apply (cut_tac DERIV_power [OF DERIV_ident])
   1.295 +apply (simp add: real_scaleR_def real_of_nat_def)
   1.296 +done
   1.297 +
   1.298 +text{*Power of -1*}
   1.299 +
   1.300 +lemma DERIV_inverse:
   1.301 +  fixes x :: "'a::{real_normed_field,recpower}"
   1.302 +  shows "x \<noteq> 0 ==> DERIV (%x. inverse(x)) x :> (-(inverse x ^ Suc (Suc 0)))"
   1.303 +by (drule DERIV_inverse' [OF DERIV_ident]) (simp add: power_Suc)
   1.304 +
   1.305 +text{*Derivative of inverse*}
   1.306 +lemma DERIV_inverse_fun:
   1.307 +  fixes x :: "'a::{real_normed_field,recpower}"
   1.308 +  shows "[| DERIV f x :> d; f(x) \<noteq> 0 |]
   1.309 +      ==> DERIV (%x. inverse(f x)) x :> (- (d * inverse(f(x) ^ Suc (Suc 0))))"
   1.310 +by (drule (1) DERIV_inverse') (simp add: mult_ac power_Suc nonzero_inverse_mult_distrib)
   1.311 +
   1.312 +text{*Derivative of quotient*}
   1.313 +lemma DERIV_quotient:
   1.314 +  fixes x :: "'a::{real_normed_field,recpower}"
   1.315 +  shows "[| DERIV f x :> d; DERIV g x :> e; g(x) \<noteq> 0 |]
   1.316 +       ==> DERIV (%y. f(y) / (g y)) x :> (d*g(x) - (e*f(x))) / (g(x) ^ Suc (Suc 0))"
   1.317 +by (drule (2) DERIV_divide) (simp add: mult_commute power_Suc)
   1.318 +
   1.319 +
   1.320 +subsection {* Differentiability predicate *}
   1.321 +
   1.322 +lemma differentiableD: "f differentiable x ==> \<exists>D. DERIV f x :> D"
   1.323 +by (simp add: differentiable_def)
   1.324 +
   1.325 +lemma differentiableI: "DERIV f x :> D ==> f differentiable x"
   1.326 +by (force simp add: differentiable_def)
   1.327 +
   1.328 +lemma differentiable_const: "(\<lambda>z. a) differentiable x"
   1.329 +  apply (unfold differentiable_def)
   1.330 +  apply (rule_tac x=0 in exI)
   1.331 +  apply simp
   1.332 +  done
   1.333 +
   1.334 +lemma differentiable_sum:
   1.335 +  assumes "f differentiable x"
   1.336 +  and "g differentiable x"
   1.337 +  shows "(\<lambda>x. f x + g x) differentiable x"
   1.338 +proof -
   1.339 +  from prems have "\<exists>D. DERIV f x :> D" by (unfold differentiable_def)
   1.340 +  then obtain df where "DERIV f x :> df" ..
   1.341 +  moreover from prems have "\<exists>D. DERIV g x :> D" by (unfold differentiable_def)
   1.342 +  then obtain dg where "DERIV g x :> dg" ..
   1.343 +  ultimately have "DERIV (\<lambda>x. f x + g x) x :> df + dg" by (rule DERIV_add)
   1.344 +  hence "\<exists>D. DERIV (\<lambda>x. f x + g x) x :> D" by auto
   1.345 +  thus ?thesis by (fold differentiable_def)
   1.346 +qed
   1.347 +
   1.348 +lemma differentiable_diff:
   1.349 +  assumes "f differentiable x"
   1.350 +  and "g differentiable x"
   1.351 +  shows "(\<lambda>x. f x - g x) differentiable x"
   1.352 +proof -
   1.353 +  from prems have "f differentiable x" by simp
   1.354 +  moreover
   1.355 +  from prems have "\<exists>D. DERIV g x :> D" by (unfold differentiable_def)
   1.356 +  then obtain dg where "DERIV g x :> dg" ..
   1.357 +  then have "DERIV (\<lambda>x. - g x) x :> -dg" by (rule DERIV_minus)
   1.358 +  hence "\<exists>D. DERIV (\<lambda>x. - g x) x :> D" by auto
   1.359 +  hence "(\<lambda>x. - g x) differentiable x" by (fold differentiable_def)
   1.360 +  ultimately 
   1.361 +  show ?thesis
   1.362 +    by (auto simp: diff_def dest: differentiable_sum)
   1.363 +qed
   1.364 +
   1.365 +lemma differentiable_mult:
   1.366 +  assumes "f differentiable x"
   1.367 +  and "g differentiable x"
   1.368 +  shows "(\<lambda>x. f x * g x) differentiable x"
   1.369 +proof -
   1.370 +  from prems have "\<exists>D. DERIV f x :> D" by (unfold differentiable_def)
   1.371 +  then obtain df where "DERIV f x :> df" ..
   1.372 +  moreover from prems have "\<exists>D. DERIV g x :> D" by (unfold differentiable_def)
   1.373 +  then obtain dg where "DERIV g x :> dg" ..
   1.374 +  ultimately have "DERIV (\<lambda>x. f x * g x) x :> df * g x + dg * f x" by (simp add: DERIV_mult)
   1.375 +  hence "\<exists>D. DERIV (\<lambda>x. f x * g x) x :> D" by auto
   1.376 +  thus ?thesis by (fold differentiable_def)
   1.377 +qed
   1.378 +
   1.379 +
   1.380 +subsection {* Nested Intervals and Bisection *}
   1.381 +
   1.382 +text{*Lemmas about nested intervals and proof by bisection (cf.Harrison).
   1.383 +     All considerably tidied by lcp.*}
   1.384 +
   1.385 +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)"
   1.386 +apply (induct "no")
   1.387 +apply (auto intro: order_trans)
   1.388 +done
   1.389 +
   1.390 +lemma f_inc_g_dec_Beq_f: "[| \<forall>n. f(n) \<le> f(Suc n);
   1.391 +         \<forall>n. g(Suc n) \<le> g(n);
   1.392 +         \<forall>n. f(n) \<le> g(n) |]
   1.393 +      ==> Bseq (f :: nat \<Rightarrow> real)"
   1.394 +apply (rule_tac k = "f 0" and K = "g 0" in BseqI2, rule allI)
   1.395 +apply (induct_tac "n")
   1.396 +apply (auto intro: order_trans)
   1.397 +apply (rule_tac y = "g (Suc na)" in order_trans)
   1.398 +apply (induct_tac [2] "na")
   1.399 +apply (auto intro: order_trans)
   1.400 +done
   1.401 +
   1.402 +lemma f_inc_g_dec_Beq_g: "[| \<forall>n. f(n) \<le> f(Suc n);
   1.403 +         \<forall>n. g(Suc n) \<le> g(n);
   1.404 +         \<forall>n. f(n) \<le> g(n) |]
   1.405 +      ==> Bseq (g :: nat \<Rightarrow> real)"
   1.406 +apply (subst Bseq_minus_iff [symmetric])
   1.407 +apply (rule_tac g = "%x. - (f x)" in f_inc_g_dec_Beq_f)
   1.408 +apply auto
   1.409 +done
   1.410 +
   1.411 +lemma f_inc_imp_le_lim:
   1.412 +  fixes f :: "nat \<Rightarrow> real"
   1.413 +  shows "\<lbrakk>\<forall>n. f n \<le> f (Suc n); convergent f\<rbrakk> \<Longrightarrow> f n \<le> lim f"
   1.414 +apply (rule linorder_not_less [THEN iffD1])
   1.415 +apply (auto simp add: convergent_LIMSEQ_iff LIMSEQ_iff monoseq_Suc)
   1.416 +apply (drule real_less_sum_gt_zero)
   1.417 +apply (drule_tac x = "f n + - lim f" in spec, safe)
   1.418 +apply (drule_tac P = "%na. no\<le>na --> ?Q na" and x = "no + n" in spec, auto)
   1.419 +apply (subgoal_tac "lim f \<le> f (no + n) ")
   1.420 +apply (drule_tac no=no and m=n in lemma_f_mono_add)
   1.421 +apply (auto simp add: add_commute)
   1.422 +apply (induct_tac "no")
   1.423 +apply simp
   1.424 +apply (auto intro: order_trans simp add: diff_minus abs_if)
   1.425 +done
   1.426 +
   1.427 +lemma lim_uminus: "convergent g ==> lim (%x. - g x) = - (lim g)"
   1.428 +apply (rule LIMSEQ_minus [THEN limI])
   1.429 +apply (simp add: convergent_LIMSEQ_iff)
   1.430 +done
   1.431 +
   1.432 +lemma g_dec_imp_lim_le:
   1.433 +  fixes g :: "nat \<Rightarrow> real"
   1.434 +  shows "\<lbrakk>\<forall>n. g (Suc n) \<le> g(n); convergent g\<rbrakk> \<Longrightarrow> lim g \<le> g n"
   1.435 +apply (subgoal_tac "- (g n) \<le> - (lim g) ")
   1.436 +apply (cut_tac [2] f = "%x. - (g x)" in f_inc_imp_le_lim)
   1.437 +apply (auto simp add: lim_uminus convergent_minus_iff [symmetric])
   1.438 +done
   1.439 +
   1.440 +lemma lemma_nest: "[| \<forall>n. f(n) \<le> f(Suc n);
   1.441 +         \<forall>n. g(Suc n) \<le> g(n);
   1.442 +         \<forall>n. f(n) \<le> g(n) |]
   1.443 +      ==> \<exists>l m :: real. l \<le> m &  ((\<forall>n. f(n) \<le> l) & f ----> l) &
   1.444 +                            ((\<forall>n. m \<le> g(n)) & g ----> m)"
   1.445 +apply (subgoal_tac "monoseq f & monoseq g")
   1.446 +prefer 2 apply (force simp add: LIMSEQ_iff monoseq_Suc)
   1.447 +apply (subgoal_tac "Bseq f & Bseq g")
   1.448 +prefer 2 apply (blast intro: f_inc_g_dec_Beq_f f_inc_g_dec_Beq_g)
   1.449 +apply (auto dest!: Bseq_monoseq_convergent simp add: convergent_LIMSEQ_iff)
   1.450 +apply (rule_tac x = "lim f" in exI)
   1.451 +apply (rule_tac x = "lim g" in exI)
   1.452 +apply (auto intro: LIMSEQ_le)
   1.453 +apply (auto simp add: f_inc_imp_le_lim g_dec_imp_lim_le convergent_LIMSEQ_iff)
   1.454 +done
   1.455 +
   1.456 +lemma lemma_nest_unique: "[| \<forall>n. f(n) \<le> f(Suc n);
   1.457 +         \<forall>n. g(Suc n) \<le> g(n);
   1.458 +         \<forall>n. f(n) \<le> g(n);
   1.459 +         (%n. f(n) - g(n)) ----> 0 |]
   1.460 +      ==> \<exists>l::real. ((\<forall>n. f(n) \<le> l) & f ----> l) &
   1.461 +                ((\<forall>n. l \<le> g(n)) & g ----> l)"
   1.462 +apply (drule lemma_nest, auto)
   1.463 +apply (subgoal_tac "l = m")
   1.464 +apply (drule_tac [2] X = f in LIMSEQ_diff)
   1.465 +apply (auto intro: LIMSEQ_unique)
   1.466 +done
   1.467 +
   1.468 +text{*The universal quantifiers below are required for the declaration
   1.469 +  of @{text Bolzano_nest_unique} below.*}
   1.470 +
   1.471 +lemma Bolzano_bisect_le:
   1.472 + "a \<le> b ==> \<forall>n. fst (Bolzano_bisect P a b n) \<le> snd (Bolzano_bisect P a b n)"
   1.473 +apply (rule allI)
   1.474 +apply (induct_tac "n")
   1.475 +apply (auto simp add: Let_def split_def)
   1.476 +done
   1.477 +
   1.478 +lemma Bolzano_bisect_fst_le_Suc: "a \<le> b ==>
   1.479 +   \<forall>n. fst(Bolzano_bisect P a b n) \<le> fst (Bolzano_bisect P a b (Suc n))"
   1.480 +apply (rule allI)
   1.481 +apply (induct_tac "n")
   1.482 +apply (auto simp add: Bolzano_bisect_le Let_def split_def)
   1.483 +done
   1.484 +
   1.485 +lemma Bolzano_bisect_Suc_le_snd: "a \<le> b ==>
   1.486 +   \<forall>n. snd(Bolzano_bisect P a b (Suc n)) \<le> snd (Bolzano_bisect P a b n)"
   1.487 +apply (rule allI)
   1.488 +apply (induct_tac "n")
   1.489 +apply (auto simp add: Bolzano_bisect_le Let_def split_def)
   1.490 +done
   1.491 +
   1.492 +lemma eq_divide_2_times_iff: "((x::real) = y / (2 * z)) = (2 * x = y/z)"
   1.493 +apply (auto)
   1.494 +apply (drule_tac f = "%u. (1/2) *u" in arg_cong)
   1.495 +apply (simp)
   1.496 +done
   1.497 +
   1.498 +lemma Bolzano_bisect_diff:
   1.499 +     "a \<le> b ==>
   1.500 +      snd(Bolzano_bisect P a b n) - fst(Bolzano_bisect P a b n) =
   1.501 +      (b-a) / (2 ^ n)"
   1.502 +apply (induct "n")
   1.503 +apply (auto simp add: eq_divide_2_times_iff add_divide_distrib Let_def split_def)
   1.504 +done
   1.505 +
   1.506 +lemmas Bolzano_nest_unique =
   1.507 +    lemma_nest_unique
   1.508 +    [OF Bolzano_bisect_fst_le_Suc Bolzano_bisect_Suc_le_snd Bolzano_bisect_le]
   1.509 +
   1.510 +
   1.511 +lemma not_P_Bolzano_bisect:
   1.512 +  assumes P:    "!!a b c. [| P(a,b); P(b,c); a \<le> b; b \<le> c|] ==> P(a,c)"
   1.513 +      and notP: "~ P(a,b)"
   1.514 +      and le:   "a \<le> b"
   1.515 +  shows "~ P(fst(Bolzano_bisect P a b n), snd(Bolzano_bisect P a b n))"
   1.516 +proof (induct n)
   1.517 +  case 0 show ?case using notP by simp
   1.518 + next
   1.519 +  case (Suc n)
   1.520 +  thus ?case
   1.521 + by (auto simp del: surjective_pairing [symmetric]
   1.522 +             simp add: Let_def split_def Bolzano_bisect_le [OF le]
   1.523 +     P [of "fst (Bolzano_bisect P a b n)" _ "snd (Bolzano_bisect P a b n)"])
   1.524 +qed
   1.525 +
   1.526 +(*Now we re-package P_prem as a formula*)
   1.527 +lemma not_P_Bolzano_bisect':
   1.528 +     "[| \<forall>a b c. P(a,b) & P(b,c) & a \<le> b & b \<le> c --> P(a,c);
   1.529 +         ~ P(a,b);  a \<le> b |] ==>
   1.530 +      \<forall>n. ~ P(fst(Bolzano_bisect P a b n), snd(Bolzano_bisect P a b n))"
   1.531 +by (blast elim!: not_P_Bolzano_bisect [THEN [2] rev_notE])
   1.532 +
   1.533 +
   1.534 +
   1.535 +lemma lemma_BOLZANO:
   1.536 +     "[| \<forall>a b c. P(a,b) & P(b,c) & a \<le> b & b \<le> c --> P(a,c);
   1.537 +         \<forall>x. \<exists>d::real. 0 < d &
   1.538 +                (\<forall>a b. a \<le> x & x \<le> b & (b-a) < d --> P(a,b));
   1.539 +         a \<le> b |]
   1.540 +      ==> P(a,b)"
   1.541 +apply (rule Bolzano_nest_unique [where P1=P, THEN exE], assumption+)
   1.542 +apply (rule LIMSEQ_minus_cancel)
   1.543 +apply (simp (no_asm_simp) add: Bolzano_bisect_diff LIMSEQ_divide_realpow_zero)
   1.544 +apply (rule ccontr)
   1.545 +apply (drule not_P_Bolzano_bisect', assumption+)
   1.546 +apply (rename_tac "l")
   1.547 +apply (drule_tac x = l in spec, clarify)
   1.548 +apply (simp add: LIMSEQ_def)
   1.549 +apply (drule_tac P = "%r. 0<r --> ?Q r" and x = "d/2" in spec)
   1.550 +apply (drule_tac P = "%r. 0<r --> ?Q r" and x = "d/2" in spec)
   1.551 +apply (drule real_less_half_sum, auto)
   1.552 +apply (drule_tac x = "fst (Bolzano_bisect P a b (no + noa))" in spec)
   1.553 +apply (drule_tac x = "snd (Bolzano_bisect P a b (no + noa))" in spec)
   1.554 +apply safe
   1.555 +apply (simp_all (no_asm_simp))
   1.556 +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)
   1.557 +apply (simp (no_asm_simp) add: abs_if)
   1.558 +apply (rule real_sum_of_halves [THEN subst])
   1.559 +apply (rule add_strict_mono)
   1.560 +apply (simp_all add: diff_minus [symmetric])
   1.561 +done
   1.562 +
   1.563 +
   1.564 +lemma lemma_BOLZANO2: "((\<forall>a b c. (a \<le> b & b \<le> c & P(a,b) & P(b,c)) --> P(a,c)) &
   1.565 +       (\<forall>x. \<exists>d::real. 0 < d &
   1.566 +                (\<forall>a b. a \<le> x & x \<le> b & (b-a) < d --> P(a,b))))
   1.567 +      --> (\<forall>a b. a \<le> b --> P(a,b))"
   1.568 +apply clarify
   1.569 +apply (blast intro: lemma_BOLZANO)
   1.570 +done
   1.571 +
   1.572 +
   1.573 +subsection {* Intermediate Value Theorem *}
   1.574 +
   1.575 +text {*Prove Contrapositive by Bisection*}
   1.576 +
   1.577 +lemma IVT: "[| f(a::real) \<le> (y::real); y \<le> f(b);
   1.578 +         a \<le> b;
   1.579 +         (\<forall>x. a \<le> x & x \<le> b --> isCont f x) |]
   1.580 +      ==> \<exists>x. a \<le> x & x \<le> b & f(x) = y"
   1.581 +apply (rule contrapos_pp, assumption)
   1.582 +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)
   1.583 +apply safe
   1.584 +apply simp_all
   1.585 +apply (simp add: isCont_iff LIM_def)
   1.586 +apply (rule ccontr)
   1.587 +apply (subgoal_tac "a \<le> x & x \<le> b")
   1.588 + prefer 2
   1.589 + apply simp
   1.590 + apply (drule_tac P = "%d. 0<d --> ?P d" and x = 1 in spec, arith)
   1.591 +apply (drule_tac x = x in spec)+
   1.592 +apply simp
   1.593 +apply (drule_tac P = "%r. ?P r --> (\<exists>s>0. ?Q r s) " and x = "\<bar>y - f x\<bar>" in spec)
   1.594 +apply safe
   1.595 +apply simp
   1.596 +apply (drule_tac x = s in spec, clarify)
   1.597 +apply (cut_tac x = "f x" and y = y in linorder_less_linear, safe)
   1.598 +apply (drule_tac x = "ba-x" in spec)
   1.599 +apply (simp_all add: abs_if)
   1.600 +apply (drule_tac x = "aa-x" in spec)
   1.601 +apply (case_tac "x \<le> aa", simp_all)
   1.602 +done
   1.603 +
   1.604 +lemma IVT2: "[| f(b::real) \<le> (y::real); y \<le> f(a);
   1.605 +         a \<le> b;
   1.606 +         (\<forall>x. a \<le> x & x \<le> b --> isCont f x)
   1.607 +      |] ==> \<exists>x. a \<le> x & x \<le> b & f(x) = y"
   1.608 +apply (subgoal_tac "- f a \<le> -y & -y \<le> - f b", clarify)
   1.609 +apply (drule IVT [where f = "%x. - f x"], assumption)
   1.610 +apply (auto intro: isCont_minus)
   1.611 +done
   1.612 +
   1.613 +(*HOL style here: object-level formulations*)
   1.614 +lemma IVT_objl: "(f(a::real) \<le> (y::real) & y \<le> f(b) & a \<le> b &
   1.615 +      (\<forall>x. a \<le> x & x \<le> b --> isCont f x))
   1.616 +      --> (\<exists>x. a \<le> x & x \<le> b & f(x) = y)"
   1.617 +apply (blast intro: IVT)
   1.618 +done
   1.619 +
   1.620 +lemma IVT2_objl: "(f(b::real) \<le> (y::real) & y \<le> f(a) & a \<le> b &
   1.621 +      (\<forall>x. a \<le> x & x \<le> b --> isCont f x))
   1.622 +      --> (\<exists>x. a \<le> x & x \<le> b & f(x) = y)"
   1.623 +apply (blast intro: IVT2)
   1.624 +done
   1.625 +
   1.626 +text{*By bisection, function continuous on closed interval is bounded above*}
   1.627 +
   1.628 +lemma isCont_bounded:
   1.629 +     "[| a \<le> b; \<forall>x. a \<le> x & x \<le> b --> isCont f x |]
   1.630 +      ==> \<exists>M::real. \<forall>x::real. a \<le> x & x \<le> b --> f(x) \<le> M"
   1.631 +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)
   1.632 +apply safe
   1.633 +apply simp_all
   1.634 +apply (rename_tac x xa ya M Ma)
   1.635 +apply (cut_tac x = M and y = Ma in linorder_linear, safe)
   1.636 +apply (rule_tac x = Ma in exI, clarify)
   1.637 +apply (cut_tac x = xb and y = xa in linorder_linear, force)
   1.638 +apply (rule_tac x = M in exI, clarify)
   1.639 +apply (cut_tac x = xb and y = xa in linorder_linear, force)
   1.640 +apply (case_tac "a \<le> x & x \<le> b")
   1.641 +apply (rule_tac [2] x = 1 in exI)
   1.642 +prefer 2 apply force
   1.643 +apply (simp add: LIM_def isCont_iff)
   1.644 +apply (drule_tac x = x in spec, auto)
   1.645 +apply (erule_tac V = "\<forall>M. \<exists>x. a \<le> x & x \<le> b & ~ f x \<le> M" in thin_rl)
   1.646 +apply (drule_tac x = 1 in spec, auto)
   1.647 +apply (rule_tac x = s in exI, clarify)
   1.648 +apply (rule_tac x = "\<bar>f x\<bar> + 1" in exI, clarify)
   1.649 +apply (drule_tac x = "xa-x" in spec)
   1.650 +apply (auto simp add: abs_ge_self)
   1.651 +done
   1.652 +
   1.653 +text{*Refine the above to existence of least upper bound*}
   1.654 +
   1.655 +lemma lemma_reals_complete: "((\<exists>x. x \<in> S) & (\<exists>y. isUb UNIV S (y::real))) -->
   1.656 +      (\<exists>t. isLub UNIV S t)"
   1.657 +by (blast intro: reals_complete)
   1.658 +
   1.659 +lemma isCont_has_Ub: "[| a \<le> b; \<forall>x. a \<le> x & x \<le> b --> isCont f x |]
   1.660 +         ==> \<exists>M::real. (\<forall>x::real. a \<le> x & x \<le> b --> f(x) \<le> M) &
   1.661 +                   (\<forall>N. N < M --> (\<exists>x. a \<le> x & x \<le> b & N < f(x)))"
   1.662 +apply (cut_tac S = "Collect (%y. \<exists>x. a \<le> x & x \<le> b & y = f x)"
   1.663 +        in lemma_reals_complete)
   1.664 +apply auto
   1.665 +apply (drule isCont_bounded, assumption)
   1.666 +apply (auto simp add: isUb_def leastP_def isLub_def setge_def setle_def)
   1.667 +apply (rule exI, auto)
   1.668 +apply (auto dest!: spec simp add: linorder_not_less)
   1.669 +done
   1.670 +
   1.671 +text{*Now show that it attains its upper bound*}
   1.672 +
   1.673 +lemma isCont_eq_Ub:
   1.674 +  assumes le: "a \<le> b"
   1.675 +      and con: "\<forall>x::real. a \<le> x & x \<le> b --> isCont f x"
   1.676 +  shows "\<exists>M::real. (\<forall>x. a \<le> x & x \<le> b --> f(x) \<le> M) &
   1.677 +             (\<exists>x. a \<le> x & x \<le> b & f(x) = M)"
   1.678 +proof -
   1.679 +  from isCont_has_Ub [OF le con]
   1.680 +  obtain M where M1: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M"
   1.681 +             and M2: "!!N. N<M ==> \<exists>x. a \<le> x \<and> x \<le> b \<and> N < f x"  by blast
   1.682 +  show ?thesis
   1.683 +  proof (intro exI, intro conjI)
   1.684 +    show " \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M" by (rule M1)
   1.685 +    show "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = M"
   1.686 +    proof (rule ccontr)
   1.687 +      assume "\<not> (\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = M)"
   1.688 +      with M1 have M3: "\<forall>x. a \<le> x & x \<le> b --> f x < M"
   1.689 +        by (fastsimp simp add: linorder_not_le [symmetric])
   1.690 +      hence "\<forall>x. a \<le> x & x \<le> b --> isCont (%x. inverse (M - f x)) x"
   1.691 +        by (auto simp add: isCont_inverse isCont_diff con)
   1.692 +      from isCont_bounded [OF le this]
   1.693 +      obtain k where k: "!!x. a \<le> x & x \<le> b --> inverse (M - f x) \<le> k" by auto
   1.694 +      have Minv: "!!x. a \<le> x & x \<le> b --> 0 < inverse (M - f (x))"
   1.695 +        by (simp add: M3 compare_rls)
   1.696 +      have "!!x. a \<le> x & x \<le> b --> inverse (M - f x) < k+1" using k
   1.697 +        by (auto intro: order_le_less_trans [of _ k])
   1.698 +      with Minv
   1.699 +      have "!!x. a \<le> x & x \<le> b --> inverse(k+1) < inverse(inverse(M - f x))"
   1.700 +        by (intro strip less_imp_inverse_less, simp_all)
   1.701 +      hence invlt: "!!x. a \<le> x & x \<le> b --> inverse(k+1) < M - f x"
   1.702 +        by simp
   1.703 +      have "M - inverse (k+1) < M" using k [of a] Minv [of a] le
   1.704 +        by (simp, arith)
   1.705 +      from M2 [OF this]
   1.706 +      obtain x where ax: "a \<le> x & x \<le> b & M - inverse(k+1) < f x" ..
   1.707 +      thus False using invlt [of x] by force
   1.708 +    qed
   1.709 +  qed
   1.710 +qed
   1.711 +
   1.712 +
   1.713 +text{*Same theorem for lower bound*}
   1.714 +
   1.715 +lemma isCont_eq_Lb: "[| a \<le> b; \<forall>x. a \<le> x & x \<le> b --> isCont f x |]
   1.716 +         ==> \<exists>M::real. (\<forall>x::real. a \<le> x & x \<le> b --> M \<le> f(x)) &
   1.717 +                   (\<exists>x. a \<le> x & x \<le> b & f(x) = M)"
   1.718 +apply (subgoal_tac "\<forall>x. a \<le> x & x \<le> b --> isCont (%x. - (f x)) x")
   1.719 +prefer 2 apply (blast intro: isCont_minus)
   1.720 +apply (drule_tac f = "(%x. - (f x))" in isCont_eq_Ub)
   1.721 +apply safe
   1.722 +apply auto
   1.723 +done
   1.724 +
   1.725 +
   1.726 +text{*Another version.*}
   1.727 +
   1.728 +lemma isCont_Lb_Ub: "[|a \<le> b; \<forall>x. a \<le> x & x \<le> b --> isCont f x |]
   1.729 +      ==> \<exists>L M::real. (\<forall>x::real. a \<le> x & x \<le> b --> L \<le> f(x) & f(x) \<le> M) &
   1.730 +          (\<forall>y. L \<le> y & y \<le> M --> (\<exists>x. a \<le> x & x \<le> b & (f(x) = y)))"
   1.731 +apply (frule isCont_eq_Lb)
   1.732 +apply (frule_tac [2] isCont_eq_Ub)
   1.733 +apply (assumption+, safe)
   1.734 +apply (rule_tac x = "f x" in exI)
   1.735 +apply (rule_tac x = "f xa" in exI, simp, safe)
   1.736 +apply (cut_tac x = x and y = xa in linorder_linear, safe)
   1.737 +apply (cut_tac f = f and a = x and b = xa and y = y in IVT_objl)
   1.738 +apply (cut_tac [2] f = f and a = xa and b = x and y = y in IVT2_objl, safe)
   1.739 +apply (rule_tac [2] x = xb in exI)
   1.740 +apply (rule_tac [4] x = xb in exI, simp_all)
   1.741 +done
   1.742 +
   1.743 +
   1.744 +text{*If @{term "0 < f'(x)"} then @{term x} is Locally Strictly Increasing At The Right*}
   1.745 +
   1.746 +lemma DERIV_left_inc:
   1.747 +  fixes f :: "real => real"
   1.748 +  assumes der: "DERIV f x :> l"
   1.749 +      and l:   "0 < l"
   1.750 +  shows "\<exists>d > 0. \<forall>h > 0. h < d --> f(x) < f(x + h)"
   1.751 +proof -
   1.752 +  from l der [THEN DERIV_D, THEN LIM_D [where r = "l"]]
   1.753 +  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)"
   1.754 +    by (simp add: diff_minus)
   1.755 +  then obtain s
   1.756 +        where s:   "0 < s"
   1.757 +          and all: "!!z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < l"
   1.758 +    by auto
   1.759 +  thus ?thesis
   1.760 +  proof (intro exI conjI strip)
   1.761 +    show "0<s" using s .
   1.762 +    fix h::real
   1.763 +    assume "0 < h" "h < s"
   1.764 +    with all [of h] show "f x < f (x+h)"
   1.765 +    proof (simp add: abs_if pos_less_divide_eq diff_minus [symmetric]
   1.766 +    split add: split_if_asm)
   1.767 +      assume "~ (f (x+h) - f x) / h < l" and h: "0 < h"
   1.768 +      with l
   1.769 +      have "0 < (f (x+h) - f x) / h" by arith
   1.770 +      thus "f x < f (x+h)"
   1.771 +  by (simp add: pos_less_divide_eq h)
   1.772 +    qed
   1.773 +  qed
   1.774 +qed
   1.775 +
   1.776 +lemma DERIV_left_dec:
   1.777 +  fixes f :: "real => real"
   1.778 +  assumes der: "DERIV f x :> l"
   1.779 +      and l:   "l < 0"
   1.780 +  shows "\<exists>d > 0. \<forall>h > 0. h < d --> f(x) < f(x-h)"
   1.781 +proof -
   1.782 +  from l der [THEN DERIV_D, THEN LIM_D [where r = "-l"]]
   1.783 +  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)"
   1.784 +    by (simp add: diff_minus)
   1.785 +  then obtain s
   1.786 +        where s:   "0 < s"
   1.787 +          and all: "!!z. z \<noteq> 0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>(f(x+z) - f x) / z - l\<bar> < -l"
   1.788 +    by auto
   1.789 +  thus ?thesis
   1.790 +  proof (intro exI conjI strip)
   1.791 +    show "0<s" using s .
   1.792 +    fix h::real
   1.793 +    assume "0 < h" "h < s"
   1.794 +    with all [of "-h"] show "f x < f (x-h)"
   1.795 +    proof (simp add: abs_if pos_less_divide_eq diff_minus [symmetric]
   1.796 +    split add: split_if_asm)
   1.797 +      assume " - ((f (x-h) - f x) / h) < l" and h: "0 < h"
   1.798 +      with l
   1.799 +      have "0 < (f (x-h) - f x) / h" by arith
   1.800 +      thus "f x < f (x-h)"
   1.801 +  by (simp add: pos_less_divide_eq h)
   1.802 +    qed
   1.803 +  qed
   1.804 +qed
   1.805 +
   1.806 +lemma DERIV_local_max:
   1.807 +  fixes f :: "real => real"
   1.808 +  assumes der: "DERIV f x :> l"
   1.809 +      and d:   "0 < d"
   1.810 +      and le:  "\<forall>y. \<bar>x-y\<bar> < d --> f(y) \<le> f(x)"
   1.811 +  shows "l = 0"
   1.812 +proof (cases rule: linorder_cases [of l 0])
   1.813 +  case equal thus ?thesis .
   1.814 +next
   1.815 +  case less
   1.816 +  from DERIV_left_dec [OF der less]
   1.817 +  obtain d' where d': "0 < d'"
   1.818 +             and lt: "\<forall>h > 0. h < d' \<longrightarrow> f x < f (x-h)" by blast
   1.819 +  from real_lbound_gt_zero [OF d d']
   1.820 +  obtain e where "0 < e \<and> e < d \<and> e < d'" ..
   1.821 +  with lt le [THEN spec [where x="x-e"]]
   1.822 +  show ?thesis by (auto simp add: abs_if)
   1.823 +next
   1.824 +  case greater
   1.825 +  from DERIV_left_inc [OF der greater]
   1.826 +  obtain d' where d': "0 < d'"
   1.827 +             and lt: "\<forall>h > 0. h < d' \<longrightarrow> f x < f (x + h)" by blast
   1.828 +  from real_lbound_gt_zero [OF d d']
   1.829 +  obtain e where "0 < e \<and> e < d \<and> e < d'" ..
   1.830 +  with lt le [THEN spec [where x="x+e"]]
   1.831 +  show ?thesis by (auto simp add: abs_if)
   1.832 +qed
   1.833 +
   1.834 +
   1.835 +text{*Similar theorem for a local minimum*}
   1.836 +lemma DERIV_local_min:
   1.837 +  fixes f :: "real => real"
   1.838 +  shows "[| DERIV f x :> l; 0 < d; \<forall>y. \<bar>x-y\<bar> < d --> f(x) \<le> f(y) |] ==> l = 0"
   1.839 +by (drule DERIV_minus [THEN DERIV_local_max], auto)
   1.840 +
   1.841 +
   1.842 +text{*In particular, if a function is locally flat*}
   1.843 +lemma DERIV_local_const:
   1.844 +  fixes f :: "real => real"
   1.845 +  shows "[| DERIV f x :> l; 0 < d; \<forall>y. \<bar>x-y\<bar> < d --> f(x) = f(y) |] ==> l = 0"
   1.846 +by (auto dest!: DERIV_local_max)
   1.847 +
   1.848 +text{*Lemma about introducing open ball in open interval*}
   1.849 +lemma lemma_interval_lt:
   1.850 +     "[| a < x;  x < b |]
   1.851 +      ==> \<exists>d::real. 0 < d & (\<forall>y. \<bar>x-y\<bar> < d --> a < y & y < b)"
   1.852 +
   1.853 +apply (simp add: abs_less_iff)
   1.854 +apply (insert linorder_linear [of "x-a" "b-x"], safe)
   1.855 +apply (rule_tac x = "x-a" in exI)
   1.856 +apply (rule_tac [2] x = "b-x" in exI, auto)
   1.857 +done
   1.858 +
   1.859 +lemma lemma_interval: "[| a < x;  x < b |] ==>
   1.860 +        \<exists>d::real. 0 < d &  (\<forall>y. \<bar>x-y\<bar> < d --> a \<le> y & y \<le> b)"
   1.861 +apply (drule lemma_interval_lt, auto)
   1.862 +apply (auto intro!: exI)
   1.863 +done
   1.864 +
   1.865 +text{*Rolle's Theorem.
   1.866 +   If @{term f} is defined and continuous on the closed interval
   1.867 +   @{text "[a,b]"} and differentiable on the open interval @{text "(a,b)"},
   1.868 +   and @{term "f(a) = f(b)"},
   1.869 +   then there exists @{text "x0 \<in> (a,b)"} such that @{term "f'(x0) = 0"}*}
   1.870 +theorem Rolle:
   1.871 +  assumes lt: "a < b"
   1.872 +      and eq: "f(a) = f(b)"
   1.873 +      and con: "\<forall>x. a \<le> x & x \<le> b --> isCont f x"
   1.874 +      and dif [rule_format]: "\<forall>x. a < x & x < b --> f differentiable x"
   1.875 +  shows "\<exists>z::real. a < z & z < b & DERIV f z :> 0"
   1.876 +proof -
   1.877 +  have le: "a \<le> b" using lt by simp
   1.878 +  from isCont_eq_Ub [OF le con]
   1.879 +  obtain x where x_max: "\<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> f z \<le> f x"
   1.880 +             and alex: "a \<le> x" and xleb: "x \<le> b"
   1.881 +    by blast
   1.882 +  from isCont_eq_Lb [OF le con]
   1.883 +  obtain x' where x'_min: "\<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> f x' \<le> f z"
   1.884 +              and alex': "a \<le> x'" and x'leb: "x' \<le> b"
   1.885 +    by blast
   1.886 +  show ?thesis
   1.887 +  proof cases
   1.888 +    assume axb: "a < x & x < b"
   1.889 +        --{*@{term f} attains its maximum within the interval*}
   1.890 +    hence ax: "a<x" and xb: "x<b" by arith + 
   1.891 +    from lemma_interval [OF ax xb]
   1.892 +    obtain d where d: "0<d" and bound: "\<forall>y. \<bar>x-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
   1.893 +      by blast
   1.894 +    hence bound': "\<forall>y. \<bar>x-y\<bar> < d \<longrightarrow> f y \<le> f x" using x_max
   1.895 +      by blast
   1.896 +    from differentiableD [OF dif [OF axb]]
   1.897 +    obtain l where der: "DERIV f x :> l" ..
   1.898 +    have "l=0" by (rule DERIV_local_max [OF der d bound'])
   1.899 +        --{*the derivative at a local maximum is zero*}
   1.900 +    thus ?thesis using ax xb der by auto
   1.901 +  next
   1.902 +    assume notaxb: "~ (a < x & x < b)"
   1.903 +    hence xeqab: "x=a | x=b" using alex xleb by arith
   1.904 +    hence fb_eq_fx: "f b = f x" by (auto simp add: eq)
   1.905 +    show ?thesis
   1.906 +    proof cases
   1.907 +      assume ax'b: "a < x' & x' < b"
   1.908 +        --{*@{term f} attains its minimum within the interval*}
   1.909 +      hence ax': "a<x'" and x'b: "x'<b" by arith+ 
   1.910 +      from lemma_interval [OF ax' x'b]
   1.911 +      obtain d where d: "0<d" and bound: "\<forall>y. \<bar>x'-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
   1.912 +  by blast
   1.913 +      hence bound': "\<forall>y. \<bar>x'-y\<bar> < d \<longrightarrow> f x' \<le> f y" using x'_min
   1.914 +  by blast
   1.915 +      from differentiableD [OF dif [OF ax'b]]
   1.916 +      obtain l where der: "DERIV f x' :> l" ..
   1.917 +      have "l=0" by (rule DERIV_local_min [OF der d bound'])
   1.918 +        --{*the derivative at a local minimum is zero*}
   1.919 +      thus ?thesis using ax' x'b der by auto
   1.920 +    next
   1.921 +      assume notax'b: "~ (a < x' & x' < b)"
   1.922 +        --{*@{term f} is constant througout the interval*}
   1.923 +      hence x'eqab: "x'=a | x'=b" using alex' x'leb by arith
   1.924 +      hence fb_eq_fx': "f b = f x'" by (auto simp add: eq)
   1.925 +      from dense [OF lt]
   1.926 +      obtain r where ar: "a < r" and rb: "r < b" by blast
   1.927 +      from lemma_interval [OF ar rb]
   1.928 +      obtain d where d: "0<d" and bound: "\<forall>y. \<bar>r-y\<bar> < d \<longrightarrow> a \<le> y \<and> y \<le> b"
   1.929 +  by blast
   1.930 +      have eq_fb: "\<forall>z. a \<le> z --> z \<le> b --> f z = f b"
   1.931 +      proof (clarify)
   1.932 +        fix z::real
   1.933 +        assume az: "a \<le> z" and zb: "z \<le> b"
   1.934 +        show "f z = f b"
   1.935 +        proof (rule order_antisym)
   1.936 +          show "f z \<le> f b" by (simp add: fb_eq_fx x_max az zb)
   1.937 +          show "f b \<le> f z" by (simp add: fb_eq_fx' x'_min az zb)
   1.938 +        qed
   1.939 +      qed
   1.940 +      have bound': "\<forall>y. \<bar>r-y\<bar> < d \<longrightarrow> f r = f y"
   1.941 +      proof (intro strip)
   1.942 +        fix y::real
   1.943 +        assume lt: "\<bar>r-y\<bar> < d"
   1.944 +        hence "f y = f b" by (simp add: eq_fb bound)
   1.945 +        thus "f r = f y" by (simp add: eq_fb ar rb order_less_imp_le)
   1.946 +      qed
   1.947 +      from differentiableD [OF dif [OF conjI [OF ar rb]]]
   1.948 +      obtain l where der: "DERIV f r :> l" ..
   1.949 +      have "l=0" by (rule DERIV_local_const [OF der d bound'])
   1.950 +        --{*the derivative of a constant function is zero*}
   1.951 +      thus ?thesis using ar rb der by auto
   1.952 +    qed
   1.953 +  qed
   1.954 +qed
   1.955 +
   1.956 +
   1.957 +subsection{*Mean Value Theorem*}
   1.958 +
   1.959 +lemma lemma_MVT:
   1.960 +     "f a - (f b - f a)/(b-a) * a = f b - (f b - f a)/(b-a) * (b::real)"
   1.961 +proof cases
   1.962 +  assume "a=b" thus ?thesis by simp
   1.963 +next
   1.964 +  assume "a\<noteq>b"
   1.965 +  hence ba: "b-a \<noteq> 0" by arith
   1.966 +  show ?thesis
   1.967 +    by (rule real_mult_left_cancel [OF ba, THEN iffD1],
   1.968 +        simp add: right_diff_distrib,
   1.969 +        simp add: left_diff_distrib)
   1.970 +qed
   1.971 +
   1.972 +theorem MVT:
   1.973 +  assumes lt:  "a < b"
   1.974 +      and con: "\<forall>x. a \<le> x & x \<le> b --> isCont f x"
   1.975 +      and dif [rule_format]: "\<forall>x. a < x & x < b --> f differentiable x"
   1.976 +  shows "\<exists>l z::real. a < z & z < b & DERIV f z :> l &
   1.977 +                   (f(b) - f(a) = (b-a) * l)"
   1.978 +proof -
   1.979 +  let ?F = "%x. f x - ((f b - f a) / (b-a)) * x"
   1.980 +  have contF: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont ?F x" using con
   1.981 +    by (fast intro: isCont_diff isCont_const isCont_mult isCont_ident)
   1.982 +  have difF: "\<forall>x. a < x \<and> x < b \<longrightarrow> ?F differentiable x"
   1.983 +  proof (clarify)
   1.984 +    fix x::real
   1.985 +    assume ax: "a < x" and xb: "x < b"
   1.986 +    from differentiableD [OF dif [OF conjI [OF ax xb]]]
   1.987 +    obtain l where der: "DERIV f x :> l" ..
   1.988 +    show "?F differentiable x"
   1.989 +      by (rule differentiableI [where D = "l - (f b - f a)/(b-a)"],
   1.990 +          blast intro: DERIV_diff DERIV_cmult_Id der)
   1.991 +  qed
   1.992 +  from Rolle [where f = ?F, OF lt lemma_MVT contF difF]
   1.993 +  obtain z where az: "a < z" and zb: "z < b" and der: "DERIV ?F z :> 0"
   1.994 +    by blast
   1.995 +  have "DERIV (%x. ((f b - f a)/(b-a)) * x) z :> (f b - f a)/(b-a)"
   1.996 +    by (rule DERIV_cmult_Id)
   1.997 +  hence derF: "DERIV (\<lambda>x. ?F x + (f b - f a) / (b - a) * x) z
   1.998 +                   :> 0 + (f b - f a) / (b - a)"
   1.999 +    by (rule DERIV_add [OF der])
  1.1000 +  show ?thesis
  1.1001 +  proof (intro exI conjI)
  1.1002 +    show "a < z" using az .
  1.1003 +    show "z < b" using zb .
  1.1004 +    show "f b - f a = (b - a) * ((f b - f a)/(b-a))" by (simp)
  1.1005 +    show "DERIV f z :> ((f b - f a)/(b-a))"  using derF by simp
  1.1006 +  qed
  1.1007 +qed
  1.1008 +
  1.1009 +
  1.1010 +text{*A function is constant if its derivative is 0 over an interval.*}
  1.1011 +
  1.1012 +lemma DERIV_isconst_end:
  1.1013 +  fixes f :: "real => real"
  1.1014 +  shows "[| a < b;
  1.1015 +         \<forall>x. a \<le> x & x \<le> b --> isCont f x;
  1.1016 +         \<forall>x. a < x & x < b --> DERIV f x :> 0 |]
  1.1017 +        ==> f b = f a"
  1.1018 +apply (drule MVT, assumption)
  1.1019 +apply (blast intro: differentiableI)
  1.1020 +apply (auto dest!: DERIV_unique simp add: diff_eq_eq)
  1.1021 +done
  1.1022 +
  1.1023 +lemma DERIV_isconst1:
  1.1024 +  fixes f :: "real => real"
  1.1025 +  shows "[| a < b;
  1.1026 +         \<forall>x. a \<le> x & x \<le> b --> isCont f x;
  1.1027 +         \<forall>x. a < x & x < b --> DERIV f x :> 0 |]
  1.1028 +        ==> \<forall>x. a \<le> x & x \<le> b --> f x = f a"
  1.1029 +apply safe
  1.1030 +apply (drule_tac x = a in order_le_imp_less_or_eq, safe)
  1.1031 +apply (drule_tac b = x in DERIV_isconst_end, auto)
  1.1032 +done
  1.1033 +
  1.1034 +lemma DERIV_isconst2:
  1.1035 +  fixes f :: "real => real"
  1.1036 +  shows "[| a < b;
  1.1037 +         \<forall>x. a \<le> x & x \<le> b --> isCont f x;
  1.1038 +         \<forall>x. a < x & x < b --> DERIV f x :> 0;
  1.1039 +         a \<le> x; x \<le> b |]
  1.1040 +        ==> f x = f a"
  1.1041 +apply (blast dest: DERIV_isconst1)
  1.1042 +done
  1.1043 +
  1.1044 +lemma DERIV_isconst_all:
  1.1045 +  fixes f :: "real => real"
  1.1046 +  shows "\<forall>x. DERIV f x :> 0 ==> f(x) = f(y)"
  1.1047 +apply (rule linorder_cases [of x y])
  1.1048 +apply (blast intro: sym DERIV_isCont DERIV_isconst_end)+
  1.1049 +done
  1.1050 +
  1.1051 +lemma DERIV_const_ratio_const:
  1.1052 +  fixes f :: "real => real"
  1.1053 +  shows "[|a \<noteq> b; \<forall>x. DERIV f x :> k |] ==> (f(b) - f(a)) = (b-a) * k"
  1.1054 +apply (rule linorder_cases [of a b], auto)
  1.1055 +apply (drule_tac [!] f = f in MVT)
  1.1056 +apply (auto dest: DERIV_isCont DERIV_unique simp add: differentiable_def)
  1.1057 +apply (auto dest: DERIV_unique simp add: ring_distribs diff_minus)
  1.1058 +done
  1.1059 +
  1.1060 +lemma DERIV_const_ratio_const2:
  1.1061 +  fixes f :: "real => real"
  1.1062 +  shows "[|a \<noteq> b; \<forall>x. DERIV f x :> k |] ==> (f(b) - f(a))/(b-a) = k"
  1.1063 +apply (rule_tac c1 = "b-a" in real_mult_right_cancel [THEN iffD1])
  1.1064 +apply (auto dest!: DERIV_const_ratio_const simp add: mult_assoc)
  1.1065 +done
  1.1066 +
  1.1067 +lemma real_average_minus_first [simp]: "((a + b) /2 - a) = (b-a)/(2::real)"
  1.1068 +by (simp)
  1.1069 +
  1.1070 +lemma real_average_minus_second [simp]: "((b + a)/2 - a) = (b-a)/(2::real)"
  1.1071 +by (simp)
  1.1072 +
  1.1073 +text{*Gallileo's "trick": average velocity = av. of end velocities*}
  1.1074 +
  1.1075 +lemma DERIV_const_average:
  1.1076 +  fixes v :: "real => real"
  1.1077 +  assumes neq: "a \<noteq> (b::real)"
  1.1078 +      and der: "\<forall>x. DERIV v x :> k"
  1.1079 +  shows "v ((a + b)/2) = (v a + v b)/2"
  1.1080 +proof (cases rule: linorder_cases [of a b])
  1.1081 +  case equal with neq show ?thesis by simp
  1.1082 +next
  1.1083 +  case less
  1.1084 +  have "(v b - v a) / (b - a) = k"
  1.1085 +    by (rule DERIV_const_ratio_const2 [OF neq der])
  1.1086 +  hence "(b-a) * ((v b - v a) / (b-a)) = (b-a) * k" by simp
  1.1087 +  moreover have "(v ((a + b) / 2) - v a) / ((a + b) / 2 - a) = k"
  1.1088 +    by (rule DERIV_const_ratio_const2 [OF _ der], simp add: neq)
  1.1089 +  ultimately show ?thesis using neq by force
  1.1090 +next
  1.1091 +  case greater
  1.1092 +  have "(v b - v a) / (b - a) = k"
  1.1093 +    by (rule DERIV_const_ratio_const2 [OF neq der])
  1.1094 +  hence "(b-a) * ((v b - v a) / (b-a)) = (b-a) * k" by simp
  1.1095 +  moreover have " (v ((b + a) / 2) - v a) / ((b + a) / 2 - a) = k"
  1.1096 +    by (rule DERIV_const_ratio_const2 [OF _ der], simp add: neq)
  1.1097 +  ultimately show ?thesis using neq by (force simp add: add_commute)
  1.1098 +qed
  1.1099 +
  1.1100 +
  1.1101 +text{*Dull lemma: an continuous injection on an interval must have a
  1.1102 +strict maximum at an end point, not in the middle.*}
  1.1103 +
  1.1104 +lemma lemma_isCont_inj:
  1.1105 +  fixes f :: "real \<Rightarrow> real"
  1.1106 +  assumes d: "0 < d"
  1.1107 +      and inj [rule_format]: "\<forall>z. \<bar>z-x\<bar> \<le> d --> g(f z) = z"
  1.1108 +      and cont: "\<forall>z. \<bar>z-x\<bar> \<le> d --> isCont f z"
  1.1109 +  shows "\<exists>z. \<bar>z-x\<bar> \<le> d & f x < f z"
  1.1110 +proof (rule ccontr)
  1.1111 +  assume  "~ (\<exists>z. \<bar>z-x\<bar> \<le> d & f x < f z)"
  1.1112 +  hence all [rule_format]: "\<forall>z. \<bar>z - x\<bar> \<le> d --> f z \<le> f x" by auto
  1.1113 +  show False
  1.1114 +  proof (cases rule: linorder_le_cases [of "f(x-d)" "f(x+d)"])
  1.1115 +    case le
  1.1116 +    from d cont all [of "x+d"]
  1.1117 +    have flef: "f(x+d) \<le> f x"
  1.1118 +     and xlex: "x - d \<le> x"
  1.1119 +     and cont': "\<forall>z. x - d \<le> z \<and> z \<le> x \<longrightarrow> isCont f z"
  1.1120 +       by (auto simp add: abs_if)
  1.1121 +    from IVT [OF le flef xlex cont']
  1.1122 +    obtain x' where "x-d \<le> x'" "x' \<le> x" "f x' = f(x+d)" by blast
  1.1123 +    moreover
  1.1124 +    hence "g(f x') = g (f(x+d))" by simp
  1.1125 +    ultimately show False using d inj [of x'] inj [of "x+d"]
  1.1126 +      by (simp add: abs_le_iff)
  1.1127 +  next
  1.1128 +    case ge
  1.1129 +    from d cont all [of "x-d"]
  1.1130 +    have flef: "f(x-d) \<le> f x"
  1.1131 +     and xlex: "x \<le> x+d"
  1.1132 +     and cont': "\<forall>z. x \<le> z \<and> z \<le> x+d \<longrightarrow> isCont f z"
  1.1133 +       by (auto simp add: abs_if)
  1.1134 +    from IVT2 [OF ge flef xlex cont']
  1.1135 +    obtain x' where "x \<le> x'" "x' \<le> x+d" "f x' = f(x-d)" by blast
  1.1136 +    moreover
  1.1137 +    hence "g(f x') = g (f(x-d))" by simp
  1.1138 +    ultimately show False using d inj [of x'] inj [of "x-d"]
  1.1139 +      by (simp add: abs_le_iff)
  1.1140 +  qed
  1.1141 +qed
  1.1142 +
  1.1143 +
  1.1144 +text{*Similar version for lower bound.*}
  1.1145 +
  1.1146 +lemma lemma_isCont_inj2:
  1.1147 +  fixes f g :: "real \<Rightarrow> real"
  1.1148 +  shows "[|0 < d; \<forall>z. \<bar>z-x\<bar> \<le> d --> g(f z) = z;
  1.1149 +        \<forall>z. \<bar>z-x\<bar> \<le> d --> isCont f z |]
  1.1150 +      ==> \<exists>z. \<bar>z-x\<bar> \<le> d & f z < f x"
  1.1151 +apply (insert lemma_isCont_inj
  1.1152 +          [where f = "%x. - f x" and g = "%y. g(-y)" and x = x and d = d])
  1.1153 +apply (simp add: isCont_minus linorder_not_le)
  1.1154 +done
  1.1155 +
  1.1156 +text{*Show there's an interval surrounding @{term "f(x)"} in
  1.1157 +@{text "f[[x - d, x + d]]"} .*}
  1.1158 +
  1.1159 +lemma isCont_inj_range:
  1.1160 +  fixes f :: "real \<Rightarrow> real"
  1.1161 +  assumes d: "0 < d"
  1.1162 +      and inj: "\<forall>z. \<bar>z-x\<bar> \<le> d --> g(f z) = z"
  1.1163 +      and cont: "\<forall>z. \<bar>z-x\<bar> \<le> d --> isCont f z"
  1.1164 +  shows "\<exists>e>0. \<forall>y. \<bar>y - f x\<bar> \<le> e --> (\<exists>z. \<bar>z-x\<bar> \<le> d & f z = y)"
  1.1165 +proof -
  1.1166 +  have "x-d \<le> x+d" "\<forall>z. x-d \<le> z \<and> z \<le> x+d \<longrightarrow> isCont f z" using cont d
  1.1167 +    by (auto simp add: abs_le_iff)
  1.1168 +  from isCont_Lb_Ub [OF this]
  1.1169 +  obtain L M
  1.1170 +  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"
  1.1171 +    and all2 [rule_format]:
  1.1172 +           "\<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)"
  1.1173 +    by auto
  1.1174 +  with d have "L \<le> f x & f x \<le> M" by simp
  1.1175 +  moreover have "L \<noteq> f x"
  1.1176 +  proof -
  1.1177 +    from lemma_isCont_inj2 [OF d inj cont]
  1.1178 +    obtain u where "\<bar>u - x\<bar> \<le> d" "f u < f x"  by auto
  1.1179 +    thus ?thesis using all1 [of u] by arith
  1.1180 +  qed
  1.1181 +  moreover have "f x \<noteq> M"
  1.1182 +  proof -
  1.1183 +    from lemma_isCont_inj [OF d inj cont]
  1.1184 +    obtain u where "\<bar>u - x\<bar> \<le> d" "f x < f u"  by auto
  1.1185 +    thus ?thesis using all1 [of u] by arith
  1.1186 +  qed
  1.1187 +  ultimately have "L < f x & f x < M" by arith
  1.1188 +  hence "0 < f x - L" "0 < M - f x" by arith+
  1.1189 +  from real_lbound_gt_zero [OF this]
  1.1190 +  obtain e where e: "0 < e" "e < f x - L" "e < M - f x" by auto
  1.1191 +  thus ?thesis
  1.1192 +  proof (intro exI conjI)
  1.1193 +    show "0<e" using e(1) .
  1.1194 +    show "\<forall>y. \<bar>y - f x\<bar> \<le> e \<longrightarrow> (\<exists>z. \<bar>z - x\<bar> \<le> d \<and> f z = y)"
  1.1195 +    proof (intro strip)
  1.1196 +      fix y::real
  1.1197 +      assume "\<bar>y - f x\<bar> \<le> e"
  1.1198 +      with e have "L \<le> y \<and> y \<le> M" by arith
  1.1199 +      from all2 [OF this]
  1.1200 +      obtain z where "x - d \<le> z" "z \<le> x + d" "f z = y" by blast
  1.1201 +      thus "\<exists>z. \<bar>z - x\<bar> \<le> d \<and> f z = y" 
  1.1202 +        by (force simp add: abs_le_iff)
  1.1203 +    qed
  1.1204 +  qed
  1.1205 +qed
  1.1206 +
  1.1207 +
  1.1208 +text{*Continuity of inverse function*}
  1.1209 +
  1.1210 +lemma isCont_inverse_function:
  1.1211 +  fixes f g :: "real \<Rightarrow> real"
  1.1212 +  assumes d: "0 < d"
  1.1213 +      and inj: "\<forall>z. \<bar>z-x\<bar> \<le> d --> g(f z) = z"
  1.1214 +      and cont: "\<forall>z. \<bar>z-x\<bar> \<le> d --> isCont f z"
  1.1215 +  shows "isCont g (f x)"
  1.1216 +proof (simp add: isCont_iff LIM_eq)
  1.1217 +  show "\<forall>r. 0 < r \<longrightarrow>
  1.1218 +         (\<exists>s>0. \<forall>z. z\<noteq>0 \<and> \<bar>z\<bar> < s \<longrightarrow> \<bar>g(f x + z) - g(f x)\<bar> < r)"
  1.1219 +  proof (intro strip)
  1.1220 +    fix r::real
  1.1221 +    assume r: "0<r"
  1.1222 +    from real_lbound_gt_zero [OF r d]
  1.1223 +    obtain e where e: "0 < e" and e_lt: "e < r \<and> e < d" by blast
  1.1224 +    with inj cont
  1.1225 +    have e_simps: "\<forall>z. \<bar>z-x\<bar> \<le> e --> g (f z) = z"
  1.1226 +                  "\<forall>z. \<bar>z-x\<bar> \<le> e --> isCont f z"   by auto
  1.1227 +    from isCont_inj_range [OF e this]
  1.1228 +    obtain e' where e': "0 < e'"
  1.1229 +        and all: "\<forall>y. \<bar>y - f x\<bar> \<le> e' \<longrightarrow> (\<exists>z. \<bar>z - x\<bar> \<le> e \<and> f z = y)"
  1.1230 +          by blast
  1.1231 +    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"
  1.1232 +    proof (intro exI conjI)
  1.1233 +      show "0<e'" using e' .
  1.1234 +      show "\<forall>z. z \<noteq> 0 \<and> \<bar>z\<bar> < e' \<longrightarrow> \<bar>g (f x + z) - g (f x)\<bar> < r"
  1.1235 +      proof (intro strip)
  1.1236 +        fix z::real
  1.1237 +        assume z: "z \<noteq> 0 \<and> \<bar>z\<bar> < e'"
  1.1238 +        with e e_lt e_simps all [rule_format, of "f x + z"]
  1.1239 +        show "\<bar>g (f x + z) - g (f x)\<bar> < r" by force
  1.1240 +      qed
  1.1241 +    qed
  1.1242 +  qed
  1.1243 +qed
  1.1244 +
  1.1245 +text {* Derivative of inverse function *}
  1.1246 +
  1.1247 +lemma DERIV_inverse_function:
  1.1248 +  fixes f g :: "real \<Rightarrow> real"
  1.1249 +  assumes der: "DERIV f (g x) :> D"
  1.1250 +  assumes neq: "D \<noteq> 0"
  1.1251 +  assumes a: "a < x" and b: "x < b"
  1.1252 +  assumes inj: "\<forall>y. a < y \<and> y < b \<longrightarrow> f (g y) = y"
  1.1253 +  assumes cont: "isCont g x"
  1.1254 +  shows "DERIV g x :> inverse D"
  1.1255 +unfolding DERIV_iff2
  1.1256 +proof (rule LIM_equal2)
  1.1257 +  show "0 < min (x - a) (b - x)"
  1.1258 +    using a b by arith 
  1.1259 +next
  1.1260 +  fix y
  1.1261 +  assume "norm (y - x) < min (x - a) (b - x)"
  1.1262 +  hence "a < y" and "y < b" 
  1.1263 +    by (simp_all add: abs_less_iff)
  1.1264 +  thus "(g y - g x) / (y - x) =
  1.1265 +        inverse ((f (g y) - x) / (g y - g x))"
  1.1266 +    by (simp add: inj)
  1.1267 +next
  1.1268 +  have "(\<lambda>z. (f z - f (g x)) / (z - g x)) -- g x --> D"
  1.1269 +    by (rule der [unfolded DERIV_iff2])
  1.1270 +  hence 1: "(\<lambda>z. (f z - x) / (z - g x)) -- g x --> D"
  1.1271 +    using inj a b by simp
  1.1272 +  have 2: "\<exists>d>0. \<forall>y. y \<noteq> x \<and> norm (y - x) < d \<longrightarrow> g y \<noteq> g x"
  1.1273 +  proof (safe intro!: exI)
  1.1274 +    show "0 < min (x - a) (b - x)"
  1.1275 +      using a b by simp
  1.1276 +  next
  1.1277 +    fix y
  1.1278 +    assume "norm (y - x) < min (x - a) (b - x)"
  1.1279 +    hence y: "a < y" "y < b"
  1.1280 +      by (simp_all add: abs_less_iff)
  1.1281 +    assume "g y = g x"
  1.1282 +    hence "f (g y) = f (g x)" by simp
  1.1283 +    hence "y = x" using inj y a b by simp
  1.1284 +    also assume "y \<noteq> x"
  1.1285 +    finally show False by simp
  1.1286 +  qed
  1.1287 +  have "(\<lambda>y. (f (g y) - x) / (g y - g x)) -- x --> D"
  1.1288 +    using cont 1 2 by (rule isCont_LIM_compose2)
  1.1289 +  thus "(\<lambda>y. inverse ((f (g y) - x) / (g y - g x)))
  1.1290 +        -- x --> inverse D"
  1.1291 +    using neq by (rule LIM_inverse)
  1.1292 +qed
  1.1293 +
  1.1294 +theorem GMVT:
  1.1295 +  fixes a b :: real
  1.1296 +  assumes alb: "a < b"
  1.1297 +  and fc: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x"
  1.1298 +  and fd: "\<forall>x. a < x \<and> x < b \<longrightarrow> f differentiable x"
  1.1299 +  and gc: "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont g x"
  1.1300 +  and gd: "\<forall>x. a < x \<and> x < b \<longrightarrow> g differentiable x"
  1.1301 +  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)"
  1.1302 +proof -
  1.1303 +  let ?h = "\<lambda>x. (f b - f a)*(g x) - (g b - g a)*(f x)"
  1.1304 +  from prems have "a < b" by simp
  1.1305 +  moreover have "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont ?h x"
  1.1306 +  proof -
  1.1307 +    have "\<forall>x. a <= x \<and> x <= b \<longrightarrow> isCont (\<lambda>x. f b - f a) x" by simp
  1.1308 +    with gc have "\<forall>x. a <= x \<and> x <= b \<longrightarrow> isCont (\<lambda>x. (f b - f a) * g x) x"
  1.1309 +      by (auto intro: isCont_mult)
  1.1310 +    moreover
  1.1311 +    have "\<forall>x. a <= x \<and> x <= b \<longrightarrow> isCont (\<lambda>x. g b - g a) x" by simp
  1.1312 +    with fc have "\<forall>x. a <= x \<and> x <= b \<longrightarrow> isCont (\<lambda>x. (g b - g a) * f x) x"
  1.1313 +      by (auto intro: isCont_mult)
  1.1314 +    ultimately show ?thesis
  1.1315 +      by (fastsimp intro: isCont_diff)
  1.1316 +  qed
  1.1317 +  moreover
  1.1318 +  have "\<forall>x. a < x \<and> x < b \<longrightarrow> ?h differentiable x"
  1.1319 +  proof -
  1.1320 +    have "\<forall>x. a < x \<and> x < b \<longrightarrow> (\<lambda>x. f b - f a) differentiable x" by (simp add: differentiable_const)
  1.1321 +    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)
  1.1322 +    moreover
  1.1323 +    have "\<forall>x. a < x \<and> x < b \<longrightarrow> (\<lambda>x. g b - g a) differentiable x" by (simp add: differentiable_const)
  1.1324 +    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)
  1.1325 +    ultimately show ?thesis by (simp add: differentiable_diff)
  1.1326 +  qed
  1.1327 +  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)
  1.1328 +  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" ..
  1.1329 +  then obtain c where cdef: "a < c \<and> c < b \<and> DERIV ?h c :> l \<and> ?h b - ?h a = (b - a) * l" ..
  1.1330 +
  1.1331 +  from cdef have cint: "a < c \<and> c < b" by auto
  1.1332 +  with gd have "g differentiable c" by simp
  1.1333 +  hence "\<exists>D. DERIV g c :> D" by (rule differentiableD)
  1.1334 +  then obtain g'c where g'cdef: "DERIV g c :> g'c" ..
  1.1335 +
  1.1336 +  from cdef have "a < c \<and> c < b" by auto
  1.1337 +  with fd have "f differentiable c" by simp
  1.1338 +  hence "\<exists>D. DERIV f c :> D" by (rule differentiableD)
  1.1339 +  then obtain f'c where f'cdef: "DERIV f c :> f'c" ..
  1.1340 +
  1.1341 +  from cdef have "DERIV ?h c :> l" by auto
  1.1342 +  moreover
  1.1343 +  {
  1.1344 +    have "DERIV (\<lambda>x. (f b - f a) * g x) c :> g'c * (f b - f a)"
  1.1345 +      apply (insert DERIV_const [where k="f b - f a"])
  1.1346 +      apply (drule meta_spec [of _ c])
  1.1347 +      apply (drule DERIV_mult [OF _ g'cdef])
  1.1348 +      by simp
  1.1349 +    moreover have "DERIV (\<lambda>x. (g b - g a) * f x) c :> f'c * (g b - g a)"
  1.1350 +      apply (insert DERIV_const [where k="g b - g a"])
  1.1351 +      apply (drule meta_spec [of _ c])
  1.1352 +      apply (drule DERIV_mult [OF _ f'cdef])
  1.1353 +      by simp
  1.1354 +    ultimately have "DERIV ?h c :>  g'c * (f b - f a) - f'c * (g b - g a)"
  1.1355 +      by (simp add: DERIV_diff)
  1.1356 +  }
  1.1357 +  ultimately have leq: "l =  g'c * (f b - f a) - f'c * (g b - g a)" by (rule DERIV_unique)
  1.1358 +
  1.1359 +  {
  1.1360 +    from cdef have "?h b - ?h a = (b - a) * l" by auto
  1.1361 +    also with leq have "\<dots> = (b - a) * (g'c * (f b - f a) - f'c * (g b - g a))" by simp
  1.1362 +    finally have "?h b - ?h a = (b - a) * (g'c * (f b - f a) - f'c * (g b - g a))" by simp
  1.1363 +  }
  1.1364 +  moreover
  1.1365 +  {
  1.1366 +    have "?h b - ?h a =
  1.1367 +         ((f b)*(g b) - (f a)*(g b) - (g b)*(f b) + (g a)*(f b)) -
  1.1368 +          ((f b)*(g a) - (f a)*(g a) - (g b)*(f a) + (g a)*(f a))"
  1.1369 +      by (simp add: mult_ac add_ac right_diff_distrib)
  1.1370 +    hence "?h b - ?h a = 0" by auto
  1.1371 +  }
  1.1372 +  ultimately have "(b - a) * (g'c * (f b - f a) - f'c * (g b - g a)) = 0" by auto
  1.1373 +  with alb have "g'c * (f b - f a) - f'c * (g b - g a) = 0" by simp
  1.1374 +  hence "g'c * (f b - f a) = f'c * (g b - g a)" by simp
  1.1375 +  hence "(f b - f a) * g'c = (g b - g a) * f'c" by (simp add: mult_ac)
  1.1376 +
  1.1377 +  with g'cdef f'cdef cint show ?thesis by auto
  1.1378 +qed
  1.1379 +
  1.1380 +lemma lemma_DERIV_subst: "[| DERIV f x :> D; D = E |] ==> DERIV f x :> E"
  1.1381 +by auto
  1.1382 +
  1.1383 +subsection {* Derivatives of univariate polynomials *}
  1.1384 +
  1.1385 +
  1.1386 +  
  1.1387 +primrec pderiv_aux :: "nat => real list => real list" where
  1.1388 +   pderiv_aux_Nil:  "pderiv_aux n [] = []"
  1.1389 +|  pderiv_aux_Cons: "pderiv_aux n (h#t) =
  1.1390 +                     (real n * h)#(pderiv_aux (Suc n) t)"
  1.1391 +
  1.1392 +definition
  1.1393 +  pderiv :: "real list => real list" where
  1.1394 +  "pderiv p = (if p = [] then [] else pderiv_aux 1 (tl p))"
  1.1395 +
  1.1396 +
  1.1397 +text{*The derivative*}
  1.1398 +
  1.1399 +lemma pderiv_Nil: "pderiv [] = []"
  1.1400 +
  1.1401 +apply (simp add: pderiv_def)
  1.1402 +done
  1.1403 +declare pderiv_Nil [simp]
  1.1404 +
  1.1405 +lemma pderiv_singleton: "pderiv [c] = []"
  1.1406 +by (simp add: pderiv_def)
  1.1407 +declare pderiv_singleton [simp]
  1.1408 +
  1.1409 +lemma pderiv_Cons: "pderiv (h#t) = pderiv_aux 1 t"
  1.1410 +by (simp add: pderiv_def)
  1.1411 +
  1.1412 +lemma DERIV_cmult2: "DERIV f x :> D ==> DERIV (%x. (f x) * c :: real) x :> D * c"
  1.1413 +by (simp add: DERIV_cmult mult_commute [of _ c])
  1.1414 +
  1.1415 +lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
  1.1416 +by (rule lemma_DERIV_subst, rule DERIV_pow, simp)
  1.1417 +declare DERIV_pow2 [simp] DERIV_pow [simp]
  1.1418 +
  1.1419 +lemma lemma_DERIV_poly1: "\<forall>n. DERIV (%x. (x ^ (Suc n) * poly p x)) x :>
  1.1420 +        x ^ n * poly (pderiv_aux (Suc n) p) x "
  1.1421 +apply (induct "p")
  1.1422 +apply (auto intro!: DERIV_add DERIV_cmult2 
  1.1423 +            simp add: pderiv_def right_distrib real_mult_assoc [symmetric] 
  1.1424 +            simp del: realpow_Suc)
  1.1425 +apply (subst mult_commute) 
  1.1426 +apply (simp del: realpow_Suc) 
  1.1427 +apply (simp add: mult_commute realpow_Suc [symmetric] del: realpow_Suc)
  1.1428 +done
  1.1429 +
  1.1430 +lemma lemma_DERIV_poly: "DERIV (%x. (x ^ (Suc n) * poly p x)) x :>
  1.1431 +        x ^ n * poly (pderiv_aux (Suc n) p) x "
  1.1432 +by (simp add: lemma_DERIV_poly1 del: realpow_Suc)
  1.1433 +
  1.1434 +lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: real) x :> D"
  1.1435 +by (rule lemma_DERIV_subst, rule DERIV_add, auto)
  1.1436 +
  1.1437 +lemma poly_DERIV[simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
  1.1438 +apply (induct "p")
  1.1439 +apply (auto simp add: pderiv_Cons)
  1.1440 +apply (rule DERIV_add_const)
  1.1441 +apply (rule lemma_DERIV_subst)
  1.1442 +apply (rule lemma_DERIV_poly [where n=0, simplified], simp) 
  1.1443 +done
  1.1444 +
  1.1445 +
  1.1446 +text{* Consequences of the derivative theorem above*}
  1.1447 +
  1.1448 +lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (x::real)"
  1.1449 +apply (simp add: differentiable_def)
  1.1450 +apply (blast intro: poly_DERIV)
  1.1451 +done
  1.1452 +
  1.1453 +lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
  1.1454 +by (rule poly_DERIV [THEN DERIV_isCont])
  1.1455 +
  1.1456 +lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
  1.1457 +      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
  1.1458 +apply (cut_tac f = "%x. poly p x" and a = a and b = b and y = 0 in IVT_objl)
  1.1459 +apply (auto simp add: order_le_less)
  1.1460 +done
  1.1461 +
  1.1462 +lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
  1.1463 +      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
  1.1464 +apply (insert poly_IVT_pos [where p = "-- p" ]) 
  1.1465 +apply (simp add: poly_minus neg_less_0_iff_less) 
  1.1466 +done
  1.1467 +
  1.1468 +lemma poly_MVT: "a < b ==>
  1.1469 +     \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
  1.1470 +apply (drule_tac f = "poly p" in MVT, auto)
  1.1471 +apply (rule_tac x = z in exI)
  1.1472 +apply (auto simp add: real_mult_left_cancel poly_DERIV [THEN DERIV_unique])
  1.1473 +done
  1.1474 +
  1.1475 +text{*Lemmas for Derivatives*}
  1.1476 +
  1.1477 +lemma lemma_poly_pderiv_aux_add: "\<forall>p2 n. poly (pderiv_aux n (p1 +++ p2)) x =
  1.1478 +                poly (pderiv_aux n p1 +++ pderiv_aux n p2) x"
  1.1479 +apply (induct "p1", simp, clarify) 
  1.1480 +apply (case_tac "p2")
  1.1481 +apply (auto simp add: right_distrib)
  1.1482 +done
  1.1483 +
  1.1484 +lemma poly_pderiv_aux_add: "poly (pderiv_aux n (p1 +++ p2)) x =
  1.1485 +      poly (pderiv_aux n p1 +++ pderiv_aux n p2) x"
  1.1486 +apply (simp add: lemma_poly_pderiv_aux_add)
  1.1487 +done
  1.1488 +
  1.1489 +lemma lemma_poly_pderiv_aux_cmult: "\<forall>n. poly (pderiv_aux n (c %* p)) x = poly (c %* pderiv_aux n p) x"
  1.1490 +apply (induct "p")
  1.1491 +apply (auto simp add: poly_cmult mult_ac)
  1.1492 +done
  1.1493 +
  1.1494 +lemma poly_pderiv_aux_cmult: "poly (pderiv_aux n (c %* p)) x = poly (c %* pderiv_aux n p) x"
  1.1495 +by (simp add: lemma_poly_pderiv_aux_cmult)
  1.1496 +
  1.1497 +lemma poly_pderiv_aux_minus:
  1.1498 +   "poly (pderiv_aux n (-- p)) x = poly (-- pderiv_aux n p) x"
  1.1499 +apply (simp add: poly_minus_def poly_pderiv_aux_cmult)
  1.1500 +done
  1.1501 +
  1.1502 +lemma lemma_poly_pderiv_aux_mult1: "\<forall>n. poly (pderiv_aux (Suc n) p) x = poly ((pderiv_aux n p) +++ p) x"
  1.1503 +apply (induct "p")
  1.1504 +apply (auto simp add: real_of_nat_Suc left_distrib)
  1.1505 +done
  1.1506 +
  1.1507 +lemma lemma_poly_pderiv_aux_mult: "poly (pderiv_aux (Suc n) p) x = poly ((pderiv_aux n p) +++ p) x"
  1.1508 +by (simp add: lemma_poly_pderiv_aux_mult1)
  1.1509 +
  1.1510 +lemma lemma_poly_pderiv_add: "\<forall>q. poly (pderiv (p +++ q)) x = poly (pderiv p +++ pderiv q) x"
  1.1511 +apply (induct "p", simp, clarify) 
  1.1512 +apply (case_tac "q")
  1.1513 +apply (auto simp add: poly_pderiv_aux_add poly_add pderiv_def)
  1.1514 +done
  1.1515 +
  1.1516 +lemma poly_pderiv_add: "poly (pderiv (p +++ q)) x = poly (pderiv p +++ pderiv q) x"
  1.1517 +by (simp add: lemma_poly_pderiv_add)
  1.1518 +
  1.1519 +lemma poly_pderiv_cmult: "poly (pderiv (c %* p)) x = poly (c %* (pderiv p)) x"
  1.1520 +apply (induct "p")
  1.1521 +apply (auto simp add: poly_pderiv_aux_cmult poly_cmult pderiv_def)
  1.1522 +done
  1.1523 +
  1.1524 +lemma poly_pderiv_minus: "poly (pderiv (--p)) x = poly (--(pderiv p)) x"
  1.1525 +by (simp add: poly_minus_def poly_pderiv_cmult)
  1.1526 +
  1.1527 +lemma lemma_poly_mult_pderiv:
  1.1528 +   "poly (pderiv (h#t)) x = poly ((0 # (pderiv t)) +++ t) x"
  1.1529 +apply (simp add: pderiv_def)
  1.1530 +apply (induct "t")
  1.1531 +apply (auto simp add: poly_add lemma_poly_pderiv_aux_mult)
  1.1532 +done
  1.1533 +
  1.1534 +lemma poly_pderiv_mult: "\<forall>q. poly (pderiv (p *** q)) x =
  1.1535 +      poly (p *** (pderiv q) +++ q *** (pderiv p)) x"
  1.1536 +apply (induct "p")
  1.1537 +apply (auto simp add: poly_add poly_cmult poly_pderiv_cmult poly_pderiv_add poly_mult)
  1.1538 +apply (rule lemma_poly_mult_pderiv [THEN ssubst])
  1.1539 +apply (rule lemma_poly_mult_pderiv [THEN ssubst])
  1.1540 +apply (rule poly_add [THEN ssubst])
  1.1541 +apply (rule poly_add [THEN ssubst])
  1.1542 +apply (simp (no_asm_simp) add: poly_mult right_distrib add_ac mult_ac)
  1.1543 +done
  1.1544 +
  1.1545 +lemma poly_pderiv_exp: "poly (pderiv (p %^ (Suc n))) x =
  1.1546 +         poly ((real (Suc n)) %* (p %^ n) *** pderiv p) x"
  1.1547 +apply (induct "n")
  1.1548 +apply (auto simp add: poly_add poly_pderiv_cmult poly_cmult poly_pderiv_mult
  1.1549 +                      real_of_nat_zero poly_mult real_of_nat_Suc 
  1.1550 +                      right_distrib left_distrib mult_ac)
  1.1551 +done
  1.1552 +
  1.1553 +lemma poly_pderiv_exp_prime: "poly (pderiv ([-a, 1] %^ (Suc n))) x =
  1.1554 +      poly (real (Suc n) %* ([-a, 1] %^ n)) x"
  1.1555 +apply (simp add: poly_pderiv_exp poly_mult del: pexp_Suc)
  1.1556 +apply (simp add: poly_cmult pderiv_def)
  1.1557 +done
  1.1558 +
  1.1559 +
  1.1560 +lemma real_mult_zero_disj_iff[simp]: "(x * y = 0) = (x = (0::real) | y = 0)"
  1.1561 +by simp
  1.1562 +
  1.1563 +lemma pderiv_aux_iszero [rule_format, simp]:
  1.1564 +    "\<forall>n. list_all (%c. c = 0) (pderiv_aux (Suc n) p) = list_all (%c. c = 0) p"
  1.1565 +by (induct "p", auto)
  1.1566 +
  1.1567 +lemma pderiv_aux_iszero_num: "(number_of n :: nat) \<noteq> 0
  1.1568 +      ==> (list_all (%c. c = 0) (pderiv_aux (number_of n) p) =
  1.1569 +      list_all (%c. c = 0) p)"
  1.1570 +unfolding neq0_conv
  1.1571 +apply (rule_tac n1 = "number_of n" and m1 = 0 in less_imp_Suc_add [THEN exE], force)
  1.1572 +apply (rule_tac n1 = "0 + x" in pderiv_aux_iszero [THEN subst])
  1.1573 +apply (simp (no_asm_simp) del: pderiv_aux_iszero)
  1.1574 +done
  1.1575 +
  1.1576 +instance real:: idom_char_0
  1.1577 +apply (intro_classes)
  1.1578 +done
  1.1579 +
  1.1580 +instance real:: recpower_idom_char_0
  1.1581 +apply (intro_classes)
  1.1582 +done
  1.1583 +
  1.1584 +lemma pderiv_iszero [rule_format]:
  1.1585 +     "poly (pderiv p) = poly [] --> (\<exists>h. poly p = poly [h])"
  1.1586 +apply (simp add: poly_zero)
  1.1587 +apply (induct "p", force)
  1.1588 +apply (simp add: pderiv_Cons pderiv_aux_iszero_num del: poly_Cons)
  1.1589 +apply (auto simp add: poly_zero [symmetric])
  1.1590 +done
  1.1591 +
  1.1592 +lemma pderiv_zero_obj: "poly p = poly [] --> (poly (pderiv p) = poly [])"
  1.1593 +apply (simp add: poly_zero)
  1.1594 +apply (induct "p", force)
  1.1595 +apply (simp add: pderiv_Cons pderiv_aux_iszero_num del: poly_Cons)
  1.1596 +done
  1.1597 +
  1.1598 +lemma pderiv_zero: "poly p = poly [] ==> (poly (pderiv p) = poly [])"
  1.1599 +by (blast elim: pderiv_zero_obj [THEN impE])
  1.1600 +declare pderiv_zero [simp]
  1.1601 +
  1.1602 +lemma poly_pderiv_welldef: "poly p = poly q ==> (poly (pderiv p) = poly (pderiv q))"
  1.1603 +apply (cut_tac p = "p +++ --q" in pderiv_zero_obj)
  1.1604 +apply (simp add: fun_eq poly_add poly_minus poly_pderiv_add poly_pderiv_minus del: pderiv_zero)
  1.1605 +done
  1.1606 +
  1.1607 +lemma lemma_order_pderiv [rule_format]:
  1.1608 +     "\<forall>p q a. 0 < n &
  1.1609 +       poly (pderiv p) \<noteq> poly [] &
  1.1610 +       poly p = poly ([- a, 1] %^ n *** q) & ~ [- a, 1] divides q
  1.1611 +       --> n = Suc (order a (pderiv p))"
  1.1612 +apply (induct "n", safe)
  1.1613 +apply (rule order_unique_lemma, rule conjI, assumption)
  1.1614 +apply (subgoal_tac "\<forall>r. r divides (pderiv p) = r divides (pderiv ([-a, 1] %^ Suc n *** q))")
  1.1615 +apply (drule_tac [2] poly_pderiv_welldef)
  1.1616 + prefer 2 apply (simp add: divides_def del: pmult_Cons pexp_Suc) 
  1.1617 +apply (simp del: pmult_Cons pexp_Suc) 
  1.1618 +apply (rule conjI)
  1.1619 +apply (simp add: divides_def fun_eq del: pmult_Cons pexp_Suc)
  1.1620 +apply (rule_tac x = "[-a, 1] *** (pderiv q) +++ real (Suc n) %* q" in exI)
  1.1621 +apply (simp add: poly_pderiv_mult poly_pderiv_exp_prime poly_add poly_mult poly_cmult right_distrib mult_ac del: pmult_Cons pexp_Suc)
  1.1622 +apply (simp add: poly_mult right_distrib left_distrib mult_ac del: pmult_Cons)
  1.1623 +apply (erule_tac V = "\<forall>r. r divides pderiv p = r divides pderiv ([- a, 1] %^ Suc n *** q)" in thin_rl)
  1.1624 +apply (unfold divides_def)
  1.1625 +apply (simp (no_asm) add: poly_pderiv_mult poly_pderiv_exp_prime fun_eq poly_add poly_mult del: pmult_Cons pexp_Suc)
  1.1626 +apply (rule contrapos_np, assumption)
  1.1627 +apply (rotate_tac 3, erule contrapos_np)
  1.1628 +apply (simp del: pmult_Cons pexp_Suc, safe)
  1.1629 +apply (rule_tac x = "inverse (real (Suc n)) %* (qa +++ -- (pderiv q))" in exI)
  1.1630 +apply (subgoal_tac "poly ([-a, 1] %^ n *** q) = poly ([-a, 1] %^ n *** ([-a, 1] *** (inverse (real (Suc n)) %* (qa +++ -- (pderiv q))))) ")
  1.1631 +apply (drule poly_mult_left_cancel [THEN iffD1], simp)
  1.1632 +apply (simp add: fun_eq poly_mult poly_add poly_cmult poly_minus del: pmult_Cons mult_cancel_left, safe)
  1.1633 +apply (rule_tac c1 = "real (Suc n)" in real_mult_left_cancel [THEN iffD1])
  1.1634 +apply (simp (no_asm))
  1.1635 +apply (subgoal_tac "real (Suc n) * (poly ([- a, 1] %^ n) xa * poly q xa) =
  1.1636 +          (poly qa xa + - poly (pderiv q) xa) *
  1.1637 +          (poly ([- a, 1] %^ n) xa *
  1.1638 +           ((- a + xa) * (inverse (real (Suc n)) * real (Suc n))))")
  1.1639 +apply (simp only: mult_ac)  
  1.1640 +apply (rotate_tac 2)
  1.1641 +apply (drule_tac x = xa in spec)
  1.1642 +apply (simp add: left_distrib mult_ac del: pmult_Cons)
  1.1643 +done
  1.1644 +
  1.1645 +lemma order_pderiv: "[| poly (pderiv p) \<noteq> poly []; order a p \<noteq> 0 |]
  1.1646 +      ==> (order a p = Suc (order a (pderiv p)))"
  1.1647 +apply (case_tac "poly p = poly []")
  1.1648 +apply (auto dest: pderiv_zero)
  1.1649 +apply (drule_tac a = a and p = p in order_decomp)
  1.1650 +using neq0_conv
  1.1651 +apply (blast intro: lemma_order_pderiv)
  1.1652 +done
  1.1653 +
  1.1654 +text{*Now justify the standard squarefree decomposition, i.e. f / gcd(f,f'). *}
  1.1655 +
  1.1656 +lemma poly_squarefree_decomp_order: "[| poly (pderiv p) \<noteq> poly [];
  1.1657 +         poly p = poly (q *** d);
  1.1658 +         poly (pderiv p) = poly (e *** d);
  1.1659 +         poly d = poly (r *** p +++ s *** pderiv p)
  1.1660 +      |] ==> order a q = (if order a p = 0 then 0 else 1)"
  1.1661 +apply (subgoal_tac "order a p = order a q + order a d")
  1.1662 +apply (rule_tac [2] s = "order a (q *** d)" in trans)
  1.1663 +prefer 2 apply (blast intro: order_poly)
  1.1664 +apply (rule_tac [2] order_mult)
  1.1665 + prefer 2 apply force
  1.1666 +apply (case_tac "order a p = 0", simp)
  1.1667 +apply (subgoal_tac "order a (pderiv p) = order a e + order a d")
  1.1668 +apply (rule_tac [2] s = "order a (e *** d)" in trans)
  1.1669 +prefer 2 apply (blast intro: order_poly)
  1.1670 +apply (rule_tac [2] order_mult)
  1.1671 + prefer 2 apply force
  1.1672 +apply (case_tac "poly p = poly []")
  1.1673 +apply (drule_tac p = p in pderiv_zero, simp)
  1.1674 +apply (drule order_pderiv, assumption)
  1.1675 +apply (subgoal_tac "order a (pderiv p) \<le> order a d")
  1.1676 +apply (subgoal_tac [2] " ([-a, 1] %^ (order a (pderiv p))) divides d")
  1.1677 + prefer 2 apply (simp add: poly_entire order_divides)
  1.1678 +apply (subgoal_tac [2] " ([-a, 1] %^ (order a (pderiv p))) divides p & ([-a, 1] %^ (order a (pderiv p))) divides (pderiv p) ")
  1.1679 + prefer 3 apply (simp add: order_divides)
  1.1680 + prefer 2 apply (simp add: divides_def del: pexp_Suc pmult_Cons, safe)
  1.1681 +apply (rule_tac x = "r *** qa +++ s *** qaa" in exI)
  1.1682 +apply (simp add: fun_eq poly_add poly_mult left_distrib right_distrib mult_ac del: pexp_Suc pmult_Cons, auto)
  1.1683 +done
  1.1684 +
  1.1685 +
  1.1686 +lemma poly_squarefree_decomp_order2: "[| poly (pderiv p) \<noteq> poly [];
  1.1687 +         poly p = poly (q *** d);
  1.1688 +         poly (pderiv p) = poly (e *** d);
  1.1689 +         poly d = poly (r *** p +++ s *** pderiv p)
  1.1690 +      |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
  1.1691 +apply (blast intro: poly_squarefree_decomp_order)
  1.1692 +done
  1.1693 +
  1.1694 +lemma order_pderiv2: "[| poly (pderiv p) \<noteq> poly []; order a p \<noteq> 0 |]
  1.1695 +      ==> (order a (pderiv p) = n) = (order a p = Suc n)"
  1.1696 +apply (auto dest: order_pderiv)
  1.1697 +done
  1.1698 +
  1.1699 +lemma rsquarefree_roots:
  1.1700 +  "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
  1.1701 +apply (simp add: rsquarefree_def)
  1.1702 +apply (case_tac "poly p = poly []", simp, simp)
  1.1703 +apply (case_tac "poly (pderiv p) = poly []")
  1.1704 +apply simp
  1.1705 +apply (drule pderiv_iszero, clarify)
  1.1706 +apply (subgoal_tac "\<forall>a. order a p = order a [h]")
  1.1707 +apply (simp add: fun_eq)
  1.1708 +apply (rule allI)
  1.1709 +apply (cut_tac p = "[h]" and a = a in order_root)
  1.1710 +apply (simp add: fun_eq)
  1.1711 +apply (blast intro: order_poly)
  1.1712 +apply (auto simp add: order_root order_pderiv2)
  1.1713 +apply (erule_tac x="a" in allE, simp)
  1.1714 +done
  1.1715 +
  1.1716 +lemma poly_squarefree_decomp: "[| poly (pderiv p) \<noteq> poly [];
  1.1717 +         poly p = poly (q *** d);
  1.1718 +         poly (pderiv p) = poly (e *** d);
  1.1719 +         poly d = poly (r *** p +++ s *** pderiv p)
  1.1720 +      |] ==> rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
  1.1721 +apply (frule poly_squarefree_decomp_order2, assumption+) 
  1.1722 +apply (case_tac "poly p = poly []")
  1.1723 +apply (blast dest: pderiv_zero)
  1.1724 +apply (simp (no_asm) add: rsquarefree_def order_root del: pmult_Cons)
  1.1725 +apply (simp add: poly_entire del: pmult_Cons)
  1.1726 +done
  1.1727 +
  1.1728 +end