prove HK-integrable implies Lebesgue measurable; prove HK-integral equals Lebesgue integral for nonneg functions
authorhoelzl
Fri Sep 23 10:26:04 2016 +0200 (2016-09-23)
changeset 639400d82c4c94014
parent 63939 d4b89572ae71
child 63941 f353674c2528
prove HK-integrable implies Lebesgue measurable; prove HK-integral equals Lebesgue integral for nonneg functions
src/HOL/Analysis/Complete_Measure.thy
src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy
src/HOL/Analysis/Henstock_Kurzweil_Integration.thy
src/HOL/Analysis/Measure_Space.thy
src/HOL/Library/Extended_Nonnegative_Real.thy
src/HOL/Library/Extended_Real.thy
     1.1 --- a/src/HOL/Analysis/Complete_Measure.thy	Thu Sep 22 15:56:37 2016 +0100
     1.2 +++ b/src/HOL/Analysis/Complete_Measure.thy	Fri Sep 23 10:26:04 2016 +0200
     1.3 @@ -6,6 +6,10 @@
     1.4    imports Bochner_Integration
     1.5  begin
     1.6  
     1.7 +locale complete_measure =
     1.8 +  fixes M :: "'a measure"
     1.9 +  assumes complete: "\<And>A B. B \<subseteq> A \<Longrightarrow> A \<in> null_sets M \<Longrightarrow> B \<in> sets M"
    1.10 +
    1.11  definition
    1.12    "split_completion M A p = (if A \<in> sets M then p = (A, {}) else
    1.13     \<exists>N'. A = fst p \<union> snd p \<and> fst p \<inter> snd p = {} \<and> fst p \<in> sets M \<and> snd p \<subseteq> N' \<and> N' \<in> null_sets M)"
    1.14 @@ -304,4 +308,522 @@
    1.15  lemma AE_completion_iff: "{x\<in>space M. P x} \<in> sets M \<Longrightarrow> (AE x in M. P x) \<longleftrightarrow> (AE x in completion M. P x)"
    1.16    by (simp add: AE_iff_null null_sets_completion_iff)
    1.17  
    1.18 +lemma sets_completion_AE: "(AE x in M. \<not> P x) \<Longrightarrow> Measurable.pred (completion M) P"
    1.19 +  unfolding pred_def sets_completion eventually_ae_filter
    1.20 +  by auto
    1.21 +
    1.22 +lemma null_sets_completion_iff2:
    1.23 +  "A \<in> null_sets (completion M) \<longleftrightarrow> (\<exists>N'\<in>null_sets M. A \<subseteq> N')"
    1.24 +proof safe
    1.25 +  assume "A \<in> null_sets (completion M)"
    1.26 +  then have A: "A \<in> sets (completion M)" and "main_part M A \<in> null_sets M"
    1.27 +    by (auto simp: null_sets_def)
    1.28 +  moreover obtain N where "N \<in> null_sets M" "null_part M A \<subseteq> N"
    1.29 +    using null_part[OF A] by auto
    1.30 +  ultimately show "\<exists>N'\<in>null_sets M. A \<subseteq> N'"
    1.31 +  proof (intro bexI)
    1.32 +    show "A \<subseteq> N \<union> main_part M A"
    1.33 +      using \<open>null_part M A \<subseteq> N\<close> by (subst main_part_null_part_Un[OF A, symmetric]) auto
    1.34 +  qed auto
    1.35 +next
    1.36 +  fix N assume "N \<in> null_sets M" "A \<subseteq> N"
    1.37 +  then have "A \<in> sets (completion M)" and N: "N \<in> sets M" "A \<subseteq> N" "emeasure M N = 0"
    1.38 +    by (auto intro: null_sets_completion)
    1.39 +  moreover have "emeasure (completion M) A = 0"
    1.40 +    using N by (intro emeasure_eq_0[of N _ A]) auto
    1.41 +  ultimately show "A \<in> null_sets (completion M)"
    1.42 +    by auto
    1.43 +qed
    1.44 +
    1.45 +lemma null_sets_completion_subset:
    1.46 +  "B \<subseteq> A \<Longrightarrow> A \<in> null_sets (completion M) \<Longrightarrow> B \<in> null_sets (completion M)"
    1.47 +  unfolding null_sets_completion_iff2 by auto
    1.48 +
    1.49 +lemma null_sets_restrict_space:
    1.50 +  "\<Omega> \<in> sets M \<Longrightarrow> A \<in> null_sets (restrict_space M \<Omega>) \<longleftrightarrow> A \<subseteq> \<Omega> \<and> A \<in> null_sets M"
    1.51 +  by (auto simp: null_sets_def emeasure_restrict_space sets_restrict_space)
    1.52 +lemma completion_ex_borel_measurable_real:
    1.53 +  fixes g :: "'a \<Rightarrow> real"
    1.54 +  assumes g: "g \<in> borel_measurable (completion M)"
    1.55 +  shows "\<exists>g'\<in>borel_measurable M. (AE x in M. g x = g' x)"
    1.56 +proof -
    1.57 +  have "(\<lambda>x. ennreal (g x)) \<in> completion M \<rightarrow>\<^sub>M borel" "(\<lambda>x. ennreal (- g x)) \<in> completion M \<rightarrow>\<^sub>M borel"
    1.58 +    using g by auto
    1.59 +  from this[THEN completion_ex_borel_measurable]
    1.60 +  obtain pf nf :: "'a \<Rightarrow> ennreal"
    1.61 +    where [measurable]: "nf \<in> M \<rightarrow>\<^sub>M borel" "pf \<in> M \<rightarrow>\<^sub>M borel"
    1.62 +      and ae: "AE x in M. pf x = ennreal (g x)" "AE x in M. nf x = ennreal (- g x)"
    1.63 +    by (auto simp: eq_commute)
    1.64 +  then have "AE x in M. pf x = ennreal (g x) \<and> nf x = ennreal (- g x)"
    1.65 +    by auto
    1.66 +  then obtain N where "N \<in> null_sets M" "{x\<in>space M. pf x \<noteq> ennreal (g x) \<and> nf x \<noteq> ennreal (- g x)} \<subseteq> N"
    1.67 +    by (auto elim!: AE_E)
    1.68 +  show ?thesis
    1.69 +  proof
    1.70 +    let ?F = "\<lambda>x. indicator (space M - N) x * (enn2real (pf x) - enn2real (nf x))"
    1.71 +    show "?F \<in> M \<rightarrow>\<^sub>M borel"
    1.72 +      using \<open>N \<in> null_sets M\<close> by auto
    1.73 +    show "AE x in M. g x = ?F x"
    1.74 +      using \<open>N \<in> null_sets M\<close>[THEN AE_not_in] ae AE_space
    1.75 +      apply eventually_elim
    1.76 +      subgoal for x
    1.77 +        by (cases "0::real" "g x" rule: linorder_le_cases) (auto simp: ennreal_neg)
    1.78 +      done
    1.79 +  qed
    1.80 +qed
    1.81 +
    1.82 +lemma simple_function_completion: "simple_function M f \<Longrightarrow> simple_function (completion M) f"
    1.83 +  by (simp add: simple_function_def)
    1.84 +
    1.85 +lemma simple_integral_completion:
    1.86 +  "simple_function M f \<Longrightarrow> simple_integral (completion M) f = simple_integral M f"
    1.87 +  unfolding simple_integral_def by simp
    1.88 +
    1.89 +lemma nn_integral_completion: "nn_integral (completion M) f = nn_integral M f"
    1.90 +  unfolding nn_integral_def
    1.91 +proof (safe intro!: SUP_eq)
    1.92 +  fix s assume s: "simple_function (completion M) s" and "s \<le> f"
    1.93 +  then obtain s' where s': "simple_function M s'" "AE x in M. s x = s' x"
    1.94 +    by (auto dest: completion_ex_simple_function)
    1.95 +  then obtain N where N: "N \<in> null_sets M" "{x\<in>space M. s x \<noteq> s' x} \<subseteq> N"
    1.96 +    by (auto elim!: AE_E)
    1.97 +  then have ae_N: "AE x in M. (s x \<noteq> s' x \<longrightarrow> x \<in> N) \<and> x \<notin> N"
    1.98 +    by (auto dest: AE_not_in)
    1.99 +  define s'' where "s'' x = (if x \<in> N then 0 else s x)" for x
   1.100 +  then have ae_s_eq_s'': "AE x in completion M. s x = s'' x"
   1.101 +    using s' ae_N by (intro AE_completion) auto
   1.102 +  have s'': "simple_function M s''"
   1.103 +  proof (subst simple_function_cong)
   1.104 +    show "t \<in> space M \<Longrightarrow> s'' t = (if t \<in> N then 0 else s' t)" for t
   1.105 +      using N by (auto simp: s''_def dest: sets.sets_into_space)
   1.106 +    show "simple_function M (\<lambda>t. if t \<in> N then 0 else s' t)"
   1.107 +      unfolding s''_def[abs_def] using N by (auto intro!: simple_function_If s')
   1.108 +  qed
   1.109 +
   1.110 +  show "\<exists>j\<in>{g. simple_function M g \<and> g \<le> f}. integral\<^sup>S (completion M) s \<le> integral\<^sup>S M j"
   1.111 +  proof (safe intro!: bexI[of _ s''])
   1.112 +    have "integral\<^sup>S (completion M) s = integral\<^sup>S (completion M) s''"
   1.113 +      by (intro simple_integral_cong_AE s simple_function_completion s'' ae_s_eq_s'')
   1.114 +    then show "integral\<^sup>S (completion M) s \<le> integral\<^sup>S M s''"
   1.115 +      using s'' by (simp add: simple_integral_completion)
   1.116 +    from \<open>s \<le> f\<close> show "s'' \<le> f"
   1.117 +      unfolding s''_def le_fun_def by auto
   1.118 +  qed fact
   1.119 +next
   1.120 +  fix s assume "simple_function M s" "s \<le> f"
   1.121 +  then show "\<exists>j\<in>{g. simple_function (completion M) g \<and> g \<le> f}. integral\<^sup>S M s \<le> integral\<^sup>S (completion M) j"
   1.122 +    by (intro bexI[of _ s]) (auto simp: simple_integral_completion simple_function_completion)
   1.123 +qed
   1.124 +
   1.125 +locale semifinite_measure =
   1.126 +  fixes M :: "'a measure"
   1.127 +  assumes semifinite:
   1.128 +    "\<And>A. A \<in> sets M \<Longrightarrow> emeasure M A = \<infinity> \<Longrightarrow> \<exists>B\<in>sets M. B \<subseteq> A \<and> emeasure M B < \<infinity>"
   1.129 +
   1.130 +locale locally_determined_measure = semifinite_measure +
   1.131 +  assumes locally_determined:
   1.132 +    "\<And>A. A \<subseteq> space M \<Longrightarrow> (\<And>B. B \<in> sets M \<Longrightarrow> emeasure M B < \<infinity> \<Longrightarrow> A \<inter> B \<in> sets M) \<Longrightarrow> A \<in> sets M"
   1.133 +
   1.134 +locale cld_measure = complete_measure M + locally_determined_measure M for M :: "'a measure"
   1.135 +
   1.136 +definition outer_measure_of :: "'a measure \<Rightarrow> 'a set \<Rightarrow> ennreal"
   1.137 +  where "outer_measure_of M A = (INF B : {B\<in>sets M. A \<subseteq> B}. emeasure M B)"
   1.138 +
   1.139 +lemma outer_measure_of_eq[simp]: "A \<in> sets M \<Longrightarrow> outer_measure_of M A = emeasure M A"
   1.140 +  by (auto simp: outer_measure_of_def intro!: INF_eqI emeasure_mono)
   1.141 +
   1.142 +lemma outer_measure_of_mono: "A \<subseteq> B \<Longrightarrow> outer_measure_of M A \<le> outer_measure_of M B"
   1.143 +  unfolding outer_measure_of_def by (intro INF_superset_mono) auto
   1.144 +
   1.145 +lemma outer_measure_of_attain:
   1.146 +  assumes "A \<subseteq> space M"
   1.147 +  shows "\<exists>E\<in>sets M. A \<subseteq> E \<and> outer_measure_of M A = emeasure M E"
   1.148 +proof -
   1.149 +  have "emeasure M ` {B \<in> sets M. A \<subseteq> B} \<noteq> {}"
   1.150 +    using \<open>A \<subseteq> space M\<close> by auto
   1.151 +  from ennreal_Inf_countable_INF[OF this]
   1.152 +  obtain f
   1.153 +    where f: "range f \<subseteq> emeasure M ` {B \<in> sets M. A \<subseteq> B}" "decseq f"
   1.154 +      and "outer_measure_of M A = (INF i. f i)"
   1.155 +    unfolding outer_measure_of_def by auto
   1.156 +  have "\<exists>E. \<forall>n. (E n \<in> sets M \<and> A \<subseteq> E n \<and> emeasure M (E n) \<le> f n) \<and> E (Suc n) \<subseteq> E n"
   1.157 +  proof (rule dependent_nat_choice)
   1.158 +    show "\<exists>x. x \<in> sets M \<and> A \<subseteq> x \<and> emeasure M x \<le> f 0"
   1.159 +      using f(1) by (fastforce simp: image_subset_iff image_iff intro: eq_refl[OF sym])
   1.160 +  next
   1.161 +    fix E n assume "E \<in> sets M \<and> A \<subseteq> E \<and> emeasure M E \<le> f n"
   1.162 +    moreover obtain F where "F \<in> sets M" "A \<subseteq> F" "f (Suc n) = emeasure M F"
   1.163 +      using f(1) by (auto simp: image_subset_iff image_iff)
   1.164 +    ultimately show "\<exists>y. (y \<in> sets M \<and> A \<subseteq> y \<and> emeasure M y \<le> f (Suc n)) \<and> y \<subseteq> E"
   1.165 +      by (auto intro!: exI[of _ "F \<inter> E"] emeasure_mono)
   1.166 +  qed
   1.167 +  then obtain E
   1.168 +    where [simp]: "\<And>n. E n \<in> sets M"
   1.169 +      and "\<And>n. A \<subseteq> E n"
   1.170 +      and le_f: "\<And>n. emeasure M (E n) \<le> f n"
   1.171 +      and "decseq E"
   1.172 +    by (auto simp: decseq_Suc_iff)
   1.173 +  show ?thesis
   1.174 +  proof cases
   1.175 +    assume fin: "\<exists>i. emeasure M (E i) < \<infinity>"
   1.176 +    show ?thesis
   1.177 +    proof (intro bexI[of _ "\<Inter>i. E i"] conjI)
   1.178 +      show "A \<subseteq> (\<Inter>i. E i)" "(\<Inter>i. E i) \<in> sets M"
   1.179 +        using \<open>\<And>n. A \<subseteq> E n\<close> by auto
   1.180 +
   1.181 +      have " (INF i. emeasure M (E i)) \<le> outer_measure_of M A"
   1.182 +        unfolding \<open>outer_measure_of M A = (INF n. f n)\<close>
   1.183 +        by (intro INF_superset_mono le_f) auto
   1.184 +      moreover have "outer_measure_of M A \<le> (INF i. outer_measure_of M (E i))"
   1.185 +        by (intro INF_greatest outer_measure_of_mono \<open>\<And>n. A \<subseteq> E n\<close>)
   1.186 +      ultimately have "outer_measure_of M A = (INF i. emeasure M (E i))"
   1.187 +        by auto
   1.188 +      also have "\<dots> = emeasure M (\<Inter>i. E i)"
   1.189 +        using fin by (intro INF_emeasure_decseq' \<open>decseq E\<close>) (auto simp: less_top)
   1.190 +      finally show "outer_measure_of M A = emeasure M (\<Inter>i. E i)" .
   1.191 +    qed
   1.192 +  next
   1.193 +    assume "\<nexists>i. emeasure M (E i) < \<infinity>"
   1.194 +    then have "f n = \<infinity>" for n
   1.195 +      using le_f by (auto simp: not_less top_unique)
   1.196 +    moreover have "\<exists>E\<in>sets M. A \<subseteq> E \<and> f 0 = emeasure M E"
   1.197 +      using f by auto
   1.198 +    ultimately show ?thesis
   1.199 +      unfolding \<open>outer_measure_of M A = (INF n. f n)\<close> by simp
   1.200 +  qed
   1.201 +qed
   1.202 +
   1.203 +lemma SUP_outer_measure_of_incseq:
   1.204 +  assumes A: "\<And>n. A n \<subseteq> space M" and "incseq A"
   1.205 +  shows "(SUP n. outer_measure_of M (A n)) = outer_measure_of M (\<Union>i. A i)"
   1.206 +proof (rule antisym)
   1.207 +  obtain E
   1.208 +    where E: "\<And>n. E n \<in> sets M" "\<And>n. A n \<subseteq> E n" "\<And>n. outer_measure_of M (A n) = emeasure M (E n)"
   1.209 +    using outer_measure_of_attain[OF A] by metis
   1.210 +
   1.211 +  define F where "F n = (\<Inter>i\<in>{n ..}. E i)" for n
   1.212 +  with E have F: "incseq F" "\<And>n. F n \<in> sets M"
   1.213 +    by (auto simp: incseq_def)
   1.214 +  have "A n \<subseteq> F n" for n
   1.215 +    using incseqD[OF \<open>incseq A\<close>, of n] \<open>\<And>n. A n \<subseteq> E n\<close> by (auto simp: F_def)
   1.216 +
   1.217 +  have eq: "outer_measure_of M (A n) = outer_measure_of M (F n)" for n
   1.218 +  proof (intro antisym)
   1.219 +    have "outer_measure_of M (F n) \<le> outer_measure_of M (E n)"
   1.220 +      by (intro outer_measure_of_mono) (auto simp add: F_def)
   1.221 +    with E show "outer_measure_of M (F n) \<le> outer_measure_of M (A n)"
   1.222 +      by auto
   1.223 +    show "outer_measure_of M (A n) \<le> outer_measure_of M (F n)"
   1.224 +      by (intro outer_measure_of_mono \<open>A n \<subseteq> F n\<close>)
   1.225 +  qed
   1.226 +
   1.227 +  have "outer_measure_of M (\<Union>n. A n) \<le> outer_measure_of M (\<Union>n. F n)"
   1.228 +    using \<open>\<And>n. A n \<subseteq> F n\<close> by (intro outer_measure_of_mono) auto
   1.229 +  also have "\<dots> = (SUP n. emeasure M (F n))"
   1.230 +    using F by (simp add: SUP_emeasure_incseq subset_eq)
   1.231 +  finally show "outer_measure_of M (\<Union>n. A n) \<le> (SUP n. outer_measure_of M (A n))"
   1.232 +    by (simp add: eq F)
   1.233 +qed (auto intro: SUP_least outer_measure_of_mono)
   1.234 +
   1.235 +definition measurable_envelope :: "'a measure \<Rightarrow> 'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   1.236 +  where "measurable_envelope M A E \<longleftrightarrow>
   1.237 +    (A \<subseteq> E \<and> E \<in> sets M \<and> (\<forall>F\<in>sets M. emeasure M (F \<inter> E) = outer_measure_of M (F \<inter> A)))"
   1.238 +
   1.239 +lemma measurable_envelopeD:
   1.240 +  assumes "measurable_envelope M A E"
   1.241 +  shows "A \<subseteq> E"
   1.242 +    and "E \<in> sets M"
   1.243 +    and "\<And>F. F \<in> sets M \<Longrightarrow> emeasure M (F \<inter> E) = outer_measure_of M (F \<inter> A)"
   1.244 +    and "A \<subseteq> space M"
   1.245 +  using assms sets.sets_into_space[of E] by (auto simp: measurable_envelope_def)
   1.246 +
   1.247 +lemma measurable_envelopeD1:
   1.248 +  assumes E: "measurable_envelope M A E" and F: "F \<in> sets M" "F \<subseteq> E - A"
   1.249 +  shows "emeasure M F = 0"
   1.250 +proof -
   1.251 +  have "emeasure M F = emeasure M (F \<inter> E)"
   1.252 +    using F by (intro arg_cong2[where f=emeasure]) auto
   1.253 +  also have "\<dots> = outer_measure_of M (F \<inter> A)"
   1.254 +    using measurable_envelopeD[OF E] \<open>F \<in> sets M\<close> by (auto simp: measurable_envelope_def)
   1.255 +  also have "\<dots> = outer_measure_of M {}"
   1.256 +    using \<open>F \<subseteq> E - A\<close> by (intro arg_cong2[where f=outer_measure_of]) auto
   1.257 +  finally show "emeasure M F = 0"
   1.258 +    by simp
   1.259 +qed
   1.260 +
   1.261 +lemma measurable_envelope_eq1:
   1.262 +  assumes "A \<subseteq> E" "E \<in> sets M"
   1.263 +  shows "measurable_envelope M A E \<longleftrightarrow> (\<forall>F\<in>sets M. F \<subseteq> E - A \<longrightarrow> emeasure M F = 0)"
   1.264 +proof safe
   1.265 +  assume *: "\<forall>F\<in>sets M. F \<subseteq> E - A \<longrightarrow> emeasure M F = 0"
   1.266 +  show "measurable_envelope M A E"
   1.267 +    unfolding measurable_envelope_def
   1.268 +  proof (rule ccontr, auto simp add: \<open>E \<in> sets M\<close> \<open>A \<subseteq> E\<close>)
   1.269 +    fix F assume "F \<in> sets M" "emeasure M (F \<inter> E) \<noteq> outer_measure_of M (F \<inter> A)"
   1.270 +    then have "outer_measure_of M (F \<inter> A) < emeasure M (F \<inter> E)"
   1.271 +      using outer_measure_of_mono[of "F \<inter> A" "F \<inter> E" M] \<open>A \<subseteq> E\<close> \<open>E \<in> sets M\<close> by (auto simp: less_le)
   1.272 +    then obtain G where G: "G \<in> sets M" "F \<inter> A \<subseteq> G" and less: "emeasure M G < emeasure M (E \<inter> F)"
   1.273 +      unfolding outer_measure_of_def INF_less_iff by (auto simp: ac_simps)
   1.274 +    have le: "emeasure M (G \<inter> E \<inter> F) \<le> emeasure M G"
   1.275 +      using \<open>E \<in> sets M\<close> \<open>G \<in> sets M\<close> \<open>F \<in> sets M\<close> by (auto intro!: emeasure_mono)
   1.276 +
   1.277 +    from G have "E \<inter> F - G \<in> sets M" "E \<inter> F - G \<subseteq> E - A"
   1.278 +      using \<open>F \<in> sets M\<close> \<open>E \<in> sets M\<close> by auto
   1.279 +    with * have "0 = emeasure M (E \<inter> F - G)"
   1.280 +      by auto
   1.281 +    also have "E \<inter> F - G = E \<inter> F - (G \<inter> E \<inter> F)"
   1.282 +      by auto
   1.283 +    also have "emeasure M (E \<inter> F - (G \<inter> E \<inter> F)) = emeasure M (E \<inter> F) - emeasure M (G \<inter> E \<inter> F)"
   1.284 +      using \<open>E \<in> sets M\<close> \<open>F \<in> sets M\<close> le less G by (intro emeasure_Diff) (auto simp: top_unique)
   1.285 +    also have "\<dots> > 0"
   1.286 +      using le less by (intro diff_gr0_ennreal) auto
   1.287 +    finally show False by auto
   1.288 +  qed
   1.289 +qed (rule measurable_envelopeD1)
   1.290 +
   1.291 +lemma measurable_envelopeD2:
   1.292 +  assumes E: "measurable_envelope M A E" shows "emeasure M E = outer_measure_of M A"
   1.293 +proof -
   1.294 +  from \<open>measurable_envelope M A E\<close> have "emeasure M (E \<inter> E) = outer_measure_of M (E \<inter> A)"
   1.295 +    by (auto simp: measurable_envelope_def)
   1.296 +  with measurable_envelopeD[OF E] show "emeasure M E = outer_measure_of M A"
   1.297 +    by (auto simp: Int_absorb1)
   1.298 +qed
   1.299 +
   1.300 +lemma measurable_envelope_eq2:
   1.301 +  assumes "A \<subseteq> E" "E \<in> sets M" "emeasure M E < \<infinity>"
   1.302 +  shows "measurable_envelope M A E \<longleftrightarrow> (emeasure M E = outer_measure_of M A)"
   1.303 +proof safe
   1.304 +  assume *: "emeasure M E = outer_measure_of M A"
   1.305 +  show "measurable_envelope M A E"
   1.306 +    unfolding measurable_envelope_eq1[OF \<open>A \<subseteq> E\<close> \<open>E \<in> sets M\<close>]
   1.307 +  proof (intro conjI ballI impI assms)
   1.308 +    fix F assume F: "F \<in> sets M" "F \<subseteq> E - A"
   1.309 +    with \<open>E \<in> sets M\<close> have le: "emeasure M F \<le> emeasure M  E"
   1.310 +      by (intro emeasure_mono) auto
   1.311 +    from F \<open>A \<subseteq> E\<close> have "outer_measure_of M A \<le> outer_measure_of M (E - F)"
   1.312 +      by (intro outer_measure_of_mono) auto
   1.313 +    then have "emeasure M E - 0 \<le> emeasure M (E - F)"
   1.314 +      using * \<open>E \<in> sets M\<close> \<open>F \<in> sets M\<close> by simp
   1.315 +    also have "\<dots> = emeasure M E - emeasure M F"
   1.316 +      using \<open>E \<in> sets M\<close> \<open>emeasure M E < \<infinity>\<close> F le by (intro emeasure_Diff) (auto simp: top_unique)
   1.317 +    finally show "emeasure M F = 0"
   1.318 +      using ennreal_mono_minus_cancel[of "emeasure M E" 0 "emeasure M F"] le assms by auto
   1.319 +  qed
   1.320 +qed (auto intro: measurable_envelopeD2)
   1.321 +
   1.322 +lemma measurable_envelopeI_countable:
   1.323 +  fixes A :: "nat \<Rightarrow> 'a set"
   1.324 +  assumes E: "\<And>n. measurable_envelope M (A n) (E n)"
   1.325 +  shows "measurable_envelope M (\<Union>n. A n) (\<Union>n. E n)"
   1.326 +proof (subst measurable_envelope_eq1)
   1.327 +  show "(\<Union>n. A n) \<subseteq> (\<Union>n. E n)" "(\<Union>n. E n) \<in> sets M"
   1.328 +    using measurable_envelopeD(1,2)[OF E] by auto
   1.329 +  show "\<forall>F\<in>sets M. F \<subseteq> (\<Union>n. E n) - (\<Union>n. A n) \<longrightarrow> emeasure M F = 0"
   1.330 +  proof safe
   1.331 +    fix F assume F: "F \<in> sets M" "F \<subseteq> (\<Union>n. E n) - (\<Union>n. A n)"
   1.332 +    then have "F \<inter> E n \<in> sets M" "F \<inter> E n \<subseteq> E n - A n" "F \<subseteq> (\<Union>n. E n)" for n
   1.333 +      using measurable_envelopeD(1,2)[OF E] by auto
   1.334 +    then have "emeasure M (\<Union>n. F \<inter> E n) = 0"
   1.335 +      by (intro emeasure_UN_eq_0 measurable_envelopeD1[OF E]) auto
   1.336 +    then show "emeasure M F = 0"
   1.337 +      using \<open>F \<subseteq> (\<Union>n. E n)\<close> by (auto simp: Int_absorb2)
   1.338 +  qed
   1.339 +qed
   1.340 +
   1.341 +lemma measurable_envelopeI_countable_cover:
   1.342 +  fixes A and C :: "nat \<Rightarrow> 'a set"
   1.343 +  assumes C: "A \<subseteq> (\<Union>n. C n)" "\<And>n. C n \<in> sets M" "\<And>n. emeasure M (C n) < \<infinity>"
   1.344 +  shows "\<exists>E\<subseteq>(\<Union>n. C n). measurable_envelope M A E"
   1.345 +proof -
   1.346 +  have "A \<inter> C n \<subseteq> space M" for n
   1.347 +    using \<open>C n \<in> sets M\<close> by (auto dest: sets.sets_into_space)
   1.348 +  then have "\<forall>n. \<exists>E\<in>sets M. A \<inter> C n \<subseteq> E \<and> outer_measure_of M (A \<inter> C n) = emeasure M E"
   1.349 +    using outer_measure_of_attain[of "A \<inter> C n" M for n] by auto
   1.350 +  then obtain E
   1.351 +    where E: "\<And>n. E n \<in> sets M" "\<And>n. A \<inter> C n \<subseteq> E n"
   1.352 +      and eq: "\<And>n. outer_measure_of M (A \<inter> C n) = emeasure M (E n)"
   1.353 +    by metis
   1.354 +
   1.355 +  have "outer_measure_of M (A \<inter> C n) \<le> outer_measure_of M (E n \<inter> C n)" for n
   1.356 +    using E by (intro outer_measure_of_mono) auto
   1.357 +  moreover have "outer_measure_of M (E n \<inter> C n) \<le> outer_measure_of M (E n)" for n
   1.358 +    by (intro outer_measure_of_mono) auto
   1.359 +  ultimately have eq: "outer_measure_of M (A \<inter> C n) = emeasure M (E n \<inter> C n)" for n
   1.360 +    using E C by (intro antisym) (auto simp: eq)
   1.361 +
   1.362 +  { fix n
   1.363 +    have "outer_measure_of M (A \<inter> C n) \<le> outer_measure_of M (C n)"
   1.364 +      by (intro outer_measure_of_mono) simp
   1.365 +    also have "\<dots> < \<infinity>"
   1.366 +      using assms by auto
   1.367 +    finally have "emeasure M (E n \<inter> C n) < \<infinity>"
   1.368 +      using eq by simp }
   1.369 +  then have "measurable_envelope M (\<Union>n. A \<inter> C n) (\<Union>n. E n \<inter> C n)"
   1.370 +    using E C by (intro measurable_envelopeI_countable measurable_envelope_eq2[THEN iffD2]) (auto simp: eq)
   1.371 +  with \<open>A \<subseteq> (\<Union>n. C n)\<close> show ?thesis
   1.372 +    by (intro exI[of _ "(\<Union>n. E n \<inter> C n)"]) (auto simp add: Int_absorb2)
   1.373 +qed
   1.374 +
   1.375 +lemma (in complete_measure) complete_sets_sandwich:
   1.376 +  assumes [measurable]: "A \<in> sets M" "C \<in> sets M" and subset: "A \<subseteq> B" "B \<subseteq> C"
   1.377 +    and measure: "emeasure M A = emeasure M C" "emeasure M A < \<infinity>"
   1.378 +  shows "B \<in> sets M"
   1.379 +proof -
   1.380 +  have "B - A \<in> sets M"
   1.381 +  proof (rule complete)
   1.382 +    show "B - A \<subseteq> C - A"
   1.383 +      using subset by auto
   1.384 +    show "C - A \<in> null_sets M"
   1.385 +      using measure subset by(simp add: emeasure_Diff null_setsI)
   1.386 +  qed
   1.387 +  then have "A \<union> (B - A) \<in> sets M"
   1.388 +    by measurable
   1.389 +  also have "A \<union> (B - A) = B"
   1.390 +    using \<open>A \<subseteq> B\<close> by auto
   1.391 +  finally show ?thesis .
   1.392 +qed
   1.393 +
   1.394 +lemma (in cld_measure) notin_sets_outer_measure_of_cover:
   1.395 +  assumes E: "E \<subseteq> space M" "E \<notin> sets M"
   1.396 +  shows "\<exists>B\<in>sets M. 0 < emeasure M B \<and> emeasure M B < \<infinity> \<and>
   1.397 +    outer_measure_of M (B \<inter> E) = emeasure M B \<and> outer_measure_of M (B - E) = emeasure M B"
   1.398 +proof -
   1.399 +  from locally_determined[OF \<open>E \<subseteq> space M\<close>] \<open>E \<notin> sets M\<close>
   1.400 +  obtain F
   1.401 +    where [measurable]: "F \<in> sets M" and "emeasure M F < \<infinity>" "E \<inter> F \<notin> sets M"
   1.402 +    by blast
   1.403 +  then obtain H H'
   1.404 +    where H: "measurable_envelope M (F \<inter> E) H" and H': "measurable_envelope M (F - E) H'"
   1.405 +    using measurable_envelopeI_countable_cover[of "F \<inter> E" "\<lambda>_. F" M]
   1.406 +       measurable_envelopeI_countable_cover[of "F - E" "\<lambda>_. F" M]
   1.407 +    by auto
   1.408 +  note measurable_envelopeD(2)[OF H', measurable] measurable_envelopeD(2)[OF H, measurable]
   1.409 +
   1.410 +  from measurable_envelopeD(1)[OF H'] measurable_envelopeD(1)[OF H]
   1.411 +  have subset: "F - H' \<subseteq> F \<inter> E" "F \<inter> E \<subseteq> F \<inter> H"
   1.412 +    by auto
   1.413 +  moreover define G where "G = (F \<inter> H) - (F - H')"
   1.414 +  ultimately have G: "G = F \<inter> H \<inter> H'"
   1.415 +    by auto
   1.416 +  have "emeasure M (F \<inter> H) \<noteq> 0"
   1.417 +  proof
   1.418 +    assume "emeasure M (F \<inter> H) = 0"
   1.419 +    then have "F \<inter> H \<in> null_sets M"
   1.420 +      by auto
   1.421 +    with \<open>E \<inter> F \<notin> sets M\<close> show False
   1.422 +      using complete[OF \<open>F \<inter> E \<subseteq> F \<inter> H\<close>] by (auto simp: Int_commute)
   1.423 +  qed
   1.424 +  moreover
   1.425 +  have "emeasure M (F - H') \<noteq> emeasure M (F \<inter> H)"
   1.426 +  proof
   1.427 +    assume "emeasure M (F - H') = emeasure M (F \<inter> H)"
   1.428 +    with \<open>E \<inter> F \<notin> sets M\<close> emeasure_mono[of "F \<inter> H" F M] \<open>emeasure M F < \<infinity>\<close>
   1.429 +    have "F \<inter> E \<in> sets M"
   1.430 +      by (intro complete_sets_sandwich[OF _ _ subset]) auto
   1.431 +    with \<open>E \<inter> F \<notin> sets M\<close> show False
   1.432 +      by (simp add: Int_commute)
   1.433 +  qed
   1.434 +  moreover have "emeasure M (F - H') \<le> emeasure M (F \<inter> H)"
   1.435 +    using subset by (intro emeasure_mono) auto
   1.436 +  ultimately have "emeasure M G \<noteq> 0"
   1.437 +    unfolding G_def using subset
   1.438 +    by (subst emeasure_Diff) (auto simp: top_unique diff_eq_0_iff_ennreal)
   1.439 +  show ?thesis
   1.440 +  proof (intro bexI conjI)
   1.441 +    have "emeasure M G \<le> emeasure M F"
   1.442 +      unfolding G by (auto intro!: emeasure_mono)
   1.443 +    with \<open>emeasure M F < \<infinity>\<close> show "0 < emeasure M G" "emeasure M G < \<infinity>"
   1.444 +      using \<open>emeasure M G \<noteq> 0\<close> by (auto simp: zero_less_iff_neq_zero)
   1.445 +    show [measurable]: "G \<in> sets M"
   1.446 +      unfolding G by auto
   1.447 +
   1.448 +    have "emeasure M G = outer_measure_of M (F \<inter> H' \<inter> (F \<inter> E))"
   1.449 +      using measurable_envelopeD(3)[OF H, of "F \<inter> H'"] unfolding G by (simp add: ac_simps)
   1.450 +    also have "\<dots> \<le> outer_measure_of M (G \<inter> E)"
   1.451 +      using measurable_envelopeD(1)[OF H] by (intro outer_measure_of_mono) (auto simp: G)
   1.452 +    finally show "outer_measure_of M (G \<inter> E) = emeasure M G"
   1.453 +      using outer_measure_of_mono[of "G \<inter> E" G M] by auto
   1.454 +
   1.455 +    have "emeasure M G = outer_measure_of M (F \<inter> H \<inter> (F - E))"
   1.456 +      using measurable_envelopeD(3)[OF H', of "F \<inter> H"] unfolding G by (simp add: ac_simps)
   1.457 +    also have "\<dots> \<le> outer_measure_of M (G - E)"
   1.458 +      using measurable_envelopeD(1)[OF H'] by (intro outer_measure_of_mono) (auto simp: G)
   1.459 +    finally show "outer_measure_of M (G - E) = emeasure M G"
   1.460 +      using outer_measure_of_mono[of "G - E" G M] by auto
   1.461 +  qed
   1.462 +qed
   1.463 +
   1.464 +text \<open>The following theorem is a specialization of D.H. Fremlin, Measure Theory vol 4I (413G). We
   1.465 +  only show one direction and do not use a inner regular family $K$.\<close>
   1.466 +
   1.467 +lemma (in cld_measure) borel_measurable_cld:
   1.468 +  fixes f :: "'a \<Rightarrow> real"
   1.469 +  assumes "\<And>A a b. A \<in> sets M \<Longrightarrow> 0 < emeasure M A \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow> a < b \<Longrightarrow>
   1.470 +      min (outer_measure_of M {x\<in>A. f x \<le> a}) (outer_measure_of M {x\<in>A. b \<le> f x}) < emeasure M A"
   1.471 +  shows "f \<in> M \<rightarrow>\<^sub>M borel"
   1.472 +proof (rule ccontr)
   1.473 +  let ?E = "\<lambda>a. {x\<in>space M. f x \<le> a}" and ?F = "\<lambda>a. {x\<in>space M. a \<le> f x}"
   1.474 +
   1.475 +  assume "f \<notin> M \<rightarrow>\<^sub>M borel"
   1.476 +  then obtain a where "?E a \<notin> sets M"
   1.477 +    unfolding borel_measurable_iff_le by blast
   1.478 +  from notin_sets_outer_measure_of_cover[OF _ this]
   1.479 +  obtain K
   1.480 +    where K: "K \<in> sets M" "0 < emeasure M K" "emeasure M K < \<infinity>"
   1.481 +      and eq1: "outer_measure_of M (K \<inter> ?E a) = emeasure M K"
   1.482 +      and eq2: "outer_measure_of M (K - ?E a) = emeasure M K"
   1.483 +    by auto
   1.484 +  then have me_K: "measurable_envelope M (K \<inter> ?E a) K"
   1.485 +    by (subst measurable_envelope_eq2) auto
   1.486 +
   1.487 +  define b where "b n = a + inverse (real (Suc n))" for n
   1.488 +  have "(SUP n. outer_measure_of M (K \<inter> ?F (b n))) = outer_measure_of M (\<Union>n. K \<inter> ?F (b n))"
   1.489 +  proof (intro SUP_outer_measure_of_incseq)
   1.490 +    have "x \<le> y \<Longrightarrow> b y \<le> b x" for x y
   1.491 +      by (auto simp: b_def field_simps)
   1.492 +    then show "incseq (\<lambda>n. K \<inter> {x \<in> space M. b n \<le> f x})"
   1.493 +      by (auto simp: incseq_def intro: order_trans)
   1.494 +  qed auto
   1.495 +  also have "(\<Union>n. K \<inter> ?F (b n)) = K - ?E a"
   1.496 +  proof -
   1.497 +    have "b \<longlonglongrightarrow> a"
   1.498 +      unfolding b_def by (rule LIMSEQ_inverse_real_of_nat_add)
   1.499 +    then have "\<forall>n. \<not> b n \<le> f x \<Longrightarrow> f x \<le> a" for x
   1.500 +      by (rule LIMSEQ_le_const) (auto intro: less_imp_le simp: not_le)
   1.501 +    moreover have "\<not> b n \<le> a" for n
   1.502 +      by (auto simp: b_def)
   1.503 +    ultimately show ?thesis
   1.504 +      using \<open>K \<in> sets M\<close>[THEN sets.sets_into_space] by (auto simp: subset_eq intro: order_trans)
   1.505 +  qed
   1.506 +  finally have "0 < (SUP n. outer_measure_of M (K \<inter> ?F (b n)))"
   1.507 +    using K by (simp add: eq2)
   1.508 +  then obtain n where pos_b: "0 < outer_measure_of M (K \<inter> ?F (b n))" and "a < b n"
   1.509 +    unfolding less_SUP_iff by (auto simp: b_def)
   1.510 +  from measurable_envelopeI_countable_cover[of "K \<inter> ?F (b n)" "\<lambda>_. K" M] K
   1.511 +  obtain K' where "K' \<subseteq> K" and me_K': "measurable_envelope M (K \<inter> ?F (b n)) K'"
   1.512 +    by auto
   1.513 +  then have K'_le_K: "emeasure M K' \<le> emeasure M K"
   1.514 +    by (intro emeasure_mono K)
   1.515 +  have "K' \<in> sets M"
   1.516 +    using me_K' by (rule measurable_envelopeD)
   1.517 +
   1.518 +  have "min (outer_measure_of M {x\<in>K'. f x \<le> a}) (outer_measure_of M {x\<in>K'. b n \<le> f x}) < emeasure M K'"
   1.519 +  proof (rule assms)
   1.520 +    show "0 < emeasure M K'" "emeasure M K' < \<infinity>"
   1.521 +      using measurable_envelopeD2[OF me_K'] pos_b K K'_le_K by auto
   1.522 +  qed fact+
   1.523 +  also have "{x\<in>K'. f x \<le> a} = K' \<inter> (K \<inter> ?E a)"
   1.524 +    using \<open>K' \<in> sets M\<close>[THEN sets.sets_into_space] \<open>K' \<subseteq> K\<close> by auto
   1.525 +  also have "{x\<in>K'. b n \<le> f x} = K' \<inter> (K \<inter> ?F (b n))"
   1.526 +    using \<open>K' \<in> sets M\<close>[THEN sets.sets_into_space] \<open>K' \<subseteq> K\<close> by auto
   1.527 +  finally have "min (emeasure M K) (emeasure M K') < emeasure M K'"
   1.528 +    unfolding
   1.529 +      measurable_envelopeD(3)[OF me_K \<open>K' \<in> sets M\<close>, symmetric]
   1.530 +      measurable_envelopeD(3)[OF me_K' \<open>K' \<in> sets M\<close>, symmetric]
   1.531 +    using \<open>K' \<subseteq> K\<close> by (simp add: Int_absorb1 Int_absorb2)
   1.532 +  with K'_le_K show False
   1.533 +    by (auto simp: min_def split: if_split_asm)
   1.534 +qed
   1.535 +
   1.536  end
     2.1 --- a/src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy	Thu Sep 22 15:56:37 2016 +0100
     2.2 +++ b/src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy	Fri Sep 23 10:26:04 2016 +0200
     2.3 @@ -1,7 +1,340 @@
     2.4  theory Equivalence_Lebesgue_Henstock_Integration
     2.5 -  imports Lebesgue_Measure Henstock_Kurzweil_Integration
     2.6 +  imports Lebesgue_Measure Henstock_Kurzweil_Integration Complete_Measure
     2.7  begin
     2.8  
     2.9 +lemma le_left_mono: "x \<le> y \<Longrightarrow> y \<le> a \<longrightarrow> x \<le> (a::'a::preorder)"
    2.10 +  by (auto intro: order_trans)
    2.11 +
    2.12 +lemma ball_trans:
    2.13 +  assumes "y \<in> ball z q" "r + q \<le> s" shows "ball y r \<subseteq> ball z s"
    2.14 +proof safe
    2.15 +  fix x assume x: "x \<in> ball y r"
    2.16 +  have "dist z x \<le> dist z y + dist y x"
    2.17 +    by (rule dist_triangle)
    2.18 +  also have "\<dots> < s"
    2.19 +    using assms x by auto
    2.20 +  finally show "x \<in> ball z s"
    2.21 +    by simp
    2.22 +qed
    2.23 +
    2.24 +abbreviation lebesgue :: "'a::euclidean_space measure"
    2.25 +  where "lebesgue \<equiv> completion lborel"
    2.26 +
    2.27 +abbreviation lebesgue_on :: "'a set \<Rightarrow> 'a::euclidean_space measure"
    2.28 +  where "lebesgue_on \<Omega> \<equiv> restrict_space (completion lborel) \<Omega>"
    2.29 +
    2.30 +lemma has_integral_implies_lebesgue_measurable_cbox:
    2.31 +  fixes f :: "'a :: euclidean_space \<Rightarrow> real"
    2.32 +  assumes f: "(f has_integral I) (cbox x y)"
    2.33 +  shows "f \<in> lebesgue_on (cbox x y) \<rightarrow>\<^sub>M borel"
    2.34 +proof (rule cld_measure.borel_measurable_cld)
    2.35 +  let ?L = "lebesgue_on (cbox x y)"
    2.36 +  let ?\<mu> = "emeasure ?L"
    2.37 +  let ?\<mu>' = "outer_measure_of ?L"
    2.38 +  interpret L: finite_measure ?L
    2.39 +  proof
    2.40 +    show "?\<mu> (space ?L) \<noteq> \<infinity>"
    2.41 +      by (simp add: emeasure_restrict_space space_restrict_space emeasure_lborel_cbox_eq)
    2.42 +  qed
    2.43 +
    2.44 +  show "cld_measure ?L"
    2.45 +  proof
    2.46 +    fix B A assume "B \<subseteq> A" "A \<in> null_sets ?L"
    2.47 +    then show "B \<in> sets ?L"
    2.48 +      using null_sets_completion_subset[OF \<open>B \<subseteq> A\<close>, of lborel]
    2.49 +      by (auto simp add: null_sets_restrict_space sets_restrict_space_iff intro: )
    2.50 +  next
    2.51 +    fix A assume "A \<subseteq> space ?L" "\<And>B. B \<in> sets ?L \<Longrightarrow> ?\<mu> B < \<infinity> \<Longrightarrow> A \<inter> B \<in> sets ?L"
    2.52 +    from this(1) this(2)[of "space ?L"] show "A \<in> sets ?L"
    2.53 +      by (auto simp: Int_absorb2 less_top[symmetric])
    2.54 +  qed auto
    2.55 +  then interpret cld_measure ?L
    2.56 +    .
    2.57 +
    2.58 +  have content_eq_L: "A \<in> sets borel \<Longrightarrow> A \<subseteq> cbox x y \<Longrightarrow> content A = measure ?L A" for A
    2.59 +    by (subst measure_restrict_space) (auto simp: measure_def)
    2.60 +
    2.61 +  fix E and a b :: real assume "E \<in> sets ?L" "a < b" "0 < ?\<mu> E" "?\<mu> E < \<infinity>"
    2.62 +  then obtain M :: real where "?\<mu> E = M" "0 < M"
    2.63 +    by (cases "?\<mu> E") auto
    2.64 +  define e where "e = M / (4 + 2 / (b - a))"
    2.65 +  from \<open>a < b\<close> \<open>0<M\<close> have "0 < e"
    2.66 +    by (auto intro!: divide_pos_pos simp: field_simps e_def)
    2.67 +
    2.68 +  have "e < M / (3 + 2 / (b - a))"
    2.69 +    using \<open>a < b\<close> \<open>0 < M\<close>
    2.70 +    unfolding e_def by (intro divide_strict_left_mono add_strict_right_mono mult_pos_pos) (auto simp: field_simps)
    2.71 +  then have "2 * e < (b - a) * (M - e * 3)"
    2.72 +    using \<open>0<M\<close> \<open>0 < e\<close> \<open>a < b\<close> by (simp add: field_simps)
    2.73 +
    2.74 +  have e_less_M: "e < M / 1"
    2.75 +    unfolding e_def using \<open>a < b\<close> \<open>0<M\<close> by (intro divide_strict_left_mono) (auto simp: field_simps)
    2.76 +
    2.77 +  obtain d
    2.78 +    where "gauge d"
    2.79 +      and integral_f: "\<forall>p. p tagged_division_of cbox x y \<and> d fine p \<longrightarrow>
    2.80 +        norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - I) < e"
    2.81 +    using \<open>0<e\<close> f unfolding has_integral by auto
    2.82 +
    2.83 +  define C where "C X m = X \<inter> {x. ball x (1/Suc m) \<subseteq> d x}" for X m
    2.84 +  have "incseq (C X)" for X
    2.85 +    unfolding C_def [abs_def]
    2.86 +    by (intro monoI Collect_mono conj_mono imp_refl le_left_mono subset_ball divide_left_mono Int_mono) auto
    2.87 +
    2.88 +  { fix X assume "X \<subseteq> space ?L" and eq: "?\<mu>' X = ?\<mu> E"
    2.89 +    have "(SUP m. outer_measure_of ?L (C X m)) = outer_measure_of ?L (\<Union>m. C X m)"
    2.90 +      using \<open>X \<subseteq> space ?L\<close> by (intro SUP_outer_measure_of_incseq \<open>incseq (C X)\<close>) (auto simp: C_def)
    2.91 +    also have "(\<Union>m. C X m) = X"
    2.92 +    proof -
    2.93 +      { fix x
    2.94 +        obtain e where "0 < e" "ball x e \<subseteq> d x"
    2.95 +          using gaugeD[OF \<open>gauge d\<close>, of x] unfolding open_contains_ball by auto
    2.96 +        moreover
    2.97 +        obtain n where "1 / (1 + real n) < e"
    2.98 +          using reals_Archimedean[OF \<open>0<e\<close>] by (auto simp: inverse_eq_divide)
    2.99 +        then have "ball x (1 / (1 + real n)) \<subseteq> ball x e"
   2.100 +          by (intro subset_ball) auto
   2.101 +        ultimately have "\<exists>n. ball x (1 / (1 + real n)) \<subseteq> d x"
   2.102 +          by blast }
   2.103 +      then show ?thesis
   2.104 +        by (auto simp: C_def)
   2.105 +    qed
   2.106 +    finally have "(SUP m. outer_measure_of ?L (C X m)) = ?\<mu> E"
   2.107 +      using eq by auto
   2.108 +    also have "\<dots> > M - e"
   2.109 +      using \<open>0 < M\<close> \<open>?\<mu> E = M\<close> \<open>0<e\<close> by (auto intro!: ennreal_lessI)
   2.110 +    finally have "\<exists>m. M - e < outer_measure_of ?L (C X m)"
   2.111 +      unfolding less_SUP_iff by auto }
   2.112 +  note C = this
   2.113 +
   2.114 +  let ?E = "{x\<in>E. f x \<le> a}" and ?F = "{x\<in>E. b \<le> f x}"
   2.115 +
   2.116 +  have "\<not> (?\<mu>' ?E = ?\<mu> E \<and> ?\<mu>' ?F = ?\<mu> E)"
   2.117 +  proof
   2.118 +    assume eq: "?\<mu>' ?E = ?\<mu> E \<and> ?\<mu>' ?F = ?\<mu> E"
   2.119 +    with C[of ?E] C[of ?F] \<open>E \<in> sets ?L\<close>[THEN sets.sets_into_space] obtain ma mb
   2.120 +      where "M - e < outer_measure_of ?L (C ?E ma)" "M - e < outer_measure_of ?L (C ?F mb)"
   2.121 +      by auto
   2.122 +    moreover define m where "m = max ma mb"
   2.123 +    ultimately have M_minus_e: "M - e < outer_measure_of ?L (C ?E m)" "M - e < outer_measure_of ?L (C ?F m)"
   2.124 +      using
   2.125 +        incseqD[OF \<open>incseq (C ?E)\<close>, of ma m, THEN outer_measure_of_mono]
   2.126 +        incseqD[OF \<open>incseq (C ?F)\<close>, of mb m, THEN outer_measure_of_mono]
   2.127 +      by (auto intro: less_le_trans)
   2.128 +    define d' where "d' x = d x \<inter> ball x (1 / (3 * Suc m))" for x
   2.129 +    have "gauge d'"
   2.130 +      unfolding d'_def by (intro gauge_inter \<open>gauge d\<close> gauge_ball) auto
   2.131 +    then obtain p where p: "p tagged_division_of cbox x y" "d' fine p"
   2.132 +      by (rule fine_division_exists)
   2.133 +    then have "d fine p"
   2.134 +      unfolding d'_def[abs_def] fine_def by auto
   2.135 +
   2.136 +    define s where "s = {(x::'a, k). k \<inter> (C ?E m) \<noteq> {} \<and> k \<inter> (C ?F m) \<noteq> {}}"
   2.137 +    define T where "T E k = (SOME x. x \<in> k \<inter> C E m)" for E k
   2.138 +    let ?A = "(\<lambda>(x, k). (T ?E k, k)) ` (p \<inter> s) \<union> (p - s)"
   2.139 +    let ?B = "(\<lambda>(x, k). (T ?F k, k)) ` (p \<inter> s) \<union> (p - s)"
   2.140 +
   2.141 +    { fix X assume X_eq: "X = ?E \<or> X = ?F"
   2.142 +      let ?T = "(\<lambda>(x, k). (T X k, k))"
   2.143 +      let ?p = "?T ` (p \<inter> s) \<union> (p - s)"
   2.144 +
   2.145 +      have in_s: "(x, k) \<in> s \<Longrightarrow> T X k \<in> k \<inter> C X m" for x k
   2.146 +        using someI_ex[of "\<lambda>x. x \<in> k \<inter> C X m"] X_eq unfolding ex_in_conv by (auto simp: T_def s_def)
   2.147 +
   2.148 +      { fix x k assume "(x, k) \<in> p" "(x, k) \<in> s"
   2.149 +        have k: "k \<subseteq> ball x (1 / (3 * Suc m))"
   2.150 +          using \<open>d' fine p\<close>[THEN fineD, OF \<open>(x, k) \<in> p\<close>] by (auto simp: d'_def)
   2.151 +        then have "x \<in> ball (T X k) (1 / (3 * Suc m))"
   2.152 +          using in_s[OF \<open>(x, k) \<in> s\<close>] by (auto simp: C_def subset_eq dist_commute)
   2.153 +        then have "ball x (1 / (3 * Suc m)) \<subseteq> ball (T X k) (1 / Suc m)"
   2.154 +          by (rule ball_trans) (auto simp: divide_simps)
   2.155 +        with k in_s[OF \<open>(x, k) \<in> s\<close>] have "k \<subseteq> d (T X k)"
   2.156 +          by (auto simp: C_def) }
   2.157 +      then have "d fine ?p"
   2.158 +        using \<open>d fine p\<close> by (auto intro!: fineI)
   2.159 +      moreover
   2.160 +      have "?p tagged_division_of cbox x y"
   2.161 +      proof (rule tagged_division_ofI)
   2.162 +        show "finite ?p"
   2.163 +          using p(1) by auto
   2.164 +      next
   2.165 +        fix z k assume *: "(z, k) \<in> ?p"
   2.166 +        then consider "(z, k) \<in> p" "(z, k) \<notin> s"
   2.167 +          | x' where "(x', k) \<in> p" "(x', k) \<in> s" "z = T X k"
   2.168 +          by (auto simp: T_def)
   2.169 +        then have "z \<in> k \<and> k \<subseteq> cbox x y \<and> (\<exists>a b. k = cbox a b)"
   2.170 +          using p(1) by cases (auto dest: in_s)
   2.171 +        then show "z \<in> k" "k \<subseteq> cbox x y" "\<exists>a b. k = cbox a b"
   2.172 +          by auto
   2.173 +      next
   2.174 +        fix z k z' k' assume "(z, k) \<in> ?p" "(z', k') \<in> ?p" "(z, k) \<noteq> (z', k')"
   2.175 +        with tagged_division_ofD(5)[OF p(1), of _ k _ k']
   2.176 +        show "interior k \<inter> interior k' = {}"
   2.177 +          by (auto simp: T_def dest: in_s)
   2.178 +      next
   2.179 +        have "{k. \<exists>x. (x, k) \<in> ?p} = {k. \<exists>x. (x, k) \<in> p}"
   2.180 +          by (auto simp: T_def image_iff Bex_def)
   2.181 +        then show "\<Union>{k. \<exists>x. (x, k) \<in> ?p} = cbox x y"
   2.182 +          using p(1) by auto
   2.183 +      qed
   2.184 +      ultimately have I: "norm ((\<Sum>(x, k)\<in>?p. content k *\<^sub>R f x) - I) < e"
   2.185 +        using integral_f by auto
   2.186 +
   2.187 +      have "(\<Sum>(x, k)\<in>?p. content k *\<^sub>R f x) =
   2.188 +        (\<Sum>(x, k)\<in>?T ` (p \<inter> s). content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p - s. content k *\<^sub>R f x)"
   2.189 +        using p(1)[THEN tagged_division_ofD(1)]
   2.190 +        by (safe intro!: setsum.union_inter_neutral) (auto simp: s_def T_def)
   2.191 +      also have "(\<Sum>(x, k)\<in>?T ` (p \<inter> s). content k *\<^sub>R f x) = (\<Sum>(x, k)\<in>p \<inter> s. content k *\<^sub>R f (T X k))"
   2.192 +      proof (subst setsum.reindex_nontrivial, safe)
   2.193 +        fix x1 x2 k assume 1: "(x1, k) \<in> p" "(x1, k) \<in> s" and 2: "(x2, k) \<in> p" "(x2, k) \<in> s"
   2.194 +          and eq: "content k *\<^sub>R f (T X k) \<noteq> 0"
   2.195 +        with tagged_division_ofD(5)[OF p(1), of x1 k x2 k] tagged_division_ofD(4)[OF p(1), of x1 k]
   2.196 +        show "x1 = x2"
   2.197 +          by (auto simp: content_eq_0_interior)
   2.198 +      qed (use p in \<open>auto intro!: setsum.cong\<close>)
   2.199 +      finally have eq: "(\<Sum>(x, k)\<in>?p. content k *\<^sub>R f x) =
   2.200 +        (\<Sum>(x, k)\<in>p \<inter> s. content k *\<^sub>R f (T X k)) + (\<Sum>(x, k)\<in>p - s. content k *\<^sub>R f x)" .
   2.201 +
   2.202 +      have in_T: "(x, k) \<in> s \<Longrightarrow> T X k \<in> X" for x k
   2.203 +        using in_s[of x k] by (auto simp: C_def)
   2.204 +
   2.205 +      note I eq in_T }
   2.206 +    note parts = this
   2.207 +
   2.208 +    have p_in_L: "(x, k) \<in> p \<Longrightarrow> k \<in> sets ?L" for x k
   2.209 +      using tagged_division_ofD(3, 4)[OF p(1), of x k] by (auto simp: sets_restrict_space)
   2.210 +
   2.211 +    have [simp]: "finite p"
   2.212 +      using tagged_division_ofD(1)[OF p(1)] .
   2.213 +
   2.214 +    have "(M - 3*e) * (b - a) \<le> (\<Sum>(x, k)\<in>p \<inter> s. content k) * (b - a)"
   2.215 +    proof (intro mult_right_mono)
   2.216 +      have fin: "?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}}) < \<infinity>" for X
   2.217 +        using \<open>?\<mu> E < \<infinity>\<close> by (rule le_less_trans[rotated]) (auto intro!: emeasure_mono \<open>E \<in> sets ?L\<close>)
   2.218 +      have sets: "(E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}}) \<in> sets ?L" for X
   2.219 +        using tagged_division_ofD(1)[OF p(1)] by (intro sets.Diff \<open>E \<in> sets ?L\<close> sets.finite_Union sets.Int) (auto intro: p_in_L)
   2.220 +      { fix X assume "X \<subseteq> E" "M - e < ?\<mu>' (C X m)"
   2.221 +        have "M - e \<le> ?\<mu>' (C X m)"
   2.222 +          by (rule less_imp_le) fact
   2.223 +        also have "\<dots> \<le> ?\<mu>' (E - (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}}))"
   2.224 +        proof (intro outer_measure_of_mono subsetI)
   2.225 +          fix v assume "v \<in> C X m"
   2.226 +          then have "v \<in> cbox x y" "v \<in> E"
   2.227 +            using \<open>E \<subseteq> space ?L\<close> \<open>X \<subseteq> E\<close> by (auto simp: space_restrict_space C_def)
   2.228 +          then obtain z k where "(z, k) \<in> p" "v \<in> k"
   2.229 +            using tagged_division_ofD(6)[OF p(1), symmetric] by auto
   2.230 +          then show "v \<in> E - E \<inter> (\<Union>{k\<in>snd`p. k \<inter> C X m = {}})"
   2.231 +            using \<open>v \<in> C X m\<close> \<open>v \<in> E\<close> by auto
   2.232 +        qed
   2.233 +        also have "\<dots> = ?\<mu> E - ?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}})"
   2.234 +          using \<open>E \<in> sets ?L\<close> fin[of X] sets[of X] by (auto intro!: emeasure_Diff)
   2.235 +        finally have "?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}}) \<le> e"
   2.236 +          using \<open>0 < e\<close> e_less_M apply (cases "?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C X m = {}})")
   2.237 +          by (auto simp add: \<open>?\<mu> E = M\<close> ennreal_minus ennreal_le_iff2)
   2.238 +        note this }
   2.239 +      note upper_bound = this
   2.240 +
   2.241 +      have "?\<mu> (E \<inter> \<Union>(snd`(p - s))) =
   2.242 +        ?\<mu> ((E \<inter> \<Union>{k\<in>snd`p. k \<inter> C ?E m = {}}) \<union> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C ?F m = {}}))"
   2.243 +        by (intro arg_cong[where f="?\<mu>"]) (auto simp: s_def image_def Bex_def)
   2.244 +      also have "\<dots> \<le> ?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C ?E m = {}}) + ?\<mu> (E \<inter> \<Union>{k\<in>snd`p. k \<inter> C ?F m = {}})"
   2.245 +        using sets[of ?E] sets[of ?F] M_minus_e by (intro emeasure_subadditive) auto
   2.246 +      also have "\<dots> \<le> e + ennreal e"
   2.247 +        using upper_bound[of ?E] upper_bound[of ?F] M_minus_e by (intro add_mono) auto
   2.248 +      finally have "?\<mu> E - 2*e \<le> ?\<mu> (E - (E \<inter> \<Union>(snd`(p - s))))"
   2.249 +        using \<open>0 < e\<close> \<open>E \<in> sets ?L\<close> tagged_division_ofD(1)[OF p(1)]
   2.250 +        by (subst emeasure_Diff)
   2.251 +           (auto simp: ennreal_plus[symmetric] top_unique simp del: ennreal_plus
   2.252 +                 intro!: sets.Int sets.finite_UN ennreal_mono_minus intro: p_in_L)
   2.253 +      also have "\<dots> \<le> ?\<mu> (\<Union>x\<in>p \<inter> s. snd x)"
   2.254 +      proof (safe intro!: emeasure_mono subsetI)
   2.255 +        fix v assume "v \<in> E" and not: "v \<notin> (\<Union>x\<in>p \<inter> s. snd x)"
   2.256 +        then have "v \<in> cbox x y"
   2.257 +          using \<open>E \<subseteq> space ?L\<close> by (auto simp: space_restrict_space)
   2.258 +        then obtain z k where "(z, k) \<in> p" "v \<in> k"
   2.259 +          using tagged_division_ofD(6)[OF p(1), symmetric] by auto
   2.260 +        with not show "v \<in> UNION (p - s) snd"
   2.261 +          by (auto intro!: bexI[of _ "(z, k)"] elim: ballE[of _ _ "(z, k)"])
   2.262 +      qed (auto intro!: sets.Int sets.finite_UN ennreal_mono_minus intro: p_in_L)
   2.263 +      also have "\<dots> = measure ?L (\<Union>x\<in>p \<inter> s. snd x)"
   2.264 +        by (auto intro!: emeasure_eq_ennreal_measure)
   2.265 +      finally have "M - 2 * e \<le> measure ?L (\<Union>x\<in>p \<inter> s. snd x)"
   2.266 +        unfolding \<open>?\<mu> E = M\<close> using \<open>0 < e\<close> by (simp add: ennreal_minus)
   2.267 +      also have "measure ?L (\<Union>x\<in>p \<inter> s. snd x) = content (\<Union>x\<in>p \<inter> s. snd x)"
   2.268 +        using tagged_division_ofD(1,3,4) [OF p(1)]
   2.269 +        by (intro content_eq_L[symmetric])
   2.270 +           (fastforce intro!: sets.finite_UN UN_least del: subsetI)+
   2.271 +      also have "content (\<Union>x\<in>p \<inter> s. snd x) \<le> (\<Sum>k\<in>p \<inter> s. content (snd k))"
   2.272 +        using p(1) by (auto simp: emeasure_lborel_cbox_eq intro!: measure_subadditive_finite
   2.273 +                            dest!: p(1)[THEN tagged_division_ofD(4)])
   2.274 +      finally show "M - 3 * e \<le> (\<Sum>(x, y)\<in>p \<inter> s. content y)"
   2.275 +        using \<open>0 < e\<close> by (simp add: split_beta)
   2.276 +    qed (use \<open>a < b\<close> in auto)
   2.277 +    also have "\<dots> = (\<Sum>(x, k)\<in>p \<inter> s. content k * (b - a))"
   2.278 +      by (simp add: setsum_distrib_right split_beta')
   2.279 +    also have "\<dots> \<le> (\<Sum>(x, k)\<in>p \<inter> s. content k * (f (T ?F k) - f (T ?E k)))"
   2.280 +      using parts(3) by (auto intro!: setsum_mono mult_left_mono diff_mono)
   2.281 +    also have "\<dots> = (\<Sum>(x, k)\<in>p \<inter> s. content k * f (T ?F k)) - (\<Sum>(x, k)\<in>p \<inter> s. content k * f (T ?E k))"
   2.282 +      by (auto intro!: setsum.cong simp: field_simps setsum_subtractf[symmetric])
   2.283 +    also have "\<dots> = (\<Sum>(x, k)\<in>?B. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>?A. content k *\<^sub>R f x)"
   2.284 +      by (subst (1 2) parts) auto
   2.285 +    also have "\<dots> \<le> norm ((\<Sum>(x, k)\<in>?B. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>?A. content k *\<^sub>R f x))"
   2.286 +      by auto
   2.287 +    also have "\<dots> \<le> e + e"
   2.288 +      using parts(1)[of ?E] parts(1)[of ?F] by (intro norm_diff_triangle_le[of _ I]) auto
   2.289 +    finally show False
   2.290 +      using \<open>2 * e < (b - a) * (M - e * 3)\<close> by (auto simp: field_simps)
   2.291 +  qed
   2.292 +  moreover have "?\<mu>' ?E \<le> ?\<mu> E" "?\<mu>' ?F \<le> ?\<mu> E"
   2.293 +    unfolding outer_measure_of_eq[OF \<open>E \<in> sets ?L\<close>, symmetric] by (auto intro!: outer_measure_of_mono)
   2.294 +  ultimately show "min (?\<mu>' ?E) (?\<mu>' ?F) < ?\<mu> E"
   2.295 +    unfolding min_less_iff_disj by (auto simp: less_le)
   2.296 +qed
   2.297 +
   2.298 +lemma has_integral_implies_lebesgue_measurable_real:
   2.299 +  fixes f :: "'a :: euclidean_space \<Rightarrow> real"
   2.300 +  assumes f: "(f has_integral I) \<Omega>"
   2.301 +  shows "(\<lambda>x. f x * indicator \<Omega> x) \<in> lebesgue \<rightarrow>\<^sub>M borel"
   2.302 +proof -
   2.303 +  define B :: "nat \<Rightarrow> 'a set" where "B n = cbox (- real n *\<^sub>R One) (real n *\<^sub>R One)" for n
   2.304 +  show "(\<lambda>x. f x * indicator \<Omega> x) \<in> lebesgue \<rightarrow>\<^sub>M borel"
   2.305 +  proof (rule measurable_piecewise_restrict)
   2.306 +    have "(\<Union>n. box (- real n *\<^sub>R One) (real n *\<^sub>R One)) \<subseteq> UNION UNIV B"
   2.307 +      unfolding B_def by (intro UN_mono box_subset_cbox order_refl)
   2.308 +    then show "countable (range B)" "space lebesgue \<subseteq> UNION UNIV B"
   2.309 +      by (auto simp: B_def UN_box_eq_UNIV)
   2.310 +  next
   2.311 +    fix \<Omega>' assume "\<Omega>' \<in> range B"
   2.312 +    then obtain n where \<Omega>': "\<Omega>' = B n" by auto
   2.313 +    then show "\<Omega>' \<inter> space lebesgue \<in> sets lebesgue"
   2.314 +      by (auto simp: B_def)
   2.315 +
   2.316 +    have "f integrable_on \<Omega>"
   2.317 +      using f by auto
   2.318 +    then have "(\<lambda>x. f x * indicator \<Omega> x) integrable_on \<Omega>"
   2.319 +      by (auto simp: integrable_on_def cong: has_integral_cong)
   2.320 +    then have "(\<lambda>x. f x * indicator \<Omega> x) integrable_on (\<Omega> \<union> B n)"
   2.321 +      by (rule integrable_on_superset[rotated 2]) auto
   2.322 +    then have "(\<lambda>x. f x * indicator \<Omega> x) integrable_on B n"
   2.323 +      unfolding B_def by (rule integrable_on_subcbox) auto
   2.324 +    then show "(\<lambda>x. f x * indicator \<Omega> x) \<in> lebesgue_on \<Omega>' \<rightarrow>\<^sub>M borel"
   2.325 +      unfolding B_def \<Omega>' by (auto intro: has_integral_implies_lebesgue_measurable_cbox simp: integrable_on_def)
   2.326 +  qed
   2.327 +qed
   2.328 +
   2.329 +lemma has_integral_implies_lebesgue_measurable:
   2.330 +  fixes f :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
   2.331 +  assumes f: "(f has_integral I) \<Omega>"
   2.332 +  shows "(\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> lebesgue \<rightarrow>\<^sub>M borel"
   2.333 +proof (intro borel_measurable_euclidean_space[where 'c='b, THEN iffD2] ballI)
   2.334 +  fix i :: "'b" assume "i \<in> Basis"
   2.335 +  have "(\<lambda>x. (f x \<bullet> i) * indicator \<Omega> x) \<in> borel_measurable (completion lborel)"
   2.336 +    using has_integral_linear[OF f bounded_linear_inner_left, of i]
   2.337 +    by (intro has_integral_implies_lebesgue_measurable_real) (auto simp: comp_def)
   2.338 +  then show "(\<lambda>x. indicator \<Omega> x *\<^sub>R f x \<bullet> i) \<in> borel_measurable (completion lborel)"
   2.339 +    by (simp add: ac_simps)
   2.340 +qed
   2.341 +
   2.342  subsection \<open>Equivalence Lebesgue integral on @{const lborel} and HK-integral\<close>
   2.343  
   2.344  lemma has_integral_measure_lborel:
   2.345 @@ -347,6 +680,82 @@
   2.346    qed
   2.347  qed
   2.348  
   2.349 +lemma has_integral_AE:
   2.350 +  assumes ae: "AE x in lborel. x \<in> \<Omega> \<longrightarrow> f x = g x"
   2.351 +  shows "(f has_integral x) \<Omega> = (g has_integral x) \<Omega>"
   2.352 +proof -
   2.353 +  from ae obtain N
   2.354 +    where N: "N \<in> sets borel" "emeasure lborel N = 0" "{x. \<not> (x \<in> \<Omega> \<longrightarrow> f x = g x)} \<subseteq> N"
   2.355 +    by (auto elim!: AE_E)
   2.356 +  then have not_N: "AE x in lborel. x \<notin> N"
   2.357 +    by (simp add: AE_iff_measurable)
   2.358 +  show ?thesis
   2.359 +  proof (rule has_integral_spike_eq[symmetric])
   2.360 +    show "\<forall>x\<in>\<Omega> - N. f x = g x" using N(3) by auto
   2.361 +    show "negligible N"
   2.362 +      unfolding negligible_def
   2.363 +    proof (intro allI)
   2.364 +      fix a b :: "'a"
   2.365 +      let ?F = "\<lambda>x::'a. if x \<in> cbox a b then indicator N x else 0 :: real"
   2.366 +      have "integrable lborel ?F = integrable lborel (\<lambda>x::'a. 0::real)"
   2.367 +        using not_N N(1) by (intro integrable_cong_AE) auto
   2.368 +      moreover have "(LINT x|lborel. ?F x) = (LINT x::'a|lborel. 0::real)"
   2.369 +        using not_N N(1) by (intro integral_cong_AE) auto
   2.370 +      ultimately have "(?F has_integral 0) UNIV"
   2.371 +        using has_integral_integral_real[of ?F] by simp
   2.372 +      then show "(indicator N has_integral (0::real)) (cbox a b)"
   2.373 +        unfolding has_integral_restrict_univ .
   2.374 +    qed
   2.375 +  qed
   2.376 +qed
   2.377 +
   2.378 +lemma nn_integral_has_integral_lebesgue:
   2.379 +  fixes f :: "'a::euclidean_space \<Rightarrow> real"
   2.380 +  assumes nonneg: "\<And>x. 0 \<le> f x" and I: "(f has_integral I) \<Omega>"
   2.381 +  shows "integral\<^sup>N lborel (\<lambda>x. indicator \<Omega> x * f x) = I"
   2.382 +proof -
   2.383 +  from I have "(\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> lebesgue \<rightarrow>\<^sub>M borel"
   2.384 +    by (rule has_integral_implies_lebesgue_measurable)
   2.385 +  then obtain f' :: "'a \<Rightarrow> real"
   2.386 +    where [measurable]: "f' \<in> borel \<rightarrow>\<^sub>M borel" and eq: "AE x in lborel. indicator \<Omega> x * f x = f' x"
   2.387 +    by (auto dest: completion_ex_borel_measurable_real)
   2.388 +
   2.389 +  from I have "((\<lambda>x. abs (indicator \<Omega> x * f x)) has_integral I) UNIV"
   2.390 +    using nonneg by (simp add: indicator_def if_distrib[of "\<lambda>x. x * f y" for y] cong: if_cong)
   2.391 +  also have "((\<lambda>x. abs (indicator \<Omega> x * f x)) has_integral I) UNIV \<longleftrightarrow> ((\<lambda>x. abs (f' x)) has_integral I) UNIV"
   2.392 +    using eq by (intro has_integral_AE) auto
   2.393 +  finally have "integral\<^sup>N lborel (\<lambda>x. abs (f' x)) = I"
   2.394 +    by (rule nn_integral_has_integral_lborel[rotated 2]) auto
   2.395 +  also have "integral\<^sup>N lborel (\<lambda>x. abs (f' x)) = integral\<^sup>N lborel (\<lambda>x. abs (indicator \<Omega> x * f x))"
   2.396 +    using eq by (intro nn_integral_cong_AE) auto
   2.397 +  finally show ?thesis
   2.398 +    using nonneg by auto
   2.399 +qed
   2.400 +
   2.401 +lemma has_integral_iff_nn_integral_lebesgue:
   2.402 +  assumes f: "\<And>x. 0 \<le> f x"
   2.403 +  shows "(f has_integral r) UNIV \<longleftrightarrow> (f \<in> lebesgue \<rightarrow>\<^sub>M borel \<and> integral\<^sup>N lebesgue f = r \<and> 0 \<le> r)" (is "?I = ?N")
   2.404 +proof
   2.405 +  assume ?I
   2.406 +  have "0 \<le> r"
   2.407 +    using has_integral_nonneg[OF \<open>?I\<close>] f by auto
   2.408 +  then show ?N
   2.409 +    using nn_integral_has_integral_lebesgue[OF f \<open>?I\<close>]
   2.410 +      has_integral_implies_lebesgue_measurable[OF \<open>?I\<close>]
   2.411 +    by (auto simp: nn_integral_completion)
   2.412 +next
   2.413 +  assume ?N
   2.414 +  then obtain f' where f': "f' \<in> borel \<rightarrow>\<^sub>M borel" "AE x in lborel. f x = f' x"
   2.415 +    by (auto dest: completion_ex_borel_measurable_real)
   2.416 +  moreover have "(\<integral>\<^sup>+ x. ennreal \<bar>f' x\<bar> \<partial>lborel) = (\<integral>\<^sup>+ x. ennreal \<bar>f x\<bar> \<partial>lborel)"
   2.417 +    using f' by (intro nn_integral_cong_AE) auto
   2.418 +  moreover have "((\<lambda>x. \<bar>f' x\<bar>) has_integral r) UNIV \<longleftrightarrow> ((\<lambda>x. \<bar>f x\<bar>) has_integral r) UNIV"
   2.419 +    using f' by (intro has_integral_AE) auto
   2.420 +  moreover note nn_integral_has_integral[of "\<lambda>x. \<bar>f' x\<bar>" r] \<open>?N\<close>
   2.421 +  ultimately show ?I
   2.422 +    using f by (auto simp: nn_integral_completion)
   2.423 +qed
   2.424 +
   2.425  context
   2.426    fixes f::"'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   2.427  begin
     3.1 --- a/src/HOL/Analysis/Henstock_Kurzweil_Integration.thy	Thu Sep 22 15:56:37 2016 +0100
     3.2 +++ b/src/HOL/Analysis/Henstock_Kurzweil_Integration.thy	Fri Sep 23 10:26:04 2016 +0200
     3.3 @@ -1393,10 +1393,7 @@
     3.4  proof (rule tagged_division_ofI)
     3.5    note assm = tagged_division_ofD[OF assms(2)[rule_format]]
     3.6    show "finite (\<Union>(pfn ` iset))"
     3.7 -    apply (rule finite_Union)
     3.8 -    using assms
     3.9 -    apply auto
    3.10 -    done
    3.11 +    using assms by auto
    3.12    have "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>(pfn ` iset)} = \<Union>((\<lambda>i. \<Union>{k. \<exists>x. (x, k) \<in> pfn i}) ` iset)"
    3.13      by blast
    3.14    also have "\<dots> = \<Union>iset"
    3.15 @@ -1936,8 +1933,7 @@
    3.16  definition has_integral_compact_interval (infixr "has'_integral'_compact'_interval" 46)
    3.17    where "(f has_integral_compact_interval y) i \<longleftrightarrow>
    3.18      (\<forall>e>0. \<exists>d. gauge d \<and>
    3.19 -      (\<forall>p. p tagged_division_of i \<and> d fine p \<longrightarrow>
    3.20 -        norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - y) < e))"
    3.21 +      (\<forall>p. p tagged_division_of i \<and> d fine p \<longrightarrow> norm ((\<Sum>(x,k)\<in>p. content k *\<^sub>R f x) - y) < e))"
    3.22  
    3.23  definition has_integral ::
    3.24      "('n::euclidean_space \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'b \<Rightarrow> 'n set \<Rightarrow> bool"
     4.1 --- a/src/HOL/Analysis/Measure_Space.thy	Thu Sep 22 15:56:37 2016 +0100
     4.2 +++ b/src/HOL/Analysis/Measure_Space.thy	Fri Sep 23 10:26:04 2016 +0200
     4.3 @@ -551,6 +551,28 @@
     4.4         (insert finite A, auto intro: INF_lower emeasure_mono)
     4.5  qed
     4.6  
     4.7 +lemma INF_emeasure_decseq':
     4.8 +  assumes A: "\<And>i. A i \<in> sets M" and "decseq A"
     4.9 +  and finite: "\<exists>i. emeasure M (A i) \<noteq> \<infinity>"
    4.10 +  shows "(INF n. emeasure M (A n)) = emeasure M (\<Inter>i. A i)"
    4.11 +proof -
    4.12 +  from finite obtain i where i: "emeasure M (A i) < \<infinity>"
    4.13 +    by (auto simp: less_top)
    4.14 +  have fin: "i \<le> j \<Longrightarrow> emeasure M (A j) < \<infinity>" for j
    4.15 +    by (rule le_less_trans[OF emeasure_mono i]) (auto intro!: decseqD[OF \<open>decseq A\<close>] A)
    4.16 +
    4.17 +  have "(INF n. emeasure M (A n)) = (INF n. emeasure M (A (n + i)))"
    4.18 +  proof (rule INF_eq)
    4.19 +    show "\<exists>j\<in>UNIV. emeasure M (A (j + i)) \<le> emeasure M (A i')" for i'
    4.20 +      by (intro bexI[of _ i'] emeasure_mono decseqD[OF \<open>decseq A\<close>] A) auto
    4.21 +  qed auto
    4.22 +  also have "\<dots> = emeasure M (INF n. (A (n + i)))"
    4.23 +    using A \<open>decseq A\<close> fin by (intro INF_emeasure_decseq) (auto simp: decseq_def less_top)
    4.24 +  also have "(INF n. (A (n + i))) = (INF n. A n)"
    4.25 +    by (meson INF_eq UNIV_I assms(2) decseqD le_add1)
    4.26 +  finally show ?thesis .
    4.27 +qed
    4.28 +
    4.29  lemma emeasure_INT_decseq_subset:
    4.30    fixes F :: "nat \<Rightarrow> 'a set"
    4.31    assumes I: "I \<noteq> {}" and F: "\<And>i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> i \<le> j \<Longrightarrow> F j \<subseteq> F i"
     5.1 --- a/src/HOL/Library/Extended_Nonnegative_Real.thy	Thu Sep 22 15:56:37 2016 +0100
     5.2 +++ b/src/HOL/Library/Extended_Nonnegative_Real.thy	Fri Sep 23 10:26:04 2016 +0200
     5.3 @@ -1574,6 +1574,19 @@
     5.4      done
     5.5    done
     5.6  
     5.7 +lemma ennreal_Inf_countable_INF:
     5.8 +  "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ennreal. decseq f \<and> range f \<subseteq> A \<and> Inf A = (INF i. f i)"
     5.9 +  including ennreal.lifting
    5.10 +  unfolding decseq_def
    5.11 +  apply transfer
    5.12 +  subgoal for A
    5.13 +    using Inf_countable_INF[of A]
    5.14 +    apply (clarsimp simp add: decseq_def[symmetric])
    5.15 +    subgoal for f
    5.16 +      by (intro exI[of _ f]) auto
    5.17 +    done
    5.18 +  done
    5.19 +
    5.20  lemma ennreal_SUP_countable_SUP:
    5.21    "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ennreal. range f \<subseteq> g`A \<and> SUPREMUM A g = SUPREMUM UNIV f"
    5.22    using ennreal_Sup_countable_SUP [of "g`A"] by auto
     6.1 --- a/src/HOL/Library/Extended_Real.thy	Thu Sep 22 15:56:37 2016 +0100
     6.2 +++ b/src/HOL/Library/Extended_Real.thy	Fri Sep 23 10:26:04 2016 +0200
     6.3 @@ -2228,6 +2228,16 @@
     6.4      by auto
     6.5  qed
     6.6  
     6.7 +lemma Inf_countable_INF:
     6.8 +  assumes "A \<noteq> {}" shows "\<exists>f::nat \<Rightarrow> ereal. decseq f \<and> range f \<subseteq> A \<and> Inf A = (INF i. f i)"
     6.9 +proof -
    6.10 +  obtain f where "incseq f" "range f \<subseteq> uminus`A" "Sup (uminus`A) = (SUP i. f i)"
    6.11 +    using Sup_countable_SUP[of "uminus ` A"] \<open>A \<noteq> {}\<close> by auto
    6.12 +  then show ?thesis
    6.13 +    by (intro exI[of _ "\<lambda>x. - f x"])
    6.14 +       (auto simp: ereal_Sup_uminus_image_eq ereal_INF_uminus_eq eq_commute[of "- _"])
    6.15 +qed
    6.16 +
    6.17  lemma SUP_countable_SUP:
    6.18    "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> g`A \<and> SUPREMUM A g = SUPREMUM UNIV f"
    6.19    using Sup_countable_SUP [of "g`A"] by auto