merged
authorAndreas Lochbihler
Wed Oct 10 15:17:18 2012 +0200 (2012-10-10)
changeset 4980939db47ed6d54
parent 49808 418991ce7567
parent 49805 9a2a53be24a2
child 49810 53f14f62cca2
merged
     1.1 --- a/src/HOL/Probability/Binary_Product_Measure.thy	Wed Oct 10 15:16:44 2012 +0200
     1.2 +++ b/src/HOL/Probability/Binary_Product_Measure.thy	Wed Oct 10 15:17:18 2012 +0200
     1.3 @@ -33,15 +33,22 @@
     1.4        {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}
     1.5        (\<lambda>X. \<integral>\<^isup>+x. (\<integral>\<^isup>+y. indicator X (x,y) \<partial>B) \<partial>A)"
     1.6  
     1.7 +lemma pair_measure_closed: "{a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B} \<subseteq> Pow (space A \<times> space B)"
     1.8 +  using space_closed[of A] space_closed[of B] by auto
     1.9 +
    1.10  lemma space_pair_measure:
    1.11    "space (A \<Otimes>\<^isub>M B) = space A \<times> space B"
    1.12 -  unfolding pair_measure_def using space_closed[of A] space_closed[of B]
    1.13 -  by (intro space_measure_of) auto
    1.14 +  unfolding pair_measure_def using pair_measure_closed[of A B]
    1.15 +  by (rule space_measure_of)
    1.16  
    1.17  lemma sets_pair_measure:
    1.18    "sets (A \<Otimes>\<^isub>M B) = sigma_sets (space A \<times> space B) {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}"
    1.19 -  unfolding pair_measure_def using space_closed[of A] space_closed[of B]
    1.20 -  by (intro sets_measure_of) auto
    1.21 +  unfolding pair_measure_def using pair_measure_closed[of A B]
    1.22 +  by (rule sets_measure_of)
    1.23 +
    1.24 +lemma sets_pair_measure_cong[cong]:
    1.25 +  "sets M1 = sets M1' \<Longrightarrow> sets M2 = sets M2' \<Longrightarrow> sets (M1 \<Otimes>\<^isub>M M2) = sets (M1' \<Otimes>\<^isub>M M2')"
    1.26 +  unfolding sets_pair_measure by (simp cong: sets_eq_imp_space_eq)
    1.27  
    1.28  lemma pair_measureI[intro, simp]:
    1.29    "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^isub>M B)"
    1.30 @@ -54,21 +61,30 @@
    1.31    unfolding pair_measure_def using 1 2
    1.32    by (intro measurable_measure_of) (auto dest: sets_into_space)
    1.33  
    1.34 +lemma measurable_Pair:
    1.35 +  assumes f: "f \<in> measurable M M1" and g: "g \<in> measurable M M2"
    1.36 +  shows "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^isub>M M2)"
    1.37 +proof (rule measurable_pair_measureI)
    1.38 +  show "(\<lambda>x. (f x, g x)) \<in> space M \<rightarrow> space M1 \<times> space M2"
    1.39 +    using f g by (auto simp: measurable_def)
    1.40 +  fix A B assume *: "A \<in> sets M1" "B \<in> sets M2"
    1.41 +  have "(\<lambda>x. (f x, g x)) -` (A \<times> B) \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
    1.42 +    by auto
    1.43 +  also have "\<dots> \<in> sets M"
    1.44 +    by (rule Int) (auto intro!: measurable_sets * f g)
    1.45 +  finally show "(\<lambda>x. (f x, g x)) -` (A \<times> B) \<inter> space M \<in> sets M" .
    1.46 +qed
    1.47 +
    1.48 +lemma measurable_pair:
    1.49 +  assumes "(fst \<circ> f) \<in> measurable M M1" "(snd \<circ> f) \<in> measurable M M2"
    1.50 +  shows "f \<in> measurable M (M1 \<Otimes>\<^isub>M M2)"
    1.51 +  using measurable_Pair[OF assms] by simp
    1.52 +
    1.53  lemma measurable_fst[intro!, simp]: "fst \<in> measurable (M1 \<Otimes>\<^isub>M M2) M1"
    1.54 -  unfolding measurable_def
    1.55 -proof safe
    1.56 -  fix A assume A: "A \<in> sets M1"
    1.57 -  from this[THEN sets_into_space] have "fst -` A \<inter> space M1 \<times> space M2 = A \<times> space M2" by auto
    1.58 -  with A show "fst -` A \<inter> space (M1 \<Otimes>\<^isub>M M2) \<in> sets (M1 \<Otimes>\<^isub>M M2)" by (simp add: space_pair_measure)
    1.59 -qed (simp add: space_pair_measure)
    1.60 +  by (auto simp: fst_vimage_eq_Times space_pair_measure sets_into_space times_Int_times measurable_def)
    1.61  
    1.62  lemma measurable_snd[intro!, simp]: "snd \<in> measurable (M1 \<Otimes>\<^isub>M M2) M2"
    1.63 -  unfolding measurable_def
    1.64 -proof safe
    1.65 -  fix A assume A: "A \<in> sets M2"
    1.66 -  from this[THEN sets_into_space] have "snd -` A \<inter> space M1 \<times> space M2 = space M1 \<times> A" by auto
    1.67 -  with A show "snd -` A \<inter> space (M1 \<Otimes>\<^isub>M M2) \<in> sets (M1 \<Otimes>\<^isub>M M2)" by (simp add: space_pair_measure)
    1.68 -qed (simp add: space_pair_measure)
    1.69 +  by (auto simp: snd_vimage_eq_Times space_pair_measure sets_into_space times_Int_times measurable_def)
    1.70  
    1.71  lemma measurable_fst': "f \<in> measurable M (N \<Otimes>\<^isub>M P) \<Longrightarrow> (\<lambda>x. fst (f x)) \<in> measurable M N"
    1.72    using measurable_comp[OF _ measurable_fst] by (auto simp: comp_def)
    1.73 @@ -84,55 +100,29 @@
    1.74  
    1.75  lemma measurable_pair_iff:
    1.76    "f \<in> measurable M (M1 \<Otimes>\<^isub>M M2) \<longleftrightarrow> (fst \<circ> f) \<in> measurable M M1 \<and> (snd \<circ> f) \<in> measurable M M2"
    1.77 -proof safe
    1.78 -  assume f: "(fst \<circ> f) \<in> measurable M M1" and s: "(snd \<circ> f) \<in> measurable M M2"
    1.79 -  show "f \<in> measurable M (M1 \<Otimes>\<^isub>M M2)"
    1.80 -  proof (rule measurable_pair_measureI)
    1.81 -    show "f \<in> space M \<rightarrow> space M1 \<times> space M2"
    1.82 -      using f s by (auto simp: mem_Times_iff measurable_def comp_def)
    1.83 -    fix A B assume "A \<in> sets M1" "B \<in> sets M2"
    1.84 -    moreover have "(fst \<circ> f) -` A \<inter> space M \<in> sets M" "(snd \<circ> f) -` B \<inter> space M \<in> sets M"
    1.85 -      using f `A \<in> sets M1` s `B \<in> sets M2` by (auto simp: measurable_sets)
    1.86 -    moreover have "f -` (A \<times> B) \<inter> space M = ((fst \<circ> f) -` A \<inter> space M) \<inter> ((snd \<circ> f) -` B \<inter> space M)"
    1.87 -      by (auto simp: vimage_Times)
    1.88 -    ultimately show "f -` (A \<times> B) \<inter> space M \<in> sets M" by auto
    1.89 -  qed
    1.90 -qed auto
    1.91 +  using measurable_pair[of f M M1 M2] by auto
    1.92  
    1.93 -lemma measurable_pair:
    1.94 -  "(fst \<circ> f) \<in> measurable M M1 \<Longrightarrow> (snd \<circ> f) \<in> measurable M M2 \<Longrightarrow> f \<in> measurable M (M1 \<Otimes>\<^isub>M M2)"
    1.95 -  unfolding measurable_pair_iff by simp
    1.96 +lemma measurable_split_conv:
    1.97 +  "(\<lambda>(x, y). f x y) \<in> measurable A B \<longleftrightarrow> (\<lambda>x. f (fst x) (snd x)) \<in> measurable A B"
    1.98 +  by (intro arg_cong2[where f="op \<in>"]) auto
    1.99  
   1.100  lemma measurable_pair_swap': "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^isub>M M2) (M2 \<Otimes>\<^isub>M M1)"
   1.101 -proof (rule measurable_pair_measureI)
   1.102 -  fix A B assume "A \<in> sets M2" "B \<in> sets M1"
   1.103 -  moreover then have "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space (M1 \<Otimes>\<^isub>M M2) = B \<times> A"
   1.104 -    by (auto dest: sets_into_space simp: space_pair_measure)
   1.105 -  ultimately show "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space (M1 \<Otimes>\<^isub>M M2) \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   1.106 -    by auto
   1.107 -qed (auto simp add: space_pair_measure)
   1.108 +  by (auto intro!: measurable_Pair simp: measurable_split_conv)
   1.109  
   1.110  lemma measurable_pair_swap:
   1.111    assumes f: "f \<in> measurable (M1 \<Otimes>\<^isub>M M2) M" shows "(\<lambda>(x,y). f (y, x)) \<in> measurable (M2 \<Otimes>\<^isub>M M1) M"
   1.112 -proof -
   1.113 -  have "(\<lambda>x. f (case x of (x, y) \<Rightarrow> (y, x))) = (\<lambda>(x, y). f (y, x))" by auto
   1.114 -  then show ?thesis
   1.115 -    using measurable_comp[OF measurable_pair_swap' f] by (simp add: comp_def)
   1.116 -qed
   1.117 +  using measurable_comp[OF measurable_Pair f] by (auto simp: measurable_split_conv comp_def)
   1.118  
   1.119  lemma measurable_pair_swap_iff:
   1.120    "f \<in> measurable (M2 \<Otimes>\<^isub>M M1) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable (M1 \<Otimes>\<^isub>M M2) M"
   1.121    using measurable_pair_swap[of "\<lambda>(x,y). f (y, x)"]
   1.122    by (auto intro!: measurable_pair_swap)
   1.123  
   1.124 +lemma measurable_ident[intro, simp]: "(\<lambda>x. x) \<in> measurable M M"
   1.125 +  unfolding measurable_def by auto
   1.126 +
   1.127  lemma measurable_Pair1': "x \<in> space M1 \<Longrightarrow> Pair x \<in> measurable M2 (M1 \<Otimes>\<^isub>M M2)"
   1.128 -proof (rule measurable_pair_measureI)
   1.129 -  fix A B assume "A \<in> sets M1" "B \<in> sets M2"
   1.130 -  moreover then have "Pair x -` (A \<times> B) \<inter> space M2 = (if x \<in> A then B else {})"
   1.131 -    by (auto dest: sets_into_space simp: space_pair_measure)
   1.132 -  ultimately show "Pair x -` (A \<times> B) \<inter> space M2 \<in> sets M2"
   1.133 -    by auto
   1.134 -qed (auto simp add: space_pair_measure)
   1.135 +  by (auto intro!: measurable_Pair)
   1.136  
   1.137  lemma sets_Pair1: assumes A: "A \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "Pair x -` A \<in> sets M2"
   1.138  proof -
   1.139 @@ -144,8 +134,7 @@
   1.140  qed
   1.141  
   1.142  lemma measurable_Pair2': "y \<in> space M2 \<Longrightarrow> (\<lambda>x. (x, y)) \<in> measurable M1 (M1 \<Otimes>\<^isub>M M2)"
   1.143 -  using measurable_comp[OF measurable_Pair1' measurable_pair_swap', of y M2 M1]
   1.144 -  by (simp add: comp_def)
   1.145 +  by (auto intro!: measurable_Pair)
   1.146  
   1.147  lemma sets_Pair2: assumes A: "A \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "(\<lambda>x. (x, y)) -` A \<in> sets M1"
   1.148  proof -
   1.149 @@ -172,46 +161,115 @@
   1.150    unfolding Int_stable_def
   1.151    by safe (auto simp add: times_Int_times)
   1.152  
   1.153 -lemma finite_measure_cut_measurable:
   1.154 -  assumes "sigma_finite_measure M1" "finite_measure M2"
   1.155 -  assumes "Q \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   1.156 -  shows "(\<lambda>x. emeasure M2 (Pair x -` Q)) \<in> borel_measurable M1"
   1.157 +lemma (in finite_measure) finite_measure_cut_measurable:
   1.158 +  assumes "Q \<in> sets (N \<Otimes>\<^isub>M M)"
   1.159 +  shows "(\<lambda>x. emeasure M (Pair x -` Q)) \<in> borel_measurable N"
   1.160      (is "?s Q \<in> _")
   1.161 +  using Int_stable_pair_measure_generator pair_measure_closed assms
   1.162 +  unfolding sets_pair_measure
   1.163 +proof (induct rule: sigma_sets_induct_disjoint)
   1.164 +  case (compl A)
   1.165 +  with sets_into_space have "\<And>x. emeasure M (Pair x -` ((space N \<times> space M) - A)) =
   1.166 +      (if x \<in> space N then emeasure M (space M) - ?s A x else 0)"
   1.167 +    unfolding sets_pair_measure[symmetric]
   1.168 +    by (auto intro!: emeasure_compl simp: vimage_Diff sets_Pair1)
   1.169 +  with compl top show ?case
   1.170 +    by (auto intro!: measurable_If simp: space_pair_measure)
   1.171 +next
   1.172 +  case (union F)
   1.173 +  moreover then have "\<And>x. emeasure M (\<Union>i. Pair x -` F i) = (\<Sum>i. ?s (F i) x)"
   1.174 +    unfolding sets_pair_measure[symmetric]
   1.175 +    by (intro suminf_emeasure[symmetric]) (auto simp: disjoint_family_on_def sets_Pair1)
   1.176 +  ultimately show ?case
   1.177 +    by (auto simp: vimage_UN)
   1.178 +qed (auto simp add: if_distrib Int_def[symmetric] intro!: measurable_If)
   1.179 +
   1.180 +lemma (in sigma_finite_measure) measurable_emeasure_Pair:
   1.181 +  assumes Q: "Q \<in> sets (N \<Otimes>\<^isub>M M)" shows "(\<lambda>x. emeasure M (Pair x -` Q)) \<in> borel_measurable N" (is "?s Q \<in> _")
   1.182  proof -
   1.183 -  interpret M1: sigma_finite_measure M1 by fact
   1.184 -  interpret M2: finite_measure M2 by fact
   1.185 -  let ?\<Omega> = "space M1 \<times> space M2" and ?D = "{A\<in>sets (M1 \<Otimes>\<^isub>M M2). ?s A \<in> borel_measurable M1}"
   1.186 -  note space_pair_measure[simp]
   1.187 -  interpret dynkin_system ?\<Omega> ?D
   1.188 -  proof (intro dynkin_systemI)
   1.189 -    fix A assume "A \<in> ?D" then show "A \<subseteq> ?\<Omega>"
   1.190 -      using sets_into_space[of A "M1 \<Otimes>\<^isub>M M2"] by simp
   1.191 -  next
   1.192 -    from top show "?\<Omega> \<in> ?D"
   1.193 -      by (auto simp add: if_distrib intro!: measurable_If)
   1.194 -  next
   1.195 -    fix A assume "A \<in> ?D"
   1.196 -    with sets_into_space have "\<And>x. emeasure M2 (Pair x -` (?\<Omega> - A)) =
   1.197 -        (if x \<in> space M1 then emeasure M2 (space M2) - ?s A x else 0)"
   1.198 -      by (auto intro!: emeasure_compl simp: vimage_Diff sets_Pair1)
   1.199 -    with `A \<in> ?D` top show "?\<Omega> - A \<in> ?D"
   1.200 -      by (auto intro!: measurable_If)
   1.201 -  next
   1.202 -    fix F :: "nat \<Rightarrow> ('a\<times>'b) set" assume "disjoint_family F" "range F \<subseteq> ?D"
   1.203 -    moreover then have "\<And>x. emeasure M2 (\<Union>i. Pair x -` F i) = (\<Sum>i. ?s (F i) x)"
   1.204 -      by (intro suminf_emeasure[symmetric]) (auto simp: disjoint_family_on_def sets_Pair1)
   1.205 -    ultimately show "(\<Union>i. F i) \<in> ?D"
   1.206 -      by (auto simp: vimage_UN intro!: borel_measurable_psuminf)
   1.207 +  from sigma_finite_disjoint guess F . note F = this
   1.208 +  then have F_sets: "\<And>i. F i \<in> sets M" by auto
   1.209 +  let ?C = "\<lambda>x i. F i \<inter> Pair x -` Q"
   1.210 +  { fix i
   1.211 +    have [simp]: "space N \<times> F i \<inter> space N \<times> space M = space N \<times> F i"
   1.212 +      using F sets_into_space by auto
   1.213 +    let ?R = "density M (indicator (F i))"
   1.214 +    have "finite_measure ?R"
   1.215 +      using F by (intro finite_measureI) (auto simp: emeasure_restricted subset_eq)
   1.216 +    then have "(\<lambda>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))) \<in> borel_measurable N"
   1.217 +     by (rule finite_measure.finite_measure_cut_measurable) (auto intro: Q)
   1.218 +    moreover have "\<And>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))
   1.219 +        = emeasure M (F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q))"
   1.220 +      using Q F_sets by (intro emeasure_restricted) (auto intro: sets_Pair1)
   1.221 +    moreover have "\<And>x. F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q) = ?C x i"
   1.222 +      using sets_into_space[OF Q] by (auto simp: space_pair_measure)
   1.223 +    ultimately have "(\<lambda>x. emeasure M (?C x i)) \<in> borel_measurable N"
   1.224 +      by simp }
   1.225 +  moreover
   1.226 +  { fix x
   1.227 +    have "(\<Sum>i. emeasure M (?C x i)) = emeasure M (\<Union>i. ?C x i)"
   1.228 +    proof (intro suminf_emeasure)
   1.229 +      show "range (?C x) \<subseteq> sets M"
   1.230 +        using F `Q \<in> sets (N \<Otimes>\<^isub>M M)` by (auto intro!: sets_Pair1)
   1.231 +      have "disjoint_family F" using F by auto
   1.232 +      show "disjoint_family (?C x)"
   1.233 +        by (rule disjoint_family_on_bisimulation[OF `disjoint_family F`]) auto
   1.234 +    qed
   1.235 +    also have "(\<Union>i. ?C x i) = Pair x -` Q"
   1.236 +      using F sets_into_space[OF `Q \<in> sets (N \<Otimes>\<^isub>M M)`]
   1.237 +      by (auto simp: space_pair_measure)
   1.238 +    finally have "emeasure M (Pair x -` Q) = (\<Sum>i. emeasure M (?C x i))"
   1.239 +      by simp }
   1.240 +  ultimately show ?thesis using `Q \<in> sets (N \<Otimes>\<^isub>M M)` F_sets
   1.241 +    by auto
   1.242 +qed
   1.243 +
   1.244 +lemma (in sigma_finite_measure) emeasure_pair_measure:
   1.245 +  assumes "X \<in> sets (N \<Otimes>\<^isub>M M)"
   1.246 +  shows "emeasure (N \<Otimes>\<^isub>M M) X = (\<integral>\<^isup>+ x. \<integral>\<^isup>+ y. indicator X (x, y) \<partial>M \<partial>N)" (is "_ = ?\<mu> X")
   1.247 +proof (rule emeasure_measure_of[OF pair_measure_def])
   1.248 +  show "positive (sets (N \<Otimes>\<^isub>M M)) ?\<mu>"
   1.249 +    by (auto simp: positive_def positive_integral_positive)
   1.250 +  have eq[simp]: "\<And>A x y. indicator A (x, y) = indicator (Pair x -` A) y"
   1.251 +    by (auto simp: indicator_def)
   1.252 +  show "countably_additive (sets (N \<Otimes>\<^isub>M M)) ?\<mu>"
   1.253 +  proof (rule countably_additiveI)
   1.254 +    fix F :: "nat \<Rightarrow> ('b \<times> 'a) set" assume F: "range F \<subseteq> sets (N \<Otimes>\<^isub>M M)" "disjoint_family F"
   1.255 +    from F have *: "\<And>i. F i \<in> sets (N \<Otimes>\<^isub>M M)" "(\<Union>i. F i) \<in> sets (N \<Otimes>\<^isub>M M)" by auto
   1.256 +    moreover from F have "\<And>i. (\<lambda>x. emeasure M (Pair x -` F i)) \<in> borel_measurable N"
   1.257 +      by (intro measurable_emeasure_Pair) auto
   1.258 +    moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   1.259 +      by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   1.260 +    moreover have "\<And>x. range (\<lambda>i. Pair x -` F i) \<subseteq> sets M"
   1.261 +      using F by (auto simp: sets_Pair1)
   1.262 +    ultimately show "(\<Sum>n. ?\<mu> (F n)) = ?\<mu> (\<Union>i. F i)"
   1.263 +      by (auto simp add: vimage_UN positive_integral_suminf[symmetric] suminf_emeasure subset_eq emeasure_nonneg sets_Pair1
   1.264 +               intro!: positive_integral_cong positive_integral_indicator[symmetric])
   1.265    qed
   1.266 -  let ?G = "{a \<times> b | a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   1.267 -  have "sigma_sets ?\<Omega> ?G = ?D"
   1.268 -  proof (rule dynkin_lemma)
   1.269 -    show "?G \<subseteq> ?D"
   1.270 -      by (auto simp: if_distrib Int_def[symmetric] intro!: measurable_If)
   1.271 -  qed (auto simp: sets_pair_measure  Int_stable_pair_measure_generator)
   1.272 -  with `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)` have "Q \<in> ?D"
   1.273 -    by (simp add: sets_pair_measure[symmetric])
   1.274 -  then show "?s Q \<in> borel_measurable M1" by simp
   1.275 +  show "{a \<times> b |a b. a \<in> sets N \<and> b \<in> sets M} \<subseteq> Pow (space N \<times> space M)"
   1.276 +    using space_closed[of N] space_closed[of M] by auto
   1.277 +qed fact
   1.278 +
   1.279 +lemma (in sigma_finite_measure) emeasure_pair_measure_alt:
   1.280 +  assumes X: "X \<in> sets (N \<Otimes>\<^isub>M M)"
   1.281 +  shows "emeasure (N  \<Otimes>\<^isub>M M) X = (\<integral>\<^isup>+x. emeasure M (Pair x -` X) \<partial>N)"
   1.282 +proof -
   1.283 +  have [simp]: "\<And>x y. indicator X (x, y) = indicator (Pair x -` X) y"
   1.284 +    by (auto simp: indicator_def)
   1.285 +  show ?thesis
   1.286 +    using X by (auto intro!: positive_integral_cong simp: emeasure_pair_measure sets_Pair1)
   1.287 +qed
   1.288 +
   1.289 +lemma (in sigma_finite_measure) emeasure_pair_measure_Times:
   1.290 +  assumes A: "A \<in> sets N" and B: "B \<in> sets M"
   1.291 +  shows "emeasure (N \<Otimes>\<^isub>M M) (A \<times> B) = emeasure N A * emeasure M B"
   1.292 +proof -
   1.293 +  have "emeasure (N \<Otimes>\<^isub>M M) (A \<times> B) = (\<integral>\<^isup>+x. emeasure M B * indicator A x \<partial>N)"
   1.294 +    using A B by (auto intro!: positive_integral_cong simp: emeasure_pair_measure_alt)
   1.295 +  also have "\<dots> = emeasure M B * emeasure N A"
   1.296 +    using A by (simp add: emeasure_nonneg positive_integral_cmult_indicator)
   1.297 +  finally show ?thesis
   1.298 +    by (simp add: ac_simps)
   1.299  qed
   1.300  
   1.301  subsection {* Binary products of $\sigma$-finite emeasure spaces *}
   1.302 @@ -219,114 +277,21 @@
   1.303  locale pair_sigma_finite = M1: sigma_finite_measure M1 + M2: sigma_finite_measure M2
   1.304    for M1 :: "'a measure" and M2 :: "'b measure"
   1.305  
   1.306 -lemma sets_pair_measure_cong[cong]:
   1.307 -  "sets M1 = sets M1' \<Longrightarrow> sets M2 = sets M2' \<Longrightarrow> sets (M1 \<Otimes>\<^isub>M M2) = sets (M1' \<Otimes>\<^isub>M M2')"
   1.308 -  unfolding sets_pair_measure by (simp cong: sets_eq_imp_space_eq)
   1.309 -
   1.310  lemma (in pair_sigma_finite) measurable_emeasure_Pair1:
   1.311 -  assumes Q: "Q \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "(\<lambda>x. emeasure M2 (Pair x -` Q)) \<in> borel_measurable M1" (is "?s Q \<in> _")
   1.312 -proof -
   1.313 -  from M2.sigma_finite_disjoint guess F . note F = this
   1.314 -  then have F_sets: "\<And>i. F i \<in> sets M2" by auto
   1.315 -  have M1: "sigma_finite_measure M1" ..
   1.316 -  let ?C = "\<lambda>x i. F i \<inter> Pair x -` Q"
   1.317 -  { fix i
   1.318 -    have [simp]: "space M1 \<times> F i \<inter> space M1 \<times> space M2 = space M1 \<times> F i"
   1.319 -      using F sets_into_space by auto
   1.320 -    let ?R = "density M2 (indicator (F i))"
   1.321 -    have "(\<lambda>x. emeasure ?R (Pair x -` (space M1 \<times> space ?R \<inter> Q))) \<in> borel_measurable M1"
   1.322 -    proof (intro finite_measure_cut_measurable[OF M1])
   1.323 -      show "finite_measure ?R"
   1.324 -        using F by (intro finite_measureI) (auto simp: emeasure_restricted subset_eq)
   1.325 -      show "(space M1 \<times> space ?R) \<inter> Q \<in> sets (M1 \<Otimes>\<^isub>M ?R)"
   1.326 -        using Q by (simp add: Int)
   1.327 -    qed
   1.328 -    moreover have "\<And>x. emeasure ?R (Pair x -` (space M1 \<times> space ?R \<inter> Q))
   1.329 -        = emeasure M2 (F i \<inter> Pair x -` (space M1 \<times> space ?R \<inter> Q))"
   1.330 -      using Q F_sets by (intro emeasure_restricted) (auto intro: sets_Pair1)
   1.331 -    moreover have "\<And>x. F i \<inter> Pair x -` (space M1 \<times> space ?R \<inter> Q) = ?C x i"
   1.332 -      using sets_into_space[OF Q] by (auto simp: space_pair_measure)
   1.333 -    ultimately have "(\<lambda>x. emeasure M2 (?C x i)) \<in> borel_measurable M1"
   1.334 -      by simp }
   1.335 -  moreover
   1.336 -  { fix x
   1.337 -    have "(\<Sum>i. emeasure M2 (?C x i)) = emeasure M2 (\<Union>i. ?C x i)"
   1.338 -    proof (intro suminf_emeasure)
   1.339 -      show "range (?C x) \<subseteq> sets M2"
   1.340 -        using F `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)` by (auto intro!: sets_Pair1)
   1.341 -      have "disjoint_family F" using F by auto
   1.342 -      show "disjoint_family (?C x)"
   1.343 -        by (rule disjoint_family_on_bisimulation[OF `disjoint_family F`]) auto
   1.344 -    qed
   1.345 -    also have "(\<Union>i. ?C x i) = Pair x -` Q"
   1.346 -      using F sets_into_space[OF `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)`]
   1.347 -      by (auto simp: space_pair_measure)
   1.348 -    finally have "emeasure M2 (Pair x -` Q) = (\<Sum>i. emeasure M2 (?C x i))"
   1.349 -      by simp }
   1.350 -  ultimately show ?thesis using `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)` F_sets
   1.351 -    by auto
   1.352 -qed
   1.353 +  "Q \<in> sets (M1 \<Otimes>\<^isub>M M2) \<Longrightarrow> (\<lambda>x. emeasure M2 (Pair x -` Q)) \<in> borel_measurable M1"
   1.354 +  using M2.measurable_emeasure_Pair .
   1.355  
   1.356  lemma (in pair_sigma_finite) measurable_emeasure_Pair2:
   1.357    assumes Q: "Q \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "(\<lambda>y. emeasure M1 ((\<lambda>x. (x, y)) -` Q)) \<in> borel_measurable M2"
   1.358  proof -
   1.359 -  interpret Q: pair_sigma_finite M2 M1 by default
   1.360    have "(\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^isub>M M1) \<in> sets (M2 \<Otimes>\<^isub>M M1)"
   1.361      using Q measurable_pair_swap' by (auto intro: measurable_sets)
   1.362 -  note Q.measurable_emeasure_Pair1[OF this]
   1.363 +  note M1.measurable_emeasure_Pair[OF this]
   1.364    moreover have "\<And>y. Pair y -` ((\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^isub>M M1)) = (\<lambda>x. (x, y)) -` Q"
   1.365      using Q[THEN sets_into_space] by (auto simp: space_pair_measure)
   1.366    ultimately show ?thesis by simp
   1.367  qed
   1.368  
   1.369 -lemma (in pair_sigma_finite) emeasure_pair_measure:
   1.370 -  assumes "X \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   1.371 -  shows "emeasure (M1 \<Otimes>\<^isub>M M2) X = (\<integral>\<^isup>+ x. \<integral>\<^isup>+ y. indicator X (x, y) \<partial>M2 \<partial>M1)" (is "_ = ?\<mu> X")
   1.372 -proof (rule emeasure_measure_of[OF pair_measure_def])
   1.373 -  show "positive (sets (M1 \<Otimes>\<^isub>M M2)) ?\<mu>"
   1.374 -    by (auto simp: positive_def positive_integral_positive)
   1.375 -  have eq[simp]: "\<And>A x y. indicator A (x, y) = indicator (Pair x -` A) y"
   1.376 -    by (auto simp: indicator_def)
   1.377 -  show "countably_additive (sets (M1 \<Otimes>\<^isub>M M2)) ?\<mu>"
   1.378 -  proof (rule countably_additiveI)
   1.379 -    fix F :: "nat \<Rightarrow> ('a \<times> 'b) set" assume F: "range F \<subseteq> sets (M1 \<Otimes>\<^isub>M M2)" "disjoint_family F"
   1.380 -    from F have *: "\<And>i. F i \<in> sets (M1 \<Otimes>\<^isub>M M2)" "(\<Union>i. F i) \<in> sets (M1 \<Otimes>\<^isub>M M2)" by auto
   1.381 -    moreover from F have "\<And>i. (\<lambda>x. emeasure M2 (Pair x -` F i)) \<in> borel_measurable M1"
   1.382 -      by (intro measurable_emeasure_Pair1) auto
   1.383 -    moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   1.384 -      by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   1.385 -    moreover have "\<And>x. range (\<lambda>i. Pair x -` F i) \<subseteq> sets M2"
   1.386 -      using F by (auto simp: sets_Pair1)
   1.387 -    ultimately show "(\<Sum>n. ?\<mu> (F n)) = ?\<mu> (\<Union>i. F i)"
   1.388 -      by (auto simp add: vimage_UN positive_integral_suminf[symmetric] suminf_emeasure subset_eq emeasure_nonneg sets_Pair1
   1.389 -               intro!: positive_integral_cong positive_integral_indicator[symmetric])
   1.390 -  qed
   1.391 -  show "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2} \<subseteq> Pow (space M1 \<times> space M2)"
   1.392 -    using space_closed[of M1] space_closed[of M2] by auto
   1.393 -qed fact
   1.394 -
   1.395 -lemma (in pair_sigma_finite) emeasure_pair_measure_alt:
   1.396 -  assumes X: "X \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   1.397 -  shows "emeasure (M1  \<Otimes>\<^isub>M M2) X = (\<integral>\<^isup>+x. emeasure M2 (Pair x -` X) \<partial>M1)"
   1.398 -proof -
   1.399 -  have [simp]: "\<And>x y. indicator X (x, y) = indicator (Pair x -` X) y"
   1.400 -    by (auto simp: indicator_def)
   1.401 -  show ?thesis
   1.402 -    using X by (auto intro!: positive_integral_cong simp: emeasure_pair_measure sets_Pair1)
   1.403 -qed
   1.404 -
   1.405 -lemma (in pair_sigma_finite) emeasure_pair_measure_Times:
   1.406 -  assumes A: "A \<in> sets M1" and B: "B \<in> sets M2"
   1.407 -  shows "emeasure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = emeasure M1 A * emeasure M2 B"
   1.408 -proof -
   1.409 -  have "emeasure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = (\<integral>\<^isup>+x. emeasure M2 B * indicator A x \<partial>M1)"
   1.410 -    using A B by (auto intro!: positive_integral_cong simp: emeasure_pair_measure_alt)
   1.411 -  also have "\<dots> = emeasure M2 B * emeasure M1 A"
   1.412 -    using A by (simp add: emeasure_nonneg positive_integral_cmult_indicator)
   1.413 -  finally show ?thesis
   1.414 -    by (simp add: ac_simps)
   1.415 -qed
   1.416 -
   1.417  lemma (in pair_sigma_finite) sigma_finite_up_in_pair_measure_generator:
   1.418    defines "E \<equiv> {A \<times> B | A B. A \<in> sets M1 \<and> B \<in> sets M2}"
   1.419    shows "\<exists>F::nat \<Rightarrow> ('a \<times> 'b) set. range F \<subseteq> E \<and> incseq F \<and> (\<Union>i. F i) = space M1 \<times> space M2 \<and>
   1.420 @@ -366,7 +331,7 @@
   1.421    qed
   1.422  qed
   1.423  
   1.424 -sublocale pair_sigma_finite \<subseteq> sigma_finite_measure "M1 \<Otimes>\<^isub>M M2"
   1.425 +sublocale pair_sigma_finite \<subseteq> P: sigma_finite_measure "M1 \<Otimes>\<^isub>M M2"
   1.426  proof
   1.427    from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   1.428    show "\<exists>F::nat \<Rightarrow> ('a \<times> 'b) set. range F \<subseteq> sets (M1 \<Otimes>\<^isub>M M2) \<and> (\<Union>i. F i) = space (M1 \<Otimes>\<^isub>M M2) \<and> (\<forall>i. emeasure (M1 \<Otimes>\<^isub>M M2) (F i) \<noteq> \<infinity>)"
   1.429 @@ -396,7 +361,6 @@
   1.430  lemma (in pair_sigma_finite) distr_pair_swap:
   1.431    "M1 \<Otimes>\<^isub>M M2 = distr (M2 \<Otimes>\<^isub>M M1) (M1 \<Otimes>\<^isub>M M2) (\<lambda>(x, y). (y, x))" (is "?P = ?D")
   1.432  proof -
   1.433 -  interpret Q: pair_sigma_finite M2 M1 by default
   1.434    from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   1.435    let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   1.436    show ?thesis
   1.437 @@ -408,7 +372,7 @@
   1.438      then show "sets ?D = sigma_sets (space ?P) ?E"
   1.439        by simp
   1.440    next
   1.441 -    show "range F \<subseteq> ?E" "incseq F" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   1.442 +    show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   1.443        using F by (auto simp: space_pair_measure)
   1.444    next
   1.445      fix X assume "X \<in> ?E"
   1.446 @@ -416,7 +380,7 @@
   1.447      have "(\<lambda>(y, x). (x, y)) -` X \<inter> space (M2 \<Otimes>\<^isub>M M1) = B \<times> A"
   1.448        using sets_into_space[OF A] sets_into_space[OF B] by (auto simp: space_pair_measure)
   1.449      with A B show "emeasure (M1 \<Otimes>\<^isub>M M2) X = emeasure ?D X"
   1.450 -      by (simp add: emeasure_pair_measure_Times Q.emeasure_pair_measure_Times emeasure_distr
   1.451 +      by (simp add: M2.emeasure_pair_measure_Times M1.emeasure_pair_measure_Times emeasure_distr
   1.452                      measurable_pair_swap' ac_simps)
   1.453    qed
   1.454  qed
   1.455 @@ -426,138 +390,14 @@
   1.456    shows "emeasure (M1 \<Otimes>\<^isub>M M2) A = (\<integral>\<^isup>+y. emeasure M1 ((\<lambda>x. (x, y)) -` A) \<partial>M2)"
   1.457      (is "_ = ?\<nu> A")
   1.458  proof -
   1.459 -  interpret Q: pair_sigma_finite M2 M1 by default
   1.460    have [simp]: "\<And>y. (Pair y -` ((\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^isub>M M1))) = (\<lambda>x. (x, y)) -` A"
   1.461      using sets_into_space[OF A] by (auto simp: space_pair_measure)
   1.462    show ?thesis using A
   1.463      by (subst distr_pair_swap)
   1.464         (simp_all del: vimage_Int add: measurable_sets[OF measurable_pair_swap']
   1.465 -                 Q.emeasure_pair_measure_alt emeasure_distr[OF measurable_pair_swap' A])
   1.466 +                 M1.emeasure_pair_measure_alt emeasure_distr[OF measurable_pair_swap' A])
   1.467  qed
   1.468  
   1.469 -section "Fubinis theorem"
   1.470 -
   1.471 -lemma (in pair_sigma_finite) simple_function_cut:
   1.472 -  assumes f: "simple_function (M1 \<Otimes>\<^isub>M M2) f" "\<And>x. 0 \<le> f x"
   1.473 -  shows "(\<lambda>x. \<integral>\<^isup>+y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
   1.474 -    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   1.475 -proof -
   1.476 -  have f_borel: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   1.477 -    using f(1) by (rule borel_measurable_simple_function)
   1.478 -  let ?F = "\<lambda>z. f -` {z} \<inter> space (M1 \<Otimes>\<^isub>M M2)"
   1.479 -  let ?F' = "\<lambda>x z. Pair x -` ?F z"
   1.480 -  { fix x assume "x \<in> space M1"
   1.481 -    have [simp]: "\<And>z y. indicator (?F z) (x, y) = indicator (?F' x z) y"
   1.482 -      by (auto simp: indicator_def)
   1.483 -    have "\<And>y. y \<in> space M2 \<Longrightarrow> (x, y) \<in> space (M1 \<Otimes>\<^isub>M M2)" using `x \<in> space M1`
   1.484 -      by (simp add: space_pair_measure)
   1.485 -    moreover have "\<And>x z. ?F' x z \<in> sets M2" using f_borel
   1.486 -      by (rule sets_Pair1[OF measurable_sets]) auto
   1.487 -    ultimately have "simple_function M2 (\<lambda> y. f (x, y))"
   1.488 -      apply (rule_tac simple_function_cong[THEN iffD2, OF _])
   1.489 -      apply (rule simple_function_indicator_representation[OF f(1)])
   1.490 -      using `x \<in> space M1` by auto }
   1.491 -  note M2_sf = this
   1.492 -  { fix x assume x: "x \<in> space M1"
   1.493 -    then have "(\<integral>\<^isup>+y. f (x, y) \<partial>M2) = (\<Sum>z\<in>f ` space (M1 \<Otimes>\<^isub>M M2). z * emeasure M2 (?F' x z))"
   1.494 -      unfolding positive_integral_eq_simple_integral[OF M2_sf[OF x] f(2)]
   1.495 -      unfolding simple_integral_def
   1.496 -    proof (safe intro!: setsum_mono_zero_cong_left)
   1.497 -      from f(1) show "finite (f ` space (M1 \<Otimes>\<^isub>M M2))" by (rule simple_functionD)
   1.498 -    next
   1.499 -      fix y assume "y \<in> space M2" then show "f (x, y) \<in> f ` space (M1 \<Otimes>\<^isub>M M2)"
   1.500 -        using `x \<in> space M1` by (auto simp: space_pair_measure)
   1.501 -    next
   1.502 -      fix x' y assume "(x', y) \<in> space (M1 \<Otimes>\<^isub>M M2)"
   1.503 -        "f (x', y) \<notin> (\<lambda>y. f (x, y)) ` space M2"
   1.504 -      then have *: "?F' x (f (x', y)) = {}"
   1.505 -        by (force simp: space_pair_measure)
   1.506 -      show  "f (x', y) * emeasure M2 (?F' x (f (x', y))) = 0"
   1.507 -        unfolding * by simp
   1.508 -    qed (simp add: vimage_compose[symmetric] comp_def
   1.509 -                   space_pair_measure) }
   1.510 -  note eq = this
   1.511 -  moreover have "\<And>z. ?F z \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   1.512 -    by (auto intro!: f_borel borel_measurable_vimage)
   1.513 -  moreover then have "\<And>z. (\<lambda>x. emeasure M2 (?F' x z)) \<in> borel_measurable M1"
   1.514 -    by (auto intro!: measurable_emeasure_Pair1 simp del: vimage_Int)
   1.515 -  moreover have *: "\<And>i x. 0 \<le> emeasure M2 (Pair x -` (f -` {i} \<inter> space (M1 \<Otimes>\<^isub>M M2)))"
   1.516 -    using f(1)[THEN simple_functionD(2)] f(2) by (intro emeasure_nonneg)
   1.517 -  moreover { fix i assume "i \<in> f`space (M1 \<Otimes>\<^isub>M M2)"
   1.518 -    with * have "\<And>x. 0 \<le> i * emeasure M2 (Pair x -` (f -` {i} \<inter> space (M1 \<Otimes>\<^isub>M M2)))"
   1.519 -      using f(2) by auto }
   1.520 -  ultimately
   1.521 -  show "(\<lambda>x. \<integral>\<^isup>+y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
   1.522 -    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f" using f(2)
   1.523 -    by (auto simp del: vimage_Int cong: measurable_cong intro!: setsum_cong
   1.524 -             simp add: positive_integral_setsum simple_integral_def
   1.525 -                       positive_integral_cmult
   1.526 -                       positive_integral_cong[OF eq]
   1.527 -                       positive_integral_eq_simple_integral[OF f]
   1.528 -                       emeasure_pair_measure_alt[symmetric])
   1.529 -qed
   1.530 -
   1.531 -lemma (in pair_sigma_finite) positive_integral_fst_measurable:
   1.532 -  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   1.533 -  shows "(\<lambda>x. \<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
   1.534 -      (is "?C f \<in> borel_measurable M1")
   1.535 -    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   1.536 -proof -
   1.537 -  from borel_measurable_implies_simple_function_sequence'[OF f] guess F . note F = this
   1.538 -  then have F_borel: "\<And>i. F i \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   1.539 -    by (auto intro: borel_measurable_simple_function)
   1.540 -  note sf = simple_function_cut[OF F(1,5)]
   1.541 -  then have "(\<lambda>x. SUP i. ?C (F i) x) \<in> borel_measurable M1"
   1.542 -    using F(1) by auto
   1.543 -  moreover
   1.544 -  { fix x assume "x \<in> space M1"
   1.545 -    from F measurable_Pair2[OF F_borel `x \<in> space M1`]
   1.546 -    have "(\<integral>\<^isup>+y. (SUP i. F i (x, y)) \<partial>M2) = (SUP i. ?C (F i) x)"
   1.547 -      by (intro positive_integral_monotone_convergence_SUP)
   1.548 -         (auto simp: incseq_Suc_iff le_fun_def)
   1.549 -    then have "(SUP i. ?C (F i) x) = ?C f x"
   1.550 -      unfolding F(4) positive_integral_max_0 by simp }
   1.551 -  note SUPR_C = this
   1.552 -  ultimately show "?C f \<in> borel_measurable M1"
   1.553 -    by (simp cong: measurable_cong)
   1.554 -  have "(\<integral>\<^isup>+x. (SUP i. F i x) \<partial>(M1 \<Otimes>\<^isub>M M2)) = (SUP i. integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) (F i))"
   1.555 -    using F_borel F
   1.556 -    by (intro positive_integral_monotone_convergence_SUP) auto
   1.557 -  also have "(SUP i. integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) (F i)) = (SUP i. \<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. F i (x, y) \<partial>M2) \<partial>M1)"
   1.558 -    unfolding sf(2) by simp
   1.559 -  also have "\<dots> = \<integral>\<^isup>+ x. (SUP i. \<integral>\<^isup>+ y. F i (x, y) \<partial>M2) \<partial>M1" using F sf(1)
   1.560 -    by (intro positive_integral_monotone_convergence_SUP[symmetric])
   1.561 -       (auto intro!: positive_integral_mono positive_integral_positive
   1.562 -             simp: incseq_Suc_iff le_fun_def)
   1.563 -  also have "\<dots> = \<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. (SUP i. F i (x, y)) \<partial>M2) \<partial>M1"
   1.564 -    using F_borel F(2,5)
   1.565 -    by (auto intro!: positive_integral_cong positive_integral_monotone_convergence_SUP[symmetric] measurable_Pair2
   1.566 -             simp: incseq_Suc_iff le_fun_def)
   1.567 -  finally show "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   1.568 -    using F by (simp add: positive_integral_max_0)
   1.569 -qed
   1.570 -
   1.571 -lemma (in pair_sigma_finite) positive_integral_snd_measurable:
   1.572 -  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   1.573 -  shows "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   1.574 -proof -
   1.575 -  interpret Q: pair_sigma_finite M2 M1 by default
   1.576 -  note measurable_pair_swap[OF f]
   1.577 -  from Q.positive_integral_fst_measurable[OF this]
   1.578 -  have "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>\<^isup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^isub>M M1))"
   1.579 -    by simp
   1.580 -  also have "(\<integral>\<^isup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   1.581 -    by (subst distr_pair_swap)
   1.582 -       (auto simp: positive_integral_distr[OF measurable_pair_swap' f] intro!: positive_integral_cong)
   1.583 -  finally show ?thesis .
   1.584 -qed
   1.585 -
   1.586 -lemma (in pair_sigma_finite) Fubini:
   1.587 -  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   1.588 -  shows "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1)"
   1.589 -  unfolding positive_integral_snd_measurable[OF assms]
   1.590 -  unfolding positive_integral_fst_measurable[OF assms] ..
   1.591 -
   1.592  lemma (in pair_sigma_finite) AE_pair:
   1.593    assumes "AE x in (M1 \<Otimes>\<^isub>M M2). Q x"
   1.594    shows "AE x in M1. (AE y in M2. Q (x, y))"
   1.595 @@ -568,7 +408,7 @@
   1.596    proof (rule AE_I)
   1.597      from N measurable_emeasure_Pair1[OF `N \<in> sets (M1 \<Otimes>\<^isub>M M2)`]
   1.598      show "emeasure M1 {x\<in>space M1. emeasure M2 (Pair x -` N) \<noteq> 0} = 0"
   1.599 -      by (auto simp: emeasure_pair_measure_alt positive_integral_0_iff emeasure_nonneg)
   1.600 +      by (auto simp: M2.emeasure_pair_measure_alt positive_integral_0_iff emeasure_nonneg)
   1.601      show "{x \<in> space M1. emeasure M2 (Pair x -` N) \<noteq> 0} \<in> sets M1"
   1.602        by (intro borel_measurable_ereal_neq_const measurable_emeasure_Pair1 N)
   1.603      { fix x assume "x \<in> space M1" "emeasure M2 (Pair x -` N) = 0"
   1.604 @@ -593,7 +433,7 @@
   1.605      by (rule sets_Collect) fact
   1.606    then have "emeasure (M1 \<Otimes>\<^isub>M M2) {x \<in> space (M1 \<Otimes>\<^isub>M M2). \<not> P x} =
   1.607        (\<integral>\<^isup>+ x. \<integral>\<^isup>+ y. indicator {x \<in> space (M1 \<Otimes>\<^isub>M M2). \<not> P x} (x, y) \<partial>M2 \<partial>M1)"
   1.608 -    by (simp add: emeasure_pair_measure)
   1.609 +    by (simp add: M2.emeasure_pair_measure)
   1.610    also have "\<dots> = (\<integral>\<^isup>+ x. \<integral>\<^isup>+ y. 0 \<partial>M2 \<partial>M1)"
   1.611      using ae
   1.612      apply (safe intro!: positive_integral_cong_AE)
   1.613 @@ -609,24 +449,6 @@
   1.614      (AE x in M1. AE y in M2. P x y) \<longleftrightarrow> (AE x in (M1 \<Otimes>\<^isub>M M2). P (fst x) (snd x))"
   1.615    using AE_pair[of "\<lambda>x. P (fst x) (snd x)"] AE_pair_measure[of "\<lambda>x. P (fst x) (snd x)"] by auto
   1.616  
   1.617 -lemma AE_distr_iff:
   1.618 -  assumes f: "f \<in> measurable M N" and P: "{x \<in> space N. P x} \<in> sets N"
   1.619 -  shows "(AE x in distr M N f. P x) \<longleftrightarrow> (AE x in M. P (f x))"
   1.620 -proof (subst (1 2) AE_iff_measurable[OF _ refl])
   1.621 -  from P show "{x \<in> space (distr M N f). \<not> P x} \<in> sets (distr M N f)"
   1.622 -    by (auto intro!: sets_Collect_neg)
   1.623 -  moreover
   1.624 -  have "f -` {x \<in> space N. P x} \<inter> space M = {x \<in> space M. P (f x)}"
   1.625 -    using f by (auto dest: measurable_space)
   1.626 -  then show "{x \<in> space M. \<not> P (f x)} \<in> sets M"
   1.627 -    using measurable_sets[OF f P] by (auto intro!: sets_Collect_neg)
   1.628 -  moreover have "f -` {x\<in>space N. \<not> P x} \<inter> space M = {x \<in> space M. \<not> P (f x)}"
   1.629 -    using f by (auto dest: measurable_space)
   1.630 -  ultimately show "(emeasure (distr M N f) {x \<in> space (distr M N f). \<not> P x} = 0) =
   1.631 -    (emeasure M {x \<in> space M. \<not> P (f x)} = 0)"
   1.632 -    using f by (simp add: emeasure_distr)
   1.633 -qed
   1.634 -
   1.635  lemma (in pair_sigma_finite) AE_commute:
   1.636    assumes P: "{x\<in>space (M1 \<Otimes>\<^isub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   1.637    shows "(AE x in M1. AE y in M2. P x y) \<longleftrightarrow> (AE y in M2. AE x in M1. P x y)"
   1.638 @@ -648,6 +470,82 @@
   1.639      done
   1.640  qed
   1.641  
   1.642 +section "Fubinis theorem"
   1.643 +
   1.644 +lemma measurable_compose_Pair1:
   1.645 +  "x \<in> space M1 \<Longrightarrow> g \<in> measurable (M1 \<Otimes>\<^isub>M M2) L \<Longrightarrow> (\<lambda>y. g (x, y)) \<in> measurable M2 L"
   1.646 +  by (rule measurable_compose[OF measurable_Pair]) auto
   1.647 +
   1.648 +lemma (in pair_sigma_finite) borel_measurable_positive_integral_fst:
   1.649 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)" "\<And>x. 0 \<le> f x"
   1.650 +  shows "(\<lambda>x. \<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
   1.651 +using f proof induct
   1.652 +  case (cong u v)
   1.653 +  then have "\<And>w x. w \<in> space M1 \<Longrightarrow> x \<in> space M2 \<Longrightarrow> u (w, x) = v (w, x)"
   1.654 +    by (auto simp: space_pair_measure)
   1.655 +  show ?case
   1.656 +    apply (subst measurable_cong)
   1.657 +    apply (rule positive_integral_cong)
   1.658 +    apply fact+
   1.659 +    done
   1.660 +next
   1.661 +  case (set Q)
   1.662 +  have [simp]: "\<And>x y. indicator Q (x, y) = indicator (Pair x -` Q) y"
   1.663 +    by (auto simp: indicator_def)
   1.664 +  have "\<And>x. x \<in> space M1 \<Longrightarrow> emeasure M2 (Pair x -` Q) = \<integral>\<^isup>+ y. indicator Q (x, y) \<partial>M2"
   1.665 +    by (simp add: sets_Pair1[OF set])
   1.666 +  from this M2.measurable_emeasure_Pair[OF set] show ?case
   1.667 +    by (rule measurable_cong[THEN iffD1])
   1.668 +qed (simp_all add: positive_integral_add positive_integral_cmult measurable_compose_Pair1
   1.669 +                   positive_integral_monotone_convergence_SUP incseq_def le_fun_def
   1.670 +              cong: measurable_cong)
   1.671 +
   1.672 +lemma (in pair_sigma_finite) positive_integral_fst:
   1.673 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)" "\<And>x. 0 \<le> f x"
   1.674 +  shows "(\<integral>\<^isup>+ x. \<integral>\<^isup>+ y. f (x, y) \<partial>M2 \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f" (is "?I f = _")
   1.675 +using f proof induct
   1.676 +  case (cong u v)
   1.677 +  moreover then have "?I u = ?I v"
   1.678 +    by (intro positive_integral_cong) (auto simp: space_pair_measure)
   1.679 +  ultimately show ?case
   1.680 +    by (simp cong: positive_integral_cong)
   1.681 +qed (simp_all add: M2.emeasure_pair_measure positive_integral_cmult positive_integral_add
   1.682 +                   positive_integral_monotone_convergence_SUP
   1.683 +                   measurable_compose_Pair1 positive_integral_positive
   1.684 +                   borel_measurable_positive_integral_fst positive_integral_mono incseq_def le_fun_def
   1.685 +              cong: positive_integral_cong)
   1.686 +
   1.687 +lemma (in pair_sigma_finite) positive_integral_fst_measurable:
   1.688 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   1.689 +  shows "(\<lambda>x. \<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
   1.690 +      (is "?C f \<in> borel_measurable M1")
   1.691 +    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   1.692 +  using f
   1.693 +    borel_measurable_positive_integral_fst[of "\<lambda>x. max 0 (f x)"]
   1.694 +    positive_integral_fst[of "\<lambda>x. max 0 (f x)"]
   1.695 +  unfolding positive_integral_max_0 by auto
   1.696 +
   1.697 +lemma (in pair_sigma_finite) positive_integral_snd_measurable:
   1.698 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   1.699 +  shows "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   1.700 +proof -
   1.701 +  interpret Q: pair_sigma_finite M2 M1 by default
   1.702 +  note measurable_pair_swap[OF f]
   1.703 +  from Q.positive_integral_fst_measurable[OF this]
   1.704 +  have "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>\<^isup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^isub>M M1))"
   1.705 +    by simp
   1.706 +  also have "(\<integral>\<^isup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   1.707 +    by (subst distr_pair_swap)
   1.708 +       (auto simp: positive_integral_distr[OF measurable_pair_swap' f] intro!: positive_integral_cong)
   1.709 +  finally show ?thesis .
   1.710 +qed
   1.711 +
   1.712 +lemma (in pair_sigma_finite) Fubini:
   1.713 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   1.714 +  shows "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1)"
   1.715 +  unfolding positive_integral_snd_measurable[OF assms]
   1.716 +  unfolding positive_integral_fst_measurable[OF assms] ..
   1.717 +
   1.718  lemma (in pair_sigma_finite) integrable_product_swap:
   1.719    assumes "integrable (M1 \<Otimes>\<^isub>M M2) f"
   1.720    shows "integrable (M2 \<Otimes>\<^isub>M M1) (\<lambda>(x,y). f (y,x))"
   1.721 @@ -817,7 +715,7 @@
   1.722      by (intro finite_subset[OF _ B]) auto
   1.723    have fin_X: "finite X" using X_subset by (rule finite_subset) (auto simp: A B)
   1.724    show "emeasure ?P X = emeasure ?C X"
   1.725 -    apply (subst P.emeasure_pair_measure_alt[OF X])
   1.726 +    apply (subst B.emeasure_pair_measure_alt[OF X])
   1.727      apply (subst emeasure_count_space)
   1.728      using X_subset apply auto []
   1.729      apply (simp add: fin_Pair emeasure_count_space X_subset fin_X)
   1.730 @@ -861,7 +759,7 @@
   1.731      by simp
   1.732  
   1.733    show "emeasure ?L A = emeasure ?R A"
   1.734 -    apply (subst L.emeasure_pair_measure[OF A])
   1.735 +    apply (subst D2.emeasure_pair_measure[OF A])
   1.736      apply (subst emeasure_density)
   1.737          using f_fst g_snd apply (simp add: split_beta')
   1.738        using A apply simp
   1.739 @@ -924,7 +822,7 @@
   1.740      by (auto simp: measurable_pair_iff)
   1.741    fix A assume A: "A \<in> sets ?P"
   1.742    then have "emeasure ?P A = (\<integral>\<^isup>+x. emeasure (distr N T g) (Pair x -` A) \<partial>distr M S f)"
   1.743 -    by (rule ST.emeasure_pair_measure_alt)
   1.744 +    by (rule T.emeasure_pair_measure_alt)
   1.745    also have "\<dots> = (\<integral>\<^isup>+x. emeasure N (g -` (Pair x -` A) \<inter> space N) \<partial>distr M S f)"
   1.746      using g A by (simp add: sets_Pair1 emeasure_distr)
   1.747    also have "\<dots> = (\<integral>\<^isup>+x. emeasure N (g -` (Pair (f x) -` A) \<inter> space N) \<partial>M)"
   1.748 @@ -933,7 +831,7 @@
   1.749    also have "\<dots> = (\<integral>\<^isup>+x. emeasure N (Pair x -` ((\<lambda>(x, y). (f x, g y)) -` A \<inter> space (M \<Otimes>\<^isub>M N))) \<partial>M)"
   1.750      by (intro positive_integral_cong arg_cong2[where f=emeasure]) (auto simp: space_pair_measure)
   1.751    also have "\<dots> = emeasure (M \<Otimes>\<^isub>M N) ((\<lambda>(x, y). (f x, g y)) -` A \<inter> space (M \<Otimes>\<^isub>M N))"
   1.752 -    using fg by (intro MN.emeasure_pair_measure_alt[symmetric] measurable_sets[OF _ A])
   1.753 +    using fg by (intro N.emeasure_pair_measure_alt[symmetric] measurable_sets[OF _ A])
   1.754                  (auto cong: measurable_cong')
   1.755    also have "\<dots> = emeasure ?D A"
   1.756      using fg A by (subst emeasure_distr) auto
     2.1 --- a/src/HOL/Probability/Borel_Space.thy	Wed Oct 10 15:16:44 2012 +0200
     2.2 +++ b/src/HOL/Probability/Borel_Space.thy	Wed Oct 10 15:17:18 2012 +0200
     2.3 @@ -632,7 +632,7 @@
     2.4    "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
     2.5    using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
     2.6  
     2.7 -lemma borel_measurable_euclidean_component:
     2.8 +lemma borel_measurable_euclidean_component':
     2.9    "(\<lambda>x::'a::euclidean_space. x $$ i) \<in> borel_measurable borel"
    2.10  proof (rule borel_measurableI)
    2.11    fix S::"real set" assume "open S"
    2.12 @@ -641,13 +641,18 @@
    2.13      by (auto intro: borel_open)
    2.14  qed
    2.15  
    2.16 +lemma borel_measurable_euclidean_component:
    2.17 +  fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
    2.18 +  assumes f: "f \<in> borel_measurable M"
    2.19 +  shows "(\<lambda>x. f x $$ i) \<in> borel_measurable M"
    2.20 +  using measurable_comp[OF f borel_measurable_euclidean_component'] by (simp add: comp_def)
    2.21 +
    2.22  lemma borel_measurable_euclidean_space:
    2.23    fixes f :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
    2.24    shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M)"
    2.25  proof safe
    2.26    fix i assume "f \<in> borel_measurable M"
    2.27    then show "(\<lambda>x. f x $$ i) \<in> borel_measurable M"
    2.28 -    using measurable_comp[of f _ _ "\<lambda>x. x $$ i", unfolded comp_def]
    2.29      by (auto intro: borel_measurable_euclidean_component)
    2.30  next
    2.31    assume f: "\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M"
    2.32 @@ -657,6 +662,144 @@
    2.33  
    2.34  subsection "Borel measurable operators"
    2.35  
    2.36 +lemma borel_measurable_continuous_on1:
    2.37 +  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
    2.38 +  assumes "continuous_on UNIV f"
    2.39 +  shows "f \<in> borel_measurable borel"
    2.40 +  apply(rule borel_measurableI)
    2.41 +  using continuous_open_preimage[OF assms] unfolding vimage_def by auto
    2.42 +
    2.43 +lemma borel_measurable_continuous_on:
    2.44 +  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
    2.45 +  assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
    2.46 +  shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
    2.47 +  using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
    2.48 +
    2.49 +lemma borel_measurable_continuous_on_open':
    2.50 +  fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
    2.51 +  assumes cont: "continuous_on A f" "open A"
    2.52 +  shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
    2.53 +proof (rule borel_measurableI)
    2.54 +  fix S :: "'b set" assume "open S"
    2.55 +  then have "open {x\<in>A. f x \<in> S}"
    2.56 +    by (intro continuous_open_preimage[OF cont]) auto
    2.57 +  then have *: "{x\<in>A. f x \<in> S} \<in> sets borel" by auto
    2.58 +  have "?f -` S \<inter> space borel = 
    2.59 +    {x\<in>A. f x \<in> S} \<union> (if c \<in> S then space borel - A else {})"
    2.60 +    by (auto split: split_if_asm)
    2.61 +  also have "\<dots> \<in> sets borel"
    2.62 +    using * `open A` by (auto simp del: space_borel intro!: Un)
    2.63 +  finally show "?f -` S \<inter> space borel \<in> sets borel" .
    2.64 +qed
    2.65 +
    2.66 +lemma borel_measurable_continuous_on_open:
    2.67 +  fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
    2.68 +  assumes cont: "continuous_on A f" "open A"
    2.69 +  assumes g: "g \<in> borel_measurable M"
    2.70 +  shows "(\<lambda>x. if g x \<in> A then f (g x) else c) \<in> borel_measurable M"
    2.71 +  using measurable_comp[OF g borel_measurable_continuous_on_open'[OF cont], of c]
    2.72 +  by (simp add: comp_def)
    2.73 +
    2.74 +lemma borel_measurable_uminus[simp, intro]:
    2.75 +  fixes g :: "'a \<Rightarrow> real"
    2.76 +  assumes g: "g \<in> borel_measurable M"
    2.77 +  shows "(\<lambda>x. - g x) \<in> borel_measurable M"
    2.78 +  by (rule borel_measurable_continuous_on[OF _ g]) (auto intro: continuous_on_minus continuous_on_id)
    2.79 +
    2.80 +lemma euclidean_component_prod:
    2.81 +  fixes x :: "'a :: euclidean_space \<times> 'b :: euclidean_space"
    2.82 +  shows "x $$ i = (if i < DIM('a) then fst x $$ i else snd x $$ (i - DIM('a)))"
    2.83 +  unfolding euclidean_component_def basis_prod_def inner_prod_def by auto
    2.84 +
    2.85 +lemma borel_measurable_Pair[simp, intro]:
    2.86 +  fixes f :: "'a \<Rightarrow> 'b::ordered_euclidean_space" and g :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
    2.87 +  assumes f: "f \<in> borel_measurable M"
    2.88 +  assumes g: "g \<in> borel_measurable M"
    2.89 +  shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
    2.90 +proof (intro borel_measurable_iff_halfspace_le[THEN iffD2] allI impI)
    2.91 +  fix i and a :: real assume i: "i < DIM('b \<times> 'c)"
    2.92 +  have [simp]: "\<And>P A B C. {w. (P \<longrightarrow> A w \<and> B w) \<and> (\<not> P \<longrightarrow> A w \<and> C w)} = 
    2.93 +    {w. A w \<and> (P \<longrightarrow> B w) \<and> (\<not> P \<longrightarrow> C w)}" by auto
    2.94 +  from i f g show "{w \<in> space M. (f w, g w) $$ i \<le> a} \<in> sets M"
    2.95 +    by (auto simp: euclidean_component_prod intro!: sets_Collect borel_measurable_euclidean_component)
    2.96 +qed
    2.97 +
    2.98 +lemma continuous_on_fst: "continuous_on UNIV fst"
    2.99 +proof -
   2.100 +  have [simp]: "range fst = UNIV" by (auto simp: image_iff)
   2.101 +  show ?thesis
   2.102 +    using closed_vimage_fst
   2.103 +    by (auto simp: continuous_on_closed closed_closedin vimage_def)
   2.104 +qed
   2.105 +
   2.106 +lemma continuous_on_snd: "continuous_on UNIV snd"
   2.107 +proof -
   2.108 +  have [simp]: "range snd = UNIV" by (auto simp: image_iff)
   2.109 +  show ?thesis
   2.110 +    using closed_vimage_snd
   2.111 +    by (auto simp: continuous_on_closed closed_closedin vimage_def)
   2.112 +qed
   2.113 +
   2.114 +lemma borel_measurable_continuous_Pair:
   2.115 +  fixes f :: "'a \<Rightarrow> 'b::ordered_euclidean_space" and g :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   2.116 +  assumes [simp]: "f \<in> borel_measurable M"
   2.117 +  assumes [simp]: "g \<in> borel_measurable M"
   2.118 +  assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
   2.119 +  shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   2.120 +proof -
   2.121 +  have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
   2.122 +  show ?thesis
   2.123 +    unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
   2.124 +qed
   2.125 +
   2.126 +lemma borel_measurable_add[simp, intro]:
   2.127 +  fixes f g :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   2.128 +  assumes f: "f \<in> borel_measurable M"
   2.129 +  assumes g: "g \<in> borel_measurable M"
   2.130 +  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   2.131 +  using f g
   2.132 +  by (rule borel_measurable_continuous_Pair)
   2.133 +     (auto intro: continuous_on_fst continuous_on_snd continuous_on_add)
   2.134 +
   2.135 +lemma borel_measurable_setsum[simp, intro]:
   2.136 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   2.137 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   2.138 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   2.139 +proof cases
   2.140 +  assume "finite S"
   2.141 +  thus ?thesis using assms by induct auto
   2.142 +qed simp
   2.143 +
   2.144 +lemma borel_measurable_diff[simp, intro]:
   2.145 +  fixes f :: "'a \<Rightarrow> real"
   2.146 +  assumes f: "f \<in> borel_measurable M"
   2.147 +  assumes g: "g \<in> borel_measurable M"
   2.148 +  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   2.149 +  unfolding diff_minus using assms by fast
   2.150 +
   2.151 +lemma borel_measurable_times[simp, intro]:
   2.152 +  fixes f :: "'a \<Rightarrow> real"
   2.153 +  assumes f: "f \<in> borel_measurable M"
   2.154 +  assumes g: "g \<in> borel_measurable M"
   2.155 +  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   2.156 +  using f g
   2.157 +  by (rule borel_measurable_continuous_Pair)
   2.158 +     (auto intro: continuous_on_fst continuous_on_snd continuous_on_mult)
   2.159 +
   2.160 +lemma continuous_on_dist:
   2.161 +  fixes f :: "'a :: t2_space \<Rightarrow> 'b :: metric_space"
   2.162 +  shows "continuous_on A f \<Longrightarrow> continuous_on A g \<Longrightarrow> continuous_on A (\<lambda>x. dist (f x) (g x))"
   2.163 +  unfolding continuous_on_eq_continuous_within by (auto simp: continuous_dist)
   2.164 +
   2.165 +lemma borel_measurable_dist[simp, intro]:
   2.166 +  fixes g f :: "'a \<Rightarrow> 'b::ordered_euclidean_space"
   2.167 +  assumes f: "f \<in> borel_measurable M"
   2.168 +  assumes g: "g \<in> borel_measurable M"
   2.169 +  shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
   2.170 +  using f g
   2.171 +  by (rule borel_measurable_continuous_Pair)
   2.172 +     (intro continuous_on_dist continuous_on_fst continuous_on_snd)
   2.173 +  
   2.174  lemma affine_borel_measurable_vector:
   2.175    fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   2.176    assumes "f \<in> borel_measurable M"
   2.177 @@ -683,116 +826,6 @@
   2.178    shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
   2.179    using affine_borel_measurable_vector[OF assms] by (simp add: mult_commute)
   2.180  
   2.181 -lemma borel_measurable_add[simp, intro]:
   2.182 -  fixes f :: "'a \<Rightarrow> real"
   2.183 -  assumes f: "f \<in> borel_measurable M"
   2.184 -  assumes g: "g \<in> borel_measurable M"
   2.185 -  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   2.186 -proof -
   2.187 -  have 1: "\<And>a. {w\<in>space M. a \<le> f w + g w} = {w \<in> space M. a + g w * -1 \<le> f w}"
   2.188 -    by auto
   2.189 -  have "\<And>a. (\<lambda>w. a + (g w) * -1) \<in> borel_measurable M"
   2.190 -    by (rule affine_borel_measurable [OF g])
   2.191 -  then have "\<And>a. {w \<in> space M. (\<lambda>w. a + (g w) * -1)(w) \<le> f w} \<in> sets M" using f
   2.192 -    by auto
   2.193 -  then have "\<And>a. {w \<in> space M. a \<le> f w + g w} \<in> sets M"
   2.194 -    by (simp add: 1)
   2.195 -  then show ?thesis
   2.196 -    by (simp add: borel_measurable_iff_ge)
   2.197 -qed
   2.198 -
   2.199 -lemma borel_measurable_setsum[simp, intro]:
   2.200 -  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   2.201 -  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   2.202 -  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   2.203 -proof cases
   2.204 -  assume "finite S"
   2.205 -  thus ?thesis using assms by induct auto
   2.206 -qed simp
   2.207 -
   2.208 -lemma borel_measurable_square:
   2.209 -  fixes f :: "'a \<Rightarrow> real"
   2.210 -  assumes f: "f \<in> borel_measurable M"
   2.211 -  shows "(\<lambda>x. (f x)^2) \<in> borel_measurable M"
   2.212 -proof -
   2.213 -  {
   2.214 -    fix a
   2.215 -    have "{w \<in> space M. (f w)\<twosuperior> \<le> a} \<in> sets M"
   2.216 -    proof (cases rule: linorder_cases [of a 0])
   2.217 -      case less
   2.218 -      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} = {}"
   2.219 -        by auto (metis less order_le_less_trans power2_less_0)
   2.220 -      also have "... \<in> sets M"
   2.221 -        by (rule empty_sets)
   2.222 -      finally show ?thesis .
   2.223 -    next
   2.224 -      case equal
   2.225 -      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   2.226 -             {w \<in> space M. f w \<le> 0} \<inter> {w \<in> space M. 0 \<le> f w}"
   2.227 -        by auto
   2.228 -      also have "... \<in> sets M"
   2.229 -        apply (insert f)
   2.230 -        apply (rule Int)
   2.231 -        apply (simp add: borel_measurable_iff_le)
   2.232 -        apply (simp add: borel_measurable_iff_ge)
   2.233 -        done
   2.234 -      finally show ?thesis .
   2.235 -    next
   2.236 -      case greater
   2.237 -      have "\<forall>x. (f x ^ 2 \<le> sqrt a ^ 2) = (- sqrt a  \<le> f x & f x \<le> sqrt a)"
   2.238 -        by (metis abs_le_interval_iff abs_of_pos greater real_sqrt_abs
   2.239 -                  real_sqrt_le_iff real_sqrt_power)
   2.240 -      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   2.241 -             {w \<in> space M. -(sqrt a) \<le> f w} \<inter> {w \<in> space M. f w \<le> sqrt a}"
   2.242 -        using greater by auto
   2.243 -      also have "... \<in> sets M"
   2.244 -        apply (insert f)
   2.245 -        apply (rule Int)
   2.246 -        apply (simp add: borel_measurable_iff_ge)
   2.247 -        apply (simp add: borel_measurable_iff_le)
   2.248 -        done
   2.249 -      finally show ?thesis .
   2.250 -    qed
   2.251 -  }
   2.252 -  thus ?thesis by (auto simp add: borel_measurable_iff_le)
   2.253 -qed
   2.254 -
   2.255 -lemma times_eq_sum_squares:
   2.256 -   fixes x::real
   2.257 -   shows"x*y = ((x+y)^2)/4 - ((x-y)^ 2)/4"
   2.258 -by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric])
   2.259 -
   2.260 -lemma borel_measurable_uminus[simp, intro]:
   2.261 -  fixes g :: "'a \<Rightarrow> real"
   2.262 -  assumes g: "g \<in> borel_measurable M"
   2.263 -  shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   2.264 -proof -
   2.265 -  have "(\<lambda>x. - g x) = (\<lambda>x. 0 + (g x) * -1)"
   2.266 -    by simp
   2.267 -  also have "... \<in> borel_measurable M"
   2.268 -    by (fast intro: affine_borel_measurable g)
   2.269 -  finally show ?thesis .
   2.270 -qed
   2.271 -
   2.272 -lemma borel_measurable_times[simp, intro]:
   2.273 -  fixes f :: "'a \<Rightarrow> real"
   2.274 -  assumes f: "f \<in> borel_measurable M"
   2.275 -  assumes g: "g \<in> borel_measurable M"
   2.276 -  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   2.277 -proof -
   2.278 -  have 1: "(\<lambda>x. 0 + (f x + g x)\<twosuperior> * inverse 4) \<in> borel_measurable M"
   2.279 -    using assms by (fast intro: affine_borel_measurable borel_measurable_square)
   2.280 -  have "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) =
   2.281 -        (\<lambda>x. 0 + ((f x + -g x) ^ 2 * inverse -4))"
   2.282 -    by (simp add: minus_divide_right)
   2.283 -  also have "... \<in> borel_measurable M"
   2.284 -    using f g by (fast intro: affine_borel_measurable borel_measurable_square f g)
   2.285 -  finally have 2: "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) \<in> borel_measurable M" .
   2.286 -  show ?thesis
   2.287 -    apply (simp add: times_eq_sum_squares diff_minus)
   2.288 -    using 1 2 by simp
   2.289 -qed
   2.290 -
   2.291  lemma borel_measurable_setprod[simp, intro]:
   2.292    fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   2.293    assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   2.294 @@ -802,26 +835,17 @@
   2.295    thus ?thesis using assms by induct auto
   2.296  qed simp
   2.297  
   2.298 -lemma borel_measurable_diff[simp, intro]:
   2.299 -  fixes f :: "'a \<Rightarrow> real"
   2.300 -  assumes f: "f \<in> borel_measurable M"
   2.301 -  assumes g: "g \<in> borel_measurable M"
   2.302 -  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   2.303 -  unfolding diff_minus using assms by fast
   2.304 -
   2.305  lemma borel_measurable_inverse[simp, intro]:
   2.306    fixes f :: "'a \<Rightarrow> real"
   2.307 -  assumes "f \<in> borel_measurable M"
   2.308 +  assumes f: "f \<in> borel_measurable M"
   2.309    shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   2.310 -  unfolding borel_measurable_iff_ge unfolding inverse_eq_divide
   2.311 -proof safe
   2.312 -  fix a :: real
   2.313 -  have *: "{w \<in> space M. a \<le> 1 / f w} =
   2.314 -      ({w \<in> space M. 0 < f w} \<inter> {w \<in> space M. a * f w \<le> 1}) \<union>
   2.315 -      ({w \<in> space M. f w < 0} \<inter> {w \<in> space M. 1 \<le> a * f w}) \<union>
   2.316 -      ({w \<in> space M. f w = 0} \<inter> {w \<in> space M. a \<le> 0})" by (auto simp: le_divide_eq)
   2.317 -  show "{w \<in> space M. a \<le> 1 / f w} \<in> sets M" using assms unfolding *
   2.318 -    by (auto intro!: Int Un)
   2.319 +proof -
   2.320 +  have *: "\<And>x::real. inverse x = (if x \<in> UNIV - {0} then inverse x else 0)" by auto
   2.321 +  show ?thesis
   2.322 +    apply (subst *)
   2.323 +    apply (rule borel_measurable_continuous_on_open)
   2.324 +    apply (auto intro!: f continuous_on_inverse continuous_on_id)
   2.325 +    done
   2.326  qed
   2.327  
   2.328  lemma borel_measurable_divide[simp, intro]:
   2.329 @@ -837,30 +861,14 @@
   2.330    assumes "f \<in> borel_measurable M"
   2.331    assumes "g \<in> borel_measurable M"
   2.332    shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   2.333 -  unfolding borel_measurable_iff_le
   2.334 -proof safe
   2.335 -  fix a
   2.336 -  have "{x \<in> space M. max (g x) (f x) \<le> a} =
   2.337 -    {x \<in> space M. g x \<le> a} \<inter> {x \<in> space M. f x \<le> a}" by auto
   2.338 -  thus "{x \<in> space M. max (g x) (f x) \<le> a} \<in> sets M"
   2.339 -    using assms unfolding borel_measurable_iff_le
   2.340 -    by (auto intro!: Int)
   2.341 -qed
   2.342 +  unfolding max_def by (auto intro!: assms measurable_If)
   2.343  
   2.344  lemma borel_measurable_min[intro, simp]:
   2.345    fixes f g :: "'a \<Rightarrow> real"
   2.346    assumes "f \<in> borel_measurable M"
   2.347    assumes "g \<in> borel_measurable M"
   2.348    shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
   2.349 -  unfolding borel_measurable_iff_ge
   2.350 -proof safe
   2.351 -  fix a
   2.352 -  have "{x \<in> space M. a \<le> min (g x) (f x)} =
   2.353 -    {x \<in> space M. a \<le> g x} \<inter> {x \<in> space M. a \<le> f x}" by auto
   2.354 -  thus "{x \<in> space M. a \<le> min (g x) (f x)} \<in> sets M"
   2.355 -    using assms unfolding borel_measurable_iff_ge
   2.356 -    by (auto intro!: Int)
   2.357 -qed
   2.358 +  unfolding min_def by (auto intro!: assms measurable_If)
   2.359  
   2.360  lemma borel_measurable_abs[simp, intro]:
   2.361    assumes "f \<in> borel_measurable M"
   2.362 @@ -872,76 +880,50 @@
   2.363  
   2.364  lemma borel_measurable_nth[simp, intro]:
   2.365    "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
   2.366 -  using borel_measurable_euclidean_component
   2.367 +  using borel_measurable_euclidean_component'
   2.368    unfolding nth_conv_component by auto
   2.369  
   2.370 -lemma borel_measurable_continuous_on1:
   2.371 -  fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   2.372 -  assumes "continuous_on UNIV f"
   2.373 -  shows "f \<in> borel_measurable borel"
   2.374 -  apply(rule borel_measurableI)
   2.375 -  using continuous_open_preimage[OF assms] unfolding vimage_def by auto
   2.376 -
   2.377 -lemma borel_measurable_continuous_on:
   2.378 -  fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   2.379 -  assumes cont: "continuous_on A f" "open A"
   2.380 -  shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
   2.381 -proof (rule borel_measurableI)
   2.382 -  fix S :: "'b set" assume "open S"
   2.383 -  then have "open {x\<in>A. f x \<in> S}"
   2.384 -    by (intro continuous_open_preimage[OF cont]) auto
   2.385 -  then have *: "{x\<in>A. f x \<in> S} \<in> sets borel" by auto
   2.386 -  have "?f -` S \<inter> space borel = 
   2.387 -    {x\<in>A. f x \<in> S} \<union> (if c \<in> S then space borel - A else {})"
   2.388 -    by (auto split: split_if_asm)
   2.389 -  also have "\<dots> \<in> sets borel"
   2.390 -    using * `open A` by (auto simp del: space_borel intro!: Un)
   2.391 -  finally show "?f -` S \<inter> space borel \<in> sets borel" .
   2.392 -qed
   2.393 -
   2.394  lemma convex_measurable:
   2.395    fixes a b :: real
   2.396    assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> { a <..< b}"
   2.397    assumes q: "convex_on { a <..< b} q"
   2.398 -  shows "q \<circ> X \<in> borel_measurable M"
   2.399 +  shows "(\<lambda>x. q (X x)) \<in> borel_measurable M"
   2.400  proof -
   2.401 -  have "(\<lambda>x. if x \<in> {a <..< b} then q x else 0) \<in> borel_measurable borel"
   2.402 -  proof (rule borel_measurable_continuous_on)
   2.403 +  have "(\<lambda>x. if X x \<in> {a <..< b} then q (X x) else 0) \<in> borel_measurable M" (is "?qX")
   2.404 +  proof (rule borel_measurable_continuous_on_open[OF _ _ X(1)])
   2.405      show "open {a<..<b}" by auto
   2.406      from this q show "continuous_on {a<..<b} q"
   2.407        by (rule convex_on_continuous)
   2.408    qed
   2.409 -  then have "(\<lambda>x. if x \<in> {a <..< b} then q x else 0) \<circ> X \<in> borel_measurable M" (is ?qX)
   2.410 -    using X by (intro measurable_comp) auto
   2.411 -  moreover have "?qX \<longleftrightarrow> q \<circ> X \<in> borel_measurable M"
   2.412 +  moreover have "?qX \<longleftrightarrow> (\<lambda>x. q (X x)) \<in> borel_measurable M"
   2.413      using X by (intro measurable_cong) auto
   2.414    ultimately show ?thesis by simp
   2.415  qed
   2.416  
   2.417 -lemma borel_measurable_borel_log: assumes "1 < b" shows "log b \<in> borel_measurable borel"
   2.418 +lemma borel_measurable_ln[simp,intro]:
   2.419 +  assumes f: "f \<in> borel_measurable M"
   2.420 +  shows "(\<lambda>x. ln (f x)) \<in> borel_measurable M"
   2.421  proof -
   2.422    { fix x :: real assume x: "x \<le> 0"
   2.423      { fix x::real assume "x \<le> 0" then have "\<And>u. exp u = x \<longleftrightarrow> False" by auto }
   2.424 -    from this[of x] x this[of 0] have "log b 0 = log b x"
   2.425 -      by (auto simp: ln_def log_def) }
   2.426 -  note log_imp = this
   2.427 -  have "(\<lambda>x. if x \<in> {0<..} then log b x else log b 0) \<in> borel_measurable borel"
   2.428 -  proof (rule borel_measurable_continuous_on)
   2.429 -    show "continuous_on {0<..} (log b)"
   2.430 -      by (auto intro!: continuous_at_imp_continuous_on DERIV_log DERIV_isCont
   2.431 +    from this[of x] x this[of 0] have "ln 0 = ln x"
   2.432 +      by (auto simp: ln_def) }
   2.433 +  note ln_imp = this
   2.434 +  have "(\<lambda>x. if f x \<in> {0<..} then ln (f x) else ln 0) \<in> borel_measurable M"
   2.435 +  proof (rule borel_measurable_continuous_on_open[OF _ _ f])
   2.436 +    show "continuous_on {0<..} ln"
   2.437 +      by (auto intro!: continuous_at_imp_continuous_on DERIV_ln DERIV_isCont
   2.438                 simp: continuous_isCont[symmetric])
   2.439      show "open ({0<..}::real set)" by auto
   2.440    qed
   2.441 -  also have "(\<lambda>x. if x \<in> {0<..} then log b x else log b 0) = log b"
   2.442 -    by (simp add: fun_eq_iff not_less log_imp)
   2.443 +  also have "(\<lambda>x. if x \<in> {0<..} then ln x else ln 0) = ln"
   2.444 +    by (simp add: fun_eq_iff not_less ln_imp)
   2.445    finally show ?thesis .
   2.446  qed
   2.447  
   2.448  lemma borel_measurable_log[simp,intro]:
   2.449 -  assumes f: "f \<in> borel_measurable M" and "1 < b"
   2.450 -  shows "(\<lambda>x. log b (f x)) \<in> borel_measurable M"
   2.451 -  using measurable_comp[OF f borel_measurable_borel_log[OF `1 < b`]]
   2.452 -  by (simp add: comp_def)
   2.453 +  "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log b (f x)) \<in> borel_measurable M"
   2.454 +  unfolding log_def by auto
   2.455  
   2.456  lemma borel_measurable_real_floor:
   2.457    "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
   2.458 @@ -967,45 +949,91 @@
   2.459  
   2.460  subsection "Borel space on the extended reals"
   2.461  
   2.462 -lemma borel_measurable_ereal_borel:
   2.463 -  "ereal \<in> borel_measurable borel"
   2.464 -proof (rule borel_measurableI)
   2.465 -  fix X :: "ereal set" assume "open X"
   2.466 -  then have "open (ereal -` X \<inter> space borel)"
   2.467 -    by (simp add: open_ereal_vimage)
   2.468 -  then show "ereal -` X \<inter> space borel \<in> sets borel" by auto
   2.469 -qed
   2.470 -
   2.471  lemma borel_measurable_ereal[simp, intro]:
   2.472    assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   2.473 -  using measurable_comp[OF f borel_measurable_ereal_borel] unfolding comp_def .
   2.474 +  using continuous_on_ereal f by (rule borel_measurable_continuous_on)
   2.475  
   2.476 -lemma borel_measurable_real_of_ereal_borel:
   2.477 -  "(real :: ereal \<Rightarrow> real) \<in> borel_measurable borel"
   2.478 -proof (rule borel_measurableI)
   2.479 -  fix B :: "real set" assume "open B"
   2.480 -  have *: "ereal -` real -` (B - {0}) = B - {0}" by auto
   2.481 -  have open_real: "open (real -` (B - {0}) :: ereal set)"
   2.482 -    unfolding open_ereal_def * using `open B` by auto
   2.483 -  show "(real -` B \<inter> space borel :: ereal set) \<in> sets borel"
   2.484 -  proof cases
   2.485 -    assume "0 \<in> B"
   2.486 -    then have *: "real -` B = real -` (B - {0}) \<union> {-\<infinity>, \<infinity>, 0::ereal}"
   2.487 -      by (auto simp add: real_of_ereal_eq_0)
   2.488 -    then show "(real -` B :: ereal set) \<inter> space borel \<in> sets borel"
   2.489 -      using open_real by auto
   2.490 -  next
   2.491 -    assume "0 \<notin> B"
   2.492 -    then have *: "(real -` B :: ereal set) = real -` (B - {0})"
   2.493 -      by (auto simp add: real_of_ereal_eq_0)
   2.494 -    then show "(real -` B :: ereal set) \<inter> space borel \<in> sets borel"
   2.495 -      using open_real by auto
   2.496 -  qed
   2.497 +lemma borel_measurable_real_of_ereal[simp, intro]:
   2.498 +  fixes f :: "'a \<Rightarrow> ereal" 
   2.499 +  assumes f: "f \<in> borel_measurable M"
   2.500 +  shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
   2.501 +proof -
   2.502 +  have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) \<in> borel_measurable M"
   2.503 +    using continuous_on_real
   2.504 +    by (rule borel_measurable_continuous_on_open[OF _ _ f]) auto
   2.505 +  also have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) = (\<lambda>x. real (f x))"
   2.506 +    by auto
   2.507 +  finally show ?thesis .
   2.508 +qed
   2.509 +
   2.510 +lemma borel_measurable_ereal_cases:
   2.511 +  fixes f :: "'a \<Rightarrow> ereal" 
   2.512 +  assumes f: "f \<in> borel_measurable M"
   2.513 +  assumes H: "(\<lambda>x. H (ereal (real (f x)))) \<in> borel_measurable M"
   2.514 +  shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
   2.515 +proof -
   2.516 +  let ?F = "\<lambda>x. if x \<in> f -` {\<infinity>} then H \<infinity> else if x \<in> f -` {-\<infinity>} then H (-\<infinity>) else H (ereal (real (f x)))"
   2.517 +  { fix x have "H (f x) = ?F x" by (cases "f x") auto }
   2.518 +  moreover 
   2.519 +  have "?F \<in> borel_measurable M"
   2.520 +    by (intro measurable_If_set f measurable_sets[OF f] H) auto
   2.521 +  ultimately
   2.522 +  show ?thesis by simp
   2.523  qed
   2.524  
   2.525 -lemma borel_measurable_real_of_ereal[simp, intro]:
   2.526 -  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: ereal)) \<in> borel_measurable M"
   2.527 -  using measurable_comp[OF f borel_measurable_real_of_ereal_borel] unfolding comp_def .
   2.528 +lemma
   2.529 +  fixes f :: "'a \<Rightarrow> ereal" assumes f[simp]: "f \<in> borel_measurable M"
   2.530 +  shows borel_measurable_ereal_abs[intro, simp]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
   2.531 +    and borel_measurable_ereal_inverse[simp, intro]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
   2.532 +    and borel_measurable_uminus_ereal[intro]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
   2.533 +  by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
   2.534 +
   2.535 +lemma borel_measurable_uminus_eq_ereal[simp]:
   2.536 +  "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   2.537 +proof
   2.538 +  assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
   2.539 +qed auto
   2.540 +
   2.541 +lemma sets_Collect_If_set:
   2.542 +  assumes "A \<inter> space M \<in> sets M" "{x \<in> space M. P x} \<in> sets M" "{x \<in> space M. Q x} \<in> sets M"
   2.543 +  shows "{x \<in> space M. if x \<in> A then P x else Q x} \<in> sets M"
   2.544 +proof -
   2.545 +  have *: "{x \<in> space M. if x \<in> A then P x else Q x} = 
   2.546 +    {x \<in> space M. if x \<in> A \<inter> space M then P x else Q x}" by auto
   2.547 +  show ?thesis unfolding * unfolding if_bool_eq_conj using assms
   2.548 +    by (auto intro!: sets_Collect simp: Int_def conj_commute)
   2.549 +qed
   2.550 +
   2.551 +lemma set_Collect_ereal2:
   2.552 +  fixes f g :: "'a \<Rightarrow> ereal" 
   2.553 +  assumes f: "f \<in> borel_measurable M"
   2.554 +  assumes g: "g \<in> borel_measurable M"
   2.555 +  assumes H: "{x \<in> space M. H (ereal (real (f x))) (ereal (real (g x)))} \<in> sets M"
   2.556 +    "{x \<in> space M. H (-\<infinity>) (ereal (real (g x)))} \<in> sets M"
   2.557 +    "{x \<in> space M. H (\<infinity>) (ereal (real (g x)))} \<in> sets M"
   2.558 +    "{x \<in> space M. H (ereal (real (f x))) (-\<infinity>)} \<in> sets M"
   2.559 +    "{x \<in> space M. H (ereal (real (f x))) (\<infinity>)} \<in> sets M"
   2.560 +  shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
   2.561 +proof -
   2.562 +  let ?G = "\<lambda>y x. if x \<in> g -` {\<infinity>} then H y \<infinity> else if x \<in> g -` {-\<infinity>} then H y (-\<infinity>) else H y (ereal (real (g x)))"
   2.563 +  let ?F = "\<lambda>x. if x \<in> f -` {\<infinity>} then ?G \<infinity> x else if x \<in> f -` {-\<infinity>} then ?G (-\<infinity>) x else ?G (ereal (real (f x))) x"
   2.564 +  { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
   2.565 +  moreover 
   2.566 +  have "{x \<in> space M. ?F x} \<in> sets M"
   2.567 +    by (intro sets_Collect H measurable_sets[OF f] measurable_sets[OF g] sets_Collect_If_set) auto
   2.568 +  ultimately
   2.569 +  show ?thesis by simp
   2.570 +qed
   2.571 +
   2.572 +lemma
   2.573 +  fixes f g :: "'a \<Rightarrow> ereal"
   2.574 +  assumes f: "f \<in> borel_measurable M"
   2.575 +  assumes g: "g \<in> borel_measurable M"
   2.576 +  shows borel_measurable_ereal_le[intro,simp]: "{x \<in> space M. f x \<le> g x} \<in> sets M"
   2.577 +    and borel_measurable_ereal_less[intro,simp]: "{x \<in> space M. f x < g x} \<in> sets M"
   2.578 +    and borel_measurable_ereal_eq[intro,simp]: "{w \<in> space M. f w = g w} \<in> sets M"
   2.579 +    and borel_measurable_ereal_neq[intro,simp]: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   2.580 +  using f g by (auto simp: f g set_Collect_ereal2[OF f g] intro!: sets_Collect_neg)
   2.581  
   2.582  lemma borel_measurable_ereal_iff:
   2.583    shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   2.584 @@ -1029,52 +1057,6 @@
   2.585    finally show "f \<in> borel_measurable M" .
   2.586  qed (auto intro: measurable_sets borel_measurable_real_of_ereal)
   2.587  
   2.588 -lemma less_eq_ge_measurable:
   2.589 -  fixes f :: "'a \<Rightarrow> 'c::linorder"
   2.590 -  shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
   2.591 -proof
   2.592 -  assume "f -` {a <..} \<inter> space M \<in> sets M"
   2.593 -  moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
   2.594 -  ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
   2.595 -next
   2.596 -  assume "f -` {..a} \<inter> space M \<in> sets M"
   2.597 -  moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
   2.598 -  ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
   2.599 -qed
   2.600 -
   2.601 -lemma greater_eq_le_measurable:
   2.602 -  fixes f :: "'a \<Rightarrow> 'c::linorder"
   2.603 -  shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
   2.604 -proof
   2.605 -  assume "f -` {a ..} \<inter> space M \<in> sets M"
   2.606 -  moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
   2.607 -  ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
   2.608 -next
   2.609 -  assume "f -` {..< a} \<inter> space M \<in> sets M"
   2.610 -  moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
   2.611 -  ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
   2.612 -qed
   2.613 -
   2.614 -lemma borel_measurable_uminus_borel_ereal:
   2.615 -  "(uminus :: ereal \<Rightarrow> ereal) \<in> borel_measurable borel"
   2.616 -proof (rule borel_measurableI)
   2.617 -  fix X :: "ereal set" assume "open X"
   2.618 -  have "uminus -` X = uminus ` X" by (force simp: image_iff)
   2.619 -  then have "open (uminus -` X)" using `open X` ereal_open_uminus by auto
   2.620 -  then show "uminus -` X \<inter> space borel \<in> sets borel" by auto
   2.621 -qed
   2.622 -
   2.623 -lemma borel_measurable_uminus_ereal[intro]:
   2.624 -  assumes "f \<in> borel_measurable M"
   2.625 -  shows "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
   2.626 -  using measurable_comp[OF assms borel_measurable_uminus_borel_ereal] by (simp add: comp_def)
   2.627 -
   2.628 -lemma borel_measurable_uminus_eq_ereal[simp]:
   2.629 -  "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   2.630 -proof
   2.631 -  assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
   2.632 -qed auto
   2.633 -
   2.634  lemma borel_measurable_eq_atMost_ereal:
   2.635    fixes f :: "'a \<Rightarrow> ereal"
   2.636    shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
   2.637 @@ -1118,94 +1100,88 @@
   2.638    then show "f \<in> borel_measurable M" by simp
   2.639  qed (simp add: measurable_sets)
   2.640  
   2.641 +lemma greater_eq_le_measurable:
   2.642 +  fixes f :: "'a \<Rightarrow> 'c::linorder"
   2.643 +  shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
   2.644 +proof
   2.645 +  assume "f -` {a ..} \<inter> space M \<in> sets M"
   2.646 +  moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
   2.647 +  ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
   2.648 +next
   2.649 +  assume "f -` {..< a} \<inter> space M \<in> sets M"
   2.650 +  moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
   2.651 +  ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
   2.652 +qed
   2.653 +
   2.654  lemma borel_measurable_ereal_iff_less:
   2.655    "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
   2.656    unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
   2.657  
   2.658 +lemma less_eq_ge_measurable:
   2.659 +  fixes f :: "'a \<Rightarrow> 'c::linorder"
   2.660 +  shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
   2.661 +proof
   2.662 +  assume "f -` {a <..} \<inter> space M \<in> sets M"
   2.663 +  moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
   2.664 +  ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
   2.665 +next
   2.666 +  assume "f -` {..a} \<inter> space M \<in> sets M"
   2.667 +  moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
   2.668 +  ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
   2.669 +qed
   2.670 +
   2.671  lemma borel_measurable_ereal_iff_ge:
   2.672    "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
   2.673    unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
   2.674  
   2.675 -lemma borel_measurable_ereal_eq_const:
   2.676 -  fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
   2.677 -  shows "{x\<in>space M. f x = c} \<in> sets M"
   2.678 -proof -
   2.679 -  have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
   2.680 -  then show ?thesis using assms by (auto intro!: measurable_sets)
   2.681 -qed
   2.682 -
   2.683 -lemma borel_measurable_ereal_neq_const:
   2.684 -  fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
   2.685 -  shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
   2.686 -proof -
   2.687 -  have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
   2.688 -  then show ?thesis using assms by (auto intro!: measurable_sets)
   2.689 -qed
   2.690 -
   2.691 -lemma borel_measurable_ereal_le[intro,simp]:
   2.692 -  fixes f g :: "'a \<Rightarrow> ereal"
   2.693 +lemma borel_measurable_ereal2:
   2.694 +  fixes f g :: "'a \<Rightarrow> ereal" 
   2.695    assumes f: "f \<in> borel_measurable M"
   2.696    assumes g: "g \<in> borel_measurable M"
   2.697 -  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
   2.698 +  assumes H: "(\<lambda>x. H (ereal (real (f x))) (ereal (real (g x)))) \<in> borel_measurable M"
   2.699 +    "(\<lambda>x. H (-\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
   2.700 +    "(\<lambda>x. H (\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
   2.701 +    "(\<lambda>x. H (ereal (real (f x))) (-\<infinity>)) \<in> borel_measurable M"
   2.702 +    "(\<lambda>x. H (ereal (real (f x))) (\<infinity>)) \<in> borel_measurable M"
   2.703 +  shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   2.704  proof -
   2.705 -  have "{x \<in> space M. f x \<le> g x} =
   2.706 -    {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>
   2.707 -    f -` {-\<infinity>} \<inter> space M \<union> g -` {\<infinity>} \<inter> space M" (is "?l = ?r")
   2.708 -  proof (intro set_eqI)
   2.709 -    fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases rule: ereal2_cases[of "f x" "g x"]) auto
   2.710 -  qed
   2.711 -  with f g show ?thesis by (auto intro!: Un simp: measurable_sets)
   2.712 +  let ?G = "\<lambda>y x. if x \<in> g -` {\<infinity>} then H y \<infinity> else if x \<in> g -` {-\<infinity>} then H y (-\<infinity>) else H y (ereal (real (g x)))"
   2.713 +  let ?F = "\<lambda>x. if x \<in> f -` {\<infinity>} then ?G \<infinity> x else if x \<in> f -` {-\<infinity>} then ?G (-\<infinity>) x else ?G (ereal (real (f x))) x"
   2.714 +  { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
   2.715 +  moreover 
   2.716 +  have "?F \<in> borel_measurable M"
   2.717 +    by (intro measurable_If_set measurable_sets[OF f] measurable_sets[OF g] H) auto
   2.718 +  ultimately
   2.719 +  show ?thesis by simp
   2.720  qed
   2.721  
   2.722 -lemma borel_measurable_ereal_less[intro,simp]:
   2.723 -  fixes f :: "'a \<Rightarrow> ereal"
   2.724 -  assumes f: "f \<in> borel_measurable M"
   2.725 -  assumes g: "g \<in> borel_measurable M"
   2.726 -  shows "{x \<in> space M. f x < g x} \<in> sets M"
   2.727 -proof -
   2.728 -  have "{x \<in> space M. f x < g x} = space M - {x \<in> space M. g x \<le> f x}" by auto
   2.729 -  then show ?thesis using g f by auto
   2.730 -qed
   2.731 -
   2.732 -lemma borel_measurable_ereal_eq[intro,simp]:
   2.733 -  fixes f :: "'a \<Rightarrow> ereal"
   2.734 -  assumes f: "f \<in> borel_measurable M"
   2.735 -  assumes g: "g \<in> borel_measurable M"
   2.736 -  shows "{w \<in> space M. f w = g w} \<in> sets M"
   2.737 -proof -
   2.738 -  have "{x \<in> space M. f x = g x} = {x \<in> space M. g x \<le> f x} \<inter> {x \<in> space M. f x \<le> g x}" by auto
   2.739 -  then show ?thesis using g f by auto
   2.740 -qed
   2.741 -
   2.742 -lemma borel_measurable_ereal_neq[intro,simp]:
   2.743 -  fixes f :: "'a \<Rightarrow> ereal"
   2.744 -  assumes f: "f \<in> borel_measurable M"
   2.745 -  assumes g: "g \<in> borel_measurable M"
   2.746 -  shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   2.747 -proof -
   2.748 -  have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}" by auto
   2.749 -  thus ?thesis using f g by auto
   2.750 -qed
   2.751 +lemma
   2.752 +  fixes f :: "'a \<Rightarrow> ereal" assumes f: "f \<in> borel_measurable M"
   2.753 +  shows borel_measurable_ereal_eq_const: "{x\<in>space M. f x = c} \<in> sets M"
   2.754 +    and borel_measurable_ereal_neq_const: "{x\<in>space M. f x \<noteq> c} \<in> sets M"
   2.755 +  using f by auto
   2.756  
   2.757  lemma split_sets:
   2.758    "{x\<in>space M. P x \<or> Q x} = {x\<in>space M. P x} \<union> {x\<in>space M. Q x}"
   2.759    "{x\<in>space M. P x \<and> Q x} = {x\<in>space M. P x} \<inter> {x\<in>space M. Q x}"
   2.760    by auto
   2.761  
   2.762 -lemma borel_measurable_ereal_add[intro, simp]:
   2.763 +lemma
   2.764    fixes f :: "'a \<Rightarrow> ereal"
   2.765 -  assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   2.766 -  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   2.767 -proof -
   2.768 -  { fix x assume "x \<in> space M" then have "f x + g x =
   2.769 -      (if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
   2.770 -        else if f x = -\<infinity> \<or> g x = -\<infinity> then -\<infinity>
   2.771 -        else ereal (real (f x) + real (g x)))"
   2.772 -      by (cases rule: ereal2_cases[of "f x" "g x"]) auto }
   2.773 -  with assms show ?thesis
   2.774 -    by (auto cong: measurable_cong simp: split_sets
   2.775 -             intro!: Un measurable_If measurable_sets)
   2.776 -qed
   2.777 +  assumes [simp]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   2.778 +  shows borel_measurable_ereal_add[intro, simp]: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   2.779 +    and borel_measurable_ereal_times[intro, simp]: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   2.780 +    and borel_measurable_ereal_min[simp, intro]: "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
   2.781 +    and borel_measurable_ereal_max[simp, intro]: "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   2.782 +  by (auto simp add: borel_measurable_ereal2 measurable_If min_def max_def)
   2.783 +
   2.784 +lemma
   2.785 +  fixes f g :: "'a \<Rightarrow> ereal"
   2.786 +  assumes "f \<in> borel_measurable M"
   2.787 +  assumes "g \<in> borel_measurable M"
   2.788 +  shows borel_measurable_ereal_diff[simp, intro]: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   2.789 +    and borel_measurable_ereal_divide[simp, intro]: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
   2.790 +  unfolding minus_ereal_def divide_ereal_def using assms by auto
   2.791  
   2.792  lemma borel_measurable_ereal_setsum[simp, intro]:
   2.793    fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
   2.794 @@ -1215,39 +1191,7 @@
   2.795    assume "finite S"
   2.796    thus ?thesis using assms
   2.797      by induct auto
   2.798 -qed (simp add: borel_measurable_const)
   2.799 -
   2.800 -lemma borel_measurable_ereal_abs[intro, simp]:
   2.801 -  fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
   2.802 -  shows "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
   2.803 -proof -
   2.804 -  { fix x have "\<bar>f x\<bar> = (if 0 \<le> f x then f x else - f x)" by auto }
   2.805 -  then show ?thesis using assms by (auto intro!: measurable_If)
   2.806 -qed
   2.807 -
   2.808 -lemma borel_measurable_ereal_times[intro, simp]:
   2.809 -  fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   2.810 -  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   2.811 -proof -
   2.812 -  { fix f g :: "'a \<Rightarrow> ereal"
   2.813 -    assume b: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   2.814 -      and pos: "\<And>x. 0 \<le> f x" "\<And>x. 0 \<le> g x"
   2.815 -    { fix x have *: "f x * g x = (if f x = 0 \<or> g x = 0 then 0
   2.816 -        else if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
   2.817 -        else ereal (real (f x) * real (g x)))"
   2.818 -      apply (cases rule: ereal2_cases[of "f x" "g x"])
   2.819 -      using pos[of x] by auto }
   2.820 -    with b have "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   2.821 -      by (auto cong: measurable_cong simp: split_sets
   2.822 -               intro!: Un measurable_If measurable_sets) }
   2.823 -  note pos_times = this
   2.824 -  have *: "(\<lambda>x. f x * g x) =
   2.825 -    (\<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>))"
   2.826 -    by (auto simp: fun_eq_iff)
   2.827 -  show ?thesis using assms unfolding *
   2.828 -    by (intro measurable_If pos_times borel_measurable_uminus_ereal)
   2.829 -       (auto simp: split_sets intro!: Int)
   2.830 -qed
   2.831 +qed simp
   2.832  
   2.833  lemma borel_measurable_ereal_setprod[simp, intro]:
   2.834    fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
   2.835 @@ -1258,20 +1202,6 @@
   2.836    thus ?thesis using assms by induct auto
   2.837  qed simp
   2.838  
   2.839 -lemma borel_measurable_ereal_min[simp, intro]:
   2.840 -  fixes f g :: "'a \<Rightarrow> ereal"
   2.841 -  assumes "f \<in> borel_measurable M"
   2.842 -  assumes "g \<in> borel_measurable M"
   2.843 -  shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
   2.844 -  using assms unfolding min_def by (auto intro!: measurable_If)
   2.845 -
   2.846 -lemma borel_measurable_ereal_max[simp, intro]:
   2.847 -  fixes f g :: "'a \<Rightarrow> ereal"
   2.848 -  assumes "f \<in> borel_measurable M"
   2.849 -  and "g \<in> borel_measurable M"
   2.850 -  shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   2.851 -  using assms unfolding max_def by (auto intro!: measurable_If)
   2.852 -
   2.853  lemma borel_measurable_SUP[simp, intro]:
   2.854    fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
   2.855    assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
   2.856 @@ -1298,38 +1228,24 @@
   2.857      using assms by auto
   2.858  qed
   2.859  
   2.860 -lemma borel_measurable_liminf[simp, intro]:
   2.861 -  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   2.862 -  assumes "\<And>i. f i \<in> borel_measurable M"
   2.863 -  shows "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   2.864 -  unfolding liminf_SUPR_INFI using assms by auto
   2.865 -
   2.866 -lemma borel_measurable_limsup[simp, intro]:
   2.867 +lemma
   2.868    fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   2.869    assumes "\<And>i. f i \<in> borel_measurable M"
   2.870 -  shows "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
   2.871 -  unfolding limsup_INFI_SUPR using assms by auto
   2.872 -
   2.873 -lemma borel_measurable_ereal_diff[simp, intro]:
   2.874 -  fixes f g :: "'a \<Rightarrow> ereal"
   2.875 -  assumes "f \<in> borel_measurable M"
   2.876 -  assumes "g \<in> borel_measurable M"
   2.877 -  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   2.878 -  unfolding minus_ereal_def using assms by auto
   2.879 +  shows borel_measurable_liminf[simp, intro]: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   2.880 +    and borel_measurable_limsup[simp, intro]: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
   2.881 +  unfolding liminf_SUPR_INFI limsup_INFI_SUPR using assms by auto
   2.882  
   2.883 -lemma borel_measurable_ereal_inverse[simp, intro]:
   2.884 -  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
   2.885 +lemma borel_measurable_ereal_LIMSEQ:
   2.886 +  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   2.887 +  assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
   2.888 +  and u: "\<And>i. u i \<in> borel_measurable M"
   2.889 +  shows "u' \<in> borel_measurable M"
   2.890  proof -
   2.891 -  { fix x have "inverse (f x) = (if f x = 0 then \<infinity> else ereal (inverse (real (f x))))"
   2.892 -      by (cases "f x") auto }
   2.893 -  with f show ?thesis
   2.894 -    by (auto intro!: measurable_If)
   2.895 +  have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
   2.896 +    using u' by (simp add: lim_imp_Liminf[symmetric])
   2.897 +  then show ?thesis by (simp add: u cong: measurable_cong)
   2.898  qed
   2.899  
   2.900 -lemma borel_measurable_ereal_divide[simp, intro]:
   2.901 -  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. f x / g x :: ereal) \<in> borel_measurable M"
   2.902 -  unfolding divide_ereal_def by auto
   2.903 -
   2.904  lemma borel_measurable_psuminf[simp, intro]:
   2.905    fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   2.906    assumes "\<And>i. f i \<in> borel_measurable M" and pos: "\<And>i x. x \<in> space M \<Longrightarrow> 0 \<le> f i x"
   2.907 @@ -1354,4 +1270,38 @@
   2.908    ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
   2.909  qed
   2.910  
   2.911 -end
   2.912 +lemma sets_Collect_Cauchy: 
   2.913 +  fixes f :: "nat \<Rightarrow> 'a => real"
   2.914 +  assumes f: "\<And>i. f i \<in> borel_measurable M"
   2.915 +  shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
   2.916 +  unfolding Cauchy_iff2 using f by (auto intro!: sets_Collect)
   2.917 +
   2.918 +lemma borel_measurable_lim:
   2.919 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
   2.920 +  assumes f: "\<And>i. f i \<in> borel_measurable M"
   2.921 +  shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
   2.922 +proof -
   2.923 +  have *: "\<And>x. lim (\<lambda>i. f i x) =
   2.924 +    (if Cauchy (\<lambda>i. f i x) then lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) else (THE x. False))"
   2.925 +    by (auto simp: lim_def convergent_eq_cauchy[symmetric])
   2.926 +  { fix x have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
   2.927 +      by (cases "Cauchy (\<lambda>i. f i x)")
   2.928 +         (auto simp add: convergent_eq_cauchy[symmetric] convergent_def) }
   2.929 +  note convergent = this
   2.930 +  show ?thesis
   2.931 +    unfolding *
   2.932 +    apply (intro measurable_If sets_Collect_Cauchy f borel_measurable_const)
   2.933 +    apply (rule borel_measurable_LIMSEQ)
   2.934 +    apply (rule convergent_LIMSEQ_iff[THEN iffD1, OF convergent])
   2.935 +    apply (intro measurable_If sets_Collect_Cauchy f borel_measurable_const)
   2.936 +    done
   2.937 +qed
   2.938 +
   2.939 +lemma borel_measurable_suminf:
   2.940 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
   2.941 +  assumes f: "\<And>i. f i \<in> borel_measurable M"
   2.942 +  shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   2.943 +  unfolding suminf_def sums_def[abs_def] lim_def[symmetric]
   2.944 +  by (simp add: f borel_measurable_lim)
   2.945 +
   2.946 +end 
     3.1 --- a/src/HOL/Probability/Caratheodory.thy	Wed Oct 10 15:16:44 2012 +0200
     3.2 +++ b/src/HOL/Probability/Caratheodory.thy	Wed Oct 10 15:17:18 2012 +0200
     3.3 @@ -9,12 +9,6 @@
     3.4    imports Measure_Space
     3.5  begin
     3.6  
     3.7 -lemma sums_def2:
     3.8 -  "f sums x \<longleftrightarrow> (\<lambda>n. (\<Sum>i\<le>n. f i)) ----> x"
     3.9 -  unfolding sums_def
    3.10 -  apply (subst LIMSEQ_Suc_iff[symmetric])
    3.11 -  unfolding atLeastLessThanSuc_atLeastAtMost atLeast0AtMost ..
    3.12 -
    3.13  text {*
    3.14    Originally from the Hurd/Coble measure theory development, translated by Lawrence Paulson.
    3.15  *}
    3.16 @@ -684,146 +678,6 @@
    3.17  
    3.18  subsubsection {*Alternative instances of caratheodory*}
    3.19  
    3.20 -lemma (in ring_of_sets) countably_additive_iff_continuous_from_below:
    3.21 -  assumes f: "positive M f" "additive M f"
    3.22 -  shows "countably_additive M f \<longleftrightarrow>
    3.23 -    (\<forall>A. range A \<subseteq> M \<longrightarrow> incseq A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Union>i. A i))"
    3.24 -  unfolding countably_additive_def
    3.25 -proof safe
    3.26 -  assume count_sum: "\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> UNION UNIV A \<in> M \<longrightarrow> (\<Sum>i. f (A i)) = f (UNION UNIV A)"
    3.27 -  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
    3.28 -  then have dA: "range (disjointed A) \<subseteq> M" by (auto simp: range_disjointed_sets)
    3.29 -  with count_sum[THEN spec, of "disjointed A"] A(3)
    3.30 -  have f_UN: "(\<Sum>i. f (disjointed A i)) = f (\<Union>i. A i)"
    3.31 -    by (auto simp: UN_disjointed_eq disjoint_family_disjointed)
    3.32 -  moreover have "(\<lambda>n. (\<Sum>i=0..<n. f (disjointed A i))) ----> (\<Sum>i. f (disjointed A i))"
    3.33 -    using f(1)[unfolded positive_def] dA
    3.34 -    by (auto intro!: summable_sumr_LIMSEQ_suminf summable_ereal_pos)
    3.35 -  from LIMSEQ_Suc[OF this]
    3.36 -  have "(\<lambda>n. (\<Sum>i\<le>n. f (disjointed A i))) ----> (\<Sum>i. f (disjointed A i))"
    3.37 -    unfolding atLeastLessThanSuc_atLeastAtMost atLeast0AtMost .
    3.38 -  moreover have "\<And>n. (\<Sum>i\<le>n. f (disjointed A i)) = f (A n)"
    3.39 -    using disjointed_additive[OF f A(1,2)] .
    3.40 -  ultimately show "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)" by simp
    3.41 -next
    3.42 -  assume cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> incseq A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Union>i. A i)"
    3.43 -  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "disjoint_family A" "(\<Union>i. A i) \<in> M"
    3.44 -  have *: "(\<Union>n. (\<Union>i\<le>n. A i)) = (\<Union>i. A i)" by auto
    3.45 -  have "(\<lambda>n. f (\<Union>i\<le>n. A i)) ----> f (\<Union>i. A i)"
    3.46 -  proof (unfold *[symmetric], intro cont[rule_format])
    3.47 -    show "range (\<lambda>i. \<Union> i\<le>i. A i) \<subseteq> M" "(\<Union>i. \<Union> i\<le>i. A i) \<in> M"
    3.48 -      using A * by auto
    3.49 -  qed (force intro!: incseq_SucI)
    3.50 -  moreover have "\<And>n. f (\<Union>i\<le>n. A i) = (\<Sum>i\<le>n. f (A i))"
    3.51 -    using A
    3.52 -    by (intro additive_sum[OF f, of _ A, symmetric])
    3.53 -       (auto intro: disjoint_family_on_mono[where B=UNIV])
    3.54 -  ultimately
    3.55 -  have "(\<lambda>i. f (A i)) sums f (\<Union>i. A i)"
    3.56 -    unfolding sums_def2 by simp
    3.57 -  from sums_unique[OF this]
    3.58 -  show "(\<Sum>i. f (A i)) = f (\<Union>i. A i)" by simp
    3.59 -qed
    3.60 -
    3.61 -lemma (in ring_of_sets) continuous_from_above_iff_empty_continuous:
    3.62 -  assumes f: "positive M f" "additive M f"
    3.63 -  shows "(\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) \<in> M \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Inter>i. A i))
    3.64 -     \<longleftrightarrow> (\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> 0)"
    3.65 -proof safe
    3.66 -  assume cont: "(\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) \<in> M \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Inter>i. A i))"
    3.67 -  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "decseq A" "(\<Inter>i. A i) = {}" "\<forall>i. f (A i) \<noteq> \<infinity>"
    3.68 -  with cont[THEN spec, of A] show "(\<lambda>i. f (A i)) ----> 0"
    3.69 -    using `positive M f`[unfolded positive_def] by auto
    3.70 -next
    3.71 -  assume cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> 0"
    3.72 -  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "decseq A" "(\<Inter>i. A i) \<in> M" "\<forall>i. f (A i) \<noteq> \<infinity>"
    3.73 -
    3.74 -  have f_mono: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<subseteq> b \<Longrightarrow> f a \<le> f b"
    3.75 -    using additive_increasing[OF f] unfolding increasing_def by simp
    3.76 -
    3.77 -  have decseq_fA: "decseq (\<lambda>i. f (A i))"
    3.78 -    using A by (auto simp: decseq_def intro!: f_mono)
    3.79 -  have decseq: "decseq (\<lambda>i. A i - (\<Inter>i. A i))"
    3.80 -    using A by (auto simp: decseq_def)
    3.81 -  then have decseq_f: "decseq (\<lambda>i. f (A i - (\<Inter>i. A i)))"
    3.82 -    using A unfolding decseq_def by (auto intro!: f_mono Diff)
    3.83 -  have "f (\<Inter>x. A x) \<le> f (A 0)"
    3.84 -    using A by (auto intro!: f_mono)
    3.85 -  then have f_Int_fin: "f (\<Inter>x. A x) \<noteq> \<infinity>"
    3.86 -    using A by auto
    3.87 -  { fix i
    3.88 -    have "f (A i - (\<Inter>i. A i)) \<le> f (A i)" using A by (auto intro!: f_mono)
    3.89 -    then have "f (A i - (\<Inter>i. A i)) \<noteq> \<infinity>"
    3.90 -      using A by auto }
    3.91 -  note f_fin = this
    3.92 -  have "(\<lambda>i. f (A i - (\<Inter>i. A i))) ----> 0"
    3.93 -  proof (intro cont[rule_format, OF _ decseq _ f_fin])
    3.94 -    show "range (\<lambda>i. A i - (\<Inter>i. A i)) \<subseteq> M" "(\<Inter>i. A i - (\<Inter>i. A i)) = {}"
    3.95 -      using A by auto
    3.96 -  qed
    3.97 -  from INF_Lim_ereal[OF decseq_f this]
    3.98 -  have "(INF n. f (A n - (\<Inter>i. A i))) = 0" .
    3.99 -  moreover have "(INF n. f (\<Inter>i. A i)) = f (\<Inter>i. A i)"
   3.100 -    by auto
   3.101 -  ultimately have "(INF n. f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i)) = 0 + f (\<Inter>i. A i)"
   3.102 -    using A(4) f_fin f_Int_fin
   3.103 -    by (subst INFI_ereal_add) (auto simp: decseq_f)
   3.104 -  moreover {
   3.105 -    fix n
   3.106 -    have "f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i) = f ((A n - (\<Inter>i. A i)) \<union> (\<Inter>i. A i))"
   3.107 -      using A by (subst f(2)[THEN additiveD]) auto
   3.108 -    also have "(A n - (\<Inter>i. A i)) \<union> (\<Inter>i. A i) = A n"
   3.109 -      by auto
   3.110 -    finally have "f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i) = f (A n)" . }
   3.111 -  ultimately have "(INF n. f (A n)) = f (\<Inter>i. A i)"
   3.112 -    by simp
   3.113 -  with LIMSEQ_ereal_INFI[OF decseq_fA]
   3.114 -  show "(\<lambda>i. f (A i)) ----> f (\<Inter>i. A i)" by simp
   3.115 -qed
   3.116 -
   3.117 -lemma positiveD1: "positive M f \<Longrightarrow> f {} = 0" by (auto simp: positive_def)
   3.118 -lemma positiveD2: "positive M f \<Longrightarrow> A \<in> M \<Longrightarrow> 0 \<le> f A" by (auto simp: positive_def)
   3.119 -
   3.120 -lemma (in ring_of_sets) empty_continuous_imp_continuous_from_below:
   3.121 -  assumes f: "positive M f" "additive M f" "\<forall>A\<in>M. f A \<noteq> \<infinity>"
   3.122 -  assumes cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   3.123 -  assumes A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
   3.124 -  shows "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)"
   3.125 -proof -
   3.126 -  have "\<forall>A\<in>M. \<exists>x. f A = ereal x"
   3.127 -  proof
   3.128 -    fix A assume "A \<in> M" with f show "\<exists>x. f A = ereal x"
   3.129 -      unfolding positive_def by (cases "f A") auto
   3.130 -  qed
   3.131 -  from bchoice[OF this] guess f' .. note f' = this[rule_format]
   3.132 -  from A have "(\<lambda>i. f ((\<Union>i. A i) - A i)) ----> 0"
   3.133 -    by (intro cont[rule_format]) (auto simp: decseq_def incseq_def)
   3.134 -  moreover
   3.135 -  { fix i
   3.136 -    have "f ((\<Union>i. A i) - A i) + f (A i) = f ((\<Union>i. A i) - A i \<union> A i)"
   3.137 -      using A by (intro f(2)[THEN additiveD, symmetric]) auto
   3.138 -    also have "(\<Union>i. A i) - A i \<union> A i = (\<Union>i. A i)"
   3.139 -      by auto
   3.140 -    finally have "f' (\<Union>i. A i) - f' (A i) = f' ((\<Union>i. A i) - A i)"
   3.141 -      using A by (subst (asm) (1 2 3) f') auto
   3.142 -    then have "f ((\<Union>i. A i) - A i) = ereal (f' (\<Union>i. A i) - f' (A i))"
   3.143 -      using A f' by auto }
   3.144 -  ultimately have "(\<lambda>i. f' (\<Union>i. A i) - f' (A i)) ----> 0"
   3.145 -    by (simp add: zero_ereal_def)
   3.146 -  then have "(\<lambda>i. f' (A i)) ----> f' (\<Union>i. A i)"
   3.147 -    by (rule LIMSEQ_diff_approach_zero2[OF tendsto_const])
   3.148 -  then show "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)"
   3.149 -    using A by (subst (1 2) f') auto
   3.150 -qed
   3.151 -
   3.152 -lemma (in ring_of_sets) empty_continuous_imp_countably_additive:
   3.153 -  assumes f: "positive M f" "additive M f" and fin: "\<forall>A\<in>M. f A \<noteq> \<infinity>"
   3.154 -  assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   3.155 -  shows "countably_additive M f"
   3.156 -  using countably_additive_iff_continuous_from_below[OF f]
   3.157 -  using empty_continuous_imp_continuous_from_below[OF f fin] cont
   3.158 -  by blast
   3.159 -
   3.160  lemma (in ring_of_sets) caratheodory_empty_continuous:
   3.161    assumes f: "positive M f" "additive M f" and fin: "\<And>A. A \<in> M \<Longrightarrow> f A \<noteq> \<infinity>"
   3.162    assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
     4.1 --- a/src/HOL/Probability/Finite_Product_Measure.thy	Wed Oct 10 15:16:44 2012 +0200
     4.2 +++ b/src/HOL/Probability/Finite_Product_Measure.thy	Wed Oct 10 15:17:18 2012 +0200
     4.3 @@ -44,45 +44,45 @@
     4.4    unfolding extensional_def by auto
     4.5  
     4.6  definition
     4.7 -  "merge I x J y = (\<lambda>i. if i \<in> I then x i else if i \<in> J then y i else undefined)"
     4.8 +  "merge I J = (\<lambda>(x, y) i. if i \<in> I then x i else if i \<in> J then y i else undefined)"
     4.9  
    4.10  lemma merge_apply[simp]:
    4.11 -  "I \<inter> J = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I x J y i = x i"
    4.12 -  "I \<inter> J = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I x J y i = y i"
    4.13 -  "J \<inter> I = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I x J y i = x i"
    4.14 -  "J \<inter> I = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I x J y i = y i"
    4.15 -  "i \<notin> I \<Longrightarrow> i \<notin> J \<Longrightarrow> merge I x J y i = undefined"
    4.16 +  "I \<inter> J = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I J (x, y) i = x i"
    4.17 +  "I \<inter> J = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I J (x, y) i = y i"
    4.18 +  "J \<inter> I = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I J (x, y) i = x i"
    4.19 +  "J \<inter> I = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I J (x, y) i = y i"
    4.20 +  "i \<notin> I \<Longrightarrow> i \<notin> J \<Longrightarrow> merge I J (x, y) i = undefined"
    4.21    unfolding merge_def by auto
    4.22  
    4.23  lemma merge_commute:
    4.24 -  "I \<inter> J = {} \<Longrightarrow> merge I x J y = merge J y I x"
    4.25 +  "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) = merge J I (y, x)"
    4.26    by (auto simp: merge_def intro!: ext)
    4.27  
    4.28  lemma Pi_cancel_merge_range[simp]:
    4.29 -  "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge I A J B) \<longleftrightarrow> x \<in> Pi I A"
    4.30 -  "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge J B I A) \<longleftrightarrow> x \<in> Pi I A"
    4.31 -  "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge I A J B) \<longleftrightarrow> x \<in> Pi I A"
    4.32 -  "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge J B I A) \<longleftrightarrow> x \<in> Pi I A"
    4.33 +  "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge I J (A, B)) \<longleftrightarrow> x \<in> Pi I A"
    4.34 +  "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge J I (B, A)) \<longleftrightarrow> x \<in> Pi I A"
    4.35 +  "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge I J (A, B)) \<longleftrightarrow> x \<in> Pi I A"
    4.36 +  "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge J I (B, A)) \<longleftrightarrow> x \<in> Pi I A"
    4.37    by (auto simp: Pi_def)
    4.38  
    4.39  lemma Pi_cancel_merge[simp]:
    4.40 -  "I \<inter> J = {} \<Longrightarrow> merge I x J y \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    4.41 -  "J \<inter> I = {} \<Longrightarrow> merge I x J y \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    4.42 -  "I \<inter> J = {} \<Longrightarrow> merge I x J y \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    4.43 -  "J \<inter> I = {} \<Longrightarrow> merge I x J y \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    4.44 +  "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    4.45 +  "J \<inter> I = {} \<Longrightarrow> merge I J (x, y) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    4.46 +  "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    4.47 +  "J \<inter> I = {} \<Longrightarrow> merge I J (x, y) \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    4.48    by (auto simp: Pi_def)
    4.49  
    4.50 -lemma extensional_merge[simp]: "merge I x J y \<in> extensional (I \<union> J)"
    4.51 +lemma extensional_merge[simp]: "merge I J (x, y) \<in> extensional (I \<union> J)"
    4.52    by (auto simp: extensional_def)
    4.53  
    4.54  lemma restrict_Pi_cancel: "restrict x I \<in> Pi I A \<longleftrightarrow> x \<in> Pi I A"
    4.55    by (auto simp: restrict_def Pi_def)
    4.56  
    4.57  lemma restrict_merge[simp]:
    4.58 -  "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
    4.59 -  "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    4.60 -  "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
    4.61 -  "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    4.62 +  "I \<inter> J = {} \<Longrightarrow> restrict (merge I J (x, y)) I = restrict x I"
    4.63 +  "I \<inter> J = {} \<Longrightarrow> restrict (merge I J (x, y)) J = restrict y J"
    4.64 +  "J \<inter> I = {} \<Longrightarrow> restrict (merge I J (x, y)) I = restrict x I"
    4.65 +  "J \<inter> I = {} \<Longrightarrow> restrict (merge I J (x, y)) J = restrict y J"
    4.66    by (auto simp: restrict_def)
    4.67  
    4.68  lemma extensional_insert_undefined[intro, simp]:
    4.69 @@ -95,7 +95,7 @@
    4.70    shows "a \<in> extensional (insert i I)"
    4.71    using assms unfolding extensional_def by auto
    4.72  
    4.73 -lemma merge_singleton[simp]: "i \<notin> I \<Longrightarrow> merge I x {i} y = restrict (x(i := y i)) (insert i I)"
    4.74 +lemma merge_singleton[simp]: "i \<notin> I \<Longrightarrow> merge I {i} (x,y) = restrict (x(i := y i)) (insert i I)"
    4.75    unfolding merge_def by (auto simp: fun_eq_iff)
    4.76  
    4.77  lemma Pi_Int: "Pi I E \<inter> Pi I F = (\<Pi> i\<in>I. E i \<inter> F i)"
    4.78 @@ -118,24 +118,24 @@
    4.79  
    4.80  lemma restrict_vimage:
    4.81    assumes "I \<inter> J = {}"
    4.82 -  shows "(\<lambda>x. (restrict x I, restrict x J)) -` (Pi\<^isub>E I E \<times> Pi\<^isub>E J F) = Pi (I \<union> J) (merge I E J F)"
    4.83 +  shows "(\<lambda>x. (restrict x I, restrict x J)) -` (Pi\<^isub>E I E \<times> Pi\<^isub>E J F) = Pi (I \<union> J) (merge I J (E, F))"
    4.84    using assms by (auto simp: restrict_Pi_cancel)
    4.85  
    4.86  lemma merge_vimage:
    4.87    assumes "I \<inter> J = {}"
    4.88 -  shows "(\<lambda>(x,y). merge I x J y) -` Pi\<^isub>E (I \<union> J) E = Pi I E \<times> Pi J E"
    4.89 +  shows "merge I J -` Pi\<^isub>E (I \<union> J) E = Pi I E \<times> Pi J E"
    4.90    using assms by (auto simp: restrict_Pi_cancel)
    4.91  
    4.92  lemma restrict_fupd[simp]: "i \<notin> I \<Longrightarrow> restrict (f (i := x)) I = restrict f I"
    4.93    by (auto simp: restrict_def)
    4.94  
    4.95  lemma merge_restrict[simp]:
    4.96 -  "merge I (restrict x I) J y = merge I x J y"
    4.97 -  "merge I x J (restrict y J) = merge I x J y"
    4.98 +  "merge I J (restrict x I, y) = merge I J (x, y)"
    4.99 +  "merge I J (x, restrict y J) = merge I J (x, y)"
   4.100    unfolding merge_def by auto
   4.101  
   4.102  lemma merge_x_x_eq_restrict[simp]:
   4.103 -  "merge I x J x = restrict x (I \<union> J)"
   4.104 +  "merge I J (x, x) = restrict x (I \<union> J)"
   4.105    unfolding merge_def by auto
   4.106  
   4.107  lemma Pi_fupd_iff: "i \<in> I \<Longrightarrow> f \<in> Pi I (B(i := A)) \<longleftrightarrow> f \<in> Pi (I - {i}) B \<and> f i \<in> A"
   4.108 @@ -504,8 +504,7 @@
   4.109  qed (insert `i \<in> I`, auto simp: space_PiM)
   4.110  
   4.111  lemma measurable_add_dim:
   4.112 -  assumes "i \<notin> I"
   4.113 -  shows "(\<lambda>(f, y). f(i := y)) \<in> measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) (Pi\<^isub>M (insert i I) M)"
   4.114 +  "(\<lambda>(f, y). f(i := y)) \<in> measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) (Pi\<^isub>M (insert i I) M)"
   4.115      (is "?f \<in> measurable ?P ?I")
   4.116  proof (rule measurable_PiM_single)
   4.117    fix j A assume j: "j \<in> insert i I" and A: "A \<in> sets (M j)"
   4.118 @@ -519,19 +518,18 @@
   4.119  qed (auto simp: space_pair_measure space_PiM)
   4.120  
   4.121  lemma measurable_merge:
   4.122 -  assumes "I \<inter> J = {}"
   4.123 -  shows "(\<lambda>(x, y). merge I x J y) \<in> measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) (Pi\<^isub>M (I \<union> J) M)"
   4.124 +  "merge I J \<in> measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) (Pi\<^isub>M (I \<union> J) M)"
   4.125      (is "?f \<in> measurable ?P ?U")
   4.126  proof (rule measurable_PiM_single)
   4.127    fix i A assume A: "A \<in> sets (M i)" "i \<in> I \<union> J"
   4.128 -  then have "{\<omega> \<in> space ?P. prod_case (\<lambda>x. merge I x J) \<omega> i \<in> A} =
   4.129 +  then have "{\<omega> \<in> space ?P. merge I J \<omega> i \<in> A} =
   4.130      (if i \<in> I then ((\<lambda>x. x i) \<circ> fst) -` A \<inter> space ?P else ((\<lambda>x. x i) \<circ> snd) -` A \<inter> space ?P)"
   4.131 -    using `I \<inter> J = {}` by auto
   4.132 +    by (auto simp: merge_def)
   4.133    also have "\<dots> \<in> sets ?P"
   4.134      using A
   4.135      by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
   4.136 -  finally show "{\<omega> \<in> space ?P. prod_case (\<lambda>x. merge I x J) \<omega> i \<in> A} \<in> sets ?P" .
   4.137 -qed (insert assms, auto simp: space_pair_measure space_PiM)
   4.138 +  finally show "{\<omega> \<in> space ?P. merge I J \<omega> i \<in> A} \<in> sets ?P" .
   4.139 +qed (auto simp: space_pair_measure space_PiM Pi_iff merge_def extensional_def)
   4.140  
   4.141  lemma measurable_restrict:
   4.142    assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> measurable N (M i)"
   4.143 @@ -587,44 +585,38 @@
   4.144    qed
   4.145  qed
   4.146  
   4.147 -lemma (in product_sigma_finite)
   4.148 -  assumes "finite I"
   4.149 -  shows sigma_finite: "sigma_finite_measure (PiM I M)"
   4.150 -  and emeasure_PiM:
   4.151 -    "\<And>A. (\<And>i. i\<in>I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> emeasure (PiM I M) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   4.152 -using `finite I` proof induct
   4.153 -  case empty
   4.154 +lemma
   4.155 +  shows space_PiM_empty: "space (Pi\<^isub>M {} M) = {\<lambda>k. undefined}"
   4.156 +    and sets_PiM_empty: "sets (Pi\<^isub>M {} M) = { {}, {\<lambda>k. undefined} }"
   4.157 +  by (simp_all add: space_PiM sets_PiM_single image_constant sigma_sets_empty_eq)
   4.158 +
   4.159 +lemma emeasure_PiM_empty[simp]: "emeasure (PiM {} M) {\<lambda>_. undefined} = 1"
   4.160 +proof -
   4.161    let ?\<mu> = "\<lambda>A. if A = {} then 0 else (1::ereal)"
   4.162 -  have "prod_algebra {} M = {{\<lambda>_. undefined}}"
   4.163 -    by (auto simp: prod_algebra_def prod_emb_def intro!: image_eqI)
   4.164 -  then have sets_empty: "sets (PiM {} M) = {{}, {\<lambda>_. undefined}}"
   4.165 -    by (simp add: sets_PiM)
   4.166    have "emeasure (Pi\<^isub>M {} M) (prod_emb {} M {} (\<Pi>\<^isub>E i\<in>{}. {})) = 1"
   4.167    proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   4.168 -    have "finite (space (PiM {} M))"
   4.169 -      by (simp add: space_PiM)
   4.170 -    moreover show "positive (PiM {} M) ?\<mu>"
   4.171 +    show "positive (PiM {} M) ?\<mu>"
   4.172        by (auto simp: positive_def)
   4.173 -    ultimately show "countably_additive (PiM {} M) ?\<mu>"
   4.174 -      by (rule countably_additiveI_finite) (auto simp: additive_def space_PiM sets_empty)
   4.175 +    show "countably_additive (PiM {} M) ?\<mu>"
   4.176 +      by (rule countably_additiveI_finite)
   4.177 +         (auto simp: additive_def positive_def sets_PiM_empty space_PiM_empty intro!: )
   4.178    qed (auto simp: prod_emb_def)
   4.179 -  also have *: "(prod_emb {} M {} (\<Pi>\<^isub>E i\<in>{}. {})) = {\<lambda>_. undefined}"
   4.180 +  also have "(prod_emb {} M {} (\<Pi>\<^isub>E i\<in>{}. {})) = {\<lambda>_. undefined}"
   4.181      by (auto simp: prod_emb_def)
   4.182 -  finally have emeasure_eq: "emeasure (Pi\<^isub>M {} M) {\<lambda>_. undefined} = 1" .
   4.183 +  finally show ?thesis
   4.184 +    by simp
   4.185 +qed
   4.186  
   4.187 -  interpret finite_measure "PiM {} M"
   4.188 -    by default (simp add: space_PiM emeasure_eq)
   4.189 -  case 1 show ?case ..
   4.190 +lemma PiM_empty: "PiM {} M = count_space {\<lambda>_. undefined}"
   4.191 +  by (rule measure_eqI) (auto simp add: sets_PiM_empty one_ereal_def)
   4.192  
   4.193 -  case 2 show ?case
   4.194 -    using emeasure_eq * by simp
   4.195 -next
   4.196 +lemma (in product_sigma_finite) emeasure_PiM:
   4.197 +  "finite I \<Longrightarrow> (\<And>i. i\<in>I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> emeasure (PiM I M) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   4.198 +proof (induct I arbitrary: A rule: finite_induct)
   4.199    case (insert i I)
   4.200    interpret finite_product_sigma_finite M I by default fact
   4.201    have "finite (insert i I)" using `finite I` by auto
   4.202    interpret I': finite_product_sigma_finite M "insert i I" by default fact
   4.203 -  interpret I: sigma_finite_measure "PiM I M" by fact
   4.204 -  interpret P: pair_sigma_finite "PiM I M" "M i" ..
   4.205    let ?h = "(\<lambda>(f, y). f(i := y))"
   4.206  
   4.207    let ?P = "distr (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) (Pi\<^isub>M (insert i I) M) ?h"
   4.208 @@ -632,67 +624,73 @@
   4.209    let ?I = "{j \<in> insert i I. emeasure (M j) (space (M j)) \<noteq> 1}"
   4.210    let ?f = "\<lambda>J E j. if j \<in> J then emeasure (M j) (E j) else emeasure (M j) (space (M j))"
   4.211  
   4.212 -  { case 2
   4.213 -    have "emeasure (Pi\<^isub>M (insert i I) M) (prod_emb (insert i I) M (insert i I) (Pi\<^isub>E (insert i I) A)) =
   4.214 -      (\<Prod>i\<in>insert i I. emeasure (M i) (A i))"
   4.215 -    proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   4.216 -      fix J E assume "(J \<noteq> {} \<or> insert i I = {}) \<and> finite J \<and> J \<subseteq> insert i I \<and> E \<in> (\<Pi> j\<in>J. sets (M j))"
   4.217 -      then have J: "J \<noteq> {}" "finite J" "J \<subseteq> insert i I" and E: "\<forall>j\<in>J. E j \<in> sets (M j)" by auto
   4.218 -      let ?p = "prod_emb (insert i I) M J (Pi\<^isub>E J E)"
   4.219 -      let ?p' = "prod_emb I M (J - {i}) (\<Pi>\<^isub>E j\<in>J-{i}. E j)"
   4.220 -      have "?\<mu> ?p =
   4.221 -        emeasure (Pi\<^isub>M I M \<Otimes>\<^isub>M (M i)) (?h -` ?p \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i))"
   4.222 -        by (intro emeasure_distr measurable_add_dim sets_PiM_I) fact+
   4.223 -      also have "?h -` ?p \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) = ?p' \<times> (if i \<in> J then E i else space (M i))"
   4.224 -        using J E[rule_format, THEN sets_into_space]
   4.225 -        by (force simp: space_pair_measure space_PiM Pi_iff prod_emb_iff split: split_if_asm)
   4.226 -      also have "emeasure (Pi\<^isub>M I M \<Otimes>\<^isub>M (M i)) (?p' \<times> (if i \<in> J then E i else space (M i))) =
   4.227 -        emeasure (Pi\<^isub>M I M) ?p' * emeasure (M i) (if i \<in> J then (E i) else space (M i))"
   4.228 -        using J E by (intro P.emeasure_pair_measure_Times sets_PiM_I) auto
   4.229 -      also have "?p' = (\<Pi>\<^isub>E j\<in>I. if j \<in> J-{i} then E j else space (M j))"
   4.230 -        using J E[rule_format, THEN sets_into_space]
   4.231 -        by (auto simp: prod_emb_iff Pi_iff split: split_if_asm) blast+
   4.232 -      also have "emeasure (Pi\<^isub>M I M) (\<Pi>\<^isub>E j\<in>I. if j \<in> J-{i} then E j else space (M j)) =
   4.233 -        (\<Prod> j\<in>I. if j \<in> J-{i} then emeasure (M j) (E j) else emeasure (M j) (space (M j)))"
   4.234 -        using E by (subst insert) (auto intro!: setprod_cong)
   4.235 -      also have "(\<Prod>j\<in>I. if j \<in> J - {i} then emeasure (M j) (E j) else emeasure (M j) (space (M j))) *
   4.236 -         emeasure (M i) (if i \<in> J then E i else space (M i)) = (\<Prod>j\<in>insert i I. ?f J E j)"
   4.237 -        using insert by (auto simp: mult_commute intro!: arg_cong2[where f="op *"] setprod_cong)
   4.238 -      also have "\<dots> = (\<Prod>j\<in>J \<union> ?I. ?f J E j)"
   4.239 -        using insert(1,2) J E by (intro setprod_mono_one_right) auto
   4.240 -      finally show "?\<mu> ?p = \<dots>" .
   4.241 +  have "emeasure (Pi\<^isub>M (insert i I) M) (prod_emb (insert i I) M (insert i I) (Pi\<^isub>E (insert i I) A)) =
   4.242 +    (\<Prod>i\<in>insert i I. emeasure (M i) (A i))"
   4.243 +  proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   4.244 +    fix J E assume "(J \<noteq> {} \<or> insert i I = {}) \<and> finite J \<and> J \<subseteq> insert i I \<and> E \<in> (\<Pi> j\<in>J. sets (M j))"
   4.245 +    then have J: "J \<noteq> {}" "finite J" "J \<subseteq> insert i I" and E: "\<forall>j\<in>J. E j \<in> sets (M j)" by auto
   4.246 +    let ?p = "prod_emb (insert i I) M J (Pi\<^isub>E J E)"
   4.247 +    let ?p' = "prod_emb I M (J - {i}) (\<Pi>\<^isub>E j\<in>J-{i}. E j)"
   4.248 +    have "?\<mu> ?p =
   4.249 +      emeasure (Pi\<^isub>M I M \<Otimes>\<^isub>M (M i)) (?h -` ?p \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i))"
   4.250 +      by (intro emeasure_distr measurable_add_dim sets_PiM_I) fact+
   4.251 +    also have "?h -` ?p \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) = ?p' \<times> (if i \<in> J then E i else space (M i))"
   4.252 +      using J E[rule_format, THEN sets_into_space]
   4.253 +      by (force simp: space_pair_measure space_PiM Pi_iff prod_emb_iff split: split_if_asm)
   4.254 +    also have "emeasure (Pi\<^isub>M I M \<Otimes>\<^isub>M (M i)) (?p' \<times> (if i \<in> J then E i else space (M i))) =
   4.255 +      emeasure (Pi\<^isub>M I M) ?p' * emeasure (M i) (if i \<in> J then (E i) else space (M i))"
   4.256 +      using J E by (intro M.emeasure_pair_measure_Times sets_PiM_I) auto
   4.257 +    also have "?p' = (\<Pi>\<^isub>E j\<in>I. if j \<in> J-{i} then E j else space (M j))"
   4.258 +      using J E[rule_format, THEN sets_into_space]
   4.259 +      by (auto simp: prod_emb_iff Pi_iff split: split_if_asm) blast+
   4.260 +    also have "emeasure (Pi\<^isub>M I M) (\<Pi>\<^isub>E j\<in>I. if j \<in> J-{i} then E j else space (M j)) =
   4.261 +      (\<Prod> j\<in>I. if j \<in> J-{i} then emeasure (M j) (E j) else emeasure (M j) (space (M j)))"
   4.262 +      using E by (subst insert) (auto intro!: setprod_cong)
   4.263 +    also have "(\<Prod>j\<in>I. if j \<in> J - {i} then emeasure (M j) (E j) else emeasure (M j) (space (M j))) *
   4.264 +       emeasure (M i) (if i \<in> J then E i else space (M i)) = (\<Prod>j\<in>insert i I. ?f J E j)"
   4.265 +      using insert by (auto simp: mult_commute intro!: arg_cong2[where f="op *"] setprod_cong)
   4.266 +    also have "\<dots> = (\<Prod>j\<in>J \<union> ?I. ?f J E j)"
   4.267 +      using insert(1,2) J E by (intro setprod_mono_one_right) auto
   4.268 +    finally show "?\<mu> ?p = \<dots>" .
   4.269  
   4.270 -      show "prod_emb (insert i I) M J (Pi\<^isub>E J E) \<in> Pow (\<Pi>\<^isub>E i\<in>insert i I. space (M i))"
   4.271 -        using J E[rule_format, THEN sets_into_space] by (auto simp: prod_emb_iff)
   4.272 -    next
   4.273 -      show "positive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>" "countably_additive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>"
   4.274 -        using emeasure_positive[of ?P] emeasure_countably_additive[of ?P] by simp_all
   4.275 -    next
   4.276 -      show "(insert i I \<noteq> {} \<or> insert i I = {}) \<and> finite (insert i I) \<and>
   4.277 -        insert i I \<subseteq> insert i I \<and> A \<in> (\<Pi> j\<in>insert i I. sets (M j))"
   4.278 -        using insert(1,2) 2 by auto
   4.279 -    qed (auto intro!: setprod_cong)
   4.280 -    with 2[THEN sets_into_space] show ?case by (subst (asm) prod_emb_PiE_same_index) auto }
   4.281 -  note product = this
   4.282 +    show "prod_emb (insert i I) M J (Pi\<^isub>E J E) \<in> Pow (\<Pi>\<^isub>E i\<in>insert i I. space (M i))"
   4.283 +      using J E[rule_format, THEN sets_into_space] by (auto simp: prod_emb_iff)
   4.284 +  next
   4.285 +    show "positive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>" "countably_additive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>"
   4.286 +      using emeasure_positive[of ?P] emeasure_countably_additive[of ?P] by simp_all
   4.287 +  next
   4.288 +    show "(insert i I \<noteq> {} \<or> insert i I = {}) \<and> finite (insert i I) \<and>
   4.289 +      insert i I \<subseteq> insert i I \<and> A \<in> (\<Pi> j\<in>insert i I. sets (M j))"
   4.290 +      using insert by auto
   4.291 +  qed (auto intro!: setprod_cong)
   4.292 +  with insert show ?case
   4.293 +    by (subst (asm) prod_emb_PiE_same_index) (auto intro!: sets_into_space)
   4.294 +qed (simp add: emeasure_PiM_empty)
   4.295  
   4.296 -  from I'.sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   4.297 -  then have F: "\<And>j. j \<in> insert i I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
   4.298 -    "incseq (\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k)"
   4.299 -    "(\<Union>k. \<Pi>\<^isub>E j \<in> insert i I. F j k) = space (Pi\<^isub>M (insert i I) M)"
   4.300 -    "\<And>k. \<And>j. j \<in> insert i I \<Longrightarrow> emeasure (M j) (F j k) \<noteq> \<infinity>"
   4.301 +lemma (in product_sigma_finite) sigma_finite: 
   4.302 +  assumes "finite I"
   4.303 +  shows "sigma_finite_measure (PiM I M)"
   4.304 +proof -
   4.305 +  interpret finite_product_sigma_finite M I by default fact
   4.306 +
   4.307 +  from sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   4.308 +  then have F: "\<And>j. j \<in> I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
   4.309 +    "incseq (\<lambda>k. \<Pi>\<^isub>E j \<in> I. F j k)"
   4.310 +    "(\<Union>k. \<Pi>\<^isub>E j \<in> I. F j k) = space (Pi\<^isub>M I M)"
   4.311 +    "\<And>k. \<And>j. j \<in> I \<Longrightarrow> emeasure (M j) (F j k) \<noteq> \<infinity>"
   4.312      by blast+
   4.313 -  let ?F = "\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k"
   4.314 +  let ?F = "\<lambda>k. \<Pi>\<^isub>E j \<in> I. F j k"
   4.315  
   4.316 -  case 1 show ?case
   4.317 +  show ?thesis
   4.318    proof (unfold_locales, intro exI[of _ ?F] conjI allI)
   4.319 -    show "range ?F \<subseteq> sets (Pi\<^isub>M (insert i I) M)" using F(1) insert(1,2) by auto
   4.320 +    show "range ?F \<subseteq> sets (Pi\<^isub>M I M)" using F(1) `finite I` by auto
   4.321    next
   4.322 -    from F(3) show "(\<Union>i. ?F i) = space (Pi\<^isub>M (insert i I) M)" by simp
   4.323 +    from F(3) show "(\<Union>i. ?F i) = space (Pi\<^isub>M I M)" by simp
   4.324    next
   4.325      fix j
   4.326 -    from F `finite I` setprod_PInf[of "insert i I", OF emeasure_nonneg, of M]
   4.327 -    show "emeasure (\<Pi>\<^isub>M i\<in>insert i I. M i) (?F j) \<noteq> \<infinity>"
   4.328 -      by (subst product) auto
   4.329 +    from F `finite I` setprod_PInf[of I, OF emeasure_nonneg, of M]
   4.330 +    show "emeasure (\<Pi>\<^isub>M i\<in>I. M i) (?F j) \<noteq> \<infinity>"
   4.331 +      by (subst emeasure_PiM) auto
   4.332    qed
   4.333  qed
   4.334  
   4.335 @@ -703,18 +701,6 @@
   4.336    "(\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> emeasure (Pi\<^isub>M I M) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   4.337    using emeasure_PiM[OF finite_index] by auto
   4.338  
   4.339 -lemma (in product_sigma_finite) product_measure_empty[simp]:
   4.340 -  "emeasure (Pi\<^isub>M {} M) {\<lambda>x. undefined} = 1"
   4.341 -proof -
   4.342 -  interpret finite_product_sigma_finite M "{}" by default auto
   4.343 -  from measure_times[of "\<lambda>x. {}"] show ?thesis by simp
   4.344 -qed
   4.345 -
   4.346 -lemma
   4.347 -  shows space_PiM_empty: "space (Pi\<^isub>M {} M) = {\<lambda>k. undefined}"
   4.348 -    and sets_PiM_empty: "sets (Pi\<^isub>M {} M) = { {}, {\<lambda>k. undefined} }"
   4.349 -  by (simp_all add: space_PiM sets_PiM_single image_constant sigma_sets_empty_eq)
   4.350 -
   4.351  lemma (in product_sigma_finite) positive_integral_empty:
   4.352    assumes pos: "0 \<le> f (\<lambda>k. undefined)"
   4.353    shows "integral\<^isup>P (Pi\<^isub>M {} M) f = f (\<lambda>k. undefined)"
   4.354 @@ -734,7 +720,7 @@
   4.355  
   4.356  lemma (in product_sigma_finite) distr_merge:
   4.357    assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   4.358 -  shows "distr (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) (Pi\<^isub>M (I \<union> J) M) (\<lambda>(x,y). merge I x J y) = Pi\<^isub>M (I \<union> J) M"
   4.359 +  shows "distr (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) (Pi\<^isub>M (I \<union> J) M) (merge I J) = Pi\<^isub>M (I \<union> J) M"
   4.360     (is "?D = ?P")
   4.361  proof -
   4.362    interpret I: finite_product_sigma_finite M I by default fact
   4.363 @@ -742,7 +728,7 @@
   4.364    have "finite (I \<union> J)" using fin by auto
   4.365    interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
   4.366    interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   4.367 -  let ?g = "\<lambda>(x,y). merge I x J y"
   4.368 +  let ?g = "merge I J"
   4.369  
   4.370    from IJ.sigma_finite_pairs obtain F where
   4.371      F: "\<And>i. i\<in> I \<union> J \<Longrightarrow> range (F i) \<subseteq> sets (M i)"
   4.372 @@ -765,7 +751,6 @@
   4.373  
   4.374      show "range ?F \<subseteq> prod_algebra (I \<union> J) M" using F
   4.375        using fin by (auto simp: prod_algebra_eq_finite)
   4.376 -    show "incseq ?F" by fact
   4.377      show "(\<Union>i. \<Pi>\<^isub>E ia\<in>I \<union> J. F ia i) = (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i))"
   4.378        using F(3) by (simp add: space_PiM)
   4.379    next
   4.380 @@ -786,7 +771,7 @@
   4.381        using A by (intro emeasure_distr measurable_merge) (auto simp: sets_PiM)
   4.382      also have "emeasure ?B ?X = (\<Prod>i\<in>I. emeasure (M i) (F i)) * (\<Prod>i\<in>J. emeasure (M i) (F i))"
   4.383        using `finite J` `finite I` F X
   4.384 -      by (simp add: P.emeasure_pair_measure_Times I.measure_times J.measure_times Pi_iff)
   4.385 +      by (simp add: J.emeasure_pair_measure_Times I.measure_times J.measure_times Pi_iff)
   4.386      also have "\<dots> = (\<Prod>i\<in>I \<union> J. emeasure (M i) (F i))"
   4.387        using `finite J` `finite I` `I \<inter> J = {}`  by (simp add: setprod_Un_one)
   4.388      also have "\<dots> = emeasure ?P (Pi\<^isub>E (I \<union> J) F)"
   4.389 @@ -800,16 +785,16 @@
   4.390    assumes IJ: "I \<inter> J = {}" "finite I" "finite J"
   4.391    and f: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
   4.392    shows "integral\<^isup>P (Pi\<^isub>M (I \<union> J) M) f =
   4.393 -    (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (merge I x J y) \<partial>(Pi\<^isub>M J M)) \<partial>(Pi\<^isub>M I M))"
   4.394 +    (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (merge I J (x, y)) \<partial>(Pi\<^isub>M J M)) \<partial>(Pi\<^isub>M I M))"
   4.395  proof -
   4.396    interpret I: finite_product_sigma_finite M I by default fact
   4.397    interpret J: finite_product_sigma_finite M J by default fact
   4.398    interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   4.399 -  have P_borel: "(\<lambda>x. f (case x of (x, y) \<Rightarrow> merge I x J y)) \<in> borel_measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
   4.400 -    using measurable_comp[OF measurable_merge[OF IJ(1)] f] by (simp add: comp_def)
   4.401 +  have P_borel: "(\<lambda>x. f (merge I J x)) \<in> borel_measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
   4.402 +    using measurable_comp[OF measurable_merge f] by (simp add: comp_def)
   4.403    show ?thesis
   4.404      apply (subst distr_merge[OF IJ, symmetric])
   4.405 -    apply (subst positive_integral_distr[OF measurable_merge f, OF IJ(1)])
   4.406 +    apply (subst positive_integral_distr[OF measurable_merge f])
   4.407      apply (subst P.positive_integral_fst_measurable(2)[symmetric, OF P_borel])
   4.408      apply simp
   4.409      done
   4.410 @@ -840,7 +825,7 @@
   4.411  qed
   4.412  
   4.413  lemma (in product_sigma_finite) product_positive_integral_insert:
   4.414 -  assumes [simp]: "finite I" "i \<notin> I"
   4.415 +  assumes I[simp]: "finite I" "i \<notin> I"
   4.416      and f: "f \<in> borel_measurable (Pi\<^isub>M (insert i I) M)"
   4.417    shows "integral\<^isup>P (Pi\<^isub>M (insert i I) M) f = (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x(i := y)) \<partial>(M i)) \<partial>(Pi\<^isub>M I M))"
   4.418  proof -
   4.419 @@ -849,17 +834,17 @@
   4.420    have IJ: "I \<inter> {i} = {}" and insert: "I \<union> {i} = insert i I"
   4.421      using f by auto
   4.422    show ?thesis
   4.423 -    unfolding product_positive_integral_fold[OF IJ, unfolded insert, simplified, OF f]
   4.424 -  proof (rule positive_integral_cong, subst product_positive_integral_singleton)
   4.425 +    unfolding product_positive_integral_fold[OF IJ, unfolded insert, OF I(1) i.finite_index f]
   4.426 +  proof (rule positive_integral_cong, subst product_positive_integral_singleton[symmetric])
   4.427      fix x assume x: "x \<in> space (Pi\<^isub>M I M)"
   4.428 -    let ?f = "\<lambda>y. f (restrict (x(i := y)) (insert i I))"
   4.429 -    have f'_eq: "\<And>y. ?f y = f (x(i := y))"
   4.430 -      using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def space_PiM)
   4.431 -    show "?f \<in> borel_measurable (M i)" unfolding f'_eq
   4.432 +    let ?f = "\<lambda>y. f (x(i := y))"
   4.433 +    show "?f \<in> borel_measurable (M i)"
   4.434        using measurable_comp[OF measurable_component_update f, OF x `i \<notin> I`]
   4.435        unfolding comp_def .
   4.436 -    show "integral\<^isup>P (M i) ?f = \<integral>\<^isup>+ y. f (x(i:=y)) \<partial>M i"
   4.437 -      unfolding f'_eq by simp
   4.438 +    show "(\<integral>\<^isup>+ y. f (merge I {i} (x, y)) \<partial>Pi\<^isub>M {i} M) = (\<integral>\<^isup>+ y. f (x(i := y i)) \<partial>Pi\<^isub>M {i} M)"
   4.439 +      using x
   4.440 +      by (auto intro!: positive_integral_cong arg_cong[where f=f]
   4.441 +               simp add: space_PiM extensional_def)
   4.442    qed
   4.443  qed
   4.444  
   4.445 @@ -902,29 +887,54 @@
   4.446  lemma (in product_sigma_finite) product_integral_fold:
   4.447    assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   4.448    and f: "integrable (Pi\<^isub>M (I \<union> J) M) f"
   4.449 -  shows "integral\<^isup>L (Pi\<^isub>M (I \<union> J) M) f = (\<integral>x. (\<integral>y. f (merge I x J y) \<partial>Pi\<^isub>M J M) \<partial>Pi\<^isub>M I M)"
   4.450 +  shows "integral\<^isup>L (Pi\<^isub>M (I \<union> J) M) f = (\<integral>x. (\<integral>y. f (merge I J (x, y)) \<partial>Pi\<^isub>M J M) \<partial>Pi\<^isub>M I M)"
   4.451  proof -
   4.452    interpret I: finite_product_sigma_finite M I by default fact
   4.453    interpret J: finite_product_sigma_finite M J by default fact
   4.454    have "finite (I \<union> J)" using fin by auto
   4.455    interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
   4.456    interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   4.457 -  let ?M = "\<lambda>(x, y). merge I x J y"
   4.458 +  let ?M = "merge I J"
   4.459    let ?f = "\<lambda>x. f (?M x)"
   4.460    from f have f_borel: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
   4.461      by auto
   4.462 -  have P_borel: "(\<lambda>x. f (case x of (x, y) \<Rightarrow> merge I x J y)) \<in> borel_measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
   4.463 -    using measurable_comp[OF measurable_merge[OF IJ(1)] f_borel] by (simp add: comp_def)
   4.464 +  have P_borel: "(\<lambda>x. f (merge I J x)) \<in> borel_measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
   4.465 +    using measurable_comp[OF measurable_merge f_borel] by (simp add: comp_def)
   4.466    have f_int: "integrable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) ?f"
   4.467 -    by (rule integrable_distr[OF measurable_merge[OF IJ]]) (simp add: distr_merge[OF IJ fin] f)
   4.468 +    by (rule integrable_distr[OF measurable_merge]) (simp add: distr_merge[OF IJ fin] f)
   4.469    show ?thesis
   4.470      apply (subst distr_merge[symmetric, OF IJ fin])
   4.471 -    apply (subst integral_distr[OF measurable_merge[OF IJ] f_borel])
   4.472 +    apply (subst integral_distr[OF measurable_merge f_borel])
   4.473      apply (subst P.integrable_fst_measurable(2)[symmetric, OF f_int])
   4.474      apply simp
   4.475      done
   4.476  qed
   4.477  
   4.478 +lemma (in product_sigma_finite)
   4.479 +  assumes IJ: "I \<inter> J = {}" "finite I" "finite J" and A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
   4.480 +  shows emeasure_fold_integral:
   4.481 +    "emeasure (Pi\<^isub>M (I \<union> J) M) A = (\<integral>\<^isup>+x. emeasure (Pi\<^isub>M J M) ((\<lambda>y. merge I J (x, y)) -` A \<inter> space (Pi\<^isub>M J M)) \<partial>Pi\<^isub>M I M)" (is ?I)
   4.482 +    and emeasure_fold_measurable:
   4.483 +    "(\<lambda>x. emeasure (Pi\<^isub>M J M) ((\<lambda>y. merge I J (x, y)) -` A \<inter> space (Pi\<^isub>M J M))) \<in> borel_measurable (Pi\<^isub>M I M)" (is ?B)
   4.484 +proof -
   4.485 +  interpret I: finite_product_sigma_finite M I by default fact
   4.486 +  interpret J: finite_product_sigma_finite M J by default fact
   4.487 +  interpret IJ: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" ..
   4.488 +  have merge: "merge I J -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) \<in> sets (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
   4.489 +    by (intro measurable_sets[OF _ A] measurable_merge assms)
   4.490 +
   4.491 +  show ?I
   4.492 +    apply (subst distr_merge[symmetric, OF IJ])
   4.493 +    apply (subst emeasure_distr[OF measurable_merge A])
   4.494 +    apply (subst J.emeasure_pair_measure_alt[OF merge])
   4.495 +    apply (auto intro!: positive_integral_cong arg_cong2[where f=emeasure] simp: space_pair_measure)
   4.496 +    done
   4.497 +
   4.498 +  show ?B
   4.499 +    using IJ.measurable_emeasure_Pair1[OF merge]
   4.500 +    by (simp add: vimage_compose[symmetric] comp_def space_pair_measure cong: measurable_cong)
   4.501 +qed
   4.502 +
   4.503  lemma (in product_sigma_finite) product_integral_insert:
   4.504    assumes I: "finite I" "i \<notin> I"
   4.505      and f: "integrable (Pi\<^isub>M (insert i I) M) f"
   4.506 @@ -932,7 +942,7 @@
   4.507  proof -
   4.508    have "integral\<^isup>L (Pi\<^isub>M (insert i I) M) f = integral\<^isup>L (Pi\<^isub>M (I \<union> {i}) M) f"
   4.509      by simp
   4.510 -  also have "\<dots> = (\<integral>x. (\<integral>y. f (merge I x {i} y) \<partial>Pi\<^isub>M {i} M) \<partial>Pi\<^isub>M I M)"
   4.511 +  also have "\<dots> = (\<integral>x. (\<integral>y. f (merge I {i} (x,y)) \<partial>Pi\<^isub>M {i} M) \<partial>Pi\<^isub>M I M)"
   4.512      using f I by (intro product_integral_fold) auto
   4.513    also have "\<dots> = (\<integral>x. (\<integral>y. f (x(i := y)) \<partial>M i) \<partial>Pi\<^isub>M I M)"
   4.514    proof (rule integral_cong, subst product_integral_singleton[symmetric])
   4.515 @@ -942,7 +952,7 @@
   4.516      show "(\<lambda>y. f (x(i := y))) \<in> borel_measurable (M i)"
   4.517        using measurable_comp[OF measurable_component_update f_borel, OF x `i \<notin> I`]
   4.518        unfolding comp_def .
   4.519 -    from x I show "(\<integral> y. f (merge I x {i} y) \<partial>Pi\<^isub>M {i} M) = (\<integral> xa. f (x(i := xa i)) \<partial>Pi\<^isub>M {i} M)"
   4.520 +    from x I show "(\<integral> y. f (merge I {i} (x,y)) \<partial>Pi\<^isub>M {i} M) = (\<integral> xa. f (x(i := xa i)) \<partial>Pi\<^isub>M {i} M)"
   4.521        by (auto intro!: integral_cong arg_cong[where f=f] simp: merge_def space_PiM extensional_def)
   4.522    qed
   4.523    finally show ?thesis .
   4.524 @@ -980,30 +990,83 @@
   4.525  
   4.526  lemma (in product_sigma_finite) product_integral_setprod:
   4.527    fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
   4.528 -  assumes "finite I" "I \<noteq> {}" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
   4.529 +  assumes "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
   4.530    shows "(\<integral>x. (\<Prod>i\<in>I. f i (x i)) \<partial>Pi\<^isub>M I M) = (\<Prod>i\<in>I. integral\<^isup>L (M i) (f i))"
   4.531 -using assms proof (induct rule: finite_ne_induct)
   4.532 -  case (singleton i)
   4.533 -  then show ?case by (simp add: product_integral_singleton integrable_def)
   4.534 +using assms proof induct
   4.535 +  case empty
   4.536 +  interpret finite_measure "Pi\<^isub>M {} M"
   4.537 +    by rule (simp add: space_PiM)
   4.538 +  show ?case by (simp add: space_PiM measure_def)
   4.539  next
   4.540    case (insert i I)
   4.541    then have iI: "finite (insert i I)" by auto
   4.542    then have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
   4.543      integrable (Pi\<^isub>M J M) (\<lambda>x. (\<Prod>i\<in>J. f i (x i)))"
   4.544 -    by (intro product_integrable_setprod insert(5)) (auto intro: finite_subset)
   4.545 +    by (intro product_integrable_setprod insert(4)) (auto intro: finite_subset)
   4.546    interpret I: finite_product_sigma_finite M I by default fact
   4.547    have *: "\<And>x y. (\<Prod>j\<in>I. f j (if j = i then y else x j)) = (\<Prod>j\<in>I. f j (x j))"
   4.548      using `i \<notin> I` by (auto intro!: setprod_cong)
   4.549    show ?case
   4.550 -    unfolding product_integral_insert[OF insert(1,3) prod[OF subset_refl]]
   4.551 +    unfolding product_integral_insert[OF insert(1,2) prod[OF subset_refl]]
   4.552      by (simp add: * insert integral_multc integral_cmult[OF prod] subset_insertI)
   4.553  qed
   4.554  
   4.555 +lemma sets_Collect_single:
   4.556 +  "i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> { x \<in> space (Pi\<^isub>M I M). x i \<in> A } \<in> sets (Pi\<^isub>M I M)"
   4.557 +  unfolding sets_PiM_single
   4.558 +  by (auto intro!: sigma_sets.Basic exI[of _ i] exI[of _ A]) (auto simp: space_PiM)
   4.559 +
   4.560 +lemma sigma_prod_algebra_sigma_eq_infinite:
   4.561 +  fixes E :: "'i \<Rightarrow> 'a set set"
   4.562 +  assumes S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
   4.563 +    and S_in_E: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> E i"
   4.564 +  assumes E_closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M i))"
   4.565 +    and E_generates: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sigma_sets (space (M i)) (E i)"
   4.566 +  defines "P == {{f\<in>\<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A} | i A. i \<in> I \<and> A \<in> E i}"
   4.567 +  shows "sets (PiM I M) = sigma_sets (space (PiM I M)) P"
   4.568 +proof
   4.569 +  let ?P = "sigma (space (Pi\<^isub>M I M)) P"
   4.570 +  have P_closed: "P \<subseteq> Pow (space (Pi\<^isub>M I M))"
   4.571 +    using E_closed by (auto simp: space_PiM P_def Pi_iff subset_eq)
   4.572 +  then have space_P: "space ?P = (\<Pi>\<^isub>E i\<in>I. space (M i))"
   4.573 +    by (simp add: space_PiM)
   4.574 +  have "sets (PiM I M) =
   4.575 +      sigma_sets (space ?P) {{f \<in> \<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A} |i A. i \<in> I \<and> A \<in> sets (M i)}"
   4.576 +    using sets_PiM_single[of I M] by (simp add: space_P)
   4.577 +  also have "\<dots> \<subseteq> sets (sigma (space (PiM I M)) P)"
   4.578 +  proof (safe intro!: sigma_sets_subset)
   4.579 +    fix i A assume "i \<in> I" and A: "A \<in> sets (M i)"
   4.580 +    then have "(\<lambda>x. x i) \<in> measurable ?P (sigma (space (M i)) (E i))"
   4.581 +      apply (subst measurable_iff_measure_of)
   4.582 +      apply (simp_all add: P_closed)
   4.583 +      using E_closed
   4.584 +      apply (force simp: subset_eq space_PiM)
   4.585 +      apply (force simp: subset_eq space_PiM)
   4.586 +      apply (auto simp: P_def intro!: sigma_sets.Basic exI[of _ i])
   4.587 +      apply (rule_tac x=Aa in exI)
   4.588 +      apply (auto simp: space_PiM)
   4.589 +      done
   4.590 +    from measurable_sets[OF this, of A] A `i \<in> I` E_closed
   4.591 +    have "(\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
   4.592 +      by (simp add: E_generates)
   4.593 +    also have "(\<lambda>x. x i) -` A \<inter> space ?P = {f \<in> \<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A}"
   4.594 +      using P_closed by (auto simp: space_PiM)
   4.595 +    finally show "\<dots> \<in> sets ?P" .
   4.596 +  qed
   4.597 +  finally show "sets (PiM I M) \<subseteq> sigma_sets (space (PiM I M)) P"
   4.598 +    by (simp add: P_closed)
   4.599 +  show "sigma_sets (space (PiM I M)) P \<subseteq> sets (PiM I M)"
   4.600 +    unfolding P_def space_PiM[symmetric]
   4.601 +    by (intro sigma_sets_subset) (auto simp: E_generates sets_Collect_single)
   4.602 +qed
   4.603 +
   4.604 +lemma bchoice_iff: "(\<forall>a\<in>A. \<exists>b. P a b) \<longleftrightarrow> (\<exists>f. \<forall>a\<in>A. P a (f a))"
   4.605 +  by metis
   4.606 +
   4.607  lemma sigma_prod_algebra_sigma_eq:
   4.608 -  fixes E :: "'i \<Rightarrow> 'a set set"
   4.609 +  fixes E :: "'i \<Rightarrow> 'a set set" and S :: "'i \<Rightarrow> nat \<Rightarrow> 'a set"
   4.610    assumes "finite I"
   4.611 -  assumes S_mono: "\<And>i. i \<in> I \<Longrightarrow> incseq (S i)"
   4.612 -    and S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
   4.613 +  assumes S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
   4.614      and S_in_E: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> E i"
   4.615    assumes E_closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M i))"
   4.616      and E_generates: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sigma_sets (space (M i)) (E i)"
   4.617 @@ -1011,6 +1074,9 @@
   4.618    shows "sets (PiM I M) = sigma_sets (space (PiM I M)) P"
   4.619  proof
   4.620    let ?P = "sigma (space (Pi\<^isub>M I M)) P"
   4.621 +  from `finite I`[THEN ex_bij_betw_finite_nat] guess T ..
   4.622 +  then have T: "\<And>i. i \<in> I \<Longrightarrow> T i < card I" "\<And>i. i\<in>I \<Longrightarrow> the_inv_into I T (T i) = i"
   4.623 +    by (auto simp add: bij_betw_def set_eq_iff image_iff the_inv_into_f_f)
   4.624    have P_closed: "P \<subseteq> Pow (space (Pi\<^isub>M I M))"
   4.625      using E_closed by (auto simp: space_PiM P_def Pi_iff subset_eq)
   4.626    then have space_P: "space ?P = (\<Pi>\<^isub>E i\<in>I. space (M i))"
   4.627 @@ -1033,15 +1099,18 @@
   4.628            using E_closed `i \<in> I` by (auto simp: space_P Pi_iff subset_eq split: split_if_asm)
   4.629          also have "\<dots> = (\<Pi>\<^isub>E j\<in>I. \<Union>n. if i = j then A else S j n)"
   4.630            by (intro PiE_cong) (simp add: S_union)
   4.631 -        also have "\<dots> = (\<Union>n. \<Pi>\<^isub>E j\<in>I. if i = j then A else S j n)"
   4.632 -          using S_mono
   4.633 -          by (subst Pi_UN[symmetric, OF `finite I`]) (auto simp: incseq_def)
   4.634 +        also have "\<dots> = (\<Union>xs\<in>{xs. length xs = card I}. \<Pi>\<^isub>E j\<in>I. if i = j then A else S j (xs ! T j))"
   4.635 +          using T
   4.636 +          apply (auto simp: Pi_iff bchoice_iff)
   4.637 +          apply (rule_tac x="map (\<lambda>n. f (the_inv_into I T n)) [0..<card I]" in exI)
   4.638 +          apply (auto simp: bij_betw_def)
   4.639 +          done
   4.640          also have "\<dots> \<in> sets ?P"
   4.641          proof (safe intro!: countable_UN)
   4.642 -          fix n show "(\<Pi>\<^isub>E j\<in>I. if i = j then A else S j n) \<in> sets ?P"
   4.643 +          fix xs show "(\<Pi>\<^isub>E j\<in>I. if i = j then A else S j (xs ! T j)) \<in> sets ?P"
   4.644              using A S_in_E
   4.645              by (simp add: P_closed)
   4.646 -               (auto simp: P_def subset_eq intro!: exI[of _ "\<lambda>j. if i = j then A else S j n"])
   4.647 +               (auto simp: P_def subset_eq intro!: exI[of _ "\<lambda>j. if i = j then A else S j (xs ! T j)"])
   4.648          qed
   4.649          finally show "(\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
   4.650            using P_closed by simp
     5.1 --- a/src/HOL/Probability/Independent_Family.thy	Wed Oct 10 15:16:44 2012 +0200
     5.2 +++ b/src/HOL/Probability/Independent_Family.thy	Wed Oct 10 15:17:18 2012 +0200
     5.3 @@ -23,13 +23,6 @@
     5.4  qed auto
     5.5  
     5.6  definition (in prob_space)
     5.7 -  "indep_events A I \<longleftrightarrow> (A`I \<subseteq> events) \<and>
     5.8 -    (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j)))"
     5.9 -
    5.10 -definition (in prob_space)
    5.11 -  "indep_event A B \<longleftrightarrow> indep_events (bool_case A B) UNIV"
    5.12 -
    5.13 -definition (in prob_space)
    5.14    "indep_sets F I \<longleftrightarrow> (\<forall>i\<in>I. F i \<subseteq> events) \<and>
    5.15      (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> (\<forall>A\<in>Pi J F. prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))))"
    5.16  
    5.17 @@ -37,22 +30,27 @@
    5.18    "indep_set A B \<longleftrightarrow> indep_sets (bool_case A B) UNIV"
    5.19  
    5.20  definition (in prob_space)
    5.21 -  "indep_vars M' X I \<longleftrightarrow>
    5.22 -    (\<forall>i\<in>I. random_variable (M' i) (X i)) \<and>
    5.23 -    indep_sets (\<lambda>i. sigma_sets (space M) { X i -` A \<inter> space M | A. A \<in> sets (M' i)}) I"
    5.24 +  indep_events_def_alt: "indep_events A I \<longleftrightarrow> indep_sets (\<lambda>i. {A i}) I"
    5.25 +
    5.26 +lemma image_subset_iff_funcset: "F ` A \<subseteq> B \<longleftrightarrow> F \<in> A \<rightarrow> B"
    5.27 +  by auto
    5.28 +
    5.29 +lemma (in prob_space) indep_events_def:
    5.30 +  "indep_events A I \<longleftrightarrow> (A`I \<subseteq> events) \<and>
    5.31 +    (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j)))"
    5.32 +  unfolding indep_events_def_alt indep_sets_def
    5.33 +  apply (simp add: Ball_def Pi_iff image_subset_iff_funcset)
    5.34 +  apply (intro conj_cong refl arg_cong[where f=All] ext imp_cong)
    5.35 +  apply auto
    5.36 +  done
    5.37  
    5.38  definition (in prob_space)
    5.39 -  "indep_var Ma A Mb B \<longleftrightarrow> indep_vars (bool_case Ma Mb) (bool_case A B) UNIV"
    5.40 +  "indep_event A B \<longleftrightarrow> indep_events (bool_case A B) UNIV"
    5.41  
    5.42  lemma (in prob_space) indep_sets_cong:
    5.43    "I = J \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> F i = G i) \<Longrightarrow> indep_sets F I \<longleftrightarrow> indep_sets G J"
    5.44    by (simp add: indep_sets_def, intro conj_cong all_cong imp_cong ball_cong) blast+
    5.45  
    5.46 -lemma (in prob_space) indep_sets_singleton_iff_indep_events:
    5.47 -  "indep_sets (\<lambda>i. {F i}) I \<longleftrightarrow> indep_events F I"
    5.48 -  unfolding indep_sets_def indep_events_def
    5.49 -  by (simp, intro conj_cong ball_cong all_cong imp_cong) (auto simp: Pi_iff)
    5.50 -
    5.51  lemma (in prob_space) indep_events_finite_index_events:
    5.52    "indep_events F I \<longleftrightarrow> (\<forall>J\<subseteq>I. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> indep_events F J)"
    5.53    by (auto simp: indep_events_def)
    5.54 @@ -86,6 +84,15 @@
    5.55      using indep by (auto simp: indep_sets_def)
    5.56  qed
    5.57  
    5.58 +lemma (in prob_space) indep_sets_mono:
    5.59 +  assumes indep: "indep_sets F I"
    5.60 +  assumes mono: "J \<subseteq> I" "\<And>i. i\<in>J \<Longrightarrow> G i \<subseteq> F i"
    5.61 +  shows "indep_sets G J"
    5.62 +  apply (rule indep_sets_mono_sets)
    5.63 +  apply (rule indep_sets_mono_index)
    5.64 +  apply (fact +)
    5.65 +  done
    5.66 +
    5.67  lemma (in prob_space) indep_setsI:
    5.68    assumes "\<And>i. i \<in> I \<Longrightarrow> F i \<subseteq> events"
    5.69      and "\<And>A J. J \<noteq> {} \<Longrightarrow> J \<subseteq> I \<Longrightarrow> finite J \<Longrightarrow> (\<forall>j\<in>J. A j \<in> F j) \<Longrightarrow> prob (\<Inter>j\<in>J. A j) = (\<Prod>j\<in>J. prob (A j))"
    5.70 @@ -117,16 +124,6 @@
    5.71    using indep[unfolded indep_set_def, THEN indep_setsD, of UNIV "bool_case a b"] ev
    5.72    by (simp add: ac_simps UNIV_bool)
    5.73  
    5.74 -lemma (in prob_space) indep_var_eq:
    5.75 -  "indep_var S X T Y \<longleftrightarrow>
    5.76 -    (random_variable S X \<and> random_variable T Y) \<and>
    5.77 -    indep_set
    5.78 -      (sigma_sets (space M) { X -` A \<inter> space M | A. A \<in> sets S})
    5.79 -      (sigma_sets (space M) { Y -` A \<inter> space M | A. A \<in> sets T})"
    5.80 -  unfolding indep_var_def indep_vars_def indep_set_def UNIV_bool
    5.81 -  by (intro arg_cong2[where f="op \<and>"] arg_cong2[where f=indep_sets] ext)
    5.82 -     (auto split: bool.split)
    5.83 -
    5.84  lemma (in prob_space)
    5.85    assumes indep: "indep_set A B"
    5.86    shows indep_setD_ev1: "A \<subseteq> events"
    5.87 @@ -326,6 +323,36 @@
    5.88      by (rule indep_sets_mono_sets) (intro subsetI sigma_sets.Basic)
    5.89  qed
    5.90  
    5.91 +definition (in prob_space)
    5.92 +  indep_vars_def2: "indep_vars M' X I \<longleftrightarrow>
    5.93 +    (\<forall>i\<in>I. random_variable (M' i) (X i)) \<and>
    5.94 +    indep_sets (\<lambda>i. { X i -` A \<inter> space M | A. A \<in> sets (M' i)}) I"
    5.95 +
    5.96 +definition (in prob_space)
    5.97 +  "indep_var Ma A Mb B \<longleftrightarrow> indep_vars (bool_case Ma Mb) (bool_case A B) UNIV"
    5.98 +
    5.99 +lemma (in prob_space) indep_vars_def:
   5.100 +  "indep_vars M' X I \<longleftrightarrow>
   5.101 +    (\<forall>i\<in>I. random_variable (M' i) (X i)) \<and>
   5.102 +    indep_sets (\<lambda>i. sigma_sets (space M) { X i -` A \<inter> space M | A. A \<in> sets (M' i)}) I"
   5.103 +  unfolding indep_vars_def2
   5.104 +  apply (rule conj_cong[OF refl])
   5.105 +  apply (rule indep_sets_sigma_sets_iff[symmetric])
   5.106 +  apply (auto simp: Int_stable_def)
   5.107 +  apply (rule_tac x="A \<inter> Aa" in exI)
   5.108 +  apply auto
   5.109 +  done
   5.110 +
   5.111 +lemma (in prob_space) indep_var_eq:
   5.112 +  "indep_var S X T Y \<longleftrightarrow>
   5.113 +    (random_variable S X \<and> random_variable T Y) \<and>
   5.114 +    indep_set
   5.115 +      (sigma_sets (space M) { X -` A \<inter> space M | A. A \<in> sets S})
   5.116 +      (sigma_sets (space M) { Y -` A \<inter> space M | A. A \<in> sets T})"
   5.117 +  unfolding indep_var_def indep_vars_def indep_set_def UNIV_bool
   5.118 +  by (intro arg_cong2[where f="op \<and>"] arg_cong2[where f=indep_sets] ext)
   5.119 +     (auto split: bool.split)
   5.120 +
   5.121  lemma (in prob_space) indep_sets2_eq:
   5.122    "indep_set A B \<longleftrightarrow> A \<subseteq> events \<and> B \<subseteq> events \<and> (\<forall>a\<in>A. \<forall>b\<in>B. prob (a \<inter> b) = prob a * prob b)"
   5.123    unfolding indep_set_def
   5.124 @@ -468,27 +495,25 @@
   5.125      by (simp cong: indep_sets_cong)
   5.126  qed
   5.127  
   5.128 -definition (in prob_space) terminal_events where
   5.129 -  "terminal_events A = (\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   5.130 +definition (in prob_space) tail_events where
   5.131 +  "tail_events A = (\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   5.132  
   5.133 -lemma (in prob_space) terminal_events_sets:
   5.134 -  assumes A: "\<And>i. A i \<subseteq> events"
   5.135 -  assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   5.136 -  assumes X: "X \<in> terminal_events A"
   5.137 -  shows "X \<in> events"
   5.138 -proof -
   5.139 +lemma (in prob_space) tail_events_sets:
   5.140 +  assumes A: "\<And>i::nat. A i \<subseteq> events"
   5.141 +  shows "tail_events A \<subseteq> events"
   5.142 +proof
   5.143 +  fix X assume X: "X \<in> tail_events A"
   5.144    let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   5.145 -  interpret A: sigma_algebra "space M" "A i" for i by fact
   5.146 -  from X have "\<And>n. X \<in> sigma_sets (space M) (UNION {n..} A)" by (auto simp: terminal_events_def)
   5.147 +  from X have "\<And>n::nat. X \<in> sigma_sets (space M) (UNION {n..} A)" by (auto simp: tail_events_def)
   5.148    from this[of 0] have "X \<in> sigma_sets (space M) (UNION UNIV A)" by simp
   5.149    then show "X \<in> events"
   5.150      by induct (insert A, auto)
   5.151  qed
   5.152  
   5.153 -lemma (in prob_space) sigma_algebra_terminal_events:
   5.154 +lemma (in prob_space) sigma_algebra_tail_events:
   5.155    assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   5.156 -  shows "sigma_algebra (space M) (terminal_events A)"
   5.157 -  unfolding terminal_events_def
   5.158 +  shows "sigma_algebra (space M) (tail_events A)"
   5.159 +  unfolding tail_events_def
   5.160  proof (simp add: sigma_algebra_iff2, safe)
   5.161    let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   5.162    interpret A: sigma_algebra "space M" "A i" for i by fact
   5.163 @@ -505,20 +530,22 @@
   5.164  
   5.165  lemma (in prob_space) kolmogorov_0_1_law:
   5.166    fixes A :: "nat \<Rightarrow> 'a set set"
   5.167 -  assumes A: "\<And>i. A i \<subseteq> events"
   5.168    assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   5.169    assumes indep: "indep_sets A UNIV"
   5.170 -  and X: "X \<in> terminal_events A"
   5.171 +  and X: "X \<in> tail_events A"
   5.172    shows "prob X = 0 \<or> prob X = 1"
   5.173  proof -
   5.174 +  have A: "\<And>i. A i \<subseteq> events"
   5.175 +    using indep unfolding indep_sets_def by simp
   5.176 +
   5.177    let ?D = "{D \<in> events. prob (X \<inter> D) = prob X * prob D}"
   5.178    interpret A: sigma_algebra "space M" "A i" for i by fact
   5.179 -  interpret T: sigma_algebra "space M" "terminal_events A"
   5.180 -    by (rule sigma_algebra_terminal_events) fact
   5.181 +  interpret T: sigma_algebra "space M" "tail_events A"
   5.182 +    by (rule sigma_algebra_tail_events) fact
   5.183    have "X \<subseteq> space M" using T.space_closed X by auto
   5.184  
   5.185    have X_in: "X \<in> events"
   5.186 -    by (rule terminal_events_sets) fact+
   5.187 +    using tail_events_sets A X by auto
   5.188  
   5.189    interpret D: dynkin_system "space M" ?D
   5.190    proof (rule dynkin_systemI)
   5.191 @@ -583,7 +610,7 @@
   5.192      proof (simp add: subset_eq, rule)
   5.193        fix D assume D: "D \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
   5.194        have "X \<in> sigma_sets (space M) (\<Union>m\<in>{Suc n..}. A m)"
   5.195 -        using X unfolding terminal_events_def by simp
   5.196 +        using X unfolding tail_events_def by simp
   5.197        from indep_setD[OF indep D this] indep_setD_ev1[OF indep] D
   5.198        show "D \<in> events \<and> prob (X \<inter> D) = prob X * prob D"
   5.199          by (auto simp add: ac_simps)
   5.200 @@ -591,12 +618,12 @@
   5.201    then have "(\<Union>n. sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) \<subseteq> ?D" (is "?A \<subseteq> _")
   5.202      by auto
   5.203  
   5.204 -  note `X \<in> terminal_events A`
   5.205 +  note `X \<in> tail_events A`
   5.206    also {
   5.207      have "\<And>n. sigma_sets (space M) (\<Union>i\<in>{n..}. A i) \<subseteq> sigma_sets (space M) ?A"
   5.208        by (intro sigma_sets_subseteq UN_mono) auto
   5.209 -   then have "terminal_events A \<subseteq> sigma_sets (space M) ?A"
   5.210 -      unfolding terminal_events_def by auto }
   5.211 +   then have "tail_events A \<subseteq> sigma_sets (space M) ?A"
   5.212 +      unfolding tail_events_def by auto }
   5.213    also have "sigma_sets (space M) ?A = dynkin (space M) ?A"
   5.214    proof (rule sigma_eq_dynkin)
   5.215      { fix B n assume "B \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
   5.216 @@ -628,34 +655,33 @@
   5.217  
   5.218  lemma (in prob_space) borel_0_1_law:
   5.219    fixes F :: "nat \<Rightarrow> 'a set"
   5.220 -  assumes F: "range F \<subseteq> events" "indep_events F UNIV"
   5.221 +  assumes F2: "indep_events F UNIV"
   5.222    shows "prob (\<Inter>n. \<Union>m\<in>{n..}. F m) = 0 \<or> prob (\<Inter>n. \<Union>m\<in>{n..}. F m) = 1"
   5.223  proof (rule kolmogorov_0_1_law[of "\<lambda>i. sigma_sets (space M) { F i }"])
   5.224 -  show "\<And>i. sigma_sets (space M) {F i} \<subseteq> events"
   5.225 -    using F(1) sets_into_space
   5.226 -    by (subst sigma_sets_singleton) auto
   5.227 +  have F1: "range F \<subseteq> events"
   5.228 +    using F2 by (simp add: indep_events_def subset_eq)
   5.229    { fix i show "sigma_algebra (space M) (sigma_sets (space M) {F i})"
   5.230 -      using sigma_algebra_sigma_sets[of "{F i}" "space M"] F sets_into_space
   5.231 +      using sigma_algebra_sigma_sets[of "{F i}" "space M"] F1 sets_into_space
   5.232        by auto }
   5.233    show "indep_sets (\<lambda>i. sigma_sets (space M) {F i}) UNIV"
   5.234    proof (rule indep_sets_sigma)
   5.235      show "indep_sets (\<lambda>i. {F i}) UNIV"
   5.236 -      unfolding indep_sets_singleton_iff_indep_events by fact
   5.237 +      unfolding indep_events_def_alt[symmetric] by fact
   5.238      fix i show "Int_stable {F i}"
   5.239        unfolding Int_stable_def by simp
   5.240    qed
   5.241    let ?Q = "\<lambda>n. \<Union>i\<in>{n..}. F i"
   5.242 -  show "(\<Inter>n. \<Union>m\<in>{n..}. F m) \<in> terminal_events (\<lambda>i. sigma_sets (space M) {F i})"
   5.243 -    unfolding terminal_events_def
   5.244 +  show "(\<Inter>n. \<Union>m\<in>{n..}. F m) \<in> tail_events (\<lambda>i. sigma_sets (space M) {F i})"
   5.245 +    unfolding tail_events_def
   5.246    proof
   5.247      fix j
   5.248      interpret S: sigma_algebra "space M" "sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   5.249 -      using order_trans[OF F(1) space_closed]
   5.250 +      using order_trans[OF F1 space_closed]
   5.251        by (intro sigma_algebra_sigma_sets) (simp add: sigma_sets_singleton subset_eq)
   5.252      have "(\<Inter>n. ?Q n) = (\<Inter>n\<in>{j..}. ?Q n)"
   5.253        by (intro decseq_SucI INT_decseq_offset UN_mono) auto
   5.254      also have "\<dots> \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   5.255 -      using order_trans[OF F(1) space_closed]
   5.256 +      using order_trans[OF F1 space_closed]
   5.257        by (safe intro!: S.countable_INT S.countable_UN)
   5.258           (auto simp: sigma_sets_singleton intro!: sigma_sets.Basic bexI)
   5.259      finally show "(\<Inter>n. ?Q n) \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   5.260 @@ -872,7 +898,7 @@
   5.261        show "sets ?P' = sigma_sets (space ?P) (prod_algebra I M')"
   5.262          by (simp add: sets_PiM space_PiM cong: prod_algebra_cong)
   5.263        let ?A = "\<lambda>i. \<Pi>\<^isub>E i\<in>I. space (M' i)"
   5.264 -      show "range ?A \<subseteq> prod_algebra I M'" "incseq ?A" "(\<Union>i. ?A i) = space (Pi\<^isub>M I M')"
   5.265 +      show "range ?A \<subseteq> prod_algebra I M'" "(\<Union>i. ?A i) = space (Pi\<^isub>M I M')"
   5.266          by (auto simp: space_PiM intro!: space_in_prod_algebra cong: prod_algebra_cong)
   5.267        { fix i show "emeasure ?D (\<Pi>\<^isub>E i\<in>I. space (M' i)) \<noteq> \<infinity>" by auto }
   5.268      next
   5.269 @@ -1022,13 +1048,13 @@
   5.270      then show "sets M = sigma_sets (space ?P) ?E"
   5.271        using sets[symmetric] by simp
   5.272    next
   5.273 -    show "range F \<subseteq> ?E" "incseq F" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   5.274 +    show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   5.275        using F by (auto simp: space_pair_measure)
   5.276    next
   5.277      fix X assume "X \<in> ?E"
   5.278      then obtain A B where X[simp]: "X = A \<times> B" and A: "A \<in> sets M1" and B: "B \<in> sets M2" by auto
   5.279      then have "emeasure ?P X = emeasure M1 A * emeasure M2 B"
   5.280 -       by (simp add: emeasure_pair_measure_Times)
   5.281 +       by (simp add: M2.emeasure_pair_measure_Times)
   5.282      also have "\<dots> = emeasure M (A \<times> B)"
   5.283        using A B emeasure by auto
   5.284      finally show "emeasure ?P X = emeasure M X"
   5.285 @@ -1136,7 +1162,7 @@
   5.286        also have "\<dots> = emeasure (?S \<Otimes>\<^isub>M ?T) (A \<times> B)"
   5.287          unfolding `?S \<Otimes>\<^isub>M ?T = ?J` ..
   5.288        also have "\<dots> = emeasure ?S A * emeasure ?T B"
   5.289 -        using ab by (simp add: XY.emeasure_pair_measure_Times)
   5.290 +        using ab by (simp add: Y.emeasure_pair_measure_Times)
   5.291        finally show "prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)) =
   5.292          prob (X -` A \<inter> space M) * prob (Y -` B \<inter> space M)"
   5.293          using rvs ab by (simp add: emeasure_eq_measure emeasure_distr)
   5.294 @@ -1144,4 +1170,12 @@
   5.295    qed
   5.296  qed
   5.297  
   5.298 +lemma (in prob_space) distributed_joint_indep:
   5.299 +  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
   5.300 +  assumes X: "distributed M S X Px" and Y: "distributed M T Y Py"
   5.301 +  assumes indep: "indep_var S X T Y"
   5.302 +  shows "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) (\<lambda>(x, y). Px x * Py y)"
   5.303 +  using indep_var_distribution_eq[of S X T Y] indep
   5.304 +  by (intro distributed_joint_indep'[OF S T X Y]) auto
   5.305 +
   5.306  end
     6.1 --- a/src/HOL/Probability/Infinite_Product_Measure.thy	Wed Oct 10 15:16:44 2012 +0200
     6.2 +++ b/src/HOL/Probability/Infinite_Product_Measure.thy	Wed Oct 10 15:17:18 2012 +0200
     6.3 @@ -8,41 +8,16 @@
     6.4    imports Probability_Measure Caratheodory
     6.5  begin
     6.6  
     6.7 -lemma (in product_sigma_finite)
     6.8 -  assumes IJ: "I \<inter> J = {}" "finite I" "finite J" and A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
     6.9 -  shows emeasure_fold_integral:
    6.10 -    "emeasure (Pi\<^isub>M (I \<union> J) M) A = (\<integral>\<^isup>+x. emeasure (Pi\<^isub>M J M) (merge I x J -` A \<inter> space (Pi\<^isub>M J M)) \<partial>Pi\<^isub>M I M)" (is ?I)
    6.11 -    and emeasure_fold_measurable:
    6.12 -    "(\<lambda>x. emeasure (Pi\<^isub>M J M) (merge I x J -` A \<inter> space (Pi\<^isub>M J M))) \<in> borel_measurable (Pi\<^isub>M I M)" (is ?B)
    6.13 -proof -
    6.14 -  interpret I: finite_product_sigma_finite M I by default fact
    6.15 -  interpret J: finite_product_sigma_finite M J by default fact
    6.16 -  interpret IJ: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" ..
    6.17 -  have merge: "(\<lambda>(x, y). merge I x J y) -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) \<in> sets (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
    6.18 -    by (intro measurable_sets[OF _ A] measurable_merge assms)
    6.19 -
    6.20 -  show ?I
    6.21 -    apply (subst distr_merge[symmetric, OF IJ])
    6.22 -    apply (subst emeasure_distr[OF measurable_merge[OF IJ(1)] A])
    6.23 -    apply (subst IJ.emeasure_pair_measure_alt[OF merge])
    6.24 -    apply (auto intro!: positive_integral_cong arg_cong2[where f=emeasure] simp: space_pair_measure)
    6.25 -    done
    6.26 -
    6.27 -  show ?B
    6.28 -    using IJ.measurable_emeasure_Pair1[OF merge]
    6.29 -    by (simp add: vimage_compose[symmetric] comp_def space_pair_measure cong: measurable_cong)
    6.30 -qed
    6.31 -
    6.32  lemma restrict_extensional_sub[intro]: "A \<subseteq> B \<Longrightarrow> restrict f A \<in> extensional B"
    6.33    unfolding restrict_def extensional_def by auto
    6.34  
    6.35  lemma restrict_restrict[simp]: "restrict (restrict f A) B = restrict f (A \<inter> B)"
    6.36    unfolding restrict_def by (simp add: fun_eq_iff)
    6.37  
    6.38 -lemma split_merge: "P (merge I x J y i) \<longleftrightarrow> (i \<in> I \<longrightarrow> P (x i)) \<and> (i \<in> J - I \<longrightarrow> P (y i)) \<and> (i \<notin> I \<union> J \<longrightarrow> P undefined)"
    6.39 +lemma split_merge: "P (merge I J (x,y) i) \<longleftrightarrow> (i \<in> I \<longrightarrow> P (x i)) \<and> (i \<in> J - I \<longrightarrow> P (y i)) \<and> (i \<notin> I \<union> J \<longrightarrow> P undefined)"
    6.40    unfolding merge_def by auto
    6.41  
    6.42 -lemma extensional_merge_sub: "I \<union> J \<subseteq> K \<Longrightarrow> merge I x J y \<in> extensional K"
    6.43 +lemma extensional_merge_sub: "I \<union> J \<subseteq> K \<Longrightarrow> merge I J (x, y) \<in> extensional K"
    6.44    unfolding merge_def extensional_def by auto
    6.45  
    6.46  lemma injective_vimage_restrict:
    6.47 @@ -57,7 +32,7 @@
    6.48    show "x \<in> A \<longleftrightarrow> x \<in> B"
    6.49    proof cases
    6.50      assume x: "x \<in> (\<Pi>\<^isub>E i\<in>J. S i)"
    6.51 -    have "x \<in> A \<longleftrightarrow> merge J x (I - J) y \<in> (\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^isub>E i\<in>I. S i)"
    6.52 +    have "x \<in> A \<longleftrightarrow> merge J (I - J) (x,y) \<in> (\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^isub>E i\<in>I. S i)"
    6.53        using y x `J \<subseteq> I` by (auto simp add: Pi_iff extensional_restrict extensional_merge_sub split: split_merge)
    6.54      then show "x \<in> A \<longleftrightarrow> x \<in> B"
    6.55        using y x `J \<subseteq> I` by (auto simp add: Pi_iff extensional_restrict extensional_merge_sub eq split: split_merge)
    6.56 @@ -112,7 +87,7 @@
    6.57    let ?\<Omega> = "(\<Pi>\<^isub>E k\<in>J. space (M k))"
    6.58    show "Int_stable ?J"
    6.59      by (rule Int_stable_PiE)
    6.60 -  show "range ?F \<subseteq> ?J" "incseq ?F" "(\<Union>i. ?F i) = ?\<Omega>"
    6.61 +  show "range ?F \<subseteq> ?J" "(\<Union>i. ?F i) = ?\<Omega>"
    6.62      using `finite J` by (auto intro!: prod_algebraI_finite)
    6.63    { fix i show "emeasure ?P (?F i) \<noteq> \<infinity>" by simp }
    6.64    show "?J \<subseteq> Pow ?\<Omega>" by (auto simp: Pi_iff dest: sets_into_space)
    6.65 @@ -156,7 +131,7 @@
    6.66    show "X \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))" "Y \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))"
    6.67      using sets[THEN sets_into_space] by (auto simp: space_PiM)
    6.68    have "\<forall>i\<in>L. \<exists>x. x \<in> space (M i)"
    6.69 -    using M.not_empty by auto
    6.70 +      using M.not_empty by auto
    6.71    from bchoice[OF this]
    6.72    show "(\<Pi>\<^isub>E i\<in>L. space (M i)) \<noteq> {}" by auto
    6.73    show "(\<lambda>x. restrict x J) -` X \<inter> (\<Pi>\<^isub>E i\<in>L. space (M i)) = (\<lambda>x. restrict x J) -` Y \<inter> (\<Pi>\<^isub>E i\<in>L. space (M i))"
    6.74 @@ -199,15 +174,24 @@
    6.75  qed
    6.76  
    6.77  lemma (in product_prob_space) sets_PiM_generator:
    6.78 -  assumes "I \<noteq> {}" shows "sets (PiM I M) = sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) generator"
    6.79 -proof
    6.80 -  show "sets (Pi\<^isub>M I M) \<subseteq> sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) generator"
    6.81 -    unfolding sets_PiM
    6.82 -  proof (safe intro!: sigma_sets_subseteq)
    6.83 -    fix A assume "A \<in> prod_algebra I M" with `I \<noteq> {}` show "A \<in> generator"
    6.84 -      by (auto intro!: generatorI' elim!: prod_algebraE)
    6.85 -  qed
    6.86 -qed (auto simp: generator_def space_PiM[symmetric] intro!: sigma_sets_subset)
    6.87 +  "sets (PiM I M) = sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) generator"
    6.88 +proof cases
    6.89 +  assume "I = {}" then show ?thesis
    6.90 +    unfolding generator_def
    6.91 +    by (auto simp: sets_PiM_empty sigma_sets_empty_eq cong: conj_cong)
    6.92 +next
    6.93 +  assume "I \<noteq> {}"
    6.94 +  show ?thesis
    6.95 +  proof
    6.96 +    show "sets (Pi\<^isub>M I M) \<subseteq> sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) generator"
    6.97 +      unfolding sets_PiM
    6.98 +    proof (safe intro!: sigma_sets_subseteq)
    6.99 +      fix A assume "A \<in> prod_algebra I M" with `I \<noteq> {}` show "A \<in> generator"
   6.100 +        by (auto intro!: generatorI' elim!: prod_algebraE)
   6.101 +    qed
   6.102 +  qed (auto simp: generator_def space_PiM[symmetric] intro!: sigma_sets_subset)
   6.103 +qed
   6.104 +
   6.105  
   6.106  lemma (in product_prob_space) generatorI:
   6.107    "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> A = emb I J X \<Longrightarrow> A \<in> generator"
   6.108 @@ -255,23 +239,19 @@
   6.109  qed
   6.110  
   6.111  lemma (in product_prob_space) merge_sets:
   6.112 -  assumes "finite J" "finite K" "J \<inter> K = {}" and A: "A \<in> sets (Pi\<^isub>M (J \<union> K) M)" and x: "x \<in> space (Pi\<^isub>M J M)"
   6.113 -  shows "merge J x K -` A \<inter> space (Pi\<^isub>M K M) \<in> sets (Pi\<^isub>M K M)"
   6.114 -proof -
   6.115 -  from sets_Pair1[OF
   6.116 -    measurable_merge[THEN measurable_sets, OF `J \<inter> K = {}`], OF A, of x] x
   6.117 -  show ?thesis
   6.118 -      by (simp add: space_pair_measure comp_def vimage_compose[symmetric])
   6.119 -qed
   6.120 +  assumes "J \<inter> K = {}" and A: "A \<in> sets (Pi\<^isub>M (J \<union> K) M)" and x: "x \<in> space (Pi\<^isub>M J M)"
   6.121 +  shows "(\<lambda>y. merge J K (x,y)) -` A \<inter> space (Pi\<^isub>M K M) \<in> sets (Pi\<^isub>M K M)"
   6.122 +  by (rule measurable_sets[OF _ A] measurable_compose[OF measurable_Pair measurable_merge]  
   6.123 +           measurable_const x measurable_ident)+
   6.124  
   6.125  lemma (in product_prob_space) merge_emb:
   6.126    assumes "K \<subseteq> I" "J \<subseteq> I" and y: "y \<in> space (Pi\<^isub>M J M)"
   6.127 -  shows "(merge J y (I - J) -` emb I K X \<inter> space (Pi\<^isub>M I M)) =
   6.128 -    emb I (K - J) (merge J y (K - J) -` emb (J \<union> K) K X \<inter> space (Pi\<^isub>M (K - J) M))"
   6.129 +  shows "((\<lambda>x. merge J (I - J) (y, x)) -` emb I K X \<inter> space (Pi\<^isub>M I M)) =
   6.130 +    emb I (K - J) ((\<lambda>x. merge J (K - J) (y, x)) -` emb (J \<union> K) K X \<inter> space (Pi\<^isub>M (K - J) M))"
   6.131  proof -
   6.132 -  have [simp]: "\<And>x J K L. merge J y K (restrict x L) = merge J y (K \<inter> L) x"
   6.133 +  have [simp]: "\<And>x J K L. merge J K (y, restrict x L) = merge J (K \<inter> L) (y, x)"
   6.134      by (auto simp: restrict_def merge_def)
   6.135 -  have [simp]: "\<And>x J K L. restrict (merge J y K x) L = merge (J \<inter> L) y (K \<inter> L) x"
   6.136 +  have [simp]: "\<And>x J K L. restrict (merge J K (y, x)) L = merge (J \<inter> L) (K \<inter> L) (y, x)"
   6.137      by (auto simp: restrict_def merge_def)
   6.138    have [simp]: "(I - J) \<inter> K = K - J" using `K \<subseteq> I` `J \<subseteq> I` by auto
   6.139    have [simp]: "(K - J) \<inter> (K \<union> J) = K - J" by auto
   6.140 @@ -356,16 +336,16 @@
   6.141        "K - J \<noteq> {}" "K - J \<subseteq> I" "\<mu>G Z = emeasure (Pi\<^isub>M K M) X"
   6.142        by (auto simp: subset_insertI)
   6.143  
   6.144 -    let ?M = "\<lambda>y. merge J y (K - J) -` emb (J \<union> K) K X \<inter> space (Pi\<^isub>M (K - J) M)"
   6.145 +    let ?M = "\<lambda>y. (\<lambda>x. merge J (K - J) (y, x)) -` emb (J \<union> K) K X \<inter> space (Pi\<^isub>M (K - J) M)"
   6.146      { fix y assume y: "y \<in> space (Pi\<^isub>M J M)"
   6.147        note * = merge_emb[OF `K \<subseteq> I` `J \<subseteq> I` y, of X]
   6.148        moreover
   6.149        have **: "?M y \<in> sets (Pi\<^isub>M (K - J) M)"
   6.150          using J K y by (intro merge_sets) auto
   6.151        ultimately
   6.152 -      have ***: "(merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M)) \<in> ?G"
   6.153 +      have ***: "((\<lambda>x. merge J (I - J) (y, x)) -` Z \<inter> space (Pi\<^isub>M I M)) \<in> ?G"
   6.154          using J K by (intro generatorI) auto
   6.155 -      have "\<mu>G (merge J y (I - J) -` emb I K X \<inter> space (Pi\<^isub>M I M)) = emeasure (Pi\<^isub>M (K - J) M) (?M y)"
   6.156 +      have "\<mu>G ((\<lambda>x. merge J (I - J) (y, x)) -` emb I K X \<inter> space (Pi\<^isub>M I M)) = emeasure (Pi\<^isub>M (K - J) M) (?M y)"
   6.157          unfolding * using K J by (subst \<mu>G_eq[OF _ _ _ **]) auto
   6.158        note * ** *** this }
   6.159      note merge_in_G = this
   6.160 @@ -379,7 +359,7 @@
   6.161        using K J by simp
   6.162      also have "\<dots> = (\<integral>\<^isup>+ x. emeasure (Pi\<^isub>M (K - J) M) (?M x) \<partial>Pi\<^isub>M J M)"
   6.163        using K J by (subst emeasure_fold_integral) auto
   6.164 -    also have "\<dots> = (\<integral>\<^isup>+ y. \<mu>G (merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M)) \<partial>Pi\<^isub>M J M)"
   6.165 +    also have "\<dots> = (\<integral>\<^isup>+ y. \<mu>G ((\<lambda>x. merge J (I - J) (y, x)) -` Z \<inter> space (Pi\<^isub>M I M)) \<partial>Pi\<^isub>M J M)"
   6.166        (is "_ = (\<integral>\<^isup>+x. \<mu>G (?MZ x) \<partial>Pi\<^isub>M J M)")
   6.167      proof (intro positive_integral_cong)
   6.168        fix x assume x: "x \<in> space (Pi\<^isub>M J M)"
   6.169 @@ -395,7 +375,7 @@
   6.170          by (intro KmJ.measure_le_1 merge_in_G(2)[OF x]) }
   6.171      note le_1 = this
   6.172  
   6.173 -    let ?q = "\<lambda>y. \<mu>G (merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M))"
   6.174 +    let ?q = "\<lambda>y. \<mu>G ((\<lambda>x. merge J (I - J) (y,x)) -` Z \<inter> space (Pi\<^isub>M I M))"
   6.175      have "?q \<in> borel_measurable (Pi\<^isub>M J M)"
   6.176        unfolding `Z = emb I K X` using J K merge_in_G(3)
   6.177        by (simp add: merge_in_G  \<mu>G_eq emeasure_fold_measurable cong: measurable_cong)
   6.178 @@ -441,7 +421,7 @@
   6.179          using \<mu>G_spec[of "J 0" "A 0" "X 0"] J A_eq
   6.180          by (auto intro!: INF_lower2[of 0] J.measure_le_1)
   6.181  
   6.182 -      let ?M = "\<lambda>K Z y. merge K y (I - K) -` Z \<inter> space (Pi\<^isub>M I M)"
   6.183 +      let ?M = "\<lambda>K Z y. (\<lambda>x. merge K (I - K) (y, x)) -` Z \<inter> space (Pi\<^isub>M I M)"
   6.184  
   6.185        { fix Z k assume Z: "range Z \<subseteq> ?G" "decseq Z" "\<forall>n. ?a / 2^k \<le> \<mu>G (Z n)"
   6.186          then have Z_sets: "\<And>n. Z n \<in> ?G" by auto
   6.187 @@ -549,9 +529,9 @@
   6.188              from Ex_w[OF this `?D \<noteq> {}`] J[of "Suc k"]
   6.189              obtain w' where w': "w' \<in> space (Pi\<^isub>M ?D M)"
   6.190                "\<forall>n. ?a / 2 ^ (Suc k + 1) \<le> \<mu>G (?M ?D (?M (J k) (A n) (w k)) w')" by auto
   6.191 -            let ?w = "merge (J k) (w k) ?D w'"
   6.192 -            have [simp]: "\<And>x. merge (J k) (w k) (I - J k) (merge ?D w' (I - ?D) x) =
   6.193 -              merge (J (Suc k)) ?w (I - (J (Suc k))) x"
   6.194 +            let ?w = "merge (J k) ?D (w k, w')"
   6.195 +            have [simp]: "\<And>x. merge (J k) (I - J k) (w k, merge ?D (I - ?D) (w', x)) =
   6.196 +              merge (J (Suc k)) (I - (J (Suc k))) (?w, x)"
   6.197                using J(3)[of "Suc k"] J(3)[of k] J_mono[of k "Suc k"]
   6.198                by (auto intro!: ext split: split_merge)
   6.199              have *: "\<And>n. ?M ?D (?M (J k) (A n) (w k)) w' = ?M (J (Suc k)) (A n) ?w"
   6.200 @@ -577,7 +557,7 @@
   6.201            using positive_\<mu>G[OF I_not_empty, unfolded positive_def] `0 < ?a` `?a \<le> 1`
   6.202            by (cases ?a) (auto simp: divide_le_0_iff power_le_zero_eq)
   6.203          then obtain x where "x \<in> ?M (J k) (A k) (w k)" by auto
   6.204 -        then have "merge (J k) (w k) (I - J k) x \<in> A k" by auto
   6.205 +        then have "merge (J k) (I - J k) (w k, x) \<in> A k" by auto
   6.206          then have "\<exists>x\<in>A k. restrict x (J k) = w k"
   6.207            using `w k \<in> space (Pi\<^isub>M (J k) M)`
   6.208            by (intro rev_bexI) (auto intro!: ext simp: extensional_def space_PiM)
   6.209 @@ -657,7 +637,7 @@
   6.210        using X by (auto simp add: emeasure_PiM) 
   6.211    next
   6.212      show "positive (sets (Pi\<^isub>M I M)) \<mu>" "countably_additive (sets (Pi\<^isub>M I M)) \<mu>"
   6.213 -      using \<mu> unfolding sets_PiM_generator[OF `I \<noteq> {}`] by (auto simp: measure_space_def)
   6.214 +      using \<mu> unfolding sets_PiM_generator by (auto simp: measure_space_def)
   6.215    qed
   6.216  qed
   6.217  
     7.1 --- a/src/HOL/Probability/Information.thy	Wed Oct 10 15:16:44 2012 +0200
     7.2 +++ b/src/HOL/Probability/Information.thy	Wed Oct 10 15:17:18 2012 +0200
     7.3 @@ -22,104 +22,6 @@
     7.4    "(\<Sum>x\<in>A \<times> B. f x) = (\<Sum>x\<in>A. setsum (\<lambda>y. f (x, y)) B)"
     7.5    unfolding setsum_cartesian_product by simp
     7.6  
     7.7 -section "Convex theory"
     7.8 -
     7.9 -lemma log_setsum:
    7.10 -  assumes "finite s" "s \<noteq> {}"
    7.11 -  assumes "b > 1"
    7.12 -  assumes "(\<Sum> i \<in> s. a i) = 1"
    7.13 -  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0"
    7.14 -  assumes "\<And> i. i \<in> s \<Longrightarrow> y i \<in> {0 <..}"
    7.15 -  shows "log b (\<Sum> i \<in> s. a i * y i) \<ge> (\<Sum> i \<in> s. a i * log b (y i))"
    7.16 -proof -
    7.17 -  have "convex_on {0 <..} (\<lambda> x. - log b x)"
    7.18 -    by (rule minus_log_convex[OF `b > 1`])
    7.19 -  hence "- log b (\<Sum> i \<in> s. a i * y i) \<le> (\<Sum> i \<in> s. a i * - log b (y i))"
    7.20 -    using convex_on_setsum[of _ _ "\<lambda> x. - log b x"] assms pos_is_convex by fastforce
    7.21 -  thus ?thesis by (auto simp add:setsum_negf le_imp_neg_le)
    7.22 -qed
    7.23 -
    7.24 -lemma log_setsum':
    7.25 -  assumes "finite s" "s \<noteq> {}"
    7.26 -  assumes "b > 1"
    7.27 -  assumes "(\<Sum> i \<in> s. a i) = 1"
    7.28 -  assumes pos: "\<And> i. i \<in> s \<Longrightarrow> 0 \<le> a i"
    7.29 -          "\<And> i. \<lbrakk> i \<in> s ; 0 < a i \<rbrakk> \<Longrightarrow> 0 < y i"
    7.30 -  shows "log b (\<Sum> i \<in> s. a i * y i) \<ge> (\<Sum> i \<in> s. a i * log b (y i))"
    7.31 -proof -
    7.32 -  have "\<And>y. (\<Sum> i \<in> s - {i. a i = 0}. a i * y i) = (\<Sum> i \<in> s. a i * y i)"
    7.33 -    using assms by (auto intro!: setsum_mono_zero_cong_left)
    7.34 -  moreover have "log b (\<Sum> i \<in> s - {i. a i = 0}. a i * y i) \<ge> (\<Sum> i \<in> s - {i. a i = 0}. a i * log b (y i))"
    7.35 -  proof (rule log_setsum)
    7.36 -    have "setsum a (s - {i. a i = 0}) = setsum a s"
    7.37 -      using assms(1) by (rule setsum_mono_zero_cong_left) auto
    7.38 -    thus sum_1: "setsum a (s - {i. a i = 0}) = 1"
    7.39 -      "finite (s - {i. a i = 0})" using assms by simp_all
    7.40 -
    7.41 -    show "s - {i. a i = 0} \<noteq> {}"
    7.42 -    proof
    7.43 -      assume *: "s - {i. a i = 0} = {}"
    7.44 -      hence "setsum a (s - {i. a i = 0}) = 0" by (simp add: * setsum_empty)
    7.45 -      with sum_1 show False by simp
    7.46 -    qed
    7.47 -
    7.48 -    fix i assume "i \<in> s - {i. a i = 0}"
    7.49 -    hence "i \<in> s" "a i \<noteq> 0" by simp_all
    7.50 -    thus "0 \<le> a i" "y i \<in> {0<..}" using pos[of i] by auto
    7.51 -  qed fact+
    7.52 -  ultimately show ?thesis by simp
    7.53 -qed
    7.54 -
    7.55 -lemma log_setsum_divide:
    7.56 -  assumes "finite S" and "S \<noteq> {}" and "1 < b"
    7.57 -  assumes "(\<Sum>x\<in>S. g x) = 1"
    7.58 -  assumes pos: "\<And>x. x \<in> S \<Longrightarrow> g x \<ge> 0" "\<And>x. x \<in> S \<Longrightarrow> f x \<ge> 0"
    7.59 -  assumes g_pos: "\<And>x. \<lbrakk> x \<in> S ; 0 < g x \<rbrakk> \<Longrightarrow> 0 < f x"
    7.60 -  shows "- (\<Sum>x\<in>S. g x * log b (g x / f x)) \<le> log b (\<Sum>x\<in>S. f x)"
    7.61 -proof -
    7.62 -  have log_mono: "\<And>x y. 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log b x \<le> log b y"
    7.63 -    using `1 < b` by (subst log_le_cancel_iff) auto
    7.64 -
    7.65 -  have "- (\<Sum>x\<in>S. g x * log b (g x / f x)) = (\<Sum>x\<in>S. g x * log b (f x / g x))"
    7.66 -  proof (unfold setsum_negf[symmetric], rule setsum_cong)
    7.67 -    fix x assume x: "x \<in> S"
    7.68 -    show "- (g x * log b (g x / f x)) = g x * log b (f x / g x)"
    7.69 -    proof (cases "g x = 0")
    7.70 -      case False
    7.71 -      with pos[OF x] g_pos[OF x] have "0 < f x" "0 < g x" by simp_all
    7.72 -      thus ?thesis using `1 < b` by (simp add: log_divide field_simps)
    7.73 -    qed simp
    7.74 -  qed rule
    7.75 -  also have "... \<le> log b (\<Sum>x\<in>S. g x * (f x / g x))"
    7.76 -  proof (rule log_setsum')
    7.77 -    fix x assume x: "x \<in> S" "0 < g x"
    7.78 -    with g_pos[OF x] show "0 < f x / g x" by (safe intro!: divide_pos_pos)
    7.79 -  qed fact+
    7.80 -  also have "... = log b (\<Sum>x\<in>S - {x. g x = 0}. f x)" using `finite S`
    7.81 -    by (auto intro!: setsum_mono_zero_cong_right arg_cong[where f="log b"]
    7.82 -        split: split_if_asm)
    7.83 -  also have "... \<le> log b (\<Sum>x\<in>S. f x)"
    7.84 -  proof (rule log_mono)
    7.85 -    have "0 = (\<Sum>x\<in>S - {x. g x = 0}. 0)" by simp
    7.86 -    also have "... < (\<Sum>x\<in>S - {x. g x = 0}. f x)" (is "_ < ?sum")
    7.87 -    proof (rule setsum_strict_mono)
    7.88 -      show "finite (S - {x. g x = 0})" using `finite S` by simp
    7.89 -      show "S - {x. g x = 0} \<noteq> {}"
    7.90 -      proof
    7.91 -        assume "S - {x. g x = 0} = {}"
    7.92 -        hence "(\<Sum>x\<in>S. g x) = 0" by (subst setsum_0') auto
    7.93 -        with `(\<Sum>x\<in>S. g x) = 1` show False by simp
    7.94 -      qed
    7.95 -      fix x assume "x \<in> S - {x. g x = 0}"
    7.96 -      thus "0 < f x" using g_pos[of x] pos(1)[of x] by auto
    7.97 -    qed
    7.98 -    finally show "0 < ?sum" .
    7.99 -    show "(\<Sum>x\<in>S - {x. g x = 0}. f x) \<le> (\<Sum>x\<in>S. f x)"
   7.100 -      using `finite S` pos by (auto intro!: setsum_mono2)
   7.101 -  qed
   7.102 -  finally show ?thesis .
   7.103 -qed
   7.104 -
   7.105  lemma split_pairs:
   7.106    "((A, B) = X) \<longleftrightarrow> (fst X = A \<and> snd X = B)" and
   7.107    "(X = (A, B)) \<longleftrightarrow> (fst X = A \<and> snd X = B)" by auto
   7.108 @@ -194,7 +96,7 @@
   7.109    unfolding KL_divergence_def
   7.110  proof (subst integral_density)
   7.111    show "entropy_density b M (density M (\<lambda>x. ereal (f x))) \<in> borel_measurable M"
   7.112 -    using f `1 < b`
   7.113 +    using f
   7.114      by (auto simp: comp_def entropy_density_def intro!: borel_measurable_log borel_measurable_RN_deriv_density)
   7.115    have "density M (RN_deriv M (density M f)) = density M f"
   7.116      using f by (intro density_RN_deriv_density) auto
   7.117 @@ -437,6 +339,115 @@
   7.118    finally show ?thesis .
   7.119  qed
   7.120  
   7.121 +subsection {* Finite Entropy *}
   7.122 +
   7.123 +definition (in information_space) 
   7.124 +  "finite_entropy S X f \<longleftrightarrow> distributed M S X f \<and> integrable S (\<lambda>x. f x * log b (f x))"
   7.125 +
   7.126 +lemma (in information_space) finite_entropy_simple_function:
   7.127 +  assumes X: "simple_function M X"
   7.128 +  shows "finite_entropy (count_space (X`space M)) X (\<lambda>a. measure M {x \<in> space M. X x = a})"
   7.129 +  unfolding finite_entropy_def
   7.130 +proof
   7.131 +  have [simp]: "finite (X ` space M)"
   7.132 +    using X by (auto simp: simple_function_def)
   7.133 +  then show "integrable (count_space (X ` space M))
   7.134 +     (\<lambda>x. prob {xa \<in> space M. X xa = x} * log b (prob {xa \<in> space M. X xa = x}))"
   7.135 +    by (rule integrable_count_space)
   7.136 +  have d: "distributed M (count_space (X ` space M)) X (\<lambda>x. ereal (if x \<in> X`space M then prob {xa \<in> space M. X xa = x} else 0))"
   7.137 +    by (rule distributed_simple_function_superset[OF X]) (auto intro!: arg_cong[where f=prob])
   7.138 +  show "distributed M (count_space (X ` space M)) X (\<lambda>x. ereal (prob {xa \<in> space M. X xa = x}))"
   7.139 +    by (rule distributed_cong_density[THEN iffD1, OF _ _ _ d]) auto
   7.140 +qed
   7.141 +
   7.142 +lemma distributed_transform_AE:
   7.143 +  assumes T: "T \<in> measurable P Q" "absolutely_continuous Q (distr P Q T)"
   7.144 +  assumes g: "distributed M Q Y g"
   7.145 +  shows "AE x in P. 0 \<le> g (T x)"
   7.146 +  using g
   7.147 +  apply (subst AE_distr_iff[symmetric, OF T(1)])
   7.148 +  apply (simp add: distributed_borel_measurable)
   7.149 +  apply (rule absolutely_continuous_AE[OF _ T(2)])
   7.150 +  apply simp
   7.151 +  apply (simp add: distributed_AE)
   7.152 +  done
   7.153 +
   7.154 +lemma ac_fst:
   7.155 +  assumes "sigma_finite_measure T"
   7.156 +  shows "absolutely_continuous S (distr (S \<Otimes>\<^isub>M T) S fst)"
   7.157 +proof -
   7.158 +  interpret sigma_finite_measure T by fact
   7.159 +  { fix A assume "A \<in> sets S" "emeasure S A = 0"
   7.160 +    moreover then have "fst -` A \<inter> space (S \<Otimes>\<^isub>M T) = A \<times> space T"
   7.161 +      by (auto simp: space_pair_measure dest!: sets_into_space)
   7.162 +    ultimately have "emeasure (S \<Otimes>\<^isub>M T) (fst -` A \<inter> space (S \<Otimes>\<^isub>M T)) = 0"
   7.163 +      by (simp add: emeasure_pair_measure_Times) }
   7.164 +  then show ?thesis
   7.165 +    unfolding absolutely_continuous_def
   7.166 +    apply (auto simp: null_sets_distr_iff)
   7.167 +    apply (auto simp: null_sets_def intro!: measurable_sets)
   7.168 +    done
   7.169 +qed
   7.170 +
   7.171 +lemma ac_snd:
   7.172 +  assumes "sigma_finite_measure T"
   7.173 +  shows "absolutely_continuous T (distr (S \<Otimes>\<^isub>M T) T snd)"
   7.174 +proof -
   7.175 +  interpret sigma_finite_measure T by fact
   7.176 +  { fix A assume "A \<in> sets T" "emeasure T A = 0"
   7.177 +    moreover then have "snd -` A \<inter> space (S \<Otimes>\<^isub>M T) = space S \<times> A"
   7.178 +      by (auto simp: space_pair_measure dest!: sets_into_space)
   7.179 +    ultimately have "emeasure (S \<Otimes>\<^isub>M T) (snd -` A \<inter> space (S \<Otimes>\<^isub>M T)) = 0"
   7.180 +      by (simp add: emeasure_pair_measure_Times) }
   7.181 +  then show ?thesis
   7.182 +    unfolding absolutely_continuous_def
   7.183 +    apply (auto simp: null_sets_distr_iff)
   7.184 +    apply (auto simp: null_sets_def intro!: measurable_sets)
   7.185 +    done
   7.186 +qed
   7.187 +
   7.188 +lemma distributed_integrable:
   7.189 +  "distributed M N X f \<Longrightarrow> g \<in> borel_measurable N \<Longrightarrow>
   7.190 +    integrable N (\<lambda>x. f x * g x) \<longleftrightarrow> integrable M (\<lambda>x. g (X x))"
   7.191 +  by (auto simp: distributed_real_measurable distributed_real_AE distributed_measurable
   7.192 +                    distributed_distr_eq_density[symmetric] integral_density[symmetric] integrable_distr_eq)
   7.193 +  
   7.194 +lemma distributed_transform_integrable:
   7.195 +  assumes Px: "distributed M N X Px"
   7.196 +  assumes "distributed M P Y Py"
   7.197 +  assumes Y: "Y = (\<lambda>x. T (X x))" and T: "T \<in> measurable N P" and f: "f \<in> borel_measurable P"
   7.198 +  shows "integrable P (\<lambda>x. Py x * f x) \<longleftrightarrow> integrable N (\<lambda>x. Px x * f (T x))"
   7.199 +proof -
   7.200 +  have "integrable P (\<lambda>x. Py x * f x) \<longleftrightarrow> integrable M (\<lambda>x. f (Y x))"
   7.201 +    by (rule distributed_integrable) fact+
   7.202 +  also have "\<dots> \<longleftrightarrow> integrable M (\<lambda>x. f (T (X x)))"
   7.203 +    using Y by simp
   7.204 +  also have "\<dots> \<longleftrightarrow> integrable N (\<lambda>x. Px x * f (T x))"
   7.205 +    using measurable_comp[OF T f] Px by (intro distributed_integrable[symmetric]) (auto simp: comp_def)
   7.206 +  finally show ?thesis .
   7.207 +qed
   7.208 +
   7.209 +lemma integrable_cong_AE_imp: "integrable M g \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> (AE x in M. g x = f x) \<Longrightarrow> integrable M f"
   7.210 +  using integrable_cong_AE by blast
   7.211 +
   7.212 +lemma (in information_space) finite_entropy_integrable:
   7.213 +  "finite_entropy S X Px \<Longrightarrow> integrable S (\<lambda>x. Px x * log b (Px x))"
   7.214 +  unfolding finite_entropy_def by auto
   7.215 +
   7.216 +lemma (in information_space) finite_entropy_distributed:
   7.217 +  "finite_entropy S X Px \<Longrightarrow> distributed M S X Px"
   7.218 +  unfolding finite_entropy_def by auto
   7.219 +
   7.220 +lemma (in information_space) finite_entropy_integrable_transform:
   7.221 +  assumes Fx: "finite_entropy S X Px"
   7.222 +  assumes Fy: "distributed M T Y Py"
   7.223 +    and "X = (\<lambda>x. f (Y x))"
   7.224 +    and "f \<in> measurable T S"
   7.225 +  shows "integrable T (\<lambda>x. Py x * log b (Px (f x)))"
   7.226 +  using assms unfolding finite_entropy_def
   7.227 +  using distributed_transform_integrable[of M T Y Py S X Px f "\<lambda>x. log b (Px x)"]
   7.228 +  by (auto intro: distributed_real_measurable)
   7.229 +
   7.230  subsection {* Mutual Information *}
   7.231  
   7.232  definition (in prob_space)
   7.233 @@ -510,6 +521,120 @@
   7.234  
   7.235  lemma (in information_space)
   7.236    fixes Pxy :: "'b \<times> 'c \<Rightarrow> real" and Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
   7.237 +  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
   7.238 +  assumes Fx: "finite_entropy S X Px" and Fy: "finite_entropy T Y Py"
   7.239 +  assumes Fxy: "finite_entropy (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
   7.240 +  defines "f \<equiv> \<lambda>x. Pxy x * log b (Pxy x / (Px (fst x) * Py (snd x)))"
   7.241 +  shows mutual_information_distr': "mutual_information b S T X Y = integral\<^isup>L (S \<Otimes>\<^isub>M T) f" (is "?M = ?R")
   7.242 +    and mutual_information_nonneg': "0 \<le> mutual_information b S T X Y"
   7.243 +proof -
   7.244 +  have Px: "distributed M S X Px"
   7.245 +    using Fx by (auto simp: finite_entropy_def)
   7.246 +  have Py: "distributed M T Y Py"
   7.247 +    using Fy by (auto simp: finite_entropy_def)
   7.248 +  have Pxy: "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
   7.249 +    using Fxy by (auto simp: finite_entropy_def)
   7.250 +
   7.251 +  have X: "random_variable S X"
   7.252 +    using Px by (auto simp: distributed_def finite_entropy_def)
   7.253 +  have Y: "random_variable T Y"
   7.254 +    using Py by (auto simp: distributed_def finite_entropy_def)
   7.255 +  interpret S: sigma_finite_measure S by fact
   7.256 +  interpret T: sigma_finite_measure T by fact
   7.257 +  interpret ST: pair_sigma_finite S T ..
   7.258 +  interpret X: prob_space "distr M S X" using X by (rule prob_space_distr)
   7.259 +  interpret Y: prob_space "distr M T Y" using Y by (rule prob_space_distr)
   7.260 +  interpret XY: pair_prob_space "distr M S X" "distr M T Y" ..
   7.261 +  let ?P = "S \<Otimes>\<^isub>M T"
   7.262 +  let ?D = "distr M ?P (\<lambda>x. (X x, Y x))"
   7.263 +
   7.264 +  { fix A assume "A \<in> sets S"
   7.265 +    with X Y have "emeasure (distr M S X) A = emeasure ?D (A \<times> space T)"
   7.266 +      by (auto simp: emeasure_distr measurable_Pair measurable_space
   7.267 +               intro!: arg_cong[where f="emeasure M"]) }
   7.268 +  note marginal_eq1 = this
   7.269 +  { fix A assume "A \<in> sets T"
   7.270 +    with X Y have "emeasure (distr M T Y) A = emeasure ?D (space S \<times> A)"
   7.271 +      by (auto simp: emeasure_distr measurable_Pair measurable_space
   7.272 +               intro!: arg_cong[where f="emeasure M"]) }
   7.273 +  note marginal_eq2 = this
   7.274 +
   7.275 +  have eq: "(\<lambda>x. ereal (Px (fst x) * Py (snd x))) = (\<lambda>(x, y). ereal (Px x) * ereal (Py y))"
   7.276 +    by auto
   7.277 +
   7.278 +  have distr_eq: "distr M S X \<Otimes>\<^isub>M distr M T Y = density ?P (\<lambda>x. ereal (Px (fst x) * Py (snd x)))"
   7.279 +    unfolding Px(1)[THEN distributed_distr_eq_density] Py(1)[THEN distributed_distr_eq_density] eq
   7.280 +  proof (subst pair_measure_density)
   7.281 +    show "(\<lambda>x. ereal (Px x)) \<in> borel_measurable S" "(\<lambda>y. ereal (Py y)) \<in> borel_measurable T"
   7.282 +      "AE x in S. 0 \<le> ereal (Px x)" "AE y in T. 0 \<le> ereal (Py y)"
   7.283 +      using Px Py by (auto simp: distributed_def)
   7.284 +    show "sigma_finite_measure (density S Px)" unfolding Px(1)[THEN distributed_distr_eq_density, symmetric] ..
   7.285 +    show "sigma_finite_measure (density T Py)" unfolding Py(1)[THEN distributed_distr_eq_density, symmetric] ..
   7.286 +  qed (fact | simp)+
   7.287 +  
   7.288 +  have M: "?M = KL_divergence b (density ?P (\<lambda>x. ereal (Px (fst x) * Py (snd x)))) (density ?P (\<lambda>x. ereal (Pxy x)))"
   7.289 +    unfolding mutual_information_def distr_eq Pxy(1)[THEN distributed_distr_eq_density] ..
   7.290 +
   7.291 +  from Px Py have f: "(\<lambda>x. Px (fst x) * Py (snd x)) \<in> borel_measurable ?P"
   7.292 +    by (intro borel_measurable_times) (auto intro: distributed_real_measurable measurable_fst'' measurable_snd'')
   7.293 +  have PxPy_nonneg: "AE x in ?P. 0 \<le> Px (fst x) * Py (snd x)"
   7.294 +  proof (rule ST.AE_pair_measure)
   7.295 +    show "{x \<in> space ?P. 0 \<le> Px (fst x) * Py (snd x)} \<in> sets ?P"
   7.296 +      using f by auto
   7.297 +    show "AE x in S. AE y in T. 0 \<le> Px (fst (x, y)) * Py (snd (x, y))"
   7.298 +      using Px Py by (auto simp: zero_le_mult_iff dest!: distributed_real_AE)
   7.299 +  qed
   7.300 +
   7.301 +  have "(AE x in ?P. Px (fst x) = 0 \<longrightarrow> Pxy x = 0)"
   7.302 +    by (rule subdensity_real[OF measurable_fst Pxy Px]) auto
   7.303 +  moreover
   7.304 +  have "(AE x in ?P. Py (snd x) = 0 \<longrightarrow> Pxy x = 0)"
   7.305 +    by (rule subdensity_real[OF measurable_snd Pxy Py]) auto
   7.306 +  ultimately have ac: "AE x in ?P. Px (fst x) * Py (snd x) = 0 \<longrightarrow> Pxy x = 0"
   7.307 +    by eventually_elim auto
   7.308 +
   7.309 +  show "?M = ?R"
   7.310 +    unfolding M f_def
   7.311 +    using b_gt_1 f PxPy_nonneg Pxy[THEN distributed_real_measurable] Pxy[THEN distributed_real_AE] ac
   7.312 +    by (rule ST.KL_density_density)
   7.313 +
   7.314 +  have X: "X = fst \<circ> (\<lambda>x. (X x, Y x))" and Y: "Y = snd \<circ> (\<lambda>x. (X x, Y x))"
   7.315 +    by auto
   7.316 +
   7.317 +  have "integrable (S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Pxy x) - Pxy x * log b (Px (fst x)) - Pxy x * log b (Py (snd x)))"
   7.318 +    using finite_entropy_integrable[OF Fxy]
   7.319 +    using finite_entropy_integrable_transform[OF Fx Pxy, of fst]
   7.320 +    using finite_entropy_integrable_transform[OF Fy Pxy, of snd]
   7.321 +    by simp
   7.322 +  moreover have "f \<in> borel_measurable (S \<Otimes>\<^isub>M T)"
   7.323 +    unfolding f_def using Px Py Pxy
   7.324 +    by (auto intro: distributed_real_measurable measurable_fst'' measurable_snd''
   7.325 +      intro!: borel_measurable_times borel_measurable_log borel_measurable_divide)
   7.326 +  ultimately have int: "integrable (S \<Otimes>\<^isub>M T) f"
   7.327 +    apply (rule integrable_cong_AE_imp)
   7.328 +    using
   7.329 +      distributed_transform_AE[OF measurable_fst ac_fst, of T, OF T Px]
   7.330 +      distributed_transform_AE[OF measurable_snd ac_snd, of _ _ _ _ S, OF T Py]
   7.331 +      subdensity_real[OF measurable_fst Pxy Px X]
   7.332 +      subdensity_real[OF measurable_snd Pxy Py Y]
   7.333 +      distributed_real_AE[OF Pxy]
   7.334 +    by eventually_elim
   7.335 +       (auto simp: f_def log_divide_eq log_mult_eq field_simps zero_less_mult_iff mult_nonneg_nonneg)
   7.336 +
   7.337 +  show "0 \<le> ?M" unfolding M
   7.338 +  proof (rule ST.KL_density_density_nonneg
   7.339 +    [OF b_gt_1 f PxPy_nonneg _ Pxy[THEN distributed_real_measurable] Pxy[THEN distributed_real_AE] _ ac int[unfolded f_def]])
   7.340 +    show "prob_space (density (S \<Otimes>\<^isub>M T) (\<lambda>x. ereal (Pxy x))) "
   7.341 +      unfolding distributed_distr_eq_density[OF Pxy, symmetric]
   7.342 +      using distributed_measurable[OF Pxy] by (rule prob_space_distr)
   7.343 +    show "prob_space (density (S \<Otimes>\<^isub>M T) (\<lambda>x. ereal (Px (fst x) * Py (snd x))))"
   7.344 +      unfolding distr_eq[symmetric] by unfold_locales
   7.345 +  qed
   7.346 +qed
   7.347 +
   7.348 +
   7.349 +lemma (in information_space)
   7.350 +  fixes Pxy :: "'b \<times> 'c \<Rightarrow> real" and Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
   7.351    assumes "sigma_finite_measure S" "sigma_finite_measure T"
   7.352    assumes Px: "distributed M S X Px" and Py: "distributed M T Y Py"
   7.353    assumes Pxy: "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
   7.354 @@ -664,84 +789,209 @@
   7.355    entropy_Pow ("\<H>'(_')") where
   7.356    "\<H>(X) \<equiv> entropy b (count_space (X`space M)) X"
   7.357  
   7.358 -lemma (in information_space) entropy_distr:
   7.359 +lemma (in prob_space) distributed_RN_deriv:
   7.360 +  assumes X: "distributed M S X Px"
   7.361 +  shows "AE x in S. RN_deriv S (density S Px) x = Px x"
   7.362 +proof -
   7.363 +  note D = distributed_measurable[OF X] distributed_borel_measurable[OF X] distributed_AE[OF X]
   7.364 +  interpret X: prob_space "distr M S X"
   7.365 +    using D(1) by (rule prob_space_distr)
   7.366 +
   7.367 +  have sf: "sigma_finite_measure (distr M S X)" by default
   7.368 +  show ?thesis
   7.369 +    using D
   7.370 +    apply (subst eq_commute)
   7.371 +    apply (intro RN_deriv_unique_sigma_finite)
   7.372 +    apply (auto intro: divide_nonneg_nonneg measure_nonneg
   7.373 +             simp: distributed_distr_eq_density[symmetric, OF X] sf)
   7.374 +    done
   7.375 +qed
   7.376 +
   7.377 +lemma (in information_space)
   7.378    fixes X :: "'a \<Rightarrow> 'b"
   7.379 -  assumes "sigma_finite_measure MX" and X: "distributed M MX X f"
   7.380 -  shows "entropy b MX X = - (\<integral>x. f x * log b (f x) \<partial>MX)"
   7.381 +  assumes X: "distributed M MX X f"
   7.382 +  shows entropy_distr: "entropy b MX X = - (\<integral>x. f x * log b (f x) \<partial>MX)" (is ?eq)
   7.383 +proof -
   7.384 +  note D = distributed_measurable[OF X] distributed_borel_measurable[OF X] distributed_AE[OF X]
   7.385 +  note ae = distributed_RN_deriv[OF X]
   7.386 +
   7.387 +  have ae_eq: "AE x in distr M MX X. log b (real (RN_deriv MX (distr M MX X) x)) =
   7.388 +    log b (f x)"
   7.389 +    unfolding distributed_distr_eq_density[OF X]
   7.390 +    apply (subst AE_density)
   7.391 +    using D apply simp
   7.392 +    using ae apply eventually_elim
   7.393 +    apply auto
   7.394 +    done
   7.395 +
   7.396 +  have int_eq: "- (\<integral> x. log b (f x) \<partial>distr M MX X) = - (\<integral> x. f x * log b (f x) \<partial>MX)"
   7.397 +    unfolding distributed_distr_eq_density[OF X]
   7.398 +    using D
   7.399 +    by (subst integral_density)
   7.400 +       (auto simp: borel_measurable_ereal_iff)
   7.401 +
   7.402 +  show ?eq
   7.403 +    unfolding entropy_def KL_divergence_def entropy_density_def comp_def
   7.404 +    apply (subst integral_cong_AE)
   7.405 +    apply (rule ae_eq)
   7.406 +    apply (rule int_eq)
   7.407 +    done
   7.408 +qed
   7.409 +
   7.410 +lemma (in prob_space) distributed_imp_emeasure_nonzero:
   7.411 +  assumes X: "distributed M MX X Px"
   7.412 +  shows "emeasure MX {x \<in> space MX. Px x \<noteq> 0} \<noteq> 0"
   7.413 +proof
   7.414 +  note Px = distributed_borel_measurable[OF X] distributed_AE[OF X]
   7.415 +  interpret X: prob_space "distr M MX X"
   7.416 +    using distributed_measurable[OF X] by (rule prob_space_distr)
   7.417 +
   7.418 +  assume "emeasure MX {x \<in> space MX. Px x \<noteq> 0} = 0"
   7.419 +  with Px have "AE x in MX. Px x = 0"
   7.420 +    by (intro AE_I[OF subset_refl]) (auto simp: borel_measurable_ereal_iff)
   7.421 +  moreover
   7.422 +  from X.emeasure_space_1 have "(\<integral>\<^isup>+x. Px x \<partial>MX) = 1"
   7.423 +    unfolding distributed_distr_eq_density[OF X] using Px
   7.424 +    by (subst (asm) emeasure_density)
   7.425 +       (auto simp: borel_measurable_ereal_iff intro!: integral_cong cong: positive_integral_cong)
   7.426 +  ultimately show False
   7.427 +    by (simp add: positive_integral_cong_AE)
   7.428 +qed
   7.429 +
   7.430 +lemma (in information_space) entropy_le:
   7.431 +  fixes Px :: "'b \<Rightarrow> real" and MX :: "'b measure"
   7.432 +  assumes X: "distributed M MX X Px"
   7.433 +  and fin: "emeasure MX {x \<in> space MX. Px x \<noteq> 0} \<noteq> \<infinity>"
   7.434 +  and int: "integrable MX (\<lambda>x. - Px x * log b (Px x))"
   7.435 +  shows "entropy b MX X \<le> log b (measure MX {x \<in> space MX. Px x \<noteq> 0})"
   7.436  proof -
   7.437 -  interpret MX: sigma_finite_measure MX by fact
   7.438 -  from X show ?thesis
   7.439 -    unfolding entropy_def X[THEN distributed_distr_eq_density]
   7.440 -    by (subst MX.KL_density[OF b_gt_1]) (simp_all add: distributed_real_AE distributed_real_measurable)
   7.441 +  note Px = distributed_borel_measurable[OF X] distributed_AE[OF X]
   7.442 +  interpret X: prob_space "distr M MX X"
   7.443 +    using distributed_measurable[OF X] by (rule prob_space_distr)
   7.444 +
   7.445 +  have " - log b (measure MX {x \<in> space MX. Px x \<noteq> 0}) = 
   7.446 +    - log b (\<integral> x. indicator {x \<in> space MX. Px x \<noteq> 0} x \<partial>MX)"
   7.447 +    using Px fin
   7.448 +    by (subst integral_indicator) (auto simp: measure_def borel_measurable_ereal_iff)
   7.449 +  also have "- log b (\<integral> x. indicator {x \<in> space MX. Px x \<noteq> 0} x \<partial>MX) = - log b (\<integral> x. 1 / Px x \<partial>distr M MX X)"
   7.450 +    unfolding distributed_distr_eq_density[OF X] using Px
   7.451 +    apply (intro arg_cong[where f="log b"] arg_cong[where f=uminus])
   7.452 +    by (subst integral_density) (auto simp: borel_measurable_ereal_iff intro!: integral_cong)
   7.453 +  also have "\<dots> \<le> (\<integral> x. - log b (1 / Px x) \<partial>distr M MX X)"
   7.454 +  proof (rule X.jensens_inequality[of "\<lambda>x. 1 / Px x" "{0<..}" 0 1 "\<lambda>x. - log b x"])
   7.455 +    show "AE x in distr M MX X. 1 / Px x \<in> {0<..}"
   7.456 +      unfolding distributed_distr_eq_density[OF X]
   7.457 +      using Px by (auto simp: AE_density)
   7.458 +    have [simp]: "\<And>x. x \<in> space MX \<Longrightarrow> ereal (if Px x = 0 then 0 else 1) = indicator {x \<in> space MX. Px x \<noteq> 0} x"
   7.459 +      by (auto simp: one_ereal_def)
   7.460 +    have "(\<integral>\<^isup>+ x. max 0 (ereal (- (if Px x = 0 then 0 else 1))) \<partial>MX) = (\<integral>\<^isup>+ x. 0 \<partial>MX)"
   7.461 +      by (intro positive_integral_cong) (auto split: split_max)
   7.462 +    then show "integrable (distr M MX X) (\<lambda>x. 1 / Px x)"
   7.463 +      unfolding distributed_distr_eq_density[OF X] using Px
   7.464 +      by (auto simp: positive_integral_density integrable_def borel_measurable_ereal_iff fin positive_integral_max_0
   7.465 +              cong: positive_integral_cong)
   7.466 +    have "integrable MX (\<lambda>x. Px x * log b (1 / Px x)) =
   7.467 +      integrable MX (\<lambda>x. - Px x * log b (Px x))"
   7.468 +      using Px
   7.469 +      by (intro integrable_cong_AE)
   7.470 +         (auto simp: borel_measurable_ereal_iff log_divide_eq
   7.471 +                  intro!: measurable_If)
   7.472 +    then show "integrable (distr M MX X) (\<lambda>x. - log b (1 / Px x))"
   7.473 +      unfolding distributed_distr_eq_density[OF X]
   7.474 +      using Px int
   7.475 +      by (subst integral_density) (auto simp: borel_measurable_ereal_iff)
   7.476 +  qed (auto simp: minus_log_convex[OF b_gt_1])
   7.477 +  also have "\<dots> = (\<integral> x. log b (Px x) \<partial>distr M MX X)"
   7.478 +    unfolding distributed_distr_eq_density[OF X] using Px
   7.479 +    by (intro integral_cong_AE) (auto simp: AE_density log_divide_eq)
   7.480 +  also have "\<dots> = - entropy b MX X"
   7.481 +    unfolding distributed_distr_eq_density[OF X] using Px
   7.482 +    by (subst entropy_distr[OF X]) (auto simp: borel_measurable_ereal_iff integral_density)
   7.483 +  finally show ?thesis
   7.484 +    by simp
   7.485 +qed
   7.486 +
   7.487 +lemma (in information_space) entropy_le_space:
   7.488 +  fixes Px :: "'b \<Rightarrow> real" and MX :: "'b measure"
   7.489 +  assumes X: "distributed M MX X Px"
   7.490 +  and fin: "finite_measure MX"
   7.491 +  and int: "integrable MX (\<lambda>x. - Px x * log b (Px x))"
   7.492 +  shows "entropy b MX X \<le> log b (measure MX (space MX))"
   7.493 +proof -
   7.494 +  note Px = distributed_borel_measurable[OF X] distributed_AE[OF X]
   7.495 +  interpret finite_measure MX by fact
   7.496 +  have "entropy b MX X \<le> log b (measure MX {x \<in> space MX. Px x \<noteq> 0})"
   7.497 +    using int X by (intro entropy_le) auto
   7.498 +  also have "\<dots> \<le> log b (measure MX (space MX))"
   7.499 +    using Px distributed_imp_emeasure_nonzero[OF X]
   7.500 +    by (intro log_le)
   7.501 +       (auto intro!: borel_measurable_ereal_iff finite_measure_mono b_gt_1
   7.502 +                     less_le[THEN iffD2] measure_nonneg simp: emeasure_eq_measure)
   7.503 +  finally show ?thesis .
   7.504 +qed
   7.505 +
   7.506 +lemma (in prob_space) uniform_distributed_params:
   7.507 +  assumes X: "distributed M MX X (\<lambda>x. indicator A x / measure MX A)"
   7.508 +  shows "A \<in> sets MX" "measure MX A \<noteq> 0"
   7.509 +proof -
   7.510 +  interpret X: prob_space "distr M MX X"
   7.511 +    using distributed_measurable[OF X] by (rule prob_space_distr)
   7.512 +
   7.513 +  show "measure MX A \<noteq> 0"
   7.514 +  proof
   7.515 +    assume "measure MX A = 0"
   7.516 +    with X.emeasure_space_1 X.prob_space distributed_distr_eq_density[OF X]
   7.517 +    show False
   7.518 +      by (simp add: emeasure_density zero_ereal_def[symmetric])
   7.519 +  qed
   7.520 +  with measure_notin_sets[of A MX] show "A \<in> sets MX"
   7.521 +    by blast
   7.522  qed
   7.523  
   7.524  lemma (in information_space) entropy_uniform:
   7.525 -  assumes "sigma_finite_measure MX"
   7.526 -  assumes A: "A \<in> sets MX" "emeasure MX A \<noteq> 0" "emeasure MX A \<noteq> \<infinity>"
   7.527 -  assumes X: "distributed M MX X (\<lambda>x. 1 / measure MX A * indicator A x)"
   7.528 +  assumes X: "distributed M MX X (\<lambda>x. indicator A x / measure MX A)" (is "distributed _ _ _ ?f")
   7.529    shows "entropy b MX X = log b (measure MX A)"
   7.530 -proof (subst entropy_distr[OF _ X])
   7.531 -  let ?f = "\<lambda>x. 1 / measure MX A * indicator A x"
   7.532 -  have "- (\<integral>x. ?f x * log b (?f x) \<partial>MX) = 
   7.533 -    - (\<integral>x. (log b (1 / measure MX A) / measure MX A) * indicator A x \<partial>MX)"
   7.534 -    by (auto intro!: integral_cong simp: indicator_def)
   7.535 -  also have "\<dots> = - log b (inverse (measure MX A))"
   7.536 -    using A by (subst integral_cmult(2))
   7.537 -               (simp_all add: measure_def real_of_ereal_eq_0 integral_cmult inverse_eq_divide)
   7.538 -  also have "\<dots> = log b (measure MX A)"
   7.539 -    using b_gt_1 A by (subst log_inverse) (auto simp add: measure_def less_le real_of_ereal_eq_0
   7.540 -                                                          emeasure_nonneg real_of_ereal_pos)
   7.541 -  finally show "- (\<integral>x. ?f x * log b (?f x) \<partial>MX) = log b (measure MX A)" by simp
   7.542 -qed fact+
   7.543 +proof (subst entropy_distr[OF X])
   7.544 +  have [simp]: "emeasure MX A \<noteq> \<infinity>"
   7.545 +    using uniform_distributed_params[OF X] by (auto simp add: measure_def)
   7.546 +  have eq: "(\<integral> x. indicator A x / measure MX A * log b (indicator A x / measure MX A) \<partial>MX) =
   7.547 +    (\<integral> x. (- log b (measure MX A) / measure MX A) * indicator A x \<partial>MX)"
   7.548 +    using measure_nonneg[of MX A] uniform_distributed_params[OF X]
   7.549 +    by (auto intro!: integral_cong split: split_indicator simp: log_divide_eq)
   7.550 +  show "- (\<integral> x. indicator A x / measure MX A * log b (indicator A x / measure MX A) \<partial>MX) =
   7.551 +    log b (measure MX A)"
   7.552 +    unfolding eq using uniform_distributed_params[OF X]
   7.553 +    by (subst lebesgue_integral_cmult) (auto simp: measure_def)
   7.554 +qed
   7.555  
   7.556  lemma (in information_space) entropy_simple_distributed:
   7.557 -  fixes X :: "'a \<Rightarrow> 'b"
   7.558 -  assumes X: "simple_distributed M X f"
   7.559 -  shows "\<H>(X) = - (\<Sum>x\<in>X`space M. f x * log b (f x))"
   7.560 -proof (subst entropy_distr[OF _ simple_distributed[OF X]])
   7.561 -  show "sigma_finite_measure (count_space (X ` space M))"
   7.562 -    using X by (simp add: sigma_finite_measure_count_space_finite simple_distributed_def)
   7.563 -  show "- (\<integral>x. f x * log b (f x) \<partial>(count_space (X`space M))) = - (\<Sum>x\<in>X ` space M. f x * log b (f x))"
   7.564 -    using X by (auto simp add: lebesgue_integral_count_space_finite)
   7.565 -qed
   7.566 +  "simple_distributed M X f \<Longrightarrow> \<H>(X) = - (\<Sum>x\<in>X`space M. f x * log b (f x))"
   7.567 +  by (subst entropy_distr[OF simple_distributed])
   7.568 +     (auto simp add: lebesgue_integral_count_space_finite)
   7.569  
   7.570  lemma (in information_space) entropy_le_card_not_0:
   7.571    assumes X: "simple_distributed M X f"
   7.572    shows "\<H>(X) \<le> log b (card (X ` space M \<inter> {x. f x \<noteq> 0}))"
   7.573  proof -
   7.574 -  have "\<H>(X) = (\<Sum>x\<in>X`space M. f x * log b (1 / f x))"
   7.575 -    unfolding entropy_simple_distributed[OF X] setsum_negf[symmetric]
   7.576 -    using X by (auto dest: simple_distributed_nonneg intro!: setsum_cong simp: log_simps less_le)
   7.577 -  also have "\<dots> \<le> log b (\<Sum>x\<in>X`space M. f x * (1 / f x))"
   7.578 -    using not_empty b_gt_1 `simple_distributed M X f`
   7.579 -    by (intro log_setsum') (auto simp: simple_distributed_nonneg simple_distributed_setsum_space)
   7.580 -  also have "\<dots> = log b (\<Sum>x\<in>X`space M. if f x \<noteq> 0 then 1 else 0)"
   7.581 -    by (intro arg_cong[where f="\<lambda>X. log b X"] setsum_cong) auto
   7.582 -  finally show ?thesis
   7.583 -    using `simple_distributed M X f` by (auto simp: setsum_cases real_eq_of_nat)
   7.584 +  let ?X = "count_space (X`space M)"
   7.585 +  have "\<H>(X) \<le> log b (measure ?X {x \<in> space ?X. f x \<noteq> 0})"
   7.586 +    by (rule entropy_le[OF simple_distributed[OF X]])
   7.587 +       (simp_all add: simple_distributed_finite[OF X] subset_eq integrable_count_space emeasure_count_space)
   7.588 +  also have "measure ?X {x \<in> space ?X. f x \<noteq> 0} = card (X ` space M \<inter> {x. f x \<noteq> 0})"
   7.589 +    by (simp_all add: simple_distributed_finite[OF X] subset_eq emeasure_count_space measure_def Int_def)
   7.590 +  finally show ?thesis .
   7.591  qed
   7.592  
   7.593  lemma (in information_space) entropy_le_card:
   7.594 -  assumes "simple_distributed M X f"
   7.595 +  assumes X: "simple_distributed M X f"
   7.596    shows "\<H>(X) \<le> log b (real (card (X ` space M)))"
   7.597 -proof cases
   7.598 -  assume "X ` space M \<inter> {x. f x \<noteq> 0} = {}"
   7.599 -  then have "\<And>x. x\<in>X`space M \<Longrightarrow> f x = 0" by auto
   7.600 -  moreover
   7.601 -  have "0 < card (X`space M)"
   7.602 -    using `simple_distributed M X f` not_empty by (auto simp: card_gt_0_iff)
   7.603 -  then have "log b 1 \<le> log b (real (card (X`space M)))"
   7.604 -    using b_gt_1 by (intro log_le) auto
   7.605 -  ultimately show ?thesis using assms by (simp add: entropy_simple_distributed)
   7.606 -next
   7.607 -  assume False: "X ` space M \<inter> {x. f x \<noteq> 0} \<noteq> {}"
   7.608 -  have "card (X ` space M \<inter> {x. f x \<noteq> 0}) \<le> card (X ` space M)"
   7.609 -    (is "?A \<le> ?B") using assms not_empty
   7.610 -    by (auto intro!: card_mono simp: simple_function_def simple_distributed_def)
   7.611 -  note entropy_le_card_not_0[OF assms]
   7.612 -  also have "log b (real ?A) \<le> log b (real ?B)"
   7.613 -    using b_gt_1 False not_empty `?A \<le> ?B` assms
   7.614 -    by (auto intro!: log_le simp: card_gt_0_iff simp: simple_distributed_def)
   7.615 +proof -
   7.616 +  let ?X = "count_space (X`space M)"
   7.617 +  have "\<H>(X) \<le> log b (measure ?X (space ?X))"
   7.618 +    by (rule entropy_le_space[OF simple_distributed[OF X]])
   7.619 +       (simp_all add: simple_distributed_finite[OF X] subset_eq integrable_count_space emeasure_count_space finite_measure_count_space)
   7.620 +  also have "measure ?X (space ?X) = card (X ` space M)"
   7.621 +    by (simp_all add: simple_distributed_finite[OF X] subset_eq emeasure_count_space measure_def)
   7.622    finally show ?thesis .
   7.623  qed
   7.624  
   7.625 @@ -757,7 +1007,18 @@
   7.626    "\<I>(X ; Y | Z) \<equiv> conditional_mutual_information b
   7.627      (count_space (X ` space M)) (count_space (Y ` space M)) (count_space (Z ` space M)) X Y Z"
   7.628  
   7.629 -lemma (in information_space) conditional_mutual_information_generic_eq:
   7.630 +lemma (in pair_sigma_finite) borel_measurable_positive_integral_fst:
   7.631 +  "(\<lambda>(x, y). f x y) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2) \<Longrightarrow> (\<lambda>x. \<integral>\<^isup>+ y. f x y \<partial>M2) \<in> borel_measurable M1"
   7.632 +  using positive_integral_fst_measurable(1)[of "\<lambda>(x, y). f x y"] by simp
   7.633 +
   7.634 +lemma (in pair_sigma_finite) borel_measurable_positive_integral_snd:
   7.635 +  assumes "(\<lambda>(x, y). f x y) \<in> borel_measurable (M2 \<Otimes>\<^isub>M M1)" shows "(\<lambda>x. \<integral>\<^isup>+ y. f x y \<partial>M1) \<in> borel_measurable M2"
   7.636 +proof -
   7.637 +  interpret Q: pair_sigma_finite M2 M1 by default
   7.638 +  from Q.borel_measurable_positive_integral_fst assms show ?thesis by simp
   7.639 +qed
   7.640 +
   7.641 +lemma (in information_space)
   7.642    assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T" and P: "sigma_finite_measure P"
   7.643    assumes Px: "distributed M S X Px"
   7.644    assumes Pz: "distributed M P Z Pz"
   7.645 @@ -766,16 +1027,19 @@
   7.646    assumes Pxyz: "distributed M (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>x. (X x, Y x, Z x)) Pxyz"
   7.647    assumes I1: "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Px x * Pyz (y, z))))"
   7.648    assumes I2: "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxz (x, z) / (Px x * Pz z)))"
   7.649 -  shows "conditional_mutual_information b S T P X Y Z
   7.650 -    = (\<integral>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))"
   7.651 +  shows conditional_mutual_information_generic_eq: "conditional_mutual_information b S T P X Y Z
   7.652 +    = (\<integral>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))" (is "?eq")
   7.653 +    and conditional_mutual_information_generic_nonneg: "0 \<le> conditional_mutual_information b S T P X Y Z" (is "?nonneg")
   7.654  proof -
   7.655    interpret S: sigma_finite_measure S by fact
   7.656    interpret T: sigma_finite_measure T by fact
   7.657    interpret P: sigma_finite_measure P by fact
   7.658    interpret TP: pair_sigma_finite T P ..
   7.659    interpret SP: pair_sigma_finite S P ..
   7.660 +  interpret ST: pair_sigma_finite S T ..
   7.661    interpret SPT: pair_sigma_finite "S \<Otimes>\<^isub>M P" T ..
   7.662    interpret STP: pair_sigma_finite S "T \<Otimes>\<^isub>M P" ..
   7.663 +  interpret TPS: pair_sigma_finite "T \<Otimes>\<^isub>M P" S ..
   7.664    have TP: "sigma_finite_measure (T \<Otimes>\<^isub>M P)" ..
   7.665    have SP: "sigma_finite_measure (S \<Otimes>\<^isub>M P)" ..
   7.666    have YZ: "random_variable (T \<Otimes>\<^isub>M P) (\<lambda>x. (Y x, Z x))"
   7.667 @@ -811,27 +1075,27 @@
   7.668    finally have mi_eq:
   7.669      "mutual_information b S P X Z = (\<integral>(x,y,z). Pxyz (x,y,z) * log b (Pxz (x,z) / (Px x * Pz z)) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))" .
   7.670    
   7.671 -  have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Px (fst x) = 0 \<longrightarrow> Pxyz x = 0"
   7.672 +  have ae1: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Px (fst x) = 0 \<longrightarrow> Pxyz x = 0"
   7.673      by (intro subdensity_real[of fst, OF _ Pxyz Px]) auto
   7.674 -  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pz (snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
   7.675 +  moreover have ae2: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pz (snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
   7.676      by (intro subdensity_real[of "\<lambda>x. snd (snd x)", OF _ Pxyz Pz]) (auto intro: measurable_snd')
   7.677 -  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pxz (fst x, snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
   7.678 +  moreover have ae3: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pxz (fst x, snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
   7.679      by (intro subdensity_real[of "\<lambda>x. (fst x, snd (snd x))", OF _ Pxyz Pxz]) (auto intro: measurable_Pair measurable_snd')
   7.680 -  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pyz (snd x) = 0 \<longrightarrow> Pxyz x = 0"
   7.681 +  moreover have ae4: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pyz (snd x) = 0 \<longrightarrow> Pxyz x = 0"
   7.682      by (intro subdensity_real[of snd, OF _ Pxyz Pyz]) (auto intro: measurable_Pair)
   7.683 -  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Px (fst x)"
   7.684 +  moreover have ae5: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Px (fst x)"
   7.685      using Px by (intro STP.AE_pair_measure) (auto simp: comp_def intro!: measurable_fst'' dest: distributed_real_AE distributed_real_measurable)
   7.686 -  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pyz (snd x)"
   7.687 +  moreover have ae6: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pyz (snd x)"
   7.688      using Pyz by (intro STP.AE_pair_measure) (auto simp: comp_def intro!: measurable_snd'' dest: distributed_real_AE distributed_real_measurable)
   7.689 -  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pz (snd (snd x))"
   7.690 +  moreover have ae7: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pz (snd (snd x))"
   7.691      using Pz Pz[THEN distributed_real_measurable] by (auto intro!: measurable_snd'' TP.AE_pair_measure STP.AE_pair_measure AE_I2[of S] dest: distributed_real_AE)
   7.692 -  moreover have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pxz (fst x, snd (snd x))"
   7.693 +  moreover have ae8: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pxz (fst x, snd (snd x))"
   7.694      using Pxz[THEN distributed_real_AE, THEN SP.AE_pair]
   7.695      using measurable_comp[OF measurable_Pair[OF measurable_fst measurable_comp[OF measurable_snd measurable_snd]] Pxz[THEN distributed_real_measurable], of T]
   7.696      using measurable_comp[OF measurable_snd measurable_Pair2[OF Pxz[THEN distributed_real_measurable]], of _ T]
   7.697      by (auto intro!: TP.AE_pair_measure STP.AE_pair_measure simp: comp_def)
   7.698    moreover note Pxyz[THEN distributed_real_AE]
   7.699 -  ultimately have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P.
   7.700 +  ultimately have ae: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P.
   7.701      Pxyz x * log b (Pxyz x / (Px (fst x) * Pyz (snd x))) -
   7.702      Pxyz x * log b (Pxz (fst x, snd (snd x)) / (Px (fst x) * Pz (snd (snd x)))) =
   7.703      Pxyz x * log b (Pxyz x * Pz (snd (snd x)) / (Pxz (fst x, snd (snd x)) * Pyz (snd x))) "
   7.704 @@ -846,13 +1110,454 @@
   7.705          using b_gt_1 by (simp add: log_simps mult_pos_pos less_imp_le field_simps)
   7.706      qed simp
   7.707    qed
   7.708 -  with I1 I2 show ?thesis
   7.709 +  with I1 I2 show ?eq
   7.710      unfolding conditional_mutual_information_def
   7.711      apply (subst mi_eq)
   7.712      apply (subst mutual_information_distr[OF S TP Px Pyz Pxyz])
   7.713      apply (subst integral_diff(2)[symmetric])
   7.714      apply (auto intro!: integral_cong_AE simp: split_beta' simp del: integral_diff)
   7.715      done
   7.716 +
   7.717 +  let ?P = "density (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) Pxyz"
   7.718 +  interpret P: prob_space ?P
   7.719 +    unfolding distributed_distr_eq_density[OF Pxyz, symmetric]
   7.720 +    using distributed_measurable[OF Pxyz] by (rule prob_space_distr)
   7.721 +
   7.722 +  let ?Q = "density (T \<Otimes>\<^isub>M P) Pyz"
   7.723 +  interpret Q: prob_space ?Q
   7.724 +    unfolding distributed_distr_eq_density[OF Pyz, symmetric]
   7.725 +    using distributed_measurable[OF Pyz] by (rule prob_space_distr)
   7.726 +
   7.727 +  let ?f = "\<lambda>(x, y, z). Pxz (x, z) * (Pyz (y, z) / Pz z) / Pxyz (x, y, z)"
   7.728 +
   7.729 +  from subdensity_real[of snd, OF _ Pyz Pz]
   7.730 +  have aeX1: "AE x in T \<Otimes>\<^isub>M P. Pz (snd x) = 0 \<longrightarrow> Pyz x = 0" by (auto simp: comp_def)
   7.731 +  have aeX2: "AE x in T \<Otimes>\<^isub>M P. 0 \<le> Pz (snd x)"
   7.732 +    using Pz by (intro TP.AE_pair_measure) (auto simp: comp_def intro!: measurable_snd'' dest: distributed_real_AE distributed_real_measurable)
   7.733 +
   7.734 +  have aeX3: "AE y in T \<Otimes>\<^isub>M P. (\<integral>\<^isup>+ x. ereal (Pxz (x, snd y)) \<partial>S) = ereal (Pz (snd y))"
   7.735 +    using Pz distributed_marginal_eq_joint2[OF P S Pz Pxz]
   7.736 +    apply (intro TP.AE_pair_measure)
   7.737 +    apply (auto simp: comp_def measurable_split_conv
   7.738 +                intro!: measurable_snd'' borel_measurable_ereal_eq borel_measurable_ereal
   7.739 +                        SP.borel_measurable_positive_integral_snd measurable_compose[OF _ Pxz[THEN distributed_real_measurable]]
   7.740 +                        measurable_Pair
   7.741 +                dest: distributed_real_AE distributed_real_measurable)
   7.742 +    done
   7.743 +
   7.744 +  note M = borel_measurable_divide borel_measurable_diff borel_measurable_times borel_measurable_ereal
   7.745 +           measurable_compose[OF _ measurable_snd]
   7.746 +           measurable_Pair
   7.747 +           measurable_compose[OF _ Pxyz[THEN distributed_real_measurable]]
   7.748 +           measurable_compose[OF _ Pxz[THEN distributed_real_measurable]]
   7.749 +           measurable_compose[OF _ Pyz[THEN distributed_real_measurable]]
   7.750 +           measurable_compose[OF _ Pz[THEN distributed_real_measurable]]
   7.751 +           measurable_compose[OF _ Px[THEN distributed_real_measurable]]
   7.752 +           STP.borel_measurable_positive_integral_snd
   7.753 +  have "(\<integral>\<^isup>+ x. ?f x \<partial>?P) \<le> (\<integral>\<^isup>+ (x, y, z). Pxz (x, z) * (Pyz (y, z) / Pz z) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))"
   7.754 +    apply (subst positive_integral_density)
   7.755 +    apply (rule distributed_borel_measurable[OF Pxyz])
   7.756 +    apply (rule distributed_AE[OF Pxyz])
   7.757 +    apply (auto simp add: borel_measurable_ereal_iff split_beta' intro!: M) []
   7.758 +    apply (rule positive_integral_mono_AE)
   7.759 +    using ae5 ae6 ae7 ae8
   7.760 +    apply eventually_elim
   7.761 +    apply (auto intro!: divide_nonneg_nonneg mult_nonneg_nonneg)
   7.762 +    done
   7.763 +  also have "\<dots> = (\<integral>\<^isup>+(y, z). \<integral>\<^isup>+ x. ereal (Pxz (x, z)) * ereal (Pyz (y, z) / Pz z) \<partial>S \<partial>T \<Otimes>\<^isub>M P)"
   7.764 +    by (subst STP.positive_integral_snd_measurable[symmetric])
   7.765 +       (auto simp add: borel_measurable_ereal_iff split_beta' intro!: M)
   7.766 +  also have "\<dots> = (\<integral>\<^isup>+x. ereal (Pyz x) * 1 \<partial>T \<Otimes>\<^isub>M P)"
   7.767 +    apply (rule positive_integral_cong_AE)
   7.768 +    using aeX1 aeX2 aeX3 distributed_AE[OF Pyz] AE_space
   7.769 +    apply eventually_elim
   7.770 +  proof (case_tac x, simp del: times_ereal.simps add: space_pair_measure)
   7.771 +    fix a b assume "Pz b = 0 \<longrightarrow> Pyz (a, b) = 0" "0 \<le> Pz b" "a \<in> space T \<and> b \<in> space P"
   7.772 +      "(\<integral>\<^isup>+ x. ereal (Pxz (x, b)) \<partial>S) = ereal (Pz b)" "0 \<le> Pyz (a, b)" 
   7.773 +    then show "(\<integral>\<^isup>+ x. ereal (Pxz (x, b)) * ereal (Pyz (a, b) / Pz b) \<partial>S) = ereal (Pyz (a, b))"
   7.774 +      apply (subst positive_integral_multc)
   7.775 +      apply (auto intro!: borel_measurable_ereal divide_nonneg_nonneg
   7.776 +                          measurable_compose[OF _ Pxz[THEN distributed_real_measurable]] measurable_Pair
   7.777 +                  split: prod.split)
   7.778 +      done
   7.779 +  qed
   7.780 +  also have "\<dots> = 1"
   7.781 +    using Q.emeasure_space_1 distributed_AE[OF Pyz] distributed_distr_eq_density[OF Pyz]
   7.782 +    by (subst positive_integral_density[symmetric]) (auto intro!: M)
   7.783 +  finally have le1: "(\<integral>\<^isup>+ x. ?f x \<partial>?P) \<le> 1" .
   7.784 +  also have "\<dots> < \<infinity>" by simp
   7.785 +  finally have fin: "(\<integral>\<^isup>+ x. ?f x \<partial>?P) \<noteq> \<infinity>" by simp
   7.786 +
   7.787 +  have pos: "(\<integral>\<^isup>+ x. ?f x \<partial>?P) \<noteq> 0"
   7.788 +    apply (subst positive_integral_density)
   7.789 +    apply (rule distributed_borel_measurable[OF Pxyz])
   7.790 +    apply (rule distributed_AE[OF Pxyz])
   7.791 +    apply (auto simp add: borel_measurable_ereal_iff split_beta' intro!: M) []
   7.792 +    apply (simp add: split_beta')
   7.793 +  proof
   7.794 +    let ?g = "\<lambda>x. ereal (if Pxyz x = 0 then 0 else Pxz (fst x, snd (snd x)) * Pyz (snd x) / Pz (snd (snd x)))"
   7.795 +    assume "(\<integral>\<^isup>+ x. ?g x \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P)) = 0"
   7.796 +    then have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. ?g x \<le> 0"
   7.797 +      by (intro positive_integral_0_iff_AE[THEN iffD1]) (auto intro!: M borel_measurable_ereal measurable_If)
   7.798 +    then have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pxyz x = 0"
   7.799 +      using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
   7.800 +      by eventually_elim (auto split: split_if_asm simp: mult_le_0_iff divide_le_0_iff)
   7.801 +    then have "(\<integral>\<^isup>+ x. ereal (Pxyz x) \<partial>S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) = 0"
   7.802 +      by (subst positive_integral_cong_AE[of _ "\<lambda>x. 0"]) auto
   7.803 +    with P.emeasure_space_1 show False
   7.804 +      by (subst (asm) emeasure_density) (auto intro!: M cong: positive_integral_cong)
   7.805 +  qed
   7.806 +
   7.807 +  have neg: "(\<integral>\<^isup>+ x. - ?f x \<partial>?P) = 0"
   7.808 +    apply (rule positive_integral_0_iff_AE[THEN iffD2])
   7.809 +    apply (auto intro!: M simp: split_beta') []
   7.810 +    apply (subst AE_density)
   7.811 +    apply (auto intro!: M simp: split_beta') []
   7.812 +    using ae5 ae6 ae7 ae8
   7.813 +    apply eventually_elim
   7.814 +    apply (auto intro!: mult_nonneg_nonneg divide_nonneg_nonneg)
   7.815 +    done
   7.816 +
   7.817 +  have I3: "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))))"
   7.818 +    apply (rule integrable_cong_AE[THEN iffD1, OF _ _ _ integral_diff(1)[OF I1 I2]])
   7.819 +    using ae
   7.820 +    apply (auto intro!: M simp: split_beta')
   7.821 +    done
   7.822 +
   7.823 +  have "- log b 1 \<le> - log b (integral\<^isup>L ?P ?f)"
   7.824 +  proof (intro le_imp_neg_le log_le[OF b_gt_1])
   7.825 +    show "0 < integral\<^isup>L ?P ?f"
   7.826 +      using neg pos fin positive_integral_positive[of ?P ?f]
   7.827 +      by (cases "(\<integral>\<^isup>+ x. ?f x \<partial>?P)") (auto simp add: lebesgue_integral_def less_le split_beta')
   7.828 +    show "integral\<^isup>L ?P ?f \<le> 1"
   7.829 +      using neg le1 fin positive_integral_positive[of ?P ?f]
   7.830 +      by (cases "(\<integral>\<^isup>+ x. ?f x \<partial>?P)") (auto simp add: lebesgue_integral_def split_beta' one_ereal_def)
   7.831 +  qed
   7.832 +  also have "- log b (integral\<^isup>L ?P ?f) \<le> (\<integral> x. - log b (?f x) \<partial>?P)"
   7.833 +  proof (rule P.jensens_inequality[where a=0 and b=1 and I="{0<..}"])
   7.834 +    show "AE x in ?P. ?f x \<in> {0<..}"
   7.835 +      unfolding AE_density[OF distributed_borel_measurable[OF Pxyz]]
   7.836 +      using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
   7.837 +      by eventually_elim (auto simp: divide_pos_pos mult_pos_pos)
   7.838 +    show "integrable ?P ?f"
   7.839 +      unfolding integrable_def 
   7.840 +      using fin neg by (auto intro!: M simp: split_beta')
   7.841 +    show "integrable ?P (\<lambda>x. - log b (?f x))"
   7.842 +      apply (subst integral_density)
   7.843 +      apply (auto intro!: M) []
   7.844 +      apply (auto intro!: M distributed_real_AE[OF Pxyz]) []
   7.845 +      apply (auto intro!: M borel_measurable_uminus borel_measurable_log simp: split_beta') []
   7.846 +      apply (rule integrable_cong_AE[THEN iffD1, OF _ _ _ I3])
   7.847 +      apply (auto intro!: M borel_measurable_uminus borel_measurable_log simp: split_beta') []
   7.848 +      apply (auto intro!: M borel_measurable_uminus borel_measurable_log simp: split_beta') []
   7.849 +      using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
   7.850 +      apply eventually_elim
   7.851 +      apply (auto simp: log_divide_eq log_mult_eq zero_le_mult_iff zero_less_mult_iff zero_less_divide_iff field_simps)
   7.852 +      done
   7.853 +  qed (auto simp: b_gt_1 minus_log_convex)
   7.854 +  also have "\<dots> = conditional_mutual_information b S T P X Y Z"
   7.855 +    unfolding `?eq`
   7.856 +    apply (subst integral_density)
   7.857 +    apply (auto intro!: M) []
   7.858 +    apply (auto intro!: M distributed_real_AE[OF Pxyz]) []
   7.859 +    apply (auto intro!: M borel_measurable_uminus borel_measurable_log simp: split_beta') []
   7.860 +    apply (intro integral_cong_AE)
   7.861 +    using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
   7.862 +    apply eventually_elim
   7.863 +    apply (auto simp: log_divide_eq zero_less_mult_iff zero_less_divide_iff field_simps)
   7.864 +    done
   7.865 +  finally show ?nonneg
   7.866 +    by simp
   7.867 +qed
   7.868 +
   7.869 +lemma (in information_space)
   7.870 +  fixes Px :: "_ \<Rightarrow> real"
   7.871 +  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T" and P: "sigma_finite_measure P"
   7.872 +  assumes Fx: "finite_entropy S X Px"
   7.873 +  assumes Fz: "finite_entropy P Z Pz"
   7.874 +  assumes Fyz: "finite_entropy (T \<Otimes>\<^isub>M P) (\<lambda>x. (Y x, Z x)) Pyz"
   7.875 +  assumes Fxz: "finite_entropy (S \<Otimes>\<^isub>M P) (\<lambda>x. (X x, Z x)) Pxz"
   7.876 +  assumes Fxyz: "finite_entropy (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>x. (X x, Y x, Z x)) Pxyz"
   7.877 +  shows conditional_mutual_information_generic_eq': "conditional_mutual_information b S T P X Y Z
   7.878 +    = (\<integral>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))" (is "?eq")
   7.879 +    and conditional_mutual_information_generic_nonneg': "0 \<le> conditional_mutual_information b S T P X Y Z" (is "?nonneg")
   7.880 +proof -
   7.881 +  note Px = Fx[THEN finite_entropy_distributed]
   7.882 +  note Pz = Fz[THEN finite_entropy_distributed]
   7.883 +  note Pyz = Fyz[THEN finite_entropy_distributed]
   7.884 +  note Pxz = Fxz[THEN finite_entropy_distributed]
   7.885 +  note Pxyz = Fxyz[THEN finite_entropy_distributed]
   7.886 +
   7.887 +  interpret S: sigma_finite_measure S by fact
   7.888 +  interpret T: sigma_finite_measure T by fact
   7.889 +  interpret P: sigma_finite_measure P by fact
   7.890 +  interpret TP: pair_sigma_finite T P ..
   7.891 +  interpret SP: pair_sigma_finite S P ..
   7.892 +  interpret ST: pair_sigma_finite S T ..
   7.893 +  interpret SPT: pair_sigma_finite "S \<Otimes>\<^isub>M P" T ..
   7.894 +  interpret STP: pair_sigma_finite S "T \<Otimes>\<^isub>M P" ..
   7.895 +  interpret TPS: pair_sigma_finite "T \<Otimes>\<^isub>M P" S ..
   7.896 +  have TP: "sigma_finite_measure (T \<Otimes>\<^isub>M P)" ..
   7.897 +  have SP: "sigma_finite_measure (S \<Otimes>\<^isub>M P)" ..
   7.898 +  have YZ: "random_variable (T \<Otimes>\<^isub>M P) (\<lambda>x. (Y x, Z x))"
   7.899 +    using Pyz by (simp add: distributed_measurable)
   7.900 +
   7.901 +  have Pxyz_f: "\<And>M f. f \<in> measurable M (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) \<Longrightarrow> (\<lambda>x. Pxyz (f x)) \<in> borel_measurable M"
   7.902 +    using measurable_comp[OF _ Pxyz[THEN distributed_real_measurable]] by (auto simp: comp_def)
   7.903 +
   7.904 +  { fix f g h M
   7.905 +    assume f: "f \<in> measurable M S" and g: "g \<in> measurable M P" and h: "h \<in> measurable M (S \<Otimes>\<^isub>M P)"
   7.906 +    from measurable_comp[OF h Pxz[THEN distributed_real_measurable]]
   7.907 +         measurable_comp[OF f Px[THEN distributed_real_measurable]]
   7.908 +         measurable_comp[OF g Pz[THEN distributed_real_measurable]]
   7.909 +    have "(\<lambda>x. log b (Pxz (h x) / (Px (f x) * Pz (g x)))) \<in> borel_measurable M"
   7.910 +      by (simp add: comp_def b_gt_1) }
   7.911 +  note borel_log = this
   7.912 +
   7.913 +  have measurable_cut: "(\<lambda>(x, y, z). (x, z)) \<in> measurable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (S \<Otimes>\<^isub>M P)"
   7.914 +    by (auto simp add: split_beta' comp_def intro!: measurable_Pair measurable_snd')
   7.915 +  
   7.916 +  from Pxz Pxyz have distr_eq: "distr M (S \<Otimes>\<^isub>M P) (\<lambda>x. (X x, Z x)) =
   7.917 +    distr (distr M (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>x. (X x, Y x, Z x))) (S \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). (x, z))"
   7.918 +    by (subst distr_distr[OF measurable_cut]) (auto dest: distributed_measurable simp: comp_def)
   7.919 +
   7.920 +  have "mutual_information b S P X Z =
   7.921 +    (\<integral>x. Pxz x * log b (Pxz x / (Px (fst x) * Pz (snd x))) \<partial>(S \<Otimes>\<^isub>M P))"
   7.922 +    by (rule mutual_information_distr[OF S P Px Pz Pxz])
   7.923 +  also have "\<dots> = (\<integral>(x,y,z). Pxyz (x,y,z) * log b (Pxz (x,z) / (Px x * Pz z)) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))"
   7.924 +    using b_gt_1 Pxz Px Pz
   7.925 +    by (subst distributed_transform_integral[OF Pxyz Pxz, where T="\<lambda>(x, y, z). (x, z)"])
   7.926 +       (auto simp: split_beta' intro!: measurable_Pair measurable_snd' measurable_snd'' measurable_fst'' borel_measurable_times
   7.927 +             dest!: distributed_real_measurable)
   7.928 +  finally have mi_eq:
   7.929 +    "mutual_information b S P X Z = (\<integral>(x,y,z). Pxyz (x,y,z) * log b (Pxz (x,z) / (Px x * Pz z)) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))" .
   7.930 +  
   7.931 +  have ae1: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Px (fst x) = 0 \<longrightarrow> Pxyz x = 0"
   7.932 +    by (intro subdensity_real[of fst, OF _ Pxyz Px]) auto
   7.933 +  moreover have ae2: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pz (snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
   7.934 +    by (intro subdensity_real[of "\<lambda>x. snd (snd x)", OF _ Pxyz Pz]) (auto intro: measurable_snd')
   7.935 +  moreover have ae3: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pxz (fst x, snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
   7.936 +    by (intro subdensity_real[of "\<lambda>x. (fst x, snd (snd x))", OF _ Pxyz Pxz]) (auto intro: measurable_Pair measurable_snd')
   7.937 +  moreover have ae4: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pyz (snd x) = 0 \<longrightarrow> Pxyz x = 0"
   7.938 +    by (intro subdensity_real[of snd, OF _ Pxyz Pyz]) (auto intro: measurable_Pair)
   7.939 +  moreover have ae5: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Px (fst x)"
   7.940 +    using Px by (intro STP.AE_pair_measure) (auto simp: comp_def intro!: measurable_fst'' dest: distributed_real_AE distributed_real_measurable)
   7.941 +  moreover have ae6: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pyz (snd x)"
   7.942 +    using Pyz by (intro STP.AE_pair_measure) (auto simp: comp_def intro!: measurable_snd'' dest: distributed_real_AE distributed_real_measurable)
   7.943 +  moreover have ae7: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pz (snd (snd x))"
   7.944 +    using Pz Pz[THEN distributed_real_measurable] by (auto intro!: measurable_snd'' TP.AE_pair_measure STP.AE_pair_measure AE_I2[of S] dest: distributed_real_AE)
   7.945 +  moreover have ae8: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. 0 \<le> Pxz (fst x, snd (snd x))"
   7.946 +    using Pxz[THEN distributed_real_AE, THEN SP.AE_pair]
   7.947 +    using measurable_comp[OF measurable_Pair[OF measurable_fst measurable_comp[OF measurable_snd measurable_snd]] Pxz[THEN distributed_real_measurable], of T]
   7.948 +    using measurable_comp[OF measurable_snd measurable_Pair2[OF Pxz[THEN distributed_real_measurable]], of _ T]
   7.949 +    by (auto intro!: TP.AE_pair_measure STP.AE_pair_measure simp: comp_def)
   7.950 +  moreover note ae9 = Pxyz[THEN distributed_real_AE]
   7.951 +  ultimately have ae: "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P.
   7.952 +    Pxyz x * log b (Pxyz x / (Px (fst x) * Pyz (snd x))) -
   7.953 +    Pxyz x * log b (Pxz (fst x, snd (snd x)) / (Px (fst x) * Pz (snd (snd x)))) =
   7.954 +    Pxyz x * log b (Pxyz x * Pz (snd (snd x)) / (Pxz (fst x, snd (snd x)) * Pyz (snd x))) "
   7.955 +  proof eventually_elim
   7.956 +    case (goal1 x)
   7.957 +    show ?case
   7.958 +    proof cases
   7.959 +      assume "Pxyz x \<noteq> 0"
   7.960 +      with goal1 have "0 < Px (fst x)" "0 < Pz (snd (snd x))" "0 < Pxz (fst x, snd (snd x))" "0 < Pyz (snd x)" "0 < Pxyz x"
   7.961 +        by auto
   7.962 +      then show ?thesis
   7.963 +        using b_gt_1 by (simp add: log_simps mult_pos_pos less_imp_le field_simps)
   7.964 +    qed simp
   7.965 +  qed
   7.966 +
   7.967 +  have "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P)
   7.968 +    (\<lambda>x. Pxyz x * log b (Pxyz x) - Pxyz x * log b (Px (fst x)) - Pxyz x * log b (Pyz (snd x)))"
   7.969 +    using finite_entropy_integrable[OF Fxyz]
   7.970 +    using finite_entropy_integrable_transform[OF Fx Pxyz, of fst]
   7.971 +    using finite_entropy_integrable_transform[OF Fyz Pxyz, of snd]
   7.972 +    by simp
   7.973 +  moreover have "(\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Px x * Pyz (y, z)))) \<in> borel_measurable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P)"
   7.974 +    using Pxyz Px Pyz
   7.975 +    by (auto intro!: borel_measurable_times measurable_fst'' measurable_snd'' dest!: distributed_real_measurable simp: split_beta')
   7.976 +  ultimately have I1: "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Px x * Pyz (y, z))))"
   7.977 +    apply (rule integrable_cong_AE_imp)
   7.978 +    using ae1 ae4 ae5 ae6 ae9
   7.979 +    by eventually_elim
   7.980 +       (auto simp: log_divide_eq log_mult_eq mult_nonneg_nonneg field_simps zero_less_mult_iff)
   7.981 +
   7.982 +  have "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P)
   7.983 +    (\<lambda>x. Pxyz x * log b (Pxz (fst x, snd (snd x))) - Pxyz x * log b (Px (fst x)) - Pxyz x * log b (Pz (snd (snd x))))"
   7.984 +    using finite_entropy_integrable_transform[OF Fxz Pxyz, of "\<lambda>x. (fst x, snd (snd x))"]
   7.985 +    using finite_entropy_integrable_transform[OF Fx Pxyz, of fst]
   7.986 +    using finite_entropy_integrable_transform[OF Fz Pxyz, of "snd \<circ> snd"]
   7.987 +    by (simp add: measurable_Pair measurable_snd'' comp_def)
   7.988 +  moreover have "(\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxz (x, z) / (Px x * Pz z))) \<in> borel_measurable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P)"
   7.989 +    using Pxyz Px Pz
   7.990 +    by (auto intro!: measurable_compose[OF _ distributed_real_measurable[OF Pxz]]
   7.991 +                     measurable_Pair borel_measurable_times measurable_fst'' measurable_snd''
   7.992 +             dest!: distributed_real_measurable simp: split_beta')
   7.993 +  ultimately have I2: "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxz (x, z) / (Px x * Pz z)))"
   7.994 +    apply (rule integrable_cong_AE_imp)
   7.995 +    using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 ae9
   7.996 +    by eventually_elim
   7.997 +       (auto simp: log_divide_eq log_mult_eq mult_nonneg_nonneg field_simps zero_less_mult_iff)
   7.998 +
   7.999 +  from ae I1 I2 show ?eq
  7.1000 +    unfolding conditional_mutual_information_def
  7.1001 +    apply (subst mi_eq)
  7.1002 +    apply (subst mutual_information_distr[OF S TP Px Pyz Pxyz])
  7.1003 +    apply (subst integral_diff(2)[symmetric])
  7.1004 +    apply (auto intro!: integral_cong_AE simp: split_beta' simp del: integral_diff)
  7.1005 +    done
  7.1006 +
  7.1007 +  let ?P = "density (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) Pxyz"
  7.1008 +  interpret P: prob_space ?P
  7.1009 +    unfolding distributed_distr_eq_density[OF Pxyz, symmetric]
  7.1010 +    using distributed_measurable[OF Pxyz] by (rule prob_space_distr)
  7.1011 +
  7.1012 +  let ?Q = "density (T \<Otimes>\<^isub>M P) Pyz"
  7.1013 +  interpret Q: prob_space ?Q
  7.1014 +    unfolding distributed_distr_eq_density[OF Pyz, symmetric]
  7.1015 +    using distributed_measurable[OF Pyz] by (rule prob_space_distr)
  7.1016 +
  7.1017 +  let ?f = "\<lambda>(x, y, z). Pxz (x, z) * (Pyz (y, z) / Pz z) / Pxyz (x, y, z)"
  7.1018 +
  7.1019 +  from subdensity_real[of snd, OF _ Pyz Pz]
  7.1020 +  have aeX1: "AE x in T \<Otimes>\<^isub>M P. Pz (snd x) = 0 \<longrightarrow> Pyz x = 0" by (auto simp: comp_def)
  7.1021 +  have aeX2: "AE x in T \<Otimes>\<^isub>M P. 0 \<le> Pz (snd x)"
  7.1022 +    using Pz by (intro TP.AE_pair_measure) (auto simp: comp_def intro!: measurable_snd'' dest: distributed_real_AE distributed_real_measurable)
  7.1023 +
  7.1024 +  have aeX3: "AE y in T \<Otimes>\<^isub>M P. (\<integral>\<^isup>+ x. ereal (Pxz (x, snd y)) \<partial>S) = ereal (Pz (snd y))"
  7.1025 +    using Pz distributed_marginal_eq_joint2[OF P S Pz Pxz]
  7.1026 +    apply (intro TP.AE_pair_measure)
  7.1027 +    apply (auto simp: comp_def measurable_split_conv
  7.1028 +                intro!: measurable_snd'' borel_measurable_ereal_eq borel_measurable_ereal
  7.1029 +                        SP.borel_measurable_positive_integral_snd measurable_compose[OF _ Pxz[THEN distributed_real_measurable]]
  7.1030 +                        measurable_Pair
  7.1031 +                dest: distributed_real_AE distributed_real_measurable)
  7.1032 +    done
  7.1033 +
  7.1034 +  note M = borel_measurable_divide borel_measurable_diff borel_measurable_times borel_measurable_ereal
  7.1035 +           measurable_compose[OF _ measurable_snd]
  7.1036 +           measurable_Pair
  7.1037 +           measurable_compose[OF _ Pxyz[THEN distributed_real_measurable]]
  7.1038 +           measurable_compose[OF _ Pxz[THEN distributed_real_measurable]]
  7.1039 +           measurable_compose[OF _ Pyz[THEN distributed_real_measurable]]
  7.1040 +           measurable_compose[OF _ Pz[THEN distributed_real_measurable]]
  7.1041 +           measurable_compose[OF _ Px[THEN distributed_real_measurable]]
  7.1042 +           STP.borel_measurable_positive_integral_snd
  7.1043 +  have "(\<integral>\<^isup>+ x. ?f x \<partial>?P) \<le> (\<integral>\<^isup>+ (x, y, z). Pxz (x, z) * (Pyz (y, z) / Pz z) \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P))"
  7.1044 +    apply (subst positive_integral_density)
  7.1045 +    apply (rule distributed_borel_measurable[OF Pxyz])
  7.1046 +    apply (rule distributed_AE[OF Pxyz])
  7.1047 +    apply (auto simp add: borel_measurable_ereal_iff split_beta' intro!: M) []
  7.1048 +    apply (rule positive_integral_mono_AE)
  7.1049 +    using ae5 ae6 ae7 ae8
  7.1050 +    apply eventually_elim
  7.1051 +    apply (auto intro!: divide_nonneg_nonneg mult_nonneg_nonneg)
  7.1052 +    done
  7.1053 +  also have "\<dots> = (\<integral>\<^isup>+(y, z). \<integral>\<^isup>+ x. ereal (Pxz (x, z)) * ereal (Pyz (y, z) / Pz z) \<partial>S \<partial>T \<Otimes>\<^isub>M P)"
  7.1054 +    by (subst STP.positive_integral_snd_measurable[symmetric])
  7.1055 +       (auto simp add: borel_measurable_ereal_iff split_beta' intro!: M)
  7.1056 +  also have "\<dots> = (\<integral>\<^isup>+x. ereal (Pyz x) * 1 \<partial>T \<Otimes>\<^isub>M P)"
  7.1057 +    apply (rule positive_integral_cong_AE)
  7.1058 +    using aeX1 aeX2 aeX3 distributed_AE[OF Pyz] AE_space
  7.1059 +    apply eventually_elim
  7.1060 +  proof (case_tac x, simp del: times_ereal.simps add: space_pair_measure)
  7.1061 +    fix a b assume "Pz b = 0 \<longrightarrow> Pyz (a, b) = 0" "0 \<le> Pz b" "a \<in> space T \<and> b \<in> space P"
  7.1062 +      "(\<integral>\<^isup>+ x. ereal (Pxz (x, b)) \<partial>S) = ereal (Pz b)" "0 \<le> Pyz (a, b)" 
  7.1063 +    then show "(\<integral>\<^isup>+ x. ereal (Pxz (x, b)) * ereal (Pyz (a, b) / Pz b) \<partial>S) = ereal (Pyz (a, b))"
  7.1064 +      apply (subst positive_integral_multc)
  7.1065 +      apply (auto intro!: borel_measurable_ereal divide_nonneg_nonneg
  7.1066 +                          measurable_compose[OF _ Pxz[THEN distributed_real_measurable]] measurable_Pair
  7.1067 +                  split: prod.split)
  7.1068 +      done
  7.1069 +  qed
  7.1070 +  also have "\<dots> = 1"
  7.1071 +    using Q.emeasure_space_1 distributed_AE[OF Pyz] distributed_distr_eq_density[OF Pyz]
  7.1072 +    by (subst positive_integral_density[symmetric]) (auto intro!: M)
  7.1073 +  finally have le1: "(\<integral>\<^isup>+ x. ?f x \<partial>?P) \<le> 1" .
  7.1074 +  also have "\<dots> < \<infinity>" by simp
  7.1075 +  finally have fin: "(\<integral>\<^isup>+ x. ?f x \<partial>?P) \<noteq> \<infinity>" by simp
  7.1076 +
  7.1077 +  have pos: "(\<integral>\<^isup>+ x. ?f x \<partial>?P) \<noteq> 0"
  7.1078 +    apply (subst positive_integral_density)
  7.1079 +    apply (rule distributed_borel_measurable[OF Pxyz])
  7.1080 +    apply (rule distributed_AE[OF Pxyz])
  7.1081 +    apply (auto simp add: borel_measurable_ereal_iff split_beta' intro!: M) []
  7.1082 +    apply (simp add: split_beta')
  7.1083 +  proof
  7.1084 +    let ?g = "\<lambda>x. ereal (if Pxyz x = 0 then 0 else Pxz (fst x, snd (snd x)) * Pyz (snd x) / Pz (snd (snd x)))"
  7.1085 +    assume "(\<integral>\<^isup>+ x. ?g x \<partial>(S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P)) = 0"
  7.1086 +    then have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. ?g x \<le> 0"
  7.1087 +      by (intro positive_integral_0_iff_AE[THEN iffD1]) (auto intro!: M borel_measurable_ereal measurable_If)
  7.1088 +    then have "AE x in S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P. Pxyz x = 0"
  7.1089 +      using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
  7.1090 +      by eventually_elim (auto split: split_if_asm simp: mult_le_0_iff divide_le_0_iff)
  7.1091 +    then have "(\<integral>\<^isup>+ x. ereal (Pxyz x) \<partial>S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) = 0"
  7.1092 +      by (subst positive_integral_cong_AE[of _ "\<lambda>x. 0"]) auto
  7.1093 +    with P.emeasure_space_1 show False
  7.1094 +      by (subst (asm) emeasure_density) (auto intro!: M cong: positive_integral_cong)
  7.1095 +  qed
  7.1096 +
  7.1097 +  have neg: "(\<integral>\<^isup>+ x. - ?f x \<partial>?P) = 0"
  7.1098 +    apply (rule positive_integral_0_iff_AE[THEN iffD2])
  7.1099 +    apply (auto intro!: M simp: split_beta') []
  7.1100 +    apply (subst AE_density)
  7.1101 +    apply (auto intro!: M simp: split_beta') []
  7.1102 +    using ae5 ae6 ae7 ae8
  7.1103 +    apply eventually_elim
  7.1104 +    apply (auto intro!: mult_nonneg_nonneg divide_nonneg_nonneg)
  7.1105 +    done
  7.1106 +
  7.1107 +  have I3: "integrable (S \<Otimes>\<^isub>M T \<Otimes>\<^isub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))))"
  7.1108 +    apply (rule integrable_cong_AE[THEN iffD1, OF _ _ _ integral_diff(1)[OF I1 I2]])
  7.1109 +    using ae
  7.1110 +    apply (auto intro!: M simp: split_beta')
  7.1111 +    done
  7.1112 +
  7.1113 +  have "- log b 1 \<le> - log b (integral\<^isup>L ?P ?f)"
  7.1114 +  proof (intro le_imp_neg_le log_le[OF b_gt_1])
  7.1115 +    show "0 < integral\<^isup>L ?P ?f"
  7.1116 +      using neg pos fin positive_integral_positive[of ?P ?f]
  7.1117 +      by (cases "(\<integral>\<^isup>+ x. ?f x \<partial>?P)") (auto simp add: lebesgue_integral_def less_le split_beta')
  7.1118 +    show "integral\<^isup>L ?P ?f \<le> 1"
  7.1119 +      using neg le1 fin positive_integral_positive[of ?P ?f]
  7.1120 +      by (cases "(\<integral>\<^isup>+ x. ?f x \<partial>?P)") (auto simp add: lebesgue_integral_def split_beta' one_ereal_def)
  7.1121 +  qed
  7.1122 +  also have "- log b (integral\<^isup>L ?P ?f) \<le> (\<integral> x. - log b (?f x) \<partial>?P)"
  7.1123 +  proof (rule P.jensens_inequality[where a=0 and b=1 and I="{0<..}"])
  7.1124 +    show "AE x in ?P. ?f x \<in> {0<..}"
  7.1125 +      unfolding AE_density[OF distributed_borel_measurable[OF Pxyz]]
  7.1126 +      using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
  7.1127 +      by eventually_elim (auto simp: divide_pos_pos mult_pos_pos)
  7.1128 +    show "integrable ?P ?f"
  7.1129 +      unfolding integrable_def 
  7.1130 +      using fin neg by (auto intro!: M simp: split_beta')
  7.1131 +    show "integrable ?P (\<lambda>x. - log b (?f x))"
  7.1132 +      apply (subst integral_density)
  7.1133 +      apply (auto intro!: M) []
  7.1134 +      apply (auto intro!: M distributed_real_AE[OF Pxyz]) []
  7.1135 +      apply (auto intro!: M borel_measurable_uminus borel_measurable_log simp: split_beta') []
  7.1136 +      apply (rule integrable_cong_AE[THEN iffD1, OF _ _ _ I3])
  7.1137 +      apply (auto intro!: M borel_measurable_uminus borel_measurable_log simp: split_beta') []
  7.1138 +      apply (auto intro!: M borel_measurable_uminus borel_measurable_log simp: split_beta') []
  7.1139 +      using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
  7.1140 +      apply eventually_elim
  7.1141 +      apply (auto simp: log_divide_eq log_mult_eq zero_le_mult_iff zero_less_mult_iff zero_less_divide_iff field_simps)
  7.1142 +      done
  7.1143 +  qed (auto simp: b_gt_1 minus_log_convex)
  7.1144 +  also have "\<dots> = conditional_mutual_information b S T P X Y Z"
  7.1145 +    unfolding `?eq`
  7.1146 +    apply (subst integral_density)
  7.1147 +    apply (auto intro!: M) []
  7.1148 +    apply (auto intro!: M distributed_real_AE[OF Pxyz]) []
  7.1149 +    apply (auto intro!: M borel_measurable_uminus borel_measurable_log simp: split_beta') []
  7.1150 +    apply (intro integral_cong_AE)
  7.1151 +    using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
  7.1152 +    apply eventually_elim
  7.1153 +    apply (auto simp: log_divide_eq zero_less_mult_iff zero_less_divide_iff field_simps)
  7.1154 +    done
  7.1155 +  finally show ?nonneg
  7.1156 +    by simp
  7.1157  qed
  7.1158  
  7.1159  lemma (in information_space) conditional_mutual_information_eq:
  7.1160 @@ -909,92 +1614,28 @@
  7.1161    assumes X: "simple_function M X" and Y: "simple_function M Y" and Z: "simple_function M Z"
  7.1162    shows "0 \<le> \<I>(X ; Y | Z)"
  7.1163  proof -
  7.1164 -  def Pz \<equiv> "\<lambda>x. if x \<in> Z`space M then measure M (Z -` {x} \<inter> space M) else 0"
  7.1165 -  def Pxz \<equiv> "\<lambda>x. if x \<in> (\<lambda>x. (X x, Z x))`space M then measure M ((\<lambda>x. (X x, Z x)) -` {x} \<inter> space M) else 0"
  7.1166 -  def Pyz \<equiv> "\<lambda>x. if x \<in> (\<lambda>x. (Y x, Z x))`space M then measure M ((\<lambda>x. (Y x, Z x)) -` {x} \<inter> space M) else 0"
  7.1167 -  def Pxyz \<equiv> "\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x, Z x))`space M then measure M ((\<lambda>x. (X x, Y x, Z x)) -` {x} \<inter> space M) else 0"
  7.1168 -  let ?M = "X`space M \<times> Y`space M \<times> Z`space M"
  7.1169 -
  7.1170 -  note XZ = simple_function_Pair[OF X Z]
  7.1171 -  note YZ = simple_function_Pair[OF Y Z]
  7.1172 -  note XYZ = simple_function_Pair[OF X simple_function_Pair[OF Y Z]]
  7.1173 -  have Pz: "simple_distributed M Z Pz"
  7.1174 -    using Z by (rule simple_distributedI) (auto simp: Pz_def)
  7.1175 -  have Pxz: "simple_distributed M (\<lambda>x. (X x, Z x)) Pxz"
  7.1176 -    using XZ by (rule simple_distributedI) (auto simp: Pxz_def)
  7.1177 -  have Pyz: "simple_distributed M (\<lambda>x. (Y x, Z x)) Pyz"
  7.1178 -    using YZ by (rule simple_distributedI) (auto simp: Pyz_def)
  7.1179 -  have Pxyz: "simple_distributed M (\<lambda>x. (X x, Y x, Z x)) Pxyz"
  7.1180 -    using XYZ by (rule simple_distributedI) (auto simp: Pxyz_def)
  7.1181 -
  7.1182 -  { fix z assume z: "z \<in> Z ` space M" then have "(\<Sum>x\<in>X ` space M. Pxz (x, z)) = Pz z"
  7.1183 -      using distributed_marginal_eq_joint_simple[OF X Pz Pxz z]
  7.1184 -      by (auto intro!: setsum_cong simp: Pxz_def) }
  7.1185 -  note marginal1 = this
  7.1186 -
  7.1187 -  { fix z assume z: "z \<in> Z ` space M" then have "(\<Sum>y\<in>Y ` space M. Pyz (y, z)) = Pz z"
  7.1188 -      using distributed_marginal_eq_joint_simple[OF Y Pz Pyz z]
  7.1189 -      by (auto intro!: setsum_cong simp: Pyz_def) }
  7.1190 -  note marginal2 = this
  7.1191 -
  7.1192 -  have "- \<I>(X ; Y | Z) = - (\<Sum>(x, y, z) \<in> ?M. Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))))"
  7.1193 -    unfolding conditional_mutual_information_eq[OF Pz Pyz Pxz Pxyz]
  7.1194 -    using X Y Z by (auto intro!: setsum_mono_zero_left simp: Pxyz_def simple_functionD)
  7.1195 -  also have "\<dots> \<le> log b (\<Sum>(x, y, z) \<in> ?M. Pxz (x, z) * (Pyz (y,z) / Pz z))"
  7.1196 -    unfolding split_beta'
  7.1197 -  proof (rule log_setsum_divide)
  7.1198 -    show "?M \<noteq> {}" using not_empty by simp
  7.1199 -    show "1 < b" using b_gt_1 .
  7.1200 -
  7.1201 -    show "finite ?M" using X Y Z by (auto simp: simple_functionD)
  7.1202 -
  7.1203 -    then show "(\<Sum>x\<in>?M. Pxyz (fst x, fst (snd x), snd (snd x))) = 1"
  7.1204 -      apply (subst Pxyz[THEN simple_distributed_setsum_space, symmetric])
  7.1205 -      apply simp
  7.1206 -      apply (intro setsum_mono_zero_right)
  7.1207 -      apply (auto simp: Pxyz_def)
  7.1208 -      done
  7.1209 -    let ?N = "(\<lambda>x. (X x, Y x, Z x)) ` space M"
  7.1210 -    fix x assume x: "x \<in> ?M"
  7.1211 -    let ?Q = "Pxyz (fst x, fst (snd x), snd (snd x))"
  7.1212 -    let ?P = "Pxz (fst x, snd (snd x)) * (Pyz (fst (snd x), snd (snd x)) / Pz (snd (snd x)))"
  7.1213 -    from x show "0 \<le> ?Q" "0 \<le> ?P"
  7.1214 -      using Pxyz[THEN simple_distributed, THEN distributed_real_AE]
  7.1215 -      using Pxz[THEN simple_distributed, THEN distributed_real_AE]
  7.1216 -      using Pyz[THEN simple_distributed, THEN distributed_real_AE]
  7.1217 -      using Pz[THEN simple_distributed, THEN distributed_real_AE]
  7.1218 -      by (auto intro!: mult_nonneg_nonneg divide_nonneg_nonneg simp: AE_count_space Pxyz_def Pxz_def Pyz_def Pz_def)
  7.1219 -    moreover assume "0 < ?Q"
  7.1220 -    moreover have "AE x in count_space ?N. Pz (snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
  7.1221 -      by (intro subdensity_real[of "\<lambda>x. snd (snd x)", OF _ Pxyz[THEN simple_distributed] Pz[THEN simple_distributed]]) (auto intro: measurable_snd')
  7.1222 -    then have "\<And>x. Pz (snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
  7.1223 -      by (auto simp: Pz_def Pxyz_def AE_count_space)
  7.1224 -    moreover have "AE x in count_space ?N. Pxz (fst x, snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
  7.1225 -      by (intro subdensity_real[of "\<lambda>x. (fst x, snd (snd x))", OF _ Pxyz[THEN simple_distributed] Pxz[THEN simple_distributed]]) (auto intro: measurable_Pair measurable_snd')
  7.1226 -    then have "\<And>x. Pxz (fst x, snd (snd x)) = 0 \<longrightarrow> Pxyz x = 0"
  7.1227 -      by (auto simp: Pz_def Pxyz_def AE_count_space)
  7.1228 -    moreover have "AE x in count_space ?N. Pyz (snd x) = 0 \<longrightarrow> Pxyz x = 0"
  7.1229 -      by (intro subdensity_real[of snd, OF _ Pxyz[THEN simple_distributed] Pyz[THEN simple_distributed]]) (auto intro: measurable_Pair)
  7.1230 -    then have "\<And>x. Pyz (snd x) = 0 \<longrightarrow> Pxyz x = 0"
  7.1231 -      by (auto simp: Pz_def Pxyz_def AE_count_space)
  7.1232 -    ultimately show "0 < ?P" using x by (auto intro!: divide_pos_pos mult_pos_pos simp: less_le)
  7.1233 -  qed
  7.1234 -  also have "(\<Sum>(x, y, z) \<in> ?M. Pxz (x, z) * (Pyz (y,z) / Pz z)) = (\<Sum>z\<in>Z`space M. Pz z)"
  7.1235 -    apply (simp add: setsum_cartesian_product')
  7.1236 -    apply (subst setsum_commute)
  7.1237 -    apply (subst (2) setsum_commute)
  7.1238 -    apply (auto simp: setsum_divide_distrib[symmetric] setsum_product[symmetric] marginal1 marginal2
  7.1239 -          intro!: setsum_cong)
  7.1240 -    done
  7.1241 -  also have "log b (\<Sum>z\<in>Z`space M. Pz z) = 0"
  7.1242 -    using Pz[THEN simple_distributed_setsum_space] by simp
  7.1243 -  finally show ?thesis by simp
  7.1244 +  have [simp]: "count_space (X ` space M) \<Otimes>\<^isub>M count_space (Y ` space M) \<Otimes>\<^isub>M count_space (Z ` space M) =
  7.1245 +      count_space (X`space M \<times> Y`space M \<times> Z`space M)"
  7.1246 +    by (simp add: pair_measure_count_space X Y Z simple_functionD)
  7.1247 +  note sf = sigma_finite_measure_count_space_finite[OF simple_functionD(1)]
  7.1248 +  note sd = simple_distributedI[OF _ refl]
  7.1249 +  note sp = simple_function_Pair
  7.1250 +  show ?thesis
  7.1251 +   apply (rule conditional_mutual_information_generic_nonneg[OF sf[OF X] sf[OF Y] sf[OF Z]])
  7.1252 +   apply (rule simple_distributed[OF sd[OF X]])
  7.1253 +   apply (rule simple_distributed[OF sd[OF Z]])
  7.1254 +   apply (rule simple_distributed_joint[OF sd[OF sp[OF Y Z]]])
  7.1255 +   apply (rule simple_distributed_joint[OF sd[OF sp[OF X Z]]])
  7.1256 +   apply (rule simple_distributed_joint2[OF sd[OF sp[OF X sp[OF Y Z]]]])
  7.1257 +   apply (auto intro!: integrable_count_space simp: X Y Z simple_functionD)
  7.1258 +   done
  7.1259  qed
  7.1260  
  7.1261  subsection {* Conditional Entropy *}
  7.1262  
  7.1263  definition (in prob_space)
  7.1264 -  "conditional_entropy b S T X Y = entropy b (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) - entropy b T Y"
  7.1265 +  "conditional_entropy b S T X Y = - (\<integral>(x, y). log b (real (RN_deriv (S \<Otimes>\<^isub>M T) (distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))) (x, y)) / 
  7.1266 +    real (RN_deriv T (distr M T Y) y)) \<partial>distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)))"
  7.1267  
  7.1268  abbreviation (in information_space)
  7.1269    conditional_entropy_Pow ("\<H>'(_ | _')") where
  7.1270 @@ -1003,66 +1644,112 @@
  7.1271  lemma (in information_space) conditional_entropy_generic_eq:
  7.1272    fixes Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
  7.1273    assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
  7.1274 -  assumes Px: "distributed M S X Px"
  7.1275    assumes Py: "distributed M T Y Py"
  7.1276    assumes Pxy: "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  7.1277 -  assumes I1: "integrable (S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Pxy x))"
  7.1278 -  assumes I2: "integrable (S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Py (snd x)))"
  7.1279    shows "conditional_entropy b S T X Y = - (\<integral>(x, y). Pxy (x, y) * log b (Pxy (x, y) / Py y) \<partial>(S \<Otimes>\<^isub>M T))"
  7.1280  proof -
  7.1281    interpret S: sigma_finite_measure S by fact
  7.1282    interpret T: sigma_finite_measure T by fact
  7.1283    interpret ST: pair_sigma_finite S T ..
  7.1284 -  have ST: "sigma_finite_measure (S \<Otimes>\<^isub>M T)" ..
  7.1285  
  7.1286 -  interpret Pxy: prob_space "density (S \<Otimes>\<^isub>M T) Pxy"
  7.1287 -    unfolding Pxy[THEN distributed_distr_eq_density, symmetric]
  7.1288 -    using Pxy[THEN distributed_measurable] by (rule prob_space_distr)
  7.1289 +  have "AE x in density (S \<Otimes>\<^isub>M T) (\<lambda>x. ereal (Pxy x)). Pxy x = real (RN_deriv (S \<Otimes>\<^isub>M T) (distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))) x)"
  7.1290 +    unfolding AE_density[OF distributed_borel_measurable, OF Pxy]
  7.1291 +    unfolding distributed_distr_eq_density[OF Pxy]
  7.1292 +    using distributed_RN_deriv[OF Pxy]
  7.1293 +    by auto
  7.1294 +  moreover
  7.1295 +  have "AE x in density (S \<Otimes>\<^isub>M T) (\<lambda>x. ereal (Pxy x)). Py (snd x) = real (RN_deriv T (distr M T Y) (snd x))"
  7.1296 +    unfolding AE_density[OF distributed_borel_measurable, OF Pxy]
  7.1297 +    unfolding distributed_distr_eq_density[OF Py]
  7.1298 +    apply (rule ST.AE_pair_measure)
  7.1299 +    apply (auto intro!: sets_Collect borel_measurable_eq measurable_compose[OF _ distributed_real_measurable[OF Py]]
  7.1300 +                        distributed_real_measurable[OF Pxy] distributed_real_AE[OF Py]
  7.1301 +                        borel_measurable_real_of_ereal measurable_compose[OF _ borel_measurable_RN_deriv_density])
  7.1302 +    using distributed_RN_deriv[OF Py]
  7.1303 +    apply auto
  7.1304 +    done    
  7.1305 +  ultimately
  7.1306 +  have "conditional_entropy b S T X Y = - (\<integral>x. Pxy x * log b (Pxy x / Py (snd x)) \<partial>(S \<Otimes>\<^isub>M T))"
  7.1307 +    unfolding conditional_entropy_def neg_equal_iff_equal
  7.1308 +    apply (subst integral_density(1)[symmetric])
  7.1309 +    apply (auto simp: distributed_real_measurable[OF Pxy] distributed_real_AE[OF Pxy]
  7.1310 +                      measurable_compose[OF _ distributed_real_measurable[OF Py]]
  7.1311 +                      distributed_distr_eq_density[OF Pxy]
  7.1312 +                intro!: integral_cong_AE)
  7.1313 +    done
  7.1314 +  then show ?thesis by (simp add: split_beta')
  7.1315 +qed
  7.1316  
  7.1317 -  from Py Pxy have distr_eq: "distr M T Y =
  7.1318 -    distr (distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))) T snd"
  7.1319 -    by (subst distr_distr[OF measurable_snd]) (auto dest: distributed_measurable simp: comp_def)
  7.1320 +lemma (in information_space) conditional_entropy_eq_entropy:
  7.1321 +  fixes Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
  7.1322 +  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
  7.1323 +  assumes Py: "distributed M T Y Py"
  7.1324 +  assumes Pxy: "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  7.1325 +  assumes I1: "integrable (S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Pxy x))"
  7.1326 +  assumes I2: "integrable (S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Py (snd x)))"
  7.1327 +  shows "conditional_entropy b S T X Y = entropy b (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) - entropy b T Y"
  7.1328 +proof -
  7.1329 +  interpret S: sigma_finite_measure S by fact
  7.1330 +  interpret T: sigma_finite_measure T by fact
  7.1331 +  interpret ST: pair_sigma_finite S T ..
  7.1332  
  7.1333    have "entropy b T Y = - (\<integral>y. Py y * log b (Py y) \<partial>T)"
  7.1334 -    by (rule entropy_distr[OF T Py])
  7.1335 +    by (rule entropy_distr[OF Py])
  7.1336    also have "\<dots> = - (\<integral>(x,y). Pxy (x,y) * log b (Py y) \<partial>(S \<Otimes>\<^isub>M T))"
  7.1337      using b_gt_1 Py[THEN distributed_real_measurable]
  7.1338      by (subst distributed_transform_integral[OF Pxy Py, where T=snd]) (auto intro!: integral_cong)
  7.1339    finally have e_eq: "entropy b T Y = - (\<integral>(x,y). Pxy (x,y) * log b (Py y) \<partial>(S \<Otimes>\<^isub>M T))" .
  7.1340 -  
  7.1341 -  have "AE x in S \<Otimes>\<^isub>M T. Px (fst x) = 0 \<longrightarrow> Pxy x = 0"
  7.1342 -    by (intro subdensity_real[of fst, OF _ Pxy Px]) (auto intro: measurable_Pair)
  7.1343 -  moreover have "AE x in S \<Otimes>\<^isub>M T. Py (snd x) = 0 \<longrightarrow> Pxy x = 0"
  7.1344 +
  7.1345 +  have ae2: "AE x in S \<Otimes>\<^isub>M T. Py (snd x) = 0 \<longrightarrow> Pxy x = 0"
  7.1346      by (intro subdensity_real[of snd, OF _ Pxy Py]) (auto intro: measurable_Pair)
  7.1347 -  moreover have "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Px (fst x)"
  7.1348 -    using Px by (intro ST.AE_pair_measure) (auto simp: comp_def intro!: measurable_fst'' dest: distributed_real_AE distributed_real_measurable)
  7.1349 -  moreover have "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Py (snd x)"
  7.1350 +  moreover have ae4: "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Py (snd x)"
  7.1351      using Py by (intro ST.AE_pair_measure) (auto simp: comp_def intro!: measurable_snd'' dest: distributed_real_AE distributed_real_measurable)
  7.1352 -  moreover note Pxy[THEN distributed_real_AE]
  7.1353 -  ultimately have pos: "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Pxy x \<and> 0 \<le> Px (fst x) \<and> 0 \<le> Py (snd x) \<and>
  7.1354 -    (Pxy x = 0 \<or> (Pxy x \<noteq> 0 \<longrightarrow> 0 < Pxy x \<and> 0 < Px (fst x) \<and> 0 < Py (snd x)))"
  7.1355 +  moreover note ae5 = Pxy[THEN distributed_real_AE]
  7.1356 +  ultimately have "AE x in S \<Otimes>\<^isub>M T. 0 \<le> Pxy x \<and> 0 \<le> Py (snd x) \<and>
  7.1357 +    (Pxy x = 0 \<or> (Pxy x \<noteq> 0 \<longrightarrow> 0 < Pxy x \<and> 0 < Py (snd x)))"
  7.1358      by eventually_elim auto
  7.1359 -
  7.1360 -  from pos have "AE x in S \<Otimes>\<^isub>M T.
  7.1361 +  then have ae: "AE x in S \<Otimes>\<^isub>M T.
  7.1362       Pxy x * log b (Pxy x) - Pxy x * log b (Py (snd x)) = Pxy x * log b (Pxy x / Py (snd x))"
  7.1363      by eventually_elim (auto simp: log_simps mult_pos_pos field_simps b_gt_1)
  7.1364 -  with I1 I2 show ?thesis
  7.1365 -    unfolding conditional_entropy_def
  7.1366 -    apply (subst e_eq)
  7.1367 -    apply (subst entropy_distr[OF ST Pxy])
  7.1368 -    unfolding minus_diff_minus
  7.1369 -    apply (subst integral_diff(2)[symmetric])
  7.1370 -    apply (auto intro!: integral_cong_AE simp: split_beta' simp del: integral_diff)
  7.1371 +  have "conditional_entropy b S T X Y = 
  7.1372 +    - (\<integral>x. Pxy x * log b (Pxy x) - Pxy x * log b (Py (snd x)) \<partial>(S \<Otimes>\<^isub>M T))"
  7.1373 +    unfolding conditional_entropy_generic_eq[OF S T Py Pxy] neg_equal_iff_equal
  7.1374 +    apply (intro integral_cong_AE)
  7.1375 +    using ae
  7.1376 +    apply eventually_elim
  7.1377 +    apply auto
  7.1378      done
  7.1379 +  also have "\<dots> = - (\<integral>x. Pxy x * log b (Pxy x) \<partial>(S \<Otimes>\<^isub>M T)) - - (\<integral>x.  Pxy x * log b (Py (snd x)) \<partial>(S \<Otimes>\<^isub>M T))"
  7.1380 +    by (simp add: integral_diff[OF I1 I2])
  7.1381 +  finally show ?thesis 
  7.1382 +    unfolding conditional_entropy_generic_eq[OF S T Py Pxy] entropy_distr[OF Pxy] e_eq
  7.1383 +    by (simp add: split_beta')
  7.1384 +qed
  7.1385 +
  7.1386 +lemma (in information_space) conditional_entropy_eq_entropy_simple:
  7.1387 +  assumes X: "simple_function M X" and Y: "simple_function M Y"
  7.1388 +  shows "\<H>(X | Y) = entropy b (count_space (X`space M) \<Otimes>\<^isub>M count_space (Y`space M)) (\<lambda>x. (X x, Y x)) - \<H>(Y)"
  7.1389 +proof -
  7.1390 +  have "count_space (X ` space M) \<Otimes>\<^isub>M count_space (Y ` space M) = count_space (X`space M \<times> Y`space M)"
  7.1391 +    (is "?P = ?C") using X Y by (simp add: simple_functionD pair_measure_count_space)
  7.1392 +  show ?thesis
  7.1393 +    by (rule conditional_entropy_eq_entropy sigma_finite_measure_count_space_finite
  7.1394 +                 simple_functionD  X Y simple_distributed simple_distributedI[OF _ refl]
  7.1395 +                 simple_distributed_joint simple_function_Pair integrable_count_space)+
  7.1396 +       (auto simp: `?P = ?C` intro!: integrable_count_space simple_functionD  X Y)
  7.1397  qed
  7.1398  
  7.1399  lemma (in information_space) conditional_entropy_eq:
  7.1400 -  assumes Y: "simple_distributed M Y Py" and X: "simple_function M X"
  7.1401 +  assumes Y: "simple_distributed M Y Py"
  7.1402    assumes XY: "simple_distributed M (\<lambda>x. (X x, Y x)) Pxy"
  7.1403      shows "\<H>(X | Y) = - (\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` space M. Pxy (x, y) * log b (Pxy (x, y) / Py y))"
  7.1404  proof (subst conditional_entropy_generic_eq[OF _ _
  7.1405 -  simple_distributed[OF simple_distributedI[OF X refl]] simple_distributed[OF Y] simple_distributed_joint[OF XY]])
  7.1406 -  have [simp]: "finite (X`space M)" using X by (simp add: simple_function_def)
  7.1407 +  simple_distributed[OF Y] simple_distributed_joint[OF XY]])
  7.1408 +  have "finite ((\<lambda>x. (X x, Y x))`space M)"
  7.1409 +    using XY unfolding simple_distributed_def by auto
  7.1410 +  from finite_imageI[OF this, of fst]
  7.1411 +  have [simp]: "finite (X`space M)"
  7.1412 +    by (simp add: image_compose[symmetric] comp_def)
  7.1413    note Y[THEN simple_distributed_finite, simp]
  7.1414    show "sigma_finite_measure (count_space (X ` space M))"
  7.1415      by (simp add: sigma_finite_measure_count_space_finite)
  7.1416 @@ -1071,15 +1758,11 @@
  7.1417    let ?f = "(\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x)) ` space M then Pxy x else 0)"
  7.1418    have "count_space (X ` space M) \<Otimes>\<^isub>M count_space (Y ` space M) = count_space (X`space M \<times> Y`space M)"
  7.1419      (is "?P = ?C")
  7.1420 -    using X Y by (simp add: simple_distributed_finite pair_measure_count_space)
  7.1421 -  with X Y show
  7.1422 -      "integrable ?P (\<lambda>x. ?f x * log b (?f x))"
  7.1423 -      "integrable ?P (\<lambda>x. ?f x * log b (Py (snd x)))"
  7.1424 -    by (auto intro!: integrable_count_space simp: simple_distributed_finite)
  7.1425 +    using Y by (simp add: simple_distributed_finite pair_measure_count_space)
  7.1426    have eq: "(\<lambda>(x, y). ?f (x, y) * log b (?f (x, y) / Py y)) =
  7.1427      (\<lambda>x. if x \<in> (\<lambda>x. (X x, Y x)) ` space M then Pxy x * log b (Pxy x / Py (snd x)) else 0)"
  7.1428      by auto
  7.1429 -  from X Y show "- (\<integral> (x, y). ?f (x, y) * log b (?f (x, y) / Py y) \<partial>?P) =
  7.1430 +  from Y show "- (\<integral> (x, y). ?f (x, y) * log b (?f (x, y) / Py y) \<partial>?P) =
  7.1431      - (\<Sum>(x, y)\<in>(\<lambda>x. (X x, Y x)) ` space M. Pxy (x, y) * log b (Pxy (x, y) / Py y))"
  7.1432      by (auto intro!: setsum_cong simp add: `?P = ?C` lebesgue_integral_count_space_finite simple_distributed_finite eq setsum_cases split_beta')
  7.1433  qed
  7.1434 @@ -1111,11 +1794,11 @@
  7.1435      by (intro subdensity_real[of snd, OF _ Pxy[THEN simple_distributed] Py[THEN simple_distributed]]) (auto intro: measurable_Pair)
  7.1436    then show ?thesis
  7.1437      apply (subst conditional_mutual_information_eq[OF Py Pxy Pxy Pxxy])
  7.1438 -    apply (subst conditional_entropy_eq[OF Py X Pxy])
  7.1439 +    apply (subst conditional_entropy_eq[OF Py Pxy])
  7.1440      apply (auto intro!: setsum_cong simp: Pxxy_eq setsum_negf[symmetric] eq setsum_reindex[OF inj]
  7.1441                  log_simps zero_less_mult_iff zero_le_mult_iff field_simps mult_less_0_iff AE_count_space)
  7.1442      using Py[THEN simple_distributed, THEN distributed_real_AE] Pxy[THEN simple_distributed, THEN distributed_real_AE]
  7.1443 -    apply (auto simp add: not_le[symmetric] AE_count_space)
  7.1444 +  apply (auto simp add: not_le[symmetric] AE_count_space)
  7.1445      done
  7.1446  qed
  7.1447  
  7.1448 @@ -1138,14 +1821,14 @@
  7.1449  proof -
  7.1450    have X: "entropy b S X = - (\<integral>x. Pxy x * log b (Px (fst x)) \<partial>(S \<Otimes>\<^isub>M T))"
  7.1451      using b_gt_1 Px[THEN distributed_real_measurable]
  7.1452 -    apply (subst entropy_distr[OF S Px])
  7.1453 +    apply (subst entropy_distr[OF Px])
  7.1454      apply (subst distributed_transform_integral[OF Pxy Px, where T=fst])
  7.1455      apply (auto intro!: integral_cong)
  7.1456      done
  7.1457  
  7.1458    have Y: "entropy b T Y = - (\<integral>x. Pxy x * log b (Py (snd x)) \<partial>(S \<Otimes>\<^isub>M T))"
  7.1459      using b_gt_1 Py[THEN distributed_real_measurable]
  7.1460 -    apply (subst entropy_distr[OF T Py])
  7.1461 +    apply (subst entropy_distr[OF Py])
  7.1462      apply (subst distributed_transform_integral[OF Pxy Py, where T=snd])
  7.1463      apply (auto intro!: integral_cong)
  7.1464      done
  7.1465 @@ -1156,7 +1839,7 @@
  7.1466    have ST: "sigma_finite_measure (S \<Otimes>\<^isub>M T)" ..
  7.1467  
  7.1468    have XY: "entropy b (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) = - (\<integral>x. Pxy x * log b (Pxy x) \<partial>(S \<Otimes>\<^isub>M T))"
  7.1469 -    by (subst entropy_distr[OF ST Pxy]) (auto intro!: integral_cong)
  7.1470 +    by (subst entropy_distr[OF Pxy]) (auto intro!: integral_cong)
  7.1471    
  7.1472    have "AE x in S \<Otimes>\<^isub>M T. Px (fst x) = 0 \<longrightarrow> Pxy x = 0"
  7.1473      by (intro subdensity_real[of fst, OF _ Pxy Px]) (auto intro: measurable_Pair)
  7.1474 @@ -1197,6 +1880,20 @@
  7.1475    finally show ?thesis ..
  7.1476  qed
  7.1477  
  7.1478 +lemma (in information_space) mutual_information_eq_entropy_conditional_entropy':
  7.1479 +  fixes Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real" and Pxy :: "('b \<times> 'c) \<Rightarrow> real"
  7.1480 +  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
  7.1481 +  assumes Px: "distributed M S X Px" and Py: "distributed M T Y Py"
  7.1482 +  assumes Pxy: "distributed M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  7.1483 +  assumes Ix: "integrable(S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Px (fst x)))"
  7.1484 +  assumes Iy: "integrable(S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Py (snd x)))"
  7.1485 +  assumes Ixy: "integrable(S \<Otimes>\<^isub>M T) (\<lambda>x. Pxy x * log b (Pxy x))"
  7.1486 +  shows  "mutual_information b S T X Y = entropy b S X - conditional_entropy b S T X Y"
  7.1487 +  using
  7.1488 +    mutual_information_eq_entropy_conditional_entropy_distr[OF S T Px Py Pxy Ix Iy Ixy]
  7.1489 +    conditional_entropy_eq_entropy[OF S T Py Pxy Ixy Iy]
  7.1490 +  by simp
  7.1491 +
  7.1492  lemma (in information_space) mutual_information_eq_entropy_conditional_entropy:
  7.1493    assumes sf_X: "simple_function M X" and sf_Y: "simple_function M Y"
  7.1494    shows  "\<I>(X ; Y) = \<H>(X) - \<H>(X | Y)"
  7.1495 @@ -1217,7 +1914,7 @@
  7.1496      using sigma_finite_measure_count_space_finite sigma_finite_measure_count_space_finite simple_distributed[OF X] simple_distributed[OF Y] simple_distributed_joint[OF XY]
  7.1497      by (rule mutual_information_eq_entropy_conditional_entropy_distr) (auto simp: eq integrable_count_space)
  7.1498    then show ?thesis
  7.1499 -    unfolding conditional_entropy_def by simp
  7.1500 +    unfolding conditional_entropy_eq_entropy_simple[OF sf_X sf_Y] by simp
  7.1501  qed
  7.1502  
  7.1503  lemma (in information_space) mutual_information_nonneg_simple:
  7.1504 @@ -1252,6 +1949,25 @@
  7.1505    finally show ?thesis by auto
  7.1506  qed
  7.1507  
  7.1508 +lemma (in information_space) 
  7.1509 +  fixes Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real" and Pxy :: "('b \<times> 'c) \<Rightarrow> real"
  7.1510 +  assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
  7.1511 +  assumes Px: "finite_entropy S X Px" and Py: "finite_entropy T Y Py"
  7.1512 +  assumes Pxy: "finite_entropy (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  7.1513 +  shows "conditional_entropy b S T X Y \<le> entropy b S X"
  7.1514 +proof -
  7.1515 +
  7.1516 +  have "0 \<le> mutual_information b S T X Y" 
  7.1517 +    by (rule mutual_information_nonneg') fact+
  7.1518 +  also have "\<dots> = entropy b S X - conditional_entropy b S T X Y"
  7.1519 +    apply (rule mutual_information_eq_entropy_conditional_entropy')
  7.1520 +    using assms
  7.1521 +    by (auto intro!: finite_entropy_integrable finite_entropy_distributed
  7.1522 +      finite_entropy_integrable_transform[OF Px]
  7.1523 +      finite_entropy_integrable_transform[OF Py])
  7.1524 +  finally show ?thesis by auto
  7.1525 +qed
  7.1526 +
  7.1527  lemma (in information_space) entropy_chain_rule:
  7.1528    assumes X: "simple_function M X" and Y: "simple_function M Y"
  7.1529    shows  "\<H>(\<lambda>x. (X x, Y x)) = \<H>(X) + \<H>(Y|X)"
  7.1530 @@ -1269,12 +1985,12 @@
  7.1531    also have "\<dots> = - (\<Sum>x\<in>(\<lambda>x. (Y x, X x)) ` space M. ?h x * log b (?h x))"
  7.1532      by (auto intro!: setsum_cong)
  7.1533    also have "\<dots> = entropy b (count_space (Y ` space M) \<Otimes>\<^isub>M count_space (X ` space M)) (\<lambda>x. (Y x, X x))"
  7.1534 -    by (subst entropy_distr[OF _ simple_distributed_joint[OF YX]])
  7.1535 +    by (subst entropy_distr[OF simple_distributed_joint[OF YX]])
  7.1536         (auto simp: pair_measure_count_space sigma_finite_measure_count_space_finite lebesgue_integral_count_space_finite
  7.1537               cong del: setsum_cong  intro!: setsum_mono_zero_left)
  7.1538    finally have "\<H>(\<lambda>x. (X x, Y x)) = entropy b (count_space (Y ` space M) \<Otimes>\<^isub>M count_space (X ` space M)) (\<lambda>x. (Y x, X x))" .
  7.1539    then show ?thesis
  7.1540 -    unfolding conditional_entropy_def by simp
  7.1541 +    unfolding conditional_entropy_eq_entropy_simple[OF Y X] by simp
  7.1542  qed
  7.1543  
  7.1544  lemma (in information_space) entropy_partition:
     8.1 --- a/src/HOL/Probability/Lebesgue_Integration.thy	Wed Oct 10 15:16:44 2012 +0200
     8.2 +++ b/src/HOL/Probability/Lebesgue_Integration.thy	Wed Oct 10 15:17:18 2012 +0200
     8.3 @@ -355,6 +355,98 @@
     8.4      "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
     8.5    using borel_measurable_implies_simple_function_sequence[OF u] by auto
     8.6  
     8.7 +lemma simple_function_induct[consumes 1, case_names cong set mult add, induct set: simple_function]:
     8.8 +  fixes u :: "'a \<Rightarrow> ereal"
     8.9 +  assumes u: "simple_function M u"
    8.10 +  assumes cong: "\<And>f g. simple_function M f \<Longrightarrow> simple_function M g \<Longrightarrow> (AE x in M. f x = g x) \<Longrightarrow> P f \<Longrightarrow> P g"
    8.11 +  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
    8.12 +  assumes mult: "\<And>u c. P u \<Longrightarrow> P (\<lambda>x. c * u x)"
    8.13 +  assumes add: "\<And>u v. P u \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
    8.14 +  shows "P u"
    8.15 +proof (rule cong)
    8.16 +  from AE_space show "AE x in M. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x"
    8.17 +  proof eventually_elim
    8.18 +    fix x assume x: "x \<in> space M"
    8.19 +    from simple_function_indicator_representation[OF u x]
    8.20 +    show "(\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x" ..
    8.21 +  qed
    8.22 +next
    8.23 +  from u have "finite (u ` space M)"
    8.24 +    unfolding simple_function_def by auto
    8.25 +  then show "P (\<lambda>x. \<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x)"
    8.26 +  proof induct
    8.27 +    case empty show ?case
    8.28 +      using set[of "{}"] by (simp add: indicator_def[abs_def])
    8.29 +  qed (auto intro!: add mult set simple_functionD u)
    8.30 +next
    8.31 +  show "simple_function M (\<lambda>x. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x))"
    8.32 +    apply (subst simple_function_cong)
    8.33 +    apply (rule simple_function_indicator_representation[symmetric])
    8.34 +    apply (auto intro: u)
    8.35 +    done
    8.36 +qed fact
    8.37 +
    8.38 +lemma simple_function_induct_nn[consumes 2, case_names cong set mult add]:
    8.39 +  fixes u :: "'a \<Rightarrow> ereal"
    8.40 +  assumes u: "simple_function M u" and nn: "\<And>x. 0 \<le> u x"
    8.41 +  assumes cong: "\<And>f g. simple_function M f \<Longrightarrow> simple_function M g \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> P f \<Longrightarrow> P g"
    8.42 +  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
    8.43 +  assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> simple_function M u \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
    8.44 +  assumes add: "\<And>u v. simple_function M u \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> simple_function M v \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
    8.45 +  shows "P u"
    8.46 +proof -
    8.47 +  show ?thesis
    8.48 +  proof (rule cong)
    8.49 +    fix x assume x: "x \<in> space M"
    8.50 +    from simple_function_indicator_representation[OF u x]
    8.51 +    show "(\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x" ..
    8.52 +  next
    8.53 +    show "simple_function M (\<lambda>x. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x))"
    8.54 +      apply (subst simple_function_cong)
    8.55 +      apply (rule simple_function_indicator_representation[symmetric])
    8.56 +      apply (auto intro: u)
    8.57 +      done
    8.58 +  next
    8.59 +    from u nn have "finite (u ` space M)" "\<And>x. x \<in> u ` space M \<Longrightarrow> 0 \<le> x"
    8.60 +      unfolding simple_function_def by auto
    8.61 +    then show "P (\<lambda>x. \<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x)"
    8.62 +    proof induct
    8.63 +      case empty show ?case
    8.64 +        using set[of "{}"] by (simp add: indicator_def[abs_def])
    8.65 +    qed (auto intro!: add mult set simple_functionD u setsum_nonneg
    8.66 +       simple_function_setsum)
    8.67 +  qed fact
    8.68 +qed
    8.69 +
    8.70 +lemma borel_measurable_induct[consumes 2, case_names cong set mult add seq, induct set: borel_measurable]:
    8.71 +  fixes u :: "'a \<Rightarrow> ereal"
    8.72 +  assumes u: "u \<in> borel_measurable M" "\<And>x. 0 \<le> u x"
    8.73 +  assumes cong: "\<And>f g. f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> P g \<Longrightarrow> P f"
    8.74 +  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
    8.75 +  assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
    8.76 +  assumes add: "\<And>u v. u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> v \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
    8.77 +  assumes seq: "\<And>U. (\<And>i. U i \<in> borel_measurable M) \<Longrightarrow>  (\<And>i x. 0 \<le> U i x) \<Longrightarrow>  (\<And>i. P (U i)) \<Longrightarrow> incseq U \<Longrightarrow> P (SUP i. U i)"
    8.78 +  shows "P u"
    8.79 +  using u
    8.80 +proof (induct rule: borel_measurable_implies_simple_function_sequence')
    8.81 +  fix U assume U: "\<And>i. simple_function M (U i)" "incseq U" "\<And>i. \<infinity> \<notin> range (U i)" and
    8.82 +    sup: "\<And>x. (SUP i. U i x) = max 0 (u x)" and nn: "\<And>i x. 0 \<le> U i x"
    8.83 +  have u_eq: "u = (SUP i. U i)"
    8.84 +    using nn u sup by (auto simp: max_def)
    8.85 +  
    8.86 +  from U have "\<And>i. U i \<in> borel_measurable M"
    8.87 +    by (simp add: borel_measurable_simple_function)
    8.88 +
    8.89 +  show "P u"
    8.90 +    unfolding u_eq
    8.91 +  proof (rule seq)
    8.92 +    fix i show "P (U i)"
    8.93 +      using `simple_function M (U i)` nn
    8.94 +      by (induct rule: simple_function_induct_nn)
    8.95 +         (auto intro: set mult add cong dest!: borel_measurable_simple_function)
    8.96 +  qed fact+
    8.97 +qed
    8.98 +
    8.99  lemma simple_function_If_set:
   8.100    assumes sf: "simple_function M f" "simple_function M g" and A: "A \<inter> space M \<in> sets M"
   8.101    shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
   8.102 @@ -595,7 +687,7 @@
   8.103  
   8.104  lemma simple_integral_indicator:
   8.105    assumes "A \<in> sets M"
   8.106 -  assumes "simple_function M f"
   8.107 +  assumes f: "simple_function M f"
   8.108    shows "(\<integral>\<^isup>Sx. f x * indicator A x \<partial>M) =
   8.109      (\<Sum>x \<in> f ` space M. x * (emeasure M) (f -` {x} \<inter> space M \<inter> A))"
   8.110  proof cases
   8.111 @@ -670,32 +762,6 @@
   8.112    shows "integral\<^isup>S M f = (\<integral>\<^isup>Sx. f x * indicator S x \<partial>M)"
   8.113    using assms by (intro simple_integral_cong_AE) auto
   8.114  
   8.115 -lemma simple_integral_distr:
   8.116 -  assumes T: "T \<in> measurable M M'"
   8.117 -    and f: "simple_function M' f"
   8.118 -  shows "integral\<^isup>S (distr M M' T) f = (\<integral>\<^isup>S x. f (T x) \<partial>M)"
   8.119 -  unfolding simple_integral_def
   8.120 -proof (intro setsum_mono_zero_cong_right ballI)
   8.121 -  show "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space (distr M M' T)"
   8.122 -    using T unfolding measurable_def by auto
   8.123 -  show "finite (f ` space (distr M M' T))"
   8.124 -    using f unfolding simple_function_def by auto
   8.125 -next
   8.126 -  fix i assume "i \<in> f ` space (distr M M' T) - (\<lambda>x. f (T x)) ` space M"
   8.127 -  then have "T -` (f -` {i} \<inter> space (distr M M' T)) \<inter> space M = {}" by (auto simp: image_iff)
   8.128 -  with f[THEN simple_functionD(2), of "{i}"]
   8.129 -  show "i * emeasure (distr M M' T) (f -` {i} \<inter> space (distr M M' T)) = 0"
   8.130 -    using T by (simp add: emeasure_distr)
   8.131 -next
   8.132 -  fix i assume "i \<in> (\<lambda>x. f (T x)) ` space M"
   8.133 -  then have "T -` (f -` {i} \<inter> space M') \<inter> space M = (\<lambda>x. f (T x)) -` {i} \<inter> space M"
   8.134 -    using T unfolding measurable_def by auto
   8.135 -  with f[THEN simple_functionD(2), of "{i}"] T
   8.136 -  show "i * emeasure (distr M M' T) (f -` {i} \<inter> space (distr M M' T)) =
   8.137 -      i * (emeasure M) ((\<lambda>x. f (T x)) -` {i} \<inter> space M)"
   8.138 -    by (auto simp add: emeasure_distr)
   8.139 -qed
   8.140 -
   8.141  lemma simple_integral_cmult_indicator:
   8.142    assumes A: "A \<in> sets M"
   8.143    shows "(\<integral>\<^isup>Sx. c * indicator A x \<partial>M) = c * (emeasure M) A"
   8.144 @@ -1088,7 +1154,7 @@
   8.145  qed
   8.146  
   8.147  lemma positive_integral_cmult:
   8.148 -  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "0 \<le> c"
   8.149 +  assumes f: "f \<in> borel_measurable M" "0 \<le> c"
   8.150    shows "(\<integral>\<^isup>+ x. c * f x \<partial>M) = c * integral\<^isup>P M f"
   8.151  proof -
   8.152    have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
   8.153 @@ -1101,14 +1167,14 @@
   8.154  qed
   8.155  
   8.156  lemma positive_integral_multc:
   8.157 -  assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "0 \<le> c"
   8.158 +  assumes "f \<in> borel_measurable M" "0 \<le> c"
   8.159    shows "(\<integral>\<^isup>+ x. f x * c \<partial>M) = integral\<^isup>P M f * c"
   8.160    unfolding mult_commute[of _ c] positive_integral_cmult[OF assms] by simp
   8.161  
   8.162  lemma positive_integral_indicator[simp]:
   8.163    "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. indicator A x\<partial>M) = (emeasure M) A"
   8.164    by (subst positive_integral_eq_simple_integral)
   8.165 -     (auto simp: simple_function_indicator simple_integral_indicator)
   8.166 +     (auto simp: simple_integral_indicator)
   8.167  
   8.168  lemma positive_integral_cmult_indicator:
   8.169    "0 \<le> c \<Longrightarrow> A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x \<partial>M) = c * (emeasure M) A"
   8.170 @@ -1151,7 +1217,7 @@
   8.171  qed simp
   8.172  
   8.173  lemma positive_integral_Markov_inequality:
   8.174 -  assumes u: "u \<in> borel_measurable M" "AE x in M. 0 \<le> u x" and "A \<in> sets M" and c: "0 \<le> c" "c \<noteq> \<infinity>"
   8.175 +  assumes u: "u \<in> borel_measurable M" "AE x in M. 0 \<le> u x" and "A \<in> sets M" and c: "0 \<le> c"
   8.176    shows "(emeasure M) ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
   8.177      (is "(emeasure M) ?A \<le> _ * ?PI")
   8.178  proof -
   8.179 @@ -1342,23 +1408,21 @@
   8.180    by (auto intro!: positive_integral_0_iff_AE[THEN iffD2])
   8.181  
   8.182  lemma positive_integral_subalgebra:
   8.183 -  assumes f: "f \<in> borel_measurable N" "AE x in N. 0 \<le> f x"
   8.184 +  assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x"
   8.185    and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
   8.186    shows "integral\<^isup>P N f = integral\<^isup>P M f"
   8.187  proof -
   8.188 -  from borel_measurable_implies_simple_function_sequence'[OF f(1)] guess fs . note fs = this
   8.189 -  note sf = simple_function_subalgebra[OF fs(1) N(1,2)]
   8.190 -  from positive_integral_monotone_convergence_simple[OF fs(2,5,1), symmetric]
   8.191 -  have "integral\<^isup>P N f = (SUP i. \<Sum>x\<in>fs i ` space M. x * emeasure N (fs i -` {x} \<inter> space M))"
   8.192 -    unfolding fs(4) positive_integral_max_0
   8.193 -    unfolding simple_integral_def `space N = space M` by simp
   8.194 -  also have "\<dots> = (SUP i. \<Sum>x\<in>fs i ` space M. x * (emeasure M) (fs i -` {x} \<inter> space M))"
   8.195 -    using N simple_functionD(2)[OF fs(1)] unfolding `space N = space M` by auto
   8.196 -  also have "\<dots> = integral\<^isup>P M f"
   8.197 -    using positive_integral_monotone_convergence_simple[OF fs(2,5) sf, symmetric]
   8.198 -    unfolding fs(4) positive_integral_max_0
   8.199 -    unfolding simple_integral_def `space N = space M` by simp
   8.200 -  finally show ?thesis .
   8.201 +  have [simp]: "\<And>f :: 'a \<Rightarrow> ereal. f \<in> borel_measurable N \<Longrightarrow> f \<in> borel_measurable M"
   8.202 +    using N by (auto simp: measurable_def)
   8.203 +  have [simp]: "\<And>P. (AE x in N. P x) \<Longrightarrow> (AE x in M. P x)"
   8.204 +    using N by (auto simp add: eventually_ae_filter null_sets_def)
   8.205 +  have [simp]: "\<And>A. A \<in> sets N \<Longrightarrow> A \<in> sets M"
   8.206 +    using N by auto
   8.207 +  from f show ?thesis
   8.208 +    apply induct
   8.209 +    apply (simp_all add: positive_integral_add positive_integral_cmult positive_integral_monotone_convergence_SUP N)
   8.210 +    apply (auto intro!: positive_integral_cong cong: positive_integral_cong simp: N(2)[symmetric])
   8.211 +    done
   8.212  qed
   8.213  
   8.214  section "Lebesgue Integral"
   8.215 @@ -1423,6 +1487,25 @@
   8.216    "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable M g"
   8.217    by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
   8.218  
   8.219 +lemma integral_mono_AE:
   8.220 +  assumes fg: "integrable M f" "integrable M g"
   8.221 +  and mono: "AE t in M. f t \<le> g t"
   8.222 +  shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
   8.223 +proof -
   8.224 +  have "AE x in M. ereal (f x) \<le> ereal (g x)"
   8.225 +    using mono by auto
   8.226 +  moreover have "AE x in M. ereal (- g x) \<le> ereal (- f x)"
   8.227 +    using mono by auto
   8.228 +  ultimately show ?thesis using fg
   8.229 +    by (auto intro!: add_mono positive_integral_mono_AE real_of_ereal_positive_mono
   8.230 +             simp: positive_integral_positive lebesgue_integral_def diff_minus)
   8.231 +qed
   8.232 +
   8.233 +lemma integral_mono:
   8.234 +  assumes "integrable M f" "integrable M g" "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
   8.235 +  shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
   8.236 +  using assms by (auto intro: integral_mono_AE)
   8.237 +
   8.238  lemma positive_integral_eq_integral:
   8.239    assumes f: "integrable M f"
   8.240    assumes nonneg: "AE x in M. 0 \<le> f x" 
   8.241 @@ -1571,20 +1654,16 @@
   8.242    assumes f: "f \<in> borel_measurable M" and "0 \<le> c"
   8.243    shows "(\<integral>x. c * f x \<partial>M) = c * integral\<^isup>L M f"
   8.244  proof -
   8.245 -  { have "c * real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (f x)))) =
   8.246 -      real (ereal c * integral\<^isup>P M (\<lambda>x. max 0 (ereal (f x))))"
   8.247 -      by simp
   8.248 -    also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. ereal c * max 0 (ereal (f x))))"
   8.249 +  { have "real (ereal c * integral\<^isup>P M (\<lambda>x. max 0 (ereal (f x)))) =
   8.250 +      real (integral\<^isup>P M (\<lambda>x. ereal c * max 0 (ereal (f x))))"
   8.251        using f `0 \<le> c` by (subst positive_integral_cmult) auto
   8.252      also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (c * f x))))"
   8.253        using `0 \<le> c` by (auto intro!: arg_cong[where f=real] positive_integral_cong simp: max_def zero_le_mult_iff)
   8.254      finally have "real (integral\<^isup>P M (\<lambda>x. ereal (c * f x))) = c * real (integral\<^isup>P M (\<lambda>x. ereal (f x)))"
   8.255        by (simp add: positive_integral_max_0) }
   8.256    moreover
   8.257 -  { have "c * real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (- f x)))) =
   8.258 -      real (ereal c * integral\<^isup>P M (\<lambda>x. max 0 (ereal (- f x))))"
   8.259 -      by simp
   8.260 -    also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. ereal c * max 0 (ereal (- f x))))"
   8.261 +  { have "real (ereal c * integral\<^isup>P M (\<lambda>x. max 0 (ereal (- f x)))) =
   8.262 +      real (integral\<^isup>P M (\<lambda>x. ereal c * max 0 (ereal (- f x))))"
   8.263        using f `0 \<le> c` by (subst positive_integral_cmult) auto
   8.264      also have "\<dots> = real (integral\<^isup>P M (\<lambda>x. max 0 (ereal (- c * f x))))"
   8.265        using `0 \<le> c` by (auto intro!: arg_cong[where f=real] positive_integral_cong simp: max_def mult_le_0_iff)
   8.266 @@ -1615,25 +1694,6 @@
   8.267    shows "(\<integral> x. f x * c \<partial>M) = integral\<^isup>L M f * c"
   8.268    unfolding mult_commute[of _ c] integral_cmult[OF assms] ..
   8.269  
   8.270 -lemma integral_mono_AE:
   8.271 -  assumes fg: "integrable M f" "integrable M g"
   8.272 -  and mono: "AE t in M. f t \<le> g t"
   8.273 -  shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
   8.274 -proof -
   8.275 -  have "AE x in M. ereal (f x) \<le> ereal (g x)"
   8.276 -    using mono by auto
   8.277 -  moreover have "AE x in M. ereal (- g x) \<le> ereal (- f x)"
   8.278 -    using mono by auto
   8.279 -  ultimately show ?thesis using fg
   8.280 -    by (auto intro!: add_mono positive_integral_mono_AE real_of_ereal_positive_mono
   8.281 -             simp: positive_integral_positive lebesgue_integral_def diff_minus)
   8.282 -qed
   8.283 -
   8.284 -lemma integral_mono:
   8.285 -  assumes "integrable M f" "integrable M g" "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
   8.286 -  shows "integral\<^isup>L M f \<le> integral\<^isup>L M g"
   8.287 -  using assms by (auto intro: integral_mono_AE)
   8.288 -
   8.289  lemma integral_diff[simp, intro]:
   8.290    assumes f: "integrable M f" and g: "integrable M g"
   8.291    shows "integrable M (\<lambda>t. f t - g t)"
   8.292 @@ -1685,8 +1745,33 @@
   8.293    thus "?int S" and "?I S" by auto
   8.294  qed
   8.295  
   8.296 +lemma integrable_bound:
   8.297 +  assumes "integrable M f" and f: "AE x in M. \<bar>g x\<bar> \<le> f x"
   8.298 +  assumes borel: "g \<in> borel_measurable M"
   8.299 +  shows "integrable M g"
   8.300 +proof -
   8.301 +  have "(\<integral>\<^isup>+ x. ereal (g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. ereal \<bar>g x\<bar> \<partial>M)"
   8.302 +    by (auto intro!: positive_integral_mono)
   8.303 +  also have "\<dots> \<le> (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
   8.304 +    using f by (auto intro!: positive_integral_mono_AE)
   8.305 +  also have "\<dots> < \<infinity>"
   8.306 +    using `integrable M f` unfolding integrable_def by auto
   8.307 +  finally have pos: "(\<integral>\<^isup>+ x. ereal (g x) \<partial>M) < \<infinity>" .
   8.308 +
   8.309 +  have "(\<integral>\<^isup>+ x. ereal (- g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. ereal (\<bar>g x\<bar>) \<partial>M)"
   8.310 +    by (auto intro!: positive_integral_mono)
   8.311 +  also have "\<dots> \<le> (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
   8.312 +    using f by (auto intro!: positive_integral_mono_AE)
   8.313 +  also have "\<dots> < \<infinity>"
   8.314 +    using `integrable M f` unfolding integrable_def by auto
   8.315 +  finally have neg: "(\<integral>\<^isup>+ x. ereal (- g x) \<partial>M) < \<infinity>" .
   8.316 +
   8.317 +  from neg pos borel show ?thesis
   8.318 +    unfolding integrable_def by auto
   8.319 +qed
   8.320 +
   8.321  lemma integrable_abs:
   8.322 -  assumes "integrable M f"
   8.323 +  assumes f: "integrable M f"
   8.324    shows "integrable M (\<lambda> x. \<bar>f x\<bar>)"
   8.325  proof -
   8.326    from assms have *: "(\<integral>\<^isup>+x. ereal (- \<bar>f x\<bar>)\<partial>M) = 0"
   8.327 @@ -1711,33 +1796,6 @@
   8.328      by (auto simp: integrable_def lebesgue_integral_def positive_integral_max_0)
   8.329  qed
   8.330  
   8.331 -lemma integrable_bound:
   8.332 -  assumes "integrable M f"
   8.333 -  and f: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
   8.334 -    "\<And>x. x \<in> space M \<Longrightarrow> \<bar>g x\<bar> \<le> f x"
   8.335 -  assumes borel: "g \<in> borel_measurable M"
   8.336 -  shows "integrable M g"
   8.337 -proof -
   8.338 -  have "(\<integral>\<^isup>+ x. ereal (g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. ereal \<bar>g x\<bar> \<partial>M)"
   8.339 -    by (auto intro!: positive_integral_mono)
   8.340 -  also have "\<dots> \<le> (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
   8.341 -    using f by (auto intro!: positive_integral_mono)
   8.342 -  also have "\<dots> < \<infinity>"
   8.343 -    using `integrable M f` unfolding integrable_def by auto
   8.344 -  finally have pos: "(\<integral>\<^isup>+ x. ereal (g x) \<partial>M) < \<infinity>" .
   8.345 -
   8.346 -  have "(\<integral>\<^isup>+ x. ereal (- g x) \<partial>M) \<le> (\<integral>\<^isup>+ x. ereal (\<bar>g x\<bar>) \<partial>M)"
   8.347 -    by (auto intro!: positive_integral_mono)
   8.348 -  also have "\<dots> \<le> (\<integral>\<^isup>+ x. ereal (f x) \<partial>M)"
   8.349 -    using f by (auto intro!: positive_integral_mono)
   8.350 -  also have "\<dots> < \<infinity>"
   8.351 -    using `integrable M f` unfolding integrable_def by auto
   8.352 -  finally have neg: "(\<integral>\<^isup>+ x. ereal (- g x) \<partial>M) < \<infinity>" .
   8.353 -
   8.354 -  from neg pos borel show ?thesis
   8.355 -    unfolding integrable_def by auto
   8.356 -qed
   8.357 -
   8.358  lemma lebesgue_integral_nonneg:
   8.359    assumes ae: "(AE x in M. 0 \<le> f x)" shows "0 \<le> integral\<^isup>L M f"
   8.360  proof -
   8.361 @@ -1760,11 +1818,7 @@
   8.362      using int by (simp add: integrable_abs)
   8.363    show "(\<lambda>x. max (f x) (g x)) \<in> borel_measurable M"
   8.364      using int unfolding integrable_def by auto
   8.365 -next
   8.366 -  fix x assume "x \<in> space M"
   8.367 -  show "0 \<le> \<bar>f x\<bar> + \<bar>g x\<bar>" "\<bar>max (f x) (g x)\<bar> \<le> \<bar>f x\<bar> + \<bar>g x\<bar>"
   8.368 -    by auto
   8.369 -qed
   8.370 +qed auto
   8.371  
   8.372  lemma integrable_min:
   8.373    assumes int: "integrable M f" "integrable M g"
   8.374 @@ -1774,11 +1828,7 @@
   8.375      using int by (simp add: integrable_abs)
   8.376    show "(\<lambda>x. min (f x) (g x)) \<in> borel_measurable M"
   8.377      using int unfolding integrable_def by auto
   8.378 -next
   8.379 -  fix x assume "x \<in> space M"
   8.380 -  show "0 \<le> \<bar>f x\<bar> + \<bar>g x\<bar>" "\<bar>min (f x) (g x)\<bar> \<le> \<bar>f x\<bar> + \<bar>g x\<bar>"
   8.381 -    by auto
   8.382 -qed
   8.383 +qed auto
   8.384  
   8.385  lemma integral_triangle_inequality:
   8.386    assumes "integrable M f"
   8.387 @@ -1802,74 +1852,71 @@
   8.388  qed
   8.389  
   8.390  lemma integral_monotone_convergence_pos:
   8.391 -  assumes i: "\<And>i. integrable M (f i)" and mono: "\<And>x. mono (\<lambda>n. f n x)"
   8.392 -  and pos: "\<And>x i. 0 \<le> f i x"
   8.393 -  and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
   8.394 -  and ilim: "(\<lambda>i. integral\<^isup>L M (f i)) ----> x"
   8.395 +  assumes i: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
   8.396 +    and pos: "\<And>i. AE x in M. 0 \<le> f i x"
   8.397 +    and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
   8.398 +    and ilim: "(\<lambda>i. integral\<^isup>L M (f i)) ----> x"
   8.399 +    and u: "u \<in> borel_measurable M"
   8.400    shows "integrable M u"
   8.401    and "integral\<^isup>L M u = x"
   8.402  proof -
   8.403 -  { fix x have "0 \<le> u x"
   8.404 -      using mono pos[of 0 x] incseq_le[OF _ lim, of x 0]
   8.405 -      by (simp add: mono_def incseq_def) }
   8.406 -  note pos_u = this
   8.407 -
   8.408 -  have SUP_F: "\<And>x. (SUP n. ereal (f n x)) = ereal (u x)"
   8.409 -    unfolding SUP_eq_LIMSEQ[OF mono] by (rule lim)
   8.410 -
   8.411 -  have borel_f: "\<And>i. (\<lambda>x. ereal (f i x)) \<in> borel_measurable M"
   8.412 -    using i unfolding integrable_def by auto
   8.413 -  hence "(\<lambda>x. SUP i. ereal (f i x)) \<in> borel_measurable M"
   8.414 -    by auto
   8.415 -  hence borel_u: "u \<in> borel_measurable M"
   8.416 -    by (auto simp: borel_measurable_ereal_iff SUP_F)
   8.417 -
   8.418 -  hence [simp]: "\<And>i. (\<integral>\<^isup>+x. ereal (- f i x) \<partial>M) = 0" "(\<integral>\<^isup>+x. ereal (- u x) \<partial>M) = 0"
   8.419 -    using i borel_u pos pos_u by (auto simp: positive_integral_0_iff_AE integrable_def)
   8.420 -
   8.421 -  have integral_eq: "\<And>n. (\<integral>\<^isup>+ x. ereal (f n x) \<partial>M) = ereal (integral\<^isup>L M (f n))"
   8.422 -    using i positive_integral_positive[of M] by (auto simp: ereal_real lebesgue_integral_def integrable_def)
   8.423 -
   8.424 -  have pos_integral: "\<And>n. 0 \<le> integral\<^isup>L M (f n)"
   8.425 -    using pos i by (auto simp: integral_positive)
   8.426 -  hence "0 \<le> x"
   8.427 -    using LIMSEQ_le_const[OF ilim, of 0] by auto
   8.428 -
   8.429 -  from mono pos i have pI: "(\<integral>\<^isup>+ x. ereal (u x) \<partial>M) = (SUP n. (\<integral>\<^isup>+ x. ereal (f n x) \<partial>M))"
   8.430 -    by (auto intro!: positive_integral_monotone_convergence_SUP
   8.431 -      simp: integrable_def incseq_mono incseq_Suc_iff le_fun_def SUP_F[symmetric])
   8.432 -  also have "\<dots> = ereal x" unfolding integral_eq
   8.433 -  proof (rule SUP_eq_LIMSEQ[THEN iffD2])
   8.434 -    show "mono (\<lambda>n. integral\<^isup>L M (f n))"
   8.435 -      using mono i by (auto simp: mono_def intro!: integral_mono)
   8.436 -    show "(\<lambda>n. integral\<^isup>L M (f n)) ----> x" using ilim .
   8.437 +  have "(\<integral>\<^isup>+ x. ereal (u x) \<partial>M) = (SUP n. (\<integral>\<^isup>+ x. ereal (f n x) \<partial>M))"
   8.438 +  proof (subst positive_integral_monotone_convergence_SUP_AE[symmetric])
   8.439 +    fix i
   8.440 +    from mono pos show "AE x in M. ereal (f i x) \<le> ereal (f (Suc i) x) \<and> 0 \<le> ereal (f i x)"
   8.441 +      by eventually_elim (auto simp: mono_def)
   8.442 +    show "(\<lambda>x. ereal (f i x)) \<in> borel_measurable M"
   8.443 +      using i by (auto simp: integrable_def)
   8.444 +  next
   8.445 +    show "(\<integral>\<^isup>+ x. ereal (u x) \<partial>M) = \<integral>\<^isup>+ x. (SUP i. ereal (f i x)) \<partial>M"
   8.446 +      apply (rule positive_integral_cong_AE)
   8.447 +      using lim mono
   8.448 +      by eventually_elim (simp add: SUP_eq_LIMSEQ[THEN iffD2])
   8.449    qed
   8.450 -  finally show  "integrable M u" "integral\<^isup>L M u = x" using borel_u `0 \<le> x`
   8.451 -    unfolding integrable_def lebesgue_integral_def by auto
   8.452 +  also have "\<dots> = ereal x"
   8.453 +    using mono i unfolding positive_integral_eq_integral[OF i pos]
   8.454 +    by (subst SUP_eq_LIMSEQ) (auto simp: mono_def intro!: integral_mono_AE ilim)
   8.455 +  finally have "(\<integral>\<^isup>+ x. ereal (u x) \<partial>M) = ereal x" .
   8.456 +  moreover have "(\<integral>\<^isup>+ x. ereal (- u x) \<partial>M) = 0"
   8.457 +  proof (subst positive_integral_0_iff_AE)
   8.458 +    show "(\<lambda>x. ereal (- u x)) \<in> borel_measurable M"
   8.459 +      using u by auto
   8.460 +    from mono pos[of 0] lim show "AE x in M. ereal (- u x) \<le> 0"
   8.461 +    proof eventually_elim
   8.462 +      fix x assume "mono (\<lambda>n. f n x)" "0 \<le> f 0 x" "(\<lambda>i. f i x) ----> u x"
   8.463 +      then show "ereal (- u x) \<le> 0"
   8.464 +        using incseq_le[of "\<lambda>n. f n x" "u x" 0] by (simp add: mono_def incseq_def)
   8.465 +    qed
   8.466 +  qed
   8.467 +  ultimately show "integrable M u" "integral\<^isup>L M u = x"
   8.468 +    by (auto simp: integrable_def lebesgue_integral_def u)
   8.469  qed
   8.470  
   8.471  lemma integral_monotone_convergence:
   8.472 -  assumes f: "\<And>i. integrable M (f i)" and "mono f"
   8.473 -  and lim: "\<And>x. (\<lambda>i. f i x) ----> u x"
   8.474 +  assumes f: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
   8.475 +  and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
   8.476    and ilim: "(\<lambda>i. integral\<^isup>L M (f i)) ----> x"
   8.477 +  and u: "u \<in> borel_measurable M"
   8.478    shows "integrable M u"
   8.479    and "integral\<^isup>L M u = x"
   8.480  proof -
   8.481    have 1: "\<And>i. integrable M (\<lambda>x. f i x - f 0 x)"
   8.482 -      using f by (auto intro!: integral_diff)
   8.483 -  have 2: "\<And>x. mono (\<lambda>n. f n x - f 0 x)" using `mono f`
   8.484 -      unfolding mono_def le_fun_def by auto
   8.485 -  have 3: "\<And>x n. 0 \<le> f n x - f 0 x" using `mono f`
   8.486 -      unfolding mono_def le_fun_def by (auto simp: field_simps)
   8.487 -  have 4: "\<And>x. (\<lambda>i. f i x - f 0 x) ----> u x - f 0 x"
   8.488 +    using f by auto
   8.489 +  have 2: "AE x in M. mono (\<lambda>n. f n x - f 0 x)"
   8.490 +    using mono by (auto simp: mono_def le_fun_def)
   8.491 +  have 3: "\<And>n. AE x in M. 0 \<le> f n x - f 0 x"
   8.492 +    using mono by (auto simp: field_simps mono_def le_fun_def)
   8.493 +  have 4: "AE x in M. (\<lambda>i. f i x - f 0 x) ----> u x - f 0 x"
   8.494      using lim by (auto intro!: tendsto_diff)
   8.495    have 5: "(\<lambda>i. (\<integral>x. f i x - f 0 x \<partial>M)) ----> x - integral\<^isup>L M (f 0)"
   8.496 -    using f ilim by (auto intro!: tendsto_diff simp: integral_diff)
   8.497 -  note diff = integral_monotone_convergence_pos[OF 1 2 3 4 5]
   8.498 +    using f ilim by (auto intro!: tendsto_diff)
   8.499 +  have 6: "(\<lambda>x. u x - f 0 x) \<in> borel_measurable M"
   8.500 +    using f[of 0] u by auto
   8.501 +  note diff = integral_monotone_convergence_pos[OF 1 2 3 4 5 6]
   8.502    have "integrable M (\<lambda>x. (u x - f 0 x) + f 0 x)"
   8.503      using diff(1) f by (rule integral_add(1))
   8.504    with diff(2) f show "integrable M u" "integral\<^isup>L M u = x"
   8.505 -    by (auto simp: integral_diff)
   8.506 +    by auto
   8.507  qed
   8.508  
   8.509  lemma integral_0_iff:
   8.510 @@ -1993,58 +2040,47 @@
   8.511    using gt by (intro integral_less_AE[OF int, where A="space M"]) auto
   8.512  
   8.513  lemma integral_dominated_convergence:
   8.514 -  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"
   8.515 +  assumes u: "\<And>i. integrable M (u i)" and bound: "\<And>j. AE x in M. \<bar>u j x\<bar> \<le> w x"
   8.516    and w: "integrable M w"
   8.517 -  and u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
   8.518 +  and u': "AE x in M. (\<lambda>i. u i x) ----> u' x"
   8.519 +  and borel: "u' \<in> borel_measurable M"
   8.520    shows "integrable M u'"
   8.521    and "(\<lambda>i. (\<integral>x. \<bar>u i x - u' x\<bar> \<partial>M)) ----> 0" (is "?lim_diff")
   8.522    and "(\<lambda>i. integral\<^isup>L M (u i)) ----> integral\<^isup>L M u'" (is ?lim)
   8.523  proof -
   8.524 -  { fix x j assume x: "x \<in> space M"
   8.525 -    from u'[OF x] have "(\<lambda>i. \<bar>u i x\<bar>) ----> \<bar>u' x\<bar>" by (rule tendsto_rabs)
   8.526 -    from LIMSEQ_le_const2[OF this]
   8.527 -    have "\<bar>u' x\<bar> \<le> w x" using bound[OF x] by auto }
   8.528 -  note u'_bound = this
   8.529 +  have all_bound: "AE x in M. \<forall>j. \<bar>u j x\<bar> \<le> w x"
   8.530 +    using bound by (auto simp: AE_all_countable)
   8.531 +  with u' have u'_bound: "AE x in M. \<bar>u' x\<bar> \<le> w x"
   8.532 +    by eventually_elim (auto intro: LIMSEQ_le_const2 tendsto_rabs)
   8.533  
   8.534 -  from u[unfolded integrable_def]
   8.535 -  have u'_borel: "u' \<in> borel_measurable M"
   8.536 -    using u' by (blast intro: borel_measurable_LIMSEQ[of M u])
   8.537 -
   8.538 -  { fix x assume x: "x \<in> space M"
   8.539 -    then have "0 \<le> \<bar>u 0 x\<bar>" by auto
   8.540 -    also have "\<dots> \<le> w x" using bound[OF x] by auto
   8.541 -    finally have "0 \<le> w x" . }
   8.542 -  note w_pos = this
   8.543 +  from bound[of 0] have w_pos: "AE x in M. 0 \<le> w x"
   8.544 +    by eventually_elim auto
   8.545  
   8.546    show "integrable M u'"
   8.547 -  proof (rule integrable_bound)
   8.548 -    show "integrable M w" by fact
   8.549 -    show "u' \<in> borel_measurable M" by fact
   8.550 -  next
   8.551 -    fix x assume x: "x \<in> space M" then show "0 \<le> w x" by fact
   8.552 -    show "\<bar>u' x\<bar> \<le> w x" using u'_bound[OF x] .
   8.553 -  qed
   8.554 +    by (rule integrable_bound) fact+
   8.555  
   8.556    let ?diff = "\<lambda>n x. 2 * w x - \<bar>u n x - u' x\<bar>"
   8.557    have diff: "\<And>n. integrable M (\<lambda>x. \<bar>u n x - u' x\<bar>)"
   8.558 -    using w u `integrable M u'`
   8.559 -    by (auto intro!: integral_add integral_diff integral_cmult integrable_abs)
   8.560 +    using w u `integrable M u'` by (auto intro!: integrable_abs)
   8.561  
   8.562 -  { fix j x assume x: "x \<in> space M"
   8.563 -    have "\<bar>u j x - u' x\<bar> \<le> \<bar>u j x\<bar> + \<bar>u' x\<bar>" by auto
   8.564 +  from u'_bound all_bound
   8.565 +  have diff_less_2w: "AE x in M. \<forall>j. \<bar>u j x - u' x\<bar> \<le> 2 * w x"
   8.566 +  proof (eventually_elim, intro allI)
   8.567 +    fix x j assume *: "\<bar>u' x\<bar> \<le> w x" "\<forall>j. \<bar>u j x\<bar> \<le> w x"
   8.568 +    then have "\<bar>u j x - u' x\<bar> \<le> \<bar>u j x\<bar> + \<bar>u' x\<bar>" by auto
   8.569      also have "\<dots> \<le> w x + w x"
   8.570 -      by (rule add_mono[OF bound[OF x] u'_bound[OF x]])
   8.571 -    finally have "\<bar>u j x - u' x\<bar> \<le> 2 * w x" by simp }
   8.572 -  note diff_less_2w = this
   8.573 +      using * by (intro add_mono) auto
   8.574 +    finally show "\<bar>u j x - u' x\<bar> \<le> 2 * w x" by simp
   8.575 +  qed
   8.576  
   8.577    have PI_diff: "\<And>n. (\<integral>\<^isup>+ x. ereal (?diff n x) \<partial>M) =
   8.578      (\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) - (\<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
   8.579      using diff w diff_less_2w w_pos
   8.580      by (subst positive_integral_diff[symmetric])
   8.581 -       (auto simp: integrable_def intro!: positive_integral_cong)
   8.582 +       (auto simp: integrable_def intro!: positive_integral_cong_AE)
   8.583  
   8.584    have "integrable M (\<lambda>x. 2 * w x)"
   8.585 -    using w by (auto intro: integral_cmult)
   8.586 +    using w by auto
   8.587    hence I2w_fin: "(\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) \<noteq> \<infinity>" and
   8.588      borel_2w: "(\<lambda>x. ereal (2 * w x)) \<in> borel_measurable M"
   8.589      unfolding integrable_def by auto
   8.590 @@ -2054,8 +2090,8 @@
   8.591      assume eq_0: "(\<integral>\<^isup>+ x. max 0 (ereal (2 * w x)) \<partial>M) = 0" (is "?wx = 0")
   8.592      { fix n
   8.593        have "?f n \<le> ?wx" (is "integral\<^isup>P M ?f' \<le> _")
   8.594 -        using diff_less_2w[of _ n] unfolding positive_integral_max_0
   8.595 -        by (intro positive_integral_mono) auto
   8.596 +        using diff_less_2w unfolding positive_integral_max_0
   8.597 +        by (intro positive_integral_mono_AE) auto
   8.598        then have "?f n = 0"
   8.599          using positive_integral_positive[of M ?f'] eq_0 by auto }
   8.600      then show ?thesis by (simp add: Limsup_const)
   8.601 @@ -2066,19 +2102,20 @@
   8.602        by (intro limsup_mono positive_integral_positive)
   8.603      finally have pos: "0 \<le> limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)" .
   8.604      have "?wx = (\<integral>\<^isup>+ x. liminf (\<lambda>n. max 0 (ereal (?diff n x))) \<partial>M)"
   8.605 -    proof (rule positive_integral_cong)
   8.606 -      fix x assume x: "x \<in> space M"
   8.607 +      using u'
   8.608 +    proof (intro positive_integral_cong_AE, eventually_elim)
   8.609 +      fix x assume u': "(\<lambda>i. u i x) ----> u' x"
   8.610        show "max 0 (ereal (2 * w x)) = liminf (\<lambda>n. max 0 (ereal (?diff n x)))"
   8.611          unfolding ereal_max_0
   8.612        proof (rule lim_imp_Liminf[symmetric], unfold lim_ereal)
   8.613          have "(\<lambda>i. ?diff i x) ----> 2 * w x - \<bar>u' x - u' x\<bar>"
   8.614 -          using u'[OF x] by (safe intro!: tendsto_intros)
   8.615 +          using u' by (safe intro!: tendsto_intros)
   8.616          then show "(\<lambda>i. max 0 (?diff i x)) ----> max 0 (2 * w x)"
   8.617 -          by (auto intro!: tendsto_real_max simp add: lim_ereal)
   8.618 +          by (auto intro!: tendsto_real_max)
   8.619        qed (rule trivial_limit_sequentially)
   8.620      qed
   8.621      also have "\<dots> \<le> liminf (\<lambda>n. \<integral>\<^isup>+ x. max 0 (ereal (?diff n x)) \<partial>M)"
   8.622 -      using u'_borel w u unfolding integrable_def
   8.623 +      using borel w u unfolding integrable_def
   8.624        by (intro positive_integral_lim_INF) (auto intro!: positive_integral_lim_INF)
   8.625      also have "\<dots> = (\<integral>\<^isup>+ x. ereal (2 * w x) \<partial>M) -
   8.626          limsup (\<lambda>n. \<integral>\<^isup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
   8.627 @@ -2106,7 +2143,7 @@
   8.628      by (subst integral_eq_positive_integral[of _ M]) (auto simp: ereal_real integrable_def)
   8.629    then show ?lim_diff
   8.630      using ereal_Liminf_eq_Limsup[OF trivial_limit_sequentially liminf_limsup_eq]
   8.631 -    by (simp add: lim_ereal)
   8.632 +    by simp
   8.633  
   8.634    show ?lim
   8.635    proof (rule LIMSEQ_I)
   8.636 @@ -2119,9 +2156,9 @@
   8.637      proof (safe intro!: exI[of _ N])
   8.638        fix n assume "N \<le> n"
   8.639        have "\<bar>integral\<^isup>L M (u n) - integral\<^isup>L M u'\<bar> = \<bar>(\<integral>x. u n x - u' x \<partial>M)\<bar>"
   8.640 -        using u `integrable M u'` by (auto simp: integral_diff)
   8.641 +        using u `integrable M u'` by auto
   8.642        also have "\<dots> \<le> (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M)" using u `integrable M u'`
   8.643 -        by (rule_tac integral_triangle_inequality) (auto intro!: integral_diff)
   8.644 +        by (rule_tac integral_triangle_inequality) auto
   8.645        also note N[OF `N \<le> n`]
   8.646        finally show "norm (integral\<^isup>L M (u n) - integral\<^isup>L M u') < r" by simp
   8.647      qed
   8.648 @@ -2139,6 +2176,8 @@
   8.649      using summable unfolding summable_def by auto
   8.650    from bchoice[OF this]
   8.651    obtain w where w: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. \<bar>f i x\<bar>) sums w x" by auto
   8.652 +  then have w_borel: "w \<in> borel_measurable M" unfolding sums_def
   8.653 +    by (rule borel_measurable_LIMSEQ) (auto simp: borel[THEN integrableD(1)])
   8.654  
   8.655    let ?w = "\<lambda>y. if y \<in> space M then w y else 0"
   8.656  
   8.657 @@ -2146,13 +2185,16 @@
   8.658      using sums unfolding summable_def ..
   8.659  
   8.660    have 1: "\<And>n. integrable M (\<lambda>x. \<Sum>i = 0..<n. f i x)"
   8.661 -    using borel by (auto intro!: integral_setsum)
   8.662 +    using borel by auto
   8.663  
   8.664 -  { fix j x assume [simp]: "x \<in> space M"
   8.665 +  have 2: "\<And>j. AE x in M. \<bar>\<Sum>i = 0..<j. f i x\<bar> \<le> ?w x"
   8.666 +    using AE_space
   8.667 +  proof eventually_elim
   8.668 +    fix j x assume [simp]: "x \<in> space M"
   8.669      have "\<bar>\<Sum>i = 0..< j. f i x\<bar> \<le> (\<Sum>i = 0..< j. \<bar>f i x\<bar>)" by (rule setsum_abs)
   8.670      also have "\<dots> \<le> w x" using w[of x] series_pos_le[of "\<lambda>i. \<bar>f i x\<bar>"] unfolding sums_iff by auto
   8.671 -    finally have "\<bar>\<Sum>i = 0..<j. f i x\<bar> \<le> ?w x" by simp }
   8.672 -  note 2 = this
   8.673 +    finally show "\<bar>\<Sum>i = 0..<j. f i x\<bar> \<le> ?w x" by simp
   8.674 +  qed
   8.675  
   8.676    have 3: "integrable M ?w"
   8.677    proof (rule integral_monotone_convergence(1))
   8.678 @@ -2161,21 +2203,22 @@
   8.679      have "\<And>n. integrable M (?F n)"
   8.680        using borel by (auto intro!: integral_setsum integrable_abs)
   8.681      thus "\<And>n. integrable M (?w' n)" by (simp cong: integrable_cong)
   8.682 -    show "mono ?w'"
   8.683 +    show "AE x in M. mono (\<lambda>n. ?w' n x)"
   8.684        by (auto simp: mono_def le_fun_def intro!: setsum_mono2)
   8.685 -    { fix x show "(\<lambda>n. ?w' n x) ----> ?w x"
   8.686 -        using w by (cases "x \<in> space M") (simp_all add: tendsto_const sums_def) }
   8.687 +    show "AE x in M. (\<lambda>n. ?w' n x) ----> ?w x"
   8.688 +        using w by (simp_all add: tendsto_const sums_def)
   8.689      have *: "\<And>n. integral\<^isup>L M (?w' n) = (\<Sum>i = 0..< n. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
   8.690 -      using borel by (simp add: integral_setsum integrable_abs cong: integral_cong)
   8.691 +      using borel by (simp add: integrable_abs cong: integral_cong)
   8.692      from abs_sum
   8.693      show "(\<lambda>i. integral\<^isup>L M (?w' i)) ----> x" unfolding * sums_def .
   8.694 -  qed
   8.695 +  qed (simp add: w_borel measurable_If_set)
   8.696  
   8.697    from summable[THEN summable_rabs_cancel]
   8.698 -  have 4: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>n. \<Sum>i = 0..<n. f i x) ----> (\<Sum>i. f i x)"
   8.699 +  have 4: "AE x in M. (\<lambda>n. \<Sum>i = 0..<n. f i x) ----> (\<Sum>i. f i x)"
   8.700      by (auto intro: summable_sumr_LIMSEQ_suminf)
   8.701  
   8.702 -  note int = integral_dominated_convergence(1,3)[OF 1 2 3 4]
   8.703 +  note int = integral_dominated_convergence(1,3)[OF 1 2 3 4
   8.704 +    borel_measurable_suminf[OF integrableD(1)[OF borel]]]
   8.705  
   8.706    from int show "integrable M ?S" by simp
   8.707  
   8.708 @@ -2244,59 +2287,54 @@
   8.709  
   8.710  section {* Distributions *}
   8.711  
   8.712 -lemma simple_function_distr[simp]:
   8.713 -  "simple_function (distr M M' T) f \<longleftrightarrow> simple_function M' (\<lambda>x. f x)"
   8.714 - unfolding simple_function_def by simp
   8.715 +lemma positive_integral_distr':
   8.716 +  assumes T: "T \<in> measurable M M'"
   8.717 +  and f: "f \<in> borel_measurable (distr M M' T)" "\<And>x. 0 \<le> f x"
   8.718 +  shows "integral\<^isup>P (distr M M' T) f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
   8.719 +  using f 
   8.720 +proof induct
   8.721 +  case (cong f g)
   8.722 +  with T show ?case
   8.723 +    apply (subst positive_integral_cong[of _ f g])
   8.724 +    apply simp
   8.725 +    apply (subst positive_integral_cong[of _ "\<lambda>x. f (T x)" "\<lambda>x. g (T x)"])
   8.726 +    apply (simp add: measurable_def Pi_iff)
   8.727 +    apply simp
   8.728 +    done
   8.729 +next
   8.730 +  case (set A)
   8.731 +  then have eq: "\<And>x. x \<in> space M \<Longrightarrow> indicator A (T x) = indicator (T -` A \<inter> space M) x"
   8.732 +    by (auto simp: indicator_def)
   8.733 +  from set T show ?case
   8.734 +    by (subst positive_integral_cong[OF eq])
   8.735 +       (auto simp add: emeasure_distr intro!: positive_integral_indicator[symmetric] measurable_sets)
   8.736 +qed (simp_all add: measurable_compose[OF T] T positive_integral_cmult positive_integral_add
   8.737 +                   positive_integral_monotone_convergence_SUP le_fun_def incseq_def)
   8.738  
   8.739  lemma positive_integral_distr:
   8.740    assumes T: "T \<in> measurable M M'"
   8.741    and f: "f \<in> borel_measurable M'"
   8.742    shows "integral\<^isup>P (distr M M' T) f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
   8.743 -proof -
   8.744 -  from borel_measurable_implies_simple_function_sequence'[OF f]
   8.745 -  guess f' . note f' = this
   8.746 -  then have f_distr: "\<And>i. simple_function (distr M M' T) (f' i)"
   8.747 -    by simp
   8.748 -  let ?f = "\<lambda>i x. f' i (T x)"
   8.749 -  have inc: "incseq ?f" using f' by (force simp: le_fun_def incseq_def)
   8.750 -  have sup: "\<And>x. (SUP i. ?f i x) = max 0 (f (T x))"
   8.751 -    using f'(4) .
   8.752 -  have sf: "\<And>i. simple_function M (\<lambda>x. f' i (T x))"
   8.753 -    using simple_function_comp[OF T(1) f'(1)] .
   8.754 -  show "integral\<^isup>P (distr M M' T) f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
   8.755 -    using
   8.756 -      positive_integral_monotone_convergence_simple[OF f'(2,5) f_distr]
   8.757 -      positive_integral_monotone_convergence_simple[OF inc f'(5) sf]
   8.758 -    by (simp add: positive_integral_max_0 simple_integral_distr[OF T f'(1)] f')
   8.759 -qed
   8.760 +  apply (subst (1 2) positive_integral_max_0[symmetric])
   8.761 +  apply (rule positive_integral_distr')
   8.762 +  apply (auto simp: f T)
   8.763 +  done
   8.764  
   8.765  lemma integral_distr:
   8.766 -  assumes T: "T \<in> measurable M M'"
   8.767 -  assumes f: "f \<in> borel_measurable M'"
   8.768 -  shows "integral\<^isup>L (distr M M' T) f = (\<integral>x. f (T x) \<partial>M)"
   8.769 -proof -
   8.770 -  from measurable_comp[OF T, of f borel]
   8.771 -  have borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable M'" "(\<lambda>x. ereal (- f x)) \<in> borel_measurable M'"
   8.772 -    and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
   8.773 -    using f by (auto simp: comp_def)
   8.774 -  then show ?thesis
   8.775 -    using f unfolding lebesgue_integral_def integrable_def
   8.776 -    by (auto simp: borel[THEN positive_integral_distr[OF T]])
   8.777 -qed
   8.778 +  "T \<in> measurable M M' \<Longrightarrow> f \<in> borel_measurable M' \<Longrightarrow> integral\<^isup>L (distr M M' T) f = (\<integral> x. f (T x) \<partial>M)"
   8.779 +  unfolding lebesgue_integral_def
   8.780 +  by (subst (1 2) positive_integral_distr) auto
   8.781 +
   8.782 +lemma integrable_distr_eq:
   8.783 +  assumes T: "T \<in> measurable M M'" "f \<in> borel_measurable M'"
   8.784 +  shows "integrable (distr M M' T) f \<longleftrightarrow> integrable M (\<lambda>x. f (T x))"
   8.785 +  using T measurable_comp[OF T]
   8.786 +  unfolding integrable_def 
   8.787 +  by (subst (1 2) positive_integral_distr) (auto simp: comp_def)
   8.788  
   8.789  lemma integrable_distr:
   8.790 -  assumes T: "T \<in> measurable M M'" and f: "integrable (distr M M' T) f"
   8.791 -  shows "integrable M (\<lambda>x. f (T x))"
   8.792 -proof -
   8.793 -  from measurable_comp[OF T, of f borel]
   8.794 -  have borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable M'" "(\<lambda>x. ereal (- f x)) \<in> borel_measurable M'"
   8.795 -    and "(\<lambda>x. f (T x)) \<in> borel_measurable M"
   8.796 -    using f by (auto simp: comp_def)
   8.797 -  then show ?thesis
   8.798 -    using f unfolding lebesgue_integral_def integrable_def
   8.799 -    using borel[THEN positive_integral_distr[OF T]]
   8.800 -    by (auto simp: borel[THEN positive_integral_distr[OF T]])
   8.801 -qed
   8.802 +  assumes T: "T \<in> measurable M M'" shows "integrable (distr M M' T) f \<Longrightarrow> integrable M (\<lambda>x. f (T x))"
   8.803 +  by (subst integrable_distr_eq[symmetric, OF T]) auto
   8.804  
   8.805  section {* Lebesgue integration on @{const count_space} *}
   8.806  
   8.807 @@ -2329,6 +2367,26 @@
   8.808    by (subst positive_integral_max_0[symmetric])
   8.809       (auto intro!: setsum_mono_zero_left simp: positive_integral_count_space less_le)
   8.810  
   8.811 +lemma lebesgue_integral_count_space_finite_support:
   8.812 +  assumes f: "finite {a\<in>A. f a \<noteq> 0}" shows "(\<integral>x. f x \<partial>count_space A) = (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. f a)"
   8.813 +proof -
   8.814 +  have *: "\<And>r::real. 0 < max 0 r \<longleftrightarrow> 0 < r" "\<And>x. max 0 (ereal x) = ereal (max 0 x)"
   8.815 +    "\<And>a. a \<in> A \<and> 0 < f a \<Longrightarrow> max 0 (f a) = f a"
   8.816 +    "\<And>a. a \<in> A \<and> f a < 0 \<Longrightarrow> max 0 (- f a) = - f a"
   8.817 +    "{a \<in> A. f a \<noteq> 0} = {a \<in> A. 0 < f a} \<union> {a \<in> A. f a < 0}"
   8.818 +    "({a \<in> A. 0 < f a} \<inter> {a \<in> A. f a < 0}) = {}"
   8.819 +    by (auto split: split_max)
   8.820 +  have "finite {a \<in> A. 0 < f a}" "finite {a \<in> A. f a < 0}"
   8.821 +    by (auto intro: finite_subset[OF _ f])
   8.822 +  then show ?thesis
   8.823 +    unfolding lebesgue_integral_def
   8.824 +    apply (subst (1 2) positive_integral_max_0[symmetric])
   8.825 +    apply (subst (1 2) positive_integral_count_space)
   8.826 +    apply (auto simp add: * setsum_negf setsum_Un
   8.827 +                simp del: ereal_max)
   8.828 +    done
   8.829 +qed
   8.830 +
   8.831  lemma lebesgue_integral_count_space_finite:
   8.832      "finite A \<Longrightarrow> (\<integral>x. f x \<partial>count_space A) = (\<Sum>a\<in>A. f a)"
   8.833    apply (auto intro!: setsum_mono_zero_left
   8.834 @@ -2337,6 +2395,10 @@
   8.835    apply (auto simp: max_def setsum_subtractf[symmetric] intro!: setsum_cong)
   8.836    done
   8.837  
   8.838 +lemma borel_measurable_count_space[simp, intro!]:
   8.839 +  "f \<in> borel_measurable (count_space A)"
   8.840 +  by simp
   8.841 +
   8.842  section {* Measure spaces with an associated density *}
   8.843  
   8.844  definition density :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
   8.845 @@ -2449,65 +2511,50 @@
   8.846         (auto elim: eventually_elim2)
   8.847  qed
   8.848  
   8.849 -lemma positive_integral_density:
   8.850 +lemma positive_integral_density':
   8.851    assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
   8.852 -  assumes g': "g' \<in> borel_measurable M"
   8.853 -  shows "integral\<^isup>P (density M f) g' = (\<integral>\<^isup>+ x. f x * g' x \<partial>M)"
   8.854 -proof -
   8.855 -  def g \<equiv> "\<lambda>x. max 0 (g' x)"
   8.856 -  then have g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
   8.857 -    using g' by auto
   8.858 -  from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess G . note G = this
   8.859 -  note G' = borel_measurable_simple_function[OF this(1)] simple_functionD[OF G(1)]
   8.860 -  note G'(2)[simp]
   8.861 -  { fix P have "AE x in M. P x \<Longrightarrow> AE x in M. P x"
   8.862 -      using positive_integral_null_set[of _ _ f]
   8.863 -      by (auto simp: eventually_ae_filter ) }
   8.864 -  note ac = this
   8.865 -  with G(4) f g have G_M': "AE x in density M f. (SUP i. G i x) = g x"
   8.866 -    by (auto simp add: AE_density[OF f(1)] max_def)
   8.867 -  { fix i
   8.868 -    let ?I = "\<lambda>y x. indicator (G i -` {y} \<inter> space M) x"
   8.869 -    { fix x assume *: "x \<in> space M" "0 \<le> f x" "0 \<le> g x"
   8.870 -      then have [simp]: "G i ` space M \<inter> {y. G i x = y \<and> x \<in> space M} = {G i x}" by auto
   8.871 -      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))"
   8.872 -        by (subst setsum_ereal_right_distrib) (auto simp: ac_simps)
   8.873 -      also have "\<dots> = f x * G i x"
   8.874 -        by (simp add: indicator_def if_distrib setsum_cases)
   8.875 -      finally have "(\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) = f x * G i x" . }
   8.876 -    note to_singleton = this
   8.877 -    have "integral\<^isup>P (density M f) (G i) = integral\<^isup>S (density M f) (G i)"
   8.878 -      using G by (intro positive_integral_eq_simple_integral) simp_all
   8.879 -    also have "\<dots> = (\<Sum>y\<in>G i`space M. y * (\<integral>\<^isup>+x. f x * ?I y x \<partial>M))"
   8.880 -      using f G(1)
   8.881 -      by (auto intro!: setsum_cong arg_cong2[where f="op *"] emeasure_density
   8.882 -               simp: simple_function_def simple_integral_def)
   8.883 -    also have "\<dots> = (\<Sum>y\<in>G i`space M. (\<integral>\<^isup>+x. y * (f x * ?I y x) \<partial>M))"
   8.884 -      using f G' G by (auto intro!: setsum_cong positive_integral_cmult[symmetric])
   8.885 -    also have "\<dots> = (\<integral>\<^isup>+x. (\<Sum>y\<in>G i`space M. y * (f x * ?I y x)) \<partial>M)"
   8.886 -      using f G' G by (auto intro!: positive_integral_setsum[symmetric])
   8.887 -    finally have "integral\<^isup>P (density M f) (G i) = (\<integral>\<^isup>+x. f x * G i x \<partial>M)"
   8.888 -      using f g G' to_singleton by (auto intro!: positive_integral_cong_AE) }
   8.889 -  note [simp] = this
   8.890 -  have "integral\<^isup>P (density M f) g = (SUP i. integral\<^isup>P (density M f) (G i))" using G'(1) G_M'(1) G
   8.891 -    using positive_integral_monotone_convergence_SUP[symmetric, OF `incseq G`, of "density M f"]
   8.892 -    by (simp cong: positive_integral_cong_AE)
   8.893 -  also have "\<dots> = (SUP i. (\<integral>\<^isup>+x. f x * G i x \<partial>M))" by simp
   8.894 -  also have "\<dots> = (\<integral>\<^isup>+x. (SUP i. f x * G i x) \<partial>M)"
   8.895 -    using f G' G(2)[THEN incseq_SucD] G
   8.896 -    by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
   8.897 -       (auto simp: ereal_mult_left_mono le_fun_def ereal_zero_le_0_iff)
   8.898 -  also have "\<dots> = (\<integral>\<^isup>+x. f x * g x \<partial>M)" using f G' G g
   8.899 -    by (intro positive_integral_cong_AE)
   8.900 -       (auto simp add: SUPR_ereal_cmult split: split_max)
   8.901 -  also have "\<dots> = (\<integral>\<^isup>+x. f x * g' x \<partial>M)"
   8.902 -    using f(2)
   8.903 -    by (subst (2) positive_integral_max_0[symmetric])
   8.904 -       (auto simp: g_def max_def ereal_zero_le_0_iff intro!: positive_integral_cong_AE)
   8.905 -  finally show "integral\<^isup>P (density M f) g' = (\<integral>\<^isup>+x. f x * g' x \<partial>M)"
   8.906 -    unfolding g_def positive_integral_max_0 .
   8.907 +  assumes g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
   8.908 +  shows "integral\<^isup>P (density M f) g = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
   8.909 +using g proof induct
   8.910 +  case (cong u v)
   8.911 +  then show ?case
   8.912 +    apply (subst positive_integral_cong[OF cong(3)])
   8.913 +    apply (simp_all cong: positive_integral_cong)
   8.914 +    done
   8.915 +next
   8.916 +  case (set A) then show ?case
   8.917 +    by (simp add: emeasure_density f)
   8.918 +next
   8.919 +  case (mult u c)
   8.920 +  moreover have "\<And>x. f x * (c * u x) = c * (f x * u x)" by (simp add: field_simps)
   8.921 +  ultimately show ?case
   8.922 +    by (simp add: f positive_integral_cmult)
   8.923 +next
   8.924 +  case (add u v)
   8.925 +  moreover then have "\<And>x. f x * (v x + u x) = f x * v x + f x * u x"
   8.926 +    by (simp add: ereal_right_distrib)
   8.927 +  moreover note f
   8.928 +  ultimately show ?case
   8.929 +    by (auto simp add: positive_integral_add ereal_zero_le_0_iff intro!: positive_integral_add[symmetric])
   8.930 +next
   8.931 +  case (seq U)
   8.932 +  from f(2) have eq: "AE x in M. f x * (SUP i. U i x) = (SUP i. f x * U i x)"
   8.933 +    by eventually_elim (simp add: SUPR_ereal_cmult seq)
   8.934 +  from seq f show ?case
   8.935 +    apply (simp add: positive_integral_monotone_convergence_SUP)
   8.936 +    apply (subst positive_integral_cong_AE[OF eq])
   8.937 +    apply (subst positive_integral_monotone_convergence_SUP_AE)
   8.938 +    apply (auto simp: incseq_def le_fun_def intro!: ereal_mult_left_mono)
   8.939 +    done
   8.940  qed
   8.941  
   8.942 +lemma positive_integral_density:
   8.943 +  "f \<in> borel_measurable M \<Longrightarrow> AE x in M. 0 \<le> f x \<Longrightarrow> g' \<in> borel_measurable M \<Longrightarrow> 
   8.944 +    integral\<^isup>P (density M f) g' = (\<integral>\<^isup>+ x. f x * g' x \<partial>M)"
   8.945 +  by (subst (1 2) positive_integral_max_0[symmetric])
   8.946 +     (auto intro!: positive_integral_cong_AE
   8.947 +           simp: measurable_If max_def ereal_zero_le_0_iff positive_integral_density')
   8.948 +
   8.949  lemma integral_density:
   8.950    assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
   8.951      and g: "g \<in> borel_measurable M"
   8.952 @@ -2615,9 +2662,14 @@
   8.953  qed
   8.954  
   8.955  lemma emeasure_point_measure_finite:
   8.956 -  "finite A \<Longrightarrow> (\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> X \<subseteq> A \<Longrightarrow> emeasure (point_measure A f) X = (\<Sum>a|a\<in>X. f a)"
   8.957 +  "finite A \<Longrightarrow> (\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> X \<subseteq> A \<Longrightarrow> emeasure (point_measure A f) X = (\<Sum>a\<in>X. f a)"
   8.958    by (subst emeasure_point_measure) (auto dest: finite_subset intro!: setsum_mono_zero_left simp: less_le)
   8.959  
   8.960 +lemma emeasure_point_measure_finite2:
   8.961 +  "X \<subseteq> A \<Longrightarrow> finite X \<Longrightarrow> (\<And>i. i \<in> X \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> emeasure (point_measure A f) X = (\<Sum>a\<in>X. f a)"
   8.962 +  by (subst emeasure_point_measure)
   8.963 +     (auto dest: finite_subset intro!: setsum_mono_zero_left simp: less_le)
   8.964 +
   8.965  lemma null_sets_point_measure_iff:
   8.966    "X \<in> null_sets (point_measure A f) \<longleftrightarrow> X \<subseteq> A \<and> (\<forall>x\<in>X. f x \<le> 0)"
   8.967   by (auto simp: AE_count_space null_sets_density_iff point_measure_def)
     9.1 --- a/src/HOL/Probability/Lebesgue_Measure.thy	Wed Oct 10 15:16:44 2012 +0200
     9.2 +++ b/src/HOL/Probability/Lebesgue_Measure.thy	Wed Oct 10 15:17:18 2012 +0200
     9.3 @@ -9,20 +9,23 @@
     9.4    imports Finite_Product_Measure
     9.5  begin
     9.6  
     9.7 +lemma borel_measurable_indicator':
     9.8 +  "A \<in> sets borel \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. indicator A (f x)) \<in> borel_measurable M"
     9.9 +  using measurable_comp[OF _ borel_measurable_indicator, of f M borel A] by (auto simp add: comp_def)
    9.10 +
    9.11  lemma borel_measurable_sets:
    9.12    assumes "f \<in> measurable borel M" "A \<in> sets M"
    9.13    shows "f -` A \<in> sets borel"
    9.14    using measurable_sets[OF assms] by simp
    9.15  
    9.16 -lemma measurable_identity[intro,simp]:
    9.17 -  "(\<lambda>x. x) \<in> measurable M M"
    9.18 -  unfolding measurable_def by auto
    9.19 -
    9.20  subsection {* Standard Cubes *}
    9.21  
    9.22  definition cube :: "nat \<Rightarrow> 'a::ordered_euclidean_space set" where
    9.23    "cube n \<equiv> {\<chi>\<chi> i. - real n .. \<chi>\<chi> i. real n}"
    9.24  
    9.25 +lemma borel_cube[intro]: "cube n \<in> sets borel"
    9.26 +  unfolding cube_def by auto
    9.27 +
    9.28  lemma cube_closed[intro]: "closed (cube n)"
    9.29    unfolding cube_def by auto
    9.30  
    9.31 @@ -154,7 +157,7 @@
    9.32      then have UN_A[simp, intro]: "\<And>i n. (indicator (\<Union>i. A i) :: _ \<Rightarrow> real) integrable_on cube n"
    9.33        by (auto simp: sets_lebesgue)
    9.34      show "(\<Sum>n. ?\<mu> (A n)) = ?\<mu> (\<Union>i. A i)"
    9.35 -    proof (subst suminf_SUP_eq, safe intro!: incseq_SucI)
    9.36 +    proof (subst suminf_SUP_eq, safe intro!: incseq_SucI) 
    9.37        fix i n show "ereal (?m n i) \<le> ereal (?m (Suc n) i)"
    9.38          using cube_subset[of n "Suc n"] by (auto intro!: integral_subset_le incseq_SucI)
    9.39      next
    9.40 @@ -193,7 +196,6 @@
    9.41        qed
    9.42      qed
    9.43    qed
    9.44 -next
    9.45  qed (auto, fact)
    9.46  
    9.47  lemma has_integral_interval_cube:
    9.48 @@ -279,14 +281,16 @@
    9.49  
    9.50  lemma lmeasure_finite_has_integral:
    9.51    fixes s :: "'a::ordered_euclidean_space set"
    9.52 -  assumes "s \<in> sets lebesgue" "emeasure lebesgue s = ereal m" "0 \<le> m"
    9.53 +  assumes "s \<in> sets lebesgue" "emeasure lebesgue s = ereal m"
    9.54    shows "(indicator s has_integral m) UNIV"
    9.55  proof -
    9.56    let ?I = "indicator :: 'a set \<Rightarrow> 'a \<Rightarrow> real"
    9.57 +  have "0 \<le> m"
    9.58 +    using emeasure_nonneg[of lebesgue s] `emeasure lebesgue s = ereal m` by simp
    9.59    have **: "(?I s) integrable_on UNIV \<and> (\<lambda>k. integral UNIV (?I (s \<inter> cube k))) ----> integral UNIV (?I s)"
    9.60    proof (intro monotone_convergence_increasing allI ballI)
    9.61      have LIMSEQ: "(\<lambda>n. integral (cube n) (?I s)) ----> m"
    9.62 -      using assms(2) unfolding lmeasure_iff_LIMSEQ[OF assms(1, 3)] .
    9.63 +      using assms(2) unfolding lmeasure_iff_LIMSEQ[OF assms(1) `0 \<le> m`] .
    9.64      { fix n have "integral (cube n) (?I s) \<le> m"
    9.65          using cube_subset assms
    9.66          by (intro incseq_le[where L=m] LIMSEQ incseq_def[THEN iffD2] integral_subset_le allI impI)
    9.67 @@ -316,7 +320,7 @@
    9.68    note ** = conjunctD2[OF this]
    9.69    have m: "m = integral UNIV (?I s)"
    9.70      apply (intro LIMSEQ_unique[OF _ **(2)])
    9.71 -    using assms(2) unfolding lmeasure_iff_LIMSEQ[OF assms(1,3)] integral_indicator_UNIV .
    9.72 +    using assms(2) unfolding lmeasure_iff_LIMSEQ[OF assms(1) `0 \<le> m`] integral_indicator_UNIV .
    9.73    show ?thesis
    9.74      unfolding m by (intro integrable_integral **)
    9.75  qed
    9.76 @@ -366,14 +370,14 @@
    9.77  qed
    9.78  
    9.79  lemma has_integral_iff_lmeasure:
    9.80 -  "(indicator A has_integral m) UNIV \<longleftrightarrow> (A \<in> sets lebesgue \<and> 0 \<le> m \<and> emeasure lebesgue A = ereal m)"
    9.81 +  "(indicator A has_integral m) UNIV \<longleftrightarrow> (A \<in> sets lebesgue \<and> emeasure lebesgue A = ereal m)"
    9.82  proof
    9.83    assume "(indicator A has_integral m) UNIV"
    9.84    with has_integral_lmeasure[OF this] has_integral_lebesgue[OF this]
    9.85 -  show "A \<in> sets lebesgue \<and> 0 \<le> m \<and> emeasure lebesgue A = ereal m"
    9.86 +  show "A \<in> sets lebesgue \<and> emeasure lebesgue A = ereal m"
    9.87      by (auto intro: has_integral_nonneg)
    9.88  next
    9.89 -  assume "A \<in> sets lebesgue \<and> 0 \<le> m \<and> emeasure lebesgue A = ereal m"
    9.90 +  assume "A \<in> sets lebesgue \<and> emeasure lebesgue A = ereal m"
    9.91    then show "(indicator A has_integral m) UNIV" by (intro lmeasure_finite_has_integral) auto
    9.92  qed
    9.93  
    9.94 @@ -450,6 +454,9 @@
    9.95      by (auto simp: cube_def content_closed_interval_cases setprod_constant)
    9.96  qed simp
    9.97  
    9.98 +lemma lmeasure_complete: "A \<subseteq> B \<Longrightarrow> B \<in> null_sets lebesgue \<Longrightarrow> A \<in> null_sets lebesgue"
    9.99 +  unfolding negligible_iff_lebesgue_null_sets[symmetric] by (auto simp: negligible_subset)
   9.100 +
   9.101  lemma
   9.102    fixes a b ::"'a::ordered_euclidean_space"
   9.103    shows lmeasure_atLeastAtMost[simp]: "emeasure lebesgue {a..b} = ereal (content {a..b})"
   9.104 @@ -475,43 +482,44 @@
   9.105    fixes a :: "'a::ordered_euclidean_space" shows "emeasure lebesgue {a} = 0"
   9.106    using lmeasure_atLeastAtMost[of a a] by simp
   9.107  
   9.108 +lemma AE_lebesgue_singleton:
   9.109 +  fixes a :: "'a::ordered_euclidean_space" shows "AE x in lebesgue. x \<noteq> a"
   9.110 +  by (rule AE_I[where N="{a}"]) auto
   9.111 +
   9.112  declare content_real[simp]
   9.113  
   9.114  lemma
   9.115    fixes a b :: real
   9.116    shows lmeasure_real_greaterThanAtMost[simp]:
   9.117      "emeasure lebesgue {a <.. b} = ereal (if a \<le> b then b - a else 0)"
   9.118 -proof cases
   9.119 -  assume "a < b"
   9.120 -  then have "emeasure lebesgue {a <.. b} = emeasure lebesgue {a .. b} - emeasure lebesgue {a}"
   9.121 -    by (subst emeasure_Diff[symmetric])
   9.122 -       (auto intro!: arg_cong[where f="emeasure lebesgue"])
   9.123 +proof -
   9.124 +  have "emeasure lebesgue {a <.. b} = emeasure lebesgue {a .. b}"
   9.125 +    using AE_lebesgue_singleton[of a]
   9.126 +    by (intro emeasure_eq_AE) auto
   9.127    then show ?thesis by auto
   9.128 -qed auto
   9.129 +qed
   9.130  
   9.131  lemma
   9.132    fixes a b :: real
   9.133    shows lmeasure_real_atLeastLessThan[simp]:
   9.134      "emeasure lebesgue {a ..< b} = ereal (if a \<le> b then b - a else 0)"
   9.135 -proof cases
   9.136 -  assume "a < b"
   9.137 -  then have "emeasure lebesgue {a ..< b} = emeasure lebesgue {a .. b} - emeasure lebesgue {b}"
   9.138 -    by (subst emeasure_Diff[symmetric])
   9.139 -       (auto intro!: arg_cong[where f="emeasure lebesgue"])
   9.140 +proof -
   9.141 +  have "emeasure lebesgue {a ..< b} = emeasure lebesgue {a .. b}"
   9.142 +    using AE_lebesgue_singleton[of b]
   9.143 +    by (intro emeasure_eq_AE) auto
   9.144    then show ?thesis by auto
   9.145 -qed auto
   9.146 +qed
   9.147  
   9.148  lemma
   9.149    fixes a b :: real
   9.150    shows lmeasure_real_greaterThanLessThan[simp]:
   9.151      "emeasure lebesgue {a <..< b} = ereal (if a \<le> b then b - a else 0)"
   9.152 -proof cases
   9.153 -  assume "a < b"
   9.154 -  then have "emeasure lebesgue {a <..< b} = emeasure lebesgue {a <.. b} - emeasure lebesgue {b}"
   9.155 -    by (subst emeasure_Diff[symmetric])
   9.156 -       (auto intro!: arg_cong[where f="emeasure lebesgue"])
   9.157 +proof -
   9.158 +  have "emeasure lebesgue {a <..< b} = emeasure lebesgue {a .. b}"
   9.159 +    using AE_lebesgue_singleton[of a] AE_lebesgue_singleton[of b]
   9.160 +    by (intro emeasure_eq_AE) auto
   9.161    then show ?thesis by auto
   9.162 -qed auto
   9.163 +qed
   9.164  
   9.165  subsection {* Lebesgue-Borel measure *}
   9.166  
   9.167 @@ -544,6 +552,61 @@
   9.168      by (intro exI[of _ A]) (auto simp: subset_eq)
   9.169  qed
   9.170  
   9.171 +lemma Int_stable_atLeastAtMost:
   9.172 +  fixes x::"'a::ordered_euclidean_space"
   9.173 +  shows "Int_stable (range (\<lambda>(a, b::'a). {a..b}))"
   9.174 +  by (auto simp: inter_interval Int_stable_def)
   9.175 +
   9.176 +lemma lborel_eqI:
   9.177 +  fixes M :: "'a::ordered_euclidean_space measure"
   9.178 +  assumes emeasure_eq: "\<And>a b. emeasure M {a .. b} = content {a .. b}"
   9.179 +  assumes sets_eq: "sets M = sets borel"
   9.180 +  shows "lborel = M"
   9.181 +proof (rule measure_eqI_generator_eq[OF Int_stable_atLeastAtMost])
   9.182 +  let ?P = "\<Pi>\<^isub>M i\<in>{..<DIM('a::ordered_euclidean_space)}. lborel"
   9.183 +  let ?E = "range (\<lambda>(a, b). {a..b} :: 'a set)"
   9.184 +  show "?E \<subseteq> Pow UNIV" "sets lborel = sigma_sets UNIV ?E" "sets M = sigma_sets UNIV ?E"
   9.185 +    by (simp_all add: borel_eq_atLeastAtMost sets_eq)
   9.186 +
   9.187 +  show "range cube \<subseteq> ?E" unfolding cube_def [abs_def] by auto
   9.188 +  { fix x :: 'a have "\<exists>n. x \<in> cube n" using mem_big_cube[of x] by fastforce }
   9.189 +  then show "(\<Union>i. cube i :: 'a set) = UNIV" by auto
   9.190 +
   9.191 +  { fix i show "emeasure lborel (cube i) \<noteq> \<infinity>" unfolding cube_def by auto }
   9.192 +  { fix X assume "X \<in> ?E" then show "emeasure lborel X = emeasure M X"
   9.193 +      by (auto simp: emeasure_eq) }
   9.194 +qed
   9.195 +
   9.196 +lemma lebesgue_real_affine:
   9.197 +  fixes c :: real assumes "c \<noteq> 0"
   9.198 +  shows "lborel = density (distr lborel borel (\<lambda>x. t + c * x)) (\<lambda>_. \<bar>c\<bar>)" (is "_ = ?D")
   9.199 +proof (rule lborel_eqI)
   9.200 +  fix a b show "emeasure ?D {a..b} = content {a .. b}"
   9.201 +  proof cases
   9.202 +    assume "0 < c"
   9.203 +    then have "(\<lambda>x. t + c * x) -` {a..b} = {(a - t) / c .. (b - t) / c}"
   9.204 +      by (auto simp: field_simps)
   9.205 +    with `0 < c` show ?thesis
   9.206 +      by (cases "a \<le> b")
   9.207 +         (auto simp: field_simps emeasure_density positive_integral_distr positive_integral_cmult
   9.208 +                     borel_measurable_indicator' emeasure_distr)
   9.209 +  next
   9.210 +    assume "\<not> 0 < c" with `c \<noteq> 0` have "c < 0" by auto
   9.211 +    then have *: "(\<lambda>x. t + c * x) -` {a..b} = {(b - t) / c .. (a - t) / c}"
   9.212 +      by (auto simp: field_simps)
   9.213 +    with `c < 0` show ?thesis
   9.214 +      by (cases "a \<le> b")
   9.215 +         (auto simp: field_simps emeasure_density positive_integral_distr
   9.216 +                     positive_integral_cmult borel_measurable_indicator' emeasure_distr)
   9.217 +  qed
   9.218 +qed simp
   9.219 +
   9.220 +lemma lebesgue_integral_real_affine:
   9.221 +  fixes c :: real assumes c: "c \<noteq> 0" and f: "f \<in> borel_measurable borel"
   9.222 +  shows "(\<integral> x. f x \<partial> lborel) = \<bar>c\<bar> * (\<integral> x. f (t + c * x) \<partial>lborel)"
   9.223 +  by (subst lebesgue_real_affine[OF c, of t])
   9.224 +     (simp add: f integral_density integral_distr lebesgue_integral_cmult)
   9.225 +
   9.226  subsection {* Lebesgue integrable implies Gauge integrable *}
   9.227  
   9.228  lemma has_integral_cmult_real:
   9.229 @@ -632,6 +695,69 @@
   9.230    qed
   9.231  qed
   9.232  
   9.233 +lemma borel_measurable_real_induct[consumes 2, case_names cong set mult add seq, induct set: borel_measurable]:
   9.234 +  fixes u :: "'a \<Rightarrow> real"
   9.235 +  assumes u: "u \<in> borel_measurable M" "\<And>x. 0 \<le> u x"
   9.236 +  assumes cong: "\<And>f g. f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> P g \<Longrightarrow> P f"
   9.237 +  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
   9.238 +  assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
   9.239 +  assumes add: "\<And>u v. u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> v \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
   9.240 +  assumes seq: "\<And>U u. (\<And>i. U i \<in> borel_measurable M) \<Longrightarrow> (\<And>i x. 0 \<le> U i x) \<Longrightarrow> (\<And>x. (\<lambda>i. U i x) ----> u x) \<Longrightarrow> (\<And>i. P (U i)) \<Longrightarrow> incseq U \<Longrightarrow> P u"
   9.241 +  shows "P u"
   9.242 +proof -
   9.243 +  have "(\<lambda>x. ereal (u x)) \<in> borel_measurable M"
   9.244 +    using u by auto
   9.245 +  then obtain U where U: "\<And>i. simple_function M (U i)" "incseq U" "\<And>i. \<infinity> \<notin> range (U i)" and
   9.246 +    "\<And>x. (SUP i. U i x) = max 0 (ereal (u x))" and nn: "\<And>i x. 0 \<le> U i x"
   9.247 +    using borel_measurable_implies_simple_function_sequence'[of u M] by auto
   9.248 +  then have u_eq: "\<And>x. ereal (u x) = (SUP i. U i x)"
   9.249 +    using u by (auto simp: max_def)
   9.250 +
   9.251 +  have [simp]: "\<And>i x. U i x \<noteq> \<infinity>" using U by (auto simp: image_def eq_commute)
   9.252 +
   9.253 +  { fix i x have [simp]: "U i x \<noteq> -\<infinity>" using nn[of i x] by auto }
   9.254 +  note this[simp]
   9.255 +
   9.256 +  show "P u"
   9.257 +  proof (rule seq)
   9.258 +    show "\<And>i. (\<lambda>x. real (U i x)) \<in> borel_measurable M"
   9.259 +      using U by (auto intro: borel_measurable_simple_function)
   9.260 +    show "\<And>i x. 0 \<le> real (U i x)"
   9.261 +      using nn by (auto simp: real_of_ereal_pos)
   9.262 +    show "incseq (\<lambda>i x. real (U i x))"
   9.263 +      using U(2) by (auto simp: incseq_def image_iff le_fun_def intro!: real_of_ereal_positive_mono nn)
   9.264 +    then show "\<And>x. (\<lambda>i. real (U i x)) ----> u x"
   9.265 +      by (intro SUP_eq_LIMSEQ[THEN iffD1])
   9.266 +         (auto simp: incseq_mono incseq_def le_fun_def u_eq ereal_real
   9.267 +               intro!: arg_cong2[where f=SUPR] ext)
   9.268 +    show "\<And>i. P (\<lambda>x. real (U i x))"
   9.269 +    proof (rule cong)
   9.270 +      fix x i assume x: "x \<in> space M"
   9.271 +      have [simp]: "\<And>A x. real (indicator A x :: ereal) = indicator A x"
   9.272 +        by (auto simp: indicator_def one_ereal_def)
   9.273 +      { fix y assume "y \<in> U i ` space M"
   9.274 +        then have "0 \<le> y" "y \<noteq> \<infinity>" using nn by auto
   9.275 +        then have "\<bar>y * indicator (U i -` {y} \<inter> space M) x\<bar> \<noteq> \<infinity>"
   9.276 +          by (auto simp: indicator_def) }
   9.277 +      then show "real (U i x) = (\<Sum>y \<in> U i ` space M. real y * indicator (U i -` {y} \<inter> space M) x)"
   9.278 +        unfolding simple_function_indicator_representation[OF U(1) x]
   9.279 +        by (subst setsum_real_of_ereal[symmetric]) auto
   9.280 +    next
   9.281 +      fix i
   9.282 +      have "finite (U i ` space M)" "\<And>x. x \<in> U i ` space M \<Longrightarrow> 0 \<le> x""\<And>x. x \<in> U i ` space M \<Longrightarrow> x \<noteq> \<infinity>"
   9.283 +        using U(1) nn by (auto simp: simple_function_def)
   9.284 +      then show "P (\<lambda>x. \<Sum>y \<in> U i ` space M. real y * indicator (U i -` {y} \<inter> space M) x)"
   9.285 +      proof induct
   9.286 +        case empty then show ?case
   9.287 +          using set[of "{}"] by (simp add: indicator_def[abs_def])
   9.288 +      qed (auto intro!: add mult set simple_functionD U setsum_nonneg borel_measurable_setsum mult_nonneg_nonneg real_of_ereal_pos)
   9.289 +    qed (auto intro: borel_measurable_simple_function U simple_functionD intro!: borel_measurable_setsum borel_measurable_times)
   9.290 +  qed
   9.291 +qed
   9.292 +
   9.293 +lemma ereal_indicator: "ereal (indicator A x) = indicator A x"
   9.294 +  by (auto simp: indicator_def one_ereal_def)
   9.295 +
   9.296  lemma positive_integral_has_integral:
   9.297    fixes f :: "'a::ordered_euclidean_space \<Rightarrow> ereal"
   9.298    assumes f: "f \<in> borel_measurable lebesgue" "range f \<subseteq> {0..<\<infinity>}" "integral\<^isup>P lebesgue f \<noteq> \<infinity>"
   9.299 @@ -777,201 +903,22 @@
   9.300      unfolding lebesgue_integral_eq_borel[OF borel] by simp
   9.301  qed
   9.302  
   9.303 -subsection {* Equivalence between product spaces and euclidean spaces *}
   9.304 -
   9.305 -definition e2p :: "'a::ordered_euclidean_space \<Rightarrow> (nat \<Rightarrow> real)" where
   9.306 -  "e2p x = (\<lambda>i\<in>{..<DIM('a)}. x$$i)"
   9.307 -
   9.308 -definition p2e :: "(nat \<Rightarrow> real) \<Rightarrow> 'a::ordered_euclidean_space" where
   9.309 -  "p2e x = (\<chi>\<chi> i. x i)"
   9.310 -
   9.311 -lemma e2p_p2e[simp]:
   9.312 -  "x \<in> extensional {..<DIM('a)} \<Longrightarrow> e2p (p2e x::'a::ordered_euclidean_space) = x"
   9.313 -  by (auto simp: fun_eq_iff extensional_def p2e_def e2p_def)
   9.314 -
   9.315 -lemma p2e_e2p[simp]:
   9.316 -  "p2e (e2p x) = (x::'a::ordered_euclidean_space)"
   9.317 -  by (auto simp: euclidean_eq[where 'a='a] p2e_def e2p_def)
   9.318 -
   9.319 -interpretation lborel_product: product_sigma_finite "\<lambda>x. lborel::real measure"
   9.320 -  by default
   9.321 -
   9.322 -interpretation lborel_space: finite_product_sigma_finite "\<lambda>x. lborel::real measure" "{..<n}" for n :: nat
   9.323 -  by default auto
   9.324 -
   9.325 -lemma bchoice_iff: "(\<forall>x\<in>A. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x\<in>A. P x (f x))"
   9.326 -  by metis
   9.327 -
   9.328 -lemma sets_product_borel:
   9.329 -  assumes I: "finite I"
   9.330 -  shows "sets (\<Pi>\<^isub>M i\<in>I. lborel) = sigma_sets (\<Pi>\<^isub>E i\<in>I. UNIV) { \<Pi>\<^isub>E i\<in>I. {..< x i :: real} | x. True}" (is "_ = ?G")
   9.331 -proof (subst sigma_prod_algebra_sigma_eq[where S="\<lambda>_ i::nat. {..<real i}" and E="\<lambda>_. range lessThan", OF I])
   9.332 -  show "sigma_sets (space (Pi\<^isub>M I (\<lambda>i. lborel))) {Pi\<^isub>E I F |F. \<forall>i\<in>I. F i \<in> range lessThan} = ?G"
   9.333 -    by (intro arg_cong2[where f=sigma_sets]) (auto simp: space_PiM image_iff bchoice_iff)
   9.334 -qed (auto simp: borel_eq_lessThan incseq_def reals_Archimedean2 image_iff intro: real_natceiling_ge)
   9.335 -
   9.336 -lemma measurable_e2p:
   9.337 -  "e2p \<in> measurable (borel::'a::ordered_euclidean_space measure) (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. (lborel :: real measure))"
   9.338 -proof (rule measurable_sigma_sets[OF sets_product_borel])
   9.339 -  fix A :: "(nat \<Rightarrow> real) set" assume "A \<in> {\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {..<x i} |x. True} "
   9.340 -  then obtain x where  "A = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {..<x i})" by auto
   9.341 -  then have "e2p -` A = {..< (\<chi>\<chi> i. x i) :: 'a}"
   9.342 -    using DIM_positive by (auto simp add: Pi_iff set_eq_iff e2p_def
   9.343 -      euclidean_eq[where 'a='a] eucl_less[where 'a='a])
   9.344 -  then show "e2p -` A \<inter> space (borel::'a measure) \<in> sets borel" by simp
   9.345 -qed (auto simp: e2p_def)
   9.346 -
   9.347 -lemma measurable_p2e:
   9.348 -  "p2e \<in> measurable (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. (lborel :: real measure))
   9.349 -    (borel :: 'a::ordered_euclidean_space measure)"
   9.350 -  (is "p2e \<in> measurable ?P _")
   9.351 -proof (safe intro!: borel_measurable_iff_halfspace_le[THEN iffD2])
   9.352 -  fix x i
   9.353 -  let ?A = "{w \<in> space ?P. (p2e w :: 'a) $$ i \<le> x}"
   9.354 -  assume "i < DIM('a)"
   9.355 -  then have "?A = (\<Pi>\<^isub>E j\<in>{..<DIM('a)}. if i = j then {.. x} else UNIV)"
   9.356 -    using DIM_positive by (auto simp: space_PiM p2e_def split: split_if_asm)
   9.357 -  then show "?A \<in> sets ?P"
   9.358 -    by auto
   9.359 -qed
   9.360 -
   9.361 -lemma Int_stable_atLeastAtMost:
   9.362 -  fixes x::"'a::ordered_euclidean_space"
   9.363 -  shows "Int_stable (range (\<lambda>(a, b::'a). {a..b}))"
   9.364 -  by (auto simp: inter_interval Int_stable_def)
   9.365 -
   9.366 -lemma lborel_eqI:
   9.367 -  fixes M :: "'a::ordered_euclidean_space measure"
   9.368 -  assumes emeasure_eq: "\<And>a b. emeasure M {a .. b} = content {a .. b}"
   9.369 -  assumes sets_eq: "sets M = sets borel"
   9.370 -  shows "lborel = M"
   9.371 -proof (rule measure_eqI_generator_eq[OF Int_stable_atLeastAtMost])
   9.372 -  let ?P = "\<Pi>\<^isub>M i\<in>{..<DIM('a::ordered_euclidean_space)}. lborel"
   9.373 -  let ?E = "range (\<lambda>(a, b). {a..b} :: 'a set)"
   9.374 -  show "?E \<subseteq> Pow UNIV" "sets lborel = sigma_sets UNIV ?E" "sets M = sigma_sets UNIV ?E"
   9.375 -    by (simp_all add: borel_eq_atLeastAtMost sets_eq)
   9.376 -
   9.377 -  show "range cube \<subseteq> ?E" unfolding cube_def [abs_def] by auto
   9.378 -  show "incseq cube" using cube_subset_Suc by (auto intro!: incseq_SucI)
   9.379 -  { fix x :: 'a have "\<exists>n. x \<in> cube n" using mem_big_cube[of x] by fastforce }
   9.380 -  then show "(\<Union>i. cube i :: 'a set) = UNIV" by auto
   9.381 -
   9.382 -  { fix i show "emeasure lborel (cube i) \<noteq> \<infinity>" unfolding cube_def by auto }
   9.383 -  { fix X assume "X \<in> ?E" then show "emeasure lborel X = emeasure M X"
   9.384 -      by (auto simp: emeasure_eq) }
   9.385 -qed
   9.386 -
   9.387 -lemma lborel_eq_lborel_space:
   9.388 -  "(lborel :: 'a measure) = distr (\<Pi>\<^isub>M i\<in>{..<DIM('a::ordered_euclidean_space)}. lborel) lborel p2e"
   9.389 -  (is "?B = ?D")
   9.390 -proof (rule lborel_eqI)
   9.391 -  show "sets ?D = sets borel" by simp
   9.392 -  let ?P = "(\<Pi>\<^isub>M i\<in>{..<DIM('a::ordered_euclidean_space)}. lborel)"
   9.393 -  fix a b :: 'a
   9.394 -  have *: "p2e -` {a .. b} \<inter> space ?P = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {a $$ i .. b $$ i})"
   9.395 -    by (auto simp: Pi_iff eucl_le[where 'a='a] p2e_def space_PiM)
   9.396 -  have "emeasure ?P (p2e -` {a..b} \<inter> space ?P) = content {a..b}"
   9.397 -  proof cases
   9.398 -    assume "{a..b} \<noteq> {}"
   9.399 -    then have "a \<le> b"
   9.400 -      by (simp add: interval_ne_empty eucl_le[where 'a='a])
   9.401 -    then have "emeasure lborel {a..b} = (\<Prod>x<DIM('a). emeasure lborel {a $$ x .. b $$ x})"
   9.402 -      by (auto simp: content_closed_interval eucl_le[where 'a='a]
   9.403 -               intro!: setprod_ereal[symmetric])
   9.404 -    also have "\<dots> = emeasure ?P (p2e -` {a..b} \<inter> space ?P)"
   9.405 -      unfolding * by (subst lborel_space.measure_times) auto
   9.406 -    finally show ?thesis by simp
   9.407 -  qed simp
   9.408 -  then show "emeasure ?D {a .. b} = content {a .. b}"
   9.409 -    by (simp add: emeasure_distr measurable_p2e)
   9.410 -qed
   9.411 -
   9.412 -lemma borel_fubini_positiv_integral:
   9.413 -  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> ereal"
   9.414 -  assumes f: "f \<in> borel_measurable borel"
   9.415 -  shows "integral\<^isup>P lborel f = \<integral>\<^isup>+x. f (p2e x) \<partial>(\<Pi>\<^isub>M i\<in>{..<DIM('a)}. lborel)"
   9.416 -  by (subst lborel_eq_lborel_space) (simp add: positive_integral_distr measurable_p2e f)
   9.417 -
   9.418 -lemma borel_fubini_integrable:
   9.419 -  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   9.420 -  shows "integrable lborel f \<longleftrightarrow>
   9.421 -    integrable (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. lborel) (\<lambda>x. f (p2e x))"
   9.422 -    (is "_ \<longleftrightarrow> integrable ?B ?f")
   9.423 -proof
   9.424 -  assume "integrable lborel f"
   9.425 -  moreover then have f: "f \<in> borel_measurable borel"
   9.426 -    by auto
   9.427 -  moreover with measurable_p2e
   9.428 -  have "f \<circ> p2e \<in> borel_measurable ?B"
   9.429 -    by (rule measurable_comp)
   9.430 -  ultimately show "integrable ?B ?f"
   9.431 -    by (simp add: comp_def borel_fubini_positiv_integral integrable_def)
   9.432 -next
   9.433 -  assume "integrable ?B ?f"
   9.434 -  moreover
   9.435 -  then have "?f \<circ> e2p \<in> borel_measurable (borel::'a measure)"
   9.436 -    by (auto intro!: measurable_e2p)
   9.437 -  then have "f \<in> borel_measurable borel"
   9.438 -    by (simp cong: measurable_cong)
   9.439 -  ultimately show "integrable lborel f"
   9.440 -    by (simp add: borel_fubini_positiv_integral integrable_def)
   9.441 -qed
   9.442 -
   9.443 -lemma borel_fubini:
   9.444 -  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   9.445 -  assumes f: "f \<in> borel_measurable borel"
   9.446 -  shows "integral\<^isup>L lborel f = \<integral>x. f (p2e x) \<partial>((\<Pi>\<^isub>M i\<in>{..<DIM('a)}. lborel))"
   9.447 -  using f by (simp add: borel_fubini_positiv_integral lebesgue_integral_def)
   9.448 -
   9.449 -lemma borel_measurable_indicator':
   9.450 -  "A \<in> sets borel \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. indicator A (f x)) \<in> borel_measurable M"
   9.451 -  using measurable_comp[OF _ borel_measurable_indicator, of f M borel A] by (auto simp add: comp_def)
   9.452 -
   9.453 -lemma lebesgue_real_affine:
   9.454 -  fixes c :: real assumes "c \<noteq> 0"
   9.455 -  shows "lborel = density (distr lborel borel (\<lambda>x. t + c * x)) (\<lambda>_. \<bar>c\<bar>)" (is "_ = ?D")
   9.456 -proof (rule lborel_eqI)
   9.457 -  fix a b show "emeasure ?D {a..b} = content {a .. b}"
   9.458 -  proof cases
   9.459 -    assume "0 < c"
   9.460 -    then have "(\<lambda>x. t + c * x) -` {a..b} = {(a - t) / c .. (b - t) / c}"
   9.461 -      by (auto simp: field_simps)
   9.462 -    with `0 < c` show ?thesis
   9.463 -      by (cases "a \<le> b")
   9.464 -         (auto simp: field_simps emeasure_density positive_integral_distr positive_integral_cmult
   9.465 -                     borel_measurable_indicator' emeasure_distr)
   9.466 -  next
   9.467 -    assume "\<not> 0 < c" with `c \<noteq> 0` have "c < 0" by auto
   9.468 -    then have *: "(\<lambda>x. t + c * x) -` {a..b} = {(b - t) / c .. (a - t) / c}"
   9.469 -      by (auto simp: field_simps)
   9.470 -    with `c < 0` show ?thesis
   9.471 -      by (cases "a \<le> b")
   9.472 -         (auto simp: field_simps emeasure_density positive_integral_distr
   9.473 -                     positive_integral_cmult borel_measurable_indicator' emeasure_distr)
   9.474 -  qed
   9.475 -qed simp
   9.476 -
   9.477 -lemma borel_cube[intro]: "cube n \<in> sets borel"
   9.478 -  unfolding cube_def by auto
   9.479 -
   9.480  lemma integrable_on_cmult_iff:
   9.481    fixes c :: real assumes "c \<noteq> 0"
   9.482    shows "(\<lambda>x. c * f x) integrable_on s \<longleftrightarrow> f integrable_on s"
   9.483    using integrable_cmul[of "\<lambda>x. c * f x" s "1 / c"] integrable_cmul[of f s c] `c \<noteq> 0`
   9.484    by auto
   9.485  
   9.486 -lemma positive_integral_borel_has_integral:
   9.487 +lemma positive_integral_lebesgue_has_integral:
   9.488    fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   9.489 -  assumes f_borel: "f \<in> borel_measurable borel" and nonneg: "\<And>x. 0 \<le> f x"
   9.490 +  assumes f_borel: "f \<in> borel_measurable lebesgue" and nonneg: "\<And>x. 0 \<le> f x"
   9.491    assumes I: "(f has_integral I) UNIV"
   9.492 -  shows "(\<integral>\<^isup>+x. f x \<partial>lborel) = I"
   9.493 +  shows "(\<integral>\<^isup>+x. f x \<partial>lebesgue) = I"
   9.494  proof -
   9.495 -  from f_borel have "(\<lambda>x. ereal (f x)) \<in> borel_measurable borel" by auto
   9.496 +  from f_borel have "(\<lambda>x. ereal (f x)) \<in> borel_measurable lebesgue" by auto
   9.497    from borel_measurable_implies_simple_function_sequence'[OF this] guess F . note F = this
   9.498  
   9.499 -  have lebesgue_eq: "(\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue) = (\<integral>\<^isup>+ x. ereal (f x) \<partial>lborel)"
   9.500 -    using f_borel by (intro lebesgue_positive_integral_eq_borel) auto
   9.501 -  also have "\<dots> = (SUP i. integral\<^isup>S lborel (F i))"
   9.502 +  have "(\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue) = (SUP i. integral\<^isup>S lebesgue (F i))"
   9.503      using F
   9.504      by (subst positive_integral_monotone_convergence_simple)
   9.505         (simp_all add: positive_integral_max_0 simple_function_def)
   9.506 @@ -1043,11 +990,8 @@
   9.507        unfolding simple_integral_def setsum_Pinfty space_lebesgue by blast
   9.508      moreover have "0 \<le> integral\<^isup>S lebesgue (F i)"
   9.509        using F(1,5) by (intro simple_integral_positive) (auto simp: simple_function_def)
   9.510 -    moreover have "integral\<^isup>S lebesgue (F i) = integral\<^isup>S lborel (F i)"
   9.511 -      using F(1)[of i, THEN borel_measurable_simple_function]
   9.512 -      by (rule lebesgue_simple_integral_eq_borel)
   9.513 -    ultimately show "integral\<^isup>S lborel (F i) \<le> ereal I"
   9.514 -      by (cases "integral\<^isup>S lborel (F i)") auto
   9.515 +    ultimately show "integral\<^isup>S lebesgue (F i) \<le> ereal I"
   9.516 +      by (cases "integral\<^isup>S lebesgue (F i)") auto
   9.517    qed
   9.518    also have "\<dots> < \<infinity>" by simp
   9.519    finally have finite: "(\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue) \<noteq> \<infinity>" by simp
   9.520 @@ -1059,14 +1003,142 @@
   9.521    with I have "I = real (\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue)"
   9.522      by (rule has_integral_unique)
   9.523    with finite positive_integral_positive[of _ "\<lambda>x. ereal (f x)"] show ?thesis
   9.524 -    by (cases "\<integral>\<^isup>+ x. ereal (f x) \<partial>lborel") (auto simp: lebesgue_eq)
   9.525 +    by (cases "\<integral>\<^isup>+ x. ereal (f x) \<partial>lebesgue") auto
   9.526  qed
   9.527  
   9.528 -lemma has_integral_iff_positive_integral:
   9.529 +lemma has_integral_iff_positive_integral_lebesgue:
   9.530 +  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   9.531 +  assumes f: "f \<in> borel_measurable lebesgue" "\<And>x. 0 \<le> f x"
   9.532 +  shows "(f has_integral I) UNIV \<longleftrightarrow> integral\<^isup>P lebesgue f = I"
   9.533 +  using f positive_integral_lebesgue_has_integral[of f I] positive_integral_has_integral[of f]
   9.534 +  by (auto simp: subset_eq)
   9.535 +
   9.536 +lemma has_integral_iff_positive_integral_lborel:
   9.537    fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   9.538    assumes f: "f \<in> borel_measurable borel" "\<And>x. 0 \<le> f x"
   9.539    shows "(f has_integral I) UNIV \<longleftrightarrow> integral\<^isup>P lborel f = I"
   9.540 -  using f positive_integral_borel_has_integral[of f I] positive_integral_has_integral[of f]
   9.541 -  by (auto simp: subset_eq borel_measurable_lebesgueI lebesgue_positive_integral_eq_borel)
   9.542 +  using assms
   9.543 +  by (subst has_integral_iff_positive_integral_lebesgue)
   9.544 +     (auto simp: borel_measurable_lebesgueI lebesgue_positive_integral_eq_borel)
   9.545 +
   9.546 +subsection {* Equivalence between product spaces and euclidean spaces *}
   9.547 +
   9.548 +definition e2p :: "'a::ordered_euclidean_space \<Rightarrow> (nat \<Rightarrow> real)" where
   9.549 +  "e2p x = (\<lambda>i\<in>{..<DIM('a)}. x$$i)"
   9.550 +
   9.551 +definition p2e :: "(nat \<Rightarrow> real) \<Rightarrow> 'a::ordered_euclidean_space" where
   9.552 +  "p2e x = (\<chi>\<chi> i. x i)"
   9.553 +
   9.554 +lemma e2p_p2e[simp]:
   9.555 +  "x \<in> extensional {..<DIM('a)} \<Longrightarrow> e2p (p2e x::'a::ordered_euclidean_space) = x"
   9.556 +  by (auto simp: fun_eq_iff extensional_def p2e_def e2p_def)
   9.557 +
   9.558 +lemma p2e_e2p[simp]:
   9.559 +  "p2e (e2p x) = (x::'a::ordered_euclidean_space)"
   9.560 +  by (auto simp: euclidean_eq[where 'a='a] p2e_def e2p_def)
   9.561 +
   9.562 +interpretation lborel_product: product_sigma_finite "\<lambda>x. lborel::real measure"
   9.563 +  by default
   9.564 +
   9.565 +interpretation lborel_space: finite_product_sigma_finite "\<lambda>x. lborel::real measure" "{..<n}" for n :: nat
   9.566 +  by default auto
   9.567 +
   9.568 +lemma bchoice_iff: "(\<forall>x\<in>A. \<exists>y. P x y) \<longleftrightarrow> (\<exists>f. \<forall>x\<in>A. P x (f x))"
   9.569 +  by metis
   9.570 +
   9.571 +lemma sets_product_borel:
   9.572 +  assumes I: "finite I"
   9.573 +  shows "sets (\<Pi>\<^isub>M i\<in>I. lborel) = sigma_sets (\<Pi>\<^isub>E i\<in>I. UNIV) { \<Pi>\<^isub>E i\<in>I. {..< x i :: real} | x. True}" (is "_ = ?G")
   9.574 +proof (subst sigma_prod_algebra_sigma_eq[where S="\<lambda>_ i::nat. {..<real i}" and E="\<lambda>_. range lessThan", OF I])
   9.575 +  show "sigma_sets (space (Pi\<^isub>M I (\<lambda>i. lborel))) {Pi\<^isub>E I F |F. \<forall>i\<in>I. F i \<in> range lessThan} = ?G"
   9.576 +    by (intro arg_cong2[where f=sigma_sets]) (auto simp: space_PiM image_iff bchoice_iff)
   9.577 +qed (auto simp: borel_eq_lessThan reals_Archimedean2)
   9.578 +
   9.579 +lemma measurable_e2p:
   9.580 +  "e2p \<in> measurable (borel::'a::ordered_euclidean_space measure) (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. (lborel :: real measure))"
   9.581 +proof (rule measurable_sigma_sets[OF sets_product_borel])
   9.582 +  fix A :: "(nat \<Rightarrow> real) set" assume "A \<in> {\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {..<x i} |x. True} "
   9.583 +  then obtain x where  "A = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {..<x i})" by auto
   9.584 +  then have "e2p -` A = {..< (\<chi>\<chi> i. x i) :: 'a}"
   9.585 +    using DIM_positive by (auto simp add: Pi_iff set_eq_iff e2p_def
   9.586 +      euclidean_eq[where 'a='a] eucl_less[where 'a='a])
   9.587 +  then show "e2p -` A \<inter> space (borel::'a measure) \<in> sets borel" by simp
   9.588 +qed (auto simp: e2p_def)
   9.589 +
   9.590 +lemma measurable_p2e:
   9.591 +  "p2e \<in> measurable (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. (lborel :: real measure))
   9.592 +    (borel :: 'a::ordered_euclidean_space measure)"
   9.593 +  (is "p2e \<in> measurable ?P _")
   9.594 +proof (safe intro!: borel_measurable_iff_halfspace_le[THEN iffD2])
   9.595 +  fix x i
   9.596 +  let ?A = "{w \<in> space ?P. (p2e w :: 'a) $$ i \<le> x}"
   9.597 +  assume "i < DIM('a)"
   9.598 +  then have "?A = (\<Pi>\<^isub>E j\<in>{..<DIM('a)}. if i = j then {.. x} else UNIV)"
   9.599 +    using DIM_positive by (auto simp: space_PiM p2e_def split: split_if_asm)
   9.600 +  then show "?A \<in> sets ?P"
   9.601 +    by auto
   9.602 +qed
   9.603 +
   9.604 +lemma lborel_eq_lborel_space:
   9.605 +  "(lborel :: 'a measure) = distr (\<Pi>\<^isub>M i\<in>{..<DIM('a::ordered_euclidean_space)}. lborel) borel p2e"
   9.606 +  (is "?B = ?D")
   9.607 +proof (rule lborel_eqI)
   9.608 +  show "sets ?D = sets borel" by simp
   9.609 +  let ?P = "(\<Pi>\<^isub>M i\<in>{..<DIM('a::ordered_euclidean_space)}. lborel)"
   9.610 +  fix a b :: 'a
   9.611 +  have *: "p2e -` {a .. b} \<inter> space ?P = (\<Pi>\<^isub>E i\<in>{..<DIM('a)}. {a $$ i .. b $$ i})"
   9.612 +    by (auto simp: Pi_iff eucl_le[where 'a='a] p2e_def space_PiM)
   9.613 +  have "emeasure ?P (p2e -` {a..b} \<inter> space ?P) = content {a..b}"
   9.614 +  proof cases
   9.615 +    assume "{a..b} \<noteq> {}"
   9.616 +    then have "a \<le> b"
   9.617 +      by (simp add: interval_ne_empty eucl_le[where 'a='a])
   9.618 +    then have "emeasure lborel {a..b} = (\<Prod>x<DIM('a). emeasure lborel {a $$ x .. b $$ x})"
   9.619 +      by (auto simp: content_closed_interval eucl_le[where 'a='a]
   9.620 +               intro!: setprod_ereal[symmetric])
   9.621 +    also have "\<dots> = emeasure ?P (p2e -` {a..b} \<inter> space ?P)"
   9.622 +      unfolding * by (subst lborel_space.measure_times) auto
   9.623 +    finally show ?thesis by simp
   9.624 +  qed simp
   9.625 +  then show "emeasure ?D {a .. b} = content {a .. b}"
   9.626 +    by (simp add: emeasure_distr measurable_p2e)
   9.627 +qed
   9.628 +
   9.629 +lemma borel_fubini_positiv_integral:
   9.630 +  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> ereal"
   9.631 +  assumes f: "f \<in> borel_measurable borel"
   9.632 +  shows "integral\<^isup>P lborel f = \<integral>\<^isup>+x. f (p2e x) \<partial>(\<Pi>\<^isub>M i\<in>{..<DIM('a)}. lborel)"
   9.633 +  by (subst lborel_eq_lborel_space) (simp add: positive_integral_distr measurable_p2e f)
   9.634 +
   9.635 +lemma borel_fubini_integrable:
   9.636 +  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   9.637 +  shows "integrable lborel f \<longleftrightarrow>
   9.638 +    integrable (\<Pi>\<^isub>M i\<in>{..<DIM('a)}. lborel) (\<lambda>x. f (p2e x))"
   9.639 +    (is "_ \<longleftrightarrow> integrable ?B ?f")
   9.640 +proof
   9.641 +  assume "integrable lborel f"
   9.642 +  moreover then have f: "f \<in> borel_measurable borel"
   9.643 +    by auto
   9.644 +  moreover with measurable_p2e
   9.645 +  have "f \<circ> p2e \<in> borel_measurable ?B"
   9.646 +    by (rule measurable_comp)
   9.647 +  ultimately show "integrable ?B ?f"
   9.648 +    by (simp add: comp_def borel_fubini_positiv_integral integrable_def)
   9.649 +next
   9.650 +  assume "integrable ?B ?f"
   9.651 +  moreover
   9.652 +  then have "?f \<circ> e2p \<in> borel_measurable (borel::'a measure)"
   9.653 +    by (auto intro!: measurable_e2p)
   9.654 +  then have "f \<in> borel_measurable borel"
   9.655 +    by (simp cong: measurable_cong)
   9.656 +  ultimately show "integrable lborel f"
   9.657 +    by (simp add: borel_fubini_positiv_integral integrable_def)
   9.658 +qed
   9.659 +
   9.660 +lemma borel_fubini:
   9.661 +  fixes f :: "'a::ordered_euclidean_space \<Rightarrow> real"
   9.662 +  assumes f: "f \<in> borel_measurable borel"
   9.663 +  shows "integral\<^isup>L lborel f = \<integral>x. f (p2e x) \<partial>((\<Pi>\<^isub>M i\<in>{..<DIM('a)}. lborel))"
   9.664 +  using f by (simp add: borel_fubini_positiv_integral lebesgue_integral_def)
   9.665  
   9.666  end
    10.1 --- a/src/HOL/Probability/Measure_Space.thy	Wed Oct 10 15:16:44 2012 +0200
    10.2 +++ b/src/HOL/Probability/Measure_Space.thy	Wed Oct 10 15:17:18 2012 +0200
    10.3 @@ -12,6 +12,12 @@
    10.4    "~~/src/HOL/Multivariate_Analysis/Extended_Real_Limits"
    10.5  begin
    10.6  
    10.7 +lemma sums_def2:
    10.8 +  "f sums x \<longleftrightarrow> (\<lambda>n. (\<Sum>i\<le>n. f i)) ----> x"
    10.9 +  unfolding sums_def
   10.10 +  apply (subst LIMSEQ_Suc_iff[symmetric])
   10.11 +  unfolding atLeastLessThanSuc_atLeastAtMost atLeast0AtMost ..
   10.12 +
   10.13  lemma suminf_cmult_indicator:
   10.14    fixes f :: "nat \<Rightarrow> ereal"
   10.15    assumes "disjoint_family A" "x \<in> A i" "\<And>i. 0 \<le> f i"
   10.16 @@ -90,6 +96,9 @@
   10.17  definition increasing where
   10.18    "increasing M \<mu> \<longleftrightarrow> (\<forall>x\<in>M. \<forall>y\<in>M. x \<subseteq> y \<longrightarrow> \<mu> x \<le> \<mu> y)"
   10.19  
   10.20 +lemma positiveD1: "positive M f \<Longrightarrow> f {} = 0" by (auto simp: positive_def)
   10.21 +lemma positiveD2: "positive M f \<Longrightarrow> A \<in> M \<Longrightarrow> 0 \<le> f A" by (auto simp: positive_def)
   10.22 +
   10.23  lemma positiveD_empty:
   10.24    "positive M f \<Longrightarrow> f {} = 0"
   10.25    by (auto simp add: positive_def)
   10.26 @@ -240,6 +249,143 @@
   10.27    finally show "(\<Sum>i. \<mu> (F i)) = \<mu> (\<Union>i. F i)" .
   10.28  qed
   10.29  
   10.30 +lemma (in ring_of_sets) countably_additive_iff_continuous_from_below:
   10.31 +  assumes f: "positive M f" "additive M f"
   10.32 +  shows "countably_additive M f \<longleftrightarrow>
   10.33 +    (\<forall>A. range A \<subseteq> M \<longrightarrow> incseq A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Union>i. A i))"
   10.34 +  unfolding countably_additive_def
   10.35 +proof safe
   10.36 +  assume count_sum: "\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> UNION UNIV A \<in> M \<longrightarrow> (\<Sum>i. f (A i)) = f (UNION UNIV A)"
   10.37 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
   10.38 +  then have dA: "range (disjointed A) \<subseteq> M" by (auto simp: range_disjointed_sets)
   10.39 +  with count_sum[THEN spec, of "disjointed A"] A(3)
   10.40 +  have f_UN: "(\<Sum>i. f (disjointed A i)) = f (\<Union>i. A i)"
   10.41 +    by (auto simp: UN_disjointed_eq disjoint_family_disjointed)
   10.42 +  moreover have "(\<lambda>n. (\<Sum>i=0..<n. f (disjointed A i))) ----> (\<Sum>i. f (disjointed A i))"
   10.43 +    using f(1)[unfolded positive_def] dA
   10.44 +    by (auto intro!: summable_sumr_LIMSEQ_suminf summable_ereal_pos)
   10.45 +  from LIMSEQ_Suc[OF this]
   10.46 +  have "(\<lambda>n. (\<Sum>i\<le>n. f (disjointed A i))) ----> (\<Sum>i. f (disjointed A i))"
   10.47 +    unfolding atLeastLessThanSuc_atLeastAtMost atLeast0AtMost .
   10.48 +  moreover have "\<And>n. (\<Sum>i\<le>n. f (disjointed A i)) = f (A n)"
   10.49 +    using disjointed_additive[OF f A(1,2)] .
   10.50 +  ultimately show "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)" by simp
   10.51 +next
   10.52 +  assume cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> incseq A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Union>i. A i)"
   10.53 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "disjoint_family A" "(\<Union>i. A i) \<in> M"
   10.54 +  have *: "(\<Union>n. (\<Union>i\<le>n. A i)) = (\<Union>i. A i)" by auto
   10.55 +  have "(\<lambda>n. f (\<Union>i\<le>n. A i)) ----> f (\<Union>i. A i)"
   10.56 +  proof (unfold *[symmetric], intro cont[rule_format])
   10.57 +    show "range (\<lambda>i. \<Union> i\<le>i. A i) \<subseteq> M" "(\<Union>i. \<Union> i\<le>i. A i) \<in> M"
   10.58 +      using A * by auto
   10.59 +  qed (force intro!: incseq_SucI)
   10.60 +  moreover have "\<And>n. f (\<Union>i\<le>n. A i) = (\<Sum>i\<le>n. f (A i))"
   10.61 +    using A
   10.62 +    by (intro additive_sum[OF f, of _ A, symmetric])
   10.63 +       (auto intro: disjoint_family_on_mono[where B=UNIV])
   10.64 +  ultimately
   10.65 +  have "(\<lambda>i. f (A i)) sums f (\<Union>i. A i)"
   10.66 +    unfolding sums_def2 by simp
   10.67 +  from sums_unique[OF this]
   10.68 +  show "(\<Sum>i. f (A i)) = f (\<Union>i. A i)" by simp
   10.69 +qed
   10.70 +
   10.71 +lemma (in ring_of_sets) continuous_from_above_iff_empty_continuous:
   10.72 +  assumes f: "positive M f" "additive M f"
   10.73 +  shows "(\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) \<in> M \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Inter>i. A i))
   10.74 +     \<longleftrightarrow> (\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> 0)"
   10.75 +proof safe
   10.76 +  assume cont: "(\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) \<in> M \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Inter>i. A i))"
   10.77 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "decseq A" "(\<Inter>i. A i) = {}" "\<forall>i. f (A i) \<noteq> \<infinity>"
   10.78 +  with cont[THEN spec, of A] show "(\<lambda>i. f (A i)) ----> 0"
   10.79 +    using `positive M f`[unfolded positive_def] by auto
   10.80 +next
   10.81 +  assume cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   10.82 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "decseq A" "(\<Inter>i. A i) \<in> M" "\<forall>i. f (A i) \<noteq> \<infinity>"
   10.83 +
   10.84 +  have f_mono: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<subseteq> b \<Longrightarrow> f a \<le> f b"
   10.85 +    using additive_increasing[OF f] unfolding increasing_def by simp
   10.86 +
   10.87 +  have decseq_fA: "decseq (\<lambda>i. f (A i))"
   10.88 +    using A by (auto simp: decseq_def intro!: f_mono)
   10.89 +  have decseq: "decseq (\<lambda>i. A i - (\<Inter>i. A i))"
   10.90 +    using A by (auto simp: decseq_def)
   10.91 +  then have decseq_f: "decseq (\<lambda>i. f (A i - (\<Inter>i. A i)))"
   10.92 +    using A unfolding decseq_def by (auto intro!: f_mono Diff)
   10.93 +  have "f (\<Inter>x. A x) \<le> f (A 0)"
   10.94 +    using A by (auto intro!: f_mono)
   10.95 +  then have f_Int_fin: "f (\<Inter>x. A x) \<noteq> \<infinity>"
   10.96 +    using A by auto
   10.97 +  { fix i
   10.98 +    have "f (A i - (\<Inter>i. A i)) \<le> f (A i)" using A by (auto intro!: f_mono)
   10.99 +    then have "f (A i - (\<Inter>i. A i)) \<noteq> \<infinity>"
  10.100 +      using A by auto }
  10.101 +  note f_fin = this
  10.102 +  have "(\<lambda>i. f (A i - (\<Inter>i. A i))) ----> 0"
  10.103 +  proof (intro cont[rule_format, OF _ decseq _ f_fin])
  10.104 +    show "range (\<lambda>i. A i - (\<Inter>i. A i)) \<subseteq> M" "(\<Inter>i. A i - (\<Inter>i. A i)) = {}"
  10.105 +      using A by auto
  10.106 +  qed
  10.107 +  from INF_Lim_ereal[OF decseq_f this]
  10.108 +  have "(INF n. f (A n - (\<Inter>i. A i))) = 0" .
  10.109 +  moreover have "(INF n. f (\<Inter>i. A i)) = f (\<Inter>i. A i)"
  10.110 +    by auto
  10.111 +  ultimately have "(INF n. f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i)) = 0 + f (\<Inter>i. A i)"
  10.112 +    using A(4) f_fin f_Int_fin
  10.113 +    by (subst INFI_ereal_add) (auto simp: decseq_f)
  10.114 +  moreover {
  10.115 +    fix n
  10.116 +    have "f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i) = f ((A n - (\<Inter>i. A i)) \<union> (\<Inter>i. A i))"
  10.117 +      using A by (subst f(2)[THEN additiveD]) auto
  10.118 +    also have "(A n - (\<Inter>i. A i)) \<union> (\<Inter>i. A i) = A n"
  10.119 +      by auto
  10.120 +    finally have "f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i) = f (A n)" . }
  10.121 +  ultimately have "(INF n. f (A n)) = f (\<Inter>i. A i)"
  10.122 +    by simp
  10.123 +  with LIMSEQ_ereal_INFI[OF decseq_fA]
  10.124 +  show "(\<lambda>i. f (A i)) ----> f (\<Inter>i. A i)" by simp
  10.125 +qed
  10.126 +
  10.127 +lemma (in ring_of_sets) empty_continuous_imp_continuous_from_below:
  10.128 +  assumes f: "positive M f" "additive M f" "\<forall>A\<in>M. f A \<noteq> \<infinity>"
  10.129 +  assumes cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<lambda>i. f (A i)) ----> 0"
  10.130 +  assumes A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
  10.131 +  shows "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)"
  10.132 +proof -
  10.133 +  have "\<forall>A\<in>M. \<exists>x. f A = ereal x"
  10.134 +  proof
  10.135 +    fix A assume "A \<in> M" with f show "\<exists>x. f A = ereal x"
  10.136 +      unfolding positive_def by (cases "f A") auto
  10.137 +  qed
  10.138 +  from bchoice[OF this] guess f' .. note f' = this[rule_format]
  10.139 +  from A have "(\<lambda>i. f ((\<Union>i. A i) - A i)) ----> 0"
  10.140 +    by (intro cont[rule_format]) (auto simp: decseq_def incseq_def)
  10.141 +  moreover
  10.142 +  { fix i
  10.143 +    have "f ((\<Union>i. A i) - A i) + f (A i) = f ((\<Union>i. A i) - A i \<union> A i)"
  10.144 +      using A by (intro f(2)[THEN additiveD, symmetric]) auto
  10.145 +    also have "(\<Union>i. A i) - A i \<union> A i = (\<Union>i. A i)"
  10.146 +      by auto
  10.147 +    finally have "f' (\<Union>i. A i) - f' (A i) = f' ((\<Union>i. A i) - A i)"
  10.148 +      using A by (subst (asm) (1 2 3) f') auto
  10.149 +    then have "f ((\<Union>i. A i) - A i) = ereal (f' (\<Union>i. A i) - f' (A i))"
  10.150 +      using A f' by auto }
  10.151 +  ultimately have "(\<lambda>i. f' (\<Union>i. A i) - f' (A i)) ----> 0"
  10.152 +    by (simp add: zero_ereal_def)
  10.153 +  then have "(\<lambda>i. f' (A i)) ----> f' (\<Union>i. A i)"
  10.154 +    by (rule LIMSEQ_diff_approach_zero2[OF tendsto_const])
  10.155 +  then show "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)"
  10.156 +    using A by (subst (1 2) f') auto
  10.157 +qed
  10.158 +
  10.159 +lemma (in ring_of_sets) empty_continuous_imp_countably_additive:
  10.160 +  assumes f: "positive M f" "additive M f" and fin: "\<forall>A\<in>M. f A \<noteq> \<infinity>"
  10.161 +  assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
  10.162 +  shows "countably_additive M f"
  10.163 +  using countably_additive_iff_continuous_from_below[OF f]
  10.164 +  using empty_continuous_imp_continuous_from_below[OF f fin] cont
  10.165 +  by blast
  10.166 +
  10.167  section {* Properties of @{const emeasure} *}
  10.168  
  10.169  lemma emeasure_positive: "positive (sets M) (emeasure M)"
  10.170 @@ -314,84 +460,21 @@
  10.171      using finite `0 \<le> emeasure M B` by auto
  10.172  qed
  10.173  
  10.174 -lemma emeasure_countable_increasing:
  10.175 -  assumes A: "range A \<subseteq> sets M"
  10.176 -      and A0: "A 0 = {}"
  10.177 -      and ASuc: "\<And>n. A n \<subseteq> A (Suc n)"
  10.178 -  shows "(SUP n. emeasure M (A n)) = emeasure M (\<Union>i. A i)"
  10.179 -proof -
  10.180 -  { fix n
  10.181 -    have "emeasure M (A n) = (\<Sum>i<n. emeasure M (A (Suc i) - A i))"
  10.182 -      proof (induct n)
  10.183 -        case 0 thus ?case by (auto simp add: A0)
  10.184 -      next
  10.185 -        case (Suc m)
  10.186 -        have "A (Suc m) = A m \<union> (A (Suc m) - A m)"
  10.187 -          by (metis ASuc Un_Diff_cancel Un_absorb1)
  10.188 -        hence "emeasure M (A (Suc m)) =
  10.189 -               emeasure M (A m) + emeasure M (A (Suc m) - A m)"
  10.190 -          by (subst plus_emeasure)
  10.191 -             (auto simp add: emeasure_additive range_subsetD [OF A])
  10.192 -        with Suc show ?case
  10.193 -          by simp
  10.194 -      qed }
  10.195 -  note Meq = this
  10.196 -  have Aeq: "(\<Union>i. A (Suc i) - A i) = (\<Union>i. A i)"
  10.197 -    proof (rule UN_finite2_eq [where k=1], simp)
  10.198 -      fix i
  10.199 -      show "(\<Union>i\<in>{0..<i}. A (Suc i) - A i) = (\<Union>i\<in>{0..<Suc i}. A i)"
  10.200 -        proof (induct i)
  10.201 -          case 0 thus ?case by (simp add: A0)
  10.202 -        next
  10.203 -          case (Suc i)
  10.204 -          thus ?case
  10.205 -            by (auto simp add: atLeastLessThanSuc intro: subsetD [OF ASuc])
  10.206 -        qed
  10.207 -    qed
  10.208 -  have A1: "\<And>i. A (Suc i) - A i \<in> sets M"
  10.209 -    by (metis A Diff range_subsetD)
  10.210 -  have A2: "(\<Union>i. A (Suc i) - A i) \<in> sets M"
  10.211 -    by (blast intro: range_subsetD [OF A])
  10.212 -  have "(SUP n. \<Sum>i<n. emeasure M (A (Suc i) - A i)) = (\<Sum>i. emeasure M (A (Suc i) - A i))"
  10.213 -    using A by (auto intro!: suminf_ereal_eq_SUPR[symmetric])
  10.214 -  also have "\<dots> = emeasure M (\<Union>i. A (Suc i) - A i)"
  10.215 -    by (rule suminf_emeasure)
  10.216 -       (auto simp add: disjoint_family_Suc ASuc A1 A2)
  10.217 -  also have "... =  emeasure M (\<Union>i. A i)"
  10.218 -    by (simp add: Aeq)
  10.219 -  finally have "(SUP n. \<Sum>i<n. emeasure M (A (Suc i) - A i)) = emeasure M (\<Union>i. A i)" .
  10.220 -  then show ?thesis by (auto simp add: Meq)
  10.221 -qed
  10.222 -
  10.223 -lemma SUP_emeasure_incseq:
  10.224 -  assumes A: "range A \<subseteq> sets M" and "incseq A"
  10.225 -  shows "(SUP n. emeasure M (A n)) = emeasure M (\<Union>i. A i)"
  10.226 -proof -
  10.227 -  have *: "(SUP n. emeasure M (nat_case {} A (Suc n))) = (SUP n. emeasure M (nat_case {} A n))"
  10.228 -    using A by (auto intro!: SUPR_eq exI split: nat.split)
  10.229 -  have ueq: "(\<Union>i. nat_case {} A i) = (\<Union>i. A i)"
  10.230 -    by (auto simp add: split: nat.splits)
  10.231 -  have meq: "\<And>n. emeasure M (A n) = (emeasure M \<circ> nat_case {} A) (Suc n)"
  10.232 -    by simp
  10.233 -  have "(SUP n. emeasure M (nat_case {} A n)) = emeasure M (\<Union>i. nat_case {} A i)"
  10.234 -    using range_subsetD[OF A] incseq_SucD[OF `incseq A`]
  10.235 -    by (force split: nat.splits intro!: emeasure_countable_increasing)
  10.236 -  also have "emeasure M (\<Union>i. nat_case {} A i) = emeasure M (\<Union>i. A i)"
  10.237 -    by (simp add: ueq)
  10.238 -  finally have "(SUP n. emeasure M (nat_case {} A n)) = emeasure M (\<Union>i. A i)" .
  10.239 -  thus ?thesis unfolding meq * comp_def .
  10.240 -qed
  10.241 +lemma Lim_emeasure_incseq:
  10.242 +  "range A \<subseteq> sets M \<Longrightarrow> incseq A \<Longrightarrow> (\<lambda>i. (emeasure M (A i))) ----> emeasure M (\<Union>i. A i)"
  10.243 +  using emeasure_countably_additive
  10.244 +  by (auto simp add: countably_additive_iff_continuous_from_below emeasure_positive emeasure_additive)
  10.245  
  10.246  lemma incseq_emeasure:
  10.247    assumes "range B \<subseteq> sets M" "incseq B"
  10.248    shows "incseq (\<lambda>i. emeasure M (B i))"
  10.249    using assms by (auto simp: incseq_def intro!: emeasure_mono)
  10.250  
  10.251 -lemma Lim_emeasure_incseq:
  10.252 +lemma SUP_emeasure_incseq:
  10.253    assumes A: "range A \<subseteq> sets M" "incseq A"
  10.254 -  shows "(\<lambda>i. (emeasure M (A i))) ----> emeasure M (\<Union>i. A i)"
  10.255 -  using LIMSEQ_ereal_SUPR[OF incseq_emeasure, OF A]
  10.256 -    SUP_emeasure_incseq[OF A] by simp
  10.257 +  shows "(SUP n. emeasure M (A n)) = emeasure M (\<Union>i. A i)"
  10.258 +  using LIMSEQ_ereal_SUPR[OF incseq_emeasure, OF A] Lim_emeasure_incseq[OF A]
  10.259 +  by (simp add: LIMSEQ_unique)
  10.260  
  10.261  lemma decseq_emeasure:
  10.262    assumes "range B \<subseteq> sets M" "decseq B"
  10.263 @@ -545,70 +628,78 @@
  10.264    and eq: "\<And>X. X \<in> E \<Longrightarrow> emeasure M X = emeasure N X"
  10.265    and M: "sets M = sigma_sets \<Omega> E"
  10.266    and N: "sets N = sigma_sets \<Omega> E"
  10.267 -  and A: "range A \<subseteq> E" "incseq A" "(\<Union>i. A i) = \<Omega>" "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  10.268 +  and A: "range A \<subseteq> E" "(\<Union>i. A i) = \<Omega>" "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  10.269    shows "M = N"
  10.270  proof -
  10.271 -  let ?D = "\<lambda>F. {D. D \<in> sigma_sets \<Omega> E \<and> emeasure M (F \<inter> D) = emeasure N (F \<inter> D)}"
  10.272 +  let ?\<mu>  = "emeasure M" and ?\<nu> = "emeasure N"
  10.273    interpret S: sigma_algebra \<Omega> "sigma_sets \<Omega> E" by (rule sigma_algebra_sigma_sets) fact
  10.274 -  { fix F assume "F \<in> E" and "emeasure M F \<noteq> \<infinity>"
  10.275 +  have "space M = \<Omega>"
  10.276 +    using top[of M] space_closed[of M] S.top S.space_closed `sets M = sigma_sets \<Omega> E` by blast
  10.277 +
  10.278 +  { fix F D assume "F \<in> E" and "?\<mu> F \<noteq> \<infinity>"
  10.279      then have [intro]: "F \<in> sigma_sets \<Omega> E" by auto
  10.280 -    have "emeasure N F \<noteq> \<infinity>" using `emeasure M F \<noteq> \<infinity>` `F \<in> E` eq by simp
  10.281 -    interpret D: dynkin_system \<Omega> "?D F"
  10.282 -    proof (rule dynkin_systemI, simp_all)
  10.283 -      fix A assume "A \<in> sigma_sets \<Omega> E \<and> emeasure M (F \<inter> A) = emeasure N (F \<inter> A)"
  10.284 -      then show "A \<subseteq> \<Omega>" using S.sets_into_space by auto
  10.285 +    have "?\<nu> F \<noteq> \<infinity>" using `?\<mu> F \<noteq> \<infinity>` `F \<in> E` eq by simp
  10.286 +    assume "D \<in> sets M"
  10.287 +    with `Int_stable E` `E \<subseteq> Pow \<Omega>` have "emeasure M (F \<inter> D) = emeasure N (F \<inter> D)"
  10.288 +      unfolding M
  10.289 +    proof (induct rule: sigma_sets_induct_disjoint)
  10.290 +      case (basic A)
  10.291 +      then have "F \<inter> A \<in> E" using `Int_stable E` `F \<in> E` by (auto simp: Int_stable_def)
  10.292 +      then show ?case using eq by auto
  10.293      next
  10.294 -      have "F \<inter> \<Omega> = F" using `F \<in> E` `E \<subseteq> Pow \<Omega>` by auto
  10.295 -      then show "emeasure M (F \<inter> \<Omega>) = emeasure N (F \<inter> \<Omega>)"
  10.296 -        using `F \<in> E` eq by (auto intro: sigma_sets_top)
  10.297 +      case empty then show ?case by simp
  10.298      next
  10.299 -      fix A assume *: "A \<in> sigma_sets \<Omega> E \<and> emeasure M (F \<inter> A) = emeasure N (F \<inter> A)"
  10.300 +      case (compl A)
  10.301        then have **: "F \<inter> (\<Omega> - A) = F - (F \<inter> A)"
  10.302          and [intro]: "F \<inter> A \<in> sigma_sets \<Omega> E"
  10.303 -        using `F \<in> E` S.sets_into_space by auto
  10.304 -      have "emeasure N (F \<inter> A) \<le> emeasure N F" by (auto intro!: emeasure_mono simp: M N)
  10.305 -      then have "emeasure N (F \<inter> A) \<noteq> \<infinity>" using `emeasure N F \<noteq> \<infinity>` by auto
  10.306 -      have "emeasure M (F \<inter> A) \<le> emeasure M F" by (auto intro!: emeasure_mono simp: M N)
  10.307 -      then have "emeasure M (F \<inter> A) \<noteq> \<infinity>" using `emeasure M F \<noteq> \<infinity>` by auto
  10.308 -      then have "emeasure M (F \<inter> (\<Omega> - A)) = emeasure M F - emeasure M (F \<inter> A)" unfolding **
  10.309 +        using `F \<in> E` S.sets_into_space by (auto simp: M)
  10.310 +      have "?\<nu> (F \<inter> A) \<le> ?\<nu> F" by (auto intro!: emeasure_mono simp: M N)
  10.311 +      then have "?\<nu> (F \<inter> A) \<noteq> \<infinity>" using `?\<nu> F \<noteq> \<infinity>` by auto
  10.312 +      have "?\<mu> (F \<inter> A) \<le> ?\<mu> F" by (auto intro!: emeasure_mono simp: M N)
  10.313 +      then have "?\<mu> (F \<inter> A) \<noteq> \<infinity>" using `?\<mu> F \<noteq> \<infinity>` by auto
  10.314 +      then have "?\<mu> (F \<inter> (\<Omega> - A)) = ?\<mu> F - ?\<mu> (F \<inter> A)" unfolding **
  10.315          using `F \<inter> A \<in> sigma_sets \<Omega> E` by (auto intro!: emeasure_Diff simp: M N)
  10.316 -      also have "\<dots> = emeasure N F - emeasure N (F \<inter> A)" using eq `F \<in> E` * by simp
  10.317 -      also have "\<dots> = emeasure N (F \<inter> (\<Omega> - A))" unfolding **
  10.318 -        using `F \<inter> A \<in> sigma_sets \<Omega> E` `emeasure N (F \<inter> A) \<noteq> \<infinity>`
  10.319 +      also have "\<dots> = ?\<nu> F - ?\<nu> (F \<inter> A)" using eq `F \<in> E` compl by simp
  10.320 +      also have "\<dots> = ?\<nu> (F \<inter> (\<Omega> - A))" unfolding **
  10.321 +        using `F \<inter> A \<in> sigma_sets \<Omega> E` `?\<nu> (F \<inter> A) \<noteq> \<infinity>`
  10.322          by (auto intro!: emeasure_Diff[symmetric] simp: M N)
  10.323 -      finally show "\<Omega> - A \<in> sigma_sets \<Omega> E \<and> emeasure M (F \<inter> (\<Omega> - A)) = emeasure N (F \<inter> (\<Omega> - A))"
  10.324 -        using * by auto
  10.325 +      finally show ?case
  10.326 +        using `space M = \<Omega>` by auto
  10.327      next
  10.328 -      fix A :: "nat \<Rightarrow> 'a set"
  10.329 -      assume "disjoint_family A" "range A \<subseteq> {X \<in> sigma_sets \<Omega> E. emeasure M (F \<inter> X) = emeasure N (F \<inter> X)}"
  10.330 -      then have A: "range (\<lambda>i. F \<inter> A i) \<subseteq> sigma_sets \<Omega> E" "F \<inter> (\<Union>x. A x) = (\<Union>x. F \<inter> A x)"
  10.331 -        "disjoint_family (\<lambda>i. F \<inter> A i)" "\<And>i. emeasure M (F \<inter> A i) = emeasure N (F \<inter> A i)" "range A \<subseteq> sigma_sets \<Omega> E"
  10.332 -        by (auto simp: disjoint_family_on_def subset_eq)
  10.333 -      then show "(\<Union>x. A x) \<in> sigma_sets \<Omega> E \<and> emeasure M (F \<inter> (\<Union>x. A x)) = emeasure N (F \<inter> (\<Union>x. A x))"
  10.334 -        by (auto simp: M N suminf_emeasure[symmetric] simp del: UN_simps)
  10.335 -    qed
  10.336 -    have *: "sigma_sets \<Omega> E = ?D F"
  10.337 -      using `F \<in> E` `Int_stable E`
  10.338 -      by (intro D.dynkin_lemma) (auto simp add: Int_stable_def eq)
  10.339 -    have "\<And>D. D \<in> sigma_sets \<Omega> E \<Longrightarrow> emeasure M (F \<inter> D) = emeasure N (F \<inter> D)"
  10.340 -      by (subst (asm) *) auto }
  10.341 +      case (union A)
  10.342 +      then have "?\<mu> (\<Union>x. F \<inter> A x) = ?\<nu> (\<Union>x. F \<inter> A x)"
  10.343 +        by (subst (1 2) suminf_emeasure[symmetric]) (auto simp: disjoint_family_on_def subset_eq M N)
  10.344 +      with A show ?case
  10.345 +        by auto
  10.346 +    qed }
  10.347    note * = this
  10.348    show "M = N"
  10.349    proof (rule measure_eqI)
  10.350      show "sets M = sets N"
  10.351        using M N by simp
  10.352 -    fix X assume "X \<in> sets M"
  10.353 -    then have "X \<in> sigma_sets \<Omega> E"
  10.354 -      using M by simp
  10.355 -    let ?A = "\<lambda>i. A i \<inter> X"
  10.356 -    have "range ?A \<subseteq> sigma_sets \<Omega> E" "incseq ?A"
  10.357 -      using A(1,2) `X \<in> sigma_sets \<Omega> E` by (auto simp: incseq_def)
  10.358 -    moreover
  10.359 -    { fix i have "emeasure M (?A i) = emeasure N (?A i)"
  10.360 -        using *[of "A i" X] `X \<in> sigma_sets \<Omega> E` A finite by auto }
  10.361 -    ultimately show "emeasure M X = emeasure N X"
  10.362 -      using SUP_emeasure_incseq[of ?A M] SUP_emeasure_incseq[of ?A N] A(3) `X \<in> sigma_sets \<Omega> E`
  10.363 -      by (auto simp: M N SUP_emeasure_incseq)
  10.364 +    have [simp, intro]: "\<And>i. A i \<in> sets M"
  10.365 +      using A(1) by (auto simp: subset_eq M)
  10.366 +    fix F assume "F \<in> sets M"
  10.367 +    let ?D = "disjointed (\<lambda>i. F \<inter> A i)"
  10.368 +    from `space M = \<Omega>` have F_eq: "F = (\<Union>i. ?D i)"
  10.369 +      using `F \<in> sets M`[THEN sets_into_space] A(2)[symmetric] by (auto simp: UN_disjointed_eq)
  10.370 +    have [simp, intro]: "\<And>i. ?D i \<in> sets M"
  10.371 +      using range_disjointed_sets[of "\<lambda>i. F \<inter> A i" M] `F \<in> sets M`
  10.372 +      by (auto simp: subset_eq)
  10.373 +    have "disjoint_family ?D"
  10.374 +      by (auto simp: disjoint_family_disjointed)
  10.375 +     moreover
  10.376 +    { fix i
  10.377 +      have "A i \<inter> ?D i = ?D i"
  10.378 +        by (auto simp: disjointed_def)
  10.379 +      then have "emeasure M (?D i) = emeasure N (?D i)"
  10.380 +        using *[of "A i" "?D i", OF _ A(3)] A(1) by auto }
  10.381 +     ultimately show "emeasure M F = emeasure N F"
  10.382 +      using N M
  10.383 +      apply (subst (1 2) F_eq)
  10.384 +      apply (subst (1 2) suminf_emeasure[symmetric])
  10.385 +      apply auto
  10.386 +      done
  10.387    qed
  10.388  qed
  10.389  
  10.390 @@ -1016,6 +1107,24 @@
  10.391         (auto simp: emeasure_distr measurable_def)
  10.392  qed
  10.393  
  10.394 +lemma AE_distr_iff:
  10.395 +  assumes f: "f \<in> measurable M N" and P: "{x \<in> space N. P x} \<in> sets N"
  10.396 +  shows "(AE x in distr M N f. P x) \<longleftrightarrow> (AE x in M. P (f x))"
  10.397 +proof (subst (1 2) AE_iff_measurable[OF _ refl])
  10.398 +  from P show "{x \<in> space (distr M N f). \<not> P x} \<in> sets (distr M N f)"
  10.399 +    by (auto intro!: sets_Collect_neg)
  10.400 +  moreover
  10.401 +  have "f -` {x \<in> space N. P x} \<inter> space M = {x \<in> space M. P (f x)}"
  10.402 +    using f by (auto dest: measurable_space)
  10.403 +  then show "{x \<in> space M. \<not> P (f x)} \<in> sets M"
  10.404 +    using measurable_sets[OF f P] by (auto intro!: sets_Collect_neg)
  10.405 +  moreover have "f -` {x\<in>space N. \<not> P x} \<inter> space M = {x \<in> space M. \<not> P (f x)}"
  10.406 +    using f by (auto dest: measurable_space)
  10.407 +  ultimately show "(emeasure (distr M N f) {x \<in> space (distr M N f). \<not> P x} = 0) =
  10.408 +    (emeasure M {x \<in> space M. \<not> P (f x)} = 0)"
  10.409 +    using f by (simp add: emeasure_distr)
  10.410 +qed
  10.411 +
  10.412  lemma null_sets_distr_iff:
  10.413    "f \<in> measurable M N \<Longrightarrow> A \<in> null_sets (distr M N f) \<longleftrightarrow> f -` A \<inter> space M \<in> null_sets M \<and> A \<in> sets N"
  10.414    by (auto simp add: null_sets_def emeasure_distr measurable_sets)
  10.415 @@ -1295,103 +1404,81 @@
  10.416      unfolding measurable_def by auto
  10.417  qed
  10.418  
  10.419 +lemma strict_monoI_Suc:
  10.420 +  assumes ord [simp]: "(\<And>n. f n < f (Suc n))" shows "strict_mono f"
  10.421 +  unfolding strict_mono_def
  10.422 +proof safe
  10.423 +  fix n m :: nat assume "n < m" then show "f n < f m"
  10.424 +    by (induct m) (auto simp: less_Suc_eq intro: less_trans ord)
  10.425 +qed
  10.426 +
  10.427  lemma emeasure_count_space:
  10.428    assumes "X \<subseteq> A" shows "emeasure (count_space A) X = (if finite X then ereal (card X) else \<infinity>)"
  10.429      (is "_ = ?M X")
  10.430    unfolding count_space_def
  10.431  proof (rule emeasure_measure_of_sigma)
  10.432 +  show "X \<in> Pow A" using `X \<subseteq> A` by auto
  10.433    show "sigma_algebra A (Pow A)" by (rule sigma_algebra_Pow)
  10.434 -
  10.435 -  show "positive (Pow A) ?M"
  10.436 +  show positive: "positive (Pow A) ?M"
  10.437      by (auto simp: positive_def)
  10.438 +  have additive: "additive (Pow A) ?M"
  10.439 +    by (auto simp: card_Un_disjoint additive_def)
  10.440  
  10.441 -  show "countably_additive (Pow A) ?M"
  10.442 -  proof (unfold countably_additive_def, safe)
  10.443 -      fix F :: "nat \<Rightarrow> 'a set" assume disj: "disjoint_family F"
  10.444 -      show "(\<Sum>i. ?M (F i)) = ?M (\<Union>i. F i)"
  10.445 -      proof cases
  10.446 -        assume "\<forall>i. finite (F i)"
  10.447 -        then have finite_F: "\<And>i. finite (F i)" by auto
  10.448 -        have "\<forall>i\<in>{i. F i \<noteq> {}}. \<exists>x. x \<in> F i" by auto
  10.449 -        from bchoice[OF this] obtain f where f: "\<And>i. F i \<noteq> {} \<Longrightarrow> f i \<in> F i" by auto
  10.450 +  interpret ring_of_sets A "Pow A"
  10.451 +    by (rule ring_of_setsI) auto
  10.452 +  show "countably_additive (Pow A) ?M" 
  10.453 +    unfolding countably_additive_iff_continuous_from_below[OF positive additive]
  10.454 +  proof safe
  10.455 +    fix F :: "nat \<Rightarrow> 'a set" assume "incseq F"
  10.456 +    show "(\<lambda>i. ?M (F i)) ----> ?M (\<Union>i. F i)"
  10.457 +    proof cases
  10.458 +      assume "\<exists>i. \<forall>j\<ge>i. F i = F j"
  10.459 +      then guess i .. note i = this
  10.460 +      { fix j from i `incseq F` have "F j \<subseteq> F i"
  10.461 +          by (cases "i \<le> j") (auto simp: incseq_def) }
  10.462 +      then have eq: "(\<Union>i. F i) = F i"
  10.463 +        by auto
  10.464 +      with i show ?thesis
  10.465 +        by (auto intro!: Lim_eventually eventually_sequentiallyI[where c=i])
  10.466 +    next
  10.467 +      assume "\<not> (\<exists>i. \<forall>j\<ge>i. F i = F j)"
  10.468 +      then obtain f where "\<And>i. i \<le> f i" "\<And>i. F i \<noteq> F (f i)" by metis
  10.469 +      moreover then have "\<And>i. F i \<subseteq> F (f i)" using `incseq F` by (auto simp: incseq_def)
  10.470 +      ultimately have *: "\<And>i. F i \<subset> F (f i)" by auto
  10.471  
  10.472 -        have inj_f: "inj_on f {i. F i \<noteq> {}}"
  10.473 -        proof (rule inj_onI, simp)
  10.474 -          fix i j a b assume *: "f i = f j" "F i \<noteq> {}" "F j \<noteq> {}"
  10.475 -          then have "f i \<in> F i" "f j \<in> F j" using f by force+
  10.476 -          with disj * show "i = j" by (auto simp: disjoint_family_on_def)
  10.477 -        qed
  10.478 -        have fin_eq: "finite (\<Union>i. F i) \<longleftrightarrow> finite {i. F i \<noteq> {}}"
  10.479 -        proof
  10.480 -          assume "finite (\<Union>i. F i)"
  10.481 -          show "finite {i. F i \<noteq> {}}"
  10.482 -          proof (rule finite_imageD)
  10.483 -            from f have "f`{i. F i \<noteq> {}} \<subseteq> (\<Union>i. F i)" by auto
  10.484 -            then show "finite (f`{i. F i \<noteq> {}})"
  10.485 -              by (rule finite_subset) fact
  10.486 -          qed fact
  10.487 -        next
  10.488 -          assume "finite {i. F i \<noteq> {}}"
  10.489 -          with finite_F have "finite (\<Union>i\<in>{i. F i \<noteq> {}}. F i)"
  10.490 -            by auto
  10.491 -          also have "(\<Union>i\<in>{i. F i \<noteq> {}}. F i) = (\<Union>i. F i)"
  10.492 -            by auto
  10.493 -          finally show "finite (\<Union>i. F i)" .
  10.494 -        qed
  10.495 -        
  10.496 -        show ?thesis
  10.497 -        proof cases
  10.498 -          assume *: "finite (\<Union>i. F i)"
  10.499 -          with finite_F have "finite {i. ?M (F i) \<noteq> 0} "
  10.500 -            by (simp add: fin_eq)
  10.501 -          then have "(\<Sum>i. ?M (F i)) = (\<Sum>i | ?M (F i) \<noteq> 0. ?M (F i))"
  10.502 -            by (rule suminf_finite) auto
  10.503 -          also have "\<dots> = ereal (\<Sum>i | F i \<noteq> {}. card (F i))"
  10.504 -            using finite_F by simp
  10.505 -          also have "\<dots> = ereal (card (\<Union>i \<in> {i. F i \<noteq> {}}. F i))"
  10.506 -            using * finite_F disj
  10.507 -            by (subst card_UN_disjoint) (auto simp: disjoint_family_on_def fin_eq)
  10.508 -          also have "\<dots> = ?M (\<Union>i. F i)"
  10.509 -            using * by (auto intro!: arg_cong[where f=card])
  10.510 -          finally show ?thesis .
  10.511 -        next
  10.512 -          assume inf: "infinite (\<Union>i. F i)"
  10.513 -          { fix i
  10.514 -            have "\<exists>N. i \<le> (\<Sum>i<N. card (F i))"
  10.515 -            proof (induct i)
  10.516 -              case (Suc j)
  10.517 -              from Suc obtain N where N: "j \<le> (\<Sum>i<N. card (F i))" by auto
  10.518 -              have "infinite ({i. F i \<noteq> {}} - {..< N})"
  10.519 -                using inf by (auto simp: fin_eq)
  10.520 -              then have "{i. F i \<noteq> {}} - {..< N} \<noteq> {}"
  10.521 -                by (metis finite.emptyI)
  10.522 -              then obtain i where i: "F i \<noteq> {}" "N \<le> i"
  10.523 -                by (auto simp: not_less[symmetric])
  10.524 +      have "incseq (\<lambda>i. ?M (F i))"
  10.525 +        using `incseq F` unfolding incseq_def by (auto simp: card_mono dest: finite_subset)
  10.526 +      then have "(\<lambda>i. ?M (F i)) ----> (SUP n. ?M (F n))"
  10.527 +        by (rule LIMSEQ_ereal_SUPR)
  10.528  
  10.529 -              note N
  10.530 -              also have "(\<Sum>i<N. card (F i)) \<le> (\<Sum>i<i. card (F i))"
  10.531 -                by (rule setsum_mono2) (auto simp: i)
  10.532 -              also have "\<dots> < (\<Sum>i<i. card (F i)) + card (F i)"
  10.533 -                using finite_F `F i \<noteq> {}` by (simp add: card_gt_0_iff)
  10.534 -              finally have "j < (\<Sum>i<Suc i. card (F i))"
  10.535 -                by simp
  10.536 -              then show ?case unfolding Suc_le_eq by blast
  10.537 -            qed simp }
  10.538 -          with finite_F inf show ?thesis
  10.539 -            by (auto simp del: real_of_nat_setsum intro!: SUP_PInfty
  10.540 -                     simp add: suminf_ereal_eq_SUPR real_of_nat_setsum[symmetric])
  10.541 -        qed
  10.542 -      next
  10.543 -        assume "\<not> (\<forall>i. finite (F i))"
  10.544 -        then obtain j where j: "infinite (F j)" by auto
  10.545 -        then have "infinite (\<Union>i. F i)"
  10.546 -          using finite_subset[of "F j" "\<Union>i. F i"] by auto
  10.547 -        moreover have "\<And>i. 0 \<le> ?M (F i)" by auto
  10.548 -        ultimately show ?thesis
  10.549 -          using suminf_PInfty[of "\<lambda>i. ?M (F i)" j] j by auto
  10.550 +      moreover have "(SUP n. ?M (F n)) = \<infinity>"
  10.551 +      proof (rule SUP_PInfty)
  10.552 +        fix n :: nat show "\<exists>k::nat\<in>UNIV. ereal n \<le> ?M (F k)"
  10.553 +        proof (induct n)
  10.554 +          case (Suc n)
  10.555 +          then guess k .. note k = this
  10.556 +          moreover have "finite (F k) \<Longrightarrow> finite (F (f k)) \<Longrightarrow> card (F k) < card (F (f k))"
  10.557 +            using `F k \<subset> F (f k)` by (simp add: psubset_card_mono)
  10.558 +          moreover have "finite (F (f k)) \<Longrightarrow> finite (F k)"
  10.559 +            using `k \<le> f k` `incseq F` by (aut