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