src/HOL/Probability/Lebesgue_Measure.thy
author hoelzl
Fri Nov 16 18:45:57 2012 +0100 (2012-11-16)
changeset 50104 de19856feb54
parent 50003 8c213922ed49
child 50105 65d5b18e1626
permissions -rw-r--r--
move theorems to be more generally useable
     1 (*  Title:      HOL/Probability/Lebesgue_Measure.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Robert Himmelmann, TU München
     4 *)
     5 
     6 header {* Lebsegue measure *}
     7 
     8 theory Lebesgue_Measure
     9   imports Finite_Product_Measure
    10 begin
    11 
    12 lemma bchoice_iff: "(\<forall>x\<in>A. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x\<in>A. P x (f x))"
    13   by metis
    14 
    15 lemma absolutely_integrable_on_indicator[simp]:
    16   fixes A :: "'a::ordered_euclidean_space set"
    17   shows "((indicator A :: _ \<Rightarrow> real) absolutely_integrable_on X) \<longleftrightarrow>
    18     (indicator A :: _ \<Rightarrow> real) integrable_on X"
    19   unfolding absolutely_integrable_on_def by simp
    20 
    21 lemma has_integral_indicator_UNIV:
    22   fixes s A :: "'a::ordered_euclidean_space set" and x :: real
    23   shows "((indicator (s \<inter> A) :: 'a\<Rightarrow>real) has_integral x) UNIV = ((indicator s :: _\<Rightarrow>real) has_integral x) A"
    24 proof -
    25   have "(\<lambda>x. if x \<in> A then indicator s x else 0) = (indicator (s \<inter> A) :: _\<Rightarrow>real)"
    26     by (auto simp: fun_eq_iff indicator_def)
    27   then show ?thesis
    28     unfolding has_integral_restrict_univ[where s=A, symmetric] by simp
    29 qed
    30 
    31 lemma
    32   fixes s a :: "'a::ordered_euclidean_space set"
    33   shows integral_indicator_UNIV:
    34     "integral UNIV (indicator (s \<inter> A) :: 'a\<Rightarrow>real) = integral A (indicator s :: _\<Rightarrow>real)"
    35   and integrable_indicator_UNIV:
    36     "(indicator (s \<inter> A) :: 'a\<Rightarrow>real) integrable_on UNIV \<longleftrightarrow> (indicator s :: 'a\<Rightarrow>real) integrable_on A"
    37   unfolding integral_def integrable_on_def has_integral_indicator_UNIV by auto
    38 
    39 subsection {* Standard Cubes *}
    40 
    41 definition cube :: "nat \<Rightarrow> 'a::ordered_euclidean_space set" where
    42   "cube n \<equiv> {\<chi>\<chi> i. - real n .. \<chi>\<chi> i. real n}"
    43 
    44 lemma borel_cube[intro]: "cube n \<in> sets borel"
    45   unfolding cube_def by auto
    46 
    47 lemma cube_closed[intro]: "closed (cube n)"
    48   unfolding cube_def by auto
    49 
    50 lemma cube_subset[intro]: "n \<le> N \<Longrightarrow> cube n \<subseteq> cube N"
    51   by (fastforce simp: eucl_le[where 'a='a] cube_def)
    52 
    53 lemma cube_subset_iff:
    54   "cube n \<subseteq> cube N \<longleftrightarrow> n \<le> N"
    55 proof
    56   assume subset: "cube n \<subseteq> (cube N::'a set)"
    57   then have "((\<chi>\<chi> i. real n)::'a) \<in> cube N"
    58     using DIM_positive[where 'a='a]
    59     by (fastforce simp: cube_def eucl_le[where 'a='a])
    60   then show "n \<le> N"
    61     by (fastforce simp: cube_def eucl_le[where 'a='a])
    62 next
    63   assume "n \<le> N" then show "cube n \<subseteq> (cube N::'a set)" by (rule cube_subset)
    64 qed
    65 
    66 lemma ball_subset_cube:"ball (0::'a::ordered_euclidean_space) (real n) \<subseteq> cube n"
    67   unfolding cube_def subset_eq mem_interval apply safe unfolding euclidean_lambda_beta'
    68 proof- fix x::'a and i assume as:"x\<in>ball 0 (real n)" "i<DIM('a)"
    69   thus "- real n \<le> x $$ i" "real n \<ge> x $$ i"
    70     using component_le_norm[of x i] by(auto simp: dist_norm)
    71 qed
    72 
    73 lemma mem_big_cube: obtains n where "x \<in> cube n"
    74 proof- from reals_Archimedean2[of "norm x"] guess n ..
    75   thus ?thesis apply-apply(rule that[where n=n])
    76     apply(rule ball_subset_cube[unfolded subset_eq,rule_format])
    77     by (auto simp add:dist_norm)
    78 qed
    79 
    80 lemma cube_subset_Suc[intro]: "cube n \<subseteq> cube (Suc n)"
    81   unfolding cube_def subset_eq apply safe unfolding mem_interval apply auto done
    82 
    83 lemma has_integral_interval_cube:
    84   fixes a b :: "'a::ordered_euclidean_space"
    85   shows "(indicator {a .. b} has_integral
    86     content ({\<chi>\<chi> i. max (- real n) (a $$ i) .. \<chi>\<chi> i. min (real n) (b $$ i)} :: 'a set)) (cube n)"
    87     (is "(?I has_integral content ?R) (cube n)")
    88 proof -
    89   let "{?N .. ?P}" = ?R
    90   have [simp]: "(\<lambda>x. if x \<in> cube n then ?I x else 0) = indicator ?R"
    91     by (auto simp: indicator_def cube_def fun_eq_iff eucl_le[where 'a='a])
    92   have "(?I has_integral content ?R) (cube n) \<longleftrightarrow> (indicator ?R has_integral content ?R) UNIV"
    93     unfolding has_integral_restrict_univ[where s="cube n", symmetric] by simp
    94   also have "\<dots> \<longleftrightarrow> ((\<lambda>x. 1) has_integral content ?R) ?R"
    95     unfolding indicator_def [abs_def] has_integral_restrict_univ ..
    96   finally show ?thesis
    97     using has_integral_const[of "1::real" "?N" "?P"] by simp
    98 qed
    99 
   100 subsection {* Lebesgue measure *}
   101 
   102 definition lebesgue :: "'a::ordered_euclidean_space measure" where
   103   "lebesgue = measure_of UNIV {A. \<forall>n. (indicator A :: 'a \<Rightarrow> real) integrable_on cube n}
   104     (\<lambda>A. SUP n. ereal (integral (cube n) (indicator A)))"
   105 
   106 lemma space_lebesgue[simp]: "space lebesgue = UNIV"
   107   unfolding lebesgue_def by simp
   108 
   109 lemma lebesgueI: "(\<And>n. (indicator A :: _ \<Rightarrow> real) integrable_on cube n) \<Longrightarrow> A \<in> sets lebesgue"
   110   unfolding lebesgue_def by simp
   111 
   112 lemma sigma_algebra_lebesgue:
   113   defines "leb \<equiv> {A. \<forall>n. (indicator A :: 'a::ordered_euclidean_space \<Rightarrow> real) integrable_on cube n}"
   114   shows "sigma_algebra UNIV leb"
   115 proof (safe intro!: sigma_algebra_iff2[THEN iffD2])
   116   fix A assume A: "A \<in> leb"
   117   moreover have "indicator (UNIV - A) = (\<lambda>x. 1 - indicator A x :: real)"
   118     by (auto simp: fun_eq_iff indicator_def)
   119   ultimately show "UNIV - A \<in> leb"
   120     using A by (auto intro!: integrable_sub simp: cube_def leb_def)
   121 next
   122   fix n show "{} \<in> leb"
   123     by (auto simp: cube_def indicator_def[abs_def] leb_def)
   124 next
   125   fix A :: "nat \<Rightarrow> _" assume A: "range A \<subseteq> leb"
   126   have "\<forall>n. (indicator (\<Union>i. A i) :: _ \<Rightarrow> real) integrable_on cube n" (is "\<forall>n. ?g integrable_on _")
   127   proof (intro dominated_convergence[where g="?g"] ballI allI)
   128     fix k n show "(indicator (\<Union>i<k. A i) :: _ \<Rightarrow> real) integrable_on cube n"
   129     proof (induct k)
   130       case (Suc k)
   131       have *: "(\<Union> i<Suc k. A i) = (\<Union> i<k. A i) \<union> A k"
   132         unfolding lessThan_Suc UN_insert by auto
   133       have *: "(\<lambda>x. max (indicator (\<Union> i<k. A i) x) (indicator (A k) x) :: real) =
   134           indicator (\<Union> i<Suc k. A i)" (is "(\<lambda>x. max (?f x) (?g x)) = _")
   135         by (auto simp: fun_eq_iff * indicator_def)
   136       show ?case
   137         using absolutely_integrable_max[of ?f "cube n" ?g] A Suc
   138         by (simp add: * leb_def subset_eq)
   139     qed auto
   140   qed (auto intro: LIMSEQ_indicator_UN simp: cube_def)
   141   then show "(\<Union>i. A i) \<in> leb" by (auto simp: leb_def)
   142 qed simp
   143 
   144 lemma sets_lebesgue: "sets lebesgue = {A. \<forall>n. (indicator A :: _ \<Rightarrow> real) integrable_on cube n}"
   145   unfolding lebesgue_def sigma_algebra.sets_measure_of_eq[OF sigma_algebra_lebesgue] ..
   146 
   147 lemma lebesgueD: "A \<in> sets lebesgue \<Longrightarrow> (indicator A :: _ \<Rightarrow> real) integrable_on cube n"
   148   unfolding sets_lebesgue by simp
   149 
   150 lemma emeasure_lebesgue:
   151   assumes "A \<in> sets lebesgue"
   152   shows "emeasure lebesgue A = (SUP n. ereal (integral (cube n) (indicator A)))"
   153     (is "_ = ?\<mu> A")
   154 proof (rule emeasure_measure_of[OF lebesgue_def])
   155   have *: "indicator {} = (\<lambda>x. 0 :: real)" by (simp add: fun_eq_iff)
   156   show "positive (sets lebesgue) ?\<mu>"
   157   proof (unfold positive_def, intro conjI ballI)
   158     show "?\<mu> {} = 0" by (simp add: integral_0 *)
   159     fix A :: "'a set" assume "A \<in> sets lebesgue" then show "0 \<le> ?\<mu> A"
   160       by (auto intro!: SUP_upper2 Integration.integral_nonneg simp: sets_lebesgue)
   161   qed
   162 next
   163   show "countably_additive (sets lebesgue) ?\<mu>"
   164   proof (intro countably_additive_def[THEN iffD2] allI impI)
   165     fix A :: "nat \<Rightarrow> 'a set" assume rA: "range A \<subseteq> sets lebesgue" "disjoint_family A"
   166     then have A[simp, intro]: "\<And>i n. (indicator (A i) :: _ \<Rightarrow> real) integrable_on cube n"
   167       by (auto dest: lebesgueD)
   168     let ?m = "\<lambda>n i. integral (cube n) (indicator (A i) :: _\<Rightarrow>real)"
   169     let ?M = "\<lambda>n I. integral (cube n) (indicator (\<Union>i\<in>I. A i) :: _\<Rightarrow>real)"
   170     have nn[simp, intro]: "\<And>i n. 0 \<le> ?m n i" by (auto intro!: Integration.integral_nonneg)
   171     assume "(\<Union>i. A i) \<in> sets lebesgue"
   172     then have UN_A[simp, intro]: "\<And>i n. (indicator (\<Union>i. A i) :: _ \<Rightarrow> real) integrable_on cube n"
   173       by (auto simp: sets_lebesgue)
   174     show "(\<Sum>n. ?\<mu> (A n)) = ?\<mu> (\<Union>i. A i)"
   175     proof (subst suminf_SUP_eq, safe intro!: incseq_SucI) 
   176       fix i n show "ereal (?m n i) \<le> ereal (?m (Suc n) i)"
   177         using cube_subset[of n "Suc n"] by (auto intro!: integral_subset_le incseq_SucI)
   178     next
   179       fix i n show "0 \<le> ereal (?m n i)"
   180         using rA unfolding lebesgue_def
   181         by (auto intro!: SUP_upper2 integral_nonneg)
   182     next
   183       show "(SUP n. \<Sum>i. ereal (?m n i)) = (SUP n. ereal (?M n UNIV))"
   184       proof (intro arg_cong[where f="SUPR UNIV"] ext sums_unique[symmetric] sums_ereal[THEN iffD2] sums_def[THEN iffD2])
   185         fix n
   186         have "\<And>m. (UNION {..<m} A) \<in> sets lebesgue" using rA by auto
   187         from lebesgueD[OF this]
   188         have "(\<lambda>m. ?M n {..< m}) ----> ?M n UNIV"
   189           (is "(\<lambda>m. integral _ (?A m)) ----> ?I")
   190           by (intro dominated_convergence(2)[where f="?A" and h="\<lambda>x. 1::real"])
   191              (auto intro: LIMSEQ_indicator_UN simp: cube_def)
   192         moreover
   193         { fix m have *: "(\<Sum>x<m. ?m n x) = ?M n {..< m}"
   194           proof (induct m)
   195             case (Suc m)
   196             have "(\<Union>i<m. A i) \<in> sets lebesgue" using rA by auto
   197             then have "(indicator (\<Union>i<m. A i) :: _\<Rightarrow>real) integrable_on (cube n)"
   198               by (auto dest!: lebesgueD)
   199             moreover
   200             have "(\<Union>i<m. A i) \<inter> A m = {}"
   201               using rA(2)[unfolded disjoint_family_on_def, THEN bspec, of m]
   202               by auto
   203             then have "\<And>x. indicator (\<Union>i<Suc m. A i) x =
   204               indicator (\<Union>i<m. A i) x + (indicator (A m) x :: real)"
   205               by (auto simp: indicator_add lessThan_Suc ac_simps)
   206             ultimately show ?case
   207               using Suc A by (simp add: Integration.integral_add[symmetric])
   208           qed auto }
   209         ultimately show "(\<lambda>m. \<Sum>x = 0..<m. ?m n x) ----> ?M n UNIV"
   210           by (simp add: atLeast0LessThan)
   211       qed
   212     qed
   213   qed
   214 qed (auto, fact)
   215 
   216 lemma lebesgueI_borel[intro, simp]:
   217   fixes s::"'a::ordered_euclidean_space set"
   218   assumes "s \<in> sets borel" shows "s \<in> sets lebesgue"
   219 proof -
   220   have "s \<in> sigma_sets (space lebesgue) (range (\<lambda>(a, b). {a .. (b :: 'a\<Colon>ordered_euclidean_space)}))"
   221     using assms by (simp add: borel_eq_atLeastAtMost)
   222   also have "\<dots> \<subseteq> sets lebesgue"
   223   proof (safe intro!: sigma_sets_subset lebesgueI)
   224     fix n :: nat and a b :: 'a
   225     let ?N = "\<chi>\<chi> i. max (- real n) (a $$ i)"
   226     let ?P = "\<chi>\<chi> i. min (real n) (b $$ i)"
   227     show "(indicator {a..b} :: 'a\<Rightarrow>real) integrable_on cube n"
   228       unfolding integrable_on_def
   229       using has_integral_interval_cube[of a b] by auto
   230   qed
   231   finally show ?thesis .
   232 qed
   233 
   234 lemma borel_measurable_lebesgueI:
   235   "f \<in> borel_measurable borel \<Longrightarrow> f \<in> borel_measurable lebesgue"
   236   unfolding measurable_def by simp
   237 
   238 lemma lebesgueI_negligible[dest]: fixes s::"'a::ordered_euclidean_space set"
   239   assumes "negligible s" shows "s \<in> sets lebesgue"
   240   using assms by (force simp: cube_def integrable_on_def negligible_def intro!: lebesgueI)
   241 
   242 lemma lmeasure_eq_0:
   243   fixes S :: "'a::ordered_euclidean_space set"
   244   assumes "negligible S" shows "emeasure lebesgue S = 0"
   245 proof -
   246   have "\<And>n. integral (cube n) (indicator S :: 'a\<Rightarrow>real) = 0"
   247     unfolding lebesgue_integral_def using assms
   248     by (intro integral_unique some1_equality ex_ex1I)
   249        (auto simp: cube_def negligible_def)
   250   then show ?thesis
   251     using assms by (simp add: emeasure_lebesgue lebesgueI_negligible)
   252 qed
   253 
   254 lemma lmeasure_iff_LIMSEQ:
   255   assumes A: "A \<in> sets lebesgue" and "0 \<le> m"
   256   shows "emeasure lebesgue A = ereal m \<longleftrightarrow> (\<lambda>n. integral (cube n) (indicator A :: _ \<Rightarrow> real)) ----> m"
   257 proof (subst emeasure_lebesgue[OF A], intro SUP_eq_LIMSEQ)
   258   show "mono (\<lambda>n. integral (cube n) (indicator A::_=>real))"
   259     using cube_subset assms by (intro monoI integral_subset_le) (auto dest!: lebesgueD)
   260 qed
   261 
   262 lemma lmeasure_finite_has_integral:
   263   fixes s :: "'a::ordered_euclidean_space set"
   264   assumes "s \<in> sets lebesgue" "emeasure lebesgue s = ereal m"
   265   shows "(indicator s has_integral m) UNIV"
   266 proof -
   267   let ?I = "indicator :: 'a set \<Rightarrow> 'a \<Rightarrow> real"
   268   have "0 \<le> m"
   269     using emeasure_nonneg[of lebesgue s] `emeasure lebesgue s = ereal m` by simp
   270   have **: "(?I s) integrable_on UNIV \<and> (\<lambda>k. integral UNIV (?I (s \<inter> cube k))) ----> integral UNIV (?I s)"
   271   proof (intro monotone_convergence_increasing allI ballI)
   272     have LIMSEQ: "(\<lambda>n. integral (cube n) (?I s)) ----> m"
   273       using assms(2) unfolding lmeasure_iff_LIMSEQ[OF assms(1) `0 \<le> m`] .
   274     { fix n have "integral (cube n) (?I s) \<le> m"
   275         using cube_subset assms
   276         by (intro incseq_le[where L=m] LIMSEQ incseq_def[THEN iffD2] integral_subset_le allI impI)
   277            (auto dest!: lebesgueD) }
   278     moreover
   279     { fix n have "0 \<le> integral (cube n) (?I s)"
   280       using assms by (auto dest!: lebesgueD intro!: Integration.integral_nonneg) }
   281     ultimately
   282     show "bounded {integral UNIV (?I (s \<inter> cube k)) |k. True}"
   283       unfolding bounded_def
   284       apply (rule_tac exI[of _ 0])
   285       apply (rule_tac exI[of _ m])
   286       by (auto simp: dist_real_def integral_indicator_UNIV)
   287     fix k show "?I (s \<inter> cube k) integrable_on UNIV"
   288       unfolding integrable_indicator_UNIV using assms by (auto dest!: lebesgueD)
   289     fix x show "?I (s \<inter> cube k) x \<le> ?I (s \<inter> cube (Suc k)) x"
   290       using cube_subset[of k "Suc k"] by (auto simp: indicator_def)
   291   next
   292     fix x :: 'a
   293     from mem_big_cube obtain k where k: "x \<in> cube k" .
   294     { fix n have "?I (s \<inter> cube (n + k)) x = ?I s x"
   295       using k cube_subset[of k "n + k"] by (auto simp: indicator_def) }
   296     note * = this
   297     show "(\<lambda>k. ?I (s \<inter> cube k) x) ----> ?I s x"
   298       by (rule LIMSEQ_offset[where k=k]) (auto simp: *)
   299   qed
   300   note ** = conjunctD2[OF this]
   301   have m: "m = integral UNIV (?I s)"
   302     apply (intro LIMSEQ_unique[OF _ **(2)])
   303     using assms(2) unfolding lmeasure_iff_LIMSEQ[OF assms(1) `0 \<le> m`] integral_indicator_UNIV .
   304   show ?thesis
   305     unfolding m by (intro integrable_integral **)
   306 qed
   307 
   308 lemma lmeasure_finite_integrable: assumes s: "s \<in> sets lebesgue" and "emeasure lebesgue s \<noteq> \<infinity>"
   309   shows "(indicator s :: _ \<Rightarrow> real) integrable_on UNIV"
   310 proof (cases "emeasure lebesgue s")
   311   case (real m)
   312   with lmeasure_finite_has_integral[OF `s \<in> sets lebesgue` this] emeasure_nonneg[of lebesgue s]
   313   show ?thesis unfolding integrable_on_def by auto
   314 qed (insert assms emeasure_nonneg[of lebesgue s], auto)
   315 
   316 lemma has_integral_lebesgue: assumes "((indicator s :: _\<Rightarrow>real) has_integral m) UNIV"
   317   shows "s \<in> sets lebesgue"
   318 proof (intro lebesgueI)
   319   let ?I = "indicator :: 'a set \<Rightarrow> 'a \<Rightarrow> real"
   320   fix n show "(?I s) integrable_on cube n" unfolding cube_def
   321   proof (intro integrable_on_subinterval)
   322     show "(?I s) integrable_on UNIV"
   323       unfolding integrable_on_def using assms by auto
   324   qed auto
   325 qed
   326 
   327 lemma has_integral_lmeasure: assumes "((indicator s :: _\<Rightarrow>real) has_integral m) UNIV"
   328   shows "emeasure lebesgue s = ereal m"
   329 proof (intro lmeasure_iff_LIMSEQ[THEN iffD2])
   330   let ?I = "indicator :: 'a set \<Rightarrow> 'a \<Rightarrow> real"
   331   show "s \<in> sets lebesgue" using has_integral_lebesgue[OF assms] .
   332   show "0 \<le> m" using assms by (rule has_integral_nonneg) auto
   333   have "(\<lambda>n. integral UNIV (?I (s \<inter> cube n))) ----> integral UNIV (?I s)"
   334   proof (intro dominated_convergence(2) ballI)
   335     show "(?I s) integrable_on UNIV" unfolding integrable_on_def using assms by auto
   336     fix n show "?I (s \<inter> cube n) integrable_on UNIV"
   337       unfolding integrable_indicator_UNIV using `s \<in> sets lebesgue` by (auto dest: lebesgueD)
   338     fix x show "norm (?I (s \<inter> cube n) x) \<le> ?I s x" by (auto simp: indicator_def)
   339   next
   340     fix x :: 'a
   341     from mem_big_cube obtain k where k: "x \<in> cube k" .
   342     { fix n have "?I (s \<inter> cube (n + k)) x = ?I s x"
   343       using k cube_subset[of k "n + k"] by (auto simp: indicator_def) }
   344     note * = this
   345     show "(\<lambda>k. ?I (s \<inter> cube k) x) ----> ?I s x"
   346       by (rule LIMSEQ_offset[where k=k]) (auto simp: *)
   347   qed
   348   then show "(\<lambda>n. integral (cube n) (?I s)) ----> m"
   349     unfolding integral_unique[OF assms] integral_indicator_UNIV by simp
   350 qed
   351 
   352 lemma has_integral_iff_lmeasure:
   353   "(indicator A has_integral m) UNIV \<longleftrightarrow> (A \<in> sets lebesgue \<and> emeasure lebesgue A = ereal m)"
   354 proof
   355   assume "(indicator A has_integral m) UNIV"
   356   with has_integral_lmeasure[OF this] has_integral_lebesgue[OF this]
   357   show "A \<in> sets lebesgue \<and> emeasure lebesgue A = ereal m"
   358     by (auto intro: has_integral_nonneg)
   359 next
   360   assume "A \<in> sets lebesgue \<and> emeasure lebesgue A = ereal m"
   361   then show "(indicator A has_integral m) UNIV" by (intro lmeasure_finite_has_integral) auto
   362 qed
   363 
   364 lemma lmeasure_eq_integral: assumes "(indicator s::_\<Rightarrow>real) integrable_on UNIV"
   365   shows "emeasure lebesgue s = ereal (integral UNIV (indicator s))"
   366   using assms unfolding integrable_on_def
   367 proof safe
   368   fix y :: real assume "(indicator s has_integral y) UNIV"
   369   from this[unfolded has_integral_iff_lmeasure] integral_unique[OF this]
   370   show "emeasure lebesgue s = ereal (integral UNIV (indicator s))" by simp
   371 qed
   372 
   373 lemma lebesgue_simple_function_indicator:
   374   fixes f::"'a::ordered_euclidean_space \<Rightarrow> ereal"
   375   assumes f:"simple_function lebesgue f"
   376   shows "f = (\<lambda>x. (\<Sum>y \<in> f ` UNIV. y * indicator (f -` {y}) x))"
   377   by (rule, subst simple_function_indicator_representation[OF f]) auto
   378 
   379 lemma integral_eq_lmeasure:
   380   "(indicator s::_\<Rightarrow>real) integrable_on UNIV \<Longrightarrow> integral UNIV (indicator s) = real (emeasure lebesgue s)"
   381   by (subst lmeasure_eq_integral) (auto intro!: integral_nonneg)
   382 
   383 lemma lmeasure_finite: assumes "(indicator s::_\<Rightarrow>real) integrable_on UNIV" shows "emeasure lebesgue s \<noteq> \<infinity>"
   384   using lmeasure_eq_integral[OF assms] by auto
   385 
   386 lemma negligible_iff_lebesgue_null_sets:
   387   "negligible A \<longleftrightarrow> A \<in> null_sets lebesgue"
   388 proof
   389   assume "negligible A"
   390   from this[THEN lebesgueI_negligible] this[THEN lmeasure_eq_0]
   391   show "A \<in> null_sets lebesgue" by auto
   392 next
   393   assume A: "A \<in> null_sets lebesgue"
   394   then have *:"((indicator A) has_integral (0::real)) UNIV" using lmeasure_finite_has_integral[of A]
   395     by (auto simp: null_sets_def)
   396   show "negligible A" unfolding negligible_def
   397   proof (intro allI)
   398     fix a b :: 'a
   399     have integrable: "(indicator A :: _\<Rightarrow>real) integrable_on {a..b}"
   400       by (intro integrable_on_subinterval has_integral_integrable) (auto intro: *)
   401     then have "integral {a..b} (indicator A) \<le> (integral UNIV (indicator A) :: real)"
   402       using * by (auto intro!: integral_subset_le)
   403     moreover have "(0::real) \<le> integral {a..b} (indicator A)"
   404       using integrable by (auto intro!: integral_nonneg)
   405     ultimately have "integral {a..b} (indicator A) = (0::real)"
   406       using integral_unique[OF *] by auto
   407     then show "(indicator A has_integral (0::real)) {a..b}"
   408       using integrable_integral[OF integrable] by simp
   409   qed
   410 qed
   411 
   412 lemma lmeasure_UNIV[intro]: "emeasure lebesgue (UNIV::'a::ordered_euclidean_space set) = \<infinity>"
   413 proof (simp add: emeasure_lebesgue, intro SUP_PInfty bexI)
   414   fix n :: nat
   415   have "indicator UNIV = (\<lambda>x::'a. 1 :: real)" by auto
   416   moreover
   417   { have "real n \<le> (2 * real n) ^ DIM('a)"
   418     proof (cases n)
   419       case 0 then show ?thesis by auto
   420     next
   421       case (Suc n')
   422       have "real n \<le> (2 * real n)^1" by auto
   423       also have "(2 * real n)^1 \<le> (2 * real n) ^ DIM('a)"
   424         using Suc DIM_positive[where 'a='a] by (intro power_increasing) (auto simp: real_of_nat_Suc)
   425       finally show ?thesis .
   426     qed }
   427   ultimately show "ereal (real n) \<le> ereal (integral (cube n) (indicator UNIV::'a\<Rightarrow>real))"
   428     using integral_const DIM_positive[where 'a='a]
   429     by (auto simp: cube_def content_closed_interval_cases setprod_constant)
   430 qed simp
   431 
   432 lemma lmeasure_complete: "A \<subseteq> B \<Longrightarrow> B \<in> null_sets lebesgue \<Longrightarrow> A \<in> null_sets lebesgue"
   433   unfolding negligible_iff_lebesgue_null_sets[symmetric] by (auto simp: negligible_subset)
   434 
   435 lemma
   436   fixes a b ::"'a::ordered_euclidean_space"
   437   shows lmeasure_atLeastAtMost[simp]: "emeasure lebesgue {a..b} = ereal (content {a..b})"
   438 proof -
   439   have "(indicator (UNIV \<inter> {a..b})::_\<Rightarrow>real) integrable_on UNIV"
   440     unfolding integrable_indicator_UNIV by (simp add: integrable_const indicator_def [abs_def])
   441   from lmeasure_eq_integral[OF this] show ?thesis unfolding integral_indicator_UNIV
   442     by (simp add: indicator_def [abs_def])
   443 qed
   444 
   445 lemma lmeasure_singleton[simp]:
   446   fixes a :: "'a::ordered_euclidean_space" shows "emeasure lebesgue {a} = 0"
   447   using lmeasure_atLeastAtMost[of a a] by simp
   448 
   449 lemma AE_lebesgue_singleton:
   450   fixes a :: "'a::ordered_euclidean_space" shows "AE x in lebesgue. x \<noteq> a"
   451   by (rule AE_I[where N="{a}"]) auto
   452 
   453 declare content_real[simp]
   454 
   455 lemma
   456   fixes a b :: real
   457   shows lmeasure_real_greaterThanAtMost[simp]:
   458     "emeasure lebesgue {a <.. b} = ereal (if a \<le> b then b - a else 0)"
   459 proof -
   460   have "emeasure lebesgue {a <.. b} = emeasure lebesgue {a .. b}"
   461     using AE_lebesgue_singleton[of a]
   462     by (intro emeasure_eq_AE) auto
   463   then show ?thesis by auto
   464 qed
   465 
   466 lemma
   467   fixes a b :: real
   468   shows lmeasure_real_atLeastLessThan[simp]:
   469     "emeasure lebesgue {a ..< b} = ereal (if a \<le> b then b - a else 0)"
   470 proof -
   471   have "emeasure lebesgue {a ..< b} = emeasure lebesgue {a .. b}"
   472     using AE_lebesgue_singleton[of b]
   473     by (intro emeasure_eq_AE) auto
   474   then show ?thesis by auto
   475 qed
   476 
   477 lemma
   478   fixes a b :: real
   479   shows lmeasure_real_greaterThanLessThan[simp]:
   480     "emeasure lebesgue {a <..< b} = ereal (if a \<le> b then b - a else 0)"
   481 proof -
   482   have "emeasure lebesgue {a <..< b} = emeasure lebesgue {a .. b}"
   483     using AE_lebesgue_singleton[of a] AE_lebesgue_singleton[of b]
   484     by (intro emeasure_eq_AE) auto
   485   then show ?thesis by auto
   486 qed
   487 
   488 subsection {* Lebesgue-Borel measure *}
   489 
   490 definition "lborel = measure_of UNIV (sets borel) (emeasure lebesgue)"
   491 
   492 lemma
   493   shows space_lborel[simp]: "space lborel = UNIV"
   494   and sets_lborel[simp]: "sets lborel = sets borel"
   495   and measurable_lborel1[simp]: "measurable lborel = measurable borel"
   496   and measurable_lborel2[simp]: "measurable A lborel = measurable A borel"
   497   using sigma_sets_eq[of borel]
   498   by (auto simp add: lborel_def measurable_def[abs_def])
   499 
   500 lemma emeasure_lborel[simp]: "A \<in> sets borel \<Longrightarrow> emeasure lborel A = emeasure lebesgue A"
   501   by (rule emeasure_measure_of[OF lborel_def])
   502      (auto simp: positive_def emeasure_nonneg countably_additive_def intro!: suminf_emeasure)
   503 
   504 interpretation lborel: sigma_finite_measure lborel
   505 proof (default, intro conjI exI[of _ "\<lambda>n. cube n"])
   506   show "range cube \<subseteq> sets lborel" by (auto intro: borel_closed)
   507   { fix x :: 'a have "\<exists>n. x\<in>cube n" using mem_big_cube by auto }
   508   then show "(\<Union>i. cube i) = (space lborel :: 'a set)" using mem_big_cube by auto
   509   show "\<forall>i. emeasure lborel (cube i) \<noteq> \<infinity>" by (simp add: cube_def)
   510 qed
   511 
   512 interpretation lebesgue: sigma_finite_measure lebesgue
   513 proof
   514   from lborel.sigma_finite guess A :: "nat \<Rightarrow> 'a set" ..
   515   then show "\<exists>A::nat \<Rightarrow> 'a set. range A \<subseteq> sets lebesgue \<and> (\<Union>i. A i) = space lebesgue \<and> (\<forall>i. emeasure lebesgue (A i) \<noteq> \<infinity>)"
   516     by (intro exI[of _ A]) (auto simp: subset_eq)
   517 qed
   518 
   519 lemma Int_stable_atLeastAtMost:
   520   fixes x::"'a::ordered_euclidean_space"
   521   shows "Int_stable (range (\<lambda>(a, b::'a). {a..b}))"
   522   by (auto simp: inter_interval Int_stable_def)
   523 
   524 lemma lborel_eqI:
   525   fixes M :: "'a::ordered_euclidean_space measure"
   526   assumes emeasure_eq: "\<And>a b. emeasure M {a .. b} = content {a .. b}"
   527   assumes sets_eq: "sets M = sets borel"
   528   shows "lborel = M"
   529 proof (rule measure_eqI_generator_eq[OF Int_stable_atLeastAtMost])
   530   let ?P = "\<Pi>\<^isub>M i\<in>{..<DIM('a::ordered_euclidean_space)}. lborel"
   531   let ?E = "range (\<lambda>(a, b). {a..b} :: 'a set)"
   532   show "?E \<subseteq> Pow UNIV" "sets lborel = sigma_sets UNIV ?E" "sets M = sigma_sets UNIV ?E"
   533     by (simp_all add: borel_eq_atLeastAtMost sets_eq)
   534 
   535   show "range cube \<subseteq> ?E" unfolding cube_def [abs_def] by auto
   536   { fix x :: 'a have "\<exists>n. x \<in> cube n" using mem_big_cube[of x] by fastforce }
   537   then show "(\<Union>i. cube i :: 'a set) = UNIV" by auto
   538 
   539   { fix i show "emeasure lborel (cube i) \<noteq> \<infinity>" unfolding cube_def by auto }
   540   { fix X assume "X \<in> ?E" then show "emeasure lborel X = emeasure M X"
   541       by (auto simp: emeasure_eq) }
   542 qed
   543 
   544 lemma lebesgue_real_affine:
   545   fixes c :: real assumes "c \<noteq> 0"
   546   shows "lborel = density (distr lborel borel (\<lambda>x. t + c * x)) (\<lambda>_. \<bar>c\<bar>)" (is "_ = ?D")
   547 proof (rule lborel_eqI)
   548   fix a b show "emeasure ?D {a..b} = content {a .. b}"
   549   proof cases
   550     assume "0 < c"
   551     then have "(\<lambda>x. t + c * x) -` {a..b} = {(a - t) / c .. (b - t) / c}"
   552       by (auto simp: field_simps)
   553     with `0 < c` show ?thesis
   554       by (cases "a \<le> b")
   555          (auto simp: field_simps emeasure_density positive_integral_distr positive_integral_cmult
   556                      borel_measurable_indicator' emeasure_distr)
   557   next
   558     assume "\<not> 0 < c" with `c \<noteq> 0` have "c < 0" by auto
   559     then have *: "(\<lambda>x. t + c * x) -` {a..b} = {(b - t) / c .. (a - t) / c}"
   560       by (auto simp: field_simps)
   561     with `c < 0` show ?thesis
   562       by (cases "a \<le> b")
   563          (auto simp: field_simps emeasure_density positive_integral_distr
   564                      positive_integral_cmult borel_measurable_indicator' emeasure_distr)
   565   qed
   566 qed simp
   567 
   568 lemma lebesgue_integral_real_affine:
   569   fixes c :: real assumes c: "c \<noteq> 0" and f: "f \<in> borel_measurable borel"
   570   shows "(\<integral> x. f x \<partial> lborel) = \<bar>c\<bar> * (\<integral> x. f (t + c * x) \<partial>lborel)"
   571   by (subst lebesgue_real_affine[OF c, of t])
   572      (simp add: f integral_density integral_distr lebesgue_integral_cmult)
   573 
   574 subsection {* Lebesgue integrable implies Gauge integrable *}
   575 
   576 lemma simple_function_has_integral:
   577   fixes f::"'a::ordered_euclidean_space \<Rightarrow> ereal"
   578   assumes f:"simple_function lebesgue f"
   579   and f':"range f \<subseteq> {0..<\<infinity>}"
   580   and om:"\<And>x. x \<in> range f \<Longrightarrow> emeasure lebesgue (f -` {x} \<inter> UNIV) = \<infinity> \<Longrightarrow> x = 0"
   581   shows "((\<lambda>x. real (f x)) has_integral (real (integral\<^isup>S lebesgue f))) UNIV"
   582   unfolding simple_integral_def space_lebesgue
   583 proof (subst lebesgue_simple_function_indicator)
   584   let ?M = "\<lambda>x. emeasure lebesgue (f -` {x} \<inter> UNIV)"
   585   let ?F = "\<lambda>x. indicator (f -` {x})"
   586   { fix x y assume "y \<in> range f"
   587     from subsetD[OF f' this] have "y * ?F y x = ereal (real y * ?F y x)"
   588       by (cases rule: ereal2_cases[of y "?F y x"])
   589          (auto simp: indicator_def one_ereal_def split: split_if_asm) }
   590   moreover
   591   { fix x assume x: "x\<in>range f"
   592     have "x * ?M x = real x * real (?M x)"
   593     proof cases
   594       assume "x \<noteq> 0" with om[OF x] have "?M x \<noteq> \<infinity>" by auto
   595       with subsetD[OF f' x] f[THEN simple_functionD(2)] show ?thesis
   596         by (cases rule: ereal2_cases[of x "?M x"]) auto
   597     qed simp }
   598   ultimately
   599   have "((\<lambda>x. real (\<Sum>y\<in>range f. y * ?F y x)) has_integral real (\<Sum>x\<in>range f. x * ?M x)) UNIV \<longleftrightarrow>
   600     ((\<lambda>x. \<Sum>y\<in>range f. real y * ?F y x) has_integral (\<Sum>x\<in>range f. real x * real (?M x))) UNIV"
   601     by simp
   602   also have \<dots>
   603   proof (intro has_integral_setsum has_integral_cmult_real lmeasure_finite_has_integral
   604                real_of_ereal_pos emeasure_nonneg ballI)
   605     show *: "finite (range f)" "\<And>y. f -` {y} \<in> sets lebesgue"
   606       using simple_functionD[OF f] by auto
   607     fix y assume "real y \<noteq> 0" "y \<in> range f"
   608     with * om[OF this(2)] show "emeasure lebesgue (f -` {y}) = ereal (real (?M y))"
   609       by (auto simp: ereal_real)
   610   qed
   611   finally show "((\<lambda>x. real (\<Sum>y\<in>range f. y * ?F y x)) has_integral real (\<Sum>x\<in>range f. x * ?M x)) UNIV" .
   612 qed fact
   613 
   614 lemma simple_function_has_integral':
   615   fixes f::"'a::ordered_euclidean_space \<Rightarrow> ereal"
   616   assumes f: "simple_function lebesgue f" "\<And>x. 0 \<le> f x"
   617   and i: "integral\<^isup>S lebesgue f \<noteq> \<infinity>"
   618   shows "((\<lambda>x. real (f x)) has_integral (real (integral\<^isup>S lebesgue f))) UNIV"
   619 proof -
   620   let ?f = "\<lambda>x. if x \<in> f -` {\<infinity>} then 0 else f x"
   621   note f(1)[THEN simple_functionD(2)]
   622   then have [simp, intro]: "\<And>X. f -` X \<in> sets lebesgue" by auto
   623   have f': "simple_function lebesgue ?f"
   624     using f by (intro simple_function_If_set) auto
   625   have rng: "range ?f \<subseteq> {0..<\<infinity>}" using f by auto
   626   have "AE x in lebesgue. f x = ?f x"
   627     using simple_integral_PInf[OF f i]
   628     by (intro AE_I[where N="f -` {\<infinity>} \<inter> space lebesgue"]) auto
   629   from f(1) f' this have eq: "integral\<^isup>S lebesgue f = integral\<^isup>S lebesgue ?f"
   630     by (rule simple_integral_cong_AE)
   631   have real_eq: "\<And>x. real (f x) = real (?f x)" by auto
   632 
   633   show ?thesis
   634     unfolding eq real_eq
   635   proof (rule simple_function_has_integral[OF f' rng])
   636     fix x assume x: "x \<in> range ?f" and inf: "emeasure lebesgue (?f -` {x} \<inter> UNIV) = \<infinity>"
   637     have "x * emeasure lebesgue (?f -` {x} \<inter> UNIV) = (\<integral>\<^isup>S y. x * indicator (?f -` {x}) y \<partial>lebesgue)"
   638       using f'[THEN simple_functionD(2)]
   639       by (simp add: simple_integral_cmult_indicator)
   640     also have "\<dots> \<le> integral\<^isup>S lebesgue f"
   641       using f'[THEN simple_functionD(2)] f
   642       by (intro simple_integral_mono simple_function_mult simple_function_indicator)
   643          (auto split: split_indicator)
   644     finally show "x = 0" unfolding inf using i subsetD[OF rng x] by (auto split: split_if_asm)
   645   qed
   646 qed
   647 
   648 lemma positive_integral_has_integral:
   649   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> ereal"
   650   assumes f: "f \<in> borel_measurable lebesgue" "range f \<subseteq> {0..<\<infinity>}" "integral\<^isup>P lebesgue f \<noteq> \<infinity>"
   651   shows "((\<lambda>x. real (f x)) has_integral (real (integral\<^isup>P lebesgue f))) UNIV"
   652 proof -
   653   from borel_measurable_implies_simple_function_sequence'[OF f(1)]
   654   guess u . note u = this
   655   have SUP_eq: "\<And>x. (SUP i. u i x) = f x"
   656     using u(4) f(2)[THEN subsetD] by (auto split: split_max)
   657   let ?u = "\<lambda>i x. real (u i x)"
   658   note u_eq = positive_integral_eq_simple_integral[OF u(1,5), symmetric]
   659   { fix i
   660     note u_eq
   661     also have "integral\<^isup>P lebesgue (u i) \<le> (\<integral>\<^isup>+x. max 0 (f x) \<partial>lebesgue)"
   662       by (intro positive_integral_mono) (auto intro: SUP_upper simp: u(4)[symmetric])
   663     finally have "integral\<^isup>S lebesgue (u i) \<noteq> \<infinity>"
   664       unfolding positive_integral_max_0 using f by auto }
   665   note u_fin = this
   666   then have u_int: "\<And>i. (?u i has_integral real (integral\<^isup>S lebesgue (u i))) UNIV"
   667     by (rule simple_function_has_integral'[OF u(1,5)])
   668   have "\<forall>x. \<exists>r\<ge>0. f x = ereal r"
   669   proof
   670     fix x from f(2) have "0 \<le> f x" "f x \<noteq> \<infinity>" by (auto simp: subset_eq)
   671     then show "\<exists>r\<ge>0. f x = ereal r" by (cases "f x") auto
   672   qed
   673   from choice[OF this] obtain f' where f': "f = (\<lambda>x. ereal (f' x))" "\<And>x. 0 \<le> f' x" by auto
   674 
   675   have "\<forall>i. \<exists>r. \<forall>x. 0 \<le> r x \<and> u i x = ereal (r x)"
   676   proof
   677     fix i show "\<exists>r. \<forall>x. 0 \<le> r x \<and> u i x = ereal (r x)"
   678     proof (intro choice allI)
   679       fix i x have "u i x \<noteq> \<infinity>" using u(3)[of i] by (auto simp: image_iff) metis
   680       then show "\<exists>r\<ge>0. u i x = ereal r" using u(5)[of i x] by (cases "u i x") auto
   681     qed
   682   qed
   683   from choice[OF this] obtain u' where
   684       u': "u = (\<lambda>i x. ereal (u' i x))" "\<And>i x. 0 \<le> u' i x" by (auto simp: fun_eq_iff)
   685 
   686   have convergent: "f' integrable_on UNIV \<and>
   687     (\<lambda>k. integral UNIV (u' k)) ----> integral UNIV f'"
   688   proof (intro monotone_convergence_increasing allI ballI)
   689     show int: "\<And>k. (u' k) integrable_on UNIV"
   690       using u_int unfolding integrable_on_def u' by auto
   691     show "\<And>k x. u' k x \<le> u' (Suc k) x" using u(2,3,5)
   692       by (auto simp: incseq_Suc_iff le_fun_def image_iff u' intro!: real_of_ereal_positive_mono)
   693     show "\<And>x. (\<lambda>k. u' k x) ----> f' x"
   694       using SUP_eq u(2)
   695       by (intro SUP_eq_LIMSEQ[THEN iffD1]) (auto simp: u' f' incseq_mono incseq_Suc_iff le_fun_def)
   696     show "bounded {integral UNIV (u' k)|k. True}"
   697     proof (safe intro!: bounded_realI)
   698       fix k
   699       have "\<bar>integral UNIV (u' k)\<bar> = integral UNIV (u' k)"
   700         by (intro abs_of_nonneg integral_nonneg int ballI u')
   701       also have "\<dots> = real (integral\<^isup>S lebesgue (u k))"
   702         using u_int[THEN integral_unique] by (simp add: u')
   703       also have "\<dots> = real (integral\<^isup>P lebesgue (u k))"
   704         using positive_integral_eq_simple_integral[OF u(1,5)] by simp
   705       also have "\<dots> \<le> real (integral\<^isup>P lebesgue f)" using f
   706         by (auto intro!: real_of_ereal_positive_mono positive_integral_positive
   707              positive_integral_mono SUP_upper simp: SUP_eq[symmetric])
   708       finally show "\<bar>integral UNIV (u' k)\<bar> \<le> real (integral\<^isup>P lebesgue f)" .
   709     qed
   710   qed
   711 
   712   have "integral\<^isup>P lebesgue f = ereal (integral UNIV f')"
   713   proof (rule tendsto_unique[OF trivial_limit_sequentially])
   714     have "(\<lambda>i. integral\<^isup>S lebesgue (u i)) ----> (SUP i. integral\<^isup>P lebesgue (u i))"
   715       unfolding u_eq by (intro LIMSEQ_ereal_SUPR incseq_positive_integral u)
   716     also note positive_integral_monotone_convergence_SUP
   717       [OF u(2)  borel_measurable_simple_function[OF u(1)] u(5), symmetric]
   718     finally show "(\<lambda>k. integral\<^isup>S lebesgue (u k)) ----> integral\<^isup>P lebesgue f"
   719       unfolding SUP_eq .
   720 
   721     { fix k
   722       have "0 \<le> integral\<^isup>S lebesgue (u k)"
   723         using u by (auto intro!: simple_integral_positive)
   724       then have "integral\<^isup>S lebesgue (u k) = ereal (real (integral\<^isup>S lebesgue (u k)))"
   725         using u_fin by (auto simp: ereal_real) }
   726     note * = this
   727     show "(\<lambda>k. integral\<^isup>S lebesgue (u k)) ----> ereal (integral UNIV f')"
   728       using convergent using u_int[THEN integral_unique, symmetric]
   729       by (subst *) (simp add: u')
   730   qed
   731   then show ?thesis using convergent by (simp add: f' integrable_integral)
   732 qed
   733 
   734 lemma lebesgue_integral_has_integral:
   735   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   736   assumes f: "integrable lebesgue f"
   737   shows "(f has_integral (integral\<^isup>L lebesgue f)) UNIV"
   738 proof -
   739   let ?n = "\<lambda>x. real (ereal (max 0 (- f x)))" and ?p = "\<lambda>x. real (ereal (max 0 (f x)))"
   740   have *: "f = (\<lambda>x. ?p x - ?n x)" by (auto simp del: ereal_max)
   741   { fix f :: "'a \<Rightarrow> real" have "(\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue) = (\<integral>\<^isup>+ x. ereal (max 0 (f x)) \<partial>lebesgue)"
   742       by (intro positive_integral_cong_pos) (auto split: split_max) }
   743   note eq = this
   744   show ?thesis
   745     unfolding lebesgue_integral_def
   746     apply (subst *)
   747     apply (rule has_integral_sub)
   748     unfolding eq[of f] eq[of "\<lambda>x. - f x"]
   749     apply (safe intro!: positive_integral_has_integral)
   750     using integrableD[OF f]
   751     by (auto simp: zero_ereal_def[symmetric] positive_integral_max_0  split: split_max
   752              intro!: measurable_If)
   753 qed
   754 
   755 lemma lebesgue_simple_integral_eq_borel:
   756   assumes f: "f \<in> borel_measurable borel"
   757   shows "integral\<^isup>S lebesgue f = integral\<^isup>S lborel f"
   758   using f[THEN measurable_sets]
   759   by (auto intro!: setsum_cong arg_cong2[where f="op *"] emeasure_lborel[symmetric]
   760            simp: simple_integral_def)
   761 
   762 lemma lebesgue_positive_integral_eq_borel:
   763   assumes f: "f \<in> borel_measurable borel"
   764   shows "integral\<^isup>P lebesgue f = integral\<^isup>P lborel f"
   765 proof -
   766   from f have "integral\<^isup>P lebesgue (\<lambda>x. max 0 (f x)) = integral\<^isup>P lborel (\<lambda>x. max 0 (f x))"
   767     by (auto intro!: positive_integral_subalgebra[symmetric])
   768   then show ?thesis unfolding positive_integral_max_0 .
   769 qed
   770 
   771 lemma lebesgue_integral_eq_borel:
   772   assumes "f \<in> borel_measurable borel"
   773   shows "integrable lebesgue f \<longleftrightarrow> integrable lborel f" (is ?P)
   774     and "integral\<^isup>L lebesgue f = integral\<^isup>L lborel f" (is ?I)
   775 proof -
   776   have "sets lborel \<subseteq> sets lebesgue" by auto
   777   from integral_subalgebra[of f lborel, OF _ this _ _] assms
   778   show ?P ?I by auto
   779 qed
   780 
   781 lemma borel_integral_has_integral:
   782   fixes f::"'a::ordered_euclidean_space => real"
   783   assumes f:"integrable lborel f"
   784   shows "(f has_integral (integral\<^isup>L lborel f)) UNIV"
   785 proof -
   786   have borel: "f \<in> borel_measurable borel"
   787     using f unfolding integrable_def by auto
   788   from f show ?thesis
   789     using lebesgue_integral_has_integral[of f]
   790     unfolding lebesgue_integral_eq_borel[OF borel] by simp
   791 qed
   792 
   793 lemma positive_integral_lebesgue_has_integral:
   794   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   795   assumes f_borel: "f \<in> borel_measurable lebesgue" and nonneg: "\<And>x. 0 \<le> f x"
   796   assumes I: "(f has_integral I) UNIV"
   797   shows "(\<integral>\<^isup>+x. f x \<partial>lebesgue) = I"
   798 proof -
   799   from f_borel have "(\<lambda>x. ereal (f x)) \<in> borel_measurable lebesgue" by auto
   800   from borel_measurable_implies_simple_function_sequence'[OF this] guess F . note F = this
   801 
   802   have "(\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue) = (SUP i. integral\<^isup>S lebesgue (F i))"
   803     using F
   804     by (subst positive_integral_monotone_convergence_simple)
   805        (simp_all add: positive_integral_max_0 simple_function_def)
   806   also have "\<dots> \<le> ereal I"
   807   proof (rule SUP_least)
   808     fix i :: nat
   809 
   810     { fix z
   811       from F(4)[of z] have "F i z \<le> ereal (f z)"
   812         by (metis SUP_upper UNIV_I ereal_max_0 min_max.sup_absorb2 nonneg)
   813       with F(5)[of i z] have "real (F i z) \<le> f z"
   814         by (cases "F i z") simp_all }
   815     note F_bound = this
   816 
   817     { fix x :: ereal assume x: "x \<noteq> 0" "x \<in> range (F i)"
   818       with F(3,5)[of i] have [simp]: "real x \<noteq> 0"
   819         by (metis image_iff order_eq_iff real_of_ereal_le_0)
   820       let ?s = "(\<lambda>n z. real x * indicator (F i -` {x} \<inter> cube n) z) :: nat \<Rightarrow> 'a \<Rightarrow> real"
   821       have "(\<lambda>z::'a. real x * indicator (F i -` {x}) z) integrable_on UNIV"
   822       proof (rule dominated_convergence(1))
   823         fix n :: nat
   824         have "(\<lambda>z. indicator (F i -` {x} \<inter> cube n) z :: real) integrable_on cube n"
   825           using x F(1)[of i]
   826           by (intro lebesgueD) (auto simp: simple_function_def)
   827         then have cube: "?s n integrable_on cube n"
   828           by (simp add: integrable_on_cmult_iff)
   829         show "?s n integrable_on UNIV"
   830           by (rule integrable_on_superset[OF _ _ cube]) auto
   831       next
   832         show "f integrable_on UNIV"
   833           unfolding integrable_on_def using I by auto
   834       next
   835         fix n from F_bound show "\<forall>x\<in>UNIV. norm (?s n x) \<le> f x"
   836           using nonneg F(5) by (auto split: split_indicator)
   837       next
   838         show "\<forall>z\<in>UNIV. (\<lambda>n. ?s n z) ----> real x * indicator (F i -` {x}) z"
   839         proof
   840           fix z :: 'a
   841           from mem_big_cube[of z] guess j .
   842           then have x: "eventually (\<lambda>n. ?s n z = real x * indicator (F i -` {x}) z) sequentially"
   843             by (auto intro!: eventually_sequentiallyI[where c=j] dest!: cube_subset split: split_indicator)
   844           then show "(\<lambda>n. ?s n z) ----> real x * indicator (F i -` {x}) z"
   845             by (rule Lim_eventually)
   846         qed
   847       qed
   848       then have "(indicator (F i -` {x}) :: 'a \<Rightarrow> real) integrable_on UNIV"
   849         by (simp add: integrable_on_cmult_iff) }
   850     note F_finite = lmeasure_finite[OF this]
   851 
   852     have "((\<lambda>x. real (F i x)) has_integral real (integral\<^isup>S lebesgue (F i))) UNIV"
   853     proof (rule simple_function_has_integral[of "F i"])
   854       show "simple_function lebesgue (F i)"
   855         using F(1) by (simp add: simple_function_def)
   856       show "range (F i) \<subseteq> {0..<\<infinity>}"
   857         using F(3,5)[of i] by (auto simp: image_iff) metis
   858     next
   859       fix x assume "x \<in> range (F i)" "emeasure lebesgue (F i -` {x} \<inter> UNIV) = \<infinity>"
   860       with F_finite[of x] show "x = 0" by auto
   861     qed
   862     from this I have "real (integral\<^isup>S lebesgue (F i)) \<le> I"
   863       by (rule has_integral_le) (intro ballI F_bound)
   864     moreover
   865     { fix x assume x: "x \<in> range (F i)"
   866       with F(3,5)[of i] have "x = 0 \<or> (0 < x \<and> x \<noteq> \<infinity>)"
   867         by (auto  simp: image_iff le_less) metis
   868       with F_finite[OF _ x] x have "x * emeasure lebesgue (F i -` {x} \<inter> UNIV) \<noteq> \<infinity>"
   869         by auto }
   870     then have "integral\<^isup>S lebesgue (F i) \<noteq> \<infinity>"
   871       unfolding simple_integral_def setsum_Pinfty space_lebesgue by blast
   872     moreover have "0 \<le> integral\<^isup>S lebesgue (F i)"
   873       using F(1,5) by (intro simple_integral_positive) (auto simp: simple_function_def)
   874     ultimately show "integral\<^isup>S lebesgue (F i) \<le> ereal I"
   875       by (cases "integral\<^isup>S lebesgue (F i)") auto
   876   qed
   877   also have "\<dots> < \<infinity>" by simp
   878   finally have finite: "(\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue) \<noteq> \<infinity>" by simp
   879   have borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable lebesgue"
   880     using f_borel by (auto intro: borel_measurable_lebesgueI)
   881   from positive_integral_has_integral[OF borel _ finite]
   882   have "(f has_integral real (\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue)) UNIV"
   883     using nonneg by (simp add: subset_eq)
   884   with I have "I = real (\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue)"
   885     by (rule has_integral_unique)
   886   with finite positive_integral_positive[of _ "\<lambda>x. ereal (f x)"] show ?thesis
   887     by (cases "\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue") auto
   888 qed
   889 
   890 lemma has_integral_iff_positive_integral_lebesgue:
   891   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   892   assumes f: "f \<in> borel_measurable lebesgue" "\<And>x. 0 \<le> f x"
   893   shows "(f has_integral I) UNIV \<longleftrightarrow> integral\<^isup>P lebesgue f = I"
   894   using f positive_integral_lebesgue_has_integral[of f I] positive_integral_has_integral[of f]
   895   by (auto simp: subset_eq)
   896 
   897 lemma has_integral_iff_positive_integral_lborel:
   898   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   899   assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x"
   900   shows "(f has_integral I) UNIV \<longleftrightarrow> integral\<^isup>P lborel f = I"
   901   using assms
   902   by (subst has_integral_iff_positive_integral_lebesgue)
   903      (auto simp: borel_measurable_lebesgueI lebesgue_positive_integral_eq_borel)
   904 
   905 subsection {* Equivalence between product spaces and euclidean spaces *}
   906 
   907 definition e2p :: "'a::ordered_euclidean_space \<Rightarrow> (nat \<Rightarrow> real)" where
   908   "e2p x = (\<lambda>i\<in>{..<DIM('a)}. x$$i)"
   909 
   910 definition p2e :: "(nat \<Rightarrow> real) \<Rightarrow> 'a::ordered_euclidean_space" where
   911   "p2e x = (\<chi>\<chi> i. x i)"
   912 
   913 lemma e2p_p2e[simp]:
   914   "x \<in> extensional {..<DIM('a)} \<Longrightarrow> e2p (p2e x::'a::ordered_euclidean_space) = x"
   915   by (auto simp: fun_eq_iff extensional_def p2e_def e2p_def)
   916 
   917 lemma p2e_e2p[simp]:
   918   "p2e (e2p x) = (x::'a::ordered_euclidean_space)"
   919   by (auto simp: euclidean_eq[where 'a='a] p2e_def e2p_def)
   920 
   921 interpretation lborel_product: product_sigma_finite "\<lambda>x. lborel::real measure"
   922   by default
   923 
   924 interpretation lborel_space: finite_product_sigma_finite "\<lambda>x. lborel::real measure" "{..<n}" for n :: nat
   925   by default auto
   926 
   927 lemma sets_product_borel:
   928   assumes I: "finite I"
   929   shows "sets (\<Pi>\<^isub>M i\<in>I. lborel) = sigma_sets (\<Pi>\<^isub>E i\<in>I. UNIV) { \<Pi>\<^isub>E i\<in>I. {..< x i :: real} | x. True}" (is "_ = ?G")
   930 proof (subst sigma_prod_algebra_sigma_eq[where S="\<lambda>_ i::nat. {..<real i}" and E="\<lambda>_. range lessThan", OF I])
   931   show "sigma_sets (space (Pi\<^isub>M I (\<lambda>i. lborel))) {Pi\<^isub>E I F |F. \<forall>i\<in>I. F i \<in> range lessThan} = ?G"
   932     by (intro arg_cong2[where f=sigma_sets]) (auto simp: space_PiM image_iff bchoice_iff)
   933 qed (auto simp: borel_eq_lessThan reals_Archimedean2)
   934 
   935 lemma measurable_e2p[measurable]:
   936   "e2p \<in> measurable (borel::'a::ordered_euclidean_space measure) (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. (lborel :: real measure))"
   937 proof (rule measurable_sigma_sets[OF sets_product_borel])
   938   fix A :: "(nat \<Rightarrow> real) set" assume "A \<in> {\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {..<x i} |x. True} "
   939   then obtain x where  "A = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {..<x i})" by auto
   940   then have "e2p -` A = {..< (\<chi>\<chi> i. x i) :: 'a}"
   941     using DIM_positive by (auto simp add: Pi_iff set_eq_iff e2p_def
   942       euclidean_eq[where 'a='a] eucl_less[where 'a='a])
   943   then show "e2p -` A \<inter> space (borel::'a measure) \<in> sets borel" by simp
   944 qed (auto simp: e2p_def)
   945 
   946 (* FIXME: conversion in measurable prover *)
   947 lemma lborelD_Collect[measurable]: "{x\<in>space borel. P x} \<in> sets borel \<Longrightarrow> {x\<in>space lborel. P x} \<in> sets lborel" by simp
   948 lemma lborelD[measurable]: "A \<in> sets borel \<Longrightarrow> A \<in> sets lborel" by simp
   949 
   950 lemma measurable_p2e[measurable]:
   951   "p2e \<in> measurable (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. (lborel :: real measure))
   952     (borel :: 'a::ordered_euclidean_space measure)"
   953   (is "p2e \<in> measurable ?P _")
   954 proof (safe intro!: borel_measurable_iff_halfspace_le[THEN iffD2])
   955   fix x i
   956   let ?A = "{w \<in> space ?P. (p2e w :: 'a) $$ i \<le> x}"
   957   assume "i < DIM('a)"
   958   then have "?A = (\<Pi>\<^isub>E j\<in>{..<DIM('a)}. if i = j then {.. x} else UNIV)"
   959     using DIM_positive by (auto simp: space_PiM p2e_def split: split_if_asm)
   960   then show "?A \<in> sets ?P"
   961     by auto
   962 qed
   963 
   964 lemma lborel_eq_lborel_space:
   965   "(lborel :: 'a measure) = distr (\<Pi>\<^isub>M i\<in>{..<DIM('a::ordered_euclidean_space)}. lborel) borel p2e"
   966   (is "?B = ?D")
   967 proof (rule lborel_eqI)
   968   show "sets ?D = sets borel" by simp
   969   let ?P = "(\<Pi>\<^isub>M i\<in>{..<DIM('a::ordered_euclidean_space)}. lborel)"
   970   fix a b :: 'a
   971   have *: "p2e -` {a .. b} \<inter> space ?P = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {a $$ i .. b $$ i})"
   972     by (auto simp: Pi_iff eucl_le[where 'a='a] p2e_def space_PiM)
   973   have "emeasure ?P (p2e -` {a..b} \<inter> space ?P) = content {a..b}"
   974   proof cases
   975     assume "{a..b} \<noteq> {}"
   976     then have "a \<le> b"
   977       by (simp add: interval_ne_empty eucl_le[where 'a='a])
   978     then have "emeasure lborel {a..b} = (\<Prod>x<DIM('a). emeasure lborel {a $$ x .. b $$ x})"
   979       by (auto simp: content_closed_interval eucl_le[where 'a='a]
   980                intro!: setprod_ereal[symmetric])
   981     also have "\<dots> = emeasure ?P (p2e -` {a..b} \<inter> space ?P)"
   982       unfolding * by (subst lborel_space.measure_times) auto
   983     finally show ?thesis by simp
   984   qed simp
   985   then show "emeasure ?D {a .. b} = content {a .. b}"
   986     by (simp add: emeasure_distr measurable_p2e)
   987 qed
   988 
   989 lemma borel_fubini_positiv_integral:
   990   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> ereal"
   991   assumes f: "f \<in> borel_measurable borel"
   992   shows "integral\<^isup>P lborel f = \<integral>\<^isup>+x. f (p2e x) \<partial>(\<Pi>\<^isub>M i\<in>{..<DIM('a)}. lborel)"
   993   by (subst lborel_eq_lborel_space) (simp add: positive_integral_distr measurable_p2e f)
   994 
   995 lemma borel_fubini_integrable:
   996   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   997   shows "integrable lborel f \<longleftrightarrow>
   998     integrable (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. lborel) (\<lambda>x. f (p2e x))"
   999     (is "_ \<longleftrightarrow> integrable ?B ?f")
  1000 proof
  1001   assume "integrable lborel f"
  1002   moreover then have f: "f \<in> borel_measurable borel"
  1003     by auto
  1004   moreover with measurable_p2e
  1005   have "f \<circ> p2e \<in> borel_measurable ?B"
  1006     by (rule measurable_comp)
  1007   ultimately show "integrable ?B ?f"
  1008     by (simp add: comp_def borel_fubini_positiv_integral integrable_def)
  1009 next
  1010   assume "integrable ?B ?f"
  1011   moreover
  1012   then have "?f \<circ> e2p \<in> borel_measurable (borel::'a measure)"
  1013     by (auto intro!: measurable_e2p)
  1014   then have "f \<in> borel_measurable borel"
  1015     by (simp cong: measurable_cong)
  1016   ultimately show "integrable lborel f"
  1017     by (simp add: borel_fubini_positiv_integral integrable_def)
  1018 qed
  1019 
  1020 lemma borel_fubini:
  1021   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
  1022   assumes f: "f \<in> borel_measurable borel"
  1023   shows "integral\<^isup>L lborel f = \<integral>x. f (p2e x) \<partial>((\<Pi>\<^isub>M i\<in>{..<DIM('a)}. lborel))"
  1024   using f by (simp add: borel_fubini_positiv_integral lebesgue_integral_def)
  1025 
  1026 end