HOL-Analysis: move gauges and (tagged) divisions to its own theory file
authorhoelzl
Thu Sep 29 13:02:43 2016 +0200 (2016-09-29)
changeset 63957c3da799b1b45
parent 63956 b235e845c8e8
child 63958 02de4a58e210
HOL-Analysis: move gauges and (tagged) divisions to its own theory file
src/HOL/Analysis/Convex_Euclidean_Space.thy
src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy
src/HOL/Analysis/Henstock_Kurzweil_Integration.thy
src/HOL/Analysis/Tagged_Division.thy
src/HOL/Analysis/Topology_Euclidean_Space.thy
     1.1 --- a/src/HOL/Analysis/Convex_Euclidean_Space.thy	Wed Sep 28 16:15:51 2016 +0200
     1.2 +++ b/src/HOL/Analysis/Convex_Euclidean_Space.thy	Thu Sep 29 13:02:43 2016 +0200
     1.3 @@ -6417,6 +6417,26 @@
     1.4    unfolding segment_convex_hull
     1.5    by (auto intro!: hull_subset[unfolded subset_eq, rule_format])
     1.6  
     1.7 +lemma eventually_closed_segment:
     1.8 +  fixes x0::"'a::real_normed_vector"
     1.9 +  assumes "open X0" "x0 \<in> X0"
    1.10 +  shows "\<forall>\<^sub>F x in at x0 within U. closed_segment x0 x \<subseteq> X0"
    1.11 +proof -
    1.12 +  from openE[OF assms]
    1.13 +  obtain e where e: "0 < e" "ball x0 e \<subseteq> X0" .
    1.14 +  then have "\<forall>\<^sub>F x in at x0 within U. x \<in> ball x0 e"
    1.15 +    by (auto simp: dist_commute eventually_at)
    1.16 +  then show ?thesis
    1.17 +  proof eventually_elim
    1.18 +    case (elim x)
    1.19 +    have "x0 \<in> ball x0 e" using \<open>e > 0\<close> by simp
    1.20 +    from convex_ball[unfolded convex_contains_segment, rule_format, OF this elim]
    1.21 +    have "closed_segment x0 x \<subseteq> ball x0 e" .
    1.22 +    also note \<open>\<dots> \<subseteq> X0\<close>
    1.23 +    finally show ?case .
    1.24 +  qed
    1.25 +qed
    1.26 +
    1.27  lemma segment_furthest_le:
    1.28    fixes a b x y :: "'a::euclidean_space"
    1.29    assumes "x \<in> closed_segment a b"
    1.30 @@ -10500,7 +10520,7 @@
    1.31  
    1.32  lemma collinear_3_expand:
    1.33     "collinear{a,b,c} \<longleftrightarrow> a = c \<or> (\<exists>u. b = u *\<^sub>R a + (1 - u) *\<^sub>R c)"
    1.34 -proof -                          
    1.35 +proof -
    1.36    have "collinear{a,b,c} = collinear{a,c,b}"
    1.37      by (simp add: insert_commute)
    1.38    also have "... = collinear {0, a - c, b - c}"
     2.1 --- a/src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy	Wed Sep 28 16:15:51 2016 +0200
     2.2 +++ b/src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy	Thu Sep 29 13:02:43 2016 +0200
     2.3 @@ -1209,8 +1209,8 @@
     2.4        have sum_p': "(\<Sum>(x, k)\<in>p'. norm (integral k f)) = (\<Sum>k\<in>snd ` p'. norm (integral k f))"
     2.5          apply (subst setsum.over_tagged_division_lemma[OF p'',of "\<lambda>k. norm (integral k f)"])
     2.6          unfolding norm_eq_zero
     2.7 -        apply (rule integral_null)
     2.8 -        apply assumption
     2.9 +         apply (rule integral_null)
    2.10 +        apply (simp add: content_eq_0_interior)
    2.11          apply rule
    2.12          done
    2.13        note snd_p = division_ofD[OF division_of_tagged_division[OF p(1)]]
    2.14 @@ -1657,7 +1657,7 @@
    2.15            show "(\<Sum>(x, k)\<in>p. norm (integral k f)) \<le> ?S"
    2.16              using partial_division_of_tagged_division[of p "cbox a b"] p(1)
    2.17              apply (subst setsum.over_tagged_division_lemma[OF p(1)])
    2.18 -            apply (simp add: integral_null)
    2.19 +            apply (simp add: content_eq_0_interior)
    2.20              apply (intro cSUP_upper2[OF D(2), of "snd ` p"])
    2.21              apply (auto simp: tagged_partial_division_of_def)
    2.22              done
     3.1 --- a/src/HOL/Analysis/Henstock_Kurzweil_Integration.thy	Wed Sep 28 16:15:51 2016 +0200
     3.2 +++ b/src/HOL/Analysis/Henstock_Kurzweil_Integration.thy	Thu Sep 29 13:02:43 2016 +0200
     3.3 @@ -6,201 +6,49 @@
     3.4  
     3.5  theory Henstock_Kurzweil_Integration
     3.6  imports
     3.7 -  Lebesgue_Measure
     3.8 +  Lebesgue_Measure Tagged_Division
     3.9  begin
    3.10  
    3.11 -lemmas scaleR_simps = scaleR_zero_left scaleR_minus_left scaleR_left_diff_distrib
    3.12 -  scaleR_zero_right scaleR_minus_right scaleR_right_diff_distrib scaleR_eq_0_iff
    3.13 -  scaleR_cancel_left scaleR_cancel_right scaleR_add_right scaleR_add_left real_vector_class.scaleR_one
    3.14 -
    3.15 -
    3.16 -subsection \<open>Sundries\<close>
    3.17 -
    3.18 -
    3.19 -text\<open>A transitive relation is well-founded if all initial segments are finite.\<close>
    3.20 -lemma wf_finite_segments:
    3.21 -  assumes "irrefl r" and "trans r" and "\<And>x. finite {y. (y, x) \<in> r}"
    3.22 -  shows "wf (r)"
    3.23 -  apply (simp add: trans_wf_iff wf_iff_acyclic_if_finite converse_def assms)
    3.24 -  using acyclic_def assms irrefl_def trans_Restr by fastforce
    3.25 -
    3.26 -text\<open>For creating values between @{term u} and @{term v}.\<close>
    3.27 -lemma scaling_mono:
    3.28 -  fixes u::"'a::linordered_field"
    3.29 -  assumes "u \<le> v" "0 \<le> r" "r \<le> s"
    3.30 -    shows "u + r * (v - u) / s \<le> v"
    3.31 +(* BEGIN MOVE *)
    3.32 +lemma swap_continuous:
    3.33 +  assumes "continuous_on (cbox (a,c) (b,d)) (\<lambda>(x,y). f x y)"
    3.34 +    shows "continuous_on (cbox (c,a) (d,b)) (\<lambda>(x, y). f y x)"
    3.35  proof -
    3.36 -  have "r/s \<le> 1" using assms
    3.37 -    using divide_le_eq_1 by fastforce
    3.38 -  then have "(r/s) * (v - u) \<le> 1 * (v - u)"
    3.39 -    by (meson diff_ge_0_iff_ge mult_right_mono \<open>u \<le> v\<close>)
    3.40 +  have "(\<lambda>(x, y). f y x) = (\<lambda>(x, y). f x y) \<circ> prod.swap"
    3.41 +    by auto
    3.42    then show ?thesis
    3.43 -    by (simp add: field_simps)
    3.44 +    apply (rule ssubst)
    3.45 +    apply (rule continuous_on_compose)
    3.46 +    apply (simp add: split_def)
    3.47 +    apply (rule continuous_intros | simp add: assms)+
    3.48 +    done
    3.49  qed
    3.50  
    3.51 -lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
    3.52 -lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
    3.53 -lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
    3.54 -
    3.55 -lemma cond_cases: "(P \<Longrightarrow> Q x) \<Longrightarrow> (\<not> P \<Longrightarrow> Q y) \<Longrightarrow> Q (if P then x else y)"
    3.56 +
    3.57 +lemma norm_minus2: "norm (x1-x2, y1-y2) = norm (x2-x1, y2-y1)"
    3.58 +  by (simp add: norm_minus_eqI)
    3.59 +
    3.60 +lemma norm_diff2: "\<lbrakk>y = y1 + y2; x = x1 + x2; e = e1 + e2; norm(y1 - x1) \<le> e1; norm(y2 - x2) \<le> e2\<rbrakk>
    3.61 +  \<Longrightarrow> norm(y - x) \<le> e"
    3.62 +  using norm_triangle_mono [of "y1 - x1" "e1" "y2 - x2" "e2"]
    3.63 +  by (simp add: add_diff_add)
    3.64 +
    3.65 +lemma setcomp_dot1: "{z. P (z \<bullet> (i,0))} = {(x,y). P(x \<bullet> i)}"
    3.66    by auto
    3.67  
    3.68 -declare norm_triangle_ineq4[intro]
    3.69 -
    3.70 -lemma transitive_stepwise_le:
    3.71 -  assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" and "\<And>n. R n (Suc n)"
    3.72 -  shows "\<forall>n\<ge>m. R m n"
    3.73 -proof (intro allI impI)
    3.74 -  show "m \<le> n \<Longrightarrow> R m n" for n
    3.75 -    by (induction rule: dec_induct)
    3.76 -       (use assms in blast)+
    3.77 -qed
    3.78 -
    3.79 -subsection \<open>Some useful lemmas about intervals.\<close>
    3.80 +lemma setcomp_dot2: "{z. P (z \<bullet> (0,i))} = {(x,y). P(y \<bullet> i)}"
    3.81 +  by auto
    3.82 +
    3.83 +lemma Sigma_Int_Paircomp1: "(Sigma A B) \<inter> {(x, y). P x} = Sigma (A \<inter> {x. P x}) B"
    3.84 +  by blast
    3.85 +
    3.86 +lemma Sigma_Int_Paircomp2: "(Sigma A B) \<inter> {(x, y). P y} = Sigma A (\<lambda>z. B z \<inter> {y. P y})"
    3.87 +  by blast
    3.88  
    3.89  lemma empty_as_interval: "{} = cbox One (0::'a::euclidean_space)"
    3.90    using nonempty_Basis
    3.91    by (fastforce simp add: set_eq_iff mem_box)
    3.92 -
    3.93 -lemma interior_subset_union_intervals:
    3.94 -  assumes "i = cbox a b"
    3.95 -    and "j = cbox c d"
    3.96 -    and "interior j \<noteq> {}"
    3.97 -    and "i \<subseteq> j \<union> s"
    3.98 -    and "interior i \<inter> interior j = {}"
    3.99 -  shows "interior i \<subseteq> interior s"
   3.100 -proof -
   3.101 -  have "box a b \<inter> cbox c d = {}"
   3.102 -     using inter_interval_mixed_eq_empty[of c d a b] and assms(3,5)
   3.103 -     unfolding assms(1,2) interior_cbox by auto
   3.104 -  moreover
   3.105 -  have "box a b \<subseteq> cbox c d \<union> s"
   3.106 -    apply (rule order_trans,rule box_subset_cbox)
   3.107 -    using assms(4) unfolding assms(1,2)
   3.108 -    apply auto
   3.109 -    done
   3.110 -  ultimately
   3.111 -  show ?thesis
   3.112 -    unfolding assms interior_cbox
   3.113 -      by auto (metis IntI UnE empty_iff interior_maximal open_box subsetCE subsetI)
   3.114 -qed
   3.115 -
   3.116 -lemma interior_Union_subset_cbox:
   3.117 -  assumes "finite f"
   3.118 -  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"
   3.119 -    and t: "closed t"
   3.120 -  shows "interior (\<Union>f) \<subseteq> t"
   3.121 -proof -
   3.122 -  have [simp]: "s \<in> f \<Longrightarrow> closed s" for s
   3.123 -    using f by auto
   3.124 -  define E where "E = {s\<in>f. interior s = {}}"
   3.125 -  then have "finite E" "E \<subseteq> {s\<in>f. interior s = {}}"
   3.126 -    using \<open>finite f\<close> by auto
   3.127 -  then have "interior (\<Union>f) = interior (\<Union>(f - E))"
   3.128 -  proof (induction E rule: finite_subset_induct')
   3.129 -    case (insert s f')
   3.130 -    have "interior (\<Union>(f - insert s f') \<union> s) = interior (\<Union>(f - insert s f'))"
   3.131 -      using insert.hyps \<open>finite f\<close> by (intro interior_closed_Un_empty_interior) auto
   3.132 -    also have "\<Union>(f - insert s f') \<union> s = \<Union>(f - f')"
   3.133 -      using insert.hyps by auto
   3.134 -    finally show ?case
   3.135 -      by (simp add: insert.IH)
   3.136 -  qed simp
   3.137 -  also have "\<dots> \<subseteq> \<Union>(f - E)"
   3.138 -    by (rule interior_subset)
   3.139 -  also have "\<dots> \<subseteq> t"
   3.140 -  proof (rule Union_least)
   3.141 -    fix s assume "s \<in> f - E"
   3.142 -    with f[of s] obtain a b where s: "s \<in> f" "s = cbox a b" "box a b \<noteq> {}"
   3.143 -      by (fastforce simp: E_def)
   3.144 -    have "closure (interior s) \<subseteq> closure t"
   3.145 -      by (intro closure_mono f \<open>s \<in> f\<close>)
   3.146 -    with s \<open>closed t\<close> show "s \<subseteq> t"
   3.147 -      by (simp add: closure_box)
   3.148 -  qed
   3.149 -  finally show ?thesis .
   3.150 -qed
   3.151 -
   3.152 -lemma inter_interior_unions_intervals:
   3.153 -    "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) = {}"
   3.154 -  using interior_Union_subset_cbox[of f "UNIV - s"] by auto
   3.155 -
   3.156 -lemma interval_split:
   3.157 -  fixes a :: "'a::euclidean_space"
   3.158 -  assumes "k \<in> Basis"
   3.159 -  shows
   3.160 -    "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)"
   3.161 -    "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"
   3.162 -  apply (rule_tac[!] set_eqI)
   3.163 -  unfolding Int_iff mem_box mem_Collect_eq
   3.164 -  using assms
   3.165 -  apply auto
   3.166 -  done
   3.167 -
   3.168 -lemma interval_not_empty: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow> cbox a b \<noteq> {}"
   3.169 -  by (simp add: box_ne_empty)
   3.170 -
   3.171 -subsection \<open>Bounds on intervals where they exist.\<close>
   3.172 -
   3.173 -definition interval_upperbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
   3.174 -  where "interval_upperbound s = (\<Sum>i\<in>Basis. (SUP x:s. x\<bullet>i) *\<^sub>R i)"
   3.175 -
   3.176 -definition interval_lowerbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
   3.177 -  where "interval_lowerbound s = (\<Sum>i\<in>Basis. (INF x:s. x\<bullet>i) *\<^sub>R i)"
   3.178 -
   3.179 -lemma interval_upperbound[simp]:
   3.180 -  "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
   3.181 -    interval_upperbound (cbox a b) = (b::'a::euclidean_space)"
   3.182 -  unfolding interval_upperbound_def euclidean_representation_setsum cbox_def
   3.183 -  by (safe intro!: cSup_eq) auto
   3.184 -
   3.185 -lemma interval_lowerbound[simp]:
   3.186 -  "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
   3.187 -    interval_lowerbound (cbox a b) = (a::'a::euclidean_space)"
   3.188 -  unfolding interval_lowerbound_def euclidean_representation_setsum cbox_def
   3.189 -  by (safe intro!: cInf_eq) auto
   3.190 -
   3.191 -lemmas interval_bounds = interval_upperbound interval_lowerbound
   3.192 -
   3.193 -lemma
   3.194 -  fixes X::"real set"
   3.195 -  shows interval_upperbound_real[simp]: "interval_upperbound X = Sup X"
   3.196 -    and interval_lowerbound_real[simp]: "interval_lowerbound X = Inf X"
   3.197 -  by (auto simp: interval_upperbound_def interval_lowerbound_def)
   3.198 -
   3.199 -lemma interval_bounds'[simp]:
   3.200 -  assumes "cbox a b \<noteq> {}"
   3.201 -  shows "interval_upperbound (cbox a b) = b"
   3.202 -    and "interval_lowerbound (cbox a b) = a"
   3.203 -  using assms unfolding box_ne_empty by auto
   3.204 -
   3.205 -lemma interval_upperbound_Times:
   3.206 -  assumes "A \<noteq> {}" and "B \<noteq> {}"
   3.207 -  shows "interval_upperbound (A \<times> B) = (interval_upperbound A, interval_upperbound B)"
   3.208 -proof-
   3.209 -  from assms have fst_image_times': "A = fst ` (A \<times> B)" by simp
   3.210 -  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)"
   3.211 -      by (subst (2) fst_image_times') (simp del: fst_image_times add: o_def inner_Pair_0)
   3.212 -  moreover from assms have snd_image_times': "B = snd ` (A \<times> B)" by simp
   3.213 -  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)"
   3.214 -      by (subst (2) snd_image_times') (simp del: snd_image_times add: o_def inner_Pair_0)
   3.215 -  ultimately show ?thesis unfolding interval_upperbound_def
   3.216 -      by (subst setsum_Basis_prod_eq) (auto simp add: setsum_prod)
   3.217 -qed
   3.218 -
   3.219 -lemma interval_lowerbound_Times:
   3.220 -  assumes "A \<noteq> {}" and "B \<noteq> {}"
   3.221 -  shows "interval_lowerbound (A \<times> B) = (interval_lowerbound A, interval_lowerbound B)"
   3.222 -proof-
   3.223 -  from assms have fst_image_times': "A = fst ` (A \<times> B)" by simp
   3.224 -  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)"
   3.225 -      by (subst (2) fst_image_times') (simp del: fst_image_times add: o_def inner_Pair_0)
   3.226 -  moreover from assms have snd_image_times': "B = snd ` (A \<times> B)" by simp
   3.227 -  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)"
   3.228 -      by (subst (2) snd_image_times') (simp del: snd_image_times add: o_def inner_Pair_0)
   3.229 -  ultimately show ?thesis unfolding interval_lowerbound_def
   3.230 -      by (subst setsum_Basis_prod_eq) (auto simp add: setsum_prod)
   3.231 -qed
   3.232 +(* END MOVE *)
   3.233  
   3.234  subsection \<open>Content (length, area, volume...) of an interval.\<close>
   3.235  
   3.236 @@ -322,1508 +170,34 @@
   3.237      by (auto simp: not_le)
   3.238  qed
   3.239  
   3.240 -subsection \<open>The notion of a gauge --- simply an open set containing the point.\<close>
   3.241 -
   3.242 -definition "gauge d \<longleftrightarrow> (\<forall>x. x \<in> d x \<and> open (d x))"
   3.243 -
   3.244 -lemma gaugeI:
   3.245 -  assumes "\<And>x. x \<in> g x"
   3.246 -    and "\<And>x. open (g x)"
   3.247 -  shows "gauge g"
   3.248 -  using assms unfolding gauge_def by auto
   3.249 -
   3.250 -lemma gaugeD[dest]:
   3.251 -  assumes "gauge d"
   3.252 -  shows "x \<in> d x"
   3.253 -    and "open (d x)"
   3.254 -  using assms unfolding gauge_def by auto
   3.255 -
   3.256 -lemma gauge_ball_dependent: "\<forall>x. 0 < e x \<Longrightarrow> gauge (\<lambda>x. ball x (e x))"
   3.257 -  unfolding gauge_def by auto
   3.258 -
   3.259 -lemma gauge_ball[intro]: "0 < e \<Longrightarrow> gauge (\<lambda>x. ball x e)"
   3.260 -  unfolding gauge_def by auto
   3.261 -
   3.262 -lemma gauge_trivial[intro!]: "gauge (\<lambda>x. ball x 1)"
   3.263 -  by (rule gauge_ball) auto
   3.264 -
   3.265 -lemma gauge_inter[intro]: "gauge d1 \<Longrightarrow> gauge d2 \<Longrightarrow> gauge (\<lambda>x. d1 x \<inter> d2 x)"
   3.266 -  unfolding gauge_def by auto
   3.267 -
   3.268 -lemma gauge_inters:
   3.269 -  assumes "finite s"
   3.270 -    and "\<forall>d\<in>s. gauge (f d)"
   3.271 -  shows "gauge (\<lambda>x. \<Inter>{f d x | d. d \<in> s})"
   3.272 -proof -
   3.273 -  have *: "\<And>x. {f d x |d. d \<in> s} = (\<lambda>d. f d x) ` s"
   3.274 -    by auto
   3.275 -  show ?thesis
   3.276 -    unfolding gauge_def unfolding *
   3.277 -    using assms unfolding Ball_def Inter_iff mem_Collect_eq gauge_def by auto
   3.278 -qed
   3.279 -
   3.280 -lemma gauge_existence_lemma:
   3.281 -  "(\<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)"
   3.282 -  by (metis zero_less_one)
   3.283 -
   3.284 -
   3.285 -subsection \<open>Divisions.\<close>
   3.286 -
   3.287 -definition division_of (infixl "division'_of" 40)
   3.288 -where
   3.289 -  "s division_of i \<longleftrightarrow>
   3.290 -    finite s \<and>
   3.291 -    (\<forall>k\<in>s. k \<subseteq> i \<and> k \<noteq> {} \<and> (\<exists>a b. k = cbox a b)) \<and>
   3.292 -    (\<forall>k1\<in>s. \<forall>k2\<in>s. k1 \<noteq> k2 \<longrightarrow> interior(k1) \<inter> interior(k2) = {}) \<and>
   3.293 -    (\<Union>s = i)"
   3.294 -
   3.295 -lemma division_ofD[dest]:
   3.296 -  assumes "s division_of i"
   3.297 -  shows "finite s"
   3.298 -    and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
   3.299 -    and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
   3.300 -    and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
   3.301 -    and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}"
   3.302 -    and "\<Union>s = i"
   3.303 -  using assms unfolding division_of_def by auto
   3.304 -
   3.305 -lemma division_ofI:
   3.306 -  assumes "finite s"
   3.307 -    and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
   3.308 -    and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
   3.309 -    and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
   3.310 -    and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
   3.311 -    and "\<Union>s = i"
   3.312 -  shows "s division_of i"
   3.313 -  using assms unfolding division_of_def by auto
   3.314 -
   3.315 -lemma division_of_finite: "s division_of i \<Longrightarrow> finite s"
   3.316 -  unfolding division_of_def by auto
   3.317 -
   3.318 -lemma division_of_self[intro]: "cbox a b \<noteq> {} \<Longrightarrow> {cbox a b} division_of (cbox a b)"
   3.319 -  unfolding division_of_def by auto
   3.320 -
   3.321 -lemma division_of_trivial[simp]: "s division_of {} \<longleftrightarrow> s = {}"
   3.322 -  unfolding division_of_def by auto
   3.323 -
   3.324 -lemma division_of_sing[simp]:
   3.325 -  "s division_of cbox a (a::'a::euclidean_space) \<longleftrightarrow> s = {cbox a a}"
   3.326 -  (is "?l = ?r")
   3.327 -proof
   3.328 -  assume ?r
   3.329 -  moreover
   3.330 -  { fix k
   3.331 -    assume "s = {{a}}" "k\<in>s"
   3.332 -    then have "\<exists>x y. k = cbox x y"
   3.333 -      apply (rule_tac x=a in exI)+
   3.334 -      apply (force simp: cbox_sing)
   3.335 -      done
   3.336 -  }
   3.337 -  ultimately show ?l
   3.338 -    unfolding division_of_def cbox_sing by auto
   3.339 -next
   3.340 -  assume ?l
   3.341 -  note * = conjunctD4[OF this[unfolded division_of_def cbox_sing]]
   3.342 -  {
   3.343 -    fix x
   3.344 -    assume x: "x \<in> s" have "x = {a}"
   3.345 -      using *(2)[rule_format,OF x] by auto
   3.346 -  }
   3.347 -  moreover have "s \<noteq> {}"
   3.348 -    using *(4) by auto
   3.349 -  ultimately show ?r
   3.350 -    unfolding cbox_sing by auto
   3.351 -qed
   3.352 -
   3.353 -lemma elementary_empty: obtains p where "p division_of {}"
   3.354 -  unfolding division_of_trivial by auto
   3.355 -
   3.356 -lemma elementary_interval: obtains p where "p division_of (cbox a b)"
   3.357 -  by (metis division_of_trivial division_of_self)
   3.358 -
   3.359 -lemma division_contains: "s division_of i \<Longrightarrow> \<forall>x\<in>i. \<exists>k\<in>s. x \<in> k"
   3.360 -  unfolding division_of_def by auto
   3.361 -
   3.362 -lemma forall_in_division:
   3.363 -  "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))"
   3.364 -  unfolding division_of_def by fastforce
   3.365 -
   3.366 -lemma division_of_subset:
   3.367 -  assumes "p division_of (\<Union>p)"
   3.368 -    and "q \<subseteq> p"
   3.369 -  shows "q division_of (\<Union>q)"
   3.370 -proof (rule division_ofI)
   3.371 -  note * = division_ofD[OF assms(1)]
   3.372 -  show "finite q"
   3.373 -    using "*"(1) assms(2) infinite_super by auto
   3.374 -  {
   3.375 -    fix k
   3.376 -    assume "k \<in> q"
   3.377 -    then have kp: "k \<in> p"
   3.378 -      using assms(2) by auto
   3.379 -    show "k \<subseteq> \<Union>q"
   3.380 -      using \<open>k \<in> q\<close> by auto
   3.381 -    show "\<exists>a b. k = cbox a b"
   3.382 -      using *(4)[OF kp] by auto
   3.383 -    show "k \<noteq> {}"
   3.384 -      using *(3)[OF kp] by auto
   3.385 -  }
   3.386 -  fix k1 k2
   3.387 -  assume "k1 \<in> q" "k2 \<in> q" "k1 \<noteq> k2"
   3.388 -  then have **: "k1 \<in> p" "k2 \<in> p" "k1 \<noteq> k2"
   3.389 -    using assms(2) by auto
   3.390 -  show "interior k1 \<inter> interior k2 = {}"
   3.391 -    using *(5)[OF **] by auto
   3.392 -qed auto
   3.393 -
   3.394 -lemma division_of_union_self[intro]: "p division_of s \<Longrightarrow> p division_of (\<Union>p)"
   3.395 -  unfolding division_of_def by auto
   3.396 -
   3.397  lemma division_of_content_0:
   3.398    assumes "content (cbox a b) = 0" "d division_of (cbox a b)"
   3.399    shows "\<forall>k\<in>d. content k = 0"
   3.400    unfolding forall_in_division[OF assms(2)]
   3.401    by (metis antisym_conv assms content_pos_le content_subset division_ofD(2))
   3.402  
   3.403 -lemma division_inter:
   3.404 -  fixes s1 s2 :: "'a::euclidean_space set"
   3.405 -  assumes "p1 division_of s1"
   3.406 -    and "p2 division_of s2"
   3.407 -  shows "{k1 \<inter> k2 | k1 k2. k1 \<in> p1 \<and> k2 \<in> p2 \<and> k1 \<inter> k2 \<noteq> {}} division_of (s1 \<inter> s2)"
   3.408 -  (is "?A' division_of _")
   3.409 -proof -
   3.410 -  let ?A = "{s. s \<in>  (\<lambda>(k1,k2). k1 \<inter> k2) ` (p1 \<times> p2) \<and> s \<noteq> {}}"
   3.411 -  have *: "?A' = ?A" by auto
   3.412 -  show ?thesis
   3.413 -    unfolding *
   3.414 -  proof (rule division_ofI)
   3.415 -    have "?A \<subseteq> (\<lambda>(x, y). x \<inter> y) ` (p1 \<times> p2)"
   3.416 -      by auto
   3.417 -    moreover have "finite (p1 \<times> p2)"
   3.418 -      using assms unfolding division_of_def by auto
   3.419 -    ultimately show "finite ?A" by auto
   3.420 -    have *: "\<And>s. \<Union>{x\<in>s. x \<noteq> {}} = \<Union>s"
   3.421 -      by auto
   3.422 -    show "\<Union>?A = s1 \<inter> s2"
   3.423 -      apply (rule set_eqI)
   3.424 -      unfolding * and UN_iff
   3.425 -      using division_ofD(6)[OF assms(1)] and division_ofD(6)[OF assms(2)]
   3.426 -      apply auto
   3.427 -      done
   3.428 -    {
   3.429 -      fix k
   3.430 -      assume "k \<in> ?A"
   3.431 -      then obtain k1 k2 where k: "k = k1 \<inter> k2" "k1 \<in> p1" "k2 \<in> p2" "k \<noteq> {}"
   3.432 -        by auto
   3.433 -      then show "k \<noteq> {}"
   3.434 -        by auto
   3.435 -      show "k \<subseteq> s1 \<inter> s2"
   3.436 -        using division_ofD(2)[OF assms(1) k(2)] and division_ofD(2)[OF assms(2) k(3)]
   3.437 -        unfolding k by auto
   3.438 -      obtain a1 b1 where k1: "k1 = cbox a1 b1"
   3.439 -        using division_ofD(4)[OF assms(1) k(2)] by blast
   3.440 -      obtain a2 b2 where k2: "k2 = cbox a2 b2"
   3.441 -        using division_ofD(4)[OF assms(2) k(3)] by blast
   3.442 -      show "\<exists>a b. k = cbox a b"
   3.443 -        unfolding k k1 k2 unfolding Int_interval by auto
   3.444 -    }
   3.445 -    fix k1 k2
   3.446 -    assume "k1 \<in> ?A"
   3.447 -    then obtain x1 y1 where k1: "k1 = x1 \<inter> y1" "x1 \<in> p1" "y1 \<in> p2" "k1 \<noteq> {}"
   3.448 -      by auto
   3.449 -    assume "k2 \<in> ?A"
   3.450 -    then obtain x2 y2 where k2: "k2 = x2 \<inter> y2" "x2 \<in> p1" "y2 \<in> p2" "k2 \<noteq> {}"
   3.451 -      by auto
   3.452 -    assume "k1 \<noteq> k2"
   3.453 -    then have th: "x1 \<noteq> x2 \<or> y1 \<noteq> y2"
   3.454 -      unfolding k1 k2 by auto
   3.455 -    have *: "interior x1 \<inter> interior x2 = {} \<or> interior y1 \<inter> interior y2 = {} \<Longrightarrow>
   3.456 -      interior (x1 \<inter> y1) \<subseteq> interior x1 \<Longrightarrow> interior (x1 \<inter> y1) \<subseteq> interior y1 \<Longrightarrow>
   3.457 -      interior (x2 \<inter> y2) \<subseteq> interior x2 \<Longrightarrow> interior (x2 \<inter> y2) \<subseteq> interior y2 \<Longrightarrow>
   3.458 -      interior (x1 \<inter> y1) \<inter> interior (x2 \<inter> y2) = {}" by auto
   3.459 -    show "interior k1 \<inter> interior k2 = {}"
   3.460 -      unfolding k1 k2
   3.461 -      apply (rule *)
   3.462 -      using assms division_ofD(5) k1 k2(2) k2(3) th apply auto
   3.463 -      done
   3.464 -  qed
   3.465 -qed
   3.466 -
   3.467 -lemma division_inter_1:
   3.468 -  assumes "d division_of i"
   3.469 -    and "cbox a (b::'a::euclidean_space) \<subseteq> i"
   3.470 -  shows "{cbox a b \<inter> k | k. k \<in> d \<and> cbox a b \<inter> k \<noteq> {}} division_of (cbox a b)"
   3.471 -proof (cases "cbox a b = {}")
   3.472 -  case True
   3.473 -  show ?thesis
   3.474 -    unfolding True and division_of_trivial by auto
   3.475 -next
   3.476 -  case False
   3.477 -  have *: "cbox a b \<inter> i = cbox a b" using assms(2) by auto
   3.478 -  show ?thesis
   3.479 -    using division_inter[OF division_of_self[OF False] assms(1)]
   3.480 -    unfolding * by auto
   3.481 -qed
   3.482 -
   3.483 -lemma elementary_inter:
   3.484 -  fixes s t :: "'a::euclidean_space set"
   3.485 -  assumes "p1 division_of s"
   3.486 -    and "p2 division_of t"
   3.487 -  shows "\<exists>p. p division_of (s \<inter> t)"
   3.488 -using assms division_inter by blast
   3.489 -
   3.490 -lemma elementary_inters:
   3.491 -  assumes "finite f"
   3.492 -    and "f \<noteq> {}"
   3.493 -    and "\<forall>s\<in>f. \<exists>p. p division_of (s::('a::euclidean_space) set)"
   3.494 -  shows "\<exists>p. p division_of (\<Inter>f)"
   3.495 -  using assms
   3.496 -proof (induct f rule: finite_induct)
   3.497 -  case (insert x f)
   3.498 -  show ?case
   3.499 -  proof (cases "f = {}")
   3.500 -    case True
   3.501 -    then show ?thesis
   3.502 -      unfolding True using insert by auto
   3.503 -  next
   3.504 -    case False
   3.505 -    obtain p where "p division_of \<Inter>f"
   3.506 -      using insert(3)[OF False insert(5)[unfolded ball_simps,THEN conjunct2]] ..
   3.507 -    moreover obtain px where "px division_of x"
   3.508 -      using insert(5)[rule_format,OF insertI1] ..
   3.509 -    ultimately show ?thesis
   3.510 -      by (simp add: elementary_inter Inter_insert)
   3.511 -  qed
   3.512 -qed auto
   3.513 -
   3.514 -lemma division_disjoint_union:
   3.515 -  assumes "p1 division_of s1"
   3.516 -    and "p2 division_of s2"
   3.517 -    and "interior s1 \<inter> interior s2 = {}"
   3.518 -  shows "(p1 \<union> p2) division_of (s1 \<union> s2)"
   3.519 -proof (rule division_ofI)
   3.520 -  note d1 = division_ofD[OF assms(1)]
   3.521 -  note d2 = division_ofD[OF assms(2)]
   3.522 -  show "finite (p1 \<union> p2)"
   3.523 -    using d1(1) d2(1) by auto
   3.524 -  show "\<Union>(p1 \<union> p2) = s1 \<union> s2"
   3.525 -    using d1(6) d2(6) by auto
   3.526 -  {
   3.527 -    fix k1 k2
   3.528 -    assume as: "k1 \<in> p1 \<union> p2" "k2 \<in> p1 \<union> p2" "k1 \<noteq> k2"
   3.529 -    moreover
   3.530 -    let ?g="interior k1 \<inter> interior k2 = {}"
   3.531 -    {
   3.532 -      assume as: "k1\<in>p1" "k2\<in>p2"
   3.533 -      have ?g
   3.534 -        using interior_mono[OF d1(2)[OF as(1)]] interior_mono[OF d2(2)[OF as(2)]]
   3.535 -        using assms(3) by blast
   3.536 -    }
   3.537 -    moreover
   3.538 -    {
   3.539 -      assume as: "k1\<in>p2" "k2\<in>p1"
   3.540 -      have ?g
   3.541 -        using interior_mono[OF d1(2)[OF as(2)]] interior_mono[OF d2(2)[OF as(1)]]
   3.542 -        using assms(3) by blast
   3.543 -    }
   3.544 -    ultimately show ?g
   3.545 -      using d1(5)[OF _ _ as(3)] and d2(5)[OF _ _ as(3)] by auto
   3.546 -  }
   3.547 -  fix k
   3.548 -  assume k: "k \<in> p1 \<union> p2"
   3.549 -  show "k \<subseteq> s1 \<union> s2"
   3.550 -    using k d1(2) d2(2) by auto
   3.551 -  show "k \<noteq> {}"
   3.552 -    using k d1(3) d2(3) by auto
   3.553 -  show "\<exists>a b. k = cbox a b"
   3.554 -    using k d1(4) d2(4) by auto
   3.555 -qed
   3.556 -
   3.557 -lemma partial_division_extend_1:
   3.558 -  fixes a b c d :: "'a::euclidean_space"
   3.559 -  assumes incl: "cbox c d \<subseteq> cbox a b"
   3.560 -    and nonempty: "cbox c d \<noteq> {}"
   3.561 -  obtains p where "p division_of (cbox a b)" "cbox c d \<in> p"
   3.562 -proof
   3.563 -  let ?B = "\<lambda>f::'a\<Rightarrow>'a \<times> 'a.
   3.564 -    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)"
   3.565 -  define p where "p = ?B ` (Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)})"
   3.566 -
   3.567 -  show "cbox c d \<in> p"
   3.568 -    unfolding p_def
   3.569 -    by (auto simp add: box_eq_empty cbox_def intro!: image_eqI[where x="\<lambda>(i::'a)\<in>Basis. (c, d)"])
   3.570 -  {
   3.571 -    fix i :: 'a
   3.572 -    assume "i \<in> Basis"
   3.573 -    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"
   3.574 -      unfolding box_eq_empty subset_box by (auto simp: not_le)
   3.575 -  }
   3.576 -  note ord = this
   3.577 -
   3.578 -  show "p division_of (cbox a b)"
   3.579 -  proof (rule division_ofI)
   3.580 -    show "finite p"
   3.581 -      unfolding p_def by (auto intro!: finite_PiE)
   3.582 -    {
   3.583 -      fix k
   3.584 -      assume "k \<in> p"
   3.585 -      then obtain f where f: "f \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and k: "k = ?B f"
   3.586 -        by (auto simp: p_def)
   3.587 -      then show "\<exists>a b. k = cbox a b"
   3.588 -        by auto
   3.589 -      have "k \<subseteq> cbox a b \<and> k \<noteq> {}"
   3.590 -      proof (simp add: k box_eq_empty subset_box not_less, safe)
   3.591 -        fix i :: 'a
   3.592 -        assume i: "i \<in> Basis"
   3.593 -        with f have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
   3.594 -          by (auto simp: PiE_iff)
   3.595 -        with i ord[of i]
   3.596 -        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"
   3.597 -          by auto
   3.598 -      qed
   3.599 -      then show "k \<noteq> {}" "k \<subseteq> cbox a b"
   3.600 -        by auto
   3.601 -      {
   3.602 -        fix l
   3.603 -        assume "l \<in> p"
   3.604 -        then obtain g where g: "g \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and l: "l = ?B g"
   3.605 -          by (auto simp: p_def)
   3.606 -        assume "l \<noteq> k"
   3.607 -        have "\<exists>i\<in>Basis. f i \<noteq> g i"
   3.608 -        proof (rule ccontr)
   3.609 -          assume "\<not> ?thesis"
   3.610 -          with f g have "f = g"
   3.611 -            by (auto simp: PiE_iff extensional_def intro!: ext)
   3.612 -          with \<open>l \<noteq> k\<close> show False
   3.613 -            by (simp add: l k)
   3.614 -        qed
   3.615 -        then obtain i where *: "i \<in> Basis" "f i \<noteq> g i" ..
   3.616 -        then have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
   3.617 -                  "g i = (a, c) \<or> g i = (c, d) \<or> g i = (d, b)"
   3.618 -          using f g by (auto simp: PiE_iff)
   3.619 -        with * ord[of i] show "interior l \<inter> interior k = {}"
   3.620 -          by (auto simp add: l k interior_cbox disjoint_interval intro!: bexI[of _ i])
   3.621 -      }
   3.622 -      note \<open>k \<subseteq> cbox a b\<close>
   3.623 -    }
   3.624 -    moreover
   3.625 -    {
   3.626 -      fix x assume x: "x \<in> cbox a b"
   3.627 -      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)}"
   3.628 -      proof
   3.629 -        fix i :: 'a
   3.630 -        assume "i \<in> Basis"
   3.631 -        with x ord[of i]
   3.632 -        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>
   3.633 -            (d \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i)"
   3.634 -          by (auto simp: cbox_def)
   3.635 -        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)}"
   3.636 -          by auto
   3.637 -      qed
   3.638 -      then obtain f where
   3.639 -        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)}"
   3.640 -        unfolding bchoice_iff ..
   3.641 -      moreover from f have "restrict f Basis \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}"
   3.642 -        by auto
   3.643 -      moreover from f have "x \<in> ?B (restrict f Basis)"
   3.644 -        by (auto simp: mem_box)
   3.645 -      ultimately have "\<exists>k\<in>p. x \<in> k"
   3.646 -        unfolding p_def by blast
   3.647 -    }
   3.648 -    ultimately show "\<Union>p = cbox a b"
   3.649 -      by auto
   3.650 -  qed
   3.651 -qed
   3.652 -
   3.653 -lemma partial_division_extend_interval:
   3.654 -  assumes "p division_of (\<Union>p)" "(\<Union>p) \<subseteq> cbox a b"
   3.655 -  obtains q where "p \<subseteq> q" "q division_of cbox a (b::'a::euclidean_space)"
   3.656 -proof (cases "p = {}")
   3.657 -  case True
   3.658 -  obtain q where "q division_of (cbox a b)"
   3.659 -    by (rule elementary_interval)
   3.660 -  then show ?thesis
   3.661 -    using True that by blast
   3.662 -next
   3.663 -  case False
   3.664 -  note p = division_ofD[OF assms(1)]
   3.665 -  have div_cbox: "\<forall>k\<in>p. \<exists>q. q division_of cbox a b \<and> k \<in> q"
   3.666 -  proof
   3.667 -    fix k
   3.668 -    assume kp: "k \<in> p"
   3.669 -    obtain c d where k: "k = cbox c d"
   3.670 -      using p(4)[OF kp] by blast
   3.671 -    have *: "cbox c d \<subseteq> cbox a b" "cbox c d \<noteq> {}"
   3.672 -      using p(2,3)[OF kp, unfolded k] using assms(2)
   3.673 -      by (blast intro: order.trans)+
   3.674 -    obtain q where "q division_of cbox a b" "cbox c d \<in> q"
   3.675 -      by (rule partial_division_extend_1[OF *])
   3.676 -    then show "\<exists>q. q division_of cbox a b \<and> k \<in> q"
   3.677 -      unfolding k by auto
   3.678 -  qed
   3.679 -  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"
   3.680 -    using bchoice[OF div_cbox] by blast
   3.681 -  { fix x
   3.682 -    assume x: "x \<in> p"
   3.683 -    have "q x division_of \<Union>q x"
   3.684 -      apply (rule division_ofI)
   3.685 -      using division_ofD[OF q(1)[OF x]]
   3.686 -      apply auto
   3.687 -      done }
   3.688 -  then have "\<And>x. x \<in> p \<Longrightarrow> \<exists>d. d division_of \<Union>(q x - {x})"
   3.689 -    by (meson Diff_subset division_of_subset)
   3.690 -  then have "\<exists>d. d division_of \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p)"
   3.691 -    apply -
   3.692 -    apply (rule elementary_inters [OF finite_imageI[OF p(1)]])
   3.693 -    apply (auto simp: False elementary_inters [OF finite_imageI[OF p(1)]])
   3.694 -    done
   3.695 -  then obtain d where d: "d division_of \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p)" ..
   3.696 -  have "d \<union> p division_of cbox a b"
   3.697 -  proof -
   3.698 -    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
   3.699 -    have cbox_eq: "cbox a b = \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p) \<union> \<Union>p"
   3.700 -    proof (rule te[OF False], clarify)
   3.701 -      fix i
   3.702 -      assume i: "i \<in> p"
   3.703 -      show "\<Union>(q i - {i}) \<union> i = cbox a b"
   3.704 -        using division_ofD(6)[OF q(1)[OF i]] using q(2)[OF i] by auto
   3.705 -    qed
   3.706 -    { fix k
   3.707 -      assume k: "k \<in> p"
   3.708 -      have *: "\<And>u t s. t \<inter> s = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<inter> t = {}"
   3.709 -        by auto
   3.710 -      have "interior (\<Inter>i\<in>p. \<Union>(q i - {i})) \<inter> interior k = {}"
   3.711 -      proof (rule *[OF inter_interior_unions_intervals])
   3.712 -        note qk=division_ofD[OF q(1)[OF k]]
   3.713 -        show "finite (q k - {k})" "open (interior k)" "\<forall>t\<in>q k - {k}. \<exists>a b. t = cbox a b"
   3.714 -          using qk by auto
   3.715 -        show "\<forall>t\<in>q k - {k}. interior k \<inter> interior t = {}"
   3.716 -          using qk(5) using q(2)[OF k] by auto
   3.717 -        show "interior (\<Inter>i\<in>p. \<Union>(q i - {i})) \<subseteq> interior (\<Union>(q k - {k}))"
   3.718 -          apply (rule interior_mono)+
   3.719 -          using k
   3.720 -          apply auto
   3.721 -          done
   3.722 -      qed } note [simp] = this
   3.723 -    show "d \<union> p division_of (cbox a b)"
   3.724 -      unfolding cbox_eq
   3.725 -      apply (rule division_disjoint_union[OF d assms(1)])
   3.726 -      apply (rule inter_interior_unions_intervals)
   3.727 -      apply (rule p open_interior ballI)+
   3.728 -      apply simp_all
   3.729 -      done
   3.730 -  qed
   3.731 -  then show ?thesis
   3.732 -    by (meson Un_upper2 that)
   3.733 -qed
   3.734 -
   3.735 -lemma elementary_bounded[dest]:
   3.736 -  fixes s :: "'a::euclidean_space set"
   3.737 -  shows "p division_of s \<Longrightarrow> bounded s"
   3.738 -  unfolding division_of_def by (metis bounded_Union bounded_cbox)
   3.739 -
   3.740 -lemma elementary_subset_cbox:
   3.741 -  "p division_of s \<Longrightarrow> \<exists>a b. s \<subseteq> cbox a (b::'a::euclidean_space)"
   3.742 -  by (meson elementary_bounded bounded_subset_cbox)
   3.743 -
   3.744 -lemma division_union_intervals_exists:
   3.745 -  fixes a b :: "'a::euclidean_space"
   3.746 -  assumes "cbox a b \<noteq> {}"
   3.747 -  obtains p where "(insert (cbox a b) p) division_of (cbox a b \<union> cbox c d)"
   3.748 -proof (cases "cbox c d = {}")
   3.749 -  case True
   3.750 -  show ?thesis
   3.751 -    apply (rule that[of "{}"])
   3.752 -    unfolding True
   3.753 -    using assms
   3.754 -    apply auto
   3.755 -    done
   3.756 -next
   3.757 -  case False
   3.758 -  show ?thesis
   3.759 -  proof (cases "cbox a b \<inter> cbox c d = {}")
   3.760 -    case True
   3.761 -    then show ?thesis
   3.762 -      by (metis that False assms division_disjoint_union division_of_self insert_is_Un interior_Int interior_empty)
   3.763 -  next
   3.764 -    case False
   3.765 -    obtain u v where uv: "cbox a b \<inter> cbox c d = cbox u v"
   3.766 -      unfolding Int_interval by auto
   3.767 -    have uv_sub: "cbox u v \<subseteq> cbox c d" using uv by auto
   3.768 -    obtain p where "p division_of cbox c d" "cbox u v \<in> p"
   3.769 -      by (rule partial_division_extend_1[OF uv_sub False[unfolded uv]])
   3.770 -    note p = this division_ofD[OF this(1)]
   3.771 -    have "interior (cbox a b \<inter> \<Union>(p - {cbox u v})) = interior(cbox u v \<inter> \<Union>(p - {cbox u v}))"
   3.772 -      apply (rule arg_cong[of _ _ interior])
   3.773 -      using p(8) uv by auto
   3.774 -    also have "\<dots> = {}"
   3.775 -      unfolding interior_Int
   3.776 -      apply (rule inter_interior_unions_intervals)
   3.777 -      using p(6) p(7)[OF p(2)] p(3)
   3.778 -      apply auto
   3.779 -      done
   3.780 -    finally have [simp]: "interior (cbox a b) \<inter> interior (\<Union>(p - {cbox u v})) = {}" by simp
   3.781 -    have cbe: "cbox a b \<union> cbox c d = cbox a b \<union> \<Union>(p - {cbox u v})"
   3.782 -      using p(8) unfolding uv[symmetric] by auto
   3.783 -    have "insert (cbox a b) (p - {cbox u v}) division_of cbox a b \<union> \<Union>(p - {cbox u v})"
   3.784 -    proof -
   3.785 -      have "{cbox a b} division_of cbox a b"
   3.786 -        by (simp add: assms division_of_self)
   3.787 -      then show "insert (cbox a b) (p - {cbox u v}) division_of cbox a b \<union> \<Union>(p - {cbox u v})"
   3.788 -        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))
   3.789 -    qed
   3.790 -    with that[of "p - {cbox u v}"] show ?thesis by (simp add: cbe)
   3.791 -  qed
   3.792 -qed
   3.793 -
   3.794 -lemma division_of_unions:
   3.795 -  assumes "finite f"
   3.796 -    and "\<And>p. p \<in> f \<Longrightarrow> p division_of (\<Union>p)"
   3.797 -    and "\<And>k1 k2. k1 \<in> \<Union>f \<Longrightarrow> k2 \<in> \<Union>f \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
   3.798 -  shows "\<Union>f division_of \<Union>\<Union>f"
   3.799 -  using assms
   3.800 -  by (auto intro!: division_ofI)
   3.801 -
   3.802 -lemma elementary_union_interval:
   3.803 -  fixes a b :: "'a::euclidean_space"
   3.804 -  assumes "p division_of \<Union>p"
   3.805 -  obtains q where "q division_of (cbox a b \<union> \<Union>p)"
   3.806 -proof -
   3.807 -  note assm = division_ofD[OF assms]
   3.808 -  have lem1: "\<And>f s. \<Union>\<Union>(f ` s) = \<Union>((\<lambda>x. \<Union>(f x)) ` s)"
   3.809 -    by auto
   3.810 -  have lem2: "\<And>f s. f \<noteq> {} \<Longrightarrow> \<Union>{s \<union> t |t. t \<in> f} = s \<union> \<Union>f"
   3.811 +lemma setsum_content_null:
   3.812 +  assumes "content (cbox a b) = 0"
   3.813 +    and "p tagged_division_of (cbox a b)"
   3.814 +  shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
   3.815 +proof (rule setsum.neutral, rule)
   3.816 +  fix y
   3.817 +  assume y: "y \<in> p"
   3.818 +  obtain x k where xk: "y = (x, k)"
   3.819 +    using surj_pair[of y] by blast
   3.820 +  note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
   3.821 +  from this(2) obtain c d where k: "k = cbox c d" by blast
   3.822 +  have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x"
   3.823 +    unfolding xk by auto
   3.824 +  also have "\<dots> = 0"
   3.825 +    using content_subset[OF assm(1)[unfolded k]] content_pos_le[of c d]
   3.826 +    unfolding assms(1) k
   3.827      by auto
   3.828 -  {
   3.829 -    presume "p = {} \<Longrightarrow> thesis"
   3.830 -      "cbox a b = {} \<Longrightarrow> thesis"
   3.831 -      "cbox a b \<noteq> {} \<Longrightarrow> interior (cbox a b) = {} \<Longrightarrow> thesis"
   3.832 -      "p \<noteq> {} \<Longrightarrow> interior (cbox a b)\<noteq>{} \<Longrightarrow> cbox a b \<noteq> {} \<Longrightarrow> thesis"
   3.833 -    then show thesis by auto
   3.834 -  next
   3.835 -    assume as: "p = {}"
   3.836 -    obtain p where "p division_of (cbox a b)"
   3.837 -      by (rule elementary_interval)
   3.838 -    then show thesis
   3.839 -      using as that by auto
   3.840 -  next
   3.841 -    assume as: "cbox a b = {}"
   3.842 -    show thesis
   3.843 -      using as assms that by auto
   3.844 -  next
   3.845 -    assume as: "interior (cbox a b) = {}" "cbox a b \<noteq> {}"
   3.846 -    show thesis
   3.847 -      apply (rule that[of "insert (cbox a b) p"],rule division_ofI)
   3.848 -      unfolding finite_insert
   3.849 -      apply (rule assm(1)) unfolding Union_insert
   3.850 -      using assm(2-4) as
   3.851 -      apply -
   3.852 -      apply (fast dest: assm(5))+
   3.853 -      done
   3.854 -  next
   3.855 -    assume as: "p \<noteq> {}" "interior (cbox a b) \<noteq> {}" "cbox a b \<noteq> {}"
   3.856 -    have "\<forall>k\<in>p. \<exists>q. (insert (cbox a b) q) division_of (cbox a b \<union> k)"
   3.857 -    proof
   3.858 -      fix k
   3.859 -      assume kp: "k \<in> p"
   3.860 -      from assm(4)[OF kp] obtain c d where "k = cbox c d" by blast
   3.861 -      then show "\<exists>q. (insert (cbox a b) q) division_of (cbox a b \<union> k)"
   3.862 -        by (meson as(3) division_union_intervals_exists)
   3.863 -    qed
   3.864 -    from bchoice[OF this] obtain q where "\<forall>x\<in>p. insert (cbox a b) (q x) division_of (cbox a b) \<union> x" ..
   3.865 -    note q = division_ofD[OF this[rule_format]]
   3.866 -    let ?D = "\<Union>{insert (cbox a b) (q k) | k. k \<in> p}"
   3.867 -    show thesis
   3.868 -    proof (rule that[OF division_ofI])
   3.869 -      have *: "{insert (cbox a b) (q k) |k. k \<in> p} = (\<lambda>k. insert (cbox a b) (q k)) ` p"
   3.870 -        by auto
   3.871 -      show "finite ?D"
   3.872 -        using "*" assm(1) q(1) by auto
   3.873 -      show "\<Union>?D = cbox a b \<union> \<Union>p"
   3.874 -        unfolding * lem1
   3.875 -        unfolding lem2[OF as(1), of "cbox a b", symmetric]
   3.876 -        using q(6)
   3.877 -        by auto
   3.878 -      fix k
   3.879 -      assume k: "k \<in> ?D"
   3.880 -      then show "k \<subseteq> cbox a b \<union> \<Union>p"
   3.881 -        using q(2) by auto
   3.882 -      show "k \<noteq> {}"
   3.883 -        using q(3) k by auto
   3.884 -      show "\<exists>a b. k = cbox a b"
   3.885 -        using q(4) k by auto
   3.886 -      fix k'
   3.887 -      assume k': "k' \<in> ?D" "k \<noteq> k'"
   3.888 -      obtain x where x: "k \<in> insert (cbox a b) (q x)" "x\<in>p"
   3.889 -        using k by auto
   3.890 -      obtain x' where x': "k'\<in>insert (cbox a b) (q x')" "x'\<in>p"
   3.891 -        using k' by auto
   3.892 -      show "interior k \<inter> interior k' = {}"
   3.893 -      proof (cases "x = x'")
   3.894 -        case True
   3.895 -        show ?thesis
   3.896 -          using True k' q(5) x' x by auto
   3.897 -      next
   3.898 -        case False
   3.899 -        {
   3.900 -          presume "k = cbox a b \<Longrightarrow> ?thesis"
   3.901 -            and "k' = cbox a b \<Longrightarrow> ?thesis"
   3.902 -            and "k \<noteq> cbox a b \<Longrightarrow> k' \<noteq> cbox a b \<Longrightarrow> ?thesis"
   3.903 -          then show ?thesis by linarith
   3.904 -        next
   3.905 -          assume as': "k  = cbox a b"
   3.906 -          show ?thesis
   3.907 -            using as' k' q(5) x' by blast
   3.908 -        next
   3.909 -          assume as': "k' = cbox a b"
   3.910 -          show ?thesis
   3.911 -            using as' k'(2) q(5) x by blast
   3.912 -        }
   3.913 -        assume as': "k \<noteq> cbox a b" "k' \<noteq> cbox a b"
   3.914 -        obtain c d where k: "k = cbox c d"
   3.915 -          using q(4)[OF x(2,1)] by blast
   3.916 -        have "interior k \<inter> interior (cbox a b) = {}"
   3.917 -          using as' k'(2) q(5) x by blast
   3.918 -        then have "interior k \<subseteq> interior x"
   3.919 -        using interior_subset_union_intervals
   3.920 -          by (metis as(2) k q(2) x interior_subset_union_intervals)
   3.921 -        moreover
   3.922 -        obtain c d where c_d: "k' = cbox c d"
   3.923 -          using q(4)[OF x'(2,1)] by blast
   3.924 -        have "interior k' \<inter> interior (cbox a b) = {}"
   3.925 -          using as'(2) q(5) x' by blast
   3.926 -        then have "interior k' \<subseteq> interior x'"
   3.927 -          by (metis as(2) c_d interior_subset_union_intervals q(2) x'(1) x'(2))
   3.928 -        ultimately show ?thesis
   3.929 -          using assm(5)[OF x(2) x'(2) False] by auto
   3.930 -      qed
   3.931 -    qed
   3.932 -  }
   3.933 -qed
   3.934 -
   3.935 -lemma elementary_unions_intervals:
   3.936 -  assumes fin: "finite f"
   3.937 -    and "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = cbox a (b::'a::euclidean_space)"
   3.938 -  obtains p where "p division_of (\<Union>f)"
   3.939 -proof -
   3.940 -  have "\<exists>p. p division_of (\<Union>f)"
   3.941 -  proof (induct_tac f rule:finite_subset_induct)
   3.942 -    show "\<exists>p. p division_of \<Union>{}" using elementary_empty by auto
   3.943 -  next
   3.944 -    fix x F
   3.945 -    assume as: "finite F" "x \<notin> F" "\<exists>p. p division_of \<Union>F" "x\<in>f"
   3.946 -    from this(3) obtain p where p: "p division_of \<Union>F" ..
   3.947 -    from assms(2)[OF as(4)] obtain a b where x: "x = cbox a b" by blast
   3.948 -    have *: "\<Union>F = \<Union>p"
   3.949 -      using division_ofD[OF p] by auto
   3.950 -    show "\<exists>p. p division_of \<Union>insert x F"
   3.951 -      using elementary_union_interval[OF p[unfolded *], of a b]
   3.952 -      unfolding Union_insert x * by metis
   3.953 -  qed (insert assms, auto)
   3.954 -  then show ?thesis
   3.955 -    using that by auto
   3.956 -qed
   3.957 -
   3.958 -lemma elementary_union:
   3.959 -  fixes s t :: "'a::euclidean_space set"
   3.960 -  assumes "ps division_of s" "pt division_of t"
   3.961 -  obtains p where "p division_of (s \<union> t)"
   3.962 -proof -
   3.963 -  have *: "s \<union> t = \<Union>ps \<union> \<Union>pt"
   3.964 -    using assms unfolding division_of_def by auto
   3.965 -  show ?thesis
   3.966 -    apply (rule elementary_unions_intervals[of "ps \<union> pt"])
   3.967 -    using assms apply auto
   3.968 -    by (simp add: * that)
   3.969 -qed
   3.970 -
   3.971 -lemma partial_division_extend:
   3.972 -  fixes t :: "'a::euclidean_space set"
   3.973 -  assumes "p division_of s"
   3.974 -    and "q division_of t"
   3.975 -    and "s \<subseteq> t"
   3.976 -  obtains r where "p \<subseteq> r" and "r division_of t"
   3.977 -proof -
   3.978 -  note divp = division_ofD[OF assms(1)] and divq = division_ofD[OF assms(2)]
   3.979 -  obtain a b where ab: "t \<subseteq> cbox a b"
   3.980 -    using elementary_subset_cbox[OF assms(2)] by auto
   3.981 -  obtain r1 where "p \<subseteq> r1" "r1 division_of (cbox a b)"
   3.982 -    using assms
   3.983 -    by (metis ab dual_order.trans partial_division_extend_interval divp(6))
   3.984 -  note r1 = this division_ofD[OF this(2)]
   3.985 -  obtain p' where "p' division_of \<Union>(r1 - p)"
   3.986 -    apply (rule elementary_unions_intervals[of "r1 - p"])
   3.987 -    using r1(3,6)
   3.988 -    apply auto
   3.989 -    done
   3.990 -  then obtain r2 where r2: "r2 division_of (\<Union>(r1 - p)) \<inter> (\<Union>q)"
   3.991 -    by (metis assms(2) divq(6) elementary_inter)
   3.992 -  {
   3.993 -    fix x
   3.994 -    assume x: "x \<in> t" "x \<notin> s"
   3.995 -    then have "x\<in>\<Union>r1"
   3.996 -      unfolding r1 using ab by auto
   3.997 -    then obtain r where r: "r \<in> r1" "x \<in> r"
   3.998 -      unfolding Union_iff ..
   3.999 -    moreover
  3.1000 -    have "r \<notin> p"
  3.1001 -    proof
  3.1002 -      assume "r \<in> p"
  3.1003 -      then have "x \<in> s" using divp(2) r by auto
  3.1004 -      then show False using x by auto
  3.1005 -    qed
  3.1006 -    ultimately have "x\<in>\<Union>(r1 - p)" by auto
  3.1007 -  }
  3.1008 -  then have *: "t = \<Union>p \<union> (\<Union>(r1 - p) \<inter> \<Union>q)"
  3.1009 -    unfolding divp divq using assms(3) by auto
  3.1010 -  show ?thesis
  3.1011 -    apply (rule that[of "p \<union> r2"])
  3.1012 -    unfolding *
  3.1013 -    defer
  3.1014 -    apply (rule division_disjoint_union)
  3.1015 -    unfolding divp(6)
  3.1016 -    apply(rule assms r2)+
  3.1017 -  proof -
  3.1018 -    have "interior s \<inter> interior (\<Union>(r1-p)) = {}"
  3.1019 -    proof (rule inter_interior_unions_intervals)
  3.1020 -      show "finite (r1 - p)" and "open (interior s)" and "\<forall>t\<in>r1-p. \<exists>a b. t = cbox a b"
  3.1021 -        using r1 by auto
  3.1022 -      have *: "\<And>s. (\<And>x. x \<in> s \<Longrightarrow> False) \<Longrightarrow> s = {}"
  3.1023 -        by auto
  3.1024 -      show "\<forall>t\<in>r1-p. interior s \<inter> interior t = {}"
  3.1025 -      proof
  3.1026 -        fix m x
  3.1027 -        assume as: "m \<in> r1 - p"
  3.1028 -        have "interior m \<inter> interior (\<Union>p) = {}"
  3.1029 -        proof (rule inter_interior_unions_intervals)
  3.1030 -          show "finite p" and "open (interior m)" and "\<forall>t\<in>p. \<exists>a b. t = cbox a b"
  3.1031 -            using divp by auto
  3.1032 -          show "\<forall>t\<in>p. interior m \<inter> interior t = {}"
  3.1033 -            by (metis DiffD1 DiffD2 as r1(1) r1(7) set_rev_mp)
  3.1034 -        qed
  3.1035 -        then show "interior s \<inter> interior m = {}"
  3.1036 -          unfolding divp by auto
  3.1037 -      qed
  3.1038 -    qed
  3.1039 -    then show "interior s \<inter> interior (\<Union>(r1-p) \<inter> (\<Union>q)) = {}"
  3.1040 -      using interior_subset by auto
  3.1041 -  qed auto
  3.1042 -qed
  3.1043 -
  3.1044 -lemma division_split_left_inj:
  3.1045 -  fixes type :: "'a::euclidean_space"
  3.1046 -  assumes "d division_of i"
  3.1047 -    and "k1 \<in> d"
  3.1048 -    and "k2 \<in> d"
  3.1049 -    and "k1 \<noteq> k2"
  3.1050 -    and "k1 \<inter> {x::'a. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c}"
  3.1051 -    and k: "k\<in>Basis"
  3.1052 -  shows "content(k1 \<inter> {x. x\<bullet>k \<le> c}) = 0"
  3.1053 -proof -
  3.1054 -  note d=division_ofD[OF assms(1)]
  3.1055 -  have *: "\<And>(a::'a) b c. content (cbox a b \<inter> {x. x\<bullet>k \<le> c}) = 0 \<longleftrightarrow>
  3.1056 -    interior(cbox a b \<inter> {x. x\<bullet>k \<le> c}) = {}"
  3.1057 -    unfolding  interval_split[OF k] content_eq_0_interior by auto
  3.1058 -  guess u1 v1 using d(4)[OF assms(2)] by (elim exE) note uv1=this
  3.1059 -  guess u2 v2 using d(4)[OF assms(3)] by (elim exE) note uv2=this
  3.1060 -  have **: "\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}"
  3.1061 -    by auto
  3.1062 -  show ?thesis
  3.1063 -    unfolding uv1 uv2 *
  3.1064 -    apply (rule **[OF d(5)[OF assms(2-4)]])
  3.1065 -    apply (simp add: uv1)
  3.1066 -    using assms(5) uv1 by auto
  3.1067 -qed
  3.1068 -
  3.1069 -lemma division_split_right_inj:
  3.1070 -  fixes type :: "'a::euclidean_space"
  3.1071 -  assumes "d division_of i"
  3.1072 -    and "k1 \<in> d"
  3.1073 -    and "k2 \<in> d"
  3.1074 -    and "k1 \<noteq> k2"
  3.1075 -    and "k1 \<inter> {x::'a. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c}"
  3.1076 -    and k: "k \<in> Basis"
  3.1077 -  shows "content (k1 \<inter> {x. x\<bullet>k \<ge> c}) = 0"
  3.1078 -proof -
  3.1079 -  note d=division_ofD[OF assms(1)]
  3.1080 -  have *: "\<And>a b::'a. \<And>c. content(cbox a b \<inter> {x. x\<bullet>k \<ge> c}) = 0 \<longleftrightarrow>
  3.1081 -    interior(cbox a b \<inter> {x. x\<bullet>k \<ge> c}) = {}"
  3.1082 -    unfolding interval_split[OF k] content_eq_0_interior by auto
  3.1083 -  guess u1 v1 using d(4)[OF assms(2)] by (elim exE) note uv1=this
  3.1084 -  guess u2 v2 using d(4)[OF assms(3)] by (elim exE) note uv2=this
  3.1085 -  have **: "\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}"
  3.1086 -    by auto
  3.1087 -  show ?thesis
  3.1088 -    unfolding uv1 uv2 *
  3.1089 -    apply (rule **[OF d(5)[OF assms(2-4)]])
  3.1090 -    apply (simp add: uv1)
  3.1091 -    using assms(5) uv1 by auto
  3.1092 +  finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
  3.1093  qed
  3.1094  
  3.1095 -
  3.1096 -lemma division_split:
  3.1097 -  fixes a :: "'a::euclidean_space"
  3.1098 -  assumes "p division_of (cbox a b)"
  3.1099 -    and k: "k\<in>Basis"
  3.1100 -  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})"
  3.1101 -      (is "?p1 division_of ?I1")
  3.1102 -    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})"
  3.1103 -      (is "?p2 division_of ?I2")
  3.1104 -proof (rule_tac[!] division_ofI)
  3.1105 -  note p = division_ofD[OF assms(1)]
  3.1106 -  show "finite ?p1" "finite ?p2"
  3.1107 -    using p(1) by auto
  3.1108 -  show "\<Union>?p1 = ?I1" "\<Union>?p2 = ?I2"
  3.1109 -    unfolding p(6)[symmetric] by auto
  3.1110 -  {
  3.1111 -    fix k
  3.1112 -    assume "k \<in> ?p1"
  3.1113 -    then guess l unfolding mem_Collect_eq by (elim exE conjE) note l=this
  3.1114 -    guess u v using p(4)[OF l(2)] by (elim exE) note uv=this
  3.1115 -    show "k \<subseteq> ?I1"
  3.1116 -      using l p(2) uv by force
  3.1117 -    show  "k \<noteq> {}"
  3.1118 -      by (simp add: l)
  3.1119 -    show  "\<exists>a b. k = cbox a b"
  3.1120 -      apply (simp add: l uv p(2-3)[OF l(2)])
  3.1121 -      apply (subst interval_split[OF k])
  3.1122 -      apply (auto intro: order.trans)
  3.1123 -      done
  3.1124 -    fix k'
  3.1125 -    assume "k' \<in> ?p1"
  3.1126 -    then guess l' unfolding mem_Collect_eq by (elim exE conjE) note l'=this
  3.1127 -    assume "k \<noteq> k'"
  3.1128 -    then show "interior k \<inter> interior k' = {}"
  3.1129 -      unfolding l l' using p(5)[OF l(2) l'(2)] by auto
  3.1130 -  }
  3.1131 -  {
  3.1132 -    fix k
  3.1133 -    assume "k \<in> ?p2"
  3.1134 -    then guess l unfolding mem_Collect_eq by (elim exE conjE) note l=this
  3.1135 -    guess u v using p(4)[OF l(2)] by (elim exE) note uv=this
  3.1136 -    show "k \<subseteq> ?I2"
  3.1137 -      using l p(2) uv by force
  3.1138 -    show  "k \<noteq> {}"
  3.1139 -      by (simp add: l)
  3.1140 -    show  "\<exists>a b. k = cbox a b"
  3.1141 -      apply (simp add: l uv p(2-3)[OF l(2)])
  3.1142 -      apply (subst interval_split[OF k])
  3.1143 -      apply (auto intro: order.trans)
  3.1144 -      done
  3.1145 -    fix k'
  3.1146 -    assume "k' \<in> ?p2"
  3.1147 -    then guess l' unfolding mem_Collect_eq by (elim exE conjE) note l'=this
  3.1148 -    assume "k \<noteq> k'"
  3.1149 -    then show "interior k \<inter> interior k' = {}"
  3.1150 -      unfolding l l' using p(5)[OF l(2) l'(2)] by auto
  3.1151 -  }
  3.1152 -qed
  3.1153 -
  3.1154 -subsection \<open>Tagged (partial) divisions.\<close>
  3.1155 -
  3.1156 -definition tagged_partial_division_of (infixr "tagged'_partial'_division'_of" 40)
  3.1157 -  where "s tagged_partial_division_of i \<longleftrightarrow>
  3.1158 -    finite s \<and>
  3.1159 -    (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
  3.1160 -    (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
  3.1161 -      interior k1 \<inter> interior k2 = {})"
  3.1162 -
  3.1163 -lemma tagged_partial_division_ofD[dest]:
  3.1164 -  assumes "s tagged_partial_division_of i"
  3.1165 -  shows "finite s"
  3.1166 -    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  3.1167 -    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  3.1168 -    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  3.1169 -    and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow>
  3.1170 -      (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
  3.1171 -  using assms unfolding tagged_partial_division_of_def by blast+
  3.1172 -
  3.1173 -definition tagged_division_of (infixr "tagged'_division'_of" 40)
  3.1174 -  where "s tagged_division_of i \<longleftrightarrow> s tagged_partial_division_of i \<and> (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  3.1175 -
  3.1176 -lemma tagged_division_of_finite: "s tagged_division_of i \<Longrightarrow> finite s"
  3.1177 -  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  3.1178 -
  3.1179 -lemma tagged_division_of:
  3.1180 -  "s tagged_division_of i \<longleftrightarrow>
  3.1181 -    finite s \<and>
  3.1182 -    (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
  3.1183 -    (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
  3.1184 -      interior k1 \<inter> interior k2 = {}) \<and>
  3.1185 -    (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  3.1186 -  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  3.1187 -
  3.1188 -lemma tagged_division_ofI:
  3.1189 -  assumes "finite s"
  3.1190 -    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  3.1191 -    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  3.1192 -    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  3.1193 -    and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
  3.1194 -      interior k1 \<inter> interior k2 = {}"
  3.1195 -    and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  3.1196 -  shows "s tagged_division_of i"
  3.1197 -  unfolding tagged_division_of
  3.1198 -  using assms
  3.1199 -  apply auto
  3.1200 -  apply fastforce+
  3.1201 -  done
  3.1202 -
  3.1203 -lemma tagged_division_ofD[dest]:  (*FIXME USE A LOCALE*)
  3.1204 -  assumes "s tagged_division_of i"
  3.1205 -  shows "finite s"
  3.1206 -    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  3.1207 -    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  3.1208 -    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  3.1209 -    and "\<And>x1 k1 x2 k2. (x1, k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
  3.1210 -      interior k1 \<inter> interior k2 = {}"
  3.1211 -    and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  3.1212 -  using assms unfolding tagged_division_of by blast+
  3.1213 -
  3.1214 -lemma division_of_tagged_division:
  3.1215 -  assumes "s tagged_division_of i"
  3.1216 -  shows "(snd ` s) division_of i"
  3.1217 -proof (rule division_ofI)
  3.1218 -  note assm = tagged_division_ofD[OF assms]
  3.1219 -  show "\<Union>(snd ` s) = i" "finite (snd ` s)"
  3.1220 -    using assm by auto
  3.1221 -  fix k
  3.1222 -  assume k: "k \<in> snd ` s"
  3.1223 -  then obtain xk where xk: "(xk, k) \<in> s"
  3.1224 -    by auto
  3.1225 -  then show "k \<subseteq> i" "k \<noteq> {}" "\<exists>a b. k = cbox a b"
  3.1226 -    using assm by fastforce+
  3.1227 -  fix k'
  3.1228 -  assume k': "k' \<in> snd ` s" "k \<noteq> k'"
  3.1229 -  from this(1) obtain xk' where xk': "(xk', k') \<in> s"
  3.1230 -    by auto
  3.1231 -  then show "interior k \<inter> interior k' = {}"
  3.1232 -    using assm(5) k'(2) xk by blast
  3.1233 -qed
  3.1234 -
  3.1235 -lemma partial_division_of_tagged_division:
  3.1236 -  assumes "s tagged_partial_division_of i"
  3.1237 -  shows "(snd ` s) division_of \<Union>(snd ` s)"
  3.1238 -proof (rule division_ofI)
  3.1239 -  note assm = tagged_partial_division_ofD[OF assms]
  3.1240 -  show "finite (snd ` s)" "\<Union>(snd ` s) = \<Union>(snd ` s)"
  3.1241 -    using assm by auto
  3.1242 -  fix k
  3.1243 -  assume k: "k \<in> snd ` s"
  3.1244 -  then obtain xk where xk: "(xk, k) \<in> s"
  3.1245 -    by auto
  3.1246 -  then show "k \<noteq> {}" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>(snd ` s)"
  3.1247 -    using assm by auto
  3.1248 -  fix k'
  3.1249 -  assume k': "k' \<in> snd ` s" "k \<noteq> k'"
  3.1250 -  from this(1) obtain xk' where xk': "(xk', k') \<in> s"
  3.1251 -    by auto
  3.1252 -  then show "interior k \<inter> interior k' = {}"
  3.1253 -    using assm(5) k'(2) xk by auto
  3.1254 -qed
  3.1255 -
  3.1256 -lemma tagged_partial_division_subset:
  3.1257 -  assumes "s tagged_partial_division_of i"
  3.1258 -    and "t \<subseteq> s"
  3.1259 -  shows "t tagged_partial_division_of i"
  3.1260 -  using assms
  3.1261 -  unfolding tagged_partial_division_of_def
  3.1262 -  using finite_subset[OF assms(2)]
  3.1263 -  by blast
  3.1264 -
  3.1265 -lemma (in comm_monoid_set) over_tagged_division_lemma:
  3.1266 -  assumes "p tagged_division_of i"
  3.1267 -    and "\<And>u v. cbox u v \<noteq> {} \<Longrightarrow> content (cbox u v) = 0 \<Longrightarrow> d (cbox u v) = \<^bold>1"
  3.1268 -  shows "F (\<lambda>(x,k). d k) p = F d (snd ` p)"
  3.1269 -proof -
  3.1270 -  have *: "(\<lambda>(x,k). d k) = d \<circ> snd"
  3.1271 -    unfolding o_def by (rule ext) auto
  3.1272 -  note assm = tagged_division_ofD[OF assms(1)]
  3.1273 -  show ?thesis
  3.1274 -    unfolding *
  3.1275 -  proof (rule reindex_nontrivial[symmetric])
  3.1276 -    show "finite p"
  3.1277 -      using assm by auto
  3.1278 -    fix x y
  3.1279 -    assume "x\<in>p" "y\<in>p" "x\<noteq>y" "snd x = snd y"
  3.1280 -    obtain a b where ab: "snd x = cbox a b"
  3.1281 -      using assm(4)[of "fst x" "snd x"] \<open>x\<in>p\<close> by auto
  3.1282 -    have "(fst x, snd y) \<in> p" "(fst x, snd y) \<noteq> y"
  3.1283 -      by (metis prod.collapse \<open>x\<in>p\<close> \<open>snd x = snd y\<close> \<open>x \<noteq> y\<close>)+
  3.1284 -    with \<open>x\<in>p\<close> \<open>y\<in>p\<close> have "interior (snd x) \<inter> interior (snd y) = {}"
  3.1285 -      by (intro assm(5)[of "fst x" _ "fst y"]) auto
  3.1286 -    then have "content (cbox a b) = 0"
  3.1287 -      unfolding \<open>snd x = snd y\<close>[symmetric] ab content_eq_0_interior by auto
  3.1288 -    then have "d (cbox a b) = \<^bold>1"
  3.1289 -      using assm(2)[of "fst x" "snd x"] \<open>x\<in>p\<close> ab[symmetric] by (intro assms(2)) auto
  3.1290 -    then show "d (snd x) = \<^bold>1"
  3.1291 -      unfolding ab by auto
  3.1292 -  qed
  3.1293 -qed
  3.1294 -
  3.1295 -lemma tag_in_interval: "p tagged_division_of i \<Longrightarrow> (x, k) \<in> p \<Longrightarrow> x \<in> i"
  3.1296 -  by auto
  3.1297 -
  3.1298 -lemma tagged_division_of_empty: "{} tagged_division_of {}"
  3.1299 -  unfolding tagged_division_of by auto
  3.1300 -
  3.1301 -lemma tagged_partial_division_of_trivial[simp]: "p tagged_partial_division_of {} \<longleftrightarrow> p = {}"
  3.1302 -  unfolding tagged_partial_division_of_def by auto
  3.1303 -
  3.1304 -lemma tagged_division_of_trivial[simp]: "p tagged_division_of {} \<longleftrightarrow> p = {}"
  3.1305 -  unfolding tagged_division_of by auto
  3.1306 -
  3.1307 -lemma tagged_division_of_self: "x \<in> cbox a b \<Longrightarrow> {(x,cbox a b)} tagged_division_of (cbox a b)"
  3.1308 -  by (rule tagged_division_ofI) auto
  3.1309 -
  3.1310 -lemma tagged_division_of_self_real: "x \<in> {a .. b::real} \<Longrightarrow> {(x,{a .. b})} tagged_division_of {a .. b}"
  3.1311 -  unfolding box_real[symmetric]
  3.1312 -  by (rule tagged_division_of_self)
  3.1313 -
  3.1314 -lemma tagged_division_union:
  3.1315 -  assumes "p1 tagged_division_of s1"
  3.1316 -    and "p2 tagged_division_of s2"
  3.1317 -    and "interior s1 \<inter> interior s2 = {}"
  3.1318 -  shows "(p1 \<union> p2) tagged_division_of (s1 \<union> s2)"
  3.1319 -proof (rule tagged_division_ofI)
  3.1320 -  note p1 = tagged_division_ofD[OF assms(1)]
  3.1321 -  note p2 = tagged_division_ofD[OF assms(2)]
  3.1322 -  show "finite (p1 \<union> p2)"
  3.1323 -    using p1(1) p2(1) by auto
  3.1324 -  show "\<Union>{k. \<exists>x. (x, k) \<in> p1 \<union> p2} = s1 \<union> s2"
  3.1325 -    using p1(6) p2(6) by blast
  3.1326 -  fix x k
  3.1327 -  assume xk: "(x, k) \<in> p1 \<union> p2"
  3.1328 -  show "x \<in> k" "\<exists>a b. k = cbox a b"
  3.1329 -    using xk p1(2,4) p2(2,4) by auto
  3.1330 -  show "k \<subseteq> s1 \<union> s2"
  3.1331 -    using xk p1(3) p2(3) by blast
  3.1332 -  fix x' k'
  3.1333 -  assume xk': "(x', k') \<in> p1 \<union> p2" "(x, k) \<noteq> (x', k')"
  3.1334 -  have *: "\<And>a b. a \<subseteq> s1 \<Longrightarrow> b \<subseteq> s2 \<Longrightarrow> interior a \<inter> interior b = {}"
  3.1335 -    using assms(3) interior_mono by blast
  3.1336 -  show "interior k \<inter> interior k' = {}"
  3.1337 -    apply (cases "(x, k) \<in> p1")
  3.1338 -    apply (meson "*" UnE assms(1) assms(2) p1(5) tagged_division_ofD(3) xk'(1) xk'(2))
  3.1339 -    by (metis "*" UnE assms(1) assms(2) inf_sup_aci(1) p2(5) tagged_division_ofD(3) xk xk'(1) xk'(2))
  3.1340 -qed
  3.1341 -
  3.1342 -lemma tagged_division_unions:
  3.1343 -  assumes "finite iset"
  3.1344 -    and "\<forall>i\<in>iset. pfn i tagged_division_of i"
  3.1345 -    and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior(i1) \<inter> interior(i2) = {}"
  3.1346 -  shows "\<Union>(pfn ` iset) tagged_division_of (\<Union>iset)"
  3.1347 -proof (rule tagged_division_ofI)
  3.1348 -  note assm = tagged_division_ofD[OF assms(2)[rule_format]]
  3.1349 -  show "finite (\<Union>(pfn ` iset))"
  3.1350 -    using assms by auto
  3.1351 -  have "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>((\<lambda>i. \<Union>{k. \<exists>x. (x, k) \<in> pfn i}) ` iset)"
  3.1352 -    by blast
  3.1353 -  also have "\<dots> = \<Union>iset"
  3.1354 -    using assm(6) by auto
  3.1355 -  finally show "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>iset" .
  3.1356 -  fix x k
  3.1357 -  assume xk: "(x, k) \<in> \<Union>(pfn ` iset)"
  3.1358 -  then obtain i where i: "i \<in> iset" "(x, k) \<in> pfn i"
  3.1359 -    by auto
  3.1360 -  show "x \<in> k" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>iset"
  3.1361 -    using assm(2-4)[OF i] using i(1) by auto
  3.1362 -  fix x' k'
  3.1363 -  assume xk': "(x', k') \<in> \<Union>(pfn ` iset)" "(x, k) \<noteq> (x', k')"
  3.1364 -  then obtain i' where i': "i' \<in> iset" "(x', k') \<in> pfn i'"
  3.1365 -    by auto
  3.1366 -  have *: "\<And>a b. i \<noteq> i' \<Longrightarrow> a \<subseteq> i \<Longrightarrow> b \<subseteq> i' \<Longrightarrow> interior a \<inter> interior b = {}"
  3.1367 -    using i(1) i'(1)
  3.1368 -    using assms(3)[rule_format] interior_mono
  3.1369 -    by blast
  3.1370 -  show "interior k \<inter> interior k' = {}"
  3.1371 -    apply (cases "i = i'")
  3.1372 -    using assm(5) i' i(2) xk'(2) apply blast
  3.1373 -    using "*" assm(3) i' i by auto
  3.1374 -qed
  3.1375 -
  3.1376 -lemma tagged_partial_division_of_union_self:
  3.1377 -  assumes "p tagged_partial_division_of s"
  3.1378 -  shows "p tagged_division_of (\<Union>(snd ` p))"
  3.1379 -  apply (rule tagged_division_ofI)
  3.1380 -  using tagged_partial_division_ofD[OF assms]
  3.1381 -  apply auto
  3.1382 -  done
  3.1383 -
  3.1384 -lemma tagged_division_of_union_self:
  3.1385 -  assumes "p tagged_division_of s"
  3.1386 -  shows "p tagged_division_of (\<Union>(snd ` p))"
  3.1387 -  apply (rule tagged_division_ofI)
  3.1388 -  using tagged_division_ofD[OF assms]
  3.1389 -  apply auto
  3.1390 -  done
  3.1391 -
  3.1392 -subsection \<open>Functions closed on boxes: morphisms from boxes to monoids\<close>
  3.1393 -
  3.1394 -text \<open>This auxiliary structure is used to sum up over the elements of a division. Main theorem is
  3.1395 -  @{text operative_division}. Instances for the monoid are @{typ "'a option"}, @{typ real}, and
  3.1396 -  @{typ bool}.\<close>
  3.1397 -
  3.1398 -lemma property_empty_interval: "\<forall>a b. content (cbox a b) = 0 \<longrightarrow> P (cbox a b) \<Longrightarrow> P {}"
  3.1399 -  using content_empty unfolding empty_as_interval by auto
  3.1400 -
  3.1401 -paragraph \<open>Using additivity of lifted function to encode definedness.\<close>
  3.1402 -
  3.1403 -definition lift_option :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a option \<Rightarrow> 'b option \<Rightarrow> 'c option"
  3.1404 -where
  3.1405 -  "lift_option f a' b' = Option.bind a' (\<lambda>a. Option.bind b' (\<lambda>b. Some (f a b)))"
  3.1406 -
  3.1407 -lemma lift_option_simps[simp]:
  3.1408 -  "lift_option f (Some a) (Some b) = Some (f a b)"
  3.1409 -  "lift_option f None b' = None"
  3.1410 -  "lift_option f a' None = None"
  3.1411 -  by (auto simp: lift_option_def)
  3.1412 -
  3.1413 -lemma comm_monoid_lift_option:
  3.1414 -  assumes "comm_monoid f z"
  3.1415 -  shows "comm_monoid (lift_option f) (Some z)"
  3.1416 -proof -
  3.1417 -  from assms interpret comm_monoid f z .
  3.1418 -  show ?thesis
  3.1419 -    by standard (auto simp: lift_option_def ac_simps split: bind_split)
  3.1420 -qed
  3.1421 -
  3.1422 -lemma comm_monoid_and: "comm_monoid HOL.conj True"
  3.1423 -  by standard auto
  3.1424 -
  3.1425 -lemma comm_monoid_set_and: "comm_monoid_set HOL.conj True"
  3.1426 -  by (rule comm_monoid_set.intro) (fact comm_monoid_and)
  3.1427 -
  3.1428 -paragraph \<open>Operative\<close>
  3.1429 -
  3.1430 -definition (in comm_monoid) operative :: "('b::euclidean_space set \<Rightarrow> 'a) \<Rightarrow> bool"
  3.1431 -  where "operative g \<longleftrightarrow>
  3.1432 -    (\<forall>a b. content (cbox a b) = 0 \<longrightarrow> g (cbox a b) = \<^bold>1) \<and>
  3.1433 -    (\<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}))"
  3.1434 -
  3.1435 -lemma (in comm_monoid) operativeD[dest]:
  3.1436 -  assumes "operative g"
  3.1437 -  shows "\<And>a b. content (cbox a b) = 0 \<Longrightarrow> g (cbox a b) = \<^bold>1"
  3.1438 -    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})"
  3.1439 -  using assms unfolding operative_def by auto
  3.1440 -
  3.1441 -lemma (in comm_monoid) operative_empty: "operative g \<Longrightarrow> g {} = \<^bold>1"
  3.1442 -  unfolding operative_def by (rule property_empty_interval) auto
  3.1443 -
  3.1444  lemma operative_content[intro]: "add.operative content"
  3.1445 -  by (force simp add: add.operative_def content_split[symmetric])
  3.1446 -
  3.1447 -definition "division_points (k::('a::euclidean_space) set) d =
  3.1448 -   {(j,x). j \<in> Basis \<and> (interval_lowerbound k)\<bullet>j < x \<and> x < (interval_upperbound k)\<bullet>j \<and>
  3.1449 -     (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  3.1450 -
  3.1451 -lemma division_points_finite:
  3.1452 -  fixes i :: "'a::euclidean_space set"
  3.1453 -  assumes "d division_of i"
  3.1454 -  shows "finite (division_points i d)"
  3.1455 -proof -
  3.1456 -  note assm = division_ofD[OF assms]
  3.1457 -  let ?M = "\<lambda>j. {(j,x)|x. (interval_lowerbound i)\<bullet>j < x \<and> x < (interval_upperbound i)\<bullet>j \<and>
  3.1458 -    (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  3.1459 -  have *: "division_points i d = \<Union>(?M ` Basis)"
  3.1460 -    unfolding division_points_def by auto
  3.1461 -  show ?thesis
  3.1462 -    unfolding * using assm by auto
  3.1463 -qed
  3.1464 -
  3.1465 -lemma division_points_subset:
  3.1466 -  fixes a :: "'a::euclidean_space"
  3.1467 -  assumes "d division_of (cbox a b)"
  3.1468 -    and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  3.1469 -    and k: "k \<in> Basis"
  3.1470 -  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>
  3.1471 -      division_points (cbox a b) d" (is ?t1)
  3.1472 -    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>
  3.1473 -      division_points (cbox a b) d" (is ?t2)
  3.1474 -proof -
  3.1475 -  note assm = division_ofD[OF assms(1)]
  3.1476 -  have *: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  3.1477 -    "\<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"
  3.1478 -    "\<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"
  3.1479 -    "min (b \<bullet> k) c = c" "max (a \<bullet> k) c = c"
  3.1480 -    using assms using less_imp_le by auto
  3.1481 -  show ?t1 (*FIXME a horrible mess*)
  3.1482 -    unfolding division_points_def interval_split[OF k, of a b]
  3.1483 -    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  3.1484 -    unfolding *
  3.1485 -    apply (rule subsetI)
  3.1486 -    unfolding mem_Collect_eq split_beta
  3.1487 -    apply (erule bexE conjE)+
  3.1488 -    apply (simp add: )
  3.1489 -    apply (erule exE conjE)+
  3.1490 -  proof
  3.1491 -    fix i l x
  3.1492 -    assume as:
  3.1493 -      "a \<bullet> fst x < snd x" "snd x < (if fst x = k then c else b \<bullet> fst x)"
  3.1494 -      "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  3.1495 -      "i = l \<inter> {x. x \<bullet> k \<le> c}" "l \<in> d" "l \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}"
  3.1496 -      and fstx: "fst x \<in> Basis"
  3.1497 -    from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  3.1498 -    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"
  3.1499 -      using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  3.1500 -    have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  3.1501 -      using l using as(6) unfolding box_ne_empty[symmetric] by auto
  3.1502 -    show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  3.1503 -      apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  3.1504 -      using as(1-3,5) fstx
  3.1505 -      unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  3.1506 -      apply (auto split: if_split_asm)
  3.1507 -      done
  3.1508 -    show "snd x < b \<bullet> fst x"
  3.1509 -      using as(2) \<open>c < b\<bullet>k\<close> by (auto split: if_split_asm)
  3.1510 -  qed
  3.1511 -  show ?t2
  3.1512 -    unfolding division_points_def interval_split[OF k, of a b]
  3.1513 -    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  3.1514 -    unfolding *
  3.1515 -    unfolding subset_eq
  3.1516 -    apply rule
  3.1517 -    unfolding mem_Collect_eq split_beta
  3.1518 -    apply (erule bexE conjE)+
  3.1519 -    apply (simp only: mem_Collect_eq inner_setsum_left_Basis simp_thms)
  3.1520 -    apply (erule exE conjE)+
  3.1521 -  proof
  3.1522 -    fix i l x
  3.1523 -    assume as:
  3.1524 -      "(if fst x = k then c else a \<bullet> fst x) < snd x" "snd x < b \<bullet> fst x"
  3.1525 -      "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  3.1526 -      "i = l \<inter> {x. c \<le> x \<bullet> k}" "l \<in> d" "l \<inter> {x. c \<le> x \<bullet> k} \<noteq> {}"
  3.1527 -      and fstx: "fst x \<in> Basis"
  3.1528 -    from assm(4)[OF this(5)] guess u v by (elim exE) note l=this
  3.1529 -    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"
  3.1530 -      using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  3.1531 -    have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  3.1532 -      using l using as(6) unfolding box_ne_empty[symmetric] by auto
  3.1533 -    show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  3.1534 -      apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  3.1535 -      using as(1-3,5) fstx
  3.1536 -      unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  3.1537 -      apply (auto split: if_split_asm)
  3.1538 -      done
  3.1539 -    show "a \<bullet> fst x < snd x"
  3.1540 -      using as(1) \<open>a\<bullet>k < c\<close> by (auto split: if_split_asm)
  3.1541 -   qed
  3.1542 -qed
  3.1543 -
  3.1544 -lemma division_points_psubset:
  3.1545 -  fixes a :: "'a::euclidean_space"
  3.1546 -  assumes "d division_of (cbox a b)"
  3.1547 -      and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  3.1548 -      and "l \<in> d"
  3.1549 -      and "interval_lowerbound l\<bullet>k = c \<or> interval_upperbound l\<bullet>k = c"
  3.1550 -      and k: "k \<in> Basis"
  3.1551 -  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>
  3.1552 -         division_points (cbox a b) d" (is "?D1 \<subset> ?D")
  3.1553 -    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>
  3.1554 -         division_points (cbox a b) d" (is "?D2 \<subset> ?D")
  3.1555 -proof -
  3.1556 -  have ab: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  3.1557 -    using assms(2) by (auto intro!:less_imp_le)
  3.1558 -  guess u v using division_ofD(4)[OF assms(1,5)] by (elim exE) note l=this
  3.1559 -  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"
  3.1560 -    using division_ofD(2,2,3)[OF assms(1,5)] unfolding l box_ne_empty
  3.1561 -    using subset_box(1)
  3.1562 -    apply auto
  3.1563 -    apply blast+
  3.1564 -    done
  3.1565 -  have *: "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  3.1566 -          "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  3.1567 -    unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  3.1568 -    using uv[rule_format, of k] ab k
  3.1569 -    by auto
  3.1570 -  have "\<exists>x. x \<in> ?D - ?D1"
  3.1571 -    using assms(3-)
  3.1572 -    unfolding division_points_def interval_bounds[OF ab]
  3.1573 -    apply -
  3.1574 -    apply (erule disjE)
  3.1575 -    apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  3.1576 -    apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  3.1577 -    done
  3.1578 -  moreover have "?D1 \<subseteq> ?D"
  3.1579 -    by (auto simp add: assms division_points_subset)
  3.1580 -  ultimately show "?D1 \<subset> ?D"
  3.1581 -    by blast
  3.1582 -  have *: "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  3.1583 -    "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  3.1584 -    unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  3.1585 -    using uv[rule_format, of k] ab k
  3.1586 -    by auto
  3.1587 -  have "\<exists>x. x \<in> ?D - ?D2"
  3.1588 -    using assms(3-)
  3.1589 -    unfolding division_points_def interval_bounds[OF ab]
  3.1590 -    apply -
  3.1591 -    apply (erule disjE)
  3.1592 -    apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  3.1593 -    apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  3.1594 -    done
  3.1595 -  moreover have "?D2 \<subseteq> ?D"
  3.1596 -    by (auto simp add: assms division_points_subset)
  3.1597 -  ultimately show "?D2 \<subset> ?D"
  3.1598 -    by blast
  3.1599 -qed
  3.1600 -
  3.1601 -lemma (in comm_monoid_set) operative_division:
  3.1602 -  fixes g :: "'b::euclidean_space set \<Rightarrow> 'a"
  3.1603 -  assumes g: "operative g" and d: "d division_of (cbox a b)" shows "F g d = g (cbox a b)"
  3.1604 -proof -
  3.1605 -  define C where [abs_def]: "C = card (division_points (cbox a b) d)"
  3.1606 -  then show ?thesis
  3.1607 -    using d
  3.1608 -  proof (induction C arbitrary: a b d rule: less_induct)
  3.1609 -    case (less a b d)
  3.1610 -    show ?case
  3.1611 -    proof cases
  3.1612 -      show "content (cbox a b) = 0 \<Longrightarrow> F g d = g (cbox a b)"
  3.1613 -        using division_of_content_0[OF _ less.prems] operativeD(1)[OF  g] division_ofD(4)[OF less.prems]
  3.1614 -        by (fastforce intro!: neutral)
  3.1615 -    next
  3.1616 -      assume "content (cbox a b) \<noteq> 0"
  3.1617 -      note ab = this[unfolded content_lt_nz[symmetric] content_pos_lt_eq]
  3.1618 -      then have ab': "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  3.1619 -        by (auto intro!: less_imp_le)
  3.1620 -      show "F g d = g (cbox a b)"
  3.1621 -      proof (cases "division_points (cbox a b) d = {}")
  3.1622 -        case True
  3.1623 -        { fix u v and j :: 'b
  3.1624 -          assume j: "j \<in> Basis" and as: "cbox u v \<in> d"
  3.1625 -          then have "cbox u v \<noteq> {}"
  3.1626 -            using less.prems by blast
  3.1627 -          then have uv: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i" "u\<bullet>j \<le> v\<bullet>j"
  3.1628 -            using j unfolding box_ne_empty by auto
  3.1629 -          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)"
  3.1630 -            using as j by auto
  3.1631 -          have "(j, u\<bullet>j) \<notin> division_points (cbox a b) d"
  3.1632 -               "(j, v\<bullet>j) \<notin> division_points (cbox a b) d" using True by auto
  3.1633 -          note this[unfolded de_Morgan_conj division_points_def mem_Collect_eq split_conv interval_bounds[OF ab'] bex_simps]
  3.1634 -          note *[OF this(1)] *[OF this(2)] note this[unfolded interval_bounds[OF uv(1)]]
  3.1635 -          moreover
  3.1636 -          have "a\<bullet>j \<le> u\<bullet>j" "v\<bullet>j \<le> b\<bullet>j"
  3.1637 -            using division_ofD(2,2,3)[OF \<open>d division_of cbox a b\<close> as]
  3.1638 -            apply (metis j subset_box(1) uv(1))
  3.1639 -            by (metis \<open>cbox u v \<subseteq> cbox a b\<close> j subset_box(1) uv(1))
  3.1640 -          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"
  3.1641 -            unfolding not_less de_Morgan_disj using ab[rule_format,of j] uv(2) j by force }
  3.1642 -        then have d': "\<forall>i\<in>d. \<exists>u v. i = cbox u v \<and>
  3.1643 -          (\<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)"
  3.1644 -          unfolding forall_in_division[OF less.prems] by blast
  3.1645 -        have "(1/2) *\<^sub>R (a+b) \<in> cbox a b"
  3.1646 -          unfolding mem_box using ab by(auto intro!: less_imp_le simp: inner_simps)
  3.1647 -        note this[unfolded division_ofD(6)[OF \<open>d division_of cbox a b\<close>,symmetric] Union_iff]
  3.1648 -        then guess i .. note i=this
  3.1649 -        guess u v using d'[rule_format,OF i(1)] by (elim exE conjE) note uv=this
  3.1650 -        have "cbox a b \<in> d"
  3.1651 -        proof -
  3.1652 -          have "u = a" "v = b"
  3.1653 -            unfolding euclidean_eq_iff[where 'a='b]
  3.1654 -          proof safe
  3.1655 -            fix j :: 'b
  3.1656 -            assume j: "j \<in> Basis"
  3.1657 -            note i(2)[unfolded uv mem_box,rule_format,of j]
  3.1658 -            then show "u \<bullet> j = a \<bullet> j" and "v \<bullet> j = b \<bullet> j"
  3.1659 -              using uv(2)[rule_format,of j] j by (auto simp: inner_simps)
  3.1660 -          qed
  3.1661 -          then have "i = cbox a b" using uv by auto
  3.1662 -          then show ?thesis using i by auto
  3.1663 -        qed
  3.1664 -        then have deq: "d = insert (cbox a b) (d - {cbox a b})"
  3.1665 -          by auto
  3.1666 -        have "F g (d - {cbox a b}) = \<^bold>1"
  3.1667 -        proof (intro neutral ballI)
  3.1668 -          fix x
  3.1669 -          assume x: "x \<in> d - {cbox a b}"
  3.1670 -          then have "x\<in>d"
  3.1671 -            by auto note d'[rule_format,OF this]
  3.1672 -          then guess u v by (elim exE conjE) note uv=this
  3.1673 -          have "u \<noteq> a \<or> v \<noteq> b"
  3.1674 -            using x[unfolded uv] by auto
  3.1675 -          then obtain j where "u\<bullet>j \<noteq> a\<bullet>j \<or> v\<bullet>j \<noteq> b\<bullet>j" and j: "j \<in> Basis"
  3.1676 -            unfolding euclidean_eq_iff[where 'a='b] by auto
  3.1677 -          then have "u\<bullet>j = v\<bullet>j"
  3.1678 -            using uv(2)[rule_format,OF j] by auto
  3.1679 -          then have "content (cbox u v) = 0"
  3.1680 -            unfolding content_eq_0 using j
  3.1681 -            by force
  3.1682 -          then show "g x = \<^bold>1"
  3.1683 -            unfolding uv(1) by (rule operativeD(1)[OF g])
  3.1684 -        qed
  3.1685 -        then show "F g d = g (cbox a b)"
  3.1686 -          using division_ofD[OF less.prems]
  3.1687 -          apply (subst deq)
  3.1688 -          apply (subst insert)
  3.1689 -          apply auto
  3.1690 -          done
  3.1691 -      next
  3.1692 -        case False
  3.1693 -        then have "\<exists>x. x \<in> division_points (cbox a b) d"
  3.1694 -          by auto
  3.1695 -        then guess k c
  3.1696 -          unfolding split_paired_Ex division_points_def mem_Collect_eq split_conv
  3.1697 -          apply (elim exE conjE)
  3.1698 -          done
  3.1699 -        note this(2-4,1) note kc=this[unfolded interval_bounds[OF ab']]
  3.1700 -        from this(3) guess j .. note j=this
  3.1701 -        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> {}}"
  3.1702 -        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> {}}"
  3.1703 -        define cb where "cb = (\<Sum>i\<in>Basis. (if i = k then c else b\<bullet>i) *\<^sub>R i)"
  3.1704 -        define ca where "ca = (\<Sum>i\<in>Basis. (if i = k then c else a\<bullet>i) *\<^sub>R i)"
  3.1705 -        note division_points_psubset[OF \<open>d division_of cbox a b\<close> ab kc(1-2) j]
  3.1706 -        note psubset_card_mono[OF _ this(1)] psubset_card_mono[OF _ this(2)]
  3.1707 -        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})"
  3.1708 -          unfolding interval_split[OF kc(4)]
  3.1709 -          apply (rule_tac[!] "less.hyps"[rule_format])
  3.1710 -          using division_split[OF \<open>d division_of cbox a b\<close>, where k=k and c=c]
  3.1711 -          apply (simp_all add: interval_split kc d1_def d2_def division_points_finite[OF \<open>d division_of cbox a b\<close>])
  3.1712 -          done
  3.1713 -        { fix l y
  3.1714 -          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"
  3.1715 -          from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  3.1716 -          have "g (l \<inter> {x. x \<bullet> k \<le> c}) = \<^bold>1"
  3.1717 -            unfolding leq interval_split[OF kc(4)]
  3.1718 -            apply (rule operativeD[OF g])
  3.1719 -            unfolding interval_split[symmetric, OF kc(4)]
  3.1720 -            using division_split_left_inj less as kc leq by blast
  3.1721 -        } note fxk_le = this
  3.1722 -        { fix l y
  3.1723 -          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"
  3.1724 -          from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  3.1725 -          have "g (l \<inter> {x. x \<bullet> k \<ge> c}) = \<^bold>1"
  3.1726 -            unfolding leq interval_split[OF kc(4)]
  3.1727 -            apply (rule operativeD(1)[OF g])
  3.1728 -            unfolding interval_split[symmetric,OF kc(4)]
  3.1729 -            using division_split_right_inj less leq as kc by blast
  3.1730 -        } note fxk_ge = this
  3.1731 -        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> {}}"
  3.1732 -          using d1_def by auto
  3.1733 -        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> {}}"
  3.1734 -          using d2_def by auto
  3.1735 -        have "g (cbox a b) = F g d1 \<^bold>* F g d2" (is "_ = ?prev")
  3.1736 -          unfolding * using g kc(4) by blast
  3.1737 -        also have "F g d1 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<le> c})) d"
  3.1738 -          unfolding d1_alt using division_of_finite[OF less.prems] fxk_le
  3.1739 -          by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  3.1740 -        also have "F g d2 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<ge> c})) d"
  3.1741 -          unfolding d2_alt using division_of_finite[OF less.prems] fxk_ge
  3.1742 -          by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  3.1743 -        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})"
  3.1744 -          unfolding forall_in_division[OF \<open>d division_of cbox a b\<close>]
  3.1745 -          using g kc(4) by blast
  3.1746 -        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"
  3.1747 -          using * by (simp add: distrib)
  3.1748 -        finally show ?thesis by auto
  3.1749 -      qed
  3.1750 -    qed
  3.1751 -  qed
  3.1752 -qed
  3.1753 -
  3.1754 -lemma (in comm_monoid_set) operative_tagged_division:
  3.1755 -  assumes f: "operative g" and d: "d tagged_division_of (cbox a b)"
  3.1756 -  shows "F (\<lambda>(x, l). g l) d = g (cbox a b)"
  3.1757 -  unfolding d[THEN division_of_tagged_division, THEN operative_division[OF f], symmetric]
  3.1758 -  by (simp add: f[THEN operativeD(1)] over_tagged_division_lemma[OF d])
  3.1759 +  by (force simp add: add.operative_def content_split[symmetric] content_eq_0_interior)
  3.1760  
  3.1761  lemma additive_content_division: "d division_of (cbox a b) \<Longrightarrow> setsum content d = content (cbox a b)"
  3.1762    by (metis operative_content setsum.operative_division)
  3.1763 @@ -1835,537 +209,8 @@
  3.1764  lemma content_real_eq_0: "content {a .. b::real} = 0 \<longleftrightarrow> a \<ge> b"
  3.1765    by (metis atLeastatMost_empty_iff2 content_empty content_real diff_self eq_iff le_cases le_iff_diff_le_0)
  3.1766  
  3.1767 -lemma interval_real_split:
  3.1768 -  "{a .. b::real} \<inter> {x. x \<le> c} = {a .. min b c}"
  3.1769 -  "{a .. b} \<inter> {x. c \<le> x} = {max a c .. b}"
  3.1770 -  apply (metis Int_atLeastAtMostL1 atMost_def)
  3.1771 -  apply (metis Int_atLeastAtMostL2 atLeast_def)
  3.1772 -  done
  3.1773 -
  3.1774 -lemma (in comm_monoid) operative_1_lt:
  3.1775 -  "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  3.1776 -    ((\<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}))"
  3.1777 -  apply (simp add: operative_def content_real_eq_0 atMost_def[symmetric] atLeast_def[symmetric]
  3.1778 -              del: content_real_if)
  3.1779 -proof safe
  3.1780 -  fix a b c :: real
  3.1781 -  assume *: "\<forall>a b c. g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  3.1782 -  assume "a < c" "c < b"
  3.1783 -  with *[rule_format, of a b c] show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  3.1784 -    by (simp add: less_imp_le min.absorb2 max.absorb2)
  3.1785 -next
  3.1786 -  fix a b c :: real
  3.1787 -  assume as: "\<forall>a b. b \<le> a \<longrightarrow> g {a..b} = \<^bold>1"
  3.1788 -    "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  3.1789 -  from as(1)[rule_format, of 0 1] as(1)[rule_format, of a a for a] as(2)
  3.1790 -  have [simp]: "g {} = \<^bold>1" "\<And>a. g {a} = \<^bold>1"
  3.1791 -    "\<And>a b c. a < c \<Longrightarrow> c < b \<Longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  3.1792 -    by auto
  3.1793 -  show "g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  3.1794 -    by (auto simp: min_def max_def le_less)
  3.1795 -qed
  3.1796 -
  3.1797 -lemma (in comm_monoid) operative_1_le:
  3.1798 -  "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  3.1799 -    ((\<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}))"
  3.1800 -  unfolding operative_1_lt
  3.1801 -proof safe
  3.1802 -  fix a b c :: real
  3.1803 -  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"
  3.1804 -  show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  3.1805 -    apply (rule as(1)[rule_format])
  3.1806 -    using as(2-)
  3.1807 -    apply auto
  3.1808 -    done
  3.1809 -next
  3.1810 -  fix a b c :: real
  3.1811 -  assume "\<forall>a b. b \<le> a \<longrightarrow> g {a .. b} = \<^bold>1"
  3.1812 -    and "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  3.1813 -    and "a \<le> c"
  3.1814 -    and "c \<le> b"
  3.1815 -  note as = this[rule_format]
  3.1816 -  show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  3.1817 -  proof (cases "c = a \<or> c = b")
  3.1818 -    case False
  3.1819 -    then show ?thesis
  3.1820 -      apply -
  3.1821 -      apply (subst as(2))
  3.1822 -      using as(3-)
  3.1823 -      apply auto
  3.1824 -      done
  3.1825 -  next
  3.1826 -    case True
  3.1827 -    then show ?thesis
  3.1828 -    proof
  3.1829 -      assume *: "c = a"
  3.1830 -      then have "g {a .. c} = \<^bold>1"
  3.1831 -        apply -
  3.1832 -        apply (rule as(1)[rule_format])
  3.1833 -        apply auto
  3.1834 -        done
  3.1835 -      then show ?thesis
  3.1836 -        unfolding * by auto
  3.1837 -    next
  3.1838 -      assume *: "c = b"
  3.1839 -      then have "g {c .. b} = \<^bold>1"
  3.1840 -        apply -
  3.1841 -        apply (rule as(1)[rule_format])
  3.1842 -        apply auto
  3.1843 -        done
  3.1844 -      then show ?thesis
  3.1845 -        unfolding * by auto
  3.1846 -    qed
  3.1847 -  qed
  3.1848 -qed
  3.1849 -
  3.1850 -subsection \<open>Fine-ness of a partition w.r.t. a gauge.\<close>
  3.1851 -
  3.1852 -definition fine  (infixr "fine" 46)
  3.1853 -  where "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d x)"
  3.1854 -
  3.1855 -lemma fineI:
  3.1856 -  assumes "\<And>x k. (x, k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  3.1857 -  shows "d fine s"
  3.1858 -  using assms unfolding fine_def by auto
  3.1859 -
  3.1860 -lemma fineD[dest]:
  3.1861 -  assumes "d fine s"
  3.1862 -  shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  3.1863 -  using assms unfolding fine_def by auto
  3.1864 -
  3.1865 -lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
  3.1866 -  unfolding fine_def by auto
  3.1867 -
  3.1868 -lemma fine_inters:
  3.1869 - "(\<lambda>x. \<Inter>{f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
  3.1870 -  unfolding fine_def by blast
  3.1871 -
  3.1872 -lemma fine_union: "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
  3.1873 -  unfolding fine_def by blast
  3.1874 -
  3.1875 -lemma fine_unions: "(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
  3.1876 -  unfolding fine_def by auto
  3.1877 -
  3.1878 -lemma fine_subset: "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
  3.1879 -  unfolding fine_def by blast
  3.1880 -
  3.1881 -subsection \<open>Some basic combining lemmas.\<close>
  3.1882 -
  3.1883 -lemma tagged_division_unions_exists:
  3.1884 -  assumes "finite iset"
  3.1885 -    and "\<forall>i\<in>iset. \<exists>p. p tagged_division_of i \<and> d fine p"
  3.1886 -    and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior i1 \<inter> interior i2 = {}"
  3.1887 -    and "\<Union>iset = i"
  3.1888 -   obtains p where "p tagged_division_of i" and "d fine p"
  3.1889 -proof -
  3.1890 -  obtain pfn where pfn:
  3.1891 -    "\<And>x. x \<in> iset \<Longrightarrow> pfn x tagged_division_of x"
  3.1892 -    "\<And>x. x \<in> iset \<Longrightarrow> d fine pfn x"
  3.1893 -    using bchoice[OF assms(2)] by auto
  3.1894 -  show thesis
  3.1895 -    apply (rule_tac p="\<Union>(pfn ` iset)" in that)
  3.1896 -    using assms(1) assms(3) assms(4) pfn(1) tagged_division_unions apply force
  3.1897 -    by (metis (mono_tags, lifting) fine_unions imageE pfn(2))
  3.1898 -qed
  3.1899 -
  3.1900 -
  3.1901 -subsection \<open>The set we're concerned with must be closed.\<close>
  3.1902 -
  3.1903 -lemma division_of_closed:
  3.1904 -  fixes i :: "'n::euclidean_space set"
  3.1905 -  shows "s division_of i \<Longrightarrow> closed i"
  3.1906 -  unfolding division_of_def by fastforce
  3.1907 -
  3.1908 -subsection \<open>General bisection principle for intervals; might be useful elsewhere.\<close>
  3.1909 -
  3.1910 -lemma interval_bisection_step:
  3.1911 -  fixes type :: "'a::euclidean_space"
  3.1912 -  assumes "P {}"
  3.1913 -    and "\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P (s \<union> t)"
  3.1914 -    and "\<not> P (cbox a (b::'a))"
  3.1915 -  obtains c d where "\<not> P (cbox c d)"
  3.1916 -    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"
  3.1917 -proof -
  3.1918 -  have "cbox a b \<noteq> {}"
  3.1919 -    using assms(1,3) by metis
  3.1920 -  then have ab: "\<And>i. i\<in>Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i"
  3.1921 -    by (force simp: mem_box)
  3.1922 -  { fix f
  3.1923 -    have "\<lbrakk>finite f;
  3.1924 -           \<And>s. s\<in>f \<Longrightarrow> P s;
  3.1925 -           \<And>s. s\<in>f \<Longrightarrow> \<exists>a b. s = cbox a b;
  3.1926 -           \<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)"
  3.1927 -    proof (induct f rule: finite_induct)
  3.1928 -      case empty
  3.1929 -      show ?case
  3.1930 -        using assms(1) by auto
  3.1931 -    next
  3.1932 -      case (insert x f)
  3.1933 -      show ?case
  3.1934 -        unfolding Union_insert
  3.1935 -        apply (rule assms(2)[rule_format])
  3.1936 -        using inter_interior_unions_intervals [of f "interior x"]
  3.1937 -        apply (auto simp: insert)
  3.1938 -        by (metis IntI empty_iff insert.hyps(2) insert.prems(3) insert_iff)
  3.1939 -    qed
  3.1940 -  } note UN_cases = this
  3.1941 -  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>
  3.1942 -    (c\<bullet>i = (a\<bullet>i + b\<bullet>i) / 2) \<and> (d\<bullet>i = b\<bullet>i)}"
  3.1943 -  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"
  3.1944 -  {
  3.1945 -    presume "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d) \<Longrightarrow> False"
  3.1946 -    then show thesis
  3.1947 -      unfolding atomize_not not_all
  3.1948 -      by (blast intro: that)
  3.1949 -  }
  3.1950 -  assume as: "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d)"
  3.1951 -  have "P (\<Union>?A)"
  3.1952 -  proof (rule UN_cases)
  3.1953 -    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)
  3.1954 -      (\<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}"
  3.1955 -    have "?A \<subseteq> ?B"
  3.1956 -    proof
  3.1957 -      fix x
  3.1958 -      assume "x \<in> ?A"
  3.1959 -      then obtain c d
  3.1960 -        where x:  "x = cbox c d"
  3.1961 -                  "\<And>i. i \<in> Basis \<Longrightarrow>
  3.1962 -                        c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  3.1963 -                        c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i" by blast
  3.1964 -      show "x \<in> ?B"
  3.1965 -        unfolding image_iff x
  3.1966 -        apply (rule_tac x="{i. i\<in>Basis \<and> c\<bullet>i = a\<bullet>i}" in bexI)
  3.1967 -        apply (rule arg_cong2 [where f = cbox])
  3.1968 -        using x(2) ab
  3.1969 -        apply (auto simp add: euclidean_eq_iff[where 'a='a])
  3.1970 -        by fastforce
  3.1971 -    qed
  3.1972 -    then show "finite ?A"
  3.1973 -      by (rule finite_subset) auto
  3.1974 -  next
  3.1975 -    fix s
  3.1976 -    assume "s \<in> ?A"
  3.1977 -    then obtain c d
  3.1978 -      where s: "s = cbox c d"
  3.1979 -               "\<And>i. i \<in> Basis \<Longrightarrow>
  3.1980 -                     c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  3.1981 -                     c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  3.1982 -      by blast
  3.1983 -    show "P s"
  3.1984 -      unfolding s
  3.1985 -      apply (rule as[rule_format])
  3.1986 -      using ab s(2) by force
  3.1987 -    show "\<exists>a b. s = cbox a b"
  3.1988 -      unfolding s by auto
  3.1989 -    fix t
  3.1990 -    assume "t \<in> ?A"
  3.1991 -    then obtain e f where t:
  3.1992 -      "t = cbox e f"
  3.1993 -      "\<And>i. i \<in> Basis \<Longrightarrow>
  3.1994 -        e \<bullet> i = a \<bullet> i \<and> f \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  3.1995 -        e \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> f \<bullet> i = b \<bullet> i"
  3.1996 -      by blast
  3.1997 -    assume "s \<noteq> t"
  3.1998 -    then have "\<not> (c = e \<and> d = f)"
  3.1999 -      unfolding s t by auto
  3.2000 -    then obtain i where "c\<bullet>i \<noteq> e\<bullet>i \<or> d\<bullet>i \<noteq> f\<bullet>i" and i': "i \<in> Basis"
  3.2001 -      unfolding euclidean_eq_iff[where 'a='a] by auto
  3.2002 -    then have i: "c\<bullet>i \<noteq> e\<bullet>i" "d\<bullet>i \<noteq> f\<bullet>i"
  3.2003 -      using s(2) t(2) apply fastforce
  3.2004 -      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
  3.2005 -    have *: "\<And>s t. (\<And>a. a \<in> s \<Longrightarrow> a \<in> t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}"
  3.2006 -      by auto
  3.2007 -    show "interior s \<inter> interior t = {}"
  3.2008 -      unfolding s t interior_cbox
  3.2009 -    proof (rule *)
  3.2010 -      fix x
  3.2011 -      assume "x \<in> box c d" "x \<in> box e f"
  3.2012 -      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"
  3.2013 -        unfolding mem_box using i'
  3.2014 -        by force+
  3.2015 -      show False  using s(2)[OF i']
  3.2016 -      proof safe
  3.2017 -        assume as: "c \<bullet> i = a \<bullet> i" "d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2"
  3.2018 -        show False
  3.2019 -          using t(2)[OF i'] and i x unfolding as by (fastforce simp add:field_simps)
  3.2020 -      next
  3.2021 -        assume as: "c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2" "d \<bullet> i = b \<bullet> i"
  3.2022 -        show False
  3.2023 -          using t(2)[OF i'] and i x unfolding as by(fastforce simp add:field_simps)
  3.2024 -      qed
  3.2025 -    qed
  3.2026 -  qed
  3.2027 -  also have "\<Union>?A = cbox a b"
  3.2028 -  proof (rule set_eqI,rule)
  3.2029 -    fix x
  3.2030 -    assume "x \<in> \<Union>?A"
  3.2031 -    then obtain c d where x:
  3.2032 -      "x \<in> cbox c d"
  3.2033 -      "\<And>i. i \<in> Basis \<Longrightarrow>
  3.2034 -        c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  3.2035 -        c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  3.2036 -      by blast
  3.2037 -    show "x\<in>cbox a b"
  3.2038 -      unfolding mem_box
  3.2039 -    proof safe
  3.2040 -      fix i :: 'a
  3.2041 -      assume i: "i \<in> Basis"
  3.2042 -      then show "a \<bullet> i \<le> x \<bullet> i" "x \<bullet> i \<le> b \<bullet> i"
  3.2043 -        using x(2)[OF i] x(1)[unfolded mem_box,THEN bspec, OF i] by auto
  3.2044 -    qed
  3.2045 -  next
  3.2046 -    fix x
  3.2047 -    assume x: "x \<in> cbox a b"
  3.2048 -    have "\<forall>i\<in>Basis.
  3.2049 -      \<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"
  3.2050 -      (is "\<forall>i\<in>Basis. \<exists>c d. ?P i c d")
  3.2051 -      unfolding mem_box
  3.2052 -    proof
  3.2053 -      fix i :: 'a
  3.2054 -      assume i: "i \<in> Basis"
  3.2055 -      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)"
  3.2056 -        using x[unfolded mem_box,THEN bspec, OF i] by auto
  3.2057 -      then show "\<exists>c d. ?P i c d"
  3.2058 -        by blast
  3.2059 -    qed
  3.2060 -    then show "x\<in>\<Union>?A"
  3.2061 -      unfolding Union_iff Bex_def mem_Collect_eq choice_Basis_iff
  3.2062 -      apply auto
  3.2063 -      apply (rule_tac x="cbox xa xaa" in exI)
  3.2064 -      unfolding mem_box
  3.2065 -      apply auto
  3.2066 -      done
  3.2067 -  qed
  3.2068 -  finally show False
  3.2069 -    using assms by auto
  3.2070 -qed
  3.2071 -
  3.2072 -lemma interval_bisection:
  3.2073 -  fixes type :: "'a::euclidean_space"
  3.2074 -  assumes "P {}"
  3.2075 -    and "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))"
  3.2076 -    and "\<not> P (cbox a (b::'a))"
  3.2077 -  obtains x where "x \<in> cbox a b"
  3.2078 -    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)"
  3.2079 -proof -
  3.2080 -  have "\<forall>x. \<exists>y. \<not> P (cbox (fst x) (snd x)) \<longrightarrow> (\<not> P (cbox (fst y) (snd y)) \<and>
  3.2081 -    (\<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>
  3.2082 -       2 * (snd y\<bullet>i - fst y\<bullet>i) \<le> snd x\<bullet>i - fst x\<bullet>i))" (is "\<forall>x. ?P x")
  3.2083 -  proof
  3.2084 -    show "?P x" for x
  3.2085 -    proof (cases "P (cbox (fst x) (snd x))")
  3.2086 -      case True
  3.2087 -      then show ?thesis by auto
  3.2088 -    next
  3.2089 -      case as: False
  3.2090 -      obtain c d where "\<not> P (cbox c d)"
  3.2091 -        "\<forall>i\<in>Basis.
  3.2092 -           fst x \<bullet> i \<le> c \<bullet> i \<and>
  3.2093 -           c \<bullet> i \<le> d \<bullet> i \<and>
  3.2094 -           d \<bullet> i \<le> snd x \<bullet> i \<and>
  3.2095 -           2 * (d \<bullet> i - c \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i"
  3.2096 -        by (rule interval_bisection_step[of P, OF assms(1-2) as])
  3.2097 -      then show ?thesis
  3.2098 -        apply -
  3.2099 -        apply (rule_tac x="(c,d)" in exI)
  3.2100 -        apply auto
  3.2101 -        done
  3.2102 -    qed
  3.2103 -  qed
  3.2104 -  then obtain f where f:
  3.2105 -    "\<forall>x.
  3.2106 -      \<not> P (cbox (fst x) (snd x)) \<longrightarrow>
  3.2107 -      \<not> P (cbox (fst (f x)) (snd (f x))) \<and>
  3.2108 -        (\<forall>i\<in>Basis.
  3.2109 -            fst x \<bullet> i \<le> fst (f x) \<bullet> i \<and>
  3.2110 -            fst (f x) \<bullet> i \<le> snd (f x) \<bullet> i \<and>
  3.2111 -            snd (f x) \<bullet> i \<le> snd x \<bullet> i \<and>
  3.2112 -            2 * (snd (f x) \<bullet> i - fst (f x) \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i)"
  3.2113 -    apply -
  3.2114 -    apply (drule choice)
  3.2115 -    apply blast
  3.2116 -    done
  3.2117 -  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
  3.2118 -  have "A 0 = a" "B 0 = b" "\<And>n. \<not> P (cbox (A(Suc n)) (B(Suc n))) \<and>
  3.2119 -    (\<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>
  3.2120 -    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")
  3.2121 -  proof -
  3.2122 -    show "A 0 = a" "B 0 = b"
  3.2123 -      unfolding ab_def by auto
  3.2124 -    note S = ab_def funpow.simps o_def id_apply
  3.2125 -    show "?P n" for n
  3.2126 -    proof (induct n)
  3.2127 -      case 0
  3.2128 -      then show ?case
  3.2129 -        unfolding S
  3.2130 -        apply (rule f[rule_format]) using assms(3)
  3.2131 -        apply auto
  3.2132 -        done
  3.2133 -    next
  3.2134 -      case (Suc n)
  3.2135 -      show ?case
  3.2136 -        unfolding S
  3.2137 -        apply (rule f[rule_format])
  3.2138 -        using Suc
  3.2139 -        unfolding S
  3.2140 -        apply auto
  3.2141 -        done
  3.2142 -    qed
  3.2143 -  qed
  3.2144 -  note AB = this(1-2) conjunctD2[OF this(3),rule_format]
  3.2145 -
  3.2146 -  have interv: "\<exists>n. \<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e"
  3.2147 -    if e: "0 < e" for e
  3.2148 -  proof -
  3.2149 -    obtain n where n: "(\<Sum>i\<in>Basis. b \<bullet> i - a \<bullet> i) / e < 2 ^ n"
  3.2150 -      using real_arch_pow[of 2 "(setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis) / e"] by auto
  3.2151 -    show ?thesis
  3.2152 -    proof (rule exI [where x=n], clarify)
  3.2153 -      fix x y
  3.2154 -      assume xy: "x\<in>cbox (A n) (B n)" "y\<in>cbox (A n) (B n)"
  3.2155 -      have "dist x y \<le> setsum (\<lambda>i. \<bar>(x - y)\<bullet>i\<bar>) Basis"
  3.2156 -        unfolding dist_norm by(rule norm_le_l1)
  3.2157 -      also have "\<dots> \<le> setsum (\<lambda>i. B n\<bullet>i - A n\<bullet>i) Basis"
  3.2158 -      proof (rule setsum_mono)
  3.2159 -        fix i :: 'a
  3.2160 -        assume i: "i \<in> Basis"
  3.2161 -        show "\<bar>(x - y) \<bullet> i\<bar> \<le> B n \<bullet> i - A n \<bullet> i"
  3.2162 -          using xy[unfolded mem_box,THEN bspec, OF i]
  3.2163 -          by (auto simp: inner_diff_left)
  3.2164 -      qed
  3.2165 -      also have "\<dots> \<le> setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis / 2^n"
  3.2166 -        unfolding setsum_divide_distrib
  3.2167 -      proof (rule setsum_mono)
  3.2168 -        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
  3.2169 -        proof (induct n)
  3.2170 -          case 0
  3.2171 -          then show ?case
  3.2172 -            unfolding AB by auto
  3.2173 -        next
  3.2174 -          case (Suc n)
  3.2175 -          have "B (Suc n) \<bullet> i - A (Suc n) \<bullet> i \<le> (B n \<bullet> i - A n \<bullet> i) / 2"
  3.2176 -            using AB(4)[of i n] using i by auto
  3.2177 -          also have "\<dots> \<le> (b \<bullet> i - a \<bullet> i) / 2 ^ Suc n"
  3.2178 -            using Suc by (auto simp add: field_simps)
  3.2179 -          finally show ?case .
  3.2180 -        qed
  3.2181 -      qed
  3.2182 -      also have "\<dots> < e"
  3.2183 -        using n using e by (auto simp add: field_simps)
  3.2184 -      finally show "dist x y < e" .
  3.2185 -    qed
  3.2186 -  qed
  3.2187 -  {
  3.2188 -    fix n m :: nat
  3.2189 -    assume "m \<le> n" then have "cbox (A n) (B n) \<subseteq> cbox (A m) (B m)"
  3.2190 -    proof (induction rule: inc_induct)
  3.2191 -      case (step i)
  3.2192 -      show ?case
  3.2193 -        using AB(4) by (intro order_trans[OF step.IH] subset_box_imp) auto
  3.2194 -    qed simp
  3.2195 -  } note ABsubset = this
  3.2196 -  have "\<exists>a. \<forall>n. a\<in> cbox (A n) (B n)"
  3.2197 -    by (rule decreasing_closed_nest[rule_format,OF closed_cbox _ ABsubset interv])
  3.2198 -      (metis nat.exhaust AB(1-3) assms(1,3))
  3.2199 -  then obtain x0 where x0: "\<And>n. x0 \<in> cbox (A n) (B n)"
  3.2200 -    by blast
  3.2201 -  show thesis
  3.2202 -  proof (rule that[rule_format, of x0])
  3.2203 -    show "x0\<in>cbox a b"
  3.2204 -      using x0[of 0] unfolding AB .
  3.2205 -    fix e :: real
  3.2206 -    assume "e > 0"
  3.2207 -    from interv[OF this] obtain n
  3.2208 -      where n: "\<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e" ..
  3.2209 -    have "\<not> P (cbox (A n) (B n))"
  3.2210 -      apply (cases "0 < n")
  3.2211 -      using AB(3)[of "n - 1"] assms(3) AB(1-2)
  3.2212 -      apply auto
  3.2213 -      done
  3.2214 -    moreover have "cbox (A n) (B n) \<subseteq> ball x0 e"
  3.2215 -      using n using x0[of n] by auto
  3.2216 -    moreover have "cbox (A n) (B n) \<subseteq> cbox a b"
  3.2217 -      unfolding AB(1-2)[symmetric] by (rule ABsubset) auto
  3.2218 -    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)"
  3.2219 -      apply (rule_tac x="A n" in exI)
  3.2220 -      apply (rule_tac x="B n" in exI)
  3.2221 -      apply (auto simp: x0)
  3.2222 -      done
  3.2223 -  qed
  3.2224 -qed
  3.2225 -
  3.2226 -
  3.2227 -subsection \<open>Cousin's lemma.\<close>
  3.2228 -
  3.2229 -lemma fine_division_exists:
  3.2230 -  fixes a b :: "'a::euclidean_space"
  3.2231 -  assumes "gauge g"
  3.2232 -  obtains p where "p tagged_division_of (cbox a b)" "g fine p"
  3.2233 -proof -
  3.2234 -  presume "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p) \<Longrightarrow> False"
  3.2235 -  then obtain p where "p tagged_division_of (cbox a b)" "g fine p"
  3.2236 -    by blast
  3.2237 -  then show thesis ..
  3.2238 -next
  3.2239 -  assume as: "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p)"
  3.2240 -  obtain x where x:
  3.2241 -      "x \<in> (cbox a b)"
  3.2242 -      "\<And>e. 0 < e \<Longrightarrow>
  3.2243 -        \<exists>c d.
  3.2244 -          x \<in> cbox c d \<and>
  3.2245 -          cbox c d \<subseteq> ball x e \<and>
  3.2246 -          cbox c d \<subseteq> (cbox a b) \<and>
  3.2247 -          \<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  3.2248 -    apply (rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p", OF _ _ as])
  3.2249 -    apply (simp add: fine_def)
  3.2250 -    apply (metis tagged_division_union fine_union)
  3.2251 -    apply (auto simp: )
  3.2252 -    done
  3.2253 -  obtain e where e: "e > 0" "ball x e \<subseteq> g x"
  3.2254 -    using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
  3.2255 -  from x(2)[OF e(1)]
  3.2256 -  obtain c d where c_d: "x \<in> cbox c d"
  3.2257 -                        "cbox c d \<subseteq> ball x e"
  3.2258 -                        "cbox c d \<subseteq> cbox a b"
  3.2259 -                        "\<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  3.2260 -    by blast
  3.2261 -  have "g fine {(x, cbox c d)}"
  3.2262 -    unfolding fine_def using e using c_d(2) by auto
  3.2263 -  then show False
  3.2264 -    using tagged_division_of_self[OF c_d(1)] using c_d by auto
  3.2265 -qed
  3.2266 -
  3.2267 -lemma fine_division_exists_real:
  3.2268 -  fixes a b :: real
  3.2269 -  assumes "gauge g"
  3.2270 -  obtains p where "p tagged_division_of {a .. b}" "g fine p"
  3.2271 -  by (metis assms box_real(2) fine_division_exists)
  3.2272 -
  3.2273 -subsection \<open>Division filter\<close>
  3.2274 -
  3.2275 -text \<open>Divisions over all gauges towards finer divisions.\<close>
  3.2276 -
  3.2277 -definition division_filter :: "'a::euclidean_space set \<Rightarrow> ('a \<times> 'a set) set filter"
  3.2278 -  where "division_filter s = (INF g:{g. gauge g}. principal {p. p tagged_division_of s \<and> g fine p})"
  3.2279 -
  3.2280 -lemma eventually_division_filter:
  3.2281 -  "(\<forall>\<^sub>F p in division_filter s. P p) \<longleftrightarrow>
  3.2282 -    (\<exists>g. gauge g \<and> (\<forall>p. p tagged_division_of s \<and> g fine p \<longrightarrow> P p))"
  3.2283 -  unfolding division_filter_def
  3.2284 -proof (subst eventually_INF_base; clarsimp)
  3.2285 -  fix g1 g2 :: "'a \<Rightarrow> 'a set" show "gauge g1 \<Longrightarrow> gauge g2 \<Longrightarrow> \<exists>x. gauge x \<and>
  3.2286 -    {p. p tagged_division_of s \<and> x fine p} \<subseteq> {p. p tagged_division_of s \<and> g1 fine p} \<and>
  3.2287 -    {p. p tagged_division_of s \<and> x fine p} \<subseteq> {p. p tagged_division_of s \<and> g2 fine p}"
  3.2288 -    by (intro exI[of _ "\<lambda>x. g1 x \<inter> g2 x"]) (auto simp: fine_inter)
  3.2289 -qed (auto simp: eventually_principal)
  3.2290 -
  3.2291 -lemma division_filter_not_empty: "division_filter (cbox a b) \<noteq> bot"
  3.2292 -  unfolding trivial_limit_def eventually_division_filter
  3.2293 -  by (auto elim: fine_division_exists)
  3.2294 -
  3.2295 -lemma eventually_division_filter_tagged_division:
  3.2296 -  "eventually (\<lambda>p. p tagged_division_of s) (division_filter s)"
  3.2297 -  unfolding eventually_division_filter by (intro exI[of _ "\<lambda>x. ball x 1"]) auto
  3.2298 +lemma property_empty_interval: "\<forall>a b. content (cbox a b) = 0 \<longrightarrow> P (cbox a b) \<Longrightarrow> P {}"
  3.2299 +  using content_empty unfolding empty_as_interval by auto
  3.2300  
  3.2301  subsection \<open>Gauge integral\<close>
  3.2302  
  3.2303 @@ -2443,26 +288,6 @@
  3.2304  lemma has_integral_integral: "f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
  3.2305    by auto
  3.2306  
  3.2307 -lemma setsum_content_null:
  3.2308 -  assumes "content (cbox a b) = 0"
  3.2309 -    and "p tagged_division_of (cbox a b)"
  3.2310 -  shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
  3.2311 -proof (rule setsum.neutral, rule)
  3.2312 -  fix y
  3.2313 -  assume y: "y \<in> p"
  3.2314 -  obtain x k where xk: "y = (x, k)"
  3.2315 -    using surj_pair[of y] by blast
  3.2316 -  note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
  3.2317 -  from this(2) obtain c d where k: "k = cbox c d" by blast
  3.2318 -  have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x"
  3.2319 -    unfolding xk by auto
  3.2320 -  also have "\<dots> = 0"
  3.2321 -    using content_subset[OF assm(1)[unfolded k]] content_pos_le[of c d]
  3.2322 -    unfolding assms(1) k
  3.2323 -    by auto
  3.2324 -  finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
  3.2325 -qed
  3.2326 -
  3.2327  subsection \<open>Basic theorems about integrals.\<close>
  3.2328  
  3.2329  lemma has_integral_unique:
  3.2330 @@ -3175,38 +1000,32 @@
  3.2331  
  3.2332  subsection \<open>Additivity of integral on abutting intervals.\<close>
  3.2333  
  3.2334 -lemma tagged_division_split_left_inj:
  3.2335 -  fixes x1 :: "'a::euclidean_space"
  3.2336 +lemma tagged_division_split_left_inj_content:
  3.2337    assumes d: "d tagged_division_of i"
  3.2338 -    and k12: "(x1, k1) \<in> d"
  3.2339 -             "(x2, k2) \<in> d"
  3.2340 -             "k1 \<noteq> k2"
  3.2341 -             "k1 \<inter> {x. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c}"
  3.2342 -             "k \<in> Basis"
  3.2343 +    and "(x1, k1) \<in> d" "(x2, k2) \<in> d" "k1 \<noteq> k2" "k1 \<inter> {x. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c}" "k \<in> Basis"
  3.2344    shows "content (k1 \<inter> {x. x\<bullet>k \<le> c}) = 0"
  3.2345  proof -
  3.2346 -  have *: "\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c"
  3.2347 -    by force
  3.2348 +  from tagged_division_ofD(4)[OF d \<open>(x1, k1) \<in> d\<close>] obtain a b where k1: "k1 = cbox a b"
  3.2349 +    by auto
  3.2350    show ?thesis
  3.2351 -    using k12
  3.2352 -    by (fastforce intro!:  division_split_left_inj[OF division_of_tagged_division[OF d]] *)
  3.2353 +    unfolding k1 interval_split[OF \<open>k \<in> Basis\<close>]
  3.2354 +    unfolding content_eq_0_interior
  3.2355 +    unfolding interval_split[OF \<open>k \<in> Basis\<close>, symmetric] k1[symmetric]
  3.2356 +    by (rule tagged_division_split_left_inj[OF assms])
  3.2357  qed
  3.2358  
  3.2359 -lemma tagged_division_split_right_inj:
  3.2360 -  fixes x1 :: "'a::euclidean_space"
  3.2361 +lemma tagged_division_split_right_inj_content:
  3.2362    assumes d: "d tagged_division_of i"
  3.2363 -    and k12: "(x1, k1) \<in> d"
  3.2364 -             "(x2, k2) \<in> d"
  3.2365 -             "k1 \<noteq> k2"
  3.2366 -             "k1 \<inter> {x. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c}"
  3.2367 -             "k \<in> Basis"
  3.2368 +    and "(x1, k1) \<in> d" "(x2, k2) \<in> d" "k1 \<noteq> k2" "k1 \<inter> {x. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c}" "k \<in> Basis"
  3.2369    shows "content (k1 \<inter> {x. x\<bullet>k \<ge> c}) = 0"
  3.2370  proof -
  3.2371 -  have *: "\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c"
  3.2372 -    by force
  3.2373 +  from tagged_division_ofD(4)[OF d \<open>(x1, k1) \<in> d\<close>] obtain a b where k1: "k1 = cbox a b"
  3.2374 +    by auto
  3.2375    show ?thesis
  3.2376 -    using k12
  3.2377 -    by (fastforce intro!:  division_split_right_inj[OF division_of_tagged_division[OF d]] *)
  3.2378 +    unfolding k1 interval_split[OF \<open>k \<in> Basis\<close>]
  3.2379 +    unfolding content_eq_0_interior
  3.2380 +    unfolding interval_split[OF \<open>k \<in> Basis\<close>, symmetric] k1[symmetric]
  3.2381 +    by (rule tagged_division_split_right_inj[OF assms])
  3.2382  qed
  3.2383  
  3.2384  lemma has_integral_split:
  3.2385 @@ -3284,7 +1103,8 @@
  3.2386      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>
  3.2387                           (\<forall>x k. P x k \<longrightarrow> Q x (f k))"
  3.2388        by auto
  3.2389 -    have fin_finite: "finite {(x,f k) | x k. (x,k) \<in> s \<and> P x k}" if "finite s" for f s P
  3.2390 +    have fin_finite: "finite {(x,f k) | x k. (x,k) \<in> s \<and> P x k}"
  3.2391 +      if "finite s" for s and f :: "'a set \<Rightarrow> 'a set" and P :: "'a \<Rightarrow> 'a set \<Rightarrow> bool"
  3.2392      proof -
  3.2393        from that have "finite ((\<lambda>(x, k). (x, f k)) ` s)"
  3.2394          by auto
  3.2395 @@ -3398,8 +1218,9 @@
  3.2396        also have "\<dots> = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f x) +
  3.2397          (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f x) - (i + j)"
  3.2398          unfolding lem3[OF p(3)]
  3.2399 -        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)]
  3.2400 -              simp: cont_eq)+
  3.2401 +        by (subst (1 2) setsum.reindex_nontrivial[OF p(3)])
  3.2402 +           (auto intro!: k eq0 tagged_division_split_left_inj_content[OF p(1)] tagged_division_split_right_inj_content[OF p(1)]
  3.2403 +                 simp: cont_eq)+
  3.2404        also note setsum.distrib[symmetric]
  3.2405        also have "\<And>x. x \<in> p \<Longrightarrow>
  3.2406                      (\<lambda>(x,ka). content (ka \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f x) x +
  3.2407 @@ -3429,34 +1250,6 @@
  3.2408  
  3.2409  subsection \<open>A sort of converse, integrability on subintervals.\<close>
  3.2410  
  3.2411 -lemma tagged_division_union_interval:
  3.2412 -  fixes a :: "'a::euclidean_space"
  3.2413 -  assumes "p1 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<le> (c::real)})"
  3.2414 -    and "p2 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  3.2415 -    and k: "k \<in> Basis"
  3.2416 -  shows "(p1 \<union> p2) tagged_division_of (cbox a b)"
  3.2417 -proof -
  3.2418 -  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})"
  3.2419 -    by auto
  3.2420 -  show ?thesis
  3.2421 -    apply (subst *)
  3.2422 -    apply (rule tagged_division_union[OF assms(1-2)])
  3.2423 -    unfolding interval_split[OF k] interior_cbox
  3.2424 -    using k
  3.2425 -    apply (auto simp add: box_def elim!: ballE[where x=k])
  3.2426 -    done
  3.2427 -qed
  3.2428 -
  3.2429 -lemma tagged_division_union_interval_real:
  3.2430 -  fixes a :: real
  3.2431 -  assumes "p1 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<le> (c::real)})"
  3.2432 -    and "p2 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<ge> c})"
  3.2433 -    and k: "k \<in> Basis"
  3.2434 -  shows "(p1 \<union> p2) tagged_division_of {a .. b}"
  3.2435 -  using assms
  3.2436 -  unfolding box_real[symmetric]
  3.2437 -  by (rule tagged_division_union_interval)
  3.2438 -
  3.2439  lemma has_integral_separate_sides:
  3.2440    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  3.2441    assumes "(f has_integral i) (cbox a b)"
  3.2442 @@ -3621,10 +1414,10 @@
  3.2443      qed
  3.2444    next
  3.2445      fix a b :: 'a
  3.2446 -    assume "content (cbox a b) = 0"
  3.2447 +    assume "box a b = {}"
  3.2448      then show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) = Some 0"
  3.2449        using has_integral_null_eq
  3.2450 -      by (auto simp: integrable_on_null)
  3.2451 +      by (auto simp: integrable_on_null content_eq_0_interior)
  3.2452    qed
  3.2453  qed
  3.2454  
  3.2455 @@ -4069,47 +1862,6 @@
  3.2456  
  3.2457  subsection \<open>Negligibility of hyperplane.\<close>
  3.2458  
  3.2459 -lemma interval_doublesplit:
  3.2460 -  fixes a :: "'a::euclidean_space"
  3.2461 -  assumes "k \<in> Basis"
  3.2462 -  shows "cbox a b \<inter> {x . \<bar>x\<bullet>k - c\<bar> \<le> (e::real)} =
  3.2463 -    cbox (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) (c - e) else a\<bullet>i) *\<^sub>R i)
  3.2464 -     (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) (c + e) else b\<bullet>i) *\<^sub>R i)"
  3.2465 -proof -
  3.2466 -  have *: "\<And>x c e::real. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  3.2467 -    by auto
  3.2468 -  have **: "\<And>s P Q. s \<inter> {x. P x \<and> Q x} = (s \<inter> {x. Q x}) \<inter> {x. P x}"
  3.2469 -    by blast
  3.2470 -  show ?thesis
  3.2471 -    unfolding * ** interval_split[OF assms] by (rule refl)
  3.2472 -qed
  3.2473 -
  3.2474 -lemma division_doublesplit:
  3.2475 -  fixes a :: "'a::euclidean_space"
  3.2476 -  assumes "p division_of (cbox a b)"
  3.2477 -    and k: "k \<in> Basis"
  3.2478 -  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> {}}
  3.2479 -         division_of  (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> e})"
  3.2480 -proof -
  3.2481 -  have *: "\<And>x c. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  3.2482 -    by auto
  3.2483 -  have **: "\<And>p q p' q'. p division_of q \<Longrightarrow> p = p' \<Longrightarrow> q = q' \<Longrightarrow> p' division_of q'"
  3.2484 -    by auto
  3.2485 -  note division_split(1)[OF assms, where c="c+e",unfolded interval_split[OF k]]
  3.2486 -  note division_split(2)[OF this, where c="c-e" and k=k,OF k]
  3.2487 -  then show ?thesis
  3.2488 -    apply (rule **)
  3.2489 -    subgoal
  3.2490 -      apply (simp add: abs_diff_le_iff field_simps Collect_conj_eq setcompr_eq_image[symmetric])
  3.2491 -      apply (rule equalityI)
  3.2492 -      apply blast
  3.2493 -      apply clarsimp
  3.2494 -      apply (rule_tac x="l \<inter> {x. c + e \<ge> x \<bullet> k}" in exI)
  3.2495 -      apply auto
  3.2496 -      done
  3.2497 -    by (simp add: interval_split k interval_doublesplit)
  3.2498 -qed
  3.2499 -
  3.2500  lemma content_doublesplit:
  3.2501    fixes a :: "'a::euclidean_space"
  3.2502    assumes "0 < e"
  3.2503 @@ -4262,6 +2014,8 @@
  3.2504        also have "\<dots> < e"
  3.2505        proof (subst setsum.over_tagged_division_lemma[OF p[THEN conjunct1]], goal_cases)
  3.2506          case prems: (1 u v)
  3.2507 +        then have *: "content (cbox u v) = 0"
  3.2508 +          unfolding content_eq_0_interior by simp
  3.2509          have "content (cbox u v \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<le> content (cbox u v)"
  3.2510            unfolding interval_doublesplit[OF k]
  3.2511            apply (rule content_subset)
  3.2512 @@ -4269,7 +2023,7 @@
  3.2513            apply auto
  3.2514            done
  3.2515          then show ?case
  3.2516 -          unfolding prems interval_doublesplit[OF k]
  3.2517 +          unfolding * interval_doublesplit[OF k]
  3.2518            by (blast intro: antisym)
  3.2519        next
  3.2520          have "(\<Sum>l\<in>snd ` p. content (l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) =
  3.2521 @@ -4303,188 +2057,9 @@
  3.2522  qed
  3.2523  
  3.2524  
  3.2525 -subsection \<open>A technical lemma about "refinement" of division.\<close>
  3.2526 -
  3.2527 -lemma tagged_division_finer:
  3.2528 -  fixes p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  3.2529 -  assumes "p tagged_division_of (cbox a b)"
  3.2530 -    and "gauge d"
  3.2531 -  obtains q where "q tagged_division_of (cbox a b)"
  3.2532 -    and "d fine q"
  3.2533 -    and "\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q"
  3.2534 -proof -
  3.2535 -  let ?P = "\<lambda>p. p tagged_partial_division_of (cbox a b) \<longrightarrow> gauge d \<longrightarrow>
  3.2536 -    (\<exists>q. q tagged_division_of (\<Union>{k. \<exists>x. (x,k) \<in> p}) \<and> d fine q \<and>
  3.2537 -      (\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q))"
  3.2538 -  {
  3.2539 -    have *: "finite p" "p tagged_partial_division_of (cbox a b)"
  3.2540 -      using assms(1)
  3.2541 -      unfolding tagged_division_of_def
  3.2542 -      by auto
  3.2543 -    presume "\<And>p. finite p \<Longrightarrow> ?P p"
  3.2544 -    from this[rule_format,OF * assms(2)] guess q .. note q=this
  3.2545 -    then show ?thesis
  3.2546 -      apply -
  3.2547 -      apply (rule that[of q])
  3.2548 -      unfolding tagged_division_ofD[OF assms(1)]
  3.2549 -      apply auto
  3.2550 -      done
  3.2551 -  }
  3.2552 -  fix p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  3.2553 -  assume as: "finite p"
  3.2554 -  show "?P p"
  3.2555 -    apply rule
  3.2556 -    apply rule
  3.2557 -    using as
  3.2558 -  proof (induct p)
  3.2559 -    case empty
  3.2560 -    show ?case
  3.2561 -      apply (rule_tac x="{}" in exI)
  3.2562 -      unfolding fine_def
  3.2563 -      apply auto
  3.2564 -      done
  3.2565 -  next
  3.2566 -    case (insert xk p)
  3.2567 -    guess x k using surj_pair[of xk] by (elim exE) note xk=this
  3.2568 -    note tagged_partial_division_subset[OF insert(4) subset_insertI]
  3.2569 -    from insert(3)[OF this insert(5)] guess q1 .. note q1 = conjunctD3[OF this]
  3.2570 -    have *: "\<Union>{l. \<exists>y. (y,l) \<in> insert xk p} = k \<union> \<Union>{l. \<exists>y. (y,l) \<in> p}"
  3.2571 -      unfolding xk by auto
  3.2572 -    note p = tagged_partial_division_ofD[OF insert(4)]
  3.2573 -    from p(4)[unfolded xk, OF insertI1] guess u v by (elim exE) note uv=this
  3.2574 -
  3.2575 -    have "finite {k. \<exists>x. (x, k) \<in> p}"
  3.2576 -      apply (rule finite_subset[of _ "snd ` p"])
  3.2577 -      using p
  3.2578 -      apply safe
  3.2579 -      apply (metis image_iff snd_conv)
  3.2580 -      apply auto
  3.2581 -      done
  3.2582 -    then have int: "interior (cbox u v) \<inter> interior (\<Union>{k. \<exists>x. (x, k) \<in> p}) = {}"
  3.2583 -      apply (rule inter_interior_unions_intervals)
  3.2584 -      apply (rule open_interior)
  3.2585 -      apply (rule_tac[!] ballI)
  3.2586 -      unfolding mem_Collect_eq
  3.2587 -      apply (erule_tac[!] exE)
  3.2588 -      apply (drule p(4)[OF insertI2])
  3.2589 -      apply assumption
  3.2590 -      apply (rule p(5))
  3.2591 -      unfolding uv xk
  3.2592 -      apply (rule insertI1)
  3.2593 -      apply (rule insertI2)
  3.2594 -      apply assumption
  3.2595 -      using insert(2)
  3.2596 -      unfolding uv xk
  3.2597 -      apply auto
  3.2598 -      done
  3.2599 -    show ?case
  3.2600 -    proof (cases "cbox u v \<subseteq> d x")
  3.2601 -      case True
  3.2602 -      then show ?thesis
  3.2603 -        apply (rule_tac x="{(x,cbox u v)} \<union> q1" in exI)
  3.2604 -        apply rule
  3.2605 -        unfolding * uv
  3.2606 -        apply (rule tagged_division_union)
  3.2607 -        apply (rule tagged_division_of_self)
  3.2608 -        apply (rule p[unfolded xk uv] insertI1)+
  3.2609 -        apply (rule q1)
  3.2610 -        apply (rule int)
  3.2611 -        apply rule
  3.2612 -        apply (rule fine_union)
  3.2613 -        apply (subst fine_def)
  3.2614 -        defer
  3.2615 -        apply (rule q1)
  3.2616 -        unfolding Ball_def split_paired_All split_conv
  3.2617 -        apply rule
  3.2618 -        apply rule
  3.2619 -        apply rule
  3.2620 -        apply rule
  3.2621 -        apply (erule insertE)
  3.2622 -        apply (simp add: uv xk)
  3.2623 -        apply (rule UnI2)
  3.2624 -        apply (drule q1(3)[rule_format])
  3.2625 -        unfolding xk uv
  3.2626 -        apply auto
  3.2627 -        done
  3.2628 -    next
  3.2629 -      case False
  3.2630 -      from fine_division_exists[OF assms(2), of u v] guess q2 . note q2=this
  3.2631 -      show ?thesis
  3.2632 -        apply (rule_tac x="q2 \<union> q1" in exI)
  3.2633 -        apply rule
  3.2634 -        unfolding * uv
  3.2635 -        apply (rule tagged_division_union q2 q1 int fine_union)+
  3.2636 -        unfolding Ball_def split_paired_All split_conv
  3.2637 -        apply rule
  3.2638 -        apply (rule fine_union)
  3.2639 -        apply (rule q1 q2)+
  3.2640 -        apply rule
  3.2641 -        apply rule
  3.2642 -        apply rule
  3.2643 -        apply rule
  3.2644 -        apply (erule insertE)
  3.2645 -        apply (rule UnI2)
  3.2646 -        apply (simp add: False uv xk)
  3.2647 -        apply (drule q1(3)[rule_format])
  3.2648 -        using False
  3.2649 -        unfolding xk uv
  3.2650 -        apply auto
  3.2651 -        done
  3.2652 -    qed
  3.2653 -  qed
  3.2654 -qed
  3.2655 -
  3.2656  
  3.2657  subsection \<open>Hence the main theorem about negligible sets.\<close>
  3.2658  
  3.2659 -lemma finite_product_dependent:
  3.2660 -  assumes "finite s"
  3.2661 -    and "\<And>x. x \<in> s \<Longrightarrow> finite (t x)"
  3.2662 -  shows "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
  3.2663 -  using assms
  3.2664 -proof induct
  3.2665 -  case (insert x s)
  3.2666 -  have *: "{(i, j) |i j. i \<in> insert x s \<and> j \<in> t i} =
  3.2667 -    (\<lambda>y. (x,y)) ` (t x) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  3.2668 -  show ?case
  3.2669 -    unfolding *
  3.2670 -    apply (rule finite_UnI)
  3.2671 -    using insert
  3.2672 -    apply auto
  3.2673 -    done
  3.2674 -qed auto
  3.2675 -
  3.2676 -lemma sum_sum_product:
  3.2677 -  assumes "finite s"
  3.2678 -    and "\<forall>i\<in>s. finite (t i)"
  3.2679 -  shows "setsum (\<lambda>i. setsum (x i) (t i)::real) s =
  3.2680 -    setsum (\<lambda>(i,j). x i j) {(i,j) | i j. i \<in> s \<and> j \<in> t i}"
  3.2681 -  using assms
  3.2682 -proof induct
  3.2683 -  case (insert a s)
  3.2684 -  have *: "{(i, j) |i j. i \<in> insert a s \<and> j \<in> t i} =
  3.2685 -    (\<lambda>y. (a,y)) ` (t a) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  3.2686 -  show ?case
  3.2687 -    unfolding *
  3.2688 -    apply (subst setsum.union_disjoint)
  3.2689 -    unfolding setsum.insert[OF insert(1-2)]
  3.2690 -    prefer 4
  3.2691 -    apply (subst insert(3))
  3.2692 -    unfolding add_right_cancel
  3.2693 -  proof -
  3.2694 -    show "setsum (x a) (t a) = (\<Sum>(xa, y)\<in> Pair a ` t a. x xa y)"
  3.2695 -      apply (subst setsum.reindex)
  3.2696 -      unfolding inj_on_def
  3.2697 -      apply auto
  3.2698 -      done
  3.2699 -    show "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
  3.2700 -      apply (rule finite_product_dependent)
  3.2701 -      using insert
  3.2702 -      apply auto
  3.2703 -      done
  3.2704 -  qed (insert insert, auto)
  3.2705 -qed auto
  3.2706 -
  3.2707  lemma has_integral_negligible:
  3.2708    fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  3.2709    assumes "negligible s"
  3.2710 @@ -4937,10 +2512,10 @@
  3.2711  proof safe
  3.2712    fix a b :: 'b
  3.2713    show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  3.2714 -    if "content (cbox a b) = 0"
  3.2715 +    if "box a b = {}"
  3.2716      apply (rule_tac x=f in exI)
  3.2717      using assms that
  3.2718 -    apply (auto intro!: integrable_on_null)
  3.2719 +    apply (auto simp: content_eq_0_interior)
  3.2720      done
  3.2721    {
  3.2722      fix c g
  3.2723 @@ -5059,33 +2634,6 @@
  3.2724  
  3.2725  subsection \<open>Specialization of additivity to one dimension.\<close>
  3.2726  
  3.2727 -subsection \<open>Special case of additivity we need for the FTC.\<close>
  3.2728 -
  3.2729 -lemma additive_tagged_division_1:
  3.2730 -  fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  3.2731 -  assumes "a \<le> b"
  3.2732 -    and "p tagged_division_of {a..b}"
  3.2733 -  shows "setsum (\<lambda>(x,k). f(Sup k) - f(Inf k)) p = f b - f a"
  3.2734 -proof -
  3.2735 -  let ?f = "(\<lambda>k::(real) set. if k = {} then 0 else f(interval_upperbound k) - f(interval_lowerbound k))"
  3.2736 -  have ***: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
  3.2737 -    using assms by auto
  3.2738 -  have *: "add.operative ?f"
  3.2739 -    unfolding add.operative_1_lt box_eq_empty
  3.2740 -    by auto
  3.2741 -  have **: "cbox a b \<noteq> {}"
  3.2742 -    using assms(1) by auto
  3.2743 -  note setsum.operative_tagged_division[OF * assms(2)[simplified box_real[symmetric]]]
  3.2744 -  note * = this[unfolded if_not_P[OF **] interval_bounds[OF ***],symmetric]
  3.2745 -  show ?thesis
  3.2746 -    unfolding *
  3.2747 -    apply (rule setsum.cong)
  3.2748 -    unfolding split_paired_all split_conv
  3.2749 -    using assms(2)
  3.2750 -    apply auto
  3.2751 -    done
  3.2752 -qed
  3.2753 -
  3.2754  
  3.2755  subsection \<open>A useful lemma allowing us to factor out the content size.\<close>
  3.2756  
  3.2757 @@ -5373,20 +2921,6 @@
  3.2758  qed
  3.2759  
  3.2760  
  3.2761 -subsection \<open>Attempt a systematic general set of "offset" results for components.\<close>
  3.2762 -
  3.2763 -lemma gauge_modify:
  3.2764 -  assumes "(\<forall>s. open s \<longrightarrow> open {x. f(x) \<in> s})" "gauge d"
  3.2765 -  shows "gauge (\<lambda>x. {y. f y \<in> d (f x)})"
  3.2766 -  using assms
  3.2767 -  unfolding gauge_def
  3.2768 -  apply safe
  3.2769 -  defer
  3.2770 -  apply (erule_tac x="f x" in allE)
  3.2771 -  apply (erule_tac x="d (f x)" in allE)
  3.2772 -  apply auto
  3.2773 -  done
  3.2774 -
  3.2775  
  3.2776  subsection \<open>Only need trivial subintervals if the interval itself is trivial.\<close>
  3.2777  
  3.2778 @@ -5524,10 +3058,12 @@
  3.2779    shows "comm_monoid.operative op \<and> True (\<lambda>i. f integrable_on i)"
  3.2780    unfolding comm_monoid.operative_def[OF comm_monoid_and]
  3.2781    apply safe
  3.2782 -  apply (subst integrable_on_def)
  3.2783 -  unfolding has_integral_null_eq
  3.2784 -  apply (rule, rule refl)
  3.2785 -  apply (rule, assumption, assumption)+
  3.2786 +     apply (subst integrable_on_def)
  3.2787 +     apply rule
  3.2788 +     apply (rule has_integral_null_eq[where i=0, THEN iffD2])
  3.2789 +      apply (simp add: content_eq_0_interior)
  3.2790 +     apply rule
  3.2791 +    apply (rule, assumption, assumption)+
  3.2792    unfolding integrable_on_def
  3.2793    by (auto intro!: has_integral_split)
  3.2794  
  3.2795 @@ -6121,18 +3657,6 @@
  3.2796  
  3.2797  subsection \<open>Stronger form of FCT; quite a tedious proof.\<close>
  3.2798  
  3.2799 -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)"
  3.2800 -  by (meson zero_less_one)
  3.2801 -
  3.2802 -lemma additive_tagged_division_1':
  3.2803 -  fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  3.2804 -  assumes "a \<le> b"
  3.2805 -    and "p tagged_division_of {a..b}"
  3.2806 -  shows "setsum (\<lambda>(x,k). f (Sup k) - f(Inf k)) p = f b - f a"
  3.2807 -  using additive_tagged_division_1[OF _ assms(2), of f]
  3.2808 -  using assms(1)
  3.2809 -  by auto
  3.2810 -
  3.2811  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"
  3.2812    by (simp add: split_def)
  3.2813  
  3.2814 @@ -7717,39 +5241,6 @@
  3.2815    shows "f integrable_on s \<longleftrightarrow> f integrable_on t"
  3.2816  by (blast intro: integrable_spike_set assms negligible_subset)
  3.2817  
  3.2818 -(*lemma integral_spike_set:
  3.2819 - "\<forall>f:real^M->real^N g s t.
  3.2820 -        negligible(s DIFF t \<union> t DIFF s)
  3.2821 -        \<longrightarrow> integral s f = integral t f"
  3.2822 -qed  REPEAT STRIP_TAC THEN REWRITE_TAC[integral] THEN
  3.2823 -  AP_TERM_TAC THEN ABS_TAC THEN MATCH_MP_TAC HAS_INTEGRAL_SPIKE_SET_EQ THEN
  3.2824 -  ASM_MESON_TAC[]);;
  3.2825 -
  3.2826 -lemma has_integral_interior:
  3.2827 - "\<forall>f:real^M->real^N y s.
  3.2828 -        negligible(frontier s)
  3.2829 -        \<longrightarrow> ((f has_integral y) (interior s) \<longleftrightarrow> (f has_integral y) s)"
  3.2830 -qed  REPEAT STRIP_TAC THEN MATCH_MP_TAC HAS_INTEGRAL_SPIKE_SET_EQ THEN
  3.2831 -  FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (REWRITE_RULE[IMP_CONJ]
  3.2832 -    NEGLIGIBLE_SUBSET)) THEN
  3.2833 -  REWRITE_TAC[frontier] THEN
  3.2834 -  MP_TAC(ISPEC `s:real^M->bool` INTERIOR_SUBSET) THEN
  3.2835 -  MP_TAC(ISPEC `s:real^M->bool` CLOSURE_SUBSET) THEN
  3.2836 -  SET_TAC[]);;
  3.2837 -
  3.2838 -lemma has_integral_closure:
  3.2839 - "\<forall>f:real^M->real^N y s.
  3.2840 -        negligible(frontier s)
  3.2841 -        \<longrightarrow> ((f has_integral y) (closure s) \<longleftrightarrow> (f has_integral y) s)"
  3.2842 -qed  REPEAT STRIP_TAC THEN MATCH_MP_TAC HAS_INTEGRAL_SPIKE_SET_EQ THEN
  3.2843 -  FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (REWRITE_RULE[IMP_CONJ]
  3.2844 -    NEGLIGIBLE_SUBSET)) THEN
  3.2845 -  REWRITE_TAC[frontier] THEN
  3.2846 -  MP_TAC(ISPEC `s:real^M->bool` INTERIOR_SUBSET) THEN
  3.2847 -  MP_TAC(ISPEC `s:real^M->bool` CLOSURE_SUBSET) THEN
  3.2848 -  SET_TAC[]);;*)
  3.2849 -
  3.2850 -
  3.2851  subsection \<open>More lemmas that are useful later\<close>
  3.2852  
  3.2853  lemma has_integral_subset_component_le:
  3.2854 @@ -8434,34 +5925,26 @@
  3.2855  
  3.2856  subsection \<open>Also tagged divisions\<close>
  3.2857  
  3.2858 +lemma has_integral_iff: "(f has_integral i) s \<longleftrightarrow> (f integrable_on s \<and> integral s f = i)"
  3.2859 +  by blast
  3.2860 +
  3.2861  lemma has_integral_combine_tagged_division:
  3.2862    fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
  3.2863    assumes "p tagged_division_of s"
  3.2864      and "\<forall>(x,k) \<in> p. (f has_integral (i k)) k"
  3.2865 -  shows "(f has_integral (setsum (\<lambda>(x,k). i k) p)) s"
  3.2866 +  shows "(f has_integral (\<Sum>(x,k)\<in>p. i k)) s"
  3.2867  proof -
  3.2868 -  have *: "(f has_integral (setsum (\<lambda>k. integral k f) (snd ` p))) s"
  3.2869 -    apply (rule has_integral_combine_division)
  3.2870 -    apply (rule division_of_tagged_division[OF assms(1)])
  3.2871 +  have *: "(f has_integral (\<Sum>k\<in>snd`p. integral k f)) s"
  3.2872      using assms(2)
  3.2873 -    unfolding has_integral_integral[symmetric]
  3.2874 -    apply safe
  3.2875 +    apply (intro has_integral_combine_division)
  3.2876 +    apply (auto simp: has_integral_integral[symmetric] intro: division_of_tagged_division[OF assms(1)])
  3.2877      apply auto
  3.2878      done
  3.2879 -  then show ?thesis
  3.2880 -    apply -
  3.2881 -    apply (rule subst[where P="\<lambda>i. (f has_integral i) s"])
  3.2882 -    defer
  3.2883 -    apply assumption
  3.2884 -    apply (rule trans[of _ "setsum (\<lambda>(x,k). integral k f) p"])
  3.2885 -    apply (subst eq_commute)
  3.2886 -    apply (rule setsum.over_tagged_division_lemma[OF assms(1)])
  3.2887 -    apply (rule integral_null)
  3.2888 -    apply assumption
  3.2889 -    apply (rule setsum.cong)
  3.2890 -    using assms(2)
  3.2891 -    apply auto
  3.2892 -    done
  3.2893 +  also have "(\<Sum>k\<in>snd`p. integral k f) = (\<Sum>(x, k)\<in>p. integral k f)"
  3.2894 +    by (intro setsum.over_tagged_division_lemma[OF assms(1), symmetric] integral_null)
  3.2895 +       (simp add: content_eq_0_interior)
  3.2896 +  finally show ?thesis
  3.2897 +    using assms by (auto simp add: has_integral_iff intro!: setsum.cong)
  3.2898  qed
  3.2899  
  3.2900  lemma integral_combine_tagged_division_bottomup:
  3.2901 @@ -9616,84 +7099,6 @@
  3.2902  
  3.2903  subsection \<open>differentiation under the integral sign\<close>
  3.2904  
  3.2905 -lemma tube_lemma:
  3.2906 -  assumes "compact K"
  3.2907 -  assumes "open W"
  3.2908 -  assumes "{x0} \<times> K \<subseteq> W"
  3.2909 -  shows "\<exists>X0. x0 \<in> X0 \<and> open X0 \<and> X0 \<times> K \<subseteq> W"
  3.2910 -proof -
  3.2911 -  {
  3.2912 -    fix y assume "y \<in> K"
  3.2913 -    then have "(x0, y) \<in> W" using assms by auto
  3.2914 -    with \<open>open W\<close>
  3.2915 -    have "\<exists>X0 Y. open X0 \<and> open Y \<and> x0 \<in> X0 \<and> y \<in> Y \<and> X0 \<times> Y \<subseteq> W"
  3.2916 -      by (rule open_prod_elim) blast
  3.2917 -  }
  3.2918 -  then obtain X0 Y where
  3.2919 -    *: "\<forall>y \<in> K. open (X0 y) \<and> open (Y y) \<and> x0 \<in> X0 y \<and> y \<in> Y y \<and> X0 y \<times> Y y \<subseteq> W"
  3.2920 -    by metis
  3.2921 -  from * have "\<forall>t\<in>Y ` K. open t" "K \<subseteq> \<Union>(Y ` K)" by auto
  3.2922 -  with \<open>compact K\<close> obtain CC where CC: "CC \<subseteq> Y ` K" "finite CC" "K \<subseteq> \<Union>CC"
  3.2923 -    by (rule compactE)
  3.2924 -  then obtain c where c: "\<And>C. C \<in> CC \<Longrightarrow> c C \<in> K \<and> C = Y (c C)"
  3.2925 -    by (force intro!: choice)
  3.2926 -  with * CC show ?thesis
  3.2927 -    by (force intro!: exI[where x="\<Inter>C\<in>CC. X0 (c C)"]) (* SLOW *)
  3.2928 -qed
  3.2929 -
  3.2930 -lemma continuous_on_prod_compactE:
  3.2931 -  fixes fx::"'a::topological_space \<times> 'b::topological_space \<Rightarrow> 'c::metric_space"
  3.2932 -    and e::real
  3.2933 -  assumes cont_fx: "continuous_on (U \<times> C) fx"
  3.2934 -  assumes "compact C"
  3.2935 -  assumes [intro]: "x0 \<in> U"
  3.2936 -  notes [continuous_intros] = continuous_on_compose2[OF cont_fx]
  3.2937 -  assumes "e > 0"
  3.2938 -  obtains X0 where "x0 \<in> X0" "open X0"
  3.2939 -    "\<forall>x\<in>X0 \<inter> U. \<forall>t \<in> C. dist (fx (x, t)) (fx (x0, t)) \<le> e"
  3.2940 -proof -
  3.2941 -  define psi where "psi = (\<lambda>(x, t). dist (fx (x, t)) (fx (x0, t)))"
  3.2942 -  define W0 where "W0 = {(x, t) \<in> U \<times> C. psi (x, t) < e}"
  3.2943 -  have W0_eq: "W0 = psi -` {..<e} \<inter> U \<times> C"
  3.2944 -    by (auto simp: vimage_def W0_def)
  3.2945 -  have "open {..<e}" by simp
  3.2946 -  have "continuous_on (U \<times> C) psi"
  3.2947 -    by (auto intro!: continuous_intros simp: psi_def split_beta')
  3.2948 -  from this[unfolded continuous_on_open_invariant, rule_format, OF \<open>open {..<e}\<close>]
  3.2949 -  obtain W where W: "open W" "W \<inter> U \<times> C = W0 \<inter> U \<times> C"
  3.2950 -    unfolding W0_eq by blast
  3.2951 -  have "{x0} \<times> C \<subseteq> W \<inter> U \<times> C"
  3.2952 -    unfolding W
  3.2953 -    by (auto simp: W0_def psi_def \<open>0 < e\<close>)
  3.2954 -  then have "{x0} \<times> C \<subseteq> W" by blast
  3.2955 -  from tube_lemma[OF \<open>compact C\<close> \<open>open W\<close> this]
  3.2956 -  obtain X0 where X0: "x0 \<in> X0" "open X0" "X0 \<times> C \<subseteq> W"
  3.2957 -    by blast
  3.2958 -
  3.2959 -  have "\<forall>x\<in>X0 \<inter> U. \<forall>t \<in> C. dist (fx (x, t)) (fx (x0, t)) \<le> e"
  3.2960 -  proof safe
  3.2961 -    fix x assume x: "x \<in> X0" "x \<in> U"
  3.2962 -    fix t assume t: "t \<in> C"
  3.2963 -    have "dist (fx (x, t)) (fx (x0, t)) = psi (x, t)"
  3.2964 -      by (auto simp: psi_def)
  3.2965 -    also
  3.2966 -    {
  3.2967 -      have "(x, t) \<in> X0 \<times> C"
  3.2968 -        using t x
  3.2969 -        by auto
  3.2970 -      also note \<open>\<dots> \<subseteq> W\<close>
  3.2971 -      finally have "(x, t) \<in> W" .
  3.2972 -      with t x have "(x, t) \<in> W \<inter> U \<times> C"
  3.2973 -        by blast
  3.2974 -      also note \<open>W \<inter> U \<times> C = W0 \<inter> U \<times> C\<close>
  3.2975 -      finally  have "psi (x, t) < e"
  3.2976 -        by (auto simp: W0_def)
  3.2977 -    }
  3.2978 -    finally show "dist (fx (x, t)) (fx (x0, t)) \<le> e" by simp
  3.2979 -  qed
  3.2980 -  from X0(1,2) this show ?thesis ..
  3.2981 -qed
  3.2982 -
  3.2983  lemma integral_continuous_on_param:
  3.2984    fixes f::"'a::topological_space \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::banach"
  3.2985    assumes cont_fx: "continuous_on (U \<times> cbox a b) (\<lambda>(x, t). f x t)"
  3.2986 @@ -9743,26 +7148,6 @@
  3.2987    qed
  3.2988  qed (auto intro!: continuous_on_const)
  3.2989  
  3.2990 -lemma eventually_closed_segment:
  3.2991 -  fixes x0::"'a::real_normed_vector"
  3.2992 -  assumes "open X0" "x0 \<in> X0"
  3.2993 -  shows "\<forall>\<^sub>F x in at x0 within U. closed_segment x0 x \<subseteq> X0"
  3.2994 -proof -
  3.2995 -  from openE[OF assms]
  3.2996 -  obtain e where e: "0 < e" "ball x0 e \<subseteq> X0" .
  3.2997 -  then have "\<forall>\<^sub>F x in at x0 within U. x \<in> ball x0 e"
  3.2998 -    by (auto simp: dist_commute eventually_at)
  3.2999 -  then show ?thesis
  3.3000 -  proof eventually_elim
  3.3001 -    case (elim x)
  3.3002 -    have "x0 \<in> ball x0 e" using \<open>e > 0\<close> by simp
  3.3003 -    from convex_ball[unfolded convex_contains_segment, rule_format, OF this elim]
  3.3004 -    have "closed_segment x0 x \<subseteq> ball x0 e" .
  3.3005 -    also note \<open>\<dots> \<subseteq> X0\<close>
  3.3006 -    finally show ?case .
  3.3007 -  qed
  3.3008 -qed
  3.3009 -
  3.3010  lemma leibniz_rule:
  3.3011    fixes f::"'a::banach \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::banach"
  3.3012    assumes fx: "\<And>x t. x \<in> U \<Longrightarrow> t \<in> cbox a b \<Longrightarrow>
  3.3013 @@ -9856,8 +7241,7 @@
  3.3014    qed (rule blinfun.bounded_linear_right)
  3.3015  qed (auto intro!: derivative_eq_intros simp: blinfun.bilinear_simps)
  3.3016  
  3.3017 -lemma
  3.3018 -  has_vector_derivative_eq_has_derivative_blinfun:
  3.3019 +lemma has_vector_derivative_eq_has_derivative_blinfun:
  3.3020    "(f has_vector_derivative f') (at x within U) \<longleftrightarrow>
  3.3021      (f has_derivative blinfun_scaleR_left f') (at x within U)"
  3.3022    by (simp add: has_vector_derivative_def)
  3.3023 @@ -9889,8 +7273,7 @@
  3.3024      done
  3.3025  qed
  3.3026  
  3.3027 -lemma
  3.3028 -  has_field_derivative_eq_has_derivative_blinfun:
  3.3029 +lemma has_field_derivative_eq_has_derivative_blinfun:
  3.3030    "(f has_field_derivative f') (at x within U) \<longleftrightarrow> (f has_derivative blinfun_mult_right f') (at x within U)"
  3.3031    by (simp add: has_field_derivative_def)
  3.3032  
  3.3033 @@ -9922,8 +7305,7 @@
  3.3034  
  3.3035  subsection \<open>Exchange uniform limit and integral\<close>
  3.3036  
  3.3037 -lemma
  3.3038 -  uniform_limit_integral:
  3.3039 +lemma uniform_limit_integral:
  3.3040    fixes f::"'a \<Rightarrow> 'b::euclidean_space \<Rightarrow> 'c::banach"
  3.3041    assumes u: "uniform_limit (cbox a b) f g F"
  3.3042    assumes c: "\<And>n. continuous_on (cbox a b) (f n)"
  3.3043 @@ -10117,18 +7499,6 @@
  3.3044  
  3.3045  subsection \<open>Compute a double integral using iterated integrals and switching the order of integration\<close>
  3.3046  
  3.3047 -lemma setcomp_dot1: "{z. P (z \<bullet> (i,0))} = {(x,y). P(x \<bullet> i)}"
  3.3048 -  by auto
  3.3049 -
  3.3050 -lemma setcomp_dot2: "{z. P (z \<bullet> (0,i))} = {(x,y). P(y \<bullet> i)}"
  3.3051 -  by auto
  3.3052 -
  3.3053 -lemma Sigma_Int_Paircomp1: "(Sigma A B) \<inter> {(x, y). P x} = Sigma (A \<inter> {x. P x}) B"
  3.3054 -  by blast
  3.3055 -
  3.3056 -lemma Sigma_Int_Paircomp2: "(Sigma A B) \<inter> {(x, y). P y} = Sigma A (\<lambda>z. B z \<inter> {y. P y})"
  3.3057 -  by blast
  3.3058 -
  3.3059  lemma continuous_on_imp_integrable_on_Pair1:
  3.3060    fixes f :: "_ \<Rightarrow> 'b::banach"
  3.3061    assumes con: "continuous_on (cbox (a,c) (b,d)) f" and x: "x \<in> cbox a b"
  3.3062 @@ -10180,11 +7550,6 @@
  3.3063      done
  3.3064  qed
  3.3065  
  3.3066 -lemma norm_diff2: "\<lbrakk>y = y1 + y2; x = x1 + x2; e = e1 + e2; norm(y1 - x1) \<le> e1; norm(y2 - x2) \<le> e2\<rbrakk>
  3.3067 -            \<Longrightarrow> norm(y - x) \<le> e"
  3.3068 -using norm_triangle_mono [of "y1 - x1" "e1" "y2 - x2" "e2"]
  3.3069 -  by (simp add: add_diff_add)
  3.3070 -
  3.3071  lemma integral_split:
  3.3072    fixes f :: "'a::euclidean_space \<Rightarrow> 'b::{real_normed_vector,complete_space}"
  3.3073    assumes f: "f integrable_on (cbox a b)"
  3.3074 @@ -10208,9 +7573,11 @@
  3.3075                      \<le> e * content (cbox (a,c) (b,d)))"
  3.3076  proof (auto simp: comm_monoid.operative_def[OF comm_monoid_and])
  3.3077    fix a::'a and c::'b and b::'a and d::'b and u::'a and v::'a and w::'b and z::'b
  3.3078 -  assume c0: "content (cbox (a, c) (b, d)) = 0"
  3.3079 +  assume *: "box (a, c) (b, d) = {}"
  3.3080       and cb1: "cbox (u, w) (v, z) \<subseteq> cbox (a, c) (b, d)"
  3.3081       and cb2: "cbox (u, w) (v, z) \<subseteq> s"
  3.3082 +  then have c0: "content (cbox (a, c) (b, d)) = 0"
  3.3083 +    using * unfolding content_eq_0_interior by simp
  3.3084    have c0': "content (cbox (u, w) (v, z)) = 0"
  3.3085      by (fact content_0_subset [OF c0 cb1])
  3.3086    show "norm (integral (cbox (u,w) (v,z)) f - integral (cbox u v) (\<lambda>x. integral (cbox w z) (\<lambda>y. f (x, y))))
  3.3087 @@ -10296,9 +7663,6 @@
  3.3088       integral (cbox a b) (\<lambda>x. integral (cbox c d) (\<lambda>y. k))"
  3.3089    by (simp add: content_Pair)
  3.3090  
  3.3091 -lemma norm_minus2: "norm (x1-x2, y1-y2) = norm (x2-x1, y2-y1)"
  3.3092 -  by (simp add: norm_minus_eqI)
  3.3093 -
  3.3094  lemma integral_prod_continuous:
  3.3095    fixes f :: "('a::euclidean_space * 'b::euclidean_space) \<Rightarrow> 'c::banach"
  3.3096    assumes "continuous_on (cbox (a,c) (b,d)) f" (is "continuous_on ?CBOX f")
  3.3097 @@ -10414,20 +7778,6 @@
  3.3098      by simp
  3.3099  qed
  3.3100  
  3.3101 -lemma swap_continuous:
  3.3102 -  assumes "continuous_on (cbox (a,c) (b,d)) (\<lambda>(x,y). f x y)"
  3.3103 -    shows "continuous_on (cbox (c,a) (d,b)) (\<lambda>(x, y). f y x)"
  3.3104 -proof -
  3.3105 -  have "(\<lambda>(x, y). f y x) = (\<lambda>(x, y). f x y) \<circ> prod.swap"
  3.3106 -    by auto
  3.3107 -  then show ?thesis
  3.3108 -    apply (rule ssubst)
  3.3109 -    apply (rule continuous_on_compose)
  3.3110 -    apply (simp add: split_def)
  3.3111 -    apply (rule continuous_intros | simp add: assms)+
  3.3112 -    done
  3.3113 -qed
  3.3114 -
  3.3115  lemma integral_swap_2dim:
  3.3116    fixes f :: "['a::euclidean_space, 'b::euclidean_space] \<Rightarrow> 'c::banach"
  3.3117    assumes "continuous_on (cbox (a,c) (b,d)) (\<lambda>(x,y). f x y)"
  3.3118 @@ -10731,277 +8081,6 @@
  3.3119         (insert assms, simp_all add: powr_minus powr_realpow divide_simps)
  3.3120  qed
  3.3121  
  3.3122 -subsubsection \<open>Covering lemma\<close>
  3.3123 -
  3.3124 -
  3.3125 -text\<open> Some technical lemmas used in the approximation results that follow. Proof of the covering
  3.3126 -  lemma is an obvious multidimensional generalization of Lemma 3, p65 of Swartz's
  3.3127 -  "Introduction to Gauge Integrals". \<close>
  3.3128 -
  3.3129 -proposition covering_lemma:
  3.3130 -  assumes "S \<subseteq> cbox a b" "box a b \<noteq> {}" "gauge g"
  3.3131 -  obtains \<D> where
  3.3132 -    "countable \<D>"  "\<Union>\<D> \<subseteq> cbox a b"
  3.3133 -    "\<And>K. K \<in> \<D> \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  3.3134 -    "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
  3.3135 -    "\<And>K. K \<in> \<D> \<Longrightarrow> \<exists>x \<in> S \<inter> K. K \<subseteq> g x"
  3.3136 -    "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  3.3137 -    "S \<subseteq> \<Union>\<D>"
  3.3138 -proof -
  3.3139 -  have aibi: "\<And>i. i \<in> Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i" and normab: "0 < norm(b - a)"
  3.3140 -    using \<open>box a b \<noteq> {}\<close> box_eq_empty box_sing by fastforce+
  3.3141 -  let ?K0 = "\<lambda>(n, f::'a\<Rightarrow>nat).
  3.3142 -                    cbox (\<Sum>i \<in> Basis. (a \<bullet> i + (f i / 2^n) * (b \<bullet> i - a \<bullet> i)) *\<^sub>R i)
  3.3143 -                         (\<Sum>i \<in> Basis. (a \<bullet> i + ((f i + 1) / 2^n) * (b \<bullet> i - a \<bullet> i)) *\<^sub>R i)"
  3.3144 -  let ?D0 = "?K0 ` (SIGMA n:UNIV. PiE Basis (\<lambda>i::'a. lessThan (2^n)))"
  3.3145 -  obtain \<D>0 where count: "countable \<D>0"
  3.3146 -             and sub: "\<Union>\<D>0 \<subseteq> cbox a b"
  3.3147 -             and int:  "\<And>K. K \<in> \<D>0 \<Longrightarrow> (interior K \<noteq> {}) \<and> (\<exists>c d. K = cbox c d)"
  3.3148 -             and intdj: "\<And>A B. \<lbrakk>A \<in> \<D>0; B \<in> \<D>0\<rbrakk> \<Longrightarrow> A \<subseteq> B \<or> B \<subseteq> A \<or> interior A \<inter> interior B = {}"
  3.3149 -             and SK: "\<And>x. x \<in> S \<Longrightarrow> \<exists>K \<in> \<D>0. x \<in> K \<and> K \<subseteq> g x"
  3.3150 -             and cbox: "\<And>u v. cbox u v \<in> \<D>0 \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  3.3151 -             and fin: "\<And>K. K \<in> \<D>0 \<Longrightarrow> finite {L \<in> \<D>0. K \<subseteq> L}"
  3.3152 -  proof
  3.3153 -    show "countable ?D0"
  3.3154 -      by (simp add: countable_PiE)
  3.3155 -  next
  3.3156 -    show "\<Union>?D0 \<subseteq> cbox a b"
  3.3157 -      apply (simp add: UN_subset_iff)
  3.3158 -      apply (intro conjI allI ballI subset_box_imp)
  3.3159 -       apply (simp add: divide_simps zero_le_mult_iff aibi)
  3.3160 -      apply (force simp: aibi scaling_mono nat_less_real_le dest: PiE_mem)
  3.3161 -      done
  3.3162 -  next
  3.3163 -    show "\<And>K. K \<in> ?D0 \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  3.3164 -      using \<open>box a b \<noteq> {}\<close>
  3.3165 -      by (clarsimp simp: box_eq_empty) (fastforce simp add: divide_simps dest: PiE_mem)
  3.3166 -  next
  3.3167 -    have realff: "(real w) * 2^m < (real v) * 2^n \<longleftrightarrow> w * 2^m < v * 2^n" for m n v w
  3.3168 -      using of_nat_less_iff less_imp_of_nat_less by fastforce
  3.3169 -    have *: "\<forall>v w. ?K0(m,v) \<subseteq> ?K0(n,w) \<or> ?K0(n,w) \<subseteq> ?K0(m,v) \<or> interior(?K0(m,v)) \<inter> interior(?K0(n,w)) = {}"
  3.3170 -      for m n --\<open>The symmetry argument requires a single HOL formula\<close>
  3.3171 -    proof (rule linorder_wlog [where a=m and b=n], intro allI impI)
  3.3172 -      fix v w m and n::nat
  3.3173 -      assume "m \<le> n" --\<open>WLOG we can assume @{term"m \<le> n"}, when the first disjunct becomes impossible\<close>
  3.3174 -      have "?K0(n,w) \<subseteq> ?K0(m,v) \<or> interior(?K0(m,v)) \<inter> interior(?K0(n,w)) = {}"
  3.3175 -        apply (simp add: subset_box disjoint_interval)
  3.3176 -        apply (rule ccontr)
  3.3177 -        apply (clarsimp simp add: aibi mult_le_cancel_right divide_le_cancel not_less not_le)
  3.3178 -        apply (drule_tac x=i in bspec, assumption)
  3.3179 -        using \<open>m\<le>n\<close> realff [of _ _ "1+_"] realff [of "1+_"_ "1+_"]
  3.3180 -        apply (auto simp: divide_simps add.commute not_le nat_le_iff_add realff)
  3.3181 -        apply (simp add: power_add, metis (no_types, hide_lams) mult_Suc mult_less_cancel2 not_less_eq mult.assoc)+
  3.3182 -        done
  3.3183 -      then show "?K0(m,v) \<subseteq> ?K0(n,w) \<or> ?K0(n,w) \<subseteq> ?K0(m,v) \<or> interior(?K0(m,v)) \<inter> interior(?K0(n,w)) = {}"
  3.3184 -        by meson
  3.3185 -    qed auto
  3.3186 -    show "\<And>A B. \<lbrakk>A \<in> ?D0; B \<in> ?D0\<rbrakk> \<Longrightarrow> A \<subseteq> B \<or> B \<subseteq> A \<or> interior A \<inter> interior B = {}"
  3.3187 -      apply (erule imageE SigmaE)+
  3.3188 -      using * by simp
  3.3189 -  next
  3.3190 -    show "\<exists>K \<in> ?D0. x \<in> K \<and> K \<subseteq> g x" if "x \<in> S" for x
  3.3191 -    proof (simp only: bex_simps split_paired_Bex_Sigma)
  3.3192 -      show "\<exists>n. \<exists>f \<in> Basis \<rightarrow>\<^sub>E {..<2 ^ n}. x \<in> ?K0(n,f) \<and> ?K0(n,f) \<subseteq> g x"
  3.3193 -      proof -
  3.3194 -        obtain e where "0 < e"
  3.3195 -                   and e: "\<And>y. (\<And>i. i \<in> Basis \<Longrightarrow> \<bar>x \<bullet> i - y \<bullet> i\<bar> \<le> e) \<Longrightarrow> y \<in> g x"
  3.3196 -        proof -
  3.3197 -          have "x \<in> g x" "open (g x)"
  3.3198 -            using \<open>gauge g\<close> by (auto simp: gauge_def)
  3.3199 -          then obtain \<epsilon> where "0 < \<epsilon>" and \<epsilon>: "ball x \<epsilon> \<subseteq> g x"
  3.3200 -            using openE by blast
  3.3201 -          have "norm (x - y) < \<epsilon>"
  3.3202 -               if "(\<And>i. i \<in> Basis \<Longrightarrow> \<bar>x \<bullet> i - y \<bullet> i\<bar> \<le> \<epsilon> / (2 * real DIM('a)))" for y
  3.3203 -          proof -
  3.3204 -            have "norm (x - y) \<le> (\<Sum>i\<in>Basis. \<bar>x \<bullet> i - y \<bullet> i\<bar>)"
  3.3205 -              by (metis (no_types, lifting) inner_diff_left norm_le_l1 setsum.cong)
  3.3206 -            also have "... \<le> DIM('a) * (\<epsilon> / (2 * real DIM('a)))"
  3.3207 -              by (meson setsum_bounded_above that)
  3.3208 -            also have "... = \<epsilon> / 2"
  3.3209 -              by (simp add: divide_simps)
  3.3210 -            also have "... < \<epsilon>"
  3.3211 -              by (simp add: \<open>0 < \<epsilon>\<close>)
  3.3212 -            finally show ?thesis .
  3.3213 -          qed
  3.3214 -          then show ?thesis
  3.3215 -            by (rule_tac e = "\<epsilon> / 2 / DIM('a)" in that) (simp_all add:  \<open>0 < \<epsilon>\<close> dist_norm subsetD [OF \<epsilon>])
  3.3216 -        qed
  3.3217 -        have xab: "x \<in> cbox a b"
  3.3218 -          using \<open>x \<in> S\<close> \<open>S \<subseteq> cbox a b\<close> by blast
  3.3219 -        obtain n where n: "norm (b - a) / 2^n < e"
  3.3220 -          using real_arch_pow_inv [of "e / norm(b - a)" "1/2"] normab \<open>0 < e\<close>
  3.3221 -          by (auto simp: divide_simps)
  3.3222 -        then have "norm (b - a) < e * 2^n"
  3.3223 -          by (auto simp: divide_simps)
  3.3224 -        then have bai: "b \<bullet> i - a \<bullet> i < e * 2 ^ n" if "i \<in> Basis" for i
  3.3225 -        proof -
  3.3226 -          have "b \<bullet> i - a \<bullet> i \<le> norm (b - a)"
  3.3227 -            by (metis abs_of_nonneg dual_order.trans inner_diff_left linear norm_ge_zero Basis_le_norm that)
  3.3228 -          also have "... < e * 2 ^ n"
  3.3229 -            using \<open>norm (b - a) < e * 2 ^ n\<close> by blast
  3.3230 -          finally show ?thesis .
  3.3231 -        qed
  3.3232 -        have D: "(a + n \<le> x \<and> x \<le> a + m) \<Longrightarrow> (a + n \<le> y \<and> y \<le> a + m) \<Longrightarrow> abs(x - y) \<le> m - n"
  3.3233 -                 for a m n x and y::real
  3.3234 -          by auto
  3.3235 -        have "\<forall>i\<in>Basis. \<exists>k<2 ^ n. (a \<bullet> i + real k * (b \<bullet> i - a \<bullet> i) / 2 ^ n \<le> x \<bullet> i \<and>
  3.3236 -               x \<bullet> i \<le> a \<bullet> i + (real k + 1) * (b \<bullet> i - a \<bullet> i) / 2 ^ n)"
  3.3237 -        proof
  3.3238 -          fix i::'a assume "i \<in> Basis"
  3.3239 -          consider "x \<bullet> i = b \<bullet> i" | "x \<bullet> i < b \<bullet> i"
  3.3240 -            using \<open>i \<in> Basis\<close> mem_box(2) xab by force
  3.3241 -          then show "\<exists>k<2 ^ n. (a \<bullet> i + real k * (b \<bullet> i - a \<bullet> i) / 2 ^ n \<le> x \<bullet> i \<and>
  3.3242 -                          x \<bullet> i \<le> a \<bullet> i + (real k + 1) * (b \<bullet> i - a \<bullet> i) / 2 ^ n)"
  3.3243 -          proof cases
  3.3244 -            case 1 then show ?thesis
  3.3245 -              by (rule_tac x = "2^n - 1" in exI) (auto simp: algebra_simps divide_simps of_nat_diff \<open>i \<in> Basis\<close> aibi)
  3.3246 -          next
  3.3247 -            case 2
  3.3248 -            then have abi_less: "a \<bullet> i < b \<bullet> i"
  3.3249 -              using \<open>i \<in> Basis\<close> xab by (auto simp: mem_box)
  3.3250 -            let ?k = "nat \<lfloor>2 ^ n * (x \<bullet> i - a \<bullet> i) / (b \<bullet> i - a \<bullet> i)\<rfloor>"
  3.3251 -            show ?thesis
  3.3252 -            proof (intro exI conjI)
  3.3253 -              show "?k < 2 ^ n"
  3.3254 -                using aibi xab \<open>i \<in> Basis\<close>
  3.3255 -                by (force simp: nat_less_iff floor_less_iff divide_simps 2 mem_box)
  3.3256 -            next
  3.3257 -              have "a \<bullet> i + real ?k * (b \<bullet> i - a \<bullet> i) / 2 ^ n \<le>
  3.3258 -                    a \<bullet> i + (2 ^ n * (x \<bullet> i - a \<bullet> i) / (b \<bullet> i - a \<bullet> i)) * (b \<bullet> i - a \<bullet> i) / 2 ^ n"
  3.3259 -                apply (intro add_left_mono mult_right_mono divide_right_mono of_nat_floor)
  3.3260 -                using aibi [OF \<open>i \<in> Basis\<close>] xab 2
  3.3261 -                  apply (simp_all add: \<open>i \<in> Basis\<close> mem_box divide_simps)
  3.3262 -                done
  3.3263 -              also have "... = x \<bullet> i"
  3.3264 -                using abi_less by (simp add: divide_simps)
  3.3265 -              finally show "a \<bullet> i + real ?k * (b \<bullet> i - a \<bullet> i) / 2 ^ n \<le> x \<bullet> i" .
  3.3266 -            next
  3.3267 -              have "x \<bullet> i \<le> a \<bullet> i + (2 ^ n * (x \<bullet> i - a \<bullet> i) / (b \<bullet> i - a \<bullet> i)) * (b \<bullet> i - a \<bullet> i) / 2 ^ n"
  3.3268 -                using abi_less by (simp add: divide_simps algebra_simps)
  3.3269 -              also have "... \<le> a \<bullet> i + (real ?k + 1) * (b \<bullet> i - a \<bullet> i) / 2 ^ n"
  3.3270 -                apply (intro add_left_mono mult_right_mono divide_right_mono of_nat_floor)
  3.3271 -                using aibi [OF \<open>i \<in> Basis\<close>] xab
  3.3272 -                  apply (auto simp: \<open>i \<in> Basis\<close> mem_box divide_simps)
  3.3273 -                done
  3.3274 -              finally show "x \<bullet> i \<le> a \<bullet> i + (real ?k + 1) * (b \<bullet> i - a \<bullet> i) / 2 ^ n" .
  3.3275 -            qed
  3.3276 -          qed
  3.3277 -        qed
  3.3278 -        then have "\<exists>f\<in>Basis \<rightarrow>\<^sub>E {..<2 ^ n}. x \<in> ?K0(n,f)"
  3.3279 -          apply (simp add: mem_box Bex_def)
  3.3280 -          apply (clarify dest!: bchoice)
  3.3281 -          apply (rule_tac x="restrict f Basis" in exI, simp)
  3.3282 -          done
  3.3283 -        moreover have "\<And>f. x \<in> ?K0(n,f) \<Longrightarrow> ?K0(n,f) \<subseteq> g x"
  3.3284 -          apply (clarsimp simp add: mem_box)
  3.3285 -          apply (rule e)
  3.3286 -          apply (drule bspec D, assumption)+
  3.3287 -          apply (erule order_trans)
  3.3288 -          apply (simp add: divide_simps)
  3.3289 -          using bai by (force simp: algebra_simps)
  3.3290 -        ultimately show ?thesis by auto
  3.3291 -      qed
  3.3292 -    qed
  3.3293 -  next
  3.3294 -    show "\<And>u v. cbox u v \<in> ?D0 \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  3.3295 -      by (force simp: eq_cbox box_eq_empty field_simps dest!: aibi)
  3.3296 -  next
  3.3297 -    obtain j::'a where "j \<in> Basis"
  3.3298 -      using nonempty_Basis by blast
  3.3299 -    have "finite {L \<in> ?D0. ?K0(n,f) \<subseteq> L}" if "f \<in> Basis \<rightarrow>\<^sub>E {..<2 ^ n}" for n f
  3.3300 -    proof (rule finite_subset)
  3.3301 -      let ?B = "(\<lambda>(n, f::'a\<Rightarrow>nat). cbox (\<Sum>i\<in>Basis. (a \<bullet> i + (f i) / 2^n * (b \<bullet> i - a \<bullet> i)) *\<^sub>R i)
  3.3302 -                                        (\<Sum>i\<in>Basis. (a \<bullet> i + ((f i) + 1) / 2^n * (b \<bullet> i - a \<bullet> i)) *\<^sub>R i))
  3.3303 -                ` (SIGMA m:atMost n. PiE Basis (\<lambda>i::'a. lessThan (2^m)))"
  3.3304 -      have "?K0(m,g) \<in> ?B" if "g \<in> Basis \<rightarrow>\<^sub>E {..<2 ^ m}" "?K0(n,f) \<subseteq> ?K0(m,g)" for m g
  3.3305 -      proof -
  3.3306 -        have dd: "w / m \<le> v / n \<and> (v+1) / n \<le> (w+1) / m
  3.3307 -                  \<Longrightarrow> inverse n \<le> inverse m" for w m v n::real
  3.3308 -          by (auto simp: divide_simps algebra_simps)
  3.3309 -        have bjaj: "b \<bullet> j - a \<bullet> j > 0"
  3.3310 -          using \<open>j \<in> Basis\<close> \<open>box a b \<noteq> {}\<close> box_eq_empty(1) by fastforce
  3.3311 -        have "((g j) / 2 ^ m) * (b \<bullet> j - a \<bullet> j) \<le> ((f j) / 2 ^ n) * (b \<bullet> j - a \<bullet> j) \<and>
  3.3312 -              (((f j) + 1) / 2 ^ n) * (b \<bullet> j - a \<bullet> j) \<le> (((g j) + 1) / 2 ^ m) * (b \<bullet> j - a \<bullet> j)"
  3.3313 -          using that \<open>j \<in> Basis\<close> by (simp add: subset_box algebra_simps divide_simps aibi)
  3.3314 -        then have "((g j) / 2 ^ m) \<le> ((f j) / 2 ^ n) \<and>
  3.3315 -          ((real(f j) + 1) / 2 ^ n) \<le> ((real(g j) + 1) / 2 ^ m)"
  3.3316 -          by (metis bjaj mult.commute of_nat_1 of_nat_add real_mult_le_cancel_iff2)
  3.3317 -        then have "inverse (2^n) \<le> (inverse (2^m) :: real)"
  3.3318 -          by (rule dd)
  3.3319 -        then have "m \<le> n"
  3.3320 -          by auto
  3.3321 -        show ?thesis
  3.3322 -          by (rule imageI) (simp add: \<open>m \<le> n\<close> that)
  3.3323 -      qed
  3.3324 -      then show "{L \<in> ?D0. ?K0(n,f) \<subseteq> L} \<subseteq> ?B"
  3.3325 -        by auto
  3.3326 -      show "finite ?B"
  3.3327 -        by (intro finite_imageI finite_SigmaI finite_atMost finite_lessThan finite_PiE finite_Basis)
  3.3328 -    qed
  3.3329 -    then show "finite {L \<in> ?D0. K \<subseteq> L}" if "K \<in> ?D0" for K
  3.3330 -      using that by auto
  3.3331 -  qed
  3.3332 -  let ?D1 = "{K \<in> \<D>0. \<exists>x \<in> S \<inter> K. K \<subseteq> g x}"
  3.3333 -  obtain \<D> where count: "countable \<D>"
  3.3334 -             and sub: "\<Union>\<D> \<subseteq> cbox a b"  "S \<subseteq> \<Union>\<D>"
  3.3335 -             and int:  "\<And>K. K \<in> \<D> \<Longrightarrow> (interior K \<noteq> {}) \<and> (\<exists>c d. K = cbox c d)"
  3.3336 -             and intdj: "\<And>A B. \<lbrakk>A \<in> \<D>; B \<in> \<D>\<rbrakk> \<Longrightarrow> A \<subseteq> B \<or> B \<subseteq> A \<or> interior A \<inter> interior B = {}"
  3.3337 -             and SK: "\<And>K. K \<in> \<D> \<Longrightarrow> \<exists>x. x \<in> S \<inter> K \<and> K \<subseteq> g x"
  3.3338 -             and cbox: "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  3.3339 -             and fin: "\<And>K. K \<in> \<D> \<Longrightarrow> finite {L. L \<in> \<D> \<and> K \<subseteq> L}"
  3.3340 -  proof
  3.3341 -    show "countable ?D1" using count countable_subset
  3.3342 -      by (simp add: count countable_subset)
  3.3343 -    show "\<Union>?D1 \<subseteq> cbox a b"
  3.3344 -      using sub by blast
  3.3345 -    show "S \<subseteq> \<Union>?D1"
  3.3346 -      using SK by (force simp:)
  3.3347 -    show "\<And>K. K \<in> ?D1 \<Longrightarrow> (interior K \<noteq> {}) \<and> (\<exists>c d. K = cbox c d)"
  3.3348 -      using int by blast
  3.3349 -    show "\<And>A B. \<lbrakk>A \<in> ?D1; B \<in> ?D1\<rbrakk> \<Longrightarrow> A \<subseteq> B \<or> B \<subseteq> A \<or> interior A \<inter> interior B = {}"
  3.3350 -      using intdj by blast
  3.3351 -    show "\<And>K. K \<in> ?D1 \<Longrightarrow> \<exists>x. x \<in> S \<inter> K \<and> K \<subseteq> g x"
  3.3352 -      by auto
  3.3353 -    show "\<And>u v. cbox u v \<in> ?D1 \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  3.3354 -      using cbox by blast
  3.3355 -    show "\<And>K. K \<in> ?D1 \<Longrightarrow> finite {L. L \<in> ?D1 \<and> K \<subseteq> L}"
  3.3356 -      using fin by simp (metis (mono_tags, lifting) Collect_mono rev_finite_subset)
  3.3357 -  qed
  3.3358 -  let ?\<D> = "{K \<in> \<D>. \<forall>K'. K' \<in> \<D> \<and> K \<noteq> K' \<longrightarrow> ~(K \<subseteq> K')}"
  3.3359 -  show ?thesis
  3.3360 -  proof (rule that)
  3.3361 -    show "countable ?\<D>"
  3.3362 -      by (blast intro: countable_subset [OF _ count])
  3.3363 -    show "\<Union>?\<D> \<subseteq> cbox a b"
  3.3364 -      using sub by blast
  3.3365 -    show "S \<subseteq> \<Union>?\<D>"
  3.3366 -    proof clarsimp
  3.3367 -      fix x
  3.3368 -      assume "x \<in> S"
  3.3369 -      then obtain X where "x \<in> X" "X \<in> \<D>" using \<open>S \<subseteq> \<Union>\<D>\<close> by blast
  3.3370 -      let ?R = "{(K,L). K \<in> \<D> \<and> L \<in> \<D> \<and> L \<subset> K}"
  3.3371 -      have irrR: "irrefl ?R" by (force simp: irrefl_def)
  3.3372 -      have traR: "trans ?R" by (force simp: trans_def)
  3.3373 -      have finR: "\<And>x. finite {y. (y, x) \<in> ?R}"
  3.3374 -        by simp (metis (mono_tags, lifting) fin \<open>X \<in> \<D>\<close> finite_subset mem_Collect_eq psubset_imp_subset subsetI)
  3.3375 -      have "{X \<in> \<D>. x \<in> X} \<noteq> {}"
  3.3376 -        using \<open>X \<in> \<D>\<close> \<open>x \<in> X\<close> by blast
  3.3377 -      then obtain Y where "Y \<in> {X \<in> \<D>. x \<in> X}" "\<And>Y'. (Y', Y) \<in> ?R \<Longrightarrow> Y' \<notin> {X \<in> \<D>. x \<in> X}"
  3.3378 -        by (rule wfE_min' [OF wf_finite_segments [OF irrR traR finR]]) blast
  3.3379 -      then show "\<exists>Y. Y \<in> \<D> \<and> (\<forall>K'. K' \<in> \<D> \<and> Y \<noteq> K' \<longrightarrow> \<not> Y \<subseteq> K') \<and> x \<in> Y"
  3.3380 -        by blast
  3.3381 -    qed
  3.3382 -    show "\<And>K. K \<in> ?\<D> \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  3.3383 -      by (blast intro: dest: int)
  3.3384 -    show "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) ?\<D>"
  3.3385 -      using intdj by (simp add: pairwise_def) metis
  3.3386 -    show "\<And>K. K \<in> ?\<D> \<Longrightarrow> \<exists>x \<in> S \<inter> K. K \<subseteq> g x"
  3.3387 -      using SK by force
  3.3388 -    show "\<And>u v. cbox u v \<in> ?\<D> \<Longrightarrow> \<exists>n. \<forall>i\<in>Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  3.3389 -      using cbox by force
  3.3390 -    qed
  3.3391 -qed
  3.3392 -
  3.3393  subsubsection \<open>Adaption to ordered Euclidean spaces and the Cartesian Euclidean space\<close>
  3.3394  
  3.3395  lemma integral_component_eq_cart[simp]:
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/HOL/Analysis/Tagged_Division.thy	Thu Sep 29 13:02:43 2016 +0200
     4.3 @@ -0,0 +1,2817 @@
     4.4 +(*  Title:      HOL/Analysis/Tagged_Division.thy
     4.5 +    Author:     John Harrison
     4.6 +    Author:     Robert Himmelmann, TU Muenchen (Translation from HOL light); proofs reworked by LCP
     4.7 +*)
     4.8 +
     4.9 +section \<open>Tagged divisions used for the Henstock-Kurzweil gauge integration\<close>
    4.10 +
    4.11 +theory Tagged_Division
    4.12 +imports
    4.13 +  Topology_Euclidean_Space
    4.14 +begin
    4.15 +
    4.16 +lemma finite_product_dependent:
    4.17 +  assumes "finite s"
    4.18 +    and "\<And>x. x \<in> s \<Longrightarrow> finite (t x)"
    4.19 +  shows "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
    4.20 +  using assms
    4.21 +proof induct
    4.22 +  case (insert x s)
    4.23 +  have *: "{(i, j) |i j. i \<in> insert x s \<and> j \<in> t i} =
    4.24 +    (\<lambda>y. (x,y)) ` (t x) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
    4.25 +  show ?case
    4.26 +    unfolding *
    4.27 +    apply (rule finite_UnI)
    4.28 +    using insert
    4.29 +    apply auto
    4.30 +    done
    4.31 +qed auto
    4.32 +
    4.33 +lemma sum_sum_product:
    4.34 +  assumes "finite s"
    4.35 +    and "\<forall>i\<in>s. finite (t i)"
    4.36 +  shows "setsum (\<lambda>i. setsum (x i) (t i)::real) s =
    4.37 +    setsum (\<lambda>(i,j). x i j) {(i,j) | i j. i \<in> s \<and> j \<in> t i}"
    4.38 +  using assms
    4.39 +proof induct
    4.40 +  case (insert a s)
    4.41 +  have *: "{(i, j) |i j. i \<in> insert a s \<and> j \<in> t i} =
    4.42 +    (\<lambda>y. (a,y)) ` (t a) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
    4.43 +  show ?case
    4.44 +    unfolding *
    4.45 +    apply (subst setsum.union_disjoint)
    4.46 +    unfolding setsum.insert[OF insert(1-2)]
    4.47 +    prefer 4
    4.48 +    apply (subst insert(3))
    4.49 +    unfolding add_right_cancel
    4.50 +  proof -
    4.51 +    show "setsum (x a) (t a) = (\<Sum>(xa, y)\<in> Pair a ` t a. x xa y)"
    4.52 +      apply (subst setsum.reindex)
    4.53 +      unfolding inj_on_def
    4.54 +      apply auto
    4.55 +      done
    4.56 +    show "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
    4.57 +      apply (rule finite_product_dependent)
    4.58 +      using insert
    4.59 +      apply auto
    4.60 +      done
    4.61 +  qed (insert insert, auto)
    4.62 +qed auto
    4.63 +
    4.64 +lemmas scaleR_simps = scaleR_zero_left scaleR_minus_left scaleR_left_diff_distrib
    4.65 +  scaleR_zero_right scaleR_minus_right scaleR_right_diff_distrib scaleR_eq_0_iff
    4.66 +  scaleR_cancel_left scaleR_cancel_right scaleR_add_right scaleR_add_left real_vector_class.scaleR_one
    4.67 +
    4.68 +
    4.69 +subsection \<open>Sundries\<close>
    4.70 +
    4.71 +
    4.72 +text\<open>A transitive relation is well-founded if all initial segments are finite.\<close>
    4.73 +lemma wf_finite_segments:
    4.74 +  assumes "irrefl r" and "trans r" and "\<And>x. finite {y. (y, x) \<in> r}"
    4.75 +  shows "wf (r)"
    4.76 +  apply (simp add: trans_wf_iff wf_iff_acyclic_if_finite converse_def assms)
    4.77 +  using acyclic_def assms irrefl_def trans_Restr by fastforce
    4.78 +
    4.79 +text\<open>For creating values between @{term u} and @{term v}.\<close>
    4.80 +lemma scaling_mono:
    4.81 +  fixes u::"'a::linordered_field"
    4.82 +  assumes "u \<le> v" "0 \<le> r" "r \<le> s"
    4.83 +    shows "u + r * (v - u) / s \<le> v"
    4.84 +proof -
    4.85 +  have "r/s \<le> 1" using assms
    4.86 +    using divide_le_eq_1 by fastforce
    4.87 +  then have "(r/s) * (v - u) \<le> 1 * (v - u)"
    4.88 +    by (meson diff_ge_0_iff_ge mult_right_mono \<open>u \<le> v\<close>)
    4.89 +  then show ?thesis
    4.90 +    by (simp add: field_simps)
    4.91 +qed
    4.92 +
    4.93 +lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
    4.94 +lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
    4.95 +lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
    4.96 +
    4.97 +lemma cond_cases: "(P \<Longrightarrow> Q x) \<Longrightarrow> (\<not> P \<Longrightarrow> Q y) \<Longrightarrow> Q (if P then x else y)"
    4.98 +  by auto
    4.99 +
   4.100 +declare norm_triangle_ineq4[intro]
   4.101 +
   4.102 +lemma transitive_stepwise_le:
   4.103 +  assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" and "\<And>n. R n (Suc n)"
   4.104 +  shows "\<forall>n\<ge>m. R m n"
   4.105 +proof (intro allI impI)
   4.106 +  show "m \<le> n \<Longrightarrow> R m n" for n
   4.107 +    by (induction rule: dec_induct)
   4.108 +       (use assms in blast)+
   4.109 +qed
   4.110 +
   4.111 +subsection \<open>Some useful lemmas about intervals.\<close>
   4.112 +
   4.113 +lemma interior_subset_union_intervals:
   4.114 +  assumes "i = cbox a b"
   4.115 +    and "j = cbox c d"
   4.116 +    and "interior j \<noteq> {}"
   4.117 +    and "i \<subseteq> j \<union> s"
   4.118 +    and "interior i \<inter> interior j = {}"
   4.119 +  shows "interior i \<subseteq> interior s"
   4.120 +proof -
   4.121 +  have "box a b \<inter> cbox c d = {}"
   4.122 +     using inter_interval_mixed_eq_empty[of c d a b] and assms(3,5)
   4.123 +     unfolding assms(1,2) interior_cbox by auto
   4.124 +  moreover
   4.125 +  have "box a b \<subseteq> cbox c d \<union> s"
   4.126 +    apply (rule order_trans,rule box_subset_cbox)
   4.127 +    using assms(4) unfolding assms(1,2)
   4.128 +    apply auto
   4.129 +    done
   4.130 +  ultimately
   4.131 +  show ?thesis
   4.132 +    unfolding assms interior_cbox
   4.133 +      by auto (metis IntI UnE empty_iff interior_maximal open_box subsetCE subsetI)
   4.134 +qed
   4.135 +
   4.136 +lemma interior_Union_subset_cbox:
   4.137 +  assumes "finite f"
   4.138 +  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"
   4.139 +    and t: "closed t"
   4.140 +  shows "interior (\<Union>f) \<subseteq> t"
   4.141 +proof -
   4.142 +  have [simp]: "s \<in> f \<Longrightarrow> closed s" for s
   4.143 +    using f by auto
   4.144 +  define E where "E = {s\<in>f. interior s = {}}"
   4.145 +  then have "finite E" "E \<subseteq> {s\<in>f. interior s = {}}"
   4.146 +    using \<open>finite f\<close> by auto
   4.147 +  then have "interior (\<Union>f) = interior (\<Union>(f - E))"
   4.148 +  proof (induction E rule: finite_subset_induct')
   4.149 +    case (insert s f')
   4.150 +    have "interior (\<Union>(f - insert s f') \<union> s) = interior (\<Union>(f - insert s f'))"
   4.151 +      using insert.hyps \<open>finite f\<close> by (intro interior_closed_Un_empty_interior) auto
   4.152 +    also have "\<Union>(f - insert s f') \<union> s = \<Union>(f - f')"
   4.153 +      using insert.hyps by auto
   4.154 +    finally show ?case
   4.155 +      by (simp add: insert.IH)
   4.156 +  qed simp
   4.157 +  also have "\<dots> \<subseteq> \<Union>(f - E)"
   4.158 +    by (rule interior_subset)
   4.159 +  also have "\<dots> \<subseteq> t"
   4.160 +  proof (rule Union_least)
   4.161 +    fix s assume "s \<in> f - E"
   4.162 +    with f[of s] obtain a b where s: "s \<in> f" "s = cbox a b" "box a b \<noteq> {}"
   4.163 +      by (fastforce simp: E_def)
   4.164 +    have "closure (interior s) \<subseteq> closure t"
   4.165 +      by (intro closure_mono f \<open>s \<in> f\<close>)
   4.166 +    with s \<open>closed t\<close> show "s \<subseteq> t"
   4.167 +      by simp
   4.168 +  qed
   4.169 +  finally show ?thesis .
   4.170 +qed
   4.171 +
   4.172 +lemma inter_interior_unions_intervals:
   4.173 +    "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) = {}"
   4.174 +  using interior_Union_subset_cbox[of f "UNIV - s"] by auto
   4.175 +
   4.176 +lemma interval_split:
   4.177 +  fixes a :: "'a::euclidean_space"
   4.178 +  assumes "k \<in> Basis"
   4.179 +  shows
   4.180 +    "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)"
   4.181 +    "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"
   4.182 +  apply (rule_tac[!] set_eqI)
   4.183 +  unfolding Int_iff mem_box mem_Collect_eq
   4.184 +  using assms
   4.185 +  apply auto
   4.186 +  done
   4.187 +
   4.188 +lemma interval_not_empty: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow> cbox a b \<noteq> {}"
   4.189 +  by (simp add: box_ne_empty)
   4.190 +
   4.191 +subsection \<open>Bounds on intervals where they exist.\<close>
   4.192 +
   4.193 +definition interval_upperbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
   4.194 +  where "interval_upperbound s = (\<Sum>i\<in>Basis. (SUP x:s. x\<bullet>i) *\<^sub>R i)"
   4.195 +
   4.196 +definition interval_lowerbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
   4.197 +  where "interval_lowerbound s = (\<Sum>i\<in>Basis. (INF x:s. x\<bullet>i) *\<^sub>R i)"
   4.198 +
   4.199 +lemma interval_upperbound[simp]:
   4.200 +  "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
   4.201 +    interval_upperbound (cbox a b) = (b::'a::euclidean_space)"
   4.202 +  unfolding interval_upperbound_def euclidean_representation_setsum cbox_def
   4.203 +  by (safe intro!: cSup_eq) auto
   4.204 +
   4.205 +lemma interval_lowerbound[simp]:
   4.206 +  "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
   4.207 +    interval_lowerbound (cbox a b) = (a::'a::euclidean_space)"
   4.208 +  unfolding interval_lowerbound_def euclidean_representation_setsum cbox_def
   4.209 +  by (safe intro!: cInf_eq) auto
   4.210 +
   4.211 +lemmas interval_bounds = interval_upperbound interval_lowerbound
   4.212 +
   4.213 +lemma
   4.214 +  fixes X::"real set"
   4.215 +  shows interval_upperbound_real[simp]: "interval_upperbound X = Sup X"
   4.216 +    and interval_lowerbound_real[simp]: "interval_lowerbound X = Inf X"
   4.217 +  by (auto simp: interval_upperbound_def interval_lowerbound_def)
   4.218 +
   4.219 +lemma interval_bounds'[simp]:
   4.220 +  assumes "cbox a b \<noteq> {}"
   4.221 +  shows "interval_upperbound (cbox a b) = b"
   4.222 +    and "interval_lowerbound (cbox a b) = a"
   4.223 +  using assms unfolding box_ne_empty by auto
   4.224 +
   4.225 +lemma interval_upperbound_Times:
   4.226 +  assumes "A \<noteq> {}" and "B \<noteq> {}"
   4.227 +  shows "interval_upperbound (A \<times> B) = (interval_upperbound A, interval_upperbound B)"
   4.228 +proof-
   4.229 +  from assms have fst_image_times': "A = fst ` (A \<times> B)" by simp
   4.230 +  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)"
   4.231 +      by (subst (2) fst_image_times') (simp del: fst_image_times add: o_def inner_Pair_0)
   4.232 +  moreover from assms have snd_image_times': "B = snd ` (A \<times> B)" by simp
   4.233 +  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)"
   4.234 +      by (subst (2) snd_image_times') (simp del: snd_image_times add: o_def inner_Pair_0)
   4.235 +  ultimately show ?thesis unfolding interval_upperbound_def
   4.236 +      by (subst setsum_Basis_prod_eq) (auto simp add: setsum_prod)
   4.237 +qed
   4.238 +
   4.239 +lemma interval_lowerbound_Times:
   4.240 +  assumes "A \<noteq> {}" and "B \<noteq> {}"
   4.241 +  shows "interval_lowerbound (A \<times> B) = (interval_lowerbound A, interval_lowerbound B)"
   4.242 +proof-
   4.243 +  from assms have fst_image_times': "A = fst ` (A \<times> B)" by simp
   4.244 +  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)"
   4.245 +      by (subst (2) fst_image_times') (simp del: fst_image_times add: o_def inner_Pair_0)
   4.246 +  moreover from assms have snd_image_times': "B = snd ` (A \<times> B)" by simp
   4.247 +  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)"
   4.248 +      by (subst (2) snd_image_times') (simp del: snd_image_times add: o_def inner_Pair_0)
   4.249 +  ultimately show ?thesis unfolding interval_lowerbound_def
   4.250 +      by (subst setsum_Basis_prod_eq) (auto simp add: setsum_prod)
   4.251 +qed
   4.252 +
   4.253 +subsection \<open>The notion of a gauge --- simply an open set containing the point.\<close>
   4.254 +
   4.255 +definition "gauge d \<longleftrightarrow> (\<forall>x. x \<in> d x \<and> open (d x))"
   4.256 +
   4.257 +lemma gaugeI:
   4.258 +  assumes "\<And>x. x \<in> g x"
   4.259 +    and "\<And>x. open (g x)"
   4.260 +  shows "gauge g"
   4.261 +  using assms unfolding gauge_def by auto
   4.262 +
   4.263 +lemma gaugeD[dest]:
   4.264 +  assumes "gauge d"
   4.265 +  shows "x \<in> d x"
   4.266 +    and "open (d x)"
   4.267 +  using assms unfolding gauge_def by auto
   4.268 +
   4.269 +lemma gauge_ball_dependent: "\<forall>x. 0 < e x \<Longrightarrow> gauge (\<lambda>x. ball x (e x))"
   4.270 +  unfolding gauge_def by auto
   4.271 +
   4.272 +lemma gauge_ball[intro]: "0 < e \<Longrightarrow> gauge (\<lambda>x. ball x e)"
   4.273 +  unfolding gauge_def by auto
   4.274 +
   4.275 +lemma gauge_trivial[intro!]: "gauge (\<lambda>x. ball x 1)"
   4.276 +  by (rule gauge_ball) auto
   4.277 +
   4.278 +lemma gauge_inter[intro]: "gauge d1 \<Longrightarrow> gauge d2 \<Longrightarrow> gauge (\<lambda>x. d1 x \<inter> d2 x)"
   4.279 +  unfolding gauge_def by auto
   4.280 +
   4.281 +lemma gauge_inters:
   4.282 +  assumes "finite s"
   4.283 +    and "\<forall>d\<in>s. gauge (f d)"
   4.284 +  shows "gauge (\<lambda>x. \<Inter>{f d x | d. d \<in> s})"
   4.285 +proof -
   4.286 +  have *: "\<And>x. {f d x |d. d \<in> s} = (\<lambda>d. f d x) ` s"
   4.287 +    by auto
   4.288 +  show ?thesis
   4.289 +    unfolding gauge_def unfolding *
   4.290 +    using assms unfolding Ball_def Inter_iff mem_Collect_eq gauge_def by auto
   4.291 +qed
   4.292 +
   4.293 +lemma gauge_existence_lemma:
   4.294 +  "(\<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)"
   4.295 +  by (metis zero_less_one)
   4.296 +
   4.297 +subsection \<open>Attempt a systematic general set of "offset" results for components.\<close>
   4.298 +
   4.299 +lemma gauge_modify:
   4.300 +  assumes "(\<forall>s. open s \<longrightarrow> open {x. f(x) \<in> s})" "gauge d"
   4.301 +  shows "gauge (\<lambda>x. {y. f y \<in> d (f x)})"
   4.302 +  using assms
   4.303 +  unfolding gauge_def
   4.304 +  apply safe
   4.305 +  defer
   4.306 +  apply (erule_tac x="f x" in allE)
   4.307 +  apply (erule_tac x="d (f x)" in allE)
   4.308 +  apply auto
   4.309 +  done
   4.310 +
   4.311 +subsection \<open>Divisions.\<close>
   4.312 +
   4.313 +definition division_of (infixl "division'_of" 40)
   4.314 +where
   4.315 +  "s division_of i \<longleftrightarrow>
   4.316 +    finite s \<and>
   4.317 +    (\<forall>k\<in>s. k \<subseteq> i \<and> k \<noteq> {} \<and> (\<exists>a b. k = cbox a b)) \<and>
   4.318 +    (\<forall>k1\<in>s. \<forall>k2\<in>s. k1 \<noteq> k2 \<longrightarrow> interior(k1) \<inter> interior(k2) = {}) \<and>
   4.319 +    (\<Union>s = i)"
   4.320 +
   4.321 +lemma division_ofD[dest]:
   4.322 +  assumes "s division_of i"
   4.323 +  shows "finite s"
   4.324 +    and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
   4.325 +    and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
   4.326 +    and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
   4.327 +    and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}"
   4.328 +    and "\<Union>s = i"
   4.329 +  using assms unfolding division_of_def by auto
   4.330 +
   4.331 +lemma division_ofI:
   4.332 +  assumes "finite s"
   4.333 +    and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
   4.334 +    and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
   4.335 +    and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
   4.336 +    and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
   4.337 +    and "\<Union>s = i"
   4.338 +  shows "s division_of i"
   4.339 +  using assms unfolding division_of_def by auto
   4.340 +
   4.341 +lemma division_of_finite: "s division_of i \<Longrightarrow> finite s"
   4.342 +  unfolding division_of_def by auto
   4.343 +
   4.344 +lemma division_of_self[intro]: "cbox a b \<noteq> {} \<Longrightarrow> {cbox a b} division_of (cbox a b)"
   4.345 +  unfolding division_of_def by auto
   4.346 +
   4.347 +lemma division_of_trivial[simp]: "s division_of {} \<longleftrightarrow> s = {}"
   4.348 +  unfolding division_of_def by auto
   4.349 +
   4.350 +lemma division_of_sing[simp]:
   4.351 +  "s division_of cbox a (a::'a::euclidean_space) \<longleftrightarrow> s = {cbox a a}"
   4.352 +  (is "?l = ?r")
   4.353 +proof
   4.354 +  assume ?r
   4.355 +  moreover
   4.356 +  { fix k
   4.357 +    assume "s = {{a}}" "k\<in>s"
   4.358 +    then have "\<exists>x y. k = cbox x y"
   4.359 +      apply (rule_tac x=a in exI)+
   4.360 +      apply (force simp: cbox_sing)
   4.361 +      done
   4.362 +  }
   4.363 +  ultimately show ?l
   4.364 +    unfolding division_of_def cbox_sing by auto
   4.365 +next
   4.366 +  assume ?l
   4.367 +  note * = conjunctD4[OF this[unfolded division_of_def cbox_sing]]
   4.368 +  {
   4.369 +    fix x
   4.370 +    assume x: "x \<in> s" have "x = {a}"
   4.371 +      using *(2)[rule_format,OF x] by auto
   4.372 +  }
   4.373 +  moreover have "s \<noteq> {}"
   4.374 +    using *(4) by auto
   4.375 +  ultimately show ?r
   4.376 +    unfolding cbox_sing by auto
   4.377 +qed
   4.378 +
   4.379 +lemma elementary_empty: obtains p where "p division_of {}"
   4.380 +  unfolding division_of_trivial by auto
   4.381 +
   4.382 +lemma elementary_interval: obtains p where "p division_of (cbox a b)"
   4.383 +  by (metis division_of_trivial division_of_self)
   4.384 +
   4.385 +lemma division_contains: "s division_of i \<Longrightarrow> \<forall>x\<in>i. \<exists>k\<in>s. x \<in> k"
   4.386 +  unfolding division_of_def by auto
   4.387 +
   4.388 +lemma forall_in_division:
   4.389 +  "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))"
   4.390 +  unfolding division_of_def by fastforce
   4.391 +
   4.392 +lemma division_of_subset:
   4.393 +  assumes "p division_of (\<Union>p)"
   4.394 +    and "q \<subseteq> p"
   4.395 +  shows "q division_of (\<Union>q)"
   4.396 +proof (rule division_ofI)
   4.397 +  note * = division_ofD[OF assms(1)]
   4.398 +  show "finite q"
   4.399 +    using "*"(1) assms(2) infinite_super by auto
   4.400 +  {
   4.401 +    fix k
   4.402 +    assume "k \<in> q"
   4.403 +    then have kp: "k \<in> p"
   4.404 +      using assms(2) by auto
   4.405 +    show "k \<subseteq> \<Union>q"
   4.406 +      using \<open>k \<in> q\<close> by auto
   4.407 +    show "\<exists>a b. k = cbox a b"
   4.408 +      using *(4)[OF kp] by auto
   4.409 +    show "k \<noteq> {}"
   4.410 +      using *(3)[OF kp] by auto
   4.411 +  }
   4.412 +  fix k1 k2
   4.413 +  assume "k1 \<in> q" "k2 \<in> q" "k1 \<noteq> k2"
   4.414 +  then have **: "k1 \<in> p" "k2 \<in> p" "k1 \<noteq> k2"
   4.415 +    using assms(2) by auto
   4.416 +  show "interior k1 \<inter> interior k2 = {}"
   4.417 +    using *(5)[OF **] by auto
   4.418 +qed auto
   4.419 +
   4.420 +lemma division_of_union_self[intro]: "p division_of s \<Longrightarrow> p division_of (\<Union>p)"
   4.421 +  unfolding division_of_def by auto
   4.422 +
   4.423 +lemma division_inter:
   4.424 +  fixes s1 s2 :: "'a::euclidean_space set"
   4.425 +  assumes "p1 division_of s1"
   4.426 +    and "p2 division_of s2"
   4.427 +  shows "{k1 \<inter> k2 | k1 k2. k1 \<in> p1 \<and> k2 \<in> p2 \<and> k1 \<inter> k2 \<noteq> {}} division_of (s1 \<inter> s2)"
   4.428 +  (is "?A' division_of _")
   4.429 +proof -
   4.430 +  let ?A = "{s. s \<in>  (\<lambda>(k1,k2). k1 \<inter> k2) ` (p1 \<times> p2) \<and> s \<noteq> {}}"
   4.431 +  have *: "?A' = ?A" by auto
   4.432 +  show ?thesis
   4.433 +    unfolding *
   4.434 +  proof (rule division_ofI)
   4.435 +    have "?A \<subseteq> (\<lambda>(x, y). x \<inter> y) ` (p1 \<times> p2)"
   4.436 +      by auto
   4.437 +    moreover have "finite (p1 \<times> p2)"
   4.438 +      using assms unfolding division_of_def by auto
   4.439 +    ultimately show "finite ?A" by auto
   4.440 +    have *: "\<And>s. \<Union>{x\<in>s. x \<noteq> {}} = \<Union>s"
   4.441 +      by auto
   4.442 +    show "\<Union>?A = s1 \<inter> s2"
   4.443 +      apply (rule set_eqI)
   4.444 +      unfolding * and UN_iff
   4.445 +      using division_ofD(6)[OF assms(1)] and division_ofD(6)[OF assms(2)]
   4.446 +      apply auto
   4.447 +      done
   4.448 +    {
   4.449 +      fix k
   4.450 +      assume "k \<in> ?A"
   4.451 +      then obtain k1 k2 where k: "k = k1 \<inter> k2" "k1 \<in> p1" "k2 \<in> p2" "k \<noteq> {}"
   4.452 +        by auto
   4.453 +      then show "k \<noteq> {}"
   4.454 +        by auto
   4.455 +      show "k \<subseteq> s1 \<inter> s2"
   4.456 +        using division_ofD(2)[OF assms(1) k(2)] and division_ofD(2)[OF assms(2) k(3)]
   4.457 +        unfolding k by auto
   4.458 +      obtain a1 b1 where k1: "k1 = cbox a1 b1"
   4.459 +        using division_ofD(4)[OF assms(1) k(2)] by blast
   4.460 +      obtain a2 b2 where k2: "k2 = cbox a2 b2"
   4.461 +        using division_ofD(4)[OF assms(2) k(3)] by blast
   4.462 +      show "\<exists>a b. k = cbox a b"
   4.463 +        unfolding k k1 k2 unfolding Int_interval by auto
   4.464 +    }
   4.465 +    fix k1 k2
   4.466 +    assume "k1 \<in> ?A"
   4.467 +    then obtain x1 y1 where k1: "k1 = x1 \<inter> y1" "x1 \<in> p1" "y1 \<in> p2" "k1 \<noteq> {}"
   4.468 +      by auto
   4.469 +    assume "k2 \<in> ?A"
   4.470 +    then obtain x2 y2 where k2: "k2 = x2 \<inter> y2" "x2 \<in> p1" "y2 \<in> p2" "k2 \<noteq> {}"
   4.471 +      by auto
   4.472 +    assume "k1 \<noteq> k2"
   4.473 +    then have th: "x1 \<noteq> x2 \<or> y1 \<noteq> y2"
   4.474 +      unfolding k1 k2 by auto
   4.475 +    have *: "interior x1 \<inter> interior x2 = {} \<or> interior y1 \<inter> interior y2 = {} \<Longrightarrow>
   4.476 +      interior (x1 \<inter> y1) \<subseteq> interior x1 \<Longrightarrow> interior (x1 \<inter> y1) \<subseteq> interior y1 \<Longrightarrow>
   4.477 +      interior (x2 \<inter> y2) \<subseteq> interior x2 \<Longrightarrow> interior (x2 \<inter> y2) \<subseteq> interior y2 \<Longrightarrow>
   4.478 +      interior (x1 \<inter> y1) \<inter> interior (x2 \<inter> y2) = {}" by auto
   4.479 +    show "interior k1 \<inter> interior k2 = {}"
   4.480 +      unfolding k1 k2
   4.481 +      apply (rule *)
   4.482 +      using assms division_ofD(5) k1 k2(2) k2(3) th apply auto
   4.483 +      done
   4.484 +  qed
   4.485 +qed
   4.486 +
   4.487 +lemma division_inter_1:
   4.488 +  assumes "d division_of i"
   4.489 +    and "cbox a (b::'a::euclidean_space) \<subseteq> i"
   4.490 +  shows "{cbox a b \<inter> k | k. k \<in> d \<and> cbox a b \<inter> k \<noteq> {}} division_of (cbox a b)"
   4.491 +proof (cases "cbox a b = {}")
   4.492 +  case True
   4.493 +  show ?thesis
   4.494 +    unfolding True and division_of_trivial by auto
   4.495 +next
   4.496 +  case False
   4.497 +  have *: "cbox a b \<inter> i = cbox a b" using assms(2) by auto
   4.498 +  show ?thesis
   4.499 +    using division_inter[OF division_of_self[OF False] assms(1)]
   4.500 +    unfolding * by auto
   4.501 +qed
   4.502 +
   4.503 +lemma elementary_inter:
   4.504 +  fixes s t :: "'a::euclidean_space set"
   4.505 +  assumes "p1 division_of s"
   4.506 +    and "p2 division_of t"
   4.507 +  shows "\<exists>p. p division_of (s \<inter> t)"
   4.508 +using assms division_inter by blast
   4.509 +
   4.510 +lemma elementary_inters:
   4.511 +  assumes "finite f"
   4.512 +    and "f \<noteq> {}"
   4.513 +    and "\<forall>s\<in>f. \<exists>p. p division_of (s::('a::euclidean_space) set)"
   4.514 +  shows "\<exists>p. p division_of (\<Inter>f)"
   4.515 +  using assms
   4.516 +proof (induct f rule: finite_induct)
   4.517 +  case (insert x f)
   4.518 +  show ?case
   4.519 +  proof (cases "f = {}")
   4.520 +    case True
   4.521 +    then show ?thesis
   4.522 +      unfolding True using insert by auto
   4.523 +  next
   4.524 +    case False
   4.525 +    obtain p where "p division_of \<Inter>f"
   4.526 +      using insert(3)[OF False insert(5)[unfolded ball_simps,THEN conjunct2]] ..
   4.527 +    moreover obtain px where "px division_of x"
   4.528 +      using insert(5)[rule_format,OF insertI1] ..
   4.529 +    ultimately show ?thesis
   4.530 +      by (simp add: elementary_inter Inter_insert)
   4.531 +  qed
   4.532 +qed auto
   4.533 +
   4.534 +lemma division_disjoint_union:
   4.535 +  assumes "p1 division_of s1"
   4.536 +    and "p2 division_of s2"
   4.537 +    and "interior s1 \<inter> interior s2 = {}"
   4.538 +  shows "(p1 \<union> p2) division_of (s1 \<union> s2)"
   4.539 +proof (rule division_ofI)
   4.540 +  note d1 = division_ofD[OF assms(1)]
   4.541 +  note d2 = division_ofD[OF assms(2)]
   4.542 +  show "finite (p1 \<union> p2)"
   4.543 +    using d1(1) d2(1) by auto
   4.544 +  show "\<Union>(p1 \<union> p2) = s1 \<union> s2"
   4.545 +    using d1(6) d2(6) by auto
   4.546 +  {
   4.547 +    fix k1 k2
   4.548 +    assume as: "k1 \<in> p1 \<union> p2" "k2 \<in> p1 \<union> p2" "k1 \<noteq> k2"
   4.549 +    moreover
   4.550 +    let ?g="interior k1 \<inter> interior k2 = {}"
   4.551 +    {
   4.552 +      assume as: "k1\<in>p1" "k2\<in>p2"
   4.553 +      have ?g
   4.554 +        using interior_mono[OF d1(2)[OF as(1)]] interior_mono[OF d2(2)[OF as(2)]]
   4.555 +        using assms(3) by blast
   4.556 +    }
   4.557 +    moreover
   4.558 +    {
   4.559 +      assume as: "k1\<in>p2" "k2\<in>p1"
   4.560 +      have ?g
   4.561 +        using interior_mono[OF d1(2)[OF as(2)]] interior_mono[OF d2(2)[OF as(1)]]
   4.562 +        using assms(3) by blast
   4.563 +    }
   4.564 +    ultimately show ?g
   4.565 +      using d1(5)[OF _ _ as(3)] and d2(5)[OF _ _ as(3)] by auto
   4.566 +  }
   4.567 +  fix k
   4.568 +  assume k: "k \<in> p1 \<union> p2"
   4.569 +  show "k \<subseteq> s1 \<union> s2"
   4.570 +    using k d1(2) d2(2) by auto
   4.571 +  show "k \<noteq> {}"
   4.572 +    using k d1(3) d2(3) by auto
   4.573 +  show "\<exists>a b. k = cbox a b"
   4.574 +    using k d1(4) d2(4) by auto
   4.575 +qed
   4.576 +
   4.577 +lemma partial_division_extend_1:
   4.578 +  fixes a b c d :: "'a::euclidean_space"
   4.579 +  assumes incl: "cbox c d \<subseteq> cbox a b"
   4.580 +    and nonempty: "cbox c d \<noteq> {}"
   4.581 +  obtains p where "p division_of (cbox a b)" "cbox c d \<in> p"
   4.582 +proof
   4.583 +  let ?B = "\<lambda>f::'a\<Rightarrow>'a \<times> 'a.
   4.584 +    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)"
   4.585 +  define p where "p = ?B ` (Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)})"
   4.586 +
   4.587 +  show "cbox c d \<in> p"
   4.588 +    unfolding p_def
   4.589 +    by (auto simp add: box_eq_empty cbox_def intro!: image_eqI[where x="\<lambda>(i::'a)\<in>Basis. (c, d)"])
   4.590 +  {
   4.591 +    fix i :: 'a
   4.592 +    assume "i \<in> Basis"
   4.593 +    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"
   4.594 +      unfolding box_eq_empty subset_box by (auto simp: not_le)
   4.595 +  }
   4.596 +  note ord = this
   4.597 +
   4.598 +  show "p division_of (cbox a b)"
   4.599 +  proof (rule division_ofI)
   4.600 +    show "finite p"
   4.601 +      unfolding p_def by (auto intro!: finite_PiE)
   4.602 +    {
   4.603 +      fix k
   4.604 +      assume "k \<in> p"
   4.605 +      then obtain f where f: "f \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and k: "k = ?B f"
   4.606 +        by (auto simp: p_def)
   4.607 +      then show "\<exists>a b. k = cbox a b"
   4.608 +        by auto
   4.609 +      have "k \<subseteq> cbox a b \<and> k \<noteq> {}"
   4.610 +      proof (simp add: k box_eq_empty subset_box not_less, safe)
   4.611 +        fix i :: 'a
   4.612 +        assume i: "i \<in> Basis"
   4.613 +        with f have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
   4.614 +          by (auto simp: PiE_iff)
   4.615 +        with i ord[of i]
   4.616 +        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"
   4.617 +          by auto
   4.618 +      qed
   4.619 +      then show "k \<noteq> {}" "k \<subseteq> cbox a b"
   4.620 +        by auto
   4.621 +      {
   4.622 +        fix l
   4.623 +        assume "l \<in> p"
   4.624 +        then obtain g where g: "g \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and l: "l = ?B g"
   4.625 +          by (auto simp: p_def)
   4.626 +        assume "l \<noteq> k"
   4.627 +        have "\<exists>i\<in>Basis. f i \<noteq> g i"
   4.628 +        proof (rule ccontr)
   4.629 +          assume "\<not> ?thesis"
   4.630 +          with f g have "f = g"
   4.631 +            by (auto simp: PiE_iff extensional_def fun_eq_iff)
   4.632 +          with \<open>l \<noteq> k\<close> show False
   4.633 +            by (simp add: l k)
   4.634 +        qed
   4.635 +        then obtain i where *: "i \<in> Basis" "f i \<noteq> g i" ..
   4.636 +        then have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
   4.637 +                  "g i = (a, c) \<or> g i = (c, d) \<or> g i = (d, b)"
   4.638 +          using f g by (auto simp: PiE_iff)
   4.639 +        with * ord[of i] show "interior l \<inter> interior k = {}"
   4.640 +          by (auto simp add: l k interior_cbox disjoint_interval intro!: bexI[of _ i])
   4.641 +      }
   4.642 +      note \<open>k \<subseteq> cbox a b\<close>
   4.643 +    }
   4.644 +    moreover
   4.645 +    {
   4.646 +      fix x assume x: "x \<in> cbox a b"
   4.647 +      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)}"
   4.648 +      proof
   4.649 +        fix i :: 'a
   4.650 +        assume "i \<in> Basis"
   4.651 +        with x ord[of i]
   4.652 +        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>
   4.653 +            (d \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i)"
   4.654 +          by (auto simp: cbox_def)
   4.655 +        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)}"
   4.656 +          by auto
   4.657 +      qed
   4.658 +      then obtain f where
   4.659 +        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)}"
   4.660 +        unfolding bchoice_iff ..
   4.661 +      moreover from f have "restrict f Basis \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}"
   4.662 +        by auto
   4.663 +      moreover from f have "x \<in> ?B (restrict f Basis)"
   4.664 +        by (auto simp: mem_box)
   4.665 +      ultimately have "\<exists>k\<in>p. x \<in> k"
   4.666 +        unfolding p_def by blast
   4.667 +    }
   4.668 +    ultimately show "\<Union>p = cbox a b"
   4.669 +      by auto
   4.670 +  qed
   4.671 +qed
   4.672 +
   4.673 +lemma partial_division_extend_interval:
   4.674 +  assumes "p division_of (\<Union>p)" "(\<Union>p) \<subseteq> cbox a b"
   4.675 +  obtains q where "p \<subseteq> q" "q division_of cbox a (b::'a::euclidean_space)"
   4.676 +proof (cases "p = {}")
   4.677 +  case True
   4.678 +  obtain q where "q division_of (cbox a b)"
   4.679 +    by (rule elementary_interval)
   4.680 +  then show ?thesis
   4.681 +    using True that by blast
   4.682 +next
   4.683 +  case False
   4.684 +  note p = division_ofD[OF assms(1)]
   4.685 +  have div_cbox: "\<forall>k\<in>p. \<exists>q. q division_of cbox a b \<and> k \<in> q"
   4.686 +  proof
   4.687 +    fix k
   4.688 +    assume kp: "k \<in> p"
   4.689 +    obtain c d where k: "k = cbox c d"
   4.690 +      using p(4)[OF kp] by blast
   4.691 +    have *: "cbox c d \<subseteq> cbox a b" "cbox c d \<noteq> {}"
   4.692 +      using p(2,3)[OF kp, unfolded k] using assms(2)
   4.693 +      by (blast intro: order.trans)+
   4.694 +    obtain q where "q division_of cbox a b" "cbox c d \<in> q"
   4.695 +      by (rule partial_division_extend_1[OF *])
   4.696 +    then show "\<exists>q. q division_of cbox a b \<and> k \<in> q"
   4.697 +      unfolding k by auto
   4.698 +  qed
   4.699 +  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"
   4.700 +    using bchoice[OF div_cbox] by blast
   4.701 +  { fix x
   4.702 +    assume x: "x \<in> p"
   4.703 +    have "q x division_of \<Union>q x"
   4.704 +      apply (rule division_ofI)
   4.705 +      using division_ofD[OF q(1)[OF x]]
   4.706 +      apply auto
   4.707 +      done }
   4.708 +  then have "\<And>x. x \<in> p \<Longrightarrow> \<exists>d. d division_of \<Union>(q x - {x})"
   4.709 +    by (meson Diff_subset division_of_subset)
   4.710 +  then have "\<exists>d. d division_of \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p)"
   4.711 +    apply -
   4.712 +    apply (rule elementary_inters [OF finite_imageI[OF p(1)]])
   4.713 +    apply (auto simp: False elementary_inters [OF finite_imageI[OF p(1)]])
   4.714 +    done
   4.715 +  then obtain d where d: "d division_of \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p)" ..
   4.716 +  have "d \<union> p division_of cbox a b"
   4.717 +  proof -
   4.718 +    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
   4.719 +    have cbox_eq: "cbox a b = \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p) \<union> \<Union>p"
   4.720 +    proof (rule te[OF False], clarify)
   4.721 +      fix i
   4.722 +      assume i: "i \<in> p"
   4.723 +      show "\<Union>(q i - {i}) \<union> i = cbox a b"
   4.724 +        using division_ofD(6)[OF q(1)[OF i]] using q(2)[OF i] by auto
   4.725 +    qed
   4.726 +    { fix k
   4.727 +      assume k: "k \<in> p"
   4.728 +      have *: "\<And>u t s. t \<inter> s = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<inter> t = {}"
   4.729 +        by auto
   4.730 +      have "interior (\<Inter>i\<in>p. \<Union>(q i - {i})) \<inter> interior k = {}"
   4.731 +      proof (rule *[OF inter_interior_unions_intervals])
   4.732 +        note qk=division_ofD[OF q(1)[OF k]]
   4.733 +        show "finite (q k - {k})" "open (interior k)" "\<forall>t\<in>q k - {k}. \<exists>a b. t = cbox a b"
   4.734 +          using qk by auto
   4.735 +        show "\<forall>t\<in>q k - {k}. interior k \<inter> interior t = {}"
   4.736 +          using qk(5) using q(2)[OF k] by auto
   4.737 +        show "interior (\<Inter>i\<in>p. \<Union>(q i - {i})) \<subseteq> interior (\<Union>(q k - {k}))"
   4.738 +          apply (rule interior_mono)+
   4.739 +          using k
   4.740 +          apply auto
   4.741 +          done
   4.742 +      qed } note [simp] = this
   4.743 +    show "d \<union> p division_of (cbox a b)"
   4.744 +      unfolding cbox_eq
   4.745 +      apply (rule division_disjoint_union[OF d assms(1)])
   4.746 +      apply (rule inter_interior_unions_intervals)
   4.747 +      apply (rule p open_interior ballI)+
   4.748 +      apply simp_all
   4.749 +      done
   4.750 +  qed
   4.751 +  then show ?thesis
   4.752 +    by (meson Un_upper2 that)
   4.753 +qed
   4.754 +
   4.755 +lemma elementary_bounded[dest]:
   4.756 +  fixes s :: "'a::euclidean_space set"
   4.757 +  shows "p division_of s \<Longrightarrow> bounded s"
   4.758 +  unfolding division_of_def by (metis bounded_Union bounded_cbox)
   4.759 +
   4.760 +lemma elementary_subset_cbox:
   4.761 +  "p division_of s \<Longrightarrow> \<exists>a b. s \<subseteq> cbox a (b::'a::euclidean_space)"
   4.762 +  by (meson elementary_bounded bounded_subset_cbox)
   4.763 +
   4.764 +lemma division_union_intervals_exists:
   4.765 +  fixes a b :: "'a::euclidean_space"
   4.766 +  assumes "cbox a b \<noteq> {}"
   4.767 +  obtains p where "(insert (cbox a b) p) division_of (cbox a b \<union> cbox c d)"
   4.768 +proof (cases "cbox c d = {}")
   4.769 +  case True
   4.770 +  show ?thesis
   4.771 +    apply (rule that[of "{}"])
   4.772 +    unfolding True
   4.773 +    using assms
   4.774 +    apply auto
   4.775 +    done
   4.776 +next
   4.777 +  case False
   4.778 +  show ?thesis
   4.779 +  proof (cases "cbox a b \<inter> cbox c d = {}")
   4.780 +    case True
   4.781 +    then show ?thesis
   4.782 +      by (metis that False assms division_disjoint_union division_of_self insert_is_Un interior_Int interior_empty)
   4.783 +  next
   4.784 +    case False
   4.785 +    obtain u v where uv: "cbox a b \<inter> cbox c d = cbox u v"
   4.786 +      unfolding Int_interval by auto
   4.787 +    have uv_sub: "cbox u v \<subseteq> cbox c d" using uv by auto
   4.788 +    obtain p where "p division_of cbox c d" "cbox u v \<in> p"
   4.789 +      by (rule partial_division_extend_1[OF uv_sub False[unfolded uv]])
   4.790 +    note p = this division_ofD[OF this(1)]
   4.791 +    have "interior (cbox a b \<inter> \<Union>(p - {cbox u v})) = interior(cbox u v \<inter> \<Union>(p - {cbox u v}))"
   4.792 +      apply (rule arg_cong[of _ _ interior])
   4.793 +      using p(8) uv by auto
   4.794 +    also have "\<dots> = {}"
   4.795 +      unfolding interior_Int
   4.796 +      apply (rule inter_interior_unions_intervals)
   4.797 +      using p(6) p(7)[OF p(2)] p(3)
   4.798 +      apply auto
   4.799 +      done
   4.800 +    finally have [simp]: "interior (cbox a b) \<inter> interior (\<Union>(p - {cbox u v})) = {}" by simp
   4.801 +    have cbe: "cbox a b \<union> cbox c d = cbox a b \<union> \<Union>(p - {cbox u v})"
   4.802 +      using p(8) unfolding uv[symmetric] by auto
   4.803 +    have "insert (cbox a b) (p - {cbox u v}) division_of cbox a b \<union> \<Union>(p - {cbox u v})"
   4.804 +    proof -
   4.805 +      have "{cbox a b} division_of cbox a b"
   4.806 +        by (simp add: assms division_of_self)
   4.807 +      then show "insert (cbox a b) (p - {cbox u v}) division_of cbox a b \<union> \<Union>(p - {cbox u v})"
   4.808 +        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))
   4.809 +    qed
   4.810 +    with that[of "p - {cbox u v}"] show ?thesis by (simp add: cbe)
   4.811 +  qed
   4.812 +qed
   4.813 +
   4.814 +lemma division_of_unions:
   4.815 +  assumes "finite f"
   4.816 +    and "\<And>p. p \<in> f \<Longrightarrow> p division_of (\<Union>p)"
   4.817 +    and "\<And>k1 k2. k1 \<in> \<Union>f \<Longrightarrow> k2 \<in> \<Union>f \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
   4.818 +  shows "\<Union>f division_of \<Union>\<Union>f"
   4.819 +  using assms
   4.820 +  by (auto intro!: division_ofI)
   4.821 +
   4.822 +lemma elementary_union_interval:
   4.823 +  fixes a b :: "'a::euclidean_space"
   4.824 +  assumes "p division_of \<Union>p"
   4.825 +  obtains q where "q division_of (cbox a b \<union> \<Union>p)"
   4.826 +proof -
   4.827 +  note assm = division_ofD[OF assms]
   4.828 +  have lem1: "\<And>f s. \<Union>\<Union>(f ` s) = \<Union>((\<lambda>x. \<Union>(f x)) ` s)"
   4.829 +    by auto
   4.830 +  have lem2: "\<And>f s. f \<noteq> {} \<Longrightarrow> \<Union>{s \<union> t |t. t \<in> f} = s \<union> \<Union>f"
   4.831 +    by auto
   4.832 +  {
   4.833 +    presume "p = {} \<Longrightarrow> thesis"
   4.834 +      "cbox a b = {} \<Longrightarrow> thesis"
   4.835 +      "cbox a b \<noteq> {} \<Longrightarrow> interior (cbox a b) = {} \<Longrightarrow> thesis"
   4.836 +      "p \<noteq> {} \<Longrightarrow> interior (cbox a b)\<noteq>{} \<Longrightarrow> cbox a b \<noteq> {} \<Longrightarrow> thesis"
   4.837 +    then show thesis by auto
   4.838 +  next
   4.839 +    assume as: "p = {}"
   4.840 +    obtain p where "p division_of (cbox a b)"
   4.841 +      by (rule elementary_interval)
   4.842 +    then show thesis
   4.843 +      using as that by auto
   4.844 +  next
   4.845 +    assume as: "cbox a b = {}"
   4.846 +    show thesis
   4.847 +      using as assms that by auto
   4.848 +  next
   4.849 +    assume as: "interior (cbox a b) = {}" "cbox a b \<noteq> {}"
   4.850 +    show thesis
   4.851 +      apply (rule that[of "insert (cbox a b) p"],rule division_ofI)
   4.852 +      unfolding finite_insert
   4.853 +      apply (rule assm(1)) unfolding Union_insert
   4.854 +      using assm(2-4) as
   4.855 +      apply -
   4.856 +      apply (fast dest: assm(5))+
   4.857 +      done
   4.858 +  next
   4.859 +    assume as: "p \<noteq> {}" "interior (cbox a b) \<noteq> {}" "cbox a b \<noteq> {}"
   4.860 +    have "\<forall>k\<in>p. \<exists>q. (insert (cbox a b) q) division_of (cbox a b \<union> k)"
   4.861 +    proof
   4.862 +      fix k
   4.863 +      assume kp: "k \<in> p"
   4.864 +      from assm(4)[OF kp] obtain c d where "k = cbox c d" by blast
   4.865 +      then show "\<exists>q. (insert (cbox a b) q) division_of (cbox a b \<union> k)"
   4.866 +        by (meson as(3) division_union_intervals_exists)
   4.867 +    qed
   4.868 +    from bchoice[OF this] obtain q where "\<forall>x\<in>p. insert (cbox a b) (q x) division_of (cbox a b) \<union> x" ..
   4.869 +    note q = division_ofD[OF this[rule_format]]
   4.870 +    let ?D = "\<Union>{insert (cbox a b) (q k) | k. k \<in> p}"
   4.871 +    show thesis
   4.872 +    proof (rule that[OF division_ofI])
   4.873 +      have *: "{insert (cbox a b) (q k) |k. k \<in> p} = (\<lambda>k. insert (cbox a b) (q k)) ` p"
   4.874 +        by auto
   4.875 +      show "finite ?D"
   4.876 +        using "*" assm(1) q(1) by auto
   4.877 +      show "\<Union>?D = cbox a b \<union> \<Union>p"
   4.878 +        unfolding * lem1
   4.879 +        unfolding lem2[OF as(1), of "cbox a b", symmetric]
   4.880 +        using q(6)
   4.881 +        by auto
   4.882 +      fix k
   4.883 +      assume k: "k \<in> ?D"
   4.884 +      then show "k \<subseteq> cbox a b \<union> \<Union>p"
   4.885 +        using q(2) by auto
   4.886 +      show "k \<noteq> {}"
   4.887 +        using q(3) k by auto
   4.888 +      show "\<exists>a b. k = cbox a b"
   4.889 +        using q(4) k by auto
   4.890 +      fix k'
   4.891 +      assume k': "k' \<in> ?D" "k \<noteq> k'"
   4.892 +      obtain x where x: "k \<in> insert (cbox a b) (q x)" "x\<in>p"
   4.893 +        using k by auto
   4.894 +      obtain x' where x': "k'\<in>insert (cbox a b) (q x')" "x'\<in>p"
   4.895 +        using k' by auto
   4.896 +      show "interior k \<inter> interior k' = {}"
   4.897 +      proof (cases "x = x'")
   4.898 +        case True
   4.899 +        show ?thesis
   4.900 +          using True k' q(5) x' x by auto
   4.901 +      next
   4.902 +        case False
   4.903 +        {
   4.904 +          presume "k = cbox a b \<Longrightarrow> ?thesis"
   4.905 +            and "k' = cbox a b \<Longrightarrow> ?thesis"
   4.906 +            and "k \<noteq> cbox a b \<Longrightarrow> k' \<noteq> cbox a b \<Longrightarrow> ?thesis"
   4.907 +          then show ?thesis by linarith
   4.908 +        next
   4.909 +          assume as': "k  = cbox a b"
   4.910 +          show ?thesis
   4.911 +            using as' k' q(5) x' by blast
   4.912 +        next
   4.913 +          assume as': "k' = cbox a b"
   4.914 +          show ?thesis
   4.915 +            using as' k'(2) q(5) x by blast
   4.916 +        }
   4.917 +        assume as': "k \<noteq> cbox a b" "k' \<noteq> cbox a b"
   4.918 +        obtain c d where k: "k = cbox c d"
   4.919 +          using q(4)[OF x(2,1)] by blast
   4.920 +        have "interior k \<inter> interior (cbox a b) = {}"
   4.921 +          using as' k'(2) q(5) x by blast
   4.922 +        then have "interior k \<subseteq> interior x"
   4.923 +        using interior_subset_union_intervals
   4.924 +          by (metis as(2) k q(2) x interior_subset_union_intervals)
   4.925 +        moreover
   4.926 +        obtain c d where c_d: "k' = cbox c d"
   4.927 +          using q(4)[OF x'(2,1)] by blast
   4.928 +        have "interior k' \<inter> interior (cbox a b) = {}"
   4.929 +          using as'(2) q(5) x' by blast
   4.930 +        then have "interior k' \<subseteq> interior x'"
   4.931 +          by (metis as(2) c_d interior_subset_union_intervals q(2) x'(1) x'(2))
   4.932 +        ultimately show ?thesis
   4.933 +          using assm(5)[OF x(2) x'(2) False] by auto
   4.934 +      qed
   4.935 +    qed
   4.936 +  }
   4.937 +qed
   4.938 +
   4.939 +lemma elementary_unions_intervals:
   4.940 +  assumes fin: "finite f"
   4.941 +    and "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = cbox a (b::'a::euclidean_space)"
   4.942 +  obtains p where "p division_of (\<Union>f)"
   4.943 +proof -
   4.944 +  have "\<exists>p. p division_of (\<Union>f)"
   4.945 +  proof (induct_tac f rule:finite_subset_induct)
   4.946 +    show "\<exists>p. p division_of \<Union>{}" using elementary_empty by auto
   4.947 +  next
   4.948 +    fix x F
   4.949 +    assume as: "finite F" "x \<notin> F" "\<exists>p. p division_of \<Union>F" "x\<in>f"
   4.950 +    from this(3) obtain p where p: "p division_of \<Union>F" ..
   4.951 +    from assms(2)[OF as(4)] obtain a b where x: "x = cbox a b" by blast
   4.952 +    have *: "\<Union>F = \<Union>p"
   4.953 +      using division_ofD[OF p] by auto
   4.954 +    show "\<exists>p. p division_of \<Union>insert x F"
   4.955 +      using elementary_union_interval[OF p[unfolded *], of a b]
   4.956 +      unfolding Union_insert x * by metis
   4.957 +  qed (insert assms, auto)
   4.958 +  then show ?thesis
   4.959 +    using that by auto
   4.960 +qed
   4.961 +
   4.962 +lemma elementary_union:
   4.963 +  fixes s t :: "'a::euclidean_space set"
   4.964 +  assumes "ps division_of s" "pt division_of t"
   4.965 +  obtains p where "p division_of (s \<union> t)"
   4.966 +proof -
   4.967 +  have *: "s \<union> t = \<Union>ps \<union> \<Union>pt"
   4.968 +    using assms unfolding division_of_def by auto
   4.969 +  show ?thesis
   4.970 +    apply (rule elementary_unions_intervals[of "ps \<union> pt"])
   4.971 +    using assms apply auto
   4.972 +    by (simp add: * that)
   4.973 +qed
   4.974 +
   4.975 +lemma partial_division_extend:
   4.976 +  fixes t :: "'a::euclidean_space set"
   4.977 +  assumes "p division_of s"
   4.978 +    and "q division_of t"
   4.979 +    and "s \<subseteq> t"
   4.980 +  obtains r where "p \<subseteq> r" and "r division_of t"
   4.981 +proof -
   4.982 +  note divp = division_ofD[OF assms(1)] and divq = division_ofD[OF assms(2)]
   4.983 +  obtain a b where ab: "t \<subseteq> cbox a b"
   4.984 +    using elementary_subset_cbox[OF assms(2)] by auto
   4.985 +  obtain r1 where "p \<subseteq> r1" "r1 division_of (cbox a b)"
   4.986 +    using assms
   4.987 +    by (metis ab dual_order.trans partial_division_extend_interval divp(6))
   4.988 +  note r1 = this division_ofD[OF this(2)]
   4.989 +  obtain p' where "p' division_of \<Union>(r1 - p)"
   4.990 +    apply (rule elementary_unions_intervals[of "r1 - p"])
   4.991 +    using r1(3,6)
   4.992 +    apply auto
   4.993 +    done
   4.994 +  then obtain r2 where r2: "r2 division_of (\<Union>(r1 - p)) \<inter> (\<Union>q)"
   4.995 +    by (metis assms(2) divq(6) elementary_inter)
   4.996 +  {
   4.997 +    fix x
   4.998 +    assume x: "x \<in> t" "x \<notin> s"
   4.999 +    then have "x\<in>\<Union>r1"
  4.1000 +      unfolding r1 using ab by auto
  4.1001 +    then obtain r where r: "r \<in> r1" "x \<in> r"
  4.1002 +      unfolding Union_iff ..
  4.1003 +    moreover
  4.1004 +    have "r \<notin> p"
  4.1005 +    proof
  4.1006 +      assume "r \<in> p"
  4.1007 +      then have "x \<in> s" using divp(2) r by auto
  4.1008 +      then show False using x by auto
  4.1009 +    qed
  4.1010 +    ultimately have "x\<in>\<Union>(r1 - p)" by auto
  4.1011 +  }
  4.1012 +  then have *: "t = \<Union>p \<union> (\<Union>(r1 - p) \<inter> \<Union>q)"
  4.1013 +    unfolding divp divq using assms(3) by auto
  4.1014 +  show ?thesis
  4.1015 +    apply (rule that[of "p \<union> r2"])
  4.1016 +    unfolding *
  4.1017 +    defer
  4.1018 +    apply (rule division_disjoint_union)
  4.1019 +    unfolding divp(6)
  4.1020 +    apply(rule assms r2)+
  4.1021 +  proof -
  4.1022 +    have "interior s \<inter> interior (\<Union>(r1-p)) = {}"
  4.1023 +    proof (rule inter_interior_unions_intervals)
  4.1024 +      show "finite (r1 - p)" and "open (interior s)" and "\<forall>t\<in>r1-p. \<exists>a b. t = cbox a b"
  4.1025 +        using r1 by auto
  4.1026 +      have *: "\<And>s. (\<And>x. x \<in> s \<Longrightarrow> False) \<Longrightarrow> s = {}"
  4.1027 +        by auto
  4.1028 +      show "\<forall>t\<in>r1-p. interior s \<inter> interior t = {}"
  4.1029 +      proof
  4.1030 +        fix m x
  4.1031 +        assume as: "m \<in> r1 - p"
  4.1032 +        have "interior m \<inter> interior (\<Union>p) = {}"
  4.1033 +        proof (rule inter_interior_unions_intervals)
  4.1034 +          show "finite p" and "open (interior m)" and "\<forall>t\<in>p. \<exists>a b. t = cbox a b"
  4.1035 +            using divp by auto
  4.1036 +          show "\<forall>t\<in>p. interior m \<inter> interior t = {}"
  4.1037 +            by (metis DiffD1 DiffD2 as r1(1) r1(7) set_rev_mp)
  4.1038 +        qed
  4.1039 +        then show "interior s \<inter> interior m = {}"
  4.1040 +          unfolding divp by auto
  4.1041 +      qed
  4.1042 +    qed
  4.1043 +    then show "interior s \<inter> interior (\<Union>(r1-p) \<inter> (\<Union>q)) = {}"
  4.1044 +      using interior_subset by auto
  4.1045 +  qed auto
  4.1046 +qed
  4.1047 +
  4.1048 +
  4.1049 +lemma division_split:
  4.1050 +  fixes a :: "'a::euclidean_space"
  4.1051 +  assumes "p division_of (cbox a b)"
  4.1052 +    and k: "k\<in>Basis"
  4.1053 +  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})"
  4.1054 +      (is "?p1 division_of ?I1")
  4.1055 +    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})"
  4.1056 +      (is "?p2 division_of ?I2")
  4.1057 +proof (rule_tac[!] division_ofI)
  4.1058 +  note p = division_ofD[OF assms(1)]
  4.1059 +  show "finite ?p1" "finite ?p2"
  4.1060 +    using p(1) by auto
  4.1061 +  show "\<Union>?p1 = ?I1" "\<Union>?p2 = ?I2"
  4.1062 +    unfolding p(6)[symmetric] by auto
  4.1063 +  {
  4.1064 +    fix k
  4.1065 +    assume "k \<in> ?p1"
  4.1066 +    then guess l unfolding mem_Collect_eq by (elim exE conjE) note l=this
  4.1067 +    guess u v using p(4)[OF l(2)] by (elim exE) note uv=this
  4.1068 +    show "k \<subseteq> ?I1"
  4.1069 +      using l p(2) uv by force
  4.1070 +    show  "k \<noteq> {}"
  4.1071 +      by (simp add: l)
  4.1072 +    show  "\<exists>a b. k = cbox a b"
  4.1073 +      apply (simp add: l uv p(2-3)[OF l(2)])
  4.1074 +      apply (subst interval_split[OF k])
  4.1075 +      apply (auto intro: order.trans)
  4.1076 +      done
  4.1077 +    fix k'
  4.1078 +    assume "k' \<in> ?p1"
  4.1079 +    then guess l' unfolding mem_Collect_eq by (elim exE conjE) note l'=this
  4.1080 +    assume "k \<noteq> k'"
  4.1081 +    then show "interior k \<inter> interior k' = {}"
  4.1082 +      unfolding l l' using p(5)[OF l(2) l'(2)] by auto
  4.1083 +  }
  4.1084 +  {
  4.1085 +    fix k
  4.1086 +    assume "k \<in> ?p2"
  4.1087 +    then guess l unfolding mem_Collect_eq by (elim exE conjE) note l=this
  4.1088 +    guess u v using p(4)[OF l(2)] by (elim exE) note uv=this
  4.1089 +    show "k \<subseteq> ?I2"
  4.1090 +      using l p(2) uv by force
  4.1091 +    show  "k \<noteq> {}"
  4.1092 +      by (simp add: l)
  4.1093 +    show  "\<exists>a b. k = cbox a b"
  4.1094 +      apply (simp add: l uv p(2-3)[OF l(2)])
  4.1095 +      apply (subst interval_split[OF k])
  4.1096 +      apply (auto intro: order.trans)
  4.1097 +      done
  4.1098 +    fix k'
  4.1099 +    assume "k' \<in> ?p2"
  4.1100 +    then guess l' unfolding mem_Collect_eq by (elim exE conjE) note l'=this
  4.1101 +    assume "k \<noteq> k'"
  4.1102 +    then show "interior k \<inter> interior k' = {}"
  4.1103 +      unfolding l l' using p(5)[OF l(2) l'(2)] by auto
  4.1104 +  }
  4.1105 +qed
  4.1106 +
  4.1107 +subsection \<open>Tagged (partial) divisions.\<close>
  4.1108 +
  4.1109 +definition tagged_partial_division_of (infixr "tagged'_partial'_division'_of" 40)
  4.1110 +  where "s tagged_partial_division_of i \<longleftrightarrow>
  4.1111 +    finite s \<and>
  4.1112 +    (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
  4.1113 +    (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
  4.1114 +      interior k1 \<inter> interior k2 = {})"
  4.1115 +
  4.1116 +lemma tagged_partial_division_ofD[dest]:
  4.1117 +  assumes "s tagged_partial_division_of i"
  4.1118 +  shows "finite s"
  4.1119 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  4.1120 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  4.1121 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  4.1122 +    and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow>
  4.1123 +      (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
  4.1124 +  using assms unfolding tagged_partial_division_of_def by blast+
  4.1125 +
  4.1126 +definition tagged_division_of (infixr "tagged'_division'_of" 40)
  4.1127 +  where "s tagged_division_of i \<longleftrightarrow> s tagged_partial_division_of i \<and> (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  4.1128 +
  4.1129 +lemma tagged_division_of_finite: "s tagged_division_of i \<Longrightarrow> finite s"
  4.1130 +  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  4.1131 +
  4.1132 +lemma tagged_division_of:
  4.1133 +  "s tagged_division_of i \<longleftrightarrow>
  4.1134 +    finite s \<and>
  4.1135 +    (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
  4.1136 +    (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
  4.1137 +      interior k1 \<inter> interior k2 = {}) \<and>
  4.1138 +    (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  4.1139 +  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  4.1140 +
  4.1141 +lemma tagged_division_ofI:
  4.1142 +  assumes "finite s"
  4.1143 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  4.1144 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  4.1145 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  4.1146 +    and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
  4.1147 +      interior k1 \<inter> interior k2 = {}"
  4.1148 +    and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  4.1149 +  shows "s tagged_division_of i"
  4.1150 +  unfolding tagged_division_of
  4.1151 +  using assms
  4.1152 +  apply auto
  4.1153 +  apply fastforce+
  4.1154 +  done
  4.1155 +
  4.1156 +lemma tagged_division_ofD[dest]:  (*FIXME USE A LOCALE*)
  4.1157 +  assumes "s tagged_division_of i"
  4.1158 +  shows "finite s"
  4.1159 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  4.1160 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  4.1161 +    and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  4.1162 +    and "\<And>x1 k1 x2 k2. (x1, k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
  4.1163 +      interior k1 \<inter> interior k2 = {}"
  4.1164 +    and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  4.1165 +  using assms unfolding tagged_division_of by blast+
  4.1166 +
  4.1167 +lemma division_of_tagged_division:
  4.1168 +  assumes "s tagged_division_of i"
  4.1169 +  shows "(snd ` s) division_of i"
  4.1170 +proof (rule division_ofI)
  4.1171 +  note assm = tagged_division_ofD[OF assms]
  4.1172 +  show "\<Union>(snd ` s) = i" "finite (snd ` s)"
  4.1173 +    using assm by auto
  4.1174 +  fix k
  4.1175 +  assume k: "k \<in> snd ` s"
  4.1176 +  then obtain xk where xk: "(xk, k) \<in> s"
  4.1177 +    by auto
  4.1178 +  then show "k \<subseteq> i" "k \<noteq> {}" "\<exists>a b. k = cbox a b"
  4.1179 +    using assm by fastforce+
  4.1180 +  fix k'
  4.1181 +  assume k': "k' \<in> snd ` s" "k \<noteq> k'"
  4.1182 +  from this(1) obtain xk' where xk': "(xk', k') \<in> s"
  4.1183 +    by auto
  4.1184 +  then show "interior k \<inter> interior k' = {}"
  4.1185 +    using assm(5) k'(2) xk by blast
  4.1186 +qed
  4.1187 +
  4.1188 +lemma partial_division_of_tagged_division:
  4.1189 +  assumes "s tagged_partial_division_of i"
  4.1190 +  shows "(snd ` s) division_of \<Union>(snd ` s)"
  4.1191 +proof (rule division_ofI)
  4.1192 +  note assm = tagged_partial_division_ofD[OF assms]
  4.1193 +  show "finite (snd ` s)" "\<Union>(snd ` s) = \<Union>(snd ` s)"
  4.1194 +    using assm by auto
  4.1195 +  fix k
  4.1196 +  assume k: "k \<in> snd ` s"
  4.1197 +  then obtain xk where xk: "(xk, k) \<in> s"
  4.1198 +    by auto
  4.1199 +  then show "k \<noteq> {}" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>(snd ` s)"
  4.1200 +    using assm by auto
  4.1201 +  fix k'
  4.1202 +  assume k': "k' \<in> snd ` s" "k \<noteq> k'"
  4.1203 +  from this(1) obtain xk' where xk': "(xk', k') \<in> s"
  4.1204 +    by auto
  4.1205 +  then show "interior k \<inter> interior k' = {}"
  4.1206 +    using assm(5) k'(2) xk by auto
  4.1207 +qed
  4.1208 +
  4.1209 +lemma tagged_partial_division_subset:
  4.1210 +  assumes "s tagged_partial_division_of i"
  4.1211 +    and "t \<subseteq> s"
  4.1212 +  shows "t tagged_partial_division_of i"
  4.1213 +  using assms
  4.1214 +  unfolding tagged_partial_division_of_def
  4.1215 +  using finite_subset[OF assms(2)]
  4.1216 +  by blast
  4.1217 +
  4.1218 +lemma tag_in_interval: "p tagged_division_of i \<Longrightarrow> (x, k) \<in> p \<Longrightarrow> x \<in> i"
  4.1219 +  by auto
  4.1220 +
  4.1221 +lemma tagged_division_of_empty: "{} tagged_division_of {}"
  4.1222 +  unfolding tagged_division_of by auto
  4.1223 +
  4.1224 +lemma tagged_partial_division_of_trivial[simp]: "p tagged_partial_division_of {} \<longleftrightarrow> p = {}"
  4.1225 +  unfolding tagged_partial_division_of_def by auto
  4.1226 +
  4.1227 +lemma tagged_division_of_trivial[simp]: "p tagged_division_of {} \<longleftrightarrow> p = {}"
  4.1228 +  unfolding tagged_division_of by auto
  4.1229 +
  4.1230 +lemma tagged_division_of_self: "x \<in> cbox a b \<Longrightarrow> {(x,cbox a b)} tagged_division_of (cbox a b)"
  4.1231 +  by (rule tagged_division_ofI) auto
  4.1232 +
  4.1233 +lemma tagged_division_of_self_real: "x \<in> {a .. b::real} \<Longrightarrow> {(x,{a .. b})} tagged_division_of {a .. b}"
  4.1234 +  unfolding box_real[symmetric]
  4.1235 +  by (rule tagged_division_of_self)
  4.1236 +
  4.1237 +lemma tagged_division_union:
  4.1238 +  assumes "p1 tagged_division_of s1"
  4.1239 +    and "p2 tagged_division_of s2"
  4.1240 +    and "interior s1 \<inter> interior s2 = {}"
  4.1241 +  shows "(p1 \<union> p2) tagged_division_of (s1 \<union> s2)"
  4.1242 +proof (rule tagged_division_ofI)
  4.1243 +  note p1 = tagged_division_ofD[OF assms(1)]
  4.1244 +  note p2 = tagged_division_ofD[OF assms(2)]
  4.1245 +  show "finite (p1 \<union> p2)"
  4.1246 +    using p1(1) p2(1) by auto
  4.1247 +  show "\<Union>{k. \<exists>x. (x, k) \<in> p1 \<union> p2} = s1 \<union> s2"
  4.1248 +    using p1(6) p2(6) by blast
  4.1249 +  fix x k
  4.1250 +  assume xk: "(x, k) \<in> p1 \<union> p2"
  4.1251 +  show "x \<in> k" "\<exists>a b. k = cbox a b"
  4.1252 +    using xk p1(2,4) p2(2,4) by auto
  4.1253 +  show "k \<subseteq> s1 \<union> s2"
  4.1254 +    using xk p1(3) p2(3) by blast
  4.1255 +  fix x' k'
  4.1256 +  assume xk': "(x', k') \<in> p1 \<union> p2" "(x, k) \<noteq> (x', k')"
  4.1257 +  have *: "\<And>a b. a \<subseteq> s1 \<Longrightarrow> b \<subseteq> s2 \<Longrightarrow> interior a \<inter> interior b = {}"
  4.1258 +    using assms(3) interior_mono by blast
  4.1259 +  show "interior k \<inter> interior k' = {}"
  4.1260 +    apply (cases "(x, k) \<in> p1")
  4.1261 +    apply (meson "*" UnE assms(1) assms(2) p1(5) tagged_division_ofD(3) xk'(1) xk'(2))
  4.1262 +    by (metis "*" UnE assms(1) assms(2) inf_sup_aci(1) p2(5) tagged_division_ofD(3) xk xk'(1) xk'(2))
  4.1263 +qed
  4.1264 +
  4.1265 +lemma tagged_division_unions:
  4.1266 +  assumes "finite iset"
  4.1267 +    and "\<forall>i\<in>iset. pfn i tagged_division_of i"
  4.1268 +    and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior(i1) \<inter> interior(i2) = {}"
  4.1269 +  shows "\<Union>(pfn ` iset) tagged_division_of (\<Union>iset)"
  4.1270 +proof (rule tagged_division_ofI)
  4.1271 +  note assm = tagged_division_ofD[OF assms(2)[rule_format]]
  4.1272 +  show "finite (\<Union>(pfn ` iset))"
  4.1273 +    using assms by auto
  4.1274 +  have "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>((\<lambda>i. \<Union>{k. \<exists>x. (x, k) \<in> pfn i}) ` iset)"
  4.1275 +    by blast
  4.1276 +  also have "\<dots> = \<Union>iset"
  4.1277 +    using assm(6) by auto
  4.1278 +  finally show "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>iset" .
  4.1279 +  fix x k
  4.1280 +  assume xk: "(x, k) \<in> \<Union>(pfn ` iset)"
  4.1281 +  then obtain i where i: "i \<in> iset" "(x, k) \<in> pfn i"
  4.1282 +    by auto
  4.1283 +  show "x \<in> k" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>iset"
  4.1284 +    using assm(2-4)[OF i] using i(1) by auto
  4.1285 +  fix x' k'
  4.1286 +  assume xk': "(x', k') \<in> \<Union>(pfn ` iset)" "(x, k) \<noteq> (x', k')"
  4.1287 +  then obtain i' where i': "i' \<in> iset" "(x', k') \<in> pfn i'"
  4.1288 +    by auto
  4.1289 +  have *: "\<And>a b. i \<noteq> i' \<Longrightarrow> a \<subseteq> i \<Longrightarrow> b \<subseteq> i' \<Longrightarrow> interior a \<inter> interior b = {}"
  4.1290 +    using i(1) i'(1)
  4.1291 +    using assms(3)[rule_format] interior_mono
  4.1292 +    by blast
  4.1293 +  show "interior k \<inter> interior k' = {}"
  4.1294 +    apply (cases "i = i'")
  4.1295 +    using assm(5) i' i(2) xk'(2) apply blast
  4.1296 +    using "*" assm(3) i' i by auto
  4.1297 +qed
  4.1298 +
  4.1299 +lemma tagged_partial_division_of_union_self:
  4.1300 +  assumes "p tagged_partial_division_of s"
  4.1301 +  shows "p tagged_division_of (\<Union>(snd ` p))"
  4.1302 +  apply (rule tagged_division_ofI)
  4.1303 +  using tagged_partial_division_ofD[OF assms]
  4.1304 +  apply auto
  4.1305 +  done
  4.1306 +
  4.1307 +lemma tagged_division_of_union_self:
  4.1308 +  assumes "p tagged_division_of s"
  4.1309 +  shows "p tagged_division_of (\<Union>(snd ` p))"
  4.1310 +  apply (rule tagged_division_ofI)
  4.1311 +  using tagged_division_ofD[OF assms]
  4.1312 +  apply auto
  4.1313 +  done
  4.1314 +
  4.1315 +subsection \<open>Functions closed on boxes: morphisms from boxes to monoids\<close>
  4.1316 +
  4.1317 +text \<open>This auxiliary structure is used to sum up over the elements of a division. Main theorem is
  4.1318 +  @{text operative_division}. Instances for the monoid are @{typ "'a option"}, @{typ real}, and
  4.1319 +  @{typ bool}.\<close>
  4.1320 +
  4.1321 +paragraph \<open>Using additivity of lifted function to encode definedness.\<close>
  4.1322 +
  4.1323 +definition lift_option :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a option \<Rightarrow> 'b option \<Rightarrow> 'c option"
  4.1324 +where
  4.1325 +  "lift_option f a' b' = Option.bind a' (\<lambda>a. Option.bind b' (\<lambda>b. Some (f a b)))"
  4.1326 +
  4.1327 +lemma lift_option_simps[simp]:
  4.1328 +  "lift_option f (Some a) (Some b) = Some (f a b)"
  4.1329 +  "lift_option f None b' = None"
  4.1330 +  "lift_option f a' None = None"
  4.1331 +  by (auto simp: lift_option_def)
  4.1332 +
  4.1333 +lemma comm_monoid_lift_option:
  4.1334 +  assumes "comm_monoid f z"
  4.1335 +  shows "comm_monoid (lift_option f) (Some z)"
  4.1336 +proof -
  4.1337 +  from assms interpret comm_monoid f z .
  4.1338 +  show ?thesis
  4.1339 +    by standard (auto simp: lift_option_def ac_simps split: bind_split)
  4.1340 +qed
  4.1341 +
  4.1342 +lemma comm_monoid_and: "comm_monoid HOL.conj True"
  4.1343 +  by standard auto
  4.1344 +
  4.1345 +lemma comm_monoid_set_and: "comm_monoid_set HOL.conj True"
  4.1346 +  by (rule comm_monoid_set.intro) (fact comm_monoid_and)
  4.1347 +
  4.1348 +paragraph \<open>Operative\<close>
  4.1349 +
  4.1350 +definition (in comm_monoid) operative :: "('b::euclidean_space set \<Rightarrow> 'a) \<Rightarrow> bool"
  4.1351 +  where "operative g \<longleftrightarrow>
  4.1352 +    (\<forall>a b. box a b = {} \<longrightarrow> g (cbox a b) = \<^bold>1) \<and>
  4.1353 +    (\<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}))"
  4.1354 +
  4.1355 +lemma (in comm_monoid) operativeD[dest]:
  4.1356 +  assumes "operative g"
  4.1357 +  shows "\<And>a b. box a b = {} \<Longrightarrow> g (cbox a b) = \<^bold>1"
  4.1358 +    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})"
  4.1359 +  using assms unfolding operative_def by auto
  4.1360 +
  4.1361 +lemma (in comm_monoid) operative_empty:
  4.1362 +  assumes g: "operative g" shows "g {} = \<^bold>1"
  4.1363 +proof -
  4.1364 +  have *: "cbox One (-One) = ({}::'b set)"
  4.1365 +    by (auto simp: box_eq_empty inner_setsum_left inner_Basis setsum.If_cases ex_in_conv)
  4.1366 +  moreover have "box One (-One) = ({}::'b set)"
  4.1367 +    using box_subset_cbox[of One "-One"] by (auto simp: *)
  4.1368 +  ultimately show ?thesis
  4.1369 +    using operativeD(1)[OF g, of One "-One"] by simp
  4.1370 +qed
  4.1371 +
  4.1372 +definition "division_points (k::('a::euclidean_space) set) d =
  4.1373 +   {(j,x). j \<in> Basis \<and> (interval_lowerbound k)\<bullet>j < x \<and> x < (interval_upperbound k)\<bullet>j \<and>
  4.1374 +     (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  4.1375 +
  4.1376 +lemma division_points_finite:
  4.1377 +  fixes i :: "'a::euclidean_space set"
  4.1378 +  assumes "d division_of i"
  4.1379 +  shows "finite (division_points i d)"
  4.1380 +proof -
  4.1381 +  note assm = division_ofD[OF assms]
  4.1382 +  let ?M = "\<lambda>j. {(j,x)|x. (interval_lowerbound i)\<bullet>j < x \<and> x < (interval_upperbound i)\<bullet>j \<and>
  4.1383 +    (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  4.1384 +  have *: "division_points i d = \<Union>(?M ` Basis)"
  4.1385 +    unfolding division_points_def by auto
  4.1386 +  show ?thesis
  4.1387 +    unfolding * using assm by auto
  4.1388 +qed
  4.1389 +
  4.1390 +lemma division_points_subset:
  4.1391 +  fixes a :: "'a::euclidean_space"
  4.1392 +  assumes "d division_of (cbox a b)"
  4.1393 +    and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  4.1394 +    and k: "k \<in> Basis"
  4.1395 +  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>
  4.1396 +      division_points (cbox a b) d" (is ?t1)
  4.1397 +    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>
  4.1398 +      division_points (cbox a b) d" (is ?t2)
  4.1399 +proof -
  4.1400 +  note assm = division_ofD[OF assms(1)]
  4.1401 +  have *: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  4.1402 +    "\<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"
  4.1403 +    "\<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"
  4.1404 +    "min (b \<bullet> k) c = c" "max (a \<bullet> k) c = c"
  4.1405 +    using assms using less_imp_le by auto
  4.1406 +  show ?t1 (*FIXME a horrible mess*)
  4.1407 +    unfolding division_points_def interval_split[OF k, of a b]
  4.1408 +    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  4.1409 +    unfolding *
  4.1410 +    apply (rule subsetI)
  4.1411 +    unfolding mem_Collect_eq split_beta
  4.1412 +    apply (erule bexE conjE)+
  4.1413 +    apply (simp add: )
  4.1414 +    apply (erule exE conjE)+
  4.1415 +  proof
  4.1416 +    fix i l x
  4.1417 +    assume as:
  4.1418 +      "a \<bullet> fst x < snd x" "snd x < (if fst x = k then c else b \<bullet> fst x)"
  4.1419 +      "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  4.1420 +      "i = l \<inter> {x. x \<bullet> k \<le> c}" "l \<in> d" "l \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}"
  4.1421 +      and fstx: "fst x \<in> Basis"
  4.1422 +    from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  4.1423 +    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"
  4.1424 +      using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  4.1425 +    have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  4.1426 +      using l using as(6) unfolding box_ne_empty[symmetric] by auto
  4.1427 +    show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  4.1428 +      apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  4.1429 +      using as(1-3,5) fstx
  4.1430 +      unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  4.1431 +      apply (auto split: if_split_asm)
  4.1432 +      done
  4.1433 +    show "snd x < b \<bullet> fst x"
  4.1434 +      using as(2) \<open>c < b\<bullet>k\<close> by (auto split: if_split_asm)
  4.1435 +  qed
  4.1436 +  show ?t2
  4.1437 +    unfolding division_points_def interval_split[OF k, of a b]
  4.1438 +    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  4.1439 +    unfolding *
  4.1440 +    unfolding subset_eq
  4.1441 +    apply rule
  4.1442 +    unfolding mem_Collect_eq split_beta
  4.1443 +    apply (erule bexE conjE)+
  4.1444 +    apply (simp only: mem_Collect_eq inner_setsum_left_Basis simp_thms)
  4.1445 +    apply (erule exE conjE)+
  4.1446 +  proof
  4.1447 +    fix i l x
  4.1448 +    assume as:
  4.1449 +      "(if fst x = k then c else a \<bullet> fst x) < snd x" "snd x < b \<bullet> fst x"
  4.1450 +      "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  4.1451 +      "i = l \<inter> {x. c \<le> x \<bullet> k}" "l \<in> d" "l \<inter> {x. c \<le> x \<bullet> k} \<noteq> {}"
  4.1452 +      and fstx: "fst x \<in> Basis"
  4.1453 +    from assm(4)[OF this(5)] guess u v by (elim exE) note l=this
  4.1454 +    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"
  4.1455 +      using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  4.1456 +    have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  4.1457 +      using l using as(6) unfolding box_ne_empty[symmetric] by auto
  4.1458 +    show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  4.1459 +      apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  4.1460 +      using as(1-3,5) fstx
  4.1461 +      unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  4.1462 +      apply (auto split: if_split_asm)
  4.1463 +      done
  4.1464 +    show "a \<bullet> fst x < snd x"
  4.1465 +      using as(1) \<open>a\<bullet>k < c\<close> by (auto split: if_split_asm)
  4.1466 +   qed
  4.1467 +qed
  4.1468 +
  4.1469 +lemma division_points_psubset:
  4.1470 +  fixes a :: "'a::euclidean_space"
  4.1471 +  assumes "d division_of (cbox a b)"
  4.1472 +      and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  4.1473 +      and "l \<in> d"
  4.1474 +      and "interval_lowerbound l\<bullet>k = c \<or> interval_upperbound l\<bullet>k = c"
  4.1475 +      and k: "k \<in> Basis"
  4.1476 +  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>
  4.1477 +         division_points (cbox a b) d" (is "?D1 \<subset> ?D")
  4.1478 +    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>
  4.1479 +         division_points (cbox a b) d" (is "?D2 \<subset> ?D")
  4.1480 +proof -
  4.1481 +  have ab: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  4.1482 +    using assms(2) by (auto intro!:less_imp_le)
  4.1483 +  guess u v using division_ofD(4)[OF assms(1,5)] by (elim exE) note l=this
  4.1484 +  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"
  4.1485 +    using division_ofD(2,2,3)[OF assms(1,5)] unfolding l box_ne_empty
  4.1486 +    using subset_box(1)
  4.1487 +    apply auto
  4.1488 +    apply blast+
  4.1489 +    done
  4.1490 +  have *: "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  4.1491 +          "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  4.1492 +    unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  4.1493 +    using uv[rule_format, of k] ab k
  4.1494 +    by auto
  4.1495 +  have "\<exists>x. x \<in> ?D - ?D1"
  4.1496 +    using assms(3-)
  4.1497 +    unfolding division_points_def interval_bounds[OF ab]
  4.1498 +    apply -
  4.1499 +    apply (erule disjE)
  4.1500 +    apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  4.1501 +    apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  4.1502 +    done
  4.1503 +  moreover have "?D1 \<subseteq> ?D"
  4.1504 +    by (auto simp add: assms division_points_subset)
  4.1505 +  ultimately show "?D1 \<subset> ?D"
  4.1506 +    by blast
  4.1507 +  have *: "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  4.1508 +    "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  4.1509 +    unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  4.1510 +    using uv[rule_format, of k] ab k
  4.1511 +    by auto
  4.1512 +  have "\<exists>x. x \<in> ?D - ?D2"
  4.1513 +    using assms(3-)
  4.1514 +    unfolding division_points_def interval_bounds[OF ab]
  4.1515 +    apply -
  4.1516 +    apply (erule disjE)
  4.1517 +    apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  4.1518 +    apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  4.1519 +    done
  4.1520 +  moreover have "?D2 \<subseteq> ?D"
  4.1521 +    by (auto simp add: assms division_points_subset)
  4.1522 +  ultimately show "?D2 \<subset> ?D"
  4.1523 +    by blast
  4.1524 +qed
  4.1525 +
  4.1526 +lemma division_split_left_inj:
  4.1527 +  fixes type :: "'a::euclidean_space"
  4.1528 +  assumes "d division_of i"
  4.1529 +    and "k1 \<in> d"
  4.1530 +    and "k2 \<in> d"
  4.1531 +    and "k1 \<noteq> k2"
  4.1532 +    and "k1 \<inter> {x::'a. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c}"
  4.1533 +    and k: "k\<in>Basis"
  4.1534 +  shows "interior (k1 \<inter> {x. x\<bullet>k \<le> c}) = {}"
  4.1535 +proof -
  4.1536 +  note d=division_ofD[OF assms(1)]
  4.1537 +  guess u1 v1 using d(4)[OF assms(2)] by (elim exE) note uv1=this
  4.1538 +  guess u2 v2 using d(4)[OF assms(3)] by (elim exE) note uv2=this
  4.1539 +  have **: "\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}"
  4.1540 +    by auto
  4.1541 +  show ?thesis
  4.1542 +    unfolding uv1 uv2
  4.1543 +    apply (rule **[OF d(5)[OF assms(2-4)]])
  4.1544 +    apply (simp add: uv1)
  4.1545 +    using assms(5) uv1 by auto
  4.1546 +qed
  4.1547 +
  4.1548 +lemma division_split_right_inj:
  4.1549 +  fixes type :: "'a::euclidean_space"
  4.1550 +  assumes "d division_of i"
  4.1551 +    and "k1 \<in> d"
  4.1552 +    and "k2 \<in> d"
  4.1553 +    and "k1 \<noteq> k2"
  4.1554 +    and "k1 \<inter> {x::'a. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c}"
  4.1555 +    and k: "k \<in> Basis"
  4.1556 +  shows "interior (k1 \<inter> {x. x\<bullet>k \<ge> c}) = {}"
  4.1557 +proof -
  4.1558 +  note d=division_ofD[OF assms(1)]
  4.1559 +  guess u1 v1 using d(4)[OF assms(2)] by (elim exE) note uv1=this
  4.1560 +  guess u2 v2 using d(4)[OF assms(3)] by (elim exE) note uv2=this
  4.1561 +  have **: "\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}"
  4.1562 +    by auto
  4.1563 +  show ?thesis
  4.1564 +    unfolding uv1 uv2
  4.1565 +    apply (rule **[OF d(5)[OF assms(2-4)]])
  4.1566 +    apply (simp add: uv1)
  4.1567 +    using assms(5) uv1 by auto
  4.1568 +qed
  4.1569 +
  4.1570 +lemma interval_doublesplit:
  4.1571 +  fixes a :: "'a::euclidean_space"
  4.1572 +  assumes "k \<in> Basis"
  4.1573 +  shows "cbox a b \<inter> {x . \<bar>x\<bullet>k - c\<bar> \<le> (e::real)} =
  4.1574 +    cbox (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) (c - e) else a\<bullet>i) *\<^sub>R i)
  4.1575 +     (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) (c + e) else b\<bullet>i) *\<^sub>R i)"
  4.1576 +proof -
  4.1577 +  have *: "\<And>x c e::real. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  4.1578 +    by auto
  4.1579 +  have **: "\<And>s P Q. s \<inter> {x. P x \<and> Q x} = (s \<inter> {x. Q x}) \<inter> {x. P x}"
  4.1580 +    by blast
  4.1581 +  show ?thesis
  4.1582 +    unfolding * ** interval_split[OF assms] by (rule refl)
  4.1583 +qed
  4.1584 +
  4.1585 +lemma division_doublesplit:
  4.1586 +  fixes a :: "'a::euclidean_space"
  4.1587 +  assumes "p division_of (cbox a b)"
  4.1588 +    and k: "k \<in> Basis"
  4.1589 +  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> {}}
  4.1590 +         division_of  (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> e})"
  4.1591 +proof -
  4.1592 +  have *: "\<And>x c. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  4.1593 +    by auto
  4.1594 +  have **: "\<And>p q p' q'. p division_of q \<Longrightarrow> p = p' \<Longrightarrow> q = q' \<Longrightarrow> p' division_of q'"
  4.1595 +    by auto
  4.1596 +  note division_split(1)[OF assms, where c="c+e",unfolded interval_split[OF k]]
  4.1597 +  note division_split(2)[OF this, where c="c-e" and k=k,OF k]
  4.1598 +  then show ?thesis
  4.1599 +    apply (rule **)
  4.1600 +    subgoal
  4.1601 +      apply (simp add: abs_diff_le_iff field_simps Collect_conj_eq setcompr_eq_image[symmetric])
  4.1602 +      apply (rule equalityI)
  4.1603 +      apply blast
  4.1604 +      apply clarsimp
  4.1605 +      apply (rule_tac x="l \<inter> {x. c + e \<ge> x \<bullet> k}" in exI)
  4.1606 +      apply auto
  4.1607 +      done
  4.1608 +    by (simp add: interval_split k interval_doublesplit)
  4.1609 +qed
  4.1610 +
  4.1611 +lemma (in comm_monoid_set) operative_division:
  4.1612 +  fixes g :: "'b::euclidean_space set \<Rightarrow> 'a"
  4.1613 +  assumes g: "operative g" and d: "d division_of (cbox a b)" shows "F g d = g (cbox a b)"
  4.1614 +proof -
  4.1615 +  define C where [abs_def]: "C = card (division_points (cbox a b) d)"
  4.1616 +  then show ?thesis
  4.1617 +    using d
  4.1618 +  proof (induction C arbitrary: a b d rule: less_induct)
  4.1619 +    case (less a b d)
  4.1620 +    show ?case
  4.1621 +    proof cases
  4.1622 +      assume "box a b = {}"
  4.1623 +      { fix k assume "k\<in>d"
  4.1624 +        then obtain a' b' where k: "k = cbox a' b'"
  4.1625 +          using division_ofD(4)[OF less.prems] by blast
  4.1626 +        with \<open>k\<in>d\<close> division_ofD(2)[OF less.prems] have "cbox a' b' \<subseteq> cbox a b"
  4.1627 +          by auto
  4.1628 +        then have "box a' b' \<subseteq> box a b"
  4.1629 +          unfolding subset_box by auto
  4.1630 +        then have "g k = \<^bold>1"
  4.1631 +          using operativeD(1)[OF g, of a' b'] k by (simp add: \<open>box a b = {}\<close>) }
  4.1632 +      then show "box a b = {} \<Longrightarrow> F g d = g (cbox a b)"
  4.1633 +        by (auto intro!: neutral simp: operativeD(1)[OF g])
  4.1634 +    next
  4.1635 +      assume "box a b \<noteq> {}"
  4.1636 +      then have ab: "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i" and ab': "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  4.1637 +        by (auto simp: box_ne_empty)
  4.1638 +      show "F g d = g (cbox a b)"
  4.1639 +      proof (cases "division_points (cbox a b) d = {}")
  4.1640 +        case True
  4.1641 +        { fix u v and j :: 'b
  4.1642 +          assume j: "j \<in> Basis" and as: "cbox u v \<in> d"
  4.1643 +          then have "cbox u v \<noteq> {}"
  4.1644 +            using less.prems by blast
  4.1645 +          then have uv: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i" "u\<bullet>j \<le> v\<bullet>j"
  4.1646 +            using j unfolding box_ne_empty by auto
  4.1647 +          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)"
  4.1648 +            using as j by auto
  4.1649 +          have "(j, u\<bullet>j) \<notin> division_points (cbox a b) d"
  4.1650 +               "(j, v\<bullet>j) \<notin> division_points (cbox a b) d" using True by auto
  4.1651 +          note this[unfolded de_Morgan_conj division_points_def mem_Collect_eq split_conv interval_bounds[OF ab'] bex_simps]
  4.1652 +          note *[OF this(1)] *[OF this(2)] note this[unfolded interval_bounds[OF uv(1)]]
  4.1653 +          moreover
  4.1654 +          have "a\<bullet>j \<le> u\<bullet>j" "v\<bullet>j \<le> b\<bullet>j"
  4.1655 +            using division_ofD(2,2,3)[OF \<open>d division_of cbox a b\<close> as]
  4.1656 +            apply (metis j subset_box(1) uv(1))
  4.1657 +            by (metis \<open>cbox u v \<subseteq> cbox a b\<close> j subset_box(1) uv(1))
  4.1658 +          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"
  4.1659 +            unfolding not_less de_Morgan_disj using ab[rule_format,of j] uv(2) j by force }
  4.1660 +        then have d': "\<forall>i\<in>d. \<exists>u v. i = cbox u v \<and>
  4.1661 +          (\<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)"
  4.1662 +          unfolding forall_in_division[OF less.prems] by blast
  4.1663 +        have "(1/2) *\<^sub>R (a+b) \<in> cbox a b"
  4.1664 +          unfolding mem_box using ab by (auto simp: inner_simps)
  4.1665 +        note this[unfolded division_ofD(6)[OF \<open>d division_of cbox a b\<close>,symmetric] Union_iff]
  4.1666 +        then guess i .. note i=this
  4.1667 +        guess u v using d'[rule_format,OF i(1)] by (elim exE conjE) note uv=this
  4.1668 +        have "cbox a b \<in> d"
  4.1669 +        proof -
  4.1670 +          have "u = a" "v = b"
  4.1671 +            unfolding euclidean_eq_iff[where 'a='b]
  4.1672 +          proof safe
  4.1673 +            fix j :: 'b
  4.1674 +            assume j: "j \<in> Basis"
  4.1675 +            note i(2)[unfolded uv mem_box,rule_format,of j]
  4.1676 +            then show "u \<bullet> j = a \<bullet> j" and "v \<bullet> j = b \<bullet> j"
  4.1677 +              using uv(2)[rule_format,of j] j by (auto simp: inner_simps)
  4.1678 +          qed
  4.1679 +          then have "i = cbox a b" using uv by auto
  4.1680 +          then show ?thesis using i by auto
  4.1681 +        qed
  4.1682 +        then have deq: "d = insert (cbox a b) (d - {cbox a b})"
  4.1683 +          by auto
  4.1684 +        have "F g (d - {cbox a b}) = \<^bold>1"
  4.1685 +        proof (intro neutral ballI)
  4.1686 +          fix x
  4.1687 +          assume x: "x \<in> d - {cbox a b}"
  4.1688 +          then have "x\<in>d"
  4.1689 +            by auto note d'[rule_format,OF this]
  4.1690 +          then guess u v by (elim exE conjE) note uv=this
  4.1691 +          have "u \<noteq> a \<or> v \<noteq> b"
  4.1692 +            using x[unfolded uv] by auto
  4.1693 +          then obtain j where "u\<bullet>j \<noteq> a\<bullet>j \<or> v\<bullet>j \<noteq> b\<bullet>j" and j: "j \<in> Basis"
  4.1694 +            unfolding euclidean_eq_iff[where 'a='b] by auto
  4.1695 +          then have "u\<bullet>j = v\<bullet>j"
  4.1696 +            using uv(2)[rule_format,OF j] by auto
  4.1697 +          then have "box u v = {}"
  4.1698 +            using j unfolding box_eq_empty by (auto intro!: bexI[of _ j])
  4.1699 +          then show "g x = \<^bold>1"
  4.1700 +            unfolding uv(1) by (rule operativeD(1)[OF g])
  4.1701 +        qed
  4.1702 +        then show "F g d = g (cbox a b)"
  4.1703 +          using division_ofD[OF less.prems]
  4.1704 +          apply (subst deq)
  4.1705 +          apply (subst insert)
  4.1706 +          apply auto
  4.1707 +          done
  4.1708 +      next
  4.1709 +        case False
  4.1710 +        then have "\<exists>x. x \<in> division_points (cbox a b) d"
  4.1711 +          by auto
  4.1712 +        then guess k c
  4.1713 +          unfolding split_paired_Ex division_points_def mem_Collect_eq split_conv
  4.1714 +          apply (elim exE conjE)
  4.1715 +          done
  4.1716 +        note this(2-4,1) note kc=this[unfolded interval_bounds[OF ab']]
  4.1717 +        from this(3) guess j .. note j=this
  4.1718 +        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> {}}"
  4.1719 +        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> {}}"
  4.1720 +        define cb where "cb = (\<Sum>i\<in>Basis. (if i = k then c else b\<bullet>i) *\<^sub>R i)"
  4.1721 +        define ca where "ca = (\<Sum>i\<in>Basis. (if i = k then c else a\<bullet>i) *\<^sub>R i)"
  4.1722 +        note division_points_psubset[OF \<open>d division_of cbox a b\<close> ab kc(1-2) j]
  4.1723 +        note psubset_card_mono[OF _ this(1)] psubset_card_mono[OF _ this(2)]
  4.1724 +        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})"
  4.1725 +          unfolding interval_split[OF kc(4)]
  4.1726 +          apply (rule_tac[!] "less.hyps"[rule_format])
  4.1727 +          using division_split[OF \<open>d division_of cbox a b\<close>, where k=k and c=c]
  4.1728 +          apply (simp_all add: interval_split kc d1_def d2_def division_points_finite[OF \<open>d division_of cbox a b\<close>])
  4.1729 +          done
  4.1730 +        { fix l y
  4.1731 +          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"
  4.1732 +          from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  4.1733 +          have "g (l \<inter> {x. x \<bullet> k \<le> c}) = \<^bold>1"
  4.1734 +            unfolding leq interval_split[OF kc(4)]
  4.1735 +            apply (rule operativeD[OF g])
  4.1736 +            unfolding interior_cbox[symmetric] interval_split[symmetric, OF kc(4)]
  4.1737 +            using division_split_left_inj less as kc leq by blast
  4.1738 +        } note fxk_le = this
  4.1739 +        { fix l y
  4.1740 +          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"
  4.1741 +          from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  4.1742 +          have "g (l \<inter> {x. x \<bullet> k \<ge> c}) = \<^bold>1"
  4.1743 +            unfolding leq interval_split[OF kc(4)]
  4.1744 +            apply (rule operativeD(1)[OF g])
  4.1745 +            unfolding interior_cbox[symmetric] interval_split[symmetric,OF kc(4)]
  4.1746 +            using division_split_right_inj less leq as kc by blast
  4.1747 +        } note fxk_ge = this
  4.1748 +        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> {}}"
  4.1749 +          using d1_def by auto
  4.1750 +        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> {}}"
  4.1751 +          using d2_def by auto
  4.1752 +        have "g (cbox a b) = F g d1 \<^bold>* F g d2" (is "_ = ?prev")
  4.1753 +          unfolding * using g kc(4) by blast
  4.1754 +        also have "F g d1 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<le> c})) d"
  4.1755 +          unfolding d1_alt using division_of_finite[OF less.prems] fxk_le
  4.1756 +          by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  4.1757 +        also have "F g d2 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<ge> c})) d"
  4.1758 +          unfolding d2_alt using division_of_finite[OF less.prems] fxk_ge
  4.1759 +          by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  4.1760 +        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})"
  4.1761 +          unfolding forall_in_division[OF \<open>d division_of cbox a b\<close>]
  4.1762 +          using g kc(4) by blast
  4.1763 +        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"
  4.1764 +          using * by (simp add: distrib)
  4.1765 +        finally show ?thesis by auto
  4.1766 +      qed
  4.1767 +    qed
  4.1768 +  qed
  4.1769 +qed
  4.1770 +
  4.1771 +lemma (in comm_monoid_set) over_tagged_division_lemma:
  4.1772 +  assumes "p tagged_division_of i"
  4.1773 +    and "\<And>u v. cbox u v \<noteq> {} \<Longrightarrow> box u v = {} \<Longrightarrow> d (cbox u v) = \<^bold>1"
  4.1774 +  shows "F (\<lambda>(x,k). d k) p = F d (snd ` p)"
  4.1775 +proof -
  4.1776 +  have *: "(\<lambda>(x,k). d k) = d \<circ> snd"
  4.1777 +    unfolding o_def by (rule ext) auto
  4.1778 +  note assm = tagged_division_ofD[OF assms(1)]
  4.1779 +  show ?thesis
  4.1780 +    unfolding *
  4.1781 +  proof (rule reindex_nontrivial[symmetric])
  4.1782 +    show "finite p"
  4.1783 +      using assm by auto
  4.1784 +    fix x y
  4.1785 +    assume "x\<in>p" "y\<in>p" "x\<noteq>y" "snd x = snd y"
  4.1786 +    obtain a b where ab: "snd x = cbox a b"
  4.1787 +      using assm(4)[of "fst x" "snd x"] \<open>x\<in>p\<close> by auto
  4.1788 +    have "(fst x, snd y) \<in> p" "(fst x, snd y) \<noteq> y"
  4.1789 +      by (metis prod.collapse \<open>x\<in>p\<close> \<open>snd x = snd y\<close> \<open>x \<noteq> y\<close>)+
  4.1790 +    with \<open>x\<in>p\<close> \<open>y\<in>p\<close> have "interior (snd x) \<inter> interior (snd y) = {}"
  4.1791 +      by (intro assm(5)[of "fst x" _ "fst y"]) auto
  4.1792 +    then have "box a b = {}"
  4.1793 +      unfolding \<open>snd x = snd y\<close>[symmetric] ab by auto
  4.1794 +    then have "d (cbox a b) = \<^bold>1"
  4.1795 +      using assm(2)[of "fst x" "snd x"] \<open>x\<in>p\<close> ab[symmetric] by (intro assms(2)) auto
  4.1796 +    then show "d (snd x) = \<^bold>1"
  4.1797 +      unfolding ab by auto
  4.1798 +  qed
  4.1799 +qed
  4.1800 +
  4.1801 +lemma (in comm_monoid_set) operative_tagged_division:
  4.1802 +  assumes f: "operative g" and d: "d tagged_division_of (cbox a b)"
  4.1803 +  shows "F (\<lambda>(x, l). g l) d = g (cbox a b)"
  4.1804 +  unfolding d[THEN division_of_tagged_division, THEN operative_division[OF f], symmetric]
  4.1805 +  by (simp add: f[THEN operativeD(1)] over_tagged_division_lemma[OF d])
  4.1806 +
  4.1807 +lemma interval_real_split:
  4.1808 +  "{a .. b::real} \<inter> {x. x \<le> c} = {a .. min b c}"
  4.1809 +  "{a .. b} \<inter> {x. c \<le> x} = {max a c .. b}"
  4.1810 +  apply (metis Int_atLeastAtMostL1 atMost_def)
  4.1811 +  apply (metis Int_atLeastAtMostL2 atLeast_def)
  4.1812 +  done
  4.1813 +
  4.1814 +lemma (in comm_monoid) operative_1_lt:
  4.1815 +  "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  4.1816 +    ((\<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}))"
  4.1817 +  apply (simp add: operative_def atMost_def[symmetric] atLeast_def[symmetric])
  4.1818 +proof safe
  4.1819 +  fix a b c :: real
  4.1820 +  assume *: "\<forall>a b c. g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  4.1821 +  assume "a < c" "c < b"
  4.1822 +  with *[rule_format, of a b c] show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  4.1823 +    by (simp add: less_imp_le min.absorb2 max.absorb2)
  4.1824 +next
  4.1825 +  fix a b c :: real
  4.1826 +  assume as: "\<forall>a b. b \<le> a \<longrightarrow> g {a..b} = \<^bold>1"
  4.1827 +    "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  4.1828 +  from as(1)[rule_format, of 0 1] as(1)[rule_format, of a a for a] as(2)
  4.1829 +  have [simp]: "g {} = \<^bold>1" "\<And>a. g {a} = \<^bold>1"
  4.1830 +    "\<And>a b c. a < c \<Longrightarrow> c < b \<Longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  4.1831 +    by auto
  4.1832 +  show "g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  4.1833 +    by (auto simp: min_def max_def le_less)
  4.1834 +qed
  4.1835 +
  4.1836 +lemma (in comm_monoid) operative_1_le:
  4.1837 +  "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  4.1838 +    ((\<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}))"
  4.1839 +  unfolding operative_1_lt
  4.1840 +proof safe
  4.1841 +  fix a b c :: real
  4.1842 +  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"
  4.1843 +  show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  4.1844 +    apply (rule as(1)[rule_format])
  4.1845 +    using as(2-)
  4.1846 +    apply auto
  4.1847 +    done
  4.1848 +next
  4.1849 +  fix a b c :: real
  4.1850 +  assume "\<forall>a b. b \<le> a \<longrightarrow> g {a .. b} = \<^bold>1"
  4.1851 +    and "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  4.1852 +    and "a \<le> c"
  4.1853 +    and "c \<le> b"
  4.1854 +  note as = this[rule_format]
  4.1855 +  show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  4.1856 +  proof (cases "c = a \<or> c = b")
  4.1857 +    case False
  4.1858 +    then show ?thesis
  4.1859 +      apply -
  4.1860 +      apply (subst as(2))
  4.1861 +      using as(3-)
  4.1862 +      apply auto
  4.1863 +      done
  4.1864 +  next
  4.1865 +    case True
  4.1866 +    then show ?thesis
  4.1867 +    proof
  4.1868 +      assume *: "c = a"
  4.1869 +      then have "g {a .. c} = \<^bold>1"
  4.1870 +        apply -
  4.1871 +        apply (rule as(1)[rule_format])
  4.1872 +        apply auto
  4.1873 +        done
  4.1874 +      then show ?thesis
  4.1875 +        unfolding * by auto
  4.1876 +    next
  4.1877 +      assume *: "c = b"
  4.1878 +      then have "g {c .. b} = \<^bold>1"
  4.1879 +        apply -
  4.1880 +        apply (rule as(1)[rule_format])
  4.1881 +        apply auto
  4.1882 +        done
  4.1883 +      then show ?thesis
  4.1884 +        unfolding * by auto
  4.1885 +    qed
  4.1886 +  qed
  4.1887 +qed
  4.1888 +
  4.1889 +lemma tagged_division_union_interval:
  4.1890 +  fixes a :: "'a::euclidean_space"
  4.1891 +  assumes "p1 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<le> (c::real)})"
  4.1892 +    and "p2 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  4.1893 +    and k: "k \<in> Basis"
  4.1894 +  shows "(p1 \<union> p2) tagged_division_of (cbox a b)"
  4.1895 +proof -
  4.1896 +  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})"
  4.1897 +    by auto
  4.1898 +  show ?thesis
  4.1899 +    apply (subst *)
  4.1900 +    apply (rule tagged_division_union[OF assms(1-2)])
  4.1901 +    unfolding interval_split[OF k] interior_cbox
  4.1902 +    using k
  4.1903 +    apply (auto simp add: box_def elim!: ballE[where x=k])
  4.1904 +    done
  4.1905 +qed
  4.1906 +
  4.1907 +lemma tagged_division_union_interval_real:
  4.1908 +  fixes a :: real
  4.1909 +  assumes "p1 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<le> (c::real)})"
  4.1910 +    and "p2 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<ge> c})"
  4.1911 +    and k: "k \<in> Basis"
  4.1912 +  shows "(p1 \<union> p2) tagged_division_of {a .. b}"
  4.1913 +  using assms
  4.1914 +  unfolding box_real[symmetric]
  4.1915 +  by (rule tagged_division_union_interval)
  4.1916 +
  4.1917 +lemma tagged_division_split_left_inj:
  4.1918 +  "d tagged_division_of i \<Longrightarrow> (x1, k1) \<in> d \<Longrightarrow> (x2, k2) \<in> d \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow>
  4.1919 +    k1 \<inter> {x. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c} \<Longrightarrow> k \<in> Basis \<Longrightarrow>
  4.1920 +    interior (k1 \<inter> {x. x\<bullet>k \<le> c}) = {}"
  4.1921 +  by (intro division_split_left_inj[of "snd`d" i k1 k2, OF division_of_tagged_division])
  4.1922 +     (auto simp add: snd_def[abs_def] image_iff split: prod.split )
  4.1923 +
  4.1924 +lemma tagged_division_split_right_inj:
  4.1925 +  "d tagged_division_of i \<Longrightarrow> (x1, k1) \<in> d \<Longrightarrow> (x2, k2) \<in> d \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow>
  4.1926 +    k1 \<inter> {x. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c} \<Longrightarrow> k \<in> Basis \<Longrightarrow>
  4.1927 +    interior (k1 \<inter> {x. x\<bullet>k \<ge> c}) = {}"
  4.1928 +  by (intro division_split_right_inj[of "snd`d" i k1 k2, OF division_of_tagged_division])
  4.1929 +     (auto simp add: snd_def[abs_def] image_iff split: prod.split )
  4.1930 +
  4.1931 +subsection \<open>Special case of additivity we need for the FTC.\<close>
  4.1932 +
  4.1933 +lemma additive_tagged_division_1:
  4.1934 +  fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  4.1935 +  assumes "a \<le> b"
  4.1936 +    and "p tagged_division_of {a..b}"
  4.1937 +  shows "setsum (\<lambda>(x,k). f(Sup k) - f(Inf k)) p = f b - f a"
  4.1938 +proof -
  4.1939 +  let ?f = "(\<lambda>k::(real) set. if k = {} then 0 else f(interval_upperbound k) - f(interval_lowerbound k))"
  4.1940 +  have ***: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
  4.1941 +    using assms by auto
  4.1942 +  have *: "add.operative ?f"
  4.1943 +    unfolding add.operative_1_lt box_eq_empty
  4.1944 +    by auto
  4.1945 +  have **: "cbox a b \<noteq> {}"
  4.1946 +    using assms(1) by auto
  4.1947 +  note setsum.operative_tagged_division[OF * assms(2)[simplified box_real[symmetric]]]
  4.1948 +  note * = this[unfolded if_not_P[OF **] interval_bounds[OF ***],symmetric]
  4.1949 +  show ?thesis
  4.1950 +    unfolding *
  4.1951 +    apply (rule setsum.cong)
  4.1952 +    unfolding split_paired_all split_conv
  4.1953 +    using assms(2)
  4.1954 +    apply auto
  4.1955 +    done
  4.1956 +qed
  4.1957 +
  4.1958 +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)"
  4.1959 +  by (meson zero_less_one)
  4.1960 +
  4.1961 +lemma additive_tagged_division_1':
  4.1962 +  fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  4.1963 +  assumes "a \<le> b"
  4.1964 +    and "p tagged_division_of {a..b}"
  4.1965 +  shows "setsum (\<lambda>(x,k). f (Sup k) - f(Inf k)) p = f b - f a"
  4.1966 +  using additive_tagged_division_1[OF _ assms(2), of f]
  4.1967 +  using assms(1)
  4.1968 +  by auto
  4.1969 +
  4.1970 +subsection \<open>Fine-ness of a partition w.r.t. a gauge.\<close>
  4.1971 +
  4.1972 +definition fine  (infixr "fine" 46)
  4.1973 +  where "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d x)"
  4.1974 +
  4.1975 +lemma fineI:
  4.1976 +  assumes "\<And>x k. (x, k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  4.1977 +  shows "d fine s"
  4.1978 +  using assms unfolding fine_def by auto
  4.1979 +
  4.1980 +lemma fineD[dest]:
  4.1981 +  assumes "d fine s"
  4.1982 +  shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  4.1983 +  using assms unfolding fine_def by auto
  4.1984 +
  4.1985 +lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
  4.1986 +  unfolding fine_def by auto
  4.1987 +
  4.1988 +lemma fine_inters:
  4.1989 + "(\<lambda>x. \<Inter>{f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
  4.1990 +  unfolding fine_def by blast
  4.1991 +
  4.1992 +lemma fine_union: "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
  4.1993 +  unfolding fine_def by blast
  4.1994 +
  4.1995 +lemma fine_unions: "(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
  4.1996 +  unfolding fine_def by auto
  4.1997 +
  4.1998 +lemma fine_subset: "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
  4.1999 +  unfolding fine_def by blast
  4.2000 +
  4.2001 +subsection \<open>Some basic combining lemmas.\<close>
  4.2002 +
  4.2003 +lemma tagged_division_unions_exists:
  4.2004 +  assumes "finite iset"
  4.2005 +    and "\<forall>i\<in>iset. \<exists>p. p tagged_division_of i \<and> d fine p"
  4.2006 +    and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior i1 \<inter> interior i2 = {}"
  4.2007 +    and "\<Union>iset = i"
  4.2008 +   obtains p where "p tagged_division_of i" and "d fine p"
  4.2009 +proof -
  4.2010 +  obtain pfn where pfn:
  4.2011 +    "\<And>x. x \<in> iset \<Longrightarrow> pfn x tagged_division_of x"
  4.2012 +    "\<And>x. x \<in> iset \<Longrightarrow> d fine pfn x"
  4.2013 +    using bchoice[OF assms(2)] by auto
  4.2014 +  show thesis
  4.2015 +    apply (rule_tac p="\<Union>(pfn ` iset)" in that)
  4.2016 +    using assms(1) assms(3) assms(4) pfn(1) tagged_division_unions apply force
  4.2017 +    by (metis (mono_tags, lifting) fine_unions imageE pfn(2))
  4.2018 +qed
  4.2019 +
  4.2020 +
  4.2021 +subsection \<open>The set we're concerned with must be closed.\<close>
  4.2022 +
  4.2023 +lemma division_of_closed:
  4.2024 +  fixes i :: "'n::euclidean_space set"
  4.2025 +  shows "s division_of i \<Longrightarrow> closed i"
  4.2026 +  unfolding division_of_def by fastforce
  4.2027 +
  4.2028 +subsection \<open>General bisection principle for intervals; might be useful elsewhere.\<close>
  4.2029 +
  4.2030 +lemma interval_bisection_step:
  4.2031 +  fixes type :: "'a::euclidean_space"
  4.2032 +  assumes "P {}"
  4.2033 +    and "\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P (s \<union> t)"
  4.2034 +    and "\<not> P (cbox a (b::'a))"
  4.2035 +  obtains c d where "\<not> P (cbox c d)"
  4.2036 +    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"
  4.2037 +proof -
  4.2038 +  have "cbox a b \<noteq> {}"
  4.2039 +    using assms(1,3) by metis
  4.2040 +  then have ab: "\<And>i. i\<in>Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i"
  4.2041 +    by (force simp: mem_box)
  4.2042 +  { fix f
  4.2043 +    have "\<lbrakk>finite f;
  4.2044 +           \<And>s. s\<in>f \<Longrightarrow> P s;
  4.2045 +           \<And>s. s\<in>f \<Longrightarrow> \<exists>a b. s = cbox a b;
  4.2046 +           \<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)"
  4.2047 +    proof (induct f rule: finite_induct)
  4.2048 +      case empty
  4.2049 +      show ?case
  4.2050 +        using assms(1) by auto
  4.2051 +    next
  4.2052 +      case (insert x f)
  4.2053 +      show ?case
  4.2054 +        unfolding Union_insert
  4.2055 +        apply (rule assms(2)[rule_format])
  4.2056 +        using inter_interior_unions_intervals [of f "interior x"]
  4.2057 +        apply (auto simp: insert)
  4.2058 +        by (metis IntI empty_iff insert.hyps(2) insert.prems(3) insert_iff)
  4.2059 +    qed
  4.2060 +  } note UN_cases = this
  4.2061 +  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>
  4.2062 +    (c\<bullet>i = (a\<bullet>i + b\<bullet>i) / 2) \<and> (d\<bullet>i = b\<bullet>i)}"
  4.2063 +  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"
  4.2064 +  {
  4.2065 +    presume "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d) \<Longrightarrow> False"
  4.2066 +    then show thesis
  4.2067 +      unfolding atomize_not not_all
  4.2068 +      by (blast intro: that)
  4.2069 +  }
  4.2070 +  assume as: "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d)"
  4.2071 +  have "P (\<Union>?A)"
  4.2072 +  proof (rule UN_cases)
  4.2073 +    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)
  4.2074 +      (\<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}"
  4.2075 +    have "?A \<subseteq> ?B"
  4.2076 +    proof
  4.2077 +      fix x
  4.2078 +      assume "x \<in> ?A"
  4.2079 +      then obtain c d
  4.2080 +        where x:  "x = cbox c d"
  4.2081 +                  "\<And>i. i \<in> Basis \<Longrightarrow>
  4.2082 +                        c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  4.2083 +                        c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i" by blast
  4.2084 +      show "x \<in> ?B"
  4.2085 +        unfolding image_iff x
  4.2086 +        apply (rule_tac x="{i. i\<in>Basis \<and> c\<bullet>i = a\<bullet>i}" in bexI)
  4.2087 +        apply (rule arg_cong2 [where f = cbox])
  4.2088 +        using x(2) ab
  4.2089 +        apply (auto simp add: euclidean_eq_iff[where 'a='a])
  4.2090 +        by fastforce
  4.2091 +    qed
  4.2092 +    then show "finite ?A"
  4.2093 +      by (rule finite_subset) auto
  4.2094 +  next
  4.2095 +    fix s
  4.2096 +    assume "s \<in> ?A"
  4.2097 +    then obtain c d
  4.2098 +      where s: "s = cbox c d"
  4.2099 +               "\<And>i. i \<in> Basis \<Longrightarrow>
  4.2100 +                     c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  4.2101 +                     c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  4.2102 +      by blast
  4.2103 +    show "P s"
  4.2104 +      unfolding s
  4.2105 +      apply (rule as[rule_format])
  4.2106 +      using ab s(2) by force
  4.2107 +    show "\<exists>a b. s = cbox a b"
  4.2108 +      unfolding s by auto
  4.2109 +    fix t
  4.2110 +    assume "t \<in> ?A"
  4.2111 +    then obtain e f where t:
  4.2112 +      "t = cbox e f"
  4.2113 +      "\<And>i. i \<in> Basis \<Longrightarrow>
  4.2114 +        e \<bullet> i = a \<bullet> i \<and> f \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  4.2115 +        e \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> f \<bullet> i = b \<bullet> i"
  4.2116 +      by blast
  4.2117 +    assume "s \<noteq> t"
  4.2118 +    then have "\<not> (c = e \<and> d = f)"
  4.2119 +      unfolding s t by auto
  4.2120 +    then obtain i where "c\<bullet>i \<noteq> e\<bullet>i \<or> d\<bullet>i \<noteq> f\<bullet>i" and i': "i \<in> Basis"
  4.2121 +      unfolding euclidean_eq_iff[where 'a='a] by auto
  4.2122 +    then have i: "c\<bullet>i \<noteq> e\<bullet>i" "d\<bullet>i \<noteq> f\<bullet>i"
  4.2123 +      using s(2) t(2) apply fastforce
  4.2124 +      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
  4.2125 +    have *: "\<And>s t. (\<And>a. a \<in> s \<Longrightarrow> a \<in> t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}"
  4.2126 +      by auto
  4.2127 +    show "interior s \<inter> interior t = {}"
  4.2128 +      unfolding s t interior_cbox
  4.2129 +    proof (rule *)
  4.2130 +      fix x
  4.2131 +      assume "x \<in> box c d" "x \<in> box e f"
  4.2132 +      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"
  4.2133 +        unfolding mem_box using i'
  4.2134 +        by force+
  4.2135 +      show False  using s(2)[OF i']
  4.2136 +      proof safe
  4.2137 +        assume as: "c \<bullet> i = a \<bullet> i" "d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2"
  4.2138 +        show False
  4.2139 +          using t(2)[OF i'] and i x unfolding as by (fastforce simp add:field_simps)
  4.2140 +      next
  4.2141 +        assume as: "c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2" "d \<bullet> i = b \<bullet> i"
  4.2142 +        show False
  4.2143 +          using t(2)[OF i'] and i x unfolding as by(fastforce simp add:field_simps)
  4.2144 +      qed
  4.2145 +    qed
  4.2146 +  qed
  4.2147 +  also have "\<Union>?A = cbox a b"
  4.2148 +  proof (rule set_eqI,rule)
  4.2149 +    fix x
  4.2150 +    assume "x \<in> \<Union>?A"
  4.2151 +    then obtain c d where x:
  4.2152 +      "x \<in> cbox c d"
  4.2153 +      "\<And>i. i \<in> Basis \<Longrightarrow>
  4.2154 +        c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  4.2155 +        c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  4.2156 +      by blast
  4.2157 +    show "x\<in>cbox a b"
  4.2158 +      unfolding mem_box
  4.2159 +    proof safe
  4.2160 +      fix i :: 'a
  4.2161 +      assume i: "i \<in> Basis"
  4.2162 +      then show "a \<bullet> i \<le> x \<bullet> i" "x \<bullet> i \<le> b \<bullet> i"
  4.2163 +        using x(2)[OF i] x(1)[unfolded mem_box,THEN bspec, OF i] by auto
  4.2164 +    qed
  4.2165 +  next
  4.2166 +    fix x
  4.2167 +    assume x: "x \<in> cbox a b"
  4.2168 +    have "\<forall>i\<in>Basis.
  4.2169 +      \<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"
  4.2170 +      (is "\<forall>i\<in>Basis. \<exists>c d. ?P i c d")
  4.2171 +      unfolding mem_box
  4.2172 +    proof
  4.2173 +      fix i :: 'a
  4.2174 +      assume i: "i \<in> Basis"
  4.2175 +      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)"
  4.2176 +        using x[unfolded mem_box,THEN bspec, OF i] by auto
  4.2177 +      then show "\<exists>c d. ?P i c d"
  4.2178 +        by blast
  4.2179 +    qed
  4.2180 +    then show "x\<in>\<Union>?A"
  4.2181 +      unfolding Union_iff Bex_def mem_Collect_eq choice_Basis_iff
  4.2182 +      apply auto
  4.2183 +      apply (rule_tac x="cbox xa xaa" in exI)
  4.2184 +      unfolding mem_box
  4.2185 +      apply auto
  4.2186 +      done
  4.2187 +  qed
  4.2188 +  finally show False
  4.2189 +    using assms by auto
  4.2190 +qed
  4.2191 +
  4.2192 +lemma interval_bisection:
  4.2193 +  fixes type :: "'a::euclidean_space"
  4.2194 +  assumes "P {}"
  4.2195 +    and "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))"
  4.2196 +    and "\<not> P (cbox a (b::'a))"
  4.2197 +  obtains x where "x \<in> cbox a b"
  4.2198 +    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)"
  4.2199 +proof -
  4.2200 +  have "\<forall>x. \<exists>y. \<not> P (cbox (fst x) (snd x)) \<longrightarrow> (\<not> P (cbox (fst y) (snd y)) \<and>
  4.2201 +    (\<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>
  4.2202 +       2 * (snd y\<bullet>i - fst y\<bullet>i) \<le> snd x\<bullet>i - fst x\<bullet>i))" (is "\<forall>x. ?P x")
  4.2203 +  proof
  4.2204 +    show "?P x" for x
  4.2205 +    proof (cases "P (cbox (fst x) (snd x))")
  4.2206 +      case True
  4.2207 +      then show ?thesis by auto
  4.2208 +    next
  4.2209 +      case as: False
  4.2210 +      obtain c d where "\<not> P (cbox c d)"
  4.2211 +        "\<forall>i\<in>Basis.
  4.2212 +           fst x \<bullet> i \<le> c \<bullet> i \<and>
  4.2213 +           c \<bullet> i \<le> d \<bullet> i \<and>
  4.2214 +           d \<bullet> i \<le> snd x \<bullet> i \<and>
  4.2215 +           2 * (d \<bullet> i - c \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i"
  4.2216 +        by (rule interval_bisection_step[of P, OF assms(1-2) as])
  4.2217 +      then show ?thesis
  4.2218 +        apply -
  4.2219 +        apply (rule_tac x="(c,d)" in exI)
  4.2220 +        apply auto
  4.2221 +        done
  4.2222 +    qed
  4.2223 +  qed
  4.2224 +  then obtain f where f:
  4.2225 +    "\<forall>x.
  4.2226 +      \<not> P (cbox (fst x) (snd x)) \<longrightarrow>
  4.2227 +      \<not> P (cbox (fst (f x)) (snd (f x))) \<and>
  4.2228 +        (\<forall>i\<in>Basis.
  4.2229 +            fst x \<bullet> i \<le> fst (f x) \<bullet> i \<and>
  4.2230 +            fst (f x) \<bullet> i \<le> snd (f x) \<bullet> i \<and>
  4.2231 +            snd (f x) \<bullet> i \<le> snd x \<bullet> i \<and>
  4.2232 +            2 * (snd (f x) \<bullet> i - fst (f x) \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i)"
  4.2233 +    apply -
  4.2234 +    apply (drule choice)
  4.2235 +    apply blast
  4.2236 +    done
  4.2237 +  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
  4.2238 +  have "A 0 = a" "B 0 = b" "\<And>n. \<not> P (cbox (A(Suc n)) (B(Suc n))) \<and>
  4.2239 +    (\<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>
  4.2240 +    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")
  4.2241 +  proof -
  4.2242 +    show "A 0 = a" "B 0 = b"
  4.2243 +      unfolding ab_def by auto
  4.2244 +    note S = ab_def funpow.simps o_def id_apply
  4.2245 +    show "?P n" for n
  4.2246 +    proof (induct n)
  4.2247 +      case 0
  4.2248 +      then show ?case
  4.2249 +        unfolding S
  4.2250 +        apply (rule f[rule_format]) using assms(3)
  4.2251 +        apply auto
  4.2252 +        done
  4.2253 +    next
  4.2254 +      case (Suc n)
  4.2255 +      show ?case
  4.2256 +        unfolding S
  4.2257 +        apply (rule f[rule_format])
  4.2258 +        using Suc
  4.2259 +        unfolding S
  4.2260 +        apply auto
  4.2261 +        done
  4.2262 +    qed
  4.2263 +  qed
  4.2264 +  note AB = this(1-2) conjunctD2[OF this(3),rule_format]
  4.2265 +
  4.2266 +  have interv: "\<exists>n. \<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e"
  4.2267 +    if e: "0 < e" for e
  4.2268 +  proof -
  4.2269 +    obtain n where n: "(\<Sum>i\<in>Basis. b \<bullet> i - a \<bullet> i) / e < 2 ^ n"
  4.2270 +      using real_arch_pow[of 2 "(setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis) / e"] by auto
  4.2271 +    show ?thesis
  4.2272 +    proof (rule exI [where x=n], clarify)
  4.2273 +      fix x y
  4.2274 +      assume xy: "x\<in>cbox (A n) (B n)" "y\<in>cbox (A n) (B n)"
  4.2275 +      have "dist x y \<le> setsum (\<lambda>i. \<bar>(x - y)\<bullet>i\<bar>) Basis"
  4.2276 +        unfolding dist_norm by(rule norm_le_l1)
  4.2277 +      also have "\<dots> \<le> setsum (\<lambda>i. B n\<bullet>i - A n\<bullet>i) Basis"
  4.2278 +      proof (rule setsum_mono)
  4.2279 +        fix i :: 'a
  4.2280 +        assume i: "i \<in> Basis"
  4.2281 +        show "\<bar>(x - y) \<bullet> i\<bar> \<le> B n \<bullet> i - A n \<bullet> i"
  4.2282 +          using xy[unfolded mem_box,THEN bspec, OF i]
  4.2283 +          by (auto simp: inner_diff_left)
  4.2284 +      qed
  4.2285 +      also have "\<dots> \<le> setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis / 2^n"
  4.2286 +        unfolding setsum_divide_distrib
  4.2287 +      proof (rule setsum_mono)
  4.2288 +        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
  4.2289 +        proof (induct n)
  4.2290 +          case 0
  4.2291 +          then show ?case
  4.2292 +            unfolding AB by auto
  4.2293 +        next
  4.2294 +          case (Suc n)
  4.2295 +          have "B (Suc n) \<bullet> i - A (Suc n) \<bullet> i \<le> (B n \<bullet> i - A n \<bullet> i) / 2"
  4.2296 +            using AB(4)[of i n] using i by auto
  4.2297 +          also have "\<dots> \<le> (b \<bullet> i - a \<bullet> i) / 2 ^ Suc n"
  4.2298 +            using Suc by (auto simp add: field_simps)
  4.2299 +          finally show ?case .
  4.2300 +        qed
  4.2301 +      qed
  4.2302 +      also have "\<dots> < e"
  4.2303 +        using n using e by (auto simp add: field_simps)
  4.2304 +      finally show "dist x y < e" .
  4.2305 +    qed
  4.2306 +  qed
  4.2307 +  {
  4.2308 +    fix n m :: nat
  4.2309 +    assume "m \<le> n" then have "cbox (A n) (B n) \<subseteq> cbox (A m) (B m)"
  4.2310 +    proof (induction rule: inc_induct)
  4.2311 +      case (step i)
  4.2312 +      show ?case
  4.2313 +        using AB(4) by (intro order_trans[OF step.IH] subset_box_imp) auto
  4.2314 +    qed simp
  4.2315 +  } note ABsubset = this
  4.2316 +  have "\<exists>a. \<forall>n. a\<in> cbox (A n) (B n)"
  4.2317 +    by (rule decreasing_closed_nest[rule_format,OF closed_cbox _ ABsubset interv])
  4.2318 +      (metis nat.exhaust AB(1-3) assms(1,3))
  4.2319 +  then obtain x0 where x0: "\<And>n. x0 \<in> cbox (A n) (B n)"
  4.2320 +    by blast
  4.2321 +  show thesis
  4.2322 +  proof (rule that[rule_format, of x0])
  4.2323 +    show "x0\<in>cbox a b"
  4.2324 +      using x0[of 0] unfolding AB .
  4.2325 +    fix e :: real
  4.2326 +    assume "e > 0"
  4.2327 +    from interv[OF this] obtain n
  4.2328 +      where n: "\<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e" ..
  4.2329 +    have "\<not> P (cbox (A n) (B n))"
  4.2330 +      apply (cases "0 < n")
  4.2331 +      using AB(3)[of "n - 1"] assms(3) AB(1-2)
  4.2332 +      apply auto
  4.2333 +      done
  4.2334 +    moreover have "cbox (A n) (B n) \<subseteq> ball x0 e"
  4.2335 +      using n using x0[of n] by auto
  4.2336 +    moreover have "cbox (A n) (B n) \<subseteq> cbox a b"
  4.2337 +      unfolding AB(1-2)[symmetric] by (rule ABsubset) auto
  4.2338 +    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)"
  4.2339 +      apply (rule_tac x="A n" in exI)
  4.2340 +      apply (rule_tac x="B n" in exI)
  4.2341 +      apply (auto simp: x0)
  4.2342 +      done
  4.2343 +  qed
  4.2344 +qed
  4.2345 +
  4.2346 +
  4.2347 +subsection \<open>Cousin's lemma.\<close>
  4.2348 +
  4.2349 +lemma fine_division_exists:
  4.2350 +  fixes a b :: "'a::euclidean_space"
  4.2351 +  assumes "gauge g"
  4.2352 +  obtains p where "p tagged_division_of (cbox a b)" "g fine p"
  4.2353 +proof -
  4.2354 +  presume "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p) \<Longrightarrow> False"
  4.2355 +  then obtain p where "p tagged_division_of (cbox a b)" "g fine p"
  4.2356 +    by blast
  4.2357 +  then show thesis ..
  4.2358 +next
  4.2359 +  assume as: "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p)"
  4.2360 +  obtain x where x:
  4.2361 +      "x \<in> (cbox a b)"
  4.2362 +      "\<And>e. 0 < e \<Longrightarrow>
  4.2363 +        \<exists>c d.
  4.2364 +          x \<in> cbox c d \<and>
  4.2365 +          cbox c d \<subseteq> ball x e \<and>
  4.2366 +          cbox c d \<subseteq> (cbox a b) \<and>
  4.2367 +          \<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  4.2368 +    apply (rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p", OF _ _ as])
  4.2369 +    apply (simp add: fine_def)
  4.2370 +    apply (metis tagged_division_union fine_union)
  4.2371 +    apply (auto simp: )
  4.2372 +    done
  4.2373 +  obtain e where e: "e > 0" "ball x e \<subseteq> g x"
  4.2374 +    using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
  4.2375 +  from x(2)[OF e(1)]
  4.2376 +  obtain c d where c_d: "x \<in> cbox c d"
  4.2377 +                        "cbox c d \<subseteq> ball x e"
  4.2378 +                        "cbox c d \<subseteq> cbox a b"
  4.2379 +                        "\<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  4.2380 +    by blast
  4.2381 +  have "g fine {(x, cbox c d)}"
  4.2382 +    unfolding fine_def using e using c_d(2) by auto
  4.2383 +  then show False
  4.2384 +    using tagged_division_of_self[OF c_d(1)] using c_d by auto
  4.2385 +qed
  4.2386 +
  4.2387 +lemma fine_division_exists_real:
  4.2388 +  fixes a b :: real
  4.2389 +  assumes "gauge g"
  4.2390 +  obtains p where "p tagged_division_of {a .. b}" "g fine p"
  4.2391 +  by (metis assms box_real(2) fine_division_exists)
  4.2392 +
  4.2393 +subsection \<open>A technical lemma about "refinement" of division.\<close>
  4.2394 +
  4.2395 +lemma tagged_division_finer:
  4.2396 +  fixes p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  4.2397 +  assumes "p tagged_division_of (cbox a b)"
  4.2398 +    and "gauge d"
  4.2399 +  obtains q where "q tagged_division_of (cbox a b)"
  4.2400 +    and "d fine q"
  4.2401 +    and "\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q"
  4.2402 +proof -
  4.2403 +  let ?P = "\<lambda>p. p tagged_partial_division_of (cbox a b) \<longrightarrow> gauge d \<longrightarrow>
  4.2404 +    (\<exists>q. q tagged_division_of (\<Union>{k. \<exists>x. (x,k) \<in> p}) \<and> d fine q \<and>
  4.2405 +      (\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q))"
  4.2406 +  {
  4.2407 +    have *: "finite p" "p tagged_partial_division_of (cbox a b)"
  4.2408 +      using assms(1)
  4.2409 +      unfolding tagged_division_of_def
  4.2410 +      by auto
  4.2411 +    presume "\<And>p. finite p \<Longrightarrow> ?P p"
  4.2412 +    from this[rule_format,OF * assms(2)] guess q .. note q=this
  4.2413 +    then show ?thesis
  4.2414 +      apply -
  4.2415 +      apply (rule that[of q])
  4.2416 +      unfolding tagged_division_ofD[OF assms(1)]
  4.2417 +      apply auto
  4.2418 +      done
  4.2419 +  }
  4.2420 +  fix p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  4.2421 +  assume as: "finite p"
  4.2422 +  show "?P p"
  4.2423 +    apply rule
  4.2424 +    apply rule
  4.2425 +    using as
  4.2426 +  proof (induct p)
  4.2427 +    case empty
  4.2428 +    show ?case
  4.2429 +      apply (rule_tac x="{}" in exI)
  4.2430 +      unfolding fine_def
  4.2431 +      apply auto
  4.2432 +      done
  4.2433 +  next
  4.2434 +    case (insert xk p)
  4.2435 +    guess x k using surj_pair[of xk] by (elim exE) note xk=this
  4.2436 +    note tagged_partial_division_subset[OF insert(4) subset_insertI]
  4.2437 +    from insert(3)[OF this insert(5)] guess q1 .. note q1 = conjunctD3[OF this]
  4.2438 +    have *: "\<Union>{l. \<exists>y. (y,l) \<in> insert xk p} = k \<union> \<Union>{l. \<exists>y. (y,l) \<in> p}"
  4.2439 +      unfolding xk by auto
  4.2440 +    note p = tagged_partial_division_ofD[OF insert(4)]
  4.2441 +    from p(4)[unfolded xk, OF insertI1] guess u v by (elim exE) note uv=this
  4.2442 +
  4.2443 +    have "finite {k. \<exists>x. (x, k) \<in> p}"
  4.2444 +      apply (rule finite_subset[of _ "snd ` p"])
  4.2445 +      using p
  4.2446 +      apply safe
  4.2447 +      apply (metis image_iff snd_conv)
  4.2448 +      apply auto
  4.2449 +      done
  4.2450 +    then have int: "interior (cbox u v) \<inter> interior (\<Union>{k. \<exists>x. (x, k) \<in> p}) = {}"
  4.2451 +      apply (rule inter_interior_unions_intervals)
  4.2452 +      apply (rule open_interior)
  4.2453 +      apply (rule_tac[!] ballI)
  4.2454 +      unfolding mem_Collect_eq
  4.2455 +      apply (erule_tac[!] exE)
  4.2456 +      apply (drule p(4)[OF insertI2])
  4.2457 +      apply assumption
  4.2458 +      apply (rule p(5))
  4.2459 +      unfolding uv xk
  4.2460 +      apply (rule insertI1)
  4.2461 +      apply (rule insertI2)
  4.2462 +      apply assumption
  4.2463 +      using insert(2)
  4.2464 +      unfolding uv xk
  4.2465 +      apply auto
  4.2466 +      done
  4.2467 +    show ?case
  4.2468 +    proof (cases "cbox u v \<subseteq> d x")
  4.2469 +      case True
  4.2470 +      then show ?thesis
  4.2471 +        apply (rule_tac x="{(x,cbox u v)} \<union> q1" in exI)
  4.2472 +        apply rule
  4.2473 +        unfolding * uv
  4.2474 +        apply (rule tagged_division_union)
  4.2475 +        apply (rule tagged_division_of_self)
  4.2476 +        apply (rule p[unfolded xk uv] insertI1)+
  4.2477 +        apply (rule q1)
  4.2478 +        apply (rule int)
  4.2479 +        apply rule
  4.2480 +        apply (rule fine_union)
  4.2481 +        apply (subst fine_def)
  4.2482 +        defer
  4.2483 +        apply (rule q1)
  4.2484 +        unfolding Ball_def split_paired_All split_conv
  4.2485 +        apply rule
  4.2486 +        apply rule
  4.2487 +        apply rule
  4.2488 +        apply rule
  4.2489 +        apply (erule insertE)
  4.2490 +        apply (simp add: uv xk)
  4.2491 +        apply (rule UnI2)
  4.2492 +        apply (drule q1(3)[rule_format])
  4.2493 +        unfolding xk uv
  4.2494 +        apply auto
  4.2495 +        done
  4.2496 +    next
  4.2497 +      case False
  4.2498 +      from fine_division_exists[OF assms(2), of u v] guess q2 . note q2=this
  4.2499 +      show ?thesis
  4.2500 +        apply (rule_tac x="q2 \<union> q1" in exI)
  4.2501 +        apply rule
  4.2502 +        unfolding * uv
  4.2503 +        apply (rule tagged_division_union q2 q1 int fine_union)+
  4.2504 +        unfolding Ball_def split_paired_All split_conv
  4.2505 +        apply rule
  4.2506 +        apply (rule fine_union)
  4.2507 +        apply (rule q1 q2)+
  4.2508 +        apply rule
  4.2509 +        apply rule
  4.2510 +        apply rule
  4.2511 +        apply rule
  4.2512 +        apply (erule insertE)
  4.2513 +        apply (rule UnI2)
  4.2514 +        apply (simp add: False uv xk)
  4.2515 +        apply (drule q1(3)[rule_format])
  4.2516 +        using False
  4.2517 +        unfolding xk uv
  4.2518 +        apply auto
  4.2519 +        done
  4.2520 +    qed
  4.2521 +  qed
  4.2522 +qed
  4.2523 +
  4.2524 +subsubsection \<open>Covering lemma\<close>
  4.2525 +
  4.2526 +text\<open> Some technical lemmas used in the approximation results that follow. Proof of the covering
  4.2527 +  lemma is an obvious multidimensional generalization of Lemma 3, p65 of Swartz's
  4.2528 +  "Introduction to Gauge Integrals". \<close>
  4.2529 +
  4.2530 +proposition covering_lemma:
  4.2531 +  assumes "S \<subseteq> cbox a b" "box a b \<noteq> {}" "gauge g"
  4.2532 +  obtains \<D> where
  4.2533 +    "countable \<D>"  "\<Union>\<D> \<subseteq> cbox a b"
  4.2534 +    "\<And>K. K \<in> \<D> \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  4.2535 +    "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
  4.2536 +    "\<And>K. K \<in> \<D> \<Longrightarrow> \<exists>x \<in> S \<inter> K. K \<subseteq> g x"
  4.2537 +    "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  4.2538 +    "S \<subseteq> \<Union>\<D>"
  4.2539 +proof -
  4.2540 +  have aibi: "\<And>i. i \<in> Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i" and normab: "0 < norm(b - a)"
  4.2541 +    using \<open>box a b \<noteq> {}\<close> box_eq_empty box_sing by fastforce+
  4.2542 +  let ?K0 = "\<lambda>(n, f::'a\<Rightarrow>nat).
  4.2543 +                    cbox (\<Sum>i \<in> Basis. (a \<bullet> i + (f i / 2^n) * (b \<bullet> i - a \<bullet> i)) *\<^sub>R i)
  4.2544 +                         (\<Sum>i \<in> Basis. (a \<bullet> i + ((f i + 1) / 2^n) * (b \<bullet> i - a \<bullet> i)) *\<^sub>R i)"
  4.2545 +  let ?D0 = "?K0 ` (SIGMA n:UNIV. PiE Basis (\<lambda>i::'a. lessThan (2^n)))"
  4.2546 +  obtain \<D>0 where count: "countable \<D>0"
  4.2547 +             and sub: "\<Union>\<D>0 \<subseteq> cbox a b"
  4.2548 +             and int:  "\<And>K. K \<in> \<D>0 \<Longrightarrow> (interior K \<noteq> {}) \<and> (\<exists>c d. K = cbox c d)"
  4.2549 +             and intdj: "\<And>A B. \<lbrakk>A \<in> \<D>0; B \<in> \<D>0\<rbrakk> \<Longrightarrow> A \<subseteq> B \<or> B \<subseteq> A \<or> interior A \<inter> interior B = {}"
  4.2550 +             and SK: "\<And>x. x \<in> S \<Longrightarrow> \<exists>K \<in> \<D>0. x \<in> K \<and> K \<subseteq> g x"
  4.2551 +             and cbox: "\<And>u v. cbox u v \<in> \<D>0 \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  4.2552 +             and fin: "\<And>K. K \<in> \<D>0 \<Longrightarrow> finite {L \<in> \<D>0. K \<subseteq> L}"
  4.2553 +  proof
  4.2554 +    show "countable ?D0"
  4.2555 +      by (simp add: countable_PiE)
  4.2556 +  next
  4.2557 +    show "\<Union>?D0 \<subseteq> cbox a b"
  4.2558 +      apply (simp add: UN_subset_iff)
  4.2559 +      apply (intro conjI allI ballI subset_box_imp)
  4.2560 +       apply (simp add: divide_simps zero_le_mult_iff aibi)
  4.2561 +      apply (force simp: aibi scaling_mono nat_less_real_le dest: PiE_mem)
  4.2562 +      done
  4.2563 +  next
  4.2564 +    show "\<And>K. K \<in> ?D0 \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  4.2565 +      using \<open>box a b \<noteq> {}\<close>
  4.2566 +      by (clarsimp simp: box_eq_empty) (fastforce simp add: divide_simps dest: PiE_mem)
  4.2567 +  next
  4.2568 +    have realff: "(real w) * 2^m < (real v) * 2^n \<longleftrightarrow> w * 2^m < v * 2^n" for m n v w
  4.2569 +      using of_nat_less_iff less_imp_of_nat_less by fastforce
  4.2570 +    have *: "\<forall>v w. ?K0(m,v) \<subseteq> ?K0(n,w) \<or> ?K0(n,w) \<subseteq> ?K0(m,v) \<or> interior(?K0(m,v)) \<inter> interior(?K0(n,w)) = {}"
  4.2571 +      for m n --\<open>The symmetry argument requires a single HOL formula\<close>
  4.2572 +    proof (rule linorder_wlog [where a=m and b=n], intro allI impI)
  4.2573 +      fix v w m and n::nat
  4.2574 +      assume "m \<le> n" --\<open>WLOG we can assume @{term"m \<le> n"}, when the first disjunct becomes impossible\<close>
  4.2575 +      have "?K0(n,w) \<subseteq> ?K0(m,v) \<or> interior(?K0(m,v)) \<inter> interior(?K0(n,w)) = {}"
  4.2576 +        apply (simp add: subset_box disjoint_interval)
  4.2577 +        apply (rule ccontr)
  4.2578 +        apply (clarsimp simp add: aibi mult_le_cancel_right divide_le_cancel not_less not_le)
  4.2579 +        apply (drule_tac x=i in bspec, assumption)
  4.2580 +        using \<open>m\<le>n\<close> realff [of _ _ "1+_"] realff [of "1+_"_ "1+_"]
  4.2581 +        apply (auto simp: divide_simps add.commute not_le nat_le_iff_add realff)
  4.2582 +        apply (simp add: power_add, metis (no_types, hide_lams) mult_Suc mult_less_cancel2 not_less_eq mult.assoc)+
  4.2583 +        done
  4.2584 +      then show "?K0(m,v) \<subseteq> ?K0(n,w) \<or> ?K0(n,w) \<subseteq> ?K0(m,v) \<or> interior(?K0(m,v)) \<inter> interior(?K0(n,w)) = {}"
  4.2585 +        by meson
  4.2586 +    qed auto
  4.2587 +    show "\<And>A B. \<lbrakk>A \<in> ?D0; B \<in> ?D0\<rbrakk> \<Longrightarrow> A \<subseteq> B \<or> B \<subseteq> A \<or> interior A \<inter> interior B = {}"
  4.2588 +      apply (erule imageE SigmaE)+
  4.2589 +      using * by simp
  4.2590 +  next
  4.2591 +    show "\<exists>K \<in> ?D0. x \<in> K \<and> K \<subseteq> g x" if "x \<in> S" for x
  4.2592 +    proof (simp only: bex_simps split_paired_Bex_Sigma)
  4.2593 +      show "\<exists>n. \<exists>f \<in> Basis \<rightarrow>\<^sub>E {..<2 ^ n}. x \<in> ?K0(n,f) \<and> ?K0(n,f) \<subseteq> g x"
  4.2594 +      proof -
  4.2595 +        obtain e where "0 < e"
  4.2596 +                   and e: "\<And>y. (\<And>i. i \<in> Basis \<Longrightarrow> \<bar>x \<bullet> i - y \<bullet> i\<bar> \<le> e) \<Longrightarrow> y \<in> g x"
  4.2597 +        proof -
  4.2598 +          have "x \<in> g x" "open (g x)"
  4.2599 +            using \<open>gauge g\<close> by (auto simp: gauge_def)
  4.2600 +          then obtain \<epsilon> where "0 < \<epsilon>" and \<epsilon>: "ball x \<epsilon> \<subseteq> g x"
  4.2601 +            using openE by blast
  4.2602 +          have "norm (x - y) < \<epsilon>"
  4.2603 +               if "(\<And>i. i \<in> Basis \<Longrightarrow> \<bar>x \<bullet> i - y \<bullet> i\<bar> \<le> \<epsilon> / (2 * real DIM('a)))" for y
  4.2604 +          proof -
  4.2605 +            have "norm (x - y) \<le> (\<Sum>i\<in>Basis. \<bar>x \<bullet> i - y \<bullet> i\<bar>)"
  4.2606 +              by (metis (no_types, lifting) inner_diff_left norm_le_l1 setsum.cong)
  4.2607 +            also have "... \<le> DIM('a) * (\<epsilon> / (2 * real DIM('a)))"
  4.2608 +              by (meson setsum_bounded_above that)
  4.2609 +            also have "... = \<epsilon> / 2"
  4.2610 +              by (simp add: divide_simps)
  4.2611 +            also have "... < \<epsilon>"
  4.2612 +              by (simp add: \<open>0 < \<epsilon>\<close>)
  4.2613 +            finally show ?thesis .
  4.2614 +          qed
  4.2615 +          then show ?thesis
  4.2616 +            by (rule_tac e = "\<epsilon> / 2 / DIM('a)" in that) (simp_all add:  \<open>0 < \<epsilon>\<close> dist_norm subsetD [OF \<epsilon>])
  4.2617 +        qed
  4.2618 +        have xab: "x \<in> cbox a b"
  4.2619 +          using \<open>x \<in> S\<close> \<open>S \<subseteq> cbox a b\<close> by blast
  4.2620 +        obtain n where n: "norm (b - a) / 2^n < e"
  4.2621 +          using real_arch_pow_inv [of "e / norm(b - a)" "1/2"] normab \<open>0 < e\<close>
  4.2622 +          by (auto simp: divide_simps)
  4.2623 +        then have "norm (b - a) < e * 2^n"
  4.2624 +          by (auto simp: divide_simps)
  4.2625 +        then have bai: "b \<bullet> i - a \<bullet> i < e * 2 ^ n" if "i \<in> Basis" for i
  4.2626 +        proof -
  4.2627 +          have "b \<bullet> i - a \<bullet> i \<le> norm (b - a)"
  4.2628 +            by (metis abs_of_nonneg dual_order.trans inner_diff_left linear norm_ge_zero Basis_le_norm that)
  4.2629 +          also have "... < e * 2 ^ n"
  4.2630 +            using \<open>norm (b - a) < e * 2 ^ n\<close> by blast
  4.2631 +          finally show ?thesis .
  4.2632 +        qed
  4.2633 +        have D: "(a + n \<le> x \<and> x \<le> a + m) \<Longrightarrow> (a + n \<le> y \<and> y \<le> a + m) \<Longrightarrow> abs(x - y) \<le> m - n"
  4.2634 +                 for a m n x and y::real
  4.2635 +          by auto
  4.2636 +        have "\<forall>i\<in>Basis. \<exists>k<2 ^ n. (a \<bullet> i + real k * (b \<bullet> i - a \<bullet> i) / 2 ^ n \<le> x \<bullet> i \<and>
  4.2637 +               x \<bullet> i \<le> a \<bullet> i + (real k + 1) * (b \<bullet> i - a \<bullet> i) / 2 ^ n)"
  4.2638 +        proof
  4.2639 +          fix i::'a assume "i \<in> Basis"
  4.2640 +          consider "x \<bullet> i = b \<bullet> i" | "x \<bullet> i < b \<bullet> i"
  4.2641 +            using \<open>i \<in> Basis\<close> mem_box(2) xab by force
  4.2642 +          then show "\<exists>k<2 ^ n. (a \<bullet> i + real k * (b \<bullet> i - a \<bullet> i) / 2 ^ n \<le> x \<bullet> i \<and>
  4.2643 +                          x \<bullet> i \<le> a \<bullet> i + (real k + 1) * (b \<bullet> i - a \<bullet> i) / 2 ^ n)"
  4.2644 +          proof cases
  4.2645 +            case 1 then show ?thesis
  4.2646 +              by (rule_tac x = "2^n - 1" in exI) (auto simp: algebra_simps divide_simps of_nat_diff \<open>i \<in> Basis\<close> aibi)
  4.2647 +          next
  4.2648 +            case 2
  4.2649 +            then have abi_less: "a \<bullet> i < b \<bullet> i"
  4.2650 +              using \<open>i \<in> Basis\<close> xab by (auto simp: mem_box)
  4.2651 +            let ?k = "nat \<lfloor>2 ^ n * (x \<bullet> i - a \<bullet> i) / (b \<bullet> i - a \<bullet> i)\<rfloor>"
  4.2652 +            show ?thesis
  4.2653 +            proof (intro exI conjI)
  4.2654 +              show "?k < 2 ^ n"
  4.2655 +                using aibi xab \<open>i \<in> Basis\<close>
  4.2656 +                by (force simp: nat_less_iff floor_less_iff divide_simps 2 mem_box)
  4.2657 +            next
  4.2658 +              have "a \<bullet> i + real ?k * (b \<bullet> i - a \<bullet> i) / 2 ^ n \<le>
  4.2659 +                    a \<bullet> i + (2 ^ n * (x \<bullet> i - a \<bullet> i) / (b \<bullet> i - a \<bullet> i)) * (b \<bullet> i - a \<bullet> i) / 2 ^ n"
  4.2660 +                apply (intro add_left_mono mult_right_mono divide_right_mono of_nat_floor)
  4.2661 +                using aibi [OF \<open>i \<in> Basis\<close>] xab 2
  4.2662 +                  apply (simp_all add: \<open>i \<in> Basis\<close> mem_box divide_simps)
  4.2663 +                done
  4.2664 +              also have "... = x \<bullet> i"
  4.2665 +                using abi_less by (simp add: divide_simps)
  4.2666 +              finally show "a \<bullet> i + real ?k * (b \<bullet> i - a \<bullet> i) / 2 ^ n \<le> x \<bullet> i" .
  4.2667 +            next
  4.2668 +              have "x \<bullet> i \<le> a \<bullet> i + (2 ^ n * (x \<bullet> i - a \<bullet> i) / (b \<bullet> i - a \<bullet> i)) * (b \<bullet> i - a \<bullet> i) / 2 ^ n"
  4.2669 +                using abi_less by (simp add: divide_simps algebra_simps)
  4.2670 +              also have "... \<le> a \<bullet> i + (real ?k + 1) * (b \<bullet> i - a \<bullet> i) / 2 ^ n"
  4.2671 +                apply (intro add_left_mono mult_right_mono divide_right_mono of_nat_floor)
  4.2672 +                using aibi [OF \<open>i \<in> Basis\<close>] xab
  4.2673 +                  apply (auto simp: \<open>i \<in> Basis\<close> mem_box divide_simps)
  4.2674 +                done
  4.2675 +              finally show "x \<bullet> i \<le> a \<bullet> i + (real ?k + 1) * (b \<bullet> i - a \<bullet> i) / 2 ^ n" .
  4.2676 +            qed
  4.2677 +          qed
  4.2678 +        qed
  4.2679 +        then have "\<exists>f\<in>Basis \<rightarrow>\<^sub>E {..<2 ^ n}. x \<in> ?K0(n,f)"
  4.2680 +          apply (simp add: mem_box Bex_def)
  4.2681 +          apply (clarify dest!: bchoice)
  4.2682 +          apply (rule_tac x="restrict f Basis" in exI, simp)
  4.2683 +          done
  4.2684 +        moreover have "\<And>f. x \<in> ?K0(n,f) \<Longrightarrow> ?K0(n,f) \<subseteq> g x"
  4.2685 +          apply (clarsimp simp add: mem_box)
  4.2686 +          apply (rule e)
  4.2687 +          apply (drule bspec D, assumption)+
  4.2688 +          apply (erule order_trans)
  4.2689 +          apply (simp add: divide_simps)
  4.2690 +          using bai by (force simp: algebra_simps)
  4.2691 +        ultimately show ?thesis by auto
  4.2692 +      qed
  4.2693 +    qed
  4.2694 +  next
  4.2695 +    show "\<And>u v. cbox u v \<in> ?D0 \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  4.2696 +      by (force simp: eq_cbox box_eq_empty field_simps dest!: aibi)
  4.2697 +  next
  4.2698 +    obtain j::'a where "j \<in> Basis"
  4.2699 +      using nonempty_Basis by blast
  4.2700 +    have "finite {L \<in> ?D0. ?K0(n,f) \<subseteq> L}" if "f \<in> Basis \<rightarrow>\<^sub>E {..<2 ^ n}" for n f
  4.2701 +    proof (rule finite_subset)
  4.2702 +      let ?B = "(\<lambda>(n, f::'a\<Rightarrow>nat). cbox (\<Sum>i\<in>Basis. (a \<bullet> i + (f i) / 2^n * (b \<bullet> i - a \<bullet> i)) *\<^sub>R i)
  4.2703 +                                        (\<Sum>i\<in>Basis. (a \<bullet> i + ((f i) + 1) / 2^n * (b \<bullet> i - a \<bullet> i)) *\<^sub>R i))
  4.2704 +                ` (SIGMA m:atMost n. PiE Basis (\<lambda>i::'a. lessThan (2^m)))"
  4.2705 +      have "?K0(m,g) \<in> ?B" if "g \<in> Basis \<rightarrow>\<^sub>E {..<2 ^ m}" "?K0(n,f) \<subseteq> ?K0(m,g)" for m g
  4.2706 +      proof -
  4.2707 +        have dd: "w / m \<le> v / n \<and> (v+1) / n \<le> (w+1) / m
  4.2708 +                  \<Longrightarrow> inverse n \<le> inverse m" for w m v n::real
  4.2709 +          by (auto simp: divide_simps algebra_simps)
  4.2710 +        have bjaj: "b \<bullet> j - a \<bullet> j > 0"
  4.2711 +          using \<open>j \<in> Basis\<close> \<open>box a b \<noteq> {}\<close> box_eq_empty(1) by fastforce
  4.2712 +        have "((g j) / 2 ^ m) * (b \<bullet> j - a \<bullet> j) \<le> ((f j) / 2 ^ n) * (b \<bullet> j - a \<bullet> j) \<and>
  4.2713 +              (((f j) + 1) / 2 ^ n) * (b \<bullet> j - a \<bullet> j) \<le> (((g j) + 1) / 2 ^ m) * (b \<bullet> j - a \<bullet> j)"
  4.2714 +          using that \<open>j \<in> Basis\<close> by (simp add: subset_box algebra_simps divide_simps aibi)
  4.2715 +        then have "((g j) / 2 ^ m) \<le> ((f j) / 2 ^ n) \<and>
  4.2716 +          ((real(f j) + 1) / 2 ^ n) \<le> ((real(g j) + 1) / 2 ^ m)"
  4.2717 +          by (metis bjaj mult.commute of_nat_1 of_nat_add real_mult_le_cancel_iff2)
  4.2718 +        then have "inverse (2^n) \<le> (inverse (2^m) :: real)"
  4.2719 +          by (rule dd)
  4.2720 +        then have "m \<le> n"
  4.2721 +          by auto
  4.2722 +        show ?thesis
  4.2723 +          by (rule imageI) (simp add: \<open>m \<le> n\<close> that)
  4.2724 +      qed
  4.2725 +      then show "{L \<in> ?D0. ?K0(n,f) \<subseteq> L} \<subseteq> ?B"
  4.2726 +        by auto
  4.2727 +      show "finite ?B"
  4.2728 +        by (intro finite_imageI finite_SigmaI finite_atMost finite_lessThan finite_PiE finite_Basis)
  4.2729 +    qed
  4.2730 +    then show "finite {L \<in> ?D0. K \<subseteq> L}" if "K \<in> ?D0" for K
  4.2731 +      using that by auto
  4.2732 +  qed
  4.2733 +  let ?D1 = "{K \<in> \<D>0. \<exists>x \<in> S \<inter> K. K \<subseteq> g x}"
  4.2734 +  obtain \<D> where count: "countable \<D>"
  4.2735 +             and sub: "\<Union>\<D> \<subseteq> cbox a b"  "S \<subseteq> \<Union>\<D>"
  4.2736 +             and int:  "\<And>K. K \<in> \<D> \<Longrightarrow> (interior K \<noteq> {}) \<and> (\<exists>c d. K = cbox c d)"
  4.2737 +             and intdj: "\<And>A B. \<lbrakk>A \<in> \<D>; B \<in> \<D>\<rbrakk> \<Longrightarrow> A \<subseteq> B \<or> B \<subseteq> A \<or> interior A \<inter> interior B = {}"
  4.2738 +             and SK: "\<And>K. K \<in> \<D> \<Longrightarrow> \<exists>x. x \<in> S \<inter> K \<and> K \<subseteq> g x"
  4.2739 +             and cbox: "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  4.2740 +             and fin: "\<And>K. K \<in> \<D> \<Longrightarrow> finite {L. L \<in> \<D> \<and> K \<subseteq> L}"
  4.2741 +  proof
  4.2742 +    show "countable ?D1" using count countable_subset
  4.2743 +      by (simp add: count countable_subset)
  4.2744 +    show "\<Union>?D1 \<subseteq> cbox a b"
  4.2745 +      using sub by blast
  4.2746 +    show "S \<subseteq> \<Union>?D1"
  4.2747 +      using SK by (force simp:)
  4.2748 +    show "\<And>K. K \<in> ?D1 \<Longrightarrow> (interior K \<noteq> {}) \<and> (\<exists>c d. K = cbox c d)"
  4.2749 +      using int by blast
  4.2750 +    show "\<And>A B. \<lbrakk>A \<in> ?D1; B \<in> ?D1\<rbrakk> \<Longrightarrow> A \<subseteq> B \<or> B \<subseteq> A \<or> interior A \<inter> interior B = {}"
  4.2751 +      using intdj by blast
  4.2752 +    show "\<And>K. K \<in> ?D1 \<Longrightarrow> \<exists>x. x \<in> S \<inter> K \<and> K \<subseteq> g x"
  4.2753 +      by auto
  4.2754 +    show "\<And>u v. cbox u v \<in> ?D1 \<Longrightarrow> \<exists>n. \<forall>i \<in> Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  4.2755 +      using cbox by blast
  4.2756 +    show "\<And>K. K \<in> ?D1 \<Longrightarrow> finite {L. L \<in> ?D1 \<and> K \<subseteq> L}"
  4.2757 +      using fin by simp (metis (mono_tags, lifting) Collect_mono rev_finite_subset)
  4.2758 +  qed
  4.2759 +  let ?\<D> = "{K \<in> \<D>. \<forall>K'. K' \<in> \<D> \<and> K \<noteq> K' \<longrightarrow> ~(K \<subseteq> K')}"
  4.2760 +  show ?thesis
  4.2761 +  proof (rule that)
  4.2762 +    show "countable ?\<D>"
  4.2763 +      by (blast intro: countable_subset [OF _ count])
  4.2764 +    show "\<Union>?\<D> \<subseteq> cbox a b"
  4.2765 +      using sub by blast
  4.2766 +    show "S \<subseteq> \<Union>?\<D>"
  4.2767 +    proof clarsimp
  4.2768 +      fix x
  4.2769 +      assume "x \<in> S"
  4.2770 +      then obtain X where "x \<in> X" "X \<in> \<D>" using \<open>S \<subseteq> \<Union>\<D>\<close> by blast
  4.2771 +      let ?R = "{(K,L). K \<in> \<D> \<and> L \<in> \<D> \<and> L \<subset> K}"
  4.2772 +      have irrR: "irrefl ?R" by (force simp: irrefl_def)
  4.2773 +      have traR: "trans ?R" by (force simp: trans_def)
  4.2774 +      have finR: "\<And>x. finite {y. (y, x) \<in> ?R}"
  4.2775 +        by simp (metis (mono_tags, lifting) fin \<open>X \<in> \<D>\<close> finite_subset mem_Collect_eq psubset_imp_subset subsetI)
  4.2776 +      have "{X \<in> \<D>. x \<in> X} \<noteq> {}"
  4.2777 +        using \<open>X \<in> \<D>\<close> \<open>x \<in> X\<close> by blast
  4.2778 +      then obtain Y where "Y \<in> {X \<in> \<D>. x \<in> X}" "\<And>Y'. (Y', Y) \<in> ?R \<Longrightarrow> Y' \<notin> {X \<in> \<D>. x \<in> X}"
  4.2779 +        by (rule wfE_min' [OF wf_finite_segments [OF irrR traR finR]]) blast
  4.2780 +      then show "\<exists>Y. Y \<in> \<D> \<and> (\<forall>K'. K' \<in> \<D> \<and> Y \<noteq> K' \<longrightarrow> \<not> Y \<subseteq> K') \<and> x \<in> Y"
  4.2781 +        by blast
  4.2782 +    qed
  4.2783 +    show "\<And>K. K \<in> ?\<D> \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>c d. K = cbox c d)"
  4.2784 +      by (blast intro: dest: int)
  4.2785 +    show "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) ?\<D>"
  4.2786 +      using intdj by (simp add: pairwise_def) metis
  4.2787 +    show "\<And>K. K \<in> ?\<D> \<Longrightarrow> \<exists>x \<in> S \<inter> K. K \<subseteq> g x"
  4.2788 +      using SK by force
  4.2789 +    show "\<And>u v. cbox u v \<in> ?\<D> \<Longrightarrow> \<exists>n. \<forall>i\<in>Basis. v \<bullet> i - u \<bullet> i = (b \<bullet> i - a \<bullet> i) / 2^n"
  4.2790 +      using cbox by force
  4.2791 +    qed
  4.2792 +qed
  4.2793 +
  4.2794 +subsection \<open>Division filter\<close>
  4.2795 +
  4.2796 +text \<open>Divisions over all gauges towards finer divisions.\<close>
  4.2797 +
  4.2798 +definition division_filter :: "'a::euclidean_space set \<Rightarrow> ('a \<times> 'a set) set filter"
  4.2799 +  where "division_filter s = (INF g:{g. gauge g}. principal {p. p tagged_division_of s \<and> g fine p})"
  4.2800 +
  4.2801 +lemma eventually_division_filter:
  4.2802 +  "(\<forall>\<^sub>F p in division_filter s. P p) \<longleftrightarrow>
  4.2803 +    (\<exists>g. gauge g \<and> (\<forall>p. p tagged_division_of s \<and> g fine p \<longrightarrow> P p))"
  4.2804 +  unfolding division_filter_def
  4.2805 +proof (subst eventually_INF_base; clarsimp)
  4.2806 +  fix g1 g2 :: "'a \<Rightarrow> 'a set" show "gauge g1 \<Longrightarrow> gauge g2 \<Longrightarrow> \<exists>x. gauge x \<and>
  4.2807 +    {p. p tagged_division_of s \<and> x fine p} \<subseteq> {p. p tagged_division_of s \<and> g1 fine p} \<and>
  4.2808 +    {p. p tagged_division_of s \<and> x fine p} \<subseteq> {p. p tagged_division_of s \<and> g2 fine p}"
  4.2809 +    by (intro exI[of _ "\<lambda>x. g1 x \<inter> g2 x"]) (auto simp: fine_inter)
  4.2810 +qed (auto simp: eventually_principal)
  4.2811 +
  4.2812 +lemma division_filter_not_empty: "division_filter (cbox a b) \<noteq> bot"
  4.2813 +  unfolding trivial_limit_def eventually_division_filter
  4.2814 +  by (auto elim: fine_division_exists)
  4.2815 +
  4.2816 +lemma eventually_division_filter_tagged_division:
  4.2817 +  "eventually (\<lambda>p. p tagged_division_of s) (division_filter s)"
  4.2818 +  unfolding eventually_division_filter by (intro exI[of _ "\<lambda>x. ball x 1"]) auto
  4.2819 +
  4.2820 +end
  4.2821 \ No newline at end of file
     5.1 --- a/src/HOL/Analysis/Topology_Euclidean_Space.thy	Wed Sep 28 16:15:51 2016 +0200
     5.2 +++ b/src/HOL/Analysis/Topology_Euclidean_Space.thy	Thu Sep 29 13:02:43 2016 +0200
     5.3 @@ -1414,7 +1414,7 @@
     5.4    then have "cbox a b \<subseteq> box c d" "box c d \<subseteq>cbox a b"
     5.5      by auto
     5.6    then show ?rhs
     5.7 -    apply (simp add: subset_box) 
     5.8 +    apply (simp add: subset_box)
     5.9      using \<open>cbox a b = box c d\<close> box_ne_empty box_sing
    5.10      apply (fastforce simp add:)
    5.11      done
    5.12 @@ -1435,7 +1435,7 @@
    5.13      by auto
    5.14    then show ?rhs
    5.15      apply (simp add: subset_box)
    5.16 -    using box_ne_empty(2) \<open>box a b = box c d\<close> 
    5.17 +    using box_ne_empty(2) \<open>box a b = box c d\<close>
    5.18      apply auto
    5.19       apply (meson euclidean_eqI less_eq_real_def not_less)+
    5.20      done
    5.21 @@ -10009,6 +10009,83 @@
    5.22      by (metis (no_types, lifting) IntD2 IntI f someI_ex)
    5.23  qed
    5.24  
    5.25 +lemma tube_lemma:
    5.26 +  assumes "compact K"
    5.27 +  assumes "open W"
    5.28 +  assumes "{x0} \<times> K \<subseteq> W"
    5.29 +  shows "\<exists>X0. x0 \<in> X0 \<and> open X0 \<and> X0 \<times> K \<subseteq> W"
    5.30 +proof -
    5.31 +  {
    5.32 +    fix y assume "y \<in> K"
    5.33 +    then have "(x0, y) \<in> W" using assms by auto
    5.34 +    with \<open>open W\<close>
    5.35 +    have "\<exists>X0 Y. open X0 \<and> open Y \<and> x0 \<in> X0 \<and> y \<in> Y \<and> X0 \<times> Y \<subseteq> W"
    5.36 +      by (rule open_prod_elim) blast
    5.37 +  }
    5.38 +  then obtain X0 Y where
    5.39 +    *: "\<forall>y \<in> K. open (X0 y) \<and> open (Y y) \<and> x0 \<in> X0 y \<and> y \<in> Y y \<and> X0 y \<times> Y y \<subseteq> W"
    5.40 +    by metis
    5.41 +  from * have "\<forall>t\<in>Y ` K. open t" "K \<subseteq> \<Union>(Y ` K)" by auto
    5.42 +  with \<open>compact K\<close> obtain CC where CC: "CC \<subseteq> Y ` K" "finite CC" "K \<subseteq> \<Union>CC"
    5.43 +    by (rule compactE)
    5.44 +  then obtain c where c: "\<And>C. C \<in> CC \<Longrightarrow> c C \<in> K \<and> C = Y (c C)"
    5.45 +    by (force intro!: choice)
    5.46 +  with * CC show ?thesis
    5.47 +    by (force intro!: exI[where x="\<Inter>C\<in>CC. X0 (c C)"]) (* SLOW *)
    5.48 +qed
    5.49 +
    5.50 +lemma continuous_on_prod_compactE:
    5.51 +  fixes fx::"'a::topological_space \<times> 'b::topological_space \<Rightarrow> 'c::metric_space"
    5.52 +    and e::real
    5.53 +  assumes cont_fx: "continuous_on (U \<times> C) fx"
    5.54 +  assumes "compact C"
    5.55 +  assumes [intro]: "x0 \<in> U"
    5.56 +  notes [continuous_intros] = continuous_on_compose2[OF cont_fx]
    5.57 +  assumes "e > 0"
    5.58 +  obtains X0 where "x0 \<in> X0" "open X0"
    5.59 +    "\<forall>x\<in>X0 \<inter> U. \<forall>t \<in> C. dist (fx (x, t)) (fx (x0, t)) \<le> e"
    5.60 +proof -
    5.61 +  define psi where "psi = (\<lambda>(x, t). dist (fx (x, t)) (fx (x0, t)))"
    5.62 +  define W0 where "W0 = {(x, t) \<in> U \<times> C. psi (x, t) < e}"
    5.63 +  have W0_eq: "W0 = psi -` {..<e} \<inter> U \<times> C"
    5.64 +    by (auto simp: vimage_def W0_def)
    5.65 +  have "open {..<e}" by simp
    5.66 +  have "continuous_on (U \<times> C) psi"
    5.67 +    by (auto intro!: continuous_intros simp: psi_def split_beta')
    5.68 +  from this[unfolded continuous_on_open_invariant, rule_format, OF \<open>open {..<e}\<close>]
    5.69 +  obtain W where W: "open W" "W \<inter> U \<times> C = W0 \<inter> U \<times> C"
    5.70 +    unfolding W0_eq by blast
    5.71 +  have "{x0} \<times> C \<subseteq> W \<inter> U \<times> C"
    5.72 +    unfolding W
    5.73 +    by (auto simp: W0_def psi_def \<open>0 < e\<close>)
    5.74 +  then have "{x0} \<times> C \<subseteq> W" by blast
    5.75 +  from tube_lemma[OF \<open>compact C\<close> \<open>open W\<close> this]
    5.76 +  obtain X0 where X0: "x0 \<in> X0" "open X0" "X0 \<times> C \<subseteq> W"
    5.77 +    by blast
    5.78 +
    5.79 +  have "\<forall>x\<in>X0 \<inter> U. \<forall>t \<in> C. dist (fx (x, t)) (fx (x0, t)) \<le> e"
    5.80 +  proof safe
    5.81 +    fix x assume x: "x \<in> X0" "x \<in> U"
    5.82 +    fix t assume t: "t \<in> C"
    5.83 +    have "dist (fx (x, t)) (fx (x0, t)) = psi (x, t)"
    5.84 +      by (auto simp: psi_def)
    5.85 +    also
    5.86 +    {
    5.87 +      have "(x, t) \<in> X0 \<times> C"
    5.88 +        using t x
    5.89 +        by auto
    5.90 +      also note \<open>\<dots> \<subseteq> W\<close>
    5.91 +      finally have "(x, t) \<in> W" .
    5.92 +      with t x have "(x, t) \<in> W \<inter> U \<times> C"
    5.93 +        by blast
    5.94 +      also note \<open>W \<inter> U \<times> C = W0 \<inter> U \<times> C\<close>
    5.95 +      finally  have "psi (x, t) < e"
    5.96 +        by (auto simp: W0_def)
    5.97 +    }
    5.98 +    finally show "dist (fx (x, t)) (fx (x0, t)) \<le> e" by simp
    5.99 +  qed
   5.100 +  from X0(1,2) this show ?thesis ..
   5.101 +qed
   5.102  
   5.103  no_notation
   5.104    eucl_less (infix "<e" 50)