reworked Probability theory: measures are not type restricted to positive extended reals
authorhoelzl
Mon Mar 14 14:37:49 2011 +0100 (2011-03-14)
changeset 41981cdf7693bbe08
parent 41980 28b51effc5ed
child 41982 96cbc6379e5a
reworked Probability theory: measures are not type restricted to positive extended reals
src/HOL/IsaMakefile
src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy
src/HOL/Probability/Borel_Space.thy
src/HOL/Probability/Caratheodory.thy
src/HOL/Probability/Complete_Measure.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_Extended_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/IsaMakefile	Mon Mar 14 14:37:47 2011 +0100
     1.2 +++ b/src/HOL/IsaMakefile	Mon Mar 14 14:37:49 2011 +0100
     1.3 @@ -1168,9 +1168,10 @@
     1.4    Multivariate_Analysis/Topology_Euclidean_Space.thy			\
     1.5    Multivariate_Analysis/document/root.tex				\
     1.6    Multivariate_Analysis/normarith.ML Library/Glbs.thy			\
     1.7 -  Library/Indicator_Function.thy Library/Inner_Product.thy		\
     1.8 -  Library/Numeral_Type.thy Library/Convex.thy Library/FrechetDeriv.thy	\
     1.9 -  Library/Product_Vector.thy Library/Product_plus.thy
    1.10 +  Library/Extended_Reals.thy Library/Indicator_Function.thy		\
    1.11 +  Library/Inner_Product.thy Library/Numeral_Type.thy Library/Convex.thy	\
    1.12 +  Library/FrechetDeriv.thy Library/Product_Vector.thy			\
    1.13 +  Library/Product_plus.thy
    1.14  	@cd Multivariate_Analysis; $(ISABELLE_TOOL) usedir -b -g true $(OUT)/HOL HOL-Multivariate_Analysis
    1.15  
    1.16  
    1.17 @@ -1185,7 +1186,6 @@
    1.18    Probability/ex/Koepf_Duermuth_Countermeasure.thy			\
    1.19    Probability/Information.thy Probability/Lebesgue_Integration.thy	\
    1.20    Probability/Lebesgue_Measure.thy Probability/Measure.thy		\
    1.21 -  Probability/Positive_Extended_Real.thy				\
    1.22    Probability/Probability_Space.thy Probability/Probability.thy		\
    1.23    Probability/Product_Measure.thy Probability/Radon_Nikodym.thy		\
    1.24    Probability/ROOT.ML Probability/Sigma_Algebra.thy			\
     2.1 --- a/src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy	Mon Mar 14 14:37:47 2011 +0100
     2.2 +++ b/src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy	Mon Mar 14 14:37:49 2011 +0100
     2.3 @@ -1028,7 +1028,7 @@
     2.4    qed simp
     2.5  qed
     2.6  
     2.7 -lemma setsum_of_pextreal:
     2.8 +lemma setsum_real_of_extreal:
     2.9    assumes "\<And>x. x \<in> S \<Longrightarrow> \<bar>f x\<bar> \<noteq> \<infinity>"
    2.10    shows "(\<Sum>x\<in>S. real (f x)) = real (setsum f S)"
    2.11  proof -
    2.12 @@ -1062,17 +1062,6 @@
    2.13      by induct (auto simp: extreal_right_distrib setsum_nonneg)
    2.14  qed simp
    2.15  
    2.16 -lemma setsum_real_of_extreal:
    2.17 -  assumes "\<And>x. x \<in> A \<Longrightarrow> \<bar>f x\<bar> \<noteq> \<infinity>"
    2.18 -  shows "real (\<Sum>x\<in>A. f x) = (\<Sum>x\<in>A. real (f x))"
    2.19 -proof cases
    2.20 -  assume "finite A" from this assms show ?thesis
    2.21 -  proof induct
    2.22 -    case (insert a A) then show ?case
    2.23 -      by (simp add: real_of_extreal_add setsum_Inf)
    2.24 -  qed simp
    2.25 -qed simp
    2.26 -
    2.27  lemma sums_extreal_positive:
    2.28    fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i" shows "f sums (SUP n. \<Sum>i<n. f i)"
    2.29  proof -
     3.1 --- a/src/HOL/Probability/Borel_Space.thy	Mon Mar 14 14:37:47 2011 +0100
     3.2 +++ b/src/HOL/Probability/Borel_Space.thy	Mon Mar 14 14:37:49 2011 +0100
     3.3 @@ -3,13 +3,9 @@
     3.4  header {*Borel spaces*}
     3.5  
     3.6  theory Borel_Space
     3.7 -  imports Sigma_Algebra Positive_Extended_Real Multivariate_Analysis
     3.8 +  imports Sigma_Algebra Multivariate_Analysis
     3.9  begin
    3.10  
    3.11 -lemma LIMSEQ_max:
    3.12 -  "u ----> (x::real) \<Longrightarrow> (\<lambda>i. max (u i) 0) ----> max x 0"
    3.13 -  by (fastsimp intro!: LIMSEQ_I dest!: LIMSEQ_D)
    3.14 -
    3.15  section "Generic Borel spaces"
    3.16  
    3.17  definition "borel = sigma \<lparr> space = UNIV::'a::topological_space set, sets = open\<rparr>"
    3.18 @@ -112,26 +108,8 @@
    3.19    ultimately show "?I \<in> borel_measurable M" by auto
    3.20  qed
    3.21  
    3.22 -lemma borel_measurable_translate:
    3.23 -  assumes "A \<in> sets borel" and trans: "\<And>B. open B \<Longrightarrow> f -` B \<in> sets borel"
    3.24 -  shows "f -` A \<in> sets borel"
    3.25 -proof -
    3.26 -  have "A \<in> sigma_sets UNIV open" using assms
    3.27 -    by (simp add: borel_def sigma_def)
    3.28 -  thus ?thesis
    3.29 -  proof (induct rule: sigma_sets.induct)
    3.30 -    case (Basic a) thus ?case using trans[of a] by (simp add: mem_def)
    3.31 -  next
    3.32 -    case (Compl a)
    3.33 -    moreover have "UNIV \<in> sets borel"
    3.34 -      using borel.top by simp
    3.35 -    ultimately show ?case
    3.36 -      by (auto simp: vimage_Diff borel.Diff)
    3.37 -  qed (auto simp add: vimage_UN)
    3.38 -qed
    3.39 -
    3.40  lemma (in sigma_algebra) borel_measurable_restricted:
    3.41 -  fixes f :: "'a \<Rightarrow> 'x\<Colon>{topological_space, semiring_1}" assumes "A \<in> sets M"
    3.42 +  fixes f :: "'a \<Rightarrow> extreal" assumes "A \<in> sets M"
    3.43    shows "f \<in> borel_measurable (restricted_space A) \<longleftrightarrow>
    3.44      (\<lambda>x. f x * indicator A x) \<in> borel_measurable M"
    3.45      (is "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable M")
    3.46 @@ -142,7 +120,7 @@
    3.47    show ?thesis unfolding *
    3.48      unfolding in_borel_measurable_borel
    3.49    proof (simp, safe)
    3.50 -    fix S :: "'x set" assume "S \<in> sets borel"
    3.51 +    fix S :: "extreal set" assume "S \<in> sets borel"
    3.52        "\<forall>S\<in>sets borel. ?f -` S \<inter> A \<in> op \<inter> A ` sets M"
    3.53      then have "?f -` S \<inter> A \<in> op \<inter> A ` sets M" by auto
    3.54      then have f: "?f -` S \<inter> A \<in> sets M"
    3.55 @@ -161,7 +139,7 @@
    3.56        then show ?thesis using f by auto
    3.57      qed
    3.58    next
    3.59 -    fix S :: "'x set" assume "S \<in> sets borel"
    3.60 +    fix S :: "extreal set" assume "S \<in> sets borel"
    3.61        "\<forall>S\<in>sets borel. ?f -` S \<inter> space M \<in> sets M"
    3.62      then have f: "?f -` S \<inter> space M \<in> sets M" by auto
    3.63      then show "?f -` S \<inter> A \<in> op \<inter> A ` sets M"
    3.64 @@ -1024,103 +1002,6 @@
    3.65    using borel_measurable_euclidean_component
    3.66    unfolding nth_conv_component by auto
    3.67  
    3.68 -section "Borel space over the real line with infinity"
    3.69 -
    3.70 -lemma borel_Real_measurable:
    3.71 -  "A \<in> sets borel \<Longrightarrow> Real -` A \<in> sets borel"
    3.72 -proof (rule borel_measurable_translate)
    3.73 -  fix B :: "pextreal set" assume "open B"
    3.74 -  then obtain T x where T: "open T" "Real ` (T \<inter> {0..}) = B - {\<omega>}" and
    3.75 -    x: "\<omega> \<in> B \<Longrightarrow> 0 \<le> x" "\<omega> \<in> B \<Longrightarrow> {Real x <..} \<subseteq> B"
    3.76 -    unfolding open_pextreal_def by blast
    3.77 -  have "Real -` B = Real -` (B - {\<omega>})" by auto
    3.78 -  also have "\<dots> = Real -` (Real ` (T \<inter> {0..}))" using T by simp
    3.79 -  also have "\<dots> = (if 0 \<in> T then T \<union> {.. 0} else T \<inter> {0..})"
    3.80 -    apply (auto simp add: Real_eq_Real image_iff)
    3.81 -    apply (rule_tac x="max 0 x" in bexI)
    3.82 -    by (auto simp: max_def)
    3.83 -  finally show "Real -` B \<in> sets borel"
    3.84 -    using `open T` by auto
    3.85 -qed simp
    3.86 -
    3.87 -lemma borel_real_measurable:
    3.88 -  "A \<in> sets borel \<Longrightarrow> (real -` A :: pextreal set) \<in> sets borel"
    3.89 -proof (rule borel_measurable_translate)
    3.90 -  fix B :: "real set" assume "open B"
    3.91 -  { fix x have "0 < real x \<longleftrightarrow> (\<exists>r>0. x = Real r)" by (cases x) auto }
    3.92 -  note Ex_less_real = this
    3.93 -  have *: "real -` B = (if 0 \<in> B then real -` (B \<inter> {0 <..}) \<union> {0, \<omega>} else real -` (B \<inter> {0 <..}))"
    3.94 -    by (force simp: Ex_less_real)
    3.95 -
    3.96 -  have "open (real -` (B \<inter> {0 <..}) :: pextreal set)"
    3.97 -    unfolding open_pextreal_def using `open B`
    3.98 -    by (auto intro!: open_Int exI[of _ "B \<inter> {0 <..}"] simp: image_iff Ex_less_real)
    3.99 -  then show "(real -` B :: pextreal set) \<in> sets borel" unfolding * by auto
   3.100 -qed simp
   3.101 -
   3.102 -lemma (in sigma_algebra) borel_measurable_Real[intro, simp]:
   3.103 -  assumes "f \<in> borel_measurable M"
   3.104 -  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
   3.105 -  unfolding in_borel_measurable_borel
   3.106 -proof safe
   3.107 -  fix S :: "pextreal set" assume "S \<in> sets borel"
   3.108 -  from borel_Real_measurable[OF this]
   3.109 -  have "(Real \<circ> f) -` S \<inter> space M \<in> sets M"
   3.110 -    using assms
   3.111 -    unfolding vimage_compose in_borel_measurable_borel
   3.112 -    by auto
   3.113 -  thus "(\<lambda>x. Real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
   3.114 -qed
   3.115 -
   3.116 -lemma (in sigma_algebra) borel_measurable_real[intro, simp]:
   3.117 -  fixes f :: "'a \<Rightarrow> pextreal"
   3.118 -  assumes "f \<in> borel_measurable M"
   3.119 -  shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
   3.120 -  unfolding in_borel_measurable_borel
   3.121 -proof safe
   3.122 -  fix S :: "real set" assume "S \<in> sets borel"
   3.123 -  from borel_real_measurable[OF this]
   3.124 -  have "(real \<circ> f) -` S \<inter> space M \<in> sets M"
   3.125 -    using assms
   3.126 -    unfolding vimage_compose in_borel_measurable_borel
   3.127 -    by auto
   3.128 -  thus "(\<lambda>x. real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
   3.129 -qed
   3.130 -
   3.131 -lemma (in sigma_algebra) borel_measurable_Real_eq:
   3.132 -  assumes "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
   3.133 -  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   3.134 -proof
   3.135 -  have [simp]: "(\<lambda>x. Real (f x)) -` {\<omega>} \<inter> space M = {}"
   3.136 -    by auto
   3.137 -  assume "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
   3.138 -  hence "(\<lambda>x. real (Real (f x))) \<in> borel_measurable M"
   3.139 -    by (rule borel_measurable_real)
   3.140 -  moreover have "\<And>x. x \<in> space M \<Longrightarrow> real (Real (f x)) = f x"
   3.141 -    using assms by auto
   3.142 -  ultimately show "f \<in> borel_measurable M"
   3.143 -    by (simp cong: measurable_cong)
   3.144 -qed auto
   3.145 -
   3.146 -lemma (in sigma_algebra) borel_measurable_pextreal_eq_real:
   3.147 -  "f \<in> borel_measurable M \<longleftrightarrow>
   3.148 -    ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<omega>} \<inter> space M \<in> sets M)"
   3.149 -proof safe
   3.150 -  assume "f \<in> borel_measurable M"
   3.151 -  then show "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
   3.152 -    by (auto intro: borel_measurable_vimage borel_measurable_real)
   3.153 -next
   3.154 -  assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
   3.155 -  have "f -` {\<omega>} \<inter> space M = {x\<in>space M. f x = \<omega>}" by auto
   3.156 -  with * have **: "{x\<in>space M. f x = \<omega>} \<in> sets M" by simp
   3.157 -  have f: "f = (\<lambda>x. if f x = \<omega> then \<omega> else Real (real (f x)))"
   3.158 -    by (simp add: fun_eq_iff Real_real)
   3.159 -  show "f \<in> borel_measurable M"
   3.160 -    apply (subst f)
   3.161 -    apply (rule measurable_If)
   3.162 -    using * ** by auto
   3.163 -qed
   3.164 -
   3.165  lemma borel_measurable_continuous_on1:
   3.166    fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   3.167    assumes "continuous_on UNIV f"
   3.168 @@ -1187,206 +1068,213 @@
   3.169    using measurable_comp[OF f borel_measurable_borel_log[OF `1 < b`]]
   3.170    by (simp add: comp_def)
   3.171  
   3.172 +subsection "Borel space on the extended reals"
   3.173 +
   3.174 +lemma borel_measurable_extreal_borel:
   3.175 +  "extreal \<in> borel_measurable borel"
   3.176 +  unfolding borel_def[where 'a=extreal]
   3.177 +proof (rule borel.measurable_sigma)
   3.178 +  fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
   3.179 +  then have "open X" by (auto simp: mem_def)
   3.180 +  then have "open (extreal -` X \<inter> space borel)"
   3.181 +    by (simp add: open_extreal_vimage)
   3.182 +  then show "extreal -` X \<inter> space borel \<in> sets borel" by auto
   3.183 +qed auto
   3.184 +
   3.185 +lemma (in sigma_algebra) borel_measurable_extreal[simp, intro]:
   3.186 +  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
   3.187 +  using measurable_comp[OF f borel_measurable_extreal_borel] unfolding comp_def .
   3.188 +
   3.189 +lemma borel_measurable_real_of_extreal_borel:
   3.190 +  "(real :: extreal \<Rightarrow> real) \<in> borel_measurable borel"
   3.191 +  unfolding borel_def[where 'a=real]
   3.192 +proof (rule borel.measurable_sigma)
   3.193 +  fix B :: "real set" assume "B \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
   3.194 +  then have "open B" by (auto simp: mem_def)
   3.195 +  have *: "extreal -` real -` (B - {0}) = B - {0}" by auto
   3.196 +  have open_real: "open (real -` (B - {0}) :: extreal set)"
   3.197 +    unfolding open_extreal_def * using `open B` by auto
   3.198 +  show "(real -` B \<inter> space borel :: extreal set) \<in> sets borel"
   3.199 +  proof cases
   3.200 +    assume "0 \<in> B"
   3.201 +    then have *: "real -` B = real -` (B - {0}) \<union> {-\<infinity>, \<infinity>, 0}"
   3.202 +      by (auto simp add: real_of_extreal_eq_0)
   3.203 +    then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
   3.204 +      using open_real by auto
   3.205 +  next
   3.206 +    assume "0 \<notin> B"
   3.207 +    then have *: "(real -` B :: extreal set) = real -` (B - {0})"
   3.208 +      by (auto simp add: real_of_extreal_eq_0)
   3.209 +    then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
   3.210 +      using open_real by auto
   3.211 +  qed
   3.212 +qed auto
   3.213 +
   3.214 +lemma (in sigma_algebra) borel_measurable_real_of_extreal[simp, intro]:
   3.215 +  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: extreal)) \<in> borel_measurable M"
   3.216 +  using measurable_comp[OF f borel_measurable_real_of_extreal_borel] unfolding comp_def .
   3.217 +
   3.218 +lemma (in sigma_algebra) borel_measurable_extreal_iff:
   3.219 +  shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   3.220 +proof
   3.221 +  assume "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
   3.222 +  from borel_measurable_real_of_extreal[OF this]
   3.223 +  show "f \<in> borel_measurable M" by auto
   3.224 +qed auto
   3.225 +
   3.226 +lemma (in sigma_algebra) borel_measurable_extreal_iff_real:
   3.227 +  "f \<in> borel_measurable M \<longleftrightarrow>
   3.228 +    ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<infinity>} \<inter> space M \<in> sets M \<and> f -` {-\<infinity>} \<inter> space M \<in> sets M)"
   3.229 +proof safe
   3.230 +  assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<infinity>} \<inter> space M \<in> sets M" "f -` {-\<infinity>} \<inter> space M \<in> sets M"
   3.231 +  have "f -` {\<infinity>} \<inter> space M = {x\<in>space M. f x = \<infinity>}" "f -` {-\<infinity>} \<inter> space M = {x\<in>space M. f x = -\<infinity>}" by auto
   3.232 +  with * have **: "{x\<in>space M. f x = \<infinity>} \<in> sets M" "{x\<in>space M. f x = -\<infinity>} \<in> sets M" by simp_all
   3.233 +  let "?f x" = "if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else extreal (real (f x))"
   3.234 +  have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
   3.235 +  also have "?f = f" by (auto simp: fun_eq_iff extreal_real)
   3.236 +  finally show "f \<in> borel_measurable M" .
   3.237 +qed (auto intro: measurable_sets borel_measurable_real_of_extreal)
   3.238  
   3.239  lemma (in sigma_algebra) less_eq_ge_measurable:
   3.240    fixes f :: "'a \<Rightarrow> 'c::linorder"
   3.241 -  shows "{x\<in>space M. a < f x} \<in> sets M \<longleftrightarrow> {x\<in>space M. f x \<le> a} \<in> sets M"
   3.242 +  shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
   3.243  proof
   3.244 -  assume "{x\<in>space M. f x \<le> a} \<in> sets M"
   3.245 -  moreover have "{x\<in>space M. a < f x} = space M - {x\<in>space M. f x \<le> a}" by auto
   3.246 -  ultimately show "{x\<in>space M. a < f x} \<in> sets M" by auto
   3.247 +  assume "f -` {a <..} \<inter> space M \<in> sets M"
   3.248 +  moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
   3.249 +  ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
   3.250  next
   3.251 -  assume "{x\<in>space M. a < f x} \<in> sets M"
   3.252 -  moreover have "{x\<in>space M. f x \<le> a} = space M - {x\<in>space M. a < f x}" by auto
   3.253 -  ultimately show "{x\<in>space M. f x \<le> a} \<in> sets M" by auto
   3.254 +  assume "f -` {..a} \<inter> space M \<in> sets M"
   3.255 +  moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
   3.256 +  ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
   3.257  qed
   3.258  
   3.259  lemma (in sigma_algebra) greater_eq_le_measurable:
   3.260    fixes f :: "'a \<Rightarrow> 'c::linorder"
   3.261 -  shows "{x\<in>space M. f x < a} \<in> sets M \<longleftrightarrow> {x\<in>space M. a \<le> f x} \<in> sets M"
   3.262 +  shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
   3.263  proof
   3.264 -  assume "{x\<in>space M. a \<le> f x} \<in> sets M"
   3.265 -  moreover have "{x\<in>space M. f x < a} = space M - {x\<in>space M. a \<le> f x}" by auto
   3.266 -  ultimately show "{x\<in>space M. f x < a} \<in> sets M" by auto
   3.267 +  assume "f -` {a ..} \<inter> space M \<in> sets M"
   3.268 +  moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
   3.269 +  ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
   3.270  next
   3.271 -  assume "{x\<in>space M. f x < a} \<in> sets M"
   3.272 -  moreover have "{x\<in>space M. a \<le> f x} = space M - {x\<in>space M. f x < a}" by auto
   3.273 -  ultimately show "{x\<in>space M. a \<le> f x} \<in> sets M" by auto
   3.274 +  assume "f -` {..< a} \<inter> space M \<in> sets M"
   3.275 +  moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
   3.276 +  ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
   3.277  qed
   3.278  
   3.279 -lemma (in sigma_algebra) less_eq_le_pextreal_measurable:
   3.280 -  fixes f :: "'a \<Rightarrow> pextreal"
   3.281 -  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)"
   3.282 +lemma (in sigma_algebra) borel_measurable_uminus_borel_extreal:
   3.283 +  "(uminus :: extreal \<Rightarrow> extreal) \<in> borel_measurable borel"
   3.284 +proof (subst borel_def, rule borel.measurable_sigma)
   3.285 +  fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open\<rparr>"
   3.286 +  then have "open X" by (simp add: mem_def)
   3.287 +  have "uminus -` X = uminus ` X" by (force simp: image_iff)
   3.288 +  then have "open (uminus -` X)" using `open X` extreal_open_uminus by auto
   3.289 +  then show "uminus -` X \<inter> space borel \<in> sets borel" by auto
   3.290 +qed auto
   3.291 +
   3.292 +lemma (in sigma_algebra) borel_measurable_uminus_extreal[intro]:
   3.293 +  assumes "f \<in> borel_measurable M"
   3.294 +  shows "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M"
   3.295 +  using measurable_comp[OF assms borel_measurable_uminus_borel_extreal] by (simp add: comp_def)
   3.296 +
   3.297 +lemma (in sigma_algebra) borel_measurable_uminus_eq_extreal[simp]:
   3.298 +  "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   3.299  proof
   3.300 -  assume a: "\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M"
   3.301 -  show "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
   3.302 -  proof
   3.303 -    fix a show "{x \<in> space M. a < f x} \<in> sets M"
   3.304 -    proof (cases a)
   3.305 -      case (preal r)
   3.306 -      have "{x\<in>space M. a < f x} = (\<Union>i. {x\<in>space M. a + inverse (of_nat (Suc i)) \<le> f x})"
   3.307 -      proof safe
   3.308 -        fix x assume "a < f x" and [simp]: "x \<in> space M"
   3.309 -        with ex_pextreal_inverse_of_nat_Suc_less[of "f x - a"]
   3.310 -        obtain n where "a + inverse (of_nat (Suc n)) < f x"
   3.311 -          by (cases "f x", auto simp: pextreal_minus_order)
   3.312 -        then have "a + inverse (of_nat (Suc n)) \<le> f x" by simp
   3.313 -        then show "x \<in> (\<Union>i. {x \<in> space M. a + inverse (of_nat (Suc i)) \<le> f x})"
   3.314 -          by auto
   3.315 -      next
   3.316 -        fix i x assume [simp]: "x \<in> space M"
   3.317 -        have "a < a + inverse (of_nat (Suc i))" using preal by auto
   3.318 -        also assume "a + inverse (of_nat (Suc i)) \<le> f x"
   3.319 -        finally show "a < f x" .
   3.320 -      qed
   3.321 -      with a show ?thesis by auto
   3.322 -    qed simp
   3.323 +  assume ?l from borel_measurable_uminus_extreal[OF this] show ?r by simp
   3.324 +qed auto
   3.325 +
   3.326 +lemma (in sigma_algebra) borel_measurable_eq_atMost_extreal:
   3.327 +  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
   3.328 +proof (intro iffI allI)
   3.329 +  assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
   3.330 +  show "f \<in> borel_measurable M"
   3.331 +    unfolding borel_measurable_extreal_iff_real borel_measurable_iff_le
   3.332 +  proof (intro conjI allI)
   3.333 +    fix a :: real
   3.334 +    { fix x :: extreal assume *: "\<forall>i::nat. real i < x"
   3.335 +      have "x = \<infinity>"
   3.336 +      proof (rule extreal_top)
   3.337 +        fix B from real_arch_lt[of B] guess n ..
   3.338 +        then have "extreal B < real n" by auto
   3.339 +        with * show "B \<le> x" by (metis less_trans less_imp_le)
   3.340 +      qed }
   3.341 +    then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
   3.342 +      by (auto simp: not_le)
   3.343 +    then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos by (auto simp del: UN_simps intro!: Diff)
   3.344 +    moreover
   3.345 +    have "{-\<infinity>} = {..-\<infinity>}" by auto
   3.346 +    then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
   3.347 +    moreover have "{x\<in>space M. f x \<le> extreal a} \<in> sets M"
   3.348 +      using pos[of "extreal a"] by (simp add: vimage_def Int_def conj_commute)
   3.349 +    moreover have "{w \<in> space M. real (f w) \<le> a} =
   3.350 +      (if a < 0 then {w \<in> space M. f w \<le> extreal a} - f -` {-\<infinity>} \<inter> space M
   3.351 +      else {w \<in> space M. f w \<le> extreal a} \<union> (f -` {\<infinity>} \<inter> space M) \<union> (f -` {-\<infinity>} \<inter> space M))" (is "?l = ?r")
   3.352 +      proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
   3.353 +    ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
   3.354    qed
   3.355 -next
   3.356 -  assume a': "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
   3.357 -  then have a: "\<forall>a. {x \<in> space M. f x \<le> a} \<in> sets M" unfolding less_eq_ge_measurable .
   3.358 -  show "\<forall>a. {x \<in> space M. a \<le> f x} \<in> sets M" unfolding greater_eq_le_measurable[symmetric]
   3.359 -  proof
   3.360 -    fix a show "{x \<in> space M. f x < a} \<in> sets M"
   3.361 -    proof (cases a)
   3.362 -      case (preal r)
   3.363 -      show ?thesis
   3.364 -      proof cases
   3.365 -        assume "a = 0" then show ?thesis by simp
   3.366 -      next
   3.367 -        assume "a \<noteq> 0"
   3.368 -        have "{x\<in>space M. f x < a} = (\<Union>i. {x\<in>space M. f x \<le> a - inverse (of_nat (Suc i))})"
   3.369 -        proof safe
   3.370 -          fix x assume "f x < a" and [simp]: "x \<in> space M"
   3.371 -          with ex_pextreal_inverse_of_nat_Suc_less[of "a - f x"]
   3.372 -          obtain n where "inverse (of_nat (Suc n)) < a - f x"
   3.373 -            using preal by (cases "f x") auto
   3.374 -          then have "f x \<le> a - inverse (of_nat (Suc n)) "
   3.375 -            using preal by (cases "f x") (auto split: split_if_asm)
   3.376 -          then show "x \<in> (\<Union>i. {x \<in> space M. f x \<le> a - inverse (of_nat (Suc i))})"
   3.377 -            by auto
   3.378 -        next
   3.379 -          fix i x assume [simp]: "x \<in> space M"
   3.380 -          assume "f x \<le> a - inverse (of_nat (Suc i))"
   3.381 -          also have "\<dots> < a" using `a \<noteq> 0` preal by auto
   3.382 -          finally show "f x < a" .
   3.383 -        qed
   3.384 -        with a show ?thesis by auto
   3.385 -      qed
   3.386 -    next
   3.387 -      case infinite
   3.388 -      have "f -` {\<omega>} \<inter> space M = (\<Inter>n. {x\<in>space M. of_nat n < f x})"
   3.389 -      proof (safe, simp_all, safe)
   3.390 -        fix x assume *: "\<forall>n::nat. Real (real n) < f x"
   3.391 -        show "f x = \<omega>"    proof (rule ccontr)
   3.392 -          assume "f x \<noteq> \<omega>"
   3.393 -          with real_arch_lt[of "real (f x)"] obtain n where "f x < of_nat n"
   3.394 -            by (auto simp: pextreal_noteq_omega_Ex)
   3.395 -          with *[THEN spec, of n] show False by auto
   3.396 -        qed
   3.397 -      qed
   3.398 -      with a' have \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" by auto
   3.399 -      moreover have "{x \<in> space M. f x < a} = space M - f -` {\<omega>} \<inter> space M"
   3.400 -        using infinite by auto
   3.401 -      ultimately show ?thesis by auto
   3.402 -    qed
   3.403 -  qed
   3.404 -qed
   3.405 +qed (simp add: measurable_sets)
   3.406  
   3.407 -lemma (in sigma_algebra) borel_measurable_pextreal_iff_greater:
   3.408 -  "(f::'a \<Rightarrow> pextreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a < f x} \<in> sets M)"
   3.409 -proof safe
   3.410 -  fix a assume f: "f \<in> borel_measurable M"
   3.411 -  have "{x\<in>space M. a < f x} = f -` {a <..} \<inter> space M" by auto
   3.412 -  with f show "{x\<in>space M. a < f x} \<in> sets M"
   3.413 -    by (auto intro!: measurable_sets)
   3.414 -next
   3.415 -  assume *: "\<forall>a. {x\<in>space M. a < f x} \<in> sets M"
   3.416 -  hence **: "\<forall>a. {x\<in>space M. f x < a} \<in> sets M"
   3.417 -    unfolding less_eq_le_pextreal_measurable
   3.418 -    unfolding greater_eq_le_measurable .
   3.419 -  show "f \<in> borel_measurable M" unfolding borel_measurable_pextreal_eq_real borel_measurable_iff_greater
   3.420 -  proof safe
   3.421 -    have "f -` {\<omega>} \<inter> space M = space M - {x\<in>space M. f x < \<omega>}" by auto
   3.422 -    then show \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" using ** by auto
   3.423 -    fix a
   3.424 -    have "{w \<in> space M. a < real (f w)} =
   3.425 -      (if 0 \<le> a then {w\<in>space M. Real a < f w} - (f -` {\<omega>} \<inter> space M) else space M)"
   3.426 -    proof (split split_if, safe del: notI)
   3.427 -      fix x assume "0 \<le> a"
   3.428 -      { assume "a < real (f x)" then show "Real a < f x" "x \<notin> f -` {\<omega>} \<inter> space M"
   3.429 -          using `0 \<le> a` by (cases "f x", auto) }
   3.430 -      { assume "Real a < f x" "x \<notin> f -` {\<omega>}" then show "a < real (f x)"
   3.431 -          using `0 \<le> a` by (cases "f x", auto) }
   3.432 -    next
   3.433 -      fix x assume "\<not> 0 \<le> a" then show "a < real (f x)" by (cases "f x") auto
   3.434 -    qed
   3.435 -    then show "{w \<in> space M. a < real (f w)} \<in> sets M"
   3.436 -      using \<omega> * by (auto intro!: Diff)
   3.437 -  qed
   3.438 -qed
   3.439 +lemma (in sigma_algebra) borel_measurable_eq_atLeast_extreal:
   3.440 +  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
   3.441 +proof
   3.442 +  assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
   3.443 +  moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
   3.444 +    by (auto simp: extreal_uminus_le_reorder)
   3.445 +  ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
   3.446 +    unfolding borel_measurable_eq_atMost_extreal by auto
   3.447 +  then show "f \<in> borel_measurable M" by simp
   3.448 +qed (simp add: measurable_sets)
   3.449  
   3.450 -lemma (in sigma_algebra) borel_measurable_pextreal_iff_less:
   3.451 -  "(f::'a \<Rightarrow> pextreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x < a} \<in> sets M)"
   3.452 -  using borel_measurable_pextreal_iff_greater unfolding less_eq_le_pextreal_measurable greater_eq_le_measurable .
   3.453 +lemma (in sigma_algebra) borel_measurable_extreal_iff_less:
   3.454 +  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
   3.455 +  unfolding borel_measurable_eq_atLeast_extreal greater_eq_le_measurable ..
   3.456  
   3.457 -lemma (in sigma_algebra) borel_measurable_pextreal_iff_le:
   3.458 -  "(f::'a \<Rightarrow> pextreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x \<le> a} \<in> sets M)"
   3.459 -  using borel_measurable_pextreal_iff_greater unfolding less_eq_ge_measurable .
   3.460 +lemma (in sigma_algebra) borel_measurable_extreal_iff_ge:
   3.461 +  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
   3.462 +  unfolding borel_measurable_eq_atMost_extreal less_eq_ge_measurable ..
   3.463  
   3.464 -lemma (in sigma_algebra) borel_measurable_pextreal_iff_ge:
   3.465 -  "(f::'a \<Rightarrow> pextreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M)"
   3.466 -  using borel_measurable_pextreal_iff_greater unfolding less_eq_le_pextreal_measurable .
   3.467 -
   3.468 -lemma (in sigma_algebra) borel_measurable_pextreal_eq_const:
   3.469 -  fixes f :: "'a \<Rightarrow> pextreal" assumes "f \<in> borel_measurable M"
   3.470 +lemma (in sigma_algebra) borel_measurable_extreal_eq_const:
   3.471 +  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
   3.472    shows "{x\<in>space M. f x = c} \<in> sets M"
   3.473  proof -
   3.474    have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
   3.475    then show ?thesis using assms by (auto intro!: measurable_sets)
   3.476  qed
   3.477  
   3.478 -lemma (in sigma_algebra) borel_measurable_pextreal_neq_const:
   3.479 -  fixes f :: "'a \<Rightarrow> pextreal"
   3.480 -  assumes "f \<in> borel_measurable M"
   3.481 +lemma (in sigma_algebra) borel_measurable_extreal_neq_const:
   3.482 +  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
   3.483    shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
   3.484  proof -
   3.485    have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
   3.486    then show ?thesis using assms by (auto intro!: measurable_sets)
   3.487  qed
   3.488  
   3.489 -lemma (in sigma_algebra) borel_measurable_pextreal_less[intro,simp]:
   3.490 -  fixes f g :: "'a \<Rightarrow> pextreal"
   3.491 +lemma (in sigma_algebra) borel_measurable_extreal_le[intro,simp]:
   3.492 +  fixes f g :: "'a \<Rightarrow> extreal"
   3.493 +  assumes f: "f \<in> borel_measurable M"
   3.494 +  assumes g: "g \<in> borel_measurable M"
   3.495 +  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
   3.496 +proof -
   3.497 +  have "{x \<in> space M. f x \<le> g x} =
   3.498 +    {x \<in> space M. real (f x) \<le> real (g x)} - (f -` {\<infinity>, -\<infinity>} \<inter> space M \<union> g -` {\<infinity>, -\<infinity>} \<inter> space M) \<union>
   3.499 +    f -` {-\<infinity>} \<inter> space M \<union> g -` {\<infinity>} \<inter> space M" (is "?l = ?r")
   3.500 +  proof (intro set_eqI)
   3.501 +    fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases rule: extreal2_cases[of "f x" "g x"]) auto
   3.502 +  qed
   3.503 +  with f g show ?thesis by (auto intro!: Un simp: measurable_sets)
   3.504 +qed
   3.505 +
   3.506 +lemma (in sigma_algebra) borel_measurable_extreal_less[intro,simp]:
   3.507 +  fixes f :: "'a \<Rightarrow> extreal"
   3.508    assumes f: "f \<in> borel_measurable M"
   3.509    assumes g: "g \<in> borel_measurable M"
   3.510    shows "{x \<in> space M. f x < g x} \<in> sets M"
   3.511  proof -
   3.512 -  have "(\<lambda>x. real (f x)) \<in> borel_measurable M"
   3.513 -    "(\<lambda>x. real (g x)) \<in> borel_measurable M"
   3.514 -    using assms by (auto intro!: borel_measurable_real)
   3.515 -  from borel_measurable_less[OF this]
   3.516 -  have "{x \<in> space M. real (f x) < real (g x)} \<in> sets M" .
   3.517 -  moreover have "{x \<in> space M. f x \<noteq> \<omega>} \<in> sets M" using f by (rule borel_measurable_pextreal_neq_const)
   3.518 -  moreover have "{x \<in> space M. g x = \<omega>} \<in> sets M" using g by (rule borel_measurable_pextreal_eq_const)
   3.519 -  moreover have "{x \<in> space M. g x \<noteq> \<omega>} \<in> sets M" using g by (rule borel_measurable_pextreal_neq_const)
   3.520 -  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>
   3.521 -    ({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)})"
   3.522 -    by (auto simp: real_of_pextreal_strict_mono_iff)
   3.523 -  ultimately show ?thesis by auto
   3.524 -qed
   3.525 -
   3.526 -lemma (in sigma_algebra) borel_measurable_pextreal_le[intro,simp]:
   3.527 -  fixes f :: "'a \<Rightarrow> pextreal"
   3.528 -  assumes f: "f \<in> borel_measurable M"
   3.529 -  assumes g: "g \<in> borel_measurable M"
   3.530 -  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
   3.531 -proof -
   3.532 -  have "{x \<in> space M. f x \<le> g x} = space M - {x \<in> space M. g x < f x}" by auto
   3.533 +  have "{x \<in> space M. f x < g x} = space M - {x \<in> space M. g x \<le> f x}" by auto
   3.534    then show ?thesis using g f by auto
   3.535  qed
   3.536  
   3.537 -lemma (in sigma_algebra) borel_measurable_pextreal_eq[intro,simp]:
   3.538 -  fixes f :: "'a \<Rightarrow> pextreal"
   3.539 +lemma (in sigma_algebra) borel_measurable_extreal_eq[intro,simp]:
   3.540 +  fixes f :: "'a \<Rightarrow> extreal"
   3.541    assumes f: "f \<in> borel_measurable M"
   3.542    assumes g: "g \<in> borel_measurable M"
   3.543    shows "{w \<in> space M. f w = g w} \<in> sets M"
   3.544 @@ -1395,8 +1283,8 @@
   3.545    then show ?thesis using g f by auto
   3.546  qed
   3.547  
   3.548 -lemma (in sigma_algebra) borel_measurable_pextreal_neq[intro,simp]:
   3.549 -  fixes f :: "'a \<Rightarrow> pextreal"
   3.550 +lemma (in sigma_algebra) borel_measurable_extreal_neq[intro,simp]:
   3.551 +  fixes f :: "'a \<Rightarrow> extreal"
   3.552    assumes f: "f \<in> borel_measurable M"
   3.553    assumes g: "g \<in> borel_measurable M"
   3.554    shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   3.555 @@ -1405,20 +1293,28 @@
   3.556    thus ?thesis using f g by auto
   3.557  qed
   3.558  
   3.559 -lemma (in sigma_algebra) borel_measurable_pextreal_add[intro, simp]:
   3.560 -  fixes f :: "'a \<Rightarrow> pextreal"
   3.561 +lemma (in sigma_algebra) split_sets:
   3.562 +  "{x\<in>space M. P x \<or> Q x} = {x\<in>space M. P x} \<union> {x\<in>space M. Q x}"
   3.563 +  "{x\<in>space M. P x \<and> Q x} = {x\<in>space M. P x} \<inter> {x\<in>space M. Q x}"
   3.564 +  by auto
   3.565 +
   3.566 +lemma (in sigma_algebra) borel_measurable_extreal_add[intro, simp]:
   3.567 +  fixes f :: "'a \<Rightarrow> extreal"
   3.568    assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   3.569    shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   3.570  proof -
   3.571 -  have *: "(\<lambda>x. f x + g x) =
   3.572 -     (\<lambda>x. if f x = \<omega> then \<omega> else if g x = \<omega> then \<omega> else Real (real (f x) + real (g x)))"
   3.573 -     by (auto simp: fun_eq_iff pextreal_noteq_omega_Ex)
   3.574 -  show ?thesis using assms unfolding *
   3.575 -    by (auto intro!: measurable_If)
   3.576 +  { fix x assume "x \<in> space M" then have "f x + g x =
   3.577 +      (if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
   3.578 +        else if f x = -\<infinity> \<or> g x = -\<infinity> then -\<infinity>
   3.579 +        else extreal (real (f x) + real (g x)))"
   3.580 +      by (cases rule: extreal2_cases[of "f x" "g x"]) auto }
   3.581 +  with assms show ?thesis
   3.582 +    by (auto cong: measurable_cong simp: split_sets
   3.583 +             intro!: Un measurable_If measurable_sets)
   3.584  qed
   3.585  
   3.586 -lemma (in sigma_algebra) borel_measurable_pextreal_setsum[simp, intro]:
   3.587 -  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> pextreal"
   3.588 +lemma (in sigma_algebra) borel_measurable_extreal_setsum[simp, intro]:
   3.589 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
   3.590    assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   3.591    shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   3.592  proof cases
   3.593 @@ -1427,20 +1323,49 @@
   3.594      by induct auto
   3.595  qed (simp add: borel_measurable_const)
   3.596  
   3.597 -lemma (in sigma_algebra) borel_measurable_pextreal_times[intro, simp]:
   3.598 -  fixes f :: "'a \<Rightarrow> pextreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   3.599 +lemma abs_extreal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: extreal\<bar> = x"
   3.600 +  by (cases x) auto
   3.601 +
   3.602 +lemma abs_extreal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: extreal\<bar> = -x"
   3.603 +  by (cases x) auto
   3.604 +
   3.605 +lemma abs_extreal_pos[simp]: "0 \<le> \<bar>x :: extreal\<bar>"
   3.606 +  by (cases x) auto
   3.607 +
   3.608 +lemma (in sigma_algebra) borel_measurable_extreal_abs[intro, simp]:
   3.609 +  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
   3.610 +  shows "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
   3.611 +proof -
   3.612 +  { fix x have "\<bar>f x\<bar> = (if 0 \<le> f x then f x else - f x)" by auto }
   3.613 +  then show ?thesis using assms by (auto intro!: measurable_If)
   3.614 +qed
   3.615 +
   3.616 +lemma (in sigma_algebra) borel_measurable_extreal_times[intro, simp]:
   3.617 +  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   3.618    shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   3.619  proof -
   3.620 +  { fix f g :: "'a \<Rightarrow> extreal"
   3.621 +    assume b: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   3.622 +      and pos: "\<And>x. 0 \<le> f x" "\<And>x. 0 \<le> g x"
   3.623 +    { fix x have *: "f x * g x = (if f x = 0 \<or> g x = 0 then 0
   3.624 +        else if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
   3.625 +        else extreal (real (f x) * real (g x)))"
   3.626 +      apply (cases rule: extreal2_cases[of "f x" "g x"])
   3.627 +      using pos[of x] by auto }
   3.628 +    with b have "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   3.629 +      by (auto cong: measurable_cong simp: split_sets
   3.630 +               intro!: Un measurable_If measurable_sets) }
   3.631 +  note pos_times = this
   3.632    have *: "(\<lambda>x. f x * g x) =
   3.633 -     (\<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
   3.634 -      Real (real (f x) * real (g x)))"
   3.635 -     by (auto simp: fun_eq_iff pextreal_noteq_omega_Ex)
   3.636 +    (\<lambda>x. if 0 \<le> f x \<and> 0 \<le> g x \<or> f x < 0 \<and> g x < 0 then \<bar>f x\<bar> * \<bar>g x\<bar> else - (\<bar>f x\<bar> * \<bar>g x\<bar>))"
   3.637 +    by (auto simp: fun_eq_iff)
   3.638    show ?thesis using assms unfolding *
   3.639 -    by (auto intro!: measurable_If)
   3.640 +    by (intro measurable_If pos_times borel_measurable_uminus_extreal)
   3.641 +       (auto simp: split_sets intro!: Int)
   3.642  qed
   3.643  
   3.644 -lemma (in sigma_algebra) borel_measurable_pextreal_setprod[simp, intro]:
   3.645 -  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> pextreal"
   3.646 +lemma (in sigma_algebra) borel_measurable_extreal_setprod[simp, intro]:
   3.647 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
   3.648    assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   3.649    shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   3.650  proof cases
   3.651 @@ -1448,64 +1373,73 @@
   3.652    thus ?thesis using assms by induct auto
   3.653  qed simp
   3.654  
   3.655 -lemma (in sigma_algebra) borel_measurable_pextreal_min[simp, intro]:
   3.656 -  fixes f g :: "'a \<Rightarrow> pextreal"
   3.657 +lemma (in sigma_algebra) borel_measurable_extreal_min[simp, intro]:
   3.658 +  fixes f g :: "'a \<Rightarrow> extreal"
   3.659    assumes "f \<in> borel_measurable M"
   3.660    assumes "g \<in> borel_measurable M"
   3.661    shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
   3.662    using assms unfolding min_def by (auto intro!: measurable_If)
   3.663  
   3.664 -lemma (in sigma_algebra) borel_measurable_pextreal_max[simp, intro]:
   3.665 -  fixes f g :: "'a \<Rightarrow> pextreal"
   3.666 +lemma (in sigma_algebra) borel_measurable_extreal_max[simp, intro]:
   3.667 +  fixes f g :: "'a \<Rightarrow> extreal"
   3.668    assumes "f \<in> borel_measurable M"
   3.669    and "g \<in> borel_measurable M"
   3.670    shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   3.671    using assms unfolding max_def by (auto intro!: measurable_If)
   3.672  
   3.673  lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
   3.674 -  fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> pextreal"
   3.675 +  fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> extreal"
   3.676    assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
   3.677    shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
   3.678 -  unfolding borel_measurable_pextreal_iff_greater
   3.679 -proof safe
   3.680 +  unfolding borel_measurable_extreal_iff_ge
   3.681 +proof
   3.682    fix a
   3.683 -  have "{x\<in>space M. a < ?sup x} = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
   3.684 +  have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
   3.685      by (auto simp: less_SUP_iff SUPR_apply)
   3.686 -  then show "{x\<in>space M. a < ?sup x} \<in> sets M"
   3.687 +  then show "?sup -` {a<..} \<inter> space M \<in> sets M"
   3.688      using assms by auto
   3.689  qed
   3.690  
   3.691  lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
   3.692 -  fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> pextreal"
   3.693 +  fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> extreal"
   3.694    assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
   3.695    shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
   3.696 -  unfolding borel_measurable_pextreal_iff_less
   3.697 -proof safe
   3.698 +  unfolding borel_measurable_extreal_iff_less
   3.699 +proof
   3.700    fix a
   3.701 -  have "{x\<in>space M. ?inf x < a} = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
   3.702 +  have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
   3.703      by (auto simp: INF_less_iff INFI_apply)
   3.704 -  then show "{x\<in>space M. ?inf x < a} \<in> sets M"
   3.705 +  then show "?inf -` {..<a} \<inter> space M \<in> sets M"
   3.706      using assms by auto
   3.707  qed
   3.708  
   3.709 -lemma (in sigma_algebra) borel_measurable_pextreal_diff[simp, intro]:
   3.710 -  fixes f g :: "'a \<Rightarrow> pextreal"
   3.711 +lemma (in sigma_algebra) borel_measurable_liminf[simp, intro]:
   3.712 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
   3.713 +  assumes "\<And>i. f i \<in> borel_measurable M"
   3.714 +  shows "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   3.715 +  unfolding liminf_SUPR_INFI using assms by auto
   3.716 +
   3.717 +lemma (in sigma_algebra) borel_measurable_limsup[simp, intro]:
   3.718 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
   3.719 +  assumes "\<And>i. f i \<in> borel_measurable M"
   3.720 +  shows "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
   3.721 +  unfolding limsup_INFI_SUPR using assms by auto
   3.722 +
   3.723 +lemma (in sigma_algebra) borel_measurable_extreal_diff[simp, intro]:
   3.724 +  fixes f g :: "'a \<Rightarrow> extreal"
   3.725    assumes "f \<in> borel_measurable M"
   3.726    assumes "g \<in> borel_measurable M"
   3.727    shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   3.728 -  unfolding borel_measurable_pextreal_iff_greater
   3.729 -proof safe
   3.730 -  fix a
   3.731 -  have "{x \<in> space M. a < f x - g x} = {x \<in> space M. g x + a < f x}"
   3.732 -    by (simp add: pextreal_less_minus_iff)
   3.733 -  then show "{x \<in> space M. a < f x - g x} \<in> sets M"
   3.734 -    using assms by auto
   3.735 -qed
   3.736 +  unfolding minus_extreal_def using assms by auto
   3.737  
   3.738  lemma (in sigma_algebra) borel_measurable_psuminf[simp, intro]:
   3.739 -  assumes "\<And>i. f i \<in> borel_measurable M"
   3.740 -  shows "(\<lambda>x. (\<Sum>\<^isub>\<infinity> i. f i x)) \<in> borel_measurable M"
   3.741 -  using assms unfolding psuminf_def by auto
   3.742 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
   3.743 +  assumes "\<And>i. f i \<in> borel_measurable M" and pos: "\<And>i x. x \<in> space M \<Longrightarrow> 0 \<le> f i x"
   3.744 +  shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
   3.745 +  apply (subst measurable_cong)
   3.746 +  apply (subst suminf_extreal_eq_SUPR)
   3.747 +  apply (rule pos)
   3.748 +  using assms by auto
   3.749  
   3.750  section "LIMSEQ is borel measurable"
   3.751  
   3.752 @@ -1515,28 +1449,11 @@
   3.753    and u: "\<And>i. u i \<in> borel_measurable M"
   3.754    shows "u' \<in> borel_measurable M"
   3.755  proof -
   3.756 -  let "?pu x i" = "max (u i x) 0"
   3.757 -  let "?nu x i" = "max (- u i x) 0"
   3.758 -  { fix x assume x: "x \<in> space M"
   3.759 -    have "(?pu x) ----> max (u' x) 0"
   3.760 -      "(?nu x) ----> max (- u' x) 0"
   3.761 -      using u'[OF x] by (auto intro!: LIMSEQ_max LIMSEQ_minus)
   3.762 -    from LIMSEQ_imp_lim_INF[OF _ this(1)] LIMSEQ_imp_lim_INF[OF _ this(2)]
   3.763 -    have "(SUP n. INF m. Real (u (n + m) x)) = Real (u' x)"
   3.764 -      "(SUP n. INF m. Real (- u (n + m) x)) = Real (- u' x)"
   3.765 -      by (simp_all add: Real_max'[symmetric]) }
   3.766 -  note eq = this
   3.767 -  have *: "\<And>x. real (Real (u' x)) - real (Real (- u' x)) = u' x"
   3.768 +  have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. extreal (u n x)) = extreal (u' x)"
   3.769 +    using u' by (simp add: lim_imp_Liminf trivial_limit_sequentially lim_extreal)
   3.770 +  moreover from u have "(\<lambda>x. liminf (\<lambda>n. extreal (u n x))) \<in> borel_measurable M"
   3.771      by auto
   3.772 -  have "(\<lambda>x. SUP n. INF m. Real (u (n + m) x)) \<in> borel_measurable M"
   3.773 -       "(\<lambda>x. SUP n. INF m. Real (- u (n + m) x)) \<in> borel_measurable M"
   3.774 -    using u by auto
   3.775 -  with eq[THEN measurable_cong, of M "\<lambda>x. x" borel]
   3.776 -  have "(\<lambda>x. Real (u' x)) \<in> borel_measurable M"
   3.777 -       "(\<lambda>x. Real (- u' x)) \<in> borel_measurable M" by auto
   3.778 -  note this[THEN borel_measurable_real]
   3.779 -  from borel_measurable_diff[OF this]
   3.780 -  show ?thesis unfolding * .
   3.781 +  ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_extreal_iff)
   3.782  qed
   3.783  
   3.784  end
     4.1 --- a/src/HOL/Probability/Caratheodory.thy	Mon Mar 14 14:37:47 2011 +0100
     4.2 +++ b/src/HOL/Probability/Caratheodory.thy	Mon Mar 14 14:37:49 2011 +0100
     4.3 @@ -1,36 +1,66 @@
     4.4  header {*Caratheodory Extension Theorem*}
     4.5  
     4.6  theory Caratheodory
     4.7 -  imports Sigma_Algebra Positive_Extended_Real
     4.8 +  imports Sigma_Algebra Extended_Real_Limits
     4.9  begin
    4.10  
    4.11 +lemma suminf_extreal_2dimen:
    4.12 +  fixes f:: "nat \<times> nat \<Rightarrow> extreal"
    4.13 +  assumes pos: "\<And>p. 0 \<le> f p"
    4.14 +  assumes "\<And>m. g m = (\<Sum>n. f (m,n))"
    4.15 +  shows "(\<Sum>i. f (prod_decode i)) = suminf g"
    4.16 +proof -
    4.17 +  have g_def: "g = (\<lambda>m. (\<Sum>n. f (m,n)))"
    4.18 +    using assms by (simp add: fun_eq_iff)
    4.19 +  have reindex: "\<And>B. (\<Sum>x\<in>B. f (prod_decode x)) = setsum f (prod_decode ` B)"
    4.20 +    by (simp add: setsum_reindex[OF inj_prod_decode] comp_def)
    4.21 +  { fix n
    4.22 +    let ?M = "\<lambda>f. Suc (Max (f ` prod_decode ` {..<n}))"
    4.23 +    { fix a b x assume "x < n" and [symmetric]: "(a, b) = prod_decode x"
    4.24 +      then have "a < ?M fst" "b < ?M snd"
    4.25 +        by (auto intro!: Max_ge le_imp_less_Suc image_eqI) }
    4.26 +    then have "setsum f (prod_decode ` {..<n}) \<le> setsum f ({..<?M fst} \<times> {..<?M snd})"
    4.27 +      by (auto intro!: setsum_mono3 simp: pos)
    4.28 +    then have "\<exists>a b. setsum f (prod_decode ` {..<n}) \<le> setsum f ({..<a} \<times> {..<b})" by auto }
    4.29 +  moreover
    4.30 +  { fix a b
    4.31 +    let ?M = "prod_decode ` {..<Suc (Max (prod_encode ` ({..<a} \<times> {..<b})))}"
    4.32 +    { fix a' b' assume "a' < a" "b' < b" then have "(a', b') \<in> ?M"
    4.33 +        by (auto intro!: Max_ge le_imp_less_Suc image_eqI[where x="prod_encode (a', b')"]) }
    4.34 +    then have "setsum f ({..<a} \<times> {..<b}) \<le> setsum f ?M"
    4.35 +      by (auto intro!: setsum_mono3 simp: pos) }
    4.36 +  ultimately
    4.37 +  show ?thesis unfolding g_def using pos
    4.38 +    by (auto intro!: SUPR_eq  simp: setsum_cartesian_product reindex le_SUPI2
    4.39 +                     setsum_nonneg suminf_extreal_eq_SUPR SUPR_pair
    4.40 +                     SUPR_extreal_setsum[symmetric] incseq_setsumI setsum_nonneg)
    4.41 +qed
    4.42 +
    4.43  text{*From the Hurd/Coble measure theory development, translated by Lawrence Paulson.*}
    4.44  
    4.45  subsection {* Measure Spaces *}
    4.46  
    4.47  record 'a measure_space = "'a algebra" +
    4.48 -  measure :: "'a set \<Rightarrow> pextreal"
    4.49 +  measure :: "'a set \<Rightarrow> extreal"
    4.50  
    4.51 -definition positive where "positive M f \<longleftrightarrow> f {} = (0::pextreal)"
    4.52 -  -- "Positive is enforced by the type"
    4.53 +definition positive where "positive M f \<longleftrightarrow> f {} = (0::extreal) \<and> (\<forall>A\<in>sets M. 0 \<le> f A)"
    4.54  
    4.55  definition additive where "additive M f \<longleftrightarrow>
    4.56    (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) = f x + f y)"
    4.57  
    4.58 -definition countably_additive where "countably_additive M f \<longleftrightarrow>
    4.59 -  (\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow>
    4.60 -    (\<Sum>\<^isub>\<infinity> n. f (A n)) = f (\<Union>i. A i))"
    4.61 +definition countably_additive :: "('a, 'b) algebra_scheme \<Rightarrow> ('a set \<Rightarrow> extreal) \<Rightarrow> bool" where
    4.62 +  "countably_additive M f \<longleftrightarrow> (\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow>
    4.63 +    (\<Sum>i. f (A i)) = f (\<Union>i. A i))"
    4.64  
    4.65  definition increasing where "increasing M f \<longleftrightarrow>
    4.66    (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<subseteq> y \<longrightarrow> f x \<le> f y)"
    4.67  
    4.68  definition subadditive where "subadditive M f \<longleftrightarrow>
    4.69 -  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow>
    4.70 -    f (x \<union> y) \<le> f x + f y)"
    4.71 +  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) \<le> f x + f y)"
    4.72  
    4.73  definition countably_subadditive where "countably_subadditive M f \<longleftrightarrow>
    4.74    (\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow>
    4.75 -    f (\<Union>i. A i) \<le> (\<Sum>\<^isub>\<infinity> n. f (A n)))"
    4.76 +    (f (\<Union>i. A i) \<le> (\<Sum>i. f (A i))))"
    4.77  
    4.78  definition lambda_system where "lambda_system M f = {l \<in> sets M.
    4.79    \<forall>x \<in> sets M. f (l \<inter> x) + f ((space M - l) \<inter> x) = f x}"
    4.80 @@ -39,14 +69,19 @@
    4.81    positive M f \<and> increasing M f \<and> countably_subadditive M f"
    4.82  
    4.83  definition measure_set where "measure_set M f X = {r.
    4.84 -  \<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> X \<subseteq> (\<Union>i. A i) \<and> (\<Sum>\<^isub>\<infinity> i. f (A i)) = r}"
    4.85 +  \<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> X \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
    4.86  
    4.87  locale measure_space = sigma_algebra M for M :: "('a, 'b) measure_space_scheme" +
    4.88 -  assumes empty_measure [simp]: "measure M {} = 0"
    4.89 +  assumes measure_positive: "positive M (measure M)"
    4.90        and ca: "countably_additive M (measure M)"
    4.91  
    4.92  abbreviation (in measure_space) "\<mu> \<equiv> measure M"
    4.93  
    4.94 +lemma (in measure_space)
    4.95 +  shows empty_measure[simp, intro]: "\<mu> {} = 0"
    4.96 +  and positive_measure[simp, intro!]: "\<And>A. A \<in> sets M \<Longrightarrow> 0 \<le> \<mu> A"
    4.97 +  using measure_positive unfolding positive_def by auto
    4.98 +
    4.99  lemma increasingD:
   4.100    "increasing M f \<Longrightarrow> x \<subseteq> y \<Longrightarrow> x\<in>sets M \<Longrightarrow> y\<in>sets M \<Longrightarrow> f x \<le> f y"
   4.101    by (auto simp add: increasing_def)
   4.102 @@ -61,39 +96,30 @@
   4.103      \<Longrightarrow> f (x \<union> y) = f x + f y"
   4.104    by (auto simp add: additive_def)
   4.105  
   4.106 -lemma countably_additiveD:
   4.107 -  "countably_additive M f \<Longrightarrow> range A \<subseteq> sets M \<Longrightarrow> disjoint_family A
   4.108 -    \<Longrightarrow> (\<Union>i. A i) \<in> sets M \<Longrightarrow> (\<Sum>\<^isub>\<infinity> n. f (A n)) = f (\<Union>i. A i)"
   4.109 -  by (simp add: countably_additive_def)
   4.110 -
   4.111 -lemma countably_subadditiveD:
   4.112 -  "countably_subadditive M f \<Longrightarrow> range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow>
   4.113 -   (\<Union>i. A i) \<in> sets M \<Longrightarrow> f (\<Union>i. A i) \<le> psuminf (f o A)"
   4.114 -  by (auto simp add: countably_subadditive_def o_def)
   4.115 -
   4.116  lemma countably_additiveI:
   4.117 -  "(\<And>A. range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Union>i. A i) \<in> sets M
   4.118 -    \<Longrightarrow> (\<Sum>\<^isub>\<infinity> n. f (A n)) = f (\<Union>i. A i)) \<Longrightarrow> countably_additive M f"
   4.119 -  by (simp add: countably_additive_def)
   4.120 +  assumes "\<And>A x. range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Union>i. A i) \<in> sets M
   4.121 +    \<Longrightarrow> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
   4.122 +  shows "countably_additive M f"
   4.123 +  using assms by (simp add: countably_additive_def)
   4.124  
   4.125  section "Extend binary sets"
   4.126  
   4.127  lemma LIMSEQ_binaryset:
   4.128    assumes f: "f {} = 0"
   4.129 -  shows  "(\<lambda>n. \<Sum>i = 0..<n. f (binaryset A B i)) ----> f A + f B"
   4.130 +  shows  "(\<lambda>n. \<Sum>i<n. f (binaryset A B i)) ----> f A + f B"
   4.131  proof -
   4.132 -  have "(\<lambda>n. \<Sum>i = 0..< Suc (Suc n). f (binaryset A B i)) = (\<lambda>n. f A + f B)"
   4.133 +  have "(\<lambda>n. \<Sum>i < Suc (Suc n). f (binaryset A B i)) = (\<lambda>n. f A + f B)"
   4.134      proof
   4.135        fix n
   4.136 -      show "(\<Sum>i\<Colon>nat = 0\<Colon>nat..<Suc (Suc n). f (binaryset A B i)) = f A + f B"
   4.137 +      show "(\<Sum>i < Suc (Suc n). f (binaryset A B i)) = f A + f B"
   4.138          by (induct n)  (auto simp add: binaryset_def f)
   4.139      qed
   4.140    moreover
   4.141    have "... ----> f A + f B" by (rule LIMSEQ_const)
   4.142    ultimately
   4.143 -  have "(\<lambda>n. \<Sum>i = 0..< Suc (Suc n). f (binaryset A B i)) ----> f A + f B"
   4.144 +  have "(\<lambda>n. \<Sum>i< Suc (Suc n). f (binaryset A B i)) ----> f A + f B"
   4.145      by metis
   4.146 -  hence "(\<lambda>n. \<Sum>i = 0..< n+2. f (binaryset A B i)) ----> f A + f B"
   4.147 +  hence "(\<lambda>n. \<Sum>i< n+2. f (binaryset A B i)) ----> f A + f B"
   4.148      by simp
   4.149    thus ?thesis by (rule LIMSEQ_offset [where k=2])
   4.150  qed
   4.151 @@ -101,28 +127,13 @@
   4.152  lemma binaryset_sums:
   4.153    assumes f: "f {} = 0"
   4.154    shows  "(\<lambda>n. f (binaryset A B n)) sums (f A + f B)"
   4.155 -    by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f])
   4.156 +    by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f] atLeast0LessThan)
   4.157  
   4.158  lemma suminf_binaryset_eq:
   4.159 -  fixes f :: "'a set \<Rightarrow> real"
   4.160 +  fixes f :: "'a set \<Rightarrow> 'b::{comm_monoid_add, t2_space}"
   4.161    shows "f {} = 0 \<Longrightarrow> (\<Sum>n. f (binaryset A B n)) = f A + f B"
   4.162    by (metis binaryset_sums sums_unique)
   4.163  
   4.164 -lemma binaryset_psuminf:
   4.165 -  assumes "f {} = 0"
   4.166 -  shows "(\<Sum>\<^isub>\<infinity> n. f (binaryset A B n)) = f A + f B" (is "?suminf = ?sum")
   4.167 -proof -
   4.168 -  have *: "{..<2} = {0, 1::nat}" by auto
   4.169 -  have "\<forall>n\<ge>2. f (binaryset A B n) = 0"
   4.170 -    unfolding binaryset_def
   4.171 -    using assms by auto
   4.172 -  hence "?suminf = (\<Sum>N<2. f (binaryset A B N))"
   4.173 -    by (rule psuminf_finite)
   4.174 -  also have "... = ?sum" unfolding * binaryset_def
   4.175 -    by simp
   4.176 -  finally show ?thesis .
   4.177 -qed
   4.178 -
   4.179  subsection {* Lambda Systems *}
   4.180  
   4.181  lemma (in algebra) lambda_system_eq:
   4.182 @@ -144,7 +155,7 @@
   4.183    by (simp add: lambda_system_def)
   4.184  
   4.185  lemma (in algebra) lambda_system_Compl:
   4.186 -  fixes f:: "'a set \<Rightarrow> pextreal"
   4.187 +  fixes f:: "'a set \<Rightarrow> extreal"
   4.188    assumes x: "x \<in> lambda_system M f"
   4.189    shows "space M - x \<in> lambda_system M f"
   4.190  proof -
   4.191 @@ -157,7 +168,7 @@
   4.192  qed
   4.193  
   4.194  lemma (in algebra) lambda_system_Int:
   4.195 -  fixes f:: "'a set \<Rightarrow> pextreal"
   4.196 +  fixes f:: "'a set \<Rightarrow> extreal"
   4.197    assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
   4.198    shows "x \<inter> y \<in> lambda_system M f"
   4.199  proof -
   4.200 @@ -191,7 +202,7 @@
   4.201  qed
   4.202  
   4.203  lemma (in algebra) lambda_system_Un:
   4.204 -  fixes f:: "'a set \<Rightarrow> pextreal"
   4.205 +  fixes f:: "'a set \<Rightarrow> extreal"
   4.206    assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
   4.207    shows "x \<union> y \<in> lambda_system M f"
   4.208  proof -
   4.209 @@ -250,53 +261,54 @@
   4.210      by (auto simp add: disjoint_family_on_def binaryset_def)
   4.211    hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
   4.212           (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
   4.213 -         f (\<Union>i. binaryset x y i) \<le> (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
   4.214 -    using cs by (simp add: countably_subadditive_def)
   4.215 +         f (\<Union>i. binaryset x y i) \<le> (\<Sum> n. f (binaryset x y n))"
   4.216 +    using cs by (auto simp add: countably_subadditive_def)
   4.217    hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
   4.218 -         f (x \<union> y) \<le> (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
   4.219 +         f (x \<union> y) \<le> (\<Sum> n. f (binaryset x y n))"
   4.220      by (simp add: range_binaryset_eq UN_binaryset_eq)
   4.221    thus "f (x \<union> y) \<le>  f x + f y" using f x y
   4.222 -    by (auto simp add: Un o_def binaryset_psuminf positive_def)
   4.223 +    by (auto simp add: Un o_def suminf_binaryset_eq positive_def)
   4.224  qed
   4.225  
   4.226  lemma (in algebra) additive_sum:
   4.227    fixes A:: "nat \<Rightarrow> 'a set"
   4.228 -  assumes f: "positive M f" and ad: "additive M f"
   4.229 +  assumes f: "positive M f" and ad: "additive M f" and "finite S"
   4.230        and A: "range A \<subseteq> sets M"
   4.231 -      and disj: "disjoint_family A"
   4.232 -  shows  "setsum (f \<circ> A) {0..<n} = f (\<Union>i\<in>{0..<n}. A i)"
   4.233 -proof (induct n)
   4.234 -  case 0 show ?case using f by (simp add: positive_def)
   4.235 +      and disj: "disjoint_family_on A S"
   4.236 +  shows  "(\<Sum>i\<in>S. f (A i)) = f (\<Union>i\<in>S. A i)"
   4.237 +using `finite S` disj proof induct
   4.238 +  case empty show ?case using f by (simp add: positive_def)
   4.239  next
   4.240 -  case (Suc n)
   4.241 -  have "A n \<inter> (\<Union>i\<in>{0..<n}. A i) = {}" using disj
   4.242 -    by (auto simp add: disjoint_family_on_def neq_iff) blast
   4.243 +  case (insert s S)
   4.244 +  then have "A s \<inter> (\<Union>i\<in>S. A i) = {}"
   4.245 +    by (auto simp add: disjoint_family_on_def neq_iff)
   4.246    moreover
   4.247 -  have "A n \<in> sets M" using A by blast
   4.248 -  moreover have "(\<Union>i\<in>{0..<n}. A i) \<in> sets M"
   4.249 -    by (metis A UNION_in_sets atLeast0LessThan)
   4.250 +  have "A s \<in> sets M" using A by blast
   4.251 +  moreover have "(\<Union>i\<in>S. A i) \<in> sets M"
   4.252 +    using A `finite S` by auto
   4.253    moreover
   4.254 -  ultimately have "f (A n \<union> (\<Union>i\<in>{0..<n}. A i)) = f (A n) + f(\<Union>i\<in>{0..<n}. A i)"
   4.255 +  ultimately have "f (A s \<union> (\<Union>i\<in>S. A i)) = f (A s) + f(\<Union>i\<in>S. A i)"
   4.256      using ad UNION_in_sets A by (auto simp add: additive_def)
   4.257 -  with Suc.hyps show ?case using ad
   4.258 -    by (auto simp add: atLeastLessThanSuc additive_def)
   4.259 +  with insert show ?case using ad disjoint_family_on_mono[of S "insert s S" A]
   4.260 +    by (auto simp add: additive_def subset_insertI)
   4.261  qed
   4.262  
   4.263  lemma (in algebra) increasing_additive_bound:
   4.264 -  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> pextreal"
   4.265 +  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> extreal"
   4.266    assumes f: "positive M f" and ad: "additive M f"
   4.267        and inc: "increasing M f"
   4.268        and A: "range A \<subseteq> sets M"
   4.269        and disj: "disjoint_family A"
   4.270 -  shows  "psuminf (f \<circ> A) \<le> f (space M)"
   4.271 -proof (safe intro!: psuminf_bound)
   4.272 +  shows  "(\<Sum>i. f (A i)) \<le> f (space M)"
   4.273 +proof (safe intro!: suminf_bound)
   4.274    fix N
   4.275 -  have "setsum (f \<circ> A) {0..<N} = f (\<Union>i\<in>{0..<N}. A i)"
   4.276 -    by (rule additive_sum [OF f ad A disj])
   4.277 +  note disj_N = disjoint_family_on_mono[OF _ disj, of "{..<N}"]
   4.278 +  have "(\<Sum>i<N. f (A i)) = f (\<Union>i\<in>{..<N}. A i)"
   4.279 +    by (rule additive_sum [OF f ad _ A]) (auto simp: disj_N)
   4.280    also have "... \<le> f (space M)" using space_closed A
   4.281 -    by (blast intro: increasingD [OF inc] UNION_in_sets top)
   4.282 -  finally show "setsum (f \<circ> A) {..<N} \<le> f (space M)" by (simp add: atLeast0LessThan)
   4.283 -qed
   4.284 +    by (intro increasingD[OF inc] finite_UN) auto
   4.285 +  finally show "(\<Sum>i<N. f (A i)) \<le> f (space M)" by simp
   4.286 +qed (insert f A, auto simp: positive_def)
   4.287  
   4.288  lemma lambda_system_increasing:
   4.289   "increasing M f \<Longrightarrow> increasing (M (|sets := lambda_system M f|)) f"
   4.290 @@ -307,7 +319,7 @@
   4.291    by (simp add: positive_def lambda_system_def)
   4.292  
   4.293  lemma (in algebra) lambda_system_strong_sum:
   4.294 -  fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> pextreal"
   4.295 +  fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> extreal"
   4.296    assumes f: "positive M f" and a: "a \<in> sets M"
   4.297        and A: "range A \<subseteq> lambda_system M f"
   4.298        and disj: "disjoint_family A"
   4.299 @@ -331,7 +343,7 @@
   4.300    assumes oms: "outer_measure_space M f"
   4.301        and A: "range A \<subseteq> lambda_system M f"
   4.302        and disj: "disjoint_family A"
   4.303 -  shows  "(\<Union>i. A i) \<in> lambda_system M f \<and> psuminf (f \<circ> A) = f (\<Union>i. A i)"
   4.304 +  shows  "(\<Union>i. A i) \<in> lambda_system M f \<and> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
   4.305  proof -
   4.306    have pos: "positive M f" and inc: "increasing M f"
   4.307     and csa: "countably_subadditive M f"
   4.308 @@ -347,15 +359,16 @@
   4.309  
   4.310    have U_in: "(\<Union>i. A i) \<in> sets M"
   4.311      by (metis A'' countable_UN)
   4.312 -  have U_eq: "f (\<Union>i. A i) = psuminf (f o A)"
   4.313 +  have U_eq: "f (\<Union>i. A i) = (\<Sum>i. f (A i))"
   4.314    proof (rule antisym)
   4.315 -    show "f (\<Union>i. A i) \<le> psuminf (f \<circ> A)"
   4.316 -      by (rule countably_subadditiveD [OF csa A'' disj U_in])
   4.317 -    show "psuminf (f \<circ> A) \<le> f (\<Union>i. A i)"
   4.318 -      by (rule psuminf_bound, unfold atLeast0LessThan[symmetric])
   4.319 -         (metis algebra.additive_sum [OF alg_ls] pos disj UN_Un Un_UNIV_right
   4.320 -                lambda_system_positive lambda_system_additive
   4.321 -                subset_Un_eq increasingD [OF inc] A' A'' UNION_in_sets U_in)
   4.322 +    show "f (\<Union>i. A i) \<le> (\<Sum>i. f (A i))"
   4.323 +      using csa[unfolded countably_subadditive_def] A'' disj U_in by auto
   4.324 +    have *: "\<And>i. 0 \<le> f (A i)" using pos A'' unfolding positive_def by auto
   4.325 +    have dis: "\<And>N. disjoint_family_on A {..<N}" by (intro disjoint_family_on_mono[OF _ disj]) auto
   4.326 +    show "(\<Sum>i. f (A i)) \<le> f (\<Union>i. A i)"
   4.327 +      using algebra.additive_sum [OF alg_ls lambda_system_positive[OF pos] lambda_system_additive _ A' dis]
   4.328 +      using A''
   4.329 +      by (intro suminf_bound[OF _ *]) (auto intro!: increasingD[OF inc] allI countable_UN)
   4.330    qed
   4.331    {
   4.332      fix a
   4.333 @@ -373,15 +386,15 @@
   4.334          have "a \<inter> (\<Union>i. A i) \<in> sets M"
   4.335            by (metis Int U_in a)
   4.336          ultimately
   4.337 -        have "f (a \<inter> (\<Union>i. A i)) \<le> psuminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A)"
   4.338 -          using countably_subadditiveD [OF csa, of "(\<lambda>i. a \<inter> A i)"]
   4.339 +        have "f (a \<inter> (\<Union>i. A i)) \<le> (\<Sum>i. f (a \<inter> A i))"
   4.340 +          using csa[unfolded countably_subadditive_def, rule_format, of "(\<lambda>i. a \<inter> A i)"]
   4.341            by (simp add: o_def)
   4.342          hence "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) \<le>
   4.343 -            psuminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) + f (a - (\<Union>i. A i))"
   4.344 +            (\<Sum>i. f (a \<inter> A i)) + f (a - (\<Union>i. A i))"
   4.345            by (rule add_right_mono)
   4.346          moreover
   4.347 -        have "psuminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) + f (a - (\<Union>i. A i)) \<le> f a"
   4.348 -          proof (safe intro!: psuminf_bound_add)
   4.349 +        have "(\<Sum>i. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
   4.350 +          proof (intro suminf_bound_add allI)
   4.351              fix n
   4.352              have UNION_in: "(\<Union>i\<in>{0..<n}. A i) \<in> sets M"
   4.353                by (metis A'' UNION_in_sets)
   4.354 @@ -395,8 +408,14 @@
   4.355              have "f (a - (\<Union>i. A i)) \<le> f (a - (\<Union>i\<in>{0..<n}. A i))"
   4.356                by (blast intro: increasingD [OF inc] UNION_eq_Union_image
   4.357                                 UNION_in U_in)
   4.358 -            thus "setsum (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) {..<n} + f (a - (\<Union>i. A i)) \<le> f a"
   4.359 +            thus "(\<Sum>i<n. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
   4.360                by (simp add: lambda_system_strong_sum pos A disj eq_fa add_left_mono atLeast0LessThan[symmetric])
   4.361 +          next
   4.362 +            have "\<And>i. a \<inter> A i \<in> sets M" using A'' by auto
   4.363 +            then show "\<And>i. 0 \<le> f (a \<inter> A i)" using pos[unfolded positive_def] by auto
   4.364 +            have "\<And>i. a - (\<Union>i. A i) \<in> sets M" using A'' by auto
   4.365 +            then have "\<And>i. 0 \<le> f (a - (\<Union>i. A i))" using pos[unfolded positive_def] by auto
   4.366 +            then show "f (a - (\<Union>i. A i)) \<noteq> -\<infinity>" by auto
   4.367            qed
   4.368          ultimately show "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) \<le> f a"
   4.369            by (rule order_trans)
   4.370 @@ -443,12 +462,14 @@
   4.371  proof (auto simp add: increasing_def)
   4.372    fix x y
   4.373    assume xy: "x \<in> sets M" "y \<in> sets M" "x \<subseteq> y"
   4.374 -  have "f x \<le> f x + f (y-x)" ..
   4.375 +  then have "y - x \<in> sets M" by auto
   4.376 +  then have "0 \<le> f (y-x)" using posf[unfolded positive_def] by auto
   4.377 +  then have "f x + 0 \<le> f x + f (y-x)" by (intro add_left_mono) auto
   4.378    also have "... = f (x \<union> (y-x))" using addf
   4.379      by (auto simp add: additive_def) (metis Diff_disjoint Un_Diff_cancel Diff xy(1,2))
   4.380    also have "... = f y"
   4.381      by (metis Un_Diff_cancel Un_absorb1 xy(3))
   4.382 -  finally show "f x \<le> f y" .
   4.383 +  finally show "f x \<le> f y" by simp
   4.384  qed
   4.385  
   4.386  lemma (in algebra) countably_additive_additive:
   4.387 @@ -461,27 +482,27 @@
   4.388      by (auto simp add: disjoint_family_on_def binaryset_def)
   4.389    hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
   4.390           (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
   4.391 -         f (\<Union>i. binaryset x y i) = (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
   4.392 +         f (\<Union>i. binaryset x y i) = (\<Sum> n. f (binaryset x y n))"
   4.393      using ca
   4.394      by (simp add: countably_additive_def)
   4.395    hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
   4.396 -         f (x \<union> y) = (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
   4.397 +         f (x \<union> y) = (\<Sum>n. f (binaryset x y n))"
   4.398      by (simp add: range_binaryset_eq UN_binaryset_eq)
   4.399    thus "f (x \<union> y) = f x + f y" using posf x y
   4.400 -    by (auto simp add: Un binaryset_psuminf positive_def)
   4.401 +    by (auto simp add: Un suminf_binaryset_eq positive_def)
   4.402  qed
   4.403  
   4.404  lemma inf_measure_nonempty:
   4.405    assumes f: "positive M f" and b: "b \<in> sets M" and a: "a \<subseteq> b" "{} \<in> sets M"
   4.406    shows "f b \<in> measure_set M f a"
   4.407  proof -
   4.408 -  have "psuminf (f \<circ> (\<lambda>i. {})(0 := b)) = setsum (f \<circ> (\<lambda>i. {})(0 := b)) {..<1::nat}"
   4.409 -    by (rule psuminf_finite) (simp add: f[unfolded positive_def])
   4.410 +  let ?A = "\<lambda>i::nat. (if i = 0 then b else {})"
   4.411 +  have "(\<Sum>i. f (?A i)) = (\<Sum>i<1::nat. f (?A i))"
   4.412 +    by (rule suminf_finite) (simp add: f[unfolded positive_def])
   4.413    also have "... = f b"
   4.414      by simp
   4.415 -  finally have "psuminf (f \<circ> (\<lambda>i. {})(0 := b)) = f b" .
   4.416 -  thus ?thesis using assms
   4.417 -    by (auto intro!: exI [of _ "(\<lambda>i. {})(0 := b)"]
   4.418 +  finally show ?thesis using assms
   4.419 +    by (auto intro!: exI [of _ ?A]
   4.420               simp: measure_set_def disjoint_family_on_def split_if_mem2 comp_def)
   4.421  qed
   4.422  
   4.423 @@ -489,19 +510,19 @@
   4.424    assumes posf: "positive M f" and ca: "countably_additive M f"
   4.425        and s: "s \<in> sets M"
   4.426    shows "Inf (measure_set M f s) = f s"
   4.427 -  unfolding Inf_pextreal_def
   4.428 +  unfolding Inf_extreal_def
   4.429  proof (safe intro!: Greatest_equality)
   4.430    fix z
   4.431    assume z: "z \<in> measure_set M f s"
   4.432    from this obtain A where
   4.433      A: "range A \<subseteq> sets M" and disj: "disjoint_family A"
   4.434 -    and "s \<subseteq> (\<Union>x. A x)" and si: "psuminf (f \<circ> A) = z"
   4.435 +    and "s \<subseteq> (\<Union>x. A x)" and si: "(\<Sum>i. f (A i)) = z"
   4.436      by (auto simp add: measure_set_def comp_def)
   4.437    hence seq: "s = (\<Union>i. A i \<inter> s)" by blast
   4.438    have inc: "increasing M f"
   4.439      by (metis additive_increasing ca countably_additive_additive posf)
   4.440 -  have sums: "psuminf (\<lambda>i. f (A i \<inter> s)) = f (\<Union>i. A i \<inter> s)"
   4.441 -    proof (rule countably_additiveD [OF ca])
   4.442 +  have sums: "(\<Sum>i. f (A i \<inter> s)) = f (\<Union>i. A i \<inter> s)"
   4.443 +    proof (rule ca[unfolded countably_additive_def, rule_format])
   4.444        show "range (\<lambda>n. A n \<inter> s) \<subseteq> sets M" using A s
   4.445          by blast
   4.446        show "disjoint_family (\<lambda>n. A n \<inter> s)" using disj
   4.447 @@ -509,12 +530,14 @@
   4.448        show "(\<Union>i. A i \<inter> s) \<in> sets M" using A s
   4.449          by (metis UN_extend_simps(4) s seq)
   4.450      qed
   4.451 -  hence "f s = psuminf (\<lambda>i. f (A i \<inter> s))"
   4.452 +  hence "f s = (\<Sum>i. f (A i \<inter> s))"
   4.453      using seq [symmetric] by (simp add: sums_iff)
   4.454 -  also have "... \<le> psuminf (f \<circ> A)"
   4.455 -    proof (rule psuminf_le)
   4.456 -      fix n show "f (A n \<inter> s) \<le> (f \<circ> A) n" using A s
   4.457 +  also have "... \<le> (\<Sum>i. f (A i))"
   4.458 +    proof (rule suminf_le_pos)
   4.459 +      fix n show "f (A n \<inter> s) \<le> f (A n)" using A s
   4.460          by (force intro: increasingD [OF inc])
   4.461 +      fix N have "A N \<inter> s \<in> sets M"  using A s by auto
   4.462 +      then show "0 \<le> f (A N \<inter> s)" using posf unfolding positive_def by auto
   4.463      qed
   4.464    also have "... = z" by (rule si)
   4.465    finally show "f s \<le> z" .
   4.466 @@ -525,18 +548,40 @@
   4.467      by (blast intro: inf_measure_nonempty [of _ f, OF posf s subset_refl])
   4.468  qed
   4.469  
   4.470 +lemma measure_set_pos:
   4.471 +  assumes posf: "positive M f" "r \<in> measure_set M f X"
   4.472 +  shows "0 \<le> r"
   4.473 +proof -
   4.474 +  obtain A where "range A \<subseteq> sets M" and r: "r = (\<Sum>i. f (A i))"
   4.475 +    using `r \<in> measure_set M f X` unfolding measure_set_def by auto
   4.476 +  then show "0 \<le> r" using posf unfolding r positive_def
   4.477 +    by (intro suminf_0_le) auto
   4.478 +qed
   4.479 +
   4.480 +lemma inf_measure_pos:
   4.481 +  assumes posf: "positive M f"
   4.482 +  shows "0 \<le> Inf (measure_set M f X)"
   4.483 +proof (rule complete_lattice_class.Inf_greatest)
   4.484 +  fix r assume "r \<in> measure_set M f X" with posf show "0 \<le> r"
   4.485 +    by (rule measure_set_pos)
   4.486 +qed
   4.487 +
   4.488  lemma inf_measure_empty:
   4.489 -  assumes posf: "positive M f" "{} \<in> sets M"
   4.490 +  assumes posf: "positive M f" and "{} \<in> sets M"
   4.491    shows "Inf (measure_set M f {}) = 0"
   4.492  proof (rule antisym)
   4.493    show "Inf (measure_set M f {}) \<le> 0"
   4.494      by (metis complete_lattice_class.Inf_lower `{} \<in> sets M`
   4.495                inf_measure_nonempty[OF posf] subset_refl posf[unfolded positive_def])
   4.496 -qed simp
   4.497 +qed (rule inf_measure_pos[OF posf])
   4.498  
   4.499  lemma (in algebra) inf_measure_positive:
   4.500 -  "positive M f \<Longrightarrow> positive M (\<lambda>x. Inf (measure_set M f x))"
   4.501 -  by (simp add: positive_def inf_measure_empty)
   4.502 +  assumes p: "positive M f" and "{} \<in> sets M"
   4.503 +  shows "positive M (\<lambda>x. Inf (measure_set M f x))"
   4.504 +proof (unfold positive_def, intro conjI ballI)
   4.505 +  show "Inf (measure_set M f {}) = 0" using inf_measure_empty[OF assms] by auto
   4.506 +  fix A assume "A \<in> sets M"
   4.507 +qed (rule inf_measure_pos[OF p])
   4.508  
   4.509  lemma (in algebra) inf_measure_increasing:
   4.510    assumes posf: "positive M f"
   4.511 @@ -548,25 +593,25 @@
   4.512  apply (clarsimp simp add: measure_set_def, rule_tac x=A in exI, blast)
   4.513  done
   4.514  
   4.515 -
   4.516  lemma (in algebra) inf_measure_le:
   4.517    assumes posf: "positive M f" and inc: "increasing M f"
   4.518 -      and x: "x \<in> {r . \<exists>A. range A \<subseteq> sets M \<and> s \<subseteq> (\<Union>i. A i) \<and> psuminf (f \<circ> A) = r}"
   4.519 +      and x: "x \<in> {r . \<exists>A. range A \<subseteq> sets M \<and> s \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
   4.520    shows "Inf (measure_set M f s) \<le> x"
   4.521  proof -
   4.522 -  from x
   4.523    obtain A where A: "range A \<subseteq> sets M" and ss: "s \<subseteq> (\<Union>i. A i)"
   4.524 -             and xeq: "psuminf (f \<circ> A) = x"
   4.525 -    by auto
   4.526 +             and xeq: "(\<Sum>i. f (A i)) = x"
   4.527 +    using x by auto
   4.528    have dA: "range (disjointed A) \<subseteq> sets M"
   4.529      by (metis A range_disjointed_sets)
   4.530 -  have "\<forall>n.(f o disjointed A) n \<le> (f \<circ> A) n" unfolding comp_def
   4.531 +  have "\<forall>n. f (disjointed A n) \<le> f (A n)"
   4.532      by (metis increasingD [OF inc] UNIV_I dA image_subset_iff disjointed_subset A comp_def)
   4.533 -  hence sda: "psuminf (f o disjointed A) \<le> psuminf (f \<circ> A)"
   4.534 -    by (blast intro: psuminf_le)
   4.535 -  hence ley: "psuminf (f o disjointed A) \<le> x"
   4.536 +  moreover have "\<forall>i. 0 \<le> f (disjointed A i)"
   4.537 +    using posf dA unfolding positive_def by auto
   4.538 +  ultimately have sda: "(\<Sum>i. f (disjointed A i)) \<le> (\<Sum>i. f (A i))"
   4.539 +    by (blast intro!: suminf_le_pos)
   4.540 +  hence ley: "(\<Sum>i. f (disjointed A i)) \<le> x"
   4.541      by (metis xeq)
   4.542 -  hence y: "psuminf (f o disjointed A) \<in> measure_set M f s"
   4.543 +  hence y: "(\<Sum>i. f (disjointed A i)) \<in> measure_set M f s"
   4.544      apply (auto simp add: measure_set_def)
   4.545      apply (rule_tac x="disjointed A" in exI)
   4.546      apply (simp add: disjoint_family_disjointed UN_disjointed_eq ss dA comp_def)
   4.547 @@ -576,13 +621,16 @@
   4.548  qed
   4.549  
   4.550  lemma (in algebra) inf_measure_close:
   4.551 +  fixes e :: extreal
   4.552    assumes posf: "positive M f" and e: "0 < e" and ss: "s \<subseteq> (space M)"
   4.553    shows "\<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> s \<subseteq> (\<Union>i. A i) \<and>
   4.554 -               psuminf (f \<circ> A) \<le> Inf (measure_set M f s) + e"
   4.555 -proof (cases "Inf (measure_set M f s) = \<omega>")
   4.556 +               (\<Sum>i. f (A i)) \<le> Inf (measure_set M f s) + e"
   4.557 +proof (cases "Inf (measure_set M f s) = \<infinity>")
   4.558    case False
   4.559 +  then have fin: "\<bar>Inf (measure_set M f s)\<bar> \<noteq> \<infinity>"
   4.560 +    using inf_measure_pos[OF posf, of s] by auto
   4.561    obtain l where "l \<in> measure_set M f s" "l \<le> Inf (measure_set M f s) + e"
   4.562 -    using Inf_close[OF False e] by auto
   4.563 +    using Inf_extreal_close[OF fin e] by auto
   4.564    thus ?thesis
   4.565      by (auto intro!: exI[of _ l] simp: measure_set_def comp_def)
   4.566  next
   4.567 @@ -600,9 +648,8 @@
   4.568    shows "countably_subadditive (| space = space M, sets = Pow (space M) |)
   4.569                    (\<lambda>x. Inf (measure_set M f x))"
   4.570    unfolding countably_subadditive_def o_def
   4.571 -proof (safe, simp, rule pextreal_le_epsilon)
   4.572 -  fix A :: "nat \<Rightarrow> 'a set" and e :: pextreal
   4.573 -
   4.574 +proof (safe, simp, rule extreal_le_epsilon, safe)
   4.575 +  fix A :: "nat \<Rightarrow> 'a set" and e :: extreal
   4.576    let "?outer n" = "Inf (measure_set M f (A n))"
   4.577    assume A: "range A \<subseteq> Pow (space M)"
   4.578       and disj: "disjoint_family A"
   4.579 @@ -610,21 +657,27 @@
   4.580       and e: "0 < e"
   4.581    hence "\<exists>BB. \<forall>n. range (BB n) \<subseteq> sets M \<and> disjoint_family (BB n) \<and>
   4.582                     A n \<subseteq> (\<Union>i. BB n i) \<and>
   4.583 -                   psuminf (f o BB n) \<le> ?outer n + e * (1/2)^(Suc n)"
   4.584 -    apply (safe intro!: choice inf_measure_close [of f, OF posf _])
   4.585 -    using e sb by (cases e, auto simp add: not_le mult_pos_pos)
   4.586 +                   (\<Sum>i. f (BB n i)) \<le> ?outer n + e * (1/2)^(Suc n)"
   4.587 +    apply (safe intro!: choice inf_measure_close [of f, OF posf])
   4.588 +    using e sb by (cases e) (auto simp add: not_le mult_pos_pos one_extreal_def)
   4.589    then obtain BB
   4.590      where BB: "\<And>n. (range (BB n) \<subseteq> sets M)"
   4.591        and disjBB: "\<And>n. disjoint_family (BB n)"
   4.592        and sbBB: "\<And>n. A n \<subseteq> (\<Union>i. BB n i)"
   4.593 -      and BBle: "\<And>n. psuminf (f o BB n) \<le> ?outer n + e * (1/2)^(Suc n)"
   4.594 +      and BBle: "\<And>n. (\<Sum>i. f (BB n i)) \<le> ?outer n + e * (1/2)^(Suc n)"
   4.595      by auto blast
   4.596 -  have sll: "(\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n)) \<le> psuminf ?outer + e"
   4.597 +  have sll: "(\<Sum>n. \<Sum>i. (f (BB n i))) \<le> suminf ?outer + e"
   4.598      proof -
   4.599 -      have "(\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n)) \<le> (\<Sum>\<^isub>\<infinity> n. ?outer n + e*(1/2) ^ Suc n)"
   4.600 -        by (rule psuminf_le[OF BBle])
   4.601 -      also have "... = psuminf ?outer + e"
   4.602 -        using psuminf_half_series by simp
   4.603 +      have sum_eq_1: "(\<Sum>n. e*(1/2) ^ Suc n) = e"
   4.604 +        using suminf_half_series_extreal e
   4.605 +        by (simp add: extreal_zero_le_0_iff zero_le_divide_extreal suminf_cmult_extreal)
   4.606 +      have "\<And>n i. 0 \<le> f (BB n i)" using posf[unfolded positive_def] BB by auto
   4.607 +      then have "\<And>n. 0 \<le> (\<Sum>i. f (BB n i))" by (rule suminf_0_le)
   4.608 +      then have "(\<Sum>n. \<Sum>i. (f (BB n i))) \<le> (\<Sum>n. ?outer n + e*(1/2) ^ Suc n)"
   4.609 +        by (rule suminf_le_pos[OF BBle])
   4.610 +      also have "... = suminf ?outer + e"
   4.611 +        using sum_eq_1 inf_measure_pos[OF posf] e
   4.612 +        by (subst suminf_add_extreal) (auto simp add: extreal_zero_le_0_iff)
   4.613        finally show ?thesis .
   4.614      qed
   4.615    def C \<equiv> "(split BB) o prod_decode"
   4.616 @@ -644,23 +697,25 @@
   4.617      qed
   4.618    have "(f \<circ> C) = (f \<circ> (\<lambda>(x, y). BB x y)) \<circ> prod_decode"
   4.619      by (rule ext)  (auto simp add: C_def)
   4.620 -  moreover have "psuminf ... = (\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n))" using BBle
   4.621 -    by (force intro!: psuminf_2dimen simp: o_def)
   4.622 -  ultimately have Csums: "psuminf (f \<circ> C) = (\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n))" by simp
   4.623 -  have "Inf (measure_set M f (\<Union>i. A i)) \<le> (\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n))"
   4.624 +  moreover have "suminf ... = (\<Sum>n. \<Sum>i. f (BB n i))" using BBle
   4.625 +    using BB posf[unfolded positive_def]
   4.626 +    by (force intro!: suminf_extreal_2dimen simp: o_def)
   4.627 +  ultimately have Csums: "(\<Sum>i. f (C i)) = (\<Sum>n. \<Sum>i. f (BB n i))" by (simp add: o_def)
   4.628 +  have "Inf (measure_set M f (\<Union>i. A i)) \<le> (\<Sum>n. \<Sum>i. f (BB n i))"
   4.629      apply (rule inf_measure_le [OF posf(1) inc], auto)
   4.630      apply (rule_tac x="C" in exI)
   4.631      apply (auto simp add: C sbC Csums)
   4.632      done
   4.633 -  also have "... \<le> (\<Sum>\<^isub>\<infinity>n. Inf (measure_set M f (A n))) + e" using sll
   4.634 +  also have "... \<le> (\<Sum>n. Inf (measure_set M f (A n))) + e" using sll
   4.635      by blast
   4.636 -  finally show "Inf (measure_set M f (\<Union>i. A i)) \<le> psuminf ?outer + e" .
   4.637 +  finally show "Inf (measure_set M f (\<Union>i. A i)) \<le> suminf ?outer + e" .
   4.638  qed
   4.639  
   4.640  lemma (in algebra) inf_measure_outer:
   4.641    "\<lbrakk> positive M f ; increasing M f \<rbrakk>
   4.642     \<Longrightarrow> outer_measure_space \<lparr> space = space M, sets = Pow (space M) \<rparr>
   4.643                            (\<lambda>x. Inf (measure_set M f x))"
   4.644 +  using inf_measure_pos[of M f]
   4.645    by (simp add: outer_measure_space_def inf_measure_empty
   4.646                  inf_measure_increasing inf_measure_countably_subadditive positive_def)
   4.647  
   4.648 @@ -680,13 +735,13 @@
   4.649      by blast
   4.650    have "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
   4.651          \<le> Inf (measure_set M f s)"
   4.652 -    proof (rule pextreal_le_epsilon)
   4.653 -      fix e :: pextreal
   4.654 +    proof (rule extreal_le_epsilon, intro allI impI)
   4.655 +      fix e :: extreal
   4.656        assume e: "0 < e"
   4.657        from inf_measure_close [of f, OF posf e s]
   4.658        obtain A where A: "range A \<subseteq> sets M" and disj: "disjoint_family A"
   4.659                   and sUN: "s \<subseteq> (\<Union>i. A i)"
   4.660 -                 and l: "psuminf (f \<circ> A) \<le> Inf (measure_set M f s) + e"
   4.661 +                 and l: "(\<Sum>i. f (A i)) \<le> Inf (measure_set M f s) + e"
   4.662          by auto
   4.663        have [simp]: "!!x. x \<in> sets M \<Longrightarrow>
   4.664                        (f o (\<lambda>z. z \<inter> (space M - x)) o A) = (f o (\<lambda>z. z - x) o A)"
   4.665 @@ -698,9 +753,9 @@
   4.666          assume u: "u \<in> sets M"
   4.667          have [simp]: "\<And>n. f (A n \<inter> u) \<le> f (A n)"
   4.668            by (simp add: increasingD [OF inc] u Int range_subsetD [OF A])
   4.669 -        have 2: "Inf (measure_set M f (s \<inter> u)) \<le> psuminf (f \<circ> (\<lambda>z. z \<inter> u) \<circ> A)"
   4.670 +        have 2: "Inf (measure_set M f (s \<inter> u)) \<le> (\<Sum>i. f (A i \<inter> u))"
   4.671            proof (rule complete_lattice_class.Inf_lower)
   4.672 -            show "psuminf (f \<circ> (\<lambda>z. z \<inter> u) \<circ> A) \<in> measure_set M f (s \<inter> u)"
   4.673 +            show "(\<Sum>i. f (A i \<inter> u)) \<in> measure_set M f (s \<inter> u)"
   4.674                apply (simp add: measure_set_def)
   4.675                apply (rule_tac x="(\<lambda>z. z \<inter> u) o A" in exI)
   4.676                apply (auto simp add: disjoint_family_subset [OF disj] o_def)
   4.677 @@ -709,15 +764,16 @@
   4.678                done
   4.679            qed
   4.680        } note lesum = this
   4.681 -      have inf1: "Inf (measure_set M f (s\<inter>x)) \<le> psuminf (f o (\<lambda>z. z\<inter>x) o A)"
   4.682 +      have [simp]: "\<And>i. A i \<inter> (space M - x) = A i - x" using A sets_into_space by auto
   4.683 +      have inf1: "Inf (measure_set M f (s\<inter>x)) \<le> (\<Sum>i. f (A i \<inter> x))"
   4.684          and inf2: "Inf (measure_set M f (s \<inter> (space M - x)))
   4.685 -                   \<le> psuminf (f o (\<lambda>z. z \<inter> (space M - x)) o A)"
   4.686 +                   \<le> (\<Sum>i. f (A i \<inter> (space M - x)))"
   4.687          by (metis Diff lesum top x)+
   4.688        hence "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
   4.689 -           \<le> psuminf (f o (\<lambda>s. s\<inter>x) o A) + psuminf (f o (\<lambda>s. s-x) o A)"
   4.690 -        by (simp add: x add_mono)
   4.691 -      also have "... \<le> psuminf (f o A)"
   4.692 -        by (simp add: x psuminf_add[symmetric] o_def)
   4.693 +           \<le> (\<Sum>i. f (A i \<inter> x)) + (\<Sum>i. f (A i - x))"
   4.694 +        by (simp add: add_mono x)
   4.695 +      also have "... \<le> (\<Sum>i. f (A i))" using posf[unfolded positive_def] A x
   4.696 +        by (subst suminf_add_extreal[symmetric]) auto
   4.697        also have "... \<le> Inf (measure_set M f s) + e"
   4.698          by (rule l)
   4.699        finally show "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
   4.700 @@ -732,7 +788,7 @@
   4.701      also have "... \<le> Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))"
   4.702        apply (rule subadditiveD)
   4.703        apply (rule algebra.countably_subadditive_subadditive[OF algebra_Pow])
   4.704 -      apply (simp add: positive_def inf_measure_empty[OF posf])
   4.705 +      apply (simp add: positive_def inf_measure_empty[OF posf] inf_measure_pos[OF posf])
   4.706        apply (rule inf_measure_countably_subadditive)
   4.707        using s by (auto intro!: posf inc)
   4.708      finally show ?thesis .
   4.709 @@ -751,7 +807,7 @@
   4.710  
   4.711  theorem (in algebra) caratheodory:
   4.712    assumes posf: "positive M f" and ca: "countably_additive M f"
   4.713 -  shows "\<exists>\<mu> :: 'a set \<Rightarrow> pextreal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and>
   4.714 +  shows "\<exists>\<mu> :: 'a set \<Rightarrow> extreal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and>
   4.715              measure_space \<lparr> space = space M, sets = sets (sigma M), measure = \<mu> \<rparr>"
   4.716  proof -
   4.717    have inc: "increasing M f"
     5.1 --- a/src/HOL/Probability/Complete_Measure.thy	Mon Mar 14 14:37:47 2011 +0100
     5.2 +++ b/src/HOL/Probability/Complete_Measure.thy	Mon Mar 14 14:37:49 2011 +0100
     5.3 @@ -1,7 +1,6 @@
     5.4 -(*  Title:      HOL/Probability/Complete_Measure.thy
     5.5 +(*  Title:      Complete_Measure.thy
     5.6      Author:     Robert Himmelmann, Johannes Hoelzl, TU Muenchen
     5.7  *)
     5.8 -
     5.9  theory Complete_Measure
    5.10  imports Product_Measure
    5.11  begin
    5.12 @@ -177,7 +176,8 @@
    5.13  proof -
    5.14    show "measure_space completion"
    5.15    proof
    5.16 -    show "measure completion {} = 0" by (auto simp: completion_def)
    5.17 +    show "positive completion (measure completion)"
    5.18 +      by (auto simp: completion_def positive_def)
    5.19    next
    5.20      show "countably_additive completion (measure completion)"
    5.21      proof (intro countably_additiveI)
    5.22 @@ -189,9 +189,9 @@
    5.23            using A by (subst (1 2) main_part_null_part_Un) auto
    5.24          then show "main_part (A n) \<inter> main_part (A m) = {}" by auto
    5.25        qed
    5.26 -      then have "(\<Sum>\<^isub>\<infinity>n. measure completion (A n)) = \<mu> (\<Union>i. main_part (A i))"
    5.27 +      then have "(\<Sum>n. measure completion (A n)) = \<mu> (\<Union>i. main_part (A i))"
    5.28          unfolding completion_def using A by (auto intro!: measure_countably_additive)
    5.29 -      then show "(\<Sum>\<^isub>\<infinity>n. measure completion (A n)) = measure completion (UNION UNIV A)"
    5.30 +      then show "(\<Sum>n. measure completion (A n)) = measure completion (UNION UNIV A)"
    5.31          by (simp add: completion_def \<mu>_main_part_UN[OF A(1)])
    5.32      qed
    5.33    qed
    5.34 @@ -251,30 +251,52 @@
    5.35    qed
    5.36  qed
    5.37  
    5.38 -lemma (in completeable_measure_space) completion_ex_borel_measurable:
    5.39 -  fixes g :: "'a \<Rightarrow> pextreal"
    5.40 -  assumes g: "g \<in> borel_measurable completion"
    5.41 +lemma (in completeable_measure_space) completion_ex_borel_measurable_pos:
    5.42 +  fixes g :: "'a \<Rightarrow> extreal"
    5.43 +  assumes g: "g \<in> borel_measurable completion" and "\<And>x. 0 \<le> g x"
    5.44    shows "\<exists>g'\<in>borel_measurable M. (AE x. g x = g' x)"
    5.45  proof -
    5.46 -  from g[THEN completion.borel_measurable_implies_simple_function_sequence]
    5.47 -  obtain f where "\<And>i. simple_function completion (f i)" "f \<up> g" by auto
    5.48 -  then have "\<forall>i. \<exists>f'. simple_function M f' \<and> (AE x. f i x = f' x)"
    5.49 -    using completion_ex_simple_function by auto
    5.50 +  from g[THEN completion.borel_measurable_implies_simple_function_sequence'] guess f . note f = this
    5.51 +  from this(1)[THEN completion_ex_simple_function]
    5.52 +  have "\<forall>i. \<exists>f'. simple_function M f' \<and> (AE x. f i x = f' x)" ..
    5.53    from this[THEN choice] obtain f' where
    5.54      sf: "\<And>i. simple_function M (f' i)" and
    5.55      AE: "\<forall>i. AE x. f i x = f' i x" by auto
    5.56    show ?thesis
    5.57    proof (intro bexI)
    5.58 -    from AE[unfolded all_AE_countable]
    5.59 +    from AE[unfolded AE_all_countable[symmetric]]
    5.60      show "AE x. g x = (SUP i. f' i x)" (is "AE x. g x = ?f x")
    5.61      proof (elim AE_mp, safe intro!: AE_I2)
    5.62        fix x assume eq: "\<forall>i. f i x = f' i x"
    5.63 -      moreover have "g = SUPR UNIV f" using `f \<up> g` unfolding isoton_def by simp
    5.64 -      ultimately show "g x = ?f x" by (simp add: SUPR_apply)
    5.65 +      moreover have "g x = (SUP i. f i x)"
    5.66 +        unfolding f using `0 \<le> g x` by (auto split: split_max)
    5.67 +      ultimately show "g x = ?f x" by auto
    5.68      qed
    5.69      show "?f \<in> borel_measurable M"
    5.70        using sf by (auto intro: borel_measurable_simple_function)
    5.71    qed
    5.72  qed
    5.73  
    5.74 +lemma (in completeable_measure_space) completion_ex_borel_measurable:
    5.75 +  fixes g :: "'a \<Rightarrow> extreal"
    5.76 +  assumes g: "g \<in> borel_measurable completion"
    5.77 +  shows "\<exists>g'\<in>borel_measurable M. (AE x. g x = g' x)"
    5.78 +proof -
    5.79 +  have "(\<lambda>x. max 0 (g x)) \<in> borel_measurable completion" "\<And>x. 0 \<le> max 0 (g x)" using g by auto
    5.80 +  from completion_ex_borel_measurable_pos[OF this] guess g_pos ..
    5.81 +  moreover
    5.82 +  have "(\<lambda>x. max 0 (- g x)) \<in> borel_measurable completion" "\<And>x. 0 \<le> max 0 (- g x)" using g by auto
    5.83 +  from completion_ex_borel_measurable_pos[OF this] guess g_neg ..
    5.84 +  ultimately
    5.85 +  show ?thesis
    5.86 +  proof (safe intro!: bexI[of _ "\<lambda>x. g_pos x - g_neg x"])
    5.87 +    show "AE x. max 0 (- g x) = g_neg x \<longrightarrow> max 0 (g x) = g_pos x \<longrightarrow> g x = g_pos x - g_neg x"
    5.88 +    proof (intro AE_I2 impI)
    5.89 +      fix x assume g: "max 0 (- g x) = g_neg x" "max 0 (g x) = g_pos x"
    5.90 +      show "g x = g_pos x - g_neg x" unfolding g[symmetric]
    5.91 +        by (cases "g x") (auto split: split_max)
    5.92 +    qed
    5.93 +  qed auto
    5.94 +qed
    5.95 +
    5.96  end
     6.1 --- a/src/HOL/Probability/Information.thy	Mon Mar 14 14:37:47 2011 +0100
     6.2 +++ b/src/HOL/Probability/Information.thy	Mon Mar 14 14:37:49 2011 +0100
     6.3 @@ -2,9 +2,12 @@
     6.4  imports
     6.5    Probability_Space
     6.6    "~~/src/HOL/Library/Convex"
     6.7 -  Lebesgue_Measure
     6.8  begin
     6.9  
    6.10 +lemma (in prob_space) not_zero_less_distribution[simp]:
    6.11 +  "(\<not> 0 < distribution X A) \<longleftrightarrow> distribution X A = 0"
    6.12 +  using distribution_positive[of X A] by arith
    6.13 +
    6.14  lemma log_le: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log a x \<le> log a y"
    6.15    by (subst log_le_cancel_iff) auto
    6.16  
    6.17 @@ -238,7 +241,7 @@
    6.18    have ms: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
    6.19    show "(\<Sum>x \<in> space M. log b (real (RN_deriv M \<nu> x)) * real (\<nu> {x})) = ?sum"
    6.20      using RN_deriv_finite_measure[OF ms ac]
    6.21 -    by (auto intro!: setsum_cong simp: field_simps real_of_pextreal_mult[symmetric])
    6.22 +    by (auto intro!: setsum_cong simp: field_simps)
    6.23  qed
    6.24  
    6.25  lemma (in finite_prob_space) KL_divergence_positive_finite:
    6.26 @@ -254,7 +257,8 @@
    6.27    proof (subst KL_divergence_eq_finite[OF ms ac], safe intro!: log_setsum_divide not_empty)
    6.28      show "finite (space M)" using finite_space by simp
    6.29      show "1 < b" by fact
    6.30 -    show "(\<Sum>x\<in>space M. real (\<nu> {x})) = 1" using v.finite_sum_over_space_eq_1 by simp
    6.31 +    show "(\<Sum>x\<in>space M. real (\<nu> {x})) = 1"
    6.32 +      using v.finite_sum_over_space_eq_1 by (simp add: v.\<mu>'_def)
    6.33  
    6.34      fix x assume "x \<in> space M"
    6.35      then have x: "{x} \<in> sets M" unfolding sets_eq_Pow by auto
    6.36 @@ -262,17 +266,19 @@
    6.37        then have "\<nu> {x} \<noteq> 0" by auto
    6.38        then have "\<mu> {x} \<noteq> 0"
    6.39          using ac[unfolded absolutely_continuous_def, THEN bspec, of "{x}"] x by auto
    6.40 -      thus "0 < prob {x}" using finite_measure[of "{x}"] x by auto }
    6.41 -  qed auto
    6.42 -  thus "0 \<le> KL_divergence b M \<nu>" using finite_sum_over_space_eq_1 by simp
    6.43 +      thus "0 < real (\<mu> {x})" using real_measure[OF x] by auto }
    6.44 +    show "0 \<le> real (\<mu> {x})" "0 \<le> real (\<nu> {x})"
    6.45 +      using real_measure[OF x] v.real_measure[of "{x}"] x by auto
    6.46 +  qed
    6.47 +  thus "0 \<le> KL_divergence b M \<nu>" using finite_sum_over_space_eq_1 by (simp add: \<mu>'_def)
    6.48  qed
    6.49  
    6.50  subsection {* Mutual Information *}
    6.51  
    6.52  definition (in prob_space)
    6.53    "mutual_information b S T X Y =
    6.54 -    KL_divergence b (S\<lparr>measure := distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := distribution Y\<rparr>)
    6.55 -      (joint_distribution X Y)"
    6.56 +    KL_divergence b (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>)
    6.57 +      (extreal\<circ>joint_distribution X Y)"
    6.58  
    6.59  definition (in prob_space)
    6.60    "entropy b s X = mutual_information b s s X X"
    6.61 @@ -280,38 +286,33 @@
    6.62  abbreviation (in information_space)
    6.63    mutual_information_Pow ("\<I>'(_ ; _')") where
    6.64    "\<I>(X ; Y) \<equiv> mutual_information b
    6.65 -    \<lparr> space = X`space M, sets = Pow (X`space M), measure = distribution X \<rparr>
    6.66 -    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = distribution Y \<rparr> X Y"
    6.67 +    \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr>
    6.68 +    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = extreal\<circ>distribution Y \<rparr> X Y"
    6.69  
    6.70  lemma (in prob_space) finite_variables_absolutely_continuous:
    6.71    assumes X: "finite_random_variable S X" and Y: "finite_random_variable T Y"
    6.72    shows "measure_space.absolutely_continuous
    6.73 -    (S\<lparr>measure := distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := distribution Y\<rparr>)
    6.74 -    (joint_distribution X Y)"
    6.75 +    (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>)
    6.76 +    (extreal\<circ>joint_distribution X Y)"
    6.77  proof -
    6.78 -  interpret X: finite_prob_space "S\<lparr>measure := distribution X\<rparr>"
    6.79 +  interpret X: finite_prob_space "S\<lparr>measure := extreal\<circ>distribution X\<rparr>"
    6.80      using X by (rule distribution_finite_prob_space)
    6.81 -  interpret Y: finite_prob_space "T\<lparr>measure := distribution Y\<rparr>"
    6.82 +  interpret Y: finite_prob_space "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
    6.83      using Y by (rule distribution_finite_prob_space)
    6.84    interpret XY: pair_finite_prob_space
    6.85 -    "S\<lparr>measure := distribution X\<rparr>" "T\<lparr> measure := distribution Y\<rparr>" by default
    6.86 -  interpret P: finite_prob_space "XY.P\<lparr> measure := joint_distribution X Y\<rparr>"
    6.87 +    "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" "T\<lparr> measure := extreal\<circ>distribution Y\<rparr>" by default
    6.88 +  interpret P: finite_prob_space "XY.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>"
    6.89      using assms by (auto intro!: joint_distribution_finite_prob_space)
    6.90    note rv = assms[THEN finite_random_variableD]
    6.91 -  show "XY.absolutely_continuous (joint_distribution X Y)"
    6.92 +  show "XY.absolutely_continuous (extreal\<circ>joint_distribution X Y)"
    6.93    proof (rule XY.absolutely_continuousI)
    6.94 -    show "finite_measure_space (XY.P\<lparr> measure := joint_distribution X Y\<rparr>)" by default
    6.95 +    show "finite_measure_space (XY.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)" by default
    6.96      fix x assume "x \<in> space XY.P" and "XY.\<mu> {x} = 0"
    6.97 -    then obtain a b where "(a, b) = x" and "a \<in> space S" "b \<in> space T"
    6.98 -      and distr: "distribution X {a} * distribution Y {b} = 0"
    6.99 +    then obtain a b where "x = (a, b)"
   6.100 +      and "distribution X {a} = 0 \<or> distribution Y {b} = 0"
   6.101        by (cases x) (auto simp: space_pair_measure)
   6.102 -    with X.sets_eq_Pow Y.sets_eq_Pow
   6.103 -      joint_distribution_Times_le_fst[OF rv, of "{a}" "{b}"]
   6.104 -      joint_distribution_Times_le_snd[OF rv, of "{a}" "{b}"]
   6.105 -    have "joint_distribution X Y {x} \<le> distribution Y {b}"
   6.106 -         "joint_distribution X Y {x} \<le> distribution X {a}"
   6.107 -      by (auto simp del: X.sets_eq_Pow Y.sets_eq_Pow)
   6.108 -    with distr show "joint_distribution X Y {x} = 0" by auto
   6.109 +    with finite_distribution_order(5,6)[OF X Y]
   6.110 +    show "(extreal \<circ> joint_distribution X Y) {x} = 0" by auto
   6.111    qed
   6.112  qed
   6.113  
   6.114 @@ -320,28 +321,28 @@
   6.115    assumes MY: "finite_random_variable MY Y"
   6.116    shows mutual_information_generic_eq:
   6.117      "mutual_information b MX MY X Y = (\<Sum> (x,y) \<in> space MX \<times> space MY.
   6.118 -      real (joint_distribution X Y {(x,y)}) *
   6.119 -      log b (real (joint_distribution X Y {(x,y)}) /
   6.120 -      (real (distribution X {x}) * real (distribution Y {y}))))"
   6.121 +      joint_distribution X Y {(x,y)} *
   6.122 +      log b (joint_distribution X Y {(x,y)} /
   6.123 +      (distribution X {x} * distribution Y {y})))"
   6.124      (is ?sum)
   6.125    and mutual_information_positive_generic:
   6.126       "0 \<le> mutual_information b MX MY X Y" (is ?positive)
   6.127  proof -
   6.128 -  interpret X: finite_prob_space "MX\<lparr>measure := distribution X\<rparr>"
   6.129 +  interpret X: finite_prob_space "MX\<lparr>measure := extreal\<circ>distribution X\<rparr>"
   6.130      using MX by (rule distribution_finite_prob_space)
   6.131 -  interpret Y: finite_prob_space "MY\<lparr>measure := distribution Y\<rparr>"
   6.132 +  interpret Y: finite_prob_space "MY\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   6.133      using MY by (rule distribution_finite_prob_space)
   6.134 -  interpret XY: pair_finite_prob_space "MX\<lparr>measure := distribution X\<rparr>" "MY\<lparr>measure := distribution Y\<rparr>" by default
   6.135 -  interpret P: finite_prob_space "XY.P\<lparr>measure := joint_distribution X Y\<rparr>"
   6.136 +  interpret XY: pair_finite_prob_space "MX\<lparr>measure := extreal\<circ>distribution X\<rparr>" "MY\<lparr>measure := extreal\<circ>distribution Y\<rparr>" by default
   6.137 +  interpret P: finite_prob_space "XY.P\<lparr>measure := extreal\<circ>joint_distribution X Y\<rparr>"
   6.138      using assms by (auto intro!: joint_distribution_finite_prob_space)
   6.139  
   6.140 -  have P_ms: "finite_measure_space (XY.P\<lparr>measure :=joint_distribution X Y\<rparr>)" by default
   6.141 -  have P_ps: "finite_prob_space (XY.P\<lparr>measure := joint_distribution X Y\<rparr>)" by default
   6.142 +  have P_ms: "finite_measure_space (XY.P\<lparr>measure := extreal\<circ>joint_distribution X Y\<rparr>)" by default
   6.143 +  have P_ps: "finite_prob_space (XY.P\<lparr>measure := extreal\<circ>joint_distribution X Y\<rparr>)" by default
   6.144  
   6.145    show ?sum
   6.146      unfolding Let_def mutual_information_def
   6.147      by (subst XY.KL_divergence_eq_finite[OF P_ms finite_variables_absolutely_continuous[OF MX MY]])
   6.148 -       (auto simp add: space_pair_measure setsum_cartesian_product' real_of_pextreal_mult[symmetric])
   6.149 +       (auto simp add: space_pair_measure setsum_cartesian_product')
   6.150  
   6.151    show ?positive
   6.152      using XY.KL_divergence_positive_finite[OF P_ps finite_variables_absolutely_continuous[OF MX MY] b_gt_1]
   6.153 @@ -351,10 +352,10 @@
   6.154  lemma (in information_space) mutual_information_commute_generic:
   6.155    assumes X: "random_variable S X" and Y: "random_variable T Y"
   6.156    assumes ac: "measure_space.absolutely_continuous
   6.157 -    (S\<lparr>measure := distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := distribution Y\<rparr>) (joint_distribution X Y)"
   6.158 +    (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>) (extreal\<circ>joint_distribution X Y)"
   6.159    shows "mutual_information b S T X Y = mutual_information b T S Y X"
   6.160  proof -
   6.161 -  let ?S = "S\<lparr>measure := distribution X\<rparr>" and ?T = "T\<lparr>measure := distribution Y\<rparr>"
   6.162 +  let ?S = "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" and ?T = "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   6.163    interpret S: prob_space ?S using X by (rule distribution_prob_space)
   6.164    interpret T: prob_space ?T using Y by (rule distribution_prob_space)
   6.165    interpret P: pair_prob_space ?S ?T ..
   6.166 @@ -363,13 +364,13 @@
   6.167      unfolding mutual_information_def
   6.168    proof (intro Q.KL_divergence_vimage[OF Q.measure_preserving_swap _ _ _ _ ac b_gt_1])
   6.169      show "(\<lambda>(x,y). (y,x)) \<in> measure_preserving
   6.170 -      (P.P \<lparr> measure := joint_distribution X Y\<rparr>) (Q.P \<lparr> measure := joint_distribution Y X\<rparr>)"
   6.171 +      (P.P \<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>) (Q.P \<lparr> measure := extreal\<circ>joint_distribution Y X\<rparr>)"
   6.172        using X Y unfolding measurable_def
   6.173        unfolding measure_preserving_def using P.pair_sigma_algebra_swap_measurable
   6.174 -      by (auto simp add: space_pair_measure distribution_def intro!: arg_cong[where f=\<mu>])
   6.175 -    have "prob_space (P.P\<lparr> measure := joint_distribution X Y\<rparr>)"
   6.176 +      by (auto simp add: space_pair_measure distribution_def intro!: arg_cong[where f=\<mu>'])
   6.177 +    have "prob_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
   6.178        using X Y by (auto intro!: distribution_prob_space random_variable_pairI)
   6.179 -    then show "measure_space (P.P\<lparr> measure := joint_distribution X Y\<rparr>)"
   6.180 +    then show "measure_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
   6.181        unfolding prob_space_def by simp
   6.182    qed auto
   6.183  qed
   6.184 @@ -389,8 +390,8 @@
   6.185  lemma (in information_space) mutual_information_eq:
   6.186    assumes "simple_function M X" "simple_function M Y"
   6.187    shows "\<I>(X;Y) = (\<Sum> (x,y) \<in> X ` space M \<times> Y ` space M.
   6.188 -    real (distribution (\<lambda>x. (X x, Y x)) {(x,y)}) * log b (real (distribution (\<lambda>x. (X x, Y x)) {(x,y)}) /
   6.189 -                                                   (real (distribution X {x}) * real (distribution Y {y}))))"
   6.190 +    distribution (\<lambda>x. (X x, Y x)) {(x,y)} * log b (distribution (\<lambda>x. (X x, Y x)) {(x,y)} /
   6.191 +                                                   (distribution X {x} * distribution Y {y})))"
   6.192    using assms by (simp add: mutual_information_generic_eq)
   6.193  
   6.194  lemma (in information_space) mutual_information_generic_cong:
   6.195 @@ -416,22 +417,27 @@
   6.196  
   6.197  abbreviation (in information_space)
   6.198    entropy_Pow ("\<H>'(_')") where
   6.199 -  "\<H>(X) \<equiv> entropy b \<lparr> space = X`space M, sets = Pow (X`space M), measure = distribution X \<rparr> X"
   6.200 +  "\<H>(X) \<equiv> entropy b \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr> X"
   6.201  
   6.202  lemma (in information_space) entropy_generic_eq:
   6.203 +  fixes X :: "'a \<Rightarrow> 'c"
   6.204    assumes MX: "finite_random_variable MX X"
   6.205 -  shows "entropy b MX X = -(\<Sum> x \<in> space MX. real (distribution X {x}) * log b (real (distribution X {x})))"
   6.206 +  shows "entropy b MX X = -(\<Sum> x \<in> space MX. distribution X {x} * log b (distribution X {x}))"
   6.207  proof -
   6.208 -  interpret MX: finite_prob_space "MX\<lparr>measure := distribution X\<rparr>"
   6.209 +  interpret MX: finite_prob_space "MX\<lparr>measure := extreal\<circ>distribution X\<rparr>"
   6.210      using MX by (rule distribution_finite_prob_space)
   6.211 -  let "?X x" = "real (distribution X {x})"
   6.212 -  let "?XX x y" = "real (joint_distribution X X {(x, y)})"
   6.213 -  { fix x y
   6.214 -    have "(\<lambda>x. (X x, X x)) -` {(x, y)} = (if x = y then X -` {x} else {})" by auto
   6.215 +  let "?X x" = "distribution X {x}"
   6.216 +  let "?XX x y" = "joint_distribution X X {(x, y)}"
   6.217 +
   6.218 +  { fix x y :: 'c
   6.219 +    { assume "x \<noteq> y"
   6.220 +      then have "(\<lambda>x. (X x, X x)) -` {(x,y)} \<inter> space M = {}" by auto
   6.221 +      then have "joint_distribution X X {(x, y)} = 0" by (simp add: distribution_def) }
   6.222      then have "?XX x y * log b (?XX x y / (?X x * ?X y)) =
   6.223          (if x = y then - ?X y * log b (?X y) else 0)"
   6.224 -      unfolding distribution_def by (auto simp: log_simps zero_less_mult_iff) }
   6.225 +      by (auto simp: log_simps zero_less_mult_iff) }
   6.226    note remove_XX = this
   6.227 +
   6.228    show ?thesis
   6.229      unfolding entropy_def mutual_information_generic_eq[OF MX MX]
   6.230      unfolding setsum_cartesian_product[symmetric] setsum_negf[symmetric] remove_XX
   6.231 @@ -440,7 +446,7 @@
   6.232  
   6.233  lemma (in information_space) entropy_eq:
   6.234    assumes "simple_function M X"
   6.235 -  shows "\<H>(X) = -(\<Sum> x \<in> X ` space M. real (distribution X {x}) * log b (real (distribution X {x})))"
   6.236 +  shows "\<H>(X) = -(\<Sum> x \<in> X ` space M. distribution X {x} * log b (distribution X {x}))"
   6.237    using assms by (simp add: entropy_generic_eq)
   6.238  
   6.239  lemma (in information_space) entropy_positive:
   6.240 @@ -448,63 +454,77 @@
   6.241    unfolding entropy_def by (simp add: mutual_information_positive)
   6.242  
   6.243  lemma (in information_space) entropy_certainty_eq_0:
   6.244 -  assumes "simple_function M X" and "x \<in> X ` space M" and "distribution X {x} = 1"
   6.245 +  assumes X: "simple_function M X" and "x \<in> X ` space M" and "distribution X {x} = 1"
   6.246    shows "\<H>(X) = 0"
   6.247  proof -
   6.248 -  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = distribution X\<rparr>"
   6.249 +  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = extreal\<circ>distribution X\<rparr>"
   6.250    note simple_function_imp_finite_random_variable[OF `simple_function M X`]
   6.251 -  from distribution_finite_prob_space[OF this, of "\<lparr> measure = distribution X \<rparr>"]
   6.252 +  from distribution_finite_prob_space[OF this, of "\<lparr> measure = extreal\<circ>distribution X \<rparr>"]
   6.253    interpret X: finite_prob_space ?X by simp
   6.254    have "distribution X (X ` space M - {x}) = distribution X (X ` space M) - distribution X {x}"
   6.255      using X.measure_compl[of "{x}"] assms by auto
   6.256    also have "\<dots> = 0" using X.prob_space assms by auto
   6.257    finally have X0: "distribution X (X ` space M - {x}) = 0" by auto
   6.258 -  { fix y assume asm: "y \<noteq> x" "y \<in> X ` space M"
   6.259 -    hence "{y} \<subseteq> X ` space M - {x}" by auto
   6.260 -    from X.measure_mono[OF this] X0 asm
   6.261 -    have "distribution X {y} = 0" by auto }
   6.262 -  hence fi: "\<And> y. y \<in> X ` space M \<Longrightarrow> real (distribution X {y}) = (if x = y then 1 else 0)"
   6.263 -    using assms by auto
   6.264 +  { fix y assume *: "y \<in> X ` space M"
   6.265 +    { assume asm: "y \<noteq> x"
   6.266 +      with * have "{y} \<subseteq> X ` space M - {x}" by auto
   6.267 +      from X.measure_mono[OF this] X0 asm *
   6.268 +      have "distribution X {y} = 0"  by (auto intro: antisym) }
   6.269 +    then have "distribution X {y} = (if x = y then 1 else 0)"
   6.270 +      using assms by auto }
   6.271 +  note fi = this
   6.272    have y: "\<And>y. (if x = y then 1 else 0) * log b (if x = y then 1 else 0) = 0" by simp
   6.273    show ?thesis unfolding entropy_eq[OF `simple_function M X`] by (auto simp: y fi)
   6.274  qed
   6.275  
   6.276  lemma (in information_space) entropy_le_card_not_0:
   6.277 -  assumes "simple_function M X"
   6.278 -  shows "\<H>(X) \<le> log b (real (card (X ` space M \<inter> {x . distribution X {x} \<noteq> 0})))"
   6.279 +  assumes X: "simple_function M X"
   6.280 +  shows "\<H>(X) \<le> log b (card (X ` space M \<inter> {x. distribution X {x} \<noteq> 0}))"
   6.281  proof -
   6.282 -  let "?d x" = "distribution X {x}"
   6.283 -  let "?p x" = "real (?d x)"
   6.284 +  let "?p x" = "distribution X {x}"
   6.285    have "\<H>(X) = (\<Sum>x\<in>X`space M. ?p x * log b (1 / ?p x))"
   6.286 -    by (auto intro!: setsum_cong simp: entropy_eq[OF `simple_function M X`] setsum_negf[symmetric] log_simps not_less)
   6.287 +    unfolding entropy_eq[OF X] setsum_negf[symmetric]
   6.288 +    by (auto intro!: setsum_cong simp: log_simps)
   6.289    also have "\<dots> \<le> log b (\<Sum>x\<in>X`space M. ?p x * (1 / ?p x))"
   6.290 -    apply (rule log_setsum')
   6.291 -    using not_empty b_gt_1 `simple_function M X` sum_over_space_real_distribution
   6.292 -    by (auto simp: simple_function_def)
   6.293 -  also have "\<dots> = log b (\<Sum>x\<in>X`space M. if ?d x \<noteq> 0 then 1 else 0)"
   6.294 -    using distribution_finite[OF `simple_function M X`[THEN simple_function_imp_random_variable], simplified]
   6.295 -    by (intro arg_cong[where f="\<lambda>X. log b X"] setsum_cong) (auto simp: real_of_pextreal_eq_0)
   6.296 +    using not_empty b_gt_1 `simple_function M X` sum_over_space_real_distribution[OF X]
   6.297 +    by (intro log_setsum') (auto simp: simple_function_def)
   6.298 +  also have "\<dots> = log b (\<Sum>x\<in>X`space M. if ?p x \<noteq> 0 then 1 else 0)"
   6.299 +    by (intro arg_cong[where f="\<lambda>X. log b X"] setsum_cong) auto
   6.300    finally show ?thesis
   6.301      using `simple_function M X` by (auto simp: setsum_cases real_eq_of_nat simple_function_def)
   6.302  qed
   6.303  
   6.304 +lemma (in prob_space) measure'_translate:
   6.305 +  assumes X: "random_variable S X" and A: "A \<in> sets S"
   6.306 +  shows "finite_measure.\<mu>' (S\<lparr> measure := extreal\<circ>distribution X \<rparr>) A = distribution X A"
   6.307 +proof -
   6.308 +  interpret S: prob_space "S\<lparr> measure := extreal\<circ>distribution X \<rparr>"
   6.309 +    using distribution_prob_space[OF X] .
   6.310 +  from A show "S.\<mu>' A = distribution X A"
   6.311 +    unfolding S.\<mu>'_def by (simp add: distribution_def_raw \<mu>'_def)
   6.312 +qed
   6.313 +
   6.314  lemma (in information_space) entropy_uniform_max:
   6.315 -  assumes "simple_function M X"
   6.316 +  assumes X: "simple_function M X"
   6.317    assumes "\<And>x y. \<lbrakk> x \<in> X ` space M ; y \<in> X ` space M \<rbrakk> \<Longrightarrow> distribution X {x} = distribution X {y}"
   6.318    shows "\<H>(X) = log b (real (card (X ` space M)))"
   6.319  proof -
   6.320 -  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = distribution X\<rparr>"
   6.321 -  note simple_function_imp_finite_random_variable[OF `simple_function M X`]
   6.322 -  from distribution_finite_prob_space[OF this, of "\<lparr> measure = distribution X \<rparr>"]
   6.323 +  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = undefined\<rparr>\<lparr> measure := extreal\<circ>distribution X\<rparr>"
   6.324 +  note frv = simple_function_imp_finite_random_variable[OF X]
   6.325 +  from distribution_finite_prob_space[OF this, of "\<lparr> measure = extreal\<circ>distribution X \<rparr>"]
   6.326    interpret X: finite_prob_space ?X by simp
   6.327 +  note rv = finite_random_variableD[OF frv]
   6.328    have card_gt0: "0 < card (X ` space M)" unfolding card_gt_0_iff
   6.329      using `simple_function M X` not_empty by (auto simp: simple_function_def)
   6.330 -  { fix x assume "x \<in> X ` space M"
   6.331 -    hence "real (distribution X {x}) = 1 / real (card (X ` space M))"
   6.332 -    proof (rule X.uniform_prob[simplified])
   6.333 -      fix x y assume "x \<in> X`space M" "y \<in> X`space M"
   6.334 -      from assms(2)[OF this] show "real (distribution X {x}) = real (distribution X {y})" by simp
   6.335 -    qed }
   6.336 +  { fix x assume "x \<in> space ?X"
   6.337 +    moreover then have "X.\<mu>' {x} = 1 / card (space ?X)"
   6.338 +    proof (rule X.uniform_prob)
   6.339 +      fix x y assume "x \<in> space ?X" "y \<in> space ?X"
   6.340 +      with assms(2)[of x y] show "X.\<mu>' {x} = X.\<mu>' {y}"
   6.341 +        by (subst (1 2) measure'_translate[OF rv]) auto
   6.342 +    qed
   6.343 +    ultimately have "distribution X {x} = 1 / card (space ?X)"
   6.344 +      by (subst (asm) measure'_translate[OF rv]) auto }
   6.345    thus ?thesis
   6.346      using not_empty X.finite_space b_gt_1 card_gt0
   6.347      by (simp add: entropy_eq[OF `simple_function M X`] real_eq_of_nat[symmetric] log_simps)
   6.348 @@ -552,8 +572,7 @@
   6.349  lemma (in information_space) entropy_eq_cartesian_product:
   6.350    assumes "simple_function M X" "simple_function M Y"
   6.351    shows "\<H>(\<lambda>x. (X x, Y x)) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
   6.352 -    real (joint_distribution X Y {(x,y)}) *
   6.353 -    log b (real (joint_distribution X Y {(x,y)})))"
   6.354 +    joint_distribution X Y {(x,y)} * log b (joint_distribution X Y {(x,y)}))"
   6.355  proof -
   6.356    have sf: "simple_function M (\<lambda>x. (X x, Y x))"
   6.357      using assms by (auto intro: simple_function_Pair)
   6.358 @@ -576,9 +595,9 @@
   6.359  abbreviation (in information_space)
   6.360    conditional_mutual_information_Pow ("\<I>'( _ ; _ | _ ')") where
   6.361    "\<I>(X ; Y | Z) \<equiv> conditional_mutual_information b
   6.362 -    \<lparr> space = X`space M, sets = Pow (X`space M), measure = distribution X \<rparr>
   6.363 -    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = distribution Y \<rparr>
   6.364 -    \<lparr> space = Z`space M, sets = Pow (Z`space M), measure = distribution Z \<rparr>
   6.365 +    \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr>
   6.366 +    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = extreal\<circ>distribution Y \<rparr>
   6.367 +    \<lparr> space = Z`space M, sets = Pow (Z`space M), measure = extreal\<circ>distribution Z \<rparr>
   6.368      X Y Z"
   6.369  
   6.370  lemma (in information_space) conditional_mutual_information_generic_eq:
   6.371 @@ -586,58 +605,44 @@
   6.372      and MY: "finite_random_variable MY Y"
   6.373      and MZ: "finite_random_variable MZ Z"
   6.374    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.375 -             real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) *
   6.376 -             log b (real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) /
   6.377 -    (real (joint_distribution X Z {(x, z)}) * real (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
   6.378 -  (is "_ = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * ?YZdZ y z)))")
   6.379 +             distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
   6.380 +             log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} /
   6.381 +    (joint_distribution X Z {(x, z)} * (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
   6.382 +  (is "_ = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * (?YZ y z / ?Z z))))")
   6.383  proof -
   6.384 -  let ?YZ = "\<lambda>y z. real (joint_distribution Y Z {(y, z)})"
   6.385 -  let ?X = "\<lambda>x. real (distribution X {x})"
   6.386 -  let ?Z = "\<lambda>z. real (distribution Z {z})"
   6.387 -
   6.388 -  txt {* This proof is actually quiet easy, however we need to show that the
   6.389 -    distributions are finite and the joint distributions are zero when one of
   6.390 -    the variables distribution is also zero. *}
   6.391 -
   6.392 +  let ?X = "\<lambda>x. distribution X {x}"
   6.393    note finite_var = MX MY MZ
   6.394 -  note random_var = finite_var[THEN finite_random_variableD]
   6.395 -
   6.396 -  note space_simps = space_pair_measure space_sigma algebra.simps
   6.397 -
   6.398    note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
   6.399 +  note XYZ = finite_random_variable_pairI[OF MX YZ]
   6.400    note XZ = finite_random_variable_pairI[OF finite_var(1,3)]
   6.401    note ZX = finite_random_variable_pairI[OF finite_var(3,1)]
   6.402    note YZX = finite_random_variable_pairI[OF finite_var(2) ZX]
   6.403    note order1 =
   6.404 -    finite_distribution_order(5,6)[OF finite_var(1) YZ, simplified space_simps]
   6.405 -    finite_distribution_order(5,6)[OF finite_var(1,3), simplified space_simps]
   6.406 +    finite_distribution_order(5,6)[OF finite_var(1) YZ]
   6.407 +    finite_distribution_order(5,6)[OF finite_var(1,3)]
   6.408  
   6.409 +  note random_var = finite_var[THEN finite_random_variableD]
   6.410    note finite = finite_var(1) YZ finite_var(3) XZ YZX
   6.411 -  note finite[THEN finite_distribution_finite, simplified space_simps, simp]
   6.412  
   6.413    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.414            \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
   6.415      unfolding joint_distribution_commute_singleton[of X]
   6.416      unfolding joint_distribution_assoc_singleton[symmetric]
   6.417      using finite_distribution_order(6)[OF finite_var(2) ZX]
   6.418 -    by (auto simp: space_simps)
   6.419 +    by auto
   6.420  
   6.421 -  have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * ?YZdZ y z))) =
   6.422 +  have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * (?YZ y z / ?Z z)))) =
   6.423      (\<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.424      (is "(\<Sum>(x, y, z)\<in>?S. ?L x y z) = (\<Sum>(x, y, z)\<in>?S. ?R x y z)")
   6.425    proof (safe intro!: setsum_cong)
   6.426      fix x y z assume space: "x \<in> space MX" "y \<in> space MY" "z \<in> space MZ"
   6.427 -    then have *: "?XYZ x y z / (?XZ x z * ?YZdZ y z) =
   6.428 -      (?XYZ x y z / (?X x * ?YZ y z)) / (?XZ x z / (?X x * ?Z z))"
   6.429 -      using order1(3)
   6.430 -      by (auto simp: real_of_pextreal_mult[symmetric] real_of_pextreal_eq_0)
   6.431      show "?L x y z = ?R x y z"
   6.432      proof cases
   6.433        assume "?XYZ x y z \<noteq> 0"
   6.434 -      with space b_gt_1 order1 order2 show ?thesis unfolding *
   6.435 -        by (subst log_divide)
   6.436 -           (auto simp: zero_less_divide_iff zero_less_real_of_pextreal
   6.437 -                       real_of_pextreal_eq_0 zero_less_mult_iff)
   6.438 +      with space have "0 < ?X x" "0 < ?Z z" "0 < ?XZ x z" "0 < ?YZ y z" "0 < ?XYZ x y z"
   6.439 +        using order1 order2 by (auto simp: less_le)
   6.440 +      with b_gt_1 show ?thesis
   6.441 +        by (simp add: log_mult log_divide zero_less_mult_iff zero_less_divide_iff)
   6.442      qed simp
   6.443    qed
   6.444    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.445 @@ -649,8 +654,8 @@
   6.446                setsum_left_distrib[symmetric]
   6.447      unfolding joint_distribution_commute_singleton[of X]
   6.448      unfolding joint_distribution_assoc_singleton[symmetric]
   6.449 -    using setsum_real_joint_distribution_singleton[OF finite_var(2) ZX, unfolded space_simps]
   6.450 -    by (intro setsum_cong refl) simp
   6.451 +    using setsum_joint_distribution_singleton[OF finite_var(2) ZX]
   6.452 +    by (intro setsum_cong refl) (simp add: space_pair_measure)
   6.453    also have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?X x * ?YZ y z))) -
   6.454               (\<Sum>(x, z)\<in>space MX \<times> space MZ. ?XZ x z * log b (?XZ x z / (?X x * ?Z z))) =
   6.455               conditional_mutual_information b MX MY MZ X Y Z"
   6.456 @@ -664,11 +669,11 @@
   6.457  lemma (in information_space) conditional_mutual_information_eq:
   6.458    assumes "simple_function M X" "simple_function M Y" "simple_function M Z"
   6.459    shows "\<I>(X;Y|Z) = (\<Sum>(x, y, z) \<in> X`space M \<times> Y`space M \<times> Z`space M.
   6.460 -             real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) *
   6.461 -             log b (real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) /
   6.462 -    (real (joint_distribution X Z {(x, z)}) * real (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
   6.463 -  using conditional_mutual_information_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]]
   6.464 -  by simp
   6.465 +             distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
   6.466 +             log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} /
   6.467 +    (joint_distribution X Z {(x, z)} * joint_distribution Y Z {(y,z)} / distribution Z {z})))"
   6.468 +  by (subst conditional_mutual_information_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]])
   6.469 +     simp
   6.470  
   6.471  lemma (in information_space) conditional_mutual_information_eq_mutual_information:
   6.472    assumes X: "simple_function M X" and Y: "simple_function M Y"
   6.473 @@ -683,10 +688,10 @@
   6.474  qed
   6.475  
   6.476  lemma (in prob_space) distribution_unit[simp]: "distribution (\<lambda>x. ()) {()} = 1"
   6.477 -  unfolding distribution_def using measure_space_1 by auto
   6.478 +  unfolding distribution_def using prob_space by auto
   6.479  
   6.480  lemma (in prob_space) joint_distribution_unit[simp]: "distribution (\<lambda>x. (X x, ())) {(a, ())} = distribution X {a}"
   6.481 -  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>])
   6.482 +  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
   6.483  
   6.484  lemma (in prob_space) setsum_distribution:
   6.485    assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. distribution X {a}) = 1"
   6.486 @@ -695,12 +700,13 @@
   6.487  
   6.488  lemma (in prob_space) setsum_real_distribution:
   6.489    fixes MX :: "('c, 'd) measure_space_scheme"
   6.490 -  assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. real (distribution X {a})) = 1"
   6.491 -  using setsum_real_joint_distribution[OF assms, of "\<lparr> space = UNIV, sets = Pow UNIV, measure = undefined \<rparr>" "\<lambda>x. ()" "{()}"]
   6.492 -  using sigma_algebra_Pow[of "UNIV::unit set" "\<lparr> measure = undefined \<rparr>"] by simp
   6.493 +  assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. distribution X {a}) = 1"
   6.494 +  using setsum_joint_distribution[OF assms, of "\<lparr> space = UNIV, sets = Pow UNIV, measure = undefined \<rparr>" "\<lambda>x. ()" "{()}"]
   6.495 +  using sigma_algebra_Pow[of "UNIV::unit set" "\<lparr> measure = undefined \<rparr>"]
   6.496 +  by auto
   6.497  
   6.498  lemma (in information_space) conditional_mutual_information_generic_positive:
   6.499 -  assumes "finite_random_variable MX X" and "finite_random_variable MY Y" and "finite_random_variable MZ Z"
   6.500 +  assumes X: "finite_random_variable MX X" and Y: "finite_random_variable MY Y" and Z: "finite_random_variable MZ Z"
   6.501    shows "0 \<le> conditional_mutual_information b MX MY MZ X Y Z"
   6.502  proof (cases "space MX \<times> space MY \<times> space MZ = {}")
   6.503    case True show ?thesis
   6.504 @@ -708,43 +714,35 @@
   6.505      by simp
   6.506  next
   6.507    case False
   6.508 -  let "?dXYZ A" = "real (distribution (\<lambda>x. (X x, Y x, Z x)) A)"
   6.509 -  let "?dXZ A" = "real (joint_distribution X Z A)"
   6.510 -  let "?dYZ A" = "real (joint_distribution Y Z A)"
   6.511 -  let "?dX A" = "real (distribution X A)"
   6.512 -  let "?dZ A" = "real (distribution Z A)"
   6.513 +  let ?dXYZ = "distribution (\<lambda>x. (X x, Y x, Z x))"
   6.514 +  let ?dXZ = "joint_distribution X Z"
   6.515 +  let ?dYZ = "joint_distribution Y Z"
   6.516 +  let ?dX = "distribution X"
   6.517 +  let ?dZ = "distribution Z"
   6.518    let ?M = "space MX \<times> space MY \<times> space MZ"
   6.519  
   6.520 -  have split_beta: "\<And>f. split f = (\<lambda>x. f (fst x) (snd x))" by (simp add: fun_eq_iff)
   6.521 -
   6.522 -  note space_simps = space_pair_measure space_sigma algebra.simps
   6.523 -
   6.524 -  note finite_var = assms
   6.525 -  note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
   6.526 -  note XZ = finite_random_variable_pairI[OF finite_var(1,3)]
   6.527 -  note ZX = finite_random_variable_pairI[OF finite_var(3,1)]
   6.528 -  note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
   6.529 -  note XYZ = finite_random_variable_pairI[OF finite_var(1) YZ]
   6.530 -  note finite = finite_var(3) YZ XZ XYZ
   6.531 -  note finite = finite[THEN finite_distribution_finite, simplified space_simps]
   6.532 -
   6.533 +  note YZ = finite_random_variable_pairI[OF Y Z]
   6.534 +  note XZ = finite_random_variable_pairI[OF X Z]
   6.535 +  note ZX = finite_random_variable_pairI[OF Z X]
   6.536 +  note YZ = finite_random_variable_pairI[OF Y Z]
   6.537 +  note XYZ = finite_random_variable_pairI[OF X YZ]
   6.538 +  note finite = Z YZ XZ XYZ
   6.539    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.540            \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
   6.541      unfolding joint_distribution_commute_singleton[of X]
   6.542      unfolding joint_distribution_assoc_singleton[symmetric]
   6.543 -    using finite_distribution_order(6)[OF finite_var(2) ZX]
   6.544 -    by (auto simp: space_simps)
   6.545 +    using finite_distribution_order(6)[OF Y ZX]
   6.546 +    by auto
   6.547  
   6.548    note order = order
   6.549 -    finite_distribution_order(5,6)[OF finite_var(1) YZ, simplified space_simps]
   6.550 -    finite_distribution_order(5,6)[OF finite_var(2,3), simplified space_simps]
   6.551 +    finite_distribution_order(5,6)[OF X YZ]
   6.552 +    finite_distribution_order(5,6)[OF Y Z]
   6.553  
   6.554    have "- conditional_mutual_information b MX MY MZ X Y Z = - (\<Sum>(x, y, z) \<in> ?M. ?dXYZ {(x, y, z)} *
   6.555      log b (?dXYZ {(x, y, z)} / (?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})))"
   6.556 -    unfolding conditional_mutual_information_generic_eq[OF assms] neg_equal_iff_equal
   6.557 -    by (intro setsum_cong) (auto intro!: arg_cong[where f="log b"] simp: real_of_pextreal_mult[symmetric])
   6.558 +    unfolding conditional_mutual_information_generic_eq[OF assms] neg_equal_iff_equal by auto
   6.559    also have "\<dots> \<le> log b (\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})"
   6.560 -    unfolding split_beta
   6.561 +    unfolding split_beta'
   6.562    proof (rule log_setsum_divide)
   6.563      show "?M \<noteq> {}" using False by simp
   6.564      show "1 < b" using b_gt_1 .
   6.565 @@ -757,33 +755,31 @@
   6.566        unfolding setsum_commute[of _ "space MY"]
   6.567        unfolding setsum_commute[of _ "space MZ"]
   6.568        by (simp_all add: space_pair_measure
   6.569 -        setsum_real_joint_distribution_singleton[OF `finite_random_variable MX X` YZ]
   6.570 -        setsum_real_joint_distribution_singleton[OF `finite_random_variable MY Y` finite_var(3)]
   6.571 -        setsum_real_distribution[OF `finite_random_variable MZ Z`])
   6.572 +                        setsum_joint_distribution_singleton[OF X YZ]
   6.573 +                        setsum_joint_distribution_singleton[OF Y Z]
   6.574 +                        setsum_distribution[OF Z])
   6.575  
   6.576      fix x assume "x \<in> ?M"
   6.577      let ?x = "(fst x, fst (snd x), snd (snd x))"
   6.578  
   6.579 -    show "0 \<le> ?dXYZ {?x}" using real_pextreal_nonneg .
   6.580 -    show "0 \<le> ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
   6.581 -     by (simp add: real_pextreal_nonneg mult_nonneg_nonneg divide_nonneg_nonneg)
   6.582 +    show "0 \<le> ?dXYZ {?x}"
   6.583 +      "0 \<le> ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
   6.584 +     by (simp_all add: mult_nonneg_nonneg divide_nonneg_nonneg)
   6.585  
   6.586      assume *: "0 < ?dXYZ {?x}"
   6.587 -    with `x \<in> ?M` show "0 < ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
   6.588 -      using finite order
   6.589 -      by (cases x)
   6.590 -         (auto simp add: zero_less_real_of_pextreal zero_less_mult_iff zero_less_divide_iff)
   6.591 +    with `x \<in> ?M` finite order show "0 < ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
   6.592 +      by (cases x) (auto simp add: zero_le_mult_iff zero_le_divide_iff less_le)
   6.593    qed
   6.594    also have "(\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z}) = (\<Sum>z\<in>space MZ. ?dZ {z})"
   6.595      apply (simp add: setsum_cartesian_product')
   6.596      apply (subst setsum_commute)
   6.597      apply (subst (2) setsum_commute)
   6.598      by (auto simp: setsum_divide_distrib[symmetric] setsum_product[symmetric]
   6.599 -                   setsum_real_joint_distribution_singleton[OF finite_var(1,3)]
   6.600 -                   setsum_real_joint_distribution_singleton[OF finite_var(2,3)]
   6.601 +                   setsum_joint_distribution_singleton[OF X Z]
   6.602 +                   setsum_joint_distribution_singleton[OF Y Z]
   6.603            intro!: setsum_cong)
   6.604    also have "log b (\<Sum>z\<in>space MZ. ?dZ {z}) = 0"
   6.605 -    unfolding setsum_real_distribution[OF finite_var(3)] by simp
   6.606 +    unfolding setsum_real_distribution[OF Z] by simp
   6.607    finally show ?thesis by simp
   6.608  qed
   6.609  
   6.610 @@ -800,57 +796,52 @@
   6.611  abbreviation (in information_space)
   6.612    conditional_entropy_Pow ("\<H>'(_ | _')") where
   6.613    "\<H>(X | Y) \<equiv> conditional_entropy b
   6.614 -    \<lparr> space = X`space M, sets = Pow (X`space M), measure = distribution X \<rparr>
   6.615 -    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = distribution Y \<rparr> X Y"
   6.616 +    \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr>
   6.617 +    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = extreal\<circ>distribution Y \<rparr> X Y"
   6.618  
   6.619  lemma (in information_space) conditional_entropy_positive:
   6.620    "simple_function M X \<Longrightarrow> simple_function M Y \<Longrightarrow> 0 \<le> \<H>(X | Y)"
   6.621    unfolding conditional_entropy_def by (auto intro!: conditional_mutual_information_positive)
   6.622  
   6.623 -lemma (in measure_space) empty_measureI: "A = {} \<Longrightarrow> \<mu> A = 0" by simp
   6.624 -
   6.625  lemma (in information_space) conditional_entropy_generic_eq:
   6.626    fixes MX :: "('c, 'd) measure_space_scheme" and MY :: "('e, 'f) measure_space_scheme"
   6.627    assumes MX: "finite_random_variable MX X"
   6.628    assumes MZ: "finite_random_variable MZ Z"
   6.629    shows "conditional_entropy b MX MZ X Z =
   6.630       - (\<Sum>(x, z)\<in>space MX \<times> space MZ.
   6.631 -         real (joint_distribution X Z {(x, z)}) *
   6.632 -         log b (real (joint_distribution X Z {(x, z)}) / real (distribution Z {z})))"
   6.633 +         joint_distribution X Z {(x, z)} * log b (joint_distribution X Z {(x, z)} / distribution Z {z}))"
   6.634  proof -
   6.635    interpret MX: finite_sigma_algebra MX using MX by simp
   6.636    interpret MZ: finite_sigma_algebra MZ using MZ by simp
   6.637    let "?XXZ x y z" = "joint_distribution X (\<lambda>x. (X x, Z x)) {(x, y, z)}"
   6.638    let "?XZ x z" = "joint_distribution X Z {(x, z)}"
   6.639    let "?Z z" = "distribution Z {z}"
   6.640 -  let "?f x y z" = "log b (real (?XXZ x y z) / (real (?XZ x z) * real (?XZ y z / ?Z z)))"
   6.641 +  let "?f x y z" = "log b (?XXZ x y z * ?Z z / (?XZ x z * ?XZ y z))"
   6.642    { fix x z have "?XXZ x x z = ?XZ x z"
   6.643 -      unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>]) }
   6.644 +      unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>']) }
   6.645    note this[simp]
   6.646    { fix x x' :: 'c and z assume "x' \<noteq> x"
   6.647      then have "?XXZ x x' z = 0"
   6.648 -      by (auto simp: distribution_def intro!: arg_cong[where f=\<mu>] empty_measureI) }
   6.649 +      by (auto simp: distribution_def empty_measure'[symmetric]
   6.650 +               simp del: empty_measure' intro!: arg_cong[where f=\<mu>']) }
   6.651    note this[simp]
   6.652    { fix x x' z assume *: "x \<in> space MX" "z \<in> space MZ"
   6.653 -    then have "(\<Sum>x'\<in>space MX. real (?XXZ x x' z) * ?f x x' z)
   6.654 -      = (\<Sum>x'\<in>space MX. if x = x' then real (?XZ x z) * ?f x x z else 0)"
   6.655 +    then have "(\<Sum>x'\<in>space MX. ?XXZ x x' z * ?f x x' z)
   6.656 +      = (\<Sum>x'\<in>space MX. if x = x' then ?XZ x z * ?f x x z else 0)"
   6.657        by (auto intro!: setsum_cong)
   6.658 -    also have "\<dots> = real (?XZ x z) * ?f x x z"
   6.659 +    also have "\<dots> = ?XZ x z * ?f x x z"
   6.660        using `x \<in> space MX` by (simp add: setsum_cases[OF MX.finite_space])
   6.661 -    also have "\<dots> = real (?XZ x z) * log b (real (?Z z) / real (?XZ x z))"
   6.662 -      by (auto simp: real_of_pextreal_mult[symmetric])
   6.663 -    also have "\<dots> = - real (?XZ x z) * log b (real (?XZ x z) / real (?Z z))"
   6.664 -      using assms[THEN finite_distribution_finite]
   6.665 +    also have "\<dots> = ?XZ x z * log b (?Z z / ?XZ x z)" by auto
   6.666 +    also have "\<dots> = - ?XZ x z * log b (?XZ x z / ?Z z)"
   6.667        using finite_distribution_order(6)[OF MX MZ]
   6.668 -      by (auto simp: log_simps field_simps zero_less_mult_iff zero_less_real_of_pextreal real_of_pextreal_eq_0)
   6.669 -    finally have "(\<Sum>x'\<in>space MX. real (?XXZ x x' z) * ?f x x' z) =
   6.670 -      - real (?XZ x z) * log b (real (?XZ x z) / real (?Z z))" . }
   6.671 +      by (auto simp: log_simps field_simps zero_less_mult_iff)
   6.672 +    finally have "(\<Sum>x'\<in>space MX. ?XXZ x x' z * ?f x x' z) = - ?XZ x z * log b (?XZ x z / ?Z z)" . }
   6.673    note * = this
   6.674    show ?thesis
   6.675      unfolding conditional_entropy_def
   6.676      unfolding conditional_mutual_information_generic_eq[OF MX MX MZ]
   6.677      by (auto simp: setsum_cartesian_product' setsum_negf[symmetric]
   6.678 -                   setsum_commute[of _ "space MZ"] *   simp del: divide_pextreal_def
   6.679 +                   setsum_commute[of _ "space MZ"] *
   6.680               intro!: setsum_cong)
   6.681  qed
   6.682  
   6.683 @@ -858,29 +849,27 @@
   6.684    assumes "simple_function M X" "simple_function M Z"
   6.685    shows "\<H>(X | Z) =
   6.686       - (\<Sum>(x, z)\<in>X ` space M \<times> Z ` space M.
   6.687 -         real (joint_distribution X Z {(x, z)}) *
   6.688 -         log b (real (joint_distribution X Z {(x, z)}) / real (distribution Z {z})))"
   6.689 -  using conditional_entropy_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]]
   6.690 -  by simp
   6.691 +         joint_distribution X Z {(x, z)} *
   6.692 +         log b (joint_distribution X Z {(x, z)} / distribution Z {z}))"
   6.693 +  by (subst conditional_entropy_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]])
   6.694 +     simp
   6.695  
   6.696  lemma (in information_space) conditional_entropy_eq_ce_with_hypothesis:
   6.697    assumes X: "simple_function M X" and Y: "simple_function M Y"
   6.698    shows "\<H>(X | Y) =
   6.699 -    -(\<Sum>y\<in>Y`space M. real (distribution Y {y}) *
   6.700 -      (\<Sum>x\<in>X`space M. real (joint_distribution X Y {(x,y)}) / real (distribution Y {(y)}) *
   6.701 -              log b (real (joint_distribution X Y {(x,y)}) / real (distribution Y {(y)}))))"
   6.702 +    -(\<Sum>y\<in>Y`space M. distribution Y {y} *
   6.703 +      (\<Sum>x\<in>X`space M. joint_distribution X Y {(x,y)} / distribution Y {(y)} *
   6.704 +              log b (joint_distribution X Y {(x,y)} / distribution Y {(y)})))"
   6.705    unfolding conditional_entropy_eq[OF assms]
   6.706 -  using finite_distribution_finite[OF finite_random_variable_pairI[OF assms[THEN simple_function_imp_finite_random_variable]]]
   6.707    using finite_distribution_order(5,6)[OF assms[THEN simple_function_imp_finite_random_variable]]
   6.708 -  using finite_distribution_finite[OF Y[THEN simple_function_imp_finite_random_variable]]
   6.709 -  by (auto simp: setsum_cartesian_product'  setsum_commute[of _ "Y`space M"] setsum_right_distrib real_of_pextreal_eq_0
   6.710 +  by (auto simp: setsum_cartesian_product'  setsum_commute[of _ "Y`space M"] setsum_right_distrib
   6.711             intro!: setsum_cong)
   6.712  
   6.713  lemma (in information_space) conditional_entropy_eq_cartesian_product:
   6.714    assumes "simple_function M X" "simple_function M Y"
   6.715    shows "\<H>(X | Y) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
   6.716 -    real (joint_distribution X Y {(x,y)}) *
   6.717 -    log b (real (joint_distribution X Y {(x,y)}) / real (distribution Y {y})))"
   6.718 +    joint_distribution X Y {(x,y)} *
   6.719 +    log b (joint_distribution X Y {(x,y)} / distribution Y {y}))"
   6.720    unfolding conditional_entropy_eq[OF assms]
   6.721    by (auto intro!: setsum_cong simp: setsum_cartesian_product')
   6.722  
   6.723 @@ -890,24 +879,22 @@
   6.724    assumes X: "simple_function M X" and Z: "simple_function M Z"
   6.725    shows  "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)"
   6.726  proof -
   6.727 -  let "?XZ x z" = "real (joint_distribution X Z {(x, z)})"
   6.728 -  let "?Z z" = "real (distribution Z {z})"
   6.729 -  let "?X x" = "real (distribution X {x})"
   6.730 +  let "?XZ x z" = "joint_distribution X Z {(x, z)}"
   6.731 +  let "?Z z" = "distribution Z {z}"
   6.732 +  let "?X x" = "distribution X {x}"
   6.733    note fX = X[THEN simple_function_imp_finite_random_variable]
   6.734    note fZ = Z[THEN simple_function_imp_finite_random_variable]
   6.735 -  note fX[THEN finite_distribution_finite, simp] and fZ[THEN finite_distribution_finite, simp]
   6.736    note finite_distribution_order[OF fX fZ, simp]
   6.737    { fix x z assume "x \<in> X`space M" "z \<in> Z`space M"
   6.738      have "?XZ x z * log b (?XZ x z / (?X x * ?Z z)) =
   6.739            ?XZ x z * log b (?XZ x z / ?Z z) - ?XZ x z * log b (?X x)"
   6.740 -      by (auto simp: log_simps real_of_pextreal_mult[symmetric] zero_less_mult_iff
   6.741 -                     zero_less_real_of_pextreal field_simps real_of_pextreal_eq_0 abs_mult) }
   6.742 +      by (auto simp: log_simps zero_le_mult_iff field_simps less_le) }
   6.743    note * = this
   6.744    show ?thesis
   6.745      unfolding entropy_eq[OF X] conditional_entropy_eq[OF X Z] mutual_information_eq[OF X Z]
   6.746 -    using setsum_real_joint_distribution_singleton[OF fZ fX, unfolded joint_distribution_commute_singleton[of Z X]]
   6.747 +    using setsum_joint_distribution_singleton[OF fZ fX, unfolded joint_distribution_commute_singleton[of Z X]]
   6.748      by (simp add: * setsum_cartesian_product' setsum_subtractf setsum_left_distrib[symmetric]
   6.749 -                     setsum_real_distribution)
   6.750 +                     setsum_distribution)
   6.751  qed
   6.752  
   6.753  lemma (in information_space) conditional_entropy_less_eq_entropy:
   6.754 @@ -923,21 +910,19 @@
   6.755    assumes X: "simple_function M X" and Y: "simple_function M Y"
   6.756    shows  "\<H>(\<lambda>x. (X x, Y x)) = \<H>(X) + \<H>(Y|X)"
   6.757  proof -
   6.758 -  let "?XY x y" = "real (joint_distribution X Y {(x, y)})"
   6.759 -  let "?Y y" = "real (distribution Y {y})"
   6.760 -  let "?X x" = "real (distribution X {x})"
   6.761 +  let "?XY x y" = "joint_distribution X Y {(x, y)}"
   6.762 +  let "?Y y" = "distribution Y {y}"
   6.763 +  let "?X x" = "distribution X {x}"
   6.764    note fX = X[THEN simple_function_imp_finite_random_variable]
   6.765    note fY = Y[THEN simple_function_imp_finite_random_variable]
   6.766 -  note fX[THEN finite_distribution_finite, simp] and fY[THEN finite_distribution_finite, simp]
   6.767    note finite_distribution_order[OF fX fY, simp]
   6.768    { fix x y assume "x \<in> X`space M" "y \<in> Y`space M"
   6.769      have "?XY x y * log b (?XY x y / ?X x) =
   6.770            ?XY x y * log b (?XY x y) - ?XY x y * log b (?X x)"
   6.771 -      by (auto simp: log_simps real_of_pextreal_mult[symmetric] zero_less_mult_iff
   6.772 -                     zero_less_real_of_pextreal field_simps real_of_pextreal_eq_0 abs_mult) }
   6.773 +      by (auto simp: log_simps zero_le_mult_iff field_simps less_le) }
   6.774    note * = this
   6.775    show ?thesis
   6.776 -    using setsum_real_joint_distribution_singleton[OF fY fX]
   6.777 +    using setsum_joint_distribution_singleton[OF fY fX]
   6.778      unfolding entropy_eq[OF X] conditional_entropy_eq_cartesian_product[OF Y X] entropy_eq_cartesian_product[OF X Y]
   6.779      unfolding joint_distribution_commute_singleton[of Y X] setsum_commute[of _ "X`space M"]
   6.780      by (simp add: * setsum_subtractf setsum_left_distrib[symmetric])
   6.781 @@ -1063,23 +1048,21 @@
   6.782    assumes svi: "subvimage (space M) X P"
   6.783    shows "\<H>(X) = \<H>(P) + \<H>(X|P)"
   6.784  proof -
   6.785 -  let "?XP x p" = "real (joint_distribution X P {(x, p)})"
   6.786 -  let "?X x" = "real (distribution X {x})"
   6.787 -  let "?P p" = "real (distribution P {p})"
   6.788 +  let "?XP x p" = "joint_distribution X P {(x, p)}"
   6.789 +  let "?X x" = "distribution X {x}"
   6.790 +  let "?P p" = "distribution P {p}"
   6.791    note fX = sf(1)[THEN simple_function_imp_finite_random_variable]
   6.792    note fP = sf(2)[THEN simple_function_imp_finite_random_variable]
   6.793 -  note fX[THEN finite_distribution_finite, simp] and fP[THEN finite_distribution_finite, simp]
   6.794    note finite_distribution_order[OF fX fP, simp]
   6.795 -  have "(\<Sum>x\<in>X ` space M. real (distribution X {x}) * log b (real (distribution X {x}))) =
   6.796 -    (\<Sum>y\<in>P `space M. \<Sum>x\<in>X ` space M.
   6.797 -    real (joint_distribution X P {(x, y)}) * log b (real (joint_distribution X P {(x, y)})))"
   6.798 +  have "(\<Sum>x\<in>X ` space M. ?X x * log b (?X x)) =
   6.799 +    (\<Sum>y\<in>P `space M. \<Sum>x\<in>X ` space M. ?XP x y * log b (?XP x y))"
   6.800    proof (subst setsum_image_split[OF svi],
   6.801        safe intro!: setsum_mono_zero_cong_left imageI)
   6.802      show "finite (X ` space M)" "finite (X ` space M)" "finite (P ` space M)"
   6.803        using sf unfolding simple_function_def by auto
   6.804    next
   6.805      fix p x assume in_space: "p \<in> space M" "x \<in> space M"
   6.806 -    assume "real (joint_distribution X P {(X x, P p)}) * log b (real (joint_distribution X P {(X x, P p)})) \<noteq> 0"
   6.807 +    assume "?XP (X x) (P p) * log b (?XP (X x) (P p)) \<noteq> 0"
   6.808      hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M \<noteq> {}" by (auto simp: distribution_def)
   6.809      with svi[unfolded subvimage_def, rule_format, OF `x \<in> space M`]
   6.810      show "x \<in> P -` {P p}" by auto
   6.811 @@ -1091,20 +1074,16 @@
   6.812        by auto
   6.813      hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M = X -` {X x} \<inter> space M"
   6.814        by auto
   6.815 -    thus "real (distribution X {X x}) * log b (real (distribution X {X x})) =
   6.816 -          real (joint_distribution X P {(X x, P p)}) *
   6.817 -          log b (real (joint_distribution X P {(X x, P p)}))"
   6.818 +    thus "?X (X x) * log b (?X (X x)) = ?XP (X x) (P p) * log b (?XP (X x) (P p))"
   6.819        by (auto simp: distribution_def)
   6.820    qed
   6.821 -  moreover have "\<And>x y. real (joint_distribution X P {(x, y)}) *
   6.822 -      log b (real (joint_distribution X P {(x, y)}) / real (distribution P {y})) =
   6.823 -      real (joint_distribution X P {(x, y)}) * log b (real (joint_distribution X P {(x, y)})) -
   6.824 -      real (joint_distribution X P {(x, y)}) * log b (real (distribution P {y}))"
   6.825 +  moreover have "\<And>x y. ?XP x y * log b (?XP x y / ?P y) =
   6.826 +      ?XP x y * log b (?XP x y) - ?XP x y * log b (?P y)"
   6.827      by (auto simp add: log_simps zero_less_mult_iff field_simps)
   6.828    ultimately show ?thesis
   6.829      unfolding sf[THEN entropy_eq] conditional_entropy_eq[OF sf]
   6.830 -    using setsum_real_joint_distribution_singleton[OF fX fP]
   6.831 -    by (simp add: setsum_cartesian_product' setsum_subtractf setsum_real_distribution
   6.832 +    using setsum_joint_distribution_singleton[OF fX fP]
   6.833 +    by (simp add: setsum_cartesian_product' setsum_subtractf setsum_distribution
   6.834        setsum_left_distrib[symmetric] setsum_commute[where B="P`space M"])
   6.835  qed
   6.836  
     7.1 --- a/src/HOL/Probability/Lebesgue_Integration.thy	Mon Mar 14 14:37:47 2011 +0100
     7.2 +++ b/src/HOL/Probability/Lebesgue_Integration.thy	Mon Mar 14 14:37:49 2011 +0100
     7.3 @@ -6,6 +6,88 @@
     7.4  imports Measure Borel_Space
     7.5  begin
     7.6  
     7.7 +lemma extreal_indicator_pos[simp,intro]: "0 \<le> (indicator A x ::extreal)"
     7.8 +  unfolding indicator_def by auto
     7.9 +
    7.10 +lemma tendsto_real_max:
    7.11 +  fixes x y :: real
    7.12 +  assumes "(X ---> x) net"
    7.13 +  assumes "(Y ---> y) net"
    7.14 +  shows "((\<lambda>x. max (X x) (Y x)) ---> max x y) net"
    7.15 +proof -
    7.16 +  have *: "\<And>x y :: real. max x y = y + ((x - y) + norm (x - y)) / 2"
    7.17 +    by (auto split: split_max simp: field_simps)
    7.18 +  show ?thesis
    7.19 +    unfolding *
    7.20 +    by (intro tendsto_add assms tendsto_divide tendsto_norm tendsto_diff) auto
    7.21 +qed
    7.22 +
    7.23 +lemma (in measure_space) measure_Union:
    7.24 +  assumes "finite S" "S \<subseteq> sets M" "\<And>A B. A \<in> S \<Longrightarrow> B \<in> S \<Longrightarrow> A \<noteq> B \<Longrightarrow> A \<inter> B = {}"
    7.25 +  shows "setsum \<mu> S = \<mu> (\<Union>S)"
    7.26 +proof -
    7.27 +  have "setsum \<mu> S = \<mu> (\<Union>i\<in>S. i)"
    7.28 +    using assms by (intro measure_setsum[OF `finite S`]) (auto simp: disjoint_family_on_def)
    7.29 +  also have "\<dots> = \<mu> (\<Union>S)" by (auto intro!: arg_cong[where f=\<mu>])
    7.30 +  finally show ?thesis .
    7.31 +qed
    7.32 +
    7.33 +lemma (in sigma_algebra) measurable_sets2[intro]:
    7.34 +  assumes "f \<in> measurable M M'" "g \<in> measurable M M''"
    7.35 +  and "A \<in> sets M'" "B \<in> sets M''"
    7.36 +  shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
    7.37 +proof -
    7.38 +  have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
    7.39 +    by auto
    7.40 +  then show ?thesis using assms by (auto intro: measurable_sets)
    7.41 +qed
    7.42 +
    7.43 +lemma incseq_extreal: "incseq f \<Longrightarrow> incseq (\<lambda>x. extreal (f x))"
    7.44 +  unfolding incseq_def by auto
    7.45 +
    7.46 +lemma incseq_Suc_iff: "incseq f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
    7.47 +proof
    7.48 +  assume "\<forall>n. f n \<le> f (Suc n)" then show "incseq f" by (auto intro!: incseq_SucI)
    7.49 +qed (auto simp: incseq_def)
    7.50 +
    7.51 +lemma borel_measurable_real_floor:
    7.52 +  "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
    7.53 +  unfolding borel.borel_measurable_iff_ge
    7.54 +proof (intro allI)
    7.55 +  fix a :: real
    7.56 +  { fix x have "a \<le> real \<lfloor>x\<rfloor> \<longleftrightarrow> real \<lceil>a\<rceil> \<le> x"
    7.57 +      using le_floor_eq[of "\<lceil>a\<rceil>" x] ceiling_le_iff[of a "\<lfloor>x\<rfloor>"]
    7.58 +      unfolding real_eq_of_int by simp }
    7.59 +  then have "{w::real \<in> space borel. a \<le> real \<lfloor>w\<rfloor>} = {real \<lceil>a\<rceil>..}" by auto
    7.60 +  then show "{w::real \<in> space borel. a \<le> real \<lfloor>w\<rfloor>} \<in> sets borel" by auto
    7.61 +qed
    7.62 +
    7.63 +lemma measure_preservingD2:
    7.64 +  "f \<in> measure_preserving A B \<Longrightarrow> f \<in> measurable A B"
    7.65 +  unfolding measure_preserving_def by auto
    7.66 +
    7.67 +lemma measure_preservingD3:
    7.68 +  "f \<in> measure_preserving A B \<Longrightarrow> f \<in> space A \<rightarrow> space B"
    7.69 +  unfolding measure_preserving_def measurable_def by auto
    7.70 +
    7.71 +lemma measure_preservingD:
    7.72 +  "T \<in> measure_preserving A B \<Longrightarrow> X \<in> sets B \<Longrightarrow> measure A (T -` X \<inter> space A) = measure B X"
    7.73 +  unfolding measure_preserving_def by auto
    7.74 +
    7.75 +lemma (in sigma_algebra) borel_measurable_real_natfloor[intro, simp]:
    7.76 +  assumes "f \<in> borel_measurable M"
    7.77 +  shows "(\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
    7.78 +proof -
    7.79 +  have "\<And>x. real (natfloor (f x)) = max 0 (real \<lfloor>f x\<rfloor>)"
    7.80 +    by (auto simp: max_def natfloor_def)
    7.81 +  with borel_measurable_max[OF measurable_comp[OF assms borel_measurable_real_floor] borel_measurable_const]
    7.82 +  show ?thesis by (simp add: comp_def)
    7.83 +qed
    7.84 +
    7.85 +lemma (in measure_space) AE_not_in:
    7.86 +  assumes N: "N \<in> null_sets" shows "AE x. x \<notin> N"
    7.87 +  using N by (rule AE_I') auto
    7.88 +
    7.89  lemma sums_If_finite:
    7.90    fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
    7.91    assumes finite: "finite {r. P r}"
    7.92 @@ -55,8 +137,17 @@
    7.93      by (auto intro!: finite_UN simp del: UN_simps simp: simple_function_def)
    7.94  qed
    7.95  
    7.96 +lemma (in sigma_algebra) simple_function_measurable2[intro]:
    7.97 +  assumes "simple_function M f" "simple_function M g"
    7.98 +  shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
    7.99 +proof -
   7.100 +  have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
   7.101 +    by auto
   7.102 +  then show ?thesis using assms[THEN simple_functionD(2)] by auto
   7.103 +qed
   7.104 +
   7.105  lemma (in sigma_algebra) simple_function_indicator_representation:
   7.106 -  fixes f ::"'a \<Rightarrow> pextreal"
   7.107 +  fixes f ::"'a \<Rightarrow> extreal"
   7.108    assumes f: "simple_function M f" and x: "x \<in> space M"
   7.109    shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
   7.110    (is "?l = ?r")
   7.111 @@ -71,7 +162,7 @@
   7.112  qed
   7.113  
   7.114  lemma (in measure_space) simple_function_notspace:
   7.115 -  "simple_function M (\<lambda>x. h x * indicator (- space M) x::pextreal)" (is "simple_function M ?h")
   7.116 +  "simple_function M (\<lambda>x. h x * indicator (- space M) x::extreal)" (is "simple_function M ?h")
   7.117  proof -
   7.118    have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
   7.119    hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
   7.120 @@ -111,16 +202,22 @@
   7.121  qed
   7.122  
   7.123  lemma (in sigma_algebra) simple_function_borel_measurable:
   7.124 -  fixes f :: "'a \<Rightarrow> 'x::t2_space"
   7.125 +  fixes f :: "'a \<Rightarrow> 'x::{t2_space}"
   7.126    assumes "f \<in> borel_measurable M" and "finite (f ` space M)"
   7.127    shows "simple_function M f"
   7.128    using assms unfolding simple_function_def
   7.129    by (auto intro: borel_measurable_vimage)
   7.130  
   7.131 +lemma (in sigma_algebra) simple_function_eq_borel_measurable:
   7.132 +  fixes f :: "'a \<Rightarrow> extreal"
   7.133 +  shows "simple_function M f \<longleftrightarrow> finite (f`space M) \<and> f \<in> borel_measurable M"
   7.134 +  using simple_function_borel_measurable[of f]
   7.135 +    borel_measurable_simple_function[of f]
   7.136 +  by (fastsimp simp: simple_function_def)
   7.137 +
   7.138  lemma (in sigma_algebra) simple_function_const[intro, simp]:
   7.139    "simple_function M (\<lambda>x. c)"
   7.140    by (auto intro: finite_subset simp: simple_function_def)
   7.141 -
   7.142  lemma (in sigma_algebra) simple_function_compose[intro, simp]:
   7.143    assumes "simple_function M f"
   7.144    shows "simple_function M (g \<circ> f)"
   7.145 @@ -189,6 +286,7 @@
   7.146    and simple_function_mult[intro, simp] = simple_function_compose2[where h="op *"]
   7.147    and simple_function_div[intro, simp] = simple_function_compose2[where h="op /"]
   7.148    and simple_function_inverse[intro, simp] = simple_function_compose[where g="inverse"]
   7.149 +  and simple_function_max[intro, simp] = simple_function_compose2[where h=max]
   7.150  
   7.151  lemma (in sigma_algebra) simple_function_setsum[intro, simp]:
   7.152    assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   7.153 @@ -197,247 +295,168 @@
   7.154    assume "finite P" from this assms show ?thesis by induct auto
   7.155  qed auto
   7.156  
   7.157 -lemma (in sigma_algebra) simple_function_le_measurable:
   7.158 -  assumes "simple_function M f" "simple_function M g"
   7.159 -  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
   7.160 -proof -
   7.161 -  have *: "{x \<in> space M. f x \<le> g x} =
   7.162 -    (\<Union>(F, G)\<in>f`space M \<times> g`space M.
   7.163 -      if F \<le> G then (f -` {F} \<inter> space M) \<inter> (g -` {G} \<inter> space M) else {})"
   7.164 -    apply (auto split: split_if_asm)
   7.165 -    apply (rule_tac x=x in bexI)
   7.166 -    apply (rule_tac x=x in bexI)
   7.167 -    by simp_all
   7.168 -  have **: "\<And>x y. x \<in> space M \<Longrightarrow> y \<in> space M \<Longrightarrow>
   7.169 -    (f -` {f x} \<inter> space M) \<inter> (g -` {g y} \<inter> space M) \<in> sets M"
   7.170 -    using assms unfolding simple_function_def by auto
   7.171 -  have "finite (f`space M \<times> g`space M)"
   7.172 -    using assms unfolding simple_function_def by auto
   7.173 -  thus ?thesis unfolding *
   7.174 -    apply (rule finite_UN)
   7.175 -    using assms unfolding simple_function_def
   7.176 -    by (auto intro!: **)
   7.177 -qed
   7.178 +lemma (in sigma_algebra)
   7.179 +  fixes f g :: "'a \<Rightarrow> real" assumes sf: "simple_function M f"
   7.180 +  shows simple_function_extreal[intro, simp]: "simple_function M (\<lambda>x. extreal (f x))"
   7.181 +  by (auto intro!: simple_function_compose1[OF sf])
   7.182 +
   7.183 +lemma (in sigma_algebra)
   7.184 +  fixes f g :: "'a \<Rightarrow> nat" assumes sf: "simple_function M f"
   7.185 +  shows simple_function_real_of_nat[intro, simp]: "simple_function M (\<lambda>x. real (f x))"
   7.186 +  by (auto intro!: simple_function_compose1[OF sf])
   7.187  
   7.188  lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence:
   7.189 -  fixes u :: "'a \<Rightarrow> pextreal"
   7.190 +  fixes u :: "'a \<Rightarrow> extreal"
   7.191    assumes u: "u \<in> borel_measurable M"
   7.192 -  shows "\<exists>f. (\<forall>i. simple_function M (f i) \<and> (\<forall>x\<in>space M. f i x \<noteq> \<omega>)) \<and> f \<up> u"
   7.193 +  shows "\<exists>f. incseq f \<and> (\<forall>i. \<infinity> \<notin> range (f i) \<and> simple_function M (f i)) \<and>
   7.194 +             (\<forall>x. (SUP i. f i x) = max 0 (u x)) \<and> (\<forall>i x. 0 \<le> f i x)"
   7.195  proof -
   7.196 -  have "\<exists>f. \<forall>x j. (of_nat j \<le> u x \<longrightarrow> f x j = j*2^j) \<and>
   7.197 -    (u x < of_nat j \<longrightarrow> of_nat (f x j) \<le> u x * 2^j \<and> u x * 2^j < of_nat (Suc (f x j)))"
   7.198 -    (is "\<exists>f. \<forall>x j. ?P x j (f x j)")
   7.199 -  proof(rule choice, rule, rule choice, rule)
   7.200 -    fix x j show "\<exists>n. ?P x j n"
   7.201 -    proof cases
   7.202 -      assume *: "u x < of_nat j"
   7.203 -      then obtain r where r: "u x = Real r" "0 \<le> r" by (cases "u x") auto
   7.204 -      from reals_Archimedean6a[of "r * 2^j"]
   7.205 -      obtain n :: nat where "real n \<le> r * 2 ^ j" "r * 2 ^ j < real (Suc n)"
   7.206 -        using `0 \<le> r` by (auto simp: zero_le_power zero_le_mult_iff)
   7.207 -      thus ?thesis using r * by (auto intro!: exI[of _ n])
   7.208 -    qed auto
   7.209 -  qed
   7.210 -  then obtain f where top: "\<And>j x. of_nat j \<le> u x \<Longrightarrow> f x j = j*2^j" and
   7.211 -    upper: "\<And>j x. u x < of_nat j \<Longrightarrow> of_nat (f x j) \<le> u x * 2^j" and
   7.212 -    lower: "\<And>j x. u x < of_nat j \<Longrightarrow> u x * 2^j < of_nat (Suc (f x j))" by blast
   7.213 -
   7.214 -  { fix j x P
   7.215 -    assume 1: "of_nat j \<le> u x \<Longrightarrow> P (j * 2^j)"
   7.216 -    assume 2: "\<And>k. \<lbrakk> u x < of_nat j ; of_nat k \<le> u x * 2^j ; u x * 2^j < of_nat (Suc k) \<rbrakk> \<Longrightarrow> P k"
   7.217 -    have "P (f x j)"
   7.218 -    proof cases
   7.219 -      assume "of_nat j \<le> u x" thus "P (f x j)"
   7.220 -        using top[of j x] 1 by auto
   7.221 -    next
   7.222 -      assume "\<not> of_nat j \<le> u x"
   7.223 -      hence "u x < of_nat j" "of_nat (f x j) \<le> u x * 2^j" "u x * 2^j < of_nat (Suc (f x j))"
   7.224 -        using upper lower by auto
   7.225 -      from 2[OF this] show "P (f x j)" .
   7.226 -    qed }
   7.227 -  note fI = this
   7.228 -
   7.229 -  { fix j x
   7.230 -    have "f x j = j * 2 ^ j \<longleftrightarrow> of_nat j \<le> u x"
   7.231 -      by (rule fI, simp, cases "u x") (auto split: split_if_asm) }
   7.232 -  note f_eq = this
   7.233 -
   7.234 -  { fix j x
   7.235 -    have "f x j \<le> j * 2 ^ j"
   7.236 -    proof (rule fI)
   7.237 -      fix k assume *: "u x < of_nat j"
   7.238 -      assume "of_nat k \<le> u x * 2 ^ j"
   7.239 -      also have "\<dots> \<le> of_nat (j * 2^j)"
   7.240 -        using * by (cases "u x") (auto simp: zero_le_mult_iff)
   7.241 -      finally show "k \<le> j*2^j" by (auto simp del: real_of_nat_mult)
   7.242 -    qed simp }
   7.243 +  def f \<equiv> "\<lambda>x i. if real i \<le> u x then i * 2 ^ i else natfloor (real (u x) * 2 ^ i)"
   7.244 +  { fix x j have "f x j \<le> j * 2 ^ j" unfolding f_def
   7.245 +    proof (split split_if, intro conjI impI)
   7.246 +      assume "\<not> real j \<le> u x"
   7.247 +      then have "natfloor (real (u x) * 2 ^ j) \<le> natfloor (j * 2 ^ j)"
   7.248 +         by (cases "u x") (auto intro!: natfloor_mono simp: mult_nonneg_nonneg)
   7.249 +      moreover have "real (natfloor (j * 2 ^ j)) \<le> j * 2^j"
   7.250 +        by (intro real_natfloor_le) (auto simp: mult_nonneg_nonneg)
   7.251 +      ultimately show "natfloor (real (u x) * 2 ^ j) \<le> j * 2 ^ j"
   7.252 +        unfolding real_of_nat_le_iff by auto
   7.253 +    qed auto }
   7.254    note f_upper = this
   7.255  
   7.256 -  let "?g j x" = "of_nat (f x j) / 2^j :: pextreal"
   7.257 -  show ?thesis unfolding simple_function_def isoton_fun_expand unfolding isoton_def
   7.258 -  proof (safe intro!: exI[of _ ?g])
   7.259 -    fix j
   7.260 -    have *: "?g j ` space M \<subseteq> (\<lambda>x. of_nat x / 2^j) ` {..j * 2^j}"
   7.261 -      using f_upper by auto
   7.262 -    thus "finite (?g j ` space M)" by (rule finite_subset) auto
   7.263 -  next
   7.264 -    fix j t assume "t \<in> space M"
   7.265 -    have **: "?g j -` {?g j t} \<inter> space M = {x \<in> space M. f t j = f x j}"
   7.266 -      by (auto simp: power_le_zero_eq Real_eq_Real mult_le_0_iff)
   7.267 +  have real_f:
   7.268 +    "\<And>i x. real (f x i) = (if real i \<le> u x then i * 2 ^ i else real (natfloor (real (u x) * 2 ^ i)))"
   7.269 +    unfolding f_def by auto
   7.270  
   7.271 -    show "?g j -` {?g j t} \<inter> space M \<in> sets M"
   7.272 -    proof cases
   7.273 -      assume "of_nat j \<le> u t"
   7.274 -      hence "?g j -` {?g j t} \<inter> space M = {x\<in>space M. of_nat j \<le> u x}"
   7.275 -        unfolding ** f_eq[symmetric] by auto
   7.276 -      thus "?g j -` {?g j t} \<inter> space M \<in> sets M"
   7.277 -        using u by auto
   7.278 -    next
   7.279 -      assume not_t: "\<not> of_nat j \<le> u t"
   7.280 -      hence less: "f t j < j*2^j" using f_eq[symmetric] `f t j \<le> j*2^j` by auto
   7.281 -      have split_vimage: "?g j -` {?g j t} \<inter> space M =
   7.282 -          {x\<in>space M. of_nat (f t j)/2^j \<le> u x} \<inter> {x\<in>space M. u x < of_nat (Suc (f t j))/2^j}"
   7.283 -        unfolding **
   7.284 -      proof safe
   7.285 -        fix x assume [simp]: "f t j = f x j"
   7.286 -        have *: "\<not> of_nat j \<le> u x" using not_t f_eq[symmetric] by simp
   7.287 -        hence "of_nat (f t j) \<le> u x * 2^j \<and> u x * 2^j < of_nat (Suc (f t j))"
   7.288 -          using upper lower by auto
   7.289 -        hence "of_nat (f t j) / 2^j \<le> u x \<and> u x < of_nat (Suc (f t j))/2^j" using *
   7.290 -          by (cases "u x") (auto simp: zero_le_mult_iff power_le_zero_eq divide_real_def[symmetric] field_simps)
   7.291 -        thus "of_nat (f t j) / 2^j \<le> u x" "u x < of_nat (Suc (f t j))/2^j" by auto
   7.292 +  let "?g j x" = "real (f x j) / 2^j :: extreal"
   7.293 +  show ?thesis
   7.294 +  proof (intro exI[of _ ?g] conjI allI ballI)
   7.295 +    fix i
   7.296 +    have "simple_function M (\<lambda>x. real (f x i))"
   7.297 +    proof (intro simple_function_borel_measurable)
   7.298 +      show "(\<lambda>x. real (f x i)) \<in> borel_measurable M"
   7.299 +        using u by (auto intro!: measurable_If simp: real_f)
   7.300 +      have "(\<lambda>x. real (f x i))`space M \<subseteq> real`{..i*2^i}"
   7.301 +        using f_upper[of _ i] by auto
   7.302 +      then show "finite ((\<lambda>x. real (f x i))`space M)"
   7.303 +        by (rule finite_subset) auto
   7.304 +    qed
   7.305 +    then show "simple_function M (?g i)"
   7.306 +      by (auto intro: simple_function_extreal simple_function_div)
   7.307 +  next
   7.308 +    show "incseq ?g"
   7.309 +    proof (intro incseq_extreal incseq_SucI le_funI)
   7.310 +      fix x and i :: nat
   7.311 +      have "f x i * 2 \<le> f x (Suc i)" unfolding f_def
   7.312 +      proof ((split split_if)+, intro conjI impI)
   7.313 +        assume "extreal (real i) \<le> u x" "\<not> extreal (real (Suc i)) \<le> u x"
   7.314 +        then show "i * 2 ^ i * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)"
   7.315 +          by (cases "u x") (auto intro!: le_natfloor)
   7.316        next
   7.317 -        fix x
   7.318 -        assume "of_nat (f t j) / 2^j \<le> u x" "u x < of_nat (Suc (f t j))/2^j"
   7.319 -        hence "of_nat (f t j) \<le> u x * 2 ^ j \<and> u x * 2 ^ j < of_nat (Suc (f t j))"
   7.320 -          by (cases "u x") (auto simp: zero_le_mult_iff power_le_zero_eq divide_real_def[symmetric] field_simps)
   7.321 -        hence 1: "of_nat (f t j) \<le> u x * 2 ^ j" and 2: "u x * 2 ^ j < of_nat (Suc (f t j))" by auto
   7.322 -        note 2
   7.323 -        also have "\<dots> \<le> of_nat (j*2^j)"
   7.324 -          using less by (auto simp: zero_le_mult_iff simp del: real_of_nat_mult)
   7.325 -        finally have bound_ux: "u x < of_nat j"
   7.326 -          by (cases "u x") (auto simp: zero_le_mult_iff power_le_zero_eq)
   7.327 -        show "f t j = f x j"
   7.328 -        proof (rule antisym)
   7.329 -          from 1 lower[OF bound_ux]
   7.330 -          show "f t j \<le> f x j" by (cases "u x") (auto split: split_if_asm)
   7.331 -          from upper[OF bound_ux] 2
   7.332 -          show "f x j \<le> f t j" by (cases "u x") (auto split: split_if_asm)
   7.333 +        assume "\<not> extreal (real i) \<le> u x" "extreal (real (Suc i)) \<le> u x"
   7.334 +        then show "natfloor (real (u x) * 2 ^ i) * 2 \<le> Suc i * 2 ^ Suc i"
   7.335 +          by (cases "u x") auto
   7.336 +      next
   7.337 +        assume "\<not> extreal (real i) \<le> u x" "\<not> extreal (real (Suc i)) \<le> u x"
   7.338 +        have "natfloor (real (u x) * 2 ^ i) * 2 = natfloor (real (u x) * 2 ^ i) * natfloor 2"
   7.339 +          by simp
   7.340 +        also have "\<dots> \<le> natfloor (real (u x) * 2 ^ i * 2)"
   7.341 +        proof cases
   7.342 +          assume "0 \<le> u x" then show ?thesis
   7.343 +            by (intro le_mult_natfloor) (cases "u x", auto intro!: mult_nonneg_nonneg)
   7.344 +        next
   7.345 +          assume "\<not> 0 \<le> u x" then show ?thesis
   7.346 +            by (cases "u x") (auto simp: natfloor_neg mult_nonpos_nonneg)
   7.347          qed
   7.348 -      qed
   7.349 -      show ?thesis unfolding split_vimage using u by auto
   7.350 +        also have "\<dots> = natfloor (real (u x) * 2 ^ Suc i)"
   7.351 +          by (simp add: ac_simps)
   7.352 +        finally show "natfloor (real (u x) * 2 ^ i) * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)" .
   7.353 +      qed simp
   7.354 +      then show "?g i x \<le> ?g (Suc i) x"
   7.355 +        by (auto simp: field_simps)
   7.356      qed
   7.357    next
   7.358 -    fix j t assume "?g j t = \<omega>" thus False by (auto simp: power_le_zero_eq)
   7.359 -  next
   7.360 -    fix t
   7.361 -    { fix i
   7.362 -      have "f t i * 2 \<le> f t (Suc i)"
   7.363 -      proof (rule fI)
   7.364 -        assume "of_nat (Suc i) \<le> u t"
   7.365 -        hence "of_nat i \<le> u t" by (cases "u t") auto
   7.366 -        thus "f t i * 2 \<le> Suc i * 2 ^ Suc i" unfolding f_eq[symmetric] by simp
   7.367 -      next
   7.368 -        fix k
   7.369 -        assume *: "u t * 2 ^ Suc i < of_nat (Suc k)"
   7.370 -        show "f t i * 2 \<le> k"
   7.371 -        proof (rule fI)
   7.372 -          assume "of_nat i \<le> u t"
   7.373 -          hence "of_nat (i * 2^Suc i) \<le> u t * 2^Suc i"
   7.374 -            by (cases "u t") (auto simp: zero_le_mult_iff power_le_zero_eq)
   7.375 -          also have "\<dots> < of_nat (Suc k)" using * by auto
   7.376 -          finally show "i * 2 ^ i * 2 \<le> k"
   7.377 -            by (auto simp del: real_of_nat_mult)
   7.378 -        next
   7.379 -          fix j assume "of_nat j \<le> u t * 2 ^ i"
   7.380 -          with * show "j * 2 \<le> k" by (cases "u t") (auto simp: zero_le_mult_iff power_le_zero_eq)
   7.381 +    fix x show "(SUP i. ?g i x) = max 0 (u x)"
   7.382 +    proof (rule extreal_SUPI)
   7.383 +      fix i show "?g i x \<le> max 0 (u x)" unfolding max_def f_def
   7.384 +        by (cases "u x") (auto simp: field_simps real_natfloor_le natfloor_neg
   7.385 +                                     mult_nonpos_nonneg mult_nonneg_nonneg)
   7.386 +    next
   7.387 +      fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> ?g i x \<le> y"
   7.388 +      have "\<And>i. 0 \<le> ?g i x" by (auto simp: divide_nonneg_pos)
   7.389 +      from order_trans[OF this *] have "0 \<le> y" by simp
   7.390 +      show "max 0 (u x) \<le> y"
   7.391 +      proof (cases y)
   7.392 +        case (real r)
   7.393 +        with * have *: "\<And>i. f x i \<le> r * 2^i" by (auto simp: divide_le_eq)
   7.394 +        from real_arch_lt[of r] * have "u x \<noteq> \<infinity>" by (auto simp: f_def) (metis less_le_not_le)
   7.395 +        then have "\<exists>p. max 0 (u x) = extreal p \<and> 0 \<le> p" by (cases "u x") (auto simp: max_def)
   7.396 +        then guess p .. note ux = this
   7.397 +        obtain m :: nat where m: "p < real m" using real_arch_lt ..
   7.398 +        have "p \<le> r"
   7.399 +        proof (rule ccontr)
   7.400 +          assume "\<not> p \<le> r"
   7.401 +          with LIMSEQ_inverse_realpow_zero[unfolded LIMSEQ_iff, rule_format, of 2 "p - r"]
   7.402 +          obtain N where "\<forall>n\<ge>N. r * 2^n < p * 2^n - 1" by (auto simp: inverse_eq_divide field_simps)
   7.403 +          then have "r * 2^max N m < p * 2^max N m - 1" by simp
   7.404 +          moreover
   7.405 +          have "real (natfloor (p * 2 ^ max N m)) \<le> r * 2 ^ max N m"
   7.406 +            using *[of "max N m"] m unfolding real_f using ux
   7.407 +            by (cases "0 \<le> u x") (simp_all add: max_def mult_nonneg_nonneg split: split_if_asm)
   7.408 +          then have "p * 2 ^ max N m - 1 < r * 2 ^ max N m"
   7.409 +            by (metis real_natfloor_gt_diff_one less_le_trans)
   7.410 +          ultimately show False by auto
   7.411          qed
   7.412 -      qed
   7.413 -      thus "?g i t \<le> ?g (Suc i) t"
   7.414 -        by (auto simp: zero_le_mult_iff power_le_zero_eq divide_real_def[symmetric] field_simps) }
   7.415 -    hence mono: "mono (\<lambda>i. ?g i t)" unfolding mono_iff_le_Suc by auto
   7.416 +        then show "max 0 (u x) \<le> y" using real ux by simp
   7.417 +      qed (insert `0 \<le> y`, auto)
   7.418 +    qed
   7.419 +  qed (auto simp: divide_nonneg_pos)
   7.420 +qed
   7.421  
   7.422 -    show "(SUP j. of_nat (f t j) / 2 ^ j) = u t"
   7.423 -    proof (rule pextreal_SUPI)
   7.424 -      fix j show "of_nat (f t j) / 2 ^ j \<le> u t"
   7.425 -      proof (rule fI)
   7.426 -        assume "of_nat j \<le> u t" thus "of_nat (j * 2 ^ j) / 2 ^ j \<le> u t"
   7.427 -          by (cases "u t") (auto simp: power_le_zero_eq divide_real_def[symmetric] field_simps)
   7.428 -      next
   7.429 -        fix k assume "of_nat k \<le> u t * 2 ^ j"
   7.430 -        thus "of_nat k / 2 ^ j \<le> u t"
   7.431 -          by (cases "u t")
   7.432 -             (auto simp: power_le_zero_eq divide_real_def[symmetric] field_simps zero_le_mult_iff)
   7.433 -      qed
   7.434 -    next
   7.435 -      fix y :: pextreal assume *: "\<And>j. j \<in> UNIV \<Longrightarrow> of_nat (f t j) / 2 ^ j \<le> y"
   7.436 -      show "u t \<le> y"
   7.437 -      proof (cases "u t")
   7.438 -        case (preal r)
   7.439 -        show ?thesis
   7.440 -        proof (rule ccontr)
   7.441 -          assume "\<not> u t \<le> y"
   7.442 -          then obtain p where p: "y = Real p" "0 \<le> p" "p < r" using preal by (cases y) auto
   7.443 -          with LIMSEQ_inverse_realpow_zero[of 2, unfolded LIMSEQ_iff, rule_format, of "r - p"]
   7.444 -          obtain n where n: "\<And>N. n \<le> N \<Longrightarrow> inverse (2^N) < r - p" by auto
   7.445 -          let ?N = "max n (natfloor r + 1)"
   7.446 -          have "u t < of_nat ?N" "n \<le> ?N"
   7.447 -            using ge_natfloor_plus_one_imp_gt[of r n] preal
   7.448 -            using real_natfloor_add_one_gt
   7.449 -            by (auto simp: max_def real_of_nat_Suc)
   7.450 -          from lower[OF this(1)]
   7.451 -          have "r < (real (f t ?N) + 1) / 2 ^ ?N" unfolding less_divide_eq
   7.452 -            using preal by (auto simp add: power_le_zero_eq zero_le_mult_iff real_of_nat_Suc split: split_if_asm)
   7.453 -          hence "u t < of_nat (f t ?N) / 2 ^ ?N + 1 / 2 ^ ?N"
   7.454 -            using preal by (auto simp: field_simps divide_real_def[symmetric])
   7.455 -          with n[OF `n \<le> ?N`] p preal *[of ?N]
   7.456 -          show False
   7.457 -            by (cases "f t ?N") (auto simp: power_le_zero_eq split: split_if_asm)
   7.458 -        qed
   7.459 -      next
   7.460 -        case infinite
   7.461 -        { fix j have "f t j = j*2^j" using top[of j t] infinite by simp
   7.462 -          hence "of_nat j \<le> y" using *[of j]
   7.463 -            by (cases y) (auto simp: power_le_zero_eq zero_le_mult_iff field_simps) }
   7.464 -        note all_less_y = this
   7.465 -        show ?thesis unfolding infinite
   7.466 -        proof (rule ccontr)
   7.467 -          assume "\<not> \<omega> \<le> y" then obtain r where r: "y = Real r" "0 \<le> r" by (cases y) auto
   7.468 -          moreover obtain n ::nat where "r < real n" using ex_less_of_nat by (auto simp: real_eq_of_nat)
   7.469 -          with all_less_y[of n] r show False by auto
   7.470 -        qed
   7.471 -      qed
   7.472 -    qed
   7.473 +lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence':
   7.474 +  fixes u :: "'a \<Rightarrow> extreal"
   7.475 +  assumes u: "u \<in> borel_measurable M"
   7.476 +  obtains f where "\<And>i. simple_function M (f i)" "incseq f" "\<And>i. \<infinity> \<notin> range (f i)"
   7.477 +    "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
   7.478 +  using borel_measurable_implies_simple_function_sequence[OF u] by auto
   7.479 +
   7.480 +lemma (in sigma_algebra) simple_function_If_set:
   7.481 +  assumes sf: "simple_function M f" "simple_function M g" and A: "A \<inter> space M \<in> sets M"
   7.482 +  shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
   7.483 +proof -
   7.484 +  def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
   7.485 +  show ?thesis unfolding simple_function_def
   7.486 +  proof safe
   7.487 +    have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
   7.488 +    from finite_subset[OF this] assms
   7.489 +    show "finite (?IF ` space M)" unfolding simple_function_def by auto
   7.490 +  next
   7.491 +    fix x assume "x \<in> space M"
   7.492 +    then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
   7.493 +      then ((F (f x) \<inter> (A \<inter> space M)) \<union> (G (f x) - (G (f x) \<inter> (A \<inter> space M))))
   7.494 +      else ((F (g x) \<inter> (A \<inter> space M)) \<union> (G (g x) - (G (g x) \<inter> (A \<inter> space M)))))"
   7.495 +      using sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
   7.496 +    have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
   7.497 +      unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
   7.498 +    show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
   7.499    qed
   7.500  qed
   7.501  
   7.502 -lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence':
   7.503 -  fixes u :: "'a \<Rightarrow> pextreal"
   7.504 -  assumes "u \<in> borel_measurable M"
   7.505 -  obtains (x) f where "f \<up> u" "\<And>i. simple_function M (f i)" "\<And>i. \<omega>\<notin>f i`space M"
   7.506 +lemma (in sigma_algebra) simple_function_If:
   7.507 +  assumes sf: "simple_function M f" "simple_function M g" and P: "{x\<in>space M. P x} \<in> sets M"
   7.508 +  shows "simple_function M (\<lambda>x. if P x then f x else g x)"
   7.509  proof -
   7.510 -  from borel_measurable_implies_simple_function_sequence[OF assms]
   7.511 -  obtain f where x: "\<And>i. simple_function M (f i)" "f \<up> u"
   7.512 -    and fin: "\<And>i. \<And>x. x\<in>space M \<Longrightarrow> f i x \<noteq> \<omega>" by auto
   7.513 -  { fix i from fin[of _ i] have "\<omega> \<notin> f i`space M" by fastsimp }
   7.514 -  with x show thesis by (auto intro!: that[of f])
   7.515 +  have "{x\<in>space M. P x} = {x. P x} \<inter> space M" by auto
   7.516 +  with simple_function_If_set[OF sf, of "{x. P x}"] P show ?thesis by simp
   7.517  qed
   7.518  
   7.519 -lemma (in sigma_algebra) simple_function_eq_borel_measurable:
   7.520 -  fixes f :: "'a \<Rightarrow> pextreal"
   7.521 -  shows "simple_function M f \<longleftrightarrow>
   7.522 -    finite (f`space M) \<and> f \<in> borel_measurable M"
   7.523 -  using simple_function_borel_measurable[of f]
   7.524 -    borel_measurable_simple_function[of f]
   7.525 -  by (fastsimp simp: simple_function_def)
   7.526 -
   7.527  lemma (in measure_space) simple_function_restricted:
   7.528 -  fixes f :: "'a \<Rightarrow> pextreal" assumes "A \<in> sets M"
   7.529 +  fixes f :: "'a \<Rightarrow> extreal" assumes "A \<in> sets M"
   7.530    shows "simple_function (restricted_space A) f \<longleftrightarrow> simple_function M (\<lambda>x. f x * indicator A x)"
   7.531      (is "simple_function ?R f \<longleftrightarrow> simple_function M ?f")
   7.532  proof -
   7.533    interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
   7.534 -  have "finite (f`A) \<longleftrightarrow> finite (?f`space M)"
   7.535 +  have f: "finite (f`A) \<longleftrightarrow> finite (?f`space M)"
   7.536    proof cases
   7.537      assume "A = space M"
   7.538      then have "f`A = ?f`space M" by (fastsimp simp: image_iff)
   7.539 @@ -456,7 +475,7 @@
   7.540          using `A \<in> sets M` sets_into_space by (auto intro!: bexI[of _ x])
   7.541      next
   7.542        fix x
   7.543 -      assume "indicator A x \<noteq> (0::pextreal)"
   7.544 +      assume "indicator A x \<noteq> (0::extreal)"
   7.545        then have "x \<in> A" by (auto simp: indicator_def split: split_if_asm)
   7.546        moreover assume "x \<in> space M" "\<forall>y\<in>A. ?f x \<noteq> f y"
   7.547        ultimately show "f x = 0" by auto
   7.548 @@ -467,7 +486,8 @@
   7.549      unfolding simple_function_eq_borel_measurable
   7.550        R.simple_function_eq_borel_measurable
   7.551      unfolding borel_measurable_restricted[OF `A \<in> sets M`]
   7.552 -    by auto
   7.553 +    using assms(1)[THEN sets_into_space]
   7.554 +    by (auto simp: indicator_def)
   7.555  qed
   7.556  
   7.557  lemma (in sigma_algebra) simple_function_subalgebra:
   7.558 @@ -504,7 +524,7 @@
   7.559    "integral\<^isup>S M f = (\<Sum>x \<in> f ` space M. x * measure M (f -` {x} \<inter> space M))"
   7.560  
   7.561  syntax
   7.562 -  "_simple_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> pextreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> pextreal" ("\<integral>\<^isup>S _. _ \<partial>_" [60,61] 110)
   7.563 +  "_simple_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> extreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> extreal" ("\<integral>\<^isup>S _. _ \<partial>_" [60,61] 110)
   7.564  
   7.565  translations
   7.566    "\<integral>\<^isup>S x. f \<partial>M" == "CONST integral\<^isup>S M (%x. f)"
   7.567 @@ -540,7 +560,7 @@
   7.568  qed
   7.569  
   7.570  lemma (in measure_space) simple_function_partition:
   7.571 -  assumes "simple_function M f" and "simple_function M g"
   7.572 +  assumes f: "simple_function M f" and g: "simple_function M g"
   7.573    shows "integral\<^isup>S M f = (\<Sum>A\<in>(\<lambda>x. f -` {f x} \<inter> g -` {g x} \<inter> space M) ` space M. the_elem (f`A) * \<mu> A)"
   7.574      (is "_ = setsum _ (?p ` space M)")
   7.575  proof-
   7.576 @@ -559,23 +579,16 @@
   7.577      hence "finite (?p ` (A \<inter> space M))"
   7.578        by (rule finite_subset) auto }
   7.579    note this[intro, simp]
   7.580 +  note sets = simple_function_measurable2[OF f g]
   7.581  
   7.582    { fix x assume "x \<in> space M"
   7.583      have "\<Union>(?sub (f x)) = (f -` {f x} \<inter> space M)" by auto
   7.584 -    moreover {
   7.585 -      fix x y
   7.586 -      have *: "f -` {f x} \<inter> g -` {g x} \<inter> space M
   7.587 -          = (f -` {f x} \<inter> space M) \<inter> (g -` {g x} \<inter> space M)" by auto
   7.588 -      assume "x \<in> space M" "y \<in> space M"
   7.589 -      hence "f -` {f x} \<inter> g -` {g x} \<inter> space M \<in> sets M"
   7.590 -        using assms unfolding simple_function_def * by auto }
   7.591 -    ultimately
   7.592 -    have "\<mu> (f -` {f x} \<inter> space M) = setsum (\<mu>) (?sub (f x))"
   7.593 -      by (subst measure_finitely_additive) auto }
   7.594 +    with sets have "\<mu> (f -` {f x} \<inter> space M) = setsum \<mu> (?sub (f x))"
   7.595 +      by (subst measure_Union) auto }
   7.596    hence "integral\<^isup>S M f = (\<Sum>(x,A)\<in>?SIGMA. x * \<mu> A)"
   7.597 -    unfolding simple_integral_def
   7.598 -    by (subst setsum_Sigma[symmetric],
   7.599 -       auto intro!: setsum_cong simp: setsum_right_distrib[symmetric])
   7.600 +    unfolding simple_integral_def using f sets
   7.601 +    by (subst setsum_Sigma[symmetric])
   7.602 +       (auto intro!: setsum_cong setsum_extreal_right_distrib)
   7.603    also have "\<dots> = (\<Sum>A\<in>?p ` space M. the_elem (f`A) * \<mu> A)"
   7.604    proof -
   7.605      have [simp]: "\<And>x. x \<in> space M \<Longrightarrow> f ` ?p x = {f x}" by (auto intro!: imageI)
   7.606 @@ -595,7 +608,7 @@
   7.607  qed
   7.608  
   7.609  lemma (in measure_space) simple_integral_add[simp]:
   7.610 -  assumes "simple_function M f" and "simple_function M g"
   7.611 +  assumes f: "simple_function M f" and "\<And>x. 0 \<le> f x" and g: "simple_function M g" and "\<And>x. 0 \<le> g x"
   7.612    shows "(\<integral>\<^isup>Sx. f x + g x \<partial>M) = integral\<^isup>S M f + integral\<^isup>S M g"
   7.613  proof -
   7.614    { fix x let ?S = "g -` {g x} \<inter> f -` {f x} \<inter> space M"
   7.615 @@ -603,63 +616,43 @@
   7.616      hence "(\<lambda>a. f a + g a) ` ?S = {f x + g x}" "f ` ?S = {f x}" "g ` ?S = {g x}"
   7.617          "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M = ?S"
   7.618        by auto }
   7.619 -  thus ?thesis
   7.620 +  with assms show ?thesis
   7.621      unfolding
   7.622 -      simple_function_partition[OF simple_function_add[OF assms] simple_function_Pair[OF assms]]
   7.623 -      simple_function_partition[OF `simple_function M f` `simple_function M g`]
   7.624 -      simple_function_partition[OF `simple_function M g` `simple_function M f`]
   7.625 -    apply (subst (3) Int_commute)
   7.626 -    by (auto simp add: field_simps setsum_addf[symmetric] intro!: setsum_cong)
   7.627 +      simple_function_partition[OF simple_function_add[OF f g] simple_function_Pair[OF f g]]
   7.628 +      simple_function_partition[OF f g]
   7.629 +      simple_function_partition[OF g f]
   7.630 +    by (subst (3) Int_commute)
   7.631 +       (auto simp add: extreal_left_distrib setsum_addf[symmetric] intro!: setsum_cong)
   7.632  qed
   7.633  
   7.634  lemma (in measure_space) simple_integral_setsum[simp]:
   7.635 +  assumes "\<And>i x. i \<in> P \<Longrightarrow> 0 \<le> f i x"
   7.636    assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   7.637    shows "(\<integral>\<^isup>Sx. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>S M (f i))"
   7.638  proof cases
   7.639    assume "finite P"
   7.640    from this assms show ?thesis
   7.641 -    by induct (auto simp: simple_function_setsum simple_integral_add)
   7.642 +    by induct (auto simp: simple_function_setsum simple_integral_add setsum_nonneg)
   7.643  qed auto
   7.644  
   7.645  lemma (in measure_space) simple_integral_mult[simp]:
   7.646 -  assumes "simple_function M f"
   7.647 +  assumes f: "simple_function M f" "\<And>x. 0 \<le> f x"
   7.648    shows "(\<integral>\<^isup>Sx. c * f x \<partial>M) = c * integral\<^isup>S M f"
   7.649  proof -
   7.650 -  note mult = simple_function_mult[OF simple_function_const[of c] assms]
   7.651 +  note mult = simple_function_mult[OF simple_function_const[of c] f(1)]
   7.652    { fix x let ?S = "f -` {f x} \<inter> (\<lambda>x. c * f x) -` {c * f x} \<inter> space M"
   7.653      assume "x \<in> space M"
   7.654      hence "(\<lambda>x. c * f x) ` ?S = {c * f x}" "f ` ?S = {f x}"
   7.655        by auto }
   7.656 -  thus ?thesis
   7.657 -    unfolding simple_function_partition[OF mult assms]
   7.658 -      simple_function_partition[OF assms mult]
   7.659 -    by (auto simp: setsum_right_distrib field_simps intro!: setsum_cong)
   7.660 -qed
   7.661 -
   7.662 -lemma (in sigma_algebra) simple_function_If:
   7.663 -  assumes sf: "simple_function M f" "simple_function M g" and A: "A \<in> sets M"
   7.664 -  shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
   7.665 -proof -
   7.666 -  def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
   7.667 -  show ?thesis unfolding simple_function_def
   7.668 -  proof safe
   7.669 -    have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
   7.670 -    from finite_subset[OF this] assms
   7.671 -    show "finite (?IF ` space M)" unfolding simple_function_def by auto
   7.672 -  next
   7.673 -    fix x assume "x \<in> space M"
   7.674 -    then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
   7.675 -      then ((F (f x) \<inter> A) \<union> (G (f x) - (G (f x) \<inter> A)))
   7.676 -      else ((F (g x) \<inter> A) \<union> (G (g x) - (G (g x) \<inter> A))))"
   7.677 -      using sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
   7.678 -    have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
   7.679 -      unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
   7.680 -    show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
   7.681 -  qed
   7.682 +  with assms show ?thesis
   7.683 +    unfolding simple_function_partition[OF mult f(1)]
   7.684 +              simple_function_partition[OF f(1) mult]
   7.685 +    by (subst setsum_extreal_right_distrib)
   7.686 +       (auto intro!: extreal_0_le_mult setsum_cong simp: mult_assoc)
   7.687  qed
   7.688  
   7.689  lemma (in measure_space) simple_integral_mono_AE:
   7.690 -  assumes "simple_function M f" and "simple_function M g"
   7.691 +  assumes f: "simple_function M f" and g: "simple_function M g"
   7.692    and mono: "AE x. f x \<le> g x"
   7.693    shows "integral\<^isup>S M f \<le> integral\<^isup>S M g"
   7.694  proof -
   7.695 @@ -668,14 +661,16 @@
   7.696      "\<And>x. f -` {f x} \<inter> g -` {g x} \<inter> space M = ?S x" by auto
   7.697    show ?thesis
   7.698      unfolding *
   7.699 -      simple_function_partition[OF `simple_function M f` `simple_function M g`]
   7.700 -      simple_function_partition[OF `simple_function M g` `simple_function M f`]
   7.701 +      simple_function_partition[OF f g]
   7.702 +      simple_function_partition[OF g f]
   7.703    proof (safe intro!: setsum_mono)
   7.704      fix x assume "x \<in> space M"
   7.705      then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
   7.706      show "the_elem (f`?S x) * \<mu> (?S x) \<le> the_elem (g`?S x) * \<mu> (?S x)"
   7.707      proof (cases "f x \<le> g x")
   7.708 -      case True then show ?thesis using * by (auto intro!: mult_right_mono)
   7.709 +      case True then show ?thesis
   7.710 +        using * assms(1,2)[THEN simple_functionD(2)]
   7.711 +        by (auto intro!: extreal_mult_right_mono)
   7.712      next
   7.713        case False
   7.714        obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
   7.715 @@ -685,7 +680,10 @@
   7.716          by (rule_tac Int) (auto intro!: simple_functionD)
   7.717        ultimately have "\<mu> (?S x) \<le> \<mu> N"
   7.718          using `N \<in> sets M` by (auto intro!: measure_mono)
   7.719 -      then show ?thesis using `\<mu> N = 0` by auto
   7.720 +      moreover have "0 \<le> \<mu> (?S x)"
   7.721 +        using assms(1,2)[THEN simple_functionD(2)] by auto
   7.722 +      ultimately have "\<mu> (?S x) = 0" using `\<mu> N = 0` by auto
   7.723 +      then show ?thesis by simp
   7.724      qed
   7.725    qed
   7.726  qed
   7.727 @@ -697,7 +695,8 @@
   7.728    using assms by (intro simple_integral_mono_AE) auto
   7.729  
   7.730  lemma (in measure_space) simple_integral_cong_AE:
   7.731 -  assumes "simple_function M f" "simple_function M g" and "AE x. f x = g x"
   7.732 +  assumes "simple_function M f" and "simple_function M g"
   7.733 +  and "AE x. f x = g x"
   7.734    shows "integral\<^isup>S M f = integral\<^isup>S M g"
   7.735    using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
   7.736  
   7.737 @@ -765,7 +764,7 @@
   7.738    assume "space M = {}" hence "A = {}" using sets_into_space[OF assms] by auto
   7.739    thus ?thesis unfolding simple_integral_def using `space M = {}` by auto
   7.740  next
   7.741 -  assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::pextreal}" by auto
   7.742 +  assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::extreal}" by auto
   7.743    thus ?thesis
   7.744      using simple_integral_indicator[OF assms simple_function_const[of 1]]
   7.745      using sets_into_space[OF assms]
   7.746 @@ -773,13 +772,13 @@
   7.747  qed
   7.748  
   7.749  lemma (in measure_space) simple_integral_null_set:
   7.750 -  assumes "simple_function M u" "N \<in> null_sets"
   7.751 +  assumes "simple_function M u" "\<And>x. 0 \<le> u x" and "N \<in> null_sets"
   7.752    shows "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = 0"
   7.753  proof -
   7.754 -  have "AE x. indicator N x = (0 :: pextreal)"
   7.755 +  have "AE x. indicator N x = (0 :: extreal)"
   7.756      using `N \<in> null_sets` by (auto simp: indicator_def intro!: AE_I[of _ N])
   7.757    then have "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = (\<integral>\<^isup>Sx. 0 \<partial>M)"
   7.758 -    using assms by (intro simple_integral_cong_AE) auto
   7.759 +    using assms apply (intro simple_integral_cong_AE) by auto
   7.760    then show ?thesis by simp
   7.761  qed
   7.762  
   7.763 @@ -813,7 +812,7 @@
   7.764      by (auto simp: indicator_def split: split_if_asm)
   7.765    then show "f x * \<mu> (f -` {f x} \<inter> A) =
   7.766      f x * \<mu> (?f -` {f x} \<inter> space M)"
   7.767 -    unfolding pextreal_mult_cancel_left by auto
   7.768 +    unfolding extreal_mult_cancel_left by auto
   7.769  qed
   7.770  
   7.771  lemma (in measure_space) simple_integral_subalgebra:
   7.772 @@ -821,10 +820,6 @@
   7.773    shows "integral\<^isup>S N = integral\<^isup>S M"
   7.774    unfolding simple_integral_def_raw by simp
   7.775  
   7.776 -lemma measure_preservingD:
   7.777 -  "T \<in> measure_preserving A B \<Longrightarrow> X \<in> sets B \<Longrightarrow> measure A (T -` X \<inter> space A) = measure B X"
   7.778 -  unfolding measure_preserving_def by auto
   7.779 -
   7.780  lemma (in measure_space) simple_integral_vimage:
   7.781    assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'"
   7.782      and f: "simple_function M' f"
   7.783 @@ -853,196 +848,164 @@
   7.784    qed
   7.785  qed
   7.786  
   7.787 +lemma (in measure_space) simple_integral_cmult_indicator:
   7.788 +  assumes A: "A \<in> sets M"
   7.789 +  shows "(\<integral>\<^isup>Sx. c * indicator A x \<partial>M) = c * \<mu> A"
   7.790 +  using simple_integral_mult[OF simple_function_indicator[OF A]]
   7.791 +  unfolding simple_integral_indicator_only[OF A] by simp
   7.792 +
   7.793 +lemma (in measure_space) simple_integral_positive:
   7.794 +  assumes f: "simple_function M f" and ae: "AE x. 0 \<le> f x"
   7.795 +  shows "0 \<le> integral\<^isup>S M f"
   7.796 +proof -
   7.797 +  have "integral\<^isup>S M (\<lambda>x. 0) \<le> integral\<^isup>S M f"
   7.798 +    using simple_integral_mono_AE[OF _ f ae] by auto
   7.799 +  then show ?thesis by simp
   7.800 +qed
   7.801 +
   7.802  section "Continuous positive integration"
   7.803  
   7.804  definition positive_integral_def:
   7.805 -  "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> f}. integral\<^isup>S M g)"
   7.806 +  "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f}. integral\<^isup>S M g)"
   7.807  
   7.808  syntax
   7.809 -  "_positive_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> pextreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> pextreal" ("\<integral>\<^isup>+ _. _ \<partial>_" [60,61] 110)
   7.810 +  "_positive_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> extreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> extreal" ("\<integral>\<^isup>+ _. _ \<partial>_" [60,61] 110)
   7.811  
   7.812  translations
   7.813    "\<integral>\<^isup>+ x. f \<partial>M" == "CONST integral\<^isup>P M (%x. f)"
   7.814  
   7.815 -lemma (in measure_space) positive_integral_alt: "integral\<^isup>P M f =
   7.816 -    (SUP g : {g. simple_function M g \<and> g \<le> f \<and> \<omega> \<notin> g`space M}. integral\<^isup>S M g)"
   7.817 -  (is "_ = ?alt")
   7.818 -proof (rule antisym SUP_leI)
   7.819 -  show "integral\<^isup>P M f \<le> ?alt" unfolding positive_integral_def
   7.820 -  proof (safe intro!: SUP_leI)
   7.821 -    fix g assume g: "simple_function M g" "g \<le> f"
   7.822 -    let ?G = "g -` {\<omega>} \<inter> space M"
   7.823 -    show "integral\<^isup>S M g \<le>
   7.824 -      (SUP h : {i. simple_function M i \<and> i \<le> f \<and> \<omega> \<notin> i ` space M}. integral\<^isup>S M h)"
   7.825 -      (is "integral\<^isup>S M g \<le> SUPR ?A _")
   7.826 -    proof cases
   7.827 -      let ?g = "\<lambda>x. indicator (space M - ?G) x * g x"
   7.828 -      have g': "simple_function M ?g"
   7.829 -        using g by (auto intro: simple_functionD)
   7.830 -      moreover
   7.831 -      assume "\<mu> ?G = 0"
   7.832 -      then have "AE x. g x = ?g x" using g
   7.833 -        by (intro AE_I[where N="?G"])
   7.834 -           (auto intro: simple_functionD simp: indicator_def)
   7.835 -      with g(1) g' have "integral\<^isup>S M g = integral\<^isup>S M ?g"
   7.836 -        by (rule simple_integral_cong_AE)
   7.837 -      moreover have "?g \<le> g" by (auto simp: le_fun_def indicator_def)
   7.838 -      from this `g \<le> f` have "?g \<le> f" by (rule order_trans)
   7.839 -      moreover have "\<omega> \<notin> ?g ` space M"
   7.840 -        by (auto simp: indicator_def split: split_if_asm)
   7.841 -      ultimately show ?thesis by (auto intro!: le_SUPI)
   7.842 -    next
   7.843 -      assume "\<mu> ?G \<noteq> 0"
   7.844 -      then have "?G \<noteq> {}" by auto
   7.845 -      then have "\<omega> \<in> g`space M" by force
   7.846 -      then have "space M \<noteq> {}" by auto
   7.847 -      have "SUPR ?A (integral\<^isup>S M) = \<omega>"
   7.848 -      proof (intro SUP_\<omega>[THEN iffD2] allI impI)
   7.849 -        fix x assume "x < \<omega>"
   7.850 -        then guess n unfolding less_\<omega>_Ex_of_nat .. note n = this
   7.851 -        then have "0 < n" by (intro neq0_conv[THEN iffD1] notI) simp
   7.852 -        let ?g = "\<lambda>x. (of_nat n / (if \<mu> ?G = \<omega> then 1 else \<mu> ?G)) * indicator ?G x"
   7.853 -        show "\<exists>i\<in>?A. x < integral\<^isup>S M i"
   7.854 -        proof (intro bexI impI CollectI conjI)
   7.855 -          show "simple_function M ?g" using g
   7.856 -            by (auto intro!: simple_functionD simple_function_add)
   7.857 -          have "?g \<le> g" by (auto simp: le_fun_def indicator_def)
   7.858 -          from this g(2) show "?g \<le> f" by (rule order_trans)
   7.859 -          show "\<omega> \<notin> ?g ` space M"
   7.860 -            using `\<mu> ?G \<noteq> 0` by (auto simp: indicator_def split: split_if_asm)
   7.861 -          have "x < (of_nat n / (if \<mu> ?G = \<omega> then 1 else \<mu> ?G)) * \<mu> ?G"
   7.862 -            using n `\<mu> ?G \<noteq> 0` `0 < n`
   7.863 -            by (auto simp: pextreal_noteq_omega_Ex field_simps)
   7.864 -          also have "\<dots> = integral\<^isup>S M ?g" using g `space M \<noteq> {}`
   7.865 -            by (subst simple_integral_indicator)
   7.866 -               (auto simp: image_constant ac_simps dest: simple_functionD)
   7.867 -          finally show "x < integral\<^isup>S M ?g" .
   7.868 -        qed
   7.869 -      qed
   7.870 -      then show ?thesis by simp
   7.871 -    qed
   7.872 -  qed
   7.873 -qed (auto intro!: SUP_subset simp: positive_integral_def)
   7.874 -
   7.875  lemma (in measure_space) positive_integral_cong_measure:
   7.876    assumes "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A" "sets N = sets M" "space N = space M"
   7.877    shows "integral\<^isup>P N f = integral\<^isup>P M f"
   7.878 -proof -
   7.879 -  interpret v: measure_space N
   7.880 -    by (rule measure_space_cong) fact+
   7.881 -  with assms show ?thesis
   7.882 -    unfolding positive_integral_def SUPR_def
   7.883 -    by (auto intro!: arg_cong[where f=Sup] image_cong
   7.884 -             simp: simple_integral_cong_measure[OF assms]
   7.885 -                   simple_function_cong_algebra[OF assms(2,3)])
   7.886 -qed
   7.887 +  unfolding positive_integral_def
   7.888 +  unfolding simple_function_cong_algebra[OF assms(2,3), symmetric]
   7.889 +  using AE_cong_measure[OF assms]
   7.890 +  using simple_integral_cong_measure[OF assms]
   7.891 +  by (auto intro!: SUP_cong)
   7.892 +
   7.893 +lemma (in measure_space) positive_integral_positive:
   7.894 +  "0 \<le> integral\<^isup>P M f"
   7.895 +  by (auto intro!: le_SUPI2[of "\<lambda>x. 0"] simp: positive_integral_def le_fun_def)
   7.896  
   7.897 -lemma (in measure_space) positive_integral_alt1:
   7.898 -  "integral\<^isup>P M f =
   7.899 -    (SUP g : {g. simple_function M g \<and> (\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>)}. integral\<^isup>S M g)"
   7.900 -  unfolding positive_integral_alt SUPR_def
   7.901 -proof (safe intro!: arg_cong[where f=Sup])
   7.902 -  fix g let ?g = "\<lambda>x. if x \<in> space M then g x else f x"
   7.903 -  assume "simple_function M g" "\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>"
   7.904 -  hence "?g \<le> f" "simple_function M ?g" "integral\<^isup>S M g = integral\<^isup>S M ?g"
   7.905 -    "\<omega> \<notin> g`space M"
   7.906 -    unfolding le_fun_def by (auto cong: simple_function_cong simple_integral_cong)
   7.907 -  thus "integral\<^isup>S M g \<in> integral\<^isup>S M ` {g. simple_function M g \<and> g \<le> f \<and> \<omega> \<notin> g`space M}"
   7.908 -    by auto
   7.909 -next
   7.910 -  fix g assume "simple_function M g" "g \<le> f" "\<omega> \<notin> g`space M"
   7.911 -  hence "simple_function M g" "\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>"
   7.912 -    by (auto simp add: le_fun_def image_iff)
   7.913 -  thus "integral\<^isup>S M g \<in> integral\<^isup>S M ` {g. simple_function M g \<and> (\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>)}"
   7.914 -    by auto
   7.915 -qed
   7.916 +lemma (in measure_space) positive_integral_def_finite:
   7.917 +  "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f \<and> range g \<subseteq> {0 ..< \<infinity>}}. integral\<^isup>S M g)"
   7.918 +    (is "_ = SUPR ?A ?f")
   7.919 +  unfolding positive_integral_def
   7.920 +proof (safe intro!: antisym SUP_leI)
   7.921 +  fix g assume g: "simple_function M g" "g \<le> max 0 \<circ> f"
   7.922 +  let ?G = "{x \<in> space M. \<not> g x \<noteq> \<infinity>}"
   7.923 +  note gM = g(1)[THEN borel_measurable_simple_function]
   7.924 +  have \<mu>G_pos: "0 \<le> \<mu> ?G" using gM by auto
   7.925 +  let "?g y x" = "if g x = \<infinity> then y else max 0 (g x)"
   7.926 +  from g gM have g_in_A: "\<And>y. 0 \<le> y \<Longrightarrow> y \<noteq> \<infinity> \<Longrightarrow> ?g y \<in> ?A"
   7.927 +    apply (safe intro!: simple_function_max simple_function_If)
   7.928 +    apply (force simp: max_def le_fun_def split: split_if_asm)+
   7.929 +    done
   7.930 +  show "integral\<^isup>S M g \<le> SUPR ?A ?f"
   7.931 +  proof cases
   7.932 +    have g0: "?g 0 \<in> ?A" by (intro g_in_A) auto
   7.933 +    assume "\<mu> ?G = 0"
   7.934 +    with gM have "AE x. x \<notin> ?G" by (simp add: AE_iff_null_set)
   7.935 +    with gM g show ?thesis
   7.936 +      by (intro le_SUPI2[OF g0] simple_integral_mono_AE)
   7.937 +         (auto simp: max_def intro!: simple_function_If)
   7.938 +  next
   7.939 +    assume \<mu>G: "\<mu> ?G \<noteq> 0"
   7.940 +    have "SUPR ?A (integral\<^isup>S M) = \<infinity>"
   7.941 +    proof (intro SUP_PInfty)
   7.942 +      fix n :: nat
   7.943 +      let ?y = "extreal (real n) / (if \<mu> ?G = \<infinity> then 1 else \<mu> ?G)"
   7.944 +      have "0 \<le> ?y" "?y \<noteq> \<infinity>" using \<mu>G \<mu>G_pos by (auto simp: extreal_divide_eq)
   7.945 +      then have "?g ?y \<in> ?A" by (rule g_in_A)
   7.946 +      have "real n \<le> ?y * \<mu> ?G"
   7.947 +        using \<mu>G \<mu>G_pos by (cases "\<mu> ?G") (auto simp: field_simps)
   7.948 +      also have "\<dots> = (\<integral>\<^isup>Sx. ?y * indicator ?G x \<partial>M)"
   7.949 +        using `0 \<le> ?y` `?g ?y \<in> ?A` gM
   7.950 +        by (subst simple_integral_cmult_indicator) auto
   7.951 +      also have "\<dots> \<le> integral\<^isup>S M (?g ?y)" using `?g ?y \<in> ?A` gM
   7.952 +        by (intro simple_integral_mono) auto
   7.953 +      finally show "\<exists>i\<in>?A. real n \<le> integral\<^isup>S M i"
   7.954 +        using `?g ?y \<in> ?A` by blast
   7.955 +    qed
   7.956 +    then show ?thesis by simp
   7.957 +  qed
   7.958 +qed (auto intro: le_SUPI)
   7.959  
   7.960 -lemma (in measure_space) positive_integral_cong:
   7.961 -  assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
   7.962 -  shows "integral\<^isup>P M f = integral\<^isup>P M g"
   7.963 -proof -
   7.964 -  have "\<And>h. (\<forall>x\<in>space M. h x \<le> f x \<and> h x \<noteq> \<omega>) = (\<forall>x\<in>space M. h x \<le> g x \<and> h x \<noteq> \<omega>)"
   7.965 -    using assms by auto
   7.966 -  thus ?thesis unfolding positive_integral_alt1 by auto
   7.967 +lemma (in measure_space) positive_integral_mono_AE:
   7.968 +  assumes ae: "AE x. u x \<le> v x" shows "integral\<^isup>P M u \<le> integral\<^isup>P M v"
   7.969 +  unfolding positive_integral_def
   7.970 +proof (safe intro!: SUP_mono)
   7.971 +  fix n assume n: "simple_function M n" "n \<le> max 0 \<circ> u"
   7.972 +  from ae[THEN AE_E] guess N . note N = this
   7.973 +  then have ae_N: "AE x. x \<notin> N" by (auto intro: AE_not_in)
   7.974 +  let "?n x" = "n x * indicator (space M - N) x"
   7.975 +  have "AE x. n x \<le> ?n x" "simple_function M ?n"
   7.976 +    using n N ae_N by auto
   7.977 +  moreover
   7.978 +  { fix x have "?n x \<le> max 0 (v x)"
   7.979 +    proof cases
   7.980 +      assume x: "x \<in> space M - N"
   7.981 +      with N have "u x \<le> v x" by auto
   7.982 +      with n(2)[THEN le_funD, of x] x show ?thesis
   7.983 +        by (auto simp: max_def split: split_if_asm)
   7.984 +    qed simp }
   7.985 +  then have "?n \<le> max 0 \<circ> v" by (auto simp: le_funI)
   7.986 +  moreover have "integral\<^isup>S M n \<le> integral\<^isup>S M ?n"
   7.987 +    using ae_N N n by (auto intro!: simple_integral_mono_AE)
   7.988 +  ultimately show "\<exists>m\<in>{g. simple_function M g \<and> g \<le> max 0 \<circ> v}. integral\<^isup>S M n \<le> integral\<^isup>S M m"
   7.989 +    by force
   7.990  qed
   7.991  
   7.992 -lemma (in measure_space) positive_integral_eq_simple_integral:
   7.993 -  assumes "simple_function M f"
   7.994 -  shows "integral\<^isup>P M f = integral\<^isup>S M f"
   7.995 -  unfolding positive_integral_def
   7.996 -proof (safe intro!: pextreal_SUPI)
   7.997 -  fix g assume "simple_function M g" "g \<le> f"
   7.998 -  with assms show "integral\<^isup>S M g \<le> integral\<^isup>S M f"
   7.999 -    by (auto intro!: simple_integral_mono simp: le_fun_def)
  7.1000 -next
  7.1001 -  fix y assume "\<forall>x. x\<in>{g. simple_function M g \<and> g \<le> f} \<longrightarrow> integral\<^isup>S M x \<le> y"
  7.1002 -  with assms show "integral\<^isup>S M f \<le> y" by auto
  7.1003 -qed
  7.1004 -
  7.1005 -lemma (in measure_space) positive_integral_mono_AE:
  7.1006 -  assumes ae: "AE x. u x \<le> v x"
  7.1007 -  shows "integral\<^isup>P M u \<le> integral\<^isup>P M v"
  7.1008 -  unfolding positive_integral_alt1
  7.1009 -proof (safe intro!: SUPR_mono)
  7.1010 -  fix a assume a: "simple_function M a" and mono: "\<forall>x\<in>space M. a x \<le> u x \<and> a x \<noteq> \<omega>"
  7.1011 -  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.1012 -    by (auto elim!: AE_E)
  7.1013 -  have "simple_function M (\<lambda>x. a x * indicator (space M - N) x)"
  7.1014 -    using `N \<in> sets M` a by auto
  7.1015 -  with a show "\<exists>b\<in>{g. simple_function M g \<and> (\<forall>x\<in>space M. g x \<le> v x \<and> g x \<noteq> \<omega>)}.
  7.1016 -    integral\<^isup>S M a \<le> integral\<^isup>S M b"
  7.1017 -  proof (safe intro!: bexI[of _ "\<lambda>x. a x * indicator (space M - N) x"]
  7.1018 -                      simple_integral_mono_AE)
  7.1019 -    show "AE x. a x \<le> a x * indicator (space M - N) x"
  7.1020 -    proof (rule AE_I, rule subset_refl)
  7.1021 -      have *: "{x \<in> space M. \<not> a x \<le> a x * indicator (space M - N) x} =
  7.1022 -        N \<inter> {x \<in> space M. a x \<noteq> 0}" (is "?N = _")
  7.1023 -        using `N \<in> sets M`[THEN sets_into_space] by (auto simp: indicator_def)
  7.1024 -      then show "?N \<in> sets M"
  7.1025 -        using `N \<in> sets M` `simple_function M a`[THEN borel_measurable_simple_function]
  7.1026 -        by (auto intro!: measure_mono Int)
  7.1027 -      then have "\<mu> ?N \<le> \<mu> N"
  7.1028 -        unfolding * using `N \<in> sets M` by (auto intro!: measure_mono)
  7.1029 -      then show "\<mu> ?N = 0" using `\<mu> N = 0` by auto
  7.1030 -    qed
  7.1031 -  next
  7.1032 -    fix x assume "x \<in> space M"
  7.1033 -    show "a x * indicator (space M - N) x \<le> v x"
  7.1034 -    proof (cases "x \<in> N")
  7.1035 -      case True then show ?thesis by simp
  7.1036 -    next
  7.1037 -      case False
  7.1038 -      with N mono have "a x \<le> u x" "u x \<le> v x" using `x \<in> space M` by auto
  7.1039 -      with False `x \<in> space M` show "a x * indicator (space M - N) x \<le> v x" by auto
  7.1040 -    qed
  7.1041 -    assume "a x * indicator (space M - N) x = \<omega>"
  7.1042 -    with mono `x \<in> space M` show False
  7.1043 -      by (simp split: split_if_asm add: indicator_def)
  7.1044 -  qed
  7.1045 -qed
  7.1046 +lemma (in measure_space) positive_integral_mono:
  7.1047 +  "(\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x) \<Longrightarrow> integral\<^isup>P M u \<le> integral\<^isup>P M v"
  7.1048 +  by (auto intro: positive_integral_mono_AE)
  7.1049  
  7.1050  lemma (in measure_space) positive_integral_cong_AE:
  7.1051    "AE x. u x = v x \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
  7.1052    by (auto simp: eq_iff intro!: positive_integral_mono_AE)
  7.1053  
  7.1054 -lemma (in measure_space) positive_integral_mono:
  7.1055 -  "(\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x) \<Longrightarrow> integral\<^isup>P M u \<le> integral\<^isup>P M v"
  7.1056 -  by (auto intro: positive_integral_mono_AE)
  7.1057 +lemma (in measure_space) positive_integral_cong:
  7.1058 +  "(\<And>x. x \<in> space M \<Longrightarrow> u x = v x) \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
  7.1059 +  by (auto intro: positive_integral_cong_AE)
  7.1060  
  7.1061 -lemma image_set_cong:
  7.1062 -  assumes A: "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. f x = g y"
  7.1063 -  assumes B: "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. g y = f x"
  7.1064 -  shows "f ` A = g ` B"
  7.1065 -  using assms by blast
  7.1066 +lemma (in measure_space) positive_integral_eq_simple_integral:
  7.1067 +  assumes f: "simple_function M f" "\<And>x. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
  7.1068 +proof -
  7.1069 +  let "?f x" = "f x * indicator (space M) x"
  7.1070 +  have f': "simple_function M ?f" using f by auto
  7.1071 +  with f(2) have [simp]: "max 0 \<circ> ?f = ?f"
  7.1072 +    by (auto simp: fun_eq_iff max_def split: split_indicator)
  7.1073 +  have "integral\<^isup>P M ?f \<le> integral\<^isup>S M ?f" using f'
  7.1074 +    by (force intro!: SUP_leI simple_integral_mono simp: le_fun_def positive_integral_def)
  7.1075 +  moreover have "integral\<^isup>S M ?f \<le> integral\<^isup>P M ?f"
  7.1076 +    unfolding positive_integral_def
  7.1077 +    using f' by (auto intro!: le_SUPI)
  7.1078 +  ultimately show ?thesis
  7.1079 +    by (simp cong: positive_integral_cong simple_integral_cong)
  7.1080 +qed
  7.1081 +
  7.1082 +lemma (in measure_space) positive_integral_eq_simple_integral_AE:
  7.1083 +  assumes f: "simple_function M f" "AE x. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
  7.1084 +proof -
  7.1085 +  have "AE x. f x = max 0 (f x)" using f by (auto split: split_max)
  7.1086 +  with f have "integral\<^isup>P M f = integral\<^isup>S M (\<lambda>x. max 0 (f x))"
  7.1087 +    by (simp cong: positive_integral_cong_AE simple_integral_cong_AE
  7.1088 +             add: positive_integral_eq_simple_integral)
  7.1089 +  with assms show ?thesis
  7.1090 +    by (auto intro!: simple_integral_cong_AE split: split_max)
  7.1091 +qed
  7.1092  
  7.1093  lemma (in measure_space) positive_integral_SUP_approx:
  7.1094 -  assumes "f \<up> s"
  7.1095 -  and f: "\<And>i. f i \<in> borel_measurable M"
  7.1096 -  and "simple_function M u"
  7.1097 -  and le: "u \<le> s" and real: "\<omega> \<notin> u`space M"
  7.1098 +  assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  7.1099 +  and u: "simple_function M u" "u \<le> (SUP i. f i)" "u`space M \<subseteq> {0..<\<infinity>}"
  7.1100    shows "integral\<^isup>S M u \<le> (SUP i. integral\<^isup>P M (f i))" (is "_ \<le> ?S")
  7.1101 -proof (rule pextreal_le_mult_one_interval)
  7.1102 -  fix a :: pextreal assume "0 < a" "a < 1"
  7.1103 +proof (rule extreal_le_mult_one_interval)
  7.1104 +  have "0 \<le> (SUP i. integral\<^isup>P M (f i))"
  7.1105 +    using f(3) by (auto intro!: le_SUPI2 positive_integral_positive)
  7.1106 +  then show "(SUP i. integral\<^isup>P M (f i)) \<noteq> -\<infinity>" by auto
  7.1107 +  have u_range: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> u x \<and> u x \<noteq> \<infinity>"
  7.1108 +    using u(3) by auto
  7.1109 +  fix a :: extreal assume "0 < a" "a < 1"
  7.1110    hence "a \<noteq> 0" by auto
  7.1111    let "?B i" = "{x \<in> space M. a * u x \<le> f i x}"
  7.1112    have B: "\<And>i. ?B i \<in> sets M"
  7.1113 @@ -1054,203 +1017,269 @@
  7.1114      proof safe
  7.1115        fix i x assume "a * u x \<le> f i x"
  7.1116        also have "\<dots> \<le> f (Suc i) x"
  7.1117 -        using `f \<up> s` unfolding isoton_def le_fun_def by auto
  7.1118 +        using `incseq f`[THEN incseq_SucD] unfolding le_fun_def by auto
  7.1119        finally show "a * u x \<le> f (Suc i) x" .
  7.1120      qed }
  7.1121    note B_mono = this
  7.1122  
  7.1123 -  have u: "\<And>x. x \<in> space M \<Longrightarrow> u -` {u x} \<inter> space M \<in> sets M"
  7.1124 -    using `simple_function M u` by (auto simp add: simple_function_def)
  7.1125 +  note B_u = Int[OF u(1)[THEN simple_functionD(2)] B]
  7.1126  
  7.1127 -  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.1128 -  proof safe
  7.1129 -    fix x i assume "x \<in> space M"
  7.1130 -    show "x \<in> (\<Union>i. (u -` {u x} \<inter> space M) \<inter> ?B i)"
  7.1131 -    proof cases
  7.1132 -      assume "u x = 0" thus ?thesis using `x \<in> space M` by simp
  7.1133 -    next
  7.1134 -      assume "u x \<noteq> 0"
  7.1135 -      with `a < 1` real `x \<in> space M`
  7.1136 -      have "a * u x < 1 * u x" by (rule_tac pextreal_mult_strict_right_mono) (auto simp: image_iff)
  7.1137 -      also have "\<dots> \<le> (SUP i. f i x)" using le `f \<up> s`
  7.1138 -        unfolding isoton_fun_expand by (auto simp: isoton_def le_fun_def)
  7.1139 -      finally obtain i where "a * u x < f i x" unfolding SUPR_def
  7.1140 -        by (auto simp add: less_Sup_iff)
  7.1141 -      hence "a * u x \<le> f i x" by auto
  7.1142 -      thus ?thesis using `x \<in> space M` by auto
  7.1143 +  let "?B' i n" = "(u -` {i} \<inter> space M) \<inter> ?B n"
  7.1144 +  have measure_conv: "\<And>i. \<mu> (u -` {i} \<inter> space M) = (SUP n. \<mu> (?B' i n))"
  7.1145 +  proof -
  7.1146 +    fix i
  7.1147 +    have 1: "range (?B' i) \<subseteq> sets M" using B_u by auto
  7.1148 +    have 2: "incseq (?B' i)" using B_mono by (auto intro!: incseq_SucI)
  7.1149 +    have "(\<Union>n. ?B' i n) = u -` {i} \<inter> space M"
  7.1150 +    proof safe
  7.1151 +      fix x i assume x: "x \<in> space M"
  7.1152 +      show "x \<in> (\<Union>i. ?B' (u x) i)"
  7.1153 +      proof cases
  7.1154 +        assume "u x = 0" thus ?thesis using `x \<in> space M` f(3) by simp
  7.1155 +      next
  7.1156 +        assume "u x \<noteq> 0"
  7.1157 +        with `a < 1` u_range[OF `x \<in> space M`]
  7.1158 +        have "a * u x < 1 * u x"
  7.1159 +          by (intro extreal_mult_strict_right_mono) (auto simp: image_iff)
  7.1160 +        also have "\<dots> \<le> (SUP i. f i x)" using u(2) by (auto simp: le_fun_def SUPR_apply)
  7.1161 +        finally obtain i where "a * u x < f i x" unfolding SUPR_def
  7.1162 +          by (auto simp add: less_Sup_iff)
  7.1163 +        hence "a * u x \<le> f i x" by auto
  7.1164 +        thus ?thesis using `x \<in> space M` by auto
  7.1165 +      qed
  7.1166      qed
  7.1167 -  qed auto
  7.1168 -  note measure_conv = measure_up[OF Int[OF u B] this]
  7.1169 +    then show "?thesis i" using continuity_from_below[OF 1 2] by simp
  7.1170 +  qed
  7.1171  
  7.1172    have "integral\<^isup>S M u = (SUP i. integral\<^isup>S M (?uB i))"
  7.1173      unfolding simple_integral_indicator[OF B `simple_function M u`]
  7.1174 -  proof (subst SUPR_pextreal_setsum, safe)
  7.1175 +  proof (subst SUPR_extreal_setsum, safe)
  7.1176      fix x n assume "x \<in> space M"
  7.1177 -    have "\<mu> (u -` {u x} \<inter> space M \<inter> {x \<in> space M. a * u x \<le> f n x})
  7.1178 -      \<le> \<mu> (u -` {u x} \<inter> space M \<inter> {x \<in> space M. a * u x \<le> f (Suc n) x})"
  7.1179 -      using B_mono Int[OF u[OF `x \<in> space M`] B] by (auto intro!: measure_mono)
  7.1180 -    thus "u x * \<mu> (u -` {u x} \<inter> space M \<inter> ?B n)
  7.1181 -            \<le> u x * \<mu> (u -` {u x} \<inter> space M \<inter> ?B (Suc n))"
  7.1182 -      by (auto intro: mult_left_mono)
  7.1183 +    with u_range show "incseq (\<lambda>i. u x * \<mu> (?B' (u x) i))" "\<And>i. 0 \<le> u x * \<mu> (?B' (u x) i)"
  7.1184 +      using B_mono B_u by (auto intro!: measure_mono extreal_mult_left_mono incseq_SucI simp: extreal_zero_le_0_iff)
  7.1185    next
  7.1186 -    show "integral\<^isup>S M u =
  7.1187 -      (\<Sum>i\<in>u ` space M. SUP n. i * \<mu> (u -` {i} \<inter> space M \<inter> ?B n))"
  7.1188 -      using measure_conv unfolding simple_integral_def isoton_def
  7.1189 -      by (auto intro!: setsum_cong simp: pextreal_SUP_cmult)
  7.1190 +    show "integral\<^isup>S M u = (\<Sum>i\<in>u ` space M. SUP n. i * \<mu> (?B' i n))"
  7.1191 +      using measure_conv u_range B_u unfolding simple_integral_def
  7.1192 +      by (auto intro!: setsum_cong SUPR_extreal_cmult[symmetric])
  7.1193    qed
  7.1194    moreover
  7.1195    have "a * (SUP i. integral\<^isup>S M (?uB i)) \<le> ?S"
  7.1196 -    unfolding pextreal_SUP_cmult[symmetric]
  7.1197 +    apply (subst SUPR_extreal_cmult[symmetric])
  7.1198    proof (safe intro!: SUP_mono bexI)
  7.1199      fix i
  7.1200      have "a * integral\<^isup>S M (?uB i) = (\<integral>\<^isup>Sx. a * ?uB i x \<partial>M)"
  7.1201 -      using B `simple_function M u`
  7.1202 -      by (subst simple_integral_mult[symmetric]) (auto simp: field_simps)
  7.1203 +      using B `simple_function M u` u_range
  7.1204 +      by (subst simple_integral_mult) (auto split: split_indicator)
  7.1205      also have "\<dots> \<le> integral\<^isup>P M (f i)"
  7.1206      proof -
  7.1207 -      have "\<And>x. a * ?uB i x \<le> f i x" unfolding indicator_def by auto
  7.1208 -      hence *: "simple_function M (\<lambda>x. a * ?uB i x)" using B assms(3)
  7.1209 -        by (auto intro!: simple_integral_mono)
  7.1210 -      show ?thesis unfolding positive_integral_eq_simple_integral[OF *, symmetric]
  7.1211 -        by (auto intro!: positive_integral_mono simp: indicator_def)
  7.1212 +      have *: "simple_function M (\<lambda>x. a * ?uB i x)" using B `0 < a` u(1) by auto
  7.1213 +      show ?thesis using f(3) * u_range `0 < a`
  7.1214 +        by (subst positive_integral_eq_simple_integral[symmetric])
  7.1215 +           (auto intro!: positive_integral_mono split: split_indicator)
  7.1216      qed
  7.1217      finally show "a * integral\<^isup>S M (?uB i) \<le> integral\<^isup>P M (f i)"
  7.1218        by auto
  7.1219 -  qed simp
  7.1220 +  next
  7.1221 +    fix i show "0 \<le> \<integral>\<^isup>S x. ?uB i x \<partial>M" using B `0 < a` u(1) u_range
  7.1222 +      by (intro simple_integral_positive) (auto split: split_indicator)
  7.1223 +  qed (insert `0 < a`, auto)
  7.1224    ultimately show "a * integral\<^isup>S M u \<le> ?S" by simp
  7.1225  qed
  7.1226  
  7.1227 +lemma (in measure_space) incseq_positive_integral:
  7.1228 +  assumes "incseq f" shows "incseq (\<lambda>i. integral\<^isup>P M (f i))"
  7.1229 +proof -
  7.1230 +  have "\<And>i x. f i x \<le> f (Suc i) x"
  7.1231 +    using assms by (auto dest!: incseq_SucD simp: le_fun_def)
  7.1232 +  then show ?thesis
  7.1233 +    by (auto intro!: incseq_SucI positive_integral_mono)
  7.1234 +qed
  7.1235 +
  7.1236  text {* Beppo-Levi monotone convergence theorem *}
  7.1237 -lemma (in measure_space) positive_integral_isoton:
  7.1238 -  assumes "f \<up> u" "\<And>i. f i \<in> borel_measurable M"
  7.1239 -  shows "(\<lambda>i. integral\<^isup>P M (f i)) \<up> integral\<^isup>P M u"
  7.1240 -  unfolding isoton_def
  7.1241 -proof safe
  7.1242 -  fix i show "integral\<^isup>P M (f i) \<le> integral\<^isup>P M (f (Suc i))"
  7.1243 -    apply (rule positive_integral_mono)
  7.1244 -    using `f \<up> u` unfolding isoton_def le_fun_def by auto
  7.1245 +lemma (in measure_space) positive_integral_monotone_convergence_SUP:
  7.1246 +  assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  7.1247 +  shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  7.1248 +proof (rule antisym)
  7.1249 +  show "(SUP j. integral\<^isup>P M (f j)) \<le> (\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M)"
  7.1250 +    by (auto intro!: SUP_leI le_SUPI positive_integral_mono)
  7.1251  next
  7.1252 -  have u: "u = (SUP i. f i)" using `f \<up> u` unfolding isoton_def by auto
  7.1253 -  show "(SUP i. integral\<^isup>P M (f i)) = integral\<^isup>P M u"
  7.1254 -  proof (rule antisym)
  7.1255 -    from `f \<up> u`[THEN isoton_Sup, unfolded le_fun_def]
  7.1256 -    show "(SUP j. integral\<^isup>P M (f j)) \<le> integral\<^isup>P M u"
  7.1257 -      by (auto intro!: SUP_leI positive_integral_mono)
  7.1258 -  next
  7.1259 -    show "integral\<^isup>P M u \<le> (SUP i. integral\<^isup>P M (f i))"
  7.1260 -      unfolding positive_integral_alt[of u]
  7.1261 -      by (auto intro!: SUP_leI positive_integral_SUP_approx[OF assms])
  7.1262 +  show "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) \<le> (SUP j. integral\<^isup>P M (f j))"
  7.1263 +    unfolding positive_integral_def_finite[of "\<lambda>x. SUP i. f i x"]
  7.1264 +  proof (safe intro!: SUP_leI)
  7.1265 +    fix g assume g: "simple_function M g"
  7.1266 +      and "g \<le> max 0 \<circ> (\<lambda>x. SUP i. f i x)" "range g \<subseteq> {0..<\<infinity>}"
  7.1267 +    moreover then have "\<And>x. 0 \<le> (SUP i. f i x)" and g': "g`space M \<subseteq> {0..<\<infinity>}"
  7.1268 +      using f by (auto intro!: le_SUPI2)
  7.1269 +    ultimately show "integral\<^isup>S M g \<le> (SUP j. integral\<^isup>P M (f j))"
  7.1270 +      by (intro  positive_integral_SUP_approx[OF f g _ g'])
  7.1271 +         (auto simp: le_fun_def max_def SUPR_apply)
  7.1272    qed
  7.1273  qed
  7.1274  
  7.1275 -lemma (in measure_space) positive_integral_monotone_convergence_SUP:
  7.1276 -  assumes "\<And>i x. x \<in> space M \<Longrightarrow> f i x \<le> f (Suc i) x"
  7.1277 -  assumes "\<And>i. f i \<in> borel_measurable M"
  7.1278 -  shows "(SUP i. integral\<^isup>P M (f i)) = (\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M)"
  7.1279 -    (is "_ = integral\<^isup>P M ?u")
  7.1280 +lemma (in measure_space) positive_integral_monotone_convergence_SUP_AE:
  7.1281 +  assumes f: "\<And>i. AE x. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x" "\<And>i. f i \<in> borel_measurable M"
  7.1282 +  shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  7.1283  proof -
  7.1284 -  show ?thesis
  7.1285 -  proof (rule antisym)
  7.1286 -    show "(SUP j. integral\<^isup>P M (f j)) \<le> integral\<^isup>P M ?u"
  7.1287 -      by (auto intro!: SUP_leI positive_integral_mono le_SUPI)
  7.1288 -  next
  7.1289 -    def rf \<equiv> "\<lambda>i. \<lambda>x\<in>space M. f i x" and ru \<equiv> "\<lambda>x\<in>space M. ?u x"
  7.1290 -    have "\<And>i. rf i \<in> borel_measurable M" unfolding rf_def
  7.1291 -      using assms by (simp cong: measurable_cong)
  7.1292 -    moreover have iso: "rf \<up> ru" using assms unfolding rf_def ru_def
  7.1293 -      unfolding isoton_def le_fun_def fun_eq_iff SUPR_apply
  7.1294 -      using SUP_const[OF UNIV_not_empty]
  7.1295 -      by (auto simp: restrict_def le_fun_def fun_eq_iff)
  7.1296 -    ultimately have "integral\<^isup>P M ru \<le> (SUP i. integral\<^isup>P M (rf i))"
  7.1297 -      unfolding positive_integral_alt[of ru]
  7.1298 -      by (auto simp: le_fun_def intro!: SUP_leI positive_integral_SUP_approx)
  7.1299 -    then show "integral\<^isup>P M ?u \<le> (SUP i. integral\<^isup>P M (f i))"
  7.1300 -      unfolding ru_def rf_def by (simp cong: positive_integral_cong)
  7.1301 +  from f have "AE x. \<forall>i. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x"
  7.1302 +    by (simp add: AE_all_countable)
  7.1303 +  from this[THEN AE_E] guess N . note N = this
  7.1304 +  let "?f i x" = "if x \<in> space M - N then f i x else 0"
  7.1305 +  have f_eq: "AE x. \<forall>i. ?f i x = f i x" using N by (auto intro!: AE_I[of _ N])
  7.1306 +  then have "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (\<integral>\<^isup>+ x. (SUP i. ?f i x) \<partial>M)"
  7.1307 +    by (auto intro!: positive_integral_cong_AE)
  7.1308 +  also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. ?f i x \<partial>M))"
  7.1309 +  proof (rule positive_integral_monotone_convergence_SUP)
  7.1310 +    show "incseq ?f" using N(1) by (force intro!: incseq_SucI le_funI)
  7.1311 +    { fix i show "(\<lambda>x. if x \<in> space M - N then f i x else 0) \<in> borel_measurable M"
  7.1312 +        using f N(3) by (intro measurable_If_set) auto
  7.1313 +      fix x show "0 \<le> ?f i x"
  7.1314 +        using N(1) by auto }
  7.1315    qed
  7.1316 +  also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. f i x \<partial>M))"
  7.1317 +    using f_eq by (force intro!: arg_cong[where f="SUPR UNIV"] positive_integral_cong_AE ext)
  7.1318 +  finally show ?thesis .
  7.1319 +qed
  7.1320 +
  7.1321 +lemma (in measure_space) positive_integral_monotone_convergence_SUP_AE_incseq:
  7.1322 +  assumes f: "incseq f" "\<And>i. AE x. 0 \<le> f i x" and borel: "\<And>i. f i \<in> borel_measurable M"
  7.1323 +  shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
  7.1324 +  using f[unfolded incseq_Suc_iff le_fun_def]
  7.1325 +  by (intro positive_integral_monotone_convergence_SUP_AE[OF _ borel])
  7.1326 +     auto
  7.1327 +
  7.1328 +lemma (in measure_space) positive_integral_monotone_convergence_simple:
  7.1329 +  assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
  7.1330 +  shows "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
  7.1331 +  using assms unfolding positive_integral_monotone_convergence_SUP[OF f(1)
  7.1332 +    f(3)[THEN borel_measurable_simple_function] f(2)]
  7.1333 +  by (auto intro!: positive_integral_eq_simple_integral[symmetric] arg_cong[where f="SUPR UNIV"] ext)
  7.1334 +
  7.1335 +lemma positive_integral_max_0:
  7.1336 +  "(\<integral>\<^isup>+x. max 0 (f x) \<partial>M) = integral\<^isup>P M f"
  7.1337 +  by (simp add: le_fun_def positive_integral_def)
  7.1338 +
  7.1339 +lemma (in measure_space) positive_integral_cong_pos:
  7.1340 +  assumes "\<And>x. x \<in> space M \<Longrightarrow> f x \<le> 0 \<and> g x \<le> 0 \<or> f x = g x"
  7.1341 +  shows "integral\<^isup>P M f = integral\<^isup>P M g"
  7.1342 +proof -
  7.1343 +  have "integral\<^isup>P M (\<lambda>x. max 0 (f x)) = integral\<^isup>P M (\<lambda>x. max 0 (g x))"
  7.1344 +  proof (intro positive_integral_cong)
  7.1345 +    fix x assume "x \<in> space M"
  7.1346 +    from assms[OF this] show "max 0 (f x) = max 0 (g x)"
  7.1347 +      by (auto split: split_max)
  7.1348 +  qed
  7.1349 +  then show ?thesis by (simp add: positive_integral_max_0)
  7.1350  qed
  7.1351  
  7.1352  lemma (in measure_space) SUP_simple_integral_sequences:
  7.1353 -  assumes f: "f \<up> u" "\<And>i. simple_function M (f i)"
  7.1354 -  and g: "g \<up> u" "\<And>i. simple_function M (g i)"
  7.1355 +  assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
  7.1356 +  and g: "incseq g" "\<And>i x. 0 \<le> g i x" "\<And>i. simple_function M (g i)"
  7.1357 +  and eq: "AE x. (SUP i. f i x) = (SUP i. g i x)"
  7.1358    shows "(SUP i. integral\<^isup>S M (f i)) = (SUP i. integral\<^isup>S M (g i))"
  7.1359      (is "SUPR _ ?F = SUPR _ ?G")
  7.1360  proof -
  7.1361 -  have "(SUP i. ?F i) = (SUP i. integral\<^isup>P M (f i))"
  7.1362 -    using assms by (simp add: positive_integral_eq_simple_integral)
  7.1363 -  also have "\<dots> = integral\<^isup>P M u"
  7.1364 -    using positive_integral_isoton[OF f(1) borel_measurable_simple_function[OF f(2)]]
  7.1365 -    unfolding isoton_def by simp
  7.1366 -  also have "\<dots> = (SUP i. integral\<^isup>P M (g i))"
  7.1367 -    using positive_integral_isoton[OF g(1) borel_measurable_simple_function[OF g(2)]]
  7.1368 -    unfolding isoton_def by simp
  7.1369 +  have "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
  7.1370 +    using f by (rule positive_integral_monotone_convergence_simple)
  7.1371 +  also have "\<dots> = (\<integral>\<^isup>+x. (SUP i. g i x) \<partial>M)"
  7.1372 +    unfolding eq[THEN positive_integral_cong_AE] ..
  7.1373    also have "\<dots> = (SUP i. ?G i)"
  7.1374 -    using assms by (simp add: positive_integral_eq_simple_integral)
  7.1375 -  finally show ?thesis .
  7.1376 +    using g by (rule positive_integral_monotone_convergence_simple[symmetric])
  7.1377 +  finally show ?thesis by simp
  7.1378  qed
  7.1379  
  7.1380  lemma (in measure_space) positive_integral_const[simp]:
  7.1381 -  "(\<integral>\<^isup>+ x. c \<partial>M) = c * \<mu> (space M)"
  7.1382 +  "0 \<le> c \<Longrightarrow> (\<integral>\<^isup>+ x. c \<partial>M) = c * \<mu> (space M)"
  7.1383    by (subst positive_integral_eq_simple_integral) auto
  7.1384  
  7.1385 -lemma (in measure_space) positive_integral_isoton_simple:
  7.1386 -  assumes "f \<up> u" and e: "\<And>i. simple_function M (f i)"
  7.1387 -  shows "(\<lambda>i. integral\<^isup>S M (f i)) \<up> integral\<^isup>P M u"
  7.1388 -  using positive_integral_isoton[OF `f \<up> u` e(1)[THEN borel_measurable_simple_function]]
  7.1389 -  unfolding positive_integral_eq_simple_integral[OF e] .
  7.1390 -
  7.1391 -lemma measure_preservingD2:
  7.1392 -  "f \<in> measure_preserving A B \<Longrightarrow> f \<in> measurable A B"
  7.1393 -  unfolding measure_preserving_def by auto
  7.1394 -
  7.1395  lemma (in measure_space) positive_integral_vimage:
  7.1396 -  assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'" and f: "f \<in> borel_measurable M'"
  7.1397 +  assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'"
  7.1398 +  and f: "f \<in> borel_measurable M'"
  7.1399    shows "integral\<^isup>P M' f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
  7.1400  proof -
  7.1401    interpret T: measure_space M' by (rule measure_space_vimage[OF T])
  7.1402 -  obtain f' where f': "f' \<up> f" "\<And>i. simple_function M' (f' i)"
  7.1403 -    using T.borel_measurable_implies_simple_function_sequence[OF f] by blast
  7.1404 -  then have f: "(\<lambda>i x. f' i (T x)) \<up> (\<lambda>x. f (T x))" "\<And>i. simple_function M (\<lambda>x. f' i (T x))"
  7.1405 -    using simple_function_vimage[OF T(1) measure_preservingD2[OF T(2)]] unfolding isoton_fun_expand by auto
  7.1406 +  from T.borel_measurable_implies_simple_function_sequence'[OF f]
  7.1407 +  guess f' . note f' = this
  7.1408 +  let "?f i x" = "f' i (T x)"
  7.1409 +  have inc: "incseq ?f" using f' by (force simp: le_fun_def incseq_def)
  7.1410 +  have sup: "\<And>x. (SUP i. ?f i x) = max 0 (f (T x))"
  7.1411 +    using f'(4) .
  7.1412 +  have sf: "\<And>i. simple_function M (\<lambda>x. f' i (T x))"
  7.1413 +    using simple_function_vimage[OF T(1) measure_preservingD2[OF T(2)] f'(1)] .
  7.1414    show "integral\<^isup>P M' f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
  7.1415 -    using positive_integral_isoton_simple[OF f]
  7.1416 -    using T.positive_integral_isoton_simple[OF f']
  7.1417 -    by (simp add: simple_integral_vimage[OF T f'(2)] isoton_def)
  7.1418 +    using
  7.1419 +      T.positive_integral_monotone_convergence_simple[OF f'(2,5,1)]
  7.1420 +      positive_integral_monotone_convergence_simple[OF inc f'(5) sf]
  7.1421 +    by (simp add: positive_integral_max_0 simple_integral_vimage[OF T f'(1)] f')
  7.1422  qed
  7.1423  
  7.1424  lemma (in measure_space) positive_integral_linear:
  7.1425 -  assumes f: "f \<in> borel_measurable M"
  7.1426 -  and g: "g \<in> borel_measurable M"
  7.1427 +  assumes f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" and "0 \<le> a"
  7.1428 +  and g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
  7.1429    shows "(\<integral>\<^isup>+ x. a * f x + g x \<partial>M) = a * integral\<^isup>P M f + integral\<^isup>P M g"
  7.1430      (is "integral\<^isup>P M ?L = _")
  7.1431  proof -
  7.1432 -  from borel_measurable_implies_simple_function_sequence'[OF f] guess u .
  7.1433 -  note u = this positive_integral_isoton_simple[OF this(1-2)]
  7.1434 -  from borel_measurable_implies_simple_function_sequence'[OF g] guess v .
  7.1435 -  note v = this positive_integral_isoton_simple[OF this(1-2)]
  7.1436 +  from borel_measurable_implies_simple_function_sequence'[OF f(1)] guess u .
  7.1437 +  note u = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
  7.1438 +  from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess v .
  7.1439 +  note v = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
  7.1440    let "?L' i x" = "a * u i x + v i x"
  7.1441  
  7.1442 -  have "?L \<in> borel_measurable M"
  7.1443 -    using assms by simp
  7.1444 +  have "?L \<in> borel_measurable M" using assms by auto
  7.1445    from borel_measurable_implies_simple_function_sequence'[OF this] guess l .
  7.1446 -  note positive_integral_isoton_simple[OF this(1-2)] and l = this
  7.1447 -  moreover have "(SUP i. integral\<^isup>S M (l i)) = (SUP i. integral\<^isup>S M (?L' i))"
  7.1448 -  proof (rule SUP_simple_integral_sequences[OF l(1-2)])
  7.1449 -    show "?L' \<up> ?L" "\<And>i. simple_function M (?L' i)"
  7.1450 -      using u v by (auto simp: isoton_fun_expand isoton_add isoton_cmult_right)
  7.1451 +  note l = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
  7.1452 +
  7.1453 +  have inc: "incseq (\<lambda>i. a * integral\<^isup>S M (u i))" "incseq (\<lambda>i. integral\<^isup>S M (v i))"
  7.1454 +    using u v `0 \<le> a`
  7.1455 +    by (auto simp: incseq_Suc_iff le_fun_def
  7.1456 +             intro!: add_mono extreal_mult_left_mono simple_integral_mono)
  7.1457 +  have pos: "\<And>i. 0 \<le> integral\<^isup>S M (u i)" "\<And>i. 0 \<le> integral\<^isup>S M (v i)" "\<And>i. 0 \<le> a * integral\<^isup>S M (u i)"
  7.1458 +    using u v `0 \<le> a` by (auto simp: simple_integral_positive)
  7.1459 +  { fix i from pos[of i] have "a * integral\<^isup>S M (u i) \<noteq> -\<infinity>" "integral\<^isup>S M (v i) \<noteq> -\<infinity>"
  7.1460 +      by (auto split: split_if_asm) }
  7.1461 +  note not_MInf = this
  7.1462 +
  7.1463 +  have l': "(SUP i. integral\<^isup>S M (l i)) = (SUP i. integral\<^isup>S M (?L' i))"
  7.1464 +  proof (rule SUP_simple_integral_sequences[OF l(3,6,2)])
  7.1465 +    show "incseq ?L'" "\<And>i x. 0 \<le> ?L' i x" "\<And>i. simple_function M (?L' i)"
  7.1466 +      using u v  `0 \<le> a` unfolding incseq_Suc_iff le_fun_def
  7.1467 +      by (auto intro!: add_mono extreal_mult_left_mono extreal_add_nonneg_nonneg)
  7.1468 +    { fix x
  7.1469 +      { fix i have "a * u i x \<noteq> -\<infinity>" "v i x \<noteq> -\<infinity>" "u i x \<noteq> -\<infinity>" using `0 \<le> a` u(6)[of i x] v(6)[of i x]
  7.1470 +          by auto }
  7.1471 +      then have "(SUP i. a * u i x + v i x) = a * (SUP i. u i x) + (SUP i. v i x)"
  7.1472 +        using `0 \<le> a` u(3) v(3) u(6)[of _ x] v(6)[of _ x]
  7.1473 +        by (subst SUPR_extreal_cmult[symmetric, OF u(6) `0 \<le> a`])
  7.1474 +           (auto intro!: SUPR_extreal_add
  7.1475 +                 simp: incseq_Suc_iff le_fun_def add_mono extreal_mult_left_mono extreal_add_nonneg_nonneg) }
  7.1476 +    then show "AE x. (SUP i. l i x) = (SUP i. ?L' i x)"
  7.1477 +      unfolding l(5) using `0 \<le> a` u(5) v(5) l(5) f(2) g(2)
  7.1478 +      by (intro AE_I2) (auto split: split_max simp add: extreal_add_nonneg_nonneg)
  7.1479    qed
  7.1480 -  moreover from u v have L'_isoton:
  7.1481 -      "(\<lambda>i. integral\<^isup>S M (?L' i)) \<up> a * integral\<^isup>P M f + integral\<^isup>P M g"
  7.1482 -    by (simp add: isoton_add isoton_cmult_right)
  7.1483 -  ultimately show ?thesis by (simp add: isoton_def)
  7.1484 +  also have "\<dots> = (SUP i. a * integral\<^isup>S M (u i) + integral\<^isup>S M (v i))"
  7.1485 +    using u(2, 6) v(2, 6) `0 \<le> a` by (auto intro!: arg_cong[where f="SUPR UNIV"] ext)
  7.1486 +  finally have "(\<integral>\<^isup>+ x. max 0 (a * f x + g x) \<partial>M) = a * (\<integral>\<^isup>+x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+x. max 0 (g x) \<partial>M)"
  7.1487 +    unfolding l(5)[symmetric] u(5)[symmetric] v(5)[symmetric]
  7.1488 +    unfolding l(1)[symmetric] u(1)[symmetric] v(1)[symmetric]
  7.1489 +    apply (subst SUPR_extreal_cmult[symmetric, OF pos(1) `0 \<le> a`])
  7.1490 +    apply (subst SUPR_extreal_add[symmetric, OF inc not_MInf]) .
  7.1491 +  then show ?thesis by (simp add: positive_integral_max_0)
  7.1492  qed
  7.1493  
  7.1494  lemma (in measure_space) positive_integral_cmult:
  7.1495 -  assumes "f \<in> borel_measurable M"
  7.1496 +  assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x" "0 \<le> c"
  7.1497    shows "(\<integral>\<^isup>+ x. c * f x \<partial>M) = c * integral\<^isup>P M f"
  7.1498 -  using positive_integral_linear[OF assms, of "\<lambda>x. 0" c] by auto
  7.1499 +proof -
  7.1500 +  have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
  7.1501 +    by (auto split: split_max simp: extreal_zero_le_0_iff)
  7.1502 +  have "(\<integral>\<^isup>+ x. c * f x \<partial>M) = (\<integral>\<^isup>+ x. c * max 0 (f x) \<partial>M)"
  7.1503 +    by (simp add: positive_integral_max_0)
  7.1504 +  then show ?thesis
  7.1505 +    using positive_integral_linear[OF _ _ `0 \<le> c`, of "\<lambda>x. max 0 (f x)" "\<lambda>x. 0"] f
  7.1506 +    by (auto simp: positive_integral_max_0)
  7.1507 +qed
  7.1508  
  7.1509  lemma (in measure_space) positive_integral_multc:
  7.1510 -  assumes "f \<in> borel_measurable M"
  7.1511 +  assumes "f \<in> borel_measurable M" "AE x. 0 \<le> f x" "0 \<le> c"
  7.1512    shows "(\<integral>\<^isup>+ x. f x * c \<partial>M) = integral\<^isup>P M f * c"
  7.1513    unfolding mult_commute[of _ c] positive_integral_cmult[OF assms] by simp
  7.1514  
  7.1515 @@ -1260,143 +1289,172 @@
  7.1516       (auto simp: simple_function_indicator simple_integral_indicator)
  7.1517  
  7.1518  lemma (in measure_space) positive_integral_cmult_indicator:
  7.1519 -  "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x \<partial>M) = c * \<mu> A"
  7.1520 +  "0 \<le> c \<Longrightarrow> A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x \<partial>M) = c * \<mu> A"
  7.1521    by (subst positive_integral_eq_simple_integral)
  7.1522       (auto simp: simple_function_indicator simple_integral_indicator)
  7.1523  
  7.1524  lemma (in measure_space) positive_integral_add:
  7.1525 -  assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  7.1526 +  assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
  7.1527 +  and g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  7.1528    shows "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = integral\<^isup>P M f + integral\<^isup>P M g"
  7.1529 -  using positive_integral_linear[OF assms, of 1] by simp
  7.1530 +proof -
  7.1531 +  have ae: "AE x. max 0 (f x) + max 0 (g x) = max 0 (f x + g x)"
  7.1532 +    using assms by (auto split: split_max simp: extreal_add_nonneg_nonneg)
  7.1533 +  have "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = (\<integral>\<^isup>+ x. max 0 (f x + g x) \<partial>M)"
  7.1534 +    by (simp add: positive_integral_max_0)
  7.1535 +  also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) + max 0 (g x) \<partial>M)"
  7.1536 +    unfolding ae[THEN positive_integral_cong_AE] ..
  7.1537 +  also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+ x. max 0 (g x) \<partial>M)"
  7.1538 +    using positive_integral_linear[of "\<lambda>x. max 0 (f x)" 1 "\<lambda>x. max 0 (g x)"] f g
  7.1539 +    by auto
  7.1540 +  finally show ?thesis
  7.1541 +    by (simp add: positive_integral_max_0)
  7.1542 +qed
  7.1543  
  7.1544  lemma (in measure_space) positive_integral_setsum:
  7.1545 -  assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M"
  7.1546 +  assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M" "\<And>i. i\<in>P \<Longrightarrow> AE x. 0 \<le> f i x"
  7.1547    shows "(\<integral>\<^isup>+ x. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>P M (f i))"
  7.1548  proof cases
  7.1549 -  assume "finite P"
  7.1550 -  from this assms show ?thesis
  7.1551 +  assume f: "finite P"
  7.1552 +  from assms have "AE x. \<forall>i\<in>P. 0 \<le> f i x" unfolding AE_finite_all[OF f] by auto
  7.1553 +  from f this assms(1) show ?thesis
  7.1554    proof induct
  7.1555      case (insert i P)
  7.1556 -    have "f i \<in> borel_measurable M"
  7.1557 -      "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M"
  7.1558 -      using insert by (auto intro!: borel_measurable_pextreal_setsum)
  7.1559 +    then have "f i \<in> borel_measurable M" "AE x. 0 \<le> f i x"
  7.1560 +      "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M" "AE x. 0 \<le> (\<Sum>i\<in>P. f i x)"
  7.1561 +      by (auto intro!: borel_measurable_extreal_setsum setsum_nonneg)
  7.1562      from positive_integral_add[OF this]
  7.1563      show ?case using insert by auto
  7.1564    qed simp
  7.1565  qed simp
  7.1566  
  7.1567 +lemma (in measure_space) positive_integral_Markov_inequality:
  7.1568 +  assumes u: "u \<in> borel_measurable M" "AE x. 0 \<le> u x" and "A \<in> sets M" and c: "0 \<le> c" "c \<noteq> \<infinity>"
  7.1569 +  shows "\<mu> ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  7.1570 +    (is "\<mu> ?A \<le> _ * ?PI")
  7.1571 +proof -
  7.1572 +  have "?A \<in> sets M"
  7.1573 +    using `A \<in> sets M` u by auto
  7.1574 +  hence "\<mu> ?A = (\<integral>\<^isup>+ x. indicator ?A x \<partial>M)"
  7.1575 +    using positive_integral_indicator by simp
  7.1576 +  also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x) \<partial>M)" using u c
  7.1577 +    by (auto intro!: positive_integral_mono_AE
  7.1578 +      simp: indicator_def extreal_zero_le_0_iff)
  7.1579 +  also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  7.1580 +    using assms
  7.1581 +    by (auto intro!: positive_integral_cmult borel_measurable_indicator simp: extreal_zero_le_0_iff)
  7.1582 +  finally show ?thesis .
  7.1583 +qed
  7.1584 +
  7.1585 +lemma (in measure_space) positive_integral_noteq_infinite:
  7.1586 +  assumes g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  7.1587 +  and "integral\<^isup>P M g \<noteq> \<infinity>"
  7.1588 +  shows "AE x. g x \<noteq> \<infinity>"
  7.1589 +proof (rule ccontr)
  7.1590 +  assume c: "\<not> (AE x. g x \<noteq> \<infinity>)"
  7.1591 +  have "\<mu> {x\<in>space M. g x = \<infinity>} \<noteq> 0"
  7.1592 +    using c g by (simp add: AE_iff_null_set)
  7.1593 +  moreover have "0 \<le> \<mu> {x\<in>space M. g x = \<infinity>}" using g by (auto intro: measurable_sets)
  7.1594 +  ultimately have "0 < \<mu> {x\<in>space M. g x = \<infinity>}" by auto
  7.1595 +  then have "\<infinity> = \<infinity> * \<mu> {x\<in>space M. g x = \<infinity>}" by auto
  7.1596 +  also have "\<dots> \<le> (\<integral>\<^isup>+x. \<infinity> * indicator {x\<in>space M. g x = \<infinity>} x \<partial>M)"
  7.1597 +    using g by (subst positive_integral_cmult_indicator) auto
  7.1598 +  also have "\<dots> \<le> integral\<^isup>P M g"
  7.1599 +    using assms by (auto intro!: positive_integral_mono_AE simp: indicator_def)
  7.1600 +  finally show False using `integral\<^isup>P M g \<noteq> \<infinity>` by auto
  7.1601 +qed
  7.1602 +
  7.1603  lemma (in measure_space) positive_integral_diff:
  7.1604 -  assumes f: "f \<in> borel_measurable M" and g: "g \<in> borel_measurable M"
  7.1605 -  and fin: "integral\<^isup>P M g \<noteq> \<omega>"
  7.1606 -  and mono: "\<And>x. x \<in> space M \<Longrightarrow> g x \<le> f x"
  7.1607 +  assumes f: "f \<in> borel_measurable M"
  7.1608 +  and g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  7.1609 +  and fin: "integral\<^isup>P M g \<noteq> \<infinity>"
  7.1610 +  and mono: "AE x. g x \<le> f x"
  7.1611    shows "(\<integral>\<^isup>+ x. f x - g x \<partial>M) = integral\<^isup>P M f - integral\<^isup>P M g"
  7.1612  proof -
  7.1613 -  have borel: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  7.1614 -    using f g by (rule borel_measurable_pextreal_diff)
  7.1615 -  have "(\<integral>\<^isup>+x. f x - g x \<partial>M) + integral\<^isup>P M g = integral\<^isup>P M f"
  7.1616 -    unfolding positive_integral_add[OF borel g, symmetric]
  7.1617 -  proof (rule positive_integral_cong)
  7.1618 -    fix x assume "x \<in> space M"
  7.1619 -    from mono[OF this] show "f x - g x + g x = f x"
  7.1620 -      by (cases "f x", cases "g x", simp, simp, cases "g x", auto)
  7.1621 -  qed
  7.1622 -  with mono show ?thesis
  7.1623 -    by (subst minus_pextreal_eq2[OF _ fin]) (auto intro!: positive_integral_mono)
  7.1624 +  have diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M" "AE x. 0 \<le> f x - g x"
  7.1625 +    using assms by (auto intro: extreal_diff_positive)
  7.1626 +  have pos_f: "AE x. 0 \<le> f x" using mono g by auto
  7.1627 +  { fix a b :: extreal assume "0 \<le> a" "a \<noteq> \<infinity>" "0 \<le> b" "a \<le> b" then have "b - a + a = b"
  7.1628 +      by (cases rule: extreal2_cases[of a b]) auto }
  7.1629 +  note * = this
  7.1630 +  then have "AE x. f x = f x - g x + g x"
  7.1631 +    using mono positive_integral_noteq_infinite[OF g fin] assms by auto
  7.1632 +  then have **: "integral\<^isup>P M f = (\<integral>\<^isup>+x. f x - g x \<partial>M) + integral\<^isup>P M g"
  7.1633 +    unfolding positive_integral_add[OF diff g, symmetric]
  7.1634 +    by (rule positive_integral_cong_AE)
  7.1635 +  show ?thesis unfolding **
  7.1636 +    using fin positive_integral_positive[of g]
  7.1637 +    by (cases rule: extreal2_cases[of "\<integral>\<^isup>+ x. f x - g x \<partial>M" "integral\<^isup>P M g"]) auto
  7.1638  qed
  7.1639  
  7.1640 -lemma (in measure_space) positive_integral_psuminf:
  7.1641 -  assumes "\<And>i. f i \<in> borel_measurable M"
  7.1642 -  shows "(\<integral>\<^isup>+ x. (\<Sum>\<^isub>\<infinity> i. f i x) \<partial>M) = (\<Sum>\<^isub>\<infinity> i. integral\<^isup>P M (f i))"
  7.1643 +lemma (in measure_space) positive_integral_suminf:
  7.1644 +  assumes f: "\<And>i. f i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> f i x"
  7.1645 +  shows "(\<integral>\<^isup>+ x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^isup>P M (f i))"
  7.1646  proof -
  7.1647 -  have "(\<lambda>i. (\<integral>\<^isup>+x. (\<Sum>i<i. f i x) \<partial>M)) \<up> (\<integral>\<^isup>+x. (\<Sum>\<^isub>\<infinity>i. f i x) \<partial>M)"
  7.1648 -    by (rule positive_integral_isoton)
  7.1649 -       (auto intro!: borel_measurable_pextreal_setsum assms positive_integral_mono
  7.1650 -                     arg_cong[where f=Sup]
  7.1651 -             simp: isoton_def le_fun_def psuminf_def fun_eq_iff SUPR_def Sup_fun_def)
  7.1652 -  thus ?thesis
  7.1653 -    by (auto simp: isoton_def psuminf_def positive_integral_setsum[OF assms])
  7.1654 +  have all_pos: "AE x. \<forall>i. 0 \<le> f i x"
  7.1655 +    using assms by (auto simp: AE_all_countable)
  7.1656 +  have "(\<Sum>i. integral\<^isup>P M (f i)) = (SUP n. \<Sum>i<n. integral\<^isup>P M (f i))"
  7.1657 +    using positive_integral_positive by (rule suminf_extreal_eq_SUPR)
  7.1658 +  also have "\<dots> = (SUP n. \<integral>\<^isup>+x. (\<Sum>i<n. f i x) \<partial>M)"
  7.1659 +    unfolding positive_integral_setsum[OF f] ..
  7.1660 +  also have "\<dots> = \<integral>\<^isup>+x. (SUP n. \<Sum>i<n. f i x) \<partial>M" using f all_pos
  7.1661 +    by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
  7.1662 +       (elim AE_mp, auto simp: setsum_nonneg simp del: setsum_lessThan_Suc intro!: AE_I2 setsum_mono3)
  7.1663 +  also have "\<dots> = \<integral>\<^isup>+x. (\<Sum>i. f i x) \<partial>M" using all_pos
  7.1664 +    by (intro positive_integral_cong_AE) (auto simp: suminf_extreal_eq_SUPR)
  7.1665 +  finally show ?thesis by simp
  7.1666  qed
  7.1667  
  7.1668  text {* Fatou's lemma: convergence theorem on limes inferior *}
  7.1669  lemma (in measure_space) positive_integral_lim_INF:
  7.1670 -  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> pextreal"
  7.1671 -  assumes "\<And>i. u i \<in> borel_measurable M"
  7.1672 -  shows "(\<integral>\<^isup>+ x. (SUP n. INF m. u (m + n) x) \<partial>M) \<le>
  7.1673 -    (SUP n. INF m. integral\<^isup>P M (u (m + n)))"
  7.1674 +  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
  7.1675 +  assumes u: "\<And>i. u i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> u i x"
  7.1676 +  shows "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
  7.1677  proof -
  7.1678 -  have "(\<integral>\<^isup>+x. (SUP n. INF m. u (m + n) x) \<partial>M)
  7.1679 -      = (SUP n. (\<integral>\<^isup>+x. (INF m. u (m + n) x) \<partial>M))"
  7.1680 -    using assms
  7.1681 -    by (intro positive_integral_monotone_convergence_SUP[symmetric] INF_mono)
  7.1682 -       (auto simp del: add_Suc simp add: add_Suc[symmetric])
  7.1683 -  also have "\<dots> \<le> (SUP n. INF m. integral\<^isup>P M (u (m + n)))"
  7.1684 -    by (auto intro!: SUP_mono bexI le_INFI positive_integral_mono INF_leI)
  7.1685 +  have pos: "AE x. \<forall>i. 0 \<le> u i x" using u by (auto simp: AE_all_countable)
  7.1686 +  have "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) =
  7.1687 +    (SUP n. \<integral>\<^isup>+ x. (INF i:{n..}. u i x) \<partial>M)"
  7.1688 +    unfolding liminf_SUPR_INFI using pos u
  7.1689 +    by (intro positive_integral_monotone_convergence_SUP_AE)
  7.1690 +       (elim AE_mp, auto intro!: AE_I2 intro: le_INFI INF_subset)
  7.1691 +  also have "\<dots> \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
  7.1692 +    unfolding liminf_SUPR_INFI
  7.1693 +    by (auto intro!: SUP_mono exI le_INFI positive_integral_mono INF_leI)
  7.1694    finally show ?thesis .
  7.1695  qed
  7.1696  
  7.1697  lemma (in measure_space) measure_space_density:
  7.1698 -  assumes borel: "u \<in> borel_measurable M"
  7.1699 +  assumes u: "u \<in> borel_measurable M" "AE x. 0 \<le> u x"
  7.1700      and M'[simp]: "M' = (M\<lparr>measure := \<lambda>A. (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)\<rparr>)"
  7.1701    shows "measure_space M'"
  7.1702  proof -
  7.1703    interpret M': sigma_algebra M' by (intro sigma_algebra_cong) auto
  7.1704    show ?thesis
  7.1705    proof
  7.1706 -    show "measure M' {} = 0" unfolding M' by simp
  7.1707 +    have pos: "\<And>A. AE x. 0 \<le> u x * indicator A x"
  7.1708 +      using u by (auto simp: extreal_zero_le_0_iff)
  7.1709 +    then show "positive M' (measure M')" unfolding M'
  7.1710 +      using u(1) by (auto simp: positive_def intro!: positive_integral_positive)
  7.1711      show "countably_additive M' (measure M')"
  7.1712      proof (intro countably_additiveI)
  7.1713        fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets M'"
  7.1714 -      then have "\<And>i. (\<lambda>x. u x * indicator (A i) x) \<in> borel_measurable M"
  7.1715 -        using borel by (auto intro: borel_measurable_indicator)
  7.1716 -      moreover assume "disjoint_family A"
  7.1717 -      note psuminf_indicator[OF this]
  7.1718 -      ultimately show "(\<Sum>\<^isub>\<infinity>n. measure M' (A n)) = measure M' (\<Union>x. A x)"
  7.1719 -        by (simp add: positive_integral_psuminf[symmetric])
  7.1720 +      then have *: "\<And>i. (\<lambda>x. u x * indicator (A i) x) \<in> borel_measurable M"
  7.1721 +        using u by (auto intro: borel_measurable_indicator)
  7.1722 +      assume disj: "disjoint_family A"
  7.1723 +      have "(\<Sum>n. measure M' (A n)) = (\<integral>\<^isup>+ x. (\<Sum>n. u x * indicator (A n) x) \<partial>M)"
  7.1724 +        unfolding M' using u(1) *
  7.1725 +        by (simp add: positive_integral_suminf[OF _ pos, symmetric])
  7.1726 +      also have "\<dots> = (\<integral>\<^isup>+ x. u x * (\<Sum>n. indicator (A n) x) \<partial>M)" using u
  7.1727 +        by (intro positive_integral_cong_AE)
  7.1728 +           (elim AE_mp, auto intro!: AE_I2 suminf_cmult_extreal)
  7.1729 +      also have "\<dots> = (\<integral>\<^isup>+ x. u x * indicator (\<Union>n. A n) x \<partial>M)"
  7.1730 +        unfolding suminf_indicator[OF disj] ..
  7.1731 +      finally show "(\<Sum>n. measure M' (A n)) = measure M' (\<Union>x. A x)"
  7.1732 +        unfolding M' by simp
  7.1733      qed
  7.1734    qed
  7.1735  qed
  7.1736  
  7.1737 -lemma (in measure_space) positive_integral_translated_density:
  7.1738 -  assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  7.1739 -    and M': "M' = (M\<lparr> measure := \<lambda>A. (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)\<rparr>)"
  7.1740 -  shows "integral\<^isup>P M' g = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
  7.1741 -proof -
  7.1742 -  from measure_space_density[OF assms(1) M']
  7.1743 -  interpret T: measure_space M' .
  7.1744 -  have borel[simp]:
  7.1745 -    "borel_measurable M' = borel_measurable M"
  7.1746 -    "simple_function M' = simple_function M"
  7.1747 -    unfolding measurable_def simple_function_def_raw by (auto simp: M')
  7.1748 -  from borel_measurable_implies_simple_function_sequence[OF assms(2)]
  7.1749 -  obtain G where G: "\<And>i. simple_function M (G i)" "G \<up> g" by blast
  7.1750 -  note G_borel = borel_measurable_simple_function[OF this(1)]
  7.1751 -  from T.positive_integral_isoton[unfolded borel, OF `G \<up> g` G_borel]
  7.1752 -  have *: "(\<lambda>i. integral\<^isup>P M' (G i)) \<up> integral\<^isup>P M' g" .
  7.1753 -  { fix i
  7.1754 -    have [simp]: "finite (G i ` space M)"
  7.1755 -      using G(1) unfolding simple_function_def by auto
  7.1756 -    have "integral\<^isup>P M' (G i) = integral\<^isup>S M' (G i)"
  7.1757 -      using G T.positive_integral_eq_simple_integral by simp
  7.1758 -    also have "\<dots> = (\<integral>\<^isup>+x. f x * (\<Sum>y\<in>G i`space M. y * indicator (G i -` {y} \<inter> space M) x) \<partial>M)"
  7.1759 -      apply (simp add: simple_integral_def M')
  7.1760 -      apply (subst positive_integral_cmult[symmetric])
  7.1761 -      using G_borel assms(1) apply (fastsimp intro: borel_measurable_vimage)
  7.1762 -      apply (subst positive_integral_setsum[symmetric])
  7.1763 -      using G_borel assms(1) apply (fastsimp intro: borel_measurable_vimage)
  7.1764 -      by (simp add: setsum_right_distrib field_simps)
  7.1765 -    also have "\<dots> = (\<integral>\<^isup>+x. f x * G i x \<partial>M)"
  7.1766 -      by (auto intro!: positive_integral_cong
  7.1767 -               simp: indicator_def if_distrib setsum_cases)
  7.1768 -    finally have "integral\<^isup>P M' (G i) = (\<integral>\<^isup>+x. f x * G i x \<partial>M)" . }
  7.1769 -  with * have eq_Tg: "(\<lambda>i. (\<integral>\<^isup>+x. f x * G i x \<partial>M)) \<up> integral\<^isup>P M' g" by simp
  7.1770 -  from G(2) have "(\<lambda>i x. f x * G i x) \<up> (\<lambda>x. f x * g x)"
  7.1771 -    unfolding isoton_fun_expand by (auto intro!: isoton_cmult_right)
  7.1772 -  then have "(\<lambda>i. (\<integral>\<^isup>+x. f x * G i x \<partial>M)) \<up> (\<integral>\<^isup>+x. f x * g x \<partial>M)"
  7.1773 -    using assms(1) G_borel by (auto intro!: positive_integral_isoton borel_measurable_pextreal_times)
  7.1774 -  with eq_Tg show "integral\<^isup>P M' g = (\<integral>\<^isup>+x. f x * g x \<partial>M)"
  7.1775 -    unfolding isoton_def by simp
  7.1776 -qed
  7.1777 -
  7.1778  lemma (in measure_space) positive_integral_null_set:
  7.1779    assumes "N \<in> null_sets" shows "(\<integral>\<^isup>+ x. u x * indicator N x \<partial>M) = 0"
  7.1780  proof -
  7.1781 @@ -1410,144 +1468,199 @@
  7.1782    then show ?thesis by simp
  7.1783  qed
  7.1784  
  7.1785 -lemma (in measure_space) positive_integral_Markov_inequality:
  7.1786 -  assumes borel: "u \<in> borel_measurable M" and "A \<in> sets M" and c: "c \<noteq> \<omega>"
  7.1787 -  shows "\<mu> ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  7.1788 -    (is "\<mu> ?A \<le> _ * ?PI")
  7.1789 +lemma (in measure_space) positive_integral_translated_density:
  7.1790 +  assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
  7.1791 +  assumes g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
  7.1792 +    and M': "M' = (M\<lparr> measure := \<lambda>A. (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)\<rparr>)"
  7.1793 +  shows "integral\<^isup>P M' g = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
  7.1794  proof -
  7.1795 -  have "?A \<in> sets M"
  7.1796 -    using `A \<in> sets M` borel by auto
  7.1797 -  hence "\<mu> ?A = (\<integral>\<^isup>+ x. indicator ?A x \<partial>M)"
  7.1798 -    using positive_integral_indicator by simp
  7.1799 -  also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x) \<partial>M)"
  7.1800 -  proof (rule positive_integral_mono)
  7.1801 -    fix x assume "x \<in> space M"
  7.1802 -    show "indicator ?A x \<le> c * (u x * indicator A x)"
  7.1803 -      by (cases "x \<in> ?A") auto
  7.1804 -  qed
  7.1805 -  also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
  7.1806 -    using assms
  7.1807 -    by (auto intro!: positive_integral_cmult borel_measurable_indicator)
  7.1808 -  finally show ?thesis .
  7.1809 +  from measure_space_density[OF f M']
  7.1810 +  interpret T: measure_space M' .
  7.1811 +  have borel[simp]:
  7.1812 +    "borel_measurable M' = borel_measurable M"
  7.1813 +    "simple_function M' = simple_function M"
  7.1814 +    unfolding measurable_def simple_function_def_raw by (auto simp: M')
  7.1815 +  from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess G . note G = this
  7.1816 +  note G' = borel_measurable_simple_function[OF this(1)] simple_functionD[OF G(1)]
  7.1817 +  note G'(2)[simp]
  7.1818 +  { fix P have "AE x. P x \<Longrightarrow> AE x in M'. P x"
  7.1819 +      using positive_integral_null_set[of _ f]
  7.1820 +      unfolding T.almost_everywhere_def almost_everywhere_def
  7.1821 +      by (auto simp: M') }
  7.1822 +  note ac = this
  7.1823 +  from G(4) g(2) have G_M': "AE x in M'. (SUP i. G i x) = g x"
  7.1824 +    by (auto intro!: ac split: split_max)
  7.1825 +  { fix i
  7.1826 +    let "?I y x" = "indicator (G i -` {y} \<inter> space M) x"
  7.1827 +    { fix x assume *: "x \<in> space M" "0 \<le> f x" "0 \<le> g x"
  7.1828 +      then have [simp]: "G i ` space M \<inter> {y. G i x = y \<and> x \<in> space M} = {G i x}" by auto
  7.1829 +      from * G' G have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * (\<Sum>y\<in>G i`space M. (y * ?I y x))"
  7.1830 +        by (subst setsum_extreal_right_distrib) (auto simp: ac_simps)
  7.1831 +      also have "\<dots> = f x * G i x"
  7.1832 +        by (simp add: indicator_def if_distrib setsum_cases)
  7.1833 +      finally have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * G i x" . }
  7.1834 +    note to_singleton = this
  7.1835 +    have "integral\<^isup>P M' (G i) = integral\<^isup>S M' (G i)"
  7.1836 +      using G T.positive_integral_eq_simple_integral by simp
  7.1837 +    also have "\<dots> = (\<Sum>y\<in>G i`space M. y * (\<integral>\<^isup>+x. f x * ?I y x \<partial>M))"
  7.1838 +      unfolding simple_integral_def M' by simp
  7.1839 +    also have "\<dots> = (\<Sum>y\<in>G i`space M. (\<integral>\<^isup>+x. y * (f x * ?I y x) \<partial>M))"
  7.1840 +      using f G' G by (auto intro!: setsum_cong positive_integral_cmult[symmetric])
  7.1841 +    also have "\<dots> = (\<integral>\<^isup>+x. (\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) \<partial>M)"
  7.1842 +      using f G' G by (auto intro!: positive_integral_setsum[symmetric])
  7.1843 +    finally have "integral\<^isup>P M' (G i) = (\<integral>\<^isup>+x. f x * G i x \<partial>M)"
  7.1844 +      using f g G' to_singleton by (auto intro!: positive_integral_cong_AE) }
  7.1845 +  note [simp] = this
  7.1846 +  have "integral\<^isup>P M' g = (SUP i. integral\<^isup>P M' (G i))" using G'(1) G_M'(1) G
  7.1847 +    using T.positive_integral_monotone_convergence_SUP[symmetric, OF `incseq G`]
  7.1848 +    by (simp cong: T.positive_integral_cong_AE)
  7.1849 +  also have "\<dots> = (SUP i. (\<integral>\<^isup>+x. f x * G i x \<partial>M))" by simp
  7.1850 +  also have "\<dots> = (\<integral>\<^isup>+x. (SUP i. f x * G i x) \<partial>M)"
  7.1851 +    using f G' G(2)[THEN incseq_SucD] G
  7.1852 +    by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
  7.1853 +       (auto simp: extreal_mult_left_mono le_fun_def extreal_zero_le_0_iff)
  7.1854 +  also have "\<dots> = (\<integral>\<^isup>+x. f x * g x \<partial>M)" using f G' G g
  7.1855 +    by (intro positive_integral_cong_AE)
  7.1856 +       (auto simp add: SUPR_extreal_cmult split: split_max)
  7.1857 +  finally show "integral\<^isup>P M' g = (\<integral>\<^isup>+x. f x * g x \<partial>M)" .
  7.1858  qed
  7.1859  
  7.1860  lemma (in measure_space) positive_integral_0_iff:
  7.1861 -  assumes borel: "u \<in> borel_measurable M"
  7.1862 +  assumes u: "u \<in> borel_measurable M" and pos: "AE x. 0 \<le> u x"
  7.1863    shows "integral\<^isup>P M u = 0 \<longleftrightarrow> \<mu> {x\<in>space M. u x \<noteq> 0} = 0"
  7.1864      (is "_ \<longleftrightarrow> \<mu> ?A = 0")
  7.1865  proof -
  7.1866 -  have A: "?A \<in> sets M" using borel by auto
  7.1867 -  have u: "(\<integral>\<^isup>+ x. u x * indicator ?A x \<partial>M) = integral\<^isup>P M u"
  7.1868 +  have u_eq: "(\<integral>\<^isup>+ x. u x * indicator ?A x \<partial>M) = integral\<^isup>P M u"
  7.1869      by (auto intro!: positive_integral_cong simp: indicator_def)
  7.1870 -
  7.1871    show ?thesis
  7.1872    proof
  7.1873      assume "\<mu> ?A = 0"
  7.1874 -    hence "?A \<in> null_sets" using `?A \<in> sets M` by auto
  7.1875 -    from positive_integral_null_set[OF this]
  7.1876 -    have "0 = (\<integral>\<^isup>+ x. u x * indicator ?A x \<partial>M)" by simp
  7.1877 -    thus "integral\<^isup>P M u = 0" unfolding u by simp
  7.1878 +    with positive_integral_null_set[of ?A u] u
  7.1879 +    show "integral\<^isup>P M u = 0" by (simp add: u_eq)
  7.1880    next
  7.1881 +    { fix r :: extreal and n :: nat assume gt_1: "1 \<le> real n * r"
  7.1882 +      then have "0 < real n * r" by (cases r) (auto split: split_if_asm simp: one_extreal_def)
  7.1883 +      then have "0 \<le> r" by (auto simp add: extreal_zero_less_0_iff) }
  7.1884 +    note gt_1 = this
  7.1885      assume *: "integral\<^isup>P M u = 0"
  7.1886 -    let "?M n" = "{x \<in> space M. 1 \<le> of_nat n * u x}"
  7.1887 +    let "?M n" = "{x \<in> space M. 1 \<le> real (n::nat) * u x}"
  7.1888      have "0 = (SUP n. \<mu> (?M n \<inter> ?A))"
  7.1889      proof -
  7.1890 -      { fix n
  7.1891 -        from positive_integral_Markov_inequality[OF borel `?A \<in> sets M`, of "of_nat n"]
  7.1892 -        have "\<mu> (?M n \<inter> ?A) = 0" unfolding * u by simp }
  7.1893 +      { fix n :: nat
  7.1894 +        from positive_integral_Markov_inequality[OF u pos, of ?A "extreal (real n)"]
  7.1895 +        have "\<mu> (?M n \<inter> ?A) \<le> 0" unfolding u_eq * using u by simp
  7.1896 +        moreover have "0 \<le> \<mu> (?M n \<inter> ?A)" using u by auto
  7.1897 +        ultimately have "\<mu> (?M n \<inter> ?A) = 0" by auto }
  7.1898        thus ?thesis by simp
  7.1899      qed
  7.1900      also have "\<dots> = \<mu> (\<Union>n. ?M n \<inter> ?A)"
  7.1901      proof (safe intro!: continuity_from_below)
  7.1902        fix n show "?M n \<inter> ?A \<in> sets M"
  7.1903 -        using borel by (auto intro!: Int)
  7.1904 +        using u by (auto intro!: Int)
  7.1905      next
  7.1906 -      fix n x assume "1 \<le> of_nat n * u x"
  7.1907 -      also have "\<dots> \<le> of_nat (Suc n) * u x"
  7.1908 -        by (subst (1 2) mult_commute) (auto intro!: pextreal_mult_cancel)
  7.1909 -      finally show "1 \<le> of_nat (Suc n) * u x" .
  7.1910 +      show "incseq (\<lambda>n. {x \<in> space M. 1 \<le> real n * u x} \<inter> {x \<in> space M. u x \<noteq> 0})"
  7.1911 +      proof (safe intro!: incseq_SucI)
  7.1912 +        fix n :: nat and x
  7.1913 +        assume *: "1 \<le> real n * u x"
  7.1914 +        also from gt_1[OF this] have "real n * u x \<le> real (Suc n) * u x"
  7.1915 +          using `0 \<le> u x` by (auto intro!: extreal_mult_right_mono)
  7.1916 +        finally show "1 \<le> real (Suc n) * u x" by auto
  7.1917 +      qed
  7.1918      qed
  7.1919 -    also have "\<dots> = \<mu> ?A"
  7.1920 -    proof (safe intro!: arg_cong[where f="\<mu>"])
  7.1921 -      fix x assume "u x \<noteq> 0" and [simp, intro]: "x \<in> space M"
  7.1922 +    also have "\<dots> = \<mu> {x\<in>space M. 0 < u x}"
  7.1923 +    proof (safe intro!: arg_cong[where f="\<mu>"] dest!: gt_1)
  7.1924 +      fix x assume "0 < u x" and [simp, intro]: "x \<in> space M"
  7.1925        show "x \<in> (\<Union>n. ?M n \<inter> ?A)"
  7.1926        proof (cases "u x")
  7.1927 -        case (preal r)
  7.1928 -        obtain j where "1 / r \<le> of_nat j" using ex_le_of_nat ..
  7.1929 -        hence "1 / r * r \<le> of_nat j * r" using preal unfolding mult_le_cancel_right by auto
  7.1930 -        hence "1 \<le> of_nat j * r" using preal `u x \<noteq> 0` by auto
  7.1931 -        thus ?thesis using `u x \<noteq> 0` preal by (auto simp: real_of_nat_def[symmetric])
  7.1932 -      qed auto
  7.1933 -    qed
  7.1934 -    finally show "\<mu> ?A = 0" by simp
  7.1935 +        case (real r) with `0 < u x` have "0 < r" by auto
  7.1936 +        obtain j :: nat where "1 / r \<le> real j" using real_arch_simple ..
  7.1937 +        hence "1 / r * r \<le> real j * r" unfolding mult_le_cancel_right using `0 < r` by auto
  7.1938 +        hence "1 \<le> real j * r" using real `0 < r` by auto
  7.1939 +        thus ?thesis using `0 < r` real by (auto simp: one_extreal_def)
  7.1940 +      qed (insert `0 < u x`, auto)
  7.1941 +    qed auto
  7.1942 +    finally have "\<mu> {x\<in>space M. 0 < u x} = 0" by simp
  7.1943 +    moreover
  7.1944 +    from pos have "AE x. \<not> (u x < 0)" by auto
  7.1945 +    then have "\<mu> {x\<in>space M. u x < 0} = 0"
  7.1946 +      using AE_iff_null_set u by auto
  7.1947 +    moreover have "\<mu> {x\<in>space M. u x \<noteq> 0} = \<mu> {x\<in>space M. u x < 0} + \<mu> {x\<in>space M. 0 < u x}"
  7.1948 +      using u by (subst measure_additive) (auto intro!: arg_cong[where f=\<mu>])
  7.1949 +    ultimately show "\<mu> ?A = 0" by simp
  7.1950    qed
  7.1951  qed
  7.1952  
  7.1953  lemma (in measure_space) positive_integral_0_iff_AE:
  7.1954    assumes u: "u \<in> borel_measurable M"
  7.1955 -  shows "integral\<^isup>P M u = 0 \<longleftrightarrow> (AE x. u x = 0)"
  7.1956 +  shows "integral\<^isup>P M u = 0 \<longleftrightarrow> (AE x. u x \<le> 0)"
  7.1957  proof -
  7.1958 -  have sets: "{x\<in>space M. u x \<noteq> 0} \<in> sets M"
  7.1959 +  have sets: "{x\<in>space M. max 0 (u x) \<noteq> 0} \<in> sets M"
  7.1960      using u by auto
  7.1961 -  then show ?thesis
  7.1962 -    using positive_integral_0_iff[OF u] AE_iff_null_set[OF sets] by auto
  7.1963 +  from positive_integral_0_iff[of "\<lambda>x. max 0 (u x)"]
  7.1964 +  have "integral\<^isup>P M u = 0 \<longleftrightarrow> (AE x. max 0 (u x) = 0)"
  7.1965 +    unfolding positive_integral_max_0
  7.1966 +    using AE_iff_null_set[OF sets] u by auto
  7.1967 +  also have "\<dots> \<longleftrightarrow> (AE x. u x \<le> 0)" by (auto split: split_max)
  7.1968 +  finally show ?thesis .
  7.1969  qed
  7.1970  
  7.1971  lemma (in measure_space) positive_integral_restricted:
  7.1972 -  assumes "A \<in> sets M"
  7.1973 +  assumes A: "A \<in> sets M"
  7.1974    shows "integral\<^isup>P (restricted_space A) f = (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)"
  7.1975      (is "integral\<^isup>P ?R f = integral\<^isup>P M ?f")
  7.1976  proof -
  7.1977 -  have msR: "measure_space ?R" by (rule restricted_measure_space[OF `A \<in> sets M`])
  7.1978 -  then interpret R: measure_space ?R .
  7.1979 -  have saR: "sigma_algebra ?R" by fact
  7.1980 -  have *: "integral\<^isup>P ?R f = integral\<^isup>P ?R ?f"
  7.1981 -    by (intro R.positive_integral_cong) auto
  7.1982 +  interpret R: measure_space ?R
  7.1983 +    by (rule restricted_measure_space) fact
  7.1984 +  let "?I g x" = "g x * indicator A x :: extreal"
  7.1985    show ?thesis
  7.1986 -    unfolding * positive_integral_def
  7.1987 -    unfolding simple_function_restricted[OF `A \<in> sets M`]
  7.1988 -    apply (simp add: SUPR_def)
  7.1989 -    apply (rule arg_cong[where f=Sup])
  7.1990 -  proof (auto simp add: image_iff simple_integral_restricted[OF `A \<in> sets M`])
  7.1991 -    fix g assume "simple_function M (\<lambda>x. g x * indicator A x)"
  7.1992 -      "g \<le> f"
  7.1993 -    then show "\<exists>x. simple_function M x \<and> x \<le> (\<lambda>x. f x * indicator A x) \<and>
  7.1994 -      (\<integral>\<^isup>Sx. g x * indicator A x \<partial>M) = integral\<^isup>S M x"
  7.1995 -      apply (rule_tac exI[of _ "\<lambda>x. g x * indicator A x"])
  7.1996 -      by (auto simp: indicator_def le_fun_def)
  7.1997 +    unfolding positive_integral_def
  7.1998 +    unfolding simple_function_restricted[OF A]
  7.1999 +    unfolding AE_restricted[OF A]
  7.2000 +  proof (safe intro!: SUPR_eq)
  7.2001 +    fix g assume g: "simple_function M (?I g)" and le: "g \<le> max 0 \<circ> f"
  7.2002 +    show "\<exists>j\<in>{g. simple_function M g \<and> g \<le> max 0 \<circ> ?I f}.
  7.2003 +      integral\<^isup>S (restricted_space A) g \<le> integral\<^isup>S M j"
  7.2004 +    proof (safe intro!: bexI[of _ "?I g"])
  7.2005 +      show "integral\<^isup>S (restricted_space A) g \<le> integral\<^isup>S M (?I g)"
  7.2006 +        using g A by (simp add: simple_integral_restricted)
  7.2007 +      show "?I g \<le> max 0 \<circ> ?I f"
  7.2008 +        using le by (auto simp: le_fun_def max_def indicator_def split: split_if_asm)
  7.2009 +    qed fact
  7.2010    next
  7.2011 -    fix g assume g: "simple_function M g" "g \<le> (\<lambda>x. f x * indicator A x)"
  7.2012 -    then have *: "(\<lambda>x. g x * indicator A x) = g"
  7.2013 -      "\<And>x. g x * indicator A x = g x"
  7.2014 -      "\<And>x. g x \<le> f x"
  7.2015 -      by (auto simp: le_fun_def fun_eq_iff indicator_def split: split_if_asm)
  7.2016 -    from g show "\<exists>x. simple_function M (\<lambda>xa. x xa * indicator A xa) \<and> x \<le> f \<and>
  7.2017 -      integral\<^isup>S M g = integral\<^isup>S M (\<lambda>xa. x xa * indicator A xa)"
  7.2018 -      using `A \<in> sets M`[THEN sets_into_space]
  7.2019 -      apply (rule_tac exI[of _ "\<lambda>x. g x * indicator A x"])
  7.2020 -      by (fastsimp simp: le_fun_def *)
  7.2021 +    fix g assume g: "simple_function M g" and le: "g \<le> max 0 \<circ> ?I f"
  7.2022 +    show "\<exists>i\<in>{g. simple_function M (?I g) \<and> g \<le> max 0 \<circ> f}.
  7.2023 +      integral\<^isup>S M g \<le> integral\<^isup>S (restricted_space A) i"
  7.2024 +    proof (safe intro!: bexI[of _ "?I g"])
  7.2025 +      show "?I g \<le> max 0 \<circ> f"
  7.2026 +        using le by (auto simp: le_fun_def max_def indicator_def split: split_if_asm)
  7.2027 +      from le have "\<And>x. g x \<le> ?I (?I g) x"
  7.2028 +        by (auto simp: le_fun_def max_def indicator_def split: split_if_asm)
  7.2029 +      then show "integral\<^isup>S M g \<le> integral\<^isup>S (restricted_space A) (?I g)"
  7.2030 +        using A g by (auto intro!: simple_integral_mono simp: simple_integral_restricted)
  7.2031 +      show "simple_function M (?I (?I g))" using g A by auto
  7.2032 +    qed
  7.2033    qed
  7.2034  qed
  7.2035  
  7.2036  lemma (in measure_space) positive_integral_subalgebra:
  7.2037 -  assumes borel: "f \<in> borel_measurable N"
  7.2038 +  assumes f: "f \<in> borel_measurable N" "AE x in N. 0 \<le> f x"
  7.2039    and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> measure N A = \<mu> A"
  7.2040    and sa: "sigma_algebra N"
  7.2041    shows "integral\<^isup>P N f = integral\<^isup>P M f"
  7.2042  proof -
  7.2043    interpret N: measure_space N using measure_space_subalgebra[OF sa N] .
  7.2044 -  from N.borel_measurable_implies_simple_function_sequence[OF borel]
  7.2045 -  obtain fs where Nsf: "\<And>i. simple_function N (fs i)" and "fs \<up> f" by blast
  7.2046 -  then have sf: "\<And>i. simple_function M (fs i)"
  7.2047 -    using simple_function_subalgebra[OF _ N(1,2)] by blast
  7.2048 -  from N.positive_integral_isoton_simple[OF `fs \<up> f` Nsf]
  7.2049 +  from N.borel_measurable_implies_simple_function_sequence'[OF f(1)] guess fs . note fs = this
  7.2050 +  note sf = simple_function_subalgebra[OF fs(1) N(1,2)]
  7.2051 +  from N.positive_integral_monotone_convergence_simple[OF fs(2,5,1), symmetric]
  7.2052    have "integral\<^isup>P N f = (SUP i. \<Sum>x\<in>fs i ` space M. x * N.\<mu> (fs i -` {x} \<inter> space M))"
  7.2053 -    unfolding isoton_def simple_integral_def `space N = space M` by simp
  7.2054 +    unfolding fs(4) positive_integral_max_0
  7.2055 +    unfolding simple_integral_def `space N = space M` by simp
  7.2056    also have "\<dots> = (SUP i. \<Sum>x\<in>fs i ` space M. x * \<mu> (fs i -` {x} \<inter> space M))"
  7.2057 -    using N N.simple_functionD(2)[OF Nsf] unfolding `space N = space M` by auto
  7.2058 +    using N N.simple_functionD(2)[OF fs(1)] unfolding `space N = space M` by auto
  7.2059    also have "\<dots> = integral\<^isup>P M f"
  7.2060 -    using positive_integral_isoton_simple[OF `fs \<up> f` sf]
  7.2061 -    unfolding isoton_def simple_integral_def `space N = space M` by simp
  7.2062 +    using positive_integral_monotone_convergence_simple[OF fs(2,5) sf, symmetric]
  7.2063 +    unfolding fs(4) positive_integral_max_0
  7.2064 +    unfolding simple_integral_def `space N = space M` by simp
  7.2065    finally show ?thesis .
  7.2066  qed
  7.2067  
  7.2068 @@ -1555,16 +1668,15 @@
  7.2069  
  7.2070  definition integrable where
  7.2071    "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  7.2072 -    (\<integral>\<^isup>+ x. Real (f x) \<partial>M) \<noteq> \<omega> \<and>
  7.2073 -    (\<integral>\<^isup>+ x. Real (- f x) \<partial>M) \<noteq> \<omega>"
  7.2074 +    (\<integral>\<^isup>+ x. extreal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^isup>+ x. extreal (- f x) \<partial>M) \<noteq> \<infinity>"
  7.2075  
  7.2076  lemma integrableD[dest]:
  7.2077    assumes "integrable M f"
  7.2078 -  shows "f \<in> borel_measurable M" "(\<integral>\<^isup>+ x. Real (f x) \<partial>M) \<noteq> \<omega>" "(\<integral>\<^isup>+ x. Real (- f x) \<partial>M) \<noteq> \<omega>"
  7.2079 +  shows "f \<in> borel_measurable M" "(\<integral>\<^isup>+ x. extreal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^isup>+ x. extreal (- f x) \<partial>M) \<noteq> \<infinity>"
  7.2080    using assms unfolding integrable_def by auto
  7.2081  
  7.2082  definition lebesgue_integral_def:
  7.2083 -  "integral\<^isup>L M f = real ((\<integral>\<^isup>+ x. Real (f x) \<partial>M)) - real ((\<integral>\<^isup>+ x. Real (- f x) \<partial>M))"
  7.2084 +  "integral\<^isup>L M f = real ((\<integral>\<^isup>+ x. extreal (f x) \<partial>M)) - real ((\<integral>\<^isup>+ x. extreal (- f x) \<partial>M))"
  7.2085  
  7.2086  syntax
  7.2087    "_lebesgue_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> real" ("\<integral> _. _ \<partial>_" [60,61] 110)
  7.2088 @@ -1572,6 +1684,17 @@
  7.2089  translations
  7.2090    "\<integral> x. f \<partial>M" == "CONST integral\<^isup>L M (%x. f)"
  7.2091  
  7.2092 +lemma (in measure_space) integrableE:
  7.2093 +  assumes "integrable M f"
  7.2094 +  obtains r q where
  7.2095 +    "(\<integral>\<^isup>+x. extreal (f x)\<partial>M) = extreal r"
  7.2096 +    "(\<integral>\<^isup>+x. extreal (-f x)\<partial>M) = extreal q"
  7.2097 +    "f \<in> borel_measurable M" "integral\<^isup>L M f = r - q"
  7.2098 +  using assms unfolding integrable_def lebesgue_integral_def
  7.2099 +  using positive_integral_positive[of "\<lambda>x. extreal (f x)"]
  7.2100 +  using positive_integral_positive[of "\<lambda>x. extreal (-f x)"]
  7.2101 +  by (cases rule: extreal2_cases[of "(\<integral>\<^isup>+x. extreal (-f x)\<partial>M)" "(\<integral>\<^isup>+x. extreal (f x)\<partial>M)"]) auto
  7.2102 +
  7.2103  lemma (in measure_space) integral_cong:
  7.2104    assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
  7.2105    shows "integral\<^isup>L M f = integral\<^isup>L M g"
  7.2106 @@ -1580,21 +1703,16 @@
  7.2107  lemma (in measure_space) integral_cong_measure:
  7.2108    assumes "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A" "sets N = sets M" "space N = space M"
  7.2109    shows "integral\<^isup>L N f = integral\<^isup>L M f"
  7.2110 -proof -
  7.2111 -  interpret v: measure_space N
  7.2112 -    by (rule measure_space_cong) fact+
  7.2113 -  show ?thesis
  7.2114 -    by (simp add: positive_integral_cong_measure[OF assms] lebesgue_integral_def)
  7.2115 -qed
  7.2116 +  by (simp add: positive_integral_cong_measure[OF assms] lebesgue_integral_def)
  7.2117  
  7.2118  lemma (in measure_space) integral_cong_AE:
  7.2119    assumes cong: "AE x. f x = g x"
  7.2120    shows "integral\<^isup>L M f = integral\<^isup>L M g"
  7.2121  proof -
  7.2122 -  have "AE x. Real (f x) = Real (g x)" using cong by auto
  7.2123 -  moreover have "AE x. Real (- f x) = Real (- g x)" using cong by auto
  7.2124 -  ultimately show ?thesis
  7.2125 -    by (simp cong: positive_integral_cong_AE add: lebesgue_integral_def)
  7.2126 +  have *: "AE x. extreal (f x) = extreal (g x)"
  7.2127 +    "AE x. extreal (- f x) = extreal (- g x)" using cong by auto
  7.2128 +  show ?thesis
  7.2129 +    unfolding *[THEN positive_integral_cong_AE] lebesgue_integral_def ..
  7.2130  qed
  7.2131  
  7.2132  lemma (in measure_space) integrable_cong:
  7.2133 @@ -1602,11 +1720,14 @@
  7.2134    by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
  7.2135  
  7.2136  lemma (in measure_space) integral_eq_positive_integral:
  7.2137 -  assumes "\<And>x. 0 \<le> f x"
  7.2138 -  shows "integral\<^isup>L M f = real (\<integral>\<^isup>+ x. Real (f x) \<partial>M)"
  7.2139 +  assumes f: "\<And>x. 0 \<le> f x"
  7.2140 +  shows "integral\<^isup>L M f = real (\<integral>\<^isup>+ x. extreal (f x) \<partial>M)"
  7.2141  proof -
  7.2142 -  have "\<And>x. Real (- f x) = 0" using assms by simp
  7.2143 -  thus ?thesis by (simp del: Real_eq_0 add: lebesgue_integral_def)
  7.2144 +  { fix x have "max 0 (extreal (- f x)) = 0" using f[of x] by (simp split: split_max) }
  7.2145 +  then have "0 = (\<integral>\<^isup>+ x. max 0 (extreal (- f x)) \<partial>M)" by simp
  7.2146 +  also have "\<dots> = (\<integral>\<^isup>+ x. extreal (- f x) \<partial>M)" unfolding positive_integral_max_0 ..
  7.2147 +  finally show ?thesis
  7.2148 +    unfolding lebesgue_integral_def by simp
  7.2149  qed
  7.2150  
  7.2151  lemma (in measure_space) integral_vimage:
  7.2152 @@ -1616,7 +1737,7 @@
  7.2153  proof -
  7.2154    interpret T: measure_space M' by (rule measure_space_vimage[OF T])
  7.2155    from measurable_comp[OF measure_preservingD2[OF T(2)], of f borel]
  7.2156 -  have borel: "(\<lambda>x. Real (f x)) \<in> borel_measurable M'" "(\<lambda>x. Real (- f x)) \<in> borel_measurable M'"
  7.2157 +  have borel: "(\<lambda>x. extreal (f x)) \<in> borel_measurable M'" "(\<lambda>x. extreal (- f x)) \<in> borel_measurable M'"
  7.2158      and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
  7.2159      using f by (auto simp: comp_def)
  7.2160    then show ?thesis
  7.2161 @@ -1631,7 +1752,7 @@
  7.2162  proof -
  7.2163    interpret T: measure_space M' by (rule measure_space_vimage[OF T])
  7.2164    from measurable_comp[OF measure_preservingD2[OF T(2)], of f borel]
  7.2165 -  have borel: "(\<lambda>x. Real (f x)) \<in> borel_measurable M'" "(\<lambda>x. Real (- f x)) \<in> borel_measurable M'"
  7.2166 +  have borel: "(\<lambda>x. extreal (f x)) \<in> borel_measurable M'" "(\<lambda>x. extreal (- f x)) \<in> borel_measurable M'"
  7.2167      and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
  7.2168      using f by (auto simp: comp_def)
  7.2169    then show ?thesis
  7.2170 @@ -1649,10 +1770,10 @@
  7.2171    and f_def: "\<And>x. f x = u x - v x" and pos: "\<And>x. 0 \<le> u x" "\<And>x. 0 \<le> v x"
  7.2172    shows "integrable M f" and "integral\<^isup>L M f = integral\<^isup>L M u - integral\<^isup>L M v"
  7.2173  proof -
  7.2174 -  let "?f x" = "Real (f x)"
  7.2175 -  let "?mf x" = "Real (- f x)"
  7.2176 -  let "?u x" = "Real (u x)"
  7.2177 -  let "?v x" = "Real (v x)"
  7.2178 +  let "?f x" = "max 0 (extreal (f x))"
  7.2179 +  let "?mf x" = "max 0 (extreal (- f x))"
  7.2180 +  let "?u x" = "max 0 (extreal (u x))"
  7.2181 +  let "?v x" = "max 0 (extreal (v x))"
  7.2182  
  7.2183    from borel_measurable_diff[of u v] integrable
  7.2184    have f_borel: "?f \<in> borel_measurable M" and
  7.2185 @@ -1662,73 +1783,62 @@
  7.2186      "f \<in> borel_measurable M"
  7.2187      by (auto simp: f_def[symmetric] integrable_def)
  7.2188  
  7.2189 -  have "(\<integral>\<^isup>+ x. Real (u x - v x) \<partial>M) \<le> integral\<^isup>P M ?u"
  7.2190 -    using pos by (auto intro!: positive_integral_mono)
  7.2191 -  moreover have "(\<integral>\<^isup>+ x. Real (v x - u x) \<partial>M) \<le> integral\<^isup>P M ?v"
  7.2192 -    using pos by (auto intro!: positive_integral_mono)
  7.2193 +  have "(\<integral>\<^isup>+ x. extreal (u x - v x) \<partial>M) \<le> integral\<^isup>P M ?u"
  7.2194 +    using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
  7.2195 +  moreover have "(\<integral>\<^isup>+ x. extreal (v x - u x) \<partial>M) \<le> integral\<^isup>P M ?v"
  7.2196 +    using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
  7.2197    ultimately show f: "integrable M f"
  7.2198      using `integrable M u` `integrable M v` `f \<in> borel_measurable M`
  7.2199 -    by (auto simp: integrable_def f_def)
  7.2200 -  hence mf: "integrable M (\<lambda>x. - f x)" ..
  7.2201 -
  7.2202 -  have *: "\<And>x. Real (- v x) = 0" "\<And>x. Real (- u x) = 0"
  7.2203 -    using pos by auto
  7.2204 +    by (auto simp: integrable_def f_def positive_integral_max_0)
  7.2205  
  7.2206    have "\<And>x. ?u x + ?mf x = ?v x + ?f x"
  7.2207 -    unfolding f_def using pos by simp
  7.2208 -  hence "(\<integral>\<^isup>+ x. ?u x + ?mf x \<partial>M) = (\<integral>\<^isup>+ x. ?v x + ?f x \<partial>M)" by simp
  7.2209 -  hence "real (integral\<^isup>P M ?u + integral\<^isup>P M ?mf) =
  7.2210 +    unfolding f_def using pos by (simp split: split_max)
  7.2211 +  then have "(\<integral>\<^isup>+ x. ?u x + ?mf x \<partial>M) = (\<integral>\<^isup>+ x. ?v x + ?f x \<partial>M)" by simp
  7.2212 +  then have "real (integral\<^isup>P M ?u + integral\<^isup>P M ?mf) =
  7.2213        real (integral\<^isup>P M ?v + integral\<^isup>P M ?f)"
  7.2214 -    using positive_integral_add[OF u_borel mf_borel]
  7.2215 -    using positive_integral_add[OF v_borel f_borel]
  7.2216 +    using positive_integral_add[OF u_borel _ mf_borel]
  7.2217 +    using positive_integral_add[OF v_borel _ f_borel]
  7.2218      by auto
  7.2219    then show "integral\<^isup>L M f = integral\<^isup>L M u - integral\<^isup>L M v"
  7.2220 -    using f mf `integrable M u` `integrable M v`
  7.2221 -    unfolding lebesgue_integral_def integrable_def *
  7.2222 -    by (cases "integral\<^isup>P M ?f", cases "integral\<^isup>P M ?mf", cases "integral\<^isup>P M ?v", cases "integral\<^isup>P M ?u")
  7.2223 -       (auto simp add: field_simps)
  7.2224 +    unfolding positive_integral_max_0
  7.2225 +    unfolding pos[THEN integral_eq_positive_integral]
  7.2226 +    using integrable f by (auto elim!: integrableE)
  7.2227  qed
  7.2228  
  7.2229  lemma (in measure_space) integral_linear:
  7.2230    assumes "integrable M f" "integrable M g" and "0 \<le> a"
  7.2231    shows "integrable M (\<lambda>t. a * f t + g t)"
  7.2232 -  and "(\<integral> t. a * f t + g t \<partial>M) = a * integral\<^isup>L M f + integral\<^isup>L M g"
  7.2233 +  and "(\<integral> t. a * f t + g t \<partial>M) = a * integral\<^isup>L M f + integral\<^isup>L M g" (is ?EQ)
  7.2234  proof -
  7.2235 -  let ?PI = "integral\<^isup>P M"
  7.2236 -  let "?f x" = "Real (f x)"
  7.2237 -  let "?g x" = "Real (g x)"
  7.2238 -  let "?mf x" = "Real (- f x)"
  7.2239 -  let "?mg x" = "Real (- g x)"
  7.2240 +  let "?f x" = "max 0 (extreal (f x))"
  7.2241 +  let "?g x" = "max 0 (extreal (g x))"
  7.2242 +  let "?mf x" = "max 0 (extreal (- f x))"
  7.2243 +  let "?mg x" = "max 0 (extreal (- g x))"
  7.2244    let "?p t" = "max 0 (a * f t) + max 0 (g t)"
  7.2245    let "?n t" = "max 0 (- (a * f t)) + max 0 (- g t)"
  7.2246  
  7.2247 -  have pos: "?f \<in> borel_measurable M" "?g \<in> borel_measurable M"
  7.2248 -    and neg: "?mf \<in> borel_measurable M" "?mg \<in> borel_measurable M"
  7.2249 -    and p: "?p \<in> borel_measurable M"
  7.2250 -    and n: "?n \<in> borel_measurable M"
  7.2251 -    using assms by (simp_all add: integrable_def)
  7.2252 +  from assms have linear:
  7.2253 +    "(\<integral>\<^isup>+ x. extreal a * ?f x + ?g x \<partial>M) = extreal a * integral\<^isup>P M ?f + integral\<^isup>P M ?g"
  7.2254 +    "(\<integral>\<^isup>+ x. extreal a * ?mf x + ?mg x \<partial>M) = extreal a * integral\<^isup>P M ?mf + integral\<^isup>P M ?mg"
  7.2255 +    by (auto intro!: positive_integral_linear simp: integrable_def)
  7.2256  
  7.2257 -  have *: "\<And>x. Real (?p x) = Real a * ?f x + ?g x"
  7.2258 -          "\<And>x. Real (?n x) = Real a * ?mf x + ?mg x"
  7.2259 -          "\<And>x. Real (- ?p x) = 0"
  7.2260 -          "\<And>x. Real (- ?n x) = 0"
  7.2261 -    using `0 \<le> a` by (auto simp: max_def min_def zero_le_mult_iff mult_le_0_iff add_nonpos_nonpos)
  7.2262 -
  7.2263 -  note linear =
  7.2264 -    positive_integral_linear[OF pos]
  7.2265 -    positive_integral_linear[OF neg]
  7.2266 +  have *: "(\<integral>\<^isup>+x. extreal (- ?p x) \<partial>M) = 0" "(\<integral>\<^isup>+x. extreal (- ?n x) \<partial>M) = 0"
  7.2267 +    using `0 \<le> a` assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  7.2268 +  have **: "\<And>x. extreal a * ?f x + ?g x = max 0 (extreal (?p x))"
  7.2269 +           "\<And>x. extreal a * ?mf x + ?mg x = max 0 (extreal (?n x))"
  7.2270 +    using `0 \<le> a` by (auto split: split_max simp: zero_le_mult_iff mult_le_0_iff)
  7.2271  
  7.2272    have "integrable M ?p" "integrable M ?n"
  7.2273        "\<And>t. a * f t + g t = ?p t - ?n t" "\<And>t. 0 \<le> ?p t" "\<And>t. 0 \<le> ?n t"
  7.2274 -    using assms p n unfolding integrable_def * linear by auto
  7.2275 +    using linear assms unfolding integrable_def ** *
  7.2276 +    by (auto simp: positive_integral_max_0)
  7.2277    note diff = integral_of_positive_diff[OF this]
  7.2278  
  7.2279    show "integrable M (\<lambda>t. a * f t + g t)" by (rule diff)
  7.2280 -
  7.2281 -  from assms show "(\<integral> t. a * f t + g t \<partial>M) = a * integral\<^isup>L M f + integral\<^isup>L M g"
  7.2282 -    unfolding diff(2) unfolding lebesgue_integral_def * linear integrable_def
  7.2283 -    by (cases "?PI ?f", cases "?PI ?mf", cases "?PI ?g", cases "?PI ?mg")
  7.2284 -       (auto simp add: field_simps zero_le_mult_iff)
  7.2285 +  from assms linear show ?EQ
  7.2286 +    unfolding diff(2) ** positive_integral_max_0
  7.2287 +    unfolding lebesgue_integral_def *
  7.2288 +    by (auto elim!: integrableE simp: field_simps)
  7.2289  qed
  7.2290  
  7.2291  lemma (in measure_space) integral_add[simp, intro]:
  7.2292 @@ -1772,13 +1882,13 @@
  7.2293    and mono: "AE t. f t \<le> g t"
  7.2294    shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
  7.2295  proof -
  7.2296 -  have "AE x. Real (f x) \<le> Real (g x)"
  7.2297 +  have "AE x. extreal (f x) \<le> extreal (g x)"
  7.2298      using mono by auto
  7.2299 -  moreover have "AE x. Real (- g x) \<le> Real (- f x)"
  7.2300 +  moreover have "AE x. extreal (- g x) \<le> extreal (- f x)"
  7.2301      using mono by auto
  7.2302    ultimately show ?thesis using fg
  7.2303 -    by (auto simp: lebesgue_integral_def integrable_def diff_minus
  7.2304 -             intro!: add_mono real_of_pextreal_mono positive_integral_mono_AE)
  7.2305 +    by (auto intro!: add_mono positive_integral_mono_AE real_of_extreal_positive_mono
  7.2306 +             simp: positive_integral_positive lebesgue_integral_def diff_minus)
  7.2307  qed
  7.2308  
  7.2309  lemma (in measure_space) integral_mono:
  7.2310 @@ -1795,20 +1905,21 @@
  7.2311    by auto
  7.2312  
  7.2313  lemma (in measure_space) integral_indicator[simp, intro]:
  7.2314 -  assumes "a \<in> sets M" and "\<mu> a \<noteq> \<omega>"
  7.2315 -  shows "integral\<^isup>L M (indicator a) = real (\<mu> a)" (is ?int)
  7.2316 -  and "integrable M (indicator a)" (is ?able)
  7.2317 +  assumes "A \<in> sets M" and "\<mu> A \<noteq> \<infinity>"
  7.2318 +  shows "integral\<^isup>L M (indicator A) = real (\<mu> A)" (is ?int)
  7.2319 +  and "integrable M (indicator A)" (is ?able)
  7.2320  proof -
  7.2321 -  have *:
  7.2322 -    "\<And>A x. Real (indicator A x) = indicator A x"
  7.2323 -    "\<And>A x. Real (- indicator A x) = 0" unfolding indicator_def by auto
  7.2324 +  from `A \<in> sets M` have *:
  7.2325 +    "\<And>x. extreal (indicator A x) = indicator A x"
  7.2326 +    "(\<integral>\<^isup>+x. extreal (- indicator A x) \<partial>M) = 0"
  7.2327 +    by (auto split: split_indicator simp: positive_integral_0_iff_AE one_extreal_def)
  7.2328    show ?int ?able
  7.2329      using assms unfolding lebesgue_integral_def integrable_def
  7.2330      by (auto simp: * positive_integral_indicator borel_measurable_indicator)
  7.2331  qed
  7.2332  
  7.2333  lemma (in measure_space) integral_cmul_indicator:
  7.2334 -  assumes "A \<in> sets M" and "c \<noteq> 0 \<Longrightarrow> \<mu> A \<noteq> \<omega>"
  7.2335 +  assumes "A \<in> sets M" and "c \<noteq> 0 \<Longrightarrow> \<mu> A \<noteq> \<infinity>"
  7.2336    shows "integrable M (\<lambda>x. c * indicator A x)" (is ?P)
  7.2337    and "(\<integral>x. c * indicator A x \<partial>M) = c * real (\<mu> A)" (is ?I)
  7.2338  proof -
  7.2339 @@ -1840,15 +1951,11 @@
  7.2340    assumes "integrable M f"
  7.2341    shows "integrable M (\<lambda> x. \<bar>f x\<bar>)"
  7.2342  proof -
  7.2343 -  have *:
  7.2344 -    "\<And>x. Real \<bar>f x\<bar> = Real (f x) + Real (- f x)"
  7.2345 -    "\<And>x. Real (- \<bar>f x\<bar>) = 0" by auto
  7.2346 -  have abs: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M" and
  7.2347 -    f: "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
  7.2348 -        "(\<lambda>x. Real (- f x)) \<in> borel_measurable M"
  7.2349 -    using assms unfolding integrable_def by auto
  7.2350 -  from abs assms show ?thesis unfolding integrable_def *
  7.2351 -    using positive_integral_linear[OF f, of 1] by simp
  7.2352 +  from assms have *: "(\<integral>\<^isup>+x. extreal (- \<bar>f x\<bar>)\<partial>M) = 0"
  7.2353 +    "\<And>x. extreal \<bar>f x\<bar> = max 0 (extreal (f x)) + max 0 (extreal (- f x))"
  7.2354 +    by (auto simp: integrable_def positive_integral_0_iff_AE split: split_max)
  7.2355 +  with assms show ?thesis
  7.2356 +    by (simp add: positive_integral_add positive_integral_max_0 integrable_def)
  7.2357  qed
  7.2358  
  7.2359  lemma (in measure_space) integral_subalgebra:
  7.2360 @@ -1858,12 +1965,13 @@
  7.2361      and "integral\<^isup>L N f = integral\<^isup>L M f" (is ?I)
  7.2362  proof -
  7.2363    interpret N: measure_space N using measure_space_subalgebra[OF sa N] .
  7.2364 -  have "(\<lambda>x. Real (f x)) \<in> borel_measurable N" "(\<lambda>x. Real (- f x)) \<in> borel_measurable N"
  7.2365 -    using borel by auto
  7.2366 -  note * = this[THEN positive_integral_subalgebra[OF _ N sa]]
  7.2367 -  have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
  7.2368 +  have "(\<integral>\<^isup>+ x. max 0 (extreal (f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (extreal (f x)) \<partial>M)"
  7.2369 +       "(\<integral>\<^isup>+ x. max 0 (extreal (- f x)) \<partial>N) = (\<integral>\<^isup>+ x. max 0 (extreal (- f x)) \<partial>M)"
  7.2370 +    using borel by (auto intro!: positive_integral_subalgebra N sa)
  7.2371 +  moreover have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
  7.2372      using assms unfolding measurable_def by auto
  7.2373 -  then show ?P ?I by (auto simp: * integrable_def lebesgue_integral_def)
  7.2374 +  ultimately show ?P ?I
  7.2375 +    by (auto simp: integrable_def lebesgue_integral_def positive_integral_max_0)
  7.2376  qed
  7.2377  
  7.2378  lemma (in measure_space) integrable_bound:
  7.2379 @@ -1873,21 +1981,21 @@
  7.2380    assumes borel: "g \<in> borel_measurable M"
  7.2381    shows "integrable M g"
  7.2382  proof -
  7.2383 -  have "(\<integral>\<^isup>+ x. Real (g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. Real \<bar>g x\<bar> \<partial>M)"
  7.2384 +  have "(\<integral>\<^isup>+ x. extreal (g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. extreal \<bar>g x\<bar> \<partial>M)"
  7.2385      by (auto intro!: positive_integral_mono)
  7.2386 -  also have "\<dots> \<le> (\<integral>\<^isup>+ x. Real (f x) \<partial>M)"
  7.2387 +  also have "\<dots> \<le> (\<integral>\<^isup>+ x. extreal (f x) \<partial>M)"
  7.2388      using f by (auto intro!: positive_integral_mono)
  7.2389 -  also have "\<dots> < \<omega>"
  7.2390 -    using `integrable M f` unfolding integrable_def by (auto simp: pextreal_less_\<omega>)
  7.2391 -  finally have pos: "(\<integral>\<^isup>+ x. Real (g x) \<partial>M) < \<omega>" .
  7.2392 +  also have "\<dots> < \<infinity>"
  7.2393 +    using `integrable M f` unfolding integrable_def by auto
  7.2394 +  finally have pos: "(\<integral>\<^isup>+ x. extreal (g x) \<partial>M) < \<infinity>" .
  7.2395  
  7.2396 -  have "(\<integral>\<^isup>+ x. Real (- g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. Real (\<bar>g x\<bar>) \<partial>M)"
  7.2397 +  have "(\<integral>\<^isup>+ x. extreal (- g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. extreal (\<bar>g x\<bar>) \<partial>M)"
  7.2398      by (auto intro!: positive_integral_mono)
  7.2399 -  also have "\<dots> \<le> (\<integral>\<^isup>+ x. Real (f x) \<partial>M)"
  7.2400 +  also have "\<dots> \<le> (\<integral>\<^isup>+ x. extreal (f x) \<partial>M)"
  7.2401      using f by (auto intro!: positive_integral_mono)
  7.2402 -  also have "\<dots> < \<omega>"
  7.2403 -    using `integrable M f` unfolding integrable_def by (auto simp: pextreal_less_\<omega>)
  7.2404 -  finally have neg: "(\<integral>\<^isup>+ x. Real (- g x) \<partial>M) < \<omega>" .
  7.2405 +  also have "\<dots> < \<infinity>"
  7.2406 +    using `integrable M f` unfolding integrable_def by auto
  7.2407 +  finally have neg: "(\<integral>\<^isup>+ x. extreal (- g x) \<partial>M) < \<infinity>" .
  7.2408  
  7.2409    from neg pos borel show ?thesis
  7.2410      unfolding integrable_def by auto
  7.2411 @@ -1959,41 +2067,34 @@
  7.2412        by (simp add: mono_def incseq_def) }
  7.2413    note pos_u = this
  7.2414  
  7.2415 -  hence [simp]: "\<And>i x. Real (- f i x) = 0" "\<And>x. Real (- u x) = 0"
  7.2416 -    using pos by auto
  7.2417 +  have SUP_F: "\<And>x. (SUP n. extreal (f n x)) = extreal (u x)"
  7.2418 +    unfolding SUP_eq_LIMSEQ[OF mono] by (rule lim)
  7.2419  
  7.2420 -  have SUP_F: "\<And>x. (SUP n. Real (f n x)) = Real (u x)"
  7.2421 -    using mono pos pos_u lim by (rule SUP_eq_LIMSEQ[THEN iffD2])
  7.2422 -
  7.2423 -  have borel_f: "\<And>i. (\<lambda>x. Real (f i x)) \<in> borel_measurable M"
  7.2424 +  have borel_f: "\<And>i. (\<lambda>x. extreal (f i x)) \<in> borel_measurable M"
  7.2425      using i unfolding integrable_def by auto
  7.2426 -  hence "(\<lambda>x. SUP i. Real (f i x)) \<in> borel_measurable M"
  7.2427 +  hence "(\<lambda>x. SUP i. extreal (f i x)) \<in> borel_measurable M"
  7.2428      by auto
  7.2429    hence borel_u: "u \<in> borel_measurable M"
  7.2430 -    using pos_u by (auto simp: borel_measurable_Real_eq SUP_F)
  7.2431 +    by (auto simp: borel_measurable_extreal_iff SUP_F)
  7.2432  
  7.2433 -  have integral_eq: "\<And>n. (\<integral>\<^isup>+ x. Real (f n x) \<partial>M) = Real (integral\<^isup>L M (f n))"
  7.2434 -    using i unfolding lebesgue_integral_def integrable_def by (auto simp: Real_real)
  7.2435 +  hence [simp]: "\<And>i. (\<integral>\<^isup>+x. extreal (- f i x) \<partial>M) = 0" "(\<integral>\<^isup>+x. extreal (- u x) \<partial>M) = 0"
  7.2436 +    using i borel_u pos pos_u by (auto simp: positive_integral_0_iff_AE integrable_def)
  7.2437 +
  7.2438 +  have integral_eq: "\<And>n. (\<integral>\<^isup>+ x. extreal (f n x) \<partial>M) = extreal (integral\<^isup>L M (f n))"
  7.2439 +    using i positive_integral_positive by (auto simp: extreal_real lebesgue_integral_def integrable_def)
  7.2440  
  7.2441    have pos_integral: "\<And>n. 0 \<le> integral\<^isup>L M (f n)"
  7.2442      using pos i by (auto simp: integral_positive)
  7.2443    hence "0 \<le> x"
  7.2444      using LIMSEQ_le_const[OF ilim, of 0] by auto
  7.2445  
  7.2446 -  have "(\<lambda>i. (\<integral>\<^isup>+ x. Real (f i x) \<partial>M)) \<up> (\<integral>\<^isup>+ x. Real (u x) \<partial>M)"
  7.2447 -  proof (rule positive_integral_isoton)
  7.2448 -    from SUP_F mono pos
  7.2449 -    show "(\<lambda>i x. Real (f i x)) \<up> (\<lambda>x. Real (u x))"
  7.2450 -      unfolding isoton_fun_expand by (auto simp: isoton_def mono_def)
  7.2451 -  qed (rule borel_f)
  7.2452 -  hence pI: "(\<integral>\<^isup>+ x. Real (u x) \<partial>M) = (SUP n. (\<integral>\<^isup>+ x. Real (f n x) \<partial>M))"
  7.2453 -    unfolding isoton_def by simp
  7.2454 -  also have "\<dots> = Real x" unfolding integral_eq
  7.2455 +  from mono pos i have pI: "(\<integral>\<^isup>+ x. extreal (u x) \<partial>M) = (SUP n. (\<integral>\<^isup>+ x. extreal (f n x) \<partial>M))"
  7.2456 +    by (auto intro!: positive_integral_monotone_convergence_SUP
  7.2457 +      simp: integrable_def incseq_mono incseq_Suc_iff le_fun_def SUP_F[symmetric])
  7.2458 +  also have "\<dots> = extreal x" unfolding integral_eq
  7.2459    proof (rule SUP_eq_LIMSEQ[THEN iffD2])
  7.2460      show "mono (\<lambda>n. integral\<^isup>L M (f n))"
  7.2461        using mono i by (auto simp: mono_def intro!: integral_mono)
  7.2462 -    show "\<And>n. 0 \<le> integral\<^isup>L M (f n)" using pos_integral .
  7.2463 -    show "0 \<le> x" using `0 \<le> x` .
  7.2464      show "(\<lambda>n. integral\<^isup>L M (f n)) ----> x" using ilim .
  7.2465    qed
  7.2466    finally show  "integrable M u" "integral\<^isup>L M u = x" using borel_u `0 \<le> x`
  7.2467 @@ -2028,61 +2129,72 @@
  7.2468    assumes "integrable M f"
  7.2469    shows "(\<integral>x. \<bar>f x\<bar> \<partial>M) = 0 \<longleftrightarrow> \<mu> {x\<in>space M. f x \<noteq> 0} = 0"
  7.2470  proof -
  7.2471 -  have *: "\<And>x. Real (- \<bar>f x\<bar>) = 0" by auto
  7.2472 +  have *: "(\<integral>\<^isup>+x. extreal (- \<bar>f x\<bar>) \<partial>M) = 0"
  7.2473 +    using assms by (auto simp: positive_integral_0_iff_AE integrable_def)
  7.2474    have "integrable M (\<lambda>x. \<bar>f x\<bar>)" using assms by (rule integrable_abs)
  7.2475 -  hence "(\<lambda>x. Real (\<bar>f x\<bar>)) \<in> borel_measurable M"
  7.2476 -    "(\<integral>\<^isup>+ x. Real \<bar>f x\<bar> \<partial>M) \<noteq> \<omega>" unfolding integrable_def by auto
  7.2477 +  hence "(\<lambda>x. extreal (\<bar>f x\<bar>)) \<in> borel_measurable M"
  7.2478 +    "(\<integral>\<^isup>+ x. extreal \<bar>f x\<bar> \<partial>M) \<noteq> \<infinity>" unfolding integrable_def by auto
  7.2479    from positive_integral_0_iff[OF this(1)] this(2)
  7.2480    show ?thesis unfolding lebesgue_integral_def *
  7.2481 -    by (simp add: real_of_pextreal_eq_0)
  7.2482 +    using positive_integral_positive[of "\<lambda>x. extreal \<bar>f x\<bar>"]
  7.2483 +    by (auto simp add: real_of_extreal_eq_0)
  7.2484  qed
  7.2485  
  7.2486 -lemma (in measure_space) positive_integral_omega:
  7.2487 -  assumes "f \<in> borel_measurable M"
  7.2488 -  and "integral\<^isup>P M f \<noteq> \<omega>"
  7.2489 -  shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
  7.2490 +lemma (in measure_space) positive_integral_PInf:
  7.2491 +  assumes f: "f \<in> borel_measurable M"
  7.2492 +  and not_Inf: "integral\<^isup>P M f \<noteq> \<infinity>"
  7.2493 +  shows "\<mu> (f -` {\<infinity>} \<inter> space M) = 0"
  7.2494  proof -
  7.2495 -  have "\<omega> * \<mu> (f -` {\<omega>} \<inter> space M) = (\<integral>\<^isup>+ x. \<omega> * indicator (f -` {\<omega>} \<inter> space M) x \<partial>M)"
  7.2496 -    using positive_integral_cmult_indicator[OF borel_measurable_vimage, OF assms(1), of \<omega> \<omega>] by simp
  7.2497 -  also have "\<dots> \<le> integral\<^isup>P M f"
  7.2498 -    by (auto intro!: positive_integral_mono simp: indicator_def)
  7.2499 -  finally show ?thesis
  7.2500 -    using assms(2) by (cases ?thesis) auto
  7.2501 +  have "\<infinity> * \<mu> (f -` {\<infinity>} \<inter> space M) = (\<integral>\<^isup>+ x. \<infinity> * indicator (f -` {\<infinity>} \<inter> space M) x \<partial>M)"
  7.2502 +    using f by (subst positive_integral_cmult_indicator) (auto simp: measurable_sets)
  7.2503 +  also have "\<dots> \<le> integral\<^isup>P M (\<lambda>x. max 0 (f x))"
  7.2504 +    by (auto intro!: positive_integral_mono simp: indicator_def max_def)
  7.2505 +  finally have "\<infinity> * \<mu> (f -` {\<infinity>} \<inter> space M) \<le> integral\<^isup>P M f"
  7.2506 +    by (simp add: positive_integral_max_0)
  7.2507 +  moreover have "0 \<le> \<mu> (f -` {\<infinity>} \<inter> space M)"
  7.2508 +    using f by (simp add: measurable_sets)
  7.2509 +  ultimately show ?thesis
  7.2510 +    using assms by (auto split: split_if_asm)
  7.2511  qed
  7.2512  
  7.2513 -lemma (in measure_space) positive_integral_omega_AE:
  7.2514 -  assumes "f \<in> borel_measurable M" "integral\<^isup>P M f \<noteq> \<omega>" shows "AE x. f x \<noteq> \<omega>"
  7.2515 +lemma (in measure_space) positive_integral_PInf_AE:
  7.2516 +  assumes "f \<in> borel_measurable M" "integral\<^isup>P M f \<noteq> \<infinity>" shows "AE x. f x \<noteq> \<infinity>"
  7.2517  proof (rule AE_I)
  7.2518 -  show "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
  7.2519 -    by (rule positive_integral_omega[OF assms])
  7.2520 -  show "f -` {\<omega>} \<inter> space M \<in> sets M"
  7.2521 +  show "\<mu> (f -` {\<infinity>} \<inter> space M) = 0"
  7.2522 +    by (rule positive_integral_PInf[OF assms])
  7.2523 +  show "f -` {\<infinity>} \<inter> space M \<in> sets M"
  7.2524      using assms by (auto intro: borel_measurable_vimage)
  7.2525  qed auto
  7.2526  
  7.2527 -lemma (in measure_space) simple_integral_omega:
  7.2528 -  assumes "simple_function M f"
  7.2529 -  and "integral\<^isup>S M f \<noteq> \<omega>"
  7.2530 -  shows "\<mu> (f -` {\<omega>} \<inter> space M) = 0"
  7.2531 -proof (rule positive_integral_omega)
  7.2532 +lemma (in measure_space) simple_integral_PInf:
  7.2533 +  assumes "simple_function M f" "\<And>x. 0 \<le> f x"
  7.2534 +  and "integral\<^isup>S M f \<noteq> \<infinity>"
  7.2535 +  shows "\<mu> (f -` {\<infinity>} \<inter> space M) = 0"
  7.2536 +proof (rule positive_integral_PInf)
  7.2537    show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
  7.2538 -  show "integral\<^isup>P M f \<noteq> \<omega>"
  7.2539 +  show "integral\<^isup>P M f \<noteq> \<infinity>"
  7.2540      using assms by (simp add: positive_integral_eq_simple_integral)
  7.2541  qed
  7.2542  
  7.2543  lemma (in measure_space) integral_real:
  7.2544 -  fixes f :: "'a \<Rightarrow> pextreal"
  7.2545 -  assumes [simp]: "AE x. f x \<noteq> \<omega>"
  7.2546 -  shows "(\<integral>x. real (f x) \<partial>M) = real (integral\<^isup>P M f)" (is ?plus)
  7.2547 -    and "(\<integral>x. - real (f x) \<partial>M) = - real (integral\<^isup>P M f)" (is ?minus)
  7.2548 -proof -
  7.2549 -  have "(\<integral>\<^isup>+ x. Real (real (f x)) \<partial>M) = integral\<^isup>P M f"
  7.2550 -    by (auto intro!: positive_integral_cong_AE simp: Real_real)
  7.2551 -  moreover
  7.2552 -  have "(\<integral>\<^isup>+ x. Real (- real (f x)) \<partial>M) = (\<integral>\<^isup>+ x. 0 \<partial>M)"
  7.2553 -    by (intro positive_integral_cong) auto
  7.2554 -  ultimately show ?plus ?minus
  7.2555 -    by (auto simp: lebesgue_integral_def integrable_def)
  7.2556 -qed
  7.2557 +  "AE x. \<bar>f x\<bar> \<noteq> \<infinity> \<Longrightarrow> (\<integral>x. real (f x) \<partial>M) = real (integral\<^isup>P M f) - real (integral\<^isup>P M (\<lambda>x. - f x))"
  7.2558 +  using assms unfolding lebesgue_integral_def
  7.2559 +  by (subst (1 2) positive_integral_cong_AE) (auto simp add: extreal_real)
  7.2560 +
  7.2561 +lemma liminf_extreal_cminus:
  7.2562 +  fixes f :: "nat \<Rightarrow> extreal" assumes "c \<noteq> -\<infinity>"
  7.2563 +  shows "liminf (\<lambda>x. c - f x) = c - limsup f"
  7.2564 +proof (cases c)
  7.2565 +  case PInf then show ?thesis by (simp add: Liminf_const)
  7.2566 +next
  7.2567 +  case (real r) then show ?thesis
  7.2568 +    unfolding liminf_SUPR_INFI limsup_INFI_SUPR
  7.2569 +    apply (subst INFI_extreal_cminus)
  7.2570 +    apply auto
  7.2571 +    apply (subst SUPR_extreal_cminus)
  7.2572 +    apply auto
  7.2573 +    done
  7.2574 +qed (insert `c \<noteq> -\<infinity>`, simp)
  7.2575  
  7.2576  lemma (in measure_space) integral_dominated_convergence:
  7.2577    assumes u: "\<And>i. integrable M (u i)" and bound: "\<And>x j. x\<in>space M \<Longrightarrow> \<bar>u j x\<bar> \<le> w x"
  7.2578 @@ -2129,61 +2241,76 @@
  7.2579      finally have "\<bar>u j x - u' x\<bar> \<le> 2 * w x" by simp }
  7.2580    note diff_less_2w = this
  7.2581  
  7.2582 -  have PI_diff: "\<And>m n. (\<integral>\<^isup>+ x. Real (?diff (m + n) x) \<partial>M) =
  7.2583 -    (\<integral>\<^isup>+ x. Real (2 * w x) \<partial>M) - (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar> \<partial>M)"
  7.2584 +  have PI_diff: "\<And>n. (\<integral>\<^isup>+ x. extreal (?diff n x) \<partial>M) =
  7.2585 +    (\<integral>\<^isup>+ x. extreal (2 * w x) \<partial>M) - (\<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)"
  7.2586      using diff w diff_less_2w w_pos
  7.2587      by (subst positive_integral_diff[symmetric])
  7.2588         (auto simp: integrable_def intro!: positive_integral_cong)
  7.2589  
  7.2590    have "integrable M (\<lambda>x. 2 * w x)"
  7.2591      using w by (auto intro: integral_cmult)
  7.2592 -  hence I2w_fin: "(\<integral>\<^isup>+ x. Real (2 * w x) \<partial>M) \<noteq> \<omega>" and
  7.2593 -    borel_2w: "(\<lambda>x. Real (2 * w x)) \<in> borel_measurable M"
  7.2594 +  hence I2w_fin: "(\<integral>\<^isup>+ x. extreal (2 * w x) \<partial>M) \<noteq> \<infinity>" and
  7.2595 +    borel_2w: "(\<lambda>x. extreal (2 * w x)) \<in> borel_measurable M"
  7.2596      unfolding integrable_def by auto
  7.2597  
  7.2598 -  have "(INF n. SUP m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar> \<partial>M)) = 0" (is "?lim_SUP = 0")
  7.2599 +  have "limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M) = 0" (is "limsup ?f = 0")
  7.2600    proof cases
  7.2601 -    assume eq_0: "(\<integral>\<^isup>+ x. Real (2 * w x) \<partial>M) = 0"
  7.2602 -    have "\<And>i. (\<integral>\<^isup>+ x. Real \<bar>u i x - u' x\<bar> \<partial>M) \<le> (\<integral>\<^isup>+ x. Real (2 * w x) \<partial>M)"
  7.2603 -    proof (rule positive_integral_mono)
  7.2604 -      fix i x assume "x \<in> space M" from diff_less_2w[OF this, of i]
  7.2605 -      show "Real \<bar>u i x - u' x\<bar> \<le> Real (2 * w x)" by auto
  7.2606 -    qed
  7.2607 -    hence "\<And>i. (\<integral>\<^isup>+ x. Real \<bar>u i x - u' x\<bar> \<partial>M) = 0" using eq_0 by auto
  7.2608 -    thus ?thesis by simp
  7.2609 +    assume eq_0: "(\<integral>\<^isup>+ x. max 0 (extreal (2 * w x)) \<partial>M) = 0" (is "?wx = 0")
  7.2610 +    { fix n
  7.2611 +      have "?f n \<le> ?wx" (is "integral\<^isup>P M ?f' \<le> _")
  7.2612 +        using diff_less_2w[of _ n] unfolding positive_integral_max_0
  7.2613 +        by (intro positive_integral_mono) auto
  7.2614 +      then have "?f n = 0"
  7.2615 +        using positive_integral_positive[of ?f'] eq_0 by auto }
  7.2616 +    then show ?thesis by (simp add: Limsup_const)
  7.2617    next
  7.2618 -    assume neq_0: "(\<integral>\<^isup>+ x. Real (2 * w x) \<partial>M) \<noteq> 0"
  7.2619 -    have "(\<integral>\<^isup>+ x. Real (2 * w x) \<partial>M) = (\<integral>\<^isup>+ x. (SUP n. INF m. Real (?diff (m + n) x)) \<partial>M)"
  7.2620 -    proof (rule positive_integral_cong, subst add_commute)
  7.2621 +    assume neq_0: "(\<integral>\<^isup>+ x. max 0 (extreal (2 * w x)) \<partial>M) \<noteq> 0" (is "?wx \<noteq> 0")
  7.2622 +    have "0 = limsup (\<lambda>n. 0 :: extreal)" by (simp add: Limsup_const)
  7.2623 +    also have "\<dots> \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)"
  7.2624 +      by (intro limsup_mono positive_integral_positive)
  7.2625 +    finally have pos: "0 \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)" .
  7.2626 +    have "?wx = (\<integral>\<^isup>+ x. liminf (\<lambda>n. max 0 (extreal (?diff n x))) \<partial>M)"
  7.2627 +    proof (rule positive_integral_cong)
  7.2628        fix x assume x: "x \<in> space M"
  7.2629 -      show "Real (2 * w x) = (SUP n. INF m. Real (?diff (n + m) x))"
  7.2630 -      proof (rule LIMSEQ_imp_lim_INF[symmetric])
  7.2631 -        fix j show "0 \<le> ?diff j x" using diff_less_2w[OF x, of j] by simp
  7.2632 -      next
  7.2633 +      show "max 0 (extreal (2 * w x)) = liminf (\<lambda>n. max 0 (extreal (?diff n x)))"
  7.2634 +        unfolding extreal_max_0
  7.2635 +      proof (rule lim_imp_Liminf[symmetric], unfold lim_extreal)
  7.2636          have "(\<lambda>i. ?diff i x) ----> 2 * w x - \<bar>u' x - u' x\<bar>"
  7.2637            using u'[OF x] by (safe intro!: LIMSEQ_diff LIMSEQ_const LIMSEQ_imp_rabs)
  7.2638 -        thus "(\<lambda>i. ?diff i x) ----> 2 * w x" by simp
  7.2639 -      qed
  7.2640 +        then show "(\<lambda>i. max 0 (?diff i x)) ----> max 0 (2 * w x)"
  7.2641 +          by (auto intro!: tendsto_real_max simp add: lim_extreal)
  7.2642 +      qed (rule trivial_limit_sequentially)
  7.2643      qed
  7.2644 -    also have "\<dots> \<le> (SUP n. INF m. (\<integral>\<^isup>+ x. Real (?diff (m + n) x) \<partial>M))"
  7.2645 +    also have "\<dots> \<le> liminf (\<lambda>n. \<integral>\<^isup>+ x. max 0 (extreal (?diff n x)) \<partial>M)"
  7.2646        using u'_borel w u unfolding integrable_def
  7.2647 -      by (auto intro!: positive_integral_lim_INF)
  7.2648 -    also have "\<dots> = (\<integral>\<^isup>+ x. Real (2 * w x) \<partial>M) -
  7.2649 -        (INF n. SUP m. \<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar> \<partial>M)"
  7.2650 -      unfolding PI_diff pextreal_INF_minus[OF I2w_fin] pextreal_SUP_minus ..
  7.2651 -    finally show ?thesis using neq_0 I2w_fin by (rule pextreal_le_minus_imp_0)
  7.2652 +      by (intro positive_integral_lim_INF) (auto intro!: positive_integral_lim_INF)
  7.2653 +    also have "\<dots> = (\<integral>\<^isup>+ x. extreal (2 * w x) \<partial>M) -
  7.2654 +        limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)"
  7.2655 +      unfolding PI_diff positive_integral_max_0
  7.2656 +      using positive_integral_positive[of "\<lambda>x. extreal (2 * w x)"]
  7.2657 +      by (subst liminf_extreal_cminus) auto
  7.2658 +    finally show ?thesis
  7.2659 +      using neq_0 I2w_fin positive_integral_positive[of "\<lambda>x. extreal (2 * w x)"] pos
  7.2660 +      unfolding positive_integral_max_0
  7.2661 +      by (cases rule: extreal2_cases[of "\<integral>\<^isup>+ x. extreal (2 * w x) \<partial>M" "limsup (\<lambda>n. \<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M)"])
  7.2662 +         auto
  7.2663    qed
  7.2664  
  7.2665 -  have [simp]: "\<And>n m x. Real (- \<bar>u (m + n) x - u' x\<bar>) = 0" by auto
  7.2666 -
  7.2667 -  have [simp]: "\<And>n m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar> \<partial>M) =
  7.2668 -    Real ((\<integral>x. \<bar>u (n + m) x - u' x\<bar> \<partial>M))"
  7.2669 -    using diff by (subst add_commute) (simp add: lebesgue_integral_def integrable_def Real_real)
  7.2670 -
  7.2671 -  have "(SUP n. INF m. (\<integral>\<^isup>+ x. Real \<bar>u (m + n) x - u' x\<bar> \<partial>M)) \<le> ?lim_SUP"
  7.2672 -    (is "?lim_INF \<le> _") by (subst (1 2) add_commute) (rule lim_INF_le_lim_SUP)
  7.2673 -  hence "?lim_INF = Real 0" "?lim_SUP = Real 0" using `?lim_SUP = 0` by auto
  7.2674 -  thus ?lim_diff using diff by (auto intro!: integral_positive lim_INF_eq_lim_SUP)
  7.2675 +  have "liminf ?f \<le> limsup ?f"
  7.2676 +    by (intro extreal_Liminf_le_Limsup trivial_limit_sequentially)
  7.2677 +  moreover
  7.2678 +  { have "0 = liminf (\<lambda>n. 0 :: extreal)" by (simp add: Liminf_const)
  7.2679 +    also have "\<dots> \<le> liminf ?f"
  7.2680 +      by (intro liminf_mono positive_integral_positive)
  7.2681 +    finally have "0 \<le> liminf ?f" . }
  7.2682 +  ultimately have liminf_limsup_eq: "liminf ?f = extreal 0" "limsup ?f = extreal 0"
  7.2683 +    using `limsup ?f = 0` by auto
  7.2684 +  have "\<And>n. (\<integral>\<^isup>+ x. extreal \<bar>u n x - u' x\<bar> \<partial>M) = extreal (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M)"
  7.2685 +    using diff positive_integral_positive
  7.2686 +    by (subst integral_eq_positive_integral) (auto simp: extreal_real integrable_def)
  7.2687 +  then show ?lim_diff
  7.2688 +    using extreal_Liminf_eq_Limsup[OF trivial_limit_sequentially liminf_limsup_eq]
  7.2689 +    by (simp add: lim_extreal)
  7.2690  
  7.2691    show ?lim
  7.2692    proof (rule LIMSEQ_I)
  7.2693 @@ -2266,7 +2393,7 @@
  7.2694    assumes f: "f \<in> borel_measurable M"
  7.2695    and bij: "bij_betw enum S (f ` space M)"
  7.2696    and enum_zero: "enum ` (-S) \<subseteq> {0}"
  7.2697 -  and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<omega>"
  7.2698 +  and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<infinity>"
  7.2699    and abs_summable: "summable (\<lambda>r. \<bar>enum r * real (\<mu> (f -` {enum r} \<inter> space M))\<bar>)"
  7.2700    shows "integrable M f"
  7.2701    and "(\<lambda>r. enum r * real (\<mu> (f -` {enum r} \<inter> space M))) sums integral\<^isup>L M f" (is ?sums)
  7.2702 @@ -2303,7 +2430,7 @@
  7.2703      also have "\<dots> = \<bar>enum r\<bar> * real (\<mu> (?A r))"
  7.2704        using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
  7.2705      finally have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = \<bar>enum r * real (\<mu> (?A r))\<bar>"
  7.2706 -      by (simp add: abs_mult_pos real_pextreal_pos) }
  7.2707 +      using f by (subst (2) abs_mult_pos[symmetric]) (auto intro!: real_of_extreal_pos measurable_sets) }
  7.2708    note int_abs_F = this
  7.2709  
  7.2710    have 1: "\<And>i. integrable M (\<lambda>x. ?F i x)"
  7.2711 @@ -2323,7 +2450,7 @@
  7.2712  
  7.2713  lemma (in measure_space) integral_on_finite:
  7.2714    assumes f: "f \<in> borel_measurable M" and finite: "finite (f`space M)"
  7.2715 -  and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<omega>"
  7.2716 +  and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> \<mu> (f -` {x} \<inter> space M) \<noteq> \<infinity>"
  7.2717    shows "integrable M f"
  7.2718    and "(\<integral>x. f x \<partial>M) =
  7.2719      (\<Sum> r \<in> f`space M. r * real (\<mu> (f -` {r} \<inter> space M)))" (is "?integral")
  7.2720 @@ -2353,11 +2480,12 @@
  7.2721    by (auto intro: borel_measurable_simple_function)
  7.2722  
  7.2723  lemma (in finite_measure_space) positive_integral_finite_eq_setsum:
  7.2724 -  "integral\<^isup>P M f = (\<Sum>x \<in> space M. f x * \<mu> {x})"
  7.2725 +  assumes pos: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  7.2726 +  shows "integral\<^isup>P M f = (\<Sum>x \<in> space M. f x * \<mu> {x})"
  7.2727  proof -
  7.2728    have *: "integral\<^isup>P M f = (\<integral>\<^isup>+ x. (\<Sum>y\<in>space M. f y * indicator {y} x) \<partial>M)"
  7.2729      by (auto intro!: positive_integral_cong simp add: indicator_def if_distrib setsum_cases[OF finite_space])
  7.2730 -  show ?thesis unfolding * using borel_measurable_finite[of f]
  7.2731 +  show ?thesis unfolding * using borel_measurable_finite[of f] pos
  7.2732      by (simp add: positive_integral_setsum positive_integral_cmult_indicator)
  7.2733  qed
  7.2734  
  7.2735 @@ -2365,16 +2493,20 @@
  7.2736    shows "integrable M f"
  7.2737    and "integral\<^isup>L M f = (\<Sum>x \<in> space M. f x * real (\<mu> {x}))" (is ?I)
  7.2738  proof -
  7.2739 -  have [simp]:
  7.2740 -    "(\<integral>\<^isup>+ x. Real (f x) \<partial>M) = (\<Sum>x \<in> space M. Real (f x) * \<mu> {x})"
  7.2741 -    "(\<integral>\<^isup>+ x. Real (- f x) \<partial>M) = (\<Sum>x \<in> space M. Real (- f x) * \<mu> {x})"
  7.2742 -    unfolding positive_integral_finite_eq_setsum by auto
  7.2743 -  show "integrable M f" using finite_space finite_measure
  7.2744 -    by (simp add: setsum_\<omega> integrable_def)
  7.2745 -  show ?I using finite_measure
  7.2746 -    apply (simp add: lebesgue_integral_def real_of_pextreal_setsum[symmetric]
  7.2747 -      real_of_pextreal_mult[symmetric] setsum_subtractf[symmetric])
  7.2748 -    by (rule setsum_cong) (simp_all split: split_if)
  7.2749 +  have *:
  7.2750 +    "(\<integral>\<^isup>+ x. max 0 (extreal (f x)) \<partial>M) = (\<Sum>x \<in> space M. max 0 (extreal (f x)) * \<mu> {x})"
  7.2751 +    "(\<integral>\<^isup>+ x. max 0 (extreal (- f x)) \<partial>M) = (\<Sum>x \<in> space M. max 0 (extreal (- f x)) * \<mu> {x})"
  7.2752 +    by (simp_all add: positive_integral_finite_eq_setsum)
  7.2753 +  then show "integrable M f" using finite_space finite_measure
  7.2754 +    by (simp add: setsum_Pinfty integrable_def positive_integral_max_0
  7.2755 +             split: split_max)
  7.2756 +  show ?I using finite_measure *
  7.2757 +    apply (simp add: positive_integral_max_0 lebesgue_integral_def)
  7.2758 +    apply (subst (1 2) setsum_real_of_extreal[symmetric])
  7.2759 +    apply (simp_all split: split_max add: setsum_subtractf[symmetric])
  7.2760 +    apply (intro setsum_cong[OF refl])
  7.2761 +    apply (simp split: split_max)
  7.2762 +    done
  7.2763  qed
  7.2764  
  7.2765  end
     8.1 --- a/src/HOL/Probability/Lebesgue_Measure.thy	Mon Mar 14 14:37:47 2011 +0100
     8.2 +++ b/src/HOL/Probability/Lebesgue_Measure.thy	Mon Mar 14 14:37:49 2011 +0100
     8.3 @@ -48,12 +48,12 @@
     8.4  lemma Pi_iff: "f \<in> Pi I X \<longleftrightarrow> (\<forall>i\<in>I. f i \<in> X i)"
     8.5    unfolding Pi_def by auto
     8.6  
     8.7 -subsection {* Lebesgue measure *}
     8.8 +subsection {* Lebesgue measure *} 
     8.9  
    8.10  definition lebesgue :: "'a::ordered_euclidean_space measure_space" where
    8.11    "lebesgue = \<lparr> space = UNIV,
    8.12      sets = {A. \<forall>n. (indicator A :: 'a \<Rightarrow> real) integrable_on cube n},
    8.13 -    measure = \<lambda>A. SUP n. Real (integral (cube n) (indicator A)) \<rparr>"
    8.14 +    measure = \<lambda>A. SUP n. extreal (integral (cube n) (indicator A)) \<rparr>"
    8.15  
    8.16  lemma space_lebesgue[simp]: "space lebesgue = UNIV"
    8.17    unfolding lebesgue_def by simp
    8.18 @@ -114,10 +114,33 @@
    8.19    qed (auto intro: LIMSEQ_indicator_UN simp: cube_def)
    8.20  qed simp
    8.21  
    8.22 +lemma suminf_SUP_eq:
    8.23 +  fixes f :: "nat \<Rightarrow> nat \<Rightarrow> extreal"
    8.24 +  assumes "\<And>i. incseq (\<lambda>n. f n i)" "\<And>n i. 0 \<le> f n i"
    8.25 +  shows "(\<Sum>i. SUP n. f n i) = (SUP n. \<Sum>i. f n i)"
    8.26 +proof -
    8.27 +  { fix n :: nat
    8.28 +    have "(\<Sum>i<n. SUP k. f k i) = (SUP k. \<Sum>i<n. f k i)"
    8.29 +      using assms by (auto intro!: SUPR_extreal_setsum[symmetric]) }
    8.30 +  note * = this
    8.31 +  show ?thesis using assms
    8.32 +    apply (subst (1 2) suminf_extreal_eq_SUPR)
    8.33 +    unfolding *
    8.34 +    apply (auto intro!: le_SUPI2)
    8.35 +    apply (subst SUP_commute) ..
    8.36 +qed
    8.37 +
    8.38  interpretation lebesgue: measure_space lebesgue
    8.39  proof
    8.40    have *: "indicator {} = (\<lambda>x. 0 :: real)" by (simp add: fun_eq_iff)
    8.41 -  show "measure lebesgue {} = 0" by (simp add: integral_0 * lebesgue_def)
    8.42 +  show "positive lebesgue (measure lebesgue)"
    8.43 +  proof (unfold positive_def, safe)
    8.44 +    show "measure lebesgue {} = 0" by (simp add: integral_0 * lebesgue_def)
    8.45 +    fix A assume "A \<in> sets lebesgue"
    8.46 +    then show "0 \<le> measure lebesgue A"
    8.47 +      unfolding lebesgue_def
    8.48 +      by (auto intro!: le_SUPI2 integral_nonneg)
    8.49 +  qed
    8.50  next
    8.51    show "countably_additive lebesgue (measure lebesgue)"
    8.52    proof (intro countably_additive_def[THEN iffD2] allI impI)
    8.53 @@ -130,23 +153,17 @@
    8.54      assume "(\<Union>i. A i) \<in> sets lebesgue"
    8.55      then have UN_A[simp, intro]: "\<And>i n. (indicator (\<Union>i. A i) :: _ \<Rightarrow> real) integrable_on cube n"
    8.56        by (auto dest: lebesgueD)
    8.57 -    show "(\<Sum>\<^isub>\<infinity>n. measure lebesgue (A n)) = measure lebesgue (\<Union>i. A i)"
    8.58 -    proof (simp add: lebesgue_def, subst psuminf_SUP_eq)
    8.59 -      fix n i show "Real (?m n i) \<le> Real (?m (Suc n) i)"
    8.60 -        using cube_subset[of n "Suc n"] by (auto intro!: integral_subset_le)
    8.61 +    show "(\<Sum>n. measure lebesgue (A n)) = measure lebesgue (\<Union>i. A i)"
    8.62 +    proof (simp add: lebesgue_def, subst suminf_SUP_eq, safe intro!: incseq_SucI)
    8.63 +      fix i n show "extreal (?m n i) \<le> extreal (?m (Suc n) i)"
    8.64 +        using cube_subset[of n "Suc n"] by (auto intro!: integral_subset_le incseq_SucI)
    8.65      next
    8.66 -      show "(SUP n. \<Sum>\<^isub>\<infinity>i. Real (?m n i)) = (SUP n. Real (?M n UNIV))"
    8.67 -        unfolding psuminf_def
    8.68 -      proof (subst setsum_Real, (intro arg_cong[where f="SUPR UNIV"] ext ballI nn SUP_eq_LIMSEQ[THEN iffD2])+)
    8.69 -        fix n :: nat show "mono (\<lambda>m. \<Sum>x<m. ?m n x)"
    8.70 -        proof (intro mono_iff_le_Suc[THEN iffD2] allI)
    8.71 -          fix m show "(\<Sum>x<m. ?m n x) \<le> (\<Sum>x<Suc m. ?m n x)"
    8.72 -            using nn[of n m] by auto
    8.73 -        qed
    8.74 -        show "0 \<le> ?M n UNIV"
    8.75 -          using UN_A by (auto intro!: integral_nonneg)
    8.76 -        fix m show "0 \<le> (\<Sum>x<m. ?m n x)" by (auto intro!: setsum_nonneg)
    8.77 -      next
    8.78 +      fix i n show "0 \<le> extreal (?m n i)"
    8.79 +        using rA unfolding lebesgue_def
    8.80 +        by (auto intro!: le_SUPI2 integral_nonneg)
    8.81 +    next
    8.82 +      show "(SUP n. \<Sum>i. extreal (?m n i)) = (SUP n. extreal (?M n UNIV))"
    8.83 +      proof (intro arg_cong[where f="SUPR UNIV"] ext sums_unique[symmetric] sums_extreal[THEN iffD2] sums_def[THEN iffD2])
    8.84          fix n
    8.85          have "\<And>m. (UNION {..<m} A) \<in> sets lebesgue" using rA by auto
    8.86          from lebesgueD[OF this]
    8.87 @@ -171,8 +188,8 @@
    8.88              ultimately show ?case
    8.89                using Suc A by (simp add: integral_add[symmetric])
    8.90            qed auto }
    8.91 -        ultimately show "(\<lambda>m. \<Sum>x<m. ?m n x) ----> ?M n UNIV"
    8.92 -          by simp
    8.93 +        ultimately show "(\<lambda>m. \<Sum>x = 0..<m. ?m n x) ----> ?M n UNIV"
    8.94 +          by (simp add: atLeast0LessThan)
    8.95        qed
    8.96      qed
    8.97    qed
    8.98 @@ -232,13 +249,11 @@
    8.99  
   8.100  lemma lmeasure_iff_LIMSEQ:
   8.101    assumes "A \<in> sets lebesgue" "0 \<le> m"
   8.102 -  shows "lebesgue.\<mu> A = Real m \<longleftrightarrow> (\<lambda>n. integral (cube n) (indicator A :: _ \<Rightarrow> real)) ----> m"
   8.103 +  shows "lebesgue.\<mu> A = extreal m \<longleftrightarrow> (\<lambda>n. integral (cube n) (indicator A :: _ \<Rightarrow> real)) ----> m"
   8.104  proof (simp add: lebesgue_def, intro SUP_eq_LIMSEQ)
   8.105    show "mono (\<lambda>n. integral (cube n) (indicator A::_=>real))"
   8.106      using cube_subset assms by (intro monoI integral_subset_le) (auto dest!: lebesgueD)
   8.107 -  fix n show "0 \<le> integral (cube n) (indicator A::_=>real)"
   8.108 -    using assms by (auto dest!: lebesgueD intro!: integral_nonneg)
   8.109 -qed fact
   8.110 +qed
   8.111  
   8.112  lemma has_integral_indicator_UNIV:
   8.113    fixes s A :: "'a::ordered_euclidean_space set" and x :: real
   8.114 @@ -260,7 +275,7 @@
   8.115  
   8.116  lemma lmeasure_finite_has_integral:
   8.117    fixes s :: "'a::ordered_euclidean_space set"
   8.118 -  assumes "s \<in> sets lebesgue" "lebesgue.\<mu> s = Real m" "0 \<le> m"
   8.119 +  assumes "s \<in> sets lebesgue" "lebesgue.\<mu> s = extreal m" "0 \<le> m"
   8.120    shows "(indicator s has_integral m) UNIV"
   8.121  proof -
   8.122    let ?I = "indicator :: 'a set \<Rightarrow> 'a \<Rightarrow> real"
   8.123 @@ -302,12 +317,14 @@
   8.124      unfolding m by (intro integrable_integral **)
   8.125  qed
   8.126  
   8.127 -lemma lmeasure_finite_integrable: assumes "s \<in> sets lebesgue" "lebesgue.\<mu> s \<noteq> \<omega>"
   8.128 +lemma lmeasure_finite_integrable: assumes s: "s \<in> sets lebesgue" and "lebesgue.\<mu> s \<noteq> \<infinity>"
   8.129    shows "(indicator s :: _ \<Rightarrow> real) integrable_on UNIV"
   8.130  proof (cases "lebesgue.\<mu> s")
   8.131 -  case (preal m) from lmeasure_finite_has_integral[OF `s \<in> sets lebesgue` this]
   8.132 +  case (real m)
   8.133 +  with lmeasure_finite_has_integral[OF `s \<in> sets lebesgue` this]
   8.134 +    lebesgue.positive_measure[OF s]
   8.135    show ?thesis unfolding integrable_on_def by auto
   8.136 -qed (insert assms, auto)
   8.137 +qed (insert assms lebesgue.positive_measure[OF s], auto)
   8.138  
   8.139  lemma has_integral_lebesgue: assumes "((indicator s :: _\<Rightarrow>real) has_integral m) UNIV"
   8.140    shows "s \<in> sets lebesgue"
   8.141 @@ -321,7 +338,7 @@
   8.142  qed
   8.143  
   8.144  lemma has_integral_lmeasure: assumes "((indicator s :: _\<Rightarrow>real) has_integral m) UNIV"
   8.145 -  shows "lebesgue.\<mu> s = Real m"
   8.146 +  shows "lebesgue.\<mu> s = extreal m"
   8.147  proof (intro lmeasure_iff_LIMSEQ[THEN iffD2])
   8.148    let ?I = "indicator :: 'a set \<Rightarrow> 'a \<Rightarrow> real"
   8.149    show "s \<in> sets lebesgue" using has_integral_lebesgue[OF assms] .
   8.150 @@ -346,28 +363,28 @@
   8.151  qed
   8.152  
   8.153  lemma has_integral_iff_lmeasure:
   8.154 -  "(indicator A has_integral m) UNIV \<longleftrightarrow> (A \<in> sets lebesgue \<and> 0 \<le> m \<and> lebesgue.\<mu> A = Real m)"
   8.155 +  "(indicator A has_integral m) UNIV \<longleftrightarrow> (A \<in> sets lebesgue \<and> 0 \<le> m \<and> lebesgue.\<mu> A = extreal m)"
   8.156  proof
   8.157    assume "(indicator A has_integral m) UNIV"
   8.158    with has_integral_lmeasure[OF this] has_integral_lebesgue[OF this]
   8.159 -  show "A \<in> sets lebesgue \<and> 0 \<le> m \<and> lebesgue.\<mu> A = Real m"
   8.160 +  show "A \<in> sets lebesgue \<and> 0 \<le> m \<and> lebesgue.\<mu> A = extreal m"
   8.161      by (auto intro: has_integral_nonneg)
   8.162  next
   8.163 -  assume "A \<in> sets lebesgue \<and> 0 \<le> m \<and> lebesgue.\<mu> A = Real m"
   8.164 +  assume "A \<in> sets lebesgue \<and> 0 \<le> m \<and> lebesgue.\<mu> A = extreal m"
   8.165    then show "(indicator A has_integral m) UNIV" by (intro lmeasure_finite_has_integral) auto
   8.166  qed
   8.167  
   8.168  lemma lmeasure_eq_integral: assumes "(indicator s::_\<Rightarrow>real) integrable_on UNIV"
   8.169 -  shows "lebesgue.\<mu> s = Real (integral UNIV (indicator s))"
   8.170 +  shows "lebesgue.\<mu> s = extreal (integral UNIV (indicator s))"
   8.171    using assms unfolding integrable_on_def
   8.172  proof safe
   8.173    fix y :: real assume "(indicator s has_integral y) UNIV"
   8.174    from this[unfolded has_integral_iff_lmeasure] integral_unique[OF this]
   8.175 -  show "lebesgue.\<mu> s = Real (integral UNIV (indicator s))" by simp
   8.176 +  show "lebesgue.\<mu> s = extreal (integral UNIV (indicator s))" by simp
   8.177  qed
   8.178  
   8.179  lemma lebesgue_simple_function_indicator:
   8.180 -  fixes f::"'a::ordered_euclidean_space \<Rightarrow> pextreal"
   8.181 +  fixes f::"'a::ordered_euclidean_space \<Rightarrow> extreal"
   8.182    assumes f:"simple_function lebesgue f"
   8.183    shows "f = (\<lambda>x. (\<Sum>y \<in> f ` UNIV. y * indicator (f -` {y}) x))"
   8.184    by (rule, subst lebesgue.simple_function_indicator_representation[OF f]) auto
   8.185 @@ -376,7 +393,7 @@
   8.186    "(indicator s::_\<Rightarrow>real) integrable_on UNIV \<Longrightarrow> integral UNIV (indicator s) = real (lebesgue.\<mu> s)"
   8.187    by (subst lmeasure_eq_integral) (auto intro!: integral_nonneg)
   8.188  
   8.189 -lemma lmeasure_finite: assumes "(indicator s::_\<Rightarrow>real) integrable_on UNIV" shows "lebesgue.\<mu> s \<noteq> \<omega>"
   8.190 +lemma lmeasure_finite: assumes "(indicator s::_\<Rightarrow>real) integrable_on UNIV" shows "lebesgue.\<mu> s \<noteq> \<infinity>"
   8.191    using lmeasure_eq_integral[OF assms] by auto
   8.192  
   8.193  lemma negligible_iff_lebesgue_null_sets:
   8.194 @@ -409,37 +426,29 @@
   8.195    shows "integral {a .. b} (\<lambda>x. c) = content {a .. b} *\<^sub>R c"
   8.196    by (rule integral_unique) (rule has_integral_const)
   8.197  
   8.198 -lemma lmeasure_UNIV[intro]: "lebesgue.\<mu> (UNIV::'a::ordered_euclidean_space set) = \<omega>"
   8.199 -proof (simp add: lebesgue_def SUP_\<omega>, intro allI impI)
   8.200 -  fix x assume "x < \<omega>"
   8.201 -  then obtain r where r: "x = Real r" "0 \<le> r" by (cases x) auto
   8.202 -  then obtain n where n: "r < of_nat n" using ex_less_of_nat by auto
   8.203 -  show "\<exists>i. x < Real (integral (cube i) (indicator UNIV::'a\<Rightarrow>real))"
   8.204 -  proof (intro exI[of _ n])
   8.205 -    have [simp]: "indicator UNIV = (\<lambda>x. 1)" by (auto simp: fun_eq_iff)
   8.206 -    { fix m :: nat assume "0 < m" then have "real n \<le> (\<Prod>x<m. 2 * real n)"
   8.207 -      proof (induct m)
   8.208 -        case (Suc m)
   8.209 -        show ?case
   8.210 -        proof cases
   8.211 -          assume "m = 0" then show ?thesis by (simp add: lessThan_Suc)
   8.212 -        next
   8.213 -          assume "m \<noteq> 0" then have "real n \<le> (\<Prod>x<m. 2 * real n)" using Suc by auto
   8.214 -          then show ?thesis
   8.215 -            by (auto simp: lessThan_Suc field_simps mult_le_cancel_left1)
   8.216 -        qed
   8.217 -      qed auto } note this[OF DIM_positive[where 'a='a], simp]
   8.218 -    then have [simp]: "0 \<le> (\<Prod>x<DIM('a). 2 * real n)" using real_of_nat_ge_zero by arith
   8.219 -    have "x < Real (of_nat n)" using n r by auto
   8.220 -    also have "Real (of_nat n) \<le> Real (integral (cube n) (indicator UNIV::'a\<Rightarrow>real))"
   8.221 -      by (auto simp: real_eq_of_nat[symmetric] cube_def content_closed_interval_cases)
   8.222 -    finally show "x < Real (integral (cube n) (indicator UNIV::'a\<Rightarrow>real))" .
   8.223 -  qed
   8.224 -qed
   8.225 +lemma lmeasure_UNIV[intro]: "lebesgue.\<mu> (UNIV::'a::ordered_euclidean_space set) = \<infinity>"
   8.226 +proof (simp add: lebesgue_def, intro SUP_PInfty bexI)
   8.227 +  fix n :: nat
   8.228 +  have "indicator UNIV = (\<lambda>x::'a. 1 :: real)" by auto
   8.229 +  moreover
   8.230 +  { have "real n \<le> (2 * real n) ^ DIM('a)"
   8.231 +    proof (cases n)
   8.232 +      case 0 then show ?thesis by auto
   8.233 +    next
   8.234 +      case (Suc n')
   8.235 +      have "real n \<le> (2 * real n)^1" by auto
   8.236 +      also have "(2 * real n)^1 \<le> (2 * real n) ^ DIM('a)"
   8.237 +        using Suc DIM_positive[where 'a='a] by (intro power_increasing) (auto simp: real_of_nat_Suc)
   8.238 +      finally show ?thesis .
   8.239 +    qed }
   8.240 +  ultimately show "extreal (real n) \<le> extreal (integral (cube n) (indicator UNIV::'a\<Rightarrow>real))"
   8.241 +    using integral_const DIM_positive[where 'a='a]
   8.242 +    by (auto simp: cube_def content_closed_interval_cases setprod_constant)
   8.243 +qed simp
   8.244  
   8.245  lemma
   8.246    fixes a b ::"'a::ordered_euclidean_space"
   8.247 -  shows lmeasure_atLeastAtMost[simp]: "lebesgue.\<mu> {a..b} = Real (content {a..b})"
   8.248 +  shows lmeasure_atLeastAtMost[simp]: "lebesgue.\<mu> {a..b} = extreal (content {a..b})"
   8.249  proof -
   8.250    have "(indicator (UNIV \<inter> {a..b})::_\<Rightarrow>real) integrable_on UNIV"
   8.251      unfolding integrable_indicator_UNIV by (simp add: integrable_const indicator_def_raw)
   8.252 @@ -467,7 +476,7 @@
   8.253  lemma
   8.254    fixes a b :: real
   8.255    shows lmeasure_real_greaterThanAtMost[simp]:
   8.256 -    "lebesgue.\<mu> {a <.. b} = Real (if a \<le> b then b - a else 0)"
   8.257 +    "lebesgue.\<mu> {a <.. b} = extreal (if a \<le> b then b - a else 0)"
   8.258  proof cases
   8.259    assume "a < b"
   8.260    then have "lebesgue.\<mu> {a <.. b} = lebesgue.\<mu> {a .. b} - lebesgue.\<mu> {a}"
   8.261 @@ -479,7 +488,7 @@
   8.262  lemma
   8.263    fixes a b :: real
   8.264    shows lmeasure_real_atLeastLessThan[simp]:
   8.265 -    "lebesgue.\<mu> {a ..< b} = Real (if a \<le> b then b - a else 0)"
   8.266 +    "lebesgue.\<mu> {a ..< b} = extreal (if a \<le> b then b - a else 0)"
   8.267  proof cases
   8.268    assume "a < b"
   8.269    then have "lebesgue.\<mu> {a ..< b} = lebesgue.\<mu> {a .. b} - lebesgue.\<mu> {b}"
   8.270 @@ -491,7 +500,7 @@
   8.271  lemma
   8.272    fixes a b :: real
   8.273    shows lmeasure_real_greaterThanLessThan[simp]:
   8.274 -    "lebesgue.\<mu> {a <..< b} = Real (if a \<le> b then b - a else 0)"
   8.275 +    "lebesgue.\<mu> {a <..< b} = extreal (if a \<le> b then b - a else 0)"
   8.276  proof cases
   8.277    assume "a < b"
   8.278    then have "lebesgue.\<mu> {a <..< b} = lebesgue.\<mu> {a <.. b} - lebesgue.\<mu> {b}"
   8.279 @@ -511,19 +520,16 @@
   8.280    and measurable_lborel[simp]: "measurable lborel = measurable borel"
   8.281    by (simp_all add: measurable_def_raw lborel_def)
   8.282  
   8.283 -interpretation lborel: measure_space lborel
   8.284 +interpretation lborel: measure_space "lborel :: ('a::ordered_euclidean_space) measure_space"
   8.285    where "space lborel = UNIV"
   8.286    and "sets lborel = sets borel"
   8.287    and "measure lborel = lebesgue.\<mu>"
   8.288    and "measurable lborel = measurable borel"
   8.289 -proof -
   8.290 -  show "measure_space lborel"
   8.291 -  proof
   8.292 -    show "countably_additive lborel (measure lborel)"
   8.293 -      using lebesgue.ca unfolding countably_additive_def lborel_def
   8.294 -      apply safe apply (erule_tac x=A in allE) by auto
   8.295 -  qed (auto simp: lborel_def)
   8.296 -qed simp_all
   8.297 +proof (rule lebesgue.measure_space_subalgebra)
   8.298 +  have "sigma_algebra (lborel::'a measure_space) \<longleftrightarrow> sigma_algebra (borel::'a algebra)"
   8.299 +    unfolding sigma_algebra_iff2 lborel_def by simp
   8.300 +  then show "sigma_algebra (lborel::'a measure_space)" by simp default
   8.301 +qed auto
   8.302  
   8.303  interpretation lborel: sigma_finite_measure lborel
   8.304    where "space lborel = UNIV"
   8.305 @@ -536,7 +542,7 @@
   8.306      show "range cube \<subseteq> sets lborel" by (auto intro: borel_closed)
   8.307      { fix x have "\<exists>n. x\<in>cube n" using mem_big_cube by auto }
   8.308      thus "(\<Union>i. cube i) = space lborel" by auto
   8.309 -    show "\<forall>i. measure lborel (cube i) \<noteq> \<omega>" by (simp add: cube_def)
   8.310 +    show "\<forall>i. measure lborel (cube i) \<noteq> \<infinity>" by (simp add: cube_def)
   8.311    qed
   8.312  qed simp_all
   8.313  
   8.314 @@ -544,171 +550,221 @@
   8.315  proof
   8.316    from lborel.sigma_finite guess A ..
   8.317    moreover then have "range A \<subseteq> sets lebesgue" using lebesgueI_borel by blast
   8.318 -  ultimately show "\<exists>A::nat \<Rightarrow> 'b set. range A \<subseteq> sets lebesgue \<and> (\<Union>i. A i) = space lebesgue \<and> (\<forall>i. lebesgue.\<mu> (A i) \<noteq> \<omega>)"
   8.319 +  ultimately show "\<exists>A::nat \<Rightarrow> 'b set. range A \<subseteq> sets lebesgue \<and> (\<Union>i. A i) = space lebesgue \<and> (\<forall>i. lebesgue.\<mu> (A i) \<noteq> \<infinity>)"
   8.320      by auto
   8.321  qed
   8.322  
   8.323  subsection {* Lebesgue integrable implies Gauge integrable *}
   8.324  
   8.325 +lemma positive_not_Inf:
   8.326 +  "0 \<le> x \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> \<bar>x\<bar> \<noteq> \<infinity>"
   8.327 +  by (cases x) auto
   8.328 +
   8.329 +lemma has_integral_cmult_real:
   8.330 +  fixes c :: real
   8.331 +  assumes "c \<noteq> 0 \<Longrightarrow> (f has_integral x) A"
   8.332 +  shows "((\<lambda>x. c * f x) has_integral c * x) A"
   8.333 +proof cases
   8.334 +  assume "c \<noteq> 0"
   8.335 +  from has_integral_cmul[OF assms[OF this], of c] show ?thesis
   8.336 +    unfolding real_scaleR_def .
   8.337 +qed simp
   8.338 +
   8.339  lemma simple_function_has_integral:
   8.340 -  fixes f::"'a::ordered_euclidean_space \<Rightarrow> pextreal"
   8.341 +  fixes f::"'a::ordered_euclidean_space \<Rightarrow> extreal"
   8.342    assumes f:"simple_function lebesgue f"
   8.343 -  and f':"\<forall>x. f x \<noteq> \<omega>"
   8.344 -  and om:"\<forall>x\<in>range f. lebesgue.\<mu> (f -` {x} \<inter> UNIV) = \<omega> \<longrightarrow> x = 0"
   8.345 +  and f':"range f \<subseteq> {0..<\<infinity>}"
   8.346 +  and om:"\<And>x. x \<in> range f \<Longrightarrow> lebesgue.\<mu> (f -` {x} \<inter> UNIV) = \<infinity> \<Longrightarrow> x = 0"
   8.347    shows "((\<lambda>x. real (f x)) has_integral (real (integral\<^isup>S lebesgue f))) UNIV"
   8.348 -  unfolding simple_integral_def
   8.349 -  apply(subst lebesgue_simple_function_indicator[OF f])
   8.350 -proof -
   8.351 -  case goal1
   8.352 -  have *:"\<And>x. \<forall>y\<in>range f. y * indicator (f -` {y}) x \<noteq> \<omega>"
   8.353 -    "\<forall>x\<in>range f. x * lebesgue.\<mu> (f -` {x} \<inter> UNIV) \<noteq> \<omega>"
   8.354 -    using f' om unfolding indicator_def by auto
   8.355 -  show ?case unfolding space_lebesgue real_of_pextreal_setsum'[OF *(1),THEN sym]
   8.356 -    unfolding real_of_pextreal_setsum'[OF *(2),THEN sym]
   8.357 -    unfolding real_of_pextreal_setsum space_lebesgue
   8.358 -    apply(rule has_integral_setsum)
   8.359 -  proof safe show "finite (range f)" using f by (auto dest: lebesgue.simple_functionD)
   8.360 -    fix y::'a show "((\<lambda>x. real (f y * indicator (f -` {f y}) x)) has_integral
   8.361 -      real (f y * lebesgue.\<mu> (f -` {f y} \<inter> UNIV))) UNIV"
   8.362 -    proof(cases "f y = 0") case False
   8.363 -      have mea:"(indicator (f -` {f y}) ::_\<Rightarrow>real) integrable_on UNIV"
   8.364 -        apply(rule lmeasure_finite_integrable)
   8.365 -        using assms unfolding simple_function_def using False by auto
   8.366 -      have *:"\<And>x. real (indicator (f -` {f y}) x::pextreal) = (indicator (f -` {f y}) x)"
   8.367 -        by (auto simp: indicator_def)
   8.368 -      show ?thesis unfolding real_of_pextreal_mult[THEN sym]
   8.369 -        apply(rule has_integral_cmul[where 'b=real, unfolded real_scaleR_def])
   8.370 -        unfolding Int_UNIV_right lmeasure_eq_integral[OF mea,THEN sym]
   8.371 -        unfolding integral_eq_lmeasure[OF mea, symmetric] *
   8.372 -        apply(rule integrable_integral) using mea .
   8.373 -    qed auto
   8.374 +  unfolding simple_integral_def space_lebesgue
   8.375 +proof (subst lebesgue_simple_function_indicator)
   8.376 +  let "?M x" = "lebesgue.\<mu> (f -` {x} \<inter> UNIV)"
   8.377 +  let "?F x" = "indicator (f -` {x})"
   8.378 +  { fix x y assume "y \<in> range f"
   8.379 +    from subsetD[OF f' this] have "y * ?F y x = extreal (real y * ?F y x)"
   8.380 +      by (cases rule: extreal2_cases[of y "?F y x"])
   8.381 +         (auto simp: indicator_def one_extreal_def split: split_if_asm) }
   8.382 +  moreover
   8.383 +  { fix x assume x: "x\<in>range f"
   8.384 +    have "x * ?M x = real x * real (?M x)"
   8.385 +    proof cases
   8.386 +      assume "x \<noteq> 0" with om[OF x] have "?M x \<noteq> \<infinity>" by auto
   8.387 +      with subsetD[OF f' x] f[THEN lebesgue.simple_functionD(2)] show ?thesis
   8.388 +        by (cases rule: extreal2_cases[of x "?M x"]) auto
   8.389 +    qed simp }
   8.390 +  ultimately
   8.391 +  have "((\<lambda>x. real (\<Sum>y\<in>range f. y * ?F y x)) has_integral real (\<Sum>x\<in>range f. x * ?M x)) UNIV \<longleftrightarrow>
   8.392 +    ((\<lambda>x. \<Sum>y\<in>range f. real y * ?F y x) has_integral (\<Sum>x\<in>range f. real x * real (?M x))) UNIV"
   8.393 +    by simp
   8.394 +  also have \<dots>
   8.395 +  proof (intro has_integral_setsum has_integral_cmult_real lmeasure_finite_has_integral
   8.396 +               real_of_extreal_pos lebesgue.positive_measure ballI)
   8.397 +    show *: "finite (range f)" "\<And>y. f -` {y} \<in> sets lebesgue" "\<And>y. f -` {y} \<inter> UNIV \<in> sets lebesgue"
   8.398 +      using lebesgue.simple_functionD[OF f] by auto
   8.399 +    fix y assume "real y \<noteq> 0" "y \<in> range f"
   8.400 +    with * om[OF this(2)] show "lebesgue.\<mu> (f -` {y}) = extreal (real (?M y))"
   8.401 +      by (auto simp: extreal_real)
   8.402    qed
   8.403 -qed
   8.404 +  finally show "((\<lambda>x. real (\<Sum>y\<in>range f. y * ?F y x)) has_integral real (\<Sum>x\<in>range f. x * ?M x)) UNIV" .
   8.405 +qed fact
   8.406  
   8.407  lemma bounded_realI: assumes "\<forall>x\<in>s. abs (x::real) \<le> B" shows "bounded s"
   8.408    unfolding bounded_def dist_real_def apply(rule_tac x=0 in exI)
   8.409    using assms by auto
   8.410  
   8.411  lemma simple_function_has_integral':
   8.412 -  fixes f::"'a::ordered_euclidean_space \<Rightarrow> pextreal"
   8.413 -  assumes f:"simple_function lebesgue f"
   8.414 -  and i: "integral\<^isup>S lebesgue f \<noteq> \<omega>"
   8.415 +  fixes f::"'a::ordered_euclidean_space \<Rightarrow> extreal"
   8.416 +  assumes f: "simple_function lebesgue f" "\<And>x. 0 \<le> f x"
   8.417 +  and i: "integral\<^isup>S lebesgue f \<noteq> \<infinity>"
   8.418    shows "((\<lambda>x. real (f x)) has_integral (real (integral\<^isup>S lebesgue f))) UNIV"
   8.419 -proof- let ?f = "\<lambda>x. if f x = \<omega> then 0 else f x"
   8.420 -  { fix x have "real (f x) = real (?f x)" by (cases "f x") auto } note * = this
   8.421 -  have **:"{x. f x \<noteq> ?f x} = f -` {\<omega>}" by auto
   8.422 -  have **:"lebesgue.\<mu> {x\<in>space lebesgue. f x \<noteq> ?f x} = 0"
   8.423 -    using lebesgue.simple_integral_omega[OF assms] by(auto simp add:**)
   8.424 -  show ?thesis apply(subst lebesgue.simple_integral_cong'[OF f _ **])
   8.425 -    apply(rule lebesgue.simple_function_compose1[OF f])
   8.426 -    unfolding * defer apply(rule simple_function_has_integral)
   8.427 -  proof-
   8.428 -    show "simple_function lebesgue ?f"
   8.429 -      using lebesgue.simple_function_compose1[OF f] .
   8.430 -    show "\<forall>x. ?f x \<noteq> \<omega>" by auto
   8.431 -    show "\<forall>x\<in>range ?f. lebesgue.\<mu> (?f -` {x} \<inter> UNIV) = \<omega> \<longrightarrow> x = 0"
   8.432 -    proof (safe, simp, safe, rule ccontr)
   8.433 -      fix y assume "f y \<noteq> \<omega>" "f y \<noteq> 0"
   8.434 -      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.435 -        by (auto split: split_if_asm)
   8.436 -      moreover assume "lebesgue.\<mu> ((\<lambda>x. if f x = \<omega> then 0 else f x) -` {if f y = \<omega> then 0 else f y}) = \<omega>"
   8.437 -      ultimately have "lebesgue.\<mu> (f -` {f y}) = \<omega>" by simp
   8.438 -      moreover
   8.439 -      have "f y * lebesgue.\<mu> (f -` {f y}) \<noteq> \<omega>" using i f
   8.440 -        unfolding simple_integral_def setsum_\<omega> simple_function_def
   8.441 -        by auto
   8.442 -      ultimately have "f y = 0" by (auto split: split_if_asm)
   8.443 -      then show False using `f y \<noteq> 0` by simp
   8.444 -    qed
   8.445 +proof -
   8.446 +  let ?f = "\<lambda>x. if x \<in> f -` {\<infinity>} then 0 else f x"
   8.447 +  note f(1)[THEN lebesgue.simple_functionD(2)]
   8.448 +  then have [simp, intro]: "\<And>X. f -` X \<in> sets lebesgue" by auto
   8.449 +  have f': "simple_function lebesgue ?f"
   8.450 +    using f by (intro lebesgue.simple_function_If_set) auto
   8.451 +  have rng: "range ?f \<subseteq> {0..<\<infinity>}" using f by auto
   8.452 +  have "AE x in lebesgue. f x = ?f x"
   8.453 +    using lebesgue.simple_integral_PInf[OF f i]
   8.454 +    by (intro lebesgue.AE_I[where N="f -` {\<infinity>} \<inter> space lebesgue"]) auto
   8.455 +  from f(1) f' this have eq: "integral\<^isup>S lebesgue f = integral\<^isup>S lebesgue ?f"
   8.456 +    by (rule lebesgue.simple_integral_cong_AE)
   8.457 +  have real_eq: "\<And>x. real (f x) = real (?f x)" by auto
   8.458 +
   8.459 +  show ?thesis
   8.460 +    unfolding eq real_eq
   8.461 +  proof (rule simple_function_has_integral[OF f' rng])
   8.462 +    fix x assume x: "x \<in> range ?f" and inf: "lebesgue.\<mu> (?f -` {x} \<inter> UNIV) = \<infinity>"
   8.463 +    have "x * lebesgue.\<mu> (?f -` {x} \<inter> UNIV) = (\<integral>\<^isup>S y. x * indicator (?f -` {x}) y \<partial>lebesgue)"
   8.464 +      using f'[THEN lebesgue.simple_functionD(2)]
   8.465 +      by (simp add: lebesgue.simple_integral_cmult_indicator)
   8.466 +    also have "\<dots> \<le> integral\<^isup>S lebesgue f"
   8.467 +      using f'[THEN lebesgue.simple_functionD(2)] f
   8.468 +      by (intro lebesgue.simple_integral_mono lebesgue.simple_function_mult lebesgue.simple_function_indicator)
   8.469 +         (auto split: split_indicator)
   8.470 +    finally show "x = 0" unfolding inf using i subsetD[OF rng x] by (auto split: split_if_asm)
   8.471    qed
   8.472  qed
   8.473  
   8.474 -lemma (in measure_space) positive_integral_monotone_convergence:
   8.475 -  fixes f::"nat \<Rightarrow> 'a \<Rightarrow> pextreal"
   8.476 -  assumes i: "\<And>i. f i \<in> borel_measurable M" and mono: "\<And>x. mono (\<lambda>n. f n x)"
   8.477 -  and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
   8.478 -  shows "u \<in> borel_measurable M"
   8.479 -  and "(\<lambda>i. integral\<^isup>P M (f i)) ----> integral\<^isup>P M u" (is ?ilim)
   8.480 -proof -
   8.481 -  from positive_integral_isoton[unfolded isoton_fun_expand isoton_iff_Lim_mono, of f u]
   8.482 -  show ?ilim using mono lim i by auto
   8.483 -  have "\<And>x. (SUP i. f i x) = u x" using mono lim SUP_Lim_pextreal
   8.484 -    unfolding fun_eq_iff mono_def by auto
   8.485 -  moreover have "(\<lambda>x. SUP i. f i x) \<in> borel_measurable M"
   8.486 -    using i by auto
   8.487 -  ultimately show "u \<in> borel_measurable M" by simp
   8.488 -qed
   8.489 +lemma real_of_extreal_positive_mono:
   8.490 +  "\<lbrakk>0 \<le> x; x \<le> y; y \<noteq> \<infinity>\<rbrakk> \<Longrightarrow> real x \<le> real y"
   8.491 +  by (cases rule: extreal2_cases[of x y]) auto
   8.492  
   8.493  lemma positive_integral_has_integral:
   8.494 -  fixes f::"'a::ordered_euclidean_space => pextreal"
   8.495 -  assumes f:"f \<in> borel_measurable lebesgue"
   8.496 -  and int_om:"integral\<^isup>P lebesgue f \<noteq> \<omega>"
   8.497 -  and f_om:"\<forall>x. f x \<noteq> \<omega>" (* TODO: remove this *)
   8.498 +  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> extreal"
   8.499 +  assumes f: "f \<in> borel_measurable lebesgue" "range f \<subseteq> {0..<\<infinity>}" "integral\<^isup>P lebesgue f \<noteq> \<infinity>"
   8.500    shows "((\<lambda>x. real (f x)) has_integral (real (integral\<^isup>P lebesgue f))) UNIV"
   8.501 -proof- let ?i = "integral\<^isup>P lebesgue f"
   8.502 -  from lebesgue.borel_measurable_implies_simple_function_sequence[OF f]
   8.503 -  guess u .. note conjunctD2[OF this,rule_format] note u = conjunctD2[OF this(1)] this(2)
   8.504 -  let ?u = "\<lambda>i x. real (u i x)" and ?f = "\<lambda>x. real (f x)"
   8.505 -  have u_simple:"\<And>k. integral\<^isup>S lebesgue (u k) = integral\<^isup>P lebesgue (u k)"
   8.506 -    apply(subst lebesgue.positive_integral_eq_simple_integral[THEN sym,OF u(1)]) ..
   8.507 -  have int_u_le:"\<And>k. integral\<^isup>S lebesgue (u k) \<le> integral\<^isup>P lebesgue f"
   8.508 -    unfolding u_simple apply(rule lebesgue.positive_integral_mono)
   8.509 -    using isoton_Sup[OF u(3)] unfolding le_fun_def by auto
   8.510 -  have u_int_om:"\<And>i. integral\<^isup>S lebesgue (u i) \<noteq> \<omega>"
   8.511 -  proof- case goal1 thus ?case using int_u_le[of i] int_om by auto qed
   8.512 +proof -
   8.513 +  from lebesgue.borel_measurable_implies_simple_function_sequence'[OF f(1)]
   8.514 +  guess u . note u = this
   8.515 +  have SUP_eq: "\<And>x. (SUP i. u i x) = f x"
   8.516 +    using u(4) f(2)[THEN subsetD] by (auto split: split_max)
   8.517 +  let "?u i x" = "real (u i x)"
   8.518 +  note u_eq = lebesgue.positive_integral_eq_simple_integral[OF u(1,5), symmetric]
   8.519 +  { fix i
   8.520 +    note u_eq
   8.521 +    also have "integral\<^isup>P lebesgue (u i) \<le> (\<integral>\<^isup>+x. max 0 (f x) \<partial>lebesgue)"
   8.522 +      by (intro lebesgue.positive_integral_mono) (auto intro: le_SUPI simp: u(4)[symmetric])
   8.523 +    finally have "integral\<^isup>S lebesgue (u i) \<noteq> \<infinity>"
   8.524 +      unfolding positive_integral_max_0 using f by auto }
   8.525 +  note u_fin = this
   8.526 +  then have u_int: "\<And>i. (?u i has_integral real (integral\<^isup>S lebesgue (u i))) UNIV"
   8.527 +    by (rule simple_function_has_integral'[OF u(1,5)])
   8.528 +  have "\<forall>x. \<exists>r\<ge>0. f x = extreal r"
   8.529 +  proof
   8.530 +    fix x from f(2) have "0 \<le> f x" "f x \<noteq> \<infinity>" by (auto simp: subset_eq)
   8.531 +    then show "\<exists>r\<ge>0. f x = extreal r" by (cases "f x") auto
   8.532 +  qed
   8.533 +  from choice[OF this] obtain f' where f': "f = (\<lambda>x. extreal (f' x))" "\<And>x. 0 \<le> f' x" by auto
   8.534 +
   8.535 +  have "\<forall>i. \<exists>r. \<forall>x. 0 \<le> r x \<and> u i x = extreal (r x)"
   8.536 +  proof
   8.537 +    fix i show "\<exists>r. \<forall>x. 0 \<le> r x \<and> u i x = extreal (r x)"
   8.538 +    proof (intro choice allI)
   8.539 +      fix i x have "u i x \<noteq> \<infinity>" using u(3)[of i] by (auto simp: image_iff) metis
   8.540 +      then show "\<exists>r\<ge>0. u i x = extreal r" using u(5)[of i x] by (cases "u i x") auto
   8.541 +    qed
   8.542 +  qed
   8.543 +  from choice[OF this] obtain u' where
   8.544 +      u': "u = (\<lambda>i x. extreal (u' i x))" "\<And>i x. 0 \<le> u' i x" by (auto simp: fun_eq_iff)
   8.545  
   8.546 -  note u_int = simple_function_has_integral'[OF u(1) this]
   8.547 -  have "(\<lambda>x. real (f x)) integrable_on UNIV \<and>
   8.548 -    (\<lambda>k. Integration.integral UNIV (\<lambda>x. real (u k x))) ----> Integration.integral UNIV (\<lambda>x. real (f x))"
   8.549 -    apply(rule monotone_convergence_increasing) apply(rule,rule,rule u_int)
   8.550 -  proof safe case goal1 show ?case apply(rule real_of_pextreal_mono) using u(2,3) by auto
   8.551 -  next case goal2 show ?case using u(3) apply(subst lim_Real[THEN sym])
   8.552 -      prefer 3 apply(subst Real_real') defer apply(subst Real_real')
   8.553 -      using isotone_Lim[OF u(3)[unfolded isoton_fun_expand, THEN spec]] using f_om u by auto
   8.554 -  next case goal3
   8.555 -    show ?case apply(rule bounded_realI[where B="real (integral\<^isup>P lebesgue f)"])
   8.556 -      apply safe apply(subst abs_of_nonneg) apply(rule integral_nonneg,rule) apply(rule u_int)
   8.557 -      unfolding integral_unique[OF u_int] defer apply(rule real_of_pextreal_mono[OF _ int_u_le])
   8.558 -      using u int_om by auto
   8.559 -  qed note int = conjunctD2[OF this]
   8.560 +  have convergent: "f' integrable_on UNIV \<and>
   8.561 +    (\<lambda>k. integral UNIV (u' k)) ----> integral UNIV f'"
   8.562 +  proof (intro monotone_convergence_increasing allI ballI)
   8.563 +    show int: "\<And>k. (u' k) integrable_on UNIV"
   8.564 +      using u_int unfolding integrable_on_def u' by auto
   8.565 +    show "\<And>k x. u' k x \<le> u' (Suc k) x" using u(2,3,5)
   8.566 +      by (auto simp: incseq_Suc_iff le_fun_def image_iff u' intro!: real_of_extreal_positive_mono)
   8.567 +    show "\<And>x. (\<lambda>k. u' k x) ----> f' x"
   8.568 +      using SUP_eq u(2)
   8.569 +      by (intro SUP_eq_LIMSEQ[THEN iffD1]) (auto simp: u' f' incseq_mono incseq_Suc_iff le_fun_def)
   8.570 +    show "bounded {integral UNIV (u' k)|k. True}"
   8.571 +    proof (safe intro!: bounded_realI)
   8.572 +      fix k
   8.573 +      have "\<bar>integral UNIV (u' k)\<bar> = integral UNIV (u' k)"
   8.574 +        by (intro abs_of_nonneg integral_nonneg int ballI u')
   8.575 +      also have "\<dots> = real (integral\<^isup>S lebesgue (u k))"
   8.576 +        using u_int[THEN integral_unique] by (simp add: u')
   8.577 +      also have "\<dots> = real (integral\<^isup>P lebesgue (u k))"
   8.578 +        using lebesgue.positive_integral_eq_simple_integral[OF u(1,5)] by simp
   8.579 +      also have "\<dots> \<le> real (integral\<^isup>P lebesgue f)" using f
   8.580 +        by (auto intro!: real_of_extreal_positive_mono lebesgue.positive_integral_positive
   8.581 +             lebesgue.positive_integral_mono le_SUPI simp: SUP_eq[symmetric])
   8.582 +      finally show "\<bar>integral UNIV (u' k)\<bar> \<le> real (integral\<^isup>P lebesgue f)" .
   8.583 +    qed
   8.584 +  qed
   8.585  
   8.586 -  have "(\<lambda>i. integral\<^isup>S lebesgue (u i)) ----> ?i" unfolding u_simple
   8.587 -    apply(rule lebesgue.positive_integral_monotone_convergence(2))
   8.588 -    apply(rule lebesgue.borel_measurable_simple_function[OF u(1)])
   8.589 -    using isotone_Lim[OF u(3)[unfolded isoton_fun_expand, THEN spec]] by auto
   8.590 -  hence "(\<lambda>i. real (integral\<^isup>S lebesgue (u i))) ----> real ?i" apply-
   8.591 -    apply(subst lim_Real[THEN sym]) prefer 3
   8.592 -    apply(subst Real_real') defer apply(subst Real_real')
   8.593 -    using u f_om int_om u_int_om by auto
   8.594 -  note * = LIMSEQ_unique[OF this int(2)[unfolded integral_unique[OF u_int]]]
   8.595 -  show ?thesis unfolding * by(rule integrable_integral[OF int(1)])
   8.596 +  have "integral\<^isup>P lebesgue f = extreal (integral UNIV f')"
   8.597 +  proof (rule tendsto_unique[OF trivial_limit_sequentially])
   8.598 +    have "(\<lambda>i. integral\<^isup>S lebesgue (u i)) ----> (SUP i. integral\<^isup>P lebesgue (u i))"
   8.599 +      unfolding u_eq by (intro LIMSEQ_extreal_SUPR lebesgue.incseq_positive_integral u)
   8.600 +    also note lebesgue.positive_integral_monotone_convergence_SUP
   8.601 +      [OF u(2)  lebesgue.borel_measurable_simple_function[OF u(1)] u(5), symmetric]
   8.602 +    finally show "(\<lambda>k. integral\<^isup>S lebesgue (u k)) ----> integral\<^isup>P lebesgue f"
   8.603 +      unfolding SUP_eq .
   8.604 +
   8.605 +    { fix k
   8.606 +      have "0 \<le> integral\<^isup>S lebesgue (u k)"
   8.607 +        using u by (auto intro!: lebesgue.simple_integral_positive)
   8.608 +      then have "integral\<^isup>S lebesgue (u k) = extreal (real (integral\<^isup>S lebesgue (u k)))"
   8.609 +        using u_fin by (auto simp: extreal_real) }
   8.610 +    note * = this
   8.611 +    show "(\<lambda>k. integral\<^isup>S lebesgue (u k)) ----> extreal (integral UNIV f')"
   8.612 +      using convergent using u_int[THEN integral_unique, symmetric]
   8.613 +      by (subst *) (simp add: lim_extreal u')
   8.614 +  qed
   8.615 +  then show ?thesis using convergent by (simp add: f' integrable_integral)
   8.616  qed
   8.617  
   8.618  lemma lebesgue_integral_has_integral:
   8.619 -  fixes f::"'a::ordered_euclidean_space => real"
   8.620 -  assumes f:"integrable lebesgue f"
   8.621 +  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   8.622 +  assumes f: "integrable lebesgue f"
   8.623    shows "(f has_integral (integral\<^isup>L lebesgue f)) UNIV"
   8.624 -proof- let ?n = "\<lambda>x. - min (f x) 0" and ?p = "\<lambda>x. max (f x) 0"
   8.625 -  have *:"f = (\<lambda>x. ?p x - ?n x)" apply rule by auto
   8.626 -  note f = integrableD[OF f]
   8.627 -  show ?thesis unfolding lebesgue_integral_def apply(subst *)
   8.628 -  proof(rule has_integral_sub) case goal1
   8.629 -    have *:"\<forall>x. Real (f x) \<noteq> \<omega>" by auto
   8.630 -    note lebesgue.borel_measurable_Real[OF f(1)]
   8.631 -    from positive_integral_has_integral[OF this f(2) *]
   8.632 -    show ?case unfolding real_Real_max .
   8.633 -  next case goal2
   8.634 -    have *:"\<forall>x. Real (- f x) \<noteq> \<omega>" by auto
   8.635 -    note lebesgue.borel_measurable_uminus[OF f(1)]
   8.636 -    note lebesgue.borel_measurable_Real[OF this]
   8.637 -    from positive_integral_has_integral[OF this f(3) *]
   8.638 -    show ?case unfolding real_Real_max minus_min_eq_max by auto
   8.639 -  qed
   8.640 +proof -
   8.641 +  let ?n = "\<lambda>x. real (extreal (max 0 (- f x)))" and ?p = "\<lambda>x. real (extreal (max 0 (f x)))"
   8.642 +  have *: "f = (\<lambda>x. ?p x - ?n x)" by (auto simp del: extreal_max)
   8.643 +  { fix f have "(\<integral>\<^isup>+ x. extreal (f x) \<partial>lebesgue) = (\<integral>\<^isup>+ x. extreal (max 0 (f x)) \<partial>lebesgue)"
   8.644 +      by (intro lebesgue.positive_integral_cong_pos) (auto split: split_max) }
   8.645 +  note eq = this
   8.646 +  show ?thesis
   8.647 +    unfolding lebesgue_integral_def
   8.648 +    apply (subst *)
   8.649 +    apply (rule has_integral_sub)
   8.650 +    unfolding eq[of f] eq[of "\<lambda>x. - f x"]
   8.651 +    apply (safe intro!: positive_integral_has_integral)
   8.652 +    using integrableD[OF f]
   8.653 +    by (auto simp: zero_extreal_def[symmetric] positive_integral_max_0  split: split_max
   8.654 +             intro!: lebesgue.measurable_If lebesgue.borel_measurable_extreal)
   8.655  qed
   8.656  
   8.657  lemma lebesgue_positive_integral_eq_borel:
   8.658 -  "f \<in> borel_measurable borel \<Longrightarrow> integral\<^isup>P lebesgue f = integral\<^isup>P lborel f"
   8.659 -  by (auto intro!: lebesgue.positive_integral_subalgebra[symmetric]) default
   8.660 +  assumes f: "f \<in> borel_measurable borel"
   8.661 +  shows "integral\<^isup>P lebesgue f = integral\<^isup>P lborel f"
   8.662 +proof -
   8.663 +  from f have "integral\<^isup>P lebesgue (\<lambda>x. max 0 (f x)) = integral\<^isup>P lborel (\<lambda>x. max 0 (f x))"
   8.664 +    by (auto intro!: lebesgue.positive_integral_subalgebra[symmetric]) default
   8.665 +  then show ?thesis unfolding positive_integral_max_0 .
   8.666 +qed
   8.667  
   8.668  lemma lebesgue_integral_eq_borel:
   8.669    assumes "f \<in> borel_measurable borel"
   8.670 @@ -771,7 +827,7 @@
   8.671    have "sets ?G = sets (\<Pi>\<^isub>M i\<in>I.
   8.672         sigma \<lparr> space = UNIV::real set, sets = range lessThan, measure = lebesgue.\<mu> \<rparr>)"
   8.673      by (subst sigma_product_algebra_sigma_eq[of I "\<lambda>_ i. {..<real i}" ])
   8.674 -       (auto intro!: measurable_sigma_sigma isotoneI real_arch_lt
   8.675 +       (auto intro!: measurable_sigma_sigma incseq_SucI real_arch_lt
   8.676               simp: product_algebra_def)
   8.677    then show ?thesis
   8.678      unfolding lborel_def borel_eq_lessThan lebesgue_def sigma_def by simp
   8.679 @@ -838,9 +894,10 @@
   8.680    let ?E = "\<lparr> space = UNIV :: 'a set, sets = range (\<lambda>(a,b). {a..b}) \<rparr>"
   8.681    show "Int_stable ?E" using Int_stable_cuboids .
   8.682    show "range cube \<subseteq> sets ?E" unfolding cube_def_raw by auto
   8.683 +  show "incseq cube" using cube_subset_Suc by (auto intro!: incseq_SucI)
   8.684    { fix x have "\<exists>n. x \<in> cube n" using mem_big_cube[of x] by fastsimp }
   8.685 -  then show "cube \<up> space ?E" by (intro isotoneI cube_subset_Suc) auto
   8.686 -  { fix i show "lborel.\<mu> (cube i) \<noteq> \<omega>" unfolding cube_def by auto }
   8.687 +  then show "(\<Union>i. cube i) = space ?E" by auto
   8.688 +  { fix i show "lborel.\<mu> (cube i) \<noteq> \<infinity>" unfolding cube_def by auto }
   8.689    show "A \<in> sets (sigma ?E)" "sets (sigma ?E) = sets lborel" "space ?E = space lborel"
   8.690      using assms by (simp_all add: borel_eq_atLeastAtMost)
   8.691  
   8.692 @@ -857,7 +914,7 @@
   8.693          by (simp add: interval_ne_empty eucl_le[where 'a='a])
   8.694        then have "lborel.\<mu> X = (\<Prod>x<DIM('a). lborel.\<mu> {a $$ x .. b $$ x})"
   8.695          by (auto simp: content_closed_interval eucl_le[where 'a='a]
   8.696 -                 intro!: Real_setprod )
   8.697 +                 intro!: setprod_extreal[symmetric])
   8.698        also have "\<dots> = measure ?P (?T X)"
   8.699          unfolding * by (subst lborel_space.measure_times) auto
   8.700        finally show ?thesis .
   8.701 @@ -882,7 +939,7 @@
   8.702    using lborel_eq_lborel_space[OF A] by simp
   8.703  
   8.704  lemma borel_fubini_positiv_integral:
   8.705 -  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> pextreal"
   8.706 +  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> extreal"
   8.707    assumes f: "f \<in> borel_measurable borel"
   8.708    shows "integral\<^isup>P lborel f = \<integral>\<^isup>+x. f (p2e x) \<partial>(lborel_space.P DIM('a))"
   8.709  proof (rule lborel_space.positive_integral_vimage[OF _ measure_preserving_p2e _])
     9.1 --- a/src/HOL/Probability/Measure.thy	Mon Mar 14 14:37:47 2011 +0100
     9.2 +++ b/src/HOL/Probability/Measure.thy	Mon Mar 14 14:37:49 2011 +0100
     9.3 @@ -76,7 +76,7 @@
     9.4  
     9.5  lemma (in measure_space) measure_countably_additive:
     9.6    assumes "range A \<subseteq> sets M" "disjoint_family A"
     9.7 -  shows "psuminf (\<lambda>i. \<mu> (A i)) = \<mu> (\<Union>i. A i)"
     9.8 +  shows "(\<Sum>i. \<mu> (A i)) = \<mu> (\<Union>i. A i)"
     9.9  proof -
    9.10    have "(\<Union> i. A i) \<in> sets M" using assms(1) by (rule countable_UN)
    9.11    with ca assms show ?thesis by (simp add: countably_additive_def)
    9.12 @@ -94,13 +94,13 @@
    9.13    interpret N: sigma_algebra N by (intro sigma_algebra_cong assms)
    9.14    show ?thesis
    9.15    proof
    9.16 -    show "measure N {} = 0" using assms by auto
    9.17 +    show "positive N (measure N)" using assms by (auto simp: positive_def)
    9.18      show "countably_additive N (measure N)" unfolding countably_additive_def
    9.19      proof safe
    9.20        fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets N" "disjoint_family A"
    9.21        then have "\<And>i. A i \<in> sets M" "(UNION UNIV A) \<in> sets M" unfolding assms by auto
    9.22        from measure_countably_additive[of A] A this[THEN assms(1)]
    9.23 -      show "(\<Sum>\<^isub>\<infinity>n. measure N (A n)) = measure N (UNION UNIV A)"
    9.24 +      show "(\<Sum>n. measure N (A n)) = measure N (UNION UNIV A)"
    9.25          unfolding assms by simp
    9.26      qed
    9.27    qed
    9.28 @@ -124,51 +124,51 @@
    9.29    have "b = a \<union> (b - a)" using assms by auto
    9.30    moreover have "{} = a \<inter> (b - a)" by auto
    9.31    ultimately have "\<mu> b = \<mu> a + \<mu> (b - a)"
    9.32 -    using measure_additive[of a "b - a"] local.Diff[of b a] assms by auto
    9.33 -  moreover have "\<mu> (b - a) \<ge> 0" using assms by auto
    9.34 +    using measure_additive[of a "b - a"] Diff[of b a] assms by auto
    9.35 +  moreover have "\<mu> a + 0 \<le> \<mu> a + \<mu> (b - a)" using assms by (intro add_mono) auto
    9.36    ultimately show "\<mu> a \<le> \<mu> b" by auto
    9.37  qed
    9.38  
    9.39  lemma (in measure_space) measure_compl:
    9.40 -  assumes s: "s \<in> sets M" and fin: "\<mu> s \<noteq> \<omega>"
    9.41 +  assumes s: "s \<in> sets M" and fin: "\<mu> s \<noteq> \<infinity>"
    9.42    shows "\<mu> (space M - s) = \<mu> (space M) - \<mu> s"
    9.43  proof -
    9.44    have s_less_space: "\<mu> s \<le> \<mu> (space M)"
    9.45      using s by (auto intro!: measure_mono sets_into_space)
    9.46 -
    9.47 +  from s have "0 \<le> \<mu> s" by auto
    9.48    have "\<mu> (space M) = \<mu> (s \<union> (space M - s))" using s
    9.49      by (metis Un_Diff_cancel Un_absorb1 s sets_into_space)
    9.50    also have "... = \<mu> s + \<mu> (space M - s)"
    9.51      by (rule additiveD [OF additive]) (auto simp add: s)
    9.52    finally have "\<mu> (space M) = \<mu> s + \<mu> (space M - s)" .
    9.53 -  thus ?thesis
    9.54 -    unfolding minus_pextreal_eq2[OF s_less_space fin]
    9.55 -    by (simp add: ac_simps)
    9.56 +  then show ?thesis
    9.57 +    using fin `0 \<le> \<mu> s`
    9.58 +    unfolding extreal_eq_minus_iff by (auto simp: ac_simps)
    9.59  qed
    9.60  
    9.61  lemma (in measure_space) measure_Diff:
    9.62 -  assumes finite: "\<mu> B \<noteq> \<omega>"
    9.63 +  assumes finite: "\<mu> B \<noteq> \<infinity>"
    9.64    and measurable: "A \<in> sets M" "B \<in> sets M" "B \<subseteq> A"
    9.65    shows "\<mu> (A - B) = \<mu> A - \<mu> B"
    9.66  proof -
    9.67 -  have *: "(A - B) \<union> B = A" using `B \<subseteq> A` by auto
    9.68 -
    9.69 -  have "\<mu> ((A - B) \<union> B) = \<mu> (A - B) + \<mu> B"
    9.70 -    using measurable by (rule_tac measure_additive[symmetric]) auto
    9.71 -  thus ?thesis unfolding * using `\<mu> B \<noteq> \<omega>`
    9.72 -    by (simp add: pextreal_cancel_plus_minus)
    9.73 +  have "0 \<le> \<mu> B" using assms by auto
    9.74 +  have "(A - B) \<union> B = A" using `B \<subseteq> A` by auto
    9.75 +  then have "\<mu> A = \<mu> ((A - B) \<union> B)" by simp
    9.76 +  also have "\<dots> = \<mu> (A - B) + \<mu> B"
    9.77 +    using measurable by (subst measure_additive[symmetric]) auto
    9.78 +  finally show "\<mu> (A - B) = \<mu> A - \<mu> B"
    9.79 +    unfolding extreal_eq_minus_iff
    9.80 +    using finite `0 \<le> \<mu> B` by auto
    9.81  qed
    9.82  
    9.83  lemma (in measure_space) measure_countable_increasing:
    9.84    assumes A: "range A \<subseteq> sets M"
    9.85        and A0: "A 0 = {}"
    9.86 -      and ASuc: "\<And>n.  A n \<subseteq> A (Suc n)"
    9.87 +      and ASuc: "\<And>n. A n \<subseteq> A (Suc n)"
    9.88    shows "(SUP n. \<mu> (A n)) = \<mu> (\<Union>i. A i)"
    9.89  proof -
    9.90 -  {
    9.91 -    fix n
    9.92 -    have "\<mu> (A n) =
    9.93 -          setsum (\<mu> \<circ> (\<lambda>i. A (Suc i) - A i)) {..<n}"
    9.94 +  { fix n
    9.95 +    have "\<mu> (A n) = (\<Sum>i<n. \<mu> (A (Suc i) - A i))"
    9.96        proof (induct n)
    9.97          case 0 thus ?case by (auto simp add: A0)
    9.98        next
    9.99 @@ -199,92 +199,83 @@
   9.100      by (metis A Diff range_subsetD)
   9.101    have A2: "(\<Union>i. A (Suc i) - A i) \<in> sets M"
   9.102      by (blast intro: range_subsetD [OF A])
   9.103 -  have "psuminf ( (\<lambda>i. \<mu> (A (Suc i) - A i))) = \<mu> (\<Union>i. A (Suc i) - A i)"
   9.104 +  have "(SUP n. \<Sum>i<n. \<mu> (A (Suc i) - A i)) = (\<Sum>i. \<mu> (A (Suc i) - A i))"
   9.105 +    using A by (auto intro!: suminf_extreal_eq_SUPR[symmetric])
   9.106 +  also have "\<dots> = \<mu> (\<Union>i. A (Suc i) - A i)"
   9.107      by (rule measure_countably_additive)
   9.108         (auto simp add: disjoint_family_Suc ASuc A1 A2)
   9.109    also have "... =  \<mu> (\<Union>i. A i)"
   9.110      by (simp add: Aeq)
   9.111 -  finally have "psuminf (\<lambda>i. \<mu> (A (Suc i) - A i)) = \<mu> (\<Union>i. A i)" .
   9.112 -  thus ?thesis
   9.113 -    by (auto simp add: Meq psuminf_def)
   9.114 +  finally have "(SUP n. \<Sum>i<n. \<mu> (A (Suc i) - A i)) = \<mu> (\<Union>i. A i)" .
   9.115 +  then show ?thesis by (auto simp add: Meq)
   9.116  qed
   9.117  
   9.118  lemma (in measure_space) continuity_from_below:
   9.119 -  assumes A: "range A \<subseteq> sets M"
   9.120 -      and ASuc: "!!n.  A n \<subseteq> A (Suc n)"
   9.121 +  assumes A: "range A \<subseteq> sets M" and "incseq A"
   9.122    shows "(SUP n. \<mu> (A n)) = \<mu> (\<Union>i. A i)"
   9.123  proof -
   9.124    have *: "(SUP n. \<mu> (nat_case {} A (Suc n))) = (SUP n. \<mu> (nat_case {} A n))"
   9.125 -    apply (rule Sup_mono_offset_Suc)
   9.126 -    apply (rule measure_mono)
   9.127 -    using assms by (auto split: nat.split)
   9.128 -
   9.129 +    using A by (auto intro!: SUPR_eq exI split: nat.split)
   9.130    have ueq: "(\<Union>i. nat_case {} A i) = (\<Union>i. A i)"
   9.131      by (auto simp add: split: nat.splits)
   9.132    have meq: "\<And>n. \<mu> (A n) = (\<mu> \<circ> nat_case {} A) (Suc n)"
   9.133      by simp
   9.134    have "(SUP n. \<mu> (nat_case {} A n)) = \<mu> (\<Union>i. nat_case {} A i)"
   9.135 -    by (rule measure_countable_increasing)
   9.136 -       (auto simp add: range_subsetD [OF A] subsetD [OF ASuc] split: nat.splits)
   9.137 +    using range_subsetD[OF A] incseq_SucD[OF `incseq A`]
   9.138 +    by (force split: nat.splits intro!: measure_countable_increasing)
   9.139    also have "\<mu> (\<Union>i. nat_case {} A i) = \<mu> (\<Union>i. A i)"
   9.140      by (simp add: ueq)
   9.141    finally have "(SUP n. \<mu> (nat_case {} A n)) = \<mu> (\<Union>i. A i)" .
   9.142    thus ?thesis unfolding meq * comp_def .
   9.143  qed
   9.144  
   9.145 -lemma (in measure_space) measure_up:
   9.146 -  assumes "\<And>i. B i \<in> sets M" "B \<up> P"
   9.147 -  shows "(\<lambda>i. \<mu> (B i)) \<up> \<mu> P"
   9.148 -  using assms unfolding isoton_def
   9.149 -  by (auto intro!: measure_mono continuity_from_below)
   9.150 +lemma (in measure_space) measure_incseq:
   9.151 +  assumes "range B \<subseteq> sets M" "incseq B"
   9.152 +  shows "incseq (\<lambda>i. \<mu> (B i))"
   9.153 +  using assms by (auto simp: incseq_def intro!: measure_mono)
   9.154  
   9.155 -lemma (in measure_space) continuity_from_below':
   9.156 -  assumes A: "range A \<subseteq> sets M" "\<And>i. A i \<subseteq> A (Suc i)"
   9.157 -  shows "(\<lambda>i. (\<mu> (A i))) ----> (\<mu> (\<Union>i. A i))"
   9.158 -proof- let ?A = "\<Union>i. A i"
   9.159 -  have " (\<lambda>i. \<mu> (A i)) \<up> \<mu> ?A" apply(rule measure_up)
   9.160 -    using assms unfolding complete_lattice_class.isoton_def by auto
   9.161 -  thus ?thesis by(rule isotone_Lim(1))
   9.162 -qed
   9.163 +lemma (in measure_space) continuity_from_below_Lim:
   9.164 +  assumes A: "range A \<subseteq> sets M" "incseq A"
   9.165 +  shows "(\<lambda>i. (\<mu> (A i))) ----> \<mu> (\<Union>i. A i)"
   9.166 +  using LIMSEQ_extreal_SUPR[OF measure_incseq, OF A]
   9.167 +    continuity_from_below[OF A] by simp
   9.168 +
   9.169 +lemma (in measure_space) measure_decseq:
   9.170 +  assumes "range B \<subseteq> sets M" "decseq B"
   9.171 +  shows "decseq (\<lambda>i. \<mu> (B i))"
   9.172 +  using assms by (auto simp: decseq_def intro!: measure_mono)
   9.173  
   9.174  lemma (in measure_space) continuity_from_above:
   9.175 -  assumes A: "range A \<subseteq> sets M"
   9.176 -  and mono_Suc: "\<And>n.  A (Suc n) \<subseteq> A n"
   9.177 -  and finite: "\<And>i. \<mu> (A i) \<noteq> \<omega>"
   9.178 +  assumes A: "range A \<subseteq> sets M" and "decseq A"
   9.179 +  and finite: "\<And>i. \<mu> (A i) \<noteq> \<infinity>"
   9.180    shows "(INF n. \<mu> (A n)) = \<mu> (\<Inter>i. A i)"
   9.181  proof -
   9.182 -  { fix n have "A n \<subseteq> A 0" using mono_Suc by (induct n) auto }
   9.183 -  note mono = this
   9.184 -
   9.185    have le_MI: "\<mu> (\<Inter>i. A i) \<le> \<mu> (A 0)"
   9.186      using A by (auto intro!: measure_mono)
   9.187 -  hence *: "\<mu> (\<Inter>i. A i) \<noteq> \<omega>" using finite[of 0] by auto
   9.188 +  hence *: "\<mu> (\<Inter>i. A i) \<noteq> \<infinity>" using finite[of 0] by auto
   9.189 +
   9.190 +  have A0: "0 \<le> \<mu> (A 0)" using A by auto
   9.191  
   9.192 -  have le_IM: "(INF n. \<mu> (A n)) \<le> \<mu> (A 0)"
   9.193 -    by (rule INF_leI) simp
   9.194 -
   9.195 -  have "\<mu> (A 0) - (INF n. \<mu> (A n)) = (SUP n. \<mu> (A 0 - A n))"
   9.196 -    unfolding pextreal_SUP_minus[symmetric]
   9.197 -    using mono A finite by (subst measure_Diff) auto
   9.198 +  have "\<mu> (A 0) - (INF n. \<mu> (A n)) = \<mu> (A 0) + (SUP n. - \<mu> (A n))"
   9.199 +    by (simp add: extreal_SUPR_uminus minus_extreal_def)
   9.200 +  also have "\<dots> = (SUP n. \<mu> (A 0) - \<mu> (A n))"
   9.201 +    unfolding minus_extreal_def using A0 assms
   9.202 +    by (subst SUPR_extreal_add) (auto simp add: measure_decseq)
   9.203 +  also have "\<dots> = (SUP n. \<mu> (A 0 - A n))"
   9.204 +    using A finite `decseq A`[unfolded decseq_def] by (subst measure_Diff) auto
   9.205    also have "\<dots> = \<mu> (\<Union>i. A 0 - A i)"
   9.206    proof (rule continuity_from_below)
   9.207      show "range (\<lambda>n. A 0 - A n) \<subseteq> sets M"
   9.208        using A by auto
   9.209 -    show "\<And>n. A 0 - A n \<subseteq> A 0 - A (Suc n)"
   9.210 -      using mono_Suc by auto
   9.211 +    show "incseq (\<lambda>n. A 0 - A n)"
   9.212 +      using `decseq A` by (auto simp add: incseq_def decseq_def)
   9.213    qed
   9.214    also have "\<dots> = \<mu> (A 0) - \<mu> (\<Inter>i. A i)"
   9.215 -    using mono A finite * by (simp, subst measure_Diff) auto
   9.216 +    using A finite * by (simp, subst measure_Diff) auto
   9.217    finally show ?thesis
   9.218 -    by (rule pextreal_diff_eq_diff_imp_eq[OF finite[of 0] le_IM le_MI])
   9.219 +    unfolding extreal_minus_eq_minus_iff using finite A0 by auto
   9.220  qed
   9.221  
   9.222 -lemma (in measure_space) measure_down:
   9.223 -  assumes "\<And>i. B i \<in> sets M" "B \<down> P"
   9.224 -  and finite: "\<And>i. \<mu> (B i) \<noteq> \<omega>"
   9.225 -  shows "(\<lambda>i. \<mu> (B i)) \<down> \<mu> P"
   9.226 -  using assms unfolding antiton_def
   9.227 -  by (auto intro!: measure_mono continuity_from_above)
   9.228  lemma (in measure_space) measure_insert:
   9.229    assumes sets: "{x} \<in> sets M" "A \<in> sets M" and "x \<notin> A"
   9.230    shows "\<mu> (insert x A) = \<mu> {x} + \<mu> A"
   9.231 @@ -293,109 +284,26 @@
   9.232    from measure_additive[OF sets this] show ?thesis by simp
   9.233  qed
   9.234  
   9.235 -lemma (in measure_space) measure_finite_singleton:
   9.236 -  assumes fin: "finite S"
   9.237 -  and ssets: "\<And>x. x \<in> S \<Longrightarrow> {x} \<in> sets M"
   9.238 -  shows "\<mu> S = (\<Sum>x\<in>S. \<mu> {x})"
   9.239 +lemma (in measure_space) measure_setsum:
   9.240 +  assumes "finite S" and "\<And>i. i \<in> S \<Longrightarrow> A i \<in> sets M"
   9.241 +  assumes disj: "disjoint_family_on A S"
   9.242 +  shows "(\<Sum>i\<in>S. \<mu> (A i)) = \<mu> (\<Union>i\<in>S. A i)"
   9.243  using assms proof induct
   9.244 -  case (insert x S)
   9.245 -  have *: "\<mu> S = (\<Sum>x\<in>S. \<mu> {x})" "{x} \<in> sets M"
   9.246 -    using insert.prems by (blast intro!: insert.hyps(3))+
   9.247 -
   9.248 -  have "(\<Union>x\<in>S. {x}) \<in> sets M"
   9.249 -    using  insert.prems `finite S` by (blast intro!: finite_UN)
   9.250 -  hence "S \<in> sets M" by auto
   9.251 -  from measure_insert[OF `{x} \<in> sets M` this `x \<notin> S`]
   9.252 -  show ?case using `x \<notin> S` `finite S` * by simp
   9.253 +  case (insert i S)
   9.254 +  then have "(\<Sum>i\<in>S. \<mu> (A i)) = \<mu> (\<Union>a\<in>S. A a)"
   9.255 +    by (auto intro: disjoint_family_on_mono)
   9.256 +  moreover have "A i \<inter> (\<Union>a\<in>S. A a) = {}"
   9.257 +    using `disjoint_family_on A (insert i S)` `i \<notin> S`
   9.258 +    by (auto simp: disjoint_family_on_def)
   9.259 +  ultimately show ?case using insert
   9.260 +    by (auto simp: measure_additive finite_UN)
   9.261  qed simp
   9.262  
   9.263 -lemma (in measure_space) measure_finitely_additive':
   9.264 -  assumes "f \<in> ({..< n :: nat} \<rightarrow> sets M)"
   9.265 -  assumes "\<And> a b. \<lbrakk>a < n ; b < n ; a \<noteq> b\<rbrakk> \<Longrightarrow> f a \<inter> f b = {}"
   9.266 -  assumes "s = \<Union> (f ` {..< n})"
   9.267 -  shows "(\<Sum>i<n. (\<mu> \<circ> f) i) = \<mu> s"
   9.268 -proof -
   9.269 -  def f' == "\<lambda> i. (if i < n then f i else {})"
   9.270 -  have rf: "range f' \<subseteq> sets M" unfolding f'_def
   9.271 -    using assms empty_sets by auto
   9.272 -  have df: "disjoint_family f'" unfolding f'_def disjoint_family_on_def
   9.273 -    using assms by simp
   9.274 -  have "\<Union> range f' = (\<Union> i \<in> {..< n}. f i)"
   9.275 -    unfolding f'_def by auto
   9.276 -  then have "\<mu> s = \<mu> (\<Union> range f')"
   9.277 -    using assms by simp
   9.278 -  then have part1: "(\<Sum>\<^isub>\<infinity> i. \<mu> (f' i)) = \<mu> s"
   9.279 -    using df rf ca[unfolded countably_additive_def, rule_format, of f']
   9.280 -    by auto
   9.281 -
   9.282 -  have "(\<Sum>\<^isub>\<infinity> i. \<mu> (f' i)) = (\<Sum> i< n. \<mu> (f' i))"
   9.283 -    by (rule psuminf_finite) (simp add: f'_def)
   9.284 -  also have "\<dots> = (\<Sum>i<n. \<mu> (f i))"
   9.285 -    unfolding f'_def by auto
   9.286 -  finally have part2: "(\<Sum>\<^isub>\<infinity> i. \<mu> (f' i)) = (\<Sum>i<n. \<mu> (f i))" by simp
   9.287 -  show ?thesis using part1 part2 by auto
   9.288 -qed
   9.289 -
   9.290 -
   9.291 -lemma (in measure_space) measure_finitely_additive:
   9.292 -  assumes "finite r"
   9.293 -  assumes "r \<subseteq> sets M"
   9.294 -  assumes d: "\<And> a b. \<lbrakk>a \<in> r ; b \<in> r ; a \<noteq> b\<rbrakk> \<Longrightarrow> a \<inter> b = {}"
   9.295 -  shows "(\<Sum> i \<in> r. \<mu> i) = \<mu> (\<Union> r)"
   9.296 -using assms
   9.297 -proof -
   9.298 -  (* counting the elements in r is enough *)
   9.299 -  let ?R = "{..<card r}"
   9.300 -  obtain f where f: "f ` ?R = r" "inj_on f ?R"
   9.301 -    using ex_bij_betw_nat_finite[unfolded bij_betw_def, OF `finite r`]
   9.302 -    unfolding atLeast0LessThan by auto
   9.303 -  hence f_into_sets: "f \<in> ?R \<rightarrow> sets M" using assms by auto
   9.304 -  have disj: "\<And> a b. \<lbrakk>a \<in> ?R ; b \<in> ?R ; a \<noteq> b\<rbrakk> \<Longrightarrow> f a \<inter> f b = {}"
   9.305 -  proof -
   9.306 -    fix a b assume asm: "a \<in> ?R" "b \<in> ?R" "a \<noteq> b"
   9.307 -    hence neq: "f a \<noteq> f b" using f[unfolded inj_on_def, rule_format] by blast
   9.308 -    from asm have "f a \<in> r" "f b \<in> r" using f by auto
   9.309 -    thus "f a \<inter> f b = {}" using d[of "f a" "f b"] f using neq by auto
   9.310 -  qed
   9.311 -  have "(\<Union> r) = (\<Union> i \<in> ?R. f i)"
   9.312 -    using f by auto
   9.313 -  hence "\<mu> (\<Union> r)= \<mu> (\<Union> i \<in> ?R. f i)" by simp
   9.314 -  also have "\<dots> = (\<Sum> i \<in> ?R. \<mu> (f i))"
   9.315 -    using measure_finitely_additive'[OF f_into_sets disj] by simp
   9.316 -  also have "\<dots> = (\<Sum> a \<in> r. \<mu> a)"
   9.317 -    using f[rule_format] setsum_reindex[of f ?R "\<lambda> a. \<mu> a"] by auto
   9.318 -  finally show ?thesis by simp
   9.319 -qed
   9.320 -
   9.321 -lemma (in measure_space) measure_finitely_additive'':
   9.322 -  assumes "finite s"
   9.323 -  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<in> sets M"
   9.324 -  assumes d: "disjoint_family_on a s"
   9.325 -  shows "(\<Sum> i \<in> s. \<mu> (a i)) = \<mu> (\<Union> i \<in> s. a i)"
   9.326 -using assms
   9.327 -proof -
   9.328 -  (* counting the elements in s is enough *)
   9.329 -  let ?R = "{..< card s}"
   9.330 -  obtain f where f: "f ` ?R = s" "inj_on f ?R"
   9.331 -    using ex_bij_betw_nat_finite[unfolded bij_betw_def, OF `finite s`]
   9.332 -    unfolding atLeast0LessThan by auto
   9.333 -  hence f_into_sets: "a \<circ> f \<in> ?R \<rightarrow> sets M" using assms unfolding o_def by auto
   9.334 -  have disj: "\<And> i j. \<lbrakk>i \<in> ?R ; j \<in> ?R ; i \<noteq> j\<rbrakk> \<Longrightarrow> (a \<circ> f) i \<inter> (a \<circ> f) j = {}"
   9.335 -  proof -
   9.336 -    fix i j assume asm: "i \<in> ?R" "j \<in> ?R" "i \<noteq> j"
   9.337 -    hence neq: "f i \<noteq> f j" using f[unfolded inj_on_def, rule_format] by blast
   9.338 -    from asm have "f i \<in> s" "f j \<in> s" using f by auto
   9.339 -    thus "(a \<circ> f) i \<inter> (a \<circ> f) j = {}"
   9.340 -      using d f neq unfolding disjoint_family_on_def by auto
   9.341 -  qed
   9.342 -  have "(\<Union> i \<in> s. a i) = (\<Union> i \<in> f ` ?R. a i)" using f by auto
   9.343 -  hence "(\<Union> i \<in> s. a i) = (\<Union> i \<in> ?R. a (f i))" by auto
   9.344 -  hence "\<mu> (\<Union> i \<in> s. a i) = (\<Sum> i \<in> ?R. \<mu> (a (f i)))"
   9.345 -    using measure_finitely_additive'[OF f_into_sets disj] by simp
   9.346 -  also have "\<dots> = (\<Sum> i \<in> s. \<mu> (a i))"
   9.347 -    using f[rule_format] setsum_reindex[of f ?R "\<lambda> i. \<mu> (a i)"] by auto
   9.348 -  finally show ?thesis by simp
   9.349 -qed
   9.350 +lemma (in measure_space) measure_finite_singleton:
   9.351 +  assumes "finite S" "\<And>x. x \<in> S \<Longrightarrow> {x} \<in> sets M"
   9.352 +  shows "\<mu> S = (\<Sum>x\<in>S. \<mu> {x})"
   9.353 +  using measure_setsum[of S "\<lambda>x. {x}", OF assms]
   9.354 +  by (auto simp: disjoint_family_on_def)
   9.355  
   9.356  lemma finite_additivity_sufficient:
   9.357    assumes "sigma_algebra M"
   9.358 @@ -405,7 +313,7 @@
   9.359    interpret sigma_algebra M by fact
   9.360    show ?thesis
   9.361    proof
   9.362 -    show [simp]: "measure M {} = 0" using pos by (simp add: positive_def)
   9.363 +    show [simp]: "positive M (measure M)" using pos by (simp add: positive_def)
   9.364      show "countably_additive M (measure M)"
   9.365      proof (auto simp add: countably_additive_def)
   9.366        fix A :: "nat \<Rightarrow> 'a set"
   9.367 @@ -434,12 +342,12 @@
   9.368              by blast
   9.369          qed
   9.370        then obtain N where N: "\<forall>m\<ge>N. A m = {}" by blast
   9.371 -      then have "\<forall>m\<ge>N. measure M (A m) = 0" by simp
   9.372 -      then have "(\<Sum>\<^isub>\<infinity> n. measure M (A n)) = setsum (\<lambda>m. measure M (A m)) {..<N}"
   9.373 -        by (simp add: psuminf_finite)
   9.374 +      then have "\<forall>m\<ge>N. measure M (A m) = 0" using pos[unfolded positive_def] by simp
   9.375 +      then have "(\<Sum>n. measure M (A n)) = (\<Sum>m<N. measure M (A m))"
   9.376 +        by (simp add: suminf_finite)
   9.377        also have "... = measure M (\<Union>i<N. A i)"
   9.378          proof (induct N)
   9.379 -          case 0 thus ?case by simp
   9.380 +          case 0 thus ?case using pos[unfolded positive_def] by simp
   9.381          next
   9.382            case (Suc n)
   9.383            have "measure M (A n \<union> (\<Union> x<n. A x)) = measure M (A n) + measure M (\<Union> i<n. A i)"
   9.384 @@ -465,30 +373,25 @@
   9.385              by auto (metis Int_absorb N disjoint_iff_not_equal lessThan_iff not_leE)
   9.386            thus ?thesis by simp
   9.387          qed
   9.388 -      finally show "(\<Sum>\<^isub>\<infinity> n. measure M (A n)) = measure M (\<Union>i. A i)" .
   9.389 +      finally show "(\<Sum>n. measure M (A n)) = measure M (\<Union>i. A i)" .
   9.390      qed
   9.391    qed
   9.392  qed
   9.393  
   9.394  lemma (in measure_space) measure_setsum_split:
   9.395 -  assumes "finite r" and "a \<in> sets M" and br_in_M: "b ` r \<subseteq> sets M"
   9.396 -  assumes "(\<Union>i \<in> r. b i) = space M"
   9.397 -  assumes "disjoint_family_on b r"
   9.398 -  shows "\<mu> a = (\<Sum> i \<in> r. \<mu> (a \<inter> (b i)))"
   9.399 +  assumes "finite S" and "A \<in> sets M" and br_in_M: "B ` S \<subseteq> sets M"
   9.400 +  assumes "(\<Union>i\<in>S. B i) = space M"
   9.401 +  assumes "disjoint_family_on B S"
   9.402 +  shows "\<mu> A = (\<Sum>i\<in>S. \<mu> (A \<inter> (B i)))"
   9.403  proof -
   9.404 -  have *: "\<mu> a = \<mu> (\<Union>i \<in> r. a \<inter> b i)"
   9.405 +  have *: "\<mu> A = \<mu> (\<Union>i\<in>S. A \<inter> B i)"
   9.406      using assms by auto
   9.407    show ?thesis unfolding *
   9.408 -  proof (rule measure_finitely_additive''[symmetric])
   9.409 -    show "finite r" using `finite r` by auto
   9.410 -    { fix i assume "i \<in> r"
   9.411 -      hence "b i \<in> sets M" using br_in_M by auto
   9.412 -      thus "a \<inter> b i \<in> sets M" using `a \<in> sets M` by auto
   9.413 -    }
   9.414 -    show "disjoint_family_on (\<lambda>i. a \<inter> b i) r"
   9.415 -      using `disjoint_family_on b r`
   9.416 +  proof (rule measure_setsum[symmetric])
   9.417 +    show "disjoint_family_on (\<lambda>i. A \<inter> B i) S"
   9.418 +      using `disjoint_family_on B S`
   9.419        unfolding disjoint_family_on_def by auto
   9.420 -  qed
   9.421 +  qed (insert assms, auto)
   9.422  qed
   9.423  
   9.424  lemma (in measure_space) measure_subadditive:
   9.425 @@ -506,7 +409,7 @@
   9.426  lemma (in measure_space) measure_eq_0:
   9.427    assumes "N \<in> sets M" and "\<mu> N = 0" and "K \<subseteq> N" and "K \<in> sets M"
   9.428    shows "\<mu> K = 0"
   9.429 -using measure_mono[OF assms(3,4,1)] assms(2) by auto
   9.430 +  using measure_mono[OF assms(3,4,1)] assms(2) positive_measure[OF assms(4)] by auto
   9.431  
   9.432  lemma (in measure_space) measure_finitely_subadditive:
   9.433    assumes "finite I" "A ` I \<subseteq> sets M"
   9.434 @@ -523,35 +426,38 @@
   9.435  
   9.436  lemma (in measure_space) measure_countably_subadditive:
   9.437    assumes "range f \<subseteq> sets M"
   9.438 -  shows "\<mu> (\<Union>i. f i) \<le> (\<Sum>\<^isub>\<infinity> i. \<mu> (f i))"
   9.439 +  shows "\<mu> (\<Union>i. f i) \<le> (\<Sum>i. \<mu> (f i))"
   9.440  proof -
   9.441    have "\<mu> (\<Union>i. f i) = \<mu> (\<Union>i. disjointed f i)"
   9.442      unfolding UN_disjointed_eq ..
   9.443 -  also have "\<dots> = (\<Sum>\<^isub>\<infinity> i. \<mu> (disjointed f i))"
   9.444 +  also have "\<dots> = (\<Sum>i. \<mu> (disjointed f i))"
   9.445      using range_disjointed_sets[OF assms] measure_countably_additive
   9.446      by (simp add:  disjoint_family_disjointed comp_def)
   9.447 -  also have "\<dots> \<le> (\<Sum>\<^isub>\<infinity> i. \<mu> (f i))"
   9.448 -  proof (rule psuminf_le, rule measure_mono)
   9.449 -    fix i show "disjointed f i \<subseteq> f i" by (rule disjointed_subset)
   9.450 -    show "f i \<in> sets M" "disjointed f i \<in> sets M"
   9.451 -      using assms range_disjointed_sets[OF assms] by auto
   9.452 -  qed
   9.453 +  also have "\<dots> \<le> (\<Sum>i. \<mu> (f i))"
   9.454 +    using range_disjointed_sets[OF assms] assms
   9.455 +    by (auto intro!: suminf_le_pos measure_mono positive_measure disjointed_subset)
   9.456    finally show ?thesis .
   9.457  qed
   9.458  
   9.459  lemma (in measure_space) measure_UN_eq_0:
   9.460 -  assumes "\<And> i :: nat. \<mu> (N i) = 0" and "range N \<subseteq> sets M"
   9.461 +  assumes "\<And>i::nat. \<mu> (N i) = 0" and "range N \<subseteq> sets M"
   9.462    shows "\<mu> (\<Union> i. N i) = 0"
   9.463 -using measure_countably_subadditive[OF assms(2)] assms(1) by auto
   9.464 +proof -
   9.465 +  have "0 \<le> \<mu> (\<Union> i. N i)" using assms by auto
   9.466 +  moreover have "\<mu> (\<Union> i. N i) \<le> 0"
   9.467 +    using measure_countably_subadditive[OF assms(2)] assms(1) by simp
   9.468 +  ultimately show ?thesis by simp
   9.469 +qed
   9.470  
   9.471  lemma (in measure_space) measure_inter_full_set:
   9.472 -  assumes "S \<in> sets M" "T \<in> sets M" and not_\<omega>: "\<mu> (T - S) \<noteq> \<omega>"
   9.473 +  assumes "S \<in> sets M" "T \<in> sets M" and fin: "\<mu> (T - S) \<noteq> \<infinity>"
   9.474    assumes T: "\<mu> T = \<mu> (space M)"
   9.475    shows "\<mu> (S \<inter> T) = \<mu> S"
   9.476  proof (rule antisym)
   9.477    show " \<mu> (S \<inter> T) \<le> \<mu> S"
   9.478      using assms by (auto intro!: measure_mono)
   9.479  
   9.480 +  have pos: "0 \<le> \<mu> (T - S)" using assms by auto
   9.481    show "\<mu> S \<le> \<mu> (S \<inter> T)"
   9.482    proof (rule ccontr)
   9.483      assume contr: "\<not> ?thesis"
   9.484 @@ -560,7 +466,7 @@
   9.485      also have "\<dots> \<le> \<mu> (T - S) + \<mu> (S \<inter> T)"
   9.486        using assms by (auto intro!: measure_subadditive)
   9.487      also have "\<dots> < \<mu> (T - S) + \<mu> S"
   9.488 -      by (rule pextreal_less_add[OF not_\<omega>]) (insert contr, auto)
   9.489 +      using fin contr pos by (intro extreal_less_add) auto
   9.490      also have "\<dots> = \<mu> (T \<union> S)"
   9.491        using assms by (subst measure_additive) auto
   9.492      also have "\<dots> \<le> \<mu> (space M)"
   9.493 @@ -572,11 +478,11 @@
   9.494  lemma measure_unique_Int_stable:
   9.495    fixes E :: "('a, 'b) algebra_scheme" and A :: "nat \<Rightarrow> 'a set"
   9.496    assumes "Int_stable E"
   9.497 -  and A: "range A \<subseteq> sets E" "A \<up> space E"
   9.498 +  and A: "range A \<subseteq> sets E" "incseq A" "(\<Union>i. A i) = space E"
   9.499    and M: "measure_space \<lparr>space = space E, sets = sets (sigma E), measure = \<mu>\<rparr>" (is "measure_space ?M")
   9.500    and N: "measure_space \<lparr>space = space E, sets = sets (sigma E), measure = \<nu>\<rparr>" (is "measure_space ?N")
   9.501    and eq: "\<And>X. X \<in> sets E \<Longrightarrow> \<mu> X = \<nu> X"
   9.502 -  and finite: "\<And>i. \<mu> (A i) \<noteq> \<omega>"
   9.503 +  and finite: "\<And>i. \<mu> (A i) \<noteq> \<infinity>"
   9.504    assumes "X \<in> sets (sigma E)"
   9.505    shows "\<mu> X = \<nu> X"
   9.506  proof -
   9.507 @@ -585,9 +491,9 @@
   9.508      where "space ?M = space E" and "sets ?M = sets (sigma E)" and "measure ?M = \<mu>" by (simp_all add: M)
   9.509    interpret N: measure_space ?N
   9.510      where "space ?N = space E" and "sets ?N = sets (sigma E)" and "measure ?N = \<nu>" by (simp_all add: N)
   9.511 -  { fix F assume "F \<in> sets E" and "\<mu> F \<noteq> \<omega>"
   9.512 +  { fix F assume "F \<in> sets E" and "\<mu> F \<noteq> \<infinity>"
   9.513      then have [intro]: "F \<in> sets (sigma E)" by auto
   9.514 -    have "\<nu> F \<noteq> \<omega>" using `\<mu> F \<noteq> \<omega>` `F \<in> sets E` eq by simp
   9.515 +    have "\<nu> F \<noteq> \<infinity>" using `\<mu> F \<noteq> \<infinity>` `F \<in> sets E` eq by simp
   9.516      interpret D: dynkin_system "\<lparr>space=space E, sets=?D F\<rparr>"
   9.517      proof (rule dynkin_systemI, simp_all)
   9.518        fix A assume "A \<in> sets (sigma E) \<and> \<mu> (F \<inter> A) = \<nu> (F \<inter> A)"
   9.519 @@ -602,14 +508,14 @@
   9.520          and [intro]: "F \<inter> A \<in> sets (sigma E)"
   9.521          using `F \<in> sets E` M.sets_into_space by auto
   9.522        have "\<nu> (F \<inter> A) \<le> \<nu> F" by (auto intro!: N.measure_mono)
   9.523 -      then have "\<nu> (F \<inter> A) \<noteq> \<omega>" using `\<nu> F \<noteq> \<omega>` by auto
   9.524 +      then have "\<nu> (F \<inter> A) \<noteq> \<infinity>" using `\<nu> F \<noteq> \<infinity>` by auto
   9.525        have "\<mu> (F \<inter> A) \<le> \<mu> F" by (auto intro!: M.measure_mono)
   9.526 -      then have "\<mu> (F \<inter> A) \<noteq> \<omega>" using `\<mu> F \<noteq> \<omega>` by auto
   9.527 +      then have "\<mu> (F \<inter> A) \<noteq> \<infinity>" using `\<mu> F \<noteq> \<infinity>` by auto
   9.528        then have "\<mu> (F \<inter> (space (sigma E) - A)) = \<mu> F - \<mu> (F \<inter> A)" unfolding **
   9.529          using `F \<inter> A \<in> sets (sigma E)` by (auto intro!: M.measure_Diff)
   9.530        also have "\<dots> = \<nu> F - \<nu> (F \<inter> A)" using eq `F \<in> sets E` * by simp
   9.531        also have "\<dots> = \<nu> (F \<inter> (space (sigma E) - A))" unfolding **
   9.532 -        using `F \<inter> A \<in> sets (sigma E)` `\<nu> (F \<inter> A) \<noteq> \<omega>` by (auto intro!: N.measure_Diff[symmetric])
   9.533 +        using `F \<inter> A \<in> sets (sigma E)` `\<nu> (F \<inter> A) \<noteq> \<infinity>` by (auto intro!: N.measure_Diff[symmetric])
   9.534        finally show "space E - A \<in> sets (sigma E) \<and> \<mu> (F \<inter> (space E - A)) = \<nu> (F \<inter> (space E - A))"
   9.535          using * by auto
   9.536      next
   9.537 @@ -630,15 +536,13 @@
   9.538      have "\<And>D. D \<in> sets (sigma E) \<Longrightarrow> \<mu> (F \<inter> D) = \<nu> (F \<inter> D)"
   9.539        by (subst (asm) *) auto }
   9.540    note * = this
   9.541 -  { fix i have "\<mu> (A i \<inter> X) = \<nu> (A i \<inter> X)"
   9.542 +  let "?A i" = "A i \<inter> X"
   9.543 +  have A': "range ?A \<subseteq> sets (sigma E)" "incseq ?A"
   9.544 +    using A(1,2) `X \<in> sets (sigma E)` by (auto simp: incseq_def)
   9.545 +  { fix i have "\<mu> (?A i) = \<nu> (?A i)"
   9.546        using *[of "A i" X] `X \<in> sets (sigma E)` A finite by auto }
   9.547 -  moreover
   9.548 -  have "(\<lambda>i. A i \<inter> X) \<up> X"
   9.549 -    using `X \<in> sets (sigma E)` M.sets_into_space A
   9.550 -    by (auto simp: isoton_def)
   9.551 -  then have "(\<lambda>i. \<mu> (A i \<inter> X)) \<up> \<mu> X" "(\<lambda>i. \<nu> (A i \<inter> X)) \<up> \<nu> X"
   9.552 -    using `X \<in> sets (sigma E)` A by (auto intro!: M.measure_up N.measure_up M.Int simp: subset_eq)
   9.553 -  ultimately show ?thesis by (simp add: isoton_def)
   9.554 +  with M.continuity_from_below[OF A'] N.continuity_from_below[OF A']
   9.555 +  show ?thesis using A(3) `X \<in> sets (sigma E)` by auto
   9.556  qed
   9.557  
   9.558  section "@{text \<mu>}-null sets"
   9.559 @@ -650,10 +554,10 @@
   9.560    shows "N \<union> N' \<in> null_sets"
   9.561  proof (intro conjI CollectI)
   9.562    show "N \<union> N' \<in> sets M" using assms by auto
   9.563 -  have "\<mu> (N \<union> N') \<le> \<mu> N + \<mu> N'"
   9.564 +  then have "0 \<le> \<mu> (N \<union> N')" by simp
   9.565 +  moreover have "\<mu> (N \<union> N') \<le> \<mu> N + \<mu> N'"
   9.566      using assms by (intro measure_subadditive) auto
   9.567 -  then show "\<mu> (N \<union> N') = 0"
   9.568 -    using assms by auto
   9.569 +  ultimately show "\<mu> (N \<union> N') = 0" using assms by auto
   9.570  qed
   9.571  
   9.572  lemma UN_from_nat: "(\<Union>i. N i) = (\<Union>i. N (Countable.from_nat i))"
   9.573 @@ -669,11 +573,11 @@
   9.574    shows "(\<Union>i. N i) \<in> null_sets"
   9.575  proof (intro conjI CollectI)
   9.576    show "(\<Union>i. N i) \<in> sets M" using assms by auto
   9.577 -  have "\<mu> (\<Union>i. N i) \<le> (\<Sum>\<^isub>\<infinity> n. \<mu> (N (Countable.from_nat n)))"
   9.578 +  then have "0 \<le> \<mu> (\<Union>i. N i)" by simp
   9.579 +  moreover have "\<mu> (\<Union>i. N i) \<le> (\<Sum>n. \<mu> (N (Countable.from_nat n)))"
   9.580      unfolding UN_from_nat[of N]
   9.581      using assms by (intro measure_countably_subadditive) auto
   9.582 -  then show "\<mu> (\<Union>i. N i) = 0"
   9.583 -    using assms by auto
   9.584 +  ultimately show "\<mu> (\<Union>i. N i) = 0" using assms by auto
   9.585  qed
   9.586  
   9.587  lemma (in measure_space) null_sets_finite_UN:
   9.588 @@ -681,10 +585,10 @@
   9.589    shows "(\<Union>i\<in>S. A i) \<in> null_sets"
   9.590  proof (intro CollectI conjI)
   9.591    show "(\<Union>i\<in>S. A i) \<in> sets M" using assms by (intro finite_UN) auto
   9.592 -  have "\<mu> (\<Union>i\<in>S. A i) \<le> (\<Sum>i\<in>S. \<mu> (A i))"
   9.593 +  then have "0 \<le> \<mu> (\<Union>i\<in>S. A i)" by simp
   9.594 +  moreover have "\<mu> (\<Union>i\<in>S. A i) \<le> (\<Sum>i\<in>S. \<mu> (A i))"
   9.595      using assms by (intro measure_finitely_subadditive) auto
   9.596 -  then show "\<mu> (\<Union>i\<in>S. A i) = 0"
   9.597 -    using assms by auto
   9.598 +  ultimately show "\<mu> (\<Union>i\<in>S. A i) = 0" using assms by auto
   9.599  qed
   9.600  
   9.601  lemma (in measure_space) null_set_Int1:
   9.602 @@ -731,6 +635,23 @@
   9.603    almost_everywhere :: "('a \<Rightarrow> bool) \<Rightarrow> bool" (binder "AE " 10) where
   9.604    "almost_everywhere P \<longleftrightarrow> (\<exists>N\<in>null_sets. { x \<in> space M. \<not> P x } \<subseteq> N)"
   9.605  
   9.606 +syntax
   9.607 +  "_almost_everywhere" :: "'a \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" ("AE _ in _. _" [0,0,10] 10)
   9.608 +
   9.609 +translations
   9.610 +  "AE x in M. P" == "CONST measure_space.almost_everywhere M (%x. P)"
   9.611 +
   9.612 +lemma (in measure_space) AE_cong_measure:
   9.613 +  assumes "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A" "sets N = sets M" "space N = space M"
   9.614 +  shows "(AE x in N. P x) \<longleftrightarrow> (AE x. P x)"
   9.615 +proof -
   9.616 +  interpret N: measure_space N
   9.617 +    by (rule measure_space_cong) fact+
   9.618 +  show ?thesis
   9.619 +    unfolding N.almost_everywhere_def almost_everywhere_def
   9.620 +    by (auto simp: assms)
   9.621 +qed
   9.622 +
   9.623  lemma (in measure_space) AE_I':
   9.624    "N \<in> null_sets \<Longrightarrow> {x\<in>space M. \<not> P x} \<subseteq> N \<Longrightarrow> (AE x. P x)"
   9.625    unfolding almost_everywhere_def by auto
   9.626 @@ -741,13 +662,19 @@
   9.627  proof
   9.628    assume "AE x. P x" then obtain N where N: "N \<in> sets M" "?P \<subseteq> N" "\<mu> N = 0"
   9.629      unfolding almost_everywhere_def by auto
   9.630 +  have "0 \<le> \<mu> ?P" using assms by simp
   9.631    moreover have "\<mu> ?P \<le> \<mu> N"
   9.632      using assms N(1,2) by (auto intro: measure_mono)
   9.633 -  ultimately show "?P \<in> null_sets" using assms by auto
   9.634 +  ultimately have "\<mu> ?P = 0" unfolding `\<mu> N = 0` by auto
   9.635 +  then show "?P \<in> null_sets" using assms by simp
   9.636  next
   9.637    assume "?P \<in> null_sets" with assms show "AE x. P x" by (auto intro: AE_I')
   9.638  qed
   9.639  
   9.640 +lemma (in measure_space) AE_iff_measurable:
   9.641 +  "N \<in> sets M \<Longrightarrow> {x\<in>space M. \<not> P x} = N \<Longrightarrow> (AE x. P x) \<longleftrightarrow> \<mu> N = 0"
   9.642 +  using AE_iff_null_set[of P] by simp
   9.643 +
   9.644  lemma (in measure_space) AE_True[intro, simp]: "AE x. True"
   9.645    unfolding almost_everywhere_def by auto
   9.646  
   9.647 @@ -760,13 +687,9 @@
   9.648    assumes "AE x. P x" "{x\<in>space M. P x} \<in> sets M"
   9.649    shows "\<mu> {x\<in>space M. \<not> P x} = 0" (is "\<mu> ?P = 0")
   9.650  proof -
   9.651 -  obtain A where A: "?P \<subseteq> A" "A \<in> sets M" "\<mu> A = 0"
   9.652 -    using assms by (auto elim!: AE_E)
   9.653 -  have "?P = space M - {x\<in>space M. P x}" by auto
   9.654 -  then have "?P \<in> sets M" using assms by auto
   9.655 -  with assms `?P \<subseteq> A` `A \<in> sets M` have "\<mu> ?P \<le> \<mu> A"
   9.656 -    by (auto intro!: measure_mono)
   9.657 -  then show "\<mu> ?P = 0" using A by simp
   9.658 +  have "{x\<in>space M. \<not> P x} = space M - {x\<in>space M. P x}"
   9.659 +    by auto
   9.660 +  with AE_iff_null_set[of P] assms show ?thesis by auto
   9.661  qed
   9.662  
   9.663  lemma (in measure_space) AE_I:
   9.664 @@ -788,8 +711,10 @@
   9.665  
   9.666    show ?thesis
   9.667    proof (intro AE_I)
   9.668 -    show "A \<union> B \<in> sets M" "\<mu> (A \<union> B) = 0" using A B
   9.669 -      using measure_subadditive[of A B] by auto
   9.670 +    have "0 \<le> \<mu> (A \<union> B)" using A B by auto
   9.671 +    moreover have "\<mu> (A \<union> B) \<le> 0"
   9.672 +      using measure_subadditive[of A B] A B by auto
   9.673 +    ultimately show "A \<union> B \<in> sets M" "\<mu> (A \<union> B) = 0" using A B by auto
   9.674      show "{x\<in>space M. \<not> Q x} \<subseteq> A \<union> B"
   9.675        using P imp by auto
   9.676    qed
   9.677 @@ -818,8 +743,8 @@
   9.678    "(\<And>x. x \<in> space M \<Longrightarrow> P x \<longleftrightarrow> Q x) \<Longrightarrow> (AE x. P x) \<longleftrightarrow> (AE x. Q x)"
   9.679    by auto
   9.680  
   9.681 -lemma (in measure_space) all_AE_countable:
   9.682 -  "(\<forall>i::'i::countable. AE x. P i x) \<longleftrightarrow> (AE x. \<forall>i. P i x)"
   9.683 +lemma (in measure_space) AE_all_countable:
   9.684 +  "(AE x. \<forall>i. P i x) \<longleftrightarrow> (\<forall>i::'i::countable. AE x. P i x)"
   9.685  proof
   9.686    assume "\<forall>i. AE x. P i x"
   9.687    from this[unfolded almost_everywhere_def Bex_def, THEN choice]
   9.688 @@ -833,6 +758,10 @@
   9.689      unfolding almost_everywhere_def by auto
   9.690  qed auto
   9.691  
   9.692 +lemma (in measure_space) AE_finite_all:
   9.693 +  assumes f: "finite S" shows "(AE x. \<forall>i\<in>S. P i x) \<longleftrightarrow> (\<forall>i\<in>S. AE x. P i x)"
   9.694 +  using f by induct auto
   9.695 +
   9.696  lemma (in measure_space) restricted_measure_space:
   9.697    assumes "S \<in> sets M"
   9.698    shows "measure_space (restricted_space S)"
   9.699 @@ -840,7 +769,7 @@
   9.700    unfolding measure_space_def measure_space_axioms_def
   9.701  proof safe
   9.702    show "sigma_algebra ?r" using restricted_sigma_algebra[OF assms] .
   9.703 -  show "measure ?r {} = 0" by simp
   9.704 +  show "positive ?r (measure ?r)" using `S \<in> sets M` by (auto simp: positive_def)
   9.705  
   9.706    show "countably_additive ?r (measure ?r)"
   9.707      unfolding countably_additive_def
   9.708 @@ -848,13 +777,38 @@
   9.709      fix A :: "nat \<Rightarrow> 'a set"
   9.710      assume *: "range A \<subseteq> sets ?r" and **: "disjoint_family A"
   9.711      from restriction_in_sets[OF assms *[simplified]] **
   9.712 -    show "(\<Sum>\<^isub>\<infinity> n. measure ?r (A n)) = measure ?r (\<Union>i. A i)"
   9.713 +    show "(\<Sum>n. measure ?r (A n)) = measure ?r (\<Union>i. A i)"
   9.714        using measure_countably_additive by simp
   9.715    qed
   9.716  qed
   9.717  
   9.718 +lemma (in measure_space) AE_restricted:
   9.719 +  assumes "A \<in> sets M"
   9.720 +  shows "(AE x in restricted_space A. P x) \<longleftrightarrow> (AE x. x \<in> A \<longrightarrow> P x)"
   9.721 +proof -
   9.722 +  interpret R: measure_space "restricted_space A"
   9.723 +    by (rule restricted_measure_space[OF `A \<in> sets M`])
   9.724 +  show ?thesis
   9.725 +  proof
   9.726 +    assume "AE x in restricted_space A. P x"
   9.727 +    from this[THEN R.AE_E] guess N' .
   9.728 +    then obtain N where "{x \<in> A. \<not> P x} \<subseteq> A \<inter> N" "\<mu> (A \<inter> N) = 0" "N \<in> sets M"
   9.729 +      by auto
   9.730 +    moreover then have "{x \<in> space M. \<not> (x \<in> A \<longrightarrow> P x)} \<subseteq> A \<inter> N"
   9.731 +      using `A \<in> sets M` sets_into_space by auto
   9.732 +    ultimately show "AE x. x \<in> A \<longrightarrow> P x"
   9.733 +      using `A \<in> sets M` by (auto intro!: AE_I[where N="A \<inter> N"])
   9.734 +  next
   9.735 +    assume "AE x. x \<in> A \<longrightarrow> P x"
   9.736 +    from this[THEN AE_E] guess N .
   9.737 +    then show "AE x in restricted_space A. P x"
   9.738 +      using null_set_Int1[OF _ `A \<in> sets M`] `A \<in> sets M`[THEN sets_into_space]
   9.739 +      by (auto intro!: R.AE_I[where N="A \<inter> N"] simp: subset_eq)
   9.740 +  qed
   9.741 +qed
   9.742 +
   9.743  lemma (in measure_space) measure_space_subalgebra:
   9.744 -  assumes "sigma_algebra N" and [simp]: "sets N \<subseteq> sets M" "space N = space M"
   9.745 +  assumes "sigma_algebra N" and "sets N \<subseteq> sets M" "space N = space M"
   9.746    and measure[simp]: "\<And>X. X \<in> sets N \<Longrightarrow> measure N X = measure M X"
   9.747    shows "measure_space N"
   9.748  proof -
   9.749 @@ -864,13 +818,26 @@
   9.750      from `sets N \<subseteq> sets M` have "\<And>A. range A \<subseteq> sets N \<Longrightarrow> range A \<subseteq> sets M" by blast
   9.751      then show "countably_additive N (measure N)"
   9.752        by (auto intro!: measure_countably_additive simp: countably_additive_def subset_eq)
   9.753 -  qed simp
   9.754 +    show "positive N (measure_space.measure N)"
   9.755 +      using assms(2) by (auto simp add: positive_def)
   9.756 +  qed
   9.757 +qed
   9.758 +
   9.759 +lemma (in measure_space) AE_subalgebra:
   9.760 +  assumes ae: "AE x in N. P x"
   9.761 +  and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> measure N A = \<mu> A"
   9.762 +  and sa: "sigma_algebra N"
   9.763 +  shows "AE x. P x"
   9.764 +proof -
   9.765 +  interpret N: measure_space N using measure_space_subalgebra[OF sa N] .
   9.766 +  from ae[THEN N.AE_E] guess N .
   9.767 +  with N show ?thesis unfolding almost_everywhere_def by auto
   9.768  qed
   9.769  
   9.770  section "@{text \<sigma>}-finite Measures"
   9.771  
   9.772  locale sigma_finite_measure = measure_space +
   9.773 -  assumes sigma_finite: "\<exists>A::nat \<Rightarrow> 'a set. range A \<subseteq> sets M \<and> (\<Union>i. A i) = space M \<and> (\<forall>i. \<mu> (A i) \<noteq> \<omega>)"
   9.774 +  assumes sigma_finite: "\<exists>A::nat \<Rightarrow> 'a set. range A \<subseteq> sets M \<and> (\<Union>i. A i) = space M \<and> (\<forall>i. \<mu> (A i) \<noteq> \<infinity>)"
   9.775  
   9.776  lemma (in sigma_finite_measure) restricted_sigma_finite_measure:
   9.777    assumes "S \<in> sets M"
   9.778 @@ -881,9 +848,9 @@
   9.779    show "measure_space ?r" using restricted_measure_space[OF assms] .
   9.780  next
   9.781    obtain A :: "nat \<Rightarrow> 'a set" where
   9.782 -      "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. \<mu> (A i) \<noteq> \<omega>"
   9.783 +      "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. \<mu> (A i) \<noteq> \<infinity>"
   9.784      using sigma_finite by auto
   9.785 -  show "\<exists>A::nat \<Rightarrow> 'a set. range A \<subseteq> sets ?r \<and> (\<Union>i. A i) = space ?r \<and> (\<forall>i. measure ?r (A i) \<noteq> \<omega>)"
   9.786 +  show "\<exists>A::nat \<Rightarrow> 'a set. range A \<subseteq> sets ?r \<and> (\<Union>i. A i) = space ?r \<and> (\<forall>i. measure ?r (A i) \<noteq> \<infinity>)"
   9.787    proof (safe intro!: exI[of _ "\<lambda>i. A i \<inter> S"] del: notI)
   9.788      fix i
   9.789      show "A i \<inter> S \<in> sets ?r"
   9.790 @@ -897,8 +864,7 @@
   9.791      fix i
   9.792      have "\<mu> (A i \<inter> S) \<le> \<mu> (A i)"
   9.793        using `range A \<subseteq> sets M` `S \<in> sets M` by (auto intro!: measure_mono)
   9.794 -    also have "\<dots> < \<omega>" using `\<mu> (A i) \<noteq> \<omega>` by (auto simp: pextreal_less_\<omega>)
   9.795 -    finally show "measure ?r (A i \<inter> S) \<noteq> \<omega>" by (auto simp: pextreal_less_\<omega>)
   9.796 +    then show "measure ?r (A i \<inter> S) \<noteq> \<infinity>" using `\<mu> (A i) \<noteq> \<infinity>` by auto
   9.797    qed
   9.798  qed
   9.799  
   9.800 @@ -909,7 +875,7 @@
   9.801    interpret M': measure_space M' by (intro measure_space_cong cong)
   9.802    from sigma_finite guess A .. note A = this
   9.803    then have "\<And>i. A i \<in> sets M" by auto
   9.804 -  with A have fin: "(\<forall>i. measure M' (A i) \<noteq> \<omega>)" using cong by auto
   9.805 +  with A have fin: "\<forall>i. measure M' (A i) \<noteq> \<infinity>" using cong by auto
   9.806    show ?thesis
   9.807      apply default
   9.808      using A fin cong by auto
   9.809 @@ -917,30 +883,30 @@
   9.810  
   9.811  lemma (in sigma_finite_measure) disjoint_sigma_finite:
   9.812    "\<exists>A::nat\<Rightarrow>'a set. range A \<subseteq> sets M \<and> (\<Union>i. A i) = space M \<and>
   9.813 -    (\<forall>i. \<mu> (A i) \<noteq> \<omega>) \<and> disjoint_family A"
   9.814 +    (\<forall>i. \<mu> (A i) \<noteq> \<infinity>) \<and> disjoint_family A"
   9.815  proof -
   9.816    obtain A :: "nat \<Rightarrow> 'a set" where
   9.817      range: "range A \<subseteq> sets M" and
   9.818      space: "(\<Union>i. A i) = space M" and
   9.819 -    measure: "\<And>i. \<mu> (A i) \<noteq> \<omega>"
   9.820 +    measure: "\<And>i. \<mu> (A i) \<noteq> \<infinity>"
   9.821      using sigma_finite by auto
   9.822    note range' = range_disjointed_sets[OF range] range
   9.823    { fix i
   9.824      have "\<mu> (disjointed A i) \<le> \<mu> (A i)"
   9.825        using range' disjointed_subset[of A i] by (auto intro!: measure_mono)
   9.826 -    then have "\<mu> (disjointed A i) \<noteq> \<omega>"
   9.827 +    then have "\<mu> (disjointed A i) \<noteq> \<infinity>"
   9.828        using measure[of i] by auto }
   9.829    with disjoint_family_disjointed UN_disjointed_eq[of A] space range'
   9.830    show ?thesis by (auto intro!: exI[of _ "disjointed A"])
   9.831  qed
   9.832  
   9.833  lemma (in sigma_finite_measure) sigma_finite_up:
   9.834 -  "\<exists>F. range F \<subseteq> sets M \<and> F \<up> space M \<and> (\<forall>i. \<mu> (F i) \<noteq> \<omega>)"
   9.835 +  "\<exists>F. range F \<subseteq> sets M \<and> incseq F \<and> (\<Union>i. F i) = space M \<and> (\<forall>i. \<mu> (F i) \<noteq> \<infinity>)"
   9.836  proof -
   9.837    obtain F :: "nat \<Rightarrow> 'a set" where
   9.838 -    F: "range F \<subseteq> sets M" "(\<Union>i. F i) = space M" "\<And>i. \<mu> (F i) \<noteq> \<omega>"
   9.839 +    F: "range F \<subseteq> sets M" "(\<Union>i. F i) = space M" "\<And>i. \<mu> (F i) \<noteq> \<infinity>"
   9.840      using sigma_finite by auto
   9.841 -  then show ?thesis unfolding isoton_def
   9.842 +  then show ?thesis
   9.843    proof (intro exI[of _ "\<lambda>n. \<Union>i\<le>n. F i"] conjI allI)
   9.844      from F have "\<And>x. x \<in> space M \<Longrightarrow> \<exists>i. x \<in> F i" by auto
   9.845      then show "(\<Union>n. \<Union> i\<le>n. F i) = space M"
   9.846 @@ -949,16 +915,16 @@
   9.847      fix n
   9.848      have "\<mu> (\<Union> i\<le>n. F i) \<le> (\<Sum>i\<le>n. \<mu> (F i))" using F
   9.849        by (auto intro!: measure_finitely_subadditive)
   9.850 -    also have "\<dots> < \<omega>"
   9.851 -      using F by (auto simp: setsum_\<omega>)
   9.852 -    finally show "\<mu> (\<Union> i\<le>n. F i) \<noteq> \<omega>" by simp
   9.853 -  qed force+
   9.854 +    also have "\<dots> < \<infinity>"
   9.855 +      using F by (auto simp: setsum_Pinfty)
   9.856 +    finally show "\<mu> (\<Union> i\<le>n. F i) \<noteq> \<infinity>" by simp
   9.857 +  qed (force simp: incseq_def)+
   9.858  qed
   9.859  
   9.860  section {* Measure preserving *}
   9.861  
   9.862  definition "measure_preserving A B =