src/HOL/Analysis/Henstock_Kurzweil_Integration.thy
author haftmann
Wed Aug 10 18:57:20 2016 +0200 (2016-08-10)
changeset 63659 abe0c3872d8a
parent 63627 6ddb43c6b711
child 63680 6e1e8b5abbfa
permissions -rw-r--r--
keeping lifting rules local
     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 comm_monoid_lift_option:
  1690   assumes "comm_monoid f z"
  1691   shows "comm_monoid (lift_option f) (Some z)"
  1692 proof -
  1693   from assms interpret comm_monoid f z .
  1694   show ?thesis
  1695     by standard (auto simp: lift_option_def ac_simps split: bind_split)
  1696 qed
  1697 
  1698 lemma comm_monoid_and: "comm_monoid HOL.conj True"
  1699   by standard auto
  1700 
  1701 lemma comm_monoid_set_and: "comm_monoid_set HOL.conj True"
  1702   by (rule comm_monoid_set.intro) (fact comm_monoid_and)
  1703 
  1704 paragraph \<open>Operative\<close>
  1705 
  1706 definition (in comm_monoid) operative :: "('b::euclidean_space set \<Rightarrow> 'a) \<Rightarrow> bool"
  1707   where "operative g \<longleftrightarrow>
  1708     (\<forall>a b. content (cbox a b) = 0 \<longrightarrow> g (cbox a b) = \<^bold>1) \<and>
  1709     (\<forall>a b c. \<forall>k\<in>Basis. g (cbox a b) = g (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<^bold>* g (cbox a b \<inter> {x. x\<bullet>k \<ge> c}))"
  1710 
  1711 lemma (in comm_monoid) operativeD[dest]:
  1712   assumes "operative g"
  1713   shows "\<And>a b. content (cbox a b) = 0 \<Longrightarrow> g (cbox a b) = \<^bold>1"
  1714     and "\<And>a b c k. k \<in> Basis \<Longrightarrow> g (cbox a b) = g (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<^bold>* g (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  1715   using assms unfolding operative_def by auto
  1716 
  1717 lemma (in comm_monoid) operative_empty: "operative g \<Longrightarrow> g {} = \<^bold>1"
  1718   unfolding operative_def by (rule property_empty_interval) auto
  1719 
  1720 lemma operative_content[intro]: "add.operative content"
  1721   by (force simp add: add.operative_def content_split[symmetric])
  1722 
  1723 definition "division_points (k::('a::euclidean_space) set) d =
  1724    {(j,x). j \<in> Basis \<and> (interval_lowerbound k)\<bullet>j < x \<and> x < (interval_upperbound k)\<bullet>j \<and>
  1725      (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  1726 
  1727 lemma division_points_finite:
  1728   fixes i :: "'a::euclidean_space set"
  1729   assumes "d division_of i"
  1730   shows "finite (division_points i d)"
  1731 proof -
  1732   note assm = division_ofD[OF assms]
  1733   let ?M = "\<lambda>j. {(j,x)|x. (interval_lowerbound i)\<bullet>j < x \<and> x < (interval_upperbound i)\<bullet>j \<and>
  1734     (\<exists>i\<in>d. (interval_lowerbound i)\<bullet>j = x \<or> (interval_upperbound i)\<bullet>j = x)}"
  1735   have *: "division_points i d = \<Union>(?M ` Basis)"
  1736     unfolding division_points_def by auto
  1737   show ?thesis
  1738     unfolding * using assm by auto
  1739 qed
  1740 
  1741 lemma division_points_subset:
  1742   fixes a :: "'a::euclidean_space"
  1743   assumes "d division_of (cbox a b)"
  1744     and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  1745     and k: "k \<in> Basis"
  1746   shows "division_points (cbox a b \<inter> {x. x\<bullet>k \<le> c}) {l \<inter> {x. x\<bullet>k \<le> c} | l . l \<in> d \<and> l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}} \<subseteq>
  1747       division_points (cbox a b) d" (is ?t1)
  1748     and "division_points (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) {l \<inter> {x. x\<bullet>k \<ge> c} | l . l \<in> d \<and> ~(l \<inter> {x. x\<bullet>k \<ge> c} = {})} \<subseteq>
  1749       division_points (cbox a b) d" (is ?t2)
  1750 proof -
  1751   note assm = division_ofD[OF assms(1)]
  1752   have *: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  1753     "\<forall>i\<in>Basis. a\<bullet>i \<le> (\<Sum>i\<in>Basis. (if i = k then min (b \<bullet> k) c else  b \<bullet> i) *\<^sub>R i) \<bullet> i"
  1754     "\<forall>i\<in>Basis. (\<Sum>i\<in>Basis. (if i = k then max (a \<bullet> k) c else a \<bullet> i) *\<^sub>R i) \<bullet> i \<le> b\<bullet>i"
  1755     "min (b \<bullet> k) c = c" "max (a \<bullet> k) c = c"
  1756     using assms using less_imp_le by auto
  1757   show ?t1 (*FIXME a horrible mess*)
  1758     unfolding division_points_def interval_split[OF k, of a b]
  1759     unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  1760     unfolding *
  1761     apply (rule subsetI)
  1762     unfolding mem_Collect_eq split_beta
  1763     apply (erule bexE conjE)+
  1764     apply (simp add: )
  1765     apply (erule exE conjE)+
  1766   proof
  1767     fix i l x
  1768     assume as:
  1769       "a \<bullet> fst x < snd x" "snd x < (if fst x = k then c else b \<bullet> fst x)"
  1770       "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1771       "i = l \<inter> {x. x \<bullet> k \<le> c}" "l \<in> d" "l \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}"
  1772       and fstx: "fst x \<in> Basis"
  1773     from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  1774     have *: "\<forall>i\<in>Basis. u \<bullet> i \<le> (\<Sum>i\<in>Basis. (if i = k then min (v \<bullet> k) c else v \<bullet> i) *\<^sub>R i) \<bullet> i"
  1775       using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  1776     have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  1777       using l using as(6) unfolding box_ne_empty[symmetric] by auto
  1778     show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1779       apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  1780       using as(1-3,5) fstx
  1781       unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  1782       apply (auto split: if_split_asm)
  1783       done
  1784     show "snd x < b \<bullet> fst x"
  1785       using as(2) \<open>c < b\<bullet>k\<close> by (auto split: if_split_asm)
  1786   qed
  1787   show ?t2
  1788     unfolding division_points_def interval_split[OF k, of a b]
  1789     unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)]
  1790     unfolding *
  1791     unfolding subset_eq
  1792     apply rule
  1793     unfolding mem_Collect_eq split_beta
  1794     apply (erule bexE conjE)+
  1795     apply (simp only: mem_Collect_eq inner_setsum_left_Basis simp_thms)
  1796     apply (erule exE conjE)+
  1797   proof
  1798     fix i l x
  1799     assume as:
  1800       "(if fst x = k then c else a \<bullet> fst x) < snd x" "snd x < b \<bullet> fst x"
  1801       "interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1802       "i = l \<inter> {x. c \<le> x \<bullet> k}" "l \<in> d" "l \<inter> {x. c \<le> x \<bullet> k} \<noteq> {}"
  1803       and fstx: "fst x \<in> Basis"
  1804     from assm(4)[OF this(5)] guess u v by (elim exE) note l=this
  1805     have *: "\<forall>i\<in>Basis. (\<Sum>i\<in>Basis. (if i = k then max (u \<bullet> k) c else u \<bullet> i) *\<^sub>R i) \<bullet> i \<le> v \<bullet> i"
  1806       using as(6) unfolding l interval_split[OF k] box_ne_empty as .
  1807     have **: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i"
  1808       using l using as(6) unfolding box_ne_empty[symmetric] by auto
  1809     show "\<exists>i\<in>d. interval_lowerbound i \<bullet> fst x = snd x \<or> interval_upperbound i \<bullet> fst x = snd x"
  1810       apply (rule bexI[OF _ \<open>l \<in> d\<close>])
  1811       using as(1-3,5) fstx
  1812       unfolding l interval_bounds[OF **] interval_bounds[OF *] interval_split[OF k] as
  1813       apply (auto split: if_split_asm)
  1814       done
  1815     show "a \<bullet> fst x < snd x"
  1816       using as(1) \<open>a\<bullet>k < c\<close> by (auto split: if_split_asm)
  1817    qed
  1818 qed
  1819 
  1820 lemma division_points_psubset:
  1821   fixes a :: "'a::euclidean_space"
  1822   assumes "d division_of (cbox a b)"
  1823       and "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i"  "a\<bullet>k < c" "c < b\<bullet>k"
  1824       and "l \<in> d"
  1825       and "interval_lowerbound l\<bullet>k = c \<or> interval_upperbound l\<bullet>k = c"
  1826       and k: "k \<in> Basis"
  1827   shows "division_points (cbox a b \<inter> {x. x\<bullet>k \<le> c}) {l \<inter> {x. x\<bullet>k \<le> c} | l. l\<in>d \<and> l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}} \<subset>
  1828          division_points (cbox a b) d" (is "?D1 \<subset> ?D")
  1829     and "division_points (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) {l \<inter> {x. x\<bullet>k \<ge> c} | l. l\<in>d \<and> l \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}} \<subset>
  1830          division_points (cbox a b) d" (is "?D2 \<subset> ?D")
  1831 proof -
  1832   have ab: "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  1833     using assms(2) by (auto intro!:less_imp_le)
  1834   guess u v using division_ofD(4)[OF assms(1,5)] by (elim exE) note l=this
  1835   have uv: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i" "\<forall>i\<in>Basis. a\<bullet>i \<le> u\<bullet>i \<and> v\<bullet>i \<le> b\<bullet>i"
  1836     using division_ofD(2,2,3)[OF assms(1,5)] unfolding l box_ne_empty
  1837     using subset_box(1)
  1838     apply auto
  1839     apply blast+
  1840     done
  1841   have *: "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  1842           "interval_upperbound (cbox a b \<inter> {x. x \<bullet> k \<le> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  1843     unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  1844     using uv[rule_format, of k] ab k
  1845     by auto
  1846   have "\<exists>x. x \<in> ?D - ?D1"
  1847     using assms(3-)
  1848     unfolding division_points_def interval_bounds[OF ab]
  1849     apply -
  1850     apply (erule disjE)
  1851     apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  1852     apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  1853     done
  1854   moreover have "?D1 \<subseteq> ?D"
  1855     by (auto simp add: assms division_points_subset)
  1856   ultimately show "?D1 \<subset> ?D"
  1857     by blast
  1858   have *: "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_lowerbound l \<bullet> k}) \<bullet> k = interval_lowerbound l \<bullet> k"
  1859     "interval_lowerbound (cbox a b \<inter> {x. x \<bullet> k \<ge> interval_upperbound l \<bullet> k}) \<bullet> k = interval_upperbound l \<bullet> k"
  1860     unfolding l interval_split[OF k] interval_bounds[OF uv(1)]
  1861     using uv[rule_format, of k] ab k
  1862     by auto
  1863   have "\<exists>x. x \<in> ?D - ?D2"
  1864     using assms(3-)
  1865     unfolding division_points_def interval_bounds[OF ab]
  1866     apply -
  1867     apply (erule disjE)
  1868     apply (rule_tac x="(k,(interval_lowerbound l)\<bullet>k)" in exI, force simp add: *)
  1869     apply (rule_tac x="(k,(interval_upperbound l)\<bullet>k)" in exI, force simp add: *)
  1870     done
  1871   moreover have "?D2 \<subseteq> ?D"
  1872     by (auto simp add: assms division_points_subset)
  1873   ultimately show "?D2 \<subset> ?D"
  1874     by blast
  1875 qed
  1876 
  1877 lemma (in comm_monoid_set) operative_division:
  1878   fixes g :: "'b::euclidean_space set \<Rightarrow> 'a"
  1879   assumes g: "operative g" and d: "d division_of (cbox a b)" shows "F g d = g (cbox a b)"
  1880 proof -
  1881   define C where [abs_def]: "C = card (division_points (cbox a b) d)"
  1882   then show ?thesis
  1883     using d
  1884   proof (induction C arbitrary: a b d rule: less_induct)
  1885     case (less a b d)
  1886     show ?case
  1887     proof cases
  1888       show "content (cbox a b) = 0 \<Longrightarrow> F g d = g (cbox a b)"
  1889         using division_of_content_0[OF _ less.prems] operativeD(1)[OF  g] division_ofD(4)[OF less.prems]
  1890         by (fastforce intro!: neutral)
  1891     next
  1892       assume "content (cbox a b) \<noteq> 0"
  1893       note ab = this[unfolded content_lt_nz[symmetric] content_pos_lt_eq]
  1894       then have ab': "\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i"
  1895         by (auto intro!: less_imp_le)
  1896       show "F g d = g (cbox a b)"
  1897       proof (cases "division_points (cbox a b) d = {}")
  1898         case True
  1899         { fix u v and j :: 'b
  1900           assume j: "j \<in> Basis" and as: "cbox u v \<in> d"
  1901           then have "cbox u v \<noteq> {}"
  1902             using less.prems by blast
  1903           then have uv: "\<forall>i\<in>Basis. u\<bullet>i \<le> v\<bullet>i" "u\<bullet>j \<le> v\<bullet>j"
  1904             using j unfolding box_ne_empty by auto
  1905           have *: "\<And>p r Q. \<not> j\<in>Basis \<or> p \<or> r \<or> (\<forall>x\<in>d. Q x) \<Longrightarrow> p \<or> r \<or> Q (cbox u v)"
  1906             using as j by auto
  1907           have "(j, u\<bullet>j) \<notin> division_points (cbox a b) d"
  1908                "(j, v\<bullet>j) \<notin> division_points (cbox a b) d" using True by auto
  1909           note this[unfolded de_Morgan_conj division_points_def mem_Collect_eq split_conv interval_bounds[OF ab'] bex_simps]
  1910           note *[OF this(1)] *[OF this(2)] note this[unfolded interval_bounds[OF uv(1)]]
  1911           moreover
  1912           have "a\<bullet>j \<le> u\<bullet>j" "v\<bullet>j \<le> b\<bullet>j"
  1913             using division_ofD(2,2,3)[OF \<open>d division_of cbox a b\<close> as]
  1914             apply (metis j subset_box(1) uv(1))
  1915             by (metis \<open>cbox u v \<subseteq> cbox a b\<close> j subset_box(1) uv(1))
  1916           ultimately have "u\<bullet>j = a\<bullet>j \<and> v\<bullet>j = a\<bullet>j \<or> u\<bullet>j = b\<bullet>j \<and> v\<bullet>j = b\<bullet>j \<or> u\<bullet>j = a\<bullet>j \<and> v\<bullet>j = b\<bullet>j"
  1917             unfolding not_less de_Morgan_disj using ab[rule_format,of j] uv(2) j by force }
  1918         then have d': "\<forall>i\<in>d. \<exists>u v. i = cbox u v \<and>
  1919           (\<forall>j\<in>Basis. u\<bullet>j = a\<bullet>j \<and> v\<bullet>j = a\<bullet>j \<or> u\<bullet>j = b\<bullet>j \<and> v\<bullet>j = b\<bullet>j \<or> u\<bullet>j = a\<bullet>j \<and> v\<bullet>j = b\<bullet>j)"
  1920           unfolding forall_in_division[OF less.prems] by blast
  1921         have "(1/2) *\<^sub>R (a+b) \<in> cbox a b"
  1922           unfolding mem_box using ab by(auto intro!: less_imp_le simp: inner_simps)
  1923         note this[unfolded division_ofD(6)[OF \<open>d division_of cbox a b\<close>,symmetric] Union_iff]
  1924         then guess i .. note i=this
  1925         guess u v using d'[rule_format,OF i(1)] by (elim exE conjE) note uv=this
  1926         have "cbox a b \<in> d"
  1927         proof -
  1928           have "u = a" "v = b"
  1929             unfolding euclidean_eq_iff[where 'a='b]
  1930           proof safe
  1931             fix j :: 'b
  1932             assume j: "j \<in> Basis"
  1933             note i(2)[unfolded uv mem_box,rule_format,of j]
  1934             then show "u \<bullet> j = a \<bullet> j" and "v \<bullet> j = b \<bullet> j"
  1935               using uv(2)[rule_format,of j] j by (auto simp: inner_simps)
  1936           qed
  1937           then have "i = cbox a b" using uv by auto
  1938           then show ?thesis using i by auto
  1939         qed
  1940         then have deq: "d = insert (cbox a b) (d - {cbox a b})"
  1941           by auto
  1942         have "F g (d - {cbox a b}) = \<^bold>1"
  1943         proof (intro neutral ballI)
  1944           fix x
  1945           assume x: "x \<in> d - {cbox a b}"
  1946           then have "x\<in>d"
  1947             by auto note d'[rule_format,OF this]
  1948           then guess u v by (elim exE conjE) note uv=this
  1949           have "u \<noteq> a \<or> v \<noteq> b"
  1950             using x[unfolded uv] by auto
  1951           then obtain j where "u\<bullet>j \<noteq> a\<bullet>j \<or> v\<bullet>j \<noteq> b\<bullet>j" and j: "j \<in> Basis"
  1952             unfolding euclidean_eq_iff[where 'a='b] by auto
  1953           then have "u\<bullet>j = v\<bullet>j"
  1954             using uv(2)[rule_format,OF j] by auto
  1955           then have "content (cbox u v) = 0"
  1956             unfolding content_eq_0 using j
  1957             by force
  1958           then show "g x = \<^bold>1"
  1959             unfolding uv(1) by (rule operativeD(1)[OF g])
  1960         qed
  1961         then show "F g d = g (cbox a b)"
  1962           using division_ofD[OF less.prems]
  1963           apply (subst deq)
  1964           apply (subst insert)
  1965           apply auto
  1966           done
  1967       next
  1968         case False
  1969         then have "\<exists>x. x \<in> division_points (cbox a b) d"
  1970           by auto
  1971         then guess k c
  1972           unfolding split_paired_Ex division_points_def mem_Collect_eq split_conv
  1973           apply (elim exE conjE)
  1974           done
  1975         note this(2-4,1) note kc=this[unfolded interval_bounds[OF ab']]
  1976         from this(3) guess j .. note j=this
  1977         define d1 where "d1 = {l \<inter> {x. x\<bullet>k \<le> c} | l. l \<in> d \<and> l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}}"
  1978         define d2 where "d2 = {l \<inter> {x. x\<bullet>k \<ge> c} | l. l \<in> d \<and> l \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}}"
  1979         define cb where "cb = (\<Sum>i\<in>Basis. (if i = k then c else b\<bullet>i) *\<^sub>R i)"
  1980         define ca where "ca = (\<Sum>i\<in>Basis. (if i = k then c else a\<bullet>i) *\<^sub>R i)"
  1981         note division_points_psubset[OF \<open>d division_of cbox a b\<close> ab kc(1-2) j]
  1982         note psubset_card_mono[OF _ this(1)] psubset_card_mono[OF _ this(2)]
  1983         then have *: "F g d1 = g (cbox a b \<inter> {x. x\<bullet>k \<le> c})" "F g d2 = g (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  1984           unfolding interval_split[OF kc(4)]
  1985           apply (rule_tac[!] "less.hyps"[rule_format])
  1986           using division_split[OF \<open>d division_of cbox a b\<close>, where k=k and c=c]
  1987           apply (simp_all add: interval_split kc d1_def d2_def division_points_finite[OF \<open>d division_of cbox a b\<close>])
  1988           done
  1989         { fix l y
  1990           assume as: "l \<in> d" "y \<in> d" "l \<inter> {x. x \<bullet> k \<le> c} = y \<inter> {x. x \<bullet> k \<le> c}" "l \<noteq> y"
  1991           from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  1992           have "g (l \<inter> {x. x \<bullet> k \<le> c}) = \<^bold>1"
  1993             unfolding leq interval_split[OF kc(4)]
  1994             apply (rule operativeD[OF g])
  1995             unfolding interval_split[symmetric, OF kc(4)]
  1996             using division_split_left_inj less as kc leq by blast
  1997         } note fxk_le = this
  1998         { fix l y
  1999           assume as: "l \<in> d" "y \<in> d" "l \<inter> {x. c \<le> x \<bullet> k} = y \<inter> {x. c \<le> x \<bullet> k}" "l \<noteq> y"
  2000           from division_ofD(4)[OF \<open>d division_of cbox a b\<close> this(1)] guess u v by (elim exE) note leq=this
  2001           have "g (l \<inter> {x. x \<bullet> k \<ge> c}) = \<^bold>1"
  2002             unfolding leq interval_split[OF kc(4)]
  2003             apply (rule operativeD(1)[OF g])
  2004             unfolding interval_split[symmetric,OF kc(4)]
  2005             using division_split_right_inj less leq as kc by blast
  2006         } note fxk_ge = this
  2007         have d1_alt: "d1 = (\<lambda>l. l \<inter> {x. x\<bullet>k \<le> c}) ` {l \<in> d. l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}}"
  2008           using d1_def by auto
  2009         have d2_alt: "d2 = (\<lambda>l. l \<inter> {x. x\<bullet>k \<ge> c}) ` {l \<in> d. l \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}}"
  2010           using d2_def by auto
  2011         have "g (cbox a b) = F g d1 \<^bold>* F g d2" (is "_ = ?prev")
  2012           unfolding * using g kc(4) by blast
  2013         also have "F g d1 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<le> c})) d"
  2014           unfolding d1_alt using division_of_finite[OF less.prems] fxk_le
  2015           by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  2016         also have "F g d2 = F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<ge> c})) d"
  2017           unfolding d2_alt using division_of_finite[OF less.prems] fxk_ge
  2018           by (subst reindex_nontrivial) (auto intro!: mono_neutral_cong_left simp: operative_empty[OF g])
  2019         also have *: "\<forall>x\<in>d. g x = g (x \<inter> {x. x \<bullet> k \<le> c}) \<^bold>* g (x \<inter> {x. c \<le> x \<bullet> k})"
  2020           unfolding forall_in_division[OF \<open>d division_of cbox a b\<close>]
  2021           using g kc(4) by blast
  2022         have "F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<le> c})) d \<^bold>* F (\<lambda>l. g (l \<inter> {x. x\<bullet>k \<ge> c})) d = F g d"
  2023           using * by (simp add: distrib)
  2024         finally show ?thesis by auto
  2025       qed
  2026     qed
  2027   qed
  2028 qed
  2029 
  2030 lemma (in comm_monoid_set) operative_tagged_division:
  2031   assumes f: "operative g" and d: "d tagged_division_of (cbox a b)"
  2032   shows "F (\<lambda>(x, l). g l) d = g (cbox a b)"
  2033   unfolding d[THEN division_of_tagged_division, THEN operative_division[OF f], symmetric]
  2034   by (simp add: f[THEN operativeD(1)] over_tagged_division_lemma[OF d])
  2035 
  2036 lemma additive_content_division: "d division_of (cbox a b) \<Longrightarrow> setsum content d = content (cbox a b)"
  2037   by (metis operative_content setsum.operative_division)
  2038 
  2039 lemma additive_content_tagged_division:
  2040   "d tagged_division_of (cbox a b) \<Longrightarrow> setsum (\<lambda>(x,l). content l) d = content (cbox a b)"
  2041   unfolding setsum.operative_tagged_division[OF operative_content, symmetric] by blast
  2042 
  2043 lemma
  2044   shows real_inner_1_left: "inner 1 x = x"
  2045   and real_inner_1_right: "inner x 1 = x"
  2046   by simp_all
  2047 
  2048 lemma content_real_eq_0: "content {a .. b::real} = 0 \<longleftrightarrow> a \<ge> b"
  2049   by (metis atLeastatMost_empty_iff2 content_empty content_real diff_self eq_iff le_cases le_iff_diff_le_0)
  2050 
  2051 lemma interval_real_split:
  2052   "{a .. b::real} \<inter> {x. x \<le> c} = {a .. min b c}"
  2053   "{a .. b} \<inter> {x. c \<le> x} = {max a c .. b}"
  2054   apply (metis Int_atLeastAtMostL1 atMost_def)
  2055   apply (metis Int_atLeastAtMostL2 atLeast_def)
  2056   done
  2057 
  2058 lemma (in comm_monoid) operative_1_lt:
  2059   "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  2060     ((\<forall>a b. b \<le> a \<longrightarrow> g {a .. b} = \<^bold>1) \<and> (\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a .. c} \<^bold>* g {c .. b} = g {a .. b}))"
  2061   apply (simp add: operative_def content_real_eq_0 atMost_def[symmetric] atLeast_def[symmetric]
  2062               del: content_real_if)
  2063 proof safe
  2064   fix a b c :: real
  2065   assume *: "\<forall>a b c. g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  2066   assume "a < c" "c < b"
  2067   with *[rule_format, of a b c] show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2068     by (simp add: less_imp_le min.absorb2 max.absorb2)
  2069 next
  2070   fix a b c :: real
  2071   assume as: "\<forall>a b. b \<le> a \<longrightarrow> g {a..b} = \<^bold>1"
  2072     "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2073   from as(1)[rule_format, of 0 1] as(1)[rule_format, of a a for a] as(2)
  2074   have [simp]: "g {} = \<^bold>1" "\<And>a. g {a} = \<^bold>1"
  2075     "\<And>a b c. a < c \<Longrightarrow> c < b \<Longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2076     by auto
  2077   show "g {a..b} = g {a..min b c} \<^bold>* g {max a c..b}"
  2078     by (auto simp: min_def max_def le_less)
  2079 qed
  2080 
  2081 lemma (in comm_monoid) operative_1_le:
  2082   "operative (g :: real set \<Rightarrow> 'a) \<longleftrightarrow>
  2083     ((\<forall>a b. b \<le> a \<longrightarrow> g {a..b} = \<^bold>1) \<and> (\<forall>a b c. a \<le> c \<and> c \<le> b \<longrightarrow> g {a .. c} \<^bold>* g {c .. b} = g {a .. b}))"
  2084   unfolding operative_1_lt
  2085 proof safe
  2086   fix a b c :: real
  2087   assume as: "\<forall>a b c. a \<le> c \<and> c \<le> b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}" "a < c" "c < b"
  2088   show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2089     apply (rule as(1)[rule_format])
  2090     using as(2-)
  2091     apply auto
  2092     done
  2093 next
  2094   fix a b c :: real
  2095   assume "\<forall>a b. b \<le> a \<longrightarrow> g {a .. b} = \<^bold>1"
  2096     and "\<forall>a b c. a < c \<and> c < b \<longrightarrow> g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2097     and "a \<le> c"
  2098     and "c \<le> b"
  2099   note as = this[rule_format]
  2100   show "g {a..c} \<^bold>* g {c..b} = g {a..b}"
  2101   proof (cases "c = a \<or> c = b")
  2102     case False
  2103     then show ?thesis
  2104       apply -
  2105       apply (subst as(2))
  2106       using as(3-)
  2107       apply auto
  2108       done
  2109   next
  2110     case True
  2111     then show ?thesis
  2112     proof
  2113       assume *: "c = a"
  2114       then have "g {a .. c} = \<^bold>1"
  2115         apply -
  2116         apply (rule as(1)[rule_format])
  2117         apply auto
  2118         done
  2119       then show ?thesis
  2120         unfolding * by auto
  2121     next
  2122       assume *: "c = b"
  2123       then have "g {c .. b} = \<^bold>1"
  2124         apply -
  2125         apply (rule as(1)[rule_format])
  2126         apply auto
  2127         done
  2128       then show ?thesis
  2129         unfolding * by auto
  2130     qed
  2131   qed
  2132 qed
  2133 
  2134 subsection \<open>Fine-ness of a partition w.r.t. a gauge.\<close>
  2135 
  2136 definition fine  (infixr "fine" 46)
  2137   where "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d x)"
  2138 
  2139 lemma fineI:
  2140   assumes "\<And>x k. (x, k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  2141   shows "d fine s"
  2142   using assms unfolding fine_def by auto
  2143 
  2144 lemma fineD[dest]:
  2145   assumes "d fine s"
  2146   shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  2147   using assms unfolding fine_def by auto
  2148 
  2149 lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
  2150   unfolding fine_def by auto
  2151 
  2152 lemma fine_inters:
  2153  "(\<lambda>x. \<Inter>{f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
  2154   unfolding fine_def by blast
  2155 
  2156 lemma fine_union: "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
  2157   unfolding fine_def by blast
  2158 
  2159 lemma fine_unions: "(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
  2160   unfolding fine_def by auto
  2161 
  2162 lemma fine_subset: "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
  2163   unfolding fine_def by blast
  2164 
  2165 
  2166 subsection \<open>Gauge integral. Define on compact intervals first, then use a limit.\<close>
  2167 
  2168 definition has_integral_compact_interval (infixr "has'_integral'_compact'_interval" 46)
  2169   where "(f has_integral_compact_interval y) i \<longleftrightarrow>
  2170     (\<forall>e>0. \<exists>d. gauge d \<and>
  2171       (\<forall>p. p tagged_division_of i \<and> d fine p \<longrightarrow>
  2172         norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - y) < e))"
  2173 
  2174 definition has_integral ::
  2175     "('n::euclidean_space \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'b \<Rightarrow> 'n set \<Rightarrow> bool"
  2176   (infixr "has'_integral" 46)
  2177   where "(f has_integral y) i \<longleftrightarrow>
  2178     (if \<exists>a b. i = cbox a b
  2179      then (f has_integral_compact_interval y) i
  2180      else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  2181       (\<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral_compact_interval z) (cbox a b) \<and>
  2182         norm (z - y) < e)))"
  2183 
  2184 lemma has_integral:
  2185   "(f has_integral y) (cbox a b) \<longleftrightarrow>
  2186     (\<forall>e>0. \<exists>d. gauge d \<and>
  2187       (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  2188         norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
  2189   unfolding has_integral_def has_integral_compact_interval_def
  2190   by auto
  2191 
  2192 lemma has_integral_real:
  2193   "(f has_integral y) {a .. b::real} \<longleftrightarrow>
  2194     (\<forall>e>0. \<exists>d. gauge d \<and>
  2195       (\<forall>p. p tagged_division_of {a .. b} \<and> d fine p \<longrightarrow>
  2196         norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
  2197   unfolding box_real[symmetric]
  2198   by (rule has_integral)
  2199 
  2200 lemma has_integralD[dest]:
  2201   assumes "(f has_integral y) (cbox a b)"
  2202     and "e > 0"
  2203   obtains d where "gauge d"
  2204     and "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d fine p \<Longrightarrow>
  2205       norm (setsum (\<lambda>(x,k). content(k) *\<^sub>R f(x)) p - y) < e"
  2206   using assms unfolding has_integral by auto
  2207 
  2208 lemma has_integral_alt:
  2209   "(f has_integral y) i \<longleftrightarrow>
  2210     (if \<exists>a b. i = cbox a b
  2211      then (f has_integral y) i
  2212      else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  2213       (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e)))"
  2214   unfolding has_integral
  2215   unfolding has_integral_compact_interval_def has_integral_def
  2216   by auto
  2217 
  2218 lemma has_integral_altD:
  2219   assumes "(f has_integral y) i"
  2220     and "\<not> (\<exists>a b. i = cbox a b)"
  2221     and "e>0"
  2222   obtains B where "B > 0"
  2223     and "\<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  2224       (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) (cbox a b) \<and> norm(z - y) < e)"
  2225   using assms
  2226   unfolding has_integral
  2227   unfolding has_integral_compact_interval_def has_integral_def
  2228   by auto
  2229 
  2230 definition integrable_on (infixr "integrable'_on" 46)
  2231   where "f integrable_on i \<longleftrightarrow> (\<exists>y. (f has_integral y) i)"
  2232 
  2233 definition "integral i f = (SOME y. (f has_integral y) i \<or> ~ f integrable_on i \<and> y=0)"
  2234 
  2235 lemma integrable_integral[dest]: "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
  2236   unfolding integrable_on_def integral_def by (metis (mono_tags, lifting) someI_ex)
  2237 
  2238 lemma not_integrable_integral: "~ f integrable_on i \<Longrightarrow> integral i f = 0"
  2239   unfolding integrable_on_def integral_def by blast
  2240 
  2241 lemma has_integral_integrable[intro]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
  2242   unfolding integrable_on_def by auto
  2243 
  2244 lemma has_integral_integral: "f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
  2245   by auto
  2246 
  2247 lemma setsum_content_null:
  2248   assumes "content (cbox a b) = 0"
  2249     and "p tagged_division_of (cbox a b)"
  2250   shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
  2251 proof (rule setsum.neutral, rule)
  2252   fix y
  2253   assume y: "y \<in> p"
  2254   obtain x k where xk: "y = (x, k)"
  2255     using surj_pair[of y] by blast
  2256   note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
  2257   from this(2) obtain c d where k: "k = cbox c d" by blast
  2258   have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x"
  2259     unfolding xk by auto
  2260   also have "\<dots> = 0"
  2261     using content_subset[OF assm(1)[unfolded k]] content_pos_le[of c d]
  2262     unfolding assms(1) k
  2263     by auto
  2264   finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
  2265 qed
  2266 
  2267 
  2268 subsection \<open>Some basic combining lemmas.\<close>
  2269 
  2270 lemma tagged_division_unions_exists:
  2271   assumes "finite iset"
  2272     and "\<forall>i\<in>iset. \<exists>p. p tagged_division_of i \<and> d fine p"
  2273     and "\<forall>i1\<in>iset. \<forall>i2\<in>iset. i1 \<noteq> i2 \<longrightarrow> interior i1 \<inter> interior i2 = {}"
  2274     and "\<Union>iset = i"
  2275    obtains p where "p tagged_division_of i" and "d fine p"
  2276 proof -
  2277   obtain pfn where pfn:
  2278     "\<And>x. x \<in> iset \<Longrightarrow> pfn x tagged_division_of x"
  2279     "\<And>x. x \<in> iset \<Longrightarrow> d fine pfn x"
  2280     using bchoice[OF assms(2)] by auto
  2281   show thesis
  2282     apply (rule_tac p="\<Union>(pfn ` iset)" in that)
  2283     using assms(1) assms(3) assms(4) pfn(1) tagged_division_unions apply force
  2284     by (metis (mono_tags, lifting) fine_unions imageE pfn(2))
  2285 qed
  2286 
  2287 
  2288 subsection \<open>The set we're concerned with must be closed.\<close>
  2289 
  2290 lemma division_of_closed:
  2291   fixes i :: "'n::euclidean_space set"
  2292   shows "s division_of i \<Longrightarrow> closed i"
  2293   unfolding division_of_def by fastforce
  2294 
  2295 subsection \<open>General bisection principle for intervals; might be useful elsewhere.\<close>
  2296 
  2297 lemma interval_bisection_step:
  2298   fixes type :: "'a::euclidean_space"
  2299   assumes "P {}"
  2300     and "\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P (s \<union> t)"
  2301     and "\<not> P (cbox a (b::'a))"
  2302   obtains c d where "\<not> P (cbox c d)"
  2303     and "\<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> c\<bullet>i \<le> d\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i \<and> 2 * (d\<bullet>i - c\<bullet>i) \<le> b\<bullet>i - a\<bullet>i"
  2304 proof -
  2305   have "cbox a b \<noteq> {}"
  2306     using assms(1,3) by metis
  2307   then have ab: "\<And>i. i\<in>Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i"
  2308     by (force simp: mem_box)
  2309   { fix f
  2310     have "\<lbrakk>finite f;
  2311            \<And>s. s\<in>f \<Longrightarrow> P s;
  2312            \<And>s. s\<in>f \<Longrightarrow> \<exists>a b. s = cbox a b;
  2313            \<And>s t. s\<in>f \<Longrightarrow> t\<in>f \<Longrightarrow> s \<noteq> t \<Longrightarrow> interior s \<inter> interior t = {}\<rbrakk> \<Longrightarrow> P (\<Union>f)"
  2314     proof (induct f rule: finite_induct)
  2315       case empty
  2316       show ?case
  2317         using assms(1) by auto
  2318     next
  2319       case (insert x f)
  2320       show ?case
  2321         unfolding Union_insert
  2322         apply (rule assms(2)[rule_format])
  2323         using inter_interior_unions_intervals [of f "interior x"]
  2324         apply (auto simp: insert)
  2325         by (metis IntI empty_iff insert.hyps(2) insert.prems(3) insert_iff)
  2326     qed
  2327   } note UN_cases = this
  2328   let ?A = "{cbox c d | c d::'a. \<forall>i\<in>Basis. (c\<bullet>i = a\<bullet>i) \<and> (d\<bullet>i = (a\<bullet>i + b\<bullet>i) / 2) \<or>
  2329     (c\<bullet>i = (a\<bullet>i + b\<bullet>i) / 2) \<and> (d\<bullet>i = b\<bullet>i)}"
  2330   let ?PP = "\<lambda>c d. \<forall>i\<in>Basis. a\<bullet>i \<le> c\<bullet>i \<and> c\<bullet>i \<le> d\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i \<and> 2 * (d\<bullet>i - c\<bullet>i) \<le> b\<bullet>i - a\<bullet>i"
  2331   {
  2332     presume "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d) \<Longrightarrow> False"
  2333     then show thesis
  2334       unfolding atomize_not not_all
  2335       by (blast intro: that)
  2336   }
  2337   assume as: "\<forall>c d. ?PP c d \<longrightarrow> P (cbox c d)"
  2338   have "P (\<Union>?A)"
  2339   proof (rule UN_cases)
  2340     let ?B = "(\<lambda>s. cbox (\<Sum>i\<in>Basis. (if i \<in> s then a\<bullet>i else (a\<bullet>i + b\<bullet>i) / 2) *\<^sub>R i::'a)
  2341       (\<Sum>i\<in>Basis. (if i \<in> s then (a\<bullet>i + b\<bullet>i) / 2 else b\<bullet>i) *\<^sub>R i)) ` {s. s \<subseteq> Basis}"
  2342     have "?A \<subseteq> ?B"
  2343     proof
  2344       fix x
  2345       assume "x \<in> ?A"
  2346       then obtain c d
  2347         where x:  "x = cbox c d"
  2348                   "\<And>i. i \<in> Basis \<Longrightarrow>
  2349                         c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  2350                         c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i" by blast
  2351       show "x \<in> ?B"
  2352         unfolding image_iff x
  2353         apply (rule_tac x="{i. i\<in>Basis \<and> c\<bullet>i = a\<bullet>i}" in bexI)
  2354         apply (rule arg_cong2 [where f = cbox])
  2355         using x(2) ab
  2356         apply (auto simp add: euclidean_eq_iff[where 'a='a])
  2357         by fastforce
  2358     qed
  2359     then show "finite ?A"
  2360       by (rule finite_subset) auto
  2361   next
  2362     fix s
  2363     assume "s \<in> ?A"
  2364     then obtain c d
  2365       where s: "s = cbox c d"
  2366                "\<And>i. i \<in> Basis \<Longrightarrow>
  2367                      c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  2368                      c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  2369       by blast
  2370     show "P s"
  2371       unfolding s
  2372       apply (rule as[rule_format])
  2373       using ab s(2) by force
  2374     show "\<exists>a b. s = cbox a b"
  2375       unfolding s by auto
  2376     fix t
  2377     assume "t \<in> ?A"
  2378     then obtain e f where t:
  2379       "t = cbox e f"
  2380       "\<And>i. i \<in> Basis \<Longrightarrow>
  2381         e \<bullet> i = a \<bullet> i \<and> f \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  2382         e \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> f \<bullet> i = b \<bullet> i"
  2383       by blast
  2384     assume "s \<noteq> t"
  2385     then have "\<not> (c = e \<and> d = f)"
  2386       unfolding s t by auto
  2387     then obtain i where "c\<bullet>i \<noteq> e\<bullet>i \<or> d\<bullet>i \<noteq> f\<bullet>i" and i': "i \<in> Basis"
  2388       unfolding euclidean_eq_iff[where 'a='a] by auto
  2389     then have i: "c\<bullet>i \<noteq> e\<bullet>i" "d\<bullet>i \<noteq> f\<bullet>i"
  2390       using s(2) t(2) apply fastforce
  2391       using t(2)[OF i'] \<open>c \<bullet> i \<noteq> e \<bullet> i \<or> d \<bullet> i \<noteq> f \<bullet> i\<close> i' s(2) t(2) by fastforce
  2392     have *: "\<And>s t. (\<And>a. a \<in> s \<Longrightarrow> a \<in> t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}"
  2393       by auto
  2394     show "interior s \<inter> interior t = {}"
  2395       unfolding s t interior_cbox
  2396     proof (rule *)
  2397       fix x
  2398       assume "x \<in> box c d" "x \<in> box e f"
  2399       then have x: "c\<bullet>i < d\<bullet>i" "e\<bullet>i < f\<bullet>i" "c\<bullet>i < f\<bullet>i" "e\<bullet>i < d\<bullet>i"
  2400         unfolding mem_box using i'
  2401         by force+
  2402       show False  using s(2)[OF i']
  2403       proof safe
  2404         assume as: "c \<bullet> i = a \<bullet> i" "d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2"
  2405         show False
  2406           using t(2)[OF i'] and i x unfolding as by (fastforce simp add:field_simps)
  2407       next
  2408         assume as: "c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2" "d \<bullet> i = b \<bullet> i"
  2409         show False
  2410           using t(2)[OF i'] and i x unfolding as by(fastforce simp add:field_simps)
  2411       qed
  2412     qed
  2413   qed
  2414   also have "\<Union>?A = cbox a b"
  2415   proof (rule set_eqI,rule)
  2416     fix x
  2417     assume "x \<in> \<Union>?A"
  2418     then obtain c d where x:
  2419       "x \<in> cbox c d"
  2420       "\<And>i. i \<in> Basis \<Longrightarrow>
  2421         c \<bullet> i = a \<bullet> i \<and> d \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<or>
  2422         c \<bullet> i = (a \<bullet> i + b \<bullet> i) / 2 \<and> d \<bullet> i = b \<bullet> i"
  2423       by blast
  2424     show "x\<in>cbox a b"
  2425       unfolding mem_box
  2426     proof safe
  2427       fix i :: 'a
  2428       assume i: "i \<in> Basis"
  2429       then show "a \<bullet> i \<le> x \<bullet> i" "x \<bullet> i \<le> b \<bullet> i"
  2430         using x(2)[OF i] x(1)[unfolded mem_box,THEN bspec, OF i] by auto
  2431     qed
  2432   next
  2433     fix x
  2434     assume x: "x \<in> cbox a b"
  2435     have "\<forall>i\<in>Basis.
  2436       \<exists>c d. (c = a\<bullet>i \<and> d = (a\<bullet>i + b\<bullet>i) / 2 \<or> c = (a\<bullet>i + b\<bullet>i) / 2 \<and> d = b\<bullet>i) \<and> c\<le>x\<bullet>i \<and> x\<bullet>i \<le> d"
  2437       (is "\<forall>i\<in>Basis. \<exists>c d. ?P i c d")
  2438       unfolding mem_box
  2439     proof
  2440       fix i :: 'a
  2441       assume i: "i \<in> Basis"
  2442       have "?P i (a\<bullet>i) ((a \<bullet> i + b \<bullet> i) / 2) \<or> ?P i ((a \<bullet> i + b \<bullet> i) / 2) (b\<bullet>i)"
  2443         using x[unfolded mem_box,THEN bspec, OF i] by auto
  2444       then show "\<exists>c d. ?P i c d"
  2445         by blast
  2446     qed
  2447     then show "x\<in>\<Union>?A"
  2448       unfolding Union_iff Bex_def mem_Collect_eq choice_Basis_iff
  2449       apply auto
  2450       apply (rule_tac x="cbox xa xaa" in exI)
  2451       unfolding mem_box
  2452       apply auto
  2453       done
  2454   qed
  2455   finally show False
  2456     using assms by auto
  2457 qed
  2458 
  2459 lemma interval_bisection:
  2460   fixes type :: "'a::euclidean_space"
  2461   assumes "P {}"
  2462     and "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))"
  2463     and "\<not> P (cbox a (b::'a))"
  2464   obtains x where "x \<in> cbox a b"
  2465     and "\<forall>e>0. \<exists>c d. x \<in> cbox c d \<and> cbox c d \<subseteq> ball x e \<and> cbox c d \<subseteq> cbox a b \<and> \<not> P (cbox c d)"
  2466 proof -
  2467   have "\<forall>x. \<exists>y. \<not> P (cbox (fst x) (snd x)) \<longrightarrow> (\<not> P (cbox (fst y) (snd y)) \<and>
  2468     (\<forall>i\<in>Basis. fst x\<bullet>i \<le> fst y\<bullet>i \<and> fst y\<bullet>i \<le> snd y\<bullet>i \<and> snd y\<bullet>i \<le> snd x\<bullet>i \<and>
  2469        2 * (snd y\<bullet>i - fst y\<bullet>i) \<le> snd x\<bullet>i - fst x\<bullet>i))" (is "\<forall>x. ?P x")
  2470   proof
  2471     show "?P x" for x
  2472     proof (cases "P (cbox (fst x) (snd x))")
  2473       case True
  2474       then show ?thesis by auto
  2475     next
  2476       case as: False
  2477       obtain c d where "\<not> P (cbox c d)"
  2478         "\<forall>i\<in>Basis.
  2479            fst x \<bullet> i \<le> c \<bullet> i \<and>
  2480            c \<bullet> i \<le> d \<bullet> i \<and>
  2481            d \<bullet> i \<le> snd x \<bullet> i \<and>
  2482            2 * (d \<bullet> i - c \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i"
  2483         by (rule interval_bisection_step[of P, OF assms(1-2) as])
  2484       then show ?thesis
  2485         apply -
  2486         apply (rule_tac x="(c,d)" in exI)
  2487         apply auto
  2488         done
  2489     qed
  2490   qed
  2491   then obtain f where f:
  2492     "\<forall>x.
  2493       \<not> P (cbox (fst x) (snd x)) \<longrightarrow>
  2494       \<not> P (cbox (fst (f x)) (snd (f x))) \<and>
  2495         (\<forall>i\<in>Basis.
  2496             fst x \<bullet> i \<le> fst (f x) \<bullet> i \<and>
  2497             fst (f x) \<bullet> i \<le> snd (f x) \<bullet> i \<and>
  2498             snd (f x) \<bullet> i \<le> snd x \<bullet> i \<and>
  2499             2 * (snd (f x) \<bullet> i - fst (f x) \<bullet> i) \<le> snd x \<bullet> i - fst x \<bullet> i)"
  2500     apply -
  2501     apply (drule choice)
  2502     apply blast
  2503     done
  2504   define AB A B where ab_def: "AB n = (f ^^ n) (a,b)" "A n = fst(AB n)" "B n = snd(AB n)" for n
  2505   have "A 0 = a" "B 0 = b" "\<And>n. \<not> P (cbox (A(Suc n)) (B(Suc n))) \<and>
  2506     (\<forall>i\<in>Basis. A(n)\<bullet>i \<le> A(Suc n)\<bullet>i \<and> A(Suc n)\<bullet>i \<le> B(Suc n)\<bullet>i \<and> B(Suc n)\<bullet>i \<le> B(n)\<bullet>i \<and>
  2507     2 * (B(Suc n)\<bullet>i - A(Suc n)\<bullet>i) \<le> B(n)\<bullet>i - A(n)\<bullet>i)" (is "\<And>n. ?P n")
  2508   proof -
  2509     show "A 0 = a" "B 0 = b"
  2510       unfolding ab_def by auto
  2511     note S = ab_def funpow.simps o_def id_apply
  2512     show "?P n" for n
  2513     proof (induct n)
  2514       case 0
  2515       then show ?case
  2516         unfolding S
  2517         apply (rule f[rule_format]) using assms(3)
  2518         apply auto
  2519         done
  2520     next
  2521       case (Suc n)
  2522       show ?case
  2523         unfolding S
  2524         apply (rule f[rule_format])
  2525         using Suc
  2526         unfolding S
  2527         apply auto
  2528         done
  2529     qed
  2530   qed
  2531   note AB = this(1-2) conjunctD2[OF this(3),rule_format]
  2532 
  2533   have interv: "\<exists>n. \<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e"
  2534     if e: "0 < e" for e
  2535   proof -
  2536     obtain n where n: "(\<Sum>i\<in>Basis. b \<bullet> i - a \<bullet> i) / e < 2 ^ n"
  2537       using real_arch_pow[of 2 "(setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis) / e"] by auto
  2538     show ?thesis
  2539     proof (rule exI [where x=n], clarify)
  2540       fix x y
  2541       assume xy: "x\<in>cbox (A n) (B n)" "y\<in>cbox (A n) (B n)"
  2542       have "dist x y \<le> setsum (\<lambda>i. \<bar>(x - y)\<bullet>i\<bar>) Basis"
  2543         unfolding dist_norm by(rule norm_le_l1)
  2544       also have "\<dots> \<le> setsum (\<lambda>i. B n\<bullet>i - A n\<bullet>i) Basis"
  2545       proof (rule setsum_mono)
  2546         fix i :: 'a
  2547         assume i: "i \<in> Basis"
  2548         show "\<bar>(x - y) \<bullet> i\<bar> \<le> B n \<bullet> i - A n \<bullet> i"
  2549           using xy[unfolded mem_box,THEN bspec, OF i]
  2550           by (auto simp: inner_diff_left)
  2551       qed
  2552       also have "\<dots> \<le> setsum (\<lambda>i. b\<bullet>i - a\<bullet>i) Basis / 2^n"
  2553         unfolding setsum_divide_distrib
  2554       proof (rule setsum_mono)
  2555         show "B n \<bullet> i - A n \<bullet> i \<le> (b \<bullet> i - a \<bullet> i) / 2 ^ n" if i: "i \<in> Basis" for i
  2556         proof (induct n)
  2557           case 0
  2558           then show ?case
  2559             unfolding AB by auto
  2560         next
  2561           case (Suc n)
  2562           have "B (Suc n) \<bullet> i - A (Suc n) \<bullet> i \<le> (B n \<bullet> i - A n \<bullet> i) / 2"
  2563             using AB(4)[of i n] using i by auto
  2564           also have "\<dots> \<le> (b \<bullet> i - a \<bullet> i) / 2 ^ Suc n"
  2565             using Suc by (auto simp add: field_simps)
  2566           finally show ?case .
  2567         qed
  2568       qed
  2569       also have "\<dots> < e"
  2570         using n using e by (auto simp add: field_simps)
  2571       finally show "dist x y < e" .
  2572     qed
  2573   qed
  2574   {
  2575     fix n m :: nat
  2576     assume "m \<le> n" then have "cbox (A n) (B n) \<subseteq> cbox (A m) (B m)"
  2577     proof (induction rule: inc_induct)
  2578       case (step i)
  2579       show ?case
  2580         using AB(4) by (intro order_trans[OF step.IH] subset_box_imp) auto
  2581     qed simp
  2582   } note ABsubset = this
  2583   have "\<exists>a. \<forall>n. a\<in> cbox (A n) (B n)"
  2584     by (rule decreasing_closed_nest[rule_format,OF closed_cbox _ ABsubset interv])
  2585       (metis nat.exhaust AB(1-3) assms(1,3))
  2586   then obtain x0 where x0: "\<And>n. x0 \<in> cbox (A n) (B n)"
  2587     by blast
  2588   show thesis
  2589   proof (rule that[rule_format, of x0])
  2590     show "x0\<in>cbox a b"
  2591       using x0[of 0] unfolding AB .
  2592     fix e :: real
  2593     assume "e > 0"
  2594     from interv[OF this] obtain n
  2595       where n: "\<forall>x\<in>cbox (A n) (B n). \<forall>y\<in>cbox (A n) (B n). dist x y < e" ..
  2596     have "\<not> P (cbox (A n) (B n))"
  2597       apply (cases "0 < n")
  2598       using AB(3)[of "n - 1"] assms(3) AB(1-2)
  2599       apply auto
  2600       done
  2601     moreover have "cbox (A n) (B n) \<subseteq> ball x0 e"
  2602       using n using x0[of n] by auto
  2603     moreover have "cbox (A n) (B n) \<subseteq> cbox a b"
  2604       unfolding AB(1-2)[symmetric] by (rule ABsubset) auto
  2605     ultimately show "\<exists>c d. x0 \<in> cbox c d \<and> cbox c d \<subseteq> ball x0 e \<and> cbox c d \<subseteq> cbox a b \<and> \<not> P (cbox c d)"
  2606       apply (rule_tac x="A n" in exI)
  2607       apply (rule_tac x="B n" in exI)
  2608       apply (auto simp: x0)
  2609       done
  2610   qed
  2611 qed
  2612 
  2613 
  2614 subsection \<open>Cousin's lemma.\<close>
  2615 
  2616 lemma fine_division_exists:
  2617   fixes a b :: "'a::euclidean_space"
  2618   assumes "gauge g"
  2619   obtains p where "p tagged_division_of (cbox a b)" "g fine p"
  2620 proof -
  2621   presume "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p) \<Longrightarrow> False"
  2622   then obtain p where "p tagged_division_of (cbox a b)" "g fine p"
  2623     by blast
  2624   then show thesis ..
  2625 next
  2626   assume as: "\<not> (\<exists>p. p tagged_division_of (cbox a b) \<and> g fine p)"
  2627   obtain x where x:
  2628       "x \<in> (cbox a b)"
  2629       "\<And>e. 0 < e \<Longrightarrow>
  2630         \<exists>c d.
  2631           x \<in> cbox c d \<and>
  2632           cbox c d \<subseteq> ball x e \<and>
  2633           cbox c d \<subseteq> (cbox a b) \<and>
  2634           \<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  2635     apply (rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p", OF _ _ as])
  2636     apply (simp add: fine_def)
  2637     apply (metis tagged_division_union fine_union)
  2638     apply (auto simp: )
  2639     done
  2640   obtain e where e: "e > 0" "ball x e \<subseteq> g x"
  2641     using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
  2642   from x(2)[OF e(1)]
  2643   obtain c d where c_d: "x \<in> cbox c d"
  2644                         "cbox c d \<subseteq> ball x e"
  2645                         "cbox c d \<subseteq> cbox a b"
  2646                         "\<not> (\<exists>p. p tagged_division_of cbox c d \<and> g fine p)"
  2647     by blast
  2648   have "g fine {(x, cbox c d)}"
  2649     unfolding fine_def using e using c_d(2) by auto
  2650   then show False
  2651     using tagged_division_of_self[OF c_d(1)] using c_d by auto
  2652 qed
  2653 
  2654 lemma fine_division_exists_real:
  2655   fixes a b :: real
  2656   assumes "gauge g"
  2657   obtains p where "p tagged_division_of {a .. b}" "g fine p"
  2658   by (metis assms box_real(2) fine_division_exists)
  2659 
  2660 subsection \<open>Basic theorems about integrals.\<close>
  2661 
  2662 lemma has_integral_unique:
  2663   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2664   assumes "(f has_integral k1) i"
  2665     and "(f has_integral k2) i"
  2666   shows "k1 = k2"
  2667 proof (rule ccontr)
  2668   let ?e = "norm (k1 - k2) / 2"
  2669   assume as: "k1 \<noteq> k2"
  2670   then have e: "?e > 0"
  2671     by auto
  2672   have lem: False
  2673     if f_k1: "(f has_integral k1) (cbox a b)"
  2674     and f_k2: "(f has_integral k2) (cbox a b)"
  2675     and "k1 \<noteq> k2"
  2676     for f :: "'n \<Rightarrow> 'a" and a b k1 k2
  2677   proof -
  2678     let ?e = "norm (k1 - k2) / 2"
  2679     from \<open>k1 \<noteq> k2\<close> have e: "?e > 0" by auto
  2680     obtain d1 where d1:
  2681         "gauge d1"
  2682         "\<And>p. p tagged_division_of cbox a b \<Longrightarrow>
  2683           d1 fine p \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k1) < norm (k1 - k2) / 2"
  2684       by (rule has_integralD[OF f_k1 e]) blast
  2685     obtain d2 where d2:
  2686         "gauge d2"
  2687         "\<And>p. p tagged_division_of cbox a b \<Longrightarrow>
  2688           d2 fine p \<Longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k2) < norm (k1 - k2) / 2"
  2689       by (rule has_integralD[OF f_k2 e]) blast
  2690     obtain p where p:
  2691         "p tagged_division_of cbox a b"
  2692         "(\<lambda>x. d1 x \<inter> d2 x) fine p"
  2693       by (rule fine_division_exists[OF gauge_inter[OF d1(1) d2(1)]])
  2694     let ?c = "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)"
  2695     have "norm (k1 - k2) \<le> norm (?c - k2) + norm (?c - k1)"
  2696       using norm_triangle_ineq4[of "k1 - ?c" "k2 - ?c"]
  2697       by (auto simp add:algebra_simps norm_minus_commute)
  2698     also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
  2699       apply (rule add_strict_mono)
  2700       apply (rule_tac[!] d2(2) d1(2))
  2701       using p unfolding fine_def
  2702       apply auto
  2703       done
  2704     finally show False by auto
  2705   qed
  2706   {
  2707     presume "\<not> (\<exists>a b. i = cbox a b) \<Longrightarrow> False"
  2708     then show False
  2709       using as assms lem by blast
  2710   }
  2711   assume as: "\<not> (\<exists>a b. i = cbox a b)"
  2712   obtain B1 where B1:
  2713       "0 < B1"
  2714       "\<And>a b. ball 0 B1 \<subseteq> cbox a b \<Longrightarrow>
  2715         \<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b) \<and>
  2716           norm (z - k1) < norm (k1 - k2) / 2"
  2717     by (rule has_integral_altD[OF assms(1) as,OF e]) blast
  2718   obtain B2 where B2:
  2719       "0 < B2"
  2720       "\<And>a b. ball 0 B2 \<subseteq> cbox a b \<Longrightarrow>
  2721         \<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b) \<and>
  2722           norm (z - k2) < norm (k1 - k2) / 2"
  2723     by (rule has_integral_altD[OF assms(2) as,OF e]) blast
  2724   have "\<exists>a b::'n. ball 0 B1 \<union> ball 0 B2 \<subseteq> cbox a b"
  2725     apply (rule bounded_subset_cbox)
  2726     using bounded_Un bounded_ball
  2727     apply auto
  2728     done
  2729   then obtain a b :: 'n where ab: "ball 0 B1 \<subseteq> cbox a b" "ball 0 B2 \<subseteq> cbox a b"
  2730     by blast
  2731   obtain w where w:
  2732     "((\<lambda>x. if x \<in> i then f x else 0) has_integral w) (cbox a b)"
  2733     "norm (w - k1) < norm (k1 - k2) / 2"
  2734     using B1(2)[OF ab(1)] by blast
  2735   obtain z where z:
  2736     "((\<lambda>x. if x \<in> i then f x else 0) has_integral z) (cbox a b)"
  2737     "norm (z - k2) < norm (k1 - k2) / 2"
  2738     using B2(2)[OF ab(2)] by blast
  2739   have "z = w"
  2740     using lem[OF w(1) z(1)] by auto
  2741   then have "norm (k1 - k2) \<le> norm (z - k2) + norm (w - k1)"
  2742     using norm_triangle_ineq4 [of "k1 - w" "k2 - z"]
  2743     by (auto simp add: norm_minus_commute)
  2744   also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
  2745     apply (rule add_strict_mono)
  2746     apply (rule_tac[!] z(2) w(2))
  2747     done
  2748   finally show False by auto
  2749 qed
  2750 
  2751 lemma integral_unique [intro]: "(f has_integral y) k \<Longrightarrow> integral k f = y"
  2752   unfolding integral_def
  2753   by (rule some_equality) (auto intro: has_integral_unique)
  2754 
  2755 lemma eq_integralD: "integral k f = y \<Longrightarrow> (f has_integral y) k \<or> ~ f integrable_on k \<and> y=0"
  2756   unfolding integral_def integrable_on_def
  2757   apply (erule subst)
  2758   apply (rule someI_ex)
  2759   by blast
  2760 
  2761 lemma has_integral_is_0:
  2762   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2763   assumes "\<forall>x\<in>s. f x = 0"
  2764   shows "(f has_integral 0) s"
  2765 proof -
  2766   have lem: "\<And>a b. \<And>f::'n \<Rightarrow> 'a.
  2767     (\<forall>x\<in>cbox a b. f(x) = 0) \<Longrightarrow> (f has_integral 0) (cbox a b)"
  2768     unfolding has_integral
  2769   proof clarify
  2770     fix a b e
  2771     fix f :: "'n \<Rightarrow> 'a"
  2772     assume as: "\<forall>x\<in>cbox a b. f x = 0" "0 < (e::real)"
  2773     have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  2774       if p: "p tagged_division_of cbox a b" for p
  2775     proof -
  2776       have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) = 0"
  2777       proof (rule setsum.neutral, rule)
  2778         fix x
  2779         assume x: "x \<in> p"
  2780         have "f (fst x) = 0"
  2781           using tagged_division_ofD(2-3)[OF p, of "fst x" "snd x"] using as x by auto
  2782         then show "(\<lambda>(x, k). content k *\<^sub>R f x) x = 0"
  2783           apply (subst surjective_pairing[of x])
  2784           unfolding split_conv
  2785           apply auto
  2786           done
  2787       qed
  2788       then show ?thesis
  2789         using as by auto
  2790     qed
  2791     then show "\<exists>d. gauge d \<and>
  2792         (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e)"
  2793       by auto
  2794   qed
  2795   {
  2796     presume "\<not> (\<exists>a b. s = cbox a b) \<Longrightarrow> ?thesis"
  2797     with assms lem show ?thesis
  2798       by blast
  2799   }
  2800   have *: "(\<lambda>x. if x \<in> s then f x else 0) = (\<lambda>x. 0)"
  2801     apply (rule ext)
  2802     using assms
  2803     apply auto
  2804     done
  2805   assume "\<not> (\<exists>a b. s = cbox a b)"
  2806   then show ?thesis
  2807     using lem
  2808     by (subst has_integral_alt) (force simp add: *)
  2809 qed
  2810 
  2811 lemma has_integral_0[simp]: "((\<lambda>x::'n::euclidean_space. 0) has_integral 0) s"
  2812   by (rule has_integral_is_0) auto
  2813 
  2814 lemma has_integral_0_eq[simp]: "((\<lambda>x. 0) has_integral i) s \<longleftrightarrow> i = 0"
  2815   using has_integral_unique[OF has_integral_0] by auto
  2816 
  2817 lemma has_integral_linear:
  2818   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2819   assumes "(f has_integral y) s"
  2820     and "bounded_linear h"
  2821   shows "((h \<circ> f) has_integral ((h y))) s"
  2822 proof -
  2823   interpret bounded_linear h
  2824     using assms(2) .
  2825   from pos_bounded obtain B where B: "0 < B" "\<And>x. norm (h x) \<le> norm x * B"
  2826     by blast
  2827   have lem: "\<And>(f :: 'n \<Rightarrow> 'a) y a b.
  2828     (f has_integral y) (cbox a b) \<Longrightarrow> ((h \<circ> f) has_integral h y) (cbox a b)"
  2829     unfolding has_integral
  2830   proof (clarify, goal_cases)
  2831     case prems: (1 f y a b e)
  2832     from pos_bounded
  2833     obtain B where B: "0 < B" "\<And>x. norm (h x) \<le> norm x * B"
  2834       by blast
  2835     have "e / B > 0" using prems(2) B by simp
  2836     then obtain g
  2837       where g: "gauge g"
  2838                "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> g fine p \<Longrightarrow>
  2839                     norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - y) < e / B"
  2840         using prems(1) by auto
  2841     {
  2842       fix p
  2843       assume as: "p tagged_division_of (cbox a b)" "g fine p"
  2844       have hc: "\<And>x k. h ((\<lambda>(x, k). content k *\<^sub>R f x) x) = (\<lambda>(x, k). h (content k *\<^sub>R f x)) x"
  2845         by auto
  2846       then have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = setsum (h \<circ> (\<lambda>(x, k). content k *\<^sub>R f x)) p"
  2847         unfolding o_def unfolding scaleR[symmetric] hc by simp
  2848       also have "\<dots> = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)"
  2849         using setsum[of "\<lambda>(x,k). content k *\<^sub>R f x" p] using as by auto
  2850       finally have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" .
  2851       then have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) - h y) < e"
  2852         apply (simp add: diff[symmetric])
  2853         apply (rule le_less_trans[OF B(2)])
  2854         using g(2)[OF as] B(1)
  2855         apply (auto simp add: field_simps)
  2856         done
  2857     }
  2858     with g show ?case
  2859       by (rule_tac x=g in exI) auto
  2860   qed
  2861   {
  2862     presume "\<not> (\<exists>a b. s = cbox a b) \<Longrightarrow> ?thesis"
  2863     then show ?thesis
  2864       using assms(1) lem by blast
  2865   }
  2866   assume as: "\<not> (\<exists>a b. s = cbox a b)"
  2867   then show ?thesis
  2868   proof (subst has_integral_alt, clarsimp)
  2869     fix e :: real
  2870     assume e: "e > 0"
  2871     have *: "0 < e/B" using e B(1) by simp
  2872     obtain M where M:
  2873       "M > 0"
  2874       "\<And>a b. ball 0 M \<subseteq> cbox a b \<Longrightarrow>
  2875         \<exists>z. ((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b) \<and> norm (z - y) < e / B"
  2876       using has_integral_altD[OF assms(1) as *] by blast
  2877     show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  2878       (\<exists>z. ((\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) has_integral z) (cbox a b) \<and> norm (z - h y) < e)"
  2879     proof (rule_tac x=M in exI, clarsimp simp add: M, goal_cases)
  2880       case prems: (1 a b)
  2881       obtain z where z:
  2882         "((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b)"
  2883         "norm (z - y) < e / B"
  2884         using M(2)[OF prems(1)] by blast
  2885       have *: "(\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) = h \<circ> (\<lambda>x. if x \<in> s then f x else 0)"
  2886         using zero by auto
  2887       show ?case
  2888         apply (rule_tac x="h z" in exI)
  2889         apply (simp add: * lem z(1))
  2890         apply (metis B diff le_less_trans pos_less_divide_eq z(2))
  2891         done
  2892     qed
  2893   qed
  2894 qed
  2895 
  2896 lemma has_integral_scaleR_left:
  2897   "(f has_integral y) s \<Longrightarrow> ((\<lambda>x. f x *\<^sub>R c) has_integral (y *\<^sub>R c)) s"
  2898   using has_integral_linear[OF _ bounded_linear_scaleR_left] by (simp add: comp_def)
  2899 
  2900 lemma has_integral_mult_left:
  2901   fixes c :: "_ :: real_normed_algebra"
  2902   shows "(f has_integral y) s \<Longrightarrow> ((\<lambda>x. f x * c) has_integral (y * c)) s"
  2903   using has_integral_linear[OF _ bounded_linear_mult_left] by (simp add: comp_def)
  2904 
  2905 text\<open>The case analysis eliminates the condition @{term "f integrable_on s"} at the cost
  2906      of the type class constraint \<open>division_ring\<close>\<close>
  2907 corollary integral_mult_left [simp]:
  2908   fixes c:: "'a::{real_normed_algebra,division_ring}"
  2909   shows "integral s (\<lambda>x. f x * c) = integral s f * c"
  2910 proof (cases "f integrable_on s \<or> c = 0")
  2911   case True then show ?thesis
  2912     by (force intro: has_integral_mult_left)
  2913 next
  2914   case False then have "~ (\<lambda>x. f x * c) integrable_on s"
  2915     using has_integral_mult_left [of "(\<lambda>x. f x * c)" _ s "inverse c"]
  2916     by (force simp add: mult.assoc)
  2917   with False show ?thesis by (simp add: not_integrable_integral)
  2918 qed
  2919 
  2920 corollary integral_mult_right [simp]:
  2921   fixes c:: "'a::{real_normed_field}"
  2922   shows "integral s (\<lambda>x. c * f x) = c * integral s f"
  2923 by (simp add: mult.commute [of c])
  2924 
  2925 corollary integral_divide [simp]:
  2926   fixes z :: "'a::real_normed_field"
  2927   shows "integral S (\<lambda>x. f x / z) = integral S (\<lambda>x. f x) / z"
  2928 using integral_mult_left [of S f "inverse z"]
  2929   by (simp add: divide_inverse_commute)
  2930 
  2931 lemma has_integral_mult_right:
  2932   fixes c :: "'a :: real_normed_algebra"
  2933   shows "(f has_integral y) i \<Longrightarrow> ((\<lambda>x. c * f x) has_integral (c * y)) i"
  2934   using has_integral_linear[OF _ bounded_linear_mult_right] by (simp add: comp_def)
  2935 
  2936 lemma has_integral_cmul: "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_integral (c *\<^sub>R k)) s"
  2937   unfolding o_def[symmetric]
  2938   by (metis has_integral_linear bounded_linear_scaleR_right)
  2939 
  2940 lemma has_integral_cmult_real:
  2941   fixes c :: real
  2942   assumes "c \<noteq> 0 \<Longrightarrow> (f has_integral x) A"
  2943   shows "((\<lambda>x. c * f x) has_integral c * x) A"
  2944 proof (cases "c = 0")
  2945   case True
  2946   then show ?thesis by simp
  2947 next
  2948   case False
  2949   from has_integral_cmul[OF assms[OF this], of c] show ?thesis
  2950     unfolding real_scaleR_def .
  2951 qed
  2952 
  2953 lemma has_integral_neg: "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. -(f x)) has_integral -k) s"
  2954   by (drule_tac c="-1" in has_integral_cmul) auto
  2955 
  2956 lemma has_integral_add:
  2957   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2958   assumes "(f has_integral k) s"
  2959     and "(g has_integral l) s"
  2960   shows "((\<lambda>x. f x + g x) has_integral (k + l)) s"
  2961 proof -
  2962   have lem: "((\<lambda>x. f x + g x) has_integral (k + l)) (cbox a b)"
  2963     if f_k: "(f has_integral k) (cbox a b)"
  2964     and g_l: "(g has_integral l) (cbox a b)"
  2965     for f :: "'n \<Rightarrow> 'a" and g a b k l
  2966     unfolding has_integral
  2967   proof clarify
  2968     fix e :: real
  2969     assume e: "e > 0"
  2970     then have *: "e / 2 > 0"
  2971       by auto
  2972     obtain d1 where d1:
  2973       "gauge d1"
  2974       "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d1 fine p \<Longrightarrow>
  2975         norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k) < e / 2"
  2976       using has_integralD[OF f_k *] by blast
  2977     obtain d2 where d2:
  2978       "gauge d2"
  2979       "\<And>p. p tagged_division_of (cbox a b) \<Longrightarrow> d2 fine p \<Longrightarrow>
  2980         norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - l) < e / 2"
  2981       using has_integralD[OF g_l *] by blast
  2982     show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  2983               norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e)"
  2984     proof (rule exI [where x="\<lambda>x. (d1 x) \<inter> (d2 x)"], clarsimp simp add: gauge_inter[OF d1(1) d2(1)])
  2985       fix p
  2986       assume as: "p tagged_division_of (cbox a b)" "(\<lambda>x. d1 x \<inter> d2 x) fine p"
  2987       have *: "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) =
  2988         (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p. content k *\<^sub>R g x)"
  2989         unfolding scaleR_right_distrib setsum.distrib[of "\<lambda>(x,k). content k *\<^sub>R f x" "\<lambda>(x,k). content k *\<^sub>R g x" p,symmetric]
  2990         by (rule setsum.cong) auto
  2991       from as have fine: "d1 fine p" "d2 fine p"
  2992         unfolding fine_inter by auto
  2993       have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) =
  2994             norm (((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k) + ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - l))"
  2995         unfolding * by (auto simp add: algebra_simps)
  2996       also have "\<dots> < e/2 + e/2"
  2997         apply (rule le_less_trans[OF norm_triangle_ineq])
  2998         using as d1 d2 fine
  2999         apply (blast intro: add_strict_mono)
  3000         done
  3001       finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e"
  3002         by auto
  3003     qed
  3004   qed
  3005   {
  3006     presume "\<not> (\<exists>a b. s = cbox a b) \<Longrightarrow> ?thesis"
  3007     then show ?thesis
  3008       using assms lem by force
  3009   }
  3010   assume as: "\<not> (\<exists>a b. s = cbox a b)"
  3011   then show ?thesis
  3012   proof (subst has_integral_alt, clarsimp, goal_cases)
  3013     case (1 e)
  3014     then have *: "e / 2 > 0"
  3015       by auto
  3016     from has_integral_altD[OF assms(1) as *]
  3017     obtain B1 where B1:
  3018         "0 < B1"
  3019         "\<And>a b. ball 0 B1 \<subseteq> cbox a b \<Longrightarrow>
  3020           \<exists>z. ((\<lambda>x. if x \<in> s then f x else 0) has_integral z) (cbox a b) \<and> norm (z - k) < e / 2"
  3021       by blast
  3022     from has_integral_altD[OF assms(2) as *]
  3023     obtain B2 where B2:
  3024         "0 < B2"
  3025         "\<And>a b. ball 0 B2 \<subseteq> (cbox a b) \<Longrightarrow>
  3026           \<exists>z. ((\<lambda>x. if x \<in> s then g x else 0) has_integral z) (cbox a b) \<and> norm (z - l) < e / 2"
  3027       by blast
  3028     show ?case
  3029     proof (rule_tac x="max B1 B2" in exI, clarsimp simp add: max.strict_coboundedI1 B1)
  3030       fix a b
  3031       assume "ball 0 (max B1 B2) \<subseteq> cbox a (b::'n)"
  3032       then have *: "ball 0 B1 \<subseteq> cbox a (b::'n)" "ball 0 B2 \<subseteq> cbox a (b::'n)"
  3033         by auto
  3034       obtain w where w:
  3035         "((\<lambda>x. if x \<in> s then f x else 0) has_integral w) (cbox a b)"
  3036         "norm (w - k) < e / 2"
  3037         using B1(2)[OF *(1)] by blast
  3038       obtain z where z:
  3039         "((\<lambda>x. if x \<in> s then g x else 0) has_integral z) (cbox a b)"
  3040         "norm (z - l) < e / 2"
  3041         using B2(2)[OF *(2)] by blast
  3042       have *: "\<And>x. (if x \<in> s then f x + g x else 0) =
  3043         (if x \<in> s then f x else 0) + (if x \<in> s then g x else 0)"
  3044         by auto
  3045       show "\<exists>z. ((\<lambda>x. if x \<in> s then f x + g x else 0) has_integral z) (cbox a b) \<and> norm (z - (k + l)) < e"
  3046         apply (rule_tac x="w + z" in exI)
  3047         apply (simp add: lem[OF w(1) z(1), unfolded *[symmetric]])
  3048         using norm_triangle_ineq[of "w - k" "z - l"] w(2) z(2)
  3049         apply (auto simp add: field_simps)
  3050         done
  3051     qed
  3052   qed
  3053 qed
  3054 
  3055 lemma has_integral_sub:
  3056   "(f has_integral k) s \<Longrightarrow> (g has_integral l) s \<Longrightarrow>
  3057     ((\<lambda>x. f x - g x) has_integral (k - l)) s"
  3058   using has_integral_add[OF _ has_integral_neg, of f k s g l]
  3059   by (auto simp: algebra_simps)
  3060 
  3061 lemma integral_0 [simp]:
  3062   "integral s (\<lambda>x::'n::euclidean_space. 0::'m::real_normed_vector) = 0"
  3063   by (rule integral_unique has_integral_0)+
  3064 
  3065 lemma integral_add: "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
  3066     integral s (\<lambda>x. f x + g x) = integral s f + integral s g"
  3067   by (rule integral_unique) (metis integrable_integral has_integral_add)
  3068 
  3069 lemma integral_cmul [simp]: "integral s (\<lambda>x. c *\<^sub>R f x) = c *\<^sub>R integral s f"
  3070 proof (cases "f integrable_on s \<or> c = 0")
  3071   case True with has_integral_cmul show ?thesis by force
  3072 next
  3073   case False then have "~ (\<lambda>x. c *\<^sub>R f x) integrable_on s"
  3074     using has_integral_cmul [of "(\<lambda>x. c *\<^sub>R f x)" _ s "inverse c"]
  3075     by force
  3076   with False show ?thesis by (simp add: not_integrable_integral)
  3077 qed
  3078 
  3079 lemma integral_neg [simp]: "integral s (\<lambda>x. - f x) = - integral s f"
  3080 proof (cases "f integrable_on s")
  3081   case True then show ?thesis
  3082     by (simp add: has_integral_neg integrable_integral integral_unique)
  3083 next
  3084   case False then have "~ (\<lambda>x. - f x) integrable_on s"
  3085     using has_integral_neg [of "(\<lambda>x. - f x)" _ s ]
  3086     by force
  3087   with False show ?thesis by (simp add: not_integrable_integral)
  3088 qed
  3089 
  3090 lemma integral_diff: "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
  3091     integral s (\<lambda>x. f x - g x) = integral s f - integral s g"
  3092   by (rule integral_unique) (metis integrable_integral has_integral_sub)
  3093 
  3094 lemma integrable_0: "(\<lambda>x. 0) integrable_on s"
  3095   unfolding integrable_on_def using has_integral_0 by auto
  3096 
  3097 lemma integrable_add: "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x + g x) integrable_on s"
  3098   unfolding integrable_on_def by(auto intro: has_integral_add)
  3099 
  3100 lemma integrable_cmul: "f integrable_on s \<Longrightarrow> (\<lambda>x. c *\<^sub>R f(x)) integrable_on s"
  3101   unfolding integrable_on_def by(auto intro: has_integral_cmul)
  3102 
  3103 lemma integrable_on_cmult_iff:
  3104   fixes c :: real
  3105   assumes "c \<noteq> 0"
  3106   shows "(\<lambda>x. c * f x) integrable_on s \<longleftrightarrow> f integrable_on s"
  3107   using integrable_cmul[of "\<lambda>x. c * f x" s "1 / c"] integrable_cmul[of f s c] \<open>c \<noteq> 0\<close>
  3108   by auto
  3109 
  3110 lemma integrable_on_cmult_left:
  3111   assumes "f integrable_on s"
  3112   shows "(\<lambda>x. of_real c * f x) integrable_on s"
  3113     using integrable_cmul[of f s "of_real c"] assms
  3114     by (simp add: scaleR_conv_of_real)
  3115 
  3116 lemma integrable_neg: "f integrable_on s \<Longrightarrow> (\<lambda>x. -f(x)) integrable_on s"
  3117   unfolding integrable_on_def by(auto intro: has_integral_neg)
  3118 
  3119 lemma integrable_diff:
  3120   "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x - g x) integrable_on s"
  3121   unfolding integrable_on_def by(auto intro: has_integral_sub)
  3122 
  3123 lemma integrable_linear:
  3124   "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> (h \<circ> f) integrable_on s"
  3125   unfolding integrable_on_def by(auto intro: has_integral_linear)
  3126 
  3127 lemma integral_linear:
  3128   "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> integral s (h \<circ> f) = h (integral s f)"
  3129   apply (rule has_integral_unique [where i=s and f = "h \<circ> f"])
  3130   apply (simp_all add: integrable_integral integrable_linear has_integral_linear )
  3131   done
  3132 
  3133 lemma integral_component_eq[simp]:
  3134   fixes f :: "'n::euclidean_space \<Rightarrow> 'm::euclidean_space"
  3135   assumes "f integrable_on s"
  3136   shows "integral s (\<lambda>x. f x \<bullet> k) = integral s f \<bullet> k"
  3137   unfolding integral_linear[OF assms(1) bounded_linear_component,unfolded o_def] ..
  3138 
  3139 lemma has_integral_setsum:
  3140   assumes "finite t"
  3141     and "\<forall>a\<in>t. ((f a) has_integral (i a)) s"
  3142   shows "((\<lambda>x. setsum (\<lambda>a. f a x) t) has_integral (setsum i t)) s"
  3143   using assms(1) subset_refl[of t]
  3144 proof (induct rule: finite_subset_induct)
  3145   case empty
  3146   then show ?case by auto
  3147 next
  3148   case (insert x F)
  3149   with assms show ?case
  3150     by (simp add: has_integral_add)
  3151 qed
  3152 
  3153 lemma integral_setsum:
  3154   "\<lbrakk>finite t;  \<forall>a\<in>t. (f a) integrable_on s\<rbrakk> \<Longrightarrow>
  3155    integral s (\<lambda>x. setsum (\<lambda>a. f a x) t) = setsum (\<lambda>a. integral s (f a)) t"
  3156   by (auto intro: has_integral_setsum integrable_integral)
  3157 
  3158 lemma integrable_setsum:
  3159   "\<lbrakk>finite t;  \<forall>a\<in>t. (f a) integrable_on s\<rbrakk> \<Longrightarrow> (\<lambda>x. setsum (\<lambda>a. f a x) t) integrable_on s"
  3160   unfolding integrable_on_def
  3161   apply (drule bchoice)
  3162   using has_integral_setsum[of t]
  3163   apply auto
  3164   done
  3165 
  3166 lemma has_integral_eq:
  3167   assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  3168     and "(f has_integral k) s"
  3169   shows "(g has_integral k) s"
  3170   using has_integral_sub[OF assms(2), of "\<lambda>x. f x - g x" 0]
  3171   using has_integral_is_0[of s "\<lambda>x. f x - g x"]
  3172   using assms(1)
  3173   by auto
  3174 
  3175 lemma integrable_eq: "(\<And>x. x \<in> s \<Longrightarrow> f x = g x) \<Longrightarrow> f integrable_on s \<Longrightarrow> g integrable_on s"
  3176   unfolding integrable_on_def
  3177   using has_integral_eq[of s f g] has_integral_eq by blast
  3178 
  3179 lemma has_integral_cong:
  3180   assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  3181   shows "(f has_integral i) s = (g has_integral i) s"
  3182   using has_integral_eq[of s f g] has_integral_eq[of s g f] assms
  3183   by auto
  3184 
  3185 lemma integral_cong:
  3186   assumes "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  3187   shows "integral s f = integral s g"
  3188   unfolding integral_def
  3189 by (metis (full_types, hide_lams) assms has_integral_cong integrable_eq)
  3190 
  3191 lemma integrable_on_cmult_left_iff [simp]:
  3192   assumes "c \<noteq> 0"
  3193   shows "(\<lambda>x. of_real c * f x) integrable_on s \<longleftrightarrow> f integrable_on s"
  3194         (is "?lhs = ?rhs")
  3195 proof
  3196   assume ?lhs
  3197   then have "(\<lambda>x. of_real (1 / c) * (of_real c * f x)) integrable_on s"
  3198     using integrable_cmul[of "\<lambda>x. of_real c * f x" s "1 / of_real c"]
  3199     by (simp add: scaleR_conv_of_real)
  3200   then have "(\<lambda>x. (of_real (1 / c) * of_real c * f x)) integrable_on s"
  3201     by (simp add: algebra_simps)
  3202   with \<open>c \<noteq> 0\<close> show ?rhs
  3203     by (metis (no_types, lifting) integrable_eq mult.left_neutral nonzero_divide_eq_eq of_real_1 of_real_mult)
  3204 qed (blast intro: integrable_on_cmult_left)
  3205 
  3206 lemma integrable_on_cmult_right:
  3207   fixes f :: "_ \<Rightarrow> 'b :: {comm_ring,real_algebra_1,real_normed_vector}"
  3208   assumes "f integrable_on s"
  3209   shows "(\<lambda>x. f x * of_real c) integrable_on s"
  3210 using integrable_on_cmult_left [OF assms] by (simp add: mult.commute)
  3211 
  3212 lemma integrable_on_cmult_right_iff [simp]:
  3213   fixes f :: "_ \<Rightarrow> 'b :: {comm_ring,real_algebra_1,real_normed_vector}"
  3214   assumes "c \<noteq> 0"
  3215   shows "(\<lambda>x. f x * of_real c) integrable_on s \<longleftrightarrow> f integrable_on s"
  3216 using integrable_on_cmult_left_iff [OF assms] by (simp add: mult.commute)
  3217 
  3218 lemma integrable_on_cdivide:
  3219   fixes f :: "_ \<Rightarrow> 'b :: real_normed_field"
  3220   assumes "f integrable_on s"
  3221   shows "(\<lambda>x. f x / of_real c) integrable_on s"
  3222 by (simp add: integrable_on_cmult_right divide_inverse assms of_real_inverse [symmetric] del: of_real_inverse)
  3223 
  3224 lemma integrable_on_cdivide_iff [simp]:
  3225   fixes f :: "_ \<Rightarrow> 'b :: real_normed_field"
  3226   assumes "c \<noteq> 0"
  3227   shows "(\<lambda>x. f x / of_real c) integrable_on s \<longleftrightarrow> f integrable_on s"
  3228 by (simp add: divide_inverse assms of_real_inverse [symmetric] del: of_real_inverse)
  3229 
  3230 lemma has_integral_null [intro]:
  3231   assumes "content(cbox a b) = 0"
  3232   shows "(f has_integral 0) (cbox a b)"
  3233 proof -
  3234   have "gauge (\<lambda>x. ball x 1)"
  3235     by auto
  3236   moreover
  3237   {
  3238     fix e :: real
  3239     fix p
  3240     assume e: "e > 0"
  3241     assume p: "p tagged_division_of (cbox a b)"
  3242     have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) = 0"
  3243       unfolding norm_eq_zero diff_0_right
  3244       using setsum_content_null[OF assms(1) p, of f] .
  3245     then have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  3246       using e by auto
  3247   }
  3248   ultimately show ?thesis
  3249     by (auto simp: has_integral)
  3250 qed
  3251 
  3252 lemma has_integral_null_real [intro]:
  3253   assumes "content {a .. b::real} = 0"
  3254   shows "(f has_integral 0) {a .. b}"
  3255   by (metis assms box_real(2) has_integral_null)
  3256 
  3257 lemma has_integral_null_eq[simp]: "content (cbox a b) = 0 \<Longrightarrow> (f has_integral i) (cbox a b) \<longleftrightarrow> i = 0"
  3258   by (auto simp add: has_integral_null dest!: integral_unique)
  3259 
  3260 lemma integral_null [simp]: "content (cbox a b) = 0 \<Longrightarrow> integral (cbox a b) f = 0"
  3261   by (metis has_integral_null integral_unique)
  3262 
  3263 lemma integrable_on_null [intro]: "content (cbox a b) = 0 \<Longrightarrow> f integrable_on (cbox a b)"
  3264   by (simp add: has_integral_integrable)
  3265 
  3266 lemma has_integral_empty[intro]: "(f has_integral 0) {}"
  3267   by (simp add: has_integral_is_0)
  3268 
  3269 lemma has_integral_empty_eq[simp]: "(f has_integral i) {} \<longleftrightarrow> i = 0"
  3270   by (auto simp add: has_integral_empty has_integral_unique)
  3271 
  3272 lemma integrable_on_empty[intro]: "f integrable_on {}"
  3273   unfolding integrable_on_def by auto
  3274 
  3275 lemma integral_empty[simp]: "integral {} f = 0"
  3276   by (rule integral_unique) (rule has_integral_empty)
  3277 
  3278 lemma has_integral_refl[intro]:
  3279   fixes a :: "'a::euclidean_space"
  3280   shows "(f has_integral 0) (cbox a a)"
  3281     and "(f has_integral 0) {a}"
  3282 proof -
  3283   have *: "{a} = cbox a a"
  3284     apply (rule set_eqI)
  3285     unfolding mem_box singleton_iff euclidean_eq_iff[where 'a='a]
  3286     apply safe
  3287     prefer 3
  3288     apply (erule_tac x=b in ballE)
  3289     apply (auto simp add: field_simps)
  3290     done
  3291   show "(f has_integral 0) (cbox a a)" "(f has_integral 0) {a}"
  3292     unfolding *
  3293     apply (rule_tac[!] has_integral_null)
  3294     unfolding content_eq_0_interior
  3295     unfolding interior_cbox
  3296     using box_sing
  3297     apply auto
  3298     done
  3299 qed
  3300 
  3301 lemma integrable_on_refl[intro]: "f integrable_on cbox a a"
  3302   unfolding integrable_on_def by auto
  3303 
  3304 lemma integral_refl [simp]: "integral (cbox a a) f = 0"
  3305   by (rule integral_unique) auto
  3306 
  3307 lemma integral_singleton [simp]: "integral {a} f = 0"
  3308   by auto
  3309 
  3310 lemma integral_blinfun_apply:
  3311   assumes "f integrable_on s"
  3312   shows "integral s (\<lambda>x. blinfun_apply h (f x)) = blinfun_apply h (integral s f)"
  3313   by (subst integral_linear[symmetric, OF assms blinfun.bounded_linear_right]) (simp add: o_def)
  3314 
  3315 lemma blinfun_apply_integral:
  3316   assumes "f integrable_on s"
  3317   shows "blinfun_apply (integral s f) x = integral s (\<lambda>y. blinfun_apply (f y) x)"
  3318   by (metis (no_types, lifting) assms blinfun.prod_left.rep_eq integral_blinfun_apply integral_cong)
  3319 
  3320 lemma has_integral_componentwise_iff:
  3321   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3322   shows "(f has_integral y) A \<longleftrightarrow> (\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
  3323 proof safe
  3324   fix b :: 'b assume "(f has_integral y) A"
  3325   from has_integral_linear[OF this(1) bounded_linear_component, of b]
  3326     show "((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A" by (simp add: o_def)
  3327 next
  3328   assume "(\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
  3329   hence "\<forall>b\<in>Basis. (((\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. f x \<bullet> b)) has_integral ((y \<bullet> b) *\<^sub>R b)) A"
  3330     by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
  3331   hence "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. (y \<bullet> b) *\<^sub>R b)) A"
  3332     by (intro has_integral_setsum) (simp_all add: o_def)
  3333   thus "(f has_integral y) A" by (simp add: euclidean_representation)
  3334 qed
  3335 
  3336 lemma has_integral_componentwise:
  3337   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3338   shows "(\<And>b. b \<in> Basis \<Longrightarrow> ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A) \<Longrightarrow> (f has_integral y) A"
  3339   by (subst has_integral_componentwise_iff) blast
  3340 
  3341 lemma integrable_componentwise_iff:
  3342   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3343   shows "f integrable_on A \<longleftrightarrow> (\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)"
  3344 proof
  3345   assume "f integrable_on A"
  3346   then obtain y where "(f has_integral y) A" by (auto simp: integrable_on_def)
  3347   hence "(\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral (y \<bullet> b)) A)"
  3348     by (subst (asm) has_integral_componentwise_iff)
  3349   thus "(\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)" by (auto simp: integrable_on_def)
  3350 next
  3351   assume "(\<forall>b\<in>Basis. (\<lambda>x. f x \<bullet> b) integrable_on A)"
  3352   then obtain y where "\<forall>b\<in>Basis. ((\<lambda>x. f x \<bullet> b) has_integral y b) A"
  3353     unfolding integrable_on_def by (subst (asm) bchoice_iff) blast
  3354   hence "\<forall>b\<in>Basis. (((\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. f x \<bullet> b)) has_integral (y b *\<^sub>R b)) A"
  3355     by (intro ballI has_integral_linear) (simp_all add: bounded_linear_scaleR_left)
  3356   hence "((\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b) has_integral (\<Sum>b\<in>Basis. y b *\<^sub>R b)) A"
  3357     by (intro has_integral_setsum) (simp_all add: o_def)
  3358   thus "f integrable_on A" by (auto simp: integrable_on_def o_def euclidean_representation)
  3359 qed
  3360 
  3361 lemma integrable_componentwise:
  3362   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3363   shows "(\<And>b. b \<in> Basis \<Longrightarrow> (\<lambda>x. f x \<bullet> b) integrable_on A) \<Longrightarrow> f integrable_on A"
  3364   by (subst integrable_componentwise_iff) blast
  3365 
  3366 lemma integral_componentwise:
  3367   fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  3368   assumes "f integrable_on A"
  3369   shows "integral A f = (\<Sum>b\<in>Basis. integral A (\<lambda>x. (f x \<bullet> b) *\<^sub>R b))"
  3370 proof -
  3371   from assms have integrable: "\<forall>b\<in>Basis. (\<lambda>x. x *\<^sub>R b) \<circ> (\<lambda>x. (f x \<bullet> b)) integrable_on A"
  3372     by (subst (asm) integrable_componentwise_iff, intro integrable_linear ballI)
  3373        (simp_all add: bounded_linear_scaleR_left)
  3374   have "integral A f = integral A (\<lambda>x. \<Sum>b\<in>Basis. (f x \<bullet> b) *\<^sub>R b)"
  3375     by (simp add: euclidean_representation)
  3376   also from integrable have "\<dots> = (\<Sum>a\<in>Basis. integral A (\<lambda>x. (f x \<bullet> a) *\<^sub>R a))"
  3377     by (subst integral_setsum) (simp_all add: o_def)
  3378   finally show ?thesis .
  3379 qed
  3380 
  3381 lemma integrable_component:
  3382   "f integrable_on A \<Longrightarrow> (\<lambda>x. f x \<bullet> (y :: 'b :: euclidean_space)) integrable_on A"
  3383   by (drule integrable_linear[OF _ bounded_linear_component[of y]]) (simp add: o_def)
  3384 
  3385 
  3386 
  3387 subsection \<open>Cauchy-type criterion for integrability.\<close>
  3388 
  3389 (* XXXXXXX *)
  3390 lemma integrable_cauchy:
  3391   fixes f :: "'n::euclidean_space \<Rightarrow> 'a::{real_normed_vector,complete_space}"
  3392   shows "f integrable_on cbox a b \<longleftrightarrow>
  3393     (\<forall>e>0.\<exists>d. gauge d \<and>
  3394       (\<forall>p1 p2. p1 tagged_division_of (cbox a b) \<and> d fine p1 \<and>
  3395         p2 tagged_division_of (cbox a b) \<and> d fine p2 \<longrightarrow>
  3396         norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 -
  3397         setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) < e))"
  3398   (is "?l = (\<forall>e>0. \<exists>d. ?P e d)")
  3399 proof
  3400   assume ?l
  3401   then guess y unfolding integrable_on_def has_integral .. note y=this
  3402   show "\<forall>e>0. \<exists>d. ?P e d"
  3403   proof (clarify, goal_cases)
  3404     case (1 e)
  3405     then have "e/2 > 0" by auto
  3406     then guess d
  3407       apply -
  3408       apply (drule y[rule_format])
  3409       apply (elim exE conjE)
  3410       done
  3411     note d=this[rule_format]
  3412     show ?case
  3413     proof (rule_tac x=d in exI, clarsimp simp: d)
  3414       fix p1 p2
  3415       assume as: "p1 tagged_division_of (cbox a b)" "d fine p1"
  3416                  "p2 tagged_division_of (cbox a b)" "d fine p2"
  3417       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  3418         apply (rule dist_triangle_half_l[where y=y,unfolded dist_norm])
  3419         using d(2)[OF conjI[OF as(1-2)]] d(2)[OF conjI[OF as(3-4)]] .
  3420     qed
  3421   qed
  3422 next
  3423   assume "\<forall>e>0. \<exists>d. ?P e d"
  3424   then have "\<forall>n::nat. \<exists>d. ?P (inverse(of_nat (n + 1))) d"
  3425     by auto
  3426   from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
  3427   have "\<And>n. gauge (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}})"
  3428     apply (rule gauge_inters)
  3429     using d(1)
  3430     apply auto
  3431     done
  3432   then have "\<forall>n. \<exists>p. p tagged_division_of (cbox a b) \<and> (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}}) fine p"
  3433     by (meson fine_division_exists)
  3434   from choice[OF this] guess p .. note p = conjunctD2[OF this[rule_format]]
  3435   have dp: "\<And>i n. i\<le>n \<Longrightarrow> d i fine p n"
  3436     using p(2) unfolding fine_inters by auto
  3437   have "Cauchy (\<lambda>n. setsum (\<lambda>(x,k). content k *\<^sub>R (f x)) (p n))"
  3438   proof (rule CauchyI, goal_cases)
  3439     case (1 e)
  3440     then guess N unfolding real_arch_inverse[of e] .. note N=this
  3441     show ?case
  3442       apply (rule_tac x=N in exI)
  3443     proof clarify
  3444       fix m n
  3445       assume mn: "N \<le> m" "N \<le> n"
  3446       have *: "N = (N - 1) + 1" using N by auto
  3447       show "norm ((\<Sum>(x, k)\<in>p m. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p n. content k *\<^sub>R f x)) < e"
  3448         apply (rule less_trans[OF _ N[THEN conjunct2,THEN conjunct2]])
  3449         apply(subst *)
  3450         using dp p(1) mn d(2) by auto
  3451     qed
  3452   qed
  3453   then guess y unfolding convergent_eq_cauchy[symmetric] .. note y=this[THEN LIMSEQ_D]
  3454   show ?l
  3455     unfolding integrable_on_def has_integral
  3456   proof (rule_tac x=y in exI, clarify)
  3457     fix e :: real
  3458     assume "e>0"
  3459     then have *:"e/2 > 0" by auto
  3460     then guess N1 unfolding real_arch_inverse[of "e/2"] .. note N1=this
  3461     then have N1': "N1 = N1 - 1 + 1"
  3462       by auto
  3463     guess N2 using y[OF *] .. note N2=this
  3464     have "gauge (d (N1 + N2))"
  3465       using d by auto
  3466     moreover
  3467     {
  3468       fix q
  3469       assume as: "q tagged_division_of (cbox a b)" "d (N1 + N2) fine q"
  3470       have *: "inverse (of_nat (N1 + N2 + 1)) < e / 2"
  3471         apply (rule less_trans)
  3472         using N1
  3473         apply auto
  3474         done
  3475       have "norm ((\<Sum>(x, k)\<in>q. content k *\<^sub>R f x) - y) < e"
  3476         apply (rule norm_triangle_half_r)
  3477         apply (rule less_trans[OF _ *])
  3478         apply (subst N1', rule d(2)[of "p (N1+N2)"])
  3479         using N1' as(1) as(2) dp
  3480         apply (simp add: \<open>\<forall>x. p x tagged_division_of cbox a b \<and> (\<lambda>xa. \<Inter>{d i xa |i. i \<in> {0..x}}) fine p x\<close>)
  3481         using N2 le_add2 by blast
  3482     }
  3483     ultimately show "\<exists>d. gauge d \<and>
  3484       (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  3485         norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - y) < e)"
  3486       by (rule_tac x="d (N1 + N2)" in exI) auto
  3487   qed
  3488 qed
  3489 
  3490 
  3491 subsection \<open>Additivity of integral on abutting intervals.\<close>
  3492 
  3493 lemma tagged_division_split_left_inj:
  3494   fixes x1 :: "'a::euclidean_space"
  3495   assumes d: "d tagged_division_of i"
  3496     and k12: "(x1, k1) \<in> d"
  3497              "(x2, k2) \<in> d"
  3498              "k1 \<noteq> k2"
  3499              "k1 \<inter> {x. x\<bullet>k \<le> c} = k2 \<inter> {x. x\<bullet>k \<le> c}"
  3500              "k \<in> Basis"
  3501   shows "content (k1 \<inter> {x. x\<bullet>k \<le> c}) = 0"
  3502 proof -
  3503   have *: "\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c"
  3504     by force
  3505   show ?thesis
  3506     using k12
  3507     by (fastforce intro!:  division_split_left_inj[OF division_of_tagged_division[OF d]] *)
  3508 qed
  3509 
  3510 lemma tagged_division_split_right_inj:
  3511   fixes x1 :: "'a::euclidean_space"
  3512   assumes d: "d tagged_division_of i"
  3513     and k12: "(x1, k1) \<in> d"
  3514              "(x2, k2) \<in> d"
  3515              "k1 \<noteq> k2"
  3516              "k1 \<inter> {x. x\<bullet>k \<ge> c} = k2 \<inter> {x. x\<bullet>k \<ge> c}"
  3517              "k \<in> Basis"
  3518   shows "content (k1 \<inter> {x. x\<bullet>k \<ge> c}) = 0"
  3519 proof -
  3520   have *: "\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c"
  3521     by force
  3522   show ?thesis
  3523     using k12
  3524     by (fastforce intro!:  division_split_right_inj[OF division_of_tagged_division[OF d]] *)
  3525 qed
  3526 
  3527 lemma has_integral_split:
  3528   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  3529   assumes fi: "(f has_integral i) (cbox a b \<inter> {x. x\<bullet>k \<le> c})"
  3530       and fj: "(f has_integral j) (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  3531       and k: "k \<in> Basis"
  3532   shows "(f has_integral (i + j)) (cbox a b)"
  3533 proof (unfold has_integral, rule, rule, goal_cases)
  3534   case (1 e)
  3535   then have e: "e/2 > 0"
  3536     by auto
  3537     obtain d1
  3538     where d1: "gauge d1"
  3539       and d1norm:
  3540         "\<And>p. \<lbrakk>p tagged_division_of cbox a b \<inter> {x. x \<bullet> k \<le> c};
  3541                d1 fine p\<rbrakk> \<Longrightarrow> norm ((\<Sum>(x, k) \<in> p. content k *\<^sub>R f x) - i) < e / 2"
  3542        apply (rule has_integralD[OF fi[unfolded interval_split[OF k]] e])
  3543        apply (simp add: interval_split[symmetric] k)
  3544        done
  3545     obtain d2
  3546     where d2: "gauge d2"
  3547       and d2norm:
  3548         "\<And>p. \<lbrakk>p tagged_division_of cbox a b \<inter> {x. c \<le> x \<bullet> k};
  3549                d2 fine p\<rbrakk> \<Longrightarrow> norm ((\<Sum>(x, k) \<in> p. content k *\<^sub>R f x) - j) < e / 2"
  3550        apply (rule has_integralD[OF fj[unfolded interval_split[OF k]] e])
  3551        apply (simp add: interval_split[symmetric] k)
  3552        done
  3553   let ?d = "\<lambda>x. if x\<bullet>k = c then (d1 x \<inter> d2 x) else ball x \<bar>x\<bullet>k - c\<bar> \<inter> d1 x \<inter> d2 x"
  3554   have "gauge ?d"
  3555     using d1 d2 unfolding gauge_def by auto
  3556   then show ?case
  3557   proof (rule_tac x="?d" in exI, safe)
  3558     fix p
  3559     assume "p tagged_division_of (cbox a b)" "?d fine p"
  3560     note p = this tagged_division_ofD[OF this(1)]
  3561     have xk_le_c: "\<And>x kk. (x, kk) \<in> p \<Longrightarrow> kk \<inter> {x. x\<bullet>k \<le> c} \<noteq> {} \<Longrightarrow> x\<bullet>k \<le> c"
  3562     proof -
  3563       fix x kk
  3564       assume as: "(x, kk) \<in> p" and kk: "kk \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}"
  3565       show "x\<bullet>k \<le> c"
  3566       proof (rule ccontr)
  3567         assume **: "\<not> ?thesis"
  3568         from this[unfolded not_le]
  3569         have "kk \<subseteq> ball x \<bar>x \<bullet> k - c\<bar>"
  3570           using p(2)[unfolded fine_def, rule_format,OF as] by auto
  3571         with kk obtain y where y: "y \<in> ball x \<bar>x \<bullet> k - c\<bar>" "y\<bullet>k \<le> c"
  3572           by blast
  3573         then have "\<bar>x \<bullet> k - y \<bullet> k\<bar> < \<bar>x \<bullet> k - c\<bar>"
  3574           using Basis_le_norm[OF k, of "x - y"]
  3575           by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
  3576         with y show False
  3577           using ** by (auto simp add: field_simps)
  3578       qed
  3579     qed
  3580     have xk_ge_c: "\<And>x kk. (x, kk) \<in> p \<Longrightarrow> kk \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {} \<Longrightarrow> x\<bullet>k \<ge> c"
  3581     proof -
  3582       fix x kk
  3583       assume as: "(x, kk) \<in> p" and kk: "kk \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}"
  3584       show "x\<bullet>k \<ge> c"
  3585       proof (rule ccontr)
  3586         assume **: "\<not> ?thesis"
  3587         from this[unfolded not_le] have "kk \<subseteq> ball x \<bar>x \<bullet> k - c\<bar>"
  3588           using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
  3589         with kk obtain y where y: "y \<in> ball x \<bar>x \<bullet> k - c\<bar>" "y\<bullet>k \<ge> c"
  3590           by blast
  3591         then have "\<bar>x \<bullet> k - y \<bullet> k\<bar> < \<bar>x \<bullet> k - c\<bar>"
  3592           using Basis_le_norm[OF k, of "x - y"]
  3593           by (auto simp add: dist_norm inner_diff_left intro: le_less_trans)
  3594         with y show False
  3595           using ** by (auto simp add: field_simps)
  3596       qed
  3597     qed
  3598 
  3599     have lem1: "\<And>f P Q. (\<forall>x k. (x, k) \<in> {(x, f k) | x k. P x k} \<longrightarrow> Q x k) \<longleftrightarrow>
  3600                          (\<forall>x k. P x k \<longrightarrow> Q x (f k))"
  3601       by auto
  3602     have fin_finite: "finite {(x,f k) | x k. (x,k) \<in> s \<and> P x k}" if "finite s" for f s P
  3603     proof -
  3604       from that have "finite ((\<lambda>(x, k). (x, f k)) ` s)"
  3605         by auto
  3606       then show ?thesis
  3607         by (rule rev_finite_subset) auto
  3608     qed
  3609     { fix g :: "'a set \<Rightarrow> 'a set"
  3610       fix i :: "'a \<times> 'a set"
  3611       assume "i \<in> (\<lambda>(x, k). (x, g k)) ` p - {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
  3612       then obtain x k where xk:
  3613               "i = (x, g k)"  "(x, k) \<in> p"
  3614               "(x, g k) \<notin> {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
  3615           by auto
  3616       have "content (g k) = 0"
  3617         using xk using content_empty by auto
  3618       then have "(\<lambda>(x, k). content k *\<^sub>R f x) i = 0"
  3619         unfolding xk split_conv by auto
  3620     } note [simp] = this
  3621     have lem3: "\<And>g :: 'a set \<Rightarrow> 'a set. finite p \<Longrightarrow>
  3622                   setsum (\<lambda>(x, k). content k *\<^sub>R f x) {(x,g k) |x k. (x,k) \<in> p \<and> g k \<noteq> {}} =
  3623                   setsum (\<lambda>(x, k). content k *\<^sub>R f x) ((\<lambda>(x, k). (x, g k)) ` p)"
  3624       by (rule setsum.mono_neutral_left) auto
  3625     let ?M1 = "{(x, kk \<inter> {x. x\<bullet>k \<le> c}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}}"
  3626     have d1_fine: "d1 fine ?M1"
  3627       by (force intro: fineI dest: fineD[OF p(2)] simp add: split: if_split_asm)
  3628     have "norm ((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) < e/2"
  3629     proof (rule d1norm [OF tagged_division_ofI d1_fine])
  3630       show "finite ?M1"
  3631         by (rule fin_finite p(3))+
  3632       show "\<Union>{k. \<exists>x. (x, k) \<in> ?M1} = cbox a b \<inter> {x. x\<bullet>k \<le> c}"
  3633         unfolding p(8)[symmetric] by auto
  3634       fix x l
  3635       assume xl: "(x, l) \<in> ?M1"
  3636       then guess x' l' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note xl'=this
  3637       show "x \<in> l" "l \<subseteq> cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  3638         unfolding xl'
  3639         using p(4-6)[OF xl'(3)] using xl'(4)
  3640         using xk_le_c[OF xl'(3-4)] by auto
  3641       show "\<exists>a b. l = cbox a b"
  3642         unfolding xl'
  3643         using p(6)[OF xl'(3)]
  3644         by (fastforce simp add: interval_split[OF k,where c=c])
  3645       fix y r
  3646       let ?goal = "interior l \<inter> interior r = {}"
  3647       assume yr: "(y, r) \<in> ?M1"
  3648       then guess y' r' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note yr'=this
  3649       assume as: "(x, l) \<noteq> (y, r)"
  3650       show "interior l \<inter> interior r = {}"
  3651       proof (cases "l' = r' \<longrightarrow> x' = y'")
  3652         case False
  3653         then show ?thesis
  3654           using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  3655       next
  3656         case True
  3657         then have "l' \<noteq> r'"
  3658           using as unfolding xl' yr' by auto
  3659         then show ?thesis
  3660           using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  3661       qed
  3662     qed
  3663     moreover
  3664     let ?M2 = "{(x,kk \<inter> {x. x\<bullet>k \<ge> c}) |x kk. (x,kk) \<in> p \<and> kk \<inter> {x. x\<bullet>k \<ge> c} \<noteq> {}}"
  3665     have d2_fine: "d2 fine ?M2"
  3666       by (force intro: fineI dest: fineD[OF p(2)] simp add: split: if_split_asm)
  3667     have "norm ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) < e/2"
  3668     proof (rule d2norm [OF tagged_division_ofI d2_fine])
  3669       show "finite ?M2"
  3670         by (rule fin_finite p(3))+
  3671       show "\<Union>{k. \<exists>x. (x, k) \<in> ?M2} = cbox a b \<inter> {x. x\<bullet>k \<ge> c}"
  3672         unfolding p(8)[symmetric] by auto
  3673       fix x l
  3674       assume xl: "(x, l) \<in> ?M2"
  3675       then guess x' l' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note xl'=this
  3676       show "x \<in> l" "l \<subseteq> cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  3677         unfolding xl'
  3678         using p(4-6)[OF xl'(3)] xl'(4) xk_ge_c[OF xl'(3-4)]
  3679         by auto
  3680       show "\<exists>a b. l = cbox a b"
  3681         unfolding xl'
  3682         using p(6)[OF xl'(3)]
  3683         by (fastforce simp add: interval_split[OF k, where c=c])
  3684       fix y r
  3685       let ?goal = "interior l \<inter> interior r = {}"
  3686       assume yr: "(y, r) \<in> ?M2"
  3687       then guess y' r' unfolding mem_Collect_eq unfolding prod.inject by (elim exE conjE) note yr'=this
  3688       assume as: "(x, l) \<noteq> (y, r)"
  3689       show "interior l \<inter> interior r = {}"
  3690       proof (cases "l' = r' \<longrightarrow> x' = y'")
  3691         case False
  3692         then show ?thesis
  3693           using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  3694       next
  3695         case True
  3696         then have "l' \<noteq> r'"
  3697           using as unfolding xl' yr' by auto
  3698         then show ?thesis
  3699           using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  3700       qed
  3701     qed
  3702     ultimately
  3703     have "norm (((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j)) < e/2 + e/2"
  3704       using norm_add_less by blast
  3705     also {
  3706       have eq0: "\<And>x y. x = (0::real) \<Longrightarrow> x *\<^sub>R (y::'b) = 0"
  3707         using scaleR_zero_left by auto
  3708       have cont_eq: "\<And>g. (\<lambda>(x,l). content l *\<^sub>R f x) \<circ> (\<lambda>(x,l). (x,g l)) = (\<lambda>(x,l). content (g l) *\<^sub>R f x)"
  3709         by auto
  3710       have "((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) =
  3711         (\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - (i + j)"
  3712         by auto
  3713       also have "\<dots> = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f x) +
  3714         (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f x) - (i + j)"
  3715         unfolding lem3[OF p(3)]
  3716         by (subst setsum.reindex_nontrivial[OF p(3)], auto intro!: k eq0 tagged_division_split_left_inj[OF p(1)] tagged_division_split_right_inj[OF p(1)]
  3717               simp: cont_eq)+
  3718       also note setsum.distrib[symmetric]
  3719       also have "\<And>x. x \<in> p \<Longrightarrow>
  3720                     (\<lambda>(x,ka). content (ka \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f x) x +
  3721                     (\<lambda>(x,ka). content (ka \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f x) x =
  3722                     (\<lambda>(x,ka). content ka *\<^sub>R f x) x"
  3723       proof clarify
  3724         fix a b
  3725         assume "(a, b) \<in> p"
  3726         from p(6)[OF this] guess u v by (elim exE) note uv=this
  3727         then show "content (b \<inter> {x. x \<bullet> k \<le> c}) *\<^sub>R f a + content (b \<inter> {x. c \<le> x \<bullet> k}) *\<^sub>R f a =
  3728           content b *\<^sub>R f a"
  3729           unfolding scaleR_left_distrib[symmetric]
  3730           unfolding uv content_split[OF k,of u v c]
  3731           by auto
  3732       qed
  3733       note setsum.cong [OF _ this]
  3734       finally have "(\<Sum>(x, k)\<in>{(x, kk \<inter> {x. x \<bullet> k \<le> c}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. x \<bullet> k \<le> c} \<noteq> {}}. content k *\<^sub>R f x) - i +
  3735         ((\<Sum>(x, k)\<in>{(x, kk \<inter> {x. c \<le> x \<bullet> k}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. c \<le> x \<bullet> k} \<noteq> {}}. content k *\<^sub>R f x) - j) =
  3736         (\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f x) - (i + j)"
  3737         by auto
  3738     }
  3739     finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (i + j)) < e"
  3740       by auto
  3741   qed
  3742 qed
  3743 
  3744 
  3745 subsection \<open>A sort of converse, integrability on subintervals.\<close>
  3746 
  3747 lemma tagged_division_union_interval:
  3748   fixes a :: "'a::euclidean_space"
  3749   assumes "p1 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<le> (c::real)})"
  3750     and "p2 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  3751     and k: "k \<in> Basis"
  3752   shows "(p1 \<union> p2) tagged_division_of (cbox a b)"
  3753 proof -
  3754   have *: "cbox a b = (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<union> (cbox a b \<inter> {x. x\<bullet>k \<ge> c})"
  3755     by auto
  3756   show ?thesis
  3757     apply (subst *)
  3758     apply (rule tagged_division_union[OF assms(1-2)])
  3759     unfolding interval_split[OF k] interior_cbox
  3760     using k
  3761     apply (auto simp add: box_def elim!: ballE[where x=k])
  3762     done
  3763 qed
  3764 
  3765 lemma tagged_division_union_interval_real:
  3766   fixes a :: real
  3767   assumes "p1 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<le> (c::real)})"
  3768     and "p2 tagged_division_of ({a .. b} \<inter> {x. x\<bullet>k \<ge> c})"
  3769     and k: "k \<in> Basis"
  3770   shows "(p1 \<union> p2) tagged_division_of {a .. b}"
  3771   using assms
  3772   unfolding box_real[symmetric]
  3773   by (rule tagged_division_union_interval)
  3774 
  3775 lemma has_integral_separate_sides:
  3776   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  3777   assumes "(f has_integral i) (cbox a b)"
  3778     and "e > 0"
  3779     and k: "k \<in> Basis"
  3780   obtains d where "gauge d"
  3781     "\<forall>p1 p2. p1 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<le> c}) \<and> d fine p1 \<and>
  3782         p2 tagged_division_of (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) \<and> d fine p2 \<longrightarrow>
  3783         norm ((setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 + setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) - i) < e"
  3784 proof -
  3785   guess d using has_integralD[OF assms(1-2)] . note d=this
  3786   { fix p1 p2
  3787     assume "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p1"
  3788     note p1=tagged_division_ofD[OF this(1)] this
  3789     assume "p2 tagged_division_of (cbox a b) \<inter> {x. c \<le> x \<bullet> k}" "d fine p2"
  3790     note p2=tagged_division_ofD[OF this(1)] this
  3791     note tagged_division_union_interval[OF p1(7) p2(7)] note p12 = tagged_division_ofD[OF this] this
  3792     { fix a b
  3793       assume ab: "(a, b) \<in> p1 \<inter> p2"
  3794       have "(a, b) \<in> p1"
  3795         using ab by auto
  3796       with p1 obtain u v where uv: "b = cbox u v" by auto
  3797       have "b \<subseteq> {x. x\<bullet>k = c}"
  3798         using ab p1(3)[of a b] p2(3)[of a b] by fastforce
  3799       moreover
  3800       have "interior {x::'a. x \<bullet> k = c} = {}"
  3801       proof (rule ccontr)
  3802         assume "\<not> ?thesis"
  3803         then obtain x where x: "x \<in> interior {x::'a. x\<bullet>k = c}"
  3804           by auto
  3805         then guess e unfolding mem_interior .. note e=this
  3806         have x: "x\<bullet>k = c"
  3807           using x interior_subset by fastforce
  3808         have *: "\<And>i. i \<in> Basis \<Longrightarrow> \<bar>(x - (x + (e / 2) *\<^sub>R k)) \<bullet> i\<bar> = (if i = k then e/2 else 0)"
  3809           using e k by (auto simp: inner_simps inner_not_same_Basis)
  3810         have "(\<Sum>i\<in>Basis. \<bar>(x - (x + (e / 2 ) *\<^sub>R k)) \<bullet> i\<bar>) =
  3811               (\<Sum>i\<in>Basis. (if i = k then e / 2 else 0))"
  3812           using "*" by (blast intro: setsum.cong)
  3813         also have "\<dots> < e"
  3814           apply (subst setsum.delta)
  3815           using e
  3816           apply auto
  3817           done
  3818         finally have "x + (e/2) *\<^sub>R k \<in> ball x e"
  3819           unfolding mem_ball dist_norm by(rule le_less_trans[OF norm_le_l1])
  3820         then have "x + (e/2) *\<^sub>R k \<in> {x. x\<bullet>k = c}"
  3821           using e by auto
  3822         then show False
  3823           unfolding mem_Collect_eq using e x k by (auto simp: inner_simps)
  3824       qed
  3825       ultimately have "content b = 0"
  3826         unfolding uv content_eq_0_interior
  3827         using interior_mono by blast
  3828       then have "content b *\<^sub>R f a = 0"
  3829         by auto
  3830     }
  3831     then have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) =
  3832                norm ((\<Sum>(x, k)\<in>p1 \<union> p2. content k *\<^sub>R f x) - i)"
  3833       by (subst setsum.union_inter_neutral) (auto simp: p1 p2)
  3834     also have "\<dots> < e"
  3835       by (rule k d(2) p12 fine_union p1 p2)+
  3836     finally have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) < e" .
  3837    }
  3838   then show ?thesis
  3839     by (auto intro: that[of d] d elim: )
  3840 qed
  3841 
  3842 lemma integrable_split[intro]:
  3843   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::{real_normed_vector,complete_space}"
  3844   assumes "f integrable_on cbox a b"
  3845     and k: "k \<in> Basis"
  3846   shows "f integrable_on (cbox a b \<inter> {x. x\<bullet>k \<le> c})" (is ?t1)
  3847     and "f integrable_on (cbox a b \<inter> {x. x\<bullet>k \<ge> c})" (is ?t2)
  3848 proof -
  3849   guess y using assms(1) unfolding integrable_on_def .. note y=this
  3850   define b' where "b' = (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) c else b\<bullet>i)*\<^sub>R i)"
  3851   define a' where "a' = (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) c else a\<bullet>i)*\<^sub>R i)"
  3852   show ?t1 ?t2
  3853     unfolding interval_split[OF k] integrable_cauchy
  3854     unfolding interval_split[symmetric,OF k]
  3855   proof (rule_tac[!] allI impI)+
  3856     fix e :: real
  3857     assume "e > 0"
  3858     then have "e/2>0"
  3859       by auto
  3860     from has_integral_separate_sides[OF y this k,of c] guess d . note d=this[rule_format]
  3861     let ?P = "\<lambda>A. \<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of (cbox a b) \<inter> A \<and> d fine p1 \<and>
  3862       p2 tagged_division_of (cbox a b) \<inter> A \<and> d fine p2 \<longrightarrow>
  3863       norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e)"
  3864     show "?P {x. x \<bullet> k \<le> c}"
  3865     proof (rule_tac x=d in exI, clarsimp simp add: d)
  3866       fix p1 p2
  3867       assume as: "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p1"
  3868                  "p2 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<le> c}" "d fine p2"
  3869       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  3870       proof (rule fine_division_exists[OF d(1), of a' b] )
  3871         fix p
  3872         assume "p tagged_division_of cbox a' b" "d fine p"
  3873         then show ?thesis
  3874           using as norm_triangle_half_l[OF d(2)[of p1 p] d(2)[of p2 p]]
  3875           unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  3876           by (auto simp add: algebra_simps)
  3877       qed
  3878     qed
  3879     show "?P {x. x \<bullet> k \<ge> c}"
  3880     proof (rule_tac x=d in exI, clarsimp simp add: d)
  3881       fix p1 p2
  3882       assume as: "p1 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<ge> c}" "d fine p1"
  3883                  "p2 tagged_division_of (cbox a b) \<inter> {x. x \<bullet> k \<ge> c}" "d fine p2"
  3884       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  3885       proof (rule fine_division_exists[OF d(1), of a b'] )
  3886         fix p
  3887         assume "p tagged_division_of cbox a b'" "d fine p"
  3888         then show ?thesis
  3889           using as norm_triangle_half_l[OF d(2)[of p p1] d(2)[of p p2]]
  3890           unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  3891           by (auto simp add: algebra_simps)
  3892       qed
  3893     qed
  3894   qed
  3895 qed
  3896 
  3897 lemma operative_integral:
  3898   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  3899   shows "comm_monoid.operative (lift_option op +) (Some 0)
  3900     (\<lambda>i. if f integrable_on i then Some (integral i f) else None)"
  3901 proof -
  3902   interpret comm_monoid "lift_option plus" "Some (0::'b)"
  3903     by (rule comm_monoid_lift_option)
  3904       (rule add.comm_monoid_axioms)
  3905   show ?thesis
  3906   proof (unfold operative_def, safe)
  3907     fix a b c
  3908     fix k :: 'a
  3909     assume k: "k \<in> Basis"
  3910     show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) =
  3911           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)
  3912           (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)"
  3913     proof (cases "f integrable_on cbox a b")
  3914       case True
  3915       with k show ?thesis
  3916         apply (simp add: integrable_split)
  3917         apply (rule integral_unique [OF has_integral_split[OF _ _ k]])
  3918         apply (auto intro: integrable_integral)
  3919         done
  3920     next
  3921     case False
  3922       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})"
  3923       proof (rule ccontr)
  3924         assume "\<not> ?thesis"
  3925         then have "f integrable_on cbox a b"
  3926           unfolding integrable_on_def
  3927           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)
  3928           apply (rule has_integral_split[OF _ _ k])
  3929           apply (auto intro: integrable_integral)
  3930           done
  3931         then show False
  3932           using False by auto
  3933       qed
  3934       then show ?thesis
  3935         using False by auto
  3936     qed
  3937   next
  3938     fix a b :: 'a
  3939     assume "content (cbox a b) = 0"
  3940     then show "(if f integrable_on cbox a b then Some (integral (cbox a b) f) else None) = Some 0"
  3941       using has_integral_null_eq
  3942       by (auto simp: integrable_on_null)
  3943   qed
  3944 qed
  3945 
  3946 subsection \<open>Finally, the integral of a constant\<close>
  3947 
  3948 lemma has_integral_const [intro]:
  3949   fixes a b :: "'a::euclidean_space"
  3950   shows "((\<lambda>x. c) has_integral (content (cbox a b) *\<^sub>R c)) (cbox a b)"
  3951   apply (auto intro!: exI [where x="\<lambda>x. ball x 1"] simp: split_def has_integral)
  3952   apply (subst scaleR_left.setsum[symmetric, unfolded o_def])
  3953   apply (subst additive_content_tagged_division[unfolded split_def])
  3954   apply auto
  3955   done
  3956 
  3957 lemma has_integral_const_real [intro]:
  3958   fixes a b :: real
  3959   shows "((\<lambda>x. c) has_integral (content {a .. b} *\<^sub>R c)) {a .. b}"
  3960   by (metis box_real(2) has_integral_const)
  3961 
  3962 lemma integral_const [simp]:
  3963   fixes a b :: "'a::euclidean_space"
  3964   shows "integral (cbox a b) (\<lambda>x. c) = content (cbox a b) *\<^sub>R c"
  3965   by (rule integral_unique) (rule has_integral_const)
  3966 
  3967 lemma integral_const_real [simp]:
  3968   fixes a b :: real
  3969   shows "integral {a .. b} (\<lambda>x. c) = content {a .. b} *\<^sub>R c"
  3970   by (metis box_real(2) integral_const)
  3971 
  3972 
  3973 subsection \<open>Bounds on the norm of Riemann sums and the integral itself.\<close>
  3974 
  3975 lemma dsum_bound:
  3976   assumes "p division_of (cbox a b)"
  3977     and "norm c \<le> e"
  3978   shows "norm (setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> e * content(cbox a b)"
  3979 proof -
  3980   have sumeq: "(\<Sum>i\<in>p. \<bar>content i\<bar>) = setsum content p"
  3981     apply (rule setsum.cong)
  3982     using assms
  3983     apply simp
  3984     apply (metis abs_of_nonneg assms(1) content_pos_le division_ofD(4))
  3985     done
  3986   have e: "0 \<le> e"
  3987     using assms(2) norm_ge_zero order_trans by blast
  3988   have "norm (setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> (\<Sum>i\<in>p. norm (content i *\<^sub>R c))"
  3989     using norm_setsum by blast
  3990   also have "...  \<le> e * (\<Sum>i\<in>p. \<bar>content i\<bar>)"
  3991     apply (simp add: setsum_right_distrib[symmetric] mult.commute)
  3992     using assms(2) mult_right_mono by blast
  3993   also have "... \<le> e * content (cbox a b)"
  3994     apply (rule mult_left_mono [OF _ e])
  3995     apply (simp add: sumeq)
  3996     using additive_content_division assms(1) eq_iff apply blast
  3997     done
  3998   finally show ?thesis .
  3999 qed
  4000 
  4001 lemma rsum_bound:
  4002   assumes p: "p tagged_division_of (cbox a b)"
  4003       and "\<forall>x\<in>cbox a b. norm (f x) \<le> e"
  4004     shows "norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> e * content (cbox a b)"
  4005 proof (cases "cbox a b = {}")
  4006   case True show ?thesis
  4007     using p unfolding True tagged_division_of_trivial by auto
  4008 next
  4009   case False
  4010   then have e: "e \<ge> 0"
  4011     by (meson ex_in_conv assms(2) norm_ge_zero order_trans)
  4012   have setsum_le: "setsum (content \<circ> snd) p \<le> content (cbox a b)"
  4013     unfolding additive_content_tagged_division[OF p, symmetric] split_def
  4014     by (auto intro: eq_refl)
  4015   have con: "\<And>xk. xk \<in> p \<Longrightarrow> 0 \<le> content (snd xk)"
  4016     using tagged_division_ofD(4) [OF p] content_pos_le
  4017     by force
  4018   have norm: "\<And>xk. xk \<in> p \<Longrightarrow> norm (f (fst xk)) \<le> e"
  4019     unfolding fst_conv using tagged_division_ofD(2,3)[OF p] assms
  4020     by (metis prod.collapse subset_eq)
  4021   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))"
  4022     by (rule norm_setsum)
  4023   also have "...  \<le> e * content (cbox a b)"
  4024     unfolding split_def norm_scaleR
  4025     apply (rule order_trans[OF setsum_mono])
  4026     apply (rule mult_left_mono[OF _ abs_ge_zero, of _ e])
  4027     apply (metis norm)
  4028     unfolding setsum_left_distrib[symmetric]
  4029     using con setsum_le
  4030     apply (auto simp: mult.commute intro: mult_left_mono [OF _ e])
  4031     done
  4032   finally show ?thesis .
  4033 qed
  4034 
  4035 lemma rsum_diff_bound:
  4036   assumes "p tagged_division_of (cbox a b)"
  4037     and "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e"
  4038   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>
  4039          e * content (cbox a b)"
  4040   apply (rule order_trans[OF _ rsum_bound[OF assms]])
  4041   apply (simp add: split_def scaleR_diff_right setsum_subtractf eq_refl)
  4042   done
  4043 
  4044 lemma has_integral_bound:
  4045   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  4046   assumes "0 \<le> B"
  4047       and "(f has_integral i) (cbox a b)"
  4048       and "\<forall>x\<in>cbox a b. norm (f x) \<le> B"
  4049     shows "norm i \<le> B * content (cbox a b)"
  4050 proof (rule ccontr)
  4051   assume "\<not> ?thesis"
  4052   then have *: "norm i - B * content (cbox a b) > 0"
  4053     by auto
  4054   from assms(2)[unfolded has_integral,rule_format,OF *]
  4055   guess d by (elim exE conjE) note d=this[rule_format]
  4056   from fine_division_exists[OF this(1), of a b] guess p . note p=this
  4057   have *: "\<And>s B. norm s \<le> B \<Longrightarrow> \<not> norm (s - i) < norm i - B"
  4058     unfolding not_less
  4059     by (metis norm_triangle_sub[of i] add.commute le_less_trans less_diff_eq linorder_not_le norm_minus_commute)
  4060   show False
  4061     using d(2)[OF conjI[OF p]] *[OF rsum_bound[OF p(1) assms(3)]] by auto
  4062 qed
  4063 
  4064 corollary has_integral_bound_real:
  4065   fixes f :: "real \<Rightarrow> 'b::real_normed_vector"
  4066   assumes "0 \<le> B"
  4067       and "(f has_integral i) {a .. b}"
  4068       and "\<forall>x\<in>{a .. b}. norm (f x) \<le> B"
  4069     shows "norm i \<le> B * content {a .. b}"
  4070   by (metis assms box_real(2) has_integral_bound)
  4071 
  4072 corollary integrable_bound:
  4073   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
  4074   assumes "0 \<le> B"
  4075       and "f integrable_on (cbox a b)"
  4076       and "\<And>x. x\<in>cbox a b \<Longrightarrow> norm (f x) \<le> B"
  4077     shows "norm (integral (cbox a b) f) \<le> B * content (cbox a b)"
  4078 by (metis integrable_integral has_integral_bound assms)
  4079 
  4080 
  4081 subsection \<open>Similar theorems about relationship among components.\<close>
  4082 
  4083 lemma rsum_component_le:
  4084   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4085   assumes "p tagged_division_of (cbox a b)"
  4086       and "\<forall>x\<in>cbox a b. (f x)\<bullet>i \<le> (g x)\<bullet>i"
  4087     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"
  4088 unfolding inner_setsum_left
  4089 proof (rule setsum_mono, clarify)
  4090   fix a b
  4091   assume ab: "(a, b) \<in> p"
  4092   note tagged = tagged_division_ofD(2-4)[OF assms(1) ab]
  4093   from this(3) guess u v by (elim exE) note b=this
  4094   show "(content b *\<^sub>R f a) \<bullet> i \<le> (content b *\<^sub>R g a) \<bullet> i"
  4095     unfolding b inner_simps real_scaleR_def
  4096     apply (rule mult_left_mono)
  4097     using assms(2) tagged
  4098     by (auto simp add: content_pos_le)
  4099 qed
  4100 
  4101 lemma has_integral_component_le:
  4102   fixes f g :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4103   assumes k: "k \<in> Basis"
  4104   assumes "(f has_integral i) s" "(g has_integral j) s"
  4105     and "\<forall>x\<in>s. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  4106   shows "i\<bullet>k \<le> j\<bullet>k"
  4107 proof -
  4108   have lem: "i\<bullet>k \<le> j\<bullet>k"
  4109     if f_i: "(f has_integral i) (cbox a b)"
  4110     and g_j: "(g has_integral j) (cbox a b)"
  4111     and le: "\<forall>x\<in>cbox a b. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  4112     for a b i and j :: 'b and f g :: "'a \<Rightarrow> 'b"
  4113   proof (rule ccontr)
  4114     assume "\<not> ?thesis"
  4115     then have *: "0 < (i\<bullet>k - j\<bullet>k) / 3"
  4116       by auto
  4117     guess d1 using f_i[unfolded has_integral,rule_format,OF *] by (elim exE conjE) note d1=this[rule_format]
  4118     guess d2 using g_j[unfolded has_integral,rule_format,OF *] by (elim exE conjE) note d2=this[rule_format]
  4119     obtain p where p: "p tagged_division_of cbox a b" "d1 fine p" "d2 fine p"
  4120        using fine_division_exists[OF gauge_inter[OF d1(1) d2(1)], of a b] unfolding fine_inter
  4121        by metis
  4122     note le_less_trans[OF Basis_le_norm[OF k]]
  4123     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"
  4124               "\<bar>((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - j) \<bullet> k\<bar> < (i \<bullet> k - j \<bullet> k) / 3"
  4125       using  k norm_bound_Basis_lt d1 d2 p
  4126       by blast+
  4127     then show False
  4128       unfolding inner_simps
  4129       using rsum_component_le[OF p(1) le]
  4130       by (simp add: abs_real_def split: if_split_asm)
  4131   qed
  4132   show ?thesis
  4133   proof (cases "\<exists>a b. s = cbox a b")
  4134     case True
  4135     with lem assms show ?thesis
  4136       by auto
  4137   next
  4138     case False
  4139     show ?thesis
  4140     proof (rule ccontr)
  4141       assume "\<not> i\<bullet>k \<le> j\<bullet>k"
  4142       then have ij: "(i\<bullet>k - j\<bullet>k) / 3 > 0"
  4143         by auto
  4144       note has_integral_altD[OF _ False this]
  4145       from this[OF assms(2)] this[OF assms(3)] guess B1 B2 . note B=this[rule_format]
  4146       have "bounded (ball 0 B1 \<union> ball (0::'a) B2)"
  4147         unfolding bounded_Un by(rule conjI bounded_ball)+
  4148       from bounded_subset_cbox[OF this] guess a b by (elim exE)
  4149       note ab = conjunctD2[OF this[unfolded Un_subset_iff]]
  4150       guess w1 using B(2)[OF ab(1)] .. note w1=conjunctD2[OF this]
  4151       guess w2 using B(4)[OF ab(2)] .. note w2=conjunctD2[OF this]
  4152       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"
  4153         by (simp add: abs_real_def split: if_split_asm)
  4154       note le_less_trans[OF Basis_le_norm[OF k]]
  4155       note this[OF w1(2)] this[OF w2(2)]
  4156       moreover
  4157       have "w1\<bullet>k \<le> w2\<bullet>k"
  4158         by (rule lem[OF w1(1) w2(1)]) (simp add: assms(4))
  4159       ultimately show False
  4160         unfolding inner_simps by(rule *)
  4161     qed
  4162   qed
  4163 qed
  4164 
  4165 lemma integral_component_le:
  4166   fixes g f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4167   assumes "k \<in> Basis"
  4168     and "f integrable_on s" "g integrable_on s"
  4169     and "\<forall>x\<in>s. (f x)\<bullet>k \<le> (g x)\<bullet>k"
  4170   shows "(integral s f)\<bullet>k \<le> (integral s g)\<bullet>k"
  4171   apply (rule has_integral_component_le)
  4172   using integrable_integral assms
  4173   apply auto
  4174   done
  4175 
  4176 lemma has_integral_component_nonneg:
  4177   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4178   assumes "k \<in> Basis"
  4179     and "(f has_integral i) s"
  4180     and "\<forall>x\<in>s. 0 \<le> (f x)\<bullet>k"
  4181   shows "0 \<le> i\<bullet>k"
  4182   using has_integral_component_le[OF assms(1) has_integral_0 assms(2)]
  4183   using assms(3-)
  4184   by auto
  4185 
  4186 lemma integral_component_nonneg:
  4187   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4188   assumes "k \<in> Basis"
  4189     and  "\<forall>x\<in>s. 0 \<le> (f x)\<bullet>k"
  4190   shows "0 \<le> (integral s f)\<bullet>k"
  4191 proof (cases "f integrable_on s")
  4192   case True show ?thesis
  4193     apply (rule has_integral_component_nonneg)
  4194     using assms True
  4195     apply auto
  4196     done
  4197 next
  4198   case False then show ?thesis by (simp add: not_integrable_integral)
  4199 qed
  4200 
  4201 lemma has_integral_component_neg:
  4202   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4203   assumes "k \<in> Basis"
  4204     and "(f has_integral i) s"
  4205     and "\<forall>x\<in>s. (f x)\<bullet>k \<le> 0"
  4206   shows "i\<bullet>k \<le> 0"
  4207   using has_integral_component_le[OF assms(1,2) has_integral_0] assms(2-)
  4208   by auto
  4209 
  4210 lemma has_integral_component_lbound:
  4211   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4212   assumes "(f has_integral i) (cbox a b)"
  4213     and "\<forall>x\<in>cbox a b. B \<le> f(x)\<bullet>k"
  4214     and "k \<in> Basis"
  4215   shows "B * content (cbox a b) \<le> i\<bullet>k"
  4216   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-)
  4217   by (auto simp add: field_simps)
  4218 
  4219 lemma has_integral_component_ubound:
  4220   fixes f::"'a::euclidean_space => 'b::euclidean_space"
  4221   assumes "(f has_integral i) (cbox a b)"
  4222     and "\<forall>x\<in>cbox a b. f x\<bullet>k \<le> B"
  4223     and "k \<in> Basis"
  4224   shows "i\<bullet>k \<le> B * content (cbox a b)"
  4225   using has_integral_component_le[OF assms(3,1) has_integral_const, of "\<Sum>i\<in>Basis. B *\<^sub>R i"] assms(2-)
  4226   by (auto simp add: field_simps)
  4227 
  4228 lemma integral_component_lbound:
  4229   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4230   assumes "f integrable_on cbox a b"
  4231     and "\<forall>x\<in>cbox a b. B \<le> f(x)\<bullet>k"
  4232     and "k \<in> Basis"
  4233   shows "B * content (cbox a b) \<le> (integral(cbox a b) f)\<bullet>k"
  4234   apply (rule has_integral_component_lbound)
  4235   using assms
  4236   unfolding has_integral_integral
  4237   apply auto
  4238   done
  4239 
  4240 lemma integral_component_lbound_real:
  4241   assumes "f integrable_on {a ::real .. b}"
  4242     and "\<forall>x\<in>{a .. b}. B \<le> f(x)\<bullet>k"
  4243     and "k \<in> Basis"
  4244   shows "B * content {a .. b} \<le> (integral {a .. b} f)\<bullet>k"
  4245   using assms
  4246   by (metis box_real(2) integral_component_lbound)
  4247 
  4248 lemma integral_component_ubound:
  4249   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  4250   assumes "f integrable_on cbox a b"
  4251     and "\<forall>x\<in>cbox a b. f x\<bullet>k \<le> B"
  4252     and "k \<in> Basis"
  4253   shows "(integral (cbox a b) f)\<bullet>k \<le> B * content (cbox a b)"
  4254   apply (rule has_integral_component_ubound)
  4255   using assms
  4256   unfolding has_integral_integral
  4257   apply auto
  4258   done
  4259 
  4260 lemma integral_component_ubound_real:
  4261   fixes f :: "real \<Rightarrow> 'a::euclidean_space"
  4262   assumes "f integrable_on {a .. b}"
  4263     and "\<forall>x\<in>{a .. b}. f x\<bullet>k \<le> B"
  4264     and "k \<in> Basis"
  4265   shows "(integral {a .. b} f)\<bullet>k \<le> B * content {a .. b}"
  4266   using assms
  4267   by (metis box_real(2) integral_component_ubound)
  4268 
  4269 subsection \<open>Uniform limit of integrable functions is integrable.\<close>
  4270 
  4271 lemma real_arch_invD:
  4272   "0 < (e::real) \<Longrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
  4273   by (subst(asm) real_arch_inverse)
  4274 
  4275 lemma integrable_uniform_limit:
  4276   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
  4277   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"
  4278   shows "f integrable_on cbox a b"
  4279 proof (cases "content (cbox a b) > 0")
  4280   case False then show ?thesis
  4281       using has_integral_null
  4282       by (simp add: content_lt_nz integrable_on_def)
  4283 next
  4284   case True
  4285   have *: "\<And>P. \<forall>e>(0::real). P e \<Longrightarrow> \<forall>n::nat. P (inverse (real n + 1))"
  4286     by auto
  4287   from choice[OF *[OF assms]] guess g .. note g=conjunctD2[OF this[rule_format],rule_format]
  4288   from choice[OF allI[OF g(2)[unfolded integrable_on_def], of "\<lambda>x. x"]]
  4289   obtain i where i: "\<And>x. (g x has_integral i x) (cbox a b)"
  4290       by auto
  4291   have "Cauchy i"
  4292     unfolding Cauchy_def
  4293   proof clarify
  4294     fix e :: real
  4295     assume "e>0"
  4296     then have "e / 4 / content (cbox a b) > 0"
  4297       using True by (auto simp add: field_simps)
  4298     then obtain M :: nat
  4299          where M: "M \<noteq> 0" "0 < inverse (real_of_nat M)" "inverse (of_nat M) < e / 4 / content (cbox a b)"
  4300       by (subst (asm) real_arch_inverse) auto
  4301     show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (i m) (i n) < e"
  4302     proof (rule exI [where x=M], clarify)
  4303       fix m n
  4304       assume m: "M \<le> m" and n: "M \<le> n"
  4305       have "e/4>0" using \<open>e>0\<close> by auto
  4306       note * = i[unfolded has_integral,rule_format,OF this]
  4307       from *[of m] guess gm by (elim conjE exE) note gm=this[rule_format]
  4308       from *[of n] guess gn by (elim conjE exE) note gn=this[rule_format]
  4309       from fine_division_exists[OF gauge_inter[OF gm(1) gn(1)], of a b]
  4310       obtain p where p: "p tagged_division_of cbox a b" "(\<lambda>x. gm x \<inter> gn x) fine p"
  4311         by auto
  4312       { fix s1 s2 i1 and i2::'b
  4313         assume no: "norm(s2 - s1) \<le> e/2" "norm (s1 - i1) < e/4" "norm (s2 - i2) < e/4"
  4314         have "norm (i1 - i2) \<le> norm (i1 - s1) + norm (s1 - s2) + norm (s2 - i2)"
  4315           using norm_triangle_ineq[of "i1 - s1" "s1 - i2"]
  4316           using norm_triangle_ineq[of "s1 - s2" "s2 - i2"]
  4317           by (auto simp add: algebra_simps)
  4318         also have "\<dots> < e"
  4319           using no
  4320           unfolding norm_minus_commute
  4321           by (auto simp add: algebra_simps)
  4322         finally have "norm (i1 - i2) < e" .
  4323       } note triangle3 = this
  4324       have finep: "gm fine p" "gn fine p"
  4325         using fine_inter p  by auto
  4326       { fix x
  4327         assume x: "x \<in> cbox a b"
  4328         have "norm (f x - g n x) + norm (f x - g m x) \<le> inverse (real n + 1) + inverse (real m + 1)"
  4329           using g(1)[OF x, of n] g(1)[OF x, of m] by auto
  4330         also have "\<dots> \<le> inverse (real M) + inverse (real M)"
  4331           apply (rule add_mono)
  4332           using M(2) m n by auto
  4333         also have "\<dots> = 2 / real M"
  4334           unfolding divide_inverse by auto
  4335         finally have "norm (g n x - g m x) \<le> 2 / real M"
  4336           using norm_triangle_le[of "g n x - f x" "f x - g m x" "2 / real M"]
  4337           by (auto simp add: algebra_simps simp add: norm_minus_commute)
  4338       } note norm_le = this
  4339       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"
  4340         apply (rule order_trans [OF rsum_diff_bound[OF p(1), where e="2 / real M"]])
  4341         apply (blast intro: norm_le)
  4342         using M True
  4343         by (auto simp add: field_simps)
  4344       then show "dist (i m) (i n) < e"
  4345         unfolding dist_norm
  4346         using gm gn p finep
  4347         by (auto intro!: triangle3)
  4348     qed
  4349   qed
  4350   then obtain s where s: "i \<longlonglongrightarrow> s"
  4351     using convergent_eq_cauchy[symmetric] by blast
  4352   show ?thesis
  4353     unfolding integrable_on_def has_integral
  4354   proof (rule_tac x=s in exI, clarify)
  4355     fix e::real
  4356     assume e: "0 < e"
  4357     then have *: "e/3 > 0" by auto
  4358     then obtain N1 where N1: "\<forall>n\<ge>N1. norm (i n - s) < e / 3"
  4359       using LIMSEQ_D [OF s] by metis
  4360     from e True have "e / 3 / content (cbox a b) > 0"
  4361       by (auto simp add: field_simps)
  4362     from real_arch_invD[OF this] guess N2 by (elim exE conjE) note N2=this
  4363     from i[of "N1 + N2",unfolded has_integral,rule_format,OF *] guess g' .. note g'=conjunctD2[OF this,rule_format]
  4364     { fix sf sg i
  4365       assume no: "norm (sf - sg) \<le> e / 3"
  4366                  "norm(i - s) < e / 3"
  4367                  "norm (sg - i) < e / 3"
  4368       have "norm (sf - s) \<le> norm (sf - sg) + norm (sg - i) + norm (i - s)"
  4369         using norm_triangle_ineq[of "sf - sg" "sg - s"]
  4370         using norm_triangle_ineq[of "sg -  i" " i - s"]
  4371         by (auto simp add: algebra_simps)
  4372       also have "\<dots> < e"
  4373         using no
  4374         unfolding norm_minus_commute
  4375         by (auto simp add: algebra_simps)
  4376       finally have "norm (sf - s) < e" .
  4377     } note lem = this
  4378     { fix p
  4379       assume p: "p tagged_division_of (cbox a b) \<and> g' fine p"
  4380       then have norm_less: "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g (N1 + N2) x) - i (N1 + N2)) < e / 3"
  4381         using g' by blast
  4382       have "content (cbox a b) < e / 3 * (of_nat N2)"
  4383         using N2 unfolding inverse_eq_divide using True by (auto simp add: field_simps)
  4384       moreover have "e / 3 * of_nat N2 \<le> e / 3 * (of_nat (N1 + N2) + 1)"
  4385         using \<open>e>0\<close> by auto
  4386       ultimately have "content (cbox a b) < e / 3 * (of_nat (N1 + N2) + 1)"
  4387         by linarith
  4388       then have le_e3: "inverse (real (N1 + N2) + 1) * content (cbox a b) \<le> e / 3"
  4389         unfolding inverse_eq_divide
  4390         by (auto simp add: field_simps)
  4391       have ne3: "norm (i (N1 + N2) - s) < e / 3"
  4392         using N1 by auto
  4393       have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - s) < e"
  4394         apply (rule lem[OF order_trans [OF _ le_e3] ne3 norm_less])
  4395         apply (rule rsum_diff_bound[OF p[THEN conjunct1]])
  4396         apply (blast intro: g)
  4397         done }
  4398     then show "\<exists>d. gauge d \<and>
  4399              (\<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)"
  4400       by (blast intro: g')
  4401   qed
  4402 qed
  4403 
  4404 lemmas integrable_uniform_limit_real = integrable_uniform_limit [where 'a=real, simplified]
  4405 
  4406 
  4407 subsection \<open>Negligible sets.\<close>
  4408 
  4409 definition "negligible (s:: 'a::euclidean_space set) \<longleftrightarrow>
  4410   (\<forall>a b. ((indicator s :: 'a\<Rightarrow>real) has_integral 0) (cbox a b))"
  4411 
  4412 
  4413 subsection \<open>Negligibility of hyperplane.\<close>
  4414 
  4415 lemma interval_doublesplit:
  4416   fixes a :: "'a::euclidean_space"
  4417   assumes "k \<in> Basis"
  4418   shows "cbox a b \<inter> {x . \<bar>x\<bullet>k - c\<bar> \<le> (e::real)} =
  4419     cbox (\<Sum>i\<in>Basis. (if i = k then max (a\<bullet>k) (c - e) else a\<bullet>i) *\<^sub>R i)
  4420      (\<Sum>i\<in>Basis. (if i = k then min (b\<bullet>k) (c + e) else b\<bullet>i) *\<^sub>R i)"
  4421 proof -
  4422   have *: "\<And>x c e::real. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  4423     by auto
  4424   have **: "\<And>s P Q. s \<inter> {x. P x \<and> Q x} = (s \<inter> {x. Q x}) \<inter> {x. P x}"
  4425     by blast
  4426   show ?thesis
  4427     unfolding * ** interval_split[OF assms] by (rule refl)
  4428 qed
  4429 
  4430 lemma division_doublesplit:
  4431   fixes a :: "'a::euclidean_space"
  4432   assumes "p division_of (cbox a b)"
  4433     and k: "k \<in> Basis"
  4434   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> {}}
  4435          division_of  (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> e})"
  4436 proof -
  4437   have *: "\<And>x c. \<bar>x - c\<bar> \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e"
  4438     by auto
  4439   have **: "\<And>p q p' q'. p division_of q \<Longrightarrow> p = p' \<Longrightarrow> q = q' \<Longrightarrow> p' division_of q'"
  4440     by auto
  4441   note division_split(1)[OF assms, where c="c+e",unfolded interval_split[OF k]]
  4442   note division_split(2)[OF this, where c="c-e" and k=k,OF k]
  4443   then show ?thesis
  4444     apply (rule **)
  4445     subgoal
  4446       apply (simp add: abs_diff_le_iff field_simps Collect_conj_eq setcompr_eq_image[symmetric])
  4447       apply (rule equalityI)
  4448       apply blast
  4449       apply clarsimp
  4450       apply (rule_tac x="l \<inter> {x. c + e \<ge> x \<bullet> k}" in exI)
  4451       apply auto
  4452       done
  4453     by (simp add: interval_split k interval_doublesplit)
  4454 qed
  4455 
  4456 lemma content_doublesplit:
  4457   fixes a :: "'a::euclidean_space"
  4458   assumes "0 < e"
  4459     and k: "k \<in> Basis"
  4460   obtains d where "0 < d" and "content (cbox a b \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d}) < e"
  4461 proof (cases "content (cbox a b) = 0")
  4462   case True
  4463   then have ce: "content (cbox a b) < e"
  4464     by (metis \<open>0 < e\<close>)
  4465   show ?thesis
  4466     apply (rule that[of 1])
  4467     apply simp
  4468     unfolding interval_doublesplit[OF k]
  4469     apply (rule le_less_trans[OF content_subset ce])
  4470     apply (auto simp: interval_doublesplit[symmetric] k)
  4471     done
  4472 next
  4473   case False
  4474   define d where "d = e / 3 / setprod (\<lambda>i. b\<bullet>i - a\<bullet>i) (Basis - {k})"
  4475   note False[unfolded content_eq_0 not_ex not_le, rule_format]
  4476   then have "\<And>x. x \<in> Basis \<Longrightarrow> b\<bullet>x > a\<bullet>x"
  4477     by (auto simp add:not_le)
  4478   then have prod0: "0 < setprod (\<lambda>i. b\<bullet>i - a\<bullet>i) (Basis - {k})"
  4479     by (force simp add: setprod_pos field_simps)
  4480   then have "d > 0"
  4481     using assms
  4482     by (auto simp add: d_def field_simps)
  4483   then show ?thesis
  4484   proof (rule that[of d])
  4485     have *: "Basis = insert k (Basis - {k})"
  4486       using k by auto
  4487     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"
  4488     proof -
  4489       have "(min (b \<bullet> k) (c + d) - max (a \<bullet> k) (c - d)) \<le> 2 * d"
  4490         by auto
  4491       also have "\<dots> < e / (\<Prod>i\<in>Basis - {k}. b \<bullet> i - a \<bullet> i)"
  4492         unfolding d_def
  4493         using assms prod0
  4494         by (auto simp add: field_simps)
  4495       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"
  4496         unfolding pos_less_divide_eq[OF prod0] .
  4497     qed
  4498     show "content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) < e"
  4499     proof (cases "cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d} = {}")
  4500       case True
  4501       then show ?thesis
  4502         using assms by simp
  4503     next
  4504       case False
  4505       then have
  4506           "(\<Prod>i\<in>Basis - {k}. interval_upperbound (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<bullet> i -
  4507                 interval_lowerbound (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<bullet> i)
  4508            = (\<Prod>i\<in>Basis - {k}. b\<bullet>i - a\<bullet>i)"
  4509         by (simp add: box_eq_empty interval_doublesplit[OF k])
  4510       then show "content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) < e"
  4511         unfolding content_def
  4512         using assms False
  4513         apply (subst *)
  4514         apply (subst setprod.insert)
  4515         apply (simp_all add: interval_doublesplit[OF k] box_eq_empty not_less less_e)
  4516         done
  4517     qed
  4518   qed
  4519 qed
  4520 
  4521 lemma negligible_standard_hyperplane[intro]:
  4522   fixes k :: "'a::euclidean_space"
  4523   assumes k: "k \<in> Basis"
  4524   shows "negligible {x. x\<bullet>k = c}"
  4525   unfolding negligible_def has_integral
  4526 proof (clarify, goal_cases)
  4527   case (1 a b e)
  4528   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"
  4529     by (rule content_doublesplit)
  4530   let ?i = "indicator {x::'a. x\<bullet>k = c} :: 'a\<Rightarrow>real"
  4531   show ?case
  4532     apply (rule_tac x="\<lambda>x. ball x d" in exI)
  4533     apply rule
  4534     apply (rule gauge_ball)
  4535     apply (rule d)
  4536   proof (rule, rule)
  4537     fix p
  4538     assume p: "p tagged_division_of (cbox a b) \<and> (\<lambda>x. ball x d) fine p"
  4539     have *: "(\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) =
  4540       (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x\<bullet>k - c\<bar> \<le> d}) *\<^sub>R ?i x)"
  4541       apply (rule setsum.cong)
  4542       apply (rule refl)
  4543       unfolding split_paired_all real_scaleR_def mult_cancel_right split_conv
  4544       apply cases
  4545       apply (rule disjI1)
  4546       apply assumption
  4547       apply (rule disjI2)
  4548     proof -
  4549       fix x l
  4550       assume as: "(x, l) \<in> p" "?i x \<noteq> 0"
  4551       then have xk: "x\<bullet>k = c"
  4552         unfolding indicator_def
  4553         apply -
  4554         apply (rule ccontr)
  4555         apply auto
  4556         done
  4557       show "content l = content (l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})"
  4558         apply (rule arg_cong[where f=content])
  4559         apply (rule set_eqI)
  4560         apply rule
  4561         apply rule
  4562         unfolding mem_Collect_eq
  4563       proof -
  4564         fix y
  4565         assume y: "y \<in> l"
  4566         note p[THEN conjunct2,unfolded fine_def,rule_format,OF as(1),unfolded split_conv]
  4567         note this[unfolded subset_eq mem_ball dist_norm,rule_format,OF y]
  4568         note le_less_trans[OF Basis_le_norm[OF k] this]
  4569         then show "\<bar>y \<bullet> k - c\<bar> \<le> d"
  4570           unfolding inner_simps xk by auto
  4571       qed auto
  4572     qed
  4573     note p'= tagged_division_ofD[OF p[THEN conjunct1]] and p''=division_of_tagged_division[OF p[THEN conjunct1]]
  4574     show "norm ((\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) - 0) < e"
  4575       unfolding diff_0_right *
  4576       unfolding real_scaleR_def real_norm_def
  4577       apply (subst abs_of_nonneg)
  4578       apply (rule setsum_nonneg)
  4579       apply rule
  4580       unfolding split_paired_all split_conv
  4581       apply (rule mult_nonneg_nonneg)
  4582       apply (drule p'(4))
  4583       apply (erule exE)+
  4584       apply(rule_tac b=b in back_subst)
  4585       prefer 2
  4586       apply (subst(asm) eq_commute)
  4587       apply assumption
  4588       apply (subst interval_doublesplit[OF k])
  4589       apply (rule content_pos_le)
  4590       apply (rule indicator_pos_le)
  4591     proof -
  4592       have "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) * ?i x) \<le>
  4593         (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}))"
  4594         apply (rule setsum_mono)
  4595         unfolding split_paired_all split_conv
  4596         apply (rule mult_right_le_one_le)
  4597         apply (drule p'(4))
  4598         apply (auto simp add:interval_doublesplit[OF k])
  4599         done
  4600       also have "\<dots> < e"
  4601       proof (subst setsum.over_tagged_division_lemma[OF p[THEN conjunct1]], goal_cases)
  4602         case prems: (1 u v)
  4603         have "content (cbox u v \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) \<le> content (cbox u v)"
  4604           unfolding interval_doublesplit[OF k]
  4605           apply (rule content_subset)
  4606           unfolding interval_doublesplit[symmetric,OF k]
  4607           apply auto
  4608           done
  4609         then show ?case
  4610           unfolding prems interval_doublesplit[OF k]
  4611           by (blast intro: antisym)
  4612       next
  4613         have "(\<Sum>l\<in>snd ` p. content (l \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) =
  4614           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> {}})"
  4615         proof (subst (2) setsum.reindex_nontrivial)
  4616           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> {}}"
  4617             "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}"
  4618           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> {}"
  4619             by (auto)
  4620           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) = {}"
  4621             by auto
  4622           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)"
  4623             by (auto intro: interior_mono)
  4624           ultimately have "interior (x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) = {}"
  4625             by (auto simp: eq)
  4626           then show "content (x \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) = 0"
  4627             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)
  4628         qed (insert p'(1), auto intro!: setsum.mono_neutral_right)
  4629         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)"
  4630           by simp
  4631         also have "\<dots> \<le> 1 * content (cbox a b \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})"
  4632           using division_doublesplit[OF p'' k, unfolded interval_doublesplit[OF k]]
  4633           unfolding interval_doublesplit[OF k] by (intro dsum_bound) auto
  4634         also have "\<dots> < e"
  4635           using d(2) by simp
  4636         finally show "(\<Sum>ka\<in>snd ` p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d})) < e" .
  4637       qed
  4638       finally show "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x \<bullet> k - c\<bar> \<le> d}) * ?i x) < e" .
  4639     qed
  4640   qed
  4641 qed
  4642 
  4643 
  4644 subsection \<open>A technical lemma about "refinement" of division.\<close>
  4645 
  4646 lemma tagged_division_finer:
  4647   fixes p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  4648   assumes "p tagged_division_of (cbox a b)"
  4649     and "gauge d"
  4650   obtains q where "q tagged_division_of (cbox a b)"
  4651     and "d fine q"
  4652     and "\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q"
  4653 proof -
  4654   let ?P = "\<lambda>p. p tagged_partial_division_of (cbox a b) \<longrightarrow> gauge d \<longrightarrow>
  4655     (\<exists>q. q tagged_division_of (\<Union>{k. \<exists>x. (x,k) \<in> p}) \<and> d fine q \<and>
  4656       (\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q))"
  4657   {
  4658     have *: "finite p" "p tagged_partial_division_of (cbox a b)"
  4659       using assms(1)
  4660       unfolding tagged_division_of_def
  4661       by auto
  4662     presume "\<And>p. finite p \<Longrightarrow> ?P p"
  4663     from this[rule_format,OF * assms(2)] guess q .. note q=this
  4664     then show ?thesis
  4665       apply -
  4666       apply (rule that[of q])
  4667       unfolding tagged_division_ofD[OF assms(1)]
  4668       apply auto
  4669       done
  4670   }
  4671   fix p :: "('a::euclidean_space \<times> ('a::euclidean_space set)) set"
  4672   assume as: "finite p"
  4673   show "?P p"
  4674     apply rule
  4675     apply rule
  4676     using as
  4677   proof (induct p)
  4678     case empty
  4679     show ?case
  4680       apply (rule_tac x="{}" in exI)
  4681       unfolding fine_def
  4682       apply auto
  4683       done
  4684   next
  4685     case (insert xk p)
  4686     guess x k using surj_pair[of xk] by (elim exE) note xk=this
  4687     note tagged_partial_division_subset[OF insert(4) subset_insertI]
  4688     from insert(3)[OF this insert(5)] guess q1 .. note q1 = conjunctD3[OF this]
  4689     have *: "\<Union>{l. \<exists>y. (y,l) \<in> insert xk p} = k \<union> \<Union>{l. \<exists>y. (y,l) \<in> p}"
  4690       unfolding xk by auto
  4691     note p = tagged_partial_division_ofD[OF insert(4)]
  4692     from p(4)[unfolded xk, OF insertI1] guess u v by (elim exE) note uv=this
  4693 
  4694     have "finite {k. \<exists>x. (x, k) \<in> p}"
  4695       apply (rule finite_subset[of _ "snd ` p"])
  4696       using p
  4697       apply safe
  4698       apply (metis image_iff snd_conv)
  4699       apply auto
  4700       done
  4701     then have int: "interior (cbox u v) \<inter> interior (\<Union>{k. \<exists>x. (x, k) \<in> p}) = {}"
  4702       apply (rule inter_interior_unions_intervals)
  4703       apply (rule open_interior)
  4704       apply (rule_tac[!] ballI)
  4705       unfolding mem_Collect_eq
  4706       apply (erule_tac[!] exE)
  4707       apply (drule p(4)[OF insertI2])
  4708       apply assumption
  4709       apply (rule p(5))
  4710       unfolding uv xk
  4711       apply (rule insertI1)
  4712       apply (rule insertI2)
  4713       apply assumption
  4714       using insert(2)
  4715       unfolding uv xk
  4716       apply auto
  4717       done
  4718     show ?case
  4719     proof (cases "cbox u v \<subseteq> d x")
  4720       case True
  4721       then show ?thesis
  4722         apply (rule_tac x="{(x,cbox u v)} \<union> q1" in exI)
  4723         apply rule
  4724         unfolding * uv
  4725         apply (rule tagged_division_union)
  4726         apply (rule tagged_division_of_self)
  4727         apply (rule p[unfolded xk uv] insertI1)+
  4728         apply (rule q1)
  4729         apply (rule int)
  4730         apply rule
  4731         apply (rule fine_union)
  4732         apply (subst fine_def)
  4733         defer
  4734         apply (rule q1)
  4735         unfolding Ball_def split_paired_All split_conv
  4736         apply rule
  4737         apply rule
  4738         apply rule
  4739         apply rule
  4740         apply (erule insertE)
  4741         apply (simp add: uv xk)
  4742         apply (rule UnI2)
  4743         apply (drule q1(3)[rule_format])
  4744         unfolding xk uv
  4745         apply auto
  4746         done
  4747     next
  4748       case False
  4749       from fine_division_exists[OF assms(2), of u v] guess q2 . note q2=this
  4750       show ?thesis
  4751         apply (rule_tac x="q2 \<union> q1" in exI)
  4752         apply rule
  4753         unfolding * uv
  4754         apply (rule tagged_division_union q2 q1 int fine_union)+
  4755         unfolding Ball_def split_paired_All split_conv
  4756         apply rule
  4757         apply (rule fine_union)
  4758         apply (rule q1 q2)+
  4759         apply rule
  4760         apply rule
  4761         apply rule
  4762         apply rule
  4763         apply (erule insertE)
  4764         apply (rule UnI2)
  4765         apply (simp add: False uv xk)
  4766         apply (drule q1(3)[rule_format])
  4767         using False
  4768         unfolding xk uv
  4769         apply auto
  4770         done
  4771     qed
  4772   qed
  4773 qed
  4774 
  4775 
  4776 subsection \<open>Hence the main theorem about negligible sets.\<close>
  4777 
  4778 lemma finite_product_dependent:
  4779   assumes "finite s"
  4780     and "\<And>x. x \<in> s \<Longrightarrow> finite (t x)"
  4781   shows "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
  4782   using assms
  4783 proof induct
  4784   case (insert x s)
  4785   have *: "{(i, j) |i j. i \<in> insert x s \<and> j \<in> t i} =
  4786     (\<lambda>y. (x,y)) ` (t x) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  4787   show ?case
  4788     unfolding *
  4789     apply (rule finite_UnI)
  4790     using insert
  4791     apply auto
  4792     done
  4793 qed auto
  4794 
  4795 lemma sum_sum_product:
  4796   assumes "finite s"
  4797     and "\<forall>i\<in>s. finite (t i)"
  4798   shows "setsum (\<lambda>i. setsum (x i) (t i)::real) s =
  4799     setsum (\<lambda>(i,j). x i j) {(i,j) | i j. i \<in> s \<and> j \<in> t i}"
  4800   using assms
  4801 proof induct
  4802   case (insert a s)
  4803   have *: "{(i, j) |i j. i \<in> insert a s \<and> j \<in> t i} =
  4804     (\<lambda>y. (a,y)) ` (t a) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  4805   show ?case
  4806     unfolding *
  4807     apply (subst setsum.union_disjoint)
  4808     unfolding setsum.insert[OF insert(1-2)]
  4809     prefer 4
  4810     apply (subst insert(3))
  4811     unfolding add_right_cancel
  4812   proof -
  4813     show "setsum (x a) (t a) = (\<Sum>(xa, y)\<in> Pair a ` t a. x xa y)"
  4814       apply (subst setsum.reindex)
  4815       unfolding inj_on_def
  4816       apply auto
  4817       done
  4818     show "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}"
  4819       apply (rule finite_product_dependent)
  4820       using insert
  4821       apply auto
  4822       done
  4823   qed (insert insert, auto)
  4824 qed auto
  4825 
  4826 lemma has_integral_negligible:
  4827   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  4828   assumes "negligible s"
  4829     and "\<forall>x\<in>(t - s). f x = 0"
  4830   shows "(f has_integral 0) t"
  4831 proof -
  4832   presume P: "\<And>f::'b::euclidean_space \<Rightarrow> 'a.
  4833     \<And>a b. \<forall>x. x \<notin> s \<longrightarrow> f x = 0 \<Longrightarrow> (f has_integral 0) (cbox a b)"
  4834   let ?f = "(\<lambda>x. if x \<in> t then f x else 0)"
  4835   show ?thesis
  4836     apply (rule_tac f="?f" in has_integral_eq)
  4837     unfolding if_P
  4838     apply (rule refl)
  4839     apply (subst has_integral_alt)
  4840     apply cases
  4841     apply (subst if_P, assumption)
  4842     unfolding if_not_P
  4843   proof -
  4844     assume "\<exists>a b. t = cbox a b"
  4845     then guess a b apply - by (erule exE)+ note t = this
  4846     show "(?f has_integral 0) t"
  4847       unfolding t
  4848       apply (rule P)
  4849       using assms(2)
  4850       unfolding t
  4851       apply auto
  4852       done
  4853   next
  4854     show "\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
  4855       (\<exists>z. ((\<lambda>x. if x \<in> t then ?f x else 0) has_integral z) (cbox a b) \<and> norm (z - 0) < e)"
  4856       apply safe
  4857       apply (rule_tac x=1 in exI)
  4858       apply rule
  4859       apply (rule zero_less_one)
  4860       apply safe
  4861       apply (rule_tac x=0 in exI)
  4862       apply rule
  4863       apply (rule P)
  4864       using assms(2)
  4865       apply auto
  4866       done
  4867   qed
  4868 next
  4869   fix f :: "'b \<Rightarrow> 'a"
  4870   fix a b :: 'b
  4871   assume assm: "\<forall>x. x \<notin> s \<longrightarrow> f x = 0"
  4872   show "(f has_integral 0) (cbox a b)"
  4873     unfolding has_integral
  4874   proof (safe, goal_cases)
  4875     case prems: (1 e)
  4876     then have "\<And>n. e / 2 / ((real n+1) * (2 ^ n)) > 0"
  4877       apply -
  4878       apply (rule divide_pos_pos)
  4879       defer
  4880       apply (rule mult_pos_pos)
  4881       apply (auto simp add:field_simps)
  4882       done
  4883     note assms(1)[unfolded negligible_def has_integral,rule_format,OF this,of a b]
  4884     note allI[OF this,of "\<lambda>x. x"]
  4885     from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format]]
  4886     show ?case
  4887       apply (rule_tac x="\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x" in exI)
  4888     proof safe
  4889       show "gauge (\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x)"
  4890         using d(1) unfolding gauge_def by auto
  4891       fix p
  4892       assume as: "p tagged_division_of (cbox a b)" "(\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x) fine p"
  4893       let ?goal = "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  4894       {
  4895         presume "p \<noteq> {} \<Longrightarrow> ?goal"
  4896         then show ?goal
  4897           apply (cases "p = {}")
  4898           using prems
  4899           apply auto
  4900           done
  4901       }
  4902       assume as': "p \<noteq> {}"
  4903       from real_arch_simple[of "Max((\<lambda>(x,k). norm(f x)) ` p)"] guess N ..
  4904       then have N: "\<forall>x\<in>(\<lambda>(x, k). norm (f x)) ` p. x \<le> real N"
  4905         by (meson Max_ge as(1) dual_order.trans finite_imageI tagged_division_of_finite)
  4906       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)"
  4907         by (auto intro: tagged_division_finer[OF as(1) d(1)])
  4908       from choice[OF this] guess q .. note q=conjunctD3[OF this[rule_format]]
  4909       have *: "\<And>i. (\<Sum>(x, k)\<in>q i. content k *\<^sub>R indicator s x) \<ge> (0::real)"
  4910         apply (rule setsum_nonneg)
  4911         apply safe
  4912         unfolding real_scaleR_def
  4913         apply (drule tagged_division_ofD(4)[OF q(1)])
  4914         apply (auto intro: mult_nonneg_nonneg)
  4915         done
  4916       have **: "finite s \<Longrightarrow> finite t \<Longrightarrow> (\<forall>(x,y) \<in> t. (0::real) \<le> g(x,y)) \<Longrightarrow>
  4917         (\<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
  4918         apply (rule setsum_le_included[of s t g snd f])
  4919         prefer 4
  4920         apply safe
  4921         apply (erule_tac x=x in ballE)
  4922         apply (erule exE)
  4923         apply (rule_tac x="(xa,x)" in bexI)
  4924         apply auto
  4925         done
  4926       have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) \<le> setsum (\<lambda>i. (real i + 1) *
  4927         norm (setsum (\<lambda>(x,k). content k *\<^sub>R indicator s x :: real) (q i))) {..N+1}"
  4928         unfolding real_norm_def setsum_right_distrib abs_of_nonneg[OF *] diff_0_right
  4929         apply (rule order_trans)
  4930         apply (rule norm_setsum)
  4931         apply (subst sum_sum_product)
  4932         prefer 3
  4933       proof (rule **, safe)
  4934         show "finite {(i, j) |i j. i \<in> {..N + 1} \<and> j \<in> q i}"
  4935           apply (rule finite_product_dependent)
  4936           using q
  4937           apply auto
  4938           done
  4939         fix i a b
  4940         assume as'': "(a, b) \<in> q i"
  4941         show "0 \<le> (real i + 1) * (content b *\<^sub>R indicator s a)"
  4942           unfolding real_scaleR_def
  4943           using tagged_division_ofD(4)[OF q(1) as'']
  4944           by (auto intro!: mult_nonneg_nonneg)
  4945       next
  4946         fix i :: nat
  4947         show "finite (q i)"
  4948           using q by auto
  4949       next
  4950         fix x k
  4951         assume xk: "(x, k) \<in> p"
  4952         define n where "n = nat \<lfloor>norm (f x)\<rfloor>"
  4953         have *: "norm (f x) \<in> (\<lambda>(x, k). norm (f x)) ` p"
  4954           using xk by auto
  4955         have nfx: "real n \<le> norm (f x)" "norm (f x) \<le> real n + 1"
  4956           unfolding n_def by auto
  4957         then have "n \<in> {0..N + 1}"
  4958           using N[rule_format,OF *] by auto
  4959         moreover
  4960         note as(2)[unfolded fine_def,rule_format,OF xk,unfolded split_conv]
  4961         note q(3)[rule_format,OF xk,unfolded split_conv,rule_format,OF this]
  4962         note this[unfolded n_def[symmetric]]
  4963         moreover
  4964         have "norm (content k *\<^sub>R f x) \<le> (real n + 1) * (content k * indicator s x)"
  4965         proof (cases "x \<in> s")
  4966           case False
  4967           then show ?thesis
  4968             using assm by auto
  4969         next
  4970           case True
  4971           have *: "content k \<ge> 0"
  4972             using tagged_division_ofD(4)[OF as(1) xk] by auto
  4973           moreover
  4974           have "content k * norm (f x) \<le> content k * (real n + 1)"
  4975             apply (rule mult_mono)
  4976             using nfx *
  4977             apply auto
  4978             done
  4979           ultimately
  4980           show ?thesis
  4981             unfolding abs_mult
  4982             using nfx True
  4983             by (auto simp add: field_simps)
  4984         qed
  4985         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>
  4986           (real y + 1) * (content k *\<^sub>R indicator s x)"
  4987           apply (rule_tac x=n in exI)
  4988           apply safe
  4989           apply (rule_tac x=n in exI)
  4990           apply (rule_tac x="(x,k)" in exI)
  4991           apply safe
  4992           apply auto
  4993           done
  4994       qed (insert as, auto)
  4995       also have "\<dots> \<le> setsum (\<lambda>i. e / 2 / 2 ^ i) {..N+1}"
  4996       proof (rule setsum_mono, goal_cases)
  4997         case (1 i)
  4998         then show ?case
  4999           apply (subst mult.commute, subst pos_le_divide_eq[symmetric])
  5000           using d(2)[rule_format, of "q i" i]
  5001           using q[rule_format]
  5002           apply (auto simp add: field_simps)
  5003           done
  5004       qed
  5005       also have "\<dots> < e * inverse 2 * 2"
  5006         unfolding divide_inverse setsum_right_distrib[symmetric]
  5007         apply (rule mult_strict_left_mono)
  5008         unfolding power_inverse [symmetric] lessThan_Suc_atMost[symmetric]
  5009         apply (subst geometric_sum)
  5010         using prems
  5011         apply auto
  5012         done
  5013       finally show "?goal" by auto
  5014     qed
  5015   qed
  5016 qed
  5017 
  5018 lemma has_integral_spike:
  5019   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  5020   assumes "negligible s"
  5021     and "(\<forall>x\<in>(t - s). g x = f x)"
  5022     and "(f has_integral y) t"
  5023   shows "(g has_integral y) t"
  5024 proof -
  5025   {
  5026     fix a b :: 'b
  5027     fix f g :: "'b \<Rightarrow> 'a"
  5028     fix y :: 'a
  5029     assume as: "\<forall>x \<in> cbox a b - s. g x = f x" "(f has_integral y) (cbox a b)"
  5030     have "((\<lambda>x. f x + (g x - f x)) has_integral (y + 0)) (cbox a b)"
  5031       apply (rule has_integral_add[OF as(2)])
  5032       apply (rule has_integral_negligible[OF assms(1)])
  5033       using as
  5034       apply auto
  5035       done
  5036     then have "(g has_integral y) (cbox a b)"
  5037       by auto
  5038   } note * = this
  5039   show ?thesis
  5040     apply (subst has_integral_alt)
  5041     using assms(2-)
  5042     apply -
  5043     apply (rule cond_cases)
  5044     apply safe
  5045     apply (rule *)
  5046     apply assumption+
  5047     apply (subst(asm) has_integral_alt)
  5048     unfolding if_not_P
  5049     apply (erule_tac x=e in allE)
  5050     apply safe
  5051     apply (rule_tac x=B in exI)
  5052     apply safe
  5053     apply (erule_tac x=a in allE)
  5054     apply (erule_tac x=b in allE)
  5055     apply safe
  5056     apply (rule_tac x=z in exI)
  5057     apply safe
  5058     apply (rule *[where fa2="\<lambda>x. if x\<in>t then f x else 0"])
  5059     apply auto
  5060     done
  5061 qed
  5062 
  5063 lemma has_integral_spike_eq:
  5064   assumes "negligible s"
  5065     and "\<forall>x\<in>(t - s). g x = f x"
  5066   shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  5067   apply rule
  5068   apply (rule_tac[!] has_integral_spike[OF assms(1)])
  5069   using assms(2)
  5070   apply auto
  5071   done
  5072 
  5073 lemma integrable_spike:
  5074   assumes "negligible s"
  5075     and "\<forall>x\<in>(t - s). g x = f x"
  5076     and "f integrable_on t"
  5077   shows "g integrable_on  t"
  5078   using assms
  5079   unfolding integrable_on_def
  5080   apply -
  5081   apply (erule exE)
  5082   apply rule
  5083   apply (rule has_integral_spike)
  5084   apply fastforce+
  5085   done
  5086 
  5087 lemma integral_spike:
  5088   assumes "negligible s"
  5089     and "\<forall>x\<in>(t - s). g x = f x"
  5090   shows "integral t f = integral t g"
  5091   using has_integral_spike_eq[OF assms] by (simp add: integral_def integrable_on_def)
  5092 
  5093 
  5094 subsection \<open>Some other trivialities about negligible sets.\<close>
  5095 
  5096 lemma negligible_subset[intro]:
  5097   assumes "negligible s"
  5098     and "t \<subseteq> s"
  5099   shows "negligible t"
  5100   unfolding negligible_def
  5101 proof (safe, goal_cases)
  5102   case (1 a b)
  5103   show ?case
  5104     using assms(1)[unfolded negligible_def,rule_format,of a b]
  5105     apply -
  5106     apply (rule has_integral_spike[OF assms(1)])
  5107     defer
  5108     apply assumption
  5109     using assms(2)
  5110     unfolding indicator_def
  5111     apply auto
  5112     done
  5113 qed
  5114 
  5115 lemma negligible_diff[intro?]:
  5116   assumes "negligible s"
  5117   shows "negligible (s - t)"
  5118   using assms by auto
  5119 
  5120 lemma negligible_Int:
  5121   assumes "negligible s \<or> negligible t"
  5122   shows "negligible (s \<inter> t)"
  5123   using assms by auto
  5124 
  5125 lemma negligible_Un:
  5126   assumes "negligible s"
  5127     and "negligible t"
  5128   shows "negligible (s \<union> t)"
  5129   unfolding negligible_def
  5130 proof (safe, goal_cases)
  5131   case (1 a b)
  5132   note assm = assms[unfolded negligible_def,rule_format,of a b]
  5133   then show ?case
  5134     apply (subst has_integral_spike_eq[OF assms(2)])
  5135     defer
  5136     apply assumption
  5137     unfolding indicator_def
  5138     apply auto
  5139     done
  5140 qed
  5141 
  5142 lemma negligible_Un_eq[simp]: "negligible (s \<union> t) \<longleftrightarrow> negligible s \<and> negligible t"
  5143   using negligible_Un by auto
  5144 
  5145 lemma negligible_sing[intro]: "negligible {a::'a::euclidean_space}"
  5146   using negligible_standard_hyperplane[OF SOME_Basis, of "a \<bullet> (SOME i. i \<in> Basis)"] by auto
  5147 
  5148 lemma negligible_insert[simp]: "negligible (insert a s) \<longleftrightarrow> negligible s"
  5149   apply (subst insert_is_Un)
  5150   unfolding negligible_Un_eq
  5151   apply auto
  5152   done
  5153 
  5154 lemma negligible_empty[iff]: "negligible {}"
  5155   by auto
  5156 
  5157 lemma negligible_finite[intro]:
  5158   assumes "finite s"
  5159   shows "negligible s"
  5160   using assms by (induct s) auto
  5161 
  5162 lemma negligible_Union[intro]:
  5163   assumes "finite s"
  5164     and "\<forall>t\<in>s. negligible t"
  5165   shows "negligible(\<Union>s)"
  5166   using assms by induct auto
  5167 
  5168 lemma negligible:
  5169   "negligible s \<longleftrightarrow> (\<forall>t::('a::euclidean_space) set. ((indicator s::'a\<Rightarrow>real) has_integral 0) t)"
  5170   apply safe
  5171   defer
  5172   apply (subst negligible_def)
  5173 proof -
  5174   fix t :: "'a set"
  5175   assume as: "negligible s"
  5176   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)"
  5177     by auto
  5178   show "((indicator s::'a\<Rightarrow>real) has_integral 0) t"
  5179     apply (subst has_integral_alt)
  5180     apply cases
  5181     apply (subst if_P,assumption)
  5182     unfolding if_not_P
  5183     apply safe
  5184     apply (rule as[unfolded negligible_def,rule_format])
  5185     apply (rule_tac x=1 in exI)
  5186     apply safe
  5187     apply (rule zero_less_one)
  5188     apply (rule_tac x=0 in exI)
  5189     using negligible_subset[OF as,of "s \<inter> t"]
  5190     unfolding negligible_def indicator_def [abs_def]
  5191     unfolding *
  5192     apply auto
  5193     done
  5194 qed auto
  5195 
  5196 
  5197 subsection \<open>Finite case of the spike theorem is quite commonly needed.\<close>
  5198 
  5199 lemma has_integral_spike_finite:
  5200   assumes "finite s"
  5201     and "\<forall>x\<in>t-s. g x = f x"
  5202     and "(f has_integral y) t"
  5203   shows "(g has_integral y) t"
  5204   apply (rule has_integral_spike)
  5205   using assms
  5206   apply auto
  5207   done
  5208 
  5209 lemma has_integral_spike_finite_eq:
  5210   assumes "finite s"
  5211     and "\<forall>x\<in>t-s. g x = f x"
  5212   shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  5213   apply rule
  5214   apply (rule_tac[!] has_integral_spike_finite)
  5215   using assms
  5216   apply auto
  5217   done
  5218 
  5219 lemma integrable_spike_finite:
  5220   assumes "finite s"
  5221     and "\<forall>x\<in>t-s. g x = f x"
  5222     and "f integrable_on t"
  5223   shows "g integrable_on  t"
  5224   using assms
  5225   unfolding integrable_on_def
  5226   apply safe
  5227   apply (rule_tac x=y in exI)
  5228   apply (rule has_integral_spike_finite)
  5229   apply auto
  5230   done
  5231 
  5232 
  5233 subsection \<open>In particular, the boundary of an interval is negligible.\<close>
  5234 
  5235 lemma negligible_frontier_interval: "negligible(cbox (a::'a::euclidean_space) b - box a b)"
  5236 proof -
  5237   let ?A = "\<Union>((\<lambda>k. {x. x\<bullet>k = a\<bullet>k} \<union> {x::'a. x\<bullet>k = b\<bullet>k}) ` Basis)"
  5238   have "cbox a b - box a b \<subseteq> ?A"
  5239     apply rule unfolding Diff_iff mem_box
  5240     apply simp
  5241     apply(erule conjE bexE)+
  5242     apply(rule_tac x=i in bexI)
  5243     apply auto
  5244     done
  5245   then show ?thesis
  5246     apply -
  5247     apply (rule negligible_subset[of ?A])
  5248     apply (rule negligible_Union[OF finite_imageI])
  5249     apply auto
  5250     done
  5251 qed
  5252 
  5253 lemma has_integral_spike_interior:
  5254   assumes "\<forall>x\<in>box a b. g x = f x"
  5255     and "(f has_integral y) (cbox a b)"
  5256   shows "(g has_integral y) (cbox a b)"
  5257   apply (rule has_integral_spike[OF negligible_frontier_interval _ assms(2)])
  5258   using assms(1)
  5259   apply auto
  5260   done
  5261 
  5262 lemma has_integral_spike_interior_eq:
  5263   assumes "\<forall>x\<in>box a b. g x = f x"
  5264   shows "(f has_integral y) (cbox a b) \<longleftrightarrow> (g has_integral y) (cbox a b)"
  5265   apply rule
  5266   apply (rule_tac[!] has_integral_spike_interior)
  5267   using assms
  5268   apply auto
  5269   done
  5270 
  5271 lemma integrable_spike_interior:
  5272   assumes "\<forall>x\<in>box a b. g x = f x"
  5273     and "f integrable_on cbox a b"
  5274   shows "g integrable_on cbox a b"
  5275   using assms
  5276   unfolding integrable_on_def
  5277   using has_integral_spike_interior[OF assms(1)]
  5278   by auto
  5279 
  5280 
  5281 subsection \<open>Integrability of continuous functions.\<close>
  5282 
  5283 lemma operative_approximable:
  5284   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5285   assumes "0 \<le> e"
  5286   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)"
  5287   unfolding comm_monoid.operative_def[OF comm_monoid_and]
  5288 proof safe
  5289   fix a b :: 'b
  5290   show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  5291     if "content (cbox a b) = 0"
  5292     apply (rule_tac x=f in exI)
  5293     using assms that
  5294     apply (auto intro!: integrable_on_null)
  5295     done
  5296   {
  5297     fix c g
  5298     fix k :: 'b
  5299     assume as: "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e" "g integrable_on cbox a b"
  5300     assume k: "k \<in> Basis"
  5301     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}"
  5302       "\<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}"
  5303       apply (rule_tac[!] x=g in exI)
  5304       using as(1) integrable_split[OF as(2) k]
  5305       apply auto
  5306       done
  5307   }
  5308   fix c k g1 g2
  5309   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}"
  5310     "\<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}"
  5311   assume k: "k \<in> Basis"
  5312   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"
  5313   show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  5314     apply (rule_tac x="?g" in exI)
  5315     apply safe
  5316   proof goal_cases
  5317     case (1 x)
  5318     then show ?case
  5319       apply -
  5320       apply (cases "x\<bullet>k=c")
  5321       apply (case_tac "x\<bullet>k < c")
  5322       using as assms
  5323       apply auto
  5324       done
  5325   next
  5326     case 2
  5327     presume "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<le> c}"
  5328       and "?g integrable_on cbox a b \<inter> {x. x \<bullet> k \<ge> c}"
  5329     then guess h1 h2 unfolding integrable_on_def by auto
  5330     from has_integral_split[OF this k] show ?case
  5331       unfolding integrable_on_def by auto
  5332   next
  5333     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}"
  5334       apply(rule_tac[!] integrable_spike[OF negligible_standard_hyperplane[of k c]])
  5335       using k as(2,4)
  5336       apply auto
  5337       done
  5338   qed
  5339 qed
  5340 
  5341 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))"
  5342 proof -
  5343   interpret bool: comm_monoid_set "op \<and>" True
  5344     proof qed auto
  5345   show ?thesis
  5346     by (induction s rule: infinite_finite_induct) auto
  5347 qed
  5348 
  5349 lemma approximable_on_division:
  5350   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5351   assumes "0 \<le> e"
  5352     and "d division_of (cbox a b)"
  5353     and "\<forall>i\<in>d. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  5354   obtains g where "\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e" "g integrable_on cbox a b"
  5355 proof -
  5356   note * = comm_monoid_set.operative_division[OF comm_monoid_set_and operative_approximable[OF assms(1)] assms(2)]
  5357   from assms(3) this[unfolded comm_monoid_set_F_and, of f] division_of_finite[OF assms(2)]
  5358   guess g by auto
  5359   then show thesis
  5360     apply -
  5361     apply (rule that[of g])
  5362     apply auto
  5363     done
  5364 qed
  5365 
  5366 lemma integrable_continuous:
  5367   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5368   assumes "continuous_on (cbox a b) f"
  5369   shows "f integrable_on cbox a b"
  5370 proof (rule integrable_uniform_limit, safe)
  5371   fix e :: real
  5372   assume e: "e > 0"
  5373   from compact_uniformly_continuous[OF assms compact_cbox,unfolded uniformly_continuous_on_def,rule_format,OF e] guess d ..
  5374   note d=conjunctD2[OF this,rule_format]
  5375   from fine_division_exists[OF gauge_ball[OF d(1)], of a b] guess p . note p=this
  5376   note p' = tagged_division_ofD[OF p(1)]
  5377   have *: "\<forall>i\<in>snd ` p. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  5378   proof (safe, unfold snd_conv)
  5379     fix x l
  5380     assume as: "(x, l) \<in> p"
  5381     from p'(4)[OF this] guess a b by (elim exE) note l=this
  5382     show "\<exists>g. (\<forall>x\<in>l. norm (f x - g x) \<le> e) \<and> g integrable_on l"
  5383       apply (rule_tac x="\<lambda>y. f x" in exI)
  5384     proof safe
  5385       show "(\<lambda>y. f x) integrable_on l"
  5386         unfolding integrable_on_def l
  5387         apply rule
  5388         apply (rule has_integral_const)
  5389         done
  5390       fix y
  5391       assume y: "y \<in> l"
  5392       note fineD[OF p(2) as,unfolded subset_eq,rule_format,OF this]
  5393       note d(2)[OF _ _ this[unfolded mem_ball]]
  5394       then show "norm (f y - f x) \<le> e"
  5395         using y p'(2-3)[OF as] unfolding dist_norm l norm_minus_commute by fastforce
  5396     qed
  5397   qed
  5398   from e have "e \<ge> 0"
  5399     by auto
  5400   from approximable_on_division[OF this division_of_tagged_division[OF p(1)] *] guess g .
  5401   then show "\<exists>g. (\<forall>x\<in>cbox a b. norm (f x - g x) \<le> e) \<and> g integrable_on cbox a b"
  5402     by auto
  5403 qed
  5404 
  5405 lemma integrable_continuous_real:
  5406   fixes f :: "real \<Rightarrow> 'a::banach"
  5407   assumes "continuous_on {a .. b} f"
  5408   shows "f integrable_on {a .. b}"
  5409   by (metis assms box_real(2) integrable_continuous)
  5410 
  5411 subsection \<open>Specialization of additivity to one dimension.\<close>
  5412 
  5413 subsection \<open>Special case of additivity we need for the FTC.\<close>
  5414 
  5415 lemma additive_tagged_division_1:
  5416   fixes f :: "real \<Rightarrow> 'a::real_normed_vector"
  5417   assumes "a \<le> b"
  5418     and "p tagged_division_of {a..b}"
  5419   shows "setsum (\<lambda>(x,k). f(Sup k) - f(Inf k)) p = f b - f a"
  5420 proof -
  5421   let ?f = "(\<lambda>k::(real) set. if k = {} then 0 else f(interval_upperbound k) - f(interval_lowerbound k))"
  5422   have ***: "\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i"
  5423     using assms by auto
  5424   have *: "add.operative ?f"
  5425     unfolding add.operative_1_lt box_eq_empty
  5426     by auto
  5427   have **: "cbox a b \<noteq> {}"
  5428     using assms(1) by auto
  5429   note setsum.operative_tagged_division[OF * assms(2)[simplified box_real[symmetric]]]
  5430   note * = this[unfolded if_not_P[OF **] interval_bounds[OF ***],symmetric]
  5431   show ?thesis
  5432     unfolding *
  5433     apply (rule setsum.cong)
  5434     unfolding split_paired_all split_conv
  5435     using assms(2)
  5436     apply auto
  5437     done
  5438 qed
  5439 
  5440 
  5441 subsection \<open>A useful lemma allowing us to factor out the content size.\<close>
  5442 
  5443 lemma has_integral_factor_content:
  5444   "(f has_integral i) (cbox a b) \<longleftrightarrow>
  5445     (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  5446       norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content (cbox a b)))"
  5447 proof (cases "content (cbox a b) = 0")
  5448   case True
  5449   show ?thesis
  5450     unfolding has_integral_null_eq[OF True]
  5451     apply safe
  5452     apply (rule, rule, rule gauge_trivial, safe)
  5453     unfolding setsum_content_null[OF True] True
  5454     defer
  5455     apply (erule_tac x=1 in allE)
  5456     apply safe
  5457     defer
  5458     apply (rule fine_division_exists[of _ a b])
  5459     apply assumption
  5460     apply (erule_tac x=p in allE)
  5461     unfolding setsum_content_null[OF True]
  5462     apply auto
  5463     done
  5464 next
  5465   case False
  5466   note F = this[unfolded content_lt_nz[symmetric]]
  5467   let ?P = "\<lambda>e opp. \<exists>d. gauge d \<and>
  5468     (\<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)"
  5469   show ?thesis
  5470     apply (subst has_integral)
  5471   proof safe
  5472     fix e :: real
  5473     assume e: "e > 0"
  5474     {
  5475       assume "\<forall>e>0. ?P e op <"
  5476       then show "?P (e * content (cbox a b)) op \<le>"
  5477         apply (erule_tac x="e * content (cbox a b)" in allE)
  5478         apply (erule impE)
  5479         defer
  5480         apply (erule exE,rule_tac x=d in exI)
  5481         using F e
  5482         apply (auto simp add:field_simps)
  5483         done
  5484     }
  5485     {
  5486       assume "\<forall>e>0. ?P (e * content (cbox a b)) op \<le>"
  5487       then show "?P e op <"
  5488         apply (erule_tac x="e / 2 / content (cbox a b)" in allE)
  5489         apply (erule impE)
  5490         defer
  5491         apply (erule exE,rule_tac x=d in exI)
  5492         using F e
  5493         apply (auto simp add: field_simps)
  5494         done
  5495     }
  5496   qed
  5497 qed
  5498 
  5499 lemma has_integral_factor_content_real:
  5500   "(f has_integral i) {a .. b::real} \<longleftrightarrow>
  5501     (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a .. b}  \<and> d fine p \<longrightarrow>
  5502       norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content {a .. b} ))"
  5503   unfolding box_real[symmetric]
  5504   by (rule has_integral_factor_content)
  5505 
  5506 
  5507 subsection \<open>Fundamental theorem of calculus.\<close>
  5508 
  5509 lemma interval_bounds_real:
  5510   fixes q b :: real
  5511   assumes "a \<le> b"
  5512   shows "Sup {a..b} = b"
  5513     and "Inf {a..b} = a"
  5514   using assms by auto
  5515 
  5516 lemma fundamental_theorem_of_calculus:
  5517   fixes f :: "real \<Rightarrow> 'a::banach"
  5518   assumes "a \<le> b"
  5519     and "\<forall>x\<in>{a .. b}. (f has_vector_derivative f' x) (at x within {a .. b})"
  5520   shows "(f' has_integral (f b - f a)) {a .. b}"
  5521   unfolding has_integral_factor_content box_real[symmetric]
  5522 proof safe
  5523   fix e :: real
  5524   assume e: "e > 0"
  5525   note assm = assms(2)[unfolded has_vector_derivative_def has_derivative_within_alt]
  5526   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"
  5527     using e by blast
  5528   note this[OF assm,unfolded gauge_existence_lemma]
  5529   from choice[OF this,unfolded Ball_def[symmetric]] guess d ..
  5530   note d=conjunctD2[OF this[rule_format],rule_format]
  5531   show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of (cbox a b) \<and> d fine p \<longrightarrow>
  5532     norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content (cbox a b))"
  5533     apply (rule_tac x="\<lambda>x. ball x (d x)" in exI)
  5534     apply safe
  5535     apply (rule gauge_ball_dependent)
  5536     apply rule
  5537     apply (rule d(1))
  5538   proof -
  5539     fix p
  5540     assume as: "p tagged_division_of cbox a b" "(\<lambda>x. ball x (d x)) fine p"
  5541     show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content (cbox a b)"
  5542       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]
  5543       unfolding additive_tagged_division_1[OF assms(1) as(1)[simplified box_real],of "\<lambda>x. x",symmetric]
  5544       unfolding setsum_right_distrib
  5545       defer
  5546       unfolding setsum_subtractf[symmetric]
  5547     proof (rule setsum_norm_le,safe)
  5548       fix x k
  5549       assume "(x, k) \<in> p"
  5550       note xk = tagged_division_ofD(2-4)[OF as(1) this]
  5551       from this(3) guess u v by (elim exE) note k=this
  5552       have *: "u \<le> v"
  5553         using xk unfolding k by auto
  5554       have ball: "\<forall>xa\<in>k. xa \<in> ball x (d x)"
  5555         using as(2)[unfolded fine_def,rule_format,OF \<open>(x,k)\<in>p\<close>,unfolded split_conv subset_eq] .
  5556       have "norm ((v - u) *\<^sub>R f' x - (f v - f u)) \<le>
  5557         norm (f u - f x - (u - x) *\<^sub>R f' x) + norm (f v - f x - (v - x) *\<^sub>R f' x)"
  5558         apply (rule order_trans[OF _ norm_triangle_ineq4])
  5559         apply (rule eq_refl)
  5560         apply (rule arg_cong[where f=norm])
  5561         unfolding scaleR_diff_left
  5562         apply (auto simp add:algebra_simps)
  5563         done
  5564       also have "\<dots> \<le> e * norm (u - x) + e * norm (v - x)"
  5565         apply (rule add_mono)
  5566         apply (rule d(2)[of "x" "u",unfolded o_def])
  5567         prefer 4
  5568         apply (rule d(2)[of "x" "v",unfolded o_def])
  5569         using ball[rule_format,of u] ball[rule_format,of v]
  5570         using xk(1-2)
  5571         unfolding k subset_eq
  5572         apply (auto simp add:dist_real_def)
  5573         done
  5574       also have "\<dots> \<le> e * (Sup k - Inf k)"
  5575         unfolding k interval_bounds_real[OF *]
  5576         using xk(1)
  5577         unfolding k
  5578         by (auto simp add: dist_real_def field_simps)
  5579       finally show "norm (content k *\<^sub>R f' x - (f (Sup k) - f (Inf k))) \<le>
  5580         e * (Sup k - Inf k)"
  5581         unfolding box_real k interval_bounds_real[OF *] content_real[OF *]
  5582           interval_upperbound_real interval_lowerbound_real
  5583           .
  5584     qed
  5585   qed
  5586 qed
  5587 
  5588 lemma ident_has_integral:
  5589   fixes a::real
  5590   assumes "a \<le> b"
  5591   shows "((\<lambda>x. x) has_integral (b\<^sup>2 - a\<^sup>2) / 2) {a..b}"
  5592 proof -
  5593   have "((\<lambda>x. x) has_integral inverse 2 * b\<^sup>2 - inverse 2 * a\<^sup>2) {a..b}"
  5594     apply (rule fundamental_theorem_of_calculus [OF assms], clarify)
  5595     unfolding power2_eq_square
  5596     by (rule derivative_eq_intros | simp)+
  5597   then show ?thesis
  5598     by (simp add: field_simps)
  5599 qed
  5600 
  5601 lemma integral_ident [simp]:
  5602   fixes a::real
  5603   assumes "a \<le> b"
  5604   shows "integral {a..b} (\<lambda>x. x) = (if a \<le> b then (b\<^sup>2 - a\<^sup>2) / 2 else 0)"
  5605 using ident_has_integral integral_unique by fastforce
  5606 
  5607 lemma ident_integrable_on:
  5608   fixes a::real
  5609   shows "(\<lambda>x. x) integrable_on {a..b}"
  5610 by (metis atLeastatMost_empty_iff integrable_on_def has_integral_empty ident_has_integral)
  5611 
  5612 
  5613 subsection \<open>Taylor series expansion\<close>
  5614 
  5615 lemma (in bounded_bilinear) setsum_prod_derivatives_has_vector_derivative:
  5616   assumes "p>0"
  5617   and f0: "Df 0 = f"
  5618   and Df: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5619     (Df m has_vector_derivative Df (Suc m) t) (at t within {a .. b})"
  5620   and g0: "Dg 0 = g"
  5621   and Dg: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5622     (Dg m has_vector_derivative Dg (Suc m) t) (at t within {a .. b})"
  5623   and ivl: "a \<le> t" "t \<le> b"
  5624   shows "((\<lambda>t. \<Sum>i<p. (-1)^i *\<^sub>R prod (Df i t) (Dg (p - Suc i) t))
  5625     has_vector_derivative
  5626       prod (f t) (Dg p t) - (-1)^p *\<^sub>R prod (Df p t) (g t))
  5627     (at t within {a .. b})"
  5628   using assms
  5629 proof cases
  5630   assume p: "p \<noteq> 1"
  5631   define p' where "p' = p - 2"
  5632   from assms p have p': "{..<p} = {..Suc p'}" "p = Suc (Suc p')"
  5633     by (auto simp: p'_def)
  5634   have *: "\<And>i. i \<le> p' \<Longrightarrow> Suc (Suc p' - i) = (Suc (Suc p') - i)"
  5635     by auto
  5636   let ?f = "\<lambda>i. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg ((p - i)) t))"
  5637   have "(\<Sum>i<p. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg (Suc (p - Suc i)) t) +
  5638     prod (Df (Suc i) t) (Dg (p - Suc i) t))) =
  5639     (\<Sum>i\<le>(Suc p'). ?f i - ?f (Suc i))"
  5640     by (auto simp: algebra_simps p'(2) numeral_2_eq_2 * lessThan_Suc_atMost)
  5641   also note setsum_telescope
  5642   finally
  5643   have "(\<Sum>i<p. (-1) ^ i *\<^sub>R (prod (Df i t) (Dg (Suc (p - Suc i)) t) +
  5644     prod (Df (Suc i) t) (Dg (p - Suc i) t)))
  5645     = prod (f t) (Dg p t) - (- 1) ^ p *\<^sub>R prod (Df p t) (g t)"
  5646     unfolding p'[symmetric]
  5647     by (simp add: assms)
  5648   thus ?thesis
  5649     using assms
  5650     by (auto intro!: derivative_eq_intros has_vector_derivative)
  5651 qed (auto intro!: derivative_eq_intros has_vector_derivative)
  5652 
  5653 lemma
  5654   fixes f::"real\<Rightarrow>'a::banach"
  5655   assumes "p>0"
  5656   and f0: "Df 0 = f"
  5657   and Df: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5658     (Df m has_vector_derivative Df (Suc m) t) (at t within {a .. b})"
  5659   and ivl: "a \<le> b"
  5660   defines "i \<equiv> \<lambda>x. ((b - x) ^ (p - 1) / fact (p - 1)) *\<^sub>R Df p x"
  5661   shows taylor_has_integral:
  5662     "(i has_integral f b - (\<Sum>i<p. ((b - a) ^ i / fact i) *\<^sub>R Df i a)) {a..b}"
  5663   and taylor_integral:
  5664     "f b = (\<Sum>i<p. ((b - a) ^ i / fact i) *\<^sub>R Df i a) + integral {a..b} i"
  5665   and taylor_integrable:
  5666     "i integrable_on {a .. b}"
  5667 proof goal_cases
  5668   case 1
  5669   interpret bounded_bilinear "scaleR::real\<Rightarrow>'a\<Rightarrow>'a"
  5670     by (rule bounded_bilinear_scaleR)
  5671   define g where "g s = (b - s)^(p - 1)/fact (p - 1)" for s
  5672   define Dg where [abs_def]:
  5673     "Dg n s = (if n < p then (-1)^n * (b - s)^(p - 1 - n) / fact (p - 1 - n) else 0)" for n s
  5674   have g0: "Dg 0 = g"
  5675     using \<open>p > 0\<close>
  5676     by (auto simp add: Dg_def divide_simps g_def split: if_split_asm)
  5677   {
  5678     fix m
  5679     assume "p > Suc m"
  5680     hence "p - Suc m = Suc (p - Suc (Suc m))"
  5681       by auto
  5682     hence "real (p - Suc m) * fact (p - Suc (Suc m)) = fact (p - Suc m)"
  5683       by auto
  5684   } note fact_eq = this
  5685   have Dg: "\<And>m t. m < p \<Longrightarrow> a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5686     (Dg m has_vector_derivative Dg (Suc m) t) (at t within {a .. b})"
  5687     unfolding Dg_def
  5688     by (auto intro!: derivative_eq_intros simp: has_vector_derivative_def fact_eq divide_simps)
  5689   let ?sum = "\<lambda>t. \<Sum>i<p. (- 1) ^ i *\<^sub>R Dg i t *\<^sub>R Df (p - Suc i) t"
  5690   from setsum_prod_derivatives_has_vector_derivative[of _ Dg _ _ _ Df,
  5691       OF \<open>p > 0\<close> g0 Dg f0 Df]
  5692   have deriv: "\<And>t. a \<le> t \<Longrightarrow> t \<le> b \<Longrightarrow>
  5693     (?sum has_vector_derivative
  5694       g t *\<^sub>R Df p t - (- 1) ^ p *\<^sub>R Dg p t *\<^sub>R f t) (at t within {a..b})"
  5695     by auto
  5696   from fundamental_theorem_of_calculus[rule_format, OF \<open>a \<le> b\<close> deriv]
  5697   have "(i has_integral ?sum b - ?sum a) {a .. b}"
  5698     by (simp add: i_def g_def Dg_def)
  5699   also
  5700   have one: "(- 1) ^ p' * (- 1) ^ p' = (1::real)"
  5701     and "{..<p} \<inter> {i. p = Suc i} = {p - 1}"
  5702     for p'
  5703     using \<open>p > 0\<close>
  5704     by (auto simp: power_mult_distrib[symmetric])
  5705   then have "?sum b = f b"
  5706     using Suc_pred'[OF \<open>p > 0\<close>]
  5707     by (simp add: diff_eq_eq Dg_def power_0_left le_Suc_eq if_distrib
  5708         cond_application_beta setsum.If_cases f0)
  5709   also
  5710   have "{..<p} = (\<lambda>x. p - x - 1) ` {..<p}"
  5711   proof safe
  5712     fix x
  5713     assume "x < p"
  5714     thus "x \<in> (\<lambda>x. p - x - 1) ` {..<p}"
  5715       by (auto intro!: image_eqI[where x = "p - x - 1"])
  5716   qed simp
  5717   from _ this
  5718   have "?sum a = (\<Sum>i<p. ((b - a) ^ i / fact i) *\<^sub>R Df i a)"
  5719     by (rule setsum.reindex_cong) (auto simp add: inj_on_def Dg_def one)
  5720   finally show c: ?case .
  5721   case 2 show ?case using c integral_unique by force
  5722   case 3 show ?case using c by force
  5723 qed
  5724 
  5725 
  5726 subsection \<open>Attempt a systematic general set of "offset" results for components.\<close>
  5727 
  5728 lemma gauge_modify:
  5729   assumes "(\<forall>s. open s \<longrightarrow> open {x. f(x) \<in> s})" "gauge d"
  5730   shows "gauge (\<lambda>x. {y. f y \<in> d (f x)})"
  5731   using assms
  5732   unfolding gauge_def
  5733   apply safe
  5734   defer
  5735   apply (erule_tac x="f x" in allE)
  5736   apply (erule_tac x="d (f x)" in allE)
  5737   apply auto
  5738   done
  5739 
  5740 
  5741 subsection \<open>Only need trivial subintervals if the interval itself is trivial.\<close>
  5742 
  5743 lemma division_of_nontrivial:
  5744   fixes s :: "'a::euclidean_space set set"
  5745   assumes "s division_of (cbox a b)"
  5746     and "content (cbox a b) \<noteq> 0"
  5747   shows "{k. k \<in> s \<and> content k \<noteq> 0} division_of (cbox a b)"
  5748   using assms(1)
  5749   apply -
  5750 proof (induct "card s" arbitrary: s rule: nat_less_induct)
  5751   fix s::"'a set set"
  5752   assume assm: "s division_of (cbox a b)"
  5753     "\<forall>m<card s. \<forall>x. m = card x \<longrightarrow>
  5754       x division_of (cbox a b) \<longrightarrow> {k \<in> x. content k \<noteq> 0} division_of (cbox a b)"
  5755   note s = division_ofD[OF assm(1)]
  5756   let ?thesis = "{k \<in> s. content k \<noteq> 0} division_of (cbox a b)"
  5757   {
  5758     presume *: "{k \<in> s. content k \<noteq> 0} \<noteq> s \<Longrightarrow> ?thesis"
  5759     show ?thesis
  5760       apply cases
  5761       defer
  5762       apply (rule *)
  5763       apply assumption
  5764       using assm(1)
  5765       apply auto
  5766       done
  5767   }
  5768   assume noteq: "{k \<in> s. content k \<noteq> 0} \<noteq> s"
  5769   then obtain k where k: "k \<in> s" "content k = 0"
  5770     by auto
  5771   from s(4)[OF k(1)] guess c d by (elim exE) note k=k this
  5772   from k have "card s > 0"
  5773     unfolding card_gt_0_iff using assm(1) by auto
  5774   then have card: "card (s - {k}) < card s"
  5775     using assm(1) k(1)
  5776     apply (subst card_Diff_singleton_if)
  5777     apply auto
  5778     done
  5779   have *: "closed (\<Union>(s - {k}))"
  5780     apply (rule closed_Union)
  5781     defer
  5782     apply rule
  5783     apply (drule DiffD1,drule s(4))
  5784     using assm(1)
  5785     apply auto
  5786     done
  5787   have "k \<subseteq> \<Union>(s - {k})"
  5788     apply safe
  5789     apply (rule *[unfolded closed_limpt,rule_format])
  5790     unfolding islimpt_approachable
  5791   proof safe
  5792     fix x
  5793     fix e :: real
  5794     assume as: "x \<in> k" "e > 0"
  5795     from k(2)[unfolded k content_eq_0] guess i ..
  5796     then have i:"c\<bullet>i = d\<bullet>i" "i\<in>Basis"
  5797       using s(3)[OF k(1),unfolded k] unfolding box_ne_empty by auto
  5798     then have xi: "x\<bullet>i = d\<bullet>i"
  5799       using as unfolding k mem_box by (metis antisym)
  5800     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 +
  5801       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)"
  5802     show "\<exists>x'\<in>\<Union>(s - {k}). x' \<noteq> x \<and> dist x' x < e"
  5803       apply (rule_tac x=y in bexI)
  5804     proof
  5805       have "d \<in> cbox c d"
  5806         using s(3)[OF k(1)]
  5807         unfolding k box_eq_empty mem_box
  5808         by (fastforce simp add: not_less)
  5809       then have "d \<in> cbox a b"
  5810         using s(2)[OF k(1)]
  5811         unfolding k
  5812         by auto
  5813       note di = this[unfolded mem_box,THEN bspec[where x=i]]
  5814       then have xyi: "y\<bullet>i \<noteq> x\<bullet>i"
  5815         unfolding y_def i xi
  5816         using as(2) assms(2)[unfolded content_eq_0] i(2)
  5817         by (auto elim!: ballE[of _ _ i])
  5818       then show "y \<noteq> x"
  5819         unfolding euclidean_eq_iff[where 'a='a] using i by auto
  5820       have *: "Basis = insert i (Basis - {i})"
  5821         using i by auto
  5822       have "norm (y - x) < e + setsum (\<lambda>i. 0) Basis"
  5823         apply (rule le_less_trans[OF norm_le_l1])
  5824         apply (subst *)
  5825         apply (subst setsum.insert)
  5826         prefer 3
  5827         apply (rule add_less_le_mono)
  5828       proof -
  5829         show "\<bar>(y - x) \<bullet> i\<bar> < e"
  5830           using di as(2) y_def i xi by (auto simp: inner_simps)
  5831         show "(\<Sum>i\<in>Basis - {i}. \<bar>(y - x) \<bullet> i\<bar>) \<le> (\<Sum>i\<in>Basis. 0)"
  5832           unfolding y_def by (auto simp: inner_simps)
  5833       qed auto
  5834       then show "dist y x < e"
  5835         unfolding dist_norm by auto
  5836       have "y \<notin> k"
  5837         unfolding k mem_box
  5838         apply rule
  5839         apply (erule_tac x=i in ballE)
  5840         using xyi k i xi
  5841         apply auto
  5842         done
  5843       moreover
  5844       have "y \<in> \<Union>s"
  5845         using set_rev_mp[OF as(1) s(2)[OF k(1)]] as(2) di i
  5846         unfolding s mem_box y_def
  5847         by (auto simp: field_simps elim!: ballE[of _ _ i])
  5848       ultimately
  5849       show "y \<in> \<Union>(s - {k})" by auto
  5850     qed
  5851   qed
  5852   then have "\<Union>(s - {k}) = cbox a b"
  5853     unfolding s(6)[symmetric] by auto
  5854   then have  "{ka \<in> s - {k}. content ka \<noteq> 0} division_of (cbox a b)"
  5855     apply -
  5856     apply (rule assm(2)[rule_format,OF card refl])
  5857     apply (rule division_ofI)
  5858     defer
  5859     apply (rule_tac[1-4] s)
  5860     using assm(1)
  5861     apply auto
  5862     done
  5863   moreover
  5864   have "{ka \<in> s - {k}. content ka \<noteq> 0} = {k \<in> s. content k \<noteq> 0}"
  5865     using k by auto
  5866   ultimately show ?thesis by auto
  5867 qed
  5868 
  5869 
  5870 subsection \<open>Integrability on subintervals.\<close>
  5871 
  5872 lemma operative_integrable:
  5873   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5874   shows "comm_monoid.operative op \<and> True (\<lambda>i. f integrable_on i)"
  5875   unfolding comm_monoid.operative_def[OF comm_monoid_and]
  5876   apply safe
  5877   apply (subst integrable_on_def)
  5878   unfolding has_integral_null_eq
  5879   apply (rule, rule refl)
  5880   apply (rule, assumption, assumption)+
  5881   unfolding integrable_on_def
  5882   by (auto intro!: has_integral_split)
  5883 
  5884 lemma integrable_subinterval:
  5885   fixes f :: "'b::euclidean_space \<Rightarrow> 'a::banach"
  5886   assumes "f integrable_on cbox a b"
  5887     and "cbox c d \<subseteq> cbox a b"
  5888   shows "f integrable_on cbox c d"
  5889   apply (cases "cbox c d = {}")
  5890   defer
  5891   apply (rule partial_division_extend_1[OF assms(2)],assumption)
  5892   using comm_monoid_set.operative_division[OF comm_monoid_set_and operative_integrable,symmetric,of _ _ _ f] assms(1)
  5893   apply (auto simp: comm_monoid_set_F_and)
  5894   done
  5895 
  5896 lemma integrable_subinterval_real:
  5897   fixes f :: "real \<Rightarrow> 'a::banach"
  5898   assumes "f integrable_on {a .. b}"
  5899     and "{c .. d} \<subseteq> {a .. b}"
  5900   shows "f integrable_on {c .. d}"
  5901   by (metis assms(1) assms(2) box_real(2) integrable_subinterval)
  5902 
  5903 
  5904 subsection \<open>Combining adjacent intervals in 1 dimension.\<close>
  5905 
  5906 lemma has_integral_combine:
  5907   fixes a b c :: real
  5908   assumes "a \<le> c"
  5909     and "c \<le> b"
  5910     and "(f has_integral i) {a .. c}"
  5911     and "(f has_integral (j::'a::banach)) {c .. b}"
  5912   shows "(f has_integral (i + j)) {a .. b}"
  5913 proof -
  5914   interpret comm_monoid "lift_option plus" "Some (0::'a)"
  5915     by (rule comm_monoid_lift_option)
  5916       (rule add.comm_monoid_axioms)
  5917   note operative_integral [of f, unfolded operative_1_le]
  5918   note conjunctD2 [OF this, rule_format]
  5919   note * = this(2) [OF conjI [OF assms(1-2)],
  5920     unfolded if_P [OF assms(3)]]
  5921   then have "f integrable_on cbox a b"
  5922     apply -
  5923     apply (rule ccontr)
  5924     apply (subst(asm) if_P)
  5925     defer
  5926     apply (subst(asm) if_P)
  5927     using assms(3-)
  5928     apply auto
  5929     done
  5930   with *
  5931   show ?thesis
  5932     apply -
  5933     apply (subst(asm) if_P)
  5934     defer
  5935     apply (subst(asm) if_P)
  5936     defer
  5937     apply (subst(asm) if_P)
  5938     using assms(3-)
  5939     apply (auto simp add: integrable_on_def integral_unique)
  5940     done
  5941 qed
  5942 
  5943 lemma integral_combine:
  5944   fixes f :: "real \<Rightarrow> 'a::banach"
  5945   assumes "a \<le> c"
  5946     and "c \<le> b"
  5947     and "f integrable_on {a .. b}"
  5948   shows "integral {a .. c} f + integral {c .. b} f = integral {a .. b} f"
  5949   apply (rule integral_unique[symmetric])
  5950   apply (rule has_integral_combine[OF assms(1-2)])
  5951   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)
  5952   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)
  5953 
  5954 lemma integrable_combine:
  5955   fixes f :: "real \<Rightarrow> 'a::banach"
  5956   assumes "a \<le> c"