Support product spaces on sigma finite measures.
authorhoelzl
Wed Dec 01 19:20:30 2010 +0100 (2010-12-01)
changeset 40859de0b30e6c2d2
parent 40854 f2c9ebbe04aa
child 40860 a6df324752da
Support product spaces on sigma finite measures.
Introduce the almost everywhere quantifier.
Introduce 'morphism' theorems for most constants.
Prove uniqueness of measures on cut stable generators.
Prove uniqueness of the Radon-Nikodym derivative.
Removed misleading suffix from borel_space and lebesgue_space.
Use product spaces to introduce Fubini on the Lebesgue integral.
Combine Euclidean_Lebesgue and Lebesgue_Measure.
Generalize theorems about mutual information and entropy to arbitrary probability spaces.
Remove simproc mult_log as it does not work with interpretations.
Introduce completion of measure spaces.
Change type of sigma.
Introduce dynkin systems.
src/HOL/Probability/Borel.thy
src/HOL/Probability/Borel_Space.thy
src/HOL/Probability/Caratheodory.thy
src/HOL/Probability/Complete_Measure.thy
src/HOL/Probability/Euclidean_Lebesgue.thy
src/HOL/Probability/Information.thy
src/HOL/Probability/Lebesgue_Integration.thy
src/HOL/Probability/Lebesgue_Measure.thy
src/HOL/Probability/Measure.thy
src/HOL/Probability/Positive_Infinite_Real.thy
src/HOL/Probability/Probability.thy
src/HOL/Probability/Probability_Space.thy
src/HOL/Probability/Product_Measure.thy
src/HOL/Probability/Radon_Nikodym.thy
src/HOL/Probability/Sigma_Algebra.thy
src/HOL/Probability/ex/Dining_Cryptographers.thy
src/HOL/Probability/ex/Koepf_Duermuth_Countermeasure.thy
     1.1 --- a/src/HOL/Probability/Borel.thy	Wed Dec 01 06:50:54 2010 -0800
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,1390 +0,0 @@
     1.4 -(* Author: Armin Heller, Johannes Hoelzl, TU Muenchen *)
     1.5 -
     1.6 -header {*Borel spaces*}
     1.7 -
     1.8 -theory Borel
     1.9 -  imports Sigma_Algebra Positive_Infinite_Real Multivariate_Analysis
    1.10 -begin
    1.11 -
    1.12 -lemma LIMSEQ_max:
    1.13 -  "u ----> (x::real) \<Longrightarrow> (\<lambda>i. max (u i) 0) ----> max x 0"
    1.14 -  by (fastsimp intro!: LIMSEQ_I dest!: LIMSEQ_D)
    1.15 -
    1.16 -section "Generic Borel spaces"
    1.17 -
    1.18 -definition "borel_space = sigma (UNIV::'a::topological_space set) open"
    1.19 -abbreviation "borel_measurable M \<equiv> measurable M borel_space"
    1.20 -
    1.21 -interpretation borel_space: sigma_algebra borel_space
    1.22 -  using sigma_algebra_sigma by (auto simp: borel_space_def)
    1.23 -
    1.24 -lemma in_borel_measurable:
    1.25 -   "f \<in> borel_measurable M \<longleftrightarrow>
    1.26 -    (\<forall>S \<in> sets (sigma UNIV open).
    1.27 -      f -` S \<inter> space M \<in> sets M)"
    1.28 -  by (auto simp add: measurable_def borel_space_def)
    1.29 -
    1.30 -lemma in_borel_measurable_borel_space:
    1.31 -   "f \<in> borel_measurable M \<longleftrightarrow>
    1.32 -    (\<forall>S \<in> sets borel_space.
    1.33 -      f -` S \<inter> space M \<in> sets M)"
    1.34 -  by (auto simp add: measurable_def borel_space_def)
    1.35 -
    1.36 -lemma space_borel_space[simp]: "space borel_space = UNIV"
    1.37 -  unfolding borel_space_def by auto
    1.38 -
    1.39 -lemma borel_space_open[simp]:
    1.40 -  assumes "open A" shows "A \<in> sets borel_space"
    1.41 -proof -
    1.42 -  have "A \<in> open" unfolding mem_def using assms .
    1.43 -  thus ?thesis unfolding borel_space_def sigma_def by (auto intro!: sigma_sets.Basic)
    1.44 -qed
    1.45 -
    1.46 -lemma borel_space_closed[simp]:
    1.47 -  assumes "closed A" shows "A \<in> sets borel_space"
    1.48 -proof -
    1.49 -  have "space borel_space - (- A) \<in> sets borel_space"
    1.50 -    using assms unfolding closed_def by (blast intro: borel_space_open)
    1.51 -  thus ?thesis by simp
    1.52 -qed
    1.53 -
    1.54 -lemma (in sigma_algebra) borel_measurable_vimage:
    1.55 -  fixes f :: "'a \<Rightarrow> 'x::t2_space"
    1.56 -  assumes borel: "f \<in> borel_measurable M"
    1.57 -  shows "f -` {x} \<inter> space M \<in> sets M"
    1.58 -proof (cases "x \<in> f ` space M")
    1.59 -  case True then obtain y where "x = f y" by auto
    1.60 -  from closed_sing[of "f y"]
    1.61 -  have "{f y} \<in> sets borel_space" by (rule borel_space_closed)
    1.62 -  with assms show ?thesis
    1.63 -    unfolding in_borel_measurable_borel_space `x = f y` by auto
    1.64 -next
    1.65 -  case False hence "f -` {x} \<inter> space M = {}" by auto
    1.66 -  thus ?thesis by auto
    1.67 -qed
    1.68 -
    1.69 -lemma (in sigma_algebra) borel_measurableI:
    1.70 -  fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
    1.71 -  assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
    1.72 -  shows "f \<in> borel_measurable M"
    1.73 -  unfolding borel_space_def
    1.74 -proof (rule measurable_sigma)
    1.75 -  fix S :: "'x set" assume "S \<in> open" thus "f -` S \<inter> space M \<in> sets M"
    1.76 -    using assms[of S] by (simp add: mem_def)
    1.77 -qed simp_all
    1.78 -
    1.79 -lemma borel_space_singleton[simp, intro]:
    1.80 -  fixes x :: "'a::t1_space"
    1.81 -  shows "A \<in> sets borel_space \<Longrightarrow> insert x A \<in> sets borel_space"
    1.82 -  proof (rule borel_space.insert_in_sets)
    1.83 -    show "{x} \<in> sets borel_space"
    1.84 -      using closed_sing[of x] by (rule borel_space_closed)
    1.85 -  qed simp
    1.86 -
    1.87 -lemma (in sigma_algebra) borel_measurable_const[simp, intro]:
    1.88 -  "(\<lambda>x. c) \<in> borel_measurable M"
    1.89 -  by (auto intro!: measurable_const)
    1.90 -
    1.91 -lemma (in sigma_algebra) borel_measurable_indicator[simp, intro!]:
    1.92 -  assumes A: "A \<in> sets M"
    1.93 -  shows "indicator A \<in> borel_measurable M"
    1.94 -  unfolding indicator_def_raw using A
    1.95 -  by (auto intro!: measurable_If_set borel_measurable_const)
    1.96 -
    1.97 -lemma borel_measurable_translate:
    1.98 -  assumes "A \<in> sets borel_space" and trans: "\<And>B. open B \<Longrightarrow> f -` B \<in> sets borel_space"
    1.99 -  shows "f -` A \<in> sets borel_space"
   1.100 -proof -
   1.101 -  have "A \<in> sigma_sets UNIV open" using assms
   1.102 -    by (simp add: borel_space_def sigma_def)
   1.103 -  thus ?thesis
   1.104 -  proof (induct rule: sigma_sets.induct)
   1.105 -    case (Basic a) thus ?case using trans[of a] by (simp add: mem_def)
   1.106 -  next
   1.107 -    case (Compl a)
   1.108 -    moreover have "UNIV \<in> sets borel_space"
   1.109 -      by (metis borel_space.top borel_space_def_raw mem_def space_sigma)
   1.110 -    ultimately show ?case
   1.111 -      by (auto simp: vimage_Diff borel_space.Diff)
   1.112 -  qed (auto simp add: vimage_UN)
   1.113 -qed
   1.114 -
   1.115 -lemma (in sigma_algebra) borel_measurable_restricted:
   1.116 -  fixes f :: "'a \<Rightarrow> 'x\<Colon>{topological_space, semiring_1}" assumes "A \<in> sets M"
   1.117 -  shows "f \<in> borel_measurable (restricted_space A) \<longleftrightarrow>
   1.118 -    (\<lambda>x. f x * indicator A x) \<in> borel_measurable M"
   1.119 -    (is "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable M")
   1.120 -proof -
   1.121 -  interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
   1.122 -  have *: "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable ?R"
   1.123 -    by (auto intro!: measurable_cong)
   1.124 -  show ?thesis unfolding *
   1.125 -    unfolding in_borel_measurable_borel_space
   1.126 -  proof (simp, safe)
   1.127 -    fix S :: "'x set" assume "S \<in> sets borel_space"
   1.128 -      "\<forall>S\<in>sets borel_space. ?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   1.129 -    then have "?f -` S \<inter> A \<in> op \<inter> A ` sets M" by auto
   1.130 -    then have f: "?f -` S \<inter> A \<in> sets M"
   1.131 -      using `A \<in> sets M` sets_into_space by fastsimp
   1.132 -    show "?f -` S \<inter> space M \<in> sets M"
   1.133 -    proof cases
   1.134 -      assume "0 \<in> S"
   1.135 -      then have "?f -` S \<inter> space M = ?f -` S \<inter> A \<union> (space M - A)"
   1.136 -        using `A \<in> sets M` sets_into_space by auto
   1.137 -      then show ?thesis using f `A \<in> sets M` by (auto intro!: Un Diff)
   1.138 -    next
   1.139 -      assume "0 \<notin> S"
   1.140 -      then have "?f -` S \<inter> space M = ?f -` S \<inter> A"
   1.141 -        using `A \<in> sets M` sets_into_space
   1.142 -        by (auto simp: indicator_def split: split_if_asm)
   1.143 -      then show ?thesis using f by auto
   1.144 -    qed
   1.145 -  next
   1.146 -    fix S :: "'x set" assume "S \<in> sets borel_space"
   1.147 -      "\<forall>S\<in>sets borel_space. ?f -` S \<inter> space M \<in> sets M"
   1.148 -    then have f: "?f -` S \<inter> space M \<in> sets M" by auto
   1.149 -    then show "?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   1.150 -      using `A \<in> sets M` sets_into_space
   1.151 -      apply (simp add: image_iff)
   1.152 -      apply (rule bexI[OF _ f])
   1.153 -      by auto
   1.154 -  qed
   1.155 -qed
   1.156 -
   1.157 -lemma (in sigma_algebra) borel_measurable_subalgebra:
   1.158 -  assumes "N \<subseteq> sets M" "f \<in> borel_measurable (M\<lparr>sets:=N\<rparr>)"
   1.159 -  shows "f \<in> borel_measurable M"
   1.160 -  using assms unfolding measurable_def by auto
   1.161 -
   1.162 -section "Borel spaces on euclidean spaces"
   1.163 -
   1.164 -lemma lessThan_borel[simp, intro]:
   1.165 -  fixes a :: "'a\<Colon>ordered_euclidean_space"
   1.166 -  shows "{..< a} \<in> sets borel_space"
   1.167 -  by (blast intro: borel_space_open)
   1.168 -
   1.169 -lemma greaterThan_borel[simp, intro]:
   1.170 -  fixes a :: "'a\<Colon>ordered_euclidean_space"
   1.171 -  shows "{a <..} \<in> sets borel_space"
   1.172 -  by (blast intro: borel_space_open)
   1.173 -
   1.174 -lemma greaterThanLessThan_borel[simp, intro]:
   1.175 -  fixes a b :: "'a\<Colon>ordered_euclidean_space"
   1.176 -  shows "{a<..<b} \<in> sets borel_space"
   1.177 -  by (blast intro: borel_space_open)
   1.178 -
   1.179 -lemma atMost_borel[simp, intro]:
   1.180 -  fixes a :: "'a\<Colon>ordered_euclidean_space"
   1.181 -  shows "{..a} \<in> sets borel_space"
   1.182 -  by (blast intro: borel_space_closed)
   1.183 -
   1.184 -lemma atLeast_borel[simp, intro]:
   1.185 -  fixes a :: "'a\<Colon>ordered_euclidean_space"
   1.186 -  shows "{a..} \<in> sets borel_space"
   1.187 -  by (blast intro: borel_space_closed)
   1.188 -
   1.189 -lemma atLeastAtMost_borel[simp, intro]:
   1.190 -  fixes a b :: "'a\<Colon>ordered_euclidean_space"
   1.191 -  shows "{a..b} \<in> sets borel_space"
   1.192 -  by (blast intro: borel_space_closed)
   1.193 -
   1.194 -lemma greaterThanAtMost_borel[simp, intro]:
   1.195 -  fixes a b :: "'a\<Colon>ordered_euclidean_space"
   1.196 -  shows "{a<..b} \<in> sets borel_space"
   1.197 -  unfolding greaterThanAtMost_def by blast
   1.198 -
   1.199 -lemma atLeastLessThan_borel[simp, intro]:
   1.200 -  fixes a b :: "'a\<Colon>ordered_euclidean_space"
   1.201 -  shows "{a..<b} \<in> sets borel_space"
   1.202 -  unfolding atLeastLessThan_def by blast
   1.203 -
   1.204 -lemma hafspace_less_borel[simp, intro]:
   1.205 -  fixes a :: real
   1.206 -  shows "{x::'a::euclidean_space. a < x $$ i} \<in> sets borel_space"
   1.207 -  by (auto intro!: borel_space_open open_halfspace_component_gt)
   1.208 -
   1.209 -lemma hafspace_greater_borel[simp, intro]:
   1.210 -  fixes a :: real
   1.211 -  shows "{x::'a::euclidean_space. x $$ i < a} \<in> sets borel_space"
   1.212 -  by (auto intro!: borel_space_open open_halfspace_component_lt)
   1.213 -
   1.214 -lemma hafspace_less_eq_borel[simp, intro]:
   1.215 -  fixes a :: real
   1.216 -  shows "{x::'a::euclidean_space. a \<le> x $$ i} \<in> sets borel_space"
   1.217 -  by (auto intro!: borel_space_closed closed_halfspace_component_ge)
   1.218 -
   1.219 -lemma hafspace_greater_eq_borel[simp, intro]:
   1.220 -  fixes a :: real
   1.221 -  shows "{x::'a::euclidean_space. x $$ i \<le> a} \<in> sets borel_space"
   1.222 -  by (auto intro!: borel_space_closed closed_halfspace_component_le)
   1.223 -
   1.224 -lemma (in sigma_algebra) borel_measurable_less[simp, intro]:
   1.225 -  fixes f :: "'a \<Rightarrow> real"
   1.226 -  assumes f: "f \<in> borel_measurable M"
   1.227 -  assumes g: "g \<in> borel_measurable M"
   1.228 -  shows "{w \<in> space M. f w < g w} \<in> sets M"
   1.229 -proof -
   1.230 -  have "{w \<in> space M. f w < g w} =
   1.231 -        (\<Union>r. (f -` {..< of_rat r} \<inter> space M) \<inter> (g -` {of_rat r <..} \<inter> space M))"
   1.232 -    using Rats_dense_in_real by (auto simp add: Rats_def)
   1.233 -  then show ?thesis using f g
   1.234 -    by simp (blast intro: measurable_sets)
   1.235 -qed
   1.236 -
   1.237 -lemma (in sigma_algebra) borel_measurable_le[simp, intro]:
   1.238 -  fixes f :: "'a \<Rightarrow> real"
   1.239 -  assumes f: "f \<in> borel_measurable M"
   1.240 -  assumes g: "g \<in> borel_measurable M"
   1.241 -  shows "{w \<in> space M. f w \<le> g w} \<in> sets M"
   1.242 -proof -
   1.243 -  have "{w \<in> space M. f w \<le> g w} = space M - {w \<in> space M. g w < f w}"
   1.244 -    by auto
   1.245 -  thus ?thesis using f g
   1.246 -    by simp blast
   1.247 -qed
   1.248 -
   1.249 -lemma (in sigma_algebra) borel_measurable_eq[simp, intro]:
   1.250 -  fixes f :: "'a \<Rightarrow> real"
   1.251 -  assumes f: "f \<in> borel_measurable M"
   1.252 -  assumes g: "g \<in> borel_measurable M"
   1.253 -  shows "{w \<in> space M. f w = g w} \<in> sets M"
   1.254 -proof -
   1.255 -  have "{w \<in> space M. f w = g w} =
   1.256 -        {w \<in> space M. f w \<le> g w} \<inter> {w \<in> space M. g w \<le> f w}"
   1.257 -    by auto
   1.258 -  thus ?thesis using f g by auto
   1.259 -qed
   1.260 -
   1.261 -lemma (in sigma_algebra) borel_measurable_neq[simp, intro]:
   1.262 -  fixes f :: "'a \<Rightarrow> real"
   1.263 -  assumes f: "f \<in> borel_measurable M"
   1.264 -  assumes g: "g \<in> borel_measurable M"
   1.265 -  shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   1.266 -proof -
   1.267 -  have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}"
   1.268 -    by auto
   1.269 -  thus ?thesis using f g by auto
   1.270 -qed
   1.271 -
   1.272 -subsection "Borel space equals sigma algebras over intervals"
   1.273 -
   1.274 -lemma rational_boxes:
   1.275 -  fixes x :: "'a\<Colon>ordered_euclidean_space"
   1.276 -  assumes "0 < e"
   1.277 -  shows "\<exists>a b. (\<forall>i. a $$ i \<in> \<rat>) \<and> (\<forall>i. b $$ i \<in> \<rat>) \<and> x \<in> {a <..< b} \<and> {a <..< b} \<subseteq> ball x e"
   1.278 -proof -
   1.279 -  def e' \<equiv> "e / (2 * sqrt (real (DIM ('a))))"
   1.280 -  then have e: "0 < e'" using assms by (auto intro!: divide_pos_pos)
   1.281 -  have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> y < x $$ i \<and> x $$ i - y < e'" (is "\<forall>i. ?th i")
   1.282 -  proof
   1.283 -    fix i from Rats_dense_in_real[of "x $$ i - e'" "x $$ i"] e
   1.284 -    show "?th i" by auto
   1.285 -  qed
   1.286 -  from choice[OF this] guess a .. note a = this
   1.287 -  have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> x $$ i < y \<and> y - x $$ i < e'" (is "\<forall>i. ?th i")
   1.288 -  proof
   1.289 -    fix i from Rats_dense_in_real[of "x $$ i" "x $$ i + e'"] e
   1.290 -    show "?th i" by auto
   1.291 -  qed
   1.292 -  from choice[OF this] guess b .. note b = this
   1.293 -  { fix y :: 'a assume *: "Chi a < y" "y < Chi b"
   1.294 -    have "dist x y = sqrt (\<Sum>i<DIM('a). (dist (x $$ i) (y $$ i))\<twosuperior>)"
   1.295 -      unfolding setL2_def[symmetric] by (rule euclidean_dist_l2)
   1.296 -    also have "\<dots> < sqrt (\<Sum>i<DIM('a). e^2 / real (DIM('a)))"
   1.297 -    proof (rule real_sqrt_less_mono, rule setsum_strict_mono)
   1.298 -      fix i assume i: "i \<in> {..<DIM('a)}"
   1.299 -      have "a i < y$$i \<and> y$$i < b i" using * i eucl_less[where 'a='a] by auto
   1.300 -      moreover have "a i < x$$i" "x$$i - a i < e'" using a by auto
   1.301 -      moreover have "x$$i < b i" "b i - x$$i < e'" using b by auto
   1.302 -      ultimately have "\<bar>x$$i - y$$i\<bar> < 2 * e'" by auto
   1.303 -      then have "dist (x $$ i) (y $$ i) < e/sqrt (real (DIM('a)))"
   1.304 -        unfolding e'_def by (auto simp: dist_real_def)
   1.305 -      then have "(dist (x $$ i) (y $$ i))\<twosuperior> < (e/sqrt (real (DIM('a))))\<twosuperior>"
   1.306 -        by (rule power_strict_mono) auto
   1.307 -      then show "(dist (x $$ i) (y $$ i))\<twosuperior> < e\<twosuperior> / real DIM('a)"
   1.308 -        by (simp add: power_divide)
   1.309 -    qed auto
   1.310 -    also have "\<dots> = e" using `0 < e` by (simp add: real_eq_of_nat DIM_positive)
   1.311 -    finally have "dist x y < e" . }
   1.312 -  with a b show ?thesis
   1.313 -    apply (rule_tac exI[of _ "Chi a"])
   1.314 -    apply (rule_tac exI[of _ "Chi b"])
   1.315 -    using eucl_less[where 'a='a] by auto
   1.316 -qed
   1.317 -
   1.318 -lemma ex_rat_list:
   1.319 -  fixes x :: "'a\<Colon>ordered_euclidean_space"
   1.320 -  assumes "\<And> i. x $$ i \<in> \<rat>"
   1.321 -  shows "\<exists> r. length r = DIM('a) \<and> (\<forall> i < DIM('a). of_rat (r ! i) = x $$ i)"
   1.322 -proof -
   1.323 -  have "\<forall>i. \<exists>r. x $$ i = of_rat r" using assms unfolding Rats_def by blast
   1.324 -  from choice[OF this] guess r ..
   1.325 -  then show ?thesis by (auto intro!: exI[of _ "map r [0 ..< DIM('a)]"])
   1.326 -qed
   1.327 -
   1.328 -lemma open_UNION:
   1.329 -  fixes M :: "'a\<Colon>ordered_euclidean_space set"
   1.330 -  assumes "open M"
   1.331 -  shows "M = UNION {(a, b) | a b. {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)} \<subseteq> M}
   1.332 -                   (\<lambda> (a, b). {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)})"
   1.333 -    (is "M = UNION ?idx ?box")
   1.334 -proof safe
   1.335 -  fix x assume "x \<in> M"
   1.336 -  obtain e where e: "e > 0" "ball x e \<subseteq> M"
   1.337 -    using openE[OF assms `x \<in> M`] by auto
   1.338 -  then obtain a b where ab: "x \<in> {a <..< b}" "\<And>i. a $$ i \<in> \<rat>" "\<And>i. b $$ i \<in> \<rat>" "{a <..< b} \<subseteq> ball x e"
   1.339 -    using rational_boxes[OF e(1)] by blast
   1.340 -  then obtain p q where pq: "length p = DIM ('a)"
   1.341 -                            "length q = DIM ('a)"
   1.342 -                            "\<forall> i < DIM ('a). of_rat (p ! i) = a $$ i \<and> of_rat (q ! i) = b $$ i"
   1.343 -    using ex_rat_list[OF ab(2)] ex_rat_list[OF ab(3)] by blast
   1.344 -  hence p: "Chi (of_rat \<circ> op ! p) = a"
   1.345 -    using euclidean_eq[of "Chi (of_rat \<circ> op ! p)" a]
   1.346 -    unfolding o_def by auto
   1.347 -  from pq have q: "Chi (of_rat \<circ> op ! q) = b"
   1.348 -    using euclidean_eq[of "Chi (of_rat \<circ> op ! q)" b]
   1.349 -    unfolding o_def by auto
   1.350 -  have "x \<in> ?box (p, q)"
   1.351 -    using p q ab by auto
   1.352 -  thus "x \<in> UNION ?idx ?box" using ab e p q exI[of _ p] exI[of _ q] by auto
   1.353 -qed auto
   1.354 -
   1.355 -lemma halfspace_span_open:
   1.356 -  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})))
   1.357 -    \<subseteq> sets borel_space"
   1.358 -  by (auto intro!: borel_space.sigma_sets_subset[simplified] borel_space_open
   1.359 -                   open_halfspace_component_lt simp: sets_sigma)
   1.360 -
   1.361 -lemma halfspace_lt_in_halfspace:
   1.362 -  "{x\<Colon>'a. x $$ i < a} \<in> sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})))"
   1.363 -  unfolding sets_sigma by (rule sigma_sets.Basic) auto
   1.364 -
   1.365 -lemma halfspace_gt_in_halfspace:
   1.366 -  "{x\<Colon>'a. a < x $$ i} \<in> sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})))"
   1.367 -    (is "?set \<in> sets ?SIGMA")
   1.368 -proof -
   1.369 -  interpret sigma_algebra ?SIGMA by (rule sigma_algebra_sigma) simp
   1.370 -  have *: "?set = (\<Union>n. space ?SIGMA - {x\<Colon>'a. x $$ i < a + 1 / real (Suc n)})"
   1.371 -  proof (safe, simp_all add: not_less)
   1.372 -    fix x assume "a < x $$ i"
   1.373 -    with reals_Archimedean[of "x $$ i - a"]
   1.374 -    obtain n where "a + 1 / real (Suc n) < x $$ i"
   1.375 -      by (auto simp: inverse_eq_divide field_simps)
   1.376 -    then show "\<exists>n. a + 1 / real (Suc n) \<le> x $$ i"
   1.377 -      by (blast intro: less_imp_le)
   1.378 -  next
   1.379 -    fix x n
   1.380 -    have "a < a + 1 / real (Suc n)" by auto
   1.381 -    also assume "\<dots> \<le> x"
   1.382 -    finally show "a < x" .
   1.383 -  qed
   1.384 -  show "?set \<in> sets ?SIGMA" unfolding *
   1.385 -    by (safe intro!: countable_UN Diff halfspace_lt_in_halfspace)
   1.386 -qed
   1.387 -
   1.388 -lemma (in sigma_algebra) sets_sigma_subset:
   1.389 -  assumes "A = space M"
   1.390 -  assumes "B \<subseteq> sets M"
   1.391 -  shows "sets (sigma A B) \<subseteq> sets M"
   1.392 -  by (unfold assms sets_sigma, rule sigma_sets_subset, rule assms)
   1.393 -
   1.394 -lemma open_span_halfspace:
   1.395 -  "sets borel_space \<subseteq> sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x $$ i < a})))"
   1.396 -    (is "_ \<subseteq> sets ?SIGMA")
   1.397 -proof (unfold borel_space_def, rule sigma_algebra.sets_sigma_subset, safe)
   1.398 -  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) simp
   1.399 -  then interpret sigma_algebra ?SIGMA .
   1.400 -  fix S :: "'a set" assume "S \<in> open" then have "open S" unfolding mem_def .
   1.401 -  from open_UNION[OF this]
   1.402 -  obtain I where *: "S =
   1.403 -    (\<Union>(a, b)\<in>I.
   1.404 -        (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
   1.405 -        (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
   1.406 -    unfolding greaterThanLessThan_def
   1.407 -    unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
   1.408 -    unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
   1.409 -    by blast
   1.410 -  show "S \<in> sets ?SIGMA"
   1.411 -    unfolding *
   1.412 -    by (auto intro!: countable_UN Int countable_INT halfspace_lt_in_halfspace halfspace_gt_in_halfspace)
   1.413 -qed auto
   1.414 -
   1.415 -lemma halfspace_span_halfspace_le:
   1.416 -  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))) \<subseteq>
   1.417 -   sets (sigma UNIV (range (\<lambda> (a, i). {x. x $$ i \<le> a})))"
   1.418 -  (is "_ \<subseteq> sets ?SIGMA")
   1.419 -proof (rule sigma_algebra.sets_sigma_subset, safe)
   1.420 -  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   1.421 -  then interpret sigma_algebra ?SIGMA .
   1.422 -  fix a i
   1.423 -  have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
   1.424 -  proof (safe, simp_all)
   1.425 -    fix x::'a assume *: "x$$i < a"
   1.426 -    with reals_Archimedean[of "a - x$$i"]
   1.427 -    obtain n where "x $$ i < a - 1 / (real (Suc n))"
   1.428 -      by (auto simp: field_simps inverse_eq_divide)
   1.429 -    then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
   1.430 -      by (blast intro: less_imp_le)
   1.431 -  next
   1.432 -    fix x::'a and n
   1.433 -    assume "x$$i \<le> a - 1 / real (Suc n)"
   1.434 -    also have "\<dots> < a" by auto
   1.435 -    finally show "x$$i < a" .
   1.436 -  qed
   1.437 -  show "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   1.438 -    by (safe intro!: countable_UN)
   1.439 -       (auto simp: sets_sigma intro!: sigma_sets.Basic)
   1.440 -qed auto
   1.441 -
   1.442 -lemma halfspace_span_halfspace_ge:
   1.443 -  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))) \<subseteq> 
   1.444 -   sets (sigma UNIV (range (\<lambda> (a, i). {x. a \<le> x $$ i})))"
   1.445 -  (is "_ \<subseteq> sets ?SIGMA")
   1.446 -proof (rule sigma_algebra.sets_sigma_subset, safe)
   1.447 -  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   1.448 -  then interpret sigma_algebra ?SIGMA .
   1.449 -  fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
   1.450 -  show "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   1.451 -    by (safe intro!: Diff)
   1.452 -       (auto simp: sets_sigma intro!: sigma_sets.Basic)
   1.453 -qed auto
   1.454 -
   1.455 -lemma halfspace_le_span_halfspace_gt:
   1.456 -  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a}))) \<subseteq> 
   1.457 -   sets (sigma UNIV (range (\<lambda> (a, i). {x. a < x $$ i})))"
   1.458 -  (is "_ \<subseteq> sets ?SIGMA")
   1.459 -proof (rule sigma_algebra.sets_sigma_subset, safe)
   1.460 -  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   1.461 -  then interpret sigma_algebra ?SIGMA .
   1.462 -  fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   1.463 -  show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   1.464 -    by (safe intro!: Diff)
   1.465 -       (auto simp: sets_sigma intro!: sigma_sets.Basic)
   1.466 -qed auto
   1.467 -
   1.468 -lemma halfspace_le_span_atMost:
   1.469 -  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a}))) \<subseteq>
   1.470 -   sets (sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})))"
   1.471 -  (is "_ \<subseteq> sets ?SIGMA")
   1.472 -proof (rule sigma_algebra.sets_sigma_subset, safe)
   1.473 -  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   1.474 -  then interpret sigma_algebra ?SIGMA .
   1.475 -  fix a i
   1.476 -  show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   1.477 -  proof cases
   1.478 -    assume "i < DIM('a)"
   1.479 -    then have *: "{x::'a. x$$i \<le> a} = (\<Union>k::nat. {.. (\<chi>\<chi> n. if n = i then a else real k)})"
   1.480 -    proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   1.481 -      fix x
   1.482 -      from real_arch_simple[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"] guess k::nat ..
   1.483 -      then have "\<And>i. i < DIM('a) \<Longrightarrow> x$$i \<le> real k"
   1.484 -        by (subst (asm) Max_le_iff) auto
   1.485 -      then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia \<le> real k"
   1.486 -        by (auto intro!: exI[of _ k])
   1.487 -    qed
   1.488 -    show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   1.489 -      by (safe intro!: countable_UN)
   1.490 -         (auto simp: sets_sigma intro!: sigma_sets.Basic)
   1.491 -  next
   1.492 -    assume "\<not> i < DIM('a)"
   1.493 -    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   1.494 -      using top by auto
   1.495 -  qed
   1.496 -qed auto
   1.497 -
   1.498 -lemma halfspace_le_span_greaterThan:
   1.499 -  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a}))) \<subseteq>
   1.500 -   sets (sigma UNIV (range (\<lambda>a. {a<..})))"
   1.501 -  (is "_ \<subseteq> sets ?SIGMA")
   1.502 -proof (rule sigma_algebra.sets_sigma_subset, safe)
   1.503 -  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   1.504 -  then interpret sigma_algebra ?SIGMA .
   1.505 -  fix a i
   1.506 -  show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   1.507 -  proof cases
   1.508 -    assume "i < DIM('a)"
   1.509 -    have "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   1.510 -    also have *: "{x::'a. a < x$$i} = (\<Union>k::nat. {(\<chi>\<chi> n. if n = i then a else -real k) <..})" using `i <DIM('a)`
   1.511 -    proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   1.512 -      fix x
   1.513 -      from real_arch_lt[of "Max ((\<lambda>i. -x$$i)`{..<DIM('a)})"]
   1.514 -      guess k::nat .. note k = this
   1.515 -      { fix i assume "i < DIM('a)"
   1.516 -        then have "-x$$i < real k"
   1.517 -          using k by (subst (asm) Max_less_iff) auto
   1.518 -        then have "- real k < x$$i" by simp }
   1.519 -      then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> -real k < x $$ ia"
   1.520 -        by (auto intro!: exI[of _ k])
   1.521 -    qed
   1.522 -    finally show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   1.523 -      apply (simp only:)
   1.524 -      apply (safe intro!: countable_UN Diff)
   1.525 -      by (auto simp: sets_sigma intro!: sigma_sets.Basic)
   1.526 -  next
   1.527 -    assume "\<not> i < DIM('a)"
   1.528 -    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   1.529 -      using top by auto
   1.530 -  qed
   1.531 -qed auto
   1.532 -
   1.533 -lemma atMost_span_atLeastAtMost:
   1.534 -  "sets (sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space}))) \<subseteq>
   1.535 -   sets (sigma UNIV (range (\<lambda>(a,b). {a..b})))"
   1.536 -  (is "_ \<subseteq> sets ?SIGMA")
   1.537 -proof (rule sigma_algebra.sets_sigma_subset, safe)
   1.538 -  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   1.539 -  then interpret sigma_algebra ?SIGMA .
   1.540 -  fix a::'a
   1.541 -  have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   1.542 -  proof (safe, simp_all add: eucl_le[where 'a='a])
   1.543 -    fix x
   1.544 -    from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
   1.545 -    guess k::nat .. note k = this
   1.546 -    { fix i assume "i < DIM('a)"
   1.547 -      with k have "- x$$i \<le> real k"
   1.548 -        by (subst (asm) Max_le_iff) (auto simp: field_simps)
   1.549 -      then have "- real k \<le> x$$i" by simp }
   1.550 -    then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
   1.551 -      by (auto intro!: exI[of _ k])
   1.552 -  qed
   1.553 -  show "{..a} \<in> sets ?SIGMA" unfolding *
   1.554 -    by (safe intro!: countable_UN)
   1.555 -       (auto simp: sets_sigma intro!: sigma_sets.Basic)
   1.556 -qed auto
   1.557 -
   1.558 -lemma borel_space_eq_greaterThanLessThan:
   1.559 -  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, b). {a <..< (b :: 'a \<Colon> ordered_euclidean_space)})))"
   1.560 -    (is "_ = sets ?SIGMA")
   1.561 -proof (rule antisym)
   1.562 -  show "sets ?SIGMA \<subseteq> sets borel_space"
   1.563 -    by (rule borel_space.sets_sigma_subset) auto
   1.564 -  show "sets borel_space \<subseteq> sets ?SIGMA"
   1.565 -    unfolding borel_space_def
   1.566 -  proof (rule sigma_algebra.sets_sigma_subset, safe)
   1.567 -    show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   1.568 -    then interpret sigma_algebra ?SIGMA .
   1.569 -    fix M :: "'a set" assume "M \<in> open"
   1.570 -    then have "open M" by (simp add: mem_def)
   1.571 -    show "M \<in> sets ?SIGMA"
   1.572 -      apply (subst open_UNION[OF `open M`])
   1.573 -      apply (safe intro!: countable_UN)
   1.574 -      by (auto simp add: sigma_def intro!: sigma_sets.Basic)
   1.575 -  qed auto
   1.576 -qed
   1.577 -
   1.578 -lemma borel_space_eq_atMost:
   1.579 -  "sets borel_space = sets (sigma UNIV (range (\<lambda> a. {.. a::'a\<Colon>ordered_euclidean_space})))"
   1.580 -    (is "_ = sets ?SIGMA")
   1.581 -proof (rule antisym)
   1.582 -  show "sets borel_space \<subseteq> sets ?SIGMA"
   1.583 -    using halfspace_le_span_atMost halfspace_span_halfspace_le open_span_halfspace
   1.584 -    by auto
   1.585 -  show "sets ?SIGMA \<subseteq> sets borel_space"
   1.586 -    by (rule borel_space.sets_sigma_subset) auto
   1.587 -qed
   1.588 -
   1.589 -lemma borel_space_eq_atLeastAtMost:
   1.590 -  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space, b). {a .. b})))"
   1.591 -   (is "_ = sets ?SIGMA")
   1.592 -proof (rule antisym)
   1.593 -  show "sets borel_space \<subseteq> sets ?SIGMA"
   1.594 -    using atMost_span_atLeastAtMost halfspace_le_span_atMost
   1.595 -      halfspace_span_halfspace_le open_span_halfspace
   1.596 -    by auto
   1.597 -  show "sets ?SIGMA \<subseteq> sets borel_space"
   1.598 -    by (rule borel_space.sets_sigma_subset) auto
   1.599 -qed
   1.600 -
   1.601 -lemma borel_space_eq_greaterThan:
   1.602 -  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {a <..})))"
   1.603 -   (is "_ = sets ?SIGMA")
   1.604 -proof (rule antisym)
   1.605 -  show "sets borel_space \<subseteq> sets ?SIGMA"
   1.606 -    using halfspace_le_span_greaterThan
   1.607 -      halfspace_span_halfspace_le open_span_halfspace
   1.608 -    by auto
   1.609 -  show "sets ?SIGMA \<subseteq> sets borel_space"
   1.610 -    by (rule borel_space.sets_sigma_subset) auto
   1.611 -qed
   1.612 -
   1.613 -lemma borel_space_eq_halfspace_le:
   1.614 -  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i \<le> a})))"
   1.615 -   (is "_ = sets ?SIGMA")
   1.616 -proof (rule antisym)
   1.617 -  show "sets borel_space \<subseteq> sets ?SIGMA"
   1.618 -    using open_span_halfspace halfspace_span_halfspace_le by auto
   1.619 -  show "sets ?SIGMA \<subseteq> sets borel_space"
   1.620 -    by (rule borel_space.sets_sigma_subset) auto
   1.621 -qed
   1.622 -
   1.623 -lemma borel_space_eq_halfspace_less:
   1.624 -  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i < a})))"
   1.625 -   (is "_ = sets ?SIGMA")
   1.626 -proof (rule antisym)
   1.627 -  show "sets borel_space \<subseteq> sets ?SIGMA"
   1.628 -    using open_span_halfspace .
   1.629 -  show "sets ?SIGMA \<subseteq> sets borel_space"
   1.630 -    by (rule borel_space.sets_sigma_subset) auto
   1.631 -qed
   1.632 -
   1.633 -lemma borel_space_eq_halfspace_gt:
   1.634 -  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a < x$$i})))"
   1.635 -   (is "_ = sets ?SIGMA")
   1.636 -proof (rule antisym)
   1.637 -  show "sets borel_space \<subseteq> sets ?SIGMA"
   1.638 -    using halfspace_le_span_halfspace_gt open_span_halfspace halfspace_span_halfspace_le by auto
   1.639 -  show "sets ?SIGMA \<subseteq> sets borel_space"
   1.640 -    by (rule borel_space.sets_sigma_subset) auto
   1.641 -qed
   1.642 -
   1.643 -lemma borel_space_eq_halfspace_ge:
   1.644 -  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a \<le> x$$i})))"
   1.645 -   (is "_ = sets ?SIGMA")
   1.646 -proof (rule antisym)
   1.647 -  show "sets borel_space \<subseteq> sets ?SIGMA"
   1.648 -    using halfspace_span_halfspace_ge open_span_halfspace by auto
   1.649 -  show "sets ?SIGMA \<subseteq> sets borel_space"
   1.650 -    by (rule borel_space.sets_sigma_subset) auto
   1.651 -qed
   1.652 -
   1.653 -lemma (in sigma_algebra) borel_measurable_halfspacesI:
   1.654 -  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   1.655 -  assumes "sets borel_space = sets (sigma UNIV (range F))"
   1.656 -  and "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
   1.657 -  and "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
   1.658 -  shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a::real. S a i \<in> sets M)"
   1.659 -proof safe
   1.660 -  fix a :: real and i assume i: "i < DIM('c)" and f: "f \<in> borel_measurable M"
   1.661 -  then show "S a i \<in> sets M" unfolding assms
   1.662 -    by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1) sigma_def)
   1.663 -next
   1.664 -  assume a: "\<forall>i<DIM('c). \<forall>a. S a i \<in> sets M"
   1.665 -  { fix a i have "S a i \<in> sets M"
   1.666 -    proof cases
   1.667 -      assume "i < DIM('c)"
   1.668 -      with a show ?thesis unfolding assms(2) by simp
   1.669 -    next
   1.670 -      assume "\<not> i < DIM('c)"
   1.671 -      from assms(3)[OF this] show ?thesis .
   1.672 -    qed }
   1.673 -  then have "f \<in> measurable M (sigma UNIV (range F))"
   1.674 -    by (auto intro!: measurable_sigma simp: assms(2))
   1.675 -  then show "f \<in> borel_measurable M" unfolding measurable_def
   1.676 -    unfolding assms(1) by simp
   1.677 -qed
   1.678 -
   1.679 -lemma (in sigma_algebra) borel_measurable_iff_halfspace_le:
   1.680 -  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   1.681 -  shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i \<le> a} \<in> sets M)"
   1.682 -  by (rule borel_measurable_halfspacesI[OF borel_space_eq_halfspace_le]) auto
   1.683 -
   1.684 -lemma (in sigma_algebra) borel_measurable_iff_halfspace_less:
   1.685 -  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   1.686 -  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i < a} \<in> sets M)"
   1.687 -  by (rule borel_measurable_halfspacesI[OF borel_space_eq_halfspace_less]) auto
   1.688 -
   1.689 -lemma (in sigma_algebra) borel_measurable_iff_halfspace_ge:
   1.690 -  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   1.691 -  shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a \<le> f w $$ i} \<in> sets M)"
   1.692 -  by (rule borel_measurable_halfspacesI[OF borel_space_eq_halfspace_ge]) auto
   1.693 -
   1.694 -lemma (in sigma_algebra) borel_measurable_iff_halfspace_greater:
   1.695 -  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   1.696 -  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a < f w $$ i} \<in> sets M)"
   1.697 -  by (rule borel_measurable_halfspacesI[OF borel_space_eq_halfspace_gt]) auto
   1.698 -
   1.699 -lemma (in sigma_algebra) borel_measurable_iff_le:
   1.700 -  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   1.701 -  using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   1.702 -
   1.703 -lemma (in sigma_algebra) borel_measurable_iff_less:
   1.704 -  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   1.705 -  using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   1.706 -
   1.707 -lemma (in sigma_algebra) borel_measurable_iff_ge:
   1.708 -  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   1.709 -  using borel_measurable_iff_halfspace_ge[where 'c=real] by simp
   1.710 -
   1.711 -lemma (in sigma_algebra) borel_measurable_iff_greater:
   1.712 -  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   1.713 -  using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   1.714 -
   1.715 -lemma borel_measureable_euclidean_component:
   1.716 -  "(\<lambda>x::'a::euclidean_space. x $$ i) \<in> borel_measurable borel_space"
   1.717 -  unfolding borel_space_def[where 'a=real]
   1.718 -proof (rule borel_space.measurable_sigma)
   1.719 -  fix S::"real set" assume "S \<in> open" then have "open S" unfolding mem_def .
   1.720 -  from open_vimage_euclidean_component[OF this]
   1.721 -  show "(\<lambda>x. x $$ i) -` S \<inter> space borel_space \<in> sets borel_space"
   1.722 -    by (auto intro: borel_space_open)
   1.723 -qed auto
   1.724 -
   1.725 -lemma (in sigma_algebra) borel_measureable_euclidean_space:
   1.726 -  fixes f :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   1.727 -  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M)"
   1.728 -proof safe
   1.729 -  fix i assume "f \<in> borel_measurable M"
   1.730 -  then show "(\<lambda>x. f x $$ i) \<in> borel_measurable M"
   1.731 -    using measurable_comp[of f _ _ "\<lambda>x. x $$ i", unfolded comp_def]
   1.732 -    by (auto intro: borel_measureable_euclidean_component)
   1.733 -next
   1.734 -  assume f: "\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M"
   1.735 -  then show "f \<in> borel_measurable M"
   1.736 -    unfolding borel_measurable_iff_halfspace_le by auto
   1.737 -qed
   1.738 -
   1.739 -subsection "Borel measurable operators"
   1.740 -
   1.741 -lemma (in sigma_algebra) affine_borel_measurable_vector:
   1.742 -  fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   1.743 -  assumes "f \<in> borel_measurable M"
   1.744 -  shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   1.745 -proof (rule borel_measurableI)
   1.746 -  fix S :: "'x set" assume "open S"
   1.747 -  show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   1.748 -  proof cases
   1.749 -    assume "b \<noteq> 0"
   1.750 -    with `open S` have "((\<lambda>x. (- a + x) /\<^sub>R b) ` S) \<in> open" (is "?S \<in> open")
   1.751 -      by (auto intro!: open_affinity simp: scaleR.add_right mem_def)
   1.752 -    hence "?S \<in> sets borel_space"
   1.753 -      unfolding borel_space_def by (auto simp: sigma_def intro!: sigma_sets.Basic)
   1.754 -    moreover
   1.755 -    from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   1.756 -      apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   1.757 -    ultimately show ?thesis using assms unfolding in_borel_measurable_borel_space
   1.758 -      by auto
   1.759 -  qed simp
   1.760 -qed
   1.761 -
   1.762 -lemma (in sigma_algebra) affine_borel_measurable:
   1.763 -  fixes g :: "'a \<Rightarrow> real"
   1.764 -  assumes g: "g \<in> borel_measurable M"
   1.765 -  shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
   1.766 -  using affine_borel_measurable_vector[OF assms] by (simp add: mult_commute)
   1.767 -
   1.768 -lemma (in sigma_algebra) borel_measurable_add[simp, intro]:
   1.769 -  fixes f :: "'a \<Rightarrow> real"
   1.770 -  assumes f: "f \<in> borel_measurable M"
   1.771 -  assumes g: "g \<in> borel_measurable M"
   1.772 -  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   1.773 -proof -
   1.774 -  have 1: "\<And>a. {w\<in>space M. a \<le> f w + g w} = {w \<in> space M. a + g w * -1 \<le> f w}"
   1.775 -    by auto
   1.776 -  have "\<And>a. (\<lambda>w. a + (g w) * -1) \<in> borel_measurable M"
   1.777 -    by (rule affine_borel_measurable [OF g])
   1.778 -  then have "\<And>a. {w \<in> space M. (\<lambda>w. a + (g w) * -1)(w) \<le> f w} \<in> sets M" using f
   1.779 -    by auto
   1.780 -  then have "\<And>a. {w \<in> space M. a \<le> f w + g w} \<in> sets M"
   1.781 -    by (simp add: 1)
   1.782 -  then show ?thesis
   1.783 -    by (simp add: borel_measurable_iff_ge)
   1.784 -qed
   1.785 -
   1.786 -lemma (in sigma_algebra) borel_measurable_square:
   1.787 -  fixes f :: "'a \<Rightarrow> real"
   1.788 -  assumes f: "f \<in> borel_measurable M"
   1.789 -  shows "(\<lambda>x. (f x)^2) \<in> borel_measurable M"
   1.790 -proof -
   1.791 -  {
   1.792 -    fix a
   1.793 -    have "{w \<in> space M. (f w)\<twosuperior> \<le> a} \<in> sets M"
   1.794 -    proof (cases rule: linorder_cases [of a 0])
   1.795 -      case less
   1.796 -      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} = {}"
   1.797 -        by auto (metis less order_le_less_trans power2_less_0)
   1.798 -      also have "... \<in> sets M"
   1.799 -        by (rule empty_sets)
   1.800 -      finally show ?thesis .
   1.801 -    next
   1.802 -      case equal
   1.803 -      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   1.804 -             {w \<in> space M. f w \<le> 0} \<inter> {w \<in> space M. 0 \<le> f w}"
   1.805 -        by auto
   1.806 -      also have "... \<in> sets M"
   1.807 -        apply (insert f)
   1.808 -        apply (rule Int)
   1.809 -        apply (simp add: borel_measurable_iff_le)
   1.810 -        apply (simp add: borel_measurable_iff_ge)
   1.811 -        done
   1.812 -      finally show ?thesis .
   1.813 -    next
   1.814 -      case greater
   1.815 -      have "\<forall>x. (f x ^ 2 \<le> sqrt a ^ 2) = (- sqrt a  \<le> f x & f x \<le> sqrt a)"
   1.816 -        by (metis abs_le_interval_iff abs_of_pos greater real_sqrt_abs
   1.817 -                  real_sqrt_le_iff real_sqrt_power)
   1.818 -      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   1.819 -             {w \<in> space M. -(sqrt a) \<le> f w} \<inter> {w \<in> space M. f w \<le> sqrt a}"
   1.820 -        using greater by auto
   1.821 -      also have "... \<in> sets M"
   1.822 -        apply (insert f)
   1.823 -        apply (rule Int)
   1.824 -        apply (simp add: borel_measurable_iff_ge)
   1.825 -        apply (simp add: borel_measurable_iff_le)
   1.826 -        done
   1.827 -      finally show ?thesis .
   1.828 -    qed
   1.829 -  }
   1.830 -  thus ?thesis by (auto simp add: borel_measurable_iff_le)
   1.831 -qed
   1.832 -
   1.833 -lemma times_eq_sum_squares:
   1.834 -   fixes x::real
   1.835 -   shows"x*y = ((x+y)^2)/4 - ((x-y)^ 2)/4"
   1.836 -by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric])
   1.837 -
   1.838 -lemma (in sigma_algebra) borel_measurable_uminus[simp, intro]:
   1.839 -  fixes g :: "'a \<Rightarrow> real"
   1.840 -  assumes g: "g \<in> borel_measurable M"
   1.841 -  shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   1.842 -proof -
   1.843 -  have "(\<lambda>x. - g x) = (\<lambda>x. 0 + (g x) * -1)"
   1.844 -    by simp
   1.845 -  also have "... \<in> borel_measurable M"
   1.846 -    by (fast intro: affine_borel_measurable g)
   1.847 -  finally show ?thesis .
   1.848 -qed
   1.849 -
   1.850 -lemma (in sigma_algebra) borel_measurable_times[simp, intro]:
   1.851 -  fixes f :: "'a \<Rightarrow> real"
   1.852 -  assumes f: "f \<in> borel_measurable M"
   1.853 -  assumes g: "g \<in> borel_measurable M"
   1.854 -  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   1.855 -proof -
   1.856 -  have 1: "(\<lambda>x. 0 + (f x + g x)\<twosuperior> * inverse 4) \<in> borel_measurable M"
   1.857 -    using assms by (fast intro: affine_borel_measurable borel_measurable_square)
   1.858 -  have "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) =
   1.859 -        (\<lambda>x. 0 + ((f x + -g x) ^ 2 * inverse -4))"
   1.860 -    by (simp add: minus_divide_right)
   1.861 -  also have "... \<in> borel_measurable M"
   1.862 -    using f g by (fast intro: affine_borel_measurable borel_measurable_square f g)
   1.863 -  finally have 2: "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) \<in> borel_measurable M" .
   1.864 -  show ?thesis
   1.865 -    apply (simp add: times_eq_sum_squares diff_minus)
   1.866 -    using 1 2 by simp
   1.867 -qed
   1.868 -
   1.869 -lemma (in sigma_algebra) borel_measurable_diff[simp, intro]:
   1.870 -  fixes f :: "'a \<Rightarrow> real"
   1.871 -  assumes f: "f \<in> borel_measurable M"
   1.872 -  assumes g: "g \<in> borel_measurable M"
   1.873 -  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   1.874 -  unfolding diff_minus using assms by fast
   1.875 -
   1.876 -lemma (in sigma_algebra) borel_measurable_setsum[simp, intro]:
   1.877 -  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   1.878 -  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   1.879 -  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   1.880 -proof cases
   1.881 -  assume "finite S"
   1.882 -  thus ?thesis using assms by induct auto
   1.883 -qed simp
   1.884 -
   1.885 -lemma (in sigma_algebra) borel_measurable_inverse[simp, intro]:
   1.886 -  fixes f :: "'a \<Rightarrow> real"
   1.887 -  assumes "f \<in> borel_measurable M"
   1.888 -  shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   1.889 -  unfolding borel_measurable_iff_ge unfolding inverse_eq_divide
   1.890 -proof safe
   1.891 -  fix a :: real
   1.892 -  have *: "{w \<in> space M. a \<le> 1 / f w} =
   1.893 -      ({w \<in> space M. 0 < f w} \<inter> {w \<in> space M. a * f w \<le> 1}) \<union>
   1.894 -      ({w \<in> space M. f w < 0} \<inter> {w \<in> space M. 1 \<le> a * f w}) \<union>
   1.895 -      ({w \<in> space M. f w = 0} \<inter> {w \<in> space M. a \<le> 0})" by (auto simp: le_divide_eq)
   1.896 -  show "{w \<in> space M. a \<le> 1 / f w} \<in> sets M" using assms unfolding *
   1.897 -    by (auto intro!: Int Un)
   1.898 -qed
   1.899 -
   1.900 -lemma (in sigma_algebra) borel_measurable_divide[simp, intro]:
   1.901 -  fixes f :: "'a \<Rightarrow> real"
   1.902 -  assumes "f \<in> borel_measurable M"
   1.903 -  and "g \<in> borel_measurable M"
   1.904 -  shows "(\<lambda>x. f x / g x) \<in> borel_measurable M"
   1.905 -  unfolding field_divide_inverse
   1.906 -  by (rule borel_measurable_inverse borel_measurable_times assms)+
   1.907 -
   1.908 -lemma (in sigma_algebra) borel_measurable_max[intro, simp]:
   1.909 -  fixes f g :: "'a \<Rightarrow> real"
   1.910 -  assumes "f \<in> borel_measurable M"
   1.911 -  assumes "g \<in> borel_measurable M"
   1.912 -  shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   1.913 -  unfolding borel_measurable_iff_le
   1.914 -proof safe
   1.915 -  fix a
   1.916 -  have "{x \<in> space M. max (g x) (f x) \<le> a} =
   1.917 -    {x \<in> space M. g x \<le> a} \<inter> {x \<in> space M. f x \<le> a}" by auto
   1.918 -  thus "{x \<in> space M. max (g x) (f x) \<le> a} \<in> sets M"
   1.919 -    using assms unfolding borel_measurable_iff_le
   1.920 -    by (auto intro!: Int)
   1.921 -qed
   1.922 -
   1.923 -lemma (in sigma_algebra) borel_measurable_min[intro, simp]:
   1.924 -  fixes f g :: "'a \<Rightarrow> real"
   1.925 -  assumes "f \<in> borel_measurable M"
   1.926 -  assumes "g \<in> borel_measurable M"
   1.927 -  shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
   1.928 -  unfolding borel_measurable_iff_ge
   1.929 -proof safe
   1.930 -  fix a
   1.931 -  have "{x \<in> space M. a \<le> min (g x) (f x)} =
   1.932 -    {x \<in> space M. a \<le> g x} \<inter> {x \<in> space M. a \<le> f x}" by auto
   1.933 -  thus "{x \<in> space M. a \<le> min (g x) (f x)} \<in> sets M"
   1.934 -    using assms unfolding borel_measurable_iff_ge
   1.935 -    by (auto intro!: Int)
   1.936 -qed
   1.937 -
   1.938 -lemma (in sigma_algebra) borel_measurable_abs[simp, intro]:
   1.939 -  assumes "f \<in> borel_measurable M"
   1.940 -  shows "(\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
   1.941 -proof -
   1.942 -  have *: "\<And>x. \<bar>f x\<bar> = max 0 (f x) + max 0 (- f x)" by (simp add: max_def)
   1.943 -  show ?thesis unfolding * using assms by auto
   1.944 -qed
   1.945 -
   1.946 -section "Borel space over the real line with infinity"
   1.947 -
   1.948 -lemma borel_space_Real_measurable:
   1.949 -  "A \<in> sets borel_space \<Longrightarrow> Real -` A \<in> sets borel_space"
   1.950 -proof (rule borel_measurable_translate)
   1.951 -  fix B :: "pinfreal set" assume "open B"
   1.952 -  then obtain T x where T: "open T" "Real ` (T \<inter> {0..}) = B - {\<omega>}" and
   1.953 -    x: "\<omega> \<in> B \<Longrightarrow> 0 \<le> x" "\<omega> \<in> B \<Longrightarrow> {Real x <..} \<subseteq> B"
   1.954 -    unfolding open_pinfreal_def by blast
   1.955 -
   1.956 -  have "Real -` B = Real -` (B - {\<omega>})" by auto
   1.957 -  also have "\<dots> = Real -` (Real ` (T \<inter> {0..}))" using T by simp
   1.958 -  also have "\<dots> = (if 0 \<in> T then T \<union> {.. 0} else T \<inter> {0..})"
   1.959 -    apply (auto simp add: Real_eq_Real image_iff)
   1.960 -    apply (rule_tac x="max 0 x" in bexI)
   1.961 -    by (auto simp: max_def)
   1.962 -  finally show "Real -` B \<in> sets borel_space"
   1.963 -    using `open T` by auto
   1.964 -qed simp
   1.965 -
   1.966 -lemma borel_space_real_measurable:
   1.967 -  "A \<in> sets borel_space \<Longrightarrow> (real -` A :: pinfreal set) \<in> sets borel_space"
   1.968 -proof (rule borel_measurable_translate)
   1.969 -  fix B :: "real set" assume "open B"
   1.970 -  { fix x have "0 < real x \<longleftrightarrow> (\<exists>r>0. x = Real r)" by (cases x) auto }
   1.971 -  note Ex_less_real = this
   1.972 -  have *: "real -` B = (if 0 \<in> B then real -` (B \<inter> {0 <..}) \<union> {0, \<omega>} else real -` (B \<inter> {0 <..}))"
   1.973 -    by (force simp: Ex_less_real)
   1.974 -
   1.975 -  have "open (real -` (B \<inter> {0 <..}) :: pinfreal set)"
   1.976 -    unfolding open_pinfreal_def using `open B`
   1.977 -    by (auto intro!: open_Int exI[of _ "B \<inter> {0 <..}"] simp: image_iff Ex_less_real)
   1.978 -  then show "(real -` B :: pinfreal set) \<in> sets borel_space" unfolding * by auto
   1.979 -qed simp
   1.980 -
   1.981 -lemma (in sigma_algebra) borel_measurable_Real[intro, simp]:
   1.982 -  assumes "f \<in> borel_measurable M"
   1.983 -  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
   1.984 -  unfolding in_borel_measurable_borel_space
   1.985 -proof safe
   1.986 -  fix S :: "pinfreal set" assume "S \<in> sets borel_space"
   1.987 -  from borel_space_Real_measurable[OF this]
   1.988 -  have "(Real \<circ> f) -` S \<inter> space M \<in> sets M"
   1.989 -    using assms
   1.990 -    unfolding vimage_compose in_borel_measurable_borel_space
   1.991 -    by auto
   1.992 -  thus "(\<lambda>x. Real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
   1.993 -qed
   1.994 -
   1.995 -lemma (in sigma_algebra) borel_measurable_real[intro, simp]:
   1.996 -  fixes f :: "'a \<Rightarrow> pinfreal"
   1.997 -  assumes "f \<in> borel_measurable M"
   1.998 -  shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
   1.999 -  unfolding in_borel_measurable_borel_space
  1.1000 -proof safe
  1.1001 -  fix S :: "real set" assume "S \<in> sets borel_space"
  1.1002 -  from borel_space_real_measurable[OF this]
  1.1003 -  have "(real \<circ> f) -` S \<inter> space M \<in> sets M"
  1.1004 -    using assms
  1.1005 -    unfolding vimage_compose in_borel_measurable_borel_space
  1.1006 -    by auto
  1.1007 -  thus "(\<lambda>x. real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
  1.1008 -qed
  1.1009 -
  1.1010 -lemma (in sigma_algebra) borel_measurable_Real_eq:
  1.1011 -  assumes "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1.1012 -  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  1.1013 -proof
  1.1014 -  have [simp]: "(\<lambda>x. Real (f x)) -` {\<omega>} \<inter> space M = {}"
  1.1015 -    by auto
  1.1016 -  assume "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
  1.1017 -  hence "(\<lambda>x. real (Real (f x))) \<in> borel_measurable M"
  1.1018 -    by (rule borel_measurable_real)
  1.1019 -  moreover have "\<And>x. x \<in> space M \<Longrightarrow> real (Real (f x)) = f x"
  1.1020 -    using assms by auto
  1.1021 -  ultimately show "f \<in> borel_measurable M"
  1.1022 -    by (simp cong: measurable_cong)
  1.1023 -qed auto
  1.1024 -
  1.1025 -lemma (in sigma_algebra) borel_measurable_pinfreal_eq_real:
  1.1026 -  "f \<in> borel_measurable M \<longleftrightarrow>
  1.1027 -    ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<omega>} \<inter> space M \<in> sets M)"
  1.1028 -proof safe
  1.1029 -  assume "f \<in> borel_measurable M"
  1.1030 -  then show "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
  1.1031 -    by (auto intro: borel_measurable_vimage borel_measurable_real)
  1.1032 -next
  1.1033 -  assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
  1.1034 -  have "f -` {\<omega>} \<inter> space M = {x\<in>space M. f x = \<omega>}" by auto
  1.1035 -  with * have **: "{x\<in>space M. f x = \<omega>} \<in> sets M" by simp
  1.1036 -  have f: "f = (\<lambda>x. if f x = \<omega> then \<omega> else Real (real (f x)))"
  1.1037 -    by (simp add: fun_eq_iff Real_real)
  1.1038 -  show "f \<in> borel_measurable M"
  1.1039 -    apply (subst f)
  1.1040 -    apply (rule measurable_If)
  1.1041 -    using * ** by auto
  1.1042 -qed
  1.1043 -
  1.1044 -lemma (in sigma_algebra) less_eq_ge_measurable:
  1.1045 -  fixes f :: "'a \<Rightarrow> 'c::linorder"
  1.1046 -  shows "{x\<in>space M. a < f x} \<in> sets M \<longleftrightarrow> {x\<in>space M. f x \<le> a} \<in> sets M"
  1.1047 -proof
  1.1048 -  assume "{x\<in>space M. f x \<le> a} \<in> sets M"
  1.1049 -  moreover have "{x\<in>space M. a < f x} = space M - {x\<in>space M. f x \<le> a}" by auto
  1.1050 -  ultimately show "{x\<in>space M. a < f x} \<in> sets M" by auto
  1.1051 -next
  1.1052 -  assume "{x\<in>space M. a < f x} \<in> sets M"
  1.1053 -  moreover have "{x\<in>space M. f x \<le> a} = space M - {x\<in>space M. a < f x}" by auto
  1.1054 -  ultimately show "{x\<in>space M. f x \<le> a} \<in> sets M" by auto
  1.1055 -qed
  1.1056 -
  1.1057 -lemma (in sigma_algebra) greater_eq_le_measurable:
  1.1058 -  fixes f :: "'a \<Rightarrow> 'c::linorder"
  1.1059 -  shows "{x\<in>space M. f x < a} \<in> sets M \<longleftrightarrow> {x\<in>space M. a \<le> f x} \<in> sets M"
  1.1060 -proof
  1.1061 -  assume "{x\<in>space M. a \<le> f x} \<in> sets M"
  1.1062 -  moreover have "{x\<in>space M. f x < a} = space M - {x\<in>space M. a \<le> f x}" by auto
  1.1063 -  ultimately show "{x\<in>space M. f x < a} \<in> sets M" by auto
  1.1064 -next
  1.1065 -  assume "{x\<in>space M. f x < a} \<in> sets M"
  1.1066 -  moreover have "{x\<in>space M. a \<le> f x} = space M - {x\<in>space M. f x < a}" by auto
  1.1067 -  ultimately show "{x\<in>space M. a \<le> f x} \<in> sets M" by auto
  1.1068 -qed
  1.1069 -
  1.1070 -lemma (in sigma_algebra) less_eq_le_pinfreal_measurable:
  1.1071 -  fixes f :: "'a \<Rightarrow> pinfreal"
  1.1072 -  shows "(\<forall>a. {x\<in>space M. a < f x} \<in> sets M) \<longleftrightarrow> (\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M)"
  1.1073 -proof
  1.1074 -  assume a: "\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M"
  1.1075 -  show "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
  1.1076 -  proof
  1.1077 -    fix a show "{x \<in> space M. a < f x} \<in> sets M"
  1.1078 -    proof (cases a)
  1.1079 -      case (preal r)
  1.1080 -      have "{x\<in>space M. a < f x} = (\<Union>i. {x\<in>space M. a + inverse (of_nat (Suc i)) \<le> f x})"
  1.1081 -      proof safe
  1.1082 -        fix x assume "a < f x" and [simp]: "x \<in> space M"
  1.1083 -        with ex_pinfreal_inverse_of_nat_Suc_less[of "f x - a"]
  1.1084 -        obtain n where "a + inverse (of_nat (Suc n)) < f x"
  1.1085 -          by (cases "f x", auto simp: pinfreal_minus_order)
  1.1086 -        then have "a + inverse (of_nat (Suc n)) \<le> f x" by simp
  1.1087 -        then show "x \<in> (\<Union>i. {x \<in> space M. a + inverse (of_nat (Suc i)) \<le> f x})"
  1.1088 -          by auto
  1.1089 -      next
  1.1090 -        fix i x assume [simp]: "x \<in> space M"
  1.1091 -        have "a < a + inverse (of_nat (Suc i))" using preal by auto
  1.1092 -        also assume "a + inverse (of_nat (Suc i)) \<le> f x"
  1.1093 -        finally show "a < f x" .
  1.1094 -      qed
  1.1095 -      with a show ?thesis by auto
  1.1096 -    qed simp
  1.1097 -  qed
  1.1098 -next
  1.1099 -  assume a': "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
  1.1100 -  then have a: "\<forall>a. {x \<in> space M. f x \<le> a} \<in> sets M" unfolding less_eq_ge_measurable .
  1.1101 -  show "\<forall>a. {x \<in> space M. a \<le> f x} \<in> sets M" unfolding greater_eq_le_measurable[symmetric]
  1.1102 -  proof
  1.1103 -    fix a show "{x \<in> space M. f x < a} \<in> sets M"
  1.1104 -    proof (cases a)
  1.1105 -      case (preal r)
  1.1106 -      show ?thesis
  1.1107 -      proof cases
  1.1108 -        assume "a = 0" then show ?thesis by simp
  1.1109 -      next
  1.1110 -        assume "a \<noteq> 0"
  1.1111 -        have "{x\<in>space M. f x < a} = (\<Union>i. {x\<in>space M. f x \<le> a - inverse (of_nat (Suc i))})"
  1.1112 -        proof safe
  1.1113 -          fix x assume "f x < a" and [simp]: "x \<in> space M"
  1.1114 -          with ex_pinfreal_inverse_of_nat_Suc_less[of "a - f x"]
  1.1115 -          obtain n where "inverse (of_nat (Suc n)) < a - f x"
  1.1116 -            using preal by (cases "f x") auto
  1.1117 -          then have "f x \<le> a - inverse (of_nat (Suc n)) "
  1.1118 -            using preal by (cases "f x") (auto split: split_if_asm)
  1.1119 -          then show "x \<in> (\<Union>i. {x \<in> space M. f x \<le> a - inverse (of_nat (Suc i))})"
  1.1120 -            by auto
  1.1121 -        next
  1.1122 -          fix i x assume [simp]: "x \<in> space M"
  1.1123 -          assume "f x \<le> a - inverse (of_nat (Suc i))"
  1.1124 -          also have "\<dots> < a" using `a \<noteq> 0` preal by auto
  1.1125 -          finally show "f x < a" .
  1.1126 -        qed
  1.1127 -        with a show ?thesis by auto
  1.1128 -      qed
  1.1129 -    next
  1.1130 -      case infinite
  1.1131 -      have "f -` {\<omega>} \<inter> space M = (\<Inter>n. {x\<in>space M. of_nat n < f x})"
  1.1132 -      proof (safe, simp_all, safe)
  1.1133 -        fix x assume *: "\<forall>n::nat. Real (real n) < f x"
  1.1134 -        show "f x = \<omega>"    proof (rule ccontr)
  1.1135 -          assume "f x \<noteq> \<omega>"
  1.1136 -          with real_arch_lt[of "real (f x)"] obtain n where "f x < of_nat n"
  1.1137 -            by (auto simp: pinfreal_noteq_omega_Ex)
  1.1138 -          with *[THEN spec, of n] show False by auto
  1.1139 -        qed
  1.1140 -      qed
  1.1141 -      with a' have \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" by auto
  1.1142 -      moreover have "{x \<in> space M. f x < a} = space M - f -` {\<omega>} \<inter> space M"
  1.1143 -        using infinite by auto
  1.1144 -      ultimately show ?thesis by auto
  1.1145 -    qed
  1.1146 -  qed
  1.1147 -qed
  1.1148 -
  1.1149 -lemma (in sigma_algebra) borel_measurable_pinfreal_iff_greater:
  1.1150 -  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a < f x} \<in> sets M)"
  1.1151 -proof safe
  1.1152 -  fix a assume f: "f \<in> borel_measurable M"
  1.1153 -  have "{x\<in>space M. a < f x} = f -` {a <..} \<inter> space M" by auto
  1.1154 -  with f show "{x\<in>space M. a < f x} \<in> sets M"
  1.1155 -    by (auto intro!: measurable_sets)
  1.1156 -next
  1.1157 -  assume *: "\<forall>a. {x\<in>space M. a < f x} \<in> sets M"
  1.1158 -  hence **: "\<forall>a. {x\<in>space M. f x < a} \<in> sets M"
  1.1159 -    unfolding less_eq_le_pinfreal_measurable
  1.1160 -    unfolding greater_eq_le_measurable .
  1.1161 -
  1.1162 -  show "f \<in> borel_measurable M" unfolding borel_measurable_pinfreal_eq_real borel_measurable_iff_greater
  1.1163 -  proof safe
  1.1164 -    have "f -` {\<omega>} \<inter> space M = space M - {x\<in>space M. f x < \<omega>}" by auto
  1.1165 -    then show \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" using ** by auto
  1.1166 -
  1.1167 -    fix a
  1.1168 -    have "{w \<in> space M. a < real (f w)} =
  1.1169 -      (if 0 \<le> a then {w\<in>space M. Real a < f w} - (f -` {\<omega>} \<inter> space M) else space M)"
  1.1170 -    proof (split split_if, safe del: notI)
  1.1171 -      fix x assume "0 \<le> a"
  1.1172 -      { assume "a < real (f x)" then show "Real a < f x" "x \<notin> f -` {\<omega>} \<inter> space M"
  1.1173 -          using `0 \<le> a` by (cases "f x", auto) }
  1.1174 -      { assume "Real a < f x" "x \<notin> f -` {\<omega>}" then show "a < real (f x)"
  1.1175 -          using `0 \<le> a` by (cases "f x", auto) }
  1.1176 -    next
  1.1177 -      fix x assume "\<not> 0 \<le> a" then show "a < real (f x)" by (cases "f x") auto
  1.1178 -    qed
  1.1179 -    then show "{w \<in> space M. a < real (f w)} \<in> sets M"
  1.1180 -      using \<omega> * by (auto intro!: Diff)
  1.1181 -  qed
  1.1182 -qed
  1.1183 -
  1.1184 -lemma (in sigma_algebra) borel_measurable_pinfreal_iff_less:
  1.1185 -  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x < a} \<in> sets M)"
  1.1186 -  using borel_measurable_pinfreal_iff_greater unfolding less_eq_le_pinfreal_measurable greater_eq_le_measurable .
  1.1187 -
  1.1188 -lemma (in sigma_algebra) borel_measurable_pinfreal_iff_le:
  1.1189 -  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x \<le> a} \<in> sets M)"
  1.1190 -  using borel_measurable_pinfreal_iff_greater unfolding less_eq_ge_measurable .
  1.1191 -
  1.1192 -lemma (in sigma_algebra) borel_measurable_pinfreal_iff_ge:
  1.1193 -  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M)"
  1.1194 -  using borel_measurable_pinfreal_iff_greater unfolding less_eq_le_pinfreal_measurable .
  1.1195 -
  1.1196 -lemma (in sigma_algebra) borel_measurable_pinfreal_eq_const:
  1.1197 -  fixes f :: "'a \<Rightarrow> pinfreal" assumes "f \<in> borel_measurable M"
  1.1198 -  shows "{x\<in>space M. f x = c} \<in> sets M"
  1.1199 -proof -
  1.1200 -  have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
  1.1201 -  then show ?thesis using assms by (auto intro!: measurable_sets)
  1.1202 -qed
  1.1203 -
  1.1204 -lemma (in sigma_algebra) borel_measurable_pinfreal_neq_const:
  1.1205 -  fixes f :: "'a \<Rightarrow> pinfreal"
  1.1206 -  assumes "f \<in> borel_measurable M"
  1.1207 -  shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1.1208 -proof -
  1.1209 -  have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
  1.1210 -  then show ?thesis using assms by (auto intro!: measurable_sets)
  1.1211 -qed
  1.1212 -
  1.1213 -lemma (in sigma_algebra) borel_measurable_pinfreal_less[intro,simp]:
  1.1214 -  fixes f g :: "'a \<Rightarrow> pinfreal"
  1.1215 -  assumes f: "f \<in> borel_measurable M"
  1.1216 -  assumes g: "g \<in> borel_measurable M"
  1.1217 -  shows "{x \<in> space M. f x < g x} \<in> sets M"
  1.1218 -proof -
  1.1219 -  have "(\<lambda>x. real (f x)) \<in> borel_measurable M"
  1.1220 -    "(\<lambda>x. real (g x)) \<in> borel_measurable M"
  1.1221 -    using assms by (auto intro!: borel_measurable_real)
  1.1222 -  from borel_measurable_less[OF this]
  1.1223 -  have "{x \<in> space M. real (f x) < real (g x)} \<in> sets M" .
  1.1224 -  moreover have "{x \<in> space M. f x \<noteq> \<omega>} \<in> sets M" using f by (rule borel_measurable_pinfreal_neq_const)
  1.1225 -  moreover have "{x \<in> space M. g x = \<omega>} \<in> sets M" using g by (rule borel_measurable_pinfreal_eq_const)
  1.1226 -  moreover have "{x \<in> space M. g x \<noteq> \<omega>} \<in> sets M" using g by (rule borel_measurable_pinfreal_neq_const)
  1.1227 -  moreover have "{x \<in> space M. f x < g x} = ({x \<in> space M. g x = \<omega>} \<inter> {x \<in> space M. f x \<noteq> \<omega>}) \<union>
  1.1228 -    ({x \<in> space M. g x \<noteq> \<omega>} \<inter> {x \<in> space M. f x \<noteq> \<omega>} \<inter> {x \<in> space M. real (f x) < real (g x)})"
  1.1229 -    by (auto simp: real_of_pinfreal_strict_mono_iff)
  1.1230 -  ultimately show ?thesis by auto
  1.1231 -qed
  1.1232 -
  1.1233 -lemma (in sigma_algebra) borel_measurable_pinfreal_le[intro,simp]:
  1.1234 -  fixes f :: "'a \<Rightarrow> pinfreal"
  1.1235 -  assumes f: "f \<in> borel_measurable M"
  1.1236 -  assumes g: "g \<in> borel_measurable M"
  1.1237 -  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
  1.1238 -proof -
  1.1239 -  have "{x \<in> space M. f x \<le> g x} = space M - {x \<in> space M. g x < f x}" by auto
  1.1240 -  then show ?thesis using g f by auto
  1.1241 -qed
  1.1242 -
  1.1243 -lemma (in sigma_algebra) borel_measurable_pinfreal_eq[intro,simp]:
  1.1244 -  fixes f :: "'a \<Rightarrow> pinfreal"
  1.1245 -  assumes f: "f \<in> borel_measurable M"
  1.1246 -  assumes g: "g \<in> borel_measurable M"
  1.1247 -  shows "{w \<in> space M. f w = g w} \<in> sets M"
  1.1248 -proof -
  1.1249 -  have "{x \<in> space M. f x = g x} = {x \<in> space M. g x \<le> f x} \<inter> {x \<in> space M. f x \<le> g x}" by auto
  1.1250 -  then show ?thesis using g f by auto
  1.1251 -qed
  1.1252 -
  1.1253 -lemma (in sigma_algebra) borel_measurable_pinfreal_neq[intro,simp]:
  1.1254 -  fixes f :: "'a \<Rightarrow> pinfreal"
  1.1255 -  assumes f: "f \<in> borel_measurable M"
  1.1256 -  assumes g: "g \<in> borel_measurable M"
  1.1257 -  shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
  1.1258 -proof -
  1.1259 -  have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}" by auto
  1.1260 -  thus ?thesis using f g by auto
  1.1261 -qed
  1.1262 -
  1.1263 -lemma (in sigma_algebra) borel_measurable_pinfreal_add[intro, simp]:
  1.1264 -  fixes f :: "'a \<Rightarrow> pinfreal"
  1.1265 -  assumes measure: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1.1266 -  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1.1267 -proof -
  1.1268 -  have *: "(\<lambda>x. f x + g x) =
  1.1269 -     (\<lambda>x. if f x = \<omega> then \<omega> else if g x = \<omega> then \<omega> else Real (real (f x) + real (g x)))"
  1.1270 -     by (auto simp: fun_eq_iff pinfreal_noteq_omega_Ex)
  1.1271 -  show ?thesis using assms unfolding *
  1.1272 -    by (auto intro!: measurable_If)
  1.1273 -qed
  1.1274 -
  1.1275 -lemma (in sigma_algebra) borel_measurable_pinfreal_times[intro, simp]:
  1.1276 -  fixes f :: "'a \<Rightarrow> pinfreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1.1277 -  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1.1278 -proof -
  1.1279 -  have *: "(\<lambda>x. f x * g x) =
  1.1280 -     (\<lambda>x. if f x = 0 then 0 else if g x = 0 then 0 else if f x = \<omega> then \<omega> else if g x = \<omega> then \<omega> else
  1.1281 -      Real (real (f x) * real (g x)))"
  1.1282 -     by (auto simp: fun_eq_iff pinfreal_noteq_omega_Ex)
  1.1283 -  show ?thesis using assms unfolding *
  1.1284 -    by (auto intro!: measurable_If)
  1.1285 -qed
  1.1286 -
  1.1287 -lemma (in sigma_algebra) borel_measurable_pinfreal_setsum[simp, intro]:
  1.1288 -  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> pinfreal"
  1.1289 -  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1.1290 -  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1.1291 -proof cases
  1.1292 -  assume "finite S"
  1.1293 -  thus ?thesis using assms
  1.1294 -    by induct auto
  1.1295 -qed (simp add: borel_measurable_const)
  1.1296 -
  1.1297 -lemma (in sigma_algebra) borel_measurable_pinfreal_min[intro, simp]:
  1.1298 -  fixes f g :: "'a \<Rightarrow> pinfreal"
  1.1299 -  assumes "f \<in> borel_measurable M"
  1.1300 -  assumes "g \<in> borel_measurable M"
  1.1301 -  shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1.1302 -  using assms unfolding min_def by (auto intro!: measurable_If)
  1.1303 -
  1.1304 -lemma (in sigma_algebra) borel_measurable_pinfreal_max[intro]:
  1.1305 -  fixes f g :: "'a \<Rightarrow> pinfreal"
  1.1306 -  assumes "f \<in> borel_measurable M"
  1.1307 -  and "g \<in> borel_measurable M"
  1.1308 -  shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1.1309 -  using assms unfolding max_def by (auto intro!: measurable_If)
  1.1310 -
  1.1311 -lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
  1.1312 -  fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> pinfreal"
  1.1313 -  assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1.1314 -  shows "(SUP i : A. f i) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  1.1315 -  unfolding borel_measurable_pinfreal_iff_greater
  1.1316 -proof safe
  1.1317 -  fix a
  1.1318 -  have "{x\<in>space M. a < ?sup x} = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  1.1319 -    by (auto simp: less_Sup_iff SUPR_def[where 'a=pinfreal] SUPR_fun_expand[where 'c=pinfreal])
  1.1320 -  then show "{x\<in>space M. a < ?sup x} \<in> sets M"
  1.1321 -    using assms by auto
  1.1322 -qed
  1.1323 -
  1.1324 -lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
  1.1325 -  fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> pinfreal"
  1.1326 -  assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1.1327 -  shows "(INF i : A. f i) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  1.1328 -  unfolding borel_measurable_pinfreal_iff_less
  1.1329 -proof safe
  1.1330 -  fix a
  1.1331 -  have "{x\<in>space M. ?inf x < a} = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  1.1332 -    by (auto simp: Inf_less_iff INFI_def[where 'a=pinfreal] INFI_fun_expand)
  1.1333 -  then show "{x\<in>space M. ?inf x < a} \<in> sets M"
  1.1334 -    using assms by auto
  1.1335 -qed
  1.1336 -
  1.1337 -lemma (in sigma_algebra) borel_measurable_pinfreal_diff:
  1.1338 -  fixes f g :: "'a \<Rightarrow> pinfreal"
  1.1339 -  assumes "f \<in> borel_measurable M"
  1.1340 -  assumes "g \<in> borel_measurable M"
  1.1341 -  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1.1342 -  unfolding borel_measurable_pinfreal_iff_greater
  1.1343 -proof safe
  1.1344 -  fix a
  1.1345 -  have "{x \<in> space M. a < f x - g x} = {x \<in> space M. g x + a < f x}"
  1.1346 -    by (simp add: pinfreal_less_minus_iff)
  1.1347 -  then show "{x \<in> space M. a < f x - g x} \<in> sets M"
  1.1348 -    using assms by auto
  1.1349 -qed
  1.1350 -
  1.1351 -lemma (in sigma_algebra) borel_measurable_psuminf:
  1.1352 -  assumes "\<And>i. f i \<in> borel_measurable M"
  1.1353 -  shows "(\<lambda>x. (\<Sum>\<^isub>\<infinity> i. f i x)) \<in> borel_measurable M"
  1.1354 -  using assms unfolding psuminf_def
  1.1355 -  by (auto intro!: borel_measurable_SUP[unfolded SUPR_fun_expand])
  1.1356 -
  1.1357 -section "LIMSEQ is borel measurable"
  1.1358 -
  1.1359 -lemma (in sigma_algebra) borel_measurable_LIMSEQ:
  1.1360 -  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1.1361 -  assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1.1362 -  and u: "\<And>i. u i \<in> borel_measurable M"
  1.1363 -  shows "u' \<in> borel_measurable M"
  1.1364 -proof -
  1.1365 -  let "?pu x i" = "max (u i x) 0"
  1.1366 -  let "?nu x i" = "max (- u i x) 0"
  1.1367 -
  1.1368 -  { fix x assume x: "x \<in> space M"
  1.1369 -    have "(?pu x) ----> max (u' x) 0"
  1.1370 -      "(?nu x) ----> max (- u' x) 0"
  1.1371 -      using u'[OF x] by (auto intro!: LIMSEQ_max LIMSEQ_minus)
  1.1372 -    from LIMSEQ_imp_lim_INF[OF _ this(1)] LIMSEQ_imp_lim_INF[OF _ this(2)]
  1.1373 -    have "(SUP n. INF m. Real (u (n + m) x)) = Real (u' x)"
  1.1374 -      "(SUP n. INF m. Real (- u (n + m) x)) = Real (- u' x)"
  1.1375 -      by (simp_all add: Real_max'[symmetric]) }
  1.1376 -  note eq = this
  1.1377 -
  1.1378 -  have *: "\<And>x. real (Real (u' x)) - real (Real (- u' x)) = u' x"
  1.1379 -    by auto
  1.1380 -
  1.1381 -  have "(SUP n. INF m. (\<lambda>x. Real (u (n + m) x))) \<in> borel_measurable M"
  1.1382 -       "(SUP n. INF m. (\<lambda>x. Real (- u (n + m) x))) \<in> borel_measurable M"
  1.1383 -    using u by (auto intro: borel_measurable_SUP borel_measurable_INF borel_measurable_Real)
  1.1384 -  with eq[THEN measurable_cong, of M "\<lambda>x. x" borel_space]
  1.1385 -  have "(\<lambda>x. Real (u' x)) \<in> borel_measurable M"
  1.1386 -       "(\<lambda>x. Real (- u' x)) \<in> borel_measurable M"
  1.1387 -    unfolding SUPR_fun_expand INFI_fun_expand by auto
  1.1388 -  note this[THEN borel_measurable_real]
  1.1389 -  from borel_measurable_diff[OF this]
  1.1390 -  show ?thesis unfolding * .
  1.1391 -qed
  1.1392 -
  1.1393 -end
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Probability/Borel_Space.thy	Wed Dec 01 19:20:30 2010 +0100
     2.3 @@ -0,0 +1,1466 @@
     2.4 +(* Author: Armin Heller, Johannes Hoelzl, TU Muenchen *)
     2.5 +
     2.6 +header {*Borel spaces*}
     2.7 +
     2.8 +theory Borel_Space
     2.9 +  imports Sigma_Algebra Positive_Infinite_Real Multivariate_Analysis
    2.10 +begin
    2.11 +
    2.12 +lemma (in sigma_algebra) sets_sigma_subset:
    2.13 +  assumes "space N = space M"
    2.14 +  assumes "sets N \<subseteq> sets M"
    2.15 +  shows "sets (sigma N) \<subseteq> sets M"
    2.16 +  by (unfold assms sets_sigma, rule sigma_sets_subset, rule assms)
    2.17 +
    2.18 +lemma LIMSEQ_max:
    2.19 +  "u ----> (x::real) \<Longrightarrow> (\<lambda>i. max (u i) 0) ----> max x 0"
    2.20 +  by (fastsimp intro!: LIMSEQ_I dest!: LIMSEQ_D)
    2.21 +
    2.22 +section "Generic Borel spaces"
    2.23 +
    2.24 +definition "borel = sigma \<lparr> space = UNIV::'a::topological_space set, sets = open\<rparr>"
    2.25 +abbreviation "borel_measurable M \<equiv> measurable M borel"
    2.26 +
    2.27 +interpretation borel: sigma_algebra borel
    2.28 +  by (auto simp: borel_def intro!: sigma_algebra_sigma)
    2.29 +
    2.30 +lemma in_borel_measurable:
    2.31 +   "f \<in> borel_measurable M \<longleftrightarrow>
    2.32 +    (\<forall>S \<in> sets (sigma \<lparr> space = UNIV, sets = open\<rparr>).
    2.33 +      f -` S \<inter> space M \<in> sets M)"
    2.34 +  by (auto simp add: measurable_def borel_def)
    2.35 +
    2.36 +lemma in_borel_measurable_borel:
    2.37 +   "f \<in> borel_measurable M \<longleftrightarrow>
    2.38 +    (\<forall>S \<in> sets borel.
    2.39 +      f -` S \<inter> space M \<in> sets M)"
    2.40 +  by (auto simp add: measurable_def borel_def)
    2.41 +
    2.42 +lemma space_borel[simp]: "space borel = UNIV"
    2.43 +  unfolding borel_def by auto
    2.44 +
    2.45 +lemma borel_open[simp]:
    2.46 +  assumes "open A" shows "A \<in> sets borel"
    2.47 +proof -
    2.48 +  have "A \<in> open" unfolding mem_def using assms .
    2.49 +  thus ?thesis unfolding borel_def sigma_def by (auto intro!: sigma_sets.Basic)
    2.50 +qed
    2.51 +
    2.52 +lemma borel_closed[simp]:
    2.53 +  assumes "closed A" shows "A \<in> sets borel"
    2.54 +proof -
    2.55 +  have "space borel - (- A) \<in> sets borel"
    2.56 +    using assms unfolding closed_def by (blast intro: borel_open)
    2.57 +  thus ?thesis by simp
    2.58 +qed
    2.59 +
    2.60 +lemma (in sigma_algebra) borel_measurable_vimage:
    2.61 +  fixes f :: "'a \<Rightarrow> 'x::t2_space"
    2.62 +  assumes borel: "f \<in> borel_measurable M"
    2.63 +  shows "f -` {x} \<inter> space M \<in> sets M"
    2.64 +proof (cases "x \<in> f ` space M")
    2.65 +  case True then obtain y where "x = f y" by auto
    2.66 +  from closed_sing[of "f y"]
    2.67 +  have "{f y} \<in> sets borel" by (rule borel_closed)
    2.68 +  with assms show ?thesis
    2.69 +    unfolding in_borel_measurable_borel `x = f y` by auto
    2.70 +next
    2.71 +  case False hence "f -` {x} \<inter> space M = {}" by auto
    2.72 +  thus ?thesis by auto
    2.73 +qed
    2.74 +
    2.75 +lemma (in sigma_algebra) borel_measurableI:
    2.76 +  fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
    2.77 +  assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
    2.78 +  shows "f \<in> borel_measurable M"
    2.79 +  unfolding borel_def
    2.80 +proof (rule measurable_sigma, simp_all)
    2.81 +  fix S :: "'x set" assume "S \<in> open" thus "f -` S \<inter> space M \<in> sets M"
    2.82 +    using assms[of S] by (simp add: mem_def)
    2.83 +qed
    2.84 +
    2.85 +lemma borel_singleton[simp, intro]:
    2.86 +  fixes x :: "'a::t1_space"
    2.87 +  shows "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets borel"
    2.88 +  proof (rule borel.insert_in_sets)
    2.89 +    show "{x} \<in> sets borel"
    2.90 +      using closed_sing[of x] by (rule borel_closed)
    2.91 +  qed simp
    2.92 +
    2.93 +lemma (in sigma_algebra) borel_measurable_const[simp, intro]:
    2.94 +  "(\<lambda>x. c) \<in> borel_measurable M"
    2.95 +  by (auto intro!: measurable_const)
    2.96 +
    2.97 +lemma (in sigma_algebra) borel_measurable_indicator[simp, intro!]:
    2.98 +  assumes A: "A \<in> sets M"
    2.99 +  shows "indicator A \<in> borel_measurable M"
   2.100 +  unfolding indicator_def_raw using A
   2.101 +  by (auto intro!: measurable_If_set borel_measurable_const)
   2.102 +
   2.103 +lemma (in sigma_algebra) borel_measurable_indicator_iff:
   2.104 +  "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
   2.105 +    (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
   2.106 +proof
   2.107 +  assume "?I \<in> borel_measurable M"
   2.108 +  then have "?I -` {1} \<inter> space M \<in> sets M"
   2.109 +    unfolding measurable_def by auto
   2.110 +  also have "?I -` {1} \<inter> space M = A \<inter> space M"
   2.111 +    unfolding indicator_def_raw by auto
   2.112 +  finally show "A \<inter> space M \<in> sets M" .
   2.113 +next
   2.114 +  assume "A \<inter> space M \<in> sets M"
   2.115 +  moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   2.116 +    (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   2.117 +    by (intro measurable_cong) (auto simp: indicator_def)
   2.118 +  ultimately show "?I \<in> borel_measurable M" by auto
   2.119 +qed
   2.120 +
   2.121 +lemma borel_measurable_translate:
   2.122 +  assumes "A \<in> sets borel" and trans: "\<And>B. open B \<Longrightarrow> f -` B \<in> sets borel"
   2.123 +  shows "f -` A \<in> sets borel"
   2.124 +proof -
   2.125 +  have "A \<in> sigma_sets UNIV open" using assms
   2.126 +    by (simp add: borel_def sigma_def)
   2.127 +  thus ?thesis
   2.128 +  proof (induct rule: sigma_sets.induct)
   2.129 +    case (Basic a) thus ?case using trans[of a] by (simp add: mem_def)
   2.130 +  next
   2.131 +    case (Compl a)
   2.132 +    moreover have "UNIV \<in> sets borel"
   2.133 +      using borel.top by simp
   2.134 +    ultimately show ?case
   2.135 +      by (auto simp: vimage_Diff borel.Diff)
   2.136 +  qed (auto simp add: vimage_UN)
   2.137 +qed
   2.138 +
   2.139 +lemma (in sigma_algebra) borel_measurable_restricted:
   2.140 +  fixes f :: "'a \<Rightarrow> 'x\<Colon>{topological_space, semiring_1}" assumes "A \<in> sets M"
   2.141 +  shows "f \<in> borel_measurable (restricted_space A) \<longleftrightarrow>
   2.142 +    (\<lambda>x. f x * indicator A x) \<in> borel_measurable M"
   2.143 +    (is "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable M")
   2.144 +proof -
   2.145 +  interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
   2.146 +  have *: "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable ?R"
   2.147 +    by (auto intro!: measurable_cong)
   2.148 +  show ?thesis unfolding *
   2.149 +    unfolding in_borel_measurable_borel
   2.150 +  proof (simp, safe)
   2.151 +    fix S :: "'x set" assume "S \<in> sets borel"
   2.152 +      "\<forall>S\<in>sets borel. ?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   2.153 +    then have "?f -` S \<inter> A \<in> op \<inter> A ` sets M" by auto
   2.154 +    then have f: "?f -` S \<inter> A \<in> sets M"
   2.155 +      using `A \<in> sets M` sets_into_space by fastsimp
   2.156 +    show "?f -` S \<inter> space M \<in> sets M"
   2.157 +    proof cases
   2.158 +      assume "0 \<in> S"
   2.159 +      then have "?f -` S \<inter> space M = ?f -` S \<inter> A \<union> (space M - A)"
   2.160 +        using `A \<in> sets M` sets_into_space by auto
   2.161 +      then show ?thesis using f `A \<in> sets M` by (auto intro!: Un Diff)
   2.162 +    next
   2.163 +      assume "0 \<notin> S"
   2.164 +      then have "?f -` S \<inter> space M = ?f -` S \<inter> A"
   2.165 +        using `A \<in> sets M` sets_into_space
   2.166 +        by (auto simp: indicator_def split: split_if_asm)
   2.167 +      then show ?thesis using f by auto
   2.168 +    qed
   2.169 +  next
   2.170 +    fix S :: "'x set" assume "S \<in> sets borel"
   2.171 +      "\<forall>S\<in>sets borel. ?f -` S \<inter> space M \<in> sets M"
   2.172 +    then have f: "?f -` S \<inter> space M \<in> sets M" by auto
   2.173 +    then show "?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   2.174 +      using `A \<in> sets M` sets_into_space
   2.175 +      apply (simp add: image_iff)
   2.176 +      apply (rule bexI[OF _ f])
   2.177 +      by auto
   2.178 +  qed
   2.179 +qed
   2.180 +
   2.181 +lemma (in sigma_algebra) borel_measurable_subalgebra:
   2.182 +  assumes "N \<subseteq> sets M" "f \<in> borel_measurable (M\<lparr>sets:=N\<rparr>)"
   2.183 +  shows "f \<in> borel_measurable M"
   2.184 +  using assms unfolding measurable_def by auto
   2.185 +
   2.186 +section "Borel spaces on euclidean spaces"
   2.187 +
   2.188 +lemma lessThan_borel[simp, intro]:
   2.189 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   2.190 +  shows "{..< a} \<in> sets borel"
   2.191 +  by (blast intro: borel_open)
   2.192 +
   2.193 +lemma greaterThan_borel[simp, intro]:
   2.194 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   2.195 +  shows "{a <..} \<in> sets borel"
   2.196 +  by (blast intro: borel_open)
   2.197 +
   2.198 +lemma greaterThanLessThan_borel[simp, intro]:
   2.199 +  fixes a b :: "'a\<Colon>ordered_euclidean_space"
   2.200 +  shows "{a<..<b} \<in> sets borel"
   2.201 +  by (blast intro: borel_open)
   2.202 +
   2.203 +lemma atMost_borel[simp, intro]:
   2.204 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   2.205 +  shows "{..a} \<in> sets borel"
   2.206 +  by (blast intro: borel_closed)
   2.207 +
   2.208 +lemma atLeast_borel[simp, intro]:
   2.209 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   2.210 +  shows "{a..} \<in> sets borel"
   2.211 +  by (blast intro: borel_closed)
   2.212 +
   2.213 +lemma atLeastAtMost_borel[simp, intro]:
   2.214 +  fixes a b :: "'a\<Colon>ordered_euclidean_space"
   2.215 +  shows "{a..b} \<in> sets borel"
   2.216 +  by (blast intro: borel_closed)
   2.217 +
   2.218 +lemma greaterThanAtMost_borel[simp, intro]:
   2.219 +  fixes a b :: "'a\<Colon>ordered_euclidean_space"
   2.220 +  shows "{a<..b} \<in> sets borel"
   2.221 +  unfolding greaterThanAtMost_def by blast
   2.222 +
   2.223 +lemma atLeastLessThan_borel[simp, intro]:
   2.224 +  fixes a b :: "'a\<Colon>ordered_euclidean_space"
   2.225 +  shows "{a..<b} \<in> sets borel"
   2.226 +  unfolding atLeastLessThan_def by blast
   2.227 +
   2.228 +lemma hafspace_less_borel[simp, intro]:
   2.229 +  fixes a :: real
   2.230 +  shows "{x::'a::euclidean_space. a < x $$ i} \<in> sets borel"
   2.231 +  by (auto intro!: borel_open open_halfspace_component_gt)
   2.232 +
   2.233 +lemma hafspace_greater_borel[simp, intro]:
   2.234 +  fixes a :: real
   2.235 +  shows "{x::'a::euclidean_space. x $$ i < a} \<in> sets borel"
   2.236 +  by (auto intro!: borel_open open_halfspace_component_lt)
   2.237 +
   2.238 +lemma hafspace_less_eq_borel[simp, intro]:
   2.239 +  fixes a :: real
   2.240 +  shows "{x::'a::euclidean_space. a \<le> x $$ i} \<in> sets borel"
   2.241 +  by (auto intro!: borel_closed closed_halfspace_component_ge)
   2.242 +
   2.243 +lemma hafspace_greater_eq_borel[simp, intro]:
   2.244 +  fixes a :: real
   2.245 +  shows "{x::'a::euclidean_space. x $$ i \<le> a} \<in> sets borel"
   2.246 +  by (auto intro!: borel_closed closed_halfspace_component_le)
   2.247 +
   2.248 +lemma (in sigma_algebra) borel_measurable_less[simp, intro]:
   2.249 +  fixes f :: "'a \<Rightarrow> real"
   2.250 +  assumes f: "f \<in> borel_measurable M"
   2.251 +  assumes g: "g \<in> borel_measurable M"
   2.252 +  shows "{w \<in> space M. f w < g w} \<in> sets M"
   2.253 +proof -
   2.254 +  have "{w \<in> space M. f w < g w} =
   2.255 +        (\<Union>r. (f -` {..< of_rat r} \<inter> space M) \<inter> (g -` {of_rat r <..} \<inter> space M))"
   2.256 +    using Rats_dense_in_real by (auto simp add: Rats_def)
   2.257 +  then show ?thesis using f g
   2.258 +    by simp (blast intro: measurable_sets)
   2.259 +qed
   2.260 +
   2.261 +lemma (in sigma_algebra) borel_measurable_le[simp, intro]:
   2.262 +  fixes f :: "'a \<Rightarrow> real"
   2.263 +  assumes f: "f \<in> borel_measurable M"
   2.264 +  assumes g: "g \<in> borel_measurable M"
   2.265 +  shows "{w \<in> space M. f w \<le> g w} \<in> sets M"
   2.266 +proof -
   2.267 +  have "{w \<in> space M. f w \<le> g w} = space M - {w \<in> space M. g w < f w}"
   2.268 +    by auto
   2.269 +  thus ?thesis using f g
   2.270 +    by simp blast
   2.271 +qed
   2.272 +
   2.273 +lemma (in sigma_algebra) borel_measurable_eq[simp, intro]:
   2.274 +  fixes f :: "'a \<Rightarrow> real"
   2.275 +  assumes f: "f \<in> borel_measurable M"
   2.276 +  assumes g: "g \<in> borel_measurable M"
   2.277 +  shows "{w \<in> space M. f w = g w} \<in> sets M"
   2.278 +proof -
   2.279 +  have "{w \<in> space M. f w = g w} =
   2.280 +        {w \<in> space M. f w \<le> g w} \<inter> {w \<in> space M. g w \<le> f w}"
   2.281 +    by auto
   2.282 +  thus ?thesis using f g by auto
   2.283 +qed
   2.284 +
   2.285 +lemma (in sigma_algebra) borel_measurable_neq[simp, intro]:
   2.286 +  fixes f :: "'a \<Rightarrow> real"
   2.287 +  assumes f: "f \<in> borel_measurable M"
   2.288 +  assumes g: "g \<in> borel_measurable M"
   2.289 +  shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   2.290 +proof -
   2.291 +  have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}"
   2.292 +    by auto
   2.293 +  thus ?thesis using f g by auto
   2.294 +qed
   2.295 +
   2.296 +subsection "Borel space equals sigma algebras over intervals"
   2.297 +
   2.298 +lemma rational_boxes:
   2.299 +  fixes x :: "'a\<Colon>ordered_euclidean_space"
   2.300 +  assumes "0 < e"
   2.301 +  shows "\<exists>a b. (\<forall>i. a $$ i \<in> \<rat>) \<and> (\<forall>i. b $$ i \<in> \<rat>) \<and> x \<in> {a <..< b} \<and> {a <..< b} \<subseteq> ball x e"
   2.302 +proof -
   2.303 +  def e' \<equiv> "e / (2 * sqrt (real (DIM ('a))))"
   2.304 +  then have e: "0 < e'" using assms by (auto intro!: divide_pos_pos)
   2.305 +  have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> y < x $$ i \<and> x $$ i - y < e'" (is "\<forall>i. ?th i")
   2.306 +  proof
   2.307 +    fix i from Rats_dense_in_real[of "x $$ i - e'" "x $$ i"] e
   2.308 +    show "?th i" by auto
   2.309 +  qed
   2.310 +  from choice[OF this] guess a .. note a = this
   2.311 +  have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> x $$ i < y \<and> y - x $$ i < e'" (is "\<forall>i. ?th i")
   2.312 +  proof
   2.313 +    fix i from Rats_dense_in_real[of "x $$ i" "x $$ i + e'"] e
   2.314 +    show "?th i" by auto
   2.315 +  qed
   2.316 +  from choice[OF this] guess b .. note b = this
   2.317 +  { fix y :: 'a assume *: "Chi a < y" "y < Chi b"
   2.318 +    have "dist x y = sqrt (\<Sum>i<DIM('a). (dist (x $$ i) (y $$ i))\<twosuperior>)"
   2.319 +      unfolding setL2_def[symmetric] by (rule euclidean_dist_l2)
   2.320 +    also have "\<dots> < sqrt (\<Sum>i<DIM('a). e^2 / real (DIM('a)))"
   2.321 +    proof (rule real_sqrt_less_mono, rule setsum_strict_mono)
   2.322 +      fix i assume i: "i \<in> {..<DIM('a)}"
   2.323 +      have "a i < y$$i \<and> y$$i < b i" using * i eucl_less[where 'a='a] by auto
   2.324 +      moreover have "a i < x$$i" "x$$i - a i < e'" using a by auto
   2.325 +      moreover have "x$$i < b i" "b i - x$$i < e'" using b by auto
   2.326 +      ultimately have "\<bar>x$$i - y$$i\<bar> < 2 * e'" by auto
   2.327 +      then have "dist (x $$ i) (y $$ i) < e/sqrt (real (DIM('a)))"
   2.328 +        unfolding e'_def by (auto simp: dist_real_def)
   2.329 +      then have "(dist (x $$ i) (y $$ i))\<twosuperior> < (e/sqrt (real (DIM('a))))\<twosuperior>"
   2.330 +        by (rule power_strict_mono) auto
   2.331 +      then show "(dist (x $$ i) (y $$ i))\<twosuperior> < e\<twosuperior> / real DIM('a)"
   2.332 +        by (simp add: power_divide)
   2.333 +    qed auto
   2.334 +    also have "\<dots> = e" using `0 < e` by (simp add: real_eq_of_nat DIM_positive)
   2.335 +    finally have "dist x y < e" . }
   2.336 +  with a b show ?thesis
   2.337 +    apply (rule_tac exI[of _ "Chi a"])
   2.338 +    apply (rule_tac exI[of _ "Chi b"])
   2.339 +    using eucl_less[where 'a='a] by auto
   2.340 +qed
   2.341 +
   2.342 +lemma ex_rat_list:
   2.343 +  fixes x :: "'a\<Colon>ordered_euclidean_space"
   2.344 +  assumes "\<And> i. x $$ i \<in> \<rat>"
   2.345 +  shows "\<exists> r. length r = DIM('a) \<and> (\<forall> i < DIM('a). of_rat (r ! i) = x $$ i)"
   2.346 +proof -
   2.347 +  have "\<forall>i. \<exists>r. x $$ i = of_rat r" using assms unfolding Rats_def by blast
   2.348 +  from choice[OF this] guess r ..
   2.349 +  then show ?thesis by (auto intro!: exI[of _ "map r [0 ..< DIM('a)]"])
   2.350 +qed
   2.351 +
   2.352 +lemma open_UNION:
   2.353 +  fixes M :: "'a\<Colon>ordered_euclidean_space set"
   2.354 +  assumes "open M"
   2.355 +  shows "M = UNION {(a, b) | a b. {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)} \<subseteq> M}
   2.356 +                   (\<lambda> (a, b). {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)})"
   2.357 +    (is "M = UNION ?idx ?box")
   2.358 +proof safe
   2.359 +  fix x assume "x \<in> M"
   2.360 +  obtain e where e: "e > 0" "ball x e \<subseteq> M"
   2.361 +    using openE[OF assms `x \<in> M`] by auto
   2.362 +  then obtain a b where ab: "x \<in> {a <..< b}" "\<And>i. a $$ i \<in> \<rat>" "\<And>i. b $$ i \<in> \<rat>" "{a <..< b} \<subseteq> ball x e"
   2.363 +    using rational_boxes[OF e(1)] by blast
   2.364 +  then obtain p q where pq: "length p = DIM ('a)"
   2.365 +                            "length q = DIM ('a)"
   2.366 +                            "\<forall> i < DIM ('a). of_rat (p ! i) = a $$ i \<and> of_rat (q ! i) = b $$ i"
   2.367 +    using ex_rat_list[OF ab(2)] ex_rat_list[OF ab(3)] by blast
   2.368 +  hence p: "Chi (of_rat \<circ> op ! p) = a"
   2.369 +    using euclidean_eq[of "Chi (of_rat \<circ> op ! p)" a]
   2.370 +    unfolding o_def by auto
   2.371 +  from pq have q: "Chi (of_rat \<circ> op ! q) = b"
   2.372 +    using euclidean_eq[of "Chi (of_rat \<circ> op ! q)" b]
   2.373 +    unfolding o_def by auto
   2.374 +  have "x \<in> ?box (p, q)"
   2.375 +    using p q ab by auto
   2.376 +  thus "x \<in> UNION ?idx ?box" using ab e p q exI[of _ p] exI[of _ q] by auto
   2.377 +qed auto
   2.378 +
   2.379 +lemma halfspace_span_open:
   2.380 +  "sigma_sets UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))
   2.381 +    \<subseteq> sets borel"
   2.382 +  by (auto intro!: borel.sigma_sets_subset[simplified] borel_open
   2.383 +                   open_halfspace_component_lt)
   2.384 +
   2.385 +lemma halfspace_lt_in_halfspace:
   2.386 +  "{x\<Colon>'a. x $$ i < a} \<in> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>)"
   2.387 +  by (auto intro!: sigma_sets.Basic simp: sets_sigma)
   2.388 +
   2.389 +lemma halfspace_gt_in_halfspace:
   2.390 +  "{x\<Colon>'a. a < x $$ i} \<in> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>)"
   2.391 +  (is "?set \<in> sets ?SIGMA")
   2.392 +proof -
   2.393 +  interpret sigma_algebra "?SIGMA"
   2.394 +    by (intro sigma_algebra_sigma_sets) (simp_all add: sets_sigma)
   2.395 +  have *: "?set = (\<Union>n. space ?SIGMA - {x\<Colon>'a. x $$ i < a + 1 / real (Suc n)})"
   2.396 +  proof (safe, simp_all add: not_less)
   2.397 +    fix x assume "a < x $$ i"
   2.398 +    with reals_Archimedean[of "x $$ i - a"]
   2.399 +    obtain n where "a + 1 / real (Suc n) < x $$ i"
   2.400 +      by (auto simp: inverse_eq_divide field_simps)
   2.401 +    then show "\<exists>n. a + 1 / real (Suc n) \<le> x $$ i"
   2.402 +      by (blast intro: less_imp_le)
   2.403 +  next
   2.404 +    fix x n
   2.405 +    have "a < a + 1 / real (Suc n)" by auto
   2.406 +    also assume "\<dots> \<le> x"
   2.407 +    finally show "a < x" .
   2.408 +  qed
   2.409 +  show "?set \<in> sets ?SIGMA" unfolding *
   2.410 +    by (safe intro!: countable_UN Diff halfspace_lt_in_halfspace)
   2.411 +qed
   2.412 +
   2.413 +lemma open_span_halfspace:
   2.414 +  "sets borel \<subseteq> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x $$ i < a})\<rparr>)"
   2.415 +    (is "_ \<subseteq> sets ?SIGMA")
   2.416 +proof -
   2.417 +  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) simp
   2.418 +  then interpret sigma_algebra ?SIGMA .
   2.419 +  { fix S :: "'a set" assume "S \<in> open" then have "open S" unfolding mem_def .
   2.420 +    from open_UNION[OF this]
   2.421 +    obtain I where *: "S =
   2.422 +      (\<Union>(a, b)\<in>I.
   2.423 +          (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
   2.424 +          (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
   2.425 +      unfolding greaterThanLessThan_def
   2.426 +      unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
   2.427 +      unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
   2.428 +      by blast
   2.429 +    have "S \<in> sets ?SIGMA"
   2.430 +      unfolding *
   2.431 +      by (auto intro!: countable_UN Int countable_INT halfspace_lt_in_halfspace halfspace_gt_in_halfspace) }
   2.432 +  then show ?thesis unfolding borel_def
   2.433 +    by (intro sets_sigma_subset) auto
   2.434 +qed
   2.435 +
   2.436 +lemma halfspace_span_halfspace_le:
   2.437 +  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   2.438 +   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. x $$ i \<le> a})\<rparr>)"
   2.439 +  (is "_ \<subseteq> sets ?SIGMA")
   2.440 +proof -
   2.441 +  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   2.442 +  then interpret sigma_algebra ?SIGMA .
   2.443 +  { fix a i
   2.444 +    have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
   2.445 +    proof (safe, simp_all)
   2.446 +      fix x::'a assume *: "x$$i < a"
   2.447 +      with reals_Archimedean[of "a - x$$i"]
   2.448 +      obtain n where "x $$ i < a - 1 / (real (Suc n))"
   2.449 +        by (auto simp: field_simps inverse_eq_divide)
   2.450 +      then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
   2.451 +        by (blast intro: less_imp_le)
   2.452 +    next
   2.453 +      fix x::'a and n
   2.454 +      assume "x$$i \<le> a - 1 / real (Suc n)"
   2.455 +      also have "\<dots> < a" by auto
   2.456 +      finally show "x$$i < a" .
   2.457 +    qed
   2.458 +    have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   2.459 +      by (safe intro!: countable_UN)
   2.460 +         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   2.461 +  then show ?thesis by (intro sets_sigma_subset) auto
   2.462 +qed
   2.463 +
   2.464 +lemma halfspace_span_halfspace_ge:
   2.465 +  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   2.466 +   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a \<le> x $$ i})\<rparr>)"
   2.467 +  (is "_ \<subseteq> sets ?SIGMA")
   2.468 +proof -
   2.469 +  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   2.470 +  then interpret sigma_algebra ?SIGMA .
   2.471 +  { fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
   2.472 +    have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   2.473 +      by (safe intro!: Diff)
   2.474 +         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   2.475 +  then show ?thesis by (intro sets_sigma_subset) auto
   2.476 +qed
   2.477 +
   2.478 +lemma halfspace_le_span_halfspace_gt:
   2.479 +  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   2.480 +   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a < x $$ i})\<rparr>)"
   2.481 +  (is "_ \<subseteq> sets ?SIGMA")
   2.482 +proof -
   2.483 +  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   2.484 +  then interpret sigma_algebra ?SIGMA .
   2.485 +  { fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   2.486 +    have "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   2.487 +      by (safe intro!: Diff)
   2.488 +         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   2.489 +  then show ?thesis by (intro sets_sigma_subset) auto
   2.490 +qed
   2.491 +
   2.492 +lemma halfspace_le_span_atMost:
   2.493 +  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   2.494 +   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>)"
   2.495 +  (is "_ \<subseteq> sets ?SIGMA")
   2.496 +proof -
   2.497 +  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   2.498 +  then interpret sigma_algebra ?SIGMA .
   2.499 +  have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   2.500 +  proof cases
   2.501 +    fix a i assume "i < DIM('a)"
   2.502 +    then have *: "{x::'a. x$$i \<le> a} = (\<Union>k::nat. {.. (\<chi>\<chi> n. if n = i then a else real k)})"
   2.503 +    proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   2.504 +      fix x
   2.505 +      from real_arch_simple[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"] guess k::nat ..
   2.506 +      then have "\<And>i. i < DIM('a) \<Longrightarrow> x$$i \<le> real k"
   2.507 +        by (subst (asm) Max_le_iff) auto
   2.508 +      then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia \<le> real k"
   2.509 +        by (auto intro!: exI[of _ k])
   2.510 +    qed
   2.511 +    show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   2.512 +      by (safe intro!: countable_UN)
   2.513 +         (auto simp: sets_sigma intro!: sigma_sets.Basic)
   2.514 +  next
   2.515 +    fix a i assume "\<not> i < DIM('a)"
   2.516 +    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   2.517 +      using top by auto
   2.518 +  qed
   2.519 +  then show ?thesis by (intro sets_sigma_subset) auto
   2.520 +qed
   2.521 +
   2.522 +lemma halfspace_le_span_greaterThan:
   2.523 +  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   2.524 +   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {a<..})\<rparr>)"
   2.525 +  (is "_ \<subseteq> sets ?SIGMA")
   2.526 +proof -
   2.527 +  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   2.528 +  then interpret sigma_algebra ?SIGMA .
   2.529 +  have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   2.530 +  proof cases
   2.531 +    fix a i assume "i < DIM('a)"
   2.532 +    have "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   2.533 +    also have *: "{x::'a. a < x$$i} = (\<Union>k::nat. {(\<chi>\<chi> n. if n = i then a else -real k) <..})" using `i <DIM('a)`
   2.534 +    proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   2.535 +      fix x
   2.536 +      from real_arch_lt[of "Max ((\<lambda>i. -x$$i)`{..<DIM('a)})"]
   2.537 +      guess k::nat .. note k = this
   2.538 +      { fix i assume "i < DIM('a)"
   2.539 +        then have "-x$$i < real k"
   2.540 +          using k by (subst (asm) Max_less_iff) auto
   2.541 +        then have "- real k < x$$i" by simp }
   2.542 +      then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> -real k < x $$ ia"
   2.543 +        by (auto intro!: exI[of _ k])
   2.544 +    qed
   2.545 +    finally show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   2.546 +      apply (simp only:)
   2.547 +      apply (safe intro!: countable_UN Diff)
   2.548 +      by (auto simp: sets_sigma intro!: sigma_sets.Basic)
   2.549 +  next
   2.550 +    fix a i assume "\<not> i < DIM('a)"
   2.551 +    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   2.552 +      using top by auto
   2.553 +  qed
   2.554 +  then show ?thesis by (intro sets_sigma_subset) auto
   2.555 +qed
   2.556 +
   2.557 +lemma halfspace_le_span_lessThan:
   2.558 +  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a \<le> x $$ i})\<rparr>) \<subseteq>
   2.559 +   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..<a})\<rparr>)"
   2.560 +  (is "_ \<subseteq> sets ?SIGMA")
   2.561 +proof -
   2.562 +  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   2.563 +  then interpret sigma_algebra ?SIGMA .
   2.564 +  have "\<And>a i. {x. a \<le> x$$i} \<in> sets ?SIGMA"
   2.565 +  proof cases
   2.566 +    fix a i assume "i < DIM('a)"
   2.567 +    have "{x::'a. a \<le> x$$i} = space ?SIGMA - {x::'a. x$$i < a}" by auto
   2.568 +    also have *: "{x::'a. x$$i < a} = (\<Union>k::nat. {..< (\<chi>\<chi> n. if n = i then a else real k)})" using `i <DIM('a)`
   2.569 +    proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   2.570 +      fix x
   2.571 +      from real_arch_lt[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"]
   2.572 +      guess k::nat .. note k = this
   2.573 +      { fix i assume "i < DIM('a)"
   2.574 +        then have "x$$i < real k"
   2.575 +          using k by (subst (asm) Max_less_iff) auto
   2.576 +        then have "x$$i < real k" by simp }
   2.577 +      then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia < real k"
   2.578 +        by (auto intro!: exI[of _ k])
   2.579 +    qed
   2.580 +    finally show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   2.581 +      apply (simp only:)
   2.582 +      apply (safe intro!: countable_UN Diff)
   2.583 +      by (auto simp: sets_sigma intro!: sigma_sets.Basic)
   2.584 +  next
   2.585 +    fix a i assume "\<not> i < DIM('a)"
   2.586 +    then show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   2.587 +      using top by auto
   2.588 +  qed
   2.589 +  then show ?thesis by (intro sets_sigma_subset) auto
   2.590 +qed
   2.591 +
   2.592 +lemma atMost_span_atLeastAtMost:
   2.593 +  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>) \<subseteq>
   2.594 +   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>(a,b). {a..b})\<rparr>)"
   2.595 +  (is "_ \<subseteq> sets ?SIGMA")
   2.596 +proof -
   2.597 +  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   2.598 +  then interpret sigma_algebra ?SIGMA .
   2.599 +  { fix a::'a
   2.600 +    have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   2.601 +    proof (safe, simp_all add: eucl_le[where 'a='a])
   2.602 +      fix x
   2.603 +      from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
   2.604 +      guess k::nat .. note k = this
   2.605 +      { fix i assume "i < DIM('a)"
   2.606 +        with k have "- x$$i \<le> real k"
   2.607 +          by (subst (asm) Max_le_iff) (auto simp: field_simps)
   2.608 +        then have "- real k \<le> x$$i" by simp }
   2.609 +      then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
   2.610 +        by (auto intro!: exI[of _ k])
   2.611 +    qed
   2.612 +    have "{..a} \<in> sets ?SIGMA" unfolding *
   2.613 +      by (safe intro!: countable_UN)
   2.614 +         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   2.615 +  then show ?thesis by (intro sets_sigma_subset) auto
   2.616 +qed
   2.617 +
   2.618 +lemma algebra_eqI: assumes "sets A = sets (B::'a algebra)" "space A = space B"
   2.619 +  shows "A = B" using assms by auto
   2.620 +
   2.621 +lemma borel_eq_atMost:
   2.622 +  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> a. {.. a::'a\<Colon>ordered_euclidean_space})\<rparr>)"
   2.623 +    (is "_ = ?SIGMA")
   2.624 +proof (rule algebra_eqI, rule antisym)
   2.625 +  show "sets borel \<subseteq> sets ?SIGMA"
   2.626 +    using halfspace_le_span_atMost halfspace_span_halfspace_le open_span_halfspace
   2.627 +    by auto
   2.628 +  show "sets ?SIGMA \<subseteq> sets borel"
   2.629 +    by (rule borel.sets_sigma_subset) auto
   2.630 +qed auto
   2.631 +
   2.632 +lemma borel_eq_atLeastAtMost:
   2.633 +  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space, b). {a .. b})\<rparr>)"
   2.634 +   (is "_ = ?SIGMA")
   2.635 +proof (rule algebra_eqI, rule antisym)
   2.636 +  show "sets borel \<subseteq> sets ?SIGMA"
   2.637 +    using atMost_span_atLeastAtMost halfspace_le_span_atMost
   2.638 +      halfspace_span_halfspace_le open_span_halfspace
   2.639 +    by auto
   2.640 +  show "sets ?SIGMA \<subseteq> sets borel"
   2.641 +    by (rule borel.sets_sigma_subset) auto
   2.642 +qed auto
   2.643 +
   2.644 +lemma borel_eq_greaterThan:
   2.645 +  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {a <..})\<rparr>)"
   2.646 +   (is "_ = ?SIGMA")
   2.647 +proof (rule algebra_eqI, rule antisym)
   2.648 +  show "sets borel \<subseteq> sets ?SIGMA"
   2.649 +    using halfspace_le_span_greaterThan
   2.650 +      halfspace_span_halfspace_le open_span_halfspace
   2.651 +    by auto
   2.652 +  show "sets ?SIGMA \<subseteq> sets borel"
   2.653 +    by (rule borel.sets_sigma_subset) auto
   2.654 +qed auto
   2.655 +
   2.656 +lemma borel_eq_lessThan:
   2.657 +  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {..< a})\<rparr>)"
   2.658 +   (is "_ = ?SIGMA")
   2.659 +proof (rule algebra_eqI, rule antisym)
   2.660 +  show "sets borel \<subseteq> sets ?SIGMA"
   2.661 +    using halfspace_le_span_lessThan
   2.662 +      halfspace_span_halfspace_ge open_span_halfspace
   2.663 +    by auto
   2.664 +  show "sets ?SIGMA \<subseteq> sets borel"
   2.665 +    by (rule borel.sets_sigma_subset) auto
   2.666 +qed auto
   2.667 +
   2.668 +lemma borel_eq_greaterThanLessThan:
   2.669 +  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, b). {a <..< (b :: 'a \<Colon> ordered_euclidean_space)})\<rparr>)"
   2.670 +    (is "_ = ?SIGMA")
   2.671 +proof (rule algebra_eqI, rule antisym)
   2.672 +  show "sets ?SIGMA \<subseteq> sets borel"
   2.673 +    by (rule borel.sets_sigma_subset) auto
   2.674 +  show "sets borel \<subseteq> sets ?SIGMA"
   2.675 +  proof -
   2.676 +    have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   2.677 +    then interpret sigma_algebra ?SIGMA .
   2.678 +    { fix M :: "'a set" assume "M \<in> open"
   2.679 +      then have "open M" by (simp add: mem_def)
   2.680 +      have "M \<in> sets ?SIGMA"
   2.681 +        apply (subst open_UNION[OF `open M`])
   2.682 +        apply (safe intro!: countable_UN)
   2.683 +        by (auto simp add: sigma_def intro!: sigma_sets.Basic) }
   2.684 +    then show ?thesis
   2.685 +      unfolding borel_def by (intro sets_sigma_subset) auto
   2.686 +  qed
   2.687 +qed auto
   2.688 +
   2.689 +lemma borel_eq_halfspace_le:
   2.690 +  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i \<le> a})\<rparr>)"
   2.691 +   (is "_ = ?SIGMA")
   2.692 +proof (rule algebra_eqI, rule antisym)
   2.693 +  show "sets borel \<subseteq> sets ?SIGMA"
   2.694 +    using open_span_halfspace halfspace_span_halfspace_le by auto
   2.695 +  show "sets ?SIGMA \<subseteq> sets borel"
   2.696 +    by (rule borel.sets_sigma_subset) auto
   2.697 +qed auto
   2.698 +
   2.699 +lemma borel_eq_halfspace_less:
   2.700 +  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i < a})\<rparr>)"
   2.701 +   (is "_ = ?SIGMA")
   2.702 +proof (rule algebra_eqI, rule antisym)
   2.703 +  show "sets borel \<subseteq> sets ?SIGMA"
   2.704 +    using open_span_halfspace .
   2.705 +  show "sets ?SIGMA \<subseteq> sets borel"
   2.706 +    by (rule borel.sets_sigma_subset) auto
   2.707 +qed auto
   2.708 +
   2.709 +lemma borel_eq_halfspace_gt:
   2.710 +  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a < x$$i})\<rparr>)"
   2.711 +   (is "_ = ?SIGMA")
   2.712 +proof (rule algebra_eqI, rule antisym)
   2.713 +  show "sets borel \<subseteq> sets ?SIGMA"
   2.714 +    using halfspace_le_span_halfspace_gt open_span_halfspace halfspace_span_halfspace_le by auto
   2.715 +  show "sets ?SIGMA \<subseteq> sets borel"
   2.716 +    by (rule borel.sets_sigma_subset) auto
   2.717 +qed auto
   2.718 +
   2.719 +lemma borel_eq_halfspace_ge:
   2.720 +  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a \<le> x$$i})\<rparr>)"
   2.721 +   (is "_ = ?SIGMA")
   2.722 +proof (rule algebra_eqI, rule antisym)
   2.723 +  show "sets borel \<subseteq> sets ?SIGMA"
   2.724 +    using halfspace_span_halfspace_ge open_span_halfspace by auto
   2.725 +  show "sets ?SIGMA \<subseteq> sets borel"
   2.726 +    by (rule borel.sets_sigma_subset) auto
   2.727 +qed auto
   2.728 +
   2.729 +lemma (in sigma_algebra) borel_measurable_halfspacesI:
   2.730 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   2.731 +  assumes "borel = (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   2.732 +  and "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
   2.733 +  and "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
   2.734 +  shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a::real. S a i \<in> sets M)"
   2.735 +proof safe
   2.736 +  fix a :: real and i assume i: "i < DIM('c)" and f: "f \<in> borel_measurable M"
   2.737 +  then show "S a i \<in> sets M" unfolding assms
   2.738 +    by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1) sigma_def)
   2.739 +next
   2.740 +  assume a: "\<forall>i<DIM('c). \<forall>a. S a i \<in> sets M"
   2.741 +  { fix a i have "S a i \<in> sets M"
   2.742 +    proof cases
   2.743 +      assume "i < DIM('c)"
   2.744 +      with a show ?thesis unfolding assms(2) by simp
   2.745 +    next
   2.746 +      assume "\<not> i < DIM('c)"
   2.747 +      from assms(3)[OF this] show ?thesis .
   2.748 +    qed }
   2.749 +  then have "f \<in> measurable M (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   2.750 +    by (auto intro!: measurable_sigma simp: assms(2))
   2.751 +  then show "f \<in> borel_measurable M" unfolding measurable_def
   2.752 +    unfolding assms(1) by simp
   2.753 +qed
   2.754 +
   2.755 +lemma (in sigma_algebra) borel_measurable_iff_halfspace_le:
   2.756 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   2.757 +  shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i \<le> a} \<in> sets M)"
   2.758 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   2.759 +
   2.760 +lemma (in sigma_algebra) borel_measurable_iff_halfspace_less:
   2.761 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   2.762 +  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i < a} \<in> sets M)"
   2.763 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   2.764 +
   2.765 +lemma (in sigma_algebra) borel_measurable_iff_halfspace_ge:
   2.766 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   2.767 +  shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a \<le> f w $$ i} \<in> sets M)"
   2.768 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   2.769 +
   2.770 +lemma (in sigma_algebra) borel_measurable_iff_halfspace_greater:
   2.771 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   2.772 +  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a < f w $$ i} \<in> sets M)"
   2.773 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_gt]) auto
   2.774 +
   2.775 +lemma (in sigma_algebra) borel_measurable_iff_le:
   2.776 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   2.777 +  using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   2.778 +
   2.779 +lemma (in sigma_algebra) borel_measurable_iff_less:
   2.780 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   2.781 +  using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   2.782 +
   2.783 +lemma (in sigma_algebra) borel_measurable_iff_ge:
   2.784 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   2.785 +  using borel_measurable_iff_halfspace_ge[where 'c=real] by simp
   2.786 +
   2.787 +lemma (in sigma_algebra) borel_measurable_iff_greater:
   2.788 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   2.789 +  using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   2.790 +
   2.791 +lemma borel_measureable_euclidean_component:
   2.792 +  "(\<lambda>x::'a::euclidean_space. x $$ i) \<in> borel_measurable borel"
   2.793 +  unfolding borel_def[where 'a=real]
   2.794 +proof (rule borel.measurable_sigma, simp_all)
   2.795 +  fix S::"real set" assume "S \<in> open" then have "open S" unfolding mem_def .
   2.796 +  from open_vimage_euclidean_component[OF this]
   2.797 +  show "(\<lambda>x. x $$ i) -` S \<in> sets borel"
   2.798 +    by (auto intro: borel_open)
   2.799 +qed
   2.800 +
   2.801 +lemma (in sigma_algebra) borel_measureable_euclidean_space:
   2.802 +  fixes f :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   2.803 +  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M)"
   2.804 +proof safe
   2.805 +  fix i assume "f \<in> borel_measurable M"
   2.806 +  then show "(\<lambda>x. f x $$ i) \<in> borel_measurable M"
   2.807 +    using measurable_comp[of f _ _ "\<lambda>x. x $$ i", unfolded comp_def]
   2.808 +    by (auto intro: borel_measureable_euclidean_component)
   2.809 +next
   2.810 +  assume f: "\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M"
   2.811 +  then show "f \<in> borel_measurable M"
   2.812 +    unfolding borel_measurable_iff_halfspace_le by auto
   2.813 +qed
   2.814 +
   2.815 +subsection "Borel measurable operators"
   2.816 +
   2.817 +lemma (in sigma_algebra) affine_borel_measurable_vector:
   2.818 +  fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   2.819 +  assumes "f \<in> borel_measurable M"
   2.820 +  shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   2.821 +proof (rule borel_measurableI)
   2.822 +  fix S :: "'x set" assume "open S"
   2.823 +  show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   2.824 +  proof cases
   2.825 +    assume "b \<noteq> 0"
   2.826 +    with `open S` have "((\<lambda>x. (- a + x) /\<^sub>R b) ` S) \<in> open" (is "?S \<in> open")
   2.827 +      by (auto intro!: open_affinity simp: scaleR.add_right mem_def)
   2.828 +    hence "?S \<in> sets borel"
   2.829 +      unfolding borel_def by (auto simp: sigma_def intro!: sigma_sets.Basic)
   2.830 +    moreover
   2.831 +    from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   2.832 +      apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   2.833 +    ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   2.834 +      by auto
   2.835 +  qed simp
   2.836 +qed
   2.837 +
   2.838 +lemma (in sigma_algebra) affine_borel_measurable:
   2.839 +  fixes g :: "'a \<Rightarrow> real"
   2.840 +  assumes g: "g \<in> borel_measurable M"
   2.841 +  shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
   2.842 +  using affine_borel_measurable_vector[OF assms] by (simp add: mult_commute)
   2.843 +
   2.844 +lemma (in sigma_algebra) borel_measurable_add[simp, intro]:
   2.845 +  fixes f :: "'a \<Rightarrow> real"
   2.846 +  assumes f: "f \<in> borel_measurable M"
   2.847 +  assumes g: "g \<in> borel_measurable M"
   2.848 +  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   2.849 +proof -
   2.850 +  have 1: "\<And>a. {w\<in>space M. a \<le> f w + g w} = {w \<in> space M. a + g w * -1 \<le> f w}"
   2.851 +    by auto
   2.852 +  have "\<And>a. (\<lambda>w. a + (g w) * -1) \<in> borel_measurable M"
   2.853 +    by (rule affine_borel_measurable [OF g])
   2.854 +  then have "\<And>a. {w \<in> space M. (\<lambda>w. a + (g w) * -1)(w) \<le> f w} \<in> sets M" using f
   2.855 +    by auto
   2.856 +  then have "\<And>a. {w \<in> space M. a \<le> f w + g w} \<in> sets M"
   2.857 +    by (simp add: 1)
   2.858 +  then show ?thesis
   2.859 +    by (simp add: borel_measurable_iff_ge)
   2.860 +qed
   2.861 +
   2.862 +lemma (in sigma_algebra) borel_measurable_square:
   2.863 +  fixes f :: "'a \<Rightarrow> real"
   2.864 +  assumes f: "f \<in> borel_measurable M"
   2.865 +  shows "(\<lambda>x. (f x)^2) \<in> borel_measurable M"
   2.866 +proof -
   2.867 +  {
   2.868 +    fix a
   2.869 +    have "{w \<in> space M. (f w)\<twosuperior> \<le> a} \<in> sets M"
   2.870 +    proof (cases rule: linorder_cases [of a 0])
   2.871 +      case less
   2.872 +      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} = {}"
   2.873 +        by auto (metis less order_le_less_trans power2_less_0)
   2.874 +      also have "... \<in> sets M"
   2.875 +        by (rule empty_sets)
   2.876 +      finally show ?thesis .
   2.877 +    next
   2.878 +      case equal
   2.879 +      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   2.880 +             {w \<in> space M. f w \<le> 0} \<inter> {w \<in> space M. 0 \<le> f w}"
   2.881 +        by auto
   2.882 +      also have "... \<in> sets M"
   2.883 +        apply (insert f)
   2.884 +        apply (rule Int)
   2.885 +        apply (simp add: borel_measurable_iff_le)
   2.886 +        apply (simp add: borel_measurable_iff_ge)
   2.887 +        done
   2.888 +      finally show ?thesis .
   2.889 +    next
   2.890 +      case greater
   2.891 +      have "\<forall>x. (f x ^ 2 \<le> sqrt a ^ 2) = (- sqrt a  \<le> f x & f x \<le> sqrt a)"
   2.892 +        by (metis abs_le_interval_iff abs_of_pos greater real_sqrt_abs
   2.893 +                  real_sqrt_le_iff real_sqrt_power)
   2.894 +      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   2.895 +             {w \<in> space M. -(sqrt a) \<le> f w} \<inter> {w \<in> space M. f w \<le> sqrt a}"
   2.896 +        using greater by auto
   2.897 +      also have "... \<in> sets M"
   2.898 +        apply (insert f)
   2.899 +        apply (rule Int)
   2.900 +        apply (simp add: borel_measurable_iff_ge)
   2.901 +        apply (simp add: borel_measurable_iff_le)
   2.902 +        done
   2.903 +      finally show ?thesis .
   2.904 +    qed
   2.905 +  }
   2.906 +  thus ?thesis by (auto simp add: borel_measurable_iff_le)
   2.907 +qed
   2.908 +
   2.909 +lemma times_eq_sum_squares:
   2.910 +   fixes x::real
   2.911 +   shows"x*y = ((x+y)^2)/4 - ((x-y)^ 2)/4"
   2.912 +by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric])
   2.913 +
   2.914 +lemma (in sigma_algebra) borel_measurable_uminus[simp, intro]:
   2.915 +  fixes g :: "'a \<Rightarrow> real"
   2.916 +  assumes g: "g \<in> borel_measurable M"
   2.917 +  shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   2.918 +proof -
   2.919 +  have "(\<lambda>x. - g x) = (\<lambda>x. 0 + (g x) * -1)"
   2.920 +    by simp
   2.921 +  also have "... \<in> borel_measurable M"
   2.922 +    by (fast intro: affine_borel_measurable g)
   2.923 +  finally show ?thesis .
   2.924 +qed
   2.925 +
   2.926 +lemma (in sigma_algebra) borel_measurable_times[simp, intro]:
   2.927 +  fixes f :: "'a \<Rightarrow> real"
   2.928 +  assumes f: "f \<in> borel_measurable M"
   2.929 +  assumes g: "g \<in> borel_measurable M"
   2.930 +  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   2.931 +proof -
   2.932 +  have 1: "(\<lambda>x. 0 + (f x + g x)\<twosuperior> * inverse 4) \<in> borel_measurable M"
   2.933 +    using assms by (fast intro: affine_borel_measurable borel_measurable_square)
   2.934 +  have "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) =
   2.935 +        (\<lambda>x. 0 + ((f x + -g x) ^ 2 * inverse -4))"
   2.936 +    by (simp add: minus_divide_right)
   2.937 +  also have "... \<in> borel_measurable M"
   2.938 +    using f g by (fast intro: affine_borel_measurable borel_measurable_square f g)
   2.939 +  finally have 2: "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) \<in> borel_measurable M" .
   2.940 +  show ?thesis
   2.941 +    apply (simp add: times_eq_sum_squares diff_minus)
   2.942 +    using 1 2 by simp
   2.943 +qed
   2.944 +
   2.945 +lemma (in sigma_algebra) borel_measurable_diff[simp, intro]:
   2.946 +  fixes f :: "'a \<Rightarrow> real"
   2.947 +  assumes f: "f \<in> borel_measurable M"
   2.948 +  assumes g: "g \<in> borel_measurable M"
   2.949 +  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   2.950 +  unfolding diff_minus using assms by fast
   2.951 +
   2.952 +lemma (in sigma_algebra) borel_measurable_setsum[simp, intro]:
   2.953 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   2.954 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   2.955 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   2.956 +proof cases
   2.957 +  assume "finite S"
   2.958 +  thus ?thesis using assms by induct auto
   2.959 +qed simp
   2.960 +
   2.961 +lemma (in sigma_algebra) borel_measurable_inverse[simp, intro]:
   2.962 +  fixes f :: "'a \<Rightarrow> real"
   2.963 +  assumes "f \<in> borel_measurable M"
   2.964 +  shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   2.965 +  unfolding borel_measurable_iff_ge unfolding inverse_eq_divide
   2.966 +proof safe
   2.967 +  fix a :: real
   2.968 +  have *: "{w \<in> space M. a \<le> 1 / f w} =
   2.969 +      ({w \<in> space M. 0 < f w} \<inter> {w \<in> space M. a * f w \<le> 1}) \<union>
   2.970 +      ({w \<in> space M. f w < 0} \<inter> {w \<in> space M. 1 \<le> a * f w}) \<union>
   2.971 +      ({w \<in> space M. f w = 0} \<inter> {w \<in> space M. a \<le> 0})" by (auto simp: le_divide_eq)
   2.972 +  show "{w \<in> space M. a \<le> 1 / f w} \<in> sets M" using assms unfolding *
   2.973 +    by (auto intro!: Int Un)
   2.974 +qed
   2.975 +
   2.976 +lemma (in sigma_algebra) borel_measurable_divide[simp, intro]:
   2.977 +  fixes f :: "'a \<Rightarrow> real"
   2.978 +  assumes "f \<in> borel_measurable M"
   2.979 +  and "g \<in> borel_measurable M"
   2.980 +  shows "(\<lambda>x. f x / g x) \<in> borel_measurable M"
   2.981 +  unfolding field_divide_inverse
   2.982 +  by (rule borel_measurable_inverse borel_measurable_times assms)+
   2.983 +
   2.984 +lemma (in sigma_algebra) borel_measurable_max[intro, simp]:
   2.985 +  fixes f g :: "'a \<Rightarrow> real"
   2.986 +  assumes "f \<in> borel_measurable M"
   2.987 +  assumes "g \<in> borel_measurable M"
   2.988 +  shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   2.989 +  unfolding borel_measurable_iff_le
   2.990 +proof safe
   2.991 +  fix a
   2.992 +  have "{x \<in> space M. max (g x) (f x) \<le> a} =
   2.993 +    {x \<in> space M. g x \<le> a} \<inter> {x \<in> space M. f x \<le> a}" by auto
   2.994 +  thus "{x \<in> space M. max (g x) (f x) \<le> a} \<in> sets M"
   2.995 +    using assms unfolding borel_measurable_iff_le
   2.996 +    by (auto intro!: Int)
   2.997 +qed
   2.998 +
   2.999 +lemma (in sigma_algebra) borel_measurable_min[intro, simp]:
  2.1000 +  fixes f g :: "'a \<Rightarrow> real"
  2.1001 +  assumes "f \<in> borel_measurable M"
  2.1002 +  assumes "g \<in> borel_measurable M"
  2.1003 +  shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  2.1004 +  unfolding borel_measurable_iff_ge
  2.1005 +proof safe
  2.1006 +  fix a
  2.1007 +  have "{x \<in> space M. a \<le> min (g x) (f x)} =
  2.1008 +    {x \<in> space M. a \<le> g x} \<inter> {x \<in> space M. a \<le> f x}" by auto
  2.1009 +  thus "{x \<in> space M. a \<le> min (g x) (f x)} \<in> sets M"
  2.1010 +    using assms unfolding borel_measurable_iff_ge
  2.1011 +    by (auto intro!: Int)
  2.1012 +qed
  2.1013 +
  2.1014 +lemma (in sigma_algebra) borel_measurable_abs[simp, intro]:
  2.1015 +  assumes "f \<in> borel_measurable M"
  2.1016 +  shows "(\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
  2.1017 +proof -
  2.1018 +  have *: "\<And>x. \<bar>f x\<bar> = max 0 (f x) + max 0 (- f x)" by (simp add: max_def)
  2.1019 +  show ?thesis unfolding * using assms by auto
  2.1020 +qed
  2.1021 +
  2.1022 +section "Borel space over the real line with infinity"
  2.1023 +
  2.1024 +lemma borel_Real_measurable:
  2.1025 +  "A \<in> sets borel \<Longrightarrow> Real -` A \<in> sets borel"
  2.1026 +proof (rule borel_measurable_translate)
  2.1027 +  fix B :: "pinfreal set" assume "open B"
  2.1028 +  then obtain T x where T: "open T" "Real ` (T \<inter> {0..}) = B - {\<omega>}" and
  2.1029 +    x: "\<omega> \<in> B \<Longrightarrow> 0 \<le> x" "\<omega> \<in> B \<Longrightarrow> {Real x <..} \<subseteq> B"
  2.1030 +    unfolding open_pinfreal_def by blast
  2.1031 +
  2.1032 +  have "Real -` B = Real -` (B - {\<omega>})" by auto
  2.1033 +  also have "\<dots> = Real -` (Real ` (T \<inter> {0..}))" using T by simp
  2.1034 +  also have "\<dots> = (if 0 \<in> T then T \<union> {.. 0} else T \<inter> {0..})"
  2.1035 +    apply (auto simp add: Real_eq_Real image_iff)
  2.1036 +    apply (rule_tac x="max 0 x" in bexI)
  2.1037 +    by (auto simp: max_def)
  2.1038 +  finally show "Real -` B \<in> sets borel"
  2.1039 +    using `open T` by auto
  2.1040 +qed simp
  2.1041 +
  2.1042 +lemma borel_real_measurable:
  2.1043 +  "A \<in> sets borel \<Longrightarrow> (real -` A :: pinfreal set) \<in> sets borel"
  2.1044 +proof (rule borel_measurable_translate)
  2.1045 +  fix B :: "real set" assume "open B"
  2.1046 +  { fix x have "0 < real x \<longleftrightarrow> (\<exists>r>0. x = Real r)" by (cases x) auto }
  2.1047 +  note Ex_less_real = this
  2.1048 +  have *: "real -` B = (if 0 \<in> B then real -` (B \<inter> {0 <..}) \<union> {0, \<omega>} else real -` (B \<inter> {0 <..}))"
  2.1049 +    by (force simp: Ex_less_real)
  2.1050 +
  2.1051 +  have "open (real -` (B \<inter> {0 <..}) :: pinfreal set)"
  2.1052 +    unfolding open_pinfreal_def using `open B`
  2.1053 +    by (auto intro!: open_Int exI[of _ "B \<inter> {0 <..}"] simp: image_iff Ex_less_real)
  2.1054 +  then show "(real -` B :: pinfreal set) \<in> sets borel" unfolding * by auto
  2.1055 +qed simp
  2.1056 +
  2.1057 +lemma (in sigma_algebra) borel_measurable_Real[intro, simp]:
  2.1058 +  assumes "f \<in> borel_measurable M"
  2.1059 +  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
  2.1060 +  unfolding in_borel_measurable_borel
  2.1061 +proof safe
  2.1062 +  fix S :: "pinfreal set" assume "S \<in> sets borel"
  2.1063 +  from borel_Real_measurable[OF this]
  2.1064 +  have "(Real \<circ> f) -` S \<inter> space M \<in> sets M"
  2.1065 +    using assms
  2.1066 +    unfolding vimage_compose in_borel_measurable_borel
  2.1067 +    by auto
  2.1068 +  thus "(\<lambda>x. Real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
  2.1069 +qed
  2.1070 +
  2.1071 +lemma (in sigma_algebra) borel_measurable_real[intro, simp]:
  2.1072 +  fixes f :: "'a \<Rightarrow> pinfreal"
  2.1073 +  assumes "f \<in> borel_measurable M"
  2.1074 +  shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
  2.1075 +  unfolding in_borel_measurable_borel
  2.1076 +proof safe
  2.1077 +  fix S :: "real set" assume "S \<in> sets borel"
  2.1078 +  from borel_real_measurable[OF this]
  2.1079 +  have "(real \<circ> f) -` S \<inter> space M \<in> sets M"
  2.1080 +    using assms
  2.1081 +    unfolding vimage_compose in_borel_measurable_borel
  2.1082 +    by auto
  2.1083 +  thus "(\<lambda>x. real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
  2.1084 +qed
  2.1085 +
  2.1086 +lemma (in sigma_algebra) borel_measurable_Real_eq:
  2.1087 +  assumes "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  2.1088 +  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  2.1089 +proof
  2.1090 +  have [simp]: "(\<lambda>x. Real (f x)) -` {\<omega>} \<inter> space M = {}"
  2.1091 +    by auto
  2.1092 +  assume "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
  2.1093 +  hence "(\<lambda>x. real (Real (f x))) \<in> borel_measurable M"
  2.1094 +    by (rule borel_measurable_real)
  2.1095 +  moreover have "\<And>x. x \<in> space M \<Longrightarrow> real (Real (f x)) = f x"
  2.1096 +    using assms by auto
  2.1097 +  ultimately show "f \<in> borel_measurable M"
  2.1098 +    by (simp cong: measurable_cong)
  2.1099 +qed auto
  2.1100 +
  2.1101 +lemma (in sigma_algebra) borel_measurable_pinfreal_eq_real:
  2.1102 +  "f \<in> borel_measurable M \<longleftrightarrow>
  2.1103 +    ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<omega>} \<inter> space M \<in> sets M)"
  2.1104 +proof safe
  2.1105 +  assume "f \<in> borel_measurable M"
  2.1106 +  then show "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
  2.1107 +    by (auto intro: borel_measurable_vimage borel_measurable_real)
  2.1108 +next
  2.1109 +  assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
  2.1110 +  have "f -` {\<omega>} \<inter> space M = {x\<in>space M. f x = \<omega>}" by auto
  2.1111 +  with * have **: "{x\<in>space M. f x = \<omega>} \<in> sets M" by simp
  2.1112 +  have f: "f = (\<lambda>x. if f x = \<omega> then \<omega> else Real (real (f x)))"
  2.1113 +    by (simp add: fun_eq_iff Real_real)
  2.1114 +  show "f \<in> borel_measurable M"
  2.1115 +    apply (subst f)
  2.1116 +    apply (rule measurable_If)
  2.1117 +    using * ** by auto
  2.1118 +qed
  2.1119 +
  2.1120 +lemma (in sigma_algebra) less_eq_ge_measurable:
  2.1121 +  fixes f :: "'a \<Rightarrow> 'c::linorder"
  2.1122 +  shows "{x\<in>space M. a < f x} \<in> sets M \<longleftrightarrow> {x\<in>space M. f x \<le> a} \<in> sets M"
  2.1123 +proof
  2.1124 +  assume "{x\<in>space M. f x \<le> a} \<in> sets M"
  2.1125 +  moreover have "{x\<in>space M. a < f x} = space M - {x\<in>space M. f x \<le> a}" by auto
  2.1126 +  ultimately show "{x\<in>space M. a < f x} \<in> sets M" by auto
  2.1127 +next
  2.1128 +  assume "{x\<in>space M. a < f x} \<in> sets M"
  2.1129 +  moreover have "{x\<in>space M. f x \<le> a} = space M - {x\<in>space M. a < f x}" by auto
  2.1130 +  ultimately show "{x\<in>space M. f x \<le> a} \<in> sets M" by auto
  2.1131 +qed
  2.1132 +
  2.1133 +lemma (in sigma_algebra) greater_eq_le_measurable:
  2.1134 +  fixes f :: "'a \<Rightarrow> 'c::linorder"
  2.1135 +  shows "{x\<in>space M. f x < a} \<in> sets M \<longleftrightarrow> {x\<in>space M. a \<le> f x} \<in> sets M"
  2.1136 +proof
  2.1137 +  assume "{x\<in>space M. a \<le> f x} \<in> sets M"
  2.1138 +  moreover have "{x\<in>space M. f x < a} = space M - {x\<in>space M. a \<le> f x}" by auto
  2.1139 +  ultimately show "{x\<in>space M. f x < a} \<in> sets M" by auto
  2.1140 +next
  2.1141 +  assume "{x\<in>space M. f x < a} \<in> sets M"
  2.1142 +  moreover have "{x\<in>space M. a \<le> f x} = space M - {x\<in>space M. f x < a}" by auto
  2.1143 +  ultimately show "{x\<in>space M. a \<le> f x} \<in> sets M" by auto
  2.1144 +qed
  2.1145 +
  2.1146 +lemma (in sigma_algebra) less_eq_le_pinfreal_measurable:
  2.1147 +  fixes f :: "'a \<Rightarrow> pinfreal"
  2.1148 +  shows "(\<forall>a. {x\<in>space M. a < f x} \<in> sets M) \<longleftrightarrow> (\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M)"
  2.1149 +proof
  2.1150 +  assume a: "\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M"
  2.1151 +  show "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
  2.1152 +  proof
  2.1153 +    fix a show "{x \<in> space M. a < f x} \<in> sets M"
  2.1154 +    proof (cases a)
  2.1155 +      case (preal r)
  2.1156 +      have "{x\<in>space M. a < f x} = (\<Union>i. {x\<in>space M. a + inverse (of_nat (Suc i)) \<le> f x})"
  2.1157 +      proof safe
  2.1158 +        fix x assume "a < f x" and [simp]: "x \<in> space M"
  2.1159 +        with ex_pinfreal_inverse_of_nat_Suc_less[of "f x - a"]
  2.1160 +        obtain n where "a + inverse (of_nat (Suc n)) < f x"
  2.1161 +          by (cases "f x", auto simp: pinfreal_minus_order)
  2.1162 +        then have "a + inverse (of_nat (Suc n)) \<le> f x" by simp
  2.1163 +        then show "x \<in> (\<Union>i. {x \<in> space M. a + inverse (of_nat (Suc i)) \<le> f x})"
  2.1164 +          by auto
  2.1165 +      next
  2.1166 +        fix i x assume [simp]: "x \<in> space M"
  2.1167 +        have "a < a + inverse (of_nat (Suc i))" using preal by auto
  2.1168 +        also assume "a + inverse (of_nat (Suc i)) \<le> f x"
  2.1169 +        finally show "a < f x" .
  2.1170 +      qed
  2.1171 +      with a show ?thesis by auto
  2.1172 +    qed simp
  2.1173 +  qed
  2.1174 +next
  2.1175 +  assume a': "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
  2.1176 +  then have a: "\<forall>a. {x \<in> space M. f x \<le> a} \<in> sets M" unfolding less_eq_ge_measurable .
  2.1177 +  show "\<forall>a. {x \<in> space M. a \<le> f x} \<in> sets M" unfolding greater_eq_le_measurable[symmetric]
  2.1178 +  proof
  2.1179 +    fix a show "{x \<in> space M. f x < a} \<in> sets M"
  2.1180 +    proof (cases a)
  2.1181 +      case (preal r)
  2.1182 +      show ?thesis
  2.1183 +      proof cases
  2.1184 +        assume "a = 0" then show ?thesis by simp
  2.1185 +      next
  2.1186 +        assume "a \<noteq> 0"
  2.1187 +        have "{x\<in>space M. f x < a} = (\<Union>i. {x\<in>space M. f x \<le> a - inverse (of_nat (Suc i))})"
  2.1188 +        proof safe
  2.1189 +          fix x assume "f x < a" and [simp]: "x \<in> space M"
  2.1190 +          with ex_pinfreal_inverse_of_nat_Suc_less[of "a - f x"]
  2.1191 +          obtain n where "inverse (of_nat (Suc n)) < a - f x"
  2.1192 +            using preal by (cases "f x") auto
  2.1193 +          then have "f x \<le> a - inverse (of_nat (Suc n)) "
  2.1194 +            using preal by (cases "f x") (auto split: split_if_asm)
  2.1195 +          then show "x \<in> (\<Union>i. {x \<in> space M. f x \<le> a - inverse (of_nat (Suc i))})"
  2.1196 +            by auto
  2.1197 +        next
  2.1198 +          fix i x assume [simp]: "x \<in> space M"
  2.1199 +          assume "f x \<le> a - inverse (of_nat (Suc i))"
  2.1200 +          also have "\<dots> < a" using `a \<noteq> 0` preal by auto
  2.1201 +          finally show "f x < a" .
  2.1202 +        qed
  2.1203 +        with a show ?thesis by auto
  2.1204 +      qed
  2.1205 +    next
  2.1206 +      case infinite
  2.1207 +      have "f -` {\<omega>} \<inter> space M = (\<Inter>n. {x\<in>space M. of_nat n < f x})"
  2.1208 +      proof (safe, simp_all, safe)
  2.1209 +        fix x assume *: "\<forall>n::nat. Real (real n) < f x"
  2.1210 +        show "f x = \<omega>"    proof (rule ccontr)
  2.1211 +          assume "f x \<noteq> \<omega>"
  2.1212 +          with real_arch_lt[of "real (f x)"] obtain n where "f x < of_nat n"
  2.1213 +            by (auto simp: pinfreal_noteq_omega_Ex)
  2.1214 +          with *[THEN spec, of n] show False by auto
  2.1215 +        qed
  2.1216 +      qed
  2.1217 +      with a' have \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" by auto
  2.1218 +      moreover have "{x \<in> space M. f x < a} = space M - f -` {\<omega>} \<inter> space M"
  2.1219 +        using infinite by auto
  2.1220 +      ultimately show ?thesis by auto
  2.1221 +    qed
  2.1222 +  qed
  2.1223 +qed
  2.1224 +
  2.1225 +lemma (in sigma_algebra) borel_measurable_pinfreal_iff_greater:
  2.1226 +  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a < f x} \<in> sets M)"
  2.1227 +proof safe
  2.1228 +  fix a assume f: "f \<in> borel_measurable M"
  2.1229 +  have "{x\<in>space M. a < f x} = f -` {a <..} \<inter> space M" by auto
  2.1230 +  with f show "{x\<in>space M. a < f x} \<in> sets M"
  2.1231 +    by (auto intro!: measurable_sets)
  2.1232 +next
  2.1233 +  assume *: "\<forall>a. {x\<in>space M. a < f x} \<in> sets M"
  2.1234 +  hence **: "\<forall>a. {x\<in>space M. f x < a} \<in> sets M"
  2.1235 +    unfolding less_eq_le_pinfreal_measurable
  2.1236 +    unfolding greater_eq_le_measurable .
  2.1237 +
  2.1238 +  show "f \<in> borel_measurable M" unfolding borel_measurable_pinfreal_eq_real borel_measurable_iff_greater
  2.1239 +  proof safe
  2.1240 +    have "f -` {\<omega>} \<inter> space M = space M - {x\<in>space M. f x < \<omega>}" by auto
  2.1241 +    then show \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" using ** by auto
  2.1242 +
  2.1243 +    fix a
  2.1244 +    have "{w \<in> space M. a < real (f w)} =
  2.1245 +      (if 0 \<le> a then {w\<in>space M. Real a < f w} - (f -` {\<omega>} \<inter> space M) else space M)"
  2.1246 +    proof (split split_if, safe del: notI)
  2.1247 +      fix x assume "0 \<le> a"
  2.1248 +      { assume "a < real (f x)" then show "Real a < f x" "x \<notin> f -` {\<omega>} \<inter> space M"
  2.1249 +          using `0 \<le> a` by (cases "f x", auto) }
  2.1250 +      { assume "Real a < f x" "x \<notin> f -` {\<omega>}" then show "a < real (f x)"
  2.1251 +          using `0 \<le> a` by (cases "f x", auto) }
  2.1252 +    next
  2.1253 +      fix x assume "\<not> 0 \<le> a" then show "a < real (f x)" by (cases "f x") auto
  2.1254 +    qed
  2.1255 +    then show "{w \<in> space M. a < real (f w)} \<in> sets M"
  2.1256 +      using \<omega> * by (auto intro!: Diff)
  2.1257 +  qed
  2.1258 +qed
  2.1259 +
  2.1260 +lemma (in sigma_algebra) borel_measurable_pinfreal_iff_less:
  2.1261 +  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x < a} \<in> sets M)"
  2.1262 +  using borel_measurable_pinfreal_iff_greater unfolding less_eq_le_pinfreal_measurable greater_eq_le_measurable .
  2.1263 +
  2.1264 +lemma (in sigma_algebra) borel_measurable_pinfreal_iff_le:
  2.1265 +  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x \<le> a} \<in> sets M)"
  2.1266 +  using borel_measurable_pinfreal_iff_greater unfolding less_eq_ge_measurable .
  2.1267 +
  2.1268 +lemma (in sigma_algebra) borel_measurable_pinfreal_iff_ge:
  2.1269 +  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M)"
  2.1270 +  using borel_measurable_pinfreal_iff_greater unfolding less_eq_le_pinfreal_measurable .
  2.1271 +
  2.1272 +lemma (in sigma_algebra) borel_measurable_pinfreal_eq_const:
  2.1273 +  fixes f :: "'a \<Rightarrow> pinfreal" assumes "f \<in> borel_measurable M"
  2.1274 +  shows "{x\<in>space M. f x = c} \<in> sets M"
  2.1275 +proof -
  2.1276 +  have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
  2.1277 +  then show ?thesis using assms by (auto intro!: measurable_sets)
  2.1278 +qed
  2.1279 +
  2.1280 +lemma (in sigma_algebra) borel_measurable_pinfreal_neq_const:
  2.1281 +  fixes f :: "'a \<Rightarrow> pinfreal"
  2.1282 +  assumes "f \<in> borel_measurable M"
  2.1283 +  shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  2.1284 +proof -
  2.1285 +  have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
  2.1286 +  then show ?thesis using assms by (auto intro!: measurable_sets)
  2.1287 +qed
  2.1288 +
  2.1289 +lemma (in sigma_algebra) borel_measurable_pinfreal_less[intro,simp]:
  2.1290 +  fixes f g :: "'a \<Rightarrow> pinfreal"
  2.1291 +  assumes f: "f \<in> borel_measurable M"
  2.1292 +  assumes g: "g \<in> borel_measurable M"
  2.1293 +  shows "{x \<in> space M. f x < g x} \<in> sets M"
  2.1294 +proof -
  2.1295 +  have "(\<lambda>x. real (f x)) \<in> borel_measurable M"
  2.1296 +    "(\<lambda>x. real (g x)) \<in> borel_measurable M"
  2.1297 +    using assms by (auto intro!: borel_measurable_real)
  2.1298 +  from borel_measurable_less[OF this]
  2.1299 +  have "{x \<in> space M. real (f x) < real (g x)} \<in> sets M" .
  2.1300 +  moreover have "{x \<in> space M. f x \<noteq> \<omega>} \<in> sets M" using f by (rule borel_measurable_pinfreal_neq_const)
  2.1301 +  moreover have "{x \<in> space M. g x = \<omega>} \<in> sets M" using g by (rule borel_measurable_pinfreal_eq_const)
  2.1302 +  moreover have "{x \<in> space M. g x \<noteq> \<omega>} \<in> sets M" using g by (rule borel_measurable_pinfreal_neq_const)
  2.1303 +  moreover have "{x \<in> space M. f x < g x} = ({x \<in> space M. g x = \<omega>} \<inter> {x \<in> space M. f x \<noteq> \<omega>}) \<union>
  2.1304 +    ({x \<in> space M. g x \<noteq> \<omega>} \<inter> {x \<in> space M. f x \<noteq> \<omega>} \<inter> {x \<in> space M. real (f x) < real (g x)})"
  2.1305 +    by (auto simp: real_of_pinfreal_strict_mono_iff)
  2.1306 +  ultimately show ?thesis by auto
  2.1307 +qed
  2.1308 +
  2.1309 +lemma (in sigma_algebra) borel_measurable_pinfreal_le[intro,simp]:
  2.1310 +  fixes f :: "'a \<Rightarrow> pinfreal"
  2.1311 +  assumes f: "f \<in> borel_measurable M"
  2.1312 +  assumes g: "g \<in> borel_measurable M"
  2.1313 +  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
  2.1314 +proof -
  2.1315 +  have "{x \<in> space M. f x \<le> g x} = space M - {x \<in> space M. g x < f x}" by auto
  2.1316 +  then show ?thesis using g f by auto
  2.1317 +qed
  2.1318 +
  2.1319 +lemma (in sigma_algebra) borel_measurable_pinfreal_eq[intro,simp]:
  2.1320 +  fixes f :: "'a \<Rightarrow> pinfreal"
  2.1321 +  assumes f: "f \<in> borel_measurable M"
  2.1322 +  assumes g: "g \<in> borel_measurable M"
  2.1323 +  shows "{w \<in> space M. f w = g w} \<in> sets M"
  2.1324 +proof -
  2.1325 +  have "{x \<in> space M. f x = g x} = {x \<in> space M. g x \<le> f x} \<inter> {x \<in> space M. f x \<le> g x}" by auto
  2.1326 +  then show ?thesis using g f by auto
  2.1327 +qed
  2.1328 +
  2.1329 +lemma (in sigma_algebra) borel_measurable_pinfreal_neq[intro,simp]:
  2.1330 +  fixes f :: "'a \<Rightarrow> pinfreal"
  2.1331 +  assumes f: "f \<in> borel_measurable M"
  2.1332 +  assumes g: "g \<in> borel_measurable M"
  2.1333 +  shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
  2.1334 +proof -
  2.1335 +  have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}" by auto
  2.1336 +  thus ?thesis using f g by auto
  2.1337 +qed
  2.1338 +
  2.1339 +lemma (in sigma_algebra) borel_measurable_pinfreal_add[intro, simp]:
  2.1340 +  fixes f :: "'a \<Rightarrow> pinfreal"
  2.1341 +  assumes measure: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  2.1342 +  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  2.1343 +proof -
  2.1344 +  have *: "(\<lambda>x. f x + g x) =
  2.1345 +     (\<lambda>x. if f x = \<omega> then \<omega> else if g x = \<omega> then \<omega> else Real (real (f x) + real (g x)))"
  2.1346 +     by (auto simp: fun_eq_iff pinfreal_noteq_omega_Ex)
  2.1347 +  show ?thesis using assms unfolding *
  2.1348 +    by (auto intro!: measurable_If)
  2.1349 +qed
  2.1350 +
  2.1351 +lemma (in sigma_algebra) borel_measurable_pinfreal_times[intro, simp]:
  2.1352 +  fixes f :: "'a \<Rightarrow> pinfreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  2.1353 +  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  2.1354 +proof -
  2.1355 +  have *: "(\<lambda>x. f x * g x) =
  2.1356 +     (\<lambda>x. if f x = 0 then 0 else if g x = 0 then 0 else if f x = \<omega> then \<omega> else if g x = \<omega> then \<omega> else
  2.1357 +      Real (real (f x) * real (g x)))"
  2.1358 +     by (auto simp: fun_eq_iff pinfreal_noteq_omega_Ex)
  2.1359 +  show ?thesis using assms unfolding *
  2.1360 +    by (auto intro!: measurable_If)
  2.1361 +qed
  2.1362 +
  2.1363 +lemma (in sigma_algebra) borel_measurable_pinfreal_setsum[simp, intro]:
  2.1364 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> pinfreal"
  2.1365 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  2.1366 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  2.1367 +proof cases
  2.1368 +  assume "finite S"
  2.1369 +  thus ?thesis using assms
  2.1370 +    by induct auto
  2.1371 +qed (simp add: borel_measurable_const)
  2.1372 +
  2.1373 +lemma (in sigma_algebra) borel_measurable_pinfreal_min[intro, simp]:
  2.1374 +  fixes f g :: "'a \<Rightarrow> pinfreal"
  2.1375 +  assumes "f \<in> borel_measurable M"
  2.1376 +  assumes "g \<in> borel_measurable M"
  2.1377 +  shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  2.1378 +  using assms unfolding min_def by (auto intro!: measurable_If)
  2.1379 +
  2.1380 +lemma (in sigma_algebra) borel_measurable_pinfreal_max[intro]:
  2.1381 +  fixes f g :: "'a \<Rightarrow> pinfreal"
  2.1382 +  assumes "f \<in> borel_measurable M"
  2.1383 +  and "g \<in> borel_measurable M"
  2.1384 +  shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  2.1385 +  using assms unfolding max_def by (auto intro!: measurable_If)
  2.1386 +
  2.1387 +lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
  2.1388 +  fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> pinfreal"
  2.1389 +  assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  2.1390 +  shows "(SUP i : A. f i) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  2.1391 +  unfolding borel_measurable_pinfreal_iff_greater
  2.1392 +proof safe
  2.1393 +  fix a
  2.1394 +  have "{x\<in>space M. a < ?sup x} = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  2.1395 +    by (auto simp: less_Sup_iff SUPR_def[where 'a=pinfreal] SUPR_fun_expand[where 'c=pinfreal])
  2.1396 +  then show "{x\<in>space M. a < ?sup x} \<in> sets M"
  2.1397 +    using assms by auto
  2.1398 +qed
  2.1399 +
  2.1400 +lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
  2.1401 +  fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> pinfreal"
  2.1402 +  assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  2.1403 +  shows "(INF i : A. f i) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  2.1404 +  unfolding borel_measurable_pinfreal_iff_less
  2.1405 +proof safe
  2.1406 +  fix a
  2.1407 +  have "{x\<in>space M. ?inf x < a} = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  2.1408 +    by (auto simp: Inf_less_iff INFI_def[where 'a=pinfreal] INFI_fun_expand)
  2.1409 +  then show "{x\<in>space M. ?inf x < a} \<in> sets M"
  2.1410 +    using assms by auto
  2.1411 +qed
  2.1412 +
  2.1413 +lemma (in sigma_algebra) borel_measurable_pinfreal_diff[simp, intro]:
  2.1414 +  fixes f g :: "'a \<Rightarrow> pinfreal"
  2.1415 +  assumes "f \<in> borel_measurable M"
  2.1416 +  assumes "g \<in> borel_measurable M"
  2.1417 +  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  2.1418 +  unfolding borel_measurable_pinfreal_iff_greater
  2.1419 +proof safe
  2.1420 +  fix a
  2.1421 +  have "{x \<in> space M. a < f x - g x} = {x \<in> space M. g x + a < f x}"
  2.1422 +    by (simp add: pinfreal_less_minus_iff)
  2.1423 +  then show "{x \<in> space M. a < f x - g x} \<in> sets M"
  2.1424 +    using assms by auto
  2.1425 +qed
  2.1426 +
  2.1427 +lemma (in sigma_algebra) borel_measurable_psuminf:
  2.1428 +  assumes "\<And>i. f i \<in> borel_measurable M"
  2.1429 +  shows "(\<lambda>x. (\<Sum>\<^isub>\<infinity> i. f i x)) \<in> borel_measurable M"
  2.1430 +  using assms unfolding psuminf_def
  2.1431 +  by (auto intro!: borel_measurable_SUP[unfolded SUPR_fun_expand])
  2.1432 +
  2.1433 +section "LIMSEQ is borel measurable"
  2.1434 +
  2.1435 +lemma (in sigma_algebra) borel_measurable_LIMSEQ:
  2.1436 +  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  2.1437 +  assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  2.1438 +  and u: "\<And>i. u i \<in> borel_measurable M"
  2.1439 +  shows "u' \<in> borel_measurable M"
  2.1440 +proof -
  2.1441 +  let "?pu x i" = "max (u i x) 0"
  2.1442 +  let "?nu x i" = "max (- u i x) 0"
  2.1443 +
  2.1444 +  { fix x assume x: "x \<in> space M"
  2.1445 +    have "(?pu x) ----> max (u' x) 0"
  2.1446 +      "(?nu x) ----> max (- u' x) 0"
  2.1447 +      using u'[OF x] by (auto intro!: LIMSEQ_max LIMSEQ_minus)
  2.1448 +    from LIMSEQ_imp_lim_INF[OF _ this(1)] LIMSEQ_imp_lim_INF[OF _ this(2)]
  2.1449 +    have "(SUP n. INF m. Real (u (n + m) x)) = Real (u' x)"
  2.1450 +      "(SUP n. INF m. Real (- u (n + m) x)) = Real (- u' x)"
  2.1451 +      by (simp_all add: Real_max'[symmetric]) }
  2.1452 +  note eq = this
  2.1453 +
  2.1454 +  have *: "\<And>x. real (Real (u' x)) - real (Real (- u' x)) = u' x"
  2.1455 +    by auto
  2.1456 +
  2.1457 +  have "(SUP n. INF m. (\<lambda>x. Real (u (n + m) x))) \<in> borel_measurable M"
  2.1458 +       "(SUP n. INF m. (\<lambda>x. Real (- u (n + m) x))) \<in> borel_measurable M"
  2.1459 +    using u by (auto intro: borel_measurable_SUP borel_measurable_INF borel_measurable_Real)
  2.1460 +  with eq[THEN measurable_cong, of M "\<lambda>x. x" borel]
  2.1461 +  have "(\<lambda>x. Real (u' x)) \<in> borel_measurable M"
  2.1462 +       "(\<lambda>x. Real (- u' x)) \<in> borel_measurable M"
  2.1463 +    unfolding SUPR_fun_expand INFI_fun_expand by auto
  2.1464 +  note this[THEN borel_measurable_real]
  2.1465 +  from borel_measurable_diff[OF this]
  2.1466 +  show ?thesis unfolding * .
  2.1467 +qed
  2.1468 +
  2.1469 +end
     3.1 --- a/src/HOL/Probability/Caratheodory.thy	Wed Dec 01 06:50:54 2010 -0800
     3.2 +++ b/src/HOL/Probability/Caratheodory.thy	Wed Dec 01 19:20:30 2010 +0100
     3.3 @@ -760,7 +760,7 @@
     3.4  
     3.5  theorem (in algebra) caratheodory:
     3.6    assumes posf: "positive f" and ca: "countably_additive M f"
     3.7 -  shows "\<exists>\<mu> :: 'a set \<Rightarrow> pinfreal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and> measure_space (sigma (space M) (sets M)) \<mu>"
     3.8 +  shows "\<exists>\<mu> :: 'a set \<Rightarrow> pinfreal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and> measure_space (sigma M) \<mu>"
     3.9    proof -
    3.10      have inc: "increasing M f"
    3.11        by (metis additive_increasing ca countably_additive_additive posf)
    3.12 @@ -778,7 +778,7 @@
    3.13      hence sgs_sb: "sigma_sets (space M) (sets M) \<subseteq> ls"
    3.14        using sigma_algebra.sigma_sets_subset [OF sls, of "sets M"]
    3.15        by simp
    3.16 -    have "measure_space (sigma (space M) (sets M)) ?infm"
    3.17 +    have "measure_space (sigma M) ?infm"
    3.18        unfolding sigma_def
    3.19        by (rule measure_down [OF mls], rule sigma_algebra_sigma_sets)
    3.20           (simp_all add: sgs_sb space_closed)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/HOL/Probability/Complete_Measure.thy	Wed Dec 01 19:20:30 2010 +0100
     4.3 @@ -0,0 +1,317 @@
     4.4 +(*  Title:      Complete_Measure.thy
     4.5 +    Author:     Robert Himmelmann, Johannes Hoelzl, TU Muenchen
     4.6 +*)
     4.7 +theory Complete_Measure
     4.8 +imports Product_Measure
     4.9 +begin
    4.10 +
    4.11 +locale completeable_measure_space = measure_space
    4.12 +
    4.13 +definition (in completeable_measure_space) completion :: "'a algebra" where
    4.14 +  "completion = \<lparr> space = space M,
    4.15 +    sets = { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets \<and> N \<subseteq> N' } \<rparr>"
    4.16 +
    4.17 +lemma (in completeable_measure_space) space_completion[simp]:
    4.18 +  "space completion = space M" unfolding completion_def by simp
    4.19 +
    4.20 +lemma (in completeable_measure_space) sets_completionE:
    4.21 +  assumes "A \<in> sets completion"
    4.22 +  obtains S N N' where "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets" "S \<in> sets M"
    4.23 +  using assms unfolding completion_def by auto
    4.24 +
    4.25 +lemma (in completeable_measure_space) sets_completionI:
    4.26 +  assumes "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets" "S \<in> sets M"
    4.27 +  shows "A \<in> sets completion"
    4.28 +  using assms unfolding completion_def by auto
    4.29 +
    4.30 +lemma (in completeable_measure_space) sets_completionI_sets[intro]:
    4.31 +  "A \<in> sets M \<Longrightarrow> A \<in> sets completion"
    4.32 +  unfolding completion_def by force
    4.33 +
    4.34 +lemma (in completeable_measure_space) null_sets_completion:
    4.35 +  assumes "N' \<in> null_sets" "N \<subseteq> N'" shows "N \<in> sets completion"
    4.36 +  apply(rule sets_completionI[of N "{}" N N'])
    4.37 +  using assms by auto
    4.38 +
    4.39 +sublocale completeable_measure_space \<subseteq> completion!: sigma_algebra completion
    4.40 +proof (unfold sigma_algebra_iff2, safe)
    4.41 +  fix A x assume "A \<in> sets completion" "x \<in> A"
    4.42 +  with sets_into_space show "x \<in> space completion"
    4.43 +    by (auto elim!: sets_completionE)
    4.44 +next
    4.45 +  fix A assume "A \<in> sets completion"
    4.46 +  from this[THEN sets_completionE] guess S N N' . note A = this
    4.47 +  let ?C = "space completion"
    4.48 +  show "?C - A \<in> sets completion" using A
    4.49 +    by (intro sets_completionI[of _ "(?C - S) \<inter> (?C - N')" "(?C - S) \<inter> N' \<inter> (?C - N)"])
    4.50 +       auto
    4.51 +next
    4.52 +  fix A ::"nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets completion"
    4.53 +  then have "\<forall>n. \<exists>S N N'. A n = S \<union> N \<and> S \<in> sets M \<and> N' \<in> null_sets \<and> N \<subseteq> N'"
    4.54 +    unfolding completion_def by (auto simp: image_subset_iff)
    4.55 +  from choice[OF this] guess S ..
    4.56 +  from choice[OF this] guess N ..
    4.57 +  from choice[OF this] guess N' ..
    4.58 +  then show "UNION UNIV A \<in> sets completion"
    4.59 +    using null_sets_UN[of N']
    4.60 +    by (intro sets_completionI[of _ "UNION UNIV S" "UNION UNIV N" "UNION UNIV N'"])
    4.61 +       auto
    4.62 +qed auto
    4.63 +
    4.64 +definition (in completeable_measure_space)
    4.65 +  "split_completion A p = (\<exists>N'. A = fst p \<union> snd p \<and> fst p \<inter> snd p = {} \<and>
    4.66 +    fst p \<in> sets M \<and> snd p \<subseteq> N' \<and> N' \<in> null_sets)"
    4.67 +
    4.68 +definition (in completeable_measure_space)
    4.69 +  "main_part A = fst (Eps (split_completion A))"
    4.70 +
    4.71 +definition (in completeable_measure_space)
    4.72 +  "null_part A = snd (Eps (split_completion A))"
    4.73 +
    4.74 +lemma (in completeable_measure_space) split_completion:
    4.75 +  assumes "A \<in> sets completion"
    4.76 +  shows "split_completion A (main_part A, null_part A)"
    4.77 +  unfolding main_part_def null_part_def
    4.78 +proof (rule someI2_ex)
    4.79 +  from assms[THEN sets_completionE] guess S N N' . note A = this
    4.80 +  let ?P = "(S, N - S)"
    4.81 +  show "\<exists>p. split_completion A p"
    4.82 +    unfolding split_completion_def using A
    4.83 +  proof (intro exI conjI)
    4.84 +    show "A = fst ?P \<union> snd ?P" using A by auto
    4.85 +    show "snd ?P \<subseteq> N'" using A by auto
    4.86 +  qed auto
    4.87 +qed auto
    4.88 +
    4.89 +lemma (in completeable_measure_space)
    4.90 +  assumes "S \<in> sets completion"
    4.91 +  shows main_part_sets[intro, simp]: "main_part S \<in> sets M"
    4.92 +    and main_part_null_part_Un[simp]: "main_part S \<union> null_part S = S"
    4.93 +    and main_part_null_part_Int[simp]: "main_part S \<inter> null_part S = {}"
    4.94 +  using split_completion[OF assms] by (auto simp: split_completion_def)
    4.95 +
    4.96 +lemma (in completeable_measure_space) null_part:
    4.97 +  assumes "S \<in> sets completion" shows "\<exists>N. N\<in>null_sets \<and> null_part S \<subseteq> N"
    4.98 +  using split_completion[OF assms] by (auto simp: split_completion_def)
    4.99 +
   4.100 +lemma (in completeable_measure_space) null_part_sets[intro, simp]:
   4.101 +  assumes "S \<in> sets M" shows "null_part S \<in> sets M" "\<mu> (null_part S) = 0"
   4.102 +proof -
   4.103 +  have S: "S \<in> sets completion" using assms by auto
   4.104 +  have "S - main_part S \<in> sets M" using assms by auto
   4.105 +  moreover
   4.106 +  from main_part_null_part_Un[OF S] main_part_null_part_Int[OF S]
   4.107 +  have "S - main_part S = null_part S" by auto
   4.108 +  ultimately show sets: "null_part S \<in> sets M" by auto
   4.109 +  from null_part[OF S] guess N ..
   4.110 +  with measure_eq_0[of N "null_part S"] sets
   4.111 +  show "\<mu> (null_part S) = 0" by auto
   4.112 +qed
   4.113 +
   4.114 +definition (in completeable_measure_space) "\<mu>' A = \<mu> (main_part A)"
   4.115 +
   4.116 +lemma (in completeable_measure_space) \<mu>'_set[simp]:
   4.117 +  assumes "S \<in> sets M" shows "\<mu>' S = \<mu> S"
   4.118 +proof -
   4.119 +  have S: "S \<in> sets completion" using assms by auto
   4.120 +  then have "\<mu> S = \<mu> (main_part S \<union> null_part S)" by simp
   4.121 +  also have "\<dots> = \<mu> (main_part S)"
   4.122 +    using S assms measure_additive[of "main_part S" "null_part S"]
   4.123 +    by (auto simp: measure_additive)
   4.124 +  finally show ?thesis unfolding \<mu>'_def by simp
   4.125 +qed
   4.126 +
   4.127 +lemma (in completeable_measure_space) sets_completionI_sub:
   4.128 +  assumes N: "N' \<in> null_sets" "N \<subseteq> N'"
   4.129 +  shows "N \<in> sets completion"
   4.130 +  using assms by (intro sets_completionI[of _ "{}" N N']) auto
   4.131 +
   4.132 +lemma (in completeable_measure_space) \<mu>_main_part_UN:
   4.133 +  fixes S :: "nat \<Rightarrow> 'a set"
   4.134 +  assumes "range S \<subseteq> sets completion"
   4.135 +  shows "\<mu>' (\<Union>i. (S i)) = \<mu> (\<Union>i. main_part (S i))"
   4.136 +proof -
   4.137 +  have S: "\<And>i. S i \<in> sets completion" using assms by auto
   4.138 +  then have UN: "(\<Union>i. S i) \<in> sets completion" by auto
   4.139 +  have "\<forall>i. \<exists>N. N \<in> null_sets \<and> null_part (S i) \<subseteq> N"
   4.140 +    using null_part[OF S] by auto
   4.141 +  from choice[OF this] guess N .. note N = this
   4.142 +  then have UN_N: "(\<Union>i. N i) \<in> null_sets" by (intro null_sets_UN) auto
   4.143 +  have "(\<Union>i. S i) \<in> sets completion" using S by auto
   4.144 +  from null_part[OF this] guess N' .. note N' = this
   4.145 +  let ?N = "(\<Union>i. N i) \<union> N'"
   4.146 +  have null_set: "?N \<in> null_sets" using N' UN_N by (intro null_sets_Un) auto
   4.147 +  have "main_part (\<Union>i. S i) \<union> ?N = (main_part (\<Union>i. S i) \<union> null_part (\<Union>i. S i)) \<union> ?N"
   4.148 +    using N' by auto
   4.149 +  also have "\<dots> = (\<Union>i. main_part (S i) \<union> null_part (S i)) \<union> ?N"
   4.150 +    unfolding main_part_null_part_Un[OF S] main_part_null_part_Un[OF UN] by auto
   4.151 +  also have "\<dots> = (\<Union>i. main_part (S i)) \<union> ?N"
   4.152 +    using N by auto
   4.153 +  finally have *: "main_part (\<Union>i. S i) \<union> ?N = (\<Union>i. main_part (S i)) \<union> ?N" .
   4.154 +  have "\<mu> (main_part (\<Union>i. S i)) = \<mu> (main_part (\<Union>i. S i) \<union> ?N)"
   4.155 +    using null_set UN by (intro measure_Un_null_set[symmetric]) auto
   4.156 +  also have "\<dots> = \<mu> ((\<Union>i. main_part (S i)) \<union> ?N)"
   4.157 +    unfolding * ..
   4.158 +  also have "\<dots> = \<mu> (\<Union>i. main_part (S i))"
   4.159 +    using null_set S by (intro measure_Un_null_set) auto
   4.160 +  finally show ?thesis unfolding \<mu>'_def .
   4.161 +qed
   4.162 +
   4.163 +lemma (in completeable_measure_space) \<mu>_main_part_Un:
   4.164 +  assumes S: "S \<in> sets completion" and T: "T \<in> sets completion"
   4.165 +  shows "\<mu>' (S \<union> T) = \<mu> (main_part S \<union> main_part T)"
   4.166 +proof -
   4.167 +  have UN: "(\<Union>i. binary (main_part S) (main_part T) i) = (\<Union>i. main_part (binary S T i))"
   4.168 +    unfolding binary_def by (auto split: split_if_asm)
   4.169 +  show ?thesis
   4.170 +    using \<mu>_main_part_UN[of "binary S T"] assms
   4.171 +    unfolding range_binary_eq Un_range_binary UN by auto
   4.172 +qed
   4.173 +
   4.174 +sublocale completeable_measure_space \<subseteq> completion!: measure_space completion \<mu>'
   4.175 +proof
   4.176 +  show "\<mu>' {} = 0" by auto
   4.177 +next
   4.178 +  show "countably_additive completion \<mu>'"
   4.179 +  proof (unfold countably_additive_def, intro allI conjI impI)
   4.180 +    fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets completion" "disjoint_family A"
   4.181 +    have "disjoint_family (\<lambda>i. main_part (A i))"
   4.182 +    proof (intro disjoint_family_on_bisimulation[OF A(2)])
   4.183 +      fix n m assume "A n \<inter> A m = {}"
   4.184 +      then have "(main_part (A n) \<union> null_part (A n)) \<inter> (main_part (A m) \<union> null_part (A m)) = {}"
   4.185 +        using A by (subst (1 2) main_part_null_part_Un) auto
   4.186 +      then show "main_part (A n) \<inter> main_part (A m) = {}" by auto
   4.187 +    qed
   4.188 +    then have "(\<Sum>\<^isub>\<infinity>n. \<mu>' (A n)) = \<mu> (\<Union>i. main_part (A i))"
   4.189 +      unfolding \<mu>'_def using A by (intro measure_countably_additive) auto
   4.190 +    then show "(\<Sum>\<^isub>\<infinity>n. \<mu>' (A n)) = \<mu>' (UNION UNIV A)"
   4.191 +      unfolding \<mu>_main_part_UN[OF A(1)] .
   4.192 +  qed
   4.193 +qed
   4.194 +
   4.195 +lemma (in sigma_algebra) simple_functionD':
   4.196 +  assumes "simple_function f"
   4.197 +  shows "f -` {x} \<inter> space M \<in> sets M"
   4.198 +proof cases
   4.199 +  assume "x \<in> f`space M" from simple_functionD(2)[OF assms this] show ?thesis .
   4.200 +next
   4.201 +  assume "x \<notin> f`space M" then have "f -` {x} \<inter> space M = {}" by auto
   4.202 +  then show ?thesis by auto
   4.203 +qed
   4.204 +
   4.205 +lemma (in sigma_algebra) simple_function_If:
   4.206 +  assumes sf: "simple_function f" "simple_function g" and A: "A \<in> sets M"
   4.207 +  shows "simple_function (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function ?IF")
   4.208 +proof -
   4.209 +  def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
   4.210 +  show ?thesis unfolding simple_function_def
   4.211 +  proof safe
   4.212 +    have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
   4.213 +    from finite_subset[OF this] assms
   4.214 +    show "finite (?IF ` space M)" unfolding simple_function_def by auto
   4.215 +  next
   4.216 +    fix x assume "x \<in> space M"
   4.217 +    then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
   4.218 +      then ((F (f x) \<inter> A) \<union> (G (f x) - (G (f x) \<inter> A)))
   4.219 +      else ((F (g x) \<inter> A) \<union> (G (g x) - (G (g x) \<inter> A))))"
   4.220 +      using sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
   4.221 +    have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
   4.222 +      unfolding F_def G_def using sf[THEN simple_functionD'] by auto
   4.223 +    show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
   4.224 +  qed
   4.225 +qed
   4.226 +
   4.227 +lemma (in measure_space) null_sets_finite_UN:
   4.228 +  assumes "finite S" "\<And>i. i \<in> S \<Longrightarrow> A i \<in> null_sets"
   4.229 +  shows "(\<Union>i\<in>S. A i) \<in> null_sets"
   4.230 +proof (intro CollectI conjI)
   4.231 +  show "(\<Union>i\<in>S. A i) \<in> sets M" using assms by (intro finite_UN) auto
   4.232 +  have "\<mu> (\<Union>i\<in>S. A i) \<le> (\<Sum>i\<in>S. \<mu> (A i))"
   4.233 +    using assms by (intro measure_finitely_subadditive) auto
   4.234 +  then show "\<mu> (\<Union>i\<in>S. A i) = 0"
   4.235 +    using assms by auto
   4.236 +qed
   4.237 +
   4.238 +lemma (in completeable_measure_space) completion_ex_simple_function:
   4.239 +  assumes f: "completion.simple_function f"
   4.240 +  shows "\<exists>f'. simple_function f' \<and> (AE x. f x = f' x)"
   4.241 +proof -
   4.242 +  let "?F x" = "f -` {x} \<inter> space M"
   4.243 +  have F: "\<And>x. ?F x \<in> sets completion" and fin: "finite (f`space M)"
   4.244 +    using completion.simple_functionD'[OF f]
   4.245 +      completion.simple_functionD[OF f] by simp_all
   4.246 +  have "\<forall>x. \<exists>N. N \<in> null_sets \<and> null_part (?F x) \<subseteq> N"
   4.247 +    using F null_part by auto
   4.248 +  from choice[OF this] obtain N where
   4.249 +    N: "\<And>x. null_part (?F x) \<subseteq> N x" "\<And>x. N x \<in> null_sets" by auto
   4.250 +  let ?N = "\<Union>x\<in>f`space M. N x" let "?f' x" = "if x \<in> ?N then undefined else f x"
   4.251 +  have sets: "?N \<in> null_sets" using N fin by (intro null_sets_finite_UN) auto
   4.252 +  show ?thesis unfolding simple_function_def
   4.253 +  proof (safe intro!: exI[of _ ?f'])
   4.254 +    have "?f' ` space M \<subseteq> f`space M \<union> {undefined}" by auto
   4.255 +    from finite_subset[OF this] completion.simple_functionD(1)[OF f]
   4.256 +    show "finite (?f' ` space M)" by auto
   4.257 +  next
   4.258 +    fix x assume "x \<in> space M"
   4.259 +    have "?f' -` {?f' x} \<inter> space M =
   4.260 +      (if x \<in> ?N then ?F undefined \<union> ?N
   4.261 +       else if f x = undefined then ?F (f x) \<union> ?N
   4.262 +       else ?F (f x) - ?N)"
   4.263 +      using N(2) sets_into_space by (auto split: split_if_asm)
   4.264 +    moreover { fix y have "?F y \<union> ?N \<in> sets M"
   4.265 +      proof cases
   4.266 +        assume y: "y \<in> f`space M"
   4.267 +        have "?F y \<union> ?N = (main_part (?F y) \<union> null_part (?F y)) \<union> ?N"
   4.268 +          using main_part_null_part_Un[OF F] by auto
   4.269 +        also have "\<dots> = main_part (?F y) \<union> ?N"
   4.270 +          using y N by auto
   4.271 +        finally show ?thesis
   4.272 +          using F sets by auto
   4.273 +      next
   4.274 +        assume "y \<notin> f`space M" then have "?F y = {}" by auto
   4.275 +        then show ?thesis using sets by auto
   4.276 +      qed }
   4.277 +    moreover {
   4.278 +      have "?F (f x) - ?N = main_part (?F (f x)) \<union> null_part (?F (f x)) - ?N"
   4.279 +        using main_part_null_part_Un[OF F] by auto
   4.280 +      also have "\<dots> = main_part (?F (f x)) - ?N"
   4.281 +        using N `x \<in> space M` by auto
   4.282 +      finally have "?F (f x) - ?N \<in> sets M"
   4.283 +        using F sets by auto }
   4.284 +    ultimately show "?f' -` {?f' x} \<inter> space M \<in> sets M" by auto
   4.285 +  next
   4.286 +    show "AE x. f x = ?f' x"
   4.287 +      by (rule AE_I', rule sets) auto
   4.288 +  qed
   4.289 +qed
   4.290 +
   4.291 +lemma (in completeable_measure_space) completion_ex_borel_measurable:
   4.292 +  fixes g :: "'a \<Rightarrow> pinfreal"
   4.293 +  assumes g: "g \<in> borel_measurable completion"
   4.294 +  shows "\<exists>g'\<in>borel_measurable M. (AE x. g x = g' x)"
   4.295 +proof -
   4.296 +  from g[THEN completion.borel_measurable_implies_simple_function_sequence]
   4.297 +  obtain f where "\<And>i. completion.simple_function (f i)" "f \<up> g" by auto
   4.298 +  then have "\<forall>i. \<exists>f'. simple_function f' \<and> (AE x. f i x = f' x)"
   4.299 +    using completion_ex_simple_function by auto
   4.300 +  from this[THEN choice] obtain f' where
   4.301 +    sf: "\<And>i. simple_function (f' i)" and
   4.302 +    AE: "\<forall>i. AE x. f i x = f' i x" by auto
   4.303 +  show ?thesis
   4.304 +  proof (intro bexI)
   4.305 +    from AE[unfolded all_AE_countable]
   4.306 +    show "AE x. g x = (SUP i. f' i) x" (is "AE x. g x = ?f x")
   4.307 +    proof (rule AE_mp, safe intro!: AE_cong)
   4.308 +      fix x assume eq: "\<forall>i. f i x = f' i x"
   4.309 +      have "g x = (SUP i. f i x)"
   4.310 +        using `f \<up> g` unfolding isoton_def SUPR_fun_expand by auto
   4.311 +      then show "g x = ?f x"
   4.312 +        using eq unfolding SUPR_fun_expand by auto
   4.313 +    qed
   4.314 +    show "?f \<in> borel_measurable M"
   4.315 +      using sf by (auto intro!: borel_measurable_SUP
   4.316 +        intro: borel_measurable_simple_function)
   4.317 +  qed
   4.318 +qed
   4.319 +
   4.320 +end
     5.1 --- a/src/HOL/Probability/Euclidean_Lebesgue.thy	Wed Dec 01 06:50:54 2010 -0800
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,212 +0,0 @@
     5.4 -theory Euclidean_Lebesgue
     5.5 -  imports Lebesgue_Integration Lebesgue_Measure
     5.6 -begin
     5.7 -
     5.8 -lemma simple_function_has_integral:
     5.9 -  fixes f::"'a::ordered_euclidean_space \<Rightarrow> pinfreal"
    5.10 -  assumes f:"lebesgue.simple_function f"
    5.11 -  and f':"\<forall>x. f x \<noteq> \<omega>"
    5.12 -  and om:"\<forall>x\<in>range f. lmeasure (f -` {x} \<inter> UNIV) = \<omega> \<longrightarrow> x = 0"
    5.13 -  shows "((\<lambda>x. real (f x)) has_integral (real (lebesgue.simple_integral f))) UNIV"
    5.14 -  unfolding lebesgue.simple_integral_def
    5.15 -  apply(subst lebesgue_simple_function_indicator[OF f])
    5.16 -proof- case goal1
    5.17 -  have *:"\<And>x. \<forall>y\<in>range f. y * indicator (f -` {y}) x \<noteq> \<omega>"
    5.18 -    "\<forall>x\<in>range f. x * lmeasure (f -` {x} \<inter> UNIV) \<noteq> \<omega>"
    5.19 -    using f' om unfolding indicator_def by auto
    5.20 -  show ?case unfolding space_lebesgue_space real_of_pinfreal_setsum'[OF *(1),THEN sym]
    5.21 -    unfolding real_of_pinfreal_setsum'[OF *(2),THEN sym]
    5.22 -    unfolding real_of_pinfreal_setsum space_lebesgue_space
    5.23 -    apply(rule has_integral_setsum)
    5.24 -  proof safe show "finite (range f)" using f by (auto dest: lebesgue.simple_functionD)
    5.25 -    fix y::'a show "((\<lambda>x. real (f y * indicator (f -` {f y}) x)) has_integral
    5.26 -      real (f y * lmeasure (f -` {f y} \<inter> UNIV))) UNIV"
    5.27 -    proof(cases "f y = 0") case False
    5.28 -      have mea:"gmeasurable (f -` {f y})" apply(rule glmeasurable_finite)
    5.29 -        using assms unfolding lebesgue.simple_function_def using False by auto
    5.30 -      have *:"\<And>x. real (indicator (f -` {f y}) x::pinfreal) = (if x \<in> f -` {f y} then 1 else 0)" by auto
    5.31 -      show ?thesis unfolding real_of_pinfreal_mult[THEN sym]
    5.32 -        apply(rule has_integral_cmul[where 'b=real, unfolded real_scaleR_def])
    5.33 -        unfolding Int_UNIV_right lmeasure_gmeasure[OF mea,THEN sym]
    5.34 -        unfolding measure_integral_univ[OF mea] * apply(rule integrable_integral)
    5.35 -        unfolding gmeasurable_integrable[THEN sym] using mea .
    5.36 -    qed auto
    5.37 -  qed qed
    5.38 -
    5.39 -lemma (in measure_space) positive_integral_omega:
    5.40 -  assumes "f \<in> borel_measurable M"
    5.41 -  and "positive_integral f \<noteq> \<omega>"
    5.42 -  shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
    5.43 -proof -
    5.44 -  have "\<omega> * \<mu> (f -` {\<omega>} \<inter> space M) = positive_integral (\<lambda>x. \<omega> * indicator (f -` {\<omega>} \<inter> space M) x)"
    5.45 -    using positive_integral_cmult_indicator[OF borel_measurable_vimage, OF assms(1), of \<omega> \<omega>] by simp
    5.46 -  also have "\<dots> \<le> positive_integral f"
    5.47 -    by (auto intro!: positive_integral_mono simp: indicator_def)
    5.48 -  finally show ?thesis
    5.49 -    using assms(2) by (cases ?thesis) auto
    5.50 -qed
    5.51 -
    5.52 -lemma (in measure_space) simple_integral_omega:
    5.53 -  assumes "simple_function f"
    5.54 -  and "simple_integral f \<noteq> \<omega>"
    5.55 -  shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
    5.56 -proof (rule positive_integral_omega)
    5.57 -  show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
    5.58 -  show "positive_integral f \<noteq> \<omega>"
    5.59 -    using assms by (simp add: positive_integral_eq_simple_integral)
    5.60 -qed
    5.61 -
    5.62 -lemma bounded_realI: assumes "\<forall>x\<in>s. abs (x::real) \<le> B" shows "bounded s"
    5.63 -  unfolding bounded_def dist_real_def apply(rule_tac x=0 in exI)
    5.64 -  using assms by auto
    5.65 -
    5.66 -lemma simple_function_has_integral':
    5.67 -  fixes f::"'a::ordered_euclidean_space \<Rightarrow> pinfreal"
    5.68 -  assumes f:"lebesgue.simple_function f"
    5.69 -  and i: "lebesgue.simple_integral f \<noteq> \<omega>"
    5.70 -  shows "((\<lambda>x. real (f x)) has_integral (real (lebesgue.simple_integral f))) UNIV"
    5.71 -proof- let ?f = "\<lambda>x. if f x = \<omega> then 0 else f x"
    5.72 -  { fix x have "real (f x) = real (?f x)" by (cases "f x") auto } note * = this
    5.73 -  have **:"{x. f x \<noteq> ?f x} = f -` {\<omega>}" by auto
    5.74 -  have **:"lmeasure {x\<in>space lebesgue_space. f x \<noteq> ?f x} = 0"
    5.75 -    using lebesgue.simple_integral_omega[OF assms] by(auto simp add:**)
    5.76 -  show ?thesis apply(subst lebesgue.simple_integral_cong'[OF f _ **])
    5.77 -    apply(rule lebesgue.simple_function_compose1[OF f])
    5.78 -    unfolding * defer apply(rule simple_function_has_integral)
    5.79 -  proof-
    5.80 -    show "lebesgue.simple_function ?f"
    5.81 -      using lebesgue.simple_function_compose1[OF f] .
    5.82 -    show "\<forall>x. ?f x \<noteq> \<omega>" by auto
    5.83 -    show "\<forall>x\<in>range ?f. lmeasure (?f -` {x} \<inter> UNIV) = \<omega> \<longrightarrow> x = 0"
    5.84 -    proof (safe, simp, safe, rule ccontr)
    5.85 -      fix y assume "f y \<noteq> \<omega>" "f y \<noteq> 0"
    5.86 -      hence "(\<lambda>x. if f x = \<omega> then 0 else f x) -` {if f y = \<omega> then 0 else f y} = f -` {f y}"
    5.87 -        by (auto split: split_if_asm)
    5.88 -      moreover assume "lmeasure ((\<lambda>x. if f x = \<omega> then 0 else f x) -` {if f y = \<omega> then 0 else f y}) = \<omega>"
    5.89 -      ultimately have "lmeasure (f -` {f y}) = \<omega>" by simp
    5.90 -      moreover
    5.91 -      have "f y * lmeasure (f -` {f y}) \<noteq> \<omega>" using i f
    5.92 -        unfolding lebesgue.simple_integral_def setsum_\<omega> lebesgue.simple_function_def
    5.93 -        by auto
    5.94 -      ultimately have "f y = 0" by (auto split: split_if_asm)
    5.95 -      then show False using `f y \<noteq> 0` by simp
    5.96 -    qed
    5.97 -  qed
    5.98 -qed
    5.99 -
   5.100 -lemma (in measure_space) positive_integral_monotone_convergence:
   5.101 -  fixes f::"nat \<Rightarrow> 'a \<Rightarrow> pinfreal"
   5.102 -  assumes i: "\<And>i. f i \<in> borel_measurable M" and mono: "\<And>x. mono (\<lambda>n. f n x)"
   5.103 -  and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
   5.104 -  shows "u \<in> borel_measurable M"
   5.105 -  and "(\<lambda>i. positive_integral (f i)) ----> positive_integral u" (is ?ilim)
   5.106 -proof -
   5.107 -  from positive_integral_isoton[unfolded isoton_fun_expand isoton_iff_Lim_mono, of f u]
   5.108 -  show ?ilim using mono lim i by auto
   5.109 -  have "(SUP i. f i) = u" using mono lim SUP_Lim_pinfreal
   5.110 -    unfolding fun_eq_iff SUPR_fun_expand mono_def by auto
   5.111 -  moreover have "(SUP i. f i) \<in> borel_measurable M"
   5.112 -    using i by (rule borel_measurable_SUP)
   5.113 -  ultimately show "u \<in> borel_measurable M" by simp
   5.114 -qed
   5.115 -
   5.116 -lemma positive_integral_has_integral:
   5.117 -  fixes f::"'a::ordered_euclidean_space => pinfreal"
   5.118 -  assumes f:"f \<in> borel_measurable lebesgue_space"
   5.119 -  and int_om:"lebesgue.positive_integral f \<noteq> \<omega>"
   5.120 -  and f_om:"\<forall>x. f x \<noteq> \<omega>" (* TODO: remove this *)
   5.121 -  shows "((\<lambda>x. real (f x)) has_integral (real (lebesgue.positive_integral f))) UNIV"
   5.122 -proof- let ?i = "lebesgue.positive_integral f"
   5.123 -  from lebesgue.borel_measurable_implies_simple_function_sequence[OF f]
   5.124 -  guess u .. note conjunctD2[OF this,rule_format] note u = conjunctD2[OF this(1)] this(2)
   5.125 -  let ?u = "\<lambda>i x. real (u i x)" and ?f = "\<lambda>x. real (f x)"
   5.126 -  have u_simple:"\<And>k. lebesgue.simple_integral (u k) = lebesgue.positive_integral (u k)"
   5.127 -    apply(subst lebesgue.positive_integral_eq_simple_integral[THEN sym,OF u(1)]) ..
   5.128 -  have int_u_le:"\<And>k. lebesgue.simple_integral (u k) \<le> lebesgue.positive_integral f"
   5.129 -    unfolding u_simple apply(rule lebesgue.positive_integral_mono)
   5.130 -    using isoton_Sup[OF u(3)] unfolding le_fun_def by auto
   5.131 -  have u_int_om:"\<And>i. lebesgue.simple_integral (u i) \<noteq> \<omega>"
   5.132 -  proof- case goal1 thus ?case using int_u_le[of i] int_om by auto qed
   5.133 -
   5.134 -  note u_int = simple_function_has_integral'[OF u(1) this]
   5.135 -  have "(\<lambda>x. real (f x)) integrable_on UNIV \<and>
   5.136 -    (\<lambda>k. gintegral UNIV (\<lambda>x. real (u k x))) ----> gintegral UNIV (\<lambda>x. real (f x))"
   5.137 -    apply(rule monotone_convergence_increasing) apply(rule,rule,rule u_int)
   5.138 -  proof safe case goal1 show ?case apply(rule real_of_pinfreal_mono) using u(2,3) by auto
   5.139 -  next case goal2 show ?case using u(3) apply(subst lim_Real[THEN sym])
   5.140 -      prefer 3 apply(subst Real_real') defer apply(subst Real_real')
   5.141 -      using isotone_Lim[OF u(3)[unfolded isoton_fun_expand, THEN spec]] using f_om u by auto
   5.142 -  next case goal3
   5.143 -    show ?case apply(rule bounded_realI[where B="real (lebesgue.positive_integral f)"])
   5.144 -      apply safe apply(subst abs_of_nonneg) apply(rule integral_nonneg,rule) apply(rule u_int)
   5.145 -      unfolding integral_unique[OF u_int] defer apply(rule real_of_pinfreal_mono[OF _ int_u_le])
   5.146 -      using u int_om by auto
   5.147 -  qed note int = conjunctD2[OF this]
   5.148 -
   5.149 -  have "(\<lambda>i. lebesgue.simple_integral (u i)) ----> ?i" unfolding u_simple
   5.150 -    apply(rule lebesgue.positive_integral_monotone_convergence(2))
   5.151 -    apply(rule lebesgue.borel_measurable_simple_function[OF u(1)])
   5.152 -    using isotone_Lim[OF u(3)[unfolded isoton_fun_expand, THEN spec]] by auto
   5.153 -  hence "(\<lambda>i. real (lebesgue.simple_integral (u i))) ----> real ?i" apply-
   5.154 -    apply(subst lim_Real[THEN sym]) prefer 3
   5.155 -    apply(subst Real_real') defer apply(subst Real_real')
   5.156 -    using u f_om int_om u_int_om by auto
   5.157 -  note * = LIMSEQ_unique[OF this int(2)[unfolded integral_unique[OF u_int]]]
   5.158 -  show ?thesis unfolding * by(rule integrable_integral[OF int(1)])
   5.159 -qed
   5.160 -
   5.161 -lemma lebesgue_integral_has_integral:
   5.162 -  fixes f::"'a::ordered_euclidean_space => real"
   5.163 -  assumes f:"lebesgue.integrable f"
   5.164 -  shows "(f has_integral (lebesgue.integral f)) UNIV"
   5.165 -proof- let ?n = "\<lambda>x. - min (f x) 0" and ?p = "\<lambda>x. max (f x) 0"
   5.166 -  have *:"f = (\<lambda>x. ?p x - ?n x)" apply rule by auto
   5.167 -  note f = lebesgue.integrableD[OF f]
   5.168 -  show ?thesis unfolding lebesgue.integral_def apply(subst *)
   5.169 -  proof(rule has_integral_sub) case goal1
   5.170 -    have *:"\<forall>x. Real (f x) \<noteq> \<omega>" by auto
   5.171 -    note lebesgue.borel_measurable_Real[OF f(1)]
   5.172 -    from positive_integral_has_integral[OF this f(2) *]
   5.173 -    show ?case unfolding real_Real_max .
   5.174 -  next case goal2
   5.175 -    have *:"\<forall>x. Real (- f x) \<noteq> \<omega>" by auto
   5.176 -    note lebesgue.borel_measurable_uminus[OF f(1)]
   5.177 -    note lebesgue.borel_measurable_Real[OF this]
   5.178 -    from positive_integral_has_integral[OF this f(3) *]
   5.179 -    show ?case unfolding real_Real_max minus_min_eq_max by auto
   5.180 -  qed
   5.181 -qed
   5.182 -
   5.183 -lemma lmeasurable_imp_borel[dest]: fixes s::"'a::ordered_euclidean_space set"
   5.184 -  assumes "s \<in> sets borel_space" shows "lmeasurable s"
   5.185 -proof- let ?S = "range (\<lambda>(a, b). {a .. (b :: 'a\<Colon>ordered_euclidean_space)})"
   5.186 -  have *:"?S \<subseteq> sets lebesgue_space" by auto
   5.187 -  have "s \<in> sigma_sets UNIV ?S" using assms
   5.188 -    unfolding borel_space_eq_atLeastAtMost by (simp add: sigma_def)
   5.189 -  thus ?thesis using lebesgue.sigma_subset[of ?S,unfolded sets_sigma,OF *]
   5.190 -    by auto
   5.191 -qed
   5.192 -
   5.193 -lemma lmeasurable_open[dest]:
   5.194 -  assumes "open s" shows "lmeasurable s"
   5.195 -proof- have "s \<in> sets borel_space" using assms by auto
   5.196 -  thus ?thesis by auto qed
   5.197 -
   5.198 -lemma continuous_on_imp_borel_measurable:
   5.199 -  fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::ordered_euclidean_space"
   5.200 -  assumes "continuous_on UNIV f"
   5.201 -  shows "f \<in> borel_measurable lebesgue_space"
   5.202 -  apply(rule lebesgue.borel_measurableI)
   5.203 -  unfolding lebesgue_measurable apply(rule lmeasurable_open)
   5.204 -  using continuous_open_preimage[OF assms] unfolding vimage_def by auto
   5.205 -
   5.206 -
   5.207 -lemma (in measure_space) integral_monotone_convergence_pos':
   5.208 -  assumes i: "\<And>i. integrable (f i)" and mono: "\<And>x. mono (\<lambda>n. f n x)"
   5.209 -  and pos: "\<And>x i. 0 \<le> f i x"
   5.210 -  and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
   5.211 -  and ilim: "(\<lambda>i. integral (f i)) ----> x"
   5.212 -  shows "integrable u \<and> integral u = x"
   5.213 -  using integral_monotone_convergence_pos[OF assms] by auto
   5.214 -
   5.215 -end
     6.1 --- a/src/HOL/Probability/Information.thy	Wed Dec 01 06:50:54 2010 -0800
     6.2 +++ b/src/HOL/Probability/Information.thy	Wed Dec 01 19:20:30 2010 +0100
     6.3 @@ -1,5 +1,5 @@
     6.4  theory Information
     6.5 -imports Probability_Space Product_Measure Convex Radon_Nikodym
     6.6 +imports Probability_Space Convex Lebesgue_Measure
     6.7  begin
     6.8  
     6.9  lemma log_le: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log a x \<le> log a y"
    6.10 @@ -12,43 +12,6 @@
    6.11    "(\<Sum>x\<in>A \<times> B. f x) = (\<Sum>x\<in>A. setsum (\<lambda>y. f (x, y)) B)"
    6.12    unfolding setsum_cartesian_product by simp
    6.13  
    6.14 -lemma real_of_pinfreal_inverse[simp]:
    6.15 -  fixes X :: pinfreal
    6.16 -  shows "real (inverse X) = 1 / real X"
    6.17 -  by (cases X) (auto simp: inverse_eq_divide)
    6.18 -
    6.19 -lemma (in finite_prob_space) finite_product_prob_space_of_images:
    6.20 -  "finite_prob_space \<lparr> space = X ` space M \<times> Y ` space M, sets = Pow (X ` space M \<times> Y ` space M)\<rparr>
    6.21 -                     (joint_distribution X Y)"
    6.22 -  (is "finite_prob_space ?S _")
    6.23 -proof (simp add: finite_prob_space_eq finite_product_measure_space_of_images)
    6.24 -  have "X -` X ` space M \<inter> Y -` Y ` space M \<inter> space M = space M" by auto
    6.25 -  thus "joint_distribution X Y (X ` space M \<times> Y ` space M) = 1"
    6.26 -    by (simp add: distribution_def prob_space vimage_Times comp_def measure_space_1)
    6.27 -qed
    6.28 -
    6.29 -lemma (in finite_prob_space) finite_measure_space_prod:
    6.30 -  assumes X: "finite_measure_space MX (distribution X)"
    6.31 -  assumes Y: "finite_measure_space MY (distribution Y)"
    6.32 -  shows "finite_measure_space (prod_measure_space MX MY) (joint_distribution X Y)"
    6.33 -    (is "finite_measure_space ?M ?D")
    6.34 -proof (intro finite_measure_spaceI)
    6.35 -  interpret X: finite_measure_space MX "distribution X" by fact
    6.36 -  interpret Y: finite_measure_space MY "distribution Y" by fact
    6.37 -  note finite_measure_space.finite_prod_measure_space[OF X Y, simp]
    6.38 -  show "finite (space ?M)" using X.finite_space Y.finite_space by auto
    6.39 -  show "joint_distribution X Y {} = 0" by simp
    6.40 -  show "sets ?M = Pow (space ?M)" by simp
    6.41 -  { fix x show "?D (space ?M) \<noteq> \<omega>" by (rule distribution_finite) }
    6.42 -  { fix A B assume "A \<subseteq> space ?M" "B \<subseteq> space ?M" "A \<inter> B = {}"
    6.43 -    have *: "(\<lambda>t. (X t, Y t)) -` (A \<union> B) \<inter> space M =
    6.44 -             (\<lambda>t. (X t, Y t)) -` A \<inter> space M \<union> (\<lambda>t. (X t, Y t)) -` B \<inter> space M"
    6.45 -      by auto
    6.46 -    show "?D (A \<union> B) = ?D A + ?D B" unfolding distribution_def *
    6.47 -      apply (rule measure_additive[symmetric])
    6.48 -      using `A \<inter> B = {}` by (auto simp: sets_eq_Pow) }
    6.49 -qed
    6.50 -
    6.51  section "Convex theory"
    6.52  
    6.53  lemma log_setsum:
    6.54 @@ -148,82 +111,48 @@
    6.55  qed
    6.56  
    6.57  lemma split_pairs:
    6.58 -  shows
    6.59 -    "((A, B) = X) \<longleftrightarrow> (fst X = A \<and> snd X = B)" and
    6.60 -    "(X = (A, B)) \<longleftrightarrow> (fst X = A \<and> snd X = B)" by auto
    6.61 +  "((A, B) = X) \<longleftrightarrow> (fst X = A \<and> snd X = B)" and
    6.62 +  "(X = (A, B)) \<longleftrightarrow> (fst X = A \<and> snd X = B)" by auto
    6.63  
    6.64  section "Information theory"
    6.65  
    6.66 -locale finite_information_space = finite_prob_space +
    6.67 +locale information_space = prob_space +
    6.68    fixes b :: real assumes b_gt_1: "1 < b"
    6.69  
    6.70 -context finite_information_space
    6.71 +context information_space
    6.72  begin
    6.73  
    6.74 -lemma
    6.75 -  assumes "0 \<le> A" and pos: "0 < A \<Longrightarrow> 0 < B" "0 < A \<Longrightarrow> 0 < C"
    6.76 -  shows mult_log_mult: "A * log b (B * C) = A * log b B + A * log b C" (is "?mult")
    6.77 -  and mult_log_divide: "A * log b (B / C) = A * log b B - A * log b C" (is "?div")
    6.78 +text {* Introduce some simplification rules for logarithm of base @{term b}. *}
    6.79 +
    6.80 +lemma log_neg_const:
    6.81 +  assumes "x \<le> 0"
    6.82 +  shows "log b x = log b 0"
    6.83  proof -
    6.84 -  have "?mult \<and> ?div"
    6.85 -  proof (cases "A = 0")
    6.86 -    case False
    6.87 -    hence "0 < A" using `0 \<le> A` by auto
    6.88 -      with pos[OF this] show "?mult \<and> ?div" using b_gt_1
    6.89 -        by (auto simp: log_divide log_mult field_simps)
    6.90 -  qed simp
    6.91 -  thus ?mult and ?div by auto
    6.92 +  { fix u :: real
    6.93 +    have "x \<le> 0" by fact
    6.94 +    also have "0 < exp u"
    6.95 +      using exp_gt_zero .
    6.96 +    finally have "exp u \<noteq> x"
    6.97 +      by auto }
    6.98 +  then show "log b x = log b 0"
    6.99 +    by (simp add: log_def ln_def)
   6.100  qed
   6.101  
   6.102 -ML {*
   6.103 -
   6.104 -  (* tactic to solve equations of the form @{term "W * log b (X / (Y * Z)) = W * log b X - W * log b (Y * Z)"}
   6.105 -     where @{term W} is a joint distribution of @{term X}, @{term Y}, and @{term Z}. *)
   6.106 -
   6.107 -  val mult_log_intros = [@{thm mult_log_divide}, @{thm mult_log_mult}]
   6.108 -  val intros = [@{thm divide_pos_pos}, @{thm mult_pos_pos}, @{thm real_pinfreal_nonneg},
   6.109 -    @{thm real_distribution_divide_pos_pos},
   6.110 -    @{thm real_distribution_mult_inverse_pos_pos},
   6.111 -    @{thm real_distribution_mult_pos_pos}]
   6.112 -
   6.113 -  val distribution_gt_0_tac = (rtac @{thm distribution_mono_gt_0}
   6.114 -    THEN' assume_tac
   6.115 -    THEN' clarsimp_tac (clasimpset_of @{context} addsimps2 @{thms split_pairs}))
   6.116 -
   6.117 -  val distr_mult_log_eq_tac = REPEAT_ALL_NEW (CHANGED o TRY o
   6.118 -    (resolve_tac (mult_log_intros @ intros)
   6.119 -      ORELSE' distribution_gt_0_tac
   6.120 -      ORELSE' clarsimp_tac (clasimpset_of @{context})))
   6.121 -
   6.122 -  fun instanciate_term thy redex intro =
   6.123 -    let
   6.124 -      val intro_concl = Thm.concl_of intro
   6.125 +lemma log_mult_eq:
   6.126 +  "log b (A * B) = (if 0 < A * B then log b \<bar>A\<bar> + log b \<bar>B\<bar> else log b 0)"
   6.127 +  using log_mult[of b "\<bar>A\<bar>" "\<bar>B\<bar>"] b_gt_1 log_neg_const[of "A * B"]
   6.128 +  by (auto simp: zero_less_mult_iff mult_le_0_iff)
   6.129  
   6.130 -      val lhs = intro_concl |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> fst
   6.131 -
   6.132 -      val m = SOME (Pattern.match thy (lhs, redex) (Vartab.empty, Vartab.empty))
   6.133 -        handle Pattern.MATCH => NONE
   6.134 -
   6.135 -    in
   6.136 -      Option.map (fn m => Envir.subst_term m intro_concl) m
   6.137 -    end
   6.138 +lemma log_inverse_eq:
   6.139 +  "log b (inverse B) = (if 0 < B then - log b B else log b 0)"
   6.140 +  using log_inverse[of b B] log_neg_const[of "inverse B"] b_gt_1 by simp
   6.141  
   6.142 -  fun mult_log_simproc simpset redex =
   6.143 -  let
   6.144 -    val ctxt = Simplifier.the_context simpset
   6.145 -    val thy = ProofContext.theory_of ctxt
   6.146 -    fun prove (SOME thm) = (SOME
   6.147 -          (Goal.prove ctxt [] [] thm (K (distr_mult_log_eq_tac 1))
   6.148 -           |> mk_meta_eq)
   6.149 -            handle THM _ => NONE)
   6.150 -      | prove NONE = NONE
   6.151 -  in
   6.152 -    get_first (instanciate_term thy (term_of redex) #> prove) mult_log_intros
   6.153 -  end
   6.154 -*}
   6.155 +lemma log_divide_eq:
   6.156 +  "log b (A / B) = (if 0 < A * B then log b \<bar>A\<bar> - log b \<bar>B\<bar> else log b 0)"
   6.157 +  unfolding divide_inverse log_mult_eq log_inverse_eq abs_inverse
   6.158 +  by (auto simp: zero_less_mult_iff mult_le_0_iff)
   6.159  
   6.160 -simproc_setup mult_log ("real (distribution X x) * log b (A * B)" |
   6.161 -                        "real (distribution X x) * log b (A / B)") = {* K mult_log_simproc *}
   6.162 +lemmas log_simps = log_mult_eq log_inverse_eq log_divide_eq
   6.163  
   6.164  end
   6.165  
   6.166 @@ -236,15 +165,49 @@
   6.167    "KL_divergence b M \<mu> \<nu> =
   6.168      measure_space.integral M \<mu> (\<lambda>x. log b (real (sigma_finite_measure.RN_deriv M \<nu> \<mu> x)))"
   6.169  
   6.170 +lemma (in sigma_finite_measure) KL_divergence_cong:
   6.171 +  assumes "measure_space M \<nu>"
   6.172 +  and cong: "\<And>A. A \<in> sets M \<Longrightarrow> \<mu>' A = \<mu> A" "\<And>A. A \<in> sets M \<Longrightarrow> \<nu>' A = \<nu> A"
   6.173 +  shows "KL_divergence b M \<nu>' \<mu>' = KL_divergence b M \<nu> \<mu>"
   6.174 +proof -
   6.175 +  interpret \<nu>: measure_space M \<nu> by fact
   6.176 +  show ?thesis
   6.177 +    unfolding KL_divergence_def
   6.178 +    using RN_deriv_cong[OF cong, of "\<lambda>A. A"]
   6.179 +    by (simp add: cong \<nu>.integral_cong_measure[OF cong(2)])
   6.180 +qed
   6.181 +
   6.182 +lemma (in sigma_finite_measure) KL_divergence_vimage:
   6.183 +  assumes f: "bij_betw f S (space M)"
   6.184 +  assumes \<nu>: "measure_space M \<nu>" "absolutely_continuous \<nu>"
   6.185 +  shows "KL_divergence b (vimage_algebra S f) (\<lambda>A. \<nu> (f ` A)) (\<lambda>A. \<mu> (f ` A)) = KL_divergence b M \<nu> \<mu>"
   6.186 +    (is "KL_divergence b ?M ?\<nu> ?\<mu> = _")
   6.187 +proof -
   6.188 +  interpret \<nu>: measure_space M \<nu> by fact
   6.189 +  interpret v: measure_space ?M ?\<nu>
   6.190 +    using f by (rule \<nu>.measure_space_isomorphic)
   6.191 +
   6.192 +  let ?RN = "sigma_finite_measure.RN_deriv ?M ?\<mu> ?\<nu>"
   6.193 +  from RN_deriv_vimage[OF f \<nu>]
   6.194 +  have *: "\<nu>.almost_everywhere (\<lambda>x. ?RN (the_inv_into S f x) = RN_deriv \<nu> x)"
   6.195 +    by (rule absolutely_continuous_AE[OF \<nu>])
   6.196 +
   6.197 +  show ?thesis
   6.198 +    unfolding KL_divergence_def \<nu>.integral_vimage_inv[OF f]
   6.199 +    apply (rule \<nu>.integral_cong_AE)
   6.200 +    apply (rule \<nu>.AE_mp[OF *])
   6.201 +    apply (rule \<nu>.AE_cong)
   6.202 +    apply simp
   6.203 +    done
   6.204 +qed
   6.205 +
   6.206  lemma (in finite_measure_space) KL_divergence_eq_finite:
   6.207    assumes v: "finite_measure_space M \<nu>"
   6.208 -  assumes ac: "\<forall>x\<in>space M. \<mu> {x} = 0 \<longrightarrow> \<nu> {x} = 0"
   6.209 +  assumes ac: "absolutely_continuous \<nu>"
   6.210    shows "KL_divergence b M \<nu> \<mu> = (\<Sum>x\<in>space M. real (\<nu> {x}) * log b (real (\<nu> {x}) / real (\<mu> {x})))" (is "_ = ?sum")
   6.211  proof (simp add: KL_divergence_def finite_measure_space.integral_finite_singleton[OF v])
   6.212    interpret v: finite_measure_space M \<nu> by fact
   6.213    have ms: "measure_space M \<nu>" by fact
   6.214 -  have ac: "absolutely_continuous \<nu>"
   6.215 -    using ac by (auto intro!: absolutely_continuousI[OF v])
   6.216    show "(\<Sum>x \<in> space M. log b (real (RN_deriv \<nu> x)) * real (\<nu> {x})) = ?sum"
   6.217      using RN_deriv_finite_measure[OF ms ac]
   6.218      by (auto intro!: setsum_cong simp: field_simps real_of_pinfreal_mult[symmetric])
   6.219 @@ -252,32 +215,27 @@
   6.220  
   6.221  lemma (in finite_prob_space) KL_divergence_positive_finite:
   6.222    assumes v: "finite_prob_space M \<nu>"
   6.223 -  assumes ac: "\<And>x. \<lbrakk> x \<in> space M ; \<mu> {x} = 0 \<rbrakk> \<Longrightarrow> \<nu> {x} = 0"
   6.224 +  assumes ac: "absolutely_continuous \<nu>"
   6.225    and "1 < b"
   6.226    shows "0 \<le> KL_divergence b M \<nu> \<mu>"
   6.227  proof -
   6.228    interpret v: finite_prob_space M \<nu> using v .
   6.229 -
   6.230 -  have *: "space M \<noteq> {}" using not_empty by simp
   6.231 +  have ms: "finite_measure_space M \<nu>" by default
   6.232  
   6.233 -  hence "- (KL_divergence b M \<nu> \<mu>) \<le> log b (\<Sum>x\<in>space M. real (\<mu> {x}))"
   6.234 -  proof (subst KL_divergence_eq_finite)
   6.235 -    show "finite_measure_space  M \<nu>" by fact
   6.236 +  have "- (KL_divergence b M \<nu> \<mu>) \<le> log b (\<Sum>x\<in>space M. real (\<mu> {x}))"
   6.237 +  proof (subst KL_divergence_eq_finite[OF ms ac], safe intro!: log_setsum_divide not_empty)
   6.238 +    show "finite (space M)" using finite_space by simp
   6.239 +    show "1 < b" by fact
   6.240 +    show "(\<Sum>x\<in>space M. real (\<nu> {x})) = 1" using v.finite_sum_over_space_eq_1 by simp
   6.241  
   6.242 -    show "\<forall>x\<in>space M. \<mu> {x} = 0 \<longrightarrow> \<nu> {x} = 0" using ac by auto
   6.243 -    show "- (\<Sum>x\<in>space M. real (\<nu> {x}) * log b (real (\<nu> {x}) / real (\<mu> {x}))) \<le> log b (\<Sum>x\<in>space M. real (\<mu> {x}))"
   6.244 -    proof (safe intro!: log_setsum_divide *)
   6.245 -      show "finite (space M)" using finite_space by simp
   6.246 -      show "1 < b" by fact
   6.247 -      show "(\<Sum>x\<in>space M. real (\<nu> {x})) = 1" using v.finite_sum_over_space_eq_1 by simp
   6.248 -
   6.249 -      fix x assume x: "x \<in> space M"
   6.250 -      { assume "0 < real (\<nu> {x})"
   6.251 -        hence "\<mu> {x} \<noteq> 0" using ac[OF x] by auto
   6.252 -        thus "0 < prob {x}" using finite_measure[of "{x}"] sets_eq_Pow x
   6.253 -          by (cases "\<mu> {x}") simp_all }
   6.254 -    qed auto
   6.255 -  qed
   6.256 +    fix x assume "x \<in> space M"
   6.257 +    then have x: "{x} \<in> sets M" unfolding sets_eq_Pow by auto
   6.258 +    { assume "0 < real (\<nu> {x})"
   6.259 +      then have "\<nu> {x} \<noteq> 0" by auto
   6.260 +      then have "\<mu> {x} \<noteq> 0"
   6.261 +        using ac[unfolded absolutely_continuous_def, THEN bspec, of "{x}"] x by auto
   6.262 +      thus "0 < prob {x}" using finite_measure[of "{x}"] x by auto }
   6.263 +  qed auto
   6.264    thus "0 \<le> KL_divergence b M \<nu> \<mu>" using finite_sum_over_space_eq_1 by simp
   6.265  qed
   6.266  
   6.267 @@ -285,174 +243,175 @@
   6.268  
   6.269  definition (in prob_space)
   6.270    "mutual_information b S T X Y =
   6.271 -    KL_divergence b (prod_measure_space S T)
   6.272 +    KL_divergence b (sigma (pair_algebra S T))
   6.273        (joint_distribution X Y)
   6.274 -      (prod_measure S (distribution X) T (distribution Y))"
   6.275 +      (pair_sigma_finite.pair_measure S (distribution X) T (distribution Y))"
   6.276  
   6.277 -abbreviation (in finite_information_space)
   6.278 -  finite_mutual_information ("\<I>'(_ ; _')") where
   6.279 +definition (in prob_space)
   6.280 +  "entropy b s X = mutual_information b s s X X"
   6.281 +
   6.282 +abbreviation (in information_space)
   6.283 +  mutual_information_Pow ("\<I>'(_ ; _')") where
   6.284    "\<I>(X ; Y) \<equiv> mutual_information b
   6.285      \<lparr> space = X`space M, sets = Pow (X`space M) \<rparr>
   6.286      \<lparr> space = Y`space M, sets = Pow (Y`space M) \<rparr> X Y"
   6.287  
   6.288 -lemma (in finite_information_space) mutual_information_generic_eq:
   6.289 -  assumes MX: "finite_measure_space MX (distribution X)"
   6.290 -  assumes MY: "finite_measure_space MY (distribution Y)"
   6.291 -  shows "mutual_information b MX MY X Y = (\<Sum> (x,y) \<in> space MX \<times> space MY.
   6.292 -      real (joint_distribution X Y {(x,y)}) *
   6.293 -      log b (real (joint_distribution X Y {(x,y)}) /
   6.294 -      (real (distribution X {x}) * real (distribution Y {y}))))"
   6.295 +lemma (in information_space) mutual_information_commute_generic:
   6.296 +  assumes X: "random_variable S X" and Y: "random_variable T Y"
   6.297 +  assumes ac: "measure_space.absolutely_continuous (sigma (pair_algebra S T))
   6.298 +   (pair_sigma_finite.pair_measure S (distribution X) T (distribution Y)) (joint_distribution X Y)"
   6.299 +  shows "mutual_information b S T X Y = mutual_information b T S Y X"
   6.300  proof -
   6.301 -  let ?P = "prod_measure_space MX MY"
   6.302 -  let ?\<mu> = "prod_measure MX (distribution X) MY (distribution Y)"
   6.303 -  let ?\<nu> = "joint_distribution X Y"
   6.304 -  interpret X: finite_measure_space MX "distribution X" by fact
   6.305 -  moreover interpret Y: finite_measure_space MY "distribution Y" by fact
   6.306 -  have fms: "finite_measure_space MX (distribution X)"
   6.307 -            "finite_measure_space MY (distribution Y)" by fact+
   6.308 -  have fms_P: "finite_measure_space ?P ?\<mu>"
   6.309 -    by (rule X.finite_measure_space_finite_prod_measure) fact
   6.310 -  then interpret P: finite_measure_space ?P ?\<mu> .
   6.311 -  have fms_P': "finite_measure_space ?P ?\<nu>"
   6.312 -      using finite_product_measure_space[of "space MX" "space MY"]
   6.313 -        X.finite_space Y.finite_space sigma_prod_sets_finite[OF X.finite_space Y.finite_space]
   6.314 -        X.sets_eq_Pow Y.sets_eq_Pow
   6.315 -      by (simp add: prod_measure_space_def sigma_def)
   6.316 -  then interpret P': finite_measure_space ?P ?\<nu> .
   6.317 -  { fix x assume "x \<in> space ?P"
   6.318 -    hence in_MX: "{fst x} \<in> sets MX" "{snd x} \<in> sets MY" using X.sets_eq_Pow Y.sets_eq_Pow
   6.319 -      by (auto simp: prod_measure_space_def)
   6.320 -    assume "?\<mu> {x} = 0"
   6.321 -    with X.finite_prod_measure_times[OF fms(2), of "{fst x}" "{snd x}"] in_MX
   6.322 -    have "distribution X {fst x} = 0 \<or> distribution Y {snd x} = 0"
   6.323 -      by (simp add: prod_measure_space_def)
   6.324 -    hence "joint_distribution X Y {x} = 0"
   6.325 -      by (cases x) (auto simp: distribution_order) }
   6.326 -  note measure_0 = this
   6.327 +  interpret P: prob_space "sigma (pair_algebra S T)" "joint_distribution X Y"
   6.328 +    using random_variable_pairI[OF X Y] by (rule distribution_prob_space)
   6.329 +  interpret Q: prob_space "sigma (pair_algebra T S)" "joint_distribution Y X"
   6.330 +    using random_variable_pairI[OF Y X] by (rule distribution_prob_space)
   6.331 +  interpret X: prob_space S "distribution X" using X by (rule distribution_prob_space)
   6.332 +  interpret Y: prob_space T "distribution Y" using Y by (rule distribution_prob_space)
   6.333 +  interpret ST: pair_sigma_finite S "distribution X" T "distribution Y" by default
   6.334 +  interpret TS: pair_sigma_finite T "distribution Y" S "distribution X" by default
   6.335 +
   6.336 +  have ST: "measure_space (sigma (pair_algebra S T)) (joint_distribution X Y)" by default
   6.337 +  have TS: "measure_space (sigma (pair_algebra T S)) (joint_distribution Y X)" by default
   6.338 +
   6.339 +  have bij_ST: "bij_betw (\<lambda>(x, y). (y, x)) (space (sigma (pair_algebra S T))) (space (sigma (pair_algebra T S)))"
   6.340 +    by (auto intro!: inj_onI simp: space_pair_algebra bij_betw_def)
   6.341 +  have bij_TS: "bij_betw (\<lambda>(x, y). (y, x)) (space (sigma (pair_algebra T S))) (space (sigma (pair_algebra S T)))"
   6.342 +    by (auto intro!: inj_onI simp: space_pair_algebra bij_betw_def)
   6.343 +
   6.344 +  { fix A
   6.345 +    have "joint_distribution X Y ((\<lambda>(x, y). (y, x)) ` A) = joint_distribution Y X A"
   6.346 +      unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>]) }
   6.347 +  note jd_commute = this
   6.348 +
   6.349 +  { fix A assume A: "A \<in> sets (sigma (pair_algebra T S))"
   6.350 +    have *: "\<And>x y. indicator ((\<lambda>(x, y). (y, x)) ` A) (x, y) = (indicator A (y, x) :: pinfreal)"
   6.351 +      unfolding indicator_def by auto
   6.352 +    have "ST.pair_measure ((\<lambda>(x, y). (y, x)) ` A) = TS.pair_measure A"
   6.353 +      unfolding ST.pair_measure_def TS.pair_measure_def
   6.354 +      using A by (auto simp add: TS.Fubini[symmetric] *) }
   6.355 +  note pair_measure_commute = this
   6.356 +
   6.357    show ?thesis
   6.358 -    unfolding Let_def mutual_information_def
   6.359 -    using measure_0 fms_P fms_P' MX MY P.absolutely_continuous_def
   6.360 -    by (subst P.KL_divergence_eq_finite)
   6.361 -       (auto simp add: prod_measure_space_def prod_measure_times_finite
   6.362 -         finite_prob_space_eq setsum_cartesian_product' real_of_pinfreal_mult[symmetric])
   6.363 +    unfolding mutual_information_def
   6.364 +    unfolding ST.KL_divergence_vimage[OF bij_TS ST ac, symmetric]
   6.365 +    unfolding space_sigma space_pair_algebra jd_commute
   6.366 +    unfolding ST.pair_sigma_algebra_swap[symmetric]
   6.367 +    by (simp cong: TS.KL_divergence_cong[OF TS] add: pair_measure_commute)
   6.368  qed
   6.369  
   6.370 -lemma (in finite_information_space)
   6.371 -  assumes MX: "finite_prob_space MX (distribution X)"
   6.372 -  assumes MY: "finite_prob_space MY (distribution Y)"
   6.373 -  and X_space: "X ` space M \<subseteq> space MX" and Y_space: "Y ` space M \<subseteq> space MY"
   6.374 -  shows mutual_information_eq_generic:
   6.375 +lemma (in prob_space) finite_variables_absolutely_continuous:
   6.376 +  assumes X: "finite_random_variable S X" and Y: "finite_random_variable T Y"
   6.377 +  shows "measure_space.absolutely_continuous (sigma (pair_algebra S T))
   6.378 +   (pair_sigma_finite.pair_measure S (distribution X) T (distribution Y)) (joint_distribution X Y)"
   6.379 +proof -
   6.380 +  interpret X: finite_prob_space S "distribution X" using X by (rule distribution_finite_prob_space)
   6.381 +  interpret Y: finite_prob_space T "distribution Y" using Y by (rule distribution_finite_prob_space)
   6.382 +  interpret XY: pair_finite_prob_space S "distribution X" T "distribution Y" by default
   6.383 +  interpret P: finite_prob_space XY.P "joint_distribution X Y"
   6.384 +    using assms by (intro joint_distribution_finite_prob_space)
   6.385 +  show "XY.absolutely_continuous (joint_distribution X Y)"
   6.386 +  proof (rule XY.absolutely_continuousI)
   6.387 +    show "finite_measure_space XY.P (joint_distribution X Y)" by default
   6.388 +    fix x assume "x \<in> space XY.P" and "XY.pair_measure {x} = 0"
   6.389 +    then obtain a b where "(a, b) = x" and "a \<in> space S" "b \<in> space T"
   6.390 +      and distr: "distribution X {a} * distribution Y {b} = 0"
   6.391 +      by (cases x) (auto simp: pair_algebra_def)
   6.392 +    with assms[THEN finite_random_variableD]
   6.393 +      joint_distribution_Times_le_fst[of S X T Y "{a}" "{b}"]
   6.394 +      joint_distribution_Times_le_snd[of S X T Y "{a}" "{b}"]
   6.395 +    have "joint_distribution X Y {x} \<le> distribution Y {b}"
   6.396 +         "joint_distribution X Y {x} \<le> distribution X {a}"
   6.397 +      by auto
   6.398 +    with distr show "joint_distribution X Y {x} = 0" by auto
   6.399 +  qed
   6.400 +qed
   6.401 +
   6.402 +lemma (in information_space) mutual_information_commute:
   6.403 +  assumes X: "finite_random_variable S X" and Y: "finite_random_variable T Y"
   6.404 +  shows "mutual_information b S T X Y = mutual_information b T S Y X"
   6.405 +  by (intro finite_random_variableD X Y mutual_information_commute_generic finite_variables_absolutely_continuous)
   6.406 +
   6.407 +lemma (in information_space) mutual_information_commute_simple:
   6.408 +  assumes X: "simple_function X" and Y: "simple_function Y"
   6.409 +  shows "\<I>(X;Y) = \<I>(Y;X)"
   6.410 +  by (intro X Y simple_function_imp_finite_random_variable mutual_information_commute)
   6.411 +
   6.412 +lemma (in information_space)
   6.413 +  assumes MX: "finite_random_variable MX X"
   6.414 +  assumes MY: "finite_random_variable MY Y"
   6.415 +  shows mutual_information_generic_eq:
   6.416      "mutual_information b MX MY X Y = (\<Sum> (x,y) \<in> space MX \<times> space MY.
   6.417        real (joint_distribution X Y {(x,y)}) *
   6.418        log b (real (joint_distribution X Y {(x,y)}) /
   6.419        (real (distribution X {x}) * real (distribution Y {y}))))"
   6.420 -    (is "?equality")
   6.421 +    (is ?sum)
   6.422    and mutual_information_positive_generic:
   6.423 -    "0 \<le> mutual_information b MX MY X Y" (is "?positive")
   6.424 +     "0 \<le> mutual_information b MX MY X Y" (is ?positive)
   6.425  proof -
   6.426 -  let ?P = "prod_measure_space MX MY"
   6.427 -  let ?\<mu> = "prod_measure MX (distribution X) MY (distribution Y)"
   6.428 -  let ?\<nu> = "joint_distribution X Y"
   6.429 -
   6.430 -  interpret X: finite_prob_space MX "distribution X" by fact
   6.431 -  moreover interpret Y: finite_prob_space MY "distribution Y" by fact
   6.432 -  have ms_X: "measure_space MX (distribution X)"
   6.433 -    and ms_Y: "measure_space MY (distribution Y)"
   6.434 -    and fms: "finite_measure_space MX (distribution X)" "finite_measure_space MY (distribution Y)" by fact+
   6.435 -  have fms_P: "finite_measure_space ?P ?\<mu>"
   6.436 -    by (rule X.finite_measure_space_finite_prod_measure) fact
   6.437 -  then interpret P: finite_measure_space ?P ?\<mu> .
   6.438 +  interpret X: finite_prob_space MX "distribution X" using MX by (rule distribution_finite_prob_space)
   6.439 +  interpret Y: finite_prob_space MY "distribution Y" using MY by (rule distribution_finite_prob_space)
   6.440 +  interpret XY: pair_finite_prob_space MX "distribution X" MY "distribution Y" by default
   6.441 +  interpret P: finite_prob_space XY.P "joint_distribution X Y"
   6.442 +    using assms by (intro joint_distribution_finite_prob_space)
   6.443  
   6.444 -  have fms_P': "finite_measure_space ?P ?\<nu>"
   6.445 -      using finite_product_measure_space[of "space MX" "space MY"]
   6.446 -        X.finite_space Y.finite_space sigma_prod_sets_finite[OF X.finite_space Y.finite_space]
   6.447 -        X.sets_eq_Pow Y.sets_eq_Pow
   6.448 -      by (simp add: prod_measure_space_def sigma_def)
   6.449 -  then interpret P': finite_measure_space ?P ?\<nu> .
   6.450 -
   6.451 -  { fix x assume "x \<in> space ?P"
   6.452 -    hence in_MX: "{fst x} \<in> sets MX" "{snd x} \<in> sets MY" using X.sets_eq_Pow Y.sets_eq_Pow
   6.453 -      by (auto simp: prod_measure_space_def)
   6.454 +  have P_ms: "finite_measure_space XY.P (joint_distribution X Y)" by default
   6.455 +  have P_ps: "finite_prob_space XY.P (joint_distribution X Y)" by default
   6.456  
   6.457 -    assume "?\<mu> {x} = 0"
   6.458 -    with X.finite_prod_measure_times[OF fms(2), of "{fst x}" "{snd x}"] in_MX
   6.459 -    have "distribution X {fst x} = 0 \<or> distribution Y {snd x} = 0"
   6.460 -      by (simp add: prod_measure_space_def)
   6.461 -
   6.462 -    hence "joint_distribution X Y {x} = 0"
   6.463 -      by (cases x) (auto simp: distribution_order) }
   6.464 -  note measure_0 = this
   6.465 -
   6.466 -  show ?equality
   6.467 +  show ?sum
   6.468      unfolding Let_def mutual_information_def
   6.469 -    using measure_0 fms_P fms_P' MX MY P.absolutely_continuous_def
   6.470 -    by (subst P.KL_divergence_eq_finite)
   6.471 -       (auto simp add: prod_measure_space_def prod_measure_times_finite
   6.472 -         finite_prob_space_eq setsum_cartesian_product' real_of_pinfreal_mult[symmetric])
   6.473 +    by (subst XY.KL_divergence_eq_finite[OF P_ms finite_variables_absolutely_continuous[OF MX MY]])
   6.474 +       (auto simp add: pair_algebra_def setsum_cartesian_product' real_of_pinfreal_mult[symmetric])
   6.475  
   6.476    show ?positive
   6.477 -    unfolding Let_def mutual_information_def using measure_0 b_gt_1
   6.478 -  proof (safe intro!: finite_prob_space.KL_divergence_positive_finite, simp_all)
   6.479 -    have "?\<mu> (space ?P) = 1"
   6.480 -      using X.top Y.top X.measure_space_1 Y.measure_space_1 fms
   6.481 -      by (simp add: prod_measure_space_def X.finite_prod_measure_times)
   6.482 -    with fms_P show "finite_prob_space ?P ?\<mu>"
   6.483 -      by (simp add: finite_prob_space_eq)
   6.484 -
   6.485 -    from ms_X ms_Y X.top Y.top X.measure_space_1 Y.measure_space_1 Y.not_empty X_space Y_space
   6.486 -    have "?\<nu> (space ?P) = 1" unfolding measure_space_1[symmetric]
   6.487 -      by (auto intro!: arg_cong[where f="\<mu>"]
   6.488 -               simp add: prod_measure_space_def distribution_def vimage_Times comp_def)
   6.489 -    with fms_P' show "finite_prob_space ?P ?\<nu>"
   6.490 -      by (simp add: finite_prob_space_eq)
   6.491 -  qed
   6.492 +    using XY.KL_divergence_positive_finite[OF P_ps finite_variables_absolutely_continuous[OF MX MY] b_gt_1]
   6.493 +    unfolding mutual_information_def .
   6.494  qed
   6.495  
   6.496 -lemma (in finite_information_space) mutual_information_eq:
   6.497 -  "\<I>(X;Y) = (\<Sum> (x,y) \<in> X ` space M \<times> Y ` space M.
   6.498 +lemma (in information_space) mutual_information_eq:
   6.499 +  assumes "simple_function X" "simple_function Y"
   6.500 +  shows "\<I>(X;Y) = (\<Sum> (x,y) \<in> X ` space M \<times> Y ` space M.
   6.501      real (distribution (\<lambda>x. (X x, Y x)) {(x,y)}) * log b (real (distribution (\<lambda>x. (X x, Y x)) {(x,y)}) /
   6.502                                                     (real (distribution X {x}) * real (distribution Y {y}))))"
   6.503 -  by (subst mutual_information_eq_generic) (simp_all add: finite_prob_space_of_images)
   6.504 +  using assms by (simp add: mutual_information_generic_eq)
   6.505  
   6.506 -lemma (in finite_information_space) mutual_information_cong:
   6.507 +lemma (in information_space) mutual_information_generic_cong:
   6.508    assumes X: "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
   6.509    assumes Y: "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
   6.510 -  shows "\<I>(X ; Y) = \<I>(X' ; Y')"
   6.511 -proof -
   6.512 -  have "X ` space M = X' ` space M" using X by (auto intro!: image_eqI)
   6.513 -  moreover have "Y ` space M = Y' ` space M" using Y by (auto intro!: image_eqI)
   6.514 -  ultimately show ?thesis
   6.515 -  unfolding mutual_information_eq
   6.516 -    using
   6.517 -      assms[THEN distribution_cong]
   6.518 -      joint_distribution_cong[OF assms]
   6.519 -    by (auto intro!: setsum_cong)
   6.520 -qed
   6.521 +  shows "mutual_information b MX MY X Y = mutual_information b MX MY X' Y'"
   6.522 +  unfolding mutual_information_def using X Y
   6.523 +  by (simp cong: distribution_cong)
   6.524  
   6.525 -lemma (in finite_information_space) mutual_information_positive: "0 \<le> \<I>(X;Y)"
   6.526 -  by (subst mutual_information_positive_generic) (simp_all add: finite_prob_space_of_images)
   6.527 +lemma (in information_space) mutual_information_cong:
   6.528 +  assumes X: "\<And>x. x \<in> space M \<Longrightarrow> X x = X' x"
   6.529 +  assumes Y: "\<And>x. x \<in> space M \<Longrightarrow> Y x = Y' x"
   6.530 +  shows "\<I>(X; Y) = \<I>(X'; Y')"
   6.531 +  unfolding mutual_information_def using X Y
   6.532 +  by (simp cong: distribution_cong image_cong)
   6.533 +
   6.534 +lemma (in information_space) mutual_information_positive:
   6.535 +  assumes "simple_function X" "simple_function Y"
   6.536 +  shows "0 \<le> \<I>(X;Y)"
   6.537 +  using assms by (simp add: mutual_information_positive_generic)
   6.538  
   6.539  subsection {* Entropy *}
   6.540  
   6.541 -definition (in prob_space)
   6.542 -  "entropy b s X = mutual_information b s s X X"
   6.543 -
   6.544 -abbreviation (in finite_information_space)
   6.545 -  finite_entropy ("\<H>'(_')") where
   6.546 +abbreviation (in information_space)
   6.547 +  entropy_Pow ("\<H>'(_')") where
   6.548    "\<H>(X) \<equiv> entropy b \<lparr> space = X`space M, sets = Pow (X`space M) \<rparr> X"
   6.549  
   6.550 -lemma (in finite_information_space) entropy_generic_eq:
   6.551 -  assumes MX: "finite_measure_space MX (distribution X)"
   6.552 +lemma (in information_space) entropy_generic_eq:
   6.553 +  assumes MX: "finite_random_variable MX X"
   6.554    shows "entropy b MX X = -(\<Sum> x \<in> space MX. real (distribution X {x}) * log b (real (distribution X {x})))"
   6.555  proof -
   6.556 +  interpret MX: finite_prob_space MX "distribution X" using MX by (rule distribution_finite_prob_space)
   6.557    let "?X x" = "real (distribution X {x})"
   6.558    let "?XX x y" = "real (joint_distribution X X {(x, y)})"
   6.559 -  interpret MX: finite_measure_space MX "distribution X" by fact
   6.560    { fix x y
   6.561      have "(\<lambda>x. (X x, X x)) -` {(x, y)} = (if x = y then X -` {x} else {})" by auto
   6.562      then have "?XX x y * log b (?XX x y / (?X x * ?X y)) =
   6.563          (if x = y then - ?X y * log b (?X y) else 0)"
   6.564 -      unfolding distribution_def by (auto simp: mult_log_divide) }
   6.565 +      unfolding distribution_def by (auto simp: log_simps zero_less_mult_iff) }
   6.566    note remove_XX = this
   6.567    show ?thesis
   6.568      unfolding entropy_def mutual_information_generic_eq[OF MX MX]
   6.569 @@ -460,201 +419,327 @@
   6.570      by (auto simp: setsum_cases MX.finite_space)
   6.571  qed
   6.572  
   6.573 -lemma (in finite_information_space) entropy_eq:
   6.574 -  "\<H>(X) = -(\<Sum> x \<in> X ` space M. real (distribution X {x}) * log b (real (distribution X {x})))"
   6.575 -  by (simp add: finite_measure_space entropy_generic_eq)
   6.576 +lemma (in information_space) entropy_eq:
   6.577 +  assumes "simple_function X"
   6.578 +  shows "\<H>(X) = -(\<Sum> x \<in> X ` space M. real (distribution X {x}) * log b (real (distribution X {x})))"
   6.579 +  using assms by (simp add: entropy_generic_eq)
   6.580  
   6.581 -lemma (in finite_information_space) entropy_positive: "0 \<le> \<H>(X)"
   6.582 -  unfolding entropy_def using mutual_information_positive .
   6.583 +lemma (in information_space) entropy_positive:
   6.584 +  "simple_function X \<Longrightarrow> 0 \<le> \<H>(X)"
   6.585 +  unfolding entropy_def by (simp add: mutual_information_positive)
   6.586  
   6.587 -lemma (in finite_information_space) entropy_certainty_eq_0:
   6.588 -  assumes "x \<in> X ` space M" and "distribution X {x} = 1"
   6.589 +lemma (in information_space) entropy_certainty_eq_0:
   6.590 +  assumes "simple_function X" and "x \<in> X ` space M" and "distribution X {x} = 1"
   6.591    shows "\<H>(X) = 0"
   6.592  proof -
   6.593    interpret X: finite_prob_space "\<lparr> space = X ` space M, sets = Pow (X ` space M) \<rparr>" "distribution X"
   6.594 -    by (rule finite_prob_space_of_images)
   6.595 -
   6.596 +    using simple_function_imp_finite_random_variable[OF `simple_function X`]
   6.597 +    by (rule distribution_finite_prob_space)
   6.598    have "distribution X (X ` space M - {x}) = distribution X (X ` space M) - distribution X {x}"
   6.599      using X.measure_compl[of "{x}"] assms by auto
   6.600    also have "\<dots> = 0" using X.prob_space assms by auto
   6.601    finally have X0: "distribution X (X ` space M - {x}) = 0" by auto
   6.602 -
   6.603    { fix y assume asm: "y \<noteq> x" "y \<in> X ` space M"
   6.604      hence "{y} \<subseteq> X ` space M - {x}" by auto
   6.605      from X.measure_mono[OF this] X0 asm
   6.606      have "distribution X {y} = 0" by auto }
   6.607 -
   6.608    hence fi: "\<And> y. y \<in> X ` space M \<Longrightarrow> real (distribution X {y}) = (if x = y then 1 else 0)"
   6.609      using assms by auto
   6.610 -
   6.611    have y: "\<And>y. (if x = y then 1 else 0) * log b (if x = y then 1 else 0) = 0" by simp
   6.612 -
   6.613 -  show ?thesis unfolding entropy_eq by (auto simp: y fi)
   6.614 +  show ?thesis unfolding entropy_eq[OF `simple_function X`] by (auto simp: y fi)
   6.615  qed
   6.616  
   6.617 -lemma (in finite_information_space) entropy_le_card_not_0:
   6.618 -  "\<H>(X) \<le> log b (real (card (X ` space M \<inter> {x . distribution X {x} \<noteq> 0})))"
   6.619 +lemma (in information_space) entropy_le_card_not_0:
   6.620 +  assumes "simple_function X"
   6.621 +  shows "\<H>(X) \<le> log b (real (card (X ` space M \<inter> {x . distribution X {x} \<noteq> 0})))"
   6.622  proof -
   6.623    let "?d x" = "distribution X {x}"
   6.624    let "?p x" = "real (?d x)"
   6.625    have "\<H>(X) = (\<Sum>x\<in>X`space M. ?p x * log b (1 / ?p x))"
   6.626 -    by (auto intro!: setsum_cong simp: entropy_eq setsum_negf[symmetric])
   6.627 +    by (auto intro!: setsum_cong simp: entropy_eq[OF `simple_function X`] setsum_negf[symmetric] log_simps not_less)
   6.628    also have "\<dots> \<le> log b (\<Sum>x\<in>X`space M. ?p x * (1 / ?p x))"
   6.629      apply (rule log_setsum')
   6.630 -    using not_empty b_gt_1 finite_space sum_over_space_real_distribution
   6.631 -    by auto
   6.632 +    using not_empty b_gt_1 `simple_function X` sum_over_space_real_distribution
   6.633 +    by (auto simp: simple_function_def)
   6.634    also have "\<dots> = log b (\<Sum>x\<in>X`space M. if ?d x \<noteq> 0 then 1 else 0)"
   6.635 -    apply (rule arg_cong[where f="\<lambda>f. log b (\<Sum>x\<in>X`space M. f x)"])
   6.636 -    using distribution_finite[of X] by (auto simp: fun_eq_iff real_of_pinfreal_eq_0)
   6.637 +    using distribution_finite[OF `simple_function X`[THEN simple_function_imp_random_variable], simplified]
   6.638 +    by (intro arg_cong[where f="\<lambda>X. log b X"] setsum_cong) (auto simp: real_of_pinfreal_eq_0)
   6.639    finally show ?thesis
   6.640 -    using finite_space by (auto simp: setsum_cases real_eq_of_nat)
   6.641 +    using `simple_function X` by (auto simp: setsum_cases real_eq_of_nat simple_function_def)
   6.642  qed
   6.643  
   6.644 -lemma (in finite_information_space) entropy_uniform_max:
   6.645 +lemma (in information_space) entropy_uniform_max:
   6.646 +  assumes "simple_function X"
   6.647    assumes "\<And>x y. \<lbrakk> x \<in> X ` space M ; y \<in> X ` space M \<rbrakk> \<Longrightarrow> distribution X {x} = distribution X {y}"
   6.648    shows "\<H>(X) = log b (real (card (X ` space M)))"
   6.649  proof -
   6.650 -  note uniform =
   6.651 -    finite_prob_space_of_images[of X, THEN finite_prob_space.uniform_prob, simplified]
   6.652 -
   6.653 +  interpret X: finite_prob_space "\<lparr> space = X ` space M, sets = Pow (X ` space M) \<rparr>" "distribution X"
   6.654 +    using simple_function_imp_finite_random_variable[OF `simple_function X`]
   6.655 +    by (rule distribution_finite_prob_space)
   6.656    have card_gt0: "0 < card (X ` space M)" unfolding card_gt_0_iff
   6.657 -    using finite_space not_empty by auto
   6.658 -
   6.659 +    using `simple_function X` not_empty by (auto simp: simple_function_def)
   6.660    { fix x assume "x \<in> X ` space M"
   6.661      hence "real (distribution X {x}) = 1 / real (card (X ` space M))"
   6.662 -    proof (rule uniform)
   6.663 +    proof (rule X.uniform_prob[simplified])
   6.664        fix x y assume "x \<in> X`space M" "y \<in> X`space M"
   6.665 -      from assms[OF this] show "real (distribution X {x}) = real (distribution X {y})" by simp
   6.666 +      from assms(2)[OF this] show "real (distribution X {x}) = real (distribution X {y})" by simp
   6.667      qed }
   6.668    thus ?thesis
   6.669 -    using not_empty finite_space b_gt_1 card_gt0
   6.670 -    by (simp add: entropy_eq real_eq_of_nat[symmetric] log_divide)
   6.671 +    using not_empty X.finite_space b_gt_1 card_gt0
   6.672 +    by (simp add: entropy_eq[OF `simple_function X`] real_eq_of_nat[symmetric] log_simps)
   6.673  qed
   6.674  
   6.675 -lemma (in finite_information_space) entropy_le_card:
   6.676 -  "\<H>(X) \<le> log b (real (card (X ` space M)))"
   6.677 +lemma (in information_space) entropy_le_card:
   6.678 +  assumes "simple_function X"
   6.679 +  shows "\<H>(X) \<le> log b (real (card (X ` space M)))"
   6.680  proof cases
   6.681    assume "X ` space M \<inter> {x. distribution X {x} \<noteq> 0} = {}"
   6.682    then have "\<And>x. x\<in>X`space M \<Longrightarrow> distribution X {x} = 0" by auto
   6.683    moreover
   6.684    have "0 < card (X`space M)"
   6.685 -    using finite_space not_empty unfolding card_gt_0_iff by auto
   6.686 +    using `simple_function X` not_empty
   6.687 +    by (auto simp: card_gt_0_iff simple_function_def)
   6.688    then have "log b 1 \<le> log b (real (card (X`space M)))"
   6.689      using b_gt_1 by (intro log_le) auto
   6.690 -  ultimately show ?thesis unfolding entropy_eq by simp
   6.691 +  ultimately show ?thesis using assms by (simp add: entropy_eq)
   6.692  next
   6.693    assume False: "X ` space M \<inter> {x. distribution X {x} \<noteq> 0} \<noteq> {}"
   6.694    have "card (X ` space M \<inter> {x. distribution X {x} \<noteq> 0}) \<le> card (X ` space M)"
   6.695 -    (is "?A \<le> ?B") using finite_space not_empty by (auto intro!: card_mono)
   6.696 -  note entropy_le_card_not_0
   6.697 +    (is "?A \<le> ?B") using assms not_empty by (auto intro!: card_mono simp: simple_function_def)
   6.698 +  note entropy_le_card_not_0[OF assms]
   6.699    also have "log b (real ?A) \<le> log b (real ?B)"
   6.700 -    using b_gt_1 False finite_space not_empty `?A \<le> ?B`
   6.701 -    by (auto intro!: log_le simp: card_gt_0_iff)
   6.702 +    using b_gt_1 False not_empty `?A \<le> ?B` assms
   6.703 +    by (auto intro!: log_le simp: card_gt_0_iff simp: simple_function_def)
   6.704    finally show ?thesis .
   6.705  qed
   6.706  
   6.707 -lemma (in finite_information_space) entropy_commute:
   6.708 -  "\<H>(\<lambda>x. (X x, Y x)) = \<H>(\<lambda>x. (Y x, X x))"
   6.709 +lemma (in information_space) entropy_commute:
   6.710 +  assumes "simple_function X" "simple_function Y"
   6.711 +  shows "\<H>(\<lambda>x. (X x, Y x)) = \<H>(\<lambda>x. (Y x, X x))"
   6.712  proof -
   6.713 +  have sf: "simple_function (\<lambda>x. (X x, Y x))" "simple_function (\<lambda>x. (Y x, X x))"
   6.714 +    using assms by (auto intro: simple_function_Pair)
   6.715    have *: "(\<lambda>x. (Y x, X x))`space M = (\<lambda>(a,b). (b,a))`(\<lambda>x. (X x, Y x))`space M"
   6.716      by auto
   6.717    have inj: "\<And>X. inj_on (\<lambda>(a,b). (b,a)) X"
   6.718      by (auto intro!: inj_onI)
   6.719    show ?thesis
   6.720 -    unfolding entropy_eq unfolding * setsum_reindex[OF inj]
   6.721 +    unfolding sf[THEN entropy_eq] unfolding * setsum_reindex[OF inj]
   6.722      by (simp add: joint_distribution_commute[of Y X] split_beta)
   6.723  qed
   6.724  
   6.725 -lemma (in finite_information_space) entropy_eq_cartesian_sum:
   6.726 -  "\<H>(\<lambda>x. (X x, Y x)) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
   6.727 +lemma (in information_space) entropy_eq_cartesian_product:
   6.728 +  assumes "simple_function X" "simple_function Y"
   6.729 +  shows "\<H>(\<lambda>x. (X x, Y x)) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
   6.730      real (joint_distribution X Y {(x,y)}) *
   6.731      log b (real (joint_distribution X Y {(x,y)})))"
   6.732  proof -
   6.733 +  have sf: "simple_function (\<lambda>x. (X x, Y x))"
   6.734 +    using assms by (auto intro: simple_function_Pair)
   6.735    { fix x assume "x\<notin>(\<lambda>x. (X x, Y x))`space M"
   6.736      then have "(\<lambda>x. (X x, Y x)) -` {x} \<inter> space M = {}" by auto
   6.737      then have "joint_distribution X Y {x} = 0"
   6.738        unfolding distribution_def by auto }
   6.739 -  then show ?thesis using finite_space
   6.740 -    unfolding entropy_eq neg_equal_iff_equal setsum_cartesian_product
   6.741 -    by (auto intro!: setsum_mono_zero_cong_left)
   6.742 +  then show ?thesis using sf assms
   6.743 +    unfolding entropy_eq[OF sf] neg_equal_iff_equal setsum_cartesian_product
   6.744 +    by (auto intro!: setsum_mono_zero_cong_left simp: simple_function_def)
   6.745  qed
   6.746  
   6.747  subsection {* Conditional Mutual Information *}
   6.748  
   6.749  definition (in prob_space)
   6.750    "conditional_mutual_information b M1 M2 M3 X Y Z \<equiv>
   6.751 -    mutual_information b M1 (prod_measure_space M2 M3) X (\<lambda>x. (Y x, Z x)) -
   6.752 +    mutual_information b M1 (sigma (pair_algebra M2 M3)) X (\<lambda>x. (Y x, Z x)) -
   6.753      mutual_information b M1 M3 X Z"
   6.754  
   6.755 -abbreviation (in finite_information_space)
   6.756 -  finite_conditional_mutual_information ("\<I>'( _ ; _ | _ ')") where
   6.757 +abbreviation (in information_space)
   6.758 +  conditional_mutual_information_Pow ("\<I>'( _ ; _ | _ ')") where
   6.759    "\<I>(X ; Y | Z) \<equiv> conditional_mutual_information b
   6.760      \<lparr> space = X`space M, sets = Pow (X`space M) \<rparr>
   6.761      \<lparr> space = Y`space M, sets = Pow (Y`space M) \<rparr>
   6.762      \<lparr> space = Z`space M, sets = Pow (Z`space M) \<rparr>
   6.763      X Y Z"
   6.764  
   6.765 -lemma (in finite_information_space) conditional_mutual_information_generic_eq:
   6.766 -  assumes MX: "finite_measure_space MX (distribution X)"
   6.767 -  assumes MY: "finite_measure_space MY (distribution Y)"
   6.768 -  assumes MZ: "finite_measure_space MZ (distribution Z)"
   6.769 -  shows "conditional_mutual_information b MX MY MZ X Y Z =
   6.770 -    (\<Sum>(x, y, z)\<in>space MX \<times> space MY \<times> space MZ.
   6.771 -      real (joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)}) *
   6.772 -      log b (real (joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)}) /
   6.773 -                   (real (distribution X {x}) * real (joint_distribution Y Z {(y, z)})))) -
   6.774 -    (\<Sum>(x, y)\<in>space MX \<times> space MZ.
   6.775 -      real (joint_distribution X Z {(x, y)}) *
   6.776 -      log b (real (joint_distribution X Z {(x, y)}) / (real (distribution X {x}) * real (distribution Z {y}))))"
   6.777 -  using assms finite_measure_space_prod[OF MY MZ]
   6.778 -  unfolding conditional_mutual_information_def
   6.779 -  by (subst (1 2) mutual_information_generic_eq)
   6.780 -     (simp_all add: setsum_cartesian_product' finite_measure_space.finite_prod_measure_space)
   6.781  
   6.782 -
   6.783 -lemma (in finite_information_space) conditional_mutual_information_eq:
   6.784 -  "\<I>(X ; Y | Z) = (\<Sum>(x, y, z) \<in> X ` space M \<times> Y ` space M \<times> Z ` space M.
   6.785 +lemma (in information_space) conditional_mutual_information_generic_eq:
   6.786 +  assumes MX: "finite_random_variable MX X"
   6.787 +    and MY: "finite_random_variable MY Y"
   6.788 +    and MZ: "finite_random_variable MZ Z"
   6.789 +  shows "conditional_mutual_information b MX MY MZ X Y Z = (\<Sum>(x, y, z) \<in> space MX \<times> space MY \<times> space MZ.
   6.790               real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) *
   6.791               log b (real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) /
   6.792      (real (joint_distribution X Z {(x, z)}) * real (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
   6.793 -  by (subst conditional_mutual_information_generic_eq)
   6.794 -     (auto simp add: prod_measure_space_def sigma_prod_sets_finite finite_space
   6.795 -      finite_measure_space finite_product_prob_space_of_images sigma_def
   6.796 -      setsum_cartesian_product' setsum_product setsum_subtractf setsum_addf
   6.797 -      setsum_left_distrib[symmetric] setsum_real_distribution setsum_commute[where A="Y`space M"]
   6.798 -      real_of_pinfreal_mult[symmetric]
   6.799 -    cong: setsum_cong)
   6.800 +  (is "_ = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * ?YZdZ y z)))")
   6.801 +proof -
   6.802 +  let ?YZ = "\<lambda>y z. real (joint_distribution Y Z {(y, z)})"
   6.803 +  let ?X = "\<lambda>x. real (distribution X {x})"
   6.804 +  let ?Z = "\<lambda>z. real (distribution Z {z})"
   6.805 +
   6.806 +  txt {* This proof is actually quiet easy, however we need to show that the
   6.807 +    distributions are finite and the joint distributions are zero when one of
   6.808 +    the variables distribution is also zero. *}
   6.809 +
   6.810 +  note finite_var = MX MY MZ
   6.811 +  note random_var = finite_var[THEN finite_random_variableD]
   6.812 +
   6.813 +  note space_simps = space_pair_algebra space_sigma algebra.simps
   6.814 +
   6.815 +  note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
   6.816 +  note XZ = finite_random_variable_pairI[OF finite_var(1,3)]
   6.817 +  note ZX = finite_random_variable_pairI[OF finite_var(3,1)]
   6.818 +  note YZX = finite_random_variable_pairI[OF finite_var(2) ZX]
   6.819 +  note order1 =
   6.820 +    finite_distribution_order(5,6)[OF finite_var(1) YZ, simplified space_simps]
   6.821 +    finite_distribution_order(5,6)[OF finite_var(1,3), simplified space_simps]
   6.822 +
   6.823 +  note finite = finite_var(1) YZ finite_var(3) XZ YZX
   6.824 +  note finite[THEN finite_distribution_finite, simplified space_simps, simp]
   6.825 +
   6.826 +  have order2: "\<And>x y z. \<lbrakk>x \<in> space MX; y \<in> space MY; z \<in> space MZ; joint_distribution X Z {(x, z)} = 0\<rbrakk>
   6.827 +          \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
   6.828 +    unfolding joint_distribution_commute_singleton[of X]
   6.829 +    unfolding joint_distribution_assoc_singleton[symmetric]
   6.830 +    using finite_distribution_order(6)[OF finite_var(2) ZX]
   6.831 +    by (auto simp: space_simps)
   6.832  
   6.833 -lemma (in finite_information_space) conditional_mutual_information_eq_mutual_information:
   6.834 -  "\<I>(X ; Y) = \<I>(X ; Y | (\<lambda>x. ()))"
   6.835 +  have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * ?YZdZ y z))) =
   6.836 +    (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * (log b (?XYZ x y z / (?X x * ?YZ y z)) - log b (?XZ x z / (?X x * ?Z z))))"
   6.837 +    (is "(\<Sum>(x, y, z)\<in>?S. ?L x y z) = (\<Sum>(x, y, z)\<in>?S. ?R x y z)")
   6.838 +  proof (safe intro!: setsum_cong)
   6.839 +    fix x y z assume space: "x \<in> space MX" "y \<in> space MY" "z \<in> space MZ"
   6.840 +    then have *: "?XYZ x y z / (?XZ x z * ?YZdZ y z) =
   6.841 +      (?XYZ x y z / (?X x * ?YZ y z)) / (?XZ x z / (?X x * ?Z z))"
   6.842 +      using order1(3)
   6.843 +      by (auto simp: real_of_pinfreal_mult[symmetric] real_of_pinfreal_eq_0)
   6.844 +    show "?L x y z = ?R x y z"
   6.845 +    proof cases
   6.846 +      assume "?XYZ x y z \<noteq> 0"
   6.847 +      with space b_gt_1 order1 order2 show ?thesis unfolding *
   6.848 +        by (subst log_divide)
   6.849 +           (auto simp: zero_less_divide_iff zero_less_real_of_pinfreal
   6.850 +                       real_of_pinfreal_eq_0 zero_less_mult_iff)
   6.851 +    qed simp
   6.852 +  qed
   6.853 +  also have "\<dots> = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?X x * ?YZ y z))) -
   6.854 +                  (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XZ x z / (?X x * ?Z z)))"
   6.855 +    by (auto simp add: setsum_subtractf[symmetric] field_simps intro!: setsum_cong)
   6.856 +  also have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XZ x z / (?X x * ?Z z))) =
   6.857 +             (\<Sum>(x, z)\<in>space MX \<times> space MZ. ?XZ x z * log b (?XZ x z / (?X x * ?Z z)))"
   6.858 +    unfolding setsum_cartesian_product[symmetric] setsum_commute[of _ _ "space MY"]
   6.859 +              setsum_left_distrib[symmetric]
   6.860 +    unfolding joint_distribution_commute_singleton[of X]
   6.861 +    unfolding joint_distribution_assoc_singleton[symmetric]
   6.862 +    using setsum_real_joint_distribution_singleton[OF finite_var(2) ZX, unfolded space_simps]
   6.863 +    by (intro setsum_cong refl) simp
   6.864 +  also have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?X x * ?YZ y z))) -
   6.865 +             (\<Sum>(x, z)\<in>space MX \<times> space MZ. ?XZ x z * log b (?XZ x z / (?X x * ?Z z))) =
   6.866 +             conditional_mutual_information b MX MY MZ X Y Z"
   6.867 +    unfolding conditional_mutual_information_def
   6.868 +    unfolding mutual_information_generic_eq[OF finite_var(1,3)]
   6.869 +    unfolding mutual_information_generic_eq[OF finite_var(1) YZ]
   6.870 +    by (simp add: space_sigma space_pair_algebra setsum_cartesian_product')
   6.871 +  finally show ?thesis by simp
   6.872 +qed
   6.873 +
   6.874 +lemma (in information_space) conditional_mutual_information_eq:
   6.875 +  assumes "simple_function X" "simple_function Y" "simple_function Z"
   6.876 +  shows "\<I>(X;Y|Z) = (\<Sum>(x, y, z) \<in> X`space M \<times> Y`space M \<times> Z`space M.
   6.877 +             real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) *
   6.878 +             log b (real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) /
   6.879 +    (real (joint_distribution X Z {(x, z)}) * real (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
   6.880 +  using conditional_mutual_information_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]]
   6.881 +  by simp
   6.882 +
   6.883 +lemma (in information_space) conditional_mutual_information_eq_mutual_information:
   6.884 +  assumes X: "simple_function X" and Y: "simple_function Y"
   6.885 +  shows "\<I>(X ; Y) = \<I>(X ; Y | (\<lambda>x. ()))"
   6.886  proof -
   6.887    have [simp]: "(\<lambda>x. ()) ` space M = {()}" using not_empty by auto
   6.888 -
   6.889 +  have C: "simple_function (\<lambda>x. ())" by auto
   6.890    show ?thesis
   6.891 -    unfolding conditional_mutual_information_eq mutual_information_eq
   6.892 +    unfolding conditional_mutual_information_eq[OF X Y C]
   6.893 +    unfolding mutual_information_eq[OF X Y]
   6.894      by (simp add: setsum_cartesian_product' distribution_remove_const)
   6.895  qed
   6.896  
   6.897 -lemma (in finite_information_space) conditional_mutual_information_positive:
   6.898 -  "0 \<le> \<I>(X ; Y | Z)"
   6.899 -proof -
   6.900 +lemma (in prob_space) distribution_unit[simp]: "distribution (\<lambda>x. ()) {()} = 1"
   6.901 +  unfolding distribution_def using measure_space_1 by auto
   6.902 +
   6.903 +lemma (in prob_space) joint_distribution_unit[simp]: "distribution (\<lambda>x. (X x, ())) {(a, ())} = distribution X {a}"
   6.904 +  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>])
   6.905 +
   6.906 +lemma (in prob_space) setsum_distribution:
   6.907 +  assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. distribution X {a}) = 1"
   6.908 +  using setsum_joint_distribution[OF assms, of "\<lparr> space = UNIV, sets = Pow UNIV \<rparr>" "\<lambda>x. ()" "{()}"]
   6.909 +  using sigma_algebra_Pow[of "UNIV::unit set"] by simp
   6.910 +
   6.911 +lemma (in prob_space) setsum_real_distribution:
   6.912 +  assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. real (distribution X {a})) = 1"
   6.913 +  using setsum_real_joint_distribution[OF assms, of "\<lparr> space = UNIV, sets = Pow UNIV \<rparr>" "\<lambda>x. ()" "{()}"]
   6.914 +  using sigma_algebra_Pow[of "UNIV::unit set"] by simp
   6.915 +
   6.916 +lemma (in information_space) conditional_mutual_information_generic_positive:
   6.917 +  assumes "finite_random_variable MX X" and "finite_random_variable MY Y" and "finite_random_variable MZ Z"
   6.918 +  shows "0 \<le> conditional_mutual_information b MX MY MZ X Y Z"
   6.919 +proof (cases "space MX \<times> space MY \<times> space MZ = {}")
   6.920 +  case True show ?thesis
   6.921 +    unfolding conditional_mutual_information_generic_eq[OF assms] True
   6.922 +    by simp
   6.923 +next
   6.924 +  case False
   6.925    let "?dXYZ A" = "real (distribution (\<lambda>x. (X x, Y x, Z x)) A)"
   6.926    let "?dXZ A" = "real (joint_distribution X Z A)"
   6.927    let "?dYZ A" = "real (joint_distribution Y Z A)"
   6.928    let "?dX A" = "real (distribution X A)"
   6.929    let "?dZ A" = "real (distribution Z A)"
   6.930 -  let ?M = "X ` space M \<times> Y ` space M \<times> Z ` space M"
   6.931 +  let ?M = "space MX \<times> space MY \<times> space MZ"
   6.932  
   6.933    have split_beta: "\<And>f. split f = (\<lambda>x. f (fst x) (snd x))" by (simp add: fun_eq_iff)
   6.934  
   6.935 -  have "- (\<Sum>(x, y, z) \<in> ?M. ?dXYZ {(x, y, z)} *
   6.936 -    log b (?dXYZ {(x, y, z)} / (?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})))
   6.937 -    \<le> log b (\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})"
   6.938 +  note space_simps = space_pair_algebra space_sigma algebra.simps
   6.939 +
   6.940 +  note finite_var = assms
   6.941 +  note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
   6.942 +  note XZ = finite_random_variable_pairI[OF finite_var(1,3)]
   6.943 +  note ZX = finite_random_variable_pairI[OF finite_var(3,1)]
   6.944 +  note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
   6.945 +  note XYZ = finite_random_variable_pairI[OF finite_var(1) YZ]
   6.946 +  note finite = finite_var(3) YZ XZ XYZ
   6.947 +  note finite = finite[THEN finite_distribution_finite, simplified space_simps]
   6.948 +
   6.949 +  have order: "\<And>x y z. \<lbrakk>x \<in> space MX; y \<in> space MY; z \<in> space MZ; joint_distribution X Z {(x, z)} = 0\<rbrakk>
   6.950 +          \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
   6.951 +    unfolding joint_distribution_commute_singleton[of X]
   6.952 +    unfolding joint_distribution_assoc_singleton[symmetric]
   6.953 +    using finite_distribution_order(6)[OF finite_var(2) ZX]
   6.954 +    by (auto simp: space_simps)
   6.955 +
   6.956 +  note order = order
   6.957 +    finite_distribution_order(5,6)[OF finite_var(1) YZ, simplified space_simps]
   6.958 +    finite_distribution_order(5,6)[OF finite_var(2,3), simplified space_simps]
   6.959 +
   6.960 +  have "- conditional_mutual_information b MX MY MZ X Y Z = - (\<Sum>(x, y, z) \<in> ?M. ?dXYZ {(x, y, z)} *
   6.961 +    log b (?dXYZ {(x, y, z)} / (?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})))"
   6.962 +    unfolding conditional_mutual_information_generic_eq[OF assms] neg_equal_iff_equal
   6.963 +    by (intro setsum_cong) (auto intro!: arg_cong[where f="log b"] simp: real_of_pinfreal_mult[symmetric])
   6.964 +  also have "\<dots> \<le> log b (\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})"
   6.965      unfolding split_beta
   6.966    proof (rule log_setsum_divide)
   6.967 -    show "?M \<noteq> {}" using not_empty by simp
   6.968 +    show "?M \<noteq> {}" using False by simp
   6.969      show "1 < b" using b_gt_1 .
   6.970  
   6.971 +    show "finite ?M" using assms
   6.972 +      unfolding finite_sigma_algebra_def finite_sigma_algebra_axioms_def by auto
   6.973 +
   6.974 +    show "(\<Sum>x\<in>?M. ?dXYZ {(fst x, fst (snd x), snd (snd x))}) = 1"
   6.975 +      unfolding setsum_cartesian_product'
   6.976 +      unfolding setsum_commute[of _ "space MY"]
   6.977 +      unfolding setsum_commute[of _ "space MZ"]
   6.978 +      by (simp_all add: space_pair_algebra
   6.979 +        setsum_real_joint_distribution_singleton[OF `finite_random_variable MX X` YZ]
   6.980 +        setsum_real_joint_distribution_singleton[OF `finite_random_variable MY Y` finite_var(3)]
   6.981 +        setsum_real_distribution[OF `finite_random_variable MZ Z`])
   6.982 +
   6.983      fix x assume "x \<in> ?M"
   6.984      let ?x = "(fst x, fst (snd x), snd (snd x))"
   6.985  
   6.986 @@ -663,120 +748,180 @@
   6.987       by (simp add: real_pinfreal_nonneg mult_nonneg_nonneg divide_nonneg_nonneg)
   6.988  
   6.989      assume *: "0 < ?dXYZ {?x}"
   6.990 -    thus "0 < ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
   6.991 -      apply (rule_tac divide_pos_pos mult_pos_pos)+
   6.992 -      by (auto simp add: real_distribution_gt_0 intro: distribution_order(6) distribution_mono_gt_0)
   6.993 -  qed (simp_all add: setsum_cartesian_product' sum_over_space_real_distribution setsum_real_distribution finite_space)
   6.994 -  also have "(\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z}) = (\<Sum>z\<in>Z`space M. ?dZ {z})"
   6.995 +    with `x \<in> ?M` show "0 < ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
   6.996 +      using finite order
   6.997 +      by (cases x)
   6.998 +         (auto simp add: zero_less_real_of_pinfreal zero_less_mult_iff zero_less_divide_iff)
   6.999 +  qed
  6.1000 +  also have "(\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z}) = (\<Sum>z\<in>space MZ. ?dZ {z})"
  6.1001      apply (simp add: setsum_cartesian_product')
  6.1002      apply (subst setsum_commute)
  6.1003      apply (subst (2) setsum_commute)
  6.1004 -    by (auto simp: setsum_divide_distrib[symmetric] setsum_product[symmetric] setsum_real_distribution
  6.1005 +    by (auto simp: setsum_divide_distrib[symmetric] setsum_product[symmetric]
  6.1006 +                   setsum_real_joint_distribution_singleton[OF finite_var(1,3)]
  6.1007 +                   setsum_real_joint_distribution_singleton[OF finite_var(2,3)]
  6.1008            intro!: setsum_cong)
  6.1009 -  finally show ?thesis
  6.1010 -    unfolding conditional_mutual_information_eq sum_over_space_real_distribution
  6.1011 -    by (simp add: real_of_pinfreal_mult[symmetric])
  6.1012 +  also have "log b (\<Sum>z\<in>space MZ. ?dZ {z}) = 0"
  6.1013 +    unfolding setsum_real_distribution[OF finite_var(3)] by simp
  6.1014 +  finally show ?thesis by simp
  6.1015  qed
  6.1016  
  6.1017 +lemma (in information_space) conditional_mutual_information_positive:
  6.1018 +  assumes "simple_function X" and "simple_function Y" and "simple_function Z"
  6.1019 +  shows "0 \<le> \<I>(X;Y|Z)"
  6.1020 +  using conditional_mutual_information_generic_positive[OF assms[THEN simple_function_imp_finite_random_variable]]
  6.1021 +  by simp
  6.1022 +
  6.1023  subsection {* Conditional Entropy *}
  6.1024  
  6.1025  definition (in prob_space)
  6.1026    "conditional_entropy b S T X Y = conditional_mutual_information b S S T X X Y"
  6.1027  
  6.1028 -abbreviation (in finite_information_space)
  6.1029 -  finite_conditional_entropy ("\<H>'(_ | _')") where
  6.1030 +abbreviation (in information_space)
  6.1031 +  conditional_entropy_Pow ("\<H>'(_ | _')") where
  6.1032    "\<H>(X | Y) \<equiv> conditional_entropy b
  6.1033      \<lparr> space = X`space M, sets = Pow (X`space M) \<rparr>
  6.1034      \<lparr> space = Y`space M, sets = Pow (Y`space M) \<rparr> X Y"
  6.1035  
  6.1036 -lemma (in finite_information_space) conditional_entropy_positive:
  6.1037 -  "0 \<le> \<H>(X | Y)" unfolding conditional_entropy_def using conditional_mutual_information_positive .
  6.1038 +lemma (in information_space) conditional_entropy_positive:
  6.1039 +  "simple_function X \<Longrightarrow> simple_function Y \<Longrightarrow> 0 \<le> \<H>(X | Y)"
  6.1040 +  unfolding conditional_entropy_def by (auto intro!: conditional_mutual_information_positive)
  6.1041  
  6.1042 -lemma (in finite_information_space) conditional_entropy_generic_eq:
  6.1043 -  assumes MX: "finite_measure_space MX (distribution X)"
  6.1044 -  assumes MY: "finite_measure_space MZ (distribution Z)"
  6.1045 +lemma (in measure_space) empty_measureI: "A = {} \<Longrightarrow> \<mu> A = 0" by simp
  6.1046 +
  6.1047 +lemma (in information_space) conditional_entropy_generic_eq:
  6.1048 +  assumes MX: "finite_random_variable MX X"
  6.1049 +  assumes MZ: "finite_random_variable MZ Z"
  6.1050    shows "conditional_entropy b MX MZ X Z =
  6.1051       - (\<Sum>(x, z)\<in>space MX \<times> space MZ.
  6.1052           real (joint_distribution X Z {(x, z)}) *
  6.1053           log b (real (joint_distribution X Z {(x, z)}) / real (distribution Z {z})))"
  6.1054 -  unfolding conditional_entropy_def using assms
  6.1055 -  apply (simp add: conditional_mutual_information_generic_eq
  6.1056 -                   setsum_cartesian_product' setsum_commute[of _ "space MZ"]
  6.1057 -                   setsum_negf[symmetric] setsum_subtractf[symmetric])
  6.1058 -proof (safe intro!: setsum_cong, simp)
  6.1059 -  fix z x assume "z \<in> space MZ" "x \<in> space MX"
  6.1060 -  let "?XXZ x'" = "real (joint_distribution X (\<lambda>x. (X x, Z x)) {(x, x', z)})"
  6.1061 -  let "?XZ x'" = "real (joint_distribution X Z {(x', z)})"
  6.1062 -  let "?X" = "real (distribution X {x})"
  6.1063 -  interpret MX: finite_measure_space MX "distribution X" by fact
  6.1064 -  have *: "\<And>A. A = {} \<Longrightarrow> prob A = 0" by simp
  6.1065 -  have XXZ: "\<And>x'. ?XXZ x' = (if x' = x then ?XZ x else 0)"
  6.1066 -    by (auto simp: distribution_def intro!: arg_cong[where f=prob] *)
  6.1067 -  have "(\<Sum>x'\<in>space MX. ?XXZ x' * log b (?XXZ x') - (?XXZ x' * log b ?X + ?XXZ x' * log b (?XZ x'))) =
  6.1068 -    (\<Sum>x'\<in>{x}. ?XZ x' * log b (?XZ x') - (?XZ x' * log b ?X + ?XZ x' * log b (?XZ x')))"
  6.1069 -    using `x \<in> space MX` MX.finite_space
  6.1070 -    by (safe intro!: setsum_mono_zero_cong_right)
  6.1071 -       (auto split: split_if_asm simp: XXZ)
  6.1072 -  then show "(\<Sum>x'\<in>space MX. ?XXZ x' * log b (?XXZ x') - (?XXZ x' * log b ?X + ?XXZ x' * log b (?XZ x'))) +
  6.1073 -      ?XZ x * log b ?X = 0" by simp
  6.1074 +proof -
  6.1075 +  interpret MX: finite_sigma_algebra MX using MX by simp
  6.1076 +  interpret MZ: finite_sigma_algebra MZ using MZ by simp
  6.1077 +  let "?XXZ x y z" = "joint_distribution X (\<lambda>x. (X x, Z x)) {(x, y, z)}"
  6.1078 +  let "?XZ x z" = "joint_distribution X Z {(x, z)}"
  6.1079 +  let "?Z z" = "distribution Z {z}"
  6.1080 +  let "?f x y z" = "log b (real (?XXZ x y z) / (real (?XZ x z) * real (?XZ y z / ?Z z)))"
  6.1081 +  { fix x z have "?XXZ x x z = ?XZ x z"
  6.1082 +      unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>]) }
  6.1083 +  note this[simp]
  6.1084 +  { fix x x' :: 'b and z assume "x' \<noteq> x"
  6.1085 +    then have "?XXZ x x' z = 0"
  6.1086 +      by (auto simp: distribution_def intro!: arg_cong[where f=\<mu>] empty_measureI) }
  6.1087 +  note this[simp]
  6.1088 +  { fix x x' z assume *: "x \<in> space MX" "z \<in> space MZ"
  6.1089 +    then have "(\<Sum>x'\<in>space MX. real (?XXZ x x' z) * ?f x x' z)
  6.1090 +      = (\<Sum>x'\<in>space MX. if x = x' then real (?XZ x z) * ?f x x z else 0)"
  6.1091 +      by (auto intro!: setsum_cong)
  6.1092 +    also have "\<dots> = real (?XZ x z) * ?f x x z"
  6.1093 +      using `x \<in> space MX` by (simp add: setsum_cases[OF MX.finite_space])
  6.1094 +    also have "\<dots> = real (?XZ x z) * log b (real (?Z z) / real (?XZ x z))"
  6.1095 +      by (auto simp: real_of_pinfreal_mult[symmetric])
  6.1096 +    also have "\<dots> = - real (?XZ x z) * log b (real (?XZ x z) / real (?Z z))"
  6.1097 +      using assms[THEN finite_distribution_finite]
  6.1098 +      using finite_distribution_order(6)[OF MX MZ]
  6.1099 +      by (auto simp: log_simps field_simps zero_less_mult_iff zero_less_real_of_pinfreal real_of_pinfreal_eq_0)
  6.1100 +    finally have "(\<Sum>x'\<in>space MX. real (?XXZ x x' z) * ?f x x' z) =
  6.1101 +      - real (?XZ x z) * log b (real (?XZ x z) / real (?Z z))" . }
  6.1102 +  note * = this
  6.1103 +
  6.1104 +  show ?thesis
  6.1105 +    unfolding conditional_entropy_def
  6.1106 +    unfolding conditional_mutual_information_generic_eq[OF MX MX MZ]
  6.1107 +    by (auto simp: setsum_cartesian_product' setsum_negf[symmetric]
  6.1108 +                   setsum_commute[of _ "space MZ"] *   simp del: divide_pinfreal_def
  6.1109 +             intro!: setsum_cong)
  6.1110  qed
  6.1111  
  6.1112 -lemma (in finite_information_space) conditional_entropy_eq:
  6.1113 -  "\<H>(X | Z) =
  6.1114 +lemma (in information_space) conditional_entropy_eq:
  6.1115 +  assumes "simple_function X" "simple_function Z"
  6.1116 +  shows "\<H>(X | Z) =
  6.1117       - (\<Sum>(x, z)\<in>X ` space M \<times> Z ` space M.
  6.1118           real (joint_distribution X Z {(x, z)}) *
  6.1119           log b (real (joint_distribution X Z {(x, z)}) / real (distribution Z {z})))"
  6.1120 -  by (simp add: finite_measure_space conditional_entropy_generic_eq)
  6.1121 +  using conditional_entropy_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]]
  6.1122 +  by simp
  6.1123  
  6.1124 -lemma (in finite_information_space) conditional_entropy_eq_ce_with_hypothesis:
  6.1125 -  "\<H>(X | Y) =
  6.1126 +lemma (in information_space) conditional_entropy_eq_ce_with_hypothesis:
  6.1127 +  assumes X: "simple_function X" and Y: "simple_function Y"
  6.1128 +  shows "\<H>(X | Y) =
  6.1129      -(\<Sum>y\<in>Y`space M. real (distribution Y {y}) *
  6.1130        (\<Sum>x\<in>X`space M. real (joint_distribution X Y {(x,y)}) / real (distribution Y {(y)}) *
  6.1131                log b (real (joint_distribution X Y {(x,y)}) / real (distribution Y {(y)}))))"
  6.1132 -  unfolding conditional_entropy_eq neg_equal_iff_equal
  6.1133 -  apply (simp add: setsum_commute[of _ "Y`space M"] setsum_cartesian_product' setsum_divide_distrib[symmetric])
  6.1134 -  apply (safe intro!: setsum_cong)
  6.1135 -  using real_distribution_order'[of Y _ X _]
  6.1136 -  by (auto simp add: setsum_subtractf[of _ _ "X`space M"])
  6.1137 +  unfolding conditional_entropy_eq[OF assms]
  6.1138 +  using finite_distribution_finite[OF finite_random_variable_pairI[OF assms[THEN simple_function_imp_finite_random_variable]]]
  6.1139 +  using finite_distribution_order(5,6)[OF assms[THEN simple_function_imp_finite_random_variable]]
  6.1140 +  using finite_distribution_finite[OF Y[THEN simple_function_imp_finite_random_variable]]
  6.1141 +  by (auto simp: setsum_cartesian_product'  setsum_commute[of _ "Y`space M"] setsum_right_distrib real_of_pinfreal_eq_0
  6.1142 +           intro!: setsum_cong)
  6.1143  
  6.1144 -lemma (in finite_information_space) conditional_entropy_eq_cartesian_sum:
  6.1145 -  "\<H>(X | Y) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
  6.1146 +lemma (in information_space) conditional_entropy_eq_cartesian_product:
  6.1147 +  assumes "simple_function X" "simple_function Y"
  6.1148 +  shows "\<H>(X | Y) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
  6.1149      real (joint_distribution X Y {(x,y)}) *
  6.1150      log b (real (joint_distribution X Y {(x,y)}) / real (distribution Y {y})))"
  6.1151 -proof -
  6.1152 -  { fix x assume "x\<notin>(\<lambda>x. (X x, Y x))`space M"
  6.1153 -    then have "(\<lambda>x. (X x, Y x)) -` {x} \<inter> space M = {}" by auto
  6.1154 -    then have "joint_distribution X Y {x} = 0"
  6.1155 -      unfolding distribution_def by auto }
  6.1156 -  then show ?thesis using finite_space
  6.1157 -    unfolding conditional_entropy_eq neg_equal_iff_equal setsum_cartesian_product
  6.1158 -    by (auto intro!: setsum_mono_zero_cong_left)
  6.1159 -qed
  6.1160 +  unfolding conditional_entropy_eq[OF assms]
  6.1161 +  by (auto intro!: setsum_cong simp: setsum_cartesian_product')
  6.1162  
  6.1163  subsection {* Equalities *}
  6.1164  
  6.1165 -lemma (in finite_information_space) mutual_information_eq_entropy_conditional_entropy:
  6.1166 -  "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)"
  6.1167 -  unfolding mutual_information_eq entropy_eq conditional_entropy_eq
  6.1168 -  using finite_space
  6.1169 -  by (auto simp add: setsum_addf setsum_subtractf setsum_cartesian_product'
  6.1170 -      setsum_left_distrib[symmetric] setsum_addf setsum_real_distribution)
  6.1171 +lemma (in information_space) mutual_information_eq_entropy_conditional_entropy:
  6.1172 +  assumes X: "simple_function X" and Z: "simple_function Z"
  6.1173 +  shows  "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)"
  6.1174 +proof -
  6.1175 +  let "?XZ x z" = "real (joint_distribution X Z {(x, z)})"
  6.1176 +  let "?Z z" = "real (distribution Z {z})"
  6.1177 +  let "?X x" = "real (distribution X {x})"
  6.1178 +  note fX = X[THEN simple_function_imp_finite_random_variable]
  6.1179 +  note fZ = Z[THEN simple_function_imp_finite_random_variable]
  6.1180 +  note fX[THEN finite_distribution_finite, simp] and fZ[THEN finite_distribution_finite, simp]
  6.1181 +  note finite_distribution_order[OF fX fZ, simp]
  6.1182 +  { fix x z assume "x \<in> X`space M" "z \<in> Z`space M"
  6.1183 +    have "?XZ x z * log b (?XZ x z / (?X x * ?Z z)) =
  6.1184 +          ?XZ x z * log b (?XZ x z / ?Z z) - ?XZ x z * log b (?X x)"
  6.1185 +      by (auto simp: log_simps real_of_pinfreal_mult[symmetric] zero_less_mult_iff
  6.1186 +                     zero_less_real_of_pinfreal field_simps real_of_pinfreal_eq_0 abs_mult) }
  6.1187 +  note * = this
  6.1188 +  show ?thesis
  6.1189 +    unfolding entropy_eq[OF X] conditional_entropy_eq[OF X Z] mutual_information_eq[OF X Z]
  6.1190 +    using setsum_real_joint_distribution_singleton[OF fZ fX, unfolded joint_distribution_commute_singleton[of Z X]]
  6.1191 +    by (simp add: * setsum_cartesian_product' setsum_subtractf setsum_left_distrib[symmetric]
  6.1192 +                     setsum_real_distribution)
  6.1193 +qed
  6.1194  
  6.1195 -lemma (in finite_information_space) conditional_entropy_less_eq_entropy:
  6.1196 -  "\<H>(X | Z) \<le> \<H>(X)"
  6.1197 +lemma (in information_space) conditional_entropy_less_eq_entropy:
  6.1198 +  assumes X: "simple_function X" and Z: "simple_function Z"
  6.1199 +  shows "\<H>(X | Z) \<le> \<H>(X)"
  6.1200  proof -
  6.1201 -  have "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)" using mutual_information_eq_entropy_conditional_entropy .
  6.1202 -  with mutual_information_positive[of X Z] entropy_positive[of X]
  6.1203 +  have "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)" using mutual_information_eq_entropy_conditional_entropy[OF assms] .
  6.1204 +  with mutual_information_positive[OF X Z] entropy_positive[OF X]
  6.1205    show ?thesis by auto
  6.1206  qed
  6.1207  
  6.1208 -lemma (in finite_information_space) entropy_chain_rule:
  6.1209 -  "\<H>(\<lambda>x. (X x, Y x)) = \<H>(X) + \<H>(Y|X)"
  6.1210 -  unfolding entropy_eq[of X] entropy_eq_cartesian_sum conditional_entropy_eq_cartesian_sum
  6.1211 -  unfolding setsum_commute[of _ "X`space M"] setsum_negf[symmetric] setsum_addf[symmetric]
  6.1212 -  by (rule setsum_cong)
  6.1213 -     (simp_all add: setsum_negf setsum_addf setsum_subtractf setsum_real_distribution
  6.1214 -                    setsum_left_distrib[symmetric] joint_distribution_commute[of X Y])
  6.1215 +lemma (in information_space) entropy_chain_rule:
  6.1216 +  assumes X: "simple_function X" and Y: "simple_function Y"
  6.1217 +  shows  "\<H>(\<lambda>x. (X x, Y x)) = \<H>(X) + \<H>(Y|X)"
  6.1218 +proof -
  6.1219 +  let "?XY x y" = "real (joint_distribution X Y {(x, y)})"
  6.1220 +  let "?Y y" = "real (distribution Y {y})"
  6.1221 +  let "?X x" = "real (distribution X {x})"
  6.1222 +  note fX = X[THEN simple_function_imp_finite_random_variable]
  6.1223 +  note fY = Y[THEN simple_function_imp_finite_random_variable]
  6.1224 +  note fX[THEN finite_distribution_finite, simp] and fY[THEN finite_distribution_finite, simp]
  6.1225 +  note finite_distribution_order[OF fX fY, simp]
  6.1226 +  { fix x y assume "x \<in> X`space M" "y \<in> Y`space M"
  6.1227 +    have "?XY x y * log b (?XY x y / ?X x) =
  6.1228 +          ?XY x y * log b (?XY x y) - ?XY x y * log b (?X x)"
  6.1229 +      by (auto simp: log_simps real_of_pinfreal_mult[symmetric] zero_less_mult_iff
  6.1230 +                     zero_less_real_of_pinfreal field_simps real_of_pinfreal_eq_0 abs_mult) }
  6.1231 +  note * = this
  6.1232 +  show ?thesis
  6.1233 +    using setsum_real_joint_distribution_singleton[OF fY fX]
  6.1234 +    unfolding entropy_eq[OF X] conditional_entropy_eq_cartesian_product[OF Y X] entropy_eq_cartesian_product[OF X Y]
  6.1235 +    unfolding joint_distribution_commute_singleton[of Y X] setsum_commute[of _ "X`space M"]
  6.1236 +    by (simp add: * setsum_subtractf setsum_left_distrib[symmetric])
  6.1237 +qed
  6.1238  
  6.1239  section {* Partitioning *}
  6.1240  
  6.1241 @@ -893,15 +1038,26 @@
  6.1242    finally show ?thesis .
  6.1243  qed
  6.1244  
  6.1245 -lemma (in finite_information_space) entropy_partition:
  6.1246 +lemma (in information_space) entropy_partition:
  6.1247 +  assumes sf: "simple_function X" "simple_function P"
  6.1248    assumes svi: "subvimage (space M) X P"
  6.1249    shows "\<H>(X) = \<H>(P) + \<H>(X|P)"
  6.1250  proof -
  6.1251 +  let "?XP x p" = "real (joint_distribution X P {(x, p)})"
  6.1252 +  let "?X x" = "real (distribution X {x})"
  6.1253 +  let "?P p" = "real (distribution P {p})"
  6.1254 +  note fX = sf(1)[THEN simple_function_imp_finite_random_variable]
  6.1255 +  note fP = sf(2)[THEN simple_function_imp_finite_random_variable]
  6.1256 +  note fX[THEN finite_distribution_finite, simp] and fP[THEN finite_distribution_finite, simp]
  6.1257 +  note finite_distribution_order[OF fX fP, simp]
  6.1258    have "(\<Sum>x\<in>X ` space M. real (distribution X {x}) * log b (real (distribution X {x}))) =
  6.1259      (\<Sum>y\<in>P `space M. \<Sum>x\<in>X ` space M.
  6.1260      real (joint_distribution X P {(x, y)}) * log b (real (joint_distribution X P {(x, y)})))"
  6.1261    proof (subst setsum_image_split[OF svi],
  6.1262 -      safe intro!: finite_imageI finite_space setsum_mono_zero_cong_left imageI)
  6.1263 +      safe intro!: setsum_mono_zero_cong_left imageI)
  6.1264 +    show "finite (X ` space M)" "finite (X ` space M)" "finite (P ` space M)"
  6.1265 +      using sf unfolding simple_function_def by auto
  6.1266 +  next
  6.1267      fix p x assume in_space: "p \<in> space M" "x \<in> space M"
  6.1268      assume "real (joint_distribution X P {(X x, P p)}) * log b (real (joint_distribution X P {(X x, P p)})) \<noteq> 0"
  6.1269      hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M \<noteq> {}" by (auto simp: distribution_def)
  6.1270 @@ -920,26 +1076,41 @@
  6.1271            log b (real (joint_distribution X P {(X x, P p)}))"
  6.1272        by (auto simp: distribution_def)
  6.1273    qed
  6.1274 -  thus ?thesis
  6.1275 -  unfolding entropy_eq conditional_entropy_eq
  6.1276 +  moreover have "\<And>x y. real (joint_distribution X P {(x, y)}) *
  6.1277 +      log b (real (joint_distribution X P {(x, y)}) / real (distribution P {y})) =
  6.1278 +      real (joint_distribution X P {(x, y)}) * log b (real (joint_distribution X P {(x, y)})) -
  6.1279 +      real (joint_distribution X P {(x, y)}) * log b (real (distribution P {y}))"
  6.1280 +    by (auto simp add: log_simps zero_less_mult_iff field_simps)
  6.1281 +  ultimately show ?thesis
  6.1282 +    unfolding sf[THEN entropy_eq] conditional_entropy_eq[OF sf]
  6.1283 +    using setsum_real_joint_distribution_singleton[OF fX fP]
  6.1284      by (simp add: setsum_cartesian_product' setsum_subtractf setsum_real_distribution
  6.1285        setsum_left_distrib[symmetric] setsum_commute[where B="P`space M"])
  6.1286  qed
  6.1287  
  6.1288 -corollary (in finite_information_space) entropy_data_processing:
  6.1289 -  "\<H>(f \<circ> X) \<le> \<H>(X)"
  6.1290 -  by (subst (2) entropy_partition[of _ "f \<circ> X"]) (auto intro: conditional_entropy_positive)
  6.1291 +corollary (in information_space) entropy_data_processing:
  6.1292 +  assumes X: "simple_function X" shows "\<H>(f \<circ> X) \<le> \<H>(X)"
  6.1293 +proof -
  6.1294 +  note X
  6.1295 +  moreover have fX: "simple_function (f \<circ> X)" using X by auto
  6.1296 +  moreover have "subvimage (space M) X (f \<circ> X)" by auto
  6.1297 +  ultimately have "\<H>(X) = \<H>(f\<circ>X) + \<H>(X|f\<circ>X)" by (rule entropy_partition)
  6.1298 +  then show "\<H>(f \<circ> X) \<le> \<H>(X)"
  6.1299 +    by (auto intro: conditional_entropy_positive[OF X fX])
  6.1300 +qed
  6.1301  
  6.1302 -corollary (in finite_information_space) entropy_of_inj:
  6.1303 -  assumes "inj_on f (X`space M)"
  6.1304 +corollary (in information_space) entropy_of_inj:
  6.1305 +  assumes X: "simple_function X" and inj: "inj_on f (X`space M)"
  6.1306    shows "\<H>(f \<circ> X) = \<H>(X)"
  6.1307  proof (rule antisym)
  6.1308 -  show "\<H>(f \<circ> X) \<le> \<H>(X)" using entropy_data_processing .
  6.1309 +  show "\<H>(f \<circ> X) \<le> \<H>(X)" using entropy_data_processing[OF X] .
  6.1310  next
  6.1311 +  have sf: "simple_function (f \<circ> X)"
  6.1312 +    using X by auto
  6.1313    have "\<H>(X) = \<H>(the_inv_into (X`space M) f \<circ> (f \<circ> X))"
  6.1314 -    by (auto intro!: mutual_information_cong simp: entropy_def the_inv_into_f_f[OF assms])
  6.1315 +    by (auto intro!: mutual_information_cong simp: entropy_def the_inv_into_f_f[OF inj])
  6.1316    also have "... \<le> \<H>(f \<circ> X)"
  6.1317 -    using entropy_data_processing .
  6.1318 +    using entropy_data_processing[OF sf] .
  6.1319    finally show "\<H>(X) \<le> \<H>(f \<circ> X)" .
  6.1320  qed
  6.1321  
     7.1 --- a/src/HOL/Probability/Lebesgue_Integration.thy	Wed Dec 01 06:50:54 2010 -0800
     7.2 +++ b/src/HOL/Probability/Lebesgue_Integration.thy	Wed Dec 01 19:20:30 2010 +0100
     7.3 @@ -3,12 +3,22 @@
     7.4  header {*Lebesgue Integration*}
     7.5  
     7.6  theory Lebesgue_Integration
     7.7 -imports Measure Borel
     7.8 +imports Measure Borel_Space
     7.9  begin
    7.10  
    7.11 -section "@{text \<mu>}-null sets"
    7.12 -
    7.13 -abbreviation (in measure_space) "null_sets \<equiv> {N\<in>sets M. \<mu> N = 0}"
    7.14 +lemma image_set_cong:
    7.15 +  assumes A: "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. f x = g y"
    7.16 +  assumes B: "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. g y = f x"
    7.17 +  shows "f ` A = g ` B"
    7.18 +proof safe
    7.19 +  fix x assume "x \<in> A"
    7.20 +  with A obtain y where "f x = g y" "y \<in> B" by auto
    7.21 +  then show "f x \<in> g ` B" by auto
    7.22 +next
    7.23 +  fix y assume "y \<in> B"
    7.24 +  with B obtain x where "g y = f x" "x \<in> A" by auto
    7.25 +  then show "g y \<in> f ` A" by auto
    7.26 +qed
    7.27  
    7.28  lemma sums_If_finite:
    7.29    assumes finite: "finite {r. P r}"
    7.30 @@ -469,6 +479,22 @@
    7.31    unfolding sigma_algebra.simple_function_def[OF N_subalgebra(2)]
    7.32    by auto
    7.33  
    7.34 +lemma (in sigma_algebra) simple_function_vimage:
    7.35 +  fixes g :: "'a \<Rightarrow> pinfreal" and f :: "'d \<Rightarrow> 'a"
    7.36 +  assumes g: "simple_function g" and f: "f \<in> S \<rightarrow> space M"
    7.37 +  shows "sigma_algebra.simple_function (vimage_algebra S f) (\<lambda>x. g (f x))"
    7.38 +proof -
    7.39 +  have subset: "(\<lambda>x. g (f x)) ` S \<subseteq> g ` space M"
    7.40 +    using f by auto
    7.41 +  interpret V: sigma_algebra "vimage_algebra S f"
    7.42 +    using f by (rule sigma_algebra_vimage)
    7.43 +  show ?thesis using g
    7.44 +    unfolding simple_function_eq_borel_measurable
    7.45 +    unfolding V.simple_function_eq_borel_measurable
    7.46 +    using measurable_vimage[OF _ f, of g borel]
    7.47 +    using finite_subset[OF subset] by auto
    7.48 +qed
    7.49 +
    7.50  section "Simple integral"
    7.51  
    7.52  definition (in measure_space)
    7.53 @@ -484,6 +510,19 @@
    7.54    thus ?thesis unfolding simple_integral_def by simp
    7.55  qed
    7.56  
    7.57 +lemma (in measure_space) simple_integral_cong_measure:
    7.58 +  assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<mu> A" and "simple_function f"
    7.59 +  shows "measure_space.simple_integral M \<nu> f = simple_integral f"
    7.60 +proof -
    7.61 +  interpret v: measure_space M \<nu>
    7.62 +    by (rule measure_space_cong) fact
    7.63 +  have "\<And>x. x \<in> space M \<Longrightarrow> f -` {f x} \<inter> space M \<in> sets M"
    7.64 +    using `simple_function f`[THEN simple_functionD(2)] by auto
    7.65 +  with assms show ?thesis
    7.66 +    unfolding simple_integral_def v.simple_integral_def
    7.67 +    by (auto intro!: setsum_cong)
    7.68 +qed
    7.69 +
    7.70  lemma (in measure_space) simple_integral_const[simp]:
    7.71    "simple_integral (\<lambda>x. c) = c * \<mu> (space M)"
    7.72  proof (cases "space M = {}")
    7.73 @@ -590,22 +629,62 @@
    7.74      by (auto simp: setsum_right_distrib field_simps intro!: setsum_cong)
    7.75  qed
    7.76  
    7.77 +lemma (in measure_space) simple_integral_mono_AE:
    7.78 +  assumes "simple_function f" and "simple_function g"
    7.79 +  and mono: "AE x. f x \<le> g x"
    7.80 +  shows "simple_integral f \<le> simple_integral g"
    7.81 +proof -
    7.82 +  let "?S x" = "(g -` {g x} \<inter> space M) \<inter> (f -` {f x} \<inter> space M)"
    7.83 +  have *: "\<And>x. g -` {g x} \<inter> f -` {f x} \<inter> space M = ?S x"
    7.84 +    "\<And>x. f -` {f x} \<inter> g -` {g x} \<inter> space M = ?S x" by auto
    7.85 +  show ?thesis
    7.86 +    unfolding *
    7.87 +      simple_function_partition[OF `simple_function f` `simple_function g`]
    7.88 +      simple_function_partition[OF `simple_function g` `simple_function f`]
    7.89 +  proof (safe intro!: setsum_mono)
    7.90 +    fix x assume "x \<in> space M"
    7.91 +    then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
    7.92 +    show "the_elem (f`?S x) * \<mu> (?S x) \<le> the_elem (g`?S x) * \<mu> (?S x)"
    7.93 +    proof (cases "f x \<le> g x")
    7.94 +      case True then show ?thesis using * by (auto intro!: mult_right_mono)
    7.95 +    next
    7.96 +      case False
    7.97 +      obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
    7.98 +        using mono by (auto elim!: AE_E)
    7.99 +      have "?S x \<subseteq> N" using N `x \<in> space M` False by auto
   7.100 +      moreover have "?S x \<in> sets M" using assms `x \<in> space M`
   7.101 +        by (rule_tac Int) (auto intro!: simple_functionD(2))
   7.102 +      ultimately have "\<mu> (?S x) \<le> \<mu> N"
   7.103 +        using `N \<in> sets M` by (auto intro!: measure_mono)
   7.104 +      then show ?thesis using `\<mu> N = 0` by auto
   7.105 +    qed
   7.106 +  qed
   7.107 +qed
   7.108 +
   7.109  lemma (in measure_space) simple_integral_mono:
   7.110    assumes "simple_function f" and "simple_function g"
   7.111    and mono: "\<And> x. x \<in> space M \<Longrightarrow> f x \<le> g x"
   7.112    shows "simple_integral f \<le> simple_integral g"
   7.113 -  unfolding
   7.114 -    simple_function_partition[OF `simple_function f` `simple_function g`]
   7.115 -    simple_function_partition[OF `simple_function g` `simple_function f`]
   7.116 -  apply (subst Int_commute)
   7.117 -proof (safe intro!: setsum_mono)
   7.118 -  fix x let ?S = "g -` {g x} \<inter> f -` {f x} \<inter> space M"
   7.119 -  assume "x \<in> space M"
   7.120 -  hence "f ` ?S = {f x}" "g ` ?S = {g x}" by auto
   7.121 -  thus "the_elem (f`?S) * \<mu> ?S \<le> the_elem (g`?S) * \<mu> ?S"
   7.122 -    using mono[OF `x \<in> space M`] by (auto intro!: mult_right_mono)
   7.123 +proof (rule simple_integral_mono_AE[OF assms(1, 2)])
   7.124 +  show "AE x. f x \<le> g x"
   7.125 +    using mono by (rule AE_cong) auto
   7.126  qed
   7.127  
   7.128 +lemma (in measure_space) simple_integral_cong_AE:
   7.129 +  assumes "simple_function f" "simple_function g" and "AE x. f x = g x"
   7.130 +  shows "simple_integral f = simple_integral g"
   7.131 +  using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
   7.132 +
   7.133 +lemma (in measure_space) simple_integral_cong':
   7.134 +  assumes sf: "simple_function f" "simple_function g"
   7.135 +  and mea: "\<mu> {x\<in>space M. f x \<noteq> g x} = 0"
   7.136 +  shows "simple_integral f = simple_integral g"
   7.137 +proof (intro simple_integral_cong_AE sf AE_I)
   7.138 +  show "\<mu> {x\<in>space M. f x \<noteq> g x} = 0" by fact
   7.139 +  show "{x \<in> space M. f x \<noteq> g x} \<in> sets M"
   7.140 +    using sf[THEN borel_measurable_simple_function] by auto
   7.141 +qed simp
   7.142 +
   7.143  lemma (in measure_space) simple_integral_indicator:
   7.144    assumes "A \<in> sets M"
   7.145    assumes "simple_function f"
   7.146 @@ -637,7 +716,8 @@
   7.147        using assms(2) unfolding simple_function_def by auto
   7.148      show "f ` A \<union> {0} \<subseteq> f`space M \<union> {0}"
   7.149        using sets_into_space[OF assms(1)] by auto
   7.150 -    have "\<And>x. f x \<notin> f ` A \<Longrightarrow> f -` {f x} \<inter> space M \<inter> A = {}" by (auto simp: image_iff)
   7.151 +    have "\<And>x. f x \<notin> f ` A \<Longrightarrow> f -` {f x} \<inter> space M \<inter> A = {}"
   7.152 +      by (auto simp: image_iff)
   7.153      thus "\<forall>i\<in>f ` space M \<union> {0} - (f ` A \<union> {0}).
   7.154        i * \<mu> (f -` {i} \<inter> space M \<inter> A) = 0" by auto
   7.155    next
   7.156 @@ -670,45 +750,22 @@
   7.157    assumes "simple_function u" "N \<in> null_sets"
   7.158    shows "simple_integral (\<lambda>x. u x * indicator N x) = 0"
   7.159  proof -
   7.160 -  have "simple_integral (\<lambda>x. u x * indicator N x) \<le>
   7.161 -    simple_integral (\<lambda>x. \<omega> * indicator N x)"
   7.162 -    using assms
   7.163 -    by (safe intro!: simple_integral_mono simple_function_mult simple_function_indicator simple_function_const) simp
   7.164 -  also have "... = 0" apply(subst simple_integral_mult)
   7.165 -    using assms(2) by auto
   7.166 -  finally show ?thesis by auto
   7.167 +  have "AE x. indicator N x = (0 :: pinfreal)"
   7.168 +    using `N \<in> null_sets` by (auto simp: indicator_def intro!: AE_I[of _ N])
   7.169 +  then have "simple_integral (\<lambda>x. u x * indicator N x) = simple_integral (\<lambda>x. 0)"
   7.170 +    using assms by (intro simple_integral_cong_AE) (auto intro!: AE_disjI2)
   7.171 +  then show ?thesis by simp
   7.172  qed
   7.173  
   7.174 -lemma (in measure_space) simple_integral_cong':
   7.175 -  assumes f: "simple_function f" and g: "simple_function g"
   7.176 -  and mea: "\<mu> {x\<in>space M. f x \<noteq> g x} = 0"
   7.177 -  shows "simple_integral f = simple_integral g"
   7.178 -proof -
   7.179 -  let ?h = "\<lambda>h. \<lambda>x. (h x * indicator {x\<in>space M. f x = g x} x
   7.180 -    + h x * indicator {x\<in>space M. f x \<noteq> g x} x
   7.181 -    + h x * indicator (-space M) x::pinfreal)"
   7.182 -  have *:"\<And>h. h = ?h h" unfolding indicator_def apply rule by auto
   7.183 -  have mea_neq:"{x \<in> space M. f x \<noteq> g x} \<in> sets M" using f g by (auto simp: borel_measurable_simple_function)
   7.184 -  then have mea_nullset: "{x \<in> space M. f x \<noteq> g x} \<in> null_sets" using mea by auto
   7.185 -  have h1:"\<And>h::_=>pinfreal. simple_function h \<Longrightarrow>
   7.186 -    simple_function (\<lambda>x. h x * indicator {x\<in>space M. f x = g x} x)"
   7.187 -    apply(safe intro!: simple_function_add simple_function_mult simple_function_indicator)
   7.188 -    using f g by (auto simp: borel_measurable_simple_function)
   7.189 -  have h2:"\<And>h::_\<Rightarrow>pinfreal. simple_function h \<Longrightarrow>
   7.190 -    simple_function (\<lambda>x. h x * indicator {x\<in>space M. f x \<noteq> g x} x)"
   7.191 -    apply(safe intro!: simple_function_add simple_function_mult simple_function_indicator)
   7.192 -    by(rule mea_neq)
   7.193 -  have **:"\<And>a b c d e f. a = b \<Longrightarrow> c = d \<Longrightarrow> e = f \<Longrightarrow> a+c+e = b+d+f" by auto
   7.194 -  note *** = simple_integral_add[OF simple_function_add[OF h1 h2] simple_function_notspace]
   7.195 -    simple_integral_add[OF h1 h2]
   7.196 -  show ?thesis apply(subst *[of g]) apply(subst *[of f])
   7.197 -    unfolding ***[OF f f] ***[OF g g]
   7.198 -  proof(rule **) case goal1 show ?case apply(rule arg_cong[where f=simple_integral]) apply rule 
   7.199 -      unfolding indicator_def by auto
   7.200 -  next note * = simple_integral_null_set[OF _ mea_nullset]
   7.201 -    case goal2 show ?case unfolding *[OF f] *[OF g] ..
   7.202 -  next case goal3 show ?case apply(rule simple_integral_cong) by auto
   7.203 -  qed
   7.204 +lemma (in measure_space) simple_integral_cong_AE_mult_indicator:
   7.205 +  assumes sf: "simple_function f" and eq: "AE x. x \<in> S" and "S \<in> sets M"
   7.206 +  shows "simple_integral f = simple_integral (\<lambda>x. f x * indicator S x)"
   7.207 +proof (rule simple_integral_cong_AE)
   7.208 +  show "simple_function f" by fact
   7.209 +  show "simple_function (\<lambda>x. f x * indicator S x)"
   7.210 +    using sf `S \<in> sets M` by auto
   7.211 +  from eq show "AE x. f x = f x * indicator S x"
   7.212 +    by (rule AE_mp) simp
   7.213  qed
   7.214  
   7.215  lemma (in measure_space) simple_integral_restricted:
   7.216 @@ -746,12 +803,49 @@
   7.217    unfolding simple_integral_def_raw
   7.218    unfolding measure_space.simple_integral_def_raw[OF assms] by simp
   7.219  
   7.220 +lemma (in measure_space) simple_integral_vimage:
   7.221 +  fixes g :: "'a \<Rightarrow> pinfreal" and f :: "'d \<Rightarrow> 'a"
   7.222 +  assumes f: "bij_betw f S (space M)"
   7.223 +  shows "simple_integral g =
   7.224 +         measure_space.simple_integral (vimage_algebra S f) (\<lambda>A. \<mu> (f ` A)) (\<lambda>x. g (f x))"
   7.225 +    (is "_ = measure_space.simple_integral ?T ?\<mu> _")
   7.226 +proof -
   7.227 +  from f interpret T: measure_space ?T ?\<mu> by (rule measure_space_isomorphic)
   7.228 +  have surj: "f`S = space M"
   7.229 +    using f unfolding bij_betw_def by simp
   7.230 +  have *: "(\<lambda>x. g (f x)) ` S = g ` f ` S" by auto
   7.231 +  have **: "f`S = space M" using f unfolding bij_betw_def by auto
   7.232 +  { fix x assume "x \<in> space M"
   7.233 +    have "(f ` ((\<lambda>x. g (f x)) -` {g x} \<inter> S)) =
   7.234 +      (f ` (f -` (g -` {g x}) \<inter> S))" by auto
   7.235 +    also have "f -` (g -` {g x}) \<inter> S = f -` (g -` {g x} \<inter> space M) \<inter> S"
   7.236 +      using f unfolding bij_betw_def by auto
   7.237 +    also have "(f ` (f -` (g -` {g x} \<inter> space M) \<inter> S)) = g -` {g x} \<inter> space M"
   7.238 +      using ** by (intro image_vimage_inter_eq) auto
   7.239 +    finally have "(f ` ((\<lambda>x. g (f x)) -` {g x} \<inter> S)) = g -` {g x} \<inter> space M" by auto }
   7.240 +  then show ?thesis using assms
   7.241 +    unfolding simple_integral_def T.simple_integral_def bij_betw_def
   7.242 +    by (auto simp add: * intro!: setsum_cong)
   7.243 +qed
   7.244 +
   7.245  section "Continuous posititve integration"
   7.246  
   7.247  definition (in measure_space)
   7.248    "positive_integral f =
   7.249      (SUP g : {g. simple_function g \<and> g \<le> f \<and> \<omega> \<notin> g`space M}. simple_integral g)"
   7.250  
   7.251 +lemma (in measure_space) positive_integral_cong_measure:
   7.252 +  assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<mu> A"
   7.253 +  shows "measure_space.positive_integral M \<nu> f = positive_integral f"
   7.254 +proof -
   7.255 +  interpret v: measure_space M \<nu>
   7.256 +    by (rule measure_space_cong) fact
   7.257 +  with assms show ?thesis
   7.258 +    unfolding positive_integral_def v.positive_integral_def SUPR_def
   7.259 +    by (auto intro!: arg_cong[where f=Sup] image_cong
   7.260 +             simp: simple_integral_cong_measure[of \<nu>])
   7.261 +qed
   7.262 +
   7.263  lemma (in measure_space) positive_integral_alt1:
   7.264    "positive_integral f =
   7.265      (SUP g : {g. simple_function g \<and> (\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>)}. simple_integral g)"
   7.266 @@ -863,15 +957,122 @@
   7.267    with assms show "simple_integral f \<le> y" by auto
   7.268  qed
   7.269  
   7.270 -lemma (in measure_space) positive_integral_mono:
   7.271 -  assumes mono: "\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x"
   7.272 +lemma (in measure_space) positive_integral_mono_AE:
   7.273 +  assumes ae: "AE x. u x \<le> v x"
   7.274    shows "positive_integral u \<le> positive_integral v"
   7.275    unfolding positive_integral_alt1
   7.276  proof (safe intro!: SUPR_mono)
   7.277 -  fix a assume a: "simple_function a" and "\<forall>x\<in>space M. a x \<le> u x \<and> a x \<noteq> \<omega>"
   7.278 -  with mono have "\<forall>x\<in>space M. a x \<le> v x \<and> a x \<noteq> \<omega>" by fastsimp
   7.279 -  with a show "\<exists>b\<in>{g. simple_function g \<and> (\<forall>x\<in>space M. g x \<le> v x \<and> g x \<noteq> \<omega>)}. simple_integral a \<le> simple_integral b"
   7.280 -    by (auto intro!: bexI[of _ a])
   7.281 +  fix a assume a: "simple_function a" and mono: "\<forall>x\<in>space M. a x \<le> u x \<and> a x \<noteq> \<omega>"
   7.282 +  from ae obtain N where N: "{x\<in>space M. \<not> u x \<le> v x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
   7.283 +    by (auto elim!: AE_E)
   7.284 +  have "simple_function (\<lambda>x. a x * indicator (space M - N) x)"
   7.285 +    using `N \<in> sets M` a by auto
   7.286 +  with a show "\<exists>b\<in>{g. simple_function g \<and> (\<forall>x\<in>space M. g x \<le> v x \<and> g x \<noteq> \<omega>)}.
   7.287 +    simple_integral a \<le> simple_integral b"
   7.288 +  proof (safe intro!: bexI[of _ "\<lambda>x. a x * indicator (space M - N) x"]
   7.289 +                      simple_integral_mono_AE)
   7.290 +    show "AE x. a x \<le> a x * indicator (space M - N) x"
   7.291 +    proof (rule AE_I, rule subset_refl)
   7.292 +      have *: "{x \<in> space M. \<not> a x \<le> a x * indicator (space M - N) x} =
   7.293 +        N \<inter> {x \<in> space M. a x \<noteq> 0}" (is "?N = _")
   7.294 +        using `N \<in> sets M`[THEN sets_into_space] by (auto simp: indicator_def)
   7.295 +      then show "?N \<in> sets M" 
   7.296 +        using `N \<in> sets M` `simple_function a`[THEN borel_measurable_simple_function]
   7.297 +        by (auto intro!: measure_mono Int)
   7.298 +      then have "\<mu> ?N \<le> \<mu> N"
   7.299 +        unfolding * using `N \<in> sets M` by (auto intro!: measure_mono)
   7.300 +      then show "\<mu> ?N = 0" using `\<mu> N = 0` by auto
   7.301 +    qed
   7.302 +  next
   7.303 +    fix x assume "x \<in> space M"
   7.304 +    show "a x * indicator (space M - N) x \<le> v x"
   7.305 +    proof (cases "x \<in> N")
   7.306 +      case True then show ?thesis by simp
   7.307 +    next
   7.308 +      case False
   7.309 +      with N mono have "a x \<le> u x" "u x \<le> v x" using `x \<in> space M` by auto
   7.310 +      with False `x \<in> space M` show "a x * indicator (space M - N) x \<le> v x" by auto
   7.311 +    qed
   7.312 +    assume "a x * indicator (space M - N) x = \<omega>"
   7.313 +    with mono `x \<in> space M` show False
   7.314 +      by (simp split: split_if_asm add: indicator_def)
   7.315 +  qed
   7.316 +qed
   7.317 +
   7.318 +lemma (in measure_space) positive_integral_cong_AE:
   7.319 +  "AE x. u x = v x \<Longrightarrow> positive_integral u = positive_integral v"
   7.320 +  by (auto simp: eq_iff intro!: positive_integral_mono_AE)
   7.321 +
   7.322 +lemma (in measure_space) positive_integral_mono:
   7.323 +  assumes mono: "\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x"
   7.324 +  shows "positive_integral u \<le> positive_integral v"
   7.325 +  using mono by (auto intro!: AE_cong positive_integral_mono_AE)
   7.326 +
   7.327 +lemma (in measure_space) positive_integral_vimage:
   7.328 +  fixes g :: "'a \<Rightarrow> pinfreal" and f :: "'d \<Rightarrow> 'a"
   7.329 +  assumes f: "bij_betw f S (space M)"
   7.330 +  shows "positive_integral g =
   7.331 +         measure_space.positive_integral (vimage_algebra S f) (\<lambda>A. \<mu> (f ` A)) (\<lambda>x. g (f x))"
   7.332 +    (is "_ = measure_space.positive_integral ?T ?\<mu> _")
   7.333 +proof -
   7.334 +  from f interpret T: measure_space ?T ?\<mu> by (rule measure_space_isomorphic)
   7.335 +  have f_fun: "f \<in> S \<rightarrow> space M" using assms unfolding bij_betw_def by auto
   7.336 +  from assms have inv: "bij_betw (the_inv_into S f) (space M) S"
   7.337 +    by (rule bij_betw_the_inv_into)
   7.338 +  then have inv_fun: "the_inv_into S f \<in> space M \<rightarrow> S" unfolding bij_betw_def by auto
   7.339 +
   7.340 +  have surj: "f`S = space M"
   7.341 +    using f unfolding bij_betw_def by simp
   7.342 +  have inj: "inj_on f S"
   7.343 +    using f unfolding bij_betw_def by simp
   7.344 +  have inv_f: "\<And>x. x \<in> space M \<Longrightarrow> f (the_inv_into S f x) = x"
   7.345 +    using f_the_inv_into_f[of f S] f unfolding bij_betw_def by auto
   7.346 +
   7.347 +  from simple_integral_vimage[OF assms, symmetric]
   7.348 +  have *: "simple_integral = T.simple_integral \<circ> (\<lambda>g. g \<circ> f)" by (simp add: comp_def)
   7.349 +  show ?thesis
   7.350 +    unfolding positive_integral_alt1 T.positive_integral_alt1 SUPR_def * image_compose
   7.351 +  proof (safe intro!: arg_cong[where f=Sup] image_set_cong, simp_all add: comp_def)
   7.352 +    fix g' :: "'a \<Rightarrow> pinfreal" assume "simple_function g'" "\<forall>x\<in>space M. g' x \<le> g x \<and> g' x \<noteq> \<omega>"
   7.353 +    then show "\<exists>h. T.simple_function h \<and> (\<forall>x\<in>S. h x \<le> g (f x) \<and> h x \<noteq> \<omega>) \<and>
   7.354 +                   T.simple_integral (\<lambda>x. g' (f x)) = T.simple_integral h"
   7.355 +      using f unfolding bij_betw_def
   7.356 +      by (auto intro!: exI[of _ "\<lambda>x. g' (f x)"]
   7.357 +               simp add: le_fun_def simple_function_vimage[OF _ f_fun])
   7.358 +  next
   7.359 +    fix g' :: "'d \<Rightarrow> pinfreal" assume g': "T.simple_function g'" "\<forall>x\<in>S. g' x \<le> g (f x) \<and> g' x \<noteq> \<omega>"
   7.360 +    let ?g = "\<lambda>x. g' (the_inv_into S f x)"
   7.361 +    show "\<exists>h. simple_function h \<and> (\<forall>x\<in>space M. h x \<le> g x \<and> h x \<noteq> \<omega>) \<and>
   7.362 +              T.simple_integral g' = T.simple_integral (\<lambda>x. h (f x))"
   7.363 +    proof (intro exI[of _ ?g] conjI ballI)
   7.364 +      { fix x assume x: "x \<in> space M"
   7.365 +        then have "the_inv_into S f x \<in> S" using inv_fun by auto
   7.366 +        with g' have "g' (the_inv_into S f x) \<le> g (f (the_inv_into S f x)) \<and> g' (the_inv_into S f x) \<noteq> \<omega>"
   7.367 +          by auto
   7.368 +        then show "g' (the_inv_into S f x) \<le> g x" "g' (the_inv_into S f x) \<noteq> \<omega>"
   7.369 +          using f_the_inv_into_f[of f S x] x f unfolding bij_betw_def by auto }
   7.370 +      note vimage_vimage_inv[OF f inv_f inv_fun, simp]
   7.371 +      from T.simple_function_vimage[OF g'(1), unfolded space_vimage_algebra, OF inv_fun]
   7.372 +      show "simple_function (\<lambda>x. g' (the_inv_into S f x))"
   7.373 +        unfolding simple_function_def by (simp add: simple_function_def)
   7.374 +      show "T.simple_integral g' = T.simple_integral (\<lambda>x. ?g (f x))"
   7.375 +        using the_inv_into_f_f[OF inj] by (auto intro!: T.simple_integral_cong)
   7.376 +    qed
   7.377 +  qed
   7.378 +qed
   7.379 +
   7.380 +lemma (in measure_space) positive_integral_vimage_inv:
   7.381 +  fixes g :: "'d \<Rightarrow> pinfreal" and f :: "'d \<Rightarrow> 'a"
   7.382 +  assumes f: "bij_betw f S (space M)"
   7.383 +  shows "measure_space.positive_integral (vimage_algebra S f) (\<lambda>A. \<mu> (f ` A)) g =
   7.384 +      positive_integral (\<lambda>x. g (the_inv_into S f x))"
   7.385 +proof -
   7.386 +  interpret v: measure_space "vimage_algebra S f" "\<lambda>A. \<mu> (f ` A)"
   7.387 +    using f by (rule measure_space_isomorphic)
   7.388 +  show ?thesis
   7.389 +    unfolding positive_integral_vimage[OF f, of "\<lambda>x. g (the_inv_into S f x)"]
   7.390 +    using f[unfolded bij_betw_def]
   7.391 +    by (auto intro!: v.positive_integral_cong simp: the_inv_into_f_f)
   7.392  qed
   7.393  
   7.394  lemma (in measure_space) positive_integral_SUP_approx:
   7.395 @@ -901,26 +1102,25 @@
   7.396    have u: "\<And>x. x \<in> space M \<Longrightarrow> u -` {u x} \<inter> space M \<in> sets M"
   7.397      using `simple_function u` by (auto simp add: simple_function_def)
   7.398  
   7.399 -  { fix i
   7.400 -    have "(\<lambda>n. (u -` {i} \<inter> space M) \<inter> ?B n) \<up> (u -` {i} \<inter> space M)" using B_mono unfolding isoton_def
   7.401 -    proof safe
   7.402 -      fix x assume "x \<in> space M"
   7.403 -      show "x \<in> (\<Union>i. (u -` {u x} \<inter> space M) \<inter> ?B i)"
   7.404 -      proof cases
   7.405 -        assume "u x = 0" thus ?thesis using `x \<in> space M` by simp
   7.406 -      next
   7.407 -        assume "u x \<noteq> 0"
   7.408 -        with `a < 1` real `x \<in> space M`
   7.409 -        have "a * u x < 1 * u x" by (rule_tac pinfreal_mult_strict_right_mono) (auto simp: image_iff)
   7.410 -        also have "\<dots> \<le> (SUP i. f i x)" using le `f \<up> s`
   7.411 -          unfolding isoton_fun_expand by (auto simp: isoton_def le_fun_def)
   7.412 -        finally obtain i where "a * u x < f i x" unfolding SUPR_def
   7.413 -          by (auto simp add: less_Sup_iff)
   7.414 -        hence "a * u x \<le> f i x" by auto
   7.415 -        thus ?thesis using `x \<in> space M` by auto
   7.416 -      qed
   7.417 -    qed auto }
   7.418 -  note measure_conv = measure_up[OF u Int[OF u B] this]
   7.419 +  have "\<And>i. (\<lambda>n. (u -` {i} \<inter> space M) \<inter> ?B n) \<up> (u -` {i} \<inter> space M)" using B_mono unfolding isoton_def
   7.420 +  proof safe
   7.421 +    fix x i assume "x \<in> space M"
   7.422 +    show "x \<in> (\<Union>i. (u -` {u x} \<inter> space M) \<inter> ?B i)"
   7.423 +    proof cases
   7.424 +      assume "u x = 0" thus ?thesis using `x \<in> space M` by simp
   7.425 +    next
   7.426 +      assume "u x \<noteq> 0"
   7.427 +      with `a < 1` real `x \<in> space M`
   7.428 +      have "a * u x < 1 * u x" by (rule_tac pinfreal_mult_strict_right_mono) (auto simp: image_iff)
   7.429 +      also have "\<dots> \<le> (SUP i. f i x)" using le `f \<up> s`
   7.430 +        unfolding isoton_fun_expand by (auto simp: isoton_def le_fun_def)
   7.431 +      finally obtain i where "a * u x < f i x" unfolding SUPR_def
   7.432 +        by (auto simp add: less_Sup_iff)
   7.433 +      hence "a * u x \<le> f i x" by auto
   7.434 +      thus ?thesis using `x \<in> space M` by auto
   7.435 +    qed
   7.436 +  qed auto
   7.437 +  note measure_conv = measure_up[OF Int[OF u B] this]
   7.438  
   7.439    have "simple_integral u = (SUP i. simple_integral (?uB i))"
   7.440      unfolding simple_integral_indicator[OF B `simple_function u`]
   7.441 @@ -986,6 +1186,34 @@
   7.442    qed
   7.443  qed
   7.444  
   7.445 +lemma (in measure_space) positive_integral_monotone_convergence_SUP:
   7.446 +  assumes "\<And>i x. x \<in> space M \<Longrightarrow> f i x \<le> f (Suc i) x"
   7.447 +  assumes "\<And>i. f i \<in> borel_measurable M"
   7.448 +  shows "(SUP i. positive_integral (f i)) = positive_integral (\<lambda>x. SUP i. f i x)"
   7.449 +    (is "_ = positive_integral ?u")
   7.450 +proof -
   7.451 +  have "?u \<in> borel_measurable M"
   7.452 +    using borel_measurable_SUP[of _ f] assms by (simp add: SUPR_fun_expand)
   7.453 +
   7.454 +  show ?thesis
   7.455 +  proof (rule antisym)
   7.456 +    show "(SUP j. positive_integral (f j)) \<le> positive_integral ?u"
   7.457 +      by (auto intro!: SUP_leI positive_integral_mono le_SUPI)
   7.458 +  next
   7.459 +    def rf \<equiv> "\<lambda>i. \<lambda>x\<in>space M. f i x" and ru \<equiv> "\<lambda>x\<in>space M. ?u x"
   7.460 +    have "\<And>i. rf i \<in> borel_measurable M" unfolding rf_def
   7.461 +      using assms by (simp cong: measurable_cong)
   7.462 +    moreover have iso: "rf \<up> ru" using assms unfolding rf_def ru_def
   7.463 +      unfolding isoton_def SUPR_fun_expand le_fun_def fun_eq_iff
   7.464 +      by (auto simp: restrict_def le_fun_def SUPR_fun_expand fun_eq_iff)
   7.465 +    ultimately have "positive_integral ru \<le> (SUP i. positive_integral (rf i))"
   7.466 +      unfolding positive_integral_def[of ru]
   7.467 +      by (auto simp: le_fun_def intro!: SUP_leI positive_integral_SUP_approx)
   7.468 +    then show "positive_integral ?u \<le> (SUP i. positive_integral (f i))"
   7.469 +      unfolding ru_def rf_def by (simp cong: positive_integral_cong)
   7.470 +  qed
   7.471 +qed
   7.472 +
   7.473  lemma (in measure_space) SUP_simple_integral_sequences:
   7.474    assumes f: "f \<up> u" "\<And>i. simple_function (f i)"
   7.475    and g: "g \<up> u" "\<And>i. simple_function (g i)"
   7.476 @@ -1193,47 +1421,16 @@
   7.477  qed
   7.478  
   7.479  lemma (in measure_space) positive_integral_null_set:
   7.480 -  assumes borel: "u \<in> borel_measurable M" and "N \<in> null_sets"
   7.481 -  shows "positive_integral (\<lambda>x. u x * indicator N x) = 0" (is "?I = 0")
   7.482 +  assumes "N \<in> null_sets" shows "positive_integral (\<lambda>x. u x * indicator N x) = 0"
   7.483  proof -
   7.484 -  have "N \<in> sets M" using `N \<in> null_sets` by auto
   7.485 -  have "(\<lambda>i x. min (of_nat i) (u x) * indicator N x) \<up> (\<lambda>x. u x * indicator N x)"
   7.486 -    unfolding isoton_fun_expand
   7.487 -  proof (safe intro!: isoton_cmult_left, unfold isoton_def, safe)
   7.488 -    fix j i show "min (of_nat j) (u i) \<le> min (of_nat (Suc j)) (u i)"
   7.489 -      by (rule min_max.inf_mono) auto
   7.490 -  next
   7.491 -    fix i show "(SUP j. min (of_nat j) (u i)) = u i"
   7.492 -    proof (cases "u i")
   7.493 -      case infinite
   7.494 -      moreover hence "\<And>j. min (of_nat j) (u i) = of_nat j"
   7.495 -        by (auto simp: min_def)
   7.496 -      ultimately show ?thesis by (simp add: Sup_\<omega>)
   7.497 -    next
   7.498 -      case (preal r)
   7.499 -      obtain j where "r \<le> of_nat j" using ex_le_of_nat ..
   7.500 -      hence "u i \<le> of_nat j" using preal by (auto simp: real_of_nat_def)
   7.501 -      show ?thesis
   7.502 -      proof (rule pinfreal_SUPI)
   7.503 -        fix y assume "\<And>j. j \<in> UNIV \<Longrightarrow> min (of_nat j) (u i) \<le> y"
   7.504 -        note this[of j]
   7.505 -        moreover have "min (of_nat j) (u i) = u i"
   7.506 -          using `u i \<le> of_nat j` by (auto simp: min_def)
   7.507 -        ultimately show "u i \<le> y" by simp
   7.508 -      qed simp
   7.509 -    qed
   7.510 +  have "positive_integral (\<lambda>x. u x * indicator N x) = positive_integral (\<lambda>x. 0)"
   7.511 +  proof (intro positive_integral_cong_AE AE_I)
   7.512 +    show "{x \<in> space M. u x * indicator N x \<noteq> 0} \<subseteq> N"
   7.513 +      by (auto simp: indicator_def)
   7.514 +    show "\<mu> N = 0" "N \<in> sets M"
   7.515 +      using assms by auto
   7.516    qed
   7.517 -  from positive_integral_isoton[OF this]
   7.518 -  have "?I = (SUP i. positive_integral (\<lambda>x. min (of_nat i) (u x) * indicator N x))"
   7.519 -    unfolding isoton_def using borel `N \<in> sets M` by (simp add: borel_measurable_indicator)
   7.520 -  also have "\<dots> \<le> (SUP i. positive_integral (\<lambda>x. of_nat i * indicator N x))"
   7.521 -  proof (rule SUP_mono, rule bexI, rule positive_integral_mono)
   7.522 -    fix x i show "min (of_nat i) (u x) * indicator N x \<le> of_nat i * indicator N x"
   7.523 -      by (cases "x \<in> N") auto
   7.524 -  qed simp
   7.525 -  also have "\<dots> = 0"
   7.526 -    using `N \<in> null_sets` by (simp add: positive_integral_cmult_indicator)
   7.527 -  finally show ?thesis by simp
   7.528 +  then show ?thesis by simp
   7.529  qed
   7.530  
   7.531  lemma (in measure_space) positive_integral_Markov_inequality:
   7.532 @@ -1270,7 +1467,7 @@
   7.533    proof
   7.534      assume "\<mu> ?A = 0"
   7.535      hence "?A \<in> null_sets" using `?A \<in> sets M` by auto
   7.536 -    from positive_integral_null_set[OF borel this]
   7.537 +    from positive_integral_null_set[OF this]
   7.538      have "0 = positive_integral (\<lambda>x. u x * indicator ?A x)" by simp
   7.539      thus "positive_integral u = 0" unfolding u by simp
   7.540    next
   7.541 @@ -1309,34 +1506,6 @@
   7.542    qed
   7.543  qed
   7.544  
   7.545 -lemma (in measure_space) positive_integral_cong_on_null_sets:
   7.546 -  assumes f: "f \<in> borel_measurable M" and g: "g \<in> borel_measurable M"
   7.547 -  and measure: "\<mu> {x\<in>space M. f x \<noteq> g x} = 0"
   7.548 -  shows "positive_integral f = positive_integral g"
   7.549 -proof -
   7.550 -  let ?N = "{x\<in>space M. f x \<noteq> g x}" and ?E = "{x\<in>space M. f x = g x}"
   7.551 -  let "?A h x" = "h x * indicator ?E x :: pinfreal"
   7.552 -  let "?B h x" = "h x * indicator ?N x :: pinfreal"
   7.553 -
   7.554 -  have A: "positive_integral (?A f) = positive_integral (?A g)"
   7.555 -    by (auto intro!: positive_integral_cong simp: indicator_def)
   7.556 -
   7.557 -  have [intro]: "?N \<in> sets M" "?E \<in> sets M" using f g by auto
   7.558 -  hence "?N \<in> null_sets" using measure by auto
   7.559 -  hence B: "positive_integral (?B f) = positive_integral (?B g)"
   7.560 -    using f g by (simp add: positive_integral_null_set)
   7.561 -
   7.562 -  have "positive_integral f = positive_integral (\<lambda>x. ?A f x + ?B f x)"
   7.563 -    by (auto intro!: positive_integral_cong simp: indicator_def)
   7.564 -  also have "\<dots> = positive_integral (?A f) + positive_integral (?B f)"
   7.565 -    using f g by (auto intro!: positive_integral_add borel_measurable_indicator)
   7.566 -  also have "\<dots> = positive_integral (\<lambda>x. ?A g x + ?B g x)"
   7.567 -    unfolding A B using f g by (auto intro!: positive_integral_add[symmetric] borel_measurable_indicator)
   7.568 -  also have "\<dots> = positive_integral g"
   7.569 -    by (auto intro!: positive_integral_cong simp: indicator_def)
   7.570 -  finally show ?thesis by simp
   7.571 -qed
   7.572 -
   7.573  lemma (in measure_space) positive_integral_restricted:
   7.574    assumes "A \<in> sets M"
   7.575    shows "measure_space.positive_integral (restricted_space A) \<mu> f = positive_integral (\<lambda>x. f x * indicator A x)"
   7.576 @@ -1346,13 +1515,13 @@
   7.577    then interpret R: measure_space ?R \<mu> .
   7.578    have saR: "sigma_algebra ?R" by fact
   7.579    have *: "R.positive_integral f = R.positive_integral ?f"
   7.580 -    by (auto intro!: R.positive_integral_cong)
   7.581 +    by (intro R.positive_integral_cong) auto
   7.582    show ?thesis
   7.583      unfolding * R.positive_integral_def positive_integral_def
   7.584      unfolding simple_function_restricted[OF `A \<in> sets M`]
   7.585      apply (simp add: SUPR_def)
   7.586      apply (rule arg_cong[where f=Sup])
   7.587 -  proof (auto simp: image_iff simple_integral_restricted[OF `A \<in> sets M`])
   7.588 +  proof (auto simp add: image_iff simple_integral_restricted[OF `A \<in> sets M`])
   7.589      fix g assume "simple_function (\<lambda>x. g x * indicator A x)"
   7.590        "g \<le> f" "\<forall>x\<in>A. \<omega> \<noteq> g x"
   7.591      then show "\<exists>x. simple_function x \<and> x \<le> (\<lambda>x. f x * indicator A x) \<and> (\<forall>y\<in>space M. \<omega> \<noteq> x y) \<and>
   7.592 @@ -1413,6 +1582,29 @@
   7.593    shows "integral f = integral g"
   7.594    using assms by (simp cong: positive_integral_cong add: integral_def)
   7.595  
   7.596 +lemma (in measure_space) integral_cong_measure:
   7.597 +  assumes "\<And>A. A \<in> sets M \<Longrightarrow> \<nu> A = \<mu> A"
   7.598 +  shows "measure_space.integral M \<nu> f = integral f"
   7.599 +proof -
   7.600 +  interpret v: measure_space M \<nu>
   7.601 +    by (rule measure_space_cong) fact
   7.602 +  show ?thesis
   7.603 +    unfolding integral_def v.integral_def
   7.604 +    by (simp add: positive_integral_cong_measure[OF assms])
   7.605 +qed
   7.606 +
   7.607 +lemma (in measure_space) integral_cong_AE:
   7.608 +  assumes cong: "AE x. f x = g x"
   7.609 +  shows "integral f = integral g"
   7.610 +proof -
   7.611 +  have "AE x. Real (f x) = Real (g x)"
   7.612 +    using cong by (rule AE_mp) simp
   7.613 +  moreover have "AE x. Real (- f x) = Real (- g x)"
   7.614 +    using cong by (rule AE_mp) simp
   7.615 +  ultimately show ?thesis
   7.616 +    by (simp cong: positive_integral_cong_AE add: integral_def)
   7.617 +qed
   7.618 +
   7.619  lemma (in measure_space) integrable_cong:
   7.620    "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable f \<longleftrightarrow> integrable g"
   7.621    by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
   7.622 @@ -1425,6 +1617,23 @@
   7.623    thus ?thesis by (simp del: Real_eq_0 add: integral_def)
   7.624  qed
   7.625  
   7.626 +lemma (in measure_space) integral_vimage_inv:
   7.627 +  assumes f: "bij_betw f S (space M)"
   7.628 +  shows "measure_space.integral (vimage_algebra S f) (\<lambda>A. \<mu> (f ` A)) (\<lambda>x. g x) = integral (\<lambda>x. g (the_inv_into S f x))"
   7.629 +proof -
   7.630 +  interpret v: measure_space "vimage_algebra S f" "\<lambda>A. \<mu> (f ` A)"
   7.631 +    using f by (rule measure_space_isomorphic)
   7.632 +  have "\<And>x. x \<in> space (vimage_algebra S f) \<Longrightarrow> the_inv_into S f (f x) = x"
   7.633 +    using f[unfolded bij_betw_def] by (simp add: the_inv_into_f_f)
   7.634 +  then have *: "v.positive_integral (\<lambda>x. Real (g (the_inv_into S f (f x)))) = v.positive_integral (\<lambda>x. Real (g x))"
   7.635 +     "v.positive_integral (\<lambda>x. Real (- g (the_inv_into S f (f x)))) = v.positive_integral (\<lambda>x. Real (- g x))"
   7.636 +    by (auto intro!: v.positive_integral_cong)
   7.637 +  show ?thesis
   7.638 +    unfolding integral_def v.integral_def
   7.639 +    unfolding positive_integral_vimage[OF f]
   7.640 +    by (simp add: *)
   7.641 +qed
   7.642 +
   7.643  lemma (in measure_space) integral_minus[intro, simp]:
   7.644    assumes "integrable f"
   7.645    shows "integrable (\<lambda>x. - f x)" "integral (\<lambda>x. - f x) = - integral f"
   7.646 @@ -1549,15 +1758,26 @@
   7.647    thus ?P ?I by auto
   7.648  qed
   7.649  
   7.650 +lemma (in measure_space) integral_mono_AE:
   7.651 +  assumes fg: "integrable f" "integrable g"
   7.652 +  and mono: "AE t. f t \<le> g t"
   7.653 +  shows "integral f \<le> integral g"
   7.654 +proof -
   7.655 +  have "AE x. Real (f x) \<le> Real (g x)"
   7.656 +    using mono by (rule AE_mp) (auto intro!: AE_cong)
   7.657 +  moreover have "AE x. Real (- g x) \<le> Real (- f x)" 
   7.658 +    using mono by (rule AE_mp) (auto intro!: AE_cong)
   7.659 +  ultimately show ?thesis using fg
   7.660 +    by (auto simp: integral_def integrable_def diff_minus
   7.661 +             intro!: add_mono real_of_pinfreal_mono positive_integral_mono_AE)
   7.662 +qed
   7.663 +
   7.664  lemma (in measure_space) integral_mono:
   7.665    assumes fg: "integrable f" "integrable g"
   7.666    and mono: "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
   7.667    shows "integral f \<le> integral g"
   7.668 -  using fg unfolding integral_def integrable_def diff_minus
   7.669 -proof (safe intro!: add_mono real_of_pinfreal_mono le_imp_neg_le positive_integral_mono)
   7.670 -  fix x assume "x \<in> space M" from mono[OF this]
   7.671 -  show "Real (f x) \<le> Real (g x)" "Real (- g x) \<le> Real (- f x)" by auto
   7.672 -qed
   7.673 +  apply (rule integral_mono_AE[OF fg])
   7.674 +  using mono by (rule AE_cong) auto
   7.675  
   7.676  lemma (in measure_space) integral_diff[simp, intro]:
   7.677    assumes f: "integrable f" and g: "integrable g"
   7.678 @@ -1796,6 +2016,29 @@
   7.679      by (simp add: real_of_pinfreal_eq_0)
   7.680  qed
   7.681  
   7.682 +lemma (in measure_space) positive_integral_omega:
   7.683 +  assumes "f \<in> borel_measurable M"
   7.684 +  and "positive_integral f \<noteq> \<omega>"
   7.685 +  shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
   7.686 +proof -
   7.687 +  have "\<omega> * \<mu> (f -` {\<omega>} \<inter> space M) = positive_integral (\<lambda>x. \<omega> * indicator (f -` {\<omega>} \<inter> space M) x)"
   7.688 +    using positive_integral_cmult_indicator[OF borel_measurable_vimage, OF assms(1), of \<omega> \<omega>] by simp
   7.689 +  also have "\<dots> \<le> positive_integral f"
   7.690 +    by (auto intro!: positive_integral_mono simp: indicator_def)
   7.691 +  finally show ?thesis
   7.692 +    using assms(2) by (cases ?thesis) auto
   7.693 +qed
   7.694 +
   7.695 +lemma (in measure_space) simple_integral_omega:
   7.696 +  assumes "simple_function f"
   7.697 +  and "simple_integral f \<noteq> \<omega>"
   7.698 +  shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
   7.699 +proof (rule positive_integral_omega)
   7.700 +  show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
   7.701 +  show "positive_integral f \<noteq> \<omega>"
   7.702 +    using assms by (simp add: positive_integral_eq_simple_integral)
   7.703 +qed
   7.704 +
   7.705  lemma (in measure_space) integral_dominated_convergence:
   7.706    assumes u: "\<And>i. integrable (u i)" and bound: "\<And>x j. x\<in>space M \<Longrightarrow> \<bar>u j x\<bar> \<le> w x"
   7.707    and w: "integrable w" "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> w x"
     8.1 --- a/src/HOL/Probability/Lebesgue_Measure.thy	Wed Dec 01 06:50:54 2010 -0800
     8.2 +++ b/src/HOL/Probability/Lebesgue_Measure.thy	Wed Dec 01 19:20:30 2010 +0100
     8.3 @@ -1,38 +1,115 @@
     8.4 -
     8.5 +(*  Author: Robert Himmelmann, TU Muenchen *)
     8.6  header {* Lebsegue measure *}
     8.7 -(*  Author:                     Robert Himmelmann, TU Muenchen *)
     8.8 -
     8.9  theory Lebesgue_Measure
    8.10 -  imports Gauge_Measure Measure Lebesgue_Integration
    8.11 +  imports Product_Measure Gauge_Measure Complete_Measure
    8.12  begin
    8.13  
    8.14 -subsection {* Various *}
    8.15 +lemma (in complete_lattice) SUP_pair:
    8.16 +  "(SUP i:A. SUP j:B. f i j) = (SUP p:A\<times>B. (\<lambda> (i, j). f i j) p)" (is "?l = ?r")
    8.17 +proof (intro antisym SUP_leI)
    8.18 +  fix i j assume "i \<in> A" "j \<in> B"
    8.19 +  then have "(case (i,j) of (i,j) \<Rightarrow> f i j) \<le> ?r"
    8.20 +    by (intro SUPR_upper) auto
    8.21 +  then show "f i j \<le> ?r" by auto
    8.22 +next
    8.23 +  fix p assume "p \<in> A \<times> B"
    8.24 +  then obtain i j where "p = (i,j)" "i \<in> A" "j \<in> B" by auto
    8.25 +  have "f i j \<le> (SUP j:B. f i j)" using `j \<in> B` by (intro SUPR_upper)
    8.26 +  also have "(SUP j:B. f i j) \<le> ?l" using `i \<in> A` by (intro SUPR_upper)
    8.27 +  finally show "(case p of (i, j) \<Rightarrow> f i j) \<le> ?l" using `p = (i,j)` by simp
    8.28 +qed
    8.29  
    8.30 -lemma seq_offset_iff:"f ----> l \<longleftrightarrow> (\<lambda>i. f (i + k)) ----> l"
    8.31 -  using seq_offset_rev seq_offset[of f l k] by auto
    8.32 +lemma (in complete_lattice) SUP_surj_compose:
    8.33 +  assumes *: "f`A = B" shows "SUPR A (g \<circ> f) = SUPR B g"
    8.34 +  unfolding SUPR_def unfolding *[symmetric]
    8.35 +  by (simp add: image_compose)
    8.36 +
    8.37 +lemma (in complete_lattice) SUP_swap:
    8.38 +  "(SUP i:A. SUP j:B. f i j) = (SUP j:B. SUP i:A. f i j)"
    8.39 +proof -
    8.40 +  have *: "(\<lambda>(i,j). (j,i)) ` (B \<times> A) = A \<times> B" by auto
    8.41 +  show ?thesis
    8.42 +    unfolding SUP_pair SUP_surj_compose[symmetric, OF *]
    8.43 +    by (auto intro!: arg_cong[where f=Sup] image_eqI simp: comp_def SUPR_def)
    8.44 +qed
    8.45  
    8.46 -lemma has_integral_disjoint_union: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
    8.47 -  assumes "(f has_integral i) s" "(f has_integral j) t" "s \<inter> t = {}"
    8.48 -  shows "(f has_integral (i + j)) (s \<union> t)"
    8.49 -  apply(rule has_integral_union[OF assms(1-2)]) unfolding assms by auto
    8.50 +lemma SUP_\<omega>: "(SUP i:A. f i) = \<omega> \<longleftrightarrow> (\<forall>x<\<omega>. \<exists>i\<in>A. x < f i)"
    8.51 +proof
    8.52 +  assume *: "(SUP i:A. f i) = \<omega>"
    8.53 +  show "(\<forall>x<\<omega>. \<exists>i\<in>A. x < f i)" unfolding *[symmetric]
    8.54 +  proof (intro allI impI)
    8.55 +    fix x assume "x < SUPR A f" then show "\<exists>i\<in>A. x < f i"
    8.56 +      unfolding less_SUP_iff by auto
    8.57 +  qed
    8.58 +next
    8.59 +  assume *: "\<forall>x<\<omega>. \<exists>i\<in>A. x < f i"
    8.60 +  show "(SUP i:A. f i) = \<omega>"
    8.61 +  proof (rule pinfreal_SUPI)
    8.62 +    fix y assume **: "\<And>i. i \<in> A \<Longrightarrow> f i \<le> y"
    8.63 +    show "\<omega> \<le> y"
    8.64 +    proof cases
    8.65 +      assume "y < \<omega>"
    8.66 +      from *[THEN spec, THEN mp, OF this]
    8.67 +      obtain i where "i \<in> A" "\<not> (f i \<le> y)" by auto
    8.68 +      with ** show ?thesis by auto
    8.69 +    qed auto
    8.70 +  qed auto
    8.71 +qed
    8.72  
    8.73 -lemma lim_eq: assumes "\<forall>n>N. f n = g n" shows "(f ----> l) \<longleftrightarrow> (g ----> l)" using assms 
    8.74 -proof(induct N arbitrary: f g) case 0
    8.75 -  hence *:"\<And>n. f (Suc n) = g (Suc n)" by auto
    8.76 -  show ?case apply(subst LIMSEQ_Suc_iff[THEN sym]) apply(subst(2) LIMSEQ_Suc_iff[THEN sym])
    8.77 -    unfolding * ..
    8.78 -next case (Suc n)
    8.79 -  show ?case apply(subst LIMSEQ_Suc_iff[THEN sym]) apply(subst(2) LIMSEQ_Suc_iff[THEN sym])
    8.80 -    apply(rule Suc(1)) using Suc(2) by auto
    8.81 +lemma psuminf_commute:
    8.82 +  shows "(\<Sum>\<^isub>\<infinity> i j. f i j) = (\<Sum>\<^isub>\<infinity> j i. f i j)"
    8.83 +proof -
    8.84 +  have "(SUP n. \<Sum> i < n. SUP m. \<Sum> j < m. f i j) = (SUP n. SUP m. \<Sum> i < n. \<Sum> j < m. f i j)"
    8.85 +    apply (subst SUPR_pinfreal_setsum)
    8.86 +    by auto
    8.87 +  also have "\<dots> = (SUP m n. \<Sum> j < m. \<Sum> i < n. f i j)"
    8.88 +    apply (subst SUP_swap)
    8.89 +    apply (subst setsum_commute)
    8.90 +    by auto
    8.91 +  also have "\<dots> = (SUP m. \<Sum> j < m. SUP n. \<Sum> i < n. f i j)"
    8.92 +    apply (subst SUPR_pinfreal_setsum)
    8.93 +    by auto
    8.94 +  finally show ?thesis
    8.95 +    unfolding psuminf_def by auto
    8.96 +qed
    8.97 +
    8.98 +lemma psuminf_SUP_eq:
    8.99 +  assumes "\<And>n i. f n i \<le> f (Suc n) i"
   8.100 +  shows "(\<Sum>\<^isub>\<infinity> i. SUP n::nat. f n i) = (SUP n::nat. \<Sum>\<^isub>\<infinity> i. f n i)"
   8.101 +proof -
   8.102 +  { fix n :: nat
   8.103 +    have "(\<Sum>i<n. SUP k. f k i) = (SUP k. \<Sum>i<n. f k i)"
   8.104 +      using assms by (auto intro!: SUPR_pinfreal_setsum[symmetric]) }
   8.105 +  note * = this
   8.106 +  show ?thesis
   8.107 +    unfolding psuminf_def
   8.108 +    unfolding *
   8.109 +    apply (subst SUP_swap) ..
   8.110  qed
   8.111  
   8.112  subsection {* Standard Cubes *}
   8.113  
   8.114 -definition cube where
   8.115 -  "cube (n::nat) \<equiv> {\<chi>\<chi> i. - real n .. (\<chi>\<chi> i. real n)::_::ordered_euclidean_space}"
   8.116 +definition cube :: "nat \<Rightarrow> 'a::ordered_euclidean_space set" where
   8.117 +  "cube n \<equiv> {\<chi>\<chi> i. - real n .. \<chi>\<chi> i. real n}"
   8.118 +
   8.119 +lemma cube_closed[intro]: "closed (cube n)"
   8.120 +  unfolding cube_def by auto
   8.121 +
   8.122 +lemma cube_subset[intro]: "n \<le> N \<Longrightarrow> cube n \<subseteq> cube N"
   8.123 +  by (fastsimp simp: eucl_le[where 'a='a] cube_def)
   8.124  
   8.125 -lemma cube_subset[intro]:"n\<le>N \<Longrightarrow> cube n \<subseteq> (cube N::'a::ordered_euclidean_space set)"
   8.126 -  apply(auto simp: eucl_le[where 'a='a] cube_def) apply(erule_tac[!] x=i in allE)+ by auto
   8.127 +lemma cube_subset_iff:
   8.128 +  "cube n \<subseteq> cube N \<longleftrightarrow> n \<le> N"
   8.129 +proof
   8.130 +  assume subset: "cube n \<subseteq> (cube N::'a set)"
   8.131 +  then have "((\<chi>\<chi> i. real n)::'a) \<in> cube N"
   8.132 +    using DIM_positive[where 'a='a]
   8.133 +    by (fastsimp simp: cube_def eucl_le[where 'a='a])
   8.134 +  then show "n \<le> N"
   8.135 +    by (fastsimp simp: cube_def eucl_le[where 'a='a])
   8.136 +next
   8.137 +  assume "n \<le> N" then show "cube n \<subseteq> (cube N::'a set)" by (rule cube_subset)
   8.138 +qed
   8.139  
   8.140  lemma ball_subset_cube:"ball (0::'a::ordered_euclidean_space) (real n) \<subseteq> cube n"
   8.141    unfolding cube_def subset_eq mem_interval apply safe unfolding euclidean_lambda_beta'
   8.142 @@ -63,202 +140,277 @@
   8.143    apply(rule has_gmeasure_subset[of "s\<inter>cube n" _ "cube n"])
   8.144    unfolding has_gmeasure_measure[THEN sym] using assms by auto
   8.145  
   8.146 +lemma has_gmeasure_cube[intro]: "(cube n::('a::ordered_euclidean_space) set)
   8.147 +  has_gmeasure ((2 * real n) ^ (DIM('a)))"
   8.148 +proof-
   8.149 +  have "content {\<chi>\<chi> i. - real n..(\<chi>\<chi> i. real n)::'a} = (2 * real n) ^ (DIM('a))"
   8.150 +    apply(subst content_closed_interval) defer
   8.151 +    by (auto simp add:setprod_constant)
   8.152 +  thus ?thesis unfolding cube_def
   8.153 +    using has_gmeasure_interval(1)[of "(\<chi>\<chi> i. - real n)::'a" "(\<chi>\<chi> i. real n)::'a"]
   8.154 +    by auto
   8.155 +qed
   8.156 +
   8.157 +lemma gmeasure_cube_eq[simp]:
   8.158 +  "gmeasure (cube n::('a::ordered_euclidean_space) set) = (2 * real n) ^ DIM('a)"
   8.159 +  by (intro measure_unique) auto
   8.160 +
   8.161 +lemma gmeasure_cube_ge_n: "gmeasure (cube n::('a::ordered_euclidean_space) set) \<ge> real n"
   8.162 +proof cases
   8.163 +  assume "n = 0" then show ?thesis by simp
   8.164 +next
   8.165 +  assume "n \<noteq> 0"
   8.166 +  have "real n \<le> (2 * real n)^1" by simp
   8.167 +  also have "\<dots> \<le> (2 * real n)^DIM('a)"
   8.168 +    using DIM_positive[where 'a='a] `n \<noteq> 0`
   8.169 +    by (intro power_increasing) auto
   8.170 +  also have "\<dots> = gmeasure (cube n::'a set)" by simp
   8.171 +  finally show ?thesis .
   8.172 +qed
   8.173 +
   8.174 +lemma gmeasure_setsum:
   8.175 +  assumes "finite A" and "\<And>s t. s \<in> A \<Longrightarrow> t \<in> A \<Longrightarrow> s \<noteq> t \<Longrightarrow> f s \<inter> f t = {}"
   8.176 +    and "\<And>i. i \<in> A \<Longrightarrow> gmeasurable (f i)"
   8.177 +  shows "gmeasure (\<Union>i\<in>A. f i) = (\<Sum>i\<in>A. gmeasure (f i))"
   8.178 +proof -
   8.179 +  have "gmeasure (\<Union>i\<in>A. f i) = gmeasure (\<Union>f ` A)" by auto
   8.180 +  also have "\<dots> = setsum gmeasure (f ` A)" using assms
   8.181 +  proof (intro measure_negligible_unions)
   8.182 +    fix X Y assume "X \<in> f`A" "Y \<in> f`A" "X \<noteq> Y"
   8.183 +    then have "X \<inter> Y = {}" using assms by auto
   8.184 +    then show "negligible (X \<inter> Y)" by auto
   8.185 +  qed auto
   8.186 +  also have "\<dots> = setsum gmeasure (f ` A - {{}})"
   8.187 +    using assms by (intro setsum_mono_zero_cong_right) auto
   8.188 +  also have "\<dots> = (\<Sum>i\<in>A - {i. f i = {}}. gmeasure (f i))"
   8.189 +  proof (intro setsum_reindex_cong inj_onI)
   8.190 +    fix s t assume *: "s \<in> A - {i. f i = {}}" "t \<in> A - {i. f i = {}}" "f s = f t"
   8.191 +    show "s = t"
   8.192 +    proof (rule ccontr)
   8.193 +      assume "s \<noteq> t" with assms(2)[of s t] * show False by auto
   8.194 +    qed
   8.195 +  qed auto
   8.196 +  also have "\<dots> = (\<Sum>i\<in>A. gmeasure (f i))"
   8.197 +    using assms by (intro setsum_mono_zero_cong_left) auto
   8.198 +  finally show ?thesis .
   8.199 +qed
   8.200 +
   8.201 +lemma gmeasurable_finite_UNION[intro]:
   8.202 +  assumes "\<And>i. i \<in> S \<Longrightarrow> gmeasurable (A i)" "finite S"
   8.203 +  shows "gmeasurable (\<Union>i\<in>S. A i)"
   8.204 +  unfolding UNION_eq_Union_image using assms
   8.205 +  by (intro gmeasurable_finite_unions) auto
   8.206 +
   8.207 +lemma gmeasurable_countable_UNION[intro]:
   8.208 +  fixes A :: "nat \<Rightarrow> ('a::ordered_euclidean_space) set"
   8.209 +  assumes measurable: "\<And>i. gmeasurable (A i)"
   8.210 +    and finite: "\<And>n. gmeasure (UNION {.. n} A) \<le> B"
   8.211 +  shows "gmeasurable (\<Union>i. A i)"
   8.212 +proof -
   8.213 +  have *: "\<And>n. \<Union>{A k |k. k \<le> n} = (\<Union>i\<le>n. A i)"
   8.214 +    "(\<Union>{A n |n. n \<in> UNIV}) = (\<Union>i. A i)" by auto
   8.215 +  show ?thesis
   8.216 +    by (rule gmeasurable_countable_unions_strong[of A B, unfolded *, OF assms])
   8.217 +qed
   8.218  
   8.219  subsection {* Measurability *}
   8.220  
   8.221 -definition lmeasurable :: "('a::ordered_euclidean_space) set => bool" where
   8.222 -  "lmeasurable s \<equiv> (\<forall>n::nat. gmeasurable (s \<inter> cube n))"
   8.223 +definition lebesgue :: "'a::ordered_euclidean_space algebra" where
   8.224 +  "lebesgue = \<lparr> space = UNIV, sets = {A. \<forall>n. gmeasurable (A \<inter> cube n)} \<rparr>"
   8.225 +
   8.226 +lemma space_lebesgue[simp]:"space lebesgue = UNIV"
   8.227 +  unfolding lebesgue_def by auto
   8.228  
   8.229 -lemma lmeasurableD[dest]:assumes "lmeasurable s"
   8.230 -  shows "\<And>n. gmeasurable (s \<inter> cube n)"
   8.231 -  using assms unfolding lmeasurable_def by auto
   8.232 +lemma lebesgueD[dest]: assumes "S \<in> sets lebesgue"
   8.233 +  shows "\<And>n. gmeasurable (S \<inter> cube n)"
   8.234 +  using assms unfolding lebesgue_def by auto
   8.235  
   8.236 -lemma measurable_imp_lmeasurable: assumes"gmeasurable s"
   8.237 -  shows "lmeasurable s" unfolding lmeasurable_def cube_def 
   8.238 +lemma lebesgueI[intro]: assumes "gmeasurable S"
   8.239 +  shows "S \<in> sets lebesgue" unfolding lebesgue_def cube_def
   8.240    using assms gmeasurable_interval by auto
   8.241  
   8.242 -lemma lmeasurable_empty[intro]: "lmeasurable {}"
   8.243 -  using gmeasurable_empty apply- apply(drule_tac measurable_imp_lmeasurable) .
   8.244 -
   8.245 -lemma lmeasurable_union[intro]: assumes "lmeasurable s" "lmeasurable t"
   8.246 -  shows "lmeasurable (s \<union> t)"
   8.247 -  using assms unfolding lmeasurable_def Int_Un_distrib2 
   8.248 -  by(auto intro:gmeasurable_union)
   8.249 +lemma lebesgueI2: "(\<And>n. gmeasurable (S \<inter> cube n)) \<Longrightarrow> S \<in> sets lebesgue"
   8.250 +  using assms unfolding lebesgue_def by auto
   8.251  
   8.252 -lemma lmeasurable_countable_unions_strong:
   8.253 -  fixes s::"nat => 'a::ordered_euclidean_space set"
   8.254 -  assumes "\<And>n::nat. lmeasurable(s n)"
   8.255 -  shows "lmeasurable(\<Union>{ s(n) |n. n \<in> UNIV })"
   8.256 -  unfolding lmeasurable_def
   8.257 -proof fix n::nat
   8.258 -  have *:"\<Union>{s n |n. n \<in> UNIV} \<inter> cube n = \<Union>{s k \<inter> cube n |k. k \<in> UNIV}" by auto
   8.259 -  show "gmeasurable (\<Union>{s n |n. n \<in> UNIV} \<inter> cube n)" unfolding *
   8.260 -    apply(rule gmeasurable_countable_unions_strong)
   8.261 -    apply(rule assms[unfolded lmeasurable_def,rule_format])
   8.262 -  proof- fix k::nat
   8.263 -    show "gmeasure (\<Union>{s ka \<inter> cube n |ka. ka \<le> k}) \<le> gmeasure (cube n::'a set)"
   8.264 -      apply(rule measure_subset) apply(rule gmeasurable_finite_unions)
   8.265 -      using assms(1)[unfolded lmeasurable_def] by auto
   8.266 -  qed
   8.267 +interpretation lebesgue: sigma_algebra lebesgue
   8.268 +proof
   8.269 +  show "sets lebesgue \<subseteq> Pow (space lebesgue)"
   8.270 +    unfolding lebesgue_def by auto
   8.271 +  show "{} \<in> sets lebesgue"
   8.272 +    using gmeasurable_empty by auto
   8.273 +  { fix A B :: "'a set" assume "A \<in> sets lebesgue" "B \<in> sets lebesgue"
   8.274 +    then show "A \<union> B \<in> sets lebesgue"
   8.275 +      by (auto intro: gmeasurable_union simp: lebesgue_def Int_Un_distrib2) }
   8.276 +  { fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets lebesgue"
   8.277 +    show "(\<Union>i. A i) \<in> sets lebesgue"
   8.278 +    proof (rule lebesgueI2)
   8.279 +      fix n show "gmeasurable ((\<Union>i. A i) \<inter> cube n)" unfolding UN_extend_simps
   8.280 +        using A
   8.281 +        by (intro gmeasurable_countable_UNION[where B="gmeasure (cube n::'a set)"])
   8.282 +           (auto intro!: measure_subset gmeasure_setsum simp: UN_extend_simps simp del: gmeasure_cube_eq UN_simps)
   8.283 +    qed }
   8.284 +  { fix A assume A: "A \<in> sets lebesgue" show "space lebesgue - A \<in> sets lebesgue"
   8.285 +    proof (rule lebesgueI2)
   8.286 +      fix n
   8.287 +      have *: "(space lebesgue - A) \<inter> cube n = cube n - (A \<inter> cube n)"
   8.288 +        unfolding lebesgue_def by auto
   8.289 +      show "gmeasurable ((space lebesgue - A) \<inter> cube n)" unfolding *
   8.290 +        using A by (auto intro!: gmeasurable_diff)
   8.291 +    qed }
   8.292  qed
   8.293  
   8.294 -lemma lmeasurable_inter[intro]: fixes s::"'a :: ordered_euclidean_space set"
   8.295 -  assumes "lmeasurable s" "lmeasurable t" shows "lmeasurable (s \<inter> t)"
   8.296 -  unfolding lmeasurable_def
   8.297 -proof fix n::nat
   8.298 -  have *:"s \<inter> t \<inter> cube n = (s \<inter> cube n) \<inter> (t \<inter> cube n)" by auto
   8.299 -  show "gmeasurable (s \<inter> t \<inter> cube n)"
   8.300 -    using assms unfolding lmeasurable_def *
   8.301 -    using gmeasurable_inter[of "s \<inter> cube n" "t \<inter> cube n"] by auto
   8.302 +lemma lebesgueI_borel[intro, simp]: fixes s::"'a::ordered_euclidean_space set"
   8.303 +  assumes "s \<in> sets borel" shows "s \<in> sets lebesgue"
   8.304 +proof- let ?S = "range (\<lambda>(a, b). {a .. (b :: 'a\<Colon>ordered_euclidean_space)})"
   8.305 +  have *:"?S \<subseteq> sets lebesgue" by auto
   8.306 +  have "s \<in> sigma_sets UNIV ?S" using assms
   8.307 +    unfolding borel_eq_atLeastAtMost by (simp add: sigma_def)
   8.308 +  thus ?thesis
   8.309 +    using lebesgue.sigma_subset[of "\<lparr> space = UNIV, sets = ?S\<rparr>", simplified, OF *]
   8.310 +    by (auto simp: sigma_def)
   8.311  qed
   8.312  
   8.313 -lemma lmeasurable_complement[intro]: assumes "lmeasurable s"
   8.314 -  shows "lmeasurable (UNIV - s)"
   8.315 -  unfolding lmeasurable_def
   8.316 -proof fix n::nat
   8.317 -  have *:"(UNIV - s) \<inter> cube n = cube n - (s \<inter> cube n)" by auto
   8.318 -  show "gmeasurable ((UNIV - s) \<inter> cube n)" unfolding * 
   8.319 -    apply(rule gmeasurable_diff) using assms unfolding lmeasurable_def by auto
   8.320 -qed
   8.321 -
   8.322 -lemma lmeasurable_finite_unions:
   8.323 -  assumes "finite f" "\<And>s. s \<in> f \<Longrightarrow> lmeasurable s"
   8.324 -  shows "lmeasurable (\<Union> f)" unfolding lmeasurable_def
   8.325 -proof fix n::nat have *:"(\<Union>f \<inter> cube n) = \<Union>{x \<inter> cube n | x . x\<in>f}" by auto
   8.326 -  show "gmeasurable (\<Union>f \<inter> cube n)" unfolding *
   8.327 -    apply(rule gmeasurable_finite_unions) unfolding simple_image 
   8.328 -    using assms unfolding lmeasurable_def by auto
   8.329 -qed
   8.330 -
   8.331 -lemma negligible_imp_lmeasurable[dest]: fixes s::"'a::ordered_euclidean_space set"
   8.332 -  assumes "negligible s" shows "lmeasurable s"
   8.333 -  unfolding lmeasurable_def
   8.334 -proof case goal1
   8.335 +lemma lebesgueI_negligible[dest]: fixes s::"'a::ordered_euclidean_space set"
   8.336 +  assumes "negligible s" shows "s \<in> sets lebesgue"
   8.337 +proof (rule lebesgueI2)
   8.338 +  fix n
   8.339    have *:"\<And>x. (if x \<in> cube n then indicator s x else 0) = (if x \<in> s \<inter> cube n then 1 else 0)"
   8.340      unfolding indicator_def_raw by auto
   8.341    note assms[unfolded negligible_def,rule_format,of "(\<chi>\<chi> i. - real n)::'a" "\<chi>\<chi> i. real n"]
   8.342 -  thus ?case apply-apply(rule gmeasurableI[of _ 0]) unfolding has_gmeasure_def
   8.343 +  thus "gmeasurable (s \<inter> cube n)" apply-apply(rule gmeasurableI[of _ 0]) unfolding has_gmeasure_def
   8.344      apply(subst(asm) has_integral_restrict_univ[THEN sym]) unfolding cube_def[symmetric]
   8.345      apply(subst has_integral_restrict_univ[THEN sym]) unfolding * .
   8.346  qed
   8.347  
   8.348 -
   8.349  section {* The Lebesgue Measure *}
   8.350  
   8.351 -definition has_lmeasure (infixr "has'_lmeasure" 80) where
   8.352 -  "s has_lmeasure m \<equiv> lmeasurable s \<and> ((\<lambda>n. Real (gmeasure (s \<inter> cube n))) ---> m) sequentially"
   8.353 +definition "lmeasure A = (SUP n. Real (gmeasure (A \<inter> cube n)))"
   8.354  
   8.355 -lemma has_lmeasureD: assumes "s has_lmeasure m"
   8.356 -  shows "lmeasurable s" "gmeasurable (s \<inter> cube n)"
   8.357 -  "((\<lambda>n. Real (gmeasure (s \<inter> cube n))) ---> m) sequentially"
   8.358 -  using assms unfolding has_lmeasure_def lmeasurable_def by auto
   8.359 +lemma lmeasure_eq_0: assumes "negligible S" shows "lmeasure S = 0"
   8.360 +proof -
   8.361 +  from lebesgueI_negligible[OF assms]
   8.362 +  have "\<And>n. gmeasurable (S \<inter> cube n)" by auto
   8.363 +  from gmeasurable_measure_eq_0[OF this]
   8.364 +  have "\<And>n. gmeasure (S \<inter> cube n) = 0" using assms by auto
   8.365 +  then show ?thesis unfolding lmeasure_def by simp
   8.366 +qed
   8.367 +
   8.368 +lemma lmeasure_iff_LIMSEQ:
   8.369 +  assumes "A \<in> sets lebesgue" "0 \<le> m"
   8.370 +  shows "lmeasure A = Real m \<longleftrightarrow> (\<lambda>n. (gmeasure (A \<inter> cube n))) ----> m"
   8.371 +  unfolding lmeasure_def using assms cube_subset[where 'a='a]
   8.372 +  by (intro SUP_eq_LIMSEQ monoI measure_subset) force+
   8.373  
   8.374 -lemma has_lmeasureI: assumes "lmeasurable s" "((\<lambda>n. Real (gmeasure (s \<inter> cube n))) ---> m) sequentially"
   8.375 -  shows "s has_lmeasure m" using assms unfolding has_lmeasure_def by auto
   8.376 -
   8.377 -definition lmeasure where
   8.378 -  "lmeasure s \<equiv> SOME m. s has_lmeasure m"
   8.379 +interpretation lebesgue: measure_space lebesgue lmeasure
   8.380 +proof
   8.381 +  show "lmeasure {} = 0"
   8.382 +    by (auto intro!: lmeasure_eq_0)
   8.383 +  show "countably_additive lebesgue lmeasure"
   8.384 +  proof (unfold countably_additive_def, intro allI impI conjI)
   8.385 +    fix A :: "nat \<Rightarrow> 'b set" assume "range A \<subseteq> sets lebesgue" "disjoint_family A"
   8.386 +    then have A: "\<And>i. A i \<in> sets lebesgue" by auto
   8.387 +    show "(\<Sum>\<^isub>\<infinity>n. lmeasure (A n)) = lmeasure (\<Union>i. A i)" unfolding lmeasure_def
   8.388 +    proof (subst psuminf_SUP_eq)
   8.389 +      { fix i n
   8.390 +        have "gmeasure (A i \<inter> cube n) \<le> gmeasure (A i \<inter> cube (Suc n))"
   8.391 +          using A cube_subset[of n "Suc n"] by (auto intro!: measure_subset)
   8.392 +        then show "Real (gmeasure (A i \<inter> cube n)) \<le> Real (gmeasure (A i \<inter> cube (Suc n)))"
   8.393 +          by auto }
   8.394 +      show "(SUP n. \<Sum>\<^isub>\<infinity>i. Real (gmeasure (A i \<inter> cube n))) = (SUP n. Real (gmeasure ((\<Union>i. A i) \<inter> cube n)))"
   8.395 +      proof (intro arg_cong[where f="SUPR UNIV"] ext)
   8.396 +        fix n
   8.397 +        have sums: "(\<lambda>i. gmeasure (A i \<inter> cube n)) sums gmeasure (\<Union>{A i \<inter> cube n |i. i \<in> UNIV})"
   8.398 +        proof (rule has_gmeasure_countable_negligible_unions(2))
   8.399 +          fix i show "gmeasurable (A i \<inter> cube n)" using A by auto
   8.400 +        next
   8.401 +          fix i m :: nat assume "m \<noteq> i"
   8.402 +          then have "A m \<inter> cube n \<inter> (A i \<inter> cube n) = {}"
   8.403 +            using `disjoint_family A` unfolding disjoint_family_on_def by auto
   8.404 +          then show "negligible (A m \<inter> cube n \<inter> (A i \<inter> cube n))" by auto
   8.405 +        next
   8.406 +          fix i
   8.407 +          have "(\<Sum>k = 0..i. gmeasure (A k \<inter> cube n)) = gmeasure (\<Union>k\<le>i . A k \<inter> cube n)"
   8.408 +            unfolding atLeast0AtMost using A
   8.409 +          proof (intro gmeasure_setsum[symmetric])
   8.410 +            fix s t :: nat assume "s \<noteq> t" then have "A t \<inter> A s = {}"
   8.411 +              using `disjoint_family A` unfolding disjoint_family_on_def by auto
   8.412 +            then show "A s \<inter> cube n \<inter> (A t \<inter> cube n) = {}" by auto
   8.413 +          qed auto
   8.414 +          also have "\<dots> \<le> gmeasure (cube n :: 'b set)" using A
   8.415 +            by (intro measure_subset gmeasurable_finite_UNION) auto
   8.416 +          finally show "(\<Sum>k = 0..i. gmeasure (A k \<inter> cube n)) \<le> gmeasure (cube n :: 'b set)" .
   8.417 +        qed
   8.418 +        show "(\<Sum>\<^isub>\<infinity>i. Real (gmeasure (A i \<inter> cube n))) = Real (gmeasure ((\<Union>i. A i) \<inter> cube n))"
   8.419 +          unfolding psuminf_def
   8.420 +          apply (subst setsum_Real)
   8.421 +          apply (simp add: measure_pos_le)
   8.422 +        proof (rule SUP_eq_LIMSEQ[THEN iffD2])
   8.423 +          have "(\<Union>{A i \<inter> cube n |i. i \<in> UNIV}) = (\<Union>i. A i) \<inter> cube n" by auto
   8.424 +          with sums show "(\<lambda>i. \<Sum>k<i. gmeasure (A k \<inter> cube n)) ----> gmeasure ((\<Union>i. A i) \<inter> cube n)"
   8.425 +            unfolding sums_def atLeast0LessThan by simp
   8.426 +        qed (auto intro!: monoI setsum_nonneg setsum_mono2)
   8.427 +      qed
   8.428 +    qed
   8.429 +  qed
   8.430 +qed
   8.431  
   8.432 -lemma has_lmeasure_has_gmeasure: assumes "s has_lmeasure (Real m)" "m\<ge>0"
   8.433 +lemma lmeasure_finite_has_gmeasure: assumes "s \<in> sets lebesgue" "lmeasure s = Real m" "0 \<le> m"
   8.434    shows "s has_gmeasure m"
   8.435 -proof- note s = has_lmeasureD[OF assms(1)]
   8.436 +proof-
   8.437    have *:"(\<lambda>n. (gmeasure (s \<inter> cube n))) ----> m"
   8.438 -    using s(3) apply(subst (asm) lim_Real) using s(2) assms(2) by auto
   8.439 -
   8.440 +    using `lmeasure s = Real m` unfolding lmeasure_iff_LIMSEQ[OF `s \<in> sets lebesgue` `0 \<le> m`] .
   8.441 +  have s: "\<And>n. gmeasurable (s \<inter> cube n)" using assms by auto
   8.442    have "(\<lambda>x. if x \<in> s then 1 else (0::real)) integrable_on UNIV \<and>
   8.443      (\<lambda>k. integral UNIV (\<lambda>x. if x \<in> s \<inter> cube k then 1 else (0::real)))
   8.444      ----> integral UNIV (\<lambda>x. if x \<in> s then 1 else 0)"
   8.445    proof(rule monotone_convergence_increasing)
   8.446 -    have "\<forall>n. gmeasure (s \<inter> cube n) \<le> m" apply(rule ccontr) unfolding not_all not_le
   8.447 -    proof(erule exE) fix k assume k:"m < gmeasure (s \<inter> cube k)"
   8.448 -      hence "gmeasure (s \<inter> cube k) - m > 0" by auto
   8.449 -      from *[unfolded Lim_sequentially,rule_format,OF this] guess N ..
   8.450 -      note this[unfolded dist_real_def,rule_format,of "N + k"]
   8.451 -      moreover have "gmeasure (s \<inter> cube (N + k)) \<ge> gmeasure (s \<inter> cube k)" apply-
   8.452 -        apply(rule measure_subset) prefer 3 using s(2) 
   8.453 -        using cube_subset[of k "N + k"] by auto
   8.454 -      ultimately show False by auto
   8.455 -    qed
   8.456 -    thus *:"bounded {integral UNIV (\<lambda>x. if x \<in> s \<inter> cube k then 1 else (0::real)) |k. True}" 
   8.457 -      unfolding integral_measure_univ[OF s(2)] bounded_def apply-
   8.458 +    have "lmeasure s \<le> Real m" using `lmeasure s = Real m` by simp
   8.459 +    then have "\<forall>n. gmeasure (s \<inter> cube n) \<le> m"
   8.460 +      unfolding lmeasure_def complete_lattice_class.SUP_le_iff
   8.461 +      using `0 \<le> m` by (auto simp: measure_pos_le)
   8.462 +    thus *:"bounded {integral UNIV (\<lambda>x. if x \<in> s \<inter> cube k then 1 else (0::real)) |k. True}"
   8.463 +      unfolding integral_measure_univ[OF s] bounded_def apply-
   8.464        apply(rule_tac x=0 in exI,rule_tac x=m in exI) unfolding dist_real_def
   8.465        by (auto simp: measure_pos_le)
   8.466 -
   8.467      show "\<forall>k. (\<lambda>x. if x \<in> s \<inter> cube k then (1::real) else 0) integrable_on UNIV"
   8.468        unfolding integrable_restrict_univ
   8.469 -      using s(2) unfolding gmeasurable_def has_gmeasure_def by auto
   8.470 +      using s unfolding gmeasurable_def has_gmeasure_def by auto
   8.471      have *:"\<And>n. n \<le> Suc n" by auto
   8.472      show "\<forall>k. \<forall>x\<in>UNIV. (if x \<in> s \<inter> cube k then 1 else 0) \<le> (if x \<in> s \<inter> cube (Suc k) then 1 else (0::real))"
   8.473        using cube_subset[OF *] by fastsimp
   8.474      show "\<forall>x\<in>UNIV. (\<lambda>k. if x \<in> s \<inter> cube k then 1 else 0) ----> (if x \<in> s then 1 else (0::real))"
   8.475 -      unfolding Lim_sequentially 
   8.476 +      unfolding Lim_sequentially
   8.477      proof safe case goal1 from real_arch_lt[of "norm x"] guess N .. note N = this
   8.478        show ?case apply(rule_tac x=N in exI)
   8.479        proof safe case goal1
   8.480          have "x \<in> cube n" using cube_subset[OF goal1] N
   8.481 -          using ball_subset_cube[of N] by(auto simp: dist_norm) 
   8.482 +          using ball_subset_cube[of N] by(auto simp: dist_norm)
   8.483          thus ?case using `e>0` by auto
   8.484        qed
   8.485      qed
   8.486    qed note ** = conjunctD2[OF this]
   8.487    hence *:"m = integral UNIV (\<lambda>x. if x \<in> s then 1 else 0)" apply-
   8.488 -    apply(rule LIMSEQ_unique[OF _ **(2)]) unfolding measure_integral_univ[THEN sym,OF s(2)] using * .
   8.489 +    apply(rule LIMSEQ_unique[OF _ **(2)]) unfolding measure_integral_univ[THEN sym,OF s] using * .
   8.490    show ?thesis unfolding has_gmeasure * apply(rule integrable_integral) using ** by auto
   8.491  qed
   8.492  
   8.493 -lemma has_lmeasure_unique: "s has_lmeasure m1 \<Longrightarrow> s has_lmeasure m2 \<Longrightarrow> m1 = m2"
   8.494 -  unfolding has_lmeasure_def apply(rule Lim_unique) using trivial_limit_sequentially by auto
   8.495 -
   8.496 -lemma lmeasure_unique[intro]: assumes "A has_lmeasure m" shows "lmeasure A = m"
   8.497 -  using assms unfolding lmeasure_def lmeasurable_def apply-
   8.498 -  apply(rule some_equality) defer apply(rule has_lmeasure_unique) by auto
   8.499 -
   8.500 -lemma glmeasurable_finite: assumes "lmeasurable s" "lmeasure s \<noteq> \<omega>" 
   8.501 +lemma lmeasure_finite_gmeasurable: assumes "s \<in> sets lebesgue" "lmeasure s \<noteq> \<omega>"
   8.502    shows "gmeasurable s"
   8.503 -proof-  have "\<exists>B. \<forall>n. gmeasure (s \<inter> cube n) \<le> B"
   8.504 -  proof(rule ccontr) case goal1
   8.505 -    note as = this[unfolded not_ex not_all not_le]
   8.506 -    have "s has_lmeasure \<omega>" apply- apply(rule has_lmeasureI[OF assms(1)])
   8.507 -      unfolding Lim_omega
   8.508 -    proof fix B::real
   8.509 -      from as[rule_format,of B] guess N .. note N = this
   8.510 -      have "\<And>n. N \<le> n \<Longrightarrow> B \<le> gmeasure (s \<inter> cube n)"
   8.511 -        apply(rule order_trans[where y="gmeasure (s \<inter> cube N)"]) defer
   8.512 -        apply(rule measure_subset) prefer 3
   8.513 -        using cube_subset N assms(1)[unfolded lmeasurable_def] by auto
   8.514 -      thus "\<exists>N. \<forall>n\<ge>N. Real B \<le> Real (gmeasure (s \<inter> cube n))" apply-
   8.515 -        apply(subst Real_max') apply(rule_tac x=N in exI,safe)
   8.516 -        unfolding pinfreal_less_eq apply(subst if_P) by auto
   8.517 -    qed note lmeasure_unique[OF this]
   8.518 -    thus False using assms(2) by auto
   8.519 -  qed then guess B .. note B=this
   8.520 +proof (cases "lmeasure s")
   8.521 +  case (preal m) from lmeasure_finite_has_gmeasure[OF `s \<in> sets lebesgue` this]
   8.522 +  show ?thesis unfolding gmeasurable_def by auto
   8.523 +qed (insert assms, auto)
   8.524  
   8.525 -  show ?thesis apply(rule gmeasurable_nested_unions[of "\<lambda>n. s \<inter> cube n",
   8.526 -    unfolded Union_inter_cube,THEN conjunct1, where B1=B])
   8.527 -  proof- fix n::nat
   8.528 -    show " gmeasurable (s \<inter> cube n)" using assms by auto
   8.529 -    show "gmeasure (s \<inter> cube n) \<le> B" using B by auto
   8.530 -    show "s \<inter> cube n \<subseteq> s \<inter> cube (Suc n)"
   8.531 -      by (rule Int_mono) (simp_all add: cube_subset)
   8.532 -  qed
   8.533 -qed
   8.534 -
   8.535 -lemma lmeasure_empty[intro]:"lmeasure {} = 0"
   8.536 -  apply(rule lmeasure_unique)
   8.537 -  unfolding has_lmeasure_def by auto
   8.538 -
   8.539 -lemma lmeasurableI[dest]:"s has_lmeasure m \<Longrightarrow> lmeasurable s"
   8.540 -  unfolding has_lmeasure_def by auto
   8.541 -
   8.542 -lemma has_gmeasure_has_lmeasure: assumes "s has_gmeasure m"
   8.543 -  shows "s has_lmeasure (Real m)"
   8.544 -proof- have gmea:"gmeasurable s" using assms by auto
   8.545 +lemma has_gmeasure_lmeasure: assumes "s has_gmeasure m"
   8.546 +  shows "lmeasure s = Real m"
   8.547 +proof-
   8.548 +  have gmea:"gmeasurable s" using assms by auto
   8.549 +  then have s: "s \<in> sets lebesgue" by auto
   8.550    have m:"m \<ge> 0" using assms by auto
   8.551    have *:"m = gmeasure (\<Union>{s \<inter> cube n |n. n \<in> UNIV})" unfolding Union_inter_cube
   8.552      using assms by(rule measure_unique[THEN sym])
   8.553 -  show ?thesis unfolding has_lmeasure_def
   8.554 -    apply(rule,rule measurable_imp_lmeasurable[OF gmea])
   8.555 -    apply(subst lim_Real) apply(rule,rule,rule m) unfolding *
   8.556 +  show ?thesis
   8.557 +    unfolding lmeasure_iff_LIMSEQ[OF s `0 \<le> m`] unfolding *
   8.558      apply(rule gmeasurable_nested_unions[THEN conjunct2, where B1="gmeasure s"])
   8.559    proof- fix n::nat show *:"gmeasurable (s \<inter> cube n)"
   8.560        using gmeasurable_inter[OF gmea gmeasurable_cube] .
   8.561 @@ -266,287 +418,26 @@
   8.562        apply(rule * gmea)+ by auto
   8.563      show "s \<inter> cube n \<subseteq> s \<inter> cube (Suc n)" using cube_subset[of n "Suc n"] by auto
   8.564    qed
   8.565 -qed    
   8.566 -    
   8.567 -lemma gmeasure_lmeasure: assumes "gmeasurable s" shows "lmeasure s = Real (gmeasure s)"
   8.568 -proof- note has_gmeasure_measureI[OF assms]
   8.569 -  note has_gmeasure_has_lmeasure[OF this]
   8.570 -  thus ?thesis by(rule lmeasure_unique)
   8.571 -qed
   8.572 -
   8.573 -lemma has_lmeasure_lmeasure: "lmeasurable s \<longleftrightarrow> s has_lmeasure (lmeasure s)" (is "?l = ?r")
   8.574 -proof assume ?l let ?f = "\<lambda>n. Real (gmeasure (s \<inter> cube n))"
   8.575 -  have "\<forall>n m. n\<ge>m \<longrightarrow> ?f n \<ge> ?f m" unfolding pinfreal_less_eq apply safe
   8.576 -    apply(subst if_P) defer apply(rule measure_subset) prefer 3
   8.577 -    apply(drule cube_subset) using `?l` by auto
   8.578 -  from lim_pinfreal_increasing[OF this] guess l . note l=this
   8.579 -  hence "s has_lmeasure l" using `?l` apply-apply(rule has_lmeasureI) by auto
   8.580 -  thus ?r using lmeasure_unique by auto
   8.581 -next assume ?r thus ?l unfolding has_lmeasure_def by auto
   8.582 -qed
   8.583 -
   8.584 -lemma lmeasure_subset[dest]: assumes "lmeasurable s" "lmeasurable t" "s \<subseteq> t"
   8.585 -  shows "lmeasure s \<le> lmeasure t"
   8.586 -proof(cases "lmeasure t = \<omega>")
   8.587 -  case False have som:"lmeasure s \<noteq> \<omega>"
   8.588 -  proof(rule ccontr,unfold not_not) assume as:"lmeasure s = \<omega>"
   8.589 -    have "t has_lmeasure \<omega>" using assms(2) apply(rule has_lmeasureI)
   8.590 -      unfolding Lim_omega
   8.591 -    proof case goal1
   8.592 -      note assms(1)[unfolded has_lmeasure_lmeasure]
   8.593 -      note has_lmeasureD(3)[OF this,unfolded as Lim_omega,rule_format,of B]
   8.594 -      then guess N .. note N = this
   8.595 -      show ?case apply(rule_tac x=N in exI) apply safe
   8.596 -        apply(rule order_trans) apply(rule N[rule_format],assumption)
   8.597 -        unfolding pinfreal_less_eq apply(subst if_P)defer
   8.598 -        apply(rule measure_subset) using assms by auto
   8.599 -    qed
   8.600 -    thus False using lmeasure_unique False by auto
   8.601 -  qed
   8.602 -
   8.603 -  note assms(1)[unfolded has_lmeasure_lmeasure] note has_lmeasureD(3)[OF this]
   8.604 -  hence "(\<lambda>n. Real (gmeasure (s \<inter> cube n))) ----> Real (real (lmeasure s))"
   8.605 -    unfolding Real_real'[OF som] .
   8.606 -  hence l1:"(\<lambda>n. gmeasure (s \<inter> cube n)) ----> real (lmeasure s)"
   8.607 -    apply-apply(subst(asm) lim_Real) by auto
   8.608 -
   8.609 -  note assms(2)[unfolded has_lmeasure_lmeasure] note has_lmeasureD(3)[OF this]
   8.610 -  hence "(\<lambda>n. Real (gmeasure (t \<inter> cube n))) ----> Real (real (lmeasure t))"
   8.611 -    unfolding Real_real'[OF False] .
   8.612 -  hence l2:"(\<lambda>n. gmeasure (t \<inter> cube n)) ----> real (lmeasure t)"
   8.613 -    apply-apply(subst(asm) lim_Real) by auto
   8.614 -
   8.615 -  have "real (lmeasure s) \<le> real (lmeasure t)" apply(rule LIMSEQ_le[OF l1 l2])
   8.616 -    apply(rule_tac x=0 in exI,safe) apply(rule measure_subset) using assms by auto
   8.617 -  hence "Real (real (lmeasure s)) \<le> Real (real (lmeasure t))"
   8.618 -    unfolding pinfreal_less_eq by auto
   8.619 -  thus ?thesis unfolding Real_real'[OF som] Real_real'[OF False] .
   8.620 -qed auto
   8.621 -
   8.622 -lemma has_lmeasure_negligible_unions_image:
   8.623 -  assumes "finite s" "\<And>x. x \<in> s ==> lmeasurable(f x)"
   8.624 -  "\<And>x y. x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> x \<noteq> y \<Longrightarrow> negligible((f x) \<inter> (f y))"
   8.625 -  shows "(\<Union> (f ` s)) has_lmeasure (setsum (\<lambda>x. lmeasure(f x)) s)"
   8.626 -  unfolding has_lmeasure_def
   8.627 -proof show lmeaf:"lmeasurable (\<Union>f ` s)" apply(rule lmeasurable_finite_unions)
   8.628 -    using assms(1-2) by auto
   8.629 -  show "(\<lambda>n. Real (gmeasure (\<Union>f ` s \<inter> cube n))) ----> (\<Sum>x\<in>s. lmeasure (f x))" (is ?l)
   8.630 -  proof(cases "\<exists>x\<in>s. lmeasure (f x) = \<omega>")
   8.631 -    case False hence *:"(\<Sum>x\<in>s. lmeasure (f x)) \<noteq> \<omega>" apply-
   8.632 -      apply(rule setsum_neq_omega) using assms(1) by auto
   8.633 -    have gmea:"\<And>x. x\<in>s \<Longrightarrow> gmeasurable (f x)" apply(rule glmeasurable_finite) using False assms(2) by auto
   8.634 -    have "(\<Sum>x\<in>s. lmeasure (f x)) = (\<Sum>x\<in>s. Real (gmeasure (f x)))" apply(rule setsum_cong2)
   8.635 -      apply(rule gmeasure_lmeasure) using False assms(2) gmea by auto
   8.636 -    also have "... = Real (\<Sum>x\<in>s. (gmeasure (f x)))" apply(rule setsum_Real) by auto
   8.637 -    finally have sum:"(\<Sum>x\<in>s. lmeasure (f x)) = Real (\<Sum>x\<in>s. gmeasure (f x))" .
   8.638 -    have sum_0:"(\<Sum>x\<in>s. gmeasure (f x)) \<ge> 0" apply(rule setsum_nonneg) by auto
   8.639 -    have int_un:"\<Union>f ` s has_gmeasure (\<Sum>x\<in>s. gmeasure (f x))"
   8.640 -      apply(rule has_gmeasure_negligible_unions_image) using assms gmea by auto
   8.641 -
   8.642 -    have unun:"\<Union>{\<Union>f ` s \<inter> cube n |n. n \<in> UNIV} = \<Union>f ` s" unfolding simple_image 
   8.643 -    proof safe fix x y assume as:"x \<in> f y" "y \<in> s"
   8.644 -      from mem_big_cube[of x] guess n . note n=this
   8.645 -      thus "x \<in> \<Union>range (\<lambda>n. \<Union>f ` s \<inter> cube n)" unfolding Union_iff
   8.646 -        apply-apply(rule_tac x="\<Union>f ` s \<inter> cube n" in bexI) using as by auto
   8.647 -    qed
   8.648 -    show ?l apply(subst Real_real'[OF *,THEN sym])apply(subst lim_Real) 
   8.649 -      apply rule apply rule unfolding sum real_Real if_P[OF sum_0] apply(rule sum_0)
   8.650 -      unfolding measure_unique[OF int_un,THEN sym] apply(subst(2) unun[THEN sym])
   8.651 -      apply(rule has_gmeasure_nested_unions[THEN conjunct2])
   8.652 -    proof- fix n::nat
   8.653 -      show *:"gmeasurable (\<Union>f ` s \<inter> cube n)" using lmeaf unfolding lmeasurable_def by auto
   8.654 -      thus "gmeasure (\<Union>f ` s \<inter> cube n) \<le> gmeasure (\<Union>f ` s)"
   8.655 -        apply(rule measure_subset) using int_un by auto
   8.656 -      show "\<Union>f ` s \<inter> cube n \<subseteq> \<Union>f ` s \<inter> cube (Suc n)"
   8.657 -        using cube_subset[of n "Suc n"] by auto
   8.658 -    qed
   8.659 -
   8.660 -  next case True then guess X .. note X=this
   8.661 -    hence sum:"(\<Sum>x\<in>s. lmeasure (f x)) = \<omega>" using setsum_\<omega>[THEN iffD2, of s] assms by fastsimp
   8.662 -    show ?l unfolding sum Lim_omega
   8.663 -    proof fix B::real
   8.664 -      have Xm:"(f X) has_lmeasure \<omega>" using X by (metis assms(2) has_lmeasure_lmeasure)
   8.665 -      note this[unfolded has_lmeasure_def,THEN conjunct2, unfolded Lim_omega]
   8.666 -      from this[rule_format,of B] guess N .. note N=this[rule_format]
   8.667 -      show "\<exists>N. \<forall>n\<ge>N. Real B \<le> Real (gmeasure (\<Union>f ` s \<inter> cube n))"
   8.668 -        apply(rule_tac x=N in exI)
   8.669 -      proof safe case goal1 show ?case apply(rule order_trans[OF N[OF goal1]])
   8.670 -          unfolding pinfreal_less_eq apply(subst if_P) defer
   8.671 -          apply(rule measure_subset) using has_lmeasureD(2)[OF Xm]
   8.672 -          using lmeaf unfolding lmeasurable_def using X(1) by auto
   8.673 -      qed qed qed qed
   8.674 -
   8.675 -lemma has_lmeasure_negligible_unions:
   8.676 -  assumes"finite f" "\<And>s. s \<in> f ==> s has_lmeasure (m s)"
   8.677 -  "\<And>s t. s \<in> f \<Longrightarrow> t \<in> f \<Longrightarrow> s \<noteq> t ==> negligible (s\<inter>t)"
   8.678 -  shows "(\<Union> f) has_lmeasure (setsum m f)"
   8.679 -proof- have *:"setsum m f = setsum lmeasure f" apply(rule setsum_cong2)
   8.680 -    apply(subst lmeasure_unique[OF assms(2)]) by auto
   8.681 -  show ?thesis unfolding *
   8.682 -    apply(rule has_lmeasure_negligible_unions_image[where s=f and f=id,unfolded image_id id_apply])
   8.683 -    using assms by auto
   8.684 -qed
   8.685 -
   8.686 -lemma has_lmeasure_disjoint_unions:
   8.687 -  assumes"finite f" "\<And>s. s \<in> f ==> s has_lmeasure (m s)"
   8.688 -  "\<And>s t. s \<in> f \<Longrightarrow> t \<in> f \<Longrightarrow> s \<noteq> t ==> s \<inter> t = {}"
   8.689 -  shows "(\<Union> f) has_lmeasure (setsum m f)"
   8.690 -proof- have *:"setsum m f = setsum lmeasure f" apply(rule setsum_cong2)
   8.691 -    apply(subst lmeasure_unique[OF assms(2)]) by auto
   8.692 -  show ?thesis unfolding *
   8.693 -    apply(rule has_lmeasure_negligible_unions_image[where s=f and f=id,unfolded image_id id_apply])
   8.694 -    using assms by auto
   8.695  qed
   8.696  
   8.697 -lemma has_lmeasure_nested_unions:
   8.698 -  assumes "\<And>n. lmeasurable(s n)" "\<And>n. s(n) \<subseteq> s(Suc n)"
   8.699 -  shows "lmeasurable(\<Union> { s n | n. n \<in> UNIV }) \<and>
   8.700 -  (\<lambda>n. lmeasure(s n)) ----> lmeasure(\<Union> { s(n) | n. n \<in> UNIV })" (is "?mea \<and> ?lim")
   8.701 -proof- have cube:"\<And>m. \<Union> { s(n) | n. n \<in> UNIV } \<inter> cube m = \<Union> { s(n) \<inter> cube m | n. n \<in> UNIV }" by blast
   8.702 -  have 3:"\<And>n. \<forall>m\<ge>n. s n \<subseteq> s m" apply(rule transitive_stepwise_le) using assms(2) by auto
   8.703 -  have mea:"?mea" unfolding lmeasurable_def cube apply rule 
   8.704 -    apply(rule_tac B1="gmeasure (cube n)" in has_gmeasure_nested_unions[THEN conjunct1])
   8.705 -    prefer 3 apply rule using assms(1) unfolding lmeasurable_def
   8.706 -    by(auto intro!:assms(2)[unfolded subset_eq,rule_format])
   8.707 -  show ?thesis apply(rule,rule mea)
   8.708 -  proof(cases "lmeasure(\<Union> { s(n) | n. n \<in> UNIV }) = \<omega>")
   8.709 -    case True show ?lim unfolding True Lim_omega
   8.710 -    proof(rule ccontr) case goal1 note this[unfolded not_all not_ex]
   8.711 -      hence "\<exists>B. \<forall>n. \<exists>m\<ge>n. Real B > lmeasure (s m)" by(auto simp add:not_le)
   8.712 -      from this guess B .. note B=this[rule_format]
   8.713 -
   8.714 -      have "\<forall>n. gmeasurable (s n) \<and> gmeasure (s n) \<le> max B 0" 
   8.715 -      proof safe fix n::nat from B[of n] guess m .. note m=this
   8.716 -        hence *:"lmeasure (s n) < Real B" apply-apply(rule le_less_trans)
   8.717 -          apply(rule lmeasure_subset[OF assms(1,1)]) apply(rule 3[rule_format]) by auto
   8.718 -        thus **:"gmeasurable (s n)" apply-apply(rule glmeasurable_finite[OF assms(1)]) by auto
   8.719 -        thus "gmeasure (s n) \<le> max B 0"  using * unfolding gmeasure_lmeasure[OF **] Real_max'[of B]
   8.720 -          unfolding pinfreal_less apply- apply(subst(asm) if_P) by auto
   8.721 -      qed
   8.722 -      hence "\<And>n. gmeasurable (s n)" "\<And>n. gmeasure (s n) \<le> max B 0" by auto
   8.723 -      note g = conjunctD2[OF has_gmeasure_nested_unions[of s, OF this assms(2)]]
   8.724 -      show False using True unfolding gmeasure_lmeasure[OF g(1)] by auto
   8.725 -    qed
   8.726 -  next let ?B = "lmeasure (\<Union>{s n |n. n \<in> UNIV})"
   8.727 -    case False note gmea_lim = glmeasurable_finite[OF mea this]
   8.728 -    have ls:"\<And>n. lmeasure (s n) \<le> lmeasure (\<Union>{s n |n. n \<in> UNIV})"
   8.729 -      apply(rule lmeasure_subset) using assms(1) mea by auto
   8.730 -    have "\<And>n. lmeasure (s n) \<noteq> \<omega>"
   8.731 -    proof(rule ccontr,safe) case goal1
   8.732 -      show False using False ls[of n] unfolding goal1 by auto
   8.733 -    qed
   8.734 -    note gmea = glmeasurable_finite[OF assms(1) this]
   8.735 -
   8.736 -    have "\<And>n. gmeasure (s n) \<le> real ?B"  unfolding gmeasure_lmeasure[OF gmea_lim]
   8.737 -      unfolding real_Real apply(subst if_P,rule) apply(rule measure_subset)
   8.738 -      using gmea gmea_lim by auto
   8.739 -    note has_gmeasure_nested_unions[of s, OF gmea this assms(2)]
   8.740 -    thus ?lim unfolding gmeasure_lmeasure[OF gmea] gmeasure_lmeasure[OF gmea_lim]
   8.741 -      apply-apply(subst lim_Real) by auto
   8.742 -  qed
   8.743 -qed
   8.744 -
   8.745 -lemma has_lmeasure_countable_negligible_unions: 
   8.746 -  assumes "\<And>n. lmeasurable(s n)" "\<And>m n. m \<noteq> n \<Longrightarrow> negligible(s m \<inter> s n)"
   8.747 -  shows "(\<lambda>m. setsum (\<lambda>n. lmeasure(s n)) {..m}) ----> (lmeasure(\<Union> { s(n) |n. n \<in> UNIV }))"
   8.748 -proof- have *:"\<And>n. (\<Union> (s ` {0..n})) has_lmeasure (setsum (\<lambda>k. lmeasure(s k)) {0..n})"
   8.749 -    apply(rule has_lmeasure_negligible_unions_image) using assms by auto
   8.750 -  have **:"(\<Union>{\<Union>s ` {0..n} |n. n \<in> UNIV}) = (\<Union>{s n |n. n \<in> UNIV})" unfolding simple_image by fastsimp
   8.751 -  have "lmeasurable (\<Union>{\<Union>s ` {0..n} |n. n \<in> UNIV}) \<and>
   8.752 -    (\<lambda>n. lmeasure (\<Union>(s ` {0..n}))) ----> lmeasure (\<Union>{\<Union>s ` {0..n} |n. n \<in> UNIV})"
   8.753 -    apply(rule has_lmeasure_nested_unions) apply(rule has_lmeasureD(1)[OF *])
   8.754 -    apply(rule Union_mono,rule image_mono) by auto
   8.755 -  note lem = conjunctD2[OF this,unfolded **] 
   8.756 -  show ?thesis using lem(2) unfolding lmeasure_unique[OF *] unfolding atLeast0AtMost .
   8.757 -qed
   8.758 -
   8.759 -lemma lmeasure_eq_0: assumes "negligible s" shows "lmeasure s = 0"
   8.760 -proof- note mea=negligible_imp_lmeasurable[OF assms]
   8.761 -  have *:"\<And>n. (gmeasure (s \<inter> cube n)) = 0" 
   8.762 -    unfolding gmeasurable_measure_eq_0[OF mea[unfolded lmeasurable_def,rule_format]]
   8.763 -    using assms by auto
   8.764 -  show ?thesis
   8.765 -    apply(rule lmeasure_unique) unfolding has_lmeasure_def
   8.766 -    apply(rule,rule mea) unfolding * by auto
   8.767 +lemma has_gmeasure_iff_lmeasure:
   8.768 +  "A has_gmeasure m \<longleftrightarrow> (A \<in> sets lebesgue \<and> 0 \<le> m \<and> lmeasure A = Real m)"
   8.769 +proof
   8.770 +  assume "A has_gmeasure m"
   8.771 +  with has_gmeasure_lmeasure[OF this]
   8.772 +  have "gmeasurable A" "0 \<le> m" "lmeasure A = Real m" by auto
   8.773 +  then show "A \<in> sets lebesgue \<and> 0 \<le> m \<and> lmeasure A = Real m" by auto
   8.774 +next
   8.775 +  assume "A \<in> sets lebesgue \<and> 0 \<le> m \<and> lmeasure A = Real m"
   8.776 +  then show "A has_gmeasure m" by (intro lmeasure_finite_has_gmeasure) auto
   8.777  qed
   8.778  
   8.779 -lemma negligible_img_gmeasurable: fixes s::"'a::ordered_euclidean_space set"
   8.780 -  assumes "negligible s" shows "gmeasurable s"
   8.781 -  apply(rule glmeasurable_finite)
   8.782 -  using lmeasure_eq_0[OF assms] negligible_imp_lmeasurable[OF assms] by auto
   8.783 -
   8.784 -
   8.785 -
   8.786 -
   8.787 -section {* Instantiation of _::euclidean_space as measure_space *}
   8.788 -
   8.789 -definition lebesgue_space :: "'a::ordered_euclidean_space algebra" where
   8.790 -  "lebesgue_space = \<lparr> space = UNIV, sets = lmeasurable \<rparr>"
   8.791 -
   8.792 -lemma lebesgue_measurable[simp]:"A \<in> sets lebesgue_space \<longleftrightarrow> lmeasurable A"
   8.793 -  unfolding lebesgue_space_def by(auto simp: mem_def)
   8.794 -
   8.795 -lemma mem_gmeasurable[simp]: "A \<in> gmeasurable \<longleftrightarrow> gmeasurable A"
   8.796 -  unfolding mem_def ..
   8.797 -
   8.798 -interpretation lebesgue: measure_space lebesgue_space lmeasure
   8.799 -  apply(intro_locales) unfolding measure_space_axioms_def countably_additive_def
   8.800 -  unfolding sigma_algebra_axioms_def algebra_def
   8.801 -  unfolding lebesgue_measurable
   8.802 -proof safe
   8.803 -  fix A::"nat => _" assume as:"range A \<subseteq> sets lebesgue_space" "disjoint_family A"
   8.804 -    "lmeasurable (UNION UNIV A)"
   8.805 -  have *:"UNION UNIV A = \<Union>range A" by auto
   8.806 -  show "(\<Sum>\<^isub>\<infinity>n. lmeasure (A n)) = lmeasure (UNION UNIV A)" 
   8.807 -    unfolding psuminf_def apply(rule SUP_Lim_pinfreal)
   8.808 -  proof- fix n m::nat assume mn:"m\<le>n"
   8.809 -    have *:"\<And>m. (\<Sum>n<m. lmeasure (A n)) = lmeasure (\<Union>A ` {..<m})"
   8.810 -      apply(subst lmeasure_unique[OF has_lmeasure_negligible_unions[where m=lmeasure]])
   8.811 -      apply(rule finite_imageI) apply rule apply(subst has_lmeasure_lmeasure[THEN sym])
   8.812 -    proof- fix m::nat
   8.813 -      show "(\<Sum>n<m. lmeasure (A n)) = setsum lmeasure (A ` {..<m})"
   8.814 -        apply(subst setsum_reindex_nonzero) unfolding o_def apply rule
   8.815 -        apply(rule lmeasure_eq_0) using as(2) unfolding disjoint_family_on_def
   8.816 -        apply(erule_tac x=x in ballE,safe,erule_tac x=y in ballE) by auto
   8.817 -    next fix m s assume "s \<in> A ` {..<m}"
   8.818 -      hence "s \<in> range A" by auto thus "lmeasurable s" using as(1) by fastsimp
   8.819 -    next fix m s t assume st:"s  \<in> A ` {..<m}" "t \<in> A ` {..<m}" "s \<noteq> t"
   8.820 -      from st(1-2) guess sa ta unfolding image_iff apply-by(erule bexE)+ note a=this
   8.821 -      from st(3) have "sa \<noteq> ta" unfolding a by auto
   8.822 -      thus "negligible (s \<inter> t)" 
   8.823 -        using as(2) unfolding disjoint_family_on_def a
   8.824 -        apply(erule_tac x=sa in ballE,erule_tac x=ta in ballE) by auto
   8.825 -    qed
   8.826 -
   8.827 -    have "\<And>m. lmeasurable (\<Union>A ` {..<m})"  apply(rule lmeasurable_finite_unions)
   8.828 -      apply(rule finite_imageI,rule) using as(1) by fastsimp
   8.829 -    from this this show "(\<Sum>n<m. lmeasure (A n)) \<le> (\<Sum>n<n. lmeasure (A n))" unfolding *
   8.830 -      apply(rule lmeasure_subset) apply(rule Union_mono) apply(rule image_mono) using mn by auto
   8.831 -    
   8.832 -  next have *:"UNION UNIV A = \<Union>{A n |n. n \<in> UNIV}" by auto
   8.833 -    show "(\<lambda>n. \<Sum>n<n. lmeasure (A n)) ----> lmeasure (UNION UNIV A)"
   8.834 -      apply(rule LIMSEQ_imp_Suc) unfolding lessThan_Suc_atMost *
   8.835 -      apply(rule has_lmeasure_countable_negligible_unions)
   8.836 -      using as unfolding disjoint_family_on_def subset_eq by auto
   8.837 -  qed
   8.838 -
   8.839 -next show "lmeasure {} = 0" by auto
   8.840 -next fix A::"nat => _" assume as:"range A \<subseteq> sets lebesgue_space"
   8.841 -  have *:"UNION UNIV A = (\<Union>{A n |n. n \<in> UNIV})" unfolding simple_image by auto
   8.842 -  show "lmeasurable (UNION UNIV A)" unfolding * using as unfolding subset_eq
   8.843 -    using lmeasurable_countable_unions_strong[of A] by auto
   8.844 -qed(auto simp: lebesgue_space_def mem_def)
   8.845 -
   8.846 -
   8.847 -
   8.848 -lemma lmeasurbale_closed_interval[intro]:
   8.849 -  "lmeasurable {a..b::'a::ordered_euclidean_space}"
   8.850 -  unfolding lmeasurable_def cube_def inter_interval by auto
   8.851 -
   8.852 -lemma space_lebesgue_space[simp]:"space lebesgue_space = UNIV"
   8.853 -  unfolding lebesgue_space_def by auto
   8.854 -
   8.855 -abbreviation "gintegral \<equiv> Integration.integral"
   8.856 +lemma gmeasure_lmeasure: assumes "gmeasurable s" shows "lmeasure s = Real (gmeasure s)"
   8.857 +proof -
   8.858 +  note has_gmeasure_measureI[OF assms]
   8.859 +  note has_gmeasure_lmeasure[OF this]
   8.860 +  thus ?thesis .
   8.861 +qed
   8.862  
   8.863  lemma lebesgue_simple_function_indicator:
   8.864    fixes f::"'a::ordered_euclidean_space \<Rightarrow> pinfreal"
   8.865 @@ -556,21 +447,614 @@
   8.866  
   8.867  lemma lmeasure_gmeasure:
   8.868    "gmeasurable s \<Longrightarrow> gmeasure s = real (lmeasure s)"
   8.869 -  apply(subst gmeasure_lmeasure) by auto
   8.870 +  by (subst gmeasure_lmeasure) auto
   8.871  
   8.872  lemma lmeasure_finite: assumes "gmeasurable s" shows "lmeasure s \<noteq> \<omega>"
   8.873    using gmeasure_lmeasure[OF assms] by auto
   8.874  
   8.875 -lemma negligible_lmeasure: assumes "lmeasurable s"
   8.876 -  shows "lmeasure s = 0 \<longleftrightarrow> negligible s" (is "?l = ?r")
   8.877 -proof assume ?l
   8.878 -  hence *:"gmeasurable s" using glmeasurable_finite[of s] assms by auto
   8.879 -  show ?r unfolding gmeasurable_measure_eq_0[THEN sym,OF *]
   8.880 -    unfolding lmeasure_gmeasure[OF *] using `?l` by auto
   8.881 -next assume ?r
   8.882 -  note g=negligible_img_gmeasurable[OF this] and measure_eq_0[OF this]
   8.883 -  hence "real (lmeasure s) = 0" using lmeasure_gmeasure[of s] by auto
   8.884 -  thus ?l using lmeasure_finite[OF g] apply- apply(rule real_0_imp_eq_0) by auto
   8.885 +lemma negligible_iff_lebesgue_null_sets:
   8.886 +  "negligible A \<longleftrightarrow> A \<in> lebesgue.null_sets"
   8.887 +proof
   8.888 +  assume "negligible A"
   8.889 +  from this[THEN lebesgueI_negligible] this[THEN lmeasure_eq_0]
   8.890 +  show "A \<in> lebesgue.null_sets" by auto
   8.891 +next
   8.892 +  assume A: "A \<in> lebesgue.null_sets"
   8.893 +  then have *:"gmeasurable A" using lmeasure_finite_gmeasurable[of A] by auto
   8.894 +  show "negligible A"
   8.895 +    unfolding gmeasurable_measure_eq_0[OF *, symmetric]
   8.896 +    unfolding lmeasure_gmeasure[OF *] using A by auto
   8.897 +qed
   8.898 +
   8.899 +lemma
   8.900 +  fixes a b ::"'a::ordered_euclidean_space"
   8.901 +  shows lmeasure_atLeastAtMost[simp]: "lmeasure {a..b} = Real (content {a..b})"
   8.902 +    and lmeasure_greaterThanLessThan[simp]: "lmeasure {a <..< b} = Real (content {a..b})"
   8.903 +  using has_gmeasure_interval[of a b] by (auto intro!: has_gmeasure_lmeasure)
   8.904 +
   8.905 +lemma lmeasure_cube:
   8.906 +  "lmeasure (cube n::('a::ordered_euclidean_space) set) = (Real ((2 * real n) ^ (DIM('a))))"
   8.907 +  by (intro has_gmeasure_lmeasure) auto
   8.908 +
   8.909 +lemma lmeasure_UNIV[intro]: "lmeasure UNIV = \<omega>"
   8.910 +  unfolding lmeasure_def SUP_\<omega>
   8.911 +proof (intro allI impI)
   8.912 +  fix x assume "x < \<omega>"
   8.913 +  then obtain r where r: "x = Real r" "0 \<le> r" by (cases x) auto
   8.914 +  then obtain n where n: "r < of_nat n" using ex_less_of_nat by auto
   8.915 +  show "\<exists>i\<in>UNIV. x < Real (gmeasure (UNIV \<inter> cube i))"
   8.916 +  proof (intro bexI[of _ n])
   8.917 +    have "x < Real (of_nat n)" using n r by auto
   8.918 +    also have "Real (of_nat n) \<le> Real (gmeasure (UNIV \<inter> cube n))"
   8.919 +      using gmeasure_cube_ge_n[of n] by (auto simp: real_eq_of_nat[symmetric])
   8.920 +    finally show "x < Real (gmeasure (UNIV \<inter> cube n))" .
   8.921 +  qed auto
   8.922 +qed
   8.923 +
   8.924 +lemma atLeastAtMost_singleton_euclidean[simp]:
   8.925 +  fixes a :: "'a::ordered_euclidean_space" shows "{a .. a} = {a}"
   8.926 +  by (force simp: eucl_le[where 'a='a] euclidean_eq[where 'a='a])
   8.927 +
   8.928 +lemma content_singleton[simp]: "content {a} = 0"
   8.929 +proof -
   8.930 +  have "content {a .. a} = 0"
   8.931 +    by (subst content_closed_interval) auto
   8.932 +  then show ?thesis by simp
   8.933 +qed
   8.934 +
   8.935 +lemma lmeasure_singleton[simp]:
   8.936 +  fixes a :: "'a::ordered_euclidean_space" shows "lmeasure {a} = 0"
   8.937 +  using has_gmeasure_interval[of a a] unfolding zero_pinfreal_def
   8.938 +  by (intro has_gmeasure_lmeasure)
   8.939 +     (simp add: content_closed_interval DIM_positive)
   8.940 +
   8.941 +declare content_real[simp]
   8.942 +
   8.943 +lemma
   8.944 +  fixes a b :: real
   8.945 +  shows lmeasure_real_greaterThanAtMost[simp]:
   8.946 +    "lmeasure {a <.. b} = Real (if a \<le> b then b - a else 0)"
   8.947 +proof cases
   8.948 +  assume "a < b"
   8.949 +  then have "lmeasure {a <.. b} = lmeasure {a <..< b} + lmeasure {b}"
   8.950 +    by (subst lebesgue.measure_additive)
   8.951 +       (auto intro!: lebesgueI_borel arg_cong[where f=lmeasure])
   8.952 +  then show ?thesis by auto
   8.953 +qed auto
   8.954 +
   8.955 +lemma
   8.956 +  fixes a b :: real
   8.957 +  shows lmeasure_real_atLeastLessThan[simp]:
   8.958 +    "lmeasure {a ..< b} = Real (if a \<le> b then b - a else 0)" (is ?eqlt)
   8.959 +proof cases
   8.960 +  assume "a < b"
   8.961 +  then have "lmeasure {a ..< b} = lmeasure {a} + lmeasure {a <..< b}"
   8.962 +    by (subst lebesgue.measure_additive)
   8.963 +       (auto intro!: lebesgueI_borel arg_cong[where f=lmeasure])
   8.964 +  then show ?thesis by auto
   8.965 +qed auto
   8.966 +
   8.967 +interpretation borel: measure_space borel lmeasure
   8.968 +proof
   8.969 +  show "countably_additive borel lmeasure"
   8.970 +    using lebesgue.ca unfolding countably_additive_def
   8.971 +    apply safe apply (erule_tac x=A in allE) by auto
   8.972 +qed auto
   8.973 +
   8.974 +interpretation borel: sigma_finite_measure borel lmeasure
   8.975 +proof (default, intro conjI exI[of _ "\<lambda>n. cube n"])
   8.976 +  show "range cube \<subseteq> sets borel" by (auto intro: borel_closed)
   8.977 +  { fix x have "\<exists>n. x\<in>cube n" using mem_big_cube by auto }
   8.978 +  thus "(\<Union>i. cube i) = space borel" by auto
   8.979 +  show "\<forall>i. lmeasure (cube i) \<noteq> \<omega>" unfolding lmeasure_cube by auto
   8.980 +qed
   8.981 +
   8.982 +interpretation lebesgue: sigma_finite_measure lebesgue lmeasure
   8.983 +proof
   8.984 +  from borel.sigma_finite guess A ..
   8.985 +  moreover then have "range A \<subseteq> sets lebesgue" using lebesgueI_borel by blast
   8.986 +  ultimately show "\<exists>A::nat \<Rightarrow> 'b set. range A \<subseteq> sets lebesgue \<and> (\<Union>i. A i) = space lebesgue \<and> (\<forall>i. lmeasure (A i) \<noteq> \<omega>)"
   8.987 +    by auto
   8.988 +qed
   8.989 +
   8.990 +lemma simple_function_has_integral:
   8.991 +  fixes f::"'a::ordered_euclidean_space \<Rightarrow> pinfreal"
   8.992 +  assumes f:"lebesgue.simple_function f"
   8.993 +  and f':"\<forall>x. f x \<noteq> \<omega>"
   8.994 +  and om:"\<forall>x\<in>range f. lmeasure (f -` {x} \<inter> UNIV) = \<omega> \<longrightarrow> x = 0"
   8.995 +  shows "((\<lambda>x. real (f x)) has_integral (real (lebesgue.simple_integral f))) UNIV"
   8.996 +  unfolding lebesgue.simple_integral_def
   8.997 +  apply(subst lebesgue_simple_function_indicator[OF f])
   8.998 +proof- case goal1
   8.999 +  have *:"\<And>x. \<forall>y\<in>range f. y * indicator (f -` {y}) x \<noteq> \<omega>"
  8.1000 +    "\<forall>x\<in>range f. x * lmeasure (f -` {x} \<inter> UNIV) \<noteq> \<omega>"
  8.1001 +    using f' om unfolding indicator_def by auto
  8.1002 +  show ?case unfolding space_lebesgue real_of_pinfreal_setsum'[OF *(1),THEN sym]
  8.1003 +    unfolding real_of_pinfreal_setsum'[OF *(2),THEN sym]
  8.1004 +    unfolding real_of_pinfreal_setsum space_lebesgue
  8.1005 +    apply(rule has_integral_setsum)
  8.1006 +  proof safe show "finite (range f)" using f by (auto dest: lebesgue.simple_functionD)
  8.1007 +    fix y::'a show "((\<lambda>x. real (f y * indicator (f -` {f y}) x)) has_integral
  8.1008 +      real (f y * lmeasure (f -` {f y} \<inter> UNIV))) UNIV"
  8.1009 +    proof(cases "f y = 0") case False
  8.1010 +      have mea:"gmeasurable (f -` {f y})" apply(rule lmeasure_finite_gmeasurable)
  8.1011 +        using assms unfolding lebesgue.simple_function_def using False by auto
  8.1012 +      have *:"\<And>x. real (indicator (f -` {f y}) x::pinfreal) = (if x \<in> f -` {f y} then 1 else 0)" by auto
  8.1013 +      show ?thesis unfolding real_of_pinfreal_mult[THEN sym]
  8.1014 +        apply(rule has_integral_cmul[where 'b=real, unfolded real_scaleR_def])
  8.1015 +        unfolding Int_UNIV_right lmeasure_gmeasure[OF mea,THEN sym]
  8.1016 +        unfolding measure_integral_univ[OF mea] * apply(rule integrable_integral)
  8.1017 +        unfolding gmeasurable_integrable[THEN sym] using mea .
  8.1018 +    qed auto
  8.1019 +  qed qed
  8.1020 +
  8.1021 +lemma bounded_realI: assumes "\<forall>x\<in>s. abs (x::real) \<le> B" shows "bounded s"
  8.1022 +  unfolding bounded_def dist_real_def apply(rule_tac x=0 in exI)
  8.1023 +  using assms by auto
  8.1024 +
  8.1025 +lemma simple_function_has_integral':
  8.1026 +  fixes f::"'a::ordered_euclidean_space \<Rightarrow> pinfreal"
  8.1027 +  assumes f:"lebesgue.simple_function f"
  8.1028 +  and i: "lebesgue.simple_integral f \<noteq> \<omega>"
  8.1029 +  shows "((\<lambda>x. real (f x)) has_integral (real (lebesgue.simple_integral f))) UNIV"
  8.1030 +proof- let ?f = "\<lambda>x. if f x = \<omega> then 0 else f x"
  8.1031 +  { fix x have "real (f x) = real (?f x)" by (cases "f x") auto } note * = this
  8.1032 +  have **:"{x. f x \<noteq> ?f x} = f -` {\<omega>}" by auto
  8.1033 +  have **:"lmeasure {x\<in>space lebesgue. f x \<noteq> ?f x} = 0"
  8.1034 +    using lebesgue.simple_integral_omega[OF assms] by(auto simp add:**)
  8.1035 +  show ?thesis apply(subst lebesgue.simple_integral_cong'[OF f _ **])
  8.1036 +    apply(rule lebesgue.simple_function_compose1[OF f])
  8.1037 +    unfolding * defer apply(rule simple_function_has_integral)
  8.1038 +  proof-
  8.1039 +    show "lebesgue.simple_function ?f"
  8.1040 +      using lebesgue.simple_function_compose1[OF f] .
  8.1041 +    show "\<forall>x. ?f x \<noteq> \<omega>" by auto
  8.1042 +    show "\<forall>x\<in>range ?f. lmeasure (?f -` {x} \<inter> UNIV) = \<omega> \<longrightarrow> x = 0"
  8.1043 +    proof (safe, simp, safe, rule ccontr)
  8.1044 +      fix y assume "f y \<noteq> \<omega>" "f y \<noteq> 0"
  8.1045 +      hence "(\<lambda>x. if f x = \<omega> then 0 else f x) -` {if f y = \<omega> then 0 else f y} = f -` {f y}"
  8.1046 +        by (auto split: split_if_asm)
  8.1047 +      moreover assume "lmeasure ((\<lambda>x. if f x = \<omega> then 0 else f x) -` {if f y = \<omega> then 0 else f y}) = \<omega>"
  8.1048 +      ultimately have "lmeasure (f -` {f y}) = \<omega>" by simp
  8.1049 +      moreover
  8.1050 +      have "f y * lmeasure (f -` {f y}) \<noteq> \<omega>" using i f
  8.1051 +        unfolding lebesgue.simple_integral_def setsum_\<omega> lebesgue.simple_function_def
  8.1052 +        by auto
  8.1053 +      ultimately have "f y = 0" by (auto split: split_if_asm)
  8.1054 +      then show False using `f y \<noteq> 0` by simp
  8.1055 +    qed
  8.1056 +  qed
  8.1057 +qed
  8.1058 +
  8.1059 +lemma (in measure_space) positive_integral_monotone_convergence:
  8.1060 +  fixes f::"nat \<Rightarrow> 'a \<Rightarrow> pinfreal"
  8.1061 +  assumes i: "\<And>i. f i \<in> borel_measurable M" and mono: "\<And>x. mono (\<lambda>n. f n x)"
  8.1062 +  and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  8.1063 +  shows "u \<in> borel_measurable M"
  8.1064 +  and "(\<lambda>i. positive_integral (f i)) ----> positive_integral u" (is ?ilim)
  8.1065 +proof -
  8.1066 +  from positive_integral_isoton[unfolded isoton_fun_expand isoton_iff_Lim_mono, of f u]
  8.1067 +  show ?ilim using mono lim i by auto
  8.1068 +  have "(SUP i. f i) = u" using mono lim SUP_Lim_pinfreal
  8.1069 +    unfolding fun_eq_iff SUPR_fun_expand mono_def by auto
  8.1070 +  moreover have "(SUP i. f i) \<in> borel_measurable M"
  8.1071 +    using i by (rule borel_measurable_SUP)
  8.1072 +  ultimately show "u \<in> borel_measurable M" by simp
  8.1073 +qed
  8.1074 +
  8.1075 +lemma positive_integral_has_integral:
  8.1076 +  fixes f::"'a::ordered_euclidean_space => pinfreal"
  8.1077 +  assumes f:"f \<in> borel_measurable lebesgue"
  8.1078 +  and int_om:"lebesgue.positive_integral f \<noteq> \<omega>"
  8.1079 +  and f_om:"\<forall>x. f x \<noteq> \<omega>" (* TODO: remove this *)
  8.1080 +  shows "((\<lambda>x. real (f x)) has_integral (real (lebesgue.positive_integral f))) UNIV"
  8.1081 +proof- let ?i = "lebesgue.positive_integral f"
  8.1082 +  from lebesgue.borel_measurable_implies_simple_function_sequence[OF f]
  8.1083 +  guess u .. note conjunctD2[OF this,rule_format] note u = conjunctD2[OF this(1)] this(2)
  8.1084 +  let ?u = "\<lambda>i x. real (u i x)" and ?f = "\<lambda>x. real (f x)"
  8.1085 +  have u_simple:"\<And>k. lebesgue.simple_integral (u k) = lebesgue.positive_integral (u k)"
  8.1086 +    apply(subst lebesgue.positive_integral_eq_simple_integral[THEN sym,OF u(1)]) ..
  8.1087 +  have int_u_le:"\<And>k. lebesgue.simple_integral (u k) \<le> lebesgue.positive_integral f"
  8.1088 +    unfolding u_simple apply(rule lebesgue.positive_integral_mono)
  8.1089 +    using isoton_Sup[OF u(3)] unfolding le_fun_def by auto
  8.1090 +  have u_int_om:"\<And>i. lebesgue.simple_integral (u i) \<noteq> \<omega>"
  8.1091 +  proof- case goal1 thus ?case using int_u_le[of i] int_om by auto qed
  8.1092 +
  8.1093 +  note u_int = simple_function_has_integral'[OF u(1) this]
  8.1094 +  have "(\<lambda>x. real (f x)) integrable_on UNIV \<and>
  8.1095 +    (\<lambda>k. Integration.integral UNIV (\<lambda>x. real (u k x))) ----> Integration.integral UNIV (\<lambda>x. real (f x))"
  8.1096 +    apply(rule monotone_convergence_increasing) apply(rule,rule,rule u_int)
  8.1097 +  proof safe case goal1 show ?case apply(rule real_of_pinfreal_mono) using u(2,3) by auto
  8.1098 +  next case goal2 show ?case using u(3) apply(subst lim_Real[THEN sym])
  8.1099 +      prefer 3 apply(subst Real_real') defer apply(subst Real_real')
  8.1100 +      using isotone_Lim[OF u(3)[unfolded isoton_fun_expand, THEN spec]] using f_om u by auto
  8.1101 +  next case goal3
  8.1102 +    show ?case apply(rule bounded_realI[where B="real (lebesgue.positive_integral f)"])
  8.1103 +      apply safe apply(subst abs_of_nonneg) apply(rule integral_nonneg,rule) apply(rule u_int)
  8.1104 +      unfolding integral_unique[OF u_int] defer apply(rule real_of_pinfreal_mono[OF _ int_u_le])
  8.1105 +      using u int_om by auto
  8.1106 +  qed note int = conjunctD2[OF this]
  8.1107 +
  8.1108 +  have "(\<lambda>i. lebesgue.simple_integral (u i)) ----> ?i" unfolding u_simple
  8.1109 +    apply(rule lebesgue.positive_integral_monotone_convergence(2))
  8.1110 +    apply(rule lebesgue.borel_measurable_simple_function[OF u(1)])
  8.1111 +    using isotone_Lim[OF u(3)[unfolded isoton_fun_expand, THEN spec]] by auto
  8.1112 +  hence "(\<lambda>i. real (lebesgue.simple_integral (u i))) ----> real ?i" apply-
  8.1113 +    apply(subst lim_Real[THEN sym]) prefer 3
  8.1114 +    apply(subst Real_real') defer apply(subst Real_real')
  8.1115 +    using u f_om int_om u_int_om by auto
  8.1116 +  note * = LIMSEQ_unique[OF this int(2)[unfolded integral_unique[OF u_int]]]
  8.1117 +  show ?thesis unfolding * by(rule integrable_integral[OF int(1)])
  8.1118 +qed
  8.1119 +
  8.1120 +lemma lebesgue_integral_has_integral:
  8.1121 +  fixes f::"'a::ordered_euclidean_space => real"
  8.1122 +  assumes f:"lebesgue.integrable f"
  8.1123 +  shows "(f has_integral (lebesgue.integral f)) UNIV"
  8.1124 +proof- let ?n = "\<lambda>x. - min (f x) 0" and ?p = "\<lambda>x. max (f x) 0"
  8.1125 +  have *:"f = (\<lambda>x. ?p x - ?n x)" apply rule by auto
  8.1126 +  note f = lebesgue.integrableD[OF f]
  8.1127 +  show ?thesis unfolding lebesgue.integral_def apply(subst *)
  8.1128 +  proof(rule has_integral_sub) case goal1
  8.1129 +    have *:"\<forall>x. Real (f x) \<noteq> \<omega>" by auto
  8.1130 +    note lebesgue.borel_measurable_Real[OF f(1)]
  8.1131 +    from positive_integral_has_integral[OF this f(2) *]
  8.1132 +    show ?case unfolding real_Real_max .
  8.1133 +  next case goal2
  8.1134 +    have *:"\<forall>x. Real (- f x) \<noteq> \<omega>" by auto
  8.1135 +    note lebesgue.borel_measurable_uminus[OF f(1)]
  8.1136 +    note lebesgue.borel_measurable_Real[OF this]
  8.1137 +    from positive_integral_has_integral[OF this f(3) *]
  8.1138 +    show ?case unfolding real_Real_max minus_min_eq_max by auto
  8.1139 +  qed
  8.1140 +qed
  8.1141 +
  8.1142 +lemma continuous_on_imp_borel_measurable:
  8.1143 +  fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::ordered_euclidean_space"
  8.1144 +  assumes "continuous_on UNIV f"
  8.1145 +  shows "f \<in> borel_measurable lebesgue"
  8.1146 +  apply(rule lebesgue.borel_measurableI)
  8.1147 +  using continuous_open_preimage[OF assms] unfolding vimage_def by auto
  8.1148 +
  8.1149 +lemma (in measure_space) integral_monotone_convergence_pos':
  8.1150 +  assumes i: "\<And>i. integrable (f i)" and mono: "\<And>x. mono (\<lambda>n. f n x)"
  8.1151 +  and pos: "\<And>x i. 0 \<le> f i x"
  8.1152 +  and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
  8.1153 +  and ilim: "(\<lambda>i. integral (f i)) ----> x"
  8.1154 +  shows "integrable u \<and> integral u = x"
  8.1155 +  using integral_monotone_convergence_pos[OF assms] by auto
  8.1156 +
  8.1157 +definition e2p :: "'a::ordered_euclidean_space \<Rightarrow> (nat \<Rightarrow> real)" where
  8.1158 +  "e2p x = (\<lambda>i\<in>{..<DIM('a)}. x$$i)"
  8.1159 +
  8.1160 +definition p2e :: "(nat \<Rightarrow> real) \<Rightarrow> 'a::ordered_euclidean_space" where
  8.1161 +  "p2e x = (\<chi>\<chi> i. x i)"
  8.1162 +
  8.1163 +lemma bij_euclidean_component:
  8.1164 +  "bij_betw (e2p::'a::ordered_euclidean_space \<Rightarrow> _) (UNIV :: 'a set)
  8.1165 +  ({..<DIM('a)} \<rightarrow>\<^isub>E (UNIV :: real set))"
  8.1166 +  unfolding bij_betw_def e2p_def_raw
  8.1167 +proof let ?e = "\<lambda>x.\<lambda>i\<in>{..<DIM('a::ordered_euclidean_space)}. (x::'a)$$i"
  8.1168 +  show "inj ?e" unfolding inj_on_def restrict_def apply(subst euclidean_eq) apply safe
  8.1169 +    apply(drule_tac x=i in fun_cong) by auto
  8.1170 +  { fix x::"nat \<Rightarrow> real" assume x:"\<forall>i. i \<notin> {..<DIM('a)} \<longrightarrow> x i = undefined"
  8.1171 +    hence "x = ?e (\<chi>\<chi> i. x i)" apply-apply(rule,case_tac "xa<DIM('a)") by auto
  8.1172 +    hence "x \<in> range ?e" by fastsimp
  8.1173 +  } thus "range ?e = ({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)}"
  8.1174 +    unfolding extensional_def using DIM_positive by auto
  8.1175 +qed
  8.1176 +
  8.1177 +lemma bij_p2e:
  8.1178 +  "bij_betw (p2e::_ \<Rightarrow> 'a::ordered_euclidean_space) ({..<DIM('a)} \<rightarrow>\<^isub>E (UNIV :: real set))
  8.1179 +  (UNIV :: 'a set)" (is "bij_betw ?p ?U _")
  8.1180 +  unfolding bij_betw_def
  8.1181 +proof show "inj_on ?p ?U" unfolding inj_on_def p2e_def
  8.1182 +    apply(subst euclidean_eq) apply(safe,rule) unfolding extensional_def
  8.1183 +    apply(case_tac "xa<DIM('a)") by auto
  8.1184 +  { fix x::'a have "x \<in> ?p ` extensional {..<DIM('a)}"
  8.1185 +      unfolding image_iff apply(rule_tac x="\<lambda>i. if i<DIM('a) then x$$i else undefined" in bexI)
  8.1186 +      apply(subst euclidean_eq,safe) unfolding p2e_def extensional_def by auto
  8.1187 +  } thus "?p ` ?U = UNIV" by auto
  8.1188 +qed
  8.1189 +
  8.1190 +lemma e2p_p2e[simp]: fixes z::"'a::ordered_euclidean_space"
  8.1191 +  assumes "x \<in> extensional {..<DIM('a)}"
  8.1192 +  shows "e2p (p2e x::'a) = x"
  8.1193 +proof fix i::nat
  8.1194 +  show "e2p (p2e x::'a) i = x i" unfolding e2p_def p2e_def restrict_def
  8.1195 +    using assms unfolding extensional_def by auto
  8.1196 +qed
  8.1197 +
  8.1198 +lemma p2e_e2p[simp]: fixes x::"'a::ordered_euclidean_space"
  8.1199 +  shows "p2e (e2p x) = x"
  8.1200 +  apply(subst euclidean_eq) unfolding e2p_def p2e_def restrict_def by auto
  8.1201 +
  8.1202 +interpretation borel_product: product_sigma_finite "\<lambda>x. borel::real algebra" "\<lambda>x. lmeasure"
  8.1203 +  by default
  8.1204 +
  8.1205 +lemma cube_subset_Suc[intro]: "cube n \<subseteq> cube (Suc n)"
  8.1206 +  unfolding cube_def_raw subset_eq apply safe unfolding mem_interval by auto
  8.1207 +
  8.1208 +lemma borel_vimage_algebra_eq:
  8.1209 +  "sigma_algebra.vimage_algebra
  8.1210 +         (borel :: ('a::ordered_euclidean_space) algebra) ({..<DIM('a)} \<rightarrow>\<^isub>E UNIV) p2e =
  8.1211 +  sigma (product_algebra (\<lambda>x. \<lparr> space = UNIV::real set, sets = range (\<lambda>a. {..<a}) \<rparr>) {..<DIM('a)} )"
  8.1212 +proof- note bor = borel_eq_lessThan
  8.1213 +  def F \<equiv> "product_algebra (\<lambda>x. \<lparr> space = UNIV::real set, sets = range (\<lambda>a. {..<a}) \<rparr>) {..<DIM('a)}"
  8.1214 +  def E \<equiv> "\<lparr>space = (UNIV::'a set), sets = range lessThan\<rparr>"
  8.1215 +  have *:"(({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)}) = space F" unfolding F_def by auto
  8.1216 +  show ?thesis unfolding F_def[symmetric] * bor
  8.1217 +  proof(rule vimage_algebra_sigma,unfold E_def[symmetric])
  8.1218 +    show "sets E \<subseteq> Pow (space E)" "p2e \<in> space F \<rightarrow> space E" unfolding E_def by auto
  8.1219 +  next fix A assume "A \<in> sets F"
  8.1220 +    hence A:"A \<in> (Pi\<^isub>E {..<DIM('a)}) ` ({..<DIM('a)} \<rightarrow> range lessThan)"
  8.1221 +      unfolding F_def product_algebra_def algebra.simps .
  8.1222 +    then guess B unfolding image_iff .. note B=this
  8.1223 +    hence "\<forall>x<DIM('a). B x \<in> range lessThan" by auto
  8.1224 +    hence "\<forall>x. \<exists>xa. x<DIM('a) \<longrightarrow> B x = {..<xa}" unfolding image_iff by auto
  8.1225 +    from choice[OF this] guess b .. note b=this
  8.1226 +    hence b':"\<forall>i<DIM('a). Sup (B i) = b i" using Sup_lessThan by auto
  8.1227 +
  8.1228 +    show "A \<in> (\<lambda>X. p2e -` X \<inter> space F) ` sets E" unfolding image_iff B
  8.1229 +    proof(rule_tac x="{..< \<chi>\<chi> i. Sup (B i)}" in bexI)
  8.1230 +      show "Pi\<^isub>E {..<DIM('a)} B = p2e -` {..<(\<chi>\<chi> i. Sup (B i))::'a} \<inter> space F"
  8.1231 +        unfolding F_def E_def product_algebra_def algebra.simps
  8.1232 +      proof(rule,unfold subset_eq,rule_tac[!] ballI)
  8.1233 +        fix x assume "x \<in> Pi\<^isub>E {..<DIM('a)} B"
  8.1234 +        hence *:"\<forall>i<DIM('a). x i < b i" "\<forall>i\<ge>DIM('a). x i = undefined"
  8.1235 +          unfolding Pi_def extensional_def using b by auto
  8.1236 +        have "(p2e x::'a) < (\<chi>\<chi> i. Sup (B i))" unfolding less_prod_def eucl_less[of "p2e x"]
  8.1237 +          apply safe unfolding euclidean_lambda_beta b'[rule_format] p2e_def using * by auto
  8.1238 +        moreover have "x \<in> extensional {..<DIM('a)}"
  8.1239 +          using *(2) unfolding extensional_def by auto
  8.1240 +        ultimately show "x \<in> p2e -` {..<(\<chi>\<chi> i. Sup (B i)) ::'a} \<inter>
  8.1241 +          (({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)})" by auto
  8.1242 +      next fix x assume as:"x \<in> p2e -` {..<(\<chi>\<chi> i. Sup (B i))::'a} \<inter>
  8.1243 +          (({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)})"
  8.1244 +        hence "p2e x < ((\<chi>\<chi> i. Sup (B i))::'a)" by auto
  8.1245 +        hence "\<forall>i<DIM('a). x i \<in> B i" apply-apply(subst(asm) eucl_less)
  8.1246 +          unfolding p2e_def using b b' by auto
  8.1247 +        thus "x \<in> Pi\<^isub>E {..<DIM('a)} B" using as unfolding Pi_def extensional_def by auto
  8.1248 +      qed
  8.1249 +      show "{..<(\<chi>\<chi> i. Sup (B i))::'a} \<in> sets E" unfolding E_def algebra.simps by auto
  8.1250 +    qed
  8.1251 +  next fix A assume "A \<in> sets E"
  8.1252 +    then guess a unfolding E_def algebra.simps image_iff .. note a = this(2)
  8.1253 +    def B \<equiv> "\<lambda>i. {..<a $$ i}"
  8.1254 +    show "p2e -` A \<inter> space F \<in> sets F" unfolding F_def
  8.1255 +      unfolding product_algebra_def algebra.simps image_iff
  8.1256 +      apply(rule_tac x=B in bexI) apply rule unfolding subset_eq apply(rule_tac[1-2] ballI)
  8.1257 +    proof- show "B \<in> {..<DIM('a)} \<rightarrow> range lessThan" unfolding B_def by auto
  8.1258 +      fix x assume as:"x \<in> p2e -` A \<inter> (({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)})"
  8.1259 +      hence "p2e x \<in> A" by auto
  8.1260 +      hence "\<forall>i<DIM('a). x i \<in> B i" unfolding B_def a lessThan_iff
  8.1261 +        apply-apply(subst (asm) eucl_less) unfolding p2e_def by auto
  8.1262 +      thus "x \<in> Pi\<^isub>E {..<DIM('a)} B" using as unfolding Pi_def extensional_def by auto
  8.1263 +    next fix x assume x:"x \<in> Pi\<^isub>E {..<DIM('a)} B"
  8.1264 +      moreover have "p2e x \<in> A" unfolding a lessThan_iff p2e_def apply(subst eucl_less)
  8.1265 +        using x unfolding Pi_def extensional_def B_def by auto
  8.1266 +      ultimately show "x \<in> p2e -` A \<inter> (({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)})" by auto
  8.1267 +    qed
  8.1268 +  qed
  8.1269 +qed
  8.1270 +
  8.1271 +lemma Real_mult_nonneg: assumes "x \<ge> 0" "y \<ge> 0"
  8.1272 +  shows "Real (x * y) = Real x * Real y" using assms by auto
  8.1273 +
  8.1274 +lemma Real_setprod: assumes "\<forall>x\<in>A. f x \<ge> 0" shows "Real (setprod f A) = setprod (\<lambda>x. Real (f x)) A"
  8.1275 +proof(cases "finite A")
  8.1276 +  case True thus ?thesis using assms
  8.1277 +  proof(induct A) case (insert x A)
  8.1278 +    have "0 \<le> setprod f A" apply(rule setprod_nonneg) using insert by auto
  8.1279 +    thus ?case unfolding setprod_insert[OF insert(1-2)] apply-
  8.1280 +      apply(subst Real_mult_nonneg) prefer 3 apply(subst insert(3)[THEN sym])
  8.1281 +      using insert by auto
  8.1282 +  qed auto
  8.1283 +qed auto
  8.1284 +
  8.1285 +lemma e2p_Int:"e2p ` A \<inter> e2p ` B = e2p ` (A \<inter> B)" (is "?L = ?R")
  8.1286 +  apply(rule image_Int[THEN sym]) using bij_euclidean_component
  8.1287 +  unfolding bij_betw_def by auto
  8.1288 +
  8.1289 +lemma Int_stable_cuboids: fixes x::"'a::ordered_euclidean_space"
  8.1290 +  shows "Int_stable \<lparr>space = UNIV, sets = range (\<lambda>(a, b::'a). e2p ` {a..b})\<rparr>"
  8.1291 +  unfolding Int_stable_def algebra.select_convs
  8.1292 +proof safe fix a b x y::'a
  8.1293 +  have *:"e2p ` {a..b} \<inter> e2p ` {x..y} =
  8.1294 +    (\<lambda>(a, b). e2p ` {a..b}) (\<chi>\<chi> i. max (a $$ i) (x $$ i), \<chi>\<chi> i. min (b $$ i) (y $$ i)::'a)"
  8.1295 +    unfolding e2p_Int inter_interval by auto
  8.1296 +  show "e2p ` {a..b} \<inter> e2p ` {x..y} \<in> range (\<lambda>(a, b). e2p ` {a..b::'a})" unfolding *
  8.1297 +    apply(rule range_eqI) ..
  8.1298 +qed
  8.1299 +
  8.1300 +lemma Int_stable_cuboids': fixes x::"'a::ordered_euclidean_space"
  8.1301 +  shows "Int_stable \<lparr>space = UNIV, sets = range (\<lambda>(a, b::'a). {a..b})\<rparr>"
  8.1302 +  unfolding Int_stable_def algebra.select_convs
  8.1303 +  apply safe unfolding inter_interval by auto
  8.1304 +
  8.1305 +lemma product_borel_eq_vimage:
  8.1306 +  "sigma (product_algebra (\<lambda>x. borel) {..<DIM('a::ordered_euclidean_space)}) =
  8.1307 +  sigma_algebra.vimage_algebra borel (({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)})
  8.1308 +  (p2e:: _ \<Rightarrow> 'a::ordered_euclidean_space)"
  8.1309 +  unfolding borel_vimage_algebra_eq unfolding borel_eq_lessThan
  8.1310 +  apply(subst sigma_product_algebra_sigma_eq[where S="\<lambda>i. \<lambda>n. lessThan (real n)"])
  8.1311 +  unfolding lessThan_iff
  8.1312 +proof- fix i assume i:"i<DIM('a)"
  8.1313 +  show "(\<lambda>n. {..<real n}) \<up> space \<lparr>space = UNIV, sets = range lessThan\<rparr>"
  8.1314 +    by(auto intro!:real_arch_lt isotoneI)
  8.1315 +qed auto
  8.1316 +
  8.1317 +lemma inj_on_disjoint_family_on: assumes "disjoint_family_on A S" "inj f"
  8.1318 +  shows "disjoint_family_on (\<lambda>x. f ` A x) S"
  8.1319 +  unfolding disjoint_family_on_def
  8.1320 +proof(rule,rule,rule)
  8.1321 +  fix x1 x2 assume x:"x1 \<in> S" "x2 \<in> S" "x1 \<noteq> x2"
  8.1322 +  show "f ` A x1 \<inter> f ` A x2 = {}"
  8.1323 +  proof(rule ccontr) case goal1
  8.1324 +    then obtain z where z:"z \<in> f ` A x1 \<inter> f ` A x2" by auto
  8.1325 +    then obtain z1 z2 where z12:"z1 \<in> A x1" "z2 \<in> A x2" "f z1 = z" "f z2 = z" by auto
  8.1326 +    hence "z1 = z2" using assms(2) unfolding inj_on_def by blast
  8.1327 +    hence "x1 = x2" using z12(1-2) using assms[unfolded disjoint_family_on_def] using x by auto
  8.1328 +    thus False using x(3) by auto
  8.1329 +  qed
  8.1330 +qed
  8.1331 +
  8.1332 +declare restrict_extensional[intro]
  8.1333 +
  8.1334 +lemma e2p_extensional[intro]:"e2p (y::'a::ordered_euclidean_space) \<in> extensional {..<DIM('a)}"
  8.1335 +  unfolding e2p_def by auto
  8.1336 +
  8.1337 +lemma e2p_image_vimage: fixes A::"'a::ordered_euclidean_space set"
  8.1338 +  shows "e2p ` A = p2e -` A \<inter> (({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)})"
  8.1339 +proof(rule set_eqI,rule)
  8.1340 +  fix x assume "x \<in> e2p ` A" then guess y unfolding image_iff .. note y=this
  8.1341 +  show "x \<in> p2e -` A \<inter> (({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)})"
  8.1342 +    apply safe apply(rule vimageI[OF _ y(1)]) unfolding y p2e_e2p by auto
  8.1343 +next fix x assume "x \<in> p2e -` A \<inter> (({..<DIM('a)} \<rightarrow> UNIV) \<inter> extensional {..<DIM('a)})"
  8.1344 +  thus "x \<in> e2p ` A" unfolding image_iff apply(rule_tac x="p2e x" in bexI) apply(subst e2p_p2e) by auto
  8.1345 +qed
  8.1346 +
  8.1347 +lemma lmeasure_measure_eq_borel_prod:
  8.1348 +  fixes A :: "('a::ordered_euclidean_space) set"
  8.1349 +  assumes "A \<in> sets borel"
  8.1350 +  shows "lmeasure A = borel_product.product_measure {..<DIM('a)} (e2p ` A :: (nat \<Rightarrow> real) set)"
  8.1351 +proof (rule measure_unique_Int_stable[where X=A and A=cube])
  8.1352 +  interpret fprod: finite_product_sigma_finite "\<lambda>x. borel" "\<lambda>x. lmeasure" "{..<DIM('a)}" by default auto
  8.1353 +  show "Int_stable \<lparr> space = UNIV :: 'a set, sets = range (\<lambda>(a,b). {a..b}) \<rparr>"
  8.1354 +    (is "Int_stable ?E" ) using Int_stable_cuboids' .
  8.1355 +  show "borel = sigma ?E" using borel_eq_atLeastAtMost .
  8.1356 +  show "\<And>i. lmeasure (cube i) \<noteq> \<omega>" unfolding lmeasure_cube by auto
  8.1357 +  show "\<And>X. X \<in> sets ?E \<Longrightarrow>
  8.1358 +    lmeasure X = borel_product.product_measure {..<DIM('a)} (e2p ` X :: (nat \<Rightarrow> real) set)"
  8.1359 +  proof- case goal1 then obtain a b where X:"X = {a..b}" by auto
  8.1360 +    { presume *:"X \<noteq> {} \<Longrightarrow> ?case"
  8.1361 +      show ?case apply(cases,rule *,assumption) by auto }
  8.1362 +    def XX \<equiv> "\<lambda>i. {a $$ i .. b $$ i}" assume  "X \<noteq> {}"  note X' = this[unfolded X interval_ne_empty]
  8.1363 +    have *:"Pi\<^isub>E {..<DIM('a)} XX = e2p ` X" apply(rule set_eqI)
  8.1364 +    proof fix x assume "x \<in> Pi\<^isub>E {..<DIM('a)} XX"
  8.1365 +      thus "x \<in> e2p ` X" unfolding image_iff apply(rule_tac x="\<chi>\<chi> i. x i" in bexI)
  8.1366 +        unfolding Pi_def extensional_def e2p_def restrict_def X mem_interval XX_def by rule auto
  8.1367 +    next fix x assume "x \<in> e2p ` X" then guess y unfolding image_iff .. note y = this
  8.1368 +      show "x \<in> Pi\<^isub>E {..<DIM('a)} XX" unfolding y using y(1)
  8.1369 +        unfolding Pi_def extensional_def e2p_def restrict_def X mem_interval XX_def by auto
  8.1370 +    qed
  8.1371 +    have "lmeasure X = (\<Prod>x<DIM('a). Real (b $$ x - a $$ x))"  using X' apply- unfolding X
  8.1372 +      unfolding lmeasure_atLeastAtMost content_closed_interval apply(subst Real_setprod) by auto
  8.1373 +    also have "... = (\<Prod>i<DIM('a). lmeasure (XX i))" apply(rule setprod_cong2)
  8.1374 +      unfolding XX_def lmeasure_atLeastAtMost apply(subst content_real) using X' by auto
  8.1375 +    also have "... = borel_product.product_measure {..<DIM('a)} (e2p ` X)" unfolding *[THEN sym]
  8.1376 +      apply(rule fprod.measure_times[THEN sym]) unfolding XX_def by auto
  8.1377 +    finally show ?case .
  8.1378 +  qed
  8.1379 +
  8.1380 +  show "range cube \<subseteq> sets \<lparr>space = UNIV, sets = range (\<lambda>(a, b). {a..b})\<rparr>"
  8.1381 +    unfolding cube_def_raw by auto
  8.1382 +  have "\<And>x. \<exists>xa. x \<in> cube xa" apply(rule_tac x=x in mem_big_cube) by fastsimp
  8.1383 +  thus "cube \<up> space \<lparr>space = UNIV, sets = range (\<lambda>(a, b). {a..b})\<rparr>"
  8.1384 +    apply-apply(rule isotoneI) apply(rule cube_subset_Suc) by auto
  8.1385 +  show "A \<in> sets borel " by fact
  8.1386 +  show "measure_space borel lmeasure" by default
  8.1387 +  show "measure_space borel
  8.1388 +     (\<lambda>a::'a set. finite_product_sigma_finite.measure (\<lambda>x. borel) (\<lambda>x. lmeasure) {..<DIM('a)} (e2p ` a))"
  8.1389 +    apply default unfolding countably_additive_def
  8.1390 +  proof safe fix A::"nat \<Rightarrow> 'a set" assume A:"range A \<subseteq> sets borel" "disjoint_family A"
  8.1391 +      "(\<Union>i. A i) \<in> sets borel"
  8.1392 +    note fprod.ca[unfolded countably_additive_def,rule_format]
  8.1393 +    note ca = this[of "\<lambda> n. e2p ` (A n)"]
  8.1394 +    show "(\<Sum>\<^isub>\<infinity>n. finite_product_sigma_finite.measure
  8.1395 +        (\<lambda>x. borel) (\<lambda>x. lmeasure) {..<DIM('a)} (e2p ` A n)) =
  8.1396 +           finite_product_sigma_finite.measure (\<lambda>x. borel)
  8.1397 +            (\<lambda>x. lmeasure) {..<DIM('a)} (e2p ` (\<Union>i. A i))" unfolding image_UN
  8.1398 +    proof(rule ca) show "range (\<lambda>n. e2p ` A n) \<subseteq> sets
  8.1399 +       (sigma (product_algebra (\<lambda>x. borel) {..<DIM('a)}))"
  8.1400 +        unfolding product_borel_eq_vimage
  8.1401 +      proof case goal1
  8.1402 +        then guess y unfolding image_iff .. note y=this(2)
  8.1403 +        show ?case unfolding borel.in_vimage_algebra y apply-
  8.1404 +          apply(rule_tac x="A y" in bexI,rule e2p_image_vimage)
  8.1405 +          using A(1) by auto
  8.1406 +      qed
  8.1407 +
  8.1408 +      show "disjoint_family (\<lambda>n. e2p ` A n)" apply(rule inj_on_disjoint_family_on)
  8.1409 +        using bij_euclidean_component using A(2) unfolding bij_betw_def by auto
  8.1410 +      show "(\<Union>n. e2p ` A n) \<in> sets (sigma (product_algebra (\<lambda>x. borel) {..<DIM('a)}))"
  8.1411 +        unfolding product_borel_eq_vimage borel.in_vimage_algebra
  8.1412 +      proof(rule bexI[OF _ A(3)],rule set_eqI,rule)
  8.1413 +        fix x assume x:"x \<in> (\<Union>n. e2p ` A n)" hence "p2e x \<in> (\<Union>i. A i)" by auto
  8.1414 +        moreover have "x \<in> extensional {..<DIM('a)}"
  8.1415 +          using x unfolding extensional_def e2p_def_raw by auto
  8.1416 +        ultimately show "x \<in> p2e -` (\<Union>i. A i) \<inter> (({..<DIM('a)} \<rightarrow> UNIV) \<inter>
  8.1417 +          extensional {..<DIM('a)})" by auto
  8.1418 +      next fix x assume x:"x \<in> p2e -` (\<Union>i. A i) \<inter> (({..<DIM('a)} \<rightarrow> UNIV) \<inter>
  8.1419 +          extensional {..<DIM('a)})"
  8.1420 +        hence "p2e x \<in> (\<Union>i. A i)" by auto
  8.1421 +        hence "\<exists>n. x \<in> e2p ` A n" apply safe apply(rule_tac x=i in exI)
  8.1422 +          unfolding image_iff apply(rule_tac x="p2e x" in bexI)
  8.1423 +          apply(subst e2p_p2e) using x by auto
  8.1424 +        thus "x \<in> (\<Union>n. e2p ` A n)" by auto
  8.1425 +      qed
  8.1426 +    qed
  8.1427 +  qed auto
  8.1428 +qed
  8.1429 +