src/HOL/Multivariate_Analysis/Integration.thy
author wenzelm
Wed Dec 29 17:34:41 2010 +0100 (2010-12-29)
changeset 41413 64cd30d6b0b8
parent 40513 1204d268464f
child 41432 3214c39777ab
permissions -rw-r--r--
explicit file specifications -- avoid secondary load path;
     1 
     2 header {* Kurzweil-Henstock Gauge Integration in many dimensions. *}
     3 (*  Author:                     John Harrison
     4     Translation from HOL light: Robert Himmelmann, TU Muenchen *)
     5 
     6 theory Integration
     7 imports
     8   Derivative
     9   "~~/src/HOL/Decision_Procs/Dense_Linear_Order"
    10   "~~/src/HOL/Library/Indicator_Function"
    11 begin
    12 
    13 declare [[smt_certificates="Integration.certs"]]
    14 declare [[smt_fixed=true]]
    15 declare [[smt_oracle=false]]
    16 
    17 setup {* Arith_Data.add_tactic "Ferrante-Rackoff" (K FerranteRackoff.dlo_tac) *}
    18 
    19 (*declare not_less[simp] not_le[simp]*)
    20 
    21 lemmas scaleR_simps = scaleR_zero_left scaleR_minus_left scaleR_left_diff_distrib
    22   scaleR_zero_right scaleR_minus_right scaleR_right_diff_distrib scaleR_eq_0_iff
    23   scaleR_cancel_left scaleR_cancel_right scaleR.add_right scaleR.add_left real_vector_class.scaleR_one
    24 
    25 lemma real_arch_invD:
    26   "0 < (e::real) \<Longrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
    27   by(subst(asm) real_arch_inv)
    28 subsection {* Sundries *}
    29 
    30 (*declare basis_component[simp]*)
    31 
    32 lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
    33 lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
    34 lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
    35 lemma conjunctD5: assumes "a \<and> b \<and> c \<and> d \<and> e" shows a b c d e using assms by auto
    36 
    37 declare norm_triangle_ineq4[intro] 
    38 
    39 lemma simple_image: "{f x |x . x \<in> s} = f ` s" by blast
    40 
    41 lemma linear_simps:  assumes "bounded_linear f"
    42   shows "f (a + b) = f a + f b" "f (a - b) = f a - f b" "f 0 = 0" "f (- a) = - f a" "f (s *\<^sub>R v) = s *\<^sub>R (f v)"
    43   apply(rule_tac[!] additive.add additive.minus additive.diff additive.zero bounded_linear.scaleR)
    44   using assms unfolding bounded_linear_def additive_def by auto
    45 
    46 lemma bounded_linearI:assumes "\<And>x y. f (x + y) = f x + f y"
    47   "\<And>r x. f (r *\<^sub>R x) = r *\<^sub>R f x" "\<And>x. norm (f x) \<le> norm x * K"
    48   shows "bounded_linear f"
    49   unfolding bounded_linear_def additive_def bounded_linear_axioms_def using assms by auto
    50  
    51 lemma real_le_inf_subset:
    52   assumes "t \<noteq> {}" "t \<subseteq> s" "\<exists>b. b <=* s" shows "Inf s <= Inf (t::real set)"
    53   apply(rule isGlb_le_isLb) apply(rule Inf[OF assms(1)])
    54   using assms apply-apply(erule exE) apply(rule_tac x=b in exI)
    55   unfolding isLb_def setge_def by auto
    56 
    57 lemma real_ge_sup_subset:
    58   assumes "t \<noteq> {}" "t \<subseteq> s" "\<exists>b. s *<= b" shows "Sup s >= Sup (t::real set)"
    59   apply(rule isLub_le_isUb) apply(rule Sup[OF assms(1)])
    60   using assms apply-apply(erule exE) apply(rule_tac x=b in exI)
    61   unfolding isUb_def setle_def by auto
    62 
    63 lemma bounded_linear_component[intro]: "bounded_linear (\<lambda>x::'a::euclidean_space. x $$ k)"
    64   apply(rule bounded_linearI[where K=1]) 
    65   using component_le_norm[of _ k] unfolding real_norm_def by auto
    66 
    67 lemma transitive_stepwise_lt_eq:
    68   assumes "(\<And>x y z::nat. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z)"
    69   shows "((\<forall>m. \<forall>n>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n)))" (is "?l = ?r")
    70 proof(safe) assume ?r fix n m::nat assume "m < n" thus "R m n" apply-
    71   proof(induct n arbitrary: m) case (Suc n) show ?case 
    72     proof(cases "m < n") case True
    73       show ?thesis apply(rule assms[OF Suc(1)[OF True]]) using `?r` by auto
    74     next case False hence "m = n" using Suc(2) by auto
    75       thus ?thesis using `?r` by auto
    76     qed qed auto qed auto
    77 
    78 lemma transitive_stepwise_gt:
    79   assumes "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" "\<And>n. R n (Suc n) "
    80   shows "\<forall>n>m. R m n"
    81 proof- have "\<forall>m. \<forall>n>m. R m n" apply(subst transitive_stepwise_lt_eq)
    82     apply(rule assms) apply(assumption,assumption) using assms(2) by auto
    83   thus ?thesis by auto qed
    84 
    85 lemma transitive_stepwise_le_eq:
    86   assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
    87   shows "(\<forall>m. \<forall>n\<ge>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n))" (is "?l = ?r")
    88 proof safe assume ?r fix m n::nat assume "m\<le>n" thus "R m n" apply-
    89   proof(induct n arbitrary: m) case (Suc n) show ?case 
    90     proof(cases "m \<le> n") case True show ?thesis apply(rule assms(2))
    91         apply(rule Suc(1)[OF True]) using `?r` by auto
    92     next case False hence "m = Suc n" using Suc(2) by auto
    93       thus ?thesis using assms(1) by auto
    94     qed qed(insert assms(1), auto) qed auto
    95 
    96 lemma transitive_stepwise_le:
    97   assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" "\<And>n. R n (Suc n) "
    98   shows "\<forall>n\<ge>m. R m n"
    99 proof- have "\<forall>m. \<forall>n\<ge>m. R m n" apply(subst transitive_stepwise_le_eq)
   100     apply(rule assms) apply(rule assms,assumption,assumption) using assms(3) by auto
   101   thus ?thesis by auto qed
   102 
   103 subsection {* Some useful lemmas about intervals. *}
   104 
   105 abbreviation One  where "One \<equiv> ((\<chi>\<chi> i. 1)::_::ordered_euclidean_space)"
   106 
   107 lemma empty_as_interval: "{} = {One..0}"
   108   apply(rule set_eqI,rule) defer unfolding mem_interval
   109   using UNIV_witness[where 'a='n] apply(erule_tac exE,rule_tac x=x in allE) by auto
   110 
   111 lemma interior_subset_union_intervals: 
   112   assumes "i = {a..b::'a::ordered_euclidean_space}" "j = {c..d}" "interior j \<noteq> {}" "i \<subseteq> j \<union> s" "interior(i) \<inter> interior(j) = {}"
   113   shows "interior i \<subseteq> interior s" proof-
   114   have "{a<..<b} \<inter> {c..d} = {}" using inter_interval_mixed_eq_empty[of c d a b] and assms(3,5)
   115     unfolding assms(1,2) interior_closed_interval by auto
   116   moreover have "{a<..<b} \<subseteq> {c..d} \<union> s" apply(rule order_trans,rule interval_open_subset_closed)
   117     using assms(4) unfolding assms(1,2) by auto
   118   ultimately show ?thesis apply-apply(rule interior_maximal) defer apply(rule open_interior)
   119     unfolding assms(1,2) interior_closed_interval by auto qed
   120 
   121 lemma inter_interior_unions_intervals: fixes f::"('a::ordered_euclidean_space) set set"
   122   assumes "finite f" "open s" "\<forall>t\<in>f. \<exists>a b. t = {a..b}" "\<forall>t\<in>f. s \<inter> (interior t) = {}"
   123   shows "s \<inter> interior(\<Union>f) = {}" proof(rule ccontr,unfold ex_in_conv[THEN sym]) case goal1
   124   have lem1:"\<And>x e s U. ball x e \<subseteq> s \<inter> interior U \<longleftrightarrow> ball x e \<subseteq> s \<inter> U" apply rule  defer apply(rule_tac Int_greatest)
   125     unfolding open_subset_interior[OF open_ball]  using interior_subset by auto
   126   have lem2:"\<And>x s P. \<exists>x\<in>s. P x \<Longrightarrow> \<exists>x\<in>insert x s. P x" by auto
   127   have "\<And>f. finite f \<Longrightarrow> (\<forall>t\<in>f. \<exists>a b. t = {a..b}) \<Longrightarrow> (\<exists>x. x \<in> s \<inter> interior (\<Union>f)) \<Longrightarrow> (\<exists>t\<in>f. \<exists>x. \<exists>e>0. ball x e \<subseteq> s \<inter> t)" proof- case goal1
   128   thus ?case proof(induct rule:finite_induct) 
   129     case empty from this(2) guess x .. hence False unfolding Union_empty interior_empty by auto thus ?case by auto next
   130     case (insert i f) guess x using insert(5) .. note x = this
   131     then guess e unfolding open_contains_ball_eq[OF open_Int[OF assms(2) open_interior],rule_format] .. note e=this
   132     guess a using insert(4)[rule_format,OF insertI1] .. then guess b .. note ab = this
   133     show ?case proof(cases "x\<in>i") case False hence "x \<in> UNIV - {a..b}" unfolding ab by auto
   134       then guess d unfolding open_contains_ball_eq[OF open_Diff[OF open_UNIV closed_interval],rule_format] ..
   135       hence "0 < d" "ball x (min d e) \<subseteq> UNIV - i" unfolding ab ball_min_Int by auto
   136       hence "ball x (min d e) \<subseteq> s \<inter> interior (\<Union>f)" using e unfolding lem1 unfolding  ball_min_Int by auto
   137       hence "x \<in> s \<inter> interior (\<Union>f)" using `d>0` e by auto
   138       hence "\<exists>t\<in>f. \<exists>x e. 0 < e \<and> ball x e \<subseteq> s \<inter> t" apply-apply(rule insert(3)) using insert(4) by auto thus ?thesis by auto next
   139     case True show ?thesis proof(cases "x\<in>{a<..<b}")
   140       case True then guess d unfolding open_contains_ball_eq[OF open_interval,rule_format] ..
   141       thus ?thesis apply(rule_tac x=i in bexI,rule_tac x=x in exI,rule_tac x="min d e" in exI)
   142 	unfolding ab using interval_open_subset_closed[of a b] and e by fastsimp+ next
   143     case False then obtain k where "x$$k \<le> a$$k \<or> x$$k \<ge> b$$k" and k:"k<DIM('a)" unfolding mem_interval by(auto simp add:not_less) 
   144     hence "x$$k = a$$k \<or> x$$k = b$$k" using True unfolding ab and mem_interval apply(erule_tac x=k in allE) by auto
   145     hence "\<exists>x. ball x (e/2) \<subseteq> s \<inter> (\<Union>f)" proof(erule_tac disjE)
   146       let ?z = "x - (e/2) *\<^sub>R basis k" assume as:"x$$k = a$$k" have "ball ?z (e / 2) \<inter> i = {}" apply(rule ccontr) unfolding ex_in_conv[THEN sym] proof(erule exE)
   147 	fix y assume "y \<in> ball ?z (e / 2) \<inter> i" hence "dist ?z y < e/2" and yi:"y\<in>i" by auto
   148 	hence "\<bar>(?z - y) $$ k\<bar> < e/2" using component_le_norm[of "?z - y" k] unfolding dist_norm by auto
   149 	hence "y$$k < a$$k" using e[THEN conjunct1] k by(auto simp add:field_simps basis_component as)
   150 	hence "y \<notin> i" unfolding ab mem_interval not_all apply(rule_tac x=k in exI) using k by auto thus False using yi by auto qed
   151       moreover have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)" apply(rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]]) proof
   152 	fix y assume as:"y\<in> ball ?z (e/2)" have "norm (x - y) \<le> \<bar>e\<bar> / 2 + norm (x - y - (e / 2) *\<^sub>R basis k)"
   153 	   apply-apply(rule order_trans,rule norm_triangle_sub[of "x - y" "(e/2) *\<^sub>R basis k"])
   154 	  unfolding norm_scaleR norm_basis by auto
   155 	also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2" apply(rule add_strict_left_mono) using as unfolding mem_ball dist_norm using e by(auto simp add:field_simps)
   156 	finally show "y\<in>ball x e" unfolding mem_ball dist_norm using e by(auto simp add:field_simps) qed
   157       ultimately show ?thesis apply(rule_tac x="?z" in exI) unfolding Union_insert by auto
   158     next let ?z = "x + (e/2) *\<^sub>R basis k" assume as:"x$$k = b$$k" have "ball ?z (e / 2) \<inter> i = {}" apply(rule ccontr) unfolding ex_in_conv[THEN sym] proof(erule exE)
   159 	fix y assume "y \<in> ball ?z (e / 2) \<inter> i" hence "dist ?z y < e/2" and yi:"y\<in>i" by auto
   160 	hence "\<bar>(?z - y) $$ k\<bar> < e/2" using component_le_norm[of "?z - y" k] unfolding dist_norm by auto
   161 	hence "y$$k > b$$k" using e[THEN conjunct1] k by(auto simp add:field_simps as)
   162 	hence "y \<notin> i" unfolding ab mem_interval not_all using k by(rule_tac x=k in exI,auto) thus False using yi by auto qed
   163       moreover have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)" apply(rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]]) proof
   164 	fix y assume as:"y\<in> ball ?z (e/2)" have "norm (x - y) \<le> \<bar>e\<bar> / 2 + norm (x - y + (e / 2) *\<^sub>R basis k)"
   165 	   apply-apply(rule order_trans,rule norm_triangle_sub[of "x - y" "- (e/2) *\<^sub>R basis k"])
   166 	  unfolding norm_scaleR by auto
   167 	also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2" apply(rule add_strict_left_mono) using as unfolding mem_ball dist_norm using e by(auto simp add:field_simps)
   168 	finally show "y\<in>ball x e" unfolding mem_ball dist_norm using e by(auto simp add:field_simps) qed
   169       ultimately show ?thesis apply(rule_tac x="?z" in exI) unfolding Union_insert by auto qed 
   170     then guess x .. hence "x \<in> s \<inter> interior (\<Union>f)" unfolding lem1[where U="\<Union>f",THEN sym] using centre_in_ball e[THEN conjunct1] by auto
   171     thus ?thesis apply-apply(rule lem2,rule insert(3)) using insert(4) by auto qed qed qed qed note * = this
   172   guess t using *[OF assms(1,3) goal1]  .. from this(2) guess x .. then guess e ..
   173   hence "x \<in> s" "x\<in>interior t" defer using open_subset_interior[OF open_ball, of x e t] by auto
   174   thus False using `t\<in>f` assms(4) by auto qed
   175 
   176 subsection {* Bounds on intervals where they exist. *}
   177 
   178 definition "interval_upperbound (s::('a::ordered_euclidean_space) set) = ((\<chi>\<chi> i. Sup {a. \<exists>x\<in>s. x$$i = a})::'a)"
   179 
   180 definition "interval_lowerbound (s::('a::ordered_euclidean_space) set) = ((\<chi>\<chi> i. Inf {a. \<exists>x\<in>s. x$$i = a})::'a)"
   181 
   182 lemma interval_upperbound[simp]: assumes "\<forall>i<DIM('a::ordered_euclidean_space). a$$i \<le> (b::'a)$$i" shows "interval_upperbound {a..b} = b"
   183   using assms unfolding interval_upperbound_def apply(subst euclidean_eq[where 'a='a]) apply safe
   184   unfolding euclidean_lambda_beta' apply(erule_tac x=i in allE)
   185   apply(rule Sup_unique) unfolding setle_def apply rule unfolding mem_Collect_eq apply(erule bexE) unfolding mem_interval defer
   186   apply(rule,rule) apply(rule_tac x="b$$i" in bexI) defer unfolding mem_Collect_eq apply(rule_tac x=b in bexI)
   187   unfolding mem_interval using assms by auto
   188 
   189 lemma interval_lowerbound[simp]: assumes "\<forall>i<DIM('a::ordered_euclidean_space). a$$i \<le> (b::'a)$$i" shows "interval_lowerbound {a..b} = a"
   190   using assms unfolding interval_lowerbound_def apply(subst euclidean_eq[where 'a='a]) apply safe
   191   unfolding euclidean_lambda_beta' apply(erule_tac x=i in allE)
   192   apply(rule Inf_unique) unfolding setge_def apply rule unfolding mem_Collect_eq apply(erule bexE) unfolding mem_interval defer
   193   apply(rule,rule) apply(rule_tac x="a$$i" in bexI) defer unfolding mem_Collect_eq apply(rule_tac x=a in bexI)
   194   unfolding mem_interval using assms by auto 
   195 
   196 lemmas interval_bounds = interval_upperbound interval_lowerbound
   197 
   198 lemma interval_bounds'[simp]: assumes "{a..b}\<noteq>{}" shows "interval_upperbound {a..b} = b" "interval_lowerbound {a..b} = a"
   199   using assms unfolding interval_ne_empty by auto
   200 
   201 subsection {* Content (length, area, volume...) of an interval. *}
   202 
   203 definition "content (s::('a::ordered_euclidean_space) set) =
   204        (if s = {} then 0 else (\<Prod>i<DIM('a). (interval_upperbound s)$$i - (interval_lowerbound s)$$i))"
   205 
   206 lemma interval_not_empty:"\<forall>i<DIM('a). a$$i \<le> b$$i \<Longrightarrow> {a..b::'a::ordered_euclidean_space} \<noteq> {}"
   207   unfolding interval_eq_empty unfolding not_ex not_less by auto
   208 
   209 lemma content_closed_interval: fixes a::"'a::ordered_euclidean_space" assumes "\<forall>i<DIM('a). a$$i \<le> b$$i"
   210   shows "content {a..b} = (\<Prod>i<DIM('a). b$$i - a$$i)"
   211   using interval_not_empty[OF assms] unfolding content_def interval_upperbound[OF assms] interval_lowerbound[OF assms] by auto
   212 
   213 lemma content_closed_interval': fixes a::"'a::ordered_euclidean_space" assumes "{a..b}\<noteq>{}" shows "content {a..b} = (\<Prod>i<DIM('a). b$$i - a$$i)"
   214   apply(rule content_closed_interval) using assms unfolding interval_ne_empty .
   215 
   216 lemma content_real:assumes "a\<le>b" shows "content {a..b} = b-a"
   217 proof- have *:"{..<Suc 0} = {0}" by auto
   218   show ?thesis unfolding content_def using assms by(auto simp: *)
   219 qed
   220 
   221 lemma content_unit[intro]: "content{0..One::'a::ordered_euclidean_space} = 1" proof-
   222   have *:"\<forall>i<DIM('a). (0::'a)$$i \<le> (One::'a)$$i" by auto
   223   have "0 \<in> {0..One::'a}" unfolding mem_interval by auto
   224   thus ?thesis unfolding content_def interval_bounds[OF *] using setprod_1 by auto qed
   225 
   226 lemma content_pos_le[intro]: fixes a::"'a::ordered_euclidean_space" shows "0 \<le> content {a..b}" proof(cases "{a..b}={}")
   227   case False hence *:"\<forall>i<DIM('a). a $$ i \<le> b $$ i" unfolding interval_ne_empty by assumption
   228   have "(\<Prod>i<DIM('a). interval_upperbound {a..b} $$ i - interval_lowerbound {a..b} $$ i) \<ge> 0"
   229     apply(rule setprod_nonneg) unfolding interval_bounds[OF *] using * apply(erule_tac x=x in allE) by auto
   230   thus ?thesis unfolding content_def by(auto simp del:interval_bounds') qed(unfold content_def, auto)
   231 
   232 lemma content_pos_lt: fixes a::"'a::ordered_euclidean_space" assumes "\<forall>i<DIM('a). a$$i < b$$i" shows "0 < content {a..b}"
   233 proof- have help_lemma1: "\<forall>i<DIM('a). a$$i < b$$i \<Longrightarrow> \<forall>i<DIM('a). a$$i \<le> ((b$$i)::real)" apply(rule,erule_tac x=i in allE) by auto
   234   show ?thesis unfolding content_closed_interval[OF help_lemma1[OF assms]] apply(rule setprod_pos)
   235     using assms apply(erule_tac x=x in allE) by auto qed
   236 
   237 lemma content_eq_0: "content{a..b::'a::ordered_euclidean_space} = 0 \<longleftrightarrow> (\<exists>i<DIM('a). b$$i \<le> a$$i)" proof(cases "{a..b} = {}")
   238   case True thus ?thesis unfolding content_def if_P[OF True] unfolding interval_eq_empty apply-
   239     apply(rule,erule exE) apply(rule_tac x=i in exI) by auto next
   240   case False note this[unfolded interval_eq_empty not_ex not_less]
   241   hence as:"\<forall>i<DIM('a). b $$ i \<ge> a $$ i" by fastsimp
   242   show ?thesis unfolding content_def if_not_P[OF False] setprod_zero_iff[OF finite_lessThan]
   243     apply(rule) apply(erule_tac[!] exE bexE) unfolding interval_bounds[OF as] apply(rule_tac x=x in exI) defer
   244     apply(rule_tac x=i in bexI) using as apply(erule_tac x=i in allE) by auto qed
   245 
   246 lemma cond_cases:"(P \<Longrightarrow> Q x) \<Longrightarrow> (\<not> P \<Longrightarrow> Q y) \<Longrightarrow> Q (if P then x else y)" by auto
   247 
   248 lemma content_closed_interval_cases:
   249   "content {a..b::'a::ordered_euclidean_space} = (if \<forall>i<DIM('a). a$$i \<le> b$$i then setprod (\<lambda>i. b$$i - a$$i) {..<DIM('a)} else 0)" apply(rule cond_cases) 
   250   apply(rule content_closed_interval) unfolding content_eq_0 not_all not_le defer apply(erule exE,rule_tac x=x in exI) by auto
   251 
   252 lemma content_eq_0_interior: "content {a..b} = 0 \<longleftrightarrow> interior({a..b}) = {}"
   253   unfolding content_eq_0 interior_closed_interval interval_eq_empty by auto
   254 
   255 (*lemma content_eq_0_1: "content {a..b::real^1} = 0 \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a"
   256   unfolding content_eq_0 by auto*)
   257 
   258 lemma content_pos_lt_eq: "0 < content {a..b::'a::ordered_euclidean_space} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i < b$$i)"
   259   apply(rule) defer apply(rule content_pos_lt,assumption) proof- assume "0 < content {a..b}"
   260   hence "content {a..b} \<noteq> 0" by auto thus "\<forall>i<DIM('a). a$$i < b$$i" unfolding content_eq_0 not_ex not_le by fastsimp qed
   261 
   262 lemma content_empty[simp]: "content {} = 0" unfolding content_def by auto
   263 
   264 lemma content_subset: assumes "{a..b} \<subseteq> {c..d}" shows "content {a..b::'a::ordered_euclidean_space} \<le> content {c..d}" proof(cases "{a..b}={}")
   265   case True thus ?thesis using content_pos_le[of c d] by auto next
   266   case False hence ab_ne:"\<forall>i<DIM('a). a $$ i \<le> b $$ i" unfolding interval_ne_empty by auto
   267   hence ab_ab:"a\<in>{a..b}" "b\<in>{a..b}" unfolding mem_interval by auto
   268   have "{c..d} \<noteq> {}" using assms False by auto
   269   hence cd_ne:"\<forall>i<DIM('a). c $$ i \<le> d $$ i" using assms unfolding interval_ne_empty by auto
   270   show ?thesis unfolding content_def unfolding interval_bounds[OF ab_ne] interval_bounds[OF cd_ne]
   271     unfolding if_not_P[OF False] if_not_P[OF `{c..d} \<noteq> {}`] apply(rule setprod_mono,rule) proof
   272     fix i assume i:"i\<in>{..<DIM('a)}"
   273     show "0 \<le> b $$ i - a $$ i" using ab_ne[THEN spec[where x=i]] i by auto
   274     show "b $$ i - a $$ i \<le> d $$ i - c $$ i"
   275       using assms[unfolded subset_eq mem_interval,rule_format,OF ab_ab(2),of i]
   276       using assms[unfolded subset_eq mem_interval,rule_format,OF ab_ab(1),of i] using i by auto qed qed
   277 
   278 lemma content_lt_nz: "0 < content {a..b} \<longleftrightarrow> content {a..b} \<noteq> 0"
   279   unfolding content_pos_lt_eq content_eq_0 unfolding not_ex not_le by fastsimp
   280 
   281 subsection {* The notion of a gauge --- simply an open set containing the point. *}
   282 
   283 definition gauge where "gauge d \<longleftrightarrow> (\<forall>x. x\<in>(d x) \<and> open(d x))"
   284 
   285 lemma gaugeI:assumes "\<And>x. x\<in>g x" "\<And>x. open (g x)" shows "gauge g"
   286   using assms unfolding gauge_def by auto
   287 
   288 lemma gaugeD[dest]: assumes "gauge d" shows "x\<in>d x" "open (d x)" using assms unfolding gauge_def by auto
   289 
   290 lemma gauge_ball_dependent: "\<forall>x. 0 < e x \<Longrightarrow> gauge (\<lambda>x. ball x (e x))"
   291   unfolding gauge_def by auto 
   292 
   293 lemma gauge_ball[intro]: "0 < e \<Longrightarrow> gauge (\<lambda>x. ball x e)" unfolding gauge_def by auto 
   294 
   295 lemma gauge_trivial[intro]: "gauge (\<lambda>x. ball x 1)" apply(rule gauge_ball) by auto
   296 
   297 lemma gauge_inter[intro]: "gauge d1 \<Longrightarrow> gauge d2 \<Longrightarrow> gauge (\<lambda>x. (d1 x) \<inter> (d2 x))"
   298   unfolding gauge_def by auto 
   299 
   300 lemma gauge_inters: assumes "finite s" "\<forall>d\<in>s. gauge (f d)" shows "gauge(\<lambda>x. \<Inter> {f d x | d. d \<in> s})" proof-
   301   have *:"\<And>x. {f d x |d. d \<in> s} = (\<lambda>d. f d x) ` s" by auto show ?thesis
   302   unfolding gauge_def unfolding * 
   303   using assms unfolding Ball_def Inter_iff mem_Collect_eq gauge_def by auto qed
   304 
   305 lemma gauge_existence_lemma: "(\<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)" by(meson zero_less_one)
   306 
   307 subsection {* Divisions. *}
   308 
   309 definition division_of (infixl "division'_of" 40) where
   310   "s division_of i \<equiv>
   311         finite s \<and>
   312         (\<forall>k\<in>s. k \<subseteq> i \<and> k \<noteq> {} \<and> (\<exists>a b. k = {a..b})) \<and>
   313         (\<forall>k1\<in>s. \<forall>k2\<in>s. k1 \<noteq> k2 \<longrightarrow> interior(k1) \<inter> interior(k2) = {}) \<and>
   314         (\<Union>s = i)"
   315 
   316 lemma division_ofD[dest]: assumes  "s division_of i"
   317   shows"finite s" "\<And>k. k\<in>s \<Longrightarrow> k \<subseteq> i" "\<And>k. k\<in>s \<Longrightarrow>  k \<noteq> {}" "\<And>k. k\<in>s \<Longrightarrow> (\<exists>a b. k = {a..b})"
   318   "\<And>k1 k2. \<lbrakk>k1\<in>s; k2\<in>s; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}" "\<Union>s = i" using assms unfolding division_of_def by auto
   319 
   320 lemma division_ofI:
   321   assumes "finite s" "\<And>k. k\<in>s \<Longrightarrow> k \<subseteq> i" "\<And>k. k\<in>s \<Longrightarrow>  k \<noteq> {}" "\<And>k. k\<in>s \<Longrightarrow> (\<exists>a b. k = {a..b})"
   322   "\<And>k1 k2. \<lbrakk>k1\<in>s; k2\<in>s; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}" "\<Union>s = i"
   323   shows "s division_of i" using assms unfolding division_of_def by auto
   324 
   325 lemma division_of_finite: "s division_of i \<Longrightarrow> finite s"
   326   unfolding division_of_def by auto
   327 
   328 lemma division_of_self[intro]: "{a..b} \<noteq> {} \<Longrightarrow> {{a..b}} division_of {a..b}"
   329   unfolding division_of_def by auto
   330 
   331 lemma division_of_trivial[simp]: "s division_of {} \<longleftrightarrow> s = {}" unfolding division_of_def by auto 
   332 
   333 lemma division_of_sing[simp]: "s division_of {a..a::'a::ordered_euclidean_space} \<longleftrightarrow> s = {{a..a}}" (is "?l = ?r") proof
   334   assume ?r moreover { assume "s = {{a}}" moreover fix k assume "k\<in>s" 
   335     ultimately have"\<exists>x y. k = {x..y}" apply(rule_tac x=a in exI)+ unfolding interval_sing by auto }
   336   ultimately show ?l unfolding division_of_def interval_sing by auto next
   337   assume ?l note as=conjunctD4[OF this[unfolded division_of_def interval_sing]]
   338   { fix x assume x:"x\<in>s" have "x={a}" using as(2)[rule_format,OF x] by auto }
   339   moreover have "s \<noteq> {}" using as(4) by auto ultimately show ?r unfolding interval_sing by auto qed
   340 
   341 lemma elementary_empty: obtains p where "p division_of {}"
   342   unfolding division_of_trivial by auto
   343 
   344 lemma elementary_interval: obtains p where  "p division_of {a..b}"
   345   by(metis division_of_trivial division_of_self)
   346 
   347 lemma division_contains: "s division_of i \<Longrightarrow> \<forall>x\<in>i. \<exists>k\<in>s. x \<in> k"
   348   unfolding division_of_def by auto
   349 
   350 lemma forall_in_division:
   351  "d division_of i \<Longrightarrow> ((\<forall>x\<in>d. P x) \<longleftrightarrow> (\<forall>a b. {a..b} \<in> d \<longrightarrow> P {a..b}))"
   352   unfolding division_of_def by fastsimp
   353 
   354 lemma division_of_subset: assumes "p division_of (\<Union>p)" "q \<subseteq> p" shows "q division_of (\<Union>q)"
   355   apply(rule division_ofI) proof- note as=division_ofD[OF assms(1)]
   356   show "finite q" apply(rule finite_subset) using as(1) assms(2) by auto
   357   { fix k assume "k \<in> q" hence kp:"k\<in>p" using assms(2) by auto show "k\<subseteq>\<Union>q" using `k \<in> q` by auto
   358   show "\<exists>a b. k = {a..b}" using as(4)[OF kp] by auto show "k \<noteq> {}" using as(3)[OF kp] by auto }
   359   fix k1 k2 assume "k1 \<in> q" "k2 \<in> q" "k1 \<noteq> k2" hence *:"k1\<in>p" "k2\<in>p" "k1\<noteq>k2" using assms(2) by auto
   360   show "interior k1 \<inter> interior k2 = {}" using as(5)[OF *] by auto qed auto
   361 
   362 lemma division_of_union_self[intro]: "p division_of s \<Longrightarrow> p division_of (\<Union>p)" unfolding division_of_def by auto
   363 
   364 lemma division_of_content_0: assumes "content {a..b} = 0" "d division_of {a..b}" shows "\<forall>k\<in>d. content k = 0"
   365   unfolding forall_in_division[OF assms(2)] apply(rule,rule,rule) apply(drule division_ofD(2)[OF assms(2)])
   366   apply(drule content_subset) unfolding assms(1) proof- case goal1 thus ?case using content_pos_le[of a b] by auto qed
   367 
   368 lemma division_inter: assumes "p1 division_of s1" "p2 division_of (s2::('a::ordered_euclidean_space) set)"
   369   shows "{k1 \<inter> k2 | k1 k2 .k1 \<in> p1 \<and> k2 \<in> p2 \<and> k1 \<inter> k2 \<noteq> {}} division_of (s1 \<inter> s2)" (is "?A' division_of _") proof-
   370 let ?A = "{s. s \<in>  (\<lambda>(k1,k2). k1 \<inter> k2) ` (p1 \<times> p2) \<and> s \<noteq> {}}" have *:"?A' = ?A" by auto
   371 show ?thesis unfolding * proof(rule division_ofI) have "?A \<subseteq> (\<lambda>(x, y). x \<inter> y) ` (p1 \<times> p2)" by auto
   372   moreover have "finite (p1 \<times> p2)" using assms unfolding division_of_def by auto ultimately show "finite ?A" by auto
   373   have *:"\<And>s. \<Union>{x\<in>s. x \<noteq> {}} = \<Union>s" by auto show "\<Union>?A = s1 \<inter> s2" apply(rule set_eqI) unfolding * and Union_image_eq UN_iff
   374     using division_ofD(6)[OF assms(1)] and division_ofD(6)[OF assms(2)] by auto
   375   { fix k assume "k\<in>?A" then obtain k1 k2 where k:"k = k1 \<inter> k2" "k1\<in>p1" "k2\<in>p2" "k\<noteq>{}" by auto thus "k \<noteq> {}" by auto
   376   show "k \<subseteq> s1 \<inter> s2" using division_ofD(2)[OF assms(1) k(2)] and division_ofD(2)[OF assms(2) k(3)] unfolding k by auto
   377   guess a1 using division_ofD(4)[OF assms(1) k(2)] .. then guess b1 .. note ab1=this
   378   guess a2 using division_ofD(4)[OF assms(2) k(3)] .. then guess b2 .. note ab2=this
   379   show "\<exists>a b. k = {a..b}" unfolding k ab1 ab2 unfolding inter_interval by auto } fix k1 k2
   380   assume "k1\<in>?A" then obtain x1 y1 where k1:"k1 = x1 \<inter> y1" "x1\<in>p1" "y1\<in>p2" "k1\<noteq>{}" by auto
   381   assume "k2\<in>?A" then obtain x2 y2 where k2:"k2 = x2 \<inter> y2" "x2\<in>p1" "y2\<in>p2" "k2\<noteq>{}" by auto
   382   assume "k1 \<noteq> k2" hence th:"x1\<noteq>x2 \<or> y1\<noteq>y2" unfolding k1 k2 by auto
   383   have *:"(interior x1 \<inter> interior x2 = {} \<or> interior y1 \<inter> interior y2 = {}) \<Longrightarrow>
   384       interior(x1 \<inter> y1) \<subseteq> interior(x1) \<Longrightarrow> interior(x1 \<inter> y1) \<subseteq> interior(y1) \<Longrightarrow>
   385       interior(x2 \<inter> y2) \<subseteq> interior(x2) \<Longrightarrow> interior(x2 \<inter> y2) \<subseteq> interior(y2)
   386       \<Longrightarrow> interior(x1 \<inter> y1) \<inter> interior(x2 \<inter> y2) = {}" by auto
   387   show "interior k1 \<inter> interior k2 = {}" unfolding k1 k2 apply(rule *) defer apply(rule_tac[1-4] subset_interior)
   388     using division_ofD(5)[OF assms(1) k1(2) k2(2)]
   389     using division_ofD(5)[OF assms(2) k1(3) k2(3)] using th by auto qed qed
   390 
   391 lemma division_inter_1: assumes "d division_of i" "{a..b::'a::ordered_euclidean_space} \<subseteq> i"
   392   shows "{ {a..b} \<inter> k |k. k \<in> d \<and> {a..b} \<inter> k \<noteq> {} } division_of {a..b}" proof(cases "{a..b} = {}")
   393   case True show ?thesis unfolding True and division_of_trivial by auto next
   394   have *:"{a..b} \<inter> i = {a..b}" using assms(2) by auto 
   395   case False show ?thesis using division_inter[OF division_of_self[OF False] assms(1)] unfolding * by auto qed
   396 
   397 lemma elementary_inter: assumes "p1 division_of s" "p2 division_of (t::('a::ordered_euclidean_space) set)"
   398   shows "\<exists>p. p division_of (s \<inter> t)"
   399   by(rule,rule division_inter[OF assms])
   400 
   401 lemma elementary_inters: assumes "finite f" "f\<noteq>{}" "\<forall>s\<in>f. \<exists>p. p division_of (s::('a::ordered_euclidean_space) set)"
   402   shows "\<exists>p. p division_of (\<Inter> f)" using assms apply-proof(induct f rule:finite_induct)
   403 case (insert x f) show ?case proof(cases "f={}")
   404   case True thus ?thesis unfolding True using insert by auto next
   405   case False guess p using insert(3)[OF False insert(5)[unfolded ball_simps,THEN conjunct2]] ..
   406   moreover guess px using insert(5)[rule_format,OF insertI1] .. ultimately
   407   show ?thesis unfolding Inter_insert apply(rule_tac elementary_inter) by assumption+ qed qed auto
   408 
   409 lemma division_disjoint_union:
   410   assumes "p1 division_of s1" "p2 division_of s2" "interior s1 \<inter> interior s2 = {}"
   411   shows "(p1 \<union> p2) division_of (s1 \<union> s2)" proof(rule division_ofI) 
   412   note d1 = division_ofD[OF assms(1)] and d2 = division_ofD[OF assms(2)]
   413   show "finite (p1 \<union> p2)" using d1(1) d2(1) by auto
   414   show "\<Union>(p1 \<union> p2) = s1 \<union> s2" using d1(6) d2(6) by auto
   415   { fix k1 k2 assume as:"k1 \<in> p1 \<union> p2" "k2 \<in> p1 \<union> p2" "k1 \<noteq> k2" moreover let ?g="interior k1 \<inter> interior k2 = {}"
   416   { assume as:"k1\<in>p1" "k2\<in>p2" have ?g using subset_interior[OF d1(2)[OF as(1)]] subset_interior[OF d2(2)[OF as(2)]]
   417       using assms(3) by blast } moreover
   418   { assume as:"k1\<in>p2" "k2\<in>p1" have ?g using subset_interior[OF d1(2)[OF as(2)]] subset_interior[OF d2(2)[OF as(1)]]
   419       using assms(3) by blast} ultimately
   420   show ?g using d1(5)[OF _ _ as(3)] and d2(5)[OF _ _ as(3)] by auto }
   421   fix k assume k:"k \<in> p1 \<union> p2"  show "k \<subseteq> s1 \<union> s2" using k d1(2) d2(2) by auto
   422   show "k \<noteq> {}" using k d1(3) d2(3) by auto show "\<exists>a b. k = {a..b}" using k d1(4) d2(4) by auto qed
   423 
   424 (* move *)
   425 lemma Eucl_nth_inverse[simp]: fixes x::"'a::euclidean_space" shows "(\<chi>\<chi> i. x $$ i) = x"
   426   apply(subst euclidean_eq) by auto
   427 
   428 lemma partial_division_extend_1:
   429   assumes "{c..d} \<subseteq> {a..b::'a::ordered_euclidean_space}" "{c..d} \<noteq> {}"
   430   obtains p where "p division_of {a..b}" "{c..d} \<in> p"
   431 proof- def n \<equiv> "DIM('a)" have n:"1 \<le> n" "0 < n" "n \<noteq> 0" unfolding n_def using DIM_positive[where 'a='a] by auto
   432   guess \<pi> using ex_bij_betw_nat_finite_1[OF finite_lessThan[of "DIM('a)"]] .. note \<pi>=this
   433   def \<pi>' \<equiv> "inv_into {1..n} \<pi>"
   434   have \<pi>':"bij_betw \<pi>' {..<DIM('a)} {1..n}" using bij_betw_inv_into[OF \<pi>] unfolding \<pi>'_def n_def by auto
   435   hence \<pi>'i:"\<And>i. i<DIM('a) \<Longrightarrow> \<pi>' i \<in> {1..n}" unfolding bij_betw_def by auto 
   436   have \<pi>i:"\<And>i. i\<in>{1..n} \<Longrightarrow> \<pi> i <DIM('a)" using \<pi> unfolding bij_betw_def n_def by auto 
   437   have \<pi>\<pi>'[simp]:"\<And>i. i<DIM('a) \<Longrightarrow> \<pi> (\<pi>' i) = i" unfolding \<pi>'_def
   438     apply(rule f_inv_into_f) unfolding n_def using \<pi> unfolding bij_betw_def by auto
   439   have \<pi>'\<pi>[simp]:"\<And>i. i\<in>{1..n} \<Longrightarrow> \<pi>' (\<pi> i) = i" unfolding \<pi>'_def apply(rule inv_into_f_eq)
   440     using \<pi> unfolding n_def bij_betw_def by auto
   441   have "{c..d} \<noteq> {}" using assms by auto
   442   let ?p1 = "\<lambda>l. {(\<chi>\<chi> i. if \<pi>' i < l then c$$i else a$$i)::'a .. (\<chi>\<chi> i. if \<pi>' i < l then d$$i else if \<pi>' i = l then c$$\<pi> l else b$$i)}"
   443   let ?p2 = "\<lambda>l. {(\<chi>\<chi> i. if \<pi>' i < l then c$$i else if \<pi>' i = l then d$$\<pi> l else a$$i)::'a .. (\<chi>\<chi> i. if \<pi>' i < l then d$$i else b$$i)}"
   444   let ?p =  "{?p1 l |l. l \<in> {1..n+1}} \<union> {?p2 l |l. l \<in> {1..n+1}}"
   445   have abcd:"\<And>i. i<DIM('a) \<Longrightarrow> a $$ i \<le> c $$ i \<and> c$$i \<le> d$$i \<and> d $$ i \<le> b $$ i" using assms
   446     unfolding subset_interval interval_eq_empty by auto
   447   show ?thesis apply(rule that[of ?p]) apply(rule division_ofI)
   448   proof- have "\<And>i. i<DIM('a) \<Longrightarrow> \<pi>' i < Suc n"
   449     proof(rule ccontr,unfold not_less) fix i assume i:"i<DIM('a)" and "Suc n \<le> \<pi>' i"
   450       hence "\<pi>' i \<notin> {1..n}" by auto thus False using \<pi>' i unfolding bij_betw_def by auto
   451     qed hence "c = (\<chi>\<chi> i. if \<pi>' i < Suc n then c $$ i else a $$ i)"
   452         "d = (\<chi>\<chi> i. if \<pi>' i < Suc n then d $$ i else if \<pi>' i = n + 1 then c $$ \<pi> (n + 1) else b $$ i)"
   453       unfolding euclidean_eq[where 'a='a] using \<pi>' unfolding bij_betw_def by auto
   454     thus cdp:"{c..d} \<in> ?p" apply-apply(rule UnI1) unfolding mem_Collect_eq apply(rule_tac x="n + 1" in exI) by auto
   455     have "\<And>l. l\<in>{1..n+1} \<Longrightarrow> ?p1 l \<subseteq> {a..b}"  "\<And>l. l\<in>{1..n+1} \<Longrightarrow> ?p2 l \<subseteq> {a..b}"
   456       unfolding subset_eq apply(rule_tac[!] ballI,rule_tac[!] ccontr)
   457     proof- fix l assume l:"l\<in>{1..n+1}" fix x assume "x\<notin>{a..b}"
   458       then guess i unfolding mem_interval not_all not_imp .. note i=conjunctD2[OF this]
   459       show "x \<in> ?p1 l \<Longrightarrow> False" "x \<in> ?p2 l \<Longrightarrow> False" unfolding mem_interval apply(erule_tac[!] x=i in allE)
   460         apply(case_tac[!] "\<pi>' i < l", case_tac[!] "\<pi>' i = l") using abcd[of i] i by auto 
   461     qed moreover have "\<And>x. x \<in> {a..b} \<Longrightarrow> x \<in> \<Union>?p"
   462     proof- fix x assume x:"x\<in>{a..b}"
   463       { presume "x\<notin>{c..d} \<Longrightarrow> x \<in> \<Union>?p" thus "x \<in> \<Union>?p" using cdp by blast }
   464       let ?M = "{i. i\<in>{1..n+1} \<and> \<not> (c $$ \<pi> i \<le> x $$ \<pi> i \<and> x $$ \<pi> i \<le> d $$ \<pi> i)}"
   465       assume "x\<notin>{c..d}" then guess i0 unfolding mem_interval not_all not_imp ..
   466       hence "\<pi>' i0 \<in> ?M" using \<pi>' unfolding bij_betw_def by(auto intro!:le_SucI)
   467       hence M:"finite ?M" "?M \<noteq> {}" by auto
   468       def l \<equiv> "Min ?M" note l = Min_less_iff[OF M,unfolded l_def[symmetric]] Min_in[OF M,unfolded mem_Collect_eq l_def[symmetric]]
   469         Min_gr_iff[OF M,unfolded l_def[symmetric]]
   470       have "x\<in>?p1 l \<or> x\<in>?p2 l" using l(2)[THEN conjunct2] unfolding de_Morgan_conj not_le
   471         apply- apply(erule disjE) apply(rule disjI1) defer apply(rule disjI2)
   472       proof- assume as:"x $$ \<pi> l < c $$ \<pi> l"
   473         show "x \<in> ?p1 l" unfolding mem_interval apply safe unfolding euclidean_lambda_beta'
   474         proof- case goal1 have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le using goal1 by auto
   475           thus ?case using as x[unfolded mem_interval,rule_format,of i]
   476             apply auto using l(3)[of "\<pi>' i"] using goal1 by(auto elim!:ballE[where x="\<pi>' i"])
   477         next case goal2 have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le using goal2 by auto
   478           thus ?case using as x[unfolded mem_interval,rule_format,of i]
   479             apply auto using l(3)[of "\<pi>' i"] using goal2 by(auto elim!:ballE[where x="\<pi>' i"])
   480         qed
   481       next assume as:"x $$ \<pi> l > d $$ \<pi> l"
   482         show "x \<in> ?p2 l" unfolding mem_interval apply safe unfolding euclidean_lambda_beta'
   483         proof- fix i assume i:"i<DIM('a)"
   484           have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le using i by auto
   485           thus "(if \<pi>' i < l then c $$ i else if \<pi>' i = l then d $$ \<pi> l else a $$ i) \<le> x $$ i"
   486             "x $$ i \<le> (if \<pi>' i < l then d $$ i else b $$ i)"
   487             using as x[unfolded mem_interval,rule_format,of i]
   488             apply auto using l(3)[of "\<pi>' i"] i by(auto elim!:ballE[where x="\<pi>' i"])
   489         qed qed
   490       thus "x \<in> \<Union>?p" using l(2) by blast 
   491     qed ultimately show "\<Union>?p = {a..b}" apply-apply(rule) defer apply(rule) by(assumption,blast)
   492     
   493     show "finite ?p" by auto
   494     fix k assume k:"k\<in>?p" then obtain l where l:"k = ?p1 l \<or> k = ?p2 l" "l \<in> {1..n + 1}" by auto
   495     show "k\<subseteq>{a..b}" apply(rule,unfold mem_interval,rule,rule) 
   496     proof fix i x assume i:"i<DIM('a)" assume "x \<in> k" moreover have "\<pi>' i < l \<or> \<pi>' i = l \<or> \<pi>' i > l" by auto
   497       ultimately show "a$$i \<le> x$$i" "x$$i \<le> b$$i" using abcd[of i] using l using i
   498         by(auto elim:disjE elim!:allE[where x=i] simp add:eucl_le[where 'a='a])
   499     qed have "\<And>l. ?p1 l \<noteq> {}" "\<And>l. ?p2 l \<noteq> {}" unfolding interval_eq_empty not_ex apply(rule_tac[!] allI)
   500     proof- case goal1 thus ?case using abcd[of x] by auto
   501     next   case goal2 thus ?case using abcd[of x] by auto
   502     qed thus "k \<noteq> {}" using k by auto
   503     show "\<exists>a b. k = {a..b}" using k by auto
   504     fix k' assume k':"k' \<in> ?p" "k \<noteq> k'" then obtain l' where l':"k' = ?p1 l' \<or> k' = ?p2 l'" "l' \<in> {1..n + 1}" by auto
   505     { fix k k' l l'
   506       assume k:"k\<in>?p" and l:"k = ?p1 l \<or> k = ?p2 l" "l \<in> {1..n + 1}" 
   507       assume k':"k' \<in> ?p" "k \<noteq> k'" and  l':"k' = ?p1 l' \<or> k' = ?p2 l'" "l' \<in> {1..n + 1}" 
   508       assume "l \<le> l'" fix x
   509       have "x \<notin> interior k \<inter> interior k'" 
   510       proof(rule,cases "l' = n+1") assume x:"x \<in> interior k \<inter> interior k'"
   511         case True hence "\<And>i. i<DIM('a) \<Longrightarrow> \<pi>' i < l'" using \<pi>'i using l' by(auto simp add:less_Suc_eq_le)
   512         hence *:"\<And> P Q. (\<chi>\<chi> i. if \<pi>' i < l' then P i else Q i) = ((\<chi>\<chi> i. P i)::'a)" apply-apply(subst euclidean_eq) by auto
   513         hence k':"k' = {c..d}" using l'(1) unfolding * by auto
   514         have ln:"l < n + 1" 
   515         proof(rule ccontr) case goal1 hence l2:"l = n+1" using l by auto
   516           hence "\<And>i. i<DIM('a) \<Longrightarrow> \<pi>' i < l" using \<pi>'i by(auto simp add:less_Suc_eq_le)
   517           hence *:"\<And> P Q. (\<chi>\<chi> i. if \<pi>' i < l then P i else Q i) = ((\<chi>\<chi> i. P i)::'a)" apply-apply(subst euclidean_eq) by auto
   518           hence "k = {c..d}" using l(1) \<pi>'i unfolding * by(auto)
   519           thus False using `k\<noteq>k'` k' by auto
   520         qed have **:"\<pi>' (\<pi> l) = l" using \<pi>'\<pi>[of l] using l ln by auto
   521         have "x $$ \<pi> l < c $$ \<pi> l \<or> d $$ \<pi> l < x $$ \<pi> l" using l(1) apply-
   522         proof(erule disjE)
   523           assume as:"k = ?p1 l" note * = conjunct1[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   524           show ?thesis using *[of "\<pi> l"] using ln l(2) using \<pi>i[of l] by(auto simp add:** not_less)
   525         next assume as:"k = ?p2 l" note * = conjunct1[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   526           show ?thesis using *[of "\<pi> l"] using ln l(2) using \<pi>i[of l] unfolding ** by auto
   527         qed thus False using x unfolding k' unfolding Int_iff interior_closed_interval mem_interval
   528           by(auto elim!:allE[where x="\<pi> l"])
   529       next case False hence "l < n + 1" using l'(2) using `l\<le>l'` by auto
   530         hence ln:"l \<in> {1..n}" "l' \<in> {1..n}" using l l' False by auto
   531         note \<pi>l = \<pi>'\<pi>[OF ln(1)] \<pi>'\<pi>[OF ln(2)]
   532         assume x:"x \<in> interior k \<inter> interior k'"
   533         show False using l(1) l'(1) apply-
   534         proof(erule_tac[!] disjE)+
   535           assume as:"k = ?p1 l" "k' = ?p1 l'"
   536           note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   537           have "l \<noteq> l'" using k'(2)[unfolded as] by auto
   538           thus False using *[of "\<pi> l'"] *[of "\<pi> l"] ln using \<pi>i[OF ln(1)] \<pi>i[OF ln(2)] apply(cases "l<l'")
   539             by(auto simp add:euclidean_lambda_beta' \<pi>l \<pi>i n_def)
   540         next assume as:"k = ?p2 l" "k' = ?p2 l'"
   541           note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   542           have "l \<noteq> l'" apply(rule) using k'(2)[unfolded as] by auto
   543           thus False using *[of "\<pi> l"] *[of "\<pi> l'"]  `l \<le> l'` ln by(auto simp add:euclidean_lambda_beta' \<pi>l \<pi>i n_def)
   544         next assume as:"k = ?p1 l" "k' = ?p2 l'"
   545           note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   546           show False using abcd[of "\<pi> l'"] using *[of "\<pi> l"] *[of "\<pi> l'"]  `l \<le> l'` ln apply(cases "l=l'")
   547             by(auto simp add:euclidean_lambda_beta' \<pi>l \<pi>i n_def)
   548         next assume as:"k = ?p2 l" "k' = ?p1 l'"
   549           note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   550           show False using *[of "\<pi> l"] *[of "\<pi> l'"] ln `l \<le> l'` apply(cases "l=l'") using abcd[of "\<pi> l'"] 
   551             by(auto simp add:euclidean_lambda_beta' \<pi>l \<pi>i n_def)
   552         qed qed } 
   553     from this[OF k l k' l'] this[OF k'(1) l' k _ l] have "\<And>x. x \<notin> interior k \<inter> interior k'"
   554       apply - apply(cases "l' \<le> l") using k'(2) by auto            
   555     thus "interior k \<inter> interior k' = {}" by auto        
   556 qed qed
   557 
   558 lemma partial_division_extend_interval: assumes "p division_of (\<Union>p)" "(\<Union>p) \<subseteq> {a..b}"
   559   obtains q where "p \<subseteq> q" "q division_of {a..b::'a::ordered_euclidean_space}" proof(cases "p = {}")
   560   case True guess q apply(rule elementary_interval[of a b]) .
   561   thus ?thesis apply- apply(rule that[of q]) unfolding True by auto next
   562   case False note p = division_ofD[OF assms(1)]
   563   have *:"\<forall>k\<in>p. \<exists>q. q division_of {a..b} \<and> k\<in>q" proof case goal1
   564     guess c using p(4)[OF goal1] .. then guess d .. note cd_ = this
   565     have *:"{c..d} \<subseteq> {a..b}" "{c..d} \<noteq> {}" using p(2,3)[OF goal1, unfolded cd_] using assms(2) by auto
   566     guess q apply(rule partial_division_extend_1[OF *]) . thus ?case unfolding cd_ by auto qed
   567   guess q using bchoice[OF *] .. note q = conjunctD2[OF this[rule_format]]
   568   have "\<And>x. x\<in>p \<Longrightarrow> \<exists>d. d division_of \<Union>(q x - {x})" apply(rule,rule_tac p="q x" in division_of_subset) proof-
   569     fix x assume x:"x\<in>p" show "q x division_of \<Union>q x" apply-apply(rule division_ofI)
   570       using division_ofD[OF q(1)[OF x]] by auto show "q x - {x} \<subseteq> q x" by auto qed
   571   hence "\<exists>d. d division_of \<Inter> ((\<lambda>i. \<Union>(q i - {i})) ` p)" apply- apply(rule elementary_inters)
   572     apply(rule finite_imageI[OF p(1)]) unfolding image_is_empty apply(rule False) by auto
   573   then guess d .. note d = this
   574   show ?thesis apply(rule that[of "d \<union> p"]) proof-
   575     have *:"\<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
   576     have *:"{a..b} = \<Inter> (\<lambda>i. \<Union>(q i - {i})) ` p \<union> \<Union>p" apply(rule *[OF False]) proof fix i assume i:"i\<in>p"
   577       show "\<Union>(q i - {i}) \<union> i = {a..b}" using division_ofD(6)[OF q(1)[OF i]] using q(2)[OF i] by auto qed
   578     show "d \<union> p division_of {a..b}" unfolding * apply(rule division_disjoint_union[OF d assms(1)])
   579       apply(rule inter_interior_unions_intervals) apply(rule p open_interior ballI)+ proof(assumption,rule)
   580       fix k assume k:"k\<in>p" have *:"\<And>u t s. u \<subseteq> s \<Longrightarrow> s \<inter> t = {} \<Longrightarrow> u \<inter> t = {}" by auto
   581       show "interior (\<Inter>(\<lambda>i. \<Union>(q i - {i})) ` p) \<inter> interior k = {}" apply(rule *[of _ "interior (\<Union>(q k - {k}))"])
   582 	defer apply(subst Int_commute) apply(rule inter_interior_unions_intervals) proof- note qk=division_ofD[OF q(1)[OF k]]
   583 	show "finite (q k - {k})" "open (interior k)"  "\<forall>t\<in>q k - {k}. \<exists>a b. t = {a..b}" using qk by auto
   584 	show "\<forall>t\<in>q k - {k}. interior k \<inter> interior t = {}" using qk(5) using q(2)[OF k] by auto
   585 	have *:"\<And>x s. x \<in> s \<Longrightarrow> \<Inter>s \<subseteq> x" by auto show "interior (\<Inter>(\<lambda>i. \<Union>(q i - {i})) ` p) \<subseteq> interior (\<Union>(q k - {k}))"
   586 	  apply(rule subset_interior *)+ using k by auto qed qed qed auto qed
   587 
   588 lemma elementary_bounded[dest]: "p division_of s \<Longrightarrow> bounded (s::('a::ordered_euclidean_space) set)"
   589   unfolding division_of_def by(metis bounded_Union bounded_interval) 
   590 
   591 lemma elementary_subset_interval: "p division_of s \<Longrightarrow> \<exists>a b. s \<subseteq> {a..b::'a::ordered_euclidean_space}"
   592   by(meson elementary_bounded bounded_subset_closed_interval)
   593 
   594 lemma division_union_intervals_exists: assumes "{a..b::'a::ordered_euclidean_space} \<noteq> {}"
   595   obtains p where "(insert {a..b} p) division_of ({a..b} \<union> {c..d})" proof(cases "{c..d} = {}")
   596   case True show ?thesis apply(rule that[of "{}"]) unfolding True using assms by auto next
   597   case False note false=this show ?thesis proof(cases "{a..b} \<inter> {c..d} = {}")
   598   have *:"\<And>a b. {a,b} = {a} \<union> {b}" by auto
   599   case True show ?thesis apply(rule that[of "{{c..d}}"]) unfolding * apply(rule division_disjoint_union)
   600     using false True assms using interior_subset by auto next
   601   case False obtain u v where uv:"{a..b} \<inter> {c..d} = {u..v}" unfolding inter_interval by auto
   602   have *:"{u..v} \<subseteq> {c..d}" using uv by auto
   603   guess p apply(rule partial_division_extend_1[OF * False[unfolded uv]]) . note p=this division_ofD[OF this(1)]
   604   have *:"{a..b} \<union> {c..d} = {a..b} \<union> \<Union>(p - {{u..v}})" "\<And>x s. insert x s = {x} \<union> s" using p(8) unfolding uv[THEN sym] by auto
   605   show thesis apply(rule that[of "p - {{u..v}}"]) unfolding *(1) apply(subst *(2)) apply(rule division_disjoint_union)
   606     apply(rule,rule assms) apply(rule division_of_subset[of p]) apply(rule division_of_union_self[OF p(1)]) defer
   607     unfolding interior_inter[THEN sym] proof-
   608     have *:"\<And>cd p uv ab. p \<subseteq> cd \<Longrightarrow> ab \<inter> cd = uv \<Longrightarrow> ab \<inter> p = uv \<inter> p" by auto
   609     have "interior ({a..b} \<inter> \<Union>(p - {{u..v}})) = interior({u..v} \<inter> \<Union>(p - {{u..v}}))" 
   610       apply(rule arg_cong[of _ _ interior]) apply(rule *[OF _ uv]) using p(8) by auto
   611     also have "\<dots> = {}" unfolding interior_inter apply(rule inter_interior_unions_intervals) using p(6) p(7)[OF p(2)] p(3) by auto
   612     finally show "interior ({a..b} \<inter> \<Union>(p - {{u..v}})) = {}" by assumption qed auto qed qed
   613 
   614 lemma division_of_unions: assumes "finite f"  "\<And>p. p\<in>f \<Longrightarrow> p division_of (\<Union>p)"
   615   "\<And>k1 k2. \<lbrakk>k1 \<in> \<Union>f; k2 \<in> \<Union>f; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
   616   shows "\<Union>f division_of \<Union>\<Union>f" apply(rule division_ofI) prefer 5 apply(rule assms(3)|assumption)+
   617   apply(rule finite_Union assms(1))+ prefer 3 apply(erule UnionE) apply(rule_tac s=X in division_ofD(3)[OF assms(2)])
   618   using division_ofD[OF assms(2)] by auto
   619   
   620 lemma elementary_union_interval: assumes "p division_of \<Union>p"
   621   obtains q where "q division_of ({a..b::'a::ordered_euclidean_space} \<union> \<Union>p)" proof-
   622   note assm=division_ofD[OF assms]
   623   have lem1:"\<And>f s. \<Union>\<Union> (f ` s) = \<Union>(\<lambda>x.\<Union>(f x)) ` s" by auto
   624   have lem2:"\<And>f s. f \<noteq> {} \<Longrightarrow> \<Union>{s \<union> t |t. t \<in> f} = s \<union> \<Union>f" by auto
   625 { presume "p={} \<Longrightarrow> thesis" "{a..b} = {} \<Longrightarrow> thesis" "{a..b} \<noteq> {} \<Longrightarrow> interior {a..b} = {} \<Longrightarrow> thesis"
   626     "p\<noteq>{} \<Longrightarrow> interior {a..b}\<noteq>{} \<Longrightarrow> {a..b} \<noteq> {} \<Longrightarrow> thesis"
   627   thus thesis by auto
   628 next assume as:"p={}" guess p apply(rule elementary_interval[of a b]) .
   629   thus thesis apply(rule_tac that[of p]) unfolding as by auto 
   630 next assume as:"{a..b}={}" show thesis apply(rule that) unfolding as using assms by auto
   631 next assume as:"interior {a..b} = {}" "{a..b} \<noteq> {}"
   632   show thesis apply(rule that[of "insert {a..b} p"],rule division_ofI)
   633     unfolding finite_insert apply(rule assm(1)) unfolding Union_insert  
   634     using assm(2-4) as apply- by(fastsimp dest: assm(5))+
   635 next assume as:"p \<noteq> {}" "interior {a..b} \<noteq> {}" "{a..b}\<noteq>{}"
   636   have "\<forall>k\<in>p. \<exists>q. (insert {a..b} q) division_of ({a..b} \<union> k)" proof case goal1
   637     from assm(4)[OF this] guess c .. then guess d ..
   638     thus ?case apply-apply(rule division_union_intervals_exists[OF as(3),of c d]) by auto
   639   qed from bchoice[OF this] guess q .. note q=division_ofD[OF this[rule_format]]
   640   let ?D = "\<Union>{insert {a..b} (q k) | k. k \<in> p}"
   641   show thesis apply(rule that[of "?D"]) proof(rule division_ofI)
   642     have *:"{insert {a..b} (q k) |k. k \<in> p} = (\<lambda>k. insert {a..b} (q k)) ` p" by auto
   643     show "finite ?D" apply(rule finite_Union) unfolding * apply(rule finite_imageI) using assm(1) q(1) by auto
   644     show "\<Union>?D = {a..b} \<union> \<Union>p" unfolding * lem1 unfolding lem2[OF as(1), of "{a..b}",THEN sym]
   645       using q(6) by auto
   646     fix k assume k:"k\<in>?D" thus " k \<subseteq> {a..b} \<union> \<Union>p" using q(2) by auto
   647     show "k \<noteq> {}" using q(3) k by auto show "\<exists>a b. k = {a..b}" using q(4) k by auto
   648     fix k' assume k':"k'\<in>?D" "k\<noteq>k'"
   649     obtain x  where x: "k \<in>insert {a..b} (q x)"  "x\<in>p"  using k  by auto
   650     obtain x' where x':"k'\<in>insert {a..b} (q x')" "x'\<in>p" using k' by auto
   651     show "interior k \<inter> interior k' = {}" proof(cases "x=x'")
   652       case True show ?thesis apply(rule q(5)) using x x' k' unfolding True by auto
   653     next case False 
   654       { presume "k = {a..b} \<Longrightarrow> ?thesis" "k' = {a..b} \<Longrightarrow> ?thesis" 
   655         "k \<noteq> {a..b} \<Longrightarrow> k' \<noteq> {a..b} \<Longrightarrow> ?thesis"
   656         thus ?thesis by auto }
   657       { assume as':"k  = {a..b}" show ?thesis apply(rule q(5)) using x' k'(2) unfolding as' by auto }
   658       { assume as':"k' = {a..b}" show ?thesis apply(rule q(5)) using x  k'(2) unfolding as' by auto }
   659       assume as':"k \<noteq> {a..b}" "k' \<noteq> {a..b}"
   660       guess c using q(4)[OF x(2,1)] .. then guess d .. note c_d=this
   661       have "interior k  \<inter> interior {a..b} = {}" apply(rule q(5)) using x  k'(2) using as' by auto
   662       hence "interior k \<subseteq> interior x" apply-
   663         apply(rule interior_subset_union_intervals[OF c_d _ as(2) q(2)[OF x(2,1)]]) by auto moreover
   664       guess c using q(4)[OF x'(2,1)] .. then guess d .. note c_d=this
   665       have "interior k' \<inter> interior {a..b} = {}" apply(rule q(5)) using x' k'(2) using as' by auto
   666       hence "interior k' \<subseteq> interior x'" apply-
   667         apply(rule interior_subset_union_intervals[OF c_d _ as(2) q(2)[OF x'(2,1)]]) by auto
   668       ultimately show ?thesis using assm(5)[OF x(2) x'(2) False] by auto
   669     qed qed } qed
   670 
   671 lemma elementary_unions_intervals:
   672   assumes "finite f" "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = {a..b::'a::ordered_euclidean_space}"
   673   obtains p where "p division_of (\<Union>f)" proof-
   674   have "\<exists>p. p division_of (\<Union>f)" proof(induct_tac f rule:finite_subset_induct) 
   675     show "\<exists>p. p division_of \<Union>{}" using elementary_empty by auto
   676     fix x F assume as:"finite F" "x \<notin> F" "\<exists>p. p division_of \<Union>F" "x\<in>f"
   677     from this(3) guess p .. note p=this
   678     from assms(2)[OF as(4)] guess a .. then guess b .. note ab=this
   679     have *:"\<Union>F = \<Union>p" using division_ofD[OF p] by auto
   680     show "\<exists>p. p division_of \<Union>insert x F" using elementary_union_interval[OF p[unfolded *], of a b]
   681       unfolding Union_insert ab * by auto
   682   qed(insert assms,auto) thus ?thesis apply-apply(erule exE,rule that) by auto qed
   683 
   684 lemma elementary_union: assumes "ps division_of s" "pt division_of (t::('a::ordered_euclidean_space) set)"
   685   obtains p where "p division_of (s \<union> t)"
   686 proof- have "s \<union> t = \<Union>ps \<union> \<Union>pt" using assms unfolding division_of_def by auto
   687   hence *:"\<Union>(ps \<union> pt) = s \<union> t" by auto
   688   show ?thesis apply-apply(rule elementary_unions_intervals[of "ps\<union>pt"])
   689     unfolding * prefer 3 apply(rule_tac p=p in that)
   690     using assms[unfolded division_of_def] by auto qed
   691 
   692 lemma partial_division_extend: fixes t::"('a::ordered_euclidean_space) set"
   693   assumes "p division_of s" "q division_of t" "s \<subseteq> t"
   694   obtains r where "p \<subseteq> r" "r division_of t" proof-
   695   note divp = division_ofD[OF assms(1)] and divq = division_ofD[OF assms(2)]
   696   obtain a b where ab:"t\<subseteq>{a..b}" using elementary_subset_interval[OF assms(2)] by auto
   697   guess r1 apply(rule partial_division_extend_interval) apply(rule assms(1)[unfolded divp(6)[THEN sym]])
   698     apply(rule subset_trans) by(rule ab assms[unfolded divp(6)[THEN sym]])+  note r1 = this division_ofD[OF this(2)]
   699   guess p' apply(rule elementary_unions_intervals[of "r1 - p"]) using r1(3,6) by auto 
   700   then obtain r2 where r2:"r2 division_of (\<Union>(r1 - p)) \<inter> (\<Union>q)" 
   701     apply- apply(drule elementary_inter[OF _ assms(2)[unfolded divq(6)[THEN sym]]]) by auto
   702   { fix x assume x:"x\<in>t" "x\<notin>s"
   703     hence "x\<in>\<Union>r1" unfolding r1 using ab by auto
   704     then guess r unfolding Union_iff .. note r=this moreover
   705     have "r \<notin> p" proof assume "r\<in>p" hence "x\<in>s" using divp(2) r by auto
   706       thus False using x by auto qed
   707     ultimately have "x\<in>\<Union>(r1 - p)" by auto }
   708   hence *:"t = \<Union>p \<union> (\<Union>(r1 - p) \<inter> \<Union>q)" unfolding divp divq using assms(3) by auto
   709   show ?thesis apply(rule that[of "p \<union> r2"]) unfolding * defer apply(rule division_disjoint_union)
   710     unfolding divp(6) apply(rule assms r2)+
   711   proof- have "interior s \<inter> interior (\<Union>(r1-p)) = {}"
   712     proof(rule inter_interior_unions_intervals)
   713       show "finite (r1 - p)" "open (interior s)" "\<forall>t\<in>r1-p. \<exists>a b. t = {a..b}" using r1 by auto
   714       have *:"\<And>s. (\<And>x. x \<in> s \<Longrightarrow> False) \<Longrightarrow> s = {}" by auto
   715       show "\<forall>t\<in>r1-p. interior s \<inter> interior t = {}" proof(rule)
   716         fix m x assume as:"m\<in>r1-p"
   717         have "interior m \<inter> interior (\<Union>p) = {}" proof(rule inter_interior_unions_intervals)
   718           show "finite p" "open (interior m)" "\<forall>t\<in>p. \<exists>a b. t = {a..b}" using divp by auto
   719           show "\<forall>t\<in>p. interior m \<inter> interior t = {}" apply(rule, rule r1(7)) using as using r1 by auto
   720         qed thus "interior s \<inter> interior m = {}" unfolding divp by auto
   721       qed qed        
   722     thus "interior s \<inter> interior (\<Union>(r1-p) \<inter> (\<Union>q)) = {}" using interior_subset by auto
   723   qed auto qed
   724 
   725 subsection {* Tagged (partial) divisions. *}
   726 
   727 definition tagged_partial_division_of (infixr "tagged'_partial'_division'_of" 40) where
   728   "(s tagged_partial_division_of i) \<equiv>
   729         finite s \<and>
   730         (\<forall>x k. (x,k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = {a..b})) \<and>
   731         (\<forall>x1 k1 x2 k2. (x1,k1) \<in> s \<and> (x2,k2) \<in> s \<and> ((x1,k1) \<noteq> (x2,k2))
   732                        \<longrightarrow> (interior(k1) \<inter> interior(k2) = {}))"
   733 
   734 lemma tagged_partial_division_ofD[dest]: assumes "s tagged_partial_division_of i"
   735   shows "finite s" "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k" "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
   736   "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
   737   "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2,k2) \<in> s \<Longrightarrow> (x1,k1) \<noteq> (x2,k2) \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}"
   738   using assms unfolding tagged_partial_division_of_def  apply- by blast+ 
   739 
   740 definition tagged_division_of (infixr "tagged'_division'_of" 40) where
   741   "(s tagged_division_of i) \<equiv>
   742         (s tagged_partial_division_of i) \<and> (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
   743 
   744 lemma tagged_division_of_finite[dest]: "s tagged_division_of i \<Longrightarrow> finite s"
   745   unfolding tagged_division_of_def tagged_partial_division_of_def by auto
   746 
   747 lemma tagged_division_of:
   748  "(s tagged_division_of i) \<longleftrightarrow>
   749         finite s \<and>
   750         (\<forall>x k. (x,k) \<in> s
   751                \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = {a..b})) \<and>
   752         (\<forall>x1 k1 x2 k2. (x1,k1) \<in> s \<and> (x2,k2) \<in> s \<and> ~((x1,k1) = (x2,k2))
   753                        \<longrightarrow> (interior(k1) \<inter> interior(k2) = {})) \<and>
   754         (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
   755   unfolding tagged_division_of_def tagged_partial_division_of_def by auto
   756 
   757 lemma tagged_division_ofI: assumes
   758   "finite s" "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k" "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"  "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
   759   "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2,k2) \<in> s \<Longrightarrow> ~((x1,k1) = (x2,k2)) \<Longrightarrow> (interior(k1) \<inter> interior(k2) = {})"
   760   "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
   761   shows "s tagged_division_of i"
   762   unfolding tagged_division_of apply(rule) defer apply rule
   763   apply(rule allI impI conjI assms)+ apply assumption
   764   apply(rule, rule assms, assumption) apply(rule assms, assumption)
   765   using assms(1,5-) apply- by blast+
   766 
   767 lemma tagged_division_ofD[dest]: assumes "s tagged_division_of i"
   768   shows "finite s" "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k" "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"  "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
   769   "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2,k2) \<in> s \<Longrightarrow> ~((x1,k1) = (x2,k2)) \<Longrightarrow> (interior(k1) \<inter> interior(k2) = {})"
   770   "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)" using assms unfolding tagged_division_of apply- by blast+
   771 
   772 lemma division_of_tagged_division: assumes"s tagged_division_of i"  shows "(snd ` s) division_of i"
   773 proof(rule division_ofI) note assm=tagged_division_ofD[OF assms]
   774   show "\<Union>snd ` s = i" "finite (snd ` s)" using assm by auto
   775   fix k assume k:"k \<in> snd ` s" then obtain xk where xk:"(xk, k) \<in> s" by auto
   776   thus  "k \<subseteq> i" "k \<noteq> {}" "\<exists>a b. k = {a..b}" using assm apply- by fastsimp+
   777   fix k' assume k':"k' \<in> snd ` s" "k \<noteq> k'" from this(1) obtain xk' where xk':"(xk', k') \<in> s" by auto
   778   thus "interior k \<inter> interior k' = {}" apply-apply(rule assm(5)) apply(rule xk xk')+ using k' by auto
   779 qed
   780 
   781 lemma partial_division_of_tagged_division: assumes "s tagged_partial_division_of i"
   782   shows "(snd ` s) division_of \<Union>(snd ` s)"
   783 proof(rule division_ofI) note assm=tagged_partial_division_ofD[OF assms]
   784   show "finite (snd ` s)" "\<Union>snd ` s = \<Union>snd ` s" using assm by auto
   785   fix k assume k:"k \<in> snd ` s" then obtain xk where xk:"(xk, k) \<in> s" by auto
   786   thus "k\<noteq>{}" "\<exists>a b. k = {a..b}" "k \<subseteq> \<Union>snd ` s" using assm by auto
   787   fix k' assume k':"k' \<in> snd ` s" "k \<noteq> k'" from this(1) obtain xk' where xk':"(xk', k') \<in> s" by auto
   788   thus "interior k \<inter> interior k' = {}" apply-apply(rule assm(5)) apply(rule xk xk')+ using k' by auto
   789 qed
   790 
   791 lemma tagged_partial_division_subset: assumes "s tagged_partial_division_of i" "t \<subseteq> s"
   792   shows "t tagged_partial_division_of i"
   793   using assms unfolding tagged_partial_division_of_def using finite_subset[OF assms(2)] by blast
   794 
   795 lemma setsum_over_tagged_division_lemma: fixes d::"('m::ordered_euclidean_space) set \<Rightarrow> 'a::real_normed_vector"
   796   assumes "p tagged_division_of i" "\<And>u v. {u..v} \<noteq> {} \<Longrightarrow> content {u..v} = 0 \<Longrightarrow> d {u..v} = 0"
   797   shows "setsum (\<lambda>(x,k). d k) p = setsum d (snd ` p)"
   798 proof- note assm=tagged_division_ofD[OF assms(1)]
   799   have *:"(\<lambda>(x,k). d k) = d \<circ> snd" unfolding o_def apply(rule ext) by auto
   800   show ?thesis unfolding * apply(subst eq_commute) proof(rule setsum_reindex_nonzero)
   801     show "finite p" using assm by auto
   802     fix x y assume as:"x\<in>p" "y\<in>p" "x\<noteq>y" "snd x = snd y" 
   803     obtain a b where ab:"snd x = {a..b}" using assm(4)[of "fst x" "snd x"] as(1) by auto
   804     have "(fst x, snd y) \<in> p" "(fst x, snd y) \<noteq> y" unfolding as(4)[THEN sym] using as(1-3) by auto
   805     hence "interior (snd x) \<inter> interior (snd y) = {}" apply-apply(rule assm(5)[of "fst x" _ "fst y"]) using as by auto 
   806     hence "content {a..b} = 0" unfolding as(4)[THEN sym] ab content_eq_0_interior by auto
   807     hence "d {a..b} = 0" apply-apply(rule assms(2)) using assm(2)[of "fst x" "snd x"] as(1) unfolding ab[THEN sym] by auto
   808     thus "d (snd x) = 0" unfolding ab by auto qed qed
   809 
   810 lemma tag_in_interval: "p tagged_division_of i \<Longrightarrow> (x,k) \<in> p \<Longrightarrow> x \<in> i" by auto
   811 
   812 lemma tagged_division_of_empty: "{} tagged_division_of {}"
   813   unfolding tagged_division_of by auto
   814 
   815 lemma tagged_partial_division_of_trivial[simp]:
   816  "p tagged_partial_division_of {} \<longleftrightarrow> p = {}"
   817   unfolding tagged_partial_division_of_def by auto
   818 
   819 lemma tagged_division_of_trivial[simp]:
   820  "p tagged_division_of {} \<longleftrightarrow> p = {}"
   821   unfolding tagged_division_of by auto
   822 
   823 lemma tagged_division_of_self:
   824  "x \<in> {a..b} \<Longrightarrow> {(x,{a..b})} tagged_division_of {a..b}"
   825   apply(rule tagged_division_ofI) by auto
   826 
   827 lemma tagged_division_union:
   828   assumes "p1 tagged_division_of s1"  "p2 tagged_division_of s2" "interior s1 \<inter> interior s2 = {}"
   829   shows "(p1 \<union> p2) tagged_division_of (s1 \<union> s2)"
   830 proof(rule tagged_division_ofI) note p1=tagged_division_ofD[OF assms(1)] and p2=tagged_division_ofD[OF assms(2)]
   831   show "finite (p1 \<union> p2)" using p1(1) p2(1) by auto
   832   show "\<Union>{k. \<exists>x. (x, k) \<in> p1 \<union> p2} = s1 \<union> s2" using p1(6) p2(6) by blast
   833   fix x k assume xk:"(x,k)\<in>p1\<union>p2" show "x\<in>k" "\<exists>a b. k = {a..b}" using xk p1(2,4) p2(2,4) by auto
   834   show "k\<subseteq>s1\<union>s2" using xk p1(3) p2(3) by blast
   835   fix x' k' assume xk':"(x',k')\<in>p1\<union>p2" "(x,k) \<noteq> (x',k')"
   836   have *:"\<And>a b. a\<subseteq> s1 \<Longrightarrow> b\<subseteq> s2 \<Longrightarrow> interior a \<inter> interior b = {}" using assms(3) subset_interior by blast
   837   show "interior k \<inter> interior k' = {}" apply(cases "(x,k)\<in>p1", case_tac[!] "(x',k')\<in>p1")
   838     apply(rule p1(5)) prefer 4 apply(rule *) prefer 6 apply(subst Int_commute,rule *) prefer 8 apply(rule p2(5))
   839     using p1(3) p2(3) using xk xk' by auto qed 
   840 
   841 lemma tagged_division_unions:
   842   assumes "finite iset" "\<forall>i\<in>iset. (pfn(i) tagged_division_of i)"
   843   "\<forall>i1 \<in> iset. \<forall>i2 \<in> iset. ~(i1 = i2) \<longrightarrow> (interior(i1) \<inter> interior(i2) = {})"
   844   shows "\<Union>(pfn ` iset) tagged_division_of (\<Union>iset)"
   845 proof(rule tagged_division_ofI)
   846   note assm = tagged_division_ofD[OF assms(2)[rule_format]]
   847   show "finite (\<Union>pfn ` iset)" apply(rule finite_Union) using assms by auto
   848   have "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>pfn ` iset} = \<Union>(\<lambda>i. \<Union>{k. \<exists>x. (x, k) \<in> pfn i}) ` iset" by blast 
   849   also have "\<dots> = \<Union>iset" using assm(6) by auto
   850   finally show "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>pfn ` iset} = \<Union>iset" . 
   851   fix x k assume xk:"(x,k)\<in>\<Union>pfn ` iset" then obtain i where i:"i \<in> iset" "(x, k) \<in> pfn i" by auto
   852   show "x\<in>k" "\<exists>a b. k = {a..b}" "k \<subseteq> \<Union>iset" using assm(2-4)[OF i] using i(1) by auto
   853   fix x' k' assume xk':"(x',k')\<in>\<Union>pfn ` iset" "(x, k) \<noteq> (x', k')" then obtain i' where i':"i' \<in> iset" "(x', k') \<in> pfn i'" by auto
   854   have *:"\<And>a b. i\<noteq>i' \<Longrightarrow> a\<subseteq> i \<Longrightarrow> b\<subseteq> i' \<Longrightarrow> interior a \<inter> interior b = {}" using i(1) i'(1)
   855     using assms(3)[rule_format] subset_interior by blast
   856   show "interior k \<inter> interior k' = {}" apply(cases "i=i'")
   857     using assm(5)[OF i _ xk'(2)]  i'(2) using assm(3)[OF i] assm(3)[OF i'] defer apply-apply(rule *) by auto
   858 qed
   859 
   860 lemma tagged_partial_division_of_union_self:
   861   assumes "p tagged_partial_division_of s" shows "p tagged_division_of (\<Union>(snd ` p))"
   862   apply(rule tagged_division_ofI) using tagged_partial_division_ofD[OF assms] by auto
   863 
   864 lemma tagged_division_of_union_self: assumes "p tagged_division_of s"
   865   shows "p tagged_division_of (\<Union>(snd ` p))"
   866   apply(rule tagged_division_ofI) using tagged_division_ofD[OF assms] by auto
   867 
   868 subsection {* Fine-ness of a partition w.r.t. a gauge. *}
   869 
   870 definition fine (infixr "fine" 46) where
   871   "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d(x))"
   872 
   873 lemma fineI: assumes "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
   874   shows "d fine s" using assms unfolding fine_def by auto
   875 
   876 lemma fineD[dest]: assumes "d fine s"
   877   shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x" using assms unfolding fine_def by auto
   878 
   879 lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
   880   unfolding fine_def by auto
   881 
   882 lemma fine_inters:
   883  "(\<lambda>x. \<Inter> {f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
   884   unfolding fine_def by blast
   885 
   886 lemma fine_union:
   887   "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
   888   unfolding fine_def by blast
   889 
   890 lemma fine_unions:"(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
   891   unfolding fine_def by auto
   892 
   893 lemma fine_subset:  "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
   894   unfolding fine_def by blast
   895 
   896 subsection {* Gauge integral. Define on compact intervals first, then use a limit. *}
   897 
   898 definition has_integral_compact_interval (infixr "has'_integral'_compact'_interval" 46) where
   899   "(f has_integral_compact_interval y) i \<equiv>
   900         (\<forall>e>0. \<exists>d. gauge d \<and>
   901           (\<forall>p. p tagged_division_of i \<and> d fine p
   902                         \<longrightarrow> norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - y) < e))"
   903 
   904 definition has_integral (infixr "has'_integral" 46) where 
   905 "((f::('n::ordered_euclidean_space \<Rightarrow> 'b::real_normed_vector)) has_integral y) i \<equiv>
   906         if (\<exists>a b. i = {a..b}) then (f has_integral_compact_interval y) i
   907         else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
   908               \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral_compact_interval z) {a..b} \<and>
   909                                        norm(z - y) < e))"
   910 
   911 lemma has_integral:
   912  "(f has_integral y) ({a..b}) \<longleftrightarrow>
   913         (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p
   914                         \<longrightarrow> norm(setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
   915   unfolding has_integral_def has_integral_compact_interval_def by auto
   916 
   917 lemma has_integralD[dest]: assumes
   918  "(f has_integral y) ({a..b})" "e>0"
   919   obtains d where "gauge d" "\<And>p. p tagged_division_of {a..b} \<Longrightarrow> d fine p
   920                         \<Longrightarrow> norm(setsum (\<lambda>(x,k). content(k) *\<^sub>R f(x)) p - y) < e"
   921   using assms unfolding has_integral by auto
   922 
   923 lemma has_integral_alt:
   924  "(f has_integral y) i \<longleftrightarrow>
   925       (if (\<exists>a b. i = {a..b}) then (f has_integral y) i
   926        else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
   927                                \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0)
   928                                         has_integral z) ({a..b}) \<and>
   929                                        norm(z - y) < e)))"
   930   unfolding has_integral unfolding has_integral_compact_interval_def has_integral_def by auto
   931 
   932 lemma has_integral_altD:
   933   assumes "(f has_integral y) i" "\<not> (\<exists>a b. i = {a..b})" "e>0"
   934   obtains B where "B>0" "\<forall>a b. ball 0 B \<subseteq> {a..b}\<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) ({a..b}) \<and> norm(z - y) < e)"
   935   using assms unfolding has_integral unfolding has_integral_compact_interval_def has_integral_def by auto
   936 
   937 definition integrable_on (infixr "integrable'_on" 46) where
   938   "(f integrable_on i) \<equiv> \<exists>y. (f has_integral y) i"
   939 
   940 definition "integral i f \<equiv> SOME y. (f has_integral y) i"
   941 
   942 lemma integrable_integral[dest]:
   943  "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
   944   unfolding integrable_on_def integral_def by(rule someI_ex)
   945 
   946 lemma has_integral_integrable[intro]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
   947   unfolding integrable_on_def by auto
   948 
   949 lemma has_integral_integral:"f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
   950   by auto
   951 
   952 lemma setsum_content_null:
   953   assumes "content({a..b}) = 0" "p tagged_division_of {a..b}"
   954   shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
   955 proof(rule setsum_0',rule) fix y assume y:"y\<in>p"
   956   obtain x k where xk:"y = (x,k)" using surj_pair[of y] by blast
   957   note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
   958   from this(2) guess c .. then guess d .. note c_d=this
   959   have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x" unfolding xk by auto
   960   also have "\<dots> = 0" using content_subset[OF assm(1)[unfolded c_d]] content_pos_le[of c d]
   961     unfolding assms(1) c_d by auto
   962   finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
   963 qed
   964 
   965 subsection {* Some basic combining lemmas. *}
   966 
   967 lemma tagged_division_unions_exists:
   968   assumes "finite iset" "\<forall>i \<in> iset. \<exists>p. p tagged_division_of i \<and> d fine p"
   969   "\<forall>i1\<in>iset. \<forall>i2\<in>iset. ~(i1 = i2) \<longrightarrow> (interior(i1) \<inter> interior(i2) = {})" "(\<Union>iset = i)"
   970    obtains p where "p tagged_division_of i" "d fine p"
   971 proof- guess pfn using bchoice[OF assms(2)] .. note pfn = conjunctD2[OF this[rule_format]]
   972   show thesis apply(rule_tac p="\<Union>(pfn ` iset)" in that) unfolding assms(4)[THEN sym]
   973     apply(rule tagged_division_unions[OF assms(1) _ assms(3)]) defer 
   974     apply(rule fine_unions) using pfn by auto
   975 qed
   976 
   977 subsection {* The set we're concerned with must be closed. *}
   978 
   979 lemma division_of_closed: "s division_of i \<Longrightarrow> closed (i::('n::ordered_euclidean_space) set)"
   980   unfolding division_of_def by(fastsimp intro!: closed_Union closed_interval)
   981 
   982 subsection {* General bisection principle for intervals; might be useful elsewhere. *}
   983 
   984 lemma interval_bisection_step:  fixes type::"'a::ordered_euclidean_space"
   985   assumes "P {}" "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))" "~(P {a..b::'a})"
   986   obtains c d where "~(P{c..d})"
   987   "\<forall>i<DIM('a). a$$i \<le> c$$i \<and> c$$i \<le> d$$i \<and> d$$i \<le> b$$i \<and> 2 * (d$$i - c$$i) \<le> b$$i - a$$i"
   988 proof- have "{a..b} \<noteq> {}" using assms(1,3) by auto
   989   note ab=this[unfolded interval_eq_empty not_ex not_less]
   990   { fix f have "finite f \<Longrightarrow>
   991         (\<forall>s\<in>f. P s) \<Longrightarrow>
   992         (\<forall>s\<in>f. \<exists>a b. s = {a..b}) \<Longrightarrow>
   993         (\<forall>s\<in>f.\<forall>t\<in>f. ~(s = t) \<longrightarrow> interior(s) \<inter> interior(t) = {}) \<Longrightarrow> P(\<Union>f)"
   994     proof(induct f rule:finite_induct)
   995       case empty show ?case using assms(1) by auto
   996     next case (insert x f) show ?case unfolding Union_insert apply(rule assms(2)[rule_format])
   997         apply rule defer apply rule defer apply(rule inter_interior_unions_intervals)
   998         using insert by auto
   999     qed } note * = this
  1000   let ?A = "{{c..d} | c d::'a. \<forall>i<DIM('a). (c$$i = a$$i) \<and> (d$$i = (a$$i + b$$i) / 2) \<or> (c$$i = (a$$i + b$$i) / 2) \<and> (d$$i = b$$i)}"
  1001   let ?PP = "\<lambda>c d. \<forall>i<DIM('a). a$$i \<le> c$$i \<and> c$$i \<le> d$$i \<and> d$$i \<le> b$$i \<and> 2 * (d$$i - c$$i) \<le> b$$i - a$$i"
  1002   { presume "\<forall>c d. ?PP c d \<longrightarrow> P {c..d} \<Longrightarrow> False"
  1003     thus thesis unfolding atomize_not not_all apply-apply(erule exE)+ apply(rule_tac c=x and d=xa in that) by auto }
  1004   assume as:"\<forall>c d. ?PP c d \<longrightarrow> P {c..d}"
  1005   have "P (\<Union> ?A)" proof(rule *, rule_tac[2-] ballI, rule_tac[4] ballI, rule_tac[4] impI) 
  1006     let ?B = "(\<lambda>s.{(\<chi>\<chi> i. if i \<in> s then a$$i else (a$$i + b$$i) / 2)::'a ..
  1007       (\<chi>\<chi> i. if i \<in> s then (a$$i + b$$i) / 2 else b$$i)}) ` {s. s \<subseteq> {..<DIM('a)}}"
  1008     have "?A \<subseteq> ?B" proof case goal1
  1009       then guess c unfolding mem_Collect_eq .. then guess d apply- by(erule exE,(erule conjE)+) note c_d=this[rule_format]
  1010       have *:"\<And>a b c d. a = c \<Longrightarrow> b = d \<Longrightarrow> {a..b} = {c..d}" by auto
  1011       show "x\<in>?B" unfolding image_iff apply(rule_tac x="{i. i<DIM('a) \<and> c$$i = a$$i}" in bexI)
  1012         unfolding c_d apply(rule * ) unfolding euclidean_eq[where 'a='a] apply safe unfolding euclidean_lambda_beta' mem_Collect_eq
  1013       proof- fix i assume "i<DIM('a)" thus " c $$ i = (if i < DIM('a) \<and> c $$ i = a $$ i then a $$ i else (a $$ i + b $$ i) / 2)"
  1014           "d $$ i = (if i < DIM('a) \<and> c $$ i = a $$ i then (a $$ i + b $$ i) / 2 else b $$ i)"
  1015           using c_d(2)[of i] ab[THEN spec[where x=i]] by(auto simp add:field_simps)
  1016       qed qed
  1017     thus "finite ?A" apply(rule finite_subset) by auto
  1018     fix s assume "s\<in>?A" then guess c unfolding mem_Collect_eq .. then guess d apply- by(erule exE,(erule conjE)+)
  1019     note c_d=this[rule_format]
  1020     show "P s" unfolding c_d apply(rule as[rule_format]) proof- case goal1 thus ?case 
  1021         using c_d(2)[of i] using ab[THEN spec[where x=i]] by auto qed
  1022     show "\<exists>a b. s = {a..b}" unfolding c_d by auto
  1023     fix t assume "t\<in>?A" then guess e unfolding mem_Collect_eq .. then guess f apply- by(erule exE,(erule conjE)+)
  1024     note e_f=this[rule_format]
  1025     assume "s \<noteq> t" hence "\<not> (c = e \<and> d = f)" unfolding c_d e_f by auto
  1026     then obtain i where "c$$i \<noteq> e$$i \<or> d$$i \<noteq> f$$i" and i':"i<DIM('a)" unfolding de_Morgan_conj euclidean_eq[where 'a='a] by auto
  1027     hence i:"c$$i \<noteq> e$$i" "d$$i \<noteq> f$$i" apply- apply(erule_tac[!] disjE)
  1028     proof- assume "c$$i \<noteq> e$$i" thus "d$$i \<noteq> f$$i" using c_d(2)[of i] e_f(2)[of i] by fastsimp
  1029     next   assume "d$$i \<noteq> f$$i" thus "c$$i \<noteq> e$$i" using c_d(2)[of i] e_f(2)[of i] by fastsimp
  1030     qed have *:"\<And>s t. (\<And>a. a\<in>s \<Longrightarrow> a\<in>t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}" by auto
  1031     show "interior s \<inter> interior t = {}" unfolding e_f c_d interior_closed_interval proof(rule *)
  1032       fix x assume "x\<in>{c<..<d}" "x\<in>{e<..<f}"
  1033       hence x:"c$$i < d$$i" "e$$i < f$$i" "c$$i < f$$i" "e$$i < d$$i" unfolding mem_interval using i'
  1034         apply-apply(erule_tac[!] x=i in allE)+ by auto
  1035       show False using c_d(2)[OF i'] apply- apply(erule_tac disjE)
  1036       proof(erule_tac[!] conjE) assume as:"c $$ i = a $$ i" "d $$ i = (a $$ i + b $$ i) / 2"
  1037         show False using e_f(2)[of i] and i x unfolding as by(fastsimp simp add:field_simps)
  1038       next assume as:"c $$ i = (a $$ i + b $$ i) / 2" "d $$ i = b $$ i"
  1039         show False using e_f(2)[of i] and i x unfolding as by(fastsimp simp add:field_simps)
  1040       qed qed qed
  1041   also have "\<Union> ?A = {a..b}" proof(rule set_eqI,rule)
  1042     fix x assume "x\<in>\<Union>?A" then guess Y unfolding Union_iff ..
  1043     from this(1) guess c unfolding mem_Collect_eq .. then guess d ..
  1044     note c_d = this[THEN conjunct2,rule_format] `x\<in>Y`[unfolded this[THEN conjunct1]]
  1045     show "x\<in>{a..b}" unfolding mem_interval proof safe
  1046       fix i assume "i<DIM('a)" thus "a $$ i \<le> x $$ i" "x $$ i \<le> b $$ i"
  1047         using c_d(1)[of i] c_d(2)[unfolded mem_interval,THEN spec[where x=i]] by auto qed
  1048   next fix x assume x:"x\<in>{a..b}"
  1049     have "\<forall>i<DIM('a). \<exists>c d. (c = a$$i \<and> d = (a$$i + b$$i) / 2 \<or> c = (a$$i + b$$i) / 2 \<and> d = b$$i) \<and> c\<le>x$$i \<and> x$$i \<le> d"
  1050       (is "\<forall>i<DIM('a). \<exists>c d. ?P i c d") unfolding mem_interval proof(rule,rule) fix i
  1051       have "?P i (a$$i) ((a $$ i + b $$ i) / 2) \<or> ?P i ((a $$ i + b $$ i) / 2) (b$$i)"
  1052         using x[unfolded mem_interval,THEN spec[where x=i]] by auto thus "\<exists>c d. ?P i c d" by blast
  1053     qed thus "x\<in>\<Union>?A" unfolding Union_iff unfolding lambda_skolem' unfolding Bex_def mem_Collect_eq
  1054       apply-apply(erule exE)+ apply(rule_tac x="{xa..xaa}" in exI) unfolding mem_interval by auto
  1055   qed finally show False using assms by auto qed
  1056 
  1057 lemma interval_bisection: fixes type::"'a::ordered_euclidean_space"
  1058   assumes "P {}" "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))" "\<not> P {a..b::'a}"
  1059   obtains x where "x \<in> {a..b}" "\<forall>e>0. \<exists>c d. x \<in> {c..d} \<and> {c..d} \<subseteq> ball x e \<and> {c..d} \<subseteq> {a..b} \<and> ~P({c..d})"
  1060 proof-
  1061   have "\<forall>x. \<exists>y. \<not> P {fst x..snd x} \<longrightarrow> (\<not> P {fst y..snd y} \<and>
  1062     (\<forall>i<DIM('a). fst x$$i \<le> fst y$$i \<and> fst y$$i \<le> snd y$$i \<and> snd y$$i \<le> snd x$$i \<and>
  1063                            2 * (snd y$$i - fst y$$i) \<le> snd x$$i - fst x$$i))" proof case goal1 thus ?case proof-
  1064       presume "\<not> P {fst x..snd x} \<Longrightarrow> ?thesis"
  1065       thus ?thesis apply(cases "P {fst x..snd x}") by auto
  1066     next assume as:"\<not> P {fst x..snd x}" from interval_bisection_step[of P, OF assms(1-2) as] guess c d . 
  1067       thus ?thesis apply- apply(rule_tac x="(c,d)" in exI) by auto
  1068     qed qed then guess f apply-apply(drule choice) by(erule exE) note f=this
  1069   def AB \<equiv> "\<lambda>n. (f ^^ n) (a,b)" def A \<equiv> "\<lambda>n. fst(AB n)" and B \<equiv> "\<lambda>n. snd(AB n)" note ab_def = this AB_def
  1070   have "A 0 = a" "B 0 = b" "\<And>n. \<not> P {A(Suc n)..B(Suc n)} \<and>
  1071     (\<forall>i<DIM('a). A(n)$$i \<le> A(Suc n)$$i \<and> A(Suc n)$$i \<le> B(Suc n)$$i \<and> B(Suc n)$$i \<le> B(n)$$i \<and> 
  1072     2 * (B(Suc n)$$i - A(Suc n)$$i) \<le> B(n)$$i - A(n)$$i)" (is "\<And>n. ?P n")
  1073   proof- show "A 0 = a" "B 0 = b" unfolding ab_def by auto
  1074     case goal3 note S = ab_def funpow.simps o_def id_apply show ?case
  1075     proof(induct n) case 0 thus ?case unfolding S apply(rule f[rule_format]) using assms(3) by auto
  1076     next case (Suc n) show ?case unfolding S apply(rule f[rule_format]) using Suc unfolding S by auto
  1077     qed qed note AB = this(1-2) conjunctD2[OF this(3),rule_format]
  1078 
  1079   have interv:"\<And>e. 0 < e \<Longrightarrow> \<exists>n. \<forall>x\<in>{A n..B n}. \<forall>y\<in>{A n..B n}. dist x y < e"
  1080   proof- case goal1 guess n using real_arch_pow2[of "(setsum (\<lambda>i. b$$i - a$$i) {..<DIM('a)}) / e"] .. note n=this
  1081     show ?case apply(rule_tac x=n in exI) proof(rule,rule)
  1082       fix x y assume xy:"x\<in>{A n..B n}" "y\<in>{A n..B n}"
  1083       have "dist x y \<le> setsum (\<lambda>i. abs((x - y)$$i)) {..<DIM('a)}" unfolding dist_norm by(rule norm_le_l1)
  1084       also have "\<dots> \<le> setsum (\<lambda>i. B n$$i - A n$$i) {..<DIM('a)}"
  1085       proof(rule setsum_mono) fix i show "\<bar>(x - y) $$ i\<bar> \<le> B n $$ i - A n $$ i"
  1086           using xy[unfolded mem_interval,THEN spec[where x=i]] by auto qed
  1087       also have "\<dots> \<le> setsum (\<lambda>i. b$$i - a$$i) {..<DIM('a)} / 2^n" unfolding setsum_divide_distrib
  1088       proof(rule setsum_mono) case goal1 thus ?case
  1089         proof(induct n) case 0 thus ?case unfolding AB by auto
  1090         next case (Suc n) have "B (Suc n) $$ i - A (Suc n) $$ i \<le> (B n $$ i - A n $$ i) / 2"
  1091             using AB(4)[of i n] using goal1 by auto
  1092           also have "\<dots> \<le> (b $$ i - a $$ i) / 2 ^ Suc n" using Suc by(auto simp add:field_simps) finally show ?case .
  1093         qed qed
  1094       also have "\<dots> < e" using n using goal1 by(auto simp add:field_simps) finally show "dist x y < e" .
  1095     qed qed
  1096   { fix n m ::nat assume "m \<le> n" then guess d unfolding le_Suc_ex_iff .. note d=this
  1097     have "{A n..B n} \<subseteq> {A m..B m}" unfolding d 
  1098     proof(induct d) case 0 thus ?case by auto
  1099     next case (Suc d) show ?case apply(rule subset_trans[OF _ Suc])
  1100         apply(rule) unfolding mem_interval apply(rule,erule_tac x=i in allE)
  1101       proof- case goal1 thus ?case using AB(4)[of i "m + d"] by(auto simp add:field_simps)
  1102       qed qed } note ABsubset = this 
  1103   have "\<exists>a. \<forall>n. a\<in>{A n..B n}" apply(rule decreasing_closed_nest[rule_format,OF closed_interval _ ABsubset interv])
  1104   proof- fix n show "{A n..B n} \<noteq> {}" apply(cases "0<n") using AB(3)[of "n - 1"] assms(1,3) AB(1-2) by auto qed auto
  1105   then guess x0 .. note x0=this[rule_format]
  1106   show thesis proof(rule that[rule_format,of x0])
  1107     show "x0\<in>{a..b}" using x0[of 0] unfolding AB .
  1108     fix e assume "0 < (e::real)" from interv[OF this] guess n .. note n=this
  1109     show "\<exists>c d. x0 \<in> {c..d} \<and> {c..d} \<subseteq> ball x0 e \<and> {c..d} \<subseteq> {a..b} \<and> \<not> P {c..d}"
  1110       apply(rule_tac x="A n" in exI,rule_tac x="B n" in exI) apply(rule,rule x0) apply rule defer 
  1111     proof show "\<not> P {A n..B n}" apply(cases "0<n") using AB(3)[of "n - 1"] assms(3) AB(1-2) by auto
  1112       show "{A n..B n} \<subseteq> ball x0 e" using n using x0[of n] by auto
  1113       show "{A n..B n} \<subseteq> {a..b}" unfolding AB(1-2)[symmetric] apply(rule ABsubset) by auto
  1114     qed qed qed 
  1115 
  1116 subsection {* Cousin's lemma. *}
  1117 
  1118 lemma fine_division_exists: assumes "gauge g" 
  1119   obtains p where "p tagged_division_of {a..b::'a::ordered_euclidean_space}" "g fine p"
  1120 proof- presume "\<not> (\<exists>p. p tagged_division_of {a..b} \<and> g fine p) \<Longrightarrow> False"
  1121   then guess p unfolding atomize_not not_not .. thus thesis apply-apply(rule that[of p]) by auto
  1122 next assume as:"\<not> (\<exists>p. p tagged_division_of {a..b} \<and> g fine p)"
  1123   guess x apply(rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p",rule_format,OF _ _ as])
  1124     apply(rule_tac x="{}" in exI) defer apply(erule conjE exE)+
  1125   proof- show "{} tagged_division_of {} \<and> g fine {}" unfolding fine_def by auto
  1126     fix s t p p' assume "p tagged_division_of s" "g fine p" "p' tagged_division_of t" "g fine p'" "interior s \<inter> interior t = {}"
  1127     thus "\<exists>p. p tagged_division_of s \<union> t \<and> g fine p" apply-apply(rule_tac x="p \<union> p'" in exI) apply rule
  1128       apply(rule tagged_division_union) prefer 4 apply(rule fine_union) by auto
  1129   qed note x=this
  1130   obtain e where e:"e>0" "ball x e \<subseteq> g x" using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
  1131   from x(2)[OF e(1)] guess c d apply-apply(erule exE conjE)+ . note c_d = this
  1132   have "g fine {(x, {c..d})}" unfolding fine_def using e using c_d(2) by auto
  1133   thus False using tagged_division_of_self[OF c_d(1)] using c_d by auto qed
  1134 
  1135 subsection {* Basic theorems about integrals. *}
  1136 
  1137 lemma has_integral_unique: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1138   assumes "(f has_integral k1) i" "(f has_integral k2) i" shows "k1 = k2"
  1139 proof(rule ccontr) let ?e = "norm(k1 - k2) / 2" assume as:"k1 \<noteq> k2" hence e:"?e > 0" by auto
  1140   have lem:"\<And>f::'n \<Rightarrow> 'a.  \<And> a b k1 k2.
  1141     (f has_integral k1) ({a..b}) \<Longrightarrow> (f has_integral k2) ({a..b}) \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> False"
  1142   proof- case goal1 let ?e = "norm(k1 - k2) / 2" from goal1(3) have e:"?e > 0" by auto
  1143     guess d1 by(rule has_integralD[OF goal1(1) e]) note d1=this
  1144     guess d2 by(rule has_integralD[OF goal1(2) e]) note d2=this
  1145     guess p by(rule fine_division_exists[OF gauge_inter[OF d1(1) d2(1)],of a b]) note p=this
  1146     let ?c = "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" have "norm (k1 - k2) \<le> norm (?c - k2) + norm (?c - k1)"
  1147       using norm_triangle_ineq4[of "k1 - ?c" "k2 - ?c"] by(auto simp add:algebra_simps norm_minus_commute)
  1148     also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
  1149       apply(rule add_strict_mono) apply(rule_tac[!] d2(2) d1(2)) using p unfolding fine_def by auto
  1150     finally show False by auto
  1151   qed { presume "\<not> (\<exists>a b. i = {a..b}) \<Longrightarrow> False"
  1152     thus False apply-apply(cases "\<exists>a b. i = {a..b}")
  1153       using assms by(auto simp add:has_integral intro:lem[OF _ _ as]) }
  1154   assume as:"\<not> (\<exists>a b. i = {a..b})"
  1155   guess B1 by(rule has_integral_altD[OF assms(1) as,OF e]) note B1=this[rule_format]
  1156   guess B2 by(rule has_integral_altD[OF assms(2) as,OF e]) note B2=this[rule_format]
  1157   have "\<exists>a b::'n. ball 0 B1 \<union> ball 0 B2 \<subseteq> {a..b}" apply(rule bounded_subset_closed_interval)
  1158     using bounded_Un bounded_ball by auto then guess a b apply-by(erule exE)+
  1159   note ab=conjunctD2[OF this[unfolded Un_subset_iff]]
  1160   guess w using B1(2)[OF ab(1)] .. note w=conjunctD2[OF this]
  1161   guess z using B2(2)[OF ab(2)] .. note z=conjunctD2[OF this]
  1162   have "z = w" using lem[OF w(1) z(1)] by auto
  1163   hence "norm (k1 - k2) \<le> norm (z - k2) + norm (w - k1)"
  1164     using norm_triangle_ineq4[of "k1 - w" "k2 - z"] by(auto simp add: norm_minus_commute) 
  1165   also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2" apply(rule add_strict_mono) by(rule_tac[!] z(2) w(2))
  1166   finally show False by auto qed
  1167 
  1168 lemma integral_unique[intro]:
  1169   "(f has_integral y) k \<Longrightarrow> integral k f = y"
  1170   unfolding integral_def apply(rule some_equality) by(auto intro: has_integral_unique) 
  1171 
  1172 lemma has_integral_is_0: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector" 
  1173   assumes "\<forall>x\<in>s. f x = 0" shows "(f has_integral 0) s"
  1174 proof- have lem:"\<And>a b. \<And>f::'n \<Rightarrow> 'a.
  1175     (\<forall>x\<in>{a..b}. f(x) = 0) \<Longrightarrow> (f has_integral 0) ({a..b})" unfolding has_integral
  1176   proof(rule,rule) fix a b e and f::"'n \<Rightarrow> 'a"
  1177     assume as:"\<forall>x\<in>{a..b}. f x = 0" "0 < (e::real)"
  1178     show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e)"
  1179       apply(rule_tac x="\<lambda>x. ball x 1" in exI)  apply(rule,rule gaugeI) unfolding centre_in_ball defer apply(rule open_ball)
  1180     proof(rule,rule,erule conjE) case goal1
  1181       have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) = 0" proof(rule setsum_0',rule)
  1182         fix x assume x:"x\<in>p" have "f (fst x) = 0" using tagged_division_ofD(2-3)[OF goal1(1), of "fst x" "snd x"] using as x by auto
  1183         thus "(\<lambda>(x, k). content k *\<^sub>R f x) x = 0" apply(subst surjective_pairing[of x]) unfolding split_conv by auto
  1184       qed thus ?case using as by auto
  1185     qed auto qed  { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
  1186     thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}")
  1187       using assms by(auto simp add:has_integral intro:lem) }
  1188   have *:"(\<lambda>x. if x \<in> s then f x else 0) = (\<lambda>x. 0)" apply(rule ext) using assms by auto
  1189   assume "\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P *
  1190   apply(rule,rule,rule_tac x=1 in exI,rule) defer apply(rule,rule,rule)
  1191   proof- fix e::real and a b assume "e>0"
  1192     thus "\<exists>z. ((\<lambda>x::'n. 0::'a) has_integral z) {a..b} \<and> norm (z - 0) < e"
  1193       apply(rule_tac x=0 in exI) apply(rule,rule lem) by auto
  1194   qed auto qed
  1195 
  1196 lemma has_integral_0[simp]: "((\<lambda>x::'n::ordered_euclidean_space. 0) has_integral 0) s"
  1197   apply(rule has_integral_is_0) by auto 
  1198 
  1199 lemma has_integral_0_eq[simp]: "((\<lambda>x. 0) has_integral i) s \<longleftrightarrow> i = 0"
  1200   using has_integral_unique[OF has_integral_0] by auto
  1201 
  1202 lemma has_integral_linear: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1203   assumes "(f has_integral y) s" "bounded_linear h" shows "((h o f) has_integral ((h y))) s"
  1204 proof- interpret bounded_linear h using assms(2) . from pos_bounded guess B .. note B=conjunctD2[OF this,rule_format]
  1205   have lem:"\<And>f::'n \<Rightarrow> 'a. \<And> y a b.
  1206     (f has_integral y) ({a..b}) \<Longrightarrow> ((h o f) has_integral h(y)) ({a..b})"
  1207   proof(subst has_integral,rule,rule) case goal1
  1208     from pos_bounded guess B .. note B=conjunctD2[OF this,rule_format]
  1209     have *:"e / B > 0" apply(rule divide_pos_pos) using goal1(2) B by auto
  1210     guess g using has_integralD[OF goal1(1) *] . note g=this
  1211     show ?case apply(rule_tac x=g in exI) apply(rule,rule g(1))
  1212     proof(rule,rule,erule conjE) fix p assume as:"p tagged_division_of {a..b}" "g fine p" 
  1213       have *:"\<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" by auto
  1214       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"
  1215         unfolding o_def unfolding scaleR[THEN sym] * by simp
  1216       also have "\<dots> = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" using setsum[of "\<lambda>(x,k). content k *\<^sub>R f x" p] using as by auto
  1217       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)" .
  1218       show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) - h y) < e" unfolding * diff[THEN sym]
  1219         apply(rule le_less_trans[OF B(2)]) using g(2)[OF as] B(1) by(auto simp add:field_simps)
  1220     qed qed { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
  1221     thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}") using assms by(auto simp add:has_integral intro!:lem) }
  1222   assume as:"\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P
  1223   proof(rule,rule) fix e::real  assume e:"0<e"
  1224     have *:"0 < e/B" by(rule divide_pos_pos,rule e,rule B(1))
  1225     guess M using has_integral_altD[OF assms(1) as *,rule_format] . note M=this
  1226     show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) has_integral z) {a..b} \<and> norm (z - h y) < e)"
  1227       apply(rule_tac x=M in exI) apply(rule,rule M(1))
  1228     proof(rule,rule,rule) case goal1 guess z using M(2)[OF goal1(1)] .. note z=conjunctD2[OF this]
  1229       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)"
  1230         unfolding o_def apply(rule ext) using zero by auto
  1231       show ?case apply(rule_tac x="h z" in exI,rule) unfolding * apply(rule lem[OF z(1)]) unfolding diff[THEN sym]
  1232         apply(rule le_less_trans[OF B(2)]) using B(1) z(2) by(auto simp add:field_simps)
  1233     qed qed qed
  1234 
  1235 lemma has_integral_cmul:
  1236   shows "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_integral (c *\<^sub>R k)) s"
  1237   unfolding o_def[THEN sym] apply(rule has_integral_linear,assumption)
  1238   by(rule scaleR.bounded_linear_right)
  1239 
  1240 lemma has_integral_neg:
  1241   shows "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. -(f x)) has_integral (-k)) s"
  1242   apply(drule_tac c="-1" in has_integral_cmul) by auto
  1243 
  1244 lemma has_integral_add: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector" 
  1245   assumes "(f has_integral k) s" "(g has_integral l) s"
  1246   shows "((\<lambda>x. f x + g x) has_integral (k + l)) s"
  1247 proof- have lem:"\<And>f g::'n \<Rightarrow> 'a. \<And>a b k l.
  1248     (f has_integral k) ({a..b}) \<Longrightarrow> (g has_integral l) ({a..b}) \<Longrightarrow>
  1249      ((\<lambda>x. f(x) + g(x)) has_integral (k + l)) ({a..b})" proof- case goal1
  1250     show ?case unfolding has_integral proof(rule,rule) fix e::real assume e:"e>0" hence *:"e/2>0" by auto
  1251       guess d1 using has_integralD[OF goal1(1) *] . note d1=this
  1252       guess d2 using has_integralD[OF goal1(2) *] . note d2=this
  1253       show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e)"
  1254         apply(rule_tac x="\<lambda>x. (d1 x) \<inter> (d2 x)" in exI) apply(rule,rule gauge_inter[OF d1(1) d2(1)])
  1255       proof(rule,rule,erule conjE) fix p assume as:"p tagged_division_of {a..b}" "(\<lambda>x. d1 x \<inter> d2 x) fine p"
  1256         have *:"(\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) = (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p. content k *\<^sub>R g x)"
  1257           unfolding scaleR_right_distrib setsum_addf[of "\<lambda>(x,k). content k *\<^sub>R f x" "\<lambda>(x,k). content k *\<^sub>R g x" p,THEN sym]
  1258           by(rule setsum_cong2,auto)
  1259         have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) = 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))"
  1260           unfolding * by(auto simp add:algebra_simps) also let ?res = "\<dots>"
  1261         from as have *:"d1 fine p" "d2 fine p" unfolding fine_inter by auto
  1262         have "?res < e/2 + e/2" apply(rule le_less_trans[OF norm_triangle_ineq])
  1263           apply(rule add_strict_mono) using d1(2)[OF as(1) *(1)] and d2(2)[OF as(1) *(2)] by auto
  1264         finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e" by auto
  1265       qed qed qed { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
  1266     thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}") using assms by(auto simp add:has_integral intro!:lem) }
  1267   assume as:"\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P
  1268   proof(rule,rule) case goal1 hence *:"e/2 > 0" by auto
  1269     from has_integral_altD[OF assms(1) as *] guess B1 . note B1=this[rule_format]
  1270     from has_integral_altD[OF assms(2) as *] guess B2 . note B2=this[rule_format]
  1271     show ?case apply(rule_tac x="max B1 B2" in exI) apply(rule,rule min_max.less_supI1,rule B1)
  1272     proof(rule,rule,rule) fix a b assume "ball 0 (max B1 B2) \<subseteq> {a..b::'n}"
  1273       hence *:"ball 0 B1 \<subseteq> {a..b::'n}" "ball 0 B2 \<subseteq> {a..b::'n}" by auto
  1274       guess w using B1(2)[OF *(1)] .. note w=conjunctD2[OF this]
  1275       guess z using B2(2)[OF *(2)] .. note z=conjunctD2[OF this]
  1276       have *:"\<And>x. (if x \<in> s then f x + g x else 0) = (if x \<in> s then f x else 0) + (if x \<in> s then g x else 0)" by auto
  1277       show "\<exists>z. ((\<lambda>x. if x \<in> s then f x + g x else 0) has_integral z) {a..b} \<and> norm (z - (k + l)) < e"
  1278         apply(rule_tac x="w + z" in exI) apply(rule,rule lem[OF w(1) z(1), unfolded *[THEN sym]])
  1279         using norm_triangle_ineq[of "w - k" "z - l"] w(2) z(2) by(auto simp add:field_simps)
  1280     qed qed qed
  1281 
  1282 lemma has_integral_sub:
  1283   shows "(f has_integral k) s \<Longrightarrow> (g has_integral l) s \<Longrightarrow> ((\<lambda>x. f(x) - g(x)) has_integral (k - l)) s"
  1284   using has_integral_add[OF _ has_integral_neg,of f k s g l] unfolding algebra_simps by auto
  1285 
  1286 lemma integral_0: "integral s (\<lambda>x::'n::ordered_euclidean_space. 0::'m::real_normed_vector) = 0"
  1287   by(rule integral_unique has_integral_0)+
  1288 
  1289 lemma integral_add:
  1290   shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
  1291    integral s (\<lambda>x. f x + g x) = integral s f + integral s g"
  1292   apply(rule integral_unique) apply(drule integrable_integral)+
  1293   apply(rule has_integral_add) by assumption+
  1294 
  1295 lemma integral_cmul:
  1296   shows "f integrable_on s \<Longrightarrow> integral s (\<lambda>x. c *\<^sub>R f x) = c *\<^sub>R integral s f"
  1297   apply(rule integral_unique) apply(drule integrable_integral)+
  1298   apply(rule has_integral_cmul) by assumption+
  1299 
  1300 lemma integral_neg:
  1301   shows "f integrable_on s \<Longrightarrow> integral s (\<lambda>x. - f x) = - integral s f"
  1302   apply(rule integral_unique) apply(drule integrable_integral)+
  1303   apply(rule has_integral_neg) by assumption+
  1304 
  1305 lemma integral_sub:
  1306   shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> integral s (\<lambda>x. f x - g x) = integral s f - integral s g"
  1307   apply(rule integral_unique) apply(drule integrable_integral)+
  1308   apply(rule has_integral_sub) by assumption+
  1309 
  1310 lemma integrable_0: "(\<lambda>x. 0) integrable_on s"
  1311   unfolding integrable_on_def using has_integral_0 by auto
  1312 
  1313 lemma integrable_add:
  1314   shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x + g x) integrable_on s"
  1315   unfolding integrable_on_def by(auto intro: has_integral_add)
  1316 
  1317 lemma integrable_cmul:
  1318   shows "f integrable_on s \<Longrightarrow> (\<lambda>x. c *\<^sub>R f(x)) integrable_on s"
  1319   unfolding integrable_on_def by(auto intro: has_integral_cmul)
  1320 
  1321 lemma integrable_neg:
  1322   shows "f integrable_on s \<Longrightarrow> (\<lambda>x. -f(x)) integrable_on s"
  1323   unfolding integrable_on_def by(auto intro: has_integral_neg)
  1324 
  1325 lemma integrable_sub:
  1326   shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x - g x) integrable_on s"
  1327   unfolding integrable_on_def by(auto intro: has_integral_sub)
  1328 
  1329 lemma integrable_linear:
  1330   shows "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> (h o f) integrable_on s"
  1331   unfolding integrable_on_def by(auto intro: has_integral_linear)
  1332 
  1333 lemma integral_linear:
  1334   shows "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> integral s (h o f) = h(integral s f)"
  1335   apply(rule has_integral_unique) defer unfolding has_integral_integral 
  1336   apply(drule has_integral_linear,assumption,assumption) unfolding has_integral_integral[THEN sym]
  1337   apply(rule integrable_linear) by assumption+
  1338 
  1339 lemma integral_component_eq[simp]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space"
  1340   assumes "f integrable_on s" shows "integral s (\<lambda>x. f x $$ k) = integral s f $$ k"
  1341   unfolding integral_linear[OF assms(1) bounded_linear_component,unfolded o_def] ..
  1342 
  1343 lemma has_integral_setsum:
  1344   assumes "finite t" "\<forall>a\<in>t. ((f a) has_integral (i a)) s"
  1345   shows "((\<lambda>x. setsum (\<lambda>a. f a x) t) has_integral (setsum i t)) s"
  1346 proof(insert assms(1) subset_refl[of t],induct rule:finite_subset_induct)
  1347   case (insert x F) show ?case unfolding setsum_insert[OF insert(1,3)]
  1348     apply(rule has_integral_add) using insert assms by auto
  1349 qed auto
  1350 
  1351 lemma integral_setsum:
  1352   shows "finite t \<Longrightarrow> \<forall>a\<in>t. (f a) integrable_on s \<Longrightarrow>
  1353   integral s (\<lambda>x. setsum (\<lambda>a. f a x) t) = setsum (\<lambda>a. integral s (f a)) t"
  1354   apply(rule integral_unique) apply(rule has_integral_setsum)
  1355   using integrable_integral by auto
  1356 
  1357 lemma integrable_setsum:
  1358   shows "finite t \<Longrightarrow> \<forall>a \<in> t.(f a) integrable_on s \<Longrightarrow> (\<lambda>x. setsum (\<lambda>a. f a x) t) integrable_on s"
  1359   unfolding integrable_on_def apply(drule bchoice) using has_integral_setsum[of t] by auto
  1360 
  1361 lemma has_integral_eq:
  1362   assumes "\<forall>x\<in>s. f x = g x" "(f has_integral k) s" shows "(g has_integral k) s"
  1363   using has_integral_sub[OF assms(2), of "\<lambda>x. f x - g x" 0]
  1364   using has_integral_is_0[of s "\<lambda>x. f x - g x"] using assms(1) by auto
  1365 
  1366 lemma integrable_eq:
  1367   shows "\<forall>x\<in>s. f x = g x \<Longrightarrow> f integrable_on s \<Longrightarrow> g integrable_on s"
  1368   unfolding integrable_on_def using has_integral_eq[of s f g] by auto
  1369 
  1370 lemma has_integral_eq_eq:
  1371   shows "\<forall>x\<in>s. f x = g x \<Longrightarrow> ((f has_integral k) s \<longleftrightarrow> (g has_integral k) s)"
  1372   using has_integral_eq[of s f g] has_integral_eq[of s g f] by rule auto
  1373 
  1374 lemma has_integral_null[dest]:
  1375   assumes "content({a..b}) = 0" shows  "(f has_integral 0) ({a..b})"
  1376   unfolding has_integral apply(rule,rule,rule_tac x="\<lambda>x. ball x 1" in exI,rule) defer
  1377 proof(rule,rule,erule conjE) fix e::real assume e:"e>0" thus "gauge (\<lambda>x. ball x 1)" by auto
  1378   fix p assume p:"p tagged_division_of {a..b}" (*"(\<lambda>x. ball x 1) fine p"*)
  1379   have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) = 0" unfolding norm_eq_zero diff_0_right
  1380     using setsum_content_null[OF assms(1) p, of f] . 
  1381   thus "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e" using e by auto qed
  1382 
  1383 lemma has_integral_null_eq[simp]:
  1384   shows "content({a..b}) = 0 \<Longrightarrow> ((f has_integral i) ({a..b}) \<longleftrightarrow> i = 0)"
  1385   apply rule apply(rule has_integral_unique,assumption) 
  1386   apply(drule has_integral_null,assumption)
  1387   apply(drule has_integral_null) by auto
  1388 
  1389 lemma integral_null[dest]: shows "content({a..b}) = 0 \<Longrightarrow> integral({a..b}) f = 0"
  1390   by(rule integral_unique,drule has_integral_null)
  1391 
  1392 lemma integrable_on_null[dest]: shows "content({a..b}) = 0 \<Longrightarrow> f integrable_on {a..b}"
  1393   unfolding integrable_on_def apply(drule has_integral_null) by auto
  1394 
  1395 lemma has_integral_empty[intro]: shows "(f has_integral 0) {}"
  1396   unfolding empty_as_interval apply(rule has_integral_null) 
  1397   using content_empty unfolding empty_as_interval .
  1398 
  1399 lemma has_integral_empty_eq[simp]: shows "(f has_integral i) {} \<longleftrightarrow> i = 0"
  1400   apply(rule,rule has_integral_unique,assumption) by auto
  1401 
  1402 lemma integrable_on_empty[intro]: shows "f integrable_on {}" unfolding integrable_on_def by auto
  1403 
  1404 lemma integral_empty[simp]: shows "integral {} f = 0"
  1405   apply(rule integral_unique) using has_integral_empty .
  1406 
  1407 lemma has_integral_refl[intro]: shows "(f has_integral 0) {a..a}" "(f has_integral 0) {a::'a::ordered_euclidean_space}"
  1408 proof- have *:"{a} = {a..a}" apply(rule set_eqI) unfolding mem_interval singleton_iff euclidean_eq[where 'a='a]
  1409     apply safe prefer 3 apply(erule_tac x=i in allE) by(auto simp add: field_simps)
  1410   show "(f has_integral 0) {a..a}" "(f has_integral 0) {a}" unfolding *
  1411     apply(rule_tac[!] has_integral_null) unfolding content_eq_0_interior
  1412     unfolding interior_closed_interval using interval_sing by auto qed
  1413 
  1414 lemma integrable_on_refl[intro]: shows "f integrable_on {a..a}" unfolding integrable_on_def by auto
  1415 
  1416 lemma integral_refl: shows "integral {a..a} f = 0" apply(rule integral_unique) by auto
  1417 
  1418 subsection {* Cauchy-type criterion for integrability. *}
  1419 
  1420 (* XXXXXXX *)
  1421 lemma integrable_cauchy: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::{real_normed_vector,complete_space}" 
  1422   shows "f integrable_on {a..b} \<longleftrightarrow>
  1423   (\<forall>e>0.\<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of {a..b} \<and> d fine p1 \<and>
  1424                             p2 tagged_division_of {a..b} \<and> d fine p2
  1425                             \<longrightarrow> norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 -
  1426                                      setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) < e))" (is "?l = (\<forall>e>0. \<exists>d. ?P e d)")
  1427 proof assume ?l
  1428   then guess y unfolding integrable_on_def has_integral .. note y=this
  1429   show "\<forall>e>0. \<exists>d. ?P e d" proof(rule,rule) case goal1 hence "e/2 > 0" by auto
  1430     then guess d apply- apply(drule y[rule_format]) by(erule exE,erule conjE) note d=this[rule_format]
  1431     show ?case apply(rule_tac x=d in exI,rule,rule d) apply(rule,rule,rule,(erule conjE)+)
  1432     proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b}" "d fine p1" "p2 tagged_division_of {a..b}" "d fine p2"
  1433       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"
  1434         apply(rule dist_triangle_half_l[where y=y,unfolded dist_norm])
  1435         using d(2)[OF conjI[OF as(1-2)]] d(2)[OF conjI[OF as(3-4)]] .
  1436     qed qed
  1437 next assume "\<forall>e>0. \<exists>d. ?P e d" hence "\<forall>n::nat. \<exists>d. ?P (inverse(real (n + 1))) d" by auto
  1438   from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
  1439   have "\<And>n. gauge (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}})" apply(rule gauge_inters) using d(1) by auto
  1440   hence "\<forall>n. \<exists>p. p tagged_division_of {a..b} \<and> (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}}) fine p" apply-
  1441   proof case goal1 from this[of n] show ?case apply(drule_tac fine_division_exists) by auto qed
  1442   from choice[OF this] guess p .. note p = conjunctD2[OF this[rule_format]]
  1443   have dp:"\<And>i n. i\<le>n \<Longrightarrow> d i fine p n" using p(2) unfolding fine_inters by auto
  1444   have "Cauchy (\<lambda>n. setsum (\<lambda>(x,k). content k *\<^sub>R (f x)) (p n))"
  1445   proof(rule CauchyI) case goal1 then guess N unfolding real_arch_inv[of e] .. note N=this
  1446     show ?case apply(rule_tac x=N in exI)
  1447     proof(rule,rule,rule,rule) fix m n assume mn:"N \<le> m" "N \<le> n" have *:"N = (N - 1) + 1" using N by auto
  1448       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"
  1449         apply(rule less_trans[OF _ N[THEN conjunct2,THEN conjunct2]]) apply(subst *) apply(rule d(2))
  1450         using dp p(1) using mn by auto 
  1451     qed qed
  1452   then guess y unfolding convergent_eq_cauchy[THEN sym] .. note y=this[unfolded Lim_sequentially,rule_format]
  1453   show ?l unfolding integrable_on_def has_integral apply(rule_tac x=y in exI)
  1454   proof(rule,rule) fix e::real assume "e>0" hence *:"e/2 > 0" by auto
  1455     then guess N1 unfolding real_arch_inv[of "e/2"] .. note N1=this hence N1':"N1 = N1 - 1 + 1" by auto
  1456     guess N2 using y[OF *] .. note N2=this
  1457     show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - y) < e)"
  1458       apply(rule_tac x="d (N1 + N2)" in exI) apply rule defer 
  1459     proof(rule,rule,erule conjE) show "gauge (d (N1 + N2))" using d by auto
  1460       fix q assume as:"q tagged_division_of {a..b}" "d (N1 + N2) fine q"
  1461       have *:"inverse (real (N1 + N2 + 1)) < e / 2" apply(rule less_trans) using N1 by auto
  1462       show "norm ((\<Sum>(x, k)\<in>q. content k *\<^sub>R f x) - y) < e" apply(rule norm_triangle_half_r)
  1463         apply(rule less_trans[OF _ *]) apply(subst N1', rule d(2)[of "p (N1+N2)"]) defer
  1464         using N2[rule_format,unfolded dist_norm,of "N1+N2"]
  1465         using as dp[of "N1 - 1 + 1 + N2" "N1 + N2"] using p(1)[of "N1 + N2"] using N1 by auto qed qed qed
  1466 
  1467 subsection {* Additivity of integral on abutting intervals. *}
  1468 
  1469 lemma interval_split: fixes a::"'a::ordered_euclidean_space" assumes "k<DIM('a)" shows
  1470   "{a..b} \<inter> {x. x$$k \<le> c} = {a .. (\<chi>\<chi> i. if i = k then min (b$$k) c else b$$i)}"
  1471   "{a..b} \<inter> {x. x$$k \<ge> c} = {(\<chi>\<chi> i. if i = k then max (a$$k) c else a$$i) .. b}"
  1472   apply(rule_tac[!] set_eqI) unfolding Int_iff mem_interval mem_Collect_eq using assms by auto
  1473 
  1474 lemma content_split: fixes a::"'a::ordered_euclidean_space" assumes "k<DIM('a)" shows
  1475   "content {a..b} = content({a..b} \<inter> {x. x$$k \<le> c}) + content({a..b} \<inter> {x. x$$k >= c})"
  1476 proof- note simps = interval_split[OF assms] content_closed_interval_cases eucl_le[where 'a='a]
  1477   { presume "a\<le>b \<Longrightarrow> ?thesis" thus ?thesis apply(cases "a\<le>b") unfolding simps using assms by auto }
  1478   have *:"{..<DIM('a)} = insert k ({..<DIM('a)} - {k})" "\<And>x. finite ({..<DIM('a)}-{x})" "\<And>x. x\<notin>{..<DIM('a)}-{x}"
  1479     using assms by auto
  1480   have *:"\<And>X Y Z. (\<Prod>i\<in>{..<DIM('a)}. Z i (if i = k then X else Y i)) = Z k X * (\<Prod>i\<in>{..<DIM('a)}-{k}. Z i (Y i))"
  1481     "(\<Prod>i\<in>{..<DIM('a)}. b$$i - a$$i) = (\<Prod>i\<in>{..<DIM('a)}-{k}. b$$i - a$$i) * (b$$k - a$$k)" 
  1482     apply(subst *(1)) defer apply(subst *(1)) unfolding setprod_insert[OF *(2-)] by auto
  1483   assume as:"a\<le>b" moreover have "\<And>x. min (b $$ k) c = max (a $$ k) c
  1484     \<Longrightarrow> x* (b$$k - a$$k) = x*(max (a $$ k) c - a $$ k) + x*(b $$ k - max (a $$ k) c)"
  1485     by  (auto simp add:field_simps)
  1486   moreover have **:"(\<Prod>i<DIM('a). ((\<chi>\<chi> i. if i = k then min (b $$ k) c else b $$ i)::'a) $$ i - a $$ i) = 
  1487     (\<Prod>i<DIM('a). (if i = k then min (b $$ k) c else b $$ i) - a $$ i)"
  1488     "(\<Prod>i<DIM('a). b $$ i - ((\<chi>\<chi> i. if i = k then max (a $$ k) c else a $$ i)::'a) $$ i) =
  1489     (\<Prod>i<DIM('a). b $$ i - (if i = k then max (a $$ k) c else a $$ i))"
  1490     apply(rule_tac[!] setprod.cong) by auto
  1491   have "\<not> a $$ k \<le> c \<Longrightarrow> \<not> c \<le> b $$ k \<Longrightarrow> False"
  1492     unfolding not_le using as[unfolded eucl_le[where 'a='a],rule_format,of k] assms by auto
  1493   ultimately show ?thesis using assms unfolding simps **
  1494     unfolding *(1)[of "\<lambda>i x. b$$i - x"] *(1)[of "\<lambda>i x. x - a$$i"] unfolding  *(2) 
  1495     apply(subst(2) euclidean_lambda_beta''[where 'a='a])
  1496     apply(subst(3) euclidean_lambda_beta''[where 'a='a]) by auto
  1497 qed
  1498 
  1499 lemma division_split_left_inj: fixes type::"'a::ordered_euclidean_space"
  1500   assumes "d division_of i" "k1 \<in> d" "k2 \<in> d"  "k1 \<noteq> k2" 
  1501   "k1 \<inter> {x::'a. x$$k \<le> c} = k2 \<inter> {x. x$$k \<le> c}"and k:"k<DIM('a)"
  1502   shows "content(k1 \<inter> {x. x$$k \<le> c}) = 0"
  1503 proof- note d=division_ofD[OF assms(1)]
  1504   have *:"\<And>a b::'a. \<And> c. (content({a..b} \<inter> {x. x$$k \<le> c}) = 0 \<longleftrightarrow> interior({a..b} \<inter> {x. x$$k \<le> c}) = {})"
  1505     unfolding  interval_split[OF k] content_eq_0_interior by auto
  1506   guess u1 v1 using d(4)[OF assms(2)] apply-by(erule exE)+ note uv1=this
  1507   guess u2 v2 using d(4)[OF assms(3)] apply-by(erule exE)+ note uv2=this
  1508   have **:"\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}" by auto
  1509   show ?thesis unfolding uv1 uv2 * apply(rule **[OF d(5)[OF assms(2-4)]])
  1510     defer apply(subst assms(5)[unfolded uv1 uv2]) unfolding uv1 uv2 by auto qed
  1511  
  1512 lemma division_split_right_inj: fixes type::"'a::ordered_euclidean_space"
  1513   assumes "d division_of i" "k1 \<in> d" "k2 \<in> d"  "k1 \<noteq> k2"
  1514   "k1 \<inter> {x::'a. x$$k \<ge> c} = k2 \<inter> {x. x$$k \<ge> c}" and k:"k<DIM('a)"
  1515   shows "content(k1 \<inter> {x. x$$k \<ge> c}) = 0"
  1516 proof- note d=division_ofD[OF assms(1)]
  1517   have *:"\<And>a b::'a. \<And> c. (content({a..b} \<inter> {x. x$$k >= c}) = 0 \<longleftrightarrow> interior({a..b} \<inter> {x. x$$k >= c}) = {})"
  1518     unfolding interval_split[OF k] content_eq_0_interior by auto
  1519   guess u1 v1 using d(4)[OF assms(2)] apply-by(erule exE)+ note uv1=this
  1520   guess u2 v2 using d(4)[OF assms(3)] apply-by(erule exE)+ note uv2=this
  1521   have **:"\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}" by auto
  1522   show ?thesis unfolding uv1 uv2 * apply(rule **[OF d(5)[OF assms(2-4)]])
  1523     defer apply(subst assms(5)[unfolded uv1 uv2]) unfolding uv1 uv2 by auto qed
  1524 
  1525 lemma tagged_division_split_left_inj: fixes x1::"'a::ordered_euclidean_space"
  1526   assumes "d tagged_division_of i" "(x1,k1) \<in> d" "(x2,k2) \<in> d" "k1 \<noteq> k2"  "k1 \<inter> {x. x$$k \<le> c} = k2 \<inter> {x. x$$k \<le> c}" 
  1527   and k:"k<DIM('a)"
  1528   shows "content(k1 \<inter> {x. x$$k \<le> c}) = 0"
  1529 proof- have *:"\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c" unfolding image_iff apply(rule_tac x="(a,b)" in bexI) by auto
  1530   show ?thesis apply(rule division_split_left_inj[OF division_of_tagged_division[OF assms(1)]])
  1531     apply(rule_tac[1-2] *) using assms(2-) by auto qed
  1532 
  1533 lemma tagged_division_split_right_inj: fixes x1::"'a::ordered_euclidean_space"
  1534   assumes "d tagged_division_of i" "(x1,k1) \<in> d" "(x2,k2) \<in> d" "k1 \<noteq> k2"  "k1 \<inter> {x. x$$k \<ge> c} = k2 \<inter> {x. x$$k \<ge> c}" 
  1535   and k:"k<DIM('a)"
  1536   shows "content(k1 \<inter> {x. x$$k \<ge> c}) = 0"
  1537 proof- have *:"\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c" unfolding image_iff apply(rule_tac x="(a,b)" in bexI) by auto
  1538   show ?thesis apply(rule division_split_right_inj[OF division_of_tagged_division[OF assms(1)]])
  1539     apply(rule_tac[1-2] *) using assms(2-) by auto qed
  1540 
  1541 lemma division_split: fixes a::"'a::ordered_euclidean_space"
  1542   assumes "p division_of {a..b}" and k:"k<DIM('a)"
  1543   shows "{l \<inter> {x. x$$k \<le> c} | l. l \<in> p \<and> ~(l \<inter> {x. x$$k \<le> c} = {})} division_of({a..b} \<inter> {x. x$$k \<le> c})" (is "?p1 division_of ?I1") and 
  1544         "{l \<inter> {x. x$$k \<ge> c} | l. l \<in> p \<and> ~(l \<inter> {x. x$$k \<ge> c} = {})} division_of ({a..b} \<inter> {x. x$$k \<ge> c})" (is "?p2 division_of ?I2")
  1545 proof(rule_tac[!] division_ofI) note p=division_ofD[OF assms(1)]
  1546   show "finite ?p1" "finite ?p2" using p(1) by auto show "\<Union>?p1 = ?I1" "\<Union>?p2 = ?I2" unfolding p(6)[THEN sym] by auto
  1547   { fix k assume "k\<in>?p1" then guess l unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l=this
  1548     guess u v using p(4)[OF l(2)] apply-by(erule exE)+ note uv=this
  1549     show "k\<subseteq>?I1" "k \<noteq> {}" "\<exists>a b. k = {a..b}" unfolding l
  1550       using p(2-3)[OF l(2)] l(3) unfolding uv apply- prefer 3 apply(subst interval_split[OF k]) by auto
  1551     fix k' assume "k'\<in>?p1" then guess l' unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l'=this
  1552     assume "k\<noteq>k'" thus "interior k \<inter> interior k' = {}" unfolding l l' using p(5)[OF l(2) l'(2)] by auto }
  1553   { fix k assume "k\<in>?p2" then guess l unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l=this
  1554     guess u v using p(4)[OF l(2)] apply-by(erule exE)+ note uv=this
  1555     show "k\<subseteq>?I2" "k \<noteq> {}" "\<exists>a b. k = {a..b}" unfolding l
  1556       using p(2-3)[OF l(2)] l(3) unfolding uv apply- prefer 3 apply(subst interval_split[OF k]) by auto
  1557     fix k' assume "k'\<in>?p2" then guess l' unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l'=this
  1558     assume "k\<noteq>k'" thus "interior k \<inter> interior k' = {}" unfolding l l' using p(5)[OF l(2) l'(2)] by auto }
  1559 qed
  1560 
  1561 lemma has_integral_split: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1562   assumes "(f has_integral i) ({a..b} \<inter> {x. x$$k \<le> c})"  "(f has_integral j) ({a..b} \<inter> {x. x$$k \<ge> c})" and k:"k<DIM('a)"
  1563   shows "(f has_integral (i + j)) ({a..b})"
  1564 proof(unfold has_integral,rule,rule) case goal1 hence e:"e/2>0" by auto
  1565   guess d1 using has_integralD[OF assms(1)[unfolded interval_split[OF k]] e] . note d1=this[unfolded interval_split[THEN sym,OF k]]
  1566   guess d2 using has_integralD[OF assms(2)[unfolded interval_split[OF k]] e] . note d2=this[unfolded interval_split[THEN sym,OF k]]
  1567   let ?d = "\<lambda>x. if x$$k = c then (d1 x \<inter> d2 x) else ball x (abs(x$$k - c)) \<inter> d1 x \<inter> d2 x"
  1568   show ?case apply(rule_tac x="?d" in exI,rule) defer apply(rule,rule,(erule conjE)+)
  1569   proof- show "gauge ?d" using d1(1) d2(1) unfolding gauge_def by auto
  1570     fix p assume "p tagged_division_of {a..b}" "?d fine p" note p = this tagged_division_ofD[OF this(1)]
  1571     have lem0:"\<And>x kk. (x,kk) \<in> p \<Longrightarrow> ~(kk \<inter> {x. x$$k \<le> c} = {}) \<Longrightarrow> x$$k \<le> c"
  1572          "\<And>x kk. (x,kk) \<in> p \<Longrightarrow> ~(kk \<inter> {x. x$$k \<ge> c} = {}) \<Longrightarrow> x$$k \<ge> c"
  1573     proof- fix x kk assume as:"(x,kk)\<in>p"
  1574       show "~(kk \<inter> {x. x$$k \<le> c} = {}) \<Longrightarrow> x$$k \<le> c"
  1575       proof(rule ccontr) case goal1
  1576         from this(2)[unfolded not_le] have "kk \<subseteq> ball x \<bar>x $$ k - c\<bar>"
  1577           using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
  1578         hence "\<exists>y. y \<in> ball x \<bar>x $$ k - c\<bar> \<inter> {x. x $$ k \<le> c}" using goal1(1) by blast 
  1579         then guess y .. hence "\<bar>x $$ k - y $$ k\<bar> < \<bar>x $$ k - c\<bar>" "y$$k \<le> c" apply-apply(rule le_less_trans)
  1580           using component_le_norm[of "x - y" k] by(auto simp add:dist_norm)
  1581         thus False using goal1(2)[unfolded not_le] by(auto simp add:field_simps)
  1582       qed
  1583       show "~(kk \<inter> {x. x$$k \<ge> c} = {}) \<Longrightarrow> x$$k \<ge> c"
  1584       proof(rule ccontr) case goal1
  1585         from this(2)[unfolded not_le] have "kk \<subseteq> ball x \<bar>x $$ k - c\<bar>"
  1586           using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
  1587         hence "\<exists>y. y \<in> ball x \<bar>x $$ k - c\<bar> \<inter> {x. x $$ k \<ge> c}" using goal1(1) by blast 
  1588         then guess y .. hence "\<bar>x $$ k - y $$ k\<bar> < \<bar>x $$ k - c\<bar>" "y$$k \<ge> c" apply-apply(rule le_less_trans)
  1589           using component_le_norm[of "x - y" k] by(auto simp add:dist_norm)
  1590         thus False using goal1(2)[unfolded not_le] by(auto simp add:field_simps)
  1591       qed
  1592     qed
  1593 
  1594     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> (\<forall>x k. P x k \<longrightarrow> Q x (f k))" by auto
  1595     have lem2: "\<And>f s P f. finite s \<Longrightarrow> finite {(x,f k) | x k. (x,k) \<in> s \<and> P x k}"
  1596     proof- case goal1 thus ?case apply-apply(rule finite_subset[of _ "(\<lambda>(x,k). (x,f k)) ` s"]) by auto qed
  1597     have lem3: "\<And>g::('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> bool. finite p \<Longrightarrow>
  1598       setsum (\<lambda>(x,k). content k *\<^sub>R f x) {(x,g k) |x k. (x,k) \<in> p \<and> ~(g k = {})}
  1599                = setsum (\<lambda>(x,k). content k *\<^sub>R f x) ((\<lambda>(x,k). (x,g k)) ` p)"
  1600       apply(rule setsum_mono_zero_left) prefer 3
  1601     proof fix g::"('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> bool" and i::"('a) \<times> (('a) set)"
  1602       assume "i \<in> (\<lambda>(x, k). (x, g k)) ` p - {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
  1603       then obtain x k where xk:"i=(x,g k)" "(x,k)\<in>p" "(x,g k) \<notin> {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}" by auto
  1604       have "content (g k) = 0" using xk using content_empty by auto
  1605       thus "(\<lambda>(x, k). content k *\<^sub>R f x) i = 0" unfolding xk split_conv by auto
  1606     qed auto
  1607     have lem4:"\<And>g. (\<lambda>(x,l). content (g l) *\<^sub>R f x) = (\<lambda>(x,l). content l *\<^sub>R f x) o (\<lambda>(x,l). (x,g l))" apply(rule ext) by auto
  1608 
  1609     let ?M1 = "{(x,kk \<inter> {x. x$$k \<le> c}) |x kk. (x,kk) \<in> p \<and> kk \<inter> {x. x$$k \<le> c} \<noteq> {}}"
  1610     have "norm ((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) < e/2" apply(rule d1(2),rule tagged_division_ofI)
  1611       apply(rule lem2 p(3))+ prefer 6 apply(rule fineI)
  1612     proof- show "\<Union>{k. \<exists>x. (x, k) \<in> ?M1} = {a..b} \<inter> {x. x$$k \<le> c}" unfolding p(8)[THEN sym] by auto
  1613       fix x l assume xl:"(x,l)\<in>?M1"
  1614       then guess x' l' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note xl'=this
  1615       have "l' \<subseteq> d1 x'" apply(rule order_trans[OF fineD[OF p(2) xl'(3)]]) by auto
  1616       thus "l \<subseteq> d1 x" unfolding xl' by auto
  1617       show "x\<in>l" "l \<subseteq> {a..b} \<inter> {x. x $$ k \<le> c}" unfolding xl' using p(4-6)[OF xl'(3)] using xl'(4)
  1618         using lem0(1)[OF xl'(3-4)] by auto
  1619       show  "\<exists>a b. l = {a..b}" unfolding xl' using p(6)[OF xl'(3)] by(fastsimp simp add: interval_split[OF k,where c=c])
  1620       fix y r let ?goal = "interior l \<inter> interior r = {}" assume yr:"(y,r)\<in>?M1"
  1621       then guess y' r' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note yr'=this
  1622       assume as:"(x,l) \<noteq> (y,r)" show "interior l \<inter> interior r = {}"
  1623       proof(cases "l' = r' \<longrightarrow> x' = y'")
  1624         case False thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1625       next case True hence "l' \<noteq> r'" using as unfolding xl' yr' by auto
  1626         thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1627       qed qed moreover
  1628 
  1629     let ?M2 = "{(x,kk \<inter> {x. x$$k \<ge> c}) |x kk. (x,kk) \<in> p \<and> kk \<inter> {x. x$$k \<ge> c} \<noteq> {}}" 
  1630     have "norm ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) < e/2" apply(rule d2(2),rule tagged_division_ofI)
  1631       apply(rule lem2 p(3))+ prefer 6 apply(rule fineI)
  1632     proof- show "\<Union>{k. \<exists>x. (x, k) \<in> ?M2} = {a..b} \<inter> {x. x$$k \<ge> c}" unfolding p(8)[THEN sym] by auto
  1633       fix x l assume xl:"(x,l)\<in>?M2"
  1634       then guess x' l' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note xl'=this
  1635       have "l' \<subseteq> d2 x'" apply(rule order_trans[OF fineD[OF p(2) xl'(3)]]) by auto
  1636       thus "l \<subseteq> d2 x" unfolding xl' by auto
  1637       show "x\<in>l" "l \<subseteq> {a..b} \<inter> {x. x $$ k \<ge> c}" unfolding xl' using p(4-6)[OF xl'(3)] using xl'(4)
  1638         using lem0(2)[OF xl'(3-4)] by auto
  1639       show  "\<exists>a b. l = {a..b}" unfolding xl' using p(6)[OF xl'(3)] by(fastsimp simp add: interval_split[OF k, where c=c])
  1640       fix y r let ?goal = "interior l \<inter> interior r = {}" assume yr:"(y,r)\<in>?M2"
  1641       then guess y' r' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note yr'=this
  1642       assume as:"(x,l) \<noteq> (y,r)" show "interior l \<inter> interior r = {}"
  1643       proof(cases "l' = r' \<longrightarrow> x' = y'")
  1644         case False thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1645       next case True hence "l' \<noteq> r'" using as unfolding xl' yr' by auto
  1646         thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1647       qed qed ultimately
  1648 
  1649     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"
  1650       apply- apply(rule norm_triangle_lt) by auto
  1651     also { have *:"\<And>x y. x = (0::real) \<Longrightarrow> x *\<^sub>R (y::'b) = 0" using scaleR_zero_left by auto
  1652       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)
  1653        = (\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - (i + j)" by auto
  1654       also have "\<dots> = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. x $$ k \<le> c}) *\<^sub>R f x) +
  1655         (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. c \<le> x $$ k}) *\<^sub>R f x) - (i + j)"
  1656         unfolding lem3[OF p(3)] apply(subst setsum_reindex_nonzero[OF p(3)]) defer apply(subst setsum_reindex_nonzero[OF p(3)])
  1657         defer unfolding lem4[THEN sym] apply(rule refl) unfolding split_paired_all split_conv apply(rule_tac[!] *)
  1658       proof- case goal1 thus ?case apply- apply(rule tagged_division_split_left_inj [OF p(1), of a b aa ba]) using k by auto
  1659       next case   goal2 thus ?case apply- apply(rule tagged_division_split_right_inj[OF p(1), of a b aa ba]) using k by auto
  1660       qed also note setsum_addf[THEN sym]
  1661       also have *:"\<And>x. x\<in>p \<Longrightarrow> (\<lambda>(x, ka). content (ka \<inter> {x. x $$ k \<le> c}) *\<^sub>R f x) x + (\<lambda>(x, ka). content (ka \<inter> {x. c \<le> x $$ k}) *\<^sub>R f x) x
  1662         = (\<lambda>(x,ka). content ka *\<^sub>R f x) x" unfolding split_paired_all split_conv
  1663       proof- fix a b assume "(a,b) \<in> p" from p(6)[OF this] guess u v apply-by(erule exE)+ note uv=this
  1664         thus "content (b \<inter> {x. x $$ k \<le> c}) *\<^sub>R f a + content (b \<inter> {x. c \<le> x $$ k}) *\<^sub>R f a = content b *\<^sub>R f a"
  1665           unfolding scaleR_left_distrib[THEN sym] unfolding uv content_split[OF k,of u v c] by auto
  1666       qed note setsum_cong2[OF this]
  1667       finally have "(\<Sum>(x, k)\<in>{(x, kk \<inter> {x. x $$ k \<le> c}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. x $$ k \<le> c} \<noteq> {}}. content k *\<^sub>R f x) - i +
  1668         ((\<Sum>(x, k)\<in>{(x, kk \<inter> {x. c \<le> x $$ k}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. c \<le> x $$ k} \<noteq> {}}. content k *\<^sub>R f x) - j) =
  1669         (\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f x) - (i + j)" by auto }
  1670     finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (i + j)) < e" by auto qed qed
  1671 
  1672 (*lemma has_integral_split_cart: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
  1673   assumes "(f has_integral i) ({a..b} \<inter> {x. x$k \<le> c})"  "(f has_integral j) ({a..b} \<inter> {x. x$k \<ge> c})"
  1674   shows "(f has_integral (i + j)) ({a..b})" *)
  1675 
  1676 subsection {* A sort of converse, integrability on subintervals. *}
  1677 
  1678 lemma tagged_division_union_interval: fixes a::"'a::ordered_euclidean_space"
  1679   assumes "p1 tagged_division_of ({a..b} \<inter> {x. x$$k \<le> (c::real)})"  "p2 tagged_division_of ({a..b} \<inter> {x. x$$k \<ge> c})"
  1680   and k:"k<DIM('a)"
  1681   shows "(p1 \<union> p2) tagged_division_of ({a..b})"
  1682 proof- have *:"{a..b} = ({a..b} \<inter> {x. x$$k \<le> c}) \<union> ({a..b} \<inter> {x. x$$k \<ge> c})" by auto
  1683   show ?thesis apply(subst *) apply(rule tagged_division_union[OF assms(1-2)])
  1684     unfolding interval_split[OF k] interior_closed_interval using k
  1685     by(auto simp add: eucl_less[where 'a='a] elim!:allE[where x=k]) qed
  1686 
  1687 lemma has_integral_separate_sides: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::real_normed_vector"
  1688   assumes "(f has_integral i) ({a..b})" "e>0" and k:"k<DIM('a)"
  1689   obtains d where "gauge d" "(\<forall>p1 p2. p1 tagged_division_of ({a..b} \<inter> {x. x$$k \<le> c}) \<and> d fine p1 \<and>
  1690                                 p2 tagged_division_of ({a..b} \<inter> {x. x$$k \<ge> c}) \<and> d fine p2
  1691                                 \<longrightarrow> norm((setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 +
  1692                                           setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) - i) < e)"
  1693 proof- guess d using has_integralD[OF assms(1-2)] . note d=this
  1694   show ?thesis apply(rule that[of d]) apply(rule d) apply(rule,rule,rule,(erule conjE)+)
  1695   proof- fix p1 p2 assume "p1 tagged_division_of {a..b} \<inter> {x. x $$ k \<le> c}" "d fine p1" note p1=tagged_division_ofD[OF this(1)] this
  1696                    assume "p2 tagged_division_of {a..b} \<inter> {x. c \<le> x $$ k}" "d fine p2" note p2=tagged_division_ofD[OF this(1)] this
  1697     note tagged_division_union_interval[OF p1(7) p2(7)] note p12 = tagged_division_ofD[OF this] this
  1698     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) = norm ((\<Sum>(x, k)\<in>p1 \<union> p2. content k *\<^sub>R f x) - i)"
  1699       apply(subst setsum_Un_zero) apply(rule p1 p2)+ apply(rule) unfolding split_paired_all split_conv
  1700     proof- fix a b assume ab:"(a,b) \<in> p1 \<inter> p2"
  1701       have "(a,b) \<in> p1" using ab by auto from p1(4)[OF this] guess u v apply-by(erule exE)+ note uv =this
  1702       have "b \<subseteq> {x. x$$k = c}" using ab p1(3)[of a b] p2(3)[of a b] by fastsimp
  1703       moreover have "interior {x::'a. x $$ k = c} = {}" 
  1704       proof(rule ccontr) case goal1 then obtain x where x:"x\<in>interior {x::'a. x$$k = c}" by auto
  1705         then guess e unfolding mem_interior .. note e=this
  1706         have x:"x$$k = c" using x interior_subset by fastsimp
  1707         have *:"\<And>i. i<DIM('a) \<Longrightarrow> \<bar>(x - (x + (\<chi>\<chi> i. if i = k then e / 2 else 0))) $$ i\<bar>
  1708           = (if i = k then e/2 else 0)" using e by auto
  1709         have "(\<Sum>i<DIM('a). \<bar>(x - (x + (\<chi>\<chi> i. if i = k then e / 2 else 0))) $$ i\<bar>) =
  1710           (\<Sum>i<DIM('a). (if i = k then e / 2 else 0))" apply(rule setsum_cong2) apply(subst *) by auto
  1711         also have "... < e" apply(subst setsum_delta) using e by auto 
  1712         finally have "x + (\<chi>\<chi> i. if i = k then e/2 else 0) \<in> ball x e" unfolding mem_ball dist_norm
  1713           by(rule le_less_trans[OF norm_le_l1])
  1714         hence "x + (\<chi>\<chi> i. if i = k then e/2 else 0) \<in> {x. x$$k = c}" using e by auto
  1715         thus False unfolding mem_Collect_eq using e x k by auto
  1716       qed ultimately have "content b = 0" unfolding uv content_eq_0_interior apply-apply(drule subset_interior) by auto
  1717       thus "content b *\<^sub>R f a = 0" by auto
  1718     qed auto
  1719     also have "\<dots> < e" by(rule k d(2) p12 fine_union p1 p2)+
  1720     finally show "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" . qed qed
  1721 
  1722 lemma integrable_split[intro]: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::{real_normed_vector,complete_space}"
  1723   assumes "f integrable_on {a..b}" and k:"k<DIM('a)"
  1724   shows "f integrable_on ({a..b} \<inter> {x. x$$k \<le> c})" (is ?t1) and "f integrable_on ({a..b} \<inter> {x. x$$k \<ge> c})" (is ?t2) 
  1725 proof- guess y using assms(1) unfolding integrable_on_def .. note y=this
  1726   def b' \<equiv> "(\<chi>\<chi> i. if i = k then min (b$$k) c else b$$i)::'a"
  1727   and a' \<equiv> "(\<chi>\<chi> i. if i = k then max (a$$k) c else a$$i)::'a"
  1728   show ?t1 ?t2 unfolding interval_split[OF k] integrable_cauchy unfolding interval_split[THEN sym,OF k]
  1729   proof(rule_tac[!] allI impI)+ fix e::real assume "e>0" hence "e/2>0" by auto
  1730     from has_integral_separate_sides[OF y this k,of c] guess d . note d=this[rule_format]
  1731     let ?P = "\<lambda>A. \<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of {a..b} \<inter> A \<and> d fine p1
  1732       \<and> p2 tagged_division_of {a..b} \<inter> A \<and> d fine p2 \<longrightarrow>
  1733       norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e)"
  1734     show "?P {x. x $$ k \<le> c}" apply(rule_tac x=d in exI) apply(rule,rule d) apply(rule,rule,rule)
  1735     proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b} \<inter> {x. x $$ k \<le> c} \<and> d fine p1
  1736         \<and> p2 tagged_division_of {a..b} \<inter> {x. x $$ k \<le> c} \<and> d fine p2"
  1737       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"
  1738       proof- guess p using fine_division_exists[OF d(1), of a' b] . note p=this
  1739         show ?thesis using norm_triangle_half_l[OF d(2)[of p1 p] d(2)[of p2 p]]
  1740           using as unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  1741           using p using assms by(auto simp add:algebra_simps)
  1742       qed qed  
  1743     show "?P {x. x $$ k \<ge> c}" apply(rule_tac x=d in exI) apply(rule,rule d) apply(rule,rule,rule)
  1744     proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b} \<inter> {x. x $$ k \<ge> c} \<and> d fine p1
  1745         \<and> p2 tagged_division_of {a..b} \<inter> {x. x $$ k \<ge> c} \<and> d fine p2"
  1746       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"
  1747       proof- guess p using fine_division_exists[OF d(1), of a b'] . note p=this
  1748         show ?thesis using norm_triangle_half_l[OF d(2)[of p p1] d(2)[of p p2]]
  1749           using as unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  1750           using p using assms by(auto simp add:algebra_simps) qed qed qed qed
  1751 
  1752 subsection {* Generalized notion of additivity. *}
  1753 
  1754 definition "neutral opp = (SOME x. \<forall>y. opp x y = y \<and> opp y x = y)"
  1755 
  1756 definition operative :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> (('b::ordered_euclidean_space) set \<Rightarrow> 'a) \<Rightarrow> bool" where
  1757   "operative opp f \<equiv> 
  1758     (\<forall>a b. content {a..b} = 0 \<longrightarrow> f {a..b} = neutral(opp)) \<and>
  1759     (\<forall>a b c. \<forall>k<DIM('b). f({a..b}) =
  1760                    opp (f({a..b} \<inter> {x. x$$k \<le> c}))
  1761                        (f({a..b} \<inter> {x. x$$k \<ge> c})))"
  1762 
  1763 lemma operativeD[dest]: fixes type::"'a::ordered_euclidean_space"  assumes "operative opp f"
  1764   shows "\<And>a b. content {a..b} = 0 \<Longrightarrow> f {a..b::'a} = neutral(opp)"
  1765   "\<And>a b c k. k<DIM('a) \<Longrightarrow> f({a..b}) = opp (f({a..b} \<inter> {x. x$$k \<le> c})) (f({a..b} \<inter> {x. x$$k \<ge> c}))"
  1766   using assms unfolding operative_def by auto
  1767 
  1768 lemma operative_trivial:
  1769  "operative opp f \<Longrightarrow> content({a..b}) = 0 \<Longrightarrow> f({a..b}) = neutral opp"
  1770   unfolding operative_def by auto
  1771 
  1772 lemma property_empty_interval:
  1773  "(\<forall>a b. content({a..b}) = 0 \<longrightarrow> P({a..b})) \<Longrightarrow> P {}" 
  1774   using content_empty unfolding empty_as_interval by auto
  1775 
  1776 lemma operative_empty: "operative opp f \<Longrightarrow> f {} = neutral opp"
  1777   unfolding operative_def apply(rule property_empty_interval) by auto
  1778 
  1779 subsection {* Using additivity of lifted function to encode definedness. *}
  1780 
  1781 lemma forall_option: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P(Some x))"
  1782   by (metis option.nchotomy)
  1783 
  1784 lemma exists_option:
  1785  "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P(Some x))" 
  1786   by (metis option.nchotomy)
  1787 
  1788 fun lifted where 
  1789   "lifted (opp::'a\<Rightarrow>'a\<Rightarrow>'b) (Some x) (Some y) = Some(opp x y)" |
  1790   "lifted opp None _ = (None::'b option)" |
  1791   "lifted opp _ None = None"
  1792 
  1793 lemma lifted_simp_1[simp]: "lifted opp v None = None"
  1794   apply(induct v) by auto
  1795 
  1796 definition "monoidal opp \<equiv>  (\<forall>x y. opp x y = opp y x) \<and>
  1797                    (\<forall>x y z. opp x (opp y z) = opp (opp x y) z) \<and>
  1798                    (\<forall>x. opp (neutral opp) x = x)"
  1799 
  1800 lemma monoidalI: assumes "\<And>x y. opp x y = opp y x"
  1801   "\<And>x y z. opp x (opp y z) = opp (opp x y) z"
  1802   "\<And>x. opp (neutral opp) x = x" shows "monoidal opp"
  1803   unfolding monoidal_def using assms by fastsimp
  1804 
  1805 lemma monoidal_ac: assumes "monoidal opp"
  1806   shows "opp (neutral opp) a = a" "opp a (neutral opp) = a" "opp a b = opp b a"
  1807   "opp (opp a b) c = opp a (opp b c)"  "opp a (opp b c) = opp b (opp a c)"
  1808   using assms unfolding monoidal_def apply- by metis+
  1809 
  1810 lemma monoidal_simps[simp]: assumes "monoidal opp"
  1811   shows "opp (neutral opp) a = a" "opp a (neutral opp) = a"
  1812   using monoidal_ac[OF assms] by auto
  1813 
  1814 lemma neutral_lifted[cong]: assumes "monoidal opp"
  1815   shows "neutral (lifted opp) = Some(neutral opp)"
  1816   apply(subst neutral_def) apply(rule some_equality) apply(rule,induct_tac y) prefer 3
  1817 proof- fix x assume "\<forall>y. lifted opp x y = y \<and> lifted opp y x = y"
  1818   thus "x = Some (neutral opp)" apply(induct x) defer
  1819     apply rule apply(subst neutral_def) apply(subst eq_commute,rule some_equality)
  1820     apply(rule,erule_tac x="Some y" in allE) defer apply(erule_tac x="Some x" in allE) by auto
  1821 qed(auto simp add:monoidal_ac[OF assms])
  1822 
  1823 lemma monoidal_lifted[intro]: assumes "monoidal opp" shows "monoidal(lifted opp)"
  1824   unfolding monoidal_def forall_option neutral_lifted[OF assms] using monoidal_ac[OF assms] by auto
  1825 
  1826 definition "support opp f s = {x. x\<in>s \<and> f x \<noteq> neutral opp}"
  1827 definition "fold' opp e s \<equiv> (if finite s then fold opp e s else e)"
  1828 definition "iterate opp s f \<equiv> fold' (\<lambda>x a. opp (f x) a) (neutral opp) (support opp f s)"
  1829 
  1830 lemma support_subset[intro]:"support opp f s \<subseteq> s" unfolding support_def by auto
  1831 lemma support_empty[simp]:"support opp f {} = {}" using support_subset[of opp f "{}"] by auto
  1832 
  1833 lemma fun_left_comm_monoidal[intro]: assumes "monoidal opp" shows "fun_left_comm opp"
  1834   unfolding fun_left_comm_def using monoidal_ac[OF assms] by auto
  1835 
  1836 lemma support_clauses:
  1837   "\<And>f g s. support opp f {} = {}"
  1838   "\<And>f g s. support opp f (insert x s) = (if f(x) = neutral opp then support opp f s else insert x (support opp f s))"
  1839   "\<And>f g s. support opp f (s - {x}) = (support opp f s) - {x}"
  1840   "\<And>f g s. support opp f (s \<union> t) = (support opp f s) \<union> (support opp f t)"
  1841   "\<And>f g s. support opp f (s \<inter> t) = (support opp f s) \<inter> (support opp f t)"
  1842   "\<And>f g s. support opp f (s - t) = (support opp f s) - (support opp f t)"
  1843   "\<And>f g s. support opp g (f ` s) = f ` (support opp (g o f) s)"
  1844 unfolding support_def by auto
  1845 
  1846 lemma finite_support[intro]:"finite s \<Longrightarrow> finite (support opp f s)"
  1847   unfolding support_def by auto
  1848 
  1849 lemma iterate_empty[simp]:"iterate opp {} f = neutral opp"
  1850   unfolding iterate_def fold'_def by auto 
  1851 
  1852 lemma iterate_insert[simp]: assumes "monoidal opp" "finite s"
  1853   shows "iterate opp (insert x s) f = (if x \<in> s then iterate opp s f else opp (f x) (iterate opp s f))" 
  1854 proof(cases "x\<in>s") case True hence *:"insert x s = s" by auto
  1855   show ?thesis unfolding iterate_def if_P[OF True] * by auto
  1856 next case False note x=this
  1857   note * = fun_left_comm.fun_left_comm_apply[OF fun_left_comm_monoidal[OF assms(1)]]
  1858   show ?thesis proof(cases "f x = neutral opp")
  1859     case True show ?thesis unfolding iterate_def if_not_P[OF x] support_clauses if_P[OF True]
  1860       unfolding True monoidal_simps[OF assms(1)] by auto
  1861   next case False show ?thesis unfolding iterate_def fold'_def  if_not_P[OF x] support_clauses if_not_P[OF False]
  1862       apply(subst fun_left_comm.fold_insert[OF * finite_support])
  1863       using `finite s` unfolding support_def using False x by auto qed qed 
  1864 
  1865 lemma iterate_some:
  1866   assumes "monoidal opp"  "finite s"
  1867   shows "iterate (lifted opp) s (\<lambda>x. Some(f x)) = Some (iterate opp s f)" using assms(2)
  1868 proof(induct s) case empty thus ?case using assms by auto
  1869 next case (insert x F) show ?case apply(subst iterate_insert) prefer 3 apply(subst if_not_P)
  1870     defer unfolding insert(3) lifted.simps apply rule using assms insert by auto qed
  1871 subsection {* Two key instances of additivity. *}
  1872 
  1873 lemma neutral_add[simp]:
  1874   "neutral op + = (0::_::comm_monoid_add)" unfolding neutral_def 
  1875   apply(rule some_equality) defer apply(erule_tac x=0 in allE) by auto
  1876 
  1877 lemma operative_content[intro]: "operative (op +) content" 
  1878   unfolding operative_def neutral_add apply safe 
  1879   unfolding content_split[THEN sym] ..
  1880 
  1881 lemma neutral_monoid: "neutral ((op +)::('a::comm_monoid_add) \<Rightarrow> 'a \<Rightarrow> 'a) = 0"
  1882   by (rule neutral_add) (* FIXME: duplicate *)
  1883 
  1884 lemma monoidal_monoid[intro]:
  1885   shows "monoidal ((op +)::('a::comm_monoid_add) \<Rightarrow> 'a \<Rightarrow> 'a)"
  1886   unfolding monoidal_def neutral_monoid by(auto simp add: algebra_simps) 
  1887 
  1888 lemma operative_integral: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  1889   shows "operative (lifted(op +)) (\<lambda>i. if f integrable_on i then Some(integral i f) else None)"
  1890   unfolding operative_def unfolding neutral_lifted[OF monoidal_monoid] neutral_add
  1891   apply(rule,rule,rule,rule) defer apply(rule allI impI)+
  1892 proof- fix a b c k assume k:"k<DIM('a)" show "(if f integrable_on {a..b} then Some (integral {a..b} f) else None) =
  1893     lifted op + (if f integrable_on {a..b} \<inter> {x. x $$ k \<le> c} then Some (integral ({a..b} \<inter> {x. x $$ k \<le> c}) f) else None)
  1894     (if f integrable_on {a..b} \<inter> {x. c \<le> x $$ k} then Some (integral ({a..b} \<inter> {x. c \<le> x $$ k}) f) else None)"
  1895   proof(cases "f integrable_on {a..b}") 
  1896     case True show ?thesis unfolding if_P[OF True] using k apply-
  1897       unfolding if_P[OF integrable_split(1)[OF True]] unfolding if_P[OF integrable_split(2)[OF True]]
  1898       unfolding lifted.simps option.inject apply(rule integral_unique) apply(rule has_integral_split[OF _ _ k]) 
  1899       apply(rule_tac[!] integrable_integral integrable_split)+ using True k by auto
  1900   next case False have "(\<not> (f integrable_on {a..b} \<inter> {x. x $$ k \<le> c})) \<or> (\<not> ( f integrable_on {a..b} \<inter> {x. c \<le> x $$ k}))"
  1901     proof(rule ccontr) case goal1 hence "f integrable_on {a..b}" apply- unfolding integrable_on_def
  1902         apply(rule_tac x="integral ({a..b} \<inter> {x. x $$ k \<le> c}) f + integral ({a..b} \<inter> {x. x $$ k \<ge> c}) f" in exI)
  1903         apply(rule has_integral_split[OF _ _ k]) apply(rule_tac[!] integrable_integral) by auto
  1904       thus False using False by auto
  1905     qed thus ?thesis using False by auto 
  1906   qed next 
  1907   fix a b assume as:"content {a..b::'a} = 0"
  1908   thus "(if f integrable_on {a..b} then Some (integral {a..b} f) else None) = Some 0"
  1909     unfolding if_P[OF integrable_on_null[OF as]] using has_integral_null_eq[OF as] by auto qed
  1910 
  1911 subsection {* Points of division of a partition. *}
  1912 
  1913 definition "division_points (k::('a::ordered_euclidean_space) set) d = 
  1914     {(j,x). j<DIM('a) \<and> (interval_lowerbound k)$$j < x \<and> x < (interval_upperbound k)$$j \<and>
  1915            (\<exists>i\<in>d. (interval_lowerbound i)$$j = x \<or> (interval_upperbound i)$$j = x)}"
  1916 
  1917 lemma division_points_finite: fixes i::"('a::ordered_euclidean_space) set"
  1918   assumes "d division_of i" shows "finite (division_points i d)"
  1919 proof- note assm = division_ofD[OF assms]
  1920   let ?M = "\<lambda>j. {(j,x)|x. (interval_lowerbound i)$$j < x \<and> x < (interval_upperbound i)$$j \<and>
  1921            (\<exists>i\<in>d. (interval_lowerbound i)$$j = x \<or> (interval_upperbound i)$$j = x)}"
  1922   have *:"division_points i d = \<Union>(?M ` {..<DIM('a)})"
  1923     unfolding division_points_def by auto
  1924   show ?thesis unfolding * using assm by auto qed
  1925 
  1926 lemma division_points_subset: fixes a::"'a::ordered_euclidean_space"
  1927   assumes "d division_of {a..b}" "\<forall>i<DIM('a). a$$i < b$$i"  "a$$k < c" "c < b$$k" and k:"k<DIM('a)"
  1928   shows "division_points ({a..b} \<inter> {x. x$$k \<le> c}) {l \<inter> {x. x$$k \<le> c} | l . l \<in> d \<and> ~(l \<inter> {x. x$$k \<le> c} = {})}
  1929                   \<subseteq> division_points ({a..b}) d" (is ?t1) and
  1930         "division_points ({a..b} \<inter> {x. x$$k \<ge> c}) {l \<inter> {x. x$$k \<ge> c} | l . l \<in> d \<and> ~(l \<inter> {x. x$$k \<ge> c} = {})}
  1931                   \<subseteq> division_points ({a..b}) d" (is ?t2)
  1932 proof- note assm = division_ofD[OF assms(1)]
  1933   have *:"\<forall>i<DIM('a). a$$i \<le> b$$i"   "\<forall>i<DIM('a). a$$i \<le> ((\<chi>\<chi> i. if i = k then min (b $$ k) c else b $$ i)::'a) $$ i"
  1934     "\<forall>i<DIM('a). ((\<chi>\<chi> i. if i = k then max (a $$ k) c else a $$ i)::'a) $$ i \<le> b$$i"  "min (b $$ k) c = c" "max (a $$ k) c = c"
  1935     using assms using less_imp_le by auto
  1936   show ?t1 unfolding division_points_def interval_split[OF k, of a b]
  1937     unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)] unfolding *
  1938     unfolding subset_eq apply(rule) unfolding mem_Collect_eq split_beta apply(erule bexE conjE)+
  1939     unfolding mem_Collect_eq apply(erule exE conjE)+ unfolding euclidean_lambda_beta'
  1940   proof- fix i l x assume as:"a $$ fst x < snd x" "snd x < (if fst x = k then c else b $$ fst x)"
  1941       "interval_lowerbound i $$ fst x = snd x \<or> interval_upperbound i $$ fst x = snd x"
  1942       "i = l \<inter> {x. x $$ k \<le> c}" "l \<in> d" "l \<inter> {x. x $$ k \<le> c} \<noteq> {}" and fstx:"fst x <DIM('a)"
  1943     from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  1944     have *:"\<forall>i<DIM('a). u $$ i \<le> ((\<chi>\<chi> i. if i = k then min (v $$ k) c else v $$ i)::'a) $$ i"
  1945       using as(6) unfolding l interval_split[OF k] interval_ne_empty as .
  1946     have **:"\<forall>i<DIM('a). u$$i \<le> v$$i" using l using as(6) unfolding interval_ne_empty[THEN sym] by auto
  1947     show "fst x <DIM('a) \<and> a $$ fst x < snd x \<and> snd x < b $$ fst x \<and> (\<exists>i\<in>d. interval_lowerbound i $$ fst x = snd x
  1948       \<or> interval_upperbound i $$ fst x = snd x)" apply(rule,rule fstx)
  1949       using as(1-3,5) unfolding l interval_split[OF k] interval_ne_empty as interval_bounds[OF *] apply-
  1950       apply(rule,assumption,rule) defer apply(rule_tac x="{u..v}" in bexI) unfolding interval_bounds[OF **]
  1951       apply(case_tac[!] "fst x = k") using assms fstx apply- unfolding euclidean_lambda_beta by auto
  1952   qed
  1953   show ?t2 unfolding division_points_def interval_split[OF k, of a b]
  1954     unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)] unfolding *
  1955     unfolding subset_eq apply(rule) unfolding mem_Collect_eq split_beta apply(erule bexE conjE)+
  1956     unfolding mem_Collect_eq apply(erule exE conjE)+ unfolding euclidean_lambda_beta' apply(rule,assumption)
  1957   proof- fix i l x assume as:"(if fst x = k then c else a $$ fst x) < snd x" "snd x < b $$ fst x"
  1958       "interval_lowerbound i $$ fst x = snd x \<or> interval_upperbound i $$ fst x = snd x" 
  1959       "i = l \<inter> {x. c \<le> x $$ k}" "l \<in> d" "l \<inter> {x. c \<le> x $$ k} \<noteq> {}" and fstx:"fst x < DIM('a)"
  1960     from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  1961     have *:"\<forall>i<DIM('a). ((\<chi>\<chi> i. if i = k then max (u $$ k) c else u $$ i)::'a) $$ i \<le> v $$ i"
  1962       using as(6) unfolding l interval_split[OF k] interval_ne_empty as .
  1963     have **:"\<forall>i<DIM('a). u$$i \<le> v$$i" using l using as(6) unfolding interval_ne_empty[THEN sym] by auto
  1964     show "a $$ fst x < snd x \<and> snd x < b $$ fst x \<and> (\<exists>i\<in>d. interval_lowerbound i $$ fst x = snd x \<or>
  1965       interval_upperbound i $$ fst x = snd x)"
  1966       using as(1-3,5) unfolding l interval_split[OF k] interval_ne_empty as interval_bounds[OF *] apply-
  1967       apply rule defer apply(rule,assumption) apply(rule_tac x="{u..v}" in bexI) unfolding interval_bounds[OF **]
  1968       apply(case_tac[!] "fst x = k") using assms fstx apply-  by(auto simp add:euclidean_lambda_beta'[OF k]) qed qed
  1969 
  1970 lemma division_points_psubset: fixes a::"'a::ordered_euclidean_space"
  1971   assumes "d division_of {a..b}"  "\<forall>i<DIM('a). a$$i < b$$i"  "a$$k < c" "c < b$$k"
  1972   "l \<in> d" "interval_lowerbound l$$k = c \<or> interval_upperbound l$$k = c" and k:"k<DIM('a)"
  1973   shows "division_points ({a..b} \<inter> {x. x$$k \<le> c}) {l \<inter> {x. x$$k \<le> c} | l. l\<in>d \<and> l \<inter> {x. x$$k \<le> c} \<noteq> {}}
  1974               \<subset> division_points ({a..b}) d" (is "?D1 \<subset> ?D") 
  1975         "division_points ({a..b} \<inter> {x. x$$k \<ge> c}) {l \<inter> {x. x$$k \<ge> c} | l. l\<in>d \<and> l \<inter> {x. x$$k \<ge> c} \<noteq> {}}
  1976               \<subset> division_points ({a..b}) d" (is "?D2 \<subset> ?D") 
  1977 proof- have ab:"\<forall>i<DIM('a). a$$i \<le> b$$i" using assms(2) by(auto intro!:less_imp_le)
  1978   guess u v using division_ofD(4)[OF assms(1,5)] apply-by(erule exE)+ note l=this
  1979   have uv:"\<forall>i<DIM('a). u$$i \<le> v$$i" "\<forall>i<DIM('a). a$$i \<le> u$$i \<and> v$$i \<le> b$$i"
  1980     using division_ofD(2,2,3)[OF assms(1,5)] unfolding l interval_ne_empty
  1981     unfolding subset_eq apply- defer apply(erule_tac x=u in ballE, erule_tac x=v in ballE) unfolding mem_interval by auto
  1982   have *:"interval_upperbound ({a..b} \<inter> {x. x $$ k \<le> interval_upperbound l $$ k}) $$ k = interval_upperbound l $$ k"
  1983          "interval_upperbound ({a..b} \<inter> {x. x $$ k \<le> interval_lowerbound l $$ k}) $$ k = interval_lowerbound l $$ k"
  1984     unfolding interval_split[OF k] apply(subst interval_bounds) prefer 3 apply(subst interval_bounds)
  1985     unfolding l interval_bounds[OF uv(1)] using uv[rule_format,of k] ab k by auto
  1986   have "\<exists>x. x \<in> ?D - ?D1" using assms(2-) apply-apply(erule disjE)
  1987     apply(rule_tac x="(k,(interval_lowerbound l)$$k)" in exI) defer
  1988     apply(rule_tac x="(k,(interval_upperbound l)$$k)" in exI)
  1989     unfolding division_points_def unfolding interval_bounds[OF ab] by(auto simp add:*) 
  1990   thus "?D1 \<subset> ?D" apply-apply(rule,rule division_points_subset[OF assms(1-4)]) using k by auto
  1991 
  1992   have *:"interval_lowerbound ({a..b} \<inter> {x. x $$ k \<ge> interval_lowerbound l $$ k}) $$ k = interval_lowerbound l $$ k"
  1993          "interval_lowerbound ({a..b} \<inter> {x. x $$ k \<ge> interval_upperbound l $$ k}) $$ k = interval_upperbound l $$ k"
  1994     unfolding interval_split[OF k] apply(subst interval_bounds) prefer 3 apply(subst interval_bounds)
  1995     unfolding l interval_bounds[OF uv(1)] using uv[rule_format,of k] ab k by auto
  1996   have "\<exists>x. x \<in> ?D - ?D2" using assms(2-) apply-apply(erule disjE)
  1997     apply(rule_tac x="(k,(interval_lowerbound l)$$k)" in exI) defer
  1998     apply(rule_tac x="(k,(interval_upperbound l)$$k)" in exI)
  1999     unfolding division_points_def unfolding interval_bounds[OF ab] by(auto simp add:*) 
  2000   thus "?D2 \<subset> ?D" apply-apply(rule,rule division_points_subset[OF assms(1-4) k]) by auto qed
  2001 
  2002 subsection {* Preservation by divisions and tagged divisions. *}
  2003 
  2004 lemma support_support[simp]:"support opp f (support opp f s) = support opp f s"
  2005   unfolding support_def by auto
  2006 
  2007 lemma iterate_support[simp]: "iterate opp (support opp f s) f = iterate opp s f"
  2008   unfolding iterate_def support_support by auto
  2009 
  2010 lemma iterate_expand_cases:
  2011   "iterate opp s f = (if finite(support opp f s) then iterate opp (support opp f s) f else neutral opp)"
  2012   apply(cases) apply(subst if_P,assumption) unfolding iterate_def support_support fold'_def by auto 
  2013 
  2014 lemma iterate_image: assumes "monoidal opp"  "inj_on f s"
  2015   shows "iterate opp (f ` s) g = iterate opp s (g \<circ> f)"
  2016 proof- have *:"\<And>s. finite s \<Longrightarrow>  \<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<longrightarrow> x = y \<Longrightarrow>
  2017      iterate opp (f ` s) g = iterate opp s (g \<circ> f)"
  2018   proof- case goal1 show ?case using goal1
  2019     proof(induct s) case empty thus ?case using assms(1) by auto
  2020     next case (insert x s) show ?case unfolding iterate_insert[OF assms(1) insert(1)]
  2021         unfolding if_not_P[OF insert(2)] apply(subst insert(3)[THEN sym])
  2022         unfolding image_insert defer apply(subst iterate_insert[OF assms(1)])
  2023         apply(rule finite_imageI insert)+ apply(subst if_not_P)
  2024         unfolding image_iff o_def using insert(2,4) by auto
  2025     qed qed
  2026   show ?thesis 
  2027     apply(cases "finite (support opp g (f ` s))")
  2028     apply(subst (1) iterate_support[THEN sym],subst (2) iterate_support[THEN sym])
  2029     unfolding support_clauses apply(rule *)apply(rule finite_imageD,assumption) unfolding inj_on_def[symmetric]
  2030     apply(rule subset_inj_on[OF assms(2) support_subset])+
  2031     apply(subst iterate_expand_cases) unfolding support_clauses apply(simp only: if_False)
  2032     apply(subst iterate_expand_cases) apply(subst if_not_P) by auto qed
  2033 
  2034 
  2035 (* This lemma about iterations comes up in a few places.                     *)
  2036 lemma iterate_nonzero_image_lemma:
  2037   assumes "monoidal opp" "finite s" "g(a) = neutral opp"
  2038   "\<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<and> x \<noteq> y \<longrightarrow> g(f x) = neutral opp"
  2039   shows "iterate opp {f x | x. x \<in> s \<and> f x \<noteq> a} g = iterate opp s (g \<circ> f)"
  2040 proof- have *:"{f x |x. x \<in> s \<and> ~(f x = a)} = f ` {x. x \<in> s \<and> ~(f x = a)}" by auto
  2041   have **:"support opp (g \<circ> f) {x \<in> s. f x \<noteq> a} = support opp (g \<circ> f) s"
  2042     unfolding support_def using assms(3) by auto
  2043   show ?thesis unfolding *
  2044     apply(subst iterate_support[THEN sym]) unfolding support_clauses
  2045     apply(subst iterate_image[OF assms(1)]) defer
  2046     apply(subst(2) iterate_support[THEN sym]) apply(subst **)
  2047     unfolding inj_on_def using assms(3,4) unfolding support_def by auto qed
  2048 
  2049 lemma iterate_eq_neutral:
  2050   assumes "monoidal opp"  "\<forall>x \<in> s. (f(x) = neutral opp)"
  2051   shows "(iterate opp s f = neutral opp)"
  2052 proof- have *:"support opp f s = {}" unfolding support_def using assms(2) by auto
  2053   show ?thesis apply(subst iterate_support[THEN sym]) 
  2054     unfolding * using assms(1) by auto qed
  2055 
  2056 lemma iterate_op: assumes "monoidal opp" "finite s"
  2057   shows "iterate opp s (\<lambda>x. opp (f x) (g x)) = opp (iterate opp s f) (iterate opp s g)" using assms(2)
  2058 proof(induct s) case empty thus ?case unfolding iterate_insert[OF assms(1)] using assms(1) by auto
  2059 next case (insert x F) show ?case unfolding iterate_insert[OF assms(1) insert(1)] if_not_P[OF insert(2)] insert(3)
  2060     unfolding monoidal_ac[OF assms(1)] by(rule refl) qed
  2061 
  2062 lemma iterate_eq: assumes "monoidal opp" "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  2063   shows "iterate opp s f = iterate opp s g"
  2064 proof- have *:"support opp g s = support opp f s"
  2065     unfolding support_def using assms(2) by auto
  2066   show ?thesis
  2067   proof(cases "finite (support opp f s)")
  2068     case False thus ?thesis apply(subst iterate_expand_cases,subst(2) iterate_expand_cases)
  2069       unfolding * by auto
  2070   next def su \<equiv> "support opp f s"
  2071     case True note support_subset[of opp f s] 
  2072     thus ?thesis apply- apply(subst iterate_support[THEN sym],subst(2) iterate_support[THEN sym]) unfolding * using True
  2073       unfolding su_def[symmetric]
  2074     proof(induct su) case empty show ?case by auto
  2075     next case (insert x s) show ?case unfolding iterate_insert[OF assms(1) insert(1)] 
  2076         unfolding if_not_P[OF insert(2)] apply(subst insert(3))
  2077         defer apply(subst assms(2)[of x]) using insert by auto qed qed qed
  2078 
  2079 lemma nonempty_witness: assumes "s \<noteq> {}" obtains x where "x \<in> s" using assms by auto
  2080 
  2081 lemma operative_division: fixes f::"('a::ordered_euclidean_space) set \<Rightarrow> 'b"
  2082   assumes "monoidal opp" "operative opp f" "d division_of {a..b}"
  2083   shows "iterate opp d f = f {a..b}"
  2084 proof- def C \<equiv> "card (division_points {a..b} d)" thus ?thesis using assms
  2085   proof(induct C arbitrary:a b d rule:full_nat_induct)
  2086     case goal1
  2087     { presume *:"content {a..b} \<noteq> 0 \<Longrightarrow> ?case"
  2088       thus ?case apply-apply(cases) defer apply assumption
  2089       proof- assume as:"content {a..b} = 0"
  2090         show ?case unfolding operativeD(1)[OF assms(2) as] apply(rule iterate_eq_neutral[OF goal1(2)])
  2091         proof fix x assume x:"x\<in>d"
  2092           then guess u v apply(drule_tac division_ofD(4)[OF goal1(4)]) by(erule exE)+
  2093           thus "f x = neutral opp" using division_of_content_0[OF as goal1(4)] 
  2094             using operativeD(1)[OF assms(2)] x by auto
  2095         qed qed }
  2096     assume "content {a..b} \<noteq> 0" note ab = this[unfolded content_lt_nz[THEN sym] content_pos_lt_eq]
  2097     hence ab':"\<forall>i<DIM('a). a$$i \<le> b$$i" by (auto intro!: less_imp_le) show ?case 
  2098     proof(cases "division_points {a..b} d = {}")
  2099       case True have d':"\<forall>i\<in>d. \<exists>u v. i = {u..v} \<and>
  2100         (\<forall>j<DIM('a). u$$j = a$$j \<and> v$$j = a$$j \<or> u$$j = b$$j \<and> v$$j = b$$j \<or> u$$j = a$$j \<and> v$$j = b$$j)"
  2101         unfolding forall_in_division[OF goal1(4)] apply(rule,rule,rule)
  2102         apply(rule_tac x=a in exI,rule_tac x=b in exI) apply(rule,rule refl) apply(rule,rule)
  2103       proof- fix u v j assume j:"j<DIM('a)" assume as:"{u..v} \<in> d" note division_ofD(3)[OF goal1(4) this]
  2104         hence uv:"\<forall>i<DIM('a). u$$i \<le> v$$i" "u$$j \<le> v$$j" using j unfolding interval_ne_empty by auto
  2105         have *:"\<And>p r Q. \<not> j<DIM('a) \<or> p \<or> r \<or> (\<forall>x\<in>d. Q x) \<Longrightarrow> p \<or> r \<or> (Q {u..v})" using as j by auto
  2106         have "(j, u$$j) \<notin> division_points {a..b} d"
  2107           "(j, v$$j) \<notin> division_points {a..b} d" using True by auto
  2108         note this[unfolded de_Morgan_conj division_points_def mem_Collect_eq split_conv interval_bounds[OF ab'] bex_simps]
  2109         note *[OF this(1)] *[OF this(2)] note this[unfolded interval_bounds[OF uv(1)]]
  2110         moreover have "a$$j \<le> u$$j" "v$$j \<le> b$$j" using division_ofD(2,2,3)[OF goal1(4) as] 
  2111           unfolding subset_eq apply- apply(erule_tac x=u in ballE,erule_tac[3] x=v in ballE)
  2112           unfolding interval_ne_empty mem_interval using j by auto
  2113         ultimately show "u$$j = a$$j \<and> v$$j = a$$j \<or> u$$j = b$$j \<and> v$$j = b$$j \<or> u$$j = a$$j \<and> v$$j = b$$j"
  2114           unfolding not_less de_Morgan_disj using ab[rule_format,of j] uv(2) j by auto
  2115       qed have "(1/2) *\<^sub>R (a+b) \<in> {a..b}" unfolding mem_interval using ab by(auto intro!:less_imp_le)
  2116       note this[unfolded division_ofD(6)[OF goal1(4),THEN sym] Union_iff]
  2117       then guess i .. note i=this guess u v using d'[rule_format,OF i(1)] apply-by(erule exE conjE)+ note uv=this
  2118       have "{a..b} \<in> d"
  2119       proof- { presume "i = {a..b}" thus ?thesis using i by auto }
  2120         { presume "u = a" "v = b" thus "i = {a..b}" using uv by auto }
  2121         show "u = a" "v = b" unfolding euclidean_eq[where 'a='a]
  2122         proof(safe) fix j assume j:"j<DIM('a)" note i(2)[unfolded uv mem_interval,rule_format,of j]
  2123           thus "u $$ j = a $$ j" "v $$ j = b $$ j" using uv(2)[rule_format,of j] j by auto
  2124         qed qed
  2125       hence *:"d = insert {a..b} (d - {{a..b}})" by auto
  2126       have "iterate opp (d - {{a..b}}) f = neutral opp" apply(rule iterate_eq_neutral[OF goal1(2)])
  2127       proof fix x assume x:"x \<in> d - {{a..b}}" hence "x\<in>d" by auto note d'[rule_format,OF this]
  2128         then guess u v apply-by(erule exE conjE)+ note uv=this
  2129         have "u\<noteq>a \<or> v\<noteq>b" using x[unfolded uv] by auto  
  2130         then obtain j where "u$$j \<noteq> a$$j \<or> v$$j \<noteq> b$$j" and j:"j<DIM('a)" unfolding euclidean_eq[where 'a='a] by auto
  2131         hence "u$$j = v$$j" using uv(2)[rule_format,OF j] by auto
  2132         hence "content {u..v} = 0"  unfolding content_eq_0 apply(rule_tac x=j in exI) using j by auto
  2133         thus "f x = neutral opp" unfolding uv(1) by(rule operativeD(1)[OF goal1(3)])
  2134       qed thus "iterate opp d f = f {a..b}" apply-apply(subst *) 
  2135         apply(subst iterate_insert[OF goal1(2)]) using goal1(2,4) by auto
  2136     next case False hence "\<exists>x. x\<in>division_points {a..b} d" by auto
  2137       then guess k c unfolding split_paired_Ex apply- unfolding division_points_def mem_Collect_eq split_conv
  2138         by(erule exE conjE)+ note this(2-4,1) note kc=this[unfolded interval_bounds[OF ab']]
  2139       from this(3) guess j .. note j=this
  2140       def d1 \<equiv> "{l \<inter> {x. x$$k \<le> c} | l. l \<in> d \<and> l \<inter> {x. x$$k \<le> c} \<noteq> {}}"
  2141       def d2 \<equiv> "{l \<inter> {x. x$$k \<ge> c} | l. l \<in> d \<and> l \<inter> {x. x$$k \<ge> c} \<noteq> {}}"
  2142       def cb \<equiv> "(\<chi>\<chi> i. if i = k then c else b$$i)::'a" and ca \<equiv> "(\<chi>\<chi> i. if i = k then c else a$$i)::'a"
  2143       note division_points_psubset[OF goal1(4) ab kc(1-2) j]
  2144       note psubset_card_mono[OF _ this(1)] psubset_card_mono[OF _ this(2)]
  2145       hence *:"(iterate opp d1 f) = f ({a..b} \<inter> {x. x$$k \<le> c})" "(iterate opp d2 f) = f ({a..b} \<inter> {x. x$$k \<ge> c})"
  2146         apply- unfolding interval_split[OF kc(4)] apply(rule_tac[!] goal1(1)[rule_format])
  2147         using division_split[OF goal1(4), where k=k and c=c]
  2148         unfolding interval_split[OF kc(4)] d1_def[symmetric] d2_def[symmetric] unfolding goal1(2) Suc_le_mono
  2149         using goal1(2-3) using division_points_finite[OF goal1(4)] using kc(4) by auto
  2150       have "f {a..b} = opp (iterate opp d1 f) (iterate opp d2 f)" (is "_ = ?prev")
  2151         unfolding * apply(rule operativeD(2)) using goal1(3) using kc(4) by auto 
  2152       also have "iterate opp d1 f = iterate opp d (\<lambda>l. f(l \<inter> {x. x$$k \<le> c}))"
  2153         unfolding d1_def apply(rule iterate_nonzero_image_lemma[unfolded o_def])
  2154         unfolding empty_as_interval apply(rule goal1 division_of_finite operativeD[OF goal1(3)])+
  2155         unfolding empty_as_interval[THEN sym] apply(rule content_empty)
  2156       proof(rule,rule,rule,erule conjE) fix l y assume as:"l \<in> d" "y \<in> d" "l \<inter> {x. x $$ k \<le> c} = y \<inter> {x. x $$ k \<le> c}" "l \<noteq> y" 
  2157         from division_ofD(4)[OF goal1(4) this(1)] guess u v apply-by(erule exE)+ note l=this
  2158         show "f (l \<inter> {x. x $$ k \<le> c}) = neutral opp" unfolding l interval_split[OF kc(4)] 
  2159           apply(rule operativeD(1) goal1)+ unfolding interval_split[THEN sym,OF kc(4)] apply(rule division_split_left_inj)
  2160           apply(rule goal1) unfolding l[THEN sym] apply(rule as(1),rule as(2)) by(rule kc(4) as)+
  2161       qed also have "iterate opp d2 f = iterate opp d (\<lambda>l. f(l \<inter> {x. x$$k \<ge> c}))"
  2162         unfolding d2_def apply(rule iterate_nonzero_image_lemma[unfolded o_def])
  2163         unfolding empty_as_interval apply(rule goal1 division_of_finite operativeD[OF goal1(3)])+
  2164         unfolding empty_as_interval[THEN sym] apply(rule content_empty)
  2165       proof(rule,rule,rule,erule conjE) fix l y assume as:"l \<in> d" "y \<in> d" "l \<inter> {x. c \<le> x $$ k} = y \<inter> {x. c \<le> x $$ k}" "l \<noteq> y" 
  2166         from division_ofD(4)[OF goal1(4) this(1)] guess u v apply-by(erule exE)+ note l=this
  2167         show "f (l \<inter> {x. x $$ k \<ge> c}) = neutral opp" unfolding l interval_split[OF kc(4)] 
  2168           apply(rule operativeD(1) goal1)+ unfolding interval_split[THEN sym,OF kc(4)] apply(rule division_split_right_inj)
  2169           apply(rule goal1) unfolding l[THEN sym] apply(rule as(1),rule as(2)) by(rule as kc(4))+
  2170       qed also have *:"\<forall>x\<in>d. f x = opp (f (x \<inter> {x. x $$ k \<le> c})) (f (x \<inter> {x. c \<le> x $$ k}))"
  2171         unfolding forall_in_division[OF goal1(4)] apply(rule,rule,rule,rule operativeD(2)) using goal1(3) kc by auto 
  2172       have "opp (iterate opp d (\<lambda>l. f (l \<inter> {x. x $$ k \<le> c}))) (iterate opp d (\<lambda>l. f (l \<inter> {x. c \<le> x $$ k})))
  2173         = iterate opp d f" apply(subst(3) iterate_eq[OF _ *[rule_format]]) prefer 3
  2174         apply(rule iterate_op[THEN sym]) using goal1 by auto
  2175       finally show ?thesis by auto
  2176     qed qed qed 
  2177 
  2178 lemma iterate_image_nonzero: assumes "monoidal opp"
  2179   "finite s" "\<forall>x\<in>s. \<forall>y\<in>s. ~(x = y) \<and> f x = f y \<longrightarrow> g(f x) = neutral opp"
  2180   shows "iterate opp (f ` s) g = iterate opp s (g \<circ> f)" using assms
  2181 proof(induct rule:finite_subset_induct[OF assms(2) subset_refl])
  2182   case goal1 show ?case using assms(1) by auto
  2183 next case goal2 have *:"\<And>x y. y = neutral opp \<Longrightarrow> x = opp y x" using assms(1) by auto
  2184   show ?case unfolding image_insert apply(subst iterate_insert[OF assms(1)])
  2185     apply(rule finite_imageI goal2)+
  2186     apply(cases "f a \<in> f ` F") unfolding if_P if_not_P apply(subst goal2(4)[OF assms(1) goal2(1)]) defer
  2187     apply(subst iterate_insert[OF assms(1) goal2(1)]) defer
  2188     apply(subst iterate_insert[OF assms(1) goal2(1)])
  2189     unfolding if_not_P[OF goal2(3)] defer unfolding image_iff defer apply(erule bexE)
  2190     apply(rule *) unfolding o_def apply(rule_tac y=x in goal2(7)[rule_format])
  2191     using goal2 unfolding o_def by auto qed 
  2192 
  2193 lemma operative_tagged_division: assumes "monoidal opp" "operative opp f" "d tagged_division_of {a..b}"
  2194   shows "iterate(opp) d (\<lambda>(x,l). f l) = f {a..b}"
  2195 proof- have *:"(\<lambda>(x,l). f l) = (f o snd)" unfolding o_def by(rule,auto) note assm = tagged_division_ofD[OF assms(3)]
  2196   have "iterate(opp) d (\<lambda>(x,l). f l) = iterate opp (snd ` d) f" unfolding *
  2197     apply(rule iterate_image_nonzero[THEN sym,OF assms(1)]) apply(rule tagged_division_of_finite assms)+ 
  2198     unfolding Ball_def split_paired_All snd_conv apply(rule,rule,rule,rule,rule,rule,rule,erule conjE)
  2199   proof- fix a b aa ba assume as:"(a, b) \<in> d" "(aa, ba) \<in> d" "(a, b) \<noteq> (aa, ba)" "b = ba"
  2200     guess u v using assm(4)[OF as(1)] apply-by(erule exE)+ note uv=this
  2201     show "f b = neutral opp" unfolding uv apply(rule operativeD(1)[OF assms(2)])
  2202       unfolding content_eq_0_interior using tagged_division_ofD(5)[OF assms(3) as(1-3)]
  2203       unfolding as(4)[THEN sym] uv by auto
  2204   qed also have "\<dots> = f {a..b}" 
  2205     using operative_division[OF assms(1-2) division_of_tagged_division[OF assms(3)]] .
  2206   finally show ?thesis . qed
  2207 
  2208 subsection {* Additivity of content. *}
  2209 
  2210 lemma setsum_iterate:assumes "finite s" shows "setsum f s = iterate op + s f"
  2211 proof- have *:"setsum f s = setsum f (support op + f s)"
  2212     apply(rule setsum_mono_zero_right)
  2213     unfolding support_def neutral_monoid using assms by auto
  2214   thus ?thesis unfolding * setsum_def iterate_def fold_image_def fold'_def
  2215     unfolding neutral_monoid . qed
  2216 
  2217 lemma additive_content_division: assumes "d division_of {a..b}"
  2218   shows "setsum content d = content({a..b})"
  2219   unfolding operative_division[OF monoidal_monoid operative_content assms,THEN sym]
  2220   apply(subst setsum_iterate) using assms by auto
  2221 
  2222 lemma additive_content_tagged_division:
  2223   assumes "d tagged_division_of {a..b}"
  2224   shows "setsum (\<lambda>(x,l). content l) d = content({a..b})"
  2225   unfolding operative_tagged_division[OF monoidal_monoid operative_content assms,THEN sym]
  2226   apply(subst setsum_iterate) using assms by auto
  2227 
  2228 subsection {* Finally, the integral of a constant *}
  2229 
  2230 lemma has_integral_const[intro]:
  2231   "((\<lambda>x. c) has_integral (content({a..b::'a::ordered_euclidean_space}) *\<^sub>R c)) ({a..b})"
  2232   unfolding has_integral apply(rule,rule,rule_tac x="\<lambda>x. ball x 1" in exI)
  2233   apply(rule,rule gauge_trivial)apply(rule,rule,erule conjE)
  2234   unfolding split_def apply(subst scaleR_left.setsum[THEN sym, unfolded o_def])
  2235   defer apply(subst additive_content_tagged_division[unfolded split_def]) apply assumption by auto
  2236 
  2237 subsection {* Bounds on the norm of Riemann sums and the integral itself. *}
  2238 
  2239 lemma dsum_bound: assumes "p division_of {a..b}" "norm(c) \<le> e"
  2240   shows "norm(setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> e * content({a..b})" (is "?l \<le> ?r")
  2241   apply(rule order_trans,rule setsum_norm) defer unfolding norm_scaleR setsum_left_distrib[THEN sym]
  2242   apply(rule order_trans[OF mult_left_mono],rule assms,rule setsum_abs_ge_zero)
  2243   apply(subst mult_commute) apply(rule mult_left_mono)
  2244   apply(rule order_trans[of _ "setsum content p"]) apply(rule eq_refl,rule setsum_cong2)
  2245   apply(subst abs_of_nonneg) unfolding additive_content_division[OF assms(1)]
  2246 proof- from order_trans[OF norm_ge_zero[of c] assms(2)] show "0 \<le> e" .
  2247   fix x assume "x\<in>p" from division_ofD(4)[OF assms(1) this] guess u v apply-by(erule exE)+
  2248   thus "0 \<le> content x" using content_pos_le by auto
  2249 qed(insert assms,auto)
  2250 
  2251 lemma rsum_bound: assumes "p tagged_division_of {a..b}" "\<forall>x\<in>{a..b}. norm(f x) \<le> e"
  2252   shows "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> e * content({a..b})"
  2253 proof(cases "{a..b} = {}") case True
  2254   show ?thesis using assms(1) unfolding True tagged_division_of_trivial by auto
  2255 next case False show ?thesis
  2256     apply(rule order_trans,rule setsum_norm) defer unfolding split_def norm_scaleR
  2257     apply(rule order_trans[OF setsum_mono]) apply(rule mult_left_mono[OF _ abs_ge_zero, of _ e]) defer
  2258     unfolding setsum_left_distrib[THEN sym] apply(subst mult_commute) apply(rule mult_left_mono)
  2259     apply(rule order_trans[of _ "setsum (content \<circ> snd) p"]) apply(rule eq_refl,rule setsum_cong2)
  2260     apply(subst o_def, rule abs_of_nonneg)
  2261   proof- show "setsum (content \<circ> snd) p \<le> content {a..b}" apply(rule eq_refl)
  2262       unfolding additive_content_tagged_division[OF assms(1),THEN sym] split_def by auto
  2263     guess w using nonempty_witness[OF False] .
  2264     thus "e\<ge>0" apply-apply(rule order_trans) defer apply(rule assms(2)[rule_format],assumption) by auto
  2265     fix xk assume *:"xk\<in>p" guess x k  using surj_pair[of xk] apply-by(erule exE)+ note xk = this *[unfolded this]
  2266     from tagged_division_ofD(4)[OF assms(1) xk(2)] guess u v apply-by(erule exE)+ note uv=this
  2267     show "0\<le> content (snd xk)" unfolding xk snd_conv uv by(rule content_pos_le)
  2268     show "norm (f (fst xk)) \<le> e" unfolding xk fst_conv using tagged_division_ofD(2,3)[OF assms(1) xk(2)] assms(2) by auto
  2269   qed(insert assms,auto) qed
  2270 
  2271 lemma rsum_diff_bound:
  2272   assumes "p tagged_division_of {a..b}"  "\<forall>x\<in>{a..b}. norm(f x - g x) \<le> e"
  2273   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> e * content({a..b})"
  2274   apply(rule order_trans[OF _ rsum_bound[OF assms]]) apply(rule eq_refl) apply(rule arg_cong[where f=norm])
  2275   unfolding setsum_subtractf[THEN sym] apply(rule setsum_cong2) unfolding scaleR.diff_right by auto
  2276 
  2277 lemma has_integral_bound: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::real_normed_vector"
  2278   assumes "0 \<le> B" "(f has_integral i) ({a..b})" "\<forall>x\<in>{a..b}. norm(f x) \<le> B"
  2279   shows "norm i \<le> B * content {a..b}"
  2280 proof- let ?P = "content {a..b} > 0" { presume "?P \<Longrightarrow> ?thesis"
  2281     thus ?thesis proof(cases ?P) case False
  2282       hence *:"content {a..b} = 0" using content_lt_nz by auto
  2283       hence **:"i = 0" using assms(2) apply(subst has_integral_null_eq[THEN sym]) by auto
  2284       show ?thesis unfolding * ** using assms(1) by auto
  2285     qed auto } assume ab:?P
  2286   { presume "\<not> ?thesis \<Longrightarrow> False" thus ?thesis by auto }
  2287   assume "\<not> ?thesis" hence *:"norm i - B * content {a..b} > 0" by auto
  2288   from assms(2)[unfolded has_integral,rule_format,OF *] guess d apply-by(erule exE conjE)+ note d=this[rule_format]
  2289   from fine_division_exists[OF this(1), of a b] guess p . note p=this
  2290   have *:"\<And>s B. norm s \<le> B \<Longrightarrow> \<not> (norm (s - i) < norm i - B)"
  2291   proof- case goal1 thus ?case unfolding not_less
  2292     using norm_triangle_sub[of i s] unfolding norm_minus_commute by auto
  2293   qed show False using d(2)[OF conjI[OF p]] *[OF rsum_bound[OF p(1) assms(3)]] by auto qed
  2294 
  2295 subsection {* Similar theorems about relationship among components. *}
  2296 
  2297 lemma rsum_component_le: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2298   assumes "p tagged_division_of {a..b}"  "\<forall>x\<in>{a..b}. (f x)$$i \<le> (g x)$$i"
  2299   shows "(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p)$$i \<le> (setsum (\<lambda>(x,k). content k *\<^sub>R g x) p)$$i"
  2300   unfolding  euclidean_component.setsum apply(rule setsum_mono) apply safe
  2301 proof- fix a b assume ab:"(a,b) \<in> p" note assm = tagged_division_ofD(2-4)[OF assms(1) ab]
  2302   from this(3) guess u v apply-by(erule exE)+ note b=this
  2303   show "(content b *\<^sub>R f a) $$ i \<le> (content b *\<^sub>R g a) $$ i" unfolding b
  2304     unfolding euclidean_simps real_scaleR_def apply(rule mult_left_mono)
  2305     defer apply(rule content_pos_le,rule assms(2)[rule_format]) using assm by auto qed
  2306 
  2307 lemma has_integral_component_le: fixes f g::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2308   assumes "(f has_integral i) s" "(g has_integral j) s"  "\<forall>x\<in>s. (f x)$$k \<le> (g x)$$k"
  2309   shows "i$$k \<le> j$$k"
  2310 proof- have lem:"\<And>a b i (j::'b). \<And>g f::'a \<Rightarrow> 'b. (f has_integral i) ({a..b}) \<Longrightarrow> 
  2311     (g has_integral j) ({a..b}) \<Longrightarrow> \<forall>x\<in>{a..b}. (f x)$$k \<le> (g x)$$k \<Longrightarrow> i$$k \<le> j$$k"
  2312   proof(rule ccontr) case goal1 hence *:"0 < (i$$k - j$$k) / 3" by auto
  2313     guess d1 using goal1(1)[unfolded has_integral,rule_format,OF *] apply-by(erule exE conjE)+ note d1=this[rule_format]
  2314     guess d2 using goal1(2)[unfolded has_integral,rule_format,OF *] apply-by(erule exE conjE)+ note d2=this[rule_format]
  2315     guess p using fine_division_exists[OF gauge_inter[OF d1(1) d2(1)], of a b] unfolding fine_inter .
  2316     note p = this(1) conjunctD2[OF this(2)]  note le_less_trans[OF component_le_norm, of _ _ k] term g
  2317     note this[OF d1(2)[OF conjI[OF p(1,2)]]] this[OF d2(2)[OF conjI[OF p(1,3)]]]
  2318     thus False unfolding euclidean_simps using rsum_component_le[OF p(1) goal1(3)] apply simp by smt
  2319   qed let ?P = "\<exists>a b. s = {a..b}"
  2320   { presume "\<not> ?P \<Longrightarrow> ?thesis" thus ?thesis proof(cases ?P)
  2321       case True then guess a b apply-by(erule exE)+ note s=this
  2322       show ?thesis apply(rule lem) using assms[unfolded s] by auto
  2323     qed auto } assume as:"\<not> ?P"
  2324   { presume "\<not> ?thesis \<Longrightarrow> False" thus ?thesis by auto }
  2325   assume "\<not> i$$k \<le> j$$k" hence ij:"(i$$k - j$$k) / 3 > 0" by auto
  2326   note has_integral_altD[OF _ as this] from this[OF assms(1)] this[OF assms(2)] guess B1 B2 . note B=this[rule_format]
  2327   have "bounded (ball 0 B1 \<union> ball (0::'a) B2)" unfolding bounded_Un by(rule conjI bounded_ball)+
  2328   from bounded_subset_closed_interval[OF this] guess a b apply- by(erule exE)+
  2329   note ab = conjunctD2[OF this[unfolded Un_subset_iff]]
  2330   guess w1 using B(2)[OF ab(1)] .. note w1=conjunctD2[OF this]
  2331   guess w2 using B(4)[OF ab(2)] .. note w2=conjunctD2[OF this]
  2332   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" by smt
  2333   note le_less_trans[OF component_le_norm[of _ k]] note this[OF w1(2)] this[OF w2(2)] moreover
  2334   have "w1$$k \<le> w2$$k" apply(rule lem[OF w1(1) w2(1)]) using assms by auto ultimately
  2335   show False unfolding euclidean_simps by(rule *) qed
  2336 
  2337 lemma integral_component_le: fixes g f::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2338   assumes "f integrable_on s" "g integrable_on s"  "\<forall>x\<in>s. (f x)$$k \<le> (g x)$$k"
  2339   shows "(integral s f)$$k \<le> (integral s g)$$k"
  2340   apply(rule has_integral_component_le) using integrable_integral assms by auto
  2341 
  2342 (*lemma has_integral_dest_vec1_le: fixes f::"'a::ordered_euclidean_space \<Rightarrow> real^1"
  2343   assumes "(f has_integral i) s"  "(g has_integral j) s" "\<forall>x\<in>s. f x \<le> g x"
  2344   shows "dest_vec1 i \<le> dest_vec1 j" apply(rule has_integral_component_le[OF assms(1-2)])
  2345   using assms(3) unfolding vector_le_def by auto
  2346 
  2347 lemma integral_dest_vec1_le: fixes f::"real^'n \<Rightarrow> real^1"
  2348   assumes "f integrable_on s" "g integrable_on s" "\<forall>x\<in>s. f x \<le> g x"
  2349   shows "dest_vec1(integral s f) \<le> dest_vec1(integral s g)"
  2350   apply(rule has_integral_dest_vec1_le) apply(rule_tac[1-2] integrable_integral) using assms by auto*)
  2351 
  2352 lemma has_integral_component_nonneg: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2353   assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> (f x)$$k" shows "0 \<le> i$$k" 
  2354   using has_integral_component_le[OF has_integral_0 assms(1)] using assms(2-) by auto
  2355 
  2356 lemma integral_component_nonneg: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2357   assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> (f x)$$k" shows "0 \<le> (integral s f)$$k"
  2358   apply(rule has_integral_component_nonneg) using assms by auto
  2359 
  2360 (*lemma has_integral_dest_vec1_nonneg: fixes f::"real^'n \<Rightarrow> real^1"
  2361   assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> i"
  2362   using has_integral_component_nonneg[OF assms(1), of 1]
  2363   using assms(2) unfolding vector_le_def by auto
  2364 
  2365 lemma integral_dest_vec1_nonneg: fixes f::"real^'n \<Rightarrow> real^1"
  2366   assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> integral s f"
  2367   apply(rule has_integral_dest_vec1_nonneg) using assms by auto*)
  2368 
  2369 lemma has_integral_component_neg: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::ordered_euclidean_space"
  2370   assumes "(f has_integral i) s" "\<forall>x\<in>s. (f x)$$k \<le> 0"shows "i$$k \<le> 0" 
  2371   using has_integral_component_le[OF assms(1) has_integral_0] assms(2-) by auto
  2372 
  2373 (*lemma has_integral_dest_vec1_neg: fixes f::"real^'n \<Rightarrow> real^1"
  2374   assumes "(f has_integral i) s" "\<forall>x\<in>s. f x \<le> 0" shows "i \<le> 0"
  2375   using has_integral_component_neg[OF assms(1),of 1] using assms(2) by auto*)
  2376 
  2377 lemma has_integral_component_lbound: fixes f::"'a::ordered_euclidean_space => 'b::ordered_euclidean_space"
  2378   assumes "(f has_integral i) {a..b}"  "\<forall>x\<in>{a..b}. B \<le> f(x)$$k" "k<DIM('b)" shows "B * content {a..b} \<le> i$$k"
  2379   using has_integral_component_le[OF has_integral_const assms(1),of "(\<chi>\<chi> i. B)::'b" k] assms(2-)
  2380   unfolding euclidean_simps euclidean_lambda_beta'[OF assms(3)] by(auto simp add:field_simps)
  2381 
  2382 lemma has_integral_component_ubound: fixes f::"'a::ordered_euclidean_space => 'b::ordered_euclidean_space"
  2383   assumes "(f has_integral i) {a..b}" "\<forall>x\<in>{a..b}. f x$$k \<le> B" "k<DIM('b)"
  2384   shows "i$$k \<le> B * content({a..b})"
  2385   using has_integral_component_le[OF assms(1) has_integral_const, of k "\<chi>\<chi> i. B"]
  2386   unfolding euclidean_simps euclidean_lambda_beta'[OF assms(3)] using assms(2) by(auto simp add:field_simps)
  2387 
  2388 lemma integral_component_lbound: fixes f::"'a::ordered_euclidean_space => 'b::ordered_euclidean_space"
  2389   assumes "f integrable_on {a..b}" "\<forall>x\<in>{a..b}. B \<le> f(x)$$k" "k<DIM('b)"
  2390   shows "B * content({a..b}) \<le> (integral({a..b}) f)$$k"
  2391   apply(rule has_integral_component_lbound) using assms unfolding has_integral_integral by auto
  2392 
  2393 lemma integral_component_ubound: fixes f::"'a::ordered_euclidean_space => 'b::ordered_euclidean_space"
  2394   assumes "f integrable_on {a..b}" "\<forall>x\<in>{a..b}. f(x)$$k \<le> B" "k<DIM('b)" 
  2395   shows "(integral({a..b}) f)$$k \<le> B * content({a..b})"
  2396   apply(rule has_integral_component_ubound) using assms unfolding has_integral_integral by auto
  2397 
  2398 subsection {* Uniform limit of integrable functions is integrable. *}
  2399 
  2400 lemma integrable_uniform_limit: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  2401   assumes "\<forall>e>0. \<exists>g. (\<forall>x\<in>{a..b}. norm(f x - g x) \<le> e) \<and> g integrable_on {a..b}"
  2402   shows "f integrable_on {a..b}"
  2403 proof- { presume *:"content {a..b} > 0 \<Longrightarrow> ?thesis"
  2404     show ?thesis apply cases apply(rule *,assumption)
  2405       unfolding content_lt_nz integrable_on_def using has_integral_null by auto }
  2406   assume as:"content {a..b} > 0"
  2407   have *:"\<And>P. \<forall>e>(0::real). P e \<Longrightarrow> \<forall>n::nat. P (inverse (real n+1))" by auto
  2408   from choice[OF *[OF assms]] guess g .. note g=conjunctD2[OF this[rule_format],rule_format]
  2409   from choice[OF allI[OF g(2)[unfolded integrable_on_def], of "\<lambda>x. x"]] guess i .. note i=this[rule_format]
  2410   
  2411   have "Cauchy i" unfolding Cauchy_def
  2412   proof(rule,rule) fix e::real assume "e>0"
  2413     hence "e / 4 / content {a..b} > 0" using as by(auto simp add:field_simps)
  2414     then guess M apply-apply(subst(asm) real_arch_inv) by(erule exE conjE)+ note M=this
  2415     show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (i m) (i n) < e" apply(rule_tac x=M in exI,rule,rule,rule,rule)
  2416     proof- case goal1 have "e/4>0" using `e>0` by auto note * = i[unfolded has_integral,rule_format,OF this]
  2417       from *[of m] guess gm apply-by(erule conjE exE)+ note gm=this[rule_format]
  2418       from *[of n] guess gn apply-by(erule conjE exE)+ note gn=this[rule_format]
  2419       from fine_division_exists[OF gauge_inter[OF gm(1) gn(1)], of a b] guess p . note p=this
  2420       have lem2:"\<And>s1 s2 i1 i2. norm(s2 - s1) \<le> e/2 \<Longrightarrow> norm(s1 - i1) < e / 4 \<Longrightarrow> norm(s2 - i2) < e / 4 \<Longrightarrow>norm(i1 - i2) < e"
  2421       proof- case goal1 have "norm (i1 - i2) \<le> norm (i1 - s1) + norm (s1 - s2) + norm (s2 - i2)"
  2422           using norm_triangle_ineq[of "i1 - s1" "s1 - i2"]
  2423           using norm_triangle_ineq[of "s1 - s2" "s2 - i2"] by(auto simp add:algebra_simps)
  2424         also have "\<dots> < e" using goal1 unfolding norm_minus_commute by(auto simp add:algebra_simps)
  2425         finally show ?case .
  2426       qed
  2427       show ?case unfolding dist_norm apply(rule lem2) defer
  2428         apply(rule gm(2)[OF conjI[OF p(1)]],rule_tac[2] gn(2)[OF conjI[OF p(1)]])
  2429         using conjunctD2[OF p(2)[unfolded fine_inter]] apply- apply assumption+ apply(rule order_trans)
  2430         apply(rule rsum_diff_bound[OF p(1), where e="2 / real M"])
  2431       proof show "2 / real M * content {a..b} \<le> e / 2" unfolding divide_inverse 
  2432           using M as by(auto simp add:field_simps)
  2433         fix x assume x:"x \<in> {a..b}"
  2434         have "norm (f x - g n x) + norm (f x - g m x) \<le> inverse (real n + 1) + inverse (real m + 1)" 
  2435             using g(1)[OF x, of n] g(1)[OF x, of m] by auto
  2436         also have "\<dots> \<le> inverse (real M) + inverse (real M)" apply(rule add_mono)
  2437           apply(rule_tac[!] le_imp_inverse_le) using goal1 M by auto
  2438         also have "\<dots> = 2 / real M" unfolding divide_inverse by auto
  2439         finally show "norm (g n x - g m x) \<le> 2 / real M"
  2440           using norm_triangle_le[of "g n x - f x" "f x - g m x" "2 / real M"]
  2441           by(auto simp add:algebra_simps simp add:norm_minus_commute)
  2442       qed qed qed
  2443   from this[unfolded convergent_eq_cauchy[THEN sym]] guess s .. note s=this
  2444 
  2445   show ?thesis unfolding integrable_on_def apply(rule_tac x=s in exI) unfolding has_integral
  2446   proof(rule,rule)  
  2447     case goal1 hence *:"e/3 > 0" by auto
  2448     from s[unfolded Lim_sequentially,rule_format,OF this] guess N1 .. note N1=this
  2449     from goal1 as have "e / 3 / content {a..b} > 0" by(auto simp add:field_simps)
  2450     from real_arch_invD[OF this] guess N2 apply-by(erule exE conjE)+ note N2=this
  2451     from i[of "N1 + N2",unfolded has_integral,rule_format,OF *] guess g' .. note g'=conjunctD2[OF this,rule_format]
  2452     have lem:"\<And>sf sg i. norm(sf - sg) \<le> e / 3 \<Longrightarrow> norm(i - s) < e / 3 \<Longrightarrow> norm(sg - i) < e / 3 \<Longrightarrow> norm(sf - s) < e"
  2453     proof- case goal1 have "norm (sf - s) \<le> norm (sf - sg) + norm (sg - i) + norm (i - s)"
  2454         using norm_triangle_ineq[of "sf - sg" "sg - s"]
  2455         using norm_triangle_ineq[of "sg -  i" " i - s"] by(auto simp add:algebra_simps)
  2456       also have "\<dots> < e" using goal1 unfolding norm_minus_commute by(auto simp add:algebra_simps)
  2457       finally show ?case .
  2458     qed
  2459     show ?case apply(rule_tac x=g' in exI) apply(rule,rule g')
  2460     proof(rule,rule) fix p assume p:"p tagged_division_of {a..b} \<and> g' fine p" note * = g'(2)[OF this]
  2461       show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - s) < e" apply-apply(rule lem[OF _ _ *])
  2462         apply(rule order_trans,rule rsum_diff_bound[OF p[THEN conjunct1]]) apply(rule,rule g,assumption)
  2463       proof- have "content {a..b} < e / 3 * (real N2)"
  2464           using N2 unfolding inverse_eq_divide using as by(auto simp add:field_simps)
  2465         hence "content {a..b} < e / 3 * (real (N1 + N2) + 1)"
  2466           apply-apply(rule less_le_trans,assumption) using `e>0` by auto 
  2467         thus "inverse (real (N1 + N2) + 1) * content {a..b} \<le> e / 3"
  2468           unfolding inverse_eq_divide by(auto simp add:field_simps)
  2469         show "norm (i (N1 + N2) - s) < e / 3" by(rule N1[rule_format,unfolded dist_norm],auto)
  2470       qed qed qed qed
  2471 
  2472 subsection {* Negligible sets. *}
  2473 
  2474 definition "negligible (s::('a::ordered_euclidean_space) set) \<equiv> (\<forall>a b. ((indicator s :: 'a\<Rightarrow>real) has_integral 0) {a..b})"
  2475 
  2476 subsection {* Negligibility of hyperplane. *}
  2477 
  2478 lemma vsum_nonzero_image_lemma: 
  2479   assumes "finite s" "g(a) = 0"
  2480   "\<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<and> x \<noteq> y \<longrightarrow> g(f x) = 0"
  2481   shows "setsum g {f x |x. x \<in> s \<and> f x \<noteq> a} = setsum (g o f) s"
  2482   unfolding setsum_iterate[OF assms(1)] apply(subst setsum_iterate) defer
  2483   apply(rule iterate_nonzero_image_lemma) apply(rule assms monoidal_monoid)+
  2484   unfolding assms using neutral_add unfolding neutral_add using assms by auto 
  2485 
  2486 lemma interval_doublesplit:  fixes a::"'a::ordered_euclidean_space" assumes "k<DIM('a)"
  2487   shows "{a..b} \<inter> {x . abs(x$$k - c) \<le> (e::real)} = 
  2488   {(\<chi>\<chi> i. if i = k then max (a$$k) (c - e) else a$$i) .. (\<chi>\<chi> i. if i = k then min (b$$k) (c + e) else b$$i)}"
  2489 proof- have *:"\<And>x c e::real. abs(x - c) \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e" by auto
  2490   have **:"\<And>s P Q. s \<inter> {x. P x \<and> Q x} = (s \<inter> {x. Q x}) \<inter> {x. P x}" by blast
  2491   show ?thesis unfolding * ** interval_split[OF assms] by(rule refl) qed
  2492 
  2493 lemma division_doublesplit: fixes a::"'a::ordered_euclidean_space" assumes "p division_of {a..b}" and k:"k<DIM('a)"
  2494   shows "{l \<inter> {x. abs(x$$k - c) \<le> e} |l. l \<in> p \<and> l \<inter> {x. abs(x$$k - c) \<le> e} \<noteq> {}} division_of ({a..b} \<inter> {x. abs(x$$k - c) \<le> e})"
  2495 proof- have *:"\<And>x c. abs(x - c) \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e" by auto
  2496   have **:"\<And>p q p' q'. p division_of q \<Longrightarrow> p = p' \<Longrightarrow> q = q' \<Longrightarrow> p' division_of q'" by auto
  2497   note division_split(1)[OF assms, where c="c+e",unfolded interval_split[OF k]]
  2498   note division_split(2)[OF this, where c="c-e" and k=k,OF k] 
  2499   thus ?thesis apply(rule **) using k apply- unfolding interval_doublesplit unfolding * unfolding interval_split interval_doublesplit
  2500     apply(rule set_eqI) unfolding mem_Collect_eq apply rule apply(erule conjE exE)+ apply(rule_tac x=la in exI) defer
  2501     apply(erule conjE exE)+ apply(rule_tac x="l \<inter> {x. c + e \<ge> x $$ k}" in exI) apply rule defer apply rule
  2502     apply(rule_tac x=l in exI) by blast+ qed
  2503 
  2504 lemma content_doublesplit: fixes a::"'a::ordered_euclidean_space" assumes "0 < e" and k:"k<DIM('a)"
  2505   obtains d where "0 < d" "content({a..b} \<inter> {x. abs(x$$k - c) \<le> d}) < e"
  2506 proof(cases "content {a..b} = 0")
  2507   case True show ?thesis apply(rule that[of 1]) defer unfolding interval_doublesplit[OF k]
  2508     apply(rule le_less_trans[OF content_subset]) defer apply(subst True)
  2509     unfolding interval_doublesplit[THEN sym,OF k] using assms by auto 
  2510 next case False def d \<equiv> "e / 3 / setprod (\<lambda>i. b$$i - a$$i) ({..<DIM('a)} - {k})"
  2511   note False[unfolded content_eq_0 not_ex not_le, rule_format]
  2512   hence "\<And>x. x<DIM('a) \<Longrightarrow> b$$x > a$$x" by(auto simp add:not_le)
  2513   hence prod0:"0 < setprod (\<lambda>i. b$$i - a$$i) ({..<DIM('a)} - {k})" apply-apply(rule setprod_pos) by(auto simp add:field_simps)
  2514   hence "d > 0" unfolding d_def using assms by(auto simp add:field_simps) thus ?thesis
  2515   proof(rule that[of d]) have *:"{..<DIM('a)} = insert k ({..<DIM('a)} - {k})" using k by auto
  2516     have **:"{a..b} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d} \<noteq> {} \<Longrightarrow> 
  2517       (\<Prod>i\<in>{..<DIM('a)} - {k}. interval_upperbound ({a..b} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) $$ i
  2518       - interval_lowerbound ({a..b} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) $$ i)
  2519       = (\<Prod>i\<in>{..<DIM('a)} - {k}. b$$i - a$$i)" apply(rule setprod_cong,rule refl) 
  2520       unfolding interval_doublesplit[OF k] apply(subst interval_bounds) defer apply(subst interval_bounds)
  2521       unfolding interval_eq_empty not_ex not_less by auto
  2522     show "content ({a..b} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) < e" apply(cases) unfolding content_def apply(subst if_P,assumption,rule assms)
  2523       unfolding if_not_P apply(subst *) apply(subst setprod_insert) unfolding **
  2524       unfolding interval_doublesplit[OF k] interval_eq_empty not_ex not_less prefer 3
  2525       apply(subst interval_bounds) defer apply(subst interval_bounds) unfolding euclidean_lambda_beta'[OF k] if_P[OF refl]
  2526     proof- have "(min (b $$ k) (c + d) - max (a $$ k) (c - d)) \<le> 2 * d" by auto
  2527       also have "... < e / (\<Prod>i\<in>{..<DIM('a)} - {k}. b $$ i - a $$ i)" unfolding d_def using assms prod0 by(auto simp add:field_simps)
  2528       finally show "(min (b $$ k) (c + d) - max (a $$ k) (c - d)) * (\<Prod>i\<in>{..<DIM('a)} - {k}. b $$ i - a $$ i) < e"
  2529         unfolding pos_less_divide_eq[OF prod0] . qed auto qed qed
  2530 
  2531 lemma negligible_standard_hyperplane[intro]: fixes type::"'a::ordered_euclidean_space" assumes k:"k<DIM('a)"
  2532   shows "negligible {x::'a. x$$k = (c::real)}" 
  2533   unfolding negligible_def has_integral apply(rule,rule,rule,rule)
  2534 proof-
  2535   case goal1 from content_doublesplit[OF this k,of a b c] guess d . note d=this
  2536   let ?i = "indicator {x::'a. x$$k = c} :: 'a\<Rightarrow>real"
  2537   show ?case apply(rule_tac x="\<lambda>x. ball x d" in exI) apply(rule,rule gauge_ball,rule d)
  2538   proof(rule,rule) fix p assume p:"p tagged_division_of {a..b} \<and> (\<lambda>x. ball x d) fine p"
  2539     have *:"(\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. abs(x$$k - c) \<le> d}) *\<^sub>R ?i x)"
  2540       apply(rule setsum_cong2) unfolding split_paired_all real_scaleR_def mult_cancel_right split_conv
  2541       apply(cases,rule disjI1,assumption,rule disjI2)
  2542     proof- fix x l assume as:"(x,l)\<in>p" "?i x \<noteq> 0" hence xk:"x$$k = c" unfolding indicator_def apply-by(rule ccontr,auto)
  2543       show "content l = content (l \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d})" apply(rule arg_cong[where f=content])
  2544         apply(rule set_eqI,rule,rule) unfolding mem_Collect_eq
  2545       proof- fix y assume y:"y\<in>l" note p[THEN conjunct2,unfolded fine_def,rule_format,OF as(1),unfolded split_conv]
  2546         note this[unfolded subset_eq mem_ball dist_norm,rule_format,OF y] note le_less_trans[OF component_le_norm[of _ k] this]
  2547         thus "\<bar>y $$ k - c\<bar> \<le> d" unfolding euclidean_simps xk by auto
  2548       qed auto qed
  2549     note p'= tagged_division_ofD[OF p[THEN conjunct1]] and p''=division_of_tagged_division[OF p[THEN conjunct1]]
  2550     show "norm ((\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) - 0) < e" unfolding diff_0_right * unfolding real_scaleR_def real_norm_def
  2551       apply(subst abs_of_nonneg) apply(rule setsum_nonneg,rule) unfolding split_paired_all split_conv
  2552       apply(rule mult_nonneg_nonneg) apply(drule p'(4)) apply(erule exE)+ apply(rule_tac b=b in back_subst)
  2553       prefer 2 apply(subst(asm) eq_commute) apply assumption
  2554       apply(subst interval_doublesplit[OF k]) apply(rule content_pos_le) apply(rule indicator_pos_le)
  2555     proof- have "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) * ?i x) \<le> (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}))"
  2556         apply(rule setsum_mono) unfolding split_paired_all split_conv 
  2557         apply(rule mult_right_le_one_le) apply(drule p'(4)) by(auto simp add:interval_doublesplit[OF k] intro!:content_pos_le)
  2558       also have "... < e" apply(subst setsum_over_tagged_division_lemma[OF p[THEN conjunct1]])
  2559       proof- case goal1 have "content ({u..v} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) \<le> content {u..v}"
  2560           unfolding interval_doublesplit[OF k] apply(rule content_subset) unfolding interval_doublesplit[THEN sym,OF k] by auto
  2561         thus ?case unfolding goal1 unfolding interval_doublesplit[OF k] using content_pos_le by smt
  2562       next have *:"setsum content {l \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d} |l. l \<in> snd ` p \<and> l \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d} \<noteq> {}} \<ge> 0"
  2563           apply(rule setsum_nonneg,rule) unfolding mem_Collect_eq image_iff apply(erule exE bexE conjE)+ unfolding split_paired_all 
  2564         proof- fix x l a b assume as:"x = l \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}" "(a, b) \<in> p" "l = snd (a, b)"
  2565           guess u v using p'(4)[OF as(2)] apply-by(erule exE)+ note * = this
  2566           show "content x \<ge> 0" unfolding as snd_conv * interval_doublesplit[OF k] by(rule content_pos_le)
  2567         qed have **:"norm (1::real) \<le> 1" by auto note division_doublesplit[OF p'' k,unfolded interval_doublesplit[OF k]]
  2568         note dsum_bound[OF this **,unfolded interval_doublesplit[THEN sym,OF k]]
  2569         note this[unfolded real_scaleR_def real_norm_def mult_1_right mult_1, of c d] note le_less_trans[OF this d(2)]
  2570         from this[unfolded abs_of_nonneg[OF *]] show "(\<Sum>ka\<in>snd ` p. content (ka \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d})) < e"
  2571           apply(subst vsum_nonzero_image_lemma[of "snd ` p" content "{}", unfolded o_def,THEN sym])
  2572           apply(rule finite_imageI p' content_empty)+ unfolding forall_in_division[OF p'']
  2573         proof(rule,rule,rule,rule,rule,rule,rule,erule conjE) fix m n u v
  2574           assume as:"{m..n} \<in> snd ` p" "{u..v} \<in> snd ` p" "{m..n} \<noteq> {u..v}"  "{m..n} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d} = {u..v} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}"
  2575           have "({m..n} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) \<inter> ({u..v} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) \<subseteq> {m..n} \<inter> {u..v}" by blast
  2576           note subset_interior[OF this, unfolded division_ofD(5)[OF p'' as(1-3)] interior_inter[of "{m..n}"]]
  2577           hence "interior ({m..n} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) = {}" unfolding as Int_absorb by auto
  2578           thus "content ({m..n} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) = 0" unfolding interval_doublesplit[OF k] content_eq_0_interior[THEN sym] .
  2579         qed qed
  2580       finally show "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) * ?i x) < e" .
  2581     qed qed qed
  2582 
  2583 subsection {* A technical lemma about "refinement" of division. *}
  2584 
  2585 lemma tagged_division_finer: fixes p::"(('a::ordered_euclidean_space) \<times> (('a::ordered_euclidean_space) set)) set"
  2586   assumes "p tagged_division_of {a..b}" "gauge d"
  2587   obtains q where "q tagged_division_of {a..b}" "d fine q" "\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q"
  2588 proof-
  2589   let ?P = "\<lambda>p. p tagged_partial_division_of {a..b} \<longrightarrow> gauge d \<longrightarrow>
  2590     (\<exists>q. q tagged_division_of (\<Union>{k. \<exists>x. (x,k) \<in> p}) \<and> d fine q \<and>
  2591                    (\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q))"
  2592   { have *:"finite p" "p tagged_partial_division_of {a..b}" using assms(1) unfolding tagged_division_of_def by auto
  2593     presume "\<And>p. finite p \<Longrightarrow> ?P p" from this[rule_format,OF * assms(2)] guess q .. note q=this
  2594     thus ?thesis apply-apply(rule that[of q]) unfolding tagged_division_ofD[OF assms(1)] by auto
  2595   } fix p::"(('a::ordered_euclidean_space) \<times> (('a::ordered_euclidean_space) set)) set" assume as:"finite p"
  2596   show "?P p" apply(rule,rule) using as proof(induct p) 
  2597     case empty show ?case apply(rule_tac x="{}" in exI) unfolding fine_def by auto
  2598   next case (insert xk p) guess x k using surj_pair[of xk] apply- by(erule exE)+ note xk=this
  2599     note tagged_partial_division_subset[OF insert(4) subset_insertI]
  2600     from insert(3)[OF this insert(5)] guess q1 .. note q1 = conjunctD3[OF this]
  2601     have *:"\<Union>{l. \<exists>y. (y,l) \<in> insert xk p} = k \<union> \<Union>{l. \<exists>y. (y,l) \<in> p}" unfolding xk by auto
  2602     note p = tagged_partial_division_ofD[OF insert(4)]
  2603     from p(4)[unfolded xk, OF insertI1] guess u v apply-by(erule exE)+ note uv=this
  2604 
  2605     have "finite {k. \<exists>x. (x, k) \<in> p}" 
  2606       apply(rule finite_subset[of _ "snd ` p"],rule) unfolding subset_eq image_iff mem_Collect_eq
  2607       apply(erule exE,rule_tac x="(xa,x)" in bexI) using p by auto
  2608     hence int:"interior {u..v} \<inter> interior (\<Union>{k. \<exists>x. (x, k) \<in> p}) = {}"
  2609       apply(rule inter_interior_unions_intervals) apply(rule open_interior) apply(rule_tac[!] ballI)
  2610       unfolding mem_Collect_eq apply(erule_tac[!] exE) apply(drule p(4)[OF insertI2],assumption)      
  2611       apply(rule p(5))  unfolding uv xk apply(rule insertI1,rule insertI2) apply assumption
  2612       using insert(2) unfolding uv xk by auto
  2613 
  2614     show ?case proof(cases "{u..v} \<subseteq> d x")
  2615       case True thus ?thesis apply(rule_tac x="{(x,{u..v})} \<union> q1" in exI) apply rule
  2616         unfolding * uv apply(rule tagged_division_union,rule tagged_division_of_self)
  2617         apply(rule p[unfolded xk uv] insertI1)+  apply(rule q1,rule int) 
  2618         apply(rule,rule fine_union,subst fine_def) defer apply(rule q1)
  2619         unfolding Ball_def split_paired_All split_conv apply(rule,rule,rule,rule)
  2620         apply(erule insertE) defer apply(rule UnI2) apply(drule q1(3)[rule_format]) unfolding xk uv by auto
  2621     next case False from fine_division_exists[OF assms(2), of u v] guess q2 . note q2=this
  2622       show ?thesis apply(rule_tac x="q2 \<union> q1" in exI)
  2623         apply rule unfolding * uv apply(rule tagged_division_union q2 q1 int fine_union)+
  2624         unfolding Ball_def split_paired_All split_conv apply rule apply(rule fine_union)
  2625         apply(rule q1 q2)+ apply(rule,rule,rule,rule) apply(erule insertE)
  2626         apply(rule UnI2) defer apply(drule q1(3)[rule_format])using False unfolding xk uv by auto
  2627     qed qed qed
  2628 
  2629 subsection {* Hence the main theorem about negligible sets. *}
  2630 
  2631 lemma finite_product_dependent: assumes "finite s" "\<And>x. x\<in>s\<Longrightarrow> finite (t x)"
  2632   shows "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}" using assms
  2633 proof(induct) case (insert x s) 
  2634   have *:"{(i, j) |i j. i \<in> insert x s \<and> j \<in> t i} = (\<lambda>y. (x,y)) ` (t x) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  2635   show ?case unfolding * apply(rule finite_UnI) using insert by auto qed auto
  2636 
  2637 lemma sum_sum_product: assumes "finite s" "\<forall>i\<in>s. finite (t i)"
  2638   shows "setsum (\<lambda>i. setsum (x i) (t i)::real) s = setsum (\<lambda>(i,j). x i j) {(i,j) | i j. i \<in> s \<and> j \<in> t i}" using assms
  2639 proof(induct) case (insert a s)
  2640   have *:"{(i, j) |i j. i \<in> insert a s \<and> j \<in> t i} = (\<lambda>y. (a,y)) ` (t a) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  2641   show ?case unfolding * apply(subst setsum_Un_disjoint) unfolding setsum_insert[OF insert(1-2)]
  2642     prefer 4 apply(subst insert(3)) unfolding add_right_cancel
  2643   proof- show "setsum (x a) (t a) = (\<Sum>(xa, y)\<in>Pair a ` t a. x xa y)" apply(subst setsum_reindex) unfolding inj_on_def by auto
  2644     show "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}" apply(rule finite_product_dependent) using insert by auto
  2645   qed(insert insert, auto) qed auto
  2646 
  2647 lemma has_integral_negligible: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2648   assumes "negligible s" "\<forall>x\<in>(t - s). f x = 0"
  2649   shows "(f has_integral 0) t"
  2650 proof- presume P:"\<And>f::'b::ordered_euclidean_space \<Rightarrow> 'a. \<And>a b. (\<forall>x. ~(x \<in> s) \<longrightarrow> f x = 0) \<Longrightarrow> (f has_integral 0) ({a..b})"
  2651   let ?f = "(\<lambda>x. if x \<in> t then f x else 0)"
  2652   show ?thesis apply(rule_tac f="?f" in has_integral_eq) apply(rule) unfolding if_P apply(rule refl)
  2653     apply(subst has_integral_alt) apply(cases,subst if_P,assumption) unfolding if_not_P
  2654   proof- assume "\<exists>a b. t = {a..b}" then guess a b apply-by(erule exE)+ note t = this
  2655     show "(?f has_integral 0) t" unfolding t apply(rule P) using assms(2) unfolding t by auto
  2656   next show "\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> t then ?f x else 0) has_integral z) {a..b} \<and> norm (z - 0) < e)"
  2657       apply(safe,rule_tac x=1 in exI,rule) apply(rule zero_less_one,safe) apply(rule_tac x=0 in exI)
  2658       apply(rule,rule P) using assms(2) by auto
  2659   qed
  2660 next fix f::"'b \<Rightarrow> 'a" and a b::"'b" assume assm:"\<forall>x. x \<notin> s \<longrightarrow> f x = 0" 
  2661   show "(f has_integral 0) {a..b}" unfolding has_integral
  2662   proof(safe) case goal1
  2663     hence "\<And>n. e / 2 / ((real n+1) * (2 ^ n)) > 0" 
  2664       apply-apply(rule divide_pos_pos) defer apply(rule mult_pos_pos) by(auto simp add:field_simps)
  2665     note assms(1)[unfolded negligible_def has_integral,rule_format,OF this,of a b] note allI[OF this,of "\<lambda>x. x"] 
  2666     from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format]]
  2667     show ?case apply(rule_tac x="\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x" in exI) 
  2668     proof safe show "gauge (\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x)" using d(1) unfolding gauge_def by auto
  2669       fix p assume as:"p tagged_division_of {a..b}" "(\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x) fine p" 
  2670       let ?goal = "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  2671       { presume "p\<noteq>{} \<Longrightarrow> ?goal" thus ?goal apply(cases "p={}") using goal1 by auto  }
  2672       assume as':"p \<noteq> {}" from real_arch_simple[of "Sup((\<lambda>(x,k). norm(f x)) ` p)"] guess N ..
  2673       hence N:"\<forall>x\<in>(\<lambda>(x, k). norm (f x)) ` p. x \<le> real N" apply(subst(asm) Sup_finite_le_iff) using as as' by auto
  2674       have "\<forall>i. \<exists>q. q tagged_division_of {a..b} \<and> (d i) fine q \<and> (\<forall>(x, k)\<in>p. k \<subseteq> (d i) x \<longrightarrow> (x, k) \<in> q)"
  2675         apply(rule,rule tagged_division_finer[OF as(1) d(1)]) by auto
  2676       from choice[OF this] guess q .. note q=conjunctD3[OF this[rule_format]]
  2677       have *:"\<And>i. (\<Sum>(x, k)\<in>q i. content k *\<^sub>R indicator s x) \<ge> (0::real)" apply(rule setsum_nonneg,safe) 
  2678         unfolding real_scaleR_def apply(rule mult_nonneg_nonneg) apply(drule tagged_division_ofD(4)[OF q(1)]) by auto
  2679       have **:"\<And>f g s t. finite s \<Longrightarrow> finite t \<Longrightarrow> (\<forall>(x,y) \<in> t. (0::real) \<le> g(x,y)) \<Longrightarrow> (\<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"
  2680       proof- case goal1 thus ?case apply-apply(rule setsum_le_included[of s t g snd f]) prefer 4
  2681           apply safe apply(erule_tac x=x in ballE) apply(erule exE) apply(rule_tac x="(xa,x)" in bexI) by auto qed
  2682       have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) \<le> setsum (\<lambda>i. (real i + 1) *
  2683                      norm(setsum (\<lambda>(x,k). content k *\<^sub>R indicator s x :: real) (q i))) {0..N+1}"
  2684         unfolding real_norm_def setsum_right_distrib abs_of_nonneg[OF *] diff_0_right
  2685         apply(rule order_trans,rule setsum_norm) defer apply(subst sum_sum_product) prefer 3 
  2686       proof(rule **,safe) show "finite {(i, j) |i j. i \<in> {0..N + 1} \<and> j \<in> q i}" apply(rule finite_product_dependent) using q by auto
  2687         fix i a b assume as'':"(a,b) \<in> q i" show "0 \<le> (real i + 1) * (content b *\<^sub>R indicator s a)"
  2688           unfolding real_scaleR_def apply(rule mult_nonneg_nonneg) defer apply(rule mult_nonneg_nonneg)
  2689           using tagged_division_ofD(4)[OF q(1) as''] by auto
  2690       next fix i::nat show "finite (q i)" using q by auto
  2691       next fix x k assume xk:"(x,k) \<in> p" def n \<equiv> "nat \<lfloor>norm (f x)\<rfloor>"
  2692         have *:"norm (f x) \<in> (\<lambda>(x, k). norm (f x)) ` p" using xk by auto
  2693         have nfx:"real n \<le> norm(f x)" "norm(f x) \<le> real n + 1" unfolding n_def by auto
  2694         hence "n \<in> {0..N + 1}" using N[rule_format,OF *] by auto
  2695         moreover  note as(2)[unfolded fine_def,rule_format,OF xk,unfolded split_conv]
  2696         note q(3)[rule_format,OF xk,unfolded split_conv,rule_format,OF this] note this[unfolded n_def[symmetric]]
  2697         moreover have "norm (content k *\<^sub>R f x) \<le> (real n + 1) * (content k * indicator s x)"
  2698         proof(cases "x\<in>s") case False thus ?thesis using assm by auto
  2699         next case True have *:"content k \<ge> 0" using tagged_division_ofD(4)[OF as(1) xk] by auto
  2700           moreover have "content k * norm (f x) \<le> content k * (real n + 1)" apply(rule mult_mono) using nfx * by auto
  2701           ultimately show ?thesis unfolding abs_mult using nfx True by(auto simp add:field_simps)
  2702         qed ultimately show "\<exists>y. (y, x, k) \<in> {(i, j) |i j. i \<in> {0..N + 1} \<and> j \<in> q i} \<and> norm (content k *\<^sub>R f x) \<le> (real y + 1) * (content k *\<^sub>R indicator s x)"
  2703           apply(rule_tac x=n in exI,safe) apply(rule_tac x=n in exI,rule_tac x="(x,k)" in exI,safe) by auto
  2704       qed(insert as, auto)
  2705       also have "... \<le> setsum (\<lambda>i. e / 2 / 2 ^ i) {0..N+1}" apply(rule setsum_mono) 
  2706       proof- case goal1 thus ?case apply(subst mult_commute, subst pos_le_divide_eq[THEN sym])
  2707           using d(2)[rule_format,of "q i" i] using q[rule_format] by(auto simp add:field_simps)
  2708       qed also have "... < e * inverse 2 * 2" unfolding divide_inverse setsum_right_distrib[THEN sym]
  2709         apply(rule mult_strict_left_mono) unfolding power_inverse atLeastLessThanSuc_atLeastAtMost[THEN sym]
  2710         apply(subst sumr_geometric) using goal1 by auto
  2711       finally show "?goal" by auto qed qed qed
  2712 
  2713 lemma has_integral_spike: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector"
  2714   assumes "negligible s" "(\<forall>x\<in>(t - s). g x = f x)" "(f has_integral y) t"
  2715   shows "(g has_integral y) t"
  2716 proof- { fix a b::"'b" and f g ::"'b \<Rightarrow> 'a" and y::'a
  2717     assume as:"\<forall>x \<in> {a..b} - s. g x = f x" "(f has_integral y) {a..b}"
  2718     have "((\<lambda>x. f x + (g x - f x)) has_integral (y + 0)) {a..b}" apply(rule has_integral_add[OF as(2)])
  2719       apply(rule has_integral_negligible[OF assms(1)]) using as by auto
  2720     hence "(g has_integral y) {a..b}" by auto } note * = this
  2721   show ?thesis apply(subst has_integral_alt) using assms(2-) apply-apply(rule cond_cases,safe)
  2722     apply(rule *, assumption+) apply(subst(asm) has_integral_alt) unfolding if_not_P
  2723     apply(erule_tac x=e in allE,safe,rule_tac x=B in exI,safe) apply(erule_tac x=a in allE,erule_tac x=b in allE,safe)
  2724     apply(rule_tac x=z in exI,safe) apply(rule *[where fa2="\<lambda>x. if x\<in>t then f x else 0"]) by auto qed
  2725 
  2726 lemma has_integral_spike_eq:
  2727   assumes "negligible s" "\<forall>x\<in>(t - s). g x = f x"
  2728   shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  2729   apply rule apply(rule_tac[!] has_integral_spike[OF assms(1)]) using assms(2) by auto
  2730 
  2731 lemma integrable_spike: assumes "negligible s" "\<forall>x\<in>(t - s). g x = f x" "f integrable_on t"
  2732   shows "g integrable_on  t"
  2733   using assms unfolding integrable_on_def apply-apply(erule exE)
  2734   apply(rule,rule has_integral_spike) by fastsimp+
  2735 
  2736 lemma integral_spike: assumes "negligible s" "\<forall>x\<in>(t - s). g x = f x"
  2737   shows "integral t f = integral t g"
  2738   unfolding integral_def using has_integral_spike_eq[OF assms] by auto
  2739 
  2740 subsection {* Some other trivialities about negligible sets. *}
  2741 
  2742 lemma negligible_subset[intro]: assumes "negligible s" "t \<subseteq> s" shows "negligible t" unfolding negligible_def 
  2743 proof(safe) case goal1 show ?case using assms(1)[unfolded negligible_def,rule_format,of a b]
  2744     apply-apply(rule has_integral_spike[OF assms(1)]) defer apply assumption
  2745     using assms(2) unfolding indicator_def by auto qed
  2746 
  2747 lemma negligible_diff[intro?]: assumes "negligible s" shows "negligible(s - t)" using assms by auto
  2748 
  2749 lemma negligible_inter: assumes "negligible s \<or> negligible t" shows "negligible(s \<inter> t)" using assms by auto
  2750 
  2751 lemma negligible_union: assumes "negligible s" "negligible t" shows "negligible (s \<union> t)" unfolding negligible_def 
  2752 proof safe case goal1 note assm = assms[unfolded negligible_def,rule_format,of a b]
  2753   thus ?case apply(subst has_integral_spike_eq[OF assms(2)])
  2754     defer apply assumption unfolding indicator_def by auto qed
  2755 
  2756 lemma negligible_union_eq[simp]: "negligible (s \<union> t) \<longleftrightarrow> (negligible s \<and> negligible t)"
  2757   using negligible_union by auto
  2758 
  2759 lemma negligible_sing[intro]: "negligible {a::_::ordered_euclidean_space}" 
  2760   using negligible_standard_hyperplane[of 0 "a$$0"] by auto 
  2761 
  2762 lemma negligible_insert[simp]: "negligible(insert a s) \<longleftrightarrow> negligible s"
  2763   apply(subst insert_is_Un) unfolding negligible_union_eq by auto
  2764 
  2765 lemma negligible_empty[intro]: "negligible {}" by auto
  2766 
  2767 lemma negligible_finite[intro]: assumes "finite s" shows "negligible s"
  2768   using assms apply(induct s) by auto
  2769 
  2770 lemma negligible_unions[intro]: assumes "finite s" "\<forall>t\<in>s. negligible t" shows "negligible(\<Union>s)"
  2771   using assms by(induct,auto) 
  2772 
  2773 lemma negligible:  "negligible s \<longleftrightarrow> (\<forall>t::('a::ordered_euclidean_space) set. ((indicator s::'a\<Rightarrow>real) has_integral 0) t)"
  2774   apply safe defer apply(subst negligible_def)
  2775 proof- fix t::"'a set" assume as:"negligible s"
  2776   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)" by(rule ext,auto)  
  2777   show "((indicator s::'a\<Rightarrow>real) has_integral 0) t" apply(subst has_integral_alt)
  2778     apply(cases,subst if_P,assumption) unfolding if_not_P apply(safe,rule as[unfolded negligible_def,rule_format])
  2779     apply(rule_tac x=1 in exI) apply(safe,rule zero_less_one) apply(rule_tac x=0 in exI)
  2780     using negligible_subset[OF as,of "s \<inter> t"] unfolding negligible_def indicator_def_raw unfolding * by auto qed auto
  2781 
  2782 subsection {* Finite case of the spike theorem is quite commonly needed. *}
  2783 
  2784 lemma has_integral_spike_finite: assumes "finite s" "\<forall>x\<in>t-s. g x = f x" 
  2785   "(f has_integral y) t" shows "(g has_integral y) t"
  2786   apply(rule has_integral_spike) using assms by auto
  2787 
  2788 lemma has_integral_spike_finite_eq: assumes "finite s" "\<forall>x\<in>t-s. g x = f x"
  2789   shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  2790   apply rule apply(rule_tac[!] has_integral_spike_finite) using assms by auto
  2791 
  2792 lemma integrable_spike_finite:
  2793   assumes "finite s" "\<forall>x\<in>t-s. g x = f x" "f integrable_on t" shows "g integrable_on  t"
  2794   using assms unfolding integrable_on_def apply safe apply(rule_tac x=y in exI)
  2795   apply(rule has_integral_spike_finite) by auto
  2796 
  2797 subsection {* In particular, the boundary of an interval is negligible. *}
  2798 
  2799 lemma negligible_frontier_interval: "negligible({a::'a::ordered_euclidean_space..b} - {a<..<b})"
  2800 proof- let ?A = "\<Union>((\<lambda>k. {x. x$$k = a$$k} \<union> {x::'a. x$$k = b$$k}) ` {..<DIM('a)})"
  2801   have "{a..b} - {a<..<b} \<subseteq> ?A" apply rule unfolding Diff_iff mem_interval not_all
  2802     apply(erule conjE exE)+ apply(rule_tac X="{x. x $$ xa = a $$ xa} \<union> {x. x $$ xa = b $$ xa}" in UnionI)
  2803     apply(erule_tac[!] x=xa in allE) by auto
  2804   thus ?thesis apply-apply(rule negligible_subset[of ?A]) apply(rule negligible_unions[OF finite_imageI]) by auto qed
  2805 
  2806 lemma has_integral_spike_interior:
  2807   assumes "\<forall>x\<in>{a<..<b}. g x = f x" "(f has_integral y) ({a..b})" shows "(g has_integral y) ({a..b})"
  2808   apply(rule has_integral_spike[OF negligible_frontier_interval _ assms(2)]) using assms(1) by auto
  2809 
  2810 lemma has_integral_spike_interior_eq:
  2811   assumes "\<forall>x\<in>{a<..<b}. g x = f x" shows "((f has_integral y) ({a..b}) \<longleftrightarrow> (g has_integral y) ({a..b}))"
  2812   apply rule apply(rule_tac[!] has_integral_spike_interior) using assms by auto
  2813 
  2814 lemma integrable_spike_interior: assumes "\<forall>x\<in>{a<..<b}. g x = f x" "f integrable_on {a..b}" shows "g integrable_on {a..b}"
  2815   using  assms unfolding integrable_on_def using has_integral_spike_interior[OF assms(1)] by auto
  2816 
  2817 subsection {* Integrability of continuous functions. *}
  2818 
  2819 lemma neutral_and[simp]: "neutral op \<and> = True"
  2820   unfolding neutral_def apply(rule some_equality) by auto
  2821 
  2822 lemma monoidal_and[intro]: "monoidal op \<and>" unfolding monoidal_def by auto
  2823 
  2824 lemma iterate_and[simp]: assumes "finite s" shows "(iterate op \<and>) s p \<longleftrightarrow> (\<forall>x\<in>s. p x)" using assms
  2825 apply induct unfolding iterate_insert[OF monoidal_and] by auto
  2826 
  2827 lemma operative_division_and: assumes "operative op \<and> P" "d division_of {a..b}"
  2828   shows "(\<forall>i\<in>d. P i) \<longleftrightarrow> P {a..b}"
  2829   using operative_division[OF monoidal_and assms] division_of_finite[OF assms(2)] by auto
  2830 
  2831 lemma operative_approximable: assumes "0 \<le> e" fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach"
  2832   shows "operative op \<and> (\<lambda>i. \<exists>g. (\<forall>x\<in>i. norm (f x - g (x::'b)) \<le> e) \<and> g integrable_on i)" unfolding operative_def neutral_and
  2833 proof safe fix a b::"'b" { assume "content {a..b} = 0"
  2834     thus "\<exists>g. (\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b}" 
  2835       apply(rule_tac x=f in exI) using assms by(auto intro!:integrable_on_null) }
  2836   { fix c k g assume as:"\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e" "g integrable_on {a..b}" and k:"k<DIM('b)"
  2837     show "\<exists>g. (\<forall>x\<in>{a..b} \<inter> {x. x $$ k \<le> c}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b} \<inter> {x. x $$ k \<le> c}"
  2838       "\<exists>g. (\<forall>x\<in>{a..b} \<inter> {x. c \<le> x $$ k}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b} \<inter> {x. c \<le> x $$ k}"
  2839       apply(rule_tac[!] x=g in exI) using as(1) integrable_split[OF as(2) k] by auto }
  2840   fix c k g1 g2 assume as:"\<forall>x\<in>{a..b} \<inter> {x. x $$ k \<le> c}. norm (f x - g1 x) \<le> e" "g1 integrable_on {a..b} \<inter> {x. x $$ k \<le> c}"
  2841                           "\<forall>x\<in>{a..b} \<inter> {x. c \<le> x $$ k}. norm (f x - g2 x) \<le> e" "g2 integrable_on {a..b} \<inter> {x. c \<le> x $$ k}"
  2842   assume k:"k<DIM('b)"
  2843   let ?g = "\<lambda>x. if x$$k = c then f x else if x$$k \<le> c then g1 x else g2 x"
  2844   show "\<exists>g. (\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b}" apply(rule_tac x="?g" in exI)
  2845   proof safe case goal1 thus ?case apply- apply(cases "x$$k=c", case_tac "x$$k < c") using as assms by auto
  2846   next case goal2 presume "?g integrable_on {a..b} \<inter> {x. x $$ k \<le> c}" "?g integrable_on {a..b} \<inter> {x. x $$ k \<ge> c}"
  2847     then guess h1 h2 unfolding integrable_on_def by auto from has_integral_split[OF this k] 
  2848     show ?case unfolding integrable_on_def by auto
  2849   next show "?g integrable_on {a..b} \<inter> {x. x $$ k \<le> c}" "?g integrable_on {a..b} \<inter> {x. x $$ k \<ge> c}"
  2850       apply(rule_tac[!] integrable_spike[OF negligible_standard_hyperplane[of k c]]) using k as(2,4) by auto qed qed
  2851 
  2852 lemma approximable_on_division: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach"
  2853   assumes "0 \<le> e" "d division_of {a..b}" "\<forall>i\<in>d. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  2854   obtains g where "\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e" "g integrable_on {a..b}"
  2855 proof- note * = operative_division[OF monoidal_and operative_approximable[OF assms(1)] assms(2)]
  2856   note this[unfolded iterate_and[OF division_of_finite[OF assms(2)]]] from assms(3)[unfolded this[of f]]
  2857   guess g .. thus thesis apply-apply(rule that[of g]) by auto qed
  2858 
  2859 lemma integrable_continuous: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach"
  2860   assumes "continuous_on {a..b} f" shows "f integrable_on {a..b}"
  2861 proof(rule integrable_uniform_limit,safe) fix e::real assume e:"0 < e"
  2862   from compact_uniformly_continuous[OF assms compact_interval,unfolded uniformly_continuous_on_def,rule_format,OF e] guess d ..
  2863   note d=conjunctD2[OF this,rule_format]
  2864   from fine_division_exists[OF gauge_ball[OF d(1)], of a b] guess p . note p=this
  2865   note p' = tagged_division_ofD[OF p(1)]
  2866   have *:"\<forall>i\<in>snd ` p. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  2867   proof(safe,unfold snd_conv) fix x l assume as:"(x,l) \<in> p" 
  2868     from p'(4)[OF this] guess a b apply-by(erule exE)+ note l=this
  2869     show "\<exists>g. (\<forall>x\<in>l. norm (f x - g x) \<le> e) \<and> g integrable_on l" apply(rule_tac x="\<lambda>y. f x" in exI)
  2870     proof safe show "(\<lambda>y. f x) integrable_on l" unfolding integrable_on_def l by(rule,rule has_integral_const)
  2871       fix y assume y:"y\<in>l" note fineD[OF p(2) as,unfolded subset_eq,rule_format,OF this]
  2872       note d(2)[OF _ _ this[unfolded mem_ball]]
  2873       thus "norm (f y - f x) \<le> e" using y p'(2-3)[OF as] unfolding dist_norm l norm_minus_commute by fastsimp qed qed
  2874   from e have "0 \<le> e" by auto from approximable_on_division[OF this division_of_tagged_division[OF p(1)] *] guess g .
  2875   thus "\<exists>g. (\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b}" by auto qed 
  2876 
  2877 subsection {* Specialization of additivity to one dimension. *}
  2878 
  2879 lemma operative_1_lt: assumes "monoidal opp"
  2880   shows "operative opp f \<longleftrightarrow> ((\<forall>a b. b \<le> a \<longrightarrow> f {a..b::real} = neutral opp) \<and>
  2881                 (\<forall>a b c. a < c \<and> c < b \<longrightarrow> opp (f{a..c})(f{c..b}) = f {a..b}))"
  2882   unfolding operative_def content_eq_0 DIM_real less_one dnf_simps(39,41) Eucl_real_simps
  2883     (* The dnf_simps simplify "\<exists> x. x= _ \<and> _" and "\<forall>k. k = _ \<longrightarrow> _" *)
  2884 proof safe fix a b c::"real" assume as:"\<forall>a b c. f {a..b} = opp (f ({a..b} \<inter> {x. x \<le> c}))
  2885     (f ({a..b} \<inter> {x. c \<le> x}))" "a < c" "c < b"
  2886     from this(2-) have "{a..b} \<inter> {x. x \<le> c} = {a..c}" "{a..b} \<inter> {x. x \<ge> c} = {c..b}" by auto
  2887     thus "opp (f {a..c}) (f {c..b}) = f {a..b}" unfolding as(1)[rule_format,of a b "c"] by auto
  2888 next fix a b c::real
  2889   assume as:"\<forall>a b. b \<le> a \<longrightarrow> f {a..b} = neutral opp" "\<forall>a b c. a < c \<and> c < b \<longrightarrow> opp (f {a..c}) (f {c..b}) = f {a..b}"
  2890   show "f {a..b} = opp (f ({a..b} \<inter> {x. x \<le> c})) (f ({a..b} \<inter> {x. c \<le> x}))"
  2891   proof(cases "c \<in> {a .. b}")
  2892     case False hence "c<a \<or> c>b" by auto
  2893     thus ?thesis apply-apply(erule disjE)
  2894     proof- assume "c<a" hence *:"{a..b} \<inter> {x. x \<le> c} = {1..0}"  "{a..b} \<inter> {x. c \<le> x} = {a..b}" by auto
  2895       show ?thesis unfolding * apply(subst as(1)[rule_format,of 0 1]) using assms by auto
  2896     next   assume "b<c" hence *:"{a..b} \<inter> {x. x \<le> c} = {a..b}"  "{a..b} \<inter> {x. c \<le> x} = {1..0}" by auto
  2897       show ?thesis unfolding * apply(subst as(1)[rule_format,of 0 1]) using assms by auto
  2898     qed
  2899   next case True hence *:"min (b) c = c" "max a c = c" by auto
  2900     have **:"0 < DIM(real)" by auto
  2901     have ***:"\<And>P Q. (\<chi>\<chi> i. if i = 0 then P i else Q i) = (P 0::real)" apply(subst euclidean_eq)
  2902       apply safe unfolding euclidean_lambda_beta' by auto
  2903     show ?thesis unfolding interval_split[OF **,unfolded Eucl_real_simps(1,3)] unfolding *** *
  2904     proof(cases "c = a \<or> c = b")
  2905       case False thus "f {a..b} = opp (f {a..c}) (f {c..b})"
  2906         apply-apply(subst as(2)[rule_format]) using True by auto
  2907     next case True thus "f {a..b} = opp (f {a..c}) (f {c..b})" apply-
  2908       proof(erule disjE) assume *:"c=a"
  2909         hence "f {a..c} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  2910         thus ?thesis using assms unfolding * by auto
  2911       next assume *:"c=b" hence "f {c..b} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  2912         thus ?thesis using assms unfolding * by auto qed qed qed qed
  2913 
  2914 lemma operative_1_le: assumes "monoidal opp"
  2915   shows "operative opp f \<longleftrightarrow> ((\<forall>a b. b \<le> a \<longrightarrow> f {a..b::real} = neutral opp) \<and>
  2916                 (\<forall>a b c. a \<le> c \<and> c \<le> b \<longrightarrow> opp (f{a..c})(f{c..b}) = f {a..b}))"
  2917 unfolding operative_1_lt[OF assms]
  2918 proof safe fix a b c::"real" assume as:"\<forall>a b c. a \<le> c \<and> c \<le> b \<longrightarrow> opp (f {a..c}) (f {c..b}) = f {a..b}" "a < c" "c < b"
  2919   show "opp (f {a..c}) (f {c..b}) = f {a..b}" apply(rule as(1)[rule_format]) using as(2-) by auto
  2920 next fix a b c ::"real" assume "\<forall>a b. b \<le> a \<longrightarrow> f {a..b} = neutral opp"
  2921     "\<forall>a b c. a < c \<and> c < b \<longrightarrow> opp (f {a..c}) (f {c..b}) = f {a..b}" "a \<le> c" "c \<le> b"
  2922   note as = this[rule_format]
  2923   show "opp (f {a..c}) (f {c..b}) = f {a..b}"
  2924   proof(cases "c = a \<or> c = b")
  2925     case False thus ?thesis apply-apply(subst as(2)) using as(3-) by(auto)
  2926     next case True thus ?thesis apply-
  2927       proof(erule disjE) assume *:"c=a" hence "f {a..c} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  2928         thus ?thesis using assms unfolding * by auto
  2929       next               assume *:"c=b" hence "f {c..b} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  2930         thus ?thesis using assms unfolding * by auto qed qed qed 
  2931 
  2932 subsection {* Special case of additivity we need for the FCT. *}
  2933 
  2934 lemma interval_bound_sing[simp]: "interval_upperbound {a} = a"  "interval_lowerbound {a} = a"
  2935   unfolding interval_upperbound_def interval_lowerbound_def  by auto
  2936 
  2937 lemma additive_tagged_division_1: fixes f::"real \<Rightarrow> 'a::real_normed_vector"
  2938   assumes "a \<le> b" "p tagged_division_of {a..b}"
  2939   shows "setsum (\<lambda>(x,k). f(interval_upperbound k) - f(interval_lowerbound k)) p = f b - f a"
  2940 proof- let ?f = "(\<lambda>k::(real) set. if k = {} then 0 else f(interval_upperbound k) - f(interval_lowerbound k))"
  2941   have ***:"\<forall>i<DIM(real). a $$ i \<le> b $$ i" using assms by auto 
  2942   have *:"operative op + ?f" unfolding operative_1_lt[OF monoidal_monoid] interval_eq_empty by auto
  2943   have **:"{a..b} \<noteq> {}" using assms(1) by auto note operative_tagged_division[OF monoidal_monoid * assms(2)]
  2944   note * = this[unfolded if_not_P[OF **] interval_bounds[OF ***],THEN sym]
  2945   show ?thesis unfolding * apply(subst setsum_iterate[THEN sym]) defer
  2946     apply(rule setsum_cong2) unfolding split_paired_all split_conv using assms(2) by auto qed
  2947 
  2948 subsection {* A useful lemma allowing us to factor out the content size. *}
  2949 
  2950 lemma has_integral_factor_content:
  2951   "(f has_integral i) {a..b} \<longleftrightarrow> (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p
  2952     \<longrightarrow> norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content {a..b}))"
  2953 proof(cases "content {a..b} = 0")
  2954   case True show ?thesis unfolding has_integral_null_eq[OF True] apply safe
  2955     apply(rule,rule,rule gauge_trivial,safe) unfolding setsum_content_null[OF True] True defer 
  2956     apply(erule_tac x=1 in allE,safe) defer apply(rule fine_division_exists[of _ a b],assumption)
  2957     apply(erule_tac x=p in allE) unfolding setsum_content_null[OF True] by auto
  2958 next case False note F = this[unfolded content_lt_nz[THEN sym]]
  2959   let ?P = "\<lambda>e opp. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> opp (norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i)) e)"
  2960   show ?thesis apply(subst has_integral)
  2961   proof safe fix e::real assume e:"e>0"
  2962     { assume "\<forall>e>0. ?P e op <" thus "?P (e * content {a..b}) op \<le>" apply(erule_tac x="e * content {a..b}" in allE)
  2963         apply(erule impE) defer apply(erule exE,rule_tac x=d in exI)
  2964         using F e by(auto simp add:field_simps intro:mult_pos_pos) }
  2965     {  assume "\<forall>e>0. ?P (e * content {a..b}) op \<le>" thus "?P e op <" apply(erule_tac x="e / 2 / content {a..b}" in allE)
  2966         apply(erule impE) defer apply(erule exE,rule_tac x=d in exI)
  2967         using F e by(auto simp add:field_simps intro:mult_pos_pos) } qed qed
  2968 
  2969 subsection {* Fundamental theorem of calculus. *}
  2970 
  2971 lemma interval_bounds_real: assumes "a\<le>(b::real)"
  2972   shows "interval_upperbound {a..b} = b" "interval_lowerbound {a..b} = a"
  2973   apply(rule_tac[!] interval_bounds) using assms by auto
  2974 
  2975 lemma fundamental_theorem_of_calculus: fixes f::"real \<Rightarrow> 'a::banach"
  2976   assumes "a \<le> b"  "\<forall>x\<in>{a..b}. (f has_vector_derivative f' x) (at x within {a..b})"
  2977   shows "(f' has_integral (f b - f a)) ({a..b})"
  2978 unfolding has_integral_factor_content
  2979 proof safe fix e::real assume e:"e>0"
  2980   note assm = assms(2)[unfolded has_vector_derivative_def has_derivative_within_alt]
  2981   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" using e by blast
  2982   note this[OF assm,unfolded gauge_existence_lemma] from choice[OF this,unfolded Ball_def[symmetric]]
  2983   guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
  2984   show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow>
  2985                  norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content {a..b})"
  2986     apply(rule_tac x="\<lambda>x. ball x (d x)" in exI,safe)
  2987     apply(rule gauge_ball_dependent,rule,rule d(1))
  2988   proof- fix p assume as:"p tagged_division_of {a..b}" "(\<lambda>x. ball x (d x)) fine p"
  2989     show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content {a..b}" 
  2990       unfolding content_real[OF assms(1)] additive_tagged_division_1[OF assms(1) as(1),of f,THEN sym]
  2991       unfolding additive_tagged_division_1[OF assms(1) as(1),of "\<lambda>x. x",THEN sym]
  2992       unfolding setsum_right_distrib defer unfolding setsum_subtractf[THEN sym] 
  2993     proof(rule setsum_norm_le,safe) fix x k assume "(x,k)\<in>p"
  2994       note xk = tagged_division_ofD(2-4)[OF as(1) this] from this(3) guess u v apply-by(erule exE)+ note k=this
  2995       have *:"u \<le> v" using xk unfolding k by auto
  2996       have ball:"\<forall>xa\<in>k. xa \<in> ball x (d x)" using as(2)[unfolded fine_def,rule_format,OF `(x,k)\<in>p`,
  2997         unfolded split_conv subset_eq] .
  2998       have "norm ((v - u) *\<^sub>R f' x - (f v - f u)) \<le>
  2999         norm (f u - f x - (u - x) *\<^sub>R f' x) + norm (f v - f x - (v - x) *\<^sub>R f' x)"
  3000         apply(rule order_trans[OF _ norm_triangle_ineq4]) apply(rule eq_refl) apply(rule arg_cong[where f=norm])
  3001         unfolding scaleR.diff_left by(auto simp add:algebra_simps)
  3002       also have "... \<le> e * norm (u - x) + e * norm (v - x)"
  3003         apply(rule add_mono) apply(rule d(2)[of "x" "u",unfolded o_def]) prefer 4
  3004         apply(rule d(2)[of "x" "v",unfolded o_def])
  3005         using ball[rule_format,of u] ball[rule_format,of v] 
  3006         using xk(1-2) unfolding k subset_eq by(auto simp add:dist_real_def) 
  3007       also have "... \<le> e * (interval_upperbound k - interval_lowerbound k)"
  3008         unfolding k interval_bounds_real[OF *] using xk(1) unfolding k by(auto simp add:dist_real_def field_simps)
  3009       finally show "norm (content k *\<^sub>R f' x - (f (interval_upperbound k) - f (interval_lowerbound k))) \<le>
  3010         e * (interval_upperbound k - interval_lowerbound k)" unfolding k interval_bounds_real[OF *] content_real[OF *] .
  3011     qed(insert as, auto) qed qed
  3012 
  3013 subsection {* Attempt a systematic general set of "offset" results for components. *}
  3014 
  3015 lemma gauge_modify:
  3016   assumes "(\<forall>s. open s \<longrightarrow> open {x. f(x) \<in> s})" "gauge d"
  3017   shows "gauge (\<lambda>x y. d (f x) (f y))"
  3018   using assms unfolding gauge_def apply safe defer apply(erule_tac x="f x" in allE)
  3019   apply(erule_tac x="d (f x)" in allE) unfolding mem_def Collect_def by auto
  3020 
  3021 subsection {* Only need trivial subintervals if the interval itself is trivial. *}
  3022 
  3023 lemma division_of_nontrivial: fixes s::"('a::ordered_euclidean_space) set set"
  3024   assumes "s division_of {a..b}" "content({a..b}) \<noteq> 0"
  3025   shows "{k. k \<in> s \<and> content k \<noteq> 0} division_of {a..b}" using assms(1) apply-
  3026 proof(induct "card s" arbitrary:s rule:nat_less_induct)
  3027   fix s::"'a set set" assume assm:"s division_of {a..b}"
  3028     "\<forall>m<card s. \<forall>x. m = card x \<longrightarrow> x division_of {a..b} \<longrightarrow> {k \<in> x. content k \<noteq> 0} division_of {a..b}" 
  3029   note s = division_ofD[OF assm(1)] let ?thesis = "{k \<in> s. content k \<noteq> 0} division_of {a..b}"
  3030   { presume *:"{k \<in> s. content k \<noteq> 0} \<noteq> s \<Longrightarrow> ?thesis"
  3031     show ?thesis apply cases defer apply(rule *,assumption) using assm(1) by auto }
  3032   assume noteq:"{k \<in> s. content k \<noteq> 0} \<noteq> s"
  3033   then obtain k where k:"k\<in>s" "content k = 0" by auto
  3034   from s(4)[OF k(1)] guess c d apply-by(erule exE)+ note k=k this
  3035   from k have "card s > 0" unfolding card_gt_0_iff using assm(1) by auto
  3036   hence card:"card (s - {k}) < card s" using assm(1) k(1) apply(subst card_Diff_singleton_if) by auto
  3037   have *:"closed (\<Union>(s - {k}))" apply(rule closed_Union) defer apply rule apply(drule DiffD1,drule s(4))
  3038     apply safe apply(rule closed_interval) using assm(1) by auto
  3039   have "k \<subseteq> \<Union>(s - {k})" apply safe apply(rule *[unfolded closed_limpt,rule_format]) unfolding islimpt_approachable
  3040   proof safe fix x and e::real assume as:"x\<in>k" "e>0"
  3041     from k(2)[unfolded k content_eq_0] guess i .. 
  3042     hence i:"c$$i = d$$i" "i<DIM('a)" using s(3)[OF k(1),unfolded k] unfolding interval_ne_empty by auto
  3043     hence xi:"x$$i = d$$i" using as unfolding k mem_interval by smt
  3044     def y \<equiv> "(\<chi>\<chi> j. if j = i then if c$$i \<le> (a$$i + b$$i) / 2 then c$$i +
  3045       min e (b$$i - c$$i) / 2 else c$$i - min e (c$$i - a$$i) / 2 else x$$j)::'a"
  3046     show "\<exists>x'\<in>\<Union>(s - {k}). x' \<noteq> x \<and> dist x' x < e" apply(rule_tac x=y in bexI) 
  3047     proof have "d \<in> {c..d}" using s(3)[OF k(1)] unfolding k interval_eq_empty mem_interval by(fastsimp simp add: not_less)
  3048       hence "d \<in> {a..b}" using s(2)[OF k(1)] unfolding k by auto note di = this[unfolded mem_interval,THEN spec[where x=i]]
  3049       hence xyi:"y$$i \<noteq> x$$i" unfolding y_def unfolding i xi euclidean_lambda_beta'[OF i(2)] if_P[OF refl]
  3050         apply(cases) apply(subst if_P,assumption) unfolding if_not_P not_le using as(2)
  3051         using assms(2)[unfolded content_eq_0] using i(2) by smt+ 
  3052       thus "y \<noteq> x" unfolding euclidean_eq[where 'a='a] using i by auto
  3053       have *:"{..<DIM('a)} = insert i ({..<DIM('a)} - {i})" using i by auto
  3054       have "norm (y - x) < e + setsum (\<lambda>i. 0) {..<DIM('a)}" apply(rule le_less_trans[OF norm_le_l1])
  3055         apply(subst *,subst setsum_insert) prefer 3 apply(rule add_less_le_mono)
  3056       proof- show "\<bar>(y - x) $$ i\<bar> < e" unfolding y_def euclidean_simps euclidean_lambda_beta'[OF i(2)] if_P[OF refl]
  3057           apply(cases) apply(subst if_P,assumption) unfolding if_not_P unfolding i xi using di as(2) by auto
  3058         show "(\<Sum>i\<in>{..<DIM('a)} - {i}. \<bar>(y - x) $$ i\<bar>) \<le> (\<Sum>i\<in>{..<DIM('a)}. 0)" unfolding y_def by auto 
  3059       qed auto thus "dist y x < e" unfolding dist_norm by auto
  3060       have "y\<notin>k" unfolding k mem_interval apply rule apply(erule_tac x=i in allE) using xyi unfolding k i xi by auto
  3061       moreover have "y \<in> \<Union>s" unfolding s mem_interval
  3062       proof(rule,rule) note simps = y_def euclidean_lambda_beta' if_not_P
  3063         fix j assume j:"j<DIM('a)" show "a $$ j \<le> y $$ j \<and> y $$ j \<le> b $$ j" 
  3064         proof(cases "j = i") case False have "x \<in> {a..b}" using s(2)[OF k(1)] as(1) by auto
  3065           thus ?thesis using j apply- unfolding simps if_not_P[OF False] unfolding mem_interval by auto
  3066         next case True note T = this show ?thesis
  3067           proof(cases "c $$ i \<le> (a $$ i + b $$ i) / 2")
  3068             case True show ?thesis unfolding simps if_P[OF T] if_P[OF True] unfolding i
  3069               using True as(2) di apply-apply rule unfolding T by (auto simp add:field_simps) 
  3070           next case False thus ?thesis unfolding simps if_P[OF T] if_not_P[OF False] unfolding i
  3071               using True as(2) di apply-apply rule unfolding T by (auto simp add:field_simps)
  3072           qed qed qed
  3073       ultimately show "y \<in> \<Union>(s - {k})" by auto
  3074     qed qed hence "\<Union>(s - {k}) = {a..b}" unfolding s(6)[THEN sym] by auto
  3075   hence  "{ka \<in> s - {k}. content ka \<noteq> 0} division_of {a..b}" apply-apply(rule assm(2)[rule_format,OF card refl])
  3076     apply(rule division_ofI) defer apply(rule_tac[1-4] s) using assm(1) by auto
  3077   moreover have "{ka \<in> s - {k}. content ka \<noteq> 0} = {k \<in> s. content k \<noteq> 0}" using k by auto ultimately show ?thesis by auto qed
  3078 
  3079 subsection {* Integrabibility on subintervals. *}
  3080 
  3081 lemma operative_integrable: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach" shows
  3082   "operative op \<and> (\<lambda>i. f integrable_on i)"
  3083   unfolding operative_def neutral_and apply safe apply(subst integrable_on_def)
  3084   unfolding has_integral_null_eq apply(rule,rule refl) apply(rule,assumption,assumption)+
  3085   unfolding integrable_on_def by(auto intro!: has_integral_split)
  3086 
  3087 lemma integrable_subinterval: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  3088   assumes "f integrable_on {a..b}" "{c..d} \<subseteq> {a..b}" shows "f integrable_on {c..d}" 
  3089   apply(cases "{c..d} = {}") defer apply(rule partial_division_extend_1[OF assms(2)],assumption)
  3090   using operative_division_and[OF operative_integrable,THEN sym,of _ _ _ f] assms(1) by auto
  3091 
  3092 subsection {* Combining adjacent intervals in 1 dimension. *}
  3093 
  3094 lemma has_integral_combine: assumes "(a::real) \<le> c" "c \<le> b"
  3095   "(f has_integral i) {a..c}" "(f has_integral (j::'a::banach)) {c..b}"
  3096   shows "(f has_integral (i + j)) {a..b}"
  3097 proof- note operative_integral[of f, unfolded operative_1_le[OF monoidal_lifted[OF monoidal_monoid]]]
  3098   note conjunctD2[OF this,rule_format] note * = this(2)[OF conjI[OF assms(1-2)],unfolded if_P[OF assms(3)]]
  3099   hence "f integrable_on {a..b}" apply- apply(rule ccontr) apply(subst(asm) if_P) defer
  3100     apply(subst(asm) if_P) using assms(3-) by auto
  3101   with * show ?thesis apply-apply(subst(asm) if_P) defer apply(subst(asm) if_P) defer apply(subst(asm) if_P)
  3102     unfolding lifted.simps using assms(3-) by(auto simp add: integrable_on_def integral_unique) qed
  3103 
  3104 lemma integral_combine: fixes f::"real \<Rightarrow> 'a::banach"
  3105   assumes "a \<le> c" "c \<le> b" "f integrable_on ({a..b})"
  3106   shows "integral {a..c} f + integral {c..b} f = integral({a..b}) f"
  3107   apply(rule integral_unique[THEN sym]) apply(rule has_integral_combine[OF assms(1-2)])
  3108   apply(rule_tac[!] integrable_integral integrable_subinterval[OF assms(3)])+ using assms(1-2) by auto
  3109 
  3110 lemma integrable_combine: fixes f::"real \<Rightarrow> 'a::banach"
  3111   assumes "a \<le> c" "c \<le> b" "f integrable_on {a..c}" "f integrable_on {c..b}"
  3112   shows "f integrable_on {a..b}" using assms unfolding integrable_on_def by(fastsimp intro!:has_integral_combine)
  3113 
  3114 subsection {* Reduce integrability to "local" integrability. *}
  3115 
  3116 lemma integrable_on_little_subintervals: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach"
  3117   assumes "\<forall>x\<in>{a..b}. \<exists>d>0. \<forall>u v. x \<in> {u..v} \<and> {u..v} \<subseteq> ball x d \<and> {u..v} \<subseteq> {a..b} \<longrightarrow> f integrable_on {u..v}"
  3118   shows "f integrable_on {a..b}"
  3119 proof- have "\<forall>x. \<exists>d. x\<in>{a..b} \<longrightarrow> d>0 \<and> (\<forall>u v. x \<in> {u..v} \<and> {u..v} \<subseteq> ball x d \<and> {u..v} \<subseteq> {a..b} \<longrightarrow> f integrable_on {u..v})"
  3120     using assms by auto note this[unfolded gauge_existence_lemma] from choice[OF this] guess d .. note d=this[rule_format]
  3121   guess p apply(rule fine_division_exists[OF gauge_ball_dependent,of d a b]) using d by auto note p=this(1-2)
  3122   note division_of_tagged_division[OF this(1)] note * = operative_division_and[OF operative_integrable,OF this,THEN sym,of f]
  3123   show ?thesis unfolding * apply safe unfolding snd_conv
  3124   proof- fix x k assume "(x,k) \<in> p" note tagged_division_ofD(2-4)[OF p(1) this] fineD[OF p(2) this]
  3125     thus "f integrable_on k" apply safe apply(rule d[THEN conjunct2,rule_format,of x]) by auto qed qed
  3126 
  3127 subsection {* Second FCT or existence of antiderivative. *}
  3128 
  3129 lemma integrable_const[intro]:"(\<lambda>x. c) integrable_on {a..b}"
  3130   unfolding integrable_on_def by(rule,rule has_integral_const)
  3131 
  3132 lemma integral_has_vector_derivative: fixes f::"real \<Rightarrow> 'a::banach"
  3133   assumes "continuous_on {a..b} f" "x \<in> {a..b}"
  3134   shows "((\<lambda>u. integral {a..u} f) has_vector_derivative f(x)) (at x within {a..b})"
  3135   unfolding has_vector_derivative_def has_derivative_within_alt
  3136 apply safe apply(rule scaleR.bounded_linear_left)
  3137 proof- fix e::real assume e:"e>0"
  3138   note compact_uniformly_continuous[OF assms(1) compact_interval,unfolded uniformly_continuous_on_def]
  3139   from this[rule_format,OF e] guess d apply-by(erule conjE exE)+ note d=this[rule_format]
  3140   let ?I = "\<lambda>a b. integral {a..b} f"
  3141   show "\<exists>d>0. \<forall>y\<in>{a..b}. norm (y - x) < d \<longrightarrow> norm (?I a y - ?I a x - (y - x) *\<^sub>R f x) \<le> e * norm (y - x)"
  3142   proof(rule,rule,rule d,safe) case goal1 show ?case proof(cases "y < x")
  3143       case False have "f integrable_on {a..y}" apply(rule integrable_subinterval,rule integrable_continuous)
  3144         apply(rule assms)  unfolding not_less using assms(2) goal1 by auto
  3145       hence *:"?I a y - ?I a x = ?I x y" unfolding algebra_simps apply(subst eq_commute) apply(rule integral_combine)
  3146         using False unfolding not_less using assms(2) goal1 by auto
  3147       have **:"norm (y - x) = content {x..y}" apply(subst content_real) using False unfolding not_less by auto
  3148       show ?thesis unfolding ** apply(rule has_integral_bound[where f="(\<lambda>u. f u - f x)"]) unfolding * unfolding o_def
  3149         defer apply(rule has_integral_sub) apply(rule integrable_integral)
  3150         apply(rule integrable_subinterval,rule integrable_continuous) apply(rule assms)+
  3151       proof- show "{x..y} \<subseteq> {a..b}" using goal1 assms(2) by auto
  3152         have *:"y - x = norm(y - x)" using False by auto
  3153         show "((\<lambda>xa. f x) has_integral (y - x) *\<^sub>R f x) {x.. y}" apply(subst *) unfolding ** by auto
  3154         show "\<forall>xa\<in>{x..y}. norm (f xa - f x) \<le> e" apply safe apply(rule less_imp_le)
  3155           apply(rule d(2)[unfolded dist_norm]) using assms(2) using goal1 by auto
  3156       qed(insert e,auto)
  3157     next case True have "f integrable_on {a..x}" apply(rule integrable_subinterval,rule integrable_continuous)
  3158         apply(rule assms)+  unfolding not_less using assms(2) goal1 by auto
  3159       hence *:"?I a x - ?I a y = ?I y x" unfolding algebra_simps apply(subst eq_commute) apply(rule integral_combine)
  3160         using True using assms(2) goal1 by auto
  3161       have **:"norm (y - x) = content {y..x}" apply(subst content_real) using True unfolding not_less by auto
  3162       have ***:"\<And>fy fx c::'a. fx - fy - (y - x) *\<^sub>R c = -(fy - fx - (x - y) *\<^sub>R c)" unfolding scaleR_left.diff by auto 
  3163       show ?thesis apply(subst ***) unfolding norm_minus_cancel **
  3164         apply(rule has_integral_bound[where f="(\<lambda>u. f u - f x)"]) unfolding * unfolding o_def
  3165         defer apply(rule has_integral_sub) apply(subst minus_minus[THEN sym]) unfolding minus_minus
  3166         apply(rule integrable_integral) apply(rule integrable_subinterval,rule integrable_continuous) apply(rule assms)+
  3167       proof- show "{y..x} \<subseteq> {a..b}" using goal1 assms(2) by auto
  3168         have *:"x - y = norm(y - x)" using True by auto
  3169         show "((\<lambda>xa. f x) has_integral (x - y) *\<^sub>R f x) {y..x}" apply(subst *) unfolding ** by auto
  3170         show "\<forall>xa\<in>{y..x}. norm (f xa - f x) \<le> e" apply safe apply(rule less_imp_le)
  3171           apply(rule d(2)[unfolded dist_norm]) using assms(2) using goal1 by auto
  3172       qed(insert e,auto) qed qed qed
  3173 
  3174 lemma antiderivative_continuous: assumes "continuous_on {a..b::real} f"
  3175   obtains g where "\<forall>x\<in> {a..b}. (g has_vector_derivative (f(x)::_::banach)) (at x within {a..b})"
  3176   apply(rule that,rule) using integral_has_vector_derivative[OF assms] by auto
  3177 
  3178 subsection {* Combined fundamental theorem of calculus. *}
  3179 
  3180 lemma antiderivative_integral_continuous: fixes f::"real \<Rightarrow> 'a::banach" assumes "continuous_on {a..b} f"
  3181   obtains g where "\<forall>u\<in>{a..b}. \<forall>v \<in> {a..b}. u \<le> v \<longrightarrow> (f has_integral (g v - g u)) {u..v}"
  3182 proof- from antiderivative_continuous[OF assms] guess g . note g=this
  3183   show ?thesis apply(rule that[of g])
  3184   proof safe case goal1 have "\<forall>x\<in>{u..v}. (g has_vector_derivative f x) (at x within {u..v})"
  3185       apply(rule,rule has_vector_derivative_within_subset) apply(rule g[rule_format]) using goal1(1-2) by auto
  3186     thus ?case using fundamental_theorem_of_calculus[OF goal1(3),of "g" "f"] by auto qed qed
  3187 
  3188 subsection {* General "twiddling" for interval-to-interval function image. *}
  3189 
  3190 lemma has_integral_twiddle:
  3191   assumes "0 < r" "\<forall>x. h(g x) = x" "\<forall>x. g(h x) = x" "\<forall>x. continuous (at x) g"
  3192   "\<forall>u v. \<exists>w z. g ` {u..v} = {w..z}"
  3193   "\<forall>u v. \<exists>w z. h ` {u..v} = {w..z}"
  3194   "\<forall>u v. content(g ` {u..v}) = r * content {u..v}"
  3195   "(f has_integral i) {a..b}"
  3196   shows "((\<lambda>x. f(g x)) has_integral (1 / r) *\<^sub>R i) (h ` {a..b})"
  3197 proof- { presume *:"{a..b} \<noteq> {} \<Longrightarrow> ?thesis"
  3198     show ?thesis apply cases defer apply(rule *,assumption)
  3199     proof- case goal1 thus ?thesis unfolding goal1 assms(8)[unfolded goal1 has_integral_empty_eq] by auto qed }
  3200   assume "{a..b} \<noteq> {}" from assms(6)[rule_format,of a b] guess w z apply-by(erule exE)+ note wz=this
  3201   have inj:"inj g" "inj h" unfolding inj_on_def apply safe apply(rule_tac[!] ccontr)
  3202     using assms(2) apply(erule_tac x=x in allE) using assms(2) apply(erule_tac x=y in allE) defer
  3203     using assms(3) apply(erule_tac x=x in allE) using assms(3) apply(erule_tac x=y in allE) by auto
  3204   show ?thesis unfolding has_integral_def has_integral_compact_interval_def apply(subst if_P) apply(rule,rule,rule wz)
  3205   proof safe fix e::real assume e:"e>0" hence "e * r > 0" using assms(1) by(rule mult_pos_pos)
  3206     from assms(8)[unfolded has_integral,rule_format,OF this] guess d apply-by(erule exE conjE)+ note d=this[rule_format]
  3207     def d' \<equiv> "\<lambda>x y. d (g x) (g y)" have d':"\<And>x. d' x = {y. g y \<in> (d (g x))}" unfolding d'_def by(auto simp add:mem_def)
  3208     show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of h ` {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i) < e)"
  3209     proof(rule_tac x=d' in exI,safe) show "gauge d'" using d(1) unfolding gauge_def d' using continuous_open_preimage_univ[OF assms(4)] by auto
  3210       fix p assume as:"p tagged_division_of h ` {a..b}" "d' fine p" note p = tagged_division_ofD[OF as(1)] 
  3211       have "(\<lambda>(x, k). (g x, g ` k)) ` p tagged_division_of {a..b} \<and> d fine (\<lambda>(x, k). (g x, g ` k)) ` p" unfolding tagged_division_of 
  3212       proof safe show "finite ((\<lambda>(x, k). (g x, g ` k)) ` p)" using as by auto
  3213         show "d fine (\<lambda>(x, k). (g x, g ` k)) ` p" using as(2) unfolding fine_def d' by auto
  3214         fix x k assume xk[intro]:"(x,k) \<in> p" show "g x \<in> g ` k" using p(2)[OF xk] by auto
  3215         show "\<exists>u v. g ` k = {u..v}" using p(4)[OF xk] using assms(5-6) by auto
  3216         { fix y assume "y \<in> k" thus "g y \<in> {a..b}" "g y \<in> {a..b}" using p(3)[OF xk,unfolded subset_eq,rule_format,of "h (g y)"]
  3217             using assms(2)[rule_format,of y] unfolding inj_image_mem_iff[OF inj(2)] by auto }
  3218         fix x' k' assume xk':"(x',k') \<in> p" fix z assume "z \<in> interior (g ` k)" "z \<in> interior (g ` k')"
  3219         hence *:"interior (g ` k) \<inter> interior (g ` k') \<noteq> {}" by auto
  3220         have same:"(x, k) = (x', k')" apply-apply(rule ccontr,drule p(5)[OF xk xk'])
  3221         proof- assume as:"interior k \<inter> interior k' = {}" from nonempty_witness[OF *] guess z .
  3222           hence "z \<in> g ` (interior k \<inter> interior k')" using interior_image_subset[OF assms(4) inj(1)]
  3223             unfolding image_Int[OF inj(1)] by auto thus False using as by blast
  3224         qed thus "g x = g x'" by auto
  3225         { fix z assume "z \<in> k"  thus  "g z \<in> g ` k'" using same by auto }
  3226         { fix z assume "z \<in> k'" thus  "g z \<in> g ` k"  using same by auto }
  3227       next fix x assume "x \<in> {a..b}" hence "h x \<in>  \<Union>{k. \<exists>x. (x, k) \<in> p}" using p(6) by auto
  3228         then guess X unfolding Union_iff .. note X=this from this(1) guess y unfolding mem_Collect_eq ..
  3229         thus "x \<in> \<Union>{k. \<exists>x. (x, k) \<in> (\<lambda>(x, k). (g x, g ` k)) ` p}" apply-
  3230           apply(rule_tac X="g ` X" in UnionI) defer apply(rule_tac x="h x" in image_eqI)
  3231           using X(2) assms(3)[rule_format,of x] by auto
  3232       qed note ** = d(2)[OF this] have *:"inj_on (\<lambda>(x, k). (g x, g ` k)) p" using inj(1) unfolding inj_on_def by fastsimp
  3233        have "(\<Sum>(x, k)\<in>(\<lambda>(x, k). (g x, g ` k)) ` p. content k *\<^sub>R f x) - i = r *\<^sub>R (\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - i" (is "?l = _") unfolding algebra_simps add_left_cancel
  3234         unfolding setsum_reindex[OF *] apply(subst scaleR_right.setsum) defer apply(rule setsum_cong2) unfolding o_def split_paired_all split_conv
  3235         apply(drule p(4)) apply safe unfolding assms(7)[rule_format] using p by auto
  3236       also have "... = r *\<^sub>R ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i)" (is "_ = ?r") unfolding scaleR.diff_right scaleR.scaleR_left[THEN sym]
  3237         unfolding real_scaleR_def using assms(1) by auto finally have *:"?l = ?r" .
  3238       show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i) < e" using ** unfolding * unfolding norm_scaleR
  3239         using assms(1) by(auto simp add:field_simps) qed qed qed
  3240 
  3241 subsection {* Special case of a basic affine transformation. *}
  3242 
  3243 lemma interval_image_affinity_interval: shows "\<exists>u v. (\<lambda>x. m *\<^sub>R (x::'a::ordered_euclidean_space) + c) ` {a..b} = {u..v}"
  3244   unfolding image_affinity_interval by auto
  3245 
  3246 lemma setprod_cong2: assumes "\<And>x. x \<in> A \<Longrightarrow> f x = g x" shows "setprod f A = setprod g A"
  3247   apply(rule setprod_cong) using assms by auto
  3248 
  3249 lemma content_image_affinity_interval: 
  3250  "content((\<lambda>x::'a::ordered_euclidean_space. m *\<^sub>R x + c) ` {a..b}) = (abs m) ^ DIM('a) * content {a..b}" (is "?l = ?r")
  3251 proof- { presume *:"{a..b}\<noteq>{} \<Longrightarrow> ?thesis" show ?thesis apply(cases,rule *,assumption)
  3252       unfolding not_not using content_empty by auto }
  3253   have *:"DIM('a) = card {..<DIM('a)}" by auto
  3254   assume as:"{a..b}\<noteq>{}" show ?thesis proof(cases "m \<ge> 0")
  3255     case True show ?thesis unfolding image_affinity_interval if_not_P[OF as] if_P[OF True]
  3256       unfolding content_closed_interval'[OF as] apply(subst content_closed_interval') defer apply(subst(2) *)
  3257       apply(subst setprod_constant[THEN sym]) apply(rule finite_lessThan) unfolding setprod_timesf[THEN sym]
  3258       apply(rule setprod_cong2) using True as unfolding interval_ne_empty euclidean_simps not_le  
  3259       by(auto simp add:field_simps intro:mult_left_mono)
  3260   next case False show ?thesis unfolding image_affinity_interval if_not_P[OF as] if_not_P[OF False]
  3261       unfolding content_closed_interval'[OF as] apply(subst content_closed_interval') defer apply(subst(2) *)
  3262       apply(subst setprod_constant[THEN sym]) apply(rule finite_lessThan) unfolding setprod_timesf[THEN sym]
  3263       apply(rule setprod_cong2) using False as unfolding interval_ne_empty euclidean_simps not_le 
  3264       by(auto simp add:field_simps mult_le_cancel_left_neg) qed qed
  3265 
  3266 lemma has_integral_affinity: fixes a::"'a::ordered_euclidean_space" assumes "(f has_integral i) {a..b}" "m \<noteq> 0"
  3267   shows "((\<lambda>x. f(m *\<^sub>R x + c)) has_integral ((1 / (abs(m) ^ DIM('a))) *\<^sub>R i)) ((\<lambda>x. (1 / m) *\<^sub>R x + -((1 / m) *\<^sub>R c)) ` {a..b})"
  3268   apply(rule has_integral_twiddle,safe) apply(rule zero_less_power) unfolding euclidean_eq[where 'a='a]
  3269   unfolding scaleR_right_distrib euclidean_simps scaleR.scaleR_left[THEN sym]
  3270   defer apply(insert assms(2), simp add:field_simps) apply(insert assms(2), simp add:field_simps)
  3271   apply(rule continuous_intros)+ apply(rule interval_image_affinity_interval)+ apply(rule content_image_affinity_interval) using assms by auto
  3272 
  3273 lemma integrable_affinity: assumes "f integrable_on {a..b}" "m \<noteq> 0"
  3274   shows "(\<lambda>x. f(m *\<^sub>R x + c)) integrable_on ((\<lambda>x. (1 / m) *\<^sub>R x + -((1/m) *\<^sub>R c)) ` {a..b})"
  3275   using assms unfolding integrable_on_def apply safe apply(drule has_integral_affinity) by auto
  3276 
  3277 subsection {* Special case of stretching coordinate axes separately. *}
  3278 
  3279 lemma image_stretch_interval:
  3280   "(\<lambda>x. \<chi>\<chi> k. m k * x$$k) ` {a..b::'a::ordered_euclidean_space} =
  3281   (if {a..b} = {} then {} else {(\<chi>\<chi> k. min (m(k) * a$$k) (m(k) * b$$k))::'a ..  (\<chi>\<chi> k. max (m(k) * a$$k) (m(k) * b$$k))})"
  3282   (is "?l = ?r")
  3283 proof(cases "{a..b}={}") case True thus ?thesis unfolding True by auto
  3284 next have *:"\<And>P Q. (\<forall>i<DIM('a). P i) \<and> (\<forall>i<DIM('a). Q i) \<longleftrightarrow> (\<forall>i<DIM('a). P i \<and> Q i)" by auto
  3285   case False note ab = this[unfolded interval_ne_empty]
  3286   show ?thesis apply-apply(rule set_eqI)
  3287   proof- fix x::"'a" have **:"\<And>P Q. (\<forall>i<DIM('a). P i = Q i) \<Longrightarrow> (\<forall>i<DIM('a). P i) = (\<forall>i<DIM('a). Q i)" by auto
  3288     show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" unfolding if_not_P[OF False] 
  3289       unfolding image_iff mem_interval Bex_def euclidean_simps euclidean_eq[where 'a='a] *
  3290       unfolding imp_conjR[THEN sym] apply(subst euclidean_lambda_beta'') apply(subst lambda_skolem'[THEN sym])
  3291       apply(rule **,rule,rule) unfolding euclidean_lambda_beta'
  3292     proof- fix i assume i:"i<DIM('a)" show "(\<exists>xa. (a $$ i \<le> xa \<and> xa \<le> b $$ i) \<and> x $$ i = m i * xa) =
  3293         (min (m i * a $$ i) (m i * b $$ i) \<le> x $$ i \<and> x $$ i \<le> max (m i * a $$ i) (m i * b $$ i))"
  3294       proof(cases "m i = 0") case True thus ?thesis using ab i by auto
  3295       next case False hence "0 < m i \<or> 0 > m i" by auto thus ?thesis apply-
  3296         proof(erule disjE) assume as:"0 < m i" hence *:"min (m i * a $$ i) (m i * b $$ i) = m i * a $$ i"
  3297             "max (m i * a $$ i) (m i * b $$ i) = m i * b $$ i" using ab i unfolding min_def max_def by auto
  3298           show ?thesis unfolding * apply rule defer apply(rule_tac x="1 / m i * x$$i" in exI)
  3299             using as by(auto simp add:field_simps)
  3300         next assume as:"0 > m i" hence *:"max (m i * a $$ i) (m i * b $$ i) = m i * a $$ i"
  3301             "min (m i * a $$ i) (m i * b $$ i) = m i * b $$ i" using ab as i unfolding min_def max_def 
  3302             by(auto simp add:field_simps mult_le_cancel_left_neg intro: order_antisym)
  3303           show ?thesis unfolding * apply rule defer apply(rule_tac x="1 / m i * x$$i" in exI)
  3304             using as by(auto simp add:field_simps) qed qed qed qed qed 
  3305 
  3306 lemma interval_image_stretch_interval: "\<exists>u v. (\<lambda>x. \<chi>\<chi> k. m k * x$$k) ` {a..b::'a::ordered_euclidean_space} = {u..v::'a}"
  3307   unfolding image_stretch_interval by auto 
  3308 
  3309 lemma content_image_stretch_interval:
  3310   "content((\<lambda>x::'a::ordered_euclidean_space. (\<chi>\<chi> k. m k * x$$k)::'a) ` {a..b}) = abs(setprod m {..<DIM('a)}) * content({a..b})"
  3311 proof(cases "{a..b} = {}") case True thus ?thesis
  3312     unfolding content_def image_is_empty image_stretch_interval if_P[OF True] by auto
  3313 next case False hence "(\<lambda>x. (\<chi>\<chi> k. m k * x $$ k)::'a) ` {a..b} \<noteq> {}" by auto
  3314   thus ?thesis using False unfolding content_def image_stretch_interval apply- unfolding interval_bounds' if_not_P
  3315     unfolding abs_setprod setprod_timesf[THEN sym] apply(rule setprod_cong2) unfolding lessThan_iff euclidean_lambda_beta'
  3316   proof- fix i assume i:"i<DIM('a)" have "(m i < 0 \<or> m i > 0) \<or> m i = 0" by auto
  3317     thus "max (m i * a $$ i) (m i * b $$ i) - min (m i * a $$ i) (m i * b $$ i) = \<bar>m i\<bar> * (b $$ i - a $$ i)"
  3318       apply-apply(erule disjE)+ unfolding min_def max_def using False[unfolded interval_ne_empty,rule_format,of i] i 
  3319       by(auto simp add:field_simps not_le mult_le_cancel_left_neg mult_le_cancel_left_pos) qed qed
  3320 
  3321 lemma has_integral_stretch: fixes f::"'a::ordered_euclidean_space => 'b::real_normed_vector"
  3322   assumes "(f has_integral i) {a..b}" "\<forall>k<DIM('a). ~(m k = 0)"
  3323   shows "((\<lambda>x. f(\<chi>\<chi> k. m k * x$$k)) has_integral
  3324              ((1/(abs(setprod m {..<DIM('a)}))) *\<^sub>R i)) ((\<lambda>x. (\<chi>\<chi> k. 1/(m k) * x$$k)::'a) ` {a..b})"
  3325   apply(rule has_integral_twiddle[where f=f]) unfolding zero_less_abs_iff content_image_stretch_interval
  3326   unfolding image_stretch_interval empty_as_interval euclidean_eq[where 'a='a] using assms
  3327 proof- show "\<forall>y::'a. continuous (at y) (\<lambda>x. (\<chi>\<chi> k. m k * x $$ k)::'a)"
  3328    apply(rule,rule linear_continuous_at) unfolding linear_linear
  3329    unfolding linear_def euclidean_simps euclidean_eq[where 'a='a] by(auto simp add:field_simps) qed auto
  3330 
  3331 lemma integrable_stretch:  fixes f::"'a::ordered_euclidean_space => 'b::real_normed_vector"
  3332   assumes "f integrable_on {a..b}" "\<forall>k<DIM('a). ~(m k = 0)"
  3333   shows "(\<lambda>x::'a. f(\<chi>\<chi> k. m k * x$$k)) integrable_on ((\<lambda>x. \<chi>\<chi> k. 1/(m k) * x$$k) ` {a..b})"
  3334   using assms unfolding integrable_on_def apply-apply(erule exE) 
  3335   apply(drule has_integral_stretch,assumption) by auto
  3336 
  3337 subsection {* even more special cases. *}
  3338 
  3339 lemma uminus_interval_vector[simp]:"uminus ` {a..b} = {-b .. -a::'a::ordered_euclidean_space}"
  3340   apply(rule set_eqI,rule) defer unfolding image_iff
  3341   apply(rule_tac x="-x" in bexI) by(auto simp add:minus_le_iff le_minus_iff eucl_le[where 'a='a])
  3342 
  3343 lemma has_integral_reflect_lemma[intro]: assumes "(f has_integral i) {a..b}"
  3344   shows "((\<lambda>x. f(-x)) has_integral i) {-b .. -a}"
  3345   using has_integral_affinity[OF assms, of "-1" 0] by auto
  3346 
  3347 lemma has_integral_reflect[simp]: "((\<lambda>x. f(-x)) has_integral i) {-b..-a} \<longleftrightarrow> (f has_integral i) ({a..b})"
  3348   apply rule apply(drule_tac[!] has_integral_reflect_lemma) by auto
  3349 
  3350 lemma integrable_reflect[simp]: "(\<lambda>x. f(-x)) integrable_on {-b..-a} \<longleftrightarrow> f integrable_on {a..b}"
  3351   unfolding integrable_on_def by auto
  3352 
  3353 lemma integral_reflect[simp]: "integral {-b..-a} (\<lambda>x. f(-x)) = integral ({a..b}) f"
  3354   unfolding integral_def by auto
  3355 
  3356 subsection {* Stronger form of FCT; quite a tedious proof. *}
  3357 
  3358 lemma bgauge_existence_lemma: "(\<forall>x\<in>s. \<exists>d::real. 0 < d \<and> q d x) \<longleftrightarrow> (\<forall>x. \<exists>d>0. x\<in>s \<longrightarrow> q d x)" by(meson zero_less_one)
  3359 
  3360 lemma additive_tagged_division_1': fixes f::"real \<Rightarrow> 'a::real_normed_vector"
  3361   assumes "a \<le> b" "p tagged_division_of {a..b}"
  3362   shows "setsum (\<lambda>(x,k). f (interval_upperbound k) - f(interval_lowerbound k)) p = f b - f a"
  3363   using additive_tagged_division_1[OF _ assms(2), of f] using assms(1) by auto
  3364 
  3365 lemma split_minus[simp]:"(\<lambda>(x, k). f x k) x - (\<lambda>(x, k). g x k) x = (\<lambda>(x, k). f x k - g x k) x"
  3366   unfolding split_def by(rule refl)
  3367 
  3368 lemma norm_triangle_le_sub: "norm x + norm y \<le> e \<Longrightarrow> norm (x - y) \<le> e"
  3369   apply(subst(asm)(2) norm_minus_cancel[THEN sym])
  3370   apply(drule norm_triangle_le) by(auto simp add:algebra_simps)
  3371 
  3372 lemma fundamental_theorem_of_calculus_interior: fixes f::"real => 'a::real_normed_vector"
  3373   assumes"a \<le> b" "continuous_on {a..b} f" "\<forall>x\<in>{a<..<b}. (f has_vector_derivative f'(x)) (at x)"
  3374   shows "(f' has_integral (f b - f a)) {a..b}"
  3375 proof- { presume *:"a < b \<Longrightarrow> ?thesis" 
  3376     show ?thesis proof(cases,rule *,assumption)
  3377       assume "\<not> a < b" hence "a = b" using assms(1) by auto
  3378       hence *:"{a .. b} = {b}" "f b - f a = 0" by(auto simp add:  order_antisym)
  3379       show ?thesis unfolding *(2) apply(rule has_integral_null) unfolding content_eq_0 using * `a=b` by auto
  3380     qed } assume ab:"a < b"
  3381   let ?P = "\<lambda>e. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow>
  3382                    norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content {a..b})"
  3383   { presume "\<And>e. e>0 \<Longrightarrow> ?P e" thus ?thesis unfolding has_integral_factor_content by auto }
  3384   fix e::real assume e:"e>0"
  3385   note assms(3)[unfolded has_vector_derivative_def has_derivative_at_alt ball_conj_distrib]
  3386   note conjunctD2[OF this] note bounded=this(1) and this(2)
  3387   from this(2) have "\<forall>x\<in>{a<..<b}. \<exists>d>0. \<forall>y. norm (y - x) < d \<longrightarrow> norm (f y - f x - (y - x) *\<^sub>R f' x) \<le> e/2 * norm (y - x)"
  3388     apply-apply safe apply(erule_tac x=x in ballE,erule_tac x="e/2" in allE) using e by auto note this[unfolded bgauge_existence_lemma]
  3389   from choice[OF this] guess d .. note conjunctD2[OF this[rule_format]] note d = this[rule_format]
  3390   have "bounded (f ` {a..b})" apply(rule compact_imp_bounded compact_continuous_image)+ using compact_interval assms by auto
  3391   from this[unfolded bounded_pos] guess B .. note B = this[rule_format]
  3392 
  3393   have "\<exists>da. 0 < da \<and> (\<forall>c. a \<le> c \<and> {a..c} \<subseteq> {a..b} \<and> {a..c} \<subseteq> ball a da
  3394     \<longrightarrow> norm(content {a..c} *\<^sub>R f' a - (f c - f a)) \<le> (e * (b - a)) / 4)"
  3395   proof- have "a\<in>{a..b}" using ab by auto
  3396     note assms(2)[unfolded continuous_on_eq_continuous_within,rule_format,OF this]
  3397     note * = this[unfolded continuous_within Lim_within,rule_format] have "(e * (b - a)) / 8 > 0" using e ab by(auto simp add:field_simps)
  3398     from *[OF this] guess k .. note k = conjunctD2[OF this,rule_format]
  3399     have "\<exists>l. 0 < l \<and> norm(l *\<^sub>R f' a) \<le> (e * (b - a)) / 8"
  3400     proof(cases "f' a = 0") case True
  3401       thus ?thesis apply(rule_tac x=1 in exI) using ab e by(auto intro!:mult_nonneg_nonneg) 
  3402     next case False thus ?thesis
  3403         apply(rule_tac x="(e * (b - a)) / 8 / norm (f' a)" in exI) using ab e by(auto simp add:field_simps) 
  3404     qed then guess l .. note l = conjunctD2[OF this]
  3405     show ?thesis apply(rule_tac x="min k l" in exI) apply safe unfolding min_less_iff_conj apply(rule,(rule l k)+)
  3406     proof- fix c assume as:"a \<le> c" "{a..c} \<subseteq> {a..b}" "{a..c} \<subseteq> ball a (min k l)" 
  3407       note as' = this[unfolded subset_eq Ball_def mem_ball dist_real_def mem_interval]
  3408       have "norm ((c - a) *\<^sub>R f' a - (f c - f a)) \<le> norm ((c - a) *\<^sub>R f' a) + norm (f c - f a)" by(rule norm_triangle_ineq4)
  3409       also have "... \<le> e * (b - a) / 8 + e * (b - a) / 8" 
  3410       proof(rule add_mono) case goal1 have "\<bar>c - a\<bar> \<le> \<bar>l\<bar>" using as' by auto
  3411         thus ?case apply-apply(rule order_trans[OF _ l(2)]) unfolding norm_scaleR apply(rule mult_right_mono) by auto
  3412       next case goal2 show ?case apply(rule less_imp_le) apply(cases "a = c") defer
  3413           apply(rule k(2)[unfolded dist_norm]) using as' e ab by(auto simp add:field_simps)
  3414       qed finally show "norm (content {a..c} *\<^sub>R f' a - (f c - f a)) \<le> e * (b - a) / 4"
  3415         unfolding content_real[OF as(1)] by auto
  3416     qed qed then guess da .. note da=conjunctD2[OF this,rule_format]
  3417 
  3418   have "\<exists>db>0. \<forall>c\<le>b. {c..b} \<subseteq> {a..b} \<and> {c..b} \<subseteq> ball b db \<longrightarrow>
  3419     norm(content {c..b} *\<^sub>R f' b - (f b - f c)) \<le> (e * (b - a)) / 4"
  3420   proof- have "b\<in>{a..b}" using ab by auto
  3421     note assms(2)[unfolded continuous_on_eq_continuous_within,rule_format,OF this]
  3422     note * = this[unfolded continuous_within Lim_within,rule_format] have "(e * (b - a)) / 8 > 0"
  3423       using e ab by(auto simp add:field_simps)
  3424     from *[OF this] guess k .. note k = conjunctD2[OF this,rule_format]
  3425     have "\<exists>l. 0 < l \<and> norm(l *\<^sub>R f' b) \<le> (e * (b - a)) / 8"
  3426     proof(cases "f' b = 0") case True
  3427       thus ?thesis apply(rule_tac x=1 in exI) using ab e by(auto intro!:mult_nonneg_nonneg) 
  3428     next case False thus ?thesis 
  3429         apply(rule_tac x="(e * (b - a)) / 8 / norm (f' b)" in exI)
  3430         using ab e by(auto simp add:field_simps)
  3431     qed then guess l .. note l = conjunctD2[OF this]
  3432     show ?thesis apply(rule_tac x="min k l" in exI) apply safe unfolding min_less_iff_conj apply(rule,(rule l k)+)
  3433     proof- fix c assume as:"c \<le> b" "{c..b} \<subseteq> {a..b}" "{c..b} \<subseteq> ball b (min k l)" 
  3434       note as' = this[unfolded subset_eq Ball_def mem_ball dist_real_def mem_interval]
  3435       have "norm ((b - c) *\<^sub>R f' b - (f b - f c)) \<le> norm ((b - c) *\<^sub>R f' b) + norm (f b - f c)" by(rule norm_triangle_ineq4)
  3436       also have "... \<le> e * (b - a) / 8 + e * (b - a) / 8" 
  3437       proof(rule add_mono) case goal1 have "\<bar>c - b\<bar> \<le> \<bar>l\<bar>" using as' by auto
  3438         thus ?case apply-apply(rule order_trans[OF _ l(2)]) unfolding norm_scaleR apply(rule mult_right_mono) by auto
  3439       next case goal2 show ?case apply(rule less_imp_le) apply(cases "b = c") defer apply(subst norm_minus_commute)
  3440           apply(rule k(2)[unfolded dist_norm]) using as' e ab by(auto simp add:field_simps)
  3441       qed finally show "norm (content {c..b} *\<^sub>R f' b - (f b - f c)) \<le> e * (b - a) / 4"
  3442         unfolding content_real[OF as(1)] by auto
  3443     qed qed then guess db .. note db=conjunctD2[OF this,rule_format]
  3444 
  3445   let ?d = "(\<lambda>x. ball x (if x=a then da else if x=b then db else d x))"
  3446   show "?P e" apply(rule_tac x="?d" in exI)
  3447   proof safe case goal1 show ?case apply(rule gauge_ball_dependent) using ab db(1) da(1) d(1) by auto
  3448   next case goal2 note as=this let ?A = "{t. fst t \<in> {a, b}}" note p = tagged_division_ofD[OF goal2(1)]
  3449     have pA:"p = (p \<inter> ?A) \<union> (p - ?A)" "finite (p \<inter> ?A)" "finite (p - ?A)" "(p \<inter> ?A) \<inter> (p - ?A) = {}"  using goal2 by auto
  3450     note * = additive_tagged_division_1'[OF assms(1) goal2(1), THEN sym]
  3451     have **:"\<And>n1 s1 n2 s2::real. n2 \<le> s2 / 2 \<Longrightarrow> n1 - s1 \<le> s2 / 2 \<Longrightarrow> n1 + n2 \<le> s1 + s2" by arith
  3452     show ?case unfolding content_real[OF assms(1)] and *[of "\<lambda>x. x"] *[of f] setsum_subtractf[THEN sym] split_minus
  3453       unfolding setsum_right_distrib apply(subst(2) pA,subst pA) unfolding setsum_Un_disjoint[OF pA(2-)]
  3454     proof(rule norm_triangle_le,rule **) 
  3455       case goal1 show ?case apply(rule order_trans,rule setsum_norm_le) apply(rule pA) defer apply(subst divide.setsum)
  3456       proof(rule order_refl,safe,unfold not_le o_def split_conv fst_conv,rule ccontr) fix x k assume as:"(x,k) \<in> p"
  3457           "e * (interval_upperbound k -  interval_lowerbound k) / 2
  3458           < norm (content k *\<^sub>R f' x - (f (interval_upperbound k) - f (interval_lowerbound k)))"
  3459         from p(4)[OF this(1)] guess u v apply-by(erule exE)+ note k=this
  3460         hence "u \<le> v" and uv:"{u,v}\<subseteq>{u..v}" using p(2)[OF as(1)] by auto
  3461         note result = as(2)[unfolded k interval_bounds_real[OF this(1)] content_real[OF this(1)]]
  3462 
  3463         assume as':"x \<noteq> a" "x \<noteq> b" hence "x \<in> {a<..<b}" using p(2-3)[OF as(1)] by auto
  3464         note  * = d(2)[OF this]
  3465         have "norm ((v - u) *\<^sub>R f' (x) - (f (v) - f (u))) =
  3466           norm ((f (u) - f (x) - (u - x) *\<^sub>R f' (x)) - (f (v) - f (x) - (v - x) *\<^sub>R f' (x)))" 
  3467           apply(rule arg_cong[of _ _ norm]) unfolding scaleR_left.diff by auto 
  3468         also have "... \<le> e / 2 * norm (u - x) + e / 2 * norm (v - x)" apply(rule norm_triangle_le_sub)
  3469           apply(rule add_mono) apply(rule_tac[!] *) using fineD[OF goal2(2) as(1)] as' unfolding k subset_eq
  3470           apply- apply(erule_tac x=u in ballE,erule_tac[3] x=v in ballE) using uv by(auto simp:dist_real_def)
  3471         also have "... \<le> e / 2 * norm (v - u)" using p(2)[OF as(1)] unfolding k by(auto simp add:field_simps)
  3472         finally have "e * (v - u) / 2 < e * (v - u) / 2"
  3473           apply- apply(rule less_le_trans[OF result]) using uv by auto thus False by auto qed
  3474 
  3475     next have *:"\<And>x s1 s2::real. 0 \<le> s1 \<Longrightarrow> x \<le> (s1 + s2) / 2 \<Longrightarrow> x - s1 \<le> s2 / 2" by auto
  3476       case goal2 show ?case apply(rule *) apply(rule setsum_nonneg) apply(rule,unfold split_paired_all split_conv)
  3477         defer unfolding setsum_Un_disjoint[OF pA(2-),THEN sym] pA(1)[THEN sym] unfolding setsum_right_distrib[THEN sym] 
  3478         apply(subst additive_tagged_division_1[OF _ as(1)]) apply(rule assms)
  3479       proof- fix x k assume "(x,k) \<in> p \<inter> {t. fst t \<in> {a, b}}" note xk=IntD1[OF this]
  3480         from p(4)[OF this] guess u v apply-by(erule exE)+ note uv=this
  3481         with p(2)[OF xk] have "{u..v} \<noteq> {}" by auto
  3482         thus "0 \<le> e * ((interval_upperbound k) - (interval_lowerbound k))"
  3483           unfolding uv using e by(auto simp add:field_simps)
  3484       next have *:"\<And>s f t e. setsum f s = setsum f t \<Longrightarrow> norm(setsum f t) \<le> e \<Longrightarrow> norm(setsum f s) \<le> e" by auto
  3485         show "norm (\<Sum>(x, k)\<in>p \<inter> ?A. content k *\<^sub>R f' x -
  3486           (f ((interval_upperbound k)) - f ((interval_lowerbound k)))) \<le> e * (b - a) / 2" 
  3487           apply(rule *[where t="p \<inter> {t. fst t \<in> {a, b} \<and> content(snd t) \<noteq> 0}"])
  3488           apply(rule setsum_mono_zero_right[OF pA(2)]) defer apply(rule) unfolding split_paired_all split_conv o_def
  3489         proof- fix x k assume "(x,k) \<in> p \<inter> {t. fst t \<in> {a, b}} - p \<inter> {t. fst t \<in> {a, b} \<and> content (snd t) \<noteq> 0}"
  3490           hence xk:"(x,k)\<in>p" "content k = 0" by auto from p(4)[OF xk(1)] guess u v apply-by(erule exE)+ note uv=this
  3491           have "k\<noteq>{}" using p(2)[OF xk(1)] by auto hence *:"u = v" using xk
  3492             unfolding uv content_eq_0 interval_eq_empty by auto
  3493           thus "content k *\<^sub>R (f' (x)) - (f ((interval_upperbound k)) - f ((interval_lowerbound k))) = 0" using xk unfolding uv by auto
  3494         next have *:"p \<inter> {t. fst t \<in> {a, b} \<and> content(snd t) \<noteq> 0} = 
  3495             {t. t\<in>p \<and> fst t = a \<and> content(snd t) \<noteq> 0} \<union> {t. t\<in>p \<and> fst t = b \<and> content(snd t) \<noteq> 0}" by blast
  3496           have **:"\<And>s f. \<And>e::real. (\<forall>x y. x \<in> s \<and> y \<in> s \<longrightarrow> x = y) \<Longrightarrow> (\<forall>x. x \<in> s \<longrightarrow> norm(f x) \<le> e)
  3497             \<Longrightarrow> e>0 \<Longrightarrow> norm(setsum f s) \<le> e"
  3498           proof(case_tac "s={}") case goal2 then obtain x where "x\<in>s" by auto hence *:"s = {x}" using goal2(1) by auto
  3499             thus ?case using `x\<in>s` goal2(2) by auto
  3500           qed auto
  3501           case goal2 show ?case apply(subst *, subst setsum_Un_disjoint) prefer 4
  3502             apply(rule order_trans[of _ "e * (b - a)/4 + e * (b - a)/4"]) 
  3503             apply(rule norm_triangle_le,rule add_mono) apply(rule_tac[1-2] **)
  3504           proof- let ?B = "\<lambda>x. {t \<in> p. fst t = x \<and> content (snd t) \<noteq> 0}"
  3505             have pa:"\<And>k. (a, k) \<in> p \<Longrightarrow> \<exists>v. k = {a .. v} \<and> a \<le> v" 
  3506             proof- case goal1 guess u v using p(4)[OF goal1] apply-by(erule exE)+ note uv=this
  3507               have *:"u \<le> v" using p(2)[OF goal1] unfolding uv by auto
  3508               have u:"u = a" proof(rule ccontr)  have "u \<in> {u..v}" using p(2-3)[OF goal1(1)] unfolding uv by auto 
  3509                 have "u \<ge> a" using p(2-3)[OF goal1(1)] unfolding uv subset_eq by auto moreover assume "u\<noteq>a" ultimately
  3510                 have "u > a" by auto
  3511                 thus False using p(2)[OF goal1(1)] unfolding uv by(auto simp add:)
  3512               qed thus ?case apply(rule_tac x=v in exI) unfolding uv using * by auto
  3513             qed
  3514             have pb:"\<And>k. (b, k) \<in> p \<Longrightarrow> \<exists>v. k = {v .. b} \<and> b \<ge> v" 
  3515             proof- case goal1 guess u v using p(4)[OF goal1] apply-by(erule exE)+ note uv=this
  3516               have *:"u \<le> v" using p(2)[OF goal1] unfolding uv by auto
  3517               have u:"v =  b" proof(rule ccontr)  have "u \<in> {u..v}" using p(2-3)[OF goal1(1)] unfolding uv by auto 
  3518                 have "v \<le>  b" using p(2-3)[OF goal1(1)] unfolding uv subset_eq by auto moreover assume "v\<noteq> b" ultimately
  3519                 have "v <  b" by auto
  3520                 thus False using p(2)[OF goal1(1)] unfolding uv by(auto simp add:)
  3521               qed thus ?case apply(rule_tac x=u in exI) unfolding uv using * by auto
  3522             qed
  3523 
  3524             show "\<forall>x y. x \<in> ?B a \<and> y \<in> ?B a \<longrightarrow> x = y" apply(rule,rule,rule,unfold split_paired_all)
  3525               unfolding mem_Collect_eq fst_conv snd_conv apply safe
  3526             proof- fix x k k' assume k:"( a, k) \<in> p" "( a, k') \<in> p" "content k \<noteq> 0" "content k' \<noteq> 0"
  3527               guess v using pa[OF k(1)] .. note v = conjunctD2[OF this]
  3528               guess v' using pa[OF k(2)] .. note v' = conjunctD2[OF this] let ?v = " (min (v) (v'))"
  3529               have "{ a <..< ?v} \<subseteq> k \<inter> k'" unfolding v v' by(auto simp add:) note subset_interior[OF this,unfolded interior_inter]
  3530               moreover have " ((a + ?v)/2) \<in> { a <..< ?v}" using k(3-)
  3531                 unfolding v v' content_eq_0 not_le by(auto simp add:not_le)
  3532               ultimately have " ((a + ?v)/2) \<in> interior k \<inter> interior k'" unfolding interior_open[OF open_interval] by auto
  3533               hence *:"k = k'" apply- apply(rule ccontr) using p(5)[OF k(1-2)] by auto
  3534               { assume "x\<in>k" thus "x\<in>k'" unfolding * . } { assume "x\<in>k'" thus "x\<in>k" unfolding * . }
  3535             qed 
  3536             show "\<forall>x y. x \<in> ?B b \<and> y \<in> ?B b \<longrightarrow> x = y" apply(rule,rule,rule,unfold split_paired_all)
  3537               unfolding mem_Collect_eq fst_conv snd_conv apply safe
  3538             proof- fix x k k' assume k:"( b, k) \<in> p" "( b, k') \<in> p" "content k \<noteq> 0" "content k' \<noteq> 0"
  3539               guess v using pb[OF k(1)] .. note v = conjunctD2[OF this]
  3540               guess v' using pb[OF k(2)] .. note v' = conjunctD2[OF this] let ?v = " (max (v) (v'))"
  3541               have "{?v <..<  b} \<subseteq> k \<inter> k'" unfolding v v' by(auto simp add:) note subset_interior[OF this,unfolded interior_inter]
  3542               moreover have " ((b + ?v)/2) \<in> {?v <..<  b}" using k(3-) unfolding v v' content_eq_0 not_le by auto
  3543               ultimately have " ((b + ?v)/2) \<in> interior k \<inter> interior k'" unfolding interior_open[OF open_interval] by auto
  3544               hence *:"k = k'" apply- apply(rule ccontr) using p(5)[OF k(1-2)] by auto
  3545               { assume "x\<in>k" thus "x\<in>k'" unfolding * . } { assume "x\<in>k'" thus "x\<in>k" unfolding * . }
  3546             qed
  3547 
  3548             let ?a = a and ?b = b (* a is something else while proofing the next theorem. *)
  3549             show "\<forall>x. x \<in> ?B a \<longrightarrow> norm ((\<lambda>(x, k). content k *\<^sub>R f' (x) - (f ((interval_upperbound k)) -
  3550               f ((interval_lowerbound k)))) x) \<le> e * (b - a) / 4" apply(rule,rule) unfolding mem_Collect_eq
  3551               unfolding split_paired_all fst_conv snd_conv 
  3552             proof safe case goal1 guess v using pa[OF goal1(1)] .. note v = conjunctD2[OF this]
  3553               have " ?a\<in>{ ?a..v}" using v(2) by auto hence "v \<le> ?b" using p(3)[OF goal1(1)] unfolding subset_eq v by auto
  3554               moreover have "{?a..v} \<subseteq> ball ?a da" using fineD[OF as(2) goal1(1)]
  3555                 apply-apply(subst(asm) if_P,rule refl) unfolding subset_eq apply safe apply(erule_tac x=" x" in ballE)
  3556                 by(auto simp add:subset_eq dist_real_def v) ultimately
  3557               show ?case unfolding v interval_bounds_real[OF v(2)] apply- apply(rule da(2)[of "v"])
  3558                 using goal1 fineD[OF as(2) goal1(1)] unfolding v content_eq_0 by auto
  3559             qed
  3560             show "\<forall>x. x \<in> ?B b \<longrightarrow> norm ((\<lambda>(x, k). content k *\<^sub>R f' (x) -
  3561               (f ((interval_upperbound k)) - f ((interval_lowerbound k)))) x) \<le> e * (b - a) / 4"
  3562               apply(rule,rule) unfolding mem_Collect_eq unfolding split_paired_all fst_conv snd_conv 
  3563             proof safe case goal1 guess v using pb[OF goal1(1)] .. note v = conjunctD2[OF this]
  3564               have " ?b\<in>{v.. ?b}" using v(2) by auto hence "v \<ge> ?a" using p(3)[OF goal1(1)]
  3565                 unfolding subset_eq v by auto
  3566               moreover have "{v..?b} \<subseteq> ball ?b db" using fineD[OF as(2) goal1(1)]
  3567                 apply-apply(subst(asm) if_P,rule refl) unfolding subset_eq apply safe
  3568                 apply(erule_tac x=" x" in ballE) using ab
  3569                 by(auto simp add:subset_eq v dist_real_def) ultimately
  3570               show ?case unfolding v unfolding interval_bounds_real[OF v(2)] apply- apply(rule db(2)[of "v"])
  3571                 using goal1 fineD[OF as(2) goal1(1)] unfolding v content_eq_0 by auto
  3572             qed
  3573           qed(insert p(1) ab e, auto simp add:field_simps) qed auto qed qed qed qed
  3574 
  3575 subsection {* Stronger form with finite number of exceptional points. *}
  3576 
  3577 lemma fundamental_theorem_of_calculus_interior_strong: fixes f::"real \<Rightarrow> 'a::banach"
  3578   assumes"finite s" "a \<le> b" "continuous_on {a..b} f"
  3579   "\<forall>x\<in>{a<..<b} - s. (f has_vector_derivative f'(x)) (at x)"
  3580   shows "(f' has_integral (f b - f a)) {a..b}" using assms apply- 
  3581 proof(induct "card s" arbitrary:s a b)
  3582   case 0 show ?case apply(rule fundamental_theorem_of_calculus_interior) using 0 by auto
  3583 next case (Suc n) from this(2) guess c s' apply-apply(subst(asm) eq_commute) unfolding card_Suc_eq
  3584     apply(subst(asm)(2) eq_commute) by(erule exE conjE)+ note cs = this[rule_format]
  3585   show ?case proof(cases "c\<in>{a<..<b}")
  3586     case False thus ?thesis apply- apply(rule Suc(1)[OF cs(3) _ Suc(4,5)]) apply safe defer
  3587       apply(rule Suc(6)[rule_format]) using Suc(3) unfolding cs by auto
  3588   next have *:"f b - f a = (f c - f a) + (f b - f c)" by auto
  3589     case True hence "a \<le> c" "c \<le> b" by auto
  3590     thus ?thesis apply(subst *) apply(rule has_integral_combine) apply assumption+
  3591       apply(rule_tac[!] Suc(1)[OF cs(3)]) using Suc(3) unfolding cs
  3592     proof- show "continuous_on {a..c} f" "continuous_on {c..b} f"
  3593         apply(rule_tac[!] continuous_on_subset[OF Suc(5)]) using True by auto
  3594       let ?P = "\<lambda>i j. \<forall>x\<in>{i<..<j} - s'. (f has_vector_derivative f' x) (at x)"
  3595       show "?P a c" "?P c b" apply safe apply(rule_tac[!] Suc(6)[rule_format]) using True unfolding cs by auto
  3596     qed auto qed qed
  3597 
  3598 lemma fundamental_theorem_of_calculus_strong: fixes f::"real \<Rightarrow> 'a::banach"
  3599   assumes "finite s" "a \<le> b" "continuous_on {a..b} f"
  3600   "\<forall>x\<in>{a..b} - s. (f has_vector_derivative f'(x)) (at x)"
  3601   shows "(f' has_integral (f(b) - f(a))) {a..b}"
  3602   apply(rule fundamental_theorem_of_calculus_interior_strong[OF assms(1-3), of f'])
  3603   using assms(4) by auto
  3604 
  3605 lemma indefinite_integral_continuous_left: fixes f::"real \<Rightarrow> 'a::banach"
  3606   assumes "f integrable_on {a..b}" "a < c" "c \<le> b" "0 < e"
  3607   obtains d where "0 < d" "\<forall>t. c - d < t \<and> t \<le> c \<longrightarrow> norm(integral {a..c} f - integral {a..t} f) < e"
  3608 proof- have "\<exists>w>0. \<forall>t. c - w < t \<and> t < c \<longrightarrow> norm(f c) * norm(c - t) < e / 3"
  3609   proof(cases "f c = 0") case False hence "0 < e / 3 / norm (f c)"
  3610       apply-apply(rule divide_pos_pos) using `e>0` by auto
  3611     thus ?thesis apply-apply(rule,rule,assumption,safe)
  3612     proof- fix t assume as:"t < c" and "c - e / 3 / norm (f c) < t"
  3613       hence "c - t < e / 3 / norm (f c)" by auto
  3614       hence "norm (c - t) < e / 3 / norm (f c)" using as by auto
  3615       thus "norm (f c) * norm (c - t) < e / 3" using False apply-
  3616         apply(subst mult_commute) apply(subst pos_less_divide_eq[THEN sym]) by auto
  3617     qed next case True show ?thesis apply(rule_tac x=1 in exI) unfolding True using `e>0` by auto
  3618   qed then guess w .. note w = conjunctD2[OF this,rule_format]
  3619   
  3620   have *:"e / 3 > 0" using assms by auto
  3621   have "f integrable_on {a..c}" apply(rule integrable_subinterval[OF assms(1)]) using assms(2-3) by auto
  3622   from integrable_integral[OF this,unfolded has_integral,rule_format,OF *] guess d1 ..
  3623   note d1 = conjunctD2[OF this,rule_format] def d \<equiv> "\<lambda>x. ball x w \<inter> d1 x"
  3624   have "gauge d" unfolding d_def using w(1) d1 by auto
  3625   note this[unfolded gauge_def,rule_format,of c] note conjunctD2[OF this]
  3626   from this(2)[unfolded open_contains_ball,rule_format,OF this(1)] guess k .. note k=conjunctD2[OF this]
  3627 
  3628   let ?d = "min k (c - a)/2" show ?thesis apply(rule that[of ?d])
  3629   proof safe show "?d > 0" using k(1) using assms(2) by auto
  3630     fix t assume as:"c - ?d < t" "t \<le> c" let ?thesis = "norm (integral {a..c} f - integral {a..t} f) < e"
  3631     { presume *:"t < c \<Longrightarrow> ?thesis"
  3632       show ?thesis apply(cases "t = c") defer apply(rule *)
  3633         apply(subst less_le) using `e>0` as(2) by auto } assume "t < c"
  3634 
  3635     have "f integrable_on {a..t}" apply(rule integrable_subinterval[OF assms(1)]) using assms(2-3) as(2) by auto
  3636     from integrable_integral[OF this,unfolded has_integral,rule_format,OF *] guess d2 ..
  3637     note d2 = conjunctD2[OF this,rule_format]
  3638     def d3 \<equiv> "\<lambda>x. if x \<le> t then d1 x \<inter> d2 x else d1 x"
  3639     have "gauge d3" using d2(1) d1(1) unfolding d3_def gauge_def by auto
  3640     from fine_division_exists[OF this, of a t] guess p . note p=this
  3641     note p'=tagged_division_ofD[OF this(1)]
  3642     have pt:"\<forall>(x,k)\<in>p. x \<le> t" proof safe case goal1 from p'(2,3)[OF this] show ?case by auto qed
  3643     with p(2) have "d2 fine p" unfolding fine_def d3_def apply safe apply(erule_tac x="(a,b)" in ballE)+ by auto
  3644     note d2_fin = d2(2)[OF conjI[OF p(1) this]]
  3645     
  3646     have *:"{a..c} \<inter> {x. x $$0 \<le> t} = {a..t}" "{a..c} \<inter> {x. x$$0 \<ge> t} = {t..c}"
  3647       using assms(2-3) as by(auto simp add:field_simps)
  3648     have "p \<union> {(c, {t..c})} tagged_division_of {a..c} \<and> d1 fine p \<union> {(c, {t..c})}" apply rule
  3649       apply(rule tagged_division_union_interval[of _ _ _ 0 "t"]) unfolding * apply(rule p)
  3650       apply(rule tagged_division_of_self) unfolding fine_def
  3651     proof safe fix x k y assume "(x,k)\<in>p" "y\<in>k" thus "y\<in>d1 x"
  3652         using p(2) pt unfolding fine_def d3_def apply- apply(erule_tac x="(x,k)" in ballE)+ by auto
  3653     next fix x assume "x\<in>{t..c}" hence "dist c x < k" unfolding dist_real_def
  3654         using as(1) by(auto simp add:field_simps) 
  3655       thus "x \<in> d1 c" using k(2) unfolding d_def by auto
  3656     qed(insert as(2), auto) note d1_fin = d1(2)[OF this]
  3657 
  3658     have *:"integral{a..c} f - integral {a..t} f = -(((c - t) *\<^sub>R f c + (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)) -
  3659         integral {a..c} f) + ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - integral {a..t} f) + (c - t) *\<^sub>R f c" 
  3660       "e = (e/3 + e/3) + e/3" by auto
  3661     have **:"(\<Sum>(x, k)\<in>p \<union> {(c, {t..c})}. content k *\<^sub>R f x) = (c - t) *\<^sub>R f c + (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)"
  3662     proof- have **:"\<And>x F. F \<union> {x} = insert x F" by auto
  3663       have "(c, {t..c}) \<notin> p" proof safe case goal1 from p'(2-3)[OF this]
  3664         have "c \<in> {a..t}" by auto thus False using `t<c` by auto
  3665       qed thus ?thesis unfolding ** apply- apply(subst setsum_insert) apply(rule p')
  3666         unfolding split_conv defer apply(subst content_real) using as(2) by auto qed 
  3667 
  3668     have ***:"c - w < t \<and> t < c"
  3669     proof- have "c - k < t" using `k>0` as(1) by(auto simp add:field_simps)
  3670       moreover have "k \<le> w" apply(rule ccontr) using k(2) 
  3671         unfolding subset_eq apply(erule_tac x="c + ((k + w)/2)" in ballE)
  3672         unfolding d_def using `k>0` `w>0` by(auto simp add:field_simps not_le not_less dist_real_def)
  3673       ultimately show  ?thesis using `t<c` by(auto simp add:field_simps) qed
  3674 
  3675     show ?thesis unfolding *(1) apply(subst *(2)) apply(rule norm_triangle_lt add_strict_mono)+
  3676       unfolding norm_minus_cancel apply(rule d1_fin[unfolded **]) apply(rule d2_fin)
  3677       using w(2)[OF ***] unfolding norm_scaleR by(auto simp add:field_simps) qed qed 
  3678 
  3679 lemma indefinite_integral_continuous_right: fixes f::"real \<Rightarrow> 'a::banach"
  3680   assumes "f integrable_on {a..b}" "a \<le> c" "c < b" "0 < e"
  3681   obtains d where "0 < d" "\<forall>t. c \<le> t \<and> t < c + d \<longrightarrow> norm(integral{a..c} f - integral{a..t} f) < e"
  3682 proof- have *:"(\<lambda>x. f (- x)) integrable_on {- b..- a}" "- b < - c" "- c \<le> - a" using assms by auto
  3683   from indefinite_integral_continuous_left[OF * `e>0`] guess d . note d = this let ?d = "min d (b - c)"
  3684   show ?thesis apply(rule that[of "?d"])
  3685   proof safe show "0 < ?d" using d(1) assms(3) by auto
  3686     fix t::"real" assume as:"c \<le> t" "t < c + ?d"
  3687     have *:"integral{a..c} f = integral{a..b} f - integral{c..b} f"
  3688       "integral{a..t} f = integral{a..b} f - integral{t..b} f" unfolding algebra_simps
  3689       apply(rule_tac[!] integral_combine) using assms as by auto
  3690     have "(- c) - d < (- t) \<and> - t \<le> - c" using as by auto note d(2)[rule_format,OF this]
  3691     thus "norm (integral {a..c} f - integral {a..t} f) < e" unfolding * 
  3692       unfolding integral_reflect apply-apply(subst norm_minus_commute) by(auto simp add:algebra_simps) qed qed
  3693    
  3694 lemma indefinite_integral_continuous: fixes f::"real \<Rightarrow> 'a::banach"
  3695   assumes "f integrable_on {a..b}" shows  "continuous_on {a..b} (\<lambda>x. integral {a..x} f)"
  3696 proof(unfold continuous_on_iff, safe)  fix x e assume as:"x\<in>{a..b}" "0<(e::real)"
  3697   let ?thesis = "\<exists>d>0. \<forall>x'\<in>{a..b}. dist x' x < d \<longrightarrow> dist (integral {a..x'} f) (integral {a..x} f) < e"
  3698   { presume *:"a<b \<Longrightarrow> ?thesis"
  3699     show ?thesis apply(cases,rule *,assumption)
  3700     proof- case goal1 hence "{a..b} = {x}" using as(1) apply-apply(rule set_eqI)
  3701         unfolding atLeastAtMost_iff by(auto simp only:field_simps not_less DIM_real)
  3702       thus ?case using `e>0` by auto
  3703     qed } assume "a<b"
  3704   have "(x=a \<or> x=b) \<or> (a<x \<and> x<b)" using as(1) by (auto simp add:)
  3705   thus ?thesis apply-apply(erule disjE)+
  3706   proof- assume "x=a" have "a \<le> a" by auto
  3707     from indefinite_integral_continuous_right[OF assms(1) this `a<b` `e>0`] guess d . note d=this
  3708     show ?thesis apply(rule,rule,rule d,safe) apply(subst dist_commute)
  3709       unfolding `x=a` dist_norm apply(rule d(2)[rule_format]) by auto
  3710   next   assume "x=b" have "b \<le> b" by auto
  3711     from indefinite_integral_continuous_left[OF assms(1) `a<b` this `e>0`] guess d . note d=this
  3712     show ?thesis apply(rule,rule,rule d,safe) apply(subst dist_commute)
  3713       unfolding `x=b` dist_norm apply(rule d(2)[rule_format])  by auto
  3714   next assume "a<x \<and> x<b" hence xl:"a<x" "x\<le>b" and xr:"a\<le>x" "x<b" by(auto simp add: )
  3715     from indefinite_integral_continuous_left [OF assms(1) xl `e>0`] guess d1 . note d1=this
  3716     from indefinite_integral_continuous_right[OF assms(1) xr `e>0`] guess d2 . note d2=this
  3717     show ?thesis apply(rule_tac x="min d1 d2" in exI)
  3718     proof safe show "0 < min d1 d2" using d1 d2 by auto
  3719       fix y assume "y\<in>{a..b}" "dist y x < min d1 d2"
  3720       thus "dist (integral {a..y} f) (integral {a..x} f) < e" apply-apply(subst dist_commute)
  3721         apply(cases "y < x") unfolding dist_norm apply(rule d1(2)[rule_format]) defer
  3722         apply(rule d2(2)[rule_format]) unfolding not_less by(auto simp add:field_simps)
  3723     qed qed qed 
  3724 
  3725 subsection {* This doesn't directly involve integration, but that gives an easy proof. *}
  3726 
  3727 lemma has_derivative_zero_unique_strong_interval: fixes f::"real \<Rightarrow> 'a::banach"
  3728   assumes "finite k" "continuous_on {a..b} f" "f a = y"
  3729   "\<forall>x\<in>({a..b} - k). (f has_derivative (\<lambda>h. 0)) (at x within {a..b})" "x \<in> {a..b}"
  3730   shows "f x = y"
  3731 proof- have ab:"a\<le>b" using assms by auto
  3732   have *:"a\<le>x" using assms(5) by auto
  3733   have "((\<lambda>x. 0\<Colon>'a) has_integral f x - f a) {a..x}"
  3734     apply(rule fundamental_theorem_of_calculus_interior_strong[OF assms(1) *])
  3735     apply(rule continuous_on_subset[OF assms(2)]) defer
  3736     apply safe unfolding has_vector_derivative_def apply(subst has_derivative_within_open[THEN sym])
  3737     apply assumption apply(rule open_interval) apply(rule has_derivative_within_subset[where s="{a..b}"])
  3738     using assms(4) assms(5) by auto note this[unfolded *]
  3739   note has_integral_unique[OF has_integral_0 this]
  3740   thus ?thesis unfolding assms by auto qed
  3741 
  3742 subsection {* Generalize a bit to any convex set. *}
  3743 
  3744 lemma has_derivative_zero_unique_strong_convex: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  3745   assumes "convex s" "finite k" "continuous_on s f" "c \<in> s" "f c = y"
  3746   "\<forall>x\<in>(s - k). (f has_derivative (\<lambda>h. 0)) (at x within s)" "x \<in> s"
  3747   shows "f x = y"
  3748 proof- { presume *:"x \<noteq> c \<Longrightarrow> ?thesis" show ?thesis apply(cases,rule *,assumption)
  3749       unfolding assms(5)[THEN sym] by auto } assume "x\<noteq>c"
  3750   note conv = assms(1)[unfolded convex_alt,rule_format]
  3751   have as1:"continuous_on {0..1} (f \<circ> (\<lambda>t. (1 - t) *\<^sub>R c + t *\<^sub>R x))"
  3752     apply(rule continuous_on_intros)+ apply(rule continuous_on_subset[OF assms(3)])
  3753     apply safe apply(rule conv) using assms(4,7) by auto
  3754   have *:"\<And>t xa. (1 - t) *\<^sub>R c + t *\<^sub>R x = (1 - xa) *\<^sub>R c + xa *\<^sub>R x \<Longrightarrow> t = xa"
  3755   proof- case goal1 hence "(t - xa) *\<^sub>R x = (t - xa) *\<^sub>R c" 
  3756       unfolding scaleR_simps by(auto simp add:algebra_simps)
  3757     thus ?case using `x\<noteq>c` by auto qed
  3758   have as2:"finite {t. ((1 - t) *\<^sub>R c + t *\<^sub>R x) \<in> k}" using assms(2) 
  3759     apply(rule finite_surj[where f="\<lambda>z. SOME t. (1-t) *\<^sub>R c + t *\<^sub>R x = z"])
  3760     apply safe unfolding image_iff apply rule defer apply assumption
  3761     apply(rule sym) apply(rule some_equality) defer apply(drule *) by auto
  3762   have "(f \<circ> (\<lambda>t. (1 - t) *\<^sub>R c + t *\<^sub>R x)) 1 = y"
  3763     apply(rule has_derivative_zero_unique_strong_interval[OF as2 as1, of ])
  3764     unfolding o_def using assms(5) defer apply-apply(rule)
  3765   proof- fix t assume as:"t\<in>{0..1} - {t. (1 - t) *\<^sub>R c + t *\<^sub>R x \<in> k}"
  3766     have *:"c - t *\<^sub>R c + t *\<^sub>R x \<in> s - k" apply safe apply(rule conv[unfolded scaleR_simps]) 
  3767       using `x\<in>s` `c\<in>s` as by(auto simp add: algebra_simps)
  3768     have "(f \<circ> (\<lambda>t. (1 - t) *\<^sub>R c + t *\<^sub>R x) has_derivative (\<lambda>x. 0) \<circ> (\<lambda>z. (0 - z *\<^sub>R c) + z *\<^sub>R x)) (at t within {0..1})"
  3769       apply(rule diff_chain_within) apply(rule has_derivative_add)
  3770       unfolding scaleR_simps apply(rule has_derivative_sub) apply(rule has_derivative_const)
  3771       apply(rule has_derivative_vmul_within,rule has_derivative_id)+ 
  3772       apply(rule has_derivative_within_subset,rule assms(6)[rule_format])
  3773       apply(rule *) apply safe apply(rule conv[unfolded scaleR_simps]) using `x\<in>s` `c\<in>s` by auto
  3774     thus "((\<lambda>xa. f ((1 - xa) *\<^sub>R c + xa *\<^sub>R x)) has_derivative (\<lambda>h. 0)) (at t within {0..1})" unfolding o_def .
  3775   qed auto thus ?thesis by auto qed
  3776 
  3777 subsection {* Also to any open connected set with finite set of exceptions. Could 
  3778  generalize to locally convex set with limpt-free set of exceptions. *}
  3779 
  3780 lemma has_derivative_zero_unique_strong_connected: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  3781   assumes "connected s" "open s" "finite k" "continuous_on s f" "c \<in> s" "f c = y"
  3782   "\<forall>x\<in>(s - k). (f has_derivative (\<lambda>h. 0)) (at x within s)" "x\<in>s"
  3783   shows "f x = y"
  3784 proof- have "{x \<in> s. f x \<in> {y}} = {} \<or> {x \<in> s. f x \<in> {y}} = s"
  3785     apply(rule assms(1)[unfolded connected_clopen,rule_format]) apply rule defer
  3786     apply(rule continuous_closed_in_preimage[OF assms(4) closed_sing])
  3787     apply(rule open_openin_trans[OF assms(2)]) unfolding open_contains_ball
  3788   proof safe fix x assume "x\<in>s" 
  3789     from assms(2)[unfolded open_contains_ball,rule_format,OF this] guess e .. note e=conjunctD2[OF this]
  3790     show "\<exists>e>0. ball x e \<subseteq> {xa \<in> s. f xa \<in> {f x}}" apply(rule,rule,rule e)
  3791     proof safe fix y assume y:"y \<in> ball x e" thus "y\<in>s" using e by auto
  3792       show "f y = f x" apply(rule has_derivative_zero_unique_strong_convex[OF convex_ball])
  3793         apply(rule assms) apply(rule continuous_on_subset,rule assms) apply(rule e)+
  3794         apply(subst centre_in_ball,rule e,rule) apply safe
  3795         apply(rule has_derivative_within_subset) apply(rule assms(7)[rule_format])
  3796         using y e by auto qed qed
  3797   thus ?thesis using `x\<in>s` `f c = y` `c\<in>s` by auto qed
  3798 
  3799 subsection {* Integrating characteristic function of an interval. *}
  3800 
  3801 lemma has_integral_restrict_open_subinterval: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  3802   assumes "(f has_integral i) {c..d}" "{c..d} \<subseteq> {a..b}"
  3803   shows "((\<lambda>x. if x \<in> {c<..<d} then f x else 0) has_integral i) {a..b}"
  3804 proof- def g \<equiv> "\<lambda>x. if x \<in>{c<..<d} then f x else 0"
  3805   { presume *:"{c..d}\<noteq>{} \<Longrightarrow> ?thesis"
  3806     show ?thesis apply(cases,rule *,assumption)
  3807     proof- case goal1 hence *:"{c<..<d} = {}" using interval_open_subset_closed by auto 
  3808       show ?thesis using assms(1) unfolding * using goal1 by auto
  3809     qed } assume "{c..d}\<noteq>{}"
  3810   from partial_division_extend_1[OF assms(2) this] guess p . note p=this
  3811   note mon = monoidal_lifted[OF monoidal_monoid] 
  3812   note operat = operative_division[OF this operative_integral p(1), THEN sym]
  3813   let ?P = "(if g integrable_on {a..b} then Some (integral {a..b} g) else None) = Some i"
  3814   { presume "?P" hence "g integrable_on {a..b} \<and> integral {a..b} g = i"
  3815       apply- apply(cases,subst(asm) if_P,assumption) by auto
  3816     thus ?thesis using integrable_integral unfolding g_def by auto }
  3817 
  3818   note iterate_eq_neutral[OF mon,unfolded neutral_lifted[OF monoidal_monoid]]
  3819   note * = this[unfolded neutral_monoid]
  3820   have iterate:"iterate (lifted op +) (p - {{c..d}})
  3821       (\<lambda>i. if g integrable_on i then Some (integral i g) else None) = Some 0"
  3822   proof(rule *,rule) case goal1 hence "x\<in>p" by auto note div = division_ofD(2-5)[OF p(1) this]
  3823     from div(3) guess u v apply-by(erule exE)+ note uv=this
  3824     have "interior x \<inter> interior {c..d} = {}" using div(4)[OF p(2)] goal1 by auto
  3825     hence "(g has_integral 0) x" unfolding uv apply-apply(rule has_integral_spike_interior[where f="\<lambda>x. 0"])
  3826       unfolding g_def interior_closed_interval by auto thus ?case by auto
  3827   qed
  3828 
  3829   have *:"p = insert {c..d} (p - {{c..d}})" using p by auto
  3830   have **:"g integrable_on {c..d}" apply(rule integrable_spike_interior[where f=f])
  3831     unfolding g_def defer apply(rule has_integral_integrable) using assms(1) by auto
  3832   moreover have "integral {c..d} g = i" apply(rule has_integral_unique[OF _ assms(1)])
  3833     apply(rule has_integral_spike_interior[where f=g]) defer
  3834     apply(rule integrable_integral[OF **]) unfolding g_def by auto
  3835   ultimately show ?P unfolding operat apply- apply(subst *) apply(subst iterate_insert) apply rule+
  3836     unfolding iterate defer apply(subst if_not_P) defer using p by auto qed
  3837 
  3838 lemma has_integral_restrict_closed_subinterval: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  3839   assumes "(f has_integral i) ({c..d})" "{c..d} \<subseteq> {a..b}" 
  3840   shows "((\<lambda>x. if x \<in> {c..d} then f x else 0) has_integral i) {a..b}"
  3841 proof- note has_integral_restrict_open_subinterval[OF assms]
  3842   note * = has_integral_spike[OF negligible_frontier_interval _ this]
  3843   show ?thesis apply(rule *[of c d]) using interval_open_subset_closed[of c d] by auto qed
  3844 
  3845 lemma has_integral_restrict_closed_subintervals_eq: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach" assumes "{c..d} \<subseteq> {a..b}" 
  3846   shows "((\<lambda>x. if x \<in> {c..d} then f x else 0) has_integral i) {a..b} \<longleftrightarrow> (f has_integral i) {c..d}" (is "?l = ?r")
  3847 proof(cases "{c..d} = {}") case False let ?g = "\<lambda>x. if x \<in> {c..d} then f x else 0"
  3848   show ?thesis apply rule defer apply(rule has_integral_restrict_closed_subinterval[OF _ assms])
  3849   proof assumption assume ?l hence "?g integrable_on {c..d}"
  3850       apply-apply(rule integrable_subinterval[OF _ assms]) by auto
  3851     hence *:"f integrable_on {c..d}"apply-apply(rule integrable_eq) by auto
  3852     hence "i = integral {c..d} f" apply-apply(rule has_integral_unique)
  3853       apply(rule `?l`) apply(rule has_integral_restrict_closed_subinterval[OF _ assms]) by auto
  3854     thus ?r using * by auto qed qed auto
  3855 
  3856 subsection {* Hence we can apply the limit process uniformly to all integrals. *}
  3857 
  3858 lemma has_integral': fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" shows
  3859  "(f has_integral i) s \<longleftrightarrow> (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
  3860   \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> s then f(x) else 0) has_integral z) {a..b} \<and> norm(z - i) < e))" (is "?l \<longleftrightarrow> (\<forall>e>0. ?r e)")
  3861 proof- { presume *:"\<exists>a b. s = {a..b} \<Longrightarrow> ?thesis"
  3862     show ?thesis apply(cases,rule *,assumption)
  3863       apply(subst has_integral_alt) by auto }
  3864   assume "\<exists>a b. s = {a..b}" then guess a b apply-by(erule exE)+ note s=this
  3865   from bounded_interval[of a b, THEN conjunct1, unfolded bounded_pos] guess B ..
  3866   note B = conjunctD2[OF this,rule_format] show ?thesis apply safe
  3867   proof- fix e assume ?l "e>(0::real)"
  3868     show "?r e" apply(rule_tac x="B+1" in exI) apply safe defer apply(rule_tac x=i in exI)
  3869     proof fix c d assume as:"ball 0 (B+1) \<subseteq> {c..d::'n::ordered_euclidean_space}"
  3870       thus "((\<lambda>x. if x \<in> s then f x else 0) has_integral i) {c..d}" unfolding s
  3871         apply-apply(rule has_integral_restrict_closed_subinterval) apply(rule `?l`[unfolded s])
  3872         apply safe apply(drule B(2)[rule_format]) unfolding subset_eq apply(erule_tac x=x in ballE)
  3873         by(auto simp add:dist_norm)
  3874     qed(insert B `e>0`, auto)
  3875   next assume as:"\<forall>e>0. ?r e" 
  3876     from this[rule_format,OF zero_less_one] guess C .. note C=conjunctD2[OF this,rule_format]
  3877     def c \<equiv> "(\<chi>\<chi> i. - max B C)::'n::ordered_euclidean_space" and d \<equiv> "(\<chi>\<chi> i. max B C)::'n::ordered_euclidean_space"
  3878     have c_d:"{a..b} \<subseteq> {c..d}" apply safe apply(drule B(2)) unfolding mem_interval
  3879     proof case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def
  3880         by(auto simp add:field_simps) qed
  3881     have "ball 0 C \<subseteq> {c..d}" apply safe unfolding mem_interval mem_ball dist_norm 
  3882     proof case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto qed
  3883     from C(2)[OF this] have "\<exists>y. (f has_integral y) {a..b}"
  3884       unfolding has_integral_restrict_closed_subintervals_eq[OF c_d,THEN sym] unfolding s by auto
  3885     then guess y .. note y=this
  3886 
  3887     have "y = i" proof(rule ccontr) assume "y\<noteq>i" hence "0 < norm (y - i)" by auto
  3888       from as[rule_format,OF this] guess C ..  note C=conjunctD2[OF this,rule_format]
  3889       def c \<equiv> "(\<chi>\<chi> i. - max B C)::'n::ordered_euclidean_space" and d \<equiv> "(\<chi>\<chi> i. max B C)::'n::ordered_euclidean_space"
  3890       have c_d:"{a..b} \<subseteq> {c..d}" apply safe apply(drule B(2)) unfolding mem_interval
  3891       proof case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def
  3892           by(auto simp add:field_simps) qed
  3893       have "ball 0 C \<subseteq> {c..d}" apply safe unfolding mem_interval mem_ball dist_norm 
  3894       proof case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto qed
  3895       note C(2)[OF this] then guess z .. note z = conjunctD2[OF this, unfolded s]
  3896       note this[unfolded has_integral_restrict_closed_subintervals_eq[OF c_d]]
  3897       hence "z = y" "norm (z - i) < norm (y - i)" apply- apply(rule has_integral_unique[OF _ y(1)]) .
  3898       thus False by auto qed
  3899     thus ?l using y unfolding s by auto qed qed 
  3900 
  3901 (*lemma has_integral_trans[simp]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real" shows
  3902   "((\<lambda>x. vec1 (f x)) has_integral vec1 i) s \<longleftrightarrow> (f has_integral i) s"
  3903   unfolding has_integral'[unfolded has_integral] 
  3904 proof case goal1 thus ?case apply safe
  3905   apply(erule_tac x=e in allE,safe) apply(rule_tac x=B in exI,safe)
  3906   apply(erule_tac x=a in allE, erule_tac x=b in allE,safe) 
  3907   apply(rule_tac x="dest_vec1 z" in exI,safe) apply(erule_tac x=ea in allE,safe) 
  3908   apply(rule_tac x=d in exI,safe) apply(erule_tac x=p in allE,safe)
  3909   apply(subst(asm)(2) norm_vector_1) unfolding split_def
  3910   unfolding setsum_component Cart_nth.diff cond_value_iff[of dest_vec1]
  3911     Cart_nth.scaleR vec1_dest_vec1 zero_index apply assumption
  3912   apply(subst(asm)(2) norm_vector_1) by auto
  3913 next case goal2 thus ?case apply safe
  3914   apply(erule_tac x=e in allE,safe) apply(rule_tac x=B in exI,safe)
  3915   apply(erule_tac x=a in allE, erule_tac x=b in allE,safe) 
  3916   apply(rule_tac x="vec1 z" in exI,safe) apply(erule_tac x=ea in allE,safe) 
  3917   apply(rule_tac x=d in exI,safe) apply(erule_tac x=p in allE,safe)
  3918   apply(subst norm_vector_1) unfolding split_def
  3919   unfolding setsum_component Cart_nth.diff cond_value_iff[of dest_vec1]
  3920     Cart_nth.scaleR vec1_dest_vec1 zero_index apply assumption
  3921   apply(subst norm_vector_1) by auto qed
  3922 
  3923 lemma integral_trans[simp]: assumes "(f::'n::ordered_euclidean_space \<Rightarrow> real) integrable_on s"
  3924   shows "integral s (\<lambda>x. vec1 (f x)) = vec1 (integral s f)"
  3925   apply(rule integral_unique) using assms by auto
  3926 
  3927 lemma integrable_on_trans[simp]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real" shows
  3928   "(\<lambda>x. vec1 (f x)) integrable_on s \<longleftrightarrow> (f integrable_on s)"
  3929   unfolding integrable_on_def
  3930   apply(subst(2) vec1_dest_vec1(1)[THEN sym]) unfolding has_integral_trans
  3931   apply safe defer apply(rule_tac x="vec1 y" in exI) by auto *)
  3932 
  3933 lemma has_integral_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  3934   assumes "(f has_integral i) s" "(g has_integral j) s"  "\<forall>x\<in>s. (f x) \<le> (g x)"
  3935   shows "i \<le> j" using has_integral_component_le[OF assms(1-2), of 0] using assms(3) by auto
  3936 
  3937 lemma integral_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  3938   assumes "f integrable_on s" "g integrable_on s" "\<forall>x\<in>s. f x \<le> g x"
  3939   shows "integral s f \<le> integral s g"
  3940   using has_integral_le[OF assms(1,2)[unfolded has_integral_integral] assms(3)] .
  3941 
  3942 lemma has_integral_nonneg: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  3943   assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> i" 
  3944   using has_integral_component_nonneg[of "f" "i" s 0]
  3945   unfolding o_def using assms by auto
  3946 
  3947 lemma integral_nonneg: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  3948   assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> integral s f" 
  3949   using has_integral_nonneg[OF assms(1)[unfolded has_integral_integral] assms(2)] .
  3950 
  3951 subsection {* Hence a general restriction property. *}
  3952 
  3953 lemma has_integral_restrict[simp]: assumes "s \<subseteq> t" shows
  3954   "((\<lambda>x. if x \<in> s then f x else (0::'a::banach)) has_integral i) t \<longleftrightarrow> (f has_integral i) s"
  3955 proof- have *:"\<And>x. (if x \<in> t then if x \<in> s then f x else 0 else 0) =  (if x\<in>s then f x else 0)" using assms by auto
  3956   show ?thesis apply(subst(2) has_integral') apply(subst has_integral') unfolding * by rule qed
  3957 
  3958 lemma has_integral_restrict_univ: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" shows
  3959   "((\<lambda>x. if x \<in> s then f x else 0) has_integral i) UNIV \<longleftrightarrow> (f has_integral i) s" by auto
  3960 
  3961 lemma has_integral_on_superset: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  3962   assumes "\<forall>x. ~(x \<in> s) \<longrightarrow> f x = 0" "s \<subseteq> t" "(f has_integral i) s"
  3963   shows "(f has_integral i) t"
  3964 proof- have "(\<lambda>x. if x \<in> s then f x else 0) = (\<lambda>x. if x \<in> t then f x else 0)"
  3965     apply(rule) using assms(1-2) by auto
  3966   thus ?thesis apply- using assms(3) apply(subst has_integral_restrict_univ[THEN sym])
  3967   apply- apply(subst(asm) has_integral_restrict_univ[THEN sym]) by auto qed
  3968 
  3969 lemma integrable_on_superset: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  3970   assumes "\<forall>x. ~(x \<in> s) \<longrightarrow> f x = 0" "s \<subseteq> t" "f integrable_on s"
  3971   shows "f integrable_on t"
  3972   using assms unfolding integrable_on_def by(auto intro:has_integral_on_superset)
  3973 
  3974 lemma integral_restrict_univ[intro]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  3975   shows "f integrable_on s \<Longrightarrow> integral UNIV (\<lambda>x. if x \<in> s then f x else 0) = integral s f"
  3976   apply(rule integral_unique) unfolding has_integral_restrict_univ by auto
  3977 
  3978 lemma integrable_restrict_univ: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" shows
  3979  "(\<lambda>x. if x \<in> s then f x else 0) integrable_on UNIV \<longleftrightarrow> f integrable_on s"
  3980   unfolding integrable_on_def by auto
  3981 
  3982 lemma negligible_on_intervals: "negligible s \<longleftrightarrow> (\<forall>a b. negligible(s \<inter> {a..b}))" (is "?l = ?r")
  3983 proof assume ?r show ?l unfolding negligible_def
  3984   proof safe case goal1 show ?case apply(rule has_integral_negligible[OF `?r`[rule_format,of a b]])
  3985       unfolding indicator_def by auto qed qed auto
  3986 
  3987 lemma has_integral_spike_set_eq: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  3988   assumes "negligible((s - t) \<union> (t - s))" shows "((f has_integral y) s \<longleftrightarrow> (f has_integral y) t)"
  3989   unfolding has_integral_restrict_univ[THEN sym,of f] apply(rule has_integral_spike_eq[OF assms]) by (safe, auto split: split_if_asm)
  3990 
  3991 lemma has_integral_spike_set[dest]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  3992   assumes "negligible((s - t) \<union> (t - s))" "(f has_integral y) s"
  3993   shows "(f has_integral y) t"
  3994   using assms has_integral_spike_set_eq by auto
  3995 
  3996 lemma integrable_spike_set[dest]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  3997   assumes "negligible((s - t) \<union> (t - s))" "f integrable_on s"
  3998   shows "f integrable_on t" using assms(2) unfolding integrable_on_def 
  3999   unfolding has_integral_spike_set_eq[OF assms(1)] .
  4000 
  4001 lemma integrable_spike_set_eq: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4002   assumes "negligible((s - t) \<union> (t - s))"
  4003   shows "(f integrable_on s \<longleftrightarrow> f integrable_on t)"
  4004   apply(rule,rule_tac[!] integrable_spike_set) using assms by auto
  4005 
  4006 (*lemma integral_spike_set:
  4007  "\<forall>f:real^M->real^N g s t.
  4008         negligible(s DIFF t \<union> t DIFF s)
  4009         \<longrightarrow> integral s f = integral t f"
  4010 qed  REPEAT STRIP_TAC THEN REWRITE_TAC[integral] THEN
  4011   AP_TERM_TAC THEN ABS_TAC THEN MATCH_MP_TAC HAS_INTEGRAL_SPIKE_SET_EQ THEN
  4012   ASM_MESON_TAC[]);;
  4013 
  4014 lemma has_integral_interior:
  4015  "\<forall>f:real^M->real^N y s.
  4016         negligible(frontier s)
  4017         \<longrightarrow> ((f has_integral y) (interior s) \<longleftrightarrow> (f has_integral y) s)"
  4018 qed  REPEAT STRIP_TAC THEN MATCH_MP_TAC HAS_INTEGRAL_SPIKE_SET_EQ THEN
  4019   FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (REWRITE_RULE[IMP_CONJ]
  4020     NEGLIGIBLE_SUBSET)) THEN
  4021   REWRITE_TAC[frontier] THEN
  4022   MP_TAC(ISPEC `s:real^M->bool` INTERIOR_SUBSET) THEN
  4023   MP_TAC(ISPEC `s:real^M->bool` CLOSURE_SUBSET) THEN
  4024   SET_TAC[]);;
  4025 
  4026 lemma has_integral_closure:
  4027  "\<forall>f:real^M->real^N y s.
  4028         negligible(frontier s)
  4029         \<longrightarrow> ((f has_integral y) (closure s) \<longleftrightarrow> (f has_integral y) s)"
  4030 qed  REPEAT STRIP_TAC THEN MATCH_MP_TAC HAS_INTEGRAL_SPIKE_SET_EQ THEN
  4031   FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (REWRITE_RULE[IMP_CONJ]
  4032     NEGLIGIBLE_SUBSET)) THEN
  4033   REWRITE_TAC[frontier] THEN
  4034   MP_TAC(ISPEC `s:real^M->bool` INTERIOR_SUBSET) THEN
  4035   MP_TAC(ISPEC `s:real^M->bool` CLOSURE_SUBSET) THEN
  4036   SET_TAC[]);;*)
  4037 
  4038 subsection {* More lemmas that are useful later. *}
  4039 
  4040 lemma has_integral_subset_component_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space"
  4041   assumes "s \<subseteq> t" "(f has_integral i) s" "(f has_integral j) t" "\<forall>x\<in>t. 0 \<le> f(x)$$k"
  4042   shows "i$$k \<le> j$$k"
  4043 proof- note has_integral_restrict_univ[THEN sym, of f]
  4044   note assms(2-3)[unfolded this] note * = has_integral_component_le[OF this]
  4045   show ?thesis apply(rule *) using assms(1,4) by auto qed
  4046 
  4047 lemma has_integral_subset_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4048   assumes "s \<subseteq> t" "(f has_integral i) s" "(f has_integral j) t" "\<forall>x\<in>t. 0 \<le> f(x)"
  4049   shows "i \<le> j" using has_integral_subset_component_le[OF assms(1), of "f" "i" "j" 0] using assms by auto
  4050 
  4051 lemma integral_subset_component_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space"
  4052   assumes "s \<subseteq> t" "f integrable_on s" "f integrable_on t" "\<forall>x \<in> t. 0 \<le> f(x)$$k"
  4053   shows "(integral s f)$$k \<le> (integral t f)$$k"
  4054   apply(rule has_integral_subset_component_le) using assms by auto
  4055 
  4056 lemma integral_subset_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4057   assumes "s \<subseteq> t" "f integrable_on s" "f integrable_on t" "\<forall>x \<in> t. 0 \<le> f(x)"
  4058   shows "(integral s f) \<le> (integral t f)"
  4059   apply(rule has_integral_subset_le) using assms by auto
  4060 
  4061 lemma has_integral_alt': fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4062   shows "(f has_integral i) s \<longleftrightarrow> (\<forall>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}) \<and>
  4063   (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow> norm(integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - i) < e)" (is "?l = ?r")
  4064 proof assume ?r
  4065   show ?l apply- apply(subst has_integral')
  4066   proof safe case goal1 from `?r`[THEN conjunct2,rule_format,OF this] guess B .. note B=conjunctD2[OF this]
  4067     show ?case apply(rule,rule,rule B,safe)
  4068       apply(rule_tac x="integral {a..b} (\<lambda>x. if x \<in> s then f x else 0)" in exI)
  4069       apply(drule B(2)[rule_format]) using integrable_integral[OF `?r`[THEN conjunct1,rule_format]] by auto
  4070   qed next
  4071   assume ?l note as = this[unfolded has_integral'[of f],rule_format]
  4072   let ?f = "\<lambda>x. if x \<in> s then f x else 0"
  4073   show ?r proof safe fix a b::"'n::ordered_euclidean_space"
  4074     from as[OF zero_less_one] guess B .. note B=conjunctD2[OF this,rule_format]
  4075     let ?a = "(\<chi>\<chi> i. min (a$$i) (-B))::'n::ordered_euclidean_space" and ?b = "(\<chi>\<chi> i. max (b$$i) B)::'n::ordered_euclidean_space"
  4076     show "?f integrable_on {a..b}" apply(rule integrable_subinterval[of _ ?a ?b])
  4077     proof- have "ball 0 B \<subseteq> {?a..?b}" apply safe unfolding mem_ball mem_interval dist_norm
  4078       proof case goal1 thus ?case using component_le_norm[of x i] by(auto simp add:field_simps) qed
  4079       from B(2)[OF this] guess z .. note conjunct1[OF this]
  4080       thus "?f integrable_on {?a..?b}" unfolding integrable_on_def by auto
  4081       show "{a..b} \<subseteq> {?a..?b}" apply safe unfolding mem_interval apply(rule,erule_tac x=i in allE) by auto qed
  4082 
  4083     fix e::real assume "e>0" from as[OF this] guess B .. note B=conjunctD2[OF this,rule_format]
  4084     show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow>
  4085                     norm (integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - i) < e"
  4086     proof(rule,rule,rule B,safe) case goal1 from B(2)[OF this] guess z .. note z=conjunctD2[OF this]
  4087       from integral_unique[OF this(1)] show ?case using z(2) by auto qed qed qed 
  4088 
  4089 
  4090 subsection {* Continuity of the integral (for a 1-dimensional interval). *}
  4091 
  4092 lemma integrable_alt: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" shows 
  4093   "f integrable_on s \<longleftrightarrow>
  4094           (\<forall>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}) \<and>
  4095           (\<forall>e>0. \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> {a..b} \<and> ball 0 B \<subseteq> {c..d}
  4096   \<longrightarrow> norm(integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) -
  4097           integral {c..d}  (\<lambda>x. if x \<in> s then f x else 0)) < e)" (is "?l = ?r")
  4098 proof assume ?l then guess y unfolding integrable_on_def .. note this[unfolded has_integral_alt'[of f]]
  4099   note y=conjunctD2[OF this,rule_format] show ?r apply safe apply(rule y)
  4100   proof- case goal1 hence "e/2 > 0" by auto from y(2)[OF this] guess B .. note B=conjunctD2[OF this,rule_format]
  4101     show ?case apply(rule,rule,rule B)
  4102     proof safe case goal1 show ?case apply(rule norm_triangle_half_l)
  4103         using B(2)[OF goal1(1)] B(2)[OF goal1(2)] by auto qed qed
  4104         
  4105 next assume ?r note as = conjunctD2[OF this,rule_format]
  4106   have "Cauchy (\<lambda>n. integral ({(\<chi>\<chi> i. - real n)::'n .. (\<chi>\<chi> i. real n)}) (\<lambda>x. if x \<in> s then f x else 0))"
  4107   proof(unfold Cauchy_def,safe) case goal1
  4108     from as(2)[OF this] guess B .. note B = conjunctD2[OF this,rule_format]
  4109     from real_arch_simple[of B] guess N .. note N = this
  4110     { fix n assume n:"n \<ge> N" have "ball 0 B \<subseteq> {(\<chi>\<chi> i. - real n)::'n..\<chi>\<chi> i. real n}" apply safe
  4111         unfolding mem_ball mem_interval dist_norm
  4112       proof case goal1 thus ?case using component_le_norm[of x i]
  4113           using n N by(auto simp add:field_simps) qed }
  4114     thus ?case apply-apply(rule_tac x=N in exI) apply safe unfolding dist_norm apply(rule B(2)) by auto
  4115   qed from this[unfolded convergent_eq_cauchy[THEN sym]] guess i ..
  4116   note i = this[unfolded Lim_sequentially, rule_format]
  4117 
  4118   show ?l unfolding integrable_on_def has_integral_alt'[of f] apply(rule_tac x=i in exI)
  4119     apply safe apply(rule as(1)[unfolded integrable_on_def])
  4120   proof- case goal1 hence *:"e/2 > 0" by auto
  4121     from i[OF this] guess N .. note N =this[rule_format]
  4122     from as(2)[OF *] guess B .. note B=conjunctD2[OF this,rule_format] let ?B = "max (real N) B"
  4123     show ?case apply(rule_tac x="?B" in exI)
  4124     proof safe show "0 < ?B" using B(1) by auto
  4125       fix a b assume ab:"ball 0 ?B \<subseteq> {a..b::'n::ordered_euclidean_space}"
  4126       from real_arch_simple[of ?B] guess n .. note n=this
  4127       show "norm (integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - i) < e"
  4128         apply(rule norm_triangle_half_l) apply(rule B(2)) defer apply(subst norm_minus_commute)
  4129         apply(rule N[unfolded dist_norm, of n])
  4130       proof safe show "N \<le> n" using n by auto
  4131         fix x::"'n::ordered_euclidean_space" assume x:"x \<in> ball 0 B" hence "x\<in> ball 0 ?B" by auto
  4132         thus "x\<in>{a..b}" using ab by blast 
  4133         show "x\<in>{\<chi>\<chi> i. - real n..\<chi>\<chi> i. real n}" using x unfolding mem_interval mem_ball dist_norm apply-
  4134         proof case goal1 thus ?case using component_le_norm[of x i]
  4135             using n by(auto simp add:field_simps) qed qed qed qed qed
  4136 
  4137 lemma integrable_altD: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4138   assumes "f integrable_on s"
  4139   shows "\<And>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}"
  4140   "\<And>e. e>0 \<Longrightarrow> \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> {a..b} \<and> ball 0 B \<subseteq> {c..d}
  4141   \<longrightarrow> norm(integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - integral {c..d}  (\<lambda>x. if x \<in> s then f x else 0)) < e"
  4142   using assms[unfolded integrable_alt[of f]] by auto
  4143 
  4144 lemma integrable_on_subinterval: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4145   assumes "f integrable_on s" "{a..b} \<subseteq> s" shows "f integrable_on {a..b}"
  4146   apply(rule integrable_eq) defer apply(rule integrable_altD(1)[OF assms(1)])
  4147   using assms(2) by auto
  4148 
  4149 subsection {* A straddling criterion for integrability. *}
  4150 
  4151 lemma integrable_straddle_interval: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4152   assumes "\<forall>e>0. \<exists>g  h i j. (g has_integral i) ({a..b}) \<and> (h has_integral j) ({a..b}) \<and>
  4153   norm(i - j) < e \<and> (\<forall>x\<in>{a..b}. (g x) \<le> (f x) \<and> (f x) \<le>(h x))"
  4154   shows "f integrable_on {a..b}"
  4155 proof(subst integrable_cauchy,safe)
  4156   case goal1 hence e:"e/3 > 0" by auto note assms[rule_format,OF this]
  4157   then guess g h i j apply- by(erule exE conjE)+ note obt = this
  4158   from obt(1)[unfolded has_integral[of g], rule_format, OF e] guess d1 .. note d1=conjunctD2[OF this,rule_format]
  4159   from obt(2)[unfolded has_integral[of h], rule_format, OF e] guess d2 .. note d2=conjunctD2[OF this,rule_format]
  4160   show ?case apply(rule_tac x="\<lambda>x. d1 x \<inter> d2 x" in exI) apply(rule conjI gauge_inter d1 d2)+ unfolding fine_inter
  4161   proof safe have **:"\<And>i j g1 g2 h1 h2 f1 f2. g1 - h2 \<le> f1 - f2 \<Longrightarrow> f1 - f2 \<le> h1 - g2 \<Longrightarrow>
  4162       abs(i - j) < e / 3 \<Longrightarrow> abs(g2 - i) < e / 3 \<Longrightarrow>  abs(g1 - i) < e / 3 \<Longrightarrow> 
  4163       abs(h2 - j) < e / 3 \<Longrightarrow> abs(h1 - j) < e / 3 \<Longrightarrow> abs(f1 - f2) < e" using `e>0` by arith
  4164     case goal1 note tagged_division_ofD(2-4) note * = this[OF goal1(1)] this[OF goal1(4)]
  4165 
  4166     have "(\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p1. content k *\<^sub>R g x) \<ge> 0"
  4167       "0 \<le> (\<Sum>(x, k)\<in>p2. content k *\<^sub>R h x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)" 
  4168       "(\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R g x) \<ge> 0"
  4169       "0 \<le> (\<Sum>(x, k)\<in>p1. content k *\<^sub>R h x) - (\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x)" 
  4170       unfolding setsum_subtractf[THEN sym] apply- apply(rule_tac[!] setsum_nonneg)
  4171       apply safe unfolding real_scaleR_def mult.diff_right[THEN sym]
  4172       apply(rule_tac[!] mult_nonneg_nonneg)
  4173     proof- fix a b assume ab:"(a,b) \<in> p1"
  4174       show "0 \<le> content b" using *(3)[OF ab] apply safe using content_pos_le . thus "0 \<le> content b" .
  4175       show "0 \<le> f a - g a" "0 \<le> h a - f a" using *(1-2)[OF ab] using obt(4)[rule_format,of a] by auto
  4176     next fix a b assume ab:"(a,b) \<in> p2"
  4177       show "0 \<le> content b" using *(6)[OF ab] apply safe using content_pos_le . thus "0 \<le> content b" .
  4178       show "0 \<le> f a - g a" "0 \<le> h a - f a" using *(4-5)[OF ab] using obt(4)[rule_format,of a] by auto qed 
  4179 
  4180     thus ?case apply- unfolding real_norm_def apply(rule **) defer defer
  4181       unfolding real_norm_def[THEN sym] apply(rule obt(3))
  4182       apply(rule d1(2)[OF conjI[OF goal1(4,5)]])
  4183       apply(rule d1(2)[OF conjI[OF goal1(1,2)]])
  4184       apply(rule d2(2)[OF conjI[OF goal1(4,6)]])
  4185       apply(rule d2(2)[OF conjI[OF goal1(1,3)]]) by auto qed qed 
  4186      
  4187 lemma integrable_straddle: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4188   assumes "\<forall>e>0. \<exists>g h i j. (g has_integral i) s \<and> (h has_integral j) s \<and>
  4189   norm(i - j) < e \<and> (\<forall>x\<in>s. (g x) \<le>(f x) \<and>(f x) \<le>(h x))"
  4190   shows "f integrable_on s"
  4191 proof- have "\<And>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}"
  4192   proof(rule integrable_straddle_interval,safe) case goal1 hence *:"e/4 > 0" by auto
  4193     from assms[rule_format,OF this] guess g h i j apply-by(erule exE conjE)+ note obt=this
  4194     note obt(1)[unfolded has_integral_alt'[of g]] note conjunctD2[OF this, rule_format]
  4195     note g = this(1) and this(2)[OF *] from this(2) guess B1 .. note B1 = conjunctD2[OF this,rule_format]
  4196     note obt(2)[unfolded has_integral_alt'[of h]] note conjunctD2[OF this, rule_format]
  4197     note h = this(1) and this(2)[OF *] from this(2) guess B2 .. note B2 = conjunctD2[OF this,rule_format]
  4198     def c \<equiv> "(\<chi>\<chi> i. min (a$$i) (- (max B1 B2)))::'n" and d \<equiv> "(\<chi>\<chi> i. max (b$$i) (max B1 B2))::'n"
  4199     have *:"ball 0 B1 \<subseteq> {c..d}" "ball 0 B2 \<subseteq> {c..d}" apply safe unfolding mem_ball mem_interval dist_norm
  4200     proof(rule_tac[!] allI)
  4201       case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto next
  4202       case goal2 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto qed
  4203     have **:"\<And>ch cg ag ah::real. norm(ah - ag) \<le> norm(ch - cg) \<Longrightarrow> norm(cg - i) < e / 4 \<Longrightarrow>
  4204       norm(ch - j) < e / 4 \<Longrightarrow> norm(ag - ah) < e"
  4205       using obt(3) unfolding real_norm_def by arith 
  4206     show ?case apply(rule_tac x="\<lambda>x. if x \<in> s then g x else 0" in exI)
  4207                apply(rule_tac x="\<lambda>x. if x \<in> s then h x else 0" in exI)
  4208       apply(rule_tac x="integral {a..b} (\<lambda>x. if x \<in> s then g x else 0)" in exI)
  4209       apply(rule_tac x="integral {a..b} (\<lambda>x. if x \<in> s then h x else 0)" in exI)
  4210       apply safe apply(rule_tac[1-2] integrable_integral,rule g,rule h)
  4211       apply(rule **[OF _ B1(2)[OF *(1)] B2(2)[OF *(2)]])
  4212     proof- have *:"\<And>x f g. (if x \<in> s then f x else 0) - (if x \<in> s then g x else 0) =
  4213         (if x \<in> s then f x - g x else (0::real))" by auto
  4214       note ** = abs_of_nonneg[OF integral_nonneg[OF integrable_sub, OF h g]]
  4215       show " norm (integral {a..b} (\<lambda>x. if x \<in> s then h x else 0) -
  4216                    integral {a..b} (\<lambda>x. if x \<in> s then g x else 0))
  4217            \<le> norm (integral {c..d} (\<lambda>x. if x \<in> s then h x else 0) -
  4218                    integral {c..d} (\<lambda>x. if x \<in> s then g x else 0))"
  4219         unfolding integral_sub[OF h g,THEN sym] real_norm_def apply(subst **) defer apply(subst **) defer
  4220         apply(rule has_integral_subset_le) defer apply(rule integrable_integral integrable_sub h g)+
  4221       proof safe fix x assume "x\<in>{a..b}" thus "x\<in>{c..d}" unfolding mem_interval c_def d_def
  4222           apply - apply rule apply(erule_tac x=i in allE) by auto
  4223       qed(insert obt(4), auto) qed(insert obt(4), auto) qed note interv = this
  4224 
  4225   show ?thesis unfolding integrable_alt[of f] apply safe apply(rule interv)
  4226   proof- case goal1 hence *:"e/3 > 0" by auto
  4227     from assms[rule_format,OF this] guess g h i j apply-by(erule exE conjE)+ note obt=this
  4228     note obt(1)[unfolded has_integral_alt'[of g]] note conjunctD2[OF this, rule_format]
  4229     note g = this(1) and this(2)[OF *] from this(2) guess B1 .. note B1 = conjunctD2[OF this,rule_format]
  4230     note obt(2)[unfolded has_integral_alt'[of h]] note conjunctD2[OF this, rule_format]
  4231     note h = this(1) and this(2)[OF *] from this(2) guess B2 .. note B2 = conjunctD2[OF this,rule_format]
  4232     show ?case apply(rule_tac x="max B1 B2" in exI) apply safe apply(rule min_max.less_supI1,rule B1)
  4233     proof- fix a b c d::"'n::ordered_euclidean_space" assume as:"ball 0 (max B1 B2) \<subseteq> {a..b}" "ball 0 (max B1 B2) \<subseteq> {c..d}"
  4234       have **:"ball 0 B1 \<subseteq> ball (0::'n::ordered_euclidean_space) (max B1 B2)" "ball 0 B2 \<subseteq> ball (0::'n::ordered_euclidean_space) (max B1 B2)" by auto
  4235       have *:"\<And>ga gc ha hc fa fc::real. abs(ga - i) < e / 3 \<and> abs(gc - i) < e / 3 \<and> abs(ha - j) < e / 3 \<and>
  4236         abs(hc - j) < e / 3 \<and> abs(i - j) < e / 3 \<and> ga \<le> fa \<and> fa \<le> ha \<and> gc \<le> fc \<and> fc \<le> hc\<Longrightarrow> abs(fa - fc) < e" by smt
  4237       show "norm (integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - integral {c..d} (\<lambda>x. if x \<in> s then f x else 0)) < e"
  4238         unfolding real_norm_def apply(rule *, safe) unfolding real_norm_def[THEN sym]
  4239         apply(rule B1(2),rule order_trans,rule **,rule as(1)) 
  4240         apply(rule B1(2),rule order_trans,rule **,rule as(2)) 
  4241         apply(rule B2(2),rule order_trans,rule **,rule as(1)) 
  4242         apply(rule B2(2),rule order_trans,rule **,rule as(2)) 
  4243         apply(rule obt) apply(rule_tac[!] integral_le) using obt
  4244         by(auto intro!: h g interv) qed qed qed 
  4245 
  4246 subsection {* Adding integrals over several sets. *}
  4247 
  4248 lemma has_integral_union: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4249   assumes "(f has_integral i) s" "(f has_integral j) t" "negligible(s \<inter> t)"
  4250   shows "(f has_integral (i + j)) (s \<union> t)"
  4251 proof- note * = has_integral_restrict_univ[THEN sym, of f]
  4252   show ?thesis unfolding * apply(rule has_integral_spike[OF assms(3)])
  4253     defer apply(rule has_integral_add[OF assms(1-2)[unfolded *]]) by auto qed
  4254 
  4255 lemma has_integral_unions: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4256   assumes "finite t" "\<forall>s\<in>t. (f has_integral (i s)) s"  "\<forall>s\<in>t. \<forall>s'\<in>t. ~(s = s') \<longrightarrow> negligible(s \<inter> s')"
  4257   shows "(f has_integral (setsum i t)) (\<Union>t)"
  4258 proof- note * = has_integral_restrict_univ[THEN sym, of f]
  4259   have **:"negligible (\<Union>((\<lambda>(a,b). a \<inter> b) ` {(a,b). a \<in> t \<and> b \<in> {y. y \<in> t \<and> ~(a = y)}}))"
  4260     apply(rule negligible_unions) apply(rule finite_imageI) apply(rule finite_subset[of _ "t \<times> t"]) defer 
  4261     apply(rule finite_cartesian_product[OF assms(1,1)]) using assms(3) by auto 
  4262   note assms(2)[unfolded *] note has_integral_setsum[OF assms(1) this]
  4263   thus ?thesis unfolding * apply-apply(rule has_integral_spike[OF **]) defer apply assumption
  4264   proof safe case goal1 thus ?case
  4265     proof(cases "x\<in>\<Union>t") case True then guess s unfolding Union_iff .. note s=this
  4266       hence *:"\<forall>b\<in>t. x \<in> b \<longleftrightarrow> b = s" using goal1(3) by blast
  4267       show ?thesis unfolding if_P[OF True] apply(rule trans) defer
  4268         apply(rule setsum_cong2) apply(subst *, assumption) apply(rule refl)
  4269         unfolding setsum_delta[OF assms(1)] using s by auto qed auto qed qed
  4270 
  4271 subsection {* In particular adding integrals over a division, maybe not of an interval. *}
  4272 
  4273 lemma has_integral_combine_division: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4274   assumes "d division_of s" "\<forall>k\<in>d. (f has_integral (i k)) k"
  4275   shows "(f has_integral (setsum i d)) s"
  4276 proof- note d = division_ofD[OF assms(1)]
  4277   show ?thesis unfolding d(6)[THEN sym] apply(rule has_integral_unions)
  4278     apply(rule d assms)+ apply(rule,rule,rule)
  4279   proof- case goal1 from d(4)[OF this(1)] d(4)[OF this(2)]
  4280     guess a c b d apply-by(erule exE)+ note obt=this
  4281     from d(5)[OF goal1] show ?case unfolding obt interior_closed_interval
  4282       apply-apply(rule negligible_subset[of "({a..b}-{a<..<b}) \<union> ({c..d}-{c<..<d})"])
  4283       apply(rule negligible_union negligible_frontier_interval)+ by auto qed qed
  4284 
  4285 lemma integral_combine_division_bottomup: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4286   assumes "d division_of s" "\<forall>k\<in>d. f integrable_on k"
  4287   shows "integral s f = setsum (\<lambda>i. integral i f) d"
  4288   apply(rule integral_unique) apply(rule has_integral_combine_division[OF assms(1)])
  4289   using assms(2) unfolding has_integral_integral .
  4290 
  4291 lemma has_integral_combine_division_topdown: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4292   assumes "f integrable_on s" "d division_of k" "k \<subseteq> s"
  4293   shows "(f has_integral (setsum (\<lambda>i. integral i f) d)) k"
  4294   apply(rule has_integral_combine_division[OF assms(2)])
  4295   apply safe unfolding has_integral_integral[THEN sym]
  4296 proof- case goal1 from division_ofD(2,4)[OF assms(2) this]
  4297   show ?case apply safe apply(rule integrable_on_subinterval)
  4298     apply(rule assms) using assms(3) by auto qed
  4299 
  4300 lemma integral_combine_division_topdown: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4301   assumes "f integrable_on s" "d division_of s"
  4302   shows "integral s f = setsum (\<lambda>i. integral i f) d"
  4303   apply(rule integral_unique,rule has_integral_combine_division_topdown) using assms by auto
  4304 
  4305 lemma integrable_combine_division: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4306   assumes "d division_of s" "\<forall>i\<in>d. f integrable_on i"
  4307   shows "f integrable_on s"
  4308   using assms(2) unfolding integrable_on_def
  4309   by(metis has_integral_combine_division[OF assms(1)])
  4310 
  4311 lemma integrable_on_subdivision: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4312   assumes "d division_of i" "f integrable_on s" "i \<subseteq> s"
  4313   shows "f integrable_on i"
  4314   apply(rule integrable_combine_division assms)+
  4315 proof safe case goal1 note division_ofD(2,4)[OF assms(1) this]
  4316   thus ?case apply safe apply(rule integrable_on_subinterval[OF assms(2)])
  4317     using assms(3) by auto qed
  4318 
  4319 subsection {* Also tagged divisions. *}
  4320 
  4321 lemma has_integral_combine_tagged_division: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4322   assumes "p tagged_division_of s" "\<forall>(x,k) \<in> p. (f has_integral (i k)) k"
  4323   shows "(f has_integral (setsum (\<lambda>(x,k). i k) p)) s"
  4324 proof- have *:"(f has_integral (setsum (\<lambda>k. integral k f) (snd ` p))) s"
  4325     apply(rule has_integral_combine_division) apply(rule division_of_tagged_division[OF assms(1)])
  4326     using assms(2) unfolding has_integral_integral[THEN sym] by(safe,auto)
  4327   thus ?thesis apply- apply(rule subst[where P="\<lambda>i. (f has_integral i) s"]) defer apply assumption
  4328     apply(rule trans[of _ "setsum (\<lambda>(x,k). integral k f) p"]) apply(subst eq_commute)
  4329     apply(rule setsum_over_tagged_division_lemma[OF assms(1)]) apply(rule integral_null,assumption)
  4330     apply(rule setsum_cong2) using assms(2) by auto qed
  4331 
  4332 lemma integral_combine_tagged_division_bottomup: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4333   assumes "p tagged_division_of {a..b}" "\<forall>(x,k)\<in>p. f integrable_on k"
  4334   shows "integral {a..b} f = setsum (\<lambda>(x,k). integral k f) p"
  4335   apply(rule integral_unique) apply(rule has_integral_combine_tagged_division[OF assms(1)])
  4336   using assms(2) by auto
  4337 
  4338 lemma has_integral_combine_tagged_division_topdown: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4339   assumes "f integrable_on {a..b}" "p tagged_division_of {a..b}"
  4340   shows "(f has_integral (setsum (\<lambda>(x,k). integral k f) p)) {a..b}"
  4341   apply(rule has_integral_combine_tagged_division[OF assms(2)])
  4342 proof safe case goal1 note tagged_division_ofD(3-4)[OF assms(2) this]
  4343   thus ?case using integrable_subinterval[OF assms(1)] by auto qed
  4344 
  4345 lemma integral_combine_tagged_division_topdown: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4346   assumes "f integrable_on {a..b}" "p tagged_division_of {a..b}"
  4347   shows "integral {a..b} f = setsum (\<lambda>(x,k). integral k f) p"
  4348   apply(rule integral_unique,rule has_integral_combine_tagged_division_topdown) using assms by auto
  4349 
  4350 subsection {* Henstock's lemma. *}
  4351 
  4352 lemma henstock_lemma_part1: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4353   assumes "f integrable_on {a..b}" "0 < e" "gauge d"
  4354   "(\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - integral({a..b}) f) < e)"
  4355   and p:"p tagged_partial_division_of {a..b}" "d fine p"
  4356   shows "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x - integral k f) p) \<le> e" (is "?x \<le> e")
  4357 proof-  { presume "\<And>k. 0<k \<Longrightarrow> ?x \<le> e + k" thus ?thesis by arith }
  4358   fix k::real assume k:"k>0" note p' = tagged_partial_division_ofD[OF p(1)]
  4359   have "\<Union>snd ` p \<subseteq> {a..b}" using p'(3) by fastsimp
  4360   note partial_division_of_tagged_division[OF p(1)] this
  4361   from partial_division_extend_interval[OF this] guess q . note q=this and q' = division_ofD[OF this(2)]
  4362   def r \<equiv> "q - snd ` p" have "snd ` p \<inter> r = {}" unfolding r_def by auto
  4363   have r:"finite r" using q' unfolding r_def by auto
  4364 
  4365   have "\<forall>i\<in>r. \<exists>p. p tagged_division_of i \<and> d fine p \<and>
  4366     norm(setsum (\<lambda>(x,j). content j *\<^sub>R f x) p - integral i f) < k / (real (card r) + 1)"
  4367   proof safe case goal1 hence i:"i \<in> q" unfolding r_def by auto
  4368     from q'(4)[OF this] guess u v apply-by(erule exE)+ note uv=this
  4369     have *:"k / (real (card r) + 1) > 0" apply(rule divide_pos_pos,rule k) by auto
  4370     have "f integrable_on {u..v}" apply(rule integrable_subinterval[OF assms(1)])
  4371       using q'(2)[OF i] unfolding uv by auto
  4372     note integrable_integral[OF this, unfolded has_integral[of f]]
  4373     from this[rule_format,OF *] guess dd .. note dd=conjunctD2[OF this,rule_format]
  4374     note gauge_inter[OF `gauge d` dd(1)] from fine_division_exists[OF this,of u v] guess qq .
  4375     thus ?case apply(rule_tac x=qq in exI) using dd(2)[of qq] unfolding fine_inter uv by auto qed
  4376   from bchoice[OF this] guess qq .. note qq=this[rule_format]
  4377 
  4378   let ?p = "p \<union> \<Union>qq ` r" have "norm ((\<Sum>(x, k)\<in>?p. content k *\<^sub>R f x) - integral {a..b} f) < e"
  4379     apply(rule assms(4)[rule_format])
  4380   proof show "d fine ?p" apply(rule fine_union,rule p) apply(rule fine_unions) using qq by auto 
  4381     note * = tagged_partial_division_of_union_self[OF p(1)]
  4382     have "p \<union> \<Union>qq ` r tagged_division_of \<Union>snd ` p \<union> \<Union>r"
  4383     proof(rule tagged_division_union[OF * tagged_division_unions])
  4384       show "finite r" by fact case goal2 thus ?case using qq by auto
  4385     next case goal3 thus ?case apply(rule,rule,rule) apply(rule q'(5)) unfolding r_def by auto
  4386     next case goal4 thus ?case apply(rule inter_interior_unions_intervals) apply(fact,rule)
  4387         apply(rule,rule q') defer apply(rule,subst Int_commute) 
  4388         apply(rule inter_interior_unions_intervals) apply(rule finite_imageI,rule p',rule) defer
  4389         apply(rule,rule q') using q(1) p' unfolding r_def by auto qed
  4390     moreover have "\<Union>snd ` p \<union> \<Union>r = {a..b}" "{qq i |i. i \<in> r} = qq ` r"
  4391       unfolding Union_Un_distrib[THEN sym] r_def using q by auto
  4392     ultimately show "?p tagged_division_of {a..b}" by fastsimp qed
  4393 
  4394   hence "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>\<Union>qq ` r. content k *\<^sub>R f x) -
  4395     integral {a..b} f) < e" apply(subst setsum_Un_zero[THEN sym]) apply(rule p') prefer 3 
  4396     apply assumption apply rule apply(rule finite_imageI,rule r) apply safe apply(drule qq)
  4397   proof- fix x l k assume as:"(x,l)\<in>p" "(x,l)\<in>qq k" "k\<in>r"
  4398     note qq[OF this(3)] note tagged_division_ofD(3,4)[OF conjunct1[OF this] as(2)]
  4399     from this(2) guess u v apply-by(erule exE)+ note uv=this
  4400     have "l\<in>snd ` p" unfolding image_iff apply(rule_tac x="(x,l)" in bexI) using as by auto
  4401     hence "l\<in>q" "k\<in>q" "l\<noteq>k" using as(1,3) q(1) unfolding r_def by auto
  4402     note q'(5)[OF this] hence "interior l = {}" using subset_interior[OF `l \<subseteq> k`] by blast
  4403     thus "content l *\<^sub>R f x = 0" unfolding uv content_eq_0_interior[THEN sym] by auto qed auto
  4404 
  4405   hence "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + setsum (setsum (\<lambda>(x, k). content k *\<^sub>R f x))
  4406     (qq ` r) - integral {a..b} f) < e" apply(subst(asm) setsum_UNION_zero)
  4407     prefer 4 apply assumption apply(rule finite_imageI,fact)
  4408     unfolding split_paired_all split_conv image_iff defer apply(erule bexE)+
  4409   proof- fix x m k l T1 T2 assume "(x,m)\<in>T1" "(x,m)\<in>T2" "T1\<noteq>T2" "k\<in>r" "l\<in>r" "T1 = qq k" "T2 = qq l"
  4410     note as = this(1-5)[unfolded this(6-)] note kl = tagged_division_ofD(3,4)[OF qq[THEN conjunct1]]
  4411     from this(2)[OF as(4,1)] guess u v apply-by(erule exE)+ note uv=this
  4412     have *:"interior (k \<inter> l) = {}" unfolding interior_inter apply(rule q')
  4413       using as unfolding r_def by auto
  4414     have "interior m = {}" unfolding subset_empty[THEN sym] unfolding *[THEN sym]
  4415       apply(rule subset_interior) using kl(1)[OF as(4,1)] kl(1)[OF as(5,2)] by auto
  4416     thus "content m *\<^sub>R f x = 0" unfolding uv content_eq_0_interior[THEN sym] by auto 
  4417   qed(insert qq, auto)
  4418 
  4419   hence **:"norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + setsum (setsum (\<lambda>(x, k). content k *\<^sub>R f x) \<circ> qq) r -
  4420     integral {a..b} f) < e" apply(subst(asm) setsum_reindex_nonzero) apply fact
  4421     apply(rule setsum_0',rule) unfolding split_paired_all split_conv defer apply assumption
  4422   proof- fix k l x m assume as:"k\<in>r" "l\<in>r" "k\<noteq>l" "qq k = qq l" "(x,m)\<in>qq k"
  4423     note tagged_division_ofD(6)[OF qq[THEN conjunct1]] from this[OF as(1)] this[OF as(2)] 
  4424     show "content m *\<^sub>R f x = 0"  using as(3) unfolding as by auto qed
  4425   
  4426   have *:"\<And>ir ip i cr cp. norm((cp + cr) - i) < e \<Longrightarrow> norm(cr - ir) < k \<Longrightarrow> 
  4427     ip + ir = i \<Longrightarrow> norm(cp - ip) \<le> e + k" 
  4428   proof- case goal1 thus ?case  using norm_triangle_le[of "cp + cr - i" "- (cr - ir)"]  
  4429       unfolding goal1(3)[THEN sym] norm_minus_cancel by(auto simp add:algebra_simps) qed
  4430   
  4431   have "?x =  norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p. integral k f))"
  4432     unfolding split_def setsum_subtractf ..
  4433   also have "... \<le> e + k" apply(rule *[OF **, where ir="setsum (\<lambda>k. integral k f) r"])
  4434   proof- case goal2 have *:"(\<Sum>(x, k)\<in>p. integral k f) = (\<Sum>k\<in>snd ` p. integral k f)"
  4435       apply(subst setsum_reindex_nonzero) apply fact
  4436       unfolding split_paired_all snd_conv split_def o_def
  4437     proof- fix x l y m assume as:"(x,l)\<in>p" "(y,m)\<in>p" "(x,l)\<noteq>(y,m)" "l = m"
  4438       from p'(4)[OF as(1)] guess u v apply-by(erule exE)+ note uv=this
  4439       show "integral l f = 0" unfolding uv apply(rule integral_unique)
  4440         apply(rule has_integral_null) unfolding content_eq_0_interior
  4441         using p'(5)[OF as(1-3)] unfolding uv as(4)[THEN sym] by auto
  4442     qed auto 
  4443     show ?case unfolding integral_combine_division_topdown[OF assms(1) q(2)] * r_def
  4444       apply(rule setsum_Un_disjoint'[THEN sym]) using q(1) q'(1) p'(1) by auto
  4445   next  case goal1 have *:"k * real (card r) / (1 + real (card r)) < k" using k by(auto simp add:field_simps)
  4446     show ?case apply(rule le_less_trans[of _ "setsum (\<lambda>x. k / (real (card r) + 1)) r"])
  4447       unfolding setsum_subtractf[THEN sym] apply(rule setsum_norm_le,fact)
  4448       apply rule apply(drule qq) defer unfolding divide_inverse setsum_left_distrib[THEN sym]
  4449       unfolding divide_inverse[THEN sym] using * by(auto simp add:field_simps real_eq_of_nat)
  4450   qed finally show "?x \<le> e + k" . qed
  4451 
  4452 lemma henstock_lemma_part2: fixes f::"'m::ordered_euclidean_space \<Rightarrow> 'n::ordered_euclidean_space"
  4453   assumes "f integrable_on {a..b}" "0 < e" "gauge d"
  4454   "\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p -
  4455           integral({a..b}) f) < e"    "p tagged_partial_division_of {a..b}" "d fine p"
  4456   shows "setsum (\<lambda>(x,k). norm(content k *\<^sub>R f x - integral k f)) p \<le> 2 * real (DIM('n)) * e"
  4457   unfolding split_def apply(rule setsum_norm_allsubsets_bound) defer 
  4458   apply(rule henstock_lemma_part1[unfolded split_def,OF assms(1-3)])
  4459   apply safe apply(rule assms[rule_format,unfolded split_def]) defer
  4460   apply(rule tagged_partial_division_subset,rule assms,assumption)
  4461   apply(rule fine_subset,assumption,rule assms) using assms(5) by auto
  4462   
  4463 lemma henstock_lemma: fixes f::"'m::ordered_euclidean_space \<Rightarrow> 'n::ordered_euclidean_space"
  4464   assumes "f integrable_on {a..b}" "e>0"
  4465   obtains d where "gauge d"
  4466   "\<forall>p. p tagged_partial_division_of {a..b} \<and> d fine p
  4467   \<longrightarrow> setsum (\<lambda>(x,k). norm(content k *\<^sub>R f x - integral k f)) p < e"
  4468 proof- have *:"e / (2 * (real DIM('n) + 1)) > 0" apply(rule divide_pos_pos) using assms(2) by auto
  4469   from integrable_integral[OF assms(1),unfolded has_integral[of f],rule_format,OF this]
  4470   guess d .. note d = conjunctD2[OF this] show thesis apply(rule that,rule d)
  4471   proof safe case goal1 note * = henstock_lemma_part2[OF assms(1) * d this]
  4472     show ?case apply(rule le_less_trans[OF *]) using `e>0` by(auto simp add:field_simps) qed qed
  4473 
  4474 subsection {* monotone convergence (bounded interval first). *}
  4475 
  4476 lemma monotone_convergence_interval: fixes f::"nat \<Rightarrow> 'n::ordered_euclidean_space \<Rightarrow> real"
  4477   assumes "\<forall>k. (f k) integrable_on {a..b}"
  4478   "\<forall>k. \<forall>x\<in>{a..b}.(f k x) \<le> (f (Suc k) x)"
  4479   "\<forall>x\<in>{a..b}. ((\<lambda>k. f k x) ---> g x) sequentially"
  4480   "bounded {integral {a..b} (f k) | k . k \<in> UNIV}"
  4481   shows "g integrable_on {a..b} \<and> ((\<lambda>k. integral ({a..b}) (f k)) ---> integral ({a..b}) g) sequentially"
  4482 proof(case_tac[!] "content {a..b} = 0") assume as:"content {a..b} = 0"
  4483   show ?thesis using integrable_on_null[OF as] unfolding integral_null[OF as] using Lim_const by auto
  4484 next assume ab:"content {a..b} \<noteq> 0"
  4485   have fg:"\<forall>x\<in>{a..b}. \<forall> k. (f k x) $$ 0 \<le> (g x) $$ 0"
  4486   proof safe case goal1 note assms(3)[rule_format,OF this]
  4487     note * = Lim_component_ge[OF this trivial_limit_sequentially]
  4488     show ?case apply(rule *) unfolding eventually_sequentially
  4489       apply(rule_tac x=k in exI) apply- apply(rule transitive_stepwise_le)
  4490       using assms(2)[rule_format,OF goal1] by auto qed
  4491   have "\<exists>i. ((\<lambda>k. integral ({a..b}) (f k)) ---> i) sequentially"
  4492     apply(rule bounded_increasing_convergent) defer
  4493     apply rule apply(rule integral_le) apply safe
  4494     apply(rule assms(1-2)[rule_format])+ using assms(4) by auto
  4495   then guess i .. note i=this
  4496   have i':"\<And>k. (integral({a..b}) (f k)) \<le> i$$0"
  4497     apply(rule Lim_component_ge,rule i) apply(rule trivial_limit_sequentially)
  4498     unfolding eventually_sequentially apply(rule_tac x=k in exI)
  4499     apply(rule transitive_stepwise_le) prefer 3 unfolding Eucl_real_simps apply(rule integral_le)
  4500     apply(rule assms(1-2)[rule_format])+ using assms(2) by auto
  4501 
  4502   have "(g has_integral i) {a..b}" unfolding has_integral
  4503   proof safe case goal1 note e=this
  4504     hence "\<forall>k. (\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow>
  4505              norm ((\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f k x) - integral {a..b} (f k)) < e / 2 ^ (k + 2)))"
  4506       apply-apply(rule,rule assms(1)[unfolded has_integral_integral has_integral,rule_format])
  4507       apply(rule divide_pos_pos) by auto
  4508     from choice[OF this] guess c .. note c=conjunctD2[OF this[rule_format],rule_format]
  4509 
  4510     have "\<exists>r. \<forall>k\<ge>r. 0 \<le> i$$0 - (integral {a..b} (f k)) \<and> i$$0 - (integral {a..b} (f k)) < e / 4"
  4511     proof- case goal1 have "e/4 > 0" using e by auto
  4512       from i[unfolded Lim_sequentially,rule_format,OF this] guess r ..
  4513       thus ?case apply(rule_tac x=r in exI) apply rule
  4514         apply(erule_tac x=k in allE)
  4515       proof- case goal1 thus ?case using i'[of k] unfolding dist_real_def by auto qed qed
  4516     then guess r .. note r=conjunctD2[OF this[rule_format]]
  4517 
  4518     have "\<forall>x\<in>{a..b}. \<exists>n\<ge>r. \<forall>k\<ge>n. 0 \<le> (g x)$$0 - (f k x)$$0 \<and>
  4519            (g x)$$0 - (f k x)$$0 < e / (4 * content({a..b}))"
  4520     proof case goal1 have "e / (4 * content {a..b}) > 0" apply(rule divide_pos_pos,fact)
  4521         using ab content_pos_le[of a b] by auto
  4522       from assms(3)[rule_format,OF goal1,unfolded Lim_sequentially,rule_format,OF this]
  4523       guess n .. note n=this
  4524       thus ?case apply(rule_tac x="n + r" in exI) apply safe apply(erule_tac[2-3] x=k in allE)
  4525         unfolding dist_real_def using fg[rule_format,OF goal1] by(auto simp add:field_simps) qed
  4526     from bchoice[OF this] guess m .. note m=conjunctD2[OF this[rule_format],rule_format]
  4527     def d \<equiv> "\<lambda>x. c (m x) x" 
  4528 
  4529     show ?case apply(rule_tac x=d in exI)
  4530     proof safe show "gauge d" using c(1) unfolding gauge_def d_def by auto
  4531     next fix p assume p:"p tagged_division_of {a..b}" "d fine p"
  4532       note p'=tagged_division_ofD[OF p(1)]
  4533       have "\<exists>a. \<forall>x\<in>p. m (fst x) \<le> a" by(rule upper_bound_finite_set,fact)
  4534       then guess s .. note s=this
  4535       have *:"\<forall>a b c d. norm(a - b) \<le> e / 4 \<and> norm(b - c) < e / 2 \<and>
  4536             norm(c - d) < e / 4 \<longrightarrow> norm(a - d) < e" 
  4537       proof safe case goal1 thus ?case using norm_triangle_lt[of "a - b" "b - c" "3* e/4"]
  4538           norm_triangle_lt[of "a - b + (b - c)" "c - d" e] unfolding norm_minus_cancel
  4539           by(auto simp add:algebra_simps) qed
  4540       show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - i) < e" apply(rule *[rule_format,where
  4541           b="\<Sum>(x, k)\<in>p. content k *\<^sub>R f (m x) x" and c="\<Sum>(x, k)\<in>p. integral k (f (m x))"])
  4542       proof safe case goal1
  4543          show ?case apply(rule order_trans[of _ "\<Sum>(x, k)\<in>p. content k * (e / (4 * content {a..b}))"])
  4544            unfolding setsum_subtractf[THEN sym] apply(rule order_trans,rule setsum_norm[OF p'(1)])
  4545            apply(rule setsum_mono) unfolding split_paired_all split_conv
  4546            unfolding split_def setsum_left_distrib[THEN sym] scaleR.diff_right[THEN sym]
  4547            unfolding additive_content_tagged_division[OF p(1), unfolded split_def]
  4548          proof- fix x k assume xk:"(x,k) \<in> p" hence x:"x\<in>{a..b}" using p'(2-3)[OF xk] by auto
  4549            from p'(4)[OF xk] guess u v apply-by(erule exE)+ note uv=this
  4550            show " norm (content k *\<^sub>R (g x - f (m x) x)) \<le> content k * (e / (4 * content {a..b}))"
  4551              unfolding norm_scaleR uv unfolding abs_of_nonneg[OF content_pos_le] 
  4552              apply(rule mult_left_mono) using m(2)[OF x,of "m x"] by auto
  4553          qed(insert ab,auto)
  4554          
  4555        next case goal2 show ?case apply(rule le_less_trans[of _ "norm (\<Sum>j = 0..s.
  4556            \<Sum>(x, k)\<in>{xk\<in>p. m (fst xk) = j}. content k *\<^sub>R f (m x) x - integral k (f (m x)))"])
  4557            apply(subst setsum_group) apply fact apply(rule finite_atLeastAtMost) defer
  4558            apply(subst split_def)+ unfolding setsum_subtractf apply rule
  4559          proof- show "norm (\<Sum>j = 0..s. \<Sum>(x, k)\<in>{xk \<in> p.
  4560              m (fst xk) = j}. content k *\<^sub>R f (m x) x - integral k (f (m x))) < e / 2"
  4561              apply(rule le_less_trans[of _ "setsum (\<lambda>i. e / 2^(i+2)) {0..s}"])
  4562              apply(rule setsum_norm_le[OF finite_atLeastAtMost])
  4563            proof show "(\<Sum>i = 0..s. e / 2 ^ (i + 2)) < e / 2"
  4564                unfolding power_add divide_inverse inverse_mult_distrib
  4565                unfolding setsum_right_distrib[THEN sym] setsum_left_distrib[THEN sym]
  4566                unfolding power_inverse sum_gp apply(rule mult_strict_left_mono[OF _ e])
  4567                unfolding power2_eq_square by auto
  4568              fix t assume "t\<in>{0..s}"
  4569              show "norm (\<Sum>(x, k)\<in>{xk \<in> p. m (fst xk) = t}. content k *\<^sub>R f (m x) x -
  4570                integral k (f (m x))) \<le> e / 2 ^ (t + 2)"apply(rule order_trans[of _
  4571                "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f t x - integral k (f t)) {xk \<in> p. m (fst xk) = t})"])
  4572                apply(rule eq_refl) apply(rule arg_cong[where f=norm]) apply(rule setsum_cong2) defer
  4573                apply(rule henstock_lemma_part1) apply(rule assms(1)[rule_format])
  4574                apply(rule divide_pos_pos,rule e) defer  apply safe apply(rule c)+
  4575                apply rule apply assumption+ apply(rule tagged_partial_division_subset[of p])
  4576                apply(rule p(1)[unfolded tagged_division_of_def,THEN conjunct1]) defer
  4577                unfolding fine_def apply safe apply(drule p(2)[unfolded fine_def,rule_format])
  4578                unfolding d_def by auto qed
  4579          qed(insert s, auto)
  4580 
  4581        next case goal3
  4582          note comb = integral_combine_tagged_division_topdown[OF assms(1)[rule_format] p(1)]
  4583          have *:"\<And>sr sx ss ks kr::real. kr = sr \<longrightarrow> ks = ss \<longrightarrow> ks \<le> i \<and> sr \<le> sx \<and> sx \<le> ss \<and> 0 \<le> i$$0 - kr$$0
  4584            \<and> i$$0 - kr$$0 < e/4 \<longrightarrow> abs(sx - i) < e/4" by auto 
  4585          show ?case unfolding real_norm_def apply(rule *[rule_format],safe)
  4586            apply(rule comb[of r],rule comb[of s]) apply(rule i'[unfolded Eucl_real_simps]) 
  4587            apply(rule_tac[1-2] setsum_mono) unfolding split_paired_all split_conv
  4588            apply(rule_tac[1-2] integral_le[OF ])
  4589          proof safe show "0 \<le> i$$0 - (integral {a..b} (f r))$$0" using r(1) by auto
  4590            show "i$$0 - (integral {a..b} (f r))$$0 < e / 4" using r(2) by auto
  4591            fix x k assume xk:"(x,k)\<in>p" from p'(4)[OF this] guess u v apply-by(erule exE)+ note uv=this
  4592            show "f r integrable_on k" "f s integrable_on k" "f (m x) integrable_on k" "f (m x) integrable_on k" 
  4593              unfolding uv apply(rule_tac[!] integrable_on_subinterval[OF assms(1)[rule_format]])
  4594              using p'(3)[OF xk] unfolding uv by auto 
  4595            fix y assume "y\<in>k" hence "y\<in>{a..b}" using p'(3)[OF xk] by auto
  4596            hence *:"\<And>m. \<forall>n\<ge>m. (f m y) \<le> (f n y)" apply-apply(rule transitive_stepwise_le) using assms(2) by auto
  4597            show "(f r y) \<le> (f (m x) y)" "(f (m x) y) \<le> (f s y)"
  4598              apply(rule_tac[!] *[rule_format]) using s[rule_format,OF xk] m(1)[of x] p'(2-3)[OF xk] by auto
  4599          qed qed qed qed note * = this 
  4600 
  4601    have "integral {a..b} g = i" apply(rule integral_unique) using * .
  4602    thus ?thesis using i * by auto qed
  4603 
  4604 lemma monotone_convergence_increasing: fixes f::"nat \<Rightarrow> 'n::ordered_euclidean_space \<Rightarrow> real"
  4605   assumes "\<forall>k. (f k) integrable_on s"  "\<forall>k. \<forall>x\<in>s. (f k x) \<le> (f (Suc k) x)"
  4606   "\<forall>x\<in>s. ((\<lambda>k. f k x) ---> g x) sequentially" "bounded {integral s (f k)| k. True}"
  4607   shows "g integrable_on s \<and> ((\<lambda>k. integral s (f k)) ---> integral s g) sequentially"
  4608 proof- have lem:"\<And>f::nat \<Rightarrow> 'n::ordered_euclidean_space \<Rightarrow> real. \<And> g s. \<forall>k.\<forall>x\<in>s. 0 \<le> (f k x) \<Longrightarrow> \<forall>k. (f k) integrable_on s \<Longrightarrow>
  4609     \<forall>k. \<forall>x\<in>s. (f k x) \<le> (f (Suc k) x) \<Longrightarrow> \<forall>x\<in>s. ((\<lambda>k. f k x) ---> g x) sequentially  \<Longrightarrow>
  4610     bounded {integral s (f k)| k. True} \<Longrightarrow> g integrable_on s \<and> ((\<lambda>k. integral s (f k)) ---> integral s g) sequentially"
  4611   proof- case goal1 note assms=this[rule_format]
  4612     have "\<forall>x\<in>s. \<forall>k. (f k x)$$0 \<le> (g x)$$0" apply safe apply(rule Lim_component_ge)
  4613       apply(rule goal1(4)[rule_format],assumption) apply(rule trivial_limit_sequentially)
  4614       unfolding eventually_sequentially apply(rule_tac x=k in exI)
  4615       apply(rule transitive_stepwise_le) using goal1(3) by auto note fg=this[rule_format]
  4616 
  4617     have "\<exists>i. ((\<lambda>k. integral s (f k)) ---> i) sequentially" apply(rule bounded_increasing_convergent)
  4618       apply(rule goal1(5)) apply(rule,rule integral_le) apply(rule goal1(2)[rule_format])+
  4619       using goal1(3) by auto then guess i .. note i=this
  4620     have "\<And>k. \<forall>x\<in>s. \<forall>n\<ge>k. f k x \<le> f n x" apply(rule,rule transitive_stepwise_le) using goal1(3) by auto
  4621     hence i':"\<forall>k. (integral s (f k))$$0 \<le> i$$0" apply-apply(rule,rule Lim_component_ge)
  4622       apply(rule i,rule trivial_limit_sequentially) unfolding eventually_sequentially
  4623       apply(rule_tac x=k in exI,safe) apply(rule integral_component_le)
  4624       apply(rule goal1(2)[rule_format])+ by auto 
  4625 
  4626     note int = assms(2)[unfolded integrable_alt[of _ s],THEN conjunct1,rule_format]
  4627     have ifif:"\<And>k t. (\<lambda>x. if x \<in> t then if x \<in> s then f k x else 0 else 0) =
  4628       (\<lambda>x. if x \<in> t\<inter>s then f k x else 0)" apply(rule ext) by auto
  4629     have int':"\<And>k a b. f k integrable_on {a..b} \<inter> s" apply(subst integrable_restrict_univ[THEN sym])
  4630       apply(subst ifif[THEN sym]) apply(subst integrable_restrict_univ) using int .
  4631     have "\<And>a b. (\<lambda>x. if x \<in> s then g x else 0) integrable_on {a..b} \<and>
  4632       ((\<lambda>k. integral {a..b} (\<lambda>x. if x \<in> s then f k x else 0)) --->
  4633       integral {a..b} (\<lambda>x. if x \<in> s then g x else 0)) sequentially"
  4634     proof(rule monotone_convergence_interval,safe)
  4635       case goal1 show ?case using int .
  4636     next case goal2 thus ?case apply-apply(cases "x\<in>s") using assms(3) by auto
  4637     next case goal3 thus ?case apply-apply(cases "x\<in>s") using assms(4) by auto
  4638     next case goal4 note * = integral_nonneg
  4639       have "\<And>k. norm (integral {a..b} (\<lambda>x. if x \<in> s then f k x else 0)) \<le> norm (integral s (f k))"
  4640         unfolding real_norm_def apply(subst abs_of_nonneg) apply(rule *[OF int])
  4641         apply(safe,case_tac "x\<in>s") apply(drule assms(1)) prefer 3
  4642         apply(subst abs_of_nonneg) apply(rule *[OF assms(2) goal1(1)[THEN spec]])
  4643         apply(subst integral_restrict_univ[THEN sym,OF int]) 
  4644         unfolding ifif unfolding integral_restrict_univ[OF int']
  4645         apply(rule integral_subset_le[OF _ int' assms(2)]) using assms(1) by auto
  4646       thus ?case using assms(5) unfolding bounded_iff apply safe
  4647         apply(rule_tac x=aa in exI,safe) apply(erule_tac x="integral s (f k)" in ballE)
  4648         apply(rule order_trans) apply assumption by auto qed note g = conjunctD2[OF this]
  4649 
  4650     have "(g has_integral i) s" unfolding has_integral_alt' apply safe apply(rule g(1))
  4651     proof- case goal1 hence "e/4>0" by auto
  4652       from i[unfolded Lim_sequentially,rule_format,OF this] guess N .. note N=this
  4653       note assms(2)[of N,unfolded has_integral_integral has_integral_alt'[of "f N"]]
  4654       from this[THEN conjunct2,rule_format,OF `e/4>0`] guess B .. note B=conjunctD2[OF this]
  4655       show ?case apply(rule,rule,rule B,safe)
  4656       proof- fix a b::"'n::ordered_euclidean_space" assume ab:"ball 0 B \<subseteq> {a..b}"
  4657         from `e>0` have "e/2>0" by auto
  4658         from g(2)[unfolded Lim_sequentially,of a b,rule_format,OF this] guess M .. note M=this
  4659         have **:"norm (integral {a..b} (\<lambda>x. if x \<in> s then f N x else 0) - i) < e/2"
  4660           apply(rule norm_triangle_half_l) using B(2)[rule_format,OF ab] N[rule_format,of N]
  4661           unfolding dist_norm apply-defer apply(subst norm_minus_commute) by auto
  4662         have *:"\<And>f1 f2 g. abs(f1 - i) < e / 2 \<longrightarrow> abs(f2 - g) < e / 2 \<longrightarrow> f1 \<le> f2 \<longrightarrow> f2 \<le> i
  4663           \<longrightarrow> abs(g - i) < e" unfolding Eucl_real_simps by arith
  4664         show "norm (integral {a..b} (\<lambda>x. if x \<in> s then g x else 0) - i) < e" 
  4665           unfolding real_norm_def apply(rule *[rule_format])
  4666           apply(rule **[unfolded real_norm_def])
  4667           apply(rule M[rule_format,of "M + N",unfolded dist_real_def]) apply(rule le_add1)
  4668           apply(rule integral_le[OF int int]) defer
  4669           apply(rule order_trans[OF _ i'[rule_format,of "M + N",unfolded Eucl_real_simps]])
  4670         proof safe case goal2 have "\<And>m. x\<in>s \<Longrightarrow> \<forall>n\<ge>m. (f m x)$$0 \<le> (f n x)$$0"
  4671             apply(rule transitive_stepwise_le) using assms(3) by auto thus ?case by auto
  4672         next case goal1 show ?case apply(subst integral_restrict_univ[THEN sym,OF int]) 
  4673             unfolding ifif integral_restrict_univ[OF int']
  4674             apply(rule integral_subset_le[OF _ int']) using assms by auto
  4675         qed qed qed 
  4676     thus ?case apply safe defer apply(drule integral_unique) using i by auto qed
  4677 
  4678   have sub:"\<And>k. integral s (\<lambda>x. f k x - f 0 x) = integral s (f k) - integral s (f 0)"
  4679     apply(subst integral_sub) apply(rule assms(1)[rule_format])+ by rule
  4680   have "\<And>x m. x\<in>s \<Longrightarrow> \<forall>n\<ge>m. (f m x) \<le> (f n x)" apply(rule transitive_stepwise_le)
  4681     using assms(2) by auto note * = this[rule_format]
  4682   have "(\<lambda>x. g x - f 0 x) integrable_on s \<and>((\<lambda>k. integral s (\<lambda>x. f (Suc k) x - f 0 x)) --->
  4683       integral s (\<lambda>x. g x - f 0 x)) sequentially" apply(rule lem,safe)
  4684   proof- case goal1 thus ?case using *[of x 0 "Suc k"] by auto
  4685   next case goal2 thus ?case apply(rule integrable_sub) using assms(1) by auto
  4686   next case goal3 thus ?case using *[of x "Suc k" "Suc (Suc k)"] by auto
  4687   next case goal4 thus ?case apply-apply(rule Lim_sub) 
  4688       using seq_offset[OF assms(3)[rule_format],of x 1] by auto
  4689   next case goal5 thus ?case using assms(4) unfolding bounded_iff
  4690       apply safe apply(rule_tac x="a + norm (integral s (\<lambda>x. f 0 x))" in exI)
  4691       apply safe apply(erule_tac x="integral s (\<lambda>x. f (Suc k) x)" in ballE) unfolding sub
  4692       apply(rule order_trans[OF norm_triangle_ineq4]) by auto qed
  4693   note conjunctD2[OF this] note Lim_add[OF this(2) Lim_const[of "integral s (f 0)"]]
  4694     integrable_add[OF this(1) assms(1)[rule_format,of 0]]
  4695   thus ?thesis unfolding sub apply-apply rule defer apply(subst(asm) integral_sub)
  4696     using assms(1) apply auto apply(rule seq_offset_rev[where k=1]) by auto qed
  4697 
  4698 lemma monotone_convergence_decreasing: fixes f::"nat \<Rightarrow> 'n::ordered_euclidean_space \<Rightarrow> real"
  4699   assumes "\<forall>k. (f k) integrable_on s"  "\<forall>k. \<forall>x\<in>s. (f (Suc k) x) \<le> (f k x)"
  4700   "\<forall>x\<in>s. ((\<lambda>k. f k x) ---> g x) sequentially" "bounded {integral s (f k)| k. True}"
  4701   shows "g integrable_on s \<and> ((\<lambda>k. integral s (f k)) ---> integral s g) sequentially"
  4702 proof- note assm = assms[rule_format]
  4703   have *:"{integral s (\<lambda>x. - f k x) |k. True} = op *\<^sub>R -1 ` {integral s (f k)| k. True}"
  4704     apply safe unfolding image_iff apply(rule_tac x="integral s (f k)" in bexI) prefer 3
  4705     apply(rule_tac x=k in exI) unfolding integral_neg[OF assm(1)] by auto
  4706   have "(\<lambda>x. - g x) integrable_on s \<and> ((\<lambda>k. integral s (\<lambda>x. - f k x))
  4707     ---> integral s (\<lambda>x. - g x))  sequentially" apply(rule monotone_convergence_increasing)
  4708     apply(safe,rule integrable_neg) apply(rule assm) defer apply(rule Lim_neg)
  4709     apply(rule assm,assumption) unfolding * apply(rule bounded_scaling) using assm by auto
  4710   note * = conjunctD2[OF this]
  4711   show ?thesis apply rule using integrable_neg[OF *(1)] defer
  4712     using Lim_neg[OF *(2)] apply- unfolding integral_neg[OF assm(1)]
  4713     unfolding integral_neg[OF *(1),THEN sym] by auto qed
  4714 
  4715 subsection {* absolute integrability (this is the same as Lebesgue integrability). *}
  4716 
  4717 definition absolutely_integrable_on (infixr "absolutely'_integrable'_on" 46) where
  4718   "f absolutely_integrable_on s \<longleftrightarrow> f integrable_on s \<and> (\<lambda>x. (norm(f x))) integrable_on s"
  4719 
  4720 lemma absolutely_integrable_onI[intro?]:
  4721   "f integrable_on s \<Longrightarrow> (\<lambda>x. (norm(f x))) integrable_on s \<Longrightarrow> f absolutely_integrable_on s"
  4722   unfolding absolutely_integrable_on_def by auto
  4723 
  4724 lemma absolutely_integrable_onD[dest]: assumes "f absolutely_integrable_on s"
  4725   shows "f integrable_on s" "(\<lambda>x. (norm(f x))) integrable_on s"
  4726   using assms unfolding absolutely_integrable_on_def by auto
  4727 
  4728 (*lemma absolutely_integrable_on_trans[simp]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real" shows
  4729   "(vec1 o f) absolutely_integrable_on s \<longleftrightarrow> f absolutely_integrable_on s"
  4730   unfolding absolutely_integrable_on_def o_def by auto*)
  4731 
  4732 lemma integral_norm_bound_integral: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4733   assumes "f integrable_on s" "g integrable_on s" "\<forall>x\<in>s. norm(f x) \<le> g x"
  4734   shows "norm(integral s f) \<le> (integral s g)"
  4735 proof- have *:"\<And>x y. (\<forall>e::real. 0 < e \<longrightarrow> x < y + e) \<longrightarrow> x \<le> y" apply(safe,rule ccontr)
  4736     apply(erule_tac x="x - y" in allE) by auto
  4737   have "\<And>e sg dsa dia ig. norm(sg) \<le> dsa \<longrightarrow> abs(dsa - dia) < e / 2 \<longrightarrow> norm(sg - ig) < e / 2
  4738     \<longrightarrow> norm(ig) < dia + e" 
  4739   proof safe case goal1 show ?case apply(rule le_less_trans[OF norm_triangle_sub[of ig sg]])
  4740       apply(subst real_sum_of_halves[of e,THEN sym]) unfolding add_assoc[symmetric]
  4741       apply(rule add_le_less_mono) defer apply(subst norm_minus_commute,rule goal1)
  4742       apply(rule order_trans[OF goal1(1)]) using goal1(2) by arith
  4743   qed note norm=this[rule_format]
  4744   have lem:"\<And>f::'n::ordered_euclidean_space \<Rightarrow> 'a. \<And> g a b. f integrable_on {a..b} \<Longrightarrow> g integrable_on {a..b} \<Longrightarrow>
  4745     \<forall>x\<in>{a..b}. norm(f x) \<le> (g x) \<Longrightarrow> norm(integral({a..b}) f) \<le> (integral({a..b}) g)"
  4746   proof(rule *[rule_format]) case goal1 hence *:"e/2>0" by auto
  4747     from integrable_integral[OF goal1(1),unfolded has_integral[of f],rule_format,OF *]
  4748     guess d1 .. note d1 = conjunctD2[OF this,rule_format]
  4749     from integrable_integral[OF goal1(2),unfolded has_integral[of g],rule_format,OF *]
  4750     guess d2 .. note d2 = conjunctD2[OF this,rule_format]
  4751     note gauge_inter[OF d1(1) d2(1)]
  4752     from fine_division_exists[OF this, of a b] guess p . note p=this
  4753     show ?case apply(rule norm) defer
  4754       apply(rule d2(2)[OF conjI[OF p(1)],unfolded real_norm_def]) defer
  4755       apply(rule d1(2)[OF conjI[OF p(1)]]) defer apply(rule setsum_norm_le)
  4756     proof safe fix x k assume "(x,k)\<in>p" note as = tagged_division_ofD(2-4)[OF p(1) this]
  4757       from this(3) guess u v apply-by(erule exE)+ note uv=this
  4758       show "norm (content k *\<^sub>R f x) \<le> content k *\<^sub>R g x" unfolding uv norm_scaleR
  4759         unfolding abs_of_nonneg[OF content_pos_le] real_scaleR_def
  4760         apply(rule mult_left_mono) using goal1(3) as by auto
  4761     qed(insert p[unfolded fine_inter],auto) qed
  4762 
  4763   { presume "\<And>e. 0 < e \<Longrightarrow> norm (integral s f) < integral s g + e" 
  4764     thus ?thesis apply-apply(rule *[rule_format]) by auto }
  4765   fix e::real assume "e>0" hence e:"e/2 > 0" by auto
  4766   note assms(1)[unfolded integrable_alt[of f]] note f=this[THEN conjunct1,rule_format]
  4767   note assms(2)[unfolded integrable_alt[of g]] note g=this[THEN conjunct1,rule_format]
  4768   from integrable_integral[OF assms(1),unfolded has_integral'[of f],rule_format,OF e]
  4769   guess B1 .. note B1=conjunctD2[OF this[rule_format],rule_format]
  4770   from integrable_integral[OF assms(2),unfolded has_integral'[of g],rule_format,OF e]
  4771   guess B2 .. note B2=conjunctD2[OF this[rule_format],rule_format]
  4772   from bounded_subset_closed_interval[OF bounded_ball, of "0::'n::ordered_euclidean_space" "max B1 B2"]
  4773   guess a b apply-by(erule exE)+ note ab=this[unfolded ball_max_Un]
  4774 
  4775   have "ball 0 B1 \<subseteq> {a..b}" using ab by auto
  4776   from B1(2)[OF this] guess z .. note z=conjunctD2[OF this]
  4777   have "ball 0 B2 \<subseteq> {a..b}" using ab by auto
  4778   from B2(2)[OF this] guess w .. note w=conjunctD2[OF this]
  4779 
  4780   show "norm (integral s f) < integral s g + e" apply(rule norm)
  4781     apply(rule lem[OF f g, of a b]) unfolding integral_unique[OF z(1)] integral_unique[OF w(1)]
  4782     defer apply(rule w(2)[unfolded real_norm_def],rule z(2))
  4783     apply safe apply(case_tac "x\<in>s") unfolding if_P apply(rule assms(3)[rule_format]) by auto qed
  4784 
  4785 lemma integral_norm_bound_integral_component: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4786   fixes g::"'n => 'b::ordered_euclidean_space"
  4787   assumes "f integrable_on s" "g integrable_on s"  "\<forall>x\<in>s. norm(f x) \<le> (g x)$$k"
  4788   shows "norm(integral s f) \<le> (integral s g)$$k"
  4789 proof- have "norm (integral s f) \<le> integral s ((\<lambda>x. x $$ k) o g)"
  4790     apply(rule integral_norm_bound_integral[OF assms(1)])
  4791     apply(rule integrable_linear[OF assms(2)],rule)
  4792     unfolding o_def by(rule assms)
  4793   thus ?thesis unfolding o_def integral_component_eq[OF assms(2)] . qed
  4794 
  4795 lemma has_integral_norm_bound_integral_component: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4796   fixes g::"'n => 'b::ordered_euclidean_space"
  4797   assumes "(f has_integral i) s" "(g has_integral j) s" "\<forall>x\<in>s. norm(f x) \<le> (g x)$$k"
  4798   shows "norm(i) \<le> j$$k" using integral_norm_bound_integral_component[of f s g k]
  4799   unfolding integral_unique[OF assms(1)] integral_unique[OF assms(2)]
  4800   using assms by auto
  4801 
  4802 lemma absolutely_integrable_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4803   assumes "f absolutely_integrable_on s"
  4804   shows "norm(integral s f) \<le> integral s (\<lambda>x. norm(f x))"
  4805   apply(rule integral_norm_bound_integral) using assms by auto
  4806 
  4807 lemma absolutely_integrable_0[intro]: "(\<lambda>x. 0) absolutely_integrable_on s"
  4808   unfolding absolutely_integrable_on_def by auto
  4809 
  4810 lemma absolutely_integrable_cmul[intro]:
  4811  "f absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. c *\<^sub>R f x) absolutely_integrable_on s"
  4812   unfolding absolutely_integrable_on_def using integrable_cmul[of f s c]
  4813   using integrable_cmul[of "\<lambda>x. norm (f x)" s "abs c"] by auto
  4814 
  4815 lemma absolutely_integrable_neg[intro]:
  4816  "f absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. -f(x)) absolutely_integrable_on s"
  4817   apply(drule absolutely_integrable_cmul[where c="-1"]) by auto
  4818 
  4819 lemma absolutely_integrable_norm[intro]:
  4820  "f absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. norm(f x)) absolutely_integrable_on s"
  4821   unfolding absolutely_integrable_on_def by auto
  4822 
  4823 lemma absolutely_integrable_abs[intro]:
  4824  "f absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. abs(f x::real)) absolutely_integrable_on s"
  4825   apply(drule absolutely_integrable_norm) unfolding real_norm_def .
  4826 
  4827 lemma absolutely_integrable_on_subinterval: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" shows
  4828   "f absolutely_integrable_on s \<Longrightarrow> {a..b} \<subseteq> s \<Longrightarrow> f absolutely_integrable_on {a..b}" 
  4829   unfolding absolutely_integrable_on_def by(meson integrable_on_subinterval)
  4830 
  4831 lemma absolutely_integrable_bounded_variation: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4832   assumes "f absolutely_integrable_on UNIV"
  4833   obtains B where "\<forall>d. d division_of (\<Union>d) \<longrightarrow> setsum (\<lambda>k. norm(integral k f)) d \<le> B"
  4834   apply(rule that[of "integral UNIV (\<lambda>x. norm (f x))"])
  4835 proof safe case goal1 note d = division_ofD[OF this(2)]
  4836   have "(\<Sum>k\<in>d. norm (integral k f)) \<le> (\<Sum>i\<in>d. integral i (\<lambda>x. norm (f x)))"
  4837     apply(rule setsum_mono,rule absolutely_integrable_le) apply(drule d(4),safe)
  4838     apply(rule absolutely_integrable_on_subinterval[OF assms]) by auto
  4839   also have "... \<le> integral (\<Union>d) (\<lambda>x. norm (f x))"
  4840     apply(subst integral_combine_division_topdown[OF _ goal1(2)])
  4841     using integrable_on_subdivision[OF goal1(2)] using assms by auto
  4842   also have "... \<le> integral UNIV (\<lambda>x. norm (f x))"
  4843     apply(rule integral_subset_le) 
  4844     using integrable_on_subdivision[OF goal1(2)] using assms by auto
  4845   finally show ?case . qed
  4846 
  4847 lemma helplemma:
  4848   assumes "setsum (\<lambda>x. norm(f x - g x)) s < e" "finite s"
  4849   shows "abs(setsum (\<lambda>x. norm(f x)) s - setsum (\<lambda>x. norm(g x)) s) < e"
  4850   unfolding setsum_subtractf[THEN sym] apply(rule le_less_trans[OF setsum_abs])
  4851   apply(rule le_less_trans[OF _ assms(1)]) apply(rule setsum_mono)
  4852   using norm_triangle_ineq3 .
  4853 
  4854 lemma bounded_variation_absolutely_integrable_interval:
  4855   fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space" assumes "f integrable_on {a..b}"
  4856   "\<forall>d. d division_of {a..b} \<longrightarrow> setsum (\<lambda>k. norm(integral k f)) d \<le> B"
  4857   shows "f absolutely_integrable_on {a..b}"
  4858 proof- let ?S = "(\<lambda>d. setsum (\<lambda>k. norm(integral k f)) d) ` {d. d division_of {a..b} }" def i \<equiv> "Sup ?S"
  4859   have i:"isLub UNIV ?S i" unfolding i_def apply(rule Sup)
  4860     apply(rule elementary_interval) defer apply(rule_tac x=B in exI)
  4861     apply(rule setleI) using assms(2) by auto
  4862   show ?thesis apply(rule,rule assms) apply rule apply(subst has_integral[of _ i])
  4863   proof safe case goal1 hence "i - e / 2 \<notin> Collect (isUb UNIV (setsum (\<lambda>k. norm (integral k f)) `
  4864         {d. d division_of {a..b}}))" using isLub_ubs[OF i,rule_format]
  4865       unfolding setge_def ubs_def by auto 
  4866     hence " \<exists>y. y division_of {a..b} \<and> i - e / 2 < (\<Sum>k\<in>y. norm (integral k f))"
  4867       unfolding mem_Collect_eq isUb_def setle_def by(simp add:not_le) then guess d .. note d=conjunctD2[OF this]
  4868     note d' = division_ofD[OF this(1)]
  4869 
  4870     have "\<forall>x. \<exists>e>0. \<forall>i\<in>d. x \<notin> i \<longrightarrow> ball x e \<inter> i = {}"
  4871     proof case goal1 have "\<exists>da>0. \<forall>xa\<in>\<Union>{i \<in> d. x \<notin> i}. da \<le> dist x xa"
  4872         apply(rule separate_point_closed) apply(rule closed_Union)
  4873         apply(rule finite_subset[OF _ d'(1)]) apply safe apply(drule d'(4)) by auto
  4874       thus ?case apply safe apply(rule_tac x=da in exI,safe)
  4875         apply(erule_tac x=xa in ballE) by auto
  4876     qed from choice[OF this] guess k .. note k=conjunctD2[OF this[rule_format],rule_format]
  4877 
  4878     have "e/2 > 0" using goal1 by auto
  4879     from henstock_lemma[OF assms(1) this] guess g . note g=this[rule_format]
  4880     let ?g = "\<lambda>x. g x \<inter> ball x (k x)"
  4881     show ?case apply(rule_tac x="?g" in exI) apply safe
  4882     proof- show "gauge ?g" using g(1) unfolding gauge_def using k(1) by auto
  4883       fix p assume "p tagged_division_of {a..b}" "?g fine p"
  4884       note p = this(1) conjunctD2[OF this(2)[unfolded fine_inter]]
  4885       note p' = tagged_division_ofD[OF p(1)]
  4886       def p' \<equiv> "{(x,k) | x k. \<exists>i l. x \<in> i \<and> i \<in> d \<and> (x,l) \<in> p \<and> k = i \<inter> l}"
  4887       have gp':"g fine p'" using p(2) unfolding p'_def fine_def by auto
  4888       have p'':"p' tagged_division_of {a..b}" apply(rule tagged_division_ofI)
  4889       proof- show "finite p'" apply(rule finite_subset[of _ "(\<lambda>(k,(x,l)). (x,k \<inter> l))
  4890           ` {(k,xl) | k xl. k \<in> d \<and> xl \<in> p}"]) unfolding p'_def 
  4891           defer apply(rule finite_imageI,rule finite_product_dependent[OF d'(1) p'(1)])
  4892           apply safe unfolding image_iff apply(rule_tac x="(i,x,l)" in bexI) by auto
  4893         fix x k assume "(x,k)\<in>p'"
  4894         hence "\<exists>i l. x \<in> i \<and> i \<in> d \<and> (x, l) \<in> p \<and> k = i \<inter> l" unfolding p'_def by auto
  4895         then guess i l apply-by(erule exE)+ note il=conjunctD4[OF this]
  4896         show "x\<in>k" "k\<subseteq>{a..b}" using p'(2-3)[OF il(3)] il by auto
  4897         show "\<exists>a b. k = {a..b}" unfolding il using p'(4)[OF il(3)] d'(4)[OF il(2)]
  4898           apply safe unfolding inter_interval by auto
  4899       next fix x1 k1 assume "(x1,k1)\<in>p'"
  4900         hence "\<exists>i l. x1 \<in> i \<and> i \<in> d \<and> (x1, l) \<in> p \<and> k1 = i \<inter> l" unfolding p'_def by auto
  4901         then guess i1 l1 apply-by(erule exE)+ note il1=conjunctD4[OF this]
  4902         fix x2 k2 assume "(x2,k2)\<in>p'"
  4903         hence "\<exists>i l. x2 \<in> i \<and> i \<in> d \<and> (x2, l) \<in> p \<and> k2 = i \<inter> l" unfolding p'_def by auto
  4904         then guess i2 l2 apply-by(erule exE)+ note il2=conjunctD4[OF this]
  4905         assume "(x1, k1) \<noteq> (x2, k2)"
  4906         hence "interior(i1) \<inter> interior(i2) = {} \<or> interior(l1) \<inter> interior(l2) = {}"
  4907           using d'(5)[OF il1(2) il2(2)] p'(5)[OF il1(3) il2(3)] unfolding il1 il2 by auto
  4908         thus "interior k1 \<inter> interior k2 = {}" unfolding il1 il2 by auto
  4909       next have *:"\<forall>(x, X) \<in> p'. X \<subseteq> {a..b}" unfolding p'_def using d' by auto
  4910         show "\<Union>{k. \<exists>x. (x, k) \<in> p'} = {a..b}" apply rule apply(rule Union_least)
  4911           unfolding mem_Collect_eq apply(erule exE) apply(drule *[rule_format]) apply safe
  4912         proof- fix y assume y:"y\<in>{a..b}"
  4913           hence "\<exists>x l. (x, l) \<in> p \<and> y\<in>l" unfolding p'(6)[THEN sym] by auto
  4914           then guess x l apply-by(erule exE)+ note xl=conjunctD2[OF this]
  4915           hence "\<exists>k. k\<in>d \<and> y\<in>k" using y unfolding d'(6)[THEN sym] by auto
  4916           then guess i .. note i = conjunctD2[OF this]
  4917           have "x\<in>i" using fineD[OF p(3) xl(1)] using k(2)[OF i(1), of x] using i(2) xl(2) by auto
  4918           thus "y\<in>\<Union>{k. \<exists>x. (x, k) \<in> p'}" unfolding p'_def Union_iff apply(rule_tac x="i \<inter> l" in bexI)
  4919             defer unfolding mem_Collect_eq apply(rule_tac x=x in exI)+ apply(rule_tac x="i\<inter>l" in exI)
  4920             apply safe apply(rule_tac x=i in exI) apply(rule_tac x=l in exI) using i xl by auto 
  4921         qed qed 
  4922 
  4923       hence "(\<Sum>(x, k)\<in>p'. norm (content k *\&l