src/HOL/Multivariate_Analysis/Integration.thy
changeset 35292 e4a431b6d9b7
parent 35291 ead7bfc30b26
child 35540 3d073a3e1c61
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Multivariate_Analysis/Integration.thy	Mon Feb 22 20:41:49 2010 +0100
     1.3 @@ -0,0 +1,3473 @@
     1.4 +
     1.5 +header {* Kurzweil-Henstock gauge integration in many dimensions. *}
     1.6 +(*  Author:                     John Harrison
     1.7 +    Translation from HOL light: Robert Himmelmann, TU Muenchen *)
     1.8 +
     1.9 +theory Integration
    1.10 +  imports Derivative SMT
    1.11 +begin
    1.12 +
    1.13 +declare [[smt_certificates="~~/src/HOL/Multivariate_Analysis/Integration.cert"]]
    1.14 +declare [[smt_record=true]]
    1.15 +declare [[z3_proofs=true]]
    1.16 +
    1.17 +lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
    1.18 +lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
    1.19 +lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
    1.20 +lemma conjunctD5: assumes "a \<and> b \<and> c \<and> d \<and> e" shows a b c d e using assms by auto
    1.21 +
    1.22 +declare smult_conv_scaleR[simp]
    1.23 +
    1.24 +subsection {* Some useful lemmas about intervals. *}
    1.25 +
    1.26 +lemma empty_as_interval: "{} = {1..0::real^'n}"
    1.27 +  apply(rule set_ext,rule) defer unfolding vector_le_def mem_interval
    1.28 +  using UNIV_witness[where 'a='n] apply(erule_tac exE,rule_tac x=x in allE) by auto
    1.29 +
    1.30 +lemma interior_subset_union_intervals: 
    1.31 +  assumes "i = {a..b::real^'n}" "j = {c..d}" "interior j \<noteq> {}" "i \<subseteq> j \<union> s" "interior(i) \<inter> interior(j) = {}"
    1.32 +  shows "interior i \<subseteq> interior s" proof-
    1.33 +  have "{a<..<b} \<inter> {c..d} = {}" using inter_interval_mixed_eq_empty[of c d a b] and assms(3,5)
    1.34 +    unfolding assms(1,2) interior_closed_interval by auto
    1.35 +  moreover have "{a<..<b} \<subseteq> {c..d} \<union> s" apply(rule order_trans,rule interval_open_subset_closed)
    1.36 +    using assms(4) unfolding assms(1,2) by auto
    1.37 +  ultimately show ?thesis apply-apply(rule interior_maximal) defer apply(rule open_interior)
    1.38 +    unfolding assms(1,2) interior_closed_interval by auto qed
    1.39 +
    1.40 +lemma inter_interior_unions_intervals: fixes f::"(real^'n) set set"
    1.41 +  assumes "finite f" "open s" "\<forall>t\<in>f. \<exists>a b. t = {a..b}" "\<forall>t\<in>f. s \<inter> (interior t) = {}"
    1.42 +  shows "s \<inter> interior(\<Union>f) = {}" proof(rule ccontr,unfold ex_in_conv[THEN sym]) case goal1
    1.43 +  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)
    1.44 +    unfolding open_subset_interior[OF open_ball]  using interior_subset by auto
    1.45 +  have lem2:"\<And>x s P. \<exists>x\<in>s. P x \<Longrightarrow> \<exists>x\<in>insert x s. P x" by auto
    1.46 +  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
    1.47 +  thus ?case proof(induct rule:finite_induct) 
    1.48 +    case empty from this(2) guess x .. hence False unfolding Union_empty interior_empty by auto thus ?case by auto next
    1.49 +    case (insert i f) guess x using insert(5) .. note x = this
    1.50 +    then guess e unfolding open_contains_ball_eq[OF open_Int[OF assms(2) open_interior],rule_format] .. note e=this
    1.51 +    guess a using insert(4)[rule_format,OF insertI1] .. then guess b .. note ab = this
    1.52 +    show ?case proof(cases "x\<in>i") case False hence "x \<in> UNIV - {a..b}" unfolding ab by auto
    1.53 +      then guess d unfolding open_contains_ball_eq[OF open_Diff[OF open_UNIV closed_interval],rule_format] ..
    1.54 +      hence "0 < d" "ball x (min d e) \<subseteq> UNIV - i" using e unfolding ab by auto
    1.55 +      hence "ball x (min d e) \<subseteq> s \<inter> interior (\<Union>f)" using e unfolding lem1 by auto hence "x \<in> s \<inter> interior (\<Union>f)" using `d>0` e by auto
    1.56 +      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
    1.57 +    case True show ?thesis proof(cases "x\<in>{a<..<b}")
    1.58 +      case True then guess d unfolding open_contains_ball_eq[OF open_interval,rule_format] ..
    1.59 +      thus ?thesis apply(rule_tac x=i in bexI,rule_tac x=x in exI,rule_tac x="min d e" in exI)
    1.60 +	unfolding ab using interval_open_subset_closed[of a b] and e by fastsimp+ next
    1.61 +    case False then obtain k where "x$k \<le> a$k \<or> x$k \<ge> b$k" unfolding mem_interval by(auto simp add:not_less) 
    1.62 +    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
    1.63 +    hence "\<exists>x. ball x (e/2) \<subseteq> s \<inter> (\<Union>f)" proof(erule_tac disjE)
    1.64 +      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)
    1.65 +	fix y assume "y \<in> ball ?z (e / 2) \<inter> i" hence "dist ?z y < e/2" and yi:"y\<in>i" by auto
    1.66 +	hence "\<bar>(?z - y) $ k\<bar> < e/2" using component_le_norm[of "?z - y" k] unfolding vector_dist_norm by auto
    1.67 +	hence "y$k < a$k" unfolding vector_component_simps vector_scaleR_component as using e[THEN conjunct1] by(auto simp add:field_simps)
    1.68 +	hence "y \<notin> i" unfolding ab mem_interval not_all by(rule_tac x=k in exI,auto) thus False using yi by auto qed
    1.69 +      moreover have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)" apply(rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]]) proof
    1.70 +	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)"
    1.71 +	   apply-apply(rule order_trans,rule norm_triangle_sub[of "x - y" "(e/2) *\<^sub>R basis k"])
    1.72 +	  unfolding norm_scaleR norm_basis by auto
    1.73 +	also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2" apply(rule add_strict_left_mono) using as unfolding mem_ball vector_dist_norm using e by(auto simp add:field_simps)
    1.74 +	finally show "y\<in>ball x e" unfolding mem_ball vector_dist_norm using e by(auto simp add:field_simps) qed
    1.75 +      ultimately show ?thesis apply(rule_tac x="?z" in exI) unfolding Union_insert by auto
    1.76 +    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)
    1.77 +	fix y assume "y \<in> ball ?z (e / 2) \<inter> i" hence "dist ?z y < e/2" and yi:"y\<in>i" by auto
    1.78 +	hence "\<bar>(?z - y) $ k\<bar> < e/2" using component_le_norm[of "?z - y" k] unfolding vector_dist_norm by auto
    1.79 +	hence "y$k > b$k" unfolding vector_component_simps vector_scaleR_component as using e[THEN conjunct1] by(auto simp add:field_simps)
    1.80 +	hence "y \<notin> i" unfolding ab mem_interval not_all by(rule_tac x=k in exI,auto) thus False using yi by auto qed
    1.81 +      moreover have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)" apply(rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]]) proof
    1.82 +	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)"
    1.83 +	   apply-apply(rule order_trans,rule norm_triangle_sub[of "x - y" "- (e/2) *\<^sub>R basis k"])
    1.84 +	  unfolding norm_scaleR norm_basis by auto
    1.85 +	also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2" apply(rule add_strict_left_mono) using as unfolding mem_ball vector_dist_norm using e by(auto simp add:field_simps)
    1.86 +	finally show "y\<in>ball x e" unfolding mem_ball vector_dist_norm using e by(auto simp add:field_simps) qed
    1.87 +      ultimately show ?thesis apply(rule_tac x="?z" in exI) unfolding Union_insert by auto qed 
    1.88 +    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
    1.89 +    thus ?thesis apply-apply(rule lem2,rule insert(3)) using insert(4) by auto qed qed qed qed note * = this
    1.90 +  guess t using *[OF assms(1,3) goal1]  .. from this(2) guess x .. then guess e ..
    1.91 +  hence "x \<in> s" "x\<in>interior t" defer using open_subset_interior[OF open_ball, of x e t] by auto
    1.92 +  thus False using `t\<in>f` assms(4) by auto qed
    1.93 +subsection {* Bounds on intervals where they exist. *}
    1.94 +
    1.95 +definition "interval_upperbound (s::(real^'n) set) = (\<chi> i. Sup {a. \<exists>x\<in>s. x$i = a})"
    1.96 +
    1.97 +definition "interval_lowerbound (s::(real^'n) set) = (\<chi> i. Inf {a. \<exists>x\<in>s. x$i = a})"
    1.98 +
    1.99 +lemma interval_upperbound[simp]: assumes "\<forall>i. a$i \<le> b$i" shows "interval_upperbound {a..b} = b"
   1.100 +  using assms unfolding interval_upperbound_def Cart_eq Cart_lambda_beta apply-apply(rule,erule_tac x=i in allE)
   1.101 +  apply(rule Sup_unique) unfolding setle_def apply rule unfolding mem_Collect_eq apply(erule bexE) unfolding mem_interval defer
   1.102 +  apply(rule,rule) apply(rule_tac x="b$i" in bexI) defer unfolding mem_Collect_eq apply(rule_tac x=b in bexI)
   1.103 +  unfolding mem_interval using assms by auto
   1.104 +
   1.105 +lemma interval_lowerbound[simp]: assumes "\<forall>i. a$i \<le> b$i" shows "interval_lowerbound {a..b} = a"
   1.106 +  using assms unfolding interval_lowerbound_def Cart_eq Cart_lambda_beta apply-apply(rule,erule_tac x=i in allE)
   1.107 +  apply(rule Inf_unique) unfolding setge_def apply rule unfolding mem_Collect_eq apply(erule bexE) unfolding mem_interval defer
   1.108 +  apply(rule,rule) apply(rule_tac x="a$i" in bexI) defer unfolding mem_Collect_eq apply(rule_tac x=a in bexI)
   1.109 +  unfolding mem_interval using assms by auto
   1.110 +
   1.111 +lemmas interval_bounds = interval_upperbound interval_lowerbound
   1.112 +
   1.113 +lemma interval_bounds'[simp]: assumes "{a..b}\<noteq>{}" shows "interval_upperbound {a..b} = b" "interval_lowerbound {a..b} = a"
   1.114 +  using assms unfolding interval_ne_empty by auto
   1.115 +
   1.116 +lemma interval_upperbound_1[simp]: "dest_vec1 a \<le> dest_vec1 b \<Longrightarrow> interval_upperbound {a..b} = (b::real^1)"
   1.117 +  apply(rule interval_upperbound) by auto
   1.118 +
   1.119 +lemma interval_lowerbound_1[simp]: "dest_vec1 a \<le> dest_vec1 b \<Longrightarrow> interval_lowerbound {a..b} = (a::real^1)"
   1.120 +  apply(rule interval_lowerbound) by auto
   1.121 +
   1.122 +lemmas interval_bound_1 = interval_upperbound_1 interval_lowerbound_1
   1.123 +
   1.124 +subsection {* Content (length, area, volume...) of an interval. *}
   1.125 +
   1.126 +definition "content (s::(real^'n) set) =
   1.127 +       (if s = {} then 0 else (\<Prod>i\<in>UNIV. (interval_upperbound s)$i - (interval_lowerbound s)$i))"
   1.128 +
   1.129 +lemma interval_not_empty:"\<forall>i. a$i \<le> b$i \<Longrightarrow> {a..b::real^'n} \<noteq> {}"
   1.130 +  unfolding interval_eq_empty unfolding not_ex not_less by assumption
   1.131 +
   1.132 +lemma content_closed_interval: assumes "\<forall>i. a$i \<le> b$i"
   1.133 +  shows "content {a..b} = (\<Prod>i\<in>UNIV. b$i - a$i)"
   1.134 +  using interval_not_empty[OF assms] unfolding content_def interval_upperbound[OF assms] interval_lowerbound[OF assms] by auto
   1.135 +
   1.136 +lemma content_closed_interval': assumes "{a..b}\<noteq>{}" shows "content {a..b} = (\<Prod>i\<in>UNIV. b$i - a$i)"
   1.137 +  apply(rule content_closed_interval) using assms unfolding interval_ne_empty .
   1.138 +
   1.139 +lemma content_1:"dest_vec1 a \<le> dest_vec1 b \<Longrightarrow> content {a..b} = dest_vec1 b - dest_vec1 a"
   1.140 +  using content_closed_interval[of a b] by auto
   1.141 +
   1.142 +lemma content_1':"a \<le> b \<Longrightarrow> content {vec1 a..vec1 b} = b - a" using content_1[of "vec a" "vec b"] by auto
   1.143 +
   1.144 +lemma content_unit[intro]: "content{0..1::real^'n} = 1" proof-
   1.145 +  have *:"\<forall>i. 0$i \<le> (1::real^'n::finite)$i" by auto
   1.146 +  have "0 \<in> {0..1::real^'n::finite}" unfolding mem_interval by auto
   1.147 +  thus ?thesis unfolding content_def interval_bounds[OF *] using setprod_1 by auto qed
   1.148 +
   1.149 +lemma content_pos_le[intro]: "0 \<le> content {a..b}" proof(cases "{a..b}={}")
   1.150 +  case False hence *:"\<forall>i. a $ i \<le> b $ i" unfolding interval_ne_empty by assumption
   1.151 +  have "(\<Prod>i\<in>UNIV. interval_upperbound {a..b} $ i - interval_lowerbound {a..b} $ i) \<ge> 0"
   1.152 +    apply(rule setprod_nonneg) unfolding interval_bounds[OF *] using * apply(erule_tac x=x in allE) by auto
   1.153 +  thus ?thesis unfolding content_def by(auto simp del:interval_bounds') qed(unfold content_def, auto)
   1.154 +
   1.155 +lemma content_pos_lt: assumes "\<forall>i. a$i < b$i" shows "0 < content {a..b}"
   1.156 +proof- have help_lemma1: "\<forall>i. a$i < b$i \<Longrightarrow> \<forall>i. a$i \<le> ((b$i)::real)" apply(rule,erule_tac x=i in allE) by auto
   1.157 +  show ?thesis unfolding content_closed_interval[OF help_lemma1[OF assms]] apply(rule setprod_pos)
   1.158 +    using assms apply(erule_tac x=x in allE) by auto qed
   1.159 +
   1.160 +lemma content_pos_lt_1: "dest_vec1 a < dest_vec1 b \<Longrightarrow> 0 < content({a..b})"
   1.161 +  apply(rule content_pos_lt) by auto
   1.162 +
   1.163 +lemma content_eq_0: "content({a..b::real^'n}) = 0 \<longleftrightarrow> (\<exists>i. b$i \<le> a$i)" proof(cases "{a..b} = {}")
   1.164 +  case True thus ?thesis unfolding content_def if_P[OF True] unfolding interval_eq_empty apply-
   1.165 +    apply(rule,erule exE) apply(rule_tac x=i in exI) by auto next
   1.166 +  guess a using UNIV_witness[where 'a='n] .. case False note as=this[unfolded interval_eq_empty not_ex not_less]
   1.167 +  show ?thesis unfolding content_def if_not_P[OF False] setprod_zero_iff[OF finite_UNIV]
   1.168 +    apply(rule) apply(erule_tac[!] exE bexE) unfolding interval_bounds[OF as] apply(rule_tac x=x in exI) defer
   1.169 +    apply(rule_tac x=i in bexI) using as apply(erule_tac x=i in allE) by auto qed
   1.170 +
   1.171 +lemma cond_cases:"(P \<Longrightarrow> Q x) \<Longrightarrow> (\<not> P \<Longrightarrow> Q y) \<Longrightarrow> Q (if P then x else y)" by auto
   1.172 +
   1.173 +lemma content_closed_interval_cases:
   1.174 +  "content {a..b} = (if \<forall>i. a$i \<le> b$i then setprod (\<lambda>i. b$i - a$i) UNIV else 0)" apply(rule cond_cases) 
   1.175 +  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
   1.176 +
   1.177 +lemma content_eq_0_interior: "content {a..b} = 0 \<longleftrightarrow> interior({a..b}) = {}"
   1.178 +  unfolding content_eq_0 interior_closed_interval interval_eq_empty by auto
   1.179 +
   1.180 +lemma content_eq_0_1: "content {a..b::real^1} = 0 \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a"
   1.181 +  unfolding content_eq_0 by auto
   1.182 +
   1.183 +lemma content_pos_lt_eq: "0 < content {a..b} \<longleftrightarrow> (\<forall>i. a$i < b$i)"
   1.184 +  apply(rule) defer apply(rule content_pos_lt,assumption) proof- assume "0 < content {a..b}"
   1.185 +  hence "content {a..b} \<noteq> 0" by auto thus "\<forall>i. a$i < b$i" unfolding content_eq_0 not_ex not_le by auto qed
   1.186 +
   1.187 +lemma content_empty[simp]: "content {} = 0" unfolding content_def by auto
   1.188 +
   1.189 +lemma content_subset: assumes "{a..b} \<subseteq> {c..d}" shows "content {a..b::real^'n} \<le> content {c..d}" proof(cases "{a..b}={}")
   1.190 +  case True thus ?thesis using content_pos_le[of c d] by auto next
   1.191 +  case False hence ab_ne:"\<forall>i. a $ i \<le> b $ i" unfolding interval_ne_empty by auto
   1.192 +  hence ab_ab:"a\<in>{a..b}" "b\<in>{a..b}" unfolding mem_interval by auto
   1.193 +  have "{c..d} \<noteq> {}" using assms False by auto
   1.194 +  hence cd_ne:"\<forall>i. c $ i \<le> d $ i" using assms unfolding interval_ne_empty by auto
   1.195 +  show ?thesis unfolding content_def unfolding interval_bounds[OF ab_ne] interval_bounds[OF cd_ne]
   1.196 +    unfolding if_not_P[OF False] if_not_P[OF `{c..d} \<noteq> {}`] apply(rule setprod_mono,rule) proof fix i::'n
   1.197 +    show "0 \<le> b $ i - a $ i" using ab_ne[THEN spec[where x=i]] by auto
   1.198 +    show "b $ i - a $ i \<le> d $ i - c $ i"
   1.199 +      using assms[unfolded subset_eq mem_interval,rule_format,OF ab_ab(2),of i]
   1.200 +      using assms[unfolded subset_eq mem_interval,rule_format,OF ab_ab(1),of i] by auto qed qed
   1.201 +
   1.202 +lemma content_lt_nz: "0 < content {a..b} \<longleftrightarrow> content {a..b} \<noteq> 0"
   1.203 +  unfolding content_pos_lt_eq content_eq_0 unfolding not_ex not_le by auto
   1.204 +
   1.205 +subsection {* The notion of a gauge --- simply an open set containing the point. *}
   1.206 +
   1.207 +definition gauge where "gauge d \<longleftrightarrow> (\<forall>x. x\<in>(d x) \<and> open(d x))"
   1.208 +
   1.209 +lemma gaugeI:assumes "\<And>x. x\<in>g x" "\<And>x. open (g x)" shows "gauge g"
   1.210 +  using assms unfolding gauge_def by auto
   1.211 +
   1.212 +lemma gaugeD[dest]: assumes "gauge d" shows "x\<in>d x" "open (d x)" using assms unfolding gauge_def by auto
   1.213 +
   1.214 +lemma gauge_ball_dependent: "\<forall>x. 0 < e x \<Longrightarrow> gauge (\<lambda>x. ball x (e x))"
   1.215 +  unfolding gauge_def by auto 
   1.216 +
   1.217 +lemma gauge_ball[intro?]: "0 < e \<Longrightarrow> gauge (\<lambda>x. ball x e)" unfolding gauge_def by auto 
   1.218 +
   1.219 +lemma gauge_trivial[intro]: "gauge (\<lambda>x. ball x 1)" apply(rule gauge_ball) by auto
   1.220 +
   1.221 +lemma gauge_inter: "gauge d1 \<Longrightarrow> gauge d2 \<Longrightarrow> gauge (\<lambda>x. (d1 x) \<inter> (d2 x))"
   1.222 +  unfolding gauge_def by auto 
   1.223 +
   1.224 +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-
   1.225 +  have *:"\<And>x. {f d x |d. d \<in> s} = (\<lambda>d. f d x) ` s" by auto show ?thesis
   1.226 +  unfolding gauge_def unfolding * 
   1.227 +  using assms unfolding Ball_def Inter_iff mem_Collect_eq gauge_def by auto qed
   1.228 +
   1.229 +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)
   1.230 +
   1.231 +subsection {* Divisions. *}
   1.232 +
   1.233 +definition division_of (infixl "division'_of" 40) where
   1.234 +  "s division_of i \<equiv>
   1.235 +        finite s \<and>
   1.236 +        (\<forall>k\<in>s. k \<subseteq> i \<and> k \<noteq> {} \<and> (\<exists>a b. k = {a..b})) \<and>
   1.237 +        (\<forall>k1\<in>s. \<forall>k2\<in>s. k1 \<noteq> k2 \<longrightarrow> interior(k1) \<inter> interior(k2) = {}) \<and>
   1.238 +        (\<Union>s = i)"
   1.239 +
   1.240 +lemma division_ofD[dest]: assumes  "s division_of i"
   1.241 +  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})"
   1.242 +  "\<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
   1.243 +
   1.244 +lemma division_ofI:
   1.245 +  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})"
   1.246 +  "\<And>k1 k2. \<lbrakk>k1\<in>s; k2\<in>s; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}" "\<Union>s = i"
   1.247 +  shows "s division_of i" using assms unfolding division_of_def by auto
   1.248 +
   1.249 +lemma division_of_finite: "s division_of i \<Longrightarrow> finite s"
   1.250 +  unfolding division_of_def by auto
   1.251 +
   1.252 +lemma division_of_self[intro]: "{a..b} \<noteq> {} \<Longrightarrow> {{a..b}} division_of {a..b}"
   1.253 +  unfolding division_of_def by auto
   1.254 +
   1.255 +lemma division_of_trivial[simp]: "s division_of {} \<longleftrightarrow> s = {}" unfolding division_of_def by auto 
   1.256 +
   1.257 +lemma division_of_sing[simp]: "s division_of {a..a::real^'n} \<longleftrightarrow> s = {{a..a}}" (is "?l = ?r") proof
   1.258 +  assume ?r moreover { assume "s = {{a}}" moreover fix k assume "k\<in>s" 
   1.259 +    ultimately have"\<exists>x y. k = {x..y}" apply(rule_tac x=a in exI)+ unfolding interval_sing[THEN conjunct1] by auto }
   1.260 +  ultimately show ?l unfolding division_of_def interval_sing[THEN conjunct1] by auto next
   1.261 +  assume ?l note as=conjunctD4[OF this[unfolded division_of_def interval_sing[THEN conjunct1]]]
   1.262 +  { fix x assume x:"x\<in>s" have "x={a}" using as(2)[rule_format,OF x] by auto }
   1.263 +  moreover have "s \<noteq> {}" using as(4) by auto ultimately show ?r unfolding interval_sing[THEN conjunct1] by auto qed
   1.264 +
   1.265 +lemma elementary_empty: obtains p where "p division_of {}"
   1.266 +  unfolding division_of_trivial by auto
   1.267 +
   1.268 +lemma elementary_interval: obtains p where  "p division_of {a..b}"
   1.269 +  by(metis division_of_trivial division_of_self)
   1.270 +
   1.271 +lemma division_contains: "s division_of i \<Longrightarrow> \<forall>x\<in>i. \<exists>k\<in>s. x \<in> k"
   1.272 +  unfolding division_of_def by auto
   1.273 +
   1.274 +lemma forall_in_division:
   1.275 + "d division_of i \<Longrightarrow> ((\<forall>x\<in>d. P x) \<longleftrightarrow> (\<forall>a b. {a..b} \<in> d \<longrightarrow> P {a..b}))"
   1.276 +  unfolding division_of_def by fastsimp
   1.277 +
   1.278 +lemma division_of_subset: assumes "p division_of (\<Union>p)" "q \<subseteq> p" shows "q division_of (\<Union>q)"
   1.279 +  apply(rule division_ofI) proof- note as=division_ofD[OF assms(1)]
   1.280 +  show "finite q" apply(rule finite_subset) using as(1) assms(2) by auto
   1.281 +  { 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
   1.282 +  show "\<exists>a b. k = {a..b}" using as(4)[OF kp] by auto show "k \<noteq> {}" using as(3)[OF kp] by auto }
   1.283 +  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
   1.284 +  show "interior k1 \<inter> interior k2 = {}" using as(5)[OF *] by auto qed auto
   1.285 +
   1.286 +lemma division_of_union_self[intro]: "p division_of s \<Longrightarrow> p division_of (\<Union>p)" unfolding division_of_def by auto
   1.287 +
   1.288 +lemma division_of_content_0: assumes "content {a..b} = 0" "d division_of {a..b}" shows "\<forall>k\<in>d. content k = 0"
   1.289 +  unfolding forall_in_division[OF assms(2)] apply(rule,rule,rule) apply(drule division_ofD(2)[OF assms(2)])
   1.290 +  apply(drule content_subset) unfolding assms(1) proof- case goal1 thus ?case using content_pos_le[of a b] by auto qed
   1.291 +
   1.292 +lemma division_inter: assumes "p1 division_of s1" "p2 division_of (s2::(real^'a) set)"
   1.293 +  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-
   1.294 +let ?A = "{s. s \<in>  (\<lambda>(k1,k2). k1 \<inter> k2) ` (p1 \<times> p2) \<and> s \<noteq> {}}" have *:"?A' = ?A" by auto
   1.295 +show ?thesis unfolding * proof(rule division_ofI) have "?A \<subseteq> (\<lambda>(x, y). x \<inter> y) ` (p1 \<times> p2)" by auto
   1.296 +  moreover have "finite (p1 \<times> p2)" using assms unfolding division_of_def by auto ultimately show "finite ?A" by auto
   1.297 +  have *:"\<And>s. \<Union>{x\<in>s. x \<noteq> {}} = \<Union>s" by auto show "\<Union>?A = s1 \<inter> s2" apply(rule set_ext) unfolding * and Union_image_eq UN_iff
   1.298 +    using division_ofD(6)[OF assms(1)] and division_ofD(6)[OF assms(2)] by auto
   1.299 +  { 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
   1.300 +  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
   1.301 +  guess a1 using division_ofD(4)[OF assms(1) k(2)] .. then guess b1 .. note ab1=this
   1.302 +  guess a2 using division_ofD(4)[OF assms(2) k(3)] .. then guess b2 .. note ab2=this
   1.303 +  show "\<exists>a b. k = {a..b}" unfolding k ab1 ab2 unfolding inter_interval by auto } fix k1 k2
   1.304 +  assume "k1\<in>?A" then obtain x1 y1 where k1:"k1 = x1 \<inter> y1" "x1\<in>p1" "y1\<in>p2" "k1\<noteq>{}" by auto
   1.305 +  assume "k2\<in>?A" then obtain x2 y2 where k2:"k2 = x2 \<inter> y2" "x2\<in>p1" "y2\<in>p2" "k2\<noteq>{}" by auto
   1.306 +  assume "k1 \<noteq> k2" hence th:"x1\<noteq>x2 \<or> y1\<noteq>y2" unfolding k1 k2 by auto
   1.307 +  have *:"(interior x1 \<inter> interior x2 = {} \<or> interior y1 \<inter> interior y2 = {}) \<Longrightarrow>
   1.308 +      interior(x1 \<inter> y1) \<subseteq> interior(x1) \<Longrightarrow> interior(x1 \<inter> y1) \<subseteq> interior(y1) \<Longrightarrow>
   1.309 +      interior(x2 \<inter> y2) \<subseteq> interior(x2) \<Longrightarrow> interior(x2 \<inter> y2) \<subseteq> interior(y2)
   1.310 +      \<Longrightarrow> interior(x1 \<inter> y1) \<inter> interior(x2 \<inter> y2) = {}" by auto
   1.311 +  show "interior k1 \<inter> interior k2 = {}" unfolding k1 k2 apply(rule *) defer apply(rule_tac[1-4] subset_interior)
   1.312 +    using division_ofD(5)[OF assms(1) k1(2) k2(2)]
   1.313 +    using division_ofD(5)[OF assms(2) k1(3) k2(3)] using th by auto qed qed
   1.314 +
   1.315 +lemma division_inter_1: assumes "d division_of i" "{a..b::real^'n} \<subseteq> i"
   1.316 +  shows "{ {a..b} \<inter> k |k. k \<in> d \<and> {a..b} \<inter> k \<noteq> {} } division_of {a..b}" proof(cases "{a..b} = {}")
   1.317 +  case True show ?thesis unfolding True and division_of_trivial by auto next
   1.318 +  have *:"{a..b} \<inter> i = {a..b}" using assms(2) by auto 
   1.319 +  case False show ?thesis using division_inter[OF division_of_self[OF False] assms(1)] unfolding * by auto qed
   1.320 +
   1.321 +lemma elementary_inter: assumes "p1 division_of s" "p2 division_of (t::(real^'n) set)"
   1.322 +  shows "\<exists>p. p division_of (s \<inter> t)"
   1.323 +  by(rule,rule division_inter[OF assms])
   1.324 +
   1.325 +lemma elementary_inters: assumes "finite f" "f\<noteq>{}" "\<forall>s\<in>f. \<exists>p. p division_of (s::(real^'n) set)"
   1.326 +  shows "\<exists>p. p division_of (\<Inter> f)" using assms apply-proof(induct f rule:finite_induct)
   1.327 +case (insert x f) show ?case proof(cases "f={}")
   1.328 +  case True thus ?thesis unfolding True using insert by auto next
   1.329 +  case False guess p using insert(3)[OF False insert(5)[unfolded ball_simps,THEN conjunct2]] ..
   1.330 +  moreover guess px using insert(5)[rule_format,OF insertI1] .. ultimately
   1.331 +  show ?thesis unfolding Inter_insert apply(rule_tac elementary_inter) by assumption+ qed qed auto
   1.332 +
   1.333 +lemma division_disjoint_union:
   1.334 +  assumes "p1 division_of s1" "p2 division_of s2" "interior s1 \<inter> interior s2 = {}"
   1.335 +  shows "(p1 \<union> p2) division_of (s1 \<union> s2)" proof(rule division_ofI) 
   1.336 +  note d1 = division_ofD[OF assms(1)] and d2 = division_ofD[OF assms(2)]
   1.337 +  show "finite (p1 \<union> p2)" using d1(1) d2(1) by auto
   1.338 +  show "\<Union>(p1 \<union> p2) = s1 \<union> s2" using d1(6) d2(6) by auto
   1.339 +  { 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 = {}"
   1.340 +  { 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)]]
   1.341 +      using assms(3) by blast } moreover
   1.342 +  { 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)]]
   1.343 +      using assms(3) by blast} ultimately
   1.344 +  show ?g using d1(5)[OF _ _ as(3)] and d2(5)[OF _ _ as(3)] by auto }
   1.345 +  fix k assume k:"k \<in> p1 \<union> p2"  show "k \<subseteq> s1 \<union> s2" using k d1(2) d2(2) by auto
   1.346 +  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
   1.347 +
   1.348 +lemma partial_division_extend_1:
   1.349 +  assumes "{c..d} \<subseteq> {a..b::real^'n}" "{c..d} \<noteq> {}"
   1.350 +  obtains p where "p division_of {a..b}" "{c..d} \<in> p"
   1.351 +proof- def n \<equiv> "CARD('n)" have n:"1 \<le> n" "0 < n" "n \<noteq> 0" unfolding n_def by auto
   1.352 +  guess \<pi> using ex_bij_betw_nat_finite_1[OF finite_UNIV[where 'a='n]] .. note \<pi>=this
   1.353 +  def \<pi>' \<equiv> "inv_into {1..n} \<pi>"
   1.354 +  have \<pi>':"bij_betw \<pi>' UNIV {1..n}" using bij_betw_inv_into[OF \<pi>] unfolding \<pi>'_def n_def by auto
   1.355 +  hence \<pi>'i:"\<And>i. \<pi>' i \<in> {1..n}" unfolding bij_betw_def by auto 
   1.356 +  have \<pi>\<pi>'[simp]:"\<And>i. \<pi> (\<pi>' i) = i" unfolding \<pi>'_def apply(rule f_inv_into_f) unfolding n_def using \<pi> unfolding bij_betw_def by auto
   1.357 +  have \<pi>'\<pi>[simp]:"\<And>i. i\<in>{1..n} \<Longrightarrow> \<pi>' (\<pi> i) = i" unfolding \<pi>'_def apply(rule inv_into_f_eq) using \<pi> unfolding n_def bij_betw_def by auto
   1.358 +  have "{c..d} \<noteq> {}" using assms by auto
   1.359 +  let ?p1 = "\<lambda>l. {(\<chi> i. if \<pi>' i < l then c$i else a$i) .. (\<chi> i. if \<pi>' i < l then d$i else if \<pi>' i = l then c$\<pi> l else b$i)}"
   1.360 +  let ?p2 = "\<lambda>l. {(\<chi> i. if \<pi>' i < l then c$i else if \<pi>' i = l then d$\<pi> l else a$i) .. (\<chi> i. if \<pi>' i < l then d$i else b$i)}"
   1.361 +  let ?p =  "{?p1 l |l. l \<in> {1..n+1}} \<union> {?p2 l |l. l \<in> {1..n+1}}"
   1.362 +  have abcd:"\<And>i. a $ i \<le> c $ i \<and> c$i \<le> d$i \<and> d $ i \<le> b $ i" using assms unfolding subset_interval interval_eq_empty by(auto simp add:not_le not_less)
   1.363 +  show ?thesis apply(rule that[of ?p]) apply(rule division_ofI)
   1.364 +  proof- have "\<And>i. \<pi>' i < Suc n"
   1.365 +    proof(rule ccontr,unfold not_less) fix i assume "Suc n \<le> \<pi>' i"
   1.366 +      hence "\<pi>' i \<notin> {1..n}" by auto thus False using \<pi>' unfolding bij_betw_def by auto
   1.367 +    qed hence "c = (\<chi> i. if \<pi>' i < Suc n then c $ i else a $ i)"
   1.368 +        "d = (\<chi> i. if \<pi>' i < Suc n then d $ i else if \<pi>' i = n + 1 then c $ \<pi> (n + 1) else b $ i)"
   1.369 +      unfolding Cart_eq Cart_lambda_beta using \<pi>' unfolding bij_betw_def by auto
   1.370 +    thus cdp:"{c..d} \<in> ?p" apply-apply(rule UnI1) unfolding mem_Collect_eq apply(rule_tac x="n + 1" in exI) by auto
   1.371 +    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}"
   1.372 +      unfolding subset_eq apply(rule_tac[!] ballI,rule_tac[!] ccontr)
   1.373 +    proof- fix l assume l:"l\<in>{1..n+1}" fix x assume "x\<notin>{a..b}"
   1.374 +      then guess i unfolding mem_interval not_all .. note i=this
   1.375 +      show "x \<in> ?p1 l \<Longrightarrow> False" "x \<in> ?p2 l \<Longrightarrow> False" unfolding mem_interval apply(erule_tac[!] x=i in allE)
   1.376 +        apply(case_tac[!] "\<pi>' i < l", case_tac[!] "\<pi>' i = l") using abcd[of i] i by auto 
   1.377 +    qed moreover have "\<And>x. x \<in> {a..b} \<Longrightarrow> x \<in> \<Union>?p"
   1.378 +    proof- fix x assume x:"x\<in>{a..b}"
   1.379 +      { presume "x\<notin>{c..d} \<Longrightarrow> x \<in> \<Union>?p" thus "x \<in> \<Union>?p" using cdp by blast }
   1.380 +      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)}"
   1.381 +      assume "x\<notin>{c..d}" then guess i0 unfolding mem_interval not_all ..
   1.382 +      hence "\<pi>' i0 \<in> ?M" using \<pi>' unfolding bij_betw_def by(auto intro!:le_SucI)
   1.383 +      hence M:"finite ?M" "?M \<noteq> {}" by auto
   1.384 +      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]]
   1.385 +        Min_gr_iff[OF M,unfolded l_def[symmetric]]
   1.386 +      have "x\<in>?p1 l \<or> x\<in>?p2 l" using l(2)[THEN conjunct2] unfolding de_Morgan_conj not_le
   1.387 +        apply- apply(erule disjE) apply(rule disjI1) defer apply(rule disjI2)
   1.388 +      proof- assume as:"x $ \<pi> l < c $ \<pi> l"
   1.389 +        show "x \<in> ?p1 l" unfolding mem_interval Cart_lambda_beta
   1.390 +        proof case goal1 have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le by auto
   1.391 +          thus ?case using as x[unfolded mem_interval,rule_format,of i]
   1.392 +            apply auto using l(3)[of "\<pi>' i"] by(auto elim!:ballE[where x="\<pi>' i"])
   1.393 +        qed
   1.394 +      next assume as:"x $ \<pi> l > d $ \<pi> l"
   1.395 +        show "x \<in> ?p2 l" unfolding mem_interval Cart_lambda_beta
   1.396 +        proof case goal1 have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le by auto
   1.397 +          thus ?case using as x[unfolded mem_interval,rule_format,of i]
   1.398 +            apply auto using l(3)[of "\<pi>' i"] by(auto elim!:ballE[where x="\<pi>' i"])
   1.399 +        qed qed
   1.400 +      thus "x \<in> \<Union>?p" using l(2) by blast 
   1.401 +    qed ultimately show "\<Union>?p = {a..b}" apply-apply(rule) defer apply(rule) by(assumption,blast)
   1.402 +    
   1.403 +    show "finite ?p" by auto
   1.404 +    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
   1.405 +    show "k\<subseteq>{a..b}" apply(rule,unfold mem_interval,rule,rule) 
   1.406 +    proof- fix i::'n and x assume "x \<in> k" moreover have "\<pi>' i < l \<or> \<pi>' i = l \<or> \<pi>' i > l" by auto
   1.407 +      ultimately show "a$i \<le> x$i" "x$i \<le> b$i" using abcd[of i] using l by(auto elim:disjE elim!:allE[where x=i] simp add:vector_le_def)
   1.408 +    qed have "\<And>l. ?p1 l \<noteq> {}" "\<And>l. ?p2 l \<noteq> {}" unfolding interval_eq_empty not_ex apply(rule_tac[!] allI)
   1.409 +    proof- case goal1 thus ?case using abcd[of x] by auto
   1.410 +    next   case goal2 thus ?case using abcd[of x] by auto
   1.411 +    qed thus "k \<noteq> {}" using k by auto
   1.412 +    show "\<exists>a b. k = {a..b}" using k by auto
   1.413 +    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
   1.414 +    { fix k k' l l'
   1.415 +      assume k:"k\<in>?p" and l:"k = ?p1 l \<or> k = ?p2 l" "l \<in> {1..n + 1}" 
   1.416 +      assume k':"k' \<in> ?p" "k \<noteq> k'" and  l':"k' = ?p1 l' \<or> k' = ?p2 l'" "l' \<in> {1..n + 1}" 
   1.417 +      assume "l \<le> l'" fix x
   1.418 +      have "x \<notin> interior k \<inter> interior k'" 
   1.419 +      proof(rule,cases "l' = n+1") assume x:"x \<in> interior k \<inter> interior k'"
   1.420 +        case True hence "\<And>i. \<pi>' i < l'" using \<pi>'i by(auto simp add:less_Suc_eq_le)
   1.421 +        hence k':"k' = {c..d}" using l'(1) \<pi>'i by(auto simp add:Cart_nth_inverse)
   1.422 +        have ln:"l < n + 1" 
   1.423 +        proof(rule ccontr) case goal1 hence l2:"l = n+1" using l by auto
   1.424 +          hence "\<And>i. \<pi>' i < l" using \<pi>'i by(auto simp add:less_Suc_eq_le)
   1.425 +          hence "k = {c..d}" using l(1) \<pi>'i by(auto simp add:Cart_nth_inverse)
   1.426 +          thus False using `k\<noteq>k'` k' by auto
   1.427 +        qed have **:"\<pi>' (\<pi> l) = l" using \<pi>'\<pi>[of l] using l ln by auto
   1.428 +        have "x $ \<pi> l < c $ \<pi> l \<or> d $ \<pi> l < x $ \<pi> l" using l(1) apply-
   1.429 +        proof(erule disjE)
   1.430 +          assume as:"k = ?p1 l" note * = conjunct1[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   1.431 +          show ?thesis using *[of "\<pi> l"] using ln unfolding Cart_lambda_beta ** by auto
   1.432 +        next assume as:"k = ?p2 l" note * = conjunct1[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   1.433 +          show ?thesis using *[of "\<pi> l"] using ln unfolding Cart_lambda_beta ** by auto
   1.434 +        qed thus False using x unfolding k' unfolding Int_iff interior_closed_interval mem_interval
   1.435 +          by(auto elim!:allE[where x="\<pi> l"])
   1.436 +      next case False hence "l < n + 1" using l'(2) using `l\<le>l'` by auto
   1.437 +        hence ln:"l \<in> {1..n}" "l' \<in> {1..n}" using l l' False by auto
   1.438 +        note \<pi>l = \<pi>'\<pi>[OF ln(1)] \<pi>'\<pi>[OF ln(2)]
   1.439 +        assume x:"x \<in> interior k \<inter> interior k'"
   1.440 +        show False using l(1) l'(1) apply-
   1.441 +        proof(erule_tac[!] disjE)+
   1.442 +          assume as:"k = ?p1 l" "k' = ?p1 l'"
   1.443 +          note * = x[unfolded as Int_iff interior_closed_interval mem_interval]
   1.444 +          have "l \<noteq> l'" using k'(2)[unfolded as] by auto
   1.445 +          thus False using * by(smt Cart_lambda_beta \<pi>l)
   1.446 +        next assume as:"k = ?p2 l" "k' = ?p2 l'"
   1.447 +          note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   1.448 +          have "l \<noteq> l'" apply(rule) using k'(2)[unfolded as] by auto
   1.449 +          thus False using *[of "\<pi> l"] *[of "\<pi> l'"]
   1.450 +            unfolding Cart_lambda_beta \<pi>l using `l \<le> l'` by auto
   1.451 +        next assume as:"k = ?p1 l" "k' = ?p2 l'"
   1.452 +          note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   1.453 +          show False using *[of "\<pi> l"] *[of "\<pi> l'"]
   1.454 +            unfolding Cart_lambda_beta \<pi>l using `l \<le> l'` using abcd[of "\<pi> l'"] by smt 
   1.455 +        next assume as:"k = ?p2 l" "k' = ?p1 l'"
   1.456 +          note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   1.457 +          show False using *[of "\<pi> l"] *[of "\<pi> l'"]
   1.458 +            unfolding Cart_lambda_beta \<pi>l using `l \<le> l'` using abcd[of "\<pi> l'"] by smt
   1.459 +        qed qed } 
   1.460 +    from this[OF k l k' l'] this[OF k'(1) l' k _ l] have "\<And>x. x \<notin> interior k \<inter> interior k'"
   1.461 +      apply - apply(cases "l' \<le> l") using k'(2) by auto            
   1.462 +    thus "interior k \<inter> interior k' = {}" by auto        
   1.463 +qed qed
   1.464 +
   1.465 +lemma partial_division_extend_interval: assumes "p division_of (\<Union>p)" "(\<Union>p) \<subseteq> {a..b}"
   1.466 +  obtains q where "p \<subseteq> q" "q division_of {a..b::real^'n}" proof(cases "p = {}")
   1.467 +  case True guess q apply(rule elementary_interval[of a b]) .
   1.468 +  thus ?thesis apply- apply(rule that[of q]) unfolding True by auto next
   1.469 +  case False note p = division_ofD[OF assms(1)]
   1.470 +  have *:"\<forall>k\<in>p. \<exists>q. q division_of {a..b} \<and> k\<in>q" proof case goal1
   1.471 +    guess c using p(4)[OF goal1] .. then guess d .. note cd_ = this
   1.472 +    have *:"{c..d} \<subseteq> {a..b}" "{c..d} \<noteq> {}" using p(2,3)[OF goal1, unfolded cd_] using assms(2) by auto
   1.473 +    guess q apply(rule partial_division_extend_1[OF *]) . thus ?case unfolding cd_ by auto qed
   1.474 +  guess q using bchoice[OF *] .. note q = conjunctD2[OF this[rule_format]]
   1.475 +  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-
   1.476 +    fix x assume x:"x\<in>p" show "q x division_of \<Union>q x" apply-apply(rule division_ofI)
   1.477 +      using division_ofD[OF q(1)[OF x]] by auto show "q x - {x} \<subseteq> q x" by auto qed
   1.478 +  hence "\<exists>d. d division_of \<Inter> ((\<lambda>i. \<Union>(q i - {i})) ` p)" apply- apply(rule elementary_inters)
   1.479 +    apply(rule finite_imageI[OF p(1)]) unfolding image_is_empty apply(rule False) by auto
   1.480 +  then guess d .. note d = this
   1.481 +  show ?thesis apply(rule that[of "d \<union> p"]) proof-
   1.482 +    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
   1.483 +    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"
   1.484 +      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
   1.485 +    show "d \<union> p division_of {a..b}" unfolding * apply(rule division_disjoint_union[OF d assms(1)])
   1.486 +      apply(rule inter_interior_unions_intervals) apply(rule p open_interior ballI)+ proof(assumption,rule)
   1.487 +      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
   1.488 +      show "interior (\<Inter>(\<lambda>i. \<Union>(q i - {i})) ` p) \<inter> interior k = {}" apply(rule *[of _ "interior (\<Union>(q k - {k}))"])
   1.489 +	defer apply(subst Int_commute) apply(rule inter_interior_unions_intervals) proof- note qk=division_ofD[OF q(1)[OF k]]
   1.490 +	show "finite (q k - {k})" "open (interior k)"  "\<forall>t\<in>q k - {k}. \<exists>a b. t = {a..b}" using qk by auto
   1.491 +	show "\<forall>t\<in>q k - {k}. interior k \<inter> interior t = {}" using qk(5) using q(2)[OF k] by auto
   1.492 +	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}))"
   1.493 +	  apply(rule subset_interior *)+ using k by auto qed qed qed auto qed
   1.494 +
   1.495 +lemma elementary_bounded[dest]: "p division_of s \<Longrightarrow> bounded (s::(real^'n) set)"
   1.496 +  unfolding division_of_def by(metis bounded_Union bounded_interval) 
   1.497 +
   1.498 +lemma elementary_subset_interval: "p division_of s \<Longrightarrow> \<exists>a b. s \<subseteq> {a..b::real^'n}"
   1.499 +  by(meson elementary_bounded bounded_subset_closed_interval)
   1.500 +
   1.501 +lemma division_union_intervals_exists: assumes "{a..b::real^'n} \<noteq> {}"
   1.502 +  obtains p where "(insert {a..b} p) division_of ({a..b} \<union> {c..d})" proof(cases "{c..d} = {}")
   1.503 +  case True show ?thesis apply(rule that[of "{}"]) unfolding True using assms by auto next
   1.504 +  case False note false=this show ?thesis proof(cases "{a..b} \<inter> {c..d} = {}")
   1.505 +  have *:"\<And>a b. {a,b} = {a} \<union> {b}" by auto
   1.506 +  case True show ?thesis apply(rule that[of "{{c..d}}"]) unfolding * apply(rule division_disjoint_union)
   1.507 +    using false True assms using interior_subset by auto next
   1.508 +  case False obtain u v where uv:"{a..b} \<inter> {c..d} = {u..v}" unfolding inter_interval by auto
   1.509 +  have *:"{u..v} \<subseteq> {c..d}" using uv by auto
   1.510 +  guess p apply(rule partial_division_extend_1[OF * False[unfolded uv]]) . note p=this division_ofD[OF this(1)]
   1.511 +  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
   1.512 +  show thesis apply(rule that[of "p - {{u..v}}"]) unfolding *(1) apply(subst *(2)) apply(rule division_disjoint_union)
   1.513 +    apply(rule,rule assms) apply(rule division_of_subset[of p]) apply(rule division_of_union_self[OF p(1)]) defer
   1.514 +    unfolding interior_inter[THEN sym] proof-
   1.515 +    have *:"\<And>cd p uv ab. p \<subseteq> cd \<Longrightarrow> ab \<inter> cd = uv \<Longrightarrow> ab \<inter> p = uv \<inter> p" by auto
   1.516 +    have "interior ({a..b} \<inter> \<Union>(p - {{u..v}})) = interior({u..v} \<inter> \<Union>(p - {{u..v}}))" 
   1.517 +      apply(rule arg_cong[of _ _ interior]) apply(rule *[OF _ uv]) using p(8) by auto
   1.518 +    also have "\<dots> = {}" unfolding interior_inter apply(rule inter_interior_unions_intervals) using p(6) p(7)[OF p(2)] p(3) by auto
   1.519 +    finally show "interior ({a..b} \<inter> \<Union>(p - {{u..v}})) = {}" by assumption qed auto qed qed
   1.520 +
   1.521 +lemma division_of_unions: assumes "finite f"  "\<And>p. p\<in>f \<Longrightarrow> p division_of (\<Union>p)"
   1.522 +  "\<And>k1 k2. \<lbrakk>k1 \<in> \<Union>f; k2 \<in> \<Union>f; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
   1.523 +  shows "\<Union>f division_of \<Union>\<Union>f" apply(rule division_ofI) prefer 5 apply(rule assms(3)|assumption)+
   1.524 +  apply(rule finite_Union assms(1))+ prefer 3 apply(erule UnionE) apply(rule_tac s=X in division_ofD(3)[OF assms(2)])
   1.525 +  using division_ofD[OF assms(2)] by auto
   1.526 +  
   1.527 +lemma elementary_union_interval: assumes "p division_of \<Union>p"
   1.528 +  obtains q where "q division_of ({a..b::real^'n} \<union> \<Union>p)" proof-
   1.529 +  note assm=division_ofD[OF assms]
   1.530 +  have lem1:"\<And>f s. \<Union>\<Union> (f ` s) = \<Union>(\<lambda>x.\<Union>(f x)) ` s" by auto
   1.531 +  have lem2:"\<And>f s. f \<noteq> {} \<Longrightarrow> \<Union>{s \<union> t |t. t \<in> f} = s \<union> \<Union>f" by auto
   1.532 +{ presume "p={} \<Longrightarrow> thesis" "{a..b} = {} \<Longrightarrow> thesis" "{a..b} \<noteq> {} \<Longrightarrow> interior {a..b} = {} \<Longrightarrow> thesis"
   1.533 +    "p\<noteq>{} \<Longrightarrow> interior {a..b}\<noteq>{} \<Longrightarrow> {a..b} \<noteq> {} \<Longrightarrow> thesis"
   1.534 +  thus thesis by auto
   1.535 +next assume as:"p={}" guess p apply(rule elementary_interval[of a b]) .
   1.536 +  thus thesis apply(rule_tac that[of p]) unfolding as by auto 
   1.537 +next assume as:"{a..b}={}" show thesis apply(rule that) unfolding as using assms by auto
   1.538 +next assume as:"interior {a..b} = {}" "{a..b} \<noteq> {}"
   1.539 +  show thesis apply(rule that[of "insert {a..b} p"],rule division_ofI)
   1.540 +    unfolding finite_insert apply(rule assm(1)) unfolding Union_insert  
   1.541 +    using assm(2-4) as apply- by(fastsimp dest: assm(5))+
   1.542 +next assume as:"p \<noteq> {}" "interior {a..b} \<noteq> {}" "{a..b}\<noteq>{}"
   1.543 +  have "\<forall>k\<in>p. \<exists>q. (insert {a..b} q) division_of ({a..b} \<union> k)" proof case goal1
   1.544 +    from assm(4)[OF this] guess c .. then guess d ..
   1.545 +    thus ?case apply-apply(rule division_union_intervals_exists[OF as(3),of c d]) by auto
   1.546 +  qed from bchoice[OF this] guess q .. note q=division_ofD[OF this[rule_format]]
   1.547 +  let ?D = "\<Union>{insert {a..b} (q k) | k. k \<in> p}"
   1.548 +  show thesis apply(rule that[of "?D"]) proof(rule division_ofI)
   1.549 +    have *:"{insert {a..b} (q k) |k. k \<in> p} = (\<lambda>k. insert {a..b} (q k)) ` p" by auto
   1.550 +    show "finite ?D" apply(rule finite_Union) unfolding * apply(rule finite_imageI) using assm(1) q(1) by auto
   1.551 +    show "\<Union>?D = {a..b} \<union> \<Union>p" unfolding * lem1 unfolding lem2[OF as(1), of "{a..b}",THEN sym]
   1.552 +      using q(6) by auto
   1.553 +    fix k assume k:"k\<in>?D" thus " k \<subseteq> {a..b} \<union> \<Union>p" using q(2) by auto
   1.554 +    show "k \<noteq> {}" using q(3) k by auto show "\<exists>a b. k = {a..b}" using q(4) k by auto
   1.555 +    fix k' assume k':"k'\<in>?D" "k\<noteq>k'"
   1.556 +    obtain x  where x: "k \<in>insert {a..b} (q x)"  "x\<in>p"  using k  by auto
   1.557 +    obtain x' where x':"k'\<in>insert {a..b} (q x')" "x'\<in>p" using k' by auto
   1.558 +    show "interior k \<inter> interior k' = {}" proof(cases "x=x'")
   1.559 +      case True show ?thesis apply(rule q(5)) using x x' k' unfolding True by auto
   1.560 +    next case False 
   1.561 +      { presume "k = {a..b} \<Longrightarrow> ?thesis" "k' = {a..b} \<Longrightarrow> ?thesis" 
   1.562 +        "k \<noteq> {a..b} \<Longrightarrow> k' \<noteq> {a..b} \<Longrightarrow> ?thesis"
   1.563 +        thus ?thesis by auto }
   1.564 +      { assume as':"k  = {a..b}" show ?thesis apply(rule q(5)) using x' k'(2) unfolding as' by auto }
   1.565 +      { assume as':"k' = {a..b}" show ?thesis apply(rule q(5)) using x  k'(2) unfolding as' by auto }
   1.566 +      assume as':"k \<noteq> {a..b}" "k' \<noteq> {a..b}"
   1.567 +      guess c using q(4)[OF x(2,1)] .. then guess d .. note c_d=this
   1.568 +      have "interior k  \<inter> interior {a..b} = {}" apply(rule q(5)) using x  k'(2) using as' by auto
   1.569 +      hence "interior k \<subseteq> interior x" apply-
   1.570 +        apply(rule interior_subset_union_intervals[OF c_d _ as(2) q(2)[OF x(2,1)]]) by auto moreover
   1.571 +      guess c using q(4)[OF x'(2,1)] .. then guess d .. note c_d=this
   1.572 +      have "interior k' \<inter> interior {a..b} = {}" apply(rule q(5)) using x' k'(2) using as' by auto
   1.573 +      hence "interior k' \<subseteq> interior x'" apply-
   1.574 +        apply(rule interior_subset_union_intervals[OF c_d _ as(2) q(2)[OF x'(2,1)]]) by auto
   1.575 +      ultimately show ?thesis using assm(5)[OF x(2) x'(2) False] by auto
   1.576 +    qed qed } qed
   1.577 +
   1.578 +lemma elementary_unions_intervals:
   1.579 +  assumes "finite f" "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = {a..b::real^'n}"
   1.580 +  obtains p where "p division_of (\<Union>f)" proof-
   1.581 +  have "\<exists>p. p division_of (\<Union>f)" proof(induct_tac f rule:finite_subset_induct) 
   1.582 +    show "\<exists>p. p division_of \<Union>{}" using elementary_empty by auto
   1.583 +    fix x F assume as:"finite F" "x \<notin> F" "\<exists>p. p division_of \<Union>F" "x\<in>f"
   1.584 +    from this(3) guess p .. note p=this
   1.585 +    from assms(2)[OF as(4)] guess a .. then guess b .. note ab=this
   1.586 +    have *:"\<Union>F = \<Union>p" using division_ofD[OF p] by auto
   1.587 +    show "\<exists>p. p division_of \<Union>insert x F" using elementary_union_interval[OF p[unfolded *], of a b]
   1.588 +      unfolding Union_insert ab * by auto
   1.589 +  qed(insert assms,auto) thus ?thesis apply-apply(erule exE,rule that) by auto qed
   1.590 +
   1.591 +lemma elementary_union: assumes "ps division_of s" "pt division_of (t::(real^'n) set)"
   1.592 +  obtains p where "p division_of (s \<union> t)"
   1.593 +proof- have "s \<union> t = \<Union>ps \<union> \<Union>pt" using assms unfolding division_of_def by auto
   1.594 +  hence *:"\<Union>(ps \<union> pt) = s \<union> t" by auto
   1.595 +  show ?thesis apply-apply(rule elementary_unions_intervals[of "ps\<union>pt"])
   1.596 +    unfolding * prefer 3 apply(rule_tac p=p in that)
   1.597 +    using assms[unfolded division_of_def] by auto qed
   1.598 +
   1.599 +lemma partial_division_extend: fixes t::"(real^'n) set"
   1.600 +  assumes "p division_of s" "q division_of t" "s \<subseteq> t"
   1.601 +  obtains r where "p \<subseteq> r" "r division_of t" proof-
   1.602 +  note divp = division_ofD[OF assms(1)] and divq = division_ofD[OF assms(2)]
   1.603 +  obtain a b where ab:"t\<subseteq>{a..b}" using elementary_subset_interval[OF assms(2)] by auto
   1.604 +  guess r1 apply(rule partial_division_extend_interval) apply(rule assms(1)[unfolded divp(6)[THEN sym]])
   1.605 +    apply(rule subset_trans) by(rule ab assms[unfolded divp(6)[THEN sym]])+  note r1 = this division_ofD[OF this(2)]
   1.606 +  guess p' apply(rule elementary_unions_intervals[of "r1 - p"]) using r1(3,6) by auto 
   1.607 +  then obtain r2 where r2:"r2 division_of (\<Union>(r1 - p)) \<inter> (\<Union>q)" 
   1.608 +    apply- apply(drule elementary_inter[OF _ assms(2)[unfolded divq(6)[THEN sym]]]) by auto
   1.609 +  { fix x assume x:"x\<in>t" "x\<notin>s"
   1.610 +    hence "x\<in>\<Union>r1" unfolding r1 using ab by auto
   1.611 +    then guess r unfolding Union_iff .. note r=this moreover
   1.612 +    have "r \<notin> p" proof assume "r\<in>p" hence "x\<in>s" using divp(2) r by auto
   1.613 +      thus False using x by auto qed
   1.614 +    ultimately have "x\<in>\<Union>(r1 - p)" by auto }
   1.615 +  hence *:"t = \<Union>p \<union> (\<Union>(r1 - p) \<inter> \<Union>q)" unfolding divp divq using assms(3) by auto
   1.616 +  show ?thesis apply(rule that[of "p \<union> r2"]) unfolding * defer apply(rule division_disjoint_union)
   1.617 +    unfolding divp(6) apply(rule assms r2)+
   1.618 +  proof- have "interior s \<inter> interior (\<Union>(r1-p)) = {}"
   1.619 +    proof(rule inter_interior_unions_intervals)
   1.620 +      show "finite (r1 - p)" "open (interior s)" "\<forall>t\<in>r1-p. \<exists>a b. t = {a..b}" using r1 by auto
   1.621 +      have *:"\<And>s. (\<And>x. x \<in> s \<Longrightarrow> False) \<Longrightarrow> s = {}" by auto
   1.622 +      show "\<forall>t\<in>r1-p. interior s \<inter> interior t = {}" proof(rule)
   1.623 +        fix m x assume as:"m\<in>r1-p"
   1.624 +        have "interior m \<inter> interior (\<Union>p) = {}" proof(rule inter_interior_unions_intervals)
   1.625 +          show "finite p" "open (interior m)" "\<forall>t\<in>p. \<exists>a b. t = {a..b}" using divp by auto
   1.626 +          show "\<forall>t\<in>p. interior m \<inter> interior t = {}" apply(rule, rule r1(7)) using as using r1 by auto
   1.627 +        qed thus "interior s \<inter> interior m = {}" unfolding divp by auto
   1.628 +      qed qed        
   1.629 +    thus "interior s \<inter> interior (\<Union>(r1-p) \<inter> (\<Union>q)) = {}" using interior_subset by auto
   1.630 +  qed auto qed
   1.631 +
   1.632 +subsection {* Tagged (partial) divisions. *}
   1.633 +
   1.634 +definition tagged_partial_division_of (infixr "tagged'_partial'_division'_of" 40) where
   1.635 +  "(s tagged_partial_division_of i) \<equiv>
   1.636 +        finite s \<and>
   1.637 +        (\<forall>x k. (x,k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = {a..b})) \<and>
   1.638 +        (\<forall>x1 k1 x2 k2. (x1,k1) \<in> s \<and> (x2,k2) \<in> s \<and> ((x1,k1) \<noteq> (x2,k2))
   1.639 +                       \<longrightarrow> (interior(k1) \<inter> interior(k2) = {}))"
   1.640 +
   1.641 +lemma tagged_partial_division_ofD[dest]: assumes "s tagged_partial_division_of i"
   1.642 +  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"
   1.643 +  "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
   1.644 +  "\<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) = {}"
   1.645 +  using assms unfolding tagged_partial_division_of_def  apply- by blast+ 
   1.646 +
   1.647 +definition tagged_division_of (infixr "tagged'_division'_of" 40) where
   1.648 +  "(s tagged_division_of i) \<equiv>
   1.649 +        (s tagged_partial_division_of i) \<and> (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
   1.650 +
   1.651 +lemma tagged_division_of_finite[dest]: "s tagged_division_of i \<Longrightarrow> finite s"
   1.652 +  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
   1.653 +
   1.654 +lemma tagged_division_of:
   1.655 + "(s tagged_division_of i) \<longleftrightarrow>
   1.656 +        finite s \<and>
   1.657 +        (\<forall>x k. (x,k) \<in> s
   1.658 +               \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = {a..b})) \<and>
   1.659 +        (\<forall>x1 k1 x2 k2. (x1,k1) \<in> s \<and> (x2,k2) \<in> s \<and> ~((x1,k1) = (x2,k2))
   1.660 +                       \<longrightarrow> (interior(k1) \<inter> interior(k2) = {})) \<and>
   1.661 +        (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
   1.662 +  unfolding tagged_division_of_def tagged_partial_division_of_def by auto
   1.663 +
   1.664 +lemma tagged_division_ofI: assumes
   1.665 +  "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}"
   1.666 +  "\<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) = {})"
   1.667 +  "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
   1.668 +  shows "s tagged_division_of i"
   1.669 +  unfolding tagged_division_of apply(rule) defer apply rule
   1.670 +  apply(rule allI impI conjI assms)+ apply assumption
   1.671 +  apply(rule, rule assms, assumption) apply(rule assms, assumption)
   1.672 +  using assms(1,5-) apply- by blast+
   1.673 +
   1.674 +lemma tagged_division_ofD[dest]: assumes "s tagged_division_of i"
   1.675 +  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}"
   1.676 +  "\<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) = {})"
   1.677 +  "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)" using assms unfolding tagged_division_of apply- by blast+
   1.678 +
   1.679 +lemma division_of_tagged_division: assumes"s tagged_division_of i"  shows "(snd ` s) division_of i"
   1.680 +proof(rule division_ofI) note assm=tagged_division_ofD[OF assms]
   1.681 +  show "\<Union>snd ` s = i" "finite (snd ` s)" using assm by auto
   1.682 +  fix k assume k:"k \<in> snd ` s" then obtain xk where xk:"(xk, k) \<in> s" by auto
   1.683 +  thus  "k \<subseteq> i" "k \<noteq> {}" "\<exists>a b. k = {a..b}" using assm apply- by fastsimp+
   1.684 +  fix k' assume k':"k' \<in> snd ` s" "k \<noteq> k'" from this(1) obtain xk' where xk':"(xk', k') \<in> s" by auto
   1.685 +  thus "interior k \<inter> interior k' = {}" apply-apply(rule assm(5)) apply(rule xk xk')+ using k' by auto
   1.686 +qed
   1.687 +
   1.688 +lemma partial_division_of_tagged_division: assumes "s tagged_partial_division_of i"
   1.689 +  shows "(snd ` s) division_of \<Union>(snd ` s)"
   1.690 +proof(rule division_ofI) note assm=tagged_partial_division_ofD[OF assms]
   1.691 +  show "finite (snd ` s)" "\<Union>snd ` s = \<Union>snd ` s" using assm by auto
   1.692 +  fix k assume k:"k \<in> snd ` s" then obtain xk where xk:"(xk, k) \<in> s" by auto
   1.693 +  thus "k\<noteq>{}" "\<exists>a b. k = {a..b}" "k \<subseteq> \<Union>snd ` s" using assm by auto
   1.694 +  fix k' assume k':"k' \<in> snd ` s" "k \<noteq> k'" from this(1) obtain xk' where xk':"(xk', k') \<in> s" by auto
   1.695 +  thus "interior k \<inter> interior k' = {}" apply-apply(rule assm(5)) apply(rule xk xk')+ using k' by auto
   1.696 +qed
   1.697 +
   1.698 +lemma tagged_partial_division_subset: assumes "s tagged_partial_division_of i" "t \<subseteq> s"
   1.699 +  shows "t tagged_partial_division_of i"
   1.700 +  using assms unfolding tagged_partial_division_of_def using finite_subset[OF assms(2)] by blast
   1.701 +
   1.702 +lemma setsum_over_tagged_division_lemma: fixes d::"(real^'m) set \<Rightarrow> 'a::real_normed_vector"
   1.703 +  assumes "p tagged_division_of i" "\<And>u v. {u..v} \<noteq> {} \<Longrightarrow> content {u..v} = 0 \<Longrightarrow> d {u..v} = 0"
   1.704 +  shows "setsum (\<lambda>(x,k). d k) p = setsum d (snd ` p)"
   1.705 +proof- note assm=tagged_division_ofD[OF assms(1)]
   1.706 +  have *:"(\<lambda>(x,k). d k) = d \<circ> snd" unfolding o_def apply(rule ext) by auto
   1.707 +  show ?thesis unfolding * apply(subst eq_commute) proof(rule setsum_reindex_nonzero)
   1.708 +    show "finite p" using assm by auto
   1.709 +    fix x y assume as:"x\<in>p" "y\<in>p" "x\<noteq>y" "snd x = snd y" 
   1.710 +    obtain a b where ab:"snd x = {a..b}" using assm(4)[of "fst x" "snd x"] as(1) by auto
   1.711 +    have "(fst x, snd y) \<in> p" "(fst x, snd y) \<noteq> y" unfolding as(4)[THEN sym] using as(1-3) by auto
   1.712 +    hence "interior (snd x) \<inter> interior (snd y) = {}" apply-apply(rule assm(5)[of "fst x" _ "fst y"]) using as by auto 
   1.713 +    hence "content {a..b} = 0" unfolding as(4)[THEN sym] ab content_eq_0_interior by auto
   1.714 +    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
   1.715 +    thus "d (snd x) = 0" unfolding ab by auto qed qed
   1.716 +
   1.717 +lemma tag_in_interval: "p tagged_division_of i \<Longrightarrow> (x,k) \<in> p \<Longrightarrow> x \<in> i" by auto
   1.718 +
   1.719 +lemma tagged_division_of_empty: "{} tagged_division_of {}"
   1.720 +  unfolding tagged_division_of by auto
   1.721 +
   1.722 +lemma tagged_partial_division_of_trivial[simp]:
   1.723 + "p tagged_partial_division_of {} \<longleftrightarrow> p = {}"
   1.724 +  unfolding tagged_partial_division_of_def by auto
   1.725 +
   1.726 +lemma tagged_division_of_trivial[simp]:
   1.727 + "p tagged_division_of {} \<longleftrightarrow> p = {}"
   1.728 +  unfolding tagged_division_of by auto
   1.729 +
   1.730 +lemma tagged_division_of_self:
   1.731 + "x \<in> {a..b} \<Longrightarrow> {(x,{a..b})} tagged_division_of {a..b}"
   1.732 +  apply(rule tagged_division_ofI) by auto
   1.733 +
   1.734 +lemma tagged_division_union:
   1.735 +  assumes "p1 tagged_division_of s1"  "p2 tagged_division_of s2" "interior s1 \<inter> interior s2 = {}"
   1.736 +  shows "(p1 \<union> p2) tagged_division_of (s1 \<union> s2)"
   1.737 +proof(rule tagged_division_ofI) note p1=tagged_division_ofD[OF assms(1)] and p2=tagged_division_ofD[OF assms(2)]
   1.738 +  show "finite (p1 \<union> p2)" using p1(1) p2(1) by auto
   1.739 +  show "\<Union>{k. \<exists>x. (x, k) \<in> p1 \<union> p2} = s1 \<union> s2" using p1(6) p2(6) by blast
   1.740 +  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
   1.741 +  show "k\<subseteq>s1\<union>s2" using xk p1(3) p2(3) by blast
   1.742 +  fix x' k' assume xk':"(x',k')\<in>p1\<union>p2" "(x,k) \<noteq> (x',k')"
   1.743 +  have *:"\<And>a b. a\<subseteq> s1 \<Longrightarrow> b\<subseteq> s2 \<Longrightarrow> interior a \<inter> interior b = {}" using assms(3) subset_interior by blast
   1.744 +  show "interior k \<inter> interior k' = {}" apply(cases "(x,k)\<in>p1", case_tac[!] "(x',k')\<in>p1")
   1.745 +    apply(rule p1(5)) prefer 4 apply(rule *) prefer 6 apply(subst Int_commute,rule *) prefer 8 apply(rule p2(5))
   1.746 +    using p1(3) p2(3) using xk xk' by auto qed 
   1.747 +
   1.748 +lemma tagged_division_unions:
   1.749 +  assumes "finite iset" "\<forall>i\<in>iset. (pfn(i) tagged_division_of i)"
   1.750 +  "\<forall>i1 \<in> iset. \<forall>i2 \<in> iset. ~(i1 = i2) \<longrightarrow> (interior(i1) \<inter> interior(i2) = {})"
   1.751 +  shows "\<Union>(pfn ` iset) tagged_division_of (\<Union>iset)"
   1.752 +proof(rule tagged_division_ofI)
   1.753 +  note assm = tagged_division_ofD[OF assms(2)[rule_format]]
   1.754 +  show "finite (\<Union>pfn ` iset)" apply(rule finite_Union) using assms by auto
   1.755 +  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 
   1.756 +  also have "\<dots> = \<Union>iset" using assm(6) by auto
   1.757 +  finally show "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>pfn ` iset} = \<Union>iset" . 
   1.758 +  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
   1.759 +  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
   1.760 +  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
   1.761 +  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)
   1.762 +    using assms(3)[rule_format] subset_interior by blast
   1.763 +  show "interior k \<inter> interior k' = {}" apply(cases "i=i'")
   1.764 +    using assm(5)[OF i _ xk'(2)]  i'(2) using assm(3)[OF i] assm(3)[OF i'] defer apply-apply(rule *) by auto
   1.765 +qed
   1.766 +
   1.767 +lemma tagged_partial_division_of_union_self:
   1.768 +  assumes "p tagged_partial_division_of s" shows "p tagged_division_of (\<Union>(snd ` p))"
   1.769 +  apply(rule tagged_division_ofI) using tagged_partial_division_ofD[OF assms] by auto
   1.770 +
   1.771 +lemma tagged_division_of_union_self: assumes "p tagged_division_of s"
   1.772 +  shows "p tagged_division_of (\<Union>(snd ` p))"
   1.773 +  apply(rule tagged_division_ofI) using tagged_division_ofD[OF assms] by auto
   1.774 +
   1.775 +subsection {* Fine-ness of a partition w.r.t. a gauge. *}
   1.776 +
   1.777 +definition fine (infixr "fine" 46) where
   1.778 +  "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d(x))"
   1.779 +
   1.780 +lemma fineI: assumes "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
   1.781 +  shows "d fine s" using assms unfolding fine_def by auto
   1.782 +
   1.783 +lemma fineD[dest]: assumes "d fine s"
   1.784 +  shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x" using assms unfolding fine_def by auto
   1.785 +
   1.786 +lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
   1.787 +  unfolding fine_def by auto
   1.788 +
   1.789 +lemma fine_inters:
   1.790 + "(\<lambda>x. \<Inter> {f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
   1.791 +  unfolding fine_def by blast
   1.792 +
   1.793 +lemma fine_union:
   1.794 +  "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
   1.795 +  unfolding fine_def by blast
   1.796 +
   1.797 +lemma fine_unions:"(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
   1.798 +  unfolding fine_def by auto
   1.799 +
   1.800 +lemma fine_subset:  "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
   1.801 +  unfolding fine_def by blast
   1.802 +
   1.803 +subsection {* Gauge integral. Define on compact intervals first, then use a limit. *}
   1.804 +
   1.805 +definition has_integral_compact_interval (infixr "has'_integral'_compact'_interval" 46) where
   1.806 +  "(f has_integral_compact_interval y) i \<equiv>
   1.807 +        (\<forall>e>0. \<exists>d. gauge d \<and>
   1.808 +          (\<forall>p. p tagged_division_of i \<and> d fine p
   1.809 +                        \<longrightarrow> norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - y) < e))"
   1.810 +
   1.811 +definition has_integral (infixr "has'_integral" 46) where 
   1.812 +"((f::(real^'n \<Rightarrow> 'b::real_normed_vector)) has_integral y) i \<equiv>
   1.813 +        if (\<exists>a b. i = {a..b}) then (f has_integral_compact_interval y) i
   1.814 +        else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
   1.815 +              \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral_compact_interval z) {a..b} \<and>
   1.816 +                                       norm(z - y) < e))"
   1.817 +
   1.818 +lemma has_integral:
   1.819 + "(f has_integral y) ({a..b}) \<longleftrightarrow>
   1.820 +        (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p
   1.821 +                        \<longrightarrow> norm(setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
   1.822 +  unfolding has_integral_def has_integral_compact_interval_def by auto
   1.823 +
   1.824 +lemma has_integralD[dest]: assumes
   1.825 + "(f has_integral y) ({a..b})" "e>0"
   1.826 +  obtains d where "gauge d" "\<And>p. p tagged_division_of {a..b} \<Longrightarrow> d fine p
   1.827 +                        \<Longrightarrow> norm(setsum (\<lambda>(x,k). content(k) *\<^sub>R f(x)) p - y) < e"
   1.828 +  using assms unfolding has_integral by auto
   1.829 +
   1.830 +lemma has_integral_alt:
   1.831 + "(f has_integral y) i \<longleftrightarrow>
   1.832 +      (if (\<exists>a b. i = {a..b}) then (f has_integral y) i
   1.833 +       else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
   1.834 +                               \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0)
   1.835 +                                        has_integral z) ({a..b}) \<and>
   1.836 +                                       norm(z - y) < e)))"
   1.837 +  unfolding has_integral unfolding has_integral_compact_interval_def has_integral_def by auto
   1.838 +
   1.839 +lemma has_integral_altD:
   1.840 +  assumes "(f has_integral y) i" "\<not> (\<exists>a b. i = {a..b})" "e>0"
   1.841 +  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)"
   1.842 +  using assms unfolding has_integral unfolding has_integral_compact_interval_def has_integral_def by auto
   1.843 +
   1.844 +definition integrable_on (infixr "integrable'_on" 46) where
   1.845 +  "(f integrable_on i) \<equiv> \<exists>y. (f has_integral y) i"
   1.846 +
   1.847 +definition "integral i f \<equiv> SOME y. (f has_integral y) i"
   1.848 +
   1.849 +lemma integrable_integral[dest]:
   1.850 + "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
   1.851 +  unfolding integrable_on_def integral_def by(rule someI_ex)
   1.852 +
   1.853 +lemma has_integral_integrable[intro]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
   1.854 +  unfolding integrable_on_def by auto
   1.855 +
   1.856 +lemma has_integral_integral:"f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
   1.857 +  by auto
   1.858 +
   1.859 +lemma has_integral_vec1: assumes "(f has_integral k) {a..b}"
   1.860 +  shows "((\<lambda>x. vec1 (f x)) has_integral (vec1 k)) {a..b}"
   1.861 +proof- have *:"\<And>p. (\<Sum>(x, k)\<in>p. content k *\<^sub>R vec1 (f x)) - vec1 k = vec1 ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k)"
   1.862 +    unfolding vec_sub Cart_eq by(auto simp add:vec1_dest_vec1_simps split_beta)
   1.863 +  show ?thesis using assms unfolding has_integral apply safe
   1.864 +    apply(erule_tac x=e in allE,safe) apply(rule_tac x=d in exI,safe)
   1.865 +    apply(erule_tac x=p in allE,safe) unfolding * norm_vector_1 by auto qed
   1.866 +
   1.867 +lemma setsum_content_null:
   1.868 +  assumes "content({a..b}) = 0" "p tagged_division_of {a..b}"
   1.869 +  shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
   1.870 +proof(rule setsum_0',rule) fix y assume y:"y\<in>p"
   1.871 +  obtain x k where xk:"y = (x,k)" using surj_pair[of y] by blast
   1.872 +  note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
   1.873 +  from this(2) guess c .. then guess d .. note c_d=this
   1.874 +  have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x" unfolding xk by auto
   1.875 +  also have "\<dots> = 0" using content_subset[OF assm(1)[unfolded c_d]] content_pos_le[of c d]
   1.876 +    unfolding assms(1) c_d by auto
   1.877 +  finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
   1.878 +qed
   1.879 +
   1.880 +subsection {* Some basic combining lemmas. *}
   1.881 +
   1.882 +lemma tagged_division_unions_exists:
   1.883 +  assumes "finite iset" "\<forall>i \<in> iset. \<exists>p. p tagged_division_of i \<and> d fine p"
   1.884 +  "\<forall>i1\<in>iset. \<forall>i2\<in>iset. ~(i1 = i2) \<longrightarrow> (interior(i1) \<inter> interior(i2) = {})" "(\<Union>iset = i)"
   1.885 +   obtains p where "p tagged_division_of i" "d fine p"
   1.886 +proof- guess pfn using bchoice[OF assms(2)] .. note pfn = conjunctD2[OF this[rule_format]]
   1.887 +  show thesis apply(rule_tac p="\<Union>(pfn ` iset)" in that) unfolding assms(4)[THEN sym]
   1.888 +    apply(rule tagged_division_unions[OF assms(1) _ assms(3)]) defer 
   1.889 +    apply(rule fine_unions) using pfn by auto
   1.890 +qed
   1.891 +
   1.892 +subsection {* The set we're concerned with must be closed. *}
   1.893 +
   1.894 +lemma division_of_closed: "s division_of i \<Longrightarrow> closed (i::(real^'n) set)"
   1.895 +  unfolding division_of_def by(fastsimp intro!: closed_Union closed_interval)
   1.896 +
   1.897 +subsection {* General bisection principle for intervals; might be useful elsewhere. *}
   1.898 +
   1.899 +lemma interval_bisection_step:
   1.900 +  assumes "P {}" "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))" "~(P {a..b::real^'n})"
   1.901 +  obtains c d where "~(P{c..d})"
   1.902 +  "\<forall>i. 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"
   1.903 +proof- have "{a..b} \<noteq> {}" using assms(1,3) by auto
   1.904 +  note ab=this[unfolded interval_eq_empty not_ex not_less]
   1.905 +  { fix f have "finite f \<Longrightarrow>
   1.906 +        (\<forall>s\<in>f. P s) \<Longrightarrow>
   1.907 +        (\<forall>s\<in>f. \<exists>a b. s = {a..b}) \<Longrightarrow>
   1.908 +        (\<forall>s\<in>f.\<forall>t\<in>f. ~(s = t) \<longrightarrow> interior(s) \<inter> interior(t) = {}) \<Longrightarrow> P(\<Union>f)"
   1.909 +    proof(induct f rule:finite_induct)
   1.910 +      case empty show ?case using assms(1) by auto
   1.911 +    next case (insert x f) show ?case unfolding Union_insert apply(rule assms(2)[rule_format])
   1.912 +        apply rule defer apply rule defer apply(rule inter_interior_unions_intervals)
   1.913 +        using insert by auto
   1.914 +    qed } note * = this
   1.915 +  let ?A = "{{c..d} | c d. \<forall>i. (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)}"
   1.916 +  let ?PP = "\<lambda>c d. \<forall>i. 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"
   1.917 +  { presume "\<forall>c d. ?PP c d \<longrightarrow> P {c..d} \<Longrightarrow> False"
   1.918 +    thus thesis unfolding atomize_not not_all apply-apply(erule exE)+ apply(rule_tac c=x and d=xa in that) by auto }
   1.919 +  assume as:"\<forall>c d. ?PP c d \<longrightarrow> P {c..d}"
   1.920 +  have "P (\<Union> ?A)" proof(rule *, rule_tac[2-] ballI, rule_tac[4] ballI, rule_tac[4] impI) 
   1.921 +    let ?B = "(\<lambda>s.{(\<chi> i. if i \<in> s then a$i else (a$i + b$i) / 2) ..
   1.922 +      (\<chi> i. if i \<in> s then (a$i + b$i) / 2 else b$i)}) ` {s. s \<subseteq> UNIV}"
   1.923 +    have "?A \<subseteq> ?B" proof case goal1
   1.924 +      then guess c unfolding mem_Collect_eq .. then guess d apply- by(erule exE,(erule conjE)+) note c_d=this[rule_format]
   1.925 +      have *:"\<And>a b c d. a = c \<Longrightarrow> b = d \<Longrightarrow> {a..b} = {c..d}" by auto
   1.926 +      show "x\<in>?B" unfolding image_iff apply(rule_tac x="{i. c$i = a$i}" in bexI)
   1.927 +        unfolding c_d apply(rule * ) unfolding Cart_eq cond_component Cart_lambda_beta
   1.928 +      proof(rule_tac[1-2] allI) fix i show "c $ i = (if i \<in> {i. c $ i = a $ i} then a $ i else (a $ i + b $ i) / 2)"
   1.929 +          "d $ i = (if i \<in> {i. c $ i = a $ i} then (a $ i + b $ i) / 2 else b $ i)"
   1.930 +          using c_d(2)[of i] ab[THEN spec[where x=i]] by(auto simp add:field_simps)
   1.931 +      qed auto qed
   1.932 +    thus "finite ?A" apply(rule finite_subset[of _ ?B]) by auto
   1.933 +    fix s assume "s\<in>?A" then guess c unfolding mem_Collect_eq .. then guess d apply- by(erule exE,(erule conjE)+)
   1.934 +    note c_d=this[rule_format]
   1.935 +    show "P s" unfolding c_d apply(rule as[rule_format]) proof- case goal1 show ?case 
   1.936 +        using c_d(2)[of i] using ab[THEN spec[where x=i]] by auto qed
   1.937 +    show "\<exists>a b. s = {a..b}" unfolding c_d by auto
   1.938 +    fix t assume "t\<in>?A" then guess e unfolding mem_Collect_eq .. then guess f apply- by(erule exE,(erule conjE)+)
   1.939 +    note e_f=this[rule_format]
   1.940 +    assume "s \<noteq> t" hence "\<not> (c = e \<and> d = f)" unfolding c_d e_f by auto
   1.941 +    then obtain i where "c$i \<noteq> e$i \<or> d$i \<noteq> f$i" unfolding de_Morgan_conj Cart_eq by auto
   1.942 +    hence i:"c$i \<noteq> e$i" "d$i \<noteq> f$i" apply- apply(erule_tac[!] disjE)
   1.943 +    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
   1.944 +    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
   1.945 +    qed have *:"\<And>s t. (\<And>a. a\<in>s \<Longrightarrow> a\<in>t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}" by auto
   1.946 +    show "interior s \<inter> interior t = {}" unfolding e_f c_d interior_closed_interval proof(rule *)
   1.947 +      fix x assume "x\<in>{c<..<d}" "x\<in>{e<..<f}"
   1.948 +      hence x:"c$i < d$i" "e$i < f$i" "c$i < f$i" "e$i < d$i" unfolding mem_interval apply-apply(erule_tac[!] x=i in allE)+ by auto
   1.949 +      show False using c_d(2)[of i] apply- apply(erule_tac disjE)
   1.950 +      proof(erule_tac[!] conjE) assume as:"c $ i = a $ i" "d $ i = (a $ i + b $ i) / 2"
   1.951 +        show False using e_f(2)[of i] and i x unfolding as by(fastsimp simp add:field_simps)
   1.952 +      next assume as:"c $ i = (a $ i + b $ i) / 2" "d $ i = b $ i"
   1.953 +        show False using e_f(2)[of i] and i x unfolding as by(fastsimp simp add:field_simps)
   1.954 +      qed qed qed
   1.955 +  also have "\<Union> ?A = {a..b}" proof(rule set_ext,rule)
   1.956 +    fix x assume "x\<in>\<Union>?A" then guess Y unfolding Union_iff ..
   1.957 +    from this(1) guess c unfolding mem_Collect_eq .. then guess d ..
   1.958 +    note c_d = this[THEN conjunct2,rule_format] `x\<in>Y`[unfolded this[THEN conjunct1]]
   1.959 +    show "x\<in>{a..b}" unfolding mem_interval proof 
   1.960 +      fix i show "a $ i \<le> x $ i \<and> x $ i \<le> b $ i"
   1.961 +        using c_d(1)[of i] c_d(2)[unfolded mem_interval,THEN spec[where x=i]] by auto qed
   1.962 +  next fix x assume x:"x\<in>{a..b}"
   1.963 +    have "\<forall>i. \<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"
   1.964 +      (is "\<forall>i. \<exists>c d. ?P i c d") unfolding mem_interval proof fix i
   1.965 +      have "?P i (a$i) ((a $ i + b $ i) / 2) \<or> ?P i ((a $ i + b $ i) / 2) (b$i)"
   1.966 +        using x[unfolded mem_interval,THEN spec[where x=i]] by auto thus "\<exists>c d. ?P i c d" by blast
   1.967 +    qed thus "x\<in>\<Union>?A" unfolding Union_iff lambda_skolem unfolding Bex_def mem_Collect_eq
   1.968 +      apply-apply(erule exE)+ apply(rule_tac x="{xa..xaa}" in exI) unfolding mem_interval by auto
   1.969 +  qed finally show False using assms by auto qed
   1.970 +
   1.971 +lemma interval_bisection:
   1.972 +  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::real^'n}"
   1.973 +  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})"
   1.974 +proof-
   1.975 +  have "\<forall>x. \<exists>y. \<not> P {fst x..snd x} \<longrightarrow> (\<not> P {fst y..snd y} \<and> (\<forall>i. fst x$i \<le> fst y$i \<and> fst y$i \<le> snd y$i \<and> snd y$i \<le> snd x$i \<and>
   1.976 +                           2 * (snd y$i - fst y$i) \<le> snd x$i - fst x$i))" proof case goal1 thus ?case proof-
   1.977 +      presume "\<not> P {fst x..snd x} \<Longrightarrow> ?thesis"
   1.978 +      thus ?thesis apply(cases "P {fst x..snd x}") by auto
   1.979 +    next assume as:"\<not> P {fst x..snd x}" from interval_bisection_step[of P, OF assms(1-2) as] guess c d . 
   1.980 +      thus ?thesis apply- apply(rule_tac x="(c,d)" in exI) by auto
   1.981 +    qed qed then guess f apply-apply(drule choice) by(erule exE) note f=this
   1.982 +  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
   1.983 +  have "A 0 = a" "B 0 = b" "\<And>n. \<not> P {A(Suc n)..B(Suc n)} \<and>
   1.984 +    (\<forall>i. 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> 
   1.985 +    2 * (B(Suc n)$i - A(Suc n)$i) \<le> B(n)$i - A(n)$i)" (is "\<And>n. ?P n")
   1.986 +  proof- show "A 0 = a" "B 0 = b" unfolding ab_def by auto
   1.987 +    case goal3 note S = ab_def funpow.simps o_def id_apply show ?case
   1.988 +    proof(induct n) case 0 thus ?case unfolding S apply(rule f[rule_format]) using assms(3) by auto
   1.989 +    next case (Suc n) show ?case unfolding S apply(rule f[rule_format]) using Suc unfolding S by auto
   1.990 +    qed qed note AB = this(1-2) conjunctD2[OF this(3),rule_format]
   1.991 +
   1.992 +  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"
   1.993 +  proof- case goal1 guess n using real_arch_pow2[of "(setsum (\<lambda>i. b$i - a$i) UNIV) / e"] .. note n=this
   1.994 +    show ?case apply(rule_tac x=n in exI) proof(rule,rule)
   1.995 +      fix x y assume xy:"x\<in>{A n..B n}" "y\<in>{A n..B n}"
   1.996 +      have "dist x y \<le> setsum (\<lambda>i. abs((x - y)$i)) UNIV" unfolding vector_dist_norm by(rule norm_le_l1)
   1.997 +      also have "\<dots> \<le> setsum (\<lambda>i. B n$i - A n$i) UNIV"
   1.998 +      proof(rule setsum_mono) fix i show "\<bar>(x - y) $ i\<bar> \<le> B n $ i - A n $ i"
   1.999 +          using xy[unfolded mem_interval,THEN spec[where x=i]]
  1.1000 +          unfolding vector_minus_component by auto qed
  1.1001 +      also have "\<dots> \<le> setsum (\<lambda>i. b$i - a$i) UNIV / 2^n" unfolding setsum_divide_distrib
  1.1002 +      proof(rule setsum_mono) case goal1 thus ?case
  1.1003 +        proof(induct n) case 0 thus ?case unfolding AB by auto
  1.1004 +        next case (Suc n) have "B (Suc n) $ i - A (Suc n) $ i \<le> (B n $ i - A n $ i) / 2" using AB(4)[of n i] by auto
  1.1005 +          also have "\<dots> \<le> (b $ i - a $ i) / 2 ^ Suc n" using Suc by(auto simp add:field_simps) finally show ?case .
  1.1006 +        qed qed
  1.1007 +      also have "\<dots> < e" using n using goal1 by(auto simp add:field_simps) finally show "dist x y < e" .
  1.1008 +    qed qed
  1.1009 +  { fix n m ::nat assume "m \<le> n" then guess d unfolding le_Suc_ex_iff .. note d=this
  1.1010 +    have "{A n..B n} \<subseteq> {A m..B m}" unfolding d 
  1.1011 +    proof(induct d) case 0 thus ?case by auto
  1.1012 +    next case (Suc d) show ?case apply(rule subset_trans[OF _ Suc])
  1.1013 +        apply(rule) unfolding mem_interval apply(rule,erule_tac x=i in allE)
  1.1014 +      proof- case goal1 thus ?case using AB(4)[of "m + d" i] by(auto simp add:field_simps)
  1.1015 +      qed qed } note ABsubset = this 
  1.1016 +  have "\<exists>a. \<forall>n. a\<in>{A n..B n}" apply(rule decreasing_closed_nest[rule_format,OF closed_interval _ ABsubset interv])
  1.1017 +  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
  1.1018 +  then guess x0 .. note x0=this[rule_format]
  1.1019 +  show thesis proof(rule that[rule_format,of x0])
  1.1020 +    show "x0\<in>{a..b}" using x0[of 0] unfolding AB .
  1.1021 +    fix e assume "0 < (e::real)" from interv[OF this] guess n .. note n=this
  1.1022 +    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}"
  1.1023 +      apply(rule_tac x="A n" in exI,rule_tac x="B n" in exI) apply(rule,rule x0) apply rule defer 
  1.1024 +    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
  1.1025 +      show "{A n..B n} \<subseteq> ball x0 e" using n using x0[of n] by auto
  1.1026 +      show "{A n..B n} \<subseteq> {a..b}" unfolding AB(1-2)[symmetric] apply(rule ABsubset) by auto
  1.1027 +    qed qed qed 
  1.1028 +
  1.1029 +subsection {* Cousin's lemma. *}
  1.1030 +
  1.1031 +lemma fine_division_exists: assumes "gauge g" 
  1.1032 +  obtains p where "p tagged_division_of {a..b::real^'n}" "g fine p"
  1.1033 +proof- presume "\<not> (\<exists>p. p tagged_division_of {a..b} \<and> g fine p) \<Longrightarrow> False"
  1.1034 +  then guess p unfolding atomize_not not_not .. thus thesis apply-apply(rule that[of p]) by auto
  1.1035 +next assume as:"\<not> (\<exists>p. p tagged_division_of {a..b} \<and> g fine p)"
  1.1036 +  guess x apply(rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p",rule_format,OF _ _ as])
  1.1037 +    apply(rule_tac x="{}" in exI) defer apply(erule conjE exE)+
  1.1038 +  proof- show "{} tagged_division_of {} \<and> g fine {}" unfolding fine_def by auto
  1.1039 +    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 = {}"
  1.1040 +    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
  1.1041 +      apply(rule tagged_division_union) prefer 4 apply(rule fine_union) by auto
  1.1042 +  qed note x=this
  1.1043 +  obtain e where e:"e>0" "ball x e \<subseteq> g x" using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
  1.1044 +  from x(2)[OF e(1)] guess c d apply-apply(erule exE conjE)+ . note c_d = this
  1.1045 +  have "g fine {(x, {c..d})}" unfolding fine_def using e using c_d(2) by auto
  1.1046 +  thus False using tagged_division_of_self[OF c_d(1)] using c_d by auto qed
  1.1047 +
  1.1048 +subsection {* Basic theorems about integrals. *}
  1.1049 +
  1.1050 +lemma has_integral_unique: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
  1.1051 +  assumes "(f has_integral k1) i" "(f has_integral k2) i" shows "k1 = k2"
  1.1052 +proof(rule ccontr) let ?e = "norm(k1 - k2) / 2" assume as:"k1 \<noteq> k2" hence e:"?e > 0" by auto
  1.1053 +  have lem:"\<And>f::real^'n \<Rightarrow> 'a.  \<And> a b k1 k2.
  1.1054 +    (f has_integral k1) ({a..b}) \<Longrightarrow> (f has_integral k2) ({a..b}) \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> False"
  1.1055 +  proof- case goal1 let ?e = "norm(k1 - k2) / 2" from goal1(3) have e:"?e > 0" by auto
  1.1056 +    guess d1 by(rule has_integralD[OF goal1(1) e]) note d1=this
  1.1057 +    guess d2 by(rule has_integralD[OF goal1(2) e]) note d2=this
  1.1058 +    guess p by(rule fine_division_exists[OF gauge_inter[OF d1(1) d2(1)],of a b]) note p=this
  1.1059 +    let ?c = "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" have "norm (k1 - k2) \<le> norm (?c - k2) + norm (?c - k1)"
  1.1060 +      using norm_triangle_ineq4[of "k1 - ?c" "k2 - ?c"] by(auto simp add:group_simps norm_minus_commute)
  1.1061 +    also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
  1.1062 +      apply(rule add_strict_mono) apply(rule_tac[!] d2(2) d1(2)) using p unfolding fine_def by auto
  1.1063 +    finally show False by auto
  1.1064 +  qed { presume "\<not> (\<exists>a b. i = {a..b}) \<Longrightarrow> False"
  1.1065 +    thus False apply-apply(cases "\<exists>a b. i = {a..b}")
  1.1066 +      using assms by(auto simp add:has_integral intro:lem[OF _ _ as]) }
  1.1067 +  assume as:"\<not> (\<exists>a b. i = {a..b})"
  1.1068 +  guess B1 by(rule has_integral_altD[OF assms(1) as,OF e]) note B1=this[rule_format]
  1.1069 +  guess B2 by(rule has_integral_altD[OF assms(2) as,OF e]) note B2=this[rule_format]
  1.1070 +  have "\<exists>a b::real^'n. ball 0 B1 \<union> ball 0 B2 \<subseteq> {a..b}" apply(rule bounded_subset_closed_interval)
  1.1071 +    using bounded_Un bounded_ball by auto then guess a b apply-by(erule exE)+
  1.1072 +  note ab=conjunctD2[OF this[unfolded Un_subset_iff]]
  1.1073 +  guess w using B1(2)[OF ab(1)] .. note w=conjunctD2[OF this]
  1.1074 +  guess z using B2(2)[OF ab(2)] .. note z=conjunctD2[OF this]
  1.1075 +  have "z = w" using lem[OF w(1) z(1)] by auto
  1.1076 +  hence "norm (k1 - k2) \<le> norm (z - k2) + norm (w - k1)"
  1.1077 +    using norm_triangle_ineq4[of "k1 - w" "k2 - z"] by(auto simp add: norm_minus_commute) 
  1.1078 +  also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2" apply(rule add_strict_mono) by(rule_tac[!] z(2) w(2))
  1.1079 +  finally show False by auto qed
  1.1080 +
  1.1081 +lemma integral_unique[intro]:
  1.1082 +  "(f has_integral y) k \<Longrightarrow> integral k f = y"
  1.1083 +  unfolding integral_def apply(rule some_equality) by(auto intro: has_integral_unique) 
  1.1084 +
  1.1085 +lemma has_integral_is_0: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector" 
  1.1086 +  assumes "\<forall>x\<in>s. f x = 0" shows "(f has_integral 0) s"
  1.1087 +proof- have lem:"\<And>a b. \<And>f::real^'n \<Rightarrow> 'a.
  1.1088 +    (\<forall>x\<in>{a..b}. f(x) = 0) \<Longrightarrow> (f has_integral 0) ({a..b})" unfolding has_integral
  1.1089 +  proof(rule,rule) fix a b e and f::"real^'n \<Rightarrow> 'a"
  1.1090 +    assume as:"\<forall>x\<in>{a..b}. f x = 0" "0 < (e::real)"
  1.1091 +    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)"
  1.1092 +      apply(rule_tac x="\<lambda>x. ball x 1" in exI)  apply(rule,rule gaugeI) unfolding centre_in_ball defer apply(rule open_ball)
  1.1093 +    proof(rule,rule,erule conjE) case goal1
  1.1094 +      have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) = 0" proof(rule setsum_0',rule)
  1.1095 +        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
  1.1096 +        thus "(\<lambda>(x, k). content k *\<^sub>R f x) x = 0" apply(subst surjective_pairing[of x]) unfolding split_conv by auto
  1.1097 +      qed thus ?case using as by auto
  1.1098 +    qed auto qed  { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
  1.1099 +    thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}")
  1.1100 +      using assms by(auto simp add:has_integral intro:lem) }
  1.1101 +  have *:"(\<lambda>x. if x \<in> s then f x else 0) = (\<lambda>x. 0)" apply(rule ext) using assms by auto
  1.1102 +  assume "\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P *
  1.1103 +  apply(rule,rule,rule_tac x=1 in exI,rule) defer apply(rule,rule,rule)
  1.1104 +  proof- fix e::real and a b assume "e>0"
  1.1105 +    thus "\<exists>z. ((\<lambda>x::real^'n. 0::'a) has_integral z) {a..b} \<and> norm (z - 0) < e"
  1.1106 +      apply(rule_tac x=0 in exI) apply(rule,rule lem) by auto
  1.1107 +  qed auto qed
  1.1108 +
  1.1109 +lemma has_integral_0[simp]: "((\<lambda>x::real^'n. 0) has_integral 0) s"
  1.1110 +  apply(rule has_integral_is_0) by auto 
  1.1111 +
  1.1112 +lemma has_integral_0_eq[simp]: "((\<lambda>x. 0) has_integral i) s \<longleftrightarrow> i = 0"
  1.1113 +  using has_integral_unique[OF has_integral_0] by auto
  1.1114 +
  1.1115 +lemma has_integral_linear: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
  1.1116 +  assumes "(f has_integral y) s" "bounded_linear h" shows "((h o f) has_integral ((h y))) s"
  1.1117 +proof- interpret bounded_linear h using assms(2) . from pos_bounded guess B .. note B=conjunctD2[OF this,rule_format]
  1.1118 +  have lem:"\<And>f::real^'n \<Rightarrow> 'a. \<And> y a b.
  1.1119 +    (f has_integral y) ({a..b}) \<Longrightarrow> ((h o f) has_integral h(y)) ({a..b})"
  1.1120 +  proof(subst has_integral,rule,rule) case goal1
  1.1121 +    from pos_bounded guess B .. note B=conjunctD2[OF this,rule_format]
  1.1122 +    have *:"e / B > 0" apply(rule divide_pos_pos) using goal1(2) B by auto
  1.1123 +    guess g using has_integralD[OF goal1(1) *] . note g=this
  1.1124 +    show ?case apply(rule_tac x=g in exI) apply(rule,rule g(1))
  1.1125 +    proof(rule,rule,erule conjE) fix p assume as:"p tagged_division_of {a..b}" "g fine p" 
  1.1126 +      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
  1.1127 +      have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = setsum (h \<circ> (\<lambda>(x, k). content k *\<^sub>R f x)) p"
  1.1128 +        unfolding o_def unfolding scaleR[THEN sym] * by simp
  1.1129 +      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
  1.1130 +      finally have *:"(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" .
  1.1131 +      show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) - h y) < e" unfolding * diff[THEN sym]
  1.1132 +        apply(rule le_less_trans[OF B(2)]) using g(2)[OF as] B(1) by(auto simp add:field_simps)
  1.1133 +    qed qed { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
  1.1134 +    thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}") using assms by(auto simp add:has_integral intro!:lem) }
  1.1135 +  assume as:"\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P
  1.1136 +  proof(rule,rule) fix e::real  assume e:"0<e"
  1.1137 +    have *:"0 < e/B" by(rule divide_pos_pos,rule e,rule B(1))
  1.1138 +    guess M using has_integral_altD[OF assms(1) as *,rule_format] . note M=this
  1.1139 +    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)"
  1.1140 +      apply(rule_tac x=M in exI) apply(rule,rule M(1))
  1.1141 +    proof(rule,rule,rule) case goal1 guess z using M(2)[OF goal1(1)] .. note z=conjunctD2[OF this]
  1.1142 +      have *:"(\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) = h \<circ> (\<lambda>x. if x \<in> s then f x else 0)"
  1.1143 +        unfolding o_def apply(rule ext) using zero by auto
  1.1144 +      show ?case apply(rule_tac x="h z" in exI,rule) unfolding * apply(rule lem[OF z(1)]) unfolding diff[THEN sym]
  1.1145 +        apply(rule le_less_trans[OF B(2)]) using B(1) z(2) by(auto simp add:field_simps)
  1.1146 +    qed qed qed
  1.1147 +
  1.1148 +lemma has_integral_cmul:
  1.1149 +  shows "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_integral (c *\<^sub>R k)) s"
  1.1150 +  unfolding o_def[THEN sym] apply(rule has_integral_linear,assumption)
  1.1151 +  by(rule scaleR.bounded_linear_right)
  1.1152 +
  1.1153 +lemma has_integral_neg:
  1.1154 +  shows "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. -(f x)) has_integral (-k)) s"
  1.1155 +  apply(drule_tac c="-1" in has_integral_cmul) by auto
  1.1156 +
  1.1157 +lemma has_integral_add: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector" 
  1.1158 +  assumes "(f has_integral k) s" "(g has_integral l) s"
  1.1159 +  shows "((\<lambda>x. f x + g x) has_integral (k + l)) s"
  1.1160 +proof- have lem:"\<And>f g::real^'n \<Rightarrow> 'a. \<And>a b k l.
  1.1161 +    (f has_integral k) ({a..b}) \<Longrightarrow> (g has_integral l) ({a..b}) \<Longrightarrow>
  1.1162 +     ((\<lambda>x. f(x) + g(x)) has_integral (k + l)) ({a..b})" proof- case goal1
  1.1163 +    show ?case unfolding has_integral proof(rule,rule) fix e::real assume e:"e>0" hence *:"e/2>0" by auto
  1.1164 +      guess d1 using has_integralD[OF goal1(1) *] . note d1=this
  1.1165 +      guess d2 using has_integralD[OF goal1(2) *] . note d2=this
  1.1166 +      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)"
  1.1167 +        apply(rule_tac x="\<lambda>x. (d1 x) \<inter> (d2 x)" in exI) apply(rule,rule gauge_inter[OF d1(1) d2(1)])
  1.1168 +      proof(rule,rule,erule conjE) fix p assume as:"p tagged_division_of {a..b}" "(\<lambda>x. d1 x \<inter> d2 x) fine p"
  1.1169 +        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)"
  1.1170 +          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]
  1.1171 +          by(rule setsum_cong2,auto)
  1.1172 +        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))"
  1.1173 +          unfolding * by(auto simp add:group_simps) also let ?res = "\<dots>"
  1.1174 +        from as have *:"d1 fine p" "d2 fine p" unfolding fine_inter by auto
  1.1175 +        have "?res < e/2 + e/2" apply(rule le_less_trans[OF norm_triangle_ineq])
  1.1176 +          apply(rule add_strict_mono) using d1(2)[OF as(1) *(1)] and d2(2)[OF as(1) *(2)] by auto
  1.1177 +        finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e" by auto
  1.1178 +      qed qed qed { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
  1.1179 +    thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}") using assms by(auto simp add:has_integral intro!:lem) }
  1.1180 +  assume as:"\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P
  1.1181 +  proof(rule,rule) case goal1 hence *:"e/2 > 0" by auto
  1.1182 +    from has_integral_altD[OF assms(1) as *] guess B1 . note B1=this[rule_format]
  1.1183 +    from has_integral_altD[OF assms(2) as *] guess B2 . note B2=this[rule_format]
  1.1184 +    show ?case apply(rule_tac x="max B1 B2" in exI) apply(rule,rule min_max.less_supI1,rule B1)
  1.1185 +    proof(rule,rule,rule) fix a b assume "ball 0 (max B1 B2) \<subseteq> {a..b::real^'n}"
  1.1186 +      hence *:"ball 0 B1 \<subseteq> {a..b::real^'n}" "ball 0 B2 \<subseteq> {a..b::real^'n}" by auto
  1.1187 +      guess w using B1(2)[OF *(1)] .. note w=conjunctD2[OF this]
  1.1188 +      guess z using B2(2)[OF *(2)] .. note z=conjunctD2[OF this]
  1.1189 +      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
  1.1190 +      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"
  1.1191 +        apply(rule_tac x="w + z" in exI) apply(rule,rule lem[OF w(1) z(1), unfolded *[THEN sym]])
  1.1192 +        using norm_triangle_ineq[of "w - k" "z - l"] w(2) z(2) by(auto simp add:field_simps)
  1.1193 +    qed qed qed
  1.1194 +
  1.1195 +lemma has_integral_sub:
  1.1196 +  shows "(f has_integral k) s \<Longrightarrow> (g has_integral l) s \<Longrightarrow> ((\<lambda>x. f(x) - g(x)) has_integral (k - l)) s"
  1.1197 +  using has_integral_add[OF _ has_integral_neg,of f k s g l] unfolding group_simps by auto
  1.1198 +
  1.1199 +lemma integral_0: "integral s (\<lambda>x::real^'n. 0::real^'m) = 0"
  1.1200 +  by(rule integral_unique has_integral_0)+
  1.1201 +
  1.1202 +lemma integral_add:
  1.1203 +  shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
  1.1204 +   integral s (\<lambda>x. f x + g x) = integral s f + integral s g"
  1.1205 +  apply(rule integral_unique) apply(drule integrable_integral)+
  1.1206 +  apply(rule has_integral_add) by assumption+
  1.1207 +
  1.1208 +lemma integral_cmul:
  1.1209 +  shows "f integrable_on s \<Longrightarrow> integral s (\<lambda>x. c *\<^sub>R f x) = c *\<^sub>R integral s f"
  1.1210 +  apply(rule integral_unique) apply(drule integrable_integral)+
  1.1211 +  apply(rule has_integral_cmul) by assumption+
  1.1212 +
  1.1213 +lemma integral_neg:
  1.1214 +  shows "f integrable_on s \<Longrightarrow> integral s (\<lambda>x. - f x) = - integral s f"
  1.1215 +  apply(rule integral_unique) apply(drule integrable_integral)+
  1.1216 +  apply(rule has_integral_neg) by assumption+
  1.1217 +
  1.1218 +lemma integral_sub:
  1.1219 +  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"
  1.1220 +  apply(rule integral_unique) apply(drule integrable_integral)+
  1.1221 +  apply(rule has_integral_sub) by assumption+
  1.1222 +
  1.1223 +lemma integrable_0: "(\<lambda>x. 0) integrable_on s"
  1.1224 +  unfolding integrable_on_def using has_integral_0 by auto
  1.1225 +
  1.1226 +lemma integrable_add:
  1.1227 +  shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x + g x) integrable_on s"
  1.1228 +  unfolding integrable_on_def by(auto intro: has_integral_add)
  1.1229 +
  1.1230 +lemma integrable_cmul:
  1.1231 +  shows "f integrable_on s \<Longrightarrow> (\<lambda>x. c *\<^sub>R f(x)) integrable_on s"
  1.1232 +  unfolding integrable_on_def by(auto intro: has_integral_cmul)
  1.1233 +
  1.1234 +lemma integrable_neg:
  1.1235 +  shows "f integrable_on s \<Longrightarrow> (\<lambda>x. -f(x)) integrable_on s"
  1.1236 +  unfolding integrable_on_def by(auto intro: has_integral_neg)
  1.1237 +
  1.1238 +lemma integrable_sub:
  1.1239 +  shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x - g x) integrable_on s"
  1.1240 +  unfolding integrable_on_def by(auto intro: has_integral_sub)
  1.1241 +
  1.1242 +lemma integrable_linear:
  1.1243 +  shows "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> (h o f) integrable_on s"
  1.1244 +  unfolding integrable_on_def by(auto intro: has_integral_linear)
  1.1245 +
  1.1246 +lemma integral_linear:
  1.1247 +  shows "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> integral s (h o f) = h(integral s f)"
  1.1248 +  apply(rule has_integral_unique) defer unfolding has_integral_integral 
  1.1249 +  apply(drule has_integral_linear,assumption,assumption) unfolding has_integral_integral[THEN sym]
  1.1250 +  apply(rule integrable_linear) by assumption+
  1.1251 +
  1.1252 +lemma has_integral_setsum:
  1.1253 +  assumes "finite t" "\<forall>a\<in>t. ((f a) has_integral (i a)) s"
  1.1254 +  shows "((\<lambda>x. setsum (\<lambda>a. f a x) t) has_integral (setsum i t)) s"
  1.1255 +proof(insert assms(1) subset_refl[of t],induct rule:finite_subset_induct)
  1.1256 +  case (insert x F) show ?case unfolding setsum_insert[OF insert(1,3)]
  1.1257 +    apply(rule has_integral_add) using insert assms by auto
  1.1258 +qed auto
  1.1259 +
  1.1260 +lemma integral_setsum:
  1.1261 +  shows "finite t \<Longrightarrow> \<forall>a\<in>t. (f a) integrable_on s \<Longrightarrow>
  1.1262 +  integral s (\<lambda>x. setsum (\<lambda>a. f a x) t) = setsum (\<lambda>a. integral s (f a)) t"
  1.1263 +  apply(rule integral_unique) apply(rule has_integral_setsum)
  1.1264 +  using integrable_integral by auto
  1.1265 +
  1.1266 +lemma integrable_setsum:
  1.1267 +  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"
  1.1268 +  unfolding integrable_on_def apply(drule bchoice) using has_integral_setsum[of t] by auto
  1.1269 +
  1.1270 +lemma has_integral_eq:
  1.1271 +  assumes "\<forall>x\<in>s. f x = g x" "(f has_integral k) s" shows "(g has_integral k) s"
  1.1272 +  using has_integral_sub[OF assms(2), of "\<lambda>x. f x - g x" 0]
  1.1273 +  using has_integral_is_0[of s "\<lambda>x. f x - g x"] using assms(1) by auto
  1.1274 +
  1.1275 +lemma integrable_eq:
  1.1276 +  shows "\<forall>x\<in>s. f x = g x \<Longrightarrow> f integrable_on s \<Longrightarrow> g integrable_on s"
  1.1277 +  unfolding integrable_on_def using has_integral_eq[of s f g] by auto
  1.1278 +
  1.1279 +lemma has_integral_eq_eq:
  1.1280 +  shows "\<forall>x\<in>s. f x = g x \<Longrightarrow> ((f has_integral k) s \<longleftrightarrow> (g has_integral k) s)"
  1.1281 +  using has_integral_eq[of s f g] has_integral_eq[of s g f] by auto 
  1.1282 +
  1.1283 +lemma has_integral_null[dest]:
  1.1284 +  assumes "content({a..b}) = 0" shows  "(f has_integral 0) ({a..b})"
  1.1285 +  unfolding has_integral apply(rule,rule,rule_tac x="\<lambda>x. ball x 1" in exI,rule) defer
  1.1286 +proof(rule,rule,erule conjE) fix e::real assume e:"e>0" thus "gauge (\<lambda>x. ball x 1)" by auto
  1.1287 +  fix p assume p:"p tagged_division_of {a..b}" (*"(\<lambda>x. ball x 1) fine p"*)
  1.1288 +  have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) = 0" unfolding norm_eq_zero diff_0_right
  1.1289 +    using setsum_content_null[OF assms(1) p, of f] . 
  1.1290 +  thus "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e" using e by auto qed
  1.1291 +
  1.1292 +lemma has_integral_null_eq[simp]:
  1.1293 +  shows "content({a..b}) = 0 \<Longrightarrow> ((f has_integral i) ({a..b}) \<longleftrightarrow> i = 0)"
  1.1294 +  apply rule apply(rule has_integral_unique,assumption) 
  1.1295 +  apply(drule has_integral_null,assumption)
  1.1296 +  apply(drule has_integral_null) by auto
  1.1297 +
  1.1298 +lemma integral_null[dest]: shows "content({a..b}) = 0 \<Longrightarrow> integral({a..b}) f = 0"
  1.1299 +  by(rule integral_unique,drule has_integral_null)
  1.1300 +
  1.1301 +lemma integrable_on_null[dest]: shows "content({a..b}) = 0 \<Longrightarrow> f integrable_on {a..b}"
  1.1302 +  unfolding integrable_on_def apply(drule has_integral_null) by auto
  1.1303 +
  1.1304 +lemma has_integral_empty[intro]: shows "(f has_integral 0) {}"
  1.1305 +  unfolding empty_as_interval apply(rule has_integral_null) 
  1.1306 +  using content_empty unfolding empty_as_interval .
  1.1307 +
  1.1308 +lemma has_integral_empty_eq[simp]: shows "(f has_integral i) {} \<longleftrightarrow> i = 0"
  1.1309 +  apply(rule,rule has_integral_unique,assumption) by auto
  1.1310 +
  1.1311 +lemma integrable_on_empty[intro]: shows "f integrable_on {}" unfolding integrable_on_def by auto
  1.1312 +
  1.1313 +lemma integral_empty[simp]: shows "integral {} f = 0"
  1.1314 +  apply(rule integral_unique) using has_integral_empty .
  1.1315 +
  1.1316 +lemma has_integral_refl[intro]: shows "(f has_integral 0) {a..a}"
  1.1317 +  apply(rule has_integral_null) unfolding content_eq_0_interior
  1.1318 +  unfolding interior_closed_interval using interval_sing by auto
  1.1319 +
  1.1320 +lemma integrable_on_refl[intro]: shows "f integrable_on {a..a}" unfolding integrable_on_def by auto
  1.1321 +
  1.1322 +lemma integral_refl: shows "integral {a..a} f = 0" apply(rule integral_unique) by auto
  1.1323 +
  1.1324 +subsection {* Cauchy-type criterion for integrability. *}
  1.1325 +
  1.1326 +lemma integrable_cauchy: fixes f::"real^'n \<Rightarrow> 'a::{real_normed_vector,complete_space}" 
  1.1327 +  shows "f integrable_on {a..b} \<longleftrightarrow>
  1.1328 +  (\<forall>e>0.\<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of {a..b} \<and> d fine p1 \<and>
  1.1329 +                            p2 tagged_division_of {a..b} \<and> d fine p2
  1.1330 +                            \<longrightarrow> norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 -
  1.1331 +                                     setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) < e))" (is "?l = (\<forall>e>0. \<exists>d. ?P e d)")
  1.1332 +proof assume ?l
  1.1333 +  then guess y unfolding integrable_on_def has_integral .. note y=this
  1.1334 +  show "\<forall>e>0. \<exists>d. ?P e d" proof(rule,rule) case goal1 hence "e/2 > 0" by auto
  1.1335 +    then guess d apply- apply(drule y[rule_format]) by(erule exE,erule conjE) note d=this[rule_format]
  1.1336 +    show ?case apply(rule_tac x=d in exI,rule,rule d) apply(rule,rule,rule,(erule conjE)+)
  1.1337 +    proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b}" "d fine p1" "p2 tagged_division_of {a..b}" "d fine p2"
  1.1338 +      show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  1.1339 +        apply(rule dist_triangle_half_l[where y=y,unfolded vector_dist_norm])
  1.1340 +        using d(2)[OF conjI[OF as(1-2)]] d(2)[OF conjI[OF as(3-4)]] .
  1.1341 +    qed qed
  1.1342 +next assume "\<forall>e>0. \<exists>d. ?P e d" hence "\<forall>n::nat. \<exists>d. ?P (inverse(real (n + 1))) d" by auto
  1.1343 +  from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
  1.1344 +  have "\<And>n. gauge (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}})" apply(rule gauge_inters) using d(1) by auto
  1.1345 +  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-
  1.1346 +  proof case goal1 from this[of n] show ?case apply(drule_tac fine_division_exists) by auto qed
  1.1347 +  from choice[OF this] guess p .. note p = conjunctD2[OF this[rule_format]]
  1.1348 +  have dp:"\<And>i n. i\<le>n \<Longrightarrow> d i fine p n" using p(2) unfolding fine_inters by auto
  1.1349 +  have "Cauchy (\<lambda>n. setsum (\<lambda>(x,k). content k *\<^sub>R (f x)) (p n))"
  1.1350 +  proof(rule CauchyI) case goal1 then guess N unfolding real_arch_inv[of e] .. note N=this
  1.1351 +    show ?case apply(rule_tac x=N in exI)
  1.1352 +    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
  1.1353 +      show "norm ((\<Sum>(x, k)\<in>p m. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p n. content k *\<^sub>R f x)) < e"
  1.1354 +        apply(rule less_trans[OF _ N[THEN conjunct2,THEN conjunct2]]) apply(subst *) apply(rule d(2))
  1.1355 +        using dp p(1) using mn by auto 
  1.1356 +    qed qed
  1.1357 +  then guess y unfolding convergent_eq_cauchy[THEN sym] .. note y=this[unfolded Lim_sequentially,rule_format]
  1.1358 +  show ?l unfolding integrable_on_def has_integral apply(rule_tac x=y in exI)
  1.1359 +  proof(rule,rule) fix e::real assume "e>0" hence *:"e/2 > 0" by auto
  1.1360 +    then guess N1 unfolding real_arch_inv[of "e/2"] .. note N1=this hence N1':"N1 = N1 - 1 + 1" by auto
  1.1361 +    guess N2 using y[OF *] .. note N2=this
  1.1362 +    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)"
  1.1363 +      apply(rule_tac x="d (N1 + N2)" in exI) apply rule defer 
  1.1364 +    proof(rule,rule,erule conjE) show "gauge (d (N1 + N2))" using d by auto
  1.1365 +      fix q assume as:"q tagged_division_of {a..b}" "d (N1 + N2) fine q"
  1.1366 +      have *:"inverse (real (N1 + N2 + 1)) < e / 2" apply(rule less_trans) using N1 by auto
  1.1367 +      show "norm ((\<Sum>(x, k)\<in>q. content k *\<^sub>R f x) - y) < e" apply(rule norm_triangle_half_r)
  1.1368 +        apply(rule less_trans[OF _ *]) apply(subst N1', rule d(2)[of "p (N1+N2)"]) defer
  1.1369 +        using N2[rule_format,unfolded vector_dist_norm,of "N1+N2"]
  1.1370 +        using as dp[of "N1 - 1 + 1 + N2" "N1 + N2"] using p(1)[of "N1 + N2"] using N1 by auto qed qed qed
  1.1371 +
  1.1372 +subsection {* Additivity of integral on abutting intervals. *}
  1.1373 +
  1.1374 +lemma interval_split:
  1.1375 +  "{a..b::real^'n} \<inter> {x. x$k \<le> c} = {a .. (\<chi> i. if i = k then min (b$k) c else b$i)}"
  1.1376 +  "{a..b} \<inter> {x. x$k \<ge> c} = {(\<chi> i. if i = k then max (a$k) c else a$i) .. b}"
  1.1377 +  apply(rule_tac[!] set_ext) unfolding Int_iff mem_interval mem_Collect_eq
  1.1378 +  unfolding Cart_lambda_beta by auto
  1.1379 +
  1.1380 +lemma content_split:
  1.1381 +  "content {a..b::real^'n} = content({a..b} \<inter> {x. x$k \<le> c}) + content({a..b} \<inter> {x. x$k >= c})"
  1.1382 +proof- note simps = interval_split content_closed_interval_cases Cart_lambda_beta vector_le_def
  1.1383 +  { presume "a\<le>b \<Longrightarrow> ?thesis" thus ?thesis apply(cases "a\<le>b") unfolding simps by auto }
  1.1384 +  have *:"UNIV = insert k (UNIV - {k})" "\<And>x. finite (UNIV-{x::'n})" "\<And>x. x\<notin>UNIV-{x}" by auto
  1.1385 +  have *:"\<And>X Y Z. (\<Prod>i\<in>UNIV. Z i (if i = k then X else Y i)) = Z k X * (\<Prod>i\<in>UNIV-{k}. Z i (Y i))"
  1.1386 +    "(\<Prod>i\<in>UNIV. b$i - a$i) = (\<Prod>i\<in>UNIV-{k}. b$i - a$i) * (b$k - a$k)" 
  1.1387 +    apply(subst *(1)) defer apply(subst *(1)) unfolding setprod_insert[OF *(2-)] by auto
  1.1388 +  assume as:"a\<le>b" moreover have "\<And>x. min (b $ k) c = max (a $ k) c
  1.1389 +    \<Longrightarrow> x* (b$k - a$k) = x*(max (a $ k) c - a $ k) + x*(b $ k - max (a $ k) c)"
  1.1390 +    by  (auto simp add:field_simps)
  1.1391 +  moreover have "\<not> a $ k \<le> c \<Longrightarrow> \<not> c \<le> b $ k \<Longrightarrow> False"
  1.1392 +    unfolding not_le using as[unfolded vector_le_def,rule_format,of k] by auto
  1.1393 +  ultimately show ?thesis 
  1.1394 +    unfolding simps unfolding *(1)[of "\<lambda>i x. b$i - x"] *(1)[of "\<lambda>i x. x - a$i"] *(2) by(auto)
  1.1395 +qed
  1.1396 +
  1.1397 +lemma division_split_left_inj:
  1.1398 +  assumes "d division_of i" "k1 \<in> d" "k2 \<in> d"  "k1 \<noteq> k2"
  1.1399 +  "k1 \<inter> {x::real^'n. x$k \<le> c} = k2 \<inter> {x. x$k \<le> c}"
  1.1400 +  shows "content(k1 \<inter> {x. x$k \<le> c}) = 0"
  1.1401 +proof- note d=division_ofD[OF assms(1)]
  1.1402 +  have *:"\<And>a b::real^'n. \<And> c k. (content({a..b} \<inter> {x. x$k \<le> c}) = 0 \<longleftrightarrow> interior({a..b} \<inter> {x. x$k \<le> c}) = {})"
  1.1403 +    unfolding interval_split content_eq_0_interior by auto
  1.1404 +  guess u1 v1 using d(4)[OF assms(2)] apply-by(erule exE)+ note uv1=this
  1.1405 +  guess u2 v2 using d(4)[OF assms(3)] apply-by(erule exE)+ note uv2=this
  1.1406 +  have **:"\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}" by auto
  1.1407 +  show ?thesis unfolding uv1 uv2 * apply(rule **[OF d(5)[OF assms(2-4)]])
  1.1408 +    defer apply(subst assms(5)[unfolded uv1 uv2]) unfolding uv1 uv2 by auto qed
  1.1409 +
  1.1410 +lemma division_split_right_inj:
  1.1411 +  assumes "d division_of i" "k1 \<in> d" "k2 \<in> d"  "k1 \<noteq> k2"
  1.1412 +  "k1 \<inter> {x::real^'n. x$k \<ge> c} = k2 \<inter> {x. x$k \<ge> c}"
  1.1413 +  shows "content(k1 \<inter> {x. x$k \<ge> c}) = 0"
  1.1414 +proof- note d=division_ofD[OF assms(1)]
  1.1415 +  have *:"\<And>a b::real^'n. \<And> c k. (content({a..b} \<inter> {x. x$k >= c}) = 0 \<longleftrightarrow> interior({a..b} \<inter> {x. x$k >= c}) = {})"
  1.1416 +    unfolding interval_split content_eq_0_interior by auto
  1.1417 +  guess u1 v1 using d(4)[OF assms(2)] apply-by(erule exE)+ note uv1=this
  1.1418 +  guess u2 v2 using d(4)[OF assms(3)] apply-by(erule exE)+ note uv2=this
  1.1419 +  have **:"\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}" by auto
  1.1420 +  show ?thesis unfolding uv1 uv2 * apply(rule **[OF d(5)[OF assms(2-4)]])
  1.1421 +    defer apply(subst assms(5)[unfolded uv1 uv2]) unfolding uv1 uv2 by auto qed
  1.1422 +
  1.1423 +lemma tagged_division_split_left_inj:
  1.1424 +  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}" 
  1.1425 +  shows "content(k1 \<inter> {x. x$k \<le> c}) = 0"
  1.1426 +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
  1.1427 +  show ?thesis apply(rule division_split_left_inj[OF division_of_tagged_division[OF assms(1)]])
  1.1428 +    apply(rule_tac[1-2] *) using assms(2-) by auto qed
  1.1429 +
  1.1430 +lemma tagged_division_split_right_inj:
  1.1431 +  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}" 
  1.1432 +  shows "content(k1 \<inter> {x. x$k \<ge> c}) = 0"
  1.1433 +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
  1.1434 +  show ?thesis apply(rule division_split_right_inj[OF division_of_tagged_division[OF assms(1)]])
  1.1435 +    apply(rule_tac[1-2] *) using assms(2-) by auto qed
  1.1436 +
  1.1437 +lemma division_split:
  1.1438 +  assumes "p division_of {a..b::real^'n}"
  1.1439 +  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 
  1.1440 +        "{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")
  1.1441 +proof(rule_tac[!] division_ofI) note p=division_ofD[OF assms]
  1.1442 +  show "finite ?p1" "finite ?p2" using p(1) by auto show "\<Union>?p1 = ?I1" "\<Union>?p2 = ?I2" unfolding p(6)[THEN sym] by auto
  1.1443 +  { fix k assume "k\<in>?p1" then guess l unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l=this
  1.1444 +    guess u v using p(4)[OF l(2)] apply-by(erule exE)+ note uv=this
  1.1445 +    show "k\<subseteq>?I1" "k \<noteq> {}" "\<exists>a b. k = {a..b}" unfolding l
  1.1446 +      using p(2-3)[OF l(2)] l(3) unfolding uv apply- prefer 3 apply(subst interval_split) by auto
  1.1447 +    fix k' assume "k'\<in>?p1" then guess l' unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l'=this
  1.1448 +    assume "k\<noteq>k'" thus "interior k \<inter> interior k' = {}" unfolding l l' using p(5)[OF l(2) l'(2)] by auto }
  1.1449 +  { fix k assume "k\<in>?p2" then guess l unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l=this
  1.1450 +    guess u v using p(4)[OF l(2)] apply-by(erule exE)+ note uv=this
  1.1451 +    show "k\<subseteq>?I2" "k \<noteq> {}" "\<exists>a b. k = {a..b}" unfolding l
  1.1452 +      using p(2-3)[OF l(2)] l(3) unfolding uv apply- prefer 3 apply(subst interval_split) by auto
  1.1453 +    fix k' assume "k'\<in>?p2" then guess l' unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l'=this
  1.1454 +    assume "k\<noteq>k'" thus "interior k \<inter> interior k' = {}" unfolding l l' using p(5)[OF l(2) l'(2)] by auto }
  1.1455 +qed
  1.1456 +
  1.1457 +lemma has_integral_split: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
  1.1458 +  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})"
  1.1459 +  shows "(f has_integral (i + j)) ({a..b})"
  1.1460 +proof(unfold has_integral,rule,rule) case goal1 hence e:"e/2>0" by auto
  1.1461 +  guess d1 using has_integralD[OF assms(1)[unfolded interval_split] e] . note d1=this[unfolded interval_split[THEN sym]]
  1.1462 +  guess d2 using has_integralD[OF assms(2)[unfolded interval_split] e] . note d2=this[unfolded interval_split[THEN sym]]
  1.1463 +  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"
  1.1464 +  show ?case apply(rule_tac x="?d" in exI,rule) defer apply(rule,rule,(erule conjE)+)
  1.1465 +  proof- show "gauge ?d" using d1(1) d2(1) unfolding gauge_def by auto
  1.1466 +    fix p assume "p tagged_division_of {a..b}" "?d fine p" note p = this tagged_division_ofD[OF this(1)]
  1.1467 +    have lem0:"\<And>x kk. (x,kk) \<in> p \<Longrightarrow> ~(kk \<inter> {x. x$k \<le> c} = {}) \<Longrightarrow> x$k \<le> c"
  1.1468 +         "\<And>x kk. (x,kk) \<in> p \<Longrightarrow> ~(kk \<inter> {x. x$k \<ge> c} = {}) \<Longrightarrow> x$k \<ge> c"
  1.1469 +    proof- fix x kk assume as:"(x,kk)\<in>p"
  1.1470 +      show "~(kk \<inter> {x. x$k \<le> c} = {}) \<Longrightarrow> x$k \<le> c"
  1.1471 +      proof(rule ccontr) case goal1
  1.1472 +        from this(2)[unfolded not_le] have "kk \<subseteq> ball x \<bar>x $ k - c\<bar>"
  1.1473 +          using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
  1.1474 +        hence "\<exists>y. y \<in> ball x \<bar>x $ k - c\<bar> \<inter> {x. x $ k \<le> c}" using goal1(1) by blast 
  1.1475 +        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)
  1.1476 +          using component_le_norm[of "x - y" k,unfolded vector_minus_component] by(auto simp add:vector_dist_norm)
  1.1477 +        thus False using goal1(2)[unfolded not_le] by(auto simp add:field_simps)
  1.1478 +      qed
  1.1479 +      show "~(kk \<inter> {x. x$k \<ge> c} = {}) \<Longrightarrow> x$k \<ge> c"
  1.1480 +      proof(rule ccontr) case goal1
  1.1481 +        from this(2)[unfolded not_le] have "kk \<subseteq> ball x \<bar>x $ k - c\<bar>"
  1.1482 +          using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
  1.1483 +        hence "\<exists>y. y \<in> ball x \<bar>x $ k - c\<bar> \<inter> {x. x $ k \<ge> c}" using goal1(1) by blast 
  1.1484 +        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)
  1.1485 +          using component_le_norm[of "x - y" k,unfolded vector_minus_component] by(auto simp add:vector_dist_norm)
  1.1486 +        thus False using goal1(2)[unfolded not_le] by(auto simp add:field_simps)
  1.1487 +      qed
  1.1488 +    qed
  1.1489 +
  1.1490 +    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
  1.1491 +    have lem2: "\<And>f s P f. finite s \<Longrightarrow> finite {(x,f k) | x k. (x,k) \<in> s \<and> P x k}"
  1.1492 +    proof- case goal1 thus ?case apply-apply(rule finite_subset[of _ "(\<lambda>(x,k). (x,f k)) ` s"]) by auto qed
  1.1493 +    have lem3: "\<And>g::(real ^ 'n \<Rightarrow> bool) \<Rightarrow> real ^ 'n \<Rightarrow> bool. finite p \<Longrightarrow>
  1.1494 +      setsum (\<lambda>(x,k). content k *\<^sub>R f x) {(x,g k) |x k. (x,k) \<in> p \<and> ~(g k = {})}
  1.1495 +               = setsum (\<lambda>(x,k). content k *\<^sub>R f x) ((\<lambda>(x,k). (x,g k)) ` p)"
  1.1496 +      apply(rule setsum_mono_zero_left) prefer 3
  1.1497 +    proof fix g::"(real ^ 'n \<Rightarrow> bool) \<Rightarrow> real ^ 'n \<Rightarrow> bool" and i::"(real^'n) \<times> ((real^'n) set)"
  1.1498 +      assume "i \<in> (\<lambda>(x, k). (x, g k)) ` p - {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
  1.1499 +      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
  1.1500 +      have "content (g k) = 0" using xk using content_empty by auto
  1.1501 +      thus "(\<lambda>(x, k). content k *\<^sub>R f x) i = 0" unfolding xk split_conv by auto
  1.1502 +    qed auto
  1.1503 +    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
  1.1504 +
  1.1505 +    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> {}}"
  1.1506 +    have "norm ((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) < e/2" apply(rule d1(2),rule tagged_division_ofI)
  1.1507 +      apply(rule lem2 p(3))+ prefer 6 apply(rule fineI)
  1.1508 +    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
  1.1509 +      fix x l assume xl:"(x,l)\<in>?M1"
  1.1510 +      then guess x' l' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note xl'=this
  1.1511 +      have "l' \<subseteq> d1 x'" apply(rule order_trans[OF fineD[OF p(2) xl'(3)]]) by auto
  1.1512 +      thus "l \<subseteq> d1 x" unfolding xl' by auto
  1.1513 +      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)
  1.1514 +        using lem0(1)[OF xl'(3-4)] by auto
  1.1515 +      show  "\<exists>a b. l = {a..b}" unfolding xl' using p(6)[OF xl'(3)] by(fastsimp simp add: interval_split[where c=c and k=k])
  1.1516 +      fix y r let ?goal = "interior l \<inter> interior r = {}" assume yr:"(y,r)\<in>?M1"
  1.1517 +      then guess y' r' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note yr'=this
  1.1518 +      assume as:"(x,l) \<noteq> (y,r)" show "interior l \<inter> interior r = {}"
  1.1519 +      proof(cases "l' = r' \<longrightarrow> x' = y'")
  1.1520 +        case False thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1.1521 +      next case True hence "l' \<noteq> r'" using as unfolding xl' yr' by auto
  1.1522 +        thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1.1523 +      qed qed moreover
  1.1524 +
  1.1525 +    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> {}}" 
  1.1526 +    have "norm ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) < e/2" apply(rule d2(2),rule tagged_division_ofI)
  1.1527 +      apply(rule lem2 p(3))+ prefer 6 apply(rule fineI)
  1.1528 +    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
  1.1529 +      fix x l assume xl:"(x,l)\<in>?M2"
  1.1530 +      then guess x' l' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note xl'=this
  1.1531 +      have "l' \<subseteq> d2 x'" apply(rule order_trans[OF fineD[OF p(2) xl'(3)]]) by auto
  1.1532 +      thus "l \<subseteq> d2 x" unfolding xl' by auto
  1.1533 +      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)
  1.1534 +        using lem0(2)[OF xl'(3-4)] by auto
  1.1535 +      show  "\<exists>a b. l = {a..b}" unfolding xl' using p(6)[OF xl'(3)] by(fastsimp simp add: interval_split[where c=c and k=k])
  1.1536 +      fix y r let ?goal = "interior l \<inter> interior r = {}" assume yr:"(y,r)\<in>?M2"
  1.1537 +      then guess y' r' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note yr'=this
  1.1538 +      assume as:"(x,l) \<noteq> (y,r)" show "interior l \<inter> interior r = {}"
  1.1539 +      proof(cases "l' = r' \<longrightarrow> x' = y'")
  1.1540 +        case False thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1.1541 +      next case True hence "l' \<noteq> r'" using as unfolding xl' yr' by auto
  1.1542 +        thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  1.1543 +      qed qed ultimately
  1.1544 +
  1.1545 +    have "norm (((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j)) < e/2 + e/2"
  1.1546 +      apply- apply(rule norm_triangle_lt) by auto
  1.1547 +    also { have *:"\<And>x y. x = (0::real) \<Longrightarrow> x *\<^sub>R (y::'a) = 0" using scaleR_zero_left by auto
  1.1548 +      have "((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j)
  1.1549 +       = (\<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
  1.1550 +      also have "\<dots> = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. x $ k \<le> c}) *\<^sub>R f x) + (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. c \<le> x $ k}) *\<^sub>R f x) - (i + j)"
  1.1551 +        unfolding lem3[OF p(3)] apply(subst setsum_reindex_nonzero[OF p(3)]) defer apply(subst setsum_reindex_nonzero[OF p(3)])
  1.1552 +        defer unfolding lem4[THEN sym] apply(rule refl) unfolding split_paired_all split_conv apply(rule_tac[!] *)
  1.1553 +      proof- case goal1 thus ?case apply- apply(rule tagged_division_split_left_inj [OF p(1), of a b aa ba]) by auto
  1.1554 +      next case   goal2 thus ?case apply- apply(rule tagged_division_split_right_inj[OF p(1), of a b aa ba]) by auto
  1.1555 +      qed also note setsum_addf[THEN sym]
  1.1556 +      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
  1.1557 +        = (\<lambda>(x,ka). content ka *\<^sub>R f x) x" unfolding split_paired_all split_conv
  1.1558 +      proof- fix a b assume "(a,b) \<in> p" from p(6)[OF this] guess u v apply-by(erule exE)+ note uv=this
  1.1559 +        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"
  1.1560 +          unfolding scaleR_left_distrib[THEN sym] unfolding uv content_split[of u v k c] by auto
  1.1561 +      qed note setsum_cong2[OF this]
  1.1562 +      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 +
  1.1563 +        ((\<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) =
  1.1564 +        (\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f x) - (i + j)" by auto }
  1.1565 +    finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (i + j)) < e" by auto qed qed
  1.1566 +
  1.1567 +subsection {* A sort of converse, integrability on subintervals. *}
  1.1568 +
  1.1569 +lemma tagged_division_union_interval:
  1.1570 +  assumes "p1 tagged_division_of ({a..b} \<inter> {x::real^'n. x$k \<le> (c::real)})"  "p2 tagged_division_of ({a..b} \<inter> {x. x$k \<ge> c})"
  1.1571 +  shows "(p1 \<union> p2) tagged_division_of ({a..b})"
  1.1572 +proof- have *:"{a..b} = ({a..b} \<inter> {x. x$k \<le> c}) \<union> ({a..b} \<inter> {x. x$k \<ge> c})" by auto
  1.1573 +  show ?thesis apply(subst *) apply(rule tagged_division_union[OF assms])
  1.1574 +    unfolding interval_split interior_closed_interval
  1.1575 +    by(auto simp add: vector_less_def Cart_lambda_beta elim!:allE[where x=k]) qed
  1.1576 +
  1.1577 +lemma has_integral_separate_sides: fixes f::"real^'m \<Rightarrow> 'a::real_normed_vector"
  1.1578 +  assumes "(f has_integral i) ({a..b})" "e>0"
  1.1579 +  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>
  1.1580 +                                p2 tagged_division_of ({a..b} \<inter> {x. x$k \<ge> c}) \<and> d fine p2
  1.1581 +                                \<longrightarrow> norm((setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 +
  1.1582 +                                          setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) - i) < e)"
  1.1583 +proof- guess d using has_integralD[OF assms] . note d=this
  1.1584 +  show ?thesis apply(rule that[of d]) apply(rule d) apply(rule,rule,rule,(erule conjE)+)
  1.1585 +  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
  1.1586 +                   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
  1.1587 +    note tagged_division_union_interval[OF p1(7) p2(7)] note p12 = tagged_division_ofD[OF this] this
  1.1588 +    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)"
  1.1589 +      apply(subst setsum_Un_zero) apply(rule p1 p2)+ apply(rule) unfolding split_paired_all split_conv
  1.1590 +    proof- fix a b assume ab:"(a,b) \<in> p1 \<inter> p2"
  1.1591 +      have "(a,b) \<in> p1" using ab by auto from p1(4)[OF this] guess u v apply-by(erule exE)+ note uv =this
  1.1592 +      have "b \<subseteq> {x. x$k = c}" using ab p1(3)[of a b] p2(3)[of a b] by fastsimp
  1.1593 +      moreover have "interior {x. x $ k = c} = {}" 
  1.1594 +      proof(rule ccontr) case goal1 then obtain x where x:"x\<in>interior {x. x$k = c}" by auto
  1.1595 +        then guess e unfolding mem_interior .. note e=this
  1.1596 +        have x:"x$k = c" using x interior_subset by fastsimp
  1.1597 +        have *:"\<And>i. \<bar>(x - (x + (\<chi> i. if i = k then e / 2 else 0))) $ i\<bar> = (if i = k then e/2 else 0)" using e by auto
  1.1598 +        have "x + (\<chi> i. if i = k then e/2 else 0) \<in> ball x e" unfolding mem_ball vector_dist_norm 
  1.1599 +          apply(rule le_less_trans[OF norm_le_l1]) unfolding * 
  1.1600 +          unfolding setsum_delta[OF finite_UNIV] using e by auto 
  1.1601 +        hence "x + (\<chi> i. if i = k then e/2 else 0) \<in> {x. x$k = c}" using e by auto
  1.1602 +        thus False unfolding mem_Collect_eq using e x by auto
  1.1603 +      qed ultimately have "content b = 0" unfolding uv content_eq_0_interior apply-apply(drule subset_interior) by auto
  1.1604 +      thus "content b *\<^sub>R f a = 0" by auto
  1.1605 +    qed auto
  1.1606 +    also have "\<dots> < e" by(rule d(2) p12 fine_union p1 p2)+
  1.1607 +    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
  1.1608 +
  1.1609 +lemma integrable_split[intro]: fixes f::"real^'n \<Rightarrow> 'a::{real_normed_vector,complete_space}" assumes "f integrable_on {a..b}"
  1.1610 +  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) 
  1.1611 +proof- guess y using assms unfolding integrable_on_def .. note y=this
  1.1612 +  def b' \<equiv> "(\<chi> i. if i = k then min (b$k) c else b$i)::real^'n"
  1.1613 +  and a' \<equiv> "(\<chi> i. if i = k then max (a$k) c else a$i)::real^'n"
  1.1614 +  show ?t1 ?t2 unfolding interval_split integrable_cauchy unfolding interval_split[THEN sym]
  1.1615 +  proof(rule_tac[!] allI impI)+ fix e::real assume "e>0" hence "e/2>0" by auto
  1.1616 +    from has_integral_separate_sides[OF y this,of k c] guess d . note d=this[rule_format]
  1.1617 +    let ?P = "\<lambda>A. \<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of {a..b} \<inter> A \<and> d fine p1 \<and> p2 tagged_division_of {a..b} \<inter> A \<and> d fine p2 \<longrightarrow>
  1.1618 +                              norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e)"
  1.1619 +    show "?P {x. x $ k \<le> c}" apply(rule_tac x=d in exI) apply(rule,rule d) apply(rule,rule,rule)
  1.1620 +    proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b} \<inter> {x. x $ k \<le> c} \<and> d fine p1 \<and> p2 tagged_division_of {a..b} \<inter> {x. x $ k \<le> c} \<and> d fine p2"
  1.1621 +      show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  1.1622 +      proof- guess p using fine_division_exists[OF d(1), of a' b] . note p=this
  1.1623 +        show ?thesis using norm_triangle_half_l[OF d(2)[of p1 p] d(2)[of p2 p]]
  1.1624 +          using as unfolding interval_split b'_def[symmetric] a'_def[symmetric]
  1.1625 +          using p using assms by(auto simp add:group_simps)
  1.1626 +      qed qed  
  1.1627 +    show "?P {x. x $ k \<ge> c}" apply(rule_tac x=d in exI) apply(rule,rule d) apply(rule,rule,rule)
  1.1628 +    proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b} \<inter> {x. x $ k \<ge> c} \<and> d fine p1 \<and> p2 tagged_division_of {a..b} \<inter> {x. x $ k \<ge> c} \<and> d fine p2"
  1.1629 +      show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  1.1630 +      proof- guess p using fine_division_exists[OF d(1), of a b'] . note p=this
  1.1631 +        show ?thesis using norm_triangle_half_l[OF d(2)[of p p1] d(2)[of p p2]]
  1.1632 +          using as unfolding interval_split b'_def[symmetric] a'_def[symmetric]
  1.1633 +          using p using assms by(auto simp add:group_simps) qed qed qed qed
  1.1634 +
  1.1635 +subsection {* Generalized notion of additivity. *}
  1.1636 +
  1.1637 +definition "neutral opp = (SOME x. \<forall>y. opp x y = y \<and> opp y x = y)"
  1.1638 +
  1.1639 +definition operative :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> ((real^'n) set \<Rightarrow> 'a) \<Rightarrow> bool" where
  1.1640 +  "operative opp f \<equiv> 
  1.1641 +    (\<forall>a b. content {a..b} = 0 \<longrightarrow> f {a..b} = neutral(opp)) \<and>
  1.1642 +    (\<forall>a b c k. f({a..b}) =
  1.1643 +                   opp (f({a..b} \<inter> {x. x$k \<le> c}))
  1.1644 +                       (f({a..b} \<inter> {x. x$k \<ge> c})))"
  1.1645 +
  1.1646 +lemma operativeD[dest]: assumes "operative opp f"
  1.1647 +  shows "\<And>a b. content {a..b} = 0 \<Longrightarrow> f {a..b} = neutral(opp)"
  1.1648 +  "\<And>a b c k. f({a..b}) = opp (f({a..b} \<inter> {x. x$k \<le> c})) (f({a..b} \<inter> {x. x$k \<ge> c}))"
  1.1649 +  using assms unfolding operative_def by auto
  1.1650 +
  1.1651 +lemma operative_trivial:
  1.1652 + "operative opp f \<Longrightarrow> content({a..b}) = 0 \<Longrightarrow> f({a..b}) = neutral opp"
  1.1653 +  unfolding operative_def by auto
  1.1654 +
  1.1655 +lemma property_empty_interval:
  1.1656 + "(\<forall>a b. content({a..b}) = 0 \<longrightarrow> P({a..b})) \<Longrightarrow> P {}" 
  1.1657 +  using content_empty unfolding empty_as_interval by auto
  1.1658 +
  1.1659 +lemma operative_empty: "operative opp f \<Longrightarrow> f {} = neutral opp"
  1.1660 +  unfolding operative_def apply(rule property_empty_interval) by auto
  1.1661 +
  1.1662 +subsection {* Using additivity of lifted function to encode definedness. *}
  1.1663 +
  1.1664 +lemma forall_option: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P(Some x))"
  1.1665 +  by (metis map_of.simps option.nchotomy)
  1.1666 +
  1.1667 +lemma exists_option:
  1.1668 + "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P(Some x))" 
  1.1669 +  by (metis map_of.simps option.nchotomy)
  1.1670 +
  1.1671 +fun lifted where 
  1.1672 +  "lifted (opp::'a\<Rightarrow>'a\<Rightarrow>'b) (Some x) (Some y) = Some(opp x y)" |
  1.1673 +  "lifted opp None _ = (None::'b option)" |
  1.1674 +  "lifted opp _ None = None"
  1.1675 +
  1.1676 +lemma lifted_simp_1[simp]: "lifted opp v None = None"
  1.1677 +  apply(induct v) by auto
  1.1678 +
  1.1679 +definition "monoidal opp \<equiv>  (\<forall>x y. opp x y = opp y x) \<and>
  1.1680 +                   (\<forall>x y z. opp x (opp y z) = opp (opp x y) z) \<and>
  1.1681 +                   (\<forall>x. opp (neutral opp) x = x)"
  1.1682 +
  1.1683 +lemma monoidalI: assumes "\<And>x y. opp x y = opp y x"
  1.1684 +  "\<And>x y z. opp x (opp y z) = opp (opp x y) z"
  1.1685 +  "\<And>x. opp (neutral opp) x = x" shows "monoidal opp"
  1.1686 +  unfolding monoidal_def using assms by fastsimp
  1.1687 +
  1.1688 +lemma monoidal_ac: assumes "monoidal opp"
  1.1689 +  shows "opp (neutral opp) a = a" "opp a (neutral opp) = a" "opp a b = opp b a"
  1.1690 +  "opp (opp a b) c = opp a (opp b c)"  "opp a (opp b c) = opp b (opp a c)"
  1.1691 +  using assms unfolding monoidal_def apply- by metis+
  1.1692 +
  1.1693 +lemma monoidal_simps[simp]: assumes "monoidal opp"
  1.1694 +  shows "opp (neutral opp) a = a" "opp a (neutral opp) = a"
  1.1695 +  using monoidal_ac[OF assms] by auto
  1.1696 +
  1.1697 +lemma neutral_lifted[cong]: assumes "monoidal opp"
  1.1698 +  shows "neutral (lifted opp) = Some(neutral opp)"
  1.1699 +  apply(subst neutral_def) apply(rule some_equality) apply(rule,induct_tac y) prefer 3
  1.1700 +proof- fix x assume "\<forall>y. lifted opp x y = y \<and> lifted opp y x = y"
  1.1701 +  thus "x = Some (neutral opp)" apply(induct x) defer
  1.1702 +    apply rule apply(subst neutral_def) apply(subst eq_commute,rule some_equality)
  1.1703 +    apply(rule,erule_tac x="Some y" in allE) defer apply(erule_tac x="Some x" in allE) by auto
  1.1704 +qed(auto simp add:monoidal_ac[OF assms])
  1.1705 +
  1.1706 +lemma monoidal_lifted[intro]: assumes "monoidal opp" shows "monoidal(lifted opp)"
  1.1707 +  unfolding monoidal_def forall_option neutral_lifted[OF assms] using monoidal_ac[OF assms] by auto
  1.1708 +
  1.1709 +definition "support opp f s = {x. x\<in>s \<and> f x \<noteq> neutral opp}"
  1.1710 +definition "fold' opp e s \<equiv> (if finite s then fold opp e s else e)"
  1.1711 +definition "iterate opp s f \<equiv> fold' (\<lambda>x a. opp (f x) a) (neutral opp) (support opp f s)"
  1.1712 +
  1.1713 +lemma support_subset[intro]:"support opp f s \<subseteq> s" unfolding support_def by auto
  1.1714 +lemma support_empty[simp]:"support opp f {} = {}" using support_subset[of opp f "{}"] by auto
  1.1715 +
  1.1716 +lemma fun_left_comm_monoidal[intro]: assumes "monoidal opp" shows "fun_left_comm opp"
  1.1717 +  unfolding fun_left_comm_def using monoidal_ac[OF assms] by auto
  1.1718 +
  1.1719 +lemma support_clauses:
  1.1720 +  "\<And>f g s. support opp f {} = {}"
  1.1721 +  "\<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))"
  1.1722 +  "\<And>f g s. support opp f (s - {x}) = (support opp f s) - {x}"
  1.1723 +  "\<And>f g s. support opp f (s \<union> t) = (support opp f s) \<union> (support opp f t)"
  1.1724 +  "\<And>f g s. support opp f (s \<inter> t) = (support opp f s) \<inter> (support opp f t)"
  1.1725 +  "\<And>f g s. support opp f (s - t) = (support opp f s) - (support opp f t)"
  1.1726 +  "\<And>f g s. support opp g (f ` s) = f ` (support opp (g o f) s)"
  1.1727 +unfolding support_def by auto
  1.1728 +
  1.1729 +lemma finite_support[intro]:"finite s \<Longrightarrow> finite (support opp f s)"
  1.1730 +  unfolding support_def by auto
  1.1731 +
  1.1732 +lemma iterate_empty[simp]:"iterate opp {} f = neutral opp"
  1.1733 +  unfolding iterate_def fold'_def by auto 
  1.1734 +
  1.1735 +lemma iterate_insert[simp]: assumes "monoidal opp" "finite s"
  1.1736 +  shows "iterate opp (insert x s) f = (if x \<in> s then iterate opp s f else opp (f x) (iterate opp s f))" 
  1.1737 +proof(cases "x\<in>s") case True hence *:"insert x s = s" by auto
  1.1738 +  show ?thesis unfolding iterate_def if_P[OF True] * by auto
  1.1739 +next case False note x=this
  1.1740 +  note * = fun_left_comm.fun_left_comm_apply[OF fun_left_comm_monoidal[OF assms(1)]]
  1.1741 +  show ?thesis proof(cases "f x = neutral opp")
  1.1742 +    case True show ?thesis unfolding iterate_def if_not_P[OF x] support_clauses if_P[OF True]
  1.1743 +      unfolding True monoidal_simps[OF assms(1)] by auto
  1.1744 +  next case False show ?thesis unfolding iterate_def fold'_def  if_not_P[OF x] support_clauses if_not_P[OF False]
  1.1745 +      apply(subst fun_left_comm.fold_insert[OF * finite_support])
  1.1746 +      using `finite s` unfolding support_def using False x by auto qed qed 
  1.1747 +
  1.1748 +lemma iterate_some:
  1.1749 +  assumes "monoidal opp"  "finite s"
  1.1750 +  shows "iterate (lifted opp) s (\<lambda>x. Some(f x)) = Some (iterate opp s f)" using assms(2)
  1.1751 +proof(induct s) case empty thus ?case using assms by auto
  1.1752 +next case (insert x F) show ?case apply(subst iterate_insert) prefer 3 apply(subst if_not_P)
  1.1753 +    defer unfolding insert(3) lifted.simps apply rule using assms insert by auto qed
  1.1754 +
  1.1755 +subsection {* Two key instances of additivity. *}
  1.1756 +
  1.1757 +lemma neutral_add[simp]:
  1.1758 +  "neutral op + = (0::_::comm_monoid_add)" unfolding neutral_def 
  1.1759 +  apply(rule some_equality) defer apply(erule_tac x=0 in allE) by auto
  1.1760 +
  1.1761 +lemma operative_content[intro]: "operative (op +) content"
  1.1762 +  unfolding operative_def content_split[THEN sym] neutral_add by auto
  1.1763 +
  1.1764 +lemma neutral_monoid[simp]: "neutral ((op +)::('a::comm_monoid_add) \<Rightarrow> 'a \<Rightarrow> 'a) = 0"
  1.1765 +  unfolding neutral_def apply(rule some_equality) defer
  1.1766 +  apply(erule_tac x=0 in allE) by auto
  1.1767 +
  1.1768 +lemma monoidal_monoid[intro]:
  1.1769 +  shows "monoidal ((op +)::('a::comm_monoid_add) \<Rightarrow> 'a \<Rightarrow> 'a)"
  1.1770 +  unfolding monoidal_def neutral_monoid by(auto simp add: group_simps) 
  1.1771 +
  1.1772 +lemma operative_integral: fixes f::"real^'n \<Rightarrow> 'a::banach"
  1.1773 +  shows "operative (lifted(op +)) (\<lambda>i. if f integrable_on i then Some(integral i f) else None)"
  1.1774 +  unfolding operative_def unfolding neutral_lifted[OF monoidal_monoid] neutral_add
  1.1775 +  apply(rule,rule,rule,rule) defer apply(rule allI)+
  1.1776 +proof- fix a b c k show "(if f integrable_on {a..b} then Some (integral {a..b} f) else None) =
  1.1777 +              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)
  1.1778 +               (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)"
  1.1779 +  proof(cases "f integrable_on {a..b}") 
  1.1780 +    case True show ?thesis unfolding if_P[OF True]
  1.1781 +      unfolding if_P[OF integrable_split(1)[OF True]] if_P[OF integrable_split(2)[OF True]]
  1.1782 +      unfolding lifted.simps option.inject apply(rule integral_unique) apply(rule has_integral_split) 
  1.1783 +      apply(rule_tac[!] integrable_integral integrable_split)+ using True by assumption+
  1.1784 +  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}))"
  1.1785 +    proof(rule ccontr) case goal1 hence "f integrable_on {a..b}" apply- unfolding integrable_on_def
  1.1786 +        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)
  1.1787 +        apply(rule has_integral_split) apply(rule_tac[!] integrable_integral) by auto
  1.1788 +      thus False using False by auto
  1.1789 +    qed thus ?thesis using False by auto 
  1.1790 +  qed next 
  1.1791 +  fix a b assume as:"content {a..b::real^'n} = 0"
  1.1792 +  thus "(if f integrable_on {a..b} then Some (integral {a..b} f) else None) = Some 0"
  1.1793 +    unfolding if_P[OF integrable_on_null[OF as]] using has_integral_null_eq[OF as] by auto qed
  1.1794 +
  1.1795 +subsection {* Points of division of a partition. *}
  1.1796 +
  1.1797 +definition "division_points (k::(real^'n) set) d = 
  1.1798 +    {(j,x). (interval_lowerbound k)$j < x \<and> x < (interval_upperbound k)$j \<and>
  1.1799 +           (\<exists>i\<in>d. (interval_lowerbound i)$j = x \<or> (interval_upperbound i)$j = x)}"
  1.1800 +
  1.1801 +lemma division_points_finite: assumes "d division_of i"
  1.1802 +  shows "finite (division_points i d)"
  1.1803 +proof- note assm = division_ofD[OF assms]
  1.1804 +  let ?M = "\<lambda>j. {(j,x)|x. (interval_lowerbound i)$j < x \<and> x < (interval_upperbound i)$j \<and>
  1.1805 +           (\<exists>i\<in>d. (interval_lowerbound i)$j = x \<or> (interval_upperbound i)$j = x)}"
  1.1806 +  have *:"division_points i d = \<Union>(?M ` UNIV)"
  1.1807 +    unfolding division_points_def by auto
  1.1808 +  show ?thesis unfolding * using assm by auto qed
  1.1809 +
  1.1810 +lemma division_points_subset:
  1.1811 +  assumes "d division_of {a..b}" "\<forall>i. a$i < b$i"  "a$k < c" "c < b$k"
  1.1812 +  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} = {})}
  1.1813 +                  \<subseteq> division_points ({a..b}) d" (is ?t1) and
  1.1814 +        "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} = {})}
  1.1815 +                  \<subseteq> division_points ({a..b}) d" (is ?t2)
  1.1816 +proof- note assm = division_ofD[OF assms(1)]
  1.1817 +  have *:"\<forall>i. a$i \<le> b$i"   "\<forall>i. a$i \<le> (\<chi> i. if i = k then min (b $ k) c else b $ i) $ i"
  1.1818 +    "\<forall>i. (\<chi> i. if i = k then max (a $ k) c else a $ i) $ i \<le> b$i"  "min (b $ k) c = c" "max (a $ k) c = c"
  1.1819 +    using assms using less_imp_le by auto
  1.1820 +  show ?t1 unfolding division_points_def interval_split[of a b]
  1.1821 +    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)] Cart_lambda_beta unfolding *
  1.1822 +    unfolding subset_eq apply(rule) unfolding mem_Collect_eq split_beta apply(erule bexE conjE)+ unfolding mem_Collect_eq apply(erule exE conjE)+
  1.1823 +  proof- fix i l x assume as:"a $ fst x < snd x" "snd x < (if fst x = k then c else b $ fst x)"
  1.1824 +      "interval_lowerbound i $ fst x = snd x \<or> interval_upperbound i $ fst x = snd x"  "i = l \<inter> {x. x $ k \<le> c}" "l \<in> d" "l \<inter> {x. x $ k \<le> c} \<noteq> {}"
  1.1825 +    from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  1.1826 +    have *:"\<forall>i. u $ i \<le> (\<chi> i. if i = k then min (v $ k) c else v $ i) $ i" using as(6) unfolding l interval_split interval_ne_empty as .
  1.1827 +    have **:"\<forall>i. u$i \<le> v$i" using l using as(6) unfolding interval_ne_empty[THEN sym] by auto
  1.1828 +    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> interval_upperbound i $ fst x = snd x)"
  1.1829 +      using as(1-3,5) unfolding l interval_split interval_ne_empty as interval_bounds[OF *] Cart_lambda_beta apply-
  1.1830 +      apply(rule,assumption,rule) defer apply(rule_tac x="{u..v}" in bexI) unfolding interval_bounds[OF **]
  1.1831 +      apply(case_tac[!] "fst x = k") using assms by auto
  1.1832 +  qed
  1.1833 +  show ?t2 unfolding division_points_def interval_split[of a b]
  1.1834 +    unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)] Cart_lambda_beta unfolding *
  1.1835 +    unfolding subset_eq apply(rule) unfolding mem_Collect_eq split_beta apply(erule bexE conjE)+ unfolding mem_Collect_eq apply(erule exE conjE)+
  1.1836 +  proof- fix i l x assume as:"(if fst x = k then c else a $ fst x) < snd x" "snd x < b $ fst x" "interval_lowerbound i $ fst x = snd x \<or> interval_upperbound i $ fst x = snd x"
  1.1837 +      "i = l \<inter> {x. c \<le> x $ k}" "l \<in> d" "l \<inter> {x. c \<le> x $ k} \<noteq> {}"
  1.1838 +    from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  1.1839 +    have *:"\<forall>i. (\<chi> i. if i = k then max (u $ k) c else u $ i) $ i \<le> v $ i" using as(6) unfolding l interval_split interval_ne_empty as .
  1.1840 +    have **:"\<forall>i. u$i \<le> v$i" using l using as(6) unfolding interval_ne_empty[THEN sym] by auto
  1.1841 +    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> interval_upperbound i $ fst x = snd x)"
  1.1842 +      using as(1-3,5) unfolding l interval_split interval_ne_empty as interval_bounds[OF *] Cart_lambda_beta apply-
  1.1843 +      apply rule defer apply(rule,assumption) apply(rule_tac x="{u..v}" in bexI) unfolding interval_bounds[OF **]
  1.1844 +      apply(case_tac[!] "fst x = k") using assms by auto qed qed
  1.1845 +
  1.1846 +lemma division_points_psubset:
  1.1847 +  assumes "d division_of {a..b}"  "\<forall>i. a$i < b$i"  "a$k < c" "c < b$k"
  1.1848 +  "l \<in> d" "interval_lowerbound l$k = c \<or> interval_upperbound l$k = c"
  1.1849 +  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> {}} \<subset> division_points ({a..b}) d" (is "?D1 \<subset> ?D") 
  1.1850 +        "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> {}} \<subset> division_points ({a..b}) d" (is "?D2 \<subset> ?D") 
  1.1851 +proof- have ab:"\<forall>i. a$i \<le> b$i" using assms(2) by(auto intro!:less_imp_le)
  1.1852 +  guess u v using division_ofD(4)[OF assms(1,5)] apply-by(erule exE)+ note l=this
  1.1853 +  have uv:"\<forall>i. u$i \<le> v$i" "\<forall>i. a$i \<le> u$i \<and> v$i \<le> b$i" using division_ofD(2,2,3)[OF assms(1,5)] unfolding l interval_ne_empty
  1.1854 +    unfolding subset_eq apply- defer apply(erule_tac x=u in ballE, erule_tac x=v in ballE) unfolding mem_interval by auto
  1.1855 +  have *:"interval_upperbound ({a..b} \<inter> {x. x $ k \<le> interval_upperbound l $ k}) $ k = interval_upperbound l $ k"
  1.1856 +         "interval_upperbound ({a..b} \<inter> {x. x $ k \<le> interval_lowerbound l $ k}) $ k = interval_lowerbound l $ k"
  1.1857 +    unfolding interval_split apply(subst interval_bounds) prefer 3 apply(subst interval_bounds)
  1.1858 +    unfolding l interval_bounds[OF uv(1)] using uv[rule_format,of k] ab by auto
  1.1859 +  have "\<exists>x. x \<in> ?D - ?D1" using assms(2-) apply-apply(erule disjE)
  1.1860 +    apply(rule_tac x="(k,(interval_lowerbound l)$k)" in exI) defer
  1.1861 +    apply(rule_tac x="(k,(interval_upperbound l)$k)" in exI)
  1.1862 +    unfolding division_points_def unfolding interval_bounds[OF ab]
  1.1863 +    apply (auto simp add:interval_bounds) unfolding * by auto
  1.1864 +  thus "?D1 \<subset> ?D" apply-apply(rule,rule division_points_subset[OF assms(1-4)]) by auto
  1.1865 +
  1.1866 +  have *:"interval_lowerbound ({a..b} \<inter> {x. x $ k \<ge> interval_lowerbound l $ k}) $ k = interval_lowerbound l $ k"
  1.1867 +         "interval_lowerbound ({a..b} \<inter> {x. x $ k \<ge> interval_upperbound l $ k}) $ k = interval_upperbound l $ k"
  1.1868 +    unfolding interval_split apply(subst interval_bounds) prefer 3 apply(subst interval_bounds)
  1.1869 +    unfolding l interval_bounds[OF uv(1)] using uv[rule_format,of k] ab by auto
  1.1870 +  have "\<exists>x. x \<in> ?D - ?D2" using assms(2-) apply-apply(erule disjE)
  1.1871 +    apply(rule_tac x="(k,(interval_lowerbound l)$k)" in exI) defer
  1.1872 +    apply(rule_tac x="(k,(interval_upperbound l)$k)" in exI)
  1.1873 +    unfolding division_points_def unfolding interval_bounds[OF ab]
  1.1874 +    apply (auto simp add:interval_bounds) unfolding * by auto
  1.1875 +  thus "?D2 \<subset> ?D" apply-apply(rule,rule division_points_subset[OF assms(1-4)]) by auto qed
  1.1876 +
  1.1877 +subsection {* Preservation by divisions and tagged divisions. *}
  1.1878 +
  1.1879 +lemma support_support[simp]:"support opp f (support opp f s) = support opp f s"
  1.1880 +  unfolding support_def by auto
  1.1881 +
  1.1882 +lemma iterate_support[simp]: "iterate opp (support opp f s) f = iterate opp s f"
  1.1883 +  unfolding iterate_def support_support by auto
  1.1884 +
  1.1885 +lemma iterate_expand_cases:
  1.1886 +  "iterate opp s f = (if finite(support opp f s) then iterate opp (support opp f s) f else neutral opp)"
  1.1887 +  apply(cases) apply(subst if_P,assumption) unfolding iterate_def support_support fold'_def by auto 
  1.1888 +
  1.1889 +lemma iterate_image: assumes "monoidal opp"  "inj_on f s"
  1.1890 +  shows "iterate opp (f ` s) g = iterate opp s (g \<circ> f)"
  1.1891 +proof- have *:"\<And>s. finite s \<Longrightarrow>  \<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<longrightarrow> x = y \<Longrightarrow>
  1.1892 +     iterate opp (f ` s) g = iterate opp s (g \<circ> f)"
  1.1893 +  proof- case goal1 show ?case using goal1
  1.1894 +    proof(induct s) case empty thus ?case using assms(1) by auto
  1.1895 +    next case (insert x s) show ?case unfolding iterate_insert[OF assms(1) insert(1)]
  1.1896 +        unfolding if_not_P[OF insert(2)] apply(subst insert(3)[THEN sym])
  1.1897 +        unfolding image_insert defer apply(subst iterate_insert[OF assms(1)])
  1.1898 +        apply(rule finite_imageI insert)+ apply(subst if_not_P)
  1.1899 +        unfolding image_iff o_def using insert(2,4) by auto
  1.1900 +    qed qed
  1.1901 +  show ?thesis 
  1.1902 +    apply(cases "finite (support opp g (f ` s))")
  1.1903 +    apply(subst (1) iterate_support[THEN sym],subst (2) iterate_support[THEN sym])
  1.1904 +    unfolding support_clauses apply(rule *)apply(rule finite_imageD,assumption) unfolding inj_on_def[symmetric]
  1.1905 +    apply(rule subset_inj_on[OF assms(2) support_subset])+
  1.1906 +    apply(subst iterate_expand_cases) unfolding support_clauses apply(simp only: if_False)
  1.1907 +    apply(subst iterate_expand_cases) apply(subst if_not_P) by auto qed
  1.1908 +
  1.1909 +
  1.1910 +(* This lemma about iterations comes up in a few places.                     *)
  1.1911 +lemma iterate_nonzero_image_lemma:
  1.1912 +  assumes "monoidal opp" "finite s" "g(a) = neutral opp"
  1.1913 +  "\<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<and> x \<noteq> y \<longrightarrow> g(f x) = neutral opp"
  1.1914 +  shows "iterate opp {f x | x. x \<in> s \<and> f x \<noteq> a} g = iterate opp s (g \<circ> f)"
  1.1915 +proof- have *:"{f x |x. x \<in> s \<and> ~(f x = a)} = f ` {x. x \<in> s \<and> ~(f x = a)}" by auto
  1.1916 +  have **:"support opp (g \<circ> f) {x \<in> s. f x \<noteq> a} = support opp (g \<circ> f) s"
  1.1917 +    unfolding support_def using assms(3) by auto
  1.1918 +  show ?thesis unfolding *
  1.1919 +    apply(subst iterate_support[THEN sym]) unfolding support_clauses
  1.1920 +    apply(subst iterate_image[OF assms(1)]) defer
  1.1921 +    apply(subst(2) iterate_support[THEN sym]) apply(subst **)
  1.1922 +    unfolding inj_on_def using assms(3,4) unfolding support_def by auto qed
  1.1923 +
  1.1924 +lemma iterate_eq_neutral:
  1.1925 +  assumes "monoidal opp"  "\<forall>x \<in> s. (f(x) = neutral opp)"
  1.1926 +  shows "(iterate opp s f = neutral opp)"
  1.1927 +proof- have *:"support opp f s = {}" unfolding support_def using assms(2) by auto
  1.1928 +  show ?thesis apply(subst iterate_support[THEN sym]) 
  1.1929 +    unfolding * using assms(1) by auto qed
  1.1930 +
  1.1931 +lemma iterate_op: assumes "monoidal opp" "finite s"
  1.1932 +  shows "iterate opp s (\<lambda>x. opp (f x) (g x)) = opp (iterate opp s f) (iterate opp s g)" using assms(2)
  1.1933 +proof(induct s) case empty thus ?case unfolding iterate_insert[OF assms(1)] using assms(1) by auto
  1.1934 +next case (insert x F) show ?case unfolding iterate_insert[OF assms(1) insert(1)] if_not_P[OF insert(2)] insert(3)
  1.1935 +    unfolding monoidal_ac[OF assms(1)] by(rule refl) qed
  1.1936 +
  1.1937 +lemma iterate_eq: assumes "monoidal opp" "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  1.1938 +  shows "iterate opp s f = iterate opp s g"
  1.1939 +proof- have *:"support opp g s = support opp f s"
  1.1940 +    unfolding support_def using assms(2) by auto
  1.1941 +  show ?thesis
  1.1942 +  proof(cases "finite (support opp f s)")
  1.1943 +    case False thus ?thesis apply(subst iterate_expand_cases,subst(2) iterate_expand_cases)
  1.1944 +      unfolding * by auto
  1.1945 +  next def su \<equiv> "support opp f s"
  1.1946 +    case True note support_subset[of opp f s] 
  1.1947 +    thus ?thesis apply- apply(subst iterate_support[THEN sym],subst(2) iterate_support[THEN sym]) unfolding * using True
  1.1948 +      unfolding su_def[symmetric]
  1.1949 +    proof(induct su) case empty show ?case by auto
  1.1950 +    next case (insert x s) show ?case unfolding iterate_insert[OF assms(1) insert(1)] 
  1.1951 +        unfolding if_not_P[OF insert(2)] apply(subst insert(3))
  1.1952 +        defer apply(subst assms(2)[of x]) using insert by auto qed qed qed
  1.1953 +
  1.1954 +lemma nonempty_witness: assumes "s \<noteq> {}" obtains x where "x \<in> s" using assms by auto
  1.1955 +
  1.1956 +lemma operative_division: fixes f::"(real^'n) set \<Rightarrow> 'a"
  1.1957 +  assumes "monoidal opp" "operative opp f" "d division_of {a..b}"
  1.1958 +  shows "iterate opp d f = f {a..b}"
  1.1959 +proof- def C \<equiv> "card (division_points {a..b} d)" thus ?thesis using assms
  1.1960 +  proof(induct C arbitrary:a b d rule:full_nat_induct)
  1.1961 +    case goal1
  1.1962 +    { presume *:"content {a..b} \<noteq> 0 \<Longrightarrow> ?case"
  1.1963 +      thus ?case apply-apply(cases) defer apply assumption
  1.1964 +      proof- assume as:"content {a..b} = 0"
  1.1965 +        show ?case unfolding operativeD(1)[OF assms(2) as] apply(rule iterate_eq_neutral[OF goal1(2)])
  1.1966 +        proof fix x assume x:"x\<in>d"
  1.1967 +          then guess u v apply(drule_tac division_ofD(4)[OF goal1(4)]) by(erule exE)+
  1.1968 +          thus "f x = neutral opp" using division_of_content_0[OF as goal1(4)] 
  1.1969 +            using operativeD(1)[OF assms(2)] x by auto
  1.1970 +        qed qed }
  1.1971 +    assume "content {a..b} \<noteq> 0" note ab = this[unfolded content_lt_nz[THEN sym] content_pos_lt_eq]
  1.1972 +    hence ab':"\<forall>i. a$i \<le> b$i" by (auto intro!: less_imp_le) show ?case 
  1.1973 +    proof(cases "division_points {a..b} d = {}")
  1.1974 +      case True have d':"\<forall>i\<in>d. \<exists>u v. i = {u..v} \<and>
  1.1975 +        (\<forall>j. 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)"
  1.1976 +        unfolding forall_in_division[OF goal1(4)] apply(rule,rule,rule)
  1.1977 +        apply(rule_tac x=a in exI,rule_tac x=b in exI) apply(rule,rule refl) apply(rule)
  1.1978 +      proof- fix u v j assume as:"{u..v} \<in> d" note division_ofD(3)[OF goal1(4) this]
  1.1979 +        hence uv:"\<forall>i. u$i \<le> v$i" "u$j \<le> v$j" unfolding interval_ne_empty by auto
  1.1980 +        have *:"\<And>p r Q. p \<or> r \<or> (\<forall>x\<in>d. Q x) \<Longrightarrow> p \<or> r \<or> (Q {u..v})" using as by auto
  1.1981 +        have "(j, u$j) \<notin> division_points {a..b} d"
  1.1982 +          "(j, v$j) \<notin> division_points {a..b} d" using True by auto
  1.1983 +        note this[unfolded de_Morgan_conj division_points_def mem_Collect_eq split_conv interval_bounds[OF ab'] bex_simps]
  1.1984 +        note *[OF this(1)] *[OF this(2)] note this[unfolded interval_bounds[OF uv(1)]]
  1.1985 +        moreover have "a$j \<le> u$j" "v$j \<le> b$j" using division_ofD(2,2,3)[OF goal1(4) as] 
  1.1986 +          unfolding subset_eq apply- apply(erule_tac x=u in ballE,erule_tac[3] x=v in ballE)
  1.1987 +          unfolding interval_ne_empty mem_interval by auto
  1.1988 +        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"
  1.1989 +          unfolding not_less de_Morgan_disj using ab[rule_format,of j] uv(2) by auto
  1.1990 +      qed have "(1/2) *\<^sub>R (a+b) \<in> {a..b}" unfolding mem_interval using ab by(auto intro!:less_imp_le)
  1.1991 +      note this[unfolded division_ofD(6)[OF goal1(4),THEN sym] Union_iff]
  1.1992 +      then guess i .. note i=this guess u v using d'[rule_format,OF i(1)] apply-by(erule exE conjE)+ note uv=this
  1.1993 +      have "{a..b} \<in> d"
  1.1994 +      proof- { presume "i = {a..b}" thus ?thesis using i by auto }
  1.1995 +        { presume "u = a" "v = b" thus "i = {a..b}" using uv by auto }
  1.1996 +        show "u = a" "v = b" unfolding Cart_eq
  1.1997 +        proof(rule_tac[!] allI) fix j note i(2)[unfolded uv mem_interval,rule_format,of j]
  1.1998 +          thus "u $ j = a $ j" "v $ j = b $ j" using uv(2)[rule_format,of j] by auto
  1.1999 +        qed qed
  1.2000 +      hence *:"d = insert {a..b} (d - {{a..b}})" by auto
  1.2001 +      have "iterate opp (d - {{a..b}}) f = neutral opp" apply(rule iterate_eq_neutral[OF goal1(2)])
  1.2002 +      proof fix x assume x:"x \<in> d - {{a..b}}" hence "x\<in>d" by auto note d'[rule_format,OF this]
  1.2003 +        then guess u v apply-by(erule exE conjE)+ note uv=this
  1.2004 +        have "u\<noteq>a \<or> v\<noteq>b" using x[unfolded uv] by auto  
  1.2005 +        then obtain j where "u$j \<noteq> a$j \<or> v$j \<noteq> b$j" unfolding Cart_eq by auto
  1.2006 +        hence "u$j = v$j" using uv(2)[rule_format,of j] by auto
  1.2007 +        hence "content {u..v} = 0"  unfolding content_eq_0 apply(rule_tac x=j in exI) by auto
  1.2008 +        thus "f x = neutral opp" unfolding uv(1) by(rule operativeD(1)[OF goal1(3)])
  1.2009 +      qed thus "iterate opp d f = f {a..b}" apply-apply(subst *) 
  1.2010 +        apply(subst iterate_insert[OF goal1(2)]) using goal1(2,4) by auto
  1.2011 +    next case False hence "\<exists>x. x\<in>division_points {a..b} d" by auto
  1.2012 +      then guess k c unfolding split_paired_Ex apply- unfolding division_points_def mem_Collect_eq split_conv
  1.2013 +        by(erule exE conjE)+ note kc=this[unfolded interval_bounds[OF ab']]
  1.2014 +      from this(3) guess j .. note j=this
  1.2015 +      def d1 \<equiv> "{l \<inter> {x. x$k \<le> c} | l. l \<in> d \<and> l \<inter> {x. x$k \<le> c} \<noteq> {}}"
  1.2016 +      def d2 \<equiv> "{l \<inter> {x. x$k \<ge> c} | l. l \<in> d \<and> l \<inter> {x. x$k \<ge> c} \<noteq> {}}"
  1.2017 +      def cb \<equiv> "(\<chi> i. if i = k then c else b$i)" and ca \<equiv> "(\<chi> i. if i = k then c else a$i)"
  1.2018 +      note division_points_psubset[OF goal1(4) ab kc(1-2) j]
  1.2019 +      note psubset_card_mono[OF _ this(1)] psubset_card_mono[OF _ this(2)]
  1.2020 +      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})"
  1.2021 +        apply- unfolding interval_split apply(rule_tac[!] goal1(1)[rule_format])
  1.2022 +        using division_split[OF goal1(4), where k=k and c=c]
  1.2023 +        unfolding interval_split d1_def[symmetric] d2_def[symmetric] unfolding goal1(2) Suc_le_mono
  1.2024 +        using goal1(2-3) using division_points_finite[OF goal1(4)] by auto
  1.2025 +      have "f {a..b} = opp (iterate opp d1 f) (iterate opp d2 f)" (is "_ = ?prev")
  1.2026 +        unfolding * apply(rule operativeD(2)) using goal1(3) .
  1.2027 +      also have "iterate opp d1 f = iterate opp d (\<lambda>l. f(l \<inter> {x. x$k \<le> c}))"
  1.2028 +        unfolding d1_def apply(rule iterate_nonzero_image_lemma[unfolded o_def])
  1.2029 +        unfolding empty_as_interval apply(rule goal1 division_of_finite operativeD[OF goal1(3)])+
  1.2030 +        unfolding empty_as_interval[THEN sym] apply(rule content_empty)
  1.2031 +      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" 
  1.2032 +        from division_ofD(4)[OF goal1(4) this(1)] guess u v apply-by(erule exE)+ note l=this
  1.2033 +        show "f (l \<inter> {x. x $ k \<le> c}) = neutral opp" unfolding l interval_split
  1.2034 +          apply(rule operativeD(1) goal1)+ unfolding interval_split[THEN sym] apply(rule division_split_left_inj)
  1.2035 +          apply(rule goal1) unfolding l[THEN sym] apply(rule as(1),rule as(2)) by(rule as)+
  1.2036 +      qed also have "iterate opp d2 f = iterate opp d (\<lambda>l. f(l \<inter> {x. x$k \<ge> c}))"
  1.2037 +        unfolding d2_def apply(rule iterate_nonzero_image_lemma[unfolded o_def])
  1.2038 +        unfolding empty_as_interval apply(rule goal1 division_of_finite operativeD[OF goal1(3)])+
  1.2039 +        unfolding empty_as_interval[THEN sym] apply(rule content_empty)
  1.2040 +      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" 
  1.2041 +        from division_ofD(4)[OF goal1(4) this(1)] guess u v apply-by(erule exE)+ note l=this
  1.2042 +        show "f (l \<inter> {x. x $ k \<ge> c}) = neutral opp" unfolding l interval_split
  1.2043 +          apply(rule operativeD(1) goal1)+ unfolding interval_split[THEN sym] apply(rule division_split_right_inj)
  1.2044 +          apply(rule goal1) unfolding l[THEN sym] apply(rule as(1),rule as(2)) by(rule as)+
  1.2045 +      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}))"
  1.2046 +        unfolding forall_in_division[OF goal1(4)] apply(rule,rule,rule,rule operativeD(2)) using goal1(3) .
  1.2047 +      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})))
  1.2048 +        = iterate opp d f" apply(subst(3) iterate_eq[OF _ *[rule_format]]) prefer 3
  1.2049 +        apply(rule iterate_op[THEN sym]) using goal1 by auto
  1.2050 +      finally show ?thesis by auto
  1.2051 +    qed qed qed 
  1.2052 +
  1.2053 +lemma iterate_image_nonzero: assumes "monoidal opp"
  1.2054 +  "finite s" "\<forall>x\<in>s. \<forall>y\<in>s. ~(x = y) \<and> f x = f y \<longrightarrow> g(f x) = neutral opp"
  1.2055 +  shows "iterate opp (f ` s) g = iterate opp s (g \<circ> f)" using assms
  1.2056 +proof(induct rule:finite_subset_induct[OF assms(2) subset_refl])
  1.2057 +  case goal1 show ?case using assms(1) by auto
  1.2058 +next case goal2 have *:"\<And>x y. y = neutral opp \<Longrightarrow> x = opp y x" using assms(1) by auto
  1.2059 +  show ?case unfolding image_insert apply(subst iterate_insert[OF assms(1)])
  1.2060 +    apply(rule finite_imageI goal2)+
  1.2061 +    apply(cases "f a \<in> f ` F") unfolding if_P if_not_P apply(subst goal2(4)[OF assms(1) goal2(1)]) defer
  1.2062 +    apply(subst iterate_insert[OF assms(1) goal2(1)]) defer
  1.2063 +    apply(subst iterate_insert[OF assms(1) goal2(1)])
  1.2064 +    unfolding if_not_P[OF goal2(3)] defer unfolding image_iff defer apply(erule bexE)
  1.2065 +    apply(rule *) unfolding o_def apply(rule_tac y=x in goal2(7)[rule_format])
  1.2066 +    using goal2 unfolding o_def by auto qed 
  1.2067 +
  1.2068 +lemma operative_tagged_division: assumes "monoidal opp" "operative opp f" "d tagged_division_of {a..b}"
  1.2069 +  shows "iterate(opp) d (\<lambda>(x,l). f l) = f {a..b}"
  1.2070 +proof- have *:"(\<lambda>(x,l). f l) = (f o snd)" unfolding o_def by(rule,auto) note assm = tagged_division_ofD[OF assms(3)]
  1.2071 +  have "iterate(opp) d (\<lambda>(x,l). f l) = iterate opp (snd ` d) f" unfolding *
  1.2072 +    apply(rule iterate_image_nonzero[THEN sym,OF assms(1)]) apply(rule tagged_division_of_finite assms)+ 
  1.2073 +    unfolding Ball_def split_paired_All snd_conv apply(rule,rule,rule,rule,rule,rule,rule,erule conjE)
  1.2074 +  proof- fix a b aa ba assume as:"(a, b) \<in> d" "(aa, ba) \<in> d" "(a, b) \<noteq> (aa, ba)" "b = ba"
  1.2075 +    guess u v using assm(4)[OF as(1)] apply-by(erule exE)+ note uv=this
  1.2076 +    show "f b = neutral opp" unfolding uv apply(rule operativeD(1)[OF assms(2)])
  1.2077 +      unfolding content_eq_0_interior using tagged_division_ofD(5)[OF assms(3) as(1-3)]
  1.2078 +      unfolding as(4)[THEN sym] uv by auto
  1.2079 +  qed also have "\<dots> = f {a..b}" 
  1.2080 +    using operative_division[OF assms(1-2) division_of_tagged_division[OF assms(3)]] .
  1.2081 +  finally show ?thesis . qed
  1.2082 +
  1.2083 +subsection {* Additivity of content. *}
  1.2084 +
  1.2085 +lemma setsum_iterate:assumes "finite s" shows "setsum f s = iterate op + s f"
  1.2086 +proof- have *:"setsum f s = setsum f (support op + f s)"
  1.2087 +    apply(rule setsum_mono_zero_right)
  1.2088 +    unfolding support_def neutral_monoid using assms by auto
  1.2089 +  thus ?thesis unfolding * setsum_def iterate_def fold_image_def fold'_def
  1.2090 +    unfolding neutral_monoid . qed
  1.2091 +
  1.2092 +lemma additive_content_division: assumes "d division_of {a..b}"
  1.2093 +  shows "setsum content d = content({a..b})"
  1.2094 +  unfolding operative_division[OF monoidal_monoid operative_content assms,THEN sym]
  1.2095 +  apply(subst setsum_iterate) using assms by auto
  1.2096 +
  1.2097 +lemma additive_content_tagged_division:
  1.2098 +  assumes "d tagged_division_of {a..b}"
  1.2099 +  shows "setsum (\<lambda>(x,l). content l) d = content({a..b})"
  1.2100 +  unfolding operative_tagged_division[OF monoidal_monoid operative_content assms,THEN sym]
  1.2101 +  apply(subst setsum_iterate) using assms by auto
  1.2102 +
  1.2103 +subsection {* Finally, the integral of a constant\<forall> *}
  1.2104 +
  1.2105 +lemma has_integral_const[intro]:
  1.2106 +  "((\<lambda>x. c) has_integral (content({a..b::real^'n}) *\<^sub>R c)) ({a..b})"
  1.2107 +  unfolding has_integral apply(rule,rule,rule_tac x="\<lambda>x. ball x 1" in exI)
  1.2108 +  apply(rule,rule gauge_trivial)apply(rule,rule,erule conjE)
  1.2109 +  unfolding split_def apply(subst scaleR_left.setsum[THEN sym, unfolded o_def])
  1.2110 +  defer apply(subst additive_content_tagged_division[unfolded split_def]) apply assumption by auto
  1.2111 +
  1.2112 +subsection {* Bounds on the norm of Riemann sums and the integral itself. *}
  1.2113 +
  1.2114 +lemma dsum_bound: assumes "p division_of {a..b}" "norm(c) \<le> e"
  1.2115 +  shows "norm(setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> e * content({a..b})" (is "?l \<le> ?r")
  1.2116 +  apply(rule order_trans,rule setsum_norm) defer unfolding norm_scaleR setsum_left_distrib[THEN sym]
  1.2117 +  apply(rule order_trans[OF mult_left_mono],rule assms,rule setsum_abs_ge_zero)
  1.2118 +  apply(subst real_mult_commute) apply(rule mult_left_mono)
  1.2119 +  apply(rule order_trans[of _ "setsum content p"]) apply(rule eq_refl,rule setsum_cong2)
  1.2120 +  apply(subst abs_of_nonneg) unfolding additive_content_division[OF assms(1)]
  1.2121 +proof- from order_trans[OF norm_ge_zero[of c] assms(2)] show "0 \<le> e" .
  1.2122 +  fix x assume "x\<in>p" from division_ofD(4)[OF assms(1) this] guess u v apply-by(erule exE)+
  1.2123 +  thus "0 \<le> content x" using content_pos_le by auto
  1.2124 +qed(insert assms,auto)
  1.2125 +
  1.2126 +lemma rsum_bound: assumes "p tagged_division_of {a..b}" "\<forall>x\<in>{a..b}. norm(f x) \<le> e"
  1.2127 +  shows "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> e * content({a..b})"
  1.2128 +proof(cases "{a..b} = {}") case True
  1.2129 +  show ?thesis using assms(1) unfolding True tagged_division_of_trivial by auto
  1.2130 +next case False show ?thesis
  1.2131 +    apply(rule order_trans,rule setsum_norm) defer unfolding split_def norm_scaleR
  1.2132 +    apply(rule order_trans[OF setsum_mono]) apply(rule mult_left_mono[OF _ abs_ge_zero, of _ e]) defer
  1.2133 +    unfolding setsum_left_distrib[THEN sym] apply(subst real_mult_commute) apply(rule mult_left_mono)
  1.2134 +    apply(rule order_trans[of _ "setsum (content \<circ> snd) p"]) apply(rule eq_refl,rule setsum_cong2)
  1.2135 +    apply(subst o_def, rule abs_of_nonneg)
  1.2136 +  proof- show "setsum (content \<circ> snd) p \<le> content {a..b}" apply(rule eq_refl)
  1.2137 +      unfolding additive_content_tagged_division[OF assms(1),THEN sym] split_def by auto
  1.2138 +    guess w using nonempty_witness[OF False] .
  1.2139 +    thus "e\<ge>0" apply-apply(rule order_trans) defer apply(rule assms(2)[rule_format],assumption) by auto
  1.2140 +    fix xk assume *:"xk\<in>p" guess x k  using surj_pair[of xk] apply-by(erule exE)+ note xk = this *[unfolded this]
  1.2141 +    from tagged_division_ofD(4)[OF assms(1) xk(2)] guess u v apply-by(erule exE)+ note uv=this
  1.2142 +    show "0\<le> content (snd xk)" unfolding xk snd_conv uv by(rule content_pos_le)
  1.2143 +    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
  1.2144 +  qed(insert assms,auto) qed
  1.2145 +
  1.2146 +lemma rsum_diff_bound:
  1.2147 +  assumes "p tagged_division_of {a..b}"  "\<forall>x\<in>{a..b}. norm(f x - g x) \<le> e"
  1.2148 +  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})"
  1.2149 +  apply(rule order_trans[OF _ rsum_bound[OF assms]]) apply(rule eq_refl) apply(rule arg_cong[where f=norm])
  1.2150 +  unfolding setsum_subtractf[THEN sym] apply(rule setsum_cong2) unfolding scaleR.diff_right by auto
  1.2151 +
  1.2152 +lemma has_integral_bound: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
  1.2153 +  assumes "0 \<le> B" "(f has_integral i) ({a..b})" "\<forall>x\<in>{a..b}. norm(f x) \<le> B"
  1.2154 +  shows "norm i \<le> B * content {a..b}"
  1.2155 +proof- let ?P = "content {a..b} > 0" { presume "?P \<Longrightarrow> ?thesis"
  1.2156 +    thus ?thesis proof(cases ?P) case False
  1.2157 +      hence *:"content {a..b} = 0" using content_lt_nz by auto
  1.2158 +      hence **:"i = 0" using assms(2) apply(subst has_integral_null_eq[THEN sym]) by auto
  1.2159 +      show ?thesis unfolding * ** using assms(1) by auto
  1.2160 +    qed auto } assume ab:?P
  1.2161 +  { presume "\<not> ?thesis \<Longrightarrow> False" thus ?thesis by auto }
  1.2162 +  assume "\<not> ?thesis" hence *:"norm i - B * content {a..b} > 0" by auto
  1.2163 +  from assms(2)[unfolded has_integral,rule_format,OF *] guess d apply-by(erule exE conjE)+ note d=this[rule_format]
  1.2164 +  from fine_division_exists[OF this(1), of a b] guess p . note p=this
  1.2165 +  have *:"\<And>s B. norm s \<le> B \<Longrightarrow> \<not> (norm (s - i) < norm i - B)"
  1.2166 +  proof- case goal1 thus ?case unfolding not_less
  1.2167 +    using norm_triangle_sub[of i s] unfolding norm_minus_commute by auto
  1.2168 +  qed show False using d(2)[OF conjI[OF p]] *[OF rsum_bound[OF p(1) assms(3)]] by auto qed
  1.2169 +
  1.2170 +subsection {* Similar theorems about relationship among components. *}
  1.2171 +
  1.2172 +lemma rsum_component_le: fixes f::"real^'n \<Rightarrow> real^'m"
  1.2173 +  assumes "p tagged_division_of {a..b}"  "\<forall>x\<in>{a..b}. (f x)$i \<le> (g x)$i"
  1.2174 +  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"
  1.2175 +  unfolding setsum_component apply(rule setsum_mono)
  1.2176 +  apply(rule mp) defer apply assumption apply(induct_tac x,rule) unfolding split_conv
  1.2177 +proof- fix a b assume ab:"(a,b) \<in> p" note assm = tagged_division_ofD(2-4)[OF assms(1) ab]
  1.2178 +  from this(3) guess u v apply-by(erule exE)+ note b=this
  1.2179 +  show "(content b *\<^sub>R f a) $ i \<le> (content b *\<^sub>R g a) $ i" unfolding b
  1.2180 +    unfolding Cart_nth.scaleR real_scaleR_def apply(rule mult_left_mono)
  1.2181 +    defer apply(rule content_pos_le,rule assms(2)[rule_format]) using assm by auto qed
  1.2182 +
  1.2183 +lemma has_integral_component_le: fixes f::"real^'n \<Rightarrow> real^'m"
  1.2184 +  assumes "(f has_integral i) s" "(g has_integral j) s"  "\<forall>x\<in>s. (f x)$k \<le> (g x)$k"
  1.2185 +  shows "i$k \<le> j$k"
  1.2186 +proof- have lem:"\<And>a b g i j. \<And>f::real^'n \<Rightarrow> real^'m. (f has_integral i) ({a..b}) \<Longrightarrow> 
  1.2187 +    (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"
  1.2188 +  proof(rule ccontr) case goal1 hence *:"0 < (i$k - j$k) / 3" by auto
  1.2189 +    guess d1 using goal1(1)[unfolded has_integral,rule_format,OF *] apply-by(erule exE conjE)+ note d1=this[rule_format]
  1.2190 +    guess d2 using goal1(2)[unfolded has_integral,rule_format,OF *] apply-by(erule exE conjE)+ note d2=this[rule_format]
  1.2191 +    guess p using fine_division_exists[OF gauge_inter[OF d1(1) d2(1)], of a b] unfolding fine_inter .
  1.2192 +    note p = this(1) conjunctD2[OF this(2)]  note le_less_trans[OF component_le_norm, of _ _ k]
  1.2193 +    note this[OF d1(2)[OF conjI[OF p(1,2)]]] this[OF d2(2)[OF conjI[OF p(1,3)]]]
  1.2194 +    thus False unfolding Cart_nth.diff using rsum_component_le[OF p(1) goal1(3)] by smt
  1.2195 +  qed let ?P = "\<exists>a b. s = {a..b}"
  1.2196 +  { presume "\<not> ?P \<Longrightarrow> ?thesis" thus ?thesis proof(cases ?P)
  1.2197 +      case True then guess a b apply-by(erule exE)+ note s=this
  1.2198 +      show ?thesis apply(rule lem) using assms[unfolded s] by auto
  1.2199 +    qed auto } assume as:"\<not> ?P"
  1.2200 +  { presume "\<not> ?thesis \<Longrightarrow> False" thus ?thesis by auto }
  1.2201 +  assume "\<not> i$k \<le> j$k" hence ij:"(i$k - j$k) / 3 > 0" by auto
  1.2202 +  note has_integral_altD[OF _ as this] from this[OF assms(1)] this[OF assms(2)] guess B1 B2 . note B=this[rule_format]
  1.2203 +  have "bounded (ball 0 B1 \<union> ball (0::real^'n) B2)" unfolding bounded_Un by(rule conjI bounded_ball)+
  1.2204 +  from bounded_subset_closed_interval[OF this] guess a b apply- by(erule exE)+
  1.2205 +  note ab = conjunctD2[OF this[unfolded Un_subset_iff]]
  1.2206 +  guess w1 using B(2)[OF ab(1)] .. note w1=conjunctD2[OF this]
  1.2207 +  guess w2 using B(4)[OF ab(2)] .. note w2=conjunctD2[OF this]
  1.2208 +  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(*SMTSMT*)
  1.2209 +  note le_less_trans[OF component_le_norm[of _ k]] note this[OF w1(2)] this[OF w2(2)] moreover
  1.2210 +  have "w1$k \<le> w2$k" apply(rule lem[OF w1(1) w2(1)]) using assms by auto ultimately
  1.2211 +  show False unfolding Cart_nth.diff by(rule *) qed
  1.2212 +
  1.2213 +lemma integral_component_le: fixes f::"real^'n \<Rightarrow> real^'m"
  1.2214 +  assumes "f integrable_on s" "g integrable_on s"  "\<forall>x\<in>s. (f x)$k \<le> (g x)$k"
  1.2215 +  shows "(integral s f)$k \<le> (integral s g)$k"
  1.2216 +  apply(rule has_integral_component_le) using integrable_integral assms by auto
  1.2217 +
  1.2218 +lemma has_integral_dest_vec1_le: fixes f::"real^'n \<Rightarrow> real^1"
  1.2219 +  assumes "(f has_integral i) s"  "(g has_integral j) s" "\<forall>x\<in>s. f x \<le> g x"
  1.2220 +  shows "dest_vec1 i \<le> dest_vec1 j" apply(rule has_integral_component_le[OF assms(1-2)])
  1.2221 +  using assms(3) unfolding vector_le_def by auto
  1.2222 +
  1.2223 +lemma integral_dest_vec1_le: fixes f::"real^'n \<Rightarrow> real^1"
  1.2224 +  assumes "f integrable_on s" "g integrable_on s" "\<forall>x\<in>s. f x \<le> g x"
  1.2225 +  shows "dest_vec1(integral s f) \<le> dest_vec1(integral s g)"
  1.2226 +  apply(rule has_integral_dest_vec1_le) apply(rule_tac[1-2] integrable_integral) using assms by auto
  1.2227 +
  1.2228 +lemma has_integral_component_pos: fixes f::"real^'n \<Rightarrow> real^'m"
  1.2229 +  assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> (f x)$k" shows "0 \<le> i$k"
  1.2230 +  using has_integral_component_le[OF has_integral_0 assms(1)] using assms(2) by auto
  1.2231 +
  1.2232 +lemma integral_component_pos: fixes f::"real^'n \<Rightarrow> real^'m"
  1.2233 +  assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> (f x)$k" shows "0 \<le> (integral s f)$k"
  1.2234 +  apply(rule has_integral_component_pos) using assms by auto
  1.2235 +
  1.2236 +lemma has_integral_dest_vec1_pos: fixes f::"real^'n \<Rightarrow> real^1"
  1.2237 +  assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> i"
  1.2238 +  using has_integral_component_pos[OF assms(1), of 1]
  1.2239 +  using assms(2) unfolding vector_le_def by auto
  1.2240 +
  1.2241 +lemma integral_dest_vec1_pos: fixes f::"real^'n \<Rightarrow> real^1"
  1.2242 +  assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> integral s f"
  1.2243 +  apply(rule has_integral_dest_vec1_pos) using assms by auto
  1.2244 +
  1.2245 +lemma has_integral_component_neg: fixes f::"real^'n \<Rightarrow> real^'m"
  1.2246 +  assumes "(f has_integral i) s" "\<forall>x\<in>s. (f x)$k \<le> 0" shows "i$k \<le> 0"
  1.2247 +  using has_integral_component_le[OF assms(1) has_integral_0] assms(2) by auto
  1.2248 +
  1.2249 +lemma has_integral_dest_vec1_neg: fixes f::"real^'n \<Rightarrow> real^1"
  1.2250 +  assumes "(f has_integral i) s" "\<forall>x\<in>s. f x \<le> 0" shows "i \<le> 0"
  1.2251 +  using has_integral_component_neg[OF assms(1),of 1] using assms(2) by auto
  1.2252 +
  1.2253 +lemma has_integral_component_lbound:
  1.2254 +  assumes "(f has_integral i) {a..b}"  "\<forall>x\<in>{a..b}. B \<le> f(x)$k" shows "B * content {a..b} \<le> i$k"
  1.2255 +  using has_integral_component_le[OF has_integral_const assms(1),of "(\<chi> i. B)" k] assms(2)
  1.2256 +  unfolding Cart_lambda_beta vector_scaleR_component by(auto simp add:field_simps)
  1.2257 +
  1.2258 +lemma has_integral_component_ubound: 
  1.2259 +  assumes "(f has_integral i) {a..b}" "\<forall>x\<in>{a..b}. f x$k \<le> B"
  1.2260 +  shows "i$k \<le> B * content({a..b})"
  1.2261 +  using has_integral_component_le[OF assms(1) has_integral_const, of k "vec B"]
  1.2262 +  unfolding vec_component Cart_nth.scaleR using assms(2) by(auto simp add:field_simps)
  1.2263 +
  1.2264 +lemma integral_component_lbound:
  1.2265 +  assumes "f integrable_on {a..b}" "\<forall>x\<in>{a..b}. B \<le> f(x)$k"
  1.2266 +  shows "B * content({a..b}) \<le> (integral({a..b}) f)$k"
  1.2267 +  apply(rule has_integral_component_lbound) using assms unfolding has_integral_integral by auto
  1.2268 +
  1.2269 +lemma integral_component_ubound:
  1.2270 +  assumes "f integrable_on {a..b}" "\<forall>x\<in>{a..b}. f(x)$k \<le> B"
  1.2271 +  shows "(integral({a..b}) f)$k \<le> B * content({a..b})"
  1.2272 +  apply(rule has_integral_component_ubound) using assms unfolding has_integral_integral by auto
  1.2273 +
  1.2274 +subsection {* Uniform limit of integrable functions is integrable. *}
  1.2275 +
  1.2276 +lemma real_arch_invD:
  1.2277 +  "0 < (e::real) \<Longrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
  1.2278 +  by(subst(asm) real_arch_inv)
  1.2279 +
  1.2280 +lemma integrable_uniform_limit: fixes f::"real^'n \<Rightarrow> 'a::banach"
  1.2281 +  assumes "\<forall>e>0. \<exists>g. (\<forall>x\<in>{a..b}. norm(f x - g x) \<le> e) \<and> g integrable_on {a..b}"
  1.2282 +  shows "f integrable_on {a..b}"
  1.2283 +proof- { presume *:"content {a..b} > 0 \<Longrightarrow> ?thesis"
  1.2284 +    show ?thesis apply cases apply(rule *,assumption)
  1.2285 +      unfolding content_lt_nz integrable_on_def using has_integral_null by auto }
  1.2286 +  assume as:"content {a..b} > 0"
  1.2287 +  have *:"\<And>P. \<forall>e>(0::real). P e \<Longrightarrow> \<forall>n::nat. P (inverse (real n+1))" by auto
  1.2288 +  from choice[OF *[OF assms]] guess g .. note g=conjunctD2[OF this[rule_format],rule_format]
  1.2289 +  from choice[OF allI[OF g(2)[unfolded integrable_on_def], of "\<lambda>x. x"]] guess i .. note i=this[rule_format]
  1.2290 +  
  1.2291 +  have "Cauchy i" unfolding Cauchy_def
  1.2292 +  proof(rule,rule) fix e::real assume "e>0"
  1.2293 +    hence "e / 4 / content {a..b} > 0" using as by(auto simp add:field_simps)
  1.2294 +    then guess M apply-apply(subst(asm) real_arch_inv) by(erule exE conjE)+ note M=this
  1.2295 +    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)
  1.2296 +    proof- case goal1 have "e/4>0" using `e>0` by auto note * = i[unfolded has_integral,rule_format,OF this]
  1.2297 +      from *[of m] guess gm apply-by(erule conjE exE)+ note gm=this[rule_format]
  1.2298 +      from *[of n] guess gn apply-by(erule conjE exE)+ note gn=this[rule_format]
  1.2299 +      from fine_division_exists[OF gauge_inter[OF gm(1) gn(1)], of a b] guess p . note p=this
  1.2300 +      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"
  1.2301 +      proof- case goal1 have "norm (i1 - i2) \<le> norm (i1 - s1) + norm (s1 - s2) + norm (s2 - i2)"
  1.2302 +          using norm_triangle_ineq[of "i1 - s1" "s1 - i2"]
  1.2303 +          using norm_triangle_ineq[of "s1 - s2" "s2 - i2"] by(auto simp add:group_simps)
  1.2304 +        also have "\<dots> < e" using goal1 unfolding norm_minus_commute by(auto simp add:group_simps)
  1.2305 +        finally show ?case .
  1.2306 +      qed
  1.2307 +      show ?case unfolding vector_dist_norm apply(rule lem2) defer
  1.2308 +        apply(rule gm(2)[OF conjI[OF p(1)]],rule_tac[2] gn(2)[OF conjI[OF p(1)]])
  1.2309 +        using conjunctD2[OF p(2)[unfolded fine_inter]] apply- apply assumption+ apply(rule order_trans)
  1.2310 +        apply(rule rsum_diff_bound[OF p(1), where e="2 / real M"])
  1.2311 +      proof show "2 / real M * content {a..b} \<le> e / 2" unfolding divide_inverse 
  1.2312 +          using M as by(auto simp add:field_simps)
  1.2313 +        fix x assume x:"x \<in> {a..b}"
  1.2314 +        have "norm (f x - g n x) + norm (f x - g m x) \<le> inverse (real n + 1) + inverse (real m + 1)" 
  1.2315 +            using g(1)[OF x, of n] g(1)[OF x, of m] by auto
  1.2316 +        also have "\<dots> \<le> inverse (real M) + inverse (real M)" apply(rule add_mono)
  1.2317 +          apply(rule_tac[!] le_imp_inverse_le) using goal1 M by auto
  1.2318 +        also have "\<dots> = 2 / real M" unfolding real_divide_def by auto
  1.2319 +        finally show "norm (g n x - g m x) \<le> 2 / real M"
  1.2320 +          using norm_triangle_le[of "g n x - f x" "f x - g m x" "2 / real M"]
  1.2321 +          by(auto simp add:group_simps simp add:norm_minus_commute)
  1.2322 +      qed qed qed
  1.2323 +  from this[unfolded convergent_eq_cauchy[THEN sym]] guess s .. note s=this
  1.2324 +
  1.2325 +  show ?thesis unfolding integrable_on_def apply(rule_tac x=s in exI) unfolding has_integral
  1.2326 +  proof(rule,rule)  
  1.2327 +    case goal1 hence *:"e/3 > 0" by auto
  1.2328 +    from s[unfolded Lim_sequentially,rule_format,OF this] guess N1 .. note N1=this
  1.2329 +    from goal1 as have "e / 3 / content {a..b} > 0" by(auto simp add:field_simps)
  1.2330 +    from real_arch_invD[OF this] guess N2 apply-by(erule exE conjE)+ note N2=this
  1.2331 +    from i[of "N1 + N2",unfolded has_integral,rule_format,OF *] guess g' .. note g'=conjunctD2[OF this,rule_format]
  1.2332 +    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"
  1.2333 +    proof- case goal1 have "norm (sf - s) \<le> norm (sf - sg) + norm (sg - i) + norm (i - s)"
  1.2334 +        using norm_triangle_ineq[of "sf - sg" "sg - s"]
  1.2335 +        using norm_triangle_ineq[of "sg -  i" " i - s"] by(auto simp add:group_simps)
  1.2336 +      also have "\<dots> < e" using goal1 unfolding norm_minus_commute by(auto simp add:group_simps)
  1.2337 +      finally show ?case .
  1.2338 +    qed
  1.2339 +    show ?case apply(rule_tac x=g' in exI) apply(rule,rule g')
  1.2340 +    proof(rule,rule) fix p assume p:"p tagged_division_of {a..b} \<and> g' fine p" note * = g'(2)[OF this]
  1.2341 +      show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - s) < e" apply-apply(rule lem[OF _ _ *])
  1.2342 +        apply(rule order_trans,rule rsum_diff_bound[OF p[THEN conjunct1]]) apply(rule,rule g,assumption)
  1.2343 +      proof- have "content {a..b} < e / 3 * (real N2)"
  1.2344 +          using N2 unfolding inverse_eq_divide using as by(auto simp add:field_simps)
  1.2345 +        hence "content {a..b} < e / 3 * (real (N1 + N2) + 1)"
  1.2346 +          apply-apply(rule less_le_trans,assumption) using `e>0` by auto 
  1.2347 +        thus "inverse (real (N1 + N2) + 1) * content {a..b} \<le> e / 3"
  1.2348 +          unfolding inverse_eq_divide by(auto simp add:field_simps)
  1.2349 +        show "norm (i (N1 + N2) - s) < e / 3" by(rule N1[rule_format,unfolded vector_dist_norm],auto)
  1.2350 +      qed qed qed qed
  1.2351 +
  1.2352 +subsection {* Negligible sets. *}
  1.2353 +
  1.2354 +definition "indicator s \<equiv> (\<lambda>x. if x \<in> s then 1 else (0::real))"
  1.2355 +
  1.2356 +lemma dest_vec1_indicator:
  1.2357 + "indicator s x = (if x \<in> s then 1 else 0)" unfolding indicator_def by auto
  1.2358 +
  1.2359 +lemma indicator_pos_le[intro]: "0 \<le> (indicator s x)" unfolding indicator_def by auto
  1.2360 +
  1.2361 +lemma indicator_le_1[intro]: "(indicator s x) \<le> 1" unfolding indicator_def by auto
  1.2362 +
  1.2363 +lemma dest_vec1_indicator_abs_le_1: "abs(indicator s x) \<le> 1"
  1.2364 +  unfolding indicator_def by auto
  1.2365 +
  1.2366 +definition "negligible (s::(real^'n) set) \<equiv> (\<forall>a b. ((indicator s) has_integral 0) {a..b})"
  1.2367 +
  1.2368 +lemma indicator_simps[simp]:"x\<in>s \<Longrightarrow> indicator s x = 1" "x\<notin>s \<Longrightarrow> indicator s x = 0"
  1.2369 +  unfolding indicator_def by auto
  1.2370 +
  1.2371 +subsection {* Negligibility of hyperplane. *}
  1.2372 +
  1.2373 +lemma vsum_nonzero_image_lemma: 
  1.2374 +  assumes "finite s" "g(a) = 0"
  1.2375 +  "\<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<and> x \<noteq> y \<longrightarrow> g(f x) = 0"
  1.2376 +  shows "setsum g {f x |x. x \<in> s \<and> f x \<noteq> a} = setsum (g o f) s"
  1.2377 +  unfolding setsum_iterate[OF assms(1)] apply(subst setsum_iterate) defer
  1.2378 +  apply(rule iterate_nonzero_image_lemma) apply(rule assms monoidal_monoid)+
  1.2379 +  unfolding assms using neutral_add unfolding neutral_add using assms by auto 
  1.2380 +
  1.2381 +lemma interval_doublesplit: shows "{a..b} \<inter> {x . abs(x$k - c) \<le> (e::real)} =
  1.2382 +  {(\<chi> i. if i = k then max (a$k) (c - e) else a$i) .. (\<chi> i. if i = k then min (b$k) (c + e) else b$i)}"
  1.2383 +proof- have *:"\<And>x c e::real. abs(x - c) \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e" by auto
  1.2384 +  have **:"\<And>s P Q. s \<inter> {x. P x \<and> Q x} = (s \<inter> {x. Q x}) \<inter> {x. P x}" by blast
  1.2385 +  show ?thesis unfolding * ** interval_split by(rule refl) qed
  1.2386 +
  1.2387 +lemma division_doublesplit: assumes "p division_of {a..b::real^'n}" 
  1.2388 +  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})"
  1.2389 +proof- have *:"\<And>x c. abs(x - c) \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e" by auto
  1.2390 +  have **:"\<And>p q p' q'. p division_of q \<Longrightarrow> p = p' \<Longrightarrow> q = q' \<Longrightarrow> p' division_of q'" by auto
  1.2391 +  note division_split(1)[OF assms, where c="c+e" and k=k,unfolded interval_split]
  1.2392 +  note division_split(2)[OF this, where c="c-e" and k=k] 
  1.2393 +  thus ?thesis apply(rule **) unfolding interval_doublesplit unfolding * unfolding interval_split interval_doublesplit
  1.2394 +    apply(rule set_ext) unfolding mem_Collect_eq apply rule apply(erule conjE exE)+ apply(rule_tac x=la in exI) defer
  1.2395 +    apply(erule conjE exE)+ apply(rule_tac x="l \<inter> {x. c + e \<ge> x $ k}" in exI) apply rule defer apply rule
  1.2396 +    apply(rule_tac x=l in exI) by blast+ qed
  1.2397 +
  1.2398 +lemma content_doublesplit: assumes "0 < e"
  1.2399 +  obtains d where "0 < d" "content({a..b} \<inter> {x. abs(x$k - c) \<le> d}) < e"
  1.2400 +proof(cases "content {a..b} = 0")
  1.2401 +  case True show ?thesis apply(rule that[of 1]) defer unfolding interval_doublesplit
  1.2402 +    apply(rule le_less_trans[OF content_subset]) defer apply(subst True)
  1.2403 +    unfolding interval_doublesplit[THEN sym] using assms by auto 
  1.2404 +next case False def d \<equiv> "e / 3 / setprod (\<lambda>i. b$i - a$i) (UNIV - {k})"
  1.2405 +  note False[unfolded content_eq_0 not_ex not_le, rule_format]
  1.2406 +  hence prod0:"0 < setprod (\<lambda>i. b$i - a$i) (UNIV - {k})" apply-apply(rule setprod_pos) by smt
  1.2407 +  hence "d > 0" unfolding d_def using assms by(auto simp add:field_simps) thus ?thesis
  1.2408 +  proof(rule that[of d]) have *:"UNIV = insert k (UNIV - {k})" by auto
  1.2409 +    have **:"{a..b} \<inter> {x. \<bar>x $ k - c\<bar> \<le> d} \<noteq> {} \<Longrightarrow> 
  1.2410 +      (\<Prod>i\<in>UNIV - {k}. interval_upperbound ({a..b} \<inter> {x. \<bar>x $ k - c\<bar> \<le> d}) $ i - interval_lowerbound ({a..b} \<inter> {x. \<bar>x $ k - c\<bar> \<le> d}) $ i)
  1.2411 +      = (\<Prod>i\<in>UNIV - {k}. b$i - a$i)" apply(rule setprod_cong,rule refl)
  1.2412 +      unfolding interval_doublesplit interval_eq_empty not_ex not_less unfolding interval_bounds by auto
  1.2413 +    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)
  1.2414 +      unfolding if_not_P apply(subst *) apply(subst setprod_insert) unfolding **
  1.2415 +      unfolding interval_doublesplit interval_eq_empty not_ex not_less unfolding interval_bounds unfolding Cart_lambda_beta if_P[OF refl]
  1.2416 +    proof- have "(min (b $ k) (c + d) - max (a $ k) (c - d)) \<le> 2 * d" by auto
  1.2417 +      also have "... < e / (\<Prod>i\<in>UNIV - {k}. b $ i - a $ i)" unfolding d_def using assms prod0 by(auto simp add:field_simps)
  1.2418 +      finally show "(min (b $ k) (c + d) - max (a $ k) (c - d)) * (\<Prod>i\<in>UNIV - {k}. b $ i - a $ i) < e"
  1.2419 +        unfolding pos_less_divide_eq[OF prod0] . qed auto qed qed
  1.2420 +
  1.2421 +lemma negligible_standard_hyperplane[intro]: "negligible {x. x$k = (c::real)}" 
  1.2422 +  unfolding negligible_def has_integral apply(rule,rule,rule,rule)
  1.2423 +proof- case goal1 from content_doublesplit[OF this,of a b k c] guess d . note d=this let ?i = "indicator {x. x$k = c}"
  1.2424 +  show ?case apply(rule_tac x="\<lambda>x. ball x d" in exI) apply(rule,rule gauge_ball,rule d)
  1.2425 +  proof(rule,rule) fix p assume p:"p tagged_division_of {a..b} \<and> (\<lambda>x. ball x d) fine p"
  1.2426 +    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)"
  1.2427 +      apply(rule setsum_cong2) unfolding split_paired_all real_scaleR_def mult_cancel_right split_conv
  1.2428 +      apply(cases,rule disjI1,assumption,rule disjI2)
  1.2429 +    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)
  1.2430 +      show "content l = content (l \<inter> {x. \<bar>x $ k - c\<bar> \<le> d})" apply(rule arg_cong[where f=content])
  1.2431 +        apply(rule set_ext,rule,rule) unfolding mem_Collect_eq
  1.2432 +      proof- fix y assume y:"y\<in>l" note p[THEN conjunct2,unfolded fine_def,rule_format,OF as(1),unfolded split_conv]
  1.2433 +        note this[unfolded subset_eq mem_ball vector_dist_norm,rule_format,OF y] note le_less_trans[OF component_le_norm[of _ k] this]
  1.2434 +        thus "\<bar>y $ k - c\<bar> \<le> d" unfolding Cart_nth.diff xk by auto
  1.2435 +      qed auto qed
  1.2436 +    note p'= tagged_division_ofD[OF p[THEN conjunct1]] and p''=division_of_tagged_division[OF p[THEN conjunct1]]
  1.2437 +    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
  1.2438 +      apply(subst abs_of_nonneg) apply(rule setsum_nonneg,rule) unfolding split_paired_all split_conv
  1.2439 +      apply(rule mult_nonneg_nonneg) apply(drule p'(4)) apply(erule exE)+ apply(rule_tac b=b in back_subst)
  1.2440 +      prefer 2 apply(subst(asm) eq_commute) apply assumption
  1.2441 +      apply(subst interval_doublesplit) apply(rule content_pos_le) apply(rule indicator_pos_le)
  1.2442 +    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}))"
  1.2443 +        apply(rule setsum_mono) unfolding split_paired_all split_conv 
  1.2444 +        apply(rule mult_right_le_one_le) apply(drule p'(4)) by(auto simp add:interval_doublesplit intro!:content_pos_le)
  1.2445 +      also have "... < e" apply(subst setsum_over_tagged_division_lemma[OF p[THEN conjunct1]])
  1.2446 +      proof- case goal1 have "content ({u..v} \<inter> {x. \<bar>x $ k - c\<bar> \<le> d}) \<le> content {u..v}"
  1.2447 +          unfolding interval_doublesplit apply(rule content_subset) unfolding interval_doublesplit[THEN sym] by auto
  1.2448 +        thus ?case unfolding goal1 unfolding interval_doublesplit using content_pos_le by smt
  1.2449 +      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"
  1.2450 +          apply(rule setsum_nonneg,rule) unfolding mem_Collect_eq image_iff apply(erule exE bexE conjE)+ unfolding split_paired_all 
  1.2451 +        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)"
  1.2452 +          guess u v using p'(4)[OF as(2)] apply-by(erule exE)+ note * = this
  1.2453 +          show "content x \<ge> 0" unfolding as snd_conv * interval_doublesplit by(rule content_pos_le)
  1.2454 +        qed have **:"norm (1::real) \<le> 1" by auto note division_doublesplit[OF p'',unfolded interval_doublesplit]
  1.2455 +        note dsum_bound[OF this **,unfolded interval_doublesplit[THEN sym]]
  1.2456 +        note this[unfolded real_scaleR_def real_norm_def class_semiring.semiring_rules, of k c d] note le_less_trans[OF this d(2)]
  1.2457 +        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"
  1.2458 +          apply(subst vsum_nonzero_image_lemma[of "snd ` p" content "{}", unfolded o_def,THEN sym])
  1.2459 +          apply(rule finite_imageI p' content_empty)+ unfolding forall_in_division[OF p'']
  1.2460 +        proof(rule,rule,rule,rule,rule,rule,rule,erule conjE) fix m n u v
  1.2461 +          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}"
  1.2462 +          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
  1.2463 +          note subset_interior[OF this, unfolded division_ofD(5)[OF p'' as(1-3)] interior_inter[of "{m..n}"]]
  1.2464 +          hence "interior ({m..n} \<inter> {x. \<bar>x $ k - c\<bar> \<le> d}) = {}" unfolding as Int_absorb by auto
  1.2465 +          thus "content ({m..n} \<inter> {x. \<bar>x $ k - c\<bar> \<le> d}) = 0" unfolding interval_doublesplit content_eq_0_interior[THEN sym] .
  1.2466 +        qed qed
  1.2467 +      finally show "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x $ k - c\<bar> \<le> d}) * ?i x) < e" .
  1.2468 +    qed qed qed
  1.2469 +
  1.2470 +subsection {* A technical lemma about "refinement" of division. *}
  1.2471 +
  1.2472 +lemma tagged_division_finer: fixes p::"((real^'n) \<times> ((real^'n) set)) set"
  1.2473 +  assumes "p tagged_division_of {a..b}" "gauge d"
  1.2474 +  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"
  1.2475 +proof-
  1.2476 +  let ?P = "\<lambda>p. p tagged_partial_division_of {a..b} \<longrightarrow> gauge d \<longrightarrow>
  1.2477 +    (\<exists>q. q tagged_division_of (\<Union>{k. \<exists>x. (x,k) \<in> p}) \<and> d fine q \<and>
  1.2478 +                   (\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q))"
  1.2479 +  { have *:"finite p" "p tagged_partial_division_of {a..b}" using assms(1) unfolding tagged_division_of_def by auto
  1.2480 +    presume "\<And>p. finite p \<Longrightarrow> ?P p" from this[rule_format,OF * assms(2)] guess q .. note q=this
  1.2481 +    thus ?thesis apply-apply(rule that[of q]) unfolding tagged_division_ofD[OF assms(1)] by auto
  1.2482 +  } fix p::"((real^'n) \<times> ((real^'n) set)) set" assume as:"finite p"
  1.2483 +  show "?P p" apply(rule,rule) using as proof(induct p) 
  1.2484 +    case empty show ?case apply(rule_tac x="{}" in exI) unfolding fine_def by auto
  1.2485 +  next case (insert xk p) guess x k using surj_pair[of xk] apply- by(erule exE)+ note xk=this
  1.2486 +    note tagged_partial_division_subset[OF insert(4) subset_insertI]
  1.2487 +    from insert(3)[OF this insert(5)] guess q1 .. note q1 = conjunctD3[OF this]
  1.2488 +    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
  1.2489 +    note p = tagged_partial_division_ofD[OF insert(4)]
  1.2490 +    from p(4)[unfolded xk, OF insertI1] guess u v apply-by(erule exE)+ note uv=this
  1.2491 +
  1.2492 +    have "finite {k. \<exists>x. (x, k) \<in> p}" 
  1.2493 +      apply(rule finite_subset[of _ "snd ` p"],rule) unfolding subset_eq image_iff mem_Collect_eq
  1.2494 +      apply(erule exE,rule_tac x="(xa,x)" in bexI) using p by auto
  1.2495 +    hence int:"interior {u..v} \<inter> interior (\<Union>{k. \<exists>x. (x, k) \<in> p}) = {}"
  1.2496 +      apply(rule inter_interior_unions_intervals) apply(rule open_interior) apply(rule_tac[!] ballI)
  1.2497 +      unfolding mem_Collect_eq apply(erule_tac[!] exE) apply(drule p(4)[OF insertI2],assumption)      
  1.2498 +      apply(rule p(5))  unfolding uv xk apply(rule insertI1,rule insertI2) apply assumption
  1.2499 +      using insert(2) unfolding uv xk by auto
  1.2500 +
  1.2501 +    show ?case proof(cases "{u..v} \<subseteq> d x")
  1.2502 +      case True thus ?thesis apply(rule_tac x="{(x,{u..v})} \<union> q1" in exI) apply rule
  1.2503 +        unfolding * uv apply(rule tagged_division_union,rule tagged_division_of_self)
  1.2504 +        apply(rule p[unfolded xk uv] insertI1)+  apply(rule q1,rule int) 
  1.2505 +        apply(rule,rule fine_union,subst fine_def) defer apply(rule q1)
  1.2506 +        unfolding Ball_def split_paired_All split_conv apply(rule,rule,rule,rule)
  1.2507 +        apply(erule insertE) defer apply(rule UnI2) apply(drule q1(3)[rule_format]) unfolding xk uv by auto
  1.2508 +    next case False from fine_division_exists[OF assms(2), of u v] guess q2 . note q2=this
  1.2509 +      show ?thesis apply(rule_tac x="q2 \<union> q1" in exI)
  1.2510 +        apply rule unfolding * uv apply(rule tagged_division_union q2 q1 int fine_union)+
  1.2511 +        unfolding Ball_def split_paired_All split_conv apply rule apply(rule fine_union)
  1.2512 +        apply(rule q1 q2)+ apply(rule,rule,rule,rule) apply(erule insertE)
  1.2513 +        apply(rule UnI2) defer apply(drule q1(3)[rule_format])using False unfolding xk uv by auto
  1.2514 +    qed qed qed
  1.2515 +
  1.2516 +subsection {* Hence the main theorem about negligible sets. *}
  1.2517 +
  1.2518 +lemma finite_product_dependent: assumes "finite s" "\<And>x. x\<in>s\<Longrightarrow> finite (t x)"
  1.2519 +  shows "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}" using assms
  1.2520 +proof(induct) case (insert x s) 
  1.2521 +  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
  1.2522 +  show ?case unfolding * apply(rule finite_UnI) using insert by auto qed auto
  1.2523 +
  1.2524 +lemma sum_sum_product: assumes "finite s" "\<forall>i\<in>s. finite (t i)"
  1.2525 +  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
  1.2526 +proof(induct) case (insert a s)
  1.2527 +  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
  1.2528 +  show ?case unfolding * apply(subst setsum_Un_disjoint) unfolding setsum_insert[OF insert(1-2)]
  1.2529 +    prefer 4 apply(subst insert(3)) unfolding add_right_cancel
  1.2530 +  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
  1.2531 +    show "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}" apply(rule finite_product_dependent) using insert by auto
  1.2532 +  qed(insert insert, auto) qed auto
  1.2533 +
  1.2534 +lemma has_integral_negligible: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
  1.2535 +  assumes "negligible s" "\<forall>x\<in>(t - s). f x = 0"
  1.2536 +  shows "(f has_integral 0) t"
  1.2537 +proof- presume P:"\<And>f::real^'n \<Rightarrow> 'a. \<And>a b. (\<forall>x. ~(x \<in> s) \<longrightarrow> f x = 0) \<Longrightarrow> (f has_integral 0) ({a..b})"
  1.2538 +  let ?f = "(\<lambda>x. if x \<in> t then f x else 0)"
  1.2539 +  show ?thesis apply(rule_tac f="?f" in has_integral_eq) apply(rule) unfolding if_P apply(rule refl)
  1.2540 +    apply(subst has_integral_alt) apply(cases,subst if_P,assumption) unfolding if_not_P
  1.2541 +  proof- assume "\<exists>a b. t = {a..b}" then guess a b apply-by(erule exE)+ note t = this
  1.2542 +    show "(?f has_integral 0) t" unfolding t apply(rule P) using assms(2) unfolding t by auto
  1.2543 +  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)"
  1.2544 +      apply(safe,rule_tac x=1 in exI,rule) apply(rule zero_less_one,safe) apply(rule_tac x=0 in exI)
  1.2545 +      apply(rule,rule P) using assms(2) by auto
  1.2546 +  qed
  1.2547 +next fix f::"real^'n \<Rightarrow> 'a" and a b::"real^'n" assume assm:"\<forall>x. x \<notin> s \<longrightarrow> f x = 0" 
  1.2548 +  show "(f has_integral 0) {a..b}" unfolding has_integral
  1.2549 +  proof(safe) case goal1
  1.2550 +    hence "\<And>n. e / 2 / ((real n+1) * (2 ^ n)) > 0" 
  1.2551 +      apply-apply(rule divide_pos_pos) defer apply(rule mult_pos_pos) by(auto simp add:field_simps)
  1.2552 +    note assms(1)[unfolded negligible_def has_integral,rule_format,OF this,of a b] note allI[OF this,of "\<lambda>x. x"] 
  1.2553 +    from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format]]
  1.2554 +    show ?case apply(rule_tac x="\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x" in exI) 
  1.2555 +    proof safe show "gauge (\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x)" using d(1) unfolding gauge_def by auto
  1.2556 +      fix p assume as:"p tagged_division_of {a..b}" "(\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x) fine p" 
  1.2557 +      let ?goal = "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  1.2558 +      { presume "p\<noteq>{} \<Longrightarrow> ?goal" thus ?goal apply(cases "p={}") using goal1 by auto  }
  1.2559 +      assume as':"p \<noteq> {}" from real_arch_simple[of "Sup((\<lambda>(x,k). norm(f x)) ` p)"] guess N ..
  1.2560 +      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
  1.2561 +      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)"
  1.2562 +        apply(rule,rule tagged_division_finer[OF as(1) d(1)]) by auto
  1.2563 +      from choice[OF this] guess q .. note q=conjunctD3[OF this[rule_format]]
  1.2564 +      have *:"\<And>i. (\<Sum>(x, k)\<in>q i. content k *\<^sub>R indicator s x) \<ge> 0" apply(rule setsum_nonneg,safe) 
  1.2565 +        unfolding real_scaleR_def apply(rule mult_nonneg_nonneg) apply(drule tagged_division_ofD(4)[OF q(1)]) by auto
  1.2566 +      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"
  1.2567 +      proof- case goal1 thus ?case apply-apply(rule setsum_le_included[of s t g snd f]) prefer 4
  1.2568 +          apply safe apply(erule_tac x=x in ballE) apply(erule exE) apply(rule_tac x="(xa,x)" in bexI) by auto qed
  1.2569 +      have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) \<le> setsum (\<lambda>i. (real i + 1) *
  1.2570 +                     norm(setsum (\<lambda>(x,k). content k *\<^sub>R indicator s x) (q i))) {0..N+1}"
  1.2571 +        unfolding real_norm_def setsum_right_distrib abs_of_nonneg[OF *] diff_0_right
  1.2572 +        apply(rule order_trans,rule setsum_norm) defer apply(subst sum_sum_product) prefer 3 
  1.2573 +      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
  1.2574 +        fix i a b assume as'':"(a,b) \<in> q i" show "0 \<le> (real i + 1) * (content b *\<^sub>R indicator s a)"
  1.2575 +          unfolding real_scaleR_def apply(rule mult_nonneg_nonneg) defer apply(rule mult_nonneg_nonneg)
  1.2576 +          using tagged_division_ofD(4)[OF q(1) as''] by auto
  1.2577 +      next fix i::nat show "finite (q i)" using q by auto
  1.2578 +      next fix x k assume xk:"(x,k) \<in> p" def n \<equiv> "nat \<lfloor>norm (f x)\<rfloor>"
  1.2579 +        have *:"norm (f x) \<in> (\<lambda>(x, k). norm (f x)) ` p" using xk by auto
  1.2580 +        have nfx:"real n \<le> norm(f x)" "norm(f x) \<le> real n + 1" unfolding n_def by auto
  1.2581 +        hence "n \<in> {0..N + 1}" using N[rule_format,OF *] by auto
  1.2582 +        moreover  note as(2)[unfolded fine_def,rule_format,OF xk,unfolded split_conv]
  1.2583 +        note q(3)[rule_format,OF xk,unfolded split_conv,rule_format,OF this] note this[unfolded n_def[symmetric]]
  1.2584 +        moreover have "norm (content k *\<^sub>R f x) \<le> (real n + 1) * (content k * indicator s x)"
  1.2585 +        proof(cases "x\<in>s") case False thus ?thesis using assm by auto
  1.2586 +        next case True have *:"content k \<ge> 0" using tagged_division_ofD(4)[OF as(1) xk] by auto
  1.2587 +          moreover have "content k * norm (f x) \<le> content k * (real n + 1)" apply(rule mult_mono) using nfx * by auto
  1.2588 +          ultimately show ?thesis unfolding abs_mult using nfx True by(auto simp add:field_simps)
  1.2589 +        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)"
  1.2590 +          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
  1.2591 +      qed(insert as, auto)
  1.2592 +      also have "... \<le> setsum (\<lambda>i. e / 2 / 2 ^ i) {0..N+1}" apply(rule setsum_mono) 
  1.2593 +      proof- case goal1 thus ?case apply(subst mult_commute, subst pos_le_divide_eq[THEN sym])
  1.2594 +          using d(2)[rule_format,of "q i" i] using q[rule_format] by(auto simp add:field_simps)
  1.2595 +      qed also have "... < e * inverse 2 * 2" unfolding real_divide_def setsum_right_distrib[THEN sym]
  1.2596 +        apply(rule mult_strict_left_mono) unfolding power_inverse atLeastLessThanSuc_atLeastAtMost[THEN sym]
  1.2597 +        apply(subst sumr_geometric) using goal1 by auto
  1.2598 +      finally show "?goal" by auto qed qed qed
  1.2599 +
  1.2600 +lemma has_integral_spike: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
  1.2601 +  assumes "negligible s" "(\<forall>x\<in>(t - s). g x = f x)" "(f has_integral y) t"
  1.2602 +  shows "(g has_integral y) t"
  1.2603 +proof- { fix a b::"real^'n" and f g ::"real^'n \<Rightarrow> 'a" and y::'a
  1.2604 +    assume as:"\<forall>x \<in> {a..b} - s. g x = f x" "(f has_integral y) {a..b}"
  1.2605 +    have "((\<lambda>x. f x + (g x - f x)) has_integral (y + 0)) {a..b}" apply(rule has_integral_add[OF as(2)])
  1.2606 +      apply(rule has_integral_negligible[OF assms(1)]) using as by auto
  1.2607 +    hence "(g has_integral y) {a..b}" by auto } note * = this
  1.2608 +  show ?thesis apply(subst has_integral_alt) using assms(2-) apply-apply(rule cond_cases,safe)
  1.2609 +    apply(rule *, assumption+) apply(subst(asm) has_integral_alt) unfolding if_not_P
  1.2610 +    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)
  1.2611 +    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
  1.2612 +
  1.2613 +lemma has_integral_spike_eq:
  1.2614 +  assumes "negligible s" "\<forall>x\<in>(t - s). g x = f x"
  1.2615 +  shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  1.2616 +  apply rule apply(rule_tac[!] has_integral_spike[OF assms(1)]) using assms(2) by auto
  1.2617 +
  1.2618 +lemma integrable_spike: assumes "negligible s" "\<forall>x\<in>(t - s). g x = f x" "f integrable_on t"
  1.2619 +  shows "g integrable_on  t"
  1.2620 +  using assms unfolding integrable_on_def apply-apply(erule exE)
  1.2621 +  apply(rule,rule has_integral_spike) by fastsimp+
  1.2622 +
  1.2623 +lemma integral_spike: assumes "negligible s" "\<forall>x\<in>(t - s). g x = f x"
  1.2624 +  shows "integral t f = integral t g"
  1.2625 +  unfolding integral_def using has_integral_spike_eq[OF assms] by auto
  1.2626 +
  1.2627 +subsection {* Some other trivialities about negligible sets. *}
  1.2628 +
  1.2629 +lemma negligible_subset[intro]: assumes "negligible s" "t \<subseteq> s" shows "negligible t" unfolding negligible_def 
  1.2630 +proof(safe) case goal1 show ?case using assms(1)[unfolded negligible_def,rule_format,of a b]
  1.2631 +    apply-apply(rule has_integral_spike[OF assms(1)]) defer apply assumption
  1.2632 +    using assms(2) unfolding indicator_def by auto qed
  1.2633 +
  1.2634 +lemma negligible_diff[intro?]: assumes "negligible s" shows "negligible(s - t)" using assms by auto
  1.2635 +
  1.2636 +lemma negligible_inter: assumes "negligible s \<or> negligible t" shows "negligible(s \<inter> t)" using assms by auto
  1.2637 +
  1.2638 +lemma negligible_union: assumes "negligible s" "negligible t" shows "negligible (s \<union> t)" unfolding negligible_def 
  1.2639 +proof safe case goal1 note assm = assms[unfolded negligible_def,rule_format,of a b]
  1.2640 +  thus ?case apply(subst has_integral_spike_eq[OF assms(2)])
  1.2641 +    defer apply assumption unfolding indicator_def by auto qed
  1.2642 +
  1.2643 +lemma negligible_union_eq[simp]: "negligible (s \<union> t) \<longleftrightarrow> (negligible s \<and> negligible t)"
  1.2644 +  using negligible_union by auto
  1.2645 +
  1.2646 +lemma negligible_sing[intro]: "negligible {a::real^'n}" 
  1.2647 +proof- guess x using UNIV_witness[where 'a='n] ..
  1.2648 +  show ?thesis using negligible_standard_hyperplane[of x "a$x"] by auto qed
  1.2649 +
  1.2650 +lemma negligible_insert[simp]: "negligible(insert a s) \<longleftrightarrow> negligible s"
  1.2651 +  apply(subst insert_is_Un) unfolding negligible_union_eq by auto
  1.2652 +
  1.2653 +lemma negligible_empty[intro]: "negligible {}" by auto
  1.2654 +
  1.2655 +lemma negligible_finite[intro]: assumes "finite s" shows "negligible s"
  1.2656 +  using assms apply(induct s) by auto
  1.2657 +
  1.2658 +lemma negligible_unions[intro]: assumes "finite s" "\<forall>t\<in>s. negligible t" shows "negligible(\<Union>s)"
  1.2659 +  using assms by(induct,auto) 
  1.2660 +
  1.2661 +lemma negligible:  "negligible s \<longleftrightarrow> (\<forall>t::(real^'n) set. (indicator s has_integral 0) t)"
  1.2662 +  apply safe defer apply(subst negligible_def)
  1.2663 +proof- fix t::"(real^'n) set" assume as:"negligible s"
  1.2664 +  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)  
  1.2665 +  show "(indicator s has_integral 0) t" apply(subst has_integral_alt)
  1.2666 +    apply(cases,subst if_P,assumption) unfolding if_not_P apply(safe,rule as[unfolded negligible_def,rule_format])
  1.2667 +    apply(rule_tac x=1 in exI) apply(safe,rule zero_less_one) apply(rule_tac x=0 in exI)
  1.2668 +    using negligible_subset[OF as,of "s \<inter> t"] unfolding negligible_def indicator_def unfolding * by auto qed auto
  1.2669 +
  1.2670 +subsection {* Finite case of the spike theorem is quite commonly needed. *}
  1.2671 +
  1.2672 +lemma has_integral_spike_finite: assumes "finite s" "\<forall>x\<in>t-s. g x = f x" 
  1.2673 +  "(f has_integral y) t" shows "(g has_integral y) t"
  1.2674 +  apply(rule has_integral_spike) using assms by auto
  1.2675 +
  1.2676 +lemma has_integral_spike_finite_eq: assumes "finite s" "\<forall>x\<in>t-s. g x = f x"
  1.2677 +  shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  1.2678 +  apply rule apply(rule_tac[!] has_integral_spike_finite) using assms by auto
  1.2679 +
  1.2680 +lemma integrable_spike_finite:
  1.2681 +  assumes "finite s" "\<forall>x\<in>t-s. g x = f x" "f integrable_on t" shows "g integrable_on  t"
  1.2682 +  using assms unfolding integrable_on_def apply safe apply(rule_tac x=y in exI)
  1.2683 +  apply(rule has_integral_spike_finite) by auto
  1.2684 +
  1.2685 +subsection {* In particular, the boundary of an interval is negligible. *}
  1.2686 +
  1.2687 +lemma negligible_frontier_interval: "negligible({a..b} - {a<..<b})"
  1.2688 +proof- let ?A = "\<Union>((\<lambda>k. {x. x$k = a$k} \<union> {x. x$k = b$k}) ` UNIV)"
  1.2689 +  have "{a..b} - {a<..<b} \<subseteq> ?A" apply rule unfolding Diff_iff mem_interval not_all
  1.2690 +    apply(erule conjE exE)+ apply(rule_tac X="{x. x $ xa = a $ xa} \<union> {x. x $ xa = b $ xa}" in UnionI)
  1.2691 +    apply(erule_tac[!] x=xa in allE) by auto
  1.2692 +  thus ?thesis apply-apply(rule negligible_subset[of ?A]) apply(rule negligible_unions[OF finite_imageI]) by auto qed
  1.2693 +
  1.2694 +lemma has_integral_spike_interior:
  1.2695 +  assumes "\<forall>x\<in>{a<..<b}. g x = f x" "(f has_integral y) ({a..b})" shows "(g has_integral y) ({a..b})"
  1.2696 +  apply(rule has_integral_spike[OF negligible_frontier_interval _ assms(2)]) using assms(1) by auto
  1.2697 +
  1.2698 +lemma has_integral_spike_interior_eq:
  1.2699 +  assumes "\<forall>x\<in>{a<..<b}. g x = f x" shows "((f has_integral y) ({a..b}) \<longleftrightarrow> (g has_integral y) ({a..b}))"
  1.2700 +  apply rule apply(rule_tac[!] has_integral_spike_interior) using assms by auto
  1.2701 +
  1.2702 +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}"
  1.2703 +  using  assms unfolding integrable_on_def using has_integral_spike_interior[OF assms(1)] by auto
  1.2704 +
  1.2705 +subsection {* Integrability of continuous functions. *}
  1.2706 +
  1.2707 +lemma neutral_and[simp]: "neutral op \<and> = True"
  1.2708 +  unfolding neutral_def apply(rule some_equality) by auto
  1.2709 +
  1.2710 +lemma monoidal_and[intro]: "monoidal op \<and>" unfolding monoidal_def by auto
  1.2711 +
  1.2712 +lemma iterate_and[simp]: assumes "finite s" shows "(iterate op \<and>) s p \<longleftrightarrow> (\<forall>x\<in>s. p x)" using assms
  1.2713 +apply induct unfolding iterate_insert[OF monoidal_and] by auto
  1.2714 +
  1.2715 +lemma operative_division_and: assumes "operative op \<and> P" "d division_of {a..b}"
  1.2716 +  shows "(\<forall>i\<in>d. P i) \<longleftrightarrow> P {a..b}"
  1.2717 +  using operative_division[OF monoidal_and assms] division_of_finite[OF assms(2)] by auto
  1.2718 +
  1.2719 +lemma operative_approximable: assumes "0 \<le> e" fixes f::"real^'n \<Rightarrow> 'a::banach"
  1.2720 +  shows "operative op \<and> (\<lambda>i. \<exists>g. (\<forall>x\<in>i. norm (f x - g (x::real^'n)) \<le> e) \<and> g integrable_on i)" unfolding operative_def neutral_and
  1.2721 +proof safe fix a b::"real^'n" { assume "content {a..b} = 0"
  1.2722 +    thus "\<exists>g. (\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b}" 
  1.2723 +      apply(rule_tac x=f in exI) using assms by(auto intro!:integrable_on_null) }
  1.2724 +  { fix c k g assume as:"\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e" "g integrable_on {a..b}"
  1.2725 +    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}"
  1.2726 +      "\<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}"
  1.2727 +      apply(rule_tac[!] x=g in exI) using as(1) integrable_split[OF as(2)] by auto }
  1.2728 +  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}"
  1.2729 +                          "\<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}"
  1.2730 +  let ?g = "\<lambda>x. if x$k = c then f x else if x$k \<le> c then g1 x else g2 x"
  1.2731 +  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)
  1.2732 +  proof safe case goal1 thus ?case apply- apply(cases "x$k=c", case_tac "x$k < c") using as assms by auto
  1.2733 +  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}"
  1.2734 +    then guess h1 h2 unfolding integrable_on_def by auto from has_integral_split[OF this]
  1.2735 +    show ?case unfolding integrable_on_def by auto
  1.2736 +  next show "?g integrable_on {a..b} \<inter> {x. x $ k \<le> c}" "?g integrable_on {a..b} \<inter> {x. x $ k \<ge> c}"
  1.2737 +      apply(rule_tac[!] integrable_spike[OF negligible_standard_hyperplane[of k c]]) using as(2,4) by auto qed qed
  1.2738 +
  1.2739 +lemma approximable_on_division: fixes f::"real^'n \<Rightarrow> 'a::banach"
  1.2740 +  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"
  1.2741 +  obtains g where "\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e" "g integrable_on {a..b}"
  1.2742 +proof- note * = operative_division[OF monoidal_and operative_approximable[OF assms(1)] assms(2)]
  1.2743 +  note this[unfolded iterate_and[OF division_of_finite[OF assms(2)]]] from assms(3)[unfolded this[of f]]
  1.2744 +  guess g .. thus thesis apply-apply(rule that[of g]) by auto qed
  1.2745 +
  1.2746 +lemma integrable_continuous: fixes f::"real^'n \<Rightarrow> 'a::banach"
  1.2747 +  assumes "continuous_on {a..b} f" shows "f integrable_on {a..b}"
  1.2748 +proof(rule integrable_uniform_limit,safe) fix e::real assume e:"0 < e"
  1.2749 +  from compact_uniformly_continuous[OF assms compact_interval,unfolded uniformly_continuous_on_def,rule_format,OF e] guess d ..
  1.2750 +  note d=conjunctD2[OF this,rule_format]
  1.2751 +  from fine_division_exists[OF gauge_ball[OF d(1)], of a b] guess p . note p=this
  1.2752 +  note p' = tagged_division_ofD[OF p(1)]
  1.2753 +  have *:"\<forall>i\<in>snd ` p. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  1.2754 +  proof(safe,unfold snd_conv) fix x l assume as:"(x,l) \<in> p" 
  1.2755 +    from p'(4)[OF this] guess a b apply-by(erule exE)+ note l=this
  1.2756 +    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)
  1.2757 +    proof safe show "(\<lambda>y. f x) integrable_on l" unfolding integrable_on_def l by(rule,rule has_integral_const)
  1.2758 +      fix y assume y:"y\<in>l" note fineD[OF p(2) as,unfolded subset_eq,rule_format,OF this]
  1.2759 +      note d(2)[OF _ _ this[unfolded mem_ball]]
  1.2760 +      thus "norm (f y - f x) \<le> e" using y p'(2-3)[OF as] unfolding vector_dist_norm l norm_minus_commute by fastsimp qed qed
  1.2761 +  from e have "0 \<le> e" by auto from approximable_on_division[OF this division_of_tagged_division[OF p(1)] *] guess g .
  1.2762 +  thus "\<exists>g. (\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b}" by auto qed 
  1.2763 +
  1.2764 +subsection {* Specialization of additivity to one dimension. *}
  1.2765 +
  1.2766 +lemma operative_1_lt: assumes "monoidal opp"
  1.2767 +  shows "operative opp f \<longleftrightarrow> ((\<forall>a b. b \<le> a \<longrightarrow> f {a..b::real^1} = neutral opp) \<and>
  1.2768 +                (\<forall>a b c. a < c \<and> c < b \<longrightarrow> opp (f{a..c})(f{c..b}) = f {a..b}))"
  1.2769 +  unfolding operative_def content_eq_0_1 forall_1 vector_le_def vector_less_def
  1.2770 +proof safe fix a b c::"real^1" assume as:"\<forall>a b c. f {a..b} = opp (f ({a..b} \<inter> {x. x $ 1 \<le> c})) (f ({a..b} \<inter> {x. c \<le> x $ 1}))" "a $ 1 < c $ 1" "c $ 1 < b $ 1"
  1.2771 +    from this(2-) have "{a..b} \<inter> {x. x $ 1 \<le> c $ 1} = {a..c}" "{a..b} \<inter> {x. x $ 1 \<ge> c $ 1} = {c..b}" by auto
  1.2772 +    thus "opp (f {a..c}) (f {c..b}) = f {a..b}" unfolding as(1)[rule_format,of a b "c$1"] by auto
  1.2773 +next fix a b::"real^1" and c::real
  1.2774 +  assume as:"\<forall>a b. b $ 1 \<le> a $ 1 \<longrightarrow> f {a..b} = neutral opp" "\<forall>a b c. a $ 1 < c $ 1 \<and> c $ 1 < b $ 1 \<longrightarrow> opp (f {a..c}) (f {c..b}) = f {a..b}"
  1.2775 +  show "f {a..b} = opp (f ({a..b} \<inter> {x. x $ 1 \<le> c})) (f ({a..b} \<inter> {x. c \<le> x $ 1}))"
  1.2776 +  proof(cases "c \<in> {a$1 .. b$1}")
  1.2777 +    case False hence "c<a$1 \<or> c>b$1" by auto
  1.2778 +    thus ?thesis apply-apply(erule disjE)
  1.2779 +    proof- assume "c<a$1" hence *:"{a..b} \<inter> {x. x $ 1 \<le> c} = {1..0}"  "{a..b} \<inter> {x. c \<le> x $ 1} = {a..b}" by auto
  1.2780 +      show ?thesis unfolding * apply(subst as(1)[rule_format,of 0 1]) using assms by auto
  1.2781 +    next   assume "b$1<c" hence *:"{a..b} \<inter> {x. x $ 1 \<le> c} = {a..b}"  "{a..b} \<inter> {x. c \<le> x $ 1} = {1..0}" by auto
  1.2782 +      show ?thesis unfolding * apply(subst as(1)[rule_format,of 0 1]) using assms by auto
  1.2783 +    qed
  1.2784 +  next case True hence *:"min (b $ 1) c = c" "max (a $ 1) c = c" by auto
  1.2785 +    show ?thesis unfolding interval_split num1_eq_iff if_True * vec_def[THEN sym]
  1.2786 +    proof(cases "c = a$1 \<or> c = b$1")
  1.2787 +      case False thus "f {a..b} = opp (f {a..vec1 c}) (f {vec1 c..b})"
  1.2788 +        apply-apply(subst as(2)[rule_format]) using True by auto
  1.2789 +    next case True thus "f {a..b} = opp (f {a..vec1 c}) (f {vec1 c..b})" apply-
  1.2790 +      proof(erule disjE) assume "c=a$1" hence *:"a = vec1 c" unfolding Cart_eq by auto 
  1.2791 +        hence "f {a..vec1 c} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  1.2792 +        thus ?thesis using assms unfolding * by auto
  1.2793 +      next assume "c=b$1" hence *:"b = vec1 c" unfolding Cart_eq by auto 
  1.2794 +        hence "f {vec1 c..b} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  1.2795 +        thus ?thesis using assms unfolding * by auto qed qed qed qed
  1.2796 +
  1.2797 +lemma operative_1_le: assumes "monoidal opp"
  1.2798 +  shows "operative opp f \<longleftrightarrow> ((\<forall>a b. b \<le> a \<longrightarrow> f {a..b::real^1} = neutral opp) \<and>
  1.2799 +                (\<forall>a b c. a \<le> c \<and> c \<le> b \<longrightarrow> opp (f{a..c})(f{c..b}) = f {a..b}))"
  1.2800 +unfolding operative_1_lt[OF assms]
  1.2801 +proof safe fix a b c::"real^1" 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"
  1.2802 +  show "opp (f {a..c}) (f {c..b}) = f {a..b}" apply(rule as(1)[rule_format]) using as(2-) unfolding vector_le_def vector_less_def by auto
  1.2803 +next fix a b c ::"real^1"
  1.2804 +  assume "\<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}" "a \<le> c" "c \<le> b"
  1.2805 +  note as = this[rule_format]
  1.2806 +  show "opp (f {a..c}) (f {c..b}) = f {a..b}"
  1.2807 +  proof(cases "c = a \<or> c = b")
  1.2808 +    case False thus ?thesis apply-apply(subst as(2)) using as(3-) unfolding vector_le_def vector_less_def Cart_eq by(auto simp del:dest_vec1_eq)
  1.2809 +    next case True thus ?thesis apply-
  1.2810 +      proof(erule disjE) assume *:"c=a" hence "f {a..c} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  1.2811 +        thus ?thesis using assms unfolding * by auto
  1.2812 +      next               assume *:"c=b" hence "f {c..b} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  1.2813 +        thus ?thesis using assms unfolding * by auto qed qed qed 
  1.2814 +
  1.2815 +subsection {* Special case of additivity we need for the FCT. *}
  1.2816 +
  1.2817 +lemma additive_tagged_division_1: fixes f::"real^1 \<Rightarrow> 'a::real_normed_vector"
  1.2818 +  assumes "dest_vec1 a \<le> dest_vec1 b" "p tagged_division_of {a..b}"
  1.2819 +  shows "setsum (\<lambda>(x,k). f(interval_upperbound k) - f(interval_lowerbound k)) p = f b - f a"
  1.2820 +proof- let ?f = "(\<lambda>k::(real^1) set. if k = {} then 0 else f(interval_upperbound k) - f(interval_lowerbound k))"
  1.2821 +  have *:"operative op + ?f" unfolding operative_1_lt[OF monoidal_monoid] interval_eq_empty_1
  1.2822 +    by(auto simp add:not_less interval_bound_1 vector_less_def)
  1.2823 +  have **:"{a..b} \<noteq> {}" using assms(1) by auto note operative_tagged_division[OF monoidal_monoid * assms(2)]
  1.2824 +  note * = this[unfolded if_not_P[OF **] interval_bound_1[OF assms(1)],THEN sym ]
  1.2825 +  show ?thesis unfolding * apply(subst setsum_iterate[THEN sym]) defer
  1.2826 +    apply(rule setsum_cong2) unfolding split_paired_all split_conv using assms(2) by auto qed
  1.2827 +
  1.2828 +subsection {* A useful lemma allowing us to factor out the content size. *}
  1.2829 +
  1.2830 +lemma has_integral_factor_content:
  1.2831 +  "(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
  1.2832 +    \<longrightarrow> norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content {a..b}))"
  1.2833 +proof(cases "content {a..b} = 0")
  1.2834 +  case True show ?thesis unfolding has_integral_null_eq[OF True] apply safe
  1.2835 +    apply(rule,rule,rule gauge_trivial,safe) unfolding setsum_content_null[OF True] True defer 
  1.2836 +    apply(erule_tac x=1 in allE,safe) defer apply(rule fine_division_exists[of _ a b],assumption)
  1.2837 +    apply(erule_tac x=p in allE) unfolding setsum_content_null[OF True] by auto
  1.2838 +next case False note F = this[unfolded content_lt_nz[THEN sym]]
  1.2839 +  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)"
  1.2840 +  show ?thesis apply(subst has_integral)
  1.2841 +  proof safe fix e::real assume e:"e>0"
  1.2842 +    { 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)
  1.2843 +        apply(erule impE) defer apply(erule exE,rule_tac x=d in exI)
  1.2844 +        using F e by(auto simp add:field_simps intro:mult_pos_pos) }
  1.2845 +    {  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)
  1.2846 +        apply(erule impE) defer apply(erule exE,rule_tac x=d in exI)
  1.2847 +        using F e by(auto simp add:field_simps intro:mult_pos_pos) } qed qed
  1.2848 +
  1.2849 +subsection {* Fundamental theorem of calculus. *}
  1.2850 +
  1.2851 +lemma fundamental_theorem_of_calculus: fixes f::"real^1 \<Rightarrow> 'a::banach"
  1.2852 +  assumes "a \<le> b"  "\<forall>x\<in>{a..b}. ((f o vec1) has_vector_derivative f'(vec1 x)) (at x within {a..b})"
  1.2853 +  shows "(f' has_integral (f(vec1 b) - f(vec1 a))) ({vec1 a..vec1 b})"
  1.2854 +unfolding has_integral_factor_content
  1.2855 +proof safe fix e::real assume e:"e>0" have ab:"dest_vec1 (vec1 a) \<le> dest_vec1 (vec1 b)" using assms(1) by auto
  1.2856 +  note assm = assms(2)[unfolded has_vector_derivative_def has_derivative_within_alt]
  1.2857 +  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
  1.2858 +  note this[OF assm,unfolded gauge_existence_lemma] from choice[OF this,unfolded Ball_def[symmetric]]
  1.2859 +  guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
  1.2860 +  show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {vec1 a..vec1 b} \<and> d fine p \<longrightarrow>
  1.2861 +                 norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f (vec1 b) - f (vec1 a))) \<le> e * content {vec1 a..vec1 b})"
  1.2862 +    apply(rule_tac x="\<lambda>x. ball x (d (dest_vec1 x))" in exI,safe)
  1.2863 +    apply(rule gauge_ball_dependent,rule,rule d(1))
  1.2864 +  proof- fix p assume as:"p tagged_division_of {vec1 a..vec1 b}" "(\<lambda>x. ball x (d (dest_vec1 x))) fine p"
  1.2865 +    show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f (vec1 b) - f (vec1 a))) \<le> e * content {vec1 a..vec1 b}" 
  1.2866 +      unfolding content_1[OF ab] additive_tagged_division_1[OF ab as(1),of f,THEN sym]
  1.2867 +      unfolding vector_minus_component[THEN sym] additive_tagged_division_1[OF ab as(1),of "\<lambda>x. x",THEN sym]
  1.2868 +      apply(subst dest_vec1_setsum) unfolding setsum_right_distrib defer unfolding setsum_subtractf[THEN sym] 
  1.2869 +    proof(rule setsum_norm_le,safe) fix x k assume "(x,k)\<in>p"
  1.2870 +      note xk = tagged_division_ofD(2-4)[OF as(1) this] from this(3) guess u v apply-by(erule exE)+ note k=this
  1.2871 +      have *:"dest_vec1 u \<le> dest_vec1 v" using xk unfolding k by auto
  1.2872 +      have ball:"\<forall>xa\<in>k. xa \<in> ball x (d (dest_vec1 x))" using as(2)[unfolded fine_def,rule_format,OF `(x,k)\<in>p`,unfolded split_conv subset_eq] .
  1.2873 +      have "norm ((v$1 - u$1) *\<^sub>R f' x - (f v - f u)) \<le> norm (f u - f x - (u$1 - x$1) *\<^sub>R f' x) + norm (f v - f x - (v$1 - x$1) *\<^sub>R f' x)"
  1.2874 +        apply(rule order_trans[OF _ norm_triangle_ineq4]) apply(rule eq_refl) apply(rule arg_cong[where f=norm])
  1.2875 +        unfolding scaleR.diff_left by(auto simp add:group_simps)
  1.2876 +      also have "... \<le> e * norm (dest_vec1 u - dest_vec1 x) + e * norm (dest_vec1 v - dest_vec1 x)"
  1.2877 +        apply(rule add_mono) apply(rule d(2)[of "x$1" "u$1",unfolded o_def vec1_dest_vec1]) prefer 4
  1.2878 +        apply(rule d(2)[of "x$1" "v$1",unfolded o_def vec1_dest_vec1])
  1.2879 +        using ball[rule_format,of u] ball[rule_format,of v] 
  1.2880 +        using xk(1-2) unfolding k subset_eq by(auto simp add:vector_dist_norm norm_real)
  1.2881 +      also have "... \<le> e * dest_vec1 (interval_upperbound k - interval_lowerbound k)"
  1.2882 +        unfolding k interval_bound_1[OF *] using xk(1) unfolding k by(auto simp add:vector_dist_norm norm_real field_simps)
  1.2883 +      finally show "norm (content k *\<^sub>R f' x - (f (interval_upperbound k) - f (interval_lowerbound k))) \<le>
  1.2884 +        e * dest_vec1 (interval_upperbound k - interval_lowerbound k)" unfolding k interval_bound_1[OF *] content_1[OF *] .
  1.2885 +    qed(insert as, auto) qed qed
  1.2886 +
  1.2887 +subsection {* Attempt a systematic general set of "offset" results for components. *}
  1.2888 +
  1.2889 +lemma gauge_modify:
  1.2890 +  assumes "(\<forall>s. open s \<longrightarrow> open {x. f(x) \<in> s})" "gauge d"
  1.2891 +  shows "gauge (\<lambda>x y. d (f x) (f y))"
  1.2892 +  using assms unfolding gauge_def apply safe defer apply(erule_tac x="f x" in allE)
  1.2893 +  apply(erule_tac x="d (f x)" in allE) unfolding mem_def Collect_def by auto
  1.2894 +
  1.2895 +subsection {* Only need trivial subintervals if the interval itself is trivial. *}
  1.2896 +
  1.2897 +lemma division_of_nontrivial: fixes s::"(real^'n) set set"
  1.2898 +  assumes "s division_of {a..b}" "content({a..b}) \<noteq> 0"
  1.2899 +  shows "{k. k \<in> s \<and> content k \<noteq> 0} division_of {a..b}" using assms(1) apply-
  1.2900 +proof(induct "card s" arbitrary:s rule:nat_less_induct)
  1.2901 +  fix s::"(real^'n) set set" assume assm:"s division_of {a..b}"
  1.2902 +    "\<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}" 
  1.2903 +  note s = division_ofD[OF assm(1)] let ?thesis = "{k \<in> s. content k \<noteq> 0} division_of {a..b}"
  1.2904 +  { presume *:"{k \<in> s. content k \<noteq> 0} \<noteq> s \<Longrightarrow> ?thesis"
  1.2905 +    show ?thesis apply cases defer apply(rule *,assumption) using assm(1) by auto }
  1.2906 +  assume noteq:"{k \<in> s. content k \<noteq> 0} \<noteq> s"
  1.2907 +  then obtain k where k:"k\<in>s" "content k = 0" by auto
  1.2908 +  from s(4)[OF k(1)] guess c d apply-by(erule exE)+ note k=k this
  1.2909 +  from k have "card s > 0" unfolding card_gt_0_iff using assm(1) by auto
  1.2910 +  hence card:"card (s - {k}) < card s" using assm(1) k(1) apply(subst card_Diff_singleton_if) by auto
  1.2911 +  have *:"closed (\<Union>(s - {k}))" apply(rule closed_Union) defer apply rule apply(drule DiffD1,drule s(4))
  1.2912 +    apply safe apply(rule closed_interval) using assm(1) by auto
  1.2913 +  have "k \<subseteq> \<Union>(s - {k})" apply safe apply(rule *[unfolded closed_limpt,rule_format]) unfolding islimpt_approachable
  1.2914 +  proof safe fix x and e::real assume as:"x\<in>k" "e>0"
  1.2915 +    from k(2)[unfolded k content_eq_0] guess i .. 
  1.2916 +    hence i:"c$i = d$i" using s(3)[OF k(1),unfolded k] unfolding interval_ne_empty by smt
  1.2917 +    hence xi:"x$i = d$i" using as unfolding k mem_interval by smt
  1.2918 +    def y \<equiv> "(\<chi> j. if j = i then if c$i \<le> (a$i + b$i) / 2 then c$i + min e (b$i - c$i) / 2 else c$i - min e (c$i - a$i) / 2 else x$j)"
  1.2919 +    show "\<exists>x'\<in>\<Union>(s - {k}). x' \<noteq> x \<and> dist x' x < e" apply(rule_tac x=y in bexI) 
  1.2920 +    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)
  1.2921 +      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]]
  1.2922 +      hence xyi:"y$i \<noteq> x$i" unfolding y_def unfolding i xi Cart_lambda_beta if_P[OF refl]
  1.2923 +        apply(cases) apply(subst if_P,assumption) unfolding if_not_P not_le using as(2) using assms(2)[unfolded content_eq_0] by smt+ 
  1.2924 +      thus "y \<noteq> x" unfolding Cart_eq by auto
  1.2925 +      have *:"UNIV = insert i (UNIV - {i})" by auto
  1.2926 +      have "norm (y - x) < e + setsum (\<lambda>i. 0) (UNIV::'n set)" apply(rule le_less_trans[OF norm_le_l1])
  1.2927 +        apply(subst *,subst setsum_insert) prefer 3 apply(rule add_less_le_mono)
  1.2928 +      proof- show "\<bar>(y - x) $ i\<bar> < e" unfolding y_def Cart_lambda_beta vector_minus_component if_P[OF refl]
  1.2929 +          apply(cases) apply(subst if_P,assumption) unfolding if_not_P unfolding i xi using di as(2) by auto
  1.2930 +        show "(\<Sum>i\<in>UNIV - {i}. \<bar>(y - x) $ i\<bar>) \<le> (\<Sum>i\<in>UNIV. 0)" unfolding y_def by auto 
  1.2931 +      qed auto thus "dist y x < e" unfolding vector_dist_norm by auto
  1.2932 +      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
  1.2933 +      moreover have "y \<in> \<Union>s" unfolding s mem_interval
  1.2934 +      proof note simps = y_def Cart_lambda_beta if_not_P
  1.2935 +        fix j::'n show "a $ j \<le> y $ j \<and> y $ j \<le> b $ j" 
  1.2936 +        proof(cases "j = i") case False have "x \<in> {a..b}" using s(2)[OF k(1)] as(1) by auto
  1.2937 +          thus ?thesis unfolding simps if_not_P[OF False] unfolding mem_interval by auto
  1.2938 +        next case True note T = this show ?thesis
  1.2939 +          proof(cases "c $ i \<le> (a $ i + b $ i) / 2")
  1.2940 +            case True show ?thesis unfolding simps if_P[OF T] if_P[OF True] unfolding i
  1.2941 +              using True as(2) di apply-apply rule unfolding T by (auto simp add:field_simps) 
  1.2942 +          next case False thus ?thesis unfolding simps if_P[OF T] if_not_P[OF False] unfolding i
  1.2943 +              using True as(2) di apply-apply rule unfolding T by (auto simp add:field_simps)
  1.2944 +          qed qed qed
  1.2945 +      ultimately show "y \<in> \<Union>(s - {k})" by auto
  1.2946 +    qed qed hence "\<Union>(s - {k}) = {a..b}" unfolding s(6)[THEN sym] by auto
  1.2947 +  hence  "{ka \<in> s - {k}. content ka \<noteq> 0} division_of {a..b}" apply-apply(rule assm(2)[rule_format,OF card refl])
  1.2948 +    apply(rule division_ofI) defer apply(rule_tac[1-4] s) using assm(1) by auto
  1.2949 +  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
  1.2950 +
  1.2951 +subsection {* Integrabibility on subintervals. *}
  1.2952 +
  1.2953 +lemma operative_integrable: fixes f::"real^'n \<Rightarrow> 'a::banach" shows
  1.2954 +  "operative op \<and> (\<lambda>i. f integrable_on i)"
  1.2955 +  unfolding operative_def neutral_and apply safe apply(subst integrable_on_def)
  1.2956 +  unfolding has_integral_null_eq apply(rule,rule refl) apply(rule,assumption)+
  1.2957 +  unfolding integrable_on_def by(auto intro: has_integral_split)
  1.2958 +
  1.2959 +lemma integrable_subinterval: fixes f::"real^'n \<Rightarrow> 'a::banach" 
  1.2960 +  assumes "f integrable_on {a..b}" "{c..d} \<subseteq> {a..b}" shows "f integrable_on {c..d}" 
  1.2961 +  apply(cases "{c..d} = {}") defer apply(rule partial_division_extend_1[OF assms(2)],assumption)
  1.2962 +  using operative_division_and[OF operative_integrable,THEN sym,of _ _ _ f] assms(1) by auto
  1.2963 +
  1.2964 +subsection {* Combining adjacent intervals in 1 dimension. *}
  1.2965 +
  1.2966 +lemma has_integral_combine: assumes "(a::real^1) \<le> c" "c \<le> b"
  1.2967 +  "(f has_integral i) {a..c}" "(f has_integral (j::'a::banach)) {c..b}"
  1.2968 +  shows "(f has_integral (i + j)) {a..b}"
  1.2969 +proof- note operative_integral[of f, unfolded operative_1_le[OF monoidal_lifted[OF monoidal_monoid]]]
  1.2970 +  note conjunctD2[OF this,rule_format] note * = this(2)[OF conjI[OF assms(1-2)],unfolded if_P[OF assms(3)]]
  1.2971 +  hence "f integrable_on {a..b}" apply- apply(rule ccontr) apply(subst(asm) if_P) defer
  1.2972 +    apply(subst(asm) if_P) using assms(3-) by auto
  1.2973 +  with * show ?thesis apply-apply(subst(asm) if_P) defer apply(subst(asm) if_P) defer apply(subst(asm) if_P)
  1.2974 +    unfolding lifted.simps using assms(3-) by(auto simp add: integrable_on_def integral_unique) qed
  1.2975 +
  1.2976 +lemma integral_combine: fixes f::"real^1 \<Rightarrow> 'a::banach"
  1.2977 +  assumes "a \<le> c" "c \<le> b" "f integrable_on ({a..b})"
  1.2978 +  shows "integral {a..c} f + integral {c..b} f = integral({a..b}) f"
  1.2979 +  apply(rule integral_unique[THEN sym]) apply(rule has_integral_combine[OF assms(1-2)])
  1.2980 +  apply(rule_tac[!] integrable_integral integrable_subinterval[OF assms(3)])+ using assms(1-2) by auto
  1.2981 +
  1.2982 +lemma integrable_combine: fixes f::"real^1 \<Rightarrow> 'a::banach"
  1.2983 +  assumes "a \<le> c" "c \<le> b" "f integrable_on {a..c}" "f integrable_on {c..b}"
  1.2984 +  shows "f integrable_on {a..b}" using assms unfolding integrable_on_def by(fastsimp intro!:has_integral_combine)
  1.2985 +
  1.2986 +subsection {* Reduce integrability to "local" integrability. *}
  1.2987 +
  1.2988 +lemma integrable_on_little_subintervals: fixes f::"real^'n \<Rightarrow> 'a::banach"
  1.2989 +  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}"
  1.2990 +  shows "f integrable_on {a..b}"
  1.2991 +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})"
  1.2992 +    using assms by auto note this[unfolded gauge_existence_lemma] from choice[OF this] guess d .. note d=this[rule_format]
  1.2993 +  guess p apply(rule fine_division_exists[OF gauge_ball_dependent,of d a b]) using d by auto note p=this(1-2)
  1.2994 +  note division_of_tagged_division[OF this(1)] note * = operative_division_and[OF operative_integrable,OF this,THEN sym,of f]
  1.2995 +  show ?thesis unfolding * apply safe unfolding snd_conv
  1.2996 +  proof- fix x k assume "(x,k) \<in> p" note tagged_division_ofD(2-4)[OF p(1) this] fineD[OF p(2) this]
  1.2997 +    thus "f integrable_on k" apply safe apply(rule d[THEN conjunct2,rule_format,of x]) by auto qed qed
  1.2998 +
  1.2999 +subsection {* Second FCT or existence of antiderivative. *}
  1.3000 +
  1.3001 +lemma integrable_const[intro]:"(\<lambda>x. c) integrable_on {a..b}"
  1.3002 +  unfolding integrable_on_def by(rule,rule has_integral_const)
  1.3003 +
  1.3004 +lemma integral_has_vector_derivative: fixes f::"real \<Rightarrow> 'a::banach"
  1.3005 +  assumes "continuous_on {a..b} f" "x \<in> {a..b}"
  1.3006 +  shows "((\<lambda>u. integral {vec a..vec u} (f o dest_vec1)) has_vector_derivative f(x)) (at x within {a..b})"
  1.3007 +  unfolding has_vector_derivative_def has_derivative_within_alt
  1.3008 +apply safe apply(rule scaleR.bounded_linear_left)
  1.3009 +proof- fix e::real assume e:"e>0"
  1.3010 +  note compact_uniformly_continuous[OF assms(1) compact_real_interval,unfolded uniformly_continuous_on_def]
  1.3011 +  from this[rule_format,OF e] guess d apply-by(erule conjE exE)+ note d=this[rule_format]
  1.3012 +  let ?I = "\<lambda>a b. integral {vec1 a..vec1 b} (f \<circ> dest_vec1)"
  1.3013 +  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)"
  1.3014 +  proof(rule,rule,rule d,safe) case goal1 show ?case proof(cases "y < x")
  1.3015 +      case False have "f \<circ> dest_vec1 integrable_on {vec1 a..vec1 y}" apply(rule integrable_subinterval,rule integrable_continuous)
  1.3016 +        apply(rule continuous_on_o_dest_vec1 assms)+  unfolding not_less using assms(2) goal1 by auto
  1.3017 +      hence *:"?I a y - ?I a x = ?I x y" unfolding group_simps apply(subst eq_commute) apply(rule integral_combine)
  1.3018 +        using False unfolding not_less using assms(2) goal1 by auto
  1.3019 +      have **:"norm (y - x) = content {vec1 x..vec1 y}" apply(subst content_1) using False unfolding not_less by auto
  1.3020 +      show ?thesis unfolding ** apply(rule has_integral_bound[where f="(\<lambda>u. f u - f x) o dest_vec1"]) unfolding * unfolding o_def
  1.3021 +        defer apply(rule has_integral_sub) apply(rule integrable_integral)
  1.3022 +        apply(rule integrable_subinterval,rule integrable_continuous) apply(rule continuous_on_o_dest_vec1[unfolded o_def] assms)+
  1.3023 +      proof- show "{vec1 x..vec1 y} \<subseteq> {vec1 a..vec1 b}" using goal1 assms(2) by auto
  1.3024 +        have *:"y - x = norm(y - x)" using False by auto
  1.3025 +        show "((\<lambda>xa. f x) has_integral (y - x) *\<^sub>R f x) {vec1 x..vec1 y}" apply(subst *) unfolding ** by auto
  1.3026 +        show "\<forall>xa\<in>{vec1 x..vec1 y}. norm (f (dest_vec1 xa) - f x) \<le> e" apply safe apply(rule less_imp_le)
  1.3027 +          apply(rule d(2)[unfolded vector_dist_norm]) using assms(2) using goal1 by auto
  1.3028 +      qed(insert e,auto)
  1.3029 +    next case True have "f \<circ> dest_vec1 integrable_on {vec1 a..vec1 x}" apply(rule integrable_subinterval,rule integrable_continuous)
  1.3030 +        apply(rule continuous_on_o_dest_vec1 assms)+  unfolding not_less using assms(2) goal1 by auto
  1.3031 +      hence *:"?I a x - ?I a y = ?I y x" unfolding group_simps apply(subst eq_commute) apply(rule integral_combine)
  1.3032 +        using True using assms(2) goal1 by auto
  1.3033 +      have **:"norm (y - x) = content {vec1 y..vec1 x}" apply(subst content_1) using True unfolding not_less by auto
  1.3034 +      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 
  1.3035 +      show ?thesis apply(subst ***) unfolding norm_minus_cancel **
  1.3036 +        apply(rule has_integral_bound[where f="(\<lambda>u. f u - f x) o dest_vec1"]) unfolding * unfolding o_def
  1.3037 +        defer apply(rule has_integral_sub) apply(subst minus_minus[THEN sym]) unfolding minus_minus
  1.3038 +        apply(rule integrable_integral) apply(rule integrable_subinterval,rule integrable_continuous)
  1.3039 +        apply(rule continuous_on_o_dest_vec1[unfolded o_def] assms)+
  1.3040 +      proof- show "{vec1 y..vec1 x} \<subseteq> {vec1 a..vec1 b}" using goal1 assms(2) by auto
  1.3041 +        have *:"x - y = norm(y - x)" using True by auto
  1.3042 +        show "((\<lambda>xa. f x) has_integral (x - y) *\<^sub>R f x) {vec1 y..vec1 x}" apply(subst *) unfolding ** by auto
  1.3043 +        show "\<forall>xa\<in>{vec1 y..vec1 x}. norm (f (dest_vec1 xa) - f x) \<le> e" apply safe apply(rule less_imp_le)
  1.3044 +          apply(rule d(2)[unfolded vector_dist_norm]) using assms(2) using goal1 by auto
  1.3045 +      qed(insert e,auto) qed qed qed
  1.3046 +
  1.3047 +lemma integral_has_vector_derivative': fixes f::"real^1 \<Rightarrow> 'a::banach"
  1.3048 +  assumes "continuous_on {a..b} f" "x \<in> {a..b}"
  1.3049 +  shows "((\<lambda>u. (integral {a..vec u} f)) has_vector_derivative f x) (at (x$1) within {a$1..b$1})"
  1.3050 +  using integral_has_vector_derivative[OF continuous_on_o_vec1[OF assms(1)], of "x$1"]
  1.3051 +  unfolding o_def vec1_dest_vec1 using assms(2) by auto
  1.3052 +
  1.3053 +lemma antiderivative_continuous: assumes "continuous_on {a..b::real} f"
  1.3054 +  obtains g where "\<forall>x\<in> {a..b}. (g has_vector_derivative (f(x)::_::banach)) (at x within {a..b})"
  1.3055 +  apply(rule that,rule) using integral_has_vector_derivative[OF assms] by auto
  1.3056 +
  1.3057 +subsection {* Combined fundamental theorem of calculus. *}
  1.3058 +
  1.3059 +lemma antiderivative_integral_continuous: fixes f::"real \<Rightarrow> 'a::banach" assumes "continuous_on {a..b} f"
  1.3060 +  obtains g where "\<forall>u\<in>{a..b}. \<forall>v \<in> {a..b}. u \<le> v \<longrightarrow> ((f o dest_vec1) has_integral (g v - g u)) {vec u..vec v}"
  1.3061 +proof- from antiderivative_continuous[OF assms] guess g . note g=this
  1.3062 +  show ?thesis apply(rule that[of g])
  1.3063 +  proof safe case goal1 have "\<forall>x\<in>{u..v}. (g has_vector_derivative f x) (at x within {u..v})"
  1.3064 +      apply(rule,rule has_vector_derivative_within_subset) apply(rule g[rule_format]) using goal1(1-2) by auto
  1.3065 +    thus ?case using fundamental_theorem_of_calculus[OF goal1(3),of "g o dest_vec1" "f o dest_vec1"]
  1.3066 +      unfolding o_def vec1_dest_vec1 by auto qed qed
  1.3067 +
  1.3068 +subsection {* General "twiddling" for interval-to-interval function image. *}
  1.3069 +
  1.3070 +lemma has_integral_twiddle:
  1.3071 +  assumes "0 < r" "\<forall>x. h(g x) = x" "\<forall>x. g(h x) = x" "\<forall>x. continuous (at x) g"
  1.3072 +  "\<forall>u v. \<exists>w z. g ` {u..v} = {w..z}"
  1.3073 +  "\<forall>u v. \<exists>w z. h ` {u..v} = {w..z}"
  1.3074 +  "\<forall>u v. content(g ` {u..v}) = r * content {u..v}"
  1.3075 +  "(f has_integral i) {a..b}"
  1.3076 +  shows "((\<lambda>x. f(g x)) has_integral (1 / r) *\<^sub>R i) (h ` {a..b})"
  1.3077 +proof- { presume *:"{a..b} \<noteq> {} \<Longrightarrow> ?thesis"
  1.3078 +    show ?thesis apply cases defer apply(rule *,assumption)
  1.3079 +    proof- case goal1 thus ?thesis unfolding goal1 assms(8)[unfolded goal1 has_integral_empty_eq] by auto qed }
  1.3080 +  assume "{a..b} \<noteq> {}" from assms(6)[rule_format,of a b] guess w z apply-by(erule exE)+ note wz=this
  1.3081 +  have inj:"inj g" "inj h" unfolding inj_on_def apply safe apply(rule_tac[!] ccontr)
  1.3082 +    using assms(2) apply(erule_tac x=x in allE) using assms(2) apply(erule_tac x=y in allE) defer
  1.3083 +    using assms(3) apply(erule_tac x=x in allE) using assms(3) apply(erule_tac x=y in allE) by auto
  1.3084 +  show ?thesis unfolding has_integral_def has_integral_compact_interval_def apply(subst if_P) apply(rule,rule,rule wz)
  1.3085 +  proof safe fix e::real assume e:"e>0" hence "e * r > 0" using assms(1) by(rule mult_pos_pos)
  1.3086 +    from assms(8)[unfolded has_integral,rule_format,OF this] guess d apply-by(erule exE conjE)+ note d=this[rule_format]
  1.3087 +    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)
  1.3088 +    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)"
  1.3089 +    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
  1.3090 +      fix p assume as:"p tagged_division_of h ` {a..b}" "d' fine p" note p = tagged_division_ofD[OF as(1)] 
  1.3091 +      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 
  1.3092 +      proof safe show "finite ((\<lambda>(x, k). (g x, g ` k)) ` p)" using as by auto
  1.3093 +        show "d fine (\<lambda>(x, k). (g x, g ` k)) ` p" using as(2) unfolding fine_def d' by auto
  1.3094 +        fix x k assume xk[intro]:"(x,k) \<in> p" show "g x \<in> g ` k" using p(2)[OF xk] by auto
  1.3095 +        show "\<exists>u v. g ` k = {u..v}" using p(4)[OF xk] using assms(5-6) by auto
  1.3096 +        { 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)"]
  1.3097 +            using assms(2)[rule_format,of y] unfolding inj_image_mem_iff[OF inj(2)] by auto }
  1.3098 +        fix x' k' assume xk':"(x',k') \<in> p" fix z assume "z \<in> interior (g ` k)" "z \<in> interior (g ` k')"
  1.3099 +        hence *:"interior (g ` k) \<inter> interior (g ` k') \<noteq> {}" by auto
  1.3100 +        have same:"(x, k) = (x', k')" apply-apply(rule ccontr,drule p(5)[OF xk xk'])
  1.3101 +        proof- assume as:"interior k \<inter> interior k' = {}" from nonempty_witness[OF *] guess z .
  1.3102 +          hence "z \<in> g ` (interior k \<inter> interior k')" using interior_image_subset[OF assms(4) inj(1)]
  1.3103 +            unfolding image_Int[OF inj(1)] by auto thus False using as by blast
  1.3104 +        qed thus "g x = g x'" by auto
  1.3105 +        { fix z assume "z \<in> k"  thus  "g z \<in> g ` k'" using same by auto }
  1.3106 +        { fix z assume "z \<in> k'" thus  "g z \<in> g ` k"  using same by auto }
  1.3107 +      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
  1.3108 +        then guess X unfolding Union_iff .. note X=this from this(1) guess y unfolding mem_Collect_eq ..
  1.3109 +        thus "x \<in> \<Union>{k. \<exists>x. (x, k) \<in> (\<lambda>(x, k). (g x, g ` k)) ` p}" apply-
  1.3110 +          apply(rule_tac X="g ` X" in UnionI) defer apply(rule_tac x="h x" in image_eqI)
  1.3111 +          using X(2) assms(3)[rule_format,of x] by auto
  1.3112 +      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
  1.3113 +       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 group_simps add_left_cancel
  1.3114 +        unfolding setsum_reindex[OF *] apply(subst scaleR_right.setsum) defer apply(rule setsum_cong2) unfolding o_def split_paired_all split_conv
  1.3115 +        apply(drule p(4)) apply safe unfolding assms(7)[rule_format] using p by auto
  1.3116 +      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]
  1.3117 +        unfolding real_scaleR_def using assms(1) by auto finally have *:"?l = ?r" .
  1.3118 +      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
  1.3119 +        using assms(1) by(auto simp add:field_simps) qed qed qed
  1.3120 +
  1.3121 +subsection {* Special case of a basic affine transformation. *}
  1.3122 +
  1.3123 +lemma interval_image_affinity_interval: shows "\<exists>u v. (\<lambda>x. m *\<^sub>R (x::real^'n) + c) ` {a..b} = {u..v}"
  1.3124 +  unfolding image_affinity_interval by auto
  1.3125 +
  1.3126 +lemmas Cart_simps = Cart_nth.add Cart_nth.minus Cart_nth.zero Cart_nth.diff Cart_nth.scaleR real_scaleR_def Cart_lambda_beta
  1.3127 +   Cart_eq vector_le_def vector_less_def
  1.3128 +
  1.3129 +lemma setprod_cong2: assumes "\<And>x. x \<in> A \<Longrightarrow> f x = g x" shows "setprod f A = setprod g A"
  1.3130 +  apply(rule setprod_cong) using assms by auto
  1.3131 +
  1.3132 +lemma content_image_affinity_interval: 
  1.3133 + "content((\<lambda>x::real^'n. m *\<^sub>R x + c) ` {a..b}) = (abs m) ^ CARD('n) * content {a..b}" (is "?l = ?r")
  1.3134 +proof- { presume *:"{a..b}\<noteq>{} \<Longrightarrow> ?thesis" show ?thesis apply(cases,rule *,assumption)
  1.3135 +      unfolding not_not using content_empty by auto }
  1.3136 +  assume as:"{a..b}\<noteq>{}" show ?thesis proof(cases "m \<ge> 0")
  1.3137 +    case True show ?thesis unfolding image_affinity_interval if_not_P[OF as] if_P[OF True]
  1.3138 +      unfolding content_closed_interval'[OF as] apply(subst content_closed_interval') 
  1.3139 +      defer apply(subst setprod_constant[THEN sym]) apply(rule finite_UNIV) unfolding setprod_timesf[THEN sym]
  1.3140 +      apply(rule setprod_cong2) using True as unfolding interval_ne_empty Cart_simps not_le  
  1.3141 +      by(auto simp add:field_simps intro:mult_left_mono)
  1.3142 +  next case False show ?thesis unfolding image_affinity_interval if_not_P[OF as] if_not_P[OF False]
  1.3143 +      unfolding content_closed_interval'[OF as] apply(subst content_closed_interval') 
  1.3144 +      defer apply(subst setprod_constant[THEN sym]) apply(rule finite_UNIV) unfolding setprod_timesf[THEN sym]
  1.3145 +      apply(rule setprod_cong2) using False as unfolding interval_ne_empty Cart_simps not_le 
  1.3146 +      by(auto simp add:field_simps mult_le_cancel_left_neg) qed qed
  1.3147 +
  1.3148 +lemma has_integral_affinity: assumes "(f has_integral i) {a..b::real^'n}" "m \<noteq> 0"
  1.3149 +  shows "((\<lambda>x. f(m *\<^sub>R x + c)) has_integral ((1 / (abs(m) ^ CARD('n::finite))) *\<^sub>R i)) ((\<lambda>x. (1 / m) *\<^sub>R x + -((1 / m) *\<^sub>R c)) ` {a..b})"
  1.3150 +  apply(rule has_integral_twiddle,safe) unfolding Cart_eq Cart_simps apply(rule zero_less_power)
  1.3151 +  defer apply(insert assms(2), simp add:field_simps) apply(insert assms(2), simp add:field_simps)
  1.3152 +  apply(rule continuous_intros)+ apply(rule interval_image_affinity_interval)+ apply(rule content_image_affinity_interval) using assms by auto
  1.3153 +
  1.3154 +lemma integrable_affinity: assumes "f integrable_on {a..b}" "m \<noteq> 0"
  1.3155 +  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})"
  1.3156 +  using assms unfolding integrable_on_def apply safe apply(drule has_integral_affinity) by auto
  1.3157 +
  1.3158 +subsection {* Special case of stretching coordinate axes separately. *}
  1.3159 +
  1.3160 +lemma image_stretch_interval:
  1.3161 +  "(\<lambda>x. \<chi> k. m k * x$k) ` {a..b::real^'n} =
  1.3162 +  (if {a..b} = {} then {} else {(\<chi> k. min (m(k) * a$k) (m(k) * b$k)) ..  (\<chi> k. max (m(k) * a$k) (m(k) * b$k))})" (is "?l = ?r")
  1.3163 +proof(cases "{a..b}={}") case True thus ?thesis unfolding True by auto
  1.3164 +next have *:"\<And>P Q. (\<forall>i. P i) \<and> (\<forall>i. Q i) \<longleftrightarrow> (\<forall>i. P i \<and> Q i)" by auto
  1.3165 +  case False note ab = this[unfolded interval_ne_empty]
  1.3166 +  show ?thesis apply-apply(rule set_ext)
  1.3167 +  proof- fix x::"real^'n" have **:"\<And>P Q. (\<forall>i. P i = Q i) \<Longrightarrow> (\<forall>i. P i) = (\<forall>i. Q i)" by auto
  1.3168 +    show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" unfolding if_not_P[OF False] 
  1.3169 +      unfolding image_iff mem_interval Bex_def Cart_simps Cart_eq *
  1.3170 +      unfolding lambda_skolem[THEN sym,of "\<lambda> i xa. (a $ i \<le> xa \<and> xa \<le> b $ i) \<and> x $ i = m i * xa"]
  1.3171 +    proof(rule **,rule) fix i::'n show "(\<exists>xa. (a $ i \<le> xa \<and> xa \<le> b $ i) \<and> x $ i = m i * xa) =
  1.3172 +        (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))"
  1.3173 +      proof(cases "m i = 0") case True thus ?thesis using ab by auto
  1.3174 +      next case False hence "0 < m i \<or> 0 > m i" by auto thus ?thesis apply-
  1.3175 +        proof(erule disjE) assume as:"0 < m i" hence *:"min (m i * a $ i) (m i * b $ i) = m i * a $ i"
  1.3176 +            "max (m i * a $ i) (m i * b $ i) = m i * b $ i" using ab unfolding min_def max_def by auto
  1.3177 +          show ?thesis unfolding * apply rule defer apply(rule_tac x="1 / m i * x$i" in exI)
  1.3178 +            using as by(auto simp add:field_simps)
  1.3179 +        next assume as:"0 > m i" hence *:"max (m i * a $ i) (m i * b $ i) = m i * a $ i"
  1.3180 +            "min (m i * a $ i) (m i * b $ i) = m i * b $ i" using ab as unfolding min_def max_def 
  1.3181 +            by(auto simp add:field_simps mult_le_cancel_left_neg intro:real_le_antisym)
  1.3182 +          show ?thesis unfolding * apply rule defer apply(rule_tac x="1 / m i * x$i" in exI)
  1.3183 +            using as by(auto simp add:field_simps) qed qed qed qed qed 
  1.3184 +
  1.3185 +lemma interval_image_stretch_interval: "\<exists>u v. (\<lambda>x. \<chi> k. m k * x$k) ` {a..b::real^'n} = {u..v}"
  1.3186 +  unfolding image_stretch_interval by auto 
  1.3187 +
  1.3188 +lemma content_image_stretch_interval:
  1.3189 +  "content((\<lambda>x::real^'n. \<chi> k. m k * x$k) ` {a..b}) = abs(setprod m UNIV) * content({a..b})"
  1.3190 +proof(cases "{a..b} = {}") case True thus ?thesis
  1.3191 +    unfolding content_def image_is_empty image_stretch_interval if_P[OF True] by auto
  1.3192 +next case False hence "(\<lambda>x. \<chi> k. m k * x $ k) ` {a..b} \<noteq> {}" by auto
  1.3193 +  thus ?thesis using False unfolding content_def image_stretch_interval apply- unfolding interval_bounds' if_not_P
  1.3194 +    unfolding abs_setprod setprod_timesf[THEN sym] apply(rule setprod_cong2) unfolding Cart_lambda_beta
  1.3195 +  proof- fix i::'n have "(m i < 0 \<or> m i > 0) \<or> m i = 0" by auto
  1.3196 +    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)"
  1.3197 +      apply-apply(erule disjE)+ unfolding min_def max_def using False[unfolded interval_ne_empty,rule_format,of i] 
  1.3198 +      by(auto simp add:field_simps not_le mult_le_cancel_left_neg mult_le_cancel_left_pos) qed qed
  1.3199 +
  1.3200 +lemma has_integral_stretch: assumes "(f has_integral i) {a..b}" "\<forall>k. ~(m k = 0)"
  1.3201 +  shows "((\<lambda>x. f(\<chi> k. m k * x$k)) has_integral
  1.3202 +             ((1/(abs(setprod m UNIV))) *\<^sub>R i)) ((\<lambda>x. \<chi> k. 1/(m k) * x$k) ` {a..b})"
  1.3203 +  apply(rule has_integral_twiddle) unfolding zero_less_abs_iff content_image_stretch_interval
  1.3204 +  unfolding image_stretch_interval empty_as_interval Cart_eq using assms
  1.3205 +proof- show "\<forall>x. continuous (at x) (\<lambda>x. \<chi> k. m k * x $ k)"
  1.3206 +   apply(rule,rule linear_continuous_at) unfolding linear_linear
  1.3207 +   unfolding linear_def Cart_simps Cart_eq by(auto simp add:field_simps) qed auto
  1.3208 +
  1.3209 +lemma integrable_stretch: 
  1.3210 +  assumes "f integrable_on {a..b}" "\<forall>k. ~(m k = 0)"
  1.3211 +  shows "(\<lambda>x. f(\<chi> k. m k * x$k)) integrable_on ((\<lambda>x. \<chi> k. 1/(m k) * x$k) ` {a..b})"
  1.3212 +  using assms unfolding integrable_on_def apply-apply(erule exE) apply(drule has_integral_stretch) by auto
  1.3213 +
  1.3214 +subsection {* even more special cases. *}
  1.3215 +
  1.3216 +lemma uminus_interval_vector[simp]:"uminus ` {a..b} = {-b .. -a::real^'n}"
  1.3217 +  apply(rule set_ext,rule) defer unfolding image_iff
  1.3218 +  apply(rule_tac x="-x" in bexI) by(auto simp add:vector_le_def minus_le_iff le_minus_iff)
  1.3219 +
  1.3220 +lemma has_integral_reflect_lemma[intro]: assumes "(f has_integral i) {a..b}"
  1.3221 +  shows "((\<lambda>x. f(-x)) has_integral i) {-b .. -a}"
  1.3222 +  using has_integral_affinity[OF assms, of "-1" 0] by auto
  1.3223 +
  1.3224 +lemma has_integral_reflect[simp]: "((\<lambda>x. f(-x)) has_integral i) {-b..-a} \<longleftrightarrow> (f has_integral i) ({a..b})"
  1.3225 +  apply rule apply(drule_tac[!] has_integral_reflect_lemma) by auto
  1.3226 +
  1.3227 +lemma integrable_reflect[simp]: "(\<lambda>x. f(-x)) integrable_on {-b..-a} \<longleftrightarrow> f integrable_on {a..b}"
  1.3228 +  unfolding integrable_on_def by auto
  1.3229 +
  1.3230 +lemma integral_reflect[simp]: "integral {-b..-a} (\<lambda>x. f(-x)) = integral ({a..b}) f"
  1.3231 +  unfolding integral_def by auto
  1.3232 +
  1.3233 +subsection {* Stronger form of FCT; quite a tedious proof. *}
  1.3234 +
  1.3235 +(** move this **)
  1.3236 +declare norm_triangle_ineq4[intro] 
  1.3237 +
  1.3238 +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)
  1.3239 +
  1.3240 +lemma additive_tagged_division_1': fixes f::"real \<Rightarrow> 'a::real_normed_vector"
  1.3241 +  assumes "a \<le> b" "p tagged_division_of {vec1 a..vec1 b}"
  1.3242 +  shows "setsum (\<lambda>(x,k). f (dest_vec1 (interval_upperbound k)) - f(dest_vec1 (interval_lowerbound k))) p = f b - f a"
  1.3243 +  using additive_tagged_division_1[OF _ assms(2), of "f o dest_vec1"]
  1.3244 +  unfolding o_def vec1_dest_vec1 using assms(1) by auto
  1.3245 +
  1.3246 +lemma split_minus[simp]:"(\<lambda>(x, k). ?f x k) x - (\<lambda>(x, k). ?g x k) x = (\<lambda>(x, k). ?f x k - ?g x k) x"
  1.3247 +  unfolding split_def by(rule refl)
  1.3248 +
  1.3249 +lemma norm_triangle_le_sub: "norm x + norm y \<le> e \<Longrightarrow> norm (x - y) \<le> e"
  1.3250 +  apply(subst(asm)(2) norm_minus_cancel[THEN sym])
  1.3251 +  apply(drule norm_triangle_le) by(auto simp add:group_simps)
  1.3252 +
  1.3253 +lemma fundamental_theorem_of_calculus_interior:
  1.3254 +  assumes"a \<le> b" "continuous_on {a..b} f" "\<forall>x\<in>{a<..<b}. (f has_vector_derivative f'(x)) (at x)"
  1.3255 +  shows "((f' o dest_vec1) has_integral (f b - f a)) {vec a..vec b}"
  1.3256 +proof- { presume *:"a < b \<Longrightarrow> ?thesis" 
  1.3257 +    show ?thesis proof(cases,rule *,assumption)
  1.3258 +      assume "\<not> a < b" hence "a = b" using assms(1) by auto
  1.3259 +      hence *:"{vec a .. vec b} = {vec b}" "f b - f a = 0" apply(auto simp add: Cart_simps) by smt
  1.3260 +      show ?thesis unfolding *(2) apply(rule has_integral_null) unfolding content_eq_0_1 using * `a=b` by auto
  1.3261 +    qed } assume ab:"a < b"
  1.3262 +  let ?P = "\<lambda>e. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {vec1 a..vec1 b} \<and> d fine p \<longrightarrow>
  1.3263 +                   norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f' \<circ> dest_vec1) x) - (f b - f a)) \<le> e * content {vec1 a..vec1 b})"
  1.3264 +  { presume "\<And>e. e>0 \<Longrightarrow> ?P e" thus ?thesis unfolding has_integral_factor_content by auto }
  1.3265 +  fix e::real assume e:"e>0"
  1.3266 +  note assms(3)[unfolded has_vector_derivative_def has_derivative_at_alt ball_conj_distrib]
  1.3267 +  note conjunctD2[OF this] note bounded=this(1) and this(2)
  1.3268 +  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)"
  1.3269 +    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]
  1.3270 +  from choice[OF this] guess d .. note conjunctD2[OF this[rule_format]] note d = this[rule_format]
  1.3271 +  have "bounded (f ` {a..b})" apply(rule compact_imp_bounded compact_continuous_image)+ using compact_real_interval assms by auto
  1.3272 +  from this[unfolded bounded_pos] guess B .. note B = this[rule_format]
  1.3273 +
  1.3274 +  have "\<exists>da. 0 < da \<and> (\<forall>c. a \<le> c \<and> {a..c} \<subseteq> {a..b} \<and> {a..c} \<subseteq> ball a da
  1.3275 +    \<longrightarrow> norm(content {vec1 a..vec1 c} *\<^sub>R f' a - (f c - f a)) \<le> (e * (b - a)) / 4)"
  1.3276 +  proof- have "a\<in>{a..b}" using ab by auto
  1.3277 +    note assms(2)[unfolded continuous_on_eq_continuous_within,rule_format,OF this]
  1.3278 +    note * = this[unfolded continuous_within Lim_within,rule_format] have "(e * (b - a)) / 8 > 0" using e ab by(auto simp add:field_simps)
  1.3279 +    from *[OF this] guess k .. note k = conjunctD2[OF this,rule_format]
  1.3280 +    have "\<exists>l. 0 < l \<and> norm(l *\<^sub>R f' a) \<le> (e * (b - a)) / 8"
  1.3281 +    proof(cases "f' a = 0") case True
  1.3282 +      thus ?thesis apply(rule_tac x=1 in exI) using ab e by(auto intro!:mult_nonneg_nonneg) 
  1.3283 +    next case False thus ?thesis 
  1.3284 +        apply(rule_tac x="(e * (b - a)) / 8 / norm (f' a)" in exI)
  1.3285 +        using ab e by(auto simp add:field_simps)
  1.3286 +    qed then guess l .. note l = conjunctD2[OF this]
  1.3287 +    show ?thesis apply(rule_tac x="min k l" in exI) apply safe unfolding min_less_iff_conj apply(rule,(rule l k)+)
  1.3288 +    proof- fix c assume as:"a \<le> c" "{a..c} \<subseteq> {a..b}" "{a..c} \<subseteq> ball a (min k l)" 
  1.3289 +      note as' = this[unfolded subset_eq Ball_def mem_ball dist_real_def mem_interval]
  1.3290 +      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)
  1.3291 +      also have "... \<le> e * (b - a) / 8 + e * (b - a) / 8" 
  1.3292 +      proof(rule add_mono) case goal1 have "\<bar>c - a\<bar> \<le> \<bar>l\<bar>" using as' by auto
  1.3293 +        thus ?case apply-apply(rule order_trans[OF _ l(2)]) unfolding norm_scaleR apply(rule mult_right_mono) by auto
  1.3294 +      next case goal2 show ?case apply(rule less_imp_le) apply(cases "a = c") defer
  1.3295 +          apply(rule k(2)[unfolded vector_dist_norm]) using as' e ab by(auto simp add:field_simps)
  1.3296 +      qed finally show "norm (content {vec1 a..vec1 c} *\<^sub>R f' a - (f c - f a)) \<le> e * (b - a) / 4" unfolding content_1'[OF as(1)] by auto
  1.3297 +    qed qed then guess da .. note da=conjunctD2[OF this,rule_format]
  1.3298 +
  1.3299 +  have "\<exists>db>0. \<forall>c\<le>b. {c..b} \<subseteq> {a..b} \<and> {c..b} \<subseteq> ball b db \<longrightarrow> norm(content {vec1 c..vec1 b} *\<^sub>R f' b - (f b - f c)) \<le> (e * (b - a)) / 4"
  1.3300 +  proof- have "b\<in>{a..b}" using ab by auto
  1.3301 +    note assms(2)[unfolded continuous_on_eq_continuous_within,rule_format,OF this]
  1.3302 +    note * = this[unfolded continuous_within Lim_within,rule_format] have "(e * (b - a)) / 8 > 0" using e ab by(auto simp add:field_simps)
  1.3303 +    from *[OF this] guess k .. note k = conjunctD2[OF this,rule_format]
  1.3304 +    have "\<exists>l. 0 < l \<and> norm(l *\<^sub>R f' b) \<le> (e * (b - a)) / 8"
  1.3305 +    proof(cases "f' b = 0") case True
  1.3306 +      thus ?thesis apply(rule_tac x=1 in exI) using ab e by(auto intro!:mult_nonneg_nonneg) 
  1.3307 +    next case False thus ?thesis 
  1.3308 +        apply(rule_tac x="(e * (b - a)) / 8 / norm (f' b)" in exI)
  1.3309 +        using ab e by(auto simp add:field_simps)
  1.3310 +    qed then guess l .. note l = conjunctD2[OF this]
  1.3311 +    show ?thesis apply(rule_tac x="min k l" in exI) apply safe unfolding min_less_iff_conj apply(rule,(rule l k)+)
  1.3312 +    proof- fix c assume as:"c \<le> b" "{c..b} \<subseteq> {a..b}" "{c..b} \<subseteq> ball b (min k l)" 
  1.3313 +      note as' = this[unfolded subset_eq Ball_def mem_ball dist_real_def mem_interval]
  1.3314 +      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)
  1.3315 +      also have "... \<le> e * (b - a) / 8 + e * (b - a) / 8" 
  1.3316 +      proof(rule add_mono) case goal1 have "\<bar>c - b\<bar> \<le> \<bar>l\<bar>" using as' by auto
  1.3317 +        thus ?case apply-apply(rule order_trans[OF _ l(2)]) unfolding norm_scaleR apply(rule mult_right_mono) by auto
  1.3318 +      next case goal2 show ?case apply(rule less_imp_le) apply(cases "b = c") defer apply(subst norm_minus_commute)
  1.3319 +          apply(rule k(2)[unfolded vector_dist_norm]) using as' e ab by(auto simp add:field_simps)
  1.3320 +      qed finally show "norm (content {vec1 c..vec1 b} *\<^sub>R f' b - (f b - f c)) \<le> e * (b - a) / 4" unfolding content_1'[OF as(1)] by auto
  1.3321 +    qed qed then guess db .. note db=conjunctD2[OF this,rule_format]
  1.3322 +
  1.3323 +  let ?d = "(\<lambda>x. ball x (if x=vec1 a then da else if x=vec b then db else d (dest_vec1 x)))"
  1.3324 +  show "?P e" apply(rule_tac x="?d" in exI)
  1.3325 +  proof safe case goal1 show ?case apply(rule gauge_ball_dependent) using ab db(1) da(1) d(1) by auto
  1.3326 +  next case goal2 note as=this let ?A = "{t. fst t \<in> {vec1 a, vec1 b}}" note p = tagged_division_ofD[OF goal2(1)]
  1.3327 +    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
  1.3328 +    note * = additive_tagged_division_1'[OF assms(1) goal2(1), THEN sym]
  1.3329 +    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
  1.3330 +    show ?case unfolding content_1'[OF assms(1)] and *[of "\<lambda>x. x"] *[of f] setsum_subtractf[THEN sym] split_minus
  1.3331 +      unfolding setsum_right_distrib apply(subst(2) pA,subst pA) unfolding setsum_Un_disjoint[OF pA(2-)]
  1.3332 +    proof(rule norm_triangle_le,rule **) 
  1.3333 +      case goal1 show ?case apply(rule order_trans,rule setsum_norm_le) apply(rule pA) defer apply(subst divide.setsum)
  1.3334 +      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"
  1.3335 +          "e * (dest_vec1 (interval_upperbound k) - dest_vec1 (interval_lowerbound k)) / 2
  1.3336 +          < norm (content k *\<^sub>R f' (dest_vec1 x) - (f (dest_vec1 (interval_upperbound k)) - f (dest_vec1 (interval_lowerbound k))))"
  1.3337 +        from p(4)[OF this(1)] guess u v apply-by(erule exE)+ note k=this
  1.3338 +        hence "\<forall>i. u$i \<le> v$i" and uv:"{u,v}\<subseteq>{u..v}" using p(2)[OF as(1)] by auto note this(1) this(1)[unfolded forall_1]
  1.3339 +        note result = as(2)[unfolded k interval_bounds[OF this(1)] content_1[OF this(2)]]
  1.3340 +
  1.3341 +        assume as':"x \<noteq> vec1 a" "x \<noteq> vec1 b" hence "x$1 \<in> {a<..<b}" using p(2-3)[OF as(1)] by(auto simp add:Cart_simps) note  * = d(2)[OF this] 
  1.3342 +        have "norm ((v$1 - u$1) *\<^sub>R f' (x$1) - (f (v$1) - f (u$1))) =
  1.3343 +          norm ((f (u$1) - f (x$1) - (u$1 - x$1) *\<^sub>R f' (x$1)) - (f (v$1) - f (x$1) - (v$1 - x$1) *\<^sub>R f' (x$1)))" 
  1.3344 +          apply(rule arg_cong[of _ _ norm]) unfolding scaleR_left.diff by auto 
  1.3345 +        also have "... \<le> e / 2 * norm (u$1 - x$1) + e / 2 * norm (v$1 - x$1)" apply(rule norm_triangle_le_sub)
  1.3346 +          apply(rule add_mono) apply(rule_tac[!] *) using fineD[OF goal2(2) as(1)] as' unfolding k subset_eq
  1.3347 +          apply- apply(erule_tac x=u in ballE,erule_tac[3] x=v in ballE) using uv by(auto simp add:dist_real)
  1.3348 +        also have "... \<le> e / 2 * norm (v$1 - u$1)" using p(2)[OF as(1)] unfolding k by(auto simp add:field_simps)
  1.3349 +        finally have "e * (dest_vec1 v - dest_vec1 u) / 2 < e * (dest_vec1 v - dest_vec1 u) / 2"
  1.3350 +          apply- apply(rule less_le_trans[OF result]) using uv by auto thus False by auto qed
  1.3351 +
  1.3352 +    next have *:"\<And>x s1 s2::real. 0 \<le> s1 \<Longrightarrow> x \<le> (s1 + s2) / 2 \<Longrightarrow> x - s1 \<le> s2 / 2" by auto
  1.3353 +      case goal2 show ?case apply(rule *) apply(rule setsum_nonneg) apply(rule,unfold split_paired_all split_conv)
  1.3354 +        defer unfolding setsum_Un_disjoint[OF pA(2-),THEN sym] pA(1)[THEN sym] unfolding setsum_right_distrib[THEN sym] 
  1.3355 +        apply(subst additive_tagged_division_1[OF _ as(1)]) unfolding vec1_dest_vec1 apply(rule assms)
  1.3356 +      proof- fix x k assume "(x,k) \<in> p \<inter> {t. fst t \<in> {vec1 a, vec1 b}}" note xk=IntD1[OF this]
  1.3357 +        from p(4)[OF this] guess u v apply-by(erule exE)+ note uv=this
  1.3358 +        with p(2)[OF xk] have "{u..v} \<noteq> {}" by auto
  1.3359 +        thus "0 \<le> e * ((interval_upperbound k)$1 - (interval_lowerbound k)$1)"
  1.3360 +          unfolding uv using e by(auto simp add:field_simps)
  1.3361 +      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
  1.3362 +        show "norm (\<Sum>(x, k)\<in>p \<inter> ?A. content k *\<^sub>R (f' \<circ> dest_vec1) x -
  1.3363 +          (f ((interval_upperbound k)$1) - f ((interval_lowerbound k)$1))) \<le> e * (b - a) / 2" 
  1.3364 +          apply(rule *[where t="p \<inter> {t. fst t \<in> {vec1 a, vec1 b} \<and> content(snd t) \<noteq> 0}"])
  1.3365 +          apply(rule setsum_mono_zero_right[OF pA(2)]) defer apply(rule) unfolding split_paired_all split_conv o_def
  1.3366 +        proof- fix x k assume "(x,k) \<in> p \<inter> {t. fst t \<in> {vec1 a, vec1 b}} - p \<inter> {t. fst t \<in> {vec1 a, vec1 b} \<and> content (snd t) \<noteq> 0}"
  1.3367 +          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
  1.3368 +          have "k\<noteq>{}" using p(2)[OF xk(1)] by auto hence *:"u = v" using xk unfolding uv content_eq_0_1 interval_eq_empty by auto
  1.3369 +          thus "content k *\<^sub>R (f' (x$1)) - (f ((interval_upperbound k)$1) - f ((interval_lowerbound k)$1)) = 0" using xk unfolding uv by auto
  1.3370 +        next have *:"p \<inter> {t. fst t \<in> {vec1 a, vec1 b} \<and> content(snd t) \<noteq> 0} = 
  1.3371 +            {t. t\<in>p \<and> fst t = vec1 a \<and> content(snd t) \<noteq> 0} \<union> {t. t\<in>p \<and> fst t = vec1 b \<and> content(snd t) \<noteq> 0}" by blast
  1.3372 +          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) \<Longrightarrow> e>0 \<Longrightarrow> norm(setsum f s) \<le> e"
  1.3373 +          proof(case_tac "s={}") case goal2 then obtain x where "x\<in>s" by auto hence *:"s = {x}" using goal2(1) by auto
  1.3374 +            thus ?case using `x\<in>s` goal2(2) by auto
  1.3375 +          qed auto
  1.3376 +          case goal2 show ?case apply(subst *, subst setsum_Un_disjoint) prefer 4 apply(rule order_trans[of _ "e * (b - a)/4 + e * (b - a)/4"]) 
  1.3377 +            apply(rule norm_triangle_le,rule add_mono) apply(rule_tac[1-2] **)
  1.3378 +          proof- let ?B = "\<lambda>x. {t \<in> p. fst t = vec1 x \<and> content (snd t) \<noteq> 0}"
  1.3379 +            have pa:"\<And>k. (vec1 a, k) \<in> p \<Longrightarrow> \<exists>v. k = {vec1 a .. v} \<and> vec1 a \<le> v" 
  1.3380 +            proof- case goal1 guess u v using p(4)[OF goal1] apply-by(erule exE)+ note uv=this
  1.3381 +              have *:"u \<le> v" using p(2)[OF goal1] unfolding uv by auto
  1.3382 +              have u:"u = vec1 a" proof(rule ccontr)  have "u \<in> {u..v}" using p(2-3)[OF goal1(1)] unfolding uv by auto 
  1.3383 +                have "u \<ge> vec1 a" using p(2-3)[OF goal1(1)] unfolding uv subset_eq by auto moreover assume "u\<noteq>vec1 a" ultimately
  1.3384 +                have "u > vec1 a" unfolding Cart_simps by auto
  1.3385 +                thus False using p(2)[OF goal1(1)] unfolding uv by(auto simp add:Cart_simps)
  1.3386 +              qed thus ?case apply(rule_tac x=v in exI) unfolding uv using * by auto
  1.3387 +            qed
  1.3388 +            have pb:"\<And>k. (vec1 b, k) \<in> p \<Longrightarrow> \<exists>v. k = {v .. vec1 b} \<and> vec1 b \<ge> v" 
  1.3389 +            proof- case goal1 guess u v using p(4)[OF goal1] apply-by(erule exE)+ note uv=this
  1.3390 +              have *:"u \<le> v" using p(2)[OF goal1] unfolding uv by auto
  1.3391 +              have u:"v = vec1 b" proof(rule ccontr)  have "u \<in> {u..v}" using p(2-3)[OF goal1(1)] unfolding uv by auto 
  1.3392 +                have "v \<le> vec1 b" using p(2-3)[OF goal1(1)] unfolding uv subset_eq by auto moreover assume "v\<noteq>vec1 b" ultimately
  1.3393 +                have "v < vec1 b" unfolding Cart_simps by auto
  1.3394 +                thus False using p(2)[OF goal1(1)] unfolding uv by(auto simp add:Cart_simps)
  1.3395 +              qed thus ?case apply(rule_tac x=u in exI) unfolding uv using * by auto
  1.3396 +            qed
  1.3397 +
  1.3398 +            show "\<forall>x y. x \<in> ?B a \<and> y \<in> ?B a \<longrightarrow> x = y" apply(rule,rule,rule,unfold split_paired_all)
  1.3399 +              unfolding mem_Collect_eq fst_conv snd_conv apply safe
  1.3400 +            proof- fix x k k' assume k:"(vec1 a, k) \<in> p" "(vec1 a, k') \<in> p" "content k \<noteq> 0" "content k' \<noteq> 0"
  1.3401 +              guess v using pa[OF k(1)] .. note v = conjunctD2[OF this]
  1.3402 +              guess v' using pa[OF k(2)] .. note v' = conjunctD2[OF this] let ?v = "vec1 (min (v$1) (v'$1))"
  1.3403 +              have "{vec1 a <..< ?v} \<subseteq> k \<inter> k'" unfolding v v' by(auto simp add:Cart_simps) note subset_interior[OF this,unfolded interior_inter]
  1.3404 +              moreover have "vec1 ((a + ?v$1)/2) \<in> {vec1 a <..< ?v}" using k(3-) unfolding v v' content_eq_0_1 not_le by(auto simp add:Cart_simps)
  1.3405 +              ultimately have "vec1 ((a + ?v$1)/2) \<in> interior k \<inter> interior k'" unfolding interior_open[OF open_interval] by auto
  1.3406 +              hence *:"k = k'" apply- apply(rule ccontr) using p(5)[OF k(1-2)] by auto
  1.3407 +              { assume "x\<in>k" thus "x\<in>k'" unfolding * . } { assume "x\<in>k'" thus "x\<in>k" unfolding * . }
  1.3408 +            qed 
  1.3409 +            show "\<forall>x y. x \<in> ?B b \<and> y \<in> ?B b \<longrightarrow> x = y" apply(rule,rule,rule,unfold split_paired_all)
  1.3410 +              unfolding mem_Collect_eq fst_conv snd_conv apply safe
  1.3411 +            proof- fix x k k' assume k:"(vec1 b, k) \<in> p" "(vec1 b, k') \<in> p" "content k \<noteq> 0" "content k' \<noteq> 0"
  1.3412 +              guess v using pb[OF k(1)] .. note v = conjunctD2[OF this]
  1.3413 +              guess v' using pb[OF k(2)] .. note v' = conjunctD2[OF this] let ?v = "vec1 (max (v$1) (v'$1))"
  1.3414 +              have "{?v <..< vec1 b} \<subseteq> k \<inter> k'" unfolding v v' by(auto simp add:Cart_simps) note subset_interior[OF this,unfolded interior_inter]
  1.3415 +              moreover have "vec1 ((b + ?v$1)/2) \<in> {?v <..< vec1 b}" using k(3-) unfolding v v' content_eq_0_1 not_le by(auto simp add:Cart_simps)
  1.3416 +              ultimately have "vec1 ((b + ?v$1)/2) \<in> interior k \<inter> interior k'" unfolding interior_open[OF open_interval] by auto
  1.3417 +              hence *:"k = k'" apply- apply(rule ccontr) using p(5)[OF k(1-2)] by auto
  1.3418 +              { assume "x\<in>k" thus "x\<in>k'" unfolding * . } { assume "x\<in>k'" thus "x\<in>k" unfolding * . }
  1.3419 +            qed
  1.3420 +
  1.3421 +            let ?a = a and ?b = b (* a is something else while proofing the next theorem. *)
  1.3422 +            show "\<forall>x. x \<in> ?B a \<longrightarrow> norm ((\<lambda>(x, k). content k *\<^sub>R f' (x$1) - (f ((interval_upperbound k)$1) - f ((interval_lowerbound k)$1))) x)
  1.3423 +              \<le> e * (b - a) / 4" apply safe unfolding fst_conv snd_conv apply safe unfolding vec1_dest_vec1
  1.3424 +            proof- case goal1 guess v using pa[OF goal1(1)] .. note v = conjunctD2[OF this]
  1.3425 +              have "vec1 ?a\<in>{vec1 ?a..v}" using v(2) by auto hence "dest_vec1 v \<le> ?b" using p(3)[OF goal1(1)] unfolding subset_eq v by auto
  1.3426 +              moreover have "{?a..dest_vec1 v} \<subseteq> ball ?a da" using fineD[OF as(2) goal1(1)]
  1.3427 +                apply-apply(subst(asm) if_P,rule refl) unfolding subset_eq apply safe apply(erule_tac x="vec1 x" in ballE)
  1.3428 +                by(auto simp add:Cart_simps subset_eq dist_real v dist_real_def) ultimately
  1.3429 +              show ?case unfolding v unfolding interval_bounds[OF v(2)[unfolded v vector_le_def]] vec1_dest_vec1 apply-
  1.3430 +                apply(rule da(2)[of "v$1",unfolded vec1_dest_vec1])
  1.3431 +                using goal1 fineD[OF as(2) goal1(1)] unfolding v content_eq_0_1 by auto
  1.3432 +            qed
  1.3433 +            show "\<forall>x. x \<in> ?B b \<longrightarrow> norm ((\<lambda>(x, k). content k *\<^sub>R f' (x$1) - (f ((interval_upperbound k)$1) - f ((interval_lowerbound k)$1))) x)
  1.3434 +              \<le> e * (b - a) / 4" apply safe unfolding fst_conv snd_conv apply safe unfolding vec1_dest_vec1
  1.3435 +            proof- case goal1 guess v using pb[OF goal1(1)] .. note v = conjunctD2[OF this]
  1.3436 +              have "vec1 ?b\<in>{v..vec1 ?b}" using v(2) by auto hence "dest_vec1 v \<ge> ?a" using p(3)[OF goal1(1)] unfolding subset_eq v by auto
  1.3437 +              moreover have "{dest_vec1 v..?b} \<subseteq> ball ?b db" using fineD[OF as(2) goal1(1)]
  1.3438 +                apply-apply(subst(asm) if_P,rule refl) unfolding subset_eq apply safe apply(erule_tac x="vec1 x" in ballE) using ab
  1.3439 +                by(auto simp add:Cart_simps subset_eq dist_real v dist_real_def) ultimately
  1.3440 +              show ?case unfolding v unfolding interval_bounds[OF v(2)[unfolded v vector_le_def]] vec1_dest_vec1 apply-
  1.3441 +                apply(rule db(2)[of "v$1",unfolded vec1_dest_vec1])
  1.3442 +                using goal1 fineD[OF as(2) goal1(1)] unfolding v content_eq_0_1 by auto
  1.3443 +            qed
  1.3444 +          qed(insert p(1) ab e, auto simp add:field_simps) qed auto qed qed qed qed
  1.3445 +
  1.3446 +subsection {* Stronger form with finite number of exceptional points. *}
  1.3447 +
  1.3448 +lemma fundamental_theorem_of_calculus_interior_strong: fixes f::"real \<Rightarrow> 'a::banach"
  1.3449 +  assumes"finite s" "a \<le> b" "continuous_on {a..b} f"
  1.3450 +  "\<forall>x\<in>{a<..<b} - s. (f has_vector_derivative f'(x)) (at x)"
  1.3451 +  shows "((f' o dest_vec1) has_integral (f b - f a)) {vec a..vec b}" using assms apply- 
  1.3452 +proof(induct "card s" arbitrary:s a b)
  1.3453 +  case 0 show ?case apply(rule fundamental_theorem_of_calculus_interior) using 0 by auto
  1.3454 +next case (Suc n) from this(2) guess c s' apply-apply(subst(asm) eq_commute) unfolding card_Suc_eq
  1.3455 +    apply(subst(asm)(2) eq_commute) by(erule exE conjE)+ note cs = this[rule_format]
  1.3456 +  show ?case proof(cases "c\<in>{a<..<b}")
  1.3457 +    case False thus ?thesis apply- apply(rule Suc(1)[OF cs(3) _ Suc(4,5)]) apply safe defer
  1.3458 +      apply(rule Suc(6)[rule_format]) using Suc(3) unfolding cs by auto
  1.3459 +  next have *:"f b - f a = (f c - f a) + (f b - f c)" by auto
  1.3460 +    case True hence "vec1 a \<le> vec1 c" "vec1 c \<le> vec1 b" by auto
  1.3461 +    thus ?thesis apply(subst *) apply(rule has_integral_combine) apply assumption+
  1.3462 +      apply(rule_tac[!] Suc(1)[OF cs(3)]) using Suc(3) unfolding cs
  1.3463 +    proof- show "continuous_on {a..c} f" "continuous_on {c..b} f"
  1.3464 +        apply(rule_tac[!] continuous_on_subset[OF Suc(5)]) using True by auto
  1.3465 +      let ?P = "\<lambda>i j. \<forall>x\<in>{i<..<j} - s'. (f has_vector_derivative f' x) (at x)"
  1.3466 +      show "?P a c" "?P c b" apply safe apply(rule_tac[!] Suc(6)[rule_format]) using True unfolding cs by auto
  1.3467 +    qed auto qed qed
  1.3468 +
  1.3469 +lemma fundamental_theorem_of_calculus_strong: fixes f::"real \<Rightarrow> 'a::banach"
  1.3470 +  assumes "finite s" "a \<le> b" "continuous_on {a..b} f"
  1.3471 +  "\<forall>x\<in>{a..b} - s. (f has_vector_derivative f'(x)) (at x)"
  1.3472 +  shows "((f' o dest_vec1) has_integral (f(b) - f(a))) {vec1 a..vec1 b}"
  1.3473 +  apply(rule fundamental_theorem_of_calculus_interior_strong[OF assms(1-3), of f'])
  1.3474 +  using assms(4) by auto
  1.3475 +
  1.3476 +end