src/HOL/ex/Gauge_Integration.thy
author hoelzl
Mon Aug 08 14:13:14 2016 +0200 (2016-08-08)
changeset 63627 6ddb43c6b711
parent 63060 293ede07b775
child 63882 018998c00003
permissions -rw-r--r--
rename HOL-Multivariate_Analysis to HOL-Analysis.
     1 (*  Author:     Jacques D. Fleuriot, University of Edinburgh
     2     Conversion to Isar and new proofs by Lawrence C Paulson, 2004
     3 
     4     Replaced by ~~/src/HOL/Analysis/Henstock_Kurzweil_Integration and
     5     Bochner_Integration.
     6 *)
     7 
     8 section\<open>Theory of Integration on real intervals\<close>
     9 
    10 theory Gauge_Integration
    11 imports Complex_Main
    12 begin
    13 
    14 text \<open>
    15 
    16 \textbf{Attention}: This theory defines the Integration on real
    17 intervals.  This is just a example theory for historical / expository interests.
    18 A better replacement is found in the Multivariate Analysis library. This defines
    19 the gauge integral on real vector spaces and in the Real Integral theory
    20 is a specialization to the integral on arbitrary real intervals.  The
    21 Multivariate Analysis package also provides a better support for analysis on
    22 integrals.
    23 
    24 \<close>
    25 
    26 text\<open>We follow John Harrison in formalizing the Gauge integral.\<close>
    27 
    28 subsection \<open>Gauges\<close>
    29 
    30 definition
    31   gauge :: "[real set, real => real] => bool" where
    32   "gauge E g = (\<forall>x\<in>E. 0 < g(x))"
    33 
    34 
    35 subsection \<open>Gauge-fine divisions\<close>
    36 
    37 inductive
    38   fine :: "[real \<Rightarrow> real, real \<times> real, (real \<times> real \<times> real) list] \<Rightarrow> bool"
    39 for
    40   \<delta> :: "real \<Rightarrow> real"
    41 where
    42   fine_Nil:
    43     "fine \<delta> (a, a) []"
    44 | fine_Cons:
    45     "\<lbrakk>fine \<delta> (b, c) D; a < b; a \<le> x; x \<le> b; b - a < \<delta> x\<rbrakk>
    46       \<Longrightarrow> fine \<delta> (a, c) ((a, x, b) # D)"
    47 
    48 lemmas fine_induct [induct set: fine] =
    49   fine.induct [of "\<delta>" "(a,b)" "D" "case_prod P", unfolded split_conv] for \<delta> a b D P
    50 
    51 lemma fine_single:
    52   "\<lbrakk>a < b; a \<le> x; x \<le> b; b - a < \<delta> x\<rbrakk> \<Longrightarrow> fine \<delta> (a, b) [(a, x, b)]"
    53 by (rule fine_Cons [OF fine_Nil])
    54 
    55 lemma fine_append:
    56   "\<lbrakk>fine \<delta> (a, b) D; fine \<delta> (b, c) D'\<rbrakk> \<Longrightarrow> fine \<delta> (a, c) (D @ D')"
    57 by (induct set: fine, simp, simp add: fine_Cons)
    58 
    59 lemma fine_imp_le: "fine \<delta> (a, b) D \<Longrightarrow> a \<le> b"
    60 by (induct set: fine, simp_all)
    61 
    62 lemma nonempty_fine_imp_less: "\<lbrakk>fine \<delta> (a, b) D; D \<noteq> []\<rbrakk> \<Longrightarrow> a < b"
    63 apply (induct set: fine, simp)
    64 apply (drule fine_imp_le, simp)
    65 done
    66 
    67 lemma fine_Nil_iff: "fine \<delta> (a, b) [] \<longleftrightarrow> a = b"
    68 by (auto elim: fine.cases intro: fine.intros)
    69 
    70 lemma fine_same_iff: "fine \<delta> (a, a) D \<longleftrightarrow> D = []"
    71 proof
    72   assume "fine \<delta> (a, a) D" thus "D = []"
    73     by (metis nonempty_fine_imp_less less_irrefl)
    74 next
    75   assume "D = []" thus "fine \<delta> (a, a) D"
    76     by (simp add: fine_Nil)
    77 qed
    78 
    79 lemma empty_fine_imp_eq: "\<lbrakk>fine \<delta> (a, b) D; D = []\<rbrakk> \<Longrightarrow> a = b"
    80 by (simp add: fine_Nil_iff)
    81 
    82 lemma mem_fine:
    83   "\<lbrakk>fine \<delta> (a, b) D; (u, x, v) \<in> set D\<rbrakk> \<Longrightarrow> u < v \<and> u \<le> x \<and> x \<le> v"
    84 by (induct set: fine, simp, force)
    85 
    86 lemma mem_fine2: "\<lbrakk>fine \<delta> (a, b) D; (u, z, v) \<in> set D\<rbrakk> \<Longrightarrow> a \<le> u \<and> v \<le> b"
    87 apply (induct arbitrary: z u v set: fine, auto)
    88 apply (simp add: fine_imp_le)
    89 apply (erule order_trans [OF less_imp_le], simp)
    90 done
    91 
    92 lemma mem_fine3: "\<lbrakk>fine \<delta> (a, b) D; (u, z, v) \<in> set D\<rbrakk> \<Longrightarrow> v - u < \<delta> z"
    93 by (induct arbitrary: z u v set: fine) auto
    94 
    95 lemma BOLZANO:
    96   fixes P :: "real \<Rightarrow> real \<Rightarrow> bool"
    97   assumes 1: "a \<le> b"
    98   assumes 2: "\<And>a b c. \<lbrakk>P a b; P b c; a \<le> b; b \<le> c\<rbrakk> \<Longrightarrow> P a c"
    99   assumes 3: "\<And>x. \<exists>d>0. \<forall>a b. a \<le> x & x \<le> b & (b-a) < d \<longrightarrow> P a b"
   100   shows "P a b"
   101   using 1 2 3 by (rule Bolzano)
   102 
   103 text\<open>We can always find a division that is fine wrt any gauge\<close>
   104 
   105 lemma fine_exists:
   106   assumes "a \<le> b" and "gauge {a..b} \<delta>" shows "\<exists>D. fine \<delta> (a, b) D"
   107 proof -
   108   {
   109     fix u v :: real assume "u \<le> v"
   110     have "a \<le> u \<Longrightarrow> v \<le> b \<Longrightarrow> \<exists>D. fine \<delta> (u, v) D"
   111       apply (induct u v rule: BOLZANO, rule \<open>u \<le> v\<close>)
   112        apply (simp, fast intro: fine_append)
   113       apply (case_tac "a \<le> x \<and> x \<le> b")
   114        apply (rule_tac x="\<delta> x" in exI)
   115        apply (rule conjI)
   116         apply (simp add: \<open>gauge {a..b} \<delta>\<close> [unfolded gauge_def])
   117        apply (clarify, rename_tac u v)
   118        apply (case_tac "u = v")
   119         apply (fast intro: fine_Nil)
   120        apply (subgoal_tac "u < v", fast intro: fine_single, simp)
   121       apply (rule_tac x="1" in exI, clarsimp)
   122       done
   123   }
   124   with \<open>a \<le> b\<close> show ?thesis by auto
   125 qed
   126 
   127 lemma fine_covers_all:
   128   assumes "fine \<delta> (a, c) D" and "a < x" and "x \<le> c"
   129   shows "\<exists> N < length D. \<forall> d t e. D ! N = (d,t,e) \<longrightarrow> d < x \<and> x \<le> e"
   130   using assms
   131 proof (induct set: fine)
   132   case (2 b c D a t)
   133   thus ?case
   134   proof (cases "b < x")
   135     case True
   136     with 2 obtain N where *: "N < length D"
   137       and **: "D ! N = (d,t,e) \<Longrightarrow> d < x \<and> x \<le> e" for d t e by auto
   138     hence "Suc N < length ((a,t,b)#D) \<and>
   139            (\<forall> d t' e. ((a,t,b)#D) ! Suc N = (d,t',e) \<longrightarrow> d < x \<and> x \<le> e)" by auto
   140     thus ?thesis by auto
   141   next
   142     case False with 2
   143     have "0 < length ((a,t,b)#D) \<and>
   144            (\<forall> d t' e. ((a,t,b)#D) ! 0 = (d,t',e) \<longrightarrow> d < x \<and> x \<le> e)" by auto
   145     thus ?thesis by auto
   146   qed
   147 qed auto
   148 
   149 lemma fine_append_split:
   150   assumes "fine \<delta> (a,b) D" and "D2 \<noteq> []" and "D = D1 @ D2"
   151   shows "fine \<delta> (a,fst (hd D2)) D1" (is "?fine1")
   152   and "fine \<delta> (fst (hd D2), b) D2" (is "?fine2")
   153 proof -
   154   from assms
   155   have "?fine1 \<and> ?fine2"
   156   proof (induct arbitrary: D1 D2)
   157     case (2 b c D a' x D1 D2)
   158     note induct = this
   159 
   160     thus ?case
   161     proof (cases D1)
   162       case Nil
   163       hence "fst (hd D2) = a'" using 2 by auto
   164       with fine_Cons[OF \<open>fine \<delta> (b,c) D\<close> induct(3,4,5)] Nil induct
   165       show ?thesis by (auto intro: fine_Nil)
   166     next
   167       case (Cons d1 D1')
   168       with induct(2)[OF \<open>D2 \<noteq> []\<close>, of D1'] induct(8)
   169       have "fine \<delta> (b, fst (hd D2)) D1'" and "fine \<delta> (fst (hd D2), c) D2" and
   170         "d1 = (a', x, b)" by auto
   171       with fine_Cons[OF this(1) induct(3,4,5), OF induct(6)] Cons
   172       show ?thesis by auto
   173     qed
   174   qed auto
   175   thus ?fine1 and ?fine2 by auto
   176 qed
   177 
   178 lemma fine_\<delta>_expand:
   179   assumes "fine \<delta> (a,b) D"
   180   and "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> \<delta> x \<le> \<delta>' x"
   181   shows "fine \<delta>' (a,b) D"
   182 using assms proof induct
   183   case 1 show ?case by (rule fine_Nil)
   184 next
   185   case (2 b c D a x)
   186   show ?case
   187   proof (rule fine_Cons)
   188     show "fine \<delta>' (b,c) D" using 2 by auto
   189     from fine_imp_le[OF 2(1)] 2(6) \<open>x \<le> b\<close>
   190     show "b - a < \<delta>' x"
   191       using 2(7)[OF \<open>a \<le> x\<close>] by auto
   192   qed (auto simp add: 2)
   193 qed
   194 
   195 lemma fine_single_boundaries:
   196   assumes "fine \<delta> (a,b) D" and "D = [(d, t, e)]"
   197   shows "a = d \<and> b = e"
   198 using assms proof induct
   199   case (2 b c  D a x)
   200   hence "D = []" and "a = d" and "b = e" by auto
   201   moreover
   202   from \<open>fine \<delta> (b,c) D\<close> \<open>D = []\<close> have "b = c"
   203     by (rule empty_fine_imp_eq)
   204   ultimately show ?case by simp
   205 qed auto
   206 
   207 lemma fine_listsum_eq_diff:
   208   fixes f :: "real \<Rightarrow> real"
   209   shows "fine \<delta> (a, b) D \<Longrightarrow> (\<Sum>(u, x, v)\<leftarrow>D. f v - f u) = f b - f a"
   210 by (induct set: fine) simp_all
   211 
   212 text\<open>Lemmas about combining gauges\<close>
   213 
   214 lemma gauge_min:
   215      "[| gauge(E) g1; gauge(E) g2 |]
   216       ==> gauge(E) (%x. min (g1(x)) (g2(x)))"
   217 by (simp add: gauge_def)
   218 
   219 lemma fine_min:
   220       "fine (%x. min (g1(x)) (g2(x))) (a,b) D
   221        ==> fine(g1) (a,b) D & fine(g2) (a,b) D"
   222 apply (erule fine.induct)
   223 apply (simp add: fine_Nil)
   224 apply (simp add: fine_Cons)
   225 done
   226 
   227 subsection \<open>Riemann sum\<close>
   228 
   229 definition
   230   rsum :: "[(real \<times> real \<times> real) list, real \<Rightarrow> real] \<Rightarrow> real" where
   231   "rsum D f = (\<Sum>(u, x, v)\<leftarrow>D. f x * (v - u))"
   232 
   233 lemma rsum_Nil [simp]: "rsum [] f = 0"
   234 unfolding rsum_def by simp
   235 
   236 lemma rsum_Cons [simp]: "rsum ((u, x, v) # D) f = f x * (v - u) + rsum D f"
   237 unfolding rsum_def by simp
   238 
   239 lemma rsum_zero [simp]: "rsum D (\<lambda>x. 0) = 0"
   240 by (induct D, auto)
   241 
   242 lemma rsum_left_distrib: "rsum D f * c = rsum D (\<lambda>x. f x * c)"
   243 by (induct D, auto simp add: algebra_simps)
   244 
   245 lemma rsum_right_distrib: "c * rsum D f = rsum D (\<lambda>x. c * f x)"
   246 by (induct D, auto simp add: algebra_simps)
   247 
   248 lemma rsum_add: "rsum D (\<lambda>x. f x + g x) =  rsum D f + rsum D g"
   249 by (induct D, auto simp add: algebra_simps)
   250 
   251 lemma rsum_append: "rsum (D1 @ D2) f = rsum D1 f + rsum D2 f"
   252 unfolding rsum_def map_append listsum_append ..
   253 
   254 
   255 subsection \<open>Gauge integrability (definite)\<close>
   256 
   257 definition
   258   Integral :: "[(real*real),real=>real,real] => bool" where
   259   "Integral = (%(a,b) f k. \<forall>e > 0.
   260                                (\<exists>\<delta>. gauge {a .. b} \<delta> &
   261                                (\<forall>D. fine \<delta> (a,b) D -->
   262                                          \<bar>rsum D f - k\<bar> < e)))"
   263 
   264 lemma Integral_eq:
   265   "Integral (a, b) f k \<longleftrightarrow>
   266     (\<forall>e>0. \<exists>\<delta>. gauge {a..b} \<delta> \<and> (\<forall>D. fine \<delta> (a,b) D \<longrightarrow> \<bar>rsum D f - k\<bar> < e))"
   267 unfolding Integral_def by simp
   268 
   269 lemma IntegralI:
   270   assumes "\<And>e. 0 < e \<Longrightarrow>
   271     \<exists>\<delta>. gauge {a..b} \<delta> \<and> (\<forall>D. fine \<delta> (a, b) D \<longrightarrow> \<bar>rsum D f - k\<bar> < e)"
   272   shows "Integral (a, b) f k"
   273 using assms unfolding Integral_def by auto
   274 
   275 lemma IntegralE:
   276   assumes "Integral (a, b) f k" and "0 < e"
   277   obtains \<delta> where "gauge {a..b} \<delta>" and "\<forall>D. fine \<delta> (a, b) D \<longrightarrow> \<bar>rsum D f - k\<bar> < e"
   278 using assms unfolding Integral_def by auto
   279 
   280 lemma Integral_def2:
   281   "Integral = (%(a,b) f k. \<forall>e>0. (\<exists>\<delta>. gauge {a..b} \<delta> &
   282                                (\<forall>D. fine \<delta> (a,b) D -->
   283                                          \<bar>rsum D f - k\<bar> \<le> e)))"
   284 unfolding Integral_def
   285 apply (safe intro!: ext)
   286 apply (fast intro: less_imp_le)
   287 apply (drule_tac x="e/2" in spec)
   288 apply force
   289 done
   290 
   291 text\<open>The integral is unique if it exists\<close>
   292 
   293 lemma Integral_unique:
   294   assumes le: "a \<le> b"
   295   assumes 1: "Integral (a, b) f k1"
   296   assumes 2: "Integral (a, b) f k2"
   297   shows "k1 = k2"
   298 proof (rule ccontr)
   299   assume "k1 \<noteq> k2"
   300   hence e: "0 < \<bar>k1 - k2\<bar> / 2" by simp
   301   obtain d1 where "gauge {a..b} d1" and
   302     d1: "\<forall>D. fine d1 (a, b) D \<longrightarrow> \<bar>rsum D f - k1\<bar> < \<bar>k1 - k2\<bar> / 2"
   303     using 1 e by (rule IntegralE)
   304   obtain d2 where "gauge {a..b} d2" and
   305     d2: "\<forall>D. fine d2 (a, b) D \<longrightarrow> \<bar>rsum D f - k2\<bar> < \<bar>k1 - k2\<bar> / 2"
   306     using 2 e by (rule IntegralE)
   307   have "gauge {a..b} (\<lambda>x. min (d1 x) (d2 x))"
   308     using \<open>gauge {a..b} d1\<close> and \<open>gauge {a..b} d2\<close>
   309     by (rule gauge_min)
   310   then obtain D where "fine (\<lambda>x. min (d1 x) (d2 x)) (a, b) D"
   311     using fine_exists [OF le] by fast
   312   hence "fine d1 (a, b) D" and "fine d2 (a, b) D"
   313     by (auto dest: fine_min)
   314   hence "\<bar>rsum D f - k1\<bar> < \<bar>k1 - k2\<bar> / 2" and "\<bar>rsum D f - k2\<bar> < \<bar>k1 - k2\<bar> / 2"
   315     using d1 d2 by simp_all
   316   hence "\<bar>rsum D f - k1\<bar> + \<bar>rsum D f - k2\<bar> < \<bar>k1 - k2\<bar> / 2 + \<bar>k1 - k2\<bar> / 2"
   317     by (rule add_strict_mono)
   318   thus False by auto
   319 qed
   320 
   321 lemma Integral_zero: "Integral(a,a) f 0"
   322 apply (rule IntegralI)
   323 apply (rule_tac x = "\<lambda>x. 1" in exI)
   324 apply (simp add: fine_same_iff gauge_def)
   325 done
   326 
   327 lemma Integral_same_iff [simp]: "Integral (a, a) f k \<longleftrightarrow> k = 0"
   328   by (auto intro: Integral_zero Integral_unique)
   329 
   330 lemma Integral_zero_fun: "Integral (a,b) (\<lambda>x. 0) 0"
   331 apply (rule IntegralI)
   332 apply (rule_tac x="\<lambda>x. 1" in exI, simp add: gauge_def)
   333 done
   334 
   335 lemma fine_rsum_const: "fine \<delta> (a,b) D \<Longrightarrow> rsum D (\<lambda>x. c) = (c * (b - a))"
   336 unfolding rsum_def
   337 by (induct set: fine, auto simp add: algebra_simps)
   338 
   339 lemma Integral_mult_const: "a \<le> b \<Longrightarrow> Integral(a,b) (\<lambda>x. c) (c * (b - a))"
   340 apply (cases "a = b", simp)
   341 apply (rule IntegralI)
   342 apply (rule_tac x = "\<lambda>x. b - a" in exI)
   343 apply (rule conjI, simp add: gauge_def)
   344 apply (clarify)
   345 apply (subst fine_rsum_const, assumption, simp)
   346 done
   347 
   348 lemma Integral_eq_diff_bounds: "a \<le> b \<Longrightarrow> Integral(a,b) (\<lambda>x. 1) (b - a)"
   349   using Integral_mult_const [of a b 1] by simp
   350 
   351 lemma Integral_mult:
   352      "[| a \<le> b; Integral(a,b) f k |] ==> Integral(a,b) (%x. c * f x) (c * k)"
   353 apply (auto simp add: order_le_less)
   354 apply (cases "c = 0", simp add: Integral_zero_fun)
   355 apply (rule IntegralI)
   356 apply (erule_tac e="e / \<bar>c\<bar>" in IntegralE, simp)
   357 apply (rule_tac x="\<delta>" in exI, clarify)
   358 apply (drule_tac x="D" in spec, clarify)
   359 apply (simp add: pos_less_divide_eq abs_mult [symmetric]
   360                  algebra_simps rsum_right_distrib)
   361 done
   362 
   363 lemma Integral_add:
   364   assumes "Integral (a, b) f x1"
   365   assumes "Integral (b, c) f x2"
   366   assumes "a \<le> b" and "b \<le> c"
   367   shows "Integral (a, c) f (x1 + x2)"
   368 proof (cases "a < b \<and> b < c", rule IntegralI)
   369   fix \<epsilon> :: real assume "0 < \<epsilon>"
   370   hence "0 < \<epsilon> / 2" by auto
   371 
   372   assume "a < b \<and> b < c"
   373   hence "a < b" and "b < c" by auto
   374 
   375   obtain \<delta>1 where \<delta>1_gauge: "gauge {a..b} \<delta>1"
   376     and I1: "fine \<delta>1 (a,b) D \<Longrightarrow> \<bar> rsum D f - x1 \<bar> < (\<epsilon> / 2)" for D
   377     using IntegralE [OF \<open>Integral (a, b) f x1\<close> \<open>0 < \<epsilon>/2\<close>] by auto
   378 
   379   obtain \<delta>2 where \<delta>2_gauge: "gauge {b..c} \<delta>2"
   380     and I2: "fine \<delta>2 (b,c) D \<Longrightarrow> \<bar> rsum D f - x2 \<bar> < (\<epsilon> / 2)" for D
   381     using IntegralE [OF \<open>Integral (b, c) f x2\<close> \<open>0 < \<epsilon>/2\<close>] by auto
   382 
   383   define \<delta> where "\<delta> x =
   384     (if x < b then min (\<delta>1 x) (b - x)
   385      else if x = b then min (\<delta>1 b) (\<delta>2 b)
   386      else min (\<delta>2 x) (x - b))" for x
   387 
   388   have "gauge {a..c} \<delta>"
   389     using \<delta>1_gauge \<delta>2_gauge unfolding \<delta>_def gauge_def by auto
   390 
   391   moreover {
   392     fix D :: "(real \<times> real \<times> real) list"
   393     assume fine: "fine \<delta> (a,c) D"
   394     from fine_covers_all[OF this \<open>a < b\<close> \<open>b \<le> c\<close>]
   395     obtain N where "N < length D"
   396       and *: "\<forall> d t e. D ! N = (d, t, e) \<longrightarrow> d < b \<and> b \<le> e"
   397       by auto
   398     obtain d t e where D_eq: "D ! N = (d, t, e)" by (cases "D!N", auto)
   399     with * have "d < b" and "b \<le> e" by auto
   400     have in_D: "(d, t, e) \<in> set D"
   401       using D_eq[symmetric] using \<open>N < length D\<close> by auto
   402 
   403     from mem_fine[OF fine in_D]
   404     have "d < e" and "d \<le> t" and "t \<le> e" by auto
   405 
   406     have "t = b"
   407     proof (rule ccontr)
   408       assume "t \<noteq> b"
   409       with mem_fine3[OF fine in_D] \<open>b \<le> e\<close> \<open>d \<le> t\<close> \<open>t \<le> e\<close> \<open>d < b\<close> \<delta>_def
   410       show False by (cases "t < b") auto
   411     qed
   412 
   413     let ?D1 = "take N D"
   414     let ?D2 = "drop N D"
   415     define D1 where "D1 = take N D @ [(d, t, b)]"
   416     define D2 where "D2 = (if b = e then [] else [(b, t, e)]) @ drop (Suc N) D"
   417 
   418     from hd_drop_conv_nth[OF \<open>N < length D\<close>]
   419     have "fst (hd ?D2) = d" using \<open>D ! N = (d, t, e)\<close> by auto
   420     with fine_append_split[OF _ _ append_take_drop_id[symmetric]]
   421     have fine1: "fine \<delta> (a,d) ?D1" and fine2: "fine \<delta> (d,c) ?D2"
   422       using \<open>N < length D\<close> fine by auto
   423 
   424     have "fine \<delta>1 (a,b) D1" unfolding D1_def
   425     proof (rule fine_append)
   426       show "fine \<delta>1 (a, d) ?D1"
   427       proof (rule fine1[THEN fine_\<delta>_expand])
   428         fix x assume "a \<le> x" "x \<le> d"
   429         hence "x \<le> b" using \<open>d < b\<close> \<open>x \<le> d\<close> by auto
   430         thus "\<delta> x \<le> \<delta>1 x" unfolding \<delta>_def by auto
   431       qed
   432 
   433       have "b - d < \<delta>1 t"
   434         using mem_fine3[OF fine in_D] \<delta>_def \<open>b \<le> e\<close> \<open>t = b\<close> by auto
   435       from \<open>d < b\<close> \<open>d \<le> t\<close> \<open>t = b\<close> this
   436       show "fine \<delta>1 (d, b) [(d, t, b)]" using fine_single by auto
   437     qed
   438     note rsum1 = I1[OF this]
   439 
   440     have drop_split: "drop N D = [D ! N] @ drop (Suc N) D"
   441       using Cons_nth_drop_Suc[OF \<open>N < length D\<close>] by simp
   442 
   443     have fine2: "fine \<delta>2 (e,c) (drop (Suc N) D)"
   444     proof (cases "drop (Suc N) D = []")
   445       case True
   446       note * = fine2[simplified drop_split True D_eq append_Nil2]
   447       have "e = c" using fine_single_boundaries[OF * refl] by auto
   448       thus ?thesis unfolding True using fine_Nil by auto
   449     next
   450       case False
   451       note * = fine_append_split[OF fine2 False drop_split]
   452       from fine_single_boundaries[OF *(1)]
   453       have "fst (hd (drop (Suc N) D)) = e" using D_eq by auto
   454       with *(2) have "fine \<delta> (e,c) (drop (Suc N) D)" by auto
   455       thus ?thesis
   456       proof (rule fine_\<delta>_expand)
   457         fix x assume "e \<le> x" and "x \<le> c"
   458         thus "\<delta> x \<le> \<delta>2 x" using \<open>b \<le> e\<close> unfolding \<delta>_def by auto
   459       qed
   460     qed
   461 
   462     have "fine \<delta>2 (b, c) D2"
   463     proof (cases "e = b")
   464       case True thus ?thesis using fine2 by (simp add: D1_def D2_def)
   465     next
   466       case False
   467       have "e - b < \<delta>2 b"
   468         using mem_fine3[OF fine in_D] \<delta>_def \<open>d < b\<close> \<open>t = b\<close> by auto
   469       with False \<open>t = b\<close> \<open>b \<le> e\<close>
   470       show ?thesis using D2_def
   471         by (auto intro!: fine_append[OF _ fine2] fine_single
   472                simp del: append_Cons)
   473     qed
   474     note rsum2 = I2[OF this]
   475 
   476     have "rsum D f = rsum (take N D) f + rsum [D ! N] f + rsum (drop (Suc N) D) f"
   477       using rsum_append[symmetric] Cons_nth_drop_Suc[OF \<open>N < length D\<close>] by auto
   478     also have "\<dots> = rsum D1 f + rsum D2 f"
   479       by (cases "b = e", auto simp add: D1_def D2_def D_eq rsum_append algebra_simps)
   480     finally have "\<bar>rsum D f - (x1 + x2)\<bar> < \<epsilon>"
   481       using add_strict_mono[OF rsum1 rsum2] by simp
   482   }
   483   ultimately show "\<exists> \<delta>. gauge {a .. c} \<delta> \<and>
   484     (\<forall>D. fine \<delta> (a,c) D \<longrightarrow> \<bar>rsum D f - (x1 + x2)\<bar> < \<epsilon>)"
   485     by blast
   486 next
   487   case False
   488   hence "a = b \<or> b = c" using \<open>a \<le> b\<close> and \<open>b \<le> c\<close> by auto
   489   thus ?thesis
   490   proof (rule disjE)
   491     assume "a = b" hence "x1 = 0"
   492       using \<open>Integral (a, b) f x1\<close> by simp
   493     thus ?thesis using \<open>a = b\<close> \<open>Integral (b, c) f x2\<close> by simp
   494   next
   495     assume "b = c" hence "x2 = 0"
   496       using \<open>Integral (b, c) f x2\<close> by simp
   497     thus ?thesis using \<open>b = c\<close> \<open>Integral (a, b) f x1\<close> by simp
   498   qed
   499 qed
   500 
   501 text\<open>Fundamental theorem of calculus (Part I)\<close>
   502 
   503 text\<open>"Straddle Lemma" : Swartz and Thompson: AMM 95(7) 1988\<close>
   504 
   505 lemma strad1:
   506   fixes z x s e :: real
   507   assumes P: "(\<And>z. z \<noteq> x \<Longrightarrow> \<bar>z - x\<bar> < s \<Longrightarrow> \<bar>(f z - f x) / (z - x) - f' x\<bar> < e / 2)"
   508   assumes "\<bar>z - x\<bar> < s"
   509   shows "\<bar>f z - f x - f' x * (z - x)\<bar> \<le> e / 2 * \<bar>z - x\<bar>"
   510 proof (cases "z = x")
   511   case True then show ?thesis by simp
   512 next
   513   case False
   514   then have "inverse (z - x) * (f z - f x - f' x * (z - x)) = (f z - f x) / (z - x) - f' x"
   515     apply (subst mult.commute)
   516     apply (simp add: left_diff_distrib)
   517     apply (simp add: mult.assoc divide_inverse)
   518     apply (simp add: ring_distribs)
   519     done
   520   moreover from False \<open>\<bar>z - x\<bar> < s\<close> have "\<bar>(f z - f x) / (z - x) - f' x\<bar> < e / 2"
   521     by (rule P)
   522   ultimately have "\<bar>inverse (z - x)\<bar> * (\<bar>f z - f x - f' x * (z - x)\<bar> * 2)
   523     \<le> \<bar>inverse (z - x)\<bar> * (e * \<bar>z - x\<bar>)"
   524     using False by (simp del: abs_inverse add: abs_mult [symmetric] ac_simps)
   525   with False have "\<bar>f z - f x - f' x * (z - x)\<bar> * 2 \<le> e * \<bar>z - x\<bar>"
   526     by simp
   527   then show ?thesis by simp
   528 qed
   529 
   530 lemma lemma_straddle:
   531   assumes f': "\<forall>x. a \<le> x & x \<le> b --> DERIV f x :> f'(x)" and "0 < e"
   532   shows "\<exists>g. gauge {a..b} g &
   533                 (\<forall>x u v. a \<le> u & u \<le> x & x \<le> v & v \<le> b & (v - u) < g(x)
   534                   --> \<bar>(f(v) - f(u)) - (f'(x) * (v - u))\<bar> \<le> e * (v - u))"
   535 proof -
   536   have "\<forall>x\<in>{a..b}.
   537         (\<exists>d > 0. \<forall>u v. u \<le> x & x \<le> v & (v - u) < d --> 
   538                        \<bar>(f(v) - f(u)) - (f'(x) * (v - u))\<bar> \<le> e * (v - u))"
   539   proof (clarsimp)
   540     fix x :: real assume "a \<le> x" and "x \<le> b"
   541     with f' have "DERIV f x :> f'(x)" by simp
   542     then have "\<forall>r>0. \<exists>s>0. \<forall>z. z \<noteq> x \<and> \<bar>z - x\<bar> < s \<longrightarrow> \<bar>(f z - f x) / (z - x) - f' x\<bar> < r"
   543       by (simp add: DERIV_iff2 LIM_eq)
   544     with \<open>0 < e\<close> obtain s
   545     where "z \<noteq> x \<Longrightarrow> \<bar>z - x\<bar> < s \<Longrightarrow> \<bar>(f z - f x) / (z - x) - f' x\<bar> < e/2" and "0 < s" for z
   546       by (drule_tac x="e/2" in spec, auto)
   547     with strad1 [of x s f f' e] have strad:
   548         "\<And>z. \<bar>z - x\<bar> < s \<Longrightarrow> \<bar>f z - f x - f' x * (z - x)\<bar> \<le> e/2 * \<bar>z - x\<bar>"
   549       by auto
   550     show "\<exists>d>0. \<forall>u v. u \<le> x \<and> x \<le> v \<and> v - u < d \<longrightarrow> \<bar>f v - f u - f' x * (v - u)\<bar> \<le> e * (v - u)"
   551     proof (safe intro!: exI)
   552       show "0 < s" by fact
   553     next
   554       fix u v :: real assume "u \<le> x" and "x \<le> v" and "v - u < s"
   555       have "\<bar>f v - f u - f' x * (v - u)\<bar> =
   556             \<bar>(f v - f x - f' x * (v - x)) + (f x - f u - f' x * (x - u))\<bar>"
   557         by (simp add: right_diff_distrib)
   558       also have "\<dots> \<le> \<bar>f v - f x - f' x * (v - x)\<bar> + \<bar>f x - f u - f' x * (x - u)\<bar>"
   559         by (rule abs_triangle_ineq)
   560       also have "\<dots> = \<bar>f v - f x - f' x * (v - x)\<bar> + \<bar>f u - f x - f' x * (u - x)\<bar>"
   561         by (simp add: right_diff_distrib)
   562       also have "\<dots> \<le> (e/2) * \<bar>v - x\<bar> + (e/2) * \<bar>u - x\<bar>"
   563         using \<open>u \<le> x\<close> \<open>x \<le> v\<close> \<open>v - u < s\<close> by (intro add_mono strad, simp_all)
   564       also have "\<dots> \<le> e * (v - u) / 2 + e * (v - u) / 2"
   565         using \<open>u \<le> x\<close> \<open>x \<le> v\<close> \<open>0 < e\<close> by (intro add_mono, simp_all)
   566       also have "\<dots> = e * (v - u)"
   567         by simp
   568       finally show "\<bar>f v - f u - f' x * (v - u)\<bar> \<le> e * (v - u)" .
   569     qed
   570   qed
   571   thus ?thesis
   572     by (simp add: gauge_def) (drule bchoice, auto)
   573 qed
   574 
   575 lemma fundamental_theorem_of_calculus:
   576   assumes "a \<le> b"
   577   assumes f': "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> DERIV f x :> f'(x)"
   578   shows "Integral (a, b) f' (f(b) - f(a))"
   579 proof (cases "a = b")
   580   assume "a = b" thus ?thesis by simp
   581 next
   582   assume "a \<noteq> b" with \<open>a \<le> b\<close> have "a < b" by simp
   583   show ?thesis
   584   proof (simp add: Integral_def2, clarify)
   585     fix e :: real assume "0 < e"
   586     with \<open>a < b\<close> have "0 < e / (b - a)" by simp
   587 
   588     from lemma_straddle [OF f' this]
   589     obtain \<delta> where "gauge {a..b} \<delta>"
   590       and \<delta>: "\<lbrakk>a \<le> u; u \<le> x; x \<le> v; v \<le> b; v - u < \<delta> x\<rbrakk> \<Longrightarrow>
   591            \<bar>f v - f u - f' x * (v - u)\<bar> \<le> e * (v - u) / (b - a)" for x u v by auto
   592 
   593     have "\<forall>D. fine \<delta> (a, b) D \<longrightarrow> \<bar>rsum D f' - (f b - f a)\<bar> \<le> e"
   594     proof (clarify)
   595       fix D assume D: "fine \<delta> (a, b) D"
   596       hence "(\<Sum>(u, x, v)\<leftarrow>D. f v - f u) = f b - f a"
   597         by (rule fine_listsum_eq_diff)
   598       hence "\<bar>rsum D f' - (f b - f a)\<bar> = \<bar>rsum D f' - (\<Sum>(u, x, v)\<leftarrow>D. f v - f u)\<bar>"
   599         by simp
   600       also have "\<dots> = \<bar>(\<Sum>(u, x, v)\<leftarrow>D. f v - f u) - rsum D f'\<bar>"
   601         by (rule abs_minus_commute)
   602       also have "\<dots> = \<bar>\<Sum>(u, x, v)\<leftarrow>D. (f v - f u) - f' x * (v - u)\<bar>"
   603         by (simp only: rsum_def listsum_subtractf split_def)
   604       also have "\<dots> \<le> (\<Sum>(u, x, v)\<leftarrow>D. \<bar>(f v - f u) - f' x * (v - u)\<bar>)"
   605         by (rule ord_le_eq_trans [OF listsum_abs], simp add: o_def split_def)
   606       also have "\<dots> \<le> (\<Sum>(u, x, v)\<leftarrow>D. (e / (b - a)) * (v - u))"
   607         apply (rule listsum_mono, clarify, rename_tac u x v)
   608         using D apply (simp add: \<delta> mem_fine mem_fine2 mem_fine3)
   609         done
   610       also have "\<dots> = e"
   611         using fine_listsum_eq_diff [OF D, where f="\<lambda>x. x"]
   612         unfolding split_def listsum_const_mult
   613         using \<open>a < b\<close> by simp
   614       finally show "\<bar>rsum D f' - (f b - f a)\<bar> \<le> e" .
   615     qed
   616 
   617     with \<open>gauge {a..b} \<delta>\<close>
   618     show "\<exists>\<delta>. gauge {a..b} \<delta> \<and> (\<forall>D. fine \<delta> (a, b) D \<longrightarrow> \<bar>rsum D f' - (f b - f a)\<bar> \<le> e)"
   619       by auto
   620   qed
   621 qed
   622 
   623 end