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