src/HOL/Analysis/Lebesgue_Measure.thy
author paulson <lp15@cam.ac.uk>
Wed Apr 18 15:57:36 2018 +0100 (22 months ago ago)
changeset 67999 1b05f74f2e5f
parent 67998 73a5a33486ee
child 68046 6aba668aea78
permissions -rw-r--r--
tidying up including contributions from Paulo Emílio de Vilhena
     1 (*  Title:      HOL/Analysis/Lebesgue_Measure.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Robert Himmelmann, TU München
     4     Author:     Jeremy Avigad
     5     Author:     Luke Serafin
     6 *)
     7 
     8 section \<open>Lebesgue measure\<close>
     9 
    10 theory Lebesgue_Measure
    11   imports Finite_Product_Measure Bochner_Integration Caratheodory Complete_Measure Summation_Tests Regularity
    12 begin
    13 
    14 lemma measure_eqI_lessThan:
    15   fixes M N :: "real measure"
    16   assumes sets: "sets M = sets borel" "sets N = sets borel"
    17   assumes fin: "\<And>x. emeasure M {x <..} < \<infinity>"
    18   assumes "\<And>x. emeasure M {x <..} = emeasure N {x <..}"
    19   shows "M = N"
    20 proof (rule measure_eqI_generator_eq_countable)
    21   let ?LT = "\<lambda>a::real. {a <..}" let ?E = "range ?LT"
    22   show "Int_stable ?E"
    23     by (auto simp: Int_stable_def lessThan_Int_lessThan)
    24 
    25   show "?E \<subseteq> Pow UNIV" "sets M = sigma_sets UNIV ?E" "sets N = sigma_sets UNIV ?E"
    26     unfolding sets borel_Ioi by auto
    27 
    28   show "?LT`Rats \<subseteq> ?E" "(\<Union>i\<in>Rats. ?LT i) = UNIV" "\<And>a. a \<in> ?LT`Rats \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
    29     using fin by (auto intro: Rats_no_bot_less simp: less_top)
    30 qed (auto intro: assms countable_rat)
    31 
    32 subsection \<open>Every right continuous and nondecreasing function gives rise to a measure\<close>
    33 
    34 definition interval_measure :: "(real \<Rightarrow> real) \<Rightarrow> real measure" where
    35   "interval_measure F = extend_measure UNIV {(a, b). a \<le> b} (\<lambda>(a, b). {a <.. b}) (\<lambda>(a, b). ennreal (F b - F a))"
    36 
    37 lemma emeasure_interval_measure_Ioc:
    38   assumes "a \<le> b"
    39   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
    40   assumes right_cont_F : "\<And>a. continuous (at_right a) F"
    41   shows "emeasure (interval_measure F) {a <.. b} = F b - F a"
    42 proof (rule extend_measure_caratheodory_pair[OF interval_measure_def \<open>a \<le> b\<close>])
    43   show "semiring_of_sets UNIV {{a<..b} |a b :: real. a \<le> b}"
    44   proof (unfold_locales, safe)
    45     fix a b c d :: real assume *: "a \<le> b" "c \<le> d"
    46     then show "\<exists>C\<subseteq>{{a<..b} |a b. a \<le> b}. finite C \<and> disjoint C \<and> {a<..b} - {c<..d} = \<Union>C"
    47     proof cases
    48       let ?C = "{{a<..b}}"
    49       assume "b < c \<or> d \<le> a \<or> d \<le> c"
    50       with * have "?C \<subseteq> {{a<..b} |a b. a \<le> b} \<and> finite ?C \<and> disjoint ?C \<and> {a<..b} - {c<..d} = \<Union>?C"
    51         by (auto simp add: disjoint_def)
    52       thus ?thesis ..
    53     next
    54       let ?C = "{{a<..c}, {d<..b}}"
    55       assume "\<not> (b < c \<or> d \<le> a \<or> d \<le> c)"
    56       with * have "?C \<subseteq> {{a<..b} |a b. a \<le> b} \<and> finite ?C \<and> disjoint ?C \<and> {a<..b} - {c<..d} = \<Union>?C"
    57         by (auto simp add: disjoint_def Ioc_inj) (metis linear)+
    58       thus ?thesis ..
    59     qed
    60   qed (auto simp: Ioc_inj, metis linear)
    61 next
    62   fix l r :: "nat \<Rightarrow> real" and a b :: real
    63   assume l_r[simp]: "\<And>n. l n \<le> r n" and "a \<le> b" and disj: "disjoint_family (\<lambda>n. {l n<..r n})"
    64   assume lr_eq_ab: "(\<Union>i. {l i<..r i}) = {a<..b}"
    65 
    66   have [intro, simp]: "\<And>a b. a \<le> b \<Longrightarrow> F a \<le> F b"
    67     by (auto intro!: l_r mono_F)
    68 
    69   { fix S :: "nat set" assume "finite S"
    70     moreover note \<open>a \<le> b\<close>
    71     moreover have "\<And>i. i \<in> S \<Longrightarrow> {l i <.. r i} \<subseteq> {a <.. b}"
    72       unfolding lr_eq_ab[symmetric] by auto
    73     ultimately have "(\<Sum>i\<in>S. F (r i) - F (l i)) \<le> F b - F a"
    74     proof (induction S arbitrary: a rule: finite_psubset_induct)
    75       case (psubset S)
    76       show ?case
    77       proof cases
    78         assume "\<exists>i\<in>S. l i < r i"
    79         with \<open>finite S\<close> have "Min (l ` {i\<in>S. l i < r i}) \<in> l ` {i\<in>S. l i < r i}"
    80           by (intro Min_in) auto
    81         then obtain m where m: "m \<in> S" "l m < r m" "l m = Min (l ` {i\<in>S. l i < r i})"
    82           by fastforce
    83 
    84         have "(\<Sum>i\<in>S. F (r i) - F (l i)) = (F (r m) - F (l m)) + (\<Sum>i\<in>S - {m}. F (r i) - F (l i))"
    85           using m psubset by (intro sum.remove) auto
    86         also have "(\<Sum>i\<in>S - {m}. F (r i) - F (l i)) \<le> F b - F (r m)"
    87         proof (intro psubset.IH)
    88           show "S - {m} \<subset> S"
    89             using \<open>m\<in>S\<close> by auto
    90           show "r m \<le> b"
    91             using psubset.prems(2)[OF \<open>m\<in>S\<close>] \<open>l m < r m\<close> by auto
    92         next
    93           fix i assume "i \<in> S - {m}"
    94           then have i: "i \<in> S" "i \<noteq> m" by auto
    95           { assume i': "l i < r i" "l i < r m"
    96             with \<open>finite S\<close> i m have "l m \<le> l i"
    97               by auto
    98             with i' have "{l i <.. r i} \<inter> {l m <.. r m} \<noteq> {}"
    99               by auto
   100             then have False
   101               using disjoint_family_onD[OF disj, of i m] i by auto }
   102           then have "l i \<noteq> r i \<Longrightarrow> r m \<le> l i"
   103             unfolding not_less[symmetric] using l_r[of i] by auto
   104           then show "{l i <.. r i} \<subseteq> {r m <.. b}"
   105             using psubset.prems(2)[OF \<open>i\<in>S\<close>] by auto
   106         qed
   107         also have "F (r m) - F (l m) \<le> F (r m) - F a"
   108           using psubset.prems(2)[OF \<open>m \<in> S\<close>] \<open>l m < r m\<close>
   109           by (auto simp add: Ioc_subset_iff intro!: mono_F)
   110         finally show ?case
   111           by (auto intro: add_mono)
   112       qed (auto simp add: \<open>a \<le> b\<close> less_le)
   113     qed }
   114   note claim1 = this
   115 
   116   (* second key induction: a lower bound on the measures of any finite collection of Ai's
   117      that cover an interval {u..v} *)
   118 
   119   { fix S u v and l r :: "nat \<Rightarrow> real"
   120     assume "finite S" "\<And>i. i\<in>S \<Longrightarrow> l i < r i" "{u..v} \<subseteq> (\<Union>i\<in>S. {l i<..< r i})"
   121     then have "F v - F u \<le> (\<Sum>i\<in>S. F (r i) - F (l i))"
   122     proof (induction arbitrary: v u rule: finite_psubset_induct)
   123       case (psubset S)
   124       show ?case
   125       proof cases
   126         assume "S = {}" then show ?case
   127           using psubset by (simp add: mono_F)
   128       next
   129         assume "S \<noteq> {}"
   130         then obtain j where "j \<in> S"
   131           by auto
   132 
   133         let ?R = "r j < u \<or> l j > v \<or> (\<exists>i\<in>S-{j}. l i \<le> l j \<and> r j \<le> r i)"
   134         show ?case
   135         proof cases
   136           assume "?R"
   137           with \<open>j \<in> S\<close> psubset.prems have "{u..v} \<subseteq> (\<Union>i\<in>S-{j}. {l i<..< r i})"
   138             apply (auto simp: subset_eq Ball_def)
   139             apply (metis Diff_iff less_le_trans leD linear singletonD)
   140             apply (metis Diff_iff less_le_trans leD linear singletonD)
   141             apply (metis order_trans less_le_not_le linear)
   142             done
   143           with \<open>j \<in> S\<close> have "F v - F u \<le> (\<Sum>i\<in>S - {j}. F (r i) - F (l i))"
   144             by (intro psubset) auto
   145           also have "\<dots> \<le> (\<Sum>i\<in>S. F (r i) - F (l i))"
   146             using psubset.prems
   147             by (intro sum_mono2 psubset) (auto intro: less_imp_le)
   148           finally show ?thesis .
   149         next
   150           assume "\<not> ?R"
   151           then have j: "u \<le> r j" "l j \<le> v" "\<And>i. i \<in> S - {j} \<Longrightarrow> r i < r j \<or> l i > l j"
   152             by (auto simp: not_less)
   153           let ?S1 = "{i \<in> S. l i < l j}"
   154           let ?S2 = "{i \<in> S. r i > r j}"
   155 
   156           have "(\<Sum>i\<in>S. F (r i) - F (l i)) \<ge> (\<Sum>i\<in>?S1 \<union> ?S2 \<union> {j}. F (r i) - F (l i))"
   157             using \<open>j \<in> S\<close> \<open>finite S\<close> psubset.prems j
   158             by (intro sum_mono2) (auto intro: less_imp_le)
   159           also have "(\<Sum>i\<in>?S1 \<union> ?S2 \<union> {j}. F (r i) - F (l i)) =
   160             (\<Sum>i\<in>?S1. F (r i) - F (l i)) + (\<Sum>i\<in>?S2 . F (r i) - F (l i)) + (F (r j) - F (l j))"
   161             using psubset(1) psubset.prems(1) j
   162             apply (subst sum.union_disjoint)
   163             apply simp_all
   164             apply (subst sum.union_disjoint)
   165             apply auto
   166             apply (metis less_le_not_le)
   167             done
   168           also (xtrans) have "(\<Sum>i\<in>?S1. F (r i) - F (l i)) \<ge> F (l j) - F u"
   169             using \<open>j \<in> S\<close> \<open>finite S\<close> psubset.prems j
   170             apply (intro psubset.IH psubset)
   171             apply (auto simp: subset_eq Ball_def)
   172             apply (metis less_le_trans not_le)
   173             done
   174           also (xtrans) have "(\<Sum>i\<in>?S2. F (r i) - F (l i)) \<ge> F v - F (r j)"
   175             using \<open>j \<in> S\<close> \<open>finite S\<close> psubset.prems j
   176             apply (intro psubset.IH psubset)
   177             apply (auto simp: subset_eq Ball_def)
   178             apply (metis le_less_trans not_le)
   179             done
   180           finally (xtrans) show ?case
   181             by (auto simp: add_mono)
   182         qed
   183       qed
   184     qed }
   185   note claim2 = this
   186 
   187   (* now prove the inequality going the other way *)
   188   have "ennreal (F b - F a) \<le> (\<Sum>i. ennreal (F (r i) - F (l i)))"
   189   proof (rule ennreal_le_epsilon)
   190     fix epsilon :: real assume egt0: "epsilon > 0"
   191     have "\<forall>i. \<exists>d>0. F (r i + d) < F (r i) + epsilon / 2^(i+2)"
   192     proof
   193       fix i
   194       note right_cont_F [of "r i"]
   195       thus "\<exists>d>0. F (r i + d) < F (r i) + epsilon / 2^(i+2)"
   196         apply -
   197         apply (subst (asm) continuous_at_right_real_increasing)
   198         apply (rule mono_F, assumption)
   199         apply (drule_tac x = "epsilon / 2 ^ (i + 2)" in spec)
   200         apply (erule impE)
   201         using egt0 by (auto simp add: field_simps)
   202     qed
   203     then obtain delta where
   204         deltai_gt0: "\<And>i. delta i > 0" and
   205         deltai_prop: "\<And>i. F (r i + delta i) < F (r i) + epsilon / 2^(i+2)"
   206       by metis
   207     have "\<exists>a' > a. F a' - F a < epsilon / 2"
   208       apply (insert right_cont_F [of a])
   209       apply (subst (asm) continuous_at_right_real_increasing)
   210       using mono_F apply force
   211       apply (drule_tac x = "epsilon / 2" in spec)
   212       using egt0 unfolding mult.commute [of 2] by force
   213     then obtain a' where a'lea [arith]: "a' > a" and
   214       a_prop: "F a' - F a < epsilon / 2"
   215       by auto
   216     define S' where "S' = {i. l i < r i}"
   217     obtain S :: "nat set" where
   218       "S \<subseteq> S'" and finS: "finite S" and
   219       Sprop: "{a'..b} \<subseteq> (\<Union>i \<in> S. {l i<..<r i + delta i})"
   220     proof (rule compactE_image)
   221       show "compact {a'..b}"
   222         by (rule compact_Icc)
   223       show "\<And>i. i \<in> S' \<Longrightarrow> open ({l i<..<r i + delta i})" by auto
   224       have "{a'..b} \<subseteq> {a <.. b}"
   225         by auto
   226       also have "{a <.. b} = (\<Union>i\<in>S'. {l i<..r i})"
   227         unfolding lr_eq_ab[symmetric] by (fastforce simp add: S'_def intro: less_le_trans)
   228       also have "\<dots> \<subseteq> (\<Union>i \<in> S'. {l i<..<r i + delta i})"
   229         apply (intro UN_mono)
   230         apply (auto simp: S'_def)
   231         apply (cut_tac i=i in deltai_gt0)
   232         apply simp
   233         done
   234       finally show "{a'..b} \<subseteq> (\<Union>i \<in> S'. {l i<..<r i + delta i})" .
   235     qed
   236     with S'_def have Sprop2: "\<And>i. i \<in> S \<Longrightarrow> l i < r i" by auto
   237     from finS have "\<exists>n. \<forall>i \<in> S. i \<le> n"
   238       by (subst finite_nat_set_iff_bounded_le [symmetric])
   239     then obtain n where Sbound [rule_format]: "\<forall>i \<in> S. i \<le> n" ..
   240     have "F b - F a' \<le> (\<Sum>i\<in>S. F (r i + delta i) - F (l i))"
   241       apply (rule claim2 [rule_format])
   242       using finS Sprop apply auto
   243       apply (frule Sprop2)
   244       apply (subgoal_tac "delta i > 0")
   245       apply arith
   246       by (rule deltai_gt0)
   247     also have "... \<le> (\<Sum>i \<in> S. F(r i) - F(l i) + epsilon / 2^(i+2))"
   248       apply (rule sum_mono)
   249       apply simp
   250       apply (rule order_trans)
   251       apply (rule less_imp_le)
   252       apply (rule deltai_prop)
   253       by auto
   254     also have "... = (\<Sum>i \<in> S. F(r i) - F(l i)) +
   255         (epsilon / 4) * (\<Sum>i \<in> S. (1 / 2)^i)" (is "_ = ?t + _")
   256       by (subst sum.distrib) (simp add: field_simps sum_distrib_left)
   257     also have "... \<le> ?t + (epsilon / 4) * (\<Sum> i < Suc n. (1 / 2)^i)"
   258       apply (rule add_left_mono)
   259       apply (rule mult_left_mono)
   260       apply (rule sum_mono2)
   261       using egt0 apply auto
   262       by (frule Sbound, auto)
   263     also have "... \<le> ?t + (epsilon / 2)"
   264       apply (rule add_left_mono)
   265       apply (subst geometric_sum)
   266       apply auto
   267       apply (rule mult_left_mono)
   268       using egt0 apply auto
   269       done
   270     finally have aux2: "F b - F a' \<le> (\<Sum>i\<in>S. F (r i) - F (l i)) + epsilon / 2"
   271       by simp
   272 
   273     have "F b - F a = (F b - F a') + (F a' - F a)"
   274       by auto
   275     also have "... \<le> (F b - F a') + epsilon / 2"
   276       using a_prop by (intro add_left_mono) simp
   277     also have "... \<le> (\<Sum>i\<in>S. F (r i) - F (l i)) + epsilon / 2 + epsilon / 2"
   278       apply (intro add_right_mono)
   279       apply (rule aux2)
   280       done
   281     also have "... = (\<Sum>i\<in>S. F (r i) - F (l i)) + epsilon"
   282       by auto
   283     also have "... \<le> (\<Sum>i\<le>n. F (r i) - F (l i)) + epsilon"
   284       using finS Sbound Sprop by (auto intro!: add_right_mono sum_mono2)
   285     finally have "ennreal (F b - F a) \<le> (\<Sum>i\<le>n. ennreal (F (r i) - F (l i))) + epsilon"
   286       using egt0 by (simp add: ennreal_plus[symmetric] sum_nonneg del: ennreal_plus)
   287     then show "ennreal (F b - F a) \<le> (\<Sum>i. ennreal (F (r i) - F (l i))) + (epsilon :: real)"
   288       by (rule order_trans) (auto intro!: add_mono sum_le_suminf simp del: sum_ennreal)
   289   qed
   290   moreover have "(\<Sum>i. ennreal (F (r i) - F (l i))) \<le> ennreal (F b - F a)"
   291     using \<open>a \<le> b\<close> by (auto intro!: suminf_le_const ennreal_le_iff[THEN iffD2] claim1)
   292   ultimately show "(\<Sum>n. ennreal (F (r n) - F (l n))) = ennreal (F b - F a)"
   293     by (rule antisym[rotated])
   294 qed (auto simp: Ioc_inj mono_F)
   295 
   296 lemma measure_interval_measure_Ioc:
   297   assumes "a \<le> b"
   298   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
   299   assumes right_cont_F : "\<And>a. continuous (at_right a) F"
   300   shows "measure (interval_measure F) {a <.. b} = F b - F a"
   301   unfolding measure_def
   302   apply (subst emeasure_interval_measure_Ioc)
   303   apply fact+
   304   apply (simp add: assms)
   305   done
   306 
   307 lemma emeasure_interval_measure_Ioc_eq:
   308   "(\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y) \<Longrightarrow> (\<And>a. continuous (at_right a) F) \<Longrightarrow>
   309     emeasure (interval_measure F) {a <.. b} = (if a \<le> b then F b - F a else 0)"
   310   using emeasure_interval_measure_Ioc[of a b F] by auto
   311 
   312 lemma sets_interval_measure [simp, measurable_cong]: "sets (interval_measure F) = sets borel"
   313   apply (simp add: sets_extend_measure interval_measure_def borel_sigma_sets_Ioc)
   314   apply (rule sigma_sets_eqI)
   315   apply auto
   316   apply (case_tac "a \<le> ba")
   317   apply (auto intro: sigma_sets.Empty)
   318   done
   319 
   320 lemma space_interval_measure [simp]: "space (interval_measure F) = UNIV"
   321   by (simp add: interval_measure_def space_extend_measure)
   322 
   323 lemma emeasure_interval_measure_Icc:
   324   assumes "a \<le> b"
   325   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
   326   assumes cont_F : "continuous_on UNIV F"
   327   shows "emeasure (interval_measure F) {a .. b} = F b - F a"
   328 proof (rule tendsto_unique)
   329   { fix a b :: real assume "a \<le> b" then have "emeasure (interval_measure F) {a <.. b} = F b - F a"
   330       using cont_F
   331       by (subst emeasure_interval_measure_Ioc)
   332          (auto intro: mono_F continuous_within_subset simp: continuous_on_eq_continuous_within) }
   333   note * = this
   334 
   335   let ?F = "interval_measure F"
   336   show "((\<lambda>a. F b - F a) \<longlongrightarrow> emeasure ?F {a..b}) (at_left a)"
   337   proof (rule tendsto_at_left_sequentially)
   338     show "a - 1 < a" by simp
   339     fix X assume "\<And>n. X n < a" "incseq X" "X \<longlonglongrightarrow> a"
   340     with \<open>a \<le> b\<close> have "(\<lambda>n. emeasure ?F {X n<..b}) \<longlonglongrightarrow> emeasure ?F (\<Inter>n. {X n <..b})"
   341       apply (intro Lim_emeasure_decseq)
   342       apply (auto simp: decseq_def incseq_def emeasure_interval_measure_Ioc *)
   343       apply force
   344       apply (subst (asm ) *)
   345       apply (auto intro: less_le_trans less_imp_le)
   346       done
   347     also have "(\<Inter>n. {X n <..b}) = {a..b}"
   348       using \<open>\<And>n. X n < a\<close>
   349       apply auto
   350       apply (rule LIMSEQ_le_const2[OF \<open>X \<longlonglongrightarrow> a\<close>])
   351       apply (auto intro: less_imp_le)
   352       apply (auto intro: less_le_trans)
   353       done
   354     also have "(\<lambda>n. emeasure ?F {X n<..b}) = (\<lambda>n. F b - F (X n))"
   355       using \<open>\<And>n. X n < a\<close> \<open>a \<le> b\<close> by (subst *) (auto intro: less_imp_le less_le_trans)
   356     finally show "(\<lambda>n. F b - F (X n)) \<longlonglongrightarrow> emeasure ?F {a..b}" .
   357   qed
   358   show "((\<lambda>a. ennreal (F b - F a)) \<longlongrightarrow> F b - F a) (at_left a)"
   359     by (rule continuous_on_tendsto_compose[where g="\<lambda>x. x" and s=UNIV])
   360        (auto simp: continuous_on_ennreal continuous_on_diff cont_F continuous_on_const)
   361 qed (rule trivial_limit_at_left_real)
   362 
   363 lemma sigma_finite_interval_measure:
   364   assumes mono_F: "\<And>x y. x \<le> y \<Longrightarrow> F x \<le> F y"
   365   assumes right_cont_F : "\<And>a. continuous (at_right a) F"
   366   shows "sigma_finite_measure (interval_measure F)"
   367   apply unfold_locales
   368   apply (intro exI[of _ "(\<lambda>(a, b). {a <.. b}) ` (\<rat> \<times> \<rat>)"])
   369   apply (auto intro!: Rats_no_top_le Rats_no_bot_less countable_rat simp: emeasure_interval_measure_Ioc_eq[OF assms])
   370   done
   371 
   372 subsection \<open>Lebesgue-Borel measure\<close>
   373 
   374 definition lborel :: "('a :: euclidean_space) measure" where
   375   "lborel = distr (\<Pi>\<^sub>M b\<in>Basis. interval_measure (\<lambda>x. x)) borel (\<lambda>f. \<Sum>b\<in>Basis. f b *\<^sub>R b)"
   376 
   377 abbreviation lebesgue :: "'a::euclidean_space measure"
   378   where "lebesgue \<equiv> completion lborel"
   379 
   380 abbreviation lebesgue_on :: "'a set \<Rightarrow> 'a::euclidean_space measure"
   381   where "lebesgue_on \<Omega> \<equiv> restrict_space (completion lborel) \<Omega>"
   382 
   383 lemma
   384   shows sets_lborel[simp, measurable_cong]: "sets lborel = sets borel"
   385     and space_lborel[simp]: "space lborel = space borel"
   386     and measurable_lborel1[simp]: "measurable M lborel = measurable M borel"
   387     and measurable_lborel2[simp]: "measurable lborel M = measurable borel M"
   388   by (simp_all add: lborel_def)
   389 
   390 lemma sets_lebesgue_on_refl [iff]: "S \<in> sets (lebesgue_on S)"
   391     by (metis inf_top.right_neutral sets.top space_borel space_completion space_lborel space_restrict_space)
   392 
   393 lemma Compl_in_sets_lebesgue: "-A \<in> sets lebesgue \<longleftrightarrow> A  \<in> sets lebesgue"
   394   by (metis Compl_eq_Diff_UNIV double_compl space_borel space_completion space_lborel Sigma_Algebra.sets.compl_sets)
   395 
   396 lemma measurable_lebesgue_cong:
   397   assumes "\<And>x. x \<in> S \<Longrightarrow> f x = g x"
   398   shows "f \<in> measurable (lebesgue_on S) M \<longleftrightarrow> g \<in> measurable (lebesgue_on S) M"
   399   by (metis (mono_tags, lifting) IntD1 assms measurable_cong_strong space_restrict_space)
   400 
   401 text\<open>Measurability of continuous functions\<close>
   402 lemma continuous_imp_measurable_on_sets_lebesgue:
   403   assumes f: "continuous_on S f" and S: "S \<in> sets lebesgue"
   404   shows "f \<in> borel_measurable (lebesgue_on S)"
   405 proof -
   406   have "sets (restrict_space borel S) \<subseteq> sets (lebesgue_on S)"
   407     by (simp add: mono_restrict_space subsetI)
   408   then show ?thesis
   409     by (simp add: borel_measurable_continuous_on_restrict [OF f] borel_measurable_subalgebra 
   410                   space_restrict_space)
   411 qed
   412 
   413 context
   414 begin
   415 
   416 interpretation sigma_finite_measure "interval_measure (\<lambda>x. x)"
   417   by (rule sigma_finite_interval_measure) auto
   418 interpretation finite_product_sigma_finite "\<lambda>_. interval_measure (\<lambda>x. x)" Basis
   419   proof qed simp
   420 
   421 lemma lborel_eq_real: "lborel = interval_measure (\<lambda>x. x)"
   422   unfolding lborel_def Basis_real_def
   423   using distr_id[of "interval_measure (\<lambda>x. x)"]
   424   by (subst distr_component[symmetric])
   425      (simp_all add: distr_distr comp_def del: distr_id cong: distr_cong)
   426 
   427 lemma lborel_eq: "lborel = distr (\<Pi>\<^sub>M b\<in>Basis. lborel) borel (\<lambda>f. \<Sum>b\<in>Basis. f b *\<^sub>R b)"
   428   by (subst lborel_def) (simp add: lborel_eq_real)
   429 
   430 lemma nn_integral_lborel_prod:
   431   assumes [measurable]: "\<And>b. b \<in> Basis \<Longrightarrow> f b \<in> borel_measurable borel"
   432   assumes nn[simp]: "\<And>b x. b \<in> Basis \<Longrightarrow> 0 \<le> f b x"
   433   shows "(\<integral>\<^sup>+x. (\<Prod>b\<in>Basis. f b (x \<bullet> b)) \<partial>lborel) = (\<Prod>b\<in>Basis. (\<integral>\<^sup>+x. f b x \<partial>lborel))"
   434   by (simp add: lborel_def nn_integral_distr product_nn_integral_prod
   435                 product_nn_integral_singleton)
   436 
   437 lemma emeasure_lborel_Icc[simp]:
   438   fixes l u :: real
   439   assumes [simp]: "l \<le> u"
   440   shows "emeasure lborel {l .. u} = u - l"
   441 proof -
   442   have "((\<lambda>f. f 1) -` {l..u} \<inter> space (Pi\<^sub>M {1} (\<lambda>b. interval_measure (\<lambda>x. x)))) = {1::real} \<rightarrow>\<^sub>E {l..u}"
   443     by (auto simp: space_PiM)
   444   then show ?thesis
   445     by (simp add: lborel_def emeasure_distr emeasure_PiM emeasure_interval_measure_Icc continuous_on_id)
   446 qed
   447 
   448 lemma emeasure_lborel_Icc_eq: "emeasure lborel {l .. u} = ennreal (if l \<le> u then u - l else 0)"
   449   by simp
   450 
   451 lemma emeasure_lborel_cbox[simp]:
   452   assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   453   shows "emeasure lborel (cbox l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   454 proof -
   455   have "(\<lambda>x. \<Prod>b\<in>Basis. indicator {l\<bullet>b .. u\<bullet>b} (x \<bullet> b) :: ennreal) = indicator (cbox l u)"
   456     by (auto simp: fun_eq_iff cbox_def split: split_indicator)
   457   then have "emeasure lborel (cbox l u) = (\<integral>\<^sup>+x. (\<Prod>b\<in>Basis. indicator {l\<bullet>b .. u\<bullet>b} (x \<bullet> b)) \<partial>lborel)"
   458     by simp
   459   also have "\<dots> = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   460     by (subst nn_integral_lborel_prod) (simp_all add: prod_ennreal inner_diff_left)
   461   finally show ?thesis .
   462 qed
   463 
   464 lemma AE_lborel_singleton: "AE x in lborel::'a::euclidean_space measure. x \<noteq> c"
   465   using SOME_Basis AE_discrete_difference [of "{c}" lborel] emeasure_lborel_cbox [of c c]
   466   by (auto simp add: power_0_left)
   467 
   468 lemma emeasure_lborel_Ioo[simp]:
   469   assumes [simp]: "l \<le> u"
   470   shows "emeasure lborel {l <..< u} = ennreal (u - l)"
   471 proof -
   472   have "emeasure lborel {l <..< u} = emeasure lborel {l .. u}"
   473     using AE_lborel_singleton[of u] AE_lborel_singleton[of l] by (intro emeasure_eq_AE) auto
   474   then show ?thesis
   475     by simp
   476 qed
   477 
   478 lemma emeasure_lborel_Ioc[simp]:
   479   assumes [simp]: "l \<le> u"
   480   shows "emeasure lborel {l <.. u} = ennreal (u - l)"
   481 proof -
   482   have "emeasure lborel {l <.. u} = emeasure lborel {l .. u}"
   483     using AE_lborel_singleton[of u] AE_lborel_singleton[of l] by (intro emeasure_eq_AE) auto
   484   then show ?thesis
   485     by simp
   486 qed
   487 
   488 lemma emeasure_lborel_Ico[simp]:
   489   assumes [simp]: "l \<le> u"
   490   shows "emeasure lborel {l ..< u} = ennreal (u - l)"
   491 proof -
   492   have "emeasure lborel {l ..< u} = emeasure lborel {l .. u}"
   493     using AE_lborel_singleton[of u] AE_lborel_singleton[of l] by (intro emeasure_eq_AE) auto
   494   then show ?thesis
   495     by simp
   496 qed
   497 
   498 lemma emeasure_lborel_box[simp]:
   499   assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   500   shows "emeasure lborel (box l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   501 proof -
   502   have "(\<lambda>x. \<Prod>b\<in>Basis. indicator {l\<bullet>b <..< u\<bullet>b} (x \<bullet> b) :: ennreal) = indicator (box l u)"
   503     by (auto simp: fun_eq_iff box_def split: split_indicator)
   504   then have "emeasure lborel (box l u) = (\<integral>\<^sup>+x. (\<Prod>b\<in>Basis. indicator {l\<bullet>b <..< u\<bullet>b} (x \<bullet> b)) \<partial>lborel)"
   505     by simp
   506   also have "\<dots> = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   507     by (subst nn_integral_lborel_prod) (simp_all add: prod_ennreal inner_diff_left)
   508   finally show ?thesis .
   509 qed
   510 
   511 lemma emeasure_lborel_cbox_eq:
   512   "emeasure lborel (cbox l u) = (if \<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b then \<Prod>b\<in>Basis. (u - l) \<bullet> b else 0)"
   513   using box_eq_empty(2)[THEN iffD2, of u l] by (auto simp: not_le)
   514 
   515 lemma emeasure_lborel_box_eq:
   516   "emeasure lborel (box l u) = (if \<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b then \<Prod>b\<in>Basis. (u - l) \<bullet> b else 0)"
   517   using box_eq_empty(1)[THEN iffD2, of u l] by (auto simp: not_le dest!: less_imp_le) force
   518 
   519 lemma emeasure_lborel_singleton[simp]: "emeasure lborel {x} = 0"
   520   using emeasure_lborel_cbox[of x x] nonempty_Basis
   521   by (auto simp del: emeasure_lborel_cbox nonempty_Basis simp add: prod_constant)
   522 
   523 lemma fmeasurable_cbox [iff]: "cbox a b \<in> fmeasurable lborel"
   524   and fmeasurable_box [iff]: "box a b \<in> fmeasurable lborel"
   525   by (auto simp: fmeasurable_def emeasure_lborel_box_eq emeasure_lborel_cbox_eq)
   526 
   527 lemma
   528   fixes l u :: real
   529   assumes [simp]: "l \<le> u"
   530   shows measure_lborel_Icc[simp]: "measure lborel {l .. u} = u - l"
   531     and measure_lborel_Ico[simp]: "measure lborel {l ..< u} = u - l"
   532     and measure_lborel_Ioc[simp]: "measure lborel {l <.. u} = u - l"
   533     and measure_lborel_Ioo[simp]: "measure lborel {l <..< u} = u - l"
   534   by (simp_all add: measure_def)
   535 
   536 lemma
   537   assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   538   shows measure_lborel_box[simp]: "measure lborel (box l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   539     and measure_lborel_cbox[simp]: "measure lborel (cbox l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   540   by (simp_all add: measure_def inner_diff_left prod_nonneg)
   541 
   542 lemma measure_lborel_cbox_eq:
   543   "measure lborel (cbox l u) = (if \<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b then \<Prod>b\<in>Basis. (u - l) \<bullet> b else 0)"
   544   using box_eq_empty(2)[THEN iffD2, of u l] by (auto simp: not_le)
   545 
   546 lemma measure_lborel_box_eq:
   547   "measure lborel (box l u) = (if \<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b then \<Prod>b\<in>Basis. (u - l) \<bullet> b else 0)"
   548   using box_eq_empty(1)[THEN iffD2, of u l] by (auto simp: not_le dest!: less_imp_le) force
   549 
   550 lemma measure_lborel_singleton[simp]: "measure lborel {x} = 0"
   551   by (simp add: measure_def)
   552 
   553 lemma sigma_finite_lborel: "sigma_finite_measure lborel"
   554 proof
   555   show "\<exists>A::'a set set. countable A \<and> A \<subseteq> sets lborel \<and> \<Union>A = space lborel \<and> (\<forall>a\<in>A. emeasure lborel a \<noteq> \<infinity>)"
   556     by (intro exI[of _ "range (\<lambda>n::nat. box (- real n *\<^sub>R One) (real n *\<^sub>R One))"])
   557        (auto simp: emeasure_lborel_cbox_eq UN_box_eq_UNIV)
   558 qed
   559 
   560 end
   561 
   562 lemma emeasure_lborel_UNIV [simp]: "emeasure lborel (UNIV::'a::euclidean_space set) = \<infinity>"
   563 proof -
   564   { fix n::nat
   565     let ?Ba = "Basis :: 'a set"
   566     have "real n \<le> (2::real) ^ card ?Ba * real n"
   567       by (simp add: mult_le_cancel_right1)
   568     also
   569     have "... \<le> (2::real) ^ card ?Ba * real (Suc n) ^ card ?Ba"
   570       apply (rule mult_left_mono)
   571       apply (metis DIM_positive One_nat_def less_eq_Suc_le less_imp_le of_nat_le_iff of_nat_power self_le_power zero_less_Suc)
   572       apply (simp add: DIM_positive)
   573       done
   574     finally have "real n \<le> (2::real) ^ card ?Ba * real (Suc n) ^ card ?Ba" .
   575   } note [intro!] = this
   576   show ?thesis
   577     unfolding UN_box_eq_UNIV[symmetric]
   578     apply (subst SUP_emeasure_incseq[symmetric])
   579     apply (auto simp: incseq_def subset_box inner_add_left prod_constant
   580       simp del: Sup_eq_top_iff SUP_eq_top_iff
   581       intro!: ennreal_SUP_eq_top)
   582     done
   583 qed
   584 
   585 lemma emeasure_lborel_countable:
   586   fixes A :: "'a::euclidean_space set"
   587   assumes "countable A"
   588   shows "emeasure lborel A = 0"
   589 proof -
   590   have "A \<subseteq> (\<Union>i. {from_nat_into A i})" using from_nat_into_surj assms by force
   591   then have "emeasure lborel A \<le> emeasure lborel (\<Union>i. {from_nat_into A i})"
   592     by (intro emeasure_mono) auto
   593   also have "emeasure lborel (\<Union>i. {from_nat_into A i}) = 0"
   594     by (rule emeasure_UN_eq_0) auto
   595   finally show ?thesis
   596     by (auto simp add: )
   597 qed
   598 
   599 lemma countable_imp_null_set_lborel: "countable A \<Longrightarrow> A \<in> null_sets lborel"
   600   by (simp add: null_sets_def emeasure_lborel_countable sets.countable)
   601 
   602 lemma finite_imp_null_set_lborel: "finite A \<Longrightarrow> A \<in> null_sets lborel"
   603   by (intro countable_imp_null_set_lborel countable_finite)
   604 
   605 lemma lborel_neq_count_space[simp]: "lborel \<noteq> count_space (A::('a::ordered_euclidean_space) set)"
   606 proof
   607   assume asm: "lborel = count_space A"
   608   have "space lborel = UNIV" by simp
   609   hence [simp]: "A = UNIV" by (subst (asm) asm) (simp only: space_count_space)
   610   have "emeasure lborel {undefined::'a} = 1"
   611       by (subst asm, subst emeasure_count_space_finite) auto
   612   moreover have "emeasure lborel {undefined} \<noteq> 1" by simp
   613   ultimately show False by contradiction
   614 qed
   615 
   616 lemma mem_closed_if_AE_lebesgue_open:
   617   assumes "open S" "closed C"
   618   assumes "AE x \<in> S in lebesgue. x \<in> C"
   619   assumes "x \<in> S"
   620   shows "x \<in> C"
   621 proof (rule ccontr)
   622   assume xC: "x \<notin> C"
   623   with openE[of "S - C"] assms
   624   obtain e where e: "0 < e" "ball x e \<subseteq> S - C"
   625     by blast
   626   then obtain a b where box: "x \<in> box a b" "box a b \<subseteq> S - C"
   627     by (metis rational_boxes order_trans)
   628   then have "0 < emeasure lebesgue (box a b)"
   629     by (auto simp: emeasure_lborel_box_eq mem_box algebra_simps intro!: prod_pos)
   630   also have "\<dots> \<le> emeasure lebesgue (S - C)"
   631     using assms box
   632     by (auto intro!: emeasure_mono)
   633   also have "\<dots> = 0"
   634     using assms
   635     by (auto simp: eventually_ae_filter completion.complete2 set_diff_eq null_setsD1)
   636   finally show False by simp
   637 qed
   638 
   639 lemma mem_closed_if_AE_lebesgue: "closed C \<Longrightarrow> (AE x in lebesgue. x \<in> C) \<Longrightarrow> x \<in> C"
   640   using mem_closed_if_AE_lebesgue_open[OF open_UNIV] by simp
   641 
   642 
   643 subsection \<open>Affine transformation on the Lebesgue-Borel\<close>
   644 
   645 lemma lborel_eqI:
   646   fixes M :: "'a::euclidean_space measure"
   647   assumes emeasure_eq: "\<And>l u. (\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b) \<Longrightarrow> emeasure M (box l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)"
   648   assumes sets_eq: "sets M = sets borel"
   649   shows "lborel = M"
   650 proof (rule measure_eqI_generator_eq)
   651   let ?E = "range (\<lambda>(a, b). box a b::'a set)"
   652   show "Int_stable ?E"
   653     by (auto simp: Int_stable_def box_Int_box)
   654 
   655   show "?E \<subseteq> Pow UNIV" "sets lborel = sigma_sets UNIV ?E" "sets M = sigma_sets UNIV ?E"
   656     by (simp_all add: borel_eq_box sets_eq)
   657 
   658   let ?A = "\<lambda>n::nat. box (- (real n *\<^sub>R One)) (real n *\<^sub>R One) :: 'a set"
   659   show "range ?A \<subseteq> ?E" "(\<Union>i. ?A i) = UNIV"
   660     unfolding UN_box_eq_UNIV by auto
   661 
   662   { fix i show "emeasure lborel (?A i) \<noteq> \<infinity>" by auto }
   663   { fix X assume "X \<in> ?E" then show "emeasure lborel X = emeasure M X"
   664       apply (auto simp: emeasure_eq emeasure_lborel_box_eq)
   665       apply (subst box_eq_empty(1)[THEN iffD2])
   666       apply (auto intro: less_imp_le simp: not_le)
   667       done }
   668 qed
   669 
   670 lemma lborel_affine_euclidean:
   671   fixes c :: "'a::euclidean_space \<Rightarrow> real" and t
   672   defines "T x \<equiv> t + (\<Sum>j\<in>Basis. (c j * (x \<bullet> j)) *\<^sub>R j)"
   673   assumes c: "\<And>j. j \<in> Basis \<Longrightarrow> c j \<noteq> 0"
   674   shows "lborel = density (distr lborel borel T) (\<lambda>_. (\<Prod>j\<in>Basis. \<bar>c j\<bar>))" (is "_ = ?D")
   675 proof (rule lborel_eqI)
   676   let ?B = "Basis :: 'a set"
   677   fix l u assume le: "\<And>b. b \<in> ?B \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b"
   678   have [measurable]: "T \<in> borel \<rightarrow>\<^sub>M borel"
   679     by (simp add: T_def[abs_def])
   680   have eq: "T -` box l u = box
   681     (\<Sum>j\<in>Basis. (((if 0 < c j then l - t else u - t) \<bullet> j) / c j) *\<^sub>R j)
   682     (\<Sum>j\<in>Basis. (((if 0 < c j then u - t else l - t) \<bullet> j) / c j) *\<^sub>R j)"
   683     using c by (auto simp: box_def T_def field_simps inner_simps divide_less_eq)
   684   with le c show "emeasure ?D (box l u) = (\<Prod>b\<in>?B. (u - l) \<bullet> b)"
   685     by (auto simp: emeasure_density emeasure_distr nn_integral_multc emeasure_lborel_box_eq inner_simps
   686                    field_simps divide_simps ennreal_mult'[symmetric] prod_nonneg prod.distrib[symmetric]
   687              intro!: prod.cong)
   688 qed simp
   689 
   690 lemma lborel_affine:
   691   fixes t :: "'a::euclidean_space"
   692   shows "c \<noteq> 0 \<Longrightarrow> lborel = density (distr lborel borel (\<lambda>x. t + c *\<^sub>R x)) (\<lambda>_. \<bar>c\<bar>^DIM('a))"
   693   using lborel_affine_euclidean[where c="\<lambda>_::'a. c" and t=t]
   694   unfolding scaleR_scaleR[symmetric] scaleR_sum_right[symmetric] euclidean_representation prod_constant by simp
   695 
   696 lemma lborel_real_affine:
   697   "c \<noteq> 0 \<Longrightarrow> lborel = density (distr lborel borel (\<lambda>x. t + c * x)) (\<lambda>_. ennreal (abs c))"
   698   using lborel_affine[of c t] by simp
   699 
   700 lemma AE_borel_affine:
   701   fixes P :: "real \<Rightarrow> bool"
   702   shows "c \<noteq> 0 \<Longrightarrow> Measurable.pred borel P \<Longrightarrow> AE x in lborel. P x \<Longrightarrow> AE x in lborel. P (t + c * x)"
   703   by (subst lborel_real_affine[where t="- t / c" and c="1 / c"])
   704      (simp_all add: AE_density AE_distr_iff field_simps)
   705 
   706 lemma nn_integral_real_affine:
   707   fixes c :: real assumes [measurable]: "f \<in> borel_measurable borel" and c: "c \<noteq> 0"
   708   shows "(\<integral>\<^sup>+x. f x \<partial>lborel) = \<bar>c\<bar> * (\<integral>\<^sup>+x. f (t + c * x) \<partial>lborel)"
   709   by (subst lborel_real_affine[OF c, of t])
   710      (simp add: nn_integral_density nn_integral_distr nn_integral_cmult)
   711 
   712 lemma lborel_integrable_real_affine:
   713   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
   714   assumes f: "integrable lborel f"
   715   shows "c \<noteq> 0 \<Longrightarrow> integrable lborel (\<lambda>x. f (t + c * x))"
   716   using f f[THEN borel_measurable_integrable] unfolding integrable_iff_bounded
   717   by (subst (asm) nn_integral_real_affine[where c=c and t=t]) (auto simp: ennreal_mult_less_top)
   718 
   719 lemma lborel_integrable_real_affine_iff:
   720   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}"
   721   shows "c \<noteq> 0 \<Longrightarrow> integrable lborel (\<lambda>x. f (t + c * x)) \<longleftrightarrow> integrable lborel f"
   722   using
   723     lborel_integrable_real_affine[of f c t]
   724     lborel_integrable_real_affine[of "\<lambda>x. f (t + c * x)" "1/c" "-t/c"]
   725   by (auto simp add: field_simps)
   726 
   727 lemma lborel_integral_real_affine:
   728   fixes f :: "real \<Rightarrow> 'a :: {banach, second_countable_topology}" and c :: real
   729   assumes c: "c \<noteq> 0" shows "(\<integral>x. f x \<partial> lborel) = \<bar>c\<bar> *\<^sub>R (\<integral>x. f (t + c * x) \<partial>lborel)"
   730 proof cases
   731   assume f[measurable]: "integrable lborel f" then show ?thesis
   732     using c f f[THEN borel_measurable_integrable] f[THEN lborel_integrable_real_affine, of c t]
   733     by (subst lborel_real_affine[OF c, of t])
   734        (simp add: integral_density integral_distr)
   735 next
   736   assume "\<not> integrable lborel f" with c show ?thesis
   737     by (simp add: lborel_integrable_real_affine_iff not_integrable_integral_eq)
   738 qed
   739 
   740 lemma
   741   fixes c :: "'a::euclidean_space \<Rightarrow> real" and t
   742   assumes c: "\<And>j. j \<in> Basis \<Longrightarrow> c j \<noteq> 0"
   743   defines "T == (\<lambda>x. t + (\<Sum>j\<in>Basis. (c j * (x \<bullet> j)) *\<^sub>R j))"
   744   shows lebesgue_affine_euclidean: "lebesgue = density (distr lebesgue lebesgue T) (\<lambda>_. (\<Prod>j\<in>Basis. \<bar>c j\<bar>))" (is "_ = ?D")
   745     and lebesgue_affine_measurable: "T \<in> lebesgue \<rightarrow>\<^sub>M lebesgue"
   746 proof -
   747   have T_borel[measurable]: "T \<in> borel \<rightarrow>\<^sub>M borel"
   748     by (auto simp: T_def[abs_def])
   749   { fix A :: "'a set" assume A: "A \<in> sets borel"
   750     then have "emeasure lborel A = 0 \<longleftrightarrow> emeasure (density (distr lborel borel T) (\<lambda>_. (\<Prod>j\<in>Basis. \<bar>c j\<bar>))) A = 0"
   751       unfolding T_def using c by (subst lborel_affine_euclidean[symmetric]) auto
   752     also have "\<dots> \<longleftrightarrow> emeasure (distr lebesgue lborel T) A = 0"
   753       using A c by (simp add: distr_completion emeasure_density nn_integral_cmult prod_nonneg cong: distr_cong)
   754     finally have "emeasure lborel A = 0 \<longleftrightarrow> emeasure (distr lebesgue lborel T) A = 0" . }
   755   then have eq: "null_sets lborel = null_sets (distr lebesgue lborel T)"
   756     by (auto simp: null_sets_def)
   757 
   758   show "T \<in> lebesgue \<rightarrow>\<^sub>M lebesgue"
   759     by (rule completion.measurable_completion2) (auto simp: eq measurable_completion)
   760 
   761   have "lebesgue = completion (density (distr lborel borel T) (\<lambda>_. (\<Prod>j\<in>Basis. \<bar>c j\<bar>)))"
   762     using c by (subst lborel_affine_euclidean[of c t]) (simp_all add: T_def[abs_def])
   763   also have "\<dots> = density (completion (distr lebesgue lborel T)) (\<lambda>_. (\<Prod>j\<in>Basis. \<bar>c j\<bar>))"
   764     using c by (auto intro!: always_eventually prod_pos completion_density_eq simp: distr_completion cong: distr_cong)
   765   also have "\<dots> = density (distr lebesgue lebesgue T) (\<lambda>_. (\<Prod>j\<in>Basis. \<bar>c j\<bar>))"
   766     by (subst completion.completion_distr_eq) (auto simp: eq measurable_completion)
   767   finally show "lebesgue = density (distr lebesgue lebesgue T) (\<lambda>_. (\<Prod>j\<in>Basis. \<bar>c j\<bar>))" .
   768 qed
   769 
   770 lemma lebesgue_measurable_scaling[measurable]: "( *\<^sub>R) x \<in> lebesgue \<rightarrow>\<^sub>M lebesgue"
   771 proof cases
   772   assume "x = 0"
   773   then have "( *\<^sub>R) x = (\<lambda>x. 0::'a)"
   774     by (auto simp: fun_eq_iff)
   775   then show ?thesis by auto
   776 next
   777   assume "x \<noteq> 0" then show ?thesis
   778     using lebesgue_affine_measurable[of "\<lambda>_. x" 0]
   779     unfolding scaleR_scaleR[symmetric] scaleR_sum_right[symmetric] euclidean_representation
   780     by (auto simp add: ac_simps)
   781 qed
   782 
   783 lemma
   784   fixes m :: real and \<delta> :: "'a::euclidean_space"
   785   defines "T r d x \<equiv> r *\<^sub>R x + d"
   786   shows emeasure_lebesgue_affine: "emeasure lebesgue (T m \<delta> ` S) = \<bar>m\<bar> ^ DIM('a) * emeasure lebesgue S" (is ?e)
   787     and measure_lebesgue_affine: "measure lebesgue (T m \<delta> ` S) = \<bar>m\<bar> ^ DIM('a) * measure lebesgue S" (is ?m)
   788 proof -
   789   show ?e
   790   proof cases
   791     assume "m = 0" then show ?thesis
   792       by (simp add: image_constant_conv T_def[abs_def])
   793   next
   794     let ?T = "T m \<delta>" and ?T' = "T (1 / m) (- ((1/m) *\<^sub>R \<delta>))"
   795     assume "m \<noteq> 0"
   796     then have s_comp_s: "?T' \<circ> ?T = id" "?T \<circ> ?T' = id"
   797       by (auto simp: T_def[abs_def] fun_eq_iff scaleR_add_right scaleR_diff_right)
   798     then have "inv ?T' = ?T" "bij ?T'"
   799       by (auto intro: inv_unique_comp o_bij)
   800     then have eq: "T m \<delta> ` S = T (1 / m) ((-1/m) *\<^sub>R \<delta>) -` S \<inter> space lebesgue"
   801       using bij_vimage_eq_inv_image[OF \<open>bij ?T'\<close>, of S] by auto
   802 
   803     have trans_eq_T: "(\<lambda>x. \<delta> + (\<Sum>j\<in>Basis. (m * (x \<bullet> j)) *\<^sub>R j)) = T m \<delta>" for m \<delta>
   804       unfolding T_def[abs_def] scaleR_scaleR[symmetric] scaleR_sum_right[symmetric]
   805       by (auto simp add: euclidean_representation ac_simps)
   806 
   807     have T[measurable]: "T r d \<in> lebesgue \<rightarrow>\<^sub>M lebesgue" for r d
   808       using lebesgue_affine_measurable[of "\<lambda>_. r" d]
   809       by (cases "r = 0") (auto simp: trans_eq_T T_def[abs_def])
   810 
   811     show ?thesis
   812     proof cases
   813       assume "S \<in> sets lebesgue" with \<open>m \<noteq> 0\<close> show ?thesis
   814         unfolding eq
   815         apply (subst lebesgue_affine_euclidean[of "\<lambda>_. m" \<delta>])
   816         apply (simp_all add: emeasure_density trans_eq_T nn_integral_cmult emeasure_distr
   817                         del: space_completion emeasure_completion)
   818         apply (simp add: vimage_comp s_comp_s prod_constant)
   819         done
   820     next
   821       assume "S \<notin> sets lebesgue"
   822       moreover have "?T ` S \<notin> sets lebesgue"
   823       proof
   824         assume "?T ` S \<in> sets lebesgue"
   825         then have "?T -` (?T ` S) \<inter> space lebesgue \<in> sets lebesgue"
   826           by (rule measurable_sets[OF T])
   827         also have "?T -` (?T ` S) \<inter> space lebesgue = S"
   828           by (simp add: vimage_comp s_comp_s eq)
   829         finally show False using \<open>S \<notin> sets lebesgue\<close> by auto
   830       qed
   831       ultimately show ?thesis
   832         by (simp add: emeasure_notin_sets)
   833     qed
   834   qed
   835   show ?m
   836     unfolding measure_def \<open>?e\<close> by (simp add: enn2real_mult prod_nonneg)
   837 qed
   838 
   839 lemma lebesgue_real_scale:
   840   assumes "c \<noteq> 0"
   841   shows   "lebesgue = density (distr lebesgue lebesgue (\<lambda>x. c * x)) (\<lambda>x. ennreal \<bar>c\<bar>)"
   842   using assms by (subst lebesgue_affine_euclidean[of "\<lambda>_. c" 0]) simp_all
   843 
   844 lemma divideR_right:
   845   fixes x y :: "'a::real_normed_vector"
   846   shows "r \<noteq> 0 \<Longrightarrow> y = x /\<^sub>R r \<longleftrightarrow> r *\<^sub>R y = x"
   847   using scaleR_cancel_left[of r y "x /\<^sub>R r"] by simp
   848 
   849 lemma lborel_has_bochner_integral_real_affine_iff:
   850   fixes x :: "'a :: {banach, second_countable_topology}"
   851   shows "c \<noteq> 0 \<Longrightarrow>
   852     has_bochner_integral lborel f x \<longleftrightarrow>
   853     has_bochner_integral lborel (\<lambda>x. f (t + c * x)) (x /\<^sub>R \<bar>c\<bar>)"
   854   unfolding has_bochner_integral_iff lborel_integrable_real_affine_iff
   855   by (simp_all add: lborel_integral_real_affine[symmetric] divideR_right cong: conj_cong)
   856 
   857 lemma lborel_distr_uminus: "distr lborel borel uminus = (lborel :: real measure)"
   858   by (subst lborel_real_affine[of "-1" 0])
   859      (auto simp: density_1 one_ennreal_def[symmetric])
   860 
   861 lemma lborel_distr_mult:
   862   assumes "(c::real) \<noteq> 0"
   863   shows "distr lborel borel (( * ) c) = density lborel (\<lambda>_. inverse \<bar>c\<bar>)"
   864 proof-
   865   have "distr lborel borel (( * ) c) = distr lborel lborel (( * ) c)" by (simp cong: distr_cong)
   866   also from assms have "... = density lborel (\<lambda>_. inverse \<bar>c\<bar>)"
   867     by (subst lborel_real_affine[of "inverse c" 0]) (auto simp: o_def distr_density_distr)
   868   finally show ?thesis .
   869 qed
   870 
   871 lemma lborel_distr_mult':
   872   assumes "(c::real) \<noteq> 0"
   873   shows "lborel = density (distr lborel borel (( * ) c)) (\<lambda>_. \<bar>c\<bar>)"
   874 proof-
   875   have "lborel = density lborel (\<lambda>_. 1)" by (rule density_1[symmetric])
   876   also from assms have "(\<lambda>_. 1 :: ennreal) = (\<lambda>_. inverse \<bar>c\<bar> * \<bar>c\<bar>)" by (intro ext) simp
   877   also have "density lborel ... = density (density lborel (\<lambda>_. inverse \<bar>c\<bar>)) (\<lambda>_. \<bar>c\<bar>)"
   878     by (subst density_density_eq) (auto simp: ennreal_mult)
   879   also from assms have "density lborel (\<lambda>_. inverse \<bar>c\<bar>) = distr lborel borel (( * ) c)"
   880     by (rule lborel_distr_mult[symmetric])
   881   finally show ?thesis .
   882 qed
   883 
   884 lemma lborel_distr_plus: "distr lborel borel ((+) c) = (lborel :: real measure)"
   885   by (subst lborel_real_affine[of 1 c]) (auto simp: density_1 one_ennreal_def[symmetric])
   886 
   887 interpretation lborel: sigma_finite_measure lborel
   888   by (rule sigma_finite_lborel)
   889 
   890 interpretation lborel_pair: pair_sigma_finite lborel lborel ..
   891 
   892 lemma lborel_prod:
   893   "lborel \<Otimes>\<^sub>M lborel = (lborel :: ('a::euclidean_space \<times> 'b::euclidean_space) measure)"
   894 proof (rule lborel_eqI[symmetric], clarify)
   895   fix la ua :: 'a and lb ub :: 'b
   896   assume lu: "\<And>a b. (a, b) \<in> Basis \<Longrightarrow> (la, lb) \<bullet> (a, b) \<le> (ua, ub) \<bullet> (a, b)"
   897   have [simp]:
   898     "\<And>b. b \<in> Basis \<Longrightarrow> la \<bullet> b \<le> ua \<bullet> b"
   899     "\<And>b. b \<in> Basis \<Longrightarrow> lb \<bullet> b \<le> ub \<bullet> b"
   900     "inj_on (\<lambda>u. (u, 0)) Basis" "inj_on (\<lambda>u. (0, u)) Basis"
   901     "(\<lambda>u. (u, 0)) ` Basis \<inter> (\<lambda>u. (0, u)) ` Basis = {}"
   902     "box (la, lb) (ua, ub) = box la ua \<times> box lb ub"
   903     using lu[of _ 0] lu[of 0] by (auto intro!: inj_onI simp add: Basis_prod_def ball_Un box_def)
   904   show "emeasure (lborel \<Otimes>\<^sub>M lborel) (box (la, lb) (ua, ub)) =
   905       ennreal (prod ((\<bullet>) ((ua, ub) - (la, lb))) Basis)"
   906     by (simp add: lborel.emeasure_pair_measure_Times Basis_prod_def prod.union_disjoint
   907                   prod.reindex ennreal_mult inner_diff_left prod_nonneg)
   908 qed (simp add: borel_prod[symmetric])
   909 
   910 (* FIXME: conversion in measurable prover *)
   911 lemma lborelD_Collect[measurable (raw)]: "{x\<in>space borel. P x} \<in> sets borel \<Longrightarrow> {x\<in>space lborel. P x} \<in> sets lborel" by simp
   912 lemma lborelD[measurable (raw)]: "A \<in> sets borel \<Longrightarrow> A \<in> sets lborel" by simp
   913 
   914 lemma emeasure_bounded_finite:
   915   assumes "bounded A" shows "emeasure lborel A < \<infinity>"
   916 proof -
   917   from bounded_subset_cbox[OF \<open>bounded A\<close>] obtain a b where "A \<subseteq> cbox a b"
   918     by auto
   919   then have "emeasure lborel A \<le> emeasure lborel (cbox a b)"
   920     by (intro emeasure_mono) auto
   921   then show ?thesis
   922     by (auto simp: emeasure_lborel_cbox_eq prod_nonneg less_top[symmetric] top_unique split: if_split_asm)
   923 qed
   924 
   925 lemma emeasure_compact_finite: "compact A \<Longrightarrow> emeasure lborel A < \<infinity>"
   926   using emeasure_bounded_finite[of A] by (auto intro: compact_imp_bounded)
   927 
   928 lemma borel_integrable_compact:
   929   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::{banach, second_countable_topology}"
   930   assumes "compact S" "continuous_on S f"
   931   shows "integrable lborel (\<lambda>x. indicator S x *\<^sub>R f x)"
   932 proof cases
   933   assume "S \<noteq> {}"
   934   have "continuous_on S (\<lambda>x. norm (f x))"
   935     using assms by (intro continuous_intros)
   936   from continuous_attains_sup[OF \<open>compact S\<close> \<open>S \<noteq> {}\<close> this]
   937   obtain M where M: "\<And>x. x \<in> S \<Longrightarrow> norm (f x) \<le> M"
   938     by auto
   939   show ?thesis
   940   proof (rule integrable_bound)
   941     show "integrable lborel (\<lambda>x. indicator S x * M)"
   942       using assms by (auto intro!: emeasure_compact_finite borel_compact integrable_mult_left)
   943     show "(\<lambda>x. indicator S x *\<^sub>R f x) \<in> borel_measurable lborel"
   944       using assms by (auto intro!: borel_measurable_continuous_on_indicator borel_compact)
   945     show "AE x in lborel. norm (indicator S x *\<^sub>R f x) \<le> norm (indicator S x * M)"
   946       by (auto split: split_indicator simp: abs_real_def dest!: M)
   947   qed
   948 qed simp
   949 
   950 lemma borel_integrable_atLeastAtMost:
   951   fixes f :: "real \<Rightarrow> real"
   952   assumes f: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> isCont f x"
   953   shows "integrable lborel (\<lambda>x. f x * indicator {a .. b} x)" (is "integrable _ ?f")
   954 proof -
   955   have "integrable lborel (\<lambda>x. indicator {a .. b} x *\<^sub>R f x)"
   956   proof (rule borel_integrable_compact)
   957     from f show "continuous_on {a..b} f"
   958       by (auto intro: continuous_at_imp_continuous_on)
   959   qed simp
   960   then show ?thesis
   961     by (auto simp: mult.commute)
   962 qed
   963 
   964 subsection\<open>Lebesgue measurable sets\<close>
   965 
   966 abbreviation lmeasurable :: "'a::euclidean_space set set"
   967 where
   968   "lmeasurable \<equiv> fmeasurable lebesgue"
   969 
   970 lemma not_measurable_UNIV [simp]: "UNIV \<notin> lmeasurable"
   971   by (simp add: fmeasurable_def)
   972 
   973 lemma lmeasurable_iff_integrable:
   974   "S \<in> lmeasurable \<longleftrightarrow> integrable lebesgue (indicator S :: 'a::euclidean_space \<Rightarrow> real)"
   975   by (auto simp: fmeasurable_def integrable_iff_bounded borel_measurable_indicator_iff ennreal_indicator)
   976 
   977 lemma lmeasurable_cbox [iff]: "cbox a b \<in> lmeasurable"
   978   and lmeasurable_box [iff]: "box a b \<in> lmeasurable"
   979   by (auto simp: fmeasurable_def emeasure_lborel_box_eq emeasure_lborel_cbox_eq)
   980 
   981 lemma fmeasurable_compact: "compact S \<Longrightarrow> S \<in> fmeasurable lborel"
   982   using emeasure_compact_finite[of S] by (intro fmeasurableI) (auto simp: borel_compact)
   983 
   984 lemma lmeasurable_compact: "compact S \<Longrightarrow> S \<in> lmeasurable"
   985   using fmeasurable_compact by (force simp: fmeasurable_def)
   986 
   987 lemma measure_frontier:
   988    "bounded S \<Longrightarrow> measure lebesgue (frontier S) = measure lebesgue (closure S) - measure lebesgue (interior S)"
   989   using closure_subset interior_subset
   990   by (auto simp: frontier_def fmeasurable_compact intro!: measurable_measure_Diff)
   991 
   992 lemma lmeasurable_closure:
   993    "bounded S \<Longrightarrow> closure S \<in> lmeasurable"
   994   by (simp add: lmeasurable_compact)
   995 
   996 lemma lmeasurable_frontier:
   997    "bounded S \<Longrightarrow> frontier S \<in> lmeasurable"
   998   by (simp add: compact_frontier_bounded lmeasurable_compact)
   999 
  1000 lemma lmeasurable_open: "bounded S \<Longrightarrow> open S \<Longrightarrow> S \<in> lmeasurable"
  1001   using emeasure_bounded_finite[of S] by (intro fmeasurableI) (auto simp: borel_open)
  1002 
  1003 lemma lmeasurable_ball [simp]: "ball a r \<in> lmeasurable"
  1004   by (simp add: lmeasurable_open)
  1005 
  1006 lemma lmeasurable_cball [simp]: "cball a r \<in> lmeasurable"
  1007   by (simp add: lmeasurable_compact)
  1008 
  1009 lemma lmeasurable_interior: "bounded S \<Longrightarrow> interior S \<in> lmeasurable"
  1010   by (simp add: bounded_interior lmeasurable_open)
  1011 
  1012 lemma null_sets_cbox_Diff_box: "cbox a b - box a b \<in> null_sets lborel"
  1013 proof -
  1014   have "emeasure lborel (cbox a b - box a b) = 0"
  1015     by (subst emeasure_Diff) (auto simp: emeasure_lborel_cbox_eq emeasure_lborel_box_eq box_subset_cbox)
  1016   then have "cbox a b - box a b \<in> null_sets lborel"
  1017     by (auto simp: null_sets_def)
  1018   then show ?thesis
  1019     by (auto dest!: AE_not_in)
  1020 qed
  1021 
  1022 lemma bounded_set_imp_lmeasurable:
  1023   assumes "bounded S" "S \<in> sets lebesgue" shows "S \<in> lmeasurable"
  1024   by (metis assms bounded_Un emeasure_bounded_finite emeasure_completion fmeasurableI main_part_null_part_Un)
  1025 
  1026 
  1027 subsection\<open>Translation preserves Lebesgue measure\<close>
  1028 
  1029 lemma sigma_sets_image:
  1030   assumes S: "S \<in> sigma_sets \<Omega> M" and "M \<subseteq> Pow \<Omega>" "f ` \<Omega> = \<Omega>" "inj_on f \<Omega>"
  1031     and M: "\<And>y. y \<in> M \<Longrightarrow> f ` y \<in> M"
  1032   shows "(f ` S) \<in> sigma_sets \<Omega> M"
  1033   using S
  1034 proof (induct S rule: sigma_sets.induct)
  1035   case (Basic a) then show ?case
  1036     by (simp add: M)
  1037 next
  1038   case Empty then show ?case
  1039     by (simp add: sigma_sets.Empty)
  1040 next
  1041   case (Compl a)
  1042   then have "\<Omega> - a \<subseteq> \<Omega>" "a \<subseteq> \<Omega>"
  1043     by (auto simp: sigma_sets_into_sp [OF \<open>M \<subseteq> Pow \<Omega>\<close>])
  1044   then show ?case
  1045     by (auto simp: inj_on_image_set_diff [OF \<open>inj_on f \<Omega>\<close>] assms intro: Compl sigma_sets.Compl)
  1046 next
  1047   case (Union a) then show ?case
  1048     by (metis image_UN sigma_sets.simps)
  1049 qed
  1050 
  1051 lemma null_sets_translation:
  1052   assumes "N \<in> null_sets lborel" shows "{x. x - a \<in> N} \<in> null_sets lborel"
  1053 proof -
  1054   have [simp]: "(\<lambda>x. x + a) ` N = {x. x - a \<in> N}"
  1055     by force
  1056   show ?thesis
  1057     using assms emeasure_lebesgue_affine [of 1 a N] by (auto simp: null_sets_def)
  1058 qed
  1059 
  1060 lemma lebesgue_sets_translation:
  1061   fixes f :: "'a \<Rightarrow> 'a::euclidean_space"
  1062   assumes S: "S \<in> sets lebesgue"
  1063   shows "((\<lambda>x. a + x) ` S) \<in> sets lebesgue"
  1064 proof -
  1065   have im_eq: "(+) a ` A = {x. x - a \<in> A}" for A
  1066     by force
  1067   have "((\<lambda>x. a + x) ` S) = ((\<lambda>x. -a + x) -` S) \<inter> (space lebesgue)"
  1068     using image_iff by fastforce
  1069   also have "\<dots> \<in> sets lebesgue"
  1070   proof (rule measurable_sets [OF measurableI assms])
  1071     fix A :: "'b set"
  1072     assume A: "A \<in> sets lebesgue"
  1073     have vim_eq: "(\<lambda>x. x - a) -` A = (+) a ` A" for A
  1074       by force
  1075     have "\<exists>s n N'. (+) a ` (S \<union> N) = s \<union> n \<and> s \<in> sets borel \<and> N' \<in> null_sets lborel \<and> n \<subseteq> N'"
  1076       if "S \<in> sets borel" and "N' \<in> null_sets lborel" and "N \<subseteq> N'" for S N N'
  1077     proof (intro exI conjI)
  1078       show "(+) a ` (S \<union> N) = (\<lambda>x. a + x) ` S \<union> (\<lambda>x. a + x) ` N"
  1079         by auto
  1080       show "(\<lambda>x. a + x) ` N' \<in> null_sets lborel"
  1081         using that by (auto simp: null_sets_translation im_eq)
  1082     qed (use that im_eq in auto)
  1083     with A have "(\<lambda>x. x - a) -` A \<in> sets lebesgue"
  1084       by (force simp: vim_eq completion_def intro!: sigma_sets_image)
  1085     then show "(+) (- a) -` A \<inter> space lebesgue \<in> sets lebesgue"
  1086       by (auto simp: vimage_def im_eq)
  1087   qed auto
  1088   finally show ?thesis .
  1089 qed
  1090 
  1091 lemma measurable_translation:
  1092    "S \<in> lmeasurable \<Longrightarrow> ((\<lambda>x. a + x) ` S) \<in> lmeasurable"
  1093   unfolding fmeasurable_def
  1094 apply (auto intro: lebesgue_sets_translation)
  1095   using  emeasure_lebesgue_affine [of 1 a S]
  1096   by (auto simp: add.commute [of _ a])
  1097 
  1098 lemma measure_translation:
  1099    "measure lebesgue ((\<lambda>x. a + x) ` S) = measure lebesgue S"
  1100   using measure_lebesgue_affine [of 1 a S]
  1101   by (auto simp: add.commute [of _ a])
  1102 
  1103 subsection \<open>A nice lemma for negligibility proofs\<close>
  1104 
  1105 lemma summable_iff_suminf_neq_top: "(\<And>n. f n \<ge> 0) \<Longrightarrow> \<not> summable f \<Longrightarrow> (\<Sum>i. ennreal (f i)) = top"
  1106   by (metis summable_suminf_not_top)
  1107 
  1108 proposition starlike_negligible_bounded_gmeasurable:
  1109   fixes S :: "'a :: euclidean_space set"
  1110   assumes S: "S \<in> sets lebesgue" and "bounded S"
  1111       and eq1: "\<And>c x. \<lbrakk>(c *\<^sub>R x) \<in> S; 0 \<le> c; x \<in> S\<rbrakk> \<Longrightarrow> c = 1"
  1112     shows "S \<in> null_sets lebesgue"
  1113 proof -
  1114   obtain M where "0 < M" "S \<subseteq> ball 0 M"
  1115     using \<open>bounded S\<close> by (auto dest: bounded_subset_ballD)
  1116 
  1117   let ?f = "\<lambda>n. root DIM('a) (Suc n)"
  1118 
  1119   have vimage_eq_image: "( *\<^sub>R) (?f n) -` S = ( *\<^sub>R) (1 / ?f n) ` S" for n
  1120     apply safe
  1121     subgoal for x by (rule image_eqI[of _ _ "?f n *\<^sub>R x"]) auto
  1122     subgoal by auto
  1123     done
  1124 
  1125   have eq: "(1 / ?f n) ^ DIM('a) = 1 / Suc n" for n
  1126     by (simp add: field_simps)
  1127 
  1128   { fix n x assume x: "root DIM('a) (1 + real n) *\<^sub>R x \<in> S"
  1129     have "1 * norm x \<le> root DIM('a) (1 + real n) * norm x"
  1130       by (rule mult_mono) auto
  1131     also have "\<dots> < M"
  1132       using x \<open>S \<subseteq> ball 0 M\<close> by auto
  1133     finally have "norm x < M" by simp }
  1134   note less_M = this
  1135 
  1136   have "(\<Sum>n. ennreal (1 / Suc n)) = top"
  1137     using not_summable_harmonic[where 'a=real] summable_Suc_iff[where f="\<lambda>n. 1 / (real n)"]
  1138     by (intro summable_iff_suminf_neq_top) (auto simp add: inverse_eq_divide)
  1139   then have "top * emeasure lebesgue S = (\<Sum>n. (1 / ?f n)^DIM('a) * emeasure lebesgue S)"
  1140     unfolding ennreal_suminf_multc eq by simp
  1141   also have "\<dots> = (\<Sum>n. emeasure lebesgue (( *\<^sub>R) (?f n) -` S))"
  1142     unfolding vimage_eq_image using emeasure_lebesgue_affine[of "1 / ?f n" 0 S for n] by simp
  1143   also have "\<dots> = emeasure lebesgue (\<Union>n. ( *\<^sub>R) (?f n) -` S)"
  1144   proof (intro suminf_emeasure)
  1145     show "disjoint_family (\<lambda>n. ( *\<^sub>R) (?f n) -` S)"
  1146       unfolding disjoint_family_on_def
  1147     proof safe
  1148       fix m n :: nat and x assume "m \<noteq> n" "?f m *\<^sub>R x \<in> S" "?f n *\<^sub>R x \<in> S"
  1149       with eq1[of "?f m / ?f n" "?f n *\<^sub>R x"] show "x \<in> {}"
  1150         by auto
  1151     qed
  1152     have "( *\<^sub>R) (?f i) -` S \<in> sets lebesgue" for i
  1153       using measurable_sets[OF lebesgue_measurable_scaling[of "?f i"] S] by auto
  1154     then show "range (\<lambda>i. ( *\<^sub>R) (?f i) -` S) \<subseteq> sets lebesgue"
  1155       by auto
  1156   qed
  1157   also have "\<dots> \<le> emeasure lebesgue (ball 0 M :: 'a set)"
  1158     using less_M by (intro emeasure_mono) auto
  1159   also have "\<dots> < top"
  1160     using lmeasurable_ball by (auto simp: fmeasurable_def)
  1161   finally have "emeasure lebesgue S = 0"
  1162     by (simp add: ennreal_top_mult split: if_split_asm)
  1163   then show "S \<in> null_sets lebesgue"
  1164     unfolding null_sets_def using \<open>S \<in> sets lebesgue\<close> by auto
  1165 qed
  1166 
  1167 corollary starlike_negligible_compact:
  1168   "compact S \<Longrightarrow> (\<And>c x. \<lbrakk>(c *\<^sub>R x) \<in> S; 0 \<le> c; x \<in> S\<rbrakk> \<Longrightarrow> c = 1) \<Longrightarrow> S \<in> null_sets lebesgue"
  1169   using starlike_negligible_bounded_gmeasurable[of S] by (auto simp: compact_eq_bounded_closed)
  1170 
  1171 proposition outer_regular_lborel_le:
  1172   assumes B[measurable]: "B \<in> sets borel" and "0 < (e::real)"
  1173   obtains U where "open U" "B \<subseteq> U" and "emeasure lborel (U - B) \<le> e"
  1174 proof -
  1175   let ?\<mu> = "emeasure lborel"
  1176   let ?B = "\<lambda>n::nat. ball 0 n :: 'a set"
  1177   let ?e = "\<lambda>n. e*((1/2)^Suc n)"
  1178   have "\<forall>n. \<exists>U. open U \<and> ?B n \<inter> B \<subseteq> U \<and> ?\<mu> (U - B) < ?e n"
  1179   proof
  1180     fix n :: nat
  1181     let ?A = "density lborel (indicator (?B n))"
  1182     have emeasure_A: "X \<in> sets borel \<Longrightarrow> emeasure ?A X = ?\<mu> (?B n \<inter> X)" for X
  1183       by (auto simp: emeasure_density borel_measurable_indicator indicator_inter_arith[symmetric])
  1184 
  1185     have finite_A: "emeasure ?A (space ?A) \<noteq> \<infinity>"
  1186       using emeasure_bounded_finite[of "?B n"] by (auto simp: emeasure_A)
  1187     interpret A: finite_measure ?A
  1188       by rule fact
  1189     have "emeasure ?A B + ?e n > (INF U:{U. B \<subseteq> U \<and> open U}. emeasure ?A U)"
  1190       using \<open>0<e\<close> by (auto simp: outer_regular[OF _ finite_A B, symmetric])
  1191     then obtain U where U: "B \<subseteq> U" "open U" and muU: "?\<mu> (?B n \<inter> B) + ?e n > ?\<mu> (?B n \<inter> U)"
  1192       unfolding INF_less_iff by (auto simp: emeasure_A)
  1193     moreover
  1194     { have "?\<mu> ((?B n \<inter> U) - B) = ?\<mu> ((?B n \<inter> U) - (?B n \<inter> B))"
  1195         using U by (intro arg_cong[where f="?\<mu>"]) auto
  1196       also have "\<dots> = ?\<mu> (?B n \<inter> U) - ?\<mu> (?B n \<inter> B)"
  1197         using U A.emeasure_finite[of B]
  1198         by (intro emeasure_Diff) (auto simp del: A.emeasure_finite simp: emeasure_A)
  1199       also have "\<dots> < ?e n"
  1200         using U muU A.emeasure_finite[of B]
  1201         by (subst minus_less_iff_ennreal)
  1202           (auto simp del: A.emeasure_finite simp: emeasure_A less_top ac_simps intro!: emeasure_mono)
  1203       finally have "?\<mu> ((?B n \<inter> U) - B) < ?e n" . }
  1204     ultimately show "\<exists>U. open U \<and> ?B n \<inter> B \<subseteq> U \<and> ?\<mu> (U - B) < ?e n"
  1205       by (intro exI[of _ "?B n \<inter> U"]) auto
  1206   qed
  1207   then obtain U
  1208     where U: "\<And>n. open (U n)" "\<And>n. ?B n \<inter> B \<subseteq> U n" "\<And>n. ?\<mu> (U n - B) < ?e n"
  1209     by metis
  1210   show ?thesis
  1211   proof
  1212     { fix x assume "x \<in> B"
  1213       moreover
  1214       obtain n where "norm x < real n"
  1215         using reals_Archimedean2 by blast
  1216       ultimately have "x \<in> (\<Union>n. U n)"
  1217         using U(2)[of n] by auto }
  1218     note * = this
  1219     then show "open (\<Union>n. U n)" "B \<subseteq> (\<Union>n. U n)"
  1220       using U by auto
  1221     have "?\<mu> (\<Union>n. U n - B) \<le> (\<Sum>n. ?\<mu> (U n - B))"
  1222       using U(1) by (intro emeasure_subadditive_countably) auto
  1223     also have "\<dots> \<le> (\<Sum>n. ennreal (?e n))"
  1224       using U(3) by (intro suminf_le) (auto intro: less_imp_le)
  1225     also have "\<dots> = ennreal (e * 1)"
  1226       using \<open>0<e\<close> by (intro suminf_ennreal_eq sums_mult power_half_series) auto
  1227     finally show "emeasure lborel ((\<Union>n. U n) - B) \<le> ennreal e"
  1228       by simp
  1229   qed
  1230 qed
  1231 
  1232 lemma outer_regular_lborel:
  1233   assumes B: "B \<in> sets borel" and "0 < (e::real)"
  1234   obtains U where "open U" "B \<subseteq> U" "emeasure lborel (U - B) < e"
  1235 proof -
  1236   obtain U where U: "open U" "B \<subseteq> U" and "emeasure lborel (U-B) \<le> e/2"
  1237     using outer_regular_lborel_le [OF B, of "e/2"] \<open>e > 0\<close>
  1238     by force
  1239   moreover have "ennreal (e/2) < ennreal e"
  1240     using \<open>e > 0\<close> by (simp add: ennreal_lessI)
  1241   ultimately have "emeasure lborel (U-B) < e"
  1242     by auto
  1243   with U show ?thesis
  1244     using that by auto
  1245 qed
  1246 
  1247 lemma completion_upper:
  1248   assumes A: "A \<in> sets (completion M)"
  1249   obtains A' where "A \<subseteq> A'" "A' \<in> sets M" "A' - A \<in> null_sets (completion M)"
  1250                    "emeasure (completion M) A = emeasure M A'"
  1251 proof -
  1252   from AE_notin_null_part[OF A] obtain N where N: "N \<in> null_sets M" "null_part M A \<subseteq> N"
  1253     unfolding eventually_ae_filter using null_part_null_sets[OF A, THEN null_setsD2, THEN sets.sets_into_space] by auto
  1254   let ?A' = "main_part M A \<union> N"
  1255   show ?thesis
  1256   proof
  1257     show "A \<subseteq> ?A'"
  1258       using \<open>null_part M A \<subseteq> N\<close> by (subst main_part_null_part_Un[symmetric, OF A]) auto
  1259     have "main_part M A \<subseteq> A"
  1260       using assms main_part_null_part_Un by auto
  1261     then have "?A' - A \<subseteq> N"
  1262       by blast
  1263     with N show "?A' - A \<in> null_sets (completion M)"
  1264       by (blast intro: null_sets_completionI completion.complete_measure_axioms complete_measure.complete2)
  1265     show "emeasure (completion M) A = emeasure M (main_part M A \<union> N)"
  1266       using A \<open>N \<in> null_sets M\<close> by (simp add: emeasure_Un_null_set)
  1267   qed (use A N in auto)
  1268 qed
  1269 
  1270 lemma lmeasurable_outer_open:
  1271   assumes S: "S \<in> sets lebesgue" and "e > 0"
  1272   obtains T where "open T" "S \<subseteq> T" "(T - S) \<in> lmeasurable" "measure lebesgue (T - S) < e"
  1273 proof -
  1274   obtain S' where S': "S \<subseteq> S'" "S' \<in> sets borel"
  1275               and null: "S' - S \<in> null_sets lebesgue"
  1276               and em: "emeasure lebesgue S = emeasure lborel S'"
  1277     using completion_upper[of S lborel] S by auto
  1278   then have f_S': "S' \<in> sets borel"
  1279     using S by (auto simp: fmeasurable_def)
  1280   with outer_regular_lborel[OF _ \<open>0<e\<close>]
  1281   obtain U where U: "open U" "S' \<subseteq> U" "emeasure lborel (U - S') < e"
  1282     by blast
  1283   show thesis
  1284   proof
  1285     show "open U" "S \<subseteq> U"
  1286       using f_S' U S' by auto
  1287   have "(U - S) = (U - S') \<union> (S' - S)"
  1288     using S' U by auto
  1289   then have eq: "emeasure lebesgue (U - S) = emeasure lborel (U - S')"
  1290     using null  by (simp add: U(1) emeasure_Un_null_set f_S' sets.Diff)
  1291   have "(U - S) \<in> sets lebesgue"
  1292     by (simp add: S U(1) sets.Diff)
  1293   then show "(U - S) \<in> lmeasurable"
  1294     unfolding fmeasurable_def using U(3) eq less_le_trans by fastforce
  1295   with eq U show "measure lebesgue (U - S) < e"
  1296     by (metis \<open>U - S \<in> lmeasurable\<close> emeasure_eq_measure2 ennreal_leI not_le)
  1297   qed
  1298 qed
  1299 
  1300 lemma sets_lebesgue_inner_closed:
  1301   assumes "S \<in> sets lebesgue" "e > 0"
  1302   obtains T where "closed T" "T \<subseteq> S" "S-T \<in> lmeasurable" "measure lebesgue (S - T) < e"
  1303 proof -
  1304   have "-S \<in> sets lebesgue"
  1305     using assms by (simp add: Compl_in_sets_lebesgue)
  1306   then obtain T where "open T" "-S \<subseteq> T"
  1307           and T: "(T - -S) \<in> lmeasurable" "measure lebesgue (T - -S) < e"
  1308     using lmeasurable_outer_open assms  by blast
  1309   show thesis
  1310   proof
  1311     show "closed (-T)"
  1312       using \<open>open T\<close> by blast
  1313     show "-T \<subseteq> S"
  1314       using \<open>- S \<subseteq> T\<close> by auto
  1315     show "S - ( -T) \<in> lmeasurable" "measure lebesgue (S - (- T)) < e"
  1316       using T by (auto simp: Int_commute)
  1317   qed
  1318 qed
  1319 
  1320 lemma lebesgue_openin:
  1321    "\<lbrakk>openin (subtopology euclidean S) T; S \<in> sets lebesgue\<rbrakk> \<Longrightarrow> T \<in> sets lebesgue"
  1322   by (metis borel_open openin_open sets.Int sets_completionI_sets sets_lborel)
  1323 
  1324 lemma lebesgue_closedin:
  1325    "\<lbrakk>closedin (subtopology euclidean S) T; S \<in> sets lebesgue\<rbrakk> \<Longrightarrow> T \<in> sets lebesgue"
  1326   by (metis borel_closed closedin_closed sets.Int sets_completionI_sets sets_lborel)
  1327 
  1328 end