src/HOL/Analysis/Henstock_Kurzweil_Integration.thy
author hoelzl
Mon Aug 08 14:13:14 2016 +0200 (2016-08-08)
changeset 63627 6ddb43c6b711
parent 63595 src/HOL/Multivariate_Analysis/Henstock_Kurzweil_Integration.thy@aca2659ebba7
child 63659 abe0c3872d8a
permissions -rw-r--r--
rename HOL-Multivariate_Analysis to HOL-Analysis.
     1 (*  Author:     John Harrison
     2     Author:     Robert Himmelmann, TU Muenchen (Translation from HOL light); proofs reworked by LCP
     3 *)
     4 
     5 section \<open>Henstock-Kurzweil gauge integration in many dimensions.\<close>
     6 
     7 theory Henstock_Kurzweil_Integration
     8 imports
     9   Derivative
    10   Uniform_Limit
    11   "~~/src/HOL/Library/Indicator_Function"
    12 begin
    13 
    14 lemmas scaleR_simps = scaleR_zero_left scaleR_minus_left scaleR_left_diff_distrib
    15   scaleR_zero_right scaleR_minus_right scaleR_right_diff_distrib scaleR_eq_0_iff
    16   scaleR_cancel_left scaleR_cancel_right scaleR_add_right scaleR_add_left real_vector_class.scaleR_one
    17 
    18 
    19 subsection \<open>Sundries\<close>
    20 
    21 lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
    22 lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
    23 lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
    24 
    25 declare norm_triangle_ineq4[intro]
    26 
    27 lemma simple_image: "{f x |x . x \<in> s} = f ` s"
    28   by blast
    29 
    30 lemma linear_simps:
    31   assumes "bounded_linear f"
    32   shows
    33     "f (a + b) = f a + f b"
    34     "f (a - b) = f a - f b"
    35     "f 0 = 0"
    36     "f (- a) = - f a"
    37     "f (s *\<^sub>R v) = s *\<^sub>R (f v)"
    38 proof -
    39   interpret f: bounded_linear f by fact
    40   show "f (a + b) = f a + f b" by (rule f.add)
    41   show "f (a - b) = f a - f b" by (rule f.diff)
    42   show "f 0 = 0" by (rule f.zero)
    43   show "f (- a) = - f a" by (rule f.minus)
    44   show "f (s *\<^sub>R v) = s *\<^sub>R (f v)" by (rule f.scaleR)
    45 qed
    46 
    47 lemma bounded_linearI:
    48   assumes "\<And>x y. f (x + y) = f x + f y"
    49     and "\<And>r x. f (r *\<^sub>R x) = r *\<^sub>R f x"
    50     and "\<And>x. norm (f x) \<le> norm x * K"
    51   shows "bounded_linear f"
    52   using assms by (rule bounded_linear_intro) (* FIXME: duplicate *)
    53 
    54 lemma bounded_linear_component [intro]: "bounded_linear (\<lambda>x::'a::euclidean_space. x \<bullet> k)"
    55   by (rule bounded_linear_inner_left)
    56 
    57 lemma transitive_stepwise_lt_eq:
    58   assumes "(\<And>x y z::nat. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z)"
    59   shows "((\<forall>m. \<forall>n>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n)))"
    60   (is "?l = ?r")
    61 proof safe
    62   assume ?r
    63   fix n m :: nat
    64   assume "m < n"
    65   then show "R m n"
    66   proof (induct n arbitrary: m)
    67     case 0
    68     then show ?case by auto
    69   next
    70     case (Suc n)
    71     show ?case
    72     proof (cases "m < n")
    73       case True
    74       show ?thesis
    75         apply (rule assms[OF Suc(1)[OF True]])
    76         using \<open>?r\<close>
    77         apply auto
    78         done
    79     next
    80       case False
    81       then have "m = n"
    82         using Suc(2) by auto
    83       then show ?thesis
    84         using \<open>?r\<close> by auto
    85     qed
    86   qed
    87 qed auto
    88 
    89 lemma transitive_stepwise_gt:
    90   assumes "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" "\<And>n. R n (Suc n)"
    91   shows "\<forall>n>m. R m n"
    92 proof -
    93   have "\<forall>m. \<forall>n>m. R m n"
    94     apply (subst transitive_stepwise_lt_eq)
    95     apply (blast intro: assms)+
    96     done
    97   then show ?thesis by auto
    98 qed
    99 
   100 lemma transitive_stepwise_le_eq:
   101   assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
   102   shows "(\<forall>m. \<forall>n\<ge>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n))"
   103   (is "?l = ?r")
   104 proof safe
   105   assume ?r
   106   fix m n :: nat
   107   assume "m \<le> n"
   108   then show "R m n"
   109   proof (induct n arbitrary: m)
   110     case 0
   111     with assms show ?case by auto
   112   next
   113     case (Suc n)
   114     show ?case
   115     proof (cases "m \<le> n")
   116       case True
   117       with Suc.hyps \<open>\<forall>n. R n (Suc n)\<close> assms show ?thesis
   118         by blast
   119     next
   120       case False
   121       then have "m = Suc n"
   122         using Suc(2) by auto
   123       then show ?thesis
   124         using assms(1) by auto
   125     qed
   126   qed
   127 qed auto
   128 
   129 lemma transitive_stepwise_le:
   130   assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
   131     and "\<And>n. R n (Suc n)"
   132   shows "\<forall>n\<ge>m. R m n"
   133 proof -
   134   have "\<forall>m. \<forall>n\<ge>m. R m n"
   135     apply (subst transitive_stepwise_le_eq)
   136     apply (blast intro: assms)+
   137     done
   138   then show ?thesis by auto
   139 qed
   140 
   141 
   142 subsection \<open>Some useful lemmas about intervals.\<close>
   143 
   144 lemma empty_as_interval: "{} = cbox One (0::'a::euclidean_space)"
   145   using nonempty_Basis
   146   by (fastforce simp add: set_eq_iff mem_box)
   147 
   148 lemma interior_subset_union_intervals:
   149   assumes "i = cbox a b"
   150     and "j = cbox c d"
   151     and "interior j \<noteq> {}"
   152     and "i \<subseteq> j \<union> s"
   153     and "interior i \<inter> interior j = {}"
   154   shows "interior i \<subseteq> interior s"
   155 proof -
   156   have "box a b \<inter> cbox c d = {}"
   157      using inter_interval_mixed_eq_empty[of c d a b] and assms(3,5)
   158      unfolding assms(1,2) interior_cbox by auto
   159   moreover
   160   have "box a b \<subseteq> cbox c d \<union> s"
   161     apply (rule order_trans,rule box_subset_cbox)
   162     using assms(4) unfolding assms(1,2)
   163     apply auto
   164     done
   165   ultimately
   166   show ?thesis
   167     unfolding assms interior_cbox
   168       by auto (metis IntI UnE empty_iff interior_maximal open_box subsetCE subsetI)
   169 qed
   170 
   171 lemma interior_Union_subset_cbox:
   172   assumes "finite f"
   173   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"
   174     and t: "closed t"
   175   shows "interior (\<Union>f) \<subseteq> t"
   176 proof -
   177   have [simp]: "s \<in> f \<Longrightarrow> closed s" for s
   178     using f by auto
   179   define E where "E = {s\<in>f. interior s = {}}"
   180   then have "finite E" "E \<subseteq> {s\<in>f. interior s = {}}"
   181     using \<open>finite f\<close> by auto
   182   then have "interior (\<Union>f) = interior (\<Union>(f - E))"
   183   proof (induction E rule: finite_subset_induct')
   184     case (insert s f')
   185     have "interior (\<Union>(f - insert s f') \<union> s) = interior (\<Union>(f - insert s f'))"
   186       using insert.hyps \<open>finite f\<close> by (intro interior_closed_Un_empty_interior) auto
   187     also have "\<Union>(f - insert s f') \<union> s = \<Union>(f - f')"
   188       using insert.hyps by auto
   189     finally show ?case
   190       by (simp add: insert.IH)
   191   qed simp
   192   also have "\<dots> \<subseteq> \<Union>(f - E)"
   193     by (rule interior_subset)
   194   also have "\<dots> \<subseteq> t"
   195   proof (rule Union_least)
   196     fix s assume "s \<in> f - E"
   197     with f[of s] obtain a b where s: "s \<in> f" "s = cbox a b" "box a b \<noteq> {}"
   198       by (fastforce simp: E_def)
   199     have "closure (interior s) \<subseteq> closure t"
   200       by (intro closure_mono f \<open>s \<in> f\<close>)
   201     with s \<open>closed t\<close> show "s \<subseteq> t"
   202       by (simp add: closure_box)
   203   qed
   204   finally show ?thesis .
   205 qed
   206 
   207 lemma inter_interior_unions_intervals:
   208     "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) = {}"
   209   using interior_Union_subset_cbox[of f "UNIV - s"] by auto
   210 
   211 subsection \<open>Bounds on intervals where they exist.\<close>
   212 
   213 definition interval_upperbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
   214   where "interval_upperbound s = (\<Sum>i\<in>Basis. (SUP x:s. x\<bullet>i) *\<^sub>R i)"
   215 
   216 definition interval_lowerbound :: "('a::euclidean_space) set \<Rightarrow> 'a"
   217    where "interval_lowerbound s = (\<Sum>i\<in>Basis. (INF x:s. x\<bullet>i) *\<^sub>R i)"
   218 
   219 lemma interval_upperbound[simp]:
   220   "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
   221     interval_upperbound (cbox a b) = (b::'a::euclidean_space)"
   222   unfolding interval_upperbound_def euclidean_representation_setsum cbox_def
   223   by (safe intro!: cSup_eq) auto
   224 
   225 lemma interval_lowerbound[simp]:
   226   "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow>
   227     interval_lowerbound (cbox a b) = (a::'a::euclidean_space)"
   228   unfolding interval_lowerbound_def euclidean_representation_setsum cbox_def
   229   by (safe intro!: cInf_eq) auto
   230 
   231 lemmas interval_bounds = interval_upperbound interval_lowerbound
   232 
   233 lemma
   234   fixes X::"real set"
   235   shows interval_upperbound_real[simp]: "interval_upperbound X = Sup X"
   236     and interval_lowerbound_real[simp]: "interval_lowerbound X = Inf X"
   237   by (auto simp: interval_upperbound_def interval_lowerbound_def)
   238 
   239 lemma interval_bounds'[simp]:
   240   assumes "cbox a b \<noteq> {}"
   241   shows "interval_upperbound (cbox a b) = b"
   242     and "interval_lowerbound (cbox a b) = a"
   243   using assms unfolding box_ne_empty by auto
   244 
   245 
   246 lemma interval_upperbound_Times:
   247   assumes "A \<noteq> {}" and "B \<noteq> {}"
   248   shows "interval_upperbound (A \<times> B) = (interval_upperbound A, interval_upperbound B)"
   249 proof-
   250   from assms have fst_image_times': "A = fst ` (A \<times> B)" by simp
   251   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)"
   252       by (subst (2) fst_image_times') (simp del: fst_image_times add: o_def inner_Pair_0)
   253   moreover from assms have snd_image_times': "B = snd ` (A \<times> B)" by simp
   254   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)"
   255       by (subst (2) snd_image_times') (simp del: snd_image_times add: o_def inner_Pair_0)
   256   ultimately show ?thesis unfolding interval_upperbound_def
   257       by (subst setsum_Basis_prod_eq) (auto simp add: setsum_prod)
   258 qed
   259 
   260 lemma interval_lowerbound_Times:
   261   assumes "A \<noteq> {}" and "B \<noteq> {}"
   262   shows "interval_lowerbound (A \<times> B) = (interval_lowerbound A, interval_lowerbound B)"
   263 proof-
   264   from assms have fst_image_times': "A = fst ` (A \<times> B)" by simp
   265   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)"
   266       by (subst (2) fst_image_times') (simp del: fst_image_times add: o_def inner_Pair_0)
   267   moreover from assms have snd_image_times': "B = snd ` (A \<times> B)" by simp
   268   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)"
   269       by (subst (2) snd_image_times') (simp del: snd_image_times add: o_def inner_Pair_0)
   270   ultimately show ?thesis unfolding interval_lowerbound_def
   271       by (subst setsum_Basis_prod_eq) (auto simp add: setsum_prod)
   272 qed
   273 
   274 subsection \<open>Content (length, area, volume...) of an interval.\<close>
   275 
   276 definition "content (s::('a::euclidean_space) set) =
   277   (if s = {} then 0 else (\<Prod>i\<in>Basis. (interval_upperbound s)\<bullet>i - (interval_lowerbound s)\<bullet>i))"
   278 
   279 lemma interval_not_empty: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i \<Longrightarrow> cbox a b \<noteq> {}"
   280   unfolding box_eq_empty unfolding not_ex not_less by auto
   281 
   282 lemma content_cbox:
   283   fixes a :: "'a::euclidean_space"
   284   assumes "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
   285   shows "content (cbox a b) = (\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
   286   using interval_not_empty[OF assms]
   287   unfolding content_def
   288   by auto
   289 
   290 lemma content_cbox':
   291   fixes a :: "'a::euclidean_space"
   292   assumes "cbox a b \<noteq> {}"
   293   shows "content (cbox a b) = (\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i)"
   294     using assms box_ne_empty(1) content_cbox by blast
   295 
   296 lemma content_real: "a \<le> b \<Longrightarrow> content {a..b} = b - a"
   297   by (auto simp: interval_upperbound_def interval_lowerbound_def content_def)
   298 
   299 lemma abs_eq_content: "\<bar>y - x\<bar> = (if x\<le>y then content {x .. y} else content {y..x})"
   300   by (auto simp: content_real)
   301 
   302 lemma content_singleton[simp]: "content {a} = 0"
   303 proof -
   304   have "content (cbox a a) = 0"
   305     by (subst content_cbox) (auto simp: ex_in_conv)
   306   then show ?thesis by (simp add: cbox_sing)
   307 qed
   308 
   309 lemma content_unit[iff]: "content(cbox 0 (One::'a::euclidean_space)) = 1"
   310  proof -
   311    have *: "\<forall>i\<in>Basis. (0::'a)\<bullet>i \<le> (One::'a)\<bullet>i"
   312     by auto
   313   have "0 \<in> cbox 0 (One::'a)"
   314     unfolding mem_box by auto
   315   then show ?thesis
   316      unfolding content_def interval_bounds[OF *] using setprod.neutral_const by auto
   317  qed
   318 
   319 lemma content_pos_le[intro]:
   320   fixes a::"'a::euclidean_space"
   321   shows "0 \<le> content (cbox a b)"
   322 proof (cases "cbox a b = {}")
   323   case False
   324   then have *: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
   325     unfolding box_ne_empty .
   326   have "0 \<le> (\<Prod>i\<in>Basis. interval_upperbound (cbox a b) \<bullet> i - interval_lowerbound (cbox a b) \<bullet> i)"
   327     apply (rule setprod_nonneg)
   328     unfolding interval_bounds[OF *]
   329     using *
   330     apply auto
   331     done
   332   also have "\<dots> = content (cbox a b)" using False by (simp add: content_def)
   333   finally show ?thesis .
   334 qed (simp add: content_def)
   335 
   336 corollary content_nonneg [simp]:
   337   fixes a::"'a::euclidean_space"
   338   shows "~ content (cbox a b) < 0"
   339 using not_le by blast
   340 
   341 lemma content_pos_lt:
   342   fixes a :: "'a::euclidean_space"
   343   assumes "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"
   344   shows "0 < content (cbox a b)"
   345   using assms
   346   by (auto simp: content_def box_eq_empty intro!: setprod_pos)
   347 
   348 lemma content_eq_0:
   349   "content (cbox a b) = 0 \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i \<le> a\<bullet>i)"
   350   by (auto simp: content_def box_eq_empty intro!: setprod_pos bexI)
   351 
   352 lemma cond_cases: "(P \<Longrightarrow> Q x) \<Longrightarrow> (\<not> P \<Longrightarrow> Q y) \<Longrightarrow> Q (if P then x else y)"
   353   by auto
   354 
   355 lemma content_cbox_cases:
   356   "content (cbox a (b::'a::euclidean_space)) =
   357     (if \<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i then setprod (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis else 0)"
   358   by (auto simp: not_le content_eq_0 intro: less_imp_le content_cbox)
   359 
   360 lemma content_eq_0_interior: "content (cbox a b) = 0 \<longleftrightarrow> interior(cbox a b) = {}"
   361   unfolding content_eq_0 interior_cbox box_eq_empty
   362   by auto
   363 
   364 lemma content_pos_lt_eq:
   365   "0 < content (cbox a (b::'a::euclidean_space)) \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i)"
   366 proof (rule iffI)
   367   assume "0 < content (cbox a b)"
   368   then have "content (cbox a b) \<noteq> 0" by auto
   369   then show "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"
   370     unfolding content_eq_0 not_ex not_le by fastforce
   371 next
   372   assume "\<forall>i\<in>Basis. a \<bullet> i < b \<bullet> i"
   373   then show "0 < content (cbox a b)"
   374     by (metis content_pos_lt)
   375 qed
   376 
   377 lemma content_empty [simp]: "content {} = 0"
   378   unfolding content_def by auto
   379 
   380 lemma content_real_if [simp]: "content {a..b} = (if a \<le> b then b - a else 0)"
   381   by (simp add: content_real)
   382 
   383 lemma content_subset:
   384   assumes "cbox a b \<subseteq> cbox c d"
   385   shows "content (cbox a b) \<le> content (cbox c d)"
   386 proof (cases "cbox a b = {}")
   387   case True
   388   then show ?thesis
   389     using content_pos_le[of c d] by auto
   390 next
   391   case False
   392   then have ab_ne: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
   393     unfolding box_ne_empty by auto
   394   then have ab_ab: "a\<in>cbox a b" "b\<in>cbox a b"
   395     unfolding mem_box by auto
   396   have "cbox c d \<noteq> {}" using assms False by auto
   397   then have cd_ne: "\<forall>i\<in>Basis. c \<bullet> i \<le> d \<bullet> i"
   398     using assms unfolding box_ne_empty by auto
   399   have "\<And>i. i \<in> Basis \<Longrightarrow> 0 \<le> b \<bullet> i - a \<bullet> i"
   400     using ab_ne by auto
   401   moreover
   402   have "\<And>i. i \<in> Basis \<Longrightarrow> b \<bullet> i - a \<bullet> i \<le> d \<bullet> i - c \<bullet> i"
   403     using assms[unfolded subset_eq mem_box,rule_format,OF ab_ab(2)]
   404           assms[unfolded subset_eq mem_box,rule_format,OF ab_ab(1)]
   405       by (metis diff_mono)
   406   ultimately show ?thesis
   407     unfolding content_def interval_bounds[OF ab_ne] interval_bounds[OF cd_ne]
   408     by (simp add: setprod_mono if_not_P[OF False] if_not_P[OF \<open>cbox c d \<noteq> {}\<close>])
   409 qed
   410 
   411 lemma content_lt_nz: "0 < content (cbox a b) \<longleftrightarrow> content (cbox a b) \<noteq> 0"
   412   unfolding content_pos_lt_eq content_eq_0 unfolding not_ex not_le by fastforce
   413 
   414 lemma content_times[simp]: "content (A \<times> B) = content A * content B"
   415 proof (cases "A \<times> B = {}")
   416   let ?ub1 = "interval_upperbound" and ?lb1 = "interval_lowerbound"
   417   let ?ub2 = "interval_upperbound" and ?lb2 = "interval_lowerbound"
   418   assume nonempty: "A \<times> B \<noteq> {}"
   419   hence "content (A \<times> B) = (\<Prod>i\<in>Basis. (?ub1 A, ?ub2 B) \<bullet> i - (?lb1 A, ?lb2 B) \<bullet> i)"
   420       unfolding content_def by (simp add: interval_upperbound_Times interval_lowerbound_Times)
   421   also have "... = content A * content B" unfolding content_def using nonempty
   422     apply (subst Basis_prod_def, subst setprod.union_disjoint, force, force, force, simp)
   423     apply (subst (1 2) setprod.reindex, auto intro: inj_onI)
   424     done
   425   finally show ?thesis .
   426 qed (auto simp: content_def)
   427 
   428 lemma content_Pair: "content (cbox (a,c) (b,d)) = content (cbox a b) * content (cbox c d)"
   429   by (simp add: cbox_Pair_eq)
   430 
   431 lemma content_cbox_pair_eq0_D:
   432    "content (cbox (a,c) (b,d)) = 0 \<Longrightarrow> content (cbox a b) = 0 \<or> content (cbox c d) = 0"
   433   by (simp add: content_Pair)
   434 
   435 lemma content_eq_0_gen:
   436   fixes s :: "'a::euclidean_space set"
   437   assumes "bounded s"
   438   shows "content s = 0 \<longleftrightarrow> (\<exists>i\<in>Basis. \<exists>v. \<forall>x \<in> s. x \<bullet> i = v)"  (is "_ = ?rhs")
   439 proof safe
   440   assume "content s = 0" then show ?rhs
   441     apply (clarsimp simp: ex_in_conv content_def split: if_split_asm)
   442     apply (rule_tac x=a in bexI)
   443     apply (rule_tac x="interval_lowerbound s \<bullet> a" in exI)
   444     apply (clarsimp simp: interval_upperbound_def interval_lowerbound_def)
   445     apply (drule cSUP_eq_cINF_D)
   446     apply (auto simp: bounded_inner_imp_bdd_above [OF assms]  bounded_inner_imp_bdd_below [OF assms])
   447     done
   448 next
   449   fix i a
   450   assume "i \<in> Basis" "\<forall>x\<in>s. x \<bullet> i = a"
   451   then show "content s = 0"
   452     apply (clarsimp simp: content_def)
   453     apply (rule_tac x=i in bexI)
   454     apply (auto simp: interval_upperbound_def interval_lowerbound_def)
   455     done
   456 qed
   457 
   458 lemma content_0_subset_gen:
   459   fixes a :: "'a::euclidean_space"
   460   assumes "content t = 0" "s \<subseteq> t" "bounded t" shows "content s = 0"
   461 proof -
   462   have "bounded s"
   463     using assms by (metis bounded_subset)
   464   then show ?thesis
   465     using assms
   466     by (auto simp: content_eq_0_gen)
   467 qed
   468 
   469 lemma content_0_subset: "\<lbrakk>content(cbox a b) = 0; s \<subseteq> cbox a b\<rbrakk> \<Longrightarrow> content s = 0"
   470   by (simp add: content_0_subset_gen bounded_cbox)
   471 
   472 
   473 lemma interval_split:
   474   fixes a :: "'a::euclidean_space"
   475   assumes "k \<in> Basis"
   476   shows
   477     "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)"
   478     "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"
   479   apply (rule_tac[!] set_eqI)
   480   unfolding Int_iff mem_box mem_Collect_eq
   481   using assms
   482   apply auto
   483   done
   484 
   485 lemma content_split:
   486   fixes a :: "'a::euclidean_space"
   487   assumes "k \<in> Basis"
   488   shows "content (cbox a b) = content(cbox a b \<inter> {x. x\<bullet>k \<le> c}) + content(cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
   489 proof cases
   490   note simps = interval_split[OF assms] content_cbox_cases
   491   have *: "Basis = insert k (Basis - {k})" "\<And>x. finite (Basis-{x})" "\<And>x. x\<notin>Basis-{x}"
   492     using assms by auto
   493   have *: "\<And>X Y Z. (\<Prod>i\<in>Basis. Z i (if i = k then X else Y i)) = Z k X * (\<Prod>i\<in>Basis-{k}. Z i (Y i))"
   494     "(\<Prod>i\<in>Basis. b\<bullet>i - a\<bullet>i) = (\<Prod>i\<in>Basis-{k}. b\<bullet>i - a\<bullet>i) * (b\<bullet>k - a\<bullet>k)"
   495     apply (subst *(1))
   496     defer
   497     apply (subst *(1))
   498     unfolding setprod.insert[OF *(2-)]
   499     apply auto
   500     done
   501   assume as: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
   502   moreover
   503   have "\<And>x. min (b \<bullet> k) c = max (a \<bullet> k) c \<Longrightarrow>
   504     x * (b\<bullet>k - a\<bullet>k) = x * (max (a \<bullet> k) c - a \<bullet> k) + x * (b \<bullet> k - max (a \<bullet> k) c)"
   505     by  (auto simp add: field_simps)
   506   moreover
   507   have **: "(\<Prod>i\<in>Basis. ((\<Sum>i\<in>Basis. (if i = k then min (b \<bullet> k) c else b \<bullet> i) *\<^sub>R i) \<bullet> i - a \<bullet> i)) =
   508       (\<Prod>i\<in>Basis. (if i = k then min (b \<bullet> k) c else b \<bullet> i) - a \<bullet> i)"
   509     "(\<Prod>i\<in>Basis. b \<bullet> i - ((\<Sum>i\<in>Basis. (if i = k then max (a \<bullet> k) c else a \<bullet> i) *\<^sub>R i) \<bullet> i)) =
   510       (\<Prod>i\<in>Basis. b \<bullet> i - (if i = k then max (a \<bullet> k) c else a \<bullet> i))"
   511     by (auto intro!: setprod.cong)
   512   have "\<not> a \<bullet> k \<le> c \<Longrightarrow> \<not> c \<le> b \<bullet> k \<Longrightarrow> False"
   513     unfolding not_le
   514     using as[unfolded ,rule_format,of k] assms
   515     by auto
   516   ultimately show ?thesis
   517     using assms
   518     unfolding simps **
   519     unfolding *(1)[of "\<lambda>i x. b\<bullet>i - x"] *(1)[of "\<lambda>i x. x - a\<bullet>i"]
   520     unfolding *(2)
   521     by auto
   522 next
   523   assume "\<not> (\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)"
   524   then have "cbox a b = {}"
   525     unfolding box_eq_empty by (auto simp: not_le)
   526   then show ?thesis
   527     by (auto simp: not_le)
   528 qed
   529 
   530 subsection \<open>The notion of a gauge --- simply an open set containing the point.\<close>
   531 
   532 definition "gauge d \<longleftrightarrow> (\<forall>x. x \<in> d x \<and> open (d x))"
   533 
   534 lemma gaugeI:
   535   assumes "\<And>x. x \<in> g x"
   536     and "\<And>x. open (g x)"
   537   shows "gauge g"
   538   using assms unfolding gauge_def by auto
   539 
   540 lemma gaugeD[dest]:
   541   assumes "gauge d"
   542   shows "x \<in> d x"
   543     and "open (d x)"
   544   using assms unfolding gauge_def by auto
   545 
   546 lemma gauge_ball_dependent: "\<forall>x. 0 < e x \<Longrightarrow> gauge (\<lambda>x. ball x (e x))"
   547   unfolding gauge_def by auto
   548 
   549 lemma gauge_ball[intro]: "0 < e \<Longrightarrow> gauge (\<lambda>x. ball x e)"
   550   unfolding gauge_def by auto
   551 
   552 lemma gauge_trivial[intro!]: "gauge (\<lambda>x. ball x 1)"
   553   by (rule gauge_ball) auto
   554 
   555 lemma gauge_inter[intro]: "gauge d1 \<Longrightarrow> gauge d2 \<Longrightarrow> gauge (\<lambda>x. d1 x \<inter> d2 x)"
   556   unfolding gauge_def by auto
   557 
   558 lemma gauge_inters:
   559   assumes "finite s"
   560     and "\<forall>d\<in>s. gauge (f d)"
   561   shows "gauge (\<lambda>x. \<Inter>{f d x | d. d \<in> s})"
   562 proof -
   563   have *: "\<And>x. {f d x |d. d \<in> s} = (\<lambda>d. f d x) ` s"
   564     by auto
   565   show ?thesis
   566     unfolding gauge_def unfolding *
   567     using assms unfolding Ball_def Inter_iff mem_Collect_eq gauge_def by auto
   568 qed
   569 
   570 lemma gauge_existence_lemma:
   571   "(\<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)"
   572   by (metis zero_less_one)
   573 
   574 
   575 subsection \<open>Divisions.\<close>
   576 
   577 definition division_of (infixl "division'_of" 40)
   578 where
   579   "s division_of i \<longleftrightarrow>
   580     finite s \<and>
   581     (\<forall>k\<in>s. k \<subseteq> i \<and> k \<noteq> {} \<and> (\<exists>a b. k = cbox a b)) \<and>
   582     (\<forall>k1\<in>s. \<forall>k2\<in>s. k1 \<noteq> k2 \<longrightarrow> interior(k1) \<inter> interior(k2) = {}) \<and>
   583     (\<Union>s = i)"
   584 
   585 lemma division_ofD[dest]:
   586   assumes "s division_of i"
   587   shows "finite s"
   588     and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
   589     and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
   590     and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
   591     and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}"
   592     and "\<Union>s = i"
   593   using assms unfolding division_of_def by auto
   594 
   595 lemma division_ofI:
   596   assumes "finite s"
   597     and "\<And>k. k \<in> s \<Longrightarrow> k \<subseteq> i"
   598     and "\<And>k. k \<in> s \<Longrightarrow> k \<noteq> {}"
   599     and "\<And>k. k \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
   600     and "\<And>k1 k2. k1 \<in> s \<Longrightarrow> k2 \<in> s \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
   601     and "\<Union>s = i"
   602   shows "s division_of i"
   603   using assms unfolding division_of_def by auto
   604 
   605 lemma division_of_finite: "s division_of i \<Longrightarrow> finite s"
   606   unfolding division_of_def by auto
   607 
   608 lemma division_of_self[intro]: "cbox a b \<noteq> {} \<Longrightarrow> {cbox a b} division_of (cbox a b)"
   609   unfolding division_of_def by auto
   610 
   611 lemma division_of_trivial[simp]: "s division_of {} \<longleftrightarrow> s = {}"
   612   unfolding division_of_def by auto
   613 
   614 lemma division_of_sing[simp]:
   615   "s division_of cbox a (a::'a::euclidean_space) \<longleftrightarrow> s = {cbox a a}"
   616   (is "?l = ?r")
   617 proof
   618   assume ?r
   619   moreover
   620   { fix k
   621     assume "s = {{a}}" "k\<in>s"
   622     then have "\<exists>x y. k = cbox x y"
   623       apply (rule_tac x=a in exI)+
   624       apply (force simp: cbox_sing)
   625       done
   626   }
   627   ultimately show ?l
   628     unfolding division_of_def cbox_sing by auto
   629 next
   630   assume ?l
   631   note * = conjunctD4[OF this[unfolded division_of_def cbox_sing]]
   632   {
   633     fix x
   634     assume x: "x \<in> s" have "x = {a}"
   635       using *(2)[rule_format,OF x] by auto
   636   }
   637   moreover have "s \<noteq> {}"
   638     using *(4) by auto
   639   ultimately show ?r
   640     unfolding cbox_sing by auto
   641 qed
   642 
   643 lemma elementary_empty: obtains p where "p division_of {}"
   644   unfolding division_of_trivial by auto
   645 
   646 lemma elementary_interval: obtains p where "p division_of (cbox a b)"
   647   by (metis division_of_trivial division_of_self)
   648 
   649 lemma division_contains: "s division_of i \<Longrightarrow> \<forall>x\<in>i. \<exists>k\<in>s. x \<in> k"
   650   unfolding division_of_def by auto
   651 
   652 lemma forall_in_division:
   653   "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))"
   654   unfolding division_of_def by fastforce
   655 
   656 lemma division_of_subset:
   657   assumes "p division_of (\<Union>p)"
   658     and "q \<subseteq> p"
   659   shows "q division_of (\<Union>q)"
   660 proof (rule division_ofI)
   661   note * = division_ofD[OF assms(1)]
   662   show "finite q"
   663     using "*"(1) assms(2) infinite_super by auto
   664   {
   665     fix k
   666     assume "k \<in> q"
   667     then have kp: "k \<in> p"
   668       using assms(2) by auto
   669     show "k \<subseteq> \<Union>q"
   670       using \<open>k \<in> q\<close> by auto
   671     show "\<exists>a b. k = cbox a b"
   672       using *(4)[OF kp] by auto
   673     show "k \<noteq> {}"
   674       using *(3)[OF kp] by auto
   675   }
   676   fix k1 k2
   677   assume "k1 \<in> q" "k2 \<in> q" "k1 \<noteq> k2"
   678   then have **: "k1 \<in> p" "k2 \<in> p" "k1 \<noteq> k2"
   679     using assms(2) by auto
   680   show "interior k1 \<inter> interior k2 = {}"
   681     using *(5)[OF **] by auto
   682 qed auto
   683 
   684 lemma division_of_union_self[intro]: "p division_of s \<Longrightarrow> p division_of (\<Union>p)"
   685   unfolding division_of_def by auto
   686 
   687 lemma division_of_content_0:
   688   assumes "content (cbox a b) = 0" "d division_of (cbox a b)"
   689   shows "\<forall>k\<in>d. content k = 0"
   690   unfolding forall_in_division[OF assms(2)]
   691   by (metis antisym_conv assms content_pos_le content_subset division_ofD(2))
   692 
   693 lemma division_inter:
   694   fixes s1 s2 :: "'a::euclidean_space set"
   695   assumes "p1 division_of s1"
   696     and "p2 division_of s2"
   697   shows "{k1 \<inter> k2 | k1 k2. k1 \<in> p1 \<and> k2 \<in> p2 \<and> k1 \<inter> k2 \<noteq> {}} division_of (s1 \<inter> s2)"
   698   (is "?A' division_of _")
   699 proof -
   700   let ?A = "{s. s \<in>  (\<lambda>(k1,k2). k1 \<inter> k2) ` (p1 \<times> p2) \<and> s \<noteq> {}}"
   701   have *: "?A' = ?A" by auto
   702   show ?thesis
   703     unfolding *
   704   proof (rule division_ofI)
   705     have "?A \<subseteq> (\<lambda>(x, y). x \<inter> y) ` (p1 \<times> p2)"
   706       by auto
   707     moreover have "finite (p1 \<times> p2)"
   708       using assms unfolding division_of_def by auto
   709     ultimately show "finite ?A" by auto
   710     have *: "\<And>s. \<Union>{x\<in>s. x \<noteq> {}} = \<Union>s"
   711       by auto
   712     show "\<Union>?A = s1 \<inter> s2"
   713       apply (rule set_eqI)
   714       unfolding * and UN_iff
   715       using division_ofD(6)[OF assms(1)] and division_ofD(6)[OF assms(2)]
   716       apply auto
   717       done
   718     {
   719       fix k
   720       assume "k \<in> ?A"
   721       then obtain k1 k2 where k: "k = k1 \<inter> k2" "k1 \<in> p1" "k2 \<in> p2" "k \<noteq> {}"
   722         by auto
   723       then show "k \<noteq> {}"
   724         by auto
   725       show "k \<subseteq> s1 \<inter> s2"
   726         using division_ofD(2)[OF assms(1) k(2)] and division_ofD(2)[OF assms(2) k(3)]
   727         unfolding k by auto
   728       obtain a1 b1 where k1: "k1 = cbox a1 b1"
   729         using division_ofD(4)[OF assms(1) k(2)] by blast
   730       obtain a2 b2 where k2: "k2 = cbox a2 b2"
   731         using division_ofD(4)[OF assms(2) k(3)] by blast
   732       show "\<exists>a b. k = cbox a b"
   733         unfolding k k1 k2 unfolding inter_interval by auto
   734     }
   735     fix k1 k2
   736     assume "k1 \<in> ?A"
   737     then obtain x1 y1 where k1: "k1 = x1 \<inter> y1" "x1 \<in> p1" "y1 \<in> p2" "k1 \<noteq> {}"
   738       by auto
   739     assume "k2 \<in> ?A"
   740     then obtain x2 y2 where k2: "k2 = x2 \<inter> y2" "x2 \<in> p1" "y2 \<in> p2" "k2 \<noteq> {}"
   741       by auto
   742     assume "k1 \<noteq> k2"
   743     then have th: "x1 \<noteq> x2 \<or> y1 \<noteq> y2"
   744       unfolding k1 k2 by auto
   745     have *: "interior x1 \<inter> interior x2 = {} \<or> interior y1 \<inter> interior y2 = {} \<Longrightarrow>
   746       interior (x1 \<inter> y1) \<subseteq> interior x1 \<Longrightarrow> interior (x1 \<inter> y1) \<subseteq> interior y1 \<Longrightarrow>
   747       interior (x2 \<inter> y2) \<subseteq> interior x2 \<Longrightarrow> interior (x2 \<inter> y2) \<subseteq> interior y2 \<Longrightarrow>
   748       interior (x1 \<inter> y1) \<inter> interior (x2 \<inter> y2) = {}" by auto
   749     show "interior k1 \<inter> interior k2 = {}"
   750       unfolding k1 k2
   751       apply (rule *)
   752       using assms division_ofD(5) k1 k2(2) k2(3) th apply auto
   753       done
   754   qed
   755 qed
   756 
   757 lemma division_inter_1:
   758   assumes "d division_of i"
   759     and "cbox a (b::'a::euclidean_space) \<subseteq> i"
   760   shows "{cbox a b \<inter> k | k. k \<in> d \<and> cbox a b \<inter> k \<noteq> {}} division_of (cbox a b)"
   761 proof (cases "cbox a b = {}")
   762   case True
   763   show ?thesis
   764     unfolding True and division_of_trivial by auto
   765 next
   766   case False
   767   have *: "cbox a b \<inter> i = cbox a b" using assms(2) by auto
   768   show ?thesis
   769     using division_inter[OF division_of_self[OF False] assms(1)]
   770     unfolding * by auto
   771 qed
   772 
   773 lemma elementary_inter:
   774   fixes s t :: "'a::euclidean_space set"
   775   assumes "p1 division_of s"
   776     and "p2 division_of t"
   777   shows "\<exists>p. p division_of (s \<inter> t)"
   778 using assms division_inter by blast
   779 
   780 lemma elementary_inters:
   781   assumes "finite f"
   782     and "f \<noteq> {}"
   783     and "\<forall>s\<in>f. \<exists>p. p division_of (s::('a::euclidean_space) set)"
   784   shows "\<exists>p. p division_of (\<Inter>f)"
   785   using assms
   786 proof (induct f rule: finite_induct)
   787   case (insert x f)
   788   show ?case
   789   proof (cases "f = {}")
   790     case True
   791     then show ?thesis
   792       unfolding True using insert by auto
   793   next
   794     case False
   795     obtain p where "p division_of \<Inter>f"
   796       using insert(3)[OF False insert(5)[unfolded ball_simps,THEN conjunct2]] ..
   797     moreover obtain px where "px division_of x"
   798       using insert(5)[rule_format,OF insertI1] ..
   799     ultimately show ?thesis
   800       by (simp add: elementary_inter Inter_insert)
   801   qed
   802 qed auto
   803 
   804 lemma division_disjoint_union:
   805   assumes "p1 division_of s1"
   806     and "p2 division_of s2"
   807     and "interior s1 \<inter> interior s2 = {}"
   808   shows "(p1 \<union> p2) division_of (s1 \<union> s2)"
   809 proof (rule division_ofI)
   810   note d1 = division_ofD[OF assms(1)]
   811   note d2 = division_ofD[OF assms(2)]
   812   show "finite (p1 \<union> p2)"
   813     using d1(1) d2(1) by auto
   814   show "\<Union>(p1 \<union> p2) = s1 \<union> s2"
   815     using d1(6) d2(6) by auto
   816   {
   817     fix k1 k2
   818     assume as: "k1 \<in> p1 \<union> p2" "k2 \<in> p1 \<union> p2" "k1 \<noteq> k2"
   819     moreover
   820     let ?g="interior k1 \<inter> interior k2 = {}"
   821     {
   822       assume as: "k1\<in>p1" "k2\<in>p2"
   823       have ?g
   824         using interior_mono[OF d1(2)[OF as(1)]] interior_mono[OF d2(2)[OF as(2)]]
   825         using assms(3) by blast
   826     }
   827     moreover
   828     {
   829       assume as: "k1\<in>p2" "k2\<in>p1"
   830       have ?g
   831         using interior_mono[OF d1(2)[OF as(2)]] interior_mono[OF d2(2)[OF as(1)]]
   832         using assms(3) by blast
   833     }
   834     ultimately show ?g
   835       using d1(5)[OF _ _ as(3)] and d2(5)[OF _ _ as(3)] by auto
   836   }
   837   fix k
   838   assume k: "k \<in> p1 \<union> p2"
   839   show "k \<subseteq> s1 \<union> s2"
   840     using k d1(2) d2(2) by auto
   841   show "k \<noteq> {}"
   842     using k d1(3) d2(3) by auto
   843   show "\<exists>a b. k = cbox a b"
   844     using k d1(4) d2(4) by auto
   845 qed
   846 
   847 lemma partial_division_extend_1:
   848   fixes a b c d :: "'a::euclidean_space"
   849   assumes incl: "cbox c d \<subseteq> cbox a b"
   850     and nonempty: "cbox c d \<noteq> {}"
   851   obtains p where "p division_of (cbox a b)" "cbox c d \<in> p"
   852 proof
   853   let ?B = "\<lambda>f::'a\<Rightarrow>'a \<times> 'a.
   854     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)"
   855   define p where "p = ?B ` (Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)})"
   856 
   857   show "cbox c d \<in> p"
   858     unfolding p_def
   859     by (auto simp add: box_eq_empty cbox_def intro!: image_eqI[where x="\<lambda>(i::'a)\<in>Basis. (c, d)"])
   860   {
   861     fix i :: 'a
   862     assume "i \<in> Basis"
   863     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"
   864       unfolding box_eq_empty subset_box by (auto simp: not_le)
   865   }
   866   note ord = this
   867 
   868   show "p division_of (cbox a b)"
   869   proof (rule division_ofI)
   870     show "finite p"
   871       unfolding p_def by (auto intro!: finite_PiE)
   872     {
   873       fix k
   874       assume "k \<in> p"
   875       then obtain f where f: "f \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and k: "k = ?B f"
   876         by (auto simp: p_def)
   877       then show "\<exists>a b. k = cbox a b"
   878         by auto
   879       have "k \<subseteq> cbox a b \<and> k \<noteq> {}"
   880       proof (simp add: k box_eq_empty subset_box not_less, safe)
   881         fix i :: 'a
   882         assume i: "i \<in> Basis"
   883         with f have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
   884           by (auto simp: PiE_iff)
   885         with i ord[of i]
   886         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"
   887           by auto
   888       qed
   889       then show "k \<noteq> {}" "k \<subseteq> cbox a b"
   890         by auto
   891       {
   892         fix l
   893         assume "l \<in> p"
   894         then obtain g where g: "g \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}" and l: "l = ?B g"
   895           by (auto simp: p_def)
   896         assume "l \<noteq> k"
   897         have "\<exists>i\<in>Basis. f i \<noteq> g i"
   898         proof (rule ccontr)
   899           assume "\<not> ?thesis"
   900           with f g have "f = g"
   901             by (auto simp: PiE_iff extensional_def intro!: ext)
   902           with \<open>l \<noteq> k\<close> show False
   903             by (simp add: l k)
   904         qed
   905         then obtain i where *: "i \<in> Basis" "f i \<noteq> g i" ..
   906         then have "f i = (a, c) \<or> f i = (c, d) \<or> f i = (d, b)"
   907                   "g i = (a, c) \<or> g i = (c, d) \<or> g i = (d, b)"
   908           using f g by (auto simp: PiE_iff)
   909         with * ord[of i] show "interior l \<inter> interior k = {}"
   910           by (auto simp add: l k interior_cbox disjoint_interval intro!: bexI[of _ i])
   911       }
   912       note \<open>k \<subseteq> cbox a b\<close>
   913     }
   914     moreover
   915     {
   916       fix x assume x: "x \<in> cbox a b"
   917       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)}"
   918       proof
   919         fix i :: 'a
   920         assume "i \<in> Basis"
   921         with x ord[of i]
   922         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>
   923             (d \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i)"
   924           by (auto simp: cbox_def)
   925         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)}"
   926           by auto
   927       qed
   928       then obtain f where
   929         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)}"
   930         unfolding bchoice_iff ..
   931       moreover from f have "restrict f Basis \<in> Basis \<rightarrow>\<^sub>E {(a, c), (c, d), (d, b)}"
   932         by auto
   933       moreover from f have "x \<in> ?B (restrict f Basis)"
   934         by (auto simp: mem_box)
   935       ultimately have "\<exists>k\<in>p. x \<in> k"
   936         unfolding p_def by blast
   937     }
   938     ultimately show "\<Union>p = cbox a b"
   939       by auto
   940   qed
   941 qed
   942 
   943 lemma partial_division_extend_interval:
   944   assumes "p division_of (\<Union>p)" "(\<Union>p) \<subseteq> cbox a b"
   945   obtains q where "p \<subseteq> q" "q division_of cbox a (b::'a::euclidean_space)"
   946 proof (cases "p = {}")
   947   case True
   948   obtain q where "q division_of (cbox a b)"
   949     by (rule elementary_interval)
   950   then show ?thesis
   951     using True that by blast
   952 next
   953   case False
   954   note p = division_ofD[OF assms(1)]
   955   have div_cbox: "\<forall>k\<in>p. \<exists>q. q division_of cbox a b \<and> k \<in> q"
   956   proof
   957     fix k
   958     assume kp: "k \<in> p"
   959     obtain c d where k: "k = cbox c d"
   960       using p(4)[OF kp] by blast
   961     have *: "cbox c d \<subseteq> cbox a b" "cbox c d \<noteq> {}"
   962       using p(2,3)[OF kp, unfolded k] using assms(2)
   963       by (blast intro: order.trans)+
   964     obtain q where "q division_of cbox a b" "cbox c d \<in> q"
   965       by (rule partial_division_extend_1[OF *])
   966     then show "\<exists>q. q division_of cbox a b \<and> k \<in> q"
   967       unfolding k by auto
   968   qed
   969   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"
   970     using bchoice[OF div_cbox] by blast
   971   { fix x
   972     assume x: "x \<in> p"
   973     have "q x division_of \<Union>q x"
   974       apply (rule division_ofI)
   975       using division_ofD[OF q(1)[OF x]]
   976       apply auto
   977       done }
   978   then have "\<And>x. x \<in> p \<Longrightarrow> \<exists>d. d division_of \<Union>(q x - {x})"
   979     by (meson Diff_subset division_of_subset)
   980   then have "\<exists>d. d division_of \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p)"
   981     apply -
   982     apply (rule elementary_inters [OF finite_imageI[OF p(1)]])
   983     apply (auto simp: False elementary_inters [OF finite_imageI[OF p(1)]])
   984     done
   985   then obtain d where d: "d division_of \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p)" ..
   986   have "d \<union> p division_of cbox a b"
   987   proof -
   988     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
   989     have cbox_eq: "cbox a b = \<Inter>((\<lambda>i. \<Union>(q i - {i})) ` p) \<union> \<Union>p"
   990     proof (rule te[OF False], clarify)
   991       fix i
   992       assume i: "i \<in> p"
   993       show "\<Union>(q i - {i}) \<union> i = cbox a b"
   994         using division_ofD(6)[OF q(1)[OF i]] using q(2)[OF i] by auto
   995     qed
   996     { fix k
   997       assume k: "k \<in> p"
   998       have *: "\<And>u t s. t \<inter> s = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<inter> t = {}"
   999         by auto
  1000       have "interior (\<Inter>i\<in>p. \<Union>(q i - {i})) \<inter> interior k = {}"
  1001       proof (rule *[OF inter_interior_unions_intervals])
  1002         note qk=division_ofD[OF q(1)[OF k]]
  1003         show "finite (q k - {k})" "open (interior k)" "\<forall>t\<in>q k - {k}. \<exists>a b. t = cbox a b"
  1004           using qk by auto
  1005         show "\<forall>t\<in>q k - {k}. interior k \<inter> interior t = {}"
  1006           using qk(5) using q(2)[OF k] by auto
  1007         show "interior (\<Inter>i\<in>p. \<Union>(q i - {i})) \<subseteq> interior (\<Union>(q k - {k}))"
  1008           apply (rule interior_mono)+
  1009           using k
  1010           apply auto
  1011           done
  1012       qed } note [simp] = this
  1013     show "d \<union> p division_of (cbox a b)"
  1014       unfolding cbox_eq
  1015       apply (rule division_disjoint_union[OF d assms(1)])
  1016       apply (rule inter_interior_unions_intervals)
  1017       apply (rule p open_interior ballI)+
  1018       apply simp_all
  1019       done
  1020   qed
  1021   then show ?thesis
  1022     by (meson Un_upper2 that)
  1023 qed
  1024 
  1025 lemma elementary_bounded[dest]:
  1026   fixes s :: "'a::euclidean_space set"
  1027   shows "p division_of s \<Longrightarrow> bounded s"
  1028   unfolding division_of_def by (metis bounded_Union bounded_cbox)
  1029 
  1030 lemma elementary_subset_cbox:
  1031   "p division_of s \<Longrightarrow> \<exists>a b. s \<subseteq> cbox a (b::'a::euclidean_space)"
  1032   by (meson elementary_bounded bounded_subset_cbox)
  1033 
  1034 lemma division_union_intervals_exists:
  1035   fixes a b :: "'a::euclidean_space"
  1036   assumes "cbox a b \<noteq> {}"
  1037   obtains p where "(insert (cbox a b) p) division_of (cbox a b \<union> cbox c d)"
  1038 proof (cases "cbox c d = {}")
  1039   case True
  1040   show ?thesis
  1041     apply (rule that[of "{}"])
  1042     unfolding True
  1043     using assms
  1044     apply auto
  1045     done
  1046 next
  1047   case False
  1048   show ?thesis
  1049   proof (cases "cbox a b \<inter> cbox c d = {}")
  1050     case True
  1051     then show ?thesis
  1052       by (metis that False assms division_disjoint_union division_of_self insert_is_Un interior_Int interior_empty)
  1053   next
  1054     case False
  1055     obtain u v where uv: "cbox a b \<inter> cbox c d = cbox u v"
  1056       unfolding inter_interval by auto
  1057     have uv_sub: "cbox u v \<subseteq> cbox c d" using uv by auto
  1058     obtain p where "p division_of cbox c d" "cbox u v \<in> p"
  1059       by (rule partial_division_extend_1[OF uv_sub False[unfolded uv]])
  1060     note p = this division_ofD[OF this(1)]
  1061     have "interior (cbox a b \<inter> \<Union>(p - {cbox u v})) = interior(cbox u v \<inter> \<Union>(p - {cbox u v}))"
  1062       apply (rule arg_cong[of _ _ interior])
  1063       using p(8) uv by auto
  1064     also have "\<dots> = {}"
  1065       unfolding interior_Int
  1066       apply (rule inter_interior_unions_intervals)
  1067       using p(6) p(7)[OF p(2)] p(3)
  1068       apply auto
  1069       done
  1070     finally have [simp]: "interior (cbox a b) \<inter> interior (\<Union>(p - {cbox u v})) = {}" by simp
  1071     have cbe: "cbox a b \<union> cbox c d = cbox a b \<union> \<Union>(p - {cbox u v})"
  1072       using p(8) unfolding uv[symmetric] by auto
  1073     have "insert (cbox a b) (p - {cbox u v}) division_of cbox a b \<union> \<Union>(p - {cbox u v})"
  1074     proof -
  1075       have "{cbox a b} division_of cbox a b"
  1076         by (simp add: assms division_of_self)
  1077       then show "insert (cbox a b) (p - {cbox u v}) division_of cbox a b \<union> \<Union>(p - {cbox u v})"
  1078         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))
  1079     qed
  1080     with that[of "p - {cbox u v}"] show ?thesis by (simp add: cbe)
  1081   qed
  1082 qed
  1083 
  1084 lemma division_of_unions:
  1085   assumes "finite f"
  1086     and "\<And>p. p \<in> f \<Longrightarrow> p division_of (\<Union>p)"
  1087     and "\<And>k1 k2. k1 \<in> \<Union>f \<Longrightarrow> k2 \<in> \<Union>f \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
  1088   shows "\<Union>f division_of \<Union>\<Union>f"
  1089   using assms
  1090   by (auto intro!: division_ofI)
  1091 
  1092 lemma elementary_union_interval:
  1093   fixes a b :: "'a::euclidean_space"
  1094   assumes "p division_of \<Union>p"
  1095   obtains q where "q division_of (cbox a b \<union> \<Union>p)"
  1096 proof -
  1097   note assm = division_ofD[OF assms]
  1098   have lem1: "\<And>f s. \<Union>\<Union>(f ` s) = \<Union>((\<lambda>x. \<Union>(f x)) ` s)"
  1099     by auto
  1100   have lem2: "\<And>f s. f \<noteq> {} \<Longrightarrow> \<Union>{s \<union> t |t. t \<in> f} = s \<union> \<Union>f"
  1101     by auto
  1102   {
  1103     presume "p = {} \<Longrightarrow> thesis"
  1104       "cbox a b = {} \<Longrightarrow> thesis"
  1105       "cbox a b \<noteq> {} \<Longrightarrow> interior (cbox a b) = {} \<Longrightarrow> thesis"
  1106       "p \<noteq> {} \<Longrightarrow> interior (cbox a b)\<noteq>{} \<Longrightarrow> cbox a b \<noteq> {} \<Longrightarrow> thesis"
  1107     then show thesis by auto
  1108   next
  1109     assume as: "p = {}"
  1110     obtain p where "p division_of (cbox a b)"
  1111       by (rule elementary_interval)
  1112     then show thesis
  1113       using as that by auto
  1114   next
  1115     assume as: "cbox a b = {}"
  1116     show thesis
  1117       using as assms that by auto
  1118   next
  1119     assume as: "interior (cbox a b) = {}" "cbox a b \<noteq> {}"
  1120     show thesis
  1121       apply (rule that[of "insert (cbox a b) p"],rule division_ofI)
  1122       unfolding finite_insert
  1123       apply (rule assm(1)) unfolding Union_insert
  1124       using assm(2-4) as
  1125       apply -
  1126       apply (fast dest: assm(5))+
  1127       done
  1128   next
  1129     assume as: "p \<noteq> {}" "interior (cbox a b) \<noteq> {}" "cbox a b \<noteq> {}"
  1130     have "\<forall>k\<in>p. \<exists>q. (insert (cbox a b) q) division_of (cbox a b \<union> k)"
  1131     proof
  1132       fix k
  1133       assume kp: "k \<in> p"
  1134       from assm(4)[OF kp] obtain c d where "k = cbox c d" by blast
  1135       then show "\<exists>q. (insert (cbox a b) q) division_of (cbox a b \<union> k)"
  1136         by (meson as(3) division_union_intervals_exists)
  1137     qed
  1138     from bchoice[OF this] obtain q where "\<forall>x\<in>p. insert (cbox a b) (q x) division_of (cbox a b) \<union> x" ..
  1139     note q = division_ofD[OF this[rule_format]]
  1140     let ?D = "\<Union>{insert (cbox a b) (q k) | k. k \<in> p}"
  1141     show thesis
  1142     proof (rule that[OF division_ofI])
  1143       have *: "{insert (cbox a b) (q k) |k. k \<in> p} = (\<lambda>k. insert (cbox a b) (q k)) ` p"
  1144         by auto
  1145       show "finite ?D"
  1146         using "*" assm(1) q(1) by auto
  1147       show "\<Union>?D = cbox a b \<union> \<Union>p"
  1148         unfolding * lem1
  1149         unfolding lem2[OF as(1), of "cbox a b", symmetric]
  1150         using q(6)
  1151         by auto
  1152       fix k
  1153       assume k: "k \<in> ?D"
  1154       then show "k \<subseteq> cbox a b \<union> \<Union>p"
  1155         using q(2) by auto
  1156       show "k \<noteq> {}"
  1157         using q(3) k by auto
  1158       show "\<exists>a b. k = cbox a b"
  1159         using q(4) k by auto
  1160       fix k'
  1161       assume k': "k' \<in> ?D" "k \<noteq> k'"
  1162       obtain x where x: "k \<in> insert (cbox a b) (q x)" "x\<in>p"
  1163         using k by auto
  1164       obtain x' where x': "k'\<in>insert (cbox a b) (q x')" "x'\<in>p"
  1165         using k' by auto
  1166       show "interior k \<inter> interior k' = {}"
  1167       proof (cases "x = x'")
  1168         case True
  1169         show ?thesis
  1170           using True k' q(5) x' x by auto
  1171       next
  1172         case False
  1173         {
  1174           presume "k = cbox a b \<Longrightarrow> ?thesis"
  1175             and "k' = cbox a b \<Longrightarrow> ?thesis"
  1176             and "k \<noteq> cbox a b \<Longrightarrow> k' \<noteq> cbox a b \<Longrightarrow> ?thesis"
  1177           then show ?thesis by linarith
  1178         next
  1179           assume as': "k  = cbox a b"
  1180           show ?thesis
  1181             using as' k' q(5) x' by blast
  1182         next
  1183           assume as': "k' = cbox a b"
  1184           show ?thesis
  1185             using as' k'(2) q(5) x by blast
  1186         }
  1187         assume as': "k \<noteq> cbox a b" "k' \<noteq> cbox a b"
  1188         obtain c d where k: "k = cbox c d"
  1189           using q(4)[OF x(2,1)] by blast
  1190         have "interior k \<inter> interior (cbox a b) = {}"
  1191           using as' k'(2) q(5) x by blast
  1192         then have "interior k \<subseteq> interior x"
  1193         using interior_subset_union_intervals
  1194           by (metis as(2) k q(2) x interior_subset_union_intervals)
  1195         moreover
  1196         obtain c d where c_d: "k' = cbox c d"
  1197           using q(4)[OF x'(2,1)] by blast
  1198         have "interior k' \<inter> interior (cbox a b) = {}"
  1199           using as'(2) q(5) x' by blast
  1200         then have "interior k' \<subseteq> interior x'"
  1201           by (metis as(2) c_d interior_subset_union_intervals q(2) x'(1) x'(2))
  1202         ultimately show ?thesis
  1203           using assm(5)[OF x(2) x'(2) False] by auto
  1204       qed
  1205     qed
  1206   }
  1207 qed
  1208 
  1209 lemma elementary_unions_intervals:
  1210   assumes fin: "finite f"
  1211     and "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = cbox a (b::'a::euclidean_space)"
  1212   obtains p where "p division_of (\<Union>f)"
  1213 proof -
  1214   have "\<exists>p. p division_of (\<Union>f)"
  1215   proof (induct_tac f rule:finite_subset_induct)
  1216     show "\<exists>p. p division_of \<Union>{}" using elementary_empty by auto
  1217   next
  1218     fix x F
  1219     assume as: "finite F" "x \<notin> F" "\<exists>p. p division_of \<Union>F" "x\<in>f"
  1220     from this(3) obtain p where p: "p division_of \<Union>F" ..
  1221     from assms(2)[OF as(4)] obtain a b where x: "x = cbox a b" by blast
  1222     have *: "\<Union>F = \<Union>p"
  1223       using division_ofD[OF p] by auto
  1224     show "\<exists>p. p division_of \<Union>insert x F"
  1225       using elementary_union_interval[OF p[unfolded *], of a b]
  1226       unfolding Union_insert x * by metis
  1227   qed (insert assms, auto)
  1228   then show ?thesis
  1229     using that by auto
  1230 qed
  1231 
  1232 lemma elementary_union:
  1233   fixes s t :: "'a::euclidean_space set"
  1234   assumes "ps division_of s" "pt division_of t"
  1235   obtains p where "p division_of (s \<union> t)"
  1236 proof -
  1237   have *: "s \<union> t = \<Union>ps \<union> \<Union>pt"
  1238     using assms unfolding division_of_def by auto
  1239   show ?thesis
  1240     apply (rule elementary_unions_intervals[of "ps \<union> pt"])
  1241     using assms apply auto
  1242     by (simp add: * that)
  1243 qed
  1244 
  1245 lemma partial_division_extend:
  1246   fixes t :: "'a::euclidean_space set"
  1247   assumes "p division_of s"
  1248     and "q division_of t"
  1249     and "s \<subseteq> t"
  1250   obtains r where "p \<subseteq> r" and "r division_of t"
  1251 proof -
  1252   note divp = division_ofD[OF assms(1)] and divq = division_ofD[OF assms(2)]
  1253   obtain a b where ab: "t \<subseteq> cbox a b"
  1254     using elementary_subset_cbox[OF assms(2)] by auto
  1255   obtain r1 where "p \<subseteq> r1" "r1 division_of (cbox a b)"
  1256     using assms
  1257     by (metis ab dual_order.trans partial_division_extend_interval divp(6))
  1258   note r1 = this division_ofD[OF this(2)]
  1259   obtain p' where "p' division_of \<Union>(r1 - p)"
  1260     apply (rule elementary_unions_intervals[of "r1 - p"])
  1261     using r1(3,6)
  1262     apply auto
  1263     done
  1264   then obtain r2 where r2: "r2 division_of (\<Union>(r1 - p)) \<inter> (\<Union>q)"
  1265     by (metis assms(2) divq(6) elementary_inter)
  1266   {
  1267     fix x
  1268     assume x: "x \<in> t" "x \<notin> s"
  1269     then have "x\<in>\<Union>r1"
  1270       unfolding r1 using ab by auto
  1271     then obtain r where r: "r \<in> r1" "x \<in> r"
  1272       unfolding Union_iff ..
  1273     moreover
  1274     have "r \<notin> p"
  1275     proof
  1276       assume "r \<in> p"
  1277       then have "x \<in> s" using divp(2) r by auto
  1278       then show False using x by auto
  1279     qed
  1280     ultimately have "x\<in>\<Union>(r1 - p)" by auto
  1281   }
  1282   then have *: "t = \<Union>p \<union> (\<Union>(r1 - p) \<inter> \<Union>q)"
  1283     unfolding divp divq using assms(3) by auto
  1284   show ?thesis
  1285     apply (rule that[of "p \<union> r2"])
  1286     unfolding *
  1287     defer
  1288     apply (rule division_disjoint_union)
  1289     unfolding divp(6)
  1290     apply(rule assms r2)+
  1291   proof -
  1292     have "interior s \<inter> interior (\<Union>(r1-p)) = {}"
  1293     proof (rule inter_interior_unions_intervals)
  1294       show "finite (r1 - p)" and "open (interior s)" and "\<forall>t\<in>r1-p. \<exists>a b. t = cbox a b"
  1295         using r1 by auto
  1296       have *: "\<And>s. (\<And>x. x \<in> s \<Longrightarrow> False) \<Longrightarrow> s = {}"
  1297         by auto
  1298       show "\<forall>t\<in>r1-p. interior s \<inter> interior t = {}"
  1299       proof
  1300         fix m x
  1301         assume as: "m \<in> r1 - p"
  1302         have "interior m \<inter> interior (\<Union>p) = {}"
  1303         proof (rule inter_interior_unions_intervals)
  1304           show "finite p" and "open (interior m)" and "\<forall>t\<in>p. \<exists>a b. t = cbox a b"
  1305             using divp by auto
  1306           show "\<forall>t\<in>p. interior m \<inter> interior t = {}"
  1307             by (metis DiffD1 DiffD2 as r1(1) r1(7) set_rev_mp)
  1308         qed
  1309         then show "interior s \<inter> interior m = {}"
  1310           unfolding divp by auto
  1311       qed
  1312     qed
  1313     then show "interior s \<inter> interior (\<Union>(r1-p) \<inter> (\<Union>q)) = {}"
  1314       using interior_subset by auto
  1315   qed auto
  1316 qed
  1317 
  1318 lemma division_split_left_inj:
  1319   fixes type :: "'a::euclidean_space"
  1320   assumes "d division_of i"
  1321     and "k1 \<in> d"
  1322     and "k2 \<in> d"
  1323     and "k1 \<noteq> k2"
  1324     and "k1 \<inter> {x::'a. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c}"
  1325     and k: "k\<in>Basis"
  1326   shows "content(k1 \<inter> {x. x\<bullet>k \<le> c}) = 0"
  1327 proof -
  1328   note d=division_ofD[OF assms(1)]
  1329   have *: "\<And>(a::'a) b c. content (cbox a b \<inter> {x. x\<bullet>k \<le> c}) = 0 \<longleftrightarrow>
  1330     interior(cbox a b \<inter> {x. x\<bullet>k \<le> c}) = {}"
  1331     unfolding  interval_split[OF k] content_eq_0_interior by auto
  1332   guess u1 v1 using d(4)[OF assms(2)] by (elim exE) note uv1=this
  1333   guess u2 v2 using d(4)[OF assms(3)] by (elim exE) note uv2=this
  1334   have **: "\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}"
  1335     by auto
  1336   show ?thesis
  1337     unfolding uv1 uv2 *
  1338     apply (rule **[OF d(5)[OF assms(2-4)]])
  1339     apply (simp add: uv1)
  1340     using assms(5) uv1 by auto
  1341 qed
  1342 
  1343 lemma division_split_right_inj:
  1344   fixes type :: "'a::euclidean_space"
  1345   assumes "d division_of i"
  1346     and "k1 \<in> d"
  1347     and "k2 \<in> d"
  1348     and "k1 \<noteq> k2"
  1349     and "k1 \<inter> {x::'a. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c}"
  1350     and k: "k \<in> Basis"
  1351   shows "content (k1 \<inter> {x. x\<bullet>k \<ge> c}) = 0"
  1352 proof -
  1353   note d=division_ofD[OF assms(1)]
  1354   have *: "\<And>a b::'a. \<And>c. content(cbox a b \<inter> {x. x\<bullet>k \<ge> c}) = 0 \<longleftrightarrow>
  1355     interior(cbox a b \<inter> {x. x\<bullet>k \<ge> c}) = {}"
  1356     unfolding interval_split[OF k] content_eq_0_interior by auto
  1357   guess u1 v1 using d(4)[OF assms(2)] by (elim exE) note uv1=this
  1358   guess u2 v2 using d(4)[OF assms(3)] by (elim exE) note uv2=this
  1359   have **: "\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}"
  1360     by auto
  1361   show ?thesis
  1362     unfolding uv1 uv2 *
  1363     apply (rule **[OF d(5)[OF assms(2-4)]])
  1364     apply (simp add: uv1)
  1365     using assms(5) uv1 by auto
  1366 qed
  1367 
  1368 
  1369 lemma division_split:
  1370   fixes a :: "'a::euclidean_space"
  1371   assumes "p division_of (cbox a b)"
  1372     and k: "k\<in>Basis"
  1373   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})"
  1374       (is "?p1 division_of ?I1")
  1375     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})"
  1376       (is "?p2 division_of ?I2")
  1377 proof (rule_tac[!] division_ofI)
  1378   note p = division_ofD[OF assms(1)]
  1379   show "finite ?p1" "finite ?p2"
  1380     using p(1) by auto
  1381   show "\<Union>?p1 = ?I1" "\<Union>?p2 = ?I2"
  1382     unfolding p(6)[symmetric] by auto
  1383   {
  1384     fix k
  1385     assume "k \<in> ?p1"
  1386     then guess l unfolding mem_Collect_eq by (elim exE conjE) note l=this
  1387     guess u v using p(4)[OF l(2)] by (elim exE) note uv=this
  1388     show "k \<subseteq> ?I1"
  1389       using l p(2) uv by force
  1390     show  "k \<noteq> {}"
  1391       by (simp add: l)
  1392     show  "\<exists>a b. k = cbox a b"
  1393       apply (simp add: l uv p(2-3)[OF l(2)])
  1394       apply (subst interval_split[OF k])
  1395       apply (auto intro: order.trans)
  1396       done
  1397     fix k'
  1398     assume "k' \<in> ?p1"
  1399     then guess l' unfolding mem_Collect_eq by (elim exE conjE) note l'=this
  1400     assume "k \<noteq> k'"
  1401     then show "interior k \<inter> interior k' = {}"
  1402       unfolding l l' using p(5)[OF l(2) l'(2)] by auto
  1403   }
  1404   {
  1405     fix k
  1406     assume "k \<in> ?p2"
  1407     then guess l unfolding mem_Collect_eq by (elim exE conjE) note l=this
  1408     guess u v using p(4)[OF l(2)] by (elim exE) note uv=this
  1409     show "k \<subseteq> ?I2"
  1410       using l p(2) uv by force
  1411     show  "k \<noteq> {}"
  1412       by (simp add: l)
  1413     show  "\<exists>a b. k = cbox a b"
  1414       apply (simp add: l uv p(2-3)[OF l(2)])
  1415       apply (subst interval_split[OF k])
  1416       apply (auto intro: order.trans)
  1417       done
  1418     fix k'
  1419     assume "k' \<in> ?p2"
  1420     then guess l' unfolding mem_Collect_eq by (elim exE conjE) note l'=this
  1421     assume "k \<noteq> k'"
  1422     then show "interior k \<inter> interior k' = {}"
  1423       unfolding l l' using p(5)[OF l(2) l'(2)] by auto
  1424   }
  1425 qed
  1426 
  1427 subsection \<open>Tagged (partial) divisions.\<close>
  1428 
  1429 definition tagged_partial_division_of (infixr "tagged'_partial'_division'_of" 40)
  1430   where "s tagged_partial_division_of i \<longleftrightarrow>
  1431     finite s \<and>
  1432     (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
  1433     (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
  1434       interior k1 \<inter> interior k2 = {})"
  1435 
  1436 lemma tagged_partial_division_ofD[dest]:
  1437   assumes "s tagged_partial_division_of i"
  1438   shows "finite s"
  1439     and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  1440     and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  1441     and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  1442     and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow>
  1443       (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
  1444   using assms unfolding tagged_partial_division_of_def by blast+
  1445 
  1446 definition tagged_division_of (infixr "tagged'_division'_of" 40)
  1447   where "s tagged_division_of i \<longleftrightarrow> s tagged_partial_division_of i \<and> (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1448 
  1449 lemma tagged_division_of_finite: "s tagged_division_of i \<Longrightarrow> finite s"
  1450   unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  1451 
  1452 lemma tagged_division_of:
  1453   "s tagged_division_of i \<longleftrightarrow>
  1454     finite s \<and>
  1455     (\<forall>x k. (x, k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = cbox a b)) \<and>
  1456     (\<forall>x1 k1 x2 k2. (x1, k1) \<in> s \<and> (x2, k2) \<in> s \<and> (x1, k1) \<noteq> (x2, k2) \<longrightarrow>
  1457       interior k1 \<inter> interior k2 = {}) \<and>
  1458     (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1459   unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  1460 
  1461 lemma tagged_division_ofI:
  1462   assumes "finite s"
  1463     and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  1464     and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  1465     and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  1466     and "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
  1467       interior k1 \<inter> interior k2 = {}"
  1468     and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1469   shows "s tagged_division_of i"
  1470   unfolding tagged_division_of
  1471   using assms
  1472   apply auto
  1473   apply fastforce+
  1474   done
  1475 
  1476 lemma tagged_division_ofD[dest]:  (*FIXME USE A LOCALE*)
  1477   assumes "s tagged_division_of i"
  1478   shows "finite s"
  1479     and "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k"
  1480     and "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  1481     and "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = cbox a b"
  1482     and "\<And>x1 k1 x2 k2. (x1, k1) \<in> s \<Longrightarrow> (x2, k2) \<in> s \<Longrightarrow> (x1, k1) \<noteq> (x2, k2) \<Longrightarrow>
  1483       interior k1 \<inter> interior k2 = {}"
  1484     and "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1485   using assms unfolding tagged_division_of by blast+
  1486 
  1487 lemma division_of_tagged_division:
  1488   assumes "s tagged_division_of i"
  1489   shows "(snd ` s) division_of i"
  1490 proof (rule division_ofI)
  1491   note assm = tagged_division_ofD[OF assms]
  1492   show "\<Union>(snd ` s) = i" "finite (snd ` s)"
  1493     using assm by auto
  1494   fix k
  1495   assume k: "k \<in> snd ` s"
  1496   then obtain xk where xk: "(xk, k) \<in> s"
  1497     by auto
  1498   then show "k \<subseteq> i" "k \<noteq> {}" "\<exists>a b. k = cbox a b"
  1499     using assm by fastforce+
  1500   fix k'
  1501   assume k': "k' \<in> snd ` s" "k \<noteq> k'"
  1502   from this(1) obtain xk' where xk': "(xk', k') \<in> s"
  1503     by auto
  1504   then show "interior k \<inter> interior k' = {}"
  1505     using assm(5) k'(2) xk by blast
  1506 qed
  1507 
  1508 lemma partial_division_of_tagged_division:
  1509   assumes "s tagged_partial_division_of i"
  1510   shows "(snd ` s) division_of \<Union>(snd ` s)"
  1511 proof (rule division_ofI)
  1512   note assm = tagged_partial_division_ofD[OF assms]
  1513   show "finite (snd ` s)" "\<Union>(snd ` s) = \<Union>(snd ` s)"
  1514     using assm by auto
  1515   fix k
  1516   assume k: "k \<in> snd ` s"
  1517   then obtain xk where xk: "(xk, k) \<in> s"
  1518     by auto
  1519   then show "k \<noteq> {}" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>(snd ` s)"
  1520     using assm by auto
  1521   fix k'
  1522   assume k': "k' \<in> snd ` s" "k \<noteq> k'"
  1523   from this(1) obtain xk' where xk': "(xk', k') \<in> s"
  1524     by auto
  1525   then show "interior k \<inter> interior k' = {}"
  1526     using assm(5) k'(2) xk by auto
  1527 qed
  1528 
  1529 lemma tagged_partial_division_subset:
  1530   assumes "s tagged_partial_division_of i"
  1531     and "t \<subseteq> s"
  1532   shows "t tagged_partial_division_of i"
  1533   using assms
  1534   unfolding tagged_partial_division_of_def
  1535   using finite_subset[OF assms(2)]
  1536   by blast
  1537 
  1538 lemma (in comm_monoid_set) over_tagged_division_lemma:
  1539   assumes "p tagged_division_of i"
  1540     and "\<And>u v. cbox u v \<noteq> {} \<Longrightarrow> content (cbox u v) = 0 \<Longrightarrow> d (cbox u v) = \<^bold>1"
  1541   shows "F (\<lambda>(x,k). d k) p = F d (snd ` p)"
  1542 proof -
  1543   have *: "(\<lambda>(x,k). d k) = d \<circ> snd"
  1544     unfolding o_def by (rule ext) auto
  1545   note assm = tagged_division_ofD[OF assms(1)]
  1546   show ?thesis
  1547     unfolding *
  1548   proof (rule reindex_nontrivial[symmetric])
  1549     show "finite p"
  1550       using assm by auto
  1551     fix x y
  1552     assume "x\<in>p" "y\<in>p" "x\<noteq>y" "snd x = snd y"
  1553     obtain a b where ab: "snd x = cbox a b"
  1554       using assm(4)[of "fst x" "snd x"] \<open>x\<in>p\<close> by auto
  1555     have "(fst x, snd y) \<in> p" "(fst x, snd y) \<noteq> y"
  1556       by (metis prod.collapse \<open>x\<in>p\<close> \<open>snd x = snd y\<close> \<open>x \<noteq> y\<close>)+
  1557     with \<open>x\<in>p\<close> \<open>y\<in>p\<close> have "interior (snd x) \<inter> interior (snd y) = {}"
  1558       by (intro assm(5)[of "fst x" _ "fst y"]) auto
  1559     then have "content (cbox a b) = 0"
  1560       unfolding \<open>snd x = snd y\<close>[symmetric] ab content_eq_0_interior by auto
  1561     then have "d (cbox a b) = \<^bold>1"
  1562       using assm(2)[of "fst x" "snd x"] \<open>x\<in>p\<close> ab[symmetric] by (intro assms(2)) auto
  1563     then show "d (snd x) = \<^bold>1"
  1564       unfolding ab by auto
  1565   qed
  1566 qed
  1567 
  1568 lemma tag_in_interval: "p tagged_division_of i \<Longrightarrow> (x, k) \<in> p \<Longrightarrow> x \<in> i"
  1569   by auto
  1570 
  1571 lemma tagged_division_of_empty: "{} tagged_division_of {}"
  1572   unfolding tagged_division_of by auto
  1573 
  1574 lemma tagged_partial_division_of_trivial[simp]: "p tagged_partial_division_of {} \<longleftrightarrow> p = {}"
  1575   unfolding tagged_partial_division_of_def by auto
  1576 
  1577 lemma tagged_division_of_trivial[simp]: "p tagged_division_of {} \<longleftrightarrow> p = {}"
  1578   unfolding tagged_division_of by auto
  1579 
  1580 lemma tagged_division_of_self: "x \<in> cbox a b \<Longrightarrow> {(x,cbox a b)} tagged_division_of (cbox a b)"
  1581   by (rule tagged_division_ofI) auto
  1582 
  1583 lemma tagged_division_of_self_real: "x \<in> {a .. b::real} \<Longrightarrow> {(x,{a .. b})} tagged_division_of {a .. b}"
  1584   unfolding box_real[symmetric]
  1585   by (rule tagged_division_of_self)
  1586 
  1587 lemma tagged_division_union:
  1588   assumes "p1 tagged_division_of s1"
  1589     and "p2 tagged_division_of s2"
  1590     and "interior s1 \<inter> interior s2 = {}"
  1591   shows "(p1 \<union> p2) tagged_division_of (s1 \<union> s2)"
  1592 proof (rule tagged_division_ofI)
  1593   note p1 = tagged_division_ofD[OF assms(1)]
  1594   note p2 = tagged_division_ofD[OF assms(2)]
  1595   show "finite (p1 \<union> p2)"
  1596     using p1(1) p2(1) by auto
  1597   show "\<Union>{k. \<exists>x. (x, k) \<in> p1 \<union> p2} = s1 \<union> s2"
  1598     using p1(6) p2(6) by blast
  1599   fix x k
  1600   assume xk: "(x, k) \<in> p1 \<union> p2"
  1601   show "x \<in> k" "\<exists>a b. k = cbox a b"
  1602     using xk p1(2,4) p2(2,4) by auto
  1603   show "k \<subseteq> s1 \<union> s2"
  1604     using xk p1(3) p2(3) by blast
  1605   fix x' k'
  1606   assume xk': "(x', k') \<in> p1 \<union> p2" "(x, k) \<noteq> (x', k')"
  1607   have *: "\<And>a b. a \<subseteq> s1 \<Longrightarrow> b \<subseteq> s2 \<Longrightarrow> interior a \<inter> interior b = {}"
  1608     using assms(3) interior_mono by blast
  1609   show "interior k \<inter> interior k' = {}"
  1610     apply (cases "(x, k) \<in> p1")
  1611     apply (meson "*" UnE assms(1) assms(2) p1(5) tagged_division_ofD(3) xk'(1) xk'(2))
  1612     by (metis "*" UnE assms(1) assms(2) inf_sup_aci(1) p2(5) tagged_division_ofD(3) xk xk'(1) xk'(2))
  1613 qed
  1614 
  1615 lemma tagged_division_unions:
  1616   assumes "finite iset"
  1617     and "\<forall>i\<in>iset. pfn i tagged_division_of i"
  1618     and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior(i1) \<inter> interior(i2) = {}"
  1619   shows "\<Union>(pfn ` iset) tagged_division_of (\<Union>iset)"
  1620 proof (rule tagged_division_ofI)
  1621   note assm = tagged_division_ofD[OF assms(2)[rule_format]]
  1622   show "finite (\<Union>(pfn ` iset))"
  1623     apply (rule finite_Union)
  1624     using assms
  1625     apply auto
  1626     done
  1627   have "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>((\<lambda>i. \<Union>{k. \<exists>x. (x, k) \<in> pfn i}) ` iset)"
  1628     by blast
  1629   also have "\<dots> = \<Union>iset"
  1630     using assm(6) by auto
  1631   finally show "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>iset" .
  1632   fix x k
  1633   assume xk: "(x, k) \<in> \<Union>(pfn ` iset)"
  1634   then obtain i where i: "i \<in> iset" "(x, k) \<in> pfn i"
  1635     by auto
  1636   show "x \<in> k" "\<exists>a b. k = cbox a b" "k \<subseteq> \<Union>iset"
  1637     using assm(2-4)[OF i] using i(1) by auto
  1638   fix x' k'
  1639   assume xk': "(x', k') \<in> \<Union>(pfn ` iset)" "(x, k) \<noteq> (x', k')"
  1640   then obtain i' where i': "i' \<in> iset" "(x', k') \<in> pfn i'"
  1641     by auto
  1642   have *: "\<And>a b. i \<noteq> i' \<Longrightarrow> a \<subseteq> i \<Longrightarrow> b \<subseteq> i' \<Longrightarrow> interior a \<inter> interior b = {}"
  1643     using i(1) i'(1)
  1644     using assms(3)[rule_format] interior_mono
  1645     by blast
  1646   show "interior k \<inter> interior k' = {}"
  1647     apply (cases "i = i'")
  1648     using assm(5) i' i(2) xk'(2) apply blast
  1649     using "*" assm(3) i' i by auto
  1650 qed
  1651 
  1652 lemma tagged_partial_division_of_union_self:
  1653   assumes "p tagged_partial_division_of s"
  1654   shows "p tagged_division_of (\<Union>(snd ` p))"
  1655   apply (rule tagged_division_ofI)
  1656   using tagged_partial_division_ofD[OF assms]
  1657   apply auto
  1658   done
  1659 
  1660 lemma tagged_division_of_union_self:
  1661   assumes "p tagged_division_of s"
  1662   shows "p tagged_division_of (\<Union>(snd ` p))"
  1663   apply (rule tagged_division_ofI)
  1664   using tagged_division_ofD[OF assms]
  1665   apply auto
  1666   done
  1667 
  1668 subsection \<open>Functions closed on boxes: morphisms from boxes to monoids\<close>
  1669 
  1670 text \<open>This auxiliary structure is used to sum up over the elements of a division. Main theorem is
  1671   @{text operative_division}. Instances for the monoid are @{typ "'a option"}, @{typ real}, and
  1672   @{typ bool}.\<close>
  1673 
  1674 lemma property_empty_interval: "\<forall>a b. content (cbox a b) = 0 \<longrightarrow> P (cbox a b) \<Longrightarrow> P {}"
  1675   using content_empty unfolding empty_as_interval by auto
  1676 
  1677 paragraph \<open>Using additivity of lifted function to encode definedness.\<close>
  1678 
  1679 definition lift_option :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a option \<Rightarrow> 'b option \<Rightarrow> 'c option"
  1680 where
  1681   "lift_option f a' b' = Option.bind a' (\<lambda>a. Option.bind b' (\<lambda>b. Some (f a b)))"
  1682 
  1683 lemma lift_option_simps[simp]:
  1684   "lift_option f (Some a) (Some b) = Some (f a b)"
  1685   "lift_option f None b' = None"
  1686   "lift_option f a' None = None"
  1687   by (auto simp: lift_option_def)
  1688 
  1689 lemma (in comm_monoid) comm_monoid_lift_option: "comm_monoid (lift_option op \<^bold>*) (Some \<^bold>1)"
  1690   proof qed (auto simp: lift_option_def ac_simps split: bind_split)
  1691 
  1692 lemma (in comm_monoid) comm_monoid_set_lift_option: "comm_monoid_set (lift_option op \<^bold>*) (Some \<^bold>1)"
  1693   proof qed (auto simp: lift_option_def ac_simps split: bind_split)
  1694 
  1695 lemma comm_monoid_and: "comm_monoid op \<and> True"
  1696   proof qed auto
  1697 
  1698 lemma comm_monoid_set_and: "comm_monoid_set op \<and> True"
  1699   proof qed auto
  1700 
  1701 paragraph \<open>Operative\<close>
  1702 
  1703 definition (in comm_monoid) operative :: "('b::euclidean_space set \<Rightarrow> 'a) \<Rightarrow> bool"
  1704   where "operative g \<longleftrightarrow>
  1705     (\<forall>a b. content (cbox a b) = 0 \<longrightarrow> g (cbox a b) = \<^bold>1) \<and>
  1706     (\<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}))"
  1707 
  1708 lemma (in comm_monoid) operativeD[dest]:
  1709   assumes "operative g"
  1710   shows "\<And>a b. content (cbox a b) = 0 \<Longrightarrow> g (cbox a b) = \<^bold>1"
  1711     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})"
  1712   using assms unfolding operative_def by auto
  1713 
  1714 lemma (in comm_monoid) operative_empty: "operative g \<Longrightarrow> g {} = \<^bold>1"
  1715   unfolding operative_def by (rule property_empty_interval) auto
  1716 
  1717 lemma operative_content[intro]: "add.operative content"
  1718   by (force simp add: add.operative_def content_split[symmetric])
  1719 
  1720 definition "division_points (k::('a::euclidean_space) set) d =
  1721    {(j,x). j \<in> Basis \<and> (interval_lowerbound k)\<bullet>j < x \<and> x < (interval_upperbound k)\<bullet>j \<and>
  1722      (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  1723 
  1724 lemma division_points_finite:
  1725   fixes i :: "'a::euclidean_space set"
  1726   assumes "d division_of i"
  1727   shows "finite (division_points i d)"
  1728 proof -
  1729   note assm = division_ofD[OF assms]
  1730   let ?M = "\<lambda>j. {(j,x)|x. (interval_lowerbound i)\<bullet>j < x \<and> x < (interval_upperbound i)\<bullet>j \<and>
  1731     (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  1732   have *: "division_points i d = \<Union>(?M ` Basis)"
  1733     unfolding division_points_def by auto
  1734   show ?thesis
  1735     unfolding * using assm by auto
  1736 qed
  1737 
  1738 lemma division_points_subset:
  1739   fixes a :: "'a::euclidean_space"
  1740   assumes "d division_of (cbox a b)"
  1741     and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  1742     and k: "k \<in> Basis"
  1743   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>
  1744       division_points (cbox a b) d" (is ?t1)
  1745     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>
  1746       division_points (cbox a b) d" (is ?t2)
  1747 proof -
  1748   note assm = division_ofD[OF assms(1)]
  1749   have *: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  1750     "\<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"
  1751     "\<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"
  1752     "min (b \<bullet> k) c = c" "max (a \<bullet> k) c = c"
  1753     using assms using less_imp_le by auto
  1754   show ?t1 (*FIXME a horrible mess*)
  1755     unfolding division_points_def interval_split[OF k, of a b]
  1756     unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  1757     unfolding *
  1758     apply (rule subsetI)
  1759     unfolding mem_Collect_eq split_beta
  1760     apply (erule bexE conjE)+
  1761     apply (simp add: )
  1762     apply (erule exE conjE)+
  1763   proof
  1764     fix i l x
  1765     assume as:
  1766       "a \<bullet> fst x < snd x" "snd x < (if fst x = k then c else b \<bullet> fst x)"
  1767       "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1768       "i = l \<inter> {x. x \<bullet> k \<le> c}" "l \<in> d" "l \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}"
  1769       and fstx: "fst x \<in> Basis"
  1770     from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  1771     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"
  1772       using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  1773     have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  1774       using l using as(6) unfolding box_ne_empty[symmetric] by auto
  1775     show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1776       apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  1777       using as(1-3,5) fstx
  1778       unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  1779       apply (auto split: if_split_asm)
  1780       done
  1781     show "snd x < b \<bullet> fst x"
  1782       using as(2) \<open>c < b\<bullet>k\<close> by (auto split: if_split_asm)
  1783   qed
  1784   show ?t2
  1785     unfolding division_points_def interval_split[OF k, of a b]
  1786     unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  1787     unfolding *
  1788     unfolding subset_eq
  1789     apply rule
  1790     unfolding mem_Collect_eq split_beta
  1791     apply (erule bexE conjE)+
  1792     apply (simp only: mem_Collect_eq inner_setsum_left_Basis simp_thms)
  1793     apply (erule exE conjE)+
  1794   proof
  1795     fix i l x
  1796     assume as:
  1797       "(if fst x = k then c else a \<bullet> fst x) < snd x" "snd x < b \<bullet> fst x"
  1798       "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1799       "i = l \<inter> {x. c \<le> x \<bullet> k}" "l \<in> d" "l \<inter> {x. c \<le> x \<bullet> k} \<noteq> {}"
  1800       and fstx: "fst x \<in> Basis"
  1801     from assm(4)[OF this(5)] guess u v by (elim exE) note l=this
  1802     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"
  1803       using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  1804     have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  1805       using l using as(6) unfolding box_ne_empty[symmetric] by auto
  1806     show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1807       apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  1808       using as(1-3,5) fstx
  1809       unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  1810       apply (auto split: if_split_asm)
  1811       done
  1812     show "a \<bullet> fst x < snd x"
  1813       using as(1) \<open>a\<bullet>k < c\<close> by (auto split: if_split_asm)
  1814    qed
  1815 qed
  1816 
  1817 lemma division_points_psubset:
  1818   fixes a :: "'a::euclidean_space"
  1819   assumes "d division_of (cbox a b)"
  1820       and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  1821       and "l \<in> d"
  1822       and "interval_lowerbound l\<bullet>k = c \<or> interval_upperbound l\<bullet>k = c"
  1823       and k: "k \<in> Basis"
  1824   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>
  1825          division_points (cbox a b) d" (is "?D1 \<subset> ?D")
  1826     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>
  1827          division_points (cbox a b) d" (is "?D2 \<subset> ?D")
  1828 proof -
  1829   have ab: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  1830     using assms(2) by (auto intro!:less_imp_le)
  1831   guess u v using division_ofD(4)[OF assms(1,5)] by (elim exE) note l=this
  1832   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"
  1833     using division_ofD(2,2,3)[OF assms(1,5)] unfolding l box_ne_empty
  1834     using subset_box(1)
  1835     apply auto
  1836     apply blast+
  1837     done
  1838   have *: "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  1839           "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  1840     unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  1841     using uv[rule_format, of k] ab k
  1842     by auto
  1843   have "\<exists>x. x \<in> ?D - ?D1"
  1844     using assms(3-)
  1845     unfolding division_points_def interval_bounds[OF ab]
  1846     apply -
  1847     apply (erule disjE)
  1848     apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  1849     apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  1850     done
  1851   moreover have "?D1 \<subseteq> ?D"
  1852     by (auto simp add: assms division_points_subset)
  1853   ultimately show "?D1 \<subset> ?D"
  1854     by blast
  1855   have *: "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  1856     "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  1857     unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  1858     using uv[rule_format, of k] ab k
  1859     by auto
  1860   have "\<exists>x. x \<in> ?D - ?D2"
  1861     using assms(3-)
  1862     unfolding division_points_def interval_bounds[OF ab]
  1863     apply -
  1864     apply (erule disjE)
  1865     apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  1866     apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  1867     done
  1868   moreover have "?D2 \<subseteq> ?D"
  1869     by (auto simp add: assms division_points_subset)
  1870   ultimately show "?D2 \<subset> ?D"
  1871     by blast
  1872 qed
  1873 
  1874 lemma (in comm_monoid_set) operative_division:
  1875   fixes g :: "'b::euclidean_space set \<Rightarrow> 'a"
  1876   assumes g: "operative g" and d: "d division_of (cbox a b)" shows "F g d = g (cbox a b)"
  1877 proof -
  1878   define C where [abs_def]: "C = card (division_points (cbox a b) d)"
  1879   then show ?thesis
  1880     using d
  1881   proof (induction C arbitrary: a b d rule: less_induct)
  1882     case (less a b d)
  1883     show ?case
  1884     proof cases
  1885       show "content (cbox a b) = 0 \<Longrightarrow> F g d = g (cbox a b)"
  1886         using division_of_content_0[OF _ less.prems] operativeD(1)[OF  g] division_ofD(4)[OF less.prems]
  1887         by (fastforce intro!: neutral)
  1888     next
  1889       assume "content (cbox a b) \<noteq> 0"
  1890       note ab = this[unfolded content_lt_nz[symmetric] content_pos_lt_eq]
  1891       then have ab': "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  1892         by (auto intro!: less_imp_le)
  1893       show "F g d = g (cbox a b)"
  1894       proof (cases "division_points (cbox a b) d = {}")
  1895         case True
  1896         { fix u v and j :: 'b
  1897           assume j: "j \<in> Basis" and as: "cbox u v \<in> d"
  1898           then have "cbox u v \<noteq> {}"
  1899             using less.prems by blast
  1900           then have uv: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i" "u\<bullet>j \<le> v\<bullet>j"
  1901             using j unfolding box_ne_empty by auto
  1902           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)"
  1903             using as j by auto
  1904           have "(j, u\<bullet>j) \<notin> division_points (cbox a b) d"
  1905                "(j, v\<bullet>j) \<notin> division_points (cbox a b) d" using True by auto
  1906           note this[unfolded de_Morgan_conj division_points_def mem_Collect_eq split_conv interval_bounds[OF ab'] bex_simps]
  1907           note *[OF this(1)] *[OF this(2)] note this[unfolded interval_bounds[OF uv(1)]]
  1908           moreover
  1909           have "a\<bullet>j \<le> u\<bullet>j" "v\<bullet>j \<le> b\<bullet>j"
  1910             using division_ofD(2,2,3)[OF \<open>d division_of cbox a b\<close> as]
  1911             apply (metis j subset_box(1) uv(1))
  1912             by (metis \<open>cbox u v \<subseteq> cbox a b\<close> j subset_box(1) uv(1))
  1913           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"
  1914             unfolding not_less de_Morgan_disj using ab[rule_format,of j] uv(2) j by force }
  1915         then have d': "\<forall>i\<in>d. \<exists>u v. i = cbox u v \<and>
  1916           (\<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)"
  1917           unfolding forall_in_division[OF less.prems] by blast
  1918         have "(1/2) *\<^sub>R (a+b) \<in> cbox a b"
  1919           unfolding mem_box using ab by(auto intro!: less_imp_le simp: inner_simps)
  1920         note this[unfolded division_ofD(6)[OF \<open>d division_of cbox a b\<close>,symmetric] Union_iff]
  1921         then guess i .. note i=this
  1922         guess u v using d'[rule_format,OF i(1)] by (elim exE conjE) note uv=this
  1923         have "cbox a b \<in> d"
  1924         proof -
  1925           have "u = a" "v = b"
  1926             unfolding euclidean_eq_iff[where 'a='b]
  1927           proof safe
  1928             fix j :: 'b
  1929             assume j: "j \<in> Basis"
  1930             note i(2)[unfolded uv mem_box,rule_format,of j]
  1931             then show "u \<bullet> j = a \<bullet> j" and "v \<bullet> j = b \<bullet> j"
  1932               using uv(2)[rule_format,of j] j by (auto simp: inner_simps)
  1933           qed
  1934           then have "i = cbox a b" using uv by auto
  1935           then show ?thesis using i by auto
  1936         qed
  1937         then have deq: "d = insert (cbox a b) (d - {cbox a b})"
  1938           by auto
  1939         have "F g (d - {cbox a b}) = \<^bold>1"
  1940         proof (intro neutral ballI)
  1941           fix x
  1942           assume x: "x \<in> d - {cbox a b}"
  1943           then have "x\<in>d"
  1944             by auto note d'[rule_format,OF this]
  1945           then guess u v by (elim exE conjE) note uv=this
  1946           have "u \<noteq> a \<or> v \<noteq> b"
  1947             using x[unfolded uv] by auto
  1948           then obtain j where "u\<bullet>j \<noteq> a\<bullet>j \<or> v\<bullet>j \<noteq> b\<bullet>j" and j: "j \<in> Basis"
  1949             unfolding euclidean_eq_iff[where 'a='b] by auto
  1950           then have "u\<bullet>j = v\<bullet>j"
  1951             using uv(2)[rule_format,OF j] by auto
  1952           then have "content (cbox u v) = 0"
  1953             unfolding content_eq_0 using j
  1954             by force
  1955           then show "g x = \<^bold>1"
  1956             unfolding uv(1) by (rule operativeD(1)[OF g])
  1957         qed
  1958         then show "F g d = g (cbox a b)"
  1959           using division_ofD[OF less.prems]
  1960           apply (subst deq)
  1961           apply (subst insert)
  1962           apply auto
  1963           done
  1964       next
  1965         case False
  1966         then have "\<exists>x. x \<in> division_points (cbox a b) d"
  1967           by auto
  1968         then guess k c
  1969           unfolding split_paired_Ex division_points_def mem_Collect_eq split_conv
  1970           apply (elim exE conjE)
  1971           done
  1972         note this(2-4,1) note kc=this[unfolded interval_bounds[OF ab']]
  1973         from this(3) guess j .. note j=this
  1974         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> {}}"
  1975         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> {}}"
  1976         define cb where "cb = (\<Sum>i\<in>Basis. (if i = k then c else b\<bullet>i) *\<^sub>R i)"
  1977         define ca where "ca = (\<Sum>i\<in>Basis. (if i = k then c else a\<bullet>i) *\<^sub>R i)"
  1978         note division_points_psubset[OF \<open>d division_of cbox a b\<close> ab kc(1-2) j]
  1979         note psubset_card_mono[OF _ this(1)] psubset_card_mono[OF _ this(2)]
  1980         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})"
  1981           unfolding interval_split[OF kc(4)]
  1982           apply (rule_tac[!] "less.hyps"[rule_format])
  1983           using division_split[OF \<open>d division_of cbox a b\<close>, where k=k and c=c]
  1984           apply (simp_all add: interval_split kc d1_def d2_def division_points_finite[OF \<open>d division_of cbox a b\<close>])
  1985           done
  1986         { fix l y
  1987           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"
  1988           from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  1989           have "g (l \<inter> {x. x \<bullet> k \<le> c}) = \<^bold>1"
  1990             unfolding leq interval_split[OF kc(4)]
  1991             apply (rule operativeD[OF g])
  1992             unfolding interval_split[symmetric, OF kc(4)]
  1993             using division_split_left_inj less as kc leq by blast
  1994         } note fxk_le = this
  1995         { fix l y
  1996           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"
  1997           from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  1998           have "g (l \<inter> {x. x \<bullet> k \<ge> c}) = \<^bold>1"
  1999             unfolding leq interval_split[OF kc(4)]
  2000             apply (rule operativeD(1)[OF g])
  2001             unfolding interval_split[symmetric,OF kc(4)]
  2002             using division_split_right_inj less leq as kc by blast
  2003         } note fxk_ge = this
  2004         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> {}}"
  2005           using d1_def by auto
  2006         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> {}}"
  2007           using d2_def by auto
  2008         have "g (cbox a b) = F g d1 \<^bold>* F g d2" (is "_ = ?prev")
  2009           unfolding * using g kc(4) by blast
  2010         also have "F g d1 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<le> c})) d"
  2011           unfolding d1_alt using division_of_finite[OF less.prems] fxk_le
  2012           by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  2013         also have "F g d2 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<ge> c})) d"
  2014           unfolding d2_alt using division_of_finite[OF less.prems] fxk_ge
  2015           by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  2016         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})"
  2017           unfolding forall_in_division[OF \<open>d division_of cbox a b\<close>]
  2018           using g kc(4) by blast
  2019         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"
  2020           using * by (simp add: distrib)
  2021         finally show ?thesis by auto
  2022       qed
  2023     qed
  2024   qed
  2025 qed
  2026 
  2027 lemma (in comm_monoid_set) operative_tagged_division:
  2028   assumes f: "operative g" and d: "d tagged_division_of (cbox a b)"
  2029   shows "F (\<lambda>(x, l). g l) d = g (cbox a b)"
  2030   unfolding d[THEN division_of_tagged_division, THEN operative_division[OF f], symmetric]
  2031   by (simp add: f[THEN operativeD(1)] over_tagged_division_lemma[OF d])
  2032 
  2033 lemma additive_content_division: "d division_of (cbox a b) \<Longrightarrow> setsum content d = content (cbox a b)"
  2034   by (metis operative_content setsum.operative_division)
  2035 
  2036 lemma additive_content_tagged_division:
  2037   "d tagged_division_of (cbox a b) \<Longrightarrow> setsum (\<lambda>(x,l). content l) d = content (cbox a b)"
  2038   unfolding setsum.operative_tagged_division[OF operative_content, symmetric] by blast
  2039 
  2040 lemma
  2041   shows real_inner_1_left: "inner 1 x = x"
  2042   and real_inner_1_right: "inner x 1 = x"
  2043   by simp_all
  2044 
  2045 lemma content_real_eq_0: "content {a .. b::real} = 0 \<longleftrightarrow> a \<ge> b"
  2046   by (metis atLeastatMost_empty_iff2 content_empty content_real diff_self eq_iff le_cases le_iff_diff_le_0)
  2047 
  2048 lemma interval_real_split:
  2049   "{a .. b::real} \<inter> {x. x \<le> c} = {a .. min b c}"
  2050   "{a .. b} \<inter> {x. c \<le> x} = {max a c .. b}"
  2051   apply (metis Int_atLeastAtMostL1 atMost_def)
  2052   apply (metis Int_atLeastAtMostL2 atLeast_def)
  2053   done
  2054 
  2055 lemma (in comm_monoid) operative_1_lt:
  2056   "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  2057     ((\<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}))"
  2058   apply (simp add: operative_def content_real_eq_0 atMost_def[symmetric] atLeast_def[symmetric]
  2059               del: content_real_if)
  2060 proof safe
  2061   fix a b c :: real
  2062   assume *: "\<forall>a b c. g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  2063   assume "a < c" "c < b"
  2064   with *[rule_format, of a b c] show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2065     by (simp add: less_imp_le min.absorb2 max.absorb2)
  2066 next
  2067   fix a b c :: real
  2068   assume as: "\<forall>a b. b \<le> a \<longrightarrow> g {a..b} = \<^bold>1"
  2069     "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2070   from as(1)[rule_format, of 0 1] as(1)[rule_format, of a a for a] as(2)
  2071   have [simp]: "g {} = \<^bold>1" "\<And>a. g {a} = \<^bold>1"
  2072     "\<And>a b c. a < c \<Longrightarrow> c < b \<Longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2073     by auto
  2074   show "g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  2075     by (auto simp: min_def max_def le_less)
  2076 qed
  2077 
  2078 lemma (in comm_monoid) operative_1_le:
  2079   "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  2080     ((\<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}))"
  2081   unfolding operative_1_lt
  2082 proof safe
  2083   fix a b c :: real
  2084   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"
  2085   show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2086     apply (rule as(1)[rule_format])
  2087     using as(2-)
  2088     apply auto
  2089     done
  2090 next
  2091   fix a b c :: real
  2092   assume "\<forall>a b. b \<le> a \<longrightarrow> g {a .. b} = \<^bold>1"
  2093     and "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2094     and "a \<le> c"
  2095     and "c \<le> b"
  2096   note as = this[rule_format]
  2097   show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2098   proof (cases "c = a \<or> c = b")
  2099     case False
  2100     then show ?thesis
  2101       apply -
  2102       apply (subst as(2))
  2103       using as(3-)
  2104       apply auto
  2105       done
  2106   next
  2107     case True
  2108     then show ?thesis
  2109     proof
  2110       assume *: "c = a"
  2111       then have "g {a .. c} = \<^bold>1"
  2112         apply -
  2113         apply (rule as(1)[rule_format])
  2114         apply auto
  2115         done
  2116       then show ?thesis
  2117         unfolding * by auto
  2118     next
  2119       assume *: "c = b"
  2120       then have "g {c .. b} = \<^bold>1"
  2121         apply -
  2122         apply (rule as(1)[rule_format])
  2123         apply auto
  2124         done
  2125       then show ?thesis
  2126         unfolding * by auto
  2127     qed
  2128   qed
  2129 qed
  2130 
  2131 subsection \<open>Fine-ness of a partition w.r.t. a gauge.\<close>
  2132 
  2133 definition fine  (infixr "fine" 46)
  2134   where "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d x)"
  2135 
  2136 lemma fineI:
  2137   assumes "\<And>x k. (x, k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  2138   shows "d fine s"
  2139   using assms unfolding fine_def by auto
  2140 
  2141 lemma fineD[dest]:
  2142   assumes "d fine s"
  2143   shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  2144   using assms unfolding fine_def by auto
  2145 
  2146 lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
  2147   unfolding fine_def by auto
  2148 
  2149 lemma fine_inters:
  2150  "(\<lambda>x. \<Inter>{f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
  2151   unfolding fine_def by blast
  2152 
  2153 lemma fine_union: "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
  2154   unfolding fine_def by blast
  2155 
  2156 lemma fine_unions: "(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
  2157   unfolding fine_def by auto
  2158 
  2159 lemma fine_subset: "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
  2160   unfolding fine_def by blast
  2161 
  2162 
  2163 subsection \<open>Gauge integral. Define on compact intervals first, then use a limit.\<close>
  2164 
  2165 definition has_integral_compact_interval (infixr "has'_integral'_compact'_interval" 46)
  2166   where "(f has_integral_compact_interval y) i \<longleftrightarrow>
  2167     (\<forall>e>0. \<exists>d. gauge d \<and>
  2168       (\<forall>p. p tagged_division_of i \<and> d fine p \<longrightarrow>
  2169         norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - y) < e))"
  2170 
  2171 definition has_integral ::
  2172     "('n::euclidean_space \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'b \<Rightarrow> 'n set \<Rightarrow> bool"
  2173   (infixr "has'_integral" 46)
  2174   where "(f has_integral y) i \<longleftrightarrow>
  2175     (if \<exists>a b. i = cbox a b
  2176      then (f has_integral_compact_interval y) i
  2177      else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  2178       (\<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral_compact_interval z) (cbox a b) \<and>
  2179         norm (z - y) < e)))"
  2180 
  2181 lemma has_integral:
  2182   "(f has_integral y) (cbox a b) \<longleftrightarrow>
  2183     (\<forall>e>0. \<exists>d. gauge d \<and>
  2184       (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  2185         norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
  2186   unfolding has_integral_def has_integral_compact_interval_def
  2187   by auto
  2188 
  2189 lemma has_integral_real:
  2190   "(f has_integral y) {a .. b::real} \<longleftrightarrow>
  2191     (\<forall>e>0. \<exists>d. gauge d \<and>
  2192       (\<forall>p. p tagged_division_of {a .. b} \<and> d fine p \<longrightarrow>
  2193         norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
  2194   unfolding box_real[symmetric]
  2195   by (rule has_integral)
  2196 
  2197 lemma has_integralD[dest]:
  2198   assumes "(f has_integral y) (cbox a b)"
  2199     and "e > 0"
  2200   obtains d where "gauge d"
  2201     and "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d fine p \<Longrightarrow>
  2202       norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f(x)) p - y) < e"
  2203   using assms unfolding has_integral by auto
  2204 
  2205 lemma has_integral_alt:
  2206   "(f has_integral y) i \<longleftrightarrow>
  2207     (if \<exists>a b. i = cbox a b
  2208      then (f has_integral y) i
  2209      else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  2210       (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e)))"
  2211   unfolding has_integral
  2212   unfolding has_integral_compact_interval_def has_integral_def
  2213   by auto
  2214 
  2215 lemma has_integral_altD:
  2216   assumes "(f has_integral y) i"
  2217     and "\<not> (\<exists>a b. i = cbox a b)"
  2218     and "e>0"
  2219   obtains B where "B > 0"
  2220     and "\<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  2221       (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) (cbox a b) \<and> norm(z - y) < e)"
  2222   using assms
  2223   unfolding has_integral
  2224   unfolding has_integral_compact_interval_def has_integral_def
  2225   by auto
  2226 
  2227 definition integrable_on (infixr "integrable'_on" 46)
  2228   where "f integrable_on i \<longleftrightarrow> (\<exists>y. (f has_integral y) i)"
  2229 
  2230 definition "integral i f = (SOME y. (f has_integral y) i \<or> ~ f integrable_on i \<and> y=0)"
  2231 
  2232 lemma integrable_integral[dest]: "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
  2233   unfolding integrable_on_def integral_def by (metis (mono_tags, lifting) someI_ex)
  2234 
  2235 lemma not_integrable_integral: "~ f integrable_on i \<Longrightarrow> integral i f = 0"
  2236   unfolding integrable_on_def integral_def by blast
  2237 
  2238 lemma has_integral_integrable[intro]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
  2239   unfolding integrable_on_def by auto
  2240 
  2241 lemma has_integral_integral: "f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
  2242   by auto
  2243 
  2244 lemma setsum_content_null:
  2245   assumes "content (cbox a b) = 0"
  2246     and "p tagged_division_of (cbox a b)"
  2247   shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
  2248 proof (rule setsum.neutral, rule)
  2249   fix y
  2250   assume y: "y \<in> p"
  2251   obtain x k where xk: "y = (x, k)"
  2252     using surj_pair[of y] by blast
  2253   note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
  2254   from this(2) obtain c d where k: "k = cbox c d" by blast
  2255   have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x"
  2256     unfolding xk by auto
  2257   also have "\<dots> = 0"
  2258     using content_subset[OF assm(1)[unfolded k]] content_pos_le[of c d]
  2259     unfolding assms(1) k
  2260     by auto
  2261   finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
  2262 qed
  2263 
  2264 
  2265 subsection \<open>Some basic combining lemmas.\<close>
  2266 
  2267 lemma tagged_division_unions_exists:
  2268   assumes "finite iset"
  2269     and "\<forall>i\<in>iset. \<exists>p. p tagged_division_of i \<and> d fine p"
  2270     and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior i1 \<inter> interior i2 = {}"
  2271     and "\<Union>iset = i"
  2272    obtains p where "p tagged_division_of i" and "d fine p"
  2273 proof -
  2274   obtain pfn where pfn:
  2275     "\<And>x. x \<in> iset \<Longrightarrow> pfn x tagged_division_of x"
  2276     "\<And>x. x \<in> iset \<Longrightarrow> d fine pfn x"
  2277     using bchoice[OF assms(2)] by auto
  2278   show thesis
  2279     apply (rule_tac p="\<Union>(pfn ` iset)" in that)
  2280     using assms(1) assms(3) assms(4) pfn(1) tagged_division_unions apply force
  2281     by (metis (mono_tags, lifting) fine_unions imageE pfn(2))
  2282 qed
  2283 
  2284 
  2285 subsection \<open>The set we're concerned with must be closed.\<close>
  2286 
  2287 lemma division_of_closed:
  2288   fixes i :: "'n::euclidean_space set"
  2289   shows "s division_of i \<Longrightarrow> closed i"
  2290   unfolding division_of_def by fastforce
  2291 
  2292 subsection \<open>General bisection principle for intervals; might be useful elsewhere.\<close>
  2293 
  2294 lemma interval_bisection_step:
  2295   fixes type :: "'a::euclidean_space"
  2296   assumes "P {}"
  2297     and "\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P (s \<union> t)"
  2298     and "\<not> P (cbox a (b::'a))"
  2299   obtains c d where "\<not> P (cbox c d)"
  2300     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"
  2301 proof -
  2302   have "cbox a b \<noteq> {}"
  2303     using assms(1,3) by metis
  2304   then have ab: "\<And>i. i\<in>Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i"
  2305     by (force simp: mem_box)
  2306   { fix f
  2307     have "\<lbrakk>finite f;
  2308            \<And>s. s\<in>f \<Longrightarrow> P s;
  2309            \<And>s. s\<in>f \<Longrightarrow> \<exists>a b. s = cbox a b;
  2310            \<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)"
  2311     proof (induct f rule: finite_induct)
  2312       case empty
  2313       show ?case
  2314         using assms(1) by auto
  2315     next
  2316       case (insert x f)
  2317       show ?case
  2318         unfolding Union_insert
  2319         apply (rule assms(2)[rule_format])
  2320         using inter_interior_unions_intervals [of f "interior x"]
  2321         apply (auto simp: insert)
  2322         by (metis IntI empty_iff insert.hyps(2) insert.prems(3) insert_iff)
  2323     qed
  2324   } note UN_cases = this
  2325   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>
  2326     (c\<bullet>i = (a\<bullet>i + b\<bullet>i) / 2) \<and> (d\<bullet>i = b\<bullet>i)}"
  2327   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"
  2328   {
  2329     presume "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d) \<Longrightarrow> False"
  2330     then show thesis
  2331       unfolding atomize_not not_all
  2332       by (blast intro: that)
  2333   }
  2334   assume as: "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d)"
  2335   have "P (\<Union>?A)"
  2336   proof (rule UN_cases)
  2337     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)
  2338       (\<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}"
  2339     have "?A \<subseteq> ?B"
  2340     proof
  2341       fix x
  2342       assume "x \<in> ?A"
  2343       then obtain c d
  2344         where x:  "x = cbox c d"
  2345                   "\<And>i. i \<in> Basis \<Longrightarrow>
  2346                         c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  2347                         c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i" by blast
  2348       show "x \<in> ?B"
  2349         unfolding image_iff x
  2350         apply (rule_tac x="{i. i\<in>Basis \<and> c\<bullet>i = a\<bullet>i}" in bexI)
  2351         apply (rule arg_cong2 [where f = cbox])
  2352         using x(2) ab
  2353         apply (auto simp add: euclidean_eq_iff[where 'a='a])
  2354         by fastforce
  2355     qed
  2356     then show "finite ?A"
  2357       by (rule finite_subset) auto
  2358   next
  2359     fix s
  2360     assume "s \<in> ?A"
  2361     then obtain c d
  2362       where s: "s = cbox c d"
  2363                "\<And>i. i \<in> Basis \<Longrightarrow>
  2364                      c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  2365                      c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  2366       by blast
  2367     show "P s"
  2368       unfolding s
  2369       apply (rule as[rule_format])
  2370       using ab s(2) by force
  2371     show "\<exists>a b. s = cbox a b"
  2372       unfolding s by auto
  2373     fix t
  2374     assume "t \<in> ?A"
  2375     then obtain e f where t:
  2376       "t = cbox e f"
  2377       "\<And>i. i \<in> Basis \<Longrightarrow>
  2378         e \<bullet> i = a \<bullet> i \<and> f \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  2379         e \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> f \<bullet> i = b \<bullet> i"
  2380       by blast
  2381     assume "s \<noteq> t"
  2382     then have "\<not> (c = e \<and> d = f)"
  2383       unfolding s t by auto
  2384     then obtain i where "c\<bullet>i \<noteq> e\<bullet>i \<or> d\<bullet>i \<noteq> f\<bullet>i" and i': "i \<in> Basis"
  2385       unfolding euclidean_eq_iff[where 'a='a] by auto
  2386     then have i: "c\<bullet>i \<noteq> e\<bullet>i" "d\<bullet>i \<noteq> f\<bullet>i"
  2387       using s(2) t(2) apply fastforce
  2388       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
  2389     have *: "\<And>s t. (\<And>a. a \<in> s \<Longrightarrow> a \<in> t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}"
  2390       by auto
  2391     show "interior s \<inter> interior t = {}"
  2392       unfolding s t interior_cbox
  2393     proof (rule *)
  2394       fix x
  2395       assume "x \<in> box c d" "x \<in> box e f"
  2396       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"
  2397         unfolding mem_box using i'
  2398         by force+
  2399       show False  using s(2)[OF i']
  2400       proof safe
  2401         assume as: "c \<bullet> i = a \<bullet> i" "d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2"
  2402         show False
  2403           using t(2)[OF i'] and i x unfolding as by (fastforce simp add:field_simps)
  2404       next
  2405         assume as: "c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2" "d \<bullet> i = b \<bullet> i"
  2406         show False
  2407           using t(2)[OF i'] and i x unfolding as by(fastforce simp add:field_simps)
  2408       qed
  2409     qed
  2410   qed
  2411   also have "\<Union>?A = cbox a b"
  2412   proof (rule set_eqI,rule)
  2413     fix x
  2414     assume "x \<in> \<Union>?A"
  2415     then obtain c d where x:
  2416       "x \<in> cbox c d"
  2417       "\<And>i. i \<in> Basis \<Longrightarrow>
  2418         c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  2419         c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  2420       by blast
  2421     show "x\<in>cbox a b"
  2422       unfolding mem_box
  2423     proof safe
  2424       fix i :: 'a
  2425       assume i: "i \<in> Basis"
  2426       then show "a \<bullet> i \<le> x \<bullet> i" "x \<bullet> i \<le> b \<bullet> i"
  2427         using x(2)[OF i] x(1)[unfolded mem_box,THEN bspec, OF i] by auto
  2428     qed
  2429   next
  2430     fix x
  2431     assume x: "x \<in> cbox a b"
  2432     have "\<forall>i\<in>Basis.
  2433       \<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"
  2434       (is "\<forall>i\<in>Basis. \<exists>c d. ?P i c d")
  2435       unfolding mem_box
  2436     proof
  2437       fix i :: 'a
  2438       assume i: "i \<in> Basis"
  2439       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)"
  2440         using x[unfolded mem_box,THEN bspec, OF i] by auto
  2441       then show "\<exists>c d. ?P i c d"
  2442         by blast
  2443     qed
  2444     then show "x\<in>\<Union>?A"
  2445       unfolding Union_iff Bex_def mem_Collect_eq choice_Basis_iff
  2446       apply auto
  2447       apply (rule_tac x="cbox xa xaa" in exI)
  2448       unfolding mem_box
  2449       apply auto
  2450       done
  2451   qed
  2452   finally show False
  2453     using assms by auto
  2454 qed
  2455 
  2456 lemma interval_bisection:
  2457   fixes type :: "'a::euclidean_space"
  2458   assumes "P {}"
  2459     and "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))"
  2460     and "\<not> P (cbox a (b::'a))"
  2461   obtains x where "x \<in> cbox a b"
  2462     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)"
  2463 proof -
  2464   have "\<forall>x. \<exists>y. \<not> P (cbox (fst x) (snd x)) \<longrightarrow> (\<not> P (cbox (fst y) (snd y)) \<and>
  2465     (\<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>
  2466        2 * (snd y\<bullet>i - fst y\<bullet>i) \<le> snd x\<bullet>i - fst x\<bullet>i))" (is "\<forall>x. ?P x")
  2467   proof
  2468     show "?P x" for x
  2469     proof (cases "P (cbox (fst x) (snd x))")
  2470       case True
  2471       then show ?thesis by auto
  2472     next
  2473       case as: False
  2474       obtain c d where "\<not> P (cbox c d)"
  2475         "\<forall>i\<in>Basis.
  2476            fst x \<bullet> i \<le> c \<bullet> i \<and>
  2477            c \<bullet> i \<le> d \<bullet> i \<and>
  2478            d \<bullet> i \<le> snd x \<bullet> i \<and>
  2479            2 * (d \<bullet> i - c \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i"
  2480         by (rule interval_bisection_step[of P, OF assms(1-2) as])
  2481       then show ?thesis
  2482         apply -
  2483         apply (rule_tac x="(c,d)" in exI)
  2484         apply auto
  2485         done
  2486     qed
  2487   qed
  2488   then obtain f where f:
  2489     "\<forall>x.
  2490       \<not> P (cbox (fst x) (snd x)) \<longrightarrow>
  2491       \<not> P (cbox (fst (f x)) (snd (f x))) \<and>
  2492         (\<forall>i\<in>Basis.
  2493             fst x \<bullet> i \<le> fst (f x) \<bullet> i \<and>
  2494             fst (f x) \<bullet> i \<le> snd (f x) \<bullet> i \<and>
  2495             snd (f x) \<bullet> i \<le> snd x \<bullet> i \<and>
  2496             2 * (snd (f x) \<bullet> i - fst (f x) \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i)"
  2497     apply -
  2498     apply (drule choice)
  2499     apply blast
  2500     done
  2501   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
  2502   have "A 0 = a" "B 0 = b" "\<And>n. \<not> P (cbox (A(Suc n)) (B(Suc n))) \<and>
  2503     (\<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>
  2504     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")
  2505   proof -
  2506     show "A 0 = a" "B 0 = b"
  2507       unfolding ab_def by auto
  2508     note S = ab_def funpow.simps o_def id_apply
  2509     show "?P n" for n
  2510     proof (induct n)
  2511       case 0
  2512       then show ?case
  2513         unfolding S
  2514         apply (rule f[rule_format]) using assms(3)
  2515         apply auto
  2516         done
  2517     next
  2518       case (Suc n)
  2519       show ?case
  2520         unfolding S
  2521         apply (rule f[rule_format])
  2522         using Suc
  2523         unfolding S
  2524         apply auto
  2525         done
  2526     qed
  2527   qed
  2528   note AB = this(1-2) conjunctD2[OF this(3),rule_format]
  2529 
  2530   have interv: "\<exists>n. \<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e"
  2531     if e: "0 < e" for e
  2532   proof -
  2533     obtain n where n: "(\<Sum>i\<in>Basis. b \<bullet> i - a \<bullet> i) / e < 2 ^ n"
  2534       using real_arch_pow[of 2 "(setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis) / e"] by auto
  2535     show ?thesis
  2536     proof (rule exI [where x=n], clarify)
  2537       fix x y
  2538       assume xy: "x\<in>cbox (A n) (B n)" "y\<in>cbox (A n) (B n)"
  2539       have "dist x y \<le> setsum (\<lambda>i. \<bar>(x - y)\<bullet>i\<bar>) Basis"
  2540         unfolding dist_norm by(rule norm_le_l1)
  2541       also have "\<dots> \<le> setsum (\<lambda>i. B n\<bullet>i - A n\<bullet>i) Basis"
  2542       proof (rule setsum_mono)
  2543         fix i :: 'a
  2544         assume i: "i \<in> Basis"
  2545         show "\<bar>(x - y) \<bullet> i\<bar> \<le> B n \<bullet> i - A n \<bullet> i"
  2546           using xy[unfolded mem_box,THEN bspec, OF i]
  2547           by (auto simp: inner_diff_left)
  2548       qed
  2549       also have "\<dots> \<le> setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis / 2^n"
  2550         unfolding setsum_divide_distrib
  2551       proof (rule setsum_mono)
  2552         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
  2553         proof (induct n)
  2554           case 0
  2555           then show ?case
  2556             unfolding AB by auto
  2557         next
  2558           case (Suc n)
  2559           have "B (Suc n) \<bullet> i - A (Suc n) \<bullet> i \<le> (B n \<bullet> i - A n \<bullet> i) / 2"
  2560             using AB(4)[of i n] using i by auto
  2561           also have "\<dots> \<le> (b \<bullet> i - a \<bullet> i) / 2 ^ Suc n"
  2562             using Suc by (auto simp add: field_simps)
  2563           finally show ?case .
  2564         qed
  2565       qed
  2566       also have "\<dots> < e"
  2567         using n using e by (auto simp add: field_simps)
  2568       finally show "dist x y < e" .
  2569     qed
  2570   qed
  2571   {
  2572     fix n m :: nat
  2573     assume "m \<le> n" then have "cbox (A n) (B n) \<subseteq> cbox (A m) (B m)"
  2574     proof (induction rule: inc_induct)
  2575       case (step i)
  2576       show ?case
  2577         using AB(4) by (intro order_trans[OF step.IH] subset_box_imp) auto
  2578     qed simp
  2579   } note ABsubset = this
  2580   have "\<exists>a. \<forall>n. a\<in> cbox (A n) (B n)"
  2581     by (rule decreasing_closed_nest[rule_format,OF closed_cbox _ ABsubset interv])
  2582       (metis nat.exhaust AB(1-3) assms(1,3))
  2583   then obtain x0 where x0: "\<And>n. x0 \<in> cbox (A n) (B n)"
  2584     by blast
  2585   show thesis
  2586   proof (rule that[rule_format, of x0])
  2587     show "x0\<in>cbox a b"
  2588       using x0[of 0] unfolding AB .
  2589     fix e :: real
  2590     assume "e > 0"
  2591     from interv[OF this] obtain n
  2592       where n: "\<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e" ..
  2593     have "\<not> P (cbox (A n) (B n))"
  2594       apply (cases "0 < n")
  2595       using AB(3)[of "n - 1"] assms(3) AB(1-2)
  2596       apply auto
  2597       done
  2598     moreover have "cbox (A n) (B n) \<subseteq> ball x0 e"
  2599       using n using x0[of n] by auto
  2600     moreover have "cbox (A n) (B n) \<subseteq> cbox a b"
  2601       unfolding AB(1-2)[symmetric] by (rule ABsubset) auto
  2602     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)"
  2603       apply (rule_tac x="A n" in exI)
  2604       apply (rule_tac x="B n" in exI)
  2605       apply (auto simp: x0)
  2606       done
  2607   qed
  2608 qed
  2609 
  2610 
  2611 subsection \<open>Cousin's lemma.\<close>
  2612 
  2613 lemma fine_division_exists:
  2614   fixes a b :: "'a::euclidean_space"
  2615   assumes "gauge g"
  2616   obtains p where "p tagged_division_of (cbox a b)" "g fine p"
  2617 proof -
  2618   presume "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p) \<Longrightarrow> False"
  2619   then obtain p where "p tagged_division_of (cbox a b)" "g fine p"
  2620     by blast
  2621   then show thesis ..
  2622 next
  2623   assume as: "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p)"
  2624   obtain x where x:
  2625       "x \<in> (cbox a b)"
  2626       "\<And>e. 0 < e \<Longrightarrow>
  2627         \<exists>c d.
  2628           x \<in> cbox c d \<and>
  2629           cbox c d \<subseteq> ball x e \<and>
  2630           cbox c d \<subseteq> (cbox a b) \<and>
  2631           \<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  2632     apply (rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p", OF _ _ as])
  2633     apply (simp add: fine_def)
  2634     apply (metis tagged_division_union fine_union)
  2635     apply (auto simp: )
  2636     done
  2637   obtain e where e: "e > 0" "ball x e \<subseteq> g x"
  2638     using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
  2639   from x(2)[OF e(1)]
  2640   obtain c d where c_d: "x \<in> cbox c d"
  2641                         "cbox c d \<subseteq> ball x e"
  2642                         "cbox c d \<subseteq> cbox a b"
  2643                         "\<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  2644     by blast
  2645   have "g fine {(x, cbox c d)}"
  2646     unfolding fine_def using e using c_d(2) by auto
  2647   then show False
  2648     using tagged_division_of_self[OF c_d(1)] using c_d by auto
  2649 qed
  2650 
  2651 lemma fine_division_exists_real:
  2652   fixes a b :: real
  2653   assumes "gauge g"
  2654   obtains p where "p tagged_division_of {a .. b}" "g fine p"
  2655   by (metis assms box_real(2) fine_division_exists)
  2656 
  2657 subsection \<open>Basic theorems about integrals.\<close>
  2658 
  2659 lemma has_integral_unique:
  2660   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2661   assumes "(f has_integral k1) i"
  2662     and "(f has_integral k2) i"
  2663   shows "k1 = k2"
  2664 proof (rule ccontr)
  2665   let ?e = "norm (k1 - k2) / 2"
  2666   assume as: "k1 \<noteq> k2"
  2667   then have e: "?e > 0"
  2668     by auto
  2669   have lem: False
  2670     if f_k1: "(f has_integral k1) (cbox a b)"
  2671     and f_k2: "(f has_integral k2) (cbox a b)"
  2672     and "k1 \<noteq> k2"
  2673     for f :: "'n \<Rightarrow> 'a" and a b k1 k2
  2674   proof -
  2675     let ?e = "norm (k1 - k2) / 2"
  2676     from \<open>k1 \<noteq> k2\<close> have e: "?e > 0" by auto
  2677     obtain d1 where d1:
  2678         "gauge d1"
  2679         "\<And>p. p tagged_division_of cbox a b \<Longrightarrow>
  2680           d1 fine p \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k1) < norm (k1 - k2) / 2"
  2681       by (rule has_integralD[OF f_k1 e]) blast
  2682     obtain d2 where d2:
  2683         "gauge d2"
  2684         "\<And>p. p tagged_division_of cbox a b \<Longrightarrow>
  2685           d2 fine p \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k2) < norm (k1 - k2) / 2"
  2686       by (rule has_integralD[OF f_k2 e]) blast
  2687     obtain p where p:
  2688         "p tagged_division_of cbox a b"
  2689         "(\<lambda>x. d1 x \<inter> d2 x) fine p"
  2690       by (rule fine_division_exists[OF gauge_inter[OF d1(1) d2(1)]])
  2691     let ?c = "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)"
  2692     have "norm (k1 - k2) \<le> norm (?c - k2) + norm (?c - k1)"
  2693       using norm_triangle_ineq4[of "k1 - ?c" "k2 - ?c"]
  2694       by (auto simp add:algebra_simps norm_minus_commute)
  2695     also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
  2696       apply (rule add_strict_mono)
  2697       apply (rule_tac[!] d2(2) d1(2))
  2698       using p unfolding fine_def
  2699       apply auto
  2700       done
  2701     finally show False by auto
  2702   qed
  2703   {
  2704     presume "\<not> (\<exists>a b. i = cbox a b) \<Longrightarrow> False"
  2705     then show False
  2706       using as assms lem by blast
  2707   }
  2708   assume as: "\<not> (\<exists>a b. i = cbox a b)"
  2709   obtain B1 where B1:
  2710       "0 < B1"
  2711       "\<And>a b. ball 0 B1 \<subseteq> cbox a b \<Longrightarrow>
  2712         \<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b) \<and>
  2713           norm (z - k1) < norm (k1 - k2) / 2"
  2714     by (rule has_integral_altD[OF assms(1) as,OF e]) blast
  2715   obtain B2 where B2:
  2716       "0 < B2"
  2717       "\<And>a b. ball 0 B2 \<subseteq> cbox a b \<Longrightarrow>
  2718         \<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b) \<and>
  2719           norm (z - k2) < norm (k1 - k2) / 2"
  2720     by (rule has_integral_altD[OF assms(2) as,OF e]) blast
  2721   have "\<exists>a b::'n. ball 0 B1 \<union> ball 0 B2 \<subseteq> cbox a b"
  2722     apply (rule bounded_subset_cbox)
  2723     using bounded_Un bounded_ball
  2724     apply auto
  2725     done
  2726   then obtain a b :: 'n where ab: "ball 0 B1 \<subseteq> cbox a b" "ball 0 B2 \<subseteq> cbox a b"
  2727     by blast
  2728   obtain w where w:
  2729     "((\<lambda>x. if x \<in> i then f x else 0) has_integral w) (cbox a b)"
  2730     "norm (w - k1) < norm (k1 - k2) / 2"
  2731     using B1(2)[OF ab(1)] by blast
  2732   obtain z where z:
  2733     "((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b)"
  2734     "norm (z - k2) < norm (k1 - k2) / 2"
  2735     using B2(2)[OF ab(2)] by blast
  2736   have "z = w"
  2737     using lem[OF w(1) z(1)] by auto
  2738   then have "norm (k1 - k2) \<le> norm (z - k2) + norm (w - k1)"
  2739     using norm_triangle_ineq4 [of "k1 - w" "k2 - z"]
  2740     by (auto simp add: norm_minus_commute)
  2741   also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
  2742     apply (rule add_strict_mono)
  2743     apply (rule_tac[!] z(2) w(2))
  2744     done
  2745   finally show False by auto
  2746 qed
  2747 
  2748 lemma integral_unique [intro]: "(f has_integral y) k \<Longrightarrow> integral k f = y"
  2749   unfolding integral_def
  2750   by (rule some_equality) (auto intro: has_integral_unique)
  2751 
  2752 lemma eq_integralD: "integral k f = y \<Longrightarrow> (f has_integral y) k \<or> ~ f integrable_on k \<and> y=0"
  2753   unfolding integral_def integrable_on_def
  2754   apply (erule subst)
  2755   apply (rule someI_ex)
  2756   by blast
  2757 
  2758 lemma has_integral_is_0:
  2759   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2760   assumes "\<forall>x\<in>s. f x = 0"
  2761   shows "(f has_integral 0) s"
  2762 proof -
  2763   have lem: "\<And>a b. \<And>f::'n \<Rightarrow> 'a.
  2764     (\<forall>x\<in>cbox a b. f(x) = 0) \<Longrightarrow> (f has_integral 0) (cbox a b)"
  2765     unfolding has_integral
  2766   proof clarify
  2767     fix a b e
  2768     fix f :: "'n \<Rightarrow> 'a"
  2769     assume as: "\<forall>x\<in>cbox a b. f x = 0" "0 < (e::real)"
  2770     have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  2771       if p: "p tagged_division_of cbox a b" for p
  2772     proof -
  2773       have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) = 0"
  2774       proof (rule setsum.neutral, rule)
  2775         fix x
  2776         assume x: "x \<in> p"
  2777         have "f (fst x) = 0"
  2778           using tagged_division_ofD(2-3)[OF p, of "fst x" "snd x"] using as x by auto
  2779         then show "(\<lambda>(x, k). content k *\<^sub>R f x) x = 0"
  2780           apply (subst surjective_pairing[of x])
  2781           unfolding split_conv
  2782           apply auto
  2783           done
  2784       qed
  2785       then show ?thesis
  2786         using as by auto
  2787     qed
  2788     then show "\<exists>d. gauge d \<and>
  2789         (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e)"
  2790       by auto
  2791   qed
  2792   {
  2793     presume "\<not> (\<exists>a b. s = cbox a b) \<Longrightarrow> ?thesis"
  2794     with assms lem show ?thesis
  2795       by blast
  2796   }
  2797   have *: "(\<lambda>x. if x \<in> s then f x else 0) = (\<lambda>x. 0)"
  2798     apply (rule ext)
  2799     using assms
  2800     apply auto
  2801     done
  2802   assume "\<not> (\<exists>a b. s = cbox a b)"
  2803   then show ?thesis
  2804     using lem
  2805     by (subst has_integral_alt) (force simp add: *)
  2806 qed
  2807 
  2808 lemma has_integral_0[simp]: "((\<lambda>x::'n::euclidean_space. 0) has_integral 0) s"
  2809   by (rule has_integral_is_0) auto
  2810 
  2811 lemma has_integral_0_eq[simp]: "((\<lambda>x. 0) has_integral i) s \<longleftrightarrow> i = 0"
  2812   using has_integral_unique[OF has_integral_0] by auto
  2813 
  2814 lemma has_integral_linear:
  2815   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2816   assumes "(f has_integral y) s"
  2817     and "bounded_linear h"
  2818   shows "((h \<circ> f) has_integral ((h y))) s"
  2819 proof -
  2820   interpret bounded_linear h
  2821     using assms(2) .
  2822   from pos_bounded obtain B where B: "0 < B" "\<And>x. norm (h x) \<le> norm x * B"
  2823     by blast
  2824   have lem: "\<And>(f :: 'n \<Rightarrow> 'a) y a b.
  2825     (f has_integral y) (cbox a b) \<Longrightarrow> ((h \<circ> f) has_integral h y) (cbox a b)"
  2826     unfolding has_integral
  2827   proof (clarify, goal_cases)
  2828     case prems: (1 f y a b e)
  2829     from pos_bounded
  2830     obtain B where B: "0 < B" "\<And>x. norm (h x) \<le> norm x * B"
  2831       by blast
  2832     have "e / B > 0" using prems(2) B by simp
  2833     then obtain g
  2834       where g: "gauge g"
  2835                "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> g fine p \<Longrightarrow>
  2836                     norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - y) < e / B"
  2837         using prems(1) by auto
  2838     {
  2839       fix p
  2840       assume as: "p tagged_division_of (cbox a b)" "g fine p"
  2841       have hc: "\<And>x k. h ((\<lambda>(x, k). content k *\<^sub>R f x) x) = (\<lambda>(x, k). h (content k *\<^sub>R f x)) x"
  2842         by auto
  2843       then have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = setsum (h \<circ> (\<lambda>(x, k). content k *\<^sub>R f x)) p"
  2844         unfolding o_def unfolding scaleR[symmetric] hc by simp
  2845       also have "\<dots> = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)"
  2846         using setsum[of "\<lambda>(x,k). content k *\<^sub>R f x" p] using as by auto
  2847       finally have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" .
  2848       then have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) - h y) < e"
  2849         apply (simp add: diff[symmetric])
  2850         apply (rule le_less_trans[OF B(2)])
  2851         using g(2)[OF as] B(1)
  2852         apply (auto simp add: field_simps)
  2853         done
  2854     }
  2855     with g show ?case
  2856       by (rule_tac x=g in exI) auto
  2857   qed
  2858   {
  2859     presume "\<not> (\<exists>a b. s = cbox a b) \<Longrightarrow> ?thesis"
  2860     then show ?thesis
  2861       using assms(1) lem by blast
  2862   }
  2863   assume as: "\<not> (\<exists>a b. s = cbox a b)"
  2864   then show ?thesis
  2865   proof (subst has_integral_alt, clarsimp)
  2866     fix e :: real
  2867     assume e: "e > 0"
  2868     have *: "0 < e/B" using e B(1) by simp
  2869     obtain M where M:
  2870       "M > 0"
  2871       "\<And>a b. ball 0 M \<subseteq> cbox a b \<Longrightarrow>
  2872         \<exists>z. ((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e / B"
  2873       using has_integral_altD[OF assms(1) as *] by blast
  2874     show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  2875       (\<exists>z. ((\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) has_integral z) (cbox a b) \<and> norm (z - h y) < e)"
  2876     proof (rule_tac x=M in exI, clarsimp simp add: M, goal_cases)
  2877       case prems: (1 a b)
  2878       obtain z where z:
  2879         "((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b)"
  2880         "norm (z - y) < e / B"
  2881         using M(2)[OF prems(1)] by blast
  2882       have *: "(\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) = h \<circ> (\<lambda>x. if x \<in> s then f x else 0)"
  2883         using zero by auto
  2884       show ?case
  2885         apply (rule_tac x="h z" in exI)
  2886         apply (simp add: * lem z(1))
  2887         apply (metis B diff le_less_trans pos_less_divide_eq z(2))
  2888         done
  2889     qed
  2890   qed
  2891 qed
  2892 
  2893 lemma has_integral_scaleR_left:
  2894   "(f has_integral y) s \<Longrightarrow> ((\<lambda>x. f x *\<^sub>R c) has_integral (y *\<^sub>R c)) s"
  2895   using has_integral_linear[OF _ bounded_linear_scaleR_left] by (simp add: comp_def)
  2896 
  2897 lemma has_integral_mult_left:
  2898   fixes c :: "_ :: real_normed_algebra"
  2899   shows "(f has_integral y) s \<Longrightarrow> ((\<lambda>x. f x * c) has_integral (y * c)) s"
  2900   using has_integral_linear[OF _ bounded_linear_mult_left] by (simp add: comp_def)
  2901 
  2902 text\<open>The case analysis eliminates the condition @{term "f integrable_on s"} at the cost
  2903      of the type class constraint \<open>division_ring\<close>\<close>
  2904 corollary integral_mult_left [simp]:
  2905   fixes c:: "'a::{real_normed_algebra,division_ring}"
  2906   shows "integral s (\<lambda>x. f x * c) = integral s f * c"
  2907 proof (cases "f integrable_on s \<or> c = 0")
  2908   case True then show ?thesis
  2909     by (force intro: has_integral_mult_left)
  2910 next
  2911   case False then have "~ (\<lambda>x. f x * c) integrable_on s"
  2912     using has_integral_mult_left [of "(\<lambda>x. f x * c)" _ s "inverse c"]
  2913     by (force simp add: mult.assoc)
  2914   with False show ?thesis by (simp add: not_integrable_integral)
  2915 qed
  2916 
  2917 corollary integral_mult_right [simp]:
  2918   fixes c:: "'a::{real_normed_field}"
  2919   shows "integral s (\<lambda>x. c * f x) = c * integral s f"
  2920 by (simp add: mult.commute [of c])
  2921 
  2922 corollary integral_divide [simp]:
  2923   fixes z :: "'a::real_normed_field"
  2924   shows "integral S (\<lambda>x. f x / z) = integral S (\<lambda>x. f x) / z"
  2925 using integral_mult_left [of S f "inverse z"]
  2926   by (simp add: divide_inverse_commute)
  2927 
  2928 lemma has_integral_mult_right:
  2929   fixes c :: "'a :: real_normed_algebra"
  2930   shows "(f has_integral y) i \<Longrightarrow> ((\<lambda>x. c * f x) has_integral (c * y)) i"
  2931   using has_integral_linear[OF _ bounded_linear_mult_right] by (simp add: comp_def)
  2932 
  2933 lemma has_integral_cmul: "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_integral (c *\<^sub>R k)) s"
  2934   unfolding o_def[symmetric]
  2935   by (metis has_integral_linear bounded_linear_scaleR_right)
  2936 
  2937 lemma has_integral_cmult_real:
  2938   fixes c :: real
  2939   assumes "c \<noteq> 0 \<Longrightarrow> (f has_integral x) A"
  2940   shows "((\<lambda>x. c * f x) has_integral c * x) A"
  2941 proof (cases "c = 0")
  2942   case True
  2943   then show ?thesis by simp
  2944 next
  2945   case False
  2946   from has_integral_cmul[OF assms[OF this], of c] show ?thesis
  2947     unfolding real_scaleR_def .
  2948 qed
  2949 
  2950 lemma has_integral_neg: "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. -(f x)) has_integral -k) s"
  2951   by (drule_tac c="-1" in has_integral_cmul) auto
  2952 
  2953 lemma has_integral_add:
  2954   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2955   assumes "(f has_integral k) s"
  2956     and "(g has_integral l) s"
  2957   shows "((\<lambda>x. f x + g x) has_integral (k + l)) s"
  2958 proof -
  2959   have lem: "((\<lambda>x. f x + g x) has_integral (k + l)) (cbox a b)"
  2960     if f_k: "(f has_integral k) (cbox a b)"
  2961     and g_l: "(g has_integral l) (cbox a b)"
  2962     for f :: "'n \<Rightarrow> 'a" and g a b k l
  2963     unfolding has_integral
  2964   proof clarify
  2965     fix e :: real
  2966     assume e: "e > 0"
  2967     then have *: "e / 2 > 0"
  2968       by auto
  2969     obtain d1 where d1:
  2970       "gauge d1"
  2971       "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d1 fine p \<Longrightarrow>
  2972         norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k) < e / 2"
  2973       using has_integralD[OF f_k *] by blast
  2974     obtain d2 where d2:
  2975       "gauge d2"
  2976       "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d2 fine p \<Longrightarrow>
  2977         norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - l) < e / 2"
  2978       using has_integralD[OF g_l *] by blast
  2979     show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  2980               norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e)"
  2981     proof (rule exI [where x="\<lambda>x. (d1 x) \<inter> (d2 x)"], clarsimp simp add: gauge_inter[OF d1(1) d2(1)])
  2982       fix p
  2983       assume as: "p tagged_division_of (cbox a b)" "(\<lambda>x. d1 x \<inter> d2 x) fine p"
  2984       have *: "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) =
  2985         (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p. content k *\<^sub>R g x)"
  2986         unfolding scaleR_right_distrib setsum.distrib[of "\<lambda>(x,k). content k *\<^sub>R f x" "\<lambda>(x,k). content k *\<^sub>R g x" p,symmetric]
  2987         by (rule setsum.cong) auto
  2988       from as have fine: "d1 fine p" "d2 fine p"
  2989         unfolding fine_inter by auto
  2990       have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) =
  2991             norm (((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k) + ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - l))"
  2992         unfolding * by (auto simp add: algebra_simps)
  2993       also have "\<dots> < e/2 + e/2"
  2994         apply (rule le_less_trans[OF norm_triangle_ineq])
  2995         using as d1 d2 fine
  2996         apply (blast intro: add_strict_mono)
  2997         done
  2998       finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e"
  2999         by auto
  3000     qed
  3001   qed
  3002   {
  3003     presume "\<not> (\<exists>a b. s = cbox a b) \<Longrightarrow> ?thesis"
  3004     then show ?thesis
  3005       using assms lem by force
  3006   }
  3007   assume as: "\<not> (\<exists>a b. s = cbox a b)"
  3008   then show ?thesis
  3009   proof (subst has_integral_alt, clarsimp, goal_cases)
  3010     case (1 e)
  3011     then have *: "e / 2 > 0"
  3012       by auto
  3013     from has_integral_altD[OF assms(1) as *]
  3014     obtain B1 where B1:
  3015         "0 < B1"
  3016         "\<And>a b. ball 0 B1 \<subseteq> cbox a b \<Longrightarrow>
  3017           \<exists>z. ((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b) \<and> norm (z - k) < e / 2"
  3018       by blast
  3019     from has_integral_altD[OF assms(2) as *]
  3020     obtain B2 where B2:
  3021         "0 < B2"
  3022         "\<And>a b. ball 0 B2 \<subseteq> (cbox a b) \<Longrightarrow>
  3023           \<exists>z. ((\<lambda>x. if x \<in> s then g x else 0) has_integral z) (cbox a b) \<and> norm (z - l) < e / 2"
  3024       by blast
  3025     show ?case
  3026     proof (rule_tac x="max B1 B2" in exI, clarsimp simp add: max.strict_coboundedI1 B1)
  3027       fix a b
  3028       assume "ball 0 (max B1 B2) \<subseteq> cbox a (b::'n)"
  3029       then have *: "ball 0 B1 \<subseteq> cbox a (b::'n)" "ball 0 B2 \<subseteq> cbox a (b::'n)"
  3030         by auto
  3031       obtain w where w:
  3032         "((\<lambda>x. if x \<in> s then f x else 0) has_integral w) (cbox a b)"
  3033         "norm (w - k) < e / 2"
  3034         using B1(2)[OF *(1)] by blast
  3035       obtain z where z:
  3036         "((\<lambda>x. if x \<in> s then g x else 0) has_integral z) (cbox a b)"
  3037         "norm (z - l) < e / 2"
  3038         using B2(2)[OF *(2)] by blast
  3039       have *: "\<And>x. (if x \<in> s then f x + g x else 0) =
  3040         (if x \<in> s then f x else 0) + (if x \<in> s then g x else 0)"
  3041         by auto
  3042       show "\<exists>z. ((\<lambda>x. if x \<in> s then f x + g x else 0) has_integral z) (cbox a b) \<and> norm (z - (k + l)) < e"
  3043         apply (rule_tac x="w + z" in exI)
  3044         apply (simp add: lem[OF w(1) z(1), unfolded *[symmetric]])
  3045         using norm_triangle_ineq[of "w - k" "z - l"] w(2) z(2)
  3046         apply (auto simp add: field_simps)
  3047         done
  3048     qed
  3049   qed
  3050 qed
  3051 
  3052 lemma has_integral_sub:
  3053   "(f has_integral k) s \<Longrightarrow> (g has_integral l) s \<Longrightarrow>
  3054     ((\<lambda>x. f x - g x) has_integral (k - l)) s"
  3055   using has_integral_add[OF _ has_integral_neg, of f k s g l]
  3056   by (auto simp: algebra_simps)
  3057 
  3058 lemma integral_0 [simp]:
  3059   "integral s (\<lambda>x::'n::euclidean_space. 0::'m::real_normed_vector) = 0"
  3060   by (rule integral_unique has_integral_0)+
  3061 
  3062 lemma integral_add: "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
  3063     integral s (\<lambda>x. f x + g x) = integral s f + integral s g"
  3064   by (rule integral_unique) (metis integrable_integral has_integral_add)
  3065 
  3066 lemma integral_cmul [simp]: "integral s (\<lambda>x. c *\<^sub>R f x) = c *\<^sub>R integral s f"
  3067 proof (cases "f integrable_on s \<or> c = 0")
  3068   case True with has_integral_cmul show ?thesis by force
  3069 next
  3070   case False then have "~ (\<lambda>x. c *\<^sub>R f x) integrable_on s"
  3071     using has_integral_cmul [of "(\<lambda>x. c *\<^sub>R f x)" _ s "inverse c"]
  3072     by force
  3073   with False show ?thesis by (simp add: not_integrable_integral)
  3074 qed
  3075 
  3076 lemma integral_neg [simp]: "integral s (\<lambda>x. - f x) = - integral s f"
  3077 proof (cases "f integrable_on s")
  3078   case True then show ?thesis
  3079     by (simp add: has_integral_neg integrable_integral integral_unique)
  3080 next
  3081   case False then have "~ (\<lambda>x. - f x) integrable_on s"
  3082     using has_integral_neg [of "(\<lambda>x. - f x)" _ s ]
  3083     by force
  3084   with False show ?thesis by (simp add: not_integrable_integral)
  3085 qed
  3086 
  3087 lemma integral_diff: "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
  3088     integral s (\<lambda>x. f x - g x) = integral s f - integral s g"
  3089   by (rule integral_unique) (metis integrable_integral has_integral_sub)
  3090 
  3091 lemma integrable_0: "(\<lambda>x. 0) integrable_on s"
  3092   unfolding integrable_on_def using has_integral_0 by auto
  3093 
  3094 lemma integrable_add: "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x + g x) integrable_on s"
  3095   unfolding integrable_on_def by(auto intro: has_integral_add)
  3096 
  3097 lemma integrable_cmul: "f integrable_on s \<Longrightarrow> (\<lambda>x. c *\<^sub>R f(x)) integrable_on s"
  3098   unfolding integrable_on_def by(auto intro: has_integral_cmul)
  3099 
  3100 lemma integrable_on_cmult_iff:
  3101   fixes c :: real
  3102   assumes "c \<noteq> 0"
  3103   shows "(\<lambda>x. c * f x) integrable_on s \<longleftrightarrow> f integrable_on s"
  3104   using integrable_cmul[of "\<lambda>x. c * f x" s "1 / c"] integrable_cmul[of f s c] \<open>c \<noteq> 0\<close>
  3105   by auto
  3106 
  3107 lemma integrable_on_cmult_left:
  3108   assumes "f integrable_on s"
  3109   shows "(\<lambda>x. of_real c * f x) integrable_on s"
  3110     using integrable_cmul[of f s "of_real c"] assms
  3111     by (simp add: scaleR_conv_of_real)
  3112 
  3113 lemma integrable_neg: "f integrable_on s \<Longrightarrow> (\<lambda>x. -f(x)) integrable_on s"
  3114   unfolding integrable_on_def by(auto intro: has_integral_neg)
  3115 
  3116 lemma integrable_diff:
  3117   "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x - g x) integrable_on s"
  3118   unfolding integrable_on_def by(auto intro: has_integral_sub)
  3119 
  3120 lemma integrable_linear:
  3121   "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> (h \<circ> f) integrable_on s"
  3122   unfolding integrable_on_def by(auto intro: has_integral_linear)
  3123 
  3124 lemma integral_linear:
  3125   "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> integral s (h \<circ> f) = h (integral s f)"
  3126   apply (rule has_integral_unique [where i=s and f = "h \<circ> f"])
  3127   apply (simp_all add: integrable_integral integrable_linear has_integral_linear )
  3128   done
  3129 
  3130 lemma integral_component_eq[simp]:
  3131   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3132   assumes "f integrable_on s"
  3133   shows "integral s (\<lambda>x. f x \<bullet> k) = integral s f \<bullet> k"
  3134   unfolding integral_linear[OF assms(1) bounded_linear_component,unfolded o_def] ..
  3135 
  3136 lemma has_integral_setsum:
  3137   assumes "finite t"
  3138     and "\<forall>a\<in>t. ((f a) has_integral (i a)) s"
  3139   shows "((\<lambda>x. setsum (\<lambda>a. f a x) t) has_integral (setsum i t)) s"
  3140   using assms(1) subset_refl[of t]
  3141 proof (induct rule: finite_subset_induct)
  3142   case empty
  3143   then show ?case by auto
  3144 next
  3145   case (insert x F)
  3146   with assms show ?case
  3147     by (simp add: has_integral_add)
  3148 qed
  3149 
  3150 lemma integral_setsum:
  3151   "\<lbrakk>finite t;  \<forall>a\<in>t. (f a) integrable_on s\<rbrakk> \<Longrightarrow>
  3152    integral s (\<lambda>x. setsum (\<lambda>a. f a x) t) = setsum (\<lambda>a. integral s (f a)) t"
  3153   by (auto intro: has_integral_setsum integrable_integral)
  3154 
  3155 lemma integrable_setsum:
  3156   "\<lbrakk>finite t;  \<forall>a\<in>t. (f a) integrable_on s\<rbrakk> \<Longrightarrow> (\<lambda>x. setsum (\<lambda>a. f a x) t) integrable_on s"
  3157   unfolding integrable_on_def
  3158   apply (drule bchoice)
  3159   using has_integral_setsum[of t]
  3160   apply auto
  3161   done
  3162 
  3163 lemma has_integral_eq:
  3164   assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  3165     and "(f has_integral k) s"
  3166   shows "(g has_integral k) s"
  3167   using has_integral_sub[OF assms(2), of "\<lambda>x. f x - g x" 0]
  3168   using has_integral_is_0[of s "\<lambda>x. f x - g x"]
  3169   using assms(1)
  3170   by auto
  3171 
  3172 lemma integrable_eq: "(\<And>x. x \<in> s \<Longrightarrow> f x = g x) \<Longrightarrow> f integrable_on s \<Longrightarrow> g integrable_on s"
  3173   unfolding integrable_on_def
  3174   using has_integral_eq[of s f g] has_integral_eq by blast
  3175 
  3176 lemma has_integral_cong:
  3177   assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  3178   shows "(f has_integral i) s = (g has_integral i) s"
  3179   using has_integral_eq[of s f g] has_integral_eq[of s g f] assms
  3180   by auto
  3181 
  3182 lemma integral_cong:
  3183   assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  3184   shows "integral s f = integral s g"
  3185   unfolding integral_def
  3186 by (metis (full_types, hide_lams) assms has_integral_cong integrable_eq)
  3187 
  3188 lemma integrable_on_cmult_left_iff [simp]:
  3189   assumes "c \<noteq> 0"
  3190   shows "(\<lambda>x. of_real c * f x) integrable_on s \<longleftrightarrow> f integrable_on s"
  3191         (is "?lhs = ?rhs")
  3192 proof
  3193   assume ?lhs
  3194   then have "(\<lambda>x. of_real (1 / c) * (of_real c * f x)) integrable_on s"
  3195     using integrable_cmul[of "\<lambda>x. of_real c * f x" s "1 / of_real c"]
  3196     by (simp add: scaleR_conv_of_real)
  3197   then have "(\<lambda>x. (of_real (1 / c) * of_real c * f x)) integrable_on s"
  3198     by (simp add: algebra_simps)
  3199   with \<open>c \<noteq> 0\<close> show ?rhs
  3200     by (metis (no_types, lifting) integrable_eq mult.left_neutral nonzero_divide_eq_eq of_real_1 of_real_mult)
  3201 qed (blast intro: integrable_on_cmult_left)
  3202 
  3203 lemma integrable_on_cmult_right:
  3204   fixes f :: "_ \<Rightarrow> 'b :: {comm_ring,real_algebra_1,real_normed_vector}"
  3205   assumes "f integrable_on s"
  3206   shows "(\<lambda>x. f x * of_real c) integrable_on s"
  3207 using integrable_on_cmult_left [OF assms] by (simp add: mult.commute)
  3208 
  3209 lemma integrable_on_cmult_right_iff [simp]:
  3210   fixes f :: "_ \<Rightarrow> 'b :: {comm_ring,real_algebra_1,real_normed_vector}"
  3211   assumes "c \<noteq> 0"
  3212   shows "(\<lambda>x. f x * of_real c) integrable_on s \<longleftrightarrow> f integrable_on s"
  3213 using integrable_on_cmult_left_iff [OF assms] by (simp add: mult.commute)
  3214 
  3215 lemma integrable_on_cdivide:
  3216   fixes f :: "_ \<Rightarrow> 'b :: real_normed_field"
  3217   assumes "f integrable_on s"
  3218   shows "(\<lambda>x. f x / of_real c) integrable_on s"
  3219 by (simp add: integrable_on_cmult_right divide_inverse assms of_real_inverse [symmetric] del: of_real_inverse)
  3220 
  3221 lemma integrable_on_cdivide_iff [simp]:
  3222   fixes f :: "_ \<Rightarrow> 'b :: real_normed_field"
  3223   assumes "c \<noteq> 0"
  3224   shows "(\<lambda>x. f x / of_real c) integrable_on s \<longleftrightarrow> f integrable_on s"
  3225 by (simp add: divide_inverse assms of_real_inverse [symmetric] del: of_real_inverse)
  3226 
  3227 lemma has_integral_null [intro]:
  3228   assumes "content(cbox a b) = 0"
  3229   shows "(f has_integral 0) (cbox a b)"
  3230 proof -
  3231   have "gauge (\<lambda>x. ball x 1)"
  3232     by auto
  3233   moreover
  3234   {
  3235     fix e :: real
  3236     fix p
  3237     assume e: "e > 0"
  3238     assume p: "p tagged_division_of (cbox a b)"
  3239     have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) = 0"
  3240       unfolding norm_eq_zero diff_0_right
  3241       using setsum_content_null[OF assms(1) p, of f] .
  3242     then have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  3243       using e by auto
  3244   }
  3245   ultimately show ?thesis
  3246     by (auto simp: has_integral)
  3247 qed
  3248 
  3249 lemma has_integral_null_real [intro]:
  3250   assumes "content {a .. b::real} = 0"
  3251   shows "(f has_integral 0) {a .. b}"
  3252   by (metis assms box_real(2) has_integral_null)
  3253 
  3254 lemma has_integral_null_eq[simp]: "content (cbox a b) = 0 \<Longrightarrow> (f has_integral i) (cbox a b) \<longleftrightarrow> i = 0"
  3255   by (auto simp add: has_integral_null dest!: integral_unique)
  3256 
  3257 lemma integral_null [simp]: "content (cbox a b) = 0 \<Longrightarrow> integral (cbox a b) f = 0"
  3258   by (metis has_integral_null integral_unique)
  3259 
  3260 lemma integrable_on_null [intro]: "content (cbox a b) = 0 \<Longrightarrow> f integrable_on (cbox a b)"
  3261   by (simp add: has_integral_integrable)
  3262 
  3263 lemma has_integral_empty[intro]: "(f has_integral 0) {}"
  3264   by (simp add: has_integral_is_0)
  3265 
  3266 lemma has_integral_empty_eq[simp]: "(f has_integral i) {} \<longleftrightarrow> i = 0"
  3267   by (auto simp add: has_integral_empty has_integral_unique)
  3268 
  3269 lemma integrable_on_empty[intro]: "f integrable_on {}"
  3270   unfolding integrable_on_def by auto
  3271 
  3272 lemma integral_empty[simp]: "integral {} f = 0"
  3273   by (rule integral_unique) (rule has_integral_empty)
  3274 
  3275 lemma has_integral_refl[intro]:
  3276   fixes a :: "'a::euclidean_space"
  3277   shows "(f has_integral 0) (cbox a a)"
  3278     and "(f has_integral 0) {a}"
  3279 proof -
  3280   have *: "{a} = cbox a a"
  3281     apply (rule set_eqI)
  3282     unfolding mem_box singleton_iff euclidean_eq_iff[where 'a='a]
  3283     apply safe
  3284     prefer 3
  3285     apply (erule_tac x=b in ballE)
  3286     apply (auto simp add: field_simps)
  3287     done
  3288   show "(f has_integral 0) (cbox a a)" "(f has_integral 0) {a}"
  3289     unfolding *
  3290     apply (rule_tac[!] has_integral_null)
  3291     unfolding content_eq_0_interior
  3292     unfolding interior_cbox
  3293     using box_sing
  3294     apply auto
  3295     done
  3296 qed
  3297 
  3298 lemma integrable_on_refl[intro]: "f integrable_on cbox a a"
  3299   unfolding integrable_on_def by auto
  3300 
  3301 lemma integral_refl [simp]: "integral (cbox a a) f = 0"
  3302   by (rule integral_unique) auto
  3303 
  3304 lemma integral_singleton [simp]: "integral {a} f = 0"
  3305   by auto
  3306 
  3307 lemma integral_blinfun_apply:
  3308   assumes "f integrable_on s"
  3309   shows "integral s (\<lambda>x. blinfun_apply h (f x)) = blinfun_apply h (integral s f)"
  3310   by (subst integral_linear[symmetric, OF assms blinfun.bounded_linear_right]) (simp add: o_def)
  3311 
  3312 lemma blinfun_apply_integral:
  3313   assumes "f integrable_on s"
  3314   shows "blinfun_apply (integral s f) x = integral s (\<lambda>y. blinfun_apply (f y) x)"
  3315   by (metis (no_types, lifting) assms blinfun.prod_left.rep_eq integral_blinfun_apply integral_cong)
  3316 
  3317 lemma has_integral_componentwise_iff:
  3318   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3319   shows "(f has_integral y) A \<longleftrightarrow> (\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
  3320 proof safe
  3321   fix b :: 'b assume "(f has_integral y) A"
  3322   from has_integral_linear[OF this(1) bounded_linear_component, of b]
  3323     show "((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A" by (simp add: o_def)
  3324 next
  3325   assume "(\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
  3326   hence "\<forall>b\<in>Basis. (((\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. f x \<bullet> b)) has_integral ((y \<bullet> b) *\<^sub>R b)) A"
  3327     by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
  3328   hence "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. (y \<bullet> b) *\<^sub>R b)) A"
  3329     by (intro has_integral_setsum) (simp_all add: o_def)
  3330   thus "(f has_integral y) A" by (simp add: euclidean_representation)
  3331 qed
  3332 
  3333 lemma has_integral_componentwise:
  3334   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3335   shows "(\<And>b. b \<in> Basis \<Longrightarrow> ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A) \<Longrightarrow> (f has_integral y) A"
  3336   by (subst has_integral_componentwise_iff) blast
  3337 
  3338 lemma integrable_componentwise_iff:
  3339   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3340   shows "f integrable_on A \<longleftrightarrow> (\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)"
  3341 proof
  3342   assume "f integrable_on A"
  3343   then obtain y where "(f has_integral y) A" by (auto simp: integrable_on_def)
  3344   hence "(\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
  3345     by (subst (asm) has_integral_componentwise_iff)
  3346   thus "(\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)" by (auto simp: integrable_on_def)
  3347 next
  3348   assume "(\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)"
  3349   then obtain y where "\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral y b) A"
  3350     unfolding integrable_on_def by (subst (asm) bchoice_iff) blast
  3351   hence "\<forall>b\<in>Basis. (((\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. f x \<bullet> b)) has_integral (y b *\<^sub>R b)) A"
  3352     by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
  3353   hence "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. y b *\<^sub>R b)) A"
  3354     by (intro has_integral_setsum) (simp_all add: o_def)
  3355   thus "f integrable_on A" by (auto simp: integrable_on_def o_def euclidean_representation)
  3356 qed
  3357 
  3358 lemma integrable_componentwise:
  3359   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3360   shows "(\<And>b. b \<in> Basis \<Longrightarrow> (\<lambda>x. f x \<bullet> b) integrable_on A) \<Longrightarrow> f integrable_on A"
  3361   by (subst integrable_componentwise_iff) blast
  3362 
  3363 lemma integral_componentwise:
  3364   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3365   assumes "f integrable_on A"
  3366   shows "integral A f = (\<Sum>b\<in>Basis. integral A (\<lambda>x. (f x \<bullet> b) *\<^sub>R b))"
  3367 proof -
  3368   from assms have integrable: "\<forall>b\<in>Basis. (\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. (f x \<bullet> b)) integrable_on A"
  3369     by (subst (asm) integrable_componentwise_iff, intro integrable_linear ballI)
  3370        (simp_all add: bounded_linear_scaleR_left)
  3371   have "integral A f = integral A (\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b)"
  3372     by (simp add: euclidean_representation)
  3373   also from integrable have "\<dots> = (\<Sum>a\<in>Basis. integral A (\<lambda>x. (f x \<bullet> a) *\<^sub>R a))"
  3374     by (subst integral_setsum) (simp_all add: o_def)
  3375   finally show ?thesis .
  3376 qed
  3377 
  3378 lemma integrable_component:
  3379   "f integrable_on A \<Longrightarrow> (\<lambda>x. f x \<bullet> (y :: 'b :: euclidean_space)) integrable_on A"
  3380   by (drule integrable_linear[OF _ bounded_linear_component[of y]]) (simp add: o_def)
  3381 
  3382 
  3383 
  3384 subsection \<open>Cauchy-type criterion for integrability.\<close>
  3385 
  3386 (* XXXXXXX *)
  3387 lemma integrable_cauchy:
  3388   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::{real_normed_vector,complete_space}"
  3389   shows "f integrable_on cbox a b \<longleftrightarrow>
  3390     (\<forall>e>0.\<exists>d. gauge d \<and>
  3391       (\<forall>p1 p2. p1 tagged_division_of (cbox a b) \<and> d fine p1 \<and>
  3392         p2 tagged_division_of (cbox a b) \<and> d fine p2 \<longrightarrow>
  3393         norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 -
  3394         setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) < e))"
  3395   (is "?l = (\<forall>e>0. \<exists>d. ?P e d)")
  3396 proof
  3397   assume ?l
  3398   then guess y unfolding integrable_on_def has_integral .. note y=this
  3399   show "\<forall>e>0. \<exists>d. ?P e d"
  3400   proof (clarify, goal_cases)
  3401     case (1 e)
  3402     then have "e/2 > 0" by auto
  3403     then guess d
  3404       apply -
  3405       apply (drule y[rule_format])
  3406       apply (elim exE conjE)
  3407       done
  3408     note d=this[rule_format]
  3409     show ?case
  3410     proof (rule_tac x=d in exI, clarsimp simp: d)
  3411       fix p1 p2
  3412       assume as: "p1 tagged_division_of (cbox a b)" "d fine p1"
  3413                  "p2 tagged_division_of (cbox a b)" "d fine p2"
  3414       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  3415         apply (rule dist_triangle_half_l[where y=y,unfolded dist_norm])
  3416         using d(2)[OF conjI[OF as(1-2)]] d(2)[OF conjI[OF as(3-4)]] .
  3417     qed
  3418   qed
  3419 next
  3420   assume "\<forall>e>0. \<exists>d. ?P e d"
  3421   then have "\<forall>n::nat. \<exists>d. ?P (inverse(of_nat (n + 1))) d"
  3422     by auto
  3423   from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
  3424   have "\<And>n. gauge (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}})"
  3425     apply (rule gauge_inters)
  3426     using d(1)
  3427     apply auto
  3428     done
  3429   then have "\<forall>n. \<exists>p. p tagged_division_of (cbox a b) \<and> (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}}) fine p"
  3430     by (meson fine_division_exists)
  3431   from choice[OF this] guess p .. note p = conjunctD2[OF this[rule_format]]
  3432   have dp: "\<And>i n. i\<le>n \<Longrightarrow> d i fine p n"
  3433     using p(2) unfolding fine_inters by auto
  3434   have "Cauchy (\<lambda>n. setsum (\<lambda>(x,k). content k *\<^sub>R (f x)) (p n))"
  3435   proof (rule CauchyI, goal_cases)
  3436     case (1 e)
  3437     then guess N unfolding real_arch_inverse[of e] .. note N=this
  3438     show ?case
  3439       apply (rule_tac x=N in exI)
  3440     proof clarify
  3441       fix m n
  3442       assume mn: "N \<le> m" "N \<le> n"
  3443       have *: "N = (N - 1) + 1" using N by auto
  3444       show "norm ((\<Sum>(x, k)\<in>p m. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p n. content k *\<^sub>R f x)) < e"
  3445         apply (rule less_trans[OF _ N[THEN conjunct2,THEN conjunct2]])
  3446         apply(subst *)
  3447         using dp p(1) mn d(2) by auto
  3448     qed
  3449   qed
  3450   then guess y unfolding convergent_eq_cauchy[symmetric] .. note y=this[THEN LIMSEQ_D]
  3451   show ?l
  3452     unfolding integrable_on_def has_integral
  3453   proof (rule_tac x=y in exI, clarify)
  3454     fix e :: real
  3455     assume "e>0"
  3456     then have *:"e/2 > 0" by auto
  3457     then guess N1 unfolding real_arch_inverse[of "e/2"] .. note N1=this
  3458     then have N1': "N1 = N1 - 1 + 1"
  3459       by auto
  3460     guess N2 using y[OF *] .. note N2=this
  3461     have "gauge (d (N1 + N2))"
  3462       using d by auto
  3463     moreover
  3464     {
  3465       fix q
  3466       assume as: "q tagged_division_of (cbox a b)" "d (N1 + N2) fine q"
  3467       have *: "inverse (of_nat (N1 + N2 + 1)) < e / 2"
  3468         apply (rule less_trans)
  3469         using N1
  3470         apply auto
  3471         done
  3472       have "norm ((\<Sum>(x, k)\<in>q. content k *\<^sub>R f x) - y) < e"
  3473         apply (rule norm_triangle_half_r)
  3474         apply (rule less_trans[OF _ *])
  3475         apply (subst N1', rule d(2)[of "p (N1+N2)"])
  3476         using N1' as(1) as(2) dp
  3477         apply (simp add: \<open>\<forall>x. p x tagged_division_of cbox a b \<and> (\<lambda>xa. \<Inter>{d i xa |i. i \<in> {0..x}}) fine p x\<close>)
  3478         using N2 le_add2 by blast
  3479     }
  3480     ultimately show "\<exists>d. gauge d \<and>
  3481       (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  3482         norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - y) < e)"
  3483       by (rule_tac x="d (N1 + N2)" in exI) auto
  3484   qed
  3485 qed
  3486 
  3487 
  3488 subsection \<open>Additivity of integral on abutting intervals.\<close>
  3489 
  3490 lemma tagged_division_split_left_inj:
  3491   fixes x1 :: "'a::euclidean_space"
  3492   assumes d: "d tagged_division_of i"
  3493     and k12: "(x1, k1) \<in> d"
  3494              "(x2, k2) \<in> d"
  3495              "k1 \<noteq> k2"
  3496              "k1 \<inter> {x. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c}"
  3497              "k \<in> Basis"
  3498   shows "content (k1 \<inter> {x. x\<bullet>k \<le> c}) = 0"
  3499 proof -
  3500   have *: "\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c"
  3501     by force
  3502   show ?thesis
  3503     using k12
  3504     by (fastforce intro!:  division_split_left_inj[OF division_of_tagged_division[OF d]] *)
  3505 qed
  3506 
  3507 lemma tagged_division_split_right_inj:
  3508   fixes x1 :: "'a::euclidean_space"
  3509   assumes d: "d tagged_division_of i"
  3510     and k12: "(x1, k1) \<in> d"
  3511              "(x2, k2) \<in> d"
  3512              "k1 \<noteq> k2"
  3513              "k1 \<inter> {x. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c}"
  3514              "k \<in> Basis"
  3515   shows "content (k1 \<inter> {x. x\<bullet>k \<ge> c}) = 0"
  3516 proof -
  3517   have *: "\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c"
  3518     by force
  3519   show ?thesis
  3520     using k12
  3521     by (fastforce intro!:  division_split_right_inj[OF division_of_tagged_division[OF d]] *)
  3522 qed
  3523 
  3524 lemma has_integral_split:
  3525   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  3526   assumes fi: "(f has_integral i) (cbox a b \<inter> {x. x\<bullet>k \<le> c})"
  3527       and fj: "(f has_integral j) (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  3528       and k: "k \<in> Basis"
  3529   shows "(f has_integral (i + j)) (cbox a b)"
  3530 proof (unfold has_integral, rule, rule, goal_cases)
  3531   case (1 e)
  3532   then have e: "e/2 > 0"
  3533     by auto
  3534     obtain d1
  3535     where d1: "gauge d1"
  3536       and d1norm:
  3537         "\<And>p. \<lbrakk>p tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<le> c};
  3538                d1 fine p\<rbrakk> \<Longrightarrow> norm ((\<Sum>(x, k) \<in> p. content k *\<^sub>R f x) - i) < e / 2"
  3539        apply (rule has_integralD[OF fi[unfolded interval_split[OF k]] e])
  3540        apply (simp add: interval_split[symmetric] k)
  3541        done
  3542     obtain d2
  3543     where d2: "gauge d2"
  3544       and d2norm:
  3545         "\<And>p. \<lbrakk>p tagged_division_of cbox a b \<inter> {x. c \<le> x \<bullet> k};
  3546                d2 fine p\<rbrakk> \<Longrightarrow> norm ((\<Sum>(x, k) \<in> p. content k *\<^sub>R f x) - j) < e / 2"
  3547        apply (rule has_integralD[OF fj[unfolded interval_split[OF k]] e])
  3548        apply (simp add: interval_split[symmetric] k)
  3549        done
  3550   let ?d = "\<lambda>x. if x\<bullet>k = c then (d1 x \<inter> d2 x) else ball x \<bar>x\<bullet>k - c\<bar> \<inter> d1 x \<inter> d2 x"
  3551   have "gauge ?d"
  3552     using d1 d2 unfolding gauge_def by auto
  3553   then show ?case
  3554   proof (rule_tac x="?d" in exI, safe)
  3555     fix p
  3556     assume "p tagged_division_of (cbox a b)" "?d fine p"
  3557     note p = this tagged_division_ofD[OF this(1)]
  3558     have xk_le_c: "\<And>x kk. (x, kk) \<in> p \<Longrightarrow> kk \<inter> {x. x\<bullet>k \<le> c} \<noteq> {} \<Longrightarrow> x\<bullet>k \<le> c"
  3559     proof -
  3560       fix x kk
  3561       assume as: "(x, kk) \<in> p" and kk: "kk \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}"
  3562       show "x\<bullet>k \<le> c"
  3563       proof (rule ccontr)
  3564         assume **: "\<not> ?thesis"
  3565         from this[unfolded not_le]
  3566         have "kk \<subseteq> ball x \<bar>x \<bullet> k - c\<bar>"
  3567           using p(2)[unfolded fine_def, rule_format,OF as] by auto
  3568         with kk obtain y where y: "y \<in> ball x \<bar>x \<bullet> k - c\<bar>" "y\<bullet>k \<le> c"
  3569           by blast
  3570         then have "\<bar>x \<bullet> k - y \<bullet> k\<bar> < \<bar>x \<bullet> k - c\<bar>"
  3571           using Basis_le_norm[OF k, of "x - y"]
  3572           by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
  3573         with y show False
  3574           using ** by (auto simp add: field_simps)
  3575       qed
  3576     qed
  3577     have xk_ge_c: "\<And>x kk. (x, kk) \<in> p \<Longrightarrow> kk \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {} \<Longrightarrow> x\<bullet>k \<ge> c"
  3578     proof -
  3579       fix x kk
  3580       assume as: "(x, kk) \<in> p" and kk: "kk \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}"
  3581       show "x\<bullet>k \<ge> c"
  3582       proof (rule ccontr)
  3583         assume **: "\<not> ?thesis"
  3584         from this[unfolded not_le] have "kk \<subseteq> ball x \<bar>x \<bullet> k - c\<bar>"
  3585           using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
  3586         with kk obtain y where y: "y \<in> ball x \<bar>x \<bullet> k - c\<bar>" "y\<bullet>k \<ge> c"
  3587           by blast
  3588         then have "\<bar>x \<bullet> k - y \<bullet> k\<bar> < \<bar>x \<bullet> k - c\<bar>"
  3589           using Basis_le_norm[OF k, of "x - y"]
  3590           by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
  3591         with y show False
  3592           using ** by (auto simp add: field_simps)
  3593       qed
  3594     qed
  3595 
  3596     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>
  3597                          (\<forall>x k. P x k \<longrightarrow> Q x (f k))"
  3598       by auto
  3599     have fin_finite: "finite {(x,f k) | x k. (x,k) \<in> s \<and> P x k}" if "finite s" for f s P
  3600     proof -
  3601       from that have "finite ((\<lambda>(x, k). (x, f k)) ` s)"
  3602         by auto
  3603       then show ?thesis
  3604         by (rule rev_finite_subset) auto
  3605     qed
  3606     { fix g :: "'a set \<Rightarrow> 'a set"
  3607       fix i :: "'a \<times> 'a set"
  3608       assume "i \<in> (\<lambda>(x, k). (x, g k)) ` p - {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
  3609       then obtain x k where xk:
  3610               "i = (x, g k)"  "(x, k) \<in> p"
  3611               "(x, g k) \<notin> {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
  3612           by auto
  3613       have "content (g k) = 0"
  3614         using xk using content_empty by auto
  3615       then have "(\<lambda>(x, k). content k *\<^sub>R f x) i = 0"
  3616         unfolding xk split_conv by auto
  3617     } note [simp] = this
  3618     have lem3: "\<And>g :: 'a set \<Rightarrow> 'a set. finite p \<Longrightarrow>
  3619                   setsum (\<lambda>(x, k). content k *\<^sub>R f x) {(x,g k) |x k. (x,k) \<in> p \<and> g k \<noteq> {}} =
  3620                   setsum (\<lambda>(x, k). content k *\<^sub>R f x) ((\<lambda>(x, k). (x, g k)) ` p)"
  3621       by (rule setsum.mono_neutral_left) auto
  3622     let ?M1 = "{(x, kk \<inter> {x. x\<bullet>k \<le> c}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}}"
  3623     have d1_fine: "d1 fine ?M1"
  3624       by (force intro: fineI dest: fineD[OF p(2)] simp add: split: if_split_asm)
  3625     have "norm ((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) < e/2"
  3626     proof (rule d1norm [OF tagged_division_ofI d1_fine])
  3627       show "finite ?M1"
  3628         by (rule fin_finite p(3))+
  3629       show "\<Union>{k. \<exists>x. (x, k) \<in> ?M1} = cbox a b \<inter> {x. x\<bullet>k \<le> c}"
  3630         unfolding p(8)[symmetric] by auto
  3631       fix x l
  3632       assume xl: "(x, l) \<in> ?M1"
  3633       then guess x' l' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note xl'=this
  3634       show "x \<in> l" "l \<subseteq> cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  3635         unfolding xl'
  3636         using p(4-6)[OF xl'(3)] using xl'(4)
  3637         using xk_le_c[OF xl'(3-4)] by auto
  3638       show "\<exists>a b. l = cbox a b"
  3639         unfolding xl'
  3640         using p(6)[OF xl'(3)]
  3641         by (fastforce simp add: interval_split[OF k,where c=c])
  3642       fix y r
  3643       let ?goal = "interior l \<inter> interior r = {}"
  3644       assume yr: "(y, r) \<in> ?M1"
  3645       then guess y' r' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note yr'=this
  3646       assume as: "(x, l) \<noteq> (y, r)"
  3647       show "interior l \<inter> interior r = {}"
  3648       proof (cases "l' = r' \<longrightarrow> x' = y'")
  3649         case False
  3650         then show ?thesis
  3651           using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  3652       next
  3653         case True
  3654         then have "l' \<noteq> r'"
  3655           using as unfolding xl' yr' by auto
  3656         then show ?thesis
  3657           using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  3658       qed
  3659     qed
  3660     moreover
  3661     let ?M2 = "{(x,kk \<inter> {x. x\<bullet>k \<ge> c}) |x kk. (x,kk) \<in> p \<and> kk \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}}"
  3662     have d2_fine: "d2 fine ?M2"
  3663       by (force intro: fineI dest: fineD[OF p(2)] simp add: split: if_split_asm)
  3664     have "norm ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) < e/2"
  3665     proof (rule d2norm [OF tagged_division_ofI d2_fine])
  3666       show "finite ?M2"
  3667         by (rule fin_finite p(3))+
  3668       show "\<Union>{k. \<exists>x. (x, k) \<in> ?M2} = cbox a b \<inter> {x. x\<bullet>k \<ge> c}"
  3669         unfolding p(8)[symmetric] by auto
  3670       fix x l
  3671       assume xl: "(x, l) \<in> ?M2"
  3672       then guess x' l' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note xl'=this
  3673       show "x \<in> l" "l \<subseteq> cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  3674         unfolding xl'
  3675         using p(4-6)[OF xl'(3)] xl'(4) xk_ge_c[OF xl'(3-4)]
  3676         by auto
  3677       show "\<exists>a b. l = cbox a b"
  3678         unfolding xl'
  3679         using p(6)[OF xl'(3)]
  3680         by (fastforce simp add: interval_split[OF k, where c=c])
  3681       fix y r
  3682       let ?goal = "interior l \<inter> interior r = {}"
  3683       assume yr: "(y, r) \<in> ?M2"
  3684       then guess y' r' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note yr'=this
  3685       assume as: "(x, l) \<noteq> (y, r)"
  3686       show "interior l \<inter> interior r = {}"
  3687       proof (cases "l' = r' \<longrightarrow> x' = y'")
  3688         case False
  3689         then show ?thesis
  3690           using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  3691       next
  3692         case True
  3693         then have "l' \<noteq> r'"
  3694           using as unfolding xl' yr' by auto
  3695         then show ?thesis
  3696           using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  3697       qed
  3698     qed
  3699     ultimately
  3700     have "norm (((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j)) < e/2 + e/2"
  3701       using norm_add_less by blast
  3702     also {
  3703       have eq0: "\<And>x y. x = (0::real) \<Longrightarrow> x *\<^sub>R (y::'b) = 0"
  3704         using scaleR_zero_left by auto
  3705       have cont_eq: "\<And>g. (\<lambda>(x,l). content l *\<^sub>R f x) \<circ> (\<lambda>(x,l). (x,g l)) = (\<lambda>(x,l). content (g l) *\<^sub>R f x)"
  3706         by auto
  3707       have "((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) =
  3708         (\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - (i + j)"
  3709         by auto
  3710       also have "\<dots> = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f x) +
  3711         (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f x) - (i + j)"
  3712         unfolding lem3[OF p(3)]
  3713         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)]
  3714               simp: cont_eq)+
  3715       also note setsum.distrib[symmetric]
  3716       also have "\<And>x. x \<in> p \<Longrightarrow>
  3717                     (\<lambda>(x,ka). content (ka \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f x) x +
  3718                     (\<lambda>(x,ka). content (ka \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f x) x =
  3719                     (\<lambda>(x,ka). content ka *\<^sub>R f x) x"
  3720       proof clarify
  3721         fix a b
  3722         assume "(a, b) \<in> p"
  3723         from p(6)[OF this] guess u v by (elim exE) note uv=this
  3724         then show "content (b \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f a + content (b \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f a =
  3725           content b *\<^sub>R f a"
  3726           unfolding scaleR_left_distrib[symmetric]
  3727           unfolding uv content_split[OF k,of u v c]
  3728           by auto
  3729       qed
  3730       note setsum.cong [OF _ this]
  3731       finally have "(\<Sum>(x, k)\<in>{(x, kk \<inter> {x. x \<bullet> k \<le> c}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}}. content k *\<^sub>R f x) - i +
  3732         ((\<Sum>(x, k)\<in>{(x, kk \<inter> {x. c \<le> x \<bullet> k}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. c \<le> x \<bullet> k} \<noteq> {}}. content k *\<^sub>R f x) - j) =
  3733         (\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f x) - (i + j)"
  3734         by auto
  3735     }
  3736     finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (i + j)) < e"
  3737       by auto
  3738   qed
  3739 qed
  3740 
  3741 
  3742 subsection \<open>A sort of converse, integrability on subintervals.\<close>
  3743 
  3744 lemma tagged_division_union_interval:
  3745   fixes a :: "'a::euclidean_space"
  3746   assumes "p1 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<le> (c::real)})"
  3747     and "p2 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  3748     and k: "k \<in> Basis"
  3749   shows "(p1 \<union> p2) tagged_division_of (cbox a b)"
  3750 proof -
  3751   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})"
  3752     by auto
  3753   show ?thesis
  3754     apply (subst *)
  3755     apply (rule tagged_division_union[OF assms(1-2)])
  3756     unfolding interval_split[OF k] interior_cbox
  3757     using k
  3758     apply (auto simp add: box_def elim!: ballE[where x=k])
  3759     done
  3760 qed
  3761 
  3762 lemma tagged_division_union_interval_real:
  3763   fixes a :: real
  3764   assumes "p1 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<le> (c::real)})"
  3765     and "p2 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<ge> c})"
  3766     and k: "k \<in> Basis"
  3767   shows "(p1 \<union> p2) tagged_division_of {a .. b}"
  3768   using assms
  3769   unfolding box_real[symmetric]
  3770   by (rule tagged_division_union_interval)
  3771 
  3772 lemma has_integral_separate_sides:
  3773   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  3774   assumes "(f has_integral i) (cbox a b)"
  3775     and "e > 0"
  3776     and k: "k \<in> Basis"
  3777   obtains d where "gauge d"
  3778     "\<forall>p1 p2. p1 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<and> d fine p1 \<and>
  3779         p2 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) \<and> d fine p2 \<longrightarrow>
  3780         norm ((setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 + setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) - i) < e"
  3781 proof -
  3782   guess d using has_integralD[OF assms(1-2)] . note d=this
  3783   { fix p1 p2
  3784     assume "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p1"
  3785     note p1=tagged_division_ofD[OF this(1)] this
  3786     assume "p2 tagged_division_of (cbox a b) \<inter> {x. c \<le> x \<bullet> k}" "d fine p2"
  3787     note p2=tagged_division_ofD[OF this(1)] this
  3788     note tagged_division_union_interval[OF p1(7) p2(7)] note p12 = tagged_division_ofD[OF this] this
  3789     { fix a b
  3790       assume ab: "(a, b) \<in> p1 \<inter> p2"
  3791       have "(a, b) \<in> p1"
  3792         using ab by auto
  3793       with p1 obtain u v where uv: "b = cbox u v" by auto
  3794       have "b \<subseteq> {x. x\<bullet>k = c}"
  3795         using ab p1(3)[of a b] p2(3)[of a b] by fastforce
  3796       moreover
  3797       have "interior {x::'a. x \<bullet> k = c} = {}"
  3798       proof (rule ccontr)
  3799         assume "\<not> ?thesis"
  3800         then obtain x where x: "x \<in> interior {x::'a. x\<bullet>k = c}"
  3801           by auto
  3802         then guess e unfolding mem_interior .. note e=this
  3803         have x: "x\<bullet>k = c"
  3804           using x interior_subset by fastforce
  3805         have *: "\<And>i. i \<in> Basis \<Longrightarrow> \<bar>(x - (x + (e / 2) *\<^sub>R k)) \<bullet> i\<bar> = (if i = k then e/2 else 0)"
  3806           using e k by (auto simp: inner_simps inner_not_same_Basis)
  3807         have "(\<Sum>i\<in>Basis. \<bar>(x - (x + (e / 2 ) *\<^sub>R k)) \<bullet> i\<bar>) =
  3808               (\<Sum>i\<in>Basis. (if i = k then e / 2 else 0))"
  3809           using "*" by (blast intro: setsum.cong)
  3810         also have "\<dots> < e"
  3811           apply (subst setsum.delta)
  3812           using e
  3813           apply auto
  3814           done
  3815         finally have "x + (e/2) *\<^sub>R k \<in> ball x e"
  3816           unfolding mem_ball dist_norm by(rule le_less_trans[OF norm_le_l1])
  3817         then have "x + (e/2) *\<^sub>R k \<in> {x. x\<bullet>k = c}"
  3818           using e by auto
  3819         then show False
  3820           unfolding mem_Collect_eq using e x k by (auto simp: inner_simps)
  3821       qed
  3822       ultimately have "content b = 0"
  3823         unfolding uv content_eq_0_interior
  3824         using interior_mono by blast
  3825       then have "content b *\<^sub>R f a = 0"
  3826         by auto
  3827     }
  3828     then have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) =
  3829                norm ((\<Sum>(x, k)\<in>p1 \<union> p2. content k *\<^sub>R f x) - i)"
  3830       by (subst setsum.union_inter_neutral) (auto simp: p1 p2)
  3831     also have "\<dots> < e"
  3832       by (rule k d(2) p12 fine_union p1 p2)+
  3833     finally have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) < e" .
  3834    }
  3835   then show ?thesis
  3836     by (auto intro: that[of d] d elim: )
  3837 qed
  3838 
  3839 lemma integrable_split[intro]:
  3840   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::{real_normed_vector,complete_space}"
  3841   assumes "f integrable_on cbox a b"
  3842     and k: "k \<in> Basis"
  3843   shows "f integrable_on (cbox a b \<inter> {x. x\<bullet>k \<le> c})" (is ?t1)
  3844     and "f integrable_on (cbox a b \<inter> {x. x\<bullet>k \<ge> c})" (is ?t2)
  3845 proof -
  3846   guess y using assms(1) unfolding integrable_on_def .. note y=this
  3847   define b' where "b' = (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) c else b\<bullet>i)*\<^sub>R i)"
  3848   define a' where "a' = (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) c else a\<bullet>i)*\<^sub>R i)"
  3849   show ?t1 ?t2
  3850     unfolding interval_split[OF k] integrable_cauchy
  3851     unfolding interval_split[symmetric,OF k]
  3852   proof (rule_tac[!] allI impI)+
  3853     fix e :: real
  3854     assume "e > 0"
  3855     then have "e/2>0"
  3856       by auto
  3857     from has_integral_separate_sides[OF y this k,of c] guess d . note d=this[rule_format]
  3858     let ?P = "\<lambda>A. \<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of (cbox a b) \<inter> A \<and> d fine p1 \<and>
  3859       p2 tagged_division_of (cbox a b) \<inter> A \<and> d fine p2 \<longrightarrow>
  3860       norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e)"
  3861     show "?P {x. x \<bullet> k \<le> c}"
  3862     proof (rule_tac x=d in exI, clarsimp simp add: d)
  3863       fix p1 p2
  3864       assume as: "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p1"
  3865                  "p2 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p2"
  3866       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  3867       proof (rule fine_division_exists[OF d(1), of a' b] )
  3868         fix p
  3869         assume "p tagged_division_of cbox a' b" "d fine p"
  3870         then show ?thesis
  3871           using as norm_triangle_half_l[OF d(2)[of p1 p] d(2)[of p2 p]]
  3872           unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  3873           by (auto simp add: algebra_simps)
  3874       qed
  3875     qed
  3876     show "?P {x. x \<bullet> k \<ge> c}"
  3877     proof (rule_tac x=d in exI, clarsimp simp add: d)
  3878       fix p1 p2
  3879       assume as: "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<ge> c}" "d fine p1"
  3880                  "p2 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<ge> c}" "d fine p2"
  3881       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  3882       proof (rule fine_division_exists[OF d(1), of a b'] )
  3883         fix p
  3884         assume "p tagged_division_of cbox a b'" "d fine p"
  3885         then show ?thesis
  3886           using as norm_triangle_half_l[OF d(2)[of p p1] d(2)[of p p2]]
  3887           unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  3888           by (auto simp add: algebra_simps)
  3889       qed
  3890     qed
  3891   qed
  3892 qed
  3893 
  3894 lemma operative_integral:
  3895   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  3896   shows "comm_monoid.operative (lift_option op +) (Some 0) (\<lambda>i. if f integrable_on i then Some (integral i f) else None)"
  3897   unfolding comm_monoid.operative_def[OF add.comm_monoid_lift_option]
  3898 proof safe
  3899   fix a b c
  3900   fix k :: 'a
  3901   assume k: "k \<in> Basis"
  3902   show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) =
  3903         lift_option op + (if f integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c} then Some (integral (cbox a b \<inter> {x. x \<bullet> k \<le> c}) f) else None)
  3904         (if f integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k} then Some (integral (cbox a b \<inter> {x. c \<le> x \<bullet> k}) f) else None)"
  3905   proof (cases "f integrable_on cbox a b")
  3906     case True
  3907     with k show ?thesis
  3908       apply (simp add: integrable_split)
  3909       apply (rule integral_unique [OF has_integral_split[OF _ _ k]])
  3910       apply (auto intro: integrable_integral)
  3911       done
  3912   next
  3913     case False
  3914     have "\<not> (f integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}) \<or> \<not> ( f integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k})"
  3915     proof (rule ccontr)
  3916       assume "\<not> ?thesis"
  3917       then have "f integrable_on cbox a b"
  3918         unfolding integrable_on_def
  3919         apply (rule_tac x="integral (cbox a b \<inter> {x. x \<bullet> k \<le> c}) f + integral (cbox a b \<inter> {x. x \<bullet> k \<ge> c}) f" in exI)
  3920         apply (rule has_integral_split[OF _ _ k])
  3921         apply (auto intro: integrable_integral)
  3922         done
  3923       then show False
  3924         using False by auto
  3925     qed
  3926     then show ?thesis
  3927       using False by auto
  3928   qed
  3929 next
  3930   fix a b :: 'a
  3931   assume "content (cbox a b) = 0"
  3932   then show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) = Some 0"
  3933     using has_integral_null_eq
  3934     by (auto simp: integrable_on_null)
  3935 qed
  3936 
  3937 subsection \<open>Finally, the integral of a constant\<close>
  3938 
  3939 lemma has_integral_const [intro]:
  3940   fixes a b :: "'a::euclidean_space"
  3941   shows "((\<lambda>x. c) has_integral (content (cbox a b) *\<^sub>R c)) (cbox a b)"
  3942   apply (auto intro!: exI [where x="\<lambda>x. ball x 1"] simp: split_def has_integral)
  3943   apply (subst scaleR_left.setsum[symmetric, unfolded o_def])
  3944   apply (subst additive_content_tagged_division[unfolded split_def])
  3945   apply auto
  3946   done
  3947 
  3948 lemma has_integral_const_real [intro]:
  3949   fixes a b :: real
  3950   shows "((\<lambda>x. c) has_integral (content {a .. b} *\<^sub>R c)) {a .. b}"
  3951   by (metis box_real(2) has_integral_const)
  3952 
  3953 lemma integral_const [simp]:
  3954   fixes a b :: "'a::euclidean_space"
  3955   shows "integral (cbox a b) (\<lambda>x. c) = content (cbox a b) *\<^sub>R c"
  3956   by (rule integral_unique) (rule has_integral_const)
  3957 
  3958 lemma integral_const_real [simp]:
  3959   fixes a b :: real
  3960   shows "integral {a .. b} (\<lambda>x. c) = content {a .. b} *\<^sub>R c"
  3961   by (metis box_real(2) integral_const)
  3962 
  3963 
  3964 subsection \<open>Bounds on the norm of Riemann sums and the integral itself.\<close>
  3965 
  3966 lemma dsum_bound:
  3967   assumes "p division_of (cbox a b)"
  3968     and "norm c \<le> e"
  3969   shows "norm (setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> e * content(cbox a b)"
  3970 proof -
  3971   have sumeq: "(\<Sum>i\<in>p. \<bar>content i\<bar>) = setsum content p"
  3972     apply (rule setsum.cong)
  3973     using assms
  3974     apply simp
  3975     apply (metis abs_of_nonneg assms(1) content_pos_le division_ofD(4))
  3976     done
  3977   have e: "0 \<le> e"
  3978     using assms(2) norm_ge_zero order_trans by blast
  3979   have "norm (setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> (\<Sum>i\<in>p. norm (content i *\<^sub>R c))"
  3980     using norm_setsum by blast
  3981   also have "...  \<le> e * (\<Sum>i\<in>p. \<bar>content i\<bar>)"
  3982     apply (simp add: setsum_right_distrib[symmetric] mult.commute)
  3983     using assms(2) mult_right_mono by blast
  3984   also have "... \<le> e * content (cbox a b)"
  3985     apply (rule mult_left_mono [OF _ e])
  3986     apply (simp add: sumeq)
  3987     using additive_content_division assms(1) eq_iff apply blast
  3988     done
  3989   finally show ?thesis .
  3990 qed
  3991 
  3992 lemma rsum_bound:
  3993   assumes p: "p tagged_division_of (cbox a b)"
  3994       and "\<forall>x\<in>cbox a b. norm (f x) \<le> e"
  3995     shows "norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> e * content (cbox a b)"
  3996 proof (cases "cbox a b = {}")
  3997   case True show ?thesis
  3998     using p unfolding True tagged_division_of_trivial by auto
  3999 next
  4000   case False
  4001   then have e: "e \<ge> 0"
  4002     by (meson ex_in_conv assms(2) norm_ge_zero order_trans)
  4003   have setsum_le: "setsum (content \<circ> snd) p \<le> content (cbox a b)"
  4004     unfolding additive_content_tagged_division[OF p, symmetric] split_def
  4005     by (auto intro: eq_refl)
  4006   have con: "\<And>xk. xk \<in> p \<Longrightarrow> 0 \<le> content (snd xk)"
  4007     using tagged_division_ofD(4) [OF p] content_pos_le
  4008     by force
  4009   have norm: "\<And>xk. xk \<in> p \<Longrightarrow> norm (f (fst xk)) \<le> e"
  4010     unfolding fst_conv using tagged_division_ofD(2,3)[OF p] assms
  4011     by (metis prod.collapse subset_eq)
  4012   have "norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> (\<Sum>i\<in>p. norm (case i of (x, k) \<Rightarrow> content k *\<^sub>R f x))"
  4013     by (rule norm_setsum)
  4014   also have "...  \<le> e * content (cbox a b)"
  4015     unfolding split_def norm_scaleR
  4016     apply (rule order_trans[OF setsum_mono])
  4017     apply (rule mult_left_mono[OF _ abs_ge_zero, of _ e])
  4018     apply (metis norm)
  4019     unfolding setsum_left_distrib[symmetric]
  4020     using con setsum_le
  4021     apply (auto simp: mult.commute intro: mult_left_mono [OF _ e])
  4022     done
  4023   finally show ?thesis .
  4024 qed
  4025 
  4026 lemma rsum_diff_bound:
  4027   assumes "p tagged_division_of (cbox a b)"
  4028     and "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e"
  4029   shows "norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - setsum (\<lambda>(x,k). content k *\<^sub>R g x) p) \<le>
  4030          e * content (cbox a b)"
  4031   apply (rule order_trans[OF _ rsum_bound[OF assms]])
  4032   apply (simp add: split_def scaleR_diff_right setsum_subtractf eq_refl)
  4033   done
  4034 
  4035 lemma has_integral_bound:
  4036   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  4037   assumes "0 \<le> B"
  4038       and "(f has_integral i) (cbox a b)"
  4039       and "\<forall>x\<in>cbox a b. norm (f x) \<le> B"
  4040     shows "norm i \<le> B * content (cbox a b)"
  4041 proof (rule ccontr)
  4042   assume "\<not> ?thesis"
  4043   then have *: "norm i - B * content (cbox a b) > 0"
  4044     by auto
  4045   from assms(2)[unfolded has_integral,rule_format,OF *]
  4046   guess d by (elim exE conjE) note d=this[rule_format]
  4047   from fine_division_exists[OF this(1), of a b] guess p . note p=this
  4048   have *: "\<And>s B. norm s \<le> B \<Longrightarrow> \<not> norm (s - i) < norm i - B"
  4049     unfolding not_less
  4050     by (metis norm_triangle_sub[of i] add.commute le_less_trans less_diff_eq linorder_not_le norm_minus_commute)
  4051   show False
  4052     using d(2)[OF conjI[OF p]] *[OF rsum_bound[OF p(1) assms(3)]] by auto
  4053 qed
  4054 
  4055 corollary has_integral_bound_real:
  4056   fixes f :: "real \<Rightarrow> 'b::real_normed_vector"
  4057   assumes "0 \<le> B"
  4058       and "(f has_integral i) {a .. b}"
  4059       and "\<forall>x\<in>{a .. b}. norm (f x) \<le> B"
  4060     shows "norm i \<le> B * content {a .. b}"
  4061   by (metis assms box_real(2) has_integral_bound)
  4062 
  4063 corollary integrable_bound:
  4064   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  4065   assumes "0 \<le> B"
  4066       and "f integrable_on (cbox a b)"
  4067       and "\<And>x. x\<in>cbox a b \<Longrightarrow> norm (f x) \<le> B"
  4068     shows "norm (integral (cbox a b) f) \<le> B * content (cbox a b)"
  4069 by (metis integrable_integral has_integral_bound assms)
  4070 
  4071 
  4072 subsection \<open>Similar theorems about relationship among components.\<close>
  4073 
  4074 lemma rsum_component_le:
  4075   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4076   assumes "p tagged_division_of (cbox a b)"
  4077       and "\<forall>x\<in>cbox a b. (f x)\<bullet>i \<le> (g x)\<bullet>i"
  4078     shows "(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p)\<bullet>i \<le> (setsum (\<lambda>(x,k). content k *\<^sub>R g x) p)\<bullet>i"
  4079 unfolding inner_setsum_left
  4080 proof (rule setsum_mono, clarify)
  4081   fix a b
  4082   assume ab: "(a, b) \<in> p"
  4083   note tagged = tagged_division_ofD(2-4)[OF assms(1) ab]
  4084   from this(3) guess u v by (elim exE) note b=this
  4085   show "(content b *\<^sub>R f a) \<bullet> i \<le> (content b *\<^sub>R g a) \<bullet> i"
  4086     unfolding b inner_simps real_scaleR_def
  4087     apply (rule mult_left_mono)
  4088     using assms(2) tagged
  4089     by (auto simp add: content_pos_le)
  4090 qed
  4091 
  4092 lemma has_integral_component_le:
  4093   fixes f g :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4094   assumes k: "k \<in> Basis"
  4095   assumes "(f has_integral i) s" "(g has_integral j) s"
  4096     and "\<forall>x\<in>s. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  4097   shows "i\<bullet>k \<le> j\<bullet>k"
  4098 proof -
  4099   have lem: "i\<bullet>k \<le> j\<bullet>k"
  4100     if f_i: "(f has_integral i) (cbox a b)"
  4101     and g_j: "(g has_integral j) (cbox a b)"
  4102     and le: "\<forall>x\<in>cbox a b. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  4103     for a b i and j :: 'b and f g :: "'a \<Rightarrow> 'b"
  4104   proof (rule ccontr)
  4105     assume "\<not> ?thesis"
  4106     then have *: "0 < (i\<bullet>k - j\<bullet>k) / 3"
  4107       by auto
  4108     guess d1 using f_i[unfolded has_integral,rule_format,OF *] by (elim exE conjE) note d1=this[rule_format]
  4109     guess d2 using g_j[unfolded has_integral,rule_format,OF *] by (elim exE conjE) note d2=this[rule_format]
  4110     obtain p where p: "p tagged_division_of cbox a b" "d1 fine p" "d2 fine p"
  4111        using fine_division_exists[OF gauge_inter[OF d1(1) d2(1)], of a b] unfolding fine_inter
  4112        by metis
  4113     note le_less_trans[OF Basis_le_norm[OF k]]
  4114     then have "\<bar>((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i) \<bullet> k\<bar> < (i \<bullet> k - j \<bullet> k) / 3"
  4115               "\<bar>((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - j) \<bullet> k\<bar> < (i \<bullet> k - j \<bullet> k) / 3"
  4116       using  k norm_bound_Basis_lt d1 d2 p
  4117       by blast+
  4118     then show False
  4119       unfolding inner_simps
  4120       using rsum_component_le[OF p(1) le]
  4121       by (simp add: abs_real_def split: if_split_asm)
  4122   qed
  4123   show ?thesis
  4124   proof (cases "\<exists>a b. s = cbox a b")
  4125     case True
  4126     with lem assms show ?thesis
  4127       by auto
  4128   next
  4129     case False
  4130     show ?thesis
  4131     proof (rule ccontr)
  4132       assume "\<not> i\<bullet>k \<le> j\<bullet>k"
  4133       then have ij: "(i\<bullet>k - j\<bullet>k) / 3 > 0"
  4134         by auto
  4135       note has_integral_altD[OF _ False this]
  4136       from this[OF assms(2)] this[OF assms(3)] guess B1 B2 . note B=this[rule_format]
  4137       have "bounded (ball 0 B1 \<union> ball (0::'a) B2)"
  4138         unfolding bounded_Un by(rule conjI bounded_ball)+
  4139       from bounded_subset_cbox[OF this] guess a b by (elim exE)
  4140       note ab = conjunctD2[OF this[unfolded Un_subset_iff]]
  4141       guess w1 using B(2)[OF ab(1)] .. note w1=conjunctD2[OF this]
  4142       guess w2 using B(4)[OF ab(2)] .. note w2=conjunctD2[OF this]
  4143       have *: "\<And>w1 w2 j i::real .\<bar>w1 - i\<bar> < (i - j) / 3 \<Longrightarrow> \<bar>w2 - j\<bar> < (i - j) / 3 \<Longrightarrow> w1 \<le> w2 \<Longrightarrow> False"
  4144         by (simp add: abs_real_def split: if_split_asm)
  4145       note le_less_trans[OF Basis_le_norm[OF k]]
  4146       note this[OF w1(2)] this[OF w2(2)]
  4147       moreover
  4148       have "w1\<bullet>k \<le> w2\<bullet>k"
  4149         by (rule lem[OF w1(1) w2(1)]) (simp add: assms(4))
  4150       ultimately show False
  4151         unfolding inner_simps by(rule *)
  4152     qed
  4153   qed
  4154 qed
  4155 
  4156 lemma integral_component_le:
  4157   fixes g f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4158   assumes "k \<in> Basis"
  4159     and "f integrable_on s" "g integrable_on s"
  4160     and "\<forall>x\<in>s. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  4161   shows "(integral s f)\<bullet>k \<le> (integral s g)\<bullet>k"
  4162   apply (rule has_integral_component_le)
  4163   using integrable_integral assms
  4164   apply auto
  4165   done
  4166 
  4167 lemma has_integral_component_nonneg:
  4168   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4169   assumes "k \<in> Basis"
  4170     and "(f has_integral i) s"
  4171     and "\<forall>x\<in>s. 0 \<le> (f x)\<bullet>k"
  4172   shows "0 \<le> i\<bullet>k"
  4173   using has_integral_component_le[OF assms(1) has_integral_0 assms(2)]
  4174   using assms(3-)
  4175   by auto
  4176 
  4177 lemma integral_component_nonneg:
  4178   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4179   assumes "k \<in> Basis"
  4180     and  "\<forall>x\<in>s. 0 \<le> (f x)\<bullet>k"
  4181   shows "0 \<le> (integral s f)\<bullet>k"
  4182 proof (cases "f integrable_on s")
  4183   case True show ?thesis
  4184     apply (rule has_integral_component_nonneg)
  4185     using assms True
  4186     apply auto
  4187     done
  4188 next
  4189   case False then show ?thesis by (simp add: not_integrable_integral)
  4190 qed
  4191 
  4192 lemma has_integral_component_neg:
  4193   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4194   assumes "k \<in> Basis"
  4195     and "(f has_integral i) s"
  4196     and "\<forall>x\<in>s. (f x)\<bullet>k \<le> 0"
  4197   shows "i\<bullet>k \<le> 0"
  4198   using has_integral_component_le[OF assms(1,2) has_integral_0] assms(2-)
  4199   by auto
  4200 
  4201 lemma has_integral_component_lbound:
  4202   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4203   assumes "(f has_integral i) (cbox a b)"
  4204     and "\<forall>x\<in>cbox a b. B \<le> f(x)\<bullet>k"
  4205     and "k \<in> Basis"
  4206   shows "B * content (cbox a b) \<le> i\<bullet>k"
  4207   using has_integral_component_le[OF assms(3) has_integral_const assms(1),of "(\<Sum>i\<in>Basis. B *\<^sub>R i)::'b"] assms(2-)
  4208   by (auto simp add: field_simps)
  4209 
  4210 lemma has_integral_component_ubound:
  4211   fixes f::"'a::euclidean_space => 'b::euclidean_space"
  4212   assumes "(f has_integral i) (cbox a b)"
  4213     and "\<forall>x\<in>cbox a b. f x\<bullet>k \<le> B"
  4214     and "k \<in> Basis"
  4215   shows "i\<bullet>k \<le> B * content (cbox a b)"
  4216   using has_integral_component_le[OF assms(3,1) has_integral_const, of "\<Sum>i\<in>Basis. B *\<^sub>R i"] assms(2-)
  4217   by (auto simp add: field_simps)
  4218 
  4219 lemma integral_component_lbound:
  4220   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4221   assumes "f integrable_on cbox a b"
  4222     and "\<forall>x\<in>cbox a b. B \<le> f(x)\<bullet>k"
  4223     and "k \<in> Basis"
  4224   shows "B * content (cbox a b) \<le> (integral(cbox a b) f)\<bullet>k"
  4225   apply (rule has_integral_component_lbound)
  4226   using assms
  4227   unfolding has_integral_integral
  4228   apply auto
  4229   done
  4230 
  4231 lemma integral_component_lbound_real:
  4232   assumes "f integrable_on {a ::real .. b}"
  4233     and "\<forall>x\<in>{a .. b}. B \<le> f(x)\<bullet>k"
  4234     and "k \<in> Basis"
  4235   shows "B * content {a .. b} \<le> (integral {a .. b} f)\<bullet>k"
  4236   using assms
  4237   by (metis box_real(2) integral_component_lbound)
  4238 
  4239 lemma integral_component_ubound:
  4240   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4241   assumes "f integrable_on cbox a b"
  4242     and "\<forall>x\<in>cbox a b. f x\<bullet>k \<le> B"
  4243     and "k \<in> Basis"
  4244   shows "(integral (cbox a b) f)\<bullet>k \<le> B * content (cbox a b)"
  4245   apply (rule has_integral_component_ubound)
  4246   using assms
  4247   unfolding has_integral_integral
  4248   apply auto
  4249   done
  4250 
  4251 lemma integral_component_ubound_real:
  4252   fixes f :: "real \<Rightarrow> 'a::euclidean_space"
  4253   assumes "f integrable_on {a .. b}"
  4254     and "\<forall>x\<in>{a .. b}. f x\<bullet>k \<le> B"
  4255     and "k \<in> Basis"
  4256   shows "(integral {a .. b} f)\<bullet>k \<le> B * content {a .. b}"
  4257   using assms
  4258   by (metis box_real(2) integral_component_ubound)
  4259 
  4260 subsection \<open>Uniform limit of integrable functions is integrable.\<close>
  4261 
  4262 lemma real_arch_invD:
  4263   "0 < (e::real) \<Longrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
  4264   by (subst(asm) real_arch_inverse)
  4265 
  4266 lemma integrable_uniform_limit:
  4267   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4268   assumes "\<forall>e>0. \<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  4269   shows "f integrable_on cbox a b"
  4270 proof (cases "content (cbox a b) > 0")
  4271   case False then show ?thesis
  4272       using has_integral_null
  4273       by (simp add: content_lt_nz integrable_on_def)
  4274 next
  4275   case True
  4276   have *: "\<And>P. \<forall>e>(0::real). P e \<Longrightarrow> \<forall>n::nat. P (inverse (real n + 1))"
  4277     by auto
  4278   from choice[OF *[OF assms]] guess g .. note g=conjunctD2[OF this[rule_format],rule_format]
  4279   from choice[OF allI[OF g(2)[unfolded integrable_on_def], of "\<lambda>x. x"]]
  4280   obtain i where i: "\<And>x. (g x has_integral i x) (cbox a b)"
  4281       by auto
  4282   have "Cauchy i"
  4283     unfolding Cauchy_def
  4284   proof clarify
  4285     fix e :: real
  4286     assume "e>0"
  4287     then have "e / 4 / content (cbox a b) > 0"
  4288       using True by (auto simp add: field_simps)
  4289     then obtain M :: nat
  4290          where M: "M \<noteq> 0" "0 < inverse (real_of_nat M)" "inverse (of_nat M) < e / 4 / content (cbox a b)"
  4291       by (subst (asm) real_arch_inverse) auto
  4292     show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (i m) (i n) < e"
  4293     proof (rule exI [where x=M], clarify)
  4294       fix m n
  4295       assume m: "M \<le> m" and n: "M \<le> n"
  4296       have "e/4>0" using \<open>e>0\<close> by auto
  4297       note * = i[unfolded has_integral,rule_format,OF this]
  4298       from *[of m] guess gm by (elim conjE exE) note gm=this[rule_format]
  4299       from *[of n] guess gn by (elim conjE exE) note gn=this[rule_format]
  4300       from fine_division_exists[OF gauge_inter[OF gm(1) gn(1)], of a b]
  4301       obtain p where p: "p tagged_division_of cbox a b" "(\<lambda>x. gm x \<inter> gn x) fine p"
  4302         by auto
  4303       { fix s1 s2 i1 and i2::'b
  4304         assume no: "norm(s2 - s1) \<le> e/2" "norm (s1 - i1) < e/4" "norm (s2 - i2) < e/4"
  4305         have "norm (i1 - i2) \<le> norm (i1 - s1) + norm (s1 - s2) + norm (s2 - i2)"
  4306           using norm_triangle_ineq[of "i1 - s1" "s1 - i2"]
  4307           using norm_triangle_ineq[of "s1 - s2" "s2 - i2"]
  4308           by (auto simp add: algebra_simps)
  4309         also have "\<dots> < e"
  4310           using no
  4311           unfolding norm_minus_commute
  4312           by (auto simp add: algebra_simps)
  4313         finally have "norm (i1 - i2) < e" .
  4314       } note triangle3 = this
  4315       have finep: "gm fine p" "gn fine p"
  4316         using fine_inter p  by auto
  4317       { fix x
  4318         assume x: "x \<in> cbox a b"
  4319         have "norm (f x - g n x) + norm (f x - g m x) \<le> inverse (real n + 1) + inverse (real m + 1)"
  4320           using g(1)[OF x, of n] g(1)[OF x, of m] by auto
  4321         also have "\<dots> \<le> inverse (real M) + inverse (real M)"
  4322           apply (rule add_mono)
  4323           using M(2) m n by auto
  4324         also have "\<dots> = 2 / real M"
  4325           unfolding divide_inverse by auto
  4326         finally have "norm (g n x - g m x) \<le> 2 / real M"
  4327           using norm_triangle_le[of "g n x - f x" "f x - g m x" "2 / real M"]
  4328           by (auto simp add: algebra_simps simp add: norm_minus_commute)
  4329       } note norm_le = this
  4330       have le_e2: "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g n x) - (\<Sum>(x, k)\<in>p. content k *\<^sub>R g m x)) \<le> e / 2"
  4331         apply (rule order_trans [OF rsum_diff_bound[OF p(1), where e="2 / real M"]])
  4332         apply (blast intro: norm_le)
  4333         using M True
  4334         by (auto simp add: field_simps)
  4335       then show "dist (i m) (i n) < e"
  4336         unfolding dist_norm
  4337         using gm gn p finep
  4338         by (auto intro!: triangle3)
  4339     qed
  4340   qed
  4341   then obtain s where s: "i \<longlonglongrightarrow> s"
  4342     using convergent_eq_cauchy[symmetric] by blast
  4343   show ?thesis
  4344     unfolding integrable_on_def has_integral
  4345   proof (rule_tac x=s in exI, clarify)
  4346     fix e::real
  4347     assume e: "0 < e"
  4348     then have *: "e/3 > 0" by auto
  4349     then obtain N1 where N1: "\<forall>n\<ge>N1. norm (i n - s) < e / 3"
  4350       using LIMSEQ_D [OF s] by metis
  4351     from e True have "e / 3 / content (cbox a b) > 0"
  4352       by (auto simp add: field_simps)
  4353     from real_arch_invD[OF this] guess N2 by (elim exE conjE) note N2=this
  4354     from i[of "N1 + N2",unfolded has_integral,rule_format,OF *] guess g' .. note g'=conjunctD2[OF this,rule_format]
  4355     { fix sf sg i
  4356       assume no: "norm (sf - sg) \<le> e / 3"
  4357                  "norm(i - s) < e / 3"
  4358                  "norm (sg - i) < e / 3"
  4359       have "norm (sf - s) \<le> norm (sf - sg) + norm (sg - i) + norm (i - s)"
  4360         using norm_triangle_ineq[of "sf - sg" "sg - s"]
  4361         using norm_triangle_ineq[of "sg -  i" " i - s"]
  4362         by (auto simp add: algebra_simps)
  4363       also have "\<dots> < e"
  4364         using no
  4365         unfolding norm_minus_commute
  4366         by (auto simp add: algebra_simps)
  4367       finally have "norm (sf - s) < e" .
  4368     } note lem = this
  4369     { fix p
  4370       assume p: "p tagged_division_of (cbox a b) \<and> g' fine p"
  4371       then have norm_less: "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g (N1 + N2) x) - i (N1 + N2)) < e / 3"
  4372         using g' by blast
  4373       have "content (cbox a b) < e / 3 * (of_nat N2)"
  4374         using N2 unfolding inverse_eq_divide using True by (auto simp add: field_simps)
  4375       moreover have "e / 3 * of_nat N2 \<le> e / 3 * (of_nat (N1 + N2) + 1)"
  4376         using \<open>e>0\<close> by auto
  4377       ultimately have "content (cbox a b) < e / 3 * (of_nat (N1 + N2) + 1)"
  4378         by linarith
  4379       then have le_e3: "inverse (real (N1 + N2) + 1) * content (cbox a b) \<le> e / 3"
  4380         unfolding inverse_eq_divide
  4381         by (auto simp add: field_simps)
  4382       have ne3: "norm (i (N1 + N2) - s) < e / 3"
  4383         using N1 by auto
  4384       have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - s) < e"
  4385         apply (rule lem[OF order_trans [OF _ le_e3] ne3 norm_less])
  4386         apply (rule rsum_diff_bound[OF p[THEN conjunct1]])
  4387         apply (blast intro: g)
  4388         done }
  4389     then show "\<exists>d. gauge d \<and>
  4390              (\<forall>p. p tagged_division_of cbox a b \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - s) < e)"
  4391       by (blast intro: g')
  4392   qed
  4393 qed
  4394 
  4395 lemmas integrable_uniform_limit_real = integrable_uniform_limit [where 'a=real, simplified]
  4396 
  4397 
  4398 subsection \<open>Negligible sets.\<close>
  4399 
  4400 definition "negligible (s:: 'a::euclidean_space set) \<longleftrightarrow>
  4401   (\<forall>a b. ((indicator s :: 'a\<Rightarrow>real) has_integral 0) (cbox a b))"
  4402 
  4403 
  4404 subsection \<open>Negligibility of hyperplane.\<close>
  4405 
  4406 lemma interval_doublesplit:
  4407   fixes a :: "'a::euclidean_space"
  4408   assumes "k \<in> Basis"
  4409   shows "cbox a b \<inter> {x . \<bar>x\<bullet>k - c\<bar> \<le> (e::real)} =
  4410     cbox (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) (c - e) else a\<bullet>i) *\<^sub>R i)
  4411      (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) (c + e) else b\<bullet>i) *\<^sub>R i)"
  4412 proof -
  4413   have *: "\<And>x c e::real. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  4414     by auto
  4415   have **: "\<And>s P Q. s \<inter> {x. P x \<and> Q x} = (s \<inter> {x. Q x}) \<inter> {x. P x}"
  4416     by blast
  4417   show ?thesis
  4418     unfolding * ** interval_split[OF assms] by (rule refl)
  4419 qed
  4420 
  4421 lemma division_doublesplit:
  4422   fixes a :: "'a::euclidean_space"
  4423   assumes "p division_of (cbox a b)"
  4424     and k: "k \<in> Basis"
  4425   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> {}}
  4426          division_of  (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> e})"
  4427 proof -
  4428   have *: "\<And>x c. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  4429     by auto
  4430   have **: "\<And>p q p' q'. p division_of q \<Longrightarrow> p = p' \<Longrightarrow> q = q' \<Longrightarrow> p' division_of q'"
  4431     by auto
  4432   note division_split(1)[OF assms, where c="c+e",unfolded interval_split[OF k]]
  4433   note division_split(2)[OF this, where c="c-e" and k=k,OF k]
  4434   then show ?thesis
  4435     apply (rule **)
  4436     subgoal
  4437       apply (simp add: abs_diff_le_iff field_simps Collect_conj_eq setcompr_eq_image[symmetric])
  4438       apply (rule equalityI)
  4439       apply blast
  4440       apply clarsimp
  4441       apply (rule_tac x="l \<inter> {x. c + e \<ge> x \<bullet> k}" in exI)
  4442       apply auto
  4443       done
  4444     by (simp add: interval_split k interval_doublesplit)
  4445 qed
  4446 
  4447 lemma content_doublesplit:
  4448   fixes a :: "'a::euclidean_space"
  4449   assumes "0 < e"
  4450     and k: "k \<in> Basis"
  4451   obtains d where "0 < d" and "content (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d}) < e"
  4452 proof (cases "content (cbox a b) = 0")
  4453   case True
  4454   then have ce: "content (cbox a b) < e"
  4455     by (metis \<open>0 < e\<close>)
  4456   show ?thesis
  4457     apply (rule that[of 1])
  4458     apply simp
  4459     unfolding interval_doublesplit[OF k]
  4460     apply (rule le_less_trans[OF content_subset ce])
  4461     apply (auto simp: interval_doublesplit[symmetric] k)
  4462     done
  4463 next
  4464   case False
  4465   define d where "d = e / 3 / setprod (\<lambda>i. b\<bullet>i - a\<bullet>i) (Basis - {k})"
  4466   note False[unfolded content_eq_0 not_ex not_le, rule_format]
  4467   then have "\<And>x. x \<in> Basis \<Longrightarrow> b\<bullet>x > a\<bullet>x"
  4468     by (auto simp add:not_le)
  4469   then have prod0: "0 < setprod (\<lambda>i. b\<bullet>i - a\<bullet>i) (Basis - {k})"
  4470     by (force simp add: setprod_pos field_simps)
  4471   then have "d > 0"
  4472     using assms
  4473     by (auto simp add: d_def field_simps)
  4474   then show ?thesis
  4475   proof (rule that[of d])
  4476     have *: "Basis = insert k (Basis - {k})"
  4477       using k by auto
  4478     have less_e: "(min (b \<bullet> k) (c + d) - max (a \<bullet> k) (c - d)) * (\<Prod>i\<in>Basis - {k}. b \<bullet> i - a \<bullet> i) < e"
  4479     proof -
  4480       have "(min (b \<bullet> k) (c + d) - max (a \<bullet> k) (c - d)) \<le> 2 * d"
  4481         by auto
  4482       also have "\<dots> < e / (\<Prod>i\<in>Basis - {k}. b \<bullet> i - a \<bullet> i)"
  4483         unfolding d_def
  4484         using assms prod0
  4485         by (auto simp add: field_simps)
  4486       finally show "(min (b \<bullet> k) (c + d) - max (a \<bullet> k) (c - d)) * (\<Prod>i\<in>Basis - {k}. b \<bullet> i - a \<bullet> i) < e"
  4487         unfolding pos_less_divide_eq[OF prod0] .
  4488     qed
  4489     show "content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) < e"
  4490     proof (cases "cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} = {}")
  4491       case True
  4492       then show ?thesis
  4493         using assms by simp
  4494     next
  4495       case False
  4496       then have
  4497           "(\<Prod>i\<in>Basis - {k}. interval_upperbound (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<bullet> i -
  4498                 interval_lowerbound (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<bullet> i)
  4499            = (\<Prod>i\<in>Basis - {k}. b\<bullet>i - a\<bullet>i)"
  4500         by (simp add: box_eq_empty interval_doublesplit[OF k])
  4501       then show "content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) < e"
  4502         unfolding content_def
  4503         using assms False
  4504         apply (subst *)
  4505         apply (subst setprod.insert)
  4506         apply (simp_all add: interval_doublesplit[OF k] box_eq_empty not_less less_e)
  4507         done
  4508     qed
  4509   qed
  4510 qed
  4511 
  4512 lemma negligible_standard_hyperplane[intro]:
  4513   fixes k :: "'a::euclidean_space"
  4514   assumes k: "k \<in> Basis"
  4515   shows "negligible {x. x\<bullet>k = c}"
  4516   unfolding negligible_def has_integral
  4517 proof (clarify, goal_cases)
  4518   case (1 a b e)
  4519   from this and k obtain d where d: "0 < d" "content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) < e"
  4520     by (rule content_doublesplit)
  4521   let ?i = "indicator {x::'a. x\<bullet>k = c} :: 'a\<Rightarrow>real"
  4522   show ?case
  4523     apply (rule_tac x="\<lambda>x. ball x d" in exI)
  4524     apply rule
  4525     apply (rule gauge_ball)
  4526     apply (rule d)
  4527   proof (rule, rule)
  4528     fix p
  4529     assume p: "p tagged_division_of (cbox a b) \<and> (\<lambda>x. ball x d) fine p"
  4530     have *: "(\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) =
  4531       (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d}) *\<^sub>R ?i x)"
  4532       apply (rule setsum.cong)
  4533       apply (rule refl)
  4534       unfolding split_paired_all real_scaleR_def mult_cancel_right split_conv
  4535       apply cases
  4536       apply (rule disjI1)
  4537       apply assumption
  4538       apply (rule disjI2)
  4539     proof -
  4540       fix x l
  4541       assume as: "(x, l) \<in> p" "?i x \<noteq> 0"
  4542       then have xk: "x\<bullet>k = c"
  4543         unfolding indicator_def
  4544         apply -
  4545         apply (rule ccontr)
  4546         apply auto
  4547         done
  4548       show "content l = content (l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})"
  4549         apply (rule arg_cong[where f=content])
  4550         apply (rule set_eqI)
  4551         apply rule
  4552         apply rule
  4553         unfolding mem_Collect_eq
  4554       proof -
  4555         fix y
  4556         assume y: "y \<in> l"
  4557         note p[THEN conjunct2,unfolded fine_def,rule_format,OF as(1),unfolded split_conv]
  4558         note this[unfolded subset_eq mem_ball dist_norm,rule_format,OF y]
  4559         note le_less_trans[OF Basis_le_norm[OF k] this]
  4560         then show "\<bar>y \<bullet> k - c\<bar> \<le> d"
  4561           unfolding inner_simps xk by auto
  4562       qed auto
  4563     qed
  4564     note p'= tagged_division_ofD[OF p[THEN conjunct1]] and p''=division_of_tagged_division[OF p[THEN conjunct1]]
  4565     show "norm ((\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) - 0) < e"
  4566       unfolding diff_0_right *
  4567       unfolding real_scaleR_def real_norm_def
  4568       apply (subst abs_of_nonneg)
  4569       apply (rule setsum_nonneg)
  4570       apply rule
  4571       unfolding split_paired_all split_conv
  4572       apply (rule mult_nonneg_nonneg)
  4573       apply (drule p'(4))
  4574       apply (erule exE)+
  4575       apply(rule_tac b=b in back_subst)
  4576       prefer 2
  4577       apply (subst(asm) eq_commute)
  4578       apply assumption
  4579       apply (subst interval_doublesplit[OF k])
  4580       apply (rule content_pos_le)
  4581       apply (rule indicator_pos_le)
  4582     proof -
  4583       have "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) * ?i x) \<le>
  4584         (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}))"
  4585         apply (rule setsum_mono)
  4586         unfolding split_paired_all split_conv
  4587         apply (rule mult_right_le_one_le)
  4588         apply (drule p'(4))
  4589         apply (auto simp add:interval_doublesplit[OF k])
  4590         done
  4591       also have "\<dots> < e"
  4592       proof (subst setsum.over_tagged_division_lemma[OF p[THEN conjunct1]], goal_cases)
  4593         case prems: (1 u v)
  4594         have "content (cbox u v \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<le> content (cbox u v)"
  4595           unfolding interval_doublesplit[OF k]
  4596           apply (rule content_subset)
  4597           unfolding interval_doublesplit[symmetric,OF k]
  4598           apply auto
  4599           done
  4600         then show ?case
  4601           unfolding prems interval_doublesplit[OF k]
  4602           by (blast intro: antisym)
  4603       next
  4604         have "(\<Sum>l\<in>snd ` p. content (l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) =
  4605           setsum content ((\<lambda>l. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})`{l\<in>snd ` p. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}})"
  4606         proof (subst (2) setsum.reindex_nontrivial)
  4607           fix x y assume "x \<in> {l \<in> snd ` p. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}}" "y \<in> {l \<in> snd ` p. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}}"
  4608             "x \<noteq> y" and eq: "x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} = y \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}"
  4609           then obtain x' y' where "(x', x) \<in> p" "x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}" "(y', y) \<in> p" "y \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}"
  4610             by (auto)
  4611           from p'(5)[OF \<open>(x', x) \<in> p\<close> \<open>(y', y) \<in> p\<close>] \<open>x \<noteq> y\<close> have "interior (x \<inter> y) = {}"
  4612             by auto
  4613           moreover have "interior ((x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<inter> (y \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) \<subseteq> interior (x \<inter> y)"
  4614             by (auto intro: interior_mono)
  4615           ultimately have "interior (x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) = {}"
  4616             by (auto simp: eq)
  4617           then show "content (x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) = 0"
  4618             using p'(4)[OF \<open>(x', x) \<in> p\<close>] by (auto simp: interval_doublesplit[OF k] content_eq_0_interior simp del: interior_Int)
  4619         qed (insert p'(1), auto intro!: setsum.mono_neutral_right)
  4620         also have "\<dots> \<le> norm (\<Sum>l\<in>(\<lambda>l. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})`{l\<in>snd ` p. l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} \<noteq> {}}. content l *\<^sub>R 1::real)"
  4621           by simp
  4622         also have "\<dots> \<le> 1 * content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})"
  4623           using division_doublesplit[OF p'' k, unfolded interval_doublesplit[OF k]]
  4624           unfolding interval_doublesplit[OF k] by (intro dsum_bound) auto
  4625         also have "\<dots> < e"
  4626           using d(2) by simp
  4627         finally show "(\<Sum>ka\<in>snd ` p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) < e" .
  4628       qed
  4629       finally show "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) * ?i x) < e" .
  4630     qed
  4631   qed
  4632 qed
  4633 
  4634 
  4635 subsection \<open>A technical lemma about "refinement" of division.\<close>
  4636 
  4637 lemma tagged_division_finer:
  4638   fixes p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  4639   assumes "p tagged_division_of (cbox a b)"
  4640     and "gauge d"
  4641   obtains q where "q tagged_division_of (cbox a b)"
  4642     and "d fine q"
  4643     and "\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q"
  4644 proof -
  4645   let ?P = "\<lambda>p. p tagged_partial_division_of (cbox a b) \<longrightarrow> gauge d \<longrightarrow>
  4646     (\<exists>q. q tagged_division_of (\<Union>{k. \<exists>x. (x,k) \<in> p}) \<and> d fine q \<and>
  4647       (\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q))"
  4648   {
  4649     have *: "finite p" "p tagged_partial_division_of (cbox a b)"
  4650       using assms(1)
  4651       unfolding tagged_division_of_def
  4652       by auto
  4653     presume "\<And>p. finite p \<Longrightarrow> ?P p"
  4654     from this[rule_format,OF * assms(2)] guess q .. note q=this
  4655     then show ?thesis
  4656       apply -
  4657       apply (rule that[of q])
  4658       unfolding tagged_division_ofD[OF assms(1)]
  4659       apply auto
  4660       done
  4661   }
  4662   fix p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  4663   assume as: "finite p"
  4664   show "?P p"
  4665     apply rule
  4666     apply rule
  4667     using as
  4668   proof (induct p)
  4669     case empty
  4670     show ?case
  4671       apply (rule_tac x="{}" in exI)
  4672       unfolding fine_def
  4673       apply auto
  4674       done
  4675   next
  4676     case (insert xk p)
  4677     guess x k using surj_pair[of xk] by (elim exE) note xk=this
  4678     note tagged_partial_division_subset[OF insert(4) subset_insertI]
  4679     from insert(3)[OF this insert(5)] guess q1 .. note q1 = conjunctD3[OF this]
  4680     have *: "\<Union>{l. \<exists>y. (y,l) \<in> insert xk p} = k \<union> \<Union>{l. \<exists>y. (y,l) \<in> p}"
  4681       unfolding xk by auto
  4682     note p = tagged_partial_division_ofD[OF insert(4)]
  4683     from p(4)[unfolded xk, OF insertI1] guess u v by (elim exE) note uv=this
  4684 
  4685     have "finite {k. \<exists>x. (x, k) \<in> p}"
  4686       apply (rule finite_subset[of _ "snd ` p"])
  4687       using p
  4688       apply safe
  4689       apply (metis image_iff snd_conv)
  4690       apply auto
  4691       done
  4692     then have int: "interior (cbox u v) \<inter> interior (\<Union>{k. \<exists>x. (x, k) \<in> p}) = {}"
  4693       apply (rule inter_interior_unions_intervals)
  4694       apply (rule open_interior)
  4695       apply (rule_tac[!] ballI)
  4696       unfolding mem_Collect_eq
  4697       apply (erule_tac[!] exE)
  4698       apply (drule p(4)[OF insertI2])
  4699       apply assumption
  4700       apply (rule p(5))
  4701       unfolding uv xk
  4702       apply (rule insertI1)
  4703       apply (rule insertI2)
  4704       apply assumption
  4705       using insert(2)
  4706       unfolding uv xk
  4707       apply auto
  4708       done
  4709     show ?case
  4710     proof (cases "cbox u v \<subseteq> d x")
  4711       case True
  4712       then show ?thesis
  4713         apply (rule_tac x="{(x,cbox u v)} \<union> q1" in exI)
  4714         apply rule
  4715         unfolding * uv
  4716         apply (rule tagged_division_union)
  4717         apply (rule tagged_division_of_self)
  4718         apply (rule p[unfolded xk uv] insertI1)+
  4719         apply (rule q1)
  4720         apply (rule int)
  4721         apply rule
  4722         apply (rule fine_union)
  4723         apply (subst fine_def)
  4724         defer
  4725         apply (rule q1)
  4726         unfolding Ball_def split_paired_All split_conv
  4727         apply rule
  4728         apply rule
  4729         apply rule
  4730         apply rule
  4731         apply (erule insertE)
  4732         apply (simp add: uv xk)
  4733         apply (rule UnI2)
  4734         apply (drule q1(3)[rule_format])
  4735         unfolding xk uv
  4736         apply auto
  4737         done
  4738     next
  4739       case False
  4740       from fine_division_exists[OF assms(2), of u v] guess q2 . note q2=this
  4741       show ?thesis
  4742         apply (rule_tac x="q2 \<union> q1" in exI)
  4743         apply rule
  4744         unfolding * uv
  4745         apply (rule tagged_division_union q2 q1 int fine_union)+
  4746         unfolding Ball_def split_paired_All split_conv
  4747         apply rule
  4748         apply (rule fine_union)
  4749         apply (rule q1 q2)+
  4750         apply rule
  4751         apply rule
  4752         apply rule
  4753         apply rule
  4754         apply (erule insertE)
  4755         apply (rule UnI2)
  4756         apply (simp add: False uv xk)
  4757         apply (drule q1(3)[rule_format])
  4758         using False
  4759         unfolding xk uv
  4760         apply auto
  4761         done
  4762     qed
  4763   qed
  4764 qed
  4765 
  4766 
  4767 subsection \<open>Hence the main theorem about negligible sets.\<close>
  4768 
  4769 lemma finite_product_dependent:
  4770   assumes "finite s"
  4771     and "\<And>x. x \<in> s \<Longrightarrow> finite (t x)"
  4772   shows "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
  4773   using assms
  4774 proof induct
  4775   case (insert x s)
  4776   have *: "{(i, j) |i j. i \<in> insert x s \<and> j \<in> t i} =
  4777     (\<lambda>y. (x,y)) ` (t x) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  4778   show ?case
  4779     unfolding *
  4780     apply (rule finite_UnI)
  4781     using insert
  4782     apply auto
  4783     done
  4784 qed auto
  4785 
  4786 lemma sum_sum_product:
  4787   assumes "finite s"
  4788     and "\<forall>i\<in>s. finite (t i)"
  4789   shows "setsum (\<lambda>i. setsum (x i) (t i)::real) s =
  4790     setsum (\<lambda>(i,j). x i j) {(i,j) | i j. i \<in> s \<and> j \<in> t i}"
  4791   using assms
  4792 proof induct
  4793   case (insert a s)
  4794   have *: "{(i, j) |i j. i \<in> insert a s \<and> j \<in> t i} =
  4795     (\<lambda>y. (a,y)) ` (t a) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  4796   show ?case
  4797     unfolding *
  4798     apply (subst setsum.union_disjoint)
  4799     unfolding setsum.insert[OF insert(1-2)]
  4800     prefer 4
  4801     apply (subst insert(3))
  4802     unfolding add_right_cancel
  4803   proof -
  4804     show "setsum (x a) (t a) = (\<Sum>(xa, y)\<in> Pair a ` t a. x xa y)"
  4805       apply (subst setsum.reindex)
  4806       unfolding inj_on_def
  4807       apply auto
  4808       done
  4809     show "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
  4810       apply (rule finite_product_dependent)
  4811       using insert
  4812       apply auto
  4813       done
  4814   qed (insert insert, auto)
  4815 qed auto
  4816 
  4817 lemma has_integral_negligible:
  4818   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  4819   assumes "negligible s"
  4820     and "\<forall>x\<in>(t - s). f x = 0"
  4821   shows "(f has_integral 0) t"
  4822 proof -
  4823   presume P: "\<And>f::'b::euclidean_space \<Rightarrow> 'a.
  4824     \<And>a b. \<forall>x. x \<notin> s \<longrightarrow> f x = 0 \<Longrightarrow> (f has_integral 0) (cbox a b)"
  4825   let ?f = "(\<lambda>x. if x \<in> t then f x else 0)"
  4826   show ?thesis
  4827     apply (rule_tac f="?f" in has_integral_eq)
  4828     unfolding if_P
  4829     apply (rule refl)
  4830     apply (subst has_integral_alt)
  4831     apply cases
  4832     apply (subst if_P, assumption)
  4833     unfolding if_not_P
  4834   proof -
  4835     assume "\<exists>a b. t = cbox a b"
  4836     then guess a b apply - by (erule exE)+ note t = this
  4837     show "(?f has_integral 0) t"
  4838       unfolding t
  4839       apply (rule P)
  4840       using assms(2)
  4841       unfolding t
  4842       apply auto
  4843       done
  4844   next
  4845     show "\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  4846       (\<exists>z. ((\<lambda>x. if x \<in> t then ?f x else 0) has_integral z) (cbox a b) \<and> norm (z - 0) < e)"
  4847       apply safe
  4848       apply (rule_tac x=1 in exI)
  4849       apply rule
  4850       apply (rule zero_less_one)
  4851       apply safe
  4852       apply (rule_tac x=0 in exI)
  4853       apply rule
  4854       apply (rule P)
  4855       using assms(2)
  4856       apply auto
  4857       done
  4858   qed
  4859 next
  4860   fix f :: "'b \<Rightarrow> 'a"
  4861   fix a b :: 'b
  4862   assume assm: "\<forall>x. x \<notin> s \<longrightarrow> f x = 0"
  4863   show "(f has_integral 0) (cbox a b)"
  4864     unfolding has_integral
  4865   proof (safe, goal_cases)
  4866     case prems: (1 e)
  4867     then have "\<And>n. e / 2 / ((real n+1) * (2 ^ n)) > 0"
  4868       apply -
  4869       apply (rule divide_pos_pos)
  4870       defer
  4871       apply (rule mult_pos_pos)
  4872       apply (auto simp add:field_simps)
  4873       done
  4874     note assms(1)[unfolded negligible_def has_integral,rule_format,OF this,of a b]
  4875     note allI[OF this,of "\<lambda>x. x"]
  4876     from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format]]
  4877     show ?case
  4878       apply (rule_tac x="\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x" in exI)
  4879     proof safe
  4880       show "gauge (\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x)"
  4881         using d(1) unfolding gauge_def by auto
  4882       fix p
  4883       assume as: "p tagged_division_of (cbox a b)" "(\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x) fine p"
  4884       let ?goal = "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  4885       {
  4886         presume "p \<noteq> {} \<Longrightarrow> ?goal"
  4887         then show ?goal
  4888           apply (cases "p = {}")
  4889           using prems
  4890           apply auto
  4891           done
  4892       }
  4893       assume as': "p \<noteq> {}"
  4894       from real_arch_simple[of "Max((\<lambda>(x,k). norm(f x)) ` p)"] guess N ..
  4895       then have N: "\<forall>x\<in>(\<lambda>(x, k). norm (f x)) ` p. x \<le> real N"
  4896         by (meson Max_ge as(1) dual_order.trans finite_imageI tagged_division_of_finite)
  4897       have "\<forall>i. \<exists>q. q tagged_division_of (cbox a b) \<and> (d i) fine q \<and> (\<forall>(x, k)\<in>p. k \<subseteq> (d i) x \<longrightarrow> (x, k) \<in> q)"
  4898         by (auto intro: tagged_division_finer[OF as(1) d(1)])
  4899       from choice[OF this] guess q .. note q=conjunctD3[OF this[rule_format]]
  4900       have *: "\<And>i. (\<Sum>(x, k)\<in>q i. content k *\<^sub>R indicator s x) \<ge> (0::real)"
  4901         apply (rule setsum_nonneg)
  4902         apply safe
  4903         unfolding real_scaleR_def
  4904         apply (drule tagged_division_ofD(4)[OF q(1)])
  4905         apply (auto intro: mult_nonneg_nonneg)
  4906         done
  4907       have **: "finite s \<Longrightarrow> finite t \<Longrightarrow> (\<forall>(x,y) \<in> t. (0::real) \<le> g(x,y)) \<Longrightarrow>
  4908         (\<forall>y\<in>s. \<exists>x. (x,y) \<in> t \<and> f(y) \<le> g(x,y)) \<Longrightarrow> setsum f s \<le> setsum g t" for f g s t
  4909         apply (rule setsum_le_included[of s t g snd f])
  4910         prefer 4
  4911         apply safe
  4912         apply (erule_tac x=x in ballE)
  4913         apply (erule exE)
  4914         apply (rule_tac x="(xa,x)" in bexI)
  4915         apply auto
  4916         done
  4917       have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) \<le> setsum (\<lambda>i. (real i + 1) *
  4918         norm (setsum (\<lambda>(x,k). content k *\<^sub>R indicator s x :: real) (q i))) {..N+1}"
  4919         unfolding real_norm_def setsum_right_distrib abs_of_nonneg[OF *] diff_0_right
  4920         apply (rule order_trans)
  4921         apply (rule norm_setsum)
  4922         apply (subst sum_sum_product)
  4923         prefer 3
  4924       proof (rule **, safe)
  4925         show "finite {(i, j) |i j. i \<in> {..N + 1} \<and> j \<in> q i}"
  4926           apply (rule finite_product_dependent)
  4927           using q
  4928           apply auto
  4929           done
  4930         fix i a b
  4931         assume as'': "(a, b) \<in> q i"
  4932         show "0 \<le> (real i + 1) * (content b *\<^sub>R indicator s a)"
  4933           unfolding real_scaleR_def
  4934           using tagged_division_ofD(4)[OF q(1) as'']
  4935           by (auto intro!: mult_nonneg_nonneg)
  4936       next
  4937         fix i :: nat
  4938         show "finite (q i)"
  4939           using q by auto
  4940       next
  4941         fix x k
  4942         assume xk: "(x, k) \<in> p"
  4943         define n where "n = nat \<lfloor>norm (f x)\<rfloor>"
  4944         have *: "norm (f x) \<in> (\<lambda>(x, k). norm (f x)) ` p"
  4945           using xk by auto
  4946         have nfx: "real n \<le> norm (f x)" "norm (f x) \<le> real n + 1"
  4947           unfolding n_def by auto
  4948         then have "n \<in> {0..N + 1}"
  4949           using N[rule_format,OF *] by auto
  4950         moreover
  4951         note as(2)[unfolded fine_def,rule_format,OF xk,unfolded split_conv]
  4952         note q(3)[rule_format,OF xk,unfolded split_conv,rule_format,OF this]
  4953         note this[unfolded n_def[symmetric]]
  4954         moreover
  4955         have "norm (content k *\<^sub>R f x) \<le> (real n + 1) * (content k * indicator s x)"
  4956         proof (cases "x \<in> s")
  4957           case False
  4958           then show ?thesis
  4959             using assm by auto
  4960         next
  4961           case True
  4962           have *: "content k \<ge> 0"
  4963             using tagged_division_ofD(4)[OF as(1) xk] by auto
  4964           moreover
  4965           have "content k * norm (f x) \<le> content k * (real n + 1)"
  4966             apply (rule mult_mono)
  4967             using nfx *
  4968             apply auto
  4969             done
  4970           ultimately
  4971           show ?thesis
  4972             unfolding abs_mult
  4973             using nfx True
  4974             by (auto simp add: field_simps)
  4975         qed
  4976         ultimately show "\<exists>y. (y, x, k) \<in> {(i, j) |i j. i \<in> {..N + 1} \<and> j \<in> q i} \<and> norm (content k *\<^sub>R f x) \<le>
  4977           (real y + 1) * (content k *\<^sub>R indicator s x)"
  4978           apply (rule_tac x=n in exI)
  4979           apply safe
  4980           apply (rule_tac x=n in exI)
  4981           apply (rule_tac x="(x,k)" in exI)
  4982           apply safe
  4983           apply auto
  4984           done
  4985       qed (insert as, auto)
  4986       also have "\<dots> \<le> setsum (\<lambda>i. e / 2 / 2 ^ i) {..N+1}"
  4987       proof (rule setsum_mono, goal_cases)
  4988         case (1 i)
  4989         then show ?case
  4990           apply (subst mult.commute, subst pos_le_divide_eq[symmetric])
  4991           using d(2)[rule_format, of "q i" i]
  4992           using q[rule_format]
  4993           apply (auto simp add: field_simps)
  4994           done
  4995       qed
  4996       also have "\<dots> < e * inverse 2 * 2"
  4997         unfolding divide_inverse setsum_right_distrib[symmetric]
  4998         apply (rule mult_strict_left_mono)
  4999         unfolding power_inverse [symmetric] lessThan_Suc_atMost[symmetric]
  5000         apply (subst geometric_sum)
  5001         using prems
  5002         apply auto
  5003         done
  5004       finally show "?goal" by auto
  5005     qed
  5006   qed
  5007 qed
  5008 
  5009 lemma has_integral_spike:
  5010   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  5011   assumes "negligible s"
  5012     and "(\<forall>x\<in>(t - s). g x = f x)"
  5013     and "(f has_integral y) t"
  5014   shows "(g has_integral y) t"
  5015 proof -
  5016   {
  5017     fix a b :: 'b
  5018     fix f g :: "'b \<Rightarrow> 'a"
  5019     fix y :: 'a
  5020     assume as: "\<forall>x \<in> cbox a b - s. g x = f x" "(f has_integral y) (cbox a b)"
  5021     have "((\<lambda>x. f x + (g x - f x)) has_integral (y + 0)) (cbox a b)"
  5022       apply (rule has_integral_add[OF as(2)])
  5023       apply (rule has_integral_negligible[OF assms(1)])
  5024       using as
  5025       apply auto
  5026       done
  5027     then have "(g has_integral y) (cbox a b)"
  5028       by auto
  5029   } note * = this
  5030   show ?thesis
  5031     apply (subst has_integral_alt)
  5032     using assms(2-)
  5033     apply -
  5034     apply (rule cond_cases)
  5035     apply safe
  5036     apply (rule *)
  5037     apply assumption+
  5038     apply (subst(asm) has_integral_alt)
  5039     unfolding if_not_P
  5040     apply (erule_tac x=e in allE)
  5041     apply safe
  5042     apply (rule_tac x=B in exI)
  5043     apply safe
  5044     apply (erule_tac x=a in allE)
  5045     apply (erule_tac x=b in allE)
  5046     apply safe
  5047     apply (rule_tac x=z in exI)
  5048     apply safe
  5049     apply (rule *[where fa2="\<lambda>x. if x\<in>t then f x else 0"])
  5050     apply auto
  5051     done
  5052 qed
  5053 
  5054 lemma has_integral_spike_eq:
  5055   assumes "negligible s"
  5056     and "\<forall>x\<in>(t - s). g x = f x"
  5057   shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  5058   apply rule
  5059   apply (rule_tac[!] has_integral_spike[OF assms(1)])
  5060   using assms(2)
  5061   apply auto
  5062   done
  5063 
  5064 lemma integrable_spike:
  5065   assumes "negligible s"
  5066     and "\<forall>x\<in>(t - s). g x = f x"
  5067     and "f integrable_on t"
  5068   shows "g integrable_on  t"
  5069   using assms
  5070   unfolding integrable_on_def
  5071   apply -
  5072   apply (erule exE)
  5073   apply rule
  5074   apply (rule has_integral_spike)
  5075   apply fastforce+
  5076   done
  5077 
  5078 lemma integral_spike:
  5079   assumes "negligible s"
  5080     and "\<forall>x\<in>(t - s). g x = f x"
  5081   shows "integral t f = integral t g"
  5082   using has_integral_spike_eq[OF assms] by (simp add: integral_def integrable_on_def)
  5083 
  5084 
  5085 subsection \<open>Some other trivialities about negligible sets.\<close>
  5086 
  5087 lemma negligible_subset[intro]:
  5088   assumes "negligible s"
  5089     and "t \<subseteq> s"
  5090   shows "negligible t"
  5091   unfolding negligible_def
  5092 proof (safe, goal_cases)
  5093   case (1 a b)
  5094   show ?case
  5095     using assms(1)[unfolded negligible_def,rule_format,of a b]
  5096     apply -
  5097     apply (rule has_integral_spike[OF assms(1)])
  5098     defer
  5099     apply assumption
  5100     using assms(2)
  5101     unfolding indicator_def
  5102     apply auto
  5103     done
  5104 qed
  5105 
  5106 lemma negligible_diff[intro?]:
  5107   assumes "negligible s"
  5108   shows "negligible (s - t)"
  5109   using assms by auto
  5110 
  5111 lemma negligible_Int:
  5112   assumes "negligible s \<or> negligible t"
  5113   shows "negligible (s \<inter> t)"
  5114   using assms by auto
  5115 
  5116 lemma negligible_Un:
  5117   assumes "negligible s"
  5118     and "negligible t"
  5119   shows "negligible (s \<union> t)"
  5120   unfolding negligible_def
  5121 proof (safe, goal_cases)
  5122   case (1 a b)
  5123   note assm = assms[unfolded negligible_def,rule_format,of a b]
  5124   then show ?case
  5125     apply (subst has_integral_spike_eq[OF assms(2)])
  5126     defer
  5127     apply assumption
  5128     unfolding indicator_def
  5129     apply auto
  5130     done
  5131 qed
  5132 
  5133 lemma negligible_Un_eq[simp]: "negligible (s \<union> t) \<longleftrightarrow> negligible s \<and> negligible t"
  5134   using negligible_Un by auto
  5135 
  5136 lemma negligible_sing[intro]: "negligible {a::'a::euclidean_space}"
  5137   using negligible_standard_hyperplane[OF SOME_Basis, of "a \<bullet> (SOME i. i \<in> Basis)"] by auto
  5138 
  5139 lemma negligible_insert[simp]: "negligible (insert a s) \<longleftrightarrow> negligible s"
  5140   apply (subst insert_is_Un)
  5141   unfolding negligible_Un_eq
  5142   apply auto
  5143   done
  5144 
  5145 lemma negligible_empty[iff]: "negligible {}"
  5146   by auto
  5147 
  5148 lemma negligible_finite[intro]:
  5149   assumes "finite s"
  5150   shows "negligible s"
  5151   using assms by (induct s) auto
  5152 
  5153 lemma negligible_Union[intro]:
  5154   assumes "finite s"
  5155     and "\<forall>t\<in>s. negligible t"
  5156   shows "negligible(\<Union>s)"
  5157   using assms by induct auto
  5158 
  5159 lemma negligible:
  5160   "negligible s \<longleftrightarrow> (\<forall>t::('a::euclidean_space) set. ((indicator s::'a\<Rightarrow>real) has_integral 0) t)"
  5161   apply safe
  5162   defer
  5163   apply (subst negligible_def)
  5164 proof -
  5165   fix t :: "'a set"
  5166   assume as: "negligible s"
  5167   have *: "(\<lambda>x. if x \<in> s \<inter> t then 1 else 0) = (\<lambda>x. if x\<in>t then if x\<in>s then 1 else 0 else 0)"
  5168     by auto
  5169   show "((indicator s::'a\<Rightarrow>real) has_integral 0) t"
  5170     apply (subst has_integral_alt)
  5171     apply cases
  5172     apply (subst if_P,assumption)
  5173     unfolding if_not_P
  5174     apply safe
  5175     apply (rule as[unfolded negligible_def,rule_format])
  5176     apply (rule_tac x=1 in exI)
  5177     apply safe
  5178     apply (rule zero_less_one)
  5179     apply (rule_tac x=0 in exI)
  5180     using negligible_subset[OF as,of "s \<inter> t"]
  5181     unfolding negligible_def indicator_def [abs_def]
  5182     unfolding *
  5183     apply auto
  5184     done
  5185 qed auto
  5186 
  5187 
  5188 subsection \<open>Finite case of the spike theorem is quite commonly needed.\<close>
  5189 
  5190 lemma has_integral_spike_finite:
  5191   assumes "finite s"
  5192     and "\<forall>x\<in>t-s. g x = f x"
  5193     and "(f has_integral y) t"
  5194   shows "(g has_integral y) t"
  5195   apply (rule has_integral_spike)
  5196   using assms
  5197   apply auto
  5198   done
  5199 
  5200 lemma has_integral_spike_finite_eq:
  5201   assumes "finite s"
  5202     and "\<forall>x\<in>t-s. g x = f x"
  5203   shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  5204   apply rule
  5205   apply (rule_tac[!] has_integral_spike_finite)
  5206   using assms
  5207   apply auto
  5208   done
  5209 
  5210 lemma integrable_spike_finite:
  5211   assumes "finite s"
  5212     and "\<forall>x\<in>t-s. g x = f x"
  5213     and "f integrable_on t"
  5214   shows "g integrable_on  t"
  5215   using assms
  5216   unfolding integrable_on_def
  5217   apply safe
  5218   apply (rule_tac x=y in exI)
  5219   apply (rule has_integral_spike_finite)
  5220   apply auto
  5221   done
  5222 
  5223 
  5224 subsection \<open>In particular, the boundary of an interval is negligible.\<close>
  5225 
  5226 lemma negligible_frontier_interval: "negligible(cbox (a::'a::euclidean_space) b - box a b)"
  5227 proof -
  5228   let ?A = "\<Union>((\<lambda>k. {x. x\<bullet>k = a\<bullet>k} \<union> {x::'a. x\<bullet>k = b\<bullet>k}) ` Basis)"
  5229   have "cbox a b - box a b \<subseteq> ?A"
  5230     apply rule unfolding Diff_iff mem_box
  5231     apply simp
  5232     apply(erule conjE bexE)+
  5233     apply(rule_tac x=i in bexI)
  5234     apply auto
  5235     done
  5236   then show ?thesis
  5237     apply -
  5238     apply (rule negligible_subset[of ?A])
  5239     apply (rule negligible_Union[OF finite_imageI])
  5240     apply auto
  5241     done
  5242 qed
  5243 
  5244 lemma has_integral_spike_interior:
  5245   assumes "\<forall>x\<in>box a b. g x = f x"
  5246     and "(f has_integral y) (cbox a b)"
  5247   shows "(g has_integral y) (cbox a b)"
  5248   apply (rule has_integral_spike[OF negligible_frontier_interval _ assms(2)])
  5249   using assms(1)
  5250   apply auto
  5251   done
  5252 
  5253 lemma has_integral_spike_interior_eq:
  5254   assumes "\<forall>x\<in>box a b. g x = f x"
  5255   shows "(f has_integral y) (cbox a b) \<longleftrightarrow> (g has_integral y) (cbox a b)"
  5256   apply rule
  5257   apply (rule_tac[!] has_integral_spike_interior)
  5258   using assms
  5259   apply auto
  5260   done
  5261 
  5262 lemma integrable_spike_interior:
  5263   assumes "\<forall>x\<in>box a b. g x = f x"
  5264     and "f integrable_on cbox a b"
  5265   shows "g integrable_on cbox a b"
  5266   using assms
  5267   unfolding integrable_on_def
  5268   using has_integral_spike_interior[OF assms(1)]
  5269   by auto
  5270 
  5271 
  5272 subsection \<open>Integrability of continuous functions.\<close>
  5273 
  5274 lemma operative_approximable:
  5275   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5276   assumes "0 \<le> e"
  5277   shows "comm_monoid.operative op \<and> True (\<lambda>i. \<exists>g. (\<forall>x\<in>i. norm (f x - g (x::'b)) \<le> e) \<and> g integrable_on i)"
  5278   unfolding comm_monoid.operative_def[OF comm_monoid_and]
  5279 proof safe
  5280   fix a b :: 'b
  5281   show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  5282     if "content (cbox a b) = 0"
  5283     apply (rule_tac x=f in exI)
  5284     using assms that
  5285     apply (auto intro!: integrable_on_null)
  5286     done
  5287   {
  5288     fix c g
  5289     fix k :: 'b
  5290     assume as: "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e" "g integrable_on cbox a b"
  5291     assume k: "k \<in> Basis"
  5292     show "\<exists>g. (\<forall>x\<in>cbox a b \<inter> {x. x \<bullet> k \<le> c}. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  5293       "\<exists>g. (\<forall>x\<in>cbox a b \<inter> {x. c \<le> x \<bullet> k}. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k}"
  5294       apply (rule_tac[!] x=g in exI)
  5295       using as(1) integrable_split[OF as(2) k]
  5296       apply auto
  5297       done
  5298   }
  5299   fix c k g1 g2
  5300   assume as: "\<forall>x\<in>cbox a b \<inter> {x. x \<bullet> k \<le> c}. norm (f x - g1 x) \<le> e" "g1 integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  5301     "\<forall>x\<in>cbox a b \<inter> {x. c \<le> x \<bullet> k}. norm (f x - g2 x) \<le> e" "g2 integrable_on cbox a b \<inter> {x. c \<le> x \<bullet> k}"
  5302   assume k: "k \<in> Basis"
  5303   let ?g = "\<lambda>x. if x\<bullet>k = c then f x else if x\<bullet>k \<le> c then g1 x else g2 x"
  5304   show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  5305     apply (rule_tac x="?g" in exI)
  5306     apply safe
  5307   proof goal_cases
  5308     case (1 x)
  5309     then show ?case
  5310       apply -
  5311       apply (cases "x\<bullet>k=c")
  5312       apply (case_tac "x\<bullet>k < c")
  5313       using as assms
  5314       apply auto
  5315       done
  5316   next
  5317     case 2
  5318     presume "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  5319       and "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  5320     then guess h1 h2 unfolding integrable_on_def by auto
  5321     from has_integral_split[OF this k] show ?case
  5322       unfolding integrable_on_def by auto
  5323   next
  5324     show "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}" "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  5325       apply(rule_tac[!] integrable_spike[OF negligible_standard_hyperplane[of k c]])
  5326       using k as(2,4)
  5327       apply auto
  5328       done
  5329   qed
  5330 qed
  5331 
  5332 lemma comm_monoid_set_F_and: "comm_monoid_set.F op \<and> True f s \<longleftrightarrow> (finite s \<longrightarrow> (\<forall>x\<in>s. f x))"
  5333 proof -
  5334   interpret bool: comm_monoid_set "op \<and>" True
  5335     proof qed auto
  5336   show ?thesis
  5337     by (induction s rule: infinite_finite_induct) auto
  5338 qed
  5339 
  5340 lemma approximable_on_division:
  5341   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5342   assumes "0 \<le> e"
  5343     and "d division_of (cbox a b)"
  5344     and "\<forall>i\<in>d. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  5345   obtains g where "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e" "g integrable_on cbox a b"
  5346 proof -
  5347   note * = comm_monoid_set.operative_division[OF comm_monoid_set_and operative_approximable[OF assms(1)] assms(2)]
  5348   from assms(3) this[unfolded comm_monoid_set_F_and, of f] division_of_finite[OF assms(2)]
  5349   guess g by auto
  5350   then show thesis
  5351     apply -
  5352     apply (rule that[of g])
  5353     apply auto
  5354     done
  5355 qed
  5356 
  5357 lemma integrable_continuous:
  5358   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5359   assumes "continuous_on (cbox a b) f"
  5360   shows "f integrable_on cbox a b"
  5361 proof (rule integrable_uniform_limit, safe)
  5362   fix e :: real
  5363   assume e: "e > 0"
  5364   from compact_uniformly_continuous[OF assms compact_cbox,unfolded uniformly_continuous_on_def,rule_format,OF e] guess d ..
  5365   note d=conjunctD2[OF this,rule_format]
  5366   from fine_division_exists[OF gauge_ball[OF d(1)], of a b] guess p . note p=this
  5367   note p' = tagged_division_ofD[OF p(1)]
  5368   have *: "\<forall>i\<in>snd ` p. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  5369   proof (safe, unfold snd_conv)
  5370     fix x l
  5371     assume as: "(x, l) \<in> p"
  5372     from p'(4)[OF this] guess a b by (elim exE) note l=this
  5373     show "\<exists>g. (\<forall>x\<in>l. norm (f x - g x) \<le> e) \<and> g integrable_on l"
  5374       apply (rule_tac x="\<lambda>y. f x" in exI)
  5375     proof safe
  5376       show "(\<lambda>y. f x) integrable_on l"
  5377         unfolding integrable_on_def l
  5378         apply rule
  5379         apply (rule has_integral_const)
  5380         done
  5381       fix y
  5382       assume y: "y \<in> l"
  5383       note fineD[OF p(2) as,unfolded subset_eq,rule_format,OF this]
  5384       note d(2)[OF _ _ this[unfolded mem_ball]]
  5385       then show "norm (f y - f x) \<le> e"
  5386         using y p'(2-3)[OF as] unfolding dist_norm l norm_minus_commute by fastforce
  5387     qed
  5388   qed
  5389   from e have "e \<ge> 0"
  5390     by auto
  5391   from approximable_on_division[OF this division_of_tagged_division[OF p(1)] *] guess g .
  5392   then show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  5393     by auto
  5394 qed
  5395 
  5396 lemma integrable_continuous_real:
  5397   fixes f :: "real \<Rightarrow> 'a::banach"
  5398   assumes "continuous_on {a .. b} f"
  5399   shows "f integrable_on {a .. b}"
  5400   by (metis assms box_real(2) integrable_continuous)
  5401 
  5402 subsection \<open>Specialization of additivity to one dimension.\<close>
  5403 
  5404 subsection \<open>Special case of additivity we need for the FTC.\<close>
  5405 
  5406 lemma additive_tagged_division_1:
  5407   fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  5408   assumes "a \<le> b"
  5409     and "p tagged_division_of {a..b}"
  5410   shows "setsum (\<lambda>(x,k). f(Sup k) - f(Inf k)) p = f b - f a"
  5411 proof -
  5412   let ?f = "(\<lambda>k::(real) set. if k = {} then 0 else f(interval_upperbound k) - f(interval_lowerbound k))"
  5413   have ***: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
  5414     using assms by auto
  5415   have *: "add.operative ?f"
  5416     unfolding add.operative_1_lt box_eq_empty
  5417     by auto
  5418   have **: "cbox a b \<noteq> {}"
  5419     using assms(1) by auto
  5420   note setsum.operative_tagged_division[OF * assms(2)[simplified box_real[symmetric]]]
  5421   note * = this[unfolded if_not_P[OF **] interval_bounds[OF ***],symmetric]
  5422   show ?thesis
  5423     unfolding *
  5424     apply (rule setsum.cong)
  5425     unfolding split_paired_all split_conv
  5426     using assms(2)
  5427     apply auto
  5428     done
  5429 qed
  5430 
  5431 
  5432 subsection \<open>A useful lemma allowing us to factor out the content size.\<close>
  5433 
  5434 lemma has_integral_factor_content:
  5435   "(f has_integral i) (cbox a b) \<longleftrightarrow>
  5436     (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  5437       norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content (cbox a b)))"
  5438 proof (cases "content (cbox a b) = 0")
  5439   case True
  5440   show ?thesis
  5441     unfolding has_integral_null_eq[OF True]
  5442     apply safe
  5443     apply (rule, rule, rule gauge_trivial, safe)
  5444     unfolding setsum_content_null[OF True] True
  5445     defer
  5446     apply (erule_tac x=1 in allE)
  5447     apply safe
  5448     defer
  5449     apply (rule fine_division_exists[of _ a b])
  5450     apply assumption
  5451     apply (erule_tac x=p in allE)
  5452     unfolding setsum_content_null[OF True]
  5453     apply auto
  5454     done
  5455 next
  5456   case False
  5457   note F = this[unfolded content_lt_nz[symmetric]]
  5458   let ?P = "\<lambda>e opp. \<exists>d. gauge d \<and>
  5459     (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow> opp (norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i)) e)"
  5460   show ?thesis
  5461     apply (subst has_integral)
  5462   proof safe
  5463     fix e :: real
  5464     assume e: "e > 0"
  5465     {
  5466       assume "\<forall>e>0. ?P e op <"
  5467       then show "?P (e * content (cbox a b)) op \<le>"
  5468         apply (erule_tac x="e * content (cbox a b)" in allE)
  5469         apply (erule impE)
  5470         defer
  5471         apply (erule exE,rule_tac x=d in exI)
  5472         using F e
  5473         apply (auto simp add:field_simps)
  5474         done
  5475     }
  5476     {
  5477       assume "\<forall>e>0. ?P (e * content (cbox a b)) op \<le>"
  5478       then show "?P e op <"
  5479         apply (erule_tac x="e / 2 / content (cbox a b)" in allE)
  5480         apply (erule impE)
  5481         defer
  5482         apply (erule exE,rule_tac x=d in exI)
  5483         using F e
  5484         apply (auto simp add: field_simps)
  5485         done
  5486     }
  5487   qed
  5488 qed
  5489 
  5490 lemma has_integral_factor_content_real:
  5491   "(f has_integral i) {a .. b::real} \<longleftrightarrow>
  5492     (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a .. b}  \<and> d fine p \<longrightarrow>
  5493       norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content {a .. b} ))"
  5494   unfolding box_real[symmetric]
  5495   by (rule has_integral_factor_content)
  5496 
  5497 
  5498 subsection \<open>Fundamental theorem of calculus.\<close>
  5499 
  5500 lemma interval_bounds_real:
  5501   fixes q b :: real
  5502   assumes "a \<le> b"
  5503   shows "Sup {a..b} = b"
  5504     and "Inf {a..b} = a"
  5505   using assms by auto
  5506 
  5507 lemma fundamental_theorem_of_calculus:
  5508   fixes f :: "real \<Rightarrow> 'a::banach"
  5509   assumes "a \<le> b"
  5510     and "\<forall>x\<in>{a .. b}. (f has_vector_derivative f' x) (at x within {a .. b})"
  5511   shows "(f' has_integral (f b - f a)) {a .. b}"
  5512   unfolding has_integral_factor_content box_real[symmetric]
  5513 proof safe
  5514   fix e :: real
  5515   assume e: "e > 0"
  5516   note assm = assms(2)[unfolded has_vector_derivative_def has_derivative_within_alt]
  5517   have *: "\<And>P Q. \<forall>x\<in>{a .. b}. P x \<and> (\<forall>e>0. \<exists>d>0. Q x e d) \<Longrightarrow> \<forall>x. \<exists>(d::real)>0. x\<in>{a .. b} \<longrightarrow> Q x e d"
  5518     using e by blast
  5519   note this[OF assm,unfolded gauge_existence_lemma]
  5520   from choice[OF this,unfolded Ball_def[symmetric]] guess d ..
  5521   note d=conjunctD2[OF this[rule_format],rule_format]
  5522   show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  5523     norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content (cbox a b))"
  5524     apply (rule_tac x="\<lambda>x. ball x (d x)" in exI)
  5525     apply safe
  5526     apply (rule gauge_ball_dependent)
  5527     apply rule
  5528     apply (rule d(1))
  5529   proof -
  5530     fix p
  5531     assume as: "p tagged_division_of cbox a b" "(\<lambda>x. ball x (d x)) fine p"
  5532     show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content (cbox a b)"
  5533       unfolding content_real[OF assms(1), simplified box_real[symmetric]] additive_tagged_division_1[OF assms(1) as(1)[simplified box_real],of f,symmetric]
  5534       unfolding additive_tagged_division_1[OF assms(1) as(1)[simplified box_real],of "\<lambda>x. x",symmetric]
  5535       unfolding setsum_right_distrib
  5536       defer
  5537       unfolding setsum_subtractf[symmetric]
  5538     proof (rule setsum_norm_le,safe)
  5539       fix x k
  5540       assume "(x, k) \<in> p"
  5541       note xk = tagged_division_ofD(2-4)[OF as(1) this]
  5542       from this(3) guess u v by (elim exE) note k=this
  5543       have *: "u \<le> v"
  5544         using xk unfolding k by auto
  5545       have ball: "\<forall>xa\<in>k. xa \<in> ball x (d x)"
  5546         using as(2)[unfolded fine_def,rule_format,OF \<open>(x,k)\<in>p\<close>,unfolded split_conv subset_eq] .
  5547       have "norm ((v - u) *\<^sub>R f' x - (f v - f u)) \<le>
  5548         norm (f u - f x - (u - x) *\<^sub>R f' x) + norm (f v - f x - (v - x) *\<^sub>R f' x)"
  5549         apply (rule order_trans[OF _ norm_triangle_ineq4])
  5550         apply (rule eq_refl)
  5551         apply (rule arg_cong[where f=norm])
  5552         unfolding scaleR_diff_left
  5553         apply (auto simp add:algebra_simps)
  5554         done
  5555       also have "\<dots> \<le> e * norm (u - x) + e * norm (v - x)"
  5556         apply (rule add_mono)
  5557         apply (rule d(2)[of "x" "u",unfolded o_def])
  5558         prefer 4
  5559         apply (rule d(2)[of "x" "v",unfolded o_def])
  5560         using ball[rule_format,of u] ball[rule_format,of v]
  5561         using xk(1-2)
  5562         unfolding k subset_eq
  5563         apply (auto simp add:dist_real_def)
  5564         done
  5565       also have "\<dots> \<le> e * (Sup k - Inf k)"
  5566         unfolding k interval_bounds_real[OF *]
  5567         using xk(1)
  5568         unfolding k
  5569         by (auto simp add: dist_real_def field_simps)
  5570       finally show "norm (content k *\<^sub>R f' x - (f (Sup k) - f (Inf k))) \<le>
  5571         e * (Sup k - Inf k)"
  5572         unfolding box_real k interval_bounds_real[OF *] content_real[OF *]
  5573           interval_upperbound_real interval_lowerbound_real
  5574           .
  5575     qed
  5576   qed
  5577 qed
  5578 
  5579 lemma ident_has_integral:
  5580   fixes a::real
  5581   assumes "a \<le> b"
  5582   shows "((\<lambda>x. x) has_integral (b\<^sup>2 - a\<^sup>2) / 2) {a..b}"
  5583 proof -
  5584   have "((\<lambda>x. x) has_integral inverse 2 * b\<^sup>2 - inverse 2 * a\<^sup>2) {a..b}"
  5585     apply (rule fundamental_theorem_of_calculus [OF assms], clarify)
  5586     unfolding power2_eq_square
  5587     by (rule derivative_eq_intros | simp)+
  5588   then show ?thesis
  5589     by (simp add: field_simps)
  5590 qed
  5591 
  5592 lemma integral_ident [simp]:
  5593   fixes a::real
  5594   assumes "a \<le> b"
  5595   shows "integral {a..b} (\<lambda>x. x) = (if a \<le> b then (b\<^sup>2 - a\<^sup>2) / 2 else 0)"
  5596 using ident_has_integral integral_unique by fastforce
  5597 
  5598 lemma ident_integrable_on:
  5599   fixes a::real
  5600   shows "(\<lambda>x. x) integrable_on {a..b}"
  5601 by (metis atLeastatMost_empty_iff integrable_on_def has_integral_empty ident_has_integral)
  5602 
  5603 
  5604 subsection \<open>Taylor series expansion\<close>
  5605 
  5606 lemma (in bounded_bilinear) setsum_prod_derivatives_has_vector_derivative:
  5607   assumes "p>0"
  5608   and f0: "Df 0 = f"
  5609   and Df: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5610     (Df m has_vector_derivative Df (Suc m) t) (at t within {a .. b})"
  5611   and g0: "Dg 0 = g"
  5612   and Dg: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5613     (Dg m has_vector_derivative Dg (Suc m) t) (at t within {a .. b})"
  5614   and ivl: "a \<le> t" "t \<le> b"
  5615   shows "((\<lambda>t. \<Sum>i<p. (-1)^i *\<^sub>R prod (Df i t) (Dg (p - Suc i) t))
  5616     has_vector_derivative
  5617       prod (f t) (Dg p t) - (-1)^p *\<^sub>R prod (Df p t) (g t))
  5618     (at t within {a .. b})"
  5619   using assms
  5620 proof cases
  5621   assume p: "p \<noteq> 1"
  5622   define p' where "p' = p - 2"
  5623   from assms p have p': "{..<p} = {..Suc p'}" "p = Suc (Suc p')"
  5624     by (auto simp: p'_def)
  5625   have *: "\<And>i. i \<le> p' \<Longrightarrow> Suc (Suc p' - i) = (Suc (Suc p') - i)"
  5626     by auto
  5627   let ?f = "\<lambda>i. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg ((p - i)) t))"
  5628   have "(\<Sum>i<p. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg (Suc (p - Suc i)) t) +
  5629     prod (Df (Suc i) t) (Dg (p - Suc i) t))) =
  5630     (\<Sum>i\<le>(Suc p'). ?f i - ?f (Suc i))"
  5631     by (auto simp: algebra_simps p'(2) numeral_2_eq_2 * lessThan_Suc_atMost)
  5632   also note setsum_telescope
  5633   finally
  5634   have "(\<Sum>i<p. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg (Suc (p - Suc i)) t) +
  5635     prod (Df (Suc i) t) (Dg (p - Suc i) t)))
  5636     = prod (f t) (Dg p t) - (- 1) ^ p *\<^sub>R prod (Df p t) (g t)"
  5637     unfolding p'[symmetric]
  5638     by (simp add: assms)
  5639   thus ?thesis
  5640     using assms
  5641     by (auto intro!: derivative_eq_intros has_vector_derivative)
  5642 qed (auto intro!: derivative_eq_intros has_vector_derivative)
  5643 
  5644 lemma
  5645   fixes f::"real\<Rightarrow>'a::banach"
  5646   assumes "p>0"
  5647   and f0: "Df 0 = f"
  5648   and Df: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5649     (Df m has_vector_derivative Df (Suc m) t) (at t within {a .. b})"
  5650   and ivl: "a \<le> b"
  5651   defines "i \<equiv> \<lambda>x. ((b - x) ^ (p - 1) / fact (p - 1)) *\<^sub>R Df p x"
  5652   shows taylor_has_integral:
  5653     "(i has_integral f b - (\<Sum>i<p. ((b - a) ^ i / fact i) *\<^sub>R Df i a)) {a..b}"
  5654   and taylor_integral:
  5655     "f b = (\<Sum>i<p. ((b - a) ^ i / fact i) *\<^sub>R Df i a) + integral {a..b} i"
  5656   and taylor_integrable:
  5657     "i integrable_on {a .. b}"
  5658 proof goal_cases
  5659   case 1
  5660   interpret bounded_bilinear "scaleR::real\<Rightarrow>'a\<Rightarrow>'a"
  5661     by (rule bounded_bilinear_scaleR)
  5662   define g where "g s = (b - s)^(p - 1)/fact (p - 1)" for s
  5663   define Dg where [abs_def]:
  5664     "Dg n s = (if n < p then (-1)^n * (b - s)^(p - 1 - n) / fact (p - 1 - n) else 0)" for n s
  5665   have g0: "Dg 0 = g"
  5666     using \<open>p > 0\<close>
  5667     by (auto simp add: Dg_def divide_simps g_def split: if_split_asm)
  5668   {
  5669     fix m
  5670     assume "p > Suc m"
  5671     hence "p - Suc m = Suc (p - Suc (Suc m))"
  5672       by auto
  5673     hence "real (p - Suc m) * fact (p - Suc (Suc m)) = fact (p - Suc m)"
  5674       by auto
  5675   } note fact_eq = this
  5676   have Dg: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5677     (Dg m has_vector_derivative Dg (Suc m) t) (at t within {a .. b})"
  5678     unfolding Dg_def
  5679     by (auto intro!: derivative_eq_intros simp: has_vector_derivative_def fact_eq divide_simps)
  5680   let ?sum = "\<lambda>t. \<Sum>i<p. (- 1) ^ i *\<^sub>R Dg i t *\<^sub>R Df (p - Suc i) t"
  5681   from setsum_prod_derivatives_has_vector_derivative[of _ Dg _ _ _ Df,
  5682       OF \<open>p > 0\<close> g0 Dg f0 Df]
  5683   have deriv: "\<And>t. a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5684     (?sum has_vector_derivative
  5685       g t *\<^sub>R Df p t - (- 1) ^ p *\<^sub>R Dg p t *\<^sub>R f t) (at t within {a..b})"
  5686     by auto
  5687   from fundamental_theorem_of_calculus[rule_format, OF \<open>a \<le> b\<close> deriv]
  5688   have "(i has_integral ?sum b - ?sum a) {a .. b}"
  5689     by (simp add: i_def g_def Dg_def)
  5690   also
  5691   have one: "(- 1) ^ p' * (- 1) ^ p' = (1::real)"
  5692     and "{..<p} \<inter> {i. p = Suc i} = {p - 1}"
  5693     for p'
  5694     using \<open>p > 0\<close>
  5695     by (auto simp: power_mult_distrib[symmetric])
  5696   then have "?sum b = f b"
  5697     using Suc_pred'[OF \<open>p > 0\<close>]
  5698     by (simp add: diff_eq_eq Dg_def power_0_left le_Suc_eq if_distrib
  5699         cond_application_beta setsum.If_cases f0)
  5700   also
  5701   have "{..<p} = (\<lambda>x. p - x - 1) ` {..<p}"
  5702   proof safe
  5703     fix x
  5704     assume "x < p"
  5705     thus "x \<in> (\<lambda>x. p - x - 1) ` {..<p}"
  5706       by (auto intro!: image_eqI[where x = "p - x - 1"])
  5707   qed simp
  5708   from _ this
  5709   have "?sum a = (\<Sum>i<p. ((b - a) ^ i / fact i) *\<^sub>R Df i a)"
  5710     by (rule setsum.reindex_cong) (auto simp add: inj_on_def Dg_def one)
  5711   finally show c: ?case .
  5712   case 2 show ?case using c integral_unique by force
  5713   case 3 show ?case using c by force
  5714 qed
  5715 
  5716 
  5717 subsection \<open>Attempt a systematic general set of "offset" results for components.\<close>
  5718 
  5719 lemma gauge_modify:
  5720   assumes "(\<forall>s. open s \<longrightarrow> open {x. f(x) \<in> s})" "gauge d"
  5721   shows "gauge (\<lambda>x. {y. f y \<in> d (f x)})"
  5722   using assms
  5723   unfolding gauge_def
  5724   apply safe
  5725   defer
  5726   apply (erule_tac x="f x" in allE)
  5727   apply (erule_tac x="d (f x)" in allE)
  5728   apply auto
  5729   done
  5730 
  5731 
  5732 subsection \<open>Only need trivial subintervals if the interval itself is trivial.\<close>
  5733 
  5734 lemma division_of_nontrivial:
  5735   fixes s :: "'a::euclidean_space set set"
  5736   assumes "s division_of (cbox a b)"
  5737     and "content (cbox a b) \<noteq> 0"
  5738   shows "{k. k \<in> s \<and> content k \<noteq> 0} division_of (cbox a b)"
  5739   using assms(1)
  5740   apply -
  5741 proof (induct "card s" arbitrary: s rule: nat_less_induct)
  5742   fix s::"'a set set"
  5743   assume assm: "s division_of (cbox a b)"
  5744     "\<forall>m<card s. \<forall>x. m = card x \<longrightarrow>
  5745       x division_of (cbox a b) \<longrightarrow> {k \<in> x. content k \<noteq> 0} division_of (cbox a b)"
  5746   note s = division_ofD[OF assm(1)]
  5747   let ?thesis = "{k \<in> s. content k \<noteq> 0} division_of (cbox a b)"
  5748   {
  5749     presume *: "{k \<in> s. content k \<noteq> 0} \<noteq> s \<Longrightarrow> ?thesis"
  5750     show ?thesis
  5751       apply cases
  5752       defer
  5753       apply (rule *)
  5754       apply assumption
  5755       using assm(1)
  5756       apply auto
  5757       done
  5758   }
  5759   assume noteq: "{k \<in> s. content k \<noteq> 0} \<noteq> s"
  5760   then obtain k where k: "k \<in> s" "content k = 0"
  5761     by auto
  5762   from s(4)[OF k(1)] guess c d by (elim exE) note k=k this
  5763   from k have "card s > 0"
  5764     unfolding card_gt_0_iff using assm(1) by auto
  5765   then have card: "card (s - {k}) < card s"
  5766     using assm(1) k(1)
  5767     apply (subst card_Diff_singleton_if)
  5768     apply auto
  5769     done
  5770   have *: "closed (\<Union>(s - {k}))"
  5771     apply (rule closed_Union)
  5772     defer
  5773     apply rule
  5774     apply (drule DiffD1,drule s(4))
  5775     using assm(1)
  5776     apply auto
  5777     done
  5778   have "k \<subseteq> \<Union>(s - {k})"
  5779     apply safe
  5780     apply (rule *[unfolded closed_limpt,rule_format])
  5781     unfolding islimpt_approachable
  5782   proof safe
  5783     fix x
  5784     fix e :: real
  5785     assume as: "x \<in> k" "e > 0"
  5786     from k(2)[unfolded k content_eq_0] guess i ..
  5787     then have i:"c\<bullet>i = d\<bullet>i" "i\<in>Basis"
  5788       using s(3)[OF k(1),unfolded k] unfolding box_ne_empty by auto
  5789     then have xi: "x\<bullet>i = d\<bullet>i"
  5790       using as unfolding k mem_box by (metis antisym)
  5791     define y where "y = (\<Sum>j\<in>Basis. (if j = i then if c\<bullet>i \<le> (a\<bullet>i + b\<bullet>i) / 2 then c\<bullet>i +
  5792       min e (b\<bullet>i - c\<bullet>i) / 2 else c\<bullet>i - min e (c\<bullet>i - a\<bullet>i) / 2 else x\<bullet>j) *\<^sub>R j)"
  5793     show "\<exists>x'\<in>\<Union>(s - {k}). x' \<noteq> x \<and> dist x' x < e"
  5794       apply (rule_tac x=y in bexI)
  5795     proof
  5796       have "d \<in> cbox c d"
  5797         using s(3)[OF k(1)]
  5798         unfolding k box_eq_empty mem_box
  5799         by (fastforce simp add: not_less)
  5800       then have "d \<in> cbox a b"
  5801         using s(2)[OF k(1)]
  5802         unfolding k
  5803         by auto
  5804       note di = this[unfolded mem_box,THEN bspec[where x=i]]
  5805       then have xyi: "y\<bullet>i \<noteq> x\<bullet>i"
  5806         unfolding y_def i xi
  5807         using as(2) assms(2)[unfolded content_eq_0] i(2)
  5808         by (auto elim!: ballE[of _ _ i])
  5809       then show "y \<noteq> x"
  5810         unfolding euclidean_eq_iff[where 'a='a] using i by auto
  5811       have *: "Basis = insert i (Basis - {i})"
  5812         using i by auto
  5813       have "norm (y - x) < e + setsum (\<lambda>i. 0) Basis"
  5814         apply (rule le_less_trans[OF norm_le_l1])
  5815         apply (subst *)
  5816         apply (subst setsum.insert)
  5817         prefer 3
  5818         apply (rule add_less_le_mono)
  5819       proof -
  5820         show "\<bar>(y - x) \<bullet> i\<bar> < e"
  5821           using di as(2) y_def i xi by (auto simp: inner_simps)
  5822         show "(\<Sum>i\<in>Basis - {i}. \<bar>(y - x) \<bullet> i\<bar>) \<le> (\<Sum>i\<in>Basis. 0)"
  5823           unfolding y_def by (auto simp: inner_simps)
  5824       qed auto
  5825       then show "dist y x < e"
  5826         unfolding dist_norm by auto
  5827       have "y \<notin> k"
  5828         unfolding k mem_box
  5829         apply rule
  5830         apply (erule_tac x=i in ballE)
  5831         using xyi k i xi
  5832         apply auto
  5833         done
  5834       moreover
  5835       have "y \<in> \<Union>s"
  5836         using set_rev_mp[OF as(1) s(2)[OF k(1)]] as(2) di i
  5837         unfolding s mem_box y_def
  5838         by (auto simp: field_simps elim!: ballE[of _ _ i])
  5839       ultimately
  5840       show "y \<in> \<Union>(s - {k})" by auto
  5841     qed
  5842   qed
  5843   then have "\<Union>(s - {k}) = cbox a b"
  5844     unfolding s(6)[symmetric] by auto
  5845   then have  "{ka \<in> s - {k}. content ka \<noteq> 0} division_of (cbox a b)"
  5846     apply -
  5847     apply (rule assm(2)[rule_format,OF card refl])
  5848     apply (rule division_ofI)
  5849     defer
  5850     apply (rule_tac[1-4] s)
  5851     using assm(1)
  5852     apply auto
  5853     done
  5854   moreover
  5855   have "{ka \<in> s - {k}. content ka \<noteq> 0} = {k \<in> s. content k \<noteq> 0}"
  5856     using k by auto
  5857   ultimately show ?thesis by auto
  5858 qed
  5859 
  5860 
  5861 subsection \<open>Integrability on subintervals.\<close>
  5862 
  5863 lemma operative_integrable:
  5864   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5865   shows "comm_monoid.operative op \<and> True (\<lambda>i. f integrable_on i)"
  5866   unfolding comm_monoid.operative_def[OF comm_monoid_and]
  5867   apply safe
  5868   apply (subst integrable_on_def)
  5869   unfolding has_integral_null_eq
  5870   apply (rule, rule refl)
  5871   apply (rule, assumption, assumption)+
  5872   unfolding integrable_on_def
  5873   by (auto intro!: has_integral_split)
  5874 
  5875 lemma integrable_subinterval:
  5876   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5877   assumes "f integrable_on cbox a b"
  5878     and "cbox c d \<subseteq> cbox a b"
  5879   shows "f integrable_on cbox c d"
  5880   apply (cases "cbox c d = {}")
  5881   defer
  5882   apply (rule partial_division_extend_1[OF assms(2)],assumption)
  5883   using comm_monoid_set.operative_division[OF comm_monoid_set_and operative_integrable,symmetric,of _ _ _ f] assms(1)
  5884   apply (auto simp: comm_monoid_set_F_and)
  5885   done
  5886 
  5887 lemma integrable_subinterval_real:
  5888   fixes f :: "real \<Rightarrow> 'a::banach"
  5889   assumes "f integrable_on {a .. b}"
  5890     and "{c .. d} \<subseteq> {a .. b}"
  5891   shows "f integrable_on {c .. d}"
  5892   by (metis assms(1) assms(2) box_real(2) integrable_subinterval)
  5893 
  5894 
  5895 subsection \<open>Combining adjacent intervals in 1 dimension.\<close>
  5896 
  5897 lemma has_integral_combine:
  5898   fixes a b c :: real
  5899   assumes "a \<le> c"
  5900     and "c \<le> b"
  5901     and "(f has_integral i) {a .. c}"
  5902     and "(f has_integral (j::'a::banach)) {c .. b}"
  5903   shows "(f has_integral (i + j)) {a .. b}"
  5904 proof -
  5905   note operative_integral[of f, unfolded comm_monoid.operative_1_le[OF add.comm_monoid_lift_option]]
  5906   note conjunctD2[OF this,rule_format]
  5907   note * = this(2)[OF conjI[OF assms(1-2)],unfolded if_P[OF assms(3)]]
  5908   then have "f integrable_on cbox a b"
  5909     apply -
  5910     apply (rule ccontr)
  5911     apply (subst(asm) if_P)
  5912     defer
  5913     apply (subst(asm) if_P)
  5914     using assms(3-)
  5915     apply auto
  5916     done
  5917   with *
  5918   show ?thesis
  5919     apply -
  5920     apply (subst(asm) if_P)
  5921     defer
  5922     apply (subst(asm) if_P)
  5923     defer
  5924     apply (subst(asm) if_P)
  5925     using assms(3-)
  5926     apply (auto simp add: integrable_on_def integral_unique)
  5927     done
  5928 qed
  5929 
  5930 lemma integral_combine:
  5931   fixes f :: "real \<Rightarrow> 'a::banach"
  5932   assumes "a \<le> c"
  5933     and "c \<le> b"
  5934     and "f integrable_on {a .. b}"
  5935   shows "integral {a .. c} f + integral {c .. b} f = integral {a .. b} f"
  5936   apply (rule integral_unique[symmetric])
  5937   apply (rule has_integral_combine[OF assms(1-2)])
  5938   apply (metis assms(2) assms(3) atLeastatMost_subset_iff box_real(2) content_pos_le content_real_eq_0 integrable_integral integrable_subinterval le_add_same_cancel2 monoid_add_class.add.left_neutral)
  5939   by (metis assms(1) assms(3) atLeastatMost_subset_iff box_real(2) content_pos_le content_real_eq_0 integrable_integral integrable_subinterval le_add_same_cancel1 monoid_add_class.add.right_neutral)
  5940 
  5941 lemma integrable_combine:
  5942   fixes f :: "real \<Rightarrow> 'a::banach"
  5943   assumes "a \<le> c"
  5944     and "c \<le> b"
  5945     and "f integrable_on {a .. c}"
  5946     and "f integrable_on {c .. b}"
  5947   shows "f integrable_on {a .. b}"
  5948   using assms
  5949   unfolding integrable_on_def
  5950   by (fastforce intro!:has_integral_combine)
  5951 
  5952 
  5953 subsection \<open>Reduce integrability to "local" integrability.\<close>
  5954