src/HOL/Probability/Lebesgue_Measure.thy
author hoelzl
Wed Feb 23 11:40:12 2011 +0100 (2011-02-23)
changeset 41831 91a2b435dd7a
parent 41706 a207a858d1f6
child 41981 cdf7693bbe08
permissions -rw-r--r--
use measure_preserving in ..._vimage lemmas
     1 (*  Author: Robert Himmelmann, TU Muenchen *)
     2 header {* Lebsegue measure *}
     3 theory Lebesgue_Measure
     4   imports Product_Measure
     5 begin
     6 
     7 subsection {* Standard Cubes *}
     8 
     9 definition cube :: "nat \<Rightarrow> 'a::ordered_euclidean_space set" where
    10   "cube n \<equiv> {\<chi>\<chi> i. - real n .. \<chi>\<chi> i. real n}"
    11 
    12 lemma cube_closed[intro]: "closed (cube n)"
    13   unfolding cube_def by auto
    14 
    15 lemma cube_subset[intro]: "n \<le> N \<Longrightarrow> cube n \<subseteq> cube N"
    16   by (fastsimp simp: eucl_le[where 'a='a] cube_def)
    17 
    18 lemma cube_subset_iff:
    19   "cube n \<subseteq> cube N \<longleftrightarrow> n \<le> N"
    20 proof
    21   assume subset: "cube n \<subseteq> (cube N::'a set)"
    22   then have "((\<chi>\<chi> i. real n)::'a) \<in> cube N"
    23     using DIM_positive[where 'a='a]
    24     by (fastsimp simp: cube_def eucl_le[where 'a='a])
    25   then show "n \<le> N"
    26     by (fastsimp simp: cube_def eucl_le[where 'a='a])
    27 next
    28   assume "n \<le> N" then show "cube n \<subseteq> (cube N::'a set)" by (rule cube_subset)
    29 qed
    30 
    31 lemma ball_subset_cube:"ball (0::'a::ordered_euclidean_space) (real n) \<subseteq> cube n"
    32   unfolding cube_def subset_eq mem_interval apply safe unfolding euclidean_lambda_beta'
    33 proof- fix x::'a and i assume as:"x\<in>ball 0 (real n)" "i<DIM('a)"
    34   thus "- real n \<le> x $$ i" "real n \<ge> x $$ i"
    35     using component_le_norm[of x i] by(auto simp: dist_norm)
    36 qed
    37 
    38 lemma mem_big_cube: obtains n where "x \<in> cube n"
    39 proof- from real_arch_lt[of "norm x"] guess n ..
    40   thus ?thesis apply-apply(rule that[where n=n])
    41     apply(rule ball_subset_cube[unfolded subset_eq,rule_format])
    42     by (auto simp add:dist_norm)
    43 qed
    44 
    45 lemma cube_subset_Suc[intro]: "cube n \<subseteq> cube (Suc n)"
    46   unfolding cube_def_raw subset_eq apply safe unfolding mem_interval by auto
    47 
    48 lemma Pi_iff: "f \<in> Pi I X \<longleftrightarrow> (\<forall>i\<in>I. f i \<in> X i)"
    49   unfolding Pi_def by auto
    50 
    51 subsection {* Lebesgue measure *}
    52 
    53 definition lebesgue :: "'a::ordered_euclidean_space measure_space" where
    54   "lebesgue = \<lparr> space = UNIV,
    55     sets = {A. \<forall>n. (indicator A :: 'a \<Rightarrow> real) integrable_on cube n},
    56     measure = \<lambda>A. SUP n. Real (integral (cube n) (indicator A)) \<rparr>"
    57 
    58 lemma space_lebesgue[simp]: "space lebesgue = UNIV"
    59   unfolding lebesgue_def by simp
    60 
    61 lemma lebesgueD: "A \<in> sets lebesgue \<Longrightarrow> (indicator A :: _ \<Rightarrow> real) integrable_on cube n"
    62   unfolding lebesgue_def by simp
    63 
    64 lemma lebesgueI: "(\<And>n. (indicator A :: _ \<Rightarrow> real) integrable_on cube n) \<Longrightarrow> A \<in> sets lebesgue"
    65   unfolding lebesgue_def by simp
    66 
    67 lemma absolutely_integrable_on_indicator[simp]:
    68   fixes A :: "'a::ordered_euclidean_space set"
    69   shows "((indicator A :: _ \<Rightarrow> real) absolutely_integrable_on X) \<longleftrightarrow>
    70     (indicator A :: _ \<Rightarrow> real) integrable_on X"
    71   unfolding absolutely_integrable_on_def by simp
    72 
    73 lemma LIMSEQ_indicator_UN:
    74   "(\<lambda>k. indicator (\<Union> i<k. A i) x) ----> (indicator (\<Union>i. A i) x :: real)"
    75 proof cases
    76   assume "\<exists>i. x \<in> A i" then guess i .. note i = this
    77   then have *: "\<And>n. (indicator (\<Union> i<n + Suc i. A i) x :: real) = 1"
    78     "(indicator (\<Union> i. A i) x :: real) = 1" by (auto simp: indicator_def)
    79   show ?thesis
    80     apply (rule LIMSEQ_offset[of _ "Suc i"]) unfolding * by auto
    81 qed (auto simp: indicator_def)
    82 
    83 lemma indicator_add:
    84   "A \<inter> B = {} \<Longrightarrow> (indicator A x::_::monoid_add) + indicator B x = indicator (A \<union> B) x"
    85   unfolding indicator_def by auto
    86 
    87 interpretation lebesgue: sigma_algebra lebesgue
    88 proof (intro sigma_algebra_iff2[THEN iffD2] conjI allI ballI impI lebesgueI)
    89   fix A n assume A: "A \<in> sets lebesgue"
    90   have "indicator (space lebesgue - A) = (\<lambda>x. 1 - indicator A x :: real)"
    91     by (auto simp: fun_eq_iff indicator_def)
    92   then show "(indicator (space lebesgue - A) :: _ \<Rightarrow> real) integrable_on cube n"
    93     using A by (auto intro!: integrable_sub dest: lebesgueD simp: cube_def)
    94 next
    95   fix n show "(indicator {} :: _\<Rightarrow>real) integrable_on cube n"
    96     by (auto simp: cube_def indicator_def_raw)
    97 next
    98   fix A :: "nat \<Rightarrow> 'a set" and n ::nat assume "range A \<subseteq> sets lebesgue"
    99   then have A: "\<And>i. (indicator (A i) :: _ \<Rightarrow> real) integrable_on cube n"
   100     by (auto dest: lebesgueD)
   101   show "(indicator (\<Union>i. A i) :: _ \<Rightarrow> real) integrable_on cube n" (is "?g integrable_on _")
   102   proof (intro dominated_convergence[where g="?g"] ballI)
   103     fix k show "(indicator (\<Union>i<k. A i) :: _ \<Rightarrow> real) integrable_on cube n"
   104     proof (induct k)
   105       case (Suc k)
   106       have *: "(\<Union> i<Suc k. A i) = (\<Union> i<k. A i) \<union> A k"
   107         unfolding lessThan_Suc UN_insert by auto
   108       have *: "(\<lambda>x. max (indicator (\<Union> i<k. A i) x) (indicator (A k) x) :: real) =
   109           indicator (\<Union> i<Suc k. A i)" (is "(\<lambda>x. max (?f x) (?g x)) = _")
   110         by (auto simp: fun_eq_iff * indicator_def)
   111       show ?case
   112         using absolutely_integrable_max[of ?f "cube n" ?g] A Suc by (simp add: *)
   113     qed auto
   114   qed (auto intro: LIMSEQ_indicator_UN simp: cube_def)
   115 qed simp
   116 
   117 interpretation lebesgue: measure_space lebesgue
   118 proof
   119   have *: "indicator {} = (\<lambda>x. 0 :: real)" by (simp add: fun_eq_iff)
   120   show "measure lebesgue {} = 0" by (simp add: integral_0 * lebesgue_def)
   121 next
   122   show "countably_additive lebesgue (measure lebesgue)"
   123   proof (intro countably_additive_def[THEN iffD2] allI impI)
   124     fix A :: "nat \<Rightarrow> 'b set" assume rA: "range A \<subseteq> sets lebesgue" "disjoint_family A"
   125     then have A[simp, intro]: "\<And>i n. (indicator (A i) :: _ \<Rightarrow> real) integrable_on cube n"
   126       by (auto dest: lebesgueD)
   127     let "?m n i" = "integral (cube n) (indicator (A i) :: _\<Rightarrow>real)"
   128     let "?M n I" = "integral (cube n) (indicator (\<Union>i\<in>I. A i) :: _\<Rightarrow>real)"
   129     have nn[simp, intro]: "\<And>i n. 0 \<le> ?m n i" by (auto intro!: integral_nonneg)
   130     assume "(\<Union>i. A i) \<in> sets lebesgue"
   131     then have UN_A[simp, intro]: "\<And>i n. (indicator (\<Union>i. A i) :: _ \<Rightarrow> real) integrable_on cube n"
   132       by (auto dest: lebesgueD)
   133     show "(\<Sum>\<^isub>\<infinity>n. measure lebesgue (A n)) = measure lebesgue (\<Union>i. A i)"
   134     proof (simp add: lebesgue_def, subst psuminf_SUP_eq)
   135       fix n i show "Real (?m n i) \<le> Real (?m (Suc n) i)"
   136         using cube_subset[of n "Suc n"] by (auto intro!: integral_subset_le)
   137     next
   138       show "(SUP n. \<Sum>\<^isub>\<infinity>i. Real (?m n i)) = (SUP n. Real (?M n UNIV))"
   139         unfolding psuminf_def
   140       proof (subst setsum_Real, (intro arg_cong[where f="SUPR UNIV"] ext ballI nn SUP_eq_LIMSEQ[THEN iffD2])+)
   141         fix n :: nat show "mono (\<lambda>m. \<Sum>x<m. ?m n x)"
   142         proof (intro mono_iff_le_Suc[THEN iffD2] allI)
   143           fix m show "(\<Sum>x<m. ?m n x) \<le> (\<Sum>x<Suc m. ?m n x)"
   144             using nn[of n m] by auto
   145         qed
   146         show "0 \<le> ?M n UNIV"
   147           using UN_A by (auto intro!: integral_nonneg)
   148         fix m show "0 \<le> (\<Sum>x<m. ?m n x)" by (auto intro!: setsum_nonneg)
   149       next
   150         fix n
   151         have "\<And>m. (UNION {..<m} A) \<in> sets lebesgue" using rA by auto
   152         from lebesgueD[OF this]
   153         have "(\<lambda>m. ?M n {..< m}) ----> ?M n UNIV"
   154           (is "(\<lambda>m. integral _ (?A m)) ----> ?I")
   155           by (intro dominated_convergence(2)[where f="?A" and h="\<lambda>x. 1::real"])
   156              (auto intro: LIMSEQ_indicator_UN simp: cube_def)
   157         moreover
   158         { fix m have *: "(\<Sum>x<m. ?m n x) = ?M n {..< m}"
   159           proof (induct m)
   160             case (Suc m)
   161             have "(\<Union>i<m. A i) \<in> sets lebesgue" using rA by auto
   162             then have "(indicator (\<Union>i<m. A i) :: _\<Rightarrow>real) integrable_on (cube n)"
   163               by (auto dest!: lebesgueD)
   164             moreover
   165             have "(\<Union>i<m. A i) \<inter> A m = {}"
   166               using rA(2)[unfolded disjoint_family_on_def, THEN bspec, of m]
   167               by auto
   168             then have "\<And>x. indicator (\<Union>i<Suc m. A i) x =
   169               indicator (\<Union>i<m. A i) x + (indicator (A m) x :: real)"
   170               by (auto simp: indicator_add lessThan_Suc ac_simps)
   171             ultimately show ?case
   172               using Suc A by (simp add: integral_add[symmetric])
   173           qed auto }
   174         ultimately show "(\<lambda>m. \<Sum>x<m. ?m n x) ----> ?M n UNIV"
   175           by simp
   176       qed
   177     qed
   178   qed
   179 qed
   180 
   181 lemma has_integral_interval_cube:
   182   fixes a b :: "'a::ordered_euclidean_space"
   183   shows "(indicator {a .. b} has_integral
   184     content ({\<chi>\<chi> i. max (- real n) (a $$ i) .. \<chi>\<chi> i. min (real n) (b $$ i)} :: 'a set)) (cube n)"
   185     (is "(?I has_integral content ?R) (cube n)")
   186 proof -
   187   let "{?N .. ?P}" = ?R
   188   have [simp]: "(\<lambda>x. if x \<in> cube n then ?I x else 0) = indicator ?R"
   189     by (auto simp: indicator_def cube_def fun_eq_iff eucl_le[where 'a='a])
   190   have "(?I has_integral content ?R) (cube n) \<longleftrightarrow> (indicator ?R has_integral content ?R) UNIV"
   191     unfolding has_integral_restrict_univ[where s="cube n", symmetric] by simp
   192   also have "\<dots> \<longleftrightarrow> ((\<lambda>x. 1) has_integral content ?R) ?R"
   193     unfolding indicator_def_raw has_integral_restrict_univ ..
   194   finally show ?thesis
   195     using has_integral_const[of "1::real" "?N" "?P"] by simp
   196 qed
   197 
   198 lemma lebesgueI_borel[intro, simp]:
   199   fixes s::"'a::ordered_euclidean_space set"
   200   assumes "s \<in> sets borel" shows "s \<in> sets lebesgue"
   201 proof -
   202   let ?S = "range (\<lambda>(a, b). {a .. (b :: 'a\<Colon>ordered_euclidean_space)})"
   203   have *:"?S \<subseteq> sets lebesgue"
   204   proof (safe intro!: lebesgueI)
   205     fix n :: nat and a b :: 'a
   206     let ?N = "\<chi>\<chi> i. max (- real n) (a $$ i)"
   207     let ?P = "\<chi>\<chi> i. min (real n) (b $$ i)"
   208     show "(indicator {a..b} :: 'a\<Rightarrow>real) integrable_on cube n"
   209       unfolding integrable_on_def
   210       using has_integral_interval_cube[of a b] by auto
   211   qed
   212   have "s \<in> sigma_sets UNIV ?S" using assms
   213     unfolding borel_eq_atLeastAtMost by (simp add: sigma_def)
   214   thus ?thesis
   215     using lebesgue.sigma_subset[of "\<lparr> space = UNIV, sets = ?S\<rparr>", simplified, OF *]
   216     by (auto simp: sigma_def)
   217 qed
   218 
   219 lemma lebesgueI_negligible[dest]: fixes s::"'a::ordered_euclidean_space set"
   220   assumes "negligible s" shows "s \<in> sets lebesgue"
   221   using assms by (force simp: cube_def integrable_on_def negligible_def intro!: lebesgueI)
   222 
   223 lemma lmeasure_eq_0:
   224   fixes S :: "'a::ordered_euclidean_space set" assumes "negligible S" shows "lebesgue.\<mu> S = 0"
   225 proof -
   226   have "\<And>n. integral (cube n) (indicator S :: 'a\<Rightarrow>real) = 0"
   227     unfolding lebesgue_integral_def using assms
   228     by (intro integral_unique some1_equality ex_ex1I)
   229        (auto simp: cube_def negligible_def)
   230   then show ?thesis by (auto simp: lebesgue_def)
   231 qed
   232 
   233 lemma lmeasure_iff_LIMSEQ:
   234   assumes "A \<in> sets lebesgue" "0 \<le> m"
   235   shows "lebesgue.\<mu> A = Real m \<longleftrightarrow> (\<lambda>n. integral (cube n) (indicator A :: _ \<Rightarrow> real)) ----> m"
   236 proof (simp add: lebesgue_def, intro SUP_eq_LIMSEQ)
   237   show "mono (\<lambda>n. integral (cube n) (indicator A::_=>real))"
   238     using cube_subset assms by (intro monoI integral_subset_le) (auto dest!: lebesgueD)
   239   fix n show "0 \<le> integral (cube n) (indicator A::_=>real)"
   240     using assms by (auto dest!: lebesgueD intro!: integral_nonneg)
   241 qed fact
   242 
   243 lemma has_integral_indicator_UNIV:
   244   fixes s A :: "'a::ordered_euclidean_space set" and x :: real
   245   shows "((indicator (s \<inter> A) :: 'a\<Rightarrow>real) has_integral x) UNIV = ((indicator s :: _\<Rightarrow>real) has_integral x) A"
   246 proof -
   247   have "(\<lambda>x. if x \<in> A then indicator s x else 0) = (indicator (s \<inter> A) :: _\<Rightarrow>real)"
   248     by (auto simp: fun_eq_iff indicator_def)
   249   then show ?thesis
   250     unfolding has_integral_restrict_univ[where s=A, symmetric] by simp
   251 qed
   252 
   253 lemma
   254   fixes s a :: "'a::ordered_euclidean_space set"
   255   shows integral_indicator_UNIV:
   256     "integral UNIV (indicator (s \<inter> A) :: 'a\<Rightarrow>real) = integral A (indicator s :: _\<Rightarrow>real)"
   257   and integrable_indicator_UNIV:
   258     "(indicator (s \<inter> A) :: 'a\<Rightarrow>real) integrable_on UNIV \<longleftrightarrow> (indicator s :: 'a\<Rightarrow>real) integrable_on A"
   259   unfolding integral_def integrable_on_def has_integral_indicator_UNIV by auto
   260 
   261 lemma lmeasure_finite_has_integral:
   262   fixes s :: "'a::ordered_euclidean_space set"
   263   assumes "s \<in> sets lebesgue" "lebesgue.\<mu> s = Real m" "0 \<le> m"
   264   shows "(indicator s has_integral m) UNIV"
   265 proof -
   266   let ?I = "indicator :: 'a set \<Rightarrow> 'a \<Rightarrow> real"
   267   have **: "(?I s) integrable_on UNIV \<and> (\<lambda>k. integral UNIV (?I (s \<inter> cube k))) ----> integral UNIV (?I s)"
   268   proof (intro monotone_convergence_increasing allI ballI)
   269     have LIMSEQ: "(\<lambda>n. integral (cube n) (?I s)) ----> m"
   270       using assms(2) unfolding lmeasure_iff_LIMSEQ[OF assms(1, 3)] .
   271     { fix n have "integral (cube n) (?I s) \<le> m"
   272         using cube_subset assms
   273         by (intro incseq_le[where L=m] LIMSEQ incseq_def[THEN iffD2] integral_subset_le allI impI)
   274            (auto dest!: lebesgueD) }
   275     moreover
   276     { fix n have "0 \<le> integral (cube n) (?I s)"
   277       using assms by (auto dest!: lebesgueD intro!: integral_nonneg) }
   278     ultimately
   279     show "bounded {integral UNIV (?I (s \<inter> cube k)) |k. True}"
   280       unfolding bounded_def
   281       apply (rule_tac exI[of _ 0])
   282       apply (rule_tac exI[of _ m])
   283       by (auto simp: dist_real_def integral_indicator_UNIV)
   284     fix k show "?I (s \<inter> cube k) integrable_on UNIV"
   285       unfolding integrable_indicator_UNIV using assms by (auto dest!: lebesgueD)
   286     fix x show "?I (s \<inter> cube k) x \<le> ?I (s \<inter> cube (Suc k)) x"
   287       using cube_subset[of k "Suc k"] by (auto simp: indicator_def)
   288   next
   289     fix x :: 'a
   290     from mem_big_cube obtain k where k: "x \<in> cube k" .
   291     { fix n have "?I (s \<inter> cube (n + k)) x = ?I s x"
   292       using k cube_subset[of k "n + k"] by (auto simp: indicator_def) }
   293     note * = this
   294     show "(\<lambda>k. ?I (s \<inter> cube k) x) ----> ?I s x"
   295       by (rule LIMSEQ_offset[where k=k]) (auto simp: *)
   296   qed
   297   note ** = conjunctD2[OF this]
   298   have m: "m = integral UNIV (?I s)"
   299     apply (intro LIMSEQ_unique[OF _ **(2)])
   300     using assms(2) unfolding lmeasure_iff_LIMSEQ[OF assms(1,3)] integral_indicator_UNIV .
   301   show ?thesis
   302     unfolding m by (intro integrable_integral **)
   303 qed
   304 
   305 lemma lmeasure_finite_integrable: assumes "s \<in> sets lebesgue" "lebesgue.\<mu> s \<noteq> \<omega>"
   306   shows "(indicator s :: _ \<Rightarrow> real) integrable_on UNIV"
   307 proof (cases "lebesgue.\<mu> s")
   308   case (preal m) from lmeasure_finite_has_integral[OF `s \<in> sets lebesgue` this]
   309   show ?thesis unfolding integrable_on_def by auto
   310 qed (insert assms, auto)
   311 
   312 lemma has_integral_lebesgue: assumes "((indicator s :: _\<Rightarrow>real) has_integral m) UNIV"
   313   shows "s \<in> sets lebesgue"
   314 proof (intro lebesgueI)
   315   let ?I = "indicator :: 'a set \<Rightarrow> 'a \<Rightarrow> real"
   316   fix n show "(?I s) integrable_on cube n" unfolding cube_def
   317   proof (intro integrable_on_subinterval)
   318     show "(?I s) integrable_on UNIV"
   319       unfolding integrable_on_def using assms by auto
   320   qed auto
   321 qed
   322 
   323 lemma has_integral_lmeasure: assumes "((indicator s :: _\<Rightarrow>real) has_integral m) UNIV"
   324   shows "lebesgue.\<mu> s = Real m"
   325 proof (intro lmeasure_iff_LIMSEQ[THEN iffD2])
   326   let ?I = "indicator :: 'a set \<Rightarrow> 'a \<Rightarrow> real"
   327   show "s \<in> sets lebesgue" using has_integral_lebesgue[OF assms] .
   328   show "0 \<le> m" using assms by (rule has_integral_nonneg) auto
   329   have "(\<lambda>n. integral UNIV (?I (s \<inter> cube n))) ----> integral UNIV (?I s)"
   330   proof (intro dominated_convergence(2) ballI)
   331     show "(?I s) integrable_on UNIV" unfolding integrable_on_def using assms by auto
   332     fix n show "?I (s \<inter> cube n) integrable_on UNIV"
   333       unfolding integrable_indicator_UNIV using `s \<in> sets lebesgue` by (auto dest: lebesgueD)
   334     fix x show "norm (?I (s \<inter> cube n) x) \<le> ?I s x" by (auto simp: indicator_def)
   335   next
   336     fix x :: 'a
   337     from mem_big_cube obtain k where k: "x \<in> cube k" .
   338     { fix n have "?I (s \<inter> cube (n + k)) x = ?I s x"
   339       using k cube_subset[of k "n + k"] by (auto simp: indicator_def) }
   340     note * = this
   341     show "(\<lambda>k. ?I (s \<inter> cube k) x) ----> ?I s x"
   342       by (rule LIMSEQ_offset[where k=k]) (auto simp: *)
   343   qed
   344   then show "(\<lambda>n. integral (cube n) (?I s)) ----> m"
   345     unfolding integral_unique[OF assms] integral_indicator_UNIV by simp
   346 qed
   347 
   348 lemma has_integral_iff_lmeasure:
   349   "(indicator A has_integral m) UNIV \<longleftrightarrow> (A \<in> sets lebesgue \<and> 0 \<le> m \<and> lebesgue.\<mu> A = Real m)"
   350 proof
   351   assume "(indicator A has_integral m) UNIV"
   352   with has_integral_lmeasure[OF this] has_integral_lebesgue[OF this]
   353   show "A \<in> sets lebesgue \<and> 0 \<le> m \<and> lebesgue.\<mu> A = Real m"
   354     by (auto intro: has_integral_nonneg)
   355 next
   356   assume "A \<in> sets lebesgue \<and> 0 \<le> m \<and> lebesgue.\<mu> A = Real m"
   357   then show "(indicator A has_integral m) UNIV" by (intro lmeasure_finite_has_integral) auto
   358 qed
   359 
   360 lemma lmeasure_eq_integral: assumes "(indicator s::_\<Rightarrow>real) integrable_on UNIV"
   361   shows "lebesgue.\<mu> s = Real (integral UNIV (indicator s))"
   362   using assms unfolding integrable_on_def
   363 proof safe
   364   fix y :: real assume "(indicator s has_integral y) UNIV"
   365   from this[unfolded has_integral_iff_lmeasure] integral_unique[OF this]
   366   show "lebesgue.\<mu> s = Real (integral UNIV (indicator s))" by simp
   367 qed
   368 
   369 lemma lebesgue_simple_function_indicator:
   370   fixes f::"'a::ordered_euclidean_space \<Rightarrow> pextreal"
   371   assumes f:"simple_function lebesgue f"
   372   shows "f = (\<lambda>x. (\<Sum>y \<in> f ` UNIV. y * indicator (f -` {y}) x))"
   373   by (rule, subst lebesgue.simple_function_indicator_representation[OF f]) auto
   374 
   375 lemma integral_eq_lmeasure:
   376   "(indicator s::_\<Rightarrow>real) integrable_on UNIV \<Longrightarrow> integral UNIV (indicator s) = real (lebesgue.\<mu> s)"
   377   by (subst lmeasure_eq_integral) (auto intro!: integral_nonneg)
   378 
   379 lemma lmeasure_finite: assumes "(indicator s::_\<Rightarrow>real) integrable_on UNIV" shows "lebesgue.\<mu> s \<noteq> \<omega>"
   380   using lmeasure_eq_integral[OF assms] by auto
   381 
   382 lemma negligible_iff_lebesgue_null_sets:
   383   "negligible A \<longleftrightarrow> A \<in> lebesgue.null_sets"
   384 proof
   385   assume "negligible A"
   386   from this[THEN lebesgueI_negligible] this[THEN lmeasure_eq_0]
   387   show "A \<in> lebesgue.null_sets" by auto
   388 next
   389   assume A: "A \<in> lebesgue.null_sets"
   390   then have *:"((indicator A) has_integral (0::real)) UNIV" using lmeasure_finite_has_integral[of A] by auto
   391   show "negligible A" unfolding negligible_def
   392   proof (intro allI)
   393     fix a b :: 'a
   394     have integrable: "(indicator A :: _\<Rightarrow>real) integrable_on {a..b}"
   395       by (intro integrable_on_subinterval has_integral_integrable) (auto intro: *)
   396     then have "integral {a..b} (indicator A) \<le> (integral UNIV (indicator A) :: real)"
   397       using * by (auto intro!: integral_subset_le has_integral_integrable)
   398     moreover have "(0::real) \<le> integral {a..b} (indicator A)"
   399       using integrable by (auto intro!: integral_nonneg)
   400     ultimately have "integral {a..b} (indicator A) = (0::real)"
   401       using integral_unique[OF *] by auto
   402     then show "(indicator A has_integral (0::real)) {a..b}"
   403       using integrable_integral[OF integrable] by simp
   404   qed
   405 qed
   406 
   407 lemma integral_const[simp]:
   408   fixes a b :: "'a::ordered_euclidean_space"
   409   shows "integral {a .. b} (\<lambda>x. c) = content {a .. b} *\<^sub>R c"
   410   by (rule integral_unique) (rule has_integral_const)
   411 
   412 lemma lmeasure_UNIV[intro]: "lebesgue.\<mu> (UNIV::'a::ordered_euclidean_space set) = \<omega>"
   413 proof (simp add: lebesgue_def SUP_\<omega>, intro allI impI)
   414   fix x assume "x < \<omega>"
   415   then obtain r where r: "x = Real r" "0 \<le> r" by (cases x) auto
   416   then obtain n where n: "r < of_nat n" using ex_less_of_nat by auto
   417   show "\<exists>i. x < Real (integral (cube i) (indicator UNIV::'a\<Rightarrow>real))"
   418   proof (intro exI[of _ n])
   419     have [simp]: "indicator UNIV = (\<lambda>x. 1)" by (auto simp: fun_eq_iff)
   420     { fix m :: nat assume "0 < m" then have "real n \<le> (\<Prod>x<m. 2 * real n)"
   421       proof (induct m)
   422         case (Suc m)
   423         show ?case
   424         proof cases
   425           assume "m = 0" then show ?thesis by (simp add: lessThan_Suc)
   426         next
   427           assume "m \<noteq> 0" then have "real n \<le> (\<Prod>x<m. 2 * real n)" using Suc by auto
   428           then show ?thesis
   429             by (auto simp: lessThan_Suc field_simps mult_le_cancel_left1)
   430         qed
   431       qed auto } note this[OF DIM_positive[where 'a='a], simp]
   432     then have [simp]: "0 \<le> (\<Prod>x<DIM('a). 2 * real n)" using real_of_nat_ge_zero by arith
   433     have "x < Real (of_nat n)" using n r by auto
   434     also have "Real (of_nat n) \<le> Real (integral (cube n) (indicator UNIV::'a\<Rightarrow>real))"
   435       by (auto simp: real_eq_of_nat[symmetric] cube_def content_closed_interval_cases)
   436     finally show "x < Real (integral (cube n) (indicator UNIV::'a\<Rightarrow>real))" .
   437   qed
   438 qed
   439 
   440 lemma
   441   fixes a b ::"'a::ordered_euclidean_space"
   442   shows lmeasure_atLeastAtMost[simp]: "lebesgue.\<mu> {a..b} = Real (content {a..b})"
   443 proof -
   444   have "(indicator (UNIV \<inter> {a..b})::_\<Rightarrow>real) integrable_on UNIV"
   445     unfolding integrable_indicator_UNIV by (simp add: integrable_const indicator_def_raw)
   446   from lmeasure_eq_integral[OF this] show ?thesis unfolding integral_indicator_UNIV
   447     by (simp add: indicator_def_raw)
   448 qed
   449 
   450 lemma atLeastAtMost_singleton_euclidean[simp]:
   451   fixes a :: "'a::ordered_euclidean_space" shows "{a .. a} = {a}"
   452   by (force simp: eucl_le[where 'a='a] euclidean_eq[where 'a='a])
   453 
   454 lemma content_singleton[simp]: "content {a} = 0"
   455 proof -
   456   have "content {a .. a} = 0"
   457     by (subst content_closed_interval) auto
   458   then show ?thesis by simp
   459 qed
   460 
   461 lemma lmeasure_singleton[simp]:
   462   fixes a :: "'a::ordered_euclidean_space" shows "lebesgue.\<mu> {a} = 0"
   463   using lmeasure_atLeastAtMost[of a a] by simp
   464 
   465 declare content_real[simp]
   466 
   467 lemma
   468   fixes a b :: real
   469   shows lmeasure_real_greaterThanAtMost[simp]:
   470     "lebesgue.\<mu> {a <.. b} = Real (if a \<le> b then b - a else 0)"
   471 proof cases
   472   assume "a < b"
   473   then have "lebesgue.\<mu> {a <.. b} = lebesgue.\<mu> {a .. b} - lebesgue.\<mu> {a}"
   474     by (subst lebesgue.measure_Diff[symmetric])
   475        (auto intro!: arg_cong[where f=lebesgue.\<mu>])
   476   then show ?thesis by auto
   477 qed auto
   478 
   479 lemma
   480   fixes a b :: real
   481   shows lmeasure_real_atLeastLessThan[simp]:
   482     "lebesgue.\<mu> {a ..< b} = Real (if a \<le> b then b - a else 0)"
   483 proof cases
   484   assume "a < b"
   485   then have "lebesgue.\<mu> {a ..< b} = lebesgue.\<mu> {a .. b} - lebesgue.\<mu> {b}"
   486     by (subst lebesgue.measure_Diff[symmetric])
   487        (auto intro!: arg_cong[where f=lebesgue.\<mu>])
   488   then show ?thesis by auto
   489 qed auto
   490 
   491 lemma
   492   fixes a b :: real
   493   shows lmeasure_real_greaterThanLessThan[simp]:
   494     "lebesgue.\<mu> {a <..< b} = Real (if a \<le> b then b - a else 0)"
   495 proof cases
   496   assume "a < b"
   497   then have "lebesgue.\<mu> {a <..< b} = lebesgue.\<mu> {a <.. b} - lebesgue.\<mu> {b}"
   498     by (subst lebesgue.measure_Diff[symmetric])
   499        (auto intro!: arg_cong[where f=lebesgue.\<mu>])
   500   then show ?thesis by auto
   501 qed auto
   502 
   503 subsection {* Lebesgue-Borel measure *}
   504 
   505 definition "lborel = lebesgue \<lparr> sets := sets borel \<rparr>"
   506 
   507 lemma
   508   shows space_lborel[simp]: "space lborel = UNIV"
   509   and sets_lborel[simp]: "sets lborel = sets borel"
   510   and measure_lborel[simp]: "measure lborel = lebesgue.\<mu>"
   511   and measurable_lborel[simp]: "measurable lborel = measurable borel"
   512   by (simp_all add: measurable_def_raw lborel_def)
   513 
   514 interpretation lborel: measure_space lborel
   515   where "space lborel = UNIV"
   516   and "sets lborel = sets borel"
   517   and "measure lborel = lebesgue.\<mu>"
   518   and "measurable lborel = measurable borel"
   519 proof -
   520   show "measure_space lborel"
   521   proof
   522     show "countably_additive lborel (measure lborel)"
   523       using lebesgue.ca unfolding countably_additive_def lborel_def
   524       apply safe apply (erule_tac x=A in allE) by auto
   525   qed (auto simp: lborel_def)
   526 qed simp_all
   527 
   528 interpretation lborel: sigma_finite_measure lborel
   529   where "space lborel = UNIV"
   530   and "sets lborel = sets borel"
   531   and "measure lborel = lebesgue.\<mu>"
   532   and "measurable lborel = measurable borel"
   533 proof -
   534   show "sigma_finite_measure lborel"
   535   proof (default, intro conjI exI[of _ "\<lambda>n. cube n"])
   536     show "range cube \<subseteq> sets lborel" by (auto intro: borel_closed)
   537     { fix x have "\<exists>n. x\<in>cube n" using mem_big_cube by auto }
   538     thus "(\<Union>i. cube i) = space lborel" by auto
   539     show "\<forall>i. measure lborel (cube i) \<noteq> \<omega>" by (simp add: cube_def)
   540   qed
   541 qed simp_all
   542 
   543 interpretation lebesgue: sigma_finite_measure lebesgue
   544 proof
   545   from lborel.sigma_finite guess A ..
   546   moreover then have "range A \<subseteq> sets lebesgue" using lebesgueI_borel by blast
   547   ultimately show "\<exists>A::nat \<Rightarrow> 'b set. range A \<subseteq> sets lebesgue \<and> (\<Union>i. A i) = space lebesgue \<and> (\<forall>i. lebesgue.\<mu> (A i) \<noteq> \<omega>)"
   548     by auto
   549 qed
   550 
   551 subsection {* Lebesgue integrable implies Gauge integrable *}
   552 
   553 lemma simple_function_has_integral:
   554   fixes f::"'a::ordered_euclidean_space \<Rightarrow> pextreal"
   555   assumes f:"simple_function lebesgue f"
   556   and f':"\<forall>x. f x \<noteq> \<omega>"
   557   and om:"\<forall>x\<in>range f. lebesgue.\<mu> (f -` {x} \<inter> UNIV) = \<omega> \<longrightarrow> x = 0"
   558   shows "((\<lambda>x. real (f x)) has_integral (real (integral\<^isup>S lebesgue f))) UNIV"
   559   unfolding simple_integral_def
   560   apply(subst lebesgue_simple_function_indicator[OF f])
   561 proof -
   562   case goal1
   563   have *:"\<And>x. \<forall>y\<in>range f. y * indicator (f -` {y}) x \<noteq> \<omega>"
   564     "\<forall>x\<in>range f. x * lebesgue.\<mu> (f -` {x} \<inter> UNIV) \<noteq> \<omega>"
   565     using f' om unfolding indicator_def by auto
   566   show ?case unfolding space_lebesgue real_of_pextreal_setsum'[OF *(1),THEN sym]
   567     unfolding real_of_pextreal_setsum'[OF *(2),THEN sym]
   568     unfolding real_of_pextreal_setsum space_lebesgue
   569     apply(rule has_integral_setsum)
   570   proof safe show "finite (range f)" using f by (auto dest: lebesgue.simple_functionD)
   571     fix y::'a show "((\<lambda>x. real (f y * indicator (f -` {f y}) x)) has_integral
   572       real (f y * lebesgue.\<mu> (f -` {f y} \<inter> UNIV))) UNIV"
   573     proof(cases "f y = 0") case False
   574       have mea:"(indicator (f -` {f y}) ::_\<Rightarrow>real) integrable_on UNIV"
   575         apply(rule lmeasure_finite_integrable)
   576         using assms unfolding simple_function_def using False by auto
   577       have *:"\<And>x. real (indicator (f -` {f y}) x::pextreal) = (indicator (f -` {f y}) x)"
   578         by (auto simp: indicator_def)
   579       show ?thesis unfolding real_of_pextreal_mult[THEN sym]
   580         apply(rule has_integral_cmul[where 'b=real, unfolded real_scaleR_def])
   581         unfolding Int_UNIV_right lmeasure_eq_integral[OF mea,THEN sym]
   582         unfolding integral_eq_lmeasure[OF mea, symmetric] *
   583         apply(rule integrable_integral) using mea .
   584     qed auto
   585   qed
   586 qed
   587 
   588 lemma bounded_realI: assumes "\<forall>x\<in>s. abs (x::real) \<le> B" shows "bounded s"
   589   unfolding bounded_def dist_real_def apply(rule_tac x=0 in exI)
   590   using assms by auto
   591 
   592 lemma simple_function_has_integral':
   593   fixes f::"'a::ordered_euclidean_space \<Rightarrow> pextreal"
   594   assumes f:"simple_function lebesgue f"
   595   and i: "integral\<^isup>S lebesgue f \<noteq> \<omega>"
   596   shows "((\<lambda>x. real (f x)) has_integral (real (integral\<^isup>S lebesgue f))) UNIV"
   597 proof- let ?f = "\<lambda>x. if f x = \<omega> then 0 else f x"
   598   { fix x have "real (f x) = real (?f x)" by (cases "f x") auto } note * = this
   599   have **:"{x. f x \<noteq> ?f x} = f -` {\<omega>}" by auto
   600   have **:"lebesgue.\<mu> {x\<in>space lebesgue. f x \<noteq> ?f x} = 0"
   601     using lebesgue.simple_integral_omega[OF assms] by(auto simp add:**)
   602   show ?thesis apply(subst lebesgue.simple_integral_cong'[OF f _ **])
   603     apply(rule lebesgue.simple_function_compose1[OF f])
   604     unfolding * defer apply(rule simple_function_has_integral)
   605   proof-
   606     show "simple_function lebesgue ?f"
   607       using lebesgue.simple_function_compose1[OF f] .
   608     show "\<forall>x. ?f x \<noteq> \<omega>" by auto
   609     show "\<forall>x\<in>range ?f. lebesgue.\<mu> (?f -` {x} \<inter> UNIV) = \<omega> \<longrightarrow> x = 0"
   610     proof (safe, simp, safe, rule ccontr)
   611       fix y assume "f y \<noteq> \<omega>" "f y \<noteq> 0"
   612       hence "(\<lambda>x. if f x = \<omega> then 0 else f x) -` {if f y = \<omega> then 0 else f y} = f -` {f y}"
   613         by (auto split: split_if_asm)
   614       moreover assume "lebesgue.\<mu> ((\<lambda>x. if f x = \<omega> then 0 else f x) -` {if f y = \<omega> then 0 else f y}) = \<omega>"
   615       ultimately have "lebesgue.\<mu> (f -` {f y}) = \<omega>" by simp
   616       moreover
   617       have "f y * lebesgue.\<mu> (f -` {f y}) \<noteq> \<omega>" using i f
   618         unfolding simple_integral_def setsum_\<omega> simple_function_def
   619         by auto
   620       ultimately have "f y = 0" by (auto split: split_if_asm)
   621       then show False using `f y \<noteq> 0` by simp
   622     qed
   623   qed
   624 qed
   625 
   626 lemma (in measure_space) positive_integral_monotone_convergence:
   627   fixes f::"nat \<Rightarrow> 'a \<Rightarrow> pextreal"
   628   assumes i: "\<And>i. f i \<in> borel_measurable M" and mono: "\<And>x. mono (\<lambda>n. f n x)"
   629   and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
   630   shows "u \<in> borel_measurable M"
   631   and "(\<lambda>i. integral\<^isup>P M (f i)) ----> integral\<^isup>P M u" (is ?ilim)
   632 proof -
   633   from positive_integral_isoton[unfolded isoton_fun_expand isoton_iff_Lim_mono, of f u]
   634   show ?ilim using mono lim i by auto
   635   have "\<And>x. (SUP i. f i x) = u x" using mono lim SUP_Lim_pextreal
   636     unfolding fun_eq_iff mono_def by auto
   637   moreover have "(\<lambda>x. SUP i. f i x) \<in> borel_measurable M"
   638     using i by auto
   639   ultimately show "u \<in> borel_measurable M" by simp
   640 qed
   641 
   642 lemma positive_integral_has_integral:
   643   fixes f::"'a::ordered_euclidean_space => pextreal"
   644   assumes f:"f \<in> borel_measurable lebesgue"
   645   and int_om:"integral\<^isup>P lebesgue f \<noteq> \<omega>"
   646   and f_om:"\<forall>x. f x \<noteq> \<omega>" (* TODO: remove this *)
   647   shows "((\<lambda>x. real (f x)) has_integral (real (integral\<^isup>P lebesgue f))) UNIV"
   648 proof- let ?i = "integral\<^isup>P lebesgue f"
   649   from lebesgue.borel_measurable_implies_simple_function_sequence[OF f]
   650   guess u .. note conjunctD2[OF this,rule_format] note u = conjunctD2[OF this(1)] this(2)
   651   let ?u = "\<lambda>i x. real (u i x)" and ?f = "\<lambda>x. real (f x)"
   652   have u_simple:"\<And>k. integral\<^isup>S lebesgue (u k) = integral\<^isup>P lebesgue (u k)"
   653     apply(subst lebesgue.positive_integral_eq_simple_integral[THEN sym,OF u(1)]) ..
   654   have int_u_le:"\<And>k. integral\<^isup>S lebesgue (u k) \<le> integral\<^isup>P lebesgue f"
   655     unfolding u_simple apply(rule lebesgue.positive_integral_mono)
   656     using isoton_Sup[OF u(3)] unfolding le_fun_def by auto
   657   have u_int_om:"\<And>i. integral\<^isup>S lebesgue (u i) \<noteq> \<omega>"
   658   proof- case goal1 thus ?case using int_u_le[of i] int_om by auto qed
   659 
   660   note u_int = simple_function_has_integral'[OF u(1) this]
   661   have "(\<lambda>x. real (f x)) integrable_on UNIV \<and>
   662     (\<lambda>k. Integration.integral UNIV (\<lambda>x. real (u k x))) ----> Integration.integral UNIV (\<lambda>x. real (f x))"
   663     apply(rule monotone_convergence_increasing) apply(rule,rule,rule u_int)
   664   proof safe case goal1 show ?case apply(rule real_of_pextreal_mono) using u(2,3) by auto
   665   next case goal2 show ?case using u(3) apply(subst lim_Real[THEN sym])
   666       prefer 3 apply(subst Real_real') defer apply(subst Real_real')
   667       using isotone_Lim[OF u(3)[unfolded isoton_fun_expand, THEN spec]] using f_om u by auto
   668   next case goal3
   669     show ?case apply(rule bounded_realI[where B="real (integral\<^isup>P lebesgue f)"])
   670       apply safe apply(subst abs_of_nonneg) apply(rule integral_nonneg,rule) apply(rule u_int)
   671       unfolding integral_unique[OF u_int] defer apply(rule real_of_pextreal_mono[OF _ int_u_le])
   672       using u int_om by auto
   673   qed note int = conjunctD2[OF this]
   674 
   675   have "(\<lambda>i. integral\<^isup>S lebesgue (u i)) ----> ?i" unfolding u_simple
   676     apply(rule lebesgue.positive_integral_monotone_convergence(2))
   677     apply(rule lebesgue.borel_measurable_simple_function[OF u(1)])
   678     using isotone_Lim[OF u(3)[unfolded isoton_fun_expand, THEN spec]] by auto
   679   hence "(\<lambda>i. real (integral\<^isup>S lebesgue (u i))) ----> real ?i" apply-
   680     apply(subst lim_Real[THEN sym]) prefer 3
   681     apply(subst Real_real') defer apply(subst Real_real')
   682     using u f_om int_om u_int_om by auto
   683   note * = LIMSEQ_unique[OF this int(2)[unfolded integral_unique[OF u_int]]]
   684   show ?thesis unfolding * by(rule integrable_integral[OF int(1)])
   685 qed
   686 
   687 lemma lebesgue_integral_has_integral:
   688   fixes f::"'a::ordered_euclidean_space => real"
   689   assumes f:"integrable lebesgue f"
   690   shows "(f has_integral (integral\<^isup>L lebesgue f)) UNIV"
   691 proof- let ?n = "\<lambda>x. - min (f x) 0" and ?p = "\<lambda>x. max (f x) 0"
   692   have *:"f = (\<lambda>x. ?p x - ?n x)" apply rule by auto
   693   note f = integrableD[OF f]
   694   show ?thesis unfolding lebesgue_integral_def apply(subst *)
   695   proof(rule has_integral_sub) case goal1
   696     have *:"\<forall>x. Real (f x) \<noteq> \<omega>" by auto
   697     note lebesgue.borel_measurable_Real[OF f(1)]
   698     from positive_integral_has_integral[OF this f(2) *]
   699     show ?case unfolding real_Real_max .
   700   next case goal2
   701     have *:"\<forall>x. Real (- f x) \<noteq> \<omega>" by auto
   702     note lebesgue.borel_measurable_uminus[OF f(1)]
   703     note lebesgue.borel_measurable_Real[OF this]
   704     from positive_integral_has_integral[OF this f(3) *]
   705     show ?case unfolding real_Real_max minus_min_eq_max by auto
   706   qed
   707 qed
   708 
   709 lemma lebesgue_positive_integral_eq_borel:
   710   "f \<in> borel_measurable borel \<Longrightarrow> integral\<^isup>P lebesgue f = integral\<^isup>P lborel f"
   711   by (auto intro!: lebesgue.positive_integral_subalgebra[symmetric]) default
   712 
   713 lemma lebesgue_integral_eq_borel:
   714   assumes "f \<in> borel_measurable borel"
   715   shows "integrable lebesgue f \<longleftrightarrow> integrable lborel f" (is ?P)
   716     and "integral\<^isup>L lebesgue f = integral\<^isup>L lborel f" (is ?I)
   717 proof -
   718   have *: "sigma_algebra lborel" by default
   719   have "sets lborel \<subseteq> sets lebesgue" by auto
   720   from lebesgue.integral_subalgebra[of f lborel, OF _ this _ _ *] assms
   721   show ?P ?I by auto
   722 qed
   723 
   724 lemma borel_integral_has_integral:
   725   fixes f::"'a::ordered_euclidean_space => real"
   726   assumes f:"integrable lborel f"
   727   shows "(f has_integral (integral\<^isup>L lborel f)) UNIV"
   728 proof -
   729   have borel: "f \<in> borel_measurable borel"
   730     using f unfolding integrable_def by auto
   731   from f show ?thesis
   732     using lebesgue_integral_has_integral[of f]
   733     unfolding lebesgue_integral_eq_borel[OF borel] by simp
   734 qed
   735 
   736 subsection {* Equivalence between product spaces and euclidean spaces *}
   737 
   738 definition e2p :: "'a::ordered_euclidean_space \<Rightarrow> (nat \<Rightarrow> real)" where
   739   "e2p x = (\<lambda>i\<in>{..<DIM('a)}. x$$i)"
   740 
   741 definition p2e :: "(nat \<Rightarrow> real) \<Rightarrow> 'a::ordered_euclidean_space" where
   742   "p2e x = (\<chi>\<chi> i. x i)"
   743 
   744 lemma e2p_p2e[simp]:
   745   "x \<in> extensional {..<DIM('a)} \<Longrightarrow> e2p (p2e x::'a::ordered_euclidean_space) = x"
   746   by (auto simp: fun_eq_iff extensional_def p2e_def e2p_def)
   747 
   748 lemma p2e_e2p[simp]:
   749   "p2e (e2p x) = (x::'a::ordered_euclidean_space)"
   750   by (auto simp: euclidean_eq[where 'a='a] p2e_def e2p_def)
   751 
   752 interpretation lborel_product: product_sigma_finite "\<lambda>x. lborel::real measure_space"
   753   by default
   754 
   755 interpretation lborel_space: finite_product_sigma_finite "\<lambda>x. lborel::real measure_space" "{..<n}" for n :: nat
   756   where "space lborel = UNIV"
   757   and "sets lborel = sets borel"
   758   and "measure lborel = lebesgue.\<mu>"
   759   and "measurable lborel = measurable borel"
   760 proof -
   761   show "finite_product_sigma_finite (\<lambda>x. lborel::real measure_space) {..<n}"
   762     by default simp
   763 qed simp_all
   764 
   765 lemma sets_product_borel:
   766   assumes [intro]: "finite I"
   767   shows "sets (\<Pi>\<^isub>M i\<in>I.
   768      \<lparr> space = UNIV::real set, sets = range lessThan, measure = lebesgue.\<mu> \<rparr>) =
   769    sets (\<Pi>\<^isub>M i\<in>I. lborel)" (is "sets ?G = _")
   770 proof -
   771   have "sets ?G = sets (\<Pi>\<^isub>M i\<in>I.
   772        sigma \<lparr> space = UNIV::real set, sets = range lessThan, measure = lebesgue.\<mu> \<rparr>)"
   773     by (subst sigma_product_algebra_sigma_eq[of I "\<lambda>_ i. {..<real i}" ])
   774        (auto intro!: measurable_sigma_sigma isotoneI real_arch_lt
   775              simp: product_algebra_def)
   776   then show ?thesis
   777     unfolding lborel_def borel_eq_lessThan lebesgue_def sigma_def by simp
   778 qed
   779 
   780 lemma measurable_e2p:
   781   "e2p \<in> measurable (borel::'a::ordered_euclidean_space algebra)
   782                     (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. (lborel :: real measure_space))"
   783     (is "_ \<in> measurable ?E ?P")
   784 proof -
   785   let ?B = "\<lparr> space = UNIV::real set, sets = range lessThan, measure = lebesgue.\<mu> \<rparr>"
   786   let ?G = "product_algebra_generator {..<DIM('a)} (\<lambda>_. ?B)"
   787   have "e2p \<in> measurable ?E (sigma ?G)"
   788   proof (rule borel.measurable_sigma)
   789     show "e2p \<in> space ?E \<rightarrow> space ?G" by (auto simp: e2p_def)
   790     fix A assume "A \<in> sets ?G"
   791     then obtain E where A: "A = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. E i)"
   792       and "E \<in> {..<DIM('a)} \<rightarrow> (range lessThan)"
   793       by (auto elim!: product_algebraE simp: )
   794     then have "\<forall>i\<in>{..<DIM('a)}. \<exists>xs. E i = {..< xs}" by auto
   795     from this[THEN bchoice] guess xs ..
   796     then have A_eq: "A = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {..< xs i})"
   797       using A by auto
   798     have "e2p -` A = {..< (\<chi>\<chi> i. xs i) :: 'a}"
   799       using DIM_positive by (auto simp add: Pi_iff set_eq_iff e2p_def A_eq
   800         euclidean_eq[where 'a='a] eucl_less[where 'a='a])
   801     then show "e2p -` A \<inter> space ?E \<in> sets ?E" by simp
   802   qed (auto simp: product_algebra_generator_def)
   803   with sets_product_borel[of "{..<DIM('a)}"] show ?thesis
   804     unfolding measurable_def product_algebra_def by simp
   805 qed
   806 
   807 lemma measurable_p2e:
   808   "p2e \<in> measurable (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. (lborel :: real measure_space))
   809     (borel :: 'a::ordered_euclidean_space algebra)"
   810   (is "p2e \<in> measurable ?P _")
   811   unfolding borel_eq_lessThan
   812 proof (intro lborel_space.measurable_sigma)
   813   let ?E = "\<lparr> space = UNIV :: 'a set, sets = range lessThan \<rparr>"
   814   show "p2e \<in> space ?P \<rightarrow> space ?E" by simp
   815   fix A assume "A \<in> sets ?E"
   816   then obtain x where "A = {..<x}" by auto
   817   then have "p2e -` A \<inter> space ?P = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {..< x $$ i})"
   818     using DIM_positive
   819     by (auto simp: Pi_iff set_eq_iff p2e_def
   820                    euclidean_eq[where 'a='a] eucl_less[where 'a='a])
   821   then show "p2e -` A \<inter> space ?P \<in> sets ?P" by auto
   822 qed simp
   823 
   824 lemma Int_stable_cuboids:
   825   fixes x::"'a::ordered_euclidean_space"
   826   shows "Int_stable \<lparr>space = UNIV, sets = range (\<lambda>(a, b::'a). {a..b})\<rparr>"
   827   by (auto simp: inter_interval Int_stable_def)
   828 
   829 lemma lborel_eq_lborel_space:
   830   fixes A :: "('a::ordered_euclidean_space) set"
   831   assumes "A \<in> sets borel"
   832   shows "lborel.\<mu> A = lborel_space.\<mu> DIM('a) (p2e -` A \<inter> (space (lborel_space.P DIM('a))))"
   833     (is "_ = measure ?P (?T A)")
   834 proof (rule measure_unique_Int_stable_vimage)
   835   show "measure_space ?P" by default
   836   show "measure_space lborel" by default
   837 
   838   let ?E = "\<lparr> space = UNIV :: 'a set, sets = range (\<lambda>(a,b). {a..b}) \<rparr>"
   839   show "Int_stable ?E" using Int_stable_cuboids .
   840   show "range cube \<subseteq> sets ?E" unfolding cube_def_raw by auto
   841   { fix x have "\<exists>n. x \<in> cube n" using mem_big_cube[of x] by fastsimp }
   842   then show "cube \<up> space ?E" by (intro isotoneI cube_subset_Suc) auto
   843   { fix i show "lborel.\<mu> (cube i) \<noteq> \<omega>" unfolding cube_def by auto }
   844   show "A \<in> sets (sigma ?E)" "sets (sigma ?E) = sets lborel" "space ?E = space lborel"
   845     using assms by (simp_all add: borel_eq_atLeastAtMost)
   846 
   847   show "p2e \<in> measurable ?P (lborel :: 'a measure_space)"
   848     using measurable_p2e unfolding measurable_def by simp
   849   { fix X assume "X \<in> sets ?E"
   850     then obtain a b where X[simp]: "X = {a .. b}" by auto
   851     have *: "?T X = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {a $$ i .. b $$ i})"
   852       by (auto simp: Pi_iff eucl_le[where 'a='a] p2e_def)
   853     show "lborel.\<mu> X = measure ?P (?T X)"
   854     proof cases
   855       assume "X \<noteq> {}"
   856       then have "a \<le> b"
   857         by (simp add: interval_ne_empty eucl_le[where 'a='a])
   858       then have "lborel.\<mu> X = (\<Prod>x<DIM('a). lborel.\<mu> {a $$ x .. b $$ x})"
   859         by (auto simp: content_closed_interval eucl_le[where 'a='a]
   860                  intro!: Real_setprod )
   861       also have "\<dots> = measure ?P (?T X)"
   862         unfolding * by (subst lborel_space.measure_times) auto
   863       finally show ?thesis .
   864     qed simp }
   865 qed
   866 
   867 lemma measure_preserving_p2e:
   868   "p2e \<in> measure_preserving (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. (lborel :: real measure_space))
   869     (lborel::'a::ordered_euclidean_space measure_space)" (is "_ \<in> measure_preserving ?P ?E")
   870 proof
   871   show "p2e \<in> measurable ?P ?E"
   872     using measurable_p2e by (simp add: measurable_def)
   873   fix A :: "'a set" assume "A \<in> sets lborel"
   874   then show "lborel_space.\<mu> DIM('a) (p2e -` A \<inter> (space (lborel_space.P DIM('a)))) = lborel.\<mu> A"
   875     by (intro lborel_eq_lborel_space[symmetric]) simp
   876 qed
   877 
   878 lemma lebesgue_eq_lborel_space_in_borel:
   879   fixes A :: "('a::ordered_euclidean_space) set"
   880   assumes A: "A \<in> sets borel"
   881   shows "lebesgue.\<mu> A = lborel_space.\<mu> DIM('a) (p2e -` A \<inter> (space (lborel_space.P DIM('a))))"
   882   using lborel_eq_lborel_space[OF A] by simp
   883 
   884 lemma borel_fubini_positiv_integral:
   885   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> pextreal"
   886   assumes f: "f \<in> borel_measurable borel"
   887   shows "integral\<^isup>P lborel f = \<integral>\<^isup>+x. f (p2e x) \<partial>(lborel_space.P DIM('a))"
   888 proof (rule lborel_space.positive_integral_vimage[OF _ measure_preserving_p2e _])
   889   show "f \<in> borel_measurable lborel"
   890     using f by (simp_all add: measurable_def)
   891 qed default
   892 
   893 lemma borel_fubini_integrable:
   894   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   895   shows "integrable lborel f \<longleftrightarrow>
   896     integrable (lborel_space.P DIM('a)) (\<lambda>x. f (p2e x))"
   897     (is "_ \<longleftrightarrow> integrable ?B ?f")
   898 proof
   899   assume "integrable lborel f"
   900   moreover then have f: "f \<in> borel_measurable borel"
   901     by auto
   902   moreover with measurable_p2e
   903   have "f \<circ> p2e \<in> borel_measurable ?B"
   904     by (rule measurable_comp)
   905   ultimately show "integrable ?B ?f"
   906     by (simp add: comp_def borel_fubini_positiv_integral integrable_def)
   907 next
   908   assume "integrable ?B ?f"
   909   moreover then
   910   have "?f \<circ> e2p \<in> borel_measurable (borel::'a algebra)"
   911     by (auto intro!: measurable_e2p measurable_comp)
   912   then have "f \<in> borel_measurable borel"
   913     by (simp cong: measurable_cong)
   914   ultimately show "integrable lborel f"
   915     by (simp add: borel_fubini_positiv_integral integrable_def)
   916 qed
   917 
   918 lemma borel_fubini:
   919   fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   920   assumes f: "f \<in> borel_measurable borel"
   921   shows "integral\<^isup>L lborel f = \<integral>x. f (p2e x) \<partial>(lborel_space.P DIM('a))"
   922   using f by (simp add: borel_fubini_positiv_integral lebesgue_integral_def)
   923 
   924 end