src/HOL/Analysis/Vitali_Covering_Theorem.thy
changeset 67996 6a9d1b31a7c5
child 67998 73a5a33486ee
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Analysis/Vitali_Covering_Theorem.thy	Tue Apr 17 12:09:23 2018 +0100
     1.3 @@ -0,0 +1,652 @@
     1.4 +theory Vitali_Covering_Theorem
     1.5 +  imports Ball_Volume "HOL-Library.Permutations"
     1.6 +
     1.7 +begin
     1.8 +
     1.9 +lemma stretch_Galois:
    1.10 +  fixes x :: "real^'n"
    1.11 +  shows "(\<And>k. m k \<noteq> 0) \<Longrightarrow> ((y = (\<chi> k. m k * x$k)) \<longleftrightarrow> (\<chi> k. y$k / m k) = x)"
    1.12 +  by auto
    1.13 +
    1.14 +lemma lambda_swap_Galois:
    1.15 +   "(x = (\<chi> i. y $ Fun.swap m n id i) \<longleftrightarrow> (\<chi> i. x $ Fun.swap m n id i) = y)"
    1.16 +  by (auto; simp add: pointfree_idE vec_eq_iff)
    1.17 +
    1.18 +lemma lambda_add_Galois:
    1.19 +  fixes x :: "real^'n"
    1.20 +  shows "m \<noteq> n \<Longrightarrow> (x = (\<chi> i. if i = m then y$m + y$n else y$i) \<longleftrightarrow> (\<chi> i. if i = m then x$m - x$n else x$i) = y)"
    1.21 +  by (safe; simp add: vec_eq_iff)
    1.22 +
    1.23 +
    1.24 +lemma Vitali_covering_lemma_cballs_balls:
    1.25 +  fixes a :: "'a \<Rightarrow> 'b::euclidean_space"
    1.26 +  assumes "\<And>i. i \<in> K \<Longrightarrow> 0 < r i \<and> r i \<le> B"
    1.27 +  obtains C where "countable C" "C \<subseteq> K"
    1.28 +     "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) C"
    1.29 +     "\<And>i. i \<in> K \<Longrightarrow> \<exists>j. j \<in> C \<and>
    1.30 +                        \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j)) \<and>
    1.31 +                        cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
    1.32 +proof (cases "K = {}")
    1.33 +  case True
    1.34 +  with that show ?thesis
    1.35 +    by auto
    1.36 +next
    1.37 +  case False
    1.38 +  then have "B > 0"
    1.39 +    using assms less_le_trans by auto
    1.40 +  have rgt0[simp]: "\<And>i. i \<in> K \<Longrightarrow> 0 < r i"
    1.41 +    using assms by auto
    1.42 +  let ?djnt = "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j)))"
    1.43 +  have "\<exists>C. \<forall>n. (C n \<subseteq> K \<and>
    1.44 +             (\<forall>i \<in> C n. B/2 ^ n \<le> r i) \<and> ?djnt (C n) \<and>
    1.45 +             (\<forall>i \<in> K. B/2 ^ n < r i
    1.46 +                 \<longrightarrow> (\<exists>j. j \<in> C n \<and>
    1.47 +                         \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j)) \<and>
    1.48 +                         cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)))) \<and> (C n \<subseteq> C(Suc n))"
    1.49 +  proof (rule dependent_nat_choice, safe)
    1.50 +    fix C n
    1.51 +    define D where "D \<equiv> {i \<in> K. B/2 ^ Suc n < r i \<and> (\<forall>j\<in>C. disjnt (cball(a i)(r i)) (cball (a j) (r j)))}"
    1.52 +    let ?cover_ar = "\<lambda>i j. \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j)) \<and>
    1.53 +                             cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
    1.54 +    assume "C \<subseteq> K"
    1.55 +      and Ble: "\<forall>i\<in>C. B/2 ^ n \<le> r i"
    1.56 +      and djntC: "?djnt C"
    1.57 +      and cov_n: "\<forall>i\<in>K. B/2 ^ n < r i \<longrightarrow> (\<exists>j. j \<in> C \<and> ?cover_ar i j)"
    1.58 +    have *: "\<forall>C\<in>chains {C. C \<subseteq> D \<and> ?djnt C}. \<Union>C \<in> {C. C \<subseteq> D \<and> ?djnt C}"
    1.59 +    proof (clarsimp simp: chains_def)
    1.60 +      fix C
    1.61 +      assume C: "C \<subseteq> {C. C \<subseteq> D \<and> ?djnt C}" and "chain\<^sub>\<subseteq> C"
    1.62 +      show "\<Union>C \<subseteq> D \<and> ?djnt (\<Union>C)"
    1.63 +        unfolding pairwise_def
    1.64 +      proof (intro ballI conjI impI)
    1.65 +        show "\<Union>C \<subseteq> D"
    1.66 +          using C by blast
    1.67 +      next
    1.68 +        fix x y
    1.69 +        assume "x \<in> \<Union>C" and "y \<in> \<Union>C" and "x \<noteq> y"
    1.70 +        then obtain X Y where XY: "x \<in> X" "X \<in> C" "y \<in> Y" "Y \<in> C"
    1.71 +          by blast
    1.72 +        then consider "X \<subseteq> Y" | "Y \<subseteq> X"
    1.73 +          by (meson \<open>chain\<^sub>\<subseteq> C\<close> chain_subset_def)
    1.74 +        then show "disjnt (cball (a x) (r x)) (cball (a y) (r y))"
    1.75 +        proof cases
    1.76 +          case 1
    1.77 +          with C XY \<open>x \<noteq> y\<close> show ?thesis
    1.78 +            unfolding pairwise_def by blast
    1.79 +        next
    1.80 +          case 2
    1.81 +          with C XY \<open>x \<noteq> y\<close> show ?thesis
    1.82 +            unfolding pairwise_def by blast
    1.83 +        qed
    1.84 +      qed
    1.85 +    qed
    1.86 +    obtain E where "E \<subseteq> D" and djntE: "?djnt E" and maximalE: "\<And>X. \<lbrakk>X \<subseteq> D; ?djnt X; E \<subseteq> X\<rbrakk> \<Longrightarrow> X = E"
    1.87 +      using Zorn_Lemma [OF *] by safe blast
    1.88 +    show "\<exists>L. (L \<subseteq> K \<and>
    1.89 +               (\<forall>i\<in>L. B/2 ^ Suc n \<le> r i) \<and> ?djnt L \<and>
    1.90 +               (\<forall>i\<in>K. B/2 ^ Suc n < r i \<longrightarrow> (\<exists>j. j \<in> L \<and> ?cover_ar i j))) \<and> C \<subseteq> L"
    1.91 +    proof (intro exI conjI ballI)
    1.92 +      show "C \<union> E \<subseteq> K"
    1.93 +        using D_def \<open>C \<subseteq> K\<close> \<open>E \<subseteq> D\<close> by blast
    1.94 +      show "B/2 ^ Suc n \<le> r i" if i: "i \<in> C \<union> E" for i
    1.95 +        using i
    1.96 +      proof
    1.97 +        assume "i \<in> C"
    1.98 +        have "B/2 ^ Suc n \<le> B/2 ^ n"
    1.99 +          using \<open>B > 0\<close> by (simp add: divide_simps)
   1.100 +        also have "\<dots> \<le> r i"
   1.101 +          using Ble \<open>i \<in> C\<close> by blast
   1.102 +        finally show ?thesis .
   1.103 +      qed (use D_def \<open>E \<subseteq> D\<close> in auto)
   1.104 +      show "?djnt (C \<union> E)"
   1.105 +        using D_def \<open>C \<subseteq> K\<close> \<open>E \<subseteq> D\<close> djntC djntE
   1.106 +        unfolding pairwise_def disjnt_def by blast
   1.107 +    next
   1.108 +      fix i
   1.109 +      assume "i \<in> K"
   1.110 +      show "B/2 ^ Suc n < r i \<longrightarrow> (\<exists>j. j \<in> C \<union> E \<and> ?cover_ar i j)"
   1.111 +      proof (cases "r i \<le> B/2^n")
   1.112 +        case False
   1.113 +        then show ?thesis
   1.114 +          using cov_n \<open>i \<in> K\<close> by auto
   1.115 +      next
   1.116 +        case True
   1.117 +        have "cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
   1.118 +          if less: "B/2 ^ Suc n < r i" and j: "j \<in> C \<union> E"
   1.119 +            and nondis: "\<not> disjnt (cball (a i) (r i)) (cball (a j) (r j))" for j
   1.120 +        proof -
   1.121 +          obtain x where x: "dist (a i) x \<le> r i" "dist (a j) x \<le> r j"
   1.122 +            using nondis by (force simp: disjnt_def)
   1.123 +          have "dist (a i) (a j) \<le> dist (a i) x + dist x (a j)"
   1.124 +            by (simp add: dist_triangle)
   1.125 +          also have "\<dots> \<le> r i + r j"
   1.126 +            by (metis add_mono_thms_linordered_semiring(1) dist_commute x)
   1.127 +          finally have aij: "dist (a i) (a j) + r i < 5 * r j" if "r i < 2 * r j"
   1.128 +            using that by auto
   1.129 +          show ?thesis
   1.130 +            using j
   1.131 +          proof
   1.132 +            assume "j \<in> C"
   1.133 +            have "B/2^n < 2 * r j"
   1.134 +              using Ble True \<open>j \<in> C\<close> less by auto
   1.135 +            with aij True show "cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
   1.136 +              by (simp add: cball_subset_ball_iff)
   1.137 +          next
   1.138 +            assume "j \<in> E"
   1.139 +            then have "B/2 ^ n < 2 * r j"
   1.140 +              using D_def \<open>E \<subseteq> D\<close> by auto
   1.141 +            with True have "r i < 2 * r j"
   1.142 +              by auto
   1.143 +            with aij show "cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
   1.144 +              by (simp add: cball_subset_ball_iff)
   1.145 +          qed
   1.146 +        qed
   1.147 +      moreover have "\<exists>j. j \<in> C \<union> E \<and> \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j))"
   1.148 +        if "B/2 ^ Suc n < r i"
   1.149 +      proof (rule classical)
   1.150 +        assume NON: "\<not> ?thesis"
   1.151 +        show ?thesis
   1.152 +        proof (cases "i \<in> D")
   1.153 +          case True
   1.154 +          have "insert i E = E"
   1.155 +          proof (rule maximalE)
   1.156 +            show "insert i E \<subseteq> D"
   1.157 +              by (simp add: True \<open>E \<subseteq> D\<close>)
   1.158 +            show "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) (insert i E)"
   1.159 +              using False NON by (auto simp: pairwise_insert djntE disjnt_sym)
   1.160 +          qed auto
   1.161 +          then show ?thesis
   1.162 +            using \<open>i \<in> K\<close> assms by fastforce
   1.163 +        next
   1.164 +          case False
   1.165 +          with that show ?thesis
   1.166 +            by (auto simp: D_def disjnt_def \<open>i \<in> K\<close>)
   1.167 +        qed
   1.168 +      qed
   1.169 +      ultimately
   1.170 +      show "B/2 ^ Suc n < r i \<longrightarrow>
   1.171 +            (\<exists>j. j \<in> C \<union> E \<and>
   1.172 +                 \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j)) \<and>
   1.173 +                 cball (a i) (r i) \<subseteq> ball (a j) (5 * r j))"
   1.174 +        by blast
   1.175 +      qed
   1.176 +    qed auto
   1.177 +  qed (use assms in force)
   1.178 +  then obtain F where FK: "\<And>n. F n \<subseteq> K"
   1.179 +               and Fle: "\<And>n i. i \<in> F n \<Longrightarrow> B/2 ^ n \<le> r i"
   1.180 +               and Fdjnt:  "\<And>n. ?djnt (F n)"
   1.181 +               and FF:  "\<And>n i. \<lbrakk>i \<in> K; B/2 ^ n < r i\<rbrakk>
   1.182 +                        \<Longrightarrow> \<exists>j. j \<in> F n \<and> \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j)) \<and>
   1.183 +                                cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
   1.184 +               and inc:  "\<And>n. F n \<subseteq> F(Suc n)"
   1.185 +    by (force simp: all_conj_distrib)
   1.186 +  show thesis
   1.187 +  proof
   1.188 +    have *: "countable I"
   1.189 +      if "I \<subseteq> K" and pw: "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) I" for I
   1.190 +    proof -
   1.191 +      show ?thesis
   1.192 +      proof (rule countable_image_inj_on [of "\<lambda>i. cball(a i)(r i)"])
   1.193 +        show "countable ((\<lambda>i. cball (a i) (r i)) ` I)"
   1.194 +        proof (rule countable_disjoint_nonempty_interior_subsets)
   1.195 +          show "disjoint ((\<lambda>i. cball (a i) (r i)) ` I)"
   1.196 +            by (auto simp: dest: pairwiseD [OF pw] intro: pairwise_imageI)
   1.197 +          show "\<And>S. \<lbrakk>S \<in> (\<lambda>i. cball (a i) (r i)) ` I; interior S = {}\<rbrakk> \<Longrightarrow> S = {}"
   1.198 +            using \<open>I \<subseteq> K\<close>
   1.199 +            by (auto simp: not_less [symmetric])
   1.200 +        qed
   1.201 +      next
   1.202 +        have "\<And>x y. \<lbrakk>x \<in> I; y \<in> I; a x = a y; r x = r y\<rbrakk> \<Longrightarrow> x = y"
   1.203 +          using pw \<open>I \<subseteq> K\<close> assms
   1.204 +          apply (clarsimp simp: pairwise_def disjnt_def)
   1.205 +          by (metis assms centre_in_cball subsetD empty_iff inf.idem less_eq_real_def)
   1.206 +        then show "inj_on (\<lambda>i. cball (a i) (r i)) I"
   1.207 +          using \<open>I \<subseteq> K\<close> by (fastforce simp: inj_on_def cball_eq_cball_iff dest: assms)
   1.208 +      qed
   1.209 +    qed
   1.210 +    show "(Union(range F)) \<subseteq> K"
   1.211 +      using FK by blast
   1.212 +    moreover show "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) (Union(range F))"
   1.213 +    proof (rule pairwise_chain_Union)
   1.214 +      show "chain\<^sub>\<subseteq> (range F)"
   1.215 +        unfolding chain_subset_def by clarify (meson inc lift_Suc_mono_le linear subsetCE)
   1.216 +    qed (use Fdjnt in blast)
   1.217 +    ultimately show "countable (Union(range F))"
   1.218 +      by (blast intro: *)
   1.219 +  next
   1.220 +    fix i assume "i \<in> K"
   1.221 +    then obtain n where "(1/2) ^ n < r i / B"
   1.222 +      using  \<open>B > 0\<close> assms real_arch_pow_inv by fastforce
   1.223 +    then have B2: "B/2 ^ n < r i"
   1.224 +      using \<open>B > 0\<close> by (simp add: divide_simps)
   1.225 +    have "0 < r i" "r i \<le> B"
   1.226 +      by (auto simp: \<open>i \<in> K\<close> assms)
   1.227 +    show "\<exists>j. j \<in> (Union(range F)) \<and>
   1.228 +          \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j)) \<and>
   1.229 +          cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
   1.230 +      using FF [OF \<open>i \<in> K\<close> B2] by auto
   1.231 +  qed
   1.232 +qed
   1.233 +
   1.234 +subsection\<open>Vitali covering theorem\<close>
   1.235 +
   1.236 +lemma Vitali_covering_lemma_cballs:
   1.237 +  fixes a :: "'a \<Rightarrow> 'b::euclidean_space"
   1.238 +  assumes S: "S \<subseteq> (\<Union>i\<in>K. cball (a i) (r i))"
   1.239 +      and r: "\<And>i. i \<in> K \<Longrightarrow> 0 < r i \<and> r i \<le> B"
   1.240 +  obtains C where "countable C" "C \<subseteq> K"
   1.241 +     "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) C"
   1.242 +     "S \<subseteq> (\<Union>i\<in>C. cball (a i) (5 * r i))"
   1.243 +proof -
   1.244 +  obtain C where C: "countable C" "C \<subseteq> K"
   1.245 +                    "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) C"
   1.246 +           and cov: "\<And>i. i \<in> K \<Longrightarrow> \<exists>j. j \<in> C \<and> \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j)) \<and>
   1.247 +                        cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
   1.248 +    by (rule Vitali_covering_lemma_cballs_balls [OF r, where a=a]) (blast intro: that)+
   1.249 +  show ?thesis
   1.250 +  proof
   1.251 +    have "(\<Union>i\<in>K. cball (a i) (r i)) \<subseteq> (\<Union>i\<in>C. cball (a i) (5 * r i))"
   1.252 +      using cov subset_iff by fastforce
   1.253 +    with S show "S \<subseteq> (\<Union>i\<in>C. cball (a i) (5 * r i))"
   1.254 +      by blast
   1.255 +  qed (use C in auto)
   1.256 +qed
   1.257 +
   1.258 +lemma Vitali_covering_lemma_balls:
   1.259 +  fixes a :: "'a \<Rightarrow> 'b::euclidean_space"
   1.260 +  assumes S: "S \<subseteq> (\<Union>i\<in>K. ball (a i) (r i))"
   1.261 +      and r: "\<And>i. i \<in> K \<Longrightarrow> 0 < r i \<and> r i \<le> B"
   1.262 +  obtains C where "countable C" "C \<subseteq> K"
   1.263 +     "pairwise (\<lambda>i j. disjnt (ball (a i) (r i)) (ball (a j) (r j))) C"
   1.264 +     "S \<subseteq> (\<Union>i\<in>C. ball (a i) (5 * r i))"
   1.265 +proof -
   1.266 +  obtain C where C: "countable C" "C \<subseteq> K"
   1.267 +           and pw:  "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) C"
   1.268 +           and cov: "\<And>i. i \<in> K \<Longrightarrow> \<exists>j. j \<in> C \<and> \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j)) \<and>
   1.269 +                        cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
   1.270 +    by (rule Vitali_covering_lemma_cballs_balls [OF r, where a=a]) (blast intro: that)+
   1.271 +  show ?thesis
   1.272 +  proof
   1.273 +    have "(\<Union>i\<in>K. ball (a i) (r i)) \<subseteq> (\<Union>i\<in>C. ball (a i) (5 * r i))"
   1.274 +      using cov subset_iff
   1.275 +      by clarsimp (meson less_imp_le mem_ball mem_cball subset_eq)
   1.276 +    with S show "S \<subseteq> (\<Union>i\<in>C. ball (a i) (5 * r i))"
   1.277 +      by blast
   1.278 +    show "pairwise (\<lambda>i j. disjnt (ball (a i) (r i)) (ball (a j) (r j))) C"
   1.279 +      using pw
   1.280 +      by (clarsimp simp: pairwise_def) (meson ball_subset_cball disjnt_subset1 disjnt_subset2)
   1.281 +  qed (use C in auto)
   1.282 +qed
   1.283 +
   1.284 +
   1.285 +proposition Vitali_covering_theorem_cballs:
   1.286 +  fixes a :: "'a \<Rightarrow> 'n::euclidean_space"
   1.287 +  assumes r: "\<And>i. i \<in> K \<Longrightarrow> 0 < r i"
   1.288 +      and S: "\<And>x d. \<lbrakk>x \<in> S; 0 < d\<rbrakk>
   1.289 +                     \<Longrightarrow> \<exists>i. i \<in> K \<and> x \<in> cball (a i) (r i) \<and> r i < d"
   1.290 +  obtains C where "countable C" "C \<subseteq> K"
   1.291 +     "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) C"
   1.292 +     "negligible(S - (\<Union>i \<in> C. cball (a i) (r i)))"
   1.293 +proof -
   1.294 +  let ?\<mu> = "measure lebesgue"
   1.295 +  have *: "\<exists>C. countable C \<and> C \<subseteq> K \<and>
   1.296 +            pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) C \<and>
   1.297 +            negligible(S - (\<Union>i \<in> C. cball (a i) (r i)))"
   1.298 +    if r01: "\<And>i. i \<in> K \<Longrightarrow> 0 < r i \<and> r i \<le> 1"
   1.299 +       and Sd: "\<And>x d. \<lbrakk>x \<in> S; 0 < d\<rbrakk> \<Longrightarrow> \<exists>i. i \<in> K \<and> x \<in> cball (a i) (r i) \<and> r i < d"
   1.300 +     for K r and a :: "'a \<Rightarrow> 'n"
   1.301 +  proof -
   1.302 +    obtain C where C: "countable C" "C \<subseteq> K"
   1.303 +      and pwC: "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) C"
   1.304 +      and cov: "\<And>i. i \<in> K \<Longrightarrow> \<exists>j. j \<in> C \<and> \<not> disjnt (cball (a i) (r i)) (cball (a j) (r j)) \<and>
   1.305 +                        cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
   1.306 +      by (rule Vitali_covering_lemma_cballs_balls [of K r 1 a]) (auto simp: r01)
   1.307 +    have ar_injective: "\<And>x y. \<lbrakk>x \<in> C; y \<in> C; a x = a y; r x = r y\<rbrakk> \<Longrightarrow> x = y"
   1.308 +      using \<open>C \<subseteq> K\<close> pwC cov
   1.309 +      by (force simp: pairwise_def disjnt_def)
   1.310 +    show ?thesis
   1.311 +    proof (intro exI conjI)
   1.312 +      show "negligible (S - (\<Union>i\<in>C. cball (a i) (r i)))"
   1.313 +      proof (clarsimp simp: negligible_on_intervals [of "S-T" for T])
   1.314 +        fix l u
   1.315 +        show "negligible ((S - (\<Union>i\<in>C. cball (a i) (r i))) \<inter> cbox l u)"
   1.316 +          unfolding negligible_outer_le
   1.317 +        proof (intro allI impI)
   1.318 +          fix e::real
   1.319 +          assume "e > 0"
   1.320 +          define D where "D \<equiv> {i \<in> C. \<not> disjnt (ball(a i) (5 * r i)) (cbox l u)}"
   1.321 +          then have "D \<subseteq> C"
   1.322 +            by auto
   1.323 +          have "countable D"
   1.324 +            unfolding D_def using \<open>countable C\<close> by simp
   1.325 +          have UD: "(\<Union>i\<in>D. cball (a i) (r i)) \<in> lmeasurable"
   1.326 +          proof (rule fmeasurableI2)
   1.327 +            show "cbox (l - 6 *\<^sub>R One) (u + 6 *\<^sub>R One) \<in> lmeasurable"
   1.328 +              by blast
   1.329 +            have "y \<in> cbox (l - 6 *\<^sub>R One) (u + 6 *\<^sub>R One)"
   1.330 +              if "i \<in> C" and x: "x \<in> cbox l u" and ai: "dist (a i) y \<le> r i" "dist (a i) x < 5 * r i"
   1.331 +              for i x y
   1.332 +            proof -
   1.333 +              have d6: "dist y x < 6 * r i"
   1.334 +                using dist_triangle3 [of y x "a i"] that by linarith
   1.335 +              show ?thesis
   1.336 +              proof (clarsimp simp: mem_box algebra_simps)
   1.337 +                fix j::'n
   1.338 +                assume j: "j \<in> Basis"
   1.339 +                then have xyj: "\<bar>x \<bullet> j - y \<bullet> j\<bar> \<le> dist y x"
   1.340 +                  by (metis Basis_le_norm dist_commute dist_norm inner_diff_left)
   1.341 +                have "l \<bullet> j \<le> x \<bullet> j"
   1.342 +                  using \<open>j \<in> Basis\<close> mem_box \<open>x \<in> cbox l u\<close> by blast
   1.343 +                also have "\<dots> \<le> y \<bullet> j + 6 * r i"
   1.344 +                  using d6 xyj by (auto simp: algebra_simps)
   1.345 +                also have "\<dots> \<le> y \<bullet> j + 6"
   1.346 +                  using r01 [of i] \<open>C \<subseteq> K\<close> \<open>i \<in> C\<close> by auto
   1.347 +                finally have l: "l \<bullet> j \<le> y \<bullet> j + 6" .
   1.348 +                have "y \<bullet> j \<le> x \<bullet> j + 6 * r i"
   1.349 +                  using d6 xyj by (auto simp: algebra_simps)
   1.350 +                also have "\<dots> \<le> u \<bullet> j + 6 * r i"
   1.351 +                  using j  x by (auto simp: mem_box)
   1.352 +                also have "\<dots> \<le> u \<bullet> j + 6"
   1.353 +                  using r01 [of i] \<open>C \<subseteq> K\<close> \<open>i \<in> C\<close> by auto
   1.354 +                finally have u: "y \<bullet> j \<le> u \<bullet> j + 6" .
   1.355 +                show "l \<bullet> j \<le> y \<bullet> j + 6 \<and> y \<bullet> j \<le> u \<bullet> j + 6"
   1.356 +                  using l u by blast
   1.357 +              qed
   1.358 +            qed
   1.359 +            then show "(\<Union>i\<in>D. cball (a i) (r i)) \<subseteq> cbox (l - 6 *\<^sub>R One) (u + 6 *\<^sub>R One)"
   1.360 +              by (force simp: D_def disjnt_def)
   1.361 +            show "(\<Union>i\<in>D. cball (a i) (r i)) \<in> sets lebesgue"
   1.362 +              using \<open>countable D\<close> by auto
   1.363 +          qed
   1.364 +          obtain D1 where "D1 \<subseteq> D" "finite D1"
   1.365 +            and measD1: "?\<mu> (\<Union>i\<in>D. cball (a i) (r i)) - e / 5 ^ DIM('n) < ?\<mu> (\<Union>i\<in>D1. cball (a i) (r i))"
   1.366 +          proof (rule measure_countable_Union_approachable [where e = "e / 5 ^ (DIM('n))"])
   1.367 +            show "countable ((\<lambda>i. cball (a i) (r i)) ` D)"
   1.368 +              using \<open>countable D\<close> by auto
   1.369 +            show "\<And>d. d \<in> (\<lambda>i. cball (a i) (r i)) ` D \<Longrightarrow> d \<in> lmeasurable"
   1.370 +              by auto
   1.371 +            show "\<And>D'. \<lbrakk>D' \<subseteq> (\<lambda>i. cball (a i) (r i)) ` D; finite D'\<rbrakk> \<Longrightarrow> ?\<mu> (\<Union>D') \<le> ?\<mu> (\<Union>i\<in>D. cball (a i) (r i))"
   1.372 +              by (fastforce simp add: intro!: measure_mono_fmeasurable UD)
   1.373 +          qed (use \<open>e > 0\<close> in \<open>auto dest: finite_subset_image\<close>)
   1.374 +          show "\<exists>T. (S - (\<Union>i\<in>C. cball (a i) (r i))) \<inter>
   1.375 +                    cbox l u \<subseteq> T \<and> T \<in> lmeasurable \<and> ?\<mu> T \<le> e"
   1.376 +          proof (intro exI conjI)
   1.377 +            show "(S - (\<Union>i\<in>C. cball (a i) (r i))) \<inter> cbox l u \<subseteq> (\<Union>i\<in>D - D1. ball (a i) (5 * r i))"
   1.378 +            proof clarify
   1.379 +              fix x
   1.380 +              assume x: "x \<in> cbox l u" "x \<in> S" "x \<notin> (\<Union>i\<in>C. cball (a i) (r i))"
   1.381 +              have "closed (\<Union>i\<in>D1. cball (a i) (r i))"
   1.382 +                using \<open>finite D1\<close> by blast
   1.383 +              moreover have "x \<notin> (\<Union>j\<in>D1. cball (a j) (r j))"
   1.384 +                using x \<open>D1 \<subseteq> D\<close> unfolding D_def by blast
   1.385 +              ultimately obtain q where "q > 0" and q: "ball x q \<subseteq> - (\<Union>i\<in>D1. cball (a i) (r i))"
   1.386 +                by (metis (no_types, lifting) ComplI open_contains_ball closed_def)
   1.387 +              obtain i where "i \<in> K" and xi: "x \<in> cball (a i) (r i)" and ri: "r i < q/2"
   1.388 +                using Sd [OF \<open>x \<in> S\<close>] \<open>q > 0\<close> half_gt_zero by blast
   1.389 +              then obtain j where "j \<in> C"
   1.390 +                             and nondisj: "\<not> disjnt (cball (a i) (r i)) (cball (a j) (r j))"
   1.391 +                             and sub5j:  "cball (a i) (r i) \<subseteq> ball (a j) (5 * r j)"
   1.392 +                using cov [OF \<open>i \<in> K\<close>] by metis
   1.393 +              show "x \<in> (\<Union>i\<in>D - D1. ball (a i) (5 * r i))"
   1.394 +              proof
   1.395 +                show "j \<in> D - D1"
   1.396 +                proof
   1.397 +                  show "j \<in> D"
   1.398 +                    using \<open>j \<in> C\<close> sub5j \<open>x \<in> cbox l u\<close> xi by (auto simp: D_def disjnt_def)
   1.399 +                  obtain y where yi: "dist (a i) y \<le> r i" and yj: "dist (a j) y \<le> r j"
   1.400 +                    using disjnt_def nondisj by fastforce
   1.401 +                  have "dist x y \<le> r i + r i"
   1.402 +                    by (metis add_mono dist_commute dist_triangle_le mem_cball xi yi)
   1.403 +                  also have "\<dots> < q"
   1.404 +                    using ri by linarith
   1.405 +                  finally have "y \<in> ball x q"
   1.406 +                    by simp
   1.407 +                  with yj q show "j \<notin> D1"
   1.408 +                    by (auto simp: disjoint_UN_iff)
   1.409 +                qed
   1.410 +                show "x \<in> ball (a j) (5 * r j)"
   1.411 +                  using xi sub5j by blast
   1.412 +              qed
   1.413 +            qed
   1.414 +            have 3: "?\<mu> (\<Union>i\<in>D2. ball (a i) (5 * r i)) \<le> e"
   1.415 +              if D2: "D2 \<subseteq> D - D1" and "finite D2" for D2
   1.416 +            proof -
   1.417 +              have rgt0: "0 < r i" if "i \<in> D2" for i
   1.418 +                using \<open>C \<subseteq> K\<close> D_def \<open>i \<in> D2\<close> D2 r01
   1.419 +                by (simp add: subset_iff)
   1.420 +              then have inj: "inj_on (\<lambda>i. ball (a i) (5 * r i)) D2"
   1.421 +                using \<open>C \<subseteq> K\<close> D2 by (fastforce simp: inj_on_def D_def ball_eq_ball_iff intro: ar_injective)
   1.422 +              have "?\<mu> (\<Union>i\<in>D2. ball (a i) (5 * r i)) \<le> sum (?\<mu>) ((\<lambda>i. ball (a i) (5 * r i)) ` D2)"
   1.423 +                using that by (force intro: measure_Union_le)
   1.424 +              also have "\<dots>  = (\<Sum>i\<in>D2. ?\<mu> (ball (a i) (5 * r i)))"
   1.425 +                by (simp add: comm_monoid_add_class.sum.reindex [OF inj])
   1.426 +              also have "\<dots> = (\<Sum>i\<in>D2. 5 ^ DIM('n) * ?\<mu> (ball (a i) (r i)))"
   1.427 +              proof (rule sum.cong [OF refl])
   1.428 +                fix i
   1.429 +                assume "i \<in> D2"
   1.430 +                show "?\<mu> (ball (a i) (5 * r i)) = 5 ^ DIM('n) * ?\<mu> (ball (a i) (r i))"
   1.431 +                  using rgt0 [OF \<open>i \<in> D2\<close>] by (simp add: content_ball)
   1.432 +              qed
   1.433 +              also have "\<dots> = (\<Sum>i\<in>D2. ?\<mu> (ball (a i) (r i))) * 5 ^ DIM('n)"
   1.434 +                by (simp add: sum_distrib_left mult.commute)
   1.435 +              finally have "?\<mu> (\<Union>i\<in>D2. ball (a i) (5 * r i)) \<le> (\<Sum>i\<in>D2. ?\<mu> (ball (a i) (r i))) * 5 ^ DIM('n)" .
   1.436 +              moreover have "(\<Sum>i\<in>D2. ?\<mu> (ball (a i) (r i))) \<le> e / 5 ^ DIM('n)"
   1.437 +              proof -
   1.438 +                have D12_dis: "((\<Union>x\<in>D1. cball (a x) (r x)) \<inter> (\<Union>x\<in>D2. cball (a x) (r x))) \<le> {}"
   1.439 +                proof clarify
   1.440 +                  fix w d1 d2
   1.441 +                  assume "d1 \<in> D1" "w d1 d2 \<in> cball (a d1) (r d1)" "d2 \<in> D2" "w d1 d2 \<in> cball (a d2) (r d2)"
   1.442 +                  then show "w d1 d2 \<in> {}"
   1.443 +                    by (metis DiffE disjnt_iff subsetCE D2 \<open>D1 \<subseteq> D\<close> \<open>D \<subseteq> C\<close> pairwiseD [OF pwC, of d1 d2])
   1.444 +                qed
   1.445 +                have inj: "inj_on (\<lambda>i. cball (a i) (r i)) D2"
   1.446 +                  using rgt0 D2 \<open>D \<subseteq> C\<close> by (force simp: inj_on_def cball_eq_cball_iff intro!: ar_injective)
   1.447 +                have ds: "disjoint ((\<lambda>i. cball (a i) (r i)) ` D2)"
   1.448 +                  using D2 \<open>D \<subseteq> C\<close> by (auto intro: pairwiseI pairwiseD [OF pwC])
   1.449 +                have "(\<Sum>i\<in>D2. ?\<mu> (ball (a i) (r i))) = (\<Sum>i\<in>D2. ?\<mu> (cball (a i) (r i)))"
   1.450 +                  using rgt0 by (simp add: content_ball content_cball less_eq_real_def)
   1.451 +                also have "\<dots> = sum ?\<mu> ((\<lambda>i. cball (a i) (r i)) ` D2)"
   1.452 +                  by (simp add: comm_monoid_add_class.sum.reindex [OF inj])
   1.453 +                also have "\<dots> = ?\<mu> (\<Union>i\<in>D2. cball (a i) (r i))"
   1.454 +                  by (auto intro: measure_Union' [symmetric] ds simp add: \<open>finite D2\<close>)
   1.455 +                finally have "?\<mu> (\<Union>i\<in>D1. cball (a i) (r i)) + (\<Sum>i\<in>D2. ?\<mu> (ball (a i) (r i))) =
   1.456 +                              ?\<mu> (\<Union>i\<in>D1. cball (a i) (r i)) + ?\<mu> (\<Union>i\<in>D2. cball (a i) (r i))"
   1.457 +                  by simp
   1.458 +                also have "\<dots> = ?\<mu> (\<Union>i \<in> D1 \<union> D2. cball (a i) (r i))"
   1.459 +                  using D12_dis by (simp add: measure_Un3 \<open>finite D1\<close> \<open>finite D2\<close> fmeasurable.finite_UN)
   1.460 +                also have "\<dots> \<le> ?\<mu> (\<Union>i\<in>D. cball (a i) (r i))"
   1.461 +                  using D2 \<open>D1 \<subseteq> D\<close> by (fastforce intro!: measure_mono_fmeasurable [OF _ _ UD] \<open>finite D1\<close> \<open>finite D2\<close>)
   1.462 +                finally have "?\<mu> (\<Union>i\<in>D1. cball (a i) (r i)) + (\<Sum>i\<in>D2. ?\<mu> (ball (a i) (r i))) \<le> ?\<mu> (\<Union>i\<in>D. cball (a i) (r i))" .
   1.463 +                with measD1 show ?thesis
   1.464 +                  by simp
   1.465 +              qed
   1.466 +                ultimately show ?thesis
   1.467 +                  by (simp add: divide_simps)
   1.468 +            qed
   1.469 +            have co: "countable (D - D1)"
   1.470 +              by (simp add: \<open>countable D\<close>)
   1.471 +            show "(\<Union>i\<in>D - D1. ball (a i) (5 * r i)) \<in> lmeasurable"
   1.472 +              using \<open>e > 0\<close> by (auto simp: fmeasurable_UN_bound [OF co _ 3])
   1.473 +            show "?\<mu> (\<Union>i\<in>D - D1. ball (a i) (5 * r i)) \<le> e"
   1.474 +              using \<open>e > 0\<close> by (auto simp: measure_UN_bound [OF co _ 3])
   1.475 +          qed
   1.476 +        qed
   1.477 +      qed
   1.478 +    qed (use C pwC in auto)
   1.479 +  qed
   1.480 +  define K' where "K' \<equiv> {i \<in> K. r i \<le> 1}"
   1.481 +  have 1: "\<And>i. i \<in> K' \<Longrightarrow> 0 < r i \<and> r i \<le> 1"
   1.482 +    using K'_def r by auto
   1.483 +  have 2: "\<exists>i. i \<in> K' \<and> x \<in> cball (a i) (r i) \<and> r i < d"
   1.484 +    if "x \<in> S \<and> 0 < d" for x d
   1.485 +    using that by (auto simp: K'_def dest!: S [where d = "min d 1"])
   1.486 +  have "K' \<subseteq> K"
   1.487 +    using K'_def by auto
   1.488 +  then show thesis
   1.489 +    using * [OF 1 2] that by fastforce
   1.490 +qed
   1.491 +
   1.492 +
   1.493 +proposition Vitali_covering_theorem_balls:
   1.494 +  fixes a :: "'a \<Rightarrow> 'b::euclidean_space"
   1.495 +  assumes S: "\<And>x d. \<lbrakk>x \<in> S; 0 < d\<rbrakk> \<Longrightarrow> \<exists>i. i \<in> K \<and> x \<in> ball (a i) (r i) \<and> r i < d"
   1.496 +  obtains C where "countable C" "C \<subseteq> K"
   1.497 +     "pairwise (\<lambda>i j. disjnt (ball (a i) (r i)) (ball (a j) (r j))) C"
   1.498 +     "negligible(S - (\<Union>i \<in> C. ball (a i) (r i)))"
   1.499 +proof -
   1.500 +  have 1: "\<exists>i. i \<in> {i \<in> K. 0 < r i} \<and> x \<in> cball (a i) (r i) \<and> r i < d"
   1.501 +         if xd: "x \<in> S" "d > 0" for x d
   1.502 +    by (metis (mono_tags, lifting) assms ball_eq_empty less_eq_real_def mem_Collect_eq mem_ball mem_cball not_le xd(1) xd(2))
   1.503 +  obtain C where C: "countable C" "C \<subseteq> K"
   1.504 +             and pw: "pairwise (\<lambda>i j. disjnt (cball (a i) (r i)) (cball (a j) (r j))) C"
   1.505 +             and neg: "negligible(S - (\<Union>i \<in> C. cball (a i) (r i)))"
   1.506 +    by (rule Vitali_covering_theorem_cballs [of "{i \<in> K. 0 < r i}" r S a, OF _ 1]) auto
   1.507 +  show thesis
   1.508 +  proof
   1.509 +    show "pairwise (\<lambda>i j. disjnt (ball (a i) (r i)) (ball (a j) (r j))) C"
   1.510 +      apply (rule pairwise_mono [OF pw])
   1.511 +      apply (auto simp: disjnt_def)
   1.512 +      by (meson disjoint_iff_not_equal less_imp_le mem_cball)
   1.513 +    have "negligible (\<Union>i\<in>C. sphere (a i) (r i))"
   1.514 +      by (auto intro: negligible_sphere \<open>countable C\<close>)
   1.515 +    then have "negligible (S - (\<Union>i \<in> C. cball(a i)(r i)) \<union> (\<Union>i \<in> C. sphere (a i) (r i)))"
   1.516 +      by (rule negligible_Un [OF neg])
   1.517 +    then show "negligible (S - (\<Union>i\<in>C. ball (a i) (r i)))"
   1.518 +      by (rule negligible_subset) force
   1.519 +  qed (use C in auto)
   1.520 +qed
   1.521 +
   1.522 +
   1.523 +lemma negligible_eq_zero_density_alt:
   1.524 +     "negligible S \<longleftrightarrow>
   1.525 +      (\<forall>x \<in> S. \<forall>e > 0.
   1.526 +        \<exists>d U. 0 < d \<and> d \<le> e \<and> S \<inter> ball x d \<subseteq> U \<and>
   1.527 +              U \<in> lmeasurable \<and> measure lebesgue U < e * measure lebesgue (ball x d))"
   1.528 +     (is "_ = (\<forall>x \<in> S. \<forall>e > 0. ?Q x e)")
   1.529 +proof (intro iffI ballI allI impI)
   1.530 +  fix x and e :: real
   1.531 +  assume "negligible S" and "x \<in> S" and "e > 0"
   1.532 +  then
   1.533 +  show "\<exists>d U. 0 < d \<and> d \<le> e \<and> S \<inter> ball x d \<subseteq> U \<and> U \<in> lmeasurable \<and>
   1.534 +              measure lebesgue U < e * measure lebesgue (ball x d)"
   1.535 +    apply (rule_tac x=e in exI)
   1.536 +    apply (rule_tac x="S \<inter> ball x e" in exI)
   1.537 +    apply (auto simp: negligible_imp_measurable negligible_Int negligible_imp_measure0 zero_less_measure_iff)
   1.538 +    done
   1.539 +next
   1.540 +  assume R [rule_format]: "\<forall>x \<in> S. \<forall>e > 0. ?Q x e"
   1.541 +  let ?\<mu> = "measure lebesgue"
   1.542 +  have "\<exists>U. openin (subtopology euclidean S) U \<and> z \<in> U \<and> negligible U"
   1.543 +    if "z \<in> S" for z
   1.544 +  proof (intro exI conjI)
   1.545 +    show "openin (subtopology euclidean S) (S \<inter> ball z 1)"
   1.546 +      by (simp add: openin_open_Int)
   1.547 +    show "z \<in> S \<inter> ball z 1"
   1.548 +      using \<open>z \<in> S\<close> by auto
   1.549 +    show "negligible (S \<inter> ball z 1)"
   1.550 +    proof (clarsimp simp: negligible_outer_le)
   1.551 +      fix e :: "real"
   1.552 +      assume "e > 0"
   1.553 +      let ?K = "{(x,d). x \<in> S \<and> 0 < d \<and> ball x d \<subseteq> ball z 1 \<and>
   1.554 +                     (\<exists>U. S \<inter> ball x d \<subseteq> U \<and> U \<in> lmeasurable \<and>
   1.555 +                         ?\<mu> U < e / ?\<mu> (ball z 1) * ?\<mu> (ball x d))}"
   1.556 +      obtain C where "countable C" and Csub: "C \<subseteq> ?K"
   1.557 +        and pwC: "pairwise (\<lambda>i j. disjnt (ball (fst i) (snd i)) (ball (fst j) (snd j))) C"
   1.558 +        and negC: "negligible((S \<inter> ball z 1) - (\<Union>i \<in> C. ball (fst i) (snd i)))"
   1.559 +      proof (rule Vitali_covering_theorem_balls [of "S \<inter> ball z 1" ?K fst snd])
   1.560 +        fix x and d :: "real"
   1.561 +        assume x: "x \<in> S \<inter> ball z 1" and "d > 0"
   1.562 +        obtain k where "k > 0" and k: "ball x k \<subseteq> ball z 1"
   1.563 +          by (meson Int_iff open_ball openE x)
   1.564 +        let ?\<epsilon> = "min (e / ?\<mu> (ball z 1) / 2) (min (d / 2) k)"
   1.565 +        obtain r U where r: "r > 0" "r \<le> ?\<epsilon>" and U: "S \<inter> ball x r \<subseteq> U" "U \<in> lmeasurable"
   1.566 +          and mU: "?\<mu> U < ?\<epsilon> * ?\<mu> (ball x r)"
   1.567 +          using R [of x ?\<epsilon>] \<open>d > 0\<close> \<open>e > 0\<close> \<open>k > 0\<close> x by auto
   1.568 +        show "\<exists>i. i \<in> ?K \<and> x \<in> ball (fst i) (snd i) \<and> snd i < d"
   1.569 +        proof (rule exI [of _ "(x,r)"], simp, intro conjI exI)
   1.570 +          have "ball x r \<subseteq> ball x k"
   1.571 +            using r by (simp add: ball_subset_ball_iff)
   1.572 +          also have "\<dots> \<subseteq> ball z 1"
   1.573 +            using ball_subset_ball_iff k by auto
   1.574 +          finally show "ball x r \<subseteq> ball z 1" .
   1.575 +          have "?\<epsilon> * ?\<mu> (ball x r) \<le> e * content (ball x r) / content (ball z 1)"
   1.576 +            using r \<open>e > 0\<close> by (simp add: ord_class.min_def divide_simps)
   1.577 +          with mU show "?\<mu> U < e * content (ball x r) / content (ball z 1)"
   1.578 +            by auto
   1.579 +        qed (use r U x in auto)
   1.580 +      qed
   1.581 +      have "\<exists>U. case p of (x,d) \<Rightarrow> S \<inter> ball x d \<subseteq> U \<and>
   1.582 +                        U \<in> lmeasurable \<and> ?\<mu> U < e / ?\<mu> (ball z 1) * ?\<mu> (ball x d)"
   1.583 +        if "p \<in> C" for p
   1.584 +        using that Csub by auto
   1.585 +      then obtain U where U:
   1.586 +                "\<And>p. p \<in> C \<Longrightarrow>
   1.587 +                       case p of (x,d) \<Rightarrow> S \<inter> ball x d \<subseteq> U p \<and>
   1.588 +                        U p \<in> lmeasurable \<and> ?\<mu> (U p) < e / ?\<mu> (ball z 1) * ?\<mu> (ball x d)"
   1.589 +        by (rule that [OF someI_ex])
   1.590 +      let ?T = "((S \<inter> ball z 1) - (\<Union>(x,d)\<in>C. ball x d)) \<union> \<Union>(U ` C)"
   1.591 +      show "\<exists>T. S \<inter> ball z 1 \<subseteq> T \<and> T \<in> lmeasurable \<and> ?\<mu> T \<le> e"
   1.592 +      proof (intro exI conjI)
   1.593 +        show "S \<inter> ball z 1 \<subseteq> ?T"
   1.594 +          using U by fastforce
   1.595 +        { have Um: "U i \<in> lmeasurable" if "i \<in> C" for i
   1.596 +            using that U by blast
   1.597 +          have lee: "?\<mu> (\<Union>i\<in>I. U i) \<le> e" if "I \<subseteq> C" "finite I" for I
   1.598 +          proof -
   1.599 +            have "?\<mu> (\<Union>(x,d)\<in>I. ball x d) \<le> ?\<mu> (ball z 1)"
   1.600 +              apply (rule measure_mono_fmeasurable)
   1.601 +              using \<open>I \<subseteq> C\<close> \<open>finite I\<close> Csub by (force simp: prod.case_eq_if sets.finite_UN)+
   1.602 +            then have le1: "(?\<mu> (\<Union>(x,d)\<in>I. ball x d) / ?\<mu> (ball z 1)) \<le> 1"
   1.603 +              by simp
   1.604 +            have "?\<mu> (\<Union>i\<in>I. U i) \<le> (\<Sum>i\<in>I. ?\<mu> (U i))"
   1.605 +              using that U by (blast intro: measure_UNION_le)
   1.606 +            also have "\<dots> \<le> (\<Sum>(x,r)\<in>I. e / ?\<mu> (ball z 1) * ?\<mu> (ball x r))"
   1.607 +              by (rule sum_mono) (use \<open>I \<subseteq> C\<close> U in force)
   1.608 +            also have "\<dots> = (e / ?\<mu> (ball z 1)) * (\<Sum>(x,r)\<in>I. ?\<mu> (ball x r))"
   1.609 +              by (simp add: case_prod_app prod.case_distrib sum_distrib_left)
   1.610 +            also have "\<dots> = e * (?\<mu> (\<Union>(x,r)\<in>I. ball x r) / ?\<mu> (ball z 1))"
   1.611 +              apply (subst measure_UNION')
   1.612 +              using that pwC by (auto simp: case_prod_unfold elim: pairwise_mono)
   1.613 +            also have "\<dots> \<le> e"
   1.614 +              by (metis mult.commute mult.left_neutral real_mult_le_cancel_iff1 \<open>e > 0\<close> le1)
   1.615 +            finally show ?thesis .
   1.616 +          qed
   1.617 +          have "UNION C U \<in> lmeasurable" "?\<mu> (\<Union>(U ` C)) \<le> e"
   1.618 +            using \<open>e > 0\<close> Um lee
   1.619 +            by(auto intro!: fmeasurable_UN_bound [OF \<open>countable C\<close>] measure_UN_bound [OF \<open>countable C\<close>])
   1.620 +        }
   1.621 +        moreover have "?\<mu> ?T = ?\<mu> (UNION C U)"
   1.622 +        proof (rule measure_negligible_symdiff [OF \<open>UNION C U \<in> lmeasurable\<close>])
   1.623 +          show "negligible((UNION C U - ?T) \<union> (?T - UNION C U))"
   1.624 +            by (force intro!: negligible_subset [OF negC])
   1.625 +        qed
   1.626 +        ultimately show "?T \<in> lmeasurable"  "?\<mu> ?T \<le> e"
   1.627 +          by (simp_all add: fmeasurable.Un negC negligible_imp_measurable split_def)
   1.628 +      qed
   1.629 +    qed
   1.630 +  qed
   1.631 +  with locally_negligible_alt show "negligible S"
   1.632 +    by metis
   1.633 +qed
   1.634 +
   1.635 +
   1.636 +proposition negligible_eq_zero_density:
   1.637 +   "negligible S \<longleftrightarrow>
   1.638 +    (\<forall>x\<in>S. \<forall>r>0. \<forall>e>0. \<exists>d. 0 < d \<and> d \<le> r \<and>
   1.639 +                   (\<exists>U. S \<inter> ball x d \<subseteq> U \<and> U \<in> lmeasurable \<and> measure lebesgue U < e * measure lebesgue (ball x d)))"
   1.640 +proof -
   1.641 +  let ?Q = "\<lambda>x d e. \<exists>U. S \<inter> ball x d \<subseteq> U \<and> U \<in> lmeasurable \<and> measure lebesgue U < e * content (ball x d)"
   1.642 +  have "(\<forall>e>0. \<exists>d>0. d \<le> e \<and> ?Q x d e) = (\<forall>r>0. \<forall>e>0. \<exists>d>0. d \<le> r \<and> ?Q x d e)"
   1.643 +    if "x \<in> S" for x
   1.644 +  proof (intro iffI allI impI)
   1.645 +    fix r :: "real" and e :: "real"
   1.646 +    assume L [rule_format]: "\<forall>e>0. \<exists>d>0. d \<le> e \<and> ?Q x d e" and "r > 0" "e > 0"
   1.647 +    show "\<exists>d>0. d \<le> r \<and> ?Q x d e"
   1.648 +      using L [of "min r e"] apply (rule ex_forward)
   1.649 +      using \<open>r > 0\<close> \<open>e > 0\<close>  by (auto intro: less_le_trans elim!: ex_forward)
   1.650 +  qed auto
   1.651 +  then show ?thesis
   1.652 +    by (force simp: negligible_eq_zero_density_alt)
   1.653 +qed
   1.654 +
   1.655 +end