src/HOL/Analysis/Henstock_Kurzweil_Integration.thy
changeset 63627 6ddb43c6b711
parent 63595 aca2659ebba7
child 63659 abe0c3872d8a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Analysis/Henstock_Kurzweil_Integration.thy	Mon Aug 08 14:13:14 2016 +0200
     1.3 @@ -0,0 +1,12512 @@
     1.4 +(*  Author:     John Harrison
     1.5 +    Author:     Robert Himmelmann, TU Muenchen (Translation from HOL light); proofs reworked by LCP
     1.6 +*)
     1.7 +
     1.8 +section \<open>Henstock-Kurzweil gauge integration in many dimensions.\<close>
     1.9 +
    1.10 +theory Henstock_Kurzweil_Integration
    1.11 +imports
    1.12 +  Derivative
    1.13 +  Uniform_Limit
    1.14 +  "~~/src/HOL/Library/Indicator_Function"
    1.15 +begin
    1.16 +
    1.17 +lemmas scaleR_simps = scaleR_zero_left scaleR_minus_left scaleR_left_diff_distrib
    1.18 +  scaleR_zero_right scaleR_minus_right scaleR_right_diff_distrib scaleR_eq_0_iff
    1.19 +  scaleR_cancel_left scaleR_cancel_right scaleR_add_right scaleR_add_left real_vector_class.scaleR_one
    1.20 +
    1.21 +
    1.22 +subsection \<open>Sundries\<close>
    1.23 +
    1.24 +lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
    1.25 +lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
    1.26 +lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
    1.27 +
    1.28 +declare norm_triangle_ineq4[intro]
    1.29 +
    1.30 +lemma simple_image: "{f x |x . x \<in> s} = f ` s"
    1.31 +  by blast
    1.32 +
    1.33 +lemma linear_simps:
    1.34 +  assumes "bounded_linear f"
    1.35 +  shows
    1.36 +    "f (a + b) = f a + f b"
    1.37 +    "f (a - b) = f a - f b"
    1.38 +    "f 0 = 0"
    1.39 +    "f (- a) = - f a"
    1.40 +    "f (s *\<^sub>R v) = s *\<^sub>R (f v)"
    1.41 +proof -
    1.42 +  interpret f: bounded_linear f by fact
    1.43 +  show "f (a + b) = f a + f b" by (rule f.add)
    1.44 +  show "f (a - b) = f a - f b" by (rule f.diff)
    1.45 +  show "f 0 = 0" by (rule f.zero)
    1.46 +  show "f (- a) = - f a" by (rule f.minus)
    1.47 +  show "f (s *\<^sub>R v) = s *\<^sub>R (f v)" by (rule f.scaleR)
    1.48 +qed
    1.49 +
    1.50 +lemma bounded_linearI:
    1.51 +  assumes "\<And>x y. f (x + y) = f x + f y"
    1.52 +    and "\<And>r x. f (r *\<^sub>R x) = r *\<^sub>R f x"
    1.53 +    and "\<And>x. norm (f x) \<le> norm x * K"
    1.54 +  shows "bounded_linear f"
    1.55 +  using assms by (rule bounded_linear_intro) (* FIXME: duplicate *)
    1.56 +
    1.57 +lemma bounded_linear_component [intro]: "bounded_linear (\<lambda>x::'a::euclidean_space. x \<bullet> k)"
    1.58 +  by (rule bounded_linear_inner_left)
    1.59 +
    1.60 +lemma transitive_stepwise_lt_eq:
    1.61 +  assumes "(\<And>x y z::nat. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z)"
    1.62 +  shows "((\<forall>m. \<forall>n>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n)))"
    1.63 +  (is "?l = ?r")
    1.64 +proof safe
    1.65 +  assume ?r
    1.66 +  fix n m :: nat
    1.67 +  assume "m < n"
    1.68 +  then show "R m n"
    1.69 +  proof (induct n arbitrary: m)
    1.70 +    case 0
    1.71 +    then show ?case by auto
    1.72 +  next
    1.73 +    case (Suc n)
    1.74 +    show ?case
    1.75 +    proof (cases "m < n")
    1.76 +      case True
    1.77 +      show ?thesis
    1.78 +        apply (rule assms[OF Suc(1)[OF True]])
    1.79 +        using \<open>?r\<close>
    1.80 +        apply auto
    1.81 +        done
    1.82 +    next
    1.83 +      case False
    1.84 +      then have "m = n"
    1.85 +        using Suc(2) by auto
    1.86 +      then show ?thesis
    1.87 +        using \<open>?r\<close> by auto
    1.88 +    qed
    1.89 +  qed
    1.90 +qed auto
    1.91 +
    1.92 +lemma transitive_stepwise_gt:
    1.93 +  assumes "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" "\<And>n. R n (Suc n)"
    1.94 +  shows "\<forall>n>m. R m n"
    1.95 +proof -
    1.96 +  have "\<forall>m. \<forall>n>m. R m n"
    1.97 +    apply (subst transitive_stepwise_lt_eq)
    1.98 +    apply (blast intro: assms)+
    1.99 +    done
   1.100 +  then show ?thesis by auto
   1.101 +qed
   1.102 +
   1.103 +lemma transitive_stepwise_le_eq:
   1.104 +  assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
   1.105 +  shows "(\<forall>m. \<forall>n\<ge>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n))"
   1.106 +  (is "?l = ?r")
   1.107 +proof safe
   1.108 +  assume ?r
   1.109 +  fix m n :: nat
   1.110 +  assume "m \<le> n"
   1.111 +  then show "R m n"
   1.112 +  proof (induct n arbitrary: m)
   1.113 +    case 0
   1.114 +    with assms show ?case by auto
   1.115 +  next
   1.116 +    case (Suc n)
   1.117 +    show ?case
   1.118 +    proof (cases "m \<le> n")
   1.119 +      case True
   1.120 +      with Suc.hyps \<open>\<forall>n. R n (Suc n)\<close> assms show ?thesis
   1.121 +        by blast
   1.122 +    next
   1.123 +      case False
   1.124 +      then have "m = Suc n"
   1.125 +        using Suc(2) by auto
   1.126 +      then show ?thesis
   1.127 +        using assms(1) by auto
   1.128 +    qed
   1.129 +  qed
   1.130 +qed auto
   1.131 +
   1.132 +lemma transitive_stepwise_le:
   1.133 +  assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
   1.134 +    and "\<And>n. R n (Suc n)"
   1.135 +  shows "\<forall>n\<ge>m. R m n"
   1.136 +proof -
   1.137 +  have "\<forall>m. \<forall>n\<ge>m. R m n"
   1.138 +    apply (subst transitive_stepwise_le_eq)
   1.139 +    apply (blast intro: assms)+
   1.140 +    done
   1.141 +  then show ?thesis by auto
   1.142 +qed
   1.143 +
   1.144 +
   1.145 +subsection \<open>Some useful lemmas about intervals.\<close>
   1.146 +
   1.147 +lemma empty_as_interval: "{} = cbox One (0::'a::euclidean_space)"
   1.148 +  using nonempty_Basis
   1.149 +  by (fastforce simp add: set_eq_iff mem_box)
   1.150 +
   1.151 +lemma interior_subset_union_intervals:
   1.152 +  assumes "i = cbox a b"
   1.153 +    and "j = cbox c d"
   1.154 +    and "interior j \<noteq> {}"
   1.155 +    and "i \<subseteq> j \<union> s"
   1.156 +    and "interior i \<inter> interior j = {}"
   1.157 +  shows "interior i \<subseteq> interior s"
   1.158 +proof -
   1.159 +  have "box a b \<inter> cbox c d = {}"
   1.160 +     using inter_interval_mixed_eq_empty[of c d a b] and assms(3,5)
   1.161 +     unfolding assms(1,2) interior_cbox by auto
   1.162 +  moreover
   1.163 +  have "box a b \<subseteq> cbox c d \<union> s"
   1.164 +    apply (rule order_trans,rule box_subset_cbox)
   1.165 +    using assms(4) unfolding assms(1,2)
   1.166 +    apply auto
   1.167 +    done
   1.168 +  ultimately
   1.169 +  show ?thesis
   1.170 +    unfolding assms interior_cbox
   1.171 +      by auto (metis IntI UnE empty_iff interior_maximal open_box subsetCE subsetI)
   1.172 +qed
   1.173 +
   1.174 +lemma interior_Union_subset_cbox:
   1.175 +  assumes "finite f"
   1.176 +  assumes f: "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = cbox a b" "\<And>s. s \<in> f \<Longrightarrow> interior s \<subseteq> t"
   1.177 +    and t: "closed t"
   1.178 +  shows "interior (\<Union>f) \<subseteq> t"
   1.179 +proof -
   1.180 +  have [simp]: "s \<in> f \<Longrightarrow> closed s" for s
   1.181 +    using f by auto
   1.182 +  define E where "E = {s\<in>f. interior s = {}}"
   1.183 +  then have "finite E" "E \<subseteq> {s\<in>f. interior s = {}}"
   1.184 +    using \<open>finite f\<close> by auto
   1.185 +  then have "interior (\<Union>f) = interior (\<Union>(f - E))"
   1.186 +  proof (induction E rule: finite_subset_induct')
   1.187 +    case (insert s f')
   1.188 +    have "interior (\<Union>(f - insert s f') \<union> s) = interior (\<Union>(f - insert s f'))"
   1.189 +      using insert.hyps \<open>finite f\<close> by (intro interior_closed_Un_empty_interior) auto
   1.190 +    also have "\<Union>(f - insert s f') \<union> s = \<Union>(f - f')"
   1.191 +      using insert.hyps by auto
   1.192 +    finally show ?case
   1.193 +      by (simp add: insert.IH)
   1.194 +  qed simp
   1.195 +  also have "\<dots> \<subseteq> \<Union>(f - E)"
   1.196 +    by (rule interior_subset)
   1.197 +  also have "\<dots> \<subseteq> t"
   1.198 +  proof (rule Union_least)
   1.199 +    fix s assume "s \<in> f - E"
   1.200 +    with f[of s] obtain a b where s: "s \<in> f" "s = cbox a b" "box a b \<noteq> {}"
   1.201 +      by (fastforce simp: E_def)
   1.202 +    have "closure (interior s) \<subseteq> closure t"
   1.203 +      by (intro closure_mono f \<open>s \<in> f\<close>)
   1.204 +    with s \<open>closed t\<close> show "s \<subseteq> t"
   1.205 +      by (simp add: closure_box)
   1.206 +  qed
   1.207 +  finally show ?thesis .
   1.208 +qed
   1.209 +
   1.210 +lemma inter_interior_unions_intervals:
   1.211 +    "finite f \<Longrightarrow> open s \<Longrightarrow> \<forall>t\<in>f. \<exists>a b. t = cbox a b \<Longrightarrow> \<forall>t\<in>f. s \<inter> (interior t) = {} \<Longrightarrow> s \<inter> interior (\<Union>f) = {}"
   1.212 +  using interior_Union_subset_cbox[of f "UNIV - s"] by auto
   1.213 +
   1.214 +subsection \<open>Bounds on intervals where they exist.\<close>
   1.215 +
   1.216 +definition interval_upperbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
   1.217 +  where "interval_upperbound s = (\<Sum>i\<in>Basis. (SUP x:s. x\<bullet>i) *\<^sub>R i)"
   1.218 +
   1.219 +definition interval_lowerbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
   1.220 +   where "interval_lowerbound s = (\<Sum>i\<in>Basis. (INF x:s. x\<bullet>i) *\<^sub>R i)"
   1.221 +
   1.222 +lemma interval_upperbound[simp]:
   1.223 +  "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
   1.224 +    interval_upperbound (cbox a b) = (b::'a::euclidean_space)"
   1.225 +  unfolding interval_upperbound_def euclidean_representation_setsum cbox_def
   1.226 +  by (safe intro!: cSup_eq) auto
   1.227 +
   1.228 +lemma interval_lowerbound[simp]:
   1.229 +  "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
   1.230 +    interval_lowerbound (cbox a b) = (a::'a::euclidean_space)"
   1.231 +  unfolding interval_lowerbound_def euclidean_representation_setsum cbox_def
   1.232 +  by (safe intro!: cInf_eq) auto
   1.233 +
   1.234 +lemmas interval_bounds = interval_upperbound interval_lowerbound
   1.235 +
   1.236 +lemma
   1.237 +  fixes X::"real set"
   1.238 +  shows interval_upperbound_real[simp]: "interval_upperbound X = Sup X"
   1.239 +    and interval_lowerbound_real[simp]: "interval_lowerbound X = Inf X"
   1.240 +  by (auto simp: interval_upperbound_def interval_lowerbound_def)
   1.241 +
   1.242 +lemma interval_bounds'[simp]:
   1.243 +  assumes "cbox a b \<noteq> {}"
   1.244 +  shows "interval_upperbound (cbox a b) = b"
   1.245 +    and "interval_lowerbound (cbox a b) = a"
   1.246 +  using assms unfolding box_ne_empty by auto
   1.247 +
   1.248 +
   1.249 +lemma interval_upperbound_Times:
   1.250 +  assumes "A \<noteq> {}" and "B \<noteq> {}"
   1.251 +  shows "interval_upperbound (A \<times> B) = (interval_upperbound A, interval_upperbound B)"
   1.252 +proof-
   1.253 +  from assms have fst_image_times': "A = fst ` (A \<times> B)" by simp
   1.254 +  have "(\<Sum>i\<in>Basis. (SUP x:A \<times> B. x \<bullet> (i, 0)) *\<^sub>R i) = (\<Sum>i\<in>Basis. (SUP x:A. x \<bullet> i) *\<^sub>R i)"
   1.255 +      by (subst (2) fst_image_times') (simp del: fst_image_times add: o_def inner_Pair_0)
   1.256 +  moreover from assms have snd_image_times': "B = snd ` (A \<times> B)" by simp
   1.257 +  have "(\<Sum>i\<in>Basis. (SUP x:A \<times> B. x \<bullet> (0, i)) *\<^sub>R i) = (\<Sum>i\<in>Basis. (SUP x:B. x \<bullet> i) *\<^sub>R i)"
   1.258 +      by (subst (2) snd_image_times') (simp del: snd_image_times add: o_def inner_Pair_0)
   1.259 +  ultimately show ?thesis unfolding interval_upperbound_def
   1.260 +      by (subst setsum_Basis_prod_eq) (auto simp add: setsum_prod)
   1.261 +qed
   1.262 +
   1.263 +lemma interval_lowerbound_Times:
   1.264 +  assumes "A \<noteq> {}" and "B \<noteq> {}"
   1.265 +  shows "interval_lowerbound (A \<times> B) = (interval_lowerbound A, interval_lowerbound B)"
   1.266 +proof-
   1.267 +  from assms have fst_image_times': "A = fst ` (A \<times> B)" by simp
   1.268 +  have "(\<Sum>i\<in>Basis. (INF x:A \<times> B. x \<bullet> (i, 0)) *\<^sub>R i) = (\<Sum>i\<in>Basis. (INF x:A. x \<bullet> i) *\<^sub>R i)"
   1.269 +      by (subst (2) fst_image_times') (simp del: fst_image_times add: o_def inner_Pair_0)
   1.270 +  moreover from assms have snd_image_times': "B = snd ` (A \<times> B)" by simp
   1.271 +  have "(\<Sum>i\<in>Basis. (INF x:A \<times> B. x \<bullet> (0, i)) *\<^sub>R i) = (\<Sum>i\<in>Basis. (INF x:B. x \<bullet> i) *\<^sub>R i)"
   1.272 +      by (subst (2) snd_image_times') (simp del: snd_image_times add: o_def inner_Pair_0)
   1.273 +  ultimately show ?thesis unfolding interval_lowerbound_def
   1.274 +      by (subst setsum_Basis_prod_eq) (auto simp add: setsum_prod)
   1.275 +qed
   1.276 +
   1.277 +subsection \<open>Content (length, area, volume...) of an interval.\<close>
   1.278 +
   1.279 +definition "content (s::('a::euclidean_space) set) =
   1.280 +  (if s = {} then 0 else (\<Prod>i\<in>Basis. (interval_upperbound s)\<bullet>i - (interval_lowerbound s)\<bullet>i))"
   1.281 +
   1.282 +lemma interval_not_empty: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow> cbox a b \<noteq> {}"
   1.283 +  unfolding box_eq_empty unfolding not_ex not_less by auto
   1.284 +
   1.285 +lemma content_cbox:
   1.286 +  fixes a :: "'a::euclidean_space"
   1.287 +  assumes "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
   1.288 +  shows "content (cbox a b) = (\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
   1.289 +  using interval_not_empty[OF assms]
   1.290 +  unfolding content_def
   1.291 +  by auto
   1.292 +
   1.293 +lemma content_cbox':
   1.294 +  fixes a :: "'a::euclidean_space"
   1.295 +  assumes "cbox a b \<noteq> {}"
   1.296 +  shows "content (cbox a b) = (\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
   1.297 +    using assms box_ne_empty(1) content_cbox by blast
   1.298 +
   1.299 +lemma content_real: "a \<le> b \<Longrightarrow> content {a..b} = b - a"
   1.300 +  by (auto simp: interval_upperbound_def interval_lowerbound_def content_def)
   1.301 +
   1.302 +lemma abs_eq_content: "\<bar>y - x\<bar> = (if x\<le>y then content {x .. y} else content {y..x})"
   1.303 +  by (auto simp: content_real)
   1.304 +
   1.305 +lemma content_singleton[simp]: "content {a} = 0"
   1.306 +proof -
   1.307 +  have "content (cbox a a) = 0"
   1.308 +    by (subst content_cbox) (auto simp: ex_in_conv)
   1.309 +  then show ?thesis by (simp add: cbox_sing)
   1.310 +qed
   1.311 +
   1.312 +lemma content_unit[iff]: "content(cbox 0 (One::'a::euclidean_space)) = 1"
   1.313 + proof -
   1.314 +   have *: "\<forall>i\<in>Basis. (0::'a)\<bullet>i \<le> (One::'a)\<bullet>i"
   1.315 +    by auto
   1.316 +  have "0 \<in> cbox 0 (One::'a)"
   1.317 +    unfolding mem_box by auto
   1.318 +  then show ?thesis
   1.319 +     unfolding content_def interval_bounds[OF *] using setprod.neutral_const by auto
   1.320 + qed
   1.321 +
   1.322 +lemma content_pos_le[intro]:
   1.323 +  fixes a::"'a::euclidean_space"
   1.324 +  shows "0 \<le> content (cbox a b)"
   1.325 +proof (cases "cbox a b = {}")
   1.326 +  case False
   1.327 +  then have *: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
   1.328 +    unfolding box_ne_empty .
   1.329 +  have "0 \<le> (\<Prod>i\<in>Basis. interval_upperbound (cbox a b) \<bullet> i - interval_lowerbound (cbox a b) \<bullet> i)"
   1.330 +    apply (rule setprod_nonneg)
   1.331 +    unfolding interval_bounds[OF *]
   1.332 +    using *
   1.333 +    apply auto
   1.334 +    done
   1.335 +  also have "\<dots> = content (cbox a b)" using False by (simp add: content_def)
   1.336 +  finally show ?thesis .
   1.337 +qed (simp add: content_def)
   1.338 +
   1.339 +corollary content_nonneg [simp]:
   1.340 +  fixes a::"'a::euclidean_space"
   1.341 +  shows "~ content (cbox a b) < 0"
   1.342 +using not_le by blast
   1.343 +
   1.344 +lemma content_pos_lt:
   1.345 +  fixes a :: "'a::euclidean_space"
   1.346 +  assumes "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"
   1.347 +  shows "0 < content (cbox a b)"
   1.348 +  using assms
   1.349 +  by (auto simp: content_def box_eq_empty intro!: setprod_pos)
   1.350 +
   1.351 +lemma content_eq_0:
   1.352 +  "content (cbox a b) = 0 \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i \<le> a\<bullet>i)"
   1.353 +  by (auto simp: content_def box_eq_empty intro!: setprod_pos bexI)
   1.354 +
   1.355 +lemma cond_cases: "(P \<Longrightarrow> Q x) \<Longrightarrow> (\<not> P \<Longrightarrow> Q y) \<Longrightarrow> Q (if P then x else y)"
   1.356 +  by auto
   1.357 +
   1.358 +lemma content_cbox_cases:
   1.359 +  "content (cbox a (b::'a::euclidean_space)) =
   1.360 +    (if \<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i then setprod (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis else 0)"
   1.361 +  by (auto simp: not_le content_eq_0 intro: less_imp_le content_cbox)
   1.362 +
   1.363 +lemma content_eq_0_interior: "content (cbox a b) = 0 \<longleftrightarrow> interior(cbox a b) = {}"
   1.364 +  unfolding content_eq_0 interior_cbox box_eq_empty
   1.365 +  by auto
   1.366 +
   1.367 +lemma content_pos_lt_eq:
   1.368 +  "0 < content (cbox a (b::'a::euclidean_space)) \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i)"
   1.369 +proof (rule iffI)
   1.370 +  assume "0 < content (cbox a b)"
   1.371 +  then have "content (cbox a b) \<noteq> 0" by auto
   1.372 +  then show "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"
   1.373 +    unfolding content_eq_0 not_ex not_le by fastforce
   1.374 +next
   1.375 +  assume "\<forall>i\<in>Basis. a \<bullet> i < b \<bullet> i"
   1.376 +  then show "0 < content (cbox a b)"
   1.377 +    by (metis content_pos_lt)
   1.378 +qed
   1.379 +
   1.380 +lemma content_empty [simp]: "content {} = 0"
   1.381 +  unfolding content_def by auto
   1.382 +
   1.383 +lemma content_real_if [simp]: "content {a..b} = (if a \<le> b then b - a else 0)"
   1.384 +  by (simp add: content_real)
   1.385 +
   1.386 +lemma content_subset:
   1.387 +  assumes "cbox a b \<subseteq> cbox c d"
   1.388 +  shows "content (cbox a b) \<le> content (cbox c d)"
   1.389 +proof (cases "cbox a b = {}")
   1.390 +  case True
   1.391 +  then show ?thesis
   1.392 +    using content_pos_le[of c d] by auto
   1.393 +next
   1.394 +  case False
   1.395 +  then have ab_ne: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
   1.396 +    unfolding box_ne_empty by auto
   1.397 +  then have ab_ab: "a\<in>cbox a b" "b\<in>cbox a b"
   1.398 +    unfolding mem_box by auto
   1.399 +  have "cbox c d \<noteq> {}" using assms False by auto
   1.400 +  then have cd_ne: "\<forall>i\<in>Basis. c \<bullet> i \<le> d \<bullet> i"
   1.401 +    using assms unfolding box_ne_empty by auto
   1.402 +  have "\<And>i. i \<in> Basis \<Longrightarrow> 0 \<le> b \<bullet> i - a \<bullet> i"
   1.403 +    using ab_ne by auto
   1.404 +  moreover
   1.405 +  have "\<And>i. i \<in> Basis \<Longrightarrow> b \<bullet> i - a \<bullet> i \<le> d \<bullet> i - c \<bullet> i"
   1.406 +    using assms[unfolded subset_eq mem_box,rule_format,OF ab_ab(2)]
   1.407 +          assms[unfolded subset_eq mem_box,rule_format,OF ab_ab(1)]
   1.408 +      by (metis diff_mono)
   1.409 +  ultimately show ?thesis
   1.410 +    unfolding content_def interval_bounds[OF ab_ne] interval_bounds[OF cd_ne]
   1.411 +    by (simp add: setprod_mono if_not_P[OF False] if_not_P[OF \<open>cbox c d \<noteq> {}\<close>])
   1.412 +qed
   1.413 +
   1.414 +lemma content_lt_nz: "0 < content (cbox a b) \<longleftrightarrow> content (cbox a b) \<noteq> 0"
   1.415 +  unfolding content_pos_lt_eq content_eq_0 unfolding not_ex not_le by fastforce
   1.416 +
   1.417 +lemma content_times[simp]: "content (A \<times> B) = content A * content B"
   1.418 +proof (cases "A \<times> B = {}")
   1.419 +  let ?ub1 = "interval_upperbound" and ?lb1 = "interval_lowerbound"
   1.420 +  let ?ub2 = "interval_upperbound" and ?lb2 = "interval_lowerbound"
   1.421 +  assume nonempty: "A \<times> B \<noteq> {}"
   1.422 +  hence "content (A \<times> B) = (\<Prod>i\<in>Basis. (?ub1 A, ?ub2 B) \<bullet> i - (?lb1 A, ?lb2 B) \<bullet> i)"
   1.423 +      unfolding content_def by (simp add: interval_upperbound_Times interval_lowerbound_Times)
   1.424 +  also have "... = content A * content B" unfolding content_def using nonempty
   1.425 +    apply (subst Basis_prod_def, subst setprod.union_disjoint, force, force, force, simp)
   1.426 +    apply (subst (1 2) setprod.reindex, auto intro: inj_onI)
   1.427 +    done
   1.428 +  finally show ?thesis .
   1.429 +qed (auto simp: content_def)
   1.430 +
   1.431 +lemma content_Pair: "content (cbox (a,c) (b,d)) = content (cbox a b) * content (cbox c d)"
   1.432 +  by (simp add: cbox_Pair_eq)
   1.433 +
   1.434 +lemma content_cbox_pair_eq0_D:
   1.435 +   "content (cbox (a,c) (b,d)) = 0 \<Longrightarrow> content (cbox a b) = 0 \<or> content (cbox c d) = 0"
   1.436 +  by (simp add: content_Pair)
   1.437 +
   1.438 +lemma content_eq_0_gen:
   1.439 +  fixes s :: "'a::euclidean_space set"
   1.440 +  assumes "bounded s"
   1.441 +  shows "content s = 0 \<longleftrightarrow> (\<exists>i\<in>Basis. \<exists>v. \<forall>x \<in> s. x \<bullet> i = v)"  (is "_ = ?rhs")
   1.442 +proof safe
   1.443 +  assume "content s = 0" then show ?rhs
   1.444 +    apply (clarsimp simp: ex_in_conv content_def split: if_split_asm)
   1.445 +    apply (rule_tac x=a in bexI)
   1.446 +    apply (rule_tac x="interval_lowerbound s \<bullet> a" in exI)
   1.447 +    apply (clarsimp simp: interval_upperbound_def interval_lowerbound_def)
   1.448 +    apply (drule cSUP_eq_cINF_D)
   1.449 +    apply (auto simp: bounded_inner_imp_bdd_above [OF assms]  bounded_inner_imp_bdd_below [OF assms])
   1.450 +    done
   1.451 +next
   1.452 +  fix i a
   1.453 +  assume "i \<in> Basis" "\<forall>x\<in>s. x \<bullet> i = a"
   1.454 +  then show "content s = 0"
   1.455 +    apply (clarsimp simp: content_def)
   1.456 +    apply (rule_tac x=i in bexI)
   1.457 +    apply (auto simp: interval_upperbound_def interval_lowerbound_def)
   1.458 +    done
   1.459 +qed
   1.460 +
   1.461 +lemma content_0_subset_gen:
   1.462 +  fixes a :: "'a::euclidean_space"
   1.463 +  assumes "content t = 0" "s \<subseteq> t" "bounded t" shows "content s = 0"
   1.464 +proof -
   1.465 +  have "bounded s"
   1.466 +    using assms by (metis bounded_subset)
   1.467 +  then show ?thesis
   1.468 +    using assms
   1.469 +    by (auto simp: content_eq_0_gen)
   1.470 +qed
   1.471 +
   1.472 +lemma content_0_subset: "\<lbrakk>content(cbox a b) = 0; s \<subseteq> cbox a b\<rbrakk> \<Longrightarrow> content s = 0"
   1.473 +  by (simp add: content_0_subset_gen bounded_cbox)
   1.474 +
   1.475 +
   1.476 +lemma interval_split:
   1.477 +  fixes a :: "'a::euclidean_space"
   1.478 +  assumes "k \<in> Basis"
   1.479 +  shows
   1.480 +    "cbox a b \<inter> {x. x\<bullet>k \<le> c} = cbox a (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) c else b\<bullet>i) *\<^sub>R i)"
   1.481 +    "cbox a b \<inter> {x. x\<bullet>k \<ge> c} = cbox (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) c else a\<bullet>i) *\<^sub>R i) b"
   1.482 +  apply (rule_tac[!] set_eqI)
   1.483 +  unfolding Int_iff mem_box mem_Collect_eq
   1.484 +  using assms
   1.485 +  apply auto
   1.486 +  done
   1.487 +
   1.488 +lemma content_split:
   1.489 +  fixes a :: "'a::euclidean_space"
   1.490 +  assumes "k \<in> Basis"
   1.491 +  shows "content (cbox a b) = content(cbox a b \<inter> {x. x\<bullet>k \<le> c}) + content(cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
   1.492 +proof cases
   1.493 +  note simps = interval_split[OF assms] content_cbox_cases
   1.494 +  have *: "Basis = insert k (Basis - {k})" "\<And>x. finite (Basis-{x})" "\<And>x. x\<notin>Basis-{x}"
   1.495 +    using assms by auto
   1.496 +  have *: "\<And>X Y Z. (\<Prod>i\<in>Basis. Z i (if i = k then X else Y i)) = Z k X * (\<Prod>i\<in>Basis-{k}. Z i (Y i))"
   1.497 +    "(\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i) = (\<Prod>i\<in>Basis-{k}. b\<bullet>i - a\<bullet>i) * (b\<bullet>k - a\<bullet>k)"
   1.498 +    apply (subst *(1))
   1.499 +    defer
   1.500 +    apply (subst *(1))
   1.501 +    unfolding setprod.insert[OF *(2-)]
   1.502 +    apply auto
   1.503 +    done
   1.504 +  assume as: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
   1.505 +  moreover
   1.506 +  have "\<And>x. min (b \<bullet> k) c = max (a \<bullet> k) c \<Longrightarrow>
   1.507 +    x * (b\<bullet>k - a\<bullet>k) = x * (max (a \<bullet> k) c - a \<bullet> k) + x * (b \<bullet> k - max (a \<bullet> k) c)"
   1.508 +    by  (auto simp add: field_simps)
   1.509 +  moreover
   1.510 +  have **: "(\<Prod>i\<in>Basis. ((\<Sum>i\<in>Basis. (if i = k then min (b \<bullet> k) c else b \<bullet> i) *\<^sub>R i) \<bullet> i - a \<bullet> i)) =
   1.511 +      (\<Prod>i\<in>Basis. (if i = k then min (b \<bullet> k) c else b \<bullet> i) - a \<bullet> i)"
   1.512 +    "(\<Prod>i\<in>Basis. b \<bullet> i - ((\<Sum>i\<in>Basis. (if i = k then max (a \<bullet> k) c else a \<bullet> i) *\<^sub>R i) \<bullet> i)) =
   1.513 +      (\<Prod>i\<in>Basis. b \<bullet> i - (if i = k then max (a \<bullet> k) c else a \<bullet> i))"
   1.514 +    by (auto intro!: setprod.cong)
   1.515 +  have "\<not> a \<bullet> k \<le> c \<Longrightarrow> \<not> c \<le> b \<bullet> k \<Longrightarrow> False"
   1.516 +    unfolding not_le
   1.517 +    using as[unfolded ,rule_format,of k] assms
   1.518 +    by auto
   1.519 +  ultimately show ?thesis
   1.520 +    using assms
   1.521 +    unfolding simps **
   1.522 +    unfolding *(1)[of "\<lambda>i x. b\<bullet>i - x"] *(1)[of "\<lambda>i x. x - a\<bullet>i"]
   1.523 +    unfolding *(2)
   1.524 +    by auto
   1.525 +next
   1.526 +  assume "\<not> (\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)"
   1.527 +  then have "cbox a b = {}"
   1.528 +    unfolding box_eq_empty by (auto simp: not_le)
   1.529 +  then show ?thesis
   1.530 +    by (auto simp: not_le)
   1.531 +qed
   1.532 +
   1.533 +subsection \<open>The notion of a gauge --- simply an open set containing the point.\<close>
   1.534 +
   1.535 +definition "gauge d \<longleftrightarrow> (\<forall>x. x \<in> d x \<and> open (d x))"
   1.536 +
   1.537 +lemma gaugeI:
   1.538 +  assumes "\<And>x. x \<in> g x"
   1.539 +    and "\<And>x. open (g x)"
   1.540 +  shows "gauge g"
   1.541 +  using assms unfolding gauge_def by auto
   1.542 +
   1.543 +lemma gaugeD[dest]:
   1.544 +  assumes "gauge d"
   1.545 +  shows "x \<in> d x"
   1.546 +    and "open (d x)"
   1.547 +  using assms unfolding gauge_def by auto
   1.548 +
   1.549 +lemma gauge_ball_dependent: "\<forall>x. 0 < e x \<Longrightarrow> gauge (\<lambda>x. ball x (e x))"
   1.550 +  unfolding gauge_def by auto
   1.551 +
   1.552 +lemma gauge_ball[intro]: "0 < e \<Longrightarrow> gauge (\<lambda>x. ball x e)"
   1.553 +  unfolding gauge_def by auto
   1.554 +
   1.555 +lemma gauge_trivial[intro!]: "gauge (\<lambda>x. ball x 1)"
   1.556 +  by (rule gauge_ball) auto
   1.557 +
   1.558 +lemma gauge_inter[intro]: "gauge d1 \<Longrightarrow> gauge d2 \<Longrightarrow> gauge (\<lambda>x. d1 x \<inter> d2 x)"
   1.559 +  unfolding gauge_def by auto
   1.560 +
   1.561 +lemma gauge_inters:
   1.562 +  assumes "finite s"
   1.563 +    and "\<forall>d\<in>s. gauge (f d)"
   1.564 +  shows "gauge (\<lambda>x. \<Inter>{f d x | d. d \<in> s})"
   1.565 +proof -
   1.566 +  have *: "\<And>x. {f d x |d. d \<in> s} = (\<lambda>d. f d x) ` s"
   1.567 +    by auto
   1.568 +  show ?thesis
   1.569 +    unfolding gauge_def unfolding *
   1.570 +    using assms unfolding Ball_def Inter_iff mem_Collect_eq gauge_def by auto
   1.571 +qed
   1.572 +
   1.573 +lemma gauge_existence_lemma:
   1.574 +  "(\<forall>x. \<exists>d :: real. p x \<longrightarrow> 0 < d \<and> q d x) \<longleftrightarrow> (\<forall>x. \<exists>d>0. p x \<longrightarrow> q d x)"
   1.575 +  by (metis zero_less_one)
   1.576 +
   1.577 +
   1.578 +subsection \<open>Divisions.\<close>
   1.579 +
   1.580 +definition division_of (infixl "division'_of" 40)
   1.581 +where
   1.582 +  "s division_of i \<longleftrightarrow>
   1.583 +    finite s \<and>
   1.584 +    (\<forall>k\<in>s. k \<subseteq> i \<and> k \<noteq> {} \<and> (\<exists>a b. k = cbox a b)) \<and>
   1.585 +    (\<forall>k1\<in>s. \<forall>k2\<in>s. k1 \<noteq> k2 \<longrightarrow> interior(k1) \<inter> interior(k2) = {}) \<and>
   1.586 +    (\<Union>s = i)"
   1.587 +
   1.588 +lemma division_ofD[dest]:
   1.589 +  assumes "s division_of i"
   1.590 +  shows "finite s"
   1.591 +    and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
   1.592 +    and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
   1.593 +    and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
   1.594 +    and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}"
   1.595 +    and "\<Union>s = i"
   1.596 +  using assms unfolding division_of_def by auto
   1.597 +
   1.598 +lemma division_ofI:
   1.599 +  assumes "finite s"
   1.600 +    and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
   1.601 +    and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
   1.602 +    and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
   1.603 +    and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
   1.604 +    and "\<Union>s = i"
   1.605 +  shows "s division_of i"
   1.606 +  using assms unfolding division_of_def by auto
   1.607 +
   1.608 +lemma division_of_finite: "s division_of i \<Longrightarrow> finite s"
   1.609 +  unfolding division_of_def by auto
   1.610 +
   1.611 +lemma division_of_self[intro]: "cbox a b \<noteq> {} \<Longrightarrow> {cbox a b} division_of (cbox a b)"
   1.612 +  unfolding division_of_def by auto
   1.613 +
   1.614 +lemma division_of_trivial[simp]: "s division_of {} \<longleftrightarrow> s = {}"
   1.615 +  unfolding division_of_def by auto
   1.616 +
   1.617 +lemma division_of_sing[simp]:
   1.618 +  "s division_of cbox a (a::'a::euclidean_space) \<longleftrightarrow> s = {cbox a a}"
   1.619 +  (is "?l = ?r")
   1.620 +proof
   1.621 +  assume ?r
   1.622 +  moreover
   1.623 +  { fix k
   1.624 +    assume "s = {{a}}" "k\<in>s"
   1.625 +    then have "\<exists>x y. k = cbox x y"
   1.626 +      apply (rule_tac x=a in exI)+
   1.627 +      apply (force simp: cbox_sing)
   1.628 +      done
   1.629 +  }
   1.630 +  ultimately show ?l
   1.631 +    unfolding division_of_def cbox_sing by auto
   1.632 +next
   1.633 +  assume ?l
   1.634 +  note * = conjunctD4[OF this[unfolded division_of_def cbox_sing]]
   1.635 +  {
   1.636 +    fix x
   1.637 +    assume x: "x \<in> s" have "x = {a}"
   1.638 +      using *(2)[rule_format,OF x] by auto
   1.639 +  }
   1.640 +  moreover have "s \<noteq> {}"
   1.641 +    using *(4) by auto
   1.642 +  ultimately show ?r
   1.643 +    unfolding cbox_sing by auto
   1.644 +qed
   1.645 +
   1.646 +lemma elementary_empty: obtains p where "p division_of {}"
   1.647 +  unfolding division_of_trivial by auto
   1.648 +
   1.649 +lemma elementary_interval: obtains p where "p division_of (cbox a b)"
   1.650 +  by (metis division_of_trivial division_of_self)
   1.651 +
   1.652 +lemma division_contains: "s division_of i \<Longrightarrow> \<forall>x\<in>i. \<exists>k\<in>s. x \<in> k"
   1.653 +  unfolding division_of_def by auto
   1.654 +
   1.655 +lemma forall_in_division:
   1.656 +  "d division_of i \<Longrightarrow> (\<forall>x\<in>d. P x) \<longleftrightarrow> (\<forall>a b. cbox a b \<in> d \<longrightarrow> P (cbox a b))"
   1.657 +  unfolding division_of_def by fastforce
   1.658 +
   1.659 +lemma division_of_subset:
   1.660 +  assumes "p division_of (\<Union>p)"
   1.661 +    and "q \<subseteq> p"
   1.662 +  shows "q division_of (\<Union>q)"
   1.663 +proof (rule division_ofI)
   1.664 +  note * = division_ofD[OF assms(1)]
   1.665 +  show "finite q"
   1.666 +    using "*"(1) assms(2) infinite_super by auto
   1.667 +  {
   1.668 +    fix k
   1.669 +    assume "k \<in> q"
   1.670 +    then have kp: "k \<in> p"
   1.671 +      using assms(2) by auto
   1.672 +    show "k \<subseteq> \<Union>q"
   1.673 +      using \<open>k \<in> q\<close> by auto
   1.674 +    show "\<exists>a b. k = cbox a b"
   1.675 +      using *(4)[OF kp] by auto
   1.676 +    show "k \<noteq> {}"
   1.677 +      using *(3)[OF kp] by auto
   1.678 +  }
   1.679 +  fix k1 k2
   1.680 +  assume "k1 \<in> q" "k2 \<in> q" "k1 \<noteq> k2"
   1.681 +  then have **: "k1 \<in> p" "k2 \<in> p" "k1 \<noteq> k2"
   1.682 +    using assms(2) by auto
   1.683 +  show "interior k1 \<inter> interior k2 = {}"
   1.684 +    using *(5)[OF **] by auto
   1.685 +qed auto
   1.686 +
   1.687 +lemma division_of_union_self[intro]: "p division_of s \<Longrightarrow> p division_of (\<Union>p)"
   1.688 +  unfolding division_of_def by auto
   1.689 +
   1.690 +lemma division_of_content_0:
   1.691 +  assumes "content (cbox a b) = 0" "d division_of (cbox a b)"
   1.692 +  shows "\<forall>k\<in>d. content k = 0"
   1.693 +  unfolding forall_in_division[OF assms(2)]
   1.694 +  by (metis antisym_conv assms content_pos_le content_subset division_ofD(2))
   1.695 +
   1.696 +lemma division_inter:
   1.697 +  fixes s1 s2 :: "'a::euclidean_space set"
   1.698 +  assumes "p1 division_of s1"
   1.699 +    and "p2 division_of s2"
   1.700 +  shows "{k1 \<inter> k2 | k1 k2. k1 \<in> p1 \<and> k2 \<in> p2 \<and> k1 \<inter> k2 \<noteq> {}} division_of (s1 \<inter> s2)"
   1.701 +  (is "?A' division_of _")
   1.702 +proof -
   1.703 +  let ?A = "{s. s \<in>  (\<lambda>(k1,k2). k1 \<inter> k2) ` (p1 \<times> p2) \<and> s \<noteq> {}}"
   1.704 +  have *: "?A' = ?A" by auto
   1.705 +  show ?thesis
   1.706 +    unfolding *
   1.707 +  proof (rule division_ofI)
   1.708 +    have "?A \<subseteq> (\<lambda>(x, y). x \<inter> y) ` (p1 \<times> p2)"
   1.709 +      by auto
   1.710 +    moreover have "finite (p1 \<times> p2)"
   1.711 +      using assms unfolding division_of_def by auto
   1.712 +    ultimately show "finite ?A" by auto
   1.713 +    have *: "\<And>s. \<Union>{x\<in>s. x \<noteq> {}} = \<Union>s"
   1.714 +      by auto
   1.715 +    show "\<Union>?A = s1 \<inter> s2"
   1.716 +      apply (rule set_eqI)
   1.717 +      unfolding * and UN_iff
   1.718 +      using division_ofD(6)[OF assms(1)] and division_ofD(6)[OF assms(2)]
   1.719 +      apply auto
   1.720 +      done
   1.721 +    {
   1.722 +      fix k
   1.723 +      assume "k \<in> ?A"
   1.724 +      then obtain k1 k2 where k: "k = k1 \<inter> k2" "k1 \<in> p1" "k2 \<in> p2" "k \<noteq> {}"
   1.725 +        by auto
   1.726 +      then show "k \<noteq> {}"
   1.727 +        by auto
   1.728 +      show "k \<subseteq> s1 \<inter> s2"
   1.729 +        using division_ofD(2)[OF assms(1) k(2)] and division_ofD(2)[OF assms(2) k(3)]
   1.730 +        unfolding k by auto
   1.731 +      obtain a1 b1 where k1: "k1 = cbox a1 b1"
   1.732 +        using division_ofD(4)[OF assms(1) k(2)] by blast
   1.733 +      obtain a2 b2 where k2: "k2 = cbox a2 b2"
   1.734 +        using division_ofD(4)[OF assms(2) k(3)] by blast
   1.735 +      show "\<exists>a b. k = cbox a b"
   1.736 +        unfolding k k1 k2 unfolding inter_interval by auto
   1.737 +    }
   1.738 +    fix k1 k2
   1.739 +    assume "k1 \<in> ?A"
   1.740 +    then obtain x1 y1 where k1: "k1 = x1 \<inter> y1" "x1 \<in> p1" "y1 \<in> p2" "k1 \<noteq> {}"
   1.741 +      by auto
   1.742 +    assume "k2 \<in> ?A"
   1.743 +    then obtain x2 y2 where k2: "k2 = x2 \<inter> y2" "x2 \<in> p1" "y2 \<in> p2" "k2 \<noteq> {}"
   1.744 +      by auto
   1.745 +    assume "k1 \<noteq> k2"
   1.746 +    then have th: "x1 \<noteq> x2 \<or> y1 \<noteq> y2"
   1.747 +      unfolding k1 k2 by auto
   1.748 +    have *: "interior x1 \<inter> interior x2 = {} \<or> interior y1 \<inter> interior y2 = {} \<Longrightarrow>
   1.749 +      interior (x1 \<inter> y1) \<subseteq> interior x1 \<Longrightarrow> interior (x1 \<inter> y1) \<subseteq> interior y1 \<Longrightarrow>
   1.750 +      interior (x2 \<inter> y2) \<subseteq> interior x2 \<Longrightarrow> interior (x2 \<inter> y2) \<subseteq> interior y2 \<Longrightarrow>
   1.751 +      interior (x1 \<inter> y1) \<inter> interior (x2 \<inter> y2) = {}" by auto
   1.752 +    show "interior k1 \<inter> interior k2 = {}"
   1.753 +      unfolding k1 k2
   1.754 +      apply (rule *)
   1.755 +      using assms division_ofD(5) k1 k2(2) k2(3) th apply auto
   1.756 +      done
   1.757 +  qed
   1.758 +qed
   1.759 +
   1.760 +lemma division_inter_1:
   1.761 +  assumes "d division_of i"
   1.762 +    and "cbox a (b::'a::euclidean_space) \<subseteq> i"
   1.763 +  shows "{cbox a b \<inter> k | k. k \<in> d \<and> cbox a b \<inter> k \<noteq> {}} division_of (cbox a b)"
   1.764 +proof (cases "cbox a b = {}")
   1.765 +  case True
   1.766 +  show ?thesis
   1.767 +    unfolding True and division_of_trivial by auto
   1.768 +next
   1.769 +  case False
   1.770 +  have *: "cbox a b \<inter> i = cbox a b" using assms(2) by auto
   1.771 +  show ?thesis
   1.772 +    using division_inter[OF division_of_self[OF False] assms(1)]
   1.773 +    unfolding * by auto
   1.774 +qed
   1.775 +
   1.776 +lemma elementary_inter:
   1.777 +  fixes s t :: "'a::euclidean_space set"
   1.778 +  assumes "p1 division_of s"
   1.779 +    and "p2 division_of t"
   1.780 +  shows "\<exists>p. p division_of (s \<inter> t)"
   1.781 +using assms division_inter by blast
   1.782 +
   1.783 +lemma elementary_inters:
   1.784 +  assumes "finite f"
   1.785 +    and "f \<noteq> {}"
   1.786 +    and "\<forall>s\<in>f. \<exists>p. p division_of (s::('a::euclidean_space) set)"
   1.787 +  shows "\<exists>p. p division_of (\<Inter>f)"
   1.788 +  using assms
   1.789 +proof (induct f rule: finite_induct)
   1.790 +  case (insert x f)
   1.791 +  show ?case
   1.792 +  proof (cases "f = {}")
   1.793 +    case True
   1.794 +    then show ?thesis
   1.795 +      unfolding True using insert by auto
   1.796 +  next
   1.797 +    case False
   1.798 +    obtain p where "p division_of \<Inter>f"
   1.799 +      using insert(3)[OF False insert(5)[unfolded ball_simps,THEN conjunct2]] ..
   1.800 +    moreover obtain px where "px division_of x"
   1.801 +      using insert(5)[rule_format,OF insertI1] ..
   1.802 +    ultimately show ?thesis
   1.803 +      by (simp add: elementary_inter Inter_insert)
   1.804 +  qed
   1.805 +qed auto
   1.806 +
   1.807 +lemma division_disjoint_union:
   1.808 +  assumes "p1 division_of s1"
   1.809 +    and "p2 division_of s2"
   1.810 +    and "interior s1 \<inter> interior s2 = {}"
   1.811 +  shows "(p1 \<union> p2) division_of (s1 \<union> s2)"
   1.812 +proof (rule division_ofI)
   1.813 +  note d1 = division_ofD[OF assms(1)]
   1.814 +  note d2 = division_ofD[OF assms(2)]
   1.815 +  show "finite (p1 \<union> p2)"
   1.816 +    using d1(1) d2(1) by auto
   1.817 +  show "\<Union>(p1 \<union> p2) = s1 \<union> s2"
   1.818 +    using d1(6) d2(6) by auto
   1.819 +  {
   1.820 +    fix k1 k2
   1.821 +    assume as: "k1 \<in> p1 \<union> p2" "k2 \<in> p1 \<union> p2" "k1 \<noteq> k2"
   1.822 +    moreover
   1.823 +    let ?g="interior k1 \<inter> interior k2 = {}"
   1.824 +    {
   1.825 +      assume as: "k1\<in>p1" "k2\<in>p2"
   1.826 +      have ?g
   1.827 +        using interior_mono[OF d1(2)[OF as(1)]] interior_mono[OF d2(2)[OF as(2)]]
   1.828 +        using assms(3) by blast
   1.829 +    }
   1.830 +    moreover
   1.831 +    {
   1.832 +      assume as: "k1\<in>p2" "k2\<in>p1"
   1.833 +      have ?g
   1.834 +        using interior_mono[OF d1(2)[OF as(2)]] interior_mono[OF d2(2)[OF as(1)]]
   1.835 +        using assms(3) by blast
   1.836 +    }
   1.837 +    ultimately show ?g
   1.838 +      using d1(5)[OF _ _ as(3)] and d2(5)[OF _ _ as(3)] by auto
   1.839 +  }
   1.840 +  fix k
   1.841 +  assume k: "k \<in> p1 \<union> p2"
   1.842 +  show "k \<subseteq> s1 \<union> s2"
   1.843 +    using k d1(2) d2(2) by auto
   1.844 +  show "k \<noteq> {}"
   1.845 +    using k d1(3) d2(3) by auto
   1.846 +  show "\<exists>a b. k = cbox a b"
   1.847 +    using k d1(4) d2(4) by auto
   1.848 +qed
   1.849 +
   1.850 +lemma partial_division_extend_1:
   1.851 +  fixes a b c d :: "'a::euclidean_space"
   1.852 +  assumes incl: "cbox c d \<subseteq> cbox a b"
   1.853 +    and nonempty: "cbox c d \<noteq> {}"
   1.854 +  obtains p where "p division_of (cbox a b)" "cbox c d \<in> p"
   1.855 +proof
   1.856 +  let ?B = "\<lambda>f::'a\<Rightarrow>'a \<times> 'a.
   1.857 +    cbox (\<Sum>i\<in>Basis. (fst (f i) \<bullet> i) *\<^sub>R i) (\<Sum>i\<in>Basis. (snd (f i) \<bullet> i) *\<^sub>R i)"
   1.858 +  define p where "p = ?B ` (Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)})"
   1.859 +
   1.860 +  show "cbox c d \<in> p"
   1.861 +    unfolding p_def
   1.862 +    by (auto simp add: box_eq_empty cbox_def intro!: image_eqI[where x="\<lambda>(i::'a)\<in>Basis. (c, d)"])
   1.863 +  {
   1.864 +    fix i :: 'a
   1.865 +    assume "i \<in> Basis"
   1.866 +    with incl nonempty have "a \<bullet> i \<le> c \<bullet> i" "c \<bullet> i \<le> d \<bullet> i" "d \<bullet> i \<le> b \<bullet> i"
   1.867 +      unfolding box_eq_empty subset_box by (auto simp: not_le)
   1.868 +  }
   1.869 +  note ord = this
   1.870 +
   1.871 +  show "p division_of (cbox a b)"
   1.872 +  proof (rule division_ofI)
   1.873 +    show "finite p"
   1.874 +      unfolding p_def by (auto intro!: finite_PiE)
   1.875 +    {
   1.876 +      fix k
   1.877 +      assume "k \<in> p"
   1.878 +      then obtain f where f: "f \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and k: "k = ?B f"
   1.879 +        by (auto simp: p_def)
   1.880 +      then show "\<exists>a b. k = cbox a b"
   1.881 +        by auto
   1.882 +      have "k \<subseteq> cbox a b \<and> k \<noteq> {}"
   1.883 +      proof (simp add: k box_eq_empty subset_box not_less, safe)
   1.884 +        fix i :: 'a
   1.885 +        assume i: "i \<in> Basis"
   1.886 +        with f have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
   1.887 +          by (auto simp: PiE_iff)
   1.888 +        with i ord[of i]
   1.889 +        show "a \<bullet> i \<le> fst (f i) \<bullet> i" "snd (f i) \<bullet> i \<le> b \<bullet> i" "fst (f i) \<bullet> i \<le> snd (f i) \<bullet> i"
   1.890 +          by auto
   1.891 +      qed
   1.892 +      then show "k \<noteq> {}" "k \<subseteq> cbox a b"
   1.893 +        by auto
   1.894 +      {
   1.895 +        fix l
   1.896 +        assume "l \<in> p"
   1.897 +        then obtain g where g: "g \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and l: "l = ?B g"
   1.898 +          by (auto simp: p_def)
   1.899 +        assume "l \<noteq> k"
   1.900 +        have "\<exists>i\<in>Basis. f i \<noteq> g i"
   1.901 +        proof (rule ccontr)
   1.902 +          assume "\<not> ?thesis"
   1.903 +          with f g have "f = g"
   1.904 +            by (auto simp: PiE_iff extensional_def intro!: ext)
   1.905 +          with \<open>l \<noteq> k\<close> show False
   1.906 +            by (simp add: l k)
   1.907 +        qed
   1.908 +        then obtain i where *: "i \<in> Basis" "f i \<noteq> g i" ..
   1.909 +        then have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
   1.910 +                  "g i = (a, c) \<or> g i = (c, d) \<or> g i = (d, b)"
   1.911 +          using f g by (auto simp: PiE_iff)
   1.912 +        with * ord[of i] show "interior l \<inter> interior k = {}"
   1.913 +          by (auto simp add: l k interior_cbox disjoint_interval intro!: bexI[of _ i])
   1.914 +      }
   1.915 +      note \<open>k \<subseteq> cbox a b\<close>
   1.916 +    }
   1.917 +    moreover
   1.918 +    {
   1.919 +      fix x assume x: "x \<in> cbox a b"
   1.920 +      have "\<forall>i\<in>Basis. \<exists>l. x \<bullet> i \<in> {fst l \<bullet> i .. snd l \<bullet> i} \<and> l \<in> {(a, c), (c, d), (d, b)}"
   1.921 +      proof
   1.922 +        fix i :: 'a
   1.923 +        assume "i \<in> Basis"
   1.924 +        with x ord[of i]
   1.925 +        have "(a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> c \<bullet> i) \<or> (c \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> d \<bullet> i) \<or>
   1.926 +            (d \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i)"
   1.927 +          by (auto simp: cbox_def)
   1.928 +        then show "\<exists>l. x \<bullet> i \<in> {fst l \<bullet> i .. snd l \<bullet> i} \<and> l \<in> {(a, c), (c, d), (d, b)}"
   1.929 +          by auto
   1.930 +      qed
   1.931 +      then obtain f where
   1.932 +        f: "\<forall>i\<in>Basis. x \<bullet> i \<in> {fst (f i) \<bullet> i..snd (f i) \<bullet> i} \<and> f i \<in> {(a, c), (c, d), (d, b)}"
   1.933 +        unfolding bchoice_iff ..
   1.934 +      moreover from f have "restrict f Basis \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}"
   1.935 +        by auto
   1.936 +      moreover from f have "x \<in> ?B (restrict f Basis)"
   1.937 +        by (auto simp: mem_box)
   1.938 +      ultimately have "\<exists>k\<in>p. x \<in> k"
   1.939 +        unfolding p_def by blast
   1.940 +    }
   1.941 +    ultimately show "\<Union>p = cbox a b"
   1.942 +      by auto
   1.943 +  qed
   1.944 +qed
   1.945 +
   1.946 +lemma partial_division_extend_interval:
   1.947 +  assumes "p division_of (\<Union>p)" "(\<Union>p) \<subseteq> cbox a b"
   1.948 +  obtains q where "p \<subseteq> q" "q division_of cbox a (b::'a::euclidean_space)"
   1.949 +proof (cases "p = {}")
   1.950 +  case True
   1.951 +  obtain q where "q division_of (cbox a b)"
   1.952 +    by (rule elementary_interval)
   1.953 +  then show ?thesis
   1.954 +    using True that by blast
   1.955 +next
   1.956 +  case False
   1.957 +  note p = division_ofD[OF assms(1)]
   1.958 +  have div_cbox: "\<forall>k\<in>p. \<exists>q. q division_of cbox a b \<and> k \<in> q"
   1.959 +  proof
   1.960 +    fix k
   1.961 +    assume kp: "k \<in> p"
   1.962 +    obtain c d where k: "k = cbox c d"
   1.963 +      using p(4)[OF kp] by blast
   1.964 +    have *: "cbox c d \<subseteq> cbox a b" "cbox c d \<noteq> {}"
   1.965 +      using p(2,3)[OF kp, unfolded k] using assms(2)
   1.966 +      by (blast intro: order.trans)+
   1.967 +    obtain q where "q division_of cbox a b" "cbox c d \<in> q"
   1.968 +      by (rule partial_division_extend_1[OF *])
   1.969 +    then show "\<exists>q. q division_of cbox a b \<and> k \<in> q"
   1.970 +      unfolding k by auto
   1.971 +  qed
   1.972 +  obtain q where q: "\<And>x. x \<in> p \<Longrightarrow> q x division_of cbox a b" "\<And>x. x \<in> p \<Longrightarrow> x \<in> q x"
   1.973 +    using bchoice[OF div_cbox] by blast
   1.974 +  { fix x
   1.975 +    assume x: "x \<in> p"
   1.976 +    have "q x division_of \<Union>q x"
   1.977 +      apply (rule division_ofI)
   1.978 +      using division_ofD[OF q(1)[OF x]]
   1.979 +      apply auto
   1.980 +      done }
   1.981 +  then have "\<And>x. x \<in> p \<Longrightarrow> \<exists>d. d division_of \<Union>(q x - {x})"
   1.982 +    by (meson Diff_subset division_of_subset)
   1.983 +  then have "\<exists>d. d division_of \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p)"
   1.984 +    apply -
   1.985 +    apply (rule elementary_inters [OF finite_imageI[OF p(1)]])
   1.986 +    apply (auto simp: False elementary_inters [OF finite_imageI[OF p(1)]])
   1.987 +    done
   1.988 +  then obtain d where d: "d division_of \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p)" ..
   1.989 +  have "d \<union> p division_of cbox a b"
   1.990 +  proof -
   1.991 +    have te: "\<And>s f t. s \<noteq> {} \<Longrightarrow> \<forall>i\<in>s. f i \<union> i = t \<Longrightarrow> t = \<Inter>(f ` s) \<union> \<Union>s" by auto
   1.992 +    have cbox_eq: "cbox a b = \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p) \<union> \<Union>p"
   1.993 +    proof (rule te[OF False], clarify)
   1.994 +      fix i
   1.995 +      assume i: "i \<in> p"
   1.996 +      show "\<Union>(q i - {i}) \<union> i = cbox a b"
   1.997 +        using division_ofD(6)[OF q(1)[OF i]] using q(2)[OF i] by auto
   1.998 +    qed
   1.999 +    { fix k
  1.1000 +      assume k: "k \<in> p"
  1.1001 +      have *: "\<And>u t s. t \<inter> s = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<inter> t = {}"
  1.1002 +        by auto
  1.1003 +      have "interior (\<Inter>i\<in>p. \<Union>(q i - {i})) \<inter> interior k = {}"
  1.1004 +      proof (rule *[OF inter_interior_unions_intervals])
  1.1005 +        note qk=division_ofD[OF q(1)[OF k]]
  1.1006 +        show "finite (q k - {k})" "open (interior k)" "\<forall>t\<in>q k - {k}. \<exists>a b. t = cbox a b"
  1.1007 +          using qk by auto
  1.1008 +        show "\<forall>t\<in>q k - {k}. interior k \<inter> interior t = {}"
  1.1009 +          using qk(5) using q(2)[OF k] by auto
  1.1010 +        show "interior (\<Inter>i\<in>p. \<Union>(q i - {i})) \<subseteq> interior (\<Union>(q k - {k}))"
  1.1011 +          apply (rule interior_mono)+
  1.1012 +          using k
  1.1013 +          apply auto
  1.1014 +          done
  1.1015 +      qed } note [simp] = this
  1.1016 +    show "d \<union> p division_of (cbox a b)"
  1.1017 +      unfolding cbox_eq
  1.1018 +      apply (rule division_disjoint_union[OF d assms(1)])
  1.1019 +      apply (rule inter_interior_unions_intervals)
  1.1020 +      apply (rule p open_interior ballI)+
  1.1021 +      apply simp_all
  1.1022 +      done
  1.1023 +  qed
  1.1024 +  then show ?thesis
  1.1025 +    by (meson Un_upper2 that)
  1.1026 +qed
  1.1027 +
  1.1028 +lemma elementary_bounded[dest]:
  1.1029 +  fixes s :: "'a::euclidean_space set"
  1.1030 +  shows "p division_of s \<Longrightarrow> bounded s"
  1.1031 +  unfolding division_of_def by (metis bounded_Union bounded_cbox)
  1.1032 +
  1.1033 +lemma elementary_subset_cbox:
  1.1034 +  "p division_of s \<Longrightarrow> \<exists>a b. s \<subseteq> cbox a (b::'a::euclidean_space)"
  1.1035 +  by (meson elementary_bounded bounded_subset_cbox)
  1.1036 +
  1.1037 +lemma division_union_intervals_exists:
  1.1038 +  fixes a b :: "'a::euclidean_space"
  1.1039 +  assumes "cbox a b \<noteq> {}"
  1.1040 +  obtains p where "(insert (cbox a b) p) division_of (cbox a b \<union> cbox c d)"
  1.1041 +proof (cases "cbox c d = {}")
  1.1042 +  case True
  1.1043 +  show ?thesis
  1.1044 +    apply (rule that[of "{}"])
  1.1045 +    unfolding True
  1.1046 +    using assms
  1.1047 +    apply auto
  1.1048 +    done
  1.1049 +next
  1.1050 +  case False
  1.1051 +  show ?thesis
  1.1052 +  proof (cases "cbox a b \<inter> cbox c d = {}")
  1.1053 +    case True
  1.1054 +    then show ?thesis
  1.1055 +      by (metis that False assms division_disjoint_union division_of_self insert_is_Un interior_Int interior_empty)
  1.1056 +  next
  1.1057 +    case False
  1.1058 +    obtain u v where uv: "cbox a b \<inter> cbox c d = cbox u v"
  1.1059 +      unfolding inter_interval by auto
  1.1060 +    have uv_sub: "cbox u v \<subseteq> cbox c d" using uv by auto
  1.1061 +    obtain p where "p division_of cbox c d" "cbox u v \<in> p"
  1.1062 +      by (rule partial_division_extend_1[OF uv_sub False[unfolded uv]])
  1.1063 +    note p = this division_ofD[OF this(1)]
  1.1064 +    have "interior (cbox a b \<inter> \<Union>(p - {cbox u v})) = interior(cbox u v \<inter> \<Union>(p - {cbox u v}))"
  1.1065 +      apply (rule arg_cong[of _ _ interior])
  1.1066 +      using p(8) uv by auto
  1.1067 +    also have "\<dots> = {}"
  1.1068 +      unfolding interior_Int
  1.1069 +      apply (rule inter_interior_unions_intervals)
  1.1070 +      using p(6) p(7)[OF p(2)] p(3)
  1.1071 +      apply auto
  1.1072 +      done
  1.1073 +    finally have [simp]: "interior (cbox a b) \<inter> interior (\<Union>(p - {cbox u v})) = {}" by simp
  1.1074 +    have cbe: "cbox a b \<union> cbox c d = cbox a b \<union> \<Union>(p - {cbox u v})"
  1.1075 +      using p(8) unfolding uv[symmetric] by auto
  1.1076 +    have "insert (cbox a b) (p - {cbox u v}) division_of cbox a b \<union> \<Union>(p - {cbox u v})"
  1.1077 +    proof -
  1.1078 +      have "{cbox a b} division_of cbox a b"
  1.1079 +        by (simp add: assms division_of_self)
  1.1080 +      then show "insert (cbox a b) (p - {cbox u v}) division_of cbox a b \<union> \<Union>(p - {cbox u v})"
  1.1081 +        by (metis (no_types) Diff_subset \<open>interior (cbox a b) \<inter> interior (\<Union>(p - {cbox u v})) = {}\<close> division_disjoint_union division_of_subset insert_is_Un p(1) p(8))
  1.1082 +    qed
  1.1083 +    with that[of "p - {cbox u v}"] show ?thesis by (simp add: cbe)
  1.1084 +  qed
  1.1085 +qed
  1.1086 +
  1.1087 +lemma division_of_unions:
  1.1088 +  assumes "finite f"
  1.1089 +    and "\<And>p. p \<in> f \<Longrightarrow> p division_of (\<Union>p)"
  1.1090 +    and "\<And>k1 k2. k1 \<in> \<Union>f \<Longrightarrow> k2 \<in> \<Union>f \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
  1.1091 +  shows "\<Union>f division_of \<Union>\<Union>f"
  1.1092 +  using assms
  1.1093 +  by (auto intro!: division_ofI)
  1.1094 +
  1.1095 +lemma elementary_union_interval:
  1.1096 +  fixes a b :: "'a::euclidean_space"
  1.1097 +  assumes "p division_of \<Union>p"
  1.1098 +  obtains q where "q division_of (cbox a b \<union> \<Union>p)"
  1.1099 +proof -
  1.1100 +  note assm = division_ofD[OF assms]
  1.1101 +  have lem1: "\<And>f s. \<Union>\<Union>(f ` s) = \<Union>((\<lambda>x. \<Union>(f x)) ` s)"
  1.1102 +    by auto
  1.1103 +  have lem2: "\<And>f s. f \<noteq> {} \<Longrightarrow> \<Union>{s \<union> t |t. t \<in> f} = s \<union> \<Union>f"
  1.1104 +    by auto
  1.1105 +  {
  1.1106 +    presume "p = {} \<Longrightarrow> thesis"
  1.1107 +      "cbox a b = {} \<Longrightarrow> thesis"
  1.1108 +      "cbox a b \<noteq> {} \<Longrightarrow> interior (cbox a b) = {} \<Longrightarrow> thesis"
  1.1109 +      "p \<noteq> {} \<Longrightarrow> interior (cbox a b)\<noteq>{} \<Longrightarrow> cbox a b \<noteq> {} \<Longrightarrow> thesis"
  1.1110 +    then show thesis by auto
  1.1111 +  next
  1.1112 +    assume as: "p = {}"
  1.1113 +    obtain p where "p division_of (cbox a b)"
  1.1114 +      by (rule elementary_interval)
  1.1115 +    then show thesis
  1.1116 +      using as that by auto
  1.1117 +  next
  1.1118 +    assume as: "cbox a b = {}"
  1.1119 +    show thesis
  1.1120 +      using as assms that by auto
  1.1121 +  next
  1.1122 +    assume as: "interior (cbox a b) = {}" "cbox a b \<noteq> {}"
  1.1123 +    show thesis
  1.1124 +      apply (rule that[of "insert (cbox a b) p"],rule division_ofI)
  1.1125 +      unfolding finite_insert
  1.1126 +      apply (rule assm(1)) unfolding Union_insert
  1.1127 +      using assm(2-4) as
  1.1128 +      apply -
  1.1129 +      apply (fast dest: assm(5))+
  1.1130 +      done
  1.1131 +  next
  1.1132 +    assume as: "p \<noteq> {}" "interior (cbox a b) \<noteq> {}" "cbox a b \<noteq> {}"
  1.1133 +    have "\<forall>k\<in>p. \<exists>q. (insert (cbox a b) q) division_of (cbox a b \<union> k)"
  1.1134 +    proof
  1.1135 +      fix k
  1.1136 +      assume kp: "k \<in> p"
  1.1137 +      from assm(4)[OF kp] obtain c d where "k = cbox c d" by blast
  1.1138 +      then show "\<exists>q. (insert (cbox a b) q) division_of (cbox a b \<union> k)"
  1.1139 +        by (meson as(3) division_union_intervals_exists)
  1.1140 +    qed
  1.1141 +    from bchoice[OF this] obtain q where "\<forall>x\<in>p. insert (cbox a b) (q x) division_of (cbox a b) \<union> x" ..
  1.1142 +    note q = division_ofD[OF this[rule_format]]
  1.1143 +    let ?D = "\<Union>{insert (cbox a b) (q k) | k. k \<in> p}"
  1.1144 +    show thesis
  1.1145 +    proof (rule that[OF division_ofI])
  1.1146 +      have *: "{insert (cbox a b) (q k) |k. k \<in> p} = (\<lambda>k. insert (cbox a b) (q k)) ` p"
  1.1147 +        by auto
  1.1148 +      show "finite ?D"
  1.1149 +        using "*" assm(1) q(1) by auto
  1.1150 +      show "\<Union>?D = cbox a b \<union> \<Union>p"
  1.1151 +        unfolding * lem1
  1.1152 +        unfolding lem2[OF as(1), of "cbox a b", symmetric]
  1.1153 +        using q(6)
  1.1154 +        by auto
  1.1155 +      fix k
  1.1156 +      assume k: "k \<in> ?D"
  1.1157 +      then show "k \<subseteq> cbox a b \<union> \<Union>p"
  1.1158 +        using q(2) by auto
  1.1159 +      show "k \<noteq> {}"
  1.1160 +        using q(3) k by auto
  1.1161 +      show "\<exists>a b. k = cbox a b"
  1.1162 +        using q(4) k by auto
  1.1163 +      fix k'
  1.1164 +      assume k': "k' \<in> ?D" "k \<noteq> k'"
  1.1165 +      obtain x where x: "k \<in> insert (cbox a b) (q x)" "x\<in>p"
  1.1166 +        using k by auto
  1.1167 +      obtain x' where x': "k'\<in>insert (cbox a b) (q x')" "x'\<in>p"
  1.1168 +        using k' by auto
  1.1169 +      show "interior k \<inter> interior k' = {}"
  1.1170 +      proof (cases "x = x'")
  1.1171 +        case True
  1.1172 +        show ?thesis
  1.1173 +          using True k' q(5) x' x by auto
  1.1174 +      next
  1.1175 +        case False
  1.1176 +        {
  1.1177 +          presume "k = cbox a b \<Longrightarrow> ?thesis"
  1.1178 +            and "k' = cbox a b \<Longrightarrow> ?thesis"
  1.1179 +            and "k \<noteq> cbox a b \<Longrightarrow> k' \<noteq> cbox a b \<Longrightarrow> ?thesis"
  1.1180 +          then show ?thesis by linarith
  1.1181 +        next
  1.1182 +          assume as': "k  = cbox a b"
  1.1183 +          show ?thesis
  1.1184 +            using as' k' q(5) x' by blast
  1.1185 +        next
  1.1186 +          assume as': "k' = cbox a b"
  1.1187 +          show ?thesis
  1.1188 +            using as' k'(2) q(5) x by blast
  1.1189 +        }
  1.1190 +        assume as': "k \<noteq> cbox a b" "k' \<noteq> cbox a b"
  1.1191 +        obtain c d where k: "k = cbox c d"
  1.1192 +          using q(4)[OF x(2,1)] by blast
  1.1193 +        have "interior k \<inter> interior (cbox a b) = {}"
  1.1194 +          using as' k'(2) q(5) x by blast
  1.1195 +        then have "interior k \<subseteq> interior x"
  1.1196 +        using interior_subset_union_intervals
  1.1197 +          by (metis as(2) k q(2) x interior_subset_union_intervals)
  1.1198 +        moreover
  1.1199 +        obtain c d where c_d: "k' = cbox c d"
  1.1200 +          using q(4)[OF x'(2,1)] by blast
  1.1201 +        have "interior k' \<inter> interior (cbox a b) = {}"
  1.1202 +          using as'(2) q(5) x' by blast
  1.1203 +        then have "interior k' \<subseteq> interior x'"
  1.1204 +          by (metis as(2) c_d interior_subset_union_intervals q(2) x'(1) x'(2))
  1.1205 +        ultimately show ?thesis
  1.1206 +          using assm(5)[OF x(2) x'(2) False] by auto
  1.1207 +      qed
  1.1208 +    qed
  1.1209 +  }
  1.1210 +qed
  1.1211 +
  1.1212 +lemma elementary_unions_intervals:
  1.1213 +  assumes fin: "finite f"
  1.1214 +    and "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = cbox a (b::'a::euclidean_space)"
  1.1215 +  obtains p where "p division_of (\<Union>f)"
  1.1216 +proof -
  1.1217 +  have "\<exists>p. p division_of (\<Union>f)"
  1.1218 +  proof (induct_tac f rule:finite_subset_induct)
  1.1219 +    show "\<exists>p. p division_of \<Union>{}" using elementary_empty by auto
  1.1220 +  next
  1.1221 +    fix x F
  1.1222 +    assume as: "finite F" "x \<notin> F" "\<exists>p. p division_of \<Union>F" "x\<in>f"
  1.1223 +    from this(3) obtain p where p: "p division_of \<Union>F" ..
  1.1224 +    from assms(2)[OF as(4)] obtain a b where x: "x = cbox a b" by blast
  1.1225 +    have *: "\<Union>F = \<Union>p"
  1.1226 +      using division_ofD[OF p] by auto
  1.1227 +    show "\<exists>p. p division_of \<Union>insert x F"
  1.1228 +      using elementary_union_interval[OF p[unfolded *], of a b]
  1.1229 +      unfolding Union_insert x * by metis
  1.1230 +  qed (insert assms, auto)
  1.1231 +  then show ?thesis
  1.1232 +    using that by auto
  1.1233 +qed
  1.1234 +
  1.1235 +lemma elementary_union:
  1.1236 +  fixes s t :: "'a::euclidean_space set"
  1.1237 +  assumes "ps division_of s" "pt division_of t"
  1.1238 +  obtains p where "p division_of (s \<union> t)"
  1.1239 +proof -
  1.1240 +  have *: "s \<union> t = \<Union>ps \<union> \<Union>pt"
  1.1241 +    using assms unfolding division_of_def by auto
  1.1242 +  show ?thesis
  1.1243 +    apply (rule elementary_unions_intervals[of "ps \<union> pt"])
  1.1244 +    using assms apply auto
  1.1245 +    by (simp add: * that)
  1.1246 +qed
  1.1247 +
  1.1248 +lemma partial_division_extend:
  1.1249 +  fixes t :: "'a::euclidean_space set"
  1.1250 +  assumes "p division_of s"
  1.1251 +    and "q division_of t"
  1.1252 +    and "s \<subseteq> t"
  1.1253 +  obtains r where "p \<subseteq> r" and "r division_of t"
  1.1254 +proof -
  1.1255 +  note divp = division_ofD[OF assms(1)] and divq = division_ofD[OF assms(2)]
  1.1256 +  obtain a b where ab: "t \<subseteq> cbox a b"
  1.1257 +    using elementary_subset_cbox[OF assms(2)] by auto
  1.1258 +  obtain r1 where "p \<subseteq> r1" "r1 division_of (cbox a b)"
  1.1259 +    using assms
  1.1260 +    by (metis ab dual_order.trans partial_division_extend_interval divp(6))
  1.1261 +  note r1 = this division_ofD[OF this(2)]
  1.1262 +  obtain p' where "p' division_of \<Union>(r1 - p)"
  1.1263 +    apply (rule elementary_unions_intervals[of "r1 - p"])
  1.1264 +    using r1(3,6)
  1.1265 +    apply auto
  1.1266 +    done
  1.1267 +  then obtain r2 where r2: "r2 division_of (\<Union>(r1 - p)) \<inter> (\<Union>q)"
  1.1268 +    by (metis assms(2) divq(6) elementary_inter)
  1.1269 +  {
  1.1270 +    fix x
  1.1271 +    assume x: "x \<in> t" "x \<notin> s"
  1.1272 +    then have "x\<in>\<Union>r1"
  1.1273 +      unfolding r1 using ab by auto
  1.1274 +    then obtain r where r: "r \<in> r1" "x \<in> r"
  1.1275 +      unfolding Union_iff ..
  1.1276 +    moreover
  1.1277 +    have "r \<notin> p"
  1.1278 +    proof
  1.1279 +      assume "r \<in> p"
  1.1280 +      then have "x \<in> s" using divp(2) r by auto
  1.1281 +      then show False using x by auto
  1.1282 +    qed
  1.1283 +    ultimately have "x\<in>\<Union>(r1 - p)" by auto
  1.1284 +  }
  1.1285 +  then have *: "t = \<Union>p \<union> (\<Union>(r1 - p) \<inter> \<Union>q)"
  1.1286 +    unfolding divp divq using assms(3) by auto
  1.1287 +  show ?thesis
  1.1288 +    apply (rule that[of "p \<union> r2"])
  1.1289 +    unfolding *
  1.1290 +    defer
  1.1291 +    apply (rule division_disjoint_union)
  1.1292 +    unfolding divp(6)
  1.1293 +    apply(rule assms r2)+
  1.1294 +  proof -
  1.1295 +    have "interior s \<inter> interior (\<Union>(r1-p)) = {}"
  1.1296 +    proof (rule inter_interior_unions_intervals)
  1.1297 +      show "finite (r1 - p)" and "open (interior s)" and "\<forall>t\<in>r1-p. \<exists>a b. t = cbox a b"
  1.1298 +        using r1 by auto
  1.1299 +      have *: "\<And>s. (\<And>x. x \<in> s \<Longrightarrow> False) \<Longrightarrow> s = {}"
  1.1300 +        by auto
  1.1301 +      show "\<forall>t\<in>r1-p. interior s \<inter> interior t = {}"
  1.1302 +      proof
  1.1303 +        fix m x
  1.1304 +        assume as: "m \<in> r1 - p"
  1.1305 +        have "interior m \<inter> interior (\<Union>p) = {}"
  1.1306 +        proof (rule inter_interior_unions_intervals)
  1.1307 +          show "finite p" and "open (interior m)" and "\<forall>t\<in>p. \<exists>a b. t = cbox a b"
  1.1308 +            using divp by auto
  1.1309 +          show "\<forall>t\<in>p. interior m \<inter> interior t = {}"
  1.1310 +            by (metis DiffD1 DiffD2 as r1(1) r1(7) set_rev_mp)
  1.1311 +        qed
  1.1312 +        then show "interior s \<inter> interior m = {}"
  1.1313 +          unfolding divp by auto
  1.1314 +      qed
  1.1315 +    qed
  1.1316 +    then show "interior s \<inter> interior (\<Union>(r1-p) \<inter> (\<Union>q)) = {}"
  1.1317 +      using interior_subset by auto
  1.1318 +  qed auto
  1.1319 +qed
  1.1320 +
  1.1321 +lemma division_split_left_inj:
  1.1322 +  fixes type :: "'a::euclidean_space"
  1.1323 +  assumes "d division_of i"
  1.1324 +    and "k1 \<in> d"
  1.1325 +    and "k2 \<in> d"
  1.1326 +    and "k1 \<noteq> k2"
  1.1327 +    and "k1 \<inter> {x::'a. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c}"
  1.1328 +    and k: "k\<in>Basis"
  1.1329 +  shows "content(k1 \<inter> {x. x\<bullet>k \<le> c}) = 0"
  1.1330 +proof -
  1.1331 +  note d=division_ofD[OF assms(1)]
  1.1332 +  have *: "\<And>(a::'a) b c. content (cbox a b \<inter> {x. x\<bullet>k \<le> c}) = 0 \<longleftrightarrow>
  1.1333 +    interior(cbox a b \<inter> {x. x\<bullet>k \<le> c}) = {}"
  1.1334 +    unfolding  interval_split[OF k] content_eq_0_interior by auto
  1.1335 +  guess u1 v1 using d(4)[OF assms(2)] by (elim exE) note uv1=this
  1.1336 +  guess u2 v2 using d(4)[OF assms(3)] by (elim exE) note uv2=this
  1.1337 +  have **: "\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}"
  1.1338 +    by auto
  1.1339 +  show ?thesis
  1.1340 +    unfolding uv1 uv2 *
  1.1341 +    apply (rule **[OF d(5)[OF assms(2-4)]])
  1.1342 +    apply (simp add: uv1)
  1.1343 +    using assms(5) uv1 by auto
  1.1344 +qed
  1.1345 +
  1.1346 +lemma division_split_right_inj:
  1.1347 +  fixes type :: "'a::euclidean_space"
  1.1348 +  assumes "d division_of i"
  1.1349 +    and "k1 \<in> d"
  1.1350 +    and "k2 \<in> d"
  1.1351 +    and "k1 \<noteq> k2"
  1.1352 +    and "k1 \<inter> {x::'a. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c}"
  1.1353 +    and k: "k \<in> Basis"
  1.1354 +  shows "content (k1 \<inter> {x. x\<bullet>k \<ge> c}) = 0"
  1.1355 +proof -
  1.1356 +  note d=division_ofD[OF assms(1)]
  1.1357 +  have *: "\<And>a b::'a. \<And>c. content(cbox a b \<inter> {x. x\<bullet>k \<ge> c}) = 0 \<longleftrightarrow>
  1.1358 +    interior(cbox a b \<inter> {x. x\<bullet>k \<ge> c}) = {}"
  1.1359 +    unfolding interval_split[OF k] content_eq_0_interior by auto
  1.1360 +  guess u1 v1 using d(4)[OF assms(2)] by (elim exE) note uv1=this
  1.1361 +  guess u2 v2 using d(4)[OF assms(3)] by (elim exE) note uv2=this
  1.1362 +  have **: "\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}"
  1.1363 +    by auto
  1.1364 +  show ?thesis
  1.1365 +    unfolding uv1 uv2 *
  1.1366 +    apply (rule **[OF d(5)[OF assms(2-4)]])
  1.1367 +    apply (simp add: uv1)
  1.1368 +    using assms(5) uv1 by auto
  1.1369 +qed
  1.1370 +
  1.1371 +
  1.1372 +lemma division_split:
  1.1373 +  fixes a :: "'a::euclidean_space"
  1.1374 +  assumes "p division_of (cbox a b)"
  1.1375 +    and k: "k\<in>Basis"
  1.1376 +  shows "{l \<inter> {x. x\<bullet>k \<le> c} | l. l \<in> p \<and> l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}} division_of(cbox a b \<inter> {x. x\<bullet>k \<le> c})"
  1.1377 +      (is "?p1 division_of ?I1")
  1.1378 +    and "{l \<inter> {x. x\<bullet>k \<ge> c} | l. l \<in> p \<and> l \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}} division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  1.1379 +      (is "?p2 division_of ?I2")
  1.1380 +proof (rule_tac[!] division_ofI)
  1.1381 +  note p = division_ofD[OF assms(1)]
  1.1382 +  show "finite ?p1" "finite ?p2"
  1.1383 +    using p(1) by auto
  1.1384 +  show "\<Union>?p1 = ?I1" "\<Union>?p2 = ?I2"
  1.1385 +    unfolding p(6)[symmetric] by auto
  1.1386 +  {
  1.1387 +    fix k
  1.1388 +    assume "k \<in> ?p1"
  1.1389 +    then guess l unfolding mem_Collect_eq by (elim exE conjE) note l=this
  1.1390 +    guess u v using p(4)[OF l(2)] by (elim exE) note uv=this
  1.1391 +    show "k \<subseteq> ?I1"
  1.1392 +      using l p(2) uv by force
  1.1393 +    show  "k \<noteq> {}"
  1.1394 +      by (simp add: l)
  1.1395 +    show  "\<exists>a b. k = cbox a b"
  1.1396 +      apply (simp add: l uv p(2-3)[OF l(2)])
  1.1397 +      apply (subst interval_split[OF k])
  1.1398 +      apply (auto intro: order.trans)
  1.1399 +      done
  1.1400 +    fix k'
  1.1401 +    assume "k' \<in> ?p1"
  1.1402 +    then guess l' unfolding mem_Collect_eq by (elim exE conjE) note l'=this
  1.1403 +    assume "k \<noteq> k'"
  1.1404 +    then show "interior k \<inter> interior k' = {}"
  1.1405 +      unfolding l l' using p(5)[OF l(2) l'(2)] by auto
  1.1406 +  }
  1.1407 +  {
  1.1408 +    fix k
  1.1409 +    assume "k \<in> ?p2"
  1.1410 +    then guess l unfolding mem_Collect_eq by (elim exE conjE) note l=this
  1.1411 +    guess u v using p(4)[OF l(2)] by (elim exE) note uv=this
  1.1412 +    show "k \<subseteq> ?I2"
  1.1413 +      using l p(2) uv by force
  1.1414 +    show  "k \<noteq> {}"
  1.1415 +      by (simp add: l)
  1.1416 +    show  "\<exists>a b. k = cbox a b"
  1.1417 +      apply (simp add: l uv p(2-3)[OF l(2)])
  1.1418 +      apply (subst interval_split[OF k])
  1.1419 +      apply (auto intro: order.trans)
  1.1420 +      done
  1.1421 +    fix k'
  1.1422 +    assume "k' \<in> ?p2"
  1.1423 +    then guess l' unfolding mem_Collect_eq by (elim exE conjE) note l'=this
  1.1424 +    assume "k \<noteq> k'"
  1.1425 +    then show "interior k \<inter> interior k' = {}"
  1.1426 +      unfolding l l' using p(5)[OF l(2) l'(2)] by auto
  1.1427 +  }
  1.1428 +qed
  1.1429 +
  1.1430 +subsection \<open>Tagged (partial) divisions.\<close>
  1.1431 +
  1.1432 +definition tagged_partial_division_of (infixr "tagged'_partial'_division'_of" 40)
  1.1433 +  where "s tagged_partial_division_of i \<longleftrightarrow>
  1.1434 +    finite s \<and>
  1.1435 +    (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
  1.1436 +    (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
  1.1437 +      interior k1 \<inter> interior k2 = {})"
  1.1438 +
  1.1439 +lemma tagged_partial_division_ofD[dest]:
  1.1440 +  assumes "s tagged_partial_division_of i"
  1.1441 +  shows "finite s"
  1.1442 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  1.1443 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  1.1444 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  1.1445 +    and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow>
  1.1446 +      (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
  1.1447 +  using assms unfolding tagged_partial_division_of_def by blast+
  1.1448 +
  1.1449 +definition tagged_division_of (infixr "tagged'_division'_of" 40)
  1.1450 +  where "s tagged_division_of i \<longleftrightarrow> s tagged_partial_division_of i \<and> (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1.1451 +
  1.1452 +lemma tagged_division_of_finite: "s tagged_division_of i \<Longrightarrow> finite s"
  1.1453 +  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  1.1454 +
  1.1455 +lemma tagged_division_of:
  1.1456 +  "s tagged_division_of i \<longleftrightarrow>
  1.1457 +    finite s \<and>
  1.1458 +    (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
  1.1459 +    (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
  1.1460 +      interior k1 \<inter> interior k2 = {}) \<and>
  1.1461 +    (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1.1462 +  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  1.1463 +
  1.1464 +lemma tagged_division_ofI:
  1.1465 +  assumes "finite s"
  1.1466 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  1.1467 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  1.1468 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  1.1469 +    and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
  1.1470 +      interior k1 \<inter> interior k2 = {}"
  1.1471 +    and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1.1472 +  shows "s tagged_division_of i"
  1.1473 +  unfolding tagged_division_of
  1.1474 +  using assms
  1.1475 +  apply auto
  1.1476 +  apply fastforce+
  1.1477 +  done
  1.1478 +
  1.1479 +lemma tagged_division_ofD[dest]:  (*FIXME USE A LOCALE*)
  1.1480 +  assumes "s tagged_division_of i"
  1.1481 +  shows "finite s"
  1.1482 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  1.1483 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  1.1484 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  1.1485 +    and "\<And>x1 k1 x2 k2. (x1, k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
  1.1486 +      interior k1 \<inter> interior k2 = {}"
  1.1487 +    and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1.1488 +  using assms unfolding tagged_division_of by blast+
  1.1489 +
  1.1490 +lemma division_of_tagged_division:
  1.1491 +  assumes "s tagged_division_of i"
  1.1492 +  shows "(snd ` s) division_of i"
  1.1493 +proof (rule division_ofI)
  1.1494 +  note assm = tagged_division_ofD[OF assms]
  1.1495 +  show "\<Union>(snd ` s) = i" "finite (snd ` s)"
  1.1496 +    using assm by auto
  1.1497 +  fix k
  1.1498 +  assume k: "k \<in> snd ` s"
  1.1499 +  then obtain xk where xk: "(xk, k) \<in> s"
  1.1500 +    by auto
  1.1501 +  then show "k \<subseteq> i" "k \<noteq> {}" "\<exists>a b. k = cbox a b"
  1.1502 +    using assm by fastforce+
  1.1503 +  fix k'
  1.1504 +  assume k': "k' \<in> snd ` s" "k \<noteq> k'"
  1.1505 +  from this(1) obtain xk' where xk': "(xk', k') \<in> s"
  1.1506 +    by auto
  1.1507 +  then show "interior k \<inter> interior k' = {}"
  1.1508 +    using assm(5) k'(2) xk by blast
  1.1509 +qed
  1.1510 +
  1.1511 +lemma partial_division_of_tagged_division:
  1.1512 +  assumes "s tagged_partial_division_of i"
  1.1513 +  shows "(snd ` s) division_of \<Union>(snd ` s)"
  1.1514 +proof (rule division_ofI)
  1.1515 +  note assm = tagged_partial_division_ofD[OF assms]
  1.1516 +  show "finite (snd ` s)" "\<Union>(snd ` s) = \<Union>(snd ` s)"
  1.1517 +    using assm by auto
  1.1518 +  fix k
  1.1519 +  assume k: "k \<in> snd ` s"
  1.1520 +  then obtain xk where xk: "(xk, k) \<in> s"
  1.1521 +    by auto
  1.1522 +  then show "k \<noteq> {}" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>(snd ` s)"
  1.1523 +    using assm by auto
  1.1524 +  fix k'
  1.1525 +  assume k': "k' \<in> snd ` s" "k \<noteq> k'"
  1.1526 +  from this(1) obtain xk' where xk': "(xk', k') \<in> s"
  1.1527 +    by auto
  1.1528 +  then show "interior k \<inter> interior k' = {}"
  1.1529 +    using assm(5) k'(2) xk by auto
  1.1530 +qed
  1.1531 +
  1.1532 +lemma tagged_partial_division_subset:
  1.1533 +  assumes "s tagged_partial_division_of i"
  1.1534 +    and "t \<subseteq> s"
  1.1535 +  shows "t tagged_partial_division_of i"
  1.1536 +  using assms
  1.1537 +  unfolding tagged_partial_division_of_def
  1.1538 +  using finite_subset[OF assms(2)]
  1.1539 +  by blast
  1.1540 +
  1.1541 +lemma (in comm_monoid_set) over_tagged_division_lemma:
  1.1542 +  assumes "p tagged_division_of i"
  1.1543 +    and "\<And>u v. cbox u v \<noteq> {} \<Longrightarrow> content (cbox u v) = 0 \<Longrightarrow> d (cbox u v) = \<^bold>1"
  1.1544 +  shows "F (\<lambda>(x,k). d k) p = F d (snd ` p)"
  1.1545 +proof -
  1.1546 +  have *: "(\<lambda>(x,k). d k) = d \<circ> snd"
  1.1547 +    unfolding o_def by (rule ext) auto
  1.1548 +  note assm = tagged_division_ofD[OF assms(1)]
  1.1549 +  show ?thesis
  1.1550 +    unfolding *
  1.1551 +  proof (rule reindex_nontrivial[symmetric])
  1.1552 +    show "finite p"
  1.1553 +      using assm by auto
  1.1554 +    fix x y
  1.1555 +    assume "x\<in>p" "y\<in>p" "x\<noteq>y" "snd x = snd y"
  1.1556 +    obtain a b where ab: "snd x = cbox a b"
  1.1557 +      using assm(4)[of "fst x" "snd x"] \<open>x\<in>p\<close> by auto
  1.1558 +    have "(fst x, snd y) \<in> p" "(fst x, snd y) \<noteq> y"
  1.1559 +      by (metis prod.collapse \<open>x\<in>p\<close> \<open>snd x = snd y\<close> \<open>x \<noteq> y\<close>)+
  1.1560 +    with \<open>x\<in>p\<close> \<open>y\<in>p\<close> have "interior (snd x) \<inter> interior (snd y) = {}"
  1.1561 +      by (intro assm(5)[of "fst x" _ "fst y"]) auto
  1.1562 +    then have "content (cbox a b) = 0"
  1.1563 +      unfolding \<open>snd x = snd y\<close>[symmetric] ab content_eq_0_interior by auto
  1.1564 +    then have "d (cbox a b) = \<^bold>1"
  1.1565 +      using assm(2)[of "fst x" "snd x"] \<open>x\<in>p\<close> ab[symmetric] by (intro assms(2)) auto
  1.1566 +    then show "d (snd x) = \<^bold>1"
  1.1567 +      unfolding ab by auto
  1.1568 +  qed
  1.1569 +qed
  1.1570 +
  1.1571 +lemma tag_in_interval: "p tagged_division_of i \<Longrightarrow> (x, k) \<in> p \<Longrightarrow> x \<in> i"
  1.1572 +  by auto
  1.1573 +
  1.1574 +lemma tagged_division_of_empty: "{} tagged_division_of {}"
  1.1575 +  unfolding tagged_division_of by auto
  1.1576 +
  1.1577 +lemma tagged_partial_division_of_trivial[simp]: "p tagged_partial_division_of {} \<longleftrightarrow> p = {}"
  1.1578 +  unfolding tagged_partial_division_of_def by auto
  1.1579 +
  1.1580 +lemma tagged_division_of_trivial[simp]: "p tagged_division_of {} \<longleftrightarrow> p = {}"
  1.1581 +  unfolding tagged_division_of by auto
  1.1582 +
  1.1583 +lemma tagged_division_of_self: "x \<in> cbox a b \<Longrightarrow> {(x,cbox a b)} tagged_division_of (cbox a b)"
  1.1584 +  by (rule tagged_division_ofI) auto
  1.1585 +
  1.1586 +lemma tagged_division_of_self_real: "x \<in> {a .. b::real} \<Longrightarrow> {(x,{a .. b})} tagged_division_of {a .. b}"
  1.1587 +  unfolding box_real[symmetric]
  1.1588 +  by (rule tagged_division_of_self)
  1.1589 +
  1.1590 +lemma tagged_division_union:
  1.1591 +  assumes "p1 tagged_division_of s1"
  1.1592 +    and "p2 tagged_division_of s2"
  1.1593 +    and "interior s1 \<inter> interior s2 = {}"
  1.1594 +  shows "(p1 \<union> p2) tagged_division_of (s1 \<union> s2)"
  1.1595 +proof (rule tagged_division_ofI)
  1.1596 +  note p1 = tagged_division_ofD[OF assms(1)]
  1.1597 +  note p2 = tagged_division_ofD[OF assms(2)]
  1.1598 +  show "finite (p1 \<union> p2)"
  1.1599 +    using p1(1) p2(1) by auto
  1.1600 +  show "\<Union>{k. \<exists>x. (x, k) \<in> p1 \<union> p2} = s1 \<union> s2"
  1.1601 +    using p1(6) p2(6) by blast
  1.1602 +  fix x k
  1.1603 +  assume xk: "(x, k) \<in> p1 \<union> p2"
  1.1604 +  show "x \<in> k" "\<exists>a b. k = cbox a b"
  1.1605 +    using xk p1(2,4) p2(2,4) by auto
  1.1606 +  show "k \<subseteq> s1 \<union> s2"
  1.1607 +    using xk p1(3) p2(3) by blast
  1.1608 +  fix x' k'
  1.1609 +  assume xk': "(x', k') \<in> p1 \<union> p2" "(x, k) \<noteq> (x', k')"
  1.1610 +  have *: "\<And>a b. a \<subseteq> s1 \<Longrightarrow> b \<subseteq> s2 \<Longrightarrow> interior a \<inter> interior b = {}"
  1.1611 +    using assms(3) interior_mono by blast
  1.1612 +  show "interior k \<inter> interior k' = {}"
  1.1613 +    apply (cases "(x, k) \<in> p1")
  1.1614 +    apply (meson "*" UnE assms(1) assms(2) p1(5) tagged_division_ofD(3) xk'(1) xk'(2))
  1.1615 +    by (metis "*" UnE assms(1) assms(2) inf_sup_aci(1) p2(5) tagged_division_ofD(3) xk xk'(1) xk'(2))
  1.1616 +qed
  1.1617 +
  1.1618 +lemma tagged_division_unions:
  1.1619 +  assumes "finite iset"
  1.1620 +    and "\<forall>i\<in>iset. pfn i tagged_division_of i"
  1.1621 +    and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior(i1) \<inter> interior(i2) = {}"
  1.1622 +  shows "\<Union>(pfn ` iset) tagged_division_of (\<Union>iset)"
  1.1623 +proof (rule tagged_division_ofI)
  1.1624 +  note assm = tagged_division_ofD[OF assms(2)[rule_format]]
  1.1625 +  show "finite (\<Union>(pfn ` iset))"
  1.1626 +    apply (rule finite_Union)
  1.1627 +    using assms
  1.1628 +    apply auto
  1.1629 +    done
  1.1630 +  have "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>((\<lambda>i. \<Union>{k. \<exists>x. (x, k) \<in> pfn i}) ` iset)"
  1.1631 +    by blast
  1.1632 +  also have "\<dots> = \<Union>iset"
  1.1633 +    using assm(6) by auto
  1.1634 +  finally show "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>iset" .
  1.1635 +  fix x k
  1.1636 +  assume xk: "(x, k) \<in> \<Union>(pfn ` iset)"
  1.1637 +  then obtain i where i: "i \<in> iset" "(x, k) \<in> pfn i"
  1.1638 +    by auto
  1.1639 +  show "x \<in> k" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>iset"
  1.1640 +    using assm(2-4)[OF i] using i(1) by auto
  1.1641 +  fix x' k'
  1.1642 +  assume xk': "(x', k') \<in> \<Union>(pfn ` iset)" "(x, k) \<noteq> (x', k')"
  1.1643 +  then obtain i' where i': "i' \<in> iset" "(x', k') \<in> pfn i'"
  1.1644 +    by auto
  1.1645 +  have *: "\<And>a b. i \<noteq> i' \<Longrightarrow> a \<subseteq> i \<Longrightarrow> b \<subseteq> i' \<Longrightarrow> interior a \<inter> interior b = {}"
  1.1646 +    using i(1) i'(1)
  1.1647 +    using assms(3)[rule_format] interior_mono
  1.1648 +    by blast
  1.1649 +  show "interior k \<inter> interior k' = {}"
  1.1650 +    apply (cases "i = i'")
  1.1651 +    using assm(5) i' i(2) xk'(2) apply blast
  1.1652 +    using "*" assm(3) i' i by auto
  1.1653 +qed
  1.1654 +
  1.1655 +lemma tagged_partial_division_of_union_self:
  1.1656 +  assumes "p tagged_partial_division_of s"
  1.1657 +  shows "p tagged_division_of (\<Union>(snd ` p))"
  1.1658 +  apply (rule tagged_division_ofI)
  1.1659 +  using tagged_partial_division_ofD[OF assms]
  1.1660 +  apply auto
  1.1661 +  done
  1.1662 +
  1.1663 +lemma tagged_division_of_union_self:
  1.1664 +  assumes "p tagged_division_of s"
  1.1665 +  shows "p tagged_division_of (\<Union>(snd ` p))"
  1.1666 +  apply (rule tagged_division_ofI)
  1.1667 +  using tagged_division_ofD[OF assms]
  1.1668 +  apply auto
  1.1669 +  done
  1.1670 +
  1.1671 +subsection \<open>Functions closed on boxes: morphisms from boxes to monoids\<close>
  1.1672 +
  1.1673 +text \<open>This auxiliary structure is used to sum up over the elements of a division. Main theorem is
  1.1674 +  @{text operative_division}. Instances for the monoid are @{typ "'a option"}, @{typ real}, and
  1.1675 +  @{typ bool}.\<close>
  1.1676 +
  1.1677 +lemma property_empty_interval: "\<forall>a b. content (cbox a b) = 0 \<longrightarrow> P (cbox a b) \<Longrightarrow> P {}"
  1.1678 +  using content_empty unfolding empty_as_interval by auto
  1.1679 +
  1.1680 +paragraph \<open>Using additivity of lifted function to encode definedness.\<close>
  1.1681 +
  1.1682 +definition lift_option :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a option \<Rightarrow> 'b option \<Rightarrow> 'c option"
  1.1683 +where
  1.1684 +  "lift_option f a' b' = Option.bind a' (\<lambda>a. Option.bind b' (\<lambda>b. Some (f a b)))"
  1.1685 +
  1.1686 +lemma lift_option_simps[simp]:
  1.1687 +  "lift_option f (Some a) (Some b) = Some (f a b)"
  1.1688 +  "lift_option f None b' = None"
  1.1689 +  "lift_option f a' None = None"
  1.1690 +  by (auto simp: lift_option_def)
  1.1691 +
  1.1692 +lemma (in comm_monoid) comm_monoid_lift_option: "comm_monoid (lift_option op \<^bold>*) (Some \<^bold>1)"
  1.1693 +  proof qed (auto simp: lift_option_def ac_simps split: bind_split)
  1.1694 +
  1.1695 +lemma (in comm_monoid) comm_monoid_set_lift_option: "comm_monoid_set (lift_option op \<^bold>*) (Some \<^bold>1)"
  1.1696 +  proof qed (auto simp: lift_option_def ac_simps split: bind_split)
  1.1697 +
  1.1698 +lemma comm_monoid_and: "comm_monoid op \<and> True"
  1.1699 +  proof qed auto
  1.1700 +
  1.1701 +lemma comm_monoid_set_and: "comm_monoid_set op \<and> True"
  1.1702 +  proof qed auto
  1.1703 +
  1.1704 +paragraph \<open>Operative\<close>
  1.1705 +
  1.1706 +definition (in comm_monoid) operative :: "('b::euclidean_space set \<Rightarrow> 'a) \<Rightarrow> bool"
  1.1707 +  where "operative g \<longleftrightarrow>
  1.1708 +    (\<forall>a b. content (cbox a b) = 0 \<longrightarrow> g (cbox a b) = \<^bold>1) \<and>
  1.1709 +    (\<forall>a b c. \<forall>k\<in>Basis. g (cbox a b) = g (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<^bold>* g (cbox a b \<inter> {x. x\<bullet>k \<ge> c}))"
  1.1710 +
  1.1711 +lemma (in comm_monoid) operativeD[dest]:
  1.1712 +  assumes "operative g"
  1.1713 +  shows "\<And>a b. content (cbox a b) = 0 \<Longrightarrow> g (cbox a b) = \<^bold>1"
  1.1714 +    and "\<And>a b c k. k \<in> Basis \<Longrightarrow> g (cbox a b) = g (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<^bold>* g (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  1.1715 +  using assms unfolding operative_def by auto
  1.1716 +
  1.1717 +lemma (in comm_monoid) operative_empty: "operative g \<Longrightarrow> g {} = \<^bold>1"
  1.1718 +  unfolding operative_def by (rule property_empty_interval) auto
  1.1719 +
  1.1720 +lemma operative_content[intro]: "add.operative content"
  1.1721 +  by (force simp add: add.operative_def content_split[symmetric])
  1.1722 +
  1.1723 +definition "division_points (k::('a::euclidean_space) set) d =
  1.1724 +   {(j,x). j \<in> Basis \<and> (interval_lowerbound k)\<bullet>j < x \<and> x < (interval_upperbound k)\<bullet>j \<and>
  1.1725 +     (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  1.1726 +
  1.1727 +lemma division_points_finite:
  1.1728 +  fixes i :: "'a::euclidean_space set"
  1.1729 +  assumes "d division_of i"
  1.1730 +  shows "finite (division_points i d)"
  1.1731 +proof -
  1.1732 +  note assm = division_ofD[OF assms]
  1.1733 +  let ?M = "\<lambda>j. {(j,x)|x. (interval_lowerbound i)\<bullet>j < x \<and> x < (interval_upperbound i)\<bullet>j \<and>
  1.1734 +    (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  1.1735 +  have *: "division_points i d = \<Union>(?M ` Basis)"
  1.1736 +    unfolding division_points_def by auto
  1.1737 +  show ?thesis
  1.1738 +    unfolding * using assm by auto
  1.1739 +qed
  1.1740 +
  1.1741 +lemma division_points_subset:
  1.1742 +  fixes a :: "'a::euclidean_space"
  1.1743 +  assumes "d division_of (cbox a b)"
  1.1744 +    and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  1.1745 +    and k: "k \<in> Basis"
  1.1746 +  shows "division_points (cbox a b \<inter> {x. x\<bullet>k \<le> c}) {l \<inter> {x. x\<bullet>k \<le> c} | l . l \<in> d \<and> l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}} \<subseteq>
  1.1747 +      division_points (cbox a b) d" (is ?t1)
  1.1748 +    and "division_points (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) {l \<inter> {x. x\<bullet>k \<ge> c} | l . l \<in> d \<and> ~(l \<inter> {x. x\<bullet>k \<ge> c} = {})} \<subseteq>
  1.1749 +      division_points (cbox a b) d" (is ?t2)
  1.1750 +proof -
  1.1751 +  note assm = division_ofD[OF assms(1)]
  1.1752 +  have *: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  1.1753 +    "\<forall>i\<in>Basis. a\<bullet>i \<le> (\<Sum>i\<in>Basis. (if i = k then min (b \<bullet> k) c else  b \<bullet> i) *\<^sub>R i) \<bullet> i"
  1.1754 +    "\<forall>i\<in>Basis. (\<Sum>i\<in>Basis. (if i = k then max (a \<bullet> k) c else a \<bullet> i) *\<^sub>R i) \<bullet> i \<le> b\<bullet>i"
  1.1755 +    "min (b \<bullet> k) c = c" "max (a \<bullet> k) c = c"
  1.1756 +    using assms using less_imp_le by auto
  1.1757 +  show ?t1 (*FIXME a horrible mess*)
  1.1758 +    unfolding division_points_def interval_split[OF k, of a b]
  1.1759 +    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  1.1760 +    unfolding *
  1.1761 +    apply (rule subsetI)
  1.1762 +    unfolding mem_Collect_eq split_beta
  1.1763 +    apply (erule bexE conjE)+
  1.1764 +    apply (simp add: )
  1.1765 +    apply (erule exE conjE)+
  1.1766 +  proof
  1.1767 +    fix i l x
  1.1768 +    assume as:
  1.1769 +      "a \<bullet> fst x < snd x" "snd x < (if fst x = k then c else b \<bullet> fst x)"
  1.1770 +      "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1.1771 +      "i = l \<inter> {x. x \<bullet> k \<le> c}" "l \<in> d" "l \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}"
  1.1772 +      and fstx: "fst x \<in> Basis"
  1.1773 +    from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  1.1774 +    have *: "\<forall>i\<in>Basis. u \<bullet> i \<le> (\<Sum>i\<in>Basis. (if i = k then min (v \<bullet> k) c else v \<bullet> i) *\<^sub>R i) \<bullet> i"
  1.1775 +      using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  1.1776 +    have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  1.1777 +      using l using as(6) unfolding box_ne_empty[symmetric] by auto
  1.1778 +    show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1.1779 +      apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  1.1780 +      using as(1-3,5) fstx
  1.1781 +      unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  1.1782 +      apply (auto split: if_split_asm)
  1.1783 +      done
  1.1784 +    show "snd x < b \<bullet> fst x"
  1.1785 +      using as(2) \<open>c < b\<bullet>k\<close> by (auto split: if_split_asm)
  1.1786 +  qed
  1.1787 +  show ?t2
  1.1788 +    unfolding division_points_def interval_split[OF k, of a b]
  1.1789 +    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  1.1790 +    unfolding *
  1.1791 +    unfolding subset_eq
  1.1792 +    apply rule
  1.1793 +    unfolding mem_Collect_eq split_beta
  1.1794 +    apply (erule bexE conjE)+
  1.1795 +    apply (simp only: mem_Collect_eq inner_setsum_left_Basis simp_thms)
  1.1796 +    apply (erule exE conjE)+
  1.1797 +  proof
  1.1798 +    fix i l x
  1.1799 +    assume as:
  1.1800 +      "(if fst x = k then c else a \<bullet> fst x) < snd x" "snd x < b \<bullet> fst x"
  1.1801 +      "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1.1802 +      "i = l \<inter> {x. c \<le> x \<bullet> k}" "l \<in> d" "l \<inter> {x. c \<le> x \<bullet> k} \<noteq> {}"
  1.1803 +      and fstx: "fst x \<in> Basis"
  1.1804 +    from assm(4)[OF this(5)] guess u v by (elim exE) note l=this
  1.1805 +    have *: "\<forall>i\<in>Basis. (\<Sum>i\<in>Basis. (if i = k then max (u \<bullet> k) c else u \<bullet> i) *\<^sub>R i) \<bullet> i \<le> v \<bullet> i"
  1.1806 +      using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  1.1807 +    have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  1.1808 +      using l using as(6) unfolding box_ne_empty[symmetric] by auto
  1.1809 +    show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1.1810 +      apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  1.1811 +      using as(1-3,5) fstx
  1.1812 +      unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  1.1813 +      apply (auto split: if_split_asm)
  1.1814 +      done
  1.1815 +    show "a \<bullet> fst x < snd x"
  1.1816 +      using as(1) \<open>a\<bullet>k < c\<close> by (auto split: if_split_asm)
  1.1817 +   qed
  1.1818 +qed
  1.1819 +
  1.1820 +lemma division_points_psubset:
  1.1821 +  fixes a :: "'a::euclidean_space"
  1.1822 +  assumes "d division_of (cbox a b)"
  1.1823 +      and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  1.1824 +      and "l \<in> d"
  1.1825 +      and "interval_lowerbound l\<bullet>k = c \<or> interval_upperbound l\<bullet>k = c"
  1.1826 +      and k: "k \<in> Basis"
  1.1827 +  shows "division_points (cbox a b \<inter> {x. x\<bullet>k \<le> c}) {l \<inter> {x. x\<bullet>k \<le> c} | l. l\<in>d \<and> l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}} \<subset>
  1.1828 +         division_points (cbox a b) d" (is "?D1 \<subset> ?D")
  1.1829 +    and "division_points (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) {l \<inter> {x. x\<bullet>k \<ge> c} | l. l\<in>d \<and> l \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}} \<subset>
  1.1830 +         division_points (cbox a b) d" (is "?D2 \<subset> ?D")
  1.1831 +proof -
  1.1832 +  have ab: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  1.1833 +    using assms(2) by (auto intro!:less_imp_le)
  1.1834 +  guess u v using division_ofD(4)[OF assms(1,5)] by (elim exE) note l=this
  1.1835 +  have uv: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i" "\<forall>i\<in>Basis. a\<bullet>i \<le> u\<bullet>i \<and> v\<bullet>i \<le> b\<bullet>i"
  1.1836 +    using division_ofD(2,2,3)[OF assms(1,5)] unfolding l box_ne_empty
  1.1837 +    using subset_box(1)
  1.1838 +    apply auto
  1.1839 +    apply blast+
  1.1840 +    done
  1.1841 +  have *: "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  1.1842 +          "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  1.1843 +    unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  1.1844 +    using uv[rule_format, of k] ab k
  1.1845 +    by auto
  1.1846 +  have "\<exists>x. x \<in> ?D - ?D1"
  1.1847 +    using assms(3-)
  1.1848 +    unfolding division_points_def interval_bounds[OF ab]
  1.1849 +    apply -
  1.1850 +    apply (erule disjE)
  1.1851 +    apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  1.1852 +    apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  1.1853 +    done
  1.1854 +  moreover have "?D1 \<subseteq> ?D"
  1.1855 +    by (auto simp add: assms division_points_subset)
  1.1856 +  ultimately show "?D1 \<subset> ?D"
  1.1857 +    by blast
  1.1858 +  have *: "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  1.1859 +    "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  1.1860 +    unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  1.1861 +    using uv[rule_format, of k] ab k
  1.1862 +    by auto
  1.1863 +  have "\<exists>x. x \<in> ?D - ?D2"
  1.1864 +    using assms(3-)
  1.1865 +    unfolding division_points_def interval_bounds[OF ab]
  1.1866 +    apply -
  1.1867 +    apply (erule disjE)
  1.1868 +    apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  1.1869 +    apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  1.1870 +    done
  1.1871 +  moreover have "?D2 \<subseteq> ?D"
  1.1872 +    by (auto simp add: assms division_points_subset)
  1.1873 +  ultimately show "?D2 \<subset> ?D"
  1.1874 +    by blast
  1.1875 +qed
  1.1876 +
  1.1877 +lemma (in comm_monoid_set) operative_division:
  1.1878 +  fixes g :: "'b::euclidean_space set \<Rightarrow> 'a"
  1.1879 +  assumes g: "operative g" and d: "d division_of (cbox a b)" shows "F g d = g (cbox a b)"
  1.1880 +proof -
  1.1881 +  define C where [abs_def]: "C = card (division_points (cbox a b) d)"
  1.1882 +  then show ?thesis
  1.1883 +    using d
  1.1884 +  proof (induction C arbitrary: a b d rule: less_induct)
  1.1885 +    case (less a b d)
  1.1886 +    show ?case
  1.1887 +    proof cases
  1.1888 +      show "content (cbox a b) = 0 \<Longrightarrow> F g d = g (cbox a b)"
  1.1889 +        using division_of_content_0[OF _ less.prems] operativeD(1)[OF  g] division_ofD(4)[OF less.prems]
  1.1890 +        by (fastforce intro!: neutral)
  1.1891 +    next
  1.1892 +      assume "content (cbox a b) \<noteq> 0"
  1.1893 +      note ab = this[unfolded content_lt_nz[symmetric] content_pos_lt_eq]
  1.1894 +      then have ab': "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  1.1895 +        by (auto intro!: less_imp_le)
  1.1896 +      show "F g d = g (cbox a b)"
  1.1897 +      proof (cases "division_points (cbox a b) d = {}")
  1.1898 +        case True
  1.1899 +        { fix u v and j :: 'b
  1.1900 +          assume j: "j \<in> Basis" and as: "cbox u v \<in> d"
  1.1901 +          then have "cbox u v \<noteq> {}"
  1.1902 +            using less.prems by blast
  1.1903 +          then have uv: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i" "u\<bullet>j \<le> v\<bullet>j"
  1.1904 +            using j unfolding box_ne_empty by auto
  1.1905 +          have *: "\<And>p r Q. \<not> j\<in>Basis \<or> p \<or> r \<or> (\<forall>x\<in>d. Q x) \<Longrightarrow> p \<or> r \<or> Q (cbox u v)"
  1.1906 +            using as j by auto
  1.1907 +          have "(j, u\<bullet>j) \<notin> division_points (cbox a b) d"
  1.1908 +               "(j, v\<bullet>j) \<notin> division_points (cbox a b) d" using True by auto
  1.1909 +          note this[unfolded de_Morgan_conj division_points_def mem_Collect_eq split_conv interval_bounds[OF ab'] bex_simps]
  1.1910 +          note *[OF this(1)] *[OF this(2)] note this[unfolded interval_bounds[OF uv(1)]]
  1.1911 +          moreover
  1.1912 +          have "a\<bullet>j \<le> u\<bullet>j" "v\<bullet>j \<le> b\<bullet>j"
  1.1913 +            using division_ofD(2,2,3)[OF \<open>d division_of cbox a b\<close> as]
  1.1914 +            apply (metis j subset_box(1) uv(1))
  1.1915 +            by (metis \<open>cbox u v \<subseteq> cbox a b\<close> j subset_box(1) uv(1))
  1.1916 +          ultimately have "u\<bullet>j = a\<bullet>j \<and> v\<bullet>j = a\<bullet>j \<or> u\<bullet>j = b\<bullet>j \<and> v\<bullet>j = b\<bullet>j \<or> u\<bullet>j = a\<bullet>j \<and> v\<bullet>j = b\<bullet>j"
  1.1917 +            unfolding not_less de_Morgan_disj using ab[rule_format,of j] uv(2) j by force }
  1.1918 +        then have d': "\<forall>i\<in>d. \<exists>u v. i = cbox u v \<and>
  1.1919 +          (\<forall>j\<in>Basis. u\<bullet>j = a\<bullet>j \<and> v\<bullet>j = a\<bullet>j \<or> u\<bullet>j = b\<bullet>j \<and> v\<bullet>j = b\<bullet>j \<or> u\<bullet>j = a\<bullet>j \<and> v\<bullet>j = b\<bullet>j)"
  1.1920 +          unfolding forall_in_division[OF less.prems] by blast
  1.1921 +        have "(1/2) *\<^sub>R (a+b) \<in> cbox a b"
  1.1922 +          unfolding mem_box using ab by(auto intro!: less_imp_le simp: inner_simps)
  1.1923 +        note this[unfolded division_ofD(6)[OF \<open>d division_of cbox a b\<close>,symmetric] Union_iff]
  1.1924 +        then guess i .. note i=this
  1.1925 +        guess u v using d'[rule_format,OF i(1)] by (elim exE conjE) note uv=this
  1.1926 +        have "cbox a b \<in> d"
  1.1927 +        proof -
  1.1928 +          have "u = a" "v = b"
  1.1929 +            unfolding euclidean_eq_iff[where 'a='b]
  1.1930 +          proof safe
  1.1931 +            fix j :: 'b
  1.1932 +            assume j: "j \<in> Basis"
  1.1933 +            note i(2)[unfolded uv mem_box,rule_format,of j]
  1.1934 +            then show "u \<bullet> j = a \<bullet> j" and "v \<bullet> j = b \<bullet> j"
  1.1935 +              using uv(2)[rule_format,of j] j by (auto simp: inner_simps)
  1.1936 +          qed
  1.1937 +          then have "i = cbox a b" using uv by auto
  1.1938 +          then show ?thesis using i by auto
  1.1939 +        qed
  1.1940 +        then have deq: "d = insert (cbox a b) (d - {cbox a b})"
  1.1941 +          by auto
  1.1942 +        have "F g (d - {cbox a b}) = \<^bold>1"
  1.1943 +        proof (intro neutral ballI)
  1.1944 +          fix x
  1.1945 +          assume x: "x \<in> d - {cbox a b}"
  1.1946 +          then have "x\<in>d"
  1.1947 +            by auto note d'[rule_format,OF this]
  1.1948 +          then guess u v by (elim exE conjE) note uv=this
  1.1949 +          have "u \<noteq> a \<or> v \<noteq> b"
  1.1950 +            using x[unfolded uv] by auto
  1.1951 +          then obtain j where "u\<bullet>j \<noteq> a\<bullet>j \<or> v\<bullet>j \<noteq> b\<bullet>j" and j: "j \<in> Basis"
  1.1952 +            unfolding euclidean_eq_iff[where 'a='b] by auto
  1.1953 +          then have "u\<bullet>j = v\<bullet>j"
  1.1954 +            using uv(2)[rule_format,OF j] by auto
  1.1955 +          then have "content (cbox u v) = 0"
  1.1956 +            unfolding content_eq_0 using j
  1.1957 +            by force
  1.1958 +          then show "g x = \<^bold>1"
  1.1959 +            unfolding uv(1) by (rule operativeD(1)[OF g])
  1.1960 +        qed
  1.1961 +        then show "F g d = g (cbox a b)"
  1.1962 +          using division_ofD[OF less.prems]
  1.1963 +          apply (subst deq)
  1.1964 +          apply (subst insert)
  1.1965 +          apply auto
  1.1966 +          done
  1.1967 +      next
  1.1968 +        case False
  1.1969 +        then have "\<exists>x. x \<in> division_points (cbox a b) d"
  1.1970 +          by auto
  1.1971 +        then guess k c
  1.1972 +          unfolding split_paired_Ex division_points_def mem_Collect_eq split_conv
  1.1973 +          apply (elim exE conjE)
  1.1974 +          done
  1.1975 +        note this(2-4,1) note kc=this[unfolded interval_bounds[OF ab']]
  1.1976 +        from this(3) guess j .. note j=this
  1.1977 +        define d1 where "d1 = {l \<inter> {x. x\<bullet>k \<le> c} | l. l \<in> d \<and> l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}}"
  1.1978 +        define d2 where "d2 = {l \<inter> {x. x\<bullet>k \<ge> c} | l. l \<in> d \<and> l \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}}"
  1.1979 +        define cb where "cb = (\<Sum>i\<in>Basis. (if i = k then c else b\<bullet>i) *\<^sub>R i)"
  1.1980 +        define ca where "ca = (\<Sum>i\<in>Basis. (if i = k then c else a\<bullet>i) *\<^sub>R i)"
  1.1981 +        note division_points_psubset[OF \<open>d division_of cbox a b\<close> ab kc(1-2) j]
  1.1982 +        note psubset_card_mono[OF _ this(1)] psubset_card_mono[OF _ this(2)]
  1.1983 +        then have *: "F g d1 = g (cbox a b \<inter> {x. x\<bullet>k \<le> c})" "F g d2 = g (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  1.1984 +          unfolding interval_split[OF kc(4)]
  1.1985 +          apply (rule_tac[!] "less.hyps"[rule_format])
  1.1986 +          using division_split[OF \<open>d division_of cbox a b\<close>, where k=k and c=c]
  1.1987 +          apply (simp_all add: interval_split kc d1_def d2_def division_points_finite[OF \<open>d division_of cbox a b\<close>])
  1.1988 +          done
  1.1989 +        { fix l y
  1.1990 +          assume as: "l \<in> d" "y \<in> d" "l \<inter> {x. x \<bullet> k \<le> c} = y \<inter> {x. x \<bullet> k \<le> c}" "l \<noteq> y"
  1.1991 +          from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  1.1992 +          have "g (l \<inter> {x. x \<bullet> k \<le> c}) = \<^bold>1"
  1.1993 +            unfolding leq interval_split[OF kc(4)]
  1.1994 +            apply (rule operativeD[OF g])
  1.1995 +            unfolding interval_split[symmetric, OF kc(4)]
  1.1996 +            using division_split_left_inj less as kc leq by blast
  1.1997 +        } note fxk_le = this
  1.1998 +        { fix l y
  1.1999 +          assume as: "l \<in> d" "y \<in> d" "l \<inter> {x. c \<le> x \<bullet> k} = y \<inter> {x. c \<le> x \<bullet> k}" "l \<noteq> y"
  1.2000 +          from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  1.2001 +          have "g (l \<inter> {x. x \<bullet> k \<ge> c}) = \<^bold>1"
  1.2002 +            unfolding leq interval_split[OF kc(4)]
  1.2003 +            apply (rule operativeD(1)[OF g])
  1.2004 +            unfolding interval_split[symmetric,OF kc(4)]
  1.2005 +            using division_split_right_inj less leq as kc by blast
  1.2006 +        } note fxk_ge = this
  1.2007 +        have d1_alt: "d1 = (\<lambda>l. l \<inter> {x. x\<bullet>k \<le> c}) ` {l \<in> d. l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}}"
  1.2008 +          using d1_def by auto
  1.2009 +        have d2_alt: "d2 = (\<lambda>l. l \<inter> {x. x\<bullet>k \<ge> c}) ` {l \<in> d. l \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}}"
  1.2010 +          using d2_def by auto
  1.2011 +        have "g (cbox a b) = F g d1 \<^bold>* F g d2" (is "_ = ?prev")
  1.2012 +          unfolding * using g kc(4) by blast
  1.2013 +        also have "F g d1 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<le> c})) d"
  1.2014 +          unfolding d1_alt using division_of_finite[OF less.prems] fxk_le
  1.2015 +          by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  1.2016 +        also have "F g d2 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<ge> c})) d"
  1.2017 +          unfolding d2_alt using division_of_finite[OF less.prems] fxk_ge
  1.2018 +          by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  1.2019 +        also have *: "\<forall>x\<in>d. g x = g (x \<inter> {x. x \<bullet> k \<le> c}) \<^bold>* g (x \<inter> {x. c \<le> x \<bullet> k})"
  1.2020 +          unfolding forall_in_division[OF \<open>d division_of cbox a b\<close>]
  1.2021 +          using g kc(4) by blast
  1.2022 +        have "F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<le> c})) d \<^bold>* F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<ge> c})) d = F g d"
  1.2023 +          using * by (simp add: distrib)
  1.2024 +        finally show ?thesis by auto
  1.2025 +      qed
  1.2026 +    qed
  1.2027 +  qed
  1.2028 +qed
  1.2029 +
  1.2030 +lemma (in comm_monoid_set) operative_tagged_division:
  1.2031 +  assumes f: "operative g" and d: "d tagged_division_of (cbox a b)"
  1.2032 +  shows "F (\<lambda>(x, l). g l) d = g (cbox a b)"
  1.2033 +  unfolding d[THEN division_of_tagged_division, THEN operative_division[OF f], symmetric]
  1.2034 +  by (simp add: f[THEN operativeD(1)] over_tagged_division_lemma[OF d])
  1.2035 +
  1.2036 +lemma additive_content_division: "d division_of (cbox a b) \<Longrightarrow> setsum content d = content (cbox a b)"
  1.2037 +  by (metis operative_content setsum.operative_division)
  1.2038 +
  1.2039 +lemma additive_content_tagged_division:
  1.2040 +  "d tagged_division_of (cbox a b) \<Longrightarrow> setsum (\<lambda>(x,l). content l) d = content (cbox a b)"
  1.2041 +  unfolding setsum.operative_tagged_division[OF operative_content, symmetric] by blast
  1.2042 +
  1.2043 +lemma
  1.2044 +  shows real_inner_1_left: "inner 1 x = x"
  1.2045 +  and real_inner_1_right: "inner x 1 = x"
  1.2046 +  by simp_all
  1.2047 +
  1.2048 +lemma content_real_eq_0: "content {a .. b::real} = 0 \<longleftrightarrow> a \<ge> b"
  1.2049 +  by (metis atLeastatMost_empty_iff2 content_empty content_real diff_self eq_iff le_cases le_iff_diff_le_0)
  1.2050 +
  1.2051 +lemma interval_real_split:
  1.2052 +  "{a .. b::real} \<inter> {x. x \<le> c} = {a .. min b c}"
  1.2053 +  "{a .. b} \<inter> {x. c \<le> x} = {max a c .. b}"
  1.2054 +  apply (metis Int_atLeastAtMostL1 atMost_def)
  1.2055 +  apply (metis Int_atLeastAtMostL2 atLeast_def)
  1.2056 +  done
  1.2057 +
  1.2058 +lemma (in comm_monoid) operative_1_lt:
  1.2059 +  "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  1.2060 +    ((\<forall>a b. b \<le> a \<longrightarrow> g {a .. b} = \<^bold>1) \<and> (\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a .. c} \<^bold>* g {c .. b} = g {a .. b}))"
  1.2061 +  apply (simp add: operative_def content_real_eq_0 atMost_def[symmetric] atLeast_def[symmetric]
  1.2062 +              del: content_real_if)
  1.2063 +proof safe
  1.2064 +  fix a b c :: real
  1.2065 +  assume *: "\<forall>a b c. g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  1.2066 +  assume "a < c" "c < b"
  1.2067 +  with *[rule_format, of a b c] show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  1.2068 +    by (simp add: less_imp_le min.absorb2 max.absorb2)
  1.2069 +next
  1.2070 +  fix a b c :: real
  1.2071 +  assume as: "\<forall>a b. b \<le> a \<longrightarrow> g {a..b} = \<^bold>1"
  1.2072 +    "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  1.2073 +  from as(1)[rule_format, of 0 1] as(1)[rule_format, of a a for a] as(2)
  1.2074 +  have [simp]: "g {} = \<^bold>1" "\<And>a. g {a} = \<^bold>1"
  1.2075 +    "\<And>a b c. a < c \<Longrightarrow> c < b \<Longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  1.2076 +    by auto
  1.2077 +  show "g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  1.2078 +    by (auto simp: min_def max_def le_less)
  1.2079 +qed
  1.2080 +
  1.2081 +lemma (in comm_monoid) operative_1_le:
  1.2082 +  "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  1.2083 +    ((\<forall>a b. b \<le> a \<longrightarrow> g {a..b} = \<^bold>1) \<and> (\<forall>a b c. a \<le> c \<and> c \<le> b \<longrightarrow> g {a .. c} \<^bold>* g {c .. b} = g {a .. b}))"
  1.2084 +  unfolding operative_1_lt
  1.2085 +proof safe
  1.2086 +  fix a b c :: real
  1.2087 +  assume as: "\<forall>a b c. a \<le> c \<and> c \<le> b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}" "a < c" "c < b"
  1.2088 +  show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  1.2089 +    apply (rule as(1)[rule_format])
  1.2090 +    using as(2-)
  1.2091 +    apply auto
  1.2092 +    done
  1.2093 +next
  1.2094 +  fix a b c :: real
  1.2095 +  assume "\<forall>a b. b \<le> a \<longrightarrow> g {a .. b} = \<^bold>1"
  1.2096 +    and "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  1.2097 +    and "a \<le> c"
  1.2098 +    and "c \<le> b"
  1.2099 +  note as = this[rule_format]
  1.2100 +  show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  1.2101 +  proof (cases "c = a \<or> c = b")
  1.2102 +    case False
  1.2103 +    then show ?thesis
  1.2104 +      apply -
  1.2105 +      apply (subst as(2))
  1.2106 +      using as(3-)
  1.2107 +      apply auto
  1.2108 +      done
  1.2109 +  next
  1.2110 +    case True
  1.2111 +    then show ?thesis
  1.2112 +    proof
  1.2113 +      assume *: "c = a"
  1.2114 +      then have "g {a .. c} = \<^bold>1"
  1.2115 +        apply -
  1.2116 +        apply (rule as(1)[rule_format])
  1.2117 +        apply auto
  1.2118 +        done
  1.2119 +      then show ?thesis
  1.2120 +        unfolding * by auto
  1.2121 +    next
  1.2122 +      assume *: "c = b"
  1.2123 +      then have "g {c .. b} = \<^bold>1"
  1.2124 +        apply -
  1.2125 +        apply (rule as(1)[rule_format])
  1.2126 +        apply auto
  1.2127 +        done
  1.2128 +      then show ?thesis
  1.2129 +        unfolding * by auto
  1.2130 +    qed
  1.2131 +  qed
  1.2132 +qed
  1.2133 +
  1.2134 +subsection \<open>Fine-ness of a partition w.r.t. a gauge.\<close>
  1.2135 +
  1.2136 +definition fine  (infixr "fine" 46)
  1.2137 +  where "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d x)"
  1.2138 +
  1.2139 +lemma fineI:
  1.2140 +  assumes "\<And>x k. (x, k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  1.2141 +  shows "d fine s"
  1.2142 +  using assms unfolding fine_def by auto
  1.2143 +
  1.2144 +lemma fineD[dest]:
  1.2145 +  assumes "d fine s"
  1.2146 +  shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  1.2147 +  using assms unfolding fine_def by auto
  1.2148 +
  1.2149 +lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
  1.2150 +  unfolding fine_def by auto
  1.2151 +
  1.2152 +lemma fine_inters:
  1.2153 + "(\<lambda>x. \<Inter>{f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
  1.2154 +  unfolding fine_def by blast
  1.2155 +
  1.2156 +lemma fine_union: "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
  1.2157 +  unfolding fine_def by blast
  1.2158 +
  1.2159 +lemma fine_unions: "(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
  1.2160 +  unfolding fine_def by auto
  1.2161 +
  1.2162 +lemma fine_subset: "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
  1.2163 +  unfolding fine_def by blast
  1.2164 +
  1.2165 +
  1.2166 +subsection \<open>Gauge integral. Define on compact intervals first, then use a limit.\<close>
  1.2167 +
  1.2168 +definition has_integral_compact_interval (infixr "has'_integral'_compact'_interval" 46)
  1.2169 +  where "(f has_integral_compact_interval y) i \<longleftrightarrow>
  1.2170 +    (\<forall>e>0. \<exists>d. gauge d \<and>
  1.2171 +      (\<forall>p. p tagged_division_of i \<and> d fine p \<longrightarrow>
  1.2172 +        norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - y) < e))"
  1.2173 +
  1.2174 +definition has_integral ::
  1.2175 +    "('n::euclidean_space \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'b \<Rightarrow> 'n set \<Rightarrow> bool"
  1.2176 +  (infixr "has'_integral" 46)
  1.2177 +  where "(f has_integral y) i \<longleftrightarrow>
  1.2178 +    (if \<exists>a b. i = cbox a b
  1.2179 +     then (f has_integral_compact_interval y) i
  1.2180 +     else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  1.2181 +      (\<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral_compact_interval z) (cbox a b) \<and>
  1.2182 +        norm (z - y) < e)))"
  1.2183 +
  1.2184 +lemma has_integral:
  1.2185 +  "(f has_integral y) (cbox a b) \<longleftrightarrow>
  1.2186 +    (\<forall>e>0. \<exists>d. gauge d \<and>
  1.2187 +      (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  1.2188 +        norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
  1.2189 +  unfolding has_integral_def has_integral_compact_interval_def
  1.2190 +  by auto
  1.2191 +
  1.2192 +lemma has_integral_real:
  1.2193 +  "(f has_integral y) {a .. b::real} \<longleftrightarrow>
  1.2194 +    (\<forall>e>0. \<exists>d. gauge d \<and>
  1.2195 +      (\<forall>p. p tagged_division_of {a .. b} \<and> d fine p \<longrightarrow>
  1.2196 +        norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
  1.2197 +  unfolding box_real[symmetric]
  1.2198 +  by (rule has_integral)
  1.2199 +
  1.2200 +lemma has_integralD[dest]:
  1.2201 +  assumes "(f has_integral y) (cbox a b)"
  1.2202 +    and "e > 0"
  1.2203 +  obtains d where "gauge d"
  1.2204 +    and "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d fine p \<Longrightarrow>
  1.2205 +      norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f(x)) p - y) < e"
  1.2206 +  using assms unfolding has_integral by auto
  1.2207 +
  1.2208 +lemma has_integral_alt:
  1.2209 +  "(f has_integral y) i \<longleftrightarrow>
  1.2210 +    (if \<exists>a b. i = cbox a b
  1.2211 +     then (f has_integral y) i
  1.2212 +     else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  1.2213 +      (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e)))"
  1.2214 +  unfolding has_integral
  1.2215 +  unfolding has_integral_compact_interval_def has_integral_def
  1.2216 +  by auto
  1.2217 +
  1.2218 +lemma has_integral_altD:
  1.2219 +  assumes "(f has_integral y) i"
  1.2220 +    and "\<not> (\<exists>a b. i = cbox a b)"
  1.2221 +    and "e>0"
  1.2222 +  obtains B where "B > 0"
  1.2223 +    and "\<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  1.2224 +      (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) (cbox a b) \<and> norm(z - y) < e)"
  1.2225 +  using assms
  1.2226 +  unfolding has_integral
  1.2227 +  unfolding has_integral_compact_interval_def has_integral_def
  1.2228 +  by auto
  1.2229 +
  1.2230 +definition integrable_on (infixr "integrable'_on" 46)
  1.2231 +  where "f integrable_on i \<longleftrightarrow> (\<exists>y. (f has_integral y) i)"
  1.2232 +
  1.2233 +definition "integral i f = (SOME y. (f has_integral y) i \<or> ~ f integrable_on i \<and> y=0)"
  1.2234 +
  1.2235 +lemma integrable_integral[dest]: "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
  1.2236 +  unfolding integrable_on_def integral_def by (metis (mono_tags, lifting) someI_ex)
  1.2237 +
  1.2238 +lemma not_integrable_integral: "~ f integrable_on i \<Longrightarrow> integral i f = 0"
  1.2239 +  unfolding integrable_on_def integral_def by blast
  1.2240 +
  1.2241 +lemma has_integral_integrable[intro]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
  1.2242 +  unfolding integrable_on_def by auto
  1.2243 +
  1.2244 +lemma has_integral_integral: "f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
  1.2245 +  by auto
  1.2246 +
  1.2247 +lemma setsum_content_null:
  1.2248 +  assumes "content (cbox a b) = 0"
  1.2249 +    and "p tagged_division_of (cbox a b)"
  1.2250 +  shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
  1.2251 +proof (rule setsum.neutral, rule)
  1.2252 +  fix y
  1.2253 +  assume y: "y \<in> p"
  1.2254 +  obtain x k where xk: "y = (x, k)"
  1.2255 +    using surj_pair[of y] by blast
  1.2256 +  note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
  1.2257 +  from this(2) obtain c d where k: "k = cbox c d" by blast
  1.2258 +  have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x"
  1.2259 +    unfolding xk by auto
  1.2260 +  also have "\<dots> = 0"
  1.2261 +    using content_subset[OF assm(1)[unfolded k]] content_pos_le[of c d]
  1.2262 +    unfolding assms(1) k
  1.2263 +    by auto
  1.2264 +  finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
  1.2265 +qed
  1.2266 +
  1.2267 +
  1.2268 +subsection \<open>Some basic combining lemmas.\<close>
  1.2269 +
  1.2270 +lemma tagged_division_unions_exists:
  1.2271 +  assumes "finite iset"
  1.2272 +    and "\<forall>i\<in>iset. \<exists>p. p tagged_division_of i \<and> d fine p"
  1.2273 +    and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior i1 \<inter> interior i2 = {}"
  1.2274 +    and "\<Union>iset = i"
  1.2275 +   obtains p where "p tagged_division_of i" and "d fine p"
  1.2276 +proof -
  1.2277 +  obtain pfn where pfn:
  1.2278 +    "\<And>x. x \<in> iset \<Longrightarrow> pfn x tagged_division_of x"
  1.2279 +    "\<And>x. x \<in> iset \<Longrightarrow> d fine pfn x"
  1.2280 +    using bchoice[OF assms(2)] by auto
  1.2281 +  show thesis
  1.2282 +    apply (rule_tac p="\<Union>(pfn ` iset)" in that)
  1.2283 +    using assms(1) assms(3) assms(4) pfn(1) tagged_division_unions apply force
  1.2284 +    by (metis (mono_tags, lifting) fine_unions imageE pfn(2))
  1.2285 +qed
  1.2286 +
  1.2287 +
  1.2288 +subsection \<open>The set we're concerned with must be closed.\<close>
  1.2289 +
  1.2290 +lemma division_of_closed:
  1.2291 +  fixes i :: "'n::euclidean_space set"
  1.2292 +  shows "s division_of i \<Longrightarrow> closed i"
  1.2293 +  unfolding division_of_def by fastforce
  1.2294 +
  1.2295 +subsection \<open>General bisection principle for intervals; might be useful elsewhere.\<close>
  1.2296 +
  1.2297 +lemma interval_bisection_step:
  1.2298 +  fixes type :: "'a::euclidean_space"
  1.2299 +  assumes "P {}"
  1.2300 +    and "\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P (s \<union> t)"
  1.2301 +    and "\<not> P (cbox a (b::'a))"
  1.2302 +  obtains c d where "\<not> P (cbox c d)"
  1.2303 +    and "\<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> c\<bullet>i \<le> d\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i \<and> 2 * (d\<bullet>i - c\<bullet>i) \<le> b\<bullet>i - a\<bullet>i"
  1.2304 +proof -
  1.2305 +  have "cbox a b \<noteq> {}"
  1.2306 +    using assms(1,3) by metis
  1.2307 +  then have ab: "\<And>i. i\<in>Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i"
  1.2308 +    by (force simp: mem_box)
  1.2309 +  { fix f
  1.2310 +    have "\<lbrakk>finite f;
  1.2311 +           \<And>s. s\<in>f \<Longrightarrow> P s;
  1.2312 +           \<And>s. s\<in>f \<Longrightarrow> \<exists>a b. s = cbox a b;
  1.2313 +           \<And>s t. s\<in>f \<Longrightarrow> t\<in>f \<Longrightarrow> s \<noteq> t \<Longrightarrow> interior s \<inter> interior t = {}\<rbrakk> \<Longrightarrow> P (\<Union>f)"
  1.2314 +    proof (induct f rule: finite_induct)
  1.2315 +      case empty
  1.2316 +      show ?case
  1.2317 +        using assms(1) by auto
  1.2318 +    next
  1.2319 +      case (insert x f)
  1.2320 +      show ?case
  1.2321 +        unfolding Union_insert
  1.2322 +        apply (rule assms(2)[rule_format])
  1.2323 +        using inter_interior_unions_intervals [of f "interior x"]
  1.2324 +        apply (auto simp: insert)
  1.2325 +        by (metis IntI empty_iff insert.hyps(2) insert.prems(3) insert_iff)
  1.2326 +    qed
  1.2327 +  } note UN_cases = this
  1.2328 +  let ?A = "{cbox c d | c d::'a. \<forall>i\<in>Basis. (c\<bullet>i = a\<bullet>i) \<and> (d\<bullet>i = (a\<bullet>i + b\<bullet>i) / 2) \<or>
  1.2329 +    (c\<bullet>i = (a\<bullet>i + b\<bullet>i) / 2) \<and> (d\<bullet>i = b\<bullet>i)}"
  1.2330 +  let ?PP = "\<lambda>c d. \<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> c\<bullet>i \<le> d\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i \<and> 2 * (d\<bullet>i - c\<bullet>i) \<le> b\<bullet>i - a\<bullet>i"
  1.2331 +  {
  1.2332 +    presume "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d) \<Longrightarrow> False"
  1.2333 +    then show thesis
  1.2334 +      unfolding atomize_not not_all
  1.2335 +      by (blast intro: that)
  1.2336 +  }
  1.2337 +  assume as: "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d)"
  1.2338 +  have "P (\<Union>?A)"
  1.2339 +  proof (rule UN_cases)
  1.2340 +    let ?B = "(\<lambda>s. cbox (\<Sum>i\<in>Basis. (if i \<in> s then a\<bullet>i else (a\<bullet>i + b\<bullet>i) / 2) *\<^sub>R i::'a)
  1.2341 +      (\<Sum>i\<in>Basis. (if i \<in> s then (a\<bullet>i + b\<bullet>i) / 2 else b\<bullet>i) *\<^sub>R i)) ` {s. s \<subseteq> Basis}"
  1.2342 +    have "?A \<subseteq> ?B"
  1.2343 +    proof
  1.2344 +      fix x
  1.2345 +      assume "x \<in> ?A"
  1.2346 +      then obtain c d
  1.2347 +        where x:  "x = cbox c d"
  1.2348 +                  "\<And>i. i \<in> Basis \<Longrightarrow>
  1.2349 +                        c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  1.2350 +                        c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i" by blast
  1.2351 +      show "x \<in> ?B"
  1.2352 +        unfolding image_iff x
  1.2353 +        apply (rule_tac x="{i. i\<in>Basis \<and> c\<bullet>i = a\<bullet>i}" in bexI)
  1.2354 +        apply (rule arg_cong2 [where f = cbox])
  1.2355 +        using x(2) ab
  1.2356 +        apply (auto simp add: euclidean_eq_iff[where 'a='a])
  1.2357 +        by fastforce
  1.2358 +    qed
  1.2359 +    then show "finite ?A"
  1.2360 +      by (rule finite_subset) auto
  1.2361 +  next
  1.2362 +    fix s
  1.2363 +    assume "s \<in> ?A"
  1.2364 +    then obtain c d
  1.2365 +      where s: "s = cbox c d"
  1.2366 +               "\<And>i. i \<in> Basis \<Longrightarrow>
  1.2367 +                     c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  1.2368 +                     c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  1.2369 +      by blast
  1.2370 +    show "P s"
  1.2371 +      unfolding s
  1.2372 +      apply (rule as[rule_format])
  1.2373 +      using ab s(2) by force
  1.2374 +    show "\<exists>a b. s = cbox a b"
  1.2375 +      unfolding s by auto
  1.2376 +    fix t
  1.2377 +    assume "t \<in> ?A"
  1.2378 +    then obtain e f where t:
  1.2379 +      "t = cbox e f"
  1.2380 +      "\<And>i. i \<in> Basis \<Longrightarrow>
  1.2381 +        e \<bullet> i = a \<bullet> i \<and> f \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  1.2382 +        e \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> f \<bullet> i = b \<bullet> i"
  1.2383 +      by blast
  1.2384 +    assume "s \<noteq> t"
  1.2385 +    then have "\<not> (c = e \<and> d = f)"
  1.2386 +      unfolding s t by auto
  1.2387 +    then obtain i where "c\<bullet>i \<noteq> e\<bullet>i \<or> d\<bullet>i \<noteq> f\<bullet>i" and i': "i \<in> Basis"
  1.2388 +      unfolding euclidean_eq_iff[where 'a='a] by auto
  1.2389 +    then have i: "c\<bullet>i \<noteq> e\<bullet>i" "d\<bullet>i \<noteq> f\<bullet>i"
  1.2390 +      using s(2) t(2) apply fastforce
  1.2391 +      using t(2)[OF i'] \<open>c \<bullet> i \<noteq> e \<bullet> i \<or> d \<bullet> i \<noteq> f \<bullet> i\<close> i' s(2) t(2) by fastforce
  1.2392 +    have *: "\<And>s t. (\<And>a. a \<in> s \<Longrightarrow> a \<in> t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}"
  1.2393 +      by auto
  1.2394 +    show "interior s \<inter> interior t = {}"
  1.2395 +      unfolding s t interior_cbox
  1.2396 +    proof (rule *)
  1.2397 +      fix x
  1.2398 +      assume "x \<in> box c d" "x \<in> box e f"
  1.2399 +      then have x: "c\<bullet>i < d\<bullet>i" "e\<bullet>i < f\<bullet>i" "c\<bullet>i < f\<bullet>i" "e\<bullet>i < d\<bullet>i"
  1.2400 +        unfolding mem_box using i'
  1.2401 +        by force+
  1.2402 +      show False  using s(2)[OF i']
  1.2403 +      proof safe
  1.2404 +        assume as: "c \<bullet> i = a \<bullet> i" "d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2"
  1.2405 +        show False
  1.2406 +          using t(2)[OF i'] and i x unfolding as by (fastforce simp add:field_simps)
  1.2407 +      next
  1.2408 +        assume as: "c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2" "d \<bullet> i = b \<bullet> i"
  1.2409 +        show False
  1.2410 +          using t(2)[OF i'] and i x unfolding as by(fastforce simp add:field_simps)
  1.2411 +      qed
  1.2412 +    qed
  1.2413 +  qed
  1.2414 +  also have "\<Union>?A = cbox a b"
  1.2415 +  proof (rule set_eqI,rule)
  1.2416 +    fix x
  1.2417 +    assume "x \<in> \<Union>?A"
  1.2418 +    then obtain c d where x:
  1.2419 +      "x \<in> cbox c d"
  1.2420 +      "\<And>i. i \<in> Basis \<Longrightarrow>
  1.2421 +        c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  1.2422 +        c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  1.2423 +      by blast
  1.2424 +    show "x\<in>cbox a b"
  1.2425 +      unfolding mem_box
  1.2426 +    proof safe
  1.2427 +      fix i :: 'a
  1.2428 +      assume i: "i \<in> Basis"
  1.2429 +      then show "a \<bullet> i \<le> x \<bullet> i" "x \<bullet> i \<le> b \<bullet> i"
  1.2430 +        using x(2)[OF i] x(1)[unfolded mem_box,THEN bspec, OF i] by auto
  1.2431 +    qed
  1.2432 +  next
  1.2433 +    fix x
  1.2434 +    assume x: "x \<in> cbox a b"
  1.2435 +    have "\<forall>i\<in>Basis.
  1.2436 +      \<exists>c d. (c = a\<bullet>i \<and> d = (a\<bullet>i + b\<bullet>i) / 2 \<or> c = (a\<bullet>i + b\<bullet>i) / 2 \<and> d = b\<bullet>i) \<and> c\<le>x\<bullet>i \<and> x\<bullet>i \<le> d"
  1.2437 +      (is "\<forall>i\<in>Basis. \<exists>c d. ?P i c d")
  1.2438 +      unfolding mem_box
  1.2439 +    proof
  1.2440 +      fix i :: 'a
  1.2441 +      assume i: "i \<in> Basis"
  1.2442 +      have "?P i (a\<bullet>i) ((a \<bullet> i + b \<bullet> i) / 2) \<or> ?P i ((a \<bullet> i + b \<bullet> i) / 2) (b\<bullet>i)"
  1.2443 +        using x[unfolded mem_box,THEN bspec, OF i] by auto
  1.2444 +      then show "\<exists>c d. ?P i c d"
  1.2445 +        by blast
  1.2446 +    qed
  1.2447 +    then show "x\<in>\<Union>?A"
  1.2448 +      unfolding Union_iff Bex_def mem_Collect_eq choice_Basis_iff
  1.2449 +      apply auto
  1.2450 +      apply (rule_tac x="cbox xa xaa" in exI)
  1.2451 +      unfolding mem_box
  1.2452 +      apply auto
  1.2453 +      done
  1.2454 +  qed
  1.2455 +  finally show False
  1.2456 +    using assms by auto
  1.2457 +qed
  1.2458 +
  1.2459 +lemma interval_bisection:
  1.2460 +  fixes type :: "'a::euclidean_space"
  1.2461 +  assumes "P {}"
  1.2462 +    and "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))"
  1.2463 +    and "\<not> P (cbox a (b::'a))"
  1.2464 +  obtains x where "x \<in> cbox a b"
  1.2465 +    and "\<forall>e>0. \<exists>c d. x \<in> cbox c d \<and> cbox c d \<subseteq> ball x e \<and> cbox c d \<subseteq> cbox a b \<and> \<not> P (cbox c d)"
  1.2466 +proof -
  1.2467 +  have "\<forall>x. \<exists>y. \<not> P (cbox (fst x) (snd x)) \<longrightarrow> (\<not> P (cbox (fst y) (snd y)) \<and>
  1.2468 +    (\<forall>i\<in>Basis. fst x\<bullet>i \<le> fst y\<bullet>i \<and> fst y\<bullet>i \<le> snd y\<bullet>i \<and> snd y\<bullet>i \<le> snd x\<bullet>i \<and>
  1.2469 +       2 * (snd y\<bullet>i - fst y\<bullet>i) \<le> snd x\<bullet>i - fst x\<bullet>i))" (is "\<forall>x. ?P x")
  1.2470 +  proof
  1.2471 +    show "?P x" for x
  1.2472 +    proof (cases "P (cbox (fst x) (snd x))")
  1.2473 +      case True
  1.2474 +      then show ?thesis by auto
  1.2475 +    next
  1.2476 +      case as: False
  1.2477 +      obtain c d where "\<not> P (cbox c d)"
  1.2478 +        "\<forall>i\<in>Basis.
  1.2479 +           fst x \<bullet> i \<le> c \<bullet> i \<and>
  1.2480 +           c \<bullet> i \<le> d \<bullet> i \<and>
  1.2481 +           d \<bullet> i \<le> snd x \<bullet> i \<and>
  1.2482 +           2 * (d \<bullet> i - c \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i"
  1.2483 +        by (rule interval_bisection_step[of P, OF assms(1-2) as])
  1.2484 +      then show ?thesis
  1.2485 +        apply -
  1.2486 +        apply (rule_tac x="(c,d)" in exI)
  1.2487 +        apply auto
  1.2488 +        done
  1.2489 +    qed
  1.2490 +  qed
  1.2491 +  then obtain f where f:
  1.2492 +    "\<forall>x.
  1.2493 +      \<not> P (cbox (fst x) (snd x)) \<longrightarrow>
  1.2494 +      \<not> P (cbox (fst (f x)) (snd (f x))) \<and>
  1.2495 +        (\<forall>i\<in>Basis.
  1.2496 +            fst x \<bullet> i \<le> fst (f x) \<bullet> i \<and>
  1.2497 +            fst (f x) \<bullet> i \<le> snd (f x) \<bullet> i \<and>
  1.2498 +            snd (f x) \<bullet> i \<le> snd x \<bullet> i \<and>
  1.2499 +            2 * (snd (f x) \<bullet> i - fst (f x) \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i)"
  1.2500 +    apply -
  1.2501 +    apply (drule choice)
  1.2502 +    apply blast
  1.2503 +    done
  1.2504 +  define AB A B where ab_def: "AB n = (f ^^ n) (a,b)" "A n = fst(AB n)" "B n = snd(AB n)" for n
  1.2505 +  have "A 0 = a" "B 0 = b" "\<And>n. \<not> P (cbox (A(Suc n)) (B(Suc n))) \<and>
  1.2506 +    (\<forall>i\<in>Basis. A(n)\<bullet>i \<le> A(Suc n)\<bullet>i \<and> A(Suc n)\<bullet>i \<le> B(Suc n)\<bullet>i \<and> B(Suc n)\<bullet>i \<le> B(n)\<bullet>i \<and>
  1.2507 +    2 * (B(Suc n)\<bullet>i - A(Suc n)\<bullet>i) \<le> B(n)\<bullet>i - A(n)\<bullet>i)" (is "\<And>n. ?P n")
  1.2508 +  proof -
  1.2509 +    show "A 0 = a" "B 0 = b"
  1.2510 +      unfolding ab_def by auto
  1.2511 +    note S = ab_def funpow.simps o_def id_apply
  1.2512 +    show "?P n" for n
  1.2513 +    proof (induct n)
  1.2514 +      case 0
  1.2515 +      then show ?case
  1.2516 +        unfolding S
  1.2517 +        apply (rule f[rule_format]) using assms(3)
  1.2518 +        apply auto
  1.2519 +        done
  1.2520 +    next
  1.2521 +      case (Suc n)
  1.2522 +      show ?case
  1.2523 +        unfolding S
  1.2524 +        apply (rule f[rule_format])
  1.2525 +        using Suc
  1.2526 +        unfolding S
  1.2527 +        apply auto
  1.2528 +        done
  1.2529 +    qed
  1.2530 +  qed
  1.2531 +  note AB = this(1-2) conjunctD2[OF this(3),rule_format]
  1.2532 +
  1.2533 +  have interv: "\<exists>n. \<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e"
  1.2534 +    if e: "0 < e" for e
  1.2535 +  proof -
  1.2536 +    obtain n where n: "(\<Sum>i\<in>Basis. b \<bullet> i - a \<bullet> i) / e < 2 ^ n"
  1.2537 +      using real_arch_pow[of 2 "(setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis) / e"] by auto
  1.2538 +    show ?thesis
  1.2539 +    proof (rule exI [where x=n], clarify)
  1.2540 +      fix x y
  1.2541 +      assume xy: "x\<in>cbox (A n) (B n)" "y\<in>cbox (A n) (B n)"
  1.2542 +      have "dist x y \<le> setsum (\<lambda>i. \<bar>(x - y)\<bullet>i\<bar>) Basis"
  1.2543 +        unfolding dist_norm by(rule norm_le_l1)
  1.2544 +      also have "\<dots> \<le> setsum (\<lambda>i. B n\<bullet>i - A n\<bullet>i) Basis"
  1.2545 +      proof (rule setsum_mono)
  1.2546 +        fix i :: 'a
  1.2547 +        assume i: "i \<in> Basis"
  1.2548 +        show "\<bar>(x - y) \<bullet> i\<bar> \<le> B n \<bullet> i - A n \<bullet> i"
  1.2549 +          using xy[unfolded mem_box,THEN bspec, OF i]
  1.2550 +          by (auto simp: inner_diff_left)
  1.2551 +      qed
  1.2552 +      also have "\<dots> \<le> setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis / 2^n"
  1.2553 +        unfolding setsum_divide_distrib
  1.2554 +      proof (rule setsum_mono)
  1.2555 +        show "B n \<bullet> i - A n \<bullet> i \<le> (b \<bullet> i - a \<bullet> i) / 2 ^ n" if i: "i \<in> Basis" for i
  1.2556 +        proof (induct n)
  1.2557 +          case 0
  1.2558 +          then show ?case
  1.2559 +            unfolding AB by auto
  1.2560 +        next
  1.2561 +          case (Suc n)
  1.2562 +          have "B (Suc n) \<bullet> i - A (Suc n) \<bullet> i \<le> (B n \<bullet> i - A n \<bullet> i) / 2"
  1.2563 +            using AB(4)[of i n] using i by auto
  1.2564 +          also have "\<dots> \<le> (b \<bullet> i - a \<bullet> i) / 2 ^ Suc n"
  1.2565 +            using Suc by (auto simp add: field_simps)
  1.2566 +          finally show ?case .
  1.2567 +        qed
  1.2568 +      qed
  1.2569 +      also have "\<dots> < e"
  1.2570 +        using n using e by (auto simp add: field_simps)
  1.2571 +      finally show "dist x y < e" .
  1.2572 +    qed
  1.2573 +  qed
  1.2574 +  {
  1.2575 +    fix n m :: nat
  1.2576 +    assume "m \<le> n" then have "cbox (A n) (B n) \<subseteq> cbox (A m) (B m)"
  1.2577 +    proof (induction rule: inc_induct)
  1.2578 +      case (step i)
  1.2579 +      show ?case
  1.2580 +        using AB(4) by (intro order_trans[OF step.IH] subset_box_imp) auto
  1.2581 +    qed simp
  1.2582 +  } note ABsubset = this
  1.2583 +  have "\<exists>a. \<forall>n. a\<in> cbox (A n) (B n)"
  1.2584 +    by (rule decreasing_closed_nest[rule_format,OF closed_cbox _ ABsubset interv])
  1.2585 +      (metis nat.exhaust AB(1-3) assms(1,3))
  1.2586 +  then obtain x0 where x0: "\<And>n. x0 \<in> cbox (A n) (B n)"
  1.2587 +    by blast
  1.2588 +  show thesis
  1.2589 +  proof (rule that[rule_format, of x0])
  1.2590 +    show "x0\<in>cbox a b"
  1.2591 +      using x0[of 0] unfolding AB .
  1.2592 +    fix e :: real
  1.2593 +    assume "e > 0"
  1.2594 +    from interv[OF this] obtain n
  1.2595 +      where n: "\<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e" ..
  1.2596 +    have "\<not> P (cbox (A n) (B n))"
  1.2597 +      apply (cases "0 < n")
  1.2598 +      using AB(3)[of "n - 1"] assms(3) AB(1-2)
  1.2599 +      apply auto
  1.2600 +      done
  1.2601 +    moreover have "cbox (A n) (B n) \<subseteq> ball x0 e"
  1.2602 +      using n using x0[of n] by auto
  1.2603 +    moreover have "cbox (A n) (B n) \<subseteq> cbox a b"
  1.2604 +      unfolding AB(1-2)[symmetric] by (rule ABsubset) auto
  1.2605 +    ultimately show "\<exists>c d. x0 \<in> cbox c d \<and> cbox c d \<subseteq> ball x0 e \<and> cbox c d \<subseteq> cbox a b \<and> \<not> P (cbox c d)"
  1.2606 +      apply (rule_tac x="A n" in exI)
  1.2607 +      apply (rule_tac x="B n" in exI)
  1.2608 +      apply (auto simp: x0)
  1.2609 +      done
  1.2610 +  qed
  1.2611 +qed
  1.2612 +
  1.2613 +
  1.2614 +subsection \<open>Cousin's lemma.\<close>
  1.2615 +
  1.2616 +lemma fine_division_exists:
  1.2617 +  fixes a b :: "'a::euclidean_space"
  1.2618 +  assumes "gauge g"
  1.2619 +  obtains p where "p tagged_division_of (cbox a b)" "g fine p"
  1.2620 +proof -
  1.2621 +  presume "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p) \<Longrightarrow> False"
  1.2622 +  then obtain p where "p tagged_division_of (cbox a b)" "g fine p"
  1.2623 +    by blast
  1.2624 +  then show thesis ..
  1.2625 +next
  1.2626 +  assume as: "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p)"
  1.2627 +  obtain x where x:
  1.2628 +      "x \<in> (cbox a b)"
  1.2629 +      "\<And>e. 0 < e \<Longrightarrow>
  1.2630 +        \<exists>c d.
  1.2631 +          x \<in> cbox c d \<and>
  1.2632 +          cbox c d \<subseteq> ball x e \<and>
  1.2633 +          cbox c d \<subseteq> (cbox a b) \<and>
  1.2634 +          \<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  1.2635 +    apply (rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p", OF _ _ as])
  1.2636 +    apply (simp add: fine_def)
  1.2637 +    apply (metis tagged_division_union fine_union)
  1.2638 +    apply (auto simp: )
  1.2639 +    done
  1.2640 +  obtain e where e: "e > 0" "ball x e \<subseteq> g x"
  1.2641 +    using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
  1.2642 +  from x(2)[OF e(1)]
  1.2643 +  obtain c d where c_d: "x \<in> cbox c d"
  1.2644 +                        "cbox c d \<subseteq> ball x e"
  1.2645 +                        "cbox c d \<subseteq> cbox a b"
  1.2646 +                        "\<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  1.2647 +    by blast
  1.2648 +  have "g fine {(x, cbox c d)}"
  1.2649 +    unfolding fine_def using e using c_d(2) by auto
  1.2650 +  then show False
  1.2651 +    using tagged_division_of_self[OF c_d(1)] using c_d by auto
  1.2652 +qed
  1.2653 +
  1.2654 +lemma fine_division_exists_real:
  1.2655 +  fixes a b :: real
  1.2656 +  assumes "gauge g"
  1.2657 +  obtains p where "p tagged_division_of {a .. b}" "g fine p"
  1.2658 +  by (metis assms box_real(2) fine_division_exists)
  1.2659 +
  1.2660 +subsection \<open>Basic theorems about integrals.\<close>
  1.2661 +
  1.2662 +lemma has_integral_unique:
  1.2663 +  fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1.2664 +  assumes "(f has_integral k1) i"
  1.2665 +    and "(f has_integral k2) i"
  1.2666 +  shows "k1 = k2"
  1.2667 +proof (rule ccontr)
  1.2668 +  let ?e = "norm (k1 - k2) / 2"
  1.2669 +  assume as: "k1 \<noteq> k2"
  1.2670 +  then have e: "?e > 0"
  1.2671 +    by auto
  1.2672 +  have lem: False
  1.2673 +    if f_k1: "(f has_integral k1) (cbox a b)"
  1.2674 +    and f_k2: "(f has_integral k2) (cbox a b)"
  1.2675 +    and "k1 \<noteq> k2"
  1.2676 +    for f :: "'n \<Rightarrow> 'a" and a b k1 k2
  1.2677 +  proof -
  1.2678 +    let ?e = "norm (k1 - k2) / 2"
  1.2679 +    from \<open>k1 \<noteq> k2\<close> have e: "?e > 0" by auto
  1.2680 +    obtain d1 where d1:
  1.2681 +        "gauge d1"
  1.2682 +        "\<And>p. p tagged_division_of cbox a b \<Longrightarrow>
  1.2683 +          d1 fine p \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k1) < norm (k1 - k2) / 2"
  1.2684 +      by (rule has_integralD[OF f_k1 e]) blast
  1.2685 +    obtain d2 where d2:
  1.2686 +        "gauge d2"
  1.2687 +        "\<And>p. p tagged_division_of cbox a b \<Longrightarrow>
  1.2688 +          d2 fine p \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k2) < norm (k1 - k2) / 2"
  1.2689 +      by (rule has_integralD[OF f_k2 e]) blast
  1.2690 +    obtain p where p:
  1.2691 +        "p tagged_division_of cbox a b"
  1.2692 +        "(\<lambda>x. d1 x \<inter> d2 x) fine p"
  1.2693 +      by (rule fine_division_exists[OF gauge_inter[OF d1(1) d2(1)]])
  1.2694 +    let ?c = "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)"
  1.2695 +    have "norm (k1 - k2) \<le> norm (?c - k2) + norm (?c - k1)"
  1.2696 +      using norm_triangle_ineq4[of "k1 - ?c" "k2 - ?c"]
  1.2697 +      by (auto simp add:algebra_simps norm_minus_commute)
  1.2698 +    also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
  1.2699 +      apply (rule add_strict_mono)
  1.2700 +      apply (rule_tac[!] d2(2) d1(2))
  1.2701 +      using p unfolding fine_def
  1.2702 +      apply auto
  1.2703 +      done
  1.2704 +    finally show False by auto
  1.2705 +  qed
  1.2706 +  {
  1.2707 +    presume "\<not> (\<exists>a b. i = cbox a b) \<Longrightarrow> False"
  1.2708 +    then show False
  1.2709 +      using as assms lem by blast
  1.2710 +  }
  1.2711 +  assume as: "\<not> (\<exists>a b. i = cbox a b)"
  1.2712 +  obtain B1 where B1:
  1.2713 +      "0 < B1"
  1.2714 +      "\<And>a b. ball 0 B1 \<subseteq> cbox a b \<Longrightarrow>
  1.2715 +        \<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b) \<and>
  1.2716 +          norm (z - k1) < norm (k1 - k2) / 2"
  1.2717 +    by (rule has_integral_altD[OF assms(1) as,OF e]) blast
  1.2718 +  obtain B2 where B2:
  1.2719 +      "0 < B2"
  1.2720 +      "\<And>a b. ball 0 B2 \<subseteq> cbox a b \<Longrightarrow>
  1.2721 +        \<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b) \<and>
  1.2722 +          norm (z - k2) < norm (k1 - k2) / 2"
  1.2723 +    by (rule has_integral_altD[OF assms(2) as,OF e]) blast
  1.2724 +  have "\<exists>a b::'n. ball 0 B1 \<union> ball 0 B2 \<subseteq> cbox a b"
  1.2725 +    apply (rule bounded_subset_cbox)
  1.2726 +    using bounded_Un bounded_ball
  1.2727 +    apply auto
  1.2728 +    done
  1.2729 +  then obtain a b :: 'n where ab: "ball 0 B1 \<subseteq> cbox a b" "ball 0 B2 \<subseteq> cbox a b"
  1.2730 +    by blast
  1.2731 +  obtain w where w:
  1.2732 +    "((\<lambda>x. if x \<in> i then f x else 0) has_integral w) (cbox a b)"
  1.2733 +    "norm (w - k1) < norm (k1 - k2) / 2"
  1.2734 +    using B1(2)[OF ab(1)] by blast
  1.2735 +  obtain z where z:
  1.2736 +    "((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b)"
  1.2737 +    "norm (z - k2) < norm (k1 - k2) / 2"
  1.2738 +    using B2(2)[OF ab(2)] by blast
  1.2739 +  have "z = w"
  1.2740 +    using lem[OF w(1) z(1)] by auto
  1.2741 +  then have "norm (k1 - k2) \<le> norm (z - k2) + norm (w - k1)"
  1.2742 +    using norm_triangle_ineq4 [of "k1 - w" "k2 - z"]
  1.2743 +    by (auto simp add: norm_minus_commute)
  1.2744 +  also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
  1.2745 +    apply (rule add_strict_mono)
  1.2746 +    apply (rule_tac[!] z(2) w(2))
  1.2747 +    done
  1.2748 +  finally show False by auto
  1.2749 +qed
  1.2750 +
  1.2751 +lemma integral_unique [intro]: "(f has_integral y) k \<Longrightarrow> integral k f = y"
  1.2752 +  unfolding integral_def
  1.2753 +  by (rule some_equality) (auto intro: has_integral_unique)
  1.2754 +
  1.2755 +lemma eq_integralD: "integral k f = y \<Longrightarrow> (f has_integral y) k \<or> ~ f integrable_on k \<and> y=0"
  1.2756 +  unfolding integral_def integrable_on_def
  1.2757 +  apply (erule subst)
  1.2758 +  apply (rule someI_ex)
  1.2759 +  by blast
  1.2760 +
  1.2761 +lemma has_integral_is_0:
  1.2762 +  fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1.2763 +  assumes "\<forall>x\<in>s. f x = 0"
  1.2764 +  shows "(f has_integral 0) s"
  1.2765 +proof -
  1.2766 +  have lem: "\<And>a b. \<And>f::'n \<Rightarrow> 'a.
  1.2767 +    (\<forall>x\<in>cbox a b. f(x) = 0) \<Longrightarrow> (f has_integral 0) (cbox a b)"
  1.2768 +    unfolding has_integral
  1.2769 +  proof clarify
  1.2770 +    fix a b e
  1.2771 +    fix f :: "'n \<Rightarrow> 'a"
  1.2772 +    assume as: "\<forall>x\<in>cbox a b. f x = 0" "0 < (e::real)"
  1.2773 +    have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  1.2774 +      if p: "p tagged_division_of cbox a b" for p
  1.2775 +    proof -
  1.2776 +      have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) = 0"
  1.2777 +      proof (rule setsum.neutral, rule)
  1.2778 +        fix x
  1.2779 +        assume x: "x \<in> p"
  1.2780 +        have "f (fst x) = 0"
  1.2781 +          using tagged_division_ofD(2-3)[OF p, of "fst x" "snd x"] using as x by auto
  1.2782 +        then show "(\<lambda>(x, k). content k *\<^sub>R f x) x = 0"
  1.2783 +          apply (subst surjective_pairing[of x])
  1.2784 +          unfolding split_conv
  1.2785 +          apply auto
  1.2786 +          done
  1.2787 +      qed
  1.2788 +      then show ?thesis
  1.2789 +        using as by auto
  1.2790 +    qed
  1.2791 +    then show "\<exists>d. gauge d \<and>
  1.2792 +        (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e)"
  1.2793 +      by auto
  1.2794 +  qed
  1.2795 +  {
  1.2796 +    presume "\<not> (\<exists>a b. s = cbox a b) \<Longrightarrow> ?thesis"
  1.2797 +    with assms lem show ?thesis
  1.2798 +      by blast
  1.2799 +  }
  1.2800 +  have *: "(\<lambda>x. if x \<in> s then f x else 0) = (\<lambda>x. 0)"
  1.2801 +    apply (rule ext)
  1.2802 +    using assms
  1.2803 +    apply auto
  1.2804 +    done
  1.2805 +  assume "\<not> (\<exists>a b. s = cbox a b)"
  1.2806 +  then show ?thesis
  1.2807 +    using lem
  1.2808 +    by (subst has_integral_alt) (force simp add: *)
  1.2809 +qed
  1.2810 +
  1.2811 +lemma has_integral_0[simp]: "((\<lambda>x::'n::euclidean_space. 0) has_integral 0) s"
  1.2812 +  by (rule has_integral_is_0) auto
  1.2813 +
  1.2814 +lemma has_integral_0_eq[simp]: "((\<lambda>x. 0) has_integral i) s \<longleftrightarrow> i = 0"
  1.2815 +  using has_integral_unique[OF has_integral_0] by auto
  1.2816 +
  1.2817 +lemma has_integral_linear:
  1.2818 +  fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1.2819 +  assumes "(f has_integral y) s"
  1.2820 +    and "bounded_linear h"
  1.2821 +  shows "((h \<circ> f) has_integral ((h y))) s"
  1.2822 +proof -
  1.2823 +  interpret bounded_linear h
  1.2824 +    using assms(2) .
  1.2825 +  from pos_bounded obtain B where B: "0 < B" "\<And>x. norm (h x) \<le> norm x * B"
  1.2826 +    by blast
  1.2827 +  have lem: "\<And>(f :: 'n \<Rightarrow> 'a) y a b.
  1.2828 +    (f has_integral y) (cbox a b) \<Longrightarrow> ((h \<circ> f) has_integral h y) (cbox a b)"
  1.2829 +    unfolding has_integral
  1.2830 +  proof (clarify, goal_cases)
  1.2831 +    case prems: (1 f y a b e)
  1.2832 +    from pos_bounded
  1.2833 +    obtain B where B: "0 < B" "\<And>x. norm (h x) \<le> norm x * B"
  1.2834 +      by blast
  1.2835 +    have "e / B > 0" using prems(2) B by simp
  1.2836 +    then obtain g
  1.2837 +      where g: "gauge g"
  1.2838 +               "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> g fine p \<Longrightarrow>
  1.2839 +                    norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - y) < e / B"
  1.2840 +        using prems(1) by auto
  1.2841 +    {
  1.2842 +      fix p
  1.2843 +      assume as: "p tagged_division_of (cbox a b)" "g fine p"
  1.2844 +      have hc: "\<And>x k. h ((\<lambda>(x, k). content k *\<^sub>R f x) x) = (\<lambda>(x, k). h (content k *\<^sub>R f x)) x"
  1.2845 +        by auto
  1.2846 +      then have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = setsum (h \<circ> (\<lambda>(x, k). content k *\<^sub>R f x)) p"
  1.2847 +        unfolding o_def unfolding scaleR[symmetric] hc by simp
  1.2848 +      also have "\<dots> = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)"
  1.2849 +        using setsum[of "\<lambda>(x,k). content k *\<^sub>R f x" p] using as by auto
  1.2850 +      finally have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" .
  1.2851 +      then have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) - h y) < e"
  1.2852 +        apply (simp add: diff[symmetric])
  1.2853 +        apply (rule le_less_trans[OF B(2)])
  1.2854 +        using g(2)[OF as] B(1)
  1.2855 +        apply (auto simp add: field_simps)
  1.2856 +        done
  1.2857 +    }
  1.2858 +    with g show ?case
  1.2859 +      by (rule_tac x=g in exI) auto
  1.2860 +  qed
  1.2861 +  {
  1.2862 +    presume "\<not> (\<exists>a b. s = cbox a b) \<Longrightarrow> ?thesis"
  1.2863 +    then show ?thesis
  1.2864 +      using assms(1) lem by blast
  1.2865 +  }
  1.2866 +  assume as: "\<not> (\<exists>a b. s = cbox a b)"
  1.2867 +  then show ?thesis
  1.2868 +  proof (subst has_integral_alt, clarsimp)
  1.2869 +    fix e :: real
  1.2870 +    assume e: "e > 0"
  1.2871 +    have *: "0 < e/B" using e B(1) by simp
  1.2872 +    obtain M where M:
  1.2873 +      "M > 0"
  1.2874 +      "\<And>a b. ball 0 M \<subseteq> cbox a b \<Longrightarrow>
  1.2875 +        \<exists>z. ((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e / B"
  1.2876 +      using has_integral_altD[OF assms(1) as *] by blast
  1.2877 +    show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  1.2878 +      (\<exists>z. ((\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) has_integral z) (cbox a b) \<and> norm (z - h y) < e)"
  1.2879 +    proof (rule_tac x=M in exI, clarsimp simp add: M, goal_cases)
  1.2880 +      case prems: (1 a b)
  1.2881 +      obtain z where z:
  1.2882 +        "((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b)"
  1.2883 +        "norm (z - y) < e / B"
  1.2884 +        using M(2)[OF prems(1)] by blast
  1.2885 +      have *: "(\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) = h \<circ> (\<lambda>x. if x \<in> s then f x else 0)"
  1.2886 +        using zero by auto
  1.2887 +      show ?case
  1.2888 +        apply (rule_tac x="h z" in exI)
  1.2889 +        apply (simp add: * lem z(1))
  1.2890 +        apply (metis B diff le_less_trans pos_less_divide_eq z(2))
  1.2891 +        done
  1.2892 +    qed
  1.2893 +  qed
  1.2894 +qed
  1.2895 +
  1.2896 +lemma has_integral_scaleR_left:
  1.2897 +  "(f has_integral y) s \<Longrightarrow> ((\<lambda>x. f x *\<^sub>R c) has_integral (y *\<^sub>R c)) s"
  1.2898 +  using has_integral_linear[OF _ bounded_linear_scaleR_left] by (simp add: comp_def)
  1.2899 +
  1.2900 +lemma has_integral_mult_left:
  1.2901 +  fixes c :: "_ :: real_normed_algebra"
  1.2902 +  shows "(f has_integral y) s \<Longrightarrow> ((\<lambda>x. f x * c) has_integral (y * c)) s"
  1.2903 +  using has_integral_linear[OF _ bounded_linear_mult_left] by (simp add: comp_def)
  1.2904 +
  1.2905 +text\<open>The case analysis eliminates the condition @{term "f integrable_on s"} at the cost
  1.2906 +     of the type class constraint \<open>division_ring\<close>\<close>
  1.2907 +corollary integral_mult_left [simp]:
  1.2908 +  fixes c:: "'a::{real_normed_algebra,division_ring}"
  1.2909 +  shows "integral s (\<lambda>x. f x * c) = integral s f * c"
  1.2910 +proof (cases "f integrable_on s \<or> c = 0")
  1.2911 +  case True then show ?thesis
  1.2912 +    by (force intro: has_integral_mult_left)
  1.2913 +next
  1.2914 +  case False then have "~ (\<lambda>x. f x * c) integrable_on s"
  1.2915 +    using has_integral_mult_left [of "(\<lambda>x. f x * c)" _ s "inverse c"]
  1.2916 +    by (force simp add: mult.assoc)
  1.2917 +  with False show ?thesis by (simp add: not_integrable_integral)
  1.2918 +qed
  1.2919 +
  1.2920 +corollary integral_mult_right [simp]:
  1.2921 +  fixes c:: "'a::{real_normed_field}"
  1.2922 +  shows "integral s (\<lambda>x. c * f x) = c * integral s f"
  1.2923 +by (simp add: mult.commute [of c])
  1.2924 +
  1.2925 +corollary integral_divide [simp]:
  1.2926 +  fixes z :: "'a::real_normed_field"
  1.2927 +  shows "integral S (\<lambda>x. f x / z) = integral S (\<lambda>x. f x) / z"
  1.2928 +using integral_mult_left [of S f "inverse z"]
  1.2929 +  by (simp add: divide_inverse_commute)
  1.2930 +
  1.2931 +lemma has_integral_mult_right:
  1.2932 +  fixes c :: "'a :: real_normed_algebra"
  1.2933 +  shows "(f has_integral y) i \<Longrightarrow> ((\<lambda>x. c * f x) has_integral (c * y)) i"
  1.2934 +  using has_integral_linear[OF _ bounded_linear_mult_right] by (simp add: comp_def)
  1.2935 +
  1.2936 +lemma has_integral_cmul: "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_integral (c *\<^sub>R k)) s"
  1.2937 +  unfolding o_def[symmetric]
  1.2938 +  by (metis has_integral_linear bounded_linear_scaleR_right)
  1.2939 +
  1.2940 +lemma has_integral_cmult_real:
  1.2941 +  fixes c :: real
  1.2942 +  assumes "c \<noteq> 0 \<Longrightarrow> (f has_integral x) A"
  1.2943 +  shows "((\<lambda>x. c * f x) has_integral c * x) A"
  1.2944 +proof (cases "c = 0")
  1.2945 +  case True
  1.2946 +  then show ?thesis by simp
  1.2947 +next
  1.2948 +  case False
  1.2949 +  from has_integral_cmul[OF assms[OF this], of c] show ?thesis
  1.2950 +    unfolding real_scaleR_def .
  1.2951 +qed
  1.2952 +
  1.2953 +lemma has_integral_neg: "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. -(f x)) has_integral -k) s"
  1.2954 +  by (drule_tac c="-1" in has_integral_cmul) auto
  1.2955 +
  1.2956 +lemma has_integral_add:
  1.2957 +  fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1.2958 +  assumes "(f has_integral k) s"
  1.2959 +    and "(g has_integral l) s"
  1.2960 +  shows "((\<lambda>x. f x + g x) has_integral (k + l)) s"
  1.2961 +proof -
  1.2962 +  have lem: "((\<lambda>x. f x + g x) has_integral (k + l)) (cbox a b)"
  1.2963 +    if f_k: "(f has_integral k) (cbox a b)"
  1.2964 +    and g_l: "(g has_integral l) (cbox a b)"
  1.2965 +    for f :: "'n \<Rightarrow> 'a" and g a b k l
  1.2966 +    unfolding has_integral
  1.2967 +  proof clarify
  1.2968 +    fix e :: real
  1.2969 +    assume e: "e > 0"
  1.2970 +    then have *: "e / 2 > 0"
  1.2971 +      by auto
  1.2972 +    obtain d1 where d1:
  1.2973 +      "gauge d1"
  1.2974 +      "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d1 fine p \<Longrightarrow>
  1.2975 +        norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k) < e / 2"
  1.2976 +      using has_integralD[OF f_k *] by blast
  1.2977 +    obtain d2 where d2:
  1.2978 +      "gauge d2"
  1.2979 +      "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d2 fine p \<Longrightarrow>
  1.2980 +        norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - l) < e / 2"
  1.2981 +      using has_integralD[OF g_l *] by blast
  1.2982 +    show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  1.2983 +              norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e)"
  1.2984 +    proof (rule exI [where x="\<lambda>x. (d1 x) \<inter> (d2 x)"], clarsimp simp add: gauge_inter[OF d1(1) d2(1)])
  1.2985 +      fix p
  1.2986 +      assume as: "p tagged_division_of (cbox a b)" "(\<lambda>x. d1 x \<inter> d2 x) fine p"
  1.2987 +      have *: "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) =
  1.2988 +        (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p. content k *\<^sub>R g x)"
  1.2989 +        unfolding scaleR_right_distrib setsum.distrib[of "\<lambda>(x,k). content k *\<^sub>R f x" "\<lambda>(x,k). content k *\<^sub>R g x" p,symmetric]
  1.2990 +        by (rule setsum.cong) auto
  1.2991 +      from as have fine: "d1 fine p" "d2 fine p"
  1.2992 +        unfolding fine_inter by auto
  1.2993 +      have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) =
  1.2994 +            norm (((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k) + ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - l))"
  1.2995 +        unfolding * by (auto simp add: algebra_simps)
  1.2996 +      also have "\<dots> < e/2 + e/2"
  1.2997 +        apply (rule le_less_trans[OF norm_triangle_ineq])
  1.2998 +        using as d1 d2 fine
  1.2999 +        apply (blast intro: add_strict_mono)
  1.3000 +        done
  1.3001 +      finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e"
  1.3002 +        by auto
  1.3003 +    qed
  1.3004 +  qed
  1.3005 +  {
  1.3006 +    presume "\<not> (\<exists>a b. s = cbox a b) \<Longrightarrow> ?thesis"
  1.3007 +    then show ?thesis
  1.3008 +      using assms lem by force
  1.3009 +  }
  1.3010 +  assume as: "\<not> (\<exists>a b. s = cbox a b)"
  1.3011 +  then show ?thesis
  1.3012 +  proof (subst has_integral_alt, clarsimp, goal_cases)
  1.3013 +    case (1 e)
  1.3014 +    then have *: "e / 2 > 0"
  1.3015 +      by auto
  1.3016 +    from has_integral_altD[OF assms(1) as *]
  1.3017 +    obtain B1 where B1:
  1.3018 +        "0 < B1"
  1.3019 +        "\<And>a b. ball 0 B1 \<subseteq> cbox a b \<Longrightarrow>
  1.3020 +          \<exists>z. ((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b) \<and> norm (z - k) < e / 2"
  1.3021 +      by blast
  1.3022 +    from has_integral_altD[OF assms(2) as *]
  1.3023 +    obtain B2 where B2:
  1.3024 +        "0 < B2"
  1.3025 +        "\<And>a b. ball 0 B2 \<subseteq> (cbox a b) \<Longrightarrow>
  1.3026 +          \<exists>z. ((\<lambda>x. if x \<in> s then g x else 0) has_integral z) (cbox a b) \<and> norm (z - l) < e / 2"
  1.3027 +      by blast
  1.3028 +    show ?case
  1.3029 +    proof (rule_tac x="max B1 B2" in exI, clarsimp simp add: max.strict_coboundedI1 B1)
  1.3030 +      fix a b
  1.3031 +      assume "ball 0 (max B1 B2) \<subseteq> cbox a (b::'n)"
  1.3032 +      then have *: "ball 0 B1 \<subseteq> cbox a (b::'n)" "ball 0 B2 \<subseteq> cbox a (b::'n)"
  1.3033 +        by auto
  1.3034 +      obtain w where w:
  1.3035 +        "((\<lambda>x. if x \<in> s then f x else 0) has_integral w) (cbox a b)"
  1.3036 +        "norm (w - k) < e / 2"
  1.3037 +        using B1(2)[OF *(1)] by blast
  1.3038 +      obtain z where z:
  1.3039 +        "((\<lambda>x. if x \<in> s then g x else 0) has_integral z) (cbox a b)"
  1.3040 +        "norm (z - l) < e / 2"
  1.3041 +        using B2(2)[OF *(2)] by blast
  1.3042 +      have *: "\<And>x. (if x \<in> s then f x + g x else 0) =
  1.3043 +        (if x \<in> s then f x else 0) + (if x \<in> s then g x else 0)"
  1.3044 +        by auto
  1.3045 +      show "\<exists>z. ((\<lambda>x. if x \<in> s then f x + g x else 0) has_integral z) (cbox a b) \<and> norm (z - (k + l)) < e"
  1.3046 +        apply (rule_tac x="w + z" in exI)
  1.3047 +        apply (simp add: lem[OF w(1) z(1), unfolded *[symmetric]])
  1.3048 +        using norm_triangle_ineq[of "w - k" "z - l"] w(2) z(2)
  1.3049 +        apply (auto simp add: field_simps)
  1.3050 +        done
  1.3051 +    qed
  1.3052 +  qed
  1.3053 +qed
  1.3054 +
  1.3055 +lemma has_integral_sub:
  1.3056 +  "(f has_integral k) s \<Longrightarrow> (g has_integral l) s \<Longrightarrow>
  1.3057 +    ((\<lambda>x. f x - g x) has_integral (k - l)) s"
  1.3058 +  using has_integral_add[OF _ has_integral_neg, of f k s g l]
  1.3059 +  by (auto simp: algebra_simps)
  1.3060 +
  1.3061 +lemma integral_0 [simp]:
  1.3062 +  "integral s (\<lambda>x::'n::euclidean_space. 0::'m::real_normed_vector) = 0"
  1.3063 +  by (rule integral_unique has_integral_0)+
  1.3064 +
  1.3065 +lemma integral_add: "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
  1.3066 +    integral s (\<lambda>x. f x + g x) = integral s f + integral s g"
  1.3067 +  by (rule integral_unique) (metis integrable_integral has_integral_add)
  1.3068 +
  1.3069 +lemma integral_cmul [simp]: "integral s (\<lambda>x. c *\<^sub>R f x) = c *\<^sub>R integral s f"
  1.3070 +proof (cases "f integrable_on s \<or> c = 0")
  1.3071 +  case True with has_integral_cmul show ?thesis by force
  1.3072 +next
  1.3073 +  case False then have "~ (\<lambda>x. c *\<^sub>R f x) integrable_on s"
  1.3074 +    using has_integral_cmul [of "(\<lambda>x. c *\<^sub>R f x)" _ s "inverse c"]
  1.3075 +    by force
  1.3076 +  with False show ?thesis by (simp add: not_integrable_integral)
  1.3077 +qed
  1.3078 +
  1.3079 +lemma integral_neg [simp]: "integral s (\<lambda>x. - f x) = - integral s f"
  1.3080 +proof (cases "f integrable_on s")
  1.3081 +  case True then show ?thesis
  1.3082 +    by (simp add: has_integral_neg integrable_integral integral_unique)
  1.3083 +next
  1.3084 +  case False then have "~ (\<lambda>x. - f x) integrable_on s"
  1.3085 +    using has_integral_neg [of "(\<lambda>x. - f x)" _ s ]
  1.3086 +    by force
  1.3087 +  with False show ?thesis by (simp add: not_integrable_integral)
  1.3088 +qed
  1.3089 +
  1.3090 +lemma integral_diff: "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
  1.3091 +    integral s (\<lambda>x. f x - g x) = integral s f - integral s g"
  1.3092 +  by (rule integral_unique) (metis integrable_integral has_integral_sub)
  1.3093 +
  1.3094 +lemma integrable_0: "(\<lambda>x. 0) integrable_on s"
  1.3095 +  unfolding integrable_on_def using has_integral_0 by auto
  1.3096 +
  1.3097 +lemma integrable_add: "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x + g x) integrable_on s"
  1.3098 +  unfolding integrable_on_def by(auto intro: has_integral_add)
  1.3099 +
  1.3100 +lemma integrable_cmul: "f integrable_on s \<Longrightarrow> (\<lambda>x. c *\<^sub>R f(x)) integrable_on s"
  1.3101 +  unfolding integrable_on_def by(auto intro: has_integral_cmul)
  1.3102 +
  1.3103 +lemma integrable_on_cmult_iff:
  1.3104 +  fixes c :: real
  1.3105 +  assumes "c \<noteq> 0"
  1.3106 +  shows "(\<lambda>x. c * f x) integrable_on s \<longleftrightarrow> f integrable_on s"
  1.3107 +  using integrable_cmul[of "\<lambda>x. c * f x" s "1 / c"] integrable_cmul[of f s c] \<open>c \<noteq> 0\<close>
  1.3108 +  by auto
  1.3109 +
  1.3110 +lemma integrable_on_cmult_left:
  1.3111 +  assumes "f integrable_on s"
  1.3112 +  shows "(\<lambda>x. of_real c * f x) integrable_on s"
  1.3113 +    using integrable_cmul[of f s "of_real c"] assms
  1.3114 +    by (simp add: scaleR_conv_of_real)
  1.3115 +
  1.3116 +lemma integrable_neg: "f integrable_on s \<Longrightarrow> (\<lambda>x. -f(x)) integrable_on s"
  1.3117 +  unfolding integrable_on_def by(auto intro: has_integral_neg)
  1.3118 +
  1.3119 +lemma integrable_diff:
  1.3120 +  "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x - g x) integrable_on s"
  1.3121 +  unfolding integrable_on_def by(auto intro: has_integral_sub)
  1.3122 +
  1.3123 +lemma integrable_linear:
  1.3124 +  "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> (h \<circ> f) integrable_on s"
  1.3125 +  unfolding integrable_on_def by(auto intro: has_integral_linear)
  1.3126 +
  1.3127 +lemma integral_linear:
  1.3128 +  "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> integral s (h \<circ> f) = h (integral s f)"
  1.3129 +  apply (rule has_integral_unique [where i=s and f = "h \<circ> f"])
  1.3130 +  apply (simp_all add: integrable_integral integrable_linear has_integral_linear )
  1.3131 +  done
  1.3132 +
  1.3133 +lemma integral_component_eq[simp]:
  1.3134 +  fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  1.3135 +  assumes "f integrable_on s"
  1.3136 +  shows "integral s (\<lambda>x. f x \<bullet> k) = integral s f \<bullet> k"
  1.3137 +  unfolding integral_linear[OF assms(1) bounded_linear_component,unfolded o_def] ..
  1.3138 +
  1.3139 +lemma has_integral_setsum:
  1.3140 +  assumes "finite t"
  1.3141 +    and "\<forall>a\<in>t. ((f a) has_integral (i a)) s"
  1.3142 +  shows "((\<lambda>x. setsum (\<lambda>a. f a x) t) has_integral (setsum i t)) s"
  1.3143 +  using assms(1) subset_refl[of t]
  1.3144 +proof (induct rule: finite_subset_induct)
  1.3145 +  case empty
  1.3146 +  then show ?case by auto
  1.3147 +next
  1.3148 +  case (insert x F)
  1.3149 +  with assms show ?case
  1.3150 +    by (simp add: has_integral_add)
  1.3151 +qed
  1.3152 +
  1.3153 +lemma integral_setsum:
  1.3154 +  "\<lbrakk>finite t;  \<forall>a\<in>t. (f a) integrable_on s\<rbrakk> \<Longrightarrow>
  1.3155 +   integral s (\<lambda>x. setsum (\<lambda>a. f a x) t) = setsum (\<lambda>a. integral s (f a)) t"
  1.3156 +  by (auto intro: has_integral_setsum integrable_integral)
  1.3157 +
  1.3158 +lemma integrable_setsum:
  1.3159 +  "\<lbrakk>finite t;  \<forall>a\<in>t. (f a) integrable_on s\<rbrakk> \<Longrightarrow> (\<lambda>x. setsum (\<lambda>a. f a x) t) integrable_on s"
  1.3160 +  unfolding integrable_on_def
  1.3161 +  apply (drule bchoice)
  1.3162 +  using has_integral_setsum[of t]
  1.3163 +  apply auto
  1.3164 +  done
  1.3165 +
  1.3166 +lemma has_integral_eq:
  1.3167 +  assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  1.3168 +    and "(f has_integral k) s"
  1.3169 +  shows "(g has_integral k) s"
  1.3170 +  using has_integral_sub[OF assms(2), of "\<lambda>x. f x - g x" 0]
  1.3171 +  using has_integral_is_0[of s "\<lambda>x. f x - g x"]
  1.3172 +  using assms(1)
  1.3173 +  by auto
  1.3174 +
  1.3175 +lemma integrable_eq: "(\<And>x. x \<in> s \<Longrightarrow> f x = g x) \<Longrightarrow> f integrable_on s \<Longrightarrow> g integrable_on s"
  1.3176 +  unfolding integrable_on_def
  1.3177 +  using has_integral_eq[of s f g] has_integral_eq by blast
  1.3178 +
  1.3179 +lemma has_integral_cong:
  1.3180 +  assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  1.3181 +  shows "(f has_integral i) s = (g has_integral i) s"
  1.3182 +  using has_integral_eq[of s f g] has_integral_eq[of s g f] assms
  1.3183 +  by auto
  1.3184 +
  1.3185 +lemma integral_cong:
  1.3186 +  assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  1.3187 +  shows "integral s f = integral s g"
  1.3188 +  unfolding integral_def
  1.3189 +by (metis (full_types, hide_lams) assms has_integral_cong integrable_eq)
  1.3190 +
  1.3191 +lemma integrable_on_cmult_left_iff [simp]:
  1.3192 +  assumes "c \<noteq> 0"
  1.3193 +  shows "(\<lambda>x. of_real c * f x) integrable_on s \<longleftrightarrow> f integrable_on s"
  1.3194 +        (is "?lhs = ?rhs")
  1.3195 +proof
  1.3196 +  assume ?lhs
  1.3197 +  then have "(\<lambda>x. of_real (1 / c) * (of_real c * f x)) integrable_on s"
  1.3198 +    using integrable_cmul[of "\<lambda>x. of_real c * f x" s "1 / of_real c"]
  1.3199 +    by (simp add: scaleR_conv_of_real)
  1.3200 +  then have "(\<lambda>x. (of_real (1 / c) * of_real c * f x)) integrable_on s"
  1.3201 +    by (simp add: algebra_simps)
  1.3202 +  with \<open>c \<noteq> 0\<close> show ?rhs
  1.3203 +    by (metis (no_types, lifting) integrable_eq mult.left_neutral nonzero_divide_eq_eq of_real_1 of_real_mult)
  1.3204 +qed (blast intro: integrable_on_cmult_left)
  1.3205 +
  1.3206 +lemma integrable_on_cmult_right:
  1.3207 +  fixes f :: "_ \<Rightarrow> 'b :: {comm_ring,real_algebra_1,real_normed_vector}"
  1.3208 +  assumes "f integrable_on s"
  1.3209 +  shows "(\<lambda>x. f x * of_real c) integrable_on s"
  1.3210 +using integrable_on_cmult_left [OF assms] by (simp add: mult.commute)
  1.3211 +
  1.3212 +lemma integrable_on_cmult_right_iff [simp]:
  1.3213 +  fixes f :: "_ \<Rightarrow> 'b :: {comm_ring,real_algebra_1,real_normed_vector}"
  1.3214 +  assumes "c \<noteq> 0"
  1.3215 +  shows "(\<lambda>x. f x * of_real c) integrable_on s \<longleftrightarrow> f integrable_on s"
  1.3216 +using integrable_on_cmult_left_iff [OF assms] by (simp add: mult.commute)
  1.3217 +
  1.3218 +lemma integrable_on_cdivide:
  1.3219 +  fixes f :: "_ \<Rightarrow> 'b :: real_normed_field"
  1.3220 +  assumes "f integrable_on s"
  1.3221 +  shows "(\<lambda>x. f x / of_real c) integrable_on s"
  1.3222 +by (simp add: integrable_on_cmult_right divide_inverse assms of_real_inverse [symmetric] del: of_real_inverse)
  1.3223 +
  1.3224 +lemma integrable_on_cdivide_iff [simp]:
  1.3225 +  fixes f :: "_ \<Rightarrow> 'b :: real_normed_field"
  1.3226 +  assumes "c \<noteq> 0"
  1.3227 +  shows "(\<lambda>x. f x / of_real c) integrable_on s \<longleftrightarrow> f integrable_on s"
  1.3228 +by (simp add: divide_inverse assms of_real_inverse [symmetric] del: of_real_inverse)
  1.3229 +
  1.3230 +lemma has_integral_null [intro]:
  1.3231 +  assumes "content(cbox a b) = 0"
  1.3232 +  shows "(f has_integral 0) (cbox a b)"
  1.3233 +proof -
  1.3234 +  have "gauge (\<lambda>x. ball x 1)"
  1.3235 +    by auto
  1.3236 +  moreover
  1.3237 +  {
  1.3238 +    fix e :: real
  1.3239 +    fix p
  1.3240 +    assume e: "e > 0"
  1.3241 +    assume p: "p tagged_division_of (cbox a b)"
  1.3242 +    have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) = 0"
  1.3243 +      unfolding norm_eq_zero diff_0_right
  1.3244 +      using setsum_content_null[OF assms(1) p, of f] .
  1.3245 +    then have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  1.3246 +      using e by auto
  1.3247 +  }
  1.3248 +  ultimately show ?thesis
  1.3249 +    by (auto simp: has_integral)
  1.3250 +qed
  1.3251 +
  1.3252 +lemma has_integral_null_real [intro]:
  1.3253 +  assumes "content {a .. b::real} = 0"
  1.3254 +  shows "(f has_integral 0) {a .. b}"
  1.3255 +  by (metis assms box_real(2) has_integral_null)
  1.3256 +
  1.3257 +lemma has_integral_null_eq[simp]: "content (cbox a b) = 0 \<Longrightarrow> (f has_integral i) (cbox a b) \<longleftrightarrow> i = 0"
  1.3258 +  by (auto simp add: has_integral_null dest!: integral_unique)
  1.3259 +
  1.3260 +lemma integral_null [simp]: "content (cbox a b) = 0 \<Longrightarrow> integral (cbox a b) f = 0"
  1.3261 +  by (metis has_integral_null integral_unique)
  1.3262 +
  1.3263 +lemma integrable_on_null [intro]: "content (cbox a b) = 0 \<Longrightarrow> f integrable_on (cbox a b)"
  1.3264 +  by (simp add: has_integral_integrable)
  1.3265 +
  1.3266 +lemma has_integral_empty[intro]: "(f has_integral 0) {}"
  1.3267 +  by (simp add: has_integral_is_0)
  1.3268 +
  1.3269 +lemma has_integral_empty_eq[simp]: "(f has_integral i) {} \<longleftrightarrow> i = 0"
  1.3270 +  by (auto simp add: has_integral_empty has_integral_unique)
  1.3271 +
  1.3272 +lemma integrable_on_empty[intro]: "f integrable_on {}"
  1.3273 +  unfolding integrable_on_def by auto
  1.3274 +
  1.3275 +lemma integral_empty[simp]: "integral {} f = 0"
  1.3276 +  by (rule integral_unique) (rule has_integral_empty)
  1.3277 +
  1.3278 +lemma has_integral_refl[intro]:
  1.3279 +  fixes a :: "'a::euclidean_space"
  1.3280 +  shows "(f has_integral 0) (cbox a a)"
  1.3281 +    and "(f has_integral 0) {a}"
  1.3282 +proof -
  1.3283 +  have *: "{a} = cbox a a"
  1.3284 +    apply (rule set_eqI)
  1.3285 +    unfolding mem_box singleton_iff euclidean_eq_iff[where 'a='a]
  1.3286 +    apply safe
  1.3287 +    prefer 3
  1.3288 +    apply (erule_tac x=b in ballE)
  1.3289 +    apply (auto simp add: field_simps)
  1.3290 +    done
  1.3291 +  show "(f has_integral 0) (cbox a a)" "(f has_integral 0) {a}"
  1.3292 +    unfolding *
  1.3293 +    apply (rule_tac[!] has_integral_null)
  1.3294 +    unfolding content_eq_0_interior
  1.3295 +    unfolding interior_cbox
  1.3296 +    using box_sing
  1.3297 +    apply auto
  1.3298 +    done
  1.3299 +qed
  1.3300 +
  1.3301 +lemma integrable_on_refl[intro]: "f integrable_on cbox a a"
  1.3302 +  unfolding integrable_on_def by auto
  1.3303 +
  1.3304 +lemma integral_refl [simp]: "integral (cbox a a) f = 0"
  1.3305 +  by (rule integral_unique) auto
  1.3306 +
  1.3307 +lemma integral_singleton [simp]: "integral {a} f = 0"
  1.3308 +  by auto
  1.3309 +
  1.3310 +lemma integral_blinfun_apply:
  1.3311 +  assumes "f integrable_on s"
  1.3312 +  shows "integral s (\<lambda>x. blinfun_apply h (f x)) = blinfun_apply h (integral s f)"
  1.3313 +  by (subst integral_linear[symmetric, OF assms blinfun.bounded_linear_right]) (simp add: o_def)
  1.3314 +
  1.3315 +lemma blinfun_apply_integral:
  1.3316 +  assumes "f integrable_on s"
  1.3317 +  shows "blinfun_apply (integral s f) x = integral s (\<lambda>y. blinfun_apply (f y) x)"
  1.3318 +  by (metis (no_types, lifting) assms blinfun.prod_left.rep_eq integral_blinfun_apply integral_cong)
  1.3319 +
  1.3320 +lemma has_integral_componentwise_iff:
  1.3321 +  fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  1.3322 +  shows "(f has_integral y) A \<longleftrightarrow> (\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
  1.3323 +proof safe
  1.3324 +  fix b :: 'b assume "(f has_integral y) A"
  1.3325 +  from has_integral_linear[OF this(1) bounded_linear_component, of b]
  1.3326 +    show "((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A" by (simp add: o_def)
  1.3327 +next
  1.3328 +  assume "(\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
  1.3329 +  hence "\<forall>b\<in>Basis. (((\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. f x \<bullet> b)) has_integral ((y \<bullet> b) *\<^sub>R b)) A"
  1.3330 +    by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
  1.3331 +  hence "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. (y \<bullet> b) *\<^sub>R b)) A"
  1.3332 +    by (intro has_integral_setsum) (simp_all add: o_def)
  1.3333 +  thus "(f has_integral y) A" by (simp add: euclidean_representation)
  1.3334 +qed
  1.3335 +
  1.3336 +lemma has_integral_componentwise:
  1.3337 +  fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  1.3338 +  shows "(\<And>b. b \<in> Basis \<Longrightarrow> ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A) \<Longrightarrow> (f has_integral y) A"
  1.3339 +  by (subst has_integral_componentwise_iff) blast
  1.3340 +
  1.3341 +lemma integrable_componentwise_iff:
  1.3342 +  fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  1.3343 +  shows "f integrable_on A \<longleftrightarrow> (\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)"
  1.3344 +proof
  1.3345 +  assume "f integrable_on A"
  1.3346 +  then obtain y where "(f has_integral y) A" by (auto simp: integrable_on_def)
  1.3347 +  hence "(\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
  1.3348 +    by (subst (asm) has_integral_componentwise_iff)
  1.3349 +  thus "(\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)" by (auto simp: integrable_on_def)
  1.3350 +next
  1.3351 +  assume "(\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)"
  1.3352 +  then obtain y where "\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral y b) A"
  1.3353 +    unfolding integrable_on_def by (subst (asm) bchoice_iff) blast
  1.3354 +  hence "\<forall>b\<in>Basis. (((\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. f x \<bullet> b)) has_integral (y b *\<^sub>R b)) A"
  1.3355 +    by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
  1.3356 +  hence "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. y b *\<^sub>R b)) A"
  1.3357 +    by (intro has_integral_setsum) (simp_all add: o_def)
  1.3358 +  thus "f integrable_on A" by (auto simp: integrable_on_def o_def euclidean_representation)
  1.3359 +qed
  1.3360 +
  1.3361 +lemma integrable_componentwise:
  1.3362 +  fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  1.3363 +  shows "(\<And>b. b \<in> Basis \<Longrightarrow> (\<lambda>x. f x \<bullet> b) integrable_on A) \<Longrightarrow> f integrable_on A"
  1.3364 +  by (subst integrable_componentwise_iff) blast
  1.3365 +
  1.3366 +lemma integral_componentwise:
  1.3367 +  fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  1.3368 +  assumes "f integrable_on A"
  1.3369 +  shows "integral A f = (\<Sum>b\<in>Basis. integral A (\<lambda>x. (f x \<bullet> b) *\<^sub>R b))"
  1.3370 +proof -
  1.3371 +  from assms have integrable: "\<forall>b\<in>Basis. (\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. (f x \<bullet> b)) integrable_on A"
  1.3372 +    by (subst (asm) integrable_componentwise_iff, intro integrable_linear ballI)
  1.3373 +       (simp_all add: bounded_linear_scaleR_left)
  1.3374 +  have "integral A f = integral A (\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b)"
  1.3375 +    by (simp add: euclidean_representation)
  1.3376 +  also from integrable have "\<dots> = (\<Sum>a\<in>Basis. integral A (\<lambda>x. (f x \<bullet> a) *\<^sub>R a))"
  1.3377 +    by (subst integral_setsum) (simp_all add: o_def)
  1.3378 +  finally show ?thesis .
  1.3379 +qed
  1.3380 +
  1.3381 +lemma integrable_component:
  1.3382 +  "f integrable_on A \<Longrightarrow> (\<lambda>x. f x \<bullet> (y :: 'b :: euclidean_space)) integrable_on A"
  1.3383 +  by (drule integrable_linear[OF _ bounded_linear_component[of y]]) (simp add: o_def)
  1.3384 +
  1.3385 +
  1.3386 +
  1.3387 +subsection \<open>Cauchy-type criterion for integrability.\<close>
  1.3388 +
  1.3389 +(* XXXXXXX *)
  1.3390 +lemma integrable_cauchy:
  1.3391 +  fixes f :: "'n::euclidean_space \<Rightarrow> 'a::{real_normed_vector,complete_space}"
  1.3392 +  shows "f integrable_on cbox a b \<longleftrightarrow>
  1.3393 +    (\<forall>e>0.\<exists>d. gauge d \<and>
  1.3394 +      (\<forall>p1 p2. p1 tagged_division_of (cbox a b) \<and> d fine p1 \<and>
  1.3395 +        p2 tagged_division_of (cbox a b) \<and> d fine p2 \<longrightarrow>
  1.3396 +        norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 -
  1.3397 +        setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) < e))"
  1.3398 +  (is "?l = (\<forall>e>0. \<exists>d. ?P e d)")
  1.3399 +proof
  1.3400 +  assume ?l
  1.3401 +  then guess y unfolding integrable_on_def has_integral .. note y=this
  1.3402 +  show "\<forall>e>0. \<exists>d. ?P e d"
  1.3403 +  proof (clarify, goal_cases)
  1.3404 +    case (1 e)
  1.3405 +    then have "e/2 > 0" by auto
  1.3406 +    then guess d
  1.3407 +      apply -
  1.3408 +      apply (drule y[rule_format])
  1.3409 +      apply (elim exE conjE)
  1.3410 +      done
  1.3411 +    note d=this[rule_format]
  1.3412 +    show ?case
  1.3413 +    proof (rule_tac x=d in exI, clarsimp simp: d)
  1.3414 +      fix p1 p2
  1.3415 +      assume as: "p1 tagged_division_of (cbox a b)" "d fine p1"
  1.3416 +                 "p2 tagged_division_of (cbox a b)" "d fine p2"
  1.3417 +      show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  1.3418 +        apply (rule dist_triangle_half_l[where y=y,unfolded dist_norm])
  1.3419 +        using d(2)[OF conjI[OF as(1-2)]] d(2)[OF conjI[OF as(3-4)]] .
  1.3420 +    qed
  1.3421 +  qed
  1.3422 +next
  1.3423 +  assume "\<forall>e>0. \<exists>d. ?P e d"
  1.3424 +  then have "\<forall>n::nat. \<exists>d. ?P (inverse(of_nat (n + 1))) d"
  1.3425 +    by auto
  1.3426 +  from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
  1.3427 +  have "\<And>n. gauge (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}})"
  1.3428 +    apply (rule gauge_inters)
  1.3429 +    using d(1)
  1.3430 +    apply auto
  1.3431 +    done
  1.3432 +  then have "\<forall>n. \<exists>p. p tagged_division_of (cbox a b) \<and> (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}}) fine p"
  1.3433 +    by (meson fine_division_exists)
  1.3434 +  from choice[OF this] guess p .. note p = conjunctD2[OF this[rule_format]]
  1.3435 +  have dp: "\<And>i n. i\<le>n \<Longrightarrow> d i fine p n"
  1.3436 +    using p(2) unfolding fine_inters by auto
  1.3437 +  have "Cauchy (\<lambda>n. setsum (\<lambda>(x,k). content k *\<^sub>R (f x)) (p n))"
  1.3438 +  proof (rule CauchyI, goal_cases)
  1.3439 +    case (1 e)
  1.3440 +    then guess N unfolding real_arch_inverse[of e] .. note N=this
  1.3441 +    show ?case
  1.3442 +      apply (rule_tac x=N in exI)
  1.3443 +    proof clarify
  1.3444 +      fix m n
  1.3445 +      assume mn: "N \<le> m" "N \<le> n"
  1.3446 +      have *: "N = (N - 1) + 1" using N by auto
  1.3447 +      show "norm ((\<Sum>(x, k)\<in>p m. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p n. content k *\<^sub>R f x)) < e"
  1.3448 +        apply (rule less_trans[OF _ N[THEN conjunct2,THEN conjunct2]])
  1.3449 +        apply(subst *)
  1.3450 +        using dp p(1) mn d(2) by auto
  1.3451 +    qed
  1.3452 +  qed
  1.3453 +  then guess y unfolding convergent_eq_cauchy[symmetric] .. note y=this[THEN LIMSEQ_D]
  1.3454 +  show ?l
  1.3455 +    unfolding integrable_on_def has_integral
  1.3456 +  proof (rule_tac x=y in exI, clarify)
  1.3457 +    fix e :: real
  1.3458 +    assume "e>0"
  1.3459 +    then have *:"e/2 > 0" by auto
  1.3460 +    then guess N1 unfolding real_arch_inverse[of "e/2"] .. note N1=this
  1.3461 +    then have N1': "N1 = N1 - 1 + 1"
  1.3462 +      by auto
  1.3463 +    guess N2 using y[OF *] .. note N2=this
  1.3464 +    have "gauge (d (N1 + N2))"
  1.3465 +      using d by auto
  1.3466 +    moreover
  1.3467 +    {
  1.3468 +      fix q
  1.3469 +      assume as: "q tagged_division_of (cbox a b)" "d (N1 + N2) fine q"
  1.3470 +      have *: "inverse (of_nat (N1 + N2 + 1)) < e / 2"
  1.3471 +        apply (rule less_trans)
  1.3472 +        using N1
  1.3473 +        apply auto
  1.3474 +        done
  1.3475 +      have "norm ((\<Sum>(x, k)\<in>q. content k *\<^sub>R f x) - y) < e"
  1.3476 +        apply (rule norm_triangle_half_r)
  1.3477 +        apply (rule less_trans[OF _ *])
  1.3478 +        apply (subst N1', rule d(2)[of "p (N1+N2)"])
  1.3479 +        using N1' as(1) as(2) dp
  1.3480 +        apply (simp add: \<open>\<forall>x. p x tagged_division_of cbox a b \<and> (\<lambda>xa. \<Inter>{d i xa |i. i \<in> {0..x}}) fine p x\<close>)
  1.3481 +        using N2 le_add2 by blast
  1.3482 +    }
  1.3483 +    ultimately show "\<exists>d. gauge d \<and>
  1.3484 +      (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  1.3485 +        norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - y) < e)"
  1.3486 +      by (rule_tac x="d (N1 + N2)" in exI) auto
  1.3487 +  qed
  1.3488 +qed
  1.3489 +
  1.3490 +
  1.3491 +subsection \<open>Additivity of integral on abutting intervals.\<close>
  1.3492 +
  1.3493 +lemma tagged_division_split_left_inj:
  1.3494 +  fixes x1 :: "'a::euclidean_space"
  1.3495 +  assumes d: "d tagged_division_of i"
  1.3496 +    and k12: "(x1, k1) \<in> d"
  1.3497 +             "(x2, k2) \<in> d"
  1.3498 +             "k1 \<noteq> k2"
  1.3499 +             "k1 \<inter> {x. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c}"
  1.3500 +             "k \<in> Basis"
  1.3501 +  shows "content (k1 \<inter> {x. x\<bullet>k \<le> c}) = 0"
  1.3502 +proof -
  1.3503 +  have *: "\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c"
  1.3504 +    by force
  1.3505 +  show ?thesis
  1.3506 +    using k12
  1.3507 +    by (fastforce intro!:  division_split_left_inj[OF division_of_tagged_division[OF d]] *)
  1.3508 +qed
  1.3509 +
  1.3510 +lemma tagged_division_split_right_inj:
  1.3511 +  fixes x1 :: "'a::euclidean_space"
  1.3512 +  assumes d: "d tagged_division_of i"
  1.3513 +    and k12: "(x1, k1) \<in> d"
  1.3514 +             "(x2, k2) \<in> d"
  1.3515 +             "k1 \<noteq> k2"
  1.3516 +             "k1 \<inter> {x. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c}"
  1.3517 +             "k \<in> Basis"
  1.3518 +  shows "content (k1 \<inter> {x. x\<bullet>k \<ge> c}) = 0"
  1.3519 +proof -
  1.3520 +  have *: "\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c"
  1.3521 +    by force
  1.3522 +  show ?thesis
  1.3523 +    using k12
  1.3524 +    by (fastforce intro!:  division_split_right_inj[OF division_of_tagged_division[OF d]] *)
  1.3525 +qed
  1.3526 +
  1.3527 +lemma has_integral_split:
  1.3528 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1.3529 +  assumes fi: "(f has_integral i) (cbox a b \<inter> {x. x\<bullet>k \<le> c})"
  1.3530 +      and fj: "(f has_integral j) (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  1.3531 +      and k: "k \<in> Basis"
  1.3532 +  shows "(f has_integral (i + j)) (cbox a b)"
  1.3533 +proof (unfold has_integral, rule, rule, goal_cases)
  1.3534 +  case (1 e)
  1.3535 +  then have e: "e/2 > 0"
  1.3536 +    by auto
  1.3537 +    obtain d1
  1.3538 +    where d1: "gauge d1"
  1.3539 +      and d1norm:
  1.3540 +        "\<And>p. \<lbrakk>p tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<le> c};
  1.3541 +               d1 fine p\<rbrakk> \<Longrightarrow> norm ((\<Sum>(x, k) \<in> p. content k *\<^sub>R f x) - i) < e / 2"
  1.3542 +       apply (rule has_integralD[OF fi[unfolded interval_split[OF k]] e])
  1.3543 +       apply (simp add: interval_split[symmetric] k)
  1.3544 +       done
  1.3545 +    obtain d2
  1.3546 +    where d2: "gauge d2"
  1.3547 +      and d2norm:
  1.3548 +        "\<And>p. \<lbrakk>p tagged_division_of cbox a b \<inter> {x. c \<le> x \<bullet> k};
  1.3549 +               d2 fine p\<rbrakk> \<Longrightarrow> norm ((\<Sum>(x, k) \<in> p. content k *\<^sub>R f x) - j) < e / 2"
  1.3550 +       apply (rule has_integralD[OF fj[unfolded interval_split[OF k]] e])
  1.3551 +       apply (simp add: interval_split[symmetric] k)
  1.3552 +       done
  1.3553 +  let ?d = "\<lambda>x. if x\<bullet>k = c then (d1 x \<inter> d2 x) else ball x \<bar>x\<bullet>k - c\<bar> \<inter> d1 x \<inter> d2 x"
  1.3554 +  have "gauge ?d"
  1.3555 +    using d1 d2 unfolding gauge_def by auto
  1.3556 +  then show ?case
  1.3557 +  proof (rule_tac x="?d" in exI, safe)
  1.3558 +    fix p
  1.3559 +    assume "p tagged_division_of (cbox a b)" "?d fine p"
  1.3560 +    note p = this tagged_division_ofD[OF this(1)]
  1.3561 +    have xk_le_c: "\<And>x kk. (x, kk) \<in> p \<Longrightarrow> kk \<inter> {x. x\<bullet>k \<le> c} \<noteq> {} \<Longrightarrow> x\<bullet>k \<le> c"
  1.3562 +    proof -
  1.3563 +      fix x kk
  1.3564 +      assume as: "(x, kk) \<in> p" and kk: "kk \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}"
  1.3565 +      show "x\<bullet>k \<le> c"
  1.3566 +      proof (rule ccontr)
  1.3567 +        assume **: "\<not> ?thesis"
  1.3568 +        from this[unfolded not_le]
  1.3569 +        have "kk \<subseteq> ball x \<bar>x \<bullet> k - c\<bar>"
  1.3570 +          using p(2)[unfolded fine_def, rule_format,OF as] by auto
  1.3571 +        with kk obtain y where y: "y \<in> ball x \<bar>x \<bullet> k - c\<bar>" "y\<bullet>k \<le> c"
  1.3572 +          by blast
  1.3573 +        then have "\<bar>x \<bullet> k - y \<bullet> k\<bar> < \<bar>x \<bullet> k - c\<bar>"
  1.3574 +          using Basis_le_norm[OF k, of "x - y"]
  1.3575 +          by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
  1.3576 +        with y show False
  1.3577 +          using ** by (auto simp add: field_simps)
  1.3578 +      qed
  1.3579 +    qed
  1.3580 +    have xk_ge_c: "\<And>x kk. (x, kk) \<in> p \<Longrightarrow> kk \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {} \<Longrightarrow> x\<bullet>k \<ge> c"
  1.3581 +    proof -
  1.3582 +      fix x kk
  1.3583 +      assume as: "(x, kk) \<in> p" and kk: "kk \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}"
  1.3584 +      show "x\<bullet>k \<ge> c"
  1.3585 +      proof (rule ccontr)
  1.3586 +        assume **: "\<not> ?thesis"
  1.3587 +        from this[unfolded not_le] have "kk \<subseteq> ball x \<bar>x \<bullet> k - c\<bar>"
  1.3588 +          using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
  1.3589 +        with kk obtain y where y: "y \<in> ball x \<bar>x \<bullet> k - c\<bar>" "y\<bullet>k \<ge> c"
  1.3590 +          by blast
  1.3591 +        then have "\<bar>x \<bullet> k - y \<bullet> k\<bar> < \<bar>x \<bullet> k - c\<bar>"
  1.3592 +          using Basis_le_norm[OF k, of "x - y"]
  1.3593 +          by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
  1.3594 +        with y show False
  1.3595 +          using ** by (auto simp add: field_simps)
  1.3596 +      qed
  1.3597 +    qed
  1.3598 +
  1.3599 +    have lem1: "\<And>f P Q. (\<forall>x k. (x, k) \<in> {(x, f k) | x k. P x k} \<longrightarrow> Q x k) \<longleftrightarrow>
  1.3600 +                         (\<forall>x k. P x k \<longrightarrow> Q x (f k))"
  1.3601 +      by auto
  1.3602 +    have fin_finite: "finite {(x,f k) | x k. (x,k) \<in> s \<and> P x k}" if "finite s" for f s P
  1.3603 +    proof -
  1.3604 +      from that have "finite ((\<lambda>(x, k). (x, f k)) ` s)"
  1.3605 +        by auto
  1.3606 +      then show ?thesis
  1.3607 +        by (rule rev_finite_subset) auto
  1.3608 +    qed
  1.3609 +    { fix g :: "'a set \<Rightarrow> 'a set"
  1.3610 +      fix i :: "'a \<times> 'a set"
  1.3611 +      assume "i \<in> (\<lambda>(x, k). (x, g k)) ` p - {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
  1.3612 +      then obtain x k where xk:
  1.3613 +              "i = (x, g k)"  "(x, k) \<in> p"
  1.3614 +              "(x, g k) \<notin> {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
  1.3615 +          by auto
  1.3616 +      have "content (g k) = 0"
  1.3617 +        using xk using content_empty by auto
  1.3618 +      then have "(\<lambda>(x, k). content k *\<^sub>R f x) i = 0"
  1.3619 +        unfolding xk split_conv by auto
  1.3620 +    } note [simp] = this
  1.3621 +    have lem3: "\<And>g :: 'a set \<Rightarrow> 'a set. finite p \<Longrightarrow>
  1.3622 +                  setsum (\<lambda>(x, k). content k *\<^sub>R f x) {(x,g k) |x k. (x,k) \<in> p \<and> g k \<noteq> {}} =
  1.3623 +                  setsum (\<lambda>(x, k). content k *\<^sub>R f x) ((\<lambda>(x, k). (x, g k)) ` p)"
  1.3624 +      by (rule setsum.mono_neutral_left) auto
  1.3625 +    let ?M1 = "{(x, kk \<inter> {x. x\<bullet>k \<le> c}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}}"
  1.3626 +    have d1_fine: "d1 fine ?M1"
  1.3627 +      by (force intro: fineI dest: fineD[OF p(2)] simp add: split: if_split_asm)
  1.3628 +    have "norm ((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) < e/2"
  1.3629 +    proof (rule d1norm [OF tagged_division_ofI d1_fine])
  1.3630 +      show "finite ?M1"
  1.3631 +        by (rule fin_finite p(3))+
  1.3632 +      show "\<Union>{k. \<exists>x. (x, k) \<in> ?M1} = cbox a b \<inter> {x. x\<bullet>k \<le> c}"
  1.3633 +        unfolding p(8)[symmetric] by auto
  1.3634 +      fix x l
  1.3635 +      assume xl: "(x, l) \<in> ?M1"
  1.3636 +      then guess x' l' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note xl'=this
  1.3637 +      show "x \<in> l" "l \<subseteq> cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  1.3638 +        unfolding xl'
  1.3639 +        using p(4-6)[OF xl'(3)] using xl'(4)
  1.3640 +        using xk_le_c[OF xl'(3-4)] by auto
  1.3641 +      show "\<exists>a b. l = cbox a b"
  1.3642 +        unfolding xl'
  1.3643 +        using p(6)[OF xl'(3)]
  1.3644 +        by (fastforce simp add: interval_split[OF k,where c=c])
  1.3645 +      fix y r
  1.3646 +      let ?goal = "interior l \<inter> interior r = {}"
  1.3647 +      assume yr: "(y, r) \<in> ?M1"
  1.3648 +      then guess y' r' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note yr'=this
  1.3649 +      assume as: "(x, l) \<noteq> (y, r)"
  1.3650 +      show "interior l \<inter> interior r = {}"
  1.3651 +      proof (cases "l' = r' \<longrightarrow> x' = y'")
  1.3652 +        case False
  1.3653 +        then show ?thesis
  1.3654 +          using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1.3655 +      next
  1.3656 +        case True
  1.3657 +        then have "l' \<noteq> r'"
  1.3658 +          using as unfolding xl' yr' by auto
  1.3659 +        then show ?thesis
  1.3660 +          using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1.3661 +      qed
  1.3662 +    qed
  1.3663 +    moreover
  1.3664 +    let ?M2 = "{(x,kk \<inter> {x. x\<bullet>k \<ge> c}) |x kk. (x,kk) \<in> p \<and> kk \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}}"
  1.3665 +    have d2_fine: "d2 fine ?M2"
  1.3666 +      by (force intro: fineI dest: fineD[OF p(2)] simp add: split: if_split_asm)
  1.3667 +    have "norm ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) < e/2"
  1.3668 +    proof (rule d2norm [OF tagged_division_ofI d2_fine])
  1.3669 +      show "finite ?M2"
  1.3670 +        by (rule fin_finite p(3))+
  1.3671 +      show "\<Union>{k. \<exists>x. (x, k) \<in> ?M2} = cbox a b \<inter> {x. x\<bullet>k \<ge> c}"
  1.3672 +        unfolding p(8)[symmetric] by auto
  1.3673 +      fix x l
  1.3674 +      assume xl: "(x, l) \<in> ?M2"
  1.3675 +      then guess x' l' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note xl'=this
  1.3676 +      show "x \<in> l" "l \<subseteq> cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  1.3677 +        unfolding xl'
  1.3678 +        using p(4-6)[OF xl'(3)] xl'(4) xk_ge_c[OF xl'(3-4)]
  1.3679 +        by auto
  1.3680 +      show "\<exists>a b. l = cbox a b"
  1.3681 +        unfolding xl'
  1.3682 +        using p(6)[OF xl'(3)]
  1.3683 +        by (fastforce simp add: interval_split[OF k, where c=c])
  1.3684 +      fix y r
  1.3685 +      let ?goal = "interior l \<inter> interior r = {}"
  1.3686 +      assume yr: "(y, r) \<in> ?M2"
  1.3687 +      then guess y' r' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note yr'=this
  1.3688 +      assume as: "(x, l) \<noteq> (y, r)"
  1.3689 +      show "interior l \<inter> interior r = {}"
  1.3690 +      proof (cases "l' = r' \<longrightarrow> x' = y'")
  1.3691 +        case False
  1.3692 +        then show ?thesis
  1.3693 +          using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1.3694 +      next
  1.3695 +        case True
  1.3696 +        then have "l' \<noteq> r'"
  1.3697 +          using as unfolding xl' yr' by auto
  1.3698 +        then show ?thesis
  1.3699 +          using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1.3700 +      qed
  1.3701 +    qed
  1.3702 +    ultimately
  1.3703 +    have "norm (((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j)) < e/2 + e/2"
  1.3704 +      using norm_add_less by blast
  1.3705 +    also {
  1.3706 +      have eq0: "\<And>x y. x = (0::real) \<Longrightarrow> x *\<^sub>R (y::'b) = 0"
  1.3707 +        using scaleR_zero_left by auto
  1.3708 +      have cont_eq: "\<And>g. (\<lambda>(x,l). content l *\<^sub>R f x) \<circ> (\<lambda>(x,l). (x,g l)) = (\<lambda>(x,l). content (g l) *\<^sub>R f x)"
  1.3709 +        by auto
  1.3710 +      have "((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) =
  1.3711 +        (\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - (i + j)"
  1.3712 +        by auto
  1.3713 +      also have "\<dots> = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f x) +
  1.3714 +        (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f x) - (i + j)"
  1.3715 +        unfolding lem3[OF p(3)]
  1.3716 +        by (subst setsum.reindex_nontrivial[OF p(3)], auto intro!: k eq0 tagged_division_split_left_inj[OF p(1)] tagged_division_split_right_inj[OF p(1)]
  1.3717 +              simp: cont_eq)+
  1.3718 +      also note setsum.distrib[symmetric]
  1.3719 +      also have "\<And>x. x \<in> p \<Longrightarrow>
  1.3720 +                    (\<lambda>(x,ka). content (ka \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f x) x +
  1.3721 +                    (\<lambda>(x,ka). content (ka \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f x) x =
  1.3722 +                    (\<lambda>(x,ka). content ka *\<^sub>R f x) x"
  1.3723 +      proof clarify
  1.3724 +        fix a b
  1.3725 +        assume "(a, b) \<in> p"
  1.3726 +        from p(6)[OF this] guess u v by (elim exE) note uv=this
  1.3727 +        then show "content (b \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f a + content (b \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f a =
  1.3728 +          content b *\<^sub>R f a"
  1.3729 +          unfolding scaleR_left_distrib[symmetric]
  1.3730 +          unfolding uv content_split[OF k,of u v c]
  1.3731 +          by auto
  1.3732 +      qed
  1.3733 +      note setsum.cong [OF _ this]
  1.3734 +      finally have "(\<Sum>(x, k)\<in>{(x, kk \<inter> {x. x \<bullet> k \<le> c}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}}. content k *\<^sub>R f x) - i +
  1.3735 +        ((\<Sum>(x, k)\<in>{(x, kk \<inter> {x. c \<le> x \<bullet> k}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. c \<le> x \<bullet> k} \<noteq> {}}. content k *\<^sub>R f x) - j) =
  1.3736 +        (\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f x) - (i + j)"
  1.3737 +        by auto
  1.3738 +    }
  1.3739 +    finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (i + j)) < e"
  1.3740 +      by auto
  1.3741 +  qed
  1.3742 +qed
  1.3743 +
  1.3744 +
  1.3745 +subsection \<open>A sort of converse, integrability on subintervals.\<close>
  1.3746 +
  1.3747 +lemma tagged_division_union_interval:
  1.3748 +  fixes a :: "'a::euclidean_space"
  1.3749 +  assumes "p1 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<le> (c::real)})"
  1.3750 +    and "p2 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  1.3751 +    and k: "k \<in> Basis"
  1.3752 +  shows "(p1 \<union> p2) tagged_division_of (cbox a b)"
  1.3753 +proof -
  1.3754 +  have *: "cbox a b = (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<union> (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  1.3755 +    by auto
  1.3756 +  show ?thesis
  1.3757 +    apply (subst *)
  1.3758 +    apply (rule tagged_division_union[OF assms(1-2)])
  1.3759 +    unfolding interval_split[OF k] interior_cbox
  1.3760 +    using k
  1.3761 +    apply (auto simp add: box_def elim!: ballE[where x=k])
  1.3762 +    done
  1.3763 +qed
  1.3764 +
  1.3765 +lemma tagged_division_union_interval_real:
  1.3766 +  fixes a :: real
  1.3767 +  assumes "p1 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<le> (c::real)})"
  1.3768 +    and "p2 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<ge> c})"
  1.3769 +    and k: "k \<in> Basis"
  1.3770 +  shows "(p1 \<union> p2) tagged_division_of {a .. b}"
  1.3771 +  using assms
  1.3772 +  unfolding box_real[symmetric]
  1.3773 +  by (rule tagged_division_union_interval)
  1.3774 +
  1.3775 +lemma has_integral_separate_sides:
  1.3776 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1.3777 +  assumes "(f has_integral i) (cbox a b)"
  1.3778 +    and "e > 0"
  1.3779 +    and k: "k \<in> Basis"
  1.3780 +  obtains d where "gauge d"
  1.3781 +    "\<forall>p1 p2. p1 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<and> d fine p1 \<and>
  1.3782 +        p2 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) \<and> d fine p2 \<longrightarrow>
  1.3783 +        norm ((setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 + setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) - i) < e"
  1.3784 +proof -
  1.3785 +  guess d using has_integralD[OF assms(1-2)] . note d=this
  1.3786 +  { fix p1 p2
  1.3787 +    assume "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p1"
  1.3788 +    note p1=tagged_division_ofD[OF this(1)] this
  1.3789 +    assume "p2 tagged_division_of (cbox a b) \<inter> {x. c \<le> x \<bullet> k}" "d fine p2"
  1.3790 +    note p2=tagged_division_ofD[OF this(1)] this
  1.3791 +    note tagged_division_union_interval[OF p1(7) p2(7)] note p12 = tagged_division_ofD[OF this] this
  1.3792 +    { fix a b
  1.3793 +      assume ab: "(a, b) \<in> p1 \<inter> p2"
  1.3794 +      have "(a, b) \<in> p1"
  1.3795 +        using ab by auto
  1.3796 +      with p1 obtain u v where uv: "b = cbox u v" by auto
  1.3797 +      have "b \<subseteq> {x. x\<bullet>k = c}"
  1.3798 +        using ab p1(3)[of a b] p2(3)[of a b] by fastforce
  1.3799 +      moreover
  1.3800 +      have "interior {x::'a. x \<bullet> k = c} = {}"
  1.3801 +      proof (rule ccontr)
  1.3802 +        assume "\<not> ?thesis"
  1.3803 +        then obtain x where x: "x \<in> interior {x::'a. x\<bullet>k = c}"
  1.3804 +          by auto
  1.3805 +        then guess e unfolding mem_interior .. note e=this
  1.3806 +        have x: "x\<bullet>k = c"
  1.3807 +          using x interior_subset by fastforce
  1.3808 +        have *: "\<And>i. i \<in> Basis \<Longrightarrow> \<bar>(x - (x + (e / 2) *\<^sub>R k)) \<bullet> i\<bar> = (if i = k then e/2 else 0)"
  1.3809 +          using e k by (auto simp: inner_simps inner_not_same_Basis)
  1.3810 +        have "(\<Sum>i\<in>Basis. \<bar>(x - (x + (e / 2 ) *\<^sub>R k)) \<bullet> i\<bar>) =
  1.3811 +              (\<Sum>i\<in>Basis. (if i = k then e / 2 else 0))"
  1.3812 +          using "*" by (blast intro: setsum.cong)
  1.3813 +        also have "\<dots> < e"
  1.3814 +          apply (subst setsum.delta)
  1.3815 +          using e
  1.3816 +          apply auto
  1.3817 +          done
  1.3818 +        finally have "x + (e/2) *\<^sub>R k \<in> ball x e"
  1.3819 +          unfolding mem_ball dist_norm by(rule le_less_trans[OF norm_le_l1])
  1.3820 +        then have "x + (e/2) *\<^sub>R k \<in> {x. x\<bullet>k = c}"
  1.3821 +          using e by auto
  1.3822 +        then show False
  1.3823 +          unfolding mem_Collect_eq using e x k by (auto simp: inner_simps)
  1.3824 +      qed
  1.3825 +      ultimately have "content b = 0"
  1.3826 +        unfolding uv content_eq_0_interior
  1.3827 +        using interior_mono by blast
  1.3828 +      then have "content b *\<^sub>R f a = 0"
  1.3829 +        by auto
  1.3830 +    }
  1.3831 +    then have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) =
  1.3832 +               norm ((\<Sum>(x, k)\<in>p1 \<union> p2. content k *\<^sub>R f x) - i)"
  1.3833 +      by (subst setsum.union_inter_neutral) (auto simp: p1 p2)
  1.3834 +    also have "\<dots> < e"
  1.3835 +      by (rule k d(2) p12 fine_union p1 p2)+
  1.3836 +    finally have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) < e" .
  1.3837 +   }
  1.3838 +  then show ?thesis
  1.3839 +    by (auto intro: that[of d] d elim: )
  1.3840 +qed
  1.3841 +
  1.3842 +lemma integrable_split[intro]:
  1.3843 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::{real_normed_vector,complete_space}"
  1.3844 +  assumes "f integrable_on cbox a b"
  1.3845 +    and k: "k \<in> Basis"
  1.3846 +  shows "f integrable_on (cbox a b \<inter> {x. x\<bullet>k \<le> c})" (is ?t1)
  1.3847 +    and "f integrable_on (cbox a b \<inter> {x. x\<bullet>k \<ge> c})" (is ?t2)
  1.3848 +proof -
  1.3849 +  guess y using assms(1) unfolding integrable_on_def .. note y=this
  1.3850 +  define b' where "b' = (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) c else b\<bullet>i)*\<^sub>R i)"
  1.3851 +  define a' where "a' = (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) c else a\<bullet>i)*\<^sub>R i)"
  1.3852 +  show ?t1 ?t2
  1.3853 +    unfolding interval_split[OF k] integrable_cauchy
  1.3854 +    unfolding interval_split[symmetric,OF k]
  1.3855 +  proof (rule_tac[!] allI impI)+
  1.3856 +    fix e :: real
  1.3857 +    assume "e > 0"
  1.3858 +    then have "e/2>0"
  1.3859 +      by auto
  1.3860 +    from has_integral_separate_sides[OF y this k,of c] guess d . note d=this[rule_format]
  1.3861 +    let ?P = "\<lambda>A. \<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of (cbox a b) \<inter> A \<and> d fine p1 \<and>
  1.3862 +      p2 tagged_division_of (cbox a b) \<inter> A \<and> d fine p2 \<longrightarrow>
  1.3863 +      norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e)"
  1.3864 +    show "?P {x. x \<bullet> k \<le> c}"
  1.3865 +    proof (rule_tac x=d in exI, clarsimp simp add: d)
  1.3866 +      fix p1 p2
  1.3867 +      assume as: "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p1"
  1.3868 +                 "p2 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p2"
  1.3869 +      show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  1.3870 +      proof (rule fine_division_exists[OF d(1), of a' b] )
  1.3871 +        fix p
  1.3872 +        assume "p tagged_division_of cbox a' b" "d fine p"
  1.3873 +        then show ?thesis
  1.3874 +          using as norm_triangle_half_l[OF d(2)[of p1 p] d(2)[of p2 p]]
  1.3875 +          unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  1.3876 +          by (auto simp add: algebra_simps)
  1.3877 +      qed
  1.3878 +    qed
  1.3879 +    show "?P {x. x \<bullet> k \<ge> c}"
  1.3880 +    proof (rule_tac x=d in exI, clarsimp simp add: d)
  1.3881 +      fix p1 p2
  1.3882 +      assume as: "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<ge> c}" "d fine p1"
  1.3883 +                 "p2 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<ge> c}" "d fine p2"
  1.3884 +      show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  1.3885 +      proof (rule fine_division_exists[OF d(1), of a b'] )
  1.3886 +        fix p
  1.3887 +        assume "p tagged_division_of cbox a b'" "d fine p"
  1.3888 +        then show ?thesis
  1.3889 +          using as norm_triangle_half_l[OF d(2)[of p p1] d(2)[of p p2]]
  1.3890 +          unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  1.3891 +          by (auto simp add: algebra_simps)
  1.3892 +      qed
  1.3893 +    qed
  1.3894 +  qed
  1.3895 +qed
  1.3896 +
  1.3897 +lemma operative_integral:
  1.3898 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  1.3899 +  shows "comm_monoid.operative (lift_option op +) (Some 0) (\<lambda>i. if f integrable_on i then Some (integral i f) else None)"
  1.3900 +  unfolding comm_monoid.operative_def[OF add.comm_monoid_lift_option]
  1.3901 +proof safe
  1.3902 +  fix a b c
  1.3903 +  fix k :: 'a
  1.3904 +  assume k: "k \<in> Basis"
  1.3905 +  show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) =
  1.3906 +        lift_option op + (if f integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c} then Some (integral (cbox a b \<inter> {x. x \<bullet> k \<le> c}) f) else None)
  1.3907 +        (if f integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k} then Some (integral (cbox a b \<inter> {x. c \<le> x \<bullet> k}) f) else None)"
  1.3908 +  proof (cases "f integrable_on cbox a b")
  1.3909 +    case True
  1.3910 +    with k show ?thesis
  1.3911 +      apply (simp add: integrable_split)
  1.3912 +      apply (rule integral_unique [OF has_integral_split[OF _ _ k]])
  1.3913 +      apply (auto intro: integrable_integral)
  1.3914 +      done
  1.3915 +  next
  1.3916 +    case False
  1.3917 +    have "\<not> (f integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}) \<or> \<not> ( f integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k})"
  1.3918 +    proof (rule ccontr)
  1.3919 +      assume "\<not> ?thesis"
  1.3920 +      then have "f integrable_on cbox a b"
  1.3921 +        unfolding integrable_on_def
  1.3922 +        apply (rule_tac x="integral (cbox a b \<inter> {x. x \<bullet> k \<le> c}) f + integral (cbox a b \<inter> {x. x \<bullet> k \<ge> c}) f" in exI)
  1.3923 +        apply (rule has_integral_split[OF _ _ k])
  1.3924 +        apply (auto intro: integrable_integral)
  1.3925 +        done
  1.3926 +      then show False
  1.3927 +        using False by auto
  1.3928 +    qed
  1.3929 +    then show ?thesis
  1.3930 +      using False by auto
  1.3931 +  qed
  1.3932 +next
  1.3933 +  fix a b :: 'a
  1.3934 +  assume "content (cbox a b) = 0"
  1.3935 +  then show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) = Some 0"
  1.3936 +    using has_integral_null_eq
  1.3937 +    by (auto simp: integrable_on_null)
  1.3938 +qed
  1.3939 +
  1.3940 +subsection \<open>Finally, the integral of a constant\<close>
  1.3941 +
  1.3942 +lemma has_integral_const [intro]:
  1.3943 +  fixes a b :: "'a::euclidean_space"
  1.3944 +  shows "((\<lambda>x. c) has_integral (content (cbox a b) *\<^sub>R c)) (cbox a b)"
  1.3945 +  apply (auto intro!: exI [where x="\<lambda>x. ball x 1"] simp: split_def has_integral)
  1.3946 +  apply (subst scaleR_left.setsum[symmetric, unfolded o_def])
  1.3947 +  apply (subst additive_content_tagged_division[unfolded split_def])
  1.3948 +  apply auto
  1.3949 +  done
  1.3950 +
  1.3951 +lemma has_integral_const_real [intro]:
  1.3952 +  fixes a b :: real
  1.3953 +  shows "((\<lambda>x. c) has_integral (content {a .. b} *\<^sub>R c)) {a .. b}"
  1.3954 +  by (metis box_real(2) has_integral_const)
  1.3955 +
  1.3956 +lemma integral_const [simp]:
  1.3957 +  fixes a b :: "'a::euclidean_space"
  1.3958 +  shows "integral (cbox a b) (\<lambda>x. c) = content (cbox a b) *\<^sub>R c"
  1.3959 +  by (rule integral_unique) (rule has_integral_const)
  1.3960 +
  1.3961 +lemma integral_const_real [simp]:
  1.3962 +  fixes a b :: real
  1.3963 +  shows "integral {a .. b} (\<lambda>x. c) = content {a .. b} *\<^sub>R c"
  1.3964 +  by (metis box_real(2) integral_const)
  1.3965 +
  1.3966 +
  1.3967 +subsection \<open>Bounds on the norm of Riemann sums and the integral itself.\<close>
  1.3968 +
  1.3969 +lemma dsum_bound:
  1.3970 +  assumes "p division_of (cbox a b)"
  1.3971 +    and "norm c \<le> e"
  1.3972 +  shows "norm (setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> e * content(cbox a b)"
  1.3973 +proof -
  1.3974 +  have sumeq: "(\<Sum>i\<in>p. \<bar>content i\<bar>) = setsum content p"
  1.3975 +    apply (rule setsum.cong)
  1.3976 +    using assms
  1.3977 +    apply simp
  1.3978 +    apply (metis abs_of_nonneg assms(1) content_pos_le division_ofD(4))
  1.3979 +    done
  1.3980 +  have e: "0 \<le> e"
  1.3981 +    using assms(2) norm_ge_zero order_trans by blast
  1.3982 +  have "norm (setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> (\<Sum>i\<in>p. norm (content i *\<^sub>R c))"
  1.3983 +    using norm_setsum by blast
  1.3984 +  also have "...  \<le> e * (\<Sum>i\<in>p. \<bar>content i\<bar>)"
  1.3985 +    apply (simp add: setsum_right_distrib[symmetric] mult.commute)
  1.3986 +    using assms(2) mult_right_mono by blast
  1.3987 +  also have "... \<le> e * content (cbox a b)"
  1.3988 +    apply (rule mult_left_mono [OF _ e])
  1.3989 +    apply (simp add: sumeq)
  1.3990 +    using additive_content_division assms(1) eq_iff apply blast
  1.3991 +    done
  1.3992 +  finally show ?thesis .
  1.3993 +qed
  1.3994 +
  1.3995 +lemma rsum_bound:
  1.3996 +  assumes p: "p tagged_division_of (cbox a b)"
  1.3997 +      and "\<forall>x\<in>cbox a b. norm (f x) \<le> e"
  1.3998 +    shows "norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> e * content (cbox a b)"
  1.3999 +proof (cases "cbox a b = {}")
  1.4000 +  case True show ?thesis
  1.4001 +    using p unfolding True tagged_division_of_trivial by auto
  1.4002 +next
  1.4003 +  case False
  1.4004 +  then have e: "e \<ge> 0"
  1.4005 +    by (meson ex_in_conv assms(2) norm_ge_zero order_trans)
  1.4006 +  have setsum_le: "setsum (content \<circ> snd) p \<le> content (cbox a b)"
  1.4007 +    unfolding additive_content_tagged_division[OF p, symmetric] split_def
  1.4008 +    by (auto intro: eq_refl)
  1.4009 +  have con: "\<And>xk. xk \<in> p \<Longrightarrow> 0 \<le> content (snd xk)"
  1.4010 +    using tagged_division_ofD(4) [OF p] content_pos_le
  1.4011 +    by force
  1.4012 +  have norm: "\<And>xk. xk \<in> p \<Longrightarrow> norm (f (fst xk)) \<le> e"
  1.4013 +    unfolding fst_conv using tagged_division_ofD(2,3)[OF p] assms
  1.4014 +    by (metis prod.collapse subset_eq)
  1.4015 +  have "norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> (\<Sum>i\<in>p. norm (case i of (x, k) \<Rightarrow> content k *\<^sub>R f x))"
  1.4016 +    by (rule norm_setsum)
  1.4017 +  also have "...  \<le> e * content (cbox a b)"
  1.4018 +    unfolding split_def norm_scaleR
  1.4019 +    apply (rule order_trans[OF setsum_mono])
  1.4020 +    apply (rule mult_left_mono[OF _ abs_ge_zero, of _ e])
  1.4021 +    apply (metis norm)
  1.4022 +    unfolding setsum_left_distrib[symmetric]
  1.4023 +    using con setsum_le
  1.4024 +    apply (auto simp: mult.commute intro: mult_left_mono [OF _ e])
  1.4025 +    done
  1.4026 +  finally show ?thesis .
  1.4027 +qed
  1.4028 +
  1.4029 +lemma rsum_diff_bound:
  1.4030 +  assumes "p tagged_division_of (cbox a b)"
  1.4031 +    and "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e"
  1.4032 +  shows "norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - setsum (\<lambda>(x,k). content k *\<^sub>R g x) p) \<le>
  1.4033 +         e * content (cbox a b)"
  1.4034 +  apply (rule order_trans[OF _ rsum_bound[OF assms]])
  1.4035 +  apply (simp add: split_def scaleR_diff_right setsum_subtractf eq_refl)
  1.4036 +  done
  1.4037 +
  1.4038 +lemma has_integral_bound:
  1.4039 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1.4040 +  assumes "0 \<le> B"
  1.4041 +      and "(f has_integral i) (cbox a b)"
  1.4042 +      and "\<forall>x\<in>cbox a b. norm (f x) \<le> B"
  1.4043 +    shows "norm i \<le> B * content (cbox a b)"
  1.4044 +proof (rule ccontr)
  1.4045 +  assume "\<not> ?thesis"
  1.4046 +  then have *: "norm i - B * content (cbox a b) > 0"
  1.4047 +    by auto
  1.4048 +  from assms(2)[unfolded has_integral,rule_format,OF *]
  1.4049 +  guess d by (elim exE conjE) note d=this[rule_format]
  1.4050 +  from fine_division_exists[OF this(1), of a b] guess p . note p=this
  1.4051 +  have *: "\<And>s B. norm s \<le> B \<Longrightarrow> \<not> norm (s - i) < norm i - B"
  1.4052 +    unfolding not_less
  1.4053 +    by (metis norm_triangle_sub[of i] add.commute le_less_trans less_diff_eq linorder_not_le norm_minus_commute)
  1.4054 +  show False
  1.4055 +    using d(2)[OF conjI[OF p]] *[OF rsum_bound[OF p(1) assms(3)]] by auto
  1.4056 +qed
  1.4057 +
  1.4058 +corollary has_integral_bound_real:
  1.4059 +  fixes f :: "real \<Rightarrow> 'b::real_normed_vector"
  1.4060 +  assumes "0 \<le> B"
  1.4061 +      and "(f has_integral i) {a .. b}"
  1.4062 +      and "\<forall>x\<in>{a .. b}. norm (f x) \<le> B"
  1.4063 +    shows "norm i \<le> B * content {a .. b}"
  1.4064 +  by (metis assms box_real(2) has_integral_bound)
  1.4065 +
  1.4066 +corollary integrable_bound:
  1.4067 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1.4068 +  assumes "0 \<le> B"
  1.4069 +      and "f integrable_on (cbox a b)"
  1.4070 +      and "\<And>x. x\<in>cbox a b \<Longrightarrow> norm (f x) \<le> B"
  1.4071 +    shows "norm (integral (cbox a b) f) \<le> B * content (cbox a b)"
  1.4072 +by (metis integrable_integral has_integral_bound assms)
  1.4073 +
  1.4074 +
  1.4075 +subsection \<open>Similar theorems about relationship among components.\<close>
  1.4076 +
  1.4077 +lemma rsum_component_le:
  1.4078 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.4079 +  assumes "p tagged_division_of (cbox a b)"
  1.4080 +      and "\<forall>x\<in>cbox a b. (f x)\<bullet>i \<le> (g x)\<bullet>i"
  1.4081 +    shows "(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p)\<bullet>i \<le> (setsum (\<lambda>(x,k). content k *\<^sub>R g x) p)\<bullet>i"
  1.4082 +unfolding inner_setsum_left
  1.4083 +proof (rule setsum_mono, clarify)
  1.4084 +  fix a b
  1.4085 +  assume ab: "(a, b) \<in> p"
  1.4086 +  note tagged = tagged_division_ofD(2-4)[OF assms(1) ab]
  1.4087 +  from this(3) guess u v by (elim exE) note b=this
  1.4088 +  show "(content b *\<^sub>R f a) \<bullet> i \<le> (content b *\<^sub>R g a) \<bullet> i"
  1.4089 +    unfolding b inner_simps real_scaleR_def
  1.4090 +    apply (rule mult_left_mono)
  1.4091 +    using assms(2) tagged
  1.4092 +    by (auto simp add: content_pos_le)
  1.4093 +qed
  1.4094 +
  1.4095 +lemma has_integral_component_le:
  1.4096 +  fixes f g :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.4097 +  assumes k: "k \<in> Basis"
  1.4098 +  assumes "(f has_integral i) s" "(g has_integral j) s"
  1.4099 +    and "\<forall>x\<in>s. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  1.4100 +  shows "i\<bullet>k \<le> j\<bullet>k"
  1.4101 +proof -
  1.4102 +  have lem: "i\<bullet>k \<le> j\<bullet>k"
  1.4103 +    if f_i: "(f has_integral i) (cbox a b)"
  1.4104 +    and g_j: "(g has_integral j) (cbox a b)"
  1.4105 +    and le: "\<forall>x\<in>cbox a b. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  1.4106 +    for a b i and j :: 'b and f g :: "'a \<Rightarrow> 'b"
  1.4107 +  proof (rule ccontr)
  1.4108 +    assume "\<not> ?thesis"
  1.4109 +    then have *: "0 < (i\<bullet>k - j\<bullet>k) / 3"
  1.4110 +      by auto
  1.4111 +    guess d1 using f_i[unfolded has_integral,rule_format,OF *] by (elim exE conjE) note d1=this[rule_format]
  1.4112 +    guess d2 using g_j[unfolded has_integral,rule_format,OF *] by (elim exE conjE) note d2=this[rule_format]
  1.4113 +    obtain p where p: "p tagged_division_of cbox a b" "d1 fine p" "d2 fine p"
  1.4114 +       using fine_division_exists[OF gauge_inter[OF d1(1) d2(1)], of a b] unfolding fine_inter
  1.4115 +       by metis
  1.4116 +    note le_less_trans[OF Basis_le_norm[OF k]]
  1.4117 +    then have "\<bar>((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i) \<bullet> k\<bar> < (i \<bullet> k - j \<bullet> k) / 3"
  1.4118 +              "\<bar>((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - j) \<bullet> k\<bar> < (i \<bullet> k - j \<bullet> k) / 3"
  1.4119 +      using  k norm_bound_Basis_lt d1 d2 p
  1.4120 +      by blast+
  1.4121 +    then show False
  1.4122 +      unfolding inner_simps
  1.4123 +      using rsum_component_le[OF p(1) le]
  1.4124 +      by (simp add: abs_real_def split: if_split_asm)
  1.4125 +  qed
  1.4126 +  show ?thesis
  1.4127 +  proof (cases "\<exists>a b. s = cbox a b")
  1.4128 +    case True
  1.4129 +    with lem assms show ?thesis
  1.4130 +      by auto
  1.4131 +  next
  1.4132 +    case False
  1.4133 +    show ?thesis
  1.4134 +    proof (rule ccontr)
  1.4135 +      assume "\<not> i\<bullet>k \<le> j\<bullet>k"
  1.4136 +      then have ij: "(i\<bullet>k - j\<bullet>k) / 3 > 0"
  1.4137 +        by auto
  1.4138 +      note has_integral_altD[OF _ False this]
  1.4139 +      from this[OF assms(2)] this[OF assms(3)] guess B1 B2 . note B=this[rule_format]
  1.4140 +      have "bounded (ball 0 B1 \<union> ball (0::'a) B2)"
  1.4141 +        unfolding bounded_Un by(rule conjI bounded_ball)+
  1.4142 +      from bounded_subset_cbox[OF this] guess a b by (elim exE)
  1.4143 +      note ab = conjunctD2[OF this[unfolded Un_subset_iff]]
  1.4144 +      guess w1 using B(2)[OF ab(1)] .. note w1=conjunctD2[OF this]
  1.4145 +      guess w2 using B(4)[OF ab(2)] .. note w2=conjunctD2[OF this]
  1.4146 +      have *: "\<And>w1 w2 j i::real .\<bar>w1 - i\<bar> < (i - j) / 3 \<Longrightarrow> \<bar>w2 - j\<bar> < (i - j) / 3 \<Longrightarrow> w1 \<le> w2 \<Longrightarrow> False"
  1.4147 +        by (simp add: abs_real_def split: if_split_asm)
  1.4148 +      note le_less_trans[OF Basis_le_norm[OF k]]
  1.4149 +      note this[OF w1(2)] this[OF w2(2)]
  1.4150 +      moreover
  1.4151 +      have "w1\<bullet>k \<le> w2\<bullet>k"
  1.4152 +        by (rule lem[OF w1(1) w2(1)]) (simp add: assms(4))
  1.4153 +      ultimately show False
  1.4154 +        unfolding inner_simps by(rule *)
  1.4155 +    qed
  1.4156 +  qed
  1.4157 +qed
  1.4158 +
  1.4159 +lemma integral_component_le:
  1.4160 +  fixes g f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.4161 +  assumes "k \<in> Basis"
  1.4162 +    and "f integrable_on s" "g integrable_on s"
  1.4163 +    and "\<forall>x\<in>s. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  1.4164 +  shows "(integral s f)\<bullet>k \<le> (integral s g)\<bullet>k"
  1.4165 +  apply (rule has_integral_component_le)
  1.4166 +  using integrable_integral assms
  1.4167 +  apply auto
  1.4168 +  done
  1.4169 +
  1.4170 +lemma has_integral_component_nonneg:
  1.4171 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.4172 +  assumes "k \<in> Basis"
  1.4173 +    and "(f has_integral i) s"
  1.4174 +    and "\<forall>x\<in>s. 0 \<le> (f x)\<bullet>k"
  1.4175 +  shows "0 \<le> i\<bullet>k"
  1.4176 +  using has_integral_component_le[OF assms(1) has_integral_0 assms(2)]
  1.4177 +  using assms(3-)
  1.4178 +  by auto
  1.4179 +
  1.4180 +lemma integral_component_nonneg:
  1.4181 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.4182 +  assumes "k \<in> Basis"
  1.4183 +    and  "\<forall>x\<in>s. 0 \<le> (f x)\<bullet>k"
  1.4184 +  shows "0 \<le> (integral s f)\<bullet>k"
  1.4185 +proof (cases "f integrable_on s")
  1.4186 +  case True show ?thesis
  1.4187 +    apply (rule has_integral_component_nonneg)
  1.4188 +    using assms True
  1.4189 +    apply auto
  1.4190 +    done
  1.4191 +next
  1.4192 +  case False then show ?thesis by (simp add: not_integrable_integral)
  1.4193 +qed
  1.4194 +
  1.4195 +lemma has_integral_component_neg:
  1.4196 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.4197 +  assumes "k \<in> Basis"
  1.4198 +    and "(f has_integral i) s"
  1.4199 +    and "\<forall>x\<in>s. (f x)\<bullet>k \<le> 0"
  1.4200 +  shows "i\<bullet>k \<le> 0"
  1.4201 +  using has_integral_component_le[OF assms(1,2) has_integral_0] assms(2-)
  1.4202 +  by auto
  1.4203 +
  1.4204 +lemma has_integral_component_lbound:
  1.4205 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.4206 +  assumes "(f has_integral i) (cbox a b)"
  1.4207 +    and "\<forall>x\<in>cbox a b. B \<le> f(x)\<bullet>k"
  1.4208 +    and "k \<in> Basis"
  1.4209 +  shows "B * content (cbox a b) \<le> i\<bullet>k"
  1.4210 +  using has_integral_component_le[OF assms(3) has_integral_const assms(1),of "(\<Sum>i\<in>Basis. B *\<^sub>R i)::'b"] assms(2-)
  1.4211 +  by (auto simp add: field_simps)
  1.4212 +
  1.4213 +lemma has_integral_component_ubound:
  1.4214 +  fixes f::"'a::euclidean_space => 'b::euclidean_space"
  1.4215 +  assumes "(f has_integral i) (cbox a b)"
  1.4216 +    and "\<forall>x\<in>cbox a b. f x\<bullet>k \<le> B"
  1.4217 +    and "k \<in> Basis"
  1.4218 +  shows "i\<bullet>k \<le> B * content (cbox a b)"
  1.4219 +  using has_integral_component_le[OF assms(3,1) has_integral_const, of "\<Sum>i\<in>Basis. B *\<^sub>R i"] assms(2-)
  1.4220 +  by (auto simp add: field_simps)
  1.4221 +
  1.4222 +lemma integral_component_lbound:
  1.4223 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.4224 +  assumes "f integrable_on cbox a b"
  1.4225 +    and "\<forall>x\<in>cbox a b. B \<le> f(x)\<bullet>k"
  1.4226 +    and "k \<in> Basis"
  1.4227 +  shows "B * content (cbox a b) \<le> (integral(cbox a b) f)\<bullet>k"
  1.4228 +  apply (rule has_integral_component_lbound)
  1.4229 +  using assms
  1.4230 +  unfolding has_integral_integral
  1.4231 +  apply auto
  1.4232 +  done
  1.4233 +
  1.4234 +lemma integral_component_lbound_real:
  1.4235 +  assumes "f integrable_on {a ::real .. b}"
  1.4236 +    and "\<forall>x\<in>{a .. b}. B \<le> f(x)\<bullet>k"
  1.4237 +    and "k \<in> Basis"
  1.4238 +  shows "B * content {a .. b} \<le> (integral {a .. b} f)\<bullet>k"
  1.4239 +  using assms
  1.4240 +  by (metis box_real(2) integral_component_lbound)
  1.4241 +
  1.4242 +lemma integral_component_ubound:
  1.4243 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.4244 +  assumes "f integrable_on cbox a b"
  1.4245 +    and "\<forall>x\<in>cbox a b. f x\<bullet>k \<le> B"
  1.4246 +    and "k \<in> Basis"
  1.4247 +  shows "(integral (cbox a b) f)\<bullet>k \<le> B * content (cbox a b)"
  1.4248 +  apply (rule has_integral_component_ubound)
  1.4249 +  using assms
  1.4250 +  unfolding has_integral_integral
  1.4251 +  apply auto
  1.4252 +  done
  1.4253 +
  1.4254 +lemma integral_component_ubound_real:
  1.4255 +  fixes f :: "real \<Rightarrow> 'a::euclidean_space"
  1.4256 +  assumes "f integrable_on {a .. b}"
  1.4257 +    and "\<forall>x\<in>{a .. b}. f x\<bullet>k \<le> B"
  1.4258 +    and "k \<in> Basis"
  1.4259 +  shows "(integral {a .. b} f)\<bullet>k \<le> B * content {a .. b}"
  1.4260 +  using assms
  1.4261 +  by (metis box_real(2) integral_component_ubound)
  1.4262 +
  1.4263 +subsection \<open>Uniform limit of integrable functions is integrable.\<close>
  1.4264 +
  1.4265 +lemma real_arch_invD:
  1.4266 +  "0 < (e::real) \<Longrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
  1.4267 +  by (subst(asm) real_arch_inverse)
  1.4268 +
  1.4269 +lemma integrable_uniform_limit:
  1.4270 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  1.4271 +  assumes "\<forall>e>0. \<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  1.4272 +  shows "f integrable_on cbox a b"
  1.4273 +proof (cases "content (cbox a b) > 0")
  1.4274 +  case False then show ?thesis
  1.4275 +      using has_integral_null
  1.4276 +      by (simp add: content_lt_nz integrable_on_def)
  1.4277 +next
  1.4278 +  case True
  1.4279 +  have *: "\<And>P. \<forall>e>(0::real). P e \<Longrightarrow> \<forall>n::nat. P (inverse (real n + 1))"
  1.4280 +    by auto
  1.4281 +  from choice[OF *[OF assms]] guess g .. note g=conjunctD2[OF this[rule_format],rule_format]
  1.4282 +  from choice[OF allI[OF g(2)[unfolded integrable_on_def], of "\<lambda>x. x"]]
  1.4283 +  obtain i where i: "\<And>x. (g x has_integral i x) (cbox a b)"
  1.4284 +      by auto
  1.4285 +  have "Cauchy i"
  1.4286 +    unfolding Cauchy_def
  1.4287 +  proof clarify
  1.4288 +    fix e :: real
  1.4289 +    assume "e>0"
  1.4290 +    then have "e / 4 / content (cbox a b) > 0"
  1.4291 +      using True by (auto simp add: field_simps)
  1.4292 +    then obtain M :: nat
  1.4293 +         where M: "M \<noteq> 0" "0 < inverse (real_of_nat M)" "inverse (of_nat M) < e / 4 / content (cbox a b)"
  1.4294 +      by (subst (asm) real_arch_inverse) auto
  1.4295 +    show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (i m) (i n) < e"
  1.4296 +    proof (rule exI [where x=M], clarify)
  1.4297 +      fix m n
  1.4298 +      assume m: "M \<le> m" and n: "M \<le> n"
  1.4299 +      have "e/4>0" using \<open>e>0\<close> by auto
  1.4300 +      note * = i[unfolded has_integral,rule_format,OF this]
  1.4301 +      from *[of m] guess gm by (elim conjE exE) note gm=this[rule_format]
  1.4302 +      from *[of n] guess gn by (elim conjE exE) note gn=this[rule_format]
  1.4303 +      from fine_division_exists[OF gauge_inter[OF gm(1) gn(1)], of a b]
  1.4304 +      obtain p where p: "p tagged_division_of cbox a b" "(\<lambda>x. gm x \<inter> gn x) fine p"
  1.4305 +        by auto
  1.4306 +      { fix s1 s2 i1 and i2::'b
  1.4307 +        assume no: "norm(s2 - s1) \<le> e/2" "norm (s1 - i1) < e/4" "norm (s2 - i2) < e/4"
  1.4308 +        have "norm (i1 - i2) \<le> norm (i1 - s1) + norm (s1 - s2) + norm (s2 - i2)"
  1.4309 +          using norm_triangle_ineq[of "i1 - s1" "s1 - i2"]
  1.4310 +          using norm_triangle_ineq[of "s1 - s2" "s2 - i2"]
  1.4311 +          by (auto simp add: algebra_simps)
  1.4312 +        also have "\<dots> < e"
  1.4313 +          using no
  1.4314 +          unfolding norm_minus_commute
  1.4315 +          by (auto simp add: algebra_simps)
  1.4316 +        finally have "norm (i1 - i2) < e" .
  1.4317 +      } note triangle3 = this
  1.4318 +      have finep: "gm fine p" "gn fine p"
  1.4319 +        using fine_inter p  by auto
  1.4320 +      { fix x
  1.4321 +        assume x: "x \<in> cbox a b"
  1.4322 +        have "norm (f x - g n x) + norm (f x - g m x) \<le> inverse (real n + 1) + inverse (real m + 1)"
  1.4323 +          using g(1)[OF x, of n] g(1)[OF x, of m] by auto
  1.4324 +        also have "\<dots> \<le> inverse (real M) + inverse (real M)"
  1.4325 +          apply (rule add_mono)
  1.4326 +          using M(2) m n by auto
  1.4327 +        also have "\<dots> = 2 / real M"
  1.4328 +          unfolding divide_inverse by auto
  1.4329 +        finally have "norm (g n x - g m x) \<le> 2 / real M"
  1.4330 +          using norm_triangle_le[of "g n x - f x" "f x - g m x" "2 / real M"]
  1.4331 +          by (auto simp add: algebra_simps simp add: norm_minus_commute)
  1.4332 +      } note norm_le = this
  1.4333 +      have le_e2: "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g n x) - (\<Sum>(x, k)\<in>p. content k *\<^sub>R g m x)) \<le> e / 2"
  1.4334 +        apply (rule order_trans [OF rsum_diff_bound[OF p(1), where e="2 / real M"]])
  1.4335 +        apply (blast intro: norm_le)
  1.4336 +        using M True
  1.4337 +        by (auto simp add: field_simps)
  1.4338 +      then show "dist (i m) (i n) < e"
  1.4339 +        unfolding dist_norm
  1.4340 +        using gm gn p finep
  1.4341 +        by (auto intro!: triangle3)
  1.4342 +    qed
  1.4343 +  qed
  1.4344 +  then obtain s where s: "i \<longlonglongrightarrow> s"
  1.4345 +    using convergent_eq_cauchy[symmetric] by blast
  1.4346 +  show ?thesis
  1.4347 +    unfolding integrable_on_def has_integral
  1.4348 +  proof (rule_tac x=s in exI, clarify)
  1.4349 +    fix e::real
  1.4350 +    assume e: "0 < e"
  1.4351 +    then have *: "e/3 > 0" by auto
  1.4352 +    then obtain N1 where N1: "\<forall>n\<ge>N1. norm (i n - s) < e / 3"
  1.4353 +      using LIMSEQ_D [OF s] by metis
  1.4354 +    from e True have "e / 3 / content (cbox a b) > 0"
  1.4355 +      by (auto simp add: field_simps)
  1.4356 +    from real_arch_invD[OF this] guess N2 by (elim exE conjE) note N2=this
  1.4357 +    from i[of "N1 + N2",unfolded has_integral,rule_format,OF *] guess g' .. note g'=conjunctD2[OF this,rule_format]
  1.4358 +    { fix sf sg i
  1.4359 +      assume no: "norm (sf - sg) \<le> e / 3"
  1.4360 +                 "norm(i - s) < e / 3"
  1.4361 +                 "norm (sg - i) < e / 3"
  1.4362 +      have "norm (sf - s) \<le> norm (sf - sg) + norm (sg - i) + norm (i - s)"
  1.4363 +        using norm_triangle_ineq[of "sf - sg" "sg - s"]
  1.4364 +        using norm_triangle_ineq[of "sg -  i" " i - s"]
  1.4365 +        by (auto simp add: algebra_simps)
  1.4366 +      also have "\<dots> < e"
  1.4367 +        using no
  1.4368 +        unfolding norm_minus_commute
  1.4369 +        by (auto simp add: algebra_simps)
  1.4370 +      finally have "norm (sf - s) < e" .
  1.4371 +    } note lem = this
  1.4372 +    { fix p
  1.4373 +      assume p: "p tagged_division_of (cbox a b) \<and> g' fine p"
  1.4374 +      then have norm_less: "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g (N1 + N2) x) - i (N1 + N2)) < e / 3"
  1.4375 +        using g' by blast
  1.4376 +      have "content (cbox a b) < e / 3 * (of_nat N2)"
  1.4377 +        using N2 unfolding inverse_eq_divide using True by (auto simp add: field_simps)
  1.4378 +      moreover have "e / 3 * of_nat N2 \<le> e / 3 * (of_nat (N1 + N2) + 1)"
  1.4379 +        using \<open>e>0\<close> by auto
  1.4380 +      ultimately have "content (cbox a b) < e / 3 * (of_nat (N1 + N2) + 1)"
  1.4381 +        by linarith
  1.4382 +      then have le_e3: "inverse (real (N1 + N2) + 1) * content (cbox a b) \<le> e / 3"
  1.4383 +        unfolding inverse_eq_divide
  1.4384 +        by (auto simp add: field_simps)
  1.4385 +      have ne3: "norm (i (N1 + N2) - s) < e / 3"
  1.4386 +        using N1 by auto
  1.4387 +      have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - s) < e"
  1.4388 +        apply (rule lem[OF order_trans [OF _ le_e3] ne3 norm_less])
  1.4389 +        apply (rule rsum_diff_bound[OF p[THEN conjunct1]])
  1.4390 +        apply (blast intro: g)
  1.4391 +        done }
  1.4392 +    then show "\<exists>d. gauge d \<and>
  1.4393 +             (\<forall>p. p tagged_division_of cbox a b \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - s) < e)"
  1.4394 +      by (blast intro: g')
  1.4395 +  qed
  1.4396 +qed
  1.4397 +
  1.4398 +lemmas integrable_uniform_limit_real = integrable_uniform_limit [where 'a=real, simplified]
  1.4399 +
  1.4400 +
  1.4401 +subsection \<open>Negligible sets.\<close>
  1.4402 +
  1.4403 +definition "negligible (s:: 'a::euclidean_space set) \<longleftrightarrow>
  1.4404 +  (\<forall>a b. ((indicator s :: 'a\<Rightarrow>real) has_integral 0) (cbox a b))"
  1.4405 +
  1.4406 +
  1.4407 +subsection \<open>Negligibility of hyperplane.\<close>
  1.4408 +
  1.4409 +lemma interval_doublesplit:
  1.4410 +  fixes a :: "'a::euclidean_space"
  1.4411 +  assumes "k \<in> Basis"
  1.4412 +  shows "cbox a b \<inter> {x . \<bar>x\<bullet>k - c\<bar> \<le> (e::real)} =
  1.4413 +    cbox (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) (c - e) else a\<bullet>i) *\<^sub>R i)
  1.4414 +     (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) (c + e) else b\<bullet>i) *\<^sub>R i)"
  1.4415 +proof -
  1.4416 +  have *: "\<And>x c e::real. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  1.4417 +    by auto
  1.4418 +  have **: "\<And>s P Q. s \<inter> {x. P x \<and> Q x} = (s \<inter> {x. Q x}) \<inter> {x. P x}"
  1.4419 +    by blast
  1.4420 +  show ?thesis
  1.4421 +    unfolding * ** interval_split[OF assms] by (rule refl)
  1.4422 +qed
  1.4423 +
  1.4424 +lemma division_doublesplit:
  1.4425 +  fixes a :: "'a::euclidean_space"
  1.4426 +  assumes "p division_of (cbox a b)"
  1.4427 +    and k: "k \<in> Basis"
  1.4428 +  shows "(\<lambda>l. l \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> e}) ` {l\<in>p. l \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> e} \<noteq> {}}
  1.4429 +         division_of  (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> e})"
  1.4430 +proof -
  1.4431 +  have *: "\<And>x c. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  1.4432 +    by auto
  1.4433 +  have **: "\<And>p q p' q'. p division_of q \<Longrightarrow> p = p' \<Longrightarrow> q = q' \<Longrightarrow> p' division_of q'"
  1.4434 +    by auto
  1.4435 +  note division_split(1)[OF assms, where c="c+e",unfolded interval_split[OF k]]
  1.4436 +  note division_split(2)[OF this, where c="c-e" and k=k,OF k]
  1.4437 +  then show ?thesis
  1.4438 +    apply (rule **)
  1.4439 +    subgoal
  1.4440 +      apply (simp add: abs_diff_le_iff field_simps Collect_conj_eq setcompr_eq_image[symmetric])
  1.4441 +      apply (rule equalityI)
  1.4442 +      apply blast
  1.4443 +      apply clarsimp
  1.4444 +      apply (rule_tac x="l \<inter> {x. c + e \<ge> x \<bullet> k}" in exI)
  1.4445 +      apply auto
  1.4446 +      done
  1.4447 +    by (simp add: interval_split k interval_doublesplit)
  1.4448 +qed
  1.4449 +
  1.4450 +lemma content_doublesplit:
  1.4451 +  fixes a :: "'a::euclidean_space"
  1.4452 +  assumes "0 < e"
  1.4453 +    and k: "k \<in> Basis"
  1.4454 +  obtains d where "0 < d" and "content (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d}) < e"
  1.4455 +proof (cases "content (cbox a b) = 0")
  1.4456 +  case True
  1.4457 +  then have ce: "content (cbox a b) < e"
  1.4458 +    by (metis \<open>0 < e\<close>)
  1.4459 +  show ?thesis
  1.4460 +    apply (rule that[of 1])
  1.4461 +    apply simp
  1.4462 +    unfolding interval_doublesplit[OF k]
  1.4463 +    apply (rule le_less_trans[OF content_subset ce])
  1.4464 +    apply (auto simp: interval_doublesplit[symmetric] k)
  1.4465 +    done
  1.4466 +next
  1.4467 +  case False
  1.4468 +  define d where "d = e / 3 / setprod (\<lambda>i. b\<bullet>i - a\<bullet>i) (Basis - {k})"
  1.4469 +  note False[unfolded content_eq_0 not_ex not_le, rule_format]
  1.4470 +  then have "\<And>x. x \<in> Basis \<Longrightarrow> b\<bullet>x > a\<bullet>x"
  1.4471 +    by (auto simp add:not_le)
  1.4472 +  then have prod0: "0 < setprod (\<lambda>i. b\<bullet>i - a\<bullet>i) (Basis - {k})"
  1.4473 +    by (force simp add: setprod_pos field_simps)
  1.4474 +  then have "d > 0"
  1.4475 +    using assms
  1.4476 +    by (auto simp add: d_def field_simps)
  1.4477 +  then show ?thesis
  1.4478 +  proof (rule that[of d])
  1.4479 +    have *: "Basis = insert k (Basis - {k})"
  1.4480 +      using k by auto
  1.4481 +    have less_e: "(min (b \<bullet> k) (c + d) - max (a \<bullet> k) (c - d)) * (\<Prod>i\<in>Basis - {k}. b \<bullet> i - a \<bullet> i) < e"
  1.4482 +    proof -
  1.4483 +      have "(min (b \<bullet> k) (c + d) - max (a \<bullet> k) (c - d)) \<le> 2 * d"
  1.4484 +        by auto
  1.4485 +      also have "\<dots> < e / (\<Prod>i\<in>Basis - {k}. b \<bullet> i - a \<bullet> i)"
  1.4486 +        unfolding d_def
  1.4487 +        using assms prod0
  1.4488 +        by (auto simp add: field_simps)
  1.4489 +      finally show "(min (b \<bullet> k) (c + d) - max (a \<bullet> k) (c - d)) * (\<Prod>i\<in>Basis - {k}. b \<bullet> i - a \<bullet> i) < e"
  1.4490 +        unfolding pos_less_divide_eq[OF prod0] .
  1.4491 +    qed
  1.4492 +    show "content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) < e"
  1.4493 +    proof (cases "cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} = {}")
  1.4494 +      case True
  1.4495 +      then show ?thesis
  1.4496 +        using assms by simp
  1.4497 +    next
  1.4498 +      case False
  1.4499 +      then have
  1.4500 +          "(\<Prod>i\<in>Basis - {k}. interval_upperbound (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<bullet> i -
  1.4501 +                interval_lowerbound (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<bullet> i)
  1.4502 +           = (\<Prod>i\<in>Basis - {k}. b\<bullet>i - a\<bullet>i)"
  1.4503 +        by (simp add: box_eq_empty interval_doublesplit[OF k])
  1.4504 +      then show "content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) < e"
  1.4505 +        unfolding content_def
  1.4506 +        using assms False
  1.4507 +        apply (subst *)
  1.4508 +        apply (subst setprod.insert)
  1.4509 +        apply (simp_all add: interval_doublesplit[OF k] box_eq_empty not_less less_e)
  1.4510 +        done
  1.4511 +    qed
  1.4512 +  qed
  1.4513 +qed
  1.4514 +
  1.4515 +lemma negligible_standard_hyperplane[intro]:
  1.4516 +  fixes k :: "'a::euclidean_space"
  1.4517 +  assumes k: "k \<in> Basis"
  1.4518 +  shows "negligible {x. x\<bullet>k = c}"
  1.4519 +  unfolding negligible_def has_integral
  1.4520 +proof (clarify, goal_cases)
  1.4521 +  case (1 a b e)
  1.4522 +  from this and k obtain d where d: "0 < d" "content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) < e"
  1.4523 +    by (rule content_doublesplit)
  1.4524 +  let ?i = "indicator {x::'a. x\<bullet>k = c} :: 'a\<Rightarrow>real"
  1.4525 +  show ?case
  1.4526 +    apply (rule_tac x="\<lambda>x. ball x d" in exI)
  1.4527 +    apply rule
  1.4528 +    apply (rule gauge_ball)
  1.4529 +    apply (rule d)
  1.4530 +  proof (rule, rule)
  1.4531 +    fix p
  1.4532 +    assume p: "p tagged_division_of (cbox a b) \<and> (\<lambda>x. ball x d) fine p"
  1.4533 +    have *: "(\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) =
  1.4534 +      (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d}) *\<^sub>R ?i x)"
  1.4535 +      apply (rule setsum.cong)
  1.4536 +      apply (rule refl)
  1.4537 +      unfolding split_paired_all real_scaleR_def mult_cancel_right split_conv
  1.4538 +      apply cases
  1.4539 +      apply (rule disjI1)
  1.4540 +      apply assumption
  1.4541 +      apply (rule disjI2)
  1.4542 +    proof -
  1.4543 +      fix x l
  1.4544 +      assume as: "(x, l) \<in> p" "?i x \<noteq> 0"
  1.4545 +      then have xk: "x\<bullet>k = c"
  1.4546 +        unfolding indicator_def
  1.4547 +        apply -
  1.4548 +        apply (rule ccontr)
  1.4549 +        apply auto
  1.4550 +        done
  1.4551 +      show "content l = content (l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})"
  1.4552 +        apply (rule arg_cong[where f=content])
  1.4553 +        apply (rule set_eqI)
  1.4554 +        apply rule
  1.4555 +        apply rule
  1.4556 +        unfolding mem_Collect_eq
  1.4557 +      proof -
  1.4558 +        fix y
  1.4559 +        assume y: "y \<in> l"
  1.4560 +        note p[THEN conjunct2,unfolded fine_def,rule_format,OF as(1),unfolded split_conv]
  1.4561 +        note this[unfolded subset_eq mem_ball dist_norm,rule_format,OF y]
  1.4562 +        note le_less_trans[OF Basis_le_norm[OF k] this]
  1.4563 +        then show "\<bar>y \<bullet> k - c\<bar> \<le> d"
  1.4564 +          unfolding inner_simps xk by auto
  1.4565 +      qed auto
  1.4566 +    qed
  1.4567 +    note p'= tagged_division_ofD[OF p[THEN conjunct1]] and p''=division_of_tagged_division[OF p[THEN conjunct1]]
  1.4568 +    show "norm ((\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) - 0) < e"
  1.4569 +      unfolding diff_0_right *
  1.4570 +      unfolding real_scaleR_def real_norm_def
  1.4571 +      apply (subst abs_of_nonneg)
  1.4572 +      apply (rule setsum_nonneg)
  1.4573 +      apply rule
  1.4574 +      unfolding split_paired_all split_conv
  1.4575 +      apply (rule mult_nonneg_nonneg)
  1.4576 +      apply (drule p'(4))
  1.4577 +      apply (erule exE)+
  1.4578 +      apply(rule_tac b=b in back_subst)
  1.4579 +      prefer 2
  1.4580 +      apply (subst(asm) eq_commute)
  1.4581 +      apply assumption
  1.4582 +      apply (subst interval_doublesplit[OF k])
  1.4583 +      apply (rule content_pos_le)
  1.4584 +      apply (rule indicator_pos_le)
  1.4585 +    proof -
  1.4586 +      have "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) * ?i x) \<le>
  1.4587 +        (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}))"
  1.4588 +        apply (rule setsum_mono)
  1.4589 +        unfolding split_paired_all split_conv
  1.4590 +        apply (rule mult_right_le_one_le)
  1.4591 +        apply (drule p'(4))
  1.4592 +        apply (auto simp add:interval_doublesplit[OF k])
  1.4593 +        done
  1.4594 +      also have "\<dots> < e"
  1.4595 +      proof (subst setsum.over_tagged_division_lemma[OF p[THEN conjunct1]], goal_cases)
  1.4596 +        case prems: (1 u v)
  1.4597 +        have "content (cbox u v \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<le> content (cbox u v)"
  1.4598 +          unfolding interval_doublesplit[OF k]
  1.4599 +          apply (rule content_subset)
  1.4600 +          unfolding interval_doublesplit[symmetric,OF k]
  1.4601 +          apply auto
  1.4602 +          done
  1.4603 +        then show ?case
  1.4604 +          unfolding prems interval_doublesplit[OF k]
  1.4605 +          by (blast intro: antisym)
  1.4606 +      next
  1.4607 +        have "(\<Sum>l\<in>snd ` p. content (l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) =
  1.4608 +          setsum content ((\<lambda>l. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})`{l\<in>snd ` p. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}})"
  1.4609 +        proof (subst (2) setsum.reindex_nontrivial)
  1.4610 +          fix x y assume "x \<in> {l \<in> snd ` p. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}}" "y \<in> {l \<in> snd ` p. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}}"
  1.4611 +            "x \<noteq> y" and eq: "x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} = y \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}"
  1.4612 +          then obtain x' y' where "(x', x) \<in> p" "x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}" "(y', y) \<in> p" "y \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}"
  1.4613 +            by (auto)
  1.4614 +          from p'(5)[OF \<open>(x', x) \<in> p\<close> \<open>(y', y) \<in> p\<close>] \<open>x \<noteq> y\<close> have "interior (x \<inter> y) = {}"
  1.4615 +            by auto
  1.4616 +          moreover have "interior ((x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<inter> (y \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) \<subseteq> interior (x \<inter> y)"
  1.4617 +            by (auto intro: interior_mono)
  1.4618 +          ultimately have "interior (x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) = {}"
  1.4619 +            by (auto simp: eq)
  1.4620 +          then show "content (x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) = 0"
  1.4621 +            using p'(4)[OF \<open>(x', x) \<in> p\<close>] by (auto simp: interval_doublesplit[OF k] content_eq_0_interior simp del: interior_Int)
  1.4622 +        qed (insert p'(1), auto intro!: setsum.mono_neutral_right)
  1.4623 +        also have "\<dots> \<le> norm (\<Sum>l\<in>(\<lambda>l. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})`{l\<in>snd ` p. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}}. content l *\<^sub>R 1::real)"
  1.4624 +          by simp
  1.4625 +        also have "\<dots> \<le> 1 * content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})"
  1.4626 +          using division_doublesplit[OF p'' k, unfolded interval_doublesplit[OF k]]
  1.4627 +          unfolding interval_doublesplit[OF k] by (intro dsum_bound) auto
  1.4628 +        also have "\<dots> < e"
  1.4629 +          using d(2) by simp
  1.4630 +        finally show "(\<Sum>ka\<in>snd ` p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) < e" .
  1.4631 +      qed
  1.4632 +      finally show "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) * ?i x) < e" .
  1.4633 +    qed
  1.4634 +  qed
  1.4635 +qed
  1.4636 +
  1.4637 +
  1.4638 +subsection \<open>A technical lemma about "refinement" of division.\<close>
  1.4639 +
  1.4640 +lemma tagged_division_finer:
  1.4641 +  fixes p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  1.4642 +  assumes "p tagged_division_of (cbox a b)"
  1.4643 +    and "gauge d"
  1.4644 +  obtains q where "q tagged_division_of (cbox a b)"
  1.4645 +    and "d fine q"
  1.4646 +    and "\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q"
  1.4647 +proof -
  1.4648 +  let ?P = "\<lambda>p. p tagged_partial_division_of (cbox a b) \<longrightarrow> gauge d \<longrightarrow>
  1.4649 +    (\<exists>q. q tagged_division_of (\<Union>{k. \<exists>x. (x,k) \<in> p}) \<and> d fine q \<and>
  1.4650 +      (\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q))"
  1.4651 +  {
  1.4652 +    have *: "finite p" "p tagged_partial_division_of (cbox a b)"
  1.4653 +      using assms(1)
  1.4654 +      unfolding tagged_division_of_def
  1.4655 +      by auto
  1.4656 +    presume "\<And>p. finite p \<Longrightarrow> ?P p"
  1.4657 +    from this[rule_format,OF * assms(2)] guess q .. note q=this
  1.4658 +    then show ?thesis
  1.4659 +      apply -
  1.4660 +      apply (rule that[of q])
  1.4661 +      unfolding tagged_division_ofD[OF assms(1)]
  1.4662 +      apply auto
  1.4663 +      done
  1.4664 +  }
  1.4665 +  fix p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  1.4666 +  assume as: "finite p"
  1.4667 +  show "?P p"
  1.4668 +    apply rule
  1.4669 +    apply rule
  1.4670 +    using as
  1.4671 +  proof (induct p)
  1.4672 +    case empty
  1.4673 +    show ?case
  1.4674 +      apply (rule_tac x="{}" in exI)
  1.4675 +      unfolding fine_def
  1.4676 +      apply auto
  1.4677 +      done
  1.4678 +  next
  1.4679 +    case (insert xk p)
  1.4680 +    guess x k using surj_pair[of xk] by (elim exE) note xk=this
  1.4681 +    note tagged_partial_division_subset[OF insert(4) subset_insertI]
  1.4682 +    from insert(3)[OF this insert(5)] guess q1 .. note q1 = conjunctD3[OF this]
  1.4683 +    have *: "\<Union>{l. \<exists>y. (y,l) \<in> insert xk p} = k \<union> \<Union>{l. \<exists>y. (y,l) \<in> p}"
  1.4684 +      unfolding xk by auto
  1.4685 +    note p = tagged_partial_division_ofD[OF insert(4)]
  1.4686 +    from p(4)[unfolded xk, OF insertI1] guess u v by (elim exE) note uv=this
  1.4687 +
  1.4688 +    have "finite {k. \<exists>x. (x, k) \<in> p}"
  1.4689 +      apply (rule finite_subset[of _ "snd ` p"])
  1.4690 +      using p
  1.4691 +      apply safe
  1.4692 +      apply (metis image_iff snd_conv)
  1.4693 +      apply auto
  1.4694 +      done
  1.4695 +    then have int: "interior (cbox u v) \<inter> interior (\<Union>{k. \<exists>x. (x, k) \<in> p}) = {}"
  1.4696 +      apply (rule inter_interior_unions_intervals)
  1.4697 +      apply (rule open_interior)
  1.4698 +      apply (rule_tac[!] ballI)
  1.4699 +      unfolding mem_Collect_eq
  1.4700 +      apply (erule_tac[!] exE)
  1.4701 +      apply (drule p(4)[OF insertI2])
  1.4702 +      apply assumption
  1.4703 +      apply (rule p(5))
  1.4704 +      unfolding uv xk
  1.4705 +      apply (rule insertI1)
  1.4706 +      apply (rule insertI2)
  1.4707 +      apply assumption
  1.4708 +      using insert(2)
  1.4709 +      unfolding uv xk
  1.4710 +      apply auto
  1.4711 +      done
  1.4712 +    show ?case
  1.4713 +    proof (cases "cbox u v \<subseteq> d x")
  1.4714 +      case True
  1.4715 +      then show ?thesis
  1.4716 +        apply (rule_tac x="{(x,cbox u v)} \<union> q1" in exI)
  1.4717 +        apply rule
  1.4718 +        unfolding * uv
  1.4719 +        apply (rule tagged_division_union)
  1.4720 +        apply (rule tagged_division_of_self)
  1.4721 +        apply (rule p[unfolded xk uv] insertI1)+
  1.4722 +        apply (rule q1)
  1.4723 +        apply (rule int)
  1.4724 +        apply rule
  1.4725 +        apply (rule fine_union)
  1.4726 +        apply (subst fine_def)
  1.4727 +        defer
  1.4728 +        apply (rule q1)
  1.4729 +        unfolding Ball_def split_paired_All split_conv
  1.4730 +        apply rule
  1.4731 +        apply rule
  1.4732 +        apply rule
  1.4733 +        apply rule
  1.4734 +        apply (erule insertE)
  1.4735 +        apply (simp add: uv xk)
  1.4736 +        apply (rule UnI2)
  1.4737 +        apply (drule q1(3)[rule_format])
  1.4738 +        unfolding xk uv
  1.4739 +        apply auto
  1.4740 +        done
  1.4741 +    next
  1.4742 +      case False
  1.4743 +      from fine_division_exists[OF assms(2), of u v] guess q2 . note q2=this
  1.4744 +      show ?thesis
  1.4745 +        apply (rule_tac x="q2 \<union> q1" in exI)
  1.4746 +        apply rule
  1.4747 +        unfolding * uv
  1.4748 +        apply (rule tagged_division_union q2 q1 int fine_union)+
  1.4749 +        unfolding Ball_def split_paired_All split_conv
  1.4750 +        apply rule
  1.4751 +        apply (rule fine_union)
  1.4752 +        apply (rule q1 q2)+
  1.4753 +        apply rule
  1.4754 +        apply rule
  1.4755 +        apply rule
  1.4756 +        apply rule
  1.4757 +        apply (erule insertE)
  1.4758 +        apply (rule UnI2)
  1.4759 +        apply (simp add: False uv xk)
  1.4760 +        apply (drule q1(3)[rule_format])
  1.4761 +        using False
  1.4762 +        unfolding xk uv
  1.4763 +        apply auto
  1.4764 +        done
  1.4765 +    qed
  1.4766 +  qed
  1.4767 +qed
  1.4768 +
  1.4769 +
  1.4770 +subsection \<open>Hence the main theorem about negligible sets.\<close>
  1.4771 +
  1.4772 +lemma finite_product_dependent:
  1.4773 +  assumes "finite s"
  1.4774 +    and "\<And>x. x \<in> s \<Longrightarrow> finite (t x)"
  1.4775 +  shows "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
  1.4776 +  using assms
  1.4777 +proof induct
  1.4778 +  case (insert x s)
  1.4779 +  have *: "{(i, j) |i j. i \<in> insert x s \<and> j \<in> t i} =
  1.4780 +    (\<lambda>y. (x,y)) ` (t x) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  1.4781 +  show ?case
  1.4782 +    unfolding *
  1.4783 +    apply (rule finite_UnI)
  1.4784 +    using insert
  1.4785 +    apply auto
  1.4786 +    done
  1.4787 +qed auto
  1.4788 +
  1.4789 +lemma sum_sum_product:
  1.4790 +  assumes "finite s"
  1.4791 +    and "\<forall>i\<in>s. finite (t i)"
  1.4792 +  shows "setsum (\<lambda>i. setsum (x i) (t i)::real) s =
  1.4793 +    setsum (\<lambda>(i,j). x i j) {(i,j) | i j. i \<in> s \<and> j \<in> t i}"
  1.4794 +  using assms
  1.4795 +proof induct
  1.4796 +  case (insert a s)
  1.4797 +  have *: "{(i, j) |i j. i \<in> insert a s \<and> j \<in> t i} =
  1.4798 +    (\<lambda>y. (a,y)) ` (t a) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  1.4799 +  show ?case
  1.4800 +    unfolding *
  1.4801 +    apply (subst setsum.union_disjoint)
  1.4802 +    unfolding setsum.insert[OF insert(1-2)]
  1.4803 +    prefer 4
  1.4804 +    apply (subst insert(3))
  1.4805 +    unfolding add_right_cancel
  1.4806 +  proof -
  1.4807 +    show "setsum (x a) (t a) = (\<Sum>(xa, y)\<in> Pair a ` t a. x xa y)"
  1.4808 +      apply (subst setsum.reindex)
  1.4809 +      unfolding inj_on_def
  1.4810 +      apply auto
  1.4811 +      done
  1.4812 +    show "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
  1.4813 +      apply (rule finite_product_dependent)
  1.4814 +      using insert
  1.4815 +      apply auto
  1.4816 +      done
  1.4817 +  qed (insert insert, auto)
  1.4818 +qed auto
  1.4819 +
  1.4820 +lemma has_integral_negligible:
  1.4821 +  fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1.4822 +  assumes "negligible s"
  1.4823 +    and "\<forall>x\<in>(t - s). f x = 0"
  1.4824 +  shows "(f has_integral 0) t"
  1.4825 +proof -
  1.4826 +  presume P: "\<And>f::'b::euclidean_space \<Rightarrow> 'a.
  1.4827 +    \<And>a b. \<forall>x. x \<notin> s \<longrightarrow> f x = 0 \<Longrightarrow> (f has_integral 0) (cbox a b)"
  1.4828 +  let ?f = "(\<lambda>x. if x \<in> t then f x else 0)"
  1.4829 +  show ?thesis
  1.4830 +    apply (rule_tac f="?f" in has_integral_eq)
  1.4831 +    unfolding if_P
  1.4832 +    apply (rule refl)
  1.4833 +    apply (subst has_integral_alt)
  1.4834 +    apply cases
  1.4835 +    apply (subst if_P, assumption)
  1.4836 +    unfolding if_not_P
  1.4837 +  proof -
  1.4838 +    assume "\<exists>a b. t = cbox a b"
  1.4839 +    then guess a b apply - by (erule exE)+ note t = this
  1.4840 +    show "(?f has_integral 0) t"
  1.4841 +      unfolding t
  1.4842 +      apply (rule P)
  1.4843 +      using assms(2)
  1.4844 +      unfolding t
  1.4845 +      apply auto
  1.4846 +      done
  1.4847 +  next
  1.4848 +    show "\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  1.4849 +      (\<exists>z. ((\<lambda>x. if x \<in> t then ?f x else 0) has_integral z) (cbox a b) \<and> norm (z - 0) < e)"
  1.4850 +      apply safe
  1.4851 +      apply (rule_tac x=1 in exI)
  1.4852 +      apply rule
  1.4853 +      apply (rule zero_less_one)
  1.4854 +      apply safe
  1.4855 +      apply (rule_tac x=0 in exI)
  1.4856 +      apply rule
  1.4857 +      apply (rule P)
  1.4858 +      using assms(2)
  1.4859 +      apply auto
  1.4860 +      done
  1.4861 +  qed
  1.4862 +next
  1.4863 +  fix f :: "'b \<Rightarrow> 'a"
  1.4864 +  fix a b :: 'b
  1.4865 +  assume assm: "\<forall>x. x \<notin> s \<longrightarrow> f x = 0"
  1.4866 +  show "(f has_integral 0) (cbox a b)"
  1.4867 +    unfolding has_integral
  1.4868 +  proof (safe, goal_cases)
  1.4869 +    case prems: (1 e)
  1.4870 +    then have "\<And>n. e / 2 / ((real n+1) * (2 ^ n)) > 0"
  1.4871 +      apply -
  1.4872 +      apply (rule divide_pos_pos)
  1.4873 +      defer
  1.4874 +      apply (rule mult_pos_pos)
  1.4875 +      apply (auto simp add:field_simps)
  1.4876 +      done
  1.4877 +    note assms(1)[unfolded negligible_def has_integral,rule_format,OF this,of a b]
  1.4878 +    note allI[OF this,of "\<lambda>x. x"]
  1.4879 +    from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format]]
  1.4880 +    show ?case
  1.4881 +      apply (rule_tac x="\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x" in exI)
  1.4882 +    proof safe
  1.4883 +      show "gauge (\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x)"
  1.4884 +        using d(1) unfolding gauge_def by auto
  1.4885 +      fix p
  1.4886 +      assume as: "p tagged_division_of (cbox a b)" "(\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x) fine p"
  1.4887 +      let ?goal = "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  1.4888 +      {
  1.4889 +        presume "p \<noteq> {} \<Longrightarrow> ?goal"
  1.4890 +        then show ?goal
  1.4891 +          apply (cases "p = {}")
  1.4892 +          using prems
  1.4893 +          apply auto
  1.4894 +          done
  1.4895 +      }
  1.4896 +      assume as': "p \<noteq> {}"
  1.4897 +      from real_arch_simple[of "Max((\<lambda>(x,k). norm(f x)) ` p)"] guess N ..
  1.4898 +      then have N: "\<forall>x\<in>(\<lambda>(x, k). norm (f x)) ` p. x \<le> real N"
  1.4899 +        by (meson Max_ge as(1) dual_order.trans finite_imageI tagged_division_of_finite)
  1.4900 +      have "\<forall>i. \<exists>q. q tagged_division_of (cbox a b) \<and> (d i) fine q \<and> (\<forall>(x, k)\<in>p. k \<subseteq> (d i) x \<longrightarrow> (x, k) \<in> q)"
  1.4901 +        by (auto intro: tagged_division_finer[OF as(1) d(1)])
  1.4902 +      from choice[OF this] guess q .. note q=conjunctD3[OF this[rule_format]]
  1.4903 +      have *: "\<And>i. (\<Sum>(x, k)\<in>q i. content k *\<^sub>R indicator s x) \<ge> (0::real)"
  1.4904 +        apply (rule setsum_nonneg)
  1.4905 +        apply safe
  1.4906 +        unfolding real_scaleR_def
  1.4907 +        apply (drule tagged_division_ofD(4)[OF q(1)])
  1.4908 +        apply (auto intro: mult_nonneg_nonneg)
  1.4909 +        done
  1.4910 +      have **: "finite s \<Longrightarrow> finite t \<Longrightarrow> (\<forall>(x,y) \<in> t. (0::real) \<le> g(x,y)) \<Longrightarrow>
  1.4911 +        (\<forall>y\<in>s. \<exists>x. (x,y) \<in> t \<and> f(y) \<le> g(x,y)) \<Longrightarrow> setsum f s \<le> setsum g t" for f g s t
  1.4912 +        apply (rule setsum_le_included[of s t g snd f])
  1.4913 +        prefer 4
  1.4914 +        apply safe
  1.4915 +        apply (erule_tac x=x in ballE)
  1.4916 +        apply (erule exE)
  1.4917 +        apply (rule_tac x="(xa,x)" in bexI)
  1.4918 +        apply auto
  1.4919 +        done
  1.4920 +      have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) \<le> setsum (\<lambda>i. (real i + 1) *
  1.4921 +        norm (setsum (\<lambda>(x,k). content k *\<^sub>R indicator s x :: real) (q i))) {..N+1}"
  1.4922 +        unfolding real_norm_def setsum_right_distrib abs_of_nonneg[OF *] diff_0_right
  1.4923 +        apply (rule order_trans)
  1.4924 +        apply (rule norm_setsum)
  1.4925 +        apply (subst sum_sum_product)
  1.4926 +        prefer 3
  1.4927 +      proof (rule **, safe)
  1.4928 +        show "finite {(i, j) |i j. i \<in> {..N + 1} \<and> j \<in> q i}"
  1.4929 +          apply (rule finite_product_dependent)
  1.4930 +          using q
  1.4931 +          apply auto
  1.4932 +          done
  1.4933 +        fix i a b
  1.4934 +        assume as'': "(a, b) \<in> q i"
  1.4935 +        show "0 \<le> (real i + 1) * (content b *\<^sub>R indicator s a)"
  1.4936 +          unfolding real_scaleR_def
  1.4937 +          using tagged_division_ofD(4)[OF q(1) as'']
  1.4938 +          by (auto intro!: mult_nonneg_nonneg)
  1.4939 +      next
  1.4940 +        fix i :: nat
  1.4941 +        show "finite (q i)"
  1.4942 +          using q by auto
  1.4943 +      next
  1.4944 +        fix x k
  1.4945 +        assume xk: "(x, k) \<in> p"
  1.4946 +        define n where "n = nat \<lfloor>norm (f x)\<rfloor>"
  1.4947 +        have *: "norm (f x) \<in> (\<lambda>(x, k). norm (f x)) ` p"
  1.4948 +          using xk by auto
  1.4949 +        have nfx: "real n \<le> norm (f x)" "norm (f x) \<le> real n + 1"
  1.4950 +          unfolding n_def by auto
  1.4951 +        then have "n \<in> {0..N + 1}"
  1.4952 +          using N[rule_format,OF *] by auto
  1.4953 +        moreover
  1.4954 +        note as(2)[unfolded fine_def,rule_format,OF xk,unfolded split_conv]
  1.4955 +        note q(3)[rule_format,OF xk,unfolded split_conv,rule_format,OF this]
  1.4956 +        note this[unfolded n_def[symmetric]]
  1.4957 +        moreover
  1.4958 +        have "norm (content k *\<^sub>R f x) \<le> (real n + 1) * (content k * indicator s x)"
  1.4959 +        proof (cases "x \<in> s")
  1.4960 +          case False
  1.4961 +          then show ?thesis
  1.4962 +            using assm by auto
  1.4963 +        next
  1.4964 +          case True
  1.4965 +          have *: "content k \<ge> 0"
  1.4966 +            using tagged_division_ofD(4)[OF as(1) xk] by auto
  1.4967 +          moreover
  1.4968 +          have "content k * norm (f x) \<le> content k * (real n + 1)"
  1.4969 +            apply (rule mult_mono)
  1.4970 +            using nfx *
  1.4971 +            apply auto
  1.4972 +            done
  1.4973 +          ultimately
  1.4974 +          show ?thesis
  1.4975 +            unfolding abs_mult
  1.4976 +            using nfx True
  1.4977 +            by (auto simp add: field_simps)
  1.4978 +        qed
  1.4979 +        ultimately show "\<exists>y. (y, x, k) \<in> {(i, j) |i j. i \<in> {..N + 1} \<and> j \<in> q i} \<and> norm (content k *\<^sub>R f x) \<le>
  1.4980 +          (real y + 1) * (content k *\<^sub>R indicator s x)"
  1.4981 +          apply (rule_tac x=n in exI)
  1.4982 +          apply safe
  1.4983 +          apply (rule_tac x=n in exI)
  1.4984 +          apply (rule_tac x="(x,k)" in exI)
  1.4985 +          apply safe
  1.4986 +          apply auto
  1.4987 +          done
  1.4988 +      qed (insert as, auto)
  1.4989 +      also have "\<dots> \<le> setsum (\<lambda>i. e / 2 / 2 ^ i) {..N+1}"
  1.4990 +      proof (rule setsum_mono, goal_cases)
  1.4991 +        case (1 i)
  1.4992 +        then show ?case
  1.4993 +          apply (subst mult.commute, subst pos_le_divide_eq[symmetric])
  1.4994 +          using d(2)[rule_format, of "q i" i]
  1.4995 +          using q[rule_format]
  1.4996 +          apply (auto simp add: field_simps)
  1.4997 +          done
  1.4998 +      qed
  1.4999 +      also have "\<dots> < e * inverse 2 * 2"
  1.5000 +        unfolding divide_inverse setsum_right_distrib[symmetric]
  1.5001 +        apply (rule mult_strict_left_mono)
  1.5002 +        unfolding power_inverse [symmetric] lessThan_Suc_atMost[symmetric]
  1.5003 +        apply (subst geometric_sum)
  1.5004 +        using prems
  1.5005 +        apply auto
  1.5006 +        done
  1.5007 +      finally show "?goal" by auto
  1.5008 +    qed
  1.5009 +  qed
  1.5010 +qed
  1.5011 +
  1.5012 +lemma has_integral_spike:
  1.5013 +  fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1.5014 +  assumes "negligible s"
  1.5015 +    and "(\<forall>x\<in>(t - s). g x = f x)"
  1.5016 +    and "(f has_integral y) t"
  1.5017 +  shows "(g has_integral y) t"
  1.5018 +proof -
  1.5019 +  {
  1.5020 +    fix a b :: 'b
  1.5021 +    fix f g :: "'b \<Rightarrow> 'a"
  1.5022 +    fix y :: 'a
  1.5023 +    assume as: "\<forall>x \<in> cbox a b - s. g x = f x" "(f has_integral y) (cbox a b)"
  1.5024 +    have "((\<lambda>x. f x + (g x - f x)) has_integral (y + 0)) (cbox a b)"
  1.5025 +      apply (rule has_integral_add[OF as(2)])
  1.5026 +      apply (rule has_integral_negligible[OF assms(1)])
  1.5027 +      using as
  1.5028 +      apply auto
  1.5029 +      done
  1.5030 +    then have "(g has_integral y) (cbox a b)"
  1.5031 +      by auto
  1.5032 +  } note * = this
  1.5033 +  show ?thesis
  1.5034 +    apply (subst has_integral_alt)
  1.5035 +    using assms(2-)
  1.5036 +    apply -
  1.5037 +    apply (rule cond_cases)
  1.5038 +    apply safe
  1.5039 +    apply (rule *)
  1.5040 +    apply assumption+
  1.5041 +    apply (subst(asm) has_integral_alt)
  1.5042 +    unfolding if_not_P
  1.5043 +    apply (erule_tac x=e in allE)
  1.5044 +    apply safe
  1.5045 +    apply (rule_tac x=B in exI)
  1.5046 +    apply safe
  1.5047 +    apply (erule_tac x=a in allE)
  1.5048 +    apply (erule_tac x=b in allE)
  1.5049 +    apply safe
  1.5050 +    apply (rule_tac x=z in exI)
  1.5051 +    apply safe
  1.5052 +    apply (rule *[where fa2="\<lambda>x. if x\<in>t then f x else 0"])
  1.5053 +    apply auto
  1.5054 +    done
  1.5055 +qed
  1.5056 +
  1.5057 +lemma has_integral_spike_eq:
  1.5058 +  assumes "negligible s"
  1.5059 +    and "\<forall>x\<in>(t - s). g x = f x"
  1.5060 +  shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  1.5061 +  apply rule
  1.5062 +  apply (rule_tac[!] has_integral_spike[OF assms(1)])
  1.5063 +  using assms(2)
  1.5064 +  apply auto
  1.5065 +  done
  1.5066 +
  1.5067 +lemma integrable_spike:
  1.5068 +  assumes "negligible s"
  1.5069 +    and "\<forall>x\<in>(t - s). g x = f x"
  1.5070 +    and "f integrable_on t"
  1.5071 +  shows "g integrable_on  t"
  1.5072 +  using assms
  1.5073 +  unfolding integrable_on_def
  1.5074 +  apply -
  1.5075 +  apply (erule exE)
  1.5076 +  apply rule
  1.5077 +  apply (rule has_integral_spike)
  1.5078 +  apply fastforce+
  1.5079 +  done
  1.5080 +
  1.5081 +lemma integral_spike:
  1.5082 +  assumes "negligible s"
  1.5083 +    and "\<forall>x\<in>(t - s). g x = f x"
  1.5084 +  shows "integral t f = integral t g"
  1.5085 +  using has_integral_spike_eq[OF assms] by (simp add: integral_def integrable_on_def)
  1.5086 +
  1.5087 +
  1.5088 +subsection \<open>Some other trivialities about negligible sets.\<close>
  1.5089 +
  1.5090 +lemma negligible_subset[intro]:
  1.5091 +  assumes "negligible s"
  1.5092 +    and "t \<subseteq> s"
  1.5093 +  shows "negligible t"
  1.5094 +  unfolding negligible_def
  1.5095 +proof (safe, goal_cases)
  1.5096 +  case (1 a b)
  1.5097 +  show ?case
  1.5098 +    using assms(1)[unfolded negligible_def,rule_format,of a b]
  1.5099 +    apply -
  1.5100 +    apply (rule has_integral_spike[OF assms(1)])
  1.5101 +    defer
  1.5102 +    apply assumption
  1.5103 +    using assms(2)
  1.5104 +    unfolding indicator_def
  1.5105 +    apply auto
  1.5106 +    done
  1.5107 +qed
  1.5108 +
  1.5109 +lemma negligible_diff[intro?]:
  1.5110 +  assumes "negligible s"
  1.5111 +  shows "negligible (s - t)"
  1.5112 +  using assms by auto
  1.5113 +
  1.5114 +lemma negligible_Int:
  1.5115 +  assumes "negligible s \<or> negligible t"
  1.5116 +  shows "negligible (s \<inter> t)"
  1.5117 +  using assms by auto
  1.5118 +
  1.5119 +lemma negligible_Un:
  1.5120 +  assumes "negligible s"
  1.5121 +    and "negligible t"
  1.5122 +  shows "negligible (s \<union> t)"
  1.5123 +  unfolding negligible_def
  1.5124 +proof (safe, goal_cases)
  1.5125 +  case (1 a b)
  1.5126 +  note assm = assms[unfolded negligible_def,rule_format,of a b]
  1.5127 +  then show ?case
  1.5128 +    apply (subst has_integral_spike_eq[OF assms(2)])
  1.5129 +    defer
  1.5130 +    apply assumption
  1.5131 +    unfolding indicator_def
  1.5132 +    apply auto
  1.5133 +    done
  1.5134 +qed
  1.5135 +
  1.5136 +lemma negligible_Un_eq[simp]: "negligible (s \<union> t) \<longleftrightarrow> negligible s \<and> negligible t"
  1.5137 +  using negligible_Un by auto
  1.5138 +
  1.5139 +lemma negligible_sing[intro]: "negligible {a::'a::euclidean_space}"
  1.5140 +  using negligible_standard_hyperplane[OF SOME_Basis, of "a \<bullet> (SOME i. i \<in> Basis)"] by auto
  1.5141 +
  1.5142 +lemma negligible_insert[simp]: "negligible (insert a s) \<longleftrightarrow> negligible s"
  1.5143 +  apply (subst insert_is_Un)
  1.5144 +  unfolding negligible_Un_eq
  1.5145 +  apply auto
  1.5146 +  done
  1.5147 +
  1.5148 +lemma negligible_empty[iff]: "negligible {}"
  1.5149 +  by auto
  1.5150 +
  1.5151 +lemma negligible_finite[intro]:
  1.5152 +  assumes "finite s"
  1.5153 +  shows "negligible s"
  1.5154 +  using assms by (induct s) auto
  1.5155 +
  1.5156 +lemma negligible_Union[intro]:
  1.5157 +  assumes "finite s"
  1.5158 +    and "\<forall>t\<in>s. negligible t"
  1.5159 +  shows "negligible(\<Union>s)"
  1.5160 +  using assms by induct auto
  1.5161 +
  1.5162 +lemma negligible:
  1.5163 +  "negligible s \<longleftrightarrow> (\<forall>t::('a::euclidean_space) set. ((indicator s::'a\<Rightarrow>real) has_integral 0) t)"
  1.5164 +  apply safe
  1.5165 +  defer
  1.5166 +  apply (subst negligible_def)
  1.5167 +proof -
  1.5168 +  fix t :: "'a set"
  1.5169 +  assume as: "negligible s"
  1.5170 +  have *: "(\<lambda>x. if x \<in> s \<inter> t then 1 else 0) = (\<lambda>x. if x\<in>t then if x\<in>s then 1 else 0 else 0)"
  1.5171 +    by auto
  1.5172 +  show "((indicator s::'a\<Rightarrow>real) has_integral 0) t"
  1.5173 +    apply (subst has_integral_alt)
  1.5174 +    apply cases
  1.5175 +    apply (subst if_P,assumption)
  1.5176 +    unfolding if_not_P
  1.5177 +    apply safe
  1.5178 +    apply (rule as[unfolded negligible_def,rule_format])
  1.5179 +    apply (rule_tac x=1 in exI)
  1.5180 +    apply safe
  1.5181 +    apply (rule zero_less_one)
  1.5182 +    apply (rule_tac x=0 in exI)
  1.5183 +    using negligible_subset[OF as,of "s \<inter> t"]
  1.5184 +    unfolding negligible_def indicator_def [abs_def]
  1.5185 +    unfolding *
  1.5186 +    apply auto
  1.5187 +    done
  1.5188 +qed auto
  1.5189 +
  1.5190 +
  1.5191 +subsection \<open>Finite case of the spike theorem is quite commonly needed.\<close>
  1.5192 +
  1.5193 +lemma has_integral_spike_finite:
  1.5194 +  assumes "finite s"
  1.5195 +    and "\<forall>x\<in>t-s. g x = f x"
  1.5196 +    and "(f has_integral y) t"
  1.5197 +  shows "(g has_integral y) t"
  1.5198 +  apply (rule has_integral_spike)
  1.5199 +  using assms
  1.5200 +  apply auto
  1.5201 +  done
  1.5202 +
  1.5203 +lemma has_integral_spike_finite_eq:
  1.5204 +  assumes "finite s"
  1.5205 +    and "\<forall>x\<in>t-s. g x = f x"
  1.5206 +  shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  1.5207 +  apply rule
  1.5208 +  apply (rule_tac[!] has_integral_spike_finite)
  1.5209 +  using assms
  1.5210 +  apply auto
  1.5211 +  done
  1.5212 +
  1.5213 +lemma integrable_spike_finite:
  1.5214 +  assumes "finite s"
  1.5215 +    and "\<forall>x\<in>t-s. g x = f x"
  1.5216 +    and "f integrable_on t"
  1.5217 +  shows "g integrable_on  t"
  1.5218 +  using assms
  1.5219 +  unfolding integrable_on_def
  1.5220 +  apply safe
  1.5221 +  apply (rule_tac x=y in exI)
  1.5222 +  apply (rule has_integral_spike_finite)
  1.5223 +  apply auto
  1.5224 +  done
  1.5225 +
  1.5226 +
  1.5227 +subsection \<open>In particular, the boundary of an interval is negligible.\<close>
  1.5228 +
  1.5229 +lemma negligible_frontier_interval: "negligible(cbox (a::'a::euclidean_space) b - box a b)"
  1.5230 +proof -
  1.5231 +  let ?A = "\<Union>((\<lambda>k. {x. x\<bullet>k = a\<bullet>k} \<union> {x::'a. x\<bullet>k = b\<bullet>k}) ` Basis)"
  1.5232 +  have "cbox a b - box a b \<subseteq> ?A"
  1.5233 +    apply rule unfolding Diff_iff mem_box
  1.5234 +    apply simp
  1.5235 +    apply(erule conjE bexE)+
  1.5236 +    apply(rule_tac x=i in bexI)
  1.5237 +    apply auto
  1.5238 +    done
  1.5239 +  then show ?thesis
  1.5240 +    apply -
  1.5241 +    apply (rule negligible_subset[of ?A])
  1.5242 +    apply (rule negligible_Union[OF finite_imageI])
  1.5243 +    apply auto
  1.5244 +    done
  1.5245 +qed
  1.5246 +
  1.5247 +lemma has_integral_spike_interior:
  1.5248 +  assumes "\<forall>x\<in>box a b. g x = f x"
  1.5249 +    and "(f has_integral y) (cbox a b)"
  1.5250 +  shows "(g has_integral y) (cbox a b)"
  1.5251 +  apply (rule has_integral_spike[OF negligible_frontier_interval _ assms(2)])
  1.5252 +  using assms(1)
  1.5253 +  apply auto
  1.5254 +  done
  1.5255 +
  1.5256 +lemma has_integral_spike_interior_eq:
  1.5257 +  assumes "\<forall>x\<in>box a b. g x = f x"
  1.5258 +  shows "(f has_integral y) (cbox a b) \<longleftrightarrow> (g has_integral y) (cbox a b)"
  1.5259 +  apply rule
  1.5260 +  apply (rule_tac[!] has_integral_spike_interior)
  1.5261 +  using assms
  1.5262 +  apply auto
  1.5263 +  done
  1.5264 +
  1.5265 +lemma integrable_spike_interior:
  1.5266 +  assumes "\<forall>x\<in>box a b. g x = f x"
  1.5267 +    and "f integrable_on cbox a b"
  1.5268 +  shows "g integrable_on cbox a b"
  1.5269 +  using assms
  1.5270 +  unfolding integrable_on_def
  1.5271 +  using has_integral_spike_interior[OF assms(1)]
  1.5272 +  by auto
  1.5273 +
  1.5274 +
  1.5275 +subsection \<open>Integrability of continuous functions.\<close>
  1.5276 +
  1.5277 +lemma operative_approximable:
  1.5278 +  fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  1.5279 +  assumes "0 \<le> e"
  1.5280 +  shows "comm_monoid.operative op \<and> True (\<lambda>i. \<exists>g. (\<forall>x\<in>i. norm (f x - g (x::'b)) \<le> e) \<and> g integrable_on i)"
  1.5281 +  unfolding comm_monoid.operative_def[OF comm_monoid_and]
  1.5282 +proof safe
  1.5283 +  fix a b :: 'b
  1.5284 +  show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  1.5285 +    if "content (cbox a b) = 0"
  1.5286 +    apply (rule_tac x=f in exI)
  1.5287 +    using assms that
  1.5288 +    apply (auto intro!: integrable_on_null)
  1.5289 +    done
  1.5290 +  {
  1.5291 +    fix c g
  1.5292 +    fix k :: 'b
  1.5293 +    assume as: "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e" "g integrable_on cbox a b"
  1.5294 +    assume k: "k \<in> Basis"
  1.5295 +    show "\<exists>g. (\<forall>x\<in>cbox a b \<inter> {x. x \<bullet> k \<le> c}. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  1.5296 +      "\<exists>g. (\<forall>x\<in>cbox a b \<inter> {x. c \<le> x \<bullet> k}. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k}"
  1.5297 +      apply (rule_tac[!] x=g in exI)
  1.5298 +      using as(1) integrable_split[OF as(2) k]
  1.5299 +      apply auto
  1.5300 +      done
  1.5301 +  }
  1.5302 +  fix c k g1 g2
  1.5303 +  assume as: "\<forall>x\<in>cbox a b \<inter> {x. x \<bullet> k \<le> c}. norm (f x - g1 x) \<le> e" "g1 integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  1.5304 +    "\<forall>x\<in>cbox a b \<inter> {x. c \<le> x \<bullet> k}. norm (f x - g2 x) \<le> e" "g2 integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k}"
  1.5305 +  assume k: "k \<in> Basis"
  1.5306 +  let ?g = "\<lambda>x. if x\<bullet>k = c then f x else if x\<bullet>k \<le> c then g1 x else g2 x"
  1.5307 +  show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  1.5308 +    apply (rule_tac x="?g" in exI)
  1.5309 +    apply safe
  1.5310 +  proof goal_cases
  1.5311 +    case (1 x)
  1.5312 +    then show ?case
  1.5313 +      apply -
  1.5314 +      apply (cases "x\<bullet>k=c")
  1.5315 +      apply (case_tac "x\<bullet>k < c")
  1.5316 +      using as assms
  1.5317 +      apply auto
  1.5318 +      done
  1.5319 +  next
  1.5320 +    case 2
  1.5321 +    presume "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  1.5322 +      and "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  1.5323 +    then guess h1 h2 unfolding integrable_on_def by auto
  1.5324 +    from has_integral_split[OF this k] show ?case
  1.5325 +      unfolding integrable_on_def by auto
  1.5326 +  next
  1.5327 +    show "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}" "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  1.5328 +      apply(rule_tac[!] integrable_spike[OF negligible_standard_hyperplane[of k c]])
  1.5329 +      using k as(2,4)
  1.5330 +      apply auto
  1.5331 +      done
  1.5332 +  qed
  1.5333 +qed
  1.5334 +
  1.5335 +lemma comm_monoid_set_F_and: "comm_monoid_set.F op \<and> True f s \<longleftrightarrow> (finite s \<longrightarrow> (\<forall>x\<in>s. f x))"
  1.5336 +proof -
  1.5337 +  interpret bool: comm_monoid_set "op \<and>" True
  1.5338 +    proof qed auto
  1.5339 +  show ?thesis
  1.5340 +    by (induction s rule: infinite_finite_induct) auto
  1.5341 +qed
  1.5342 +
  1.5343 +lemma approximable_on_division:
  1.5344 +  fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  1.5345 +  assumes "0 \<le> e"
  1.5346 +    and "d division_of (cbox a b)"
  1.5347 +    and "\<forall>i\<in>d. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  1.5348 +  obtains g where "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e" "g integrable_on cbox a b"
  1.5349 +proof -
  1.5350 +  note * = comm_monoid_set.operative_division[OF comm_monoid_set_and operative_approximable[OF assms(1)] assms(2)]
  1.5351 +  from assms(3) this[unfolded comm_monoid_set_F_and, of f] division_of_finite[OF assms(2)]
  1.5352 +  guess g by auto
  1.5353 +  then show thesis
  1.5354 +    apply -
  1.5355 +    apply (rule that[of g])
  1.5356 +    apply auto
  1.5357 +    done
  1.5358 +qed
  1.5359 +
  1.5360 +lemma integrable_continuous:
  1.5361 +  fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  1.5362 +  assumes "continuous_on (cbox a b) f"
  1.5363 +  shows "f integrable_on cbox a b"
  1.5364 +proof (rule integrable_uniform_limit, safe)
  1.5365 +  fix e :: real
  1.5366 +  assume e: "e > 0"
  1.5367 +  from compact_uniformly_continuous[OF assms compact_cbox,unfolded uniformly_continuous_on_def,rule_format,OF e] guess d ..
  1.5368 +  note d=conjunctD2[OF this,rule_format]
  1.5369 +  from fine_division_exists[OF gauge_ball[OF d(1)], of a b] guess p . note p=this
  1.5370 +  note p' = tagged_division_ofD[OF p(1)]
  1.5371 +  have *: "\<forall>i\<in>snd ` p. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  1.5372 +  proof (safe, unfold snd_conv)
  1.5373 +    fix x l
  1.5374 +    assume as: "(x, l) \<in> p"
  1.5375 +    from p'(4)[OF this] guess a b by (elim exE) note l=this
  1.5376 +    show "\<exists>g. (\<forall>x\<in>l. norm (f x - g x) \<le> e) \<and> g integrable_on l"
  1.5377 +      apply (rule_tac x="\<lambda>y. f x" in exI)
  1.5378 +    proof safe
  1.5379 +      show "(\<lambda>y. f x) integrable_on l"
  1.5380 +        unfolding integrable_on_def l
  1.5381 +        apply rule
  1.5382 +        apply (rule has_integral_const)
  1.5383 +        done
  1.5384 +      fix y
  1.5385 +      assume y: "y \<in> l"
  1.5386 +      note fineD[OF p(2) as,unfolded subset_eq,rule_format,OF this]
  1.5387 +      note d(2)[OF _ _ this[unfolded mem_ball]]
  1.5388 +      then show "norm (f y - f x) \<le> e"
  1.5389 +        using y p'(2-3)[OF as] unfolding dist_norm l norm_minus_commute by fastforce
  1.5390 +    qed
  1.5391 +  qed
  1.5392 +  from e have "e \<ge> 0"
  1.5393 +    by auto
  1.5394 +  from approximable_on_division[OF this division_of_tagged_division[OF p(1)] *] guess g .
  1.5395 +  then show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  1.5396 +    by auto
  1.5397 +qed
  1.5398 +
  1.5399 +lemma integrable_continuous_real:
  1.5400 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.5401 +  assumes "continuous_on {a .. b} f"
  1.5402 +  shows "f integrable_on {a .. b}"
  1.5403 +  by (metis assms box_real(2) integrable_continuous)
  1.5404 +
  1.5405 +subsection \<open>Specialization of additivity to one dimension.\<close>
  1.5406 +
  1.5407 +subsection \<open>Special case of additivity we need for the FTC.\<close>
  1.5408 +
  1.5409 +lemma additive_tagged_division_1:
  1.5410 +  fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  1.5411 +  assumes "a \<le> b"
  1.5412 +    and "p tagged_division_of {a..b}"
  1.5413 +  shows "setsum (\<lambda>(x,k). f(Sup k) - f(Inf k)) p = f b - f a"
  1.5414 +proof -
  1.5415 +  let ?f = "(\<lambda>k::(real) set. if k = {} then 0 else f(interval_upperbound k) - f(interval_lowerbound k))"
  1.5416 +  have ***: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
  1.5417 +    using assms by auto
  1.5418 +  have *: "add.operative ?f"
  1.5419 +    unfolding add.operative_1_lt box_eq_empty
  1.5420 +    by auto
  1.5421 +  have **: "cbox a b \<noteq> {}"
  1.5422 +    using assms(1) by auto
  1.5423 +  note setsum.operative_tagged_division[OF * assms(2)[simplified box_real[symmetric]]]
  1.5424 +  note * = this[unfolded if_not_P[OF **] interval_bounds[OF ***],symmetric]
  1.5425 +  show ?thesis
  1.5426 +    unfolding *
  1.5427 +    apply (rule setsum.cong)
  1.5428 +    unfolding split_paired_all split_conv
  1.5429 +    using assms(2)
  1.5430 +    apply auto
  1.5431 +    done
  1.5432 +qed
  1.5433 +
  1.5434 +
  1.5435 +subsection \<open>A useful lemma allowing us to factor out the content size.\<close>
  1.5436 +
  1.5437 +lemma has_integral_factor_content:
  1.5438 +  "(f has_integral i) (cbox a b) \<longleftrightarrow>
  1.5439 +    (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  1.5440 +      norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content (cbox a b)))"
  1.5441 +proof (cases "content (cbox a b) = 0")
  1.5442 +  case True
  1.5443 +  show ?thesis
  1.5444 +    unfolding has_integral_null_eq[OF True]
  1.5445 +    apply safe
  1.5446 +    apply (rule, rule, rule gauge_trivial, safe)
  1.5447 +    unfolding setsum_content_null[OF True] True
  1.5448 +    defer
  1.5449 +    apply (erule_tac x=1 in allE)
  1.5450 +    apply safe
  1.5451 +    defer
  1.5452 +    apply (rule fine_division_exists[of _ a b])
  1.5453 +    apply assumption
  1.5454 +    apply (erule_tac x=p in allE)
  1.5455 +    unfolding setsum_content_null[OF True]
  1.5456 +    apply auto
  1.5457 +    done
  1.5458 +next
  1.5459 +  case False
  1.5460 +  note F = this[unfolded content_lt_nz[symmetric]]
  1.5461 +  let ?P = "\<lambda>e opp. \<exists>d. gauge d \<and>
  1.5462 +    (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow> opp (norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i)) e)"
  1.5463 +  show ?thesis
  1.5464 +    apply (subst has_integral)
  1.5465 +  proof safe
  1.5466 +    fix e :: real
  1.5467 +    assume e: "e > 0"
  1.5468 +    {
  1.5469 +      assume "\<forall>e>0. ?P e op <"
  1.5470 +      then show "?P (e * content (cbox a b)) op \<le>"
  1.5471 +        apply (erule_tac x="e * content (cbox a b)" in allE)
  1.5472 +        apply (erule impE)
  1.5473 +        defer
  1.5474 +        apply (erule exE,rule_tac x=d in exI)
  1.5475 +        using F e
  1.5476 +        apply (auto simp add:field_simps)
  1.5477 +        done
  1.5478 +    }
  1.5479 +    {
  1.5480 +      assume "\<forall>e>0. ?P (e * content (cbox a b)) op \<le>"
  1.5481 +      then show "?P e op <"
  1.5482 +        apply (erule_tac x="e / 2 / content (cbox a b)" in allE)
  1.5483 +        apply (erule impE)
  1.5484 +        defer
  1.5485 +        apply (erule exE,rule_tac x=d in exI)
  1.5486 +        using F e
  1.5487 +        apply (auto simp add: field_simps)
  1.5488 +        done
  1.5489 +    }
  1.5490 +  qed
  1.5491 +qed
  1.5492 +
  1.5493 +lemma has_integral_factor_content_real:
  1.5494 +  "(f has_integral i) {a .. b::real} \<longleftrightarrow>
  1.5495 +    (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a .. b}  \<and> d fine p \<longrightarrow>
  1.5496 +      norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content {a .. b} ))"
  1.5497 +  unfolding box_real[symmetric]
  1.5498 +  by (rule has_integral_factor_content)
  1.5499 +
  1.5500 +
  1.5501 +subsection \<open>Fundamental theorem of calculus.\<close>
  1.5502 +
  1.5503 +lemma interval_bounds_real:
  1.5504 +  fixes q b :: real
  1.5505 +  assumes "a \<le> b"
  1.5506 +  shows "Sup {a..b} = b"
  1.5507 +    and "Inf {a..b} = a"
  1.5508 +  using assms by auto
  1.5509 +
  1.5510 +lemma fundamental_theorem_of_calculus:
  1.5511 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.5512 +  assumes "a \<le> b"
  1.5513 +    and "\<forall>x\<in>{a .. b}. (f has_vector_derivative f' x) (at x within {a .. b})"
  1.5514 +  shows "(f' has_integral (f b - f a)) {a .. b}"
  1.5515 +  unfolding has_integral_factor_content box_real[symmetric]
  1.5516 +proof safe
  1.5517 +  fix e :: real
  1.5518 +  assume e: "e > 0"
  1.5519 +  note assm = assms(2)[unfolded has_vector_derivative_def has_derivative_within_alt]
  1.5520 +  have *: "\<And>P Q. \<forall>x\<in>{a .. b}. P x \<and> (\<forall>e>0. \<exists>d>0. Q x e d) \<Longrightarrow> \<forall>x. \<exists>(d::real)>0. x\<in>{a .. b} \<longrightarrow> Q x e d"
  1.5521 +    using e by blast
  1.5522 +  note this[OF assm,unfolded gauge_existence_lemma]
  1.5523 +  from choice[OF this,unfolded Ball_def[symmetric]] guess d ..
  1.5524 +  note d=conjunctD2[OF this[rule_format],rule_format]
  1.5525 +  show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  1.5526 +    norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content (cbox a b))"
  1.5527 +    apply (rule_tac x="\<lambda>x. ball x (d x)" in exI)
  1.5528 +    apply safe
  1.5529 +    apply (rule gauge_ball_dependent)
  1.5530 +    apply rule
  1.5531 +    apply (rule d(1))
  1.5532 +  proof -
  1.5533 +    fix p
  1.5534 +    assume as: "p tagged_division_of cbox a b" "(\<lambda>x. ball x (d x)) fine p"
  1.5535 +    show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content (cbox a b)"
  1.5536 +      unfolding content_real[OF assms(1), simplified box_real[symmetric]] additive_tagged_division_1[OF assms(1) as(1)[simplified box_real],of f,symmetric]
  1.5537 +      unfolding additive_tagged_division_1[OF assms(1) as(1)[simplified box_real],of "\<lambda>x. x",symmetric]
  1.5538 +      unfolding setsum_right_distrib
  1.5539 +      defer
  1.5540 +      unfolding setsum_subtractf[symmetric]
  1.5541 +    proof (rule setsum_norm_le,safe)
  1.5542 +      fix x k
  1.5543 +      assume "(x, k) \<in> p"
  1.5544 +      note xk = tagged_division_ofD(2-4)[OF as(1) this]
  1.5545 +      from this(3) guess u v by (elim exE) note k=this
  1.5546 +      have *: "u \<le> v"
  1.5547 +        using xk unfolding k by auto
  1.5548 +      have ball: "\<forall>xa\<in>k. xa \<in> ball x (d x)"
  1.5549 +        using as(2)[unfolded fine_def,rule_format,OF \<open>(x,k)\<in>p\<close>,unfolded split_conv subset_eq] .
  1.5550 +      have "norm ((v - u) *\<^sub>R f' x - (f v - f u)) \<le>
  1.5551 +        norm (f u - f x - (u - x) *\<^sub>R f' x) + norm (f v - f x - (v - x) *\<^sub>R f' x)"
  1.5552 +        apply (rule order_trans[OF _ norm_triangle_ineq4])
  1.5553 +        apply (rule eq_refl)
  1.5554 +        apply (rule arg_cong[where f=norm])
  1.5555 +        unfolding scaleR_diff_left
  1.5556 +        apply (auto simp add:algebra_simps)
  1.5557 +        done
  1.5558 +      also have "\<dots> \<le> e * norm (u - x) + e * norm (v - x)"
  1.5559 +        apply (rule add_mono)
  1.5560 +        apply (rule d(2)[of "x" "u",unfolded o_def])
  1.5561 +        prefer 4
  1.5562 +        apply (rule d(2)[of "x" "v",unfolded o_def])
  1.5563 +        using ball[rule_format,of u] ball[rule_format,of v]
  1.5564 +        using xk(1-2)
  1.5565 +        unfolding k subset_eq
  1.5566 +        apply (auto simp add:dist_real_def)
  1.5567 +        done
  1.5568 +      also have "\<dots> \<le> e * (Sup k - Inf k)"
  1.5569 +        unfolding k interval_bounds_real[OF *]
  1.5570 +        using xk(1)
  1.5571 +        unfolding k
  1.5572 +        by (auto simp add: dist_real_def field_simps)
  1.5573 +      finally show "norm (content k *\<^sub>R f' x - (f (Sup k) - f (Inf k))) \<le>
  1.5574 +        e * (Sup k - Inf k)"
  1.5575 +        unfolding box_real k interval_bounds_real[OF *] content_real[OF *]
  1.5576 +          interval_upperbound_real interval_lowerbound_real
  1.5577 +          .
  1.5578 +    qed
  1.5579 +  qed
  1.5580 +qed
  1.5581 +
  1.5582 +lemma ident_has_integral:
  1.5583 +  fixes a::real
  1.5584 +  assumes "a \<le> b"
  1.5585 +  shows "((\<lambda>x. x) has_integral (b\<^sup>2 - a\<^sup>2) / 2) {a..b}"
  1.5586 +proof -
  1.5587 +  have "((\<lambda>x. x) has_integral inverse 2 * b\<^sup>2 - inverse 2 * a\<^sup>2) {a..b}"
  1.5588 +    apply (rule fundamental_theorem_of_calculus [OF assms], clarify)
  1.5589 +    unfolding power2_eq_square
  1.5590 +    by (rule derivative_eq_intros | simp)+
  1.5591 +  then show ?thesis
  1.5592 +    by (simp add: field_simps)
  1.5593 +qed
  1.5594 +
  1.5595 +lemma integral_ident [simp]:
  1.5596 +  fixes a::real
  1.5597 +  assumes "a \<le> b"
  1.5598 +  shows "integral {a..b} (\<lambda>x. x) = (if a \<le> b then (b\<^sup>2 - a\<^sup>2) / 2 else 0)"
  1.5599 +using ident_has_integral integral_unique by fastforce
  1.5600 +
  1.5601 +lemma ident_integrable_on:
  1.5602 +  fixes a::real
  1.5603 +  shows "(\<lambda>x. x) integrable_on {a..b}"
  1.5604 +by (metis atLeastatMost_empty_iff integrable_on_def has_integral_empty ident_has_integral)
  1.5605 +
  1.5606 +
  1.5607 +subsection \<open>Taylor series expansion\<close>
  1.5608 +
  1.5609 +lemma (in bounded_bilinear) setsum_prod_derivatives_has_vector_derivative:
  1.5610 +  assumes "p>0"
  1.5611 +  and f0: "Df 0 = f"
  1.5612 +  and Df: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  1.5613 +    (Df m has_vector_derivative Df (Suc m) t) (at t within {a .. b})"
  1.5614 +  and g0: "Dg 0 = g"
  1.5615 +  and Dg: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  1.5616 +    (Dg m has_vector_derivative Dg (Suc m) t) (at t within {a .. b})"
  1.5617 +  and ivl: "a \<le> t" "t \<le> b"
  1.5618 +  shows "((\<lambda>t. \<Sum>i<p. (-1)^i *\<^sub>R prod (Df i t) (Dg (p - Suc i) t))
  1.5619 +    has_vector_derivative
  1.5620 +      prod (f t) (Dg p t) - (-1)^p *\<^sub>R prod (Df p t) (g t))
  1.5621 +    (at t within {a .. b})"
  1.5622 +  using assms
  1.5623 +proof cases
  1.5624 +  assume p: "p \<noteq> 1"
  1.5625 +  define p' where "p' = p - 2"
  1.5626 +  from assms p have p': "{..<p} = {..Suc p'}" "p = Suc (Suc p')"
  1.5627 +    by (auto simp: p'_def)
  1.5628 +  have *: "\<And>i. i \<le> p' \<Longrightarrow> Suc (Suc p' - i) = (Suc (Suc p') - i)"
  1.5629 +    by auto
  1.5630 +  let ?f = "\<lambda>i. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg ((p - i)) t))"
  1.5631 +  have "(\<Sum>i<p. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg (Suc (p - Suc i)) t) +
  1.5632 +    prod (Df (Suc i) t) (Dg (p - Suc i) t))) =
  1.5633 +    (\<Sum>i\<le>(Suc p'). ?f i - ?f (Suc i))"
  1.5634 +    by (auto simp: algebra_simps p'(2) numeral_2_eq_2 * lessThan_Suc_atMost)
  1.5635 +  also note setsum_telescope
  1.5636 +  finally
  1.5637 +  have "(\<Sum>i<p. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg (Suc (p - Suc i)) t) +
  1.5638 +    prod (Df (Suc i) t) (Dg (p - Suc i) t)))
  1.5639 +    = prod (f t) (Dg p t) - (- 1) ^ p *\<^sub>R prod (Df p t) (g t)"
  1.5640 +    unfolding p'[symmetric]
  1.5641 +    by (simp add: assms)
  1.5642 +  thus ?thesis
  1.5643 +    using assms
  1.5644 +    by (auto intro!: derivative_eq_intros has_vector_derivative)
  1.5645 +qed (auto intro!: derivative_eq_intros has_vector_derivative)
  1.5646 +
  1.5647 +lemma
  1.5648 +  fixes f::"real\<Rightarrow>'a::banach"
  1.5649 +  assumes "p>0"
  1.5650 +  and f0: "Df 0 = f"
  1.5651 +  and Df: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  1.5652 +    (Df m has_vector_derivative Df (Suc m) t) (at t within {a .. b})"
  1.5653 +  and ivl: "a \<le> b"
  1.5654 +  defines "i \<equiv> \<lambda>x. ((b - x) ^ (p - 1) / fact (p - 1)) *\<^sub>R Df p x"
  1.5655 +  shows taylor_has_integral:
  1.5656 +    "(i has_integral f b - (\<Sum>i<p. ((b - a) ^ i / fact i) *\<^sub>R Df i a)) {a..b}"
  1.5657 +  and taylor_integral:
  1.5658 +    "f b = (\<Sum>i<p. ((b - a) ^ i / fact i) *\<^sub>R Df i a) + integral {a..b} i"
  1.5659 +  and taylor_integrable:
  1.5660 +    "i integrable_on {a .. b}"
  1.5661 +proof goal_cases
  1.5662 +  case 1
  1.5663 +  interpret bounded_bilinear "scaleR::real\<Rightarrow>'a\<Rightarrow>'a"
  1.5664 +    by (rule bounded_bilinear_scaleR)
  1.5665 +  define g where "g s = (b - s)^(p - 1)/fact (p - 1)" for s
  1.5666 +  define Dg where [abs_def]:
  1.5667 +    "Dg n s = (if n < p then (-1)^n * (b - s)^(p - 1 - n) / fact (p - 1 - n) else 0)" for n s
  1.5668 +  have g0: "Dg 0 = g"
  1.5669 +    using \<open>p > 0\<close>
  1.5670 +    by (auto simp add: Dg_def divide_simps g_def split: if_split_asm)
  1.5671 +  {
  1.5672 +    fix m
  1.5673 +    assume "p > Suc m"
  1.5674 +    hence "p - Suc m = Suc (p - Suc (Suc m))"
  1.5675 +      by auto
  1.5676 +    hence "real (p - Suc m) * fact (p - Suc (Suc m)) = fact (p - Suc m)"
  1.5677 +      by auto
  1.5678 +  } note fact_eq = this
  1.5679 +  have Dg: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  1.5680 +    (Dg m has_vector_derivative Dg (Suc m) t) (at t within {a .. b})"
  1.5681 +    unfolding Dg_def
  1.5682 +    by (auto intro!: derivative_eq_intros simp: has_vector_derivative_def fact_eq divide_simps)
  1.5683 +  let ?sum = "\<lambda>t. \<Sum>i<p. (- 1) ^ i *\<^sub>R Dg i t *\<^sub>R Df (p - Suc i) t"
  1.5684 +  from setsum_prod_derivatives_has_vector_derivative[of _ Dg _ _ _ Df,
  1.5685 +      OF \<open>p > 0\<close> g0 Dg f0 Df]
  1.5686 +  have deriv: "\<And>t. a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  1.5687 +    (?sum has_vector_derivative
  1.5688 +      g t *\<^sub>R Df p t - (- 1) ^ p *\<^sub>R Dg p t *\<^sub>R f t) (at t within {a..b})"
  1.5689 +    by auto
  1.5690 +  from fundamental_theorem_of_calculus[rule_format, OF \<open>a \<le> b\<close> deriv]
  1.5691 +  have "(i has_integral ?sum b - ?sum a) {a .. b}"
  1.5692 +    by (simp add: i_def g_def Dg_def)
  1.5693 +  also
  1.5694 +  have one: "(- 1) ^ p' * (- 1) ^ p' = (1::real)"
  1.5695 +    and "{..<p} \<inter> {i. p = Suc i} = {p - 1}"
  1.5696 +    for p'
  1.5697 +    using \<open>p > 0\<close>
  1.5698 +    by (auto simp: power_mult_distrib[symmetric])
  1.5699 +  then have "?sum b = f b"
  1.5700 +    using Suc_pred'[OF \<open>p > 0\<close>]
  1.5701 +    by (simp add: diff_eq_eq Dg_def power_0_left le_Suc_eq if_distrib
  1.5702 +        cond_application_beta setsum.If_cases f0)
  1.5703 +  also
  1.5704 +  have "{..<p} = (\<lambda>x. p - x - 1) ` {..<p}"
  1.5705 +  proof safe
  1.5706 +    fix x
  1.5707 +    assume "x < p"
  1.5708 +    thus "x \<in> (\<lambda>x. p - x - 1) ` {..<p}"
  1.5709 +      by (auto intro!: image_eqI[where x = "p - x - 1"])
  1.5710 +  qed simp
  1.5711 +  from _ this
  1.5712 +  have "?sum a = (\<Sum>i<p. ((b - a) ^ i / fact i) *\<^sub>R Df i a)"
  1.5713 +    by (rule setsum.reindex_cong) (auto simp add: inj_on_def Dg_def one)
  1.5714 +  finally show c: ?case .
  1.5715 +  case 2 show ?case using c integral_unique by force
  1.5716 +  case 3 show ?case using c by force
  1.5717 +qed
  1.5718 +
  1.5719 +
  1.5720 +subsection \<open>Attempt a systematic general set of "offset" results for components.\<close>
  1.5721 +
  1.5722 +lemma gauge_modify:
  1.5723 +  assumes "(\<forall>s. open s \<longrightarrow> open {x. f(x) \<in> s})" "gauge d"
  1.5724 +  shows "gauge (\<lambda>x. {y. f y \<in> d (f x)})"
  1.5725 +  using assms
  1.5726 +  unfolding gauge_def
  1.5727 +  apply safe
  1.5728 +  defer
  1.5729 +  apply (erule_tac x="f x" in allE)
  1.5730 +  apply (erule_tac x="d (f x)" in allE)
  1.5731 +  apply auto
  1.5732 +  done
  1.5733 +
  1.5734 +
  1.5735 +subsection \<open>Only need trivial subintervals if the interval itself is trivial.\<close>
  1.5736 +
  1.5737 +lemma division_of_nontrivial:
  1.5738 +  fixes s :: "'a::euclidean_space set set"
  1.5739 +  assumes "s division_of (cbox a b)"
  1.5740 +    and "content (cbox a b) \<noteq> 0"
  1.5741 +  shows "{k. k \<in> s \<and> content k \<noteq> 0} division_of (cbox a b)"
  1.5742 +  using assms(1)
  1.5743 +  apply -
  1.5744 +proof (induct "card s" arbitrary: s rule: nat_less_induct)
  1.5745 +  fix s::"'a set set"
  1.5746 +  assume assm: "s division_of (cbox a b)"
  1.5747 +    "\<forall>m<card s. \<forall>x. m = card x \<longrightarrow>
  1.5748 +      x division_of (cbox a b) \<longrightarrow> {k \<in> x. content k \<noteq> 0} division_of (cbox a b)"
  1.5749 +  note s = division_ofD[OF assm(1)]
  1.5750 +  let ?thesis = "{k \<in> s. content k \<noteq> 0} division_of (cbox a b)"
  1.5751 +  {
  1.5752 +    presume *: "{k \<in> s. content k \<noteq> 0} \<noteq> s \<Longrightarrow> ?thesis"
  1.5753 +    show ?thesis
  1.5754 +      apply cases
  1.5755 +      defer
  1.5756 +      apply (rule *)
  1.5757 +      apply assumption
  1.5758 +      using assm(1)
  1.5759 +      apply auto
  1.5760 +      done
  1.5761 +  }
  1.5762 +  assume noteq: "{k \<in> s. content k \<noteq> 0} \<noteq> s"
  1.5763 +  then obtain k where k: "k \<in> s" "content k = 0"
  1.5764 +    by auto
  1.5765 +  from s(4)[OF k(1)] guess c d by (elim exE) note k=k this
  1.5766 +  from k have "card s > 0"
  1.5767 +    unfolding card_gt_0_iff using assm(1) by auto
  1.5768 +  then have card: "card (s - {k}) < card s"
  1.5769 +    using assm(1) k(1)
  1.5770 +    apply (subst card_Diff_singleton_if)
  1.5771 +    apply auto
  1.5772 +    done
  1.5773 +  have *: "closed (\<Union>(s - {k}))"
  1.5774 +    apply (rule closed_Union)
  1.5775 +    defer
  1.5776 +    apply rule
  1.5777 +    apply (drule DiffD1,drule s(4))
  1.5778 +    using assm(1)
  1.5779 +    apply auto
  1.5780 +    done
  1.5781 +  have "k \<subseteq> \<Union>(s - {k})"
  1.5782 +    apply safe
  1.5783 +    apply (rule *[unfolded closed_limpt,rule_format])
  1.5784 +    unfolding islimpt_approachable
  1.5785 +  proof safe
  1.5786 +    fix x
  1.5787 +    fix e :: real
  1.5788 +    assume as: "x \<in> k" "e > 0"
  1.5789 +    from k(2)[unfolded k content_eq_0] guess i ..
  1.5790 +    then have i:"c\<bullet>i = d\<bullet>i" "i\<in>Basis"
  1.5791 +      using s(3)[OF k(1),unfolded k] unfolding box_ne_empty by auto
  1.5792 +    then have xi: "x\<bullet>i = d\<bullet>i"
  1.5793 +      using as unfolding k mem_box by (metis antisym)
  1.5794 +    define y where "y = (\<Sum>j\<in>Basis. (if j = i then if c\<bullet>i \<le> (a\<bullet>i + b\<bullet>i) / 2 then c\<bullet>i +
  1.5795 +      min e (b\<bullet>i - c\<bullet>i) / 2 else c\<bullet>i - min e (c\<bullet>i - a\<bullet>i) / 2 else x\<bullet>j) *\<^sub>R j)"
  1.5796 +    show "\<exists>x'\<in>\<Union>(s - {k}). x' \<noteq> x \<and> dist x' x < e"
  1.5797 +      apply (rule_tac x=y in bexI)
  1.5798 +    proof
  1.5799 +      have "d \<in> cbox c d"
  1.5800 +        using s(3)[OF k(1)]
  1.5801 +        unfolding k box_eq_empty mem_box
  1.5802 +        by (fastforce simp add: not_less)
  1.5803 +      then have "d \<in> cbox a b"
  1.5804 +        using s(2)[OF k(1)]
  1.5805 +        unfolding k
  1.5806 +        by auto
  1.5807 +      note di = this[unfolded mem_box,THEN bspec[where x=i]]
  1.5808 +      then have xyi: "y\<bullet>i \<noteq> x\<bullet>i"
  1.5809 +        unfolding y_def i xi
  1.5810 +        using as(2) assms(2)[unfolded content_eq_0] i(2)
  1.5811 +        by (auto elim!: ballE[of _ _ i])
  1.5812 +      then show "y \<noteq> x"
  1.5813 +        unfolding euclidean_eq_iff[where 'a='a] using i by auto
  1.5814 +      have *: "Basis = insert i (Basis - {i})"
  1.5815 +        using i by auto
  1.5816 +      have "norm (y - x) < e + setsum (\<lambda>i. 0) Basis"
  1.5817 +        apply (rule le_less_trans[OF norm_le_l1])
  1.5818 +        apply (subst *)
  1.5819 +        apply (subst setsum.insert)
  1.5820 +        prefer 3
  1.5821 +        apply (rule add_less_le_mono)
  1.5822 +      proof -
  1.5823 +        show "\<bar>(y - x) \<bullet> i\<bar> < e"
  1.5824 +          using di as(2) y_def i xi by (auto simp: inner_simps)
  1.5825 +        show "(\<Sum>i\<in>Basis - {i}. \<bar>(y - x) \<bullet> i\<bar>) \<le> (\<Sum>i\<in>Basis. 0)"
  1.5826 +          unfolding y_def by (auto simp: inner_simps)
  1.5827 +      qed auto
  1.5828 +      then show "dist y x < e"
  1.5829 +        unfolding dist_norm by auto
  1.5830 +      have "y \<notin> k"
  1.5831 +        unfolding k mem_box
  1.5832 +        apply rule
  1.5833 +        apply (erule_tac x=i in ballE)
  1.5834 +        using xyi k i xi
  1.5835 +        apply auto
  1.5836 +        done
  1.5837 +      moreover
  1.5838 +      have "y \<in> \<Union>s"
  1.5839 +        using set_rev_mp[OF as(1) s(2)[OF k(1)]] as(2) di i
  1.5840 +        unfolding s mem_box y_def
  1.5841 +        by (auto simp: field_simps elim!: ballE[of _ _ i])
  1.5842 +      ultimately
  1.5843 +      show "y \<in> \<Union>(s - {k})" by auto
  1.5844 +    qed
  1.5845 +  qed
  1.5846 +  then have "\<Union>(s - {k}) = cbox a b"
  1.5847 +    unfolding s(6)[symmetric] by auto
  1.5848 +  then have  "{ka \<in> s - {k}. content ka \<noteq> 0} division_of (cbox a b)"
  1.5849 +    apply -
  1.5850 +    apply (rule assm(2)[rule_format,OF card refl])
  1.5851 +    apply (rule division_ofI)
  1.5852 +    defer
  1.5853 +    apply (rule_tac[1-4] s)
  1.5854 +    using assm(1)
  1.5855 +    apply auto
  1.5856 +    done
  1.5857 +  moreover
  1.5858 +  have "{ka \<in> s - {k}. content ka \<noteq> 0} = {k \<in> s. content k \<noteq> 0}"
  1.5859 +    using k by auto
  1.5860 +  ultimately show ?thesis by auto
  1.5861 +qed
  1.5862 +
  1.5863 +
  1.5864 +subsection \<open>Integrability on subintervals.\<close>
  1.5865 +
  1.5866 +lemma operative_integrable:
  1.5867 +  fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  1.5868 +  shows "comm_monoid.operative op \<and> True (\<lambda>i. f integrable_on i)"
  1.5869 +  unfolding comm_monoid.operative_def[OF comm_monoid_and]
  1.5870 +  apply safe
  1.5871 +  apply (subst integrable_on_def)
  1.5872 +  unfolding has_integral_null_eq
  1.5873 +  apply (rule, rule refl)
  1.5874 +  apply (rule, assumption, assumption)+
  1.5875 +  unfolding integrable_on_def
  1.5876 +  by (auto intro!: has_integral_split)
  1.5877 +
  1.5878 +lemma integrable_subinterval:
  1.5879 +  fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  1.5880 +  assumes "f integrable_on cbox a b"
  1.5881 +    and "cbox c d \<subseteq> cbox a b"
  1.5882 +  shows "f integrable_on cbox c d"
  1.5883 +  apply (cases "cbox c d = {}")
  1.5884 +  defer
  1.5885 +  apply (rule partial_division_extend_1[OF assms(2)],assumption)
  1.5886 +  using comm_monoid_set.operative_division[OF comm_monoid_set_and operative_integrable,symmetric,of _ _ _ f] assms(1)
  1.5887 +  apply (auto simp: comm_monoid_set_F_and)
  1.5888 +  done
  1.5889 +
  1.5890 +lemma integrable_subinterval_real:
  1.5891 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.5892 +  assumes "f integrable_on {a .. b}"
  1.5893 +    and "{c .. d} \<subseteq> {a .. b}"
  1.5894 +  shows "f integrable_on {c .. d}"
  1.5895 +  by (metis assms(1) assms(2) box_real(2) integrable_subinterval)
  1.5896 +
  1.5897 +
  1.5898 +subsection \<open>Combining adjacent intervals in 1 dimension.\<close>
  1.5899 +
  1.5900 +lemma has_integral_combine:
  1.5901 +  fixes a b c :: real
  1.5902 +  assumes "a \<le> c"
  1.5903 +    and "c \<le> b"
  1.5904 +    and "(f has_integral i) {a .. c}"
  1.5905 +    and "(f has_integral (j::'a::banach)) {c .. b}"
  1.5906 +  shows "(f has_integral (i + j)) {a .. b}"
  1.5907 +proof -
  1.5908 +  note operative_integral[of f, unfolded comm_monoid.operative_1_le[OF add.comm_monoid_lift_option]]
  1.5909 +  note conjunctD2[OF this,rule_format]
  1.5910 +  note * = this(2)[OF conjI[OF assms(1-2)],unfolded if_P[OF assms(3)]]
  1.5911 +  then have "f integrable_on cbox a b"
  1.5912 +    apply -
  1.5913 +    apply (rule ccontr)
  1.5914 +    apply (subst(asm) if_P)
  1.5915 +    defer
  1.5916 +    apply (subst(asm) if_P)
  1.5917 +    using assms(3-)
  1.5918 +    apply auto
  1.5919 +    done
  1.5920 +  with *
  1.5921 +  show ?thesis
  1.5922 +    apply -
  1.5923 +    apply (subst(asm) if_P)
  1.5924 +    defer
  1.5925 +    apply (subst(asm) if_P)
  1.5926 +    defer
  1.5927 +    apply (subst(asm) if_P)
  1.5928 +    using assms(3-)
  1.5929 +    apply (auto simp add: integrable_on_def integral_unique)
  1.5930 +    done
  1.5931 +qed
  1.5932 +
  1.5933 +lemma integral_combine:
  1.5934 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.5935 +  assumes "a \<le> c"
  1.5936 +    and "c \<le> b"
  1.5937 +    and "f integrable_on {a .. b}"
  1.5938 +  shows "integral {a .. c} f + integral {c .. b} f = integral {a .. b} f"
  1.5939 +  apply (rule integral_unique[symmetric])
  1.5940 +  apply (rule has_integral_combine[OF assms(1-2)])
  1.5941 +  apply (metis assms(2) assms(3) atLeastatMost_subset_iff box_real(2) content_pos_le content_real_eq_0 integrable_integral integrable_subinterval le_add_same_cancel2 monoid_add_class.add.left_neutral)
  1.5942 +  by (metis assms(1) assms(3) atLeastatMost_subset_iff box_real(2) content_pos_le content_real_eq_0 integrable_integral integrable_subinterval le_add_same_cancel1 monoid_add_class.add.right_neutral)
  1.5943 +
  1.5944 +lemma integrable_combine:
  1.5945 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.5946 +  assumes "a \<le> c"
  1.5947 +    and "c \<le> b"
  1.5948 +    and "f integrable_on {a .. c}"
  1.5949 +    and "f integrable_on {c .. b}"
  1.5950 +  shows "f integrable_on {a .. b}"
  1.5951 +  using assms
  1.5952 +  unfolding integrable_on_def
  1.5953 +  by (fastforce intro!:has_integral_combine)
  1.5954 +
  1.5955 +
  1.5956 +subsection \<open>Reduce integrability to "local" integrability.\<close>
  1.5957 +
  1.5958 +lemma integrable_on_little_subintervals:
  1.5959 +  fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  1.5960 +  assumes "\<forall>x\<in>cbox a b. \<exists>d>0. \<forall>u v. x \<in> cbox u v \<and> cbox u v \<subseteq> ball x d \<and> cbox u v \<subseteq> cbox a b \<longrightarrow>
  1.5961 +    f integrable_on cbox u v"
  1.5962 +  shows "f integrable_on cbox a b"
  1.5963 +proof -
  1.5964 +  have "\<forall>x. \<exists>d. x\<in>cbox a b \<longrightarrow> d>0 \<and> (\<forall>u v. x \<in> cbox u v \<and> cbox u v \<subseteq> ball x d \<and> cbox u v \<subseteq> cbox a b \<longrightarrow>
  1.5965 +    f integrable_on cbox u v)"
  1.5966 +    using assms by auto
  1.5967 +  note this[unfolded gauge_existence_lemma]
  1.5968 +  from choice[OF this] guess d .. note d=this[rule_format]
  1.5969 +  guess p
  1.5970 +    apply (rule fine_division_exists[OF gauge_ball_dependent,of d a b])
  1.5971 +    using d
  1.5972 +    by auto
  1.5973 +  note p=this(1-2)
  1.5974 +  note division_of_tagged_division[OF this(1)]
  1.5975 +  note * = comm_monoid_set.operative_division[OF comm_monoid_set_and operative_integrable, OF this, symmetric, of f]
  1.5976 +  show ?thesis
  1.5977 +    unfolding * comm_monoid_set_F_and
  1.5978 +    apply safe
  1.5979 +    unfolding snd_conv
  1.5980 +  proof -
  1.5981 +    fix x k
  1.5982 +    assume "(x, k) \<in> p"
  1.5983 +    note tagged_division_ofD(2-4)[OF p(1) this] fineD[OF p(2) this]
  1.5984 +    then show "f integrable_on k"
  1.5985 +      apply safe
  1.5986 +      apply (rule d[THEN conjunct2,rule_format,of x])
  1.5987 +      apply (auto intro: order.trans)
  1.5988 +      done
  1.5989 +  qed
  1.5990 +qed
  1.5991 +
  1.5992 +
  1.5993 +subsection \<open>Second FTC or existence of antiderivative.\<close>
  1.5994 +
  1.5995 +lemma integrable_const[intro]: "(\<lambda>x. c) integrable_on cbox a b"
  1.5996 +  unfolding integrable_on_def
  1.5997 +  apply rule
  1.5998 +  apply (rule has_integral_const)
  1.5999 +  done
  1.6000 +
  1.6001 +lemma integral_has_vector_derivative_continuous_at:
  1.6002 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.6003 +  assumes f: "f integrable_on {a..b}"
  1.6004 +      and x: "x \<in> {a..b}"
  1.6005 +      and fx: "continuous (at x within {a..b}) f"
  1.6006 +  shows "((\<lambda>u. integral {a..u} f) has_vector_derivative f x) (at x within {a..b})"
  1.6007 +proof -
  1.6008 +  let ?I = "\<lambda>a b. integral {a..b} f"
  1.6009 +  { fix e::real
  1.6010 +    assume "e > 0"
  1.6011 +    obtain d where "d>0" and d: "\<And>x'. \<lbrakk>x' \<in> {a..b}; \<bar>x' - x\<bar> < d\<rbrakk> \<Longrightarrow> norm(f x' - f x) \<le> e"
  1.6012 +      using \<open>e>0\<close> fx by (auto simp: continuous_within_eps_delta dist_norm less_imp_le)
  1.6013 +    have "norm (integral {a..y} f - integral {a..x} f - (y - x) *\<^sub>R f x) \<le> e * \<bar>y - x\<bar>"
  1.6014 +           if y: "y \<in> {a..b}" and yx: "\<bar>y - x\<bar> < d" for y
  1.6015 +    proof (cases "y < x")
  1.6016 +      case False
  1.6017 +      have "f integrable_on {a..y}"
  1.6018 +        using f y by (simp add: integrable_subinterval_real)
  1.6019 +      then have Idiff: "?I a y - ?I a x = ?I x y"
  1.6020 +        using False x by (simp add: algebra_simps integral_combine)
  1.6021 +      have fux_int: "((\<lambda>u. f u - f x) has_integral integral {x..y} f - (y - x) *\<^sub>R f x) {x..y}"
  1.6022 +        apply (rule has_integral_sub)
  1.6023 +        using x y apply (force intro: integrable_integral [OF integrable_subinterval_real [OF f]])
  1.6024 +        using has_integral_const_real [of "f x" x y] False
  1.6025 +        apply (simp add: )
  1.6026 +        done
  1.6027 +      show ?thesis
  1.6028 +        using False
  1.6029 +        apply (simp add: abs_eq_content del: content_real_if)
  1.6030 +        apply (rule has_integral_bound_real[where f="(\<lambda>u. f u - f x)"])
  1.6031 +        using yx False d x y \<open>e>0\<close> apply (auto simp add: Idiff fux_int)
  1.6032 +        done
  1.6033 +    next
  1.6034 +      case True
  1.6035 +      have "f integrable_on {a..x}"
  1.6036 +        using f x by (simp add: integrable_subinterval_real)
  1.6037 +      then have Idiff: "?I a x - ?I a y = ?I y x"
  1.6038 +        using True x y by (simp add: algebra_simps integral_combine)
  1.6039 +      have fux_int: "((\<lambda>u. f u - f x) has_integral integral {y..x} f - (x - y) *\<^sub>R f x) {y..x}"
  1.6040 +        apply (rule has_integral_sub)
  1.6041 +        using x y apply (force intro: integrable_integral [OF integrable_subinterval_real [OF f]])
  1.6042 +        using has_integral_const_real [of "f x" y x] True
  1.6043 +        apply (simp add: )
  1.6044 +        done
  1.6045 +      have "norm (integral {a..x} f - integral {a..y} f - (x - y) *\<^sub>R f x) \<le> e * \<bar>y - x\<bar>"
  1.6046 +        using True
  1.6047 +        apply (simp add: abs_eq_content del: content_real_if)
  1.6048 +        apply (rule has_integral_bound_real[where f="(\<lambda>u. f u - f x)"])
  1.6049 +        using yx True d x y \<open>e>0\<close> apply (auto simp add: Idiff fux_int)
  1.6050 +        done
  1.6051 +      then show ?thesis
  1.6052 +        by (simp add: algebra_simps norm_minus_commute)
  1.6053 +    qed
  1.6054 +    then have "\<exists>d>0. \<forall>y\<in>{a..b}. \<bar>y - x\<bar> < d \<longrightarrow> norm (integral {a..y} f - integral {a..x} f - (y - x) *\<^sub>R f x) \<le> e * \<bar>y - x\<bar>"
  1.6055 +      using \<open>d>0\<close> by blast
  1.6056 +  }
  1.6057 +  then show ?thesis
  1.6058 +    by (simp add: has_vector_derivative_def has_derivative_within_alt bounded_linear_scaleR_left)
  1.6059 +qed
  1.6060 +
  1.6061 +lemma integral_has_vector_derivative:
  1.6062 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.6063 +  assumes "continuous_on {a .. b} f"
  1.6064 +    and "x \<in> {a .. b}"
  1.6065 +  shows "((\<lambda>u. integral {a .. u} f) has_vector_derivative f(x)) (at x within {a .. b})"
  1.6066 +apply (rule integral_has_vector_derivative_continuous_at [OF integrable_continuous_real])
  1.6067 +using assms
  1.6068 +apply (auto simp: continuous_on_eq_continuous_within)
  1.6069 +done
  1.6070 +
  1.6071 +lemma antiderivative_continuous:
  1.6072 +  fixes q b :: real
  1.6073 +  assumes "continuous_on {a .. b} f"
  1.6074 +  obtains g where "\<forall>x\<in>{a .. b}. (g has_vector_derivative (f x::_::banach)) (at x within {a .. b})"
  1.6075 +  apply (rule that)
  1.6076 +  apply rule
  1.6077 +  using integral_has_vector_derivative[OF assms]
  1.6078 +  apply auto
  1.6079 +  done
  1.6080 +
  1.6081 +
  1.6082 +subsection \<open>Combined fundamental theorem of calculus.\<close>
  1.6083 +
  1.6084 +lemma antiderivative_integral_continuous:
  1.6085 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.6086 +  assumes "continuous_on {a .. b} f"
  1.6087 +  obtains g where "\<forall>u\<in>{a .. b}. \<forall>v \<in> {a .. b}. u \<le> v \<longrightarrow> (f has_integral (g v - g u)) {u .. v}"
  1.6088 +proof -
  1.6089 +  from antiderivative_continuous[OF assms] guess g . note g=this
  1.6090 +  show ?thesis
  1.6091 +    apply (rule that[of g])
  1.6092 +    apply safe
  1.6093 +  proof goal_cases
  1.6094 +    case prems: (1 u v)
  1.6095 +    have "\<forall>x\<in>cbox u v. (g has_vector_derivative f x) (at x within cbox u v)"
  1.6096 +      apply rule
  1.6097 +      apply (rule has_vector_derivative_within_subset)
  1.6098 +      apply (rule g[rule_format])
  1.6099 +      using prems(1,2)
  1.6100 +      apply auto
  1.6101 +      done
  1.6102 +    then show ?case
  1.6103 +      using fundamental_theorem_of_calculus[OF prems(3), of g f] by auto
  1.6104 +  qed
  1.6105 +qed
  1.6106 +
  1.6107 +
  1.6108 +subsection \<open>General "twiddling" for interval-to-interval function image.\<close>
  1.6109 +
  1.6110 +lemma has_integral_twiddle:
  1.6111 +  assumes "0 < r"
  1.6112 +    and "\<forall>x. h(g x) = x"
  1.6113 +    and "\<forall>x. g(h x) = x"
  1.6114 +    and "\<forall>x. continuous (at x) g"
  1.6115 +    and "\<forall>u v. \<exists>w z. g ` cbox u v = cbox w z"
  1.6116 +    and "\<forall>u v. \<exists>w z. h ` cbox u v = cbox w z"
  1.6117 +    and "\<forall>u v. content(g ` cbox u v) = r * content (cbox u v)"
  1.6118 +    and "(f has_integral i) (cbox a b)"
  1.6119 +  shows "((\<lambda>x. f(g x)) has_integral (1 / r) *\<^sub>R i) (h ` cbox a b)"
  1.6120 +proof -
  1.6121 +  show ?thesis when *: "cbox a b \<noteq> {} \<Longrightarrow> ?thesis"
  1.6122 +    apply cases
  1.6123 +    defer
  1.6124 +    apply (rule *)
  1.6125 +    apply assumption
  1.6126 +  proof goal_cases
  1.6127 +    case prems: 1
  1.6128 +    then show ?thesis
  1.6129 +      unfolding prems assms(8)[unfolded prems has_integral_empty_eq] by auto
  1.6130 +  qed
  1.6131 +  assume "cbox a b \<noteq> {}"
  1.6132 +  from assms(6)[rule_format,of a b] guess w z by (elim exE) note wz=this
  1.6133 +  have inj: "inj g" "inj h"
  1.6134 +    unfolding inj_on_def
  1.6135 +    apply safe
  1.6136 +    apply(rule_tac[!] ccontr)
  1.6137 +    using assms(2)
  1.6138 +    apply(erule_tac x=x in allE)
  1.6139 +    using assms(2)
  1.6140 +    apply(erule_tac x=y in allE)
  1.6141 +    defer
  1.6142 +    using assms(3)
  1.6143 +    apply (erule_tac x=x in allE)
  1.6144 +    using assms(3)
  1.6145 +    apply(erule_tac x=y in allE)
  1.6146 +    apply auto
  1.6147 +    done
  1.6148 +  show ?thesis
  1.6149 +    unfolding has_integral_def has_integral_compact_interval_def
  1.6150 +    apply (subst if_P)
  1.6151 +    apply rule
  1.6152 +    apply rule
  1.6153 +    apply (rule wz)
  1.6154 +  proof safe
  1.6155 +    fix e :: real
  1.6156 +    assume e: "e > 0"
  1.6157 +    with assms(1) have "e * r > 0" by simp
  1.6158 +    from assms(8)[unfolded has_integral,rule_format,OF this] guess d by (elim exE conjE) note d=this[rule_format]
  1.6159 +    define d' where "d' x = {y. g y \<in> d (g x)}" for x
  1.6160 +    have d': "\<And>x. d' x = {y. g y \<in> (d (g x))}"
  1.6161 +      unfolding d'_def ..
  1.6162 +    show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of h ` cbox a b \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i) < e)"
  1.6163 +    proof (rule_tac x=d' in exI, safe)
  1.6164 +      show "gauge d'"
  1.6165 +        using d(1)
  1.6166 +        unfolding gauge_def d'
  1.6167 +        using continuous_open_preimage_univ[OF assms(4)]
  1.6168 +        by auto
  1.6169 +      fix p
  1.6170 +      assume as: "p tagged_division_of h ` cbox a b" "d' fine p"
  1.6171 +      note p = tagged_division_ofD[OF as(1)]
  1.6172 +      have "(\<lambda>(x, k). (g x, g ` k)) ` p tagged_division_of (cbox a b) \<and> d fine (\<lambda>(x, k). (g x, g ` k)) ` p"
  1.6173 +        unfolding tagged_division_of
  1.6174 +      proof safe
  1.6175 +        show "finite ((\<lambda>(x, k). (g x, g ` k)) ` p)"
  1.6176 +          using as by auto
  1.6177 +        show "d fine (\<lambda>(x, k). (g x, g ` k)) ` p"
  1.6178 +          using as(2) unfolding fine_def d' by auto
  1.6179 +        fix x k
  1.6180 +        assume xk[intro]: "(x, k) \<in> p"
  1.6181 +        show "g x \<in> g ` k"
  1.6182 +          using p(2)[OF xk] by auto
  1.6183 +        show "\<exists>u v. g ` k = cbox u v"
  1.6184 +          using p(4)[OF xk] using assms(5-6) by auto
  1.6185 +        {
  1.6186 +          fix y
  1.6187 +          assume "y \<in> k"
  1.6188 +          then show "g y \<in> cbox a b" "g y \<in> cbox a b"
  1.6189 +            using p(3)[OF xk,unfolded subset_eq,rule_format,of "h (g y)"]
  1.6190 +            using assms(2)[rule_format,of y]
  1.6191 +            unfolding inj_image_mem_iff[OF inj(2)]
  1.6192 +            by auto
  1.6193 +        }
  1.6194 +        fix x' k'
  1.6195 +        assume xk': "(x', k') \<in> p"
  1.6196 +        fix z
  1.6197 +        assume z: "z \<in> interior (g ` k)" "z \<in> interior (g ` k')"
  1.6198 +        have same: "(x, k) = (x', k')"
  1.6199 +          apply -
  1.6200 +          apply (rule ccontr)
  1.6201 +          apply (drule p(5)[OF xk xk'])
  1.6202 +        proof -
  1.6203 +          assume as: "interior k \<inter> interior k' = {}"
  1.6204 +          have "z \<in> g ` (interior k \<inter> interior k')"
  1.6205 +            using interior_image_subset[OF assms(4) inj(1)] z
  1.6206 +            unfolding image_Int[OF inj(1)] by blast
  1.6207 +          then show False
  1.6208 +            using as by blast
  1.6209 +        qed
  1.6210 +        then show "g x = g x'"
  1.6211 +          by auto
  1.6212 +        {
  1.6213 +          fix z
  1.6214 +          assume "z \<in> k"
  1.6215 +          then show "g z \<in> g ` k'"
  1.6216 +            using same by auto
  1.6217 +        }
  1.6218 +        {
  1.6219 +          fix z
  1.6220 +          assume "z \<in> k'"
  1.6221 +          then show "g z \<in> g ` k"
  1.6222 +            using same by auto
  1.6223 +        }
  1.6224 +      next
  1.6225 +        fix x
  1.6226 +        assume "x \<in> cbox a b"
  1.6227 +        then have "h x \<in>  \<Union>{k. \<exists>x. (x, k) \<in> p}"
  1.6228 +          using p(6) by auto
  1.6229 +        then guess X unfolding Union_iff .. note X=this
  1.6230 +        from this(1) guess y unfolding mem_Collect_eq ..
  1.6231 +        then show "x \<in> \<Union>{k. \<exists>x. (x, k) \<in> (\<lambda>(x, k). (g x, g ` k)) ` p}"
  1.6232 +          apply -
  1.6233 +          apply (rule_tac X="g ` X" in UnionI)
  1.6234 +          defer
  1.6235 +          apply (rule_tac x="h x" in image_eqI)
  1.6236 +          using X(2) assms(3)[rule_format,of x]
  1.6237 +          apply auto
  1.6238 +          done
  1.6239 +      qed
  1.6240 +        note ** = d(2)[OF this]
  1.6241 +        have *: "inj_on (\<lambda>(x, k). (g x, g ` k)) p"
  1.6242 +          using inj(1) unfolding inj_on_def by fastforce
  1.6243 +        have "(\<Sum>(x, k)\<in>(\<lambda>(x, k). (g x, g ` k)) ` p. content k *\<^sub>R f x) - i = r *\<^sub>R (\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - i" (is "?l = _")
  1.6244 +          using assms(7)
  1.6245 +          apply (simp only: algebra_simps add_left_cancel scaleR_right.setsum)
  1.6246 +          apply (subst setsum.reindex_bij_betw[symmetric, where h="\<lambda>(x, k). (g x, g ` k)" and S=p])
  1.6247 +          apply (auto intro!: * setsum.cong simp: bij_betw_def dest!: p(4))
  1.6248 +          done
  1.6249 +      also have "\<dots> = r *\<^sub>R ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i)" (is "_ = ?r")
  1.6250 +        unfolding scaleR_diff_right scaleR_scaleR
  1.6251 +        using assms(1)
  1.6252 +        by auto
  1.6253 +      finally have *: "?l = ?r" .
  1.6254 +      show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i) < e"
  1.6255 +        using **
  1.6256 +        unfolding *
  1.6257 +        unfolding norm_scaleR
  1.6258 +        using assms(1)
  1.6259 +        by (auto simp add:field_simps)
  1.6260 +    qed
  1.6261 +  qed
  1.6262 +qed
  1.6263 +
  1.6264 +
  1.6265 +subsection \<open>Special case of a basic affine transformation.\<close>
  1.6266 +
  1.6267 +lemma interval_image_affinity_interval:
  1.6268 +  "\<exists>u v. (\<lambda>x. m *\<^sub>R (x::'a::euclidean_space) + c) ` cbox a b = cbox u v"
  1.6269 +  unfolding image_affinity_cbox
  1.6270 +  by auto
  1.6271 +
  1.6272 +lemma content_image_affinity_cbox:
  1.6273 +  "content((\<lambda>x::'a::euclidean_space. m *\<^sub>R x + c) ` cbox a b) =
  1.6274 +    \<bar>m\<bar> ^ DIM('a) * content (cbox a b)" (is "?l = ?r")
  1.6275 +proof (cases "cbox a b = {}")
  1.6276 +  case True then show ?thesis by simp
  1.6277 +next
  1.6278 +  case False
  1.6279 +  show ?thesis
  1.6280 +  proof (cases "m \<ge> 0")
  1.6281 +    case True
  1.6282 +    with \<open>cbox a b \<noteq> {}\<close> have "cbox (m *\<^sub>R a + c) (m *\<^sub>R b + c) \<noteq> {}"
  1.6283 +      unfolding box_ne_empty
  1.6284 +      apply (intro ballI)
  1.6285 +      apply (erule_tac x=i in ballE)
  1.6286 +      apply (auto simp: inner_simps mult_left_mono)
  1.6287 +      done
  1.6288 +    moreover from True have *: "\<And>i. (m *\<^sub>R b + c) \<bullet> i - (m *\<^sub>R a + c) \<bullet> i = m *\<^sub>R (b - a) \<bullet> i"
  1.6289 +      by (simp add: inner_simps field_simps)
  1.6290 +    ultimately show ?thesis
  1.6291 +      by (simp add: image_affinity_cbox True content_cbox'
  1.6292 +        setprod.distrib setprod_constant inner_diff_left)
  1.6293 +  next
  1.6294 +    case False
  1.6295 +    with \<open>cbox a b \<noteq> {}\<close> have "cbox (m *\<^sub>R b + c) (m *\<^sub>R a + c) \<noteq> {}"
  1.6296 +      unfolding box_ne_empty
  1.6297 +      apply (intro ballI)
  1.6298 +      apply (erule_tac x=i in ballE)
  1.6299 +      apply (auto simp: inner_simps mult_left_mono)
  1.6300 +      done
  1.6301 +    moreover from False have *: "\<And>i. (m *\<^sub>R a + c) \<bullet> i - (m *\<^sub>R b + c) \<bullet> i = (-m) *\<^sub>R (b - a) \<bullet> i"
  1.6302 +      by (simp add: inner_simps field_simps)
  1.6303 +    ultimately show ?thesis using False
  1.6304 +      by (simp add: image_affinity_cbox content_cbox'
  1.6305 +        setprod.distrib[symmetric] setprod_constant[symmetric] inner_diff_left)
  1.6306 +  qed
  1.6307 +qed
  1.6308 +
  1.6309 +lemma has_integral_affinity:
  1.6310 +  fixes a :: "'a::euclidean_space"
  1.6311 +  assumes "(f has_integral i) (cbox a b)"
  1.6312 +      and "m \<noteq> 0"
  1.6313 +  shows "((\<lambda>x. f(m *\<^sub>R x + c)) has_integral ((1 / (\<bar>m\<bar> ^ DIM('a))) *\<^sub>R i)) ((\<lambda>x. (1 / m) *\<^sub>R x + -((1 / m) *\<^sub>R c)) ` cbox a b)"
  1.6314 +  apply (rule has_integral_twiddle)
  1.6315 +  using assms
  1.6316 +  apply (safe intro!: interval_image_affinity_interval content_image_affinity_cbox)
  1.6317 +  apply (rule zero_less_power)
  1.6318 +  unfolding scaleR_right_distrib
  1.6319 +  apply auto
  1.6320 +  done
  1.6321 +
  1.6322 +lemma integrable_affinity:
  1.6323 +  assumes "f integrable_on cbox a b"
  1.6324 +    and "m \<noteq> 0"
  1.6325 +  shows "(\<lambda>x. f(m *\<^sub>R x + c)) integrable_on ((\<lambda>x. (1 / m) *\<^sub>R x + -((1/m) *\<^sub>R c)) ` cbox a b)"
  1.6326 +  using assms
  1.6327 +  unfolding integrable_on_def
  1.6328 +  apply safe
  1.6329 +  apply (drule has_integral_affinity)
  1.6330 +  apply auto
  1.6331 +  done
  1.6332 +
  1.6333 +lemmas has_integral_affinity01 = has_integral_affinity [of _ _ 0 "1::real", simplified]
  1.6334 +
  1.6335 +subsection \<open>Special case of stretching coordinate axes separately.\<close>
  1.6336 +
  1.6337 +lemma content_image_stretch_interval:
  1.6338 +  "content ((\<lambda>x::'a::euclidean_space. (\<Sum>k\<in>Basis. (m k * (x\<bullet>k))*\<^sub>R k)::'a) ` cbox a b) =
  1.6339 +    \<bar>setprod m Basis\<bar> * content (cbox a b)"
  1.6340 +proof (cases "cbox a b = {}")
  1.6341 +  case True
  1.6342 +  then show ?thesis
  1.6343 +    unfolding content_def image_is_empty image_stretch_interval if_P[OF True] by auto
  1.6344 +next
  1.6345 +  case False
  1.6346 +  then have "(\<lambda>x. (\<Sum>k\<in>Basis. (m k * (x\<bullet>k))*\<^sub>R k)) ` cbox a b \<noteq> {}"
  1.6347 +    by auto
  1.6348 +  then show ?thesis
  1.6349 +    using False
  1.6350 +    unfolding content_def image_stretch_interval
  1.6351 +    apply -
  1.6352 +    unfolding interval_bounds' if_not_P
  1.6353 +    unfolding abs_setprod setprod.distrib[symmetric]
  1.6354 +    apply (rule setprod.cong)
  1.6355 +    apply (rule refl)
  1.6356 +    unfolding lessThan_iff
  1.6357 +    apply (simp only: inner_setsum_left_Basis)
  1.6358 +  proof -
  1.6359 +    fix i :: 'a
  1.6360 +    assume i: "i \<in> Basis"
  1.6361 +    have "(m i < 0 \<or> m i > 0) \<or> m i = 0"
  1.6362 +      by auto
  1.6363 +    then show "max (m i * (a \<bullet> i)) (m i * (b \<bullet> i)) - min (m i * (a \<bullet> i)) (m i * (b \<bullet> i)) =
  1.6364 +      \<bar>m i\<bar> * (b \<bullet> i - a \<bullet> i)"
  1.6365 +      apply -
  1.6366 +      apply (erule disjE)+
  1.6367 +      unfolding min_def max_def
  1.6368 +      using False[unfolded box_ne_empty,rule_format,of i] i
  1.6369 +      apply (auto simp add:field_simps not_le mult_le_cancel_left_neg mult_le_cancel_left_pos)
  1.6370 +      done
  1.6371 +  qed
  1.6372 +qed
  1.6373 +
  1.6374 +lemma has_integral_stretch:
  1.6375 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1.6376 +  assumes "(f has_integral i) (cbox a b)"
  1.6377 +    and "\<forall>k\<in>Basis. m k \<noteq> 0"
  1.6378 +  shows "((\<lambda>x. f (\<Sum>k\<in>Basis. (m k * (x\<bullet>k))*\<^sub>R k)) has_integral
  1.6379 +    ((1/ \<bar>setprod m Basis\<bar>) *\<^sub>R i)) ((\<lambda>x. (\<Sum>k\<in>Basis. (1 / m k * (x\<bullet>k))*\<^sub>R k)) ` cbox a b)"
  1.6380 +  apply (rule has_integral_twiddle[where f=f])
  1.6381 +  unfolding zero_less_abs_iff content_image_stretch_interval
  1.6382 +  unfolding image_stretch_interval empty_as_interval euclidean_eq_iff[where 'a='a]
  1.6383 +  using assms
  1.6384 +proof -
  1.6385 +  show "\<forall>y::'a. continuous (at y) (\<lambda>x. (\<Sum>k\<in>Basis. (m k * (x\<bullet>k))*\<^sub>R k))"
  1.6386 +    apply rule
  1.6387 +    apply (rule linear_continuous_at)
  1.6388 +    unfolding linear_linear
  1.6389 +    unfolding linear_iff inner_simps euclidean_eq_iff[where 'a='a]
  1.6390 +    apply (auto simp add: field_simps)
  1.6391 +    done
  1.6392 +qed auto
  1.6393 +
  1.6394 +lemma integrable_stretch:
  1.6395 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1.6396 +  assumes "f integrable_on cbox a b"
  1.6397 +    and "\<forall>k\<in>Basis. m k \<noteq> 0"
  1.6398 +  shows "(\<lambda>x::'a. f (\<Sum>k\<in>Basis. (m k * (x\<bullet>k))*\<^sub>R k)) integrable_on
  1.6399 +    ((\<lambda>x. \<Sum>k\<in>Basis. (1 / m k * (x\<bullet>k))*\<^sub>R k) ` cbox a b)"
  1.6400 +  using assms
  1.6401 +  unfolding integrable_on_def
  1.6402 +  apply -
  1.6403 +  apply (erule exE)
  1.6404 +  apply (drule has_integral_stretch)
  1.6405 +  apply assumption
  1.6406 +  apply auto
  1.6407 +  done
  1.6408 +
  1.6409 +subsection \<open>even more special cases.\<close>
  1.6410 +
  1.6411 +lemma uminus_interval_vector[simp]:
  1.6412 +  fixes a b :: "'a::euclidean_space"
  1.6413 +  shows "uminus ` cbox a b = cbox (-b) (-a)"
  1.6414 +  apply (rule set_eqI)
  1.6415 +  apply rule
  1.6416 +  defer
  1.6417 +  unfolding image_iff
  1.6418 +  apply (rule_tac x="-x" in bexI)
  1.6419 +  apply (auto simp add:minus_le_iff le_minus_iff mem_box)
  1.6420 +  done
  1.6421 +
  1.6422 +lemma has_integral_reflect_lemma[intro]:
  1.6423 +  assumes "(f has_integral i) (cbox a b)"
  1.6424 +  shows "((\<lambda>x. f(-x)) has_integral i) (cbox (-b) (-a))"
  1.6425 +  using has_integral_affinity[OF assms, of "-1" 0]
  1.6426 +  by auto
  1.6427 +
  1.6428 +lemma has_integral_reflect_lemma_real[intro]:
  1.6429 +  assumes "(f has_integral i) {a .. b::real}"
  1.6430 +  shows "((\<lambda>x. f(-x)) has_integral i) {-b .. -a}"
  1.6431 +  using assms
  1.6432 +  unfolding box_real[symmetric]
  1.6433 +  by (rule has_integral_reflect_lemma)
  1.6434 +
  1.6435 +lemma has_integral_reflect[simp]:
  1.6436 +  "((\<lambda>x. f (-x)) has_integral i) (cbox (-b) (-a)) \<longleftrightarrow> (f has_integral i) (cbox a b)"
  1.6437 +  apply rule
  1.6438 +  apply (drule_tac[!] has_integral_reflect_lemma)
  1.6439 +  apply auto
  1.6440 +  done
  1.6441 +
  1.6442 +lemma integrable_reflect[simp]: "(\<lambda>x. f(-x)) integrable_on cbox (-b) (-a) \<longleftrightarrow> f integrable_on cbox a b"
  1.6443 +  unfolding integrable_on_def by auto
  1.6444 +
  1.6445 +lemma integrable_reflect_real[simp]: "(\<lambda>x. f(-x)) integrable_on {-b .. -a} \<longleftrightarrow> f integrable_on {a .. b::real}"
  1.6446 +  unfolding box_real[symmetric]
  1.6447 +  by (rule integrable_reflect)
  1.6448 +
  1.6449 +lemma integral_reflect[simp]: "integral (cbox (-b) (-a)) (\<lambda>x. f (-x)) = integral (cbox a b) f"
  1.6450 +  unfolding integral_def by auto
  1.6451 +
  1.6452 +lemma integral_reflect_real[simp]: "integral {-b .. -a} (\<lambda>x. f (-x)) = integral {a .. b::real} f"
  1.6453 +  unfolding box_real[symmetric]
  1.6454 +  by (rule integral_reflect)
  1.6455 +
  1.6456 +
  1.6457 +subsection \<open>Stronger form of FCT; quite a tedious proof.\<close>
  1.6458 +
  1.6459 +lemma bgauge_existence_lemma: "(\<forall>x\<in>s. \<exists>d::real. 0 < d \<and> q d x) \<longleftrightarrow> (\<forall>x. \<exists>d>0. x\<in>s \<longrightarrow> q d x)"
  1.6460 +  by (meson zero_less_one)
  1.6461 +
  1.6462 +lemma additive_tagged_division_1':
  1.6463 +  fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  1.6464 +  assumes "a \<le> b"
  1.6465 +    and "p tagged_division_of {a..b}"
  1.6466 +  shows "setsum (\<lambda>(x,k). f (Sup k) - f(Inf k)) p = f b - f a"
  1.6467 +  using additive_tagged_division_1[OF _ assms(2), of f]
  1.6468 +  using assms(1)
  1.6469 +  by auto
  1.6470 +
  1.6471 +lemma split_minus[simp]: "(\<lambda>(x, k). f x k) x - (\<lambda>(x, k). g x k) x = (\<lambda>(x, k). f x k - g x k) x"
  1.6472 +  by (simp add: split_def)
  1.6473 +
  1.6474 +lemma norm_triangle_le_sub: "norm x + norm y \<le> e \<Longrightarrow> norm (x - y) \<le> e"
  1.6475 +  apply (subst(asm)(2) norm_minus_cancel[symmetric])
  1.6476 +  apply (drule norm_triangle_le)
  1.6477 +  apply (auto simp add: algebra_simps)
  1.6478 +  done
  1.6479 +
  1.6480 +lemma fundamental_theorem_of_calculus_interior:
  1.6481 +  fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  1.6482 +  assumes "a \<le> b"
  1.6483 +    and "continuous_on {a .. b} f"
  1.6484 +    and "\<forall>x\<in>{a <..< b}. (f has_vector_derivative f'(x)) (at x)"
  1.6485 +  shows "(f' has_integral (f b - f a)) {a .. b}"
  1.6486 +proof -
  1.6487 +  {
  1.6488 +    presume *: "a < b \<Longrightarrow> ?thesis"
  1.6489 +    show ?thesis
  1.6490 +    proof (cases "a < b")
  1.6491 +      case True
  1.6492 +      then show ?thesis by (rule *)
  1.6493 +    next
  1.6494 +      case False
  1.6495 +      then have "a = b"
  1.6496 +        using assms(1) by auto
  1.6497 +      then have *: "cbox a b = {b}" "f b - f a = 0"
  1.6498 +        by (auto simp add:  order_antisym)
  1.6499 +      show ?thesis
  1.6500 +        unfolding *(2)
  1.6501 +        unfolding content_eq_0
  1.6502 +        using * \<open>a = b\<close>
  1.6503 +        by (auto simp: ex_in_conv)
  1.6504 +    qed
  1.6505 +  }
  1.6506 +  assume ab: "a < b"
  1.6507 +  let ?P = "\<lambda>e. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a .. b} \<and> d fine p \<longrightarrow>
  1.6508 +    norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content {a .. b})"
  1.6509 +  { presume "\<And>e. e > 0 \<Longrightarrow> ?P e" then show ?thesis unfolding has_integral_factor_content_real by auto }
  1.6510 +  fix e :: real
  1.6511 +  assume e: "e > 0"
  1.6512 +  note assms(3)[unfolded has_vector_derivative_def has_derivative_at_alt ball_conj_distrib]
  1.6513 +  note conjunctD2[OF this]
  1.6514 +  note bounded=this(1) and this(2)
  1.6515 +  from this(2) have "\<forall>x\<in>box a b. \<exists>d>0. \<forall>y. norm (y - x) < d \<longrightarrow>
  1.6516 +    norm (f y - f x - (y - x) *\<^sub>R f' x) \<le> e/2 * norm (y - x)"
  1.6517 +    apply -
  1.6518 +    apply safe
  1.6519 +    apply (erule_tac x=x in ballE)
  1.6520 +    apply (erule_tac x="e/2" in allE)
  1.6521 +    using e
  1.6522 +    apply auto
  1.6523 +    done
  1.6524 +  note this[unfolded bgauge_existence_lemma]
  1.6525 +  from choice[OF this] guess d ..
  1.6526 +  note conjunctD2[OF this[rule_format]]
  1.6527 +  note d = this[rule_format]
  1.6528 +  have "bounded (f ` cbox a b)"
  1.6529 +    apply (rule compact_imp_bounded compact_continuous_image)+
  1.6530 +    using compact_cbox assms
  1.6531 +    apply auto
  1.6532 +    done
  1.6533 +  from this[unfolded bounded_pos] guess B .. note B = this[rule_format]
  1.6534 +
  1.6535 +  have "\<exists>da. 0 < da \<and> (\<forall>c. a \<le> c \<and> {a .. c} \<subseteq> {a .. b} \<and> {a .. c} \<subseteq> ball a da \<longrightarrow>
  1.6536 +    norm (content {a .. c} *\<^sub>R f' a - (f c - f a)) \<le> (e * (b - a)) / 4)"
  1.6537 +  proof -
  1.6538 +    have "a \<in> {a .. b}"
  1.6539 +      using ab by auto
  1.6540 +    note assms(2)[unfolded continuous_on_eq_continuous_within,rule_format,OF this]
  1.6541 +    note * = this[unfolded continuous_within Lim_within,rule_format]
  1.6542 +    have "(e * (b - a)) / 8 > 0"
  1.6543 +      using e ab by (auto simp add: field_simps)
  1.6544 +    from *[OF this] guess k .. note k = conjunctD2[OF this,rule_format]
  1.6545 +    have "\<exists>l. 0 < l \<and> norm(l *\<^sub>R f' a) \<le> (e * (b - a)) / 8"
  1.6546 +    proof (cases "f' a = 0")
  1.6547 +      case True
  1.6548 +      thus ?thesis using ab e by auto
  1.6549 +    next
  1.6550 +      case False
  1.6551 +      then show ?thesis
  1.6552 +        apply (rule_tac x="(e * (b - a)) / 8 / norm (f' a)" in exI)
  1.6553 +        using ab e
  1.6554 +        apply (auto simp add: field_simps)
  1.6555 +        done
  1.6556 +    qed
  1.6557 +    then guess l .. note l = conjunctD2[OF this]
  1.6558 +    show ?thesis
  1.6559 +      apply (rule_tac x="min k l" in exI)
  1.6560 +      apply safe
  1.6561 +      unfolding min_less_iff_conj
  1.6562 +      apply rule
  1.6563 +      apply (rule l k)+
  1.6564 +    proof -
  1.6565 +      fix c
  1.6566 +      assume as: "a \<le> c" "{a .. c} \<subseteq> {a .. b}" "{a .. c} \<subseteq> ball a (min k l)"
  1.6567 +      note as' = this[unfolded subset_eq Ball_def mem_ball dist_real_def mem_box]
  1.6568 +      have "norm ((c - a) *\<^sub>R f' a - (f c - f a)) \<le> norm ((c - a) *\<^sub>R f' a) + norm (f c - f a)"
  1.6569 +        by (rule norm_triangle_ineq4)
  1.6570 +      also have "\<dots> \<le> e * (b - a) / 8 + e * (b - a) / 8"
  1.6571 +      proof (rule add_mono)
  1.6572 +        have "\<bar>c - a\<bar> \<le> \<bar>l\<bar>"
  1.6573 +          using as' by auto
  1.6574 +        then show "norm ((c - a) *\<^sub>R f' a) \<le> e * (b - a) / 8"
  1.6575 +          apply -
  1.6576 +          apply (rule order_trans[OF _ l(2)])
  1.6577 +          unfolding norm_scaleR
  1.6578 +          apply (rule mult_right_mono)
  1.6579 +          apply auto
  1.6580 +          done
  1.6581 +      next
  1.6582 +        show "norm (f c - f a) \<le> e * (b - a) / 8"
  1.6583 +          apply (rule less_imp_le)
  1.6584 +          apply (cases "a = c")
  1.6585 +          defer
  1.6586 +          apply (rule k(2)[unfolded dist_norm])
  1.6587 +          using as' e ab
  1.6588 +          apply (auto simp add: field_simps)
  1.6589 +          done
  1.6590 +      qed
  1.6591 +      finally show "norm (content {a .. c} *\<^sub>R f' a - (f c - f a)) \<le> e * (b - a) / 4"
  1.6592 +        unfolding content_real[OF as(1)] by auto
  1.6593 +    qed
  1.6594 +  qed
  1.6595 +  then guess da .. note da=conjunctD2[OF this,rule_format]
  1.6596 +
  1.6597 +  have "\<exists>db>0. \<forall>c\<le>b. {c .. b} \<subseteq> {a .. b} \<and> {c .. b} \<subseteq> ball b db \<longrightarrow>
  1.6598 +    norm (content {c .. b} *\<^sub>R f' b - (f b - f c)) \<le> (e * (b - a)) / 4"
  1.6599 +  proof -
  1.6600 +    have "b \<in> {a .. b}"
  1.6601 +      using ab by auto
  1.6602 +    note assms(2)[unfolded continuous_on_eq_continuous_within,rule_format,OF this]
  1.6603 +    note * = this[unfolded continuous_within Lim_within,rule_format] have "(e * (b - a)) / 8 > 0"
  1.6604 +      using e ab by (auto simp add: field_simps)
  1.6605 +    from *[OF this] guess k .. note k = conjunctD2[OF this,rule_format]
  1.6606 +    have "\<exists>l. 0 < l \<and> norm (l *\<^sub>R f' b) \<le> (e * (b - a)) / 8"
  1.6607 +    proof (cases "f' b = 0")
  1.6608 +      case True
  1.6609 +      thus ?thesis using ab e by auto
  1.6610 +    next
  1.6611 +      case False
  1.6612 +      then show ?thesis
  1.6613 +        apply (rule_tac x="(e * (b - a)) / 8 / norm (f' b)" in exI)
  1.6614 +        using ab e
  1.6615 +        apply (auto simp add: field_simps)
  1.6616 +        done
  1.6617 +    qed
  1.6618 +    then guess l .. note l = conjunctD2[OF this]
  1.6619 +    show ?thesis
  1.6620 +      apply (rule_tac x="min k l" in exI)
  1.6621 +      apply safe
  1.6622 +      unfolding min_less_iff_conj
  1.6623 +      apply rule
  1.6624 +      apply (rule l k)+
  1.6625 +    proof -
  1.6626 +      fix c
  1.6627 +      assume as: "c \<le> b" "{c..b} \<subseteq> {a..b}" "{c..b} \<subseteq> ball b (min k l)"
  1.6628 +      note as' = this[unfolded subset_eq Ball_def mem_ball dist_real_def mem_box]
  1.6629 +      have "norm ((b - c) *\<^sub>R f' b - (f b - f c)) \<le> norm ((b - c) *\<^sub>R f' b) + norm (f b - f c)"
  1.6630 +        by (rule norm_triangle_ineq4)
  1.6631 +      also have "\<dots> \<le> e * (b - a) / 8 + e * (b - a) / 8"
  1.6632 +      proof (rule add_mono)
  1.6633 +        have "\<bar>c - b\<bar> \<le> \<bar>l\<bar>"
  1.6634 +          using as' by auto
  1.6635 +        then show "norm ((b - c) *\<^sub>R f' b) \<le> e * (b - a) / 8"
  1.6636 +          apply -
  1.6637 +          apply (rule order_trans[OF _ l(2)])
  1.6638 +          unfolding norm_scaleR
  1.6639 +          apply (rule mult_right_mono)
  1.6640 +          apply auto
  1.6641 +          done
  1.6642 +      next
  1.6643 +        show "norm (f b - f c) \<le> e * (b - a) / 8"
  1.6644 +          apply (rule less_imp_le)
  1.6645 +          apply (cases "b = c")
  1.6646 +          defer
  1.6647 +          apply (subst norm_minus_commute)
  1.6648 +          apply (rule k(2)[unfolded dist_norm])
  1.6649 +          using as' e ab
  1.6650 +          apply (auto simp add: field_simps)
  1.6651 +          done
  1.6652 +      qed
  1.6653 +      finally show "norm (content {c .. b} *\<^sub>R f' b - (f b - f c)) \<le> e * (b - a) / 4"
  1.6654 +        unfolding content_real[OF as(1)] by auto
  1.6655 +    qed
  1.6656 +  qed
  1.6657 +  then guess db .. note db=conjunctD2[OF this,rule_format]
  1.6658 +
  1.6659 +  let ?d = "(\<lambda>x. ball x (if x=a then da else if x=b then db else d x))"
  1.6660 +  show "?P e"
  1.6661 +    apply (rule_tac x="?d" in exI)
  1.6662 +  proof (safe, goal_cases)
  1.6663 +    case 1
  1.6664 +    show ?case
  1.6665 +      apply (rule gauge_ball_dependent)
  1.6666 +      using ab db(1) da(1) d(1)
  1.6667 +      apply auto
  1.6668 +      done
  1.6669 +  next
  1.6670 +    case as: (2 p)
  1.6671 +    let ?A = "{t. fst t \<in> {a, b}}"
  1.6672 +    note p = tagged_division_ofD[OF as(1)]
  1.6673 +    have pA: "p = (p \<inter> ?A) \<union> (p - ?A)" "finite (p \<inter> ?A)" "finite (p - ?A)" "(p \<inter> ?A) \<inter> (p - ?A) = {}"
  1.6674 +      using as by auto
  1.6675 +    note * = additive_tagged_division_1'[OF assms(1) as(1), symmetric]
  1.6676 +    have **: "\<And>n1 s1 n2 s2::real. n2 \<le> s2 / 2 \<Longrightarrow> n1 - s1 \<le> s2 / 2 \<Longrightarrow> n1 + n2 \<le> s1 + s2"
  1.6677 +      by arith
  1.6678 +    show ?case
  1.6679 +      unfolding content_real[OF assms(1)] and *[of "\<lambda>x. x"] *[of f] setsum_subtractf[symmetric] split_minus
  1.6680 +      unfolding setsum_right_distrib
  1.6681 +      apply (subst(2) pA)
  1.6682 +      apply (subst pA)
  1.6683 +      unfolding setsum.union_disjoint[OF pA(2-)]
  1.6684 +    proof (rule norm_triangle_le, rule **, goal_cases)
  1.6685 +      case 1
  1.6686 +      show ?case
  1.6687 +        apply (rule order_trans)
  1.6688 +        apply (rule setsum_norm_le)
  1.6689 +        defer
  1.6690 +        apply (subst setsum_divide_distrib)
  1.6691 +        apply (rule order_refl)
  1.6692 +        apply safe
  1.6693 +        apply (unfold not_le o_def split_conv fst_conv)
  1.6694 +      proof (rule ccontr)
  1.6695 +        fix x k
  1.6696 +        assume xk: "(x, k) \<in> p"
  1.6697 +          "e * (Sup k -  Inf k) / 2 <
  1.6698 +            norm (content k *\<^sub>R f' x - (f (Sup k) - f (Inf k)))"
  1.6699 +        from p(4)[OF this(1)] guess u v by (elim exE) note k=this
  1.6700 +        then have "u \<le> v" and uv: "{u, v} \<subseteq> cbox u v"
  1.6701 +          using p(2)[OF xk(1)] by auto
  1.6702 +        note result = xk(2)[unfolded k box_real interval_bounds_real[OF this(1)] content_real[OF this(1)]]
  1.6703 +
  1.6704 +        assume as': "x \<noteq> a" "x \<noteq> b"
  1.6705 +        then have "x \<in> box a b"
  1.6706 +          using p(2-3)[OF xk(1)] by (auto simp: mem_box)
  1.6707 +        note  * = d(2)[OF this]
  1.6708 +        have "norm ((v - u) *\<^sub>R f' (x) - (f (v) - f (u))) =
  1.6709 +          norm ((f (u) - f (x) - (u - x) *\<^sub>R f' (x)) - (f (v) - f (x) - (v - x) *\<^sub>R f' (x)))"
  1.6710 +          apply (rule arg_cong[of _ _ norm])
  1.6711 +          unfolding scaleR_left.diff
  1.6712 +          apply auto
  1.6713 +          done
  1.6714 +        also have "\<dots> \<le> e / 2 * norm (u - x) + e / 2 * norm (v - x)"
  1.6715 +          apply (rule norm_triangle_le_sub)
  1.6716 +          apply (rule add_mono)
  1.6717 +          apply (rule_tac[!] *)
  1.6718 +          using fineD[OF as(2) xk(1)] as'
  1.6719 +          unfolding k subset_eq
  1.6720 +          apply -
  1.6721 +          apply (erule_tac x=u in ballE)
  1.6722 +          apply (erule_tac[3] x=v in ballE)
  1.6723 +          using uv
  1.6724 +          apply (auto simp:dist_real_def)
  1.6725 +          done
  1.6726 +        also have "\<dots> \<le> e / 2 * norm (v - u)"
  1.6727 +          using p(2)[OF xk(1)]
  1.6728 +          unfolding k
  1.6729 +          by (auto simp add: field_simps)
  1.6730 +        finally have "e * (v - u) / 2 < e * (v - u) / 2"
  1.6731 +          apply -
  1.6732 +          apply (rule less_le_trans[OF result])
  1.6733 +          using uv
  1.6734 +          apply auto
  1.6735 +          done
  1.6736 +        then show False by auto
  1.6737 +      qed
  1.6738 +    next
  1.6739 +      have *: "\<And>x s1 s2::real. 0 \<le> s1 \<Longrightarrow> x \<le> (s1 + s2) / 2 \<Longrightarrow> x - s1 \<le> s2 / 2"
  1.6740 +        by auto
  1.6741 +      case 2
  1.6742 +      show ?case
  1.6743 +        apply (rule *)
  1.6744 +        apply (rule setsum_nonneg)
  1.6745 +        apply rule
  1.6746 +        apply (unfold split_paired_all split_conv)
  1.6747 +        defer
  1.6748 +        unfolding setsum.union_disjoint[OF pA(2-),symmetric] pA(1)[symmetric]
  1.6749 +        unfolding setsum_right_distrib[symmetric]
  1.6750 +        apply (subst additive_tagged_division_1[OF _ as(1)])
  1.6751 +        apply (rule assms)
  1.6752 +      proof -
  1.6753 +        fix x k
  1.6754 +        assume "(x, k) \<in> p \<inter> {t. fst t \<in> {a, b}}"
  1.6755 +        note xk=IntD1[OF this]
  1.6756 +        from p(4)[OF this] guess u v by (elim exE) note uv=this
  1.6757 +        with p(2)[OF xk] have "cbox u v \<noteq> {}"
  1.6758 +          by auto
  1.6759 +        then show "0 \<le> e * ((Sup k) - (Inf k))"
  1.6760 +          unfolding uv using e by (auto simp add: field_simps)
  1.6761 +      next
  1.6762 +        have *: "\<And>s f t e. setsum f s = setsum f t \<Longrightarrow> norm (setsum f t) \<le> e \<Longrightarrow> norm (setsum f s) \<le> e"
  1.6763 +          by auto
  1.6764 +        show "norm (\<Sum>(x, k)\<in>p \<inter> ?A. content k *\<^sub>R f' x -
  1.6765 +          (f ((Sup k)) - f ((Inf k)))) \<le> e * (b - a) / 2"
  1.6766 +          apply (rule *[where t1="p \<inter> {t. fst t \<in> {a, b} \<and> content(snd t) \<noteq> 0}"])
  1.6767 +          apply (rule setsum.mono_neutral_right[OF pA(2)])
  1.6768 +          defer
  1.6769 +          apply rule
  1.6770 +          unfolding split_paired_all split_conv o_def
  1.6771 +        proof goal_cases
  1.6772 +          fix x k
  1.6773 +          assume "(x, k) \<in> p \<inter> {t. fst t \<in> {a, b}} - p \<inter> {t. fst t \<in> {a, b} \<and> content (snd t) \<noteq> 0}"
  1.6774 +          then have xk: "(x, k) \<in> p" "content k = 0"
  1.6775 +            by auto
  1.6776 +          from p(4)[OF xk(1)] guess u v by (elim exE) note uv=this
  1.6777 +          have "k \<noteq> {}"
  1.6778 +            using p(2)[OF xk(1)] by auto
  1.6779 +          then have *: "u = v"
  1.6780 +            using xk
  1.6781 +            unfolding uv content_eq_0 box_eq_empty
  1.6782 +            by auto
  1.6783 +          then show "content k *\<^sub>R (f' (x)) - (f ((Sup k)) - f ((Inf k))) = 0"
  1.6784 +            using xk unfolding uv by auto
  1.6785 +        next
  1.6786 +          have *: "p \<inter> {t. fst t \<in> {a, b} \<and> content(snd t) \<noteq> 0} =
  1.6787 +            {t. t\<in>p \<and> fst t = a \<and> content(snd t) \<noteq> 0} \<union> {t. t\<in>p \<and> fst t = b \<and> content(snd t) \<noteq> 0}"
  1.6788 +            by blast
  1.6789 +          have **: "norm (setsum f s) \<le> e"
  1.6790 +            if "\<forall>x y. x \<in> s \<and> y \<in> s \<longrightarrow> x = y"
  1.6791 +            and "\<forall>x. x \<in> s \<longrightarrow> norm (f x) \<le> e"
  1.6792 +            and "e > 0"
  1.6793 +            for s f and e :: real
  1.6794 +          proof (cases "s = {}")
  1.6795 +            case True
  1.6796 +            with that show ?thesis by auto
  1.6797 +          next
  1.6798 +            case False
  1.6799 +            then obtain x where "x \<in> s"
  1.6800 +              by auto
  1.6801 +            then have *: "s = {x}"
  1.6802 +              using that(1) by auto
  1.6803 +            then show ?thesis
  1.6804 +              using \<open>x \<in> s\<close> that(2) by auto
  1.6805 +          qed
  1.6806 +          case 2
  1.6807 +          show ?case
  1.6808 +            apply (subst *)
  1.6809 +            apply (subst setsum.union_disjoint)
  1.6810 +            prefer 4
  1.6811 +            apply (rule order_trans[of _ "e * (b - a)/4 + e * (b - a)/4"])
  1.6812 +            apply (rule norm_triangle_le,rule add_mono)
  1.6813 +            apply (rule_tac[1-2] **)
  1.6814 +          proof -
  1.6815 +            let ?B = "\<lambda>x. {t \<in> p. fst t = x \<and> content (snd t) \<noteq> 0}"
  1.6816 +            have pa: "\<exists>v. k = cbox a v \<and> a \<le> v" if "(a, k) \<in> p" for k
  1.6817 +            proof -
  1.6818 +              guess u v using p(4)[OF that] by (elim exE) note uv=this
  1.6819 +              have *: "u \<le> v"
  1.6820 +                using p(2)[OF that] unfolding uv by auto
  1.6821 +              have u: "u = a"
  1.6822 +              proof (rule ccontr)
  1.6823 +                have "u \<in> cbox u v"
  1.6824 +                  using p(2-3)[OF that(1)] unfolding uv by auto
  1.6825 +                have "u \<ge> a"
  1.6826 +                  using p(2-3)[OF that(1)] unfolding uv subset_eq by auto
  1.6827 +                moreover assume "\<not> ?thesis"
  1.6828 +                ultimately have "u > a" by auto
  1.6829 +                then show False
  1.6830 +                  using p(2)[OF that(1)] unfolding uv by (auto simp add:)
  1.6831 +              qed
  1.6832 +              then show ?thesis
  1.6833 +                apply (rule_tac x=v in exI)
  1.6834 +                unfolding uv
  1.6835 +                using *
  1.6836 +                apply auto
  1.6837 +                done
  1.6838 +            qed
  1.6839 +            have pb: "\<exists>v. k = cbox v b \<and> b \<ge> v" if "(b, k) \<in> p" for k
  1.6840 +            proof -
  1.6841 +              guess u v using p(4)[OF that] by (elim exE) note uv=this
  1.6842 +              have *: "u \<le> v"
  1.6843 +                using p(2)[OF that] unfolding uv by auto
  1.6844 +              have u: "v = b"
  1.6845 +              proof (rule ccontr)
  1.6846 +                have "u \<in> cbox u v"
  1.6847 +                  using p(2-3)[OF that(1)] unfolding uv by auto
  1.6848 +                have "v \<le> b"
  1.6849 +                  using p(2-3)[OF that(1)] unfolding uv subset_eq by auto
  1.6850 +                moreover assume "\<not> ?thesis"
  1.6851 +                ultimately have "v < b" by auto
  1.6852 +                then show False
  1.6853 +                  using p(2)[OF that(1)] unfolding uv by (auto simp add:)
  1.6854 +              qed
  1.6855 +              then show ?thesis
  1.6856 +                apply (rule_tac x=u in exI)
  1.6857 +                unfolding uv
  1.6858 +                using *
  1.6859 +                apply auto
  1.6860 +                done
  1.6861 +            qed
  1.6862 +            show "\<forall>x y. x \<in> ?B a \<and> y \<in> ?B a \<longrightarrow> x = y"
  1.6863 +              apply (rule,rule,rule,unfold split_paired_all)
  1.6864 +              unfolding mem_Collect_eq fst_conv snd_conv
  1.6865 +              apply safe
  1.6866 +            proof -
  1.6867 +              fix x k k'
  1.6868 +              assume k: "(a, k) \<in> p" "(a, k') \<in> p" "content k \<noteq> 0" "content k' \<noteq> 0"
  1.6869 +              guess v using pa[OF k(1)] .. note v = conjunctD2[OF this]
  1.6870 +              guess v' using pa[OF k(2)] .. note v' = conjunctD2[OF this] let ?v = "min v v'"
  1.6871 +              have "box a ?v \<subseteq> k \<inter> k'"
  1.6872 +                unfolding v v' by (auto simp add: mem_box)
  1.6873 +              note interior_mono[OF this,unfolded interior_Int]
  1.6874 +              moreover have "(a + ?v)/2 \<in> box a ?v"
  1.6875 +                using k(3-)
  1.6876 +                unfolding v v' content_eq_0 not_le
  1.6877 +                by (auto simp add: mem_box)
  1.6878 +              ultimately have "(a + ?v)/2 \<in> interior k \<inter> interior k'"
  1.6879 +                unfolding interior_open[OF open_box] by auto
  1.6880 +              then have *: "k = k'"
  1.6881 +                apply -
  1.6882 +                apply (rule ccontr)
  1.6883 +                using p(5)[OF k(1-2)]
  1.6884 +                apply auto
  1.6885 +                done
  1.6886 +              { assume "x \<in> k" then show "x \<in> k'" unfolding * . }
  1.6887 +              { assume "x \<in> k'" then show "x \<in> k" unfolding * . }
  1.6888 +            qed
  1.6889 +            show "\<forall>x y. x \<in> ?B b \<and> y \<in> ?B b \<longrightarrow> x = y"
  1.6890 +              apply rule
  1.6891 +              apply rule
  1.6892 +              apply rule
  1.6893 +              apply (unfold split_paired_all)
  1.6894 +              unfolding mem_Collect_eq fst_conv snd_conv
  1.6895 +              apply safe
  1.6896 +            proof -
  1.6897 +              fix x k k'
  1.6898 +              assume k: "(b, k) \<in> p" "(b, k') \<in> p" "content k \<noteq> 0" "content k' \<noteq> 0"
  1.6899 +              guess v using pb[OF k(1)] .. note v = conjunctD2[OF this]
  1.6900 +              guess v' using pb[OF k(2)] .. note v' = conjunctD2[OF this]
  1.6901 +              let ?v = "max v v'"
  1.6902 +              have "box ?v b \<subseteq> k \<inter> k'"
  1.6903 +                unfolding v v' by (auto simp: mem_box)
  1.6904 +                note interior_mono[OF this,unfolded interior_Int]
  1.6905 +              moreover have " ((b + ?v)/2) \<in> box ?v b"
  1.6906 +                using k(3-) unfolding v v' content_eq_0 not_le by (auto simp: mem_box)
  1.6907 +              ultimately have " ((b + ?v)/2) \<in> interior k \<inter> interior k'"
  1.6908 +                unfolding interior_open[OF open_box] by auto
  1.6909 +              then have *: "k = k'"
  1.6910 +                apply -
  1.6911 +                apply (rule ccontr)
  1.6912 +                using p(5)[OF k(1-2)]
  1.6913 +                apply auto
  1.6914 +                done
  1.6915 +              { assume "x \<in> k" then show "x \<in> k'" unfolding * . }
  1.6916 +              { assume "x \<in> k'" then show "x\<in>k" unfolding * . }
  1.6917 +            qed
  1.6918 +
  1.6919 +            let ?a = a and ?b = b (* a is something else while proofing the next theorem. *)
  1.6920 +            show "\<forall>x. x \<in> ?B a \<longrightarrow> norm ((\<lambda>(x, k). content k *\<^sub>R f' x - (f (Sup k) -
  1.6921 +              f (Inf k))) x) \<le> e * (b - a) / 4"
  1.6922 +              apply rule
  1.6923 +              apply rule
  1.6924 +              unfolding mem_Collect_eq
  1.6925 +              unfolding split_paired_all fst_conv snd_conv
  1.6926 +            proof (safe, goal_cases)
  1.6927 +              case prems: 1
  1.6928 +              guess v using pa[OF prems(1)] .. note v = conjunctD2[OF this]
  1.6929 +              have "?a \<in> {?a..v}"
  1.6930 +                using v(2) by auto
  1.6931 +              then have "v \<le> ?b"
  1.6932 +                using p(3)[OF prems(1)] unfolding subset_eq v by auto
  1.6933 +              moreover have "{?a..v} \<subseteq> ball ?a da"
  1.6934 +                using fineD[OF as(2) prems(1)]
  1.6935 +                apply -
  1.6936 +                apply (subst(asm) if_P)
  1.6937 +                apply (rule refl)
  1.6938 +                unfolding subset_eq
  1.6939 +                apply safe
  1.6940 +                apply (erule_tac x=" x" in ballE)
  1.6941 +                apply (auto simp add:subset_eq dist_real_def v)
  1.6942 +                done
  1.6943 +              ultimately show ?case
  1.6944 +                unfolding v interval_bounds_real[OF v(2)] box_real
  1.6945 +                apply -
  1.6946 +                apply(rule da(2)[of "v"])
  1.6947 +                using prems fineD[OF as(2) prems(1)]
  1.6948 +                unfolding v content_eq_0
  1.6949 +                apply auto
  1.6950 +                done
  1.6951 +            qed
  1.6952 +            show "\<forall>x. x \<in> ?B b \<longrightarrow> norm ((\<lambda>(x, k). content k *\<^sub>R f' x -
  1.6953 +              (f (Sup k) - f (Inf k))) x) \<le> e * (b - a) / 4"
  1.6954 +              apply rule
  1.6955 +              apply rule
  1.6956 +              unfolding mem_Collect_eq
  1.6957 +              unfolding split_paired_all fst_conv snd_conv
  1.6958 +            proof (safe, goal_cases)
  1.6959 +              case prems: 1
  1.6960 +              guess v using pb[OF prems(1)] .. note v = conjunctD2[OF this]
  1.6961 +              have "?b \<in> {v.. ?b}"
  1.6962 +                using v(2) by auto
  1.6963 +              then have "v \<ge> ?a" using p(3)[OF prems(1)]
  1.6964 +                unfolding subset_eq v by auto
  1.6965 +              moreover have "{v..?b} \<subseteq> ball ?b db"
  1.6966 +                using fineD[OF as(2) prems(1)]
  1.6967 +                apply -
  1.6968 +                apply (subst(asm) if_P, rule refl)
  1.6969 +                unfolding subset_eq
  1.6970 +                apply safe
  1.6971 +                apply (erule_tac x=" x" in ballE)
  1.6972 +                using ab
  1.6973 +                apply (auto simp add:subset_eq v dist_real_def)
  1.6974 +                done
  1.6975 +              ultimately show ?case
  1.6976 +                unfolding v
  1.6977 +                unfolding interval_bounds_real[OF v(2)] box_real
  1.6978 +                apply -
  1.6979 +                apply(rule db(2)[of "v"])
  1.6980 +                using prems fineD[OF as(2) prems(1)]
  1.6981 +                unfolding v content_eq_0
  1.6982 +                apply auto
  1.6983 +                done
  1.6984 +            qed
  1.6985 +          qed (insert p(1) ab e, auto simp add: field_simps)
  1.6986 +        qed auto
  1.6987 +      qed
  1.6988 +    qed
  1.6989 +  qed
  1.6990 +qed
  1.6991 +
  1.6992 +
  1.6993 +subsection \<open>Stronger form with finite number of exceptional points.\<close>
  1.6994 +
  1.6995 +lemma fundamental_theorem_of_calculus_interior_strong:
  1.6996 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.6997 +  assumes "finite s"
  1.6998 +    and "a \<le> b"
  1.6999 +    and "continuous_on {a .. b} f"
  1.7000 +    and "\<forall>x\<in>{a <..< b} - s. (f has_vector_derivative f'(x)) (at x)"
  1.7001 +  shows "(f' has_integral (f b - f a)) {a .. b}"
  1.7002 +  using assms
  1.7003 +proof (induct "card s" arbitrary: s a b)
  1.7004 +  case 0
  1.7005 +  show ?case
  1.7006 +    apply (rule fundamental_theorem_of_calculus_interior)
  1.7007 +    using 0
  1.7008 +    apply auto
  1.7009 +    done
  1.7010 +next
  1.7011 +  case (Suc n)
  1.7012 +  from this(2) guess c s'
  1.7013 +    apply -
  1.7014 +    apply (subst(asm) eq_commute)
  1.7015 +    unfolding card_Suc_eq
  1.7016 +    apply (subst(asm)(2) eq_commute)
  1.7017 +    apply (elim exE conjE)
  1.7018 +    done
  1.7019 +  note cs = this[rule_format]
  1.7020 +  show ?case
  1.7021 +  proof (cases "c \<in> box a b")
  1.7022 +    case False
  1.7023 +    then show ?thesis
  1.7024 +      apply -
  1.7025 +      apply (rule Suc(1)[OF cs(3) _ Suc(4,5)])
  1.7026 +      apply safe
  1.7027 +      defer
  1.7028 +      apply (rule Suc(6)[rule_format])
  1.7029 +      using Suc(3)
  1.7030 +      unfolding cs
  1.7031 +      apply auto
  1.7032 +      done
  1.7033 +  next
  1.7034 +    have *: "f b - f a = (f c - f a) + (f b - f c)"
  1.7035 +      by auto
  1.7036 +    case True
  1.7037 +    then have "a \<le> c" "c \<le> b"
  1.7038 +      by (auto simp: mem_box)
  1.7039 +    then show ?thesis
  1.7040 +      apply (subst *)
  1.7041 +      apply (rule has_integral_combine)
  1.7042 +      apply assumption+
  1.7043 +      apply (rule_tac[!] Suc(1)[OF cs(3)])
  1.7044 +      using Suc(3)
  1.7045 +      unfolding cs
  1.7046 +    proof -
  1.7047 +      show "continuous_on {a .. c} f" "continuous_on {c .. b} f"
  1.7048 +        apply (rule_tac[!] continuous_on_subset[OF Suc(5)])
  1.7049 +        using True
  1.7050 +        apply (auto simp: mem_box)
  1.7051 +        done
  1.7052 +      let ?P = "\<lambda>i j. \<forall>x\<in>{i <..< j} - s'. (f has_vector_derivative f' x) (at x)"
  1.7053 +      show "?P a c" "?P c b"
  1.7054 +        apply safe
  1.7055 +        apply (rule_tac[!] Suc(6)[rule_format])
  1.7056 +        using True
  1.7057 +        unfolding cs
  1.7058 +        apply (auto simp: mem_box)
  1.7059 +        done
  1.7060 +    qed auto
  1.7061 +  qed
  1.7062 +qed
  1.7063 +
  1.7064 +lemma fundamental_theorem_of_calculus_strong:
  1.7065 +  fixes f :: "real \<Rightarrow> 'a::banach"
  1.7066 +  assumes "finite s"
  1.7067 +    and "a \<le> b"
  1.7068 +    and "continuous_on {a .. b} f"
  1.7069 +    and "\<forall>x\<in>{a .. b} - s. (f has_vector_derivative f'(x)) (at x)"
  1.7070 +  shows "(f' has_integral (f b - f a)) {a .. b}"
  1.7071 +  apply (rule fundamental_theorem_of_calculus_interior_strong[OF assms(1-3), of f'])
  1.7072 +  using assms(4)
  1.7073 +  apply (auto simp: mem_box)
  1.7074 +  done
  1.7075 +
  1.7076 +lemma indefinite_integral_continuous_left:
  1.7077 +  fixes f:: "real \<Rightarrow> 'a::banach"
  1.7078 +  assumes "f integrable_on {a .. b}"
  1.7079 +    and "a < c"
  1.7080 +    and "c \<le> b"
  1.7081 +    and "e > 0"
  1.7082 +  obtains d where "d > 0"
  1.7083 +    and "\<forall>t. c - d < t \<and> t \<le> c \<longrightarrow> norm (integral {a .. c} f - integral {a .. t} f) < e"
  1.7084 +proof -
  1.7085 +  have "\<exists>w>0. \<forall>t. c - w < t \<and> t < c \<longrightarrow> norm (f c) * norm(c - t) < e / 3"
  1.7086 +  proof (cases "f c = 0")
  1.7087 +    case False
  1.7088 +    hence "0 < e / 3 / norm (f c)" using \<open>e>0\<close> by simp
  1.7089 +    then show ?thesis
  1.7090 +      apply -
  1.7091 +      apply rule
  1.7092 +      apply rule
  1.7093 +      apply assumption
  1.7094 +      apply safe
  1.7095 +    proof -
  1.7096 +      fix t
  1.7097 +      assume as: "t < c" and "c - e / 3 / norm (f c) < t"
  1.7098 +      then have "c - t < e / 3 / norm (f c)"
  1.7099 +        by auto
  1.7100 +      then have "norm (c - t) < e / 3 / norm (f c)"
  1.7101 +        using as by auto
  1.7102 +      then show "norm (f c) * norm (c - t) < e / 3"
  1.7103 +        using False
  1.7104 +        apply -
  1.7105 +        apply (subst mult.commute)
  1.7106 +        apply (subst pos_less_divide_eq[symmetric])
  1.7107 +        apply auto
  1.7108 +        done
  1.7109 +    qed
  1.7110 +  next
  1.7111 +    case True
  1.7112 +    show ?thesis
  1.7113 +      apply (rule_tac x=1 in exI)
  1.7114 +      unfolding True
  1.7115 +      using \<open>e > 0\<close>
  1.7116 +      apply auto
  1.7117 +      done
  1.7118 +  qed
  1.7119 +  then guess w .. note w = conjunctD2[OF this,rule_format]
  1.7120 +
  1.7121 +  have *: "e / 3 > 0"
  1.7122 +    using assms by auto
  1.7123 +  have "f integrable_on {a .. c}"
  1.7124 +    apply (rule integrable_subinterval_real[OF assms(1)])
  1.7125 +    using assms(2-3)
  1.7126 +    apply auto
  1.7127 +    done
  1.7128 +  from integrable_integral[OF this,unfolded has_integral_real,rule_format,OF *] guess d1 ..
  1.7129 +  note d1 = conjunctD2[OF this,rule_format]
  1.7130 +  define d where [abs_def]: "d x = ball x w \<inter> d1 x"