reworked Probability theory
authorhoelzl
Mon Apr 23 12:14:35 2012 +0200 (2012-04-23)
changeset 4769405663f75964c
parent 47693 64023cf4d148
child 47695 89a90da149a9
reworked Probability theory
NEWS
src/HOL/IsaMakefile
src/HOL/Probability/Binary_Product_Measure.thy
src/HOL/Probability/Borel_Space.thy
src/HOL/Probability/Caratheodory.thy
src/HOL/Probability/Complete_Measure.thy
src/HOL/Probability/Conditional_Probability.thy
src/HOL/Probability/Finite_Product_Measure.thy
src/HOL/Probability/Independent_Family.thy
src/HOL/Probability/Infinite_Product_Measure.thy
src/HOL/Probability/Information.thy
src/HOL/Probability/Lebesgue_Integration.thy
src/HOL/Probability/Lebesgue_Measure.thy
src/HOL/Probability/Measure.thy
src/HOL/Probability/Measure_Space.thy
src/HOL/Probability/Probability.thy
src/HOL/Probability/Probability_Measure.thy
src/HOL/Probability/Radon_Nikodym.thy
src/HOL/Probability/Sigma_Algebra.thy
src/HOL/Probability/ex/Dining_Cryptographers.thy
src/HOL/Probability/ex/Koepf_Duermuth_Countermeasure.thy
     1.1 --- a/NEWS	Mon Apr 23 12:23:23 2012 +0100
     1.2 +++ b/NEWS	Mon Apr 23 12:14:35 2012 +0200
     1.3 @@ -647,6 +647,180 @@
     1.4  
     1.5    DERIV_nonneg_imp_nonincreasing ~> DERIV_nonneg_imp_nondecreasing
     1.6  
     1.7 +* Theory Library/Multiset: Improved code generation of multisets.
     1.8 +
     1.9 +* Session HOL-Probability: Introduced the type "'a measure" to represent
    1.10 +measures, this replaces the records 'a algebra and 'a measure_space. The
    1.11 +locales based on subset_class now have two locale-parameters the space
    1.12 +\<Omega> and the set of measurables sets M. The product of probability spaces
    1.13 +uses now the same constant as the finite product of sigma-finite measure
    1.14 +spaces "PiM :: ('i => 'a) measure". Most constants are defined now
    1.15 +outside of locales and gain an additional parameter, like null_sets,
    1.16 +almost_eventually or \<mu>'. Measure space constructions for distributions
    1.17 +and densities now got their own constants distr and density. Instead of
    1.18 +using locales to describe measure spaces with a finite space, the
    1.19 +measure count_space and point_measure is introduced. INCOMPATIBILITY.
    1.20 +
    1.21 +  Renamed constants:
    1.22 +  measure -> emeasure
    1.23 +  finite_measure.\<mu>' -> measure
    1.24 +  product_algebra_generator -> prod_algebra
    1.25 +  product_prob_space.emb -> prod_emb
    1.26 +  product_prob_space.infprod_algebra -> PiM
    1.27 +
    1.28 +  Removed locales:
    1.29 +  completeable_measure_space
    1.30 +  finite_measure_space
    1.31 +  finite_prob_space
    1.32 +  finite_product_finite_prob_space
    1.33 +  finite_product_sigma_algebra
    1.34 +  finite_sigma_algebra
    1.35 +  measure_space
    1.36 +  pair_finite_prob_space
    1.37 +  pair_finite_sigma_algebra
    1.38 +  pair_finite_space
    1.39 +  pair_sigma_algebra
    1.40 +  product_sigma_algebra
    1.41 +
    1.42 +  Removed constants:
    1.43 +  distribution -> use distr measure, or distributed predicate
    1.44 +  joint_distribution -> use distr measure, or distributed predicate
    1.45 +  product_prob_space.infprod_algebra -> use PiM
    1.46 +  subvimage
    1.47 +  image_space
    1.48 +  conditional_space
    1.49 +  pair_measure_generator
    1.50 +
    1.51 +  Replacement theorems:
    1.52 +  sigma_algebra.measurable_sigma -> measurable_measure_of
    1.53 +  measure_space.additive -> emeasure_additive
    1.54 +  measure_space.measure_additive -> plus_emeasure
    1.55 +  measure_space.measure_mono -> emeasure_mono
    1.56 +  measure_space.measure_top -> emeasure_space
    1.57 +  measure_space.measure_compl -> emeasure_compl
    1.58 +  measure_space.measure_Diff -> emeasure_Diff
    1.59 +  measure_space.measure_countable_increasing -> emeasure_countable_increasing
    1.60 +  measure_space.continuity_from_below -> SUP_emeasure_incseq
    1.61 +  measure_space.measure_incseq -> incseq_emeasure
    1.62 +  measure_space.continuity_from_below_Lim -> Lim_emeasure_incseq
    1.63 +  measure_space.measure_decseq -> decseq_emeasure
    1.64 +  measure_space.continuity_from_above -> INF_emeasure_decseq
    1.65 +  measure_space.measure_insert -> emeasure_insert
    1.66 +  measure_space.measure_setsum -> setsum_emeasure
    1.67 +  measure_space.measure_finite_singleton -> emeasure_eq_setsum_singleton
    1.68 +  finite_additivity_sufficient -> ring_of_sets.countably_additiveI_finite
    1.69 +  measure_space.measure_setsum_split -> setsum_emeasure_cover
    1.70 +  measure_space.measure_subadditive -> subadditive
    1.71 +  measure_space.measure_subadditive_finite -> emeasure_subadditive_finite
    1.72 +  measure_space.measure_eq_0 -> emeasure_eq_0
    1.73 +  measure_space.measure_finitely_subadditive -> emeasure_subadditive_finite
    1.74 +  measure_space.measure_countably_subadditive -> emeasure_subadditive_countably
    1.75 +  measure_space.measure_UN_eq_0 -> emeasure_UN_eq_0
    1.76 +  measure_unique_Int_stable -> measure_eqI_generator_eq
    1.77 +  measure_space.measure_Diff_null_set -> emeasure_Diff_null_set
    1.78 +  measure_space.measure_Un_null_set -> emeasure_Un_null_set
    1.79 +  measure_space.almost_everywhere_def -> eventually_ae_filter
    1.80 +  measure_space.almost_everywhere_vimage -> AE_distrD
    1.81 +  measure_space.measure_space_vimage -> emeasure_distr
    1.82 +  measure_space.AE_iff_null_set -> AE_iff_null
    1.83 +  measure_space.real_measure_Union -> measure_Union
    1.84 +  measure_space.real_measure_finite_Union -> measure_finite_Union
    1.85 +  measure_space.real_measure_Diff -> measure_Diff
    1.86 +  measure_space.real_measure_UNION -> measure_UNION
    1.87 +  measure_space.real_measure_subadditive -> measure_subadditive
    1.88 +  measure_space.real_measure_setsum_singleton -> measure_eq_setsum_singleton
    1.89 +  measure_space.real_continuity_from_below -> Lim_measure_incseq
    1.90 +  measure_space.continuity_from_above_Lim -> Lim_emeasure_decseq
    1.91 +  measure_space.real_continuity_from_above -> Lim_measure_decseq
    1.92 +  measure_space.real_measure_countably_subadditive -> measure_subadditive_countably
    1.93 +  finite_measure.finite_measure -> finite_measure.emeasure_finite
    1.94 +  finite_measure.finite_measure_eq -> finite_measure.emeasure_eq_measure
    1.95 +  finite_measure.positive_measure' -> measure_nonneg
    1.96 +  finite_measure.real_measure -> finite_measure.emeasure_real
    1.97 +  finite_measure.empty_measure -> measure_empty
    1.98 +  finite_measure.finite_measure_countably_subadditive -> finite_measure.finite_measure_subadditive_countably
    1.99 +  finite_measure.finite_measure_finite_singleton -> finite_measure.finite_measure_eq_setsum_singleton
   1.100 +  finite_measure.finite_continuity_from_below -> finite_measure.finite_Lim_measure_incseq
   1.101 +  finite_measure.finite_continuity_from_above -> finite_measure.finite_Lim_measure_decseq
   1.102 +  measure_space.simple_integral_vimage -> simple_integral_distr
   1.103 +  measure_space.integrable_vimage -> integrable_distr
   1.104 +  measure_space.positive_integral_translated_density -> positive_integral_density
   1.105 +  measure_space.integral_translated_density -> integral_density
   1.106 +  measure_space.integral_vimage -> integral_distr
   1.107 +  measure_space_density -> emeasure_density
   1.108 +  measure_space.positive_integral_vimage -> positive_integral_distr
   1.109 +  measure_space.simple_function_vimage -> simple_function_comp
   1.110 +  measure_space.simple_integral_vimage -> simple_integral_distr
   1.111 +  pair_sigma_algebra.measurable_cut_fst -> sets_Pair1
   1.112 +  pair_sigma_algebra.measurable_cut_snd -> sets_Pair2
   1.113 +  pair_sigma_algebra.measurable_pair_image_fst -> measurable_Pair1
   1.114 +  pair_sigma_algebra.measurable_pair_image_snd -> measurable_Pair2
   1.115 +  pair_sigma_algebra.measurable_product_swap -> measurable_pair_swap_iff
   1.116 +  pair_sigma_finite.measure_cut_measurable_fst -> pair_sigma_finite.measurable_emeasure_Pair1
   1.117 +  pair_sigma_finite.measure_cut_measurable_snd -> pair_sigma_finite.measurable_emeasure_Pair2
   1.118 +  measure_space.measure_not_negative -> emeasure_not_MInf
   1.119 +  pair_sigma_finite.measure_preserving_swap -> pair_sigma_finite.distr_pair_swap
   1.120 +  pair_sigma_finite.pair_measure_alt -> pair_sigma_finite.emeasure_pair_measure_alt
   1.121 +  pair_sigma_finite.pair_measure_alt2 -> pair_sigma_finite.emeasure_pair_measure_alt2
   1.122 +  pair_sigma_finite.pair_measure_times -> pair_sigma_finite.emeasure_pair_measure_Times
   1.123 +  pair_sigma_algebra.pair_sigma_algebra_measurable -> measurable_pair_swap
   1.124 +  pair_sigma_algebra.pair_sigma_algebra_swap_measurable -> measurable_pair_swap'
   1.125 +  pair_sigma_algebra.sets_swap -> sets_pair_swap
   1.126 +  finite_product_sigma_algebra.in_P -> sets_PiM_I_finite
   1.127 +  Int_stable_product_algebra_generator -> positive_integral
   1.128 +  product_sigma_finite.measure_fold -> product_sigma_finite.distr_merge
   1.129 +  product_sigma_finite.measure_preserving_component_singelton -> product_sigma_finite.distr_singleton
   1.130 +  product_sigma_finite.measure_preserving_merge -> product_sigma_finite.distr_merge
   1.131 +  finite_product_sigma_algebra.P_empty -> space_PiM_empty, sets_PiM_empty
   1.132 +  product_algebra_generator_der -> prod_algebra_eq_finite
   1.133 +  product_algebra_generator_into_space -> prod_algebra_sets_into_space
   1.134 +  product_sigma_algebra.product_algebra_into_space -> space_closed
   1.135 +  product_algebraE -> prod_algebraE_all
   1.136 +  product_algebraI -> sets_PiM_I_finite
   1.137 +  product_measure_exists -> product_sigma_finite.sigma_finite
   1.138 +  sets_product_algebra -> sets_PiM
   1.139 +  sigma_product_algebra_sigma_eq -> sigma_prod_algebra_sigma_eq
   1.140 +  space_product_algebra -> space_PiM
   1.141 +  Int_stable_cuboids -> Int_stable_atLeastAtMost
   1.142 +  measure_space.density_is_absolutely_continuous -> absolutely_continuousI_density
   1.143 +  sigma_finite_measure.RN_deriv_vimage -> sigma_finite_measure.RN_deriv_distr
   1.144 +  prob_space_unique_Int_stable -> measure_eqI_prob_space
   1.145 +  sigma_finite_measure.disjoint_sigma_finite -> sigma_finite_disjoint
   1.146 +  prob_space.measure_space_1 -> prob_space.emeasure_space_1
   1.147 +  prob_space.prob_space_vimage -> prob_space_distr
   1.148 +  prob_space.random_variable_restrict -> measurable_restrict
   1.149 +  measure_preserving -> equality "distr M N f = N" "f : measurable M N"
   1.150 +  measure_unique_Int_stable_vimage -> measure_eqI_generator_eq
   1.151 +  measure_space.measure_preserving_Int_stable -> measure_eqI_generator_eq
   1.152 +  product_prob_space.finite_index_eq_finite_product -> product_prob_space.sets_PiM_generator
   1.153 +  product_prob_space.finite_measure_infprod_emb_Pi -> product_prob_space.measure_PiM_emb
   1.154 +  finite_product_prob_space.finite_measure_times -> finite_product_prob_space.finite_measure_PiM_emb
   1.155 +  product_prob_space.infprod_spec -> product_prob_space.emeasure_PiM_emb_not_empty
   1.156 +  product_prob_space.measurable_component -> measurable_component_singleton
   1.157 +  product_prob_space.measurable_emb -> measurable_prod_emb
   1.158 +  product_prob_space.measurable_into_infprod_algebra -> measurable_PiM_single
   1.159 +  product_prob_space.measurable_singleton_infprod -> measurable_component_singleton
   1.160 +  product_prob_space.measure_emb -> emeasure_prod_emb
   1.161 +  sequence_space.measure_infprod -> sequence_space.measure_PiM_countable
   1.162 +  product_prob_space.measure_preserving_restrict -> product_prob_space.distr_restrict
   1.163 +  prob_space.indep_distribution_eq_measure -> prob_space.indep_vars_iff_distr_eq_PiM
   1.164 +  prob_space.indep_var_distributionD -> prob_space.indep_var_distribution_eq
   1.165 +  conditional_entropy_positive -> conditional_entropy_nonneg_simple
   1.166 +  conditional_entropy_eq -> conditional_entropy_simple_distributed
   1.167 +  conditional_mutual_information_eq_mutual_information -> conditional_mutual_information_eq_mutual_information_simple
   1.168 +  conditional_mutual_information_generic_positive -> conditional_mutual_information_nonneg_simple
   1.169 +  conditional_mutual_information_positive -> conditional_mutual_information_nonneg_simple
   1.170 +  entropy_commute -> entropy_commute_simple
   1.171 +  entropy_eq -> entropy_simple_distributed
   1.172 +  entropy_generic_eq -> entropy_simple_distributed
   1.173 +  entropy_positive -> entropy_nonneg_simple
   1.174 +  entropy_uniform_max -> entropy_uniform
   1.175 +  KL_eq_0 -> KL_same_eq_0
   1.176 +  KL_eq_0_imp -> KL_eq_0_iff_eq
   1.177 +  KL_ge_0 -> KL_nonneg
   1.178 +  mutual_information_eq -> mutual_information_simple_distributed
   1.179 +  mutual_information_positive -> mutual_information_nonneg_simple
   1.180 +
   1.181  * New "case_product" attribute to generate a case rule doing multiple
   1.182  case distinctions at the same time.  E.g.
   1.183  
   1.184 @@ -655,8 +829,6 @@
   1.185  produces a rule which can be used to perform case distinction on both
   1.186  a list and a nat.
   1.187  
   1.188 -* Theory Library/Multiset: Improved code generation of multisets.
   1.189 -
   1.190  * New Transfer package:
   1.191  
   1.192    - transfer_rule attribute: Maintains a collection of transfer rules,
     2.1 --- a/src/HOL/IsaMakefile	Mon Apr 23 12:23:23 2012 +0100
     2.2 +++ b/src/HOL/IsaMakefile	Mon Apr 23 12:14:35 2012 +0200
     2.3 @@ -1198,14 +1198,13 @@
     2.4  $(OUT)/HOL-Probability: $(OUT)/HOL-Multivariate_Analysis		\
     2.5    Probability/Binary_Product_Measure.thy Probability/Borel_Space.thy	\
     2.6    Probability/Caratheodory.thy Probability/Complete_Measure.thy		\
     2.7 -  Probability/Conditional_Probability.thy				\
     2.8    Probability/ex/Dining_Cryptographers.thy				\
     2.9    Probability/ex/Koepf_Duermuth_Countermeasure.thy			\
    2.10    Probability/Finite_Product_Measure.thy				\
    2.11    Probability/Independent_Family.thy					\
    2.12    Probability/Infinite_Product_Measure.thy Probability/Information.thy	\
    2.13    Probability/Lebesgue_Integration.thy Probability/Lebesgue_Measure.thy \
    2.14 -  Probability/Measure.thy Probability/Probability_Measure.thy		\
    2.15 +  Probability/Measure_Space.thy Probability/Probability_Measure.thy	\
    2.16    Probability/Probability.thy Probability/Radon_Nikodym.thy		\
    2.17    Probability/ROOT.ML Probability/Sigma_Algebra.thy			\
    2.18    Library/Countable.thy Library/FuncSet.thy Library/Nat_Bijection.thy
     3.1 --- a/src/HOL/Probability/Binary_Product_Measure.thy	Mon Apr 23 12:23:23 2012 +0100
     3.2 +++ b/src/HOL/Probability/Binary_Product_Measure.thy	Mon Apr 23 12:14:35 2012 +0200
     3.3 @@ -28,413 +28,317 @@
     3.4  
     3.5  section "Binary products"
     3.6  
     3.7 -definition
     3.8 -  "pair_measure_generator A B =
     3.9 -    \<lparr> space = space A \<times> space B,
    3.10 -      sets = {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B},
    3.11 -      measure = \<lambda>X. \<integral>\<^isup>+x. (\<integral>\<^isup>+y. indicator X (x,y) \<partial>B) \<partial>A \<rparr>"
    3.12 -
    3.13  definition pair_measure (infixr "\<Otimes>\<^isub>M" 80) where
    3.14 -  "A \<Otimes>\<^isub>M B = sigma (pair_measure_generator A B)"
    3.15 -
    3.16 -locale pair_sigma_algebra = M1: sigma_algebra M1 + M2: sigma_algebra M2
    3.17 -  for M1 :: "('a, 'c) measure_space_scheme" and M2 :: "('b, 'd) measure_space_scheme"
    3.18 -
    3.19 -abbreviation (in pair_sigma_algebra)
    3.20 -  "E \<equiv> pair_measure_generator M1 M2"
    3.21 -
    3.22 -abbreviation (in pair_sigma_algebra)
    3.23 -  "P \<equiv> M1 \<Otimes>\<^isub>M M2"
    3.24 -
    3.25 -lemma sigma_algebra_pair_measure:
    3.26 -  "sets M1 \<subseteq> Pow (space M1) \<Longrightarrow> sets M2 \<subseteq> Pow (space M2) \<Longrightarrow> sigma_algebra (pair_measure M1 M2)"
    3.27 -  by (force simp: pair_measure_def pair_measure_generator_def intro!: sigma_algebra_sigma)
    3.28 -
    3.29 -sublocale pair_sigma_algebra \<subseteq> sigma_algebra P
    3.30 -  using M1.space_closed M2.space_closed
    3.31 -  by (rule sigma_algebra_pair_measure)
    3.32 -
    3.33 -lemma pair_measure_generatorI[intro, simp]:
    3.34 -  "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (pair_measure_generator A B)"
    3.35 -  by (auto simp add: pair_measure_generator_def)
    3.36 -
    3.37 -lemma pair_measureI[intro, simp]:
    3.38 -  "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^isub>M B)"
    3.39 -  by (auto simp add: pair_measure_def)
    3.40 +  "A \<Otimes>\<^isub>M B = measure_of (space A \<times> space B)
    3.41 +      {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}
    3.42 +      (\<lambda>X. \<integral>\<^isup>+x. (\<integral>\<^isup>+y. indicator X (x,y) \<partial>B) \<partial>A)"
    3.43  
    3.44  lemma space_pair_measure:
    3.45    "space (A \<Otimes>\<^isub>M B) = space A \<times> space B"
    3.46 -  by (simp add: pair_measure_def pair_measure_generator_def)
    3.47 +  unfolding pair_measure_def using space_closed[of A] space_closed[of B]
    3.48 +  by (intro space_measure_of) auto
    3.49 +
    3.50 +lemma sets_pair_measure:
    3.51 +  "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}"
    3.52 +  unfolding pair_measure_def using space_closed[of A] space_closed[of B]
    3.53 +  by (intro sets_measure_of) auto
    3.54  
    3.55 -lemma sets_pair_measure_generator:
    3.56 -  "sets (pair_measure_generator N M) = (\<lambda>(x, y). x \<times> y) ` (sets N \<times> sets M)"
    3.57 -  unfolding pair_measure_generator_def by auto
    3.58 +lemma pair_measureI[intro, simp]:
    3.59 +  "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^isub>M B)"
    3.60 +  by (auto simp: sets_pair_measure)
    3.61  
    3.62 -lemma pair_measure_generator_sets_into_space:
    3.63 -  assumes "sets M \<subseteq> Pow (space M)" "sets N \<subseteq> Pow (space N)"
    3.64 -  shows "sets (pair_measure_generator M N) \<subseteq> Pow (space (pair_measure_generator M N))"
    3.65 -  using assms by (auto simp: pair_measure_generator_def)
    3.66 +lemma measurable_pair_measureI:
    3.67 +  assumes 1: "f \<in> space M \<rightarrow> space M1 \<times> space M2"
    3.68 +  assumes 2: "\<And>A B. A \<in> sets M1 \<Longrightarrow> B \<in> sets M2 \<Longrightarrow> f -` (A \<times> B) \<inter> space M \<in> sets M"
    3.69 +  shows "f \<in> measurable M (M1 \<Otimes>\<^isub>M M2)"
    3.70 +  unfolding pair_measure_def using 1 2
    3.71 +  by (intro measurable_measure_of) (auto dest: sets_into_space)
    3.72  
    3.73 -lemma pair_measure_generator_Int_snd:
    3.74 -  assumes "sets S1 \<subseteq> Pow (space S1)"
    3.75 -  shows "sets (pair_measure_generator S1 (algebra.restricted_space S2 A)) =
    3.76 -         sets (algebra.restricted_space (pair_measure_generator S1 S2) (space S1 \<times> A))"
    3.77 -  (is "?L = ?R")
    3.78 -  apply (auto simp: pair_measure_generator_def image_iff)
    3.79 -  using assms
    3.80 -  apply (rule_tac x="a \<times> xa" in exI)
    3.81 -  apply force
    3.82 -  using assms
    3.83 -  apply (rule_tac x="a" in exI)
    3.84 -  apply (rule_tac x="b \<inter> A" in exI)
    3.85 -  apply auto
    3.86 -  done
    3.87 +lemma measurable_fst[intro!, simp]: "fst \<in> measurable (M1 \<Otimes>\<^isub>M M2) M1"
    3.88 +  unfolding measurable_def
    3.89 +proof safe
    3.90 +  fix A assume A: "A \<in> sets M1"
    3.91 +  from this[THEN sets_into_space] have "fst -` A \<inter> space M1 \<times> space M2 = A \<times> space M2" by auto
    3.92 +  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)
    3.93 +qed (simp add: space_pair_measure)
    3.94  
    3.95 -lemma (in pair_sigma_algebra)
    3.96 -  shows measurable_fst[intro!, simp]:
    3.97 -    "fst \<in> measurable P M1" (is ?fst)
    3.98 -  and measurable_snd[intro!, simp]:
    3.99 -    "snd \<in> measurable P M2" (is ?snd)
   3.100 -proof -
   3.101 -  { fix X assume "X \<in> sets M1"
   3.102 -    then have "\<exists>X1\<in>sets M1. \<exists>X2\<in>sets M2. fst -` X \<inter> space M1 \<times> space M2 = X1 \<times> X2"
   3.103 -      apply - apply (rule bexI[of _ X]) apply (rule bexI[of _ "space M2"])
   3.104 -      using M1.sets_into_space by force+ }
   3.105 -  moreover
   3.106 -  { fix X assume "X \<in> sets M2"
   3.107 -    then have "\<exists>X1\<in>sets M1. \<exists>X2\<in>sets M2. snd -` X \<inter> space M1 \<times> space M2 = X1 \<times> X2"
   3.108 -      apply - apply (rule bexI[of _ "space M1"]) apply (rule bexI[of _ X])
   3.109 -      using M2.sets_into_space by force+ }
   3.110 -  ultimately have "?fst \<and> ?snd"
   3.111 -    by (fastforce simp: measurable_def sets_sigma space_pair_measure
   3.112 -                 intro!: sigma_sets.Basic)
   3.113 -  then show ?fst ?snd by auto
   3.114 -qed
   3.115 +lemma measurable_snd[intro!, simp]: "snd \<in> measurable (M1 \<Otimes>\<^isub>M M2) M2"
   3.116 +  unfolding measurable_def
   3.117 +proof safe
   3.118 +  fix A assume A: "A \<in> sets M2"
   3.119 +  from this[THEN sets_into_space] have "snd -` A \<inter> space M1 \<times> space M2 = space M1 \<times> A" by auto
   3.120 +  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)
   3.121 +qed (simp add: space_pair_measure)
   3.122 +
   3.123 +lemma measurable_fst': "f \<in> measurable M (N \<Otimes>\<^isub>M P) \<Longrightarrow> (\<lambda>x. fst (f x)) \<in> measurable M N"
   3.124 +  using measurable_comp[OF _ measurable_fst] by (auto simp: comp_def)
   3.125 +
   3.126 +lemma measurable_snd': "f \<in> measurable M (N \<Otimes>\<^isub>M P) \<Longrightarrow> (\<lambda>x. snd (f x)) \<in> measurable M P"
   3.127 +    using measurable_comp[OF _ measurable_snd] by (auto simp: comp_def)
   3.128  
   3.129 -lemma (in pair_sigma_algebra) measurable_pair_iff:
   3.130 -  assumes "sigma_algebra M"
   3.131 -  shows "f \<in> measurable M P \<longleftrightarrow>
   3.132 -    (fst \<circ> f) \<in> measurable M M1 \<and> (snd \<circ> f) \<in> measurable M M2"
   3.133 -proof -
   3.134 -  interpret M: sigma_algebra M by fact
   3.135 -  from assms show ?thesis
   3.136 -  proof (safe intro!: measurable_comp[where b=P])
   3.137 -    assume f: "(fst \<circ> f) \<in> measurable M M1" and s: "(snd \<circ> f) \<in> measurable M M2"
   3.138 -    show "f \<in> measurable M P" unfolding pair_measure_def
   3.139 -    proof (rule M.measurable_sigma)
   3.140 -      show "sets (pair_measure_generator M1 M2) \<subseteq> Pow (space E)"
   3.141 -        unfolding pair_measure_generator_def using M1.sets_into_space M2.sets_into_space by auto
   3.142 -      show "f \<in> space M \<rightarrow> space E"
   3.143 -        using f s by (auto simp: mem_Times_iff measurable_def comp_def space_sigma pair_measure_generator_def)
   3.144 -      fix A assume "A \<in> sets E"
   3.145 -      then obtain B C where "B \<in> sets M1" "C \<in> sets M2" "A = B \<times> C"
   3.146 -        unfolding pair_measure_generator_def by auto
   3.147 -      moreover have "(fst \<circ> f) -` B \<inter> space M \<in> sets M"
   3.148 -        using f `B \<in> sets M1` unfolding measurable_def by auto
   3.149 -      moreover have "(snd \<circ> f) -` C \<inter> space M \<in> sets M"
   3.150 -        using s `C \<in> sets M2` unfolding measurable_def by auto
   3.151 -      moreover have "f -` A \<inter> space M = ((fst \<circ> f) -` B \<inter> space M) \<inter> ((snd \<circ> f) -` C \<inter> space M)"
   3.152 -        unfolding `A = B \<times> C` by (auto simp: vimage_Times)
   3.153 -      ultimately show "f -` A \<inter> space M \<in> sets M" by auto
   3.154 -    qed
   3.155 +lemma measurable_fst'': "f \<in> measurable M N \<Longrightarrow> (\<lambda>x. f (fst x)) \<in> measurable (M \<Otimes>\<^isub>M P) N"
   3.156 +  using measurable_comp[OF measurable_fst _] by (auto simp: comp_def)
   3.157 +
   3.158 +lemma measurable_snd'': "f \<in> measurable M N \<Longrightarrow> (\<lambda>x. f (snd x)) \<in> measurable (P \<Otimes>\<^isub>M M) N"
   3.159 +  using measurable_comp[OF measurable_snd _] by (auto simp: comp_def)
   3.160 +
   3.161 +lemma measurable_pair_iff:
   3.162 +  "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"
   3.163 +proof safe
   3.164 +  assume f: "(fst \<circ> f) \<in> measurable M M1" and s: "(snd \<circ> f) \<in> measurable M M2"
   3.165 +  show "f \<in> measurable M (M1 \<Otimes>\<^isub>M M2)"
   3.166 +  proof (rule measurable_pair_measureI)
   3.167 +    show "f \<in> space M \<rightarrow> space M1 \<times> space M2"
   3.168 +      using f s by (auto simp: mem_Times_iff measurable_def comp_def)
   3.169 +    fix A B assume "A \<in> sets M1" "B \<in> sets M2"
   3.170 +    moreover have "(fst \<circ> f) -` A \<inter> space M \<in> sets M" "(snd \<circ> f) -` B \<inter> space M \<in> sets M"
   3.171 +      using f `A \<in> sets M1` s `B \<in> sets M2` by (auto simp: measurable_sets)
   3.172 +    moreover have "f -` (A \<times> B) \<inter> space M = ((fst \<circ> f) -` A \<inter> space M) \<inter> ((snd \<circ> f) -` B \<inter> space M)"
   3.173 +      by (auto simp: vimage_Times)
   3.174 +    ultimately show "f -` (A \<times> B) \<inter> space M \<in> sets M" by auto
   3.175    qed
   3.176 -qed
   3.177 +qed auto
   3.178  
   3.179 -lemma (in pair_sigma_algebra) measurable_pair:
   3.180 -  assumes "sigma_algebra M"
   3.181 -  assumes "(fst \<circ> f) \<in> measurable M M1" "(snd \<circ> f) \<in> measurable M M2"
   3.182 -  shows "f \<in> measurable M P"
   3.183 -  unfolding measurable_pair_iff[OF assms(1)] using assms(2,3) by simp
   3.184 -
   3.185 -lemma pair_measure_generatorE:
   3.186 -  assumes "X \<in> sets (pair_measure_generator M1 M2)"
   3.187 -  obtains A B where "X = A \<times> B" "A \<in> sets M1" "B \<in> sets M2"
   3.188 -  using assms unfolding pair_measure_generator_def by auto
   3.189 +lemma measurable_pair:
   3.190 +  "(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)"
   3.191 +  unfolding measurable_pair_iff by simp
   3.192  
   3.193 -lemma (in pair_sigma_algebra) pair_measure_generator_swap:
   3.194 -  "(\<lambda>X. (\<lambda>(x,y). (y,x)) -` X \<inter> space M2 \<times> space M1) ` sets E = sets (pair_measure_generator M2 M1)"
   3.195 -proof (safe elim!: pair_measure_generatorE)
   3.196 -  fix A B assume "A \<in> sets M1" "B \<in> sets M2"
   3.197 -  moreover then have "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space M2 \<times> space M1 = B \<times> A"
   3.198 -    using M1.sets_into_space M2.sets_into_space by auto
   3.199 -  ultimately show "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space M2 \<times> space M1 \<in> sets (pair_measure_generator M2 M1)"
   3.200 -    by (auto intro: pair_measure_generatorI)
   3.201 -next
   3.202 -  fix A B assume "A \<in> sets M1" "B \<in> sets M2"
   3.203 -  then show "B \<times> A \<in> (\<lambda>X. (\<lambda>(x, y). (y, x)) -` X \<inter> space M2 \<times> space M1) ` sets E"
   3.204 -    using M1.sets_into_space M2.sets_into_space
   3.205 -    by (auto intro!: image_eqI[where x="A \<times> B"] pair_measure_generatorI)
   3.206 +lemma measurable_pair_swap': "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^isub>M M2) (M2 \<Otimes>\<^isub>M M1)"
   3.207 +proof (rule measurable_pair_measureI)
   3.208 +  fix A B assume "A \<in> sets M2" "B \<in> sets M1"
   3.209 +  moreover then have "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space (M1 \<Otimes>\<^isub>M M2) = B \<times> A"
   3.210 +    by (auto dest: sets_into_space simp: space_pair_measure)
   3.211 +  ultimately show "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space (M1 \<Otimes>\<^isub>M M2) \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   3.212 +    by auto
   3.213 +qed (auto simp add: space_pair_measure)
   3.214 +
   3.215 +lemma measurable_pair_swap:
   3.216 +  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"
   3.217 +proof -
   3.218 +  have "(\<lambda>x. f (case x of (x, y) \<Rightarrow> (y, x))) = (\<lambda>(x, y). f (y, x))" by auto
   3.219 +  then show ?thesis
   3.220 +    using measurable_comp[OF measurable_pair_swap' f] by (simp add: comp_def)
   3.221  qed
   3.222  
   3.223 -lemma (in pair_sigma_algebra) sets_pair_sigma_algebra_swap:
   3.224 -  assumes Q: "Q \<in> sets P"
   3.225 -  shows "(\<lambda>(x,y). (y, x)) -` Q \<in> sets (M2 \<Otimes>\<^isub>M M1)" (is "_ \<in> sets ?Q")
   3.226 -proof -
   3.227 -  let ?f = "\<lambda>Q. (\<lambda>(x,y). (y, x)) -` Q \<inter> space M2 \<times> space M1"
   3.228 -  have *: "(\<lambda>(x,y). (y, x)) -` Q = ?f Q"
   3.229 -    using sets_into_space[OF Q] by (auto simp: space_pair_measure)
   3.230 -  have "sets (M2 \<Otimes>\<^isub>M M1) = sets (sigma (pair_measure_generator M2 M1))"
   3.231 -    unfolding pair_measure_def ..
   3.232 -  also have "\<dots> = sigma_sets (space M2 \<times> space M1) (?f ` sets E)"
   3.233 -    unfolding sigma_def pair_measure_generator_swap[symmetric]
   3.234 -    by (simp add: pair_measure_generator_def)
   3.235 -  also have "\<dots> = ?f ` sigma_sets (space M1 \<times> space M2) (sets E)"
   3.236 -    using M1.sets_into_space M2.sets_into_space
   3.237 -    by (intro sigma_sets_vimage) (auto simp: pair_measure_generator_def)
   3.238 -  also have "\<dots> = ?f ` sets P"
   3.239 -    unfolding pair_measure_def pair_measure_generator_def sigma_def by simp
   3.240 -  finally show ?thesis
   3.241 -    using Q by (subst *) auto
   3.242 -qed
   3.243 +lemma measurable_pair_swap_iff:
   3.244 +  "f \<in> measurable (M2 \<Otimes>\<^isub>M M1) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable (M1 \<Otimes>\<^isub>M M2) M"
   3.245 +  using measurable_pair_swap[of "\<lambda>(x,y). f (y, x)"]
   3.246 +  by (auto intro!: measurable_pair_swap)
   3.247  
   3.248 -lemma (in pair_sigma_algebra) pair_sigma_algebra_swap_measurable:
   3.249 -  shows "(\<lambda>(x,y). (y, x)) \<in> measurable P (M2 \<Otimes>\<^isub>M M1)"
   3.250 -    (is "?f \<in> measurable ?P ?Q")
   3.251 -  unfolding measurable_def
   3.252 -proof (intro CollectI conjI Pi_I ballI)
   3.253 -  fix x assume "x \<in> space ?P" then show "(case x of (x, y) \<Rightarrow> (y, x)) \<in> space ?Q"
   3.254 -    unfolding pair_measure_generator_def pair_measure_def by auto
   3.255 -next
   3.256 -  fix A assume "A \<in> sets (M2 \<Otimes>\<^isub>M M1)"
   3.257 -  interpret Q: pair_sigma_algebra M2 M1 by default
   3.258 -  from Q.sets_pair_sigma_algebra_swap[OF `A \<in> sets (M2 \<Otimes>\<^isub>M M1)`]
   3.259 -  show "?f -` A \<inter> space ?P \<in> sets ?P" by simp
   3.260 -qed
   3.261 +lemma measurable_Pair1': "x \<in> space M1 \<Longrightarrow> Pair x \<in> measurable M2 (M1 \<Otimes>\<^isub>M M2)"
   3.262 +proof (rule measurable_pair_measureI)
   3.263 +  fix A B assume "A \<in> sets M1" "B \<in> sets M2"
   3.264 +  moreover then have "Pair x -` (A \<times> B) \<inter> space M2 = (if x \<in> A then B else {})"
   3.265 +    by (auto dest: sets_into_space simp: space_pair_measure)
   3.266 +  ultimately show "Pair x -` (A \<times> B) \<inter> space M2 \<in> sets M2"
   3.267 +    by auto
   3.268 +qed (auto simp add: space_pair_measure)
   3.269  
   3.270 -lemma (in pair_sigma_algebra) measurable_cut_fst[simp,intro]:
   3.271 -  assumes "Q \<in> sets P" shows "Pair x -` Q \<in> sets M2"
   3.272 +lemma sets_Pair1: assumes A: "A \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "Pair x -` A \<in> sets M2"
   3.273  proof -
   3.274 -  let ?Q' = "{Q. Q \<subseteq> space P \<and> Pair x -` Q \<in> sets M2}"
   3.275 -  let ?Q = "\<lparr> space = space P, sets = ?Q' \<rparr>"
   3.276 -  interpret Q: sigma_algebra ?Q
   3.277 -    proof qed (auto simp: vimage_UN vimage_Diff space_pair_measure)
   3.278 -  have "sets E \<subseteq> sets ?Q"
   3.279 -    using M1.sets_into_space M2.sets_into_space
   3.280 -    by (auto simp: pair_measure_generator_def space_pair_measure)
   3.281 -  then have "sets P \<subseteq> sets ?Q"
   3.282 -    apply (subst pair_measure_def, intro Q.sets_sigma_subset)
   3.283 -    by (simp add: pair_measure_def)
   3.284 -  with assms show ?thesis by auto
   3.285 +  have "Pair x -` A = (if x \<in> space M1 then Pair x -` A \<inter> space M2 else {})"
   3.286 +    using A[THEN sets_into_space] by (auto simp: space_pair_measure)
   3.287 +  also have "\<dots> \<in> sets M2"
   3.288 +    using A by (auto simp add: measurable_Pair1' intro!: measurable_sets split: split_if_asm)
   3.289 +  finally show ?thesis .
   3.290  qed
   3.291  
   3.292 -lemma (in pair_sigma_algebra) measurable_cut_snd:
   3.293 -  assumes Q: "Q \<in> sets P" shows "(\<lambda>x. (x, y)) -` Q \<in> sets M1" (is "?cut Q \<in> sets M1")
   3.294 -proof -
   3.295 -  interpret Q: pair_sigma_algebra M2 M1 by default
   3.296 -  from Q.measurable_cut_fst[OF sets_pair_sigma_algebra_swap[OF Q], of y]
   3.297 -  show ?thesis by (simp add: vimage_compose[symmetric] comp_def)
   3.298 -qed
   3.299 +lemma measurable_Pair2': "y \<in> space M2 \<Longrightarrow> (\<lambda>x. (x, y)) \<in> measurable M1 (M1 \<Otimes>\<^isub>M M2)"
   3.300 +  using measurable_comp[OF measurable_Pair1' measurable_pair_swap', of y M2 M1]
   3.301 +  by (simp add: comp_def)
   3.302  
   3.303 -lemma (in pair_sigma_algebra) measurable_pair_image_snd:
   3.304 -  assumes m: "f \<in> measurable P M" and "x \<in> space M1"
   3.305 -  shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"
   3.306 -  unfolding measurable_def
   3.307 -proof (intro CollectI conjI Pi_I ballI)
   3.308 -  fix y assume "y \<in> space M2" with `f \<in> measurable P M` `x \<in> space M1`
   3.309 -  show "f (x, y) \<in> space M"
   3.310 -    unfolding measurable_def pair_measure_generator_def pair_measure_def by auto
   3.311 -next
   3.312 -  fix A assume "A \<in> sets M"
   3.313 -  then have "Pair x -` (f -` A \<inter> space P) \<in> sets M2" (is "?C \<in> _")
   3.314 -    using `f \<in> measurable P M`
   3.315 -    by (intro measurable_cut_fst) (auto simp: measurable_def)
   3.316 -  also have "?C = (\<lambda>y. f (x, y)) -` A \<inter> space M2"
   3.317 -    using `x \<in> space M1` by (auto simp: pair_measure_generator_def pair_measure_def)
   3.318 -  finally show "(\<lambda>y. f (x, y)) -` A \<inter> space M2 \<in> sets M2" .
   3.319 +lemma sets_Pair2: assumes A: "A \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "(\<lambda>x. (x, y)) -` A \<in> sets M1"
   3.320 +proof -
   3.321 +  have "(\<lambda>x. (x, y)) -` A = (if y \<in> space M2 then (\<lambda>x. (x, y)) -` A \<inter> space M1 else {})"
   3.322 +    using A[THEN sets_into_space] by (auto simp: space_pair_measure)
   3.323 +  also have "\<dots> \<in> sets M1"
   3.324 +    using A by (auto simp add: measurable_Pair2' intro!: measurable_sets split: split_if_asm)
   3.325 +  finally show ?thesis .
   3.326  qed
   3.327  
   3.328 -lemma (in pair_sigma_algebra) measurable_pair_image_fst:
   3.329 -  assumes m: "f \<in> measurable P M" and "y \<in> space M2"
   3.330 +lemma measurable_Pair2:
   3.331 +  assumes f: "f \<in> measurable (M1 \<Otimes>\<^isub>M M2) M" and x: "x \<in> space M1"
   3.332 +  shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"
   3.333 +  using measurable_comp[OF measurable_Pair1' f, OF x]
   3.334 +  by (simp add: comp_def)
   3.335 +  
   3.336 +lemma measurable_Pair1:
   3.337 +  assumes f: "f \<in> measurable (M1 \<Otimes>\<^isub>M M2) M" and y: "y \<in> space M2"
   3.338    shows "(\<lambda>x. f (x, y)) \<in> measurable M1 M"
   3.339 -proof -
   3.340 -  interpret Q: pair_sigma_algebra M2 M1 by default
   3.341 -  from Q.measurable_pair_image_snd[OF measurable_comp `y \<in> space M2`,
   3.342 -                                      OF Q.pair_sigma_algebra_swap_measurable m]
   3.343 -  show ?thesis by simp
   3.344 -qed
   3.345 +  using measurable_comp[OF measurable_Pair2' f, OF y]
   3.346 +  by (simp add: comp_def)
   3.347  
   3.348 -lemma (in pair_sigma_algebra) Int_stable_pair_measure_generator: "Int_stable E"
   3.349 +lemma Int_stable_pair_measure_generator: "Int_stable {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}"
   3.350    unfolding Int_stable_def
   3.351 -proof (intro ballI)
   3.352 -  fix A B assume "A \<in> sets E" "B \<in> sets E"
   3.353 -  then obtain A1 A2 B1 B2 where "A = A1 \<times> A2" "B = B1 \<times> B2"
   3.354 -    "A1 \<in> sets M1" "A2 \<in> sets M2" "B1 \<in> sets M1" "B2 \<in> sets M2"
   3.355 -    unfolding pair_measure_generator_def by auto
   3.356 -  then show "A \<inter> B \<in> sets E"
   3.357 -    by (auto simp add: times_Int_times pair_measure_generator_def)
   3.358 -qed
   3.359 +  by safe (auto simp add: times_Int_times)
   3.360  
   3.361  lemma finite_measure_cut_measurable:
   3.362 -  fixes M1 :: "('a, 'c) measure_space_scheme" and M2 :: "('b, 'd) measure_space_scheme"
   3.363    assumes "sigma_finite_measure M1" "finite_measure M2"
   3.364    assumes "Q \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   3.365 -  shows "(\<lambda>x. measure M2 (Pair x -` Q)) \<in> borel_measurable M1"
   3.366 +  shows "(\<lambda>x. emeasure M2 (Pair x -` Q)) \<in> borel_measurable M1"
   3.367      (is "?s Q \<in> _")
   3.368  proof -
   3.369    interpret M1: sigma_finite_measure M1 by fact
   3.370    interpret M2: finite_measure M2 by fact
   3.371 -  interpret pair_sigma_algebra M1 M2 by default
   3.372 -  have [intro]: "sigma_algebra M1" by fact
   3.373 -  have [intro]: "sigma_algebra M2" by fact
   3.374 -  let ?D = "\<lparr> space = space P, sets = {A\<in>sets P. ?s A \<in> borel_measurable M1}  \<rparr>"
   3.375 +  let ?\<Omega> = "space M1 \<times> space M2" and ?D = "{A\<in>sets (M1 \<Otimes>\<^isub>M M2). ?s A \<in> borel_measurable M1}"
   3.376    note space_pair_measure[simp]
   3.377 -  interpret dynkin_system ?D
   3.378 +  interpret dynkin_system ?\<Omega> ?D
   3.379    proof (intro dynkin_systemI)
   3.380 -    fix A assume "A \<in> sets ?D" then show "A \<subseteq> space ?D"
   3.381 -      using sets_into_space by simp
   3.382 +    fix A assume "A \<in> ?D" then show "A \<subseteq> ?\<Omega>"
   3.383 +      using sets_into_space[of A "M1 \<Otimes>\<^isub>M M2"] by simp
   3.384    next
   3.385 -    from top show "space ?D \<in> sets ?D"
   3.386 -      by (auto simp add: if_distrib intro!: M1.measurable_If)
   3.387 +    from top show "?\<Omega> \<in> ?D"
   3.388 +      by (auto simp add: if_distrib intro!: measurable_If)
   3.389    next
   3.390 -    fix A assume "A \<in> sets ?D"
   3.391 -    with sets_into_space have "\<And>x. measure M2 (Pair x -` (space M1 \<times> space M2 - A)) =
   3.392 -        (if x \<in> space M1 then measure M2 (space M2) - ?s A x else 0)"
   3.393 -      by (auto intro!: M2.measure_compl simp: vimage_Diff)
   3.394 -    with `A \<in> sets ?D` top show "space ?D - A \<in> sets ?D"
   3.395 -      by (auto intro!: Diff M1.measurable_If M1.borel_measurable_ereal_diff)
   3.396 +    fix A assume "A \<in> ?D"
   3.397 +    with sets_into_space have "\<And>x. emeasure M2 (Pair x -` (?\<Omega> - A)) =
   3.398 +        (if x \<in> space M1 then emeasure M2 (space M2) - ?s A x else 0)"
   3.399 +      by (auto intro!: emeasure_compl simp: vimage_Diff sets_Pair1)
   3.400 +    with `A \<in> ?D` top show "?\<Omega> - A \<in> ?D"
   3.401 +      by (auto intro!: measurable_If)
   3.402    next
   3.403 -    fix F :: "nat \<Rightarrow> ('a\<times>'b) set" assume "disjoint_family F" "range F \<subseteq> sets ?D"
   3.404 -    moreover then have "\<And>x. measure M2 (\<Union>i. Pair x -` F i) = (\<Sum>i. ?s (F i) x)"
   3.405 -      by (intro M2.measure_countably_additive[symmetric])
   3.406 -         (auto simp: disjoint_family_on_def)
   3.407 -    ultimately show "(\<Union>i. F i) \<in> sets ?D"
   3.408 -      by (auto simp: vimage_UN intro!: M1.borel_measurable_psuminf)
   3.409 +    fix F :: "nat \<Rightarrow> ('a\<times>'b) set" assume "disjoint_family F" "range F \<subseteq> ?D"
   3.410 +    moreover then have "\<And>x. emeasure M2 (\<Union>i. Pair x -` F i) = (\<Sum>i. ?s (F i) x)"
   3.411 +      by (intro suminf_emeasure[symmetric]) (auto simp: disjoint_family_on_def sets_Pair1)
   3.412 +    ultimately show "(\<Union>i. F i) \<in> ?D"
   3.413 +      by (auto simp: vimage_UN intro!: borel_measurable_psuminf)
   3.414    qed
   3.415 -  have "sets P = sets ?D" apply (subst pair_measure_def)
   3.416 -  proof (intro dynkin_lemma)
   3.417 -    show "Int_stable E" by (rule Int_stable_pair_measure_generator)
   3.418 -    from M1.sets_into_space have "\<And>A. A \<in> sets M1 \<Longrightarrow> {x \<in> space M1. x \<in> A} = A"
   3.419 -      by auto
   3.420 -    then show "sets E \<subseteq> sets ?D"
   3.421 -      by (auto simp: pair_measure_generator_def sets_sigma if_distrib
   3.422 -               intro: sigma_sets.Basic intro!: M1.measurable_If)
   3.423 -  qed (auto simp: pair_measure_def)
   3.424 -  with `Q \<in> sets P` have "Q \<in> sets ?D" by simp
   3.425 +  let ?G = "{a \<times> b | a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   3.426 +  have "sigma_sets ?\<Omega> ?G = ?D"
   3.427 +  proof (rule dynkin_lemma)
   3.428 +    show "?G \<subseteq> ?D"
   3.429 +      by (auto simp: if_distrib Int_def[symmetric] intro!: measurable_If)
   3.430 +  qed (auto simp: sets_pair_measure  Int_stable_pair_measure_generator)
   3.431 +  with `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)` have "Q \<in> ?D"
   3.432 +    by (simp add: sets_pair_measure[symmetric])
   3.433    then show "?s Q \<in> borel_measurable M1" by simp
   3.434  qed
   3.435  
   3.436 -subsection {* Binary products of $\sigma$-finite measure spaces *}
   3.437 +subsection {* Binary products of $\sigma$-finite emeasure spaces *}
   3.438  
   3.439 -locale pair_sigma_finite = pair_sigma_algebra M1 M2 + M1: sigma_finite_measure M1 + M2: sigma_finite_measure M2
   3.440 -  for M1 :: "('a, 'c) measure_space_scheme" and M2 :: "('b, 'd) measure_space_scheme"
   3.441 +locale pair_sigma_finite = M1: sigma_finite_measure M1 + M2: sigma_finite_measure M2
   3.442 +  for M1 :: "'a measure" and M2 :: "'b measure"
   3.443  
   3.444 -lemma (in pair_sigma_finite) measure_cut_measurable_fst:
   3.445 -  assumes "Q \<in> sets P" shows "(\<lambda>x. measure M2 (Pair x -` Q)) \<in> borel_measurable M1" (is "?s Q \<in> _")
   3.446 +lemma sets_pair_measure_cong[cong]:
   3.447 +  "sets M1 = sets M1' \<Longrightarrow> sets M2 = sets M2' \<Longrightarrow> sets (M1 \<Otimes>\<^isub>M M2) = sets (M1' \<Otimes>\<^isub>M M2')"
   3.448 +  unfolding sets_pair_measure by (simp cong: sets_eq_imp_space_eq)
   3.449 +
   3.450 +lemma (in pair_sigma_finite) measurable_emeasure_Pair1:
   3.451 +  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> _")
   3.452  proof -
   3.453 -  have [intro]: "sigma_algebra M1" and [intro]: "sigma_algebra M2" by default+
   3.454 -  have M1: "sigma_finite_measure M1" by default
   3.455 -  from M2.disjoint_sigma_finite guess F .. note F = this
   3.456 +  from M2.sigma_finite_disjoint guess F . note F = this
   3.457    then have F_sets: "\<And>i. F i \<in> sets M2" by auto
   3.458 +  have M1: "sigma_finite_measure M1" ..
   3.459    let ?C = "\<lambda>x i. F i \<inter> Pair x -` Q"
   3.460    { fix i
   3.461 -    let ?R = "M2.restricted_space (F i)"
   3.462      have [simp]: "space M1 \<times> F i \<inter> space M1 \<times> space M2 = space M1 \<times> F i"
   3.463 -      using F M2.sets_into_space by auto
   3.464 -    let ?R2 = "M2.restricted_space (F i)"
   3.465 -    have "(\<lambda>x. measure ?R2 (Pair x -` (space M1 \<times> space ?R2 \<inter> Q))) \<in> borel_measurable M1"
   3.466 +      using F sets_into_space by auto
   3.467 +    let ?R = "density M2 (indicator (F i))"
   3.468 +    have "(\<lambda>x. emeasure ?R (Pair x -` (space M1 \<times> space ?R \<inter> Q))) \<in> borel_measurable M1"
   3.469      proof (intro finite_measure_cut_measurable[OF M1])
   3.470 -      show "finite_measure ?R2"
   3.471 -        using F by (intro M2.restricted_to_finite_measure) auto
   3.472 -      have "(space M1 \<times> space ?R2) \<inter> Q \<in> (op \<inter> (space M1 \<times> F i)) ` sets P"
   3.473 -        using `Q \<in> sets P` by (auto simp: image_iff)
   3.474 -      also have "\<dots> = sigma_sets (space M1 \<times> F i) ((op \<inter> (space M1 \<times> F i)) ` sets E)"
   3.475 -        unfolding pair_measure_def pair_measure_generator_def sigma_def
   3.476 -        using `F i \<in> sets M2` M2.sets_into_space
   3.477 -        by (auto intro!: sigma_sets_Int sigma_sets.Basic)
   3.478 -      also have "\<dots> \<subseteq> sets (M1 \<Otimes>\<^isub>M ?R2)"
   3.479 -        using M1.sets_into_space
   3.480 -        apply (auto simp: times_Int_times pair_measure_def pair_measure_generator_def sigma_def
   3.481 -                    intro!: sigma_sets_subseteq)
   3.482 -        apply (rule_tac x="a" in exI)
   3.483 -        apply (rule_tac x="b \<inter> F i" in exI)
   3.484 -        by auto
   3.485 -      finally show "(space M1 \<times> space ?R2) \<inter> Q \<in> sets (M1 \<Otimes>\<^isub>M ?R2)" .
   3.486 +      show "finite_measure ?R"
   3.487 +        using F by (intro finite_measureI) (auto simp: emeasure_restricted subset_eq)
   3.488 +      show "(space M1 \<times> space ?R) \<inter> Q \<in> sets (M1 \<Otimes>\<^isub>M ?R)"
   3.489 +        using Q by (simp add: Int)
   3.490      qed
   3.491 -    moreover have "\<And>x. Pair x -` (space M1 \<times> F i \<inter> Q) = ?C x i"
   3.492 -      using `Q \<in> sets P` sets_into_space by (auto simp: space_pair_measure)
   3.493 -    ultimately have "(\<lambda>x. measure M2 (?C x i)) \<in> borel_measurable M1"
   3.494 +    moreover have "\<And>x. emeasure ?R (Pair x -` (space M1 \<times> space ?R \<inter> Q))
   3.495 +        = emeasure M2 (F i \<inter> Pair x -` (space M1 \<times> space ?R \<inter> Q))"
   3.496 +      using Q F_sets by (intro emeasure_restricted) (auto intro: sets_Pair1)
   3.497 +    moreover have "\<And>x. F i \<inter> Pair x -` (space M1 \<times> space ?R \<inter> Q) = ?C x i"
   3.498 +      using sets_into_space[OF Q] by (auto simp: space_pair_measure)
   3.499 +    ultimately have "(\<lambda>x. emeasure M2 (?C x i)) \<in> borel_measurable M1"
   3.500        by simp }
   3.501    moreover
   3.502    { fix x
   3.503 -    have "(\<Sum>i. measure M2 (?C x i)) = measure M2 (\<Union>i. ?C x i)"
   3.504 -    proof (intro M2.measure_countably_additive)
   3.505 +    have "(\<Sum>i. emeasure M2 (?C x i)) = emeasure M2 (\<Union>i. ?C x i)"
   3.506 +    proof (intro suminf_emeasure)
   3.507        show "range (?C x) \<subseteq> sets M2"
   3.508 -        using F `Q \<in> sets P` by (auto intro!: M2.Int)
   3.509 +        using F `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)` by (auto intro!: sets_Pair1)
   3.510        have "disjoint_family F" using F by auto
   3.511        show "disjoint_family (?C x)"
   3.512          by (rule disjoint_family_on_bisimulation[OF `disjoint_family F`]) auto
   3.513      qed
   3.514      also have "(\<Union>i. ?C x i) = Pair x -` Q"
   3.515 -      using F sets_into_space `Q \<in> sets P`
   3.516 +      using F sets_into_space[OF `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)`]
   3.517        by (auto simp: space_pair_measure)
   3.518 -    finally have "measure M2 (Pair x -` Q) = (\<Sum>i. measure M2 (?C x i))"
   3.519 +    finally have "emeasure M2 (Pair x -` Q) = (\<Sum>i. emeasure M2 (?C x i))"
   3.520        by simp }
   3.521 -  ultimately show ?thesis using `Q \<in> sets P` F_sets
   3.522 -    by (auto intro!: M1.borel_measurable_psuminf M2.Int)
   3.523 +  ultimately show ?thesis using `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)` F_sets
   3.524 +    by auto
   3.525  qed
   3.526  
   3.527 -lemma (in pair_sigma_finite) measure_cut_measurable_snd:
   3.528 -  assumes "Q \<in> sets P" shows "(\<lambda>y. M1.\<mu> ((\<lambda>x. (x, y)) -` Q)) \<in> borel_measurable M2"
   3.529 +lemma (in pair_sigma_finite) measurable_emeasure_Pair2:
   3.530 +  assumes Q: "Q \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "(\<lambda>y. emeasure M1 ((\<lambda>x. (x, y)) -` Q)) \<in> borel_measurable M2"
   3.531  proof -
   3.532    interpret Q: pair_sigma_finite M2 M1 by default
   3.533 -  note sets_pair_sigma_algebra_swap[OF assms]
   3.534 -  from Q.measure_cut_measurable_fst[OF this]
   3.535 -  show ?thesis by (simp add: vimage_compose[symmetric] comp_def)
   3.536 -qed
   3.537 -
   3.538 -lemma (in pair_sigma_algebra) pair_sigma_algebra_measurable:
   3.539 -  assumes "f \<in> measurable P M" shows "(\<lambda>(x,y). f (y, x)) \<in> measurable (M2 \<Otimes>\<^isub>M M1) M"
   3.540 -proof -
   3.541 -  interpret Q: pair_sigma_algebra M2 M1 by default
   3.542 -  have *: "(\<lambda>(x,y). f (y, x)) = f \<circ> (\<lambda>(x,y). (y, x))" by (simp add: fun_eq_iff)
   3.543 -  show ?thesis
   3.544 -    using Q.pair_sigma_algebra_swap_measurable assms
   3.545 -    unfolding * by (rule measurable_comp)
   3.546 +  have "(\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^isub>M M1) \<in> sets (M2 \<Otimes>\<^isub>M M1)"
   3.547 +    using Q measurable_pair_swap' by (auto intro: measurable_sets)
   3.548 +  note Q.measurable_emeasure_Pair1[OF this]
   3.549 +  moreover have "\<And>y. Pair y -` ((\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^isub>M M1)) = (\<lambda>x. (x, y)) -` Q"
   3.550 +    using Q[THEN sets_into_space] by (auto simp: space_pair_measure)
   3.551 +  ultimately show ?thesis by simp
   3.552  qed
   3.553  
   3.554 -lemma (in pair_sigma_finite) pair_measure_alt:
   3.555 -  assumes "A \<in> sets P"
   3.556 -  shows "measure (M1 \<Otimes>\<^isub>M M2) A = (\<integral>\<^isup>+ x. measure M2 (Pair x -` A) \<partial>M1)"
   3.557 -  apply (simp add: pair_measure_def pair_measure_generator_def)
   3.558 -proof (rule M1.positive_integral_cong)
   3.559 -  fix x assume "x \<in> space M1"
   3.560 -  have *: "\<And>y. indicator A (x, y) = (indicator (Pair x -` A) y :: ereal)"
   3.561 -    unfolding indicator_def by auto
   3.562 -  show "(\<integral>\<^isup>+ y. indicator A (x, y) \<partial>M2) = measure M2 (Pair x -` A)"
   3.563 -    unfolding *
   3.564 -    apply (subst M2.positive_integral_indicator)
   3.565 -    apply (rule measurable_cut_fst[OF assms])
   3.566 -    by simp
   3.567 +lemma (in pair_sigma_finite) emeasure_pair_measure:
   3.568 +  assumes "X \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   3.569 +  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")
   3.570 +proof (rule emeasure_measure_of[OF pair_measure_def])
   3.571 +  show "positive (sets (M1 \<Otimes>\<^isub>M M2)) ?\<mu>"
   3.572 +    by (auto simp: positive_def positive_integral_positive)
   3.573 +  have eq[simp]: "\<And>A x y. indicator A (x, y) = indicator (Pair x -` A) y"
   3.574 +    by (auto simp: indicator_def)
   3.575 +  show "countably_additive (sets (M1 \<Otimes>\<^isub>M M2)) ?\<mu>"
   3.576 +  proof (rule countably_additiveI)
   3.577 +    fix F :: "nat \<Rightarrow> ('a \<times> 'b) set" assume F: "range F \<subseteq> sets (M1 \<Otimes>\<^isub>M M2)" "disjoint_family F"
   3.578 +    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
   3.579 +    moreover from F have "\<And>i. (\<lambda>x. emeasure M2 (Pair x -` F i)) \<in> borel_measurable M1"
   3.580 +      by (intro measurable_emeasure_Pair1) auto
   3.581 +    moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   3.582 +      by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   3.583 +    moreover have "\<And>x. range (\<lambda>i. Pair x -` F i) \<subseteq> sets M2"
   3.584 +      using F by (auto simp: sets_Pair1)
   3.585 +    ultimately show "(\<Sum>n. ?\<mu> (F n)) = ?\<mu> (\<Union>i. F i)"
   3.586 +      by (auto simp add: vimage_UN positive_integral_suminf[symmetric] suminf_emeasure subset_eq emeasure_nonneg sets_Pair1
   3.587 +               intro!: positive_integral_cong positive_integral_indicator[symmetric])
   3.588 +  qed
   3.589 +  show "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2} \<subseteq> Pow (space M1 \<times> space M2)"
   3.590 +    using space_closed[of M1] space_closed[of M2] by auto
   3.591 +qed fact
   3.592 +
   3.593 +lemma (in pair_sigma_finite) emeasure_pair_measure_alt:
   3.594 +  assumes X: "X \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   3.595 +  shows "emeasure (M1  \<Otimes>\<^isub>M M2) X = (\<integral>\<^isup>+x. emeasure M2 (Pair x -` X) \<partial>M1)"
   3.596 +proof -
   3.597 +  have [simp]: "\<And>x y. indicator X (x, y) = indicator (Pair x -` X) y"
   3.598 +    by (auto simp: indicator_def)
   3.599 +  show ?thesis
   3.600 +    using X by (auto intro!: positive_integral_cong simp: emeasure_pair_measure sets_Pair1)
   3.601  qed
   3.602  
   3.603 -lemma (in pair_sigma_finite) pair_measure_times:
   3.604 -  assumes A: "A \<in> sets M1" and "B \<in> sets M2"
   3.605 -  shows "measure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = M1.\<mu> A * measure M2 B"
   3.606 +lemma (in pair_sigma_finite) emeasure_pair_measure_Times:
   3.607 +  assumes A: "A \<in> sets M1" and B: "B \<in> sets M2"
   3.608 +  shows "emeasure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = emeasure M1 A * emeasure M2 B"
   3.609  proof -
   3.610 -  have "measure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = (\<integral>\<^isup>+ x. measure M2 B * indicator A x \<partial>M1)"
   3.611 -    using assms by (auto intro!: M1.positive_integral_cong simp: pair_measure_alt)
   3.612 -  with assms show ?thesis
   3.613 -    by (simp add: M1.positive_integral_cmult_indicator ac_simps)
   3.614 +  have "emeasure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = (\<integral>\<^isup>+x. emeasure M2 B * indicator A x \<partial>M1)"
   3.615 +    using A B by (auto intro!: positive_integral_cong simp: emeasure_pair_measure_alt)
   3.616 +  also have "\<dots> = emeasure M2 B * emeasure M1 A"
   3.617 +    using A by (simp add: emeasure_nonneg positive_integral_cmult_indicator)
   3.618 +  finally show ?thesis
   3.619 +    by (simp add: ac_simps)
   3.620  qed
   3.621  
   3.622 -lemma (in measure_space) measure_not_negative[simp,intro]:
   3.623 -  assumes A: "A \<in> sets M" shows "\<mu> A \<noteq> - \<infinity>"
   3.624 -  using positive_measure[OF A] by auto
   3.625 -
   3.626  lemma (in pair_sigma_finite) sigma_finite_up_in_pair_measure_generator:
   3.627 -  "\<exists>F::nat \<Rightarrow> ('a \<times> 'b) set. range F \<subseteq> sets E \<and> incseq F \<and> (\<Union>i. F i) = space E \<and>
   3.628 -    (\<forall>i. measure (M1 \<Otimes>\<^isub>M M2) (F i) \<noteq> \<infinity>)"
   3.629 +  defines "E \<equiv> {A \<times> B | A B. A \<in> sets M1 \<and> B \<in> sets M2}"
   3.630 +  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>
   3.631 +    (\<forall>i. emeasure (M1 \<Otimes>\<^isub>M M2) (F i) \<noteq> \<infinity>)"
   3.632  proof -
   3.633 -  obtain F1 :: "nat \<Rightarrow> 'a set" and F2 :: "nat \<Rightarrow> 'b set" where
   3.634 -    F1: "range F1 \<subseteq> sets M1" "incseq F1" "(\<Union>i. F1 i) = space M1" "\<And>i. M1.\<mu> (F1 i) \<noteq> \<infinity>" and
   3.635 -    F2: "range F2 \<subseteq> sets M2" "incseq F2" "(\<Union>i. F2 i) = space M2" "\<And>i. M2.\<mu> (F2 i) \<noteq> \<infinity>"
   3.636 -    using M1.sigma_finite_up M2.sigma_finite_up by auto
   3.637 -  then have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)" by auto
   3.638 +  from M1.sigma_finite_incseq guess F1 . note F1 = this
   3.639 +  from M2.sigma_finite_incseq guess F2 . note F2 = this
   3.640 +  from F1 F2 have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)" by auto
   3.641    let ?F = "\<lambda>i. F1 i \<times> F2 i"
   3.642 -  show ?thesis unfolding space_pair_measure
   3.643 +  show ?thesis
   3.644    proof (intro exI[of _ ?F] conjI allI)
   3.645 -    show "range ?F \<subseteq> sets E" using F1 F2
   3.646 -      by (fastforce intro!: pair_measure_generatorI)
   3.647 +    show "range ?F \<subseteq> E" using F1 F2 by (auto simp: E_def) (metis range_subsetD)
   3.648    next
   3.649      have "space M1 \<times> space M2 \<subseteq> (\<Union>i. ?F i)"
   3.650      proof (intro subsetI)
   3.651 @@ -448,353 +352,315 @@
   3.652          by (intro SigmaI) (auto simp add: min_max.sup_commute)
   3.653        then show "x \<in> (\<Union>i. ?F i)" by auto
   3.654      qed
   3.655 -    then show "(\<Union>i. ?F i) = space E"
   3.656 -      using space by (auto simp: space pair_measure_generator_def)
   3.657 +    then show "(\<Union>i. ?F i) = space M1 \<times> space M2"
   3.658 +      using space by (auto simp: space)
   3.659    next
   3.660      fix i show "incseq (\<lambda>i. F1 i \<times> F2 i)"
   3.661        using `incseq F1` `incseq F2` unfolding incseq_Suc_iff by auto
   3.662    next
   3.663      fix i
   3.664      from F1 F2 have "F1 i \<in> sets M1" "F2 i \<in> sets M2" by auto
   3.665 -    with F1 F2 M1.positive_measure[OF this(1)] M2.positive_measure[OF this(2)]
   3.666 -    show "measure P (F1 i \<times> F2 i) \<noteq> \<infinity>"
   3.667 -      by (simp add: pair_measure_times)
   3.668 +    with F1 F2 emeasure_nonneg[of M1 "F1 i"] emeasure_nonneg[of M2 "F2 i"]
   3.669 +    show "emeasure (M1 \<Otimes>\<^isub>M M2) (F1 i \<times> F2 i) \<noteq> \<infinity>"
   3.670 +      by (auto simp add: emeasure_pair_measure_Times)
   3.671 +  qed
   3.672 +qed
   3.673 +
   3.674 +sublocale pair_sigma_finite \<subseteq> sigma_finite_measure "M1 \<Otimes>\<^isub>M M2"
   3.675 +proof
   3.676 +  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   3.677 +  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>)"
   3.678 +  proof (rule exI[of _ F], intro conjI)
   3.679 +    show "range F \<subseteq> sets (M1 \<Otimes>\<^isub>M M2)" using F by (auto simp: pair_measure_def)
   3.680 +    show "(\<Union>i. F i) = space (M1 \<Otimes>\<^isub>M M2)"
   3.681 +      using F by (auto simp: space_pair_measure)
   3.682 +    show "\<forall>i. emeasure (M1 \<Otimes>\<^isub>M M2) (F i) \<noteq> \<infinity>" using F by auto
   3.683    qed
   3.684  qed
   3.685  
   3.686 -sublocale pair_sigma_finite \<subseteq> sigma_finite_measure P
   3.687 -proof
   3.688 -  show "positive P (measure P)"
   3.689 -    unfolding pair_measure_def pair_measure_generator_def sigma_def positive_def
   3.690 -    by (auto intro: M1.positive_integral_positive M2.positive_integral_positive)
   3.691 -
   3.692 -  show "countably_additive P (measure P)"
   3.693 -    unfolding countably_additive_def
   3.694 -  proof (intro allI impI)
   3.695 -    fix F :: "nat \<Rightarrow> ('a \<times> 'b) set"
   3.696 -    assume F: "range F \<subseteq> sets P" "disjoint_family F"
   3.697 -    from F have *: "\<And>i. F i \<in> sets P" "(\<Union>i. F i) \<in> sets P" by auto
   3.698 -    moreover from F have "\<And>i. (\<lambda>x. measure M2 (Pair x -` F i)) \<in> borel_measurable M1"
   3.699 -      by (intro measure_cut_measurable_fst) auto
   3.700 -    moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   3.701 -      by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   3.702 -    moreover have "\<And>x. x \<in> space M1 \<Longrightarrow> range (\<lambda>i. Pair x -` F i) \<subseteq> sets M2"
   3.703 -      using F by auto
   3.704 -    ultimately show "(\<Sum>n. measure P (F n)) = measure P (\<Union>i. F i)"
   3.705 -      by (simp add: pair_measure_alt vimage_UN M1.positive_integral_suminf[symmetric]
   3.706 -                    M2.measure_countably_additive
   3.707 -               cong: M1.positive_integral_cong)
   3.708 -  qed
   3.709 -
   3.710 -  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   3.711 -  show "\<exists>F::nat \<Rightarrow> ('a \<times> 'b) set. range F \<subseteq> sets P \<and> (\<Union>i. F i) = space P \<and> (\<forall>i. measure P (F i) \<noteq> \<infinity>)"
   3.712 -  proof (rule exI[of _ F], intro conjI)
   3.713 -    show "range F \<subseteq> sets P" using F by (auto simp: pair_measure_def)
   3.714 -    show "(\<Union>i. F i) = space P"
   3.715 -      using F by (auto simp: pair_measure_def pair_measure_generator_def)
   3.716 -    show "\<forall>i. measure P (F i) \<noteq> \<infinity>" using F by auto
   3.717 -  qed
   3.718 +lemma sigma_finite_pair_measure:
   3.719 +  assumes A: "sigma_finite_measure A" and B: "sigma_finite_measure B"
   3.720 +  shows "sigma_finite_measure (A \<Otimes>\<^isub>M B)"
   3.721 +proof -
   3.722 +  interpret A: sigma_finite_measure A by fact
   3.723 +  interpret B: sigma_finite_measure B by fact
   3.724 +  interpret AB: pair_sigma_finite A  B ..
   3.725 +  show ?thesis ..
   3.726  qed
   3.727  
   3.728 -lemma (in pair_sigma_algebra) sets_swap:
   3.729 -  assumes "A \<in> sets P"
   3.730 +lemma sets_pair_swap:
   3.731 +  assumes "A \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   3.732    shows "(\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^isub>M M1) \<in> sets (M2 \<Otimes>\<^isub>M M1)"
   3.733 -    (is "_ -` A \<inter> space ?Q \<in> sets ?Q")
   3.734 -proof -
   3.735 -  have *: "(\<lambda>(x, y). (y, x)) -` A \<inter> space ?Q = (\<lambda>(x, y). (y, x)) -` A"
   3.736 -    using `A \<in> sets P` sets_into_space by (auto simp: space_pair_measure)
   3.737 -  show ?thesis
   3.738 -    unfolding * using assms by (rule sets_pair_sigma_algebra_swap)
   3.739 -qed
   3.740 +  using measurable_pair_swap' assms by (rule measurable_sets)
   3.741  
   3.742 -lemma (in pair_sigma_finite) pair_measure_alt2:
   3.743 -  assumes A: "A \<in> sets P"
   3.744 -  shows "\<mu> A = (\<integral>\<^isup>+y. M1.\<mu> ((\<lambda>x. (x, y)) -` A) \<partial>M2)"
   3.745 -    (is "_ = ?\<nu> A")
   3.746 +lemma (in pair_sigma_finite) distr_pair_swap:
   3.747 +  "M1 \<Otimes>\<^isub>M M2 = distr (M2 \<Otimes>\<^isub>M M1) (M1 \<Otimes>\<^isub>M M2) (\<lambda>(x, y). (y, x))" (is "?P = ?D")
   3.748  proof -
   3.749    interpret Q: pair_sigma_finite M2 M1 by default
   3.750    from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   3.751 -  have [simp]: "\<And>m. \<lparr> space = space E, sets = sets (sigma E), measure = m \<rparr> = P\<lparr> measure := m \<rparr>"
   3.752 -    unfolding pair_measure_def by simp
   3.753 -
   3.754 -  have "\<mu> A = Q.\<mu> ((\<lambda>(y, x). (x, y)) -` A \<inter> space Q.P)"
   3.755 -  proof (rule measure_unique_Int_stable_vimage[OF Int_stable_pair_measure_generator])
   3.756 -    show "measure_space P" "measure_space Q.P" by default
   3.757 -    show "(\<lambda>(y, x). (x, y)) \<in> measurable Q.P P" by (rule Q.pair_sigma_algebra_swap_measurable)
   3.758 -    show "sets (sigma E) = sets P" "space E = space P" "A \<in> sets (sigma E)"
   3.759 -      using assms unfolding pair_measure_def by auto
   3.760 -    show "range F \<subseteq> sets E" "incseq F" "(\<Union>i. F i) = space E" "\<And>i. \<mu> (F i) \<noteq> \<infinity>"
   3.761 -      using F `A \<in> sets P` by (auto simp: pair_measure_def)
   3.762 -    fix X assume "X \<in> sets E"
   3.763 -    then obtain A B where X[simp]: "X = A \<times> B" and AB: "A \<in> sets M1" "B \<in> sets M2"
   3.764 -      unfolding pair_measure_def pair_measure_generator_def by auto
   3.765 -    then have "(\<lambda>(y, x). (x, y)) -` X \<inter> space Q.P = B \<times> A"
   3.766 -      using M1.sets_into_space M2.sets_into_space by (auto simp: space_pair_measure)
   3.767 -    then show "\<mu> X = Q.\<mu> ((\<lambda>(y, x). (x, y)) -` X \<inter> space Q.P)"
   3.768 -      using AB by (simp add: pair_measure_times Q.pair_measure_times ac_simps)
   3.769 +  let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   3.770 +  show ?thesis
   3.771 +  proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   3.772 +    show "?E \<subseteq> Pow (space ?P)"
   3.773 +      using space_closed[of M1] space_closed[of M2] by (auto simp: space_pair_measure)
   3.774 +    show "sets ?P = sigma_sets (space ?P) ?E"
   3.775 +      by (simp add: sets_pair_measure space_pair_measure)
   3.776 +    then show "sets ?D = sigma_sets (space ?P) ?E"
   3.777 +      by simp
   3.778 +  next
   3.779 +    show "range F \<subseteq> ?E" "incseq F" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   3.780 +      using F by (auto simp: space_pair_measure)
   3.781 +  next
   3.782 +    fix X assume "X \<in> ?E"
   3.783 +    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
   3.784 +    have "(\<lambda>(y, x). (x, y)) -` X \<inter> space (M2 \<Otimes>\<^isub>M M1) = B \<times> A"
   3.785 +      using sets_into_space[OF A] sets_into_space[OF B] by (auto simp: space_pair_measure)
   3.786 +    with A B show "emeasure (M1 \<Otimes>\<^isub>M M2) X = emeasure ?D X"
   3.787 +      by (simp add: emeasure_pair_measure_Times Q.emeasure_pair_measure_Times emeasure_distr
   3.788 +                    measurable_pair_swap' ac_simps)
   3.789    qed
   3.790 -  then show ?thesis
   3.791 -    using sets_into_space[OF A] Q.pair_measure_alt[OF sets_swap[OF A]]
   3.792 -    by (auto simp add: Q.pair_measure_alt space_pair_measure
   3.793 -             intro!: M2.positive_integral_cong arg_cong[where f="M1.\<mu>"])
   3.794  qed
   3.795  
   3.796 -lemma pair_sigma_algebra_sigma:
   3.797 -  assumes 1: "incseq S1" "(\<Union>i. S1 i) = space E1" "range S1 \<subseteq> sets E1" and E1: "sets E1 \<subseteq> Pow (space E1)"
   3.798 -  assumes 2: "decseq S2" "(\<Union>i. S2 i) = space E2" "range S2 \<subseteq> sets E2" and E2: "sets E2 \<subseteq> Pow (space E2)"
   3.799 -  shows "sets (sigma (pair_measure_generator (sigma E1) (sigma E2))) = sets (sigma (pair_measure_generator E1 E2))"
   3.800 -    (is "sets ?S = sets ?E")
   3.801 +lemma (in pair_sigma_finite) emeasure_pair_measure_alt2:
   3.802 +  assumes A: "A \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   3.803 +  shows "emeasure (M1 \<Otimes>\<^isub>M M2) A = (\<integral>\<^isup>+y. emeasure M1 ((\<lambda>x. (x, y)) -` A) \<partial>M2)"
   3.804 +    (is "_ = ?\<nu> A")
   3.805  proof -
   3.806 -  interpret M1: sigma_algebra "sigma E1" using E1 by (rule sigma_algebra_sigma)
   3.807 -  interpret M2: sigma_algebra "sigma E2" using E2 by (rule sigma_algebra_sigma)
   3.808 -  have P: "sets (pair_measure_generator E1 E2) \<subseteq> Pow (space E1 \<times> space E2)"
   3.809 -    using E1 E2 by (auto simp add: pair_measure_generator_def)
   3.810 -  interpret E: sigma_algebra ?E unfolding pair_measure_generator_def
   3.811 -    using E1 E2 by (intro sigma_algebra_sigma) auto
   3.812 -  { fix A assume "A \<in> sets E1"
   3.813 -    then have "fst -` A \<inter> space ?E = A \<times> (\<Union>i. S2 i)"
   3.814 -      using E1 2 unfolding pair_measure_generator_def by auto
   3.815 -    also have "\<dots> = (\<Union>i. A \<times> S2 i)" by auto
   3.816 -    also have "\<dots> \<in> sets ?E" unfolding pair_measure_generator_def sets_sigma
   3.817 -      using 2 `A \<in> sets E1`
   3.818 -      by (intro sigma_sets.Union)
   3.819 -         (force simp: image_subset_iff intro!: sigma_sets.Basic)
   3.820 -    finally have "fst -` A \<inter> space ?E \<in> sets ?E" . }
   3.821 -  moreover
   3.822 -  { fix B assume "B \<in> sets E2"
   3.823 -    then have "snd -` B \<inter> space ?E = (\<Union>i. S1 i) \<times> B"
   3.824 -      using E2 1 unfolding pair_measure_generator_def by auto
   3.825 -    also have "\<dots> = (\<Union>i. S1 i \<times> B)" by auto
   3.826 -    also have "\<dots> \<in> sets ?E"
   3.827 -      using 1 `B \<in> sets E2` unfolding pair_measure_generator_def sets_sigma
   3.828 -      by (intro sigma_sets.Union)
   3.829 -         (force simp: image_subset_iff intro!: sigma_sets.Basic)
   3.830 -    finally have "snd -` B \<inter> space ?E \<in> sets ?E" . }
   3.831 -  ultimately have proj:
   3.832 -    "fst \<in> measurable ?E (sigma E1) \<and> snd \<in> measurable ?E (sigma E2)"
   3.833 -    using E1 E2 by (subst (1 2) E.measurable_iff_sigma)
   3.834 -                   (auto simp: pair_measure_generator_def sets_sigma)
   3.835 -  { fix A B assume A: "A \<in> sets (sigma E1)" and B: "B \<in> sets (sigma E2)"
   3.836 -    with proj have "fst -` A \<inter> space ?E \<in> sets ?E" "snd -` B \<inter> space ?E \<in> sets ?E"
   3.837 -      unfolding measurable_def by simp_all
   3.838 -    moreover have "A \<times> B = (fst -` A \<inter> space ?E) \<inter> (snd -` B \<inter> space ?E)"
   3.839 -      using A B M1.sets_into_space M2.sets_into_space
   3.840 -      by (auto simp: pair_measure_generator_def)
   3.841 -    ultimately have "A \<times> B \<in> sets ?E" by auto }
   3.842 -  then have "sigma_sets (space ?E) (sets (pair_measure_generator (sigma E1) (sigma E2))) \<subseteq> sets ?E"
   3.843 -    by (intro E.sigma_sets_subset) (auto simp add: pair_measure_generator_def sets_sigma)
   3.844 -  then have subset: "sets ?S \<subseteq> sets ?E"
   3.845 -    by (simp add: sets_sigma pair_measure_generator_def)
   3.846 -  show "sets ?S = sets ?E"
   3.847 -  proof (intro set_eqI iffI)
   3.848 -    fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
   3.849 -      unfolding sets_sigma
   3.850 -    proof induct
   3.851 -      case (Basic A) then show ?case
   3.852 -        by (auto simp: pair_measure_generator_def sets_sigma intro: sigma_sets.Basic)
   3.853 -    qed (auto intro: sigma_sets.intros simp: pair_measure_generator_def)
   3.854 -  next
   3.855 -    fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
   3.856 -  qed
   3.857 +  interpret Q: pair_sigma_finite M2 M1 by default
   3.858 +  have [simp]: "\<And>y. (Pair y -` ((\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^isub>M M1))) = (\<lambda>x. (x, y)) -` A"
   3.859 +    using sets_into_space[OF A] by (auto simp: space_pair_measure)
   3.860 +  show ?thesis using A
   3.861 +    by (subst distr_pair_swap)
   3.862 +       (simp_all del: vimage_Int add: measurable_sets[OF measurable_pair_swap']
   3.863 +                 Q.emeasure_pair_measure_alt emeasure_distr[OF measurable_pair_swap' A])
   3.864  qed
   3.865  
   3.866  section "Fubinis theorem"
   3.867  
   3.868  lemma (in pair_sigma_finite) simple_function_cut:
   3.869 -  assumes f: "simple_function P f" "\<And>x. 0 \<le> f x"
   3.870 +  assumes f: "simple_function (M1 \<Otimes>\<^isub>M M2) f" "\<And>x. 0 \<le> f x"
   3.871    shows "(\<lambda>x. \<integral>\<^isup>+y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
   3.872 -    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P P f"
   3.873 +    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   3.874  proof -
   3.875 -  have f_borel: "f \<in> borel_measurable P"
   3.876 +  have f_borel: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   3.877      using f(1) by (rule borel_measurable_simple_function)
   3.878 -  let ?F = "\<lambda>z. f -` {z} \<inter> space P"
   3.879 +  let ?F = "\<lambda>z. f -` {z} \<inter> space (M1 \<Otimes>\<^isub>M M2)"
   3.880    let ?F' = "\<lambda>x z. Pair x -` ?F z"
   3.881    { fix x assume "x \<in> space M1"
   3.882      have [simp]: "\<And>z y. indicator (?F z) (x, y) = indicator (?F' x z) y"
   3.883        by (auto simp: indicator_def)
   3.884 -    have "\<And>y. y \<in> space M2 \<Longrightarrow> (x, y) \<in> space P" using `x \<in> space M1`
   3.885 +    have "\<And>y. y \<in> space M2 \<Longrightarrow> (x, y) \<in> space (M1 \<Otimes>\<^isub>M M2)" using `x \<in> space M1`
   3.886        by (simp add: space_pair_measure)
   3.887      moreover have "\<And>x z. ?F' x z \<in> sets M2" using f_borel
   3.888 -      by (intro borel_measurable_vimage measurable_cut_fst)
   3.889 +      by (rule sets_Pair1[OF measurable_sets]) auto
   3.890      ultimately have "simple_function M2 (\<lambda> y. f (x, y))"
   3.891 -      apply (rule_tac M2.simple_function_cong[THEN iffD2, OF _])
   3.892 +      apply (rule_tac simple_function_cong[THEN iffD2, OF _])
   3.893        apply (rule simple_function_indicator_representation[OF f(1)])
   3.894 -      using `x \<in> space M1` by (auto simp del: space_sigma) }
   3.895 +      using `x \<in> space M1` by auto }
   3.896    note M2_sf = this
   3.897    { fix x assume x: "x \<in> space M1"
   3.898 -    then have "(\<integral>\<^isup>+y. f (x, y) \<partial>M2) = (\<Sum>z\<in>f ` space P. z * M2.\<mu> (?F' x z))"
   3.899 -      unfolding M2.positive_integral_eq_simple_integral[OF M2_sf[OF x] f(2)]
   3.900 +    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))"
   3.901 +      unfolding positive_integral_eq_simple_integral[OF M2_sf[OF x] f(2)]
   3.902        unfolding simple_integral_def
   3.903      proof (safe intro!: setsum_mono_zero_cong_left)
   3.904 -      from f(1) show "finite (f ` space P)" by (rule simple_functionD)
   3.905 +      from f(1) show "finite (f ` space (M1 \<Otimes>\<^isub>M M2))" by (rule simple_functionD)
   3.906      next
   3.907 -      fix y assume "y \<in> space M2" then show "f (x, y) \<in> f ` space P"
   3.908 +      fix y assume "y \<in> space M2" then show "f (x, y) \<in> f ` space (M1 \<Otimes>\<^isub>M M2)"
   3.909          using `x \<in> space M1` by (auto simp: space_pair_measure)
   3.910      next
   3.911 -      fix x' y assume "(x', y) \<in> space P"
   3.912 +      fix x' y assume "(x', y) \<in> space (M1 \<Otimes>\<^isub>M M2)"
   3.913          "f (x', y) \<notin> (\<lambda>y. f (x, y)) ` space M2"
   3.914        then have *: "?F' x (f (x', y)) = {}"
   3.915          by (force simp: space_pair_measure)
   3.916 -      show  "f (x', y) * M2.\<mu> (?F' x (f (x', y))) = 0"
   3.917 +      show  "f (x', y) * emeasure M2 (?F' x (f (x', y))) = 0"
   3.918          unfolding * by simp
   3.919      qed (simp add: vimage_compose[symmetric] comp_def
   3.920                     space_pair_measure) }
   3.921    note eq = this
   3.922 -  moreover have "\<And>z. ?F z \<in> sets P"
   3.923 -    by (auto intro!: f_borel borel_measurable_vimage simp del: space_sigma)
   3.924 -  moreover then have "\<And>z. (\<lambda>x. M2.\<mu> (?F' x z)) \<in> borel_measurable M1"
   3.925 -    by (auto intro!: measure_cut_measurable_fst simp del: vimage_Int)
   3.926 -  moreover have *: "\<And>i x. 0 \<le> M2.\<mu> (Pair x -` (f -` {i} \<inter> space P))"
   3.927 -    using f(1)[THEN simple_functionD(2)] f(2) by (intro M2.positive_measure measurable_cut_fst)
   3.928 -  moreover { fix i assume "i \<in> f`space P"
   3.929 -    with * have "\<And>x. 0 \<le> i * M2.\<mu> (Pair x -` (f -` {i} \<inter> space P))"
   3.930 +  moreover have "\<And>z. ?F z \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   3.931 +    by (auto intro!: f_borel borel_measurable_vimage)
   3.932 +  moreover then have "\<And>z. (\<lambda>x. emeasure M2 (?F' x z)) \<in> borel_measurable M1"
   3.933 +    by (auto intro!: measurable_emeasure_Pair1 simp del: vimage_Int)
   3.934 +  moreover have *: "\<And>i x. 0 \<le> emeasure M2 (Pair x -` (f -` {i} \<inter> space (M1 \<Otimes>\<^isub>M M2)))"
   3.935 +    using f(1)[THEN simple_functionD(2)] f(2) by (intro emeasure_nonneg)
   3.936 +  moreover { fix i assume "i \<in> f`space (M1 \<Otimes>\<^isub>M M2)"
   3.937 +    with * have "\<And>x. 0 \<le> i * emeasure M2 (Pair x -` (f -` {i} \<inter> space (M1 \<Otimes>\<^isub>M M2)))"
   3.938        using f(2) by auto }
   3.939    ultimately
   3.940    show "(\<lambda>x. \<integral>\<^isup>+y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
   3.941 -    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P P f" using f(2)
   3.942 -    by (auto simp del: vimage_Int cong: measurable_cong
   3.943 -             intro!: M1.borel_measurable_ereal_setsum setsum_cong
   3.944 -             simp add: M1.positive_integral_setsum simple_integral_def
   3.945 -                       M1.positive_integral_cmult
   3.946 -                       M1.positive_integral_cong[OF eq]
   3.947 +    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)
   3.948 +    by (auto simp del: vimage_Int cong: measurable_cong intro!: setsum_cong
   3.949 +             simp add: positive_integral_setsum simple_integral_def
   3.950 +                       positive_integral_cmult
   3.951 +                       positive_integral_cong[OF eq]
   3.952                         positive_integral_eq_simple_integral[OF f]
   3.953 -                       pair_measure_alt[symmetric])
   3.954 +                       emeasure_pair_measure_alt[symmetric])
   3.955  qed
   3.956  
   3.957  lemma (in pair_sigma_finite) positive_integral_fst_measurable:
   3.958 -  assumes f: "f \<in> borel_measurable P"
   3.959 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   3.960    shows "(\<lambda>x. \<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
   3.961        (is "?C f \<in> borel_measurable M1")
   3.962 -    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P P f"
   3.963 +    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
   3.964  proof -
   3.965    from borel_measurable_implies_simple_function_sequence'[OF f] guess F . note F = this
   3.966 -  then have F_borel: "\<And>i. F i \<in> borel_measurable P"
   3.967 +  then have F_borel: "\<And>i. F i \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   3.968      by (auto intro: borel_measurable_simple_function)
   3.969    note sf = simple_function_cut[OF F(1,5)]
   3.970    then have "(\<lambda>x. SUP i. ?C (F i) x) \<in> borel_measurable M1"
   3.971      using F(1) by auto
   3.972    moreover
   3.973    { fix x assume "x \<in> space M1"
   3.974 -    from F measurable_pair_image_snd[OF F_borel`x \<in> space M1`]
   3.975 +    from F measurable_Pair2[OF F_borel `x \<in> space M1`]
   3.976      have "(\<integral>\<^isup>+y. (SUP i. F i (x, y)) \<partial>M2) = (SUP i. ?C (F i) x)"
   3.977 -      by (intro M2.positive_integral_monotone_convergence_SUP)
   3.978 +      by (intro positive_integral_monotone_convergence_SUP)
   3.979           (auto simp: incseq_Suc_iff le_fun_def)
   3.980      then have "(SUP i. ?C (F i) x) = ?C f x"
   3.981        unfolding F(4) positive_integral_max_0 by simp }
   3.982    note SUPR_C = this
   3.983    ultimately show "?C f \<in> borel_measurable M1"
   3.984      by (simp cong: measurable_cong)
   3.985 -  have "(\<integral>\<^isup>+x. (SUP i. F i x) \<partial>P) = (SUP i. integral\<^isup>P P (F i))"
   3.986 +  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))"
   3.987      using F_borel F
   3.988      by (intro positive_integral_monotone_convergence_SUP) auto
   3.989 -  also have "(SUP i. integral\<^isup>P P (F i)) = (SUP i. \<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. F i (x, y) \<partial>M2) \<partial>M1)"
   3.990 +  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)"
   3.991      unfolding sf(2) by simp
   3.992    also have "\<dots> = \<integral>\<^isup>+ x. (SUP i. \<integral>\<^isup>+ y. F i (x, y) \<partial>M2) \<partial>M1" using F sf(1)
   3.993 -    by (intro M1.positive_integral_monotone_convergence_SUP[symmetric])
   3.994 -       (auto intro!: M2.positive_integral_mono M2.positive_integral_positive
   3.995 -                simp: incseq_Suc_iff le_fun_def)
   3.996 +    by (intro positive_integral_monotone_convergence_SUP[symmetric])
   3.997 +       (auto intro!: positive_integral_mono positive_integral_positive
   3.998 +             simp: incseq_Suc_iff le_fun_def)
   3.999    also have "\<dots> = \<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. (SUP i. F i (x, y)) \<partial>M2) \<partial>M1"
  3.1000      using F_borel F(2,5)
  3.1001 -    by (auto intro!: M1.positive_integral_cong M2.positive_integral_monotone_convergence_SUP[symmetric]
  3.1002 -             simp: incseq_Suc_iff le_fun_def measurable_pair_image_snd)
  3.1003 -  finally show "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P P f"
  3.1004 +    by (auto intro!: positive_integral_cong positive_integral_monotone_convergence_SUP[symmetric] measurable_Pair2
  3.1005 +             simp: incseq_Suc_iff le_fun_def)
  3.1006 +  finally show "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
  3.1007      using F by (simp add: positive_integral_max_0)
  3.1008  qed
  3.1009  
  3.1010 -lemma (in pair_sigma_finite) measure_preserving_swap:
  3.1011 -  "(\<lambda>(x,y). (y, x)) \<in> measure_preserving (M1 \<Otimes>\<^isub>M M2) (M2 \<Otimes>\<^isub>M M1)"
  3.1012 -proof
  3.1013 -  interpret Q: pair_sigma_finite M2 M1 by default
  3.1014 -  show *: "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^isub>M M2) (M2 \<Otimes>\<^isub>M M1)"
  3.1015 -    using pair_sigma_algebra_swap_measurable .
  3.1016 -  fix X assume "X \<in> sets (M2 \<Otimes>\<^isub>M M1)"
  3.1017 -  from measurable_sets[OF * this] this Q.sets_into_space[OF this]
  3.1018 -  show "measure (M1 \<Otimes>\<^isub>M M2) ((\<lambda>(x, y). (y, x)) -` X \<inter> space P) = measure (M2 \<Otimes>\<^isub>M M1) X"
  3.1019 -    by (auto intro!: M1.positive_integral_cong arg_cong[where f="M2.\<mu>"]
  3.1020 -      simp: pair_measure_alt Q.pair_measure_alt2 space_pair_measure)
  3.1021 -qed
  3.1022 -
  3.1023 -lemma (in pair_sigma_finite) positive_integral_product_swap:
  3.1024 -  assumes f: "f \<in> borel_measurable P"
  3.1025 -  shows "(\<integral>\<^isup>+x. f (case x of (x,y)\<Rightarrow>(y,x)) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>P P f"
  3.1026 +lemma (in pair_sigma_finite) positive_integral_snd_measurable:
  3.1027 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
  3.1028 +  shows "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
  3.1029  proof -
  3.1030    interpret Q: pair_sigma_finite M2 M1 by default
  3.1031 -  have "sigma_algebra P" by default
  3.1032 -  with f show ?thesis
  3.1033 -    by (subst Q.positive_integral_vimage[OF _ Q.measure_preserving_swap]) auto
  3.1034 -qed
  3.1035 -
  3.1036 -lemma (in pair_sigma_finite) positive_integral_snd_measurable:
  3.1037 -  assumes f: "f \<in> borel_measurable P"
  3.1038 -  shows "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>P P f"
  3.1039 -proof -
  3.1040 -  interpret Q: pair_sigma_finite M2 M1 by default
  3.1041 -  note pair_sigma_algebra_measurable[OF f]
  3.1042 +  note measurable_pair_swap[OF f]
  3.1043    from Q.positive_integral_fst_measurable[OF this]
  3.1044 -  have "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>\<^isup>+ (x, y). f (y, x) \<partial>Q.P)"
  3.1045 +  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))"
  3.1046      by simp
  3.1047 -  also have "(\<integral>\<^isup>+ (x, y). f (y, x) \<partial>Q.P) = integral\<^isup>P P f"
  3.1048 -    unfolding positive_integral_product_swap[OF f, symmetric]
  3.1049 -    by (auto intro!: Q.positive_integral_cong)
  3.1050 +  also have "(\<integral>\<^isup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
  3.1051 +    by (subst distr_pair_swap)
  3.1052 +       (auto simp: positive_integral_distr[OF measurable_pair_swap' f] intro!: positive_integral_cong)
  3.1053    finally show ?thesis .
  3.1054  qed
  3.1055  
  3.1056  lemma (in pair_sigma_finite) Fubini:
  3.1057 -  assumes f: "f \<in> borel_measurable P"
  3.1058 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
  3.1059    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)"
  3.1060    unfolding positive_integral_snd_measurable[OF assms]
  3.1061    unfolding positive_integral_fst_measurable[OF assms] ..
  3.1062  
  3.1063  lemma (in pair_sigma_finite) AE_pair:
  3.1064 -  assumes "AE x in P. Q x"
  3.1065 +  assumes "AE x in (M1 \<Otimes>\<^isub>M M2). Q x"
  3.1066    shows "AE x in M1. (AE y in M2. Q (x, y))"
  3.1067  proof -
  3.1068 -  obtain N where N: "N \<in> sets P" "\<mu> N = 0" "{x\<in>space P. \<not> Q x} \<subseteq> N"
  3.1069 -    using assms unfolding almost_everywhere_def by auto
  3.1070 +  obtain N where N: "N \<in> sets (M1 \<Otimes>\<^isub>M M2)" "emeasure (M1 \<Otimes>\<^isub>M M2) N = 0" "{x\<in>space (M1 \<Otimes>\<^isub>M M2). \<not> Q x} \<subseteq> N"
  3.1071 +    using assms unfolding eventually_ae_filter by auto
  3.1072    show ?thesis
  3.1073 -  proof (rule M1.AE_I)
  3.1074 -    from N measure_cut_measurable_fst[OF `N \<in> sets P`]
  3.1075 -    show "M1.\<mu> {x\<in>space M1. M2.\<mu> (Pair x -` N) \<noteq> 0} = 0"
  3.1076 -      by (auto simp: pair_measure_alt M1.positive_integral_0_iff)
  3.1077 -    show "{x \<in> space M1. M2.\<mu> (Pair x -` N) \<noteq> 0} \<in> sets M1"
  3.1078 -      by (intro M1.borel_measurable_ereal_neq_const measure_cut_measurable_fst N)
  3.1079 -    { fix x assume "x \<in> space M1" "M2.\<mu> (Pair x -` N) = 0"
  3.1080 -      have "M2.almost_everywhere (\<lambda>y. Q (x, y))"
  3.1081 -      proof (rule M2.AE_I)
  3.1082 -        show "M2.\<mu> (Pair x -` N) = 0" by fact
  3.1083 -        show "Pair x -` N \<in> sets M2" by (intro measurable_cut_fst N)
  3.1084 +  proof (rule AE_I)
  3.1085 +    from N measurable_emeasure_Pair1[OF `N \<in> sets (M1 \<Otimes>\<^isub>M M2)`]
  3.1086 +    show "emeasure M1 {x\<in>space M1. emeasure M2 (Pair x -` N) \<noteq> 0} = 0"
  3.1087 +      by (auto simp: emeasure_pair_measure_alt positive_integral_0_iff emeasure_nonneg)
  3.1088 +    show "{x \<in> space M1. emeasure M2 (Pair x -` N) \<noteq> 0} \<in> sets M1"
  3.1089 +      by (intro borel_measurable_ereal_neq_const measurable_emeasure_Pair1 N)
  3.1090 +    { fix x assume "x \<in> space M1" "emeasure M2 (Pair x -` N) = 0"
  3.1091 +      have "AE y in M2. Q (x, y)"
  3.1092 +      proof (rule AE_I)
  3.1093 +        show "emeasure M2 (Pair x -` N) = 0" by fact
  3.1094 +        show "Pair x -` N \<in> sets M2" using N(1) by (rule sets_Pair1)
  3.1095          show "{y \<in> space M2. \<not> Q (x, y)} \<subseteq> Pair x -` N"
  3.1096 -          using N `x \<in> space M1` unfolding space_sigma space_pair_measure by auto
  3.1097 +          using N `x \<in> space M1` unfolding space_pair_measure by auto
  3.1098        qed }
  3.1099 -    then show "{x \<in> space M1. \<not> M2.almost_everywhere (\<lambda>y. Q (x, y))} \<subseteq> {x \<in> space M1. M2.\<mu> (Pair x -` N) \<noteq> 0}"
  3.1100 +    then show "{x \<in> space M1. \<not> (AE y in M2. Q (x, y))} \<subseteq> {x \<in> space M1. emeasure M2 (Pair x -` N) \<noteq> 0}"
  3.1101        by auto
  3.1102    qed
  3.1103  qed
  3.1104  
  3.1105 -lemma (in pair_sigma_algebra) measurable_product_swap:
  3.1106 -  "f \<in> measurable (M2 \<Otimes>\<^isub>M M1) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable P M"
  3.1107 +lemma (in pair_sigma_finite) AE_pair_measure:
  3.1108 +  assumes "{x\<in>space (M1 \<Otimes>\<^isub>M M2). P x} \<in> sets (M1 \<Otimes>\<^isub>M M2)"
  3.1109 +  assumes ae: "AE x in M1. AE y in M2. P (x, y)"
  3.1110 +  shows "AE x in M1 \<Otimes>\<^isub>M M2. P x"
  3.1111 +proof (subst AE_iff_measurable[OF _ refl])
  3.1112 +  show "{x\<in>space (M1 \<Otimes>\<^isub>M M2). \<not> P x} \<in> sets (M1 \<Otimes>\<^isub>M M2)"
  3.1113 +    by (rule sets_Collect) fact
  3.1114 +  then have "emeasure (M1 \<Otimes>\<^isub>M M2) {x \<in> space (M1 \<Otimes>\<^isub>M M2). \<not> P x} =
  3.1115 +      (\<integral>\<^isup>+ x. \<integral>\<^isup>+ y. indicator {x \<in> space (M1 \<Otimes>\<^isub>M M2). \<not> P x} (x, y) \<partial>M2 \<partial>M1)"
  3.1116 +    by (simp add: emeasure_pair_measure)
  3.1117 +  also have "\<dots> = (\<integral>\<^isup>+ x. \<integral>\<^isup>+ y. 0 \<partial>M2 \<partial>M1)"
  3.1118 +    using ae
  3.1119 +    apply (safe intro!: positive_integral_cong_AE)
  3.1120 +    apply (intro AE_I2)
  3.1121 +    apply (safe intro!: positive_integral_cong_AE)
  3.1122 +    apply auto
  3.1123 +    done
  3.1124 +  finally show "emeasure (M1 \<Otimes>\<^isub>M M2) {x \<in> space (M1 \<Otimes>\<^isub>M M2). \<not> P x} = 0" by simp
  3.1125 +qed
  3.1126 +
  3.1127 +lemma (in pair_sigma_finite) AE_pair_iff:
  3.1128 +  "{x\<in>space (M1 \<Otimes>\<^isub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^isub>M M2) \<Longrightarrow>
  3.1129 +    (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))"
  3.1130 +  using AE_pair[of "\<lambda>x. P (fst x) (snd x)"] AE_pair_measure[of "\<lambda>x. P (fst x) (snd x)"] by auto
  3.1131 +
  3.1132 +lemma AE_distr_iff:
  3.1133 +  assumes f: "f \<in> measurable M N" and P: "{x \<in> space N. P x} \<in> sets N"
  3.1134 +  shows "(AE x in distr M N f. P x) \<longleftrightarrow> (AE x in M. P (f x))"
  3.1135 +proof (subst (1 2) AE_iff_measurable[OF _ refl])
  3.1136 +  from P show "{x \<in> space (distr M N f). \<not> P x} \<in> sets (distr M N f)"
  3.1137 +    by (auto intro!: sets_Collect_neg)
  3.1138 +  moreover
  3.1139 +  have "f -` {x \<in> space N. P x} \<inter> space M = {x \<in> space M. P (f x)}"
  3.1140 +    using f by (auto dest: measurable_space)
  3.1141 +  then show "{x \<in> space M. \<not> P (f x)} \<in> sets M"
  3.1142 +    using measurable_sets[OF f P] by (auto intro!: sets_Collect_neg)
  3.1143 +  moreover have "f -` {x\<in>space N. \<not> P x} \<inter> space M = {x \<in> space M. \<not> P (f x)}"
  3.1144 +    using f by (auto dest: measurable_space)
  3.1145 +  ultimately show "(emeasure (distr M N f) {x \<in> space (distr M N f). \<not> P x} = 0) =
  3.1146 +    (emeasure M {x \<in> space M. \<not> P (f x)} = 0)"
  3.1147 +    using f by (simp add: emeasure_distr)
  3.1148 +qed
  3.1149 +
  3.1150 +lemma (in pair_sigma_finite) AE_commute:
  3.1151 +  assumes P: "{x\<in>space (M1 \<Otimes>\<^isub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^isub>M M2)"
  3.1152 +  shows "(AE x in M1. AE y in M2. P x y) \<longleftrightarrow> (AE y in M2. AE x in M1. P x y)"
  3.1153  proof -
  3.1154 -  interpret Q: pair_sigma_algebra M2 M1 by default
  3.1155 -  show ?thesis
  3.1156 -    using pair_sigma_algebra_measurable[of "\<lambda>(x,y). f (y, x)"]
  3.1157 -    by (auto intro!: pair_sigma_algebra_measurable Q.pair_sigma_algebra_measurable iffI)
  3.1158 +  interpret Q: pair_sigma_finite M2 M1 ..
  3.1159 +  have [simp]: "\<And>x. (fst (case x of (x, y) \<Rightarrow> (y, x))) = snd x" "\<And>x. (snd (case x of (x, y) \<Rightarrow> (y, x))) = fst x"
  3.1160 +    by auto
  3.1161 +  have "{x \<in> space (M2 \<Otimes>\<^isub>M M1). P (snd x) (fst x)} =
  3.1162 +    (\<lambda>(x, y). (y, x)) -` {x \<in> space (M1 \<Otimes>\<^isub>M M2). P (fst x) (snd x)} \<inter> space (M2 \<Otimes>\<^isub>M M1)"
  3.1163 +    by (auto simp: space_pair_measure)
  3.1164 +  also have "\<dots> \<in> sets (M2 \<Otimes>\<^isub>M M1)"
  3.1165 +    by (intro sets_pair_swap P)
  3.1166 +  finally show ?thesis
  3.1167 +    apply (subst AE_pair_iff[OF P])
  3.1168 +    apply (subst distr_pair_swap)
  3.1169 +    apply (subst AE_distr_iff[OF measurable_pair_swap' P])
  3.1170 +    apply (subst Q.AE_pair_iff)
  3.1171 +    apply simp_all
  3.1172 +    done
  3.1173  qed
  3.1174  
  3.1175  lemma (in pair_sigma_finite) integrable_product_swap:
  3.1176 -  assumes "integrable P f"
  3.1177 +  assumes "integrable (M1 \<Otimes>\<^isub>M M2) f"
  3.1178    shows "integrable (M2 \<Otimes>\<^isub>M M1) (\<lambda>(x,y). f (y,x))"
  3.1179  proof -
  3.1180    interpret Q: pair_sigma_finite M2 M1 by default
  3.1181    have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
  3.1182    show ?thesis unfolding *
  3.1183 -    using assms unfolding integrable_def
  3.1184 -    apply (subst (1 2) positive_integral_product_swap)
  3.1185 -    using `integrable P f` unfolding integrable_def
  3.1186 -    by (auto simp: *[symmetric] Q.measurable_product_swap[symmetric])
  3.1187 +    by (rule integrable_distr[OF measurable_pair_swap'])
  3.1188 +       (simp add: distr_pair_swap[symmetric] assms)
  3.1189  qed
  3.1190  
  3.1191  lemma (in pair_sigma_finite) integrable_product_swap_iff:
  3.1192 -  "integrable (M2 \<Otimes>\<^isub>M M1) (\<lambda>(x,y). f (y,x)) \<longleftrightarrow> integrable P f"
  3.1193 +  "integrable (M2 \<Otimes>\<^isub>M M1) (\<lambda>(x,y). f (y,x)) \<longleftrightarrow> integrable (M1 \<Otimes>\<^isub>M M2) f"
  3.1194  proof -
  3.1195    interpret Q: pair_sigma_finite M2 M1 by default
  3.1196    from Q.integrable_product_swap[of "\<lambda>(x,y). f (y,x)"] integrable_product_swap[of f]
  3.1197 @@ -802,27 +668,25 @@
  3.1198  qed
  3.1199  
  3.1200  lemma (in pair_sigma_finite) integral_product_swap:
  3.1201 -  assumes "integrable P f"
  3.1202 -  shows "(\<integral>(x,y). f (y,x) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>L P f"
  3.1203 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
  3.1204 +  shows "(\<integral>(x,y). f (y,x) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>L (M1 \<Otimes>\<^isub>M M2) f"
  3.1205  proof -
  3.1206 -  interpret Q: pair_sigma_finite M2 M1 by default
  3.1207    have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
  3.1208 -  show ?thesis
  3.1209 -    unfolding lebesgue_integral_def *
  3.1210 -    apply (subst (1 2) positive_integral_product_swap)
  3.1211 -    using `integrable P f` unfolding integrable_def
  3.1212 -    by (auto simp: *[symmetric] Q.measurable_product_swap[symmetric])
  3.1213 +  show ?thesis unfolding *
  3.1214 +    by (simp add: integral_distr[symmetric, OF measurable_pair_swap' f] distr_pair_swap[symmetric])
  3.1215  qed
  3.1216  
  3.1217  lemma (in pair_sigma_finite) integrable_fst_measurable:
  3.1218 -  assumes f: "integrable P f"
  3.1219 -  shows "M1.almost_everywhere (\<lambda>x. integrable M2 (\<lambda> y. f (x, y)))" (is "?AE")
  3.1220 -    and "(\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>L P f" (is "?INT")
  3.1221 +  assumes f: "integrable (M1 \<Otimes>\<^isub>M M2) f"
  3.1222 +  shows "AE x in M1. integrable M2 (\<lambda> y. f (x, y))" (is "?AE")
  3.1223 +    and "(\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>L (M1 \<Otimes>\<^isub>M M2) f" (is "?INT")
  3.1224  proof -
  3.1225 +  have f_borel: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
  3.1226 +    using f by auto
  3.1227    let ?pf = "\<lambda>x. ereal (f x)" and ?nf = "\<lambda>x. ereal (- f x)"
  3.1228    have
  3.1229 -    borel: "?nf \<in> borel_measurable P""?pf \<in> borel_measurable P" and
  3.1230 -    int: "integral\<^isup>P P ?nf \<noteq> \<infinity>" "integral\<^isup>P P ?pf \<noteq> \<infinity>"
  3.1231 +    borel: "?nf \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)""?pf \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)" and
  3.1232 +    int: "integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) ?nf \<noteq> \<infinity>" "integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) ?pf \<noteq> \<infinity>"
  3.1233      using assms by auto
  3.1234    have "(\<integral>\<^isup>+x. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
  3.1235       "(\<integral>\<^isup>+x. (\<integral>\<^isup>+y. ereal (- f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
  3.1236 @@ -831,69 +695,92 @@
  3.1237    with borel[THEN positive_integral_fst_measurable(1)]
  3.1238    have AE_pos: "AE x in M1. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
  3.1239      "AE x in M1. (\<integral>\<^isup>+y. ereal (- f (x, y)) \<partial>M2) \<noteq> \<infinity>"
  3.1240 -    by (auto intro!: M1.positive_integral_PInf_AE )
  3.1241 +    by (auto intro!: positive_integral_PInf_AE )
  3.1242    then have AE: "AE x in M1. \<bar>\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
  3.1243      "AE x in M1. \<bar>\<integral>\<^isup>+y. ereal (- f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
  3.1244 -    by (auto simp: M2.positive_integral_positive)
  3.1245 +    by (auto simp: positive_integral_positive)
  3.1246    from AE_pos show ?AE using assms
  3.1247 -    by (simp add: measurable_pair_image_snd integrable_def)
  3.1248 +    by (simp add: measurable_Pair2[OF f_borel] integrable_def)
  3.1249    { fix f have "(\<integral>\<^isup>+ x. - \<integral>\<^isup>+ y. ereal (f x y) \<partial>M2 \<partial>M1) = (\<integral>\<^isup>+x. 0 \<partial>M1)"
  3.1250 -      using M2.positive_integral_positive
  3.1251 -      by (intro M1.positive_integral_cong_pos) (auto simp: ereal_uminus_le_reorder)
  3.1252 +      using positive_integral_positive
  3.1253 +      by (intro positive_integral_cong_pos) (auto simp: ereal_uminus_le_reorder)
  3.1254      then have "(\<integral>\<^isup>+ x. - \<integral>\<^isup>+ y. ereal (f x y) \<partial>M2 \<partial>M1) = 0" by simp }
  3.1255    note this[simp]
  3.1256 -  { fix f assume borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable P"
  3.1257 -      and int: "integral\<^isup>P P (\<lambda>x. ereal (f x)) \<noteq> \<infinity>"
  3.1258 -      and AE: "M1.almost_everywhere (\<lambda>x. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>)"
  3.1259 +  { fix f assume borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
  3.1260 +      and int: "integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) (\<lambda>x. ereal (f x)) \<noteq> \<infinity>"
  3.1261 +      and AE: "AE x in M1. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
  3.1262      have "integrable M1 (\<lambda>x. real (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2))" (is "integrable M1 ?f")
  3.1263      proof (intro integrable_def[THEN iffD2] conjI)
  3.1264        show "?f \<in> borel_measurable M1"
  3.1265 -        using borel by (auto intro!: M1.borel_measurable_real_of_ereal positive_integral_fst_measurable)
  3.1266 +        using borel by (auto intro!: positive_integral_fst_measurable)
  3.1267        have "(\<integral>\<^isup>+x. ereal (?f x) \<partial>M1) = (\<integral>\<^isup>+x. (\<integral>\<^isup>+y. ereal (f (x, y))  \<partial>M2) \<partial>M1)"
  3.1268 -        using AE M2.positive_integral_positive
  3.1269 -        by (auto intro!: M1.positive_integral_cong_AE simp: ereal_real)
  3.1270 +        using AE positive_integral_positive[of M2]
  3.1271 +        by (auto intro!: positive_integral_cong_AE simp: ereal_real)
  3.1272        then show "(\<integral>\<^isup>+x. ereal (?f x) \<partial>M1) \<noteq> \<infinity>"
  3.1273          using positive_integral_fst_measurable[OF borel] int by simp
  3.1274        have "(\<integral>\<^isup>+x. ereal (- ?f x) \<partial>M1) = (\<integral>\<^isup>+x. 0 \<partial>M1)"
  3.1275 -        by (intro M1.positive_integral_cong_pos)
  3.1276 -           (simp add: M2.positive_integral_positive real_of_ereal_pos)
  3.1277 +        by (intro positive_integral_cong_pos)
  3.1278 +           (simp add: positive_integral_positive real_of_ereal_pos)
  3.1279        then show "(\<integral>\<^isup>+x. ereal (- ?f x) \<partial>M1) \<noteq> \<infinity>" by simp
  3.1280      qed }
  3.1281    with this[OF borel(1) int(1) AE_pos(2)] this[OF borel(2) int(2) AE_pos(1)]
  3.1282    show ?INT
  3.1283 -    unfolding lebesgue_integral_def[of P] lebesgue_integral_def[of M2]
  3.1284 +    unfolding lebesgue_integral_def[of "M1 \<Otimes>\<^isub>M M2"] lebesgue_integral_def[of M2]
  3.1285        borel[THEN positive_integral_fst_measurable(2), symmetric]
  3.1286 -    using AE[THEN M1.integral_real]
  3.1287 +    using AE[THEN integral_real]
  3.1288      by simp
  3.1289  qed
  3.1290  
  3.1291  lemma (in pair_sigma_finite) integrable_snd_measurable:
  3.1292 -  assumes f: "integrable P f"
  3.1293 -  shows "M2.almost_everywhere (\<lambda>y. integrable M1 (\<lambda>x. f (x, y)))" (is "?AE")
  3.1294 -    and "(\<integral>y. (\<integral>x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>L P f" (is "?INT")
  3.1295 +  assumes f: "integrable (M1 \<Otimes>\<^isub>M M2) f"
  3.1296 +  shows "AE y in M2. integrable M1 (\<lambda>x. f (x, y))" (is "?AE")
  3.1297 +    and "(\<integral>y. (\<integral>x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>L (M1 \<Otimes>\<^isub>M M2) f" (is "?INT")
  3.1298  proof -
  3.1299    interpret Q: pair_sigma_finite M2 M1 by default
  3.1300 -  have Q_int: "integrable Q.P (\<lambda>(x, y). f (y, x))"
  3.1301 +  have Q_int: "integrable (M2 \<Otimes>\<^isub>M M1) (\<lambda>(x, y). f (y, x))"
  3.1302      using f unfolding integrable_product_swap_iff .
  3.1303    show ?INT
  3.1304      using Q.integrable_fst_measurable(2)[OF Q_int]
  3.1305 -    using integral_product_swap[OF f] by simp
  3.1306 +    using integral_product_swap[of f] f by auto
  3.1307    show ?AE
  3.1308      using Q.integrable_fst_measurable(1)[OF Q_int]
  3.1309      by simp
  3.1310  qed
  3.1311  
  3.1312 +lemma (in pair_sigma_finite) positive_integral_fst_measurable':
  3.1313 +  assumes f: "(\<lambda>x. f (fst x) (snd x)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
  3.1314 +  shows "(\<lambda>x. \<integral>\<^isup>+ y. f x y \<partial>M2) \<in> borel_measurable M1"
  3.1315 +  using positive_integral_fst_measurable(1)[OF f] by simp
  3.1316 +
  3.1317 +lemma (in pair_sigma_finite) integral_fst_measurable:
  3.1318 +  "(\<lambda>x. f (fst x) (snd x)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2) \<Longrightarrow> (\<lambda>x. \<integral> y. f x y \<partial>M2) \<in> borel_measurable M1"
  3.1319 +  by (auto simp: lebesgue_integral_def intro!: borel_measurable_diff positive_integral_fst_measurable')
  3.1320 +
  3.1321 +lemma (in pair_sigma_finite) positive_integral_snd_measurable':
  3.1322 +  assumes f: "(\<lambda>x. f (fst x) (snd x)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
  3.1323 +  shows "(\<lambda>y. \<integral>\<^isup>+ x. f x y \<partial>M1) \<in> borel_measurable M2"
  3.1324 +proof -
  3.1325 +  interpret Q: pair_sigma_finite M2 M1 ..
  3.1326 +  show ?thesis
  3.1327 +    using measurable_pair_swap[OF f]
  3.1328 +    by (intro Q.positive_integral_fst_measurable') (simp add: split_beta')
  3.1329 +qed
  3.1330 +
  3.1331 +lemma (in pair_sigma_finite) integral_snd_measurable:
  3.1332 +  "(\<lambda>x. f (fst x) (snd x)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2) \<Longrightarrow> (\<lambda>y. \<integral> x. f x y \<partial>M1) \<in> borel_measurable M2"
  3.1333 +  by (auto simp: lebesgue_integral_def intro!: borel_measurable_diff positive_integral_snd_measurable')
  3.1334 +
  3.1335  lemma (in pair_sigma_finite) Fubini_integral:
  3.1336 -  assumes f: "integrable P f"
  3.1337 +  assumes f: "integrable (M1 \<Otimes>\<^isub>M M2) f"
  3.1338    shows "(\<integral>y. (\<integral>x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1)"
  3.1339    unfolding integrable_snd_measurable[OF assms]
  3.1340    unfolding integrable_fst_measurable[OF assms] ..
  3.1341  
  3.1342 -section "Products on finite spaces"
  3.1343 +section {* Products on counting spaces, densities and distributions *}
  3.1344  
  3.1345  lemma sigma_sets_pair_measure_generator_finite:
  3.1346    assumes "finite A" and "finite B"
  3.1347 -  shows "sigma_sets (A \<times> B) { a \<times> b | a b. a \<in> Pow A \<and> b \<in> Pow B} = Pow (A \<times> B)"
  3.1348 +  shows "sigma_sets (A \<times> B) { a \<times> b | a b. a \<subseteq> A \<and> b \<subseteq> B} = Pow (A \<times> B)"
  3.1349    (is "sigma_sets ?prod ?sets = _")
  3.1350  proof safe
  3.1351    have fin: "finite (A \<times> B)" using assms by (rule finite_cartesian_product)
  3.1352 @@ -904,8 +791,7 @@
  3.1353      case empty show ?case by (rule sigma_sets.Empty)
  3.1354    next
  3.1355      case (insert a x)
  3.1356 -    hence "{a} \<in> sigma_sets ?prod ?sets"
  3.1357 -      by (auto simp: pair_measure_generator_def intro!: sigma_sets.Basic)
  3.1358 +    hence "{a} \<in> sigma_sets ?prod ?sets" by auto
  3.1359      moreover have "x \<in> sigma_sets ?prod ?sets" using insert by auto
  3.1360      ultimately show ?case unfolding insert_is_Un[of a x] by (rule sigma_sets_Un)
  3.1361    qed
  3.1362 @@ -916,48 +802,142 @@
  3.1363    show "a \<in> A" and "b \<in> B" by auto
  3.1364  qed
  3.1365  
  3.1366 -locale pair_finite_sigma_algebra = pair_sigma_algebra M1 M2 + M1: finite_sigma_algebra M1 + M2: finite_sigma_algebra M2 for M1 M2
  3.1367 -
  3.1368 -lemma (in pair_finite_sigma_algebra) finite_pair_sigma_algebra:
  3.1369 -  shows "P = \<lparr> space = space M1 \<times> space M2, sets = Pow (space M1 \<times> space M2), \<dots> = algebra.more P \<rparr>"
  3.1370 -proof -
  3.1371 -  show ?thesis
  3.1372 -    using sigma_sets_pair_measure_generator_finite[OF M1.finite_space M2.finite_space]
  3.1373 -    by (intro algebra.equality) (simp_all add: pair_measure_def pair_measure_generator_def sigma_def)
  3.1374 -qed
  3.1375 -
  3.1376 -sublocale pair_finite_sigma_algebra \<subseteq> finite_sigma_algebra P
  3.1377 -proof
  3.1378 -  show "finite (space P)"
  3.1379 -    using M1.finite_space M2.finite_space
  3.1380 -    by (subst finite_pair_sigma_algebra) simp
  3.1381 -  show "sets P = Pow (space P)"
  3.1382 -    by (subst (1 2) finite_pair_sigma_algebra) simp
  3.1383 +lemma pair_measure_count_space:
  3.1384 +  assumes A: "finite A" and B: "finite B"
  3.1385 +  shows "count_space A \<Otimes>\<^isub>M count_space B = count_space (A \<times> B)" (is "?P = ?C")
  3.1386 +proof (rule measure_eqI)
  3.1387 +  interpret A: finite_measure "count_space A" by (rule finite_measure_count_space) fact
  3.1388 +  interpret B: finite_measure "count_space B" by (rule finite_measure_count_space) fact
  3.1389 +  interpret P: pair_sigma_finite "count_space A" "count_space B" by default
  3.1390 +  show eq: "sets ?P = sets ?C"
  3.1391 +    by (simp add: sets_pair_measure sigma_sets_pair_measure_generator_finite A B)
  3.1392 +  fix X assume X: "X \<in> sets ?P"
  3.1393 +  with eq have X_subset: "X \<subseteq> A \<times> B" by simp
  3.1394 +  with A B have fin_Pair: "\<And>x. finite (Pair x -` X)"
  3.1395 +    by (intro finite_subset[OF _ B]) auto
  3.1396 +  have fin_X: "finite X" using X_subset by (rule finite_subset) (auto simp: A B)
  3.1397 +  show "emeasure ?P X = emeasure ?C X"
  3.1398 +    apply (subst P.emeasure_pair_measure_alt[OF X])
  3.1399 +    apply (subst emeasure_count_space)
  3.1400 +    using X_subset apply auto []
  3.1401 +    apply (simp add: fin_Pair emeasure_count_space X_subset fin_X)
  3.1402 +    apply (subst positive_integral_count_space)
  3.1403 +    using A apply simp
  3.1404 +    apply (simp del: real_of_nat_setsum add: real_of_nat_setsum[symmetric])
  3.1405 +    apply (subst card_gt_0_iff)
  3.1406 +    apply (simp add: fin_Pair)
  3.1407 +    apply (subst card_SigmaI[symmetric])
  3.1408 +    using A apply simp
  3.1409 +    using fin_Pair apply simp
  3.1410 +    using X_subset apply (auto intro!: arg_cong[where f=card])
  3.1411 +    done
  3.1412  qed
  3.1413  
  3.1414 -locale pair_finite_space = pair_sigma_finite M1 M2 + pair_finite_sigma_algebra M1 M2 +
  3.1415 -  M1: finite_measure_space M1 + M2: finite_measure_space M2 for M1 M2
  3.1416 +lemma pair_measure_density:
  3.1417 +  assumes f: "f \<in> borel_measurable M1" "AE x in M1. 0 \<le> f x"
  3.1418 +  assumes g: "g \<in> borel_measurable M2" "AE x in M2. 0 \<le> g x"
  3.1419 +  assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
  3.1420 +  assumes "sigma_finite_measure (density M1 f)" "sigma_finite_measure (density M2 g)"
  3.1421 +  shows "density M1 f \<Otimes>\<^isub>M density M2 g = density (M1 \<Otimes>\<^isub>M M2) (\<lambda>(x,y). f x * g y)" (is "?L = ?R")
  3.1422 +proof (rule measure_eqI)
  3.1423 +  interpret M1: sigma_finite_measure M1 by fact
  3.1424 +  interpret M2: sigma_finite_measure M2 by fact
  3.1425 +  interpret D1: sigma_finite_measure "density M1 f" by fact
  3.1426 +  interpret D2: sigma_finite_measure "density M2 g" by fact
  3.1427 +  interpret L: pair_sigma_finite "density M1 f" "density M2 g" ..
  3.1428 +  interpret R: pair_sigma_finite M1 M2 ..
  3.1429 +
  3.1430 +  fix A assume A: "A \<in> sets ?L"
  3.1431 +  then have indicator_eq: "\<And>x y. indicator A (x, y) = indicator (Pair x -` A) y"
  3.1432 +   and Pair_A: "\<And>x. Pair x -` A \<in> sets M2"
  3.1433 +    by (auto simp: indicator_def sets_Pair1)
  3.1434 +  have f_fst: "(\<lambda>p. f (fst p)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
  3.1435 +    using measurable_comp[OF measurable_fst f(1)] by (simp add: comp_def)
  3.1436 +  have g_snd: "(\<lambda>p. g (snd p)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
  3.1437 +    using measurable_comp[OF measurable_snd g(1)] by (simp add: comp_def)
  3.1438 +  have "(\<lambda>x. \<integral>\<^isup>+ y. g (snd (x, y)) * indicator A (x, y) \<partial>M2) \<in> borel_measurable M1"
  3.1439 +    using g_snd Pair_A A by (intro R.positive_integral_fst_measurable) auto
  3.1440 +  then have int_g: "(\<lambda>x. \<integral>\<^isup>+ y. g y * indicator A (x, y) \<partial>M2) \<in> borel_measurable M1"
  3.1441 +    by simp
  3.1442  
  3.1443 -lemma (in pair_finite_space) pair_measure_Pair[simp]:
  3.1444 -  assumes "a \<in> space M1" "b \<in> space M2"
  3.1445 -  shows "\<mu> {(a, b)} = M1.\<mu> {a} * M2.\<mu> {b}"
  3.1446 +  show "emeasure ?L A = emeasure ?R A"
  3.1447 +    apply (subst L.emeasure_pair_measure[OF A])
  3.1448 +    apply (subst emeasure_density)
  3.1449 +        using f_fst g_snd apply (simp add: split_beta')
  3.1450 +      using A apply simp
  3.1451 +    apply (subst positive_integral_density[OF g])
  3.1452 +      apply (simp add: indicator_eq Pair_A)
  3.1453 +    apply (subst positive_integral_density[OF f])
  3.1454 +      apply (rule int_g)
  3.1455 +    apply (subst R.positive_integral_fst_measurable(2)[symmetric])
  3.1456 +      using f g A Pair_A f_fst g_snd
  3.1457 +      apply (auto intro!: positive_integral_cong_AE R.measurable_emeasure_Pair1
  3.1458 +                  simp: positive_integral_cmult indicator_eq split_beta')
  3.1459 +    apply (intro AE_I2 impI)
  3.1460 +    apply (subst mult_assoc)
  3.1461 +    apply (subst positive_integral_cmult)
  3.1462 +          apply auto
  3.1463 +    done
  3.1464 +qed simp
  3.1465 +
  3.1466 +lemma sigma_finite_measure_distr:
  3.1467 +  assumes "sigma_finite_measure (distr M N f)" and f: "f \<in> measurable M N"
  3.1468 +  shows "sigma_finite_measure M"
  3.1469  proof -
  3.1470 -  have "\<mu> ({a}\<times>{b}) = M1.\<mu> {a} * M2.\<mu> {b}"
  3.1471 -    using M1.sets_eq_Pow M2.sets_eq_Pow assms
  3.1472 -    by (subst pair_measure_times) auto
  3.1473 -  then show ?thesis by simp
  3.1474 +  interpret sigma_finite_measure "distr M N f" by fact
  3.1475 +  from sigma_finite_disjoint guess A . note A = this
  3.1476 +  show ?thesis
  3.1477 +  proof (unfold_locales, intro conjI exI allI)
  3.1478 +    show "range (\<lambda>i. f -` A i \<inter> space M) \<subseteq> sets M"
  3.1479 +      using A f by (auto intro!: measurable_sets)
  3.1480 +    show "(\<Union>i. f -` A i \<inter> space M) = space M"
  3.1481 +      using A(1) A(2)[symmetric] f by (auto simp: measurable_def Pi_def)
  3.1482 +    fix i show "emeasure M (f -` A i \<inter> space M) \<noteq> \<infinity>"
  3.1483 +      using f A(1,2) A(3)[of i] by (simp add: emeasure_distr subset_eq)
  3.1484 +  qed
  3.1485  qed
  3.1486  
  3.1487 -lemma (in pair_finite_space) pair_measure_singleton[simp]:
  3.1488 -  assumes "x \<in> space M1 \<times> space M2"
  3.1489 -  shows "\<mu> {x} = M1.\<mu> {fst x} * M2.\<mu> {snd x}"
  3.1490 -  using pair_measure_Pair assms by (cases x) auto
  3.1491 +lemma measurable_cong':
  3.1492 +  assumes sets: "sets M = sets M'" "sets N = sets N'"
  3.1493 +  shows "measurable M N = measurable M' N'"
  3.1494 +  using sets[THEN sets_eq_imp_space_eq] sets by (simp add: measurable_def)
  3.1495  
  3.1496 -sublocale pair_finite_space \<subseteq> finite_measure_space P
  3.1497 -proof unfold_locales
  3.1498 -  show "measure P (space P) \<noteq> \<infinity>"
  3.1499 -    by (subst (2) finite_pair_sigma_algebra)
  3.1500 -       (simp add: pair_measure_times)
  3.1501 +lemma pair_measure_distr:
  3.1502 +  assumes f: "f \<in> measurable M S" and g: "g \<in> measurable N T"
  3.1503 +  assumes "sigma_finite_measure (distr M S f)" "sigma_finite_measure (distr N T g)"
  3.1504 +  shows "distr M S f \<Otimes>\<^isub>M distr N T g = distr (M \<Otimes>\<^isub>M N) (S \<Otimes>\<^isub>M T) (\<lambda>(x, y). (f x, g y))" (is "?P = ?D")
  3.1505 +proof (rule measure_eqI)
  3.1506 +  show "sets ?P = sets ?D"
  3.1507 +    by simp
  3.1508 +  interpret S: sigma_finite_measure "distr M S f" by fact
  3.1509 +  interpret T: sigma_finite_measure "distr N T g" by fact
  3.1510 +  interpret ST: pair_sigma_finite "distr M S f"  "distr N T g" ..
  3.1511 +  interpret M: sigma_finite_measure M by (rule sigma_finite_measure_distr) fact+
  3.1512 +  interpret N: sigma_finite_measure N by (rule sigma_finite_measure_distr) fact+
  3.1513 +  interpret MN: pair_sigma_finite M N ..
  3.1514 +  interpret SN: pair_sigma_finite "distr M S f" N ..
  3.1515 +  have [simp]: 
  3.1516 +    "\<And>f g. fst \<circ> (\<lambda>(x, y). (f x, g y)) = f \<circ> fst" "\<And>f g. snd \<circ> (\<lambda>(x, y). (f x, g y)) = g \<circ> snd"
  3.1517 +    by auto
  3.1518 +  then have fg: "(\<lambda>(x, y). (f x, g y)) \<in> measurable (M \<Otimes>\<^isub>M N) (S \<Otimes>\<^isub>M T)"
  3.1519 +    using measurable_comp[OF measurable_fst f] measurable_comp[OF measurable_snd g]
  3.1520 +    by (auto simp: measurable_pair_iff)
  3.1521 +  fix A assume A: "A \<in> sets ?P"
  3.1522 +  then have "emeasure ?P A = (\<integral>\<^isup>+x. emeasure (distr N T g) (Pair x -` A) \<partial>distr M S f)"
  3.1523 +    by (rule ST.emeasure_pair_measure_alt)
  3.1524 +  also have "\<dots> = (\<integral>\<^isup>+x. emeasure N (g -` (Pair x -` A) \<inter> space N) \<partial>distr M S f)"
  3.1525 +    using g A by (simp add: sets_Pair1 emeasure_distr)
  3.1526 +  also have "\<dots> = (\<integral>\<^isup>+x. emeasure N (g -` (Pair (f x) -` A) \<inter> space N) \<partial>M)"
  3.1527 +    using f g A ST.measurable_emeasure_Pair1[OF A]
  3.1528 +    by (intro positive_integral_distr) (auto simp add: sets_Pair1 emeasure_distr)
  3.1529 +  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)"
  3.1530 +    by (intro positive_integral_cong arg_cong2[where f=emeasure]) (auto simp: space_pair_measure)
  3.1531 +  also have "\<dots> = emeasure (M \<Otimes>\<^isub>M N) ((\<lambda>(x, y). (f x, g y)) -` A \<inter> space (M \<Otimes>\<^isub>M N))"
  3.1532 +    using fg by (intro MN.emeasure_pair_measure_alt[symmetric] measurable_sets[OF _ A])
  3.1533 +                (auto cong: measurable_cong')
  3.1534 +  also have "\<dots> = emeasure ?D A"
  3.1535 +    using fg A by (subst emeasure_distr) auto
  3.1536 +  finally show "emeasure ?P A = emeasure ?D A" .
  3.1537  qed
  3.1538  
  3.1539  end
  3.1540 \ No newline at end of file
     4.1 --- a/src/HOL/Probability/Borel_Space.thy	Mon Apr 23 12:23:23 2012 +0100
     4.2 +++ b/src/HOL/Probability/Borel_Space.thy	Mon Apr 23 12:14:35 2012 +0200
     4.3 @@ -11,16 +11,14 @@
     4.4  
     4.5  section "Generic Borel spaces"
     4.6  
     4.7 -definition "borel = sigma \<lparr> space = UNIV::'a::topological_space set, sets = {S. open S}\<rparr>"
     4.8 -abbreviation "borel_measurable M \<equiv> measurable M borel"
     4.9 +definition borel :: "'a::topological_space measure" where
    4.10 +  "borel = sigma UNIV {S. open S}"
    4.11  
    4.12 -interpretation borel: sigma_algebra borel
    4.13 -  by (auto simp: borel_def intro!: sigma_algebra_sigma)
    4.14 +abbreviation "borel_measurable M \<equiv> measurable M borel"
    4.15  
    4.16  lemma in_borel_measurable:
    4.17     "f \<in> borel_measurable M \<longleftrightarrow>
    4.18 -    (\<forall>S \<in> sets (sigma \<lparr> space = UNIV, sets = {S. open S}\<rparr>).
    4.19 -      f -` S \<inter> space M \<in> sets M)"
    4.20 +    (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
    4.21    by (auto simp add: measurable_def borel_def)
    4.22  
    4.23  lemma in_borel_measurable_borel:
    4.24 @@ -36,7 +34,7 @@
    4.25    assumes "open A" shows "A \<in> sets borel"
    4.26  proof -
    4.27    have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
    4.28 -  thus ?thesis unfolding borel_def sigma_def by (auto intro!: sigma_sets.Basic)
    4.29 +  thus ?thesis unfolding borel_def by auto
    4.30  qed
    4.31  
    4.32  lemma borel_closed[simp]:
    4.33 @@ -48,9 +46,9 @@
    4.34  qed
    4.35  
    4.36  lemma borel_comp[intro,simp]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
    4.37 -  unfolding Compl_eq_Diff_UNIV by (intro borel.Diff) auto
    4.38 +  unfolding Compl_eq_Diff_UNIV by (intro Diff) auto
    4.39  
    4.40 -lemma (in sigma_algebra) borel_measurable_vimage:
    4.41 +lemma borel_measurable_vimage:
    4.42    fixes f :: "'a \<Rightarrow> 'x::t2_space"
    4.43    assumes borel: "f \<in> borel_measurable M"
    4.44    shows "f -` {x} \<inter> space M \<in> sets M"
    4.45 @@ -65,12 +63,12 @@
    4.46    thus ?thesis by auto
    4.47  qed
    4.48  
    4.49 -lemma (in sigma_algebra) borel_measurableI:
    4.50 +lemma borel_measurableI:
    4.51    fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
    4.52    assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
    4.53    shows "f \<in> borel_measurable M"
    4.54    unfolding borel_def
    4.55 -proof (rule measurable_sigma, simp_all)
    4.56 +proof (rule measurable_measure_of, simp_all)
    4.57    fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
    4.58      using assms[of S] by simp
    4.59  qed
    4.60 @@ -78,22 +76,22 @@
    4.61  lemma borel_singleton[simp, intro]:
    4.62    fixes x :: "'a::t1_space"
    4.63    shows "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets borel"
    4.64 -  proof (rule borel.insert_in_sets)
    4.65 +  proof (rule insert_in_sets)
    4.66      show "{x} \<in> sets borel"
    4.67        using closed_singleton[of x] by (rule borel_closed)
    4.68    qed simp
    4.69  
    4.70 -lemma (in sigma_algebra) borel_measurable_const[simp, intro]:
    4.71 +lemma borel_measurable_const[simp, intro]:
    4.72    "(\<lambda>x. c) \<in> borel_measurable M"
    4.73 -  by (auto intro!: measurable_const)
    4.74 +  by auto
    4.75  
    4.76 -lemma (in sigma_algebra) borel_measurable_indicator[simp, intro!]:
    4.77 +lemma borel_measurable_indicator[simp, intro!]:
    4.78    assumes A: "A \<in> sets M"
    4.79    shows "indicator A \<in> borel_measurable M"
    4.80    unfolding indicator_def [abs_def] using A
    4.81 -  by (auto intro!: measurable_If_set borel_measurable_const)
    4.82 +  by (auto intro!: measurable_If_set)
    4.83  
    4.84 -lemma (in sigma_algebra) borel_measurable_indicator_iff:
    4.85 +lemma borel_measurable_indicator_iff:
    4.86    "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
    4.87      (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
    4.88  proof
    4.89 @@ -111,49 +109,7 @@
    4.90    ultimately show "?I \<in> borel_measurable M" by auto
    4.91  qed
    4.92  
    4.93 -lemma (in sigma_algebra) borel_measurable_restricted:
    4.94 -  fixes f :: "'a \<Rightarrow> ereal" assumes "A \<in> sets M"
    4.95 -  shows "f \<in> borel_measurable (restricted_space A) \<longleftrightarrow>
    4.96 -    (\<lambda>x. f x * indicator A x) \<in> borel_measurable M"
    4.97 -    (is "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable M")
    4.98 -proof -
    4.99 -  interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
   4.100 -  have *: "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable ?R"
   4.101 -    by (auto intro!: measurable_cong)
   4.102 -  show ?thesis unfolding *
   4.103 -    unfolding in_borel_measurable_borel
   4.104 -  proof (simp, safe)
   4.105 -    fix S :: "ereal set" assume "S \<in> sets borel"
   4.106 -      "\<forall>S\<in>sets borel. ?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   4.107 -    then have "?f -` S \<inter> A \<in> op \<inter> A ` sets M" by auto
   4.108 -    then have f: "?f -` S \<inter> A \<in> sets M"
   4.109 -      using `A \<in> sets M` sets_into_space by fastforce
   4.110 -    show "?f -` S \<inter> space M \<in> sets M"
   4.111 -    proof cases
   4.112 -      assume "0 \<in> S"
   4.113 -      then have "?f -` S \<inter> space M = ?f -` S \<inter> A \<union> (space M - A)"
   4.114 -        using `A \<in> sets M` sets_into_space by auto
   4.115 -      then show ?thesis using f `A \<in> sets M` by (auto intro!: Un Diff)
   4.116 -    next
   4.117 -      assume "0 \<notin> S"
   4.118 -      then have "?f -` S \<inter> space M = ?f -` S \<inter> A"
   4.119 -        using `A \<in> sets M` sets_into_space
   4.120 -        by (auto simp: indicator_def split: split_if_asm)
   4.121 -      then show ?thesis using f by auto
   4.122 -    qed
   4.123 -  next
   4.124 -    fix S :: "ereal set" assume "S \<in> sets borel"
   4.125 -      "\<forall>S\<in>sets borel. ?f -` S \<inter> space M \<in> sets M"
   4.126 -    then have f: "?f -` S \<inter> space M \<in> sets M" by auto
   4.127 -    then show "?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   4.128 -      using `A \<in> sets M` sets_into_space
   4.129 -      apply (simp add: image_iff)
   4.130 -      apply (rule bexI[OF _ f])
   4.131 -      by auto
   4.132 -  qed
   4.133 -qed
   4.134 -
   4.135 -lemma (in sigma_algebra) borel_measurable_subalgebra:
   4.136 +lemma borel_measurable_subalgebra:
   4.137    assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   4.138    shows "f \<in> borel_measurable M"
   4.139    using assms unfolding measurable_def by auto
   4.140 @@ -220,7 +176,7 @@
   4.141    shows "{x::'a::euclidean_space. x $$ i \<le> a} \<in> sets borel"
   4.142    by (auto intro!: borel_closed closed_halfspace_component_le)
   4.143  
   4.144 -lemma (in sigma_algebra) borel_measurable_less[simp, intro]:
   4.145 +lemma borel_measurable_less[simp, intro]:
   4.146    fixes f :: "'a \<Rightarrow> real"
   4.147    assumes f: "f \<in> borel_measurable M"
   4.148    assumes g: "g \<in> borel_measurable M"
   4.149 @@ -233,7 +189,7 @@
   4.150      by simp (blast intro: measurable_sets)
   4.151  qed
   4.152  
   4.153 -lemma (in sigma_algebra) borel_measurable_le[simp, intro]:
   4.154 +lemma borel_measurable_le[simp, intro]:
   4.155    fixes f :: "'a \<Rightarrow> real"
   4.156    assumes f: "f \<in> borel_measurable M"
   4.157    assumes g: "g \<in> borel_measurable M"
   4.158 @@ -245,7 +201,7 @@
   4.159      by simp blast
   4.160  qed
   4.161  
   4.162 -lemma (in sigma_algebra) borel_measurable_eq[simp, intro]:
   4.163 +lemma borel_measurable_eq[simp, intro]:
   4.164    fixes f :: "'a \<Rightarrow> real"
   4.165    assumes f: "f \<in> borel_measurable M"
   4.166    assumes g: "g \<in> borel_measurable M"
   4.167 @@ -257,7 +213,7 @@
   4.168    thus ?thesis using f g by auto
   4.169  qed
   4.170  
   4.171 -lemma (in sigma_algebra) borel_measurable_neq[simp, intro]:
   4.172 +lemma borel_measurable_neq[simp, intro]:
   4.173    fixes f :: "'a \<Rightarrow> real"
   4.174    assumes f: "f \<in> borel_measurable M"
   4.175    assumes g: "g \<in> borel_measurable M"
   4.176 @@ -351,23 +307,70 @@
   4.177    thus "x \<in> UNION ?idx ?box" using ab e p q exI[of _ p] exI[of _ q] by auto
   4.178  qed auto
   4.179  
   4.180 -lemma halfspace_span_open:
   4.181 -  "sigma_sets UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))
   4.182 -    \<subseteq> sets borel"
   4.183 -  by (auto intro!: borel.sigma_sets_subset[simplified] borel_open
   4.184 -                   open_halfspace_component_lt)
   4.185 +lemma borel_sigma_sets_subset:
   4.186 +  "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
   4.187 +  using sigma_sets_subset[of A borel] by simp
   4.188 +
   4.189 +lemma borel_eq_sigmaI1:
   4.190 +  fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   4.191 +  assumes borel_eq: "borel = sigma UNIV X"
   4.192 +  assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (range F))"
   4.193 +  assumes F: "\<And>i. F i \<in> sets borel"
   4.194 +  shows "borel = sigma UNIV (range F)"
   4.195 +  unfolding borel_def
   4.196 +proof (intro sigma_eqI antisym)
   4.197 +  have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
   4.198 +    unfolding borel_def by simp
   4.199 +  also have "\<dots> = sigma_sets UNIV X"
   4.200 +    unfolding borel_eq by simp
   4.201 +  also have "\<dots> \<subseteq> sigma_sets UNIV (range F)"
   4.202 +    using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
   4.203 +  finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (range F)" .
   4.204 +  show "sigma_sets UNIV (range F) \<subseteq> sigma_sets UNIV {S. open S}"
   4.205 +    unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
   4.206 +qed auto
   4.207  
   4.208 -lemma halfspace_lt_in_halfspace:
   4.209 -  "{x\<Colon>'a. x $$ i < a} \<in> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>)"
   4.210 -  by (auto intro!: sigma_sets.Basic simp: sets_sigma)
   4.211 +lemma borel_eq_sigmaI2:
   4.212 +  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
   4.213 +    and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   4.214 +  assumes borel_eq: "borel = sigma UNIV (range (\<lambda>(i, j). G i j))"
   4.215 +  assumes X: "\<And>i j. G i j \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   4.216 +  assumes F: "\<And>i j. F i j \<in> sets borel"
   4.217 +  shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   4.218 +  using assms by (intro borel_eq_sigmaI1[where X="range (\<lambda>(i, j). G i j)" and F="(\<lambda>(i, j). F i j)"]) auto
   4.219 +
   4.220 +lemma borel_eq_sigmaI3:
   4.221 +  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   4.222 +  assumes borel_eq: "borel = sigma UNIV X"
   4.223 +  assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   4.224 +  assumes F: "\<And>i j. F i j \<in> sets borel"
   4.225 +  shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   4.226 +  using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
   4.227 +
   4.228 +lemma borel_eq_sigmaI4:
   4.229 +  fixes F :: "'i \<Rightarrow> 'a::topological_space set"
   4.230 +    and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   4.231 +  assumes borel_eq: "borel = sigma UNIV (range (\<lambda>(i, j). G i j))"
   4.232 +  assumes X: "\<And>i j. G i j \<in> sets (sigma UNIV (range F))"
   4.233 +  assumes F: "\<And>i. F i \<in> sets borel"
   4.234 +  shows "borel = sigma UNIV (range F)"
   4.235 +  using assms by (intro borel_eq_sigmaI1[where X="range (\<lambda>(i, j). G i j)" and F=F]) auto
   4.236 +
   4.237 +lemma borel_eq_sigmaI5:
   4.238 +  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
   4.239 +  assumes borel_eq: "borel = sigma UNIV (range G)"
   4.240 +  assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   4.241 +  assumes F: "\<And>i j. F i j \<in> sets borel"
   4.242 +  shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   4.243 +  using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
   4.244  
   4.245  lemma halfspace_gt_in_halfspace:
   4.246 -  "{x\<Colon>'a. a < x $$ i} \<in> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>)"
   4.247 -  (is "?set \<in> sets ?SIGMA")
   4.248 +  "{x\<Colon>'a. a < x $$ i} \<in> sigma_sets UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))"
   4.249 +  (is "?set \<in> ?SIGMA")
   4.250  proof -
   4.251 -  interpret sigma_algebra "?SIGMA"
   4.252 -    by (intro sigma_algebra_sigma_sets) (simp_all add: sets_sigma)
   4.253 -  have *: "?set = (\<Union>n. space ?SIGMA - {x\<Colon>'a. x $$ i < a + 1 / real (Suc n)})"
   4.254 +  interpret sigma_algebra UNIV ?SIGMA
   4.255 +    by (intro sigma_algebra_sigma_sets) simp_all
   4.256 +  have *: "?set = (\<Union>n. UNIV - {x\<Colon>'a. x $$ i < a + 1 / real (Suc n)})"
   4.257    proof (safe, simp_all add: not_less)
   4.258      fix x assume "a < x $$ i"
   4.259      with reals_Archimedean[of "x $$ i - a"]
   4.260 @@ -381,100 +384,78 @@
   4.261      also assume "\<dots> \<le> x"
   4.262      finally show "a < x" .
   4.263    qed
   4.264 -  show "?set \<in> sets ?SIGMA" unfolding *
   4.265 -    by (safe intro!: countable_UN Diff halfspace_lt_in_halfspace)
   4.266 -qed
   4.267 -
   4.268 -lemma open_span_halfspace:
   4.269 -  "sets borel \<subseteq> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x $$ i < a})\<rparr>)"
   4.270 -    (is "_ \<subseteq> sets ?SIGMA")
   4.271 -proof -
   4.272 -  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) simp
   4.273 -  then interpret sigma_algebra ?SIGMA .
   4.274 -  { fix S :: "'a set" assume "S \<in> {S. open S}"
   4.275 -    then have "open S" unfolding mem_Collect_eq .
   4.276 -    from open_UNION[OF this]
   4.277 -    obtain I where *: "S =
   4.278 -      (\<Union>(a, b)\<in>I.
   4.279 -          (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
   4.280 -          (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
   4.281 -      unfolding greaterThanLessThan_def
   4.282 -      unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
   4.283 -      unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
   4.284 -      by blast
   4.285 -    have "S \<in> sets ?SIGMA"
   4.286 -      unfolding *
   4.287 -      by (auto intro!: countable_UN Int countable_INT halfspace_lt_in_halfspace halfspace_gt_in_halfspace) }
   4.288 -  then show ?thesis unfolding borel_def
   4.289 -    by (intro sets_sigma_subset) auto
   4.290 +  show "?set \<in> ?SIGMA" unfolding *
   4.291 +    by (auto intro!: Diff)
   4.292  qed
   4.293  
   4.294 -lemma halfspace_span_halfspace_le:
   4.295 -  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   4.296 -   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. x $$ i \<le> a})\<rparr>)"
   4.297 -  (is "_ \<subseteq> sets ?SIGMA")
   4.298 -proof -
   4.299 -  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   4.300 -  then interpret sigma_algebra ?SIGMA .
   4.301 -  { fix a i
   4.302 -    have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
   4.303 -    proof (safe, simp_all)
   4.304 -      fix x::'a assume *: "x$$i < a"
   4.305 -      with reals_Archimedean[of "a - x$$i"]
   4.306 -      obtain n where "x $$ i < a - 1 / (real (Suc n))"
   4.307 -        by (auto simp: field_simps inverse_eq_divide)
   4.308 -      then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
   4.309 -        by (blast intro: less_imp_le)
   4.310 -    next
   4.311 -      fix x::'a and n
   4.312 -      assume "x$$i \<le> a - 1 / real (Suc n)"
   4.313 -      also have "\<dots> < a" by auto
   4.314 -      finally show "x$$i < a" .
   4.315 -    qed
   4.316 -    have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   4.317 -      by (safe intro!: countable_UN)
   4.318 -         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   4.319 -  then show ?thesis by (intro sets_sigma_subset) auto
   4.320 -qed
   4.321 +lemma borel_eq_halfspace_less:
   4.322 +  "borel = sigma UNIV (range (\<lambda>(a, i). {x::'a::ordered_euclidean_space. x $$ i < a}))"
   4.323 +  (is "_ = ?SIGMA")
   4.324 +proof (rule borel_eq_sigmaI3[OF borel_def])
   4.325 +  fix S :: "'a set" assume "S \<in> {S. open S}"
   4.326 +  then have "open S" by simp
   4.327 +  from open_UNION[OF this]
   4.328 +  obtain I where *: "S =
   4.329 +    (\<Union>(a, b)\<in>I.
   4.330 +        (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
   4.331 +        (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
   4.332 +    unfolding greaterThanLessThan_def
   4.333 +    unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
   4.334 +    unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
   4.335 +    by blast
   4.336 +  show "S \<in> ?SIGMA"
   4.337 +    unfolding *
   4.338 +    by (safe intro!: countable_UN Int countable_INT) (auto intro!: halfspace_gt_in_halfspace)
   4.339 +qed auto
   4.340  
   4.341 -lemma halfspace_span_halfspace_ge:
   4.342 -  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   4.343 -   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a \<le> x $$ i})\<rparr>)"
   4.344 -  (is "_ \<subseteq> sets ?SIGMA")
   4.345 -proof -
   4.346 -  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   4.347 -  then interpret sigma_algebra ?SIGMA .
   4.348 -  { fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
   4.349 -    have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   4.350 -      by (safe intro!: Diff)
   4.351 -         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   4.352 -  then show ?thesis by (intro sets_sigma_subset) auto
   4.353 -qed
   4.354 +lemma borel_eq_halfspace_le:
   4.355 +  "borel = sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x $$ i \<le> a}))"
   4.356 +  (is "_ = ?SIGMA")
   4.357 +proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   4.358 +  fix a i
   4.359 +  have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
   4.360 +  proof (safe, simp_all)
   4.361 +    fix x::'a assume *: "x$$i < a"
   4.362 +    with reals_Archimedean[of "a - x$$i"]
   4.363 +    obtain n where "x $$ i < a - 1 / (real (Suc n))"
   4.364 +      by (auto simp: field_simps inverse_eq_divide)
   4.365 +    then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
   4.366 +      by (blast intro: less_imp_le)
   4.367 +  next
   4.368 +    fix x::'a and n
   4.369 +    assume "x$$i \<le> a - 1 / real (Suc n)"
   4.370 +    also have "\<dots> < a" by auto
   4.371 +    finally show "x$$i < a" .
   4.372 +  qed
   4.373 +  show "{x. x$$i < a} \<in> ?SIGMA" unfolding *
   4.374 +    by (safe intro!: countable_UN) auto
   4.375 +qed auto
   4.376  
   4.377 -lemma halfspace_le_span_halfspace_gt:
   4.378 -  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   4.379 -   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a < x $$ i})\<rparr>)"
   4.380 -  (is "_ \<subseteq> sets ?SIGMA")
   4.381 -proof -
   4.382 -  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   4.383 -  then interpret sigma_algebra ?SIGMA .
   4.384 -  { fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   4.385 -    have "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   4.386 -      by (safe intro!: Diff)
   4.387 -         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   4.388 -  then show ?thesis by (intro sets_sigma_subset) auto
   4.389 -qed
   4.390 +lemma borel_eq_halfspace_ge:
   4.391 +  "borel = sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a \<le> x $$ i}))"
   4.392 +  (is "_ = ?SIGMA")
   4.393 +proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   4.394 +  fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
   4.395 +  show "{x. x$$i < a} \<in> ?SIGMA" unfolding *
   4.396 +      by (safe intro!: compl_sets) auto
   4.397 +qed auto
   4.398  
   4.399 -lemma halfspace_le_span_atMost:
   4.400 -  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   4.401 -   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>)"
   4.402 -  (is "_ \<subseteq> sets ?SIGMA")
   4.403 -proof -
   4.404 -  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   4.405 -  then interpret sigma_algebra ?SIGMA .
   4.406 -  have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   4.407 +lemma borel_eq_halfspace_greater:
   4.408 +  "borel = sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a < x $$ i}))"
   4.409 +  (is "_ = ?SIGMA")
   4.410 +proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
   4.411 +  fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   4.412 +  show "{x. x$$i \<le> a} \<in> ?SIGMA" unfolding *
   4.413 +    by (safe intro!: compl_sets) auto
   4.414 +qed auto
   4.415 +
   4.416 +lemma borel_eq_atMost:
   4.417 +  "borel = sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space}))"
   4.418 +  (is "_ = ?SIGMA")
   4.419 +proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   4.420 +  fix a i show "{x. x$$i \<le> a} \<in> ?SIGMA"
   4.421    proof cases
   4.422 -    fix a i assume "i < DIM('a)"
   4.423 +    assume "i < DIM('a)"
   4.424      then have *: "{x::'a. x$$i \<le> a} = (\<Union>k::nat. {.. (\<chi>\<chi> n. if n = i then a else real k)})"
   4.425      proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   4.426        fix x
   4.427 @@ -484,28 +465,19 @@
   4.428        then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia \<le> real k"
   4.429          by (auto intro!: exI[of _ k])
   4.430      qed
   4.431 -    show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   4.432 -      by (safe intro!: countable_UN)
   4.433 -         (auto simp: sets_sigma intro!: sigma_sets.Basic)
   4.434 -  next
   4.435 -    fix a i assume "\<not> i < DIM('a)"
   4.436 -    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   4.437 -      using top by auto
   4.438 -  qed
   4.439 -  then show ?thesis by (intro sets_sigma_subset) auto
   4.440 -qed
   4.441 +    show "{x. x$$i \<le> a} \<in> ?SIGMA" unfolding *
   4.442 +      by (safe intro!: countable_UN) auto
   4.443 +  qed (auto intro: sigma_sets_top sigma_sets.Empty)
   4.444 +qed auto
   4.445  
   4.446 -lemma halfspace_le_span_greaterThan:
   4.447 -  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   4.448 -   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {a<..})\<rparr>)"
   4.449 -  (is "_ \<subseteq> sets ?SIGMA")
   4.450 -proof -
   4.451 -  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   4.452 -  then interpret sigma_algebra ?SIGMA .
   4.453 -  have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   4.454 +lemma borel_eq_greaterThan:
   4.455 +  "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {a<..}))"
   4.456 +  (is "_ = ?SIGMA")
   4.457 +proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   4.458 +  fix a i show "{x. x$$i \<le> a} \<in> ?SIGMA"
   4.459    proof cases
   4.460 -    fix a i assume "i < DIM('a)"
   4.461 -    have "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   4.462 +    assume "i < DIM('a)"
   4.463 +    have "{x::'a. x$$i \<le> a} = UNIV - {x::'a. a < x$$i}" by auto
   4.464      also have *: "{x::'a. a < x$$i} = (\<Union>k::nat. {(\<chi>\<chi> n. if n = i then a else -real k) <..})" using `i <DIM('a)`
   4.465      proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   4.466        fix x
   4.467 @@ -518,30 +490,22 @@
   4.468        then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> -real k < x $$ ia"
   4.469          by (auto intro!: exI[of _ k])
   4.470      qed
   4.471 -    finally show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   4.472 +    finally show "{x. x$$i \<le> a} \<in> ?SIGMA"
   4.473        apply (simp only:)
   4.474        apply (safe intro!: countable_UN Diff)
   4.475 -      apply (auto simp: sets_sigma intro!: sigma_sets.Basic)
   4.476 +      apply (auto intro: sigma_sets_top)
   4.477        done
   4.478 -  next
   4.479 -    fix a i assume "\<not> i < DIM('a)"
   4.480 -    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   4.481 -      using top by auto
   4.482 -  qed
   4.483 -  then show ?thesis by (intro sets_sigma_subset) auto
   4.484 -qed
   4.485 +  qed (auto intro: sigma_sets_top sigma_sets.Empty)
   4.486 +qed auto
   4.487  
   4.488 -lemma halfspace_le_span_lessThan:
   4.489 -  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a \<le> x $$ i})\<rparr>) \<subseteq>
   4.490 -   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..<a})\<rparr>)"
   4.491 -  (is "_ \<subseteq> sets ?SIGMA")
   4.492 -proof -
   4.493 -  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   4.494 -  then interpret sigma_algebra ?SIGMA .
   4.495 -  have "\<And>a i. {x. a \<le> x$$i} \<in> sets ?SIGMA"
   4.496 +lemma borel_eq_lessThan:
   4.497 +  "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {..<a}))"
   4.498 +  (is "_ = ?SIGMA")
   4.499 +proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
   4.500 +  fix a i show "{x. a \<le> x$$i} \<in> ?SIGMA"
   4.501    proof cases
   4.502      fix a i assume "i < DIM('a)"
   4.503 -    have "{x::'a. a \<le> x$$i} = space ?SIGMA - {x::'a. x$$i < a}" by auto
   4.504 +    have "{x::'a. a \<le> x$$i} = UNIV - {x::'a. x$$i < a}" by auto
   4.505      also have *: "{x::'a. x$$i < a} = (\<Union>k::nat. {..< (\<chi>\<chi> n. if n = i then a else real k)})" using `i <DIM('a)`
   4.506      proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   4.507        fix x
   4.508 @@ -554,184 +518,70 @@
   4.509        then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia < real k"
   4.510          by (auto intro!: exI[of _ k])
   4.511      qed
   4.512 -    finally show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   4.513 +    finally show "{x. a \<le> x$$i} \<in> ?SIGMA"
   4.514        apply (simp only:)
   4.515        apply (safe intro!: countable_UN Diff)
   4.516 -      apply (auto simp: sets_sigma intro!: sigma_sets.Basic)
   4.517 +      apply (auto intro: sigma_sets_top)
   4.518        done
   4.519 -  next
   4.520 -    fix a i assume "\<not> i < DIM('a)"
   4.521 -    then show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   4.522 -      using top by auto
   4.523 -  qed
   4.524 -  then show ?thesis by (intro sets_sigma_subset) auto
   4.525 -qed
   4.526 -
   4.527 -lemma atMost_span_atLeastAtMost:
   4.528 -  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>) \<subseteq>
   4.529 -   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>(a,b). {a..b})\<rparr>)"
   4.530 -  (is "_ \<subseteq> sets ?SIGMA")
   4.531 -proof -
   4.532 -  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   4.533 -  then interpret sigma_algebra ?SIGMA .
   4.534 -  { fix a::'a
   4.535 -    have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   4.536 -    proof (safe, simp_all add: eucl_le[where 'a='a])
   4.537 -      fix x
   4.538 -      from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
   4.539 -      guess k::nat .. note k = this
   4.540 -      { fix i assume "i < DIM('a)"
   4.541 -        with k have "- x$$i \<le> real k"
   4.542 -          by (subst (asm) Max_le_iff) (auto simp: field_simps)
   4.543 -        then have "- real k \<le> x$$i" by simp }
   4.544 -      then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
   4.545 -        by (auto intro!: exI[of _ k])
   4.546 -    qed
   4.547 -    have "{..a} \<in> sets ?SIGMA" unfolding *
   4.548 -      by (safe intro!: countable_UN)
   4.549 -         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   4.550 -  then show ?thesis by (intro sets_sigma_subset) auto
   4.551 -qed
   4.552 -
   4.553 -lemma borel_eq_atMost:
   4.554 -  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> a. {.. a::'a\<Colon>ordered_euclidean_space})\<rparr>)"
   4.555 -    (is "_ = ?SIGMA")
   4.556 -proof (intro algebra.equality antisym)
   4.557 -  show "sets borel \<subseteq> sets ?SIGMA"
   4.558 -    using halfspace_le_span_atMost halfspace_span_halfspace_le open_span_halfspace
   4.559 -    by auto
   4.560 -  show "sets ?SIGMA \<subseteq> sets borel"
   4.561 -    by (rule borel.sets_sigma_subset) auto
   4.562 +  qed (auto intro: sigma_sets_top sigma_sets.Empty)
   4.563  qed auto
   4.564  
   4.565  lemma borel_eq_atLeastAtMost:
   4.566 -  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space, b). {a .. b})\<rparr>)"
   4.567 -   (is "_ = ?SIGMA")
   4.568 -proof (intro algebra.equality antisym)
   4.569 -  show "sets borel \<subseteq> sets ?SIGMA"
   4.570 -    using atMost_span_atLeastAtMost halfspace_le_span_atMost
   4.571 -      halfspace_span_halfspace_le open_span_halfspace
   4.572 -    by auto
   4.573 -  show "sets ?SIGMA \<subseteq> sets borel"
   4.574 -    by (rule borel.sets_sigma_subset) auto
   4.575 -qed auto
   4.576 -
   4.577 -lemma borel_eq_greaterThan:
   4.578 -  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {a <..})\<rparr>)"
   4.579 -   (is "_ = ?SIGMA")
   4.580 -proof (intro algebra.equality antisym)
   4.581 -  show "sets borel \<subseteq> sets ?SIGMA"
   4.582 -    using halfspace_le_span_greaterThan
   4.583 -      halfspace_span_halfspace_le open_span_halfspace
   4.584 -    by auto
   4.585 -  show "sets ?SIGMA \<subseteq> sets borel"
   4.586 -    by (rule borel.sets_sigma_subset) auto
   4.587 -qed auto
   4.588 -
   4.589 -lemma borel_eq_lessThan:
   4.590 -  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {..< a})\<rparr>)"
   4.591 -   (is "_ = ?SIGMA")
   4.592 -proof (intro algebra.equality antisym)
   4.593 -  show "sets borel \<subseteq> sets ?SIGMA"
   4.594 -    using halfspace_le_span_lessThan
   4.595 -      halfspace_span_halfspace_ge open_span_halfspace
   4.596 -    by auto
   4.597 -  show "sets ?SIGMA \<subseteq> sets borel"
   4.598 -    by (rule borel.sets_sigma_subset) auto
   4.599 +  "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} \<Colon>'a\<Colon>ordered_euclidean_space set))"
   4.600 +  (is "_ = ?SIGMA")
   4.601 +proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
   4.602 +  fix a::'a
   4.603 +  have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   4.604 +  proof (safe, simp_all add: eucl_le[where 'a='a])
   4.605 +    fix x
   4.606 +    from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
   4.607 +    guess k::nat .. note k = this
   4.608 +    { fix i assume "i < DIM('a)"
   4.609 +      with k have "- x$$i \<le> real k"
   4.610 +        by (subst (asm) Max_le_iff) (auto simp: field_simps)
   4.611 +      then have "- real k \<le> x$$i" by simp }
   4.612 +    then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
   4.613 +      by (auto intro!: exI[of _ k])
   4.614 +  qed
   4.615 +  show "{..a} \<in> ?SIGMA" unfolding *
   4.616 +    by (safe intro!: countable_UN)
   4.617 +       (auto intro!: sigma_sets_top)
   4.618  qed auto
   4.619  
   4.620  lemma borel_eq_greaterThanLessThan:
   4.621 -  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, b). {a <..< (b :: 'a \<Colon> ordered_euclidean_space)})\<rparr>)"
   4.622 +  "borel = sigma UNIV (range (\<lambda> (a, b). {a <..< b} :: 'a \<Colon> ordered_euclidean_space set))"
   4.623      (is "_ = ?SIGMA")
   4.624 -proof (intro algebra.equality antisym)
   4.625 -  show "sets ?SIGMA \<subseteq> sets borel"
   4.626 -    by (rule borel.sets_sigma_subset) auto
   4.627 -  show "sets borel \<subseteq> sets ?SIGMA"
   4.628 -  proof -
   4.629 -    have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   4.630 -    then interpret sigma_algebra ?SIGMA .
   4.631 -    { fix M :: "'a set" assume "M \<in> {S. open S}"
   4.632 -      then have "open M" by simp
   4.633 -      have "M \<in> sets ?SIGMA"
   4.634 -        apply (subst open_UNION[OF `open M`])
   4.635 -        apply (safe intro!: countable_UN)
   4.636 -        apply (auto simp add: sigma_def intro!: sigma_sets.Basic)
   4.637 -        done }
   4.638 -    then show ?thesis
   4.639 -      unfolding borel_def by (intro sets_sigma_subset) auto
   4.640 -  qed
   4.641 +proof (rule borel_eq_sigmaI1[OF borel_def])
   4.642 +  fix M :: "'a set" assume "M \<in> {S. open S}"
   4.643 +  then have "open M" by simp
   4.644 +  show "M \<in> ?SIGMA"
   4.645 +    apply (subst open_UNION[OF `open M`])
   4.646 +    apply (safe intro!: countable_UN)
   4.647 +    apply auto
   4.648 +    done
   4.649  qed auto
   4.650  
   4.651  lemma borel_eq_atLeastLessThan:
   4.652 -  "borel = sigma \<lparr>space=UNIV, sets=range (\<lambda>(a, b). {a ..< b :: real})\<rparr>" (is "_ = ?S")
   4.653 -proof (intro algebra.equality antisym)
   4.654 -  interpret sigma_algebra ?S
   4.655 -    by (rule sigma_algebra_sigma) auto
   4.656 -  show "sets borel \<subseteq> sets ?S"
   4.657 -    unfolding borel_eq_lessThan
   4.658 -  proof (intro sets_sigma_subset subsetI)
   4.659 -    have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
   4.660 -    fix A :: "real set" assume "A \<in> sets \<lparr>space = UNIV, sets = range lessThan\<rparr>"
   4.661 -    then obtain x where "A = {..< x}" by auto
   4.662 -    then have "A = (\<Union>i::nat. {-real i ..< x})"
   4.663 -      by (auto simp: move_uminus real_arch_simple)
   4.664 -    then show "A \<in> sets ?S"
   4.665 -      by (auto simp: sets_sigma intro!: sigma_sets.intros)
   4.666 -  qed simp
   4.667 -  show "sets ?S \<subseteq> sets borel"
   4.668 -    by (intro borel.sets_sigma_subset) auto
   4.669 -qed simp_all
   4.670 -
   4.671 -lemma borel_eq_halfspace_le:
   4.672 -  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i \<le> a})\<rparr>)"
   4.673 -   (is "_ = ?SIGMA")
   4.674 -proof (intro algebra.equality antisym)
   4.675 -  show "sets borel \<subseteq> sets ?SIGMA"
   4.676 -    using open_span_halfspace halfspace_span_halfspace_le by auto
   4.677 -  show "sets ?SIGMA \<subseteq> sets borel"
   4.678 -    by (rule borel.sets_sigma_subset) auto
   4.679 +  "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
   4.680 +proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
   4.681 +  have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
   4.682 +  fix x :: real
   4.683 +  have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
   4.684 +    by (auto simp: move_uminus real_arch_simple)
   4.685 +  then show "{..< x} \<in> ?SIGMA"
   4.686 +    by (auto intro: sigma_sets.intros)
   4.687  qed auto
   4.688  
   4.689 -lemma borel_eq_halfspace_less:
   4.690 -  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i < a})\<rparr>)"
   4.691 -   (is "_ = ?SIGMA")
   4.692 -proof (intro algebra.equality antisym)
   4.693 -  show "sets borel \<subseteq> sets ?SIGMA"
   4.694 -    using open_span_halfspace .
   4.695 -  show "sets ?SIGMA \<subseteq> sets borel"
   4.696 -    by (rule borel.sets_sigma_subset) auto
   4.697 -qed auto
   4.698 -
   4.699 -lemma borel_eq_halfspace_gt:
   4.700 -  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a < x$$i})\<rparr>)"
   4.701 -   (is "_ = ?SIGMA")
   4.702 -proof (intro algebra.equality antisym)
   4.703 -  show "sets borel \<subseteq> sets ?SIGMA"
   4.704 -    using halfspace_le_span_halfspace_gt open_span_halfspace halfspace_span_halfspace_le by auto
   4.705 -  show "sets ?SIGMA \<subseteq> sets borel"
   4.706 -    by (rule borel.sets_sigma_subset) auto
   4.707 -qed auto
   4.708 -
   4.709 -lemma borel_eq_halfspace_ge:
   4.710 -  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a \<le> x$$i})\<rparr>)"
   4.711 -   (is "_ = ?SIGMA")
   4.712 -proof (intro algebra.equality antisym)
   4.713 -  show "sets borel \<subseteq> sets ?SIGMA"
   4.714 -    using halfspace_span_halfspace_ge open_span_halfspace by auto
   4.715 -  show "sets ?SIGMA \<subseteq> sets borel"
   4.716 -    by (rule borel.sets_sigma_subset) auto
   4.717 -qed auto
   4.718 -
   4.719 -lemma (in sigma_algebra) borel_measurable_halfspacesI:
   4.720 +lemma borel_measurable_halfspacesI:
   4.721    fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   4.722 -  assumes "borel = (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   4.723 -  and "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
   4.724 -  and "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
   4.725 +  assumes F: "borel = sigma UNIV (range F)"
   4.726 +  and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M" 
   4.727 +  and S: "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
   4.728    shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a::real. S a i \<in> sets M)"
   4.729  proof safe
   4.730    fix a :: real and i assume i: "i < DIM('c)" and f: "f \<in> borel_measurable M"
   4.731    then show "S a i \<in> sets M" unfolding assms
   4.732 -    by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1) sigma_def)
   4.733 +    by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1))
   4.734  next
   4.735    assume a: "\<forall>i<DIM('c). \<forall>a. S a i \<in> sets M"
   4.736    { fix a i have "S a i \<in> sets M"
   4.737 @@ -740,61 +590,58 @@
   4.738        with a show ?thesis unfolding assms(2) by simp
   4.739      next
   4.740        assume "\<not> i < DIM('c)"
   4.741 -      from assms(3)[OF this] show ?thesis .
   4.742 +      from S[OF this] show ?thesis .
   4.743      qed }
   4.744 -  then have "f \<in> measurable M (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   4.745 -    by (auto intro!: measurable_sigma simp: assms(2))
   4.746 -  then show "f \<in> borel_measurable M" unfolding measurable_def
   4.747 -    unfolding assms(1) by simp
   4.748 +  then show "f \<in> borel_measurable M"
   4.749 +    by (auto intro!: measurable_measure_of simp: S_eq F)
   4.750  qed
   4.751  
   4.752 -lemma (in sigma_algebra) borel_measurable_iff_halfspace_le:
   4.753 +lemma borel_measurable_iff_halfspace_le:
   4.754    fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   4.755    shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i \<le> a} \<in> sets M)"
   4.756    by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   4.757  
   4.758 -lemma (in sigma_algebra) borel_measurable_iff_halfspace_less:
   4.759 +lemma borel_measurable_iff_halfspace_less:
   4.760    fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   4.761    shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i < a} \<in> sets M)"
   4.762    by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   4.763  
   4.764 -lemma (in sigma_algebra) borel_measurable_iff_halfspace_ge:
   4.765 +lemma borel_measurable_iff_halfspace_ge:
   4.766    fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   4.767    shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a \<le> f w $$ i} \<in> sets M)"
   4.768    by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   4.769  
   4.770 -lemma (in sigma_algebra) borel_measurable_iff_halfspace_greater:
   4.771 +lemma borel_measurable_iff_halfspace_greater:
   4.772    fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   4.773    shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a < f w $$ i} \<in> sets M)"
   4.774 -  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_gt]) auto
   4.775 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
   4.776  
   4.777 -lemma (in sigma_algebra) borel_measurable_iff_le:
   4.778 +lemma borel_measurable_iff_le:
   4.779    "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   4.780    using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   4.781  
   4.782 -lemma (in sigma_algebra) borel_measurable_iff_less:
   4.783 +lemma borel_measurable_iff_less:
   4.784    "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   4.785    using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   4.786  
   4.787 -lemma (in sigma_algebra) borel_measurable_iff_ge:
   4.788 +lemma borel_measurable_iff_ge:
   4.789    "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   4.790    using borel_measurable_iff_halfspace_ge[where 'c=real] by simp
   4.791  
   4.792 -lemma (in sigma_algebra) borel_measurable_iff_greater:
   4.793 +lemma borel_measurable_iff_greater:
   4.794    "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   4.795    using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   4.796  
   4.797  lemma borel_measurable_euclidean_component:
   4.798    "(\<lambda>x::'a::euclidean_space. x $$ i) \<in> borel_measurable borel"
   4.799 -  unfolding borel_def[where 'a=real]
   4.800 -proof (rule borel.measurable_sigma, simp_all)
   4.801 +proof (rule borel_measurableI)
   4.802    fix S::"real set" assume "open S"
   4.803    from open_vimage_euclidean_component[OF this]
   4.804 -  show "(\<lambda>x. x $$ i) -` S \<in> sets borel"
   4.805 +  show "(\<lambda>x. x $$ i) -` S \<inter> space borel \<in> sets borel"
   4.806      by (auto intro: borel_open)
   4.807  qed
   4.808  
   4.809 -lemma (in sigma_algebra) borel_measurable_euclidean_space:
   4.810 +lemma borel_measurable_euclidean_space:
   4.811    fixes f :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   4.812    shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M)"
   4.813  proof safe
   4.814 @@ -810,7 +657,7 @@
   4.815  
   4.816  subsection "Borel measurable operators"
   4.817  
   4.818 -lemma (in sigma_algebra) affine_borel_measurable_vector:
   4.819 +lemma affine_borel_measurable_vector:
   4.820    fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   4.821    assumes "f \<in> borel_measurable M"
   4.822    shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   4.823 @@ -821,8 +668,7 @@
   4.824      assume "b \<noteq> 0"
   4.825      with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
   4.826        by (auto intro!: open_affinity simp: scaleR_add_right)
   4.827 -    hence "?S \<in> sets borel"
   4.828 -      unfolding borel_def by (auto simp: sigma_def intro!: sigma_sets.Basic)
   4.829 +    hence "?S \<in> sets borel" by auto
   4.830      moreover
   4.831      from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   4.832        apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   4.833 @@ -831,13 +677,13 @@
   4.834    qed simp
   4.835  qed
   4.836  
   4.837 -lemma (in sigma_algebra) affine_borel_measurable:
   4.838 +lemma affine_borel_measurable:
   4.839    fixes g :: "'a \<Rightarrow> real"
   4.840    assumes g: "g \<in> borel_measurable M"
   4.841    shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
   4.842    using affine_borel_measurable_vector[OF assms] by (simp add: mult_commute)
   4.843  
   4.844 -lemma (in sigma_algebra) borel_measurable_add[simp, intro]:
   4.845 +lemma borel_measurable_add[simp, intro]:
   4.846    fixes f :: "'a \<Rightarrow> real"
   4.847    assumes f: "f \<in> borel_measurable M"
   4.848    assumes g: "g \<in> borel_measurable M"
   4.849 @@ -855,7 +701,7 @@
   4.850      by (simp add: borel_measurable_iff_ge)
   4.851  qed
   4.852  
   4.853 -lemma (in sigma_algebra) borel_measurable_setsum[simp, intro]:
   4.854 +lemma borel_measurable_setsum[simp, intro]:
   4.855    fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   4.856    assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   4.857    shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   4.858 @@ -864,7 +710,7 @@
   4.859    thus ?thesis using assms by induct auto
   4.860  qed simp
   4.861  
   4.862 -lemma (in sigma_algebra) borel_measurable_square:
   4.863 +lemma borel_measurable_square:
   4.864    fixes f :: "'a \<Rightarrow> real"
   4.865    assumes f: "f \<in> borel_measurable M"
   4.866    shows "(\<lambda>x. (f x)^2) \<in> borel_measurable M"
   4.867 @@ -916,7 +762,7 @@
   4.868     shows"x*y = ((x+y)^2)/4 - ((x-y)^ 2)/4"
   4.869  by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric])
   4.870  
   4.871 -lemma (in sigma_algebra) borel_measurable_uminus[simp, intro]:
   4.872 +lemma borel_measurable_uminus[simp, intro]:
   4.873    fixes g :: "'a \<Rightarrow> real"
   4.874    assumes g: "g \<in> borel_measurable M"
   4.875    shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   4.876 @@ -928,7 +774,7 @@
   4.877    finally show ?thesis .
   4.878  qed
   4.879  
   4.880 -lemma (in sigma_algebra) borel_measurable_times[simp, intro]:
   4.881 +lemma borel_measurable_times[simp, intro]:
   4.882    fixes f :: "'a \<Rightarrow> real"
   4.883    assumes f: "f \<in> borel_measurable M"
   4.884    assumes g: "g \<in> borel_measurable M"
   4.885 @@ -947,7 +793,7 @@
   4.886      using 1 2 by simp
   4.887  qed
   4.888  
   4.889 -lemma (in sigma_algebra) borel_measurable_setprod[simp, intro]:
   4.890 +lemma borel_measurable_setprod[simp, intro]:
   4.891    fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   4.892    assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   4.893    shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   4.894 @@ -956,14 +802,14 @@
   4.895    thus ?thesis using assms by induct auto
   4.896  qed simp
   4.897  
   4.898 -lemma (in sigma_algebra) borel_measurable_diff[simp, intro]:
   4.899 +lemma borel_measurable_diff[simp, intro]:
   4.900    fixes f :: "'a \<Rightarrow> real"
   4.901    assumes f: "f \<in> borel_measurable M"
   4.902    assumes g: "g \<in> borel_measurable M"
   4.903    shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   4.904    unfolding diff_minus using assms by fast
   4.905  
   4.906 -lemma (in sigma_algebra) borel_measurable_inverse[simp, intro]:
   4.907 +lemma borel_measurable_inverse[simp, intro]:
   4.908    fixes f :: "'a \<Rightarrow> real"
   4.909    assumes "f \<in> borel_measurable M"
   4.910    shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   4.911 @@ -978,7 +824,7 @@
   4.912      by (auto intro!: Int Un)
   4.913  qed
   4.914  
   4.915 -lemma (in sigma_algebra) borel_measurable_divide[simp, intro]:
   4.916 +lemma borel_measurable_divide[simp, intro]:
   4.917    fixes f :: "'a \<Rightarrow> real"
   4.918    assumes "f \<in> borel_measurable M"
   4.919    and "g \<in> borel_measurable M"
   4.920 @@ -986,7 +832,7 @@
   4.921    unfolding field_divide_inverse
   4.922    by (rule borel_measurable_inverse borel_measurable_times assms)+
   4.923  
   4.924 -lemma (in sigma_algebra) borel_measurable_max[intro, simp]:
   4.925 +lemma borel_measurable_max[intro, simp]:
   4.926    fixes f g :: "'a \<Rightarrow> real"
   4.927    assumes "f \<in> borel_measurable M"
   4.928    assumes "g \<in> borel_measurable M"
   4.929 @@ -1001,7 +847,7 @@
   4.930      by (auto intro!: Int)
   4.931  qed
   4.932  
   4.933 -lemma (in sigma_algebra) borel_measurable_min[intro, simp]:
   4.934 +lemma borel_measurable_min[intro, simp]:
   4.935    fixes f g :: "'a \<Rightarrow> real"
   4.936    assumes "f \<in> borel_measurable M"
   4.937    assumes "g \<in> borel_measurable M"
   4.938 @@ -1016,7 +862,7 @@
   4.939      by (auto intro!: Int)
   4.940  qed
   4.941  
   4.942 -lemma (in sigma_algebra) borel_measurable_abs[simp, intro]:
   4.943 +lemma borel_measurable_abs[simp, intro]:
   4.944    assumes "f \<in> borel_measurable M"
   4.945    shows "(\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
   4.946  proof -
   4.947 @@ -1033,14 +879,14 @@
   4.948    fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   4.949    assumes "continuous_on UNIV f"
   4.950    shows "f \<in> borel_measurable borel"
   4.951 -  apply(rule borel.borel_measurableI)
   4.952 +  apply(rule borel_measurableI)
   4.953    using continuous_open_preimage[OF assms] unfolding vimage_def by auto
   4.954  
   4.955  lemma borel_measurable_continuous_on:
   4.956    fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   4.957    assumes cont: "continuous_on A f" "open A"
   4.958    shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
   4.959 -proof (rule borel.borel_measurableI)
   4.960 +proof (rule borel_measurableI)
   4.961    fix S :: "'b set" assume "open S"
   4.962    then have "open {x\<in>A. f x \<in> S}"
   4.963      by (intro continuous_open_preimage[OF cont]) auto
   4.964 @@ -1049,11 +895,11 @@
   4.965      {x\<in>A. f x \<in> S} \<union> (if c \<in> S then space borel - A else {})"
   4.966      by (auto split: split_if_asm)
   4.967    also have "\<dots> \<in> sets borel"
   4.968 -    using * `open A` by (auto simp del: space_borel intro!: borel.Un)
   4.969 +    using * `open A` by (auto simp del: space_borel intro!: Un)
   4.970    finally show "?f -` S \<inter> space borel \<in> sets borel" .
   4.971  qed
   4.972  
   4.973 -lemma (in sigma_algebra) convex_measurable:
   4.974 +lemma convex_measurable:
   4.975    fixes a b :: real
   4.976    assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> { a <..< b}"
   4.977    assumes q: "convex_on { a <..< b} q"
   4.978 @@ -1091,7 +937,7 @@
   4.979    finally show ?thesis .
   4.980  qed
   4.981  
   4.982 -lemma (in sigma_algebra) borel_measurable_log[simp,intro]:
   4.983 +lemma borel_measurable_log[simp,intro]:
   4.984    assumes f: "f \<in> borel_measurable M" and "1 < b"
   4.985    shows "(\<lambda>x. log b (f x)) \<in> borel_measurable M"
   4.986    using measurable_comp[OF f borel_measurable_borel_log[OF `1 < b`]]
   4.987 @@ -1101,25 +947,21 @@
   4.988  
   4.989  lemma borel_measurable_ereal_borel:
   4.990    "ereal \<in> borel_measurable borel"
   4.991 -  unfolding borel_def[where 'a=ereal]
   4.992 -proof (rule borel.measurable_sigma)
   4.993 -  fix X :: "ereal set" assume "X \<in> sets \<lparr>space = UNIV, sets = {S. open S} \<rparr>"
   4.994 -  then have "open X" by simp
   4.995 +proof (rule borel_measurableI)
   4.996 +  fix X :: "ereal set" assume "open X"
   4.997    then have "open (ereal -` X \<inter> space borel)"
   4.998      by (simp add: open_ereal_vimage)
   4.999    then show "ereal -` X \<inter> space borel \<in> sets borel" by auto
  4.1000 -qed auto
  4.1001 +qed
  4.1002  
  4.1003 -lemma (in sigma_algebra) borel_measurable_ereal[simp, intro]:
  4.1004 +lemma borel_measurable_ereal[simp, intro]:
  4.1005    assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  4.1006    using measurable_comp[OF f borel_measurable_ereal_borel] unfolding comp_def .
  4.1007  
  4.1008  lemma borel_measurable_real_of_ereal_borel:
  4.1009    "(real :: ereal \<Rightarrow> real) \<in> borel_measurable borel"
  4.1010 -  unfolding borel_def[where 'a=real]
  4.1011 -proof (rule borel.measurable_sigma)
  4.1012 -  fix B :: "real set" assume "B \<in> sets \<lparr>space = UNIV, sets = {S. open S} \<rparr>"
  4.1013 -  then have "open B" by simp
  4.1014 +proof (rule borel_measurableI)
  4.1015 +  fix B :: "real set" assume "open B"
  4.1016    have *: "ereal -` real -` (B - {0}) = B - {0}" by auto
  4.1017    have open_real: "open (real -` (B - {0}) :: ereal set)"
  4.1018      unfolding open_ereal_def * using `open B` by auto
  4.1019 @@ -1137,13 +979,13 @@
  4.1020      then show "(real -` B :: ereal set) \<inter> space borel \<in> sets borel"
  4.1021        using open_real by auto
  4.1022    qed
  4.1023 -qed auto
  4.1024 +qed
  4.1025  
  4.1026 -lemma (in sigma_algebra) borel_measurable_real_of_ereal[simp, intro]:
  4.1027 +lemma borel_measurable_real_of_ereal[simp, intro]:
  4.1028    assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: ereal)) \<in> borel_measurable M"
  4.1029    using measurable_comp[OF f borel_measurable_real_of_ereal_borel] unfolding comp_def .
  4.1030  
  4.1031 -lemma (in sigma_algebra) borel_measurable_ereal_iff:
  4.1032 +lemma borel_measurable_ereal_iff:
  4.1033    shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  4.1034  proof
  4.1035    assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  4.1036 @@ -1151,7 +993,7 @@
  4.1037    show "f \<in> borel_measurable M" by auto
  4.1038  qed auto
  4.1039  
  4.1040 -lemma (in sigma_algebra) borel_measurable_ereal_iff_real:
  4.1041 +lemma borel_measurable_ereal_iff_real:
  4.1042    fixes f :: "'a \<Rightarrow> ereal"
  4.1043    shows "f \<in> borel_measurable M \<longleftrightarrow>
  4.1044      ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<infinity>} \<inter> space M \<in> sets M \<and> f -` {-\<infinity>} \<inter> space M \<in> sets M)"
  4.1045 @@ -1165,7 +1007,7 @@
  4.1046    finally show "f \<in> borel_measurable M" .
  4.1047  qed (auto intro: measurable_sets borel_measurable_real_of_ereal)
  4.1048  
  4.1049 -lemma (in sigma_algebra) less_eq_ge_measurable:
  4.1050 +lemma less_eq_ge_measurable:
  4.1051    fixes f :: "'a \<Rightarrow> 'c::linorder"
  4.1052    shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
  4.1053  proof
  4.1054 @@ -1178,7 +1020,7 @@
  4.1055    ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
  4.1056  qed
  4.1057  
  4.1058 -lemma (in sigma_algebra) greater_eq_le_measurable:
  4.1059 +lemma greater_eq_le_measurable:
  4.1060    fixes f :: "'a \<Rightarrow> 'c::linorder"
  4.1061    shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
  4.1062  proof
  4.1063 @@ -1191,28 +1033,27 @@
  4.1064    ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
  4.1065  qed
  4.1066  
  4.1067 -lemma (in sigma_algebra) borel_measurable_uminus_borel_ereal:
  4.1068 +lemma borel_measurable_uminus_borel_ereal:
  4.1069    "(uminus :: ereal \<Rightarrow> ereal) \<in> borel_measurable borel"
  4.1070 -proof (subst borel_def, rule borel.measurable_sigma)
  4.1071 -  fix X :: "ereal set" assume "X \<in> sets \<lparr>space = UNIV, sets = {S. open S}\<rparr>"
  4.1072 -  then have "open X" by simp
  4.1073 +proof (rule borel_measurableI)
  4.1074 +  fix X :: "ereal set" assume "open X"
  4.1075    have "uminus -` X = uminus ` X" by (force simp: image_iff)
  4.1076    then have "open (uminus -` X)" using `open X` ereal_open_uminus by auto
  4.1077    then show "uminus -` X \<inter> space borel \<in> sets borel" by auto
  4.1078 -qed auto
  4.1079 +qed
  4.1080  
  4.1081 -lemma (in sigma_algebra) borel_measurable_uminus_ereal[intro]:
  4.1082 +lemma borel_measurable_uminus_ereal[intro]:
  4.1083    assumes "f \<in> borel_measurable M"
  4.1084    shows "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
  4.1085    using measurable_comp[OF assms borel_measurable_uminus_borel_ereal] by (simp add: comp_def)
  4.1086  
  4.1087 -lemma (in sigma_algebra) borel_measurable_uminus_eq_ereal[simp]:
  4.1088 +lemma borel_measurable_uminus_eq_ereal[simp]:
  4.1089    "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  4.1090  proof
  4.1091    assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
  4.1092  qed auto
  4.1093  
  4.1094 -lemma (in sigma_algebra) borel_measurable_eq_atMost_ereal:
  4.1095 +lemma borel_measurable_eq_atMost_ereal:
  4.1096    fixes f :: "'a \<Rightarrow> ereal"
  4.1097    shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
  4.1098  proof (intro iffI allI)
  4.1099 @@ -1244,7 +1085,7 @@
  4.1100    qed
  4.1101  qed (simp add: measurable_sets)
  4.1102  
  4.1103 -lemma (in sigma_algebra) borel_measurable_eq_atLeast_ereal:
  4.1104 +lemma borel_measurable_eq_atLeast_ereal:
  4.1105    "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  4.1106  proof
  4.1107    assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
  4.1108 @@ -1255,15 +1096,15 @@
  4.1109    then show "f \<in> borel_measurable M" by simp
  4.1110  qed (simp add: measurable_sets)
  4.1111  
  4.1112 -lemma (in sigma_algebra) borel_measurable_ereal_iff_less:
  4.1113 +lemma borel_measurable_ereal_iff_less:
  4.1114    "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  4.1115    unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
  4.1116  
  4.1117 -lemma (in sigma_algebra) borel_measurable_ereal_iff_ge:
  4.1118 +lemma borel_measurable_ereal_iff_ge:
  4.1119    "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  4.1120    unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
  4.1121  
  4.1122 -lemma (in sigma_algebra) borel_measurable_ereal_eq_const:
  4.1123 +lemma borel_measurable_ereal_eq_const:
  4.1124    fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
  4.1125    shows "{x\<in>space M. f x = c} \<in> sets M"
  4.1126  proof -
  4.1127 @@ -1271,7 +1112,7 @@
  4.1128    then show ?thesis using assms by (auto intro!: measurable_sets)
  4.1129  qed
  4.1130  
  4.1131 -lemma (in sigma_algebra) borel_measurable_ereal_neq_const:
  4.1132 +lemma borel_measurable_ereal_neq_const:
  4.1133    fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
  4.1134    shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  4.1135  proof -
  4.1136 @@ -1279,7 +1120,7 @@
  4.1137    then show ?thesis using assms by (auto intro!: measurable_sets)
  4.1138  qed
  4.1139  
  4.1140 -lemma (in sigma_algebra) borel_measurable_ereal_le[intro,simp]:
  4.1141 +lemma borel_measurable_ereal_le[intro,simp]:
  4.1142    fixes f g :: "'a \<Rightarrow> ereal"
  4.1143    assumes f: "f \<in> borel_measurable M"
  4.1144    assumes g: "g \<in> borel_measurable M"
  4.1145 @@ -1294,7 +1135,7 @@
  4.1146    with f g show ?thesis by (auto intro!: Un simp: measurable_sets)
  4.1147  qed
  4.1148  
  4.1149 -lemma (in sigma_algebra) borel_measurable_ereal_less[intro,simp]:
  4.1150 +lemma borel_measurable_ereal_less[intro,simp]:
  4.1151    fixes f :: "'a \<Rightarrow> ereal"
  4.1152    assumes f: "f \<in> borel_measurable M"
  4.1153    assumes g: "g \<in> borel_measurable M"
  4.1154 @@ -1304,7 +1145,7 @@
  4.1155    then show ?thesis using g f by auto
  4.1156  qed
  4.1157  
  4.1158 -lemma (in sigma_algebra) borel_measurable_ereal_eq[intro,simp]:
  4.1159 +lemma borel_measurable_ereal_eq[intro,simp]:
  4.1160    fixes f :: "'a \<Rightarrow> ereal"
  4.1161    assumes f: "f \<in> borel_measurable M"
  4.1162    assumes g: "g \<in> borel_measurable M"
  4.1163 @@ -1314,7 +1155,7 @@
  4.1164    then show ?thesis using g f by auto
  4.1165  qed
  4.1166  
  4.1167 -lemma (in sigma_algebra) borel_measurable_ereal_neq[intro,simp]:
  4.1168 +lemma borel_measurable_ereal_neq[intro,simp]:
  4.1169    fixes f :: "'a \<Rightarrow> ereal"
  4.1170    assumes f: "f \<in> borel_measurable M"
  4.1171    assumes g: "g \<in> borel_measurable M"
  4.1172 @@ -1324,12 +1165,12 @@
  4.1173    thus ?thesis using f g by auto
  4.1174  qed
  4.1175  
  4.1176 -lemma (in sigma_algebra) split_sets:
  4.1177 +lemma split_sets:
  4.1178    "{x\<in>space M. P x \<or> Q x} = {x\<in>space M. P x} \<union> {x\<in>space M. Q x}"
  4.1179    "{x\<in>space M. P x \<and> Q x} = {x\<in>space M. P x} \<inter> {x\<in>space M. Q x}"
  4.1180    by auto
  4.1181  
  4.1182 -lemma (in sigma_algebra) borel_measurable_ereal_add[intro, simp]:
  4.1183 +lemma borel_measurable_ereal_add[intro, simp]:
  4.1184    fixes f :: "'a \<Rightarrow> ereal"
  4.1185    assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  4.1186    shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  4.1187 @@ -1344,7 +1185,7 @@
  4.1188               intro!: Un measurable_If measurable_sets)
  4.1189  qed
  4.1190  
  4.1191 -lemma (in sigma_algebra) borel_measurable_ereal_setsum[simp, intro]:
  4.1192 +lemma borel_measurable_ereal_setsum[simp, intro]:
  4.1193    fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  4.1194    assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  4.1195    shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  4.1196 @@ -1354,7 +1195,7 @@
  4.1197      by induct auto
  4.1198  qed (simp add: borel_measurable_const)
  4.1199  
  4.1200 -lemma (in sigma_algebra) borel_measurable_ereal_abs[intro, simp]:
  4.1201 +lemma borel_measurable_ereal_abs[intro, simp]:
  4.1202    fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
  4.1203    shows "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
  4.1204  proof -
  4.1205 @@ -1362,7 +1203,7 @@
  4.1206    then show ?thesis using assms by (auto intro!: measurable_If)
  4.1207  qed
  4.1208  
  4.1209 -lemma (in sigma_algebra) borel_measurable_ereal_times[intro, simp]:
  4.1210 +lemma borel_measurable_ereal_times[intro, simp]:
  4.1211    fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  4.1212    shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  4.1213  proof -
  4.1214 @@ -1386,7 +1227,7 @@
  4.1215         (auto simp: split_sets intro!: Int)
  4.1216  qed
  4.1217  
  4.1218 -lemma (in sigma_algebra) borel_measurable_ereal_setprod[simp, intro]:
  4.1219 +lemma borel_measurable_ereal_setprod[simp, intro]:
  4.1220    fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  4.1221    assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  4.1222    shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  4.1223 @@ -1395,21 +1236,21 @@
  4.1224    thus ?thesis using assms by induct auto
  4.1225  qed simp
  4.1226  
  4.1227 -lemma (in sigma_algebra) borel_measurable_ereal_min[simp, intro]:
  4.1228 +lemma borel_measurable_ereal_min[simp, intro]:
  4.1229    fixes f g :: "'a \<Rightarrow> ereal"
  4.1230    assumes "f \<in> borel_measurable M"
  4.1231    assumes "g \<in> borel_measurable M"
  4.1232    shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  4.1233    using assms unfolding min_def by (auto intro!: measurable_If)
  4.1234  
  4.1235 -lemma (in sigma_algebra) borel_measurable_ereal_max[simp, intro]:
  4.1236 +lemma borel_measurable_ereal_max[simp, intro]:
  4.1237    fixes f g :: "'a \<Rightarrow> ereal"
  4.1238    assumes "f \<in> borel_measurable M"
  4.1239    and "g \<in> borel_measurable M"
  4.1240    shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  4.1241    using assms unfolding max_def by (auto intro!: measurable_If)
  4.1242  
  4.1243 -lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
  4.1244 +lemma borel_measurable_SUP[simp, intro]:
  4.1245    fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
  4.1246    assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  4.1247    shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  4.1248 @@ -1422,7 +1263,7 @@
  4.1249      using assms by auto
  4.1250  qed
  4.1251  
  4.1252 -lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
  4.1253 +lemma borel_measurable_INF[simp, intro]:
  4.1254    fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
  4.1255    assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  4.1256    shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  4.1257 @@ -1435,26 +1276,39 @@
  4.1258      using assms by auto
  4.1259  qed
  4.1260  
  4.1261 -lemma (in sigma_algebra) borel_measurable_liminf[simp, intro]:
  4.1262 +lemma borel_measurable_liminf[simp, intro]:
  4.1263    fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  4.1264    assumes "\<And>i. f i \<in> borel_measurable M"
  4.1265    shows "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  4.1266    unfolding liminf_SUPR_INFI using assms by auto
  4.1267  
  4.1268 -lemma (in sigma_algebra) borel_measurable_limsup[simp, intro]:
  4.1269 +lemma borel_measurable_limsup[simp, intro]:
  4.1270    fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  4.1271    assumes "\<And>i. f i \<in> borel_measurable M"
  4.1272    shows "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  4.1273    unfolding limsup_INFI_SUPR using assms by auto
  4.1274  
  4.1275 -lemma (in sigma_algebra) borel_measurable_ereal_diff[simp, intro]:
  4.1276 +lemma borel_measurable_ereal_diff[simp, intro]:
  4.1277    fixes f g :: "'a \<Rightarrow> ereal"
  4.1278    assumes "f \<in> borel_measurable M"
  4.1279    assumes "g \<in> borel_measurable M"
  4.1280    shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  4.1281    unfolding minus_ereal_def using assms by auto
  4.1282  
  4.1283 -lemma (in sigma_algebra) borel_measurable_psuminf[simp, intro]:
  4.1284 +lemma borel_measurable_ereal_inverse[simp, intro]:
  4.1285 +  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
  4.1286 +proof -
  4.1287 +  { fix x have "inverse (f x) = (if f x = 0 then \<infinity> else ereal (inverse (real (f x))))"
  4.1288 +      by (cases "f x") auto }
  4.1289 +  with f show ?thesis
  4.1290 +    by (auto intro!: measurable_If)
  4.1291 +qed
  4.1292 +
  4.1293 +lemma borel_measurable_ereal_divide[simp, intro]:
  4.1294 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. f x / g x :: ereal) \<in> borel_measurable M"
  4.1295 +  unfolding divide_ereal_def by auto
  4.1296 +
  4.1297 +lemma borel_measurable_psuminf[simp, intro]:
  4.1298    fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  4.1299    assumes "\<And>i. f i \<in> borel_measurable M" and pos: "\<And>i x. x \<in> space M \<Longrightarrow> 0 \<le> f i x"
  4.1300    shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  4.1301 @@ -1465,7 +1319,7 @@
  4.1302  
  4.1303  section "LIMSEQ is borel measurable"
  4.1304  
  4.1305 -lemma (in sigma_algebra) borel_measurable_LIMSEQ:
  4.1306 +lemma borel_measurable_LIMSEQ:
  4.1307    fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  4.1308    assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  4.1309    and u: "\<And>i. u i \<in> borel_measurable M"
     5.1 --- a/src/HOL/Probability/Caratheodory.thy	Mon Apr 23 12:23:23 2012 +0100
     5.2 +++ b/src/HOL/Probability/Caratheodory.thy	Mon Apr 23 12:14:35 2012 +0200
     5.3 @@ -6,7 +6,7 @@
     5.4  header {*Caratheodory Extension Theorem*}
     5.5  
     5.6  theory Caratheodory
     5.7 -imports Sigma_Algebra "~~/src/HOL/Multivariate_Analysis/Extended_Real_Limits"
     5.8 +  imports Measure_Space
     5.9  begin
    5.10  
    5.11  lemma sums_def2:
    5.12 @@ -53,128 +53,53 @@
    5.13  
    5.14  subsection {* Measure Spaces *}
    5.15  
    5.16 -record 'a measure_space = "'a algebra" +
    5.17 -  measure :: "'a set \<Rightarrow> ereal"
    5.18 -
    5.19 -definition positive where "positive M f \<longleftrightarrow> f {} = (0::ereal) \<and> (\<forall>A\<in>sets M. 0 \<le> f A)"
    5.20 -
    5.21 -definition additive where "additive M f \<longleftrightarrow>
    5.22 -  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) = f x + f y)"
    5.23 -
    5.24 -definition countably_additive :: "('a, 'b) algebra_scheme \<Rightarrow> ('a set \<Rightarrow> ereal) \<Rightarrow> bool" where
    5.25 -  "countably_additive M f \<longleftrightarrow> (\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow>
    5.26 -    (\<Sum>i. f (A i)) = f (\<Union>i. A i))"
    5.27 -
    5.28 -definition increasing where "increasing M f \<longleftrightarrow>
    5.29 -  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<subseteq> y \<longrightarrow> f x \<le> f y)"
    5.30 -
    5.31  definition subadditive where "subadditive M f \<longleftrightarrow>
    5.32 -  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) \<le> f x + f y)"
    5.33 +  (\<forall>x\<in>M. \<forall>y\<in>M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) \<le> f x + f y)"
    5.34  
    5.35  definition countably_subadditive where "countably_subadditive M f \<longleftrightarrow>
    5.36 -  (\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow>
    5.37 +  (\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow>
    5.38      (f (\<Union>i. A i) \<le> (\<Sum>i. f (A i))))"
    5.39  
    5.40 -definition lambda_system where "lambda_system M f = {l \<in> sets M.
    5.41 -  \<forall>x \<in> sets M. f (l \<inter> x) + f ((space M - l) \<inter> x) = f x}"
    5.42 +definition lambda_system where "lambda_system \<Omega> M f = {l \<in> M.
    5.43 +  \<forall>x \<in> M. f (l \<inter> x) + f ((\<Omega> - l) \<inter> x) = f x}"
    5.44  
    5.45  definition outer_measure_space where "outer_measure_space M f \<longleftrightarrow>
    5.46    positive M f \<and> increasing M f \<and> countably_subadditive M f"
    5.47  
    5.48  definition measure_set where "measure_set M f X = {r.
    5.49 -  \<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> X \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
    5.50 -
    5.51 -locale measure_space = sigma_algebra M for M :: "('a, 'b) measure_space_scheme" +
    5.52 -  assumes measure_positive: "positive M (measure M)"
    5.53 -      and ca: "countably_additive M (measure M)"
    5.54 -
    5.55 -abbreviation (in measure_space) "\<mu> \<equiv> measure M"
    5.56 -
    5.57 -lemma (in measure_space)
    5.58 -  shows empty_measure[simp, intro]: "\<mu> {} = 0"
    5.59 -  and positive_measure[simp, intro!]: "\<And>A. A \<in> sets M \<Longrightarrow> 0 \<le> \<mu> A"
    5.60 -  using measure_positive unfolding positive_def by auto
    5.61 -
    5.62 -lemma increasingD:
    5.63 -  "increasing M f \<Longrightarrow> x \<subseteq> y \<Longrightarrow> x\<in>sets M \<Longrightarrow> y\<in>sets M \<Longrightarrow> f x \<le> f y"
    5.64 -  by (auto simp add: increasing_def)
    5.65 +  \<exists>A. range A \<subseteq> M \<and> disjoint_family A \<and> X \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
    5.66  
    5.67  lemma subadditiveD:
    5.68 -  "subadditive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x \<in> sets M \<Longrightarrow> y \<in> sets M
    5.69 -    \<Longrightarrow> f (x \<union> y) \<le> f x + f y"
    5.70 +  "subadditive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x \<in> M \<Longrightarrow> y \<in> M \<Longrightarrow> f (x \<union> y) \<le> f x + f y"
    5.71    by (auto simp add: subadditive_def)
    5.72  
    5.73 -lemma additiveD:
    5.74 -  "additive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x \<in> sets M \<Longrightarrow> y \<in> sets M
    5.75 -    \<Longrightarrow> f (x \<union> y) = f x + f y"
    5.76 -  by (auto simp add: additive_def)
    5.77 -
    5.78 -lemma countably_additiveI:
    5.79 -  assumes "\<And>A x. range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Union>i. A i) \<in> sets M
    5.80 -    \<Longrightarrow> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
    5.81 -  shows "countably_additive M f"
    5.82 -  using assms by (simp add: countably_additive_def)
    5.83 -
    5.84 -section "Extend binary sets"
    5.85 -
    5.86 -lemma LIMSEQ_binaryset:
    5.87 -  assumes f: "f {} = 0"
    5.88 -  shows  "(\<lambda>n. \<Sum>i<n. f (binaryset A B i)) ----> f A + f B"
    5.89 -proof -
    5.90 -  have "(\<lambda>n. \<Sum>i < Suc (Suc n). f (binaryset A B i)) = (\<lambda>n. f A + f B)"
    5.91 -    proof
    5.92 -      fix n
    5.93 -      show "(\<Sum>i < Suc (Suc n). f (binaryset A B i)) = f A + f B"
    5.94 -        by (induct n)  (auto simp add: binaryset_def f)
    5.95 -    qed
    5.96 -  moreover
    5.97 -  have "... ----> f A + f B" by (rule tendsto_const)
    5.98 -  ultimately
    5.99 -  have "(\<lambda>n. \<Sum>i< Suc (Suc n). f (binaryset A B i)) ----> f A + f B"
   5.100 -    by metis
   5.101 -  hence "(\<lambda>n. \<Sum>i< n+2. f (binaryset A B i)) ----> f A + f B"
   5.102 -    by simp
   5.103 -  thus ?thesis by (rule LIMSEQ_offset [where k=2])
   5.104 -qed
   5.105 -
   5.106 -lemma binaryset_sums:
   5.107 -  assumes f: "f {} = 0"
   5.108 -  shows  "(\<lambda>n. f (binaryset A B n)) sums (f A + f B)"
   5.109 -    by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f] atLeast0LessThan)
   5.110 -
   5.111 -lemma suminf_binaryset_eq:
   5.112 -  fixes f :: "'a set \<Rightarrow> 'b::{comm_monoid_add, t2_space}"
   5.113 -  shows "f {} = 0 \<Longrightarrow> (\<Sum>n. f (binaryset A B n)) = f A + f B"
   5.114 -  by (metis binaryset_sums sums_unique)
   5.115 -
   5.116  subsection {* Lambda Systems *}
   5.117  
   5.118  lemma (in algebra) lambda_system_eq:
   5.119 -  shows "lambda_system M f = {l \<in> sets M.
   5.120 -    \<forall>x \<in> sets M. f (x \<inter> l) + f (x - l) = f x}"
   5.121 +  shows "lambda_system \<Omega> M f = {l \<in> M. \<forall>x \<in> M. f (x \<inter> l) + f (x - l) = f x}"
   5.122  proof -
   5.123 -  have [simp]: "!!l x. l \<in> sets M \<Longrightarrow> x \<in> sets M \<Longrightarrow> (space M - l) \<inter> x = x - l"
   5.124 +  have [simp]: "!!l x. l \<in> M \<Longrightarrow> x \<in> M \<Longrightarrow> (\<Omega> - l) \<inter> x = x - l"
   5.125      by (metis Int_Diff Int_absorb1 Int_commute sets_into_space)
   5.126    show ?thesis
   5.127      by (auto simp add: lambda_system_def) (metis Int_commute)+
   5.128  qed
   5.129  
   5.130  lemma (in algebra) lambda_system_empty:
   5.131 -  "positive M f \<Longrightarrow> {} \<in> lambda_system M f"
   5.132 +  "positive M f \<Longrightarrow> {} \<in> lambda_system \<Omega> M f"
   5.133    by (auto simp add: positive_def lambda_system_eq)
   5.134  
   5.135  lemma lambda_system_sets:
   5.136 -  "x \<in> lambda_system M f \<Longrightarrow> x \<in> sets M"
   5.137 +  "x \<in> lambda_system \<Omega> M f \<Longrightarrow> x \<in> M"
   5.138    by (simp add: lambda_system_def)
   5.139  
   5.140  lemma (in algebra) lambda_system_Compl:
   5.141    fixes f:: "'a set \<Rightarrow> ereal"
   5.142 -  assumes x: "x \<in> lambda_system M f"
   5.143 -  shows "space M - x \<in> lambda_system M f"
   5.144 +  assumes x: "x \<in> lambda_system \<Omega> M f"
   5.145 +  shows "\<Omega> - x \<in> lambda_system \<Omega> M f"
   5.146  proof -
   5.147 -  have "x \<subseteq> space M"
   5.148 +  have "x \<subseteq> \<Omega>"
   5.149      by (metis sets_into_space lambda_system_sets x)
   5.150 -  hence "space M - (space M - x) = x"
   5.151 +  hence "\<Omega> - (\<Omega> - x) = x"
   5.152      by (metis double_diff equalityE)
   5.153    with x show ?thesis
   5.154      by (force simp add: lambda_system_def ac_simps)
   5.155 @@ -182,16 +107,16 @@
   5.156  
   5.157  lemma (in algebra) lambda_system_Int:
   5.158    fixes f:: "'a set \<Rightarrow> ereal"
   5.159 -  assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
   5.160 -  shows "x \<inter> y \<in> lambda_system M f"
   5.161 +  assumes xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
   5.162 +  shows "x \<inter> y \<in> lambda_system \<Omega> M f"
   5.163  proof -
   5.164    from xl yl show ?thesis
   5.165    proof (auto simp add: positive_def lambda_system_eq Int)
   5.166      fix u
   5.167 -    assume x: "x \<in> sets M" and y: "y \<in> sets M" and u: "u \<in> sets M"
   5.168 -       and fx: "\<forall>z\<in>sets M. f (z \<inter> x) + f (z - x) = f z"
   5.169 -       and fy: "\<forall>z\<in>sets M. f (z \<inter> y) + f (z - y) = f z"
   5.170 -    have "u - x \<inter> y \<in> sets M"
   5.171 +    assume x: "x \<in> M" and y: "y \<in> M" and u: "u \<in> M"
   5.172 +       and fx: "\<forall>z\<in>M. f (z \<inter> x) + f (z - x) = f z"
   5.173 +       and fy: "\<forall>z\<in>M. f (z \<inter> y) + f (z - y) = f z"
   5.174 +    have "u - x \<inter> y \<in> M"
   5.175        by (metis Diff Diff_Int Un u x y)
   5.176      moreover
   5.177      have "(u - (x \<inter> y)) \<inter> y = u \<inter> y - x" by blast
   5.178 @@ -216,20 +141,20 @@
   5.179  
   5.180  lemma (in algebra) lambda_system_Un:
   5.181    fixes f:: "'a set \<Rightarrow> ereal"
   5.182 -  assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
   5.183 -  shows "x \<union> y \<in> lambda_system M f"
   5.184 +  assumes xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
   5.185 +  shows "x \<union> y \<in> lambda_system \<Omega> M f"
   5.186  proof -
   5.187 -  have "(space M - x) \<inter> (space M - y) \<in> sets M"
   5.188 +  have "(\<Omega> - x) \<inter> (\<Omega> - y) \<in> M"
   5.189      by (metis Diff_Un Un compl_sets lambda_system_sets xl yl)
   5.190    moreover
   5.191 -  have "x \<union> y = space M - ((space M - x) \<inter> (space M - y))"
   5.192 +  have "x \<union> y = \<Omega> - ((\<Omega> - x) \<inter> (\<Omega> - y))"
   5.193      by auto (metis subsetD lambda_system_sets sets_into_space xl yl)+
   5.194    ultimately show ?thesis
   5.195      by (metis lambda_system_Compl lambda_system_Int xl yl)
   5.196  qed
   5.197  
   5.198  lemma (in algebra) lambda_system_algebra:
   5.199 -  "positive M f \<Longrightarrow> algebra (M\<lparr>sets := lambda_system M f\<rparr>)"
   5.200 +  "positive M f \<Longrightarrow> algebra \<Omega> (lambda_system \<Omega> M f)"
   5.201    apply (auto simp add: algebra_iff_Un)
   5.202    apply (metis lambda_system_sets set_mp sets_into_space)
   5.203    apply (metis lambda_system_empty)
   5.204 @@ -238,117 +163,62 @@
   5.205    done
   5.206  
   5.207  lemma (in algebra) lambda_system_strong_additive:
   5.208 -  assumes z: "z \<in> sets M" and disj: "x \<inter> y = {}"
   5.209 -      and xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
   5.210 +  assumes z: "z \<in> M" and disj: "x \<inter> y = {}"
   5.211 +      and xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
   5.212    shows "f (z \<inter> (x \<union> y)) = f (z \<inter> x) + f (z \<inter> y)"
   5.213  proof -
   5.214    have "z \<inter> x = (z \<inter> (x \<union> y)) \<inter> x" using disj by blast
   5.215    moreover
   5.216    have "z \<inter> y = (z \<inter> (x \<union> y)) - x" using disj by blast
   5.217    moreover
   5.218 -  have "(z \<inter> (x \<union> y)) \<in> sets M"
   5.219 +  have "(z \<inter> (x \<union> y)) \<in> M"
   5.220      by (metis Int Un lambda_system_sets xl yl z)
   5.221    ultimately show ?thesis using xl yl
   5.222      by (simp add: lambda_system_eq)
   5.223  qed
   5.224  
   5.225 -lemma (in algebra) lambda_system_additive:
   5.226 -     "additive (M (|sets := lambda_system M f|)) f"
   5.227 +lemma (in algebra) lambda_system_additive: "additive (lambda_system \<Omega> M f) f"
   5.228  proof (auto simp add: additive_def)
   5.229    fix x and y
   5.230    assume disj: "x \<inter> y = {}"
   5.231 -     and xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
   5.232 -  hence  "x \<in> sets M" "y \<in> sets M" by (blast intro: lambda_system_sets)+
   5.233 +     and xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
   5.234 +  hence  "x \<in> M" "y \<in> M" by (blast intro: lambda_system_sets)+
   5.235    thus "f (x \<union> y) = f x + f y"
   5.236      using lambda_system_strong_additive [OF top disj xl yl]
   5.237      by (simp add: Un)
   5.238  qed
   5.239  
   5.240 -lemma (in ring_of_sets) disjointed_additive:
   5.241 -  assumes f: "positive M f" "additive M f" and A: "range A \<subseteq> sets M" "incseq A"
   5.242 -  shows "(\<Sum>i\<le>n. f (disjointed A i)) = f (A n)"
   5.243 -proof (induct n)
   5.244 -  case (Suc n)
   5.245 -  then have "(\<Sum>i\<le>Suc n. f (disjointed A i)) = f (A n) + f (disjointed A (Suc n))"
   5.246 -    by simp
   5.247 -  also have "\<dots> = f (A n \<union> disjointed A (Suc n))"
   5.248 -    using A by (subst f(2)[THEN additiveD]) (auto simp: disjointed_incseq)
   5.249 -  also have "A n \<union> disjointed A (Suc n) = A (Suc n)"
   5.250 -    using `incseq A` by (auto dest: incseq_SucD simp: disjointed_incseq)
   5.251 -  finally show ?case .
   5.252 -qed simp
   5.253 -
   5.254  lemma (in ring_of_sets) countably_subadditive_subadditive:
   5.255    assumes f: "positive M f" and cs: "countably_subadditive M f"
   5.256    shows  "subadditive M f"
   5.257  proof (auto simp add: subadditive_def)
   5.258    fix x y
   5.259 -  assume x: "x \<in> sets M" and y: "y \<in> sets M" and "x \<inter> y = {}"
   5.260 +  assume x: "x \<in> M" and y: "y \<in> M" and "x \<inter> y = {}"
   5.261    hence "disjoint_family (binaryset x y)"
   5.262      by (auto simp add: disjoint_family_on_def binaryset_def)
   5.263 -  hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
   5.264 -         (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
   5.265 +  hence "range (binaryset x y) \<subseteq> M \<longrightarrow>
   5.266 +         (\<Union>i. binaryset x y i) \<in> M \<longrightarrow>
   5.267           f (\<Union>i. binaryset x y i) \<le> (\<Sum> n. f (binaryset x y n))"
   5.268      using cs by (auto simp add: countably_subadditive_def)
   5.269 -  hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
   5.270 +  hence "{x,y,{}} \<subseteq> M \<longrightarrow> x \<union> y \<in> M \<longrightarrow>
   5.271           f (x \<union> y) \<le> (\<Sum> n. f (binaryset x y n))"
   5.272      by (simp add: range_binaryset_eq UN_binaryset_eq)
   5.273    thus "f (x \<union> y) \<le>  f x + f y" using f x y
   5.274      by (auto simp add: Un o_def suminf_binaryset_eq positive_def)
   5.275  qed
   5.276  
   5.277 -lemma (in ring_of_sets) additive_sum:
   5.278 -  fixes A:: "nat \<Rightarrow> 'a set"
   5.279 -  assumes f: "positive M f" and ad: "additive M f" and "finite S"
   5.280 -      and A: "range A \<subseteq> sets M"
   5.281 -      and disj: "disjoint_family_on A S"
   5.282 -  shows  "(\<Sum>i\<in>S. f (A i)) = f (\<Union>i\<in>S. A i)"
   5.283 -using `finite S` disj proof induct
   5.284 -  case empty show ?case using f by (simp add: positive_def)
   5.285 -next
   5.286 -  case (insert s S)
   5.287 -  then have "A s \<inter> (\<Union>i\<in>S. A i) = {}"
   5.288 -    by (auto simp add: disjoint_family_on_def neq_iff)
   5.289 -  moreover
   5.290 -  have "A s \<in> sets M" using A by blast
   5.291 -  moreover have "(\<Union>i\<in>S. A i) \<in> sets M"
   5.292 -    using A `finite S` by auto
   5.293 -  moreover
   5.294 -  ultimately have "f (A s \<union> (\<Union>i\<in>S. A i)) = f (A s) + f(\<Union>i\<in>S. A i)"
   5.295 -    using ad UNION_in_sets A by (auto simp add: additive_def)
   5.296 -  with insert show ?case using ad disjoint_family_on_mono[of S "insert s S" A]
   5.297 -    by (auto simp add: additive_def subset_insertI)
   5.298 -qed
   5.299 -
   5.300 -lemma (in algebra) increasing_additive_bound:
   5.301 -  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> ereal"
   5.302 -  assumes f: "positive M f" and ad: "additive M f"
   5.303 -      and inc: "increasing M f"
   5.304 -      and A: "range A \<subseteq> sets M"
   5.305 -      and disj: "disjoint_family A"
   5.306 -  shows  "(\<Sum>i. f (A i)) \<le> f (space M)"
   5.307 -proof (safe intro!: suminf_bound)
   5.308 -  fix N
   5.309 -  note disj_N = disjoint_family_on_mono[OF _ disj, of "{..<N}"]
   5.310 -  have "(\<Sum>i<N. f (A i)) = f (\<Union>i\<in>{..<N}. A i)"
   5.311 -    by (rule additive_sum [OF f ad _ A]) (auto simp: disj_N)
   5.312 -  also have "... \<le> f (space M)" using space_closed A
   5.313 -    by (intro increasingD[OF inc] finite_UN) auto
   5.314 -  finally show "(\<Sum>i<N. f (A i)) \<le> f (space M)" by simp
   5.315 -qed (insert f A, auto simp: positive_def)
   5.316 -
   5.317  lemma lambda_system_increasing:
   5.318 - "increasing M f \<Longrightarrow> increasing (M (|sets := lambda_system M f|)) f"
   5.319 + "increasing M f \<Longrightarrow> increasing (lambda_system \<Omega> M f) f"
   5.320    by (simp add: increasing_def lambda_system_def)
   5.321  
   5.322  lemma lambda_system_positive:
   5.323 -  "positive M f \<Longrightarrow> positive (M (|sets := lambda_system M f|)) f"
   5.324 +  "positive M f \<Longrightarrow> positive (lambda_system \<Omega> M f) f"
   5.325    by (simp add: positive_def lambda_system_def)
   5.326  
   5.327  lemma (in algebra) lambda_system_strong_sum:
   5.328    fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> ereal"
   5.329 -  assumes f: "positive M f" and a: "a \<in> sets M"
   5.330 -      and A: "range A \<subseteq> lambda_system M f"
   5.331 +  assumes f: "positive M f" and a: "a \<in> M"
   5.332 +      and A: "range A \<subseteq> lambda_system \<Omega> M f"
   5.333        and disj: "disjoint_family A"
   5.334    shows  "(\<Sum>i = 0..<n. f (a \<inter>A i)) = f (a \<inter> (\<Union>i\<in>{0..<n}. A i))"
   5.335  proof (induct n)
   5.336 @@ -357,11 +227,11 @@
   5.337    case (Suc n)
   5.338    have 2: "A n \<inter> UNION {0..<n} A = {}" using disj
   5.339      by (force simp add: disjoint_family_on_def neq_iff)
   5.340 -  have 3: "A n \<in> lambda_system M f" using A
   5.341 +  have 3: "A n \<in> lambda_system \<Omega> M f" using A
   5.342      by blast
   5.343 -  interpret l: algebra "M\<lparr>sets := lambda_system M f\<rparr>"
   5.344 +  interpret l: algebra \<Omega> "lambda_system \<Omega> M f"
   5.345      using f by (rule lambda_system_algebra)
   5.346 -  have 4: "UNION {0..<n} A \<in> lambda_system M f"
   5.347 +  have 4: "UNION {0..<n} A \<in> lambda_system \<Omega> M f"
   5.348      using A l.UNION_in_sets by simp
   5.349    from Suc.hyps show ?case
   5.350      by (simp add: atLeastLessThanSuc lambda_system_strong_additive [OF a 2 3 4])
   5.351 @@ -369,23 +239,23 @@
   5.352  
   5.353  lemma (in sigma_algebra) lambda_system_caratheodory:
   5.354    assumes oms: "outer_measure_space M f"
   5.355 -      and A: "range A \<subseteq> lambda_system M f"
   5.356 +      and A: "range A \<subseteq> lambda_system \<Omega> M f"
   5.357        and disj: "disjoint_family A"
   5.358 -  shows  "(\<Union>i. A i) \<in> lambda_system M f \<and> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
   5.359 +  shows  "(\<Union>i. A i) \<in> lambda_system \<Omega> M f \<and> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
   5.360  proof -
   5.361    have pos: "positive M f" and inc: "increasing M f"
   5.362     and csa: "countably_subadditive M f"
   5.363      by (metis oms outer_measure_space_def)+
   5.364    have sa: "subadditive M f"
   5.365      by (metis countably_subadditive_subadditive csa pos)
   5.366 -  have A': "range A \<subseteq> sets (M(|sets := lambda_system M f|))" using A
   5.367 -    by simp
   5.368 -  interpret ls: algebra "M\<lparr>sets := lambda_system M f\<rparr>"
   5.369 +  have A': "\<And>S. A`S \<subseteq> (lambda_system \<Omega> M f)" using A
   5.370 +    by auto
   5.371 +  interpret ls: algebra \<Omega> "lambda_system \<Omega> M f"
   5.372      using pos by (rule lambda_system_algebra)
   5.373 -  have A'': "range A \<subseteq> sets M"
   5.374 +  have A'': "range A \<subseteq> M"
   5.375       by (metis A image_subset_iff lambda_system_sets)
   5.376  
   5.377 -  have U_in: "(\<Union>i. A i) \<in> sets M"
   5.378 +  have U_in: "(\<Union>i. A i) \<in> M"
   5.379      by (metis A'' countable_UN)
   5.380    have U_eq: "f (\<Union>i. A i) = (\<Sum>i. f (A i))"
   5.381    proof (rule antisym)
   5.382 @@ -396,22 +266,22 @@
   5.383      show "(\<Sum>i. f (A i)) \<le> f (\<Union>i. A i)"
   5.384        using ls.additive_sum [OF lambda_system_positive[OF pos] lambda_system_additive _ A' dis]
   5.385        using A''
   5.386 -      by (intro suminf_bound[OF _ *]) (auto intro!: increasingD[OF inc] allI countable_UN)
   5.387 +      by (intro suminf_bound[OF _ *]) (auto intro!: increasingD[OF inc] countable_UN)
   5.388    qed
   5.389    {
   5.390      fix a
   5.391 -    assume a [iff]: "a \<in> sets M"
   5.392 +    assume a [iff]: "a \<in> M"
   5.393      have "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) = f a"
   5.394      proof -
   5.395        show ?thesis
   5.396        proof (rule antisym)
   5.397 -        have "range (\<lambda>i. a \<inter> A i) \<subseteq> sets M" using A''
   5.398 +        have "range (\<lambda>i. a \<inter> A i) \<subseteq> M" using A''
   5.399            by blast
   5.400          moreover
   5.401          have "disjoint_family (\<lambda>i. a \<inter> A i)" using disj
   5.402            by (auto simp add: disjoint_family_on_def)
   5.403          moreover
   5.404 -        have "a \<inter> (\<Union>i. A i) \<in> sets M"
   5.405 +        have "a \<inter> (\<Union>i. A i) \<in> M"
   5.406            by (metis Int U_in a)
   5.407          ultimately
   5.408          have "f (a \<inter> (\<Union>i. A i)) \<le> (\<Sum>i. f (a \<inter> A i))"
   5.409 @@ -424,11 +294,11 @@
   5.410          have "(\<Sum>i. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
   5.411            proof (intro suminf_bound_add allI)
   5.412              fix n
   5.413 -            have UNION_in: "(\<Union>i\<in>{0..<n}. A i) \<in> sets M"
   5.414 +            have UNION_in: "(\<Union>i\<in>{0..<n}. A i) \<in> M"
   5.415                by (metis A'' UNION_in_sets)
   5.416              have le_fa: "f (UNION {0..<n} A \<inter> a) \<le> f a" using A''
   5.417                by (blast intro: increasingD [OF inc] A'' UNION_in_sets)
   5.418 -            have ls: "(\<Union>i\<in>{0..<n}. A i) \<in> lambda_system M f"
   5.419 +            have ls: "(\<Union>i\<in>{0..<n}. A i) \<in> lambda_system \<Omega> M f"
   5.420                using ls.UNION_in_sets by (simp add: A)
   5.421              hence eq_fa: "f a = f (a \<inter> (\<Union>i\<in>{0..<n}. A i)) + f (a - (\<Union>i\<in>{0..<n}. A i))"
   5.422                by (simp add: lambda_system_eq UNION_in)
   5.423 @@ -437,9 +307,9 @@
   5.424              thus "(\<Sum>i<n. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
   5.425                by (simp add: lambda_system_strong_sum pos A disj eq_fa add_left_mono atLeast0LessThan[symmetric])
   5.426            next
   5.427 -            have "\<And>i. a \<inter> A i \<in> sets M" using A'' by auto
   5.428 +            have "\<And>i. a \<inter> A i \<in> M" using A'' by auto
   5.429              then show "\<And>i. 0 \<le> f (a \<inter> A i)" using pos[unfolded positive_def] by auto
   5.430 -            have "\<And>i. a - (\<Union>i. A i) \<in> sets M" using A'' by auto
   5.431 +            have "\<And>i. a - (\<Union>i. A i) \<in> M" using A'' by auto
   5.432              then have "\<And>i. 0 \<le> f (a - (\<Union>i. A i))" using pos[unfolded positive_def] by auto
   5.433              then show "f (a - (\<Union>i. A i)) \<noteq> -\<infinity>" by auto
   5.434            qed
   5.435 @@ -460,66 +330,29 @@
   5.436  
   5.437  lemma (in sigma_algebra) caratheodory_lemma:
   5.438    assumes oms: "outer_measure_space M f"
   5.439 -  shows "measure_space \<lparr> space = space M, sets = lambda_system M f, measure = f \<rparr>"
   5.440 -    (is "measure_space ?M")
   5.441 +  defines "L \<equiv> lambda_system \<Omega> M f"
   5.442 +  shows "measure_space \<Omega> L f"
   5.443  proof -
   5.444    have pos: "positive M f"
   5.445      by (metis oms outer_measure_space_def)
   5.446 -  have alg: "algebra ?M"
   5.447 +  have alg: "algebra \<Omega> L"
   5.448      using lambda_system_algebra [of f, OF pos]
   5.449 -    by (simp add: algebra_iff_Un)
   5.450 +    by (simp add: algebra_iff_Un L_def)
   5.451    then
   5.452 -  have "sigma_algebra ?M"
   5.453 +  have "sigma_algebra \<Omega> L"
   5.454      using lambda_system_caratheodory [OF oms]
   5.455 -    by (simp add: sigma_algebra_disjoint_iff)
   5.456 +    by (simp add: sigma_algebra_disjoint_iff L_def)
   5.457    moreover
   5.458 -  have "measure_space_axioms ?M"
   5.459 +  have "countably_additive L f" "positive L f"
   5.460      using pos lambda_system_caratheodory [OF oms]
   5.461 -    by (simp add: measure_space_axioms_def positive_def lambda_system_sets
   5.462 -                  countably_additive_def o_def)
   5.463 +    by (auto simp add: lambda_system_sets L_def countably_additive_def positive_def)
   5.464    ultimately
   5.465    show ?thesis
   5.466 -    by (simp add: measure_space_def)
   5.467 -qed
   5.468 -
   5.469 -lemma (in ring_of_sets) additive_increasing:
   5.470 -  assumes posf: "positive M f" and addf: "additive M f"
   5.471 -  shows "increasing M f"
   5.472 -proof (auto simp add: increasing_def)
   5.473 -  fix x y
   5.474 -  assume xy: "x \<in> sets M" "y \<in> sets M" "x \<subseteq> y"
   5.475 -  then have "y - x \<in> sets M" by auto
   5.476 -  then have "0 \<le> f (y-x)" using posf[unfolded positive_def] by auto
   5.477 -  then have "f x + 0 \<le> f x + f (y-x)" by (intro add_left_mono) auto
   5.478 -  also have "... = f (x \<union> (y-x))" using addf
   5.479 -    by (auto simp add: additive_def) (metis Diff_disjoint Un_Diff_cancel Diff xy(1,2))
   5.480 -  also have "... = f y"
   5.481 -    by (metis Un_Diff_cancel Un_absorb1 xy(3))
   5.482 -  finally show "f x \<le> f y" by simp
   5.483 -qed
   5.484 -
   5.485 -lemma (in ring_of_sets) countably_additive_additive:
   5.486 -  assumes posf: "positive M f" and ca: "countably_additive M f"
   5.487 -  shows "additive M f"
   5.488 -proof (auto simp add: additive_def)
   5.489 -  fix x y
   5.490 -  assume x: "x \<in> sets M" and y: "y \<in> sets M" and "x \<inter> y = {}"
   5.491 -  hence "disjoint_family (binaryset x y)"
   5.492 -    by (auto simp add: disjoint_family_on_def binaryset_def)
   5.493 -  hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
   5.494 -         (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
   5.495 -         f (\<Union>i. binaryset x y i) = (\<Sum> n. f (binaryset x y n))"
   5.496 -    using ca
   5.497 -    by (simp add: countably_additive_def)
   5.498 -  hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
   5.499 -         f (x \<union> y) = (\<Sum>n. f (binaryset x y n))"
   5.500 -    by (simp add: range_binaryset_eq UN_binaryset_eq)
   5.501 -  thus "f (x \<union> y) = f x + f y" using posf x y
   5.502 -    by (auto simp add: Un suminf_binaryset_eq positive_def)
   5.503 +    using pos by (simp add: measure_space_def)
   5.504  qed
   5.505  
   5.506  lemma inf_measure_nonempty:
   5.507 -  assumes f: "positive M f" and b: "b \<in> sets M" and a: "a \<subseteq> b" "{} \<in> sets M"
   5.508 +  assumes f: "positive M f" and b: "b \<in> M" and a: "a \<subseteq> b" "{} \<in> M"
   5.509    shows "f b \<in> measure_set M f a"
   5.510  proof -
   5.511    let ?A = "\<lambda>i::nat. (if i = 0 then b else {})"
   5.512 @@ -534,14 +367,14 @@
   5.513  
   5.514  lemma (in ring_of_sets) inf_measure_agrees:
   5.515    assumes posf: "positive M f" and ca: "countably_additive M f"
   5.516 -      and s: "s \<in> sets M"
   5.517 +      and s: "s \<in> M"
   5.518    shows "Inf (measure_set M f s) = f s"
   5.519    unfolding Inf_ereal_def
   5.520  proof (safe intro!: Greatest_equality)
   5.521    fix z
   5.522    assume z: "z \<in> measure_set M f s"
   5.523    from this obtain A where
   5.524 -    A: "range A \<subseteq> sets M" and disj: "disjoint_family A"
   5.525 +    A: "range A \<subseteq> M" and disj: "disjoint_family A"
   5.526      and "s \<subseteq> (\<Union>x. A x)" and si: "(\<Sum>i. f (A i)) = z"
   5.527      by (auto simp add: measure_set_def comp_def)
   5.528    hence seq: "s = (\<Union>i. A i \<inter> s)" by blast
   5.529 @@ -549,11 +382,11 @@
   5.530      by (metis additive_increasing ca countably_additive_additive posf)
   5.531    have sums: "(\<Sum>i. f (A i \<inter> s)) = f (\<Union>i. A i \<inter> s)"
   5.532      proof (rule ca[unfolded countably_additive_def, rule_format])
   5.533 -      show "range (\<lambda>n. A n \<inter> s) \<subseteq> sets M" using A s
   5.534 +      show "range (\<lambda>n. A n \<inter> s) \<subseteq> M" using A s
   5.535          by blast
   5.536        show "disjoint_family (\<lambda>n. A n \<inter> s)" using disj
   5.537          by (auto simp add: disjoint_family_on_def)
   5.538 -      show "(\<Union>i. A i \<inter> s) \<in> sets M" using A s
   5.539 +      show "(\<Union>i. A i \<inter> s) \<in> M" using A s
   5.540          by (metis UN_extend_simps(4) s seq)
   5.541      qed
   5.542    hence "f s = (\<Sum>i. f (A i \<inter> s))"
   5.543 @@ -562,7 +395,7 @@
   5.544      proof (rule suminf_le_pos)
   5.545        fix n show "f (A n \<inter> s) \<le> f (A n)" using A s
   5.546          by (force intro: increasingD [OF inc])
   5.547 -      fix N have "A N \<inter> s \<in> sets M"  using A s by auto
   5.548 +      fix N have "A N \<inter> s \<in> M"  using A s by auto
   5.549        then show "0 \<le> f (A N \<inter> s)" using posf unfolding positive_def by auto
   5.550      qed
   5.551    also have "... = z" by (rule si)
   5.552 @@ -578,7 +411,7 @@
   5.553    assumes posf: "positive M f" "r \<in> measure_set M f X"
   5.554    shows "0 \<le> r"
   5.555  proof -
   5.556 -  obtain A where "range A \<subseteq> sets M" and r: "r = (\<Sum>i. f (A i))"
   5.557 +  obtain A where "range A \<subseteq> M" and r: "r = (\<Sum>i. f (A i))"
   5.558      using `r \<in> measure_set M f X` unfolding measure_set_def by auto
   5.559    then show "0 \<le> r" using posf unfolding r positive_def
   5.560      by (intro suminf_0_le) auto
   5.561 @@ -593,26 +426,25 @@
   5.562  qed
   5.563  
   5.564  lemma inf_measure_empty:
   5.565 -  assumes posf: "positive M f" and "{} \<in> sets M"
   5.566 +  assumes posf: "positive M f" and "{} \<in> M"
   5.567    shows "Inf (measure_set M f {}) = 0"
   5.568  proof (rule antisym)
   5.569    show "Inf (measure_set M f {}) \<le> 0"
   5.570 -    by (metis complete_lattice_class.Inf_lower `{} \<in> sets M`
   5.571 +    by (metis complete_lattice_class.Inf_lower `{} \<in> M`
   5.572                inf_measure_nonempty[OF posf] subset_refl posf[unfolded positive_def])
   5.573  qed (rule inf_measure_pos[OF posf])
   5.574  
   5.575  lemma (in ring_of_sets) inf_measure_positive:
   5.576 -  assumes p: "positive M f" and "{} \<in> sets M"
   5.577 +  assumes p: "positive M f" and "{} \<in> M"
   5.578    shows "positive M (\<lambda>x. Inf (measure_set M f x))"
   5.579  proof (unfold positive_def, intro conjI ballI)
   5.580    show "Inf (measure_set M f {}) = 0" using inf_measure_empty[OF assms] by auto
   5.581 -  fix A assume "A \<in> sets M"
   5.582 +  fix A assume "A \<in> M"
   5.583  qed (rule inf_measure_pos[OF p])
   5.584  
   5.585  lemma (in ring_of_sets) inf_measure_increasing:
   5.586    assumes posf: "positive M f"
   5.587 -  shows "increasing \<lparr> space = space M, sets = Pow (space M) \<rparr>
   5.588 -                    (\<lambda>x. Inf (measure_set M f x))"
   5.589 +  shows "increasing (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
   5.590  apply (clarsimp simp add: increasing_def)
   5.591  apply (rule complete_lattice_class.Inf_greatest)
   5.592  apply (rule complete_lattice_class.Inf_lower)
   5.593 @@ -621,13 +453,13 @@
   5.594  
   5.595  lemma (in ring_of_sets) inf_measure_le:
   5.596    assumes posf: "positive M f" and inc: "increasing M f"
   5.597 -      and x: "x \<in> {r . \<exists>A. range A \<subseteq> sets M \<and> s \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
   5.598 +      and x: "x \<in> {r . \<exists>A. range A \<subseteq> M \<and> s \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
   5.599    shows "Inf (measure_set M f s) \<le> x"
   5.600  proof -
   5.601 -  obtain A where A: "range A \<subseteq> sets M" and ss: "s \<subseteq> (\<Union>i. A i)"
   5.602 +  obtain A where A: "range A \<subseteq> M" and ss: "s \<subseteq> (\<Union>i. A i)"
   5.603               and xeq: "(\<Sum>i. f (A i)) = x"
   5.604      using x by auto
   5.605 -  have dA: "range (disjointed A) \<subseteq> sets M"
   5.606 +  have dA: "range (disjointed A) \<subseteq> M"
   5.607      by (metis A range_disjointed_sets)
   5.608    have "\<forall>n. f (disjointed A n) \<le> f (A n)"
   5.609      by (metis increasingD [OF inc] UNIV_I dA image_subset_iff disjointed_subset A comp_def)
   5.610 @@ -648,8 +480,8 @@
   5.611  
   5.612  lemma (in ring_of_sets) inf_measure_close:
   5.613    fixes e :: ereal
   5.614 -  assumes posf: "positive M f" and e: "0 < e" and ss: "s \<subseteq> (space M)" and "Inf (measure_set M f s) \<noteq> \<infinity>"
   5.615 -  shows "\<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> s \<subseteq> (\<Union>i. A i) \<and>
   5.616 +  assumes posf: "positive M f" and e: "0 < e" and ss: "s \<subseteq> (\<Omega>)" and "Inf (measure_set M f s) \<noteq> \<infinity>"
   5.617 +  shows "\<exists>A. range A \<subseteq> M \<and> disjoint_family A \<and> s \<subseteq> (\<Union>i. A i) \<and>
   5.618                 (\<Sum>i. f (A i)) \<le> Inf (measure_set M f s) + e"
   5.619  proof -
   5.620    from `Inf (measure_set M f s) \<noteq> \<infinity>` have fin: "\<bar>Inf (measure_set M f s)\<bar> \<noteq> \<infinity>"
   5.621 @@ -662,22 +494,21 @@
   5.622  
   5.623  lemma (in ring_of_sets) inf_measure_countably_subadditive:
   5.624    assumes posf: "positive M f" and inc: "increasing M f"
   5.625 -  shows "countably_subadditive (| space = space M, sets = Pow (space M) |)
   5.626 -                  (\<lambda>x. Inf (measure_set M f x))"
   5.627 +  shows "countably_subadditive (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
   5.628  proof (simp add: countably_subadditive_def, safe)
   5.629    fix A :: "nat \<Rightarrow> 'a set"
   5.630    let ?outer = "\<lambda>B. Inf (measure_set M f B)"
   5.631 -  assume A: "range A \<subseteq> Pow (space M)"
   5.632 +  assume A: "range A \<subseteq> Pow (\<Omega>)"
   5.633       and disj: "disjoint_family A"
   5.634 -     and sb: "(\<Union>i. A i) \<subseteq> space M"
   5.635 +     and sb: "(\<Union>i. A i) \<subseteq> \<Omega>"
   5.636  
   5.637    { fix e :: ereal assume e: "0 < e" and "\<forall>i. ?outer (A i) \<noteq> \<infinity>"
   5.638 -    hence "\<exists>BB. \<forall>n. range (BB n) \<subseteq> sets M \<and> disjoint_family (BB n) \<and>
   5.639 +    hence "\<exists>BB. \<forall>n. range (BB n) \<subseteq> M \<and> disjoint_family (BB n) \<and>
   5.640          A n \<subseteq> (\<Union>i. BB n i) \<and> (\<Sum>i. f (BB n i)) \<le> ?outer (A n) + e * (1/2)^(Suc n)"
   5.641        apply (safe intro!: choice inf_measure_close [of f, OF posf])
   5.642        using e sb by (auto simp: ereal_zero_less_0_iff one_ereal_def)
   5.643      then obtain BB
   5.644 -      where BB: "\<And>n. (range (BB n) \<subseteq> sets M)"
   5.645 +      where BB: "\<And>n. (range (BB n) \<subseteq> M)"
   5.646        and disjBB: "\<And>n. disjoint_family (BB n)"
   5.647        and sbBB: "\<And>n. A n \<subseteq> (\<Union>i. BB n i)"
   5.648        and BBle: "\<And>n. (\<Sum>i. f (BB n i)) \<le> ?outer (A n) + e * (1/2)^(Suc n)"
   5.649 @@ -697,7 +528,7 @@
   5.650        finally show ?thesis .
   5.651      qed
   5.652      def C \<equiv> "(split BB) o prod_decode"
   5.653 -    have C: "!!n. C n \<in> sets M"
   5.654 +    have C: "!!n. C n \<in> M"
   5.655        apply (rule_tac p="prod_decode n" in PairE)
   5.656        apply (simp add: C_def)
   5.657        apply (metis BB subsetD rangeI)
   5.658 @@ -744,9 +575,8 @@
   5.659  qed
   5.660  
   5.661  lemma (in ring_of_sets) inf_measure_outer:
   5.662 -  "\<lbrakk> positive M f ; increasing M f \<rbrakk>
   5.663 -   \<Longrightarrow> outer_measure_space \<lparr> space = space M, sets = Pow (space M) \<rparr>
   5.664 -                          (\<lambda>x. Inf (measure_set M f x))"
   5.665 +  "\<lbrakk> positive M f ; increasing M f \<rbrakk> \<Longrightarrow>
   5.666 +    outer_measure_space (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
   5.667    using inf_measure_pos[of M f]
   5.668    by (simp add: outer_measure_space_def inf_measure_empty
   5.669                  inf_measure_increasing inf_measure_countably_subadditive positive_def)
   5.670 @@ -754,14 +584,13 @@
   5.671  lemma (in ring_of_sets) algebra_subset_lambda_system:
   5.672    assumes posf: "positive M f" and inc: "increasing M f"
   5.673        and add: "additive M f"
   5.674 -  shows "sets M \<subseteq> lambda_system \<lparr> space = space M, sets = Pow (space M) \<rparr>
   5.675 -                                (\<lambda>x. Inf (measure_set M f x))"
   5.676 +  shows "M \<subseteq> lambda_system \<Omega> (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
   5.677  proof (auto dest: sets_into_space
   5.678              simp add: algebra.lambda_system_eq [OF algebra_Pow])
   5.679    fix x s
   5.680 -  assume x: "x \<in> sets M"
   5.681 -     and s: "s \<subseteq> space M"
   5.682 -  have [simp]: "!!x. x \<in> sets M \<Longrightarrow> s \<inter> (space M - x) = s-x" using s
   5.683 +  assume x: "x \<in> M"
   5.684 +     and s: "s \<subseteq> \<Omega>"
   5.685 +  have [simp]: "!!x. x \<in> M \<Longrightarrow> s \<inter> (\<Omega> - x) = s-x" using s
   5.686      by blast
   5.687    have "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
   5.688          \<le> Inf (measure_set M f s)"
   5.689 @@ -774,7 +603,7 @@
   5.690      show ?thesis
   5.691      proof (rule complete_lattice_class.Inf_greatest)
   5.692        fix r assume "r \<in> measure_set M f s"
   5.693 -      then obtain A where A: "disjoint_family A" "range A \<subseteq> sets M" "s \<subseteq> (\<Union>i. A i)"
   5.694 +      then obtain A where A: "disjoint_family A" "range A \<subseteq> M" "s \<subseteq> (\<Union>i. A i)"
   5.695          and r: "r = (\<Sum>i. f (A i))" unfolding measure_set_def by auto
   5.696        have "Inf (measure_set M f (s \<inter> x)) \<le> (\<Sum>i. f (A i \<inter> x))"
   5.697          unfolding measure_set_def
   5.698 @@ -822,34 +651,31 @@
   5.699  qed
   5.700  
   5.701  lemma measure_down:
   5.702 -  "measure_space N \<Longrightarrow> sigma_algebra M \<Longrightarrow> sets M \<subseteq> sets N \<Longrightarrow> measure N = measure M \<Longrightarrow> measure_space M"
   5.703 -  by (simp add: measure_space_def measure_space_axioms_def positive_def
   5.704 -                countably_additive_def)
   5.705 +  "measure_space \<Omega> N \<mu> \<Longrightarrow> sigma_algebra \<Omega> M \<Longrightarrow> M \<subseteq> N \<Longrightarrow> measure_space \<Omega> M \<mu>"
   5.706 +  by (simp add: measure_space_def positive_def countably_additive_def)
   5.707       blast
   5.708  
   5.709  theorem (in ring_of_sets) caratheodory:
   5.710    assumes posf: "positive M f" and ca: "countably_additive M f"
   5.711 -  shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and>
   5.712 -            measure_space \<lparr> space = space M, sets = sets (sigma M), measure = \<mu> \<rparr>"
   5.713 +  shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> M. \<mu> s = f s) \<and> measure_space \<Omega> (sigma_sets \<Omega> M) \<mu>"
   5.714  proof -
   5.715    have inc: "increasing M f"
   5.716      by (metis additive_increasing ca countably_additive_additive posf)
   5.717    let ?infm = "(\<lambda>x. Inf (measure_set M f x))"
   5.718 -  def ls \<equiv> "lambda_system (|space = space M, sets = Pow (space M)|) ?infm"
   5.719 -  have mls: "measure_space \<lparr>space = space M, sets = ls, measure = ?infm\<rparr>"
   5.720 +  def ls \<equiv> "lambda_system \<Omega> (Pow \<Omega>) ?infm"
   5.721 +  have mls: "measure_space \<Omega> ls ?infm"
   5.722      using sigma_algebra.caratheodory_lemma
   5.723              [OF sigma_algebra_Pow  inf_measure_outer [OF posf inc]]
   5.724      by (simp add: ls_def)
   5.725 -  hence sls: "sigma_algebra (|space = space M, sets = ls, measure = ?infm|)"
   5.726 +  hence sls: "sigma_algebra \<Omega> ls"
   5.727      by (simp add: measure_space_def)
   5.728 -  have "sets M \<subseteq> ls"
   5.729 +  have "M \<subseteq> ls"
   5.730      by (simp add: ls_def)
   5.731         (metis ca posf inc countably_additive_additive algebra_subset_lambda_system)
   5.732 -  hence sgs_sb: "sigma_sets (space M) (sets M) \<subseteq> ls"
   5.733 -    using sigma_algebra.sigma_sets_subset [OF sls, of "sets M"]
   5.734 +  hence sgs_sb: "sigma_sets (\<Omega>) (M) \<subseteq> ls"
   5.735 +    using sigma_algebra.sigma_sets_subset [OF sls, of "M"]
   5.736      by simp
   5.737 -  have "measure_space \<lparr> space = space M, sets = sets (sigma M), measure = ?infm \<rparr>"
   5.738 -    unfolding sigma_def
   5.739 +  have "measure_space \<Omega> (sigma_sets \<Omega> M) ?infm"
   5.740      by (rule measure_down [OF mls], rule sigma_algebra_sigma_sets)
   5.741         (simp_all add: sgs_sb space_closed)
   5.742    thus ?thesis using inf_measure_agrees [OF posf ca]
   5.743 @@ -861,12 +687,12 @@
   5.744  lemma (in ring_of_sets) countably_additive_iff_continuous_from_below:
   5.745    assumes f: "positive M f" "additive M f"
   5.746    shows "countably_additive M f \<longleftrightarrow>
   5.747 -    (\<forall>A. range A \<subseteq> sets M \<longrightarrow> incseq A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Union>i. A i))"
   5.748 +    (\<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))"
   5.749    unfolding countably_additive_def
   5.750  proof safe
   5.751 -  assume count_sum: "\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> UNION UNIV A \<in> sets M \<longrightarrow> (\<Sum>i. f (A i)) = f (UNION UNIV A)"
   5.752 -  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets M" "incseq A" "(\<Union>i. A i) \<in> sets M"
   5.753 -  then have dA: "range (disjointed A) \<subseteq> sets M" by (auto simp: range_disjointed_sets)
   5.754 +  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)"
   5.755 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
   5.756 +  then have dA: "range (disjointed A) \<subseteq> M" by (auto simp: range_disjointed_sets)
   5.757    with count_sum[THEN spec, of "disjointed A"] A(3)
   5.758    have f_UN: "(\<Sum>i. f (disjointed A i)) = f (\<Union>i. A i)"
   5.759      by (auto simp: UN_disjointed_eq disjoint_family_disjointed)
   5.760 @@ -880,12 +706,12 @@
   5.761      using disjointed_additive[OF f A(1,2)] .
   5.762    ultimately show "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)" by simp
   5.763  next
   5.764 -  assume cont: "\<forall>A. range A \<subseteq> sets M \<longrightarrow> incseq A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Union>i. A i)"
   5.765 -  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets M" "disjoint_family A" "(\<Union>i. A i) \<in> sets M"
   5.766 +  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)"
   5.767 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "disjoint_family A" "(\<Union>i. A i) \<in> M"
   5.768    have *: "(\<Union>n. (\<Union>i\<le>n. A i)) = (\<Union>i. A i)" by auto
   5.769    have "(\<lambda>n. f (\<Union>i\<le>n. A i)) ----> f (\<Union>i. A i)"
   5.770    proof (unfold *[symmetric], intro cont[rule_format])
   5.771 -    show "range (\<lambda>i. \<Union> i\<le>i. A i) \<subseteq> sets M" "(\<Union>i. \<Union> i\<le>i. A i) \<in> sets M"
   5.772 +    show "range (\<lambda>i. \<Union> i\<le>i. A i) \<subseteq> M" "(\<Union>i. \<Union> i\<le>i. A i) \<in> M"
   5.773        using A * by auto
   5.774    qed (force intro!: incseq_SucI)
   5.775    moreover have "\<And>n. f (\<Union>i\<le>n. A i) = (\<Sum>i\<le>n. f (A i))"
   5.776 @@ -901,18 +727,18 @@
   5.777  
   5.778  lemma (in ring_of_sets) continuous_from_above_iff_empty_continuous:
   5.779    assumes f: "positive M f" "additive M f"
   5.780 -  shows "(\<forall>A. range A \<subseteq> sets M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) \<in> sets M \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Inter>i. A i))
   5.781 -     \<longleftrightarrow> (\<forall>A. range A \<subseteq> sets M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> 0)"
   5.782 +  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))
   5.783 +     \<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)"
   5.784  proof safe
   5.785 -  assume cont: "(\<forall>A. range A \<subseteq> sets M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) \<in> sets M \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> f (\<Inter>i. A i))"
   5.786 -  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets M" "decseq A" "(\<Inter>i. A i) = {}" "\<forall>i. f (A i) \<noteq> \<infinity>"
   5.787 +  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))"
   5.788 +  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>"
   5.789    with cont[THEN spec, of A] show "(\<lambda>i. f (A i)) ----> 0"
   5.790      using `positive M f`[unfolded positive_def] by auto
   5.791  next
   5.792 -  assume cont: "\<forall>A. range A \<subseteq> sets M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   5.793 -  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets M" "decseq A" "(\<Inter>i. A i) \<in> sets M" "\<forall>i. f (A i) \<noteq> \<infinity>"
   5.794 +  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"
   5.795 +  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>"
   5.796  
   5.797 -  have f_mono: "\<And>a b. a \<in> sets M \<Longrightarrow> b \<in> sets M \<Longrightarrow> a \<subseteq> b \<Longrightarrow> f a \<le> f b"
   5.798 +  have f_mono: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<subseteq> b \<Longrightarrow> f a \<le> f b"
   5.799      using additive_increasing[OF f] unfolding increasing_def by simp
   5.800  
   5.801    have decseq_fA: "decseq (\<lambda>i. f (A i))"
   5.802 @@ -932,7 +758,7 @@
   5.803    note f_fin = this
   5.804    have "(\<lambda>i. f (A i - (\<Inter>i. A i))) ----> 0"
   5.805    proof (intro cont[rule_format, OF _ decseq _ f_fin])
   5.806 -    show "range (\<lambda>i. A i - (\<Inter>i. A i)) \<subseteq> sets M" "(\<Inter>i. A i - (\<Inter>i. A i)) = {}"
   5.807 +    show "range (\<lambda>i. A i - (\<Inter>i. A i)) \<subseteq> M" "(\<Inter>i. A i - (\<Inter>i. A i)) = {}"
   5.808        using A by auto
   5.809    qed
   5.810    from INF_Lim_ereal[OF decseq_f this]
   5.811 @@ -956,17 +782,17 @@
   5.812  qed
   5.813  
   5.814  lemma positiveD1: "positive M f \<Longrightarrow> f {} = 0" by (auto simp: positive_def)
   5.815 -lemma positiveD2: "positive M f \<Longrightarrow> A \<in> sets M \<Longrightarrow> 0 \<le> f A" by (auto simp: positive_def)
   5.816 +lemma positiveD2: "positive M f \<Longrightarrow> A \<in> M \<Longrightarrow> 0 \<le> f A" by (auto simp: positive_def)
   5.817  
   5.818  lemma (in ring_of_sets) empty_continuous_imp_continuous_from_below:
   5.819 -  assumes f: "positive M f" "additive M f" "\<forall>A\<in>sets M. f A \<noteq> \<infinity>"
   5.820 -  assumes cont: "\<forall>A. range A \<subseteq> sets M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   5.821 -  assumes A: "range A \<subseteq> sets M" "incseq A" "(\<Union>i. A i) \<in> sets M"
   5.822 +  assumes f: "positive M f" "additive M f" "\<forall>A\<in>M. f A \<noteq> \<infinity>"
   5.823 +  assumes cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   5.824 +  assumes A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
   5.825    shows "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)"
   5.826  proof -
   5.827 -  have "\<forall>A\<in>sets M. \<exists>x. f A = ereal x"
   5.828 +  have "\<forall>A\<in>M. \<exists>x. f A = ereal x"
   5.829    proof
   5.830 -    fix A assume "A \<in> sets M" with f show "\<exists>x. f A = ereal x"
   5.831 +    fix A assume "A \<in> M" with f show "\<exists>x. f A = ereal x"
   5.832        unfolding positive_def by (cases "f A") auto
   5.833    qed
   5.834    from bchoice[OF this] guess f' .. note f' = this[rule_format]
   5.835 @@ -991,20 +817,19 @@
   5.836  qed
   5.837  
   5.838  lemma (in ring_of_sets) empty_continuous_imp_countably_additive:
   5.839 -  assumes f: "positive M f" "additive M f" and fin: "\<forall>A\<in>sets M. f A \<noteq> \<infinity>"
   5.840 -  assumes cont: "\<And>A. range A \<subseteq> sets M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   5.841 +  assumes f: "positive M f" "additive M f" and fin: "\<forall>A\<in>M. f A \<noteq> \<infinity>"
   5.842 +  assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   5.843    shows "countably_additive M f"
   5.844    using countably_additive_iff_continuous_from_below[OF f]
   5.845    using empty_continuous_imp_continuous_from_below[OF f fin] cont
   5.846    by blast
   5.847  
   5.848  lemma (in ring_of_sets) caratheodory_empty_continuous:
   5.849 -  assumes f: "positive M f" "additive M f" and fin: "\<And>A. A \<in> sets M \<Longrightarrow> f A \<noteq> \<infinity>"
   5.850 -  assumes cont: "\<And>A. range A \<subseteq> sets M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   5.851 -  shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and>
   5.852 -            measure_space \<lparr> space = space M, sets = sets (sigma M), measure = \<mu> \<rparr>"
   5.853 +  assumes f: "positive M f" "additive M f" and fin: "\<And>A. A \<in> M \<Longrightarrow> f A \<noteq> \<infinity>"
   5.854 +  assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   5.855 +  shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> M. \<mu> s = f s) \<and> measure_space \<Omega> (sigma_sets \<Omega> M) \<mu>"
   5.856  proof (intro caratheodory empty_continuous_imp_countably_additive f)
   5.857 -  show "\<forall>A\<in>sets M. f A \<noteq> \<infinity>" using fin by auto
   5.858 +  show "\<forall>A\<in>M. f A \<noteq> \<infinity>" using fin by auto
   5.859  qed (rule cont)
   5.860  
   5.861  end
     6.1 --- a/src/HOL/Probability/Complete_Measure.thy	Mon Apr 23 12:23:23 2012 +0100
     6.2 +++ b/src/HOL/Probability/Complete_Measure.thy	Mon Apr 23 12:14:35 2012 +0200
     6.3 @@ -6,218 +6,231 @@
     6.4  imports Lebesgue_Integration
     6.5  begin
     6.6  
     6.7 -locale completeable_measure_space = measure_space
     6.8 -
     6.9 -definition (in completeable_measure_space)
    6.10 -  "split_completion A p = (\<exists>N'. A = fst p \<union> snd p \<and> fst p \<inter> snd p = {} \<and>
    6.11 -    fst p \<in> sets M \<and> snd p \<subseteq> N' \<and> N' \<in> null_sets)"
    6.12 -
    6.13 -definition (in completeable_measure_space)
    6.14 -  "main_part A = fst (Eps (split_completion A))"
    6.15 +definition
    6.16 +  "split_completion M A p = (if A \<in> sets M then p = (A, {}) else
    6.17 +   \<exists>N'. A = fst p \<union> snd p \<and> fst p \<inter> snd p = {} \<and> fst p \<in> sets M \<and> snd p \<subseteq> N' \<and> N' \<in> null_sets M)"
    6.18  
    6.19 -definition (in completeable_measure_space)
    6.20 -  "null_part A = snd (Eps (split_completion A))"
    6.21 -
    6.22 -abbreviation (in completeable_measure_space) "\<mu>' A \<equiv> \<mu> (main_part A)"
    6.23 +definition
    6.24 +  "main_part M A = fst (Eps (split_completion M A))"
    6.25  
    6.26 -definition (in completeable_measure_space) completion :: "('a, 'b) measure_space_scheme" where
    6.27 -  "completion = \<lparr> space = space M,
    6.28 -                  sets = { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets \<and> N \<subseteq> N' },
    6.29 -                  measure = \<mu>',
    6.30 -                  \<dots> = more M \<rparr>"
    6.31 -
    6.32 +definition
    6.33 +  "null_part M A = snd (Eps (split_completion M A))"
    6.34  
    6.35 -lemma (in completeable_measure_space) space_completion[simp]:
    6.36 -  "space completion = space M" unfolding completion_def by simp
    6.37 -
    6.38 -lemma (in completeable_measure_space) sets_completionE:
    6.39 -  assumes "A \<in> sets completion"
    6.40 -  obtains S N N' where "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets" "S \<in> sets M"
    6.41 -  using assms unfolding completion_def by auto
    6.42 +definition completion :: "'a measure \<Rightarrow> 'a measure" where
    6.43 +  "completion M = measure_of (space M) { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' }
    6.44 +    (emeasure M \<circ> main_part M)"
    6.45  
    6.46 -lemma (in completeable_measure_space) sets_completionI:
    6.47 -  assumes "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets" "S \<in> sets M"
    6.48 -  shows "A \<in> sets completion"
    6.49 -  using assms unfolding completion_def by auto
    6.50 +lemma completion_into_space:
    6.51 +  "{ S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' } \<subseteq> Pow (space M)"
    6.52 +  using sets_into_space by auto
    6.53  
    6.54 -lemma (in completeable_measure_space) sets_completionI_sets[intro]:
    6.55 -  "A \<in> sets M \<Longrightarrow> A \<in> sets completion"
    6.56 -  unfolding completion_def by force
    6.57 +lemma space_completion[simp]: "space (completion M) = space M"
    6.58 +  unfolding completion_def using space_measure_of[OF completion_into_space] by simp
    6.59  
    6.60 -lemma (in completeable_measure_space) null_sets_completion:
    6.61 -  assumes "N' \<in> null_sets" "N \<subseteq> N'" shows "N \<in> sets completion"
    6.62 -  apply(rule sets_completionI[of N "{}" N N'])
    6.63 +lemma completionI:
    6.64 +  assumes "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets M" "S \<in> sets M"
    6.65 +  shows "A \<in> { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' }"
    6.66    using assms by auto
    6.67  
    6.68 -sublocale completeable_measure_space \<subseteq> completion!: sigma_algebra completion
    6.69 -proof (unfold sigma_algebra_iff2, safe)
    6.70 -  fix A x assume "A \<in> sets completion" "x \<in> A"
    6.71 -  with sets_into_space show "x \<in> space completion"
    6.72 -    by (auto elim!: sets_completionE)
    6.73 +lemma completionE:
    6.74 +  assumes "A \<in> { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' }"
    6.75 +  obtains S N N' where "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets M" "S \<in> sets M"
    6.76 +  using assms by auto
    6.77 +
    6.78 +lemma sigma_algebra_completion:
    6.79 +  "sigma_algebra (space M) { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' }"
    6.80 +    (is "sigma_algebra _ ?A")
    6.81 +  unfolding sigma_algebra_iff2
    6.82 +proof (intro conjI ballI allI impI)
    6.83 +  show "?A \<subseteq> Pow (space M)"
    6.84 +    using sets_into_space by auto
    6.85  next
    6.86 -  fix A assume "A \<in> sets completion"
    6.87 -  from this[THEN sets_completionE] guess S N N' . note A = this
    6.88 -  let ?C = "space completion"
    6.89 -  show "?C - A \<in> sets completion" using A
    6.90 -    by (intro sets_completionI[of _ "(?C - S) \<inter> (?C - N')" "(?C - S) \<inter> N' \<inter> (?C - N)"])
    6.91 -       auto
    6.92 +  show "{} \<in> ?A" by auto
    6.93  next
    6.94 -  fix A ::"nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets completion"
    6.95 -  then have "\<forall>n. \<exists>S N N'. A n = S \<union> N \<and> S \<in> sets M \<and> N' \<in> null_sets \<and> N \<subseteq> N'"
    6.96 -    unfolding completion_def by (auto simp: image_subset_iff)
    6.97 +  let ?C = "space M"
    6.98 +  fix A assume "A \<in> ?A" from completionE[OF this] guess S N N' .
    6.99 +  then show "space M - A \<in> ?A"
   6.100 +    by (intro completionI[of _ "(?C - S) \<inter> (?C - N')" "(?C - S) \<inter> N' \<inter> (?C - N)"]) auto
   6.101 +next
   6.102 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> ?A"
   6.103 +  then have "\<forall>n. \<exists>S N N'. A n = S \<union> N \<and> S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N'"
   6.104 +    by (auto simp: image_subset_iff)
   6.105    from choice[OF this] guess S ..
   6.106    from choice[OF this] guess N ..
   6.107    from choice[OF this] guess N' ..
   6.108 -  then show "UNION UNIV A \<in> sets completion"
   6.109 +  then show "UNION UNIV A \<in> ?A"
   6.110      using null_sets_UN[of N']
   6.111 -    by (intro sets_completionI[of _ "UNION UNIV S" "UNION UNIV N" "UNION UNIV N'"])
   6.112 -       auto
   6.113 -qed auto
   6.114 +    by (intro completionI[of _ "UNION UNIV S" "UNION UNIV N" "UNION UNIV N'"]) auto
   6.115 +qed
   6.116 +
   6.117 +lemma sets_completion:
   6.118 +  "sets (completion M) = { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' }"
   6.119 +  using sigma_algebra.sets_measure_of_eq[OF sigma_algebra_completion] by (simp add: completion_def)
   6.120 +
   6.121 +lemma sets_completionE:
   6.122 +  assumes "A \<in> sets (completion M)"
   6.123 +  obtains S N N' where "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets M" "S \<in> sets M"
   6.124 +  using assms unfolding sets_completion by auto
   6.125 +
   6.126 +lemma sets_completionI:
   6.127 +  assumes "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets M" "S \<in> sets M"
   6.128 +  shows "A \<in> sets (completion M)"
   6.129 +  using assms unfolding sets_completion by auto
   6.130 +
   6.131 +lemma sets_completionI_sets[intro, simp]:
   6.132 +  "A \<in> sets M \<Longrightarrow> A \<in> sets (completion M)"
   6.133 +  unfolding sets_completion by force
   6.134  
   6.135 -lemma (in completeable_measure_space) split_completion:
   6.136 -  assumes "A \<in> sets completion"
   6.137 -  shows "split_completion A (main_part A, null_part A)"
   6.138 -  unfolding main_part_def null_part_def
   6.139 -proof (rule someI2_ex)
   6.140 -  from assms[THEN sets_completionE] guess S N N' . note A = this
   6.141 -  let ?P = "(S, N - S)"
   6.142 -  show "\<exists>p. split_completion A p"
   6.143 -    unfolding split_completion_def using A
   6.144 -  proof (intro exI conjI)
   6.145 -    show "A = fst ?P \<union> snd ?P" using A by auto
   6.146 -    show "snd ?P \<subseteq> N'" using A by auto
   6.147 +lemma null_sets_completion:
   6.148 +  assumes "N' \<in> null_sets M" "N \<subseteq> N'" shows "N \<in> sets (completion M)"
   6.149 +  using assms by (intro sets_completionI[of N "{}" N N']) auto
   6.150 +
   6.151 +lemma split_completion:
   6.152 +  assumes "A \<in> sets (completion M)"
   6.153 +  shows "split_completion M A (main_part M A, null_part M A)"
   6.154 +proof cases
   6.155 +  assume "A \<in> sets M" then show ?thesis
   6.156 +    by (simp add: split_completion_def[abs_def] main_part_def null_part_def)
   6.157 +next
   6.158 +  assume nA: "A \<notin> sets M"
   6.159 +  show ?thesis
   6.160 +    unfolding main_part_def null_part_def if_not_P[OF nA]
   6.161 +  proof (rule someI2_ex)
   6.162 +    from assms[THEN sets_completionE] guess S N N' . note A = this
   6.163 +    let ?P = "(S, N - S)"
   6.164 +    show "\<exists>p. split_completion M A p"
   6.165 +      unfolding split_completion_def if_not_P[OF nA] using A
   6.166 +    proof (intro exI conjI)
   6.167 +      show "A = fst ?P \<union> snd ?P" using A by auto
   6.168 +      show "snd ?P \<subseteq> N'" using A by auto
   6.169 +   qed auto
   6.170    qed auto
   6.171 -qed auto
   6.172 +qed
   6.173  
   6.174 -lemma (in completeable_measure_space)
   6.175 -  assumes "S \<in> sets completion"
   6.176 -  shows main_part_sets[intro, simp]: "main_part S \<in> sets M"
   6.177 -    and main_part_null_part_Un[simp]: "main_part S \<union> null_part S = S"
   6.178 -    and main_part_null_part_Int[simp]: "main_part S \<inter> null_part S = {}"
   6.179 -  using split_completion[OF assms] by (auto simp: split_completion_def)
   6.180 +lemma
   6.181 +  assumes "S \<in> sets (completion M)"
   6.182 +  shows main_part_sets[intro, simp]: "main_part M S \<in> sets M"
   6.183 +    and main_part_null_part_Un[simp]: "main_part M S \<union> null_part M S = S"
   6.184 +    and main_part_null_part_Int[simp]: "main_part M S \<inter> null_part M S = {}"
   6.185 +  using split_completion[OF assms]
   6.186 +  by (auto simp: split_completion_def split: split_if_asm)
   6.187  
   6.188 -lemma (in completeable_measure_space) null_part:
   6.189 -  assumes "S \<in> sets completion" shows "\<exists>N. N\<in>null_sets \<and> null_part S \<subseteq> N"
   6.190 -  using split_completion[OF assms] by (auto simp: split_completion_def)
   6.191 +lemma main_part[simp]: "S \<in> sets M \<Longrightarrow> main_part M S = S"
   6.192 +  using split_completion[of S M]
   6.193 +  by (auto simp: split_completion_def split: split_if_asm)
   6.194  
   6.195 -lemma (in completeable_measure_space) null_part_sets[intro, simp]:
   6.196 -  assumes "S \<in> sets M" shows "null_part S \<in> sets M" "\<mu> (null_part S) = 0"
   6.197 +lemma null_part:
   6.198 +  assumes "S \<in> sets (completion M)" shows "\<exists>N. N\<in>null_sets M \<and> null_part M S \<subseteq> N"
   6.199 +  using split_completion[OF assms] by (auto simp: split_completion_def split: split_if_asm)
   6.200 +
   6.201 +lemma null_part_sets[intro, simp]:
   6.202 +  assumes "S \<in> sets M" shows "null_part M S \<in> sets M" "emeasure M (null_part M S) = 0"
   6.203  proof -
   6.204 -  have S: "S \<in> sets completion" using assms by auto
   6.205 -  have "S - main_part S \<in> sets M" using assms by auto
   6.206 +  have S: "S \<in> sets (completion M)" using assms by auto
   6.207 +  have "S - main_part M S \<in> sets M" using assms by auto
   6.208    moreover
   6.209    from main_part_null_part_Un[OF S] main_part_null_part_Int[OF S]
   6.210 -  have "S - main_part S = null_part S" by auto
   6.211 -  ultimately show sets: "null_part S \<in> sets M" by auto
   6.212 +  have "S - main_part M S = null_part M S" by auto
   6.213 +  ultimately show sets: "null_part M S \<in> sets M" by auto
   6.214    from null_part[OF S] guess N ..
   6.215 -  with measure_eq_0[of N "null_part S"] sets
   6.216 -  show "\<mu> (null_part S) = 0" by auto
   6.217 -qed
   6.218 -
   6.219 -lemma (in completeable_measure_space) \<mu>'_set[simp]:
   6.220 -  assumes "S \<in> sets M" shows "\<mu>' S = \<mu> S"
   6.221 -proof -
   6.222 -  have S: "S \<in> sets completion" using assms by auto
   6.223 -  then have "\<mu> S = \<mu> (main_part S \<union> null_part S)" by simp
   6.224 -  also have "\<dots> = \<mu>' S"
   6.225 -    using S assms measure_additive[of "main_part S" "null_part S"]
   6.226 -    by (auto simp: measure_additive)
   6.227 -  finally show ?thesis by simp
   6.228 +  with emeasure_eq_0[of N _ "null_part M S"] sets
   6.229 +  show "emeasure M (null_part M S) = 0" by auto
   6.230  qed
   6.231  
   6.232 -lemma (in completeable_measure_space) sets_completionI_sub:
   6.233 -  assumes N: "N' \<in> null_sets" "N \<subseteq> N'"
   6.234 -  shows "N \<in> sets completion"
   6.235 -  using assms by (intro sets_completionI[of _ "{}" N N']) auto
   6.236 -
   6.237 -lemma (in completeable_measure_space) \<mu>_main_part_UN:
   6.238 +lemma emeasure_main_part_UN:
   6.239    fixes S :: "nat \<Rightarrow> 'a set"
   6.240 -  assumes "range S \<subseteq> sets completion"
   6.241 -  shows "\<mu>' (\<Union>i. (S i)) = \<mu> (\<Union>i. main_part (S i))"
   6.242 +  assumes "range S \<subseteq> sets (completion M)"
   6.243 +  shows "emeasure M (main_part M (\<Union>i. (S i))) = emeasure M (\<Union>i. main_part M (S i))"
   6.244  proof -
   6.245 -  have S: "\<And>i. S i \<in> sets completion" using assms by auto
   6.246 -  then have UN: "(\<Union>i. S i) \<in> sets completion" by auto
   6.247 -  have "\<forall>i. \<exists>N. N \<in> null_sets \<and> null_part (S i) \<subseteq> N"
   6.248 +  have S: "\<And>i. S i \<in> sets (completion M)" using assms by auto
   6.249 +  then have UN: "(\<Union>i. S i) \<in> sets (completion M)" by auto
   6.250 +  have "\<forall>i. \<exists>N. N \<in> null_sets M \<and> null_part M (S i) \<subseteq> N"
   6.251      using null_part[OF S] by auto
   6.252    from choice[OF this] guess N .. note N = this
   6.253 -  then have UN_N: "(\<Union>i. N i) \<in> null_sets" by (intro null_sets_UN) auto
   6.254 -  have "(\<Union>i. S i) \<in> sets completion" using S by auto
   6.255 +  then have UN_N: "(\<Union>i. N i) \<in> null_sets M" by (intro null_sets_UN) auto
   6.256 +  have "(\<Union>i. S i) \<in> sets (completion M)" using S by auto
   6.257    from null_part[OF this] guess N' .. note N' = this
   6.258    let ?N = "(\<Union>i. N i) \<union> N'"
   6.259 -  have null_set: "?N \<in> null_sets" using N' UN_N by (intro nullsets.Un) auto
   6.260 -  have "main_part (\<Union>i. S i) \<union> ?N = (main_part (\<Union>i. S i) \<union> null_part (\<Union>i. S i)) \<union> ?N"
   6.261 +  have null_set: "?N \<in> null_sets M" using N' UN_N by (intro null_sets.Un) auto
   6.262 +  have "main_part M (\<Union>i. S i) \<union> ?N = (main_part M (\<Union>i. S i) \<union> null_part M (\<Union>i. S i)) \<union> ?N"
   6.263      using N' by auto
   6.264 -  also have "\<dots> = (\<Union>i. main_part (S i) \<union> null_part (S i)) \<union> ?N"
   6.265 +  also have "\<dots> = (\<Union>i. main_part M (S i) \<union> null_part M (S i)) \<union> ?N"
   6.266      unfolding main_part_null_part_Un[OF S] main_part_null_part_Un[OF UN] by auto
   6.267 -  also have "\<dots> = (\<Union>i. main_part (S i)) \<union> ?N"
   6.268 +  also have "\<dots> = (\<Union>i. main_part M (S i)) \<union> ?N"
   6.269      using N by auto
   6.270 -  finally have *: "main_part (\<Union>i. S i) \<union> ?N = (\<Union>i. main_part (S i)) \<union> ?N" .
   6.271 -  have "\<mu> (main_part (\<Union>i. S i)) = \<mu> (main_part (\<Union>i. S i) \<union> ?N)"
   6.272 -    using null_set UN by (intro measure_Un_null_set[symmetric]) auto
   6.273 -  also have "\<dots> = \<mu> ((\<Union>i. main_part (S i)) \<union> ?N)"
   6.274 +  finally have *: "main_part M (\<Union>i. S i) \<union> ?N = (\<Union>i. main_part M (S i)) \<union> ?N" .
   6.275 +  have "emeasure M (main_part M (\<Union>i. S i)) = emeasure M (main_part M (\<Union>i. S i) \<union> ?N)"
   6.276 +    using null_set UN by (intro emeasure_Un_null_set[symmetric]) auto
   6.277 +  also have "\<dots> = emeasure M ((\<Union>i. main_part M (S i)) \<union> ?N)"
   6.278      unfolding * ..
   6.279 -  also have "\<dots> = \<mu> (\<Union>i. main_part (S i))"
   6.280 -    using null_set S by (intro measure_Un_null_set) auto
   6.281 +  also have "\<dots> = emeasure M (\<Union>i. main_part M (S i))"
   6.282 +    using null_set S by (intro emeasure_Un_null_set) auto
   6.283    finally show ?thesis .
   6.284  qed
   6.285  
   6.286 -lemma (in completeable_measure_space) \<mu>_main_part_Un:
   6.287 -  assumes S: "S \<in> sets completion" and T: "T \<in> sets completion"
   6.288 -  shows "\<mu>' (S \<union> T) = \<mu> (main_part S \<union> main_part T)"
   6.289 -proof -
   6.290 -  have UN: "(\<Union>i. binary (main_part S) (main_part T) i) = (\<Union>i. main_part (binary S T i))"
   6.291 -    unfolding binary_def by (auto split: split_if_asm)
   6.292 -  show ?thesis
   6.293 -    using \<mu>_main_part_UN[of "binary S T"] assms
   6.294 -    unfolding range_binary_eq Un_range_binary UN by auto
   6.295 +lemma emeasure_completion[simp]:
   6.296 +  assumes S: "S \<in> sets (completion M)" shows "emeasure (completion M) S = emeasure M (main_part M S)"
   6.297 +proof (subst emeasure_measure_of[OF completion_def completion_into_space])
   6.298 +  let ?\<mu> = "emeasure M \<circ> main_part M"
   6.299 +  show "S \<in> sets (completion M)" "?\<mu> S = emeasure M (main_part M S) " using S by simp_all
   6.300 +  show "positive (sets (completion M)) ?\<mu>"
   6.301 +    by (simp add: positive_def emeasure_nonneg)
   6.302 +  show "countably_additive (sets (completion M)) ?\<mu>"
   6.303 +  proof (intro countably_additiveI)
   6.304 +    fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets (completion M)" "disjoint_family A"
   6.305 +    have "disjoint_family (\<lambda>i. main_part M (A i))"
   6.306 +    proof (intro disjoint_family_on_bisimulation[OF A(2)])
   6.307 +      fix n m assume "A n \<inter> A m = {}"
   6.308 +      then have "(main_part M (A n) \<union> null_part M (A n)) \<inter> (main_part M (A m) \<union> null_part M (A m)) = {}"
   6.309 +        using A by (subst (1 2) main_part_null_part_Un) auto
   6.310 +      then show "main_part M (A n) \<inter> main_part M (A m) = {}" by auto
   6.311 +    qed
   6.312 +    then have "(\<Sum>n. emeasure M (main_part M (A n))) = emeasure M (\<Union>i. main_part M (A i))"
   6.313 +      using A by (auto intro!: suminf_emeasure)
   6.314 +    then show "(\<Sum>n. ?\<mu> (A n)) = ?\<mu> (UNION UNIV A)"
   6.315 +      by (simp add: completion_def emeasure_main_part_UN[OF A(1)])
   6.316 +  qed
   6.317  qed
   6.318  
   6.319 -sublocale completeable_measure_space \<subseteq> completion!: measure_space completion
   6.320 -  where "measure completion = \<mu>'"
   6.321 -proof -
   6.322 -  show "measure_space completion"
   6.323 -  proof
   6.324 -    show "positive completion (measure completion)"
   6.325 -      by (auto simp: completion_def positive_def)
   6.326 -  next
   6.327 -    show "countably_additive completion (measure completion)"
   6.328 -    proof (intro countably_additiveI)
   6.329 -      fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets completion" "disjoint_family A"
   6.330 -      have "disjoint_family (\<lambda>i. main_part (A i))"
   6.331 -      proof (intro disjoint_family_on_bisimulation[OF A(2)])
   6.332 -        fix n m assume "A n \<inter> A m = {}"
   6.333 -        then have "(main_part (A n) \<union> null_part (A n)) \<inter> (main_part (A m) \<union> null_part (A m)) = {}"
   6.334 -          using A by (subst (1 2) main_part_null_part_Un) auto
   6.335 -        then show "main_part (A n) \<inter> main_part (A m) = {}" by auto
   6.336 -      qed
   6.337 -      then have "(\<Sum>n. measure completion (A n)) = \<mu> (\<Union>i. main_part (A i))"
   6.338 -        unfolding completion_def using A by (auto intro!: measure_countably_additive)
   6.339 -      then show "(\<Sum>n. measure completion (A n)) = measure completion (UNION UNIV A)"
   6.340 -        by (simp add: completion_def \<mu>_main_part_UN[OF A(1)])
   6.341 -    qed
   6.342 -  qed
   6.343 -  show "measure completion = \<mu>'" unfolding completion_def by simp
   6.344 -qed
   6.345 +lemma emeasure_completion_UN:
   6.346 +  "range S \<subseteq> sets (completion M) \<Longrightarrow>
   6.347 +    emeasure (completion M) (\<Union>i::nat. (S i)) = emeasure M (\<Union>i. main_part M (S i))"
   6.348 +  by (subst emeasure_completion) (auto simp add: emeasure_main_part_UN)
   6.349  
   6.350 -lemma (in completeable_measure_space) completion_ex_simple_function:
   6.351 -  assumes f: "simple_function completion f"
   6.352 -  shows "\<exists>f'. simple_function M f' \<and> (AE x. f x = f' x)"
   6.353 +lemma emeasure_completion_Un:
   6.354 +  assumes S: "S \<in> sets (completion M)" and T: "T \<in> sets (completion M)"
   6.355 +  shows "emeasure (completion M) (S \<union> T) = emeasure M (main_part M S \<union> main_part M T)"
   6.356 +proof (subst emeasure_completion)
   6.357 +  have UN: "(\<Union>i. binary (main_part M S) (main_part M T) i) = (\<Union>i. main_part M (binary S T i))"
   6.358 +    unfolding binary_def by (auto split: split_if_asm)
   6.359 +  show "emeasure M (main_part M (S \<union> T)) = emeasure M (main_part M S \<union> main_part M T)"
   6.360 +    using emeasure_main_part_UN[of "binary S T" M] assms
   6.361 +    unfolding range_binary_eq Un_range_binary UN by auto
   6.362 +qed (auto intro: S T)
   6.363 +
   6.364 +lemma sets_completionI_sub:
   6.365 +  assumes N: "N' \<in> null_sets M" "N \<subseteq> N'"
   6.366 +  shows "N \<in> sets (completion M)"
   6.367 +  using assms by (intro sets_completionI[of _ "{}" N N']) auto
   6.368 +
   6.369 +lemma completion_ex_simple_function:
   6.370 +  assumes f: "simple_function (completion M) f"
   6.371 +  shows "\<exists>f'. simple_function M f' \<and> (AE x in M. f x = f' x)"
   6.372  proof -
   6.373    let ?F = "\<lambda>x. f -` {x} \<inter> space M"
   6.374 -  have F: "\<And>x. ?F x \<in> sets completion" and fin: "finite (f`space M)"
   6.375 -    using completion.simple_functionD[OF f]
   6.376 -      completion.simple_functionD[OF f] by simp_all
   6.377 -  have "\<forall>x. \<exists>N. N \<in> null_sets \<and> null_part (?F x) \<subseteq> N"
   6.378 +  have F: "\<And>x. ?F x \<in> sets (completion M)" and fin: "finite (f`space M)"
   6.379 +    using simple_functionD[OF f] simple_functionD[OF f] by simp_all
   6.380 +  have "\<forall>x. \<exists>N. N \<in> null_sets M \<and> null_part M (?F x) \<subseteq> N"
   6.381      using F null_part by auto
   6.382    from choice[OF this] obtain N where
   6.383 -    N: "\<And>x. null_part (?F x) \<subseteq> N x" "\<And>x. N x \<in> null_sets" by auto
   6.384 +    N: "\<And>x. null_part M (?F x) \<subseteq> N x" "\<And>x. N x \<in> null_sets M" by auto
   6.385    let ?N = "\<Union>x\<in>f`space M. N x"
   6.386    let ?f' = "\<lambda>x. if x \<in> ?N then undefined else f x"
   6.387 -  have sets: "?N \<in> null_sets" using N fin by (intro nullsets.finite_UN) auto
   6.388 +  have sets: "?N \<in> null_sets M" using N fin by (intro null_sets.finite_UN) auto
   6.389    show ?thesis unfolding simple_function_def
   6.390    proof (safe intro!: exI[of _ ?f'])
   6.391      have "?f' ` space M \<subseteq> f`space M \<union> {undefined}" by auto
   6.392 -    from finite_subset[OF this] completion.simple_functionD(1)[OF f]
   6.393 +    from finite_subset[OF this] simple_functionD(1)[OF f]
   6.394      show "finite (?f' ` space M)" by auto
   6.395    next
   6.396      fix x assume "x \<in> space M"
   6.397 @@ -225,13 +238,13 @@
   6.398        (if x \<in> ?N then ?F undefined \<union> ?N
   6.399         else if f x = undefined then ?F (f x) \<union> ?N
   6.400         else ?F (f x) - ?N)"
   6.401 -      using N(2) sets_into_space by (auto split: split_if_asm)
   6.402 +      using N(2) sets_into_space by (auto split: split_if_asm simp: null_sets_def)
   6.403      moreover { fix y have "?F y \<union> ?N \<in> sets M"
   6.404        proof cases
   6.405          assume y: "y \<in> f`space M"
   6.406 -        have "?F y \<union> ?N = (main_part (?F y) \<union> null_part (?F y)) \<union> ?N"
   6.407 +        have "?F y \<union> ?N = (main_part M (?F y) \<union> null_part M (?F y)) \<union> ?N"
   6.408            using main_part_null_part_Un[OF F] by auto
   6.409 -        also have "\<dots> = main_part (?F y) \<union> ?N"
   6.410 +        also have "\<dots> = main_part M (?F y) \<union> ?N"
   6.411            using y N by auto
   6.412          finally show ?thesis
   6.413            using F sets by auto
   6.414 @@ -240,34 +253,34 @@
   6.415          then show ?thesis using sets by auto
   6.416        qed }
   6.417      moreover {
   6.418 -      have "?F (f x) - ?N = main_part (?F (f x)) \<union> null_part (?F (f x)) - ?N"
   6.419 +      have "?F (f x) - ?N = main_part M (?F (f x)) \<union> null_part M (?F (f x)) - ?N"
   6.420          using main_part_null_part_Un[OF F] by auto
   6.421 -      also have "\<dots> = main_part (?F (f x)) - ?N"
   6.422 +      also have "\<dots> = main_part M (?F (f x)) - ?N"
   6.423          using N `x \<in> space M` by auto
   6.424        finally have "?F (f x) - ?N \<in> sets M"
   6.425          using F sets by auto }
   6.426      ultimately show "?f' -` {?f' x} \<inter> space M \<in> sets M" by auto
   6.427    next
   6.428 -    show "AE x. f x = ?f' x"
   6.429 +    show "AE x in M. f x = ?f' x"
   6.430        by (rule AE_I', rule sets) auto
   6.431    qed
   6.432  qed
   6.433  
   6.434 -lemma (in completeable_measure_space) completion_ex_borel_measurable_pos:
   6.435 +lemma completion_ex_borel_measurable_pos:
   6.436    fixes g :: "'a \<Rightarrow> ereal"
   6.437 -  assumes g: "g \<in> borel_measurable completion" and "\<And>x. 0 \<le> g x"
   6.438 -  shows "\<exists>g'\<in>borel_measurable M. (AE x. g x = g' x)"
   6.439 +  assumes g: "g \<in> borel_measurable (completion M)" and "\<And>x. 0 \<le> g x"
   6.440 +  shows "\<exists>g'\<in>borel_measurable M. (AE x in M. g x = g' x)"
   6.441  proof -
   6.442 -  from g[THEN completion.borel_measurable_implies_simple_function_sequence'] guess f . note f = this
   6.443 +  from g[THEN borel_measurable_implies_simple_function_sequence'] guess f . note f = this
   6.444    from this(1)[THEN completion_ex_simple_function]
   6.445 -  have "\<forall>i. \<exists>f'. simple_function M f' \<and> (AE x. f i x = f' x)" ..
   6.446 +  have "\<forall>i. \<exists>f'. simple_function M f' \<and> (AE x in M. f i x = f' x)" ..
   6.447    from this[THEN choice] obtain f' where
   6.448      sf: "\<And>i. simple_function M (f' i)" and
   6.449 -    AE: "\<forall>i. AE x. f i x = f' i x" by auto
   6.450 +    AE: "\<forall>i. AE x in M. f i x = f' i x" by auto
   6.451    show ?thesis
   6.452    proof (intro bexI)
   6.453      from AE[unfolded AE_all_countable[symmetric]]
   6.454 -    show "AE x. g x = (SUP i. f' i x)" (is "AE x. g x = ?f x")
   6.455 +    show "AE x in M. g x = (SUP i. f' i x)" (is "AE x in M. g x = ?f x")
   6.456      proof (elim AE_mp, safe intro!: AE_I2)
   6.457        fix x assume eq: "\<forall>i. f i x = f' i x"
   6.458        moreover have "g x = (SUP i. f i x)"
   6.459 @@ -279,20 +292,20 @@
   6.460    qed
   6.461  qed
   6.462  
   6.463 -lemma (in completeable_measure_space) completion_ex_borel_measurable:
   6.464 +lemma completion_ex_borel_measurable:
   6.465    fixes g :: "'a \<Rightarrow> ereal"
   6.466 -  assumes g: "g \<in> borel_measurable completion"
   6.467 -  shows "\<exists>g'\<in>borel_measurable M. (AE x. g x = g' x)"
   6.468 +  assumes g: "g \<in> borel_measurable (completion M)"
   6.469 +  shows "\<exists>g'\<in>borel_measurable M. (AE x in M. g x = g' x)"
   6.470  proof -
   6.471 -  have "(\<lambda>x. max 0 (g x)) \<in> borel_measurable completion" "\<And>x. 0 \<le> max 0 (g x)" using g by auto
   6.472 +  have "(\<lambda>x. max 0 (g x)) \<in> borel_measurable (completion M)" "\<And>x. 0 \<le> max 0 (g x)" using g by auto
   6.473    from completion_ex_borel_measurable_pos[OF this] guess g_pos ..
   6.474    moreover
   6.475 -  have "(\<lambda>x. max 0 (- g x)) \<in> borel_measurable completion" "\<And>x. 0 \<le> max 0 (- g x)" using g by auto
   6.476 +  have "(\<lambda>x. max 0 (- g x)) \<in> borel_measurable (completion M)" "\<And>x. 0 \<le> max 0 (- g x)" using g by auto
   6.477    from completion_ex_borel_measurable_pos[OF this] guess g_neg ..
   6.478    ultimately
   6.479    show ?thesis
   6.480    proof (safe intro!: bexI[of _ "\<lambda>x. g_pos x - g_neg x"])
   6.481 -    show "AE x. max 0 (- g x) = g_neg x \<longrightarrow> max 0 (g x) = g_pos x \<longrightarrow> g x = g_pos x - g_neg x"
   6.482 +    show "AE x in M. max 0 (- g x) = g_neg x \<longrightarrow> max 0 (g x) = g_pos x \<longrightarrow> g x = g_pos x - g_neg x"
   6.483      proof (intro AE_I2 impI)
   6.484        fix x assume g: "max 0 (- g x) = g_neg x" "max 0 (g x) = g_pos x"
   6.485        show "g x = g_pos x - g_neg x" unfolding g[symmetric]
     7.1 --- a/src/HOL/Probability/Conditional_Probability.thy	Mon Apr 23 12:23:23 2012 +0100
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,161 +0,0 @@
     7.4 -(*  Title:      HOL/Probability/Conditional_Probability.thy
     7.5 -    Author:     Johannes Hölzl, TU München
     7.6 -*)
     7.7 -
     7.8 -header {*Conditional probability*}
     7.9 -
    7.10 -theory Conditional_Probability
    7.11 -imports Probability_Measure Radon_Nikodym
    7.12 -begin
    7.13 -
    7.14 -section "Conditional Expectation and Probability"
    7.15 -
    7.16 -definition (in prob_space)
    7.17 -  "conditional_expectation N X = (SOME Y. Y\<in>borel_measurable N \<and> (\<forall>x. 0 \<le> Y x)
    7.18 -    \<and> (\<forall>C\<in>sets N. (\<integral>\<^isup>+x. Y x * indicator C x\<partial>M) = (\<integral>\<^isup>+x. X x * indicator C x\<partial>M)))"
    7.19 -
    7.20 -lemma (in prob_space) conditional_expectation_exists:
    7.21 -  fixes X :: "'a \<Rightarrow> ereal" and N :: "('a, 'b) measure_space_scheme"
    7.22 -  assumes borel: "X \<in> borel_measurable M" "AE x. 0 \<le> X x"
    7.23 -  and N: "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M" "\<And>A. measure N A = \<mu> A"
    7.24 -  shows "\<exists>Y\<in>borel_measurable N. (\<forall>x. 0 \<le> Y x) \<and> (\<forall>C\<in>sets N.
    7.25 -      (\<integral>\<^isup>+x. Y x * indicator C x \<partial>M) = (\<integral>\<^isup>+x. X x * indicator C x \<partial>M))"
    7.26 -proof -
    7.27 -  note N(4)[simp]
    7.28 -  interpret P: prob_space N
    7.29 -    using prob_space_subalgebra[OF N] .
    7.30 -
    7.31 -  let ?f = "\<lambda>A x. X x * indicator A x"
    7.32 -  let ?Q = "\<lambda>A. integral\<^isup>P M (?f A)"
    7.33 -
    7.34 -  from measure_space_density[OF borel]
    7.35 -  have Q: "measure_space (N\<lparr> measure := ?Q \<rparr>)"
    7.36 -    apply (rule measure_space.measure_space_subalgebra[of "M\<lparr> measure := ?Q \<rparr>"])
    7.37 -    using N by (auto intro!: P.sigma_algebra_cong)
    7.38 -  then interpret Q: measure_space "N\<lparr> measure := ?Q \<rparr>" .
    7.39 -
    7.40 -  have "P.absolutely_continuous ?Q"
    7.41 -    unfolding P.absolutely_continuous_def
    7.42 -  proof safe
    7.43 -    fix A assume "A \<in> sets N" "P.\<mu> A = 0"
    7.44 -    then have f_borel: "?f A \<in> borel_measurable M" "AE x. x \<notin> A"
    7.45 -      using borel N by (auto intro!: borel_measurable_indicator AE_not_in)
    7.46 -    then show "?Q A = 0"
    7.47 -      by (auto simp add: positive_integral_0_iff_AE)
    7.48 -  qed
    7.49 -  from P.Radon_Nikodym[OF Q this]
    7.50 -  obtain Y where Y: "Y \<in> borel_measurable N" "\<And>x. 0 \<le> Y x"
    7.51 -    "\<And>A. A \<in> sets N \<Longrightarrow> ?Q A =(\<integral>\<^isup>+x. Y x * indicator A x \<partial>N)"
    7.52 -    by blast
    7.53 -  with N(2) show ?thesis
    7.54 -    by (auto intro!: bexI[OF _ Y(1)] simp: positive_integral_subalgebra[OF _ _ N(2,3,4,1)])
    7.55 -qed
    7.56 -
    7.57 -lemma (in prob_space)
    7.58 -  fixes X :: "'a \<Rightarrow> ereal" and N :: "('a, 'b) measure_space_scheme"
    7.59 -  assumes borel: "X \<in> borel_measurable M" "AE x. 0 \<le> X x"
    7.60 -  and N: "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M" "\<And>A. measure N A = \<mu> A"
    7.61 -  shows borel_measurable_conditional_expectation:
    7.62 -    "conditional_expectation N X \<in> borel_measurable N"
    7.63 -  and conditional_expectation: "\<And>C. C \<in> sets N \<Longrightarrow>
    7.64 -      (\<integral>\<^isup>+x. conditional_expectation N X x * indicator C x \<partial>M) =
    7.65 -      (\<integral>\<^isup>+x. X x * indicator C x \<partial>M)"
    7.66 -   (is "\<And>C. C \<in> sets N \<Longrightarrow> ?eq C")
    7.67 -proof -
    7.68 -  note CE = conditional_expectation_exists[OF assms, unfolded Bex_def]
    7.69 -  then show "conditional_expectation N X \<in> borel_measurable N"
    7.70 -    unfolding conditional_expectation_def by (rule someI2_ex) blast
    7.71 -
    7.72 -  from CE show "\<And>C. C \<in> sets N \<Longrightarrow> ?eq C"
    7.73 -    unfolding conditional_expectation_def by (rule someI2_ex) blast
    7.74 -qed
    7.75 -
    7.76 -lemma (in sigma_algebra) factorize_measurable_function_pos:
    7.77 -  fixes Z :: "'a \<Rightarrow> ereal" and Y :: "'a \<Rightarrow> 'c"
    7.78 -  assumes "sigma_algebra M'" and "Y \<in> measurable M M'" "Z \<in> borel_measurable M"
    7.79 -  assumes Z: "Z \<in> borel_measurable (sigma_algebra.vimage_algebra M' (space M) Y)"
    7.80 -  shows "\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. max 0 (Z x) = g (Y x)"
    7.81 -proof -
    7.82 -  interpret M': sigma_algebra M' by fact
    7.83 -  have Y: "Y \<in> space M \<rightarrow> space M'" using assms unfolding measurable_def by auto
    7.84 -  from M'.sigma_algebra_vimage[OF this]
    7.85 -  interpret va: sigma_algebra "M'.vimage_algebra (space M) Y" .
    7.86 -
    7.87 -  from va.borel_measurable_implies_simple_function_sequence'[OF Z] guess f . note f = this
    7.88 -
    7.89 -  have "\<forall>i. \<exists>g. simple_function M' g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
    7.90 -  proof
    7.91 -    fix i
    7.92 -    from f(1)[of i] have "finite (f i`space M)" and B_ex:
    7.93 -      "\<forall>z\<in>(f i)`space M. \<exists>B. B \<in> sets M' \<and> (f i) -` {z} \<inter> space M = Y -` B \<inter> space M"
    7.94 -      unfolding simple_function_def by auto
    7.95 -    from B_ex[THEN bchoice] guess B .. note B = this
    7.96 -
    7.97 -    let ?g = "\<lambda>x. \<Sum>z\<in>f i`space M. z * indicator (B z) x"
    7.98 -
    7.99 -    show "\<exists>g. simple_function M' g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
   7.100 -    proof (intro exI[of _ ?g] conjI ballI)
   7.101 -      show "simple_function M' ?g" using B by auto
   7.102 -
   7.103 -      fix x assume "x \<in> space M"
   7.104 -      then have "\<And>z. z \<in> f i`space M \<Longrightarrow> indicator (B z) (Y x) = (indicator (f i -` {z} \<inter> space M) x::ereal)"
   7.105 -        unfolding indicator_def using B by auto
   7.106 -      then show "f i x = ?g (Y x)" using `x \<in> space M` f(1)[of i]
   7.107 -        by (subst va.simple_function_indicator_representation) auto
   7.108 -    qed
   7.109 -  qed
   7.110 -  from choice[OF this] guess g .. note g = this
   7.111 -
   7.112 -  show ?thesis
   7.113 -  proof (intro ballI bexI)
   7.114 -    show "(\<lambda>x. SUP i. g i x) \<in> borel_measurable M'"
   7.115 -      using g by (auto intro: M'.borel_measurable_simple_function)
   7.116 -    fix x assume "x \<in> space M"
   7.117 -    have "max 0 (Z x) = (SUP i. f i x)" using f by simp
   7.118 -    also have "\<dots> = (SUP i. g i (Y x))"
   7.119 -      using g `x \<in> space M` by simp
   7.120 -    finally show "max 0 (Z x) = (SUP i. g i (Y x))" .
   7.121 -  qed
   7.122 -qed
   7.123 -
   7.124 -lemma (in sigma_algebra) factorize_measurable_function:
   7.125 -  fixes Z :: "'a \<Rightarrow> ereal" and Y :: "'a \<Rightarrow> 'c"
   7.126 -  assumes "sigma_algebra M'" and "Y \<in> measurable M M'" "Z \<in> borel_measurable M"
   7.127 -  shows "Z \<in> borel_measurable (sigma_algebra.vimage_algebra M' (space M) Y)
   7.128 -    \<longleftrightarrow> (\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x))"
   7.129 -proof safe
   7.130 -  interpret M': sigma_algebra M' by fact
   7.131 -  have Y: "Y \<in> space M \<rightarrow> space M'" using assms unfolding measurable_def by auto
   7.132 -  from M'.sigma_algebra_vimage[OF this]
   7.133 -  interpret va: sigma_algebra "M'.vimage_algebra (space M) Y" .
   7.134 -
   7.135 -  { fix g :: "'c \<Rightarrow> ereal" assume "g \<in> borel_measurable M'"
   7.136 -    with M'.measurable_vimage_algebra[OF Y]
   7.137 -    have "g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
   7.138 -      by (rule measurable_comp)
   7.139 -    moreover assume "\<forall>x\<in>space M. Z x = g (Y x)"
   7.140 -    then have "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y) \<longleftrightarrow>
   7.141 -       g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
   7.142 -       by (auto intro!: measurable_cong)
   7.143 -    ultimately show "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
   7.144 -      by simp }
   7.145 -
   7.146 -  assume Z: "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
   7.147 -  with assms have "(\<lambda>x. - Z x) \<in> borel_measurable M"
   7.148 -    "(\<lambda>x. - Z x) \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
   7.149 -    by auto
   7.150 -  from factorize_measurable_function_pos[OF assms(1,2) this] guess n .. note n = this
   7.151 -  from factorize_measurable_function_pos[OF assms Z] guess p .. note p = this
   7.152 -  let ?g = "\<lambda>x. p x - n x"
   7.153 -  show "\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x)"
   7.154 -  proof (intro bexI ballI)
   7.155 -    show "?g \<in> borel_measurable M'" using p n by auto
   7.156 -    fix x assume "x \<in> space M"
   7.157 -    then have "p (Y x) = max 0 (Z x)" "n (Y x) = max 0 (- Z x)"
   7.158 -      using p n by auto
   7.159 -    then show "Z x = ?g (Y x)"
   7.160 -      by (auto split: split_max)
   7.161 -  qed
   7.162 -qed
   7.163 -
   7.164 -end
   7.165 \ No newline at end of file
     8.1 --- a/src/HOL/Probability/Finite_Product_Measure.thy	Mon Apr 23 12:23:23 2012 +0100
     8.2 +++ b/src/HOL/Probability/Finite_Product_Measure.thy	Mon Apr 23 12:14:35 2012 +0200
     8.3 @@ -8,6 +8,9 @@
     8.4  imports Binary_Product_Measure
     8.5  begin
     8.6  
     8.7 +lemma split_const: "(\<lambda>(i, j). c) = (\<lambda>_. c)"
     8.8 +  by auto
     8.9 +
    8.10  lemma Pi_iff: "f \<in> Pi I X \<longleftrightarrow> (\<forall>i\<in>I. f i \<in> X i)"
    8.11    unfolding Pi_def by auto
    8.12  
    8.13 @@ -34,9 +37,6 @@
    8.14  notation (xsymbols)
    8.15    funcset_extensional  (infixr "\<rightarrow>\<^isub>E" 60)
    8.16  
    8.17 -lemma extensional_empty[simp]: "extensional {} = {\<lambda>x. undefined}"
    8.18 -  by safe (auto simp add: extensional_def fun_eq_iff)
    8.19 -
    8.20  lemma extensional_insert[intro, simp]:
    8.21    assumes "a \<in> extensional (insert i I)"
    8.22    shows "a(i := b) \<in> extensional (insert i I)"
    8.23 @@ -86,7 +86,7 @@
    8.24    "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    8.25    "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
    8.26    "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    8.27 -  by (auto simp: restrict_def intro!: ext)
    8.28 +  by (auto simp: restrict_def)
    8.29  
    8.30  lemma extensional_insert_undefined[intro, simp]:
    8.31    assumes "a \<in> extensional (insert i I)"
    8.32 @@ -130,16 +130,16 @@
    8.33    using assms by (auto simp: restrict_Pi_cancel)
    8.34  
    8.35  lemma restrict_fupd[simp]: "i \<notin> I \<Longrightarrow> restrict (f (i := x)) I = restrict f I"
    8.36 -  by (auto simp: restrict_def intro!: ext)
    8.37 +  by (auto simp: restrict_def)
    8.38  
    8.39  lemma merge_restrict[simp]:
    8.40    "merge I (restrict x I) J y = merge I x J y"
    8.41    "merge I x J (restrict y J) = merge I x J y"
    8.42 -  unfolding merge_def by (auto intro!: ext)
    8.43 +  unfolding merge_def by auto
    8.44  
    8.45  lemma merge_x_x_eq_restrict[simp]:
    8.46    "merge I x J x = restrict x (I \<union> J)"
    8.47 -  unfolding merge_def by (auto intro!: ext)
    8.48 +  unfolding merge_def by auto
    8.49  
    8.50  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"
    8.51    apply auto
    8.52 @@ -233,339 +233,355 @@
    8.53  
    8.54  section "Products"
    8.55  
    8.56 -locale product_sigma_algebra =
    8.57 -  fixes M :: "'i \<Rightarrow> ('a, 'b) measure_space_scheme"
    8.58 -  assumes sigma_algebras: "\<And>i. sigma_algebra (M i)"
    8.59 +definition prod_emb where
    8.60 +  "prod_emb I M K X = (\<lambda>x. restrict x K) -` X \<inter> (PIE i:I. space (M i))"
    8.61 +
    8.62 +lemma prod_emb_iff: 
    8.63 +  "f \<in> prod_emb I M K X \<longleftrightarrow> f \<in> extensional I \<and> (restrict f K \<in> X) \<and> (\<forall>i\<in>I. f i \<in> space (M i))"
    8.64 +  unfolding prod_emb_def by auto
    8.65  
    8.66 -locale finite_product_sigma_algebra = product_sigma_algebra M
    8.67 -  for M :: "'i \<Rightarrow> ('a, 'b) measure_space_scheme" +
    8.68 -  fixes I :: "'i set"
    8.69 -  assumes finite_index[simp, intro]: "finite I"
    8.70 +lemma
    8.71 +  shows prod_emb_empty[simp]: "prod_emb M L K {} = {}"
    8.72 +    and prod_emb_Un[simp]: "prod_emb M L K (A \<union> B) = prod_emb M L K A \<union> prod_emb M L K B"
    8.73 +    and prod_emb_Int: "prod_emb M L K (A \<inter> B) = prod_emb M L K A \<inter> prod_emb M L K B"
    8.74 +    and prod_emb_UN[simp]: "prod_emb M L K (\<Union>i\<in>I. F i) = (\<Union>i\<in>I. prod_emb M L K (F i))"
    8.75 +    and prod_emb_INT[simp]: "I \<noteq> {} \<Longrightarrow> prod_emb M L K (\<Inter>i\<in>I. F i) = (\<Inter>i\<in>I. prod_emb M L K (F i))"
    8.76 +    and prod_emb_Diff[simp]: "prod_emb M L K (A - B) = prod_emb M L K A - prod_emb M L K B"
    8.77 +  by (auto simp: prod_emb_def)
    8.78  
    8.79 -definition
    8.80 -  "product_algebra_generator I M = \<lparr> space = (\<Pi>\<^isub>E i \<in> I. space (M i)),
    8.81 -    sets = Pi\<^isub>E I ` (\<Pi> i \<in> I. sets (M i)),
    8.82 -    measure = \<lambda>A. (\<Prod>i\<in>I. measure (M i) ((SOME F. A = Pi\<^isub>E I F) i)) \<rparr>"
    8.83 +lemma prod_emb_PiE: "J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)) \<Longrightarrow>
    8.84 +    prod_emb I M J (\<Pi>\<^isub>E i\<in>J. E i) = (\<Pi>\<^isub>E i\<in>I. if i \<in> J then E i else space (M i))"
    8.85 +  by (force simp: prod_emb_def Pi_iff split_if_mem2)
    8.86 +
    8.87 +lemma prod_emb_PiE_same_index[simp]: "(\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> space (M i)) \<Longrightarrow> prod_emb I M I (Pi\<^isub>E I E) = Pi\<^isub>E I E"
    8.88 +  by (auto simp: prod_emb_def Pi_iff)
    8.89  
    8.90 -definition product_algebra_def:
    8.91 -  "Pi\<^isub>M I M = sigma (product_algebra_generator I M)
    8.92 -    \<lparr> measure := (SOME \<mu>. sigma_finite_measure (sigma (product_algebra_generator I M) \<lparr> measure := \<mu> \<rparr>) \<and>
    8.93 -      (\<forall>A\<in>\<Pi> i\<in>I. sets (M i). \<mu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. measure (M i) (A i))))\<rparr>"
    8.94 +definition PiM :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) measure" where
    8.95 +  "PiM I M = extend_measure (\<Pi>\<^isub>E i\<in>I. space (M i))
    8.96 +    {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}
    8.97 +    (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^isub>E j\<in>J. X j))
    8.98 +    (\<lambda>(J, X). \<Prod>j\<in>J \<union> {i\<in>I. emeasure (M i) (space (M i)) \<noteq> 1}. if j \<in> J then emeasure (M j) (X j) else emeasure (M j) (space (M j)))"
    8.99 +
   8.100 +definition prod_algebra :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) set set" where
   8.101 +  "prod_algebra I M = (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^isub>E j\<in>J. X j)) `
   8.102 +    {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}"
   8.103 +
   8.104 +abbreviation
   8.105 +  "Pi\<^isub>M I M \<equiv> PiM I M"
   8.106  
   8.107  syntax
   8.108 -  "_PiM"  :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
   8.109 -              ('i => 'a, 'b) measure_space_scheme"  ("(3PIM _:_./ _)" 10)
   8.110 +  "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3PIM _:_./ _)" 10)
   8.111  
   8.112  syntax (xsymbols)
   8.113 -  "_PiM" :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
   8.114 -             ('i => 'a, 'b) measure_space_scheme"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"   10)
   8.115 +  "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"  10)
   8.116  
   8.117  syntax (HTML output)
   8.118 -  "_PiM" :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
   8.119 -             ('i => 'a, 'b) measure_space_scheme"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"   10)
   8.120 +  "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"  10)
   8.121  
   8.122  translations
   8.123 -  "PIM x:I. M" == "CONST Pi\<^isub>M I (%x. M)"
   8.124 -
   8.125 -abbreviation (in finite_product_sigma_algebra) "G \<equiv> product_algebra_generator I M"
   8.126 -abbreviation (in finite_product_sigma_algebra) "P \<equiv> Pi\<^isub>M I M"
   8.127 -
   8.128 -sublocale product_sigma_algebra \<subseteq> M: sigma_algebra "M i" for i by (rule sigma_algebras)
   8.129 -
   8.130 -lemma sigma_into_space:
   8.131 -  assumes "sets M \<subseteq> Pow (space M)"
   8.132 -  shows "sets (sigma M) \<subseteq> Pow (space M)"
   8.133 -  using sigma_sets_into_sp[OF assms] unfolding sigma_def by auto
   8.134 +  "PIM x:I. M" == "CONST PiM I (%x. M)"
   8.135  
   8.136 -lemma (in product_sigma_algebra) product_algebra_generator_into_space:
   8.137 -  "sets (product_algebra_generator I M) \<subseteq> Pow (space (product_algebra_generator I M))"
   8.138 -  using M.sets_into_space unfolding product_algebra_generator_def
   8.139 -  by auto blast
   8.140 -
   8.141 -lemma (in product_sigma_algebra) product_algebra_into_space:
   8.142 -  "sets (Pi\<^isub>M I M) \<subseteq> Pow (space (Pi\<^isub>M I M))"
   8.143 -  using product_algebra_generator_into_space
   8.144 -  by (auto intro!: sigma_into_space simp add: product_algebra_def)
   8.145 -
   8.146 -lemma (in product_sigma_algebra) sigma_algebra_product_algebra: "sigma_algebra (Pi\<^isub>M I M)"
   8.147 -  using product_algebra_generator_into_space unfolding product_algebra_def
   8.148 -  by (rule sigma_algebra.sigma_algebra_cong[OF sigma_algebra_sigma]) simp_all
   8.149 -
   8.150 -sublocale finite_product_sigma_algebra \<subseteq> sigma_algebra P
   8.151 -  using sigma_algebra_product_algebra .
   8.152 +lemma prod_algebra_sets_into_space:
   8.153 +  "prod_algebra I M \<subseteq> Pow (\<Pi>\<^isub>E i\<in>I. space (M i))"
   8.154 +  using assms by (auto simp: prod_emb_def prod_algebra_def)
   8.155  
   8.156 -lemma product_algebraE:
   8.157 -  assumes "A \<in> sets (product_algebra_generator I M)"
   8.158 -  obtains E where "A = Pi\<^isub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
   8.159 -  using assms unfolding product_algebra_generator_def by auto
   8.160 -
   8.161 -lemma product_algebra_generatorI[intro]:
   8.162 -  assumes "E \<in> (\<Pi> i\<in>I. sets (M i))"
   8.163 -  shows "Pi\<^isub>E I E \<in> sets (product_algebra_generator I M)"
   8.164 -  using assms unfolding product_algebra_generator_def by auto
   8.165 -
   8.166 -lemma space_product_algebra_generator[simp]:
   8.167 -  "space (product_algebra_generator I M) = Pi\<^isub>E I (\<lambda>i. space (M i))"
   8.168 -  unfolding product_algebra_generator_def by simp
   8.169 +lemma prod_algebra_eq_finite:
   8.170 +  assumes I: "finite I"
   8.171 +  shows "prod_algebra I M = {(\<Pi>\<^isub>E i\<in>I. X i) |X. X \<in> (\<Pi> j\<in>I. sets (M j))}" (is "?L = ?R")
   8.172 +proof (intro iffI set_eqI)
   8.173 +  fix A assume "A \<in> ?L"
   8.174 +  then obtain J E where J: "J \<noteq> {} \<or> I = {}" "finite J" "J \<subseteq> I" "\<forall>i\<in>J. E i \<in> sets (M i)"
   8.175 +    and A: "A = prod_emb I M J (PIE j:J. E j)"
   8.176 +    by (auto simp: prod_algebra_def)
   8.177 +  let ?A = "\<Pi>\<^isub>E i\<in>I. if i \<in> J then E i else space (M i)"
   8.178 +  have A: "A = ?A"
   8.179 +    unfolding A using J by (intro prod_emb_PiE sets_into_space) auto
   8.180 +  show "A \<in> ?R" unfolding A using J top
   8.181 +    by (intro CollectI exI[of _ "\<lambda>i. if i \<in> J then E i else space (M i)"]) simp
   8.182 +next
   8.183 +  fix A assume "A \<in> ?R"
   8.184 +  then obtain X where "A = (\<Pi>\<^isub>E i\<in>I. X i)" and X: "X \<in> (\<Pi> j\<in>I. sets (M j))" by auto
   8.185 +  then have A: "A = prod_emb I M I (\<Pi>\<^isub>E i\<in>I. X i)"
   8.186 +    using sets_into_space by (force simp: prod_emb_def Pi_iff)
   8.187 +  from X I show "A \<in> ?L" unfolding A
   8.188 +    by (auto simp: prod_algebra_def)
   8.189 +qed
   8.190  
   8.191 -lemma space_product_algebra[simp]:
   8.192 -  "space (Pi\<^isub>M I M) = (\<Pi>\<^isub>E i\<in>I. space (M i))"
   8.193 -  unfolding product_algebra_def product_algebra_generator_def by simp
   8.194 -
   8.195 -lemma sets_product_algebra:
   8.196 -  "sets (Pi\<^isub>M I M) = sets (sigma (product_algebra_generator I M))"
   8.197 -  unfolding product_algebra_def sigma_def by simp
   8.198 +lemma prod_algebraI:
   8.199 +  "finite J \<Longrightarrow> (J \<noteq> {} \<or> I = {}) \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i))
   8.200 +    \<Longrightarrow> prod_emb I M J (PIE j:J. E j) \<in> prod_algebra I M"
   8.201 +  by (auto simp: prod_algebra_def Pi_iff)
   8.202  
   8.203 -lemma product_algebra_generator_sets_into_space:
   8.204 -  assumes "\<And>i. i\<in>I \<Longrightarrow> sets (M i) \<subseteq> Pow (space (M i))"
   8.205 -  shows "sets (product_algebra_generator I M) \<subseteq> Pow (space (product_algebra_generator I M))"
   8.206 -  using assms by (auto simp: product_algebra_generator_def) blast
   8.207 -
   8.208 -lemma (in finite_product_sigma_algebra) in_P[simp, intro]:
   8.209 -  "\<lbrakk> \<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i) \<rbrakk> \<Longrightarrow> Pi\<^isub>E I A \<in> sets P"
   8.210 -  by (auto simp: sets_product_algebra)
   8.211 -
   8.212 -lemma Int_stable_product_algebra_generator:
   8.213 -  "(\<And>i. i \<in> I \<Longrightarrow> Int_stable (M i)) \<Longrightarrow> Int_stable (product_algebra_generator I M)"
   8.214 -  by (auto simp add: product_algebra_generator_def Int_stable_def PiE_Int Pi_iff)
   8.215 +lemma prod_algebraE:
   8.216 +  assumes A: "A \<in> prod_algebra I M"
   8.217 +  obtains J E where "A = prod_emb I M J (PIE j:J. E j)"
   8.218 +    "finite J" "J \<noteq> {} \<or> I = {}" "J \<subseteq> I" "\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i)" 
   8.219 +  using A by (auto simp: prod_algebra_def)
   8.220  
   8.221 -section "Generating set generates also product algebra"
   8.222 +lemma prod_algebraE_all:
   8.223 +  assumes A: "A \<in> prod_algebra I M"
   8.224 +  obtains E where "A = Pi\<^isub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
   8.225 +proof -
   8.226 +  from A obtain E J where A: "A = prod_emb I M J (Pi\<^isub>E J E)"
   8.227 +    and J: "J \<subseteq> I" and E: "E \<in> (\<Pi> i\<in>J. sets (M i))"
   8.228 +    by (auto simp: prod_algebra_def)
   8.229 +  from E have "\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)"
   8.230 +    using sets_into_space by auto
   8.231 +  then have "A = (\<Pi>\<^isub>E i\<in>I. if i\<in>J then E i else space (M i))"
   8.232 +    using A J by (auto simp: prod_emb_PiE)
   8.233 +  moreover then have "(\<lambda>i. if i\<in>J then E i else space (M i)) \<in> (\<Pi> i\<in>I. sets (M i))"
   8.234 +    using top E by auto
   8.235 +  ultimately show ?thesis using that by auto
   8.236 +qed
   8.237  
   8.238 -lemma sigma_product_algebra_sigma_eq:
   8.239 -  assumes "finite I"
   8.240 -  assumes mono: "\<And>i. i \<in> I \<Longrightarrow> incseq (S i)"
   8.241 -  assumes union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (E i)"
   8.242 -  assumes sets_into: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> sets (E i)"
   8.243 -  and E: "\<And>i. sets (E i) \<subseteq> Pow (space (E i))"
   8.244 -  shows "sets (\<Pi>\<^isub>M i\<in>I. sigma (E i)) = sets (\<Pi>\<^isub>M i\<in>I. E i)"
   8.245 -    (is "sets ?S = sets ?E")
   8.246 -proof cases
   8.247 -  assume "I = {}" then show ?thesis
   8.248 -    by (simp add: product_algebra_def product_algebra_generator_def)
   8.249 -next
   8.250 -  assume "I \<noteq> {}"
   8.251 -  interpret E: sigma_algebra "sigma (E i)" for i
   8.252 -    using E by (rule sigma_algebra_sigma)
   8.253 -  have into_space[intro]: "\<And>i x A. A \<in> sets (E i) \<Longrightarrow> x i \<in> A \<Longrightarrow> x i \<in> space (E i)"
   8.254 -    using E by auto
   8.255 -  interpret G: sigma_algebra ?E
   8.256 -    unfolding product_algebra_def product_algebra_generator_def using E
   8.257 -    by (intro sigma_algebra.sigma_algebra_cong[OF sigma_algebra_sigma]) (auto dest: Pi_mem)
   8.258 -  { fix A i assume "i \<in> I" and A: "A \<in> sets (E i)"
   8.259 -    then have "(\<lambda>x. x i) -` A \<inter> space ?E = (\<Pi>\<^isub>E j\<in>I. if j = i then A else \<Union>n. S j n) \<inter> space ?E"
   8.260 -      using mono union unfolding incseq_Suc_iff space_product_algebra
   8.261 -      by (auto dest: Pi_mem)
   8.262 -    also have "\<dots> = (\<Union>n. (\<Pi>\<^isub>E j\<in>I. if j = i then A else S j n))"
   8.263 -      unfolding space_product_algebra
   8.264 -      apply simp
   8.265 -      apply (subst Pi_UN[OF `finite I`])
   8.266 -      using mono[THEN incseqD] apply simp
   8.267 -      apply (simp add: PiE_Int)
   8.268 -      apply (intro PiE_cong)
   8.269 -      using A sets_into by (auto intro!: into_space)
   8.270 -    also have "\<dots> \<in> sets ?E"
   8.271 -      using sets_into `A \<in> sets (E i)`
   8.272 -      unfolding sets_product_algebra sets_sigma
   8.273 -      by (intro sigma_sets.Union)
   8.274 -         (auto simp: image_subset_iff intro!: sigma_sets.Basic)
   8.275 -    finally have "(\<lambda>x. x i) -` A \<inter> space ?E \<in> sets ?E" . }
   8.276 -  then have proj:
   8.277 -    "\<And>i. i\<in>I \<Longrightarrow> (\<lambda>x. x i) \<in> measurable ?E (sigma (E i))"
   8.278 -    using E by (subst G.measurable_iff_sigma)
   8.279 -               (auto simp: sets_product_algebra sets_sigma)
   8.280 -  { fix A assume A: "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (sigma (E i))"
   8.281 -    with proj have basic: "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. x i) -` (A i) \<inter> space ?E \<in> sets ?E"
   8.282 -      unfolding measurable_def by simp
   8.283 -    have "Pi\<^isub>E I A = (\<Inter>i\<in>I. (\<lambda>x. x i) -` (A i) \<inter> space ?E)"
   8.284 -      using A E.sets_into_space `I \<noteq> {}` unfolding product_algebra_def by auto blast
   8.285 -    then have "Pi\<^isub>E I A \<in> sets ?E"
   8.286 -      using G.finite_INT[OF `finite I` `I \<noteq> {}` basic, of "\<lambda>i. i"] by simp }
   8.287 -  then have "sigma_sets (space ?E) (sets (product_algebra_generator I (\<lambda>i. sigma (E i)))) \<subseteq> sets ?E"
   8.288 -    by (intro G.sigma_sets_subset) (auto simp add: product_algebra_generator_def)
   8.289 -  then have subset: "sets ?S \<subseteq> sets ?E"
   8.290 -    by (simp add: sets_sigma sets_product_algebra)
   8.291 -  show "sets ?S = sets ?E"
   8.292 -  proof (intro set_eqI iffI)
   8.293 -    fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
   8.294 -      unfolding sets_sigma sets_product_algebra
   8.295 -    proof induct
   8.296 -      case (Basic A) then show ?case
   8.297 -        by (auto simp: sets_sigma product_algebra_generator_def intro: sigma_sets.Basic)
   8.298 -    qed (auto intro: sigma_sets.intros simp: product_algebra_generator_def)
   8.299 -  next
   8.300 -    fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
   8.301 -  qed
   8.302 +lemma Int_stable_prod_algebra: "Int_stable (prod_algebra I M)"
   8.303 +proof (unfold Int_stable_def, safe)
   8.304 +  fix A assume "A \<in> prod_algebra I M"
   8.305 +  from prod_algebraE[OF this] guess J E . note A = this
   8.306 +  fix B assume "B \<in> prod_algebra I M"
   8.307 +  from prod_algebraE[OF this] guess K F . note B = this
   8.308 +  have "A \<inter> B = prod_emb I M (J \<union> K) (\<Pi>\<^isub>E i\<in>J \<union> K. (if i \<in> J then E i else space (M i)) \<inter> 
   8.309 +      (if i \<in> K then F i else space (M i)))"
   8.310 +    unfolding A B using A(2,3,4) A(5)[THEN sets_into_space] B(2,3,4) B(5)[THEN sets_into_space]
   8.311 +    apply (subst (1 2 3) prod_emb_PiE)
   8.312 +    apply (simp_all add: subset_eq PiE_Int)
   8.313 +    apply blast
   8.314 +    apply (intro PiE_cong)
   8.315 +    apply auto
   8.316 +    done
   8.317 +  also have "\<dots> \<in> prod_algebra I M"
   8.318 +    using A B by (auto intro!: prod_algebraI)
   8.319 +  finally show "A \<inter> B \<in> prod_algebra I M" .
   8.320 +qed
   8.321 +
   8.322 +lemma prod_algebra_mono:
   8.323 +  assumes space: "\<And>i. i \<in> I \<Longrightarrow> space (E i) = space (F i)"
   8.324 +  assumes sets: "\<And>i. i \<in> I \<Longrightarrow> sets (E i) \<subseteq> sets (F i)"
   8.325 +  shows "prod_algebra I E \<subseteq> prod_algebra I F"
   8.326 +proof
   8.327 +  fix A assume "A \<in> prod_algebra I E"
   8.328 +  then obtain J G where J: "J \<noteq> {} \<or> I = {}" "finite J" "J \<subseteq> I"
   8.329 +    and A: "A = prod_emb I E J (\<Pi>\<^isub>E i\<in>J. G i)"
   8.330 +    and G: "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (E i)"
   8.331 +    by (auto simp: prod_algebra_def)
   8.332 +  moreover
   8.333 +  from space have "(\<Pi>\<^isub>E i\<in>I. space (E i)) = (\<Pi>\<^isub>E i\<in>I. space (F i))"
   8.334 +    by (rule PiE_cong)
   8.335 +  with A have "A = prod_emb I F J (\<Pi>\<^isub>E i\<in>J. G i)"
   8.336 +    by (simp add: prod_emb_def)
   8.337 +  moreover
   8.338 +  from sets G J have "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (F i)"
   8.339 +    by auto
   8.340 +  ultimately show "A \<in> prod_algebra I F"
   8.341 +    apply (simp add: prod_algebra_def image_iff)
   8.342 +    apply (intro exI[of _ J] exI[of _ G] conjI)
   8.343 +    apply auto
   8.344 +    done
   8.345  qed
   8.346  
   8.347 -lemma product_algebraI[intro]:
   8.348 -    "E \<in> (\<Pi> i\<in>I. sets (M i)) \<Longrightarrow> Pi\<^isub>E I E \<in> sets (Pi\<^isub>M I M)"
   8.349 -  using assms unfolding product_algebra_def by (auto intro: product_algebra_generatorI)
   8.350 +lemma space_PiM: "space (\<Pi>\<^isub>M i\<in>I. M i) = (\<Pi>\<^isub>E i\<in>I. space (M i))"
   8.351 +  using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro space_extend_measure) simp
   8.352 +
   8.353 +lemma sets_PiM: "sets (\<Pi>\<^isub>M i\<in>I. M i) = sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) (prod_algebra I M)"
   8.354 +  using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro sets_extend_measure) simp
   8.355  
   8.356 -lemma (in product_sigma_algebra) measurable_component_update:
   8.357 -  assumes "x \<in> space (Pi\<^isub>M I M)" and "i \<notin> I"
   8.358 -  shows "(\<lambda>v. x(i := v)) \<in> measurable (M i) (Pi\<^isub>M (insert i I) M)" (is "?f \<in> _")
   8.359 -  unfolding product_algebra_def apply simp
   8.360 -proof (intro measurable_sigma)
   8.361 -  let ?G = "product_algebra_generator (insert i I) M"
   8.362 -  show "sets ?G \<subseteq> Pow (space ?G)" using product_algebra_generator_into_space .
   8.363 -  show "?f \<in> space (M i) \<rightarrow> space ?G"
   8.364 -    using M.sets_into_space assms by auto
   8.365 -  fix A assume "A \<in> sets ?G"
   8.366 -  from product_algebraE[OF this] guess E . note E = this
   8.367 -  then have "?f -` A \<inter> space (M i) = E i \<or> ?f -` A \<inter> space (M i) = {}"
   8.368 -    using M.sets_into_space assms by auto
   8.369 -  then show "?f -` A \<inter> space (M i) \<in> sets (M i)"
   8.370 -    using E by (auto intro!: product_algebraI)
   8.371 +lemma sets_PiM_single: "sets (PiM I M) =
   8.372 +    sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) {{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)}"
   8.373 +    (is "_ = sigma_sets ?\<Omega> ?R")
   8.374 +  unfolding sets_PiM
   8.375 +proof (rule sigma_sets_eqI)
   8.376 +  interpret R: sigma_algebra ?\<Omega> "sigma_sets ?\<Omega> ?R" by (rule sigma_algebra_sigma_sets) auto
   8.377 +  fix A assume "A \<in> prod_algebra I M"
   8.378 +  from prod_algebraE[OF this] guess J X . note X = this
   8.379 +  show "A \<in> sigma_sets ?\<Omega> ?R"
   8.380 +  proof cases
   8.381 +    assume "I = {}"
   8.382 +    with X have "A = {\<lambda>x. undefined}" by (auto simp: prod_emb_def)
   8.383 +    with `I = {}` show ?thesis by (auto intro!: sigma_sets_top)
   8.384 +  next
   8.385 +    assume "I \<noteq> {}"
   8.386 +    with X have "A = (\<Inter>j\<in>J. {f\<in>(\<Pi>\<^isub>E i\<in>I. space (M i)). f j \<in> X j})"
   8.387 +      using sets_into_space[OF X(5)]
   8.388 +      by (auto simp: prod_emb_PiE[OF _ sets_into_space] Pi_iff split: split_if_asm) blast
   8.389 +    also have "\<dots> \<in> sigma_sets ?\<Omega> ?R"
   8.390 +      using X `I \<noteq> {}` by (intro R.finite_INT sigma_sets.Basic) auto
   8.391 +    finally show "A \<in> sigma_sets ?\<Omega> ?R" .
   8.392 +  qed
   8.393 +next
   8.394 +  fix A assume "A \<in> ?R"
   8.395 +  then obtain i B where A: "A = {f\<in>\<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> B}" "i \<in> I" "B \<in> sets (M i)" 
   8.396 +    by auto
   8.397 +  then have "A = prod_emb I M {i} (\<Pi>\<^isub>E i\<in>{i}. B)"
   8.398 +    using sets_into_space[OF A(3)]
   8.399 +    apply (subst prod_emb_PiE)
   8.400 +    apply (auto simp: Pi_iff split: split_if_asm)
   8.401 +    apply blast
   8.402 +    done
   8.403 +  also have "\<dots> \<in> sigma_sets ?\<Omega> (prod_algebra I M)"
   8.404 +    using A by (intro sigma_sets.Basic prod_algebraI) auto
   8.405 +  finally show "A \<in> sigma_sets ?\<Omega> (prod_algebra I M)" .
   8.406 +qed
   8.407 +
   8.408 +lemma sets_PiM_I:
   8.409 +  assumes "finite J" "J \<subseteq> I" "\<forall>i\<in>J. E i \<in> sets (M i)"
   8.410 +  shows "prod_emb I M J (PIE j:J. E j) \<in> sets (PIM i:I. M i)"
   8.411 +proof cases
   8.412 +  assume "J = {}"
   8.413 +  then have "prod_emb I M J (PIE j:J. E j) = (PIE j:I. space (M j))"
   8.414 +    by (auto simp: prod_emb_def)
   8.415 +  then show ?thesis
   8.416 +    by (auto simp add: sets_PiM intro!: sigma_sets_top)
   8.417 +next
   8.418 +  assume "J \<noteq> {}" with assms show ?thesis
   8.419 +    by (auto simp add: sets_PiM prod_algebra_def intro!: sigma_sets.Basic)
   8.420  qed
   8.421  
   8.422 -lemma (in product_sigma_algebra) measurable_add_dim:
   8.423 -  assumes "i \<notin> I"
   8.424 -  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)"
   8.425 -proof -
   8.426 -  let ?f = "(\<lambda>(f, y). f(i := y))" and ?G = "product_algebra_generator (insert i I) M"
   8.427 -  interpret Ii: pair_sigma_algebra "Pi\<^isub>M I M" "M i"
   8.428 -    unfolding pair_sigma_algebra_def
   8.429 -    by (intro sigma_algebra_product_algebra sigma_algebras conjI)
   8.430 -  have "?f \<in> measurable Ii.P (sigma ?G)"
   8.431 -  proof (rule Ii.measurable_sigma)
   8.432 -    show "sets ?G \<subseteq> Pow (space ?G)"
   8.433 -      using product_algebra_generator_into_space .
   8.434 -    show "?f \<in> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) \<rightarrow> space ?G"
   8.435 -      by (auto simp: space_pair_measure)
   8.436 -  next
   8.437 -    fix A assume "A \<in> sets ?G"
   8.438 -    then obtain F where "A = Pi\<^isub>E (insert i I) F"
   8.439 -      and F: "\<And>j. j \<in> I \<Longrightarrow> F j \<in> sets (M j)" "F i \<in> sets (M i)"
   8.440 -      by (auto elim!: product_algebraE)
   8.441 -    then have "?f -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) = Pi\<^isub>E I F \<times> (F i)"
   8.442 -      using sets_into_space `i \<notin> I`
   8.443 -      by (auto simp add: space_pair_measure) blast+
   8.444 -    then show "?f -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) \<in> sets (Pi\<^isub>M I M \<Otimes>\<^isub>M M i)"
   8.445 -      using F by (auto intro!: pair_measureI)
   8.446 -  qed
   8.447 -  then show ?thesis
   8.448 -    by (simp add: product_algebra_def)
   8.449 +lemma measurable_PiM:
   8.450 +  assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
   8.451 +  assumes sets: "\<And>X J. J \<noteq> {} \<or> I = {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)) \<Longrightarrow>
   8.452 +    f -` prod_emb I M J (Pi\<^isub>E J X) \<inter> space N \<in> sets N" 
   8.453 +  shows "f \<in> measurable N (PiM I M)"
   8.454 +  using sets_PiM prod_algebra_sets_into_space space
   8.455 +proof (rule measurable_sigma_sets)
   8.456 +  fix A assume "A \<in> prod_algebra I M"
   8.457 +  from prod_algebraE[OF this] guess J X .
   8.458 +  with sets[of J X] show "f -` A \<inter> space N \<in> sets N" by auto
   8.459 +qed
   8.460 +
   8.461 +lemma measurable_PiM_Collect:
   8.462 +  assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
   8.463 +  assumes sets: "\<And>X J. J \<noteq> {} \<or> I = {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)) \<Longrightarrow>
   8.464 +    {\<omega>\<in>space N. \<forall>i\<in>J. f \<omega> i \<in> X i} \<in> sets N" 
   8.465 +  shows "f \<in> measurable N (PiM I M)"
   8.466 +  using sets_PiM prod_algebra_sets_into_space space
   8.467 +proof (rule measurable_sigma_sets)
   8.468 +  fix A assume "A \<in> prod_algebra I M"
   8.469 +  from prod_algebraE[OF this] guess J X . note X = this
   8.470 +  have "f -` A \<inter> space N = {\<omega> \<in> space N. \<forall>i\<in>J. f \<omega> i \<in> X i}"
   8.471 +    using sets_into_space[OF X(5)] X(2-) space unfolding X(1)
   8.472 +    by (subst prod_emb_PiE) (auto simp: Pi_iff split: split_if_asm)
   8.473 +  also have "\<dots> \<in> sets N" using X(3,2,4,5) by (rule sets)
   8.474 +  finally show "f -` A \<inter> space N \<in> sets N" .
   8.475  qed
   8.476  
   8.477 -lemma (in product_sigma_algebra) measurable_merge:
   8.478 -  assumes [simp]: "I \<inter> J = {}"
   8.479 -  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)"
   8.480 -proof -
   8.481 -  let ?I = "Pi\<^isub>M I M" and ?J = "Pi\<^isub>M J M"
   8.482 -  interpret P: sigma_algebra "?I \<Otimes>\<^isub>M ?J"
   8.483 -    by (intro sigma_algebra_pair_measure product_algebra_into_space)
   8.484 -  let ?f = "\<lambda>(x, y). merge I x J y"
   8.485 -  let ?G = "product_algebra_generator (I \<union> J) M"
   8.486 -  have "?f \<in> measurable (?I \<Otimes>\<^isub>M ?J) (sigma ?G)"
   8.487 -  proof (rule P.measurable_sigma)
   8.488 -    fix A assume "A \<in> sets ?G"
   8.489 -    from product_algebraE[OF this]
   8.490 -    obtain E where E: "A = Pi\<^isub>E (I \<union> J) E" "E \<in> (\<Pi> i\<in>I \<union> J. sets (M i))" .
   8.491 -    then have *: "?f -` A \<inter> space (?I \<Otimes>\<^isub>M ?J) = Pi\<^isub>E I E \<times> Pi\<^isub>E J E"
   8.492 -      using sets_into_space `I \<inter> J = {}`
   8.493 -      by (auto simp add: space_pair_measure) fast+
   8.494 -    then show "?f -` A \<inter> space (?I \<Otimes>\<^isub>M ?J) \<in> sets (?I \<Otimes>\<^isub>M ?J)"
   8.495 -      using E unfolding * by (auto intro!: pair_measureI in_sigma product_algebra_generatorI
   8.496 -        simp: product_algebra_def)
   8.497 -  qed (insert product_algebra_generator_into_space, auto simp: space_pair_measure)
   8.498 -  then show "?f \<in> measurable (?I \<Otimes>\<^isub>M ?J) (Pi\<^isub>M (I \<union> J) M)"
   8.499 -    unfolding product_algebra_def[of "I \<union> J"] by simp
   8.500 -qed
   8.501 +lemma measurable_PiM_single:
   8.502 +  assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
   8.503 +  assumes sets: "\<And>A i. i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> {\<omega> \<in> space N. f \<omega> i \<in> A} \<in> sets N" 
   8.504 +  shows "f \<in> measurable N (PiM I M)"
   8.505 +  using sets_PiM_single
   8.506 +proof (rule measurable_sigma_sets)
   8.507 +  fix A assume "A \<in> {{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)}"
   8.508 +  then obtain B i where "A = {f \<in> \<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> B}" and B: "i \<in> I" "B \<in> sets (M i)"
   8.509 +    by auto
   8.510 +  with space have "f -` A \<inter> space N = {\<omega> \<in> space N. f \<omega> i \<in> B}" by auto
   8.511 +  also have "\<dots> \<in> sets N" using B by (rule sets)
   8.512 +  finally show "f -` A \<inter> space N \<in> sets N" .
   8.513 +qed (auto simp: space)
   8.514  
   8.515 -lemma (in product_sigma_algebra) measurable_component_singleton:
   8.516 +lemma sets_PiM_I_finite[simp, intro]:
   8.517 +  assumes "finite I" and sets: "(\<And>i. i \<in> I \<Longrightarrow> E i \<in> sets (M i))"
   8.518 +  shows "(PIE j:I. E j) \<in> sets (PIM i:I. M i)"
   8.519 +  using sets_PiM_I[of I I E M] sets_into_space[OF sets] `finite I` sets by auto
   8.520 +
   8.521 +lemma measurable_component_update:
   8.522 +  assumes "x \<in> space (Pi\<^isub>M I M)" and "i \<notin> I"
   8.523 +  shows "(\<lambda>v. x(i := v)) \<in> measurable (M i) (Pi\<^isub>M (insert i I) M)" (is "?f \<in> _")
   8.524 +proof (intro measurable_PiM_single)
   8.525 +  fix j A assume "j \<in> insert i I" "A \<in> sets (M j)"
   8.526 +  moreover have "{\<omega> \<in> space (M i). (x(i := \<omega>)) j \<in> A} =
   8.527 +    (if i = j then space (M i) \<inter> A else if x j \<in> A then space (M i) else {})"
   8.528 +    by auto
   8.529 +  ultimately show "{\<omega> \<in> space (M i). (x(i := \<omega>)) j \<in> A} \<in> sets (M i)"
   8.530 +    by auto
   8.531 +qed (insert sets_into_space assms, auto simp: space_PiM)
   8.532 +
   8.533 +lemma measurable_component_singleton:
   8.534    assumes "i \<in> I" shows "(\<lambda>x. x i) \<in> measurable (Pi\<^isub>M I M) (M i)"
   8.535  proof (unfold measurable_def, intro CollectI conjI ballI)
   8.536    fix A assume "A \<in> sets (M i)"
   8.537 -  then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M I M) = (\<Pi>\<^isub>E j\<in>I. if i = j then A else space (M j))"
   8.538 -    using M.sets_into_space `i \<in> I` by (fastforce dest: Pi_mem split: split_if_asm)
   8.539 +  then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M I M) = prod_emb I M {i} (\<Pi>\<^isub>E j\<in>{i}. A)"
   8.540 +    using sets_into_space `i \<in> I`
   8.541 +    by (fastforce dest: Pi_mem simp: prod_emb_def space_PiM split: split_if_asm)
   8.542    then show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M I M) \<in> sets (Pi\<^isub>M I M)"
   8.543 -    using `A \<in> sets (M i)` by (auto intro!: product_algebraI)
   8.544 -qed (insert `i \<in> I`, auto)
   8.545 +    using `A \<in> sets (M i)` `i \<in> I` by (auto intro!: sets_PiM_I)
   8.546 +qed (insert `i \<in> I`, auto simp: space_PiM)
   8.547 +
   8.548 +lemma measurable_add_dim:
   8.549 +  assumes "i \<notin> I"
   8.550 +  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)"
   8.551 +    (is "?f \<in> measurable ?P ?I")
   8.552 +proof (rule measurable_PiM_single)
   8.553 +  fix j A assume j: "j \<in> insert i I" and A: "A \<in> sets (M j)"
   8.554 +  have "{\<omega> \<in> space ?P. (\<lambda>(f, x). fun_upd f i x) \<omega> j \<in> A} =
   8.555 +    (if j = i then space (Pi\<^isub>M I M) \<times> A else ((\<lambda>x. x j) \<circ> fst) -` A \<inter> space ?P)"
   8.556 +    using sets_into_space[OF A] by (auto simp add: space_pair_measure space_PiM)
   8.557 +  also have "\<dots> \<in> sets ?P"
   8.558 +    using A j
   8.559 +    by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
   8.560 +  finally show "{\<omega> \<in> space ?P. prod_case (\<lambda>f. fun_upd f i) \<omega> j \<in> A} \<in> sets ?P" .
   8.561 +qed (auto simp: space_pair_measure space_PiM)
   8.562  
   8.563 -lemma (in sigma_algebra) measurable_restrict:
   8.564 -  assumes I: "finite I"
   8.565 -  assumes "\<And>i. i \<in> I \<Longrightarrow> sets (N i) \<subseteq> Pow (space (N i))"
   8.566 -  assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> measurable M (N i)"
   8.567 -  shows "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> measurable M (Pi\<^isub>M I N)"
   8.568 -  unfolding product_algebra_def
   8.569 -proof (simp, rule measurable_sigma)
   8.570 -  show "sets (product_algebra_generator I N) \<subseteq> Pow (space (product_algebra_generator I N))"
   8.571 -    by (rule product_algebra_generator_sets_into_space) fact
   8.572 -  show "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> space M \<rightarrow> space (product_algebra_generator I N)"
   8.573 -    using X by (auto simp: measurable_def)
   8.574 -  fix E assume "E \<in> sets (product_algebra_generator I N)"
   8.575 -  then obtain F where "E = Pi\<^isub>E I F" and F: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets (N i)"
   8.576 -    by (auto simp: product_algebra_generator_def)
   8.577 -  then have "(\<lambda>x. \<lambda>i\<in>I. X i x) -` E \<inter> space M = (\<Inter>i\<in>I. X i -` F i \<inter> space M) \<inter> space M"
   8.578 -    by (auto simp: Pi_iff)
   8.579 -  also have "\<dots> \<in> sets M"
   8.580 -  proof cases
   8.581 -    assume "I = {}" then show ?thesis by simp
   8.582 -  next
   8.583 -    assume "I \<noteq> {}" with X F I show ?thesis
   8.584 -      by (intro finite_INT measurable_sets Int) auto
   8.585 -  qed
   8.586 -  finally show "(\<lambda>x. \<lambda>i\<in>I. X i x) -` E \<inter> space M \<in> sets M" .
   8.587 -qed
   8.588 +lemma measurable_merge:
   8.589 +  assumes "I \<inter> J = {}"
   8.590 +  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)"
   8.591 +    (is "?f \<in> measurable ?P ?U")
   8.592 +proof (rule measurable_PiM_single)
   8.593 +  fix i A assume A: "A \<in> sets (M i)" "i \<in> I \<union> J"
   8.594 +  then have "{\<omega> \<in> space ?P. prod_case (\<lambda>x. merge I x J) \<omega> i \<in> A} =
   8.595 +    (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)"
   8.596 +    using `I \<inter> J = {}` by auto
   8.597 +  also have "\<dots> \<in> sets ?P"
   8.598 +    using A
   8.599 +    by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
   8.600 +  finally show "{\<omega> \<in> space ?P. prod_case (\<lambda>x. merge I x J) \<omega> i \<in> A} \<in> sets ?P" .
   8.601 +qed (insert assms, auto simp: space_pair_measure space_PiM)
   8.602  
   8.603 -locale product_sigma_finite = product_sigma_algebra M
   8.604 -  for M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme" +
   8.605 +lemma measurable_restrict:
   8.606 +  assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> measurable N (M i)"
   8.607 +  shows "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> measurable N (Pi\<^isub>M I M)"
   8.608 +proof (rule measurable_PiM_single)
   8.609 +  fix A i assume A: "i \<in> I" "A \<in> sets (M i)"
   8.610 +  then have "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} = X i -` A \<inter> space N"
   8.611 +    by auto
   8.612 +  then show "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} \<in> sets N"
   8.613 +    using A X by (auto intro!: measurable_sets)
   8.614 +qed (insert X, auto dest: measurable_space)
   8.615 +
   8.616 +locale product_sigma_finite =
   8.617 +  fixes M :: "'i \<Rightarrow> 'a measure"
   8.618    assumes sigma_finite_measures: "\<And>i. sigma_finite_measure (M i)"
   8.619  
   8.620  sublocale product_sigma_finite \<subseteq> M: sigma_finite_measure "M i" for i
   8.621    by (rule sigma_finite_measures)
   8.622  
   8.623 -locale finite_product_sigma_finite = finite_product_sigma_algebra M I + product_sigma_finite M
   8.624 -  for M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme" and I :: "'i set"
   8.625 -
   8.626 -lemma (in finite_product_sigma_finite) product_algebra_generator_measure:
   8.627 -  assumes "Pi\<^isub>E I F \<in> sets G"
   8.628 -  shows "measure G (Pi\<^isub>E I F) = (\<Prod>i\<in>I. M.\<mu> i (F i))"
   8.629 -proof cases
   8.630 -  assume ne: "\<forall>i\<in>I. F i \<noteq> {}"
   8.631 -  have "\<forall>i\<in>I. (SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') i = F i"
   8.632 -    by (rule someI2[where P="\<lambda>F'. Pi\<^isub>E I F = Pi\<^isub>E I F'"])
   8.633 -       (insert ne, auto simp: Pi_eq_iff)
   8.634 -  then show ?thesis
   8.635 -    unfolding product_algebra_generator_def by simp
   8.636 -next
   8.637 -  assume empty: "\<not> (\<forall>j\<in>I. F j \<noteq> {})"
   8.638 -  then have "(\<Prod>j\<in>I. M.\<mu> j (F j)) = 0"
   8.639 -    by (auto simp: setprod_ereal_0 intro!: bexI)
   8.640 -  moreover
   8.641 -  have "\<exists>j\<in>I. (SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') j = {}"
   8.642 -    by (rule someI2[where P="\<lambda>F'. Pi\<^isub>E I F = Pi\<^isub>E I F'"])
   8.643 -       (insert empty, auto simp: Pi_eq_empty_iff[symmetric])
   8.644 -  then have "(\<Prod>j\<in>I. M.\<mu> j ((SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') j)) = 0"
   8.645 -    by (auto simp: setprod_ereal_0 intro!: bexI)
   8.646 -  ultimately show ?thesis
   8.647 -    unfolding product_algebra_generator_def by simp
   8.648 -qed
   8.649 +locale finite_product_sigma_finite = product_sigma_finite M for M :: "'i \<Rightarrow> 'a measure" +
   8.650 +  fixes I :: "'i set"
   8.651 +  assumes finite_index: "finite I"
   8.652  
   8.653  lemma (in finite_product_sigma_finite) sigma_finite_pairs:
   8.654    "\<exists>F::'i \<Rightarrow> nat \<Rightarrow> 'a set.
   8.655      (\<forall>i\<in>I. range (F i) \<subseteq> sets (M i)) \<and>
   8.656 -    (\<forall>k. \<forall>i\<in>I. \<mu> i (F i k) \<noteq> \<infinity>) \<and> incseq (\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k) \<and>
   8.657 -    (\<Union>k. \<Pi>\<^isub>E i\<in>I. F i k) = space G"
   8.658 +    (\<forall>k. \<forall>i\<in>I. emeasure (M i) (F i k) \<noteq> \<infinity>) \<and> incseq (\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k) \<and>
   8.659 +    (\<Union>k. \<Pi>\<^isub>E i\<in>I. F i k) = space (PiM I M)"
   8.660  proof -
   8.661 -  have "\<forall>i::'i. \<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> sets (M i) \<and> incseq F \<and> (\<Union>i. F i) = space (M i) \<and> (\<forall>k. \<mu> i (F k) \<noteq> \<infinity>)"
   8.662 -    using M.sigma_finite_up by simp
   8.663 +  have "\<forall>i::'i. \<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> sets (M i) \<and> incseq F \<and> (\<Union>i. F i) = space (M i) \<and> (\<forall>k. emeasure (M i) (F k) \<noteq> \<infinity>)"
   8.664 +    using M.sigma_finite_incseq by metis
   8.665    from choice[OF this] guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   8.666 -  then have F: "\<And>i. range (F i) \<subseteq> sets (M i)" "\<And>i. incseq (F i)" "\<And>i. (\<Union>j. F i j) = space (M i)" "\<And>i k. \<mu> i (F i k) \<noteq> \<infinity>"
   8.667 +  then have F: "\<And>i. range (F i) \<subseteq> sets (M i)" "\<And>i. incseq (F i)" "\<And>i. (\<Union>j. F i j) = space (M i)" "\<And>i k. emeasure (M i) (F i k) \<noteq> \<infinity>"
   8.668      by auto
   8.669    let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k"
   8.670 -  note space_product_algebra[simp]
   8.671 +  note space_PiM[simp]
   8.672    show ?thesis
   8.673    proof (intro exI[of _ F] conjI allI incseq_SucI set_eqI iffI ballI)
   8.674      fix i show "range (F i) \<subseteq> sets (M i)" by fact
   8.675    next
   8.676 -    fix i k show "\<mu> i (F i k) \<noteq> \<infinity>" by fact
   8.677 +    fix i k show "emeasure (M i) (F i k) \<noteq> \<infinity>" by fact
   8.678    next
   8.679 -    fix A assume "A \<in> (\<Union>i. ?F i)" then show "A \<in> space G"
   8.680 -      using `\<And>i. range (F i) \<subseteq> sets (M i)` M.sets_into_space
   8.681 -      by (force simp: image_subset_iff)
   8.682 +    fix A assume "A \<in> (\<Union>i. ?F i)" then show "A \<in> space (PiM I M)"
   8.683 +      using `\<And>i. range (F i) \<subseteq> sets (M i)` sets_into_space
   8.684 +      by auto blast
   8.685    next
   8.686 -    fix f assume "f \<in> space G"
   8.687 +    fix f assume "f \<in> space (PiM I M)"
   8.688      with Pi_UN[OF finite_index, of "\<lambda>k i. F i k"] F
   8.689      show "f \<in> (\<Union>i. ?F i)" by (auto simp: incseq_def)
   8.690    next
   8.691 @@ -574,140 +590,144 @@
   8.692    qed
   8.693  qed
   8.694  
   8.695 -lemma sets_pair_cancel_measure[simp]:
   8.696 -  "sets (M1\<lparr>measure := m1\<rparr> \<Otimes>\<^isub>M M2) = sets (M1 \<Otimes>\<^isub>M M2)"
   8.697 -  "sets (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m2\<rparr>) = sets (M1 \<Otimes>\<^isub>M M2)"
   8.698 -  unfolding pair_measure_def pair_measure_generator_def sets_sigma
   8.699 -  by simp_all
   8.700 -
   8.701 -lemma measurable_pair_cancel_measure[simp]:
   8.702 -  "measurable (M1\<lparr>measure := m1\<rparr> \<Otimes>\<^isub>M M2) M = measurable (M1 \<Otimes>\<^isub>M M2) M"
   8.703 -  "measurable (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m2\<rparr>) M = measurable (M1 \<Otimes>\<^isub>M M2) M"
   8.704 -  "measurable M (M1\<lparr>measure := m3\<rparr> \<Otimes>\<^isub>M M2) = measurable M (M1 \<Otimes>\<^isub>M M2)"
   8.705 -  "measurable M (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m4\<rparr>) = measurable M (M1 \<Otimes>\<^isub>M M2)"
   8.706 -  unfolding measurable_def by (auto simp add: space_pair_measure)
   8.707 -
   8.708 -lemma (in product_sigma_finite) product_measure_exists:
   8.709 +lemma (in product_sigma_finite)
   8.710    assumes "finite I"
   8.711 -  shows "\<exists>\<nu>. sigma_finite_measure (sigma (product_algebra_generator I M) \<lparr> measure := \<nu> \<rparr>) \<and>
   8.712 -    (\<forall>A\<in>\<Pi> i\<in>I. sets (M i). \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i)))"
   8.713 +  shows sigma_finite: "sigma_finite_measure (PiM I M)"
   8.714 +  and emeasure_PiM:
   8.715 +    "\<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))"
   8.716  using `finite I` proof induct
   8.717    case empty
   8.718 -  interpret finite_product_sigma_finite M "{}" by default simp
   8.719 -  let ?\<nu> = "(\<lambda>A. if A = {} then 0 else 1) :: 'd set \<Rightarrow> ereal"
   8.720 -  show ?case
   8.721 -  proof (intro exI conjI ballI)
   8.722 -    have "sigma_algebra (sigma G \<lparr>measure := ?\<nu>\<rparr>)"
   8.723 -      by (rule sigma_algebra_cong) (simp_all add: product_algebra_def)
   8.724 -    then have "measure_space (sigma G\<lparr>measure := ?\<nu>\<rparr>)"
   8.725 -      by (rule finite_additivity_sufficient)
   8.726 -         (simp_all add: positive_def additive_def sets_sigma
   8.727 -                        product_algebra_generator_def image_constant)
   8.728 -    then show "sigma_finite_measure (sigma G\<lparr>measure := ?\<nu>\<rparr>)"
   8.729 -      by (auto intro!: exI[of _ "\<lambda>i. {\<lambda>_. undefined}"]
   8.730 -               simp: sigma_finite_measure_def sigma_finite_measure_axioms_def
   8.731 -                     product_algebra_generator_def)
   8.732 -  qed auto
   8.733 +  let ?\<mu> = "\<lambda>A. if A = {} then 0 else (1::ereal)"
   8.734 +  have "prod_algebra {} M = {{\<lambda>_. undefined}}"
   8.735 +    by (auto simp: prod_algebra_def prod_emb_def intro!: image_eqI)
   8.736 +  then have sets_empty: "sets (PiM {} M) = {{}, {\<lambda>_. undefined}}"
   8.737 +    by (simp add: sets_PiM)
   8.738 +  have "emeasure (Pi\<^isub>M {} M) (prod_emb {} M {} (\<Pi>\<^isub>E i\<in>{}. {})) = 1"
   8.739 +  proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   8.740 +    have "finite (space (PiM {} M))"
   8.741 +      by (simp add: space_PiM)
   8.742 +    moreover show "positive (PiM {} M) ?\<mu>"
   8.743 +      by (auto simp: positive_def)
   8.744 +    ultimately show "countably_additive (PiM {} M) ?\<mu>"
   8.745 +      by (rule countably_additiveI_finite) (auto simp: additive_def space_PiM sets_empty)
   8.746 +  qed (auto simp: prod_emb_def)
   8.747 +  also have *: "(prod_emb {} M {} (\<Pi>\<^isub>E i\<in>{}. {})) = {\<lambda>_. undefined}"
   8.748 +    by (auto simp: prod_emb_def)
   8.749 +  finally have emeasure_eq: "emeasure (Pi\<^isub>M {} M) {\<lambda>_. undefined} = 1" .
   8.750 +
   8.751 +  interpret finite_measure "PiM {} M"
   8.752 +    by default (simp add: space_PiM emeasure_eq)
   8.753 +  case 1 show ?case ..
   8.754 +
   8.755 +  case 2 show ?case
   8.756 +    using emeasure_eq * by simp
   8.757  next
   8.758    case (insert i I)
   8.759    interpret finite_product_sigma_finite M I by default fact
   8.760    have "finite (insert i I)" using `finite I` by auto
   8.761    interpret I': finite_product_sigma_finite M "insert i I" by default fact
   8.762 -  from insert obtain \<nu> where
   8.763 -    prod: "\<And>A. A \<in> (\<Pi> i\<in>I. sets (M i)) \<Longrightarrow> \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))" and
   8.764 -    "sigma_finite_measure (sigma G\<lparr> measure := \<nu> \<rparr>)" by auto
   8.765 -  then interpret I: sigma_finite_measure "P\<lparr> measure := \<nu>\<rparr>" unfolding product_algebra_def by simp
   8.766 -  interpret P: pair_sigma_finite "P\<lparr> measure := \<nu>\<rparr>" "M i" ..
   8.767 +  interpret I: sigma_finite_measure "PiM I M" by fact
   8.768 +  interpret P: pair_sigma_finite "PiM I M" "M i" ..
   8.769    let ?h = "(\<lambda>(f, y). f(i := y))"
   8.770 -  let ?\<nu> = "\<lambda>A. P.\<mu> (?h -` A \<inter> space P.P)"
   8.771 -  have I': "sigma_algebra (I'.P\<lparr> measure := ?\<nu> \<rparr>)"
   8.772 -    by (rule I'.sigma_algebra_cong) simp_all
   8.773 -  interpret I'': measure_space "I'.P\<lparr> measure := ?\<nu> \<rparr>"
   8.774 -    using measurable_add_dim[OF `i \<notin> I`]
   8.775 -    by (intro P.measure_space_vimage[OF I']) (auto simp add: measure_preserving_def)
   8.776 -  show ?case
   8.777 -  proof (intro exI[of _ ?\<nu>] conjI ballI)
   8.778 -    let ?m = "\<lambda>A. measure (Pi\<^isub>M I M\<lparr>measure := \<nu>\<rparr> \<Otimes>\<^isub>M M i) (?h -` A \<inter> space P.P)"
   8.779 -    { fix A assume A: "A \<in> (\<Pi> i\<in>insert i I. sets (M i))"
   8.780 -      then have *: "?h -` Pi\<^isub>E (insert i I) A \<inter> space P.P = Pi\<^isub>E I A \<times> A i"
   8.781 -        using `i \<notin> I` M.sets_into_space by (auto simp: space_pair_measure space_product_algebra) blast
   8.782 -      show "?m (Pi\<^isub>E (insert i I) A) = (\<Prod>i\<in>insert i I. M.\<mu> i (A i))"
   8.783 -        unfolding * using A
   8.784 -        apply (subst P.pair_measure_times)
   8.785 -        using A apply fastforce
   8.786 -        using A apply fastforce
   8.787 -        using `i \<notin> I` `finite I` prod[of A] A by (auto simp: ac_simps) }
   8.788 -    note product = this
   8.789 -    have *: "sigma I'.G\<lparr> measure := ?\<nu> \<rparr> = I'.P\<lparr> measure := ?\<nu> \<rparr>"
   8.790 -      by (simp add: product_algebra_def)
   8.791 -    show "sigma_finite_measure (sigma I'.G\<lparr> measure := ?\<nu> \<rparr>)"
   8.792 -    proof (unfold *, default, simp)
   8.793 -      from I'.sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   8.794 -      then have F: "\<And>j. j \<in> insert i I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
   8.795 -        "incseq (\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k)"
   8.796 -        "(\<Union>k. \<Pi>\<^isub>E j \<in> insert i I. F j k) = space I'.G"
   8.797 -        "\<And>k. \<And>j. j \<in> insert i I \<Longrightarrow> \<mu> j (F j k) \<noteq> \<infinity>"
   8.798 -        by blast+
   8.799 -      let ?F = "\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k"
   8.800 -      show "\<exists>F::nat \<Rightarrow> ('i \<Rightarrow> 'a) set. range F \<subseteq> sets I'.P \<and>
   8.801 -          (\<Union>i. F i) = (\<Pi>\<^isub>E i\<in>insert i I. space (M i)) \<and> (\<forall>i. ?m (F i) \<noteq> \<infinity>)"
   8.802 -      proof (intro exI[of _ ?F] conjI allI)
   8.803 -        show "range ?F \<subseteq> sets I'.P" using F(1) by auto
   8.804 -      next
   8.805 -        from F(3) show "(\<Union>i. ?F i) = (\<Pi>\<^isub>E i\<in>insert i I. space (M i))" by simp
   8.806 -      next
   8.807 -        fix j
   8.808 -        have "\<And>k. k \<in> insert i I \<Longrightarrow> 0 \<le> measure (M k) (F k j)"
   8.809 -          using F(1) by auto
   8.810 -        with F `finite I` setprod_PInf[of "insert i I", OF this] show "?\<nu> (?F j) \<noteq> \<infinity>"
   8.811 -          by (subst product) auto
   8.812 -      qed
   8.813 -    qed
   8.814 +
   8.815 +  let ?P = "distr (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) (Pi\<^isub>M (insert i I) M) ?h"
   8.816 +  let ?\<mu> = "emeasure ?P"
   8.817 +  let ?I = "{j \<in> insert i I. emeasure (M j) (space (M j)) \<noteq> 1}"
   8.818 +  let ?f = "\<lambda>J E j. if j \<in> J then emeasure (M j) (E j) else emeasure (M j) (space (M j))"
   8.819 +
   8.820 +  { case 2
   8.821 +    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)) =
   8.822 +      (\<Prod>i\<in>insert i I. emeasure (M i) (A i))"
   8.823 +    proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   8.824 +      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))"
   8.825 +      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
   8.826 +      let ?p = "prod_emb (insert i I) M J (Pi\<^isub>E J E)"
   8.827 +      let ?p' = "prod_emb I M (J - {i}) (\<Pi>\<^isub>E j\<in>J-{i}. E j)"
   8.828 +      have "?\<mu> ?p =
   8.829 +        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))"
   8.830 +        by (intro emeasure_distr measurable_add_dim sets_PiM_I) fact+
   8.831 +      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))"
   8.832 +        using J E[rule_format, THEN sets_into_space]
   8.833 +        by (force simp: space_pair_measure space_PiM Pi_iff prod_emb_iff split: split_if_asm)
   8.834 +      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))) =
   8.835 +        emeasure (Pi\<^isub>M I M) ?p' * emeasure (M i) (if i \<in> J then (E i) else space (M i))"
   8.836 +        using J E by (intro P.emeasure_pair_measure_Times sets_PiM_I) auto
   8.837 +      also have "?p' = (\<Pi>\<^isub>E j\<in>I. if j \<in> J-{i} then E j else space (M j))"
   8.838 +        using J E[rule_format, THEN sets_into_space]
   8.839 +        by (auto simp: prod_emb_iff Pi_iff split: split_if_asm) blast+
   8.840 +      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)) =
   8.841 +        (\<Prod> j\<in>I. if j \<in> J-{i} then emeasure (M j) (E j) else emeasure (M j) (space (M j)))"
   8.842 +        using E by (subst insert) (auto intro!: setprod_cong)
   8.843 +      also have "(\<Prod>j\<in>I. if j \<in> J - {i} then emeasure (M j) (E j) else emeasure (M j) (space (M j))) *
   8.844 +         emeasure (M i) (if i \<in> J then E i else space (M i)) = (\<Prod>j\<in>insert i I. ?f J E j)"
   8.845 +        using insert by (auto simp: mult_commute intro!: arg_cong2[where f="op *"] setprod_cong)
   8.846 +      also have "\<dots> = (\<Prod>j\<in>J \<union> ?I. ?f J E j)"
   8.847 +        using insert(1,2) J E by (intro setprod_mono_one_right) auto
   8.848 +      finally show "?\<mu> ?p = \<dots>" .
   8.849 +
   8.850 +      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))"
   8.851 +        using J E[rule_format, THEN sets_into_space] by (auto simp: prod_emb_iff)
   8.852 +    next
   8.853 +      show "positive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>" "countably_additive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>"
   8.854 +        using emeasure_positive[of ?P] emeasure_countably_additive[of ?P] by simp_all
   8.855 +    next
   8.856 +      show "(insert i I \<noteq> {} \<or> insert i I = {}) \<and> finite (insert i I) \<and>
   8.857 +        insert i I \<subseteq> insert i I \<and> A \<in> (\<Pi> j\<in>insert i I. sets (M j))"
   8.858 +        using insert(1,2) 2 by auto
   8.859 +    qed (auto intro!: setprod_cong)
   8.860 +    with 2[THEN sets_into_space] show ?case by (subst (asm) prod_emb_PiE_same_index) auto }
   8.861 +  note product = this
   8.862 +
   8.863 +  from I'.sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   8.864 +  then have F: "\<And>j. j \<in> insert i I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
   8.865 +    "incseq (\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k)"
   8.866 +    "(\<Union>k. \<Pi>\<^isub>E j \<in> insert i I. F j k) = space (Pi\<^isub>M (insert i I) M)"
   8.867 +    "\<And>k. \<And>j. j \<in> insert i I \<Longrightarrow> emeasure (M j) (F j k) \<noteq> \<infinity>"
   8.868 +    by blast+
   8.869 +  let ?F = "\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k"
   8.870 +
   8.871 +  case 1 show ?case
   8.872 +  proof (unfold_locales, intro exI[of _ ?F] conjI allI)
   8.873 +    show "range ?F \<subseteq> sets (Pi\<^isub>M (insert i I) M)" using F(1) insert(1,2) by auto
   8.874 +  next
   8.875 +    from F(3) show "(\<Union>i. ?F i) = space (Pi\<^isub>M (insert i I) M)" by simp
   8.876 +  next
   8.877 +    fix j
   8.878 +    from F `finite I` setprod_PInf[of "insert i I", OF emeasure_nonneg, of M]
   8.879 +    show "emeasure (\<Pi>\<^isub>M i\<in>insert i I. M i) (?F j) \<noteq> \<infinity>"
   8.880 +      by (subst product) auto
   8.881    qed
   8.882  qed
   8.883  
   8.884 -sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure P
   8.885 -  unfolding product_algebra_def
   8.886 -  using product_measure_exists[OF finite_index]
   8.887 -  by (rule someI2_ex) auto
   8.888 +sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure "Pi\<^isub>M I M"
   8.889 +  using sigma_finite[OF finite_index] .
   8.890  
   8.891  lemma (in finite_product_sigma_finite) measure_times:
   8.892 -  assumes "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)"
   8.893 -  shows "\<mu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
   8.894 -  unfolding product_algebra_def
   8.895 -  using product_measure_exists[OF finite_index]
   8.896 -  proof (rule someI2_ex, elim conjE)
   8.897 -    fix \<nu> assume *: "\<forall>A\<in>\<Pi> i\<in>I. sets (M i). \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
   8.898 -    have "Pi\<^isub>E I A = Pi\<^isub>E I (\<lambda>i\<in>I. A i)" by (auto dest: Pi_mem)
   8.899 -    then have "\<nu> (Pi\<^isub>E I A) = \<nu> (Pi\<^isub>E I (\<lambda>i\<in>I. A i))" by simp
   8.900 -    also have "\<dots> = (\<Prod>i\<in>I. M.\<mu> i ((\<lambda>i\<in>I. A i) i))" using assms * by auto
   8.901 -    finally show "measure (sigma G\<lparr>measure := \<nu>\<rparr>) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
   8.902 -      by (simp add: sigma_def)
   8.903 -  qed
   8.904 +  "(\<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))"
   8.905 +  using emeasure_PiM[OF finite_index] by auto
   8.906  
   8.907  lemma (in product_sigma_finite) product_measure_empty[simp]:
   8.908 -  "measure (Pi\<^isub>M {} M) {\<lambda>x. undefined} = 1"
   8.909 +  "emeasure (Pi\<^isub>M {} M) {\<lambda>x. undefined} = 1"
   8.910  proof -
   8.911    interpret finite_product_sigma_finite M "{}" by default auto
   8.912    from measure_times[of "\<lambda>x. {}"] show ?thesis by simp
   8.913  qed
   8.914  
   8.915 -lemma (in finite_product_sigma_algebra) P_empty:
   8.916 -  assumes "I = {}"
   8.917 -  shows "space P = {\<lambda>k. undefined}" "sets P = { {}, {\<lambda>k. undefined} }"
   8.918 -  unfolding product_algebra_def product_algebra_generator_def `I = {}`
   8.919 -  by (simp_all add: sigma_def image_constant)
   8.920 +lemma
   8.921 +  shows space_PiM_empty: "space (Pi\<^isub>M {} M) = {\<lambda>k. undefined}"
   8.922 +    and sets_PiM_empty: "sets (Pi\<^isub>M {} M) = { {}, {\<lambda>k. undefined} }"
   8.923 +  by (simp_all add: space_PiM sets_PiM_single image_constant sigma_sets_empty_eq)
   8.924  
   8.925  lemma (in product_sigma_finite) positive_integral_empty:
   8.926    assumes pos: "0 \<le> f (\<lambda>k. undefined)"
   8.927    shows "integral\<^isup>P (Pi\<^isub>M {} M) f = f (\<lambda>k. undefined)"
   8.928  proof -
   8.929    interpret finite_product_sigma_finite M "{}" by default (fact finite.emptyI)
   8.930 -  have "\<And>A. measure (Pi\<^isub>M {} M) (Pi\<^isub>E {} A) = 1"
   8.931 +  have "\<And>A. emeasure (Pi\<^isub>M {} M) (Pi\<^isub>E {} A) = 1"
   8.932      using assms by (subst measure_times) auto
   8.933    then show ?thesis
   8.934 -    unfolding positive_integral_def simple_function_def simple_integral_def [abs_def]
   8.935 -  proof (simp add: P_empty, intro antisym)
   8.936 +    unfolding positive_integral_def simple_function_def simple_integral_def[abs_def]
   8.937 +  proof (simp add: space_PiM_empty sets_PiM_empty, intro antisym)
   8.938      show "f (\<lambda>k. undefined) \<le> (SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined))"
   8.939        by (intro SUP_upper) (auto simp: le_fun_def split: split_max)
   8.940      show "(SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined)) \<le> f (\<lambda>k. undefined)" using pos
   8.941 @@ -715,71 +735,72 @@
   8.942    qed
   8.943  qed
   8.944  
   8.945 -lemma (in product_sigma_finite) measure_fold:
   8.946 +lemma (in product_sigma_finite) distr_merge:
   8.947    assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   8.948 -  assumes A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
   8.949 -  shows "measure (Pi\<^isub>M (I \<union> J) M) A =
   8.950 -    measure (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) ((\<lambda>(x,y). merge I x J y) -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M))"
   8.951 +  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"
   8.952 +   (is "?D = ?P")
   8.953  proof -
   8.954    interpret I: finite_product_sigma_finite M I by default fact
   8.955    interpret J: finite_product_sigma_finite M J by default fact
   8.956    have "finite (I \<union> J)" using fin by auto
   8.957    interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
   8.958 -  interpret P: pair_sigma_finite I.P J.P by default
   8.959 +  interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   8.960    let ?g = "\<lambda>(x,y). merge I x J y"
   8.961 -  let ?X = "\<lambda>S. ?g -` S \<inter> space P.P"
   8.962 +
   8.963    from IJ.sigma_finite_pairs obtain F where
   8.964      F: "\<And>i. i\<in> I \<union> J \<Longrightarrow> range (F i) \<subseteq> sets (M i)"
   8.965         "incseq (\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k)"
   8.966 -       "(\<Union>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k) = space IJ.G"
   8.967 -       "\<And>k. \<forall>i\<in>I\<union>J. \<mu> i (F i k) \<noteq> \<infinity>"
   8.968 +       "(\<Union>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k) = space ?P"
   8.969 +       "\<And>k. \<forall>i\<in>I\<union>J. emeasure (M i) (F i k) \<noteq> \<infinity>"
   8.970      by auto
   8.971    let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k"
   8.972 -  show "IJ.\<mu> A = P.\<mu> (?X A)"
   8.973 -  proof (rule measure_unique_Int_stable_vimage)
   8.974 -    show "measure_space IJ.P" "measure_space P.P" by default
   8.975 -    show "sets (sigma IJ.G) = sets IJ.P" "space IJ.G = space IJ.P" "A \<in> sets (sigma IJ.G)"
   8.976 -      using A unfolding product_algebra_def by auto
   8.977 -  next
   8.978 -    show "Int_stable IJ.G"
   8.979 -      by (rule Int_stable_product_algebra_generator)
   8.980 -         (auto simp: Int_stable_def)
   8.981 -    show "range ?F \<subseteq> sets IJ.G" using F
   8.982 -      by (simp add: image_subset_iff product_algebra_def
   8.983 -                    product_algebra_generator_def)
   8.984 -    show "incseq ?F" "(\<Union>i. ?F i) = space IJ.G " by fact+
   8.985 +  
   8.986 +  show ?thesis
   8.987 +  proof (rule measure_eqI_generator_eq[symmetric])
   8.988 +    show "Int_stable (prod_algebra (I \<union> J) M)"
   8.989 +      by (rule Int_stable_prod_algebra)
   8.990 +    show "prod_algebra (I \<union> J) M \<subseteq> Pow (\<Pi>\<^isub>E i \<in> I \<union> J. space (M i))"
   8.991 +      by (rule prod_algebra_sets_into_space)
   8.992 +    show "sets ?P = sigma_sets (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i)) (prod_algebra (I \<union> J) M)"
   8.993 +      by (rule sets_PiM)
   8.994 +    then show "sets ?D = sigma_sets (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i)) (prod_algebra (I \<union> J) M)"
   8.995 +      by simp
   8.996 +
   8.997 +    show "range ?F \<subseteq> prod_algebra (I \<union> J) M" using F
   8.998 +      using fin by (auto simp: prod_algebra_eq_finite)
   8.999 +    show "incseq ?F" by fact
  8.1000 +    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))"
  8.1001 +      using F(3) by (simp add: space_PiM)
  8.1002    next
  8.1003      fix k
  8.1004 -    have "\<And>j. j \<in> I \<union> J \<Longrightarrow> 0 \<le> measure (M j) (F j k)"
  8.1005 -      using F(1) by auto
  8.1006 -    with F `finite I` setprod_PInf[of "I \<union> J", OF this]
  8.1007 -    show "IJ.\<mu> (?F k) \<noteq> \<infinity>" by (subst IJ.measure_times) auto
  8.1008 +    from F `finite I` setprod_PInf[of "I \<union> J", OF emeasure_nonneg, of M]
  8.1009 +    show "emeasure ?P (?F k) \<noteq> \<infinity>" by (subst IJ.measure_times) auto
  8.1010    next
  8.1011 -    fix A assume "A \<in> sets IJ.G"
  8.1012 -    then obtain F where A: "A = Pi\<^isub>E (I \<union> J) F"
  8.1013 -      and F: "\<And>i. i \<in> I \<union> J \<Longrightarrow> F i \<in> sets (M i)"
  8.1014 -      by (auto simp: product_algebra_generator_def)
  8.1015 -    then have X: "?X A = (Pi\<^isub>E I F \<times> Pi\<^isub>E J F)"
  8.1016 -      using sets_into_space by (auto simp: space_pair_measure) blast+
  8.1017 -    then have "P.\<mu> (?X A) = (\<Prod>i\<in>I. \<mu> i (F i)) * (\<Prod>i\<in>J. \<mu> i (F i))"
  8.1018 -      using `finite J` `finite I` F
  8.1019 -      by (simp add: P.pair_measure_times I.measure_times J.measure_times)
  8.1020 -    also have "\<dots> = (\<Prod>i\<in>I \<union> J. \<mu> i (F i))"
  8.1021 +    fix A assume A: "A \<in> prod_algebra (I \<union> J) M"
  8.1022 +    with fin obtain F where A_eq: "A = (Pi\<^isub>E (I \<union> J) F)" and F: "\<forall>i\<in>I \<union> J. F i \<in> sets (M i)"
  8.1023 +      by (auto simp add: prod_algebra_eq_finite)
  8.1024 +    let ?B = "Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M"
  8.1025 +    let ?X = "?g -` A \<inter> space ?B"
  8.1026 +    have "Pi\<^isub>E I F \<subseteq> space (Pi\<^isub>M I M)" "Pi\<^isub>E J F \<subseteq> space (Pi\<^isub>M J M)"
  8.1027 +      using F[rule_format, THEN sets_into_space] by (auto simp: space_PiM)
  8.1028 +    then have X: "?X = (Pi\<^isub>E I F \<times> Pi\<^isub>E J F)"
  8.1029 +      unfolding A_eq by (subst merge_vimage) (auto simp: space_pair_measure space_PiM)
  8.1030 +    have "emeasure ?D A = emeasure ?B ?X"
  8.1031 +      using A by (intro emeasure_distr measurable_merge) (auto simp: sets_PiM)
  8.1032 +    also have "emeasure ?B ?X = (\<Prod>i\<in>I. emeasure (M i) (F i)) * (\<Prod>i\<in>J. emeasure (M i) (F i))"
  8.1033 +      using `finite J` `finite I` F X
  8.1034 +      by (simp add: P.emeasure_pair_measure_Times I.measure_times J.measure_times Pi_iff)
  8.1035 +    also have "\<dots> = (\<Prod>i\<in>I \<union> J. emeasure (M i) (F i))"
  8.1036        using `finite J` `finite I` `I \<inter> J = {}`  by (simp add: setprod_Un_one)
  8.1037 -    also have "\<dots> = IJ.\<mu> A"
  8.1038 +    also have "\<dots> = emeasure ?P (Pi\<^isub>E (I \<union> J) F)"
  8.1039        using `finite J` `finite I` F unfolding A
  8.1040        by (intro IJ.measure_times[symmetric]) auto
  8.1041 -    finally show "IJ.\<mu> A = P.\<mu> (?X A)" by (rule sym)
  8.1042 -  qed (rule measurable_merge[OF IJ])
  8.1043 +    finally show "emeasure ?P A = emeasure ?D A" using A_eq by simp
  8.1044 +  qed
  8.1045  qed
  8.1046  
  8.1047 -lemma (in product_sigma_finite) measure_preserving_merge:
  8.1048 -  assumes IJ: "I \<inter> J = {}" and "finite I" "finite J"
  8.1049 -  shows "(\<lambda>(x,y). merge I x J y) \<in> measure_preserving (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) (Pi\<^isub>M (I \<union> J) M)"
  8.1050 -  by (intro measure_preservingI measurable_merge[OF IJ] measure_fold[symmetric] assms)
  8.1051 -
  8.1052  lemma (in product_sigma_finite) product_positive_integral_fold:
  8.1053 -  assumes IJ[simp]: "I \<inter> J = {}" "finite I" "finite J"
  8.1054 +  assumes IJ: "I \<inter> J = {}" "finite I" "finite J"
  8.1055    and f: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
  8.1056    shows "integral\<^isup>P (Pi\<^isub>M (I \<union> J) M) f =
  8.1057      (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (merge I x J y) \<partial>(Pi\<^isub>M J M)) \<partial>(Pi\<^isub>M I M))"
  8.1058 @@ -787,42 +808,38 @@
  8.1059    interpret I: finite_product_sigma_finite M I by default fact
  8.1060    interpret J: finite_product_sigma_finite M J by default fact
  8.1061    interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
  8.1062 -  interpret IJ: finite_product_sigma_finite M "I \<union> J" by default simp
  8.1063 -  have P_borel: "(\<lambda>x. f (case x of (x, y) \<Rightarrow> merge I x J y)) \<in> borel_measurable P.P"
  8.1064 +  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)"
  8.1065      using measurable_comp[OF measurable_merge[OF IJ(1)] f] by (simp add: comp_def)
  8.1066    show ?thesis
  8.1067 -    unfolding P.positive_integral_fst_measurable[OF P_borel, simplified]
  8.1068 -  proof (rule P.positive_integral_vimage)
  8.1069 -    show "sigma_algebra IJ.P" by default
  8.1070 -    show "(\<lambda>(x, y). merge I x J y) \<in> measure_preserving P.P IJ.P"
  8.1071 -      using IJ by (rule measure_preserving_merge)
  8.1072 -    show "f \<in> borel_measurable IJ.P" using f by simp
  8.1073 -  qed
  8.1074 +    apply (subst distr_merge[OF IJ, symmetric])
  8.1075 +    apply (subst positive_integral_distr[OF measurable_merge f, OF IJ(1)])
  8.1076 +    apply (subst P.positive_integral_fst_measurable(2)[symmetric, OF P_borel])
  8.1077 +    apply simp
  8.1078 +    done
  8.1079  qed
  8.1080  
  8.1081 -lemma (in product_sigma_finite) measure_preserving_component_singelton:
  8.1082 -  "(\<lambda>x. x i) \<in> measure_preserving (Pi\<^isub>M {i} M) (M i)"
  8.1083 -proof (intro measure_preservingI measurable_component_singleton)
  8.1084 +lemma (in product_sigma_finite) distr_singleton:
  8.1085 +  "distr (Pi\<^isub>M {i} M) (M i) (\<lambda>x. x i) = M i" (is "?D = _")
  8.1086 +proof (intro measure_eqI[symmetric])
  8.1087    interpret I: finite_product_sigma_finite M "{i}" by default simp
  8.1088 -  fix A let ?P = "(\<lambda>x. x i) -` A \<inter> space I.P"
  8.1089 -  assume A: "A \<in> sets (M i)"
  8.1090 -  then have *: "?P = {i} \<rightarrow>\<^isub>E A" using sets_into_space by auto
  8.1091 -  show "I.\<mu> ?P = M.\<mu> i A" unfolding *
  8.1092 -    using A I.measure_times[of "\<lambda>_. A"] by auto
  8.1093 -qed auto
  8.1094 +  fix A assume A: "A \<in> sets (M i)"
  8.1095 +  moreover then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M {i} M) = (\<Pi>\<^isub>E i\<in>{i}. A)"
  8.1096 +    using sets_into_space by (auto simp: space_PiM)
  8.1097 +  ultimately show "emeasure (M i) A = emeasure ?D A"
  8.1098 +    using A I.measure_times[of "\<lambda>_. A"]
  8.1099 +    by (simp add: emeasure_distr measurable_component_singleton)
  8.1100 +qed simp
  8.1101  
  8.1102  lemma (in product_sigma_finite) product_positive_integral_singleton:
  8.1103    assumes f: "f \<in> borel_measurable (M i)"
  8.1104    shows "integral\<^isup>P (Pi\<^isub>M {i} M) (\<lambda>x. f (x i)) = integral\<^isup>P (M i) f"
  8.1105  proof -
  8.1106    interpret I: finite_product_sigma_finite M "{i}" by default simp
  8.1107 -  show ?thesis
  8.1108 -  proof (rule I.positive_integral_vimage[symmetric])
  8.1109 -    show "sigma_algebra (M i)" by (rule sigma_algebras)
  8.1110 -    show "(\<lambda>x. x i) \<in> measure_preserving (Pi\<^isub>M {i} M) (M i)"
  8.1111 -      by (rule measure_preserving_component_singelton)
  8.1112 -    show "f \<in> borel_measurable (M i)" by fact
  8.1113 -  qed
  8.1114 +  from f show ?thesis
  8.1115 +    apply (subst distr_singleton[symmetric])
  8.1116 +    apply (subst positive_integral_distr[OF measurable_component_singleton])
  8.1117 +    apply simp_all
  8.1118 +    done
  8.1119  qed
  8.1120  
  8.1121  lemma (in product_sigma_finite) product_positive_integral_insert:
  8.1122 @@ -832,19 +849,18 @@
  8.1123  proof -
  8.1124    interpret I: finite_product_sigma_finite M I by default auto
  8.1125    interpret i: finite_product_sigma_finite M "{i}" by default auto
  8.1126 -  interpret P: pair_sigma_algebra I.P i.P ..
  8.1127    have IJ: "I \<inter> {i} = {}" and insert: "I \<union> {i} = insert i I"
  8.1128      using f by auto
  8.1129    show ?thesis
  8.1130      unfolding product_positive_integral_fold[OF IJ, unfolded insert, simplified, OF f]
  8.1131 -  proof (rule I.positive_integral_cong, subst product_positive_integral_singleton)
  8.1132 -    fix x assume x: "x \<in> space I.P"
  8.1133 +  proof (rule positive_integral_cong, subst product_positive_integral_singleton)
  8.1134 +    fix x assume x: "x \<in> space (Pi\<^isub>M I M)"
  8.1135      let ?f = "\<lambda>y. f (restrict (x(i := y)) (insert i I))"
  8.1136      have f'_eq: "\<And>y. ?f y = f (x(i := y))"
  8.1137 -      using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
  8.1138 +      using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def space_PiM)
  8.1139      show "?f \<in> borel_measurable (M i)" unfolding f'_eq
  8.1140 -      using measurable_comp[OF measurable_component_update f] x `i \<notin> I`
  8.1141 -      by (simp add: comp_def)
  8.1142 +      using measurable_comp[OF measurable_component_update f, OF x `i \<notin> I`]
  8.1143 +      unfolding comp_def .
  8.1144      show "integral\<^isup>P (M i) ?f = \<integral>\<^isup>+ y. f (x(i:=y)) \<partial>M i"
  8.1145        unfolding f'_eq by simp
  8.1146    qed
  8.1147 @@ -856,10 +872,6 @@
  8.1148    and pos: "\<And>i x. i \<in> I \<Longrightarrow> 0 \<le> f i x"
  8.1149    shows "(\<integral>\<^isup>+ x. (\<Prod>i\<in>I. f i (x i)) \<partial>Pi\<^isub>M I M) = (\<Prod>i\<in>I. integral\<^isup>P (M i) (f i))"
  8.1150  using assms proof induct
  8.1151 -  case empty
  8.1152 -  interpret finite_product_sigma_finite M "{}" by default auto
  8.1153 -  show ?case by simp
  8.1154 -next
  8.1155    case (insert i I)
  8.1156    note `finite I`[intro, simp]
  8.1157    interpret I: finite_product_sigma_finite M I by default auto
  8.1158 @@ -867,16 +879,16 @@
  8.1159      using insert by (auto intro!: setprod_cong)
  8.1160    have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow> (\<lambda>x. (\<Prod>i\<in>J. f i (x i))) \<in> borel_measurable (Pi\<^isub>M J M)"
  8.1161      using sets_into_space insert
  8.1162 -    by (intro sigma_algebra.borel_measurable_ereal_setprod sigma_algebra_product_algebra
  8.1163 +    by (intro borel_measurable_ereal_setprod
  8.1164                measurable_comp[OF measurable_component_singleton, unfolded comp_def])
  8.1165         auto
  8.1166    then show ?case
  8.1167      apply (simp add: product_positive_integral_insert[OF insert(1,2) prod])
  8.1168 -    apply (simp add: insert * pos borel setprod_ereal_pos M.positive_integral_multc)
  8.1169 -    apply (subst I.positive_integral_cmult)
  8.1170 -    apply (auto simp add: pos borel insert setprod_ereal_pos positive_integral_positive)
  8.1171 +    apply (simp add: insert(2-) * pos borel setprod_ereal_pos positive_integral_multc)
  8.1172 +    apply (subst positive_integral_cmult)
  8.1173 +    apply (auto simp add: pos borel insert(2-) setprod_ereal_pos positive_integral_positive)
  8.1174      done
  8.1175 -qed
  8.1176 +qed (simp add: space_PiM)
  8.1177  
  8.1178  lemma (in product_sigma_finite) product_integral_singleton:
  8.1179    assumes f: "f \<in> borel_measurable (M i)"
  8.1180 @@ -899,48 +911,44 @@
  8.1181    interpret J: finite_product_sigma_finite M J by default fact
  8.1182    have "finite (I \<union> J)" using fin by auto
  8.1183    interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
  8.1184 -  interpret P: pair_sigma_finite I.P J.P by default
  8.1185 +  interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
  8.1186    let ?M = "\<lambda>(x, y). merge I x J y"
  8.1187    let ?f = "\<lambda>x. f (?M x)"
  8.1188 +  from f have f_borel: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
  8.1189 +    by auto
  8.1190 +  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)"
  8.1191 +    using measurable_comp[OF measurable_merge[OF IJ(1)] f_borel] by (simp add: comp_def)
  8.1192 +  have f_int: "integrable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) ?f"
  8.1193 +    by (rule integrable_distr[OF measurable_merge[OF IJ]]) (simp add: distr_merge[OF IJ fin] f)
  8.1194    show ?thesis
  8.1195 -  proof (subst P.integrable_fst_measurable(2)[of ?f, simplified])
  8.1196 -    have 1: "sigma_algebra IJ.P" by default
  8.1197 -    have 2: "?M \<in> measure_preserving P.P IJ.P" using measure_preserving_merge[OF assms(1,2,3)] .
  8.1198 -    have 3: "integrable (Pi\<^isub>M (I \<union> J) M) f" by fact
  8.1199 -    then have 4: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
  8.1200 -      by (simp add: integrable_def)
  8.1201 -    show "integrable P.P ?f"
  8.1202 -      by (rule P.integrable_vimage[where f=f, OF 1 2 3])
  8.1203 -    show "integral\<^isup>L IJ.P f = integral\<^isup>L P.P ?f"
  8.1204 -      by (rule P.integral_vimage[where f=f, OF 1 2 4])
  8.1205 -  qed
  8.1206 +    apply (subst distr_merge[symmetric, OF IJ fin])
  8.1207 +    apply (subst integral_distr[OF measurable_merge[OF IJ] f_borel])
  8.1208 +    apply (subst P.integrable_fst_measurable(2)[symmetric, OF f_int])
  8.1209 +    apply simp
  8.1210 +    done
  8.1211  qed
  8.1212  
  8.1213  lemma (in product_sigma_finite) product_integral_insert:
  8.1214 -  assumes [simp]: "finite I" "i \<notin> I"
  8.1215 +  assumes I: "finite I" "i \<notin> I"
  8.1216      and f: "integrable (Pi\<^isub>M (insert i I) M) f"
  8.1217    shows "integral\<^isup>L (Pi\<^isub>M (insert i I) M) f = (\<integral>x. (\<integral>y. f (x(i:=y)) \<partial>M i) \<partial>Pi\<^isub>M I M)"
  8.1218  proof -
  8.1219 -  interpret I: finite_product_sigma_finite M I by default auto
  8.1220 -  interpret I': finite_product_sigma_finite M "insert i I" by default auto
  8.1221 -  interpret i: finite_product_sigma_finite M "{i}" by default auto
  8.1222 -  interpret P: pair_sigma_finite I.P i.P ..
  8.1223 -  have IJ: "I \<inter> {i} = {}" by auto
  8.1224 -  show ?thesis
  8.1225 -    unfolding product_integral_fold[OF IJ, simplified, OF f]
  8.1226 -  proof (rule I.integral_cong, subst product_integral_singleton)
  8.1227 -    fix x assume x: "x \<in> space I.P"
  8.1228 -    let ?f = "\<lambda>y. f (restrict (x(i := y)) (insert i I))"
  8.1229 -    have f'_eq: "\<And>y. ?f y = f (x(i := y))"
  8.1230 -      using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
  8.1231 -    have f: "f \<in> borel_measurable I'.P" using f unfolding integrable_def by auto
  8.1232 -    show "?f \<in> borel_measurable (M i)"
  8.1233 -      unfolding measurable_cong[OF f'_eq]
  8.1234 -      using measurable_comp[OF measurable_component_update f] x `i \<notin> I`
  8.1235 -      by (simp add: comp_def)
  8.1236 -    show "integral\<^isup>L (M i) ?f = integral\<^isup>L (M i) (\<lambda>y. f (x(i := y)))"
  8.1237 -      unfolding f'_eq by simp
  8.1238 +  have "integral\<^isup>L (Pi\<^isub>M (insert i I) M) f = integral\<^isup>L (Pi\<^isub>M (I \<union> {i}) M) f"
  8.1239 +    by simp
  8.1240 +  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)"
  8.1241 +    using f I by (intro product_integral_fold) auto
  8.1242 +  also have "\<dots> = (\<integral>x. (\<integral>y. f (x(i := y)) \<partial>M i) \<partial>Pi\<^isub>M I M)"
  8.1243 +  proof (rule integral_cong, subst product_integral_singleton[symmetric])
  8.1244 +    fix x assume x: "x \<in> space (Pi\<^isub>M I M)"
  8.1245 +    have f_borel: "f \<in> borel_measurable (Pi\<^isub>M (insert i I) M)"
  8.1246 +      using f by auto
  8.1247 +    show "(\<lambda>y. f (x(i := y))) \<in> borel_measurable (M i)"
  8.1248 +      using measurable_comp[OF measurable_component_update f_borel, OF x `i \<notin> I`]
  8.1249 +      unfolding comp_def .
  8.1250 +    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)"
  8.1251 +      by (auto intro!: integral_cong arg_cong[where f=f] simp: merge_def space_PiM extensional_def)
  8.1252    qed
  8.1253 +  finally show ?thesis .
  8.1254  qed
  8.1255  
  8.1256  lemma (in product_sigma_finite) product_integrable_setprod:
  8.1257 @@ -951,24 +959,23 @@
  8.1258    interpret finite_product_sigma_finite M I by default fact
  8.1259    have f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
  8.1260      using integrable unfolding integrable_def by auto
  8.1261 -  then have borel: "?f \<in> borel_measurable P"
  8.1262 -    using measurable_comp[OF measurable_component_singleton f]
  8.1263 -    by (auto intro!: borel_measurable_setprod simp: comp_def)
  8.1264 +  have borel: "?f \<in> borel_measurable (Pi\<^isub>M I M)"
  8.1265 +    using measurable_comp[OF measurable_component_singleton[of _ I M] f] by (auto simp: comp_def)
  8.1266    moreover have "integrable (Pi\<^isub>M I M) (\<lambda>x. \<bar>\<Prod>i\<in>I. f i (x i)\<bar>)"
  8.1267    proof (unfold integrable_def, intro conjI)
  8.1268 -    show "(\<lambda>x. abs (?f x)) \<in> borel_measurable P"
  8.1269 +    show "(\<lambda>x. abs (?f x)) \<in> borel_measurable (Pi\<^isub>M I M)"
  8.1270        using borel by auto
  8.1271 -    have "(\<integral>\<^isup>+x. ereal (abs (?f x)) \<partial>P) = (\<integral>\<^isup>+x. (\<Prod>i\<in>I. ereal (abs (f i (x i)))) \<partial>P)"
  8.1272 +    have "(\<integral>\<^isup>+x. ereal (abs (?f x)) \<partial>Pi\<^isub>M I M) = (\<integral>\<^isup>+x. (\<Prod>i\<in>I. ereal (abs (f i (x i)))) \<partial>Pi\<^isub>M I M)"
  8.1273        by (simp add: setprod_ereal abs_setprod)
  8.1274      also have "\<dots> = (\<Prod>i\<in>I. (\<integral>\<^isup>+x. ereal (abs (f i x)) \<partial>M i))"
  8.1275        using f by (subst product_positive_integral_setprod) auto
  8.1276      also have "\<dots> < \<infinity>"
  8.1277 -      using integrable[THEN M.integrable_abs]
  8.1278 -      by (simp add: setprod_PInf integrable_def M.positive_integral_positive)
  8.1279 -    finally show "(\<integral>\<^isup>+x. ereal (abs (?f x)) \<partial>P) \<noteq> \<infinity>" by auto
  8.1280 -    have "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>P) = (\<integral>\<^isup>+x. 0 \<partial>P)"
  8.1281 +      using integrable[THEN integrable_abs]
  8.1282 +      by (simp add: setprod_PInf integrable_def positive_integral_positive)
  8.1283 +    finally show "(\<integral>\<^isup>+x. ereal (abs (?f x)) \<partial>(Pi\<^isub>M I M)) \<noteq> \<infinity>" by auto
  8.1284 +    have "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>(Pi\<^isub>M I M)) = (\<integral>\<^isup>+x. 0 \<partial>(Pi\<^isub>M I M))"
  8.1285        by (intro positive_integral_cong_pos) auto
  8.1286 -    then show "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>P) \<noteq> \<infinity>" by simp
  8.1287 +    then show "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>(Pi\<^isub>M I M)) \<noteq> \<infinity>" by simp
  8.1288    qed
  8.1289    ultimately show ?thesis
  8.1290      by (rule integrable_abs_iff[THEN iffD1])
  8.1291 @@ -992,7 +999,69 @@
  8.1292      using `i \<notin> I` by (auto intro!: setprod_cong)
  8.1293    show ?case
  8.1294      unfolding product_integral_insert[OF insert(1,3) prod[OF subset_refl]]
  8.1295 -    by (simp add: * insert integral_multc I.integral_cmult[OF prod] subset_insertI)
  8.1296 +    by (simp add: * insert integral_multc integral_cmult[OF prod] subset_insertI)
  8.1297  qed
  8.1298  
  8.1299 -end
  8.1300 \ No newline at end of file
  8.1301 +lemma sigma_prod_algebra_sigma_eq:
  8.1302 +  fixes E :: "'i \<Rightarrow> 'a set set"
  8.1303 +  assumes "finite I"
  8.1304 +  assumes S_mono: "\<And>i. i \<in> I \<Longrightarrow> incseq (S i)"
  8.1305 +    and S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
  8.1306 +    and S_in_E: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> E i"
  8.1307 +  assumes E_closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M i))"
  8.1308 +    and E_generates: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sigma_sets (space (M i)) (E i)"
  8.1309 +  defines "P == { Pi\<^isub>E I F | F. \<forall>i\<in>I. F i \<in> E i }"
  8.1310 +  shows "sets (PiM I M) = sigma_sets (space (PiM I M)) P"
  8.1311 +proof
  8.1312 +  let ?P = "sigma (space (Pi\<^isub>M I M)) P"
  8.1313 +  have P_closed: "P \<subseteq> Pow (space (Pi\<^isub>M I M))"
  8.1314 +    using E_closed by (auto simp: space_PiM P_def Pi_iff subset_eq)
  8.1315 +  then have space_P: "space ?P = (\<Pi>\<^isub>E i\<in>I. space (M i))"
  8.1316 +    by (simp add: space_PiM)
  8.1317 +  have "sets (PiM I M) =
  8.1318 +      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)}"
  8.1319 +    using sets_PiM_single[of I M] by (simp add: space_P)
  8.1320 +  also have "\<dots> \<subseteq> sets (sigma (space (PiM I M)) P)"
  8.1321 +  proof (safe intro!: sigma_sets_subset)
  8.1322 +    fix i A assume "i \<in> I" and A: "A \<in> sets (M i)"
  8.1323 +    have "(\<lambda>x. x i) \<in> measurable ?P (sigma (space (M i)) (E i))"
  8.1324 +    proof (subst measurable_iff_measure_of)
  8.1325 +      show "E i \<subseteq> Pow (space (M i))" using `i \<in> I` by fact
  8.1326 +      from space_P `i \<in> I` show "(\<lambda>x. x i) \<in> space ?P \<rightarrow> space (M i)"
  8.1327 +        by (auto simp: Pi_iff)
  8.1328 +      show "\<forall>A\<in>E i. (\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
  8.1329 +      proof
  8.1330 +        fix A assume A: "A \<in> E i"
  8.1331 +        then have "(\<lambda>x. x i) -` A \<inter> space ?P = (\<Pi>\<^isub>E j\<in>I. if i = j then A else space (M j))"
  8.1332 +          using E_closed `i \<in> I` by (auto simp: space_P Pi_iff subset_eq split: split_if_asm)
  8.1333 +        also have "\<dots> = (\<Pi>\<^isub>E j\<in>I. \<Union>n. if i = j then A else S j n)"
  8.1334 +          by (intro PiE_cong) (simp add: S_union)
  8.1335 +        also have "\<dots> = (\<Union>n. \<Pi>\<^isub>E j\<in>I. if i = j then A else S j n)"
  8.1336 +          using S_mono
  8.1337 +          by (subst Pi_UN[symmetric, OF `finite I`]) (auto simp: incseq_def)
  8.1338 +        also have "\<dots> \<in> sets ?P"
  8.1339 +        proof (safe intro!: countable_UN)
  8.1340 +          fix n show "(\<Pi>\<^isub>E j\<in>I. if i = j then A else S j n) \<in> sets ?P"
  8.1341 +            using A S_in_E
  8.1342 +            by (simp add: P_closed)
  8.1343 +               (auto simp: P_def subset_eq intro!: exI[of _ "\<lambda>j. if i = j then A else S j n"])
  8.1344 +        qed
  8.1345 +        finally show "(\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
  8.1346 +          using P_closed by simp
  8.1347 +      qed
  8.1348 +    qed
  8.1349 +    from measurable_sets[OF this, of A] A `i \<in> I` E_closed
  8.1350 +    have "(\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
  8.1351 +      by (simp add: E_generates)
  8.1352 +    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}"
  8.1353 +      using P_closed by (auto simp: space_PiM)
  8.1354 +    finally show "\<dots> \<in> sets ?P" .
  8.1355 +  qed
  8.1356 +  finally show "sets (PiM I M) \<subseteq> sigma_sets (space (PiM I M)) P"
  8.1357 +    by (simp add: P_closed)
  8.1358 +  show "sigma_sets (space (PiM I M)) P \<subseteq> sets (PiM I M)"
  8.1359 +    using `finite I`
  8.1360 +    by (auto intro!: sigma_sets_subset simp: E_generates P_def)
  8.1361 +qed
  8.1362 +
  8.1363 +end
     9.1 --- a/src/HOL/Probability/Independent_Family.thy	Mon Apr 23 12:23:23 2012 +0100
     9.2 +++ b/src/HOL/Probability/Independent_Family.thy	Mon Apr 23 12:14:35 2012 +0200
     9.3 @@ -5,7 +5,7 @@
     9.4  header {* Independent families of events, event sets, and random variables *}
     9.5  
     9.6  theory Independent_Family
     9.7 -  imports Probability_Measure
     9.8 +  imports Probability_Measure Infinite_Product_Measure
     9.9  begin
    9.10  
    9.11  lemma INT_decseq_offset:
    9.12 @@ -44,7 +44,7 @@
    9.13  definition (in prob_space)
    9.14    "indep_var Ma A Mb B \<longleftrightarrow> indep_vars (bool_case Ma Mb) (bool_case A B) UNIV"
    9.15  
    9.16 -lemma (in prob_space) indep_sets_cong[cong]:
    9.17 +lemma (in prob_space) indep_sets_cong:
    9.18    "I = J \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> F i = G i) \<Longrightarrow> indep_sets F I \<longleftrightarrow> indep_sets G J"
    9.19    by (simp add: indep_sets_def, intro conj_cong all_cong imp_cong ball_cong) blast+
    9.20  
    9.21 @@ -135,7 +135,7 @@
    9.22  
    9.23  lemma (in prob_space) indep_sets_dynkin:
    9.24    assumes indep: "indep_sets F I"
    9.25 -  shows "indep_sets (\<lambda>i. sets (dynkin \<lparr> space = space M, sets = F i \<rparr>)) I"
    9.26 +  shows "indep_sets (\<lambda>i. dynkin (space M) (F i)) I"
    9.27      (is "indep_sets ?F I")
    9.28  proof (subst indep_sets_finite_index_sets, intro allI impI ballI)
    9.29    fix J assume "finite J" "J \<subseteq> I" "J \<noteq> {}"
    9.30 @@ -193,7 +193,7 @@
    9.31            qed
    9.32          qed }
    9.33        note indep_sets_insert = this
    9.34 -      have "dynkin_system \<lparr> space = space M, sets = ?D \<rparr>"
    9.35 +      have "dynkin_system (space M) ?D"
    9.36        proof (rule dynkin_systemI', simp_all cong del: indep_sets_cong, safe)
    9.37          show "indep_sets (G(j := {{}})) K"
    9.38            by (rule indep_sets_insert) auto
    9.39 @@ -206,7 +206,7 @@
    9.40              using G by auto
    9.41            have "prob ((\<Inter>j\<in>J. A j) \<inter> (space M - X)) =
    9.42                prob ((\<Inter>j\<in>J. A j) - (\<Inter>i\<in>insert j J. (A(j := X)) i))"
    9.43 -            using A_sets sets_into_space X `J \<noteq> {}`
    9.44 +            using A_sets sets_into_space[of _ M] X `J \<noteq> {}`
    9.45              by (auto intro!: arg_cong[where f=prob] split: split_if_asm)
    9.46            also have "\<dots> = prob (\<Inter>j\<in>J. A j) - prob (\<Inter>i\<in>insert j J. (A(j := X)) i)"
    9.47              using J `J \<noteq> {}` `j \<notin> J` A_sets X sets_into_space
    9.48 @@ -264,9 +264,8 @@
    9.49              by (auto dest!: sums_unique)
    9.50          qed (insert F, auto)
    9.51        qed (insert sets_into_space, auto)
    9.52 -      then have mono: "sets (dynkin \<lparr>space = space M, sets = G j\<rparr>) \<subseteq>
    9.53 -        sets \<lparr>space = space M, sets = {E \<in> events. indep_sets (G(j := {E})) K}\<rparr>"
    9.54 -      proof (rule dynkin_system.dynkin_subset, simp_all cong del: indep_sets_cong, safe)
    9.55 +      then have mono: "dynkin (space M) (G j) \<subseteq> {E \<in> events. indep_sets (G(j := {E})) K}"
    9.56 +      proof (rule dynkin_system.dynkin_subset, safe)
    9.57          fix X assume "X \<in> G j"
    9.58          then show "X \<in> events" using G `j \<in> K` by auto
    9.59          from `indep_sets G K`
    9.60 @@ -292,20 +291,20 @@
    9.61              by (intro indep_setsD[OF G(1)]) auto
    9.62          qed
    9.63        qed
    9.64 -      then have "indep_sets (G(j:=sets (dynkin \<lparr>space = space M, sets = G j\<rparr>))) K"
    9.65 +      then have "indep_sets (G(j := dynkin (space M) (G j))) K"
    9.66          by (rule indep_sets_mono_sets) (insert mono, auto)
    9.67        then show ?case
    9.68          by (rule indep_sets_mono_sets) (insert `j \<in> K` `j \<notin> J`, auto simp: G_def)
    9.69      qed (insert `indep_sets F K`, simp) }
    9.70    from this[OF `indep_sets F J` `finite J` subset_refl]
    9.71 -  show "indep_sets (\<lambda>i. sets (dynkin \<lparr> space = space M, sets = F i \<rparr>)) J"
    9.72 +  show "indep_sets ?F J"
    9.73      by (rule indep_sets_mono_sets) auto
    9.74  qed
    9.75  
    9.76  lemma (in prob_space) indep_sets_sigma:
    9.77    assumes indep: "indep_sets F I"
    9.78 -  assumes stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable \<lparr> space = space M, sets = F i \<rparr>"
    9.79 -  shows "indep_sets (\<lambda>i. sets (sigma \<lparr> space = space M, sets = F i \<rparr>)) I"
    9.80 +  assumes stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (F i)"
    9.81 +  shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
    9.82  proof -
    9.83    from indep_sets_dynkin[OF indep]
    9.84    show ?thesis
    9.85 @@ -316,18 +315,12 @@
    9.86    qed
    9.87  qed
    9.88  
    9.89 -lemma (in prob_space) indep_sets_sigma_sets:
    9.90 -  assumes "indep_sets F I"
    9.91 -  assumes "\<And>i. i \<in> I \<Longrightarrow> Int_stable \<lparr> space = space M, sets = F i \<rparr>"
    9.92 -  shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
    9.93 -  using indep_sets_sigma[OF assms] by (simp add: sets_sigma)
    9.94 -
    9.95  lemma (in prob_space) indep_sets_sigma_sets_iff:
    9.96 -  assumes "\<And>i. i \<in> I \<Longrightarrow> Int_stable \<lparr> space = space M, sets = F i \<rparr>"
    9.97 +  assumes "\<And>i. i \<in> I \<Longrightarrow> Int_stable (F i)"
    9.98    shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I \<longleftrightarrow> indep_sets F I"
    9.99  proof
   9.100    assume "indep_sets F I" then show "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
   9.101 -    by (rule indep_sets_sigma_sets) fact
   9.102 +    by (rule indep_sets_sigma) fact
   9.103  next
   9.104    assume "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I" then show "indep_sets F I"
   9.105      by (rule indep_sets_mono_sets) (intro subsetI sigma_sets.Basic)
   9.106 @@ -361,15 +354,14 @@
   9.107  
   9.108  lemma (in prob_space) indep_set_sigma_sets:
   9.109    assumes "indep_set A B"
   9.110 -  assumes A: "Int_stable \<lparr> space = space M, sets = A \<rparr>"
   9.111 -  assumes B: "Int_stable \<lparr> space = space M, sets = B \<rparr>"
   9.112 +  assumes A: "Int_stable A" and B: "Int_stable B"
   9.113    shows "indep_set (sigma_sets (space M) A) (sigma_sets (space M) B)"
   9.114  proof -
   9.115    have "indep_sets (\<lambda>i. sigma_sets (space M) (case i of True \<Rightarrow> A | False \<Rightarrow> B)) UNIV"
   9.116 -  proof (rule indep_sets_sigma_sets)
   9.117 +  proof (rule indep_sets_sigma)
   9.118      show "indep_sets (bool_case A B) UNIV"
   9.119        by (rule `indep_set A B`[unfolded indep_set_def])
   9.120 -    fix i show "Int_stable \<lparr>space = space M, sets = case i of True \<Rightarrow> A | False \<Rightarrow> B\<rparr>"
   9.121 +    fix i show "Int_stable (case i of True \<Rightarrow> A | False \<Rightarrow> B)"
   9.122        using A B by (cases i) auto
   9.123    qed
   9.124    then show ?thesis
   9.125 @@ -380,7 +372,7 @@
   9.126  lemma (in prob_space) indep_sets_collect_sigma:
   9.127    fixes I :: "'j \<Rightarrow> 'i set" and J :: "'j set" and E :: "'i \<Rightarrow> 'a set set"
   9.128    assumes indep: "indep_sets E (\<Union>j\<in>J. I j)"
   9.129 -  assumes Int_stable: "\<And>i j. j \<in> J \<Longrightarrow> i \<in> I j \<Longrightarrow> Int_stable \<lparr>space = space M, sets = E i\<rparr>"
   9.130 +  assumes Int_stable: "\<And>i j. j \<in> J \<Longrightarrow> i \<in> I j \<Longrightarrow> Int_stable (E i)"
   9.131    assumes disjoint: "disjoint_family_on I J"
   9.132    shows "indep_sets (\<lambda>j. sigma_sets (space M) (\<Union>i\<in>I j. E i)) J"
   9.133  proof -
   9.134 @@ -389,30 +381,29 @@
   9.135    from indep have E: "\<And>j i. j \<in> J \<Longrightarrow> i \<in> I j \<Longrightarrow> E i \<subseteq> events"
   9.136      unfolding indep_sets_def by auto
   9.137    { fix j
   9.138 -    let ?S = "sigma \<lparr> space = space M, sets = (\<Union>i\<in>I j. E i) \<rparr>"
   9.139 +    let ?S = "sigma_sets (space M) (\<Union>i\<in>I j. E i)"
   9.140      assume "j \<in> J"
   9.141 -    from E[OF this] interpret S: sigma_algebra ?S
   9.142 -      using sets_into_space by (intro sigma_algebra_sigma) auto
   9.143 +    from E[OF this] interpret S: sigma_algebra "space M" ?S
   9.144 +      using sets_into_space[of _ M] by (intro sigma_algebra_sigma_sets) auto
   9.145  
   9.146      have "sigma_sets (space M) (\<Union>i\<in>I j. E i) = sigma_sets (space M) (?E j)"
   9.147      proof (rule sigma_sets_eqI)
   9.148        fix A assume "A \<in> (\<Union>i\<in>I j. E i)"
   9.149        then guess i ..
   9.150        then show "A \<in> sigma_sets (space M) (?E j)"
   9.151 -        by (auto intro!: sigma_sets.intros exI[of _ "{i}"] exI[of _ "\<lambda>i. A"])
   9.152 +        by (auto intro!: sigma_sets.intros(2-) exI[of _ "{i}"] exI[of _ "\<lambda>i. A"])
   9.153      next
   9.154        fix A assume "A \<in> ?E j"
   9.155        then obtain E' K where "finite K" "K \<noteq> {}" "K \<subseteq> I j" "\<And>k. k \<in> K \<Longrightarrow> E' k \<in> E k"
   9.156          and A: "A = (\<Inter>k\<in>K. E' k)"
   9.157          by auto
   9.158 -      then have "A \<in> sets ?S" unfolding A
   9.159 -        by (safe intro!: S.finite_INT)
   9.160 -           (auto simp: sets_sigma intro!: sigma_sets.Basic)
   9.161 +      then have "A \<in> ?S" unfolding A
   9.162 +        by (safe intro!: S.finite_INT) auto
   9.163        then show "A \<in> sigma_sets (space M) (\<Union>i\<in>I j. E i)"
   9.164 -        by (simp add: sets_sigma)
   9.165 +        by simp
   9.166      qed }
   9.167    moreover have "indep_sets (\<lambda>j. sigma_sets (space M) (?E j)) J"
   9.168 -  proof (rule indep_sets_sigma_sets)
   9.169 +  proof (rule indep_sets_sigma)
   9.170      show "indep_sets ?E J"
   9.171      proof (intro indep_setsI)
   9.172        fix j assume "j \<in> J" with E show "?E j \<subseteq> events" by (force  intro!: finite_INT)
   9.173 @@ -460,7 +451,7 @@
   9.174      qed
   9.175    next
   9.176      fix j assume "j \<in> J"
   9.177 -    show "Int_stable \<lparr> space = space M, sets = ?E j \<rparr>"
   9.178 +    show "Int_stable (?E j)"
   9.179      proof (rule Int_stableI)
   9.180        fix a assume "a \<in> ?E j" then obtain Ka Ea
   9.181          where a: "a = (\<Inter>k\<in>Ka. Ea k)" "finite Ka" "Ka \<noteq> {}" "Ka \<subseteq> I j" "\<And>k. k\<in>Ka \<Longrightarrow> Ea k \<in> E k" by auto
   9.182 @@ -482,12 +473,12 @@
   9.183  
   9.184  lemma (in prob_space) terminal_events_sets:
   9.185    assumes A: "\<And>i. A i \<subseteq> events"
   9.186 -  assumes "\<And>i::nat. sigma_algebra \<lparr>space = space M, sets = A i\<rparr>"
   9.187 +  assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   9.188    assumes X: "X \<in> terminal_events A"
   9.189    shows "X \<in> events"
   9.190  proof -
   9.191    let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   9.192 -  interpret A: sigma_algebra "\<lparr>space = space M, sets = A i\<rparr>" for i by fact
   9.193 +  interpret A: sigma_algebra "space M" "A i" for i by fact
   9.194    from X have "\<And>n. X \<in> sigma_sets (space M) (UNION {n..} A)" by (auto simp: terminal_events_def)
   9.195    from this[of 0] have "X \<in> sigma_sets (space M) (UNION UNIV A)" by simp
   9.196    then show "X \<in> events"
   9.197 @@ -495,12 +486,12 @@
   9.198  qed
   9.199  
   9.200  lemma (in prob_space) sigma_algebra_terminal_events:
   9.201 -  assumes "\<And>i::nat. sigma_algebra \<lparr>space = space M, sets = A i\<rparr>"
   9.202 -  shows "sigma_algebra \<lparr> space = space M, sets = terminal_events A \<rparr>"
   9.203 +  assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   9.204 +  shows "sigma_algebra (space M) (terminal_events A)"
   9.205    unfolding terminal_events_def
   9.206  proof (simp add: sigma_algebra_iff2, safe)
   9.207    let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
   9.208 -  interpret A: sigma_algebra "\<lparr>space = space M, sets = A i\<rparr>" for i by fact
   9.209 +  interpret A: sigma_algebra "space M" "A i" for i by fact
   9.210    { fix X x assume "X \<in> ?A" "x \<in> X"
   9.211      then have "\<And>n. X \<in> sigma_sets (space M) (UNION {n..} A)" by auto
   9.212      from this[of 0] have "X \<in> sigma_sets (space M) (UNION UNIV A)" by simp
   9.213 @@ -515,29 +506,29 @@
   9.214  lemma (in prob_space) kolmogorov_0_1_law:
   9.215    fixes A :: "nat \<Rightarrow> 'a set set"
   9.216    assumes A: "\<And>i. A i \<subseteq> events"
   9.217 -  assumes "\<And>i::nat. sigma_algebra \<lparr>space = space M, sets = A i\<rparr>"
   9.218 +  assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   9.219    assumes indep: "indep_sets A UNIV"
   9.220    and X: "X \<in> terminal_events A"
   9.221    shows "prob X = 0 \<or> prob X = 1"
   9.222  proof -
   9.223 -  let ?D = "\<lparr> space = space M, sets = {D \<in> events. prob (X \<inter> D) = prob X * prob D} \<rparr>"
   9.224 -  interpret A: sigma_algebra "\<lparr>space = space M, sets = A i\<rparr>" for i by fact
   9.225 -  interpret T: sigma_algebra "\<lparr> space = space M, sets = terminal_events A \<rparr>"
   9.226 +  let ?D = "{D \<in> events. prob (X \<inter> D) = prob X * prob D}"
   9.227 +  interpret A: sigma_algebra "space M" "A i" for i by fact
   9.228 +  interpret T: sigma_algebra "space M" "terminal_events A"
   9.229      by (rule sigma_algebra_terminal_events) fact
   9.230    have "X \<subseteq> space M" using T.space_closed X by auto
   9.231  
   9.232    have X_in: "X \<in> events"
   9.233      by (rule terminal_events_sets) fact+
   9.234  
   9.235 -  interpret D: dynkin_system ?D
   9.236 +  interpret D: dynkin_system "space M" ?D
   9.237    proof (rule dynkin_systemI)
   9.238 -    fix D assume "D \<in> sets ?D" then show "D \<subseteq> space ?D"
   9.239 +    fix D assume "D \<in> ?D" then show "D \<subseteq> space M"
   9.240        using sets_into_space by auto
   9.241    next
   9.242 -    show "space ?D \<in> sets ?D"
   9.243 +    show "space M \<in> ?D"
   9.244        using prob_space `X \<subseteq> space M` by (simp add: Int_absorb2)
   9.245    next
   9.246 -    fix A assume A: "A \<in> sets ?D"
   9.247 +    fix A assume A: "A \<in> ?D"
   9.248      have "prob (X \<inter> (space M - A)) = prob (X - (X \<inter> A))"
   9.249        using `X \<subseteq> space M` by (auto intro!: arg_cong[where f=prob])
   9.250      also have "\<dots> = prob X - prob (X \<inter> A)"
   9.251 @@ -547,10 +538,10 @@
   9.252      also have "\<dots> = prob X * prob (space M - A)"
   9.253        using X_in A sets_into_space
   9.254        by (subst finite_measure_Diff) (auto simp: field_simps)
   9.255 -    finally show "space ?D - A \<in> sets ?D"
   9.256 +    finally show "space M - A \<in> ?D"
   9.257        using A `X \<subseteq> space M` by auto
   9.258    next
   9.259 -    fix F :: "nat \<Rightarrow> 'a set" assume dis: "disjoint_family F" and "range F \<subseteq> sets ?D"
   9.260 +    fix F :: "nat \<Rightarrow> 'a set" assume dis: "disjoint_family F" and "range F \<subseteq> ?D"
   9.261      then have F: "range F \<subseteq> events" "\<And>i. prob (X \<inter> F i) = prob X * prob (F i)"
   9.262        by auto
   9.263      have "(\<lambda>i. prob (X \<inter> F i)) sums prob (\<Union>i. X \<inter> F i)"
   9.264 @@ -566,7 +557,7 @@
   9.265        by (intro sums_mult finite_measure_UNION F dis)
   9.266      ultimately have "prob (X \<inter> (\<Union>i. F i)) = prob X * prob (\<Union>i. F i)"
   9.267        by (auto dest!: sums_unique)
   9.268 -    with F show "(\<Union>i. F i) \<in> sets ?D"
   9.269 +    with F show "(\<Union>i. F i) \<in> ?D"
   9.270        by auto
   9.271    qed
   9.272  
   9.273 @@ -579,7 +570,7 @@
   9.274        show "disjoint_family (bool_case {..n} {Suc n..})"
   9.275          unfolding disjoint_family_on_def by (auto split: bool.split)
   9.276        fix m
   9.277 -      show "Int_stable \<lparr>space = space M, sets = A m\<rparr>"
   9.278 +      show "Int_stable (A m)"
   9.279          unfolding Int_stable_def using A.Int by auto
   9.280      qed
   9.281      also have "(\<lambda>b. sigma_sets (space M) (\<Union>m\<in>bool_case {..n} {Suc n..} b. A m)) =
   9.282 @@ -588,7 +579,7 @@
   9.283      finally have indep: "indep_set (sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) (sigma_sets (space M) (\<Union>m\<in>{Suc n..}. A m))"
   9.284        unfolding indep_set_def by simp
   9.285  
   9.286 -    have "sigma_sets (space M) (\<Union>m\<in>{..n}. A m) \<subseteq> sets ?D"
   9.287 +    have "sigma_sets (space M) (\<Union>m\<in>{..n}. A m) \<subseteq> ?D"
   9.288      proof (simp add: subset_eq, rule)
   9.289        fix D assume D: "D \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
   9.290        have "X \<in> sigma_sets (space M) (\<Union>m\<in>{Suc n..}. A m)"
   9.291 @@ -597,22 +588,27 @@
   9.292        show "D \<in> events \<and> prob (X \<inter> D) = prob X * prob D"
   9.293          by (auto simp add: ac_simps)
   9.294      qed }
   9.295 -  then have "(\<Union>n. sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) \<subseteq> sets ?D" (is "?A \<subseteq> _")
   9.296 +  then have "(\<Union>n. sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) \<subseteq> ?D" (is "?A \<subseteq> _")
   9.297      by auto
   9.298  
   9.299 -  have "sigma \<lparr> space = space M, sets = ?A \<rparr> =
   9.300 -    dynkin \<lparr> space = space M, sets = ?A \<rparr>" (is "sigma ?UA = dynkin ?UA")
   9.301 +  note `X \<in> terminal_events A`
   9.302 +  also {
   9.303 +    have "\<And>n. sigma_sets (space M) (\<Union>i\<in>{n..}. A i) \<subseteq> sigma_sets (space M) ?A"
   9.304 +      by (intro sigma_sets_subseteq UN_mono) auto
   9.305 +   then have "terminal_events A \<subseteq> sigma_sets (space M) ?A"
   9.306 +      unfolding terminal_events_def by auto }
   9.307 +  also have "sigma_sets (space M) ?A = dynkin (space M) ?A"
   9.308    proof (rule sigma_eq_dynkin)
   9.309      { fix B n assume "B \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
   9.310        then have "B \<subseteq> space M"
   9.311 -        by induct (insert A sets_into_space, auto) }
   9.312 -    then show "sets ?UA \<subseteq> Pow (space ?UA)" by auto
   9.313 -    show "Int_stable ?UA"
   9.314 +        by induct (insert A sets_into_space[of _ M], auto) }
   9.315 +    then show "?A \<subseteq> Pow (space M)" by auto
   9.316 +    show "Int_stable ?A"
   9.317      proof (rule Int_stableI)
   9.318        fix a assume "a \<in> ?A" then guess n .. note a = this
   9.319        fix b assume "b \<in> ?A" then guess m .. note b = this
   9.320 -      interpret Amn: sigma_algebra "sigma \<lparr>space = space M, sets = (\<Union>i\<in>{..max m n}. A i)\<rparr>"
   9.321 -        using A sets_into_space by (intro sigma_algebra_sigma) auto
   9.322 +      interpret Amn: sigma_algebra "space M" "sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   9.323 +        using A sets_into_space[of _ M] by (intro sigma_algebra_sigma_sets) auto
   9.324        have "sigma_sets (space M) (\<Union>i\<in>{..n}. A i) \<subseteq> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   9.325          by (intro sigma_sets_subseteq UN_mono) auto
   9.326        with a have "a \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)" by auto
   9.327 @@ -621,23 +617,13 @@
   9.328          by (intro sigma_sets_subseteq UN_mono) auto
   9.329        with b have "b \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)" by auto
   9.330        ultimately have "a \<inter> b \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
   9.331 -        using Amn.Int[of a b] by (simp add: sets_sigma)
   9.332 +        using Amn.Int[of a b] by simp
   9.333        then show "a \<inter> b \<in> (\<Union>n. sigma_sets (space M) (\<Union>i\<in>{..n}. A i))" by auto
   9.334      qed
   9.335    qed
   9.336 -  moreover have "sets (dynkin ?UA) \<subseteq> sets ?D"
   9.337 -  proof (rule D.dynkin_subset)
   9.338 -    show "sets ?UA \<subseteq> sets ?D" using `?A \<subseteq> sets ?D` by auto
   9.339 -  qed simp
   9.340 -  ultimately have "sets (sigma ?UA) \<subseteq> sets ?D" by simp
   9.341 -  moreover
   9.342 -  have "\<And>n. sigma_sets (space M) (\<Union>i\<in>{n..}. A i) \<subseteq> sigma_sets (space M) ?A"
   9.343 -    by (intro sigma_sets_subseteq UN_mono) (auto intro: sigma_sets.Basic)
   9.344 -  then have "terminal_events A \<subseteq> sets (sigma ?UA)"
   9.345 -    unfolding sets_sigma terminal_events_def by auto
   9.346 -  moreover note `X \<in> terminal_events A`
   9.347 -  ultimately have "X \<in> sets ?D" by auto
   9.348 -  then show ?thesis by auto
   9.349 +  also have "dynkin (space M) ?A \<subseteq> ?D"
   9.350 +    using `?A \<subseteq> ?D` by (auto intro!: D.dynkin_subset)
   9.351 +  finally show ?thesis by auto
   9.352  qed
   9.353  
   9.354  lemma (in prob_space) borel_0_1_law:
   9.355 @@ -648,14 +634,14 @@
   9.356    show "\<And>i. sigma_sets (space M) {F i} \<subseteq> events"
   9.357      using F(1) sets_into_space
   9.358      by (subst sigma_sets_singleton) auto
   9.359 -  { fix i show "sigma_algebra \<lparr>space = space M, sets = sigma_sets (space M) {F i}\<rparr>"
   9.360 -      using sigma_algebra_sigma[of "\<lparr>space = space M, sets = {F i}\<rparr>"] F sets_into_space
   9.361 -      by (auto simp add: sigma_def) }
   9.362 +  { fix i show "sigma_algebra (space M) (sigma_sets (space M) {F i})"
   9.363 +      using sigma_algebra_sigma_sets[of "{F i}" "space M"] F sets_into_space
   9.364 +      by auto }
   9.365    show "indep_sets (\<lambda>i. sigma_sets (space M) {F i}) UNIV"
   9.366 -  proof (rule indep_sets_sigma_sets)
   9.367 +  proof (rule indep_sets_sigma)
   9.368      show "indep_sets (\<lambda>i. {F i}) UNIV"
   9.369        unfolding indep_sets_singleton_iff_indep_events by fact
   9.370 -    fix i show "Int_stable \<lparr>space = space M, sets = {F i}\<rparr>"
   9.371 +    fix i show "Int_stable {F i}"
   9.372        unfolding Int_stable_def by simp
   9.373    qed
   9.374    let ?Q = "\<lambda>n. \<Union>i\<in>{n..}. F i"
   9.375 @@ -663,17 +649,17 @@
   9.376      unfolding terminal_events_def
   9.377    proof
   9.378      fix j
   9.379 -    interpret S: sigma_algebra "sigma \<lparr> space = space M, sets = (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})\<rparr>"
   9.380 +    interpret S: sigma_algebra "space M" "sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   9.381        using order_trans[OF F(1) space_closed]
   9.382 -      by (intro sigma_algebra_sigma) (simp add: sigma_sets_singleton subset_eq)
   9.383 +      by (intro sigma_algebra_sigma_sets) (simp add: sigma_sets_singleton subset_eq)
   9.384      have "(\<Inter>n. ?Q n) = (\<Inter>n\<in>{j..}. ?Q n)"
   9.385        by (intro decseq_SucI INT_decseq_offset UN_mono) auto
   9.386 -    also have "\<dots> \<in> sets (sigma \<lparr> space = space M, sets = (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})\<rparr>)"
   9.387 +    also have "\<dots> \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   9.388        using order_trans[OF F(1) space_closed]
   9.389        by (safe intro!: S.countable_INT S.countable_UN)
   9.390 -         (auto simp: sets_sigma sigma_sets_singleton intro!: sigma_sets.Basic bexI)
   9.391 +         (auto simp: sigma_sets_singleton intro!: sigma_sets.Basic bexI)
   9.392      finally show "(\<Inter>n. ?Q n) \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
   9.393 -      by (simp add: sets_sigma)
   9.394 +      by simp
   9.395    qed
   9.396  qed
   9.397  
   9.398 @@ -710,84 +696,84 @@
   9.399  lemma (in prob_space) indep_vars_finite:
   9.400    fixes I :: "'i set"
   9.401    assumes I: "I \<noteq> {}" "finite I"
   9.402 -    and rv: "\<And>i. i \<in> I \<Longrightarrow> random_variable (sigma (M' i)) (X i)"
   9.403 -    and Int_stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (M' i)"
   9.404 -    and space: "\<And>i. i \<in> I \<Longrightarrow> space (M' i) \<in> sets (M' i)"
   9.405 -  shows "indep_vars (\<lambda>i. sigma (M' i)) X I \<longleftrightarrow>
   9.406 -    (\<forall>A\<in>(\<Pi> i\<in>I. sets (M' i)). prob (\<Inter>j\<in>I. X j -` A j \<inter> space M) = (\<Prod>j\<in>I. prob (X j -` A j \<inter> space M)))"
   9.407 +    and M': "\<And>i. i \<in> I \<Longrightarrow> sets (M' i) = sigma_sets (space (M' i)) (E i)"
   9.408 +    and rv: "\<And>i. i \<in> I \<Longrightarrow> random_variable (M' i) (X i)"
   9.409 +    and Int_stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (E i)"
   9.410 +    and space: "\<And>i. i \<in> I \<Longrightarrow> space (M' i) \<in> E i" and closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M' i))"
   9.411 +  shows "indep_vars M' X I \<longleftrightarrow>
   9.412 +    (\<forall>A\<in>(\<Pi> i\<in>I. E i). prob (\<Inter>j\<in>I. X j -` A j \<inter> space M) = (\<Prod>j\<in>I. prob (X j -` A j \<inter> space M)))"
   9.413  proof -
   9.414    from rv have X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> space M \<rightarrow> space (M' i)"
   9.415      unfolding measurable_def by simp
   9.416  
   9.417    { fix i assume "i\<in>I"
   9.418 -    have "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (sigma (M' i))}
   9.419 -      = sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   9.420 -      unfolding sigma_sets_vimage_commute[OF X, OF `i \<in> I`]
   9.421 +    from closed[OF `i \<in> I`]
   9.422 +    have "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}
   9.423 +      = sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> E i}"
   9.424 +      unfolding sigma_sets_vimage_commute[OF X, OF `i \<in> I`, symmetric] M'[OF `i \<in> I`]
   9.425        by (subst sigma_sets_sigma_sets_eq) auto }
   9.426 -  note this[simp]
   9.427 +  note sigma_sets_X = this
   9.428  
   9.429    { fix i assume "i\<in>I"
   9.430 -    have "Int_stable \<lparr>space = space M, sets = {X i -` A \<inter> space M |A. A \<in> sets (M' i)}\<rparr>"
   9.431 +    have "Int_stable {X i -` A \<inter> space M |A. A \<in> E i}"
   9.432      proof (rule Int_stableI)
   9.433 -      fix a assume "a \<in> {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   9.434 -      then obtain A where "a = X i -` A \<inter> space M" "A \<in> sets (M' i)" by auto
   9.435 +      fix a assume "a \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   9.436 +      then obtain A where "a = X i -` A \<inter> space M" "A \<in> E i" by auto
   9.437        moreover
   9.438 -      fix b assume "b \<in> {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   9.439 -      then obtain B where "b = X i -` B \<inter> space M" "B \<in> sets (M' i)" by auto
   9.440 +      fix b assume "b \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   9.441 +      then obtain B where "b = X i -` B \<inter> space M" "B \<in> E i" by auto
   9.442        moreover
   9.443        have "(X i -` A \<inter> space M) \<inter> (X i -` B \<inter> space M) = X i -` (A \<inter> B) \<inter> space M" by auto
   9.444        moreover note Int_stable[OF `i \<in> I`]
   9.445        ultimately
   9.446 -      show "a \<inter> b \<in> {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   9.447 +      show "a \<inter> b \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   9.448          by (auto simp del: vimage_Int intro!: exI[of _ "A \<inter> B"] dest: Int_stableD)
   9.449      qed }
   9.450 -  note indep_sets_sigma_sets_iff[OF this, simp]
   9.451 +  note indep_sets_X = indep_sets_sigma_sets_iff[OF this]
   9.452  
   9.453    { fix i assume "i \<in> I"
   9.454 -    { fix A assume "A \<in> sets (M' i)"
   9.455 -      then have "A \<in> sets (sigma (M' i))" by (auto simp: sets_sigma intro: sigma_sets.Basic)
   9.456 +    { fix A assume "A \<in> E i"
   9.457 +      with M'[OF `i \<in> I`] have "A \<in> sets (M' i)" by auto
   9.458        moreover
   9.459 -      from rv[OF `i\<in>I`] have "X i \<in> measurable M (sigma (M' i))" by auto
   9.460 +      from rv[OF `i\<in>I`] have "X i \<in> measurable M (M' i)" by auto
   9.461        ultimately
   9.462        have "X i -` A \<inter> space M \<in> sets M" by (auto intro: measurable_sets) }
   9.463      with X[OF `i\<in>I`] space[OF `i\<in>I`]
   9.464 -    have "{X i -` A \<inter> space M |A. A \<in> sets (M' i)} \<subseteq> events"
   9.465 -      "space M \<in> {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   9.466 +    have "{X i -` A \<inter> space M |A. A \<in> E i} \<subseteq> events"
   9.467 +      "space M \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
   9.468        by (auto intro!: exI[of _ "space (M' i)"]) }
   9.469 -  note indep_sets_finite[OF I this, simp]
   9.470 +  note indep_sets_finite_X = indep_sets_finite[OF I this]
   9.471  
   9.472 -  have "(\<forall>A\<in>\<Pi> i\<in>I. {X i -` A \<inter> space M |A. A \<in> sets (M' i)}. prob (INTER I A) = (\<Prod>j\<in>I. prob (A j))) =
   9.473 -    (\<forall>A\<in>\<Pi> i\<in>I. sets (M' i). prob ((\<Inter>j\<in>I. X j -` A j) \<inter> space M) = (\<Prod>x\<in>I. prob (X x -` A x \<inter> space M)))"
   9.474 +  have "(\<forall>A\<in>\<Pi> i\<in>I. {X i -` A \<inter> space M |A. A \<in> E i}. prob (INTER I A) = (\<Prod>j\<in>I. prob (A j))) =
   9.475 +    (\<forall>A\<in>\<Pi> i\<in>I. E i. prob ((\<Inter>j\<in>I. X j -` A j) \<inter> space M) = (\<Prod>x\<in>I. prob (X x -` A x \<inter> space M)))"
   9.476      (is "?L = ?R")
   9.477    proof safe
   9.478 -    fix A assume ?L and A: "A \<in> (\<Pi> i\<in>I. sets (M' i))"
   9.479 +    fix A assume ?L and A: "A \<in> (\<Pi> i\<in>I. E i)"
   9.480      from `?L`[THEN bspec, of "\<lambda>i. X i -` A i \<inter> space M"] A `I \<noteq> {}`
   9.481      show "prob ((\<Inter>j\<in>I. X j -` A j) \<inter> space M) = (\<Prod>x\<in>I. prob (X x -` A x \<inter> space M))"
   9.482        by (auto simp add: Pi_iff)
   9.483    next
   9.484 -    fix A assume ?R and A: "A \<in> (\<Pi> i\<in>I. {X i -` A \<inter> space M |A. A \<in> sets (M' i)})"
   9.485 -    from A have "\<forall>i\<in>I. \<exists>B. A i = X i -` B \<inter> space M \<and> B \<in> sets (M' i)" by auto
   9.486 +    fix A assume ?R and A: "A \<in> (\<Pi> i\<in>I. {X i -` A \<inter> space M |A. A \<in> E i})"
   9.487 +    from A have "\<forall>i\<in>I. \<exists>B. A i = X i -` B \<inter> space M \<and> B \<in> E i" by auto
   9.488      from bchoice[OF this] obtain B where B: "\<forall>i\<in>I. A i = X i -` B i \<inter> space M"
   9.489 -      "B \<in> (\<Pi> i\<in>I. sets (M' i))" by auto
   9.490 +      "B \<in> (\<Pi> i\<in>I. E i)" by auto
   9.491      from `?R`[THEN bspec, OF B(2)] B(1) `I \<noteq> {}`
   9.492      show "prob (INTER I A) = (\<Prod>j\<in>I. prob (A j))"
   9.493        by simp
   9.494    qed
   9.495    then show ?thesis using `I \<noteq> {}`
   9.496 -    by (simp add: rv indep_vars_def)
   9.497 +    by (simp add: rv indep_vars_def indep_sets_X sigma_sets_X indep_sets_finite_X cong: indep_sets_cong)
   9.498  qed
   9.499  
   9.500  lemma (in prob_space) indep_vars_compose:
   9.501    assumes "indep_vars M' X I"
   9.502 -  assumes rv:
   9.503 -    "\<And>i. i \<in> I \<Longrightarrow> sigma_algebra (N i)"
   9.504 -    "\<And>i. i \<in> I \<Longrightarrow> Y i \<in> measurable (M' i) (N i)"
   9.505 +  assumes rv: "\<And>i. i \<in> I \<Longrightarrow> Y i \<in> measurable (M' i) (N i)"
   9.506    shows "indep_vars N (\<lambda>i. Y i \<circ> X i) I"
   9.507    unfolding indep_vars_def
   9.508  proof
   9.509    from rv `indep_vars M' X I`
   9.510    show "\<forall>i\<in>I. random_variable (N i) (Y i \<circ> X i)"
   9.511 -    by (auto intro!: measurable_comp simp: indep_vars_def)
   9.512 +    by (auto simp: indep_vars_def)
   9.513  
   9.514    have "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
   9.515      using `indep_vars M' X I` by (simp add: indep_vars_def)
   9.516 @@ -806,7 +792,7 @@
   9.517    qed
   9.518  qed
   9.519  
   9.520 -lemma (in prob_space) indep_varsD:
   9.521 +lemma (in prob_space) indep_varsD_finite:
   9.522    assumes X: "indep_vars M' X I"
   9.523    assumes I: "I \<noteq> {}" "finite I" "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M' i)"
   9.524    shows "prob (\<Inter>i\<in>I. X i -` A i \<inter> space M) = (\<Prod>i\<in>I. prob (X i -` A i \<inter> space M))"
   9.525 @@ -815,96 +801,134 @@
   9.526      using X by (auto simp: indep_vars_def)
   9.527    show "I \<subseteq> I" "I \<noteq> {}" "finite I" using I by auto
   9.528    show "\<forall>i\<in>I. X i -` A i \<inter> space M \<in> sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   9.529 -    using I by (auto intro: sigma_sets.Basic)
   9.530 +    using I by auto
   9.531  qed
   9.532  
   9.533 -lemma (in prob_space) indep_distribution_eq_measure:
   9.534 -  assumes I: "I \<noteq> {}" "finite I"
   9.535 +lemma (in prob_space) indep_varsD:
   9.536 +  assumes X: "indep_vars M' X I"
   9.537 +  assumes I: "J \<noteq> {}" "finite J" "J \<subseteq> I" "\<And>i. i \<in> J \<Longrightarrow> A i \<in> sets (M' i)"
   9.538 +  shows "prob (\<Inter>i\<in>J. X i -` A i \<inter> space M) = (\<Prod>i\<in>J. prob (X i -` A i \<inter> space M))"
   9.539 +proof (rule indep_setsD)
   9.540 +  show "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
   9.541 +    using X by (auto simp: indep_vars_def)
   9.542 +  show "\<forall>i\<in>J. X i -` A i \<inter> space M \<in> sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
   9.543 +    using I by auto
   9.544 +qed fact+
   9.545 +
   9.546 +lemma prod_algebra_cong:
   9.547 +  assumes "I = J" and sets: "(\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets (N i))"
   9.548 +  shows "prod_algebra I M = prod_algebra J N"
   9.549 +proof -
   9.550 +  have space: "\<And>i. i \<in> I \<Longrightarrow> space (M i) = space (N i)"
   9.551 +    using sets_eq_imp_space_eq[OF sets] by auto
   9.552 +  with sets show ?thesis unfolding `I = J`
   9.553 +    by (intro antisym prod_algebra_mono) auto
   9.554 +qed
   9.555 +
   9.556 +lemma space_in_prod_algebra:
   9.557 +  "(\<Pi>\<^isub>E i\<in>I. space (M i)) \<in> prod_algebra I M"
   9.558 +proof cases
   9.559 +  assume "I = {}" then show ?thesis
   9.560 +    by (auto simp add: prod_algebra_def image_iff prod_emb_def)
   9.561 +next
   9.562 +  assume "I \<noteq> {}"
   9.563 +  then obtain i where "i \<in> I" by auto
   9.564 +  then have "(\<Pi>\<^isub>E i\<in>I. space (M i)) = prod_emb I M {i} (\<Pi>\<^isub>E i\<in>{i}. space (M i))"
   9.565 +    by (auto simp: prod_emb_def Pi_iff)
   9.566 +  also have "\<dots> \<in> prod_algebra I M"
   9.567 +    using `i \<in> I` by (intro prod_algebraI) auto
   9.568 +  finally show ?thesis .
   9.569 +qed
   9.570 +
   9.571 +lemma (in prob_space) indep_vars_iff_distr_eq_PiM:
   9.572 +  fixes I :: "'i set" and X :: "'i \<Rightarrow> 'a \<Rightarrow> 'b"
   9.573 +  assumes "I \<noteq> {}"
   9.574    assumes rv: "\<And>i. random_variable (M' i) (X i)"
   9.575    shows "indep_vars M' X I \<longleftrightarrow>
   9.576 -    (\<forall>A\<in>sets (\<Pi>\<^isub>M i\<in>I. (M' i \<lparr> measure := ereal\<circ>distribution (X i) \<rparr>)).
   9.577 -      distribution (\<lambda>x. \<lambda>i\<in>I. X i x) A =
   9.578 -      finite_measure.\<mu>' (\<Pi>\<^isub>M i\<in>I. (M' i \<lparr> measure := ereal\<circ>distribution (X i) \<rparr>)) A)"
   9.579 -    (is "_ \<longleftrightarrow> (\<forall>X\<in>_. distribution ?D X = finite_measure.\<mu>' (Pi\<^isub>M I ?M) X)")
   9.580 +    distr M (\<Pi>\<^isub>M i\<in>I. M' i) (\<lambda>x. \<lambda>i\<in>I. X i x) = (\<Pi>\<^isub>M i\<in>I. distr M (M' i) (X i))"
   9.581  proof -
   9.582 -  interpret M': prob_space "?M i" for i
   9.583 -    using rv by (rule distribution_prob_space)
   9.584 -  interpret P: finite_product_prob_space ?M I
   9.585 -    proof qed fact
   9.586 +  let ?P = "\<Pi>\<^isub>M i\<in>I. M' i"
   9.587 +  let ?X = "\<lambda>x. \<lambda>i\<in>I. X i x"
   9.588 +  let ?D = "distr M ?P ?X"
   9.589 +  have X: "random_variable ?P ?X" by (intro measurable_restrict rv)
   9.590 +  interpret D: prob_space ?D by (intro prob_space_distr X)
   9.591  
   9.592 -  let ?D' = "(Pi\<^isub>M I ?M) \<lparr> measure := ereal \<circ> distribution ?D \<rparr>"
   9.593 -  have "random_variable P.P ?D"
   9.594 -    using `finite I` rv by (intro random_variable_restrict) auto
   9.595 -  then interpret D: prob_space ?D'
   9.596 -    by (rule distribution_prob_space)
   9.597 -
   9.598 +  let ?D' = "\<lambda>i. distr M (M' i) (X i)"
   9.599 +  let ?P' = "\<Pi>\<^isub>M i\<in>I. distr M (M' i) (X i)"
   9.600 +  interpret D': prob_space "?D' i" for i by (intro prob_space_distr rv)
   9.601 +  interpret P: product_prob_space ?D' I ..
   9.602 +    
   9.603    show ?thesis
   9.604 -  proof (intro iffI ballI)
   9.605 +  proof
   9.606      assume "indep_vars M' X I"
   9.607 -    fix A assume "A \<in> sets P.P"
   9.608 -    moreover
   9.609 -    have "D.prob A = P.prob A"
   9.610 -    proof (rule prob_space_unique_Int_stable)
   9.611 -      show "prob_space ?D'" by unfold_locales
   9.612 -      show "prob_space (Pi\<^isub>M I ?M)" by unfold_locales
   9.613 -      show "Int_stable P.G" using M'.Int
   9.614 -        by (intro Int_stable_product_algebra_generator) (simp add: Int_stable_def)
   9.615 -      show "space P.G \<in> sets P.G"
   9.616 -        using M'.top by (simp add: product_algebra_generator_def)
   9.617 -      show "space ?D' = space P.G"  "sets ?D' = sets (sigma P.G)"
   9.618 -        by (simp_all add: product_algebra_def product_algebra_generator_def sets_sigma)
   9.619 -      show "space P.P = space P.G" "sets P.P = sets (sigma P.G)"
   9.620 -        by (simp_all add: product_algebra_def)
   9.621 -      show "A \<in> sets (sigma P.G)"
   9.622 -        using `A \<in> sets P.P` by (simp add: product_algebra_def)
   9.623 +    show "?D = ?P'"
   9.624 +    proof (rule measure_eqI_generator_eq)
   9.625 +      show "Int_stable (prod_algebra I M')"
   9.626 +        by (rule Int_stable_prod_algebra)
   9.627 +      show "prod_algebra I M' \<subseteq> Pow (space ?P)"
   9.628 +        using prod_algebra_sets_into_space by (simp add: space_PiM)
   9.629 +      show "sets ?D = sigma_sets (space ?P) (prod_algebra I M')"
   9.630 +        by (simp add: sets_PiM space_PiM)
   9.631 +      show "sets ?P' = sigma_sets (space ?P) (prod_algebra I M')"
   9.632 +        by (simp add: sets_PiM space_PiM cong: prod_algebra_cong)
   9.633 +      let ?A = "\<lambda>i. \<Pi>\<^isub>E i\<in>I. space (M' i)"
   9.634 +      show "range ?A \<subseteq> prod_algebra I M'" "incseq ?A" "(\<Union>i. ?A i) = space (Pi\<^isub>M I M')"
   9.635 +        by (auto simp: space_PiM intro!: space_in_prod_algebra cong: prod_algebra_cong)
   9.636 +      { fix i show "emeasure ?D (\<Pi>\<^isub>E i\<in>I. space (M' i)) \<noteq> \<infinity>" by auto }
   9.637 +    next
   9.638 +      fix E assume E: "E \<in> prod_algebra I M'"
   9.639 +      from prod_algebraE[OF E] guess J Y . note J = this
   9.640  
   9.641 -      fix E assume E: "E \<in> sets P.G"
   9.642 -      then have "E \<in> sets P.P"
   9.643 -        by (simp add: sets_sigma sigma_sets.Basic product_algebra_def)
   9.644 -      then have "D.prob E = distribution ?D E"
   9.645 -        unfolding D.\<mu>'_def by simp
   9.646 -      also
   9.647 -      from E obtain F where "E = Pi\<^isub>E I F" and F: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets (M' i)"
   9.648 -        by (auto simp: product_algebra_generator_def)
   9.649 -      with `I \<noteq> {}` have "distribution ?D E = prob (\<Inter>i\<in>I. X i -` F i \<inter> space M)"
   9.650 -        using `I \<noteq> {}` by (auto intro!: arg_cong[where f=prob] simp: Pi_iff distribution_def)
   9.651 -      also have "\<dots> = (\<Prod>i\<in>I. prob (X i -` F i \<inter> space M))"
   9.652 -        using `indep_vars M' X I` I F by (rule indep_varsD)
   9.653 -      also have "\<dots> = P.prob E"
   9.654 -        using F by (simp add: `E = Pi\<^isub>E I F` P.prob_times M'.\<mu>'_def distribution_def)
   9.655 -      finally show "D.prob E = P.prob E" .
   9.656 +      from E have "E \<in> sets ?P" by (auto simp: sets_PiM)
   9.657 +      then have "emeasure ?D E = emeasure M (?X -` E \<inter> space M)"
   9.658 +        by (simp add: emeasure_distr X)
   9.659 +      also have "?X -` E \<inter> space M = (\<Inter>i\<in>J. X i -` Y i \<inter> space M)"
   9.660 +        using J `I \<noteq> {}` measurable_space[OF rv] by (auto simp: prod_emb_def Pi_iff split: split_if_asm)
   9.661 +      also have "emeasure M (\<Inter>i\<in>J. X i -` Y i \<inter> space M) = (\<Prod> i\<in>J. emeasure M (X i -` Y i \<inter> space M))"
   9.662 +        using `indep_vars M' X I` J `I \<noteq> {}` using indep_varsD[of M' X I J]
   9.663 +        by (auto simp: emeasure_eq_measure setprod_ereal)
   9.664 +      also have "\<dots> = (\<Prod> i\<in>J. emeasure (?D' i) (Y i))"
   9.665 +        using rv J by (simp add: emeasure_distr)
   9.666 +      also have "\<dots> = emeasure ?P' E"
   9.667 +        using P.emeasure_PiM_emb[of J Y] J by (simp add: prod_emb_def)
   9.668 +      finally show "emeasure ?D E = emeasure ?P' E" .
   9.669      qed
   9.670 -    ultimately show "distribution ?D A = P.prob A"
   9.671 -      by (simp add: D.\<mu>'_def)
   9.672    next
   9.673 -    assume eq: "\<forall>A\<in>sets P.P. distribution ?D A = P.prob A"
   9.674 -    have [simp]: "\<And>i. sigma (M' i) = M' i"
   9.675 -      using rv by (intro sigma_algebra.sigma_eq) simp
   9.676 -    have "indep_vars (\<lambda>i. sigma (M' i)) X I"
   9.677 -    proof (subst indep_vars_finite[OF I])
   9.678 -      fix i assume [simp]: "i \<in> I"
   9.679 -      show "random_variable (sigma (M' i)) (X i)"
   9.680 -        using rv[of i] by simp
   9.681 -      show "Int_stable (M' i)" "space (M' i) \<in> sets (M' i)"
   9.682 -        using M'.Int[of _ i] M'.top by (auto simp: Int_stable_def)
   9.683 +    assume "?D = ?P'"
   9.684 +    show "indep_vars M' X I" unfolding indep_vars_def
   9.685 +    proof (intro conjI indep_setsI ballI rv)
   9.686 +      fix i show "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)} \<subseteq> events"
   9.687 +        by (auto intro!: sigma_sets_subset measurable_sets rv)
   9.688      next
   9.689 -      show "\<forall>A\<in>\<Pi> i\<in>I. sets (M' i). prob (\<Inter>j\<in>I. X j -` A j \<inter> space M) = (\<Prod>j\<in>I. prob (X j -` A j \<inter> space M))"
   9.690 +      fix J Y' assume J: "J \<noteq> {}" "J \<subseteq> I" "finite J"
   9.691 +      assume Y': "\<forall>j\<in>J. Y' j \<in> sigma_sets (space M) {X j -` A \<inter> space M |A. A \<in> sets (M' j)}"
   9.692 +      have "\<forall>j\<in>J. \<exists>Y. Y' j = X j -` Y \<inter> space M \<and> Y \<in> sets (M' j)"
   9.693        proof
   9.694 -        fix A assume A: "A \<in> (\<Pi> i\<in>I. sets (M' i))"
   9.695 -        then have A_in_P: "(Pi\<^isub>E I A) \<in> sets P.P"
   9.696 -          by (auto intro!: product_algebraI)
   9.697 -        have "prob (\<Inter>j\<in>I. X j -` A j \<inter> space M) = distribution ?D (Pi\<^isub>E I A)"
   9.698 -          using `I \<noteq> {}`by (auto intro!: arg_cong[where f=prob] simp: Pi_iff distribution_def)
   9.699 -        also have "\<dots> = P.prob (Pi\<^isub>E I A)" using A_in_P eq by simp
   9.700 -        also have "\<dots> = (\<Prod>i\<in>I. M'.prob i (A i))"
   9.701 -          using A by (intro P.prob_times) auto
   9.702 -        also have "\<dots> = (\<Prod>i\<in>I. prob (X i -` A i \<inter> space M))"
   9.703 -          using A by (auto intro!: setprod_cong simp: M'.\<mu>'_def Pi_iff distribution_def)
   9.704 -        finally show "prob (\<Inter>j\<in>I. X j -` A j \<inter> space M) = (\<Prod>j\<in>I. prob (X j -` A j \<inter> space M))" .
   9.705 +        fix j assume "j \<in> J"
   9.706 +        from Y'[rule_format, OF this] rv[of j]
   9.707 +        show "\<exists>Y. Y' j = X j -` Y \<inter> space M \<and> Y \<in> sets (M' j)"
   9.708 +          by (subst (asm) sigma_sets_vimage_commute[symmetric, of _ _ "space (M' j)"])
   9.709 +             (auto dest: measurable_space simp: sigma_sets_eq)
   9.710        qed
   9.711 +      from bchoice[OF this] obtain Y where
   9.712 +        Y: "\<And>j. j \<in> J \<Longrightarrow> Y' j = X j -` Y j \<inter> space M" "\<And>j. j \<in> J \<Longrightarrow> Y j \<in> sets (M' j)" by auto
   9.713 +      let ?E = "prod_emb I M' J (Pi\<^isub>E J Y)"
   9.714 +      from Y have "(\<Inter>j\<in>J. Y' j) = ?X -` ?E \<inter> space M"
   9.715 +        using J `I \<noteq> {}` measurable_space[OF rv] by (auto simp: prod_emb_def Pi_iff split: split_if_asm)
   9.716 +      then have "emeasure M (\<Inter>j\<in>J. Y' j) = emeasure M (?X -` ?E \<inter> space M)"
   9.717 +        by simp
   9.718 +      also have "\<dots> = emeasure ?D ?E"
   9.719 +        using Y  J by (intro emeasure_distr[symmetric] X sets_PiM_I) auto
   9.720 +      also have "\<dots> = emeasure ?P' ?E"
   9.721 +        using `?D = ?P'` by simp
   9.722 +      also have "\<dots> = (\<Prod> i\<in>J. emeasure (?D' i) (Y i))"
   9.723 +        using P.emeasure_PiM_emb[of J Y] J Y by (simp add: prod_emb_def)
   9.724 +      also have "\<dots> = (\<Prod> i\<in>J. emeasure M (Y' i))"
   9.725 +        using rv J Y by (simp add: emeasure_distr)
   9.726 +      finally have "emeasure M (\<Inter>j\<in>J. Y' j) = (\<Prod> i\<in>J. emeasure M (Y' i))" .
   9.727 +      then show "prob (\<Inter>j\<in>J. Y' j) = (\<Prod> i\<in>J. prob (Y' i))"
   9.728 +        by (auto simp: emeasure_eq_measure setprod_ereal)
   9.729      qed
   9.730 -    then show "indep_vars M' X I"
   9.731 -      by simp
   9.732    qed
   9.733  qed
   9.734  
   9.735 @@ -936,56 +960,188 @@
   9.736      unfolding UNIV_bool by auto
   9.737  qed
   9.738  
   9.739 -lemma (in prob_space) indep_var_distributionD:
   9.740 -  assumes indep: "indep_var S X T Y"
   9.741 -  defines "P \<equiv> S\<lparr>measure := ereal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
   9.742 -  assumes "A \<in> sets P"
   9.743 -  shows "joint_distribution X Y A = finite_measure.\<mu>' P A"
   9.744 -proof -
   9.745 -  from indep have rvs: "random_variable S X" "random_variable T Y"
   9.746 -    by (blast dest: indep_var_rv1 indep_var_rv2)+
   9.747 +lemma measurable_bool_case[simp, intro]:
   9.748 +  "(\<lambda>(x, y). bool_case x y) \<in> measurable (M \<Otimes>\<^isub>M N) (Pi\<^isub>M UNIV (bool_case M N))"
   9.749 +    (is "?f \<in> measurable ?B ?P")
   9.750 +proof (rule measurable_PiM_single)
   9.751 +  show "?f \<in> space ?B \<rightarrow> (\<Pi>\<^isub>E i\<in>UNIV. space (bool_case M N i))"
   9.752 +    by (auto simp: space_pair_measure extensional_def split: bool.split)
   9.753 +  fix i A assume "A \<in> sets (case i of True \<Rightarrow> M | False \<Rightarrow> N)"
   9.754 +  moreover then have "{\<omega> \<in> space (M \<Otimes>\<^isub>M N). prod_case bool_case \<omega> i \<in> A}
   9.755 +    = (case i of True \<Rightarrow> A \<times> space N | False \<Rightarrow> space M \<times> A)" 
   9.756 +    by (auto simp: space_pair_measure split: bool.split dest!: sets_into_space)
   9.757 +  ultimately show "{\<omega> \<in> space (M \<Otimes>\<^isub>M N). prod_case bool_case \<omega> i \<in> A} \<in> sets ?B"
   9.758 +    by (auto split: bool.split)
   9.759 +qed
   9.760 +
   9.761 +lemma borel_measurable_indicator':
   9.762 +  "A \<in> sets N \<Longrightarrow> f \<in> measurable M N \<Longrightarrow> (\<lambda>x. indicator A (f x)) \<in> borel_measurable M"
   9.763 +  using measurable_comp[OF _ borel_measurable_indicator, of f M N A] by (auto simp add: comp_def)
   9.764 +
   9.765 +lemma (in product_sigma_finite) distr_component:
   9.766 +  "distr (M i) (Pi\<^isub>M {i} M) (\<lambda>x. \<lambda>i\<in>{i}. x) = Pi\<^isub>M {i} M" (is "?D = ?P")
   9.767 +proof (intro measure_eqI[symmetric])
   9.768 +  interpret I: finite_product_sigma_finite M "{i}" by default simp
   9.769 +
   9.770 +  have eq: "\<And>x. x \<in> extensional {i} \<Longrightarrow> (\<lambda>j\<in>{i}. x i) = x"
   9.771 +    by (auto simp: extensional_def restrict_def)
   9.772 +
   9.773 +  fix A assume A: "A \<in> sets ?P"
   9.774 +  then have "emeasure ?P A = (\<integral>\<^isup>+x. indicator A x \<partial>?P)" 
   9.775 +    by simp
   9.776 +  also have "\<dots> = (\<integral>\<^isup>+x. indicator ((\<lambda>x. \<lambda>i\<in>{i}. x) -` A \<inter> space (M i)) x \<partial>M i)" 
   9.777 +    apply (subst product_positive_integral_singleton[symmetric])
   9.778 +    apply (force intro!: measurable_restrict measurable_sets A)
   9.779 +    apply (auto intro!: positive_integral_cong simp: space_PiM indicator_def simp: eq)
   9.780 +    done
   9.781 +  also have "\<dots> = emeasure (M i) ((\<lambda>x. \<lambda>i\<in>{i}. x) -` A \<inter> space (M i))"
   9.782 +    by (force intro!: measurable_restrict measurable_sets A positive_integral_indicator)
   9.783 +  also have "\<dots> = emeasure ?D A"
   9.784 +    using A by (auto intro!: emeasure_distr[symmetric] measurable_restrict) 
   9.785 +  finally show "emeasure (Pi\<^isub>M {i} M) A = emeasure ?D A" .
   9.786 +qed simp
   9.787  
   9.788 -  let ?S = "S\<lparr>measure := ereal\<circ>distribution X\<rparr>"
   9.789 -  let ?T = "T\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
   9.790 -  interpret X: prob_space ?S by (rule distribution_prob_space) fact
   9.791 -  interpret Y: prob_space ?T by (rule distribution_prob_space) fact
   9.792 -  interpret XY: pair_prob_space ?S ?T by default
   9.793 +lemma pair_measure_eqI:
   9.794 +  assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
   9.795 +  assumes sets: "sets (M1 \<Otimes>\<^isub>M M2) = sets M"
   9.796 +  assumes emeasure: "\<And>A B. A \<in> sets M1 \<Longrightarrow> B \<in> sets M2 \<Longrightarrow> emeasure M1 A * emeasure M2 B = emeasure M (A \<times> B)"
   9.797 +  shows "M1 \<Otimes>\<^isub>M M2 = M"
   9.798 +proof -
   9.799 +  interpret M1: sigma_finite_measure M1 by fact
   9.800 +  interpret M2: sigma_finite_measure M2 by fact
   9.801 +  interpret pair_sigma_finite M1 M2 by default
   9.802 +  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   9.803 +  let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   9.804 +  let ?P = "M1 \<Otimes>\<^isub>M M2"
   9.805 +  show ?thesis
   9.806 +  proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   9.807 +    show "?E \<subseteq> Pow (space ?P)"
   9.808 +      using space_closed[of M1] space_closed[of M2] by (auto simp: space_pair_measure)
   9.809 +    show "sets ?P = sigma_sets (space ?P) ?E"
   9.810 +      by (simp add: sets_pair_measure space_pair_measure)
   9.811 +    then show "sets M = sigma_sets (s