src/HOL/Multivariate_Analysis/Integration.thy
 changeset 36243 027ae62681be parent 36081 70deefb6c093 child 36244 009b0ee1b838
```     1.1 --- a/src/HOL/Multivariate_Analysis/Integration.thy	Fri Apr 09 13:35:54 2010 +0200
1.2 +++ b/src/HOL/Multivariate_Analysis/Integration.thy	Tue Apr 20 14:07:52 2010 +0200
1.3 @@ -8,9 +8,11 @@
1.4  begin
1.5
1.6  declare [[smt_certificates="~~/src/HOL/Multivariate_Analysis/Integration.cert"]]
1.7 -declare [[smt_fixed=true]]
1.8 +declare [[smt_fixed=false]]
1.9  declare [[z3_proofs=true]]
1.10
1.11 +subsection {* Sundries *}
1.12 +
1.13  lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
1.14  lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
1.15  lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
1.16 @@ -18,6 +20,81 @@
1.17
1.18  declare smult_conv_scaleR[simp]
1.19
1.20 +lemma simple_image: "{f x |x . x \<in> s} = f ` s" by blast
1.21 +
1.22 +lemma linear_simps:  assumes "bounded_linear f"
1.23 +  shows "f (a + b) = f a + f b" "f (a - b) = f a - f b" "f 0 = 0" "f (- a) = - f a" "f (s *\<^sub>R v) = s *\<^sub>R (f v)"
1.25 +  using assms unfolding bounded_linear_def additive_def by auto
1.26 +
1.27 +lemma bounded_linearI:assumes "\<And>x y. f (x + y) = f x + f y"
1.28 +  "\<And>r x. f (r *\<^sub>R x) = r *\<^sub>R f x" "\<And>x. norm (f x) \<le> norm x * K"
1.29 +  shows "bounded_linear f"
1.30 +  unfolding bounded_linear_def additive_def bounded_linear_axioms_def using assms by auto
1.31 +
1.32 +lemma real_le_inf_subset:
1.33 +  assumes "t \<noteq> {}" "t \<subseteq> s" "\<exists>b. b <=* s" shows "Inf s <= Inf (t::real set)"
1.34 +  apply(rule isGlb_le_isLb) apply(rule Inf[OF assms(1)])
1.35 +  using assms apply-apply(erule exE) apply(rule_tac x=b in exI)
1.36 +  unfolding isLb_def setge_def by auto
1.37 +
1.38 +lemma real_ge_sup_subset:
1.39 +  assumes "t \<noteq> {}" "t \<subseteq> s" "\<exists>b. s *<= b" shows "Sup s >= Sup (t::real set)"
1.40 +  apply(rule isLub_le_isUb) apply(rule Sup[OF assms(1)])
1.41 +  using assms apply-apply(erule exE) apply(rule_tac x=b in exI)
1.42 +  unfolding isUb_def setle_def by auto
1.43 +
1.44 +lemma dist_trans[simp]:"dist (vec1 x) (vec1 y) = dist x (y::real)"
1.45 +  unfolding dist_real_def dist_vec1 ..
1.46 +
1.47 +lemma Lim_trans[simp]: fixes f::"'a \<Rightarrow> real"
1.48 +  shows "((\<lambda>x. vec1 (f x)) ---> vec1 l) net \<longleftrightarrow> (f ---> l) net"
1.49 +  using assms unfolding Lim dist_trans ..
1.50 +
1.51 +lemma bounded_linear_component[intro]: "bounded_linear (\<lambda>x::real^'n. x \$ k)"
1.52 +  apply(rule bounded_linearI[where K=1])
1.53 +  using component_le_norm[of _ k] unfolding real_norm_def by auto
1.54 +
1.55 +lemma bounded_vec1[intro]:  "bounded s \<Longrightarrow> bounded (vec1 ` (s::real set))"
1.56 +  unfolding bounded_def apply safe apply(rule_tac x="vec1 x" in exI,rule_tac x=e in exI) by auto
1.57 +
1.58 +lemma transitive_stepwise_lt_eq:
1.59 +  assumes "(\<And>x y z::nat. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z)"
1.60 +  shows "((\<forall>m. \<forall>n>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n)))" (is "?l = ?r")
1.61 +proof(safe) assume ?r fix n m::nat assume "m < n" thus "R m n" apply-
1.62 +  proof(induct n arbitrary: m) case (Suc n) show ?case
1.63 +    proof(cases "m < n") case True
1.64 +      show ?thesis apply(rule assms[OF Suc(1)[OF True]]) using `?r` by auto
1.65 +    next case False hence "m = n" using Suc(2) by auto
1.66 +      thus ?thesis using `?r` by auto
1.67 +    qed qed auto qed auto
1.68 +
1.69 +lemma transitive_stepwise_gt:
1.70 +  assumes "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" "\<And>n. R n (Suc n) "
1.71 +  shows "\<forall>n>m. R m n"
1.72 +proof- have "\<forall>m. \<forall>n>m. R m n" apply(subst transitive_stepwise_lt_eq)
1.73 +    apply(rule assms) apply(assumption,assumption) using assms(2) by auto
1.74 +  thus ?thesis by auto qed
1.75 +
1.76 +lemma transitive_stepwise_le_eq:
1.77 +  assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
1.78 +  shows "(\<forall>m. \<forall>n\<ge>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n))" (is "?l = ?r")
1.79 +proof safe assume ?r fix m n::nat assume "m\<le>n" thus "R m n" apply-
1.80 +  proof(induct n arbitrary: m) case (Suc n) show ?case
1.81 +    proof(cases "m \<le> n") case True show ?thesis apply(rule assms(2))
1.82 +        apply(rule Suc(1)[OF True]) using `?r` by auto
1.83 +    next case False hence "m = Suc n" using Suc(2) by auto
1.84 +      thus ?thesis using assms(1) by auto
1.85 +    qed qed(insert assms(1), auto) qed auto
1.86 +
1.87 +lemma transitive_stepwise_le:
1.88 +  assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" "\<And>n. R n (Suc n) "
1.89 +  shows "\<forall>n\<ge>m. R m n"
1.90 +proof- have "\<forall>m. \<forall>n\<ge>m. R m n" apply(subst transitive_stepwise_le_eq)
1.91 +    apply(rule assms) apply(rule assms,assumption,assumption) using assms(3) by auto
1.92 +  thus ?thesis by auto qed
1.93 +
1.94 +
1.95  subsection {* Some useful lemmas about intervals. *}
1.96
1.97  lemma empty_as_interval: "{} = {1..0::real^'n}"
1.98 @@ -1246,6 +1323,10 @@
1.99    apply(drule has_integral_linear,assumption,assumption) unfolding has_integral_integral[THEN sym]
1.100    apply(rule integrable_linear) by assumption+
1.101
1.102 +lemma integral_component_eq[simp]: fixes f::"real^'n \<Rightarrow> real^'m"
1.103 +  assumes "f integrable_on s" shows "integral s (\<lambda>x. f x \$ k) = integral s f \$ k"
1.104 +  using integral_linear[OF assms(1) bounded_linear_component,unfolded o_def] .
1.105 +
1.106  lemma has_integral_setsum:
1.107    assumes "finite t" "\<forall>a\<in>t. ((f a) has_integral (i a)) s"
1.108    shows "((\<lambda>x. setsum (\<lambda>a. f a x) t) has_integral (setsum i t)) s"
1.109 @@ -2225,22 +2306,22 @@
1.110    shows "dest_vec1(integral s f) \<le> dest_vec1(integral s g)"
1.111    apply(rule has_integral_dest_vec1_le) apply(rule_tac[1-2] integrable_integral) using assms by auto
1.112
1.113 -lemma has_integral_component_pos: fixes f::"real^'n \<Rightarrow> real^'m"
1.114 +lemma has_integral_component_nonneg: fixes f::"real^'n \<Rightarrow> real^'m"
1.115    assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> (f x)\$k" shows "0 \<le> i\$k"
1.116    using has_integral_component_le[OF has_integral_0 assms(1)] using assms(2) by auto
1.117
1.118 -lemma integral_component_pos: fixes f::"real^'n \<Rightarrow> real^'m"
1.119 +lemma integral_component_nonneg: fixes f::"real^'n \<Rightarrow> real^'m"
1.120    assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> (f x)\$k" shows "0 \<le> (integral s f)\$k"
1.121 -  apply(rule has_integral_component_pos) using assms by auto
1.122 -
1.123 -lemma has_integral_dest_vec1_pos: fixes f::"real^'n \<Rightarrow> real^1"
1.124 +  apply(rule has_integral_component_nonneg) using assms by auto
1.125 +
1.126 +lemma has_integral_dest_vec1_nonneg: fixes f::"real^'n \<Rightarrow> real^1"
1.127    assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> i"
1.128 -  using has_integral_component_pos[OF assms(1), of 1]
1.129 +  using has_integral_component_nonneg[OF assms(1), of 1]
1.130    using assms(2) unfolding vector_le_def by auto
1.131
1.132 -lemma integral_dest_vec1_pos: fixes f::"real^'n \<Rightarrow> real^1"
1.133 +lemma integral_dest_vec1_nonneg: fixes f::"real^'n \<Rightarrow> real^1"
1.134    assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> integral s f"
1.135 -  apply(rule has_integral_dest_vec1_pos) using assms by auto
1.136 +  apply(rule has_integral_dest_vec1_nonneg) using assms by auto
1.137
1.138  lemma has_integral_component_neg: fixes f::"real^'n \<Rightarrow> real^'m"
1.139    assumes "(f has_integral i) s" "\<forall>x\<in>s. (f x)\$k \<le> 0" shows "i\$k \<le> 0"
1.140 @@ -3779,6 +3860,57 @@
1.141        thus False by auto qed
1.142      thus ?l using y unfolding s by auto qed qed
1.143
1.144 +lemma has_integral_trans[simp]: fixes f::"real^'n \<Rightarrow> real" shows
1.145 +  "((\<lambda>x. vec1 (f x)) has_integral vec1 i) s \<longleftrightarrow> (f has_integral i) s"
1.146 +  unfolding has_integral'[unfolded has_integral]
1.147 +proof case goal1 thus ?case apply safe
1.148 +  apply(erule_tac x=e in allE,safe) apply(rule_tac x=B in exI,safe)
1.149 +  apply(erule_tac x=a in allE, erule_tac x=b in allE,safe)
1.150 +  apply(rule_tac x="dest_vec1 z" in exI,safe) apply(erule_tac x=ea in allE,safe)
1.151 +  apply(rule_tac x=d in exI,safe) apply(erule_tac x=p in allE,safe)
1.152 +  apply(subst(asm)(2) norm_vector_1) unfolding split_def
1.153 +  unfolding setsum_component Cart_nth.diff cond_value_iff[of dest_vec1]
1.154 +    Cart_nth.scaleR vec1_dest_vec1 zero_index apply assumption
1.155 +  apply(subst(asm)(2) norm_vector_1) by auto
1.156 +next case goal2 thus ?case apply safe
1.157 +  apply(erule_tac x=e in allE,safe) apply(rule_tac x=B in exI,safe)
1.158 +  apply(erule_tac x=a in allE, erule_tac x=b in allE,safe)
1.159 +  apply(rule_tac x="vec1 z" in exI,safe) apply(erule_tac x=ea in allE,safe)
1.160 +  apply(rule_tac x=d in exI,safe) apply(erule_tac x=p in allE,safe)
1.161 +  apply(subst norm_vector_1) unfolding split_def
1.162 +  unfolding setsum_component Cart_nth.diff cond_value_iff[of dest_vec1]
1.163 +    Cart_nth.scaleR vec1_dest_vec1 zero_index apply assumption
1.164 +  apply(subst norm_vector_1) by auto qed
1.165 +
1.166 +lemma integral_trans[simp]: assumes "(f::real^'n \<Rightarrow> real) integrable_on s"
1.167 +  shows "integral s (\<lambda>x. vec1 (f x)) = vec1 (integral s f)"
1.168 +  apply(rule integral_unique) using assms by auto
1.169 +
1.170 +lemma integrable_on_trans[simp]: fixes f::"real^'n \<Rightarrow> real" shows
1.171 +  "(\<lambda>x. vec1 (f x)) integrable_on s \<longleftrightarrow> (f integrable_on s)"
1.172 +  unfolding integrable_on_def
1.173 +  apply(subst(2) vec1_dest_vec1(1)[THEN sym]) unfolding has_integral_trans
1.174 +  apply safe defer apply(rule_tac x="vec1 y" in exI) by auto
1.175 +
1.176 +lemma has_integral_le: fixes f::"real^'n \<Rightarrow> real"
1.177 +  assumes "(f has_integral i) s" "(g has_integral j) s"  "\<forall>x\<in>s. (f x) \<le> (g x)"
1.178 +  shows "i \<le> j" using has_integral_component_le[of "vec1 o f" "vec1 i" s "vec1 o g" "vec1 j" 1]
1.179 +  unfolding o_def using assms by auto
1.180 +
1.181 +lemma integral_le: fixes f::"real^'n \<Rightarrow> real"
1.182 +  assumes "f integrable_on s" "g integrable_on s" "\<forall>x\<in>s. f x \<le> g x"
1.183 +  shows "integral s f \<le> integral s g"
1.184 +  using has_integral_le[OF assms(1,2)[unfolded has_integral_integral] assms(3)] .
1.185 +
1.186 +lemma has_integral_nonneg: fixes f::"real^'n \<Rightarrow> real"
1.187 +  assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> i"
1.188 +  using has_integral_component_nonneg[of "vec1 o f" "vec1 i" s 1]
1.189 +  unfolding o_def using assms by auto
1.190 +
1.191 +lemma integral_nonneg: fixes f::"real^'n \<Rightarrow> real"
1.192 +  assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> integral s f"
1.193 +  using has_integral_nonneg[OF assms(1)[unfolded has_integral_integral] assms(2)] .
1.194 +
1.195  subsection {* Hence a general restriction property. *}
1.196
1.197  lemma has_integral_restrict[simp]: assumes "s \<subseteq> t" shows
1.198 @@ -3875,11 +4007,21 @@
1.199    note assms(2-3)[unfolded this] note * = has_integral_component_le[OF this]
1.200    show ?thesis apply(rule *) using assms(1,4) by auto qed
1.201
1.202 +lemma has_integral_subset_le: fixes f::"real^'n \<Rightarrow> real"
1.203 +  assumes "s \<subseteq> t" "(f has_integral i) s" "(f has_integral j) t" "\<forall>x\<in>t. 0 \<le> f(x)"
1.204 +  shows "i \<le> j" using has_integral_subset_component_le[OF assms(1), of "vec1 o f" "vec1 i" "vec1 j" 1]
1.205 +  unfolding o_def using assms by auto
1.206 +
1.207  lemma integral_subset_component_le: fixes f::"real^'n \<Rightarrow> real^'m"
1.208    assumes "s \<subseteq> t" "f integrable_on s" "f integrable_on t" "\<forall>x \<in> t. 0 \<le> f(x)\$k"
1.209    shows "(integral s f)\$k \<le> (integral t f)\$k"
1.210    apply(rule has_integral_subset_component_le) using assms by auto
1.211
1.212 +lemma integral_subset_le: fixes f::"real^'n \<Rightarrow> real"
1.213 +  assumes "s \<subseteq> t" "f integrable_on s" "f integrable_on t" "\<forall>x \<in> t. 0 \<le> f(x)"
1.214 +  shows "(integral s f) \<le> (integral t f)"
1.215 +  apply(rule has_integral_subset_le) using assms by auto
1.216 +
1.217  lemma has_integral_alt': fixes f::"real^'n \<Rightarrow> 'a::banach"
1.218    shows "(f has_integral i) s \<longleftrightarrow> (\<forall>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}) \<and>
1.219    (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow> norm(integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - i) < e)" (is "?l = ?r")
1.220 @@ -3909,6 +4051,1478 @@
1.221        from integral_unique[OF this(1)] show ?case using z(2) by auto qed qed qed
1.222
1.223
1.224 +subsection {* Continuity of the integral (for a 1-dimensional interval). *}
1.225 +
1.226 +lemma integrable_alt: fixes f::"real^'n \<Rightarrow> 'a::banach" shows
1.227 +  "f integrable_on s \<longleftrightarrow>
1.228 +          (\<forall>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}) \<and>
1.229 +          (\<forall>e>0. \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> {a..b} \<and> ball 0 B \<subseteq> {c..d}
1.230 +  \<longrightarrow> norm(integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) -
1.231 +          integral {c..d}  (\<lambda>x. if x \<in> s then f x else 0)) < e)" (is "?l = ?r")
1.232 +proof assume ?l then guess y unfolding integrable_on_def .. note this[unfolded has_integral_alt'[of f]]
1.233 +  note y=conjunctD2[OF this,rule_format] show ?r apply safe apply(rule y)
1.234 +  proof- case goal1 hence "e/2 > 0" by auto from y(2)[OF this] guess B .. note B=conjunctD2[OF this,rule_format]
1.235 +    show ?case apply(rule,rule,rule B)
1.236 +    proof safe case goal1 show ?case apply(rule norm_triangle_half_l)
1.237 +        using B(2)[OF goal1(1)] B(2)[OF goal1(2)] by auto qed qed
1.238 +
1.239 +next assume ?r note as = conjunctD2[OF this,rule_format]
1.240 +  have "Cauchy (\<lambda>n. integral ({(\<chi> i. - real n) .. (\<chi> i. real n)}) (\<lambda>x. if x \<in> s then f x else 0))"
1.241 +  proof(unfold Cauchy_def,safe) case goal1
1.242 +    from as(2)[OF this] guess B .. note B = conjunctD2[OF this,rule_format]
1.243 +    from real_arch_simple[of B] guess N .. note N = this
1.244 +    { fix n assume n:"n \<ge> N" have "ball 0 B \<subseteq> {\<chi> i. - real n..\<chi> i. real n}" apply safe
1.245 +        unfolding mem_ball mem_interval vector_dist_norm
1.246 +      proof case goal1 thus ?case using component_le_norm[of x i]
1.247 +          using n N by(auto simp add:field_simps) qed }
1.248 +    thus ?case apply-apply(rule_tac x=N in exI) apply safe unfolding vector_dist_norm apply(rule B(2)) by auto
1.249 +  qed from this[unfolded convergent_eq_cauchy[THEN sym]] guess i ..
1.250 +  note i = this[unfolded Lim_sequentially, rule_format]
1.251 +
1.252 +  show ?l unfolding integrable_on_def has_integral_alt'[of f] apply(rule_tac x=i in exI)
1.253 +    apply safe apply(rule as(1)[unfolded integrable_on_def])
1.254 +  proof- case goal1 hence *:"e/2 > 0" by auto
1.255 +    from i[OF this] guess N .. note N =this[rule_format]
1.256 +    from as(2)[OF *] guess B .. note B=conjunctD2[OF this,rule_format] let ?B = "max (real N) B"
1.257 +    show ?case apply(rule_tac x="?B" in exI)
1.258 +    proof safe show "0 < ?B" using B(1) by auto
1.259 +      fix a b assume ab:"ball 0 ?B \<subseteq> {a..b::real^'n}"
1.260 +      from real_arch_simple[of ?B] guess n .. note n=this
1.261 +      show "norm (integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - i) < e"
1.262 +        apply(rule norm_triangle_half_l) apply(rule B(2)) defer apply(subst norm_minus_commute)
1.263 +        apply(rule N[unfolded vector_dist_norm, of n])
1.264 +      proof safe show "N \<le> n" using n by auto
1.265 +        fix x::"real^'n" assume x:"x \<in> ball 0 B" hence "x\<in> ball 0 ?B" by auto
1.266 +        thus "x\<in>{a..b}" using ab by blast
1.267 +        show "x\<in>{\<chi> i. - real n..\<chi> i. real n}" using x unfolding mem_interval mem_ball vector_dist_norm apply-
1.268 +        proof case goal1 thus ?case using component_le_norm[of x i]
1.269 +            using n by(auto simp add:field_simps) qed qed qed qed qed
1.270 +
1.271 +lemma integrable_altD: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.272 +  assumes "f integrable_on s"
1.273 +  shows "\<And>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}"
1.274 +  "\<And>e. e>0 \<Longrightarrow> \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> {a..b} \<and> ball 0 B \<subseteq> {c..d}
1.275 +  \<longrightarrow> norm(integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - integral {c..d}  (\<lambda>x. if x \<in> s then f x else 0)) < e"
1.276 +  using assms[unfolded integrable_alt[of f]] by auto
1.277 +
1.278 +lemma integrable_on_subinterval: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.279 +  assumes "f integrable_on s" "{a..b} \<subseteq> s" shows "f integrable_on {a..b}"
1.280 +  apply(rule integrable_eq) defer apply(rule integrable_altD(1)[OF assms(1)])
1.281 +  using assms(2) by auto
1.282 +
1.283 +subsection {* A straddling criterion for integrability. *}
1.284 +
1.285 +lemma integrable_straddle_interval: fixes f::"real^'n \<Rightarrow> real"
1.286 +  assumes "\<forall>e>0. \<exists>g  h i j. (g has_integral i) ({a..b}) \<and> (h has_integral j) ({a..b}) \<and>
1.287 +  norm(i - j) < e \<and> (\<forall>x\<in>{a..b}. (g x) \<le> (f x) \<and> (f x) \<le>(h x))"
1.288 +  shows "f integrable_on {a..b}"
1.289 +proof(subst integrable_cauchy,safe)
1.290 +  case goal1 hence e:"e/3 > 0" by auto note assms[rule_format,OF this]
1.291 +  then guess g h i j apply- by(erule exE conjE)+ note obt = this
1.292 +  from obt(1)[unfolded has_integral[of g], rule_format, OF e] guess d1 .. note d1=conjunctD2[OF this,rule_format]
1.293 +  from obt(2)[unfolded has_integral[of h], rule_format, OF e] guess d2 .. note d2=conjunctD2[OF this,rule_format]
1.294 +  show ?case apply(rule_tac x="\<lambda>x. d1 x \<inter> d2 x" in exI) apply(rule conjI gauge_inter d1 d2)+ unfolding fine_inter
1.295 +  proof safe have **:"\<And>i j g1 g2 h1 h2 f1 f2. g1 - h2 \<le> f1 - f2 \<Longrightarrow> f1 - f2 \<le> h1 - g2 \<Longrightarrow>
1.296 +      abs(i - j) < e / 3 \<Longrightarrow> abs(g2 - i) < e / 3 \<Longrightarrow>  abs(g1 - i) < e / 3 \<Longrightarrow>
1.297 +      abs(h2 - j) < e / 3 \<Longrightarrow> abs(h1 - j) < e / 3 \<Longrightarrow> abs(f1 - f2) < e" using `e>0` by arith
1.298 +    case goal1 note tagged_division_ofD(2-4) note * = this[OF goal1(1)] this[OF goal1(4)]
1.299 +
1.300 +    have "(\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p1. content k *\<^sub>R g x) \<ge> 0"
1.301 +      "0 \<le> (\<Sum>(x, k)\<in>p2. content k *\<^sub>R h x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)"
1.302 +      "(\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R g x) \<ge> 0"
1.303 +      "0 \<le> (\<Sum>(x, k)\<in>p1. content k *\<^sub>R h x) - (\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x)"
1.304 +      unfolding setsum_subtractf[THEN sym] apply- apply(rule_tac[!] setsum_nonneg)
1.305 +      apply safe unfolding real_scaleR_def mult.diff_right[THEN sym]
1.306 +      apply(rule_tac[!] mult_nonneg_nonneg)
1.307 +    proof- fix a b assume ab:"(a,b) \<in> p1"
1.308 +      show "0 \<le> content b" using *(3)[OF ab] apply safe using content_pos_le . thus "0 \<le> content b" .
1.309 +      show "0 \<le> f a - g a" "0 \<le> h a - f a" using *(1-2)[OF ab] using obt(4)[rule_format,of a] by auto
1.310 +    next fix a b assume ab:"(a,b) \<in> p2"
1.311 +      show "0 \<le> content b" using *(6)[OF ab] apply safe using content_pos_le . thus "0 \<le> content b" .
1.312 +      show "0 \<le> f a - g a" "0 \<le> h a - f a" using *(4-5)[OF ab] using obt(4)[rule_format,of a] by auto qed
1.313 +
1.314 +    thus ?case apply- unfolding real_norm_def apply(rule **) defer defer
1.315 +      unfolding real_norm_def[THEN sym] apply(rule obt(3))
1.316 +      apply(rule d1(2)[OF conjI[OF goal1(4,5)]])
1.317 +      apply(rule d1(2)[OF conjI[OF goal1(1,2)]])
1.318 +      apply(rule d2(2)[OF conjI[OF goal1(4,6)]])
1.319 +      apply(rule d2(2)[OF conjI[OF goal1(1,3)]]) by auto qed qed
1.320 +
1.321 +lemma integrable_straddle: fixes f::"real^'n \<Rightarrow> real"
1.322 +  assumes "\<forall>e>0. \<exists>g h i j. (g has_integral i) s \<and> (h has_integral j) s \<and>
1.323 +  norm(i - j) < e \<and> (\<forall>x\<in>s. (g x) \<le>(f x) \<and>(f x) \<le>(h x))"
1.324 +  shows "f integrable_on s"
1.325 +proof- have "\<And>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}"
1.326 +  proof(rule integrable_straddle_interval,safe) case goal1 hence *:"e/4 > 0" by auto
1.327 +    from assms[rule_format,OF this] guess g h i j apply-by(erule exE conjE)+ note obt=this
1.328 +    note obt(1)[unfolded has_integral_alt'[of g]] note conjunctD2[OF this, rule_format]
1.329 +    note g = this(1) and this(2)[OF *] from this(2) guess B1 .. note B1 = conjunctD2[OF this,rule_format]
1.330 +    note obt(2)[unfolded has_integral_alt'[of h]] note conjunctD2[OF this, rule_format]
1.331 +    note h = this(1) and this(2)[OF *] from this(2) guess B2 .. note B2 = conjunctD2[OF this,rule_format]
1.332 +    def c \<equiv> "\<chi> i. min (a\$i) (- (max B1 B2))" and d \<equiv> "\<chi> i. max (b\$i) (max B1 B2)"
1.333 +    have *:"ball 0 B1 \<subseteq> {c..d}" "ball 0 B2 \<subseteq> {c..d}" apply safe unfolding mem_ball mem_interval vector_dist_norm
1.334 +    proof(rule_tac[!] allI)
1.335 +      case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto next
1.336 +      case goal2 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto qed
1.337 +    have **:"\<And>ch cg ag ah::real. norm(ah - ag) \<le> norm(ch - cg) \<Longrightarrow> norm(cg - i) < e / 4 \<Longrightarrow>
1.338 +      norm(ch - j) < e / 4 \<Longrightarrow> norm(ag - ah) < e"
1.339 +      using obt(3) unfolding real_norm_def by arith
1.340 +    show ?case apply(rule_tac x="\<lambda>x. if x \<in> s then g x else 0" in exI)
1.341 +               apply(rule_tac x="\<lambda>x. if x \<in> s then h x else 0" in exI)
1.342 +      apply(rule_tac x="integral {a..b} (\<lambda>x. if x \<in> s then g x else 0)" in exI)
1.343 +      apply(rule_tac x="integral {a..b} (\<lambda>x. if x \<in> s then h x else 0)" in exI)
1.344 +      apply safe apply(rule_tac[1-2] integrable_integral,rule g,rule h)
1.345 +      apply(rule **[OF _ B1(2)[OF *(1)] B2(2)[OF *(2)]])
1.346 +    proof- have *:"\<And>x f g. (if x \<in> s then f x else 0) - (if x \<in> s then g x else 0) =
1.347 +        (if x \<in> s then f x - g x else (0::real))" by auto
1.348 +      note ** = abs_of_nonneg[OF integral_nonneg[OF integrable_sub, OF h g]]
1.349 +      show " norm (integral {a..b} (\<lambda>x. if x \<in> s then h x else 0) -
1.350 +                   integral {a..b} (\<lambda>x. if x \<in> s then g x else 0))
1.351 +           \<le> norm (integral {c..d} (\<lambda>x. if x \<in> s then h x else 0) -
1.352 +                   integral {c..d} (\<lambda>x. if x \<in> s then g x else 0))"
1.353 +        unfolding integral_sub[OF h g,THEN sym] real_norm_def apply(subst **) defer apply(subst **) defer
1.354 +        apply(rule has_integral_subset_le) defer apply(rule integrable_integral integrable_sub h g)+
1.355 +      proof safe fix x assume "x\<in>{a..b}" thus "x\<in>{c..d}" unfolding mem_interval c_def d_def
1.356 +          apply - apply rule apply(erule_tac x=i in allE) by auto
1.357 +      qed(insert obt(4), auto) qed(insert obt(4), auto) qed note interv = this
1.358 +
1.359 +  show ?thesis unfolding integrable_alt[of f] apply safe apply(rule interv)
1.360 +  proof- case goal1 hence *:"e/3 > 0" by auto
1.361 +    from assms[rule_format,OF this] guess g h i j apply-by(erule exE conjE)+ note obt=this
1.362 +    note obt(1)[unfolded has_integral_alt'[of g]] note conjunctD2[OF this, rule_format]
1.363 +    note g = this(1) and this(2)[OF *] from this(2) guess B1 .. note B1 = conjunctD2[OF this,rule_format]
1.364 +    note obt(2)[unfolded has_integral_alt'[of h]] note conjunctD2[OF this, rule_format]
1.365 +    note h = this(1) and this(2)[OF *] from this(2) guess B2 .. note B2 = conjunctD2[OF this,rule_format]
1.366 +    show ?case apply(rule_tac x="max B1 B2" in exI) apply safe apply(rule min_max.less_supI1,rule B1)
1.367 +    proof- fix a b c d::"real^'n" assume as:"ball 0 (max B1 B2) \<subseteq> {a..b}" "ball 0 (max B1 B2) \<subseteq> {c..d}"
1.368 +      have **:"ball 0 B1 \<subseteq> ball (0::real^'n) (max B1 B2)" "ball 0 B2 \<subseteq> ball (0::real^'n) (max B1 B2)" by auto
1.369 +      have *:"\<And>ga gc ha hc fa fc::real. abs(ga - i) < e / 3 \<and> abs(gc - i) < e / 3 \<and> abs(ha - j) < e / 3 \<and>
1.370 +        abs(hc - j) < e / 3 \<and> abs(i - j) < e / 3 \<and> ga \<le> fa \<and> fa \<le> ha \<and> gc \<le> fc \<and> fc \<le> hc\<Longrightarrow> abs(fa - fc) < e" by smt
1.371 +      show "norm (integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - integral {c..d} (\<lambda>x. if x \<in> s then f x else 0)) < e"
1.372 +        unfolding real_norm_def apply(rule *, safe) unfolding real_norm_def[THEN sym]
1.373 +        apply(rule B1(2),rule order_trans,rule **,rule as(1))
1.374 +        apply(rule B1(2),rule order_trans,rule **,rule as(2))
1.375 +        apply(rule B2(2),rule order_trans,rule **,rule as(1))
1.376 +        apply(rule B2(2),rule order_trans,rule **,rule as(2))
1.377 +        apply(rule obt) apply(rule_tac[!] integral_le) using obt
1.378 +        by(auto intro!: h g interv) qed qed qed
1.379 +
1.380 +subsection {* Adding integrals over several sets. *}
1.381 +
1.382 +lemma has_integral_union: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.383 +  assumes "(f has_integral i) s" "(f has_integral j) t" "negligible(s \<inter> t)"
1.384 +  shows "(f has_integral (i + j)) (s \<union> t)"
1.385 +proof- note * = has_integral_restrict_univ[THEN sym, of f]
1.386 +  show ?thesis unfolding * apply(rule has_integral_spike[OF assms(3)])
1.387 +    defer apply(rule has_integral_add[OF assms(1-2)[unfolded *]]) by auto qed
1.388 +
1.389 +lemma has_integral_unions: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.390 +  assumes "finite t" "\<forall>s\<in>t. (f has_integral (i s)) s"  "\<forall>s\<in>t. \<forall>s'\<in>t. ~(s = s') \<longrightarrow> negligible(s \<inter> s')"
1.391 +  shows "(f has_integral (setsum i t)) (\<Union>t)"
1.392 +proof- note * = has_integral_restrict_univ[THEN sym, of f]
1.393 +  have **:"negligible (\<Union>((\<lambda>(a,b). a \<inter> b) ` {(a,b). a \<in> t \<and> b \<in> {y. y \<in> t \<and> ~(a = y)}}))"
1.394 +    apply(rule negligible_unions) apply(rule finite_imageI) apply(rule finite_subset[of _ "t \<times> t"]) defer
1.395 +    apply(rule finite_cartesian_product[OF assms(1,1)]) using assms(3) by auto
1.396 +  note assms(2)[unfolded *] note has_integral_setsum[OF assms(1) this]
1.397 +  thus ?thesis unfolding * apply-apply(rule has_integral_spike[OF **]) defer apply assumption
1.398 +  proof safe case goal1 thus ?case
1.399 +    proof(cases "x\<in>\<Union>t") case True then guess s unfolding Union_iff .. note s=this
1.400 +      hence *:"\<forall>b\<in>t. x \<in> b \<longleftrightarrow> b = s" using goal1(3) by blast
1.401 +      show ?thesis unfolding if_P[OF True] apply(rule trans) defer
1.402 +        apply(rule setsum_cong2) apply(subst *, assumption) apply(rule refl)
1.403 +        unfolding setsum_delta[OF assms(1)] using s by auto qed auto qed qed
1.404 +
1.405 +subsection {* In particular adding integrals over a division, maybe not of an interval. *}
1.406 +
1.407 +lemma has_integral_combine_division: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.408 +  assumes "d division_of s" "\<forall>k\<in>d. (f has_integral (i k)) k"
1.409 +  shows "(f has_integral (setsum i d)) s"
1.410 +proof- note d = division_ofD[OF assms(1)]
1.411 +  show ?thesis unfolding d(6)[THEN sym] apply(rule has_integral_unions)
1.412 +    apply(rule d assms)+ apply(rule,rule,rule)
1.413 +  proof- case goal1 from d(4)[OF this(1)] d(4)[OF this(2)]
1.414 +    guess a c b d apply-by(erule exE)+ note obt=this
1.415 +    from d(5)[OF goal1] show ?case unfolding obt interior_closed_interval
1.416 +      apply-apply(rule negligible_subset[of "({a..b}-{a<..<b}) \<union> ({c..d}-{c<..<d})"])
1.417 +      apply(rule negligible_union negligible_frontier_interval)+ by auto qed qed
1.418 +
1.419 +lemma integral_combine_division_bottomup: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.420 +  assumes "d division_of s" "\<forall>k\<in>d. f integrable_on k"
1.421 +  shows "integral s f = setsum (\<lambda>i. integral i f) d"
1.422 +  apply(rule integral_unique) apply(rule has_integral_combine_division[OF assms(1)])
1.423 +  using assms(2) unfolding has_integral_integral .
1.424 +
1.425 +lemma has_integral_combine_division_topdown: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.426 +  assumes "f integrable_on s" "d division_of k" "k \<subseteq> s"
1.427 +  shows "(f has_integral (setsum (\<lambda>i. integral i f) d)) k"
1.428 +  apply(rule has_integral_combine_division[OF assms(2)])
1.429 +  apply safe unfolding has_integral_integral[THEN sym]
1.430 +proof- case goal1 from division_ofD(2,4)[OF assms(2) this]
1.431 +  show ?case apply safe apply(rule integrable_on_subinterval)
1.432 +    apply(rule assms) using assms(3) by auto qed
1.433 +
1.434 +lemma integral_combine_division_topdown: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.435 +  assumes "f integrable_on s" "d division_of s"
1.436 +  shows "integral s f = setsum (\<lambda>i. integral i f) d"
1.437 +  apply(rule integral_unique,rule has_integral_combine_division_topdown) using assms by auto
1.438 +
1.439 +lemma integrable_combine_division: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.440 +  assumes "d division_of s" "\<forall>i\<in>d. f integrable_on i"
1.441 +  shows "f integrable_on s"
1.442 +  using assms(2) unfolding integrable_on_def
1.443 +  by(metis has_integral_combine_division[OF assms(1)])
1.444 +
1.445 +lemma integrable_on_subdivision: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.446 +  assumes "d division_of i" "f integrable_on s" "i \<subseteq> s"
1.447 +  shows "f integrable_on i"
1.448 +  apply(rule integrable_combine_division assms)+
1.449 +proof safe case goal1 note division_ofD(2,4)[OF assms(1) this]
1.450 +  thus ?case apply safe apply(rule integrable_on_subinterval[OF assms(2)])
1.451 +    using assms(3) by auto qed
1.452 +
1.453 +subsection {* Also tagged divisions. *}
1.454 +
1.455 +lemma has_integral_combine_tagged_division: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.456 +  assumes "p tagged_division_of s" "\<forall>(x,k) \<in> p. (f has_integral (i k)) k"
1.457 +  shows "(f has_integral (setsum (\<lambda>(x,k). i k) p)) s"
1.458 +proof- have *:"(f has_integral (setsum (\<lambda>k. integral k f) (snd ` p))) s"
1.459 +    apply(rule has_integral_combine_division) apply(rule division_of_tagged_division[OF assms(1)])
1.460 +    using assms(2) unfolding has_integral_integral[THEN sym] by(safe,auto)
1.461 +  thus ?thesis apply- apply(rule subst[where P="\<lambda>i. (f has_integral i) s"]) defer apply assumption
1.462 +    apply(rule trans[of _ "setsum (\<lambda>(x,k). integral k f) p"]) apply(subst eq_commute)
1.463 +    apply(rule setsum_over_tagged_division_lemma[OF assms(1)]) apply(rule integral_null,assumption)
1.464 +    apply(rule setsum_cong2) using assms(2) by auto qed
1.465 +
1.466 +lemma integral_combine_tagged_division_bottomup: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.467 +  assumes "p tagged_division_of {a..b}" "\<forall>(x,k)\<in>p. f integrable_on k"
1.468 +  shows "integral {a..b} f = setsum (\<lambda>(x,k). integral k f) p"
1.469 +  apply(rule integral_unique) apply(rule has_integral_combine_tagged_division[OF assms(1)])
1.470 +  using assms(2) by auto
1.471 +
1.472 +lemma has_integral_combine_tagged_division_topdown: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.473 +  assumes "f integrable_on {a..b}" "p tagged_division_of {a..b}"
1.474 +  shows "(f has_integral (setsum (\<lambda>(x,k). integral k f) p)) {a..b}"
1.475 +  apply(rule has_integral_combine_tagged_division[OF assms(2)])
1.476 +proof safe case goal1 note tagged_division_ofD(3-4)[OF assms(2) this]
1.477 +  thus ?case using integrable_subinterval[OF assms(1)] by auto qed
1.478 +
1.479 +lemma integral_combine_tagged_division_topdown: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.480 +  assumes "f integrable_on {a..b}" "p tagged_division_of {a..b}"
1.481 +  shows "integral {a..b} f = setsum (\<lambda>(x,k). integral k f) p"
1.482 +  apply(rule integral_unique,rule has_integral_combine_tagged_division_topdown) using assms by auto
1.483 +
1.484 +subsection {* Henstock's lemma. *}
1.485 +
1.486 +lemma henstock_lemma_part1: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.487 +  assumes "f integrable_on {a..b}" "0 < e" "gauge d"
1.488 +  "(\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - integral({a..b}) f) < e)"
1.489 +  and p:"p tagged_partial_division_of {a..b}" "d fine p"
1.490 +  shows "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x - integral k f) p) \<le> e" (is "?x \<le> e")
1.491 +proof-  { presume "\<And>k. 0<k \<Longrightarrow> ?x \<le> e + k" thus ?thesis by arith }
1.492 +  fix k::real assume k:"k>0" note p' = tagged_partial_division_ofD[OF p(1)]
1.493 +  have "\<Union>snd ` p \<subseteq> {a..b}" using p'(3) by fastsimp
1.494 +  note partial_division_of_tagged_division[OF p(1)] this
1.495 +  from partial_division_extend_interval[OF this] guess q . note q=this and q' = division_ofD[OF this(2)]
1.496 +  def r \<equiv> "q - snd ` p" have "snd ` p \<inter> r = {}" unfolding r_def by auto
1.497 +  have r:"finite r" using q' unfolding r_def by auto
1.498 +
1.499 +  have "\<forall>i\<in>r. \<exists>p. p tagged_division_of i \<and> d fine p \<and>
1.500 +    norm(setsum (\<lambda>(x,j). content j *\<^sub>R f x) p - integral i f) < k / (real (card r) + 1)"
1.501 +  proof safe case goal1 hence i:"i \<in> q" unfolding r_def by auto
1.502 +    from q'(4)[OF this] guess u v apply-by(erule exE)+ note uv=this
1.503 +    have *:"k / (real (card r) + 1) > 0" apply(rule divide_pos_pos,rule k) by auto
1.504 +    have "f integrable_on {u..v}" apply(rule integrable_subinterval[OF assms(1)])
1.505 +      using q'(2)[OF i] unfolding uv by auto
1.506 +    note integrable_integral[OF this, unfolded has_integral[of f]]
1.507 +    from this[rule_format,OF *] guess dd .. note dd=conjunctD2[OF this,rule_format]
1.508 +    note gauge_inter[OF `gauge d` dd(1)] from fine_division_exists[OF this,of u v] guess qq .
1.509 +    thus ?case apply(rule_tac x=qq in exI) using dd(2)[of qq] unfolding fine_inter uv by auto qed
1.510 +  from bchoice[OF this] guess qq .. note qq=this[rule_format]
1.511 +
1.512 +  let ?p = "p \<union> \<Union>qq ` r" have "norm ((\<Sum>(x, k)\<in>?p. content k *\<^sub>R f x) - integral {a..b} f) < e"
1.513 +    apply(rule assms(4)[rule_format])
1.514 +  proof show "d fine ?p" apply(rule fine_union,rule p) apply(rule fine_unions) using qq by auto
1.515 +    note * = tagged_partial_division_of_union_self[OF p(1)]
1.516 +    have "p \<union> \<Union>qq ` r tagged_division_of \<Union>snd ` p \<union> \<Union>r"
1.517 +    proof(rule tagged_division_union[OF * tagged_division_unions])
1.518 +      show "finite r" by fact case goal2 thus ?case using qq by auto
1.519 +    next case goal3 thus ?case apply(rule,rule,rule) apply(rule q'(5)) unfolding r_def by auto
1.520 +    next case goal4 thus ?case apply(rule inter_interior_unions_intervals) apply(fact,rule)
1.521 +        apply(rule,rule q') defer apply(rule,subst Int_commute)
1.522 +        apply(rule inter_interior_unions_intervals) apply(rule finite_imageI,rule p',rule) defer
1.523 +        apply(rule,rule q') using q(1) p' unfolding r_def by auto qed
1.524 +    moreover have "\<Union>snd ` p \<union> \<Union>r = {a..b}" "{qq i |i. i \<in> r} = qq ` r"
1.525 +      unfolding Union_Un_distrib[THEN sym] r_def using q by auto
1.526 +    ultimately show "?p tagged_division_of {a..b}" by fastsimp qed
1.527 +
1.528 +  hence "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>\<Union>qq ` r. content k *\<^sub>R f x) -
1.529 +    integral {a..b} f) < e" apply(subst setsum_Un_zero[THEN sym]) apply(rule p') prefer 3
1.530 +    apply assumption apply rule apply(rule finite_imageI,rule r) apply safe apply(drule qq)
1.531 +  proof- fix x l k assume as:"(x,l)\<in>p" "(x,l)\<in>qq k" "k\<in>r"
1.532 +    note qq[OF this(3)] note tagged_division_ofD(3,4)[OF conjunct1[OF this] as(2)]
1.533 +    from this(2) guess u v apply-by(erule exE)+ note uv=this
1.534 +    have "l\<in>snd ` p" unfolding image_iff apply(rule_tac x="(x,l)" in bexI) using as by auto
1.535 +    hence "l\<in>q" "k\<in>q" "l\<noteq>k" using as(1,3) q(1) unfolding r_def by auto
1.536 +    note q'(5)[OF this] hence "interior l = {}" using subset_interior[OF `l \<subseteq> k`] by blast
1.537 +    thus "content l *\<^sub>R f x = 0" unfolding uv content_eq_0_interior[THEN sym] by auto qed auto
1.538 +
1.539 +  hence "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + setsum (setsum (\<lambda>(x, k). content k *\<^sub>R f x))
1.540 +    (qq ` r) - integral {a..b} f) < e" apply(subst(asm) setsum_UNION_zero)
1.541 +    prefer 4 apply assumption apply(rule finite_imageI,fact)
1.542 +    unfolding split_paired_all split_conv image_iff defer apply(erule bexE)+
1.543 +  proof- fix x m k l T1 T2 assume "(x,m)\<in>T1" "(x,m)\<in>T2" "T1\<noteq>T2" "k\<in>r" "l\<in>r" "T1 = qq k" "T2 = qq l"
1.544 +    note as = this(1-5)[unfolded this(6-)] note kl = tagged_division_ofD(3,4)[OF qq[THEN conjunct1]]
1.545 +    from this(2)[OF as(4,1)] guess u v apply-by(erule exE)+ note uv=this
1.546 +    have *:"interior (k \<inter> l) = {}" unfolding interior_inter apply(rule q')
1.547 +      using as unfolding r_def by auto
1.548 +    have "interior m = {}" unfolding subset_empty[THEN sym] unfolding *[THEN sym]
1.549 +      apply(rule subset_interior) using kl(1)[OF as(4,1)] kl(1)[OF as(5,2)] by auto
1.550 +    thus "content m *\<^sub>R f x = 0" unfolding uv content_eq_0_interior[THEN sym] by auto
1.551 +  qed(insert qq, auto)
1.552 +
1.553 +  hence **:"norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + setsum (setsum (\<lambda>(x, k). content k *\<^sub>R f x) \<circ> qq) r -
1.554 +    integral {a..b} f) < e" apply(subst(asm) setsum_reindex_nonzero) apply fact
1.555 +    apply(rule setsum_0',rule) unfolding split_paired_all split_conv defer apply assumption
1.556 +  proof- fix k l x m assume as:"k\<in>r" "l\<in>r" "k\<noteq>l" "qq k = qq l" "(x,m)\<in>qq k"
1.557 +    note tagged_division_ofD(6)[OF qq[THEN conjunct1]] from this[OF as(1)] this[OF as(2)]
1.558 +    show "content m *\<^sub>R f x = 0"  using as(3) unfolding as by auto qed
1.559 +
1.560 +  have *:"\<And>ir ip i cr cp. norm((cp + cr) - i) < e \<Longrightarrow> norm(cr - ir) < k \<Longrightarrow>
1.561 +    ip + ir = i \<Longrightarrow> norm(cp - ip) \<le> e + k"
1.562 +  proof- case goal1 thus ?case  using norm_triangle_le[of "cp + cr - i" "- (cr - ir)"]
1.563 +      unfolding goal1(3)[THEN sym] norm_minus_cancel by(auto simp add:group_simps) qed
1.564 +
1.565 +  have "?x =  norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p. integral k f))"
1.566 +    unfolding split_def setsum_subtractf ..
1.567 +  also have "... \<le> e + k" apply(rule *[OF **, where ir="setsum (\<lambda>k. integral k f) r"])
1.568 +  proof- case goal2 have *:"(\<Sum>(x, k)\<in>p. integral k f) = (\<Sum>k\<in>snd ` p. integral k f)"
1.569 +      apply(subst setsum_reindex_nonzero) apply fact
1.570 +      unfolding split_paired_all snd_conv split_def o_def
1.571 +    proof- fix x l y m assume as:"(x,l)\<in>p" "(y,m)\<in>p" "(x,l)\<noteq>(y,m)" "l = m"
1.572 +      from p'(4)[OF as(1)] guess u v apply-by(erule exE)+ note uv=this
1.573 +      show "integral l f = 0" unfolding uv apply(rule integral_unique)
1.574 +        apply(rule has_integral_null) unfolding content_eq_0_interior
1.575 +        using p'(5)[OF as(1-3)] unfolding uv as(4)[THEN sym] by auto
1.576 +    qed auto
1.577 +    show ?case unfolding integral_combine_division_topdown[OF assms(1) q(2)] * r_def
1.578 +      apply(rule setsum_Un_disjoint'[THEN sym]) using q(1) q'(1) p'(1) by auto
1.579 +  next  case goal1 have *:"k * real (card r) / (1 + real (card r)) < k" using k by(auto simp add:field_simps)
1.580 +    show ?case apply(rule le_less_trans[of _ "setsum (\<lambda>x. k / (real (card r) + 1)) r"])
1.581 +      unfolding setsum_subtractf[THEN sym] apply(rule setsum_norm_le,fact)
1.582 +      apply rule apply(drule qq) defer unfolding real_divide_def setsum_left_distrib[THEN sym]
1.583 +      unfolding real_divide_def[THEN sym] using * by(auto simp add:field_simps real_eq_of_nat)
1.584 +  qed finally show "?x \<le> e + k" . qed
1.585 +
1.586 +lemma henstock_lemma_part2: fixes f::"real^'m \<Rightarrow> real^'n"
1.587 +  assumes "f integrable_on {a..b}" "0 < e" "gauge d"
1.588 +  "\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p -
1.589 +          integral({a..b}) f) < e"    "p tagged_partial_division_of {a..b}" "d fine p"
1.590 +  shows "setsum (\<lambda>(x,k). norm(content k *\<^sub>R f x - integral k f)) p \<le> 2 * real (CARD('n)) * e"
1.591 +  unfolding split_def apply(rule vsum_norm_allsubsets_bound) defer
1.592 +  apply(rule henstock_lemma_part1[unfolded split_def,OF assms(1-3)])
1.593 +  apply safe apply(rule assms[rule_format,unfolded split_def]) defer
1.594 +  apply(rule tagged_partial_division_subset,rule assms,assumption)
1.595 +  apply(rule fine_subset,assumption,rule assms) using assms(5) by auto
1.596 +
1.597 +lemma henstock_lemma: fixes f::"real^'m \<Rightarrow> real^'n"
1.598 +  assumes "f integrable_on {a..b}" "e>0"
1.599 +  obtains d where "gauge d"
1.600 +  "\<forall>p. p tagged_partial_division_of {a..b} \<and> d fine p
1.601 +  \<longrightarrow> setsum (\<lambda>(x,k). norm(content k *\<^sub>R f x - integral k f)) p < e"
1.602 +proof- have *:"e / (2 * (real CARD('n) + 1)) > 0" apply(rule divide_pos_pos) using assms(2) by auto
1.603 +  from integrable_integral[OF assms(1),unfolded has_integral[of f],rule_format,OF this]
1.604 +  guess d .. note d = conjunctD2[OF this] show thesis apply(rule that,rule d)
1.605 +  proof safe case goal1 note * = henstock_lemma_part2[OF assms(1) * d this]
1.606 +    show ?case apply(rule le_less_trans[OF *]) using `e>0` by(auto simp add:field_simps) qed qed
1.607 +
1.608 +subsection {* monotone convergence (bounded interval first). *}
1.609 +
1.610 +lemma monotone_convergence_interval: fixes f::"nat \<Rightarrow> real^'n \<Rightarrow> real^1"
1.611 +  assumes "\<forall>k. (f k) integrable_on {a..b}"
1.612 +  "\<forall>k. \<forall>x\<in>{a..b}. dest_vec1(f k x) \<le> dest_vec1(f (Suc k) x)"
1.613 +  "\<forall>x\<in>{a..b}. ((\<lambda>k. f k x) ---> g x) sequentially"
1.614 +  "bounded {integral {a..b} (f k) | k . k \<in> UNIV}"
1.615 +  shows "g integrable_on {a..b} \<and> ((\<lambda>k. integral ({a..b}) (f k)) ---> integral ({a..b}) g) sequentially"
1.616 +proof(case_tac[!] "content {a..b} = 0") assume as:"content {a..b} = 0"
1.617 +  show ?thesis using integrable_on_null[OF as] unfolding integral_null[OF as] using Lim_const by auto
1.618 +next assume ab:"content {a..b} \<noteq> 0"
1.619 +  have fg:"\<forall>x\<in>{a..b}. \<forall> k. (f k x)\$1 \<le> (g x)\$1"
1.620 +  proof safe case goal1 note assms(3)[rule_format,OF this]
1.621 +    note * = Lim_component_ge[OF this trivial_limit_sequentially]
1.622 +    show ?case apply(rule *) unfolding eventually_sequentially
1.623 +      apply(rule_tac x=k in exI) apply- apply(rule transitive_stepwise_le)
1.624 +      using assms(2)[rule_format,OF goal1] by auto qed
1.625 +  have "\<exists>i. ((\<lambda>k. integral ({a..b}) (f k)) ---> i) sequentially"
1.626 +    apply(rule bounded_increasing_convergent) defer
1.627 +    apply rule apply(rule integral_component_le) apply safe
1.628 +    apply(rule assms(1-2)[rule_format])+ using assms(4) by auto
1.629 +  then guess i .. note i=this
1.630 +  have i':"\<And>k. dest_vec1(integral({a..b}) (f k)) \<le> dest_vec1 i"
1.631 +    apply(rule Lim_component_ge,rule i) apply(rule trivial_limit_sequentially)
1.632 +    unfolding eventually_sequentially apply(rule_tac x=k in exI)
1.633 +    apply(rule transitive_stepwise_le) prefer 3 apply(rule integral_dest_vec1_le)
1.634 +    apply(rule assms(1-2)[rule_format])+ using assms(2) by auto
1.635 +
1.636 +  have "(g has_integral i) {a..b}" unfolding has_integral
1.637 +  proof safe case goal1 note e=this
1.638 +    hence "\<forall>k. (\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow>
1.639 +             norm ((\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f k x) - integral {a..b} (f k)) < e / 2 ^ (k + 2)))"
1.640 +      apply-apply(rule,rule assms(1)[unfolded has_integral_integral has_integral,rule_format])
1.641 +      apply(rule divide_pos_pos) by auto
1.642 +    from choice[OF this] guess c .. note c=conjunctD2[OF this[rule_format],rule_format]
1.643 +
1.644 +    have "\<exists>r. \<forall>k\<ge>r. 0 \<le> i\$1 - dest_vec1(integral {a..b} (f k)) \<and>
1.645 +                   i\$1 - dest_vec1(integral {a..b} (f k)) < e / 4"
1.646 +    proof- case goal1 have "e/4 > 0" using e by auto
1.647 +      from i[unfolded Lim_sequentially,rule_format,OF this] guess r ..
1.648 +      thus ?case apply(rule_tac x=r in exI) apply rule
1.649 +        apply(erule_tac x=k in allE)
1.650 +      proof- case goal1 thus ?case using i'[of k] unfolding dist_real by auto qed qed
1.651 +    then guess r .. note r=conjunctD2[OF this[rule_format]]
1.652 +
1.653 +    have "\<forall>x\<in>{a..b}. \<exists>n\<ge>r. \<forall>k\<ge>n. 0 \<le> (g x)\$1 - (f k x)\$1 \<and>
1.654 +           (g x)\$1 - (f k x)\$1 < e / (4 * content({a..b}))"
1.655 +    proof case goal1 have "e / (4 * content {a..b}) > 0" apply(rule divide_pos_pos,fact)
1.656 +        using ab content_pos_le[of a b] by auto
1.657 +      from assms(3)[rule_format,OF goal1,unfolded Lim_sequentially,rule_format,OF this]
1.658 +      guess n .. note n=this
1.659 +      thus ?case apply(rule_tac x="n + r" in exI) apply safe apply(erule_tac[2-3] x=k in allE)
1.660 +        unfolding dist_real using fg[rule_format,OF goal1] by(auto simp add:field_simps) qed
1.661 +    from bchoice[OF this] guess m .. note m=conjunctD2[OF this[rule_format],rule_format]
1.662 +    def d \<equiv> "\<lambda>x. c (m x) x"
1.663 +
1.664 +    show ?case apply(rule_tac x=d in exI)
1.665 +    proof safe show "gauge d" using c(1) unfolding gauge_def d_def by auto
1.666 +    next fix p assume p:"p tagged_division_of {a..b}" "d fine p"
1.667 +      note p'=tagged_division_ofD[OF p(1)]
1.668 +      have "\<exists>a. \<forall>x\<in>p. m (fst x) \<le> a" by(rule upper_bound_finite_set,fact)
1.669 +      then guess s .. note s=this
1.670 +      have *:"\<forall>a b c d. norm(a - b) \<le> e / 4 \<and> norm(b - c) < e / 2 \<and>
1.671 +            norm(c - d) < e / 4 \<longrightarrow> norm(a - d) < e"
1.672 +      proof safe case goal1 thus ?case using norm_triangle_lt[of "a - b" "b - c" "3* e/4"]
1.673 +          norm_triangle_lt[of "a - b + (b - c)" "c - d" e] unfolding norm_minus_cancel
1.674 +          by(auto simp add:group_simps) qed
1.675 +      show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - i) < e" apply(rule *[rule_format,where
1.676 +          b="\<Sum>(x, k)\<in>p. content k *\<^sub>R f (m x) x" and c="\<Sum>(x, k)\<in>p. integral k (f (m x))"])
1.677 +      proof safe case goal1
1.678 +         show ?case apply(rule order_trans[of _ "\<Sum>(x, k)\<in>p. content k * (e / (4 * content {a..b}))"])
1.679 +           unfolding setsum_subtractf[THEN sym] apply(rule order_trans,rule setsum_norm[OF p'(1)])
1.680 +           apply(rule setsum_mono) unfolding split_paired_all split_conv
1.681 +           unfolding split_def setsum_left_distrib[THEN sym] scaleR.diff_right[THEN sym]
1.682 +           unfolding additive_content_tagged_division[OF p(1), unfolded split_def]
1.683 +         proof- fix x k assume xk:"(x,k) \<in> p" hence x:"x\<in>{a..b}" using p'(2-3)[OF xk] by auto
1.684 +           from p'(4)[OF xk] guess u v apply-by(erule exE)+ note uv=this
1.685 +           show " norm (content k *\<^sub>R (g x - f (m x) x)) \<le> content k * (e / (4 * content {a..b}))"
1.686 +             unfolding norm_scaleR uv unfolding abs_of_nonneg[OF content_pos_le]
1.687 +             apply(rule mult_left_mono) unfolding norm_real using m(2)[OF x,of "m x"] by auto
1.688 +         qed(insert ab,auto)
1.689 +
1.690 +       next case goal2 show ?case apply(rule le_less_trans[of _ "norm (\<Sum>j = 0..s.
1.691 +           \<Sum>(x, k)\<in>{xk\<in>p. m (fst xk) = j}. content k *\<^sub>R f (m x) x - integral k (f (m x)))"])
1.692 +           apply(subst setsum_group) apply fact apply(rule finite_atLeastAtMost) defer
1.693 +           apply(subst split_def)+ unfolding setsum_subtractf apply rule
1.694 +         proof- show "norm (\<Sum>j = 0..s. \<Sum>(x, k)\<in>{xk \<in> p.
1.695 +             m (fst xk) = j}. content k *\<^sub>R f (m x) x - integral k (f (m x))) < e / 2"
1.696 +             apply(rule le_less_trans[of _ "setsum (\<lambda>i. e / 2^(i+2)) {0..s}"])
1.697 +             apply(rule setsum_norm_le[OF finite_atLeastAtMost])
1.698 +           proof show "(\<Sum>i = 0..s. e / 2 ^ (i + 2)) < e / 2"
1.699 +               unfolding power_add real_divide_def inverse_mult_distrib
1.700 +               unfolding setsum_right_distrib[THEN sym] setsum_left_distrib[THEN sym]
1.701 +               unfolding power_inverse sum_gp apply(rule mult_strict_left_mono[OF _ e])
1.702 +               unfolding power2_eq_square by auto
1.703 +             fix t assume "t\<in>{0..s}"
1.704 +             show "norm (\<Sum>(x, k)\<in>{xk \<in> p. m (fst xk) = t}. content k *\<^sub>R f (m x) x -
1.705 +               integral k (f (m x))) \<le> e / 2 ^ (t + 2)"apply(rule order_trans[of _
1.706 +               "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f t x - integral k (f t)) {xk \<in> p. m (fst xk) = t})"])
1.707 +               apply(rule eq_refl) apply(rule arg_cong[where f=norm]) apply(rule setsum_cong2) defer
1.708 +               apply(rule henstock_lemma_part1) apply(rule assms(1)[rule_format])
1.709 +               apply(rule divide_pos_pos,rule e) defer  apply safe apply(rule c)+
1.710 +               apply rule apply assumption+ apply(rule tagged_partial_division_subset[of p])
1.711 +               apply(rule p(1)[unfolded tagged_division_of_def,THEN conjunct1]) defer
1.712 +               unfolding fine_def apply safe apply(drule p(2)[unfolded fine_def,rule_format])
1.713 +               unfolding d_def by auto qed
1.714 +         qed(insert s, auto)
1.715 +
1.716 +       next case goal3
1.717 +         note comb = integral_combine_tagged_division_topdown[OF assms(1)[rule_format] p(1)]
1.718 +         have *:"\<And>sr sx ss ks kr::real^1. kr = sr \<longrightarrow> ks = ss \<longrightarrow> ks \<le> i \<and> sr \<le> sx \<and> sx \<le> ss \<and> 0 \<le> i\$1 - kr\$1
1.719 +           \<and> i\$1 - kr\$1 < e/4 \<longrightarrow> abs(sx\$1 - i\$1) < e/4" unfolding Cart_eq forall_1 vector_le_def by arith
1.720 +         show ?case unfolding norm_real Cart_nth.diff apply(rule *[rule_format],safe)
1.721 +           apply(rule comb[of r],rule comb[of s]) unfolding vector_le_def forall_1 setsum_component
1.722 +           apply(rule i') apply(rule_tac[1-2] setsum_mono) unfolding split_paired_all split_conv
1.723 +           apply(rule_tac[1-2] integral_component_le[OF ])
1.724 +         proof safe show "0 \<le> i\$1 - (integral {a..b} (f r))\$1" using r(1) by auto
1.725 +           show "i\$1 - (integral {a..b} (f r))\$1 < e / 4" using r(2) by auto
1.726 +           fix x k assume xk:"(x,k)\<in>p" from p'(4)[OF this] guess u v apply-by(erule exE)+ note uv=this
1.727 +           show "f r integrable_on k" "f s integrable_on k" "f (m x) integrable_on k" "f (m x) integrable_on k"
1.728 +             unfolding uv apply(rule_tac[!] integrable_on_subinterval[OF assms(1)[rule_format]])
1.729 +             using p'(3)[OF xk] unfolding uv by auto
1.730 +           fix y assume "y\<in>k" hence "y\<in>{a..b}" using p'(3)[OF xk] by auto
1.731 +           hence *:"\<And>m. \<forall>n\<ge>m. (f m y)\$1 \<le> (f n y)\$1" apply-apply(rule transitive_stepwise_le) using assms(2) by auto
1.732 +           show "(f r y)\$1 \<le> (f (m x) y)\$1" "(f (m x) y)\$1 \<le> (f s y)\$1"
1.733 +             apply(rule_tac[!] *[rule_format]) using s[rule_format,OF xk] m(1)[of x] p'(2-3)[OF xk] by auto
1.734 +         qed qed qed qed note * = this
1.735 +
1.736 +   have "integral {a..b} g = i" apply(rule integral_unique) using * .
1.737 +   thus ?thesis using i * by auto qed
1.738 +
1.739 +lemma monotone_convergence_increasing: fixes f::"nat \<Rightarrow> real^'n \<Rightarrow> real^1"
1.740 +  assumes "\<forall>k. (f k) integrable_on s"  "\<forall>k. \<forall>x\<in>s. (f k x)\$1 \<le> (f (Suc k) x)\$1"
1.741 +  "\<forall>x\<in>s. ((\<lambda>k. f k x) ---> g x) sequentially" "bounded {integral s (f k)| k. True}"
1.742 +  shows "g integrable_on s \<and> ((\<lambda>k. integral s (f k)) ---> integral s g) sequentially"
1.743 +proof- have lem:"\<And>f::nat \<Rightarrow> real^'n \<Rightarrow> real^1. \<And> g s. \<forall>k.\<forall>x\<in>s. 0\<le>dest_vec1 (f k x) \<Longrightarrow> \<forall>k. (f k) integrable_on s \<Longrightarrow>
1.744 +    \<forall>k. \<forall>x\<in>s. (f k x)\$1 \<le> (f (Suc k) x)\$1 \<Longrightarrow> \<forall>x\<in>s. ((\<lambda>k. f k x) ---> g x) sequentially  \<Longrightarrow>
1.745 +    bounded {integral s (f k)| k. True} \<Longrightarrow> g integrable_on s \<and> ((\<lambda>k. integral s (f k)) ---> integral s g) sequentially"
1.746 +  proof- case goal1 note assms=this[rule_format]
1.747 +    have "\<forall>x\<in>s. \<forall>k. dest_vec1(f k x) \<le> dest_vec1(g x)" apply safe apply(rule Lim_component_ge)
1.748 +      apply(rule goal1(4)[rule_format],assumption) apply(rule trivial_limit_sequentially)
1.749 +      unfolding eventually_sequentially apply(rule_tac x=k in exI)
1.750 +      apply(rule transitive_stepwise_le) using goal1(3) by auto note fg=this[rule_format]
1.751 +
1.752 +    have "\<exists>i. ((\<lambda>k. integral s (f k)) ---> i) sequentially" apply(rule bounded_increasing_convergent)
1.753 +      apply(rule goal1(5)) apply(rule,rule integral_component_le) apply(rule goal1(2)[rule_format])+
1.754 +      using goal1(3) by auto then guess i .. note i=this
1.755 +    have "\<And>k. \<forall>x\<in>s. \<forall>n\<ge>k. f k x \<le> f n x" apply(rule,rule transitive_stepwise_le) using goal1(3) by auto
1.756 +    hence i':"\<forall>k. (integral s (f k))\$1 \<le> i\$1" apply-apply(rule,rule Lim_component_ge)
1.757 +      apply(rule i,rule trivial_limit_sequentially) unfolding eventually_sequentially
1.758 +      apply(rule_tac x=k in exI,safe) apply(rule integral_dest_vec1_le)
1.759 +      apply(rule goal1(2)[rule_format])+ by auto
1.760 +
1.761 +    note int = assms(2)[unfolded integrable_alt[of _ s],THEN conjunct1,rule_format]
1.762 +    have ifif:"\<And>k t. (\<lambda>x. if x \<in> t then if x \<in> s then f k x else 0 else 0) =
1.763 +      (\<lambda>x. if x \<in> t\<inter>s then f k x else 0)" apply(rule ext) by auto
1.764 +    have int':"\<And>k a b. f k integrable_on {a..b} \<inter> s" apply(subst integrable_restrict_univ[THEN sym])
1.765 +      apply(subst ifif[THEN sym]) apply(subst integrable_restrict_univ) using int .
1.766 +    have "\<And>a b. (\<lambda>x. if x \<in> s then g x else 0) integrable_on {a..b} \<and>
1.767 +      ((\<lambda>k. integral {a..b} (\<lambda>x. if x \<in> s then f k x else 0)) --->
1.768 +      integral {a..b} (\<lambda>x. if x \<in> s then g x else 0)) sequentially"
1.769 +    proof(rule monotone_convergence_interval,safe)
1.770 +      case goal1 show ?case using int .
1.771 +    next case goal2 thus ?case apply-apply(cases "x\<in>s") using assms(3) by auto
1.772 +    next case goal3 thus ?case apply-apply(cases "x\<in>s") using assms(4) by auto
1.773 +    next case goal4 note * = integral_dest_vec1_nonneg[unfolded vector_le_def forall_1 zero_index]
1.774 +      have "\<And>k. norm (integral {a..b} (\<lambda>x. if x \<in> s then f k x else 0)) \<le> norm (integral s (f k))"
1.775 +        unfolding norm_real apply(subst abs_of_nonneg) apply(rule *[OF int])
1.776 +        apply(safe,case_tac "x\<in>s") apply(drule assms(1)) prefer 3
1.777 +        apply(subst abs_of_nonneg) apply(rule *[OF assms(2) goal1(1)[THEN spec]])
1.778 +        apply(subst integral_restrict_univ[THEN sym,OF int])
1.779 +        unfolding ifif unfolding integral_restrict_univ[OF int']
1.780 +        apply(rule integral_subset_component_le[OF _ int' assms(2)]) using assms(1) by auto
1.781 +      thus ?case using assms(5) unfolding bounded_iff apply safe
1.782 +        apply(rule_tac x=aa in exI,safe) apply(erule_tac x="integral s (f k)" in ballE)
1.783 +        apply(rule order_trans) apply assumption by auto qed note g = conjunctD2[OF this]
1.784 +
1.785 +    have "(g has_integral i) s" unfolding has_integral_alt' apply safe apply(rule g(1))
1.786 +    proof- case goal1 hence "e/4>0" by auto
1.787 +      from i[unfolded Lim_sequentially,rule_format,OF this] guess N .. note N=this
1.788 +      note assms(2)[of N,unfolded has_integral_integral has_integral_alt'[of "f N"]]
1.789 +      from this[THEN conjunct2,rule_format,OF `e/4>0`] guess B .. note B=conjunctD2[OF this]
1.790 +      show ?case apply(rule,rule,rule B,safe)
1.791 +      proof- fix a b::"real^'n" assume ab:"ball 0 B \<subseteq> {a..b}"
1.792 +        from `e>0` have "e/2>0" by auto
1.793 +        from g(2)[unfolded Lim_sequentially,of a b,rule_format,OF this] guess M .. note M=this
1.794 +        have **:"norm (integral {a..b} (\<lambda>x. if x \<in> s then f N x else 0) - i) < e/2"
1.795 +          apply(rule norm_triangle_half_l) using B(2)[rule_format,OF ab] N[rule_format,of N]
1.796 +          unfolding vector_dist_norm apply-defer apply(subst norm_minus_commute) by auto
1.797 +        have *:"\<And>f1 f2 g. abs(f1 - i\$1) < e / 2 \<longrightarrow> abs(f2 - g) < e / 2 \<longrightarrow> f1 \<le> f2 \<longrightarrow> f2 \<le> i\$1
1.798 +          \<longrightarrow> abs(g - i\$1) < e" by arith
1.799 +        show "norm (integral {a..b} (\<lambda>x. if x \<in> s then g x else 0) - i) < e"
1.800 +          unfolding norm_real Cart_simps apply(rule *[rule_format])
1.801 +          apply(rule **[unfolded norm_real Cart_simps])
1.802 +          apply(rule M[rule_format,of "M + N",unfolded dist_real]) apply(rule le_add1)
1.803 +          apply(rule integral_component_le[OF int int]) defer
1.804 +          apply(rule order_trans[OF _ i'[rule_format,of "M + N"]])
1.805 +        proof safe case goal2 have "\<And>m. x\<in>s \<Longrightarrow> \<forall>n\<ge>m. (f m x)\$1 \<le> (f n x)\$1"
1.806 +            apply(rule transitive_stepwise_le) using assms(3) by auto thus ?case by auto
1.807 +        next case goal1 show ?case apply(subst integral_restrict_univ[THEN sym,OF int])
1.808 +            unfolding ifif integral_restrict_univ[OF int']
1.809 +            apply(rule integral_subset_component_le[OF _ int']) using assms by auto
1.810 +        qed qed qed
1.811 +    thus ?case apply safe defer apply(drule integral_unique) using i by auto qed
1.812 +
1.813 +  have sub:"\<And>k. integral s (\<lambda>x. f k x - f 0 x) = integral s (f k) - integral s (f 0)"
1.814 +    apply(subst integral_sub) apply(rule assms(1)[rule_format])+ by rule
1.815 +  have "\<And>x m. x\<in>s \<Longrightarrow> \<forall>n\<ge>m. dest_vec1 (f m x) \<le> dest_vec1 (f n x)" apply(rule transitive_stepwise_le)
1.816 +    using assms(2) by auto note * = this[rule_format]
1.817 +  have "(\<lambda>x. g x - f 0 x) integrable_on s \<and>((\<lambda>k. integral s (\<lambda>x. f (Suc k) x - f 0 x)) --->
1.818 +      integral s (\<lambda>x. g x - f 0 x)) sequentially" apply(rule lem,safe)
1.819 +  proof- case goal1 thus ?case using *[of x 0 "Suc k"] by auto
1.820 +  next case goal2 thus ?case apply(rule integrable_sub) using assms(1) by auto
1.821 +  next case goal3 thus ?case using *[of x "Suc k" "Suc (Suc k)"] by auto
1.822 +  next case goal4 thus ?case apply-apply(rule Lim_sub)
1.823 +      using seq_offset[OF assms(3)[rule_format],of x 1] by auto
1.824 +  next case goal5 thus ?case using assms(4) unfolding bounded_iff
1.825 +      apply safe apply(rule_tac x="a + norm (integral s (\<lambda>x. f 0 x))" in exI)
1.826 +      apply safe apply(erule_tac x="integral s (\<lambda>x. f (Suc k) x)" in ballE) unfolding sub
1.827 +      apply(rule order_trans[OF norm_triangle_ineq4]) by auto qed
1.828 +  note conjunctD2[OF this] note Lim_add[OF this(2) Lim_const[of "integral s (f 0)"]]
1.829 +    integrable_add[OF this(1) assms(1)[rule_format,of 0]]
1.830 +  thus ?thesis unfolding sub apply-apply rule defer apply(subst(asm) integral_sub)
1.831 +    using assms(1) apply auto apply(rule seq_offset_rev[where k=1]) by auto qed
1.832 +
1.833 +lemma monotone_convergence_decreasing: fixes f::"nat \<Rightarrow> real^'n \<Rightarrow> real^1"
1.834 +  assumes "\<forall>k. (f k) integrable_on s"  "\<forall>k. \<forall>x\<in>s. (f (Suc k) x)\$1 \<le> (f k x)\$1"
1.835 +  "\<forall>x\<in>s. ((\<lambda>k. f k x) ---> g x) sequentially" "bounded {integral s (f k)| k. True}"
1.836 +  shows "g integrable_on s \<and> ((\<lambda>k. integral s (f k)) ---> integral s g) sequentially"
1.837 +proof- note assm = assms[rule_format]
1.838 +  have *:"{integral s (\<lambda>x. - f k x) |k. True} = op *\<^sub>R -1 ` {integral s (f k)| k. True}"
1.839 +    apply safe unfolding image_iff apply(rule_tac x="integral s (f k)" in bexI) prefer 3
1.840 +    apply(rule_tac x=k in exI) unfolding integral_neg[OF assm(1)] by auto
1.841 +  have "(\<lambda>x. - g x) integrable_on s \<and> ((\<lambda>k. integral s (\<lambda>x. - f k x))
1.842 +    ---> integral s (\<lambda>x. - g x))  sequentially" apply(rule monotone_convergence_increasing)
1.843 +    apply(safe,rule integrable_neg) apply(rule assm) defer apply(rule Lim_neg)
1.844 +    apply(rule assm,assumption) unfolding * apply(rule bounded_scaling) using assm by auto
1.845 +  note * = conjunctD2[OF this]
1.846 +  show ?thesis apply rule using integrable_neg[OF *(1)] defer
1.847 +    using Lim_neg[OF *(2)] apply- unfolding integral_neg[OF assm(1)]
1.848 +    unfolding integral_neg[OF *(1),THEN sym] by auto qed
1.849 +
1.850 +lemma monotone_convergence_increasing_real: fixes f::"nat \<Rightarrow> real^'n \<Rightarrow> real"
1.851 +  assumes "\<forall>k. (f k) integrable_on s"  "\<forall>k. \<forall>x\<in>s. (f (Suc k) x) \<ge> (f k x)"
1.852 +  "\<forall>x\<in>s. ((\<lambda>k. f k x) ---> g x) sequentially" "bounded {integral s (f k)| k. True}"
1.853 +  shows "g integrable_on s \<and> ((\<lambda>k. integral s (f k)) ---> integral s g) sequentially"
1.854 +proof- have *:"{integral s (\<lambda>x. vec1 (f k x)) |k. True} =  vec1 ` {integral s (f k) |k. True}"
1.855 +    unfolding integral_trans[OF assms(1)[rule_format]] by auto
1.856 +  have "vec1 \<circ> g integrable_on s \<and> ((\<lambda>k. integral s (vec1 \<circ> f k)) ---> integral s (vec1 \<circ> g)) sequentially"
1.857 +    apply(rule monotone_convergence_increasing) unfolding o_def integrable_on_trans
1.858 +    unfolding vec1_dest_vec1 apply(rule assms)+ unfolding Lim_trans unfolding * using assms(3,4) by auto
1.859 +  thus ?thesis unfolding o_def unfolding integral_trans[OF assms(1)[rule_format]] by auto qed
1.860 +
1.861 +lemma monotone_convergence_decreasing_real: fixes f::"nat \<Rightarrow> real^'n \<Rightarrow> real"
1.862 +  assumes "\<forall>k. (f k) integrable_on s"  "\<forall>k. \<forall>x\<in>s. (f (Suc k) x) \<le> (f k x)"
1.863 +  "\<forall>x\<in>s. ((\<lambda>k. f k x) ---> g x) sequentially" "bounded {integral s (f k)| k. True}"
1.864 +  shows "g integrable_on s \<and> ((\<lambda>k. integral s (f k)) ---> integral s g) sequentially"
1.865 +proof- have *:"{integral s (\<lambda>x. vec1 (f k x)) |k. True} =  vec1 ` {integral s (f k) |k. True}"
1.866 +    unfolding integral_trans[OF assms(1)[rule_format]] by auto
1.867 +  have "vec1 \<circ> g integrable_on s \<and> ((\<lambda>k. integral s (vec1 \<circ> f k)) ---> integral s (vec1 \<circ> g)) sequentially"
1.868 +    apply(rule monotone_convergence_decreasing) unfolding o_def integrable_on_trans
1.869 +    unfolding vec1_dest_vec1 apply(rule assms)+ unfolding Lim_trans unfolding * using assms(3,4) by auto
1.870 +  thus ?thesis unfolding o_def unfolding integral_trans[OF assms(1)[rule_format]] by auto qed
1.871 +
1.872 +subsection {* absolute integrability (this is the same as Lebesgue integrability). *}
1.873 +
1.874 +definition absolutely_integrable_on (infixr "absolutely'_integrable'_on" 46) where
1.875 +  "f absolutely_integrable_on s \<longleftrightarrow> f integrable_on s \<and> (\<lambda>x. (norm(f x))) integrable_on s"
1.876 +
1.877 +lemma absolutely_integrable_onI[intro?]:
1.878 +  "f integrable_on s \<Longrightarrow> (\<lambda>x. (norm(f x))) integrable_on s \<Longrightarrow> f absolutely_integrable_on s"
1.879 +  unfolding absolutely_integrable_on_def by auto
1.880 +
1.881 +lemma absolutely_integrable_onD[dest]: assumes "f absolutely_integrable_on s"
1.882 +  shows "f integrable_on s" "(\<lambda>x. (norm(f x))) integrable_on s"
1.883 +  using assms unfolding absolutely_integrable_on_def by auto
1.884 +
1.885 +lemma absolutely_integrable_on_trans[simp]: fixes f::"real^'n \<Rightarrow> real" shows
1.886 +  "(vec1 o f) absolutely_integrable_on s \<longleftrightarrow> f absolutely_integrable_on s"
1.887 +  unfolding absolutely_integrable_on_def o_def by auto
1.888 +
1.889 +lemma integral_norm_bound_integral: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.890 +  assumes "f integrable_on s" "g integrable_on s" "\<forall>x\<in>s. norm(f x) \<le> g x"
1.891 +  shows "norm(integral s f) \<le> (integral s g)"
1.892 +proof- have *:"\<And>x y. (\<forall>e::real. 0 < e \<longrightarrow> x < y + e) \<longrightarrow> x \<le> y" apply(safe,rule ccontr)
1.893 +    apply(erule_tac x="x - y" in allE) by auto
1.894 +  have "\<And>e sg dsa dia ig. norm(sg) \<le> dsa \<longrightarrow> abs(dsa - dia) < e / 2 \<longrightarrow> norm(sg - ig) < e / 2
1.895 +    \<longrightarrow> norm(ig) < dia + e"
1.896 +  proof safe case goal1 show ?case apply(rule le_less_trans[OF norm_triangle_sub[of ig sg]])
1.897 +      apply(subst real_sum_of_halves[of e,THEN sym]) unfolding class_semiring.add_a
1.898 +      apply(rule add_le_less_mono) defer apply(subst norm_minus_commute,rule goal1)
1.899 +      apply(rule order_trans[OF goal1(1)]) using goal1(2) by arith
1.900 +  qed note norm=this[rule_format]
1.901 +  have lem:"\<And>f::real^'n \<Rightarrow> 'a. \<And> g a b. f integrable_on {a..b} \<Longrightarrow> g integrable_on {a..b} \<Longrightarrow>
1.902 +    \<forall>x\<in>{a..b}. norm(f x) \<le> (g x) \<Longrightarrow> norm(integral({a..b}) f) \<le> (integral({a..b}) g)"
1.903 +  proof(rule *[rule_format]) case goal1 hence *:"e/2>0" by auto
1.904 +    from integrable_integral[OF goal1(1),unfolded has_integral[of f],rule_format,OF *]
1.905 +    guess d1 .. note d1 = conjunctD2[OF this,rule_format]
1.906 +    from integrable_integral[OF goal1(2),unfolded has_integral[of g],rule_format,OF *]
1.907 +    guess d2 .. note d2 = conjunctD2[OF this,rule_format]
1.908 +    note gauge_inter[OF d1(1) d2(1)]
1.909 +    from fine_division_exists[OF this, of a b] guess p . note p=this
1.910 +    show ?case apply(rule norm) defer
1.911 +      apply(rule d2(2)[OF conjI[OF p(1)],unfolded real_norm_def]) defer
1.912 +      apply(rule d1(2)[OF conjI[OF p(1)]]) defer apply(rule setsum_norm_le)
1.913 +    proof safe fix x k assume "(x,k)\<in>p" note as = tagged_division_ofD(2-4)[OF p(1) this]
1.914 +      from this(3) guess u v apply-by(erule exE)+ note uv=this
1.915 +      show "norm (content k *\<^sub>R f x) \<le> content k *\<^sub>R g x" unfolding uv norm_scaleR
1.916 +        unfolding abs_of_nonneg[OF content_pos_le] real_scaleR_def
1.917 +        apply(rule mult_left_mono) using goal1(3) as by auto
1.918 +    qed(insert p[unfolded fine_inter],auto) qed
1.919 +
1.920 +  { presume "\<And>e. 0 < e \<Longrightarrow> norm (integral s f) < integral s g + e"
1.921 +    thus ?thesis apply-apply(rule *[rule_format]) by auto }
1.922 +  fix e::real assume "e>0" hence e:"e/2 > 0" by auto
1.923 +  note assms(1)[unfolded integrable_alt[of f]] note f=this[THEN conjunct1,rule_format]
1.924 +  note assms(2)[unfolded integrable_alt[of g]] note g=this[THEN conjunct1,rule_format]
1.925 +  from integrable_integral[OF assms(1),unfolded has_integral'[of f],rule_format,OF e]
1.926 +  guess B1 .. note B1=conjunctD2[OF this[rule_format],rule_format]
1.927 +  from integrable_integral[OF assms(2),unfolded has_integral'[of g],rule_format,OF e]
1.928 +  guess B2 .. note B2=conjunctD2[OF this[rule_format],rule_format]
1.929 +  from bounded_subset_closed_interval[OF bounded_ball, of "0::real^'n" "max B1 B2"]
1.930 +  guess a b apply-by(erule exE)+ note ab=this[unfolded ball_max_Un]
1.931 +
1.932 +  have "ball 0 B1 \<subseteq> {a..b}" using ab by auto
1.933 +  from B1(2)[OF this] guess z .. note z=conjunctD2[OF this]
1.934 +  have "ball 0 B2 \<subseteq> {a..b}" using ab by auto
1.935 +  from B2(2)[OF this] guess w .. note w=conjunctD2[OF this]
1.936 +
1.937 +  show "norm (integral s f) < integral s g + e" apply(rule norm)
1.938 +    apply(rule lem[OF f g, of a b]) unfolding integral_unique[OF z(1)] integral_unique[OF w(1)]
1.939 +    defer apply(rule w(2)[unfolded real_norm_def],rule z(2))
1.940 +    apply safe apply(case_tac "x\<in>s") unfolding if_P apply(rule assms(3)[rule_format]) by auto qed
1.941 +
1.942 +lemma integral_norm_bound_integral_component: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.943 +  assumes "f integrable_on s" "g integrable_on s"  "\<forall>x\<in>s. norm(f x) \<le> (g x)\$k"
1.944 +  shows "norm(integral s f) \<le> (integral s g)\$k"
1.945 +proof- have "norm (integral s f) \<le> integral s ((\<lambda>x. x \$ k) o g)"
1.946 +    apply(rule integral_norm_bound_integral[OF assms(1)])
1.947 +    apply(rule integrable_linear[OF assms(2)],rule)
1.948 +    unfolding o_def by(rule assms)
1.949 +  thus ?thesis unfolding o_def integral_component_eq[OF assms(2)] . qed
1.950 +
1.951 +lemma has_integral_norm_bound_integral_component: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.952 +  assumes "(f has_integral i) s" "(g has_integral j) s" "\<forall>x\<in>s. norm(f x) \<le> (g x)\$k"
1.953 +  shows "norm(i) \<le> j\$k" using integral_norm_bound_integral_component[of f s g k]
1.954 +  unfolding integral_unique[OF assms(1)] integral_unique[OF assms(2)]
1.955 +  using assms by auto
1.956 +
1.957 +lemma absolutely_integrable_le: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.958 +  assumes "f absolutely_integrable_on s"
1.959 +  shows "norm(integral s f) \<le> integral s (\<lambda>x. norm(f x))"
1.960 +  apply(rule integral_norm_bound_integral) using assms by auto
1.961 +
1.962 +lemma absolutely_integrable_0[intro]: "(\<lambda>x. 0) absolutely_integrable_on s"
1.963 +  unfolding absolutely_integrable_on_def by auto
1.964 +
1.965 +lemma absolutely_integrable_cmul[intro]:
1.966 + "f absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. c *\<^sub>R f x) absolutely_integrable_on s"
1.967 +  unfolding absolutely_integrable_on_def using integrable_cmul[of f s c]
1.968 +  using integrable_cmul[of "\<lambda>x. norm (f x)" s "abs c"] by auto
1.969 +
1.970 +lemma absolutely_integrable_neg[intro]:
1.971 + "f absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. -f(x)) absolutely_integrable_on s"
1.972 +  apply(drule absolutely_integrable_cmul[where c="-1"]) by auto
1.973 +
1.974 +lemma absolutely_integrable_norm[intro]:
1.975 + "f absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. norm(f x)) absolutely_integrable_on s"
1.976 +  unfolding absolutely_integrable_on_def by auto
1.977 +
1.978 +lemma absolutely_integrable_abs[intro]:
1.979 + "f absolutely_integrable_on s \<Longrightarrow> (\<lambda>x. abs(f x::real)) absolutely_integrable_on s"
1.980 +  apply(drule absolutely_integrable_norm) unfolding real_norm_def .
1.981 +
1.982 +lemma absolutely_integrable_on_subinterval: fixes f::"real^'n \<Rightarrow> 'a::banach" shows
1.983 +  "f absolutely_integrable_on s \<Longrightarrow> {a..b} \<subseteq> s \<Longrightarrow> f absolutely_integrable_on {a..b}"
1.984 +  unfolding absolutely_integrable_on_def by(meson integrable_on_subinterval)
1.985 +
1.986 +lemma absolutely_integrable_bounded_variation: fixes f::"real^'n \<Rightarrow> 'a::banach"
1.987 +  assumes "f absolutely_integrable_on UNIV"
1.988 +  obtains B where "\<forall>d. d division_of (\<Union>d) \<longrightarrow> setsum (\<lambda>k. norm(integral k f)) d \<le> B"
1.989 +  apply(rule that[of "integral UNIV (\<lambda>x. norm (f x))"])
1.990 +proof safe case goal1 note d = division_ofD[OF this(2)]
1.991 +  have "(\<Sum>k\<in>d. norm (integral k f)) \<le> (\<Sum>i\<in>d. integral i (\<lambda>x. norm (f x)))"
1.992 +    apply(rule setsum_mono,rule absolutely_integrable_le) apply(drule d(4),safe)
1.993 +    apply(rule absolutely_integrable_on_subinterval[OF assms]) by auto
1.994 +  also have "... \<le> integral (\<Union>d) (\<lambda>x. norm (f x))"
1.995 +    apply(subst integral_combine_division_topdown[OF _ goal1(2)])
1.996 +    using integrable_on_subdivision[OF goal1(2)] using assms by auto
1.997 +  also have "... \<le> integral UNIV (\<lambda>x. norm (f x))"
1.998 +    apply(rule integral_subset_le)
1.999 +    using integrable_on_subdivision[OF goal1(2)] using assms by auto
1.1000 +  finally show ?case . qed
1.1001 +
1.1002 +lemma helplemma:
1.1003 +  assumes "setsum (\<lambda>x. norm(f x - g x)) s < e" "finite s"
1.1004 +  shows "abs(setsum (\<lambda>x. norm(f x)) s - setsum (\<lambda>x. norm(g x)) s) < e"
1.1005 +  unfolding setsum_subtractf[THEN sym] apply(rule le_less_trans[OF setsum_abs])
1.1006 +  apply(rule le_less_trans[OF _ assms(1)]) apply(rule setsum_mono)
1.1007 +  using norm_triangle_ineq3 .
1.1008 +
1.1009 +lemma bounded_variation_absolutely_integrable_interval:
1.1010 +  fixes f::"real^'n \<Rightarrow> real^'m" assumes "f integrable_on {a..b}"
1.1011 +  "\<forall>d. d division_of {a..b} \<longrightarrow> setsum (\<lambda>k. norm(integral k f)) d \<le> B"
1.1012 +  shows "f absolutely_integrable_on {a..b}"
1.1013 +proof- let ?S = "(\<lambda>d. setsum (\<lambda>k. norm(integral k f)) d) ` {d. d division_of {a..b} }" def i \<equiv> "Sup ?S"
1.1014 +  have i:"isLub UNIV ?S i" unfolding i_def apply(rule Sup)
1.1015 +    apply(rule elementary_interval) defer apply(rule_tac x=B in exI)
1.1016 +    apply(rule setleI) using assms(2) by auto
1.1017 +  show ?thesis apply(rule,rule assms) apply rule apply(subst has_integral[of _ i])
1.1018 +  proof safe case goal1 hence "i - e / 2 \<notin> Collect (isUb UNIV (setsum (\<lambda>k. norm (integral k f)) `
1.1019 +        {d. d division_of {a..b}}))" using isLub_ubs[OF i,rule_format]
1.1020 +      unfolding setge_def ubs_def by auto
1.1021 +    hence " \<exists>y. y division_of {a..b} \<and> i - e / 2 < (\<Sum>k\<in>y. norm (integral k f))"
1.1022 +      unfolding mem_Collect_eq isUb_def setle_def by simp then guess d .. note d=conjunctD2[OF this]
1.1023 +    note d' = division_ofD[OF this(1)]
1.1024 +
1.1025 +    have "\<forall>x. \<exists>e>0. \<forall>i\<in>d. x \<notin> i \<longrightarrow> ball x e \<inter> i = {}"
1.1026 +    proof case goal1 have "\<exists>da>0. \<forall>xa\<in>\<Union>{i \<in> d. x \<notin> i}. da \<le> dist x xa"
1.1027 +        apply(rule separate_point_closed) apply(rule closed_Union)
1.1028 +        apply(rule finite_subset[OF _ d'(1)]) apply safe apply(drule d'(4)) by auto
1.1029 +      thus ?case apply safe apply(rule_tac x=da in exI,safe)
1.1030 +        apply(erule_tac x=xa in ballE) by auto
1.1031 +    qed from choice[OF this] guess k .. note k=conjunctD2[OF this[rule_format],rule_format]
1.1032 +
1.1033 +    have "e/2 > 0" using goal1 by auto
1.1034 +    from henstock_lemma[OF assms(1) this] guess g . note g=this[rule_format]
1.1035 +    let ?g = "\<lambda>x. g x \<inter> ball x (k x)"
1.1036 +    show ?case apply(rule_tac x="?g" in exI) apply safe
1.1037 +    proof- show "gauge ?g" using g(1) unfolding gauge_def using k(1) by auto
1.1038 +      fix p assume "p tagged_division_of {a..b}" "?g fine p"
1.1039 +      note p = this(1) conjunctD2[OF this(2)[unfolded fine_inter]]
1.1040 +      note p' = tagged_division_ofD[OF p(1)]
1.1041 +      def p' \<equiv> "{(x,k) | x k. \<exists>i l. x \<in> i \<and> i \<in> d \<and> (x,l) \<in> p \<and> k = i \<inter> l}"
1.1042 +      have gp':"g fine p'" using p(2) unfolding p'_def fine_def by auto
1.1043 +      have p'':"p' tagged_division_of {a..b}" apply(rule tagged_division_ofI)
1.1044 +      proof- show "finite p'" apply(rule finite_subset[of _ "(\<lambda>(k,(x,l)). (x,k \<inter> l))
1.1045 +          ` {(k,xl) | k xl. k \<in> d \<and> xl \<in> p}"]) unfolding p'_def
1.1046 +          defer apply(rule finite_imageI,rule finite_product_dependent[OF d'(1) p'(1)])
1.1047 +          apply safe unfolding image_iff apply(rule_tac x="(i,x,l)" in bexI) by auto
1.1048 +        fix x k assume "(x,k)\<in>p'"
1.1049 +        hence "\<exists>i l. x \<in> i \<and> i \<in> d \<and> (x, l) \<in> p \<and> k = i \<inter> l" unfolding p'_def by auto
1.1050 +        then guess i l apply-by(erule exE)+ note il=conjunctD4[OF this]
1.1051 +        show "x\<in>k" "k\<subseteq>{a..b}" using p'(2-3)[OF il(3)] il by auto
1.1052 +        show "\<exists>a b. k = {a..b}" unfolding il using p'(4)[OF il(3)] d'(4)[OF il(2)]
1.1053 +          apply safe unfolding inter_interval by auto
1.1054 +      next fix x1 k1 assume "(x1,k1)\<in>p'"
1.1055 +        hence "\<exists>i l. x1 \<in> i \<and> i \<in> d \<and> (x1, l) \<in> p \<and> k1 = i \<inter> l" unfolding p'_def by auto
1.1056 +        then guess i1 l1 apply-by(erule exE)+ note il1=conjunctD4[OF this]
1.1057 +        fix x2 k2 assume "(x2,k2)\<in>p'"
1.1058 +        hence "\<exists>i l. x2 \<in> i \<and> i \<in> d \<and> (x2, l) \<in> p \<and> k2 = i \<inter> l" unfolding p'_def by auto
1.1059 +        then guess i2 l2 apply-by(erule exE)+ note il2=conjunctD4[OF this]
1.1060 +        assume "(x1, k1) \<noteq> (x2, k2)"
1.1061 +        hence "interior(i1) \<inter> interior(i2) = {} \<or> interior(l1) \<inter> interior(l2) = {}"
1.1062 +          using d'(5)[OF il1(2) il2(2)] p'(5)[OF il1(3) il2(3)] unfolding il1 il2 by auto
1.1063 +        thus "interior k1 \<inter> interior k2 = {}" unfolding il1 il2 by auto
1.1064 +      next have *:"\<forall>(x, X) \<in> p'. X \<subseteq> {a..b}" unfolding p'_def using d' by auto
1.1065 +        show "\<Union>{k. \<exists>x. (x, k) \<in> p'} = {a..b}" apply rule apply(rule Union_least)
1.1066 +          unfolding mem_Collect_eq apply(erule exE) apply(drule *[rule_format]) apply safe
1.1067 +        proof- fix y assume y:"y\<in>{a..b}"
1.1068 +          hence "\<exists>x l. (x, l) \<in> p \<and> y\<in>l" unfolding p'(6)[THEN sym] by auto
1.1069 +          then guess x l apply-by(erule exE)+ note xl=conjunctD2[OF this]
1.1070 +          hence "\<exists>k. k\<in>d \<and> y\<in>k" using y unfolding d'(6)[THEN sym] by auto
1.1071 +          then guess i .. note i = conjunctD2[OF this]
1.1072 +          have "x\<in>i" using fineD[OF p(3) xl(1)] using k(2)[OF i(1), of x] using i(2) xl(2) by auto
1.1073 +          thus "y\<in>\<Union>{k. \<exists>x. (x, k) \<in> p'}" unfolding p'_def Union_iff apply(rule_tac x="i \<inter> l" in bexI)
1.1074 +            defer unfolding mem_Collect_eq apply(rule_tac x=x in exI)+ apply(rule_tac x="i\<inter>l" in exI)
1.1075 +            apply safe apply(rule_tac x=i in exI) apply(rule_tac x=l in exI) using i xl by auto
1.1076 +        qed qed
1.1077 +
1.1078 +      hence "(\<Sum>(x, k)\<in>p'. norm (content k *\<^sub>R f x - integral k f)) < e / 2"
1.1079 +        apply-apply(rule g(2)[rule_format]) unfolding tagged_division_of_def apply safe using gp' .
1.1080 +      hence **:" \<bar>(\<Sum>(x,k)\<in>p'. norm (content k *\<^sub>R f x)) - (\<Sum>(x,k)\<in>p'. norm (integral k f))\<bar> < e / 2"
1.1081 +        unfolding split_def apply(rule helplemma) using p'' by auto
1.1082 +
1.1083 +      have p'alt:"p' = {(x,(i \<inter> l)) | x i l. (x,l) \<in> p \<and> i \<in> d \<and> ~(i \<inter> l = {})}"
1.1084 +      proof safe case goal2
1.1085 +        have "x\<in>i" using fineD[OF p(3) goal2(1)] k(2)[OF goal2(2), of x] goal2(4-) by auto
1.1086 +        hence "(x, i \<inter> l) \<in> p'" unfolding p'_def apply safe
1.1087 +          apply(rule_tac x=x in exI,rule_tac x="i\<inter>l" in exI) apply safe using goal2 by auto
1.1088 +        thus ?case using goal2(3) by auto
1.1089 +      next fix x k assume "(x,k)\<in>p'"
1.1090 +        hence "\<exists>i l. x \<in> i \<and> i \<in> d \<and> (x, l) \<in> p \<and> k = i \<inter> l" unfolding p'_def by auto
1.1091 +        then guess i l apply-by(erule exE)+ note il=conjunctD4[OF this]
1.1092 +        thus "\<exists>y i l. (x, k) = (y, i \<inter> l) \<and> (y, l) \<in> p \<and> i \<in> d \<and> i \<inter> l \<noteq> {}"
1.1093 +          apply(rule_tac x=x in exI,rule_tac x=i in exI,rule_tac x=l in exI)
1.1094 +          using p'(2)[OF il(3)] by auto
1.1095 +      qed
1.1096 +      have sum_p':"(\<Sum>(x, k)\<in>p'. norm (integral k f)) = (\<Sum>k\<in>snd ` p'. norm (integral k f))"
1.1097 +        apply(subst setsum_over_tagged_division_lemma[OF p'',of "\<lambda>k. norm (integral k f)"])
1.1098 +        unfolding norm_eq_zero apply(rule integral_null,assumption) ..
1.1099 +      note snd_p = division_ofD[OF division_of_tagged_division[OF p(1)]]
1.1100 +
1.1101 +      have *:"\<And>sni sni' sf sf'. abs(sf' - sni') < e / 2 \<longrightarrow> i - e / 2 < sni \<and> sni' \<le> i \<and>
1.1102 +        sni \<le> sni' \<and> sf' = sf \<longrightarrow> abs(sf - i) < e" by arith
1.1103 +      show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R norm (f x)) - i) < e"
1.1104 +        unfolding real_norm_def apply(rule *[rule_format,OF **],safe) apply(rule d(2))
1.1105 +      proof- case goal1 show ?case unfolding sum_p'
1.1106 +          apply(rule isLubD2[OF i]) using division_of_tagged_division[OF p''] by auto
1.1107 +      next case goal2 have *:"{k \<inter> l | k l. k \<in> d \<and> l \<in> snd ` p} =
1.1108 +          (\<lambda>(k,l). k \<inter> l) ` {(k,l)|k l. k \<in> d \<and> l \<in> snd ` p}" by auto
1.1109 +        have "(\<Sum>k\<in>d. norm (integral k f)) \<le> (\<Sum>i\<in>d. \<Sum>l\<in>snd ` p. norm (integral (i \<inter> l) f))"
1.1110 +        proof(rule setsum_mono) case goal1 note k=this
1.1111 +          from d'(4)[OF this] guess u v apply-by(erule exE)+ note uv=this
1.1112 +          def d' \<equiv> "{{u..v} \<inter> l |l. l \<in> snd ` p \<and>  ~({u..v} \<inter> l = {})}" note uvab = d'(2)[OF k[unfolded uv]]
1.1113 +          have "d' division_of {u..v}" apply(subst d'_def) apply(rule division_inter_1)
1.1114 +            apply(rule division_of_tagged_division[OF p(1)]) using uvab .
1.1115 +          hence "norm (integral k f) \<le> setsum (\<lambda>k. norm (integral k f)) d'"
1.1116 +            unfolding uv apply(subst integral_combine_division_topdown[of _ _ d'])
1.1117 +            apply(rule integrable_on_subinterval[OF assms(1) uvab]) apply assumption
1.1118 +            apply(rule setsum_norm_le) by auto
1.1119 +          also have "... = (\<Sum>k\<in>{k \<inter> l |l. l \<in> snd ` p}. norm (integral k f))"
1.1120 +            apply(rule setsum_mono_zero_left) apply(subst simple_image)
1.1121 +            apply(rule finite_imageI)+ apply fact unfolding d'_def uv apply blast
1.1122 +          proof case goal1 hence "i \<in> {{u..v} \<inter> l |l. l \<in> snd ` p}" by auto
1.1123 +            from this[unfolded mem_Collect_eq] guess l .. note l=this
1.1124 +            hence "{u..v} \<inter> l = {}" using goal1 by auto thus ?case using l by auto
1.1125 +          qed also have "... = (\<Sum>l\<in>snd ` p. norm (integral (k \<inter> l) f))" unfolding  simple_image
1.1126 +            apply(rule setsum_reindex_nonzero[unfolded o_def])apply(rule finite_imageI,rule p')
1.1127 +          proof- case goal1 have "interior (k \<inter> l) \<subseteq> interior (l \<inter> y)" apply(subst(2) interior_inter)
1.1128 +              apply(rule Int_greatest) defer apply(subst goal1(4)) by auto
1.1129 +            hence *:"interior (k \<inter> l) = {}" using snd_p(5)[OF goal1(1-3)] by auto
1.1130 +            from d'(4)[OF k] snd_p(4)[OF goal1(1)] guess u1 v1 u2 v2 apply-by(erule exE)+ note uv=this
1.1131 +            show ?case using * unfolding uv inter_interval content_eq_0_interior[THEN sym] by auto
1.1132 +          qed finally show ?case .
1.1133 +        qed also have "... = (\<Sum>(i,l)\<in>{(i, l) |i l. i \<in> d \<and> l \<in> snd ` p}. norm (integral (i\<inter>l) f))"
1.1134 +          apply(subst sum_sum_product[THEN sym],fact) using p'(1) by auto
1.1135 +        also have "... = (\<Sum>x\<in>{(i, l) |i l. i \<in> d \<and> l \<in> snd ` p}. norm (integral (split op \<inter> x) f))"
1.1136 +          unfolding split_def ..
1.1137 +        also have "... = (\<Sum>k\<in>{i \<inter> l |i l. i \<in> d \<and> l \<in> snd ` p}. norm (integral k f))"
1.1138 +          unfolding * apply(rule setsum_reindex_nonzero[THEN sym,unfolded o_def])
1.1139 +          apply(rule finite_product_dependent) apply(fact,rule finite_imageI,rule p')
1.1140 +          unfolding split_paired_all mem_Collect_eq split_conv o_def
1.1141 +        proof- note * = division_ofD(4,5)[OF division_of_tagged_division,OF p(1)]
1.1142 +          fix l1 l2 k1 k2 assume as:"(l1, k1) \<noteq> (l2, k2)"  "l1 \<inter> k1 = l2 \<inter> k2"
1.1143 +            "\<exists>i l. (l1, k1) = (i, l) \<and> i \<in> d \<and> l \<in> snd ` p"
1.1144 +            "\<exists>i l. (l2, k2) = (i, l) \<and> i \<in> d \<and> l \<in> snd ` p"
1.1145 +          hence "l1 \<in> d" "k1 \<in> snd ` p" by auto from d'(4)[OF this(1)] *(1)[OF this(2)]
1.1146 +          guess u1 v1 u2 v2 apply-by(erule exE)+ note uv=this
1.1147 +          have "l1 \<noteq> l2 \<or> k1 \<noteq> k2" using as by auto
1.1148 +          hence "(interior(k1) \<inter> interior(k2) = {} \<or> interior(l1) \<inter> interior(l2) = {})" apply-
1.1149 +            apply(erule disjE) apply(rule disjI2) apply(rule d'(5)) prefer 4 apply(rule disjI1)
1.1150 +            apply(rule *) using as by auto
1.1151 +          moreover have "interior(l1 \<inter> k1) = interior(l2 \<inter> k2)" using as(2) by auto
1.1152 +          ultimately have "interior(l1 \<inter> k1) = {}" by auto
1.1153 +          thus "norm (integral (l1 \<inter> k1) f) = 0" unfolding uv inter_interval
1.1154 +            unfolding content_eq_0_interior[THEN sym] by auto
1.1155 +        qed also have "... = (\<Sum>(x, k)\<in>p'. norm (integral k f))" unfolding sum_p'
1.1156 +          apply(rule setsum_mono_zero_right) apply(subst *)
1.1157 +          apply(rule finite_imageI[OF finite_product_dependent]) apply fact
1.1158 +          apply(rule finite_imageI[OF p'(1)]) apply safe
1.1159 +        proof- case goal2 have "ia \<inter> b = {}" using goal2 unfolding p'alt image_iff Bex_def not_ex
1.1160 +            apply(erule_tac x="(a,ia\<inter>b)" in allE) by auto thus ?case by auto
1.1161 +        next case goal1 thus ?case unfolding p'_def apply safe
1.1162 +            apply(rule_tac x=i in exI,rule_tac x=l in exI) unfolding snd_conv image_iff
1.1163 +            apply safe apply(rule_tac x="(a,l)" in bexI) by auto
1.1164 +        qed finally show ?case .
1.1165 +
1.1166 +      next case goal3
1.1167 +        let ?S = "{(x, i \<inter> l) |x i l. (x, l) \<in> p \<and> i \<in> d}"
1.1168 +        have Sigma_alt:"\<And>s t. s \<times> t = {(i, j) |i j. i \<in> s \<and> j \<in> t}" by auto
1.1169 +        have *:"?S = (\<lambda>(xl,i). (fst xl, snd xl \<inter> i)) ` (p \<times> d)" (*{(xl,i)|xl i. xl\<in>p \<and> i\<in>d}"**)
1.1170 +          apply safe unfolding image_iff apply(rule_tac x="((x,l),i)" in bexI) by auto
1.1171 +        note pdfin = finite_cartesian_product[OF p'(1) d'(1)]
1.1172 +        have "(\<Sum>(x, k)\<in>p'. norm (content k *\<^sub>R f x)) = (\<Sum>(x, k)\<in>?S. \<bar>content k\<bar> * norm (f x))"
1.1173 +          unfolding norm_scaleR apply(rule setsum_mono_zero_left)
1.1174 +          apply(subst *, rule finite_imageI) apply fact unfolding p'alt apply blast
1.1175 +          apply safe apply(rule_tac x=x in exI,rule_tac x=i in exI,rule_tac x=l in exI) by auto
1.1176 +        also have "... = (\<Sum>((x,l),i)\<in>p \<times> d. \<bar>content (l \<inter> i)\<bar> * norm (f x))" unfolding *
1.1177 +          apply(subst setsum_reindex_nonzero,fact) unfolding split_paired_all
1.1178 +          unfolding  o_def split_def snd_conv fst_conv mem_Sigma_iff Pair_eq apply(erule_tac conjE)+
1.1179 +        proof- fix x1 l1 k1 x2 l2 k2 assume as:"(x1,l1)\<in>p" "(x2,l2)\<in>p" "k1\<in>d" "k2\<in>d"
1.1180 +            "x1 = x2" "l1 \<inter> k1 = l2 \<inter> k2" "\<not> ((x1 = x2 \<and> l1 = l2) \<and> k1 = k2)"
1.1181 +          from d'(4)[OF as(3)] p'(4)[OF as(1)] guess u1 v1 u2 v2 apply-by(erule exE)+ note uv=this
1.1182 +          from as have "l1 \<noteq> l2 \<or> k1 \<noteq> k2" by auto
1.1183 +          hence "(interior(k1) \<inter> interior(k2) = {} \<or> interior(l1) \<inter> interior(l2) = {})"
1.1184 +            apply-apply(erule disjE) apply(rule disjI2) defer apply(rule disjI1)
1.1185 +            apply(rule d'(5)[OF as(3-4)],assumption) apply(rule p'(5)[OF as(1-2)]) by auto
1.1186 +          moreover have "interior(l1 \<inter> k1) = interior(l2 \<inter> k2)" unfolding  as ..
1.1187 +          ultimately have "interior (l1 \<inter> k1) = {}" by auto
1.1188 +          thus "\<bar>content (l1 \<inter> k1)\<bar> * norm (f x1) = 0" unfolding uv inter_interval
1.1189 +            unfolding content_eq_0_interior[THEN sym] by auto
1.1190 +        qed safe also have "... = (\<Sum>(x, k)\<in>p. content k *\<^sub>R norm (f x))" unfolding Sigma_alt
1.1191 +          apply(subst sum_sum_product[THEN sym]) apply(rule p', rule,rule d')
1.1192 +          apply(rule setsum_cong2) unfolding split_paired_all split_conv
1.1193 +        proof- fix x l assume as:"(x,l)\<in>p"
1.1194 +          note xl = p'(2-4)[OF this] from this(3) guess u v apply-by(erule exE)+ note uv=this
1.1195 +          have "(\<Sum>i\<in>d. \<bar>content (l \<inter> i)\<bar>) = (\<Sum>k\<in>d. content (k \<inter> {u..v}))"
1.1196 +            apply(rule setsum_cong2) apply(drule d'(4),safe) apply(subst Int_commute)
1.1197 +            unfolding inter_interval uv apply(subst abs_of_nonneg) by auto
1.1198 +          also have "... = setsum content {k\<inter>{u..v}| k. k\<in>d}" unfolding simple_image
1.1199 +            apply(rule setsum_reindex_nonzero[unfolded o_def,THEN sym]) apply(rule d')
1.1200 +          proof- case goal1 from d'(4)[OF this(1)] d'(4)[OF this(2)]
1.1201 +            guess u1 v1 u2 v2 apply- by(erule exE)+ note uv=this
1.1202 +            have "{} = interior ((k \<inter> y) \<inter> {u..v})" apply(subst interior_inter)
1.1203 +              using d'(5)[OF goal1(1-3)] by auto
1.1204 +            also have "... = interior (y \<inter> (k \<inter> {u..v}))" by auto
1.1205 +            also have "... = interior (k \<inter> {u..v})" unfolding goal1(4) by auto
1.1206 +            finally show ?case unfolding uv inter_interval content_eq_0_interior ..
1.1207 +          qed also have "... = setsum content {{u..v} \<inter> k |k. k \<in> d \<and> ~({u..v} \<inter> k = {})}"
1.1208 +            apply(rule setsum_mono_zero_right) unfolding simple_image
1.1209 +            apply(rule finite_imageI,rule d') apply blast apply safe
1.1210 +            apply(rule_tac x=k in exI)
1.1211 +          proof- case goal1 from d'(4)[OF this(1)] guess a b apply-by(erule exE)+ note ab=this
1.1212 +            have "interior (k \<inter> {u..v}) \<noteq> {}" using goal1(2)
1.1213 +              unfolding ab inter_interval content_eq_0_interior by auto
1.1214 +            thus ?case using goal1(1) using interior_subset[of "k \<inter> {u..v}"] by auto
1.1215 +          qed finally show "(\<Sum>i\<in>d. \<bar>content (l \<inter> i)\<bar> * norm (f x)) = content l *\<^sub>R norm (f x)"
1.1216 +            unfolding setsum_left_distrib[THEN sym] real_scaleR_def apply -
1.1217 +            apply(subst(asm) additive_content_division[OF division_inter_1[OF d(1)]])
1.1218 +            using xl(2)[unfolded uv] unfolding uv by auto
1.1219 +        qed finally show ?case .
1.1220 +      qed qed qed qed
1.1221 +
1.1222 +lemma bounded_variation_absolutely_integrable:  fixes f::"real^'n \<Rightarrow> real^'m"
1.1223 +  assumes "f integrable_on UNIV" "\<forall>d. d division_of (\<Union>d) \<longrightarrow> setsum (\<lambda>k. norm(integral k f)) d \<le> B"
1.1224 +  shows "f absolutely_integrable_on UNIV"
1.1225 +proof(rule absolutely_integrable_onI,fact,rule)
1.1226 +  let ?S = "(\<lambda>d. setsum (\<lambda>k. norm(integral k f)) d) ` {d. d division_of  (\<Union>d)}" def i \<equiv> "Sup ?S"
1.1227 +  have i:"isLub UNIV ?S i" unfolding i_def apply(rule Sup)
1.1228 +    apply(rule elementary_interval) defer apply(rule_tac x=B in exI)
1.1229 +    apply(rule setleI) using assms(2) by auto
1.1230 +  have f_int:"\<And>a b. f absolutely_integrable_on {a..b}"
1.1231 +    apply(rule bounded_variation_absolutely_integrable_interval[where B=B])
1.1232 +    apply(rule integrable_on_subinterval[OF assms(1)]) defer apply safe
1.1233 +    apply(rule assms(2)[rule_format]) by auto
1.1234 +  show "((\<lambda>x. norm (f x)) has_integral i) UNIV" apply(subst has_integral_alt',safe)
1.1235 +  proof- case goal1 show ?case using f_int[of a b] by auto
1.1236 +  next case goal2 have "\<exists>y\<in>setsum (\<lambda>k. norm (integral k f)) ` {d. d division_of \<Union>d}. \<not> y \<le> i - e"
1.1237 +    proof(rule ccontr) case goal1 hence "i \<le> i - e" apply-
1.1238 +        apply(rule isLub_le_isUb[OF i]) apply(rule isUbI) unfolding setle_def by auto
1.1239 +      thus False using goal2 by auto
1.1240 +    qed then guess K .. note * = this[unfolded image_iff not_le]
1.1241 +    from this(1) guess d .. note this[unfolded mem_Collect_eq]
1.1242 +    note d = this(1) *(2)[unfolded this(2)] note d'=division_ofD[OF this(1)]
1.1243 +    have "bounded (\<Union>d)" by(rule elementary_bounded,fact)
1.1244 +    from this[unfolded bounded_pos] guess K .. note K=conjunctD2[OF this]
1.1245 +    show ?case apply(rule_tac x="K + 1" in exI,safe)
1.1246 +    proof- fix a b assume ab:"ball 0 (K + 1) \<subseteq> {a..b::real^'n}"
1.1247 +      have *:"\<forall>s s1. i - e < s1 \<and> s1 \<le> s \<and> s < i + e \<longrightarrow> abs(s - i) < (e::real)" by arith
1.1248 +      show "norm (integral {a..b} (\<lambda>x. if x \<in> UNIV then norm (f x) else 0) - i) < e"
1.1249 +        unfolding real_norm_def apply(rule *[rule_format],safe) apply(rule d(2))
1.1250 +      proof- case goal1 have "(\<Sum>k\<in>d. norm (integral k f)) \<le> setsum (\<lambda>k. integral k (\<lambda>x. norm (f x))) d"
1.1251 +          apply(rule setsum_mono) apply(rule absolutely_integrable_le)
1.1252 +          apply(drule d'(4),safe) by(rule f_int)
1.1253 +        also have "... = integral (\<Union>d) (\<lambda>x. norm(f x))"
1.1254 +          apply(rule integral_combine_division_bottomup[THEN sym])
1.1255 +          apply(rule d) unfolding forall_in_division[OF d(1)] using f_int by auto
1.1256 +        also have "... \<le> integral {a..b} (\<lambda>x. if x \<in> UNIV then norm (f x) else 0)"
1.1257 +        proof- case goal1 have "\<Union>d \<subseteq> {a..b}" apply rule apply(drule K(2)[rule_format])
1.1258 +            apply(rule ab[unfolded subset_eq,rule_format]) by(auto simp add:dist_norm)
1.1259 +          thus ?case apply- apply(subst if_P,rule) apply(rule integral_subset_le) defer
1.1260 +            apply(rule integrable_on_subdivision[of _ _ _ "{a..b}"])
1.1261 +            apply(rule d) using f_int[of a b] by auto
1.1262 +        qed finally show ?case .
1.1263 +
1.1264 +      next note f = absolutely_integrable_onD[OF f_int[of a b]]
1.1265 +        note * = this(2)[unfolded has_integral_integral has_integral[of "\<lambda>x. norm (f x)"],rule_format]
1.1266 +        have "e/2>0" using `e>0` by auto from *[OF this] guess d1 .. note d1=conjunctD2[OF this]
1.1267 +        from henstock_lemma[OF f(1) `e/2>0`] guess d2 . note d2=this
1.1268 +        from fine_division_exists[OF gauge_inter[OF d1(1) d2(1)], of a b] guess p .
1.1269 +        note p=this(1) conjunctD2[OF this(2)[unfolded fine_inter]]
1.1270 +        have *:"\<And>sf sf' si di. sf' = sf \<longrightarrow> si \<le> i \<longrightarrow> abs(sf - si) < e / 2
1.1271 +          \<longrightarrow> abs(sf' - di) < e / 2 \<longrightarrow> di < i + e" by arith
1.1272 +        show "integral {a..b} (\<lambda>x. if x \<in> UNIV then norm (f x) else 0) < i + e" apply(subst if_P,rule)
1.1273 +        proof(rule *[rule_format])
1.1274 +          show "\<bar>(\<Sum>(x,k)\<in>p. norm (content k *\<^sub>R f x)) - (\<Sum>(x,k)\<in>p. norm (integral k f))\<bar> < e / 2"
1.1275 +            unfolding split_def apply(rule helplemma) using d2(2)[rule_format,of p]
1.1276 +            using p(1,3) unfolding tagged_division_of_def split_def by auto
1.1277 +          show "abs ((\<Sum>(x, k)\<in>p. content k *\<^sub>R norm (f x)) - integral {a..b} (\<lambda>x. norm(f x))) < e / 2"
1.1278 +            using d1(2)[rule_format,OF conjI[OF p(1,2)]] unfolding real_norm_def .
1.1279 +          show "(\<Sum>(x, k)\<in>p. content k *\<^sub>R norm (f x)) = (\<Sum>(x, k)\<in>p. norm (content k *\<^sub>R f x))"
1.1280 +            apply(rule setsum_cong2) unfolding split_paired_all split_conv
1.1281 +            apply(drule tagged_division_ofD(4)[OF p(1)]) unfolding norm_scaleR
1.1282 +            apply(subst abs_of_nonneg) by auto
1.1283 +          show "(\<Sum>(x, k)\<in>p. norm (integral k f)) \<le> i"
1.1284 +            apply(subst setsum_over_tagged_division_lemma[OF p(1)]) defer apply(rule isLubD2[OF i])
1.1285 +            unfolding image_iff apply(rule_tac x="snd ` p" in bexI) unfolding mem_Collect_eq defer
1.1286 +            apply(rule partial_division_of_tagged_division[of _ "{a..b}"])
1.1287 +            using p(1) unfolding tagged_division_of_def by auto
1.1288 +        qed qed qed(insert K,auto) qed qed
1.1289 +
1.1290 +lemma absolutely_integrable_restrict_univ:
1.1291 + "(\<lambda>x. if x \<in> s then f x else (0::'a::banach)) absolutely_integrable_on UNIV \<longleftrightarrow> f absolutely_integrable_on s"
1.1292 +  unfolding absolutely_integrable_on_def if_distrib norm_zero integrable_restrict_univ ..
1.1293 +
1.1294 +lemma absolutely_integrable_add[intro]: fixes f g::"real^'n \<Rightarrow> real^'m"
1.1295 +  assumes "f absolutely_integrable_on s" "g absolutely_integrable_on s"
1.1296 +  shows "(\<lambda>x. f(x) + g(x)) absolutely_integrable_on s"
1.1297 +proof- let ?P = "\<And>f g::real^'n \<Rightarrow> real^'m. f absolutely_integrable_on UNIV \<Longrightarrow>
1.1298 +    g absolutely_integrable_on UNIV \<Longrightarrow> (\<lambda>x. f(x) + g(x)) absolutely_integrable_on UNIV"
1.1299 +  { presume as:"PROP ?P" note a = absolutely_integrable_restrict_univ[THEN sym]
1.1300 +    have *:"\<And>x. (if x \<in> s then f x else 0) + (if x \<in> s then g x else 0)
1.1301 +      = (if x \<in> s then f x + g x else 0)" by auto
1.1302 +    show ?thesis apply(subst a) using as[OF assms[unfolded a[of f] a[of g]]] unfolding * . }
1.1303 +  fix f g::"real^'n \<Rightarrow> real^'m" assume assms:"f absolutely_integrable_on UNIV"
1.1304 +    "g absolutely_integrable_on UNIV"
1.1305 +  note absolutely_integrable_bounded_variation
1.1306 +  from this[OF assms(1)] this[OF assms(2)] guess B1 B2 . note B=this[rule_format]
1.1307 +  show "(\<lambda>x. f(x) + g(x)) absolutely_integrable_on UNIV"
1.1308 +    apply(rule bounded_variation_absolutely_integrable[of _ "B1+B2"])
1.1309 +    apply(rule integrable_add) prefer 3
1.1310 +  proof safe case goal1 have "\<And>k. k \<in> d \<Longrightarrow> f integrable_on k \<and> g integrable_on k"
1.1311 +      apply(drule division_ofD(4)[OF goal1]) apply safe
1.1312 +      apply(rule_tac[!] integrable_on_subinterval[of _ UNIV]) using assms by auto
1.1313 +    hence "(\<Sum>k\<in>d. norm (integral k (\<lambda>x. f x + g x))) \<le>
1.1314 +      (\<Sum>k\<in>d. norm (integral k f)) + (\<Sum>k\<in>d. norm (integral k g))" apply-
1.1315 +      unfolding setsum_addf[THEN sym] apply(rule setsum_mono)
1.1316 +      apply(subst integral_add) prefer 3 apply(rule norm_triangle_ineq) by auto
1.1317 +    also have "... \<le> B1 + B2" using B(1)[OF goal1] B(2)[OF goal1] by auto
1.1318 +    finally show ?case .
1.1319 +  qed(insert assms,auto) qed
1.1320 +
1.1321 +lemma absolutely_integrable_sub[intro]: fixes f g::"real^'n \<Rightarrow> real^'m"
1.1322 +  assumes "f absolutely_integrable_on s" "g absolutely_integrable_on s"
1.1323 +  shows "(\<lambda>x. f(x) - g(x)) absolutely_integrable_on s"
1.1324 +  using absolutely_integrable_add[OF assms(1) absolutely_integrable_neg[OF assms(2)]]
1.1325 +  unfolding group_simps .
1.1326 +
1.1327 +lemma absolutely_integrable_linear: fixes f::"real^'m \<Rightarrow> real^'n" and h::"real^'n \<Rightarrow> real^'p"
1.1328 +  assumes "f absolutely_integrable_on s" "bounded_linear h"
1.1329 +  shows "(h o f) absolutely_integrable_on s"
1.1330 +proof- { presume as:"\<And>f::real^'m \<Rightarrow> real^'n. \<And>h::real^'n \<Rightarrow> real^'p.
1.1331 +    f absolutely_integrable_on UNIV \<Longrightarrow> bounded_linear h \<Longrightarrow>
1.1332 +    (h o f) absolutely_integrable_on UNIV" note a = absolutely_integrable_restrict_univ[THEN sym]
1.1333 +    show ?thesis apply(subst a) using as[OF assms[unfolded a[of f] a[of g]]]
1.1334 +      unfolding o_def if_distrib linear_simps[OF assms(2)] . }
1.1335 +  fix f::"real^'m \<Rightarrow> real^'n" and h::"real^'n \<Rightarrow> real^'p"
1.1336 +  assume assms:"f absolutely_integrable_on UNIV" "bounded_linear h"
1.1337 +  from absolutely_integrable_bounded_variation[OF assms(1)] guess B . note B=this
1.1338 +  from bounded_linear.pos_bounded[OF assms(2)] guess b .. note b=conjunctD2[OF this]
1.1339 +  show "(h o f) absolutely_integrable_on UNIV"
1.1340 +    apply(rule bounded_variation_absolutely_integrable[of _ "B * b"])
1.1341 +    apply(rule integrable_linear[OF _ assms(2)])
1.1342 +  proof safe case goal2
1.1343 +    have "(\<Sum>k\<in>d. norm (integral k (h \<circ> f))) \<le> setsum (\<lambda>k. norm(integral k f)) d * b"
1.1344 +      unfolding setsum_left_distrib apply(rule setsum_mono)
1.1345 +    proof- case goal1 from division_ofD(4)[OF goal2 this]
1.1346 +      guess u v apply-by(erule exE)+ note uv=this
1.1347 +      have *:"f integrable_on k" unfolding uv apply(rule integrable_on_subinterval[of _ UNIV])
1.1348 +        using assms by auto note this[unfolded has_integral_integral]
1.1349 +      note has_integral_linear[OF this assms(2)] integrable_linear[OF * assms(2)]
1.1350 +      note * = has_integral_unique[OF this(2)[unfolded has_integral_integral] this(1)]
1.1351 +      show ?case unfolding * using b by auto
1.1352 +    qed also have "... \<le> B * b" apply(rule mult_right_mono) using B goal2 b by auto
1.1353 +    finally show ?case .
1.1354 +  qed(insert assms,auto) qed
1.1355 +
1.1356 +lemma absolutely_integrable_setsum: fixes f::"'a \<Rightarrow> real^'n \<Rightarrow> real^'m"
1.1357 +  assumes "finite t" "\<And>a. a \<in> t \<Longrightarrow> (f a) absolutely_integrable_on s"
1.1358 +  shows "(\<lambda>x. setsum (\<lambda>a. f a x) t) absolutely_integrable_on s"
1.1359 +  using assms(1,2) apply induct defer apply(subst setsum.insert) apply assumption+ by(rule,auto)
1.1360 +
1.1361 +lemma absolutely_integrable_vector_abs:
1.1362 +  assumes "f absolutely_integrable_on s"
1.1363 +  shows "(\<lambda>x. (\<chi> i. abs(f x\$i))::real^'n) absolutely_integrable_on s"
1.1364 +proof- have *:"\<And>x. ((\<chi> i. abs(f x\$i))::real^'n) = (setsum (\<lambda>i.
1.1365 +    (((\<lambda>y. (\<chi> j. if j = i then y\$1 else 0)) o
1.1366 +    (vec1 o ((\<lambda>x. (norm((\<chi> j. if j = i then x\$i else 0)::real^'n))) o f))) x)) UNIV)"
1.1367 +    unfolding Cart_eq setsum_component Cart_lambda_beta
1.1368 +  proof case goal1 have *:"\<And>i xa. ((if i = xa then f x \$ xa else 0) \<bullet> (if i = xa then f x \$ xa else 0)) =
1.1369 +      (if i = xa then (f x \$ xa) * (f x \$ xa) else 0)" by auto
1.1370 +    have "\<bar>f x \$ i\<bar> = (setsum (\<lambda>k. if k = i then abs ((f x)\$i) else 0) UNIV)"
1.1371 +      unfolding setsum_delta[OF finite_UNIV] by auto
1.1372 +    also have "... = (\<Sum>xa\<in>UNIV. ((\<lambda>y. \<chi> j. if j = xa then dest_vec1 y else 0) \<circ>
1.1373 +                      (\<lambda>x. vec1 (norm (\<chi> j. if j = xa then x \$ xa else 0))) \<circ> f) x \$ i)"
1.1374 +      unfolding norm_eq_sqrt_inner inner_vector_def Cart_lambda_beta o_def *
1.1375 +      apply(rule setsum_cong2) unfolding setsum_delta[OF finite_UNIV] by auto
1.1376 +    finally show ?case unfolding o_def . qed
1.1377 +  show ?thesis unfolding * apply(rule absolutely_integrable_setsum) apply(rule finite_UNIV)
1.1378 +    apply(rule absolutely_integrable_linear)
1.1379 +    unfolding absolutely_integrable_on_trans unfolding o_def apply(rule absolutely_integrable_norm)
1.1380 +    apply(rule absolutely_integrable_linear[OF assms,unfolded o_def]) unfolding linear_linear
1.1381 +    apply(rule_tac[!] linearI) unfolding Cart_eq by auto
1.1382 +qed
1.1383 +
1.1384 +lemma absolutely_integrable_max: fixes f::"real^'m \<Rightarrow> real^'n"
1.1385 +  assumes "f absolutely_integrable_on s" "g absolutely_integrable_on s"
1.1386 +  shows "(\<lambda>x. (\<chi> i. max (f(x)\$i) (g(x)\$i))::real^'n) absolutely_integrable_on s"
1.1387 +proof- have *:"\<And>x. (1 / 2) *\<^sub>R ((\<chi> i. \<bar>(f x - g x) \$ i\<bar>) + (f x + g x)) = (\<chi> i. max (f(x)\$i) (g(x)\$i))"
1.1388 +    unfolding Cart_eq by auto
1.1389 +  note absolutely_integrable_sub[OF assms] absolutely_integrable_add[OF assms]
1.1390 +  note absolutely_integrable_vector_abs[OF this(1)] this(2)
1.1391 +  note absolutely_integrable_add[OF this] note absolutely_integrable_cmul[OF this,of "1/2"]
1.1392 +  thus ?thesis unfolding * . qed
1.1393 +
1.1394 +lemma absolutely_integrable_max_real: fixes f::"real^'m \<Rightarrow> real"
1.1395 +  assumes "f absolutely_integrable_on s" "g absolutely_integrable_on s"
1.1396 +  shows "(\<lambda>x. max (f x) (g x)) absolutely_integrable_on s"
1.1397 +proof- have *:"(\<lambda>x. \<chi> i. max ((vec1 \<circ> f) x \$ i) ((vec1 \<circ> g) x \$ i)) = vec1 o (\<lambda>x. max (f x) (g x))"
1.1398 +    apply rule unfolding Cart_eq by auto note absolutely_integrable_max[of "vec1 o f" s "vec1 o g"]
1.1399 +  note this[unfolded absolutely_integrable_on_trans,OF assms]
1.1400 +  thus ?thesis unfolding * by auto qed
1.1401 +
1.1402 +lemma absolutely_integrable_min: fixes f::"real^'m \<Rightarrow> real^'n"
1.1403 +  assumes "f absolutely_integrable_on s" "g absolutely_integrable_on s"
1.1404 +  shows "(\<lambda>x. (\<chi> i. min (f(x)\$i) (g(x)\$i))::real^'n) absolutely_integrable_on s"
1.1405 +proof- have *:"\<And>x. (1 / 2) *\<^sub>R ((f x + g x) - (\<chi> i. \<bar>(f x - g x) \$ i\<bar>)) = (\<chi> i. min (f(x)\$i) (g(x)\$i))"
1.1406 +    unfolding Cart_eq by auto
1.1407 +  note absolutely_integrable_add[OF assms] absolutely_integrable_sub[OF assms]
1.1408 +  note this(1) absolutely_integrable_vector_abs[OF this(2)]
1.1409 +  note absolutely_integrable_sub[OF this] note absolutely_integrable_cmul[OF this,of "1/2"]
1.1410 +  thus ?thesis unfolding * . qed
1.1411 +
1.1412 +lemma absolutely_integrable_min_real: fixes f::"real^'m \<Rightarrow> real"
1.1413 +  assumes "f absolutely_integrable_on s" "g absolutely_integrable_on s"
1.1414 +  shows "(\<lambda>x. min (f x) (g x)) absolutely_integrable_on s"
1.1415 +proof- have *:"(\<lambda>x. \<chi> i. min ((vec1 \<circ> f) x \$ i) ((vec1 \<circ> g) x \$ i)) = vec1 o (\<lambda>x. min (f x) (g x))"
1.1416 +    apply rule unfolding Cart_eq by auto note absolutely_integrable_min[of "vec1 o f" s "vec1 o g"]
1.1417 +  note this[unfolded absolutely_integrable_on_trans,OF assms]
1.1418 +  thus ?thesis unfolding * by auto qed
1.1419 +
1.1420 +lemma absolutely_integrable_abs_eq: fixes f::"real^'n \<Rightarrow> real^'m"
1.1421 +  shows "f absolutely_integrable_on s \<longleftrightarrow> f integrable_on s \<and>
1.1422 +          (\<lambda>x. (\<chi> i. abs(f x\$i))::real^'m) integrable_on s" (is "?l = ?r")
1.1423 +proof assume ?l thus ?r apply-apply rule defer
1.1424 +    apply(drule absolutely_integrable_vector_abs) by auto
1.1425 +next assume ?r { presume lem:"\<And>f::real^'n \<Rightarrow> real^'m. f integrable_on UNIV \<Longrightarrow>
1.1426 +    (\<lambda>x. (\<chi> i. abs(f(x)\$i))::real^'m) integrable_on UNIV \<Longrightarrow> f absolutely_integrable_on UNIV"
1.1427 +    have *:"\<And>x. (\<chi> i. \<bar>(if x \<in> s then f x else 0) \$ i\<bar>) = (if x\<in>s then (\<chi> i. \<bar>f x \$ i\<bar>) else 0)"
1.1428 +      unfolding Cart_eq by auto
1.1429 +    show ?l apply(subst absolutely_integrable_restrict_univ[THEN sym]) apply(rule lem)
1.1430 +      unfolding integrable_restrict_univ * using `?r` by auto }
1.1431 +  fix f::"real^'n \<Rightarrow> real^'m" assume assms:"f integrable_on UNIV"
1.1432 +    "(\<lambda>x. (\<chi> i. abs(f(x)\$i))::real^'m) integrable_on UNIV"
1.1433 +  let ?B = "setsum (\<lambda>i. integral UNIV (\<lambda>x. (\<chi> j. abs(f x\$j)) ::real^'m) \$ i) UNIV"
1.1434 +  show "f absolutely_integrable_on UNIV"
1.1435 +    apply(rule bounded_variation_absolutely_integrable[OF assms(1), where B="?B"],safe)
1.1436 +  proof- case goal1 note d=this and d'=division_ofD[OF this]
1.1437 +    have "(\<Sum>k\<in>d. norm (integral k f)) \<le>
1.1438 +      (\<Sum>k\<in>d. setsum (op \$ (integral k (\<lambda>x. \<chi> j. \<bar>f x \$ j\<bar>))) UNIV)" apply(rule setsum_mono)
1.1439 +      apply(rule order_trans[OF norm_le_l1],rule setsum_mono)
1.1440 +    proof- fix k and i::'m assume "k\<in>d"
1.1441 +      from d'(4)[OF this] guess a b apply-by(erule exE)+ note ab=this
1.1442 +      show "\<bar>integral k f \$ i\<bar> \<le> integral k (\<lambda>x. \<chi> j. \<bar>f x \$ j\<bar>) \$ i" apply(rule abs_leI)
1.1443 +        unfolding vector_uminus_component[THEN sym] defer apply(subst integral_neg[THEN sym])
1.1444 +        defer apply(rule_tac[1-2] integral_component_le) apply(rule integrable_neg)
1.1445 +        using integrable_on_subinterval[OF assms(1),of a b]
1.1446 +          integrable_on_subinterval[OF assms(2),of a b] unfolding ab by auto
1.1447 +    qed also have "... \<le> setsum (op \$ (integral UNIV (\<lambda>x. \<chi> j. \<bar>f x \$ j\<bar>))) UNIV"
1.1448 +      apply(subst setsum_commute,rule setsum_mono)
1.1449 +    proof- case goal1 have *:"(\<lambda>x. \<chi> j. \<bar>f x \$ j\<bar>) integrable_on \<Union>d"
1.1450 +        using integrable_on_subdivision[OF d assms(2)] by auto
1.1451 +      have "(\<Sum>i\<in>d. integral i (\<lambda>x. \<chi> j. \<bar>f x \$ j\<bar>) \$ j)
1.1452 +        = integral (\<Union>d) (\<lambda>x. (\<chi> j. abs(f x\$j)) ::real^'m) \$ j"
1.1453 +        unfolding setsum_component[THEN sym]
1.1454 +        apply(subst integral_combine_division_topdown[THEN sym,OF * d]) by auto
1.1455 +      also have "... \<le> integral UNIV (\<lambda>x. \<chi> j. \<bar>f x \$ j\<bar>) \$ j"
1.1456 +        apply(rule integral_subset_component_le) using assms * by auto
1.1457 +      finally show ?case .
1.1458 +    qed finally show ?case . qed qed
1.1459 +
1.1460 +lemma nonnegative_absolutely_integrable: fixes f::"real^'n \<Rightarrow> real^'m"
1.1461 +  assumes "\<forall>x\<in>s. \<forall>i. 0 \<le> f(x)\$i" "f integrable_on s"
1.1462 +  shows "f absolutely_integrable_on s"
1.1463 +  unfolding absolutely_integrable_abs_eq apply rule defer
1.1464 +  apply(rule integrable_eq[of _ f]) unfolding Cart_eq using assms by auto
1.1465 +
1.1466 +lemma absolutely_integrable_integrable_bound: fixes f::"real^'n \<Rightarrow> real^'m"
1.1467 +  assumes "\<forall>x\<in>s. norm(f x) \<le> g x" "f integrable_on s" "g integrable_on s"
1.1468 +  shows "f absolutely_integrable_on s"
1.1469 +proof- { presume *:"\<And>f::real^'n \<Rightarrow> real^'m. \<And> g. \<forall>x. norm(f x) \<le> g x \<Longrightarrow> f integrable_on UNIV
1.1470 +    \<Longrightarrow> g integrable_on UNIV \<Longrightarrow> f absolutely_integrable_on UNIV"
1.1471 +    show ?thesis apply(subst absolutely_integrable_restrict_univ[THEN sym])
1.1472 +      apply(rule *[of _ "\<lambda>x. if x\<in>s then g x else 0"])
1.1473 +      using assms unfolding integrable_restrict_univ by auto }
1.1474 +  fix g and f :: "real^'n \<Rightarrow> real^'m"
1.1475 +  assume assms:"\<forall>x. norm(f x) \<le> g x" "f integrable_on UNIV" "g integrable_on UNIV"
1.1476 +  show "f absolutely_integrable_on UNIV"
1.1477 +    apply(rule bounded_variation_absolutely_integrable[OF assms(2),where B="integral UNIV g"])
1.1478 +  proof safe case goal1 note d=this and d'=division_ofD[OF this]
1.1479 +    have "(\<Sum>k\<in>d. norm (integral k f)) \<le> (\<Sum>k\<in>d. integral k g)"
1.1480 +      apply(rule setsum_mono) apply(rule integral_norm_bound_integral) apply(drule_tac[!] d'(4),safe)
1.1481 +      apply(rule_tac[1-2] integrable_on_subinterval) using assms by auto
1.1482 +    also have "... = integral (\<Union>d) g" apply(rule integral_combine_division_bottomup[THEN sym])
1.1483 +      apply(rule d,safe) apply(drule d'(4),safe)
1.1484 +      apply(rule integrable_on_subinterval[OF assms(3)]) by auto
1.1485 +    also have "... \<le> integral UNIV g" apply(rule integral_subset_le) defer
1.1486 +      apply(rule integrable_on_subdivision[OF d,of _ UNIV]) prefer 4
1.1487 +      apply(rule,rule_tac y="norm (f x)" in order_trans) using assms by auto
1.1488 +    finally show ?case . qed qed
1.1489 +
1.1490 +lemma absolutely_integrable_integrable_bound_real: fixes f::"real^'n \<Rightarrow> real"
1.1491 +  assumes "\<forall>x\<in>s. norm(f x) \<le> g x" "f integrable_on s" "g integrable_on s"
1.1492 +  shows "f absolutely_integrable_on s"
1.1493 +  apply(subst absolutely_integrable_on_trans[THEN sym])
1.1494 +  apply(rule absolutely_integrable_integrable_bound[where g=g])
1.1495 +  using assms unfolding o_def by auto
1.1496 +
1.1497 +lemma absolutely_integrable_absolutely_integrable_bound:
1.1498 +  fixes f::"real^'n \<Rightarrow> real^'m" and g::"real^'n \<Rightarrow> real^'p"
1.1499 +  assumes "\<forall>x\<in>s. norm(f x) \<le> norm(g x)" "f integrable_on s" "g absolutely_integrable_on s"
1.1500 +  shows "f absolutely_integrable_on s"
1.1501 +  apply(rule absolutely_integrable_integrable_bound[of s f "\<lambda>x. norm (g x)"])
1.1502 +  using assms by auto
1.1503 +
1.1504 +lemma absolutely_integrable_inf_real:
1.1505 +  assumes "finite k" "k \<noteq> {}"
1.1506 +  "\<forall>i\<in>k. (\<lambda>x. (fs x i)::real) absolutely_integrable_on s"
1.1507 +  shows "(\<lambda>x. (Inf ((fs x) ` k))) absolutely_integrable_on s" using assms
1.1508 +proof induct case (insert a k) let ?P = " (\<lambda>x. if fs x ` k = {} then fs x a
1.1509 +         else min (fs x a) (Inf (fs x ` k))) absolutely_integrable_on s"
1.1510 +  show ?case unfolding image_insert
1.1511 +    apply(subst Inf_insert_finite) apply(rule finite_imageI[OF insert(1)])
1.1512 +  proof(cases "k={}") case True
1.1513 +    thus ?P apply(subst if_P) defer apply(rule insert(5)[rule_format]) by auto
1.1514 +  next case False thus ?P apply(subst if_not_P) defer
1.1515 +      apply(rule absolutely_integrable_min_real)
1.1516 +      defer apply(rule insert(3)[OF False]) using insert(5) by auto
1.1517 +  qed qed auto
1.1518 +
1.1519 +lemma absolutely_integrable_sup_real:
1.1520 +  assumes "finite k" "k \<noteq> {}"
1.1521 +  "\<forall>i\<in>k. (\<lambda>x. (fs x i)::real) absolutely_integrable_on s"
1.1522 +  shows "(\<lambda>x. (Sup ((fs x) ` k))) absolutely_integrable_on s" using assms
1.1523 +proof induct case (insert a k) let ?P = " (\<lambda>x. if fs x ` k = {} then fs x a
1.1524 +         else max (fs x a) (Sup (fs x ` k))) absolutely_integrable_on s"
1.1525 +  show ?case unfolding image_insert
1.1526 +    apply(subst Sup_insert_finite) apply(rule finite_imageI[OF insert(1)])
1.1527 +  proof(cases "k={}") case True
1.1528 +    thus ?P apply(subst if_P) defer apply(rule insert(5)[rule_format]) by auto
1.1529 +  next case False thus ?P apply(subst if_not_P) defer
1.1530 +      apply(rule absolutely_integrable_max_real)
1.1531 +      defer apply(rule insert(3)[OF False]) using insert(5) by auto
1.1532 +  qed qed auto
1.1533 +
1.1534 +subsection {* Dominated convergence. *}
1.1535 +
1.1536 +lemma dominated_convergence: fixes f::"nat \<Rightarrow> real^'n \<Rightarrow> real"
1.1537 +  assumes "\<And>k. (f k) integrable_on s" "h integrable_on s"
1.1538 +  "\<And>k. \<forall>x \<in> s. norm(f k x) \<le> (h x)"
1.1539 +  "\<forall>x \<in> s. ((\<lambda>k. f k x) ---> g x) sequentially"
1.1540 +  shows "g integrable_on s" "((\<lambda>k. integral s (f k)) ---> integral s g) sequentially"
1.1541 +proof- have "\<And>m. (\<lambda>x. Inf {f j x |j. m \<le> j}) integrable_on s \<and>
1.1542 +    ((\<lambda>k. integral s (\<lambda>x. Inf {f j x |j. j \<in> {m..m + k}})) --->
1.1543 +    integral s (\<lambda>x. Inf {f j x |j. m \<le> j}))sequentially"
1.1544 +  proof(rule monotone_convergence_decreasing_real,safe) fix m::nat
1.1545 +    show "bounded {integral s (\<lambda>x. Inf {f j x |j. j \<in> {m..m + k}}) |k. True}"
1.1546 +      unfolding bounded_iff apply(rule_tac x="integral s h" in exI)
1.1547 +    proof safe fix k::nat
1.1548 +      show "norm (integral s (\<lambda>x. Inf {f j x |j. j \<in> {m..m + k}})) \<le> integral s h"
1.1549 +        apply(rule integral_norm_bound_integral) unfolding simple_image
1.1550 +        apply(rule absolutely_integrable_onD) apply(rule absolutely_integrable_inf_real)
1.1551 +        prefer 5 unfolding real_norm_def apply(rule) apply(rule Inf_abs_ge)
1.1552 +        prefer 5 apply rule apply(rule_tac g=h in absolutely_integrable_integrable_bound_real)
1.1553 +        using assms unfolding real_norm_def by auto
1.1554 +    qed fix k::nat show "(\<lambda>x. Inf {f j x |j. j \<in> {m..m + k}}) integrable_on s"
1.1555 +      unfolding simple_image apply(rule absolutely_integrable_onD)
1.1556 +      apply(rule absolutely_integrable_inf_real) prefer 3
1.1557 +      using absolutely_integrable_integrable_bound_real[OF assms(3,1,2)] by auto
1.1558 +    fix x assume x:"x\<in>s" show "Inf {f j x |j. j \<in> {m..m + Suc k}}
1.1559 +      \<le> Inf {f j x |j. j \<in> {m..m + k}}" apply(rule Inf_ge) unfolding setge_def
1.1560 +      defer apply rule apply(subst Inf_finite_le_iff) prefer 3
1.1561 +      apply(rule_tac x=xa in bexI) by auto
1.1562 +    let ?S = "{f j x| j.  m \<le> j}" def i \<equiv> "Inf ?S"
1.1563 +    show "((\<lambda>k. Inf {f j x |j. j \<in> {m..m + k}}) ---> i) sequentially"
1.1564 +      unfolding Lim_sequentially
1.1565 +    proof safe case goal1 note e=this have i:"isGlb UNIV ?S i" unfolding i_def apply(rule Inf)
1.1566 +        defer apply(rule_tac x="- h x - 1" in exI) unfolding setge_def
1.1567 +      proof safe case goal1 thus ?case using assms(3)[rule_format,OF x, of j] by auto
1.1568 +      qed auto
1.1569 +
1.1570 +      have "\<exists>y\<in>?S. \<not> y \<ge> i + e"
1.1571 +      proof(rule ccontr) case goal1 hence "i \<ge> i + e" apply-
1.1572 +          apply(rule isGlb_le_isLb[OF i]) apply(rule isLbI) unfolding setge_def by fastsimp+
1.1573 +        thus False using e by auto
1.1574 +      qed then guess y .. note y=this[unfolded not_le]
1.1575 +      from this(1)[unfolded mem_Collect_eq] guess N .. note N=conjunctD2[OF this]
1.1576 +
1.1577 +      show ?case apply(rule_tac x=N in exI)
1.1578 +      proof safe case goal1
1.1579 +        have *:"\<And>y ix. y < i + e \<longrightarrow> i \<le> ix \<longrightarrow> ix \<le> y \<longrightarrow> abs(ix - i) < e" by arith
1.1580 +        show ?case unfolding dist_real_def apply(rule *[rule_format,OF y(2)])
1.1581 +          unfolding i_def apply(rule real_le_inf_subset) prefer 3
1.1582 +          apply(rule,rule isGlbD1[OF i]) prefer 3 apply(subst Inf_finite_le_iff)
1.1583 +          prefer 3 apply(rule_tac x=y in bexI) using N goal1 by auto
1.1584 +      qed qed qed note dec1 = conjunctD2[OF this]
1.1585 +
1.1586 +  have "\<And>m. (\<lambda>x. Sup {f j x |j. m \<le> j}) integrable_on s \<and>
1.1587 +    ((\<lambda>k. integral s (\<lambda>x. Sup {f j x |j. j \<in> {m..m + k}})) --->
1.1588 +    integral s (\<lambda>x. Sup {f j x |j. m \<le> j})) sequentially"
1.1589 +  proof(rule monotone_convergence_increasing_real,safe) fix m::nat
1.1590 +    show "bounded {integral s (\<lambda>x. Sup {f j x |j. j \<in> {m..m + k}}) |k. True}"
1.1591 +      unfolding bounded_iff apply(rule_tac x="integral s h" in exI)
1.1592 +    proof safe fix k::nat
1.1593 +      show "norm (integral s (\<lambda>x. Sup {f j x |j. j \<in> {m..m + k}})) \<le> integral s h"
1.1594 +        apply(rule integral_norm_bound_integral) unfolding simple_image
1.1595 +        apply(rule absolutely_integrable_onD) apply(rule absolutely_integrable_sup_real)
1.1596 +        prefer 5 unfolding real_norm_def apply(rule) apply(rule Sup_abs_le)
1.1597 +        prefer 5 apply rule apply(rule_tac g=h in absolutely_integrable_integrable_bound_real)
1.1598 +        using assms unfolding real_norm_def by auto
1.1599 +    qed fix k::nat show "(\<lambda>x. Sup {f j x |j. j \<in> {m..m + k}}) integrable_on s"
1.1600 +      unfolding simple_image apply(rule absolutely_integrable_onD)
1.1601 +      apply(rule absolutely_integrable_sup_real) prefer 3
1.1602 +      using absolutely_integrable_integrable_bound_real[OF assms(3,1,2)] by auto
1.1603 +    fix x assume x:"x\<in>s" show "Sup {f j x |j. j \<in> {m..m + Suc k}}
1.1604 +      \<ge> Sup {f j x |j. j \<in> {m..m + k}}" apply(rule Sup_le) unfolding setle_def
1.1605 +      defer apply rule apply(subst Sup_finite_ge_iff) prefer 3 apply(rule_tac x=y in bexI) by auto
1.1606 +    let ?S = "{f j x| j.  m \<le> j}" def i \<equiv> "Sup ?S"
1.1607 +    show "((\<lambda>k. Sup {f j x |j. j \<in> {m..m + k}}) ---> i) sequentially"
1.1608 +      unfolding Lim_sequentially
1.1609 +    proof safe case goal1 note e=this have i:"isLub UNIV ?S i" unfolding i_def apply(rule Sup)
1.1610 +        defer apply(rule_tac x="h x" in exI) unfolding setle_def
1.1611 +      proof safe case goal1 thus ?case using assms(3)[rule_format,OF x, of j] by auto
1.1612 +      qed auto
1.1613 +
1.1614 +      have "\<exists>y\<in>?S. \<not> y \<le> i - e"
1.1615 +      proof(rule ccontr) case goal1 hence "i \<le> i - e" apply-
1.1616 +          apply(rule isLub_le_isUb[OF i]) apply(rule isUbI) unfolding setle_def by fastsimp+
1.1617 +        thus False using e by auto
1.1618 +      qed then guess y .. note y=this[unfolded not_le]
1.1619 +      from this(1)[unfolded mem_Collect_eq] guess N .. note N=conjunctD2[OF this]
1.1620 +
1.1621 +      show ?case apply(rule_tac x=N in exI)
1.1622 +      proof safe case goal1
1.1623 +        have *:"\<And>y ix. i - e < y \<longrightarrow> ix \<le> i \<longrightarrow> y \<le> ix \<longrightarrow> abs(ix - i) < e" by arith
1.1624 +        show ?case unfolding dist_real_def apply(rule *[rule_format,OF y(2)])
1.1625 +          unfolding i_def apply(rule real_ge_sup_subset) prefer 3
1.1626 +          apply(rule,rule isLubD1[OF i]) prefer 3 apply(subst Sup_finite_ge_iff)
1.1627 +          prefer 3 apply(rule_tac x=y in bexI) using N goal1 by auto
1.1628 +      qed qed qed note inc1 = conjunctD2[OF this]
1.1629 +
1.1630 +  have "g integrable_on s \<and> ((\<lambda>k. integral s (\<lambda>x. Inf {f j x |j. k \<le> j})) ---> integral s g) sequentially"
1.1631 +  apply(rule monotone_convergence_increasing_real,safe) apply fact
1.1632 +  proof- show "bounded {integral s (\<lambda>x. Inf {f j x |j. k \<le> j}) |k. True}"
1.1633 +      unfolding bounded_iff apply(rule_tac x="integral s h" in exI)
1.1634 +    proof safe fix k::nat
1.1635 +      show "norm (integral s (\<lambda>x. Inf {f j x |j. k \<le> j})) \<le> integral s h"
1.1636 +        apply(rule integral_norm_bound_integral) apply fact+
1.1637 +        unfolding real_norm_def apply(rule) apply(rule Inf_abs_ge) using assms(3) by auto
1.1638 +    qed fix k::nat and x assume x:"x\<in>s"
1.1639 +
1.1640 +    have *:"\<And>x y::real. x \<ge> - y \<Longrightarrow> - x \<le> y" by auto
1.1641 +    show "Inf {f j x |j. k \<le> j} \<le> Inf {f j x |j. Suc k \<le> j}" apply-
1.1642 +      apply(rule real_le_inf_subset) prefer 3 unfolding setge_def
1.1643 +      apply(rule_tac x="- h x" in exI) apply safe apply(rule *)
1.1644 +      using assms(3)[rule_format,OF x] unfolding real_norm_def abs_le_iff by auto
1.1645 +    show "((\<lambda>k. Inf {f j x |j. k \<le> j}) ---> g x) sequentially" unfolding Lim_sequentially
1.1646 +    proof safe case goal1 hence "0<e/2" by auto
1.1647 +      from assms(4)[unfolded Lim_sequentially,rule_format,OF x this] guess N .. note N=this
1.1648 +      show ?case apply(rule_tac x=N in exI,safe) unfolding dist_real_def
1.1649 +        apply(rule le_less_trans[of _ "e/2"]) apply(rule Inf_asclose) apply safe
1.1650 +        defer apply(rule less_imp_le) using N goal1 unfolding dist_real_def by auto
1.1651 +    qed qed note inc2 = conjunctD2[OF this]
1.1652 +
1.1653 +  have "g integrable_on s \<and> ((\<lambda>k. integral s (\<lambda>x. Sup {f j x |j. k \<le> j})) ---> integral s g) sequentially"
1.1654 +  apply(rule monotone_convergence_decreasing_real,safe) apply fact
1.1655 +  proof- show "bounded {integral s (\<lambda>x. Sup {f j x |j. k \<le> j}) |k. True}"
1.1656 +      unfolding bounded_iff apply(rule_tac x="integral s h" in exI)
1.1657 +    proof safe fix k::nat
1.1658 +      show "norm (integral s (\<lambda>x. Sup {f j x |j. k \<le> j})) \<le> integral s h"
1.1659 +        apply(rule integral_norm_bound_integral) apply fact+
1.1660 +        unfolding real_norm_def apply(rule) apply(rule Sup_abs_le) using assms(3) by auto
1.1661 +    qed fix k::nat and x assume x:"x\<in>s"
1.1662 +
1.1663 +    show "Sup {f j x |j. k \<le> j} \<ge> Sup {f j x |j. Suc k \<le> j}" apply-
1.1664 +      apply(rule real_ge_sup_subset) prefer 3 unfolding setle_def
1.1665 +      apply(rule_tac x="h x" in exI) apply safe
1.1666 +      using assms(3)[rule_format,OF x] unfolding real_norm_def abs_le_iff by auto
1.1667 +    show "((\<lambda>k. Sup {f j x |j. k \<le> j}) ---> g x) sequentially" unfolding Lim_sequentially
1.1668 +    proof safe case goal1 hence "0<e/2" by auto
1.1669 +      from assms(4)[unfolded Lim_sequentially,rule_format,OF x this] guess N .. note N=this
1.1670 +      show ?case apply(rule_tac x=N in exI,safe) unfolding dist_real_def
1.1671 +        apply(rule le_less_trans[of _ "e/2"]) apply(rule Sup_asclose) apply safe
1.1672 +        defer apply(rule less_imp_le) using N goal1 unfolding dist_real_def by auto
1.1673 +    qed qed note dec2 = conjunctD2[OF this]
1.1674 +
1.1675 +  show "g integrable_on s" by fact
1.1676 +  show "((\<lambda>k. integral s (f k)) ---> integral s g) sequentially" unfolding Lim_sequentially
1.1677 +  proof safe case goal1
1.1678 +    from inc2(2)[unfolded Lim_sequentially,rule_format,OF goal1] guess N1 .. note N1=this[unfolded dist_real_def]
1.1679 +    from dec2(2)[unfolded Lim_sequentially,rule_format,OF goal1] guess N2 .. note N2=this[unfolded dist_real_def]
1.1680 +    show ?case apply(rule_tac x="N1+N2" in exI,safe)
1.1681 +    proof- fix n assume n:"n \<ge> N1 + N2"
1.1682 +      have *:"\<And>i0 i i1 g. \<bar>i0 - g\<bar> < e \<longrightarrow> \<bar>i1 - g\<bar> < e \<longrightarrow> i0 \<le> i \<longrightarrow> i \<le> i1 \<longrightarrow> \<bar>i - g\<bar> < e" by arith
1.1683 +      show "dist (integral s (f n)) (integral s g) < e" unfolding dist_real_def
1.1684 +        apply(rule *[rule_format,OF N1[rule_format] N2[rule_format], of n n])
1.1685 +      proof- show "integral s (\<lambda>x. Inf {f j x |j. n \<le> j}) \<le> integral s (f n)"
1.1686 +        proof(rule integral_le[OF dec1(1) assms(1)],safe)
1.1687 +          fix x assume x:"x \<in> s" have *:"\<And>x y::real. x \<ge> - y \<Longrightarrow> - x \<le> y" by auto
1.1688 +          show "Inf {f j x |j. n \<le> j} \<le> f n x" apply(rule Inf_lower[where z="- h x"]) defer
1.1689 +            apply(rule *) using assms(3)[rule_format,OF x] unfolding real_norm_def abs_le_iff by auto
1.1690 +        qed show "integral s (f n) \<le> integral s (\<lambda>x. Sup {f j x |j. n \<le> j})"
1.1691 +        proof(rule integral_le[OF assms(1) inc1(1)],safe)
1.1692 +          fix x assume x:"x \<in> s"
1.1693 +          show "f n x \<le> Sup {f j x |j. n \<le> j}" apply(rule Sup_upper[where z="h x"]) defer
1.1694 +            using assms(3)[rule_format,OF x] unfolding real_norm_def abs_le_iff by auto
1.1695 +        qed qed(insert n,auto) qed qed qed
1.1696
1.1697  declare [[smt_certificates=""]]
1.1698
```