reworked Probability theory
authorhoelzl
Mon, 23 Apr 2012 12:14:35 +0200
changeset 47694 05663f75964c
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
--- a/NEWS	Mon Apr 23 12:23:23 2012 +0100
+++ b/NEWS	Mon Apr 23 12:14:35 2012 +0200
@@ -647,6 +647,180 @@
 
   DERIV_nonneg_imp_nonincreasing ~> DERIV_nonneg_imp_nondecreasing
 
+* Theory Library/Multiset: Improved code generation of multisets.
+
+* Session HOL-Probability: Introduced the type "'a measure" to represent
+measures, this replaces the records 'a algebra and 'a measure_space. The
+locales based on subset_class now have two locale-parameters the space
+\<Omega> and the set of measurables sets M. The product of probability spaces
+uses now the same constant as the finite product of sigma-finite measure
+spaces "PiM :: ('i => 'a) measure". Most constants are defined now
+outside of locales and gain an additional parameter, like null_sets,
+almost_eventually or \<mu>'. Measure space constructions for distributions
+and densities now got their own constants distr and density. Instead of
+using locales to describe measure spaces with a finite space, the
+measure count_space and point_measure is introduced. INCOMPATIBILITY.
+
+  Renamed constants:
+  measure -> emeasure
+  finite_measure.\<mu>' -> measure
+  product_algebra_generator -> prod_algebra
+  product_prob_space.emb -> prod_emb
+  product_prob_space.infprod_algebra -> PiM
+
+  Removed locales:
+  completeable_measure_space
+  finite_measure_space
+  finite_prob_space
+  finite_product_finite_prob_space
+  finite_product_sigma_algebra
+  finite_sigma_algebra
+  measure_space
+  pair_finite_prob_space
+  pair_finite_sigma_algebra
+  pair_finite_space
+  pair_sigma_algebra
+  product_sigma_algebra
+
+  Removed constants:
+  distribution -> use distr measure, or distributed predicate
+  joint_distribution -> use distr measure, or distributed predicate
+  product_prob_space.infprod_algebra -> use PiM
+  subvimage
+  image_space
+  conditional_space
+  pair_measure_generator
+
+  Replacement theorems:
+  sigma_algebra.measurable_sigma -> measurable_measure_of
+  measure_space.additive -> emeasure_additive
+  measure_space.measure_additive -> plus_emeasure
+  measure_space.measure_mono -> emeasure_mono
+  measure_space.measure_top -> emeasure_space
+  measure_space.measure_compl -> emeasure_compl
+  measure_space.measure_Diff -> emeasure_Diff
+  measure_space.measure_countable_increasing -> emeasure_countable_increasing
+  measure_space.continuity_from_below -> SUP_emeasure_incseq
+  measure_space.measure_incseq -> incseq_emeasure
+  measure_space.continuity_from_below_Lim -> Lim_emeasure_incseq
+  measure_space.measure_decseq -> decseq_emeasure
+  measure_space.continuity_from_above -> INF_emeasure_decseq
+  measure_space.measure_insert -> emeasure_insert
+  measure_space.measure_setsum -> setsum_emeasure
+  measure_space.measure_finite_singleton -> emeasure_eq_setsum_singleton
+  finite_additivity_sufficient -> ring_of_sets.countably_additiveI_finite
+  measure_space.measure_setsum_split -> setsum_emeasure_cover
+  measure_space.measure_subadditive -> subadditive
+  measure_space.measure_subadditive_finite -> emeasure_subadditive_finite
+  measure_space.measure_eq_0 -> emeasure_eq_0
+  measure_space.measure_finitely_subadditive -> emeasure_subadditive_finite
+  measure_space.measure_countably_subadditive -> emeasure_subadditive_countably
+  measure_space.measure_UN_eq_0 -> emeasure_UN_eq_0
+  measure_unique_Int_stable -> measure_eqI_generator_eq
+  measure_space.measure_Diff_null_set -> emeasure_Diff_null_set
+  measure_space.measure_Un_null_set -> emeasure_Un_null_set
+  measure_space.almost_everywhere_def -> eventually_ae_filter
+  measure_space.almost_everywhere_vimage -> AE_distrD
+  measure_space.measure_space_vimage -> emeasure_distr
+  measure_space.AE_iff_null_set -> AE_iff_null
+  measure_space.real_measure_Union -> measure_Union
+  measure_space.real_measure_finite_Union -> measure_finite_Union
+  measure_space.real_measure_Diff -> measure_Diff
+  measure_space.real_measure_UNION -> measure_UNION
+  measure_space.real_measure_subadditive -> measure_subadditive
+  measure_space.real_measure_setsum_singleton -> measure_eq_setsum_singleton
+  measure_space.real_continuity_from_below -> Lim_measure_incseq
+  measure_space.continuity_from_above_Lim -> Lim_emeasure_decseq
+  measure_space.real_continuity_from_above -> Lim_measure_decseq
+  measure_space.real_measure_countably_subadditive -> measure_subadditive_countably
+  finite_measure.finite_measure -> finite_measure.emeasure_finite
+  finite_measure.finite_measure_eq -> finite_measure.emeasure_eq_measure
+  finite_measure.positive_measure' -> measure_nonneg
+  finite_measure.real_measure -> finite_measure.emeasure_real
+  finite_measure.empty_measure -> measure_empty
+  finite_measure.finite_measure_countably_subadditive -> finite_measure.finite_measure_subadditive_countably
+  finite_measure.finite_measure_finite_singleton -> finite_measure.finite_measure_eq_setsum_singleton
+  finite_measure.finite_continuity_from_below -> finite_measure.finite_Lim_measure_incseq
+  finite_measure.finite_continuity_from_above -> finite_measure.finite_Lim_measure_decseq
+  measure_space.simple_integral_vimage -> simple_integral_distr
+  measure_space.integrable_vimage -> integrable_distr
+  measure_space.positive_integral_translated_density -> positive_integral_density
+  measure_space.integral_translated_density -> integral_density
+  measure_space.integral_vimage -> integral_distr
+  measure_space_density -> emeasure_density
+  measure_space.positive_integral_vimage -> positive_integral_distr
+  measure_space.simple_function_vimage -> simple_function_comp
+  measure_space.simple_integral_vimage -> simple_integral_distr
+  pair_sigma_algebra.measurable_cut_fst -> sets_Pair1
+  pair_sigma_algebra.measurable_cut_snd -> sets_Pair2
+  pair_sigma_algebra.measurable_pair_image_fst -> measurable_Pair1
+  pair_sigma_algebra.measurable_pair_image_snd -> measurable_Pair2
+  pair_sigma_algebra.measurable_product_swap -> measurable_pair_swap_iff
+  pair_sigma_finite.measure_cut_measurable_fst -> pair_sigma_finite.measurable_emeasure_Pair1
+  pair_sigma_finite.measure_cut_measurable_snd -> pair_sigma_finite.measurable_emeasure_Pair2
+  measure_space.measure_not_negative -> emeasure_not_MInf
+  pair_sigma_finite.measure_preserving_swap -> pair_sigma_finite.distr_pair_swap
+  pair_sigma_finite.pair_measure_alt -> pair_sigma_finite.emeasure_pair_measure_alt
+  pair_sigma_finite.pair_measure_alt2 -> pair_sigma_finite.emeasure_pair_measure_alt2
+  pair_sigma_finite.pair_measure_times -> pair_sigma_finite.emeasure_pair_measure_Times
+  pair_sigma_algebra.pair_sigma_algebra_measurable -> measurable_pair_swap
+  pair_sigma_algebra.pair_sigma_algebra_swap_measurable -> measurable_pair_swap'
+  pair_sigma_algebra.sets_swap -> sets_pair_swap
+  finite_product_sigma_algebra.in_P -> sets_PiM_I_finite
+  Int_stable_product_algebra_generator -> positive_integral
+  product_sigma_finite.measure_fold -> product_sigma_finite.distr_merge
+  product_sigma_finite.measure_preserving_component_singelton -> product_sigma_finite.distr_singleton
+  product_sigma_finite.measure_preserving_merge -> product_sigma_finite.distr_merge
+  finite_product_sigma_algebra.P_empty -> space_PiM_empty, sets_PiM_empty
+  product_algebra_generator_der -> prod_algebra_eq_finite
+  product_algebra_generator_into_space -> prod_algebra_sets_into_space
+  product_sigma_algebra.product_algebra_into_space -> space_closed
+  product_algebraE -> prod_algebraE_all
+  product_algebraI -> sets_PiM_I_finite
+  product_measure_exists -> product_sigma_finite.sigma_finite
+  sets_product_algebra -> sets_PiM
+  sigma_product_algebra_sigma_eq -> sigma_prod_algebra_sigma_eq
+  space_product_algebra -> space_PiM
+  Int_stable_cuboids -> Int_stable_atLeastAtMost
+  measure_space.density_is_absolutely_continuous -> absolutely_continuousI_density
+  sigma_finite_measure.RN_deriv_vimage -> sigma_finite_measure.RN_deriv_distr
+  prob_space_unique_Int_stable -> measure_eqI_prob_space
+  sigma_finite_measure.disjoint_sigma_finite -> sigma_finite_disjoint
+  prob_space.measure_space_1 -> prob_space.emeasure_space_1
+  prob_space.prob_space_vimage -> prob_space_distr
+  prob_space.random_variable_restrict -> measurable_restrict
+  measure_preserving -> equality "distr M N f = N" "f : measurable M N"
+  measure_unique_Int_stable_vimage -> measure_eqI_generator_eq
+  measure_space.measure_preserving_Int_stable -> measure_eqI_generator_eq
+  product_prob_space.finite_index_eq_finite_product -> product_prob_space.sets_PiM_generator
+  product_prob_space.finite_measure_infprod_emb_Pi -> product_prob_space.measure_PiM_emb
+  finite_product_prob_space.finite_measure_times -> finite_product_prob_space.finite_measure_PiM_emb
+  product_prob_space.infprod_spec -> product_prob_space.emeasure_PiM_emb_not_empty
+  product_prob_space.measurable_component -> measurable_component_singleton
+  product_prob_space.measurable_emb -> measurable_prod_emb
+  product_prob_space.measurable_into_infprod_algebra -> measurable_PiM_single
+  product_prob_space.measurable_singleton_infprod -> measurable_component_singleton
+  product_prob_space.measure_emb -> emeasure_prod_emb
+  sequence_space.measure_infprod -> sequence_space.measure_PiM_countable
+  product_prob_space.measure_preserving_restrict -> product_prob_space.distr_restrict
+  prob_space.indep_distribution_eq_measure -> prob_space.indep_vars_iff_distr_eq_PiM
+  prob_space.indep_var_distributionD -> prob_space.indep_var_distribution_eq
+  conditional_entropy_positive -> conditional_entropy_nonneg_simple
+  conditional_entropy_eq -> conditional_entropy_simple_distributed
+  conditional_mutual_information_eq_mutual_information -> conditional_mutual_information_eq_mutual_information_simple
+  conditional_mutual_information_generic_positive -> conditional_mutual_information_nonneg_simple
+  conditional_mutual_information_positive -> conditional_mutual_information_nonneg_simple
+  entropy_commute -> entropy_commute_simple
+  entropy_eq -> entropy_simple_distributed
+  entropy_generic_eq -> entropy_simple_distributed
+  entropy_positive -> entropy_nonneg_simple
+  entropy_uniform_max -> entropy_uniform
+  KL_eq_0 -> KL_same_eq_0
+  KL_eq_0_imp -> KL_eq_0_iff_eq
+  KL_ge_0 -> KL_nonneg
+  mutual_information_eq -> mutual_information_simple_distributed
+  mutual_information_positive -> mutual_information_nonneg_simple
+
 * New "case_product" attribute to generate a case rule doing multiple
 case distinctions at the same time.  E.g.
 
@@ -655,8 +829,6 @@
 produces a rule which can be used to perform case distinction on both
 a list and a nat.
 
-* Theory Library/Multiset: Improved code generation of multisets.
-
 * New Transfer package:
 
   - transfer_rule attribute: Maintains a collection of transfer rules,
--- a/src/HOL/IsaMakefile	Mon Apr 23 12:23:23 2012 +0100
+++ b/src/HOL/IsaMakefile	Mon Apr 23 12:14:35 2012 +0200
@@ -1198,14 +1198,13 @@
 $(OUT)/HOL-Probability: $(OUT)/HOL-Multivariate_Analysis		\
   Probability/Binary_Product_Measure.thy Probability/Borel_Space.thy	\
   Probability/Caratheodory.thy Probability/Complete_Measure.thy		\
-  Probability/Conditional_Probability.thy				\
   Probability/ex/Dining_Cryptographers.thy				\
   Probability/ex/Koepf_Duermuth_Countermeasure.thy			\
   Probability/Finite_Product_Measure.thy				\
   Probability/Independent_Family.thy					\
   Probability/Infinite_Product_Measure.thy Probability/Information.thy	\
   Probability/Lebesgue_Integration.thy Probability/Lebesgue_Measure.thy \
-  Probability/Measure.thy Probability/Probability_Measure.thy		\
+  Probability/Measure_Space.thy Probability/Probability_Measure.thy	\
   Probability/Probability.thy Probability/Radon_Nikodym.thy		\
   Probability/ROOT.ML Probability/Sigma_Algebra.thy			\
   Library/Countable.thy Library/FuncSet.thy Library/Nat_Bijection.thy
--- a/src/HOL/Probability/Binary_Product_Measure.thy	Mon Apr 23 12:23:23 2012 +0100
+++ b/src/HOL/Probability/Binary_Product_Measure.thy	Mon Apr 23 12:14:35 2012 +0200
@@ -28,413 +28,317 @@
 
 section "Binary products"
 
-definition
-  "pair_measure_generator A B =
-    \<lparr> space = space A \<times> space B,
-      sets = {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B},
-      measure = \<lambda>X. \<integral>\<^isup>+x. (\<integral>\<^isup>+y. indicator X (x,y) \<partial>B) \<partial>A \<rparr>"
-
 definition pair_measure (infixr "\<Otimes>\<^isub>M" 80) where
-  "A \<Otimes>\<^isub>M B = sigma (pair_measure_generator A B)"
-
-locale pair_sigma_algebra = M1: sigma_algebra M1 + M2: sigma_algebra M2
-  for M1 :: "('a, 'c) measure_space_scheme" and M2 :: "('b, 'd) measure_space_scheme"
-
-abbreviation (in pair_sigma_algebra)
-  "E \<equiv> pair_measure_generator M1 M2"
-
-abbreviation (in pair_sigma_algebra)
-  "P \<equiv> M1 \<Otimes>\<^isub>M M2"
-
-lemma sigma_algebra_pair_measure:
-  "sets M1 \<subseteq> Pow (space M1) \<Longrightarrow> sets M2 \<subseteq> Pow (space M2) \<Longrightarrow> sigma_algebra (pair_measure M1 M2)"
-  by (force simp: pair_measure_def pair_measure_generator_def intro!: sigma_algebra_sigma)
-
-sublocale pair_sigma_algebra \<subseteq> sigma_algebra P
-  using M1.space_closed M2.space_closed
-  by (rule sigma_algebra_pair_measure)
-
-lemma pair_measure_generatorI[intro, simp]:
-  "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (pair_measure_generator A B)"
-  by (auto simp add: pair_measure_generator_def)
-
-lemma pair_measureI[intro, simp]:
-  "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^isub>M B)"
-  by (auto simp add: pair_measure_def)
+  "A \<Otimes>\<^isub>M B = measure_of (space A \<times> space B)
+      {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}
+      (\<lambda>X. \<integral>\<^isup>+x. (\<integral>\<^isup>+y. indicator X (x,y) \<partial>B) \<partial>A)"
 
 lemma space_pair_measure:
   "space (A \<Otimes>\<^isub>M B) = space A \<times> space B"
-  by (simp add: pair_measure_def pair_measure_generator_def)
+  unfolding pair_measure_def using space_closed[of A] space_closed[of B]
+  by (intro space_measure_of) auto
+
+lemma sets_pair_measure:
+  "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}"
+  unfolding pair_measure_def using space_closed[of A] space_closed[of B]
+  by (intro sets_measure_of) auto
 
-lemma sets_pair_measure_generator:
-  "sets (pair_measure_generator N M) = (\<lambda>(x, y). x \<times> y) ` (sets N \<times> sets M)"
-  unfolding pair_measure_generator_def by auto
+lemma pair_measureI[intro, simp]:
+  "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^isub>M B)"
+  by (auto simp: sets_pair_measure)
 
-lemma pair_measure_generator_sets_into_space:
-  assumes "sets M \<subseteq> Pow (space M)" "sets N \<subseteq> Pow (space N)"
-  shows "sets (pair_measure_generator M N) \<subseteq> Pow (space (pair_measure_generator M N))"
-  using assms by (auto simp: pair_measure_generator_def)
+lemma measurable_pair_measureI:
+  assumes 1: "f \<in> space M \<rightarrow> space M1 \<times> space M2"
+  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"
+  shows "f \<in> measurable M (M1 \<Otimes>\<^isub>M M2)"
+  unfolding pair_measure_def using 1 2
+  by (intro measurable_measure_of) (auto dest: sets_into_space)
 
-lemma pair_measure_generator_Int_snd:
-  assumes "sets S1 \<subseteq> Pow (space S1)"
-  shows "sets (pair_measure_generator S1 (algebra.restricted_space S2 A)) =
-         sets (algebra.restricted_space (pair_measure_generator S1 S2) (space S1 \<times> A))"
-  (is "?L = ?R")
-  apply (auto simp: pair_measure_generator_def image_iff)
-  using assms
-  apply (rule_tac x="a \<times> xa" in exI)
-  apply force
-  using assms
-  apply (rule_tac x="a" in exI)
-  apply (rule_tac x="b \<inter> A" in exI)
-  apply auto
-  done
+lemma measurable_fst[intro!, simp]: "fst \<in> measurable (M1 \<Otimes>\<^isub>M M2) M1"
+  unfolding measurable_def
+proof safe
+  fix A assume A: "A \<in> sets M1"
+  from this[THEN sets_into_space] have "fst -` A \<inter> space M1 \<times> space M2 = A \<times> space M2" by auto
+  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)
+qed (simp add: space_pair_measure)
 
-lemma (in pair_sigma_algebra)
-  shows measurable_fst[intro!, simp]:
-    "fst \<in> measurable P M1" (is ?fst)
-  and measurable_snd[intro!, simp]:
-    "snd \<in> measurable P M2" (is ?snd)
-proof -
-  { fix X assume "X \<in> sets M1"
-    then have "\<exists>X1\<in>sets M1. \<exists>X2\<in>sets M2. fst -` X \<inter> space M1 \<times> space M2 = X1 \<times> X2"
-      apply - apply (rule bexI[of _ X]) apply (rule bexI[of _ "space M2"])
-      using M1.sets_into_space by force+ }
-  moreover
-  { fix X assume "X \<in> sets M2"
-    then have "\<exists>X1\<in>sets M1. \<exists>X2\<in>sets M2. snd -` X \<inter> space M1 \<times> space M2 = X1 \<times> X2"
-      apply - apply (rule bexI[of _ "space M1"]) apply (rule bexI[of _ X])
-      using M2.sets_into_space by force+ }
-  ultimately have "?fst \<and> ?snd"
-    by (fastforce simp: measurable_def sets_sigma space_pair_measure
-                 intro!: sigma_sets.Basic)
-  then show ?fst ?snd by auto
-qed
+lemma measurable_snd[intro!, simp]: "snd \<in> measurable (M1 \<Otimes>\<^isub>M M2) M2"
+  unfolding measurable_def
+proof safe
+  fix A assume A: "A \<in> sets M2"
+  from this[THEN sets_into_space] have "snd -` A \<inter> space M1 \<times> space M2 = space M1 \<times> A" by auto
+  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)
+qed (simp add: space_pair_measure)
+
+lemma measurable_fst': "f \<in> measurable M (N \<Otimes>\<^isub>M P) \<Longrightarrow> (\<lambda>x. fst (f x)) \<in> measurable M N"
+  using measurable_comp[OF _ measurable_fst] by (auto simp: comp_def)
+
+lemma measurable_snd': "f \<in> measurable M (N \<Otimes>\<^isub>M P) \<Longrightarrow> (\<lambda>x. snd (f x)) \<in> measurable M P"
+    using measurable_comp[OF _ measurable_snd] by (auto simp: comp_def)
 
-lemma (in pair_sigma_algebra) measurable_pair_iff:
-  assumes "sigma_algebra M"
-  shows "f \<in> measurable M P \<longleftrightarrow>
-    (fst \<circ> f) \<in> measurable M M1 \<and> (snd \<circ> f) \<in> measurable M M2"
-proof -
-  interpret M: sigma_algebra M by fact
-  from assms show ?thesis
-  proof (safe intro!: measurable_comp[where b=P])
-    assume f: "(fst \<circ> f) \<in> measurable M M1" and s: "(snd \<circ> f) \<in> measurable M M2"
-    show "f \<in> measurable M P" unfolding pair_measure_def
-    proof (rule M.measurable_sigma)
-      show "sets (pair_measure_generator M1 M2) \<subseteq> Pow (space E)"
-        unfolding pair_measure_generator_def using M1.sets_into_space M2.sets_into_space by auto
-      show "f \<in> space M \<rightarrow> space E"
-        using f s by (auto simp: mem_Times_iff measurable_def comp_def space_sigma pair_measure_generator_def)
-      fix A assume "A \<in> sets E"
-      then obtain B C where "B \<in> sets M1" "C \<in> sets M2" "A = B \<times> C"
-        unfolding pair_measure_generator_def by auto
-      moreover have "(fst \<circ> f) -` B \<inter> space M \<in> sets M"
-        using f `B \<in> sets M1` unfolding measurable_def by auto
-      moreover have "(snd \<circ> f) -` C \<inter> space M \<in> sets M"
-        using s `C \<in> sets M2` unfolding measurable_def by auto
-      moreover have "f -` A \<inter> space M = ((fst \<circ> f) -` B \<inter> space M) \<inter> ((snd \<circ> f) -` C \<inter> space M)"
-        unfolding `A = B \<times> C` by (auto simp: vimage_Times)
-      ultimately show "f -` A \<inter> space M \<in> sets M" by auto
-    qed
+lemma measurable_fst'': "f \<in> measurable M N \<Longrightarrow> (\<lambda>x. f (fst x)) \<in> measurable (M \<Otimes>\<^isub>M P) N"
+  using measurable_comp[OF measurable_fst _] by (auto simp: comp_def)
+
+lemma measurable_snd'': "f \<in> measurable M N \<Longrightarrow> (\<lambda>x. f (snd x)) \<in> measurable (P \<Otimes>\<^isub>M M) N"
+  using measurable_comp[OF measurable_snd _] by (auto simp: comp_def)
+
+lemma measurable_pair_iff:
+  "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"
+proof safe
+  assume f: "(fst \<circ> f) \<in> measurable M M1" and s: "(snd \<circ> f) \<in> measurable M M2"
+  show "f \<in> measurable M (M1 \<Otimes>\<^isub>M M2)"
+  proof (rule measurable_pair_measureI)
+    show "f \<in> space M \<rightarrow> space M1 \<times> space M2"
+      using f s by (auto simp: mem_Times_iff measurable_def comp_def)
+    fix A B assume "A \<in> sets M1" "B \<in> sets M2"
+    moreover have "(fst \<circ> f) -` A \<inter> space M \<in> sets M" "(snd \<circ> f) -` B \<inter> space M \<in> sets M"
+      using f `A \<in> sets M1` s `B \<in> sets M2` by (auto simp: measurable_sets)
+    moreover have "f -` (A \<times> B) \<inter> space M = ((fst \<circ> f) -` A \<inter> space M) \<inter> ((snd \<circ> f) -` B \<inter> space M)"
+      by (auto simp: vimage_Times)
+    ultimately show "f -` (A \<times> B) \<inter> space M \<in> sets M" by auto
   qed
-qed
+qed auto
 
-lemma (in pair_sigma_algebra) measurable_pair:
-  assumes "sigma_algebra M"
-  assumes "(fst \<circ> f) \<in> measurable M M1" "(snd \<circ> f) \<in> measurable M M2"
-  shows "f \<in> measurable M P"
-  unfolding measurable_pair_iff[OF assms(1)] using assms(2,3) by simp
-
-lemma pair_measure_generatorE:
-  assumes "X \<in> sets (pair_measure_generator M1 M2)"
-  obtains A B where "X = A \<times> B" "A \<in> sets M1" "B \<in> sets M2"
-  using assms unfolding pair_measure_generator_def by auto
+lemma measurable_pair:
+  "(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)"
+  unfolding measurable_pair_iff by simp
 
-lemma (in pair_sigma_algebra) pair_measure_generator_swap:
-  "(\<lambda>X. (\<lambda>(x,y). (y,x)) -` X \<inter> space M2 \<times> space M1) ` sets E = sets (pair_measure_generator M2 M1)"
-proof (safe elim!: pair_measure_generatorE)
-  fix A B assume "A \<in> sets M1" "B \<in> sets M2"
-  moreover then have "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space M2 \<times> space M1 = B \<times> A"
-    using M1.sets_into_space M2.sets_into_space by auto
-  ultimately show "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space M2 \<times> space M1 \<in> sets (pair_measure_generator M2 M1)"
-    by (auto intro: pair_measure_generatorI)
-next
-  fix A B assume "A \<in> sets M1" "B \<in> sets M2"
-  then show "B \<times> A \<in> (\<lambda>X. (\<lambda>(x, y). (y, x)) -` X \<inter> space M2 \<times> space M1) ` sets E"
-    using M1.sets_into_space M2.sets_into_space
-    by (auto intro!: image_eqI[where x="A \<times> B"] pair_measure_generatorI)
+lemma measurable_pair_swap': "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^isub>M M2) (M2 \<Otimes>\<^isub>M M1)"
+proof (rule measurable_pair_measureI)
+  fix A B assume "A \<in> sets M2" "B \<in> sets M1"
+  moreover then have "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space (M1 \<Otimes>\<^isub>M M2) = B \<times> A"
+    by (auto dest: sets_into_space simp: space_pair_measure)
+  ultimately show "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space (M1 \<Otimes>\<^isub>M M2) \<in> sets (M1 \<Otimes>\<^isub>M M2)"
+    by auto
+qed (auto simp add: space_pair_measure)
+
+lemma measurable_pair_swap:
+  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"
+proof -
+  have "(\<lambda>x. f (case x of (x, y) \<Rightarrow> (y, x))) = (\<lambda>(x, y). f (y, x))" by auto
+  then show ?thesis
+    using measurable_comp[OF measurable_pair_swap' f] by (simp add: comp_def)
 qed
 
-lemma (in pair_sigma_algebra) sets_pair_sigma_algebra_swap:
-  assumes Q: "Q \<in> sets P"
-  shows "(\<lambda>(x,y). (y, x)) -` Q \<in> sets (M2 \<Otimes>\<^isub>M M1)" (is "_ \<in> sets ?Q")
-proof -
-  let ?f = "\<lambda>Q. (\<lambda>(x,y). (y, x)) -` Q \<inter> space M2 \<times> space M1"
-  have *: "(\<lambda>(x,y). (y, x)) -` Q = ?f Q"
-    using sets_into_space[OF Q] by (auto simp: space_pair_measure)
-  have "sets (M2 \<Otimes>\<^isub>M M1) = sets (sigma (pair_measure_generator M2 M1))"
-    unfolding pair_measure_def ..
-  also have "\<dots> = sigma_sets (space M2 \<times> space M1) (?f ` sets E)"
-    unfolding sigma_def pair_measure_generator_swap[symmetric]
-    by (simp add: pair_measure_generator_def)
-  also have "\<dots> = ?f ` sigma_sets (space M1 \<times> space M2) (sets E)"
-    using M1.sets_into_space M2.sets_into_space
-    by (intro sigma_sets_vimage) (auto simp: pair_measure_generator_def)
-  also have "\<dots> = ?f ` sets P"
-    unfolding pair_measure_def pair_measure_generator_def sigma_def by simp
-  finally show ?thesis
-    using Q by (subst *) auto
-qed
+lemma measurable_pair_swap_iff:
+  "f \<in> measurable (M2 \<Otimes>\<^isub>M M1) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable (M1 \<Otimes>\<^isub>M M2) M"
+  using measurable_pair_swap[of "\<lambda>(x,y). f (y, x)"]
+  by (auto intro!: measurable_pair_swap)
 
-lemma (in pair_sigma_algebra) pair_sigma_algebra_swap_measurable:
-  shows "(\<lambda>(x,y). (y, x)) \<in> measurable P (M2 \<Otimes>\<^isub>M M1)"
-    (is "?f \<in> measurable ?P ?Q")
-  unfolding measurable_def
-proof (intro CollectI conjI Pi_I ballI)
-  fix x assume "x \<in> space ?P" then show "(case x of (x, y) \<Rightarrow> (y, x)) \<in> space ?Q"
-    unfolding pair_measure_generator_def pair_measure_def by auto
-next
-  fix A assume "A \<in> sets (M2 \<Otimes>\<^isub>M M1)"
-  interpret Q: pair_sigma_algebra M2 M1 by default
-  from Q.sets_pair_sigma_algebra_swap[OF `A \<in> sets (M2 \<Otimes>\<^isub>M M1)`]
-  show "?f -` A \<inter> space ?P \<in> sets ?P" by simp
-qed
+lemma measurable_Pair1': "x \<in> space M1 \<Longrightarrow> Pair x \<in> measurable M2 (M1 \<Otimes>\<^isub>M M2)"
+proof (rule measurable_pair_measureI)
+  fix A B assume "A \<in> sets M1" "B \<in> sets M2"
+  moreover then have "Pair x -` (A \<times> B) \<inter> space M2 = (if x \<in> A then B else {})"
+    by (auto dest: sets_into_space simp: space_pair_measure)
+  ultimately show "Pair x -` (A \<times> B) \<inter> space M2 \<in> sets M2"
+    by auto
+qed (auto simp add: space_pair_measure)
 
-lemma (in pair_sigma_algebra) measurable_cut_fst[simp,intro]:
-  assumes "Q \<in> sets P" shows "Pair x -` Q \<in> sets M2"
+lemma sets_Pair1: assumes A: "A \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "Pair x -` A \<in> sets M2"
 proof -
-  let ?Q' = "{Q. Q \<subseteq> space P \<and> Pair x -` Q \<in> sets M2}"
-  let ?Q = "\<lparr> space = space P, sets = ?Q' \<rparr>"
-  interpret Q: sigma_algebra ?Q
-    proof qed (auto simp: vimage_UN vimage_Diff space_pair_measure)
-  have "sets E \<subseteq> sets ?Q"
-    using M1.sets_into_space M2.sets_into_space
-    by (auto simp: pair_measure_generator_def space_pair_measure)
-  then have "sets P \<subseteq> sets ?Q"
-    apply (subst pair_measure_def, intro Q.sets_sigma_subset)
-    by (simp add: pair_measure_def)
-  with assms show ?thesis by auto
+  have "Pair x -` A = (if x \<in> space M1 then Pair x -` A \<inter> space M2 else {})"
+    using A[THEN sets_into_space] by (auto simp: space_pair_measure)
+  also have "\<dots> \<in> sets M2"
+    using A by (auto simp add: measurable_Pair1' intro!: measurable_sets split: split_if_asm)
+  finally show ?thesis .
 qed
 
-lemma (in pair_sigma_algebra) measurable_cut_snd:
-  assumes Q: "Q \<in> sets P" shows "(\<lambda>x. (x, y)) -` Q \<in> sets M1" (is "?cut Q \<in> sets M1")
-proof -
-  interpret Q: pair_sigma_algebra M2 M1 by default
-  from Q.measurable_cut_fst[OF sets_pair_sigma_algebra_swap[OF Q], of y]
-  show ?thesis by (simp add: vimage_compose[symmetric] comp_def)
-qed
+lemma measurable_Pair2': "y \<in> space M2 \<Longrightarrow> (\<lambda>x. (x, y)) \<in> measurable M1 (M1 \<Otimes>\<^isub>M M2)"
+  using measurable_comp[OF measurable_Pair1' measurable_pair_swap', of y M2 M1]
+  by (simp add: comp_def)
 
-lemma (in pair_sigma_algebra) measurable_pair_image_snd:
-  assumes m: "f \<in> measurable P M" and "x \<in> space M1"
-  shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"
-  unfolding measurable_def
-proof (intro CollectI conjI Pi_I ballI)
-  fix y assume "y \<in> space M2" with `f \<in> measurable P M` `x \<in> space M1`
-  show "f (x, y) \<in> space M"
-    unfolding measurable_def pair_measure_generator_def pair_measure_def by auto
-next
-  fix A assume "A \<in> sets M"
-  then have "Pair x -` (f -` A \<inter> space P) \<in> sets M2" (is "?C \<in> _")
-    using `f \<in> measurable P M`
-    by (intro measurable_cut_fst) (auto simp: measurable_def)
-  also have "?C = (\<lambda>y. f (x, y)) -` A \<inter> space M2"
-    using `x \<in> space M1` by (auto simp: pair_measure_generator_def pair_measure_def)
-  finally show "(\<lambda>y. f (x, y)) -` A \<inter> space M2 \<in> sets M2" .
+lemma sets_Pair2: assumes A: "A \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "(\<lambda>x. (x, y)) -` A \<in> sets M1"
+proof -
+  have "(\<lambda>x. (x, y)) -` A = (if y \<in> space M2 then (\<lambda>x. (x, y)) -` A \<inter> space M1 else {})"
+    using A[THEN sets_into_space] by (auto simp: space_pair_measure)
+  also have "\<dots> \<in> sets M1"
+    using A by (auto simp add: measurable_Pair2' intro!: measurable_sets split: split_if_asm)
+  finally show ?thesis .
 qed
 
-lemma (in pair_sigma_algebra) measurable_pair_image_fst:
-  assumes m: "f \<in> measurable P M" and "y \<in> space M2"
+lemma measurable_Pair2:
+  assumes f: "f \<in> measurable (M1 \<Otimes>\<^isub>M M2) M" and x: "x \<in> space M1"
+  shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"
+  using measurable_comp[OF measurable_Pair1' f, OF x]
+  by (simp add: comp_def)
+  
+lemma measurable_Pair1:
+  assumes f: "f \<in> measurable (M1 \<Otimes>\<^isub>M M2) M" and y: "y \<in> space M2"
   shows "(\<lambda>x. f (x, y)) \<in> measurable M1 M"
-proof -
-  interpret Q: pair_sigma_algebra M2 M1 by default
-  from Q.measurable_pair_image_snd[OF measurable_comp `y \<in> space M2`,
-                                      OF Q.pair_sigma_algebra_swap_measurable m]
-  show ?thesis by simp
-qed
+  using measurable_comp[OF measurable_Pair2' f, OF y]
+  by (simp add: comp_def)
 
-lemma (in pair_sigma_algebra) Int_stable_pair_measure_generator: "Int_stable E"
+lemma Int_stable_pair_measure_generator: "Int_stable {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}"
   unfolding Int_stable_def
-proof (intro ballI)
-  fix A B assume "A \<in> sets E" "B \<in> sets E"
-  then obtain A1 A2 B1 B2 where "A = A1 \<times> A2" "B = B1 \<times> B2"
-    "A1 \<in> sets M1" "A2 \<in> sets M2" "B1 \<in> sets M1" "B2 \<in> sets M2"
-    unfolding pair_measure_generator_def by auto
-  then show "A \<inter> B \<in> sets E"
-    by (auto simp add: times_Int_times pair_measure_generator_def)
-qed
+  by safe (auto simp add: times_Int_times)
 
 lemma finite_measure_cut_measurable:
-  fixes M1 :: "('a, 'c) measure_space_scheme" and M2 :: "('b, 'd) measure_space_scheme"
   assumes "sigma_finite_measure M1" "finite_measure M2"
   assumes "Q \<in> sets (M1 \<Otimes>\<^isub>M M2)"
-  shows "(\<lambda>x. measure M2 (Pair x -` Q)) \<in> borel_measurable M1"
+  shows "(\<lambda>x. emeasure M2 (Pair x -` Q)) \<in> borel_measurable M1"
     (is "?s Q \<in> _")
 proof -
   interpret M1: sigma_finite_measure M1 by fact
   interpret M2: finite_measure M2 by fact
-  interpret pair_sigma_algebra M1 M2 by default
-  have [intro]: "sigma_algebra M1" by fact
-  have [intro]: "sigma_algebra M2" by fact
-  let ?D = "\<lparr> space = space P, sets = {A\<in>sets P. ?s A \<in> borel_measurable M1}  \<rparr>"
+  let ?\<Omega> = "space M1 \<times> space M2" and ?D = "{A\<in>sets (M1 \<Otimes>\<^isub>M M2). ?s A \<in> borel_measurable M1}"
   note space_pair_measure[simp]
-  interpret dynkin_system ?D
+  interpret dynkin_system ?\<Omega> ?D
   proof (intro dynkin_systemI)
-    fix A assume "A \<in> sets ?D" then show "A \<subseteq> space ?D"
-      using sets_into_space by simp
+    fix A assume "A \<in> ?D" then show "A \<subseteq> ?\<Omega>"
+      using sets_into_space[of A "M1 \<Otimes>\<^isub>M M2"] by simp
   next
-    from top show "space ?D \<in> sets ?D"
-      by (auto simp add: if_distrib intro!: M1.measurable_If)
+    from top show "?\<Omega> \<in> ?D"
+      by (auto simp add: if_distrib intro!: measurable_If)
   next
-    fix A assume "A \<in> sets ?D"
-    with sets_into_space have "\<And>x. measure M2 (Pair x -` (space M1 \<times> space M2 - A)) =
-        (if x \<in> space M1 then measure M2 (space M2) - ?s A x else 0)"
-      by (auto intro!: M2.measure_compl simp: vimage_Diff)
-    with `A \<in> sets ?D` top show "space ?D - A \<in> sets ?D"
-      by (auto intro!: Diff M1.measurable_If M1.borel_measurable_ereal_diff)
+    fix A assume "A \<in> ?D"
+    with sets_into_space have "\<And>x. emeasure M2 (Pair x -` (?\<Omega> - A)) =
+        (if x \<in> space M1 then emeasure M2 (space M2) - ?s A x else 0)"
+      by (auto intro!: emeasure_compl simp: vimage_Diff sets_Pair1)
+    with `A \<in> ?D` top show "?\<Omega> - A \<in> ?D"
+      by (auto intro!: measurable_If)
   next
-    fix F :: "nat \<Rightarrow> ('a\<times>'b) set" assume "disjoint_family F" "range F \<subseteq> sets ?D"
-    moreover then have "\<And>x. measure M2 (\<Union>i. Pair x -` F i) = (\<Sum>i. ?s (F i) x)"
-      by (intro M2.measure_countably_additive[symmetric])
-         (auto simp: disjoint_family_on_def)
-    ultimately show "(\<Union>i. F i) \<in> sets ?D"
-      by (auto simp: vimage_UN intro!: M1.borel_measurable_psuminf)
+    fix F :: "nat \<Rightarrow> ('a\<times>'b) set" assume "disjoint_family F" "range F \<subseteq> ?D"
+    moreover then have "\<And>x. emeasure M2 (\<Union>i. Pair x -` F i) = (\<Sum>i. ?s (F i) x)"
+      by (intro suminf_emeasure[symmetric]) (auto simp: disjoint_family_on_def sets_Pair1)
+    ultimately show "(\<Union>i. F i) \<in> ?D"
+      by (auto simp: vimage_UN intro!: borel_measurable_psuminf)
   qed
-  have "sets P = sets ?D" apply (subst pair_measure_def)
-  proof (intro dynkin_lemma)
-    show "Int_stable E" by (rule Int_stable_pair_measure_generator)
-    from M1.sets_into_space have "\<And>A. A \<in> sets M1 \<Longrightarrow> {x \<in> space M1. x \<in> A} = A"
-      by auto
-    then show "sets E \<subseteq> sets ?D"
-      by (auto simp: pair_measure_generator_def sets_sigma if_distrib
-               intro: sigma_sets.Basic intro!: M1.measurable_If)
-  qed (auto simp: pair_measure_def)
-  with `Q \<in> sets P` have "Q \<in> sets ?D" by simp
+  let ?G = "{a \<times> b | a b. a \<in> sets M1 \<and> b \<in> sets M2}"
+  have "sigma_sets ?\<Omega> ?G = ?D"
+  proof (rule dynkin_lemma)
+    show "?G \<subseteq> ?D"
+      by (auto simp: if_distrib Int_def[symmetric] intro!: measurable_If)
+  qed (auto simp: sets_pair_measure  Int_stable_pair_measure_generator)
+  with `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)` have "Q \<in> ?D"
+    by (simp add: sets_pair_measure[symmetric])
   then show "?s Q \<in> borel_measurable M1" by simp
 qed
 
-subsection {* Binary products of $\sigma$-finite measure spaces *}
+subsection {* Binary products of $\sigma$-finite emeasure spaces *}
 
-locale pair_sigma_finite = pair_sigma_algebra M1 M2 + M1: sigma_finite_measure M1 + M2: sigma_finite_measure M2
-  for M1 :: "('a, 'c) measure_space_scheme" and M2 :: "('b, 'd) measure_space_scheme"
+locale pair_sigma_finite = M1: sigma_finite_measure M1 + M2: sigma_finite_measure M2
+  for M1 :: "'a measure" and M2 :: "'b measure"
 
-lemma (in pair_sigma_finite) measure_cut_measurable_fst:
-  assumes "Q \<in> sets P" shows "(\<lambda>x. measure M2 (Pair x -` Q)) \<in> borel_measurable M1" (is "?s Q \<in> _")
+lemma sets_pair_measure_cong[cong]:
+  "sets M1 = sets M1' \<Longrightarrow> sets M2 = sets M2' \<Longrightarrow> sets (M1 \<Otimes>\<^isub>M M2) = sets (M1' \<Otimes>\<^isub>M M2')"
+  unfolding sets_pair_measure by (simp cong: sets_eq_imp_space_eq)
+
+lemma (in pair_sigma_finite) measurable_emeasure_Pair1:
+  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> _")
 proof -
-  have [intro]: "sigma_algebra M1" and [intro]: "sigma_algebra M2" by default+
-  have M1: "sigma_finite_measure M1" by default
-  from M2.disjoint_sigma_finite guess F .. note F = this
+  from M2.sigma_finite_disjoint guess F . note F = this
   then have F_sets: "\<And>i. F i \<in> sets M2" by auto
+  have M1: "sigma_finite_measure M1" ..
   let ?C = "\<lambda>x i. F i \<inter> Pair x -` Q"
   { fix i
-    let ?R = "M2.restricted_space (F i)"
     have [simp]: "space M1 \<times> F i \<inter> space M1 \<times> space M2 = space M1 \<times> F i"
-      using F M2.sets_into_space by auto
-    let ?R2 = "M2.restricted_space (F i)"
-    have "(\<lambda>x. measure ?R2 (Pair x -` (space M1 \<times> space ?R2 \<inter> Q))) \<in> borel_measurable M1"
+      using F sets_into_space by auto
+    let ?R = "density M2 (indicator (F i))"
+    have "(\<lambda>x. emeasure ?R (Pair x -` (space M1 \<times> space ?R \<inter> Q))) \<in> borel_measurable M1"
     proof (intro finite_measure_cut_measurable[OF M1])
-      show "finite_measure ?R2"
-        using F by (intro M2.restricted_to_finite_measure) auto
-      have "(space M1 \<times> space ?R2) \<inter> Q \<in> (op \<inter> (space M1 \<times> F i)) ` sets P"
-        using `Q \<in> sets P` by (auto simp: image_iff)
-      also have "\<dots> = sigma_sets (space M1 \<times> F i) ((op \<inter> (space M1 \<times> F i)) ` sets E)"
-        unfolding pair_measure_def pair_measure_generator_def sigma_def
-        using `F i \<in> sets M2` M2.sets_into_space
-        by (auto intro!: sigma_sets_Int sigma_sets.Basic)
-      also have "\<dots> \<subseteq> sets (M1 \<Otimes>\<^isub>M ?R2)"
-        using M1.sets_into_space
-        apply (auto simp: times_Int_times pair_measure_def pair_measure_generator_def sigma_def
-                    intro!: sigma_sets_subseteq)
-        apply (rule_tac x="a" in exI)
-        apply (rule_tac x="b \<inter> F i" in exI)
-        by auto
-      finally show "(space M1 \<times> space ?R2) \<inter> Q \<in> sets (M1 \<Otimes>\<^isub>M ?R2)" .
+      show "finite_measure ?R"
+        using F by (intro finite_measureI) (auto simp: emeasure_restricted subset_eq)
+      show "(space M1 \<times> space ?R) \<inter> Q \<in> sets (M1 \<Otimes>\<^isub>M ?R)"
+        using Q by (simp add: Int)
     qed
-    moreover have "\<And>x. Pair x -` (space M1 \<times> F i \<inter> Q) = ?C x i"
-      using `Q \<in> sets P` sets_into_space by (auto simp: space_pair_measure)
-    ultimately have "(\<lambda>x. measure M2 (?C x i)) \<in> borel_measurable M1"
+    moreover have "\<And>x. emeasure ?R (Pair x -` (space M1 \<times> space ?R \<inter> Q))
+        = emeasure M2 (F i \<inter> Pair x -` (space M1 \<times> space ?R \<inter> Q))"
+      using Q F_sets by (intro emeasure_restricted) (auto intro: sets_Pair1)
+    moreover have "\<And>x. F i \<inter> Pair x -` (space M1 \<times> space ?R \<inter> Q) = ?C x i"
+      using sets_into_space[OF Q] by (auto simp: space_pair_measure)
+    ultimately have "(\<lambda>x. emeasure M2 (?C x i)) \<in> borel_measurable M1"
       by simp }
   moreover
   { fix x
-    have "(\<Sum>i. measure M2 (?C x i)) = measure M2 (\<Union>i. ?C x i)"
-    proof (intro M2.measure_countably_additive)
+    have "(\<Sum>i. emeasure M2 (?C x i)) = emeasure M2 (\<Union>i. ?C x i)"
+    proof (intro suminf_emeasure)
       show "range (?C x) \<subseteq> sets M2"
-        using F `Q \<in> sets P` by (auto intro!: M2.Int)
+        using F `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)` by (auto intro!: sets_Pair1)
       have "disjoint_family F" using F by auto
       show "disjoint_family (?C x)"
         by (rule disjoint_family_on_bisimulation[OF `disjoint_family F`]) auto
     qed
     also have "(\<Union>i. ?C x i) = Pair x -` Q"
-      using F sets_into_space `Q \<in> sets P`
+      using F sets_into_space[OF `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)`]
       by (auto simp: space_pair_measure)
-    finally have "measure M2 (Pair x -` Q) = (\<Sum>i. measure M2 (?C x i))"
+    finally have "emeasure M2 (Pair x -` Q) = (\<Sum>i. emeasure M2 (?C x i))"
       by simp }
-  ultimately show ?thesis using `Q \<in> sets P` F_sets
-    by (auto intro!: M1.borel_measurable_psuminf M2.Int)
+  ultimately show ?thesis using `Q \<in> sets (M1 \<Otimes>\<^isub>M M2)` F_sets
+    by auto
 qed
 
-lemma (in pair_sigma_finite) measure_cut_measurable_snd:
-  assumes "Q \<in> sets P" shows "(\<lambda>y. M1.\<mu> ((\<lambda>x. (x, y)) -` Q)) \<in> borel_measurable M2"
+lemma (in pair_sigma_finite) measurable_emeasure_Pair2:
+  assumes Q: "Q \<in> sets (M1 \<Otimes>\<^isub>M M2)" shows "(\<lambda>y. emeasure M1 ((\<lambda>x. (x, y)) -` Q)) \<in> borel_measurable M2"
 proof -
   interpret Q: pair_sigma_finite M2 M1 by default
-  note sets_pair_sigma_algebra_swap[OF assms]
-  from Q.measure_cut_measurable_fst[OF this]
-  show ?thesis by (simp add: vimage_compose[symmetric] comp_def)
-qed
-
-lemma (in pair_sigma_algebra) pair_sigma_algebra_measurable:
-  assumes "f \<in> measurable P M" shows "(\<lambda>(x,y). f (y, x)) \<in> measurable (M2 \<Otimes>\<^isub>M M1) M"
-proof -
-  interpret Q: pair_sigma_algebra M2 M1 by default
-  have *: "(\<lambda>(x,y). f (y, x)) = f \<circ> (\<lambda>(x,y). (y, x))" by (simp add: fun_eq_iff)
-  show ?thesis
-    using Q.pair_sigma_algebra_swap_measurable assms
-    unfolding * by (rule measurable_comp)
+  have "(\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^isub>M M1) \<in> sets (M2 \<Otimes>\<^isub>M M1)"
+    using Q measurable_pair_swap' by (auto intro: measurable_sets)
+  note Q.measurable_emeasure_Pair1[OF this]
+  moreover have "\<And>y. Pair y -` ((\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^isub>M M1)) = (\<lambda>x. (x, y)) -` Q"
+    using Q[THEN sets_into_space] by (auto simp: space_pair_measure)
+  ultimately show ?thesis by simp
 qed
 
-lemma (in pair_sigma_finite) pair_measure_alt:
-  assumes "A \<in> sets P"
-  shows "measure (M1 \<Otimes>\<^isub>M M2) A = (\<integral>\<^isup>+ x. measure M2 (Pair x -` A) \<partial>M1)"
-  apply (simp add: pair_measure_def pair_measure_generator_def)
-proof (rule M1.positive_integral_cong)
-  fix x assume "x \<in> space M1"
-  have *: "\<And>y. indicator A (x, y) = (indicator (Pair x -` A) y :: ereal)"
-    unfolding indicator_def by auto
-  show "(\<integral>\<^isup>+ y. indicator A (x, y) \<partial>M2) = measure M2 (Pair x -` A)"
-    unfolding *
-    apply (subst M2.positive_integral_indicator)
-    apply (rule measurable_cut_fst[OF assms])
-    by simp
+lemma (in pair_sigma_finite) emeasure_pair_measure:
+  assumes "X \<in> sets (M1 \<Otimes>\<^isub>M M2)"
+  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")
+proof (rule emeasure_measure_of[OF pair_measure_def])
+  show "positive (sets (M1 \<Otimes>\<^isub>M M2)) ?\<mu>"
+    by (auto simp: positive_def positive_integral_positive)
+  have eq[simp]: "\<And>A x y. indicator A (x, y) = indicator (Pair x -` A) y"
+    by (auto simp: indicator_def)
+  show "countably_additive (sets (M1 \<Otimes>\<^isub>M M2)) ?\<mu>"
+  proof (rule countably_additiveI)
+    fix F :: "nat \<Rightarrow> ('a \<times> 'b) set" assume F: "range F \<subseteq> sets (M1 \<Otimes>\<^isub>M M2)" "disjoint_family F"
+    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
+    moreover from F have "\<And>i. (\<lambda>x. emeasure M2 (Pair x -` F i)) \<in> borel_measurable M1"
+      by (intro measurable_emeasure_Pair1) auto
+    moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
+      by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
+    moreover have "\<And>x. range (\<lambda>i. Pair x -` F i) \<subseteq> sets M2"
+      using F by (auto simp: sets_Pair1)
+    ultimately show "(\<Sum>n. ?\<mu> (F n)) = ?\<mu> (\<Union>i. F i)"
+      by (auto simp add: vimage_UN positive_integral_suminf[symmetric] suminf_emeasure subset_eq emeasure_nonneg sets_Pair1
+               intro!: positive_integral_cong positive_integral_indicator[symmetric])
+  qed
+  show "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2} \<subseteq> Pow (space M1 \<times> space M2)"
+    using space_closed[of M1] space_closed[of M2] by auto
+qed fact
+
+lemma (in pair_sigma_finite) emeasure_pair_measure_alt:
+  assumes X: "X \<in> sets (M1 \<Otimes>\<^isub>M M2)"
+  shows "emeasure (M1  \<Otimes>\<^isub>M M2) X = (\<integral>\<^isup>+x. emeasure M2 (Pair x -` X) \<partial>M1)"
+proof -
+  have [simp]: "\<And>x y. indicator X (x, y) = indicator (Pair x -` X) y"
+    by (auto simp: indicator_def)
+  show ?thesis
+    using X by (auto intro!: positive_integral_cong simp: emeasure_pair_measure sets_Pair1)
 qed
 
-lemma (in pair_sigma_finite) pair_measure_times:
-  assumes A: "A \<in> sets M1" and "B \<in> sets M2"
-  shows "measure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = M1.\<mu> A * measure M2 B"
+lemma (in pair_sigma_finite) emeasure_pair_measure_Times:
+  assumes A: "A \<in> sets M1" and B: "B \<in> sets M2"
+  shows "emeasure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = emeasure M1 A * emeasure M2 B"
 proof -
-  have "measure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = (\<integral>\<^isup>+ x. measure M2 B * indicator A x \<partial>M1)"
-    using assms by (auto intro!: M1.positive_integral_cong simp: pair_measure_alt)
-  with assms show ?thesis
-    by (simp add: M1.positive_integral_cmult_indicator ac_simps)
+  have "emeasure (M1 \<Otimes>\<^isub>M M2) (A \<times> B) = (\<integral>\<^isup>+x. emeasure M2 B * indicator A x \<partial>M1)"
+    using A B by (auto intro!: positive_integral_cong simp: emeasure_pair_measure_alt)
+  also have "\<dots> = emeasure M2 B * emeasure M1 A"
+    using A by (simp add: emeasure_nonneg positive_integral_cmult_indicator)
+  finally show ?thesis
+    by (simp add: ac_simps)
 qed
 
-lemma (in measure_space) measure_not_negative[simp,intro]:
-  assumes A: "A \<in> sets M" shows "\<mu> A \<noteq> - \<infinity>"
-  using positive_measure[OF A] by auto
-
 lemma (in pair_sigma_finite) sigma_finite_up_in_pair_measure_generator:
-  "\<exists>F::nat \<Rightarrow> ('a \<times> 'b) set. range F \<subseteq> sets E \<and> incseq F \<and> (\<Union>i. F i) = space E \<and>
-    (\<forall>i. measure (M1 \<Otimes>\<^isub>M M2) (F i) \<noteq> \<infinity>)"
+  defines "E \<equiv> {A \<times> B | A B. A \<in> sets M1 \<and> B \<in> sets M2}"
+  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>
+    (\<forall>i. emeasure (M1 \<Otimes>\<^isub>M M2) (F i) \<noteq> \<infinity>)"
 proof -
-  obtain F1 :: "nat \<Rightarrow> 'a set" and F2 :: "nat \<Rightarrow> 'b set" where
-    F1: "range F1 \<subseteq> sets M1" "incseq F1" "(\<Union>i. F1 i) = space M1" "\<And>i. M1.\<mu> (F1 i) \<noteq> \<infinity>" and
-    F2: "range F2 \<subseteq> sets M2" "incseq F2" "(\<Union>i. F2 i) = space M2" "\<And>i. M2.\<mu> (F2 i) \<noteq> \<infinity>"
-    using M1.sigma_finite_up M2.sigma_finite_up by auto
-  then have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)" by auto
+  from M1.sigma_finite_incseq guess F1 . note F1 = this
+  from M2.sigma_finite_incseq guess F2 . note F2 = this
+  from F1 F2 have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)" by auto
   let ?F = "\<lambda>i. F1 i \<times> F2 i"
-  show ?thesis unfolding space_pair_measure
+  show ?thesis
   proof (intro exI[of _ ?F] conjI allI)
-    show "range ?F \<subseteq> sets E" using F1 F2
-      by (fastforce intro!: pair_measure_generatorI)
+    show "range ?F \<subseteq> E" using F1 F2 by (auto simp: E_def) (metis range_subsetD)
   next
     have "space M1 \<times> space M2 \<subseteq> (\<Union>i. ?F i)"
     proof (intro subsetI)
@@ -448,353 +352,315 @@
         by (intro SigmaI) (auto simp add: min_max.sup_commute)
       then show "x \<in> (\<Union>i. ?F i)" by auto
     qed
-    then show "(\<Union>i. ?F i) = space E"
-      using space by (auto simp: space pair_measure_generator_def)
+    then show "(\<Union>i. ?F i) = space M1 \<times> space M2"
+      using space by (auto simp: space)
   next
     fix i show "incseq (\<lambda>i. F1 i \<times> F2 i)"
       using `incseq F1` `incseq F2` unfolding incseq_Suc_iff by auto
   next
     fix i
     from F1 F2 have "F1 i \<in> sets M1" "F2 i \<in> sets M2" by auto
-    with F1 F2 M1.positive_measure[OF this(1)] M2.positive_measure[OF this(2)]
-    show "measure P (F1 i \<times> F2 i) \<noteq> \<infinity>"
-      by (simp add: pair_measure_times)
+    with F1 F2 emeasure_nonneg[of M1 "F1 i"] emeasure_nonneg[of M2 "F2 i"]
+    show "emeasure (M1 \<Otimes>\<^isub>M M2) (F1 i \<times> F2 i) \<noteq> \<infinity>"
+      by (auto simp add: emeasure_pair_measure_Times)
+  qed
+qed
+
+sublocale pair_sigma_finite \<subseteq> sigma_finite_measure "M1 \<Otimes>\<^isub>M M2"
+proof
+  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
+  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>)"
+  proof (rule exI[of _ F], intro conjI)
+    show "range F \<subseteq> sets (M1 \<Otimes>\<^isub>M M2)" using F by (auto simp: pair_measure_def)
+    show "(\<Union>i. F i) = space (M1 \<Otimes>\<^isub>M M2)"
+      using F by (auto simp: space_pair_measure)
+    show "\<forall>i. emeasure (M1 \<Otimes>\<^isub>M M2) (F i) \<noteq> \<infinity>" using F by auto
   qed
 qed
 
-sublocale pair_sigma_finite \<subseteq> sigma_finite_measure P
-proof
-  show "positive P (measure P)"
-    unfolding pair_measure_def pair_measure_generator_def sigma_def positive_def
-    by (auto intro: M1.positive_integral_positive M2.positive_integral_positive)
-
-  show "countably_additive P (measure P)"
-    unfolding countably_additive_def
-  proof (intro allI impI)
-    fix F :: "nat \<Rightarrow> ('a \<times> 'b) set"
-    assume F: "range F \<subseteq> sets P" "disjoint_family F"
-    from F have *: "\<And>i. F i \<in> sets P" "(\<Union>i. F i) \<in> sets P" by auto
-    moreover from F have "\<And>i. (\<lambda>x. measure M2 (Pair x -` F i)) \<in> borel_measurable M1"
-      by (intro measure_cut_measurable_fst) auto
-    moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
-      by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
-    moreover have "\<And>x. x \<in> space M1 \<Longrightarrow> range (\<lambda>i. Pair x -` F i) \<subseteq> sets M2"
-      using F by auto
-    ultimately show "(\<Sum>n. measure P (F n)) = measure P (\<Union>i. F i)"
-      by (simp add: pair_measure_alt vimage_UN M1.positive_integral_suminf[symmetric]
-                    M2.measure_countably_additive
-               cong: M1.positive_integral_cong)
-  qed
-
-  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
-  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>)"
-  proof (rule exI[of _ F], intro conjI)
-    show "range F \<subseteq> sets P" using F by (auto simp: pair_measure_def)
-    show "(\<Union>i. F i) = space P"
-      using F by (auto simp: pair_measure_def pair_measure_generator_def)
-    show "\<forall>i. measure P (F i) \<noteq> \<infinity>" using F by auto
-  qed
+lemma sigma_finite_pair_measure:
+  assumes A: "sigma_finite_measure A" and B: "sigma_finite_measure B"
+  shows "sigma_finite_measure (A \<Otimes>\<^isub>M B)"
+proof -
+  interpret A: sigma_finite_measure A by fact
+  interpret B: sigma_finite_measure B by fact
+  interpret AB: pair_sigma_finite A  B ..
+  show ?thesis ..
 qed
 
-lemma (in pair_sigma_algebra) sets_swap:
-  assumes "A \<in> sets P"
+lemma sets_pair_swap:
+  assumes "A \<in> sets (M1 \<Otimes>\<^isub>M M2)"
   shows "(\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^isub>M M1) \<in> sets (M2 \<Otimes>\<^isub>M M1)"
-    (is "_ -` A \<inter> space ?Q \<in> sets ?Q")
-proof -
-  have *: "(\<lambda>(x, y). (y, x)) -` A \<inter> space ?Q = (\<lambda>(x, y). (y, x)) -` A"
-    using `A \<in> sets P` sets_into_space by (auto simp: space_pair_measure)
-  show ?thesis
-    unfolding * using assms by (rule sets_pair_sigma_algebra_swap)
-qed
+  using measurable_pair_swap' assms by (rule measurable_sets)
 
-lemma (in pair_sigma_finite) pair_measure_alt2:
-  assumes A: "A \<in> sets P"
-  shows "\<mu> A = (\<integral>\<^isup>+y. M1.\<mu> ((\<lambda>x. (x, y)) -` A) \<partial>M2)"
-    (is "_ = ?\<nu> A")
+lemma (in pair_sigma_finite) distr_pair_swap:
+  "M1 \<Otimes>\<^isub>M M2 = distr (M2 \<Otimes>\<^isub>M M1) (M1 \<Otimes>\<^isub>M M2) (\<lambda>(x, y). (y, x))" (is "?P = ?D")
 proof -
   interpret Q: pair_sigma_finite M2 M1 by default
   from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
-  have [simp]: "\<And>m. \<lparr> space = space E, sets = sets (sigma E), measure = m \<rparr> = P\<lparr> measure := m \<rparr>"
-    unfolding pair_measure_def by simp
-
-  have "\<mu> A = Q.\<mu> ((\<lambda>(y, x). (x, y)) -` A \<inter> space Q.P)"
-  proof (rule measure_unique_Int_stable_vimage[OF Int_stable_pair_measure_generator])
-    show "measure_space P" "measure_space Q.P" by default
-    show "(\<lambda>(y, x). (x, y)) \<in> measurable Q.P P" by (rule Q.pair_sigma_algebra_swap_measurable)
-    show "sets (sigma E) = sets P" "space E = space P" "A \<in> sets (sigma E)"
-      using assms unfolding pair_measure_def by auto
-    show "range F \<subseteq> sets E" "incseq F" "(\<Union>i. F i) = space E" "\<And>i. \<mu> (F i) \<noteq> \<infinity>"
-      using F `A \<in> sets P` by (auto simp: pair_measure_def)
-    fix X assume "X \<in> sets E"
-    then obtain A B where X[simp]: "X = A \<times> B" and AB: "A \<in> sets M1" "B \<in> sets M2"
-      unfolding pair_measure_def pair_measure_generator_def by auto
-    then have "(\<lambda>(y, x). (x, y)) -` X \<inter> space Q.P = B \<times> A"
-      using M1.sets_into_space M2.sets_into_space by (auto simp: space_pair_measure)
-    then show "\<mu> X = Q.\<mu> ((\<lambda>(y, x). (x, y)) -` X \<inter> space Q.P)"
-      using AB by (simp add: pair_measure_times Q.pair_measure_times ac_simps)
+  let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
+  show ?thesis
+  proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
+    show "?E \<subseteq> Pow (space ?P)"
+      using space_closed[of M1] space_closed[of M2] by (auto simp: space_pair_measure)
+    show "sets ?P = sigma_sets (space ?P) ?E"
+      by (simp add: sets_pair_measure space_pair_measure)
+    then show "sets ?D = sigma_sets (space ?P) ?E"
+      by simp
+  next
+    show "range F \<subseteq> ?E" "incseq F" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
+      using F by (auto simp: space_pair_measure)
+  next
+    fix X assume "X \<in> ?E"
+    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
+    have "(\<lambda>(y, x). (x, y)) -` X \<inter> space (M2 \<Otimes>\<^isub>M M1) = B \<times> A"
+      using sets_into_space[OF A] sets_into_space[OF B] by (auto simp: space_pair_measure)
+    with A B show "emeasure (M1 \<Otimes>\<^isub>M M2) X = emeasure ?D X"
+      by (simp add: emeasure_pair_measure_Times Q.emeasure_pair_measure_Times emeasure_distr
+                    measurable_pair_swap' ac_simps)
   qed
-  then show ?thesis
-    using sets_into_space[OF A] Q.pair_measure_alt[OF sets_swap[OF A]]
-    by (auto simp add: Q.pair_measure_alt space_pair_measure
-             intro!: M2.positive_integral_cong arg_cong[where f="M1.\<mu>"])
 qed
 
-lemma pair_sigma_algebra_sigma:
-  assumes 1: "incseq S1" "(\<Union>i. S1 i) = space E1" "range S1 \<subseteq> sets E1" and E1: "sets E1 \<subseteq> Pow (space E1)"
-  assumes 2: "decseq S2" "(\<Union>i. S2 i) = space E2" "range S2 \<subseteq> sets E2" and E2: "sets E2 \<subseteq> Pow (space E2)"
-  shows "sets (sigma (pair_measure_generator (sigma E1) (sigma E2))) = sets (sigma (pair_measure_generator E1 E2))"
-    (is "sets ?S = sets ?E")
+lemma (in pair_sigma_finite) emeasure_pair_measure_alt2:
+  assumes A: "A \<in> sets (M1 \<Otimes>\<^isub>M M2)"
+  shows "emeasure (M1 \<Otimes>\<^isub>M M2) A = (\<integral>\<^isup>+y. emeasure M1 ((\<lambda>x. (x, y)) -` A) \<partial>M2)"
+    (is "_ = ?\<nu> A")
 proof -
-  interpret M1: sigma_algebra "sigma E1" using E1 by (rule sigma_algebra_sigma)
-  interpret M2: sigma_algebra "sigma E2" using E2 by (rule sigma_algebra_sigma)
-  have P: "sets (pair_measure_generator E1 E2) \<subseteq> Pow (space E1 \<times> space E2)"
-    using E1 E2 by (auto simp add: pair_measure_generator_def)
-  interpret E: sigma_algebra ?E unfolding pair_measure_generator_def
-    using E1 E2 by (intro sigma_algebra_sigma) auto
-  { fix A assume "A \<in> sets E1"
-    then have "fst -` A \<inter> space ?E = A \<times> (\<Union>i. S2 i)"
-      using E1 2 unfolding pair_measure_generator_def by auto
-    also have "\<dots> = (\<Union>i. A \<times> S2 i)" by auto
-    also have "\<dots> \<in> sets ?E" unfolding pair_measure_generator_def sets_sigma
-      using 2 `A \<in> sets E1`
-      by (intro sigma_sets.Union)
-         (force simp: image_subset_iff intro!: sigma_sets.Basic)
-    finally have "fst -` A \<inter> space ?E \<in> sets ?E" . }
-  moreover
-  { fix B assume "B \<in> sets E2"
-    then have "snd -` B \<inter> space ?E = (\<Union>i. S1 i) \<times> B"
-      using E2 1 unfolding pair_measure_generator_def by auto
-    also have "\<dots> = (\<Union>i. S1 i \<times> B)" by auto
-    also have "\<dots> \<in> sets ?E"
-      using 1 `B \<in> sets E2` unfolding pair_measure_generator_def sets_sigma
-      by (intro sigma_sets.Union)
-         (force simp: image_subset_iff intro!: sigma_sets.Basic)
-    finally have "snd -` B \<inter> space ?E \<in> sets ?E" . }
-  ultimately have proj:
-    "fst \<in> measurable ?E (sigma E1) \<and> snd \<in> measurable ?E (sigma E2)"
-    using E1 E2 by (subst (1 2) E.measurable_iff_sigma)
-                   (auto simp: pair_measure_generator_def sets_sigma)
-  { fix A B assume A: "A \<in> sets (sigma E1)" and B: "B \<in> sets (sigma E2)"
-    with proj have "fst -` A \<inter> space ?E \<in> sets ?E" "snd -` B \<inter> space ?E \<in> sets ?E"
-      unfolding measurable_def by simp_all
-    moreover have "A \<times> B = (fst -` A \<inter> space ?E) \<inter> (snd -` B \<inter> space ?E)"
-      using A B M1.sets_into_space M2.sets_into_space
-      by (auto simp: pair_measure_generator_def)
-    ultimately have "A \<times> B \<in> sets ?E" by auto }
-  then have "sigma_sets (space ?E) (sets (pair_measure_generator (sigma E1) (sigma E2))) \<subseteq> sets ?E"
-    by (intro E.sigma_sets_subset) (auto simp add: pair_measure_generator_def sets_sigma)
-  then have subset: "sets ?S \<subseteq> sets ?E"
-    by (simp add: sets_sigma pair_measure_generator_def)
-  show "sets ?S = sets ?E"
-  proof (intro set_eqI iffI)
-    fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
-      unfolding sets_sigma
-    proof induct
-      case (Basic A) then show ?case
-        by (auto simp: pair_measure_generator_def sets_sigma intro: sigma_sets.Basic)
-    qed (auto intro: sigma_sets.intros simp: pair_measure_generator_def)
-  next
-    fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
-  qed
+  interpret Q: pair_sigma_finite M2 M1 by default
+  have [simp]: "\<And>y. (Pair y -` ((\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^isub>M M1))) = (\<lambda>x. (x, y)) -` A"
+    using sets_into_space[OF A] by (auto simp: space_pair_measure)
+  show ?thesis using A
+    by (subst distr_pair_swap)
+       (simp_all del: vimage_Int add: measurable_sets[OF measurable_pair_swap']
+                 Q.emeasure_pair_measure_alt emeasure_distr[OF measurable_pair_swap' A])
 qed
 
 section "Fubinis theorem"
 
 lemma (in pair_sigma_finite) simple_function_cut:
-  assumes f: "simple_function P f" "\<And>x. 0 \<le> f x"
+  assumes f: "simple_function (M1 \<Otimes>\<^isub>M M2) f" "\<And>x. 0 \<le> f x"
   shows "(\<lambda>x. \<integral>\<^isup>+y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
-    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P P f"
+    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
 proof -
-  have f_borel: "f \<in> borel_measurable P"
+  have f_borel: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
     using f(1) by (rule borel_measurable_simple_function)
-  let ?F = "\<lambda>z. f -` {z} \<inter> space P"
+  let ?F = "\<lambda>z. f -` {z} \<inter> space (M1 \<Otimes>\<^isub>M M2)"
   let ?F' = "\<lambda>x z. Pair x -` ?F z"
   { fix x assume "x \<in> space M1"
     have [simp]: "\<And>z y. indicator (?F z) (x, y) = indicator (?F' x z) y"
       by (auto simp: indicator_def)
-    have "\<And>y. y \<in> space M2 \<Longrightarrow> (x, y) \<in> space P" using `x \<in> space M1`
+    have "\<And>y. y \<in> space M2 \<Longrightarrow> (x, y) \<in> space (M1 \<Otimes>\<^isub>M M2)" using `x \<in> space M1`
       by (simp add: space_pair_measure)
     moreover have "\<And>x z. ?F' x z \<in> sets M2" using f_borel
-      by (intro borel_measurable_vimage measurable_cut_fst)
+      by (rule sets_Pair1[OF measurable_sets]) auto
     ultimately have "simple_function M2 (\<lambda> y. f (x, y))"
-      apply (rule_tac M2.simple_function_cong[THEN iffD2, OF _])
+      apply (rule_tac simple_function_cong[THEN iffD2, OF _])
       apply (rule simple_function_indicator_representation[OF f(1)])
-      using `x \<in> space M1` by (auto simp del: space_sigma) }
+      using `x \<in> space M1` by auto }
   note M2_sf = this
   { fix x assume x: "x \<in> space M1"
-    then have "(\<integral>\<^isup>+y. f (x, y) \<partial>M2) = (\<Sum>z\<in>f ` space P. z * M2.\<mu> (?F' x z))"
-      unfolding M2.positive_integral_eq_simple_integral[OF M2_sf[OF x] f(2)]
+    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))"
+      unfolding positive_integral_eq_simple_integral[OF M2_sf[OF x] f(2)]
       unfolding simple_integral_def
     proof (safe intro!: setsum_mono_zero_cong_left)
-      from f(1) show "finite (f ` space P)" by (rule simple_functionD)
+      from f(1) show "finite (f ` space (M1 \<Otimes>\<^isub>M M2))" by (rule simple_functionD)
     next
-      fix y assume "y \<in> space M2" then show "f (x, y) \<in> f ` space P"
+      fix y assume "y \<in> space M2" then show "f (x, y) \<in> f ` space (M1 \<Otimes>\<^isub>M M2)"
         using `x \<in> space M1` by (auto simp: space_pair_measure)
     next
-      fix x' y assume "(x', y) \<in> space P"
+      fix x' y assume "(x', y) \<in> space (M1 \<Otimes>\<^isub>M M2)"
         "f (x', y) \<notin> (\<lambda>y. f (x, y)) ` space M2"
       then have *: "?F' x (f (x', y)) = {}"
         by (force simp: space_pair_measure)
-      show  "f (x', y) * M2.\<mu> (?F' x (f (x', y))) = 0"
+      show  "f (x', y) * emeasure M2 (?F' x (f (x', y))) = 0"
         unfolding * by simp
     qed (simp add: vimage_compose[symmetric] comp_def
                    space_pair_measure) }
   note eq = this
-  moreover have "\<And>z. ?F z \<in> sets P"
-    by (auto intro!: f_borel borel_measurable_vimage simp del: space_sigma)
-  moreover then have "\<And>z. (\<lambda>x. M2.\<mu> (?F' x z)) \<in> borel_measurable M1"
-    by (auto intro!: measure_cut_measurable_fst simp del: vimage_Int)
-  moreover have *: "\<And>i x. 0 \<le> M2.\<mu> (Pair x -` (f -` {i} \<inter> space P))"
-    using f(1)[THEN simple_functionD(2)] f(2) by (intro M2.positive_measure measurable_cut_fst)
-  moreover { fix i assume "i \<in> f`space P"
-    with * have "\<And>x. 0 \<le> i * M2.\<mu> (Pair x -` (f -` {i} \<inter> space P))"
+  moreover have "\<And>z. ?F z \<in> sets (M1 \<Otimes>\<^isub>M M2)"
+    by (auto intro!: f_borel borel_measurable_vimage)
+  moreover then have "\<And>z. (\<lambda>x. emeasure M2 (?F' x z)) \<in> borel_measurable M1"
+    by (auto intro!: measurable_emeasure_Pair1 simp del: vimage_Int)
+  moreover have *: "\<And>i x. 0 \<le> emeasure M2 (Pair x -` (f -` {i} \<inter> space (M1 \<Otimes>\<^isub>M M2)))"
+    using f(1)[THEN simple_functionD(2)] f(2) by (intro emeasure_nonneg)
+  moreover { fix i assume "i \<in> f`space (M1 \<Otimes>\<^isub>M M2)"
+    with * have "\<And>x. 0 \<le> i * emeasure M2 (Pair x -` (f -` {i} \<inter> space (M1 \<Otimes>\<^isub>M M2)))"
       using f(2) by auto }
   ultimately
   show "(\<lambda>x. \<integral>\<^isup>+y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
-    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P P f" using f(2)
-    by (auto simp del: vimage_Int cong: measurable_cong
-             intro!: M1.borel_measurable_ereal_setsum setsum_cong
-             simp add: M1.positive_integral_setsum simple_integral_def
-                       M1.positive_integral_cmult
-                       M1.positive_integral_cong[OF eq]
+    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)
+    by (auto simp del: vimage_Int cong: measurable_cong intro!: setsum_cong
+             simp add: positive_integral_setsum simple_integral_def
+                       positive_integral_cmult
+                       positive_integral_cong[OF eq]
                        positive_integral_eq_simple_integral[OF f]
-                       pair_measure_alt[symmetric])
+                       emeasure_pair_measure_alt[symmetric])
 qed
 
 lemma (in pair_sigma_finite) positive_integral_fst_measurable:
-  assumes f: "f \<in> borel_measurable P"
+  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   shows "(\<lambda>x. \<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
       (is "?C f \<in> borel_measurable M1")
-    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P P f"
+    and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
 proof -
   from borel_measurable_implies_simple_function_sequence'[OF f] guess F . note F = this
-  then have F_borel: "\<And>i. F i \<in> borel_measurable P"
+  then have F_borel: "\<And>i. F i \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
     by (auto intro: borel_measurable_simple_function)
   note sf = simple_function_cut[OF F(1,5)]
   then have "(\<lambda>x. SUP i. ?C (F i) x) \<in> borel_measurable M1"
     using F(1) by auto
   moreover
   { fix x assume "x \<in> space M1"
-    from F measurable_pair_image_snd[OF F_borel`x \<in> space M1`]
+    from F measurable_Pair2[OF F_borel `x \<in> space M1`]
     have "(\<integral>\<^isup>+y. (SUP i. F i (x, y)) \<partial>M2) = (SUP i. ?C (F i) x)"
-      by (intro M2.positive_integral_monotone_convergence_SUP)
+      by (intro positive_integral_monotone_convergence_SUP)
          (auto simp: incseq_Suc_iff le_fun_def)
     then have "(SUP i. ?C (F i) x) = ?C f x"
       unfolding F(4) positive_integral_max_0 by simp }
   note SUPR_C = this
   ultimately show "?C f \<in> borel_measurable M1"
     by (simp cong: measurable_cong)
-  have "(\<integral>\<^isup>+x. (SUP i. F i x) \<partial>P) = (SUP i. integral\<^isup>P P (F i))"
+  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))"
     using F_borel F
     by (intro positive_integral_monotone_convergence_SUP) auto
-  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)"
+  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)"
     unfolding sf(2) by simp
   also have "\<dots> = \<integral>\<^isup>+ x. (SUP i. \<integral>\<^isup>+ y. F i (x, y) \<partial>M2) \<partial>M1" using F sf(1)
-    by (intro M1.positive_integral_monotone_convergence_SUP[symmetric])
-       (auto intro!: M2.positive_integral_mono M2.positive_integral_positive
-                simp: incseq_Suc_iff le_fun_def)
+    by (intro positive_integral_monotone_convergence_SUP[symmetric])
+       (auto intro!: positive_integral_mono positive_integral_positive
+             simp: incseq_Suc_iff le_fun_def)
   also have "\<dots> = \<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. (SUP i. F i (x, y)) \<partial>M2) \<partial>M1"
     using F_borel F(2,5)
-    by (auto intro!: M1.positive_integral_cong M2.positive_integral_monotone_convergence_SUP[symmetric]
-             simp: incseq_Suc_iff le_fun_def measurable_pair_image_snd)
-  finally show "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P P f"
+    by (auto intro!: positive_integral_cong positive_integral_monotone_convergence_SUP[symmetric] measurable_Pair2
+             simp: incseq_Suc_iff le_fun_def)
+  finally show "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
     using F by (simp add: positive_integral_max_0)
 qed
 
-lemma (in pair_sigma_finite) measure_preserving_swap:
-  "(\<lambda>(x,y). (y, x)) \<in> measure_preserving (M1 \<Otimes>\<^isub>M M2) (M2 \<Otimes>\<^isub>M M1)"
-proof
-  interpret Q: pair_sigma_finite M2 M1 by default
-  show *: "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^isub>M M2) (M2 \<Otimes>\<^isub>M M1)"
-    using pair_sigma_algebra_swap_measurable .
-  fix X assume "X \<in> sets (M2 \<Otimes>\<^isub>M M1)"
-  from measurable_sets[OF * this] this Q.sets_into_space[OF this]
-  show "measure (M1 \<Otimes>\<^isub>M M2) ((\<lambda>(x, y). (y, x)) -` X \<inter> space P) = measure (M2 \<Otimes>\<^isub>M M1) X"
-    by (auto intro!: M1.positive_integral_cong arg_cong[where f="M2.\<mu>"]
-      simp: pair_measure_alt Q.pair_measure_alt2 space_pair_measure)
-qed
-
-lemma (in pair_sigma_finite) positive_integral_product_swap:
-  assumes f: "f \<in> borel_measurable P"
-  shows "(\<integral>\<^isup>+x. f (case x of (x,y)\<Rightarrow>(y,x)) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>P P f"
+lemma (in pair_sigma_finite) positive_integral_snd_measurable:
+  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
+  shows "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
 proof -
   interpret Q: pair_sigma_finite M2 M1 by default
-  have "sigma_algebra P" by default
-  with f show ?thesis
-    by (subst Q.positive_integral_vimage[OF _ Q.measure_preserving_swap]) auto
-qed
-
-lemma (in pair_sigma_finite) positive_integral_snd_measurable:
-  assumes f: "f \<in> borel_measurable P"
-  shows "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>P P f"
-proof -
-  interpret Q: pair_sigma_finite M2 M1 by default
-  note pair_sigma_algebra_measurable[OF f]
+  note measurable_pair_swap[OF f]
   from Q.positive_integral_fst_measurable[OF this]
-  have "(\<integral>\<^isup>+ y. (\<integral>\<^isup>+ x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>\<^isup>+ (x, y). f (y, x) \<partial>Q.P)"
+  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))"
     by simp
-  also have "(\<integral>\<^isup>+ (x, y). f (y, x) \<partial>Q.P) = integral\<^isup>P P f"
-    unfolding positive_integral_product_swap[OF f, symmetric]
-    by (auto intro!: Q.positive_integral_cong)
+  also have "(\<integral>\<^isup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) f"
+    by (subst distr_pair_swap)
+       (auto simp: positive_integral_distr[OF measurable_pair_swap' f] intro!: positive_integral_cong)
   finally show ?thesis .
 qed
 
 lemma (in pair_sigma_finite) Fubini:
-  assumes f: "f \<in> borel_measurable P"
+  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
   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)"
   unfolding positive_integral_snd_measurable[OF assms]
   unfolding positive_integral_fst_measurable[OF assms] ..
 
 lemma (in pair_sigma_finite) AE_pair:
-  assumes "AE x in P. Q x"
+  assumes "AE x in (M1 \<Otimes>\<^isub>M M2). Q x"
   shows "AE x in M1. (AE y in M2. Q (x, y))"
 proof -
-  obtain N where N: "N \<in> sets P" "\<mu> N = 0" "{x\<in>space P. \<not> Q x} \<subseteq> N"
-    using assms unfolding almost_everywhere_def by auto
+  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"
+    using assms unfolding eventually_ae_filter by auto
   show ?thesis
-  proof (rule M1.AE_I)
-    from N measure_cut_measurable_fst[OF `N \<in> sets P`]
-    show "M1.\<mu> {x\<in>space M1. M2.\<mu> (Pair x -` N) \<noteq> 0} = 0"
-      by (auto simp: pair_measure_alt M1.positive_integral_0_iff)
-    show "{x \<in> space M1. M2.\<mu> (Pair x -` N) \<noteq> 0} \<in> sets M1"
-      by (intro M1.borel_measurable_ereal_neq_const measure_cut_measurable_fst N)
-    { fix x assume "x \<in> space M1" "M2.\<mu> (Pair x -` N) = 0"
-      have "M2.almost_everywhere (\<lambda>y. Q (x, y))"
-      proof (rule M2.AE_I)
-        show "M2.\<mu> (Pair x -` N) = 0" by fact
-        show "Pair x -` N \<in> sets M2" by (intro measurable_cut_fst N)
+  proof (rule AE_I)
+    from N measurable_emeasure_Pair1[OF `N \<in> sets (M1 \<Otimes>\<^isub>M M2)`]
+    show "emeasure M1 {x\<in>space M1. emeasure M2 (Pair x -` N) \<noteq> 0} = 0"
+      by (auto simp: emeasure_pair_measure_alt positive_integral_0_iff emeasure_nonneg)
+    show "{x \<in> space M1. emeasure M2 (Pair x -` N) \<noteq> 0} \<in> sets M1"
+      by (intro borel_measurable_ereal_neq_const measurable_emeasure_Pair1 N)
+    { fix x assume "x \<in> space M1" "emeasure M2 (Pair x -` N) = 0"
+      have "AE y in M2. Q (x, y)"
+      proof (rule AE_I)
+        show "emeasure M2 (Pair x -` N) = 0" by fact
+        show "Pair x -` N \<in> sets M2" using N(1) by (rule sets_Pair1)
         show "{y \<in> space M2. \<not> Q (x, y)} \<subseteq> Pair x -` N"
-          using N `x \<in> space M1` unfolding space_sigma space_pair_measure by auto
+          using N `x \<in> space M1` unfolding space_pair_measure by auto
       qed }
-    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}"
+    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}"
       by auto
   qed
 qed
 
-lemma (in pair_sigma_algebra) measurable_product_swap:
-  "f \<in> measurable (M2 \<Otimes>\<^isub>M M1) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable P M"
+lemma (in pair_sigma_finite) AE_pair_measure:
+  assumes "{x\<in>space (M1 \<Otimes>\<^isub>M M2). P x} \<in> sets (M1 \<Otimes>\<^isub>M M2)"
+  assumes ae: "AE x in M1. AE y in M2. P (x, y)"
+  shows "AE x in M1 \<Otimes>\<^isub>M M2. P x"
+proof (subst AE_iff_measurable[OF _ refl])
+  show "{x\<in>space (M1 \<Otimes>\<^isub>M M2). \<not> P x} \<in> sets (M1 \<Otimes>\<^isub>M M2)"
+    by (rule sets_Collect) fact
+  then have "emeasure (M1 \<Otimes>\<^isub>M M2) {x \<in> space (M1 \<Otimes>\<^isub>M M2). \<not> P x} =
+      (\<integral>\<^isup>+ x. \<integral>\<^isup>+ y. indicator {x \<in> space (M1 \<Otimes>\<^isub>M M2). \<not> P x} (x, y) \<partial>M2 \<partial>M1)"
+    by (simp add: emeasure_pair_measure)
+  also have "\<dots> = (\<integral>\<^isup>+ x. \<integral>\<^isup>+ y. 0 \<partial>M2 \<partial>M1)"
+    using ae
+    apply (safe intro!: positive_integral_cong_AE)
+    apply (intro AE_I2)
+    apply (safe intro!: positive_integral_cong_AE)
+    apply auto
+    done
+  finally show "emeasure (M1 \<Otimes>\<^isub>M M2) {x \<in> space (M1 \<Otimes>\<^isub>M M2). \<not> P x} = 0" by simp
+qed
+
+lemma (in pair_sigma_finite) AE_pair_iff:
+  "{x\<in>space (M1 \<Otimes>\<^isub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^isub>M M2) \<Longrightarrow>
+    (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))"
+  using AE_pair[of "\<lambda>x. P (fst x) (snd x)"] AE_pair_measure[of "\<lambda>x. P (fst x) (snd x)"] by auto
+
+lemma AE_distr_iff:
+  assumes f: "f \<in> measurable M N" and P: "{x \<in> space N. P x} \<in> sets N"
+  shows "(AE x in distr M N f. P x) \<longleftrightarrow> (AE x in M. P (f x))"
+proof (subst (1 2) AE_iff_measurable[OF _ refl])
+  from P show "{x \<in> space (distr M N f). \<not> P x} \<in> sets (distr M N f)"
+    by (auto intro!: sets_Collect_neg)
+  moreover
+  have "f -` {x \<in> space N. P x} \<inter> space M = {x \<in> space M. P (f x)}"
+    using f by (auto dest: measurable_space)
+  then show "{x \<in> space M. \<not> P (f x)} \<in> sets M"
+    using measurable_sets[OF f P] by (auto intro!: sets_Collect_neg)
+  moreover have "f -` {x\<in>space N. \<not> P x} \<inter> space M = {x \<in> space M. \<not> P (f x)}"
+    using f by (auto dest: measurable_space)
+  ultimately show "(emeasure (distr M N f) {x \<in> space (distr M N f). \<not> P x} = 0) =
+    (emeasure M {x \<in> space M. \<not> P (f x)} = 0)"
+    using f by (simp add: emeasure_distr)
+qed
+
+lemma (in pair_sigma_finite) AE_commute:
+  assumes P: "{x\<in>space (M1 \<Otimes>\<^isub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^isub>M M2)"
+  shows "(AE x in M1. AE y in M2. P x y) \<longleftrightarrow> (AE y in M2. AE x in M1. P x y)"
 proof -
-  interpret Q: pair_sigma_algebra M2 M1 by default
-  show ?thesis
-    using pair_sigma_algebra_measurable[of "\<lambda>(x,y). f (y, x)"]
-    by (auto intro!: pair_sigma_algebra_measurable Q.pair_sigma_algebra_measurable iffI)
+  interpret Q: pair_sigma_finite M2 M1 ..
+  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"
+    by auto
+  have "{x \<in> space (M2 \<Otimes>\<^isub>M M1). P (snd x) (fst x)} =
+    (\<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)"
+    by (auto simp: space_pair_measure)
+  also have "\<dots> \<in> sets (M2 \<Otimes>\<^isub>M M1)"
+    by (intro sets_pair_swap P)
+  finally show ?thesis
+    apply (subst AE_pair_iff[OF P])
+    apply (subst distr_pair_swap)
+    apply (subst AE_distr_iff[OF measurable_pair_swap' P])
+    apply (subst Q.AE_pair_iff)
+    apply simp_all
+    done
 qed
 
 lemma (in pair_sigma_finite) integrable_product_swap:
-  assumes "integrable P f"
+  assumes "integrable (M1 \<Otimes>\<^isub>M M2) f"
   shows "integrable (M2 \<Otimes>\<^isub>M M1) (\<lambda>(x,y). f (y,x))"
 proof -
   interpret Q: pair_sigma_finite M2 M1 by default
   have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
   show ?thesis unfolding *
-    using assms unfolding integrable_def
-    apply (subst (1 2) positive_integral_product_swap)
-    using `integrable P f` unfolding integrable_def
-    by (auto simp: *[symmetric] Q.measurable_product_swap[symmetric])
+    by (rule integrable_distr[OF measurable_pair_swap'])
+       (simp add: distr_pair_swap[symmetric] assms)
 qed
 
 lemma (in pair_sigma_finite) integrable_product_swap_iff:
-  "integrable (M2 \<Otimes>\<^isub>M M1) (\<lambda>(x,y). f (y,x)) \<longleftrightarrow> integrable P f"
+  "integrable (M2 \<Otimes>\<^isub>M M1) (\<lambda>(x,y). f (y,x)) \<longleftrightarrow> integrable (M1 \<Otimes>\<^isub>M M2) f"
 proof -
   interpret Q: pair_sigma_finite M2 M1 by default
   from Q.integrable_product_swap[of "\<lambda>(x,y). f (y,x)"] integrable_product_swap[of f]
@@ -802,27 +668,25 @@
 qed
 
 lemma (in pair_sigma_finite) integral_product_swap:
-  assumes "integrable P f"
-  shows "(\<integral>(x,y). f (y,x) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>L P f"
+  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
+  shows "(\<integral>(x,y). f (y,x) \<partial>(M2 \<Otimes>\<^isub>M M1)) = integral\<^isup>L (M1 \<Otimes>\<^isub>M M2) f"
 proof -
-  interpret Q: pair_sigma_finite M2 M1 by default
   have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
-  show ?thesis
-    unfolding lebesgue_integral_def *
-    apply (subst (1 2) positive_integral_product_swap)
-    using `integrable P f` unfolding integrable_def
-    by (auto simp: *[symmetric] Q.measurable_product_swap[symmetric])
+  show ?thesis unfolding *
+    by (simp add: integral_distr[symmetric, OF measurable_pair_swap' f] distr_pair_swap[symmetric])
 qed
 
 lemma (in pair_sigma_finite) integrable_fst_measurable:
-  assumes f: "integrable P f"
-  shows "M1.almost_everywhere (\<lambda>x. integrable M2 (\<lambda> y. f (x, y)))" (is "?AE")
-    and "(\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>L P f" (is "?INT")
+  assumes f: "integrable (M1 \<Otimes>\<^isub>M M2) f"
+  shows "AE x in M1. integrable M2 (\<lambda> y. f (x, y))" (is "?AE")
+    and "(\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>L (M1 \<Otimes>\<^isub>M M2) f" (is "?INT")
 proof -
+  have f_borel: "f \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
+    using f by auto
   let ?pf = "\<lambda>x. ereal (f x)" and ?nf = "\<lambda>x. ereal (- f x)"
   have
-    borel: "?nf \<in> borel_measurable P""?pf \<in> borel_measurable P" and
-    int: "integral\<^isup>P P ?nf \<noteq> \<infinity>" "integral\<^isup>P P ?pf \<noteq> \<infinity>"
+    borel: "?nf \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)""?pf \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)" and
+    int: "integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) ?nf \<noteq> \<infinity>" "integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) ?pf \<noteq> \<infinity>"
     using assms by auto
   have "(\<integral>\<^isup>+x. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
      "(\<integral>\<^isup>+x. (\<integral>\<^isup>+y. ereal (- f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
@@ -831,69 +695,92 @@
   with borel[THEN positive_integral_fst_measurable(1)]
   have AE_pos: "AE x in M1. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
     "AE x in M1. (\<integral>\<^isup>+y. ereal (- f (x, y)) \<partial>M2) \<noteq> \<infinity>"
-    by (auto intro!: M1.positive_integral_PInf_AE )
+    by (auto intro!: positive_integral_PInf_AE )
   then have AE: "AE x in M1. \<bar>\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
     "AE x in M1. \<bar>\<integral>\<^isup>+y. ereal (- f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
-    by (auto simp: M2.positive_integral_positive)
+    by (auto simp: positive_integral_positive)
   from AE_pos show ?AE using assms
-    by (simp add: measurable_pair_image_snd integrable_def)
+    by (simp add: measurable_Pair2[OF f_borel] integrable_def)
   { fix f have "(\<integral>\<^isup>+ x. - \<integral>\<^isup>+ y. ereal (f x y) \<partial>M2 \<partial>M1) = (\<integral>\<^isup>+x. 0 \<partial>M1)"
-      using M2.positive_integral_positive
-      by (intro M1.positive_integral_cong_pos) (auto simp: ereal_uminus_le_reorder)
+      using positive_integral_positive
+      by (intro positive_integral_cong_pos) (auto simp: ereal_uminus_le_reorder)
     then have "(\<integral>\<^isup>+ x. - \<integral>\<^isup>+ y. ereal (f x y) \<partial>M2 \<partial>M1) = 0" by simp }
   note this[simp]
-  { fix f assume borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable P"
-      and int: "integral\<^isup>P P (\<lambda>x. ereal (f x)) \<noteq> \<infinity>"
-      and AE: "M1.almost_everywhere (\<lambda>x. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>)"
+  { fix f assume borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
+      and int: "integral\<^isup>P (M1 \<Otimes>\<^isub>M M2) (\<lambda>x. ereal (f x)) \<noteq> \<infinity>"
+      and AE: "AE x in M1. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
     have "integrable M1 (\<lambda>x. real (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2))" (is "integrable M1 ?f")
     proof (intro integrable_def[THEN iffD2] conjI)
       show "?f \<in> borel_measurable M1"
-        using borel by (auto intro!: M1.borel_measurable_real_of_ereal positive_integral_fst_measurable)
+        using borel by (auto intro!: positive_integral_fst_measurable)
       have "(\<integral>\<^isup>+x. ereal (?f x) \<partial>M1) = (\<integral>\<^isup>+x. (\<integral>\<^isup>+y. ereal (f (x, y))  \<partial>M2) \<partial>M1)"
-        using AE M2.positive_integral_positive
-        by (auto intro!: M1.positive_integral_cong_AE simp: ereal_real)
+        using AE positive_integral_positive[of M2]
+        by (auto intro!: positive_integral_cong_AE simp: ereal_real)
       then show "(\<integral>\<^isup>+x. ereal (?f x) \<partial>M1) \<noteq> \<infinity>"
         using positive_integral_fst_measurable[OF borel] int by simp
       have "(\<integral>\<^isup>+x. ereal (- ?f x) \<partial>M1) = (\<integral>\<^isup>+x. 0 \<partial>M1)"
-        by (intro M1.positive_integral_cong_pos)
-           (simp add: M2.positive_integral_positive real_of_ereal_pos)
+        by (intro positive_integral_cong_pos)
+           (simp add: positive_integral_positive real_of_ereal_pos)
       then show "(\<integral>\<^isup>+x. ereal (- ?f x) \<partial>M1) \<noteq> \<infinity>" by simp
     qed }
   with this[OF borel(1) int(1) AE_pos(2)] this[OF borel(2) int(2) AE_pos(1)]
   show ?INT
-    unfolding lebesgue_integral_def[of P] lebesgue_integral_def[of M2]
+    unfolding lebesgue_integral_def[of "M1 \<Otimes>\<^isub>M M2"] lebesgue_integral_def[of M2]
       borel[THEN positive_integral_fst_measurable(2), symmetric]
-    using AE[THEN M1.integral_real]
+    using AE[THEN integral_real]
     by simp
 qed
 
 lemma (in pair_sigma_finite) integrable_snd_measurable:
-  assumes f: "integrable P f"
-  shows "M2.almost_everywhere (\<lambda>y. integrable M1 (\<lambda>x. f (x, y)))" (is "?AE")
-    and "(\<integral>y. (\<integral>x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>L P f" (is "?INT")
+  assumes f: "integrable (M1 \<Otimes>\<^isub>M M2) f"
+  shows "AE y in M2. integrable M1 (\<lambda>x. f (x, y))" (is "?AE")
+    and "(\<integral>y. (\<integral>x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^isup>L (M1 \<Otimes>\<^isub>M M2) f" (is "?INT")
 proof -
   interpret Q: pair_sigma_finite M2 M1 by default
-  have Q_int: "integrable Q.P (\<lambda>(x, y). f (y, x))"
+  have Q_int: "integrable (M2 \<Otimes>\<^isub>M M1) (\<lambda>(x, y). f (y, x))"
     using f unfolding integrable_product_swap_iff .
   show ?INT
     using Q.integrable_fst_measurable(2)[OF Q_int]
-    using integral_product_swap[OF f] by simp
+    using integral_product_swap[of f] f by auto
   show ?AE
     using Q.integrable_fst_measurable(1)[OF Q_int]
     by simp
 qed
 
+lemma (in pair_sigma_finite) positive_integral_fst_measurable':
+  assumes f: "(\<lambda>x. f (fst x) (snd x)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
+  shows "(\<lambda>x. \<integral>\<^isup>+ y. f x y \<partial>M2) \<in> borel_measurable M1"
+  using positive_integral_fst_measurable(1)[OF f] by simp
+
+lemma (in pair_sigma_finite) integral_fst_measurable:
+  "(\<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"
+  by (auto simp: lebesgue_integral_def intro!: borel_measurable_diff positive_integral_fst_measurable')
+
+lemma (in pair_sigma_finite) positive_integral_snd_measurable':
+  assumes f: "(\<lambda>x. f (fst x) (snd x)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
+  shows "(\<lambda>y. \<integral>\<^isup>+ x. f x y \<partial>M1) \<in> borel_measurable M2"
+proof -
+  interpret Q: pair_sigma_finite M2 M1 ..
+  show ?thesis
+    using measurable_pair_swap[OF f]
+    by (intro Q.positive_integral_fst_measurable') (simp add: split_beta')
+qed
+
+lemma (in pair_sigma_finite) integral_snd_measurable:
+  "(\<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"
+  by (auto simp: lebesgue_integral_def intro!: borel_measurable_diff positive_integral_snd_measurable')
+
 lemma (in pair_sigma_finite) Fubini_integral:
-  assumes f: "integrable P f"
+  assumes f: "integrable (M1 \<Otimes>\<^isub>M M2) f"
   shows "(\<integral>y. (\<integral>x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1)"
   unfolding integrable_snd_measurable[OF assms]
   unfolding integrable_fst_measurable[OF assms] ..
 
-section "Products on finite spaces"
+section {* Products on counting spaces, densities and distributions *}
 
 lemma sigma_sets_pair_measure_generator_finite:
   assumes "finite A" and "finite B"
-  shows "sigma_sets (A \<times> B) { a \<times> b | a b. a \<in> Pow A \<and> b \<in> Pow B} = Pow (A \<times> B)"
+  shows "sigma_sets (A \<times> B) { a \<times> b | a b. a \<subseteq> A \<and> b \<subseteq> B} = Pow (A \<times> B)"
   (is "sigma_sets ?prod ?sets = _")
 proof safe
   have fin: "finite (A \<times> B)" using assms by (rule finite_cartesian_product)
@@ -904,8 +791,7 @@
     case empty show ?case by (rule sigma_sets.Empty)
   next
     case (insert a x)
-    hence "{a} \<in> sigma_sets ?prod ?sets"
-      by (auto simp: pair_measure_generator_def intro!: sigma_sets.Basic)
+    hence "{a} \<in> sigma_sets ?prod ?sets" by auto
     moreover have "x \<in> sigma_sets ?prod ?sets" using insert by auto
     ultimately show ?case unfolding insert_is_Un[of a x] by (rule sigma_sets_Un)
   qed
@@ -916,48 +802,142 @@
   show "a \<in> A" and "b \<in> B" by auto
 qed
 
-locale pair_finite_sigma_algebra = pair_sigma_algebra M1 M2 + M1: finite_sigma_algebra M1 + M2: finite_sigma_algebra M2 for M1 M2
-
-lemma (in pair_finite_sigma_algebra) finite_pair_sigma_algebra:
-  shows "P = \<lparr> space = space M1 \<times> space M2, sets = Pow (space M1 \<times> space M2), \<dots> = algebra.more P \<rparr>"
-proof -
-  show ?thesis
-    using sigma_sets_pair_measure_generator_finite[OF M1.finite_space M2.finite_space]
-    by (intro algebra.equality) (simp_all add: pair_measure_def pair_measure_generator_def sigma_def)
-qed
-
-sublocale pair_finite_sigma_algebra \<subseteq> finite_sigma_algebra P
-proof
-  show "finite (space P)"
-    using M1.finite_space M2.finite_space
-    by (subst finite_pair_sigma_algebra) simp
-  show "sets P = Pow (space P)"
-    by (subst (1 2) finite_pair_sigma_algebra) simp
+lemma pair_measure_count_space:
+  assumes A: "finite A" and B: "finite B"
+  shows "count_space A \<Otimes>\<^isub>M count_space B = count_space (A \<times> B)" (is "?P = ?C")
+proof (rule measure_eqI)
+  interpret A: finite_measure "count_space A" by (rule finite_measure_count_space) fact
+  interpret B: finite_measure "count_space B" by (rule finite_measure_count_space) fact
+  interpret P: pair_sigma_finite "count_space A" "count_space B" by default
+  show eq: "sets ?P = sets ?C"
+    by (simp add: sets_pair_measure sigma_sets_pair_measure_generator_finite A B)
+  fix X assume X: "X \<in> sets ?P"
+  with eq have X_subset: "X \<subseteq> A \<times> B" by simp
+  with A B have fin_Pair: "\<And>x. finite (Pair x -` X)"
+    by (intro finite_subset[OF _ B]) auto
+  have fin_X: "finite X" using X_subset by (rule finite_subset) (auto simp: A B)
+  show "emeasure ?P X = emeasure ?C X"
+    apply (subst P.emeasure_pair_measure_alt[OF X])
+    apply (subst emeasure_count_space)
+    using X_subset apply auto []
+    apply (simp add: fin_Pair emeasure_count_space X_subset fin_X)
+    apply (subst positive_integral_count_space)
+    using A apply simp
+    apply (simp del: real_of_nat_setsum add: real_of_nat_setsum[symmetric])
+    apply (subst card_gt_0_iff)
+    apply (simp add: fin_Pair)
+    apply (subst card_SigmaI[symmetric])
+    using A apply simp
+    using fin_Pair apply simp
+    using X_subset apply (auto intro!: arg_cong[where f=card])
+    done
 qed
 
-locale pair_finite_space = pair_sigma_finite M1 M2 + pair_finite_sigma_algebra M1 M2 +
-  M1: finite_measure_space M1 + M2: finite_measure_space M2 for M1 M2
+lemma pair_measure_density:
+  assumes f: "f \<in> borel_measurable M1" "AE x in M1. 0 \<le> f x"
+  assumes g: "g \<in> borel_measurable M2" "AE x in M2. 0 \<le> g x"
+  assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
+  assumes "sigma_finite_measure (density M1 f)" "sigma_finite_measure (density M2 g)"
+  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")
+proof (rule measure_eqI)
+  interpret M1: sigma_finite_measure M1 by fact
+  interpret M2: sigma_finite_measure M2 by fact
+  interpret D1: sigma_finite_measure "density M1 f" by fact
+  interpret D2: sigma_finite_measure "density M2 g" by fact
+  interpret L: pair_sigma_finite "density M1 f" "density M2 g" ..
+  interpret R: pair_sigma_finite M1 M2 ..
+
+  fix A assume A: "A \<in> sets ?L"
+  then have indicator_eq: "\<And>x y. indicator A (x, y) = indicator (Pair x -` A) y"
+   and Pair_A: "\<And>x. Pair x -` A \<in> sets M2"
+    by (auto simp: indicator_def sets_Pair1)
+  have f_fst: "(\<lambda>p. f (fst p)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
+    using measurable_comp[OF measurable_fst f(1)] by (simp add: comp_def)
+  have g_snd: "(\<lambda>p. g (snd p)) \<in> borel_measurable (M1 \<Otimes>\<^isub>M M2)"
+    using measurable_comp[OF measurable_snd g(1)] by (simp add: comp_def)
+  have "(\<lambda>x. \<integral>\<^isup>+ y. g (snd (x, y)) * indicator A (x, y) \<partial>M2) \<in> borel_measurable M1"
+    using g_snd Pair_A A by (intro R.positive_integral_fst_measurable) auto
+  then have int_g: "(\<lambda>x. \<integral>\<^isup>+ y. g y * indicator A (x, y) \<partial>M2) \<in> borel_measurable M1"
+    by simp
 
-lemma (in pair_finite_space) pair_measure_Pair[simp]:
-  assumes "a \<in> space M1" "b \<in> space M2"
-  shows "\<mu> {(a, b)} = M1.\<mu> {a} * M2.\<mu> {b}"
+  show "emeasure ?L A = emeasure ?R A"
+    apply (subst L.emeasure_pair_measure[OF A])
+    apply (subst emeasure_density)
+        using f_fst g_snd apply (simp add: split_beta')
+      using A apply simp
+    apply (subst positive_integral_density[OF g])
+      apply (simp add: indicator_eq Pair_A)
+    apply (subst positive_integral_density[OF f])
+      apply (rule int_g)
+    apply (subst R.positive_integral_fst_measurable(2)[symmetric])
+      using f g A Pair_A f_fst g_snd
+      apply (auto intro!: positive_integral_cong_AE R.measurable_emeasure_Pair1
+                  simp: positive_integral_cmult indicator_eq split_beta')
+    apply (intro AE_I2 impI)
+    apply (subst mult_assoc)
+    apply (subst positive_integral_cmult)
+          apply auto
+    done
+qed simp
+
+lemma sigma_finite_measure_distr:
+  assumes "sigma_finite_measure (distr M N f)" and f: "f \<in> measurable M N"
+  shows "sigma_finite_measure M"
 proof -
-  have "\<mu> ({a}\<times>{b}) = M1.\<mu> {a} * M2.\<mu> {b}"
-    using M1.sets_eq_Pow M2.sets_eq_Pow assms
-    by (subst pair_measure_times) auto
-  then show ?thesis by simp
+  interpret sigma_finite_measure "distr M N f" by fact
+  from sigma_finite_disjoint guess A . note A = this
+  show ?thesis
+  proof (unfold_locales, intro conjI exI allI)
+    show "range (\<lambda>i. f -` A i \<inter> space M) \<subseteq> sets M"
+      using A f by (auto intro!: measurable_sets)
+    show "(\<Union>i. f -` A i \<inter> space M) = space M"
+      using A(1) A(2)[symmetric] f by (auto simp: measurable_def Pi_def)
+    fix i show "emeasure M (f -` A i \<inter> space M) \<noteq> \<infinity>"
+      using f A(1,2) A(3)[of i] by (simp add: emeasure_distr subset_eq)
+  qed
 qed
 
-lemma (in pair_finite_space) pair_measure_singleton[simp]:
-  assumes "x \<in> space M1 \<times> space M2"
-  shows "\<mu> {x} = M1.\<mu> {fst x} * M2.\<mu> {snd x}"
-  using pair_measure_Pair assms by (cases x) auto
+lemma measurable_cong':
+  assumes sets: "sets M = sets M'" "sets N = sets N'"
+  shows "measurable M N = measurable M' N'"
+  using sets[THEN sets_eq_imp_space_eq] sets by (simp add: measurable_def)
 
-sublocale pair_finite_space \<subseteq> finite_measure_space P
-proof unfold_locales
-  show "measure P (space P) \<noteq> \<infinity>"
-    by (subst (2) finite_pair_sigma_algebra)
-       (simp add: pair_measure_times)
+lemma pair_measure_distr:
+  assumes f: "f \<in> measurable M S" and g: "g \<in> measurable N T"
+  assumes "sigma_finite_measure (distr M S f)" "sigma_finite_measure (distr N T g)"
+  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")
+proof (rule measure_eqI)
+  show "sets ?P = sets ?D"
+    by simp
+  interpret S: sigma_finite_measure "distr M S f" by fact
+  interpret T: sigma_finite_measure "distr N T g" by fact
+  interpret ST: pair_sigma_finite "distr M S f"  "distr N T g" ..
+  interpret M: sigma_finite_measure M by (rule sigma_finite_measure_distr) fact+
+  interpret N: sigma_finite_measure N by (rule sigma_finite_measure_distr) fact+
+  interpret MN: pair_sigma_finite M N ..
+  interpret SN: pair_sigma_finite "distr M S f" N ..
+  have [simp]: 
+    "\<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"
+    by auto
+  then have fg: "(\<lambda>(x, y). (f x, g y)) \<in> measurable (M \<Otimes>\<^isub>M N) (S \<Otimes>\<^isub>M T)"
+    using measurable_comp[OF measurable_fst f] measurable_comp[OF measurable_snd g]
+    by (auto simp: measurable_pair_iff)
+  fix A assume A: "A \<in> sets ?P"
+  then have "emeasure ?P A = (\<integral>\<^isup>+x. emeasure (distr N T g) (Pair x -` A) \<partial>distr M S f)"
+    by (rule ST.emeasure_pair_measure_alt)
+  also have "\<dots> = (\<integral>\<^isup>+x. emeasure N (g -` (Pair x -` A) \<inter> space N) \<partial>distr M S f)"
+    using g A by (simp add: sets_Pair1 emeasure_distr)
+  also have "\<dots> = (\<integral>\<^isup>+x. emeasure N (g -` (Pair (f x) -` A) \<inter> space N) \<partial>M)"
+    using f g A ST.measurable_emeasure_Pair1[OF A]
+    by (intro positive_integral_distr) (auto simp add: sets_Pair1 emeasure_distr)
+  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)"
+    by (intro positive_integral_cong arg_cong2[where f=emeasure]) (auto simp: space_pair_measure)
+  also have "\<dots> = emeasure (M \<Otimes>\<^isub>M N) ((\<lambda>(x, y). (f x, g y)) -` A \<inter> space (M \<Otimes>\<^isub>M N))"
+    using fg by (intro MN.emeasure_pair_measure_alt[symmetric] measurable_sets[OF _ A])
+                (auto cong: measurable_cong')
+  also have "\<dots> = emeasure ?D A"
+    using fg A by (subst emeasure_distr) auto
+  finally show "emeasure ?P A = emeasure ?D A" .
 qed
 
 end
\ No newline at end of file
--- a/src/HOL/Probability/Borel_Space.thy	Mon Apr 23 12:23:23 2012 +0100
+++ b/src/HOL/Probability/Borel_Space.thy	Mon Apr 23 12:14:35 2012 +0200
@@ -11,16 +11,14 @@
 
 section "Generic Borel spaces"
 
-definition "borel = sigma \<lparr> space = UNIV::'a::topological_space set, sets = {S. open S}\<rparr>"
-abbreviation "borel_measurable M \<equiv> measurable M borel"
+definition borel :: "'a::topological_space measure" where
+  "borel = sigma UNIV {S. open S}"
 
-interpretation borel: sigma_algebra borel
-  by (auto simp: borel_def intro!: sigma_algebra_sigma)
+abbreviation "borel_measurable M \<equiv> measurable M borel"
 
 lemma in_borel_measurable:
    "f \<in> borel_measurable M \<longleftrightarrow>
-    (\<forall>S \<in> sets (sigma \<lparr> space = UNIV, sets = {S. open S}\<rparr>).
-      f -` S \<inter> space M \<in> sets M)"
+    (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
   by (auto simp add: measurable_def borel_def)
 
 lemma in_borel_measurable_borel:
@@ -36,7 +34,7 @@
   assumes "open A" shows "A \<in> sets borel"
 proof -
   have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
-  thus ?thesis unfolding borel_def sigma_def by (auto intro!: sigma_sets.Basic)
+  thus ?thesis unfolding borel_def by auto
 qed
 
 lemma borel_closed[simp]:
@@ -48,9 +46,9 @@
 qed
 
 lemma borel_comp[intro,simp]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
-  unfolding Compl_eq_Diff_UNIV by (intro borel.Diff) auto
+  unfolding Compl_eq_Diff_UNIV by (intro Diff) auto
 
-lemma (in sigma_algebra) borel_measurable_vimage:
+lemma borel_measurable_vimage:
   fixes f :: "'a \<Rightarrow> 'x::t2_space"
   assumes borel: "f \<in> borel_measurable M"
   shows "f -` {x} \<inter> space M \<in> sets M"
@@ -65,12 +63,12 @@
   thus ?thesis by auto
 qed
 
-lemma (in sigma_algebra) borel_measurableI:
+lemma borel_measurableI:
   fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
   assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
   shows "f \<in> borel_measurable M"
   unfolding borel_def
-proof (rule measurable_sigma, simp_all)
+proof (rule measurable_measure_of, simp_all)
   fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
     using assms[of S] by simp
 qed
@@ -78,22 +76,22 @@
 lemma borel_singleton[simp, intro]:
   fixes x :: "'a::t1_space"
   shows "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets borel"
-  proof (rule borel.insert_in_sets)
+  proof (rule insert_in_sets)
     show "{x} \<in> sets borel"
       using closed_singleton[of x] by (rule borel_closed)
   qed simp
 
-lemma (in sigma_algebra) borel_measurable_const[simp, intro]:
+lemma borel_measurable_const[simp, intro]:
   "(\<lambda>x. c) \<in> borel_measurable M"
-  by (auto intro!: measurable_const)
+  by auto
 
-lemma (in sigma_algebra) borel_measurable_indicator[simp, intro!]:
+lemma borel_measurable_indicator[simp, intro!]:
   assumes A: "A \<in> sets M"
   shows "indicator A \<in> borel_measurable M"
   unfolding indicator_def [abs_def] using A
-  by (auto intro!: measurable_If_set borel_measurable_const)
+  by (auto intro!: measurable_If_set)
 
-lemma (in sigma_algebra) borel_measurable_indicator_iff:
+lemma borel_measurable_indicator_iff:
   "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
     (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
 proof
@@ -111,49 +109,7 @@
   ultimately show "?I \<in> borel_measurable M" by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_restricted:
-  fixes f :: "'a \<Rightarrow> ereal" assumes "A \<in> sets M"
-  shows "f \<in> borel_measurable (restricted_space A) \<longleftrightarrow>
-    (\<lambda>x. f x * indicator A x) \<in> borel_measurable M"
-    (is "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable M")
-proof -
-  interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
-  have *: "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable ?R"
-    by (auto intro!: measurable_cong)
-  show ?thesis unfolding *
-    unfolding in_borel_measurable_borel
-  proof (simp, safe)
-    fix S :: "ereal set" assume "S \<in> sets borel"
-      "\<forall>S\<in>sets borel. ?f -` S \<inter> A \<in> op \<inter> A ` sets M"
-    then have "?f -` S \<inter> A \<in> op \<inter> A ` sets M" by auto
-    then have f: "?f -` S \<inter> A \<in> sets M"
-      using `A \<in> sets M` sets_into_space by fastforce
-    show "?f -` S \<inter> space M \<in> sets M"
-    proof cases
-      assume "0 \<in> S"
-      then have "?f -` S \<inter> space M = ?f -` S \<inter> A \<union> (space M - A)"
-        using `A \<in> sets M` sets_into_space by auto
-      then show ?thesis using f `A \<in> sets M` by (auto intro!: Un Diff)
-    next
-      assume "0 \<notin> S"
-      then have "?f -` S \<inter> space M = ?f -` S \<inter> A"
-        using `A \<in> sets M` sets_into_space
-        by (auto simp: indicator_def split: split_if_asm)
-      then show ?thesis using f by auto
-    qed
-  next
-    fix S :: "ereal set" assume "S \<in> sets borel"
-      "\<forall>S\<in>sets borel. ?f -` S \<inter> space M \<in> sets M"
-    then have f: "?f -` S \<inter> space M \<in> sets M" by auto
-    then show "?f -` S \<inter> A \<in> op \<inter> A ` sets M"
-      using `A \<in> sets M` sets_into_space
-      apply (simp add: image_iff)
-      apply (rule bexI[OF _ f])
-      by auto
-  qed
-qed
-
-lemma (in sigma_algebra) borel_measurable_subalgebra:
+lemma borel_measurable_subalgebra:
   assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   shows "f \<in> borel_measurable M"
   using assms unfolding measurable_def by auto
@@ -220,7 +176,7 @@
   shows "{x::'a::euclidean_space. x $$ i \<le> a} \<in> sets borel"
   by (auto intro!: borel_closed closed_halfspace_component_le)
 
-lemma (in sigma_algebra) borel_measurable_less[simp, intro]:
+lemma borel_measurable_less[simp, intro]:
   fixes f :: "'a \<Rightarrow> real"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -233,7 +189,7 @@
     by simp (blast intro: measurable_sets)
 qed
 
-lemma (in sigma_algebra) borel_measurable_le[simp, intro]:
+lemma borel_measurable_le[simp, intro]:
   fixes f :: "'a \<Rightarrow> real"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -245,7 +201,7 @@
     by simp blast
 qed
 
-lemma (in sigma_algebra) borel_measurable_eq[simp, intro]:
+lemma borel_measurable_eq[simp, intro]:
   fixes f :: "'a \<Rightarrow> real"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -257,7 +213,7 @@
   thus ?thesis using f g by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_neq[simp, intro]:
+lemma borel_measurable_neq[simp, intro]:
   fixes f :: "'a \<Rightarrow> real"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -351,23 +307,70 @@
   thus "x \<in> UNION ?idx ?box" using ab e p q exI[of _ p] exI[of _ q] by auto
 qed auto
 
-lemma halfspace_span_open:
-  "sigma_sets UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))
-    \<subseteq> sets borel"
-  by (auto intro!: borel.sigma_sets_subset[simplified] borel_open
-                   open_halfspace_component_lt)
+lemma borel_sigma_sets_subset:
+  "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
+  using sigma_sets_subset[of A borel] by simp
+
+lemma borel_eq_sigmaI1:
+  fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
+  assumes borel_eq: "borel = sigma UNIV X"
+  assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (range F))"
+  assumes F: "\<And>i. F i \<in> sets borel"
+  shows "borel = sigma UNIV (range F)"
+  unfolding borel_def
+proof (intro sigma_eqI antisym)
+  have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
+    unfolding borel_def by simp
+  also have "\<dots> = sigma_sets UNIV X"
+    unfolding borel_eq by simp
+  also have "\<dots> \<subseteq> sigma_sets UNIV (range F)"
+    using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
+  finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (range F)" .
+  show "sigma_sets UNIV (range F) \<subseteq> sigma_sets UNIV {S. open S}"
+    unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
+qed auto
 
-lemma halfspace_lt_in_halfspace:
-  "{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>)"
-  by (auto intro!: sigma_sets.Basic simp: sets_sigma)
+lemma borel_eq_sigmaI2:
+  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
+    and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
+  assumes borel_eq: "borel = sigma UNIV (range (\<lambda>(i, j). G i j))"
+  assumes X: "\<And>i j. G i j \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
+  assumes F: "\<And>i j. F i j \<in> sets borel"
+  shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
+  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
+
+lemma borel_eq_sigmaI3:
+  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
+  assumes borel_eq: "borel = sigma UNIV X"
+  assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
+  assumes F: "\<And>i j. F i j \<in> sets borel"
+  shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
+  using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
+
+lemma borel_eq_sigmaI4:
+  fixes F :: "'i \<Rightarrow> 'a::topological_space set"
+    and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
+  assumes borel_eq: "borel = sigma UNIV (range (\<lambda>(i, j). G i j))"
+  assumes X: "\<And>i j. G i j \<in> sets (sigma UNIV (range F))"
+  assumes F: "\<And>i. F i \<in> sets borel"
+  shows "borel = sigma UNIV (range F)"
+  using assms by (intro borel_eq_sigmaI1[where X="range (\<lambda>(i, j). G i j)" and F=F]) auto
+
+lemma borel_eq_sigmaI5:
+  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
+  assumes borel_eq: "borel = sigma UNIV (range G)"
+  assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
+  assumes F: "\<And>i j. F i j \<in> sets borel"
+  shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
+  using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
 
 lemma halfspace_gt_in_halfspace:
-  "{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>)"
-  (is "?set \<in> sets ?SIGMA")
+  "{x\<Colon>'a. a < x $$ i} \<in> sigma_sets UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))"
+  (is "?set \<in> ?SIGMA")
 proof -
-  interpret sigma_algebra "?SIGMA"
-    by (intro sigma_algebra_sigma_sets) (simp_all add: sets_sigma)
-  have *: "?set = (\<Union>n. space ?SIGMA - {x\<Colon>'a. x $$ i < a + 1 / real (Suc n)})"
+  interpret sigma_algebra UNIV ?SIGMA
+    by (intro sigma_algebra_sigma_sets) simp_all
+  have *: "?set = (\<Union>n. UNIV - {x\<Colon>'a. x $$ i < a + 1 / real (Suc n)})"
   proof (safe, simp_all add: not_less)
     fix x assume "a < x $$ i"
     with reals_Archimedean[of "x $$ i - a"]
@@ -381,100 +384,78 @@
     also assume "\<dots> \<le> x"
     finally show "a < x" .
   qed
-  show "?set \<in> sets ?SIGMA" unfolding *
-    by (safe intro!: countable_UN Diff halfspace_lt_in_halfspace)
-qed
-
-lemma open_span_halfspace:
-  "sets borel \<subseteq> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x $$ i < a})\<rparr>)"
-    (is "_ \<subseteq> sets ?SIGMA")
-proof -
-  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) simp
-  then interpret sigma_algebra ?SIGMA .
-  { fix S :: "'a set" assume "S \<in> {S. open S}"
-    then have "open S" unfolding mem_Collect_eq .
-    from open_UNION[OF this]
-    obtain I where *: "S =
-      (\<Union>(a, b)\<in>I.
-          (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
-          (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
-      unfolding greaterThanLessThan_def
-      unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
-      unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
-      by blast
-    have "S \<in> sets ?SIGMA"
-      unfolding *
-      by (auto intro!: countable_UN Int countable_INT halfspace_lt_in_halfspace halfspace_gt_in_halfspace) }
-  then show ?thesis unfolding borel_def
-    by (intro sets_sigma_subset) auto
+  show "?set \<in> ?SIGMA" unfolding *
+    by (auto intro!: Diff)
 qed
 
-lemma halfspace_span_halfspace_le:
-  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
-   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. x $$ i \<le> a})\<rparr>)"
-  (is "_ \<subseteq> sets ?SIGMA")
-proof -
-  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
-  then interpret sigma_algebra ?SIGMA .
-  { fix a i
-    have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
-    proof (safe, simp_all)
-      fix x::'a assume *: "x$$i < a"
-      with reals_Archimedean[of "a - x$$i"]
-      obtain n where "x $$ i < a - 1 / (real (Suc n))"
-        by (auto simp: field_simps inverse_eq_divide)
-      then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
-        by (blast intro: less_imp_le)
-    next
-      fix x::'a and n
-      assume "x$$i \<le> a - 1 / real (Suc n)"
-      also have "\<dots> < a" by auto
-      finally show "x$$i < a" .
-    qed
-    have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
-      by (safe intro!: countable_UN)
-         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
-  then show ?thesis by (intro sets_sigma_subset) auto
-qed
+lemma borel_eq_halfspace_less:
+  "borel = sigma UNIV (range (\<lambda>(a, i). {x::'a::ordered_euclidean_space. x $$ i < a}))"
+  (is "_ = ?SIGMA")
+proof (rule borel_eq_sigmaI3[OF borel_def])
+  fix S :: "'a set" assume "S \<in> {S. open S}"
+  then have "open S" by simp
+  from open_UNION[OF this]
+  obtain I where *: "S =
+    (\<Union>(a, b)\<in>I.
+        (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
+        (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
+    unfolding greaterThanLessThan_def
+    unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
+    unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
+    by blast
+  show "S \<in> ?SIGMA"
+    unfolding *
+    by (safe intro!: countable_UN Int countable_INT) (auto intro!: halfspace_gt_in_halfspace)
+qed auto
 
-lemma halfspace_span_halfspace_ge:
-  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
-   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a \<le> x $$ i})\<rparr>)"
-  (is "_ \<subseteq> sets ?SIGMA")
-proof -
-  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
-  then interpret sigma_algebra ?SIGMA .
-  { fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
-    have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
-      by (safe intro!: Diff)
-         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
-  then show ?thesis by (intro sets_sigma_subset) auto
-qed
+lemma borel_eq_halfspace_le:
+  "borel = sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x $$ i \<le> a}))"
+  (is "_ = ?SIGMA")
+proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
+  fix a i
+  have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
+  proof (safe, simp_all)
+    fix x::'a assume *: "x$$i < a"
+    with reals_Archimedean[of "a - x$$i"]
+    obtain n where "x $$ i < a - 1 / (real (Suc n))"
+      by (auto simp: field_simps inverse_eq_divide)
+    then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
+      by (blast intro: less_imp_le)
+  next
+    fix x::'a and n
+    assume "x$$i \<le> a - 1 / real (Suc n)"
+    also have "\<dots> < a" by auto
+    finally show "x$$i < a" .
+  qed
+  show "{x. x$$i < a} \<in> ?SIGMA" unfolding *
+    by (safe intro!: countable_UN) auto
+qed auto
 
-lemma halfspace_le_span_halfspace_gt:
-  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
-   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a < x $$ i})\<rparr>)"
-  (is "_ \<subseteq> sets ?SIGMA")
-proof -
-  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
-  then interpret sigma_algebra ?SIGMA .
-  { fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
-    have "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
-      by (safe intro!: Diff)
-         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
-  then show ?thesis by (intro sets_sigma_subset) auto
-qed
+lemma borel_eq_halfspace_ge:
+  "borel = sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a \<le> x $$ i}))"
+  (is "_ = ?SIGMA")
+proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
+  fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
+  show "{x. x$$i < a} \<in> ?SIGMA" unfolding *
+      by (safe intro!: compl_sets) auto
+qed auto
 
-lemma halfspace_le_span_atMost:
-  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
-   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>)"
-  (is "_ \<subseteq> sets ?SIGMA")
-proof -
-  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
-  then interpret sigma_algebra ?SIGMA .
-  have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
+lemma borel_eq_halfspace_greater:
+  "borel = sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a < x $$ i}))"
+  (is "_ = ?SIGMA")
+proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
+  fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
+  show "{x. x$$i \<le> a} \<in> ?SIGMA" unfolding *
+    by (safe intro!: compl_sets) auto
+qed auto
+
+lemma borel_eq_atMost:
+  "borel = sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space}))"
+  (is "_ = ?SIGMA")
+proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
+  fix a i show "{x. x$$i \<le> a} \<in> ?SIGMA"
   proof cases
-    fix a i assume "i < DIM('a)"
+    assume "i < DIM('a)"
     then have *: "{x::'a. x$$i \<le> a} = (\<Union>k::nat. {.. (\<chi>\<chi> n. if n = i then a else real k)})"
     proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
       fix x
@@ -484,28 +465,19 @@
       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia \<le> real k"
         by (auto intro!: exI[of _ k])
     qed
-    show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
-      by (safe intro!: countable_UN)
-         (auto simp: sets_sigma intro!: sigma_sets.Basic)
-  next
-    fix a i assume "\<not> i < DIM('a)"
-    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
-      using top by auto
-  qed
-  then show ?thesis by (intro sets_sigma_subset) auto
-qed
+    show "{x. x$$i \<le> a} \<in> ?SIGMA" unfolding *
+      by (safe intro!: countable_UN) auto
+  qed (auto intro: sigma_sets_top sigma_sets.Empty)
+qed auto
 
-lemma halfspace_le_span_greaterThan:
-  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
-   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {a<..})\<rparr>)"
-  (is "_ \<subseteq> sets ?SIGMA")
-proof -
-  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
-  then interpret sigma_algebra ?SIGMA .
-  have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
+lemma borel_eq_greaterThan:
+  "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {a<..}))"
+  (is "_ = ?SIGMA")
+proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
+  fix a i show "{x. x$$i \<le> a} \<in> ?SIGMA"
   proof cases
-    fix a i assume "i < DIM('a)"
-    have "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
+    assume "i < DIM('a)"
+    have "{x::'a. x$$i \<le> a} = UNIV - {x::'a. a < x$$i}" by auto
     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)`
     proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
       fix x
@@ -518,30 +490,22 @@
       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> -real k < x $$ ia"
         by (auto intro!: exI[of _ k])
     qed
-    finally show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
+    finally show "{x. x$$i \<le> a} \<in> ?SIGMA"
       apply (simp only:)
       apply (safe intro!: countable_UN Diff)
-      apply (auto simp: sets_sigma intro!: sigma_sets.Basic)
+      apply (auto intro: sigma_sets_top)
       done
-  next
-    fix a i assume "\<not> i < DIM('a)"
-    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
-      using top by auto
-  qed
-  then show ?thesis by (intro sets_sigma_subset) auto
-qed
+  qed (auto intro: sigma_sets_top sigma_sets.Empty)
+qed auto
 
-lemma halfspace_le_span_lessThan:
-  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a \<le> x $$ i})\<rparr>) \<subseteq>
-   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..<a})\<rparr>)"
-  (is "_ \<subseteq> sets ?SIGMA")
-proof -
-  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
-  then interpret sigma_algebra ?SIGMA .
-  have "\<And>a i. {x. a \<le> x$$i} \<in> sets ?SIGMA"
+lemma borel_eq_lessThan:
+  "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {..<a}))"
+  (is "_ = ?SIGMA")
+proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
+  fix a i show "{x. a \<le> x$$i} \<in> ?SIGMA"
   proof cases
     fix a i assume "i < DIM('a)"
-    have "{x::'a. a \<le> x$$i} = space ?SIGMA - {x::'a. x$$i < a}" by auto
+    have "{x::'a. a \<le> x$$i} = UNIV - {x::'a. x$$i < a}" by auto
     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)`
     proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
       fix x
@@ -554,184 +518,70 @@
       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia < real k"
         by (auto intro!: exI[of _ k])
     qed
-    finally show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
+    finally show "{x. a \<le> x$$i} \<in> ?SIGMA"
       apply (simp only:)
       apply (safe intro!: countable_UN Diff)
-      apply (auto simp: sets_sigma intro!: sigma_sets.Basic)
+      apply (auto intro: sigma_sets_top)
       done
-  next
-    fix a i assume "\<not> i < DIM('a)"
-    then show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
-      using top by auto
-  qed
-  then show ?thesis by (intro sets_sigma_subset) auto
-qed
-
-lemma atMost_span_atLeastAtMost:
-  "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>) \<subseteq>
-   sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>(a,b). {a..b})\<rparr>)"
-  (is "_ \<subseteq> sets ?SIGMA")
-proof -
-  have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
-  then interpret sigma_algebra ?SIGMA .
-  { fix a::'a
-    have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
-    proof (safe, simp_all add: eucl_le[where 'a='a])
-      fix x
-      from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
-      guess k::nat .. note k = this
-      { fix i assume "i < DIM('a)"
-        with k have "- x$$i \<le> real k"
-          by (subst (asm) Max_le_iff) (auto simp: field_simps)
-        then have "- real k \<le> x$$i" by simp }
-      then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
-        by (auto intro!: exI[of _ k])
-    qed
-    have "{..a} \<in> sets ?SIGMA" unfolding *
-      by (safe intro!: countable_UN)
-         (auto simp: sets_sigma intro!: sigma_sets.Basic) }
-  then show ?thesis by (intro sets_sigma_subset) auto
-qed
-
-lemma borel_eq_atMost:
-  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> a. {.. a::'a\<Colon>ordered_euclidean_space})\<rparr>)"
-    (is "_ = ?SIGMA")
-proof (intro algebra.equality antisym)
-  show "sets borel \<subseteq> sets ?SIGMA"
-    using halfspace_le_span_atMost halfspace_span_halfspace_le open_span_halfspace
-    by auto
-  show "sets ?SIGMA \<subseteq> sets borel"
-    by (rule borel.sets_sigma_subset) auto
+  qed (auto intro: sigma_sets_top sigma_sets.Empty)
 qed auto
 
 lemma borel_eq_atLeastAtMost:
-  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space, b). {a .. b})\<rparr>)"
-   (is "_ = ?SIGMA")
-proof (intro algebra.equality antisym)
-  show "sets borel \<subseteq> sets ?SIGMA"
-    using atMost_span_atLeastAtMost halfspace_le_span_atMost
-      halfspace_span_halfspace_le open_span_halfspace
-    by auto
-  show "sets ?SIGMA \<subseteq> sets borel"
-    by (rule borel.sets_sigma_subset) auto
-qed auto
-
-lemma borel_eq_greaterThan:
-  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {a <..})\<rparr>)"
-   (is "_ = ?SIGMA")
-proof (intro algebra.equality antisym)
-  show "sets borel \<subseteq> sets ?SIGMA"
-    using halfspace_le_span_greaterThan
-      halfspace_span_halfspace_le open_span_halfspace
-    by auto
-  show "sets ?SIGMA \<subseteq> sets borel"
-    by (rule borel.sets_sigma_subset) auto
-qed auto
-
-lemma borel_eq_lessThan:
-  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {..< a})\<rparr>)"
-   (is "_ = ?SIGMA")
-proof (intro algebra.equality antisym)
-  show "sets borel \<subseteq> sets ?SIGMA"
-    using halfspace_le_span_lessThan
-      halfspace_span_halfspace_ge open_span_halfspace
-    by auto
-  show "sets ?SIGMA \<subseteq> sets borel"
-    by (rule borel.sets_sigma_subset) auto
+  "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} \<Colon>'a\<Colon>ordered_euclidean_space set))"
+  (is "_ = ?SIGMA")
+proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
+  fix a::'a
+  have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
+  proof (safe, simp_all add: eucl_le[where 'a='a])
+    fix x
+    from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
+    guess k::nat .. note k = this
+    { fix i assume "i < DIM('a)"
+      with k have "- x$$i \<le> real k"
+        by (subst (asm) Max_le_iff) (auto simp: field_simps)
+      then have "- real k \<le> x$$i" by simp }
+    then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
+      by (auto intro!: exI[of _ k])
+  qed
+  show "{..a} \<in> ?SIGMA" unfolding *
+    by (safe intro!: countable_UN)
+       (auto intro!: sigma_sets_top)
 qed auto
 
 lemma borel_eq_greaterThanLessThan:
-  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, b). {a <..< (b :: 'a \<Colon> ordered_euclidean_space)})\<rparr>)"
+  "borel = sigma UNIV (range (\<lambda> (a, b). {a <..< b} :: 'a \<Colon> ordered_euclidean_space set))"
     (is "_ = ?SIGMA")
-proof (intro algebra.equality antisym)
-  show "sets ?SIGMA \<subseteq> sets borel"
-    by (rule borel.sets_sigma_subset) auto
-  show "sets borel \<subseteq> sets ?SIGMA"
-  proof -
-    have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
-    then interpret sigma_algebra ?SIGMA .
-    { fix M :: "'a set" assume "M \<in> {S. open S}"
-      then have "open M" by simp
-      have "M \<in> sets ?SIGMA"
-        apply (subst open_UNION[OF `open M`])
-        apply (safe intro!: countable_UN)
-        apply (auto simp add: sigma_def intro!: sigma_sets.Basic)
-        done }
-    then show ?thesis
-      unfolding borel_def by (intro sets_sigma_subset) auto
-  qed
+proof (rule borel_eq_sigmaI1[OF borel_def])
+  fix M :: "'a set" assume "M \<in> {S. open S}"
+  then have "open M" by simp
+  show "M \<in> ?SIGMA"
+    apply (subst open_UNION[OF `open M`])
+    apply (safe intro!: countable_UN)
+    apply auto
+    done
 qed auto
 
 lemma borel_eq_atLeastLessThan:
-  "borel = sigma \<lparr>space=UNIV, sets=range (\<lambda>(a, b). {a ..< b :: real})\<rparr>" (is "_ = ?S")
-proof (intro algebra.equality antisym)
-  interpret sigma_algebra ?S
-    by (rule sigma_algebra_sigma) auto
-  show "sets borel \<subseteq> sets ?S"
-    unfolding borel_eq_lessThan
-  proof (intro sets_sigma_subset subsetI)
-    have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
-    fix A :: "real set" assume "A \<in> sets \<lparr>space = UNIV, sets = range lessThan\<rparr>"
-    then obtain x where "A = {..< x}" by auto
-    then have "A = (\<Union>i::nat. {-real i ..< x})"
-      by (auto simp: move_uminus real_arch_simple)
-    then show "A \<in> sets ?S"
-      by (auto simp: sets_sigma intro!: sigma_sets.intros)
-  qed simp
-  show "sets ?S \<subseteq> sets borel"
-    by (intro borel.sets_sigma_subset) auto
-qed simp_all
-
-lemma borel_eq_halfspace_le:
-  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i \<le> a})\<rparr>)"
-   (is "_ = ?SIGMA")
-proof (intro algebra.equality antisym)
-  show "sets borel \<subseteq> sets ?SIGMA"
-    using open_span_halfspace halfspace_span_halfspace_le by auto
-  show "sets ?SIGMA \<subseteq> sets borel"
-    by (rule borel.sets_sigma_subset) auto
+  "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
+proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
+  have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
+  fix x :: real
+  have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
+    by (auto simp: move_uminus real_arch_simple)
+  then show "{..< x} \<in> ?SIGMA"
+    by (auto intro: sigma_sets.intros)
 qed auto
 
-lemma borel_eq_halfspace_less:
-  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i < a})\<rparr>)"
-   (is "_ = ?SIGMA")
-proof (intro algebra.equality antisym)
-  show "sets borel \<subseteq> sets ?SIGMA"
-    using open_span_halfspace .
-  show "sets ?SIGMA \<subseteq> sets borel"
-    by (rule borel.sets_sigma_subset) auto
-qed auto
-
-lemma borel_eq_halfspace_gt:
-  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a < x$$i})\<rparr>)"
-   (is "_ = ?SIGMA")
-proof (intro algebra.equality antisym)
-  show "sets borel \<subseteq> sets ?SIGMA"
-    using halfspace_le_span_halfspace_gt open_span_halfspace halfspace_span_halfspace_le by auto
-  show "sets ?SIGMA \<subseteq> sets borel"
-    by (rule borel.sets_sigma_subset) auto
-qed auto
-
-lemma borel_eq_halfspace_ge:
-  "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a \<le> x$$i})\<rparr>)"
-   (is "_ = ?SIGMA")
-proof (intro algebra.equality antisym)
-  show "sets borel \<subseteq> sets ?SIGMA"
-    using halfspace_span_halfspace_ge open_span_halfspace by auto
-  show "sets ?SIGMA \<subseteq> sets borel"
-    by (rule borel.sets_sigma_subset) auto
-qed auto
-
-lemma (in sigma_algebra) borel_measurable_halfspacesI:
+lemma borel_measurable_halfspacesI:
   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
-  assumes "borel = (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
-  and "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
-  and "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
+  assumes F: "borel = sigma UNIV (range F)"
+  and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M" 
+  and S: "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a::real. S a i \<in> sets M)"
 proof safe
   fix a :: real and i assume i: "i < DIM('c)" and f: "f \<in> borel_measurable M"
   then show "S a i \<in> sets M" unfolding assms
-    by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1) sigma_def)
+    by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1))
 next
   assume a: "\<forall>i<DIM('c). \<forall>a. S a i \<in> sets M"
   { fix a i have "S a i \<in> sets M"
@@ -740,61 +590,58 @@
       with a show ?thesis unfolding assms(2) by simp
     next
       assume "\<not> i < DIM('c)"
-      from assms(3)[OF this] show ?thesis .
+      from S[OF this] show ?thesis .
     qed }
-  then have "f \<in> measurable M (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
-    by (auto intro!: measurable_sigma simp: assms(2))
-  then show "f \<in> borel_measurable M" unfolding measurable_def
-    unfolding assms(1) by simp
+  then show "f \<in> borel_measurable M"
+    by (auto intro!: measurable_measure_of simp: S_eq F)
 qed
 
-lemma (in sigma_algebra) borel_measurable_iff_halfspace_le:
+lemma borel_measurable_iff_halfspace_le:
   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i \<le> a} \<in> sets M)"
   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
 
-lemma (in sigma_algebra) borel_measurable_iff_halfspace_less:
+lemma borel_measurable_iff_halfspace_less:
   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i < a} \<in> sets M)"
   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
 
-lemma (in sigma_algebra) borel_measurable_iff_halfspace_ge:
+lemma borel_measurable_iff_halfspace_ge:
   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a \<le> f w $$ i} \<in> sets M)"
   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
 
-lemma (in sigma_algebra) borel_measurable_iff_halfspace_greater:
+lemma borel_measurable_iff_halfspace_greater:
   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a < f w $$ i} \<in> sets M)"
-  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_gt]) auto
+  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
 
-lemma (in sigma_algebra) borel_measurable_iff_le:
+lemma borel_measurable_iff_le:
   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
 
-lemma (in sigma_algebra) borel_measurable_iff_less:
+lemma borel_measurable_iff_less:
   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
 
-lemma (in sigma_algebra) borel_measurable_iff_ge:
+lemma borel_measurable_iff_ge:
   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   using borel_measurable_iff_halfspace_ge[where 'c=real] by simp
 
-lemma (in sigma_algebra) borel_measurable_iff_greater:
+lemma borel_measurable_iff_greater:
   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
 
 lemma borel_measurable_euclidean_component:
   "(\<lambda>x::'a::euclidean_space. x $$ i) \<in> borel_measurable borel"
-  unfolding borel_def[where 'a=real]
-proof (rule borel.measurable_sigma, simp_all)
+proof (rule borel_measurableI)
   fix S::"real set" assume "open S"
   from open_vimage_euclidean_component[OF this]
-  show "(\<lambda>x. x $$ i) -` S \<in> sets borel"
+  show "(\<lambda>x. x $$ i) -` S \<inter> space borel \<in> sets borel"
     by (auto intro: borel_open)
 qed
 
-lemma (in sigma_algebra) borel_measurable_euclidean_space:
+lemma borel_measurable_euclidean_space:
   fixes f :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M)"
 proof safe
@@ -810,7 +657,7 @@
 
 subsection "Borel measurable operators"
 
-lemma (in sigma_algebra) affine_borel_measurable_vector:
+lemma affine_borel_measurable_vector:
   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   assumes "f \<in> borel_measurable M"
   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
@@ -821,8 +668,7 @@
     assume "b \<noteq> 0"
     with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
       by (auto intro!: open_affinity simp: scaleR_add_right)
-    hence "?S \<in> sets borel"
-      unfolding borel_def by (auto simp: sigma_def intro!: sigma_sets.Basic)
+    hence "?S \<in> sets borel" by auto
     moreover
     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
@@ -831,13 +677,13 @@
   qed simp
 qed
 
-lemma (in sigma_algebra) affine_borel_measurable:
+lemma affine_borel_measurable:
   fixes g :: "'a \<Rightarrow> real"
   assumes g: "g \<in> borel_measurable M"
   shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
   using affine_borel_measurable_vector[OF assms] by (simp add: mult_commute)
 
-lemma (in sigma_algebra) borel_measurable_add[simp, intro]:
+lemma borel_measurable_add[simp, intro]:
   fixes f :: "'a \<Rightarrow> real"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -855,7 +701,7 @@
     by (simp add: borel_measurable_iff_ge)
 qed
 
-lemma (in sigma_algebra) borel_measurable_setsum[simp, intro]:
+lemma borel_measurable_setsum[simp, intro]:
   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
@@ -864,7 +710,7 @@
   thus ?thesis using assms by induct auto
 qed simp
 
-lemma (in sigma_algebra) borel_measurable_square:
+lemma borel_measurable_square:
   fixes f :: "'a \<Rightarrow> real"
   assumes f: "f \<in> borel_measurable M"
   shows "(\<lambda>x. (f x)^2) \<in> borel_measurable M"
@@ -916,7 +762,7 @@
    shows"x*y = ((x+y)^2)/4 - ((x-y)^ 2)/4"
 by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric])
 
-lemma (in sigma_algebra) borel_measurable_uminus[simp, intro]:
+lemma borel_measurable_uminus[simp, intro]:
   fixes g :: "'a \<Rightarrow> real"
   assumes g: "g \<in> borel_measurable M"
   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
@@ -928,7 +774,7 @@
   finally show ?thesis .
 qed
 
-lemma (in sigma_algebra) borel_measurable_times[simp, intro]:
+lemma borel_measurable_times[simp, intro]:
   fixes f :: "'a \<Rightarrow> real"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -947,7 +793,7 @@
     using 1 2 by simp
 qed
 
-lemma (in sigma_algebra) borel_measurable_setprod[simp, intro]:
+lemma borel_measurable_setprod[simp, intro]:
   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
@@ -956,14 +802,14 @@
   thus ?thesis using assms by induct auto
 qed simp
 
-lemma (in sigma_algebra) borel_measurable_diff[simp, intro]:
+lemma borel_measurable_diff[simp, intro]:
   fixes f :: "'a \<Rightarrow> real"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   unfolding diff_minus using assms by fast
 
-lemma (in sigma_algebra) borel_measurable_inverse[simp, intro]:
+lemma borel_measurable_inverse[simp, intro]:
   fixes f :: "'a \<Rightarrow> real"
   assumes "f \<in> borel_measurable M"
   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
@@ -978,7 +824,7 @@
     by (auto intro!: Int Un)
 qed
 
-lemma (in sigma_algebra) borel_measurable_divide[simp, intro]:
+lemma borel_measurable_divide[simp, intro]:
   fixes f :: "'a \<Rightarrow> real"
   assumes "f \<in> borel_measurable M"
   and "g \<in> borel_measurable M"
@@ -986,7 +832,7 @@
   unfolding field_divide_inverse
   by (rule borel_measurable_inverse borel_measurable_times assms)+
 
-lemma (in sigma_algebra) borel_measurable_max[intro, simp]:
+lemma borel_measurable_max[intro, simp]:
   fixes f g :: "'a \<Rightarrow> real"
   assumes "f \<in> borel_measurable M"
   assumes "g \<in> borel_measurable M"
@@ -1001,7 +847,7 @@
     by (auto intro!: Int)
 qed
 
-lemma (in sigma_algebra) borel_measurable_min[intro, simp]:
+lemma borel_measurable_min[intro, simp]:
   fixes f g :: "'a \<Rightarrow> real"
   assumes "f \<in> borel_measurable M"
   assumes "g \<in> borel_measurable M"
@@ -1016,7 +862,7 @@
     by (auto intro!: Int)
 qed
 
-lemma (in sigma_algebra) borel_measurable_abs[simp, intro]:
+lemma borel_measurable_abs[simp, intro]:
   assumes "f \<in> borel_measurable M"
   shows "(\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
 proof -
@@ -1033,14 +879,14 @@
   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   assumes "continuous_on UNIV f"
   shows "f \<in> borel_measurable borel"
-  apply(rule borel.borel_measurableI)
+  apply(rule borel_measurableI)
   using continuous_open_preimage[OF assms] unfolding vimage_def by auto
 
 lemma borel_measurable_continuous_on:
   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   assumes cont: "continuous_on A f" "open A"
   shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
-proof (rule borel.borel_measurableI)
+proof (rule borel_measurableI)
   fix S :: "'b set" assume "open S"
   then have "open {x\<in>A. f x \<in> S}"
     by (intro continuous_open_preimage[OF cont]) auto
@@ -1049,11 +895,11 @@
     {x\<in>A. f x \<in> S} \<union> (if c \<in> S then space borel - A else {})"
     by (auto split: split_if_asm)
   also have "\<dots> \<in> sets borel"
-    using * `open A` by (auto simp del: space_borel intro!: borel.Un)
+    using * `open A` by (auto simp del: space_borel intro!: Un)
   finally show "?f -` S \<inter> space borel \<in> sets borel" .
 qed
 
-lemma (in sigma_algebra) convex_measurable:
+lemma convex_measurable:
   fixes a b :: real
   assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> { a <..< b}"
   assumes q: "convex_on { a <..< b} q"
@@ -1091,7 +937,7 @@
   finally show ?thesis .
 qed
 
-lemma (in sigma_algebra) borel_measurable_log[simp,intro]:
+lemma borel_measurable_log[simp,intro]:
   assumes f: "f \<in> borel_measurable M" and "1 < b"
   shows "(\<lambda>x. log b (f x)) \<in> borel_measurable M"
   using measurable_comp[OF f borel_measurable_borel_log[OF `1 < b`]]
@@ -1101,25 +947,21 @@
 
 lemma borel_measurable_ereal_borel:
   "ereal \<in> borel_measurable borel"
-  unfolding borel_def[where 'a=ereal]
-proof (rule borel.measurable_sigma)
-  fix X :: "ereal set" assume "X \<in> sets \<lparr>space = UNIV, sets = {S. open S} \<rparr>"
-  then have "open X" by simp
+proof (rule borel_measurableI)
+  fix X :: "ereal set" assume "open X"
   then have "open (ereal -` X \<inter> space borel)"
     by (simp add: open_ereal_vimage)
   then show "ereal -` X \<inter> space borel \<in> sets borel" by auto
-qed auto
+qed
 
-lemma (in sigma_algebra) borel_measurable_ereal[simp, intro]:
+lemma borel_measurable_ereal[simp, intro]:
   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   using measurable_comp[OF f borel_measurable_ereal_borel] unfolding comp_def .
 
 lemma borel_measurable_real_of_ereal_borel:
   "(real :: ereal \<Rightarrow> real) \<in> borel_measurable borel"
-  unfolding borel_def[where 'a=real]
-proof (rule borel.measurable_sigma)
-  fix B :: "real set" assume "B \<in> sets \<lparr>space = UNIV, sets = {S. open S} \<rparr>"
-  then have "open B" by simp
+proof (rule borel_measurableI)
+  fix B :: "real set" assume "open B"
   have *: "ereal -` real -` (B - {0}) = B - {0}" by auto
   have open_real: "open (real -` (B - {0}) :: ereal set)"
     unfolding open_ereal_def * using `open B` by auto
@@ -1137,13 +979,13 @@
     then show "(real -` B :: ereal set) \<inter> space borel \<in> sets borel"
       using open_real by auto
   qed
-qed auto
+qed
 
-lemma (in sigma_algebra) borel_measurable_real_of_ereal[simp, intro]:
+lemma borel_measurable_real_of_ereal[simp, intro]:
   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: ereal)) \<in> borel_measurable M"
   using measurable_comp[OF f borel_measurable_real_of_ereal_borel] unfolding comp_def .
 
-lemma (in sigma_algebra) borel_measurable_ereal_iff:
+lemma borel_measurable_ereal_iff:
   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
 proof
   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
@@ -1151,7 +993,7 @@
   show "f \<in> borel_measurable M" by auto
 qed auto
 
-lemma (in sigma_algebra) borel_measurable_ereal_iff_real:
+lemma borel_measurable_ereal_iff_real:
   fixes f :: "'a \<Rightarrow> ereal"
   shows "f \<in> borel_measurable M \<longleftrightarrow>
     ((\<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)"
@@ -1165,7 +1007,7 @@
   finally show "f \<in> borel_measurable M" .
 qed (auto intro: measurable_sets borel_measurable_real_of_ereal)
 
-lemma (in sigma_algebra) less_eq_ge_measurable:
+lemma less_eq_ge_measurable:
   fixes f :: "'a \<Rightarrow> 'c::linorder"
   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
 proof
@@ -1178,7 +1020,7 @@
   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
 qed
 
-lemma (in sigma_algebra) greater_eq_le_measurable:
+lemma greater_eq_le_measurable:
   fixes f :: "'a \<Rightarrow> 'c::linorder"
   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
 proof
@@ -1191,28 +1033,27 @@
   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_uminus_borel_ereal:
+lemma borel_measurable_uminus_borel_ereal:
   "(uminus :: ereal \<Rightarrow> ereal) \<in> borel_measurable borel"
-proof (subst borel_def, rule borel.measurable_sigma)
-  fix X :: "ereal set" assume "X \<in> sets \<lparr>space = UNIV, sets = {S. open S}\<rparr>"
-  then have "open X" by simp
+proof (rule borel_measurableI)
+  fix X :: "ereal set" assume "open X"
   have "uminus -` X = uminus ` X" by (force simp: image_iff)
   then have "open (uminus -` X)" using `open X` ereal_open_uminus by auto
   then show "uminus -` X \<inter> space borel \<in> sets borel" by auto
-qed auto
+qed
 
-lemma (in sigma_algebra) borel_measurable_uminus_ereal[intro]:
+lemma borel_measurable_uminus_ereal[intro]:
   assumes "f \<in> borel_measurable M"
   shows "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
   using measurable_comp[OF assms borel_measurable_uminus_borel_ereal] by (simp add: comp_def)
 
-lemma (in sigma_algebra) borel_measurable_uminus_eq_ereal[simp]:
+lemma borel_measurable_uminus_eq_ereal[simp]:
   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
 proof
   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
 qed auto
 
-lemma (in sigma_algebra) borel_measurable_eq_atMost_ereal:
+lemma borel_measurable_eq_atMost_ereal:
   fixes f :: "'a \<Rightarrow> ereal"
   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
 proof (intro iffI allI)
@@ -1244,7 +1085,7 @@
   qed
 qed (simp add: measurable_sets)
 
-lemma (in sigma_algebra) borel_measurable_eq_atLeast_ereal:
+lemma borel_measurable_eq_atLeast_ereal:
   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
 proof
   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
@@ -1255,15 +1096,15 @@
   then show "f \<in> borel_measurable M" by simp
 qed (simp add: measurable_sets)
 
-lemma (in sigma_algebra) borel_measurable_ereal_iff_less:
+lemma borel_measurable_ereal_iff_less:
   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
   unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
 
-lemma (in sigma_algebra) borel_measurable_ereal_iff_ge:
+lemma borel_measurable_ereal_iff_ge:
   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
   unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
 
-lemma (in sigma_algebra) borel_measurable_ereal_eq_const:
+lemma borel_measurable_ereal_eq_const:
   fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
   shows "{x\<in>space M. f x = c} \<in> sets M"
 proof -
@@ -1271,7 +1112,7 @@
   then show ?thesis using assms by (auto intro!: measurable_sets)
 qed
 
-lemma (in sigma_algebra) borel_measurable_ereal_neq_const:
+lemma borel_measurable_ereal_neq_const:
   fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
   shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
 proof -
@@ -1279,7 +1120,7 @@
   then show ?thesis using assms by (auto intro!: measurable_sets)
 qed
 
-lemma (in sigma_algebra) borel_measurable_ereal_le[intro,simp]:
+lemma borel_measurable_ereal_le[intro,simp]:
   fixes f g :: "'a \<Rightarrow> ereal"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -1294,7 +1135,7 @@
   with f g show ?thesis by (auto intro!: Un simp: measurable_sets)
 qed
 
-lemma (in sigma_algebra) borel_measurable_ereal_less[intro,simp]:
+lemma borel_measurable_ereal_less[intro,simp]:
   fixes f :: "'a \<Rightarrow> ereal"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -1304,7 +1145,7 @@
   then show ?thesis using g f by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_ereal_eq[intro,simp]:
+lemma borel_measurable_ereal_eq[intro,simp]:
   fixes f :: "'a \<Rightarrow> ereal"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -1314,7 +1155,7 @@
   then show ?thesis using g f by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_ereal_neq[intro,simp]:
+lemma borel_measurable_ereal_neq[intro,simp]:
   fixes f :: "'a \<Rightarrow> ereal"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
@@ -1324,12 +1165,12 @@
   thus ?thesis using f g by auto
 qed
 
-lemma (in sigma_algebra) split_sets:
+lemma split_sets:
   "{x\<in>space M. P x \<or> Q x} = {x\<in>space M. P x} \<union> {x\<in>space M. Q x}"
   "{x\<in>space M. P x \<and> Q x} = {x\<in>space M. P x} \<inter> {x\<in>space M. Q x}"
   by auto
 
-lemma (in sigma_algebra) borel_measurable_ereal_add[intro, simp]:
+lemma borel_measurable_ereal_add[intro, simp]:
   fixes f :: "'a \<Rightarrow> ereal"
   assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
@@ -1344,7 +1185,7 @@
              intro!: Un measurable_If measurable_sets)
 qed
 
-lemma (in sigma_algebra) borel_measurable_ereal_setsum[simp, intro]:
+lemma borel_measurable_ereal_setsum[simp, intro]:
   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
@@ -1354,7 +1195,7 @@
     by induct auto
 qed (simp add: borel_measurable_const)
 
-lemma (in sigma_algebra) borel_measurable_ereal_abs[intro, simp]:
+lemma borel_measurable_ereal_abs[intro, simp]:
   fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
   shows "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
 proof -
@@ -1362,7 +1203,7 @@
   then show ?thesis using assms by (auto intro!: measurable_If)
 qed
 
-lemma (in sigma_algebra) borel_measurable_ereal_times[intro, simp]:
+lemma borel_measurable_ereal_times[intro, simp]:
   fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
 proof -
@@ -1386,7 +1227,7 @@
        (auto simp: split_sets intro!: Int)
 qed
 
-lemma (in sigma_algebra) borel_measurable_ereal_setprod[simp, intro]:
+lemma borel_measurable_ereal_setprod[simp, intro]:
   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
@@ -1395,21 +1236,21 @@
   thus ?thesis using assms by induct auto
 qed simp
 
-lemma (in sigma_algebra) borel_measurable_ereal_min[simp, intro]:
+lemma borel_measurable_ereal_min[simp, intro]:
   fixes f g :: "'a \<Rightarrow> ereal"
   assumes "f \<in> borel_measurable M"
   assumes "g \<in> borel_measurable M"
   shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
   using assms unfolding min_def by (auto intro!: measurable_If)
 
-lemma (in sigma_algebra) borel_measurable_ereal_max[simp, intro]:
+lemma borel_measurable_ereal_max[simp, intro]:
   fixes f g :: "'a \<Rightarrow> ereal"
   assumes "f \<in> borel_measurable M"
   and "g \<in> borel_measurable M"
   shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   using assms unfolding max_def by (auto intro!: measurable_If)
 
-lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
+lemma borel_measurable_SUP[simp, intro]:
   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
@@ -1422,7 +1263,7 @@
     using assms by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
+lemma borel_measurable_INF[simp, intro]:
   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
@@ -1435,26 +1276,39 @@
     using assms by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_liminf[simp, intro]:
+lemma borel_measurable_liminf[simp, intro]:
   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   assumes "\<And>i. f i \<in> borel_measurable M"
   shows "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   unfolding liminf_SUPR_INFI using assms by auto
 
-lemma (in sigma_algebra) borel_measurable_limsup[simp, intro]:
+lemma borel_measurable_limsup[simp, intro]:
   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   assumes "\<And>i. f i \<in> borel_measurable M"
   shows "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
   unfolding limsup_INFI_SUPR using assms by auto
 
-lemma (in sigma_algebra) borel_measurable_ereal_diff[simp, intro]:
+lemma borel_measurable_ereal_diff[simp, intro]:
   fixes f g :: "'a \<Rightarrow> ereal"
   assumes "f \<in> borel_measurable M"
   assumes "g \<in> borel_measurable M"
   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   unfolding minus_ereal_def using assms by auto
 
-lemma (in sigma_algebra) borel_measurable_psuminf[simp, intro]:
+lemma borel_measurable_ereal_inverse[simp, intro]:
+  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
+proof -
+  { fix x have "inverse (f x) = (if f x = 0 then \<infinity> else ereal (inverse (real (f x))))"
+      by (cases "f x") auto }
+  with f show ?thesis
+    by (auto intro!: measurable_If)
+qed
+
+lemma borel_measurable_ereal_divide[simp, intro]:
+  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. f x / g x :: ereal) \<in> borel_measurable M"
+  unfolding divide_ereal_def by auto
+
+lemma borel_measurable_psuminf[simp, intro]:
   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   assumes "\<And>i. f i \<in> borel_measurable M" and pos: "\<And>i x. x \<in> space M \<Longrightarrow> 0 \<le> f i x"
   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
@@ -1465,7 +1319,7 @@
 
 section "LIMSEQ is borel measurable"
 
-lemma (in sigma_algebra) borel_measurable_LIMSEQ:
+lemma borel_measurable_LIMSEQ:
   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
   and u: "\<And>i. u i \<in> borel_measurable M"
--- a/src/HOL/Probability/Caratheodory.thy	Mon Apr 23 12:23:23 2012 +0100
+++ b/src/HOL/Probability/Caratheodory.thy	Mon Apr 23 12:14:35 2012 +0200
@@ -6,7 +6,7 @@
 header {*Caratheodory Extension Theorem*}
 
 theory Caratheodory
-imports Sigma_Algebra "~~/src/HOL/Multivariate_Analysis/Extended_Real_Limits"
+  imports Measure_Space
 begin
 
 lemma sums_def2:
@@ -53,128 +53,53 @@
 
 subsection {* Measure Spaces *}
 
-record 'a measure_space = "'a algebra" +
-  measure :: "'a set \<Rightarrow> ereal"
-
-definition positive where "positive M f \<longleftrightarrow> f {} = (0::ereal) \<and> (\<forall>A\<in>sets M. 0 \<le> f A)"
-
-definition additive where "additive M f \<longleftrightarrow>
-  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) = f x + f y)"
-
-definition countably_additive :: "('a, 'b) algebra_scheme \<Rightarrow> ('a set \<Rightarrow> ereal) \<Rightarrow> bool" where
-  "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>
-    (\<Sum>i. f (A i)) = f (\<Union>i. A i))"
-
-definition increasing where "increasing M f \<longleftrightarrow>
-  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<subseteq> y \<longrightarrow> f x \<le> f y)"
-
 definition subadditive where "subadditive M f \<longleftrightarrow>
-  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) \<le> f x + f y)"
+  (\<forall>x\<in>M. \<forall>y\<in>M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) \<le> f x + f y)"
 
 definition countably_subadditive where "countably_subadditive M f \<longleftrightarrow>
-  (\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow>
+  (\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow>
     (f (\<Union>i. A i) \<le> (\<Sum>i. f (A i))))"
 
-definition lambda_system where "lambda_system M f = {l \<in> sets M.
-  \<forall>x \<in> sets M. f (l \<inter> x) + f ((space M - l) \<inter> x) = f x}"
+definition lambda_system where "lambda_system \<Omega> M f = {l \<in> M.
+  \<forall>x \<in> M. f (l \<inter> x) + f ((\<Omega> - l) \<inter> x) = f x}"
 
 definition outer_measure_space where "outer_measure_space M f \<longleftrightarrow>
   positive M f \<and> increasing M f \<and> countably_subadditive M f"
 
 definition measure_set where "measure_set M f X = {r.
-  \<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}"
-
-locale measure_space = sigma_algebra M for M :: "('a, 'b) measure_space_scheme" +
-  assumes measure_positive: "positive M (measure M)"
-      and ca: "countably_additive M (measure M)"
-
-abbreviation (in measure_space) "\<mu> \<equiv> measure M"
-
-lemma (in measure_space)
-  shows empty_measure[simp, intro]: "\<mu> {} = 0"
-  and positive_measure[simp, intro!]: "\<And>A. A \<in> sets M \<Longrightarrow> 0 \<le> \<mu> A"
-  using measure_positive unfolding positive_def by auto
-
-lemma increasingD:
-  "increasing M f \<Longrightarrow> x \<subseteq> y \<Longrightarrow> x\<in>sets M \<Longrightarrow> y\<in>sets M \<Longrightarrow> f x \<le> f y"
-  by (auto simp add: increasing_def)
+  \<exists>A. range A \<subseteq> M \<and> disjoint_family A \<and> X \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
 
 lemma subadditiveD:
-  "subadditive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x \<in> sets M \<Longrightarrow> y \<in> sets M
-    \<Longrightarrow> f (x \<union> y) \<le> f x + f y"
+  "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"
   by (auto simp add: subadditive_def)
 
-lemma additiveD:
-  "additive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x \<in> sets M \<Longrightarrow> y \<in> sets M
-    \<Longrightarrow> f (x \<union> y) = f x + f y"
-  by (auto simp add: additive_def)
-
-lemma countably_additiveI:
-  assumes "\<And>A x. range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Union>i. A i) \<in> sets M
-    \<Longrightarrow> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
-  shows "countably_additive M f"
-  using assms by (simp add: countably_additive_def)
-
-section "Extend binary sets"
-
-lemma LIMSEQ_binaryset:
-  assumes f: "f {} = 0"
-  shows  "(\<lambda>n. \<Sum>i<n. f (binaryset A B i)) ----> f A + f B"
-proof -
-  have "(\<lambda>n. \<Sum>i < Suc (Suc n). f (binaryset A B i)) = (\<lambda>n. f A + f B)"
-    proof
-      fix n
-      show "(\<Sum>i < Suc (Suc n). f (binaryset A B i)) = f A + f B"
-        by (induct n)  (auto simp add: binaryset_def f)
-    qed
-  moreover
-  have "... ----> f A + f B" by (rule tendsto_const)
-  ultimately
-  have "(\<lambda>n. \<Sum>i< Suc (Suc n). f (binaryset A B i)) ----> f A + f B"
-    by metis
-  hence "(\<lambda>n. \<Sum>i< n+2. f (binaryset A B i)) ----> f A + f B"
-    by simp
-  thus ?thesis by (rule LIMSEQ_offset [where k=2])
-qed
-
-lemma binaryset_sums:
-  assumes f: "f {} = 0"
-  shows  "(\<lambda>n. f (binaryset A B n)) sums (f A + f B)"
-    by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f] atLeast0LessThan)
-
-lemma suminf_binaryset_eq:
-  fixes f :: "'a set \<Rightarrow> 'b::{comm_monoid_add, t2_space}"
-  shows "f {} = 0 \<Longrightarrow> (\<Sum>n. f (binaryset A B n)) = f A + f B"
-  by (metis binaryset_sums sums_unique)
-
 subsection {* Lambda Systems *}
 
 lemma (in algebra) lambda_system_eq:
-  shows "lambda_system M f = {l \<in> sets M.
-    \<forall>x \<in> sets M. f (x \<inter> l) + f (x - l) = f x}"
+  shows "lambda_system \<Omega> M f = {l \<in> M. \<forall>x \<in> M. f (x \<inter> l) + f (x - l) = f x}"
 proof -
-  have [simp]: "!!l x. l \<in> sets M \<Longrightarrow> x \<in> sets M \<Longrightarrow> (space M - l) \<inter> x = x - l"
+  have [simp]: "!!l x. l \<in> M \<Longrightarrow> x \<in> M \<Longrightarrow> (\<Omega> - l) \<inter> x = x - l"
     by (metis Int_Diff Int_absorb1 Int_commute sets_into_space)
   show ?thesis
     by (auto simp add: lambda_system_def) (metis Int_commute)+
 qed
 
 lemma (in algebra) lambda_system_empty:
-  "positive M f \<Longrightarrow> {} \<in> lambda_system M f"
+  "positive M f \<Longrightarrow> {} \<in> lambda_system \<Omega> M f"
   by (auto simp add: positive_def lambda_system_eq)
 
 lemma lambda_system_sets:
-  "x \<in> lambda_system M f \<Longrightarrow> x \<in> sets M"
+  "x \<in> lambda_system \<Omega> M f \<Longrightarrow> x \<in> M"
   by (simp add: lambda_system_def)
 
 lemma (in algebra) lambda_system_Compl:
   fixes f:: "'a set \<Rightarrow> ereal"
-  assumes x: "x \<in> lambda_system M f"
-  shows "space M - x \<in> lambda_system M f"
+  assumes x: "x \<in> lambda_system \<Omega> M f"
+  shows "\<Omega> - x \<in> lambda_system \<Omega> M f"
 proof -
-  have "x \<subseteq> space M"
+  have "x \<subseteq> \<Omega>"
     by (metis sets_into_space lambda_system_sets x)
-  hence "space M - (space M - x) = x"
+  hence "\<Omega> - (\<Omega> - x) = x"
     by (metis double_diff equalityE)
   with x show ?thesis
     by (force simp add: lambda_system_def ac_simps)
@@ -182,16 +107,16 @@
 
 lemma (in algebra) lambda_system_Int:
   fixes f:: "'a set \<Rightarrow> ereal"
-  assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
-  shows "x \<inter> y \<in> lambda_system M f"
+  assumes xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
+  shows "x \<inter> y \<in> lambda_system \<Omega> M f"
 proof -
   from xl yl show ?thesis
   proof (auto simp add: positive_def lambda_system_eq Int)
     fix u
-    assume x: "x \<in> sets M" and y: "y \<in> sets M" and u: "u \<in> sets M"
-       and fx: "\<forall>z\<in>sets M. f (z \<inter> x) + f (z - x) = f z"
-       and fy: "\<forall>z\<in>sets M. f (z \<inter> y) + f (z - y) = f z"
-    have "u - x \<inter> y \<in> sets M"
+    assume x: "x \<in> M" and y: "y \<in> M" and u: "u \<in> M"
+       and fx: "\<forall>z\<in>M. f (z \<inter> x) + f (z - x) = f z"
+       and fy: "\<forall>z\<in>M. f (z \<inter> y) + f (z - y) = f z"
+    have "u - x \<inter> y \<in> M"
       by (metis Diff Diff_Int Un u x y)
     moreover
     have "(u - (x \<inter> y)) \<inter> y = u \<inter> y - x" by blast
@@ -216,20 +141,20 @@
 
 lemma (in algebra) lambda_system_Un:
   fixes f:: "'a set \<Rightarrow> ereal"
-  assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
-  shows "x \<union> y \<in> lambda_system M f"
+  assumes xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
+  shows "x \<union> y \<in> lambda_system \<Omega> M f"
 proof -
-  have "(space M - x) \<inter> (space M - y) \<in> sets M"
+  have "(\<Omega> - x) \<inter> (\<Omega> - y) \<in> M"
     by (metis Diff_Un Un compl_sets lambda_system_sets xl yl)
   moreover
-  have "x \<union> y = space M - ((space M - x) \<inter> (space M - y))"
+  have "x \<union> y = \<Omega> - ((\<Omega> - x) \<inter> (\<Omega> - y))"
     by auto (metis subsetD lambda_system_sets sets_into_space xl yl)+
   ultimately show ?thesis
     by (metis lambda_system_Compl lambda_system_Int xl yl)
 qed
 
 lemma (in algebra) lambda_system_algebra:
-  "positive M f \<Longrightarrow> algebra (M\<lparr>sets := lambda_system M f\<rparr>)"
+  "positive M f \<Longrightarrow> algebra \<Omega> (lambda_system \<Omega> M f)"
   apply (auto simp add: algebra_iff_Un)
   apply (metis lambda_system_sets set_mp sets_into_space)
   apply (metis lambda_system_empty)
@@ -238,117 +163,62 @@
   done
 
 lemma (in algebra) lambda_system_strong_additive:
-  assumes z: "z \<in> sets M" and disj: "x \<inter> y = {}"
-      and xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
+  assumes z: "z \<in> M" and disj: "x \<inter> y = {}"
+      and xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
   shows "f (z \<inter> (x \<union> y)) = f (z \<inter> x) + f (z \<inter> y)"
 proof -
   have "z \<inter> x = (z \<inter> (x \<union> y)) \<inter> x" using disj by blast
   moreover
   have "z \<inter> y = (z \<inter> (x \<union> y)) - x" using disj by blast
   moreover
-  have "(z \<inter> (x \<union> y)) \<in> sets M"
+  have "(z \<inter> (x \<union> y)) \<in> M"
     by (metis Int Un lambda_system_sets xl yl z)
   ultimately show ?thesis using xl yl
     by (simp add: lambda_system_eq)
 qed
 
-lemma (in algebra) lambda_system_additive:
-     "additive (M (|sets := lambda_system M f|)) f"
+lemma (in algebra) lambda_system_additive: "additive (lambda_system \<Omega> M f) f"
 proof (auto simp add: additive_def)
   fix x and y
   assume disj: "x \<inter> y = {}"
-     and xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
-  hence  "x \<in> sets M" "y \<in> sets M" by (blast intro: lambda_system_sets)+
+     and xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
+  hence  "x \<in> M" "y \<in> M" by (blast intro: lambda_system_sets)+
   thus "f (x \<union> y) = f x + f y"
     using lambda_system_strong_additive [OF top disj xl yl]
     by (simp add: Un)
 qed
 
-lemma (in ring_of_sets) disjointed_additive:
-  assumes f: "positive M f" "additive M f" and A: "range A \<subseteq> sets M" "incseq A"
-  shows "(\<Sum>i\<le>n. f (disjointed A i)) = f (A n)"
-proof (induct n)
-  case (Suc n)
-  then have "(\<Sum>i\<le>Suc n. f (disjointed A i)) = f (A n) + f (disjointed A (Suc n))"
-    by simp
-  also have "\<dots> = f (A n \<union> disjointed A (Suc n))"
-    using A by (subst f(2)[THEN additiveD]) (auto simp: disjointed_incseq)
-  also have "A n \<union> disjointed A (Suc n) = A (Suc n)"
-    using `incseq A` by (auto dest: incseq_SucD simp: disjointed_incseq)
-  finally show ?case .
-qed simp
-
 lemma (in ring_of_sets) countably_subadditive_subadditive:
   assumes f: "positive M f" and cs: "countably_subadditive M f"
   shows  "subadditive M f"
 proof (auto simp add: subadditive_def)
   fix x y
-  assume x: "x \<in> sets M" and y: "y \<in> sets M" and "x \<inter> y = {}"
+  assume x: "x \<in> M" and y: "y \<in> M" and "x \<inter> y = {}"
   hence "disjoint_family (binaryset x y)"
     by (auto simp add: disjoint_family_on_def binaryset_def)
-  hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
-         (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
+  hence "range (binaryset x y) \<subseteq> M \<longrightarrow>
+         (\<Union>i. binaryset x y i) \<in> M \<longrightarrow>
          f (\<Union>i. binaryset x y i) \<le> (\<Sum> n. f (binaryset x y n))"
     using cs by (auto simp add: countably_subadditive_def)
-  hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
+  hence "{x,y,{}} \<subseteq> M \<longrightarrow> x \<union> y \<in> M \<longrightarrow>
          f (x \<union> y) \<le> (\<Sum> n. f (binaryset x y n))"
     by (simp add: range_binaryset_eq UN_binaryset_eq)
   thus "f (x \<union> y) \<le>  f x + f y" using f x y
     by (auto simp add: Un o_def suminf_binaryset_eq positive_def)
 qed
 
-lemma (in ring_of_sets) additive_sum:
-  fixes A:: "nat \<Rightarrow> 'a set"
-  assumes f: "positive M f" and ad: "additive M f" and "finite S"
-      and A: "range A \<subseteq> sets M"
-      and disj: "disjoint_family_on A S"
-  shows  "(\<Sum>i\<in>S. f (A i)) = f (\<Union>i\<in>S. A i)"
-using `finite S` disj proof induct
-  case empty show ?case using f by (simp add: positive_def)
-next
-  case (insert s S)
-  then have "A s \<inter> (\<Union>i\<in>S. A i) = {}"
-    by (auto simp add: disjoint_family_on_def neq_iff)
-  moreover
-  have "A s \<in> sets M" using A by blast
-  moreover have "(\<Union>i\<in>S. A i) \<in> sets M"
-    using A `finite S` by auto
-  moreover
-  ultimately have "f (A s \<union> (\<Union>i\<in>S. A i)) = f (A s) + f(\<Union>i\<in>S. A i)"
-    using ad UNION_in_sets A by (auto simp add: additive_def)
-  with insert show ?case using ad disjoint_family_on_mono[of S "insert s S" A]
-    by (auto simp add: additive_def subset_insertI)
-qed
-
-lemma (in algebra) increasing_additive_bound:
-  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> ereal"
-  assumes f: "positive M f" and ad: "additive M f"
-      and inc: "increasing M f"
-      and A: "range A \<subseteq> sets M"
-      and disj: "disjoint_family A"
-  shows  "(\<Sum>i. f (A i)) \<le> f (space M)"
-proof (safe intro!: suminf_bound)
-  fix N
-  note disj_N = disjoint_family_on_mono[OF _ disj, of "{..<N}"]
-  have "(\<Sum>i<N. f (A i)) = f (\<Union>i\<in>{..<N}. A i)"
-    by (rule additive_sum [OF f ad _ A]) (auto simp: disj_N)
-  also have "... \<le> f (space M)" using space_closed A
-    by (intro increasingD[OF inc] finite_UN) auto
-  finally show "(\<Sum>i<N. f (A i)) \<le> f (space M)" by simp
-qed (insert f A, auto simp: positive_def)
-
 lemma lambda_system_increasing:
- "increasing M f \<Longrightarrow> increasing (M (|sets := lambda_system M f|)) f"
+ "increasing M f \<Longrightarrow> increasing (lambda_system \<Omega> M f) f"
   by (simp add: increasing_def lambda_system_def)
 
 lemma lambda_system_positive:
-  "positive M f \<Longrightarrow> positive (M (|sets := lambda_system M f|)) f"
+  "positive M f \<Longrightarrow> positive (lambda_system \<Omega> M f) f"
   by (simp add: positive_def lambda_system_def)
 
 lemma (in algebra) lambda_system_strong_sum:
   fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> ereal"
-  assumes f: "positive M f" and a: "a \<in> sets M"
-      and A: "range A \<subseteq> lambda_system M f"
+  assumes f: "positive M f" and a: "a \<in> M"
+      and A: "range A \<subseteq> lambda_system \<Omega> M f"
       and disj: "disjoint_family A"
   shows  "(\<Sum>i = 0..<n. f (a \<inter>A i)) = f (a \<inter> (\<Union>i\<in>{0..<n}. A i))"
 proof (induct n)
@@ -357,11 +227,11 @@
   case (Suc n)
   have 2: "A n \<inter> UNION {0..<n} A = {}" using disj
     by (force simp add: disjoint_family_on_def neq_iff)
-  have 3: "A n \<in> lambda_system M f" using A
+  have 3: "A n \<in> lambda_system \<Omega> M f" using A
     by blast
-  interpret l: algebra "M\<lparr>sets := lambda_system M f\<rparr>"
+  interpret l: algebra \<Omega> "lambda_system \<Omega> M f"
     using f by (rule lambda_system_algebra)
-  have 4: "UNION {0..<n} A \<in> lambda_system M f"
+  have 4: "UNION {0..<n} A \<in> lambda_system \<Omega> M f"
     using A l.UNION_in_sets by simp
   from Suc.hyps show ?case
     by (simp add: atLeastLessThanSuc lambda_system_strong_additive [OF a 2 3 4])
@@ -369,23 +239,23 @@
 
 lemma (in sigma_algebra) lambda_system_caratheodory:
   assumes oms: "outer_measure_space M f"
-      and A: "range A \<subseteq> lambda_system M f"
+      and A: "range A \<subseteq> lambda_system \<Omega> M f"
       and disj: "disjoint_family A"
-  shows  "(\<Union>i. A i) \<in> lambda_system M f \<and> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
+  shows  "(\<Union>i. A i) \<in> lambda_system \<Omega> M f \<and> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
 proof -
   have pos: "positive M f" and inc: "increasing M f"
    and csa: "countably_subadditive M f"
     by (metis oms outer_measure_space_def)+
   have sa: "subadditive M f"
     by (metis countably_subadditive_subadditive csa pos)
-  have A': "range A \<subseteq> sets (M(|sets := lambda_system M f|))" using A
-    by simp
-  interpret ls: algebra "M\<lparr>sets := lambda_system M f\<rparr>"
+  have A': "\<And>S. A`S \<subseteq> (lambda_system \<Omega> M f)" using A
+    by auto
+  interpret ls: algebra \<Omega> "lambda_system \<Omega> M f"
     using pos by (rule lambda_system_algebra)
-  have A'': "range A \<subseteq> sets M"
+  have A'': "range A \<subseteq> M"
      by (metis A image_subset_iff lambda_system_sets)
 
-  have U_in: "(\<Union>i. A i) \<in> sets M"
+  have U_in: "(\<Union>i. A i) \<in> M"
     by (metis A'' countable_UN)
   have U_eq: "f (\<Union>i. A i) = (\<Sum>i. f (A i))"
   proof (rule antisym)
@@ -396,22 +266,22 @@
     show "(\<Sum>i. f (A i)) \<le> f (\<Union>i. A i)"
       using ls.additive_sum [OF lambda_system_positive[OF pos] lambda_system_additive _ A' dis]
       using A''
-      by (intro suminf_bound[OF _ *]) (auto intro!: increasingD[OF inc] allI countable_UN)
+      by (intro suminf_bound[OF _ *]) (auto intro!: increasingD[OF inc] countable_UN)
   qed
   {
     fix a
-    assume a [iff]: "a \<in> sets M"
+    assume a [iff]: "a \<in> M"
     have "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) = f a"
     proof -
       show ?thesis
       proof (rule antisym)
-        have "range (\<lambda>i. a \<inter> A i) \<subseteq> sets M" using A''
+        have "range (\<lambda>i. a \<inter> A i) \<subseteq> M" using A''
           by blast
         moreover
         have "disjoint_family (\<lambda>i. a \<inter> A i)" using disj
           by (auto simp add: disjoint_family_on_def)
         moreover
-        have "a \<inter> (\<Union>i. A i) \<in> sets M"
+        have "a \<inter> (\<Union>i. A i) \<in> M"
           by (metis Int U_in a)
         ultimately
         have "f (a \<inter> (\<Union>i. A i)) \<le> (\<Sum>i. f (a \<inter> A i))"
@@ -424,11 +294,11 @@
         have "(\<Sum>i. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
           proof (intro suminf_bound_add allI)
             fix n
-            have UNION_in: "(\<Union>i\<in>{0..<n}. A i) \<in> sets M"
+            have UNION_in: "(\<Union>i\<in>{0..<n}. A i) \<in> M"
               by (metis A'' UNION_in_sets)
             have le_fa: "f (UNION {0..<n} A \<inter> a) \<le> f a" using A''
               by (blast intro: increasingD [OF inc] A'' UNION_in_sets)
-            have ls: "(\<Union>i\<in>{0..<n}. A i) \<in> lambda_system M f"
+            have ls: "(\<Union>i\<in>{0..<n}. A i) \<in> lambda_system \<Omega> M f"
               using ls.UNION_in_sets by (simp add: A)
             hence eq_fa: "f a = f (a \<inter> (\<Union>i\<in>{0..<n}. A i)) + f (a - (\<Union>i\<in>{0..<n}. A i))"
               by (simp add: lambda_system_eq UNION_in)
@@ -437,9 +307,9 @@
             thus "(\<Sum>i<n. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
               by (simp add: lambda_system_strong_sum pos A disj eq_fa add_left_mono atLeast0LessThan[symmetric])
           next
-            have "\<And>i. a \<inter> A i \<in> sets M" using A'' by auto
+            have "\<And>i. a \<inter> A i \<in> M" using A'' by auto
             then show "\<And>i. 0 \<le> f (a \<inter> A i)" using pos[unfolded positive_def] by auto
-            have "\<And>i. a - (\<Union>i. A i) \<in> sets M" using A'' by auto
+            have "\<And>i. a - (\<Union>i. A i) \<in> M" using A'' by auto
             then have "\<And>i. 0 \<le> f (a - (\<Union>i. A i))" using pos[unfolded positive_def] by auto
             then show "f (a - (\<Union>i. A i)) \<noteq> -\<infinity>" by auto
           qed
@@ -460,66 +330,29 @@
 
 lemma (in sigma_algebra) caratheodory_lemma:
   assumes oms: "outer_measure_space M f"
-  shows "measure_space \<lparr> space = space M, sets = lambda_system M f, measure = f \<rparr>"
-    (is "measure_space ?M")
+  defines "L \<equiv> lambda_system \<Omega> M f"
+  shows "measure_space \<Omega> L f"
 proof -
   have pos: "positive M f"
     by (metis oms outer_measure_space_def)
-  have alg: "algebra ?M"
+  have alg: "algebra \<Omega> L"
     using lambda_system_algebra [of f, OF pos]
-    by (simp add: algebra_iff_Un)
+    by (simp add: algebra_iff_Un L_def)
   then
-  have "sigma_algebra ?M"
+  have "sigma_algebra \<Omega> L"
     using lambda_system_caratheodory [OF oms]
-    by (simp add: sigma_algebra_disjoint_iff)
+    by (simp add: sigma_algebra_disjoint_iff L_def)
   moreover
-  have "measure_space_axioms ?M"
+  have "countably_additive L f" "positive L f"
     using pos lambda_system_caratheodory [OF oms]
-    by (simp add: measure_space_axioms_def positive_def lambda_system_sets
-                  countably_additive_def o_def)
+    by (auto simp add: lambda_system_sets L_def countably_additive_def positive_def)
   ultimately
   show ?thesis
-    by (simp add: measure_space_def)
-qed
-
-lemma (in ring_of_sets) additive_increasing:
-  assumes posf: "positive M f" and addf: "additive M f"
-  shows "increasing M f"
-proof (auto simp add: increasing_def)
-  fix x y
-  assume xy: "x \<in> sets M" "y \<in> sets M" "x \<subseteq> y"
-  then have "y - x \<in> sets M" by auto
-  then have "0 \<le> f (y-x)" using posf[unfolded positive_def] by auto
-  then have "f x + 0 \<le> f x + f (y-x)" by (intro add_left_mono) auto
-  also have "... = f (x \<union> (y-x))" using addf
-    by (auto simp add: additive_def) (metis Diff_disjoint Un_Diff_cancel Diff xy(1,2))
-  also have "... = f y"
-    by (metis Un_Diff_cancel Un_absorb1 xy(3))
-  finally show "f x \<le> f y" by simp
-qed
-
-lemma (in ring_of_sets) countably_additive_additive:
-  assumes posf: "positive M f" and ca: "countably_additive M f"
-  shows "additive M f"
-proof (auto simp add: additive_def)
-  fix x y
-  assume x: "x \<in> sets M" and y: "y \<in> sets M" and "x \<inter> y = {}"
-  hence "disjoint_family (binaryset x y)"
-    by (auto simp add: disjoint_family_on_def binaryset_def)
-  hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
-         (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
-         f (\<Union>i. binaryset x y i) = (\<Sum> n. f (binaryset x y n))"
-    using ca
-    by (simp add: countably_additive_def)
-  hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
-         f (x \<union> y) = (\<Sum>n. f (binaryset x y n))"
-    by (simp add: range_binaryset_eq UN_binaryset_eq)
-  thus "f (x \<union> y) = f x + f y" using posf x y
-    by (auto simp add: Un suminf_binaryset_eq positive_def)
+    using pos by (simp add: measure_space_def)
 qed
 
 lemma inf_measure_nonempty:
-  assumes f: "positive M f" and b: "b \<in> sets M" and a: "a \<subseteq> b" "{} \<in> sets M"
+  assumes f: "positive M f" and b: "b \<in> M" and a: "a \<subseteq> b" "{} \<in> M"
   shows "f b \<in> measure_set M f a"
 proof -
   let ?A = "\<lambda>i::nat. (if i = 0 then b else {})"
@@ -534,14 +367,14 @@
 
 lemma (in ring_of_sets) inf_measure_agrees:
   assumes posf: "positive M f" and ca: "countably_additive M f"
-      and s: "s \<in> sets M"
+      and s: "s \<in> M"
   shows "Inf (measure_set M f s) = f s"
   unfolding Inf_ereal_def
 proof (safe intro!: Greatest_equality)
   fix z
   assume z: "z \<in> measure_set M f s"
   from this obtain A where
-    A: "range A \<subseteq> sets M" and disj: "disjoint_family A"
+    A: "range A \<subseteq> M" and disj: "disjoint_family A"
     and "s \<subseteq> (\<Union>x. A x)" and si: "(\<Sum>i. f (A i)) = z"
     by (auto simp add: measure_set_def comp_def)
   hence seq: "s = (\<Union>i. A i \<inter> s)" by blast
@@ -549,11 +382,11 @@
     by (metis additive_increasing ca countably_additive_additive posf)
   have sums: "(\<Sum>i. f (A i \<inter> s)) = f (\<Union>i. A i \<inter> s)"
     proof (rule ca[unfolded countably_additive_def, rule_format])
-      show "range (\<lambda>n. A n \<inter> s) \<subseteq> sets M" using A s
+      show "range (\<lambda>n. A n \<inter> s) \<subseteq> M" using A s
         by blast
       show "disjoint_family (\<lambda>n. A n \<inter> s)" using disj
         by (auto simp add: disjoint_family_on_def)
-      show "(\<Union>i. A i \<inter> s) \<in> sets M" using A s
+      show "(\<Union>i. A i \<inter> s) \<in> M" using A s
         by (metis UN_extend_simps(4) s seq)
     qed
   hence "f s = (\<Sum>i. f (A i \<inter> s))"
@@ -562,7 +395,7 @@
     proof (rule suminf_le_pos)
       fix n show "f (A n \<inter> s) \<le> f (A n)" using A s
         by (force intro: increasingD [OF inc])
-      fix N have "A N \<inter> s \<in> sets M"  using A s by auto
+      fix N have "A N \<inter> s \<in> M"  using A s by auto
       then show "0 \<le> f (A N \<inter> s)" using posf unfolding positive_def by auto
     qed
   also have "... = z" by (rule si)
@@ -578,7 +411,7 @@
   assumes posf: "positive M f" "r \<in> measure_set M f X"
   shows "0 \<le> r"
 proof -
-  obtain A where "range A \<subseteq> sets M" and r: "r = (\<Sum>i. f (A i))"
+  obtain A where "range A \<subseteq> M" and r: "r = (\<Sum>i. f (A i))"
     using `r \<in> measure_set M f X` unfolding measure_set_def by auto
   then show "0 \<le> r" using posf unfolding r positive_def
     by (intro suminf_0_le) auto
@@ -593,26 +426,25 @@
 qed
 
 lemma inf_measure_empty:
-  assumes posf: "positive M f" and "{} \<in> sets M"
+  assumes posf: "positive M f" and "{} \<in> M"
   shows "Inf (measure_set M f {}) = 0"
 proof (rule antisym)
   show "Inf (measure_set M f {}) \<le> 0"
-    by (metis complete_lattice_class.Inf_lower `{} \<in> sets M`
+    by (metis complete_lattice_class.Inf_lower `{} \<in> M`
               inf_measure_nonempty[OF posf] subset_refl posf[unfolded positive_def])
 qed (rule inf_measure_pos[OF posf])
 
 lemma (in ring_of_sets) inf_measure_positive:
-  assumes p: "positive M f" and "{} \<in> sets M"
+  assumes p: "positive M f" and "{} \<in> M"
   shows "positive M (\<lambda>x. Inf (measure_set M f x))"
 proof (unfold positive_def, intro conjI ballI)
   show "Inf (measure_set M f {}) = 0" using inf_measure_empty[OF assms] by auto
-  fix A assume "A \<in> sets M"
+  fix A assume "A \<in> M"
 qed (rule inf_measure_pos[OF p])
 
 lemma (in ring_of_sets) inf_measure_increasing:
   assumes posf: "positive M f"
-  shows "increasing \<lparr> space = space M, sets = Pow (space M) \<rparr>
-                    (\<lambda>x. Inf (measure_set M f x))"
+  shows "increasing (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
 apply (clarsimp simp add: increasing_def)
 apply (rule complete_lattice_class.Inf_greatest)
 apply (rule complete_lattice_class.Inf_lower)
@@ -621,13 +453,13 @@
 
 lemma (in ring_of_sets) inf_measure_le:
   assumes posf: "positive M f" and inc: "increasing M f"
-      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}"
+      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}"
   shows "Inf (measure_set M f s) \<le> x"
 proof -
-  obtain A where A: "range A \<subseteq> sets M" and ss: "s \<subseteq> (\<Union>i. A i)"
+  obtain A where A: "range A \<subseteq> M" and ss: "s \<subseteq> (\<Union>i. A i)"
              and xeq: "(\<Sum>i. f (A i)) = x"
     using x by auto
-  have dA: "range (disjointed A) \<subseteq> sets M"
+  have dA: "range (disjointed A) \<subseteq> M"
     by (metis A range_disjointed_sets)
   have "\<forall>n. f (disjointed A n) \<le> f (A n)"
     by (metis increasingD [OF inc] UNIV_I dA image_subset_iff disjointed_subset A comp_def)
@@ -648,8 +480,8 @@
 
 lemma (in ring_of_sets) inf_measure_close:
   fixes e :: ereal
-  assumes posf: "positive M f" and e: "0 < e" and ss: "s \<subseteq> (space M)" and "Inf (measure_set M f s) \<noteq> \<infinity>"
-  shows "\<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> s \<subseteq> (\<Union>i. A i) \<and>
+  assumes posf: "positive M f" and e: "0 < e" and ss: "s \<subseteq> (\<Omega>)" and "Inf (measure_set M f s) \<noteq> \<infinity>"
+  shows "\<exists>A. range A \<subseteq> M \<and> disjoint_family A \<and> s \<subseteq> (\<Union>i. A i) \<and>
                (\<Sum>i. f (A i)) \<le> Inf (measure_set M f s) + e"
 proof -
   from `Inf (measure_set M f s) \<noteq> \<infinity>` have fin: "\<bar>Inf (measure_set M f s)\<bar> \<noteq> \<infinity>"
@@ -662,22 +494,21 @@
 
 lemma (in ring_of_sets) inf_measure_countably_subadditive:
   assumes posf: "positive M f" and inc: "increasing M f"
-  shows "countably_subadditive (| space = space M, sets = Pow (space M) |)
-                  (\<lambda>x. Inf (measure_set M f x))"
+  shows "countably_subadditive (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
 proof (simp add: countably_subadditive_def, safe)
   fix A :: "nat \<Rightarrow> 'a set"
   let ?outer = "\<lambda>B. Inf (measure_set M f B)"
-  assume A: "range A \<subseteq> Pow (space M)"
+  assume A: "range A \<subseteq> Pow (\<Omega>)"
      and disj: "disjoint_family A"
-     and sb: "(\<Union>i. A i) \<subseteq> space M"
+     and sb: "(\<Union>i. A i) \<subseteq> \<Omega>"
 
   { fix e :: ereal assume e: "0 < e" and "\<forall>i. ?outer (A i) \<noteq> \<infinity>"
-    hence "\<exists>BB. \<forall>n. range (BB n) \<subseteq> sets M \<and> disjoint_family (BB n) \<and>
+    hence "\<exists>BB. \<forall>n. range (BB n) \<subseteq> M \<and> disjoint_family (BB n) \<and>
         A n \<subseteq> (\<Union>i. BB n i) \<and> (\<Sum>i. f (BB n i)) \<le> ?outer (A n) + e * (1/2)^(Suc n)"
       apply (safe intro!: choice inf_measure_close [of f, OF posf])
       using e sb by (auto simp: ereal_zero_less_0_iff one_ereal_def)
     then obtain BB
-      where BB: "\<And>n. (range (BB n) \<subseteq> sets M)"
+      where BB: "\<And>n. (range (BB n) \<subseteq> M)"
       and disjBB: "\<And>n. disjoint_family (BB n)"
       and sbBB: "\<And>n. A n \<subseteq> (\<Union>i. BB n i)"
       and BBle: "\<And>n. (\<Sum>i. f (BB n i)) \<le> ?outer (A n) + e * (1/2)^(Suc n)"
@@ -697,7 +528,7 @@
       finally show ?thesis .
     qed
     def C \<equiv> "(split BB) o prod_decode"
-    have C: "!!n. C n \<in> sets M"
+    have C: "!!n. C n \<in> M"
       apply (rule_tac p="prod_decode n" in PairE)
       apply (simp add: C_def)
       apply (metis BB subsetD rangeI)
@@ -744,9 +575,8 @@
 qed
 
 lemma (in ring_of_sets) inf_measure_outer:
-  "\<lbrakk> positive M f ; increasing M f \<rbrakk>
-   \<Longrightarrow> outer_measure_space \<lparr> space = space M, sets = Pow (space M) \<rparr>
-                          (\<lambda>x. Inf (measure_set M f x))"
+  "\<lbrakk> positive M f ; increasing M f \<rbrakk> \<Longrightarrow>
+    outer_measure_space (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
   using inf_measure_pos[of M f]
   by (simp add: outer_measure_space_def inf_measure_empty
                 inf_measure_increasing inf_measure_countably_subadditive positive_def)
@@ -754,14 +584,13 @@
 lemma (in ring_of_sets) algebra_subset_lambda_system:
   assumes posf: "positive M f" and inc: "increasing M f"
       and add: "additive M f"
-  shows "sets M \<subseteq> lambda_system \<lparr> space = space M, sets = Pow (space M) \<rparr>
-                                (\<lambda>x. Inf (measure_set M f x))"
+  shows "M \<subseteq> lambda_system \<Omega> (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
 proof (auto dest: sets_into_space
             simp add: algebra.lambda_system_eq [OF algebra_Pow])
   fix x s
-  assume x: "x \<in> sets M"
-     and s: "s \<subseteq> space M"
-  have [simp]: "!!x. x \<in> sets M \<Longrightarrow> s \<inter> (space M - x) = s-x" using s
+  assume x: "x \<in> M"
+     and s: "s \<subseteq> \<Omega>"
+  have [simp]: "!!x. x \<in> M \<Longrightarrow> s \<inter> (\<Omega> - x) = s-x" using s
     by blast
   have "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
         \<le> Inf (measure_set M f s)"
@@ -774,7 +603,7 @@
     show ?thesis
     proof (rule complete_lattice_class.Inf_greatest)
       fix r assume "r \<in> measure_set M f s"
-      then obtain A where A: "disjoint_family A" "range A \<subseteq> sets M" "s \<subseteq> (\<Union>i. A i)"
+      then obtain A where A: "disjoint_family A" "range A \<subseteq> M" "s \<subseteq> (\<Union>i. A i)"
         and r: "r = (\<Sum>i. f (A i))" unfolding measure_set_def by auto
       have "Inf (measure_set M f (s \<inter> x)) \<le> (\<Sum>i. f (A i \<inter> x))"
         unfolding measure_set_def
@@ -822,34 +651,31 @@
 qed
 
 lemma measure_down:
-  "measure_space N \<Longrightarrow> sigma_algebra M \<Longrightarrow> sets M \<subseteq> sets N \<Longrightarrow> measure N = measure M \<Longrightarrow> measure_space M"
-  by (simp add: measure_space_def measure_space_axioms_def positive_def
-                countably_additive_def)
+  "measure_space \<Omega> N \<mu> \<Longrightarrow> sigma_algebra \<Omega> M \<Longrightarrow> M \<subseteq> N \<Longrightarrow> measure_space \<Omega> M \<mu>"
+  by (simp add: measure_space_def positive_def countably_additive_def)
      blast
 
 theorem (in ring_of_sets) caratheodory:
   assumes posf: "positive M f" and ca: "countably_additive M f"
-  shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and>
-            measure_space \<lparr> space = space M, sets = sets (sigma M), measure = \<mu> \<rparr>"
+  shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> M. \<mu> s = f s) \<and> measure_space \<Omega> (sigma_sets \<Omega> M) \<mu>"
 proof -
   have inc: "increasing M f"
     by (metis additive_increasing ca countably_additive_additive posf)
   let ?infm = "(\<lambda>x. Inf (measure_set M f x))"
-  def ls \<equiv> "lambda_system (|space = space M, sets = Pow (space M)|) ?infm"
-  have mls: "measure_space \<lparr>space = space M, sets = ls, measure = ?infm\<rparr>"
+  def ls \<equiv> "lambda_system \<Omega> (Pow \<Omega>) ?infm"
+  have mls: "measure_space \<Omega> ls ?infm"
     using sigma_algebra.caratheodory_lemma
             [OF sigma_algebra_Pow  inf_measure_outer [OF posf inc]]
     by (simp add: ls_def)
-  hence sls: "sigma_algebra (|space = space M, sets = ls, measure = ?infm|)"
+  hence sls: "sigma_algebra \<Omega> ls"
     by (simp add: measure_space_def)
-  have "sets M \<subseteq> ls"
+  have "M \<subseteq> ls"
     by (simp add: ls_def)
        (metis ca posf inc countably_additive_additive algebra_subset_lambda_system)
-  hence sgs_sb: "sigma_sets (space M) (sets M) \<subseteq> ls"
-    using sigma_algebra.sigma_sets_subset [OF sls, of "sets M"]
+  hence sgs_sb: "sigma_sets (\<Omega>) (M) \<subseteq> ls"
+    using sigma_algebra.sigma_sets_subset [OF sls, of "M"]
     by simp
-  have "measure_space \<lparr> space = space M, sets = sets (sigma M), measure = ?infm \<rparr>"
-    unfolding sigma_def
+  have "measure_space \<Omega> (sigma_sets \<Omega> M) ?infm"
     by (rule measure_down [OF mls], rule sigma_algebra_sigma_sets)
        (simp_all add: sgs_sb space_closed)
   thus ?thesis using inf_measure_agrees [OF posf ca]
@@ -861,12 +687,12 @@
 lemma (in ring_of_sets) countably_additive_iff_continuous_from_below:
   assumes f: "positive M f" "additive M f"
   shows "countably_additive M f \<longleftrightarrow>
-    (\<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))"
+    (\<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))"
   unfolding countably_additive_def
 proof safe
-  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)"
-  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets M" "incseq A" "(\<Union>i. A i) \<in> sets M"
-  then have dA: "range (disjointed A) \<subseteq> sets M" by (auto simp: range_disjointed_sets)
+  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)"
+  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
+  then have dA: "range (disjointed A) \<subseteq> M" by (auto simp: range_disjointed_sets)
   with count_sum[THEN spec, of "disjointed A"] A(3)
   have f_UN: "(\<Sum>i. f (disjointed A i)) = f (\<Union>i. A i)"
     by (auto simp: UN_disjointed_eq disjoint_family_disjointed)
@@ -880,12 +706,12 @@
     using disjointed_additive[OF f A(1,2)] .
   ultimately show "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)" by simp
 next
-  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)"
-  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets M" "disjoint_family A" "(\<Union>i. A i) \<in> sets M"
+  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)"
+  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "disjoint_family A" "(\<Union>i. A i) \<in> M"
   have *: "(\<Union>n. (\<Union>i\<le>n. A i)) = (\<Union>i. A i)" by auto
   have "(\<lambda>n. f (\<Union>i\<le>n. A i)) ----> f (\<Union>i. A i)"
   proof (unfold *[symmetric], intro cont[rule_format])
-    show "range (\<lambda>i. \<Union> i\<le>i. A i) \<subseteq> sets M" "(\<Union>i. \<Union> i\<le>i. A i) \<in> sets M"
+    show "range (\<lambda>i. \<Union> i\<le>i. A i) \<subseteq> M" "(\<Union>i. \<Union> i\<le>i. A i) \<in> M"
       using A * by auto
   qed (force intro!: incseq_SucI)
   moreover have "\<And>n. f (\<Union>i\<le>n. A i) = (\<Sum>i\<le>n. f (A i))"
@@ -901,18 +727,18 @@
 
 lemma (in ring_of_sets) continuous_from_above_iff_empty_continuous:
   assumes f: "positive M f" "additive M f"
-  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))
-     \<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)"
+  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))
+     \<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)"
 proof safe
-  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))"
-  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>"
+  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))"
+  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>"
   with cont[THEN spec, of A] show "(\<lambda>i. f (A i)) ----> 0"
     using `positive M f`[unfolded positive_def] by auto
 next
-  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"
-  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>"
+  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"
+  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>"
 
-  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"
+  have f_mono: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<subseteq> b \<Longrightarrow> f a \<le> f b"
     using additive_increasing[OF f] unfolding increasing_def by simp
 
   have decseq_fA: "decseq (\<lambda>i. f (A i))"
@@ -932,7 +758,7 @@
   note f_fin = this
   have "(\<lambda>i. f (A i - (\<Inter>i. A i))) ----> 0"
   proof (intro cont[rule_format, OF _ decseq _ f_fin])
-    show "range (\<lambda>i. A i - (\<Inter>i. A i)) \<subseteq> sets M" "(\<Inter>i. A i - (\<Inter>i. A i)) = {}"
+    show "range (\<lambda>i. A i - (\<Inter>i. A i)) \<subseteq> M" "(\<Inter>i. A i - (\<Inter>i. A i)) = {}"
       using A by auto
   qed
   from INF_Lim_ereal[OF decseq_f this]
@@ -956,17 +782,17 @@
 qed
 
 lemma positiveD1: "positive M f \<Longrightarrow> f {} = 0" by (auto simp: positive_def)
-lemma positiveD2: "positive M f \<Longrightarrow> A \<in> sets M \<Longrightarrow> 0 \<le> f A" by (auto simp: positive_def)
+lemma positiveD2: "positive M f \<Longrightarrow> A \<in> M \<Longrightarrow> 0 \<le> f A" by (auto simp: positive_def)
 
 lemma (in ring_of_sets) empty_continuous_imp_continuous_from_below:
-  assumes f: "positive M f" "additive M f" "\<forall>A\<in>sets M. f A \<noteq> \<infinity>"
-  assumes cont: "\<forall>A. range A \<subseteq> sets M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<lambda>i. f (A i)) ----> 0"
-  assumes A: "range A \<subseteq> sets M" "incseq A" "(\<Union>i. A i) \<in> sets M"
+  assumes f: "positive M f" "additive M f" "\<forall>A\<in>M. f A \<noteq> \<infinity>"
+  assumes cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<lambda>i. f (A i)) ----> 0"
+  assumes A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
   shows "(\<lambda>i. f (A i)) ----> f (\<Union>i. A i)"
 proof -
-  have "\<forall>A\<in>sets M. \<exists>x. f A = ereal x"
+  have "\<forall>A\<in>M. \<exists>x. f A = ereal x"
   proof
-    fix A assume "A \<in> sets M" with f show "\<exists>x. f A = ereal x"
+    fix A assume "A \<in> M" with f show "\<exists>x. f A = ereal x"
       unfolding positive_def by (cases "f A") auto
   qed
   from bchoice[OF this] guess f' .. note f' = this[rule_format]
@@ -991,20 +817,19 @@
 qed
 
 lemma (in ring_of_sets) empty_continuous_imp_countably_additive:
-  assumes f: "positive M f" "additive M f" and fin: "\<forall>A\<in>sets M. f A \<noteq> \<infinity>"
-  assumes cont: "\<And>A. range A \<subseteq> sets M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
+  assumes f: "positive M f" "additive M f" and fin: "\<forall>A\<in>M. f A \<noteq> \<infinity>"
+  assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   shows "countably_additive M f"
   using countably_additive_iff_continuous_from_below[OF f]
   using empty_continuous_imp_continuous_from_below[OF f fin] cont
   by blast
 
 lemma (in ring_of_sets) caratheodory_empty_continuous:
-  assumes f: "positive M f" "additive M f" and fin: "\<And>A. A \<in> sets M \<Longrightarrow> f A \<noteq> \<infinity>"
-  assumes cont: "\<And>A. range A \<subseteq> sets M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
-  shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and>
-            measure_space \<lparr> space = space M, sets = sets (sigma M), measure = \<mu> \<rparr>"
+  assumes f: "positive M f" "additive M f" and fin: "\<And>A. A \<in> M \<Longrightarrow> f A \<noteq> \<infinity>"
+  assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
+  shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> M. \<mu> s = f s) \<and> measure_space \<Omega> (sigma_sets \<Omega> M) \<mu>"
 proof (intro caratheodory empty_continuous_imp_countably_additive f)
-  show "\<forall>A\<in>sets M. f A \<noteq> \<infinity>" using fin by auto
+  show "\<forall>A\<in>M. f A \<noteq> \<infinity>" using fin by auto
 qed (rule cont)
 
 end
--- a/src/HOL/Probability/Complete_Measure.thy	Mon Apr 23 12:23:23 2012 +0100
+++ b/src/HOL/Probability/Complete_Measure.thy	Mon Apr 23 12:14:35 2012 +0200
@@ -6,218 +6,231 @@
 imports Lebesgue_Integration
 begin
 
-locale completeable_measure_space = measure_space
-
-definition (in completeable_measure_space)
-  "split_completion A p = (\<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)"
-
-definition (in completeable_measure_space)
-  "main_part A = fst (Eps (split_completion A))"
+definition
+  "split_completion M A p = (if A \<in> sets M then p = (A, {}) else
+   \<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)"
 
-definition (in completeable_measure_space)
-  "null_part A = snd (Eps (split_completion A))"
-
-abbreviation (in completeable_measure_space) "\<mu>' A \<equiv> \<mu> (main_part A)"
+definition
+  "main_part M A = fst (Eps (split_completion M A))"
 
-definition (in completeable_measure_space) completion :: "('a, 'b) measure_space_scheme" where
-  "completion = \<lparr> space = space M,
-                  sets = { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets \<and> N \<subseteq> N' },
-                  measure = \<mu>',
-                  \<dots> = more M \<rparr>"
-
+definition
+  "null_part M A = snd (Eps (split_completion M A))"
 
-lemma (in completeable_measure_space) space_completion[simp]:
-  "space completion = space M" unfolding completion_def by simp
-
-lemma (in completeable_measure_space) sets_completionE:
-  assumes "A \<in> sets completion"
-  obtains S N N' where "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets" "S \<in> sets M"
-  using assms unfolding completion_def by auto
+definition completion :: "'a measure \<Rightarrow> 'a measure" where
+  "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' }
+    (emeasure M \<circ> main_part M)"
 
-lemma (in completeable_measure_space) sets_completionI:
-  assumes "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets" "S \<in> sets M"
-  shows "A \<in> sets completion"
-  using assms unfolding completion_def by auto
+lemma completion_into_space:
+  "{ S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' } \<subseteq> Pow (space M)"
+  using sets_into_space by auto
 
-lemma (in completeable_measure_space) sets_completionI_sets[intro]:
-  "A \<in> sets M \<Longrightarrow> A \<in> sets completion"
-  unfolding completion_def by force
+lemma space_completion[simp]: "space (completion M) = space M"
+  unfolding completion_def using space_measure_of[OF completion_into_space] by simp
 
-lemma (in completeable_measure_space) null_sets_completion:
-  assumes "N' \<in> null_sets" "N \<subseteq> N'" shows "N \<in> sets completion"
-  apply(rule sets_completionI[of N "{}" N N'])
+lemma completionI:
+  assumes "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets M" "S \<in> sets M"
+  shows "A \<in> { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' }"
   using assms by auto
 
-sublocale completeable_measure_space \<subseteq> completion!: sigma_algebra completion
-proof (unfold sigma_algebra_iff2, safe)
-  fix A x assume "A \<in> sets completion" "x \<in> A"
-  with sets_into_space show "x \<in> space completion"
-    by (auto elim!: sets_completionE)
+lemma completionE:
+  assumes "A \<in> { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' }"
+  obtains S N N' where "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets M" "S \<in> sets M"
+  using assms by auto
+
+lemma sigma_algebra_completion:
+  "sigma_algebra (space M) { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' }"
+    (is "sigma_algebra _ ?A")
+  unfolding sigma_algebra_iff2
+proof (intro conjI ballI allI impI)
+  show "?A \<subseteq> Pow (space M)"
+    using sets_into_space by auto
 next
-  fix A assume "A \<in> sets completion"
-  from this[THEN sets_completionE] guess S N N' . note A = this
-  let ?C = "space completion"
-  show "?C - A \<in> sets completion" using A
-    by (intro sets_completionI[of _ "(?C - S) \<inter> (?C - N')" "(?C - S) \<inter> N' \<inter> (?C - N)"])
-       auto
+  show "{} \<in> ?A" by auto
 next
-  fix A ::"nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets completion"
-  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'"
-    unfolding completion_def by (auto simp: image_subset_iff)
+  let ?C = "space M"
+  fix A assume "A \<in> ?A" from completionE[OF this] guess S N N' .
+  then show "space M - A \<in> ?A"
+    by (intro completionI[of _ "(?C - S) \<inter> (?C - N')" "(?C - S) \<inter> N' \<inter> (?C - N)"]) auto
+next
+  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> ?A"
+  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'"
+    by (auto simp: image_subset_iff)
   from choice[OF this] guess S ..
   from choice[OF this] guess N ..
   from choice[OF this] guess N' ..
-  then show "UNION UNIV A \<in> sets completion"
+  then show "UNION UNIV A \<in> ?A"
     using null_sets_UN[of N']
-    by (intro sets_completionI[of _ "UNION UNIV S" "UNION UNIV N" "UNION UNIV N'"])
-       auto
-qed auto
+    by (intro completionI[of _ "UNION UNIV S" "UNION UNIV N" "UNION UNIV N'"]) auto
+qed
+
+lemma sets_completion:
+  "sets (completion M) = { S \<union> N |S N N'. S \<in> sets M \<and> N' \<in> null_sets M \<and> N \<subseteq> N' }"
+  using sigma_algebra.sets_measure_of_eq[OF sigma_algebra_completion] by (simp add: completion_def)
+
+lemma sets_completionE:
+  assumes "A \<in> sets (completion M)"
+  obtains S N N' where "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets M" "S \<in> sets M"
+  using assms unfolding sets_completion by auto
+
+lemma sets_completionI:
+  assumes "A = S \<union> N" "N \<subseteq> N'" "N' \<in> null_sets M" "S \<in> sets M"
+  shows "A \<in> sets (completion M)"
+  using assms unfolding sets_completion by auto
+
+lemma sets_completionI_sets[intro, simp]:
+  "A \<in> sets M \<Longrightarrow> A \<in> sets (completion M)"
+  unfolding sets_completion by force
 
-lemma (in completeable_measure_space) split_completion:
-  assumes "A \<in> sets completion"
-  shows "split_completion A (main_part A, null_part A)"
-  unfolding main_part_def null_part_def
-proof (rule someI2_ex)
-  from assms[THEN sets_completionE] guess S N N' . note A = this
-  let ?P = "(S, N - S)"
-  show "\<exists>p. split_completion A p"
-    unfolding split_completion_def using A
-  proof (intro exI conjI)
-    show "A = fst ?P \<union> snd ?P" using A by auto
-    show "snd ?P \<subseteq> N'" using A by auto
+lemma null_sets_completion:
+  assumes "N' \<in> null_sets M" "N \<subseteq> N'" shows "N \<in> sets (completion M)"
+  using assms by (intro sets_completionI[of N "{}" N N']) auto
+
+lemma split_completion:
+  assumes "A \<in> sets (completion M)"
+  shows "split_completion M A (main_part M A, null_part M A)"
+proof cases
+  assume "A \<in> sets M" then show ?thesis
+    by (simp add: split_completion_def[abs_def] main_part_def null_part_def)
+next
+  assume nA: "A \<notin> sets M"
+  show ?thesis
+    unfolding main_part_def null_part_def if_not_P[OF nA]
+  proof (rule someI2_ex)
+    from assms[THEN sets_completionE] guess S N N' . note A = this
+    let ?P = "(S, N - S)"
+    show "\<exists>p. split_completion M A p"
+      unfolding split_completion_def if_not_P[OF nA] using A
+    proof (intro exI conjI)
+      show "A = fst ?P \<union> snd ?P" using A by auto
+      show "snd ?P \<subseteq> N'" using A by auto
+   qed auto
   qed auto
-qed auto
+qed
 
-lemma (in completeable_measure_space)
-  assumes "S \<in> sets completion"
-  shows main_part_sets[intro, simp]: "main_part S \<in> sets M"
-    and main_part_null_part_Un[simp]: "main_part S \<union> null_part S = S"
-    and main_part_null_part_Int[simp]: "main_part S \<inter> null_part S = {}"
-  using split_completion[OF assms] by (auto simp: split_completion_def)
+lemma
+  assumes "S \<in> sets (completion M)"
+  shows main_part_sets[intro, simp]: "main_part M S \<in> sets M"
+    and main_part_null_part_Un[simp]: "main_part M S \<union> null_part M S = S"
+    and main_part_null_part_Int[simp]: "main_part M S \<inter> null_part M S = {}"
+  using split_completion[OF assms]
+  by (auto simp: split_completion_def split: split_if_asm)
 
-lemma (in completeable_measure_space) null_part:
-  assumes "S \<in> sets completion" shows "\<exists>N. N\<in>null_sets \<and> null_part S \<subseteq> N"
-  using split_completion[OF assms] by (auto simp: split_completion_def)
+lemma main_part[simp]: "S \<in> sets M \<Longrightarrow> main_part M S = S"
+  using split_completion[of S M]
+  by (auto simp: split_completion_def split: split_if_asm)
 
-lemma (in completeable_measure_space) null_part_sets[intro, simp]:
-  assumes "S \<in> sets M" shows "null_part S \<in> sets M" "\<mu> (null_part S) = 0"
+lemma null_part:
+  assumes "S \<in> sets (completion M)" shows "\<exists>N. N\<in>null_sets M \<and> null_part M S \<subseteq> N"
+  using split_completion[OF assms] by (auto simp: split_completion_def split: split_if_asm)
+
+lemma null_part_sets[intro, simp]:
+  assumes "S \<in> sets M" shows "null_part M S \<in> sets M" "emeasure M (null_part M S) = 0"
 proof -
-  have S: "S \<in> sets completion" using assms by auto
-  have "S - main_part S \<in> sets M" using assms by auto
+  have S: "S \<in> sets (completion M)" using assms by auto
+  have "S - main_part M S \<in> sets M" using assms by auto
   moreover
   from main_part_null_part_Un[OF S] main_part_null_part_Int[OF S]
-  have "S - main_part S = null_part S" by auto
-  ultimately show sets: "null_part S \<in> sets M" by auto
+  have "S - main_part M S = null_part M S" by auto
+  ultimately show sets: "null_part M S \<in> sets M" by auto
   from null_part[OF S] guess N ..
-  with measure_eq_0[of N "null_part S"] sets
-  show "\<mu> (null_part S) = 0" by auto
-qed
-
-lemma (in completeable_measure_space) \<mu>'_set[simp]:
-  assumes "S \<in> sets M" shows "\<mu>' S = \<mu> S"
-proof -
-  have S: "S \<in> sets completion" using assms by auto
-  then have "\<mu> S = \<mu> (main_part S \<union> null_part S)" by simp
-  also have "\<dots> = \<mu>' S"
-    using S assms measure_additive[of "main_part S" "null_part S"]
-    by (auto simp: measure_additive)
-  finally show ?thesis by simp
+  with emeasure_eq_0[of N _ "null_part M S"] sets
+  show "emeasure M (null_part M S) = 0" by auto
 qed
 
-lemma (in completeable_measure_space) sets_completionI_sub:
-  assumes N: "N' \<in> null_sets" "N \<subseteq> N'"
-  shows "N \<in> sets completion"
-  using assms by (intro sets_completionI[of _ "{}" N N']) auto
-
-lemma (in completeable_measure_space) \<mu>_main_part_UN:
+lemma emeasure_main_part_UN:
   fixes S :: "nat \<Rightarrow> 'a set"
-  assumes "range S \<subseteq> sets completion"
-  shows "\<mu>' (\<Union>i. (S i)) = \<mu> (\<Union>i. main_part (S i))"
+  assumes "range S \<subseteq> sets (completion M)"
+  shows "emeasure M (main_part M (\<Union>i. (S i))) = emeasure M (\<Union>i. main_part M (S i))"
 proof -
-  have S: "\<And>i. S i \<in> sets completion" using assms by auto
-  then have UN: "(\<Union>i. S i) \<in> sets completion" by auto
-  have "\<forall>i. \<exists>N. N \<in> null_sets \<and> null_part (S i) \<subseteq> N"
+  have S: "\<And>i. S i \<in> sets (completion M)" using assms by auto
+  then have UN: "(\<Union>i. S i) \<in> sets (completion M)" by auto
+  have "\<forall>i. \<exists>N. N \<in> null_sets M \<and> null_part M (S i) \<subseteq> N"
     using null_part[OF S] by auto
   from choice[OF this] guess N .. note N = this
-  then have UN_N: "(\<Union>i. N i) \<in> null_sets" by (intro null_sets_UN) auto
-  have "(\<Union>i. S i) \<in> sets completion" using S by auto
+  then have UN_N: "(\<Union>i. N i) \<in> null_sets M" by (intro null_sets_UN) auto
+  have "(\<Union>i. S i) \<in> sets (completion M)" using S by auto
   from null_part[OF this] guess N' .. note N' = this
   let ?N = "(\<Union>i. N i) \<union> N'"
-  have null_set: "?N \<in> null_sets" using N' UN_N by (intro nullsets.Un) auto
-  have "main_part (\<Union>i. S i) \<union> ?N = (main_part (\<Union>i. S i) \<union> null_part (\<Union>i. S i)) \<union> ?N"
+  have null_set: "?N \<in> null_sets M" using N' UN_N by (intro null_sets.Un) auto
+  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"
     using N' by auto
-  also have "\<dots> = (\<Union>i. main_part (S i) \<union> null_part (S i)) \<union> ?N"
+  also have "\<dots> = (\<Union>i. main_part M (S i) \<union> null_part M (S i)) \<union> ?N"
     unfolding main_part_null_part_Un[OF S] main_part_null_part_Un[OF UN] by auto
-  also have "\<dots> = (\<Union>i. main_part (S i)) \<union> ?N"
+  also have "\<dots> = (\<Union>i. main_part M (S i)) \<union> ?N"
     using N by auto
-  finally have *: "main_part (\<Union>i. S i) \<union> ?N = (\<Union>i. main_part (S i)) \<union> ?N" .
-  have "\<mu> (main_part (\<Union>i. S i)) = \<mu> (main_part (\<Union>i. S i) \<union> ?N)"
-    using null_set UN by (intro measure_Un_null_set[symmetric]) auto
-  also have "\<dots> = \<mu> ((\<Union>i. main_part (S i)) \<union> ?N)"
+  finally have *: "main_part M (\<Union>i. S i) \<union> ?N = (\<Union>i. main_part M (S i)) \<union> ?N" .
+  have "emeasure M (main_part M (\<Union>i. S i)) = emeasure M (main_part M (\<Union>i. S i) \<union> ?N)"
+    using null_set UN by (intro emeasure_Un_null_set[symmetric]) auto
+  also have "\<dots> = emeasure M ((\<Union>i. main_part M (S i)) \<union> ?N)"
     unfolding * ..
-  also have "\<dots> = \<mu> (\<Union>i. main_part (S i))"
-    using null_set S by (intro measure_Un_null_set) auto
+  also have "\<dots> = emeasure M (\<Union>i. main_part M (S i))"
+    using null_set S by (intro emeasure_Un_null_set) auto
   finally show ?thesis .
 qed
 
-lemma (in completeable_measure_space) \<mu>_main_part_Un:
-  assumes S: "S \<in> sets completion" and T: "T \<in> sets completion"
-  shows "\<mu>' (S \<union> T) = \<mu> (main_part S \<union> main_part T)"
-proof -
-  have UN: "(\<Union>i. binary (main_part S) (main_part T) i) = (\<Union>i. main_part (binary S T i))"
-    unfolding binary_def by (auto split: split_if_asm)
-  show ?thesis
-    using \<mu>_main_part_UN[of "binary S T"] assms
-    unfolding range_binary_eq Un_range_binary UN by auto
+lemma emeasure_completion[simp]:
+  assumes S: "S \<in> sets (completion M)" shows "emeasure (completion M) S = emeasure M (main_part M S)"
+proof (subst emeasure_measure_of[OF completion_def completion_into_space])
+  let ?\<mu> = "emeasure M \<circ> main_part M"
+  show "S \<in> sets (completion M)" "?\<mu> S = emeasure M (main_part M S) " using S by simp_all
+  show "positive (sets (completion M)) ?\<mu>"
+    by (simp add: positive_def emeasure_nonneg)
+  show "countably_additive (sets (completion M)) ?\<mu>"
+  proof (intro countably_additiveI)
+    fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets (completion M)" "disjoint_family A"
+    have "disjoint_family (\<lambda>i. main_part M (A i))"
+    proof (intro disjoint_family_on_bisimulation[OF A(2)])
+      fix n m assume "A n \<inter> A m = {}"
+      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)) = {}"
+        using A by (subst (1 2) main_part_null_part_Un) auto
+      then show "main_part M (A n) \<inter> main_part M (A m) = {}" by auto
+    qed
+    then have "(\<Sum>n. emeasure M (main_part M (A n))) = emeasure M (\<Union>i. main_part M (A i))"
+      using A by (auto intro!: suminf_emeasure)
+    then show "(\<Sum>n. ?\<mu> (A n)) = ?\<mu> (UNION UNIV A)"
+      by (simp add: completion_def emeasure_main_part_UN[OF A(1)])
+  qed
 qed
 
-sublocale completeable_measure_space \<subseteq> completion!: measure_space completion
-  where "measure completion = \<mu>'"
-proof -
-  show "measure_space completion"
-  proof
-    show "positive completion (measure completion)"
-      by (auto simp: completion_def positive_def)
-  next
-    show "countably_additive completion (measure completion)"
-    proof (intro countably_additiveI)
-      fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets completion" "disjoint_family A"
-      have "disjoint_family (\<lambda>i. main_part (A i))"
-      proof (intro disjoint_family_on_bisimulation[OF A(2)])
-        fix n m assume "A n \<inter> A m = {}"
-        then have "(main_part (A n) \<union> null_part (A n)) \<inter> (main_part (A m) \<union> null_part (A m)) = {}"
-          using A by (subst (1 2) main_part_null_part_Un) auto
-        then show "main_part (A n) \<inter> main_part (A m) = {}" by auto
-      qed
-      then have "(\<Sum>n. measure completion (A n)) = \<mu> (\<Union>i. main_part (A i))"
-        unfolding completion_def using A by (auto intro!: measure_countably_additive)
-      then show "(\<Sum>n. measure completion (A n)) = measure completion (UNION UNIV A)"
-        by (simp add: completion_def \<mu>_main_part_UN[OF A(1)])
-    qed
-  qed
-  show "measure completion = \<mu>'" unfolding completion_def by simp
-qed
+lemma emeasure_completion_UN:
+  "range S \<subseteq> sets (completion M) \<Longrightarrow>
+    emeasure (completion M) (\<Union>i::nat. (S i)) = emeasure M (\<Union>i. main_part M (S i))"
+  by (subst emeasure_completion) (auto simp add: emeasure_main_part_UN)
 
-lemma (in completeable_measure_space) completion_ex_simple_function:
-  assumes f: "simple_function completion f"
-  shows "\<exists>f'. simple_function M f' \<and> (AE x. f x = f' x)"
+lemma emeasure_completion_Un:
+  assumes S: "S \<in> sets (completion M)" and T: "T \<in> sets (completion M)"
+  shows "emeasure (completion M) (S \<union> T) = emeasure M (main_part M S \<union> main_part M T)"
+proof (subst emeasure_completion)
+  have UN: "(\<Union>i. binary (main_part M S) (main_part M T) i) = (\<Union>i. main_part M (binary S T i))"
+    unfolding binary_def by (auto split: split_if_asm)
+  show "emeasure M (main_part M (S \<union> T)) = emeasure M (main_part M S \<union> main_part M T)"
+    using emeasure_main_part_UN[of "binary S T" M] assms
+    unfolding range_binary_eq Un_range_binary UN by auto
+qed (auto intro: S T)
+
+lemma sets_completionI_sub:
+  assumes N: "N' \<in> null_sets M" "N \<subseteq> N'"
+  shows "N \<in> sets (completion M)"
+  using assms by (intro sets_completionI[of _ "{}" N N']) auto
+
+lemma completion_ex_simple_function:
+  assumes f: "simple_function (completion M) f"
+  shows "\<exists>f'. simple_function M f' \<and> (AE x in M. f x = f' x)"
 proof -
   let ?F = "\<lambda>x. f -` {x} \<inter> space M"
-  have F: "\<And>x. ?F x \<in> sets completion" and fin: "finite (f`space M)"
-    using completion.simple_functionD[OF f]
-      completion.simple_functionD[OF f] by simp_all
-  have "\<forall>x. \<exists>N. N \<in> null_sets \<and> null_part (?F x) \<subseteq> N"
+  have F: "\<And>x. ?F x \<in> sets (completion M)" and fin: "finite (f`space M)"
+    using simple_functionD[OF f] simple_functionD[OF f] by simp_all
+  have "\<forall>x. \<exists>N. N \<in> null_sets M \<and> null_part M (?F x) \<subseteq> N"
     using F null_part by auto
   from choice[OF this] obtain N where
-    N: "\<And>x. null_part (?F x) \<subseteq> N x" "\<And>x. N x \<in> null_sets" by auto
+    N: "\<And>x. null_part M (?F x) \<subseteq> N x" "\<And>x. N x \<in> null_sets M" by auto
   let ?N = "\<Union>x\<in>f`space M. N x"
   let ?f' = "\<lambda>x. if x \<in> ?N then undefined else f x"
-  have sets: "?N \<in> null_sets" using N fin by (intro nullsets.finite_UN) auto
+  have sets: "?N \<in> null_sets M" using N fin by (intro null_sets.finite_UN) auto
   show ?thesis unfolding simple_function_def
   proof (safe intro!: exI[of _ ?f'])
     have "?f' ` space M \<subseteq> f`space M \<union> {undefined}" by auto
-    from finite_subset[OF this] completion.simple_functionD(1)[OF f]
+    from finite_subset[OF this] simple_functionD(1)[OF f]
     show "finite (?f' ` space M)" by auto
   next
     fix x assume "x \<in> space M"
@@ -225,13 +238,13 @@
       (if x \<in> ?N then ?F undefined \<union> ?N
        else if f x = undefined then ?F (f x) \<union> ?N
        else ?F (f x) - ?N)"
-      using N(2) sets_into_space by (auto split: split_if_asm)
+      using N(2) sets_into_space by (auto split: split_if_asm simp: null_sets_def)
     moreover { fix y have "?F y \<union> ?N \<in> sets M"
       proof cases
         assume y: "y \<in> f`space M"
-        have "?F y \<union> ?N = (main_part (?F y) \<union> null_part (?F y)) \<union> ?N"
+        have "?F y \<union> ?N = (main_part M (?F y) \<union> null_part M (?F y)) \<union> ?N"
           using main_part_null_part_Un[OF F] by auto
-        also have "\<dots> = main_part (?F y) \<union> ?N"
+        also have "\<dots> = main_part M (?F y) \<union> ?N"
           using y N by auto
         finally show ?thesis
           using F sets by auto
@@ -240,34 +253,34 @@
         then show ?thesis using sets by auto
       qed }
     moreover {
-      have "?F (f x) - ?N = main_part (?F (f x)) \<union> null_part (?F (f x)) - ?N"
+      have "?F (f x) - ?N = main_part M (?F (f x)) \<union> null_part M (?F (f x)) - ?N"
         using main_part_null_part_Un[OF F] by auto
-      also have "\<dots> = main_part (?F (f x)) - ?N"
+      also have "\<dots> = main_part M (?F (f x)) - ?N"
         using N `x \<in> space M` by auto
       finally have "?F (f x) - ?N \<in> sets M"
         using F sets by auto }
     ultimately show "?f' -` {?f' x} \<inter> space M \<in> sets M" by auto
   next
-    show "AE x. f x = ?f' x"
+    show "AE x in M. f x = ?f' x"
       by (rule AE_I', rule sets) auto
   qed
 qed
 
-lemma (in completeable_measure_space) completion_ex_borel_measurable_pos:
+lemma completion_ex_borel_measurable_pos:
   fixes g :: "'a \<Rightarrow> ereal"
-  assumes g: "g \<in> borel_measurable completion" and "\<And>x. 0 \<le> g x"
-  shows "\<exists>g'\<in>borel_measurable M. (AE x. g x = g' x)"
+  assumes g: "g \<in> borel_measurable (completion M)" and "\<And>x. 0 \<le> g x"
+  shows "\<exists>g'\<in>borel_measurable M. (AE x in M. g x = g' x)"
 proof -
-  from g[THEN completion.borel_measurable_implies_simple_function_sequence'] guess f . note f = this
+  from g[THEN borel_measurable_implies_simple_function_sequence'] guess f . note f = this
   from this(1)[THEN completion_ex_simple_function]
-  have "\<forall>i. \<exists>f'. simple_function M f' \<and> (AE x. f i x = f' x)" ..
+  have "\<forall>i. \<exists>f'. simple_function M f' \<and> (AE x in M. f i x = f' x)" ..
   from this[THEN choice] obtain f' where
     sf: "\<And>i. simple_function M (f' i)" and
-    AE: "\<forall>i. AE x. f i x = f' i x" by auto
+    AE: "\<forall>i. AE x in M. f i x = f' i x" by auto
   show ?thesis
   proof (intro bexI)
     from AE[unfolded AE_all_countable[symmetric]]
-    show "AE x. g x = (SUP i. f' i x)" (is "AE x. g x = ?f x")
+    show "AE x in M. g x = (SUP i. f' i x)" (is "AE x in M. g x = ?f x")
     proof (elim AE_mp, safe intro!: AE_I2)
       fix x assume eq: "\<forall>i. f i x = f' i x"
       moreover have "g x = (SUP i. f i x)"
@@ -279,20 +292,20 @@
   qed
 qed
 
-lemma (in completeable_measure_space) completion_ex_borel_measurable:
+lemma completion_ex_borel_measurable:
   fixes g :: "'a \<Rightarrow> ereal"
-  assumes g: "g \<in> borel_measurable completion"
-  shows "\<exists>g'\<in>borel_measurable M. (AE x. g x = g' x)"
+  assumes g: "g \<in> borel_measurable (completion M)"
+  shows "\<exists>g'\<in>borel_measurable M. (AE x in M. g x = g' x)"
 proof -
-  have "(\<lambda>x. max 0 (g x)) \<in> borel_measurable completion" "\<And>x. 0 \<le> max 0 (g x)" using g by auto
+  have "(\<lambda>x. max 0 (g x)) \<in> borel_measurable (completion M)" "\<And>x. 0 \<le> max 0 (g x)" using g by auto
   from completion_ex_borel_measurable_pos[OF this] guess g_pos ..
   moreover
-  have "(\<lambda>x. max 0 (- g x)) \<in> borel_measurable completion" "\<And>x. 0 \<le> max 0 (- g x)" using g by auto
+  have "(\<lambda>x. max 0 (- g x)) \<in> borel_measurable (completion M)" "\<And>x. 0 \<le> max 0 (- g x)" using g by auto
   from completion_ex_borel_measurable_pos[OF this] guess g_neg ..
   ultimately
   show ?thesis
   proof (safe intro!: bexI[of _ "\<lambda>x. g_pos x - g_neg x"])
-    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"
+    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"
     proof (intro AE_I2 impI)
       fix x assume g: "max 0 (- g x) = g_neg x" "max 0 (g x) = g_pos x"
       show "g x = g_pos x - g_neg x" unfolding g[symmetric]
--- a/src/HOL/Probability/Conditional_Probability.thy	Mon Apr 23 12:23:23 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,161 +0,0 @@
-(*  Title:      HOL/Probability/Conditional_Probability.thy
-    Author:     Johannes Hölzl, TU München
-*)
-
-header {*Conditional probability*}
-
-theory Conditional_Probability
-imports Probability_Measure Radon_Nikodym
-begin
-
-section "Conditional Expectation and Probability"
-
-definition (in prob_space)
-  "conditional_expectation N X = (SOME Y. Y\<in>borel_measurable N \<and> (\<forall>x. 0 \<le> Y x)
-    \<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)))"
-
-lemma (in prob_space) conditional_expectation_exists:
-  fixes X :: "'a \<Rightarrow> ereal" and N :: "('a, 'b) measure_space_scheme"
-  assumes borel: "X \<in> borel_measurable M" "AE x. 0 \<le> X x"
-  and N: "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M" "\<And>A. measure N A = \<mu> A"
-  shows "\<exists>Y\<in>borel_measurable N. (\<forall>x. 0 \<le> Y x) \<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))"
-proof -
-  note N(4)[simp]
-  interpret P: prob_space N
-    using prob_space_subalgebra[OF N] .
-
-  let ?f = "\<lambda>A x. X x * indicator A x"
-  let ?Q = "\<lambda>A. integral\<^isup>P M (?f A)"
-
-  from measure_space_density[OF borel]
-  have Q: "measure_space (N\<lparr> measure := ?Q \<rparr>)"
-    apply (rule measure_space.measure_space_subalgebra[of "M\<lparr> measure := ?Q \<rparr>"])
-    using N by (auto intro!: P.sigma_algebra_cong)
-  then interpret Q: measure_space "N\<lparr> measure := ?Q \<rparr>" .
-
-  have "P.absolutely_continuous ?Q"
-    unfolding P.absolutely_continuous_def
-  proof safe
-    fix A assume "A \<in> sets N" "P.\<mu> A = 0"
-    then have f_borel: "?f A \<in> borel_measurable M" "AE x. x \<notin> A"
-      using borel N by (auto intro!: borel_measurable_indicator AE_not_in)
-    then show "?Q A = 0"
-      by (auto simp add: positive_integral_0_iff_AE)
-  qed
-  from P.Radon_Nikodym[OF Q this]
-  obtain Y where Y: "Y \<in> borel_measurable N" "\<And>x. 0 \<le> Y x"
-    "\<And>A. A \<in> sets N \<Longrightarrow> ?Q A =(\<integral>\<^isup>+x. Y x * indicator A x \<partial>N)"
-    by blast
-  with N(2) show ?thesis
-    by (auto intro!: bexI[OF _ Y(1)] simp: positive_integral_subalgebra[OF _ _ N(2,3,4,1)])
-qed
-
-lemma (in prob_space)
-  fixes X :: "'a \<Rightarrow> ereal" and N :: "('a, 'b) measure_space_scheme"
-  assumes borel: "X \<in> borel_measurable M" "AE x. 0 \<le> X x"
-  and N: "sigma_algebra N" "sets N \<subseteq> sets M" "space N = space M" "\<And>A. measure N A = \<mu> A"
-  shows borel_measurable_conditional_expectation:
-    "conditional_expectation N X \<in> borel_measurable N"
-  and conditional_expectation: "\<And>C. C \<in> sets N \<Longrightarrow>
-      (\<integral>\<^isup>+x. conditional_expectation N X x * indicator C x \<partial>M) =
-      (\<integral>\<^isup>+x. X x * indicator C x \<partial>M)"
-   (is "\<And>C. C \<in> sets N \<Longrightarrow> ?eq C")
-proof -
-  note CE = conditional_expectation_exists[OF assms, unfolded Bex_def]
-  then show "conditional_expectation N X \<in> borel_measurable N"
-    unfolding conditional_expectation_def by (rule someI2_ex) blast
-
-  from CE show "\<And>C. C \<in> sets N \<Longrightarrow> ?eq C"
-    unfolding conditional_expectation_def by (rule someI2_ex) blast
-qed
-
-lemma (in sigma_algebra) factorize_measurable_function_pos:
-  fixes Z :: "'a \<Rightarrow> ereal" and Y :: "'a \<Rightarrow> 'c"
-  assumes "sigma_algebra M'" and "Y \<in> measurable M M'" "Z \<in> borel_measurable M"
-  assumes Z: "Z \<in> borel_measurable (sigma_algebra.vimage_algebra M' (space M) Y)"
-  shows "\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. max 0 (Z x) = g (Y x)"
-proof -
-  interpret M': sigma_algebra M' by fact
-  have Y: "Y \<in> space M \<rightarrow> space M'" using assms unfolding measurable_def by auto
-  from M'.sigma_algebra_vimage[OF this]
-  interpret va: sigma_algebra "M'.vimage_algebra (space M) Y" .
-
-  from va.borel_measurable_implies_simple_function_sequence'[OF Z] guess f . note f = this
-
-  have "\<forall>i. \<exists>g. simple_function M' g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
-  proof
-    fix i
-    from f(1)[of i] have "finite (f i`space M)" and B_ex:
-      "\<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"
-      unfolding simple_function_def by auto
-    from B_ex[THEN bchoice] guess B .. note B = this
-
-    let ?g = "\<lambda>x. \<Sum>z\<in>f i`space M. z * indicator (B z) x"
-
-    show "\<exists>g. simple_function M' g \<and> (\<forall>x\<in>space M. f i x = g (Y x))"
-    proof (intro exI[of _ ?g] conjI ballI)
-      show "simple_function M' ?g" using B by auto
-
-      fix x assume "x \<in> space M"
-      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)"
-        unfolding indicator_def using B by auto
-      then show "f i x = ?g (Y x)" using `x \<in> space M` f(1)[of i]
-        by (subst va.simple_function_indicator_representation) auto
-    qed
-  qed
-  from choice[OF this] guess g .. note g = this
-
-  show ?thesis
-  proof (intro ballI bexI)
-    show "(\<lambda>x. SUP i. g i x) \<in> borel_measurable M'"
-      using g by (auto intro: M'.borel_measurable_simple_function)
-    fix x assume "x \<in> space M"
-    have "max 0 (Z x) = (SUP i. f i x)" using f by simp
-    also have "\<dots> = (SUP i. g i (Y x))"
-      using g `x \<in> space M` by simp
-    finally show "max 0 (Z x) = (SUP i. g i (Y x))" .
-  qed
-qed
-
-lemma (in sigma_algebra) factorize_measurable_function:
-  fixes Z :: "'a \<Rightarrow> ereal" and Y :: "'a \<Rightarrow> 'c"
-  assumes "sigma_algebra M'" and "Y \<in> measurable M M'" "Z \<in> borel_measurable M"
-  shows "Z \<in> borel_measurable (sigma_algebra.vimage_algebra M' (space M) Y)
-    \<longleftrightarrow> (\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x))"
-proof safe
-  interpret M': sigma_algebra M' by fact
-  have Y: "Y \<in> space M \<rightarrow> space M'" using assms unfolding measurable_def by auto
-  from M'.sigma_algebra_vimage[OF this]
-  interpret va: sigma_algebra "M'.vimage_algebra (space M) Y" .
-
-  { fix g :: "'c \<Rightarrow> ereal" assume "g \<in> borel_measurable M'"
-    with M'.measurable_vimage_algebra[OF Y]
-    have "g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
-      by (rule measurable_comp)
-    moreover assume "\<forall>x\<in>space M. Z x = g (Y x)"
-    then have "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y) \<longleftrightarrow>
-       g \<circ> Y \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
-       by (auto intro!: measurable_cong)
-    ultimately show "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
-      by simp }
-
-  assume Z: "Z \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
-  with assms have "(\<lambda>x. - Z x) \<in> borel_measurable M"
-    "(\<lambda>x. - Z x) \<in> borel_measurable (M'.vimage_algebra (space M) Y)"
-    by auto
-  from factorize_measurable_function_pos[OF assms(1,2) this] guess n .. note n = this
-  from factorize_measurable_function_pos[OF assms Z] guess p .. note p = this
-  let ?g = "\<lambda>x. p x - n x"
-  show "\<exists>g\<in>borel_measurable M'. \<forall>x\<in>space M. Z x = g (Y x)"
-  proof (intro bexI ballI)
-    show "?g \<in> borel_measurable M'" using p n by auto
-    fix x assume "x \<in> space M"
-    then have "p (Y x) = max 0 (Z x)" "n (Y x) = max 0 (- Z x)"
-      using p n by auto
-    then show "Z x = ?g (Y x)"
-      by (auto split: split_max)
-  qed
-qed
-
-end
\ No newline at end of file
--- a/src/HOL/Probability/Finite_Product_Measure.thy	Mon Apr 23 12:23:23 2012 +0100
+++ b/src/HOL/Probability/Finite_Product_Measure.thy	Mon Apr 23 12:14:35 2012 +0200
@@ -8,6 +8,9 @@
 imports Binary_Product_Measure
 begin
 
+lemma split_const: "(\<lambda>(i, j). c) = (\<lambda>_. c)"
+  by auto
+
 lemma Pi_iff: "f \<in> Pi I X \<longleftrightarrow> (\<forall>i\<in>I. f i \<in> X i)"
   unfolding Pi_def by auto
 
@@ -34,9 +37,6 @@
 notation (xsymbols)
   funcset_extensional  (infixr "\<rightarrow>\<^isub>E" 60)
 
-lemma extensional_empty[simp]: "extensional {} = {\<lambda>x. undefined}"
-  by safe (auto simp add: extensional_def fun_eq_iff)
-
 lemma extensional_insert[intro, simp]:
   assumes "a \<in> extensional (insert i I)"
   shows "a(i := b) \<in> extensional (insert i I)"
@@ -86,7 +86,7 @@
   "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
   "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
   "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
-  by (auto simp: restrict_def intro!: ext)
+  by (auto simp: restrict_def)
 
 lemma extensional_insert_undefined[intro, simp]:
   assumes "a \<in> extensional (insert i I)"
@@ -130,16 +130,16 @@
   using assms by (auto simp: restrict_Pi_cancel)
 
 lemma restrict_fupd[simp]: "i \<notin> I \<Longrightarrow> restrict (f (i := x)) I = restrict f I"
-  by (auto simp: restrict_def intro!: ext)
+  by (auto simp: restrict_def)
 
 lemma merge_restrict[simp]:
   "merge I (restrict x I) J y = merge I x J y"
   "merge I x J (restrict y J) = merge I x J y"
-  unfolding merge_def by (auto intro!: ext)
+  unfolding merge_def by auto
 
 lemma merge_x_x_eq_restrict[simp]:
   "merge I x J x = restrict x (I \<union> J)"
-  unfolding merge_def by (auto intro!: ext)
+  unfolding merge_def by auto
 
 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"
   apply auto
@@ -233,339 +233,355 @@
 
 section "Products"
 
-locale product_sigma_algebra =
-  fixes M :: "'i \<Rightarrow> ('a, 'b) measure_space_scheme"
-  assumes sigma_algebras: "\<And>i. sigma_algebra (M i)"
+definition prod_emb where
+  "prod_emb I M K X = (\<lambda>x. restrict x K) -` X \<inter> (PIE i:I. space (M i))"
+
+lemma prod_emb_iff: 
+  "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))"
+  unfolding prod_emb_def by auto
 
-locale finite_product_sigma_algebra = product_sigma_algebra M
-  for M :: "'i \<Rightarrow> ('a, 'b) measure_space_scheme" +
-  fixes I :: "'i set"
-  assumes finite_index[simp, intro]: "finite I"
+lemma
+  shows prod_emb_empty[simp]: "prod_emb M L K {} = {}"
+    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"
+    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"
+    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))"
+    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))"
+    and prod_emb_Diff[simp]: "prod_emb M L K (A - B) = prod_emb M L K A - prod_emb M L K B"
+  by (auto simp: prod_emb_def)
 
-definition
-  "product_algebra_generator I M = \<lparr> space = (\<Pi>\<^isub>E i \<in> I. space (M i)),
-    sets = Pi\<^isub>E I ` (\<Pi> i \<in> I. sets (M i)),
-    measure = \<lambda>A. (\<Prod>i\<in>I. measure (M i) ((SOME F. A = Pi\<^isub>E I F) i)) \<rparr>"
+lemma prod_emb_PiE: "J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)) \<Longrightarrow>
+    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))"
+  by (force simp: prod_emb_def Pi_iff split_if_mem2)
+
+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"
+  by (auto simp: prod_emb_def Pi_iff)
 
-definition product_algebra_def:
-  "Pi\<^isub>M I M = sigma (product_algebra_generator I M)
-    \<lparr> measure := (SOME \<mu>. sigma_finite_measure (sigma (product_algebra_generator I M) \<lparr> measure := \<mu> \<rparr>) \<and>
-      (\<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>"
+definition PiM :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) measure" where
+  "PiM I M = extend_measure (\<Pi>\<^isub>E i\<in>I. space (M i))
+    {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}
+    (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^isub>E j\<in>J. X j))
+    (\<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)))"
+
+definition prod_algebra :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) set set" where
+  "prod_algebra I M = (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^isub>E j\<in>J. X j)) `
+    {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}"
+
+abbreviation
+  "Pi\<^isub>M I M \<equiv> PiM I M"
 
 syntax
-  "_PiM"  :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
-              ('i => 'a, 'b) measure_space_scheme"  ("(3PIM _:_./ _)" 10)
+  "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3PIM _:_./ _)" 10)
 
 syntax (xsymbols)
-  "_PiM" :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
-             ('i => 'a, 'b) measure_space_scheme"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"   10)
+  "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"  10)
 
 syntax (HTML output)
-  "_PiM" :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
-             ('i => 'a, 'b) measure_space_scheme"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"   10)
+  "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"  10)
 
 translations
-  "PIM x:I. M" == "CONST Pi\<^isub>M I (%x. M)"
-
-abbreviation (in finite_product_sigma_algebra) "G \<equiv> product_algebra_generator I M"
-abbreviation (in finite_product_sigma_algebra) "P \<equiv> Pi\<^isub>M I M"
-
-sublocale product_sigma_algebra \<subseteq> M: sigma_algebra "M i" for i by (rule sigma_algebras)
-
-lemma sigma_into_space:
-  assumes "sets M \<subseteq> Pow (space M)"
-  shows "sets (sigma M) \<subseteq> Pow (space M)"
-  using sigma_sets_into_sp[OF assms] unfolding sigma_def by auto
+  "PIM x:I. M" == "CONST PiM I (%x. M)"
 
-lemma (in product_sigma_algebra) product_algebra_generator_into_space:
-  "sets (product_algebra_generator I M) \<subseteq> Pow (space (product_algebra_generator I M))"
-  using M.sets_into_space unfolding product_algebra_generator_def
-  by auto blast
-
-lemma (in product_sigma_algebra) product_algebra_into_space:
-  "sets (Pi\<^isub>M I M) \<subseteq> Pow (space (Pi\<^isub>M I M))"
-  using product_algebra_generator_into_space
-  by (auto intro!: sigma_into_space simp add: product_algebra_def)
-
-lemma (in product_sigma_algebra) sigma_algebra_product_algebra: "sigma_algebra (Pi\<^isub>M I M)"
-  using product_algebra_generator_into_space unfolding product_algebra_def
-  by (rule sigma_algebra.sigma_algebra_cong[OF sigma_algebra_sigma]) simp_all
-
-sublocale finite_product_sigma_algebra \<subseteq> sigma_algebra P
-  using sigma_algebra_product_algebra .
+lemma prod_algebra_sets_into_space:
+  "prod_algebra I M \<subseteq> Pow (\<Pi>\<^isub>E i\<in>I. space (M i))"
+  using assms by (auto simp: prod_emb_def prod_algebra_def)
 
-lemma product_algebraE:
-  assumes "A \<in> sets (product_algebra_generator I M)"
-  obtains E where "A = Pi\<^isub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
-  using assms unfolding product_algebra_generator_def by auto
-
-lemma product_algebra_generatorI[intro]:
-  assumes "E \<in> (\<Pi> i\<in>I. sets (M i))"
-  shows "Pi\<^isub>E I E \<in> sets (product_algebra_generator I M)"
-  using assms unfolding product_algebra_generator_def by auto
-
-lemma space_product_algebra_generator[simp]:
-  "space (product_algebra_generator I M) = Pi\<^isub>E I (\<lambda>i. space (M i))"
-  unfolding product_algebra_generator_def by simp
+lemma prod_algebra_eq_finite:
+  assumes I: "finite I"
+  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")
+proof (intro iffI set_eqI)
+  fix A assume "A \<in> ?L"
+  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)"
+    and A: "A = prod_emb I M J (PIE j:J. E j)"
+    by (auto simp: prod_algebra_def)
+  let ?A = "\<Pi>\<^isub>E i\<in>I. if i \<in> J then E i else space (M i)"
+  have A: "A = ?A"
+    unfolding A using J by (intro prod_emb_PiE sets_into_space) auto
+  show "A \<in> ?R" unfolding A using J top
+    by (intro CollectI exI[of _ "\<lambda>i. if i \<in> J then E i else space (M i)"]) simp
+next
+  fix A assume "A \<in> ?R"
+  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
+  then have A: "A = prod_emb I M I (\<Pi>\<^isub>E i\<in>I. X i)"
+    using sets_into_space by (force simp: prod_emb_def Pi_iff)
+  from X I show "A \<in> ?L" unfolding A
+    by (auto simp: prod_algebra_def)
+qed
 
-lemma space_product_algebra[simp]:
-  "space (Pi\<^isub>M I M) = (\<Pi>\<^isub>E i\<in>I. space (M i))"
-  unfolding product_algebra_def product_algebra_generator_def by simp
-
-lemma sets_product_algebra:
-  "sets (Pi\<^isub>M I M) = sets (sigma (product_algebra_generator I M))"
-  unfolding product_algebra_def sigma_def by simp
+lemma prod_algebraI:
+  "finite J \<Longrightarrow> (J \<noteq> {} \<or> I = {}) \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i))
+    \<Longrightarrow> prod_emb I M J (PIE j:J. E j) \<in> prod_algebra I M"
+  by (auto simp: prod_algebra_def Pi_iff)
 
-lemma product_algebra_generator_sets_into_space:
-  assumes "\<And>i. i\<in>I \<Longrightarrow> sets (M i) \<subseteq> Pow (space (M i))"
-  shows "sets (product_algebra_generator I M) \<subseteq> Pow (space (product_algebra_generator I M))"
-  using assms by (auto simp: product_algebra_generator_def) blast
-
-lemma (in finite_product_sigma_algebra) in_P[simp, intro]:
-  "\<lbrakk> \<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i) \<rbrakk> \<Longrightarrow> Pi\<^isub>E I A \<in> sets P"
-  by (auto simp: sets_product_algebra)
-
-lemma Int_stable_product_algebra_generator:
-  "(\<And>i. i \<in> I \<Longrightarrow> Int_stable (M i)) \<Longrightarrow> Int_stable (product_algebra_generator I M)"
-  by (auto simp add: product_algebra_generator_def Int_stable_def PiE_Int Pi_iff)
+lemma prod_algebraE:
+  assumes A: "A \<in> prod_algebra I M"
+  obtains J E where "A = prod_emb I M J (PIE j:J. E j)"
+    "finite J" "J \<noteq> {} \<or> I = {}" "J \<subseteq> I" "\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i)" 
+  using A by (auto simp: prod_algebra_def)
 
-section "Generating set generates also product algebra"
+lemma prod_algebraE_all:
+  assumes A: "A \<in> prod_algebra I M"
+  obtains E where "A = Pi\<^isub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
+proof -
+  from A obtain E J where A: "A = prod_emb I M J (Pi\<^isub>E J E)"
+    and J: "J \<subseteq> I" and E: "E \<in> (\<Pi> i\<in>J. sets (M i))"
+    by (auto simp: prod_algebra_def)
+  from E have "\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)"
+    using sets_into_space by auto
+  then have "A = (\<Pi>\<^isub>E i\<in>I. if i\<in>J then E i else space (M i))"
+    using A J by (auto simp: prod_emb_PiE)
+  moreover then have "(\<lambda>i. if i\<in>J then E i else space (M i)) \<in> (\<Pi> i\<in>I. sets (M i))"
+    using top E by auto
+  ultimately show ?thesis using that by auto
+qed
 
-lemma sigma_product_algebra_sigma_eq:
-  assumes "finite I"
-  assumes mono: "\<And>i. i \<in> I \<Longrightarrow> incseq (S i)"
-  assumes union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (E i)"
-  assumes sets_into: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> sets (E i)"
-  and E: "\<And>i. sets (E i) \<subseteq> Pow (space (E i))"
-  shows "sets (\<Pi>\<^isub>M i\<in>I. sigma (E i)) = sets (\<Pi>\<^isub>M i\<in>I. E i)"
-    (is "sets ?S = sets ?E")
-proof cases
-  assume "I = {}" then show ?thesis
-    by (simp add: product_algebra_def product_algebra_generator_def)
-next
-  assume "I \<noteq> {}"
-  interpret E: sigma_algebra "sigma (E i)" for i
-    using E by (rule sigma_algebra_sigma)
-  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)"
-    using E by auto
-  interpret G: sigma_algebra ?E
-    unfolding product_algebra_def product_algebra_generator_def using E
-    by (intro sigma_algebra.sigma_algebra_cong[OF sigma_algebra_sigma]) (auto dest: Pi_mem)
-  { fix A i assume "i \<in> I" and A: "A \<in> sets (E i)"
-    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"
-      using mono union unfolding incseq_Suc_iff space_product_algebra
-      by (auto dest: Pi_mem)
-    also have "\<dots> = (\<Union>n. (\<Pi>\<^isub>E j\<in>I. if j = i then A else S j n))"
-      unfolding space_product_algebra
-      apply simp
-      apply (subst Pi_UN[OF `finite I`])
-      using mono[THEN incseqD] apply simp
-      apply (simp add: PiE_Int)
-      apply (intro PiE_cong)
-      using A sets_into by (auto intro!: into_space)
-    also have "\<dots> \<in> sets ?E"
-      using sets_into `A \<in> sets (E i)`
-      unfolding sets_product_algebra sets_sigma
-      by (intro sigma_sets.Union)
-         (auto simp: image_subset_iff intro!: sigma_sets.Basic)
-    finally have "(\<lambda>x. x i) -` A \<inter> space ?E \<in> sets ?E" . }
-  then have proj:
-    "\<And>i. i\<in>I \<Longrightarrow> (\<lambda>x. x i) \<in> measurable ?E (sigma (E i))"
-    using E by (subst G.measurable_iff_sigma)
-               (auto simp: sets_product_algebra sets_sigma)
-  { fix A assume A: "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (sigma (E i))"
-    with proj have basic: "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. x i) -` (A i) \<inter> space ?E \<in> sets ?E"
-      unfolding measurable_def by simp
-    have "Pi\<^isub>E I A = (\<Inter>i\<in>I. (\<lambda>x. x i) -` (A i) \<inter> space ?E)"
-      using A E.sets_into_space `I \<noteq> {}` unfolding product_algebra_def by auto blast
-    then have "Pi\<^isub>E I A \<in> sets ?E"
-      using G.finite_INT[OF `finite I` `I \<noteq> {}` basic, of "\<lambda>i. i"] by simp }
-  then have "sigma_sets (space ?E) (sets (product_algebra_generator I (\<lambda>i. sigma (E i)))) \<subseteq> sets ?E"
-    by (intro G.sigma_sets_subset) (auto simp add: product_algebra_generator_def)
-  then have subset: "sets ?S \<subseteq> sets ?E"
-    by (simp add: sets_sigma sets_product_algebra)
-  show "sets ?S = sets ?E"
-  proof (intro set_eqI iffI)
-    fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
-      unfolding sets_sigma sets_product_algebra
-    proof induct
-      case (Basic A) then show ?case
-        by (auto simp: sets_sigma product_algebra_generator_def intro: sigma_sets.Basic)
-    qed (auto intro: sigma_sets.intros simp: product_algebra_generator_def)
-  next
-    fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
-  qed
+lemma Int_stable_prod_algebra: "Int_stable (prod_algebra I M)"
+proof (unfold Int_stable_def, safe)
+  fix A assume "A \<in> prod_algebra I M"
+  from prod_algebraE[OF this] guess J E . note A = this
+  fix B assume "B \<in> prod_algebra I M"
+  from prod_algebraE[OF this] guess K F . note B = this
+  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> 
+      (if i \<in> K then F i else space (M i)))"
+    unfolding A B using A(2,3,4) A(5)[THEN sets_into_space] B(2,3,4) B(5)[THEN sets_into_space]
+    apply (subst (1 2 3) prod_emb_PiE)
+    apply (simp_all add: subset_eq PiE_Int)
+    apply blast
+    apply (intro PiE_cong)
+    apply auto
+    done
+  also have "\<dots> \<in> prod_algebra I M"
+    using A B by (auto intro!: prod_algebraI)
+  finally show "A \<inter> B \<in> prod_algebra I M" .
+qed
+
+lemma prod_algebra_mono:
+  assumes space: "\<And>i. i \<in> I \<Longrightarrow> space (E i) = space (F i)"
+  assumes sets: "\<And>i. i \<in> I \<Longrightarrow> sets (E i) \<subseteq> sets (F i)"
+  shows "prod_algebra I E \<subseteq> prod_algebra I F"
+proof
+  fix A assume "A \<in> prod_algebra I E"
+  then obtain J G where J: "J \<noteq> {} \<or> I = {}" "finite J" "J \<subseteq> I"
+    and A: "A = prod_emb I E J (\<Pi>\<^isub>E i\<in>J. G i)"
+    and G: "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (E i)"
+    by (auto simp: prod_algebra_def)
+  moreover
+  from space have "(\<Pi>\<^isub>E i\<in>I. space (E i)) = (\<Pi>\<^isub>E i\<in>I. space (F i))"
+    by (rule PiE_cong)
+  with A have "A = prod_emb I F J (\<Pi>\<^isub>E i\<in>J. G i)"
+    by (simp add: prod_emb_def)
+  moreover
+  from sets G J have "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (F i)"
+    by auto
+  ultimately show "A \<in> prod_algebra I F"
+    apply (simp add: prod_algebra_def image_iff)
+    apply (intro exI[of _ J] exI[of _ G] conjI)
+    apply auto
+    done
 qed
 
-lemma product_algebraI[intro]:
-    "E \<in> (\<Pi> i\<in>I. sets (M i)) \<Longrightarrow> Pi\<^isub>E I E \<in> sets (Pi\<^isub>M I M)"
-  using assms unfolding product_algebra_def by (auto intro: product_algebra_generatorI)
+lemma space_PiM: "space (\<Pi>\<^isub>M i\<in>I. M i) = (\<Pi>\<^isub>E i\<in>I. space (M i))"
+  using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro space_extend_measure) simp
+
+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)"
+  using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro sets_extend_measure) simp
 
-lemma (in product_sigma_algebra) measurable_component_update:
-  assumes "x \<in> space (Pi\<^isub>M I M)" and "i \<notin> I"
-  shows "(\<lambda>v. x(i := v)) \<in> measurable (M i) (Pi\<^isub>M (insert i I) M)" (is "?f \<in> _")
-  unfolding product_algebra_def apply simp
-proof (intro measurable_sigma)
-  let ?G = "product_algebra_generator (insert i I) M"
-  show "sets ?G \<subseteq> Pow (space ?G)" using product_algebra_generator_into_space .
-  show "?f \<in> space (M i) \<rightarrow> space ?G"
-    using M.sets_into_space assms by auto
-  fix A assume "A \<in> sets ?G"
-  from product_algebraE[OF this] guess E . note E = this
-  then have "?f -` A \<inter> space (M i) = E i \<or> ?f -` A \<inter> space (M i) = {}"
-    using M.sets_into_space assms by auto
-  then show "?f -` A \<inter> space (M i) \<in> sets (M i)"
-    using E by (auto intro!: product_algebraI)
+lemma sets_PiM_single: "sets (PiM I M) =
+    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)}"
+    (is "_ = sigma_sets ?\<Omega> ?R")
+  unfolding sets_PiM
+proof (rule sigma_sets_eqI)
+  interpret R: sigma_algebra ?\<Omega> "sigma_sets ?\<Omega> ?R" by (rule sigma_algebra_sigma_sets) auto
+  fix A assume "A \<in> prod_algebra I M"
+  from prod_algebraE[OF this] guess J X . note X = this
+  show "A \<in> sigma_sets ?\<Omega> ?R"
+  proof cases
+    assume "I = {}"
+    with X have "A = {\<lambda>x. undefined}" by (auto simp: prod_emb_def)
+    with `I = {}` show ?thesis by (auto intro!: sigma_sets_top)
+  next
+    assume "I \<noteq> {}"
+    with X have "A = (\<Inter>j\<in>J. {f\<in>(\<Pi>\<^isub>E i\<in>I. space (M i)). f j \<in> X j})"
+      using sets_into_space[OF X(5)]
+      by (auto simp: prod_emb_PiE[OF _ sets_into_space] Pi_iff split: split_if_asm) blast
+    also have "\<dots> \<in> sigma_sets ?\<Omega> ?R"
+      using X `I \<noteq> {}` by (intro R.finite_INT sigma_sets.Basic) auto
+    finally show "A \<in> sigma_sets ?\<Omega> ?R" .
+  qed
+next
+  fix A assume "A \<in> ?R"
+  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)" 
+    by auto
+  then have "A = prod_emb I M {i} (\<Pi>\<^isub>E i\<in>{i}. B)"
+    using sets_into_space[OF A(3)]
+    apply (subst prod_emb_PiE)
+    apply (auto simp: Pi_iff split: split_if_asm)
+    apply blast
+    done
+  also have "\<dots> \<in> sigma_sets ?\<Omega> (prod_algebra I M)"
+    using A by (intro sigma_sets.Basic prod_algebraI) auto
+  finally show "A \<in> sigma_sets ?\<Omega> (prod_algebra I M)" .
+qed
+
+lemma sets_PiM_I:
+  assumes "finite J" "J \<subseteq> I" "\<forall>i\<in>J. E i \<in> sets (M i)"
+  shows "prod_emb I M J (PIE j:J. E j) \<in> sets (PIM i:I. M i)"
+proof cases
+  assume "J = {}"
+  then have "prod_emb I M J (PIE j:J. E j) = (PIE j:I. space (M j))"
+    by (auto simp: prod_emb_def)
+  then show ?thesis
+    by (auto simp add: sets_PiM intro!: sigma_sets_top)
+next
+  assume "J \<noteq> {}" with assms show ?thesis
+    by (auto simp add: sets_PiM prod_algebra_def intro!: sigma_sets.Basic)
 qed
 
-lemma (in product_sigma_algebra) measurable_add_dim:
-  assumes "i \<notin> I"
-  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)"
-proof -
-  let ?f = "(\<lambda>(f, y). f(i := y))" and ?G = "product_algebra_generator (insert i I) M"
-  interpret Ii: pair_sigma_algebra "Pi\<^isub>M I M" "M i"
-    unfolding pair_sigma_algebra_def
-    by (intro sigma_algebra_product_algebra sigma_algebras conjI)
-  have "?f \<in> measurable Ii.P (sigma ?G)"
-  proof (rule Ii.measurable_sigma)
-    show "sets ?G \<subseteq> Pow (space ?G)"
-      using product_algebra_generator_into_space .
-    show "?f \<in> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) \<rightarrow> space ?G"
-      by (auto simp: space_pair_measure)
-  next
-    fix A assume "A \<in> sets ?G"
-    then obtain F where "A = Pi\<^isub>E (insert i I) F"
-      and F: "\<And>j. j \<in> I \<Longrightarrow> F j \<in> sets (M j)" "F i \<in> sets (M i)"
-      by (auto elim!: product_algebraE)
-    then have "?f -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) = Pi\<^isub>E I F \<times> (F i)"
-      using sets_into_space `i \<notin> I`
-      by (auto simp add: space_pair_measure) blast+
-    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)"
-      using F by (auto intro!: pair_measureI)
-  qed
-  then show ?thesis
-    by (simp add: product_algebra_def)
+lemma measurable_PiM:
+  assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
+  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>
+    f -` prod_emb I M J (Pi\<^isub>E J X) \<inter> space N \<in> sets N" 
+  shows "f \<in> measurable N (PiM I M)"
+  using sets_PiM prod_algebra_sets_into_space space
+proof (rule measurable_sigma_sets)
+  fix A assume "A \<in> prod_algebra I M"
+  from prod_algebraE[OF this] guess J X .
+  with sets[of J X] show "f -` A \<inter> space N \<in> sets N" by auto
+qed
+
+lemma measurable_PiM_Collect:
+  assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
+  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>
+    {\<omega>\<in>space N. \<forall>i\<in>J. f \<omega> i \<in> X i} \<in> sets N" 
+  shows "f \<in> measurable N (PiM I M)"
+  using sets_PiM prod_algebra_sets_into_space space
+proof (rule measurable_sigma_sets)
+  fix A assume "A \<in> prod_algebra I M"
+  from prod_algebraE[OF this] guess J X . note X = this
+  have "f -` A \<inter> space N = {\<omega> \<in> space N. \<forall>i\<in>J. f \<omega> i \<in> X i}"
+    using sets_into_space[OF X(5)] X(2-) space unfolding X(1)
+    by (subst prod_emb_PiE) (auto simp: Pi_iff split: split_if_asm)
+  also have "\<dots> \<in> sets N" using X(3,2,4,5) by (rule sets)
+  finally show "f -` A \<inter> space N \<in> sets N" .
 qed
 
-lemma (in product_sigma_algebra) measurable_merge:
-  assumes [simp]: "I \<inter> J = {}"
-  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)"
-proof -
-  let ?I = "Pi\<^isub>M I M" and ?J = "Pi\<^isub>M J M"
-  interpret P: sigma_algebra "?I \<Otimes>\<^isub>M ?J"
-    by (intro sigma_algebra_pair_measure product_algebra_into_space)
-  let ?f = "\<lambda>(x, y). merge I x J y"
-  let ?G = "product_algebra_generator (I \<union> J) M"
-  have "?f \<in> measurable (?I \<Otimes>\<^isub>M ?J) (sigma ?G)"
-  proof (rule P.measurable_sigma)
-    fix A assume "A \<in> sets ?G"
-    from product_algebraE[OF this]
-    obtain E where E: "A = Pi\<^isub>E (I \<union> J) E" "E \<in> (\<Pi> i\<in>I \<union> J. sets (M i))" .
-    then have *: "?f -` A \<inter> space (?I \<Otimes>\<^isub>M ?J) = Pi\<^isub>E I E \<times> Pi\<^isub>E J E"
-      using sets_into_space `I \<inter> J = {}`
-      by (auto simp add: space_pair_measure) fast+
-    then show "?f -` A \<inter> space (?I \<Otimes>\<^isub>M ?J) \<in> sets (?I \<Otimes>\<^isub>M ?J)"
-      using E unfolding * by (auto intro!: pair_measureI in_sigma product_algebra_generatorI
-        simp: product_algebra_def)
-  qed (insert product_algebra_generator_into_space, auto simp: space_pair_measure)
-  then show "?f \<in> measurable (?I \<Otimes>\<^isub>M ?J) (Pi\<^isub>M (I \<union> J) M)"
-    unfolding product_algebra_def[of "I \<union> J"] by simp
-qed
+lemma measurable_PiM_single:
+  assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
+  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" 
+  shows "f \<in> measurable N (PiM I M)"
+  using sets_PiM_single
+proof (rule measurable_sigma_sets)
+  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)}"
+  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)"
+    by auto
+  with space have "f -` A \<inter> space N = {\<omega> \<in> space N. f \<omega> i \<in> B}" by auto
+  also have "\<dots> \<in> sets N" using B by (rule sets)
+  finally show "f -` A \<inter> space N \<in> sets N" .
+qed (auto simp: space)
 
-lemma (in product_sigma_algebra) measurable_component_singleton:
+lemma sets_PiM_I_finite[simp, intro]:
+  assumes "finite I" and sets: "(\<And>i. i \<in> I \<Longrightarrow> E i \<in> sets (M i))"
+  shows "(PIE j:I. E j) \<in> sets (PIM i:I. M i)"
+  using sets_PiM_I[of I I E M] sets_into_space[OF sets] `finite I` sets by auto
+
+lemma measurable_component_update:
+  assumes "x \<in> space (Pi\<^isub>M I M)" and "i \<notin> I"
+  shows "(\<lambda>v. x(i := v)) \<in> measurable (M i) (Pi\<^isub>M (insert i I) M)" (is "?f \<in> _")
+proof (intro measurable_PiM_single)
+  fix j A assume "j \<in> insert i I" "A \<in> sets (M j)"
+  moreover have "{\<omega> \<in> space (M i). (x(i := \<omega>)) j \<in> A} =
+    (if i = j then space (M i) \<inter> A else if x j \<in> A then space (M i) else {})"
+    by auto
+  ultimately show "{\<omega> \<in> space (M i). (x(i := \<omega>)) j \<in> A} \<in> sets (M i)"
+    by auto
+qed (insert sets_into_space assms, auto simp: space_PiM)
+
+lemma measurable_component_singleton:
   assumes "i \<in> I" shows "(\<lambda>x. x i) \<in> measurable (Pi\<^isub>M I M) (M i)"
 proof (unfold measurable_def, intro CollectI conjI ballI)
   fix A assume "A \<in> sets (M i)"
-  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))"
-    using M.sets_into_space `i \<in> I` by (fastforce dest: Pi_mem split: split_if_asm)
+  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)"
+    using sets_into_space `i \<in> I`
+    by (fastforce dest: Pi_mem simp: prod_emb_def space_PiM split: split_if_asm)
   then show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M I M) \<in> sets (Pi\<^isub>M I M)"
-    using `A \<in> sets (M i)` by (auto intro!: product_algebraI)
-qed (insert `i \<in> I`, auto)
+    using `A \<in> sets (M i)` `i \<in> I` by (auto intro!: sets_PiM_I)
+qed (insert `i \<in> I`, auto simp: space_PiM)
+
+lemma measurable_add_dim:
+  assumes "i \<notin> I"
+  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)"
+    (is "?f \<in> measurable ?P ?I")
+proof (rule measurable_PiM_single)
+  fix j A assume j: "j \<in> insert i I" and A: "A \<in> sets (M j)"
+  have "{\<omega> \<in> space ?P. (\<lambda>(f, x). fun_upd f i x) \<omega> j \<in> A} =
+    (if j = i then space (Pi\<^isub>M I M) \<times> A else ((\<lambda>x. x j) \<circ> fst) -` A \<inter> space ?P)"
+    using sets_into_space[OF A] by (auto simp add: space_pair_measure space_PiM)
+  also have "\<dots> \<in> sets ?P"
+    using A j
+    by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
+  finally show "{\<omega> \<in> space ?P. prod_case (\<lambda>f. fun_upd f i) \<omega> j \<in> A} \<in> sets ?P" .
+qed (auto simp: space_pair_measure space_PiM)
 
-lemma (in sigma_algebra) measurable_restrict:
-  assumes I: "finite I"
-  assumes "\<And>i. i \<in> I \<Longrightarrow> sets (N i) \<subseteq> Pow (space (N i))"
-  assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> measurable M (N i)"
-  shows "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> measurable M (Pi\<^isub>M I N)"
-  unfolding product_algebra_def
-proof (simp, rule measurable_sigma)
-  show "sets (product_algebra_generator I N) \<subseteq> Pow (space (product_algebra_generator I N))"
-    by (rule product_algebra_generator_sets_into_space) fact
-  show "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> space M \<rightarrow> space (product_algebra_generator I N)"
-    using X by (auto simp: measurable_def)
-  fix E assume "E \<in> sets (product_algebra_generator I N)"
-  then obtain F where "E = Pi\<^isub>E I F" and F: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets (N i)"
-    by (auto simp: product_algebra_generator_def)
-  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"
-    by (auto simp: Pi_iff)
-  also have "\<dots> \<in> sets M"
-  proof cases
-    assume "I = {}" then show ?thesis by simp
-  next
-    assume "I \<noteq> {}" with X F I show ?thesis
-      by (intro finite_INT measurable_sets Int) auto
-  qed
-  finally show "(\<lambda>x. \<lambda>i\<in>I. X i x) -` E \<inter> space M \<in> sets M" .
-qed
+lemma measurable_merge:
+  assumes "I \<inter> J = {}"
+  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)"
+    (is "?f \<in> measurable ?P ?U")
+proof (rule measurable_PiM_single)
+  fix i A assume A: "A \<in> sets (M i)" "i \<in> I \<union> J"
+  then have "{\<omega> \<in> space ?P. prod_case (\<lambda>x. merge I x J) \<omega> i \<in> A} =
+    (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)"
+    using `I \<inter> J = {}` by auto
+  also have "\<dots> \<in> sets ?P"
+    using A
+    by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
+  finally show "{\<omega> \<in> space ?P. prod_case (\<lambda>x. merge I x J) \<omega> i \<in> A} \<in> sets ?P" .
+qed (insert assms, auto simp: space_pair_measure space_PiM)
 
-locale product_sigma_finite = product_sigma_algebra M
-  for M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme" +
+lemma measurable_restrict:
+  assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> measurable N (M i)"
+  shows "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> measurable N (Pi\<^isub>M I M)"
+proof (rule measurable_PiM_single)
+  fix A i assume A: "i \<in> I" "A \<in> sets (M i)"
+  then have "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} = X i -` A \<inter> space N"
+    by auto
+  then show "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} \<in> sets N"
+    using A X by (auto intro!: measurable_sets)
+qed (insert X, auto dest: measurable_space)
+
+locale product_sigma_finite =
+  fixes M :: "'i \<Rightarrow> 'a measure"
   assumes sigma_finite_measures: "\<And>i. sigma_finite_measure (M i)"
 
 sublocale product_sigma_finite \<subseteq> M: sigma_finite_measure "M i" for i
   by (rule sigma_finite_measures)
 
-locale finite_product_sigma_finite = finite_product_sigma_algebra M I + product_sigma_finite M
-  for M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme" and I :: "'i set"
-
-lemma (in finite_product_sigma_finite) product_algebra_generator_measure:
-  assumes "Pi\<^isub>E I F \<in> sets G"
-  shows "measure G (Pi\<^isub>E I F) = (\<Prod>i\<in>I. M.\<mu> i (F i))"
-proof cases
-  assume ne: "\<forall>i\<in>I. F i \<noteq> {}"
-  have "\<forall>i\<in>I. (SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') i = F i"
-    by (rule someI2[where P="\<lambda>F'. Pi\<^isub>E I F = Pi\<^isub>E I F'"])
-       (insert ne, auto simp: Pi_eq_iff)
-  then show ?thesis
-    unfolding product_algebra_generator_def by simp
-next
-  assume empty: "\<not> (\<forall>j\<in>I. F j \<noteq> {})"
-  then have "(\<Prod>j\<in>I. M.\<mu> j (F j)) = 0"
-    by (auto simp: setprod_ereal_0 intro!: bexI)
-  moreover
-  have "\<exists>j\<in>I. (SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') j = {}"
-    by (rule someI2[where P="\<lambda>F'. Pi\<^isub>E I F = Pi\<^isub>E I F'"])
-       (insert empty, auto simp: Pi_eq_empty_iff[symmetric])
-  then have "(\<Prod>j\<in>I. M.\<mu> j ((SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') j)) = 0"
-    by (auto simp: setprod_ereal_0 intro!: bexI)
-  ultimately show ?thesis
-    unfolding product_algebra_generator_def by simp
-qed
+locale finite_product_sigma_finite = product_sigma_finite M for M :: "'i \<Rightarrow> 'a measure" +
+  fixes I :: "'i set"
+  assumes finite_index: "finite I"
 
 lemma (in finite_product_sigma_finite) sigma_finite_pairs:
   "\<exists>F::'i \<Rightarrow> nat \<Rightarrow> 'a set.
     (\<forall>i\<in>I. range (F i) \<subseteq> sets (M i)) \<and>
-    (\<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>
-    (\<Union>k. \<Pi>\<^isub>E i\<in>I. F i k) = space G"
+    (\<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>
+    (\<Union>k. \<Pi>\<^isub>E i\<in>I. F i k) = space (PiM I M)"
 proof -
-  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>)"
-    using M.sigma_finite_up by simp
+  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>)"
+    using M.sigma_finite_incseq by metis
   from choice[OF this] guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
-  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>"
+  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>"
     by auto
   let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k"
-  note space_product_algebra[simp]
+  note space_PiM[simp]
   show ?thesis
   proof (intro exI[of _ F] conjI allI incseq_SucI set_eqI iffI ballI)
     fix i show "range (F i) \<subseteq> sets (M i)" by fact
   next
-    fix i k show "\<mu> i (F i k) \<noteq> \<infinity>" by fact
+    fix i k show "emeasure (M i) (F i k) \<noteq> \<infinity>" by fact
   next
-    fix A assume "A \<in> (\<Union>i. ?F i)" then show "A \<in> space G"
-      using `\<And>i. range (F i) \<subseteq> sets (M i)` M.sets_into_space
-      by (force simp: image_subset_iff)
+    fix A assume "A \<in> (\<Union>i. ?F i)" then show "A \<in> space (PiM I M)"
+      using `\<And>i. range (F i) \<subseteq> sets (M i)` sets_into_space
+      by auto blast
   next
-    fix f assume "f \<in> space G"
+    fix f assume "f \<in> space (PiM I M)"
     with Pi_UN[OF finite_index, of "\<lambda>k i. F i k"] F
     show "f \<in> (\<Union>i. ?F i)" by (auto simp: incseq_def)
   next
@@ -574,140 +590,144 @@
   qed
 qed
 
-lemma sets_pair_cancel_measure[simp]:
-  "sets (M1\<lparr>measure := m1\<rparr> \<Otimes>\<^isub>M M2) = sets (M1 \<Otimes>\<^isub>M M2)"
-  "sets (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m2\<rparr>) = sets (M1 \<Otimes>\<^isub>M M2)"
-  unfolding pair_measure_def pair_measure_generator_def sets_sigma
-  by simp_all
-
-lemma measurable_pair_cancel_measure[simp]:
-  "measurable (M1\<lparr>measure := m1\<rparr> \<Otimes>\<^isub>M M2) M = measurable (M1 \<Otimes>\<^isub>M M2) M"
-  "measurable (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m2\<rparr>) M = measurable (M1 \<Otimes>\<^isub>M M2) M"
-  "measurable M (M1\<lparr>measure := m3\<rparr> \<Otimes>\<^isub>M M2) = measurable M (M1 \<Otimes>\<^isub>M M2)"
-  "measurable M (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m4\<rparr>) = measurable M (M1 \<Otimes>\<^isub>M M2)"
-  unfolding measurable_def by (auto simp add: space_pair_measure)
-
-lemma (in product_sigma_finite) product_measure_exists:
+lemma (in product_sigma_finite)
   assumes "finite I"
-  shows "\<exists>\<nu>. sigma_finite_measure (sigma (product_algebra_generator I M) \<lparr> measure := \<nu> \<rparr>) \<and>
-    (\<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)))"
+  shows sigma_finite: "sigma_finite_measure (PiM I M)"
+  and emeasure_PiM:
+    "\<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))"
 using `finite I` proof induct
   case empty
-  interpret finite_product_sigma_finite M "{}" by default simp
-  let ?\<nu> = "(\<lambda>A. if A = {} then 0 else 1) :: 'd set \<Rightarrow> ereal"
-  show ?case
-  proof (intro exI conjI ballI)
-    have "sigma_algebra (sigma G \<lparr>measure := ?\<nu>\<rparr>)"
-      by (rule sigma_algebra_cong) (simp_all add: product_algebra_def)
-    then have "measure_space (sigma G\<lparr>measure := ?\<nu>\<rparr>)"
-      by (rule finite_additivity_sufficient)
-         (simp_all add: positive_def additive_def sets_sigma
-                        product_algebra_generator_def image_constant)
-    then show "sigma_finite_measure (sigma G\<lparr>measure := ?\<nu>\<rparr>)"
-      by (auto intro!: exI[of _ "\<lambda>i. {\<lambda>_. undefined}"]
-               simp: sigma_finite_measure_def sigma_finite_measure_axioms_def
-                     product_algebra_generator_def)
-  qed auto
+  let ?\<mu> = "\<lambda>A. if A = {} then 0 else (1::ereal)"
+  have "prod_algebra {} M = {{\<lambda>_. undefined}}"
+    by (auto simp: prod_algebra_def prod_emb_def intro!: image_eqI)
+  then have sets_empty: "sets (PiM {} M) = {{}, {\<lambda>_. undefined}}"
+    by (simp add: sets_PiM)
+  have "emeasure (Pi\<^isub>M {} M) (prod_emb {} M {} (\<Pi>\<^isub>E i\<in>{}. {})) = 1"
+  proof (subst emeasure_extend_measure_Pair[OF PiM_def])
+    have "finite (space (PiM {} M))"
+      by (simp add: space_PiM)
+    moreover show "positive (PiM {} M) ?\<mu>"
+      by (auto simp: positive_def)
+    ultimately show "countably_additive (PiM {} M) ?\<mu>"
+      by (rule countably_additiveI_finite) (auto simp: additive_def space_PiM sets_empty)
+  qed (auto simp: prod_emb_def)
+  also have *: "(prod_emb {} M {} (\<Pi>\<^isub>E i\<in>{}. {})) = {\<lambda>_. undefined}"
+    by (auto simp: prod_emb_def)
+  finally have emeasure_eq: "emeasure (Pi\<^isub>M {} M) {\<lambda>_. undefined} = 1" .
+
+  interpret finite_measure "PiM {} M"
+    by default (simp add: space_PiM emeasure_eq)
+  case 1 show ?case ..
+
+  case 2 show ?case
+    using emeasure_eq * by simp
 next
   case (insert i I)
   interpret finite_product_sigma_finite M I by default fact
   have "finite (insert i I)" using `finite I` by auto
   interpret I': finite_product_sigma_finite M "insert i I" by default fact
-  from insert obtain \<nu> where
-    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
-    "sigma_finite_measure (sigma G\<lparr> measure := \<nu> \<rparr>)" by auto
-  then interpret I: sigma_finite_measure "P\<lparr> measure := \<nu>\<rparr>" unfolding product_algebra_def by simp
-  interpret P: pair_sigma_finite "P\<lparr> measure := \<nu>\<rparr>" "M i" ..
+  interpret I: sigma_finite_measure "PiM I M" by fact
+  interpret P: pair_sigma_finite "PiM I M" "M i" ..
   let ?h = "(\<lambda>(f, y). f(i := y))"
-  let ?\<nu> = "\<lambda>A. P.\<mu> (?h -` A \<inter> space P.P)"
-  have I': "sigma_algebra (I'.P\<lparr> measure := ?\<nu> \<rparr>)"
-    by (rule I'.sigma_algebra_cong) simp_all
-  interpret I'': measure_space "I'.P\<lparr> measure := ?\<nu> \<rparr>"
-    using measurable_add_dim[OF `i \<notin> I`]
-    by (intro P.measure_space_vimage[OF I']) (auto simp add: measure_preserving_def)
-  show ?case
-  proof (intro exI[of _ ?\<nu>] conjI ballI)
-    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)"
-    { fix A assume A: "A \<in> (\<Pi> i\<in>insert i I. sets (M i))"
-      then have *: "?h -` Pi\<^isub>E (insert i I) A \<inter> space P.P = Pi\<^isub>E I A \<times> A i"
-        using `i \<notin> I` M.sets_into_space by (auto simp: space_pair_measure space_product_algebra) blast
-      show "?m (Pi\<^isub>E (insert i I) A) = (\<Prod>i\<in>insert i I. M.\<mu> i (A i))"
-        unfolding * using A
-        apply (subst P.pair_measure_times)
-        using A apply fastforce
-        using A apply fastforce
-        using `i \<notin> I` `finite I` prod[of A] A by (auto simp: ac_simps) }
-    note product = this
-    have *: "sigma I'.G\<lparr> measure := ?\<nu> \<rparr> = I'.P\<lparr> measure := ?\<nu> \<rparr>"
-      by (simp add: product_algebra_def)
-    show "sigma_finite_measure (sigma I'.G\<lparr> measure := ?\<nu> \<rparr>)"
-    proof (unfold *, default, simp)
-      from I'.sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
-      then have F: "\<And>j. j \<in> insert i I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
-        "incseq (\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k)"
-        "(\<Union>k. \<Pi>\<^isub>E j \<in> insert i I. F j k) = space I'.G"
-        "\<And>k. \<And>j. j \<in> insert i I \<Longrightarrow> \<mu> j (F j k) \<noteq> \<infinity>"
-        by blast+
-      let ?F = "\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k"
-      show "\<exists>F::nat \<Rightarrow> ('i \<Rightarrow> 'a) set. range F \<subseteq> sets I'.P \<and>
-          (\<Union>i. F i) = (\<Pi>\<^isub>E i\<in>insert i I. space (M i)) \<and> (\<forall>i. ?m (F i) \<noteq> \<infinity>)"
-      proof (intro exI[of _ ?F] conjI allI)
-        show "range ?F \<subseteq> sets I'.P" using F(1) by auto
-      next
-        from F(3) show "(\<Union>i. ?F i) = (\<Pi>\<^isub>E i\<in>insert i I. space (M i))" by simp
-      next
-        fix j
-        have "\<And>k. k \<in> insert i I \<Longrightarrow> 0 \<le> measure (M k) (F k j)"
-          using F(1) by auto
-        with F `finite I` setprod_PInf[of "insert i I", OF this] show "?\<nu> (?F j) \<noteq> \<infinity>"
-          by (subst product) auto
-      qed
-    qed
+
+  let ?P = "distr (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) (Pi\<^isub>M (insert i I) M) ?h"
+  let ?\<mu> = "emeasure ?P"
+  let ?I = "{j \<in> insert i I. emeasure (M j) (space (M j)) \<noteq> 1}"
+  let ?f = "\<lambda>J E j. if j \<in> J then emeasure (M j) (E j) else emeasure (M j) (space (M j))"
+
+  { case 2
+    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)) =
+      (\<Prod>i\<in>insert i I. emeasure (M i) (A i))"
+    proof (subst emeasure_extend_measure_Pair[OF PiM_def])
+      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))"
+      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
+      let ?p = "prod_emb (insert i I) M J (Pi\<^isub>E J E)"
+      let ?p' = "prod_emb I M (J - {i}) (\<Pi>\<^isub>E j\<in>J-{i}. E j)"
+      have "?\<mu> ?p =
+        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))"
+        by (intro emeasure_distr measurable_add_dim sets_PiM_I) fact+
+      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))"
+        using J E[rule_format, THEN sets_into_space]
+        by (force simp: space_pair_measure space_PiM Pi_iff prod_emb_iff split: split_if_asm)
+      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))) =
+        emeasure (Pi\<^isub>M I M) ?p' * emeasure (M i) (if i \<in> J then (E i) else space (M i))"
+        using J E by (intro P.emeasure_pair_measure_Times sets_PiM_I) auto
+      also have "?p' = (\<Pi>\<^isub>E j\<in>I. if j \<in> J-{i} then E j else space (M j))"
+        using J E[rule_format, THEN sets_into_space]
+        by (auto simp: prod_emb_iff Pi_iff split: split_if_asm) blast+
+      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)) =
+        (\<Prod> j\<in>I. if j \<in> J-{i} then emeasure (M j) (E j) else emeasure (M j) (space (M j)))"
+        using E by (subst insert) (auto intro!: setprod_cong)
+      also have "(\<Prod>j\<in>I. if j \<in> J - {i} then emeasure (M j) (E j) else emeasure (M j) (space (M j))) *
+         emeasure (M i) (if i \<in> J then E i else space (M i)) = (\<Prod>j\<in>insert i I. ?f J E j)"
+        using insert by (auto simp: mult_commute intro!: arg_cong2[where f="op *"] setprod_cong)
+      also have "\<dots> = (\<Prod>j\<in>J \<union> ?I. ?f J E j)"
+        using insert(1,2) J E by (intro setprod_mono_one_right) auto
+      finally show "?\<mu> ?p = \<dots>" .
+
+      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))"
+        using J E[rule_format, THEN sets_into_space] by (auto simp: prod_emb_iff)
+    next
+      show "positive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>" "countably_additive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>"
+        using emeasure_positive[of ?P] emeasure_countably_additive[of ?P] by simp_all
+    next
+      show "(insert i I \<noteq> {} \<or> insert i I = {}) \<and> finite (insert i I) \<and>
+        insert i I \<subseteq> insert i I \<and> A \<in> (\<Pi> j\<in>insert i I. sets (M j))"
+        using insert(1,2) 2 by auto
+    qed (auto intro!: setprod_cong)
+    with 2[THEN sets_into_space] show ?case by (subst (asm) prod_emb_PiE_same_index) auto }
+  note product = this
+
+  from I'.sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
+  then have F: "\<And>j. j \<in> insert i I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
+    "incseq (\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k)"
+    "(\<Union>k. \<Pi>\<^isub>E j \<in> insert i I. F j k) = space (Pi\<^isub>M (insert i I) M)"
+    "\<And>k. \<And>j. j \<in> insert i I \<Longrightarrow> emeasure (M j) (F j k) \<noteq> \<infinity>"
+    by blast+
+  let ?F = "\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k"
+
+  case 1 show ?case
+  proof (unfold_locales, intro exI[of _ ?F] conjI allI)
+    show "range ?F \<subseteq> sets (Pi\<^isub>M (insert i I) M)" using F(1) insert(1,2) by auto
+  next
+    from F(3) show "(\<Union>i. ?F i) = space (Pi\<^isub>M (insert i I) M)" by simp
+  next
+    fix j
+    from F `finite I` setprod_PInf[of "insert i I", OF emeasure_nonneg, of M]
+    show "emeasure (\<Pi>\<^isub>M i\<in>insert i I. M i) (?F j) \<noteq> \<infinity>"
+      by (subst product) auto
   qed
 qed
 
-sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure P
-  unfolding product_algebra_def
-  using product_measure_exists[OF finite_index]
-  by (rule someI2_ex) auto
+sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure "Pi\<^isub>M I M"
+  using sigma_finite[OF finite_index] .
 
 lemma (in finite_product_sigma_finite) measure_times:
-  assumes "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)"
-  shows "\<mu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
-  unfolding product_algebra_def
-  using product_measure_exists[OF finite_index]
-  proof (rule someI2_ex, elim conjE)
-    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))"
-    have "Pi\<^isub>E I A = Pi\<^isub>E I (\<lambda>i\<in>I. A i)" by (auto dest: Pi_mem)
-    then have "\<nu> (Pi\<^isub>E I A) = \<nu> (Pi\<^isub>E I (\<lambda>i\<in>I. A i))" by simp
-    also have "\<dots> = (\<Prod>i\<in>I. M.\<mu> i ((\<lambda>i\<in>I. A i) i))" using assms * by auto
-    finally show "measure (sigma G\<lparr>measure := \<nu>\<rparr>) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
-      by (simp add: sigma_def)
-  qed
+  "(\<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))"
+  using emeasure_PiM[OF finite_index] by auto
 
 lemma (in product_sigma_finite) product_measure_empty[simp]:
-  "measure (Pi\<^isub>M {} M) {\<lambda>x. undefined} = 1"
+  "emeasure (Pi\<^isub>M {} M) {\<lambda>x. undefined} = 1"
 proof -
   interpret finite_product_sigma_finite M "{}" by default auto
   from measure_times[of "\<lambda>x. {}"] show ?thesis by simp
 qed
 
-lemma (in finite_product_sigma_algebra) P_empty:
-  assumes "I = {}"
-  shows "space P = {\<lambda>k. undefined}" "sets P = { {}, {\<lambda>k. undefined} }"
-  unfolding product_algebra_def product_algebra_generator_def `I = {}`
-  by (simp_all add: sigma_def image_constant)
+lemma
+  shows space_PiM_empty: "space (Pi\<^isub>M {} M) = {\<lambda>k. undefined}"
+    and sets_PiM_empty: "sets (Pi\<^isub>M {} M) = { {}, {\<lambda>k. undefined} }"
+  by (simp_all add: space_PiM sets_PiM_single image_constant sigma_sets_empty_eq)
 
 lemma (in product_sigma_finite) positive_integral_empty:
   assumes pos: "0 \<le> f (\<lambda>k. undefined)"
   shows "integral\<^isup>P (Pi\<^isub>M {} M) f = f (\<lambda>k. undefined)"
 proof -
   interpret finite_product_sigma_finite M "{}" by default (fact finite.emptyI)
-  have "\<And>A. measure (Pi\<^isub>M {} M) (Pi\<^isub>E {} A) = 1"
+  have "\<And>A. emeasure (Pi\<^isub>M {} M) (Pi\<^isub>E {} A) = 1"
     using assms by (subst measure_times) auto
   then show ?thesis
-    unfolding positive_integral_def simple_function_def simple_integral_def [abs_def]
-  proof (simp add: P_empty, intro antisym)
+    unfolding positive_integral_def simple_function_def simple_integral_def[abs_def]
+  proof (simp add: space_PiM_empty sets_PiM_empty, intro antisym)
     show "f (\<lambda>k. undefined) \<le> (SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined))"
       by (intro SUP_upper) (auto simp: le_fun_def split: split_max)
     show "(SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined)) \<le> f (\<lambda>k. undefined)" using pos
@@ -715,71 +735,72 @@
   qed
 qed
 
-lemma (in product_sigma_finite) measure_fold:
+lemma (in product_sigma_finite) distr_merge:
   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
-  assumes A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
-  shows "measure (Pi\<^isub>M (I \<union> J) M) A =
-    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))"
+  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"
+   (is "?D = ?P")
 proof -
   interpret I: finite_product_sigma_finite M I by default fact
   interpret J: finite_product_sigma_finite M J by default fact
   have "finite (I \<union> J)" using fin by auto
   interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
-  interpret P: pair_sigma_finite I.P J.P by default
+  interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   let ?g = "\<lambda>(x,y). merge I x J y"
-  let ?X = "\<lambda>S. ?g -` S \<inter> space P.P"
+
   from IJ.sigma_finite_pairs obtain F where
     F: "\<And>i. i\<in> I \<union> J \<Longrightarrow> range (F i) \<subseteq> sets (M i)"
        "incseq (\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k)"
-       "(\<Union>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k) = space IJ.G"
-       "\<And>k. \<forall>i\<in>I\<union>J. \<mu> i (F i k) \<noteq> \<infinity>"
+       "(\<Union>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k) = space ?P"
+       "\<And>k. \<forall>i\<in>I\<union>J. emeasure (M i) (F i k) \<noteq> \<infinity>"
     by auto
   let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k"
-  show "IJ.\<mu> A = P.\<mu> (?X A)"
-  proof (rule measure_unique_Int_stable_vimage)
-    show "measure_space IJ.P" "measure_space P.P" by default
-    show "sets (sigma IJ.G) = sets IJ.P" "space IJ.G = space IJ.P" "A \<in> sets (sigma IJ.G)"
-      using A unfolding product_algebra_def by auto
-  next
-    show "Int_stable IJ.G"
-      by (rule Int_stable_product_algebra_generator)
-         (auto simp: Int_stable_def)
-    show "range ?F \<subseteq> sets IJ.G" using F
-      by (simp add: image_subset_iff product_algebra_def
-                    product_algebra_generator_def)
-    show "incseq ?F" "(\<Union>i. ?F i) = space IJ.G " by fact+
+  
+  show ?thesis
+  proof (rule measure_eqI_generator_eq[symmetric])
+    show "Int_stable (prod_algebra (I \<union> J) M)"
+      by (rule Int_stable_prod_algebra)
+    show "prod_algebra (I \<union> J) M \<subseteq> Pow (\<Pi>\<^isub>E i \<in> I \<union> J. space (M i))"
+      by (rule prod_algebra_sets_into_space)
+    show "sets ?P = sigma_sets (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i)) (prod_algebra (I \<union> J) M)"
+      by (rule sets_PiM)
+    then show "sets ?D = sigma_sets (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i)) (prod_algebra (I \<union> J) M)"
+      by simp
+
+    show "range ?F \<subseteq> prod_algebra (I \<union> J) M" using F
+      using fin by (auto simp: prod_algebra_eq_finite)
+    show "incseq ?F" by fact
+    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))"
+      using F(3) by (simp add: space_PiM)
   next
     fix k
-    have "\<And>j. j \<in> I \<union> J \<Longrightarrow> 0 \<le> measure (M j) (F j k)"
-      using F(1) by auto
-    with F `finite I` setprod_PInf[of "I \<union> J", OF this]
-    show "IJ.\<mu> (?F k) \<noteq> \<infinity>" by (subst IJ.measure_times) auto
+    from F `finite I` setprod_PInf[of "I \<union> J", OF emeasure_nonneg, of M]
+    show "emeasure ?P (?F k) \<noteq> \<infinity>" by (subst IJ.measure_times) auto
   next
-    fix A assume "A \<in> sets IJ.G"
-    then obtain F where A: "A = Pi\<^isub>E (I \<union> J) F"
-      and F: "\<And>i. i \<in> I \<union> J \<Longrightarrow> F i \<in> sets (M i)"
-      by (auto simp: product_algebra_generator_def)
-    then have X: "?X A = (Pi\<^isub>E I F \<times> Pi\<^isub>E J F)"
-      using sets_into_space by (auto simp: space_pair_measure) blast+
-    then have "P.\<mu> (?X A) = (\<Prod>i\<in>I. \<mu> i (F i)) * (\<Prod>i\<in>J. \<mu> i (F i))"
-      using `finite J` `finite I` F
-      by (simp add: P.pair_measure_times I.measure_times J.measure_times)
-    also have "\<dots> = (\<Prod>i\<in>I \<union> J. \<mu> i (F i))"
+    fix A assume A: "A \<in> prod_algebra (I \<union> J) M"
+    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)"
+      by (auto simp add: prod_algebra_eq_finite)
+    let ?B = "Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M"
+    let ?X = "?g -` A \<inter> space ?B"
+    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)"
+      using F[rule_format, THEN sets_into_space] by (auto simp: space_PiM)
+    then have X: "?X = (Pi\<^isub>E I F \<times> Pi\<^isub>E J F)"
+      unfolding A_eq by (subst merge_vimage) (auto simp: space_pair_measure space_PiM)
+    have "emeasure ?D A = emeasure ?B ?X"
+      using A by (intro emeasure_distr measurable_merge) (auto simp: sets_PiM)
+    also have "emeasure ?B ?X = (\<Prod>i\<in>I. emeasure (M i) (F i)) * (\<Prod>i\<in>J. emeasure (M i) (F i))"
+      using `finite J` `finite I` F X
+      by (simp add: P.emeasure_pair_measure_Times I.measure_times J.measure_times Pi_iff)
+    also have "\<dots> = (\<Prod>i\<in>I \<union> J. emeasure (M i) (F i))"
       using `finite J` `finite I` `I \<inter> J = {}`  by (simp add: setprod_Un_one)
-    also have "\<dots> = IJ.\<mu> A"
+    also have "\<dots> = emeasure ?P (Pi\<^isub>E (I \<union> J) F)"
       using `finite J` `finite I` F unfolding A
       by (intro IJ.measure_times[symmetric]) auto
-    finally show "IJ.\<mu> A = P.\<mu> (?X A)" by (rule sym)
-  qed (rule measurable_merge[OF IJ])
+    finally show "emeasure ?P A = emeasure ?D A" using A_eq by simp
+  qed
 qed
 
-lemma (in product_sigma_finite) measure_preserving_merge:
-  assumes IJ: "I \<inter> J = {}" and "finite I" "finite J"
-  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)"
-  by (intro measure_preservingI measurable_merge[OF IJ] measure_fold[symmetric] assms)
-
 lemma (in product_sigma_finite) product_positive_integral_fold:
-  assumes IJ[simp]: "I \<inter> J = {}" "finite I" "finite J"
+  assumes IJ: "I \<inter> J = {}" "finite I" "finite J"
   and f: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
   shows "integral\<^isup>P (Pi\<^isub>M (I \<union> J) M) f =
     (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (merge I x J y) \<partial>(Pi\<^isub>M J M)) \<partial>(Pi\<^isub>M I M))"
@@ -787,42 +808,38 @@
   interpret I: finite_product_sigma_finite M I by default fact
   interpret J: finite_product_sigma_finite M J by default fact
   interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
-  interpret IJ: finite_product_sigma_finite M "I \<union> J" by default simp
-  have P_borel: "(\<lambda>x. f (case x of (x, y) \<Rightarrow> merge I x J y)) \<in> borel_measurable P.P"
+  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)"
     using measurable_comp[OF measurable_merge[OF IJ(1)] f] by (simp add: comp_def)
   show ?thesis
-    unfolding P.positive_integral_fst_measurable[OF P_borel, simplified]
-  proof (rule P.positive_integral_vimage)
-    show "sigma_algebra IJ.P" by default
-    show "(\<lambda>(x, y). merge I x J y) \<in> measure_preserving P.P IJ.P"
-      using IJ by (rule measure_preserving_merge)
-    show "f \<in> borel_measurable IJ.P" using f by simp
-  qed
+    apply (subst distr_merge[OF IJ, symmetric])
+    apply (subst positive_integral_distr[OF measurable_merge f, OF IJ(1)])
+    apply (subst P.positive_integral_fst_measurable(2)[symmetric, OF P_borel])
+    apply simp
+    done
 qed
 
-lemma (in product_sigma_finite) measure_preserving_component_singelton:
-  "(\<lambda>x. x i) \<in> measure_preserving (Pi\<^isub>M {i} M) (M i)"
-proof (intro measure_preservingI measurable_component_singleton)
+lemma (in product_sigma_finite) distr_singleton:
+  "distr (Pi\<^isub>M {i} M) (M i) (\<lambda>x. x i) = M i" (is "?D = _")
+proof (intro measure_eqI[symmetric])
   interpret I: finite_product_sigma_finite M "{i}" by default simp
-  fix A let ?P = "(\<lambda>x. x i) -` A \<inter> space I.P"
-  assume A: "A \<in> sets (M i)"
-  then have *: "?P = {i} \<rightarrow>\<^isub>E A" using sets_into_space by auto
-  show "I.\<mu> ?P = M.\<mu> i A" unfolding *
-    using A I.measure_times[of "\<lambda>_. A"] by auto
-qed auto
+  fix A assume A: "A \<in> sets (M i)"
+  moreover then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M {i} M) = (\<Pi>\<^isub>E i\<in>{i}. A)"
+    using sets_into_space by (auto simp: space_PiM)
+  ultimately show "emeasure (M i) A = emeasure ?D A"
+    using A I.measure_times[of "\<lambda>_. A"]
+    by (simp add: emeasure_distr measurable_component_singleton)
+qed simp
 
 lemma (in product_sigma_finite) product_positive_integral_singleton:
   assumes f: "f \<in> borel_measurable (M i)"
   shows "integral\<^isup>P (Pi\<^isub>M {i} M) (\<lambda>x. f (x i)) = integral\<^isup>P (M i) f"
 proof -
   interpret I: finite_product_sigma_finite M "{i}" by default simp
-  show ?thesis
-  proof (rule I.positive_integral_vimage[symmetric])
-    show "sigma_algebra (M i)" by (rule sigma_algebras)
-    show "(\<lambda>x. x i) \<in> measure_preserving (Pi\<^isub>M {i} M) (M i)"
-      by (rule measure_preserving_component_singelton)
-    show "f \<in> borel_measurable (M i)" by fact
-  qed
+  from f show ?thesis
+    apply (subst distr_singleton[symmetric])
+    apply (subst positive_integral_distr[OF measurable_component_singleton])
+    apply simp_all
+    done
 qed
 
 lemma (in product_sigma_finite) product_positive_integral_insert:
@@ -832,19 +849,18 @@
 proof -
   interpret I: finite_product_sigma_finite M I by default auto
   interpret i: finite_product_sigma_finite M "{i}" by default auto
-  interpret P: pair_sigma_algebra I.P i.P ..
   have IJ: "I \<inter> {i} = {}" and insert: "I \<union> {i} = insert i I"
     using f by auto
   show ?thesis
     unfolding product_positive_integral_fold[OF IJ, unfolded insert, simplified, OF f]
-  proof (rule I.positive_integral_cong, subst product_positive_integral_singleton)
-    fix x assume x: "x \<in> space I.P"
+  proof (rule positive_integral_cong, subst product_positive_integral_singleton)
+    fix x assume x: "x \<in> space (Pi\<^isub>M I M)"
     let ?f = "\<lambda>y. f (restrict (x(i := y)) (insert i I))"
     have f'_eq: "\<And>y. ?f y = f (x(i := y))"
-      using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
+      using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def space_PiM)
     show "?f \<in> borel_measurable (M i)" unfolding f'_eq
-      using measurable_comp[OF measurable_component_update f] x `i \<notin> I`
-      by (simp add: comp_def)
+      using measurable_comp[OF measurable_component_update f, OF x `i \<notin> I`]
+      unfolding comp_def .
     show "integral\<^isup>P (M i) ?f = \<integral>\<^isup>+ y. f (x(i:=y)) \<partial>M i"
       unfolding f'_eq by simp
   qed
@@ -856,10 +872,6 @@
   and pos: "\<And>i x. i \<in> I \<Longrightarrow> 0 \<le> f i x"
   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))"
 using assms proof induct
-  case empty
-  interpret finite_product_sigma_finite M "{}" by default auto
-  show ?case by simp
-next
   case (insert i I)
   note `finite I`[intro, simp]
   interpret I: finite_product_sigma_finite M I by default auto
@@ -867,16 +879,16 @@
     using insert by (auto intro!: setprod_cong)
   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)"
     using sets_into_space insert
-    by (intro sigma_algebra.borel_measurable_ereal_setprod sigma_algebra_product_algebra
+    by (intro borel_measurable_ereal_setprod
               measurable_comp[OF measurable_component_singleton, unfolded comp_def])
        auto
   then show ?case
     apply (simp add: product_positive_integral_insert[OF insert(1,2) prod])
-    apply (simp add: insert * pos borel setprod_ereal_pos M.positive_integral_multc)
-    apply (subst I.positive_integral_cmult)
-    apply (auto simp add: pos borel insert setprod_ereal_pos positive_integral_positive)
+    apply (simp add: insert(2-) * pos borel setprod_ereal_pos positive_integral_multc)
+    apply (subst positive_integral_cmult)
+    apply (auto simp add: pos borel insert(2-) setprod_ereal_pos positive_integral_positive)
     done
-qed
+qed (simp add: space_PiM)
 
 lemma (in product_sigma_finite) product_integral_singleton:
   assumes f: "f \<in> borel_measurable (M i)"
@@ -899,48 +911,44 @@
   interpret J: finite_product_sigma_finite M J by default fact
   have "finite (I \<union> J)" using fin by auto
   interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
-  interpret P: pair_sigma_finite I.P J.P by default
+  interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   let ?M = "\<lambda>(x, y). merge I x J y"
   let ?f = "\<lambda>x. f (?M x)"
+  from f have f_borel: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
+    by auto
+  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)"
+    using measurable_comp[OF measurable_merge[OF IJ(1)] f_borel] by (simp add: comp_def)
+  have f_int: "integrable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) ?f"
+    by (rule integrable_distr[OF measurable_merge[OF IJ]]) (simp add: distr_merge[OF IJ fin] f)
   show ?thesis
-  proof (subst P.integrable_fst_measurable(2)[of ?f, simplified])
-    have 1: "sigma_algebra IJ.P" by default
-    have 2: "?M \<in> measure_preserving P.P IJ.P" using measure_preserving_merge[OF assms(1,2,3)] .
-    have 3: "integrable (Pi\<^isub>M (I \<union> J) M) f" by fact
-    then have 4: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
-      by (simp add: integrable_def)
-    show "integrable P.P ?f"
-      by (rule P.integrable_vimage[where f=f, OF 1 2 3])
-    show "integral\<^isup>L IJ.P f = integral\<^isup>L P.P ?f"
-      by (rule P.integral_vimage[where f=f, OF 1 2 4])
-  qed
+    apply (subst distr_merge[symmetric, OF IJ fin])
+    apply (subst integral_distr[OF measurable_merge[OF IJ] f_borel])
+    apply (subst P.integrable_fst_measurable(2)[symmetric, OF f_int])
+    apply simp
+    done
 qed
 
 lemma (in product_sigma_finite) product_integral_insert:
-  assumes [simp]: "finite I" "i \<notin> I"
+  assumes I: "finite I" "i \<notin> I"
     and f: "integrable (Pi\<^isub>M (insert i I) M) f"
   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)"
 proof -
-  interpret I: finite_product_sigma_finite M I by default auto
-  interpret I': finite_product_sigma_finite M "insert i I" by default auto
-  interpret i: finite_product_sigma_finite M "{i}" by default auto
-  interpret P: pair_sigma_finite I.P i.P ..
-  have IJ: "I \<inter> {i} = {}" by auto
-  show ?thesis
-    unfolding product_integral_fold[OF IJ, simplified, OF f]
-  proof (rule I.integral_cong, subst product_integral_singleton)
-    fix x assume x: "x \<in> space I.P"
-    let ?f = "\<lambda>y. f (restrict (x(i := y)) (insert i I))"
-    have f'_eq: "\<And>y. ?f y = f (x(i := y))"
-      using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
-    have f: "f \<in> borel_measurable I'.P" using f unfolding integrable_def by auto
-    show "?f \<in> borel_measurable (M i)"
-      unfolding measurable_cong[OF f'_eq]
-      using measurable_comp[OF measurable_component_update f] x `i \<notin> I`
-      by (simp add: comp_def)
-    show "integral\<^isup>L (M i) ?f = integral\<^isup>L (M i) (\<lambda>y. f (x(i := y)))"
-      unfolding f'_eq by simp
+  have "integral\<^isup>L (Pi\<^isub>M (insert i I) M) f = integral\<^isup>L (Pi\<^isub>M (I \<union> {i}) M) f"
+    by simp
+  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)"
+    using f I by (intro product_integral_fold) auto
+  also have "\<dots> = (\<integral>x. (\<integral>y. f (x(i := y)) \<partial>M i) \<partial>Pi\<^isub>M I M)"
+  proof (rule integral_cong, subst product_integral_singleton[symmetric])
+    fix x assume x: "x \<in> space (Pi\<^isub>M I M)"
+    have f_borel: "f \<in> borel_measurable (Pi\<^isub>M (insert i I) M)"
+      using f by auto
+    show "(\<lambda>y. f (x(i := y))) \<in> borel_measurable (M i)"
+      using measurable_comp[OF measurable_component_update f_borel, OF x `i \<notin> I`]
+      unfolding comp_def .
+    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)"
+      by (auto intro!: integral_cong arg_cong[where f=f] simp: merge_def space_PiM extensional_def)
   qed
+  finally show ?thesis .
 qed
 
 lemma (in product_sigma_finite) product_integrable_setprod:
@@ -951,24 +959,23 @@
   interpret finite_product_sigma_finite M I by default fact
   have f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
     using integrable unfolding integrable_def by auto
-  then have borel: "?f \<in> borel_measurable P"
-    using measurable_comp[OF measurable_component_singleton f]
-    by (auto intro!: borel_measurable_setprod simp: comp_def)
+  have borel: "?f \<in> borel_measurable (Pi\<^isub>M I M)"
+    using measurable_comp[OF measurable_component_singleton[of _ I M] f] by (auto simp: comp_def)
   moreover have "integrable (Pi\<^isub>M I M) (\<lambda>x. \<bar>\<Prod>i\<in>I. f i (x i)\<bar>)"
   proof (unfold integrable_def, intro conjI)
-    show "(\<lambda>x. abs (?f x)) \<in> borel_measurable P"
+    show "(\<lambda>x. abs (?f x)) \<in> borel_measurable (Pi\<^isub>M I M)"
       using borel by auto
-    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)"
+    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)"
       by (simp add: setprod_ereal abs_setprod)
     also have "\<dots> = (\<Prod>i\<in>I. (\<integral>\<^isup>+x. ereal (abs (f i x)) \<partial>M i))"
       using f by (subst product_positive_integral_setprod) auto
     also have "\<dots> < \<infinity>"
-      using integrable[THEN M.integrable_abs]
-      by (simp add: setprod_PInf integrable_def M.positive_integral_positive)
-    finally show "(\<integral>\<^isup>+x. ereal (abs (?f x)) \<partial>P) \<noteq> \<infinity>" by auto
-    have "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>P) = (\<integral>\<^isup>+x. 0 \<partial>P)"
+      using integrable[THEN integrable_abs]
+      by (simp add: setprod_PInf integrable_def positive_integral_positive)
+    finally show "(\<integral>\<^isup>+x. ereal (abs (?f x)) \<partial>(Pi\<^isub>M I M)) \<noteq> \<infinity>" by auto
+    have "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>(Pi\<^isub>M I M)) = (\<integral>\<^isup>+x. 0 \<partial>(Pi\<^isub>M I M))"
       by (intro positive_integral_cong_pos) auto
-    then show "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>P) \<noteq> \<infinity>" by simp
+    then show "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>(Pi\<^isub>M I M)) \<noteq> \<infinity>" by simp
   qed
   ultimately show ?thesis
     by (rule integrable_abs_iff[THEN iffD1])
@@ -992,7 +999,69 @@
     using `i \<notin> I` by (auto intro!: setprod_cong)
   show ?case
     unfolding product_integral_insert[OF insert(1,3) prod[OF subset_refl]]
-    by (simp add: * insert integral_multc I.integral_cmult[OF prod] subset_insertI)
+    by (simp add: * insert integral_multc integral_cmult[OF prod] subset_insertI)
 qed
 
-end
\ No newline at end of file
+lemma sigma_prod_algebra_sigma_eq:
+  fixes E :: "'i \<Rightarrow> 'a set set"
+  assumes "finite I"
+  assumes S_mono: "\<And>i. i \<in> I \<Longrightarrow> incseq (S i)"
+    and S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
+    and S_in_E: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> E i"
+  assumes E_closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M i))"
+    and E_generates: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sigma_sets (space (M i)) (E i)"
+  defines "P == { Pi\<^isub>E I F | F. \<forall>i\<in>I. F i \<in> E i }"
+  shows "sets (PiM I M) = sigma_sets (space (PiM I M)) P"
+proof
+  let ?P = "sigma (space (Pi\<^isub>M I M)) P"
+  have P_closed: "P \<subseteq> Pow (space (Pi\<^isub>M I M))"
+    using E_closed by (auto simp: space_PiM P_def Pi_iff subset_eq)
+  then have space_P: "space ?P = (\<Pi>\<^isub>E i\<in>I. space (M i))"
+    by (simp add: space_PiM)
+  have "sets (PiM I M) =
+      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)}"
+    using sets_PiM_single[of I M] by (simp add: space_P)
+  also have "\<dots> \<subseteq> sets (sigma (space (PiM I M)) P)"
+  proof (safe intro!: sigma_sets_subset)
+    fix i A assume "i \<in> I" and A: "A \<in> sets (M i)"
+    have "(\<lambda>x. x i) \<in> measurable ?P (sigma (space (M i)) (E i))"
+    proof (subst measurable_iff_measure_of)
+      show "E i \<subseteq> Pow (space (M i))" using `i \<in> I` by fact
+      from space_P `i \<in> I` show "(\<lambda>x. x i) \<in> space ?P \<rightarrow> space (M i)"
+        by (auto simp: Pi_iff)
+      show "\<forall>A\<in>E i. (\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
+      proof
+        fix A assume A: "A \<in> E i"
+        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))"
+          using E_closed `i \<in> I` by (auto simp: space_P Pi_iff subset_eq split: split_if_asm)
+        also have "\<dots> = (\<Pi>\<^isub>E j\<in>I. \<Union>n. if i = j then A else S j n)"
+          by (intro PiE_cong) (simp add: S_union)
+        also have "\<dots> = (\<Union>n. \<Pi>\<^isub>E j\<in>I. if i = j then A else S j n)"
+          using S_mono
+          by (subst Pi_UN[symmetric, OF `finite I`]) (auto simp: incseq_def)
+        also have "\<dots> \<in> sets ?P"
+        proof (safe intro!: countable_UN)
+          fix n show "(\<Pi>\<^isub>E j\<in>I. if i = j then A else S j n) \<in> sets ?P"
+            using A S_in_E
+            by (simp add: P_closed)
+               (auto simp: P_def subset_eq intro!: exI[of _ "\<lambda>j. if i = j then A else S j n"])
+        qed
+        finally show "(\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
+          using P_closed by simp
+      qed
+    qed
+    from measurable_sets[OF this, of A] A `i \<in> I` E_closed
+    have "(\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
+      by (simp add: E_generates)
+    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}"
+      using P_closed by (auto simp: space_PiM)
+    finally show "\<dots> \<in> sets ?P" .
+  qed
+  finally show "sets (PiM I M) \<subseteq> sigma_sets (space (PiM I M)) P"
+    by (simp add: P_closed)
+  show "sigma_sets (space (PiM I M)) P \<subseteq> sets (PiM I M)"
+    using `finite I`
+    by (auto intro!: sigma_sets_subset simp: E_generates P_def)
+qed
+
+end
--- a/src/HOL/Probability/Independent_Family.thy	Mon Apr 23 12:23:23 2012 +0100
+++ b/src/HOL/Probability/Independent_Family.thy	Mon Apr 23 12:14:35 2012 +0200
@@ -5,7 +5,7 @@
 header {* Independent families of events, event sets, and random variables *}
 
 theory Independent_Family
-  imports Probability_Measure
+  imports Probability_Measure Infinite_Product_Measure
 begin
 
 lemma INT_decseq_offset:
@@ -44,7 +44,7 @@
 definition (in prob_space)
   "indep_var Ma A Mb B \<longleftrightarrow> indep_vars (bool_case Ma Mb) (bool_case A B) UNIV"
 
-lemma (in prob_space) indep_sets_cong[cong]:
+lemma (in prob_space) indep_sets_cong:
   "I = J \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> F i = G i) \<Longrightarrow> indep_sets F I \<longleftrightarrow> indep_sets G J"
   by (simp add: indep_sets_def, intro conj_cong all_cong imp_cong ball_cong) blast+
 
@@ -135,7 +135,7 @@
 
 lemma (in prob_space) indep_sets_dynkin:
   assumes indep: "indep_sets F I"
-  shows "indep_sets (\<lambda>i. sets (dynkin \<lparr> space = space M, sets = F i \<rparr>)) I"
+  shows "indep_sets (\<lambda>i. dynkin (space M) (F i)) I"
     (is "indep_sets ?F I")
 proof (subst indep_sets_finite_index_sets, intro allI impI ballI)
   fix J assume "finite J" "J \<subseteq> I" "J \<noteq> {}"
@@ -193,7 +193,7 @@
           qed
         qed }
       note indep_sets_insert = this
-      have "dynkin_system \<lparr> space = space M, sets = ?D \<rparr>"
+      have "dynkin_system (space M) ?D"
       proof (rule dynkin_systemI', simp_all cong del: indep_sets_cong, safe)
         show "indep_sets (G(j := {{}})) K"
           by (rule indep_sets_insert) auto
@@ -206,7 +206,7 @@
             using G by auto
           have "prob ((\<Inter>j\<in>J. A j) \<inter> (space M - X)) =
               prob ((\<Inter>j\<in>J. A j) - (\<Inter>i\<in>insert j J. (A(j := X)) i))"
-            using A_sets sets_into_space X `J \<noteq> {}`
+            using A_sets sets_into_space[of _ M] X `J \<noteq> {}`
             by (auto intro!: arg_cong[where f=prob] split: split_if_asm)
           also have "\<dots> = prob (\<Inter>j\<in>J. A j) - prob (\<Inter>i\<in>insert j J. (A(j := X)) i)"
             using J `J \<noteq> {}` `j \<notin> J` A_sets X sets_into_space
@@ -264,9 +264,8 @@
             by (auto dest!: sums_unique)
         qed (insert F, auto)
       qed (insert sets_into_space, auto)
-      then have mono: "sets (dynkin \<lparr>space = space M, sets = G j\<rparr>) \<subseteq>
-        sets \<lparr>space = space M, sets = {E \<in> events. indep_sets (G(j := {E})) K}\<rparr>"
-      proof (rule dynkin_system.dynkin_subset, simp_all cong del: indep_sets_cong, safe)
+      then have mono: "dynkin (space M) (G j) \<subseteq> {E \<in> events. indep_sets (G(j := {E})) K}"
+      proof (rule dynkin_system.dynkin_subset, safe)
         fix X assume "X \<in> G j"
         then show "X \<in> events" using G `j \<in> K` by auto
         from `indep_sets G K`
@@ -292,20 +291,20 @@
             by (intro indep_setsD[OF G(1)]) auto
         qed
       qed
-      then have "indep_sets (G(j:=sets (dynkin \<lparr>space = space M, sets = G j\<rparr>))) K"
+      then have "indep_sets (G(j := dynkin (space M) (G j))) K"
         by (rule indep_sets_mono_sets) (insert mono, auto)
       then show ?case
         by (rule indep_sets_mono_sets) (insert `j \<in> K` `j \<notin> J`, auto simp: G_def)
     qed (insert `indep_sets F K`, simp) }
   from this[OF `indep_sets F J` `finite J` subset_refl]
-  show "indep_sets (\<lambda>i. sets (dynkin \<lparr> space = space M, sets = F i \<rparr>)) J"
+  show "indep_sets ?F J"
     by (rule indep_sets_mono_sets) auto
 qed
 
 lemma (in prob_space) indep_sets_sigma:
   assumes indep: "indep_sets F I"
-  assumes stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable \<lparr> space = space M, sets = F i \<rparr>"
-  shows "indep_sets (\<lambda>i. sets (sigma \<lparr> space = space M, sets = F i \<rparr>)) I"
+  assumes stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (F i)"
+  shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
 proof -
   from indep_sets_dynkin[OF indep]
   show ?thesis
@@ -316,18 +315,12 @@
   qed
 qed
 
-lemma (in prob_space) indep_sets_sigma_sets:
-  assumes "indep_sets F I"
-  assumes "\<And>i. i \<in> I \<Longrightarrow> Int_stable \<lparr> space = space M, sets = F i \<rparr>"
-  shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
-  using indep_sets_sigma[OF assms] by (simp add: sets_sigma)
-
 lemma (in prob_space) indep_sets_sigma_sets_iff:
-  assumes "\<And>i. i \<in> I \<Longrightarrow> Int_stable \<lparr> space = space M, sets = F i \<rparr>"
+  assumes "\<And>i. i \<in> I \<Longrightarrow> Int_stable (F i)"
   shows "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I \<longleftrightarrow> indep_sets F I"
 proof
   assume "indep_sets F I" then show "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I"
-    by (rule indep_sets_sigma_sets) fact
+    by (rule indep_sets_sigma) fact
 next
   assume "indep_sets (\<lambda>i. sigma_sets (space M) (F i)) I" then show "indep_sets F I"
     by (rule indep_sets_mono_sets) (intro subsetI sigma_sets.Basic)
@@ -361,15 +354,14 @@
 
 lemma (in prob_space) indep_set_sigma_sets:
   assumes "indep_set A B"
-  assumes A: "Int_stable \<lparr> space = space M, sets = A \<rparr>"
-  assumes B: "Int_stable \<lparr> space = space M, sets = B \<rparr>"
+  assumes A: "Int_stable A" and B: "Int_stable B"
   shows "indep_set (sigma_sets (space M) A) (sigma_sets (space M) B)"
 proof -
   have "indep_sets (\<lambda>i. sigma_sets (space M) (case i of True \<Rightarrow> A | False \<Rightarrow> B)) UNIV"
-  proof (rule indep_sets_sigma_sets)
+  proof (rule indep_sets_sigma)
     show "indep_sets (bool_case A B) UNIV"
       by (rule `indep_set A B`[unfolded indep_set_def])
-    fix i show "Int_stable \<lparr>space = space M, sets = case i of True \<Rightarrow> A | False \<Rightarrow> B\<rparr>"
+    fix i show "Int_stable (case i of True \<Rightarrow> A | False \<Rightarrow> B)"
       using A B by (cases i) auto
   qed
   then show ?thesis
@@ -380,7 +372,7 @@
 lemma (in prob_space) indep_sets_collect_sigma:
   fixes I :: "'j \<Rightarrow> 'i set" and J :: "'j set" and E :: "'i \<Rightarrow> 'a set set"
   assumes indep: "indep_sets E (\<Union>j\<in>J. I j)"
-  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>"
+  assumes Int_stable: "\<And>i j. j \<in> J \<Longrightarrow> i \<in> I j \<Longrightarrow> Int_stable (E i)"
   assumes disjoint: "disjoint_family_on I J"
   shows "indep_sets (\<lambda>j. sigma_sets (space M) (\<Union>i\<in>I j. E i)) J"
 proof -
@@ -389,30 +381,29 @@
   from indep have E: "\<And>j i. j \<in> J \<Longrightarrow> i \<in> I j \<Longrightarrow> E i \<subseteq> events"
     unfolding indep_sets_def by auto
   { fix j
-    let ?S = "sigma \<lparr> space = space M, sets = (\<Union>i\<in>I j. E i) \<rparr>"
+    let ?S = "sigma_sets (space M) (\<Union>i\<in>I j. E i)"
     assume "j \<in> J"
-    from E[OF this] interpret S: sigma_algebra ?S
-      using sets_into_space by (intro sigma_algebra_sigma) auto
+    from E[OF this] interpret S: sigma_algebra "space M" ?S
+      using sets_into_space[of _ M] by (intro sigma_algebra_sigma_sets) auto
 
     have "sigma_sets (space M) (\<Union>i\<in>I j. E i) = sigma_sets (space M) (?E j)"
     proof (rule sigma_sets_eqI)
       fix A assume "A \<in> (\<Union>i\<in>I j. E i)"
       then guess i ..
       then show "A \<in> sigma_sets (space M) (?E j)"
-        by (auto intro!: sigma_sets.intros exI[of _ "{i}"] exI[of _ "\<lambda>i. A"])
+        by (auto intro!: sigma_sets.intros(2-) exI[of _ "{i}"] exI[of _ "\<lambda>i. A"])
     next
       fix A assume "A \<in> ?E j"
       then obtain E' K where "finite K" "K \<noteq> {}" "K \<subseteq> I j" "\<And>k. k \<in> K \<Longrightarrow> E' k \<in> E k"
         and A: "A = (\<Inter>k\<in>K. E' k)"
         by auto
-      then have "A \<in> sets ?S" unfolding A
-        by (safe intro!: S.finite_INT)
-           (auto simp: sets_sigma intro!: sigma_sets.Basic)
+      then have "A \<in> ?S" unfolding A
+        by (safe intro!: S.finite_INT) auto
       then show "A \<in> sigma_sets (space M) (\<Union>i\<in>I j. E i)"
-        by (simp add: sets_sigma)
+        by simp
     qed }
   moreover have "indep_sets (\<lambda>j. sigma_sets (space M) (?E j)) J"
-  proof (rule indep_sets_sigma_sets)
+  proof (rule indep_sets_sigma)
     show "indep_sets ?E J"
     proof (intro indep_setsI)
       fix j assume "j \<in> J" with E show "?E j \<subseteq> events" by (force  intro!: finite_INT)
@@ -460,7 +451,7 @@
     qed
   next
     fix j assume "j \<in> J"
-    show "Int_stable \<lparr> space = space M, sets = ?E j \<rparr>"
+    show "Int_stable (?E j)"
     proof (rule Int_stableI)
       fix a assume "a \<in> ?E j" then obtain Ka Ea
         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
@@ -482,12 +473,12 @@
 
 lemma (in prob_space) terminal_events_sets:
   assumes A: "\<And>i. A i \<subseteq> events"
-  assumes "\<And>i::nat. sigma_algebra \<lparr>space = space M, sets = A i\<rparr>"
+  assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   assumes X: "X \<in> terminal_events A"
   shows "X \<in> events"
 proof -
   let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
-  interpret A: sigma_algebra "\<lparr>space = space M, sets = A i\<rparr>" for i by fact
+  interpret A: sigma_algebra "space M" "A i" for i by fact
   from X have "\<And>n. X \<in> sigma_sets (space M) (UNION {n..} A)" by (auto simp: terminal_events_def)
   from this[of 0] have "X \<in> sigma_sets (space M) (UNION UNIV A)" by simp
   then show "X \<in> events"
@@ -495,12 +486,12 @@
 qed
 
 lemma (in prob_space) sigma_algebra_terminal_events:
-  assumes "\<And>i::nat. sigma_algebra \<lparr>space = space M, sets = A i\<rparr>"
-  shows "sigma_algebra \<lparr> space = space M, sets = terminal_events A \<rparr>"
+  assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
+  shows "sigma_algebra (space M) (terminal_events A)"
   unfolding terminal_events_def
 proof (simp add: sigma_algebra_iff2, safe)
   let ?A = "(\<Inter>n. sigma_sets (space M) (UNION {n..} A))"
-  interpret A: sigma_algebra "\<lparr>space = space M, sets = A i\<rparr>" for i by fact
+  interpret A: sigma_algebra "space M" "A i" for i by fact
   { fix X x assume "X \<in> ?A" "x \<in> X"
     then have "\<And>n. X \<in> sigma_sets (space M) (UNION {n..} A)" by auto
     from this[of 0] have "X \<in> sigma_sets (space M) (UNION UNIV A)" by simp
@@ -515,29 +506,29 @@
 lemma (in prob_space) kolmogorov_0_1_law:
   fixes A :: "nat \<Rightarrow> 'a set set"
   assumes A: "\<And>i. A i \<subseteq> events"
-  assumes "\<And>i::nat. sigma_algebra \<lparr>space = space M, sets = A i\<rparr>"
+  assumes "\<And>i::nat. sigma_algebra (space M) (A i)"
   assumes indep: "indep_sets A UNIV"
   and X: "X \<in> terminal_events A"
   shows "prob X = 0 \<or> prob X = 1"
 proof -
-  let ?D = "\<lparr> space = space M, sets = {D \<in> events. prob (X \<inter> D) = prob X * prob D} \<rparr>"
-  interpret A: sigma_algebra "\<lparr>space = space M, sets = A i\<rparr>" for i by fact
-  interpret T: sigma_algebra "\<lparr> space = space M, sets = terminal_events A \<rparr>"
+  let ?D = "{D \<in> events. prob (X \<inter> D) = prob X * prob D}"
+  interpret A: sigma_algebra "space M" "A i" for i by fact
+  interpret T: sigma_algebra "space M" "terminal_events A"
     by (rule sigma_algebra_terminal_events) fact
   have "X \<subseteq> space M" using T.space_closed X by auto
 
   have X_in: "X \<in> events"
     by (rule terminal_events_sets) fact+
 
-  interpret D: dynkin_system ?D
+  interpret D: dynkin_system "space M" ?D
   proof (rule dynkin_systemI)
-    fix D assume "D \<in> sets ?D" then show "D \<subseteq> space ?D"
+    fix D assume "D \<in> ?D" then show "D \<subseteq> space M"
       using sets_into_space by auto
   next
-    show "space ?D \<in> sets ?D"
+    show "space M \<in> ?D"
       using prob_space `X \<subseteq> space M` by (simp add: Int_absorb2)
   next
-    fix A assume A: "A \<in> sets ?D"
+    fix A assume A: "A \<in> ?D"
     have "prob (X \<inter> (space M - A)) = prob (X - (X \<inter> A))"
       using `X \<subseteq> space M` by (auto intro!: arg_cong[where f=prob])
     also have "\<dots> = prob X - prob (X \<inter> A)"
@@ -547,10 +538,10 @@
     also have "\<dots> = prob X * prob (space M - A)"
       using X_in A sets_into_space
       by (subst finite_measure_Diff) (auto simp: field_simps)
-    finally show "space ?D - A \<in> sets ?D"
+    finally show "space M - A \<in> ?D"
       using A `X \<subseteq> space M` by auto
   next
-    fix F :: "nat \<Rightarrow> 'a set" assume dis: "disjoint_family F" and "range F \<subseteq> sets ?D"
+    fix F :: "nat \<Rightarrow> 'a set" assume dis: "disjoint_family F" and "range F \<subseteq> ?D"
     then have F: "range F \<subseteq> events" "\<And>i. prob (X \<inter> F i) = prob X * prob (F i)"
       by auto
     have "(\<lambda>i. prob (X \<inter> F i)) sums prob (\<Union>i. X \<inter> F i)"
@@ -566,7 +557,7 @@
       by (intro sums_mult finite_measure_UNION F dis)
     ultimately have "prob (X \<inter> (\<Union>i. F i)) = prob X * prob (\<Union>i. F i)"
       by (auto dest!: sums_unique)
-    with F show "(\<Union>i. F i) \<in> sets ?D"
+    with F show "(\<Union>i. F i) \<in> ?D"
       by auto
   qed
 
@@ -579,7 +570,7 @@
       show "disjoint_family (bool_case {..n} {Suc n..})"
         unfolding disjoint_family_on_def by (auto split: bool.split)
       fix m
-      show "Int_stable \<lparr>space = space M, sets = A m\<rparr>"
+      show "Int_stable (A m)"
         unfolding Int_stable_def using A.Int by auto
     qed
     also have "(\<lambda>b. sigma_sets (space M) (\<Union>m\<in>bool_case {..n} {Suc n..} b. A m)) =
@@ -588,7 +579,7 @@
     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))"
       unfolding indep_set_def by simp
 
-    have "sigma_sets (space M) (\<Union>m\<in>{..n}. A m) \<subseteq> sets ?D"
+    have "sigma_sets (space M) (\<Union>m\<in>{..n}. A m) \<subseteq> ?D"
     proof (simp add: subset_eq, rule)
       fix D assume D: "D \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
       have "X \<in> sigma_sets (space M) (\<Union>m\<in>{Suc n..}. A m)"
@@ -597,22 +588,27 @@
       show "D \<in> events \<and> prob (X \<inter> D) = prob X * prob D"
         by (auto simp add: ac_simps)
     qed }
-  then have "(\<Union>n. sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) \<subseteq> sets ?D" (is "?A \<subseteq> _")
+  then have "(\<Union>n. sigma_sets (space M) (\<Union>m\<in>{..n}. A m)) \<subseteq> ?D" (is "?A \<subseteq> _")
     by auto
 
-  have "sigma \<lparr> space = space M, sets = ?A \<rparr> =
-    dynkin \<lparr> space = space M, sets = ?A \<rparr>" (is "sigma ?UA = dynkin ?UA")
+  note `X \<in> terminal_events A`
+  also {
+    have "\<And>n. sigma_sets (space M) (\<Union>i\<in>{n..}. A i) \<subseteq> sigma_sets (space M) ?A"
+      by (intro sigma_sets_subseteq UN_mono) auto
+   then have "terminal_events A \<subseteq> sigma_sets (space M) ?A"
+      unfolding terminal_events_def by auto }
+  also have "sigma_sets (space M) ?A = dynkin (space M) ?A"
   proof (rule sigma_eq_dynkin)
     { fix B n assume "B \<in> sigma_sets (space M) (\<Union>m\<in>{..n}. A m)"
       then have "B \<subseteq> space M"
-        by induct (insert A sets_into_space, auto) }
-    then show "sets ?UA \<subseteq> Pow (space ?UA)" by auto
-    show "Int_stable ?UA"
+        by induct (insert A sets_into_space[of _ M], auto) }
+    then show "?A \<subseteq> Pow (space M)" by auto
+    show "Int_stable ?A"
     proof (rule Int_stableI)
       fix a assume "a \<in> ?A" then guess n .. note a = this
       fix b assume "b \<in> ?A" then guess m .. note b = this
-      interpret Amn: sigma_algebra "sigma \<lparr>space = space M, sets = (\<Union>i\<in>{..max m n}. A i)\<rparr>"
-        using A sets_into_space by (intro sigma_algebra_sigma) auto
+      interpret Amn: sigma_algebra "space M" "sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
+        using A sets_into_space[of _ M] by (intro sigma_algebra_sigma_sets) auto
       have "sigma_sets (space M) (\<Union>i\<in>{..n}. A i) \<subseteq> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
         by (intro sigma_sets_subseteq UN_mono) auto
       with a have "a \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)" by auto
@@ -621,23 +617,13 @@
         by (intro sigma_sets_subseteq UN_mono) auto
       with b have "b \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)" by auto
       ultimately have "a \<inter> b \<in> sigma_sets (space M) (\<Union>i\<in>{..max m n}. A i)"
-        using Amn.Int[of a b] by (simp add: sets_sigma)
+        using Amn.Int[of a b] by simp
       then show "a \<inter> b \<in> (\<Union>n. sigma_sets (space M) (\<Union>i\<in>{..n}. A i))" by auto
     qed
   qed
-  moreover have "sets (dynkin ?UA) \<subseteq> sets ?D"
-  proof (rule D.dynkin_subset)
-    show "sets ?UA \<subseteq> sets ?D" using `?A \<subseteq> sets ?D` by auto
-  qed simp
-  ultimately have "sets (sigma ?UA) \<subseteq> sets ?D" by simp
-  moreover
-  have "\<And>n. sigma_sets (space M) (\<Union>i\<in>{n..}. A i) \<subseteq> sigma_sets (space M) ?A"
-    by (intro sigma_sets_subseteq UN_mono) (auto intro: sigma_sets.Basic)
-  then have "terminal_events A \<subseteq> sets (sigma ?UA)"
-    unfolding sets_sigma terminal_events_def by auto
-  moreover note `X \<in> terminal_events A`
-  ultimately have "X \<in> sets ?D" by auto
-  then show ?thesis by auto
+  also have "dynkin (space M) ?A \<subseteq> ?D"
+    using `?A \<subseteq> ?D` by (auto intro!: D.dynkin_subset)
+  finally show ?thesis by auto
 qed
 
 lemma (in prob_space) borel_0_1_law:
@@ -648,14 +634,14 @@
   show "\<And>i. sigma_sets (space M) {F i} \<subseteq> events"
     using F(1) sets_into_space
     by (subst sigma_sets_singleton) auto
-  { fix i show "sigma_algebra \<lparr>space = space M, sets = sigma_sets (space M) {F i}\<rparr>"
-      using sigma_algebra_sigma[of "\<lparr>space = space M, sets = {F i}\<rparr>"] F sets_into_space
-      by (auto simp add: sigma_def) }
+  { fix i show "sigma_algebra (space M) (sigma_sets (space M) {F i})"
+      using sigma_algebra_sigma_sets[of "{F i}" "space M"] F sets_into_space
+      by auto }
   show "indep_sets (\<lambda>i. sigma_sets (space M) {F i}) UNIV"
-  proof (rule indep_sets_sigma_sets)
+  proof (rule indep_sets_sigma)
     show "indep_sets (\<lambda>i. {F i}) UNIV"
       unfolding indep_sets_singleton_iff_indep_events by fact
-    fix i show "Int_stable \<lparr>space = space M, sets = {F i}\<rparr>"
+    fix i show "Int_stable {F i}"
       unfolding Int_stable_def by simp
   qed
   let ?Q = "\<lambda>n. \<Union>i\<in>{n..}. F i"
@@ -663,17 +649,17 @@
     unfolding terminal_events_def
   proof
     fix j
-    interpret S: sigma_algebra "sigma \<lparr> space = space M, sets = (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})\<rparr>"
+    interpret S: sigma_algebra "space M" "sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
       using order_trans[OF F(1) space_closed]
-      by (intro sigma_algebra_sigma) (simp add: sigma_sets_singleton subset_eq)
+      by (intro sigma_algebra_sigma_sets) (simp add: sigma_sets_singleton subset_eq)
     have "(\<Inter>n. ?Q n) = (\<Inter>n\<in>{j..}. ?Q n)"
       by (intro decseq_SucI INT_decseq_offset UN_mono) auto
-    also have "\<dots> \<in> sets (sigma \<lparr> space = space M, sets = (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})\<rparr>)"
+    also have "\<dots> \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
       using order_trans[OF F(1) space_closed]
       by (safe intro!: S.countable_INT S.countable_UN)
-         (auto simp: sets_sigma sigma_sets_singleton intro!: sigma_sets.Basic bexI)
+         (auto simp: sigma_sets_singleton intro!: sigma_sets.Basic bexI)
     finally show "(\<Inter>n. ?Q n) \<in> sigma_sets (space M) (\<Union>i\<in>{j..}. sigma_sets (space M) {F i})"
-      by (simp add: sets_sigma)
+      by simp
   qed
 qed
 
@@ -710,84 +696,84 @@
 lemma (in prob_space) indep_vars_finite:
   fixes I :: "'i set"
   assumes I: "I \<noteq> {}" "finite I"
-    and rv: "\<And>i. i \<in> I \<Longrightarrow> random_variable (sigma (M' i)) (X i)"
-    and Int_stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (M' i)"
-    and space: "\<And>i. i \<in> I \<Longrightarrow> space (M' i) \<in> sets (M' i)"
-  shows "indep_vars (\<lambda>i. sigma (M' i)) X I \<longleftrightarrow>
-    (\<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)))"
+    and M': "\<And>i. i \<in> I \<Longrightarrow> sets (M' i) = sigma_sets (space (M' i)) (E i)"
+    and rv: "\<And>i. i \<in> I \<Longrightarrow> random_variable (M' i) (X i)"
+    and Int_stable: "\<And>i. i \<in> I \<Longrightarrow> Int_stable (E i)"
+    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))"
+  shows "indep_vars M' X I \<longleftrightarrow>
+    (\<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)))"
 proof -
   from rv have X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> space M \<rightarrow> space (M' i)"
     unfolding measurable_def by simp
 
   { fix i assume "i\<in>I"
-    have "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (sigma (M' i))}
-      = sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
-      unfolding sigma_sets_vimage_commute[OF X, OF `i \<in> I`]
+    from closed[OF `i \<in> I`]
+    have "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}
+      = sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> E i}"
+      unfolding sigma_sets_vimage_commute[OF X, OF `i \<in> I`, symmetric] M'[OF `i \<in> I`]
       by (subst sigma_sets_sigma_sets_eq) auto }
-  note this[simp]
+  note sigma_sets_X = this
 
   { fix i assume "i\<in>I"
-    have "Int_stable \<lparr>space = space M, sets = {X i -` A \<inter> space M |A. A \<in> sets (M' i)}\<rparr>"
+    have "Int_stable {X i -` A \<inter> space M |A. A \<in> E i}"
     proof (rule Int_stableI)
-      fix a assume "a \<in> {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
-      then obtain A where "a = X i -` A \<inter> space M" "A \<in> sets (M' i)" by auto
+      fix a assume "a \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
+      then obtain A where "a = X i -` A \<inter> space M" "A \<in> E i" by auto
       moreover
-      fix b assume "b \<in> {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
-      then obtain B where "b = X i -` B \<inter> space M" "B \<in> sets (M' i)" by auto
+      fix b assume "b \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
+      then obtain B where "b = X i -` B \<inter> space M" "B \<in> E i" by auto
       moreover
       have "(X i -` A \<inter> space M) \<inter> (X i -` B \<inter> space M) = X i -` (A \<inter> B) \<inter> space M" by auto
       moreover note Int_stable[OF `i \<in> I`]
       ultimately
-      show "a \<inter> b \<in> {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
+      show "a \<inter> b \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
         by (auto simp del: vimage_Int intro!: exI[of _ "A \<inter> B"] dest: Int_stableD)
     qed }
-  note indep_sets_sigma_sets_iff[OF this, simp]
+  note indep_sets_X = indep_sets_sigma_sets_iff[OF this]
 
   { fix i assume "i \<in> I"
-    { fix A assume "A \<in> sets (M' i)"
-      then have "A \<in> sets (sigma (M' i))" by (auto simp: sets_sigma intro: sigma_sets.Basic)
+    { fix A assume "A \<in> E i"
+      with M'[OF `i \<in> I`] have "A \<in> sets (M' i)" by auto
       moreover
-      from rv[OF `i\<in>I`] have "X i \<in> measurable M (sigma (M' i))" by auto
+      from rv[OF `i\<in>I`] have "X i \<in> measurable M (M' i)" by auto
       ultimately
       have "X i -` A \<inter> space M \<in> sets M" by (auto intro: measurable_sets) }
     with X[OF `i\<in>I`] space[OF `i\<in>I`]
-    have "{X i -` A \<inter> space M |A. A \<in> sets (M' i)} \<subseteq> events"
-      "space M \<in> {X i -` A \<inter> space M |A. A \<in> sets (M' i)}"
+    have "{X i -` A \<inter> space M |A. A \<in> E i} \<subseteq> events"
+      "space M \<in> {X i -` A \<inter> space M |A. A \<in> E i}"
       by (auto intro!: exI[of _ "space (M' i)"]) }
-  note indep_sets_finite[OF I this, simp]
+  note indep_sets_finite_X = indep_sets_finite[OF I this]
 
-  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))) =
-    (\<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)))"
+  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))) =
+    (\<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)))"
     (is "?L = ?R")
   proof safe
-    fix A assume ?L and A: "A \<in> (\<Pi> i\<in>I. sets (M' i))"
+    fix A assume ?L and A: "A \<in> (\<Pi> i\<in>I. E i)"
     from `?L`[THEN bspec, of "\<lambda>i. X i -` A i \<inter> space M"] A `I \<noteq> {}`
     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))"
       by (auto simp add: Pi_iff)
   next
-    fix A assume ?R and A: "A \<in> (\<Pi> i\<in>I. {X i -` A \<inter> space M |A. A \<in> sets (M' i)})"
-    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
+    fix A assume ?R and A: "A \<in> (\<Pi> i\<in>I. {X i -` A \<inter> space M |A. A \<in> E i})"
+    from A have "\<forall>i\<in>I. \<exists>B. A i = X i -` B \<inter> space M \<and> B \<in> E i" by auto
     from bchoice[OF this] obtain B where B: "\<forall>i\<in>I. A i = X i -` B i \<inter> space M"
-      "B \<in> (\<Pi> i\<in>I. sets (M' i))" by auto
+      "B \<in> (\<Pi> i\<in>I. E i)" by auto
     from `?R`[THEN bspec, OF B(2)] B(1) `I \<noteq> {}`
     show "prob (INTER I A) = (\<Prod>j\<in>I. prob (A j))"
       by simp
   qed
   then show ?thesis using `I \<noteq> {}`
-    by (simp add: rv indep_vars_def)
+    by (simp add: rv indep_vars_def indep_sets_X sigma_sets_X indep_sets_finite_X cong: indep_sets_cong)
 qed
 
 lemma (in prob_space) indep_vars_compose:
   assumes "indep_vars M' X I"
-  assumes rv:
-    "\<And>i. i \<in> I \<Longrightarrow> sigma_algebra (N i)"
-    "\<And>i. i \<in> I \<Longrightarrow> Y i \<in> measurable (M' i) (N i)"
+  assumes rv: "\<And>i. i \<in> I \<Longrightarrow> Y i \<in> measurable (M' i) (N i)"
   shows "indep_vars N (\<lambda>i. Y i \<circ> X i) I"
   unfolding indep_vars_def
 proof
   from rv `indep_vars M' X I`
   show "\<forall>i\<in>I. random_variable (N i) (Y i \<circ> X i)"
-    by (auto intro!: measurable_comp simp: indep_vars_def)
+    by (auto simp: indep_vars_def)
 
   have "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
     using `indep_vars M' X I` by (simp add: indep_vars_def)
@@ -806,7 +792,7 @@
   qed
 qed
 
-lemma (in prob_space) indep_varsD:
+lemma (in prob_space) indep_varsD_finite:
   assumes X: "indep_vars M' X I"
   assumes I: "I \<noteq> {}" "finite I" "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M' i)"
   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))"
@@ -815,96 +801,134 @@
     using X by (auto simp: indep_vars_def)
   show "I \<subseteq> I" "I \<noteq> {}" "finite I" using I by auto
   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)}"
-    using I by (auto intro: sigma_sets.Basic)
+    using I by auto
 qed
 
-lemma (in prob_space) indep_distribution_eq_measure:
-  assumes I: "I \<noteq> {}" "finite I"
+lemma (in prob_space) indep_varsD:
+  assumes X: "indep_vars M' X I"
+  assumes I: "J \<noteq> {}" "finite J" "J \<subseteq> I" "\<And>i. i \<in> J \<Longrightarrow> A i \<in> sets (M' i)"
+  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))"
+proof (rule indep_setsD)
+  show "indep_sets (\<lambda>i. sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)}) I"
+    using X by (auto simp: indep_vars_def)
+  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)}"
+    using I by auto
+qed fact+
+
+lemma prod_algebra_cong:
+  assumes "I = J" and sets: "(\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets (N i))"
+  shows "prod_algebra I M = prod_algebra J N"
+proof -
+  have space: "\<And>i. i \<in> I \<Longrightarrow> space (M i) = space (N i)"
+    using sets_eq_imp_space_eq[OF sets] by auto
+  with sets show ?thesis unfolding `I = J`
+    by (intro antisym prod_algebra_mono) auto
+qed
+
+lemma space_in_prod_algebra:
+  "(\<Pi>\<^isub>E i\<in>I. space (M i)) \<in> prod_algebra I M"
+proof cases
+  assume "I = {}" then show ?thesis
+    by (auto simp add: prod_algebra_def image_iff prod_emb_def)
+next
+  assume "I \<noteq> {}"
+  then obtain i where "i \<in> I" by auto
+  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))"
+    by (auto simp: prod_emb_def Pi_iff)
+  also have "\<dots> \<in> prod_algebra I M"
+    using `i \<in> I` by (intro prod_algebraI) auto
+  finally show ?thesis .
+qed
+
+lemma (in prob_space) indep_vars_iff_distr_eq_PiM:
+  fixes I :: "'i set" and X :: "'i \<Rightarrow> 'a \<Rightarrow> 'b"
+  assumes "I \<noteq> {}"
   assumes rv: "\<And>i. random_variable (M' i) (X i)"
   shows "indep_vars M' X I \<longleftrightarrow>
-    (\<forall>A\<in>sets (\<Pi>\<^isub>M i\<in>I. (M' i \<lparr> measure := ereal\<circ>distribution (X i) \<rparr>)).
-      distribution (\<lambda>x. \<lambda>i\<in>I. X i x) A =
-      finite_measure.\<mu>' (\<Pi>\<^isub>M i\<in>I. (M' i \<lparr> measure := ereal\<circ>distribution (X i) \<rparr>)) A)"
-    (is "_ \<longleftrightarrow> (\<forall>X\<in>_. distribution ?D X = finite_measure.\<mu>' (Pi\<^isub>M I ?M) X)")
+    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))"
 proof -
-  interpret M': prob_space "?M i" for i
-    using rv by (rule distribution_prob_space)
-  interpret P: finite_product_prob_space ?M I
-    proof qed fact
+  let ?P = "\<Pi>\<^isub>M i\<in>I. M' i"
+  let ?X = "\<lambda>x. \<lambda>i\<in>I. X i x"
+  let ?D = "distr M ?P ?X"
+  have X: "random_variable ?P ?X" by (intro measurable_restrict rv)
+  interpret D: prob_space ?D by (intro prob_space_distr X)
 
-  let ?D' = "(Pi\<^isub>M I ?M) \<lparr> measure := ereal \<circ> distribution ?D \<rparr>"
-  have "random_variable P.P ?D"
-    using `finite I` rv by (intro random_variable_restrict) auto
-  then interpret D: prob_space ?D'
-    by (rule distribution_prob_space)
-
+  let ?D' = "\<lambda>i. distr M (M' i) (X i)"
+  let ?P' = "\<Pi>\<^isub>M i\<in>I. distr M (M' i) (X i)"
+  interpret D': prob_space "?D' i" for i by (intro prob_space_distr rv)
+  interpret P: product_prob_space ?D' I ..
+    
   show ?thesis
-  proof (intro iffI ballI)
+  proof
     assume "indep_vars M' X I"
-    fix A assume "A \<in> sets P.P"
-    moreover
-    have "D.prob A = P.prob A"
-    proof (rule prob_space_unique_Int_stable)
-      show "prob_space ?D'" by unfold_locales
-      show "prob_space (Pi\<^isub>M I ?M)" by unfold_locales
-      show "Int_stable P.G" using M'.Int
-        by (intro Int_stable_product_algebra_generator) (simp add: Int_stable_def)
-      show "space P.G \<in> sets P.G"
-        using M'.top by (simp add: product_algebra_generator_def)
-      show "space ?D' = space P.G"  "sets ?D' = sets (sigma P.G)"
-        by (simp_all add: product_algebra_def product_algebra_generator_def sets_sigma)
-      show "space P.P = space P.G" "sets P.P = sets (sigma P.G)"
-        by (simp_all add: product_algebra_def)
-      show "A \<in> sets (sigma P.G)"
-        using `A \<in> sets P.P` by (simp add: product_algebra_def)
+    show "?D = ?P'"
+    proof (rule measure_eqI_generator_eq)
+      show "Int_stable (prod_algebra I M')"
+        by (rule Int_stable_prod_algebra)
+      show "prod_algebra I M' \<subseteq> Pow (space ?P)"
+        using prod_algebra_sets_into_space by (simp add: space_PiM)
+      show "sets ?D = sigma_sets (space ?P) (prod_algebra I M')"
+        by (simp add: sets_PiM space_PiM)
+      show "sets ?P' = sigma_sets (space ?P) (prod_algebra I M')"
+        by (simp add: sets_PiM space_PiM cong: prod_algebra_cong)
+      let ?A = "\<lambda>i. \<Pi>\<^isub>E i\<in>I. space (M' i)"
+      show "range ?A \<subseteq> prod_algebra I M'" "incseq ?A" "(\<Union>i. ?A i) = space (Pi\<^isub>M I M')"
+        by (auto simp: space_PiM intro!: space_in_prod_algebra cong: prod_algebra_cong)
+      { fix i show "emeasure ?D (\<Pi>\<^isub>E i\<in>I. space (M' i)) \<noteq> \<infinity>" by auto }
+    next
+      fix E assume E: "E \<in> prod_algebra I M'"
+      from prod_algebraE[OF E] guess J Y . note J = this
 
-      fix E assume E: "E \<in> sets P.G"
-      then have "E \<in> sets P.P"
-        by (simp add: sets_sigma sigma_sets.Basic product_algebra_def)
-      then have "D.prob E = distribution ?D E"
-        unfolding D.\<mu>'_def by simp
-      also
-      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)"
-        by (auto simp: product_algebra_generator_def)
-      with `I \<noteq> {}` have "distribution ?D E = prob (\<Inter>i\<in>I. X i -` F i \<inter> space M)"
-        using `I \<noteq> {}` by (auto intro!: arg_cong[where f=prob] simp: Pi_iff distribution_def)
-      also have "\<dots> = (\<Prod>i\<in>I. prob (X i -` F i \<inter> space M))"
-        using `indep_vars M' X I` I F by (rule indep_varsD)
-      also have "\<dots> = P.prob E"
-        using F by (simp add: `E = Pi\<^isub>E I F` P.prob_times M'.\<mu>'_def distribution_def)
-      finally show "D.prob E = P.prob E" .
+      from E have "E \<in> sets ?P" by (auto simp: sets_PiM)
+      then have "emeasure ?D E = emeasure M (?X -` E \<inter> space M)"
+        by (simp add: emeasure_distr X)
+      also have "?X -` E \<inter> space M = (\<Inter>i\<in>J. X i -` Y i \<inter> space M)"
+        using J `I \<noteq> {}` measurable_space[OF rv] by (auto simp: prod_emb_def Pi_iff split: split_if_asm)
+      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))"
+        using `indep_vars M' X I` J `I \<noteq> {}` using indep_varsD[of M' X I J]
+        by (auto simp: emeasure_eq_measure setprod_ereal)
+      also have "\<dots> = (\<Prod> i\<in>J. emeasure (?D' i) (Y i))"
+        using rv J by (simp add: emeasure_distr)
+      also have "\<dots> = emeasure ?P' E"
+        using P.emeasure_PiM_emb[of J Y] J by (simp add: prod_emb_def)
+      finally show "emeasure ?D E = emeasure ?P' E" .
     qed
-    ultimately show "distribution ?D A = P.prob A"
-      by (simp add: D.\<mu>'_def)
   next
-    assume eq: "\<forall>A\<in>sets P.P. distribution ?D A = P.prob A"
-    have [simp]: "\<And>i. sigma (M' i) = M' i"
-      using rv by (intro sigma_algebra.sigma_eq) simp
-    have "indep_vars (\<lambda>i. sigma (M' i)) X I"
-    proof (subst indep_vars_finite[OF I])
-      fix i assume [simp]: "i \<in> I"
-      show "random_variable (sigma (M' i)) (X i)"
-        using rv[of i] by simp
-      show "Int_stable (M' i)" "space (M' i) \<in> sets (M' i)"
-        using M'.Int[of _ i] M'.top by (auto simp: Int_stable_def)
+    assume "?D = ?P'"
+    show "indep_vars M' X I" unfolding indep_vars_def
+    proof (intro conjI indep_setsI ballI rv)
+      fix i show "sigma_sets (space M) {X i -` A \<inter> space M |A. A \<in> sets (M' i)} \<subseteq> events"
+        by (auto intro!: sigma_sets_subset measurable_sets rv)
     next
-      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))"
+      fix J Y' assume J: "J \<noteq> {}" "J \<subseteq> I" "finite J"
+      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)}"
+      have "\<forall>j\<in>J. \<exists>Y. Y' j = X j -` Y \<inter> space M \<and> Y \<in> sets (M' j)"
       proof
-        fix A assume A: "A \<in> (\<Pi> i\<in>I. sets (M' i))"
-        then have A_in_P: "(Pi\<^isub>E I A) \<in> sets P.P"
-          by (auto intro!: product_algebraI)
-        have "prob (\<Inter>j\<in>I. X j -` A j \<inter> space M) = distribution ?D (Pi\<^isub>E I A)"
-          using `I \<noteq> {}`by (auto intro!: arg_cong[where f=prob] simp: Pi_iff distribution_def)
-        also have "\<dots> = P.prob (Pi\<^isub>E I A)" using A_in_P eq by simp
-        also have "\<dots> = (\<Prod>i\<in>I. M'.prob i (A i))"
-          using A by (intro P.prob_times) auto
-        also have "\<dots> = (\<Prod>i\<in>I. prob (X i -` A i \<inter> space M))"
-          using A by (auto intro!: setprod_cong simp: M'.\<mu>'_def Pi_iff distribution_def)
-        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))" .
+        fix j assume "j \<in> J"
+        from Y'[rule_format, OF this] rv[of j]
+        show "\<exists>Y. Y' j = X j -` Y \<inter> space M \<and> Y \<in> sets (M' j)"
+          by (subst (asm) sigma_sets_vimage_commute[symmetric, of _ _ "space (M' j)"])
+             (auto dest: measurable_space simp: sigma_sets_eq)
       qed
+      from bchoice[OF this] obtain Y where
+        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
+      let ?E = "prod_emb I M' J (Pi\<^isub>E J Y)"
+      from Y have "(\<Inter>j\<in>J. Y' j) = ?X -` ?E \<inter> space M"
+        using J `I \<noteq> {}` measurable_space[OF rv] by (auto simp: prod_emb_def Pi_iff split: split_if_asm)
+      then have "emeasure M (\<Inter>j\<in>J. Y' j) = emeasure M (?X -` ?E \<inter> space M)"
+        by simp
+      also have "\<dots> = emeasure ?D ?E"
+        using Y  J by (intro emeasure_distr[symmetric] X sets_PiM_I) auto
+      also have "\<dots> = emeasure ?P' ?E"
+        using `?D = ?P'` by simp
+      also have "\<dots> = (\<Prod> i\<in>J. emeasure (?D' i) (Y i))"
+        using P.emeasure_PiM_emb[of J Y] J Y by (simp add: prod_emb_def)
+      also have "\<dots> = (\<Prod> i\<in>J. emeasure M (Y' i))"
+        using rv J Y by (simp add: emeasure_distr)
+      finally have "emeasure M (\<Inter>j\<in>J. Y' j) = (\<Prod> i\<in>J. emeasure M (Y' i))" .
+      then show "prob (\<Inter>j\<in>J. Y' j) = (\<Prod> i\<in>J. prob (Y' i))"
+        by (auto simp: emeasure_eq_measure setprod_ereal)
     qed
-    then show "indep_vars M' X I"
-      by simp
   qed
 qed
 
@@ -936,56 +960,188 @@
     unfolding UNIV_bool by auto
 qed
 
-lemma (in prob_space) indep_var_distributionD:
-  assumes indep: "indep_var S X T Y"
-  defines "P \<equiv> S\<lparr>measure := ereal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
-  assumes "A \<in> sets P"
-  shows "joint_distribution X Y A = finite_measure.\<mu>' P A"
-proof -
-  from indep have rvs: "random_variable S X" "random_variable T Y"
-    by (blast dest: indep_var_rv1 indep_var_rv2)+
+lemma measurable_bool_case[simp, intro]:
+  "(\<lambda>(x, y). bool_case x y) \<in> measurable (M \<Otimes>\<^isub>M N) (Pi\<^isub>M UNIV (bool_case M N))"
+    (is "?f \<in> measurable ?B ?P")
+proof (rule measurable_PiM_single)
+  show "?f \<in> space ?B \<rightarrow> (\<Pi>\<^isub>E i\<in>UNIV. space (bool_case M N i))"
+    by (auto simp: space_pair_measure extensional_def split: bool.split)
+  fix i A assume "A \<in> sets (case i of True \<Rightarrow> M | False \<Rightarrow> N)"
+  moreover then have "{\<omega> \<in> space (M \<Otimes>\<^isub>M N). prod_case bool_case \<omega> i \<in> A}
+    = (case i of True \<Rightarrow> A \<times> space N | False \<Rightarrow> space M \<times> A)" 
+    by (auto simp: space_pair_measure split: bool.split dest!: sets_into_space)
+  ultimately show "{\<omega> \<in> space (M \<Otimes>\<^isub>M N). prod_case bool_case \<omega> i \<in> A} \<in> sets ?B"
+    by (auto split: bool.split)
+qed
+
+lemma borel_measurable_indicator':
+  "A \<in> sets N \<Longrightarrow> f \<in> measurable M N \<Longrightarrow> (\<lambda>x. indicator A (f x)) \<in> borel_measurable M"
+  using measurable_comp[OF _ borel_measurable_indicator, of f M N A] by (auto simp add: comp_def)
+
+lemma (in product_sigma_finite) distr_component:
+  "distr (M i) (Pi\<^isub>M {i} M) (\<lambda>x. \<lambda>i\<in>{i}. x) = Pi\<^isub>M {i} M" (is "?D = ?P")
+proof (intro measure_eqI[symmetric])
+  interpret I: finite_product_sigma_finite M "{i}" by default simp
+
+  have eq: "\<And>x. x \<in> extensional {i} \<Longrightarrow> (\<lambda>j\<in>{i}. x i) = x"
+    by (auto simp: extensional_def restrict_def)
+
+  fix A assume A: "A \<in> sets ?P"
+  then have "emeasure ?P A = (\<integral>\<^isup>+x. indicator A x \<partial>?P)" 
+    by simp
+  also have "\<dots> = (\<integral>\<^isup>+x. indicator ((\<lambda>x. \<lambda>i\<in>{i}. x) -` A \<inter> space (M i)) x \<partial>M i)" 
+    apply (subst product_positive_integral_singleton[symmetric])
+    apply (force intro!: measurable_restrict measurable_sets A)
+    apply (auto intro!: positive_integral_cong simp: space_PiM indicator_def simp: eq)
+    done
+  also have "\<dots> = emeasure (M i) ((\<lambda>x. \<lambda>i\<in>{i}. x) -` A \<inter> space (M i))"
+    by (force intro!: measurable_restrict measurable_sets A positive_integral_indicator)
+  also have "\<dots> = emeasure ?D A"
+    using A by (auto intro!: emeasure_distr[symmetric] measurable_restrict) 
+  finally show "emeasure (Pi\<^isub>M {i} M) A = emeasure ?D A" .
+qed simp
 
-  let ?S = "S\<lparr>measure := ereal\<circ>distribution X\<rparr>"
-  let ?T = "T\<lparr>measure := ereal\<circ>distribution Y\<rparr>"
-  interpret X: prob_space ?S by (rule distribution_prob_space) fact
-  interpret Y: prob_space ?T by (rule distribution_prob_space) fact
-  interpret XY: pair_prob_space ?S ?T by default
+lemma pair_measure_eqI:
+  assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
+  assumes sets: "sets (M1 \<Otimes>\<^isub>M M2) = sets M"
+  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)"
+  shows "M1 \<Otimes>\<^isub>M M2 = M"
+proof -
+  interpret M1: sigma_finite_measure M1 by fact
+  interpret M2: sigma_finite_measure M2 by fact
+  interpret pair_sigma_finite M1 M2 by default
+  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
+  let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
+  let ?P = "M1 \<Otimes>\<^isub>M M2"
+  show ?thesis
+  proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
+    show "?E \<subseteq> Pow (space ?P)"
+      using space_closed[of M1] space_closed[of M2] by (auto simp: space_pair_measure)
+    show "sets ?P = sigma_sets (space ?P) ?E"
+      by (simp add: sets_pair_measure space_pair_measure)
+    then show "sets M = sigma_sets (space ?P) ?E"
+      using sets[symmetric] by simp
+  next
+    show "range F \<subseteq> ?E" "incseq F" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
+      using F by (auto simp: space_pair_measure)
+  next
+    fix X assume "X \<in> ?E"
+    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
+    then have "emeasure ?P X = emeasure M1 A * emeasure M2 B"
+       by (simp add: emeasure_pair_measure_Times)
+    also have "\<dots> = emeasure M (A \<times> B)"
+      using A B emeasure by auto
+    finally show "emeasure ?P X = emeasure M X"
+      by simp
+  qed
+qed
 
-  let ?J = "XY.P\<lparr> measure := ereal \<circ> joint_distribution X Y \<rparr>"
-  interpret J: prob_space ?J
-    by (rule joint_distribution_prob_space) (simp_all add: rvs)
+lemma pair_measure_eq_distr_PiM:
+  fixes M1 :: "'a measure" and M2 :: "'a measure"
+  assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
+  shows "(M1 \<Otimes>\<^isub>M M2) = distr (Pi\<^isub>M UNIV (bool_case M1 M2)) (M1 \<Otimes>\<^isub>M M2) (\<lambda>x. (x True, x False))"
+    (is "?P = ?D")
+proof (rule pair_measure_eqI[OF assms])
+  interpret B: product_sigma_finite "bool_case M1 M2"
+    unfolding product_sigma_finite_def using assms by (auto split: bool.split)
+  let ?B = "Pi\<^isub>M UNIV (bool_case M1 M2)"
 
-  have "finite_measure.\<mu>' (XY.P\<lparr> measure := ereal \<circ> joint_distribution X Y \<rparr>) A = XY.\<mu>' A"
-  proof (rule prob_space_unique_Int_stable)
-    show "Int_stable (pair_measure_generator ?S ?T)" (is "Int_stable ?P")
-      by fact
-    show "space ?P \<in> sets ?P"
-      unfolding space_pair_measure[simplified pair_measure_def space_sigma]
-      using X.top Y.top by (auto intro!: pair_measure_generatorI)
+  have [simp]: "fst \<circ> (\<lambda>x. (x True, x False)) = (\<lambda>x. x True)" "snd \<circ> (\<lambda>x. (x True, x False)) = (\<lambda>x. x False)"
+    by auto
+  fix A B assume A: "A \<in> sets M1" and B: "B \<in> sets M2"
+  have "emeasure M1 A * emeasure M2 B = (\<Prod> i\<in>UNIV. emeasure (bool_case M1 M2 i) (bool_case A B i))"
+    by (simp add: UNIV_bool ac_simps)
+  also have "\<dots> = emeasure ?B (Pi\<^isub>E UNIV (bool_case A B))"
+    using A B by (subst B.emeasure_PiM) (auto split: bool.split)
+  also have "Pi\<^isub>E UNIV (bool_case A B) = (\<lambda>x. (x True, x False)) -` (A \<times> B) \<inter> space ?B"
+    using A[THEN sets_into_space] B[THEN sets_into_space]
+    by (auto simp: Pi_iff all_bool_eq space_PiM split: bool.split)
+  finally show "emeasure M1 A * emeasure M2 B = emeasure ?D (A \<times> B)"
+    using A B
+      measurable_component_singleton[of True UNIV "bool_case M1 M2"]
+      measurable_component_singleton[of False UNIV "bool_case M1 M2"]
+    by (subst emeasure_distr) (auto simp: measurable_pair_iff)
+qed simp
 
-    show "prob_space ?J" by unfold_locales
-    show "space ?J = space ?P"
-      by (simp add: pair_measure_generator_def space_pair_measure)
-    show "sets ?J = sets (sigma ?P)"
-      by (simp add: pair_measure_def)
+lemma measurable_Pair:
+  assumes rvs: "X \<in> measurable M S" "Y \<in> measurable M T"
+  shows "(\<lambda>x. (X x, Y x)) \<in> measurable M (S \<Otimes>\<^isub>M T)"
+proof -
+  have [simp]: "fst \<circ> (\<lambda>x. (X x, Y x)) = (\<lambda>x. X x)" "snd \<circ> (\<lambda>x. (X x, Y x)) = (\<lambda>x. Y x)"
+    by auto
+  show " (\<lambda>x. (X x, Y x)) \<in> measurable M (S \<Otimes>\<^isub>M T)"
+    by (auto simp: measurable_pair_iff rvs)
+qed
+
+lemma (in prob_space) indep_var_distribution_eq:
+  "indep_var S X T Y \<longleftrightarrow> random_variable S X \<and> random_variable T Y \<and>
+    distr M S X \<Otimes>\<^isub>M distr M T Y = distr M (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))" (is "_ \<longleftrightarrow> _ \<and> _ \<and> ?S \<Otimes>\<^isub>M ?T = ?J")
+proof safe
+  assume "indep_var S X T Y"
+  then show rvs: "random_variable S X" "random_variable T Y"
+    by (blast dest: indep_var_rv1 indep_var_rv2)+
+  then have XY: "random_variable (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))"
+    by (rule measurable_Pair)
+
+  interpret X: prob_space ?S by (rule prob_space_distr) fact
+  interpret Y: prob_space ?T by (rule prob_space_distr) fact
+  interpret XY: pair_prob_space ?S ?T ..
+  show "?S \<Otimes>\<^isub>M ?T = ?J"
+  proof (rule pair_measure_eqI)
+    show "sigma_finite_measure ?S" ..
+    show "sigma_finite_measure ?T" ..
 
-    show "prob_space XY.P" by unfold_locales
-    show "space XY.P = space ?P" "sets XY.P = sets (sigma ?P)"
-      by (simp_all add: pair_measure_generator_def pair_measure_def)
+    fix A B assume A: "A \<in> sets ?S" and B: "B \<in> sets ?T"
+    have "emeasure ?J (A \<times> B) = emeasure M ((\<lambda>x. (X x, Y x)) -` (A \<times> B) \<inter> space M)"
+      using A B by (intro emeasure_distr[OF XY]) auto
+    also have "\<dots> = emeasure M (X -` A \<inter> space M) * emeasure M (Y -` B \<inter> space M)"
+      using indep_varD[OF `indep_var S X T Y`, of A B] A B by (simp add: emeasure_eq_measure)
+    also have "\<dots> = emeasure ?S A * emeasure ?T B"
+      using rvs A B by (simp add: emeasure_distr)
+    finally show "emeasure ?S A * emeasure ?T B = emeasure ?J (A \<times> B)" by simp
+  qed simp
+next
+  assume rvs: "random_variable S X" "random_variable T Y"
+  then have XY: "random_variable (S \<Otimes>\<^isub>M T) (\<lambda>x. (X x, Y x))"
+    by (rule measurable_Pair)
 
-    show "A \<in> sets (sigma ?P)"
-      using `A \<in> sets P` unfolding P_def pair_measure_def by simp
+  let ?S = "distr M S X" and ?T = "distr M T Y"
+  interpret X: prob_space ?S by (rule prob_space_distr) fact
+  interpret Y: prob_space ?T by (rule prob_space_distr) fact
+  interpret XY: pair_prob_space ?S ?T ..
+
+  assume "?S \<Otimes>\<^isub>M ?T = ?J"
 
-    fix X assume "X \<in> sets ?P"
-    then obtain A B where "A \<in> sets S" "B \<in> sets T" "X = A \<times> B"
-      by (auto simp: sets_pair_measure_generator)
-    then show "J.\<mu>' X = XY.\<mu>' X"
-      unfolding J.\<mu>'_def XY.\<mu>'_def using indep
-      by (simp add: XY.pair_measure_times)
-         (simp add: distribution_def indep_varD)
+  { fix S and X
+    have "Int_stable {X -` A \<inter> space M |A. A \<in> sets S}"
+    proof (safe intro!: Int_stableI)
+      fix A B assume "A \<in> sets S" "B \<in> sets S"
+      then show "\<exists>C. (X -` A \<inter> space M) \<inter> (X -` B \<inter> space M) = (X -` C \<inter> space M) \<and> C \<in> sets S"
+        by (intro exI[of _ "A \<inter> B"]) auto
+    qed }
+  note Int_stable = this
+
+  show "indep_var S X T Y" unfolding indep_var_eq
+  proof (intro conjI indep_set_sigma_sets Int_stable rvs)
+    show "indep_set {X -` A \<inter> space M |A. A \<in> sets S} {Y -` A \<inter> space M |A. A \<in> sets T}"
+    proof (safe intro!: indep_setI)
+      { fix A assume "A \<in> sets S" then show "X -` A \<inter> space M \<in> sets M"
+        using `X \<in> measurable M S` by (auto intro: measurable_sets) }
+      { fix A assume "A \<in> sets T" then show "Y -` A \<inter> space M \<in> sets M"
+        using `Y \<in> measurable M T` by (auto intro: measurable_sets) }
+    next
+      fix A B assume ab: "A \<in> sets S" "B \<in> sets T"
+      then have "ereal (prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M))) = emeasure ?J (A \<times> B)"
+        using XY by (auto simp add: emeasure_distr emeasure_eq_measure intro!: arg_cong[where f="prob"])
+      also have "\<dots> = emeasure (?S \<Otimes>\<^isub>M ?T) (A \<times> B)"
+        unfolding `?S \<Otimes>\<^isub>M ?T = ?J` ..
+      also have "\<dots> = emeasure ?S A * emeasure ?T B"
+        using ab by (simp add: XY.emeasure_pair_measure_Times)
+      finally show "prob ((X -` A \<inter> space M) \<inter> (Y -` B \<inter> space M)) =
+        prob (X -` A \<inter> space M) * prob (Y -` B \<inter> space M)"
+        using rvs ab by (simp add: emeasure_eq_measure emeasure_distr)
+    qed
   qed
-  then show ?thesis
-    using `A \<in> sets P` unfolding P_def J.\<mu>'_def XY.\<mu>'_def by simp
 qed
 
 end
--- a/src/HOL/Probability/Infinite_Product_Measure.thy	Mon Apr 23 12:23:23 2012 +0100
+++ b/src/HOL/Probability/Infinite_Product_Measure.thy	Mon Apr 23 12:14:35 2012 +0200
@@ -5,9 +5,49 @@
 header {*Infinite Product Measure*}
 
 theory Infinite_Product_Measure
-  imports Probability_Measure
+  imports Probability_Measure Caratheodory
 begin
 
+lemma sigma_sets_mono: assumes "A \<subseteq> sigma_sets X B" shows "sigma_sets X A \<subseteq> sigma_sets X B"
+proof
+  fix x assume "x \<in> sigma_sets X A" then show "x \<in> sigma_sets X B"
+    by induct (insert `A \<subseteq> sigma_sets X B`, auto intro: sigma_sets.intros)
+qed
+
+lemma sigma_sets_mono': assumes "A \<subseteq> B" shows "sigma_sets X A \<subseteq> sigma_sets X B"
+proof
+  fix x assume "x \<in> sigma_sets X A" then show "x \<in> sigma_sets X B"
+    by induct (insert `A \<subseteq> B`, auto intro: sigma_sets.intros)
+qed
+
+lemma sigma_sets_superset_generator: "A \<subseteq> sigma_sets X A"
+  by (auto intro: sigma_sets.Basic)
+
+lemma (in product_sigma_finite)
+  assumes IJ: "I \<inter> J = {}" "finite I" "finite J" and A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
+  shows emeasure_fold_integral:
+    "emeasure (Pi\<^isub>M (I \<union> J) M) A = (\<integral>\<^isup>+x. emeasure (Pi\<^isub>M J M) (merge I x J -` A \<inter> space (Pi\<^isub>M J M)) \<partial>Pi\<^isub>M I M)" (is ?I)
+    and emeasure_fold_measurable:
+    "(\<lambda>x. emeasure (Pi\<^isub>M J M) (merge I x J -` A \<inter> space (Pi\<^isub>M J M))) \<in> borel_measurable (Pi\<^isub>M I M)" (is ?B)
+proof -
+  interpret I: finite_product_sigma_finite M I by default fact
+  interpret J: finite_product_sigma_finite M J by default fact
+  interpret IJ: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" ..
+  have merge: "(\<lambda>(x, y). merge I x J y) -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) \<in> sets (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
+    by (intro measurable_sets[OF _ A] measurable_merge assms)
+
+  show ?I
+    apply (subst distr_merge[symmetric, OF IJ])
+    apply (subst emeasure_distr[OF measurable_merge[OF IJ(1)] A])
+    apply (subst IJ.emeasure_pair_measure_alt[OF merge])
+    apply (auto intro!: positive_integral_cong arg_cong2[where f=emeasure] simp: space_pair_measure)
+    done
+
+  show ?B
+    using IJ.measurable_emeasure_Pair1[OF merge]
+    by (simp add: vimage_compose[symmetric] comp_def space_pair_measure cong: measurable_cong)
+qed
+
 lemma restrict_extensional_sub[intro]: "A \<subseteq> B \<Longrightarrow> restrict f A \<in> extensional B"
   unfolding restrict_def extensional_def by auto
 
@@ -41,189 +81,178 @@
   qed
 qed
 
-lemma (in product_prob_space) measure_preserving_restrict:
+lemma prod_algebraI_finite:
+  "finite I \<Longrightarrow> (\<forall>i\<in>I. E i \<in> sets (M i)) \<Longrightarrow> (Pi\<^isub>E I E) \<in> prod_algebra I M"
+  using prod_algebraI[of I I E M] prod_emb_PiE_same_index[of I E M, OF sets_into_space] by simp
+
+lemma Int_stable_PiE: "Int_stable {Pi\<^isub>E J E | E. \<forall>i\<in>I. E i \<in> sets (M i)}"
+proof (safe intro!: Int_stableI)
+  fix E F assume "\<forall>i\<in>I. E i \<in> sets (M i)" "\<forall>i\<in>I. F i \<in> sets (M i)"
+  then show "\<exists>G. Pi\<^isub>E J E \<inter> Pi\<^isub>E J F = Pi\<^isub>E J G \<and> (\<forall>i\<in>I. G i \<in> sets (M i))"
+    by (auto intro!: exI[of _ "\<lambda>i. E i \<inter> F i"])
+qed
+
+lemma prod_emb_trans[simp]:
+  "J \<subseteq> K \<Longrightarrow> K \<subseteq> L \<Longrightarrow> prod_emb L M K (prod_emb K M J X) = prod_emb L M J X"
+  by (auto simp add: Int_absorb1 prod_emb_def)
+
+lemma prod_emb_Pi:
+  assumes "X \<in> (\<Pi> j\<in>J. sets (M j))" "J \<subseteq> K"
+  shows "prod_emb K M J (Pi\<^isub>E J X) = (\<Pi>\<^isub>E i\<in>K. if i \<in> J then X i else space (M i))"
+  using assms space_closed
+  by (auto simp: prod_emb_def Pi_iff split: split_if_asm) blast+
+
+lemma prod_emb_id:
+  "B \<subseteq> (\<Pi>\<^isub>E i\<in>L. space (M i)) \<Longrightarrow> prod_emb L M L B = B"
+  by (auto simp: prod_emb_def Pi_iff subset_eq extensional_restrict)
+
+lemma measurable_prod_emb[intro, simp]:
+  "J \<subseteq> L \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> prod_emb L M J X \<in> sets (Pi\<^isub>M L M)"
+  unfolding prod_emb_def space_PiM[symmetric]
+  by (auto intro!: measurable_sets measurable_restrict measurable_component_singleton)
+
+lemma measurable_restrict_subset: "J \<subseteq> L \<Longrightarrow> (\<lambda>f. restrict f J) \<in> measurable (Pi\<^isub>M L M) (Pi\<^isub>M J M)"
+  by (intro measurable_restrict measurable_component_singleton) auto
+
+lemma (in product_prob_space) distr_restrict:
   assumes "J \<noteq> {}" "J \<subseteq> K" "finite K"
-  shows "(\<lambda>f. restrict f J) \<in> measure_preserving (\<Pi>\<^isub>M i\<in>K. M i) (\<Pi>\<^isub>M i\<in>J. M i)" (is "?R \<in> _")
-proof -
-  interpret K: finite_product_prob_space M K by default fact
-  have J: "J \<noteq> {}" "finite J" using assms by (auto simp add: finite_subset)
-  interpret J: finite_product_prob_space M J
-    by default (insert J, auto)
-  from J.sigma_finite_pairs guess F .. note F = this
-  then have [simp,intro]: "\<And>k i. k \<in> J \<Longrightarrow> F k i \<in> sets (M k)"
-    by auto
-  let ?F = "\<lambda>i. \<Pi>\<^isub>E k\<in>J. F k i"
-  let ?J = "product_algebra_generator J M \<lparr> measure := measure (Pi\<^isub>M J M) \<rparr>"
-  have "?R \<in> measure_preserving (\<Pi>\<^isub>M i\<in>K. M i) (sigma ?J)"
-  proof (rule K.measure_preserving_Int_stable)
-    show "Int_stable ?J"
-      by (auto simp: Int_stable_def product_algebra_generator_def PiE_Int)
-    show "range ?F \<subseteq> sets ?J" "incseq ?F" "(\<Union>i. ?F i) = space ?J"
-      using F by auto
-    show "\<And>i. measure ?J (?F i) \<noteq> \<infinity>"
-      using F by (simp add: J.measure_times setprod_PInf)
-    have "measure_space (Pi\<^isub>M J M)" by default
-    then show "measure_space (sigma ?J)"
-      by (simp add: product_algebra_def sigma_def)
-    show "?R \<in> measure_preserving (Pi\<^isub>M K M) ?J"
-    proof (simp add: measure_preserving_def measurable_def product_algebra_generator_def del: vimage_Int,
-           safe intro!: restrict_extensional)
-      fix x k assume "k \<in> J" "x \<in> (\<Pi> i\<in>K. space (M i))"
-      then show "x k \<in> space (M k)" using `J \<subseteq> K` by auto
-    next
-      fix E assume "E \<in> (\<Pi> i\<in>J. sets (M i))"
-      then have E: "\<And>j. j \<in> J \<Longrightarrow> E j \<in> sets (M j)" by auto
-      then have *: "?R -` Pi\<^isub>E J E \<inter> (\<Pi>\<^isub>E i\<in>K. space (M i)) = (\<Pi>\<^isub>E i\<in>K. if i \<in> J then E i else space (M i))"
-        (is "?X = Pi\<^isub>E K ?M")
-        using `J \<subseteq> K` sets_into_space by (auto simp: Pi_iff split: split_if_asm) blast+
-      with E show "?X \<in> sets (Pi\<^isub>M K M)"
-        by (auto intro!: product_algebra_generatorI)
-      have "measure (Pi\<^isub>M J M) (Pi\<^isub>E J E) = (\<Prod>i\<in>J. measure (M i) (?M i))"
-        using E by (simp add: J.measure_times)
-      also have "\<dots> = measure (Pi\<^isub>M K M) ?X"
-        unfolding * using E `finite K` `J \<subseteq> K`
-        by (auto simp: K.measure_times M.measure_space_1
-                 cong del: setprod_cong
-                 intro!: setprod_mono_one_left)
-      finally show "measure (Pi\<^isub>M J M) (Pi\<^isub>E J E) = measure (Pi\<^isub>M K M) ?X" .
-    qed
-  qed
-  then show ?thesis
-    by (simp add: product_algebra_def sigma_def)
+  shows "(\<Pi>\<^isub>M i\<in>J. M i) = distr (\<Pi>\<^isub>M i\<in>K. M i) (\<Pi>\<^isub>M i\<in>J. M i) (\<lambda>f. restrict f J)" (is "?P = ?D")
+proof (rule measure_eqI_generator_eq)
+  have "finite J" using `J \<subseteq> K` `finite K` by (auto simp add: finite_subset)
+  interpret J: finite_product_prob_space M J proof qed fact
+  interpret K: finite_product_prob_space M K proof qed fact
+
+  let ?J = "{Pi\<^isub>E J E | E. \<forall>i\<in>J. E i \<in> sets (M i)}"
+  let ?F = "\<lambda>i. \<Pi>\<^isub>E k\<in>J. space (M k)"
+  let ?\<Omega> = "(\<Pi>\<^isub>E k\<in>J. space (M k))"
+  show "Int_stable ?J"
+    by (rule Int_stable_PiE)
+  show "range ?F \<subseteq> ?J" "incseq ?F" "(\<Union>i. ?F i) = ?\<Omega>"
+    using `finite J` by (auto intro!: prod_algebraI_finite)
+  { fix i show "emeasure ?P (?F i) \<noteq> \<infinity>" by simp }
+  show "?J \<subseteq> Pow ?\<Omega>" by (auto simp: Pi_iff dest: sets_into_space)
+  show "sets (\<Pi>\<^isub>M i\<in>J. M i) = sigma_sets ?\<Omega> ?J" "sets ?D = sigma_sets ?\<Omega> ?J"
+    using `finite J` by (simp_all add: sets_PiM prod_algebra_eq_finite Pi_iff)
+  
+  fix X assume "X \<in> ?J"
+  then obtain E where [simp]: "X = Pi\<^isub>E J E" and E: "\<forall>i\<in>J. E i \<in> sets (M i)" by auto
+  with `finite J` have X: "X \<in> sets (Pi\<^isub>M J M)" by auto
+
+  have "emeasure ?P X = (\<Prod> i\<in>J. emeasure (M i) (E i))"
+    using E by (simp add: J.measure_times)
+  also have "\<dots> = (\<Prod> i\<in>J. emeasure (M i) (if i \<in> J then E i else space (M i)))"
+    by simp
+  also have "\<dots> = (\<Prod> i\<in>K. emeasure (M i) (if i \<in> J then E i else space (M i)))"
+    using `finite K` `J \<subseteq> K`
+    by (intro setprod_mono_one_left) (auto simp: M.emeasure_space_1)
+  also have "\<dots> = emeasure (Pi\<^isub>M K M) (\<Pi>\<^isub>E i\<in>K. if i \<in> J then E i else space (M i))"
+    using E by (simp add: K.measure_times)
+  also have "(\<Pi>\<^isub>E i\<in>K. if i \<in> J then E i else space (M i)) = (\<lambda>f. restrict f J) -` Pi\<^isub>E J E \<inter> (\<Pi>\<^isub>E i\<in>K. space (M i))"
+    using `J \<subseteq> K` sets_into_space E by (force simp:  Pi_iff split: split_if_asm)
+  finally show "emeasure (Pi\<^isub>M J M) X = emeasure ?D X"
+    using X `J \<subseteq> K` apply (subst emeasure_distr)
+    by (auto intro!: measurable_restrict_subset simp: space_PiM)
 qed
 
-lemma (in product_prob_space) measurable_restrict:
-  assumes *: "J \<noteq> {}" "J \<subseteq> K" "finite K"
-  shows "(\<lambda>f. restrict f J) \<in> measurable (\<Pi>\<^isub>M i\<in>K. M i) (\<Pi>\<^isub>M i\<in>J. M i)"
-  using measure_preserving_restrict[OF *]
-  by (rule measure_preservingD2)
+abbreviation (in product_prob_space)
+  "emb L K X \<equiv> prod_emb L M K X"
+
+lemma (in product_prob_space) emeasure_prod_emb[simp]:
+  assumes L: "J \<noteq> {}" "J \<subseteq> L" "finite L" and X: "X \<in> sets (Pi\<^isub>M J M)"
+  shows "emeasure (Pi\<^isub>M L M) (emb L J X) = emeasure (Pi\<^isub>M J M) X"
+  by (subst distr_restrict[OF L])
+     (simp add: prod_emb_def space_PiM emeasure_distr measurable_restrict_subset L X)
 
-definition (in product_prob_space)
-  "emb J K X = (\<lambda>x. restrict x K) -` X \<inter> space (Pi\<^isub>M J M)"
+lemma (in product_prob_space) prod_emb_injective:
+  assumes "J \<noteq> {}" "J \<subseteq> L" "finite J" and sets: "X \<in> sets (Pi\<^isub>M J M)" "Y \<in> sets (Pi\<^isub>M J M)"
+  assumes "prod_emb L M J X = prod_emb L M J Y"
+  shows "X = Y"
+proof (rule injective_vimage_restrict)
+  show "X \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))" "Y \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))"
+    using sets[THEN sets_into_space] by (auto simp: space_PiM)
+  have "\<forall>i\<in>L. \<exists>x. x \<in> space (M i)"
+    using M.not_empty by auto
+  from bchoice[OF this]
+  show "(\<Pi>\<^isub>E i\<in>L. space (M i)) \<noteq> {}" by auto
+  show "(\<lambda>x. restrict x J) -` X \<inter> (\<Pi>\<^isub>E i\<in>L. space (M i)) = (\<lambda>x. restrict x J) -` Y \<inter> (\<Pi>\<^isub>E i\<in>L. space (M i))"
+    using `prod_emb L M J X = prod_emb L M J Y` by (simp add: prod_emb_def)
+qed fact
 
-lemma (in product_prob_space) emb_trans[simp]:
-  "J \<subseteq> K \<Longrightarrow> K \<subseteq> L \<Longrightarrow> emb L K (emb K J X) = emb L J X"
-  by (auto simp add: Int_absorb1 emb_def)
-
-lemma (in product_prob_space) emb_empty[simp]:
-  "emb K J {} = {}"
-  by (simp add: emb_def)
+definition (in product_prob_space) generator :: "('i \<Rightarrow> 'a) set set" where
+  "generator = (\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` sets (Pi\<^isub>M J M))"
 
-lemma (in product_prob_space) emb_Pi:
-  assumes "X \<in> (\<Pi> j\<in>J. sets (M j))" "J \<subseteq> K"
-  shows "emb K J (Pi\<^isub>E J X) = (\<Pi>\<^isub>E i\<in>K. if i \<in> J then X i else space (M i))"
-  using assms space_closed
-  by (auto simp: emb_def Pi_iff split: split_if_asm) blast+
+lemma (in product_prob_space) generatorI':
+  "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> emb I J X \<in> generator"
+  unfolding generator_def by auto
 
-lemma (in product_prob_space) emb_injective:
-  assumes "J \<noteq> {}" "J \<subseteq> L" "finite J" and sets: "X \<in> sets (Pi\<^isub>M J M)" "Y \<in> sets (Pi\<^isub>M J M)"
-  assumes "emb L J X = emb L J Y"
-  shows "X = Y"
-proof -
-  interpret J: finite_product_sigma_finite M J by default fact
-  show "X = Y"
-  proof (rule injective_vimage_restrict)
-    show "X \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))" "Y \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))"
-      using J.sets_into_space sets by auto
-    have "\<forall>i\<in>L. \<exists>x. x \<in> space (M i)"
-      using M.not_empty by auto
-    from bchoice[OF this]
-    show "(\<Pi>\<^isub>E i\<in>L. space (M i)) \<noteq> {}" by auto
-    show "(\<lambda>x. restrict x J) -` X \<inter> (\<Pi>\<^isub>E i\<in>L. space (M i)) = (\<lambda>x. restrict x J) -` Y \<inter> (\<Pi>\<^isub>E i\<in>L. space (M i))"
-      using `emb L J X = emb L J Y` by (simp add: emb_def)
-  qed fact
+lemma (in product_prob_space) algebra_generator:
+  assumes "I \<noteq> {}" shows "algebra (\<Pi>\<^isub>E i\<in>I. space (M i)) generator" (is "algebra ?\<Omega> ?G")
+proof
+  let ?G = generator
+  show "?G \<subseteq> Pow ?\<Omega>"
+    by (auto simp: generator_def prod_emb_def)
+  from `I \<noteq> {}` obtain i where "i \<in> I" by auto
+  then show "{} \<in> ?G"
+    by (auto intro!: exI[of _ "{i}"] image_eqI[where x="\<lambda>i. {}"]
+             simp: sigma_sets.Empty generator_def prod_emb_def)
+  from `i \<in> I` show "?\<Omega> \<in> ?G"
+    by (auto intro!: exI[of _ "{i}"] image_eqI[where x="Pi\<^isub>E {i} (\<lambda>i. space (M i))"]
+             simp: generator_def prod_emb_def)
+  fix A assume "A \<in> ?G"
+  then obtain JA XA where XA: "JA \<noteq> {}" "finite JA" "JA \<subseteq> I" "XA \<in> sets (Pi\<^isub>M JA M)" and A: "A = emb I JA XA"
+    by (auto simp: generator_def)
+  fix B assume "B \<in> ?G"
+  then obtain JB XB where XB: "JB \<noteq> {}" "finite JB" "JB \<subseteq> I" "XB \<in> sets (Pi\<^isub>M JB M)" and B: "B = emb I JB XB"
+    by (auto simp: generator_def)
+  let ?RA = "emb (JA \<union> JB) JA XA"
+  let ?RB = "emb (JA \<union> JB) JB XB"
+  have *: "A - B = emb I (JA \<union> JB) (?RA - ?RB)" "A \<union> B = emb I (JA \<union> JB) (?RA \<union> ?RB)"
+    using XA A XB B by auto
+  show "A - B \<in> ?G" "A \<union> B \<in> ?G"
+    unfolding * using XA XB by (safe intro!: generatorI') auto
 qed
 
-lemma (in product_prob_space) emb_id:
-  "B \<subseteq> (\<Pi>\<^isub>E i\<in>L. space (M i)) \<Longrightarrow> emb L L B = B"
-  by (auto simp: emb_def Pi_iff subset_eq extensional_restrict)
-
-lemma (in product_prob_space) emb_simps:
-  shows "emb L K (A \<union> B) = emb L K A \<union> emb L K B"
-    and "emb L K (A \<inter> B) = emb L K A \<inter> emb L K B"
-    and "emb L K (A - B) = emb L K A - emb L K B"
-  by (auto simp: emb_def)
-
-lemma (in product_prob_space) measurable_emb[intro,simp]:
-  assumes *: "J \<noteq> {}" "J \<subseteq> L" "finite L" "X \<in> sets (Pi\<^isub>M J M)"
-  shows "emb L J X \<in> sets (Pi\<^isub>M L M)"
-  using measurable_restrict[THEN measurable_sets, OF *] by (simp add: emb_def)
-
-lemma (in product_prob_space) measure_emb[intro,simp]:
-  assumes *: "J \<noteq> {}" "J \<subseteq> L" "finite L" "X \<in> sets (Pi\<^isub>M J M)"
-  shows "measure (Pi\<^isub>M L M) (emb L J X) = measure (Pi\<^isub>M J M) X"
-  using measure_preserving_restrict[THEN measure_preservingD, OF *]
-  by (simp add: emb_def)
-
-definition (in product_prob_space) generator :: "('i \<Rightarrow> 'a) measure_space" where
-  "generator = \<lparr>
-    space = (\<Pi>\<^isub>E i\<in>I. space (M i)),
-    sets = (\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` sets (Pi\<^isub>M J M)),
-    measure = undefined
-  \<rparr>"
+lemma (in product_prob_space) sets_PiM_generator:
+  assumes "I \<noteq> {}" shows "sets (PiM I M) = sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) generator"
+proof
+  show "sets (Pi\<^isub>M I M) \<subseteq> sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) generator"
+    unfolding sets_PiM
+  proof (safe intro!: sigma_sets_subseteq)
+    fix A assume "A \<in> prod_algebra I M" with `I \<noteq> {}` show "A \<in> generator"
+      by (auto intro!: generatorI' elim!: prod_algebraE)
+  qed
+qed (auto simp: generator_def space_PiM[symmetric] intro!: sigma_sets_subset)
 
 lemma (in product_prob_space) generatorI:
-  "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> A = emb I J X \<Longrightarrow> A \<in> sets generator"
-  unfolding generator_def by auto
-
-lemma (in product_prob_space) generatorI':
-  "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> emb I J X \<in> sets generator"
+  "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> A = emb I J X \<Longrightarrow> A \<in> generator"
   unfolding generator_def by auto
 
-lemma (in product_sigma_finite)
-  assumes "I \<inter> J = {}" "finite I" "finite J" and A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
-  shows measure_fold_integral:
-    "measure (Pi\<^isub>M (I \<union> J) M) A = (\<integral>\<^isup>+x. measure (Pi\<^isub>M J M) (merge I x J -` A \<inter> space (Pi\<^isub>M J M)) \<partial>Pi\<^isub>M I M)" (is ?I)
-    and measure_fold_measurable:
-    "(\<lambda>x. measure (Pi\<^isub>M J M) (merge I x J -` A \<inter> space (Pi\<^isub>M J M))) \<in> borel_measurable (Pi\<^isub>M I M)" (is ?B)
-proof -
-  interpret I: finite_product_sigma_finite M I by default fact
-  interpret J: finite_product_sigma_finite M J by default fact
-  interpret IJ: pair_sigma_finite I.P J.P ..
-  show ?I
-    unfolding measure_fold[OF assms]
-    apply (subst IJ.pair_measure_alt)
-    apply (intro measurable_sets[OF _ A] measurable_merge assms)
-    apply (auto simp: vimage_compose[symmetric] comp_def space_pair_measure
-      intro!: I.positive_integral_cong)
-    done
-
-  have "(\<lambda>(x, y). merge I x J y) -` A \<inter> space (I.P \<Otimes>\<^isub>M J.P) \<in> sets (I.P \<Otimes>\<^isub>M J.P)"
-    by (intro measurable_sets[OF _ A] measurable_merge assms)
-  from IJ.measure_cut_measurable_fst[OF this]
-  show ?B
-    apply (auto simp: vimage_compose[symmetric] comp_def space_pair_measure)
-    apply (subst (asm) measurable_cong)
-    apply auto
-    done
-qed
-
 definition (in product_prob_space)
   "\<mu>G A =
-    (THE x. \<forall>J. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> J \<subseteq> I \<longrightarrow> (\<forall>X\<in>sets (Pi\<^isub>M J M). A = emb I J X \<longrightarrow> x = measure (Pi\<^isub>M J M) X))"
+    (THE x. \<forall>J. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> J \<subseteq> I \<longrightarrow> (\<forall>X\<in>sets (Pi\<^isub>M J M). A = emb I J X \<longrightarrow> x = emeasure (Pi\<^isub>M J M) X))"
 
 lemma (in product_prob_space) \<mu>G_spec:
   assumes J: "J \<noteq> {}" "finite J" "J \<subseteq> I" "A = emb I J X" "X \<in> sets (Pi\<^isub>M J M)"
-  shows "\<mu>G A = measure (Pi\<^isub>M J M) X"
+  shows "\<mu>G A = emeasure (Pi\<^isub>M J M) X"
   unfolding \<mu>G_def
 proof (intro the_equality allI impI ballI)
   fix K Y assume K: "K \<noteq> {}" "finite K" "K \<subseteq> I" "A = emb I K Y" "Y \<in> sets (Pi\<^isub>M K M)"
-  have "measure (Pi\<^isub>M K M) Y = measure (Pi\<^isub>M (K \<union> J) M) (emb (K \<union> J) K Y)"
+  have "emeasure (Pi\<^isub>M K M) Y = emeasure (Pi\<^isub>M (K \<union> J) M) (emb (K \<union> J) K Y)"
     using K J by simp
   also have "emb (K \<union> J) K Y = emb (K \<union> J) J X"
-    using K J by (simp add: emb_injective[of "K \<union> J" I])
-  also have "measure (Pi\<^isub>M (K \<union> J) M) (emb (K \<union> J) J X) = measure (Pi\<^isub>M J M) X"
+    using K J by (simp add: prod_emb_injective[of "K \<union> J" I])
+  also have "emeasure (Pi\<^isub>M (K \<union> J) M) (emb (K \<union> J) J X) = emeasure (Pi\<^isub>M J M) X"
     using K J by simp
-  finally show "measure (Pi\<^isub>M J M) X = measure (Pi\<^isub>M K M) Y" ..
+  finally show "emeasure (Pi\<^isub>M J M) X = emeasure (Pi\<^isub>M K M) Y" ..
 qed (insert J, force)
 
 lemma (in product_prob_space) \<mu>G_eq:
-  "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> \<mu>G (emb I J X) = measure (Pi\<^isub>M J M) X"
+  "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> \<mu>G (emb I J X) = emeasure (Pi\<^isub>M J M) X"
   by (intro \<mu>G_spec) auto
 
 lemma (in product_prob_space) generator_Ex:
-  assumes *: "A \<in> sets generator"
-  shows "\<exists>J X. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I \<and> X \<in> sets (Pi\<^isub>M J M) \<and> A = emb I J X \<and> \<mu>G A = measure (Pi\<^isub>M J M) X"
+  assumes *: "A \<in> generator"
+  shows "\<exists>J X. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I \<and> X \<in> sets (Pi\<^isub>M J M) \<and> A = emb I J X \<and> \<mu>G A = emeasure (Pi\<^isub>M J M) X"
 proof -
   from * obtain J X where J: "J \<noteq> {}" "finite J" "J \<subseteq> I" "A = emb I J X" "X \<in> sets (Pi\<^isub>M J M)"
     unfolding generator_def by auto
@@ -231,11 +260,11 @@
 qed
 
 lemma (in product_prob_space) generatorE:
-  assumes A: "A \<in> sets generator"
-  obtains J X where "J \<noteq> {}" "finite J" "J \<subseteq> I" "X \<in> sets (Pi\<^isub>M J M)" "emb I J X = A" "\<mu>G A = measure (Pi\<^isub>M J M) X"
+  assumes A: "A \<in> generator"
+  obtains J X where "J \<noteq> {}" "finite J" "J \<subseteq> I" "X \<in> sets (Pi\<^isub>M J M)" "emb I J X = A" "\<mu>G A = emeasure (Pi\<^isub>M J M) X"
 proof -
   from generator_Ex[OF A] obtain X J where "J \<noteq> {}" "finite J" "J \<subseteq> I" "X \<in> sets (Pi\<^isub>M J M)" "emb I J X = A"
-    "\<mu>G A = measure (Pi\<^isub>M J M) X" by auto
+    "\<mu>G A = emeasure (Pi\<^isub>M J M) X" by auto
   then show thesis by (intro that) auto
 qed
 
@@ -243,11 +272,7 @@
   assumes "finite J" "finite K" "J \<inter> K = {}" and A: "A \<in> sets (Pi\<^isub>M (J \<union> K) M)" and x: "x \<in> space (Pi\<^isub>M J M)"
   shows "merge J x K -` A \<inter> space (Pi\<^isub>M K M) \<in> sets (Pi\<^isub>M K M)"
 proof -
-  interpret J: finite_product_sigma_algebra M J by default fact
-  interpret K: finite_product_sigma_algebra M K by default fact
-  interpret JK: pair_sigma_algebra J.P K.P ..
-
-  from JK.measurable_cut_fst[OF
+  from sets_Pair1[OF
     measurable_merge[THEN measurable_sets, OF `J \<inter> K = {}`], OF A, of x] x
   show ?thesis
       by (simp add: space_pair_measure comp_def vimage_compose[symmetric])
@@ -266,75 +291,27 @@
   have [simp]: "(K - J) \<inter> (K \<union> J) = K - J" by auto
   have [simp]: "(K - J) \<inter> K = K - J" by auto
   from y `K \<subseteq> I` `J \<subseteq> I` show ?thesis
-    by (simp split: split_merge add: emb_def Pi_iff extensional_merge_sub set_eq_iff) auto
-qed
-
-definition (in product_prob_space) infprod_algebra :: "('i \<Rightarrow> 'a) measure_space" where
-  "infprod_algebra = sigma generator \<lparr> measure :=
-    (SOME \<mu>. (\<forall>s\<in>sets generator. \<mu> s = \<mu>G s) \<and>
-       prob_space \<lparr>space = space generator, sets = sets (sigma generator), measure = \<mu>\<rparr>)\<rparr>"
-
-syntax
-  "_PiP"  :: "[pttrn, 'i set, ('b, 'd) measure_space_scheme] => ('i => 'b, 'd) measure_space_scheme"  ("(3PIP _:_./ _)" 10)
-
-syntax (xsymbols)
-  "_PiP" :: "[pttrn, 'i set, ('b, 'd) measure_space_scheme] => ('i => 'b, 'd) measure_space_scheme"  ("(3\<Pi>\<^isub>P _\<in>_./ _)"   10)
-
-syntax (HTML output)
-  "_PiP" :: "[pttrn, 'i set, ('b, 'd) measure_space_scheme] => ('i => 'b, 'd) measure_space_scheme"  ("(3\<Pi>\<^isub>P _\<in>_./ _)"   10)
-
-abbreviation
-  "Pi\<^isub>P I M \<equiv> product_prob_space.infprod_algebra M I"
-
-translations
-  "PIP x:I. M" == "CONST Pi\<^isub>P I (%x. M)"
-
-lemma (in product_prob_space) algebra_generator:
-  assumes "I \<noteq> {}" shows "algebra generator"
-proof
-  let ?G = generator
-  show "sets ?G \<subseteq> Pow (space ?G)"
-    by (auto simp: generator_def emb_def)
-  from `I \<noteq> {}` obtain i where "i \<in> I" by auto
-  then show "{} \<in> sets ?G"
-    by (auto intro!: exI[of _ "{i}"] image_eqI[where x="\<lambda>i. {}"]
-      simp: product_algebra_def sigma_def sigma_sets.Empty generator_def emb_def)
-  from `i \<in> I` show "space ?G \<in> sets ?G"
-    by (auto intro!: exI[of _ "{i}"] image_eqI[where x="Pi\<^isub>E {i} (\<lambda>i. space (M i))"]
-      simp: generator_def emb_def)
-  fix A assume "A \<in> sets ?G"
-  then obtain JA XA where XA: "JA \<noteq> {}" "finite JA" "JA \<subseteq> I" "XA \<in> sets (Pi\<^isub>M JA M)" and A: "A = emb I JA XA"
-    by (auto simp: generator_def)
-  fix B assume "B \<in> sets ?G"
-  then obtain JB XB where XB: "JB \<noteq> {}" "finite JB" "JB \<subseteq> I" "XB \<in> sets (Pi\<^isub>M JB M)" and B: "B = emb I JB XB"
-    by (auto simp: generator_def)
-  let ?RA = "emb (JA \<union> JB) JA XA"
-  let ?RB = "emb (JA \<union> JB) JB XB"
-  interpret JAB: finite_product_sigma_algebra M "JA \<union> JB"
-    by default (insert XA XB, auto)
-  have *: "A - B = emb I (JA \<union> JB) (?RA - ?RB)" "A \<union> B = emb I (JA \<union> JB) (?RA \<union> ?RB)"
-    using XA A XB B by (auto simp: emb_simps)
-  then show "A - B \<in> sets ?G" "A \<union> B \<in> sets ?G"
-    using XA XB by (auto intro!: generatorI')
+    by (simp split: split_merge add: prod_emb_def Pi_iff extensional_merge_sub set_eq_iff space_PiM)
+       auto
 qed
 
 lemma (in product_prob_space) positive_\<mu>G: 
   assumes "I \<noteq> {}"
   shows "positive generator \<mu>G"
 proof -
-  interpret G!: algebra generator by (rule algebra_generator) fact
+  interpret G!: algebra "\<Pi>\<^isub>E i\<in>I. space (M i)" generator by (rule algebra_generator) fact
   show ?thesis
   proof (intro positive_def[THEN iffD2] conjI ballI)
     from generatorE[OF G.empty_sets] guess J X . note this[simp]
     interpret J: finite_product_sigma_finite M J by default fact
     have "X = {}"
-      by (rule emb_injective[of J I]) simp_all
+      by (rule prod_emb_injective[of J I]) simp_all
     then show "\<mu>G {} = 0" by simp
   next
-    fix A assume "A \<in> sets generator"
+    fix A assume "A \<in> generator"
     from generatorE[OF this] guess J X . note this[simp]
     interpret J: finite_product_sigma_finite M J by default fact
-    show "0 \<le> \<mu>G A" by simp
+    show "0 \<le> \<mu>G A" by (simp add: emeasure_nonneg)
   qed
 qed
 
@@ -342,102 +319,47 @@
   assumes "I \<noteq> {}"
   shows "additive generator \<mu>G"
 proof -
-  interpret G!: algebra generator by (rule algebra_generator) fact
+  interpret G!: algebra "\<Pi>\<^isub>E i\<in>I. space (M i)" generator by (rule algebra_generator) fact
   show ?thesis
   proof (intro additive_def[THEN iffD2] ballI impI)
-    fix A assume "A \<in> sets generator" with generatorE guess J X . note J = this
-    fix B assume "B \<in> sets generator" with generatorE guess K Y . note K = this
+    fix A assume "A \<in> generator" with generatorE guess J X . note J = this
+    fix B assume "B \<in> generator" with generatorE guess K Y . note K = this
     assume "A \<inter> B = {}"
     have JK: "J \<union> K \<noteq> {}" "J \<union> K \<subseteq> I" "finite (J \<union> K)"
       using J K by auto
     interpret JK: finite_product_sigma_finite M "J \<union> K" by default fact
     have JK_disj: "emb (J \<union> K) J X \<inter> emb (J \<union> K) K Y = {}"
-      apply (rule emb_injective[of "J \<union> K" I])
+      apply (rule prod_emb_injective[of "J \<union> K" I])
       apply (insert `A \<inter> B = {}` JK J K)
-      apply (simp_all add: JK.Int emb_simps)
+      apply (simp_all add: Int prod_emb_Int)
       done
     have AB: "A = emb I (J \<union> K) (emb (J \<union> K) J X)" "B = emb I (J \<union> K) (emb (J \<union> K) K Y)"
       using J K by simp_all
     then have "\<mu>G (A \<union> B) = \<mu>G (emb I (J \<union> K) (emb (J \<union> K) J X \<union> emb (J \<union> K) K Y))"
-      by (simp add: emb_simps)
-    also have "\<dots> = measure (Pi\<^isub>M (J \<union> K) M) (emb (J \<union> K) J X \<union> emb (J \<union> K) K Y)"
-      using JK J(1, 4) K(1, 4) by (simp add: \<mu>G_eq JK.Un)
+      by simp
+    also have "\<dots> = emeasure (Pi\<^isub>M (J \<union> K) M) (emb (J \<union> K) J X \<union> emb (J \<union> K) K Y)"
+      using JK J(1, 4) K(1, 4) by (simp add: \<mu>G_eq Un del: prod_emb_Un)
     also have "\<dots> = \<mu>G A + \<mu>G B"
-      using J K JK_disj by (simp add: JK.measure_additive[symmetric])
+      using J K JK_disj by (simp add: plus_emeasure[symmetric])
     finally show "\<mu>G (A \<union> B) = \<mu>G A + \<mu>G B" .
   qed
 qed
 
-lemma (in product_prob_space) finite_index_eq_finite_product:
-  assumes "finite I"
-  shows "sets (sigma generator) = sets (Pi\<^isub>M I M)"
-proof safe
-  interpret I: finite_product_sigma_algebra M I by default fact
-  have space_generator[simp]: "space generator = space (Pi\<^isub>M I M)"
-    by (simp add: generator_def product_algebra_def)
-  { fix A assume "A \<in> sets (sigma generator)"
-    then show "A \<in> sets I.P" unfolding sets_sigma
-    proof induct
-      case (Basic A)
-      from generatorE[OF this] guess J X . note J = this
-      with `finite I` have "emb I J X \<in> sets I.P" by auto
-      with `emb I J X = A` show "A \<in> sets I.P" by simp
-    qed auto }
-  { fix A assume A: "A \<in> sets I.P"
-    show "A \<in> sets (sigma generator)"
-    proof cases
-      assume "I = {}"
-      with I.P_empty[OF this] A
-      have "A = space generator \<or> A = {}" 
-        unfolding space_generator by auto
-      then show ?thesis
-        by (auto simp: sets_sigma simp del: space_generator
-                 intro: sigma_sets.Empty sigma_sets_top)
-    next
-      assume "I \<noteq> {}"
-      note A this
-      moreover with I.sets_into_space have "emb I I A = A" by (intro emb_id) auto
-      ultimately show "A \<in> sets (sigma generator)"
-        using `finite I` unfolding sets_sigma
-        by (intro sigma_sets.Basic generatorI[of I A]) auto
-  qed }
-qed
-
-lemma (in product_prob_space) extend_\<mu>G:
-  "\<exists>\<mu>. (\<forall>s\<in>sets generator. \<mu> s = \<mu>G s) \<and>
-       prob_space \<lparr>space = space generator, sets = sets (sigma generator), measure = \<mu>\<rparr>"
+lemma (in product_prob_space) emeasure_PiM_emb_not_empty:
+  assumes X: "J \<noteq> {}" "J \<subseteq> I" "finite J" "\<forall>i\<in>J. X i \<in> sets (M i)"
+  shows "emeasure (Pi\<^isub>M I M) (emb I J (Pi\<^isub>E J X)) = emeasure (Pi\<^isub>M J M) (Pi\<^isub>E J X)"
 proof cases
-  assume "finite I"
-  interpret I: finite_product_prob_space M I by default fact
-  show ?thesis
-  proof (intro exI[of _ "measure (Pi\<^isub>M I M)"] ballI conjI)
-    fix A assume "A \<in> sets generator"
-    from generatorE[OF this] guess J X . note J = this
-    from J(1-4) `finite I` show "measure I.P A = \<mu>G A"
-      unfolding J(6)
-      by (subst J(5)[symmetric]) (simp add: measure_emb)
-  next
-    have [simp]: "space generator = space (Pi\<^isub>M I M)"
-      by (simp add: generator_def product_algebra_def)
-    have "\<lparr>space = space generator, sets = sets (sigma generator), measure = measure I.P\<rparr>
-      = I.P" (is "?P = _")
-      by (auto intro!: measure_space.equality simp: finite_index_eq_finite_product[OF `finite I`])
-    show "prob_space ?P"
-    proof
-      show "measure_space ?P" using `?P = I.P` by simp default
-      show "measure ?P (space ?P) = 1"
-        using I.measure_space_1 by simp
-    qed
-  qed
+  assume "finite I" with X show ?thesis by simp
 next
+  let ?\<Omega> = "\<Pi>\<^isub>E i\<in>I. space (M i)"
   let ?G = generator
   assume "\<not> finite I"
   then have I_not_empty: "I \<noteq> {}" by auto
-  interpret G!: algebra generator by (rule algebra_generator) fact
+  interpret G!: algebra ?\<Omega> generator by (rule algebra_generator) fact
   note \<mu>G_mono =
     G.additive_increasing[OF positive_\<mu>G[OF I_not_empty] additive_\<mu>G[OF I_not_empty], THEN increasingD]
 
-  { fix Z J assume J: "J \<noteq> {}" "finite J" "J \<subseteq> I" and Z: "Z \<in> sets ?G"
+  { fix Z J assume J: "J \<noteq> {}" "finite J" "J \<subseteq> I" and Z: "Z \<in> ?G"
 
     from `infinite I` `finite J` obtain k where k: "k \<in> I" "k \<notin> J"
       by (metis rev_finite_subset subsetI)
@@ -445,7 +367,7 @@
     moreover def K \<equiv> "insert k K'"
     moreover def X \<equiv> "emb K K' X'"
     ultimately have K: "K \<noteq> {}" "finite K" "K \<subseteq> I" "X \<in> sets (Pi\<^isub>M K M)" "Z = emb I K X"
-      "K - J \<noteq> {}" "K - J \<subseteq> I" "\<mu>G Z = measure (Pi\<^isub>M K M) X"
+      "K - J \<noteq> {}" "K - J \<subseteq> I" "\<mu>G Z = emeasure (Pi\<^isub>M K M) X"
       by (auto simp: subset_insertI)
 
     let ?M = "\<lambda>y. merge J y (K - J) -` emb (J \<union> K) K X \<inter> space (Pi\<^isub>M (K - J) M)"
@@ -455,9 +377,9 @@
       have **: "?M y \<in> sets (Pi\<^isub>M (K - J) M)"
         using J K y by (intro merge_sets) auto
       ultimately
-      have ***: "(merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M)) \<in> sets ?G"
+      have ***: "(merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M)) \<in> ?G"
         using J K by (intro generatorI) auto
-      have "\<mu>G (merge J y (I - J) -` emb I K X \<inter> space (Pi\<^isub>M I M)) = measure (Pi\<^isub>M (K - J) M) (?M y)"
+      have "\<mu>G (merge J y (I - J) -` emb I K X \<inter> space (Pi\<^isub>M I M)) = emeasure (Pi\<^isub>M (K - J) M) (?M y)"
         unfolding * using K J by (subst \<mu>G_eq[OF _ _ _ **]) auto
       note * ** *** this }
     note merge_in_G = this
@@ -467,16 +389,16 @@
     interpret J: finite_product_prob_space M J by default fact+
     interpret KmJ: finite_product_prob_space M "K - J" by default fact+
 
-    have "\<mu>G Z = measure (Pi\<^isub>M (J \<union> (K - J)) M) (emb (J \<union> (K - J)) K X)"
+    have "\<mu>G Z = emeasure (Pi\<^isub>M (J \<union> (K - J)) M) (emb (J \<union> (K - J)) K X)"
       using K J by simp
-    also have "\<dots> = (\<integral>\<^isup>+ x. measure (Pi\<^isub>M (K - J) M) (?M x) \<partial>Pi\<^isub>M J M)"
-      using K J by (subst measure_fold_integral) auto
+    also have "\<dots> = (\<integral>\<^isup>+ x. emeasure (Pi\<^isub>M (K - J) M) (?M x) \<partial>Pi\<^isub>M J M)"
+      using K J by (subst emeasure_fold_integral) auto
     also have "\<dots> = (\<integral>\<^isup>+ y. \<mu>G (merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M)) \<partial>Pi\<^isub>M J M)"
       (is "_ = (\<integral>\<^isup>+x. \<mu>G (?MZ x) \<partial>Pi\<^isub>M J M)")
-    proof (intro J.positive_integral_cong)
+    proof (intro positive_integral_cong)
       fix x assume x: "x \<in> space (Pi\<^isub>M J M)"
       with K merge_in_G(2)[OF this]
-      show "measure (Pi\<^isub>M (K - J) M) (?M x) = \<mu>G (?MZ x)"
+      show "emeasure (Pi\<^isub>M (K - J) M) (?M x) = \<mu>G (?MZ x)"
         unfolding `Z = emb I K X` merge_in_G(1)[OF x] by (subst \<mu>G_eq) auto
     qed
     finally have fold: "\<mu>G Z = (\<integral>\<^isup>+x. \<mu>G (?MZ x) \<partial>Pi\<^isub>M J M)" .
@@ -490,21 +412,18 @@
     let ?q = "\<lambda>y. \<mu>G (merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M))"
     have "?q \<in> borel_measurable (Pi\<^isub>M J M)"
       unfolding `Z = emb I K X` using J K merge_in_G(3)
-      by (simp add: merge_in_G  \<mu>G_eq measure_fold_measurable
-               del: space_product_algebra cong: measurable_cong)
+      by (simp add: merge_in_G  \<mu>G_eq emeasure_fold_measurable cong: measurable_cong)
     note this fold le_1 merge_in_G(3) }
   note fold = this
 
-  have "\<exists>\<mu>. (\<forall>s\<in>sets ?G. \<mu> s = \<mu>G s) \<and>
-    measure_space \<lparr>space = space ?G, sets = sets (sigma ?G), measure = \<mu>\<rparr>"
-    (is "\<exists>\<mu>. _ \<and> measure_space (?ms \<mu>)")
+  have "\<exists>\<mu>. (\<forall>s\<in>?G. \<mu> s = \<mu>G s) \<and> measure_space ?\<Omega> (sigma_sets ?\<Omega> ?G) \<mu>"
   proof (rule G.caratheodory_empty_continuous[OF positive_\<mu>G additive_\<mu>G])
-    fix A assume "A \<in> sets ?G"
+    fix A assume "A \<in> ?G"
     with generatorE guess J X . note JX = this
     interpret JK: finite_product_prob_space M J by default fact+
     from JX show "\<mu>G A \<noteq> \<infinity>" by simp
   next
-    fix A assume A: "range A \<subseteq> sets ?G" "decseq A" "(\<Inter>i. A i) = {}"
+    fix A assume A: "range A \<subseteq> ?G" "decseq A" "(\<Inter>i. A i) = {}"
     then have "decseq (\<lambda>i. \<mu>G (A i))"
       by (auto intro!: \<mu>G_mono simp: decseq_def)
     moreover
@@ -515,7 +434,7 @@
         using A positive_\<mu>G[OF I_not_empty] by (auto intro!: INF_greatest simp: positive_def)
       ultimately have "0 < ?a" by auto
 
-      have "\<forall>n. \<exists>J X. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I \<and> X \<in> sets (Pi\<^isub>M J M) \<and> A n = emb I J X \<and> \<mu>G (A n) = measure (Pi\<^isub>M J M) X"
+      have "\<forall>n. \<exists>J X. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I \<and> X \<in> sets (Pi\<^isub>M J M) \<and> A n = emb I J X \<and> \<mu>G (A n) = emeasure (Pi\<^isub>M J M) X"
         using A by (intro allI generator_Ex) auto
       then obtain J' X' where J': "\<And>n. J' n \<noteq> {}" "\<And>n. finite (J' n)" "\<And>n. J' n \<subseteq> I" "\<And>n. X' n \<in> sets (Pi\<^isub>M (J' n) M)"
         and A': "\<And>n. A n = emb I (J' n) (X' n)"
@@ -524,8 +443,8 @@
       moreover def X \<equiv> "\<lambda>n. emb (J n) (J' n) (X' n)"
       ultimately have J: "\<And>n. J n \<noteq> {}" "\<And>n. finite (J n)" "\<And>n. J n \<subseteq> I" "\<And>n. X n \<in> sets (Pi\<^isub>M (J n) M)"
         by auto
-      with A' have A_eq: "\<And>n. A n = emb I (J n) (X n)" "\<And>n. A n \<in> sets ?G"
-        unfolding J_def X_def by (subst emb_trans) (insert A, auto)
+      with A' have A_eq: "\<And>n. A n = emb I (J n) (X n)" "\<And>n. A n \<in> ?G"
+        unfolding J_def X_def by (subst prod_emb_trans) (insert A, auto)
 
       have J_mono: "\<And>n m. n \<le> m \<Longrightarrow> J n \<subseteq> J m"
         unfolding J_def by force
@@ -538,8 +457,8 @@
 
       let ?M = "\<lambda>K Z y. merge K y (I - K) -` Z \<inter> space (Pi\<^isub>M I M)"
 
-      { fix Z k assume Z: "range Z \<subseteq> sets ?G" "decseq Z" "\<forall>n. ?a / 2^k \<le> \<mu>G (Z n)"
-        then have Z_sets: "\<And>n. Z n \<in> sets ?G" by auto
+      { fix Z k assume Z: "range Z \<subseteq> ?G" "decseq Z" "\<forall>n. ?a / 2^k \<le> \<mu>G (Z n)"
+        then have Z_sets: "\<And>n. Z n \<in> ?G" by auto
         fix J' assume J': "J' \<noteq> {}" "finite J'" "J' \<subseteq> I"
         interpret J': finite_product_prob_space M J' by default fact+
 
@@ -552,13 +471,13 @@
             by (rule measurable_sets) auto }
         note Q_sets = this
 
-        have "?a / 2^(k+1) \<le> (INF n. measure (Pi\<^isub>M J' M) (?Q n))"
+        have "?a / 2^(k+1) \<le> (INF n. emeasure (Pi\<^isub>M J' M) (?Q n))"
         proof (intro INF_greatest)
           fix n
           have "?a / 2^k \<le> \<mu>G (Z n)" using Z by auto
           also have "\<dots> \<le> (\<integral>\<^isup>+ x. indicator (?Q n) x + ?a / 2^(k+1) \<partial>Pi\<^isub>M J' M)"
-            unfolding fold(2)[OF J' `Z n \<in> sets ?G`]
-          proof (intro J'.positive_integral_mono)
+            unfolding fold(2)[OF J' `Z n \<in> ?G`]
+          proof (intro positive_integral_mono)
             fix x assume x: "x \<in> space (Pi\<^isub>M J' M)"
             then have "?q n x \<le> 1 + 0"
               using J' Z fold(3) Z_sets by auto
@@ -568,15 +487,15 @@
             with x show "?q n x \<le> indicator (?Q n) x + ?a / 2^(k+1)"
               by (auto split: split_indicator simp del: power_Suc)
           qed
-          also have "\<dots> = measure (Pi\<^isub>M J' M) (?Q n) + ?a / 2^(k+1)"
-            using `0 \<le> ?a` Q_sets J'.measure_space_1
-            by (subst J'.positive_integral_add) auto
-          finally show "?a / 2^(k+1) \<le> measure (Pi\<^isub>M J' M) (?Q n)" using `?a \<le> 1`
-            by (cases rule: ereal2_cases[of ?a "measure (Pi\<^isub>M J' M) (?Q n)"])
+          also have "\<dots> = emeasure (Pi\<^isub>M J' M) (?Q n) + ?a / 2^(k+1)"
+            using `0 \<le> ?a` Q_sets J'.emeasure_space_1
+            by (subst positive_integral_add) auto
+          finally show "?a / 2^(k+1) \<le> emeasure (Pi\<^isub>M J' M) (?Q n)" using `?a \<le> 1`
+            by (cases rule: ereal2_cases[of ?a "emeasure (Pi\<^isub>M J' M) (?Q n)"])
                (auto simp: field_simps)
         qed
-        also have "\<dots> = measure (Pi\<^isub>M J' M) (\<Inter>n. ?Q n)"
-        proof (intro J'.continuity_from_above)
+        also have "\<dots> = emeasure (Pi\<^isub>M J' M) (\<Inter>n. ?Q n)"
+        proof (intro INF_emeasure_decseq)
           show "range ?Q \<subseteq> sets (Pi\<^isub>M J' M)" using Q_sets by auto
           show "decseq ?Q"
             unfolding decseq_def
@@ -587,13 +506,13 @@
             also have "?q n x \<le> ?q m x"
             proof (rule \<mu>G_mono)
               from fold(4)[OF J', OF Z_sets x]
-              show "?M J' (Z n) x \<in> sets ?G" "?M J' (Z m) x \<in> sets ?G" by auto
+              show "?M J' (Z n) x \<in> ?G" "?M J' (Z m) x \<in> ?G" by auto
               show "?M J' (Z n) x \<subseteq> ?M J' (Z m) x"
                 using `decseq Z`[THEN decseqD, OF `m \<le> n`] by auto
             qed
             finally show "?a / 2^(k+1) \<le> ?q m x" .
           qed
-        qed (intro J'.finite_measure Q_sets)
+        qed simp
         finally have "(\<Inter>n. ?Q n) \<noteq> {}"
           using `0 < ?a` `?a \<le> 1` by (cases ?a) (auto simp: divide_le_0_iff power_le_zero_eq)
         then have "\<exists>w\<in>space (Pi\<^isub>M J' M). \<forall>n. ?a / 2 ^ (k + 1) \<le> ?q n w" by auto }
@@ -631,12 +550,12 @@
               show "w k \<in> space (Pi\<^isub>M (J (Suc k)) M)"
                 using Suc by simp
               then show "restrict (w k) (J k) = w k"
-                by (simp add: extensional_restrict)
+                by (simp add: extensional_restrict space_PiM)
             qed
           next
             assume "J k \<noteq> J (Suc k)"
             with J_mono[of k "Suc k"] have "J (Suc k) - J k \<noteq> {}" (is "?D \<noteq> {}") by auto
-            have "range (\<lambda>n. ?M (J k) (A n) (w k)) \<subseteq> sets ?G"
+            have "range (\<lambda>n. ?M (J k) (A n) (w k)) \<subseteq> ?G"
               "decseq (\<lambda>n. ?M (J k) (A n) (w k))"
               "\<forall>n. ?a / 2 ^ (k + 1) \<le> \<mu>G (?M (J k) (A n) (w k))"
               using `decseq A` fold(4)[OF J(1-3) A_eq(2), of "w k" k] Suc
@@ -651,11 +570,11 @@
               by (auto intro!: ext split: split_merge)
             have *: "\<And>n. ?M ?D (?M (J k) (A n) (w k)) w' = ?M (J (Suc k)) (A n) ?w"
               using w'(1) J(3)[of "Suc k"]
-              by (auto split: split_merge intro!: extensional_merge_sub) force+
+              by (auto simp: space_PiM split: split_merge intro!: extensional_merge_sub) force+
             show ?thesis
               apply (rule exI[of _ ?w])
               using w' J_mono[of k "Suc k"] wk unfolding *
-              apply (auto split: split_merge intro!: extensional_merge_sub ext)
+              apply (auto split: split_merge intro!: extensional_merge_sub ext simp: space_PiM)
               apply (force simp: extensional_def)
               done
           qed
@@ -675,7 +594,7 @@
         then have "merge (J k) (w k) (I - J k) x \<in> A k" by auto
         then have "\<exists>x\<in>A k. restrict x (J k) = w k"
           using `w k \<in> space (Pi\<^isub>M (J k) M)`
-          by (intro rev_bexI) (auto intro!: ext simp: extensional_def)
+          by (intro rev_bexI) (auto intro!: ext simp: extensional_def space_PiM)
         ultimately have "w k \<in> space (Pi\<^isub>M (J k) M)"
           "\<exists>x\<in>A k. restrict x (J k) = w k"
           "k \<noteq> 0 \<Longrightarrow> restrict (w k) (J (k - 1)) = w (k - 1)"
@@ -707,17 +626,17 @@
       have w'_simps2: "\<And>i. i \<notin> (\<Union>k. J k) \<Longrightarrow> i \<in> I \<Longrightarrow> w' i \<in> space (M i)"
         using J by (auto simp: w'_def intro!: someI_ex[OF M.not_empty[unfolded ex_in_conv[symmetric]]])
       { fix i assume "i \<in> I" then have "w' i \<in> space (M i)"
-          using w(1) by (cases "i \<in> (\<Union>k. J k)") (force simp: w'_simps2 w'_eq)+ }
+          using w(1) by (cases "i \<in> (\<Union>k. J k)") (force simp: w'_simps2 w'_eq space_PiM)+ }
       note w'_simps[simp] = w'_eq w'_simps1 w'_simps2 this
 
       have w': "w' \<in> space (Pi\<^isub>M I M)"
-        using w(1) by (auto simp add: Pi_iff extensional_def)
+        using w(1) by (auto simp add: Pi_iff extensional_def space_PiM)
 
       { fix n
         have "restrict w' (J n) = w n" using w(1)
-          by (auto simp add: fun_eq_iff restrict_def Pi_iff extensional_def)
+          by (auto simp add: fun_eq_iff restrict_def Pi_iff extensional_def space_PiM)
         with w[of n] obtain x where "x \<in> A n" "restrict x (J n) = restrict w' (J n)" by auto
-        then have "w' \<in> A n" unfolding A_eq using w' by (auto simp: emb_def) }
+        then have "w' \<in> A n" unfolding A_eq using w' by (auto simp: prod_emb_def space_PiM) }
       then have "w' \<in> (\<Inter>i. A i)" by auto
       with `(\<Inter>i. A i) = {}` show False by auto
     qed
@@ -726,276 +645,76 @@
   qed fact+
   then guess \<mu> .. note \<mu> = this
   show ?thesis
-  proof (intro exI[of _ \<mu>] conjI)
-    show "\<forall>S\<in>sets ?G. \<mu> S = \<mu>G S" using \<mu> by simp
-    show "prob_space (?ms \<mu>)"
-    proof
-      show "measure_space (?ms \<mu>)" using \<mu> by simp
-      obtain i where "i \<in> I" using I_not_empty by auto
-      interpret i: finite_product_sigma_finite M "{i}" by default auto
-      let ?X = "\<Pi>\<^isub>E i\<in>{i}. space (M i)"
-      have X: "?X \<in> sets (Pi\<^isub>M {i} M)"
-        by auto
-      with `i \<in> I` have "emb I {i} ?X \<in> sets generator"
-        by (intro generatorI') auto
-      with \<mu> have "\<mu> (emb I {i} ?X) = \<mu>G (emb I {i} ?X)" by auto
-      with \<mu>G_eq[OF _ _ _ X] `i \<in> I` 
-      have "\<mu> (emb I {i} ?X) = measure (M i) (space (M i))"
-        by (simp add: i.measure_times)
-      also have "emb I {i} ?X = space (Pi\<^isub>P I M)"
-        using `i \<in> I` by (auto simp: emb_def infprod_algebra_def generator_def)
-      finally show "measure (?ms \<mu>) (space (?ms \<mu>)) = 1"
-        using M.measure_space_1 by (simp add: infprod_algebra_def)
-    qed
+  proof (subst emeasure_extend_measure_Pair[OF PiM_def, of I M \<mu> J X])
+    from assms show "(J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))"
+      by (simp add: Pi_iff)
+  next
+    fix J X assume J: "(J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))"
+    then show "emb I J (Pi\<^isub>E J X) \<in> Pow (\<Pi>\<^isub>E i\<in>I. space (M i))"
+      by (auto simp: Pi_iff prod_emb_def dest: sets_into_space)
+    have "emb I J (Pi\<^isub>E J X) \<in> generator"
+      using J `I \<noteq> {}` by (intro generatorI') auto
+    then have "\<mu> (emb I J (Pi\<^isub>E J X)) = \<mu>G (emb I J (Pi\<^isub>E J X))"
+      using \<mu> by simp
+    also have "\<dots> = (\<Prod> j\<in>J. if j \<in> J then emeasure (M j) (X j) else emeasure (M j) (space (M j)))"
+      using J  `I \<noteq> {}` by (subst \<mu>G_spec[OF _ _ _ refl]) (auto simp: emeasure_PiM Pi_iff)
+    also have "\<dots> = (\<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)))"
+      using J `I \<noteq> {}` by (intro setprod_mono_one_right) (auto simp: M.emeasure_space_1)
+    finally show "\<mu> (emb I J (Pi\<^isub>E J X)) = \<dots>" .
+  next
+    let ?F = "\<lambda>j. if j \<in> J then emeasure (M j) (X j) else emeasure (M j) (space (M j))"
+    have "(\<Prod>j\<in>J \<union> {i \<in> I. emeasure (M i) (space (M i)) \<noteq> 1}. ?F j) = (\<Prod>j\<in>J. ?F j)"
+      using X `I \<noteq> {}` by (intro setprod_mono_one_right) (auto simp: M.emeasure_space_1)
+    then show "(\<Prod>j\<in>J \<union> {i \<in> I. emeasure (M i) (space (M i)) \<noteq> 1}. ?F j) =
+      emeasure (Pi\<^isub>M J M) (Pi\<^isub>E J X)"
+      using X by (auto simp add: emeasure_PiM) 
+  next
+    show "positive (sets (Pi\<^isub>M I M)) \<mu>" "countably_additive (sets (Pi\<^isub>M I M)) \<mu>"
+      using \<mu> unfolding sets_PiM_generator[OF `I \<noteq> {}`] by (auto simp: measure_space_def)
   qed
 qed
 
-lemma (in product_prob_space) infprod_spec:
-  "(\<forall>s\<in>sets generator. measure (Pi\<^isub>P I M) s = \<mu>G s) \<and> prob_space (Pi\<^isub>P I M)"
-  (is "?Q infprod_algebra")
-  unfolding infprod_algebra_def
-  by (rule someI2_ex[OF extend_\<mu>G])
-     (auto simp: sigma_def generator_def)
-
-sublocale product_prob_space \<subseteq> P: prob_space "Pi\<^isub>P I M"
-  using infprod_spec by simp
-
-lemma (in product_prob_space) measure_infprod_emb:
-  assumes "J \<noteq> {}" "finite J" "J \<subseteq> I" "X \<in> sets (Pi\<^isub>M J M)"
-  shows "\<mu> (emb I J X) = measure (Pi\<^isub>M J M) X"
-proof -
-  have "emb I J X \<in> sets generator"
-    using assms by (rule generatorI')
-  with \<mu>G_eq[OF assms] infprod_spec show ?thesis by auto
-qed
-
-lemma (in product_prob_space) measurable_component:
-  assumes "i \<in> I"
-  shows "(\<lambda>x. x i) \<in> measurable (Pi\<^isub>P I M) (M i)"
-proof (unfold measurable_def, safe)
-  fix x assume "x \<in> space (Pi\<^isub>P I M)"
-  then show "x i \<in> space (M i)"
-    using `i \<in> I` by (auto simp: infprod_algebra_def generator_def)
-next
-  fix A assume "A \<in> sets (M i)"
-  with `i \<in> I` have
-    "(\<Pi>\<^isub>E x \<in> {i}. A) \<in> sets (Pi\<^isub>M {i} M)"
-    "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M) = emb I {i} (\<Pi>\<^isub>E x \<in> {i}. A)"
-    by (auto simp: infprod_algebra_def generator_def emb_def)
-  from generatorI[OF _ _ _ this] `i \<in> I`
-  show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M) \<in> sets (Pi\<^isub>P I M)"
-    unfolding infprod_algebra_def by auto
-qed
-
-lemma (in product_prob_space) emb_in_infprod_algebra[intro]:
-  fixes J assumes J: "finite J" "J \<subseteq> I" and X: "X \<in> sets (Pi\<^isub>M J M)"
-  shows "emb I J X \<in> sets (\<Pi>\<^isub>P i\<in>I. M i)"
-proof cases
-  assume "J = {}"
-  with X have "emb I J X = space (\<Pi>\<^isub>P i\<in>I. M i) \<or> emb I J X = {}"
-    by (auto simp: emb_def infprod_algebra_def generator_def
-                   product_algebra_def product_algebra_generator_def image_constant sigma_def)
-  then show ?thesis by auto
-next
-  assume "J \<noteq> {}"
-  show ?thesis unfolding infprod_algebra_def
-    by simp (intro in_sigma generatorI'  `J \<noteq> {}` J X)
-qed
-
-lemma (in product_prob_space) finite_measure_infprod_emb:
-  assumes "J \<noteq> {}" "finite J" "J \<subseteq> I" "X \<in> sets (Pi\<^isub>M J M)"
-  shows "\<mu>' (emb I J X) = finite_measure.\<mu>' (Pi\<^isub>M J M) X"
-proof -
-  interpret J: finite_product_prob_space M J by default fact+
-  from assms have "emb I J X \<in> sets (Pi\<^isub>P I M)" by auto
-  with assms show "\<mu>' (emb I J X) = J.\<mu>' X"
-    unfolding \<mu>'_def J.\<mu>'_def
-    unfolding measure_infprod_emb[OF assms]
-    by auto
-qed
-
-lemma (in finite_product_prob_space) finite_measure_times:
-  assumes "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)"
-  shows "\<mu>' (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu>' i (A i))"
-  using assms
-  unfolding \<mu>'_def M.\<mu>'_def
-  by (subst measure_times[OF assms])
-     (auto simp: finite_measure_eq M.finite_measure_eq setprod_ereal)
-
-lemma (in product_prob_space) finite_measure_infprod_emb_Pi:
-  assumes J: "finite J" "J \<subseteq> I" "\<And>j. j \<in> J \<Longrightarrow> X j \<in> sets (M j)"
-  shows "\<mu>' (emb I J (Pi\<^isub>E J X)) = (\<Prod>j\<in>J. M.\<mu>' j (X j))"
-proof cases
-  assume "J = {}"
-  then have "emb I J (Pi\<^isub>E J X) = space infprod_algebra"
-    by (auto simp: infprod_algebra_def generator_def sigma_def emb_def)
-  then show ?thesis using `J = {}` P.prob_space
-    by simp
-next
-  assume "J \<noteq> {}"
-  interpret J: finite_product_prob_space M J by default fact+
-  have "(\<Prod>i\<in>J. M.\<mu>' i (X i)) = J.\<mu>' (Pi\<^isub>E J X)"
-    using J `J \<noteq> {}` by (subst J.finite_measure_times) auto
-  also have "\<dots> = \<mu>' (emb I J (Pi\<^isub>E J X))"
-    using J `J \<noteq> {}` by (intro finite_measure_infprod_emb[symmetric]) auto
-  finally show ?thesis by simp
-qed
-
-lemma sigma_sets_mono: assumes "A \<subseteq> sigma_sets X B" shows "sigma_sets X A \<subseteq> sigma_sets X B"
+sublocale product_prob_space \<subseteq> P: prob_space "Pi\<^isub>M I M"
 proof
-  fix x assume "x \<in> sigma_sets X A" then show "x \<in> sigma_sets X B"
-    by induct (insert `A \<subseteq> sigma_sets X B`, auto intro: sigma_sets.intros)
-qed
-
-lemma sigma_sets_mono': assumes "A \<subseteq> B" shows "sigma_sets X A \<subseteq> sigma_sets X B"
-proof
-  fix x assume "x \<in> sigma_sets X A" then show "x \<in> sigma_sets X B"
-    by induct (insert `A \<subseteq> B`, auto intro: sigma_sets.intros)
+  show "emeasure (Pi\<^isub>M I M) (space (Pi\<^isub>M I M)) = 1"
+  proof cases
+    assume "I = {}" then show ?thesis by (simp add: space_PiM_empty)
+  next
+    assume "I \<noteq> {}"
+    then obtain i where "i \<in> I" by auto
+    moreover then have "emb I {i} (\<Pi>\<^isub>E i\<in>{i}. space (M i)) = (space (Pi\<^isub>M I M))"
+      by (auto simp: prod_emb_def space_PiM)
+    ultimately show ?thesis
+      using emeasure_PiM_emb_not_empty[of "{i}" "\<lambda>i. space (M i)"]
+      by (simp add: emeasure_PiM emeasure_space_1)
+  qed
 qed
 
-lemma sigma_sets_superset_generator: "A \<subseteq> sigma_sets X A"
-  by (auto intro: sigma_sets.Basic)
-
-lemma (in product_prob_space) infprod_algebra_alt:
-  "Pi\<^isub>P I M = sigma \<lparr> space = space (Pi\<^isub>P I M),
-    sets = (\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` Pi\<^isub>E J ` (\<Pi> i \<in> J. sets (M i))),
-    measure = measure (Pi\<^isub>P I M) \<rparr>"
-  (is "_ = sigma \<lparr> space = ?O, sets = ?M, measure = ?m \<rparr>")
-proof (rule measure_space.equality)
-  let ?G = "\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` sets (Pi\<^isub>M J M)"
-  have "sigma_sets ?O ?M = sigma_sets ?O ?G"
-  proof (intro equalityI sigma_sets_mono UN_least)
-    fix J assume J: "J \<in> {J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}"
-    have "emb I J ` Pi\<^isub>E J ` (\<Pi> i\<in>J. sets (M i)) \<subseteq> emb I J ` sets (Pi\<^isub>M J M)" by auto
-    also have "\<dots> \<subseteq> ?G" using J by (rule UN_upper)
-    also have "\<dots> \<subseteq> sigma_sets ?O ?G" by (rule sigma_sets_superset_generator)
-    finally show "emb I J ` Pi\<^isub>E J ` (\<Pi> i\<in>J. sets (M i)) \<subseteq> sigma_sets ?O ?G" .
-    have "emb I J ` sets (Pi\<^isub>M J M) = emb I J ` sigma_sets (space (Pi\<^isub>M J M)) (Pi\<^isub>E J ` (\<Pi> i \<in> J. sets (M i)))"
-      by (simp add: sets_sigma product_algebra_generator_def product_algebra_def)
-    also have "\<dots> = sigma_sets (space (Pi\<^isub>M I M)) (emb I J ` Pi\<^isub>E J ` (\<Pi> i \<in> J. sets (M i)))"
-      using J M.sets_into_space
-      by (auto simp: emb_def [abs_def] intro!: sigma_sets_vimage[symmetric]) blast
-    also have "\<dots> \<subseteq> sigma_sets (space (Pi\<^isub>M I M)) ?M"
-      using J by (intro sigma_sets_mono') auto
-    finally show "emb I J ` sets (Pi\<^isub>M J M) \<subseteq> sigma_sets ?O ?M"
-      by (simp add: infprod_algebra_def generator_def)
-  qed
-  then show "sets (Pi\<^isub>P I M) = sets (sigma \<lparr> space = ?O, sets = ?M, measure = ?m \<rparr>)"
-    by (simp_all add: infprod_algebra_def generator_def sets_sigma)
-qed simp_all
-
-lemma (in product_prob_space) infprod_algebra_alt2:
-  "Pi\<^isub>P I M = sigma \<lparr> space = space (Pi\<^isub>P I M),
-    sets = (\<Union>i\<in>I. (\<lambda>A. (\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M)) ` sets (M i)),
-    measure = measure (Pi\<^isub>P I M) \<rparr>"
-  (is "_ = ?S")
-proof (rule measure_space.equality)
-  let "sigma \<lparr> space = ?O, sets = ?A, \<dots> = _ \<rparr>" = ?S
-  let ?G = "(\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` Pi\<^isub>E J ` (\<Pi> i \<in> J. sets (M i)))"
-  have "sets (Pi\<^isub>P I M) = sigma_sets ?O ?G"
-    by (subst infprod_algebra_alt) (simp add: sets_sigma)
-  also have "\<dots> = sigma_sets ?O ?A"
-  proof (intro equalityI sigma_sets_mono subsetI)
-    interpret A: sigma_algebra ?S
-      by (rule sigma_algebra_sigma) auto
-    fix A assume "A \<in> ?G"
-    then obtain J B where "finite J" "J \<noteq> {}" "J \<subseteq> I" "A = emb I J (Pi\<^isub>E J B)"
-        and B: "\<And>i. i \<in> J \<Longrightarrow> B i \<in> sets (M i)"
-      by auto
-    then have A: "A = (\<Inter>j\<in>J. (\<lambda>x. x j) -` (B j) \<inter> space (Pi\<^isub>P I M))"
-      by (auto simp: emb_def infprod_algebra_def generator_def Pi_iff)
-    { fix j assume "j\<in>J"
-      with `J \<subseteq> I` have "j \<in> I" by auto
-      with `j \<in> J` B have "(\<lambda>x. x j) -` (B j) \<inter> space (Pi\<^isub>P I M) \<in> sets ?S"
-        by (auto simp: sets_sigma intro: sigma_sets.Basic) }
-    with `finite J` `J \<noteq> {}` have "A \<in> sets ?S"
-      unfolding A by (intro A.finite_INT) auto
-    then show "A \<in> sigma_sets ?O ?A" by (simp add: sets_sigma)
-  next
-    fix A assume "A \<in> ?A"
-    then obtain i B where i: "i \<in> I" "B \<in> sets (M i)"
-        and "A = (\<lambda>x. x i) -` B \<inter> space (Pi\<^isub>P I M)"
-      by auto
-    then have "A = emb I {i} (Pi\<^isub>E {i} (\<lambda>_. B))"
-      by (auto simp: emb_def infprod_algebra_def generator_def Pi_iff)
-    with i show "A \<in> sigma_sets ?O ?G"
-      by (intro sigma_sets.Basic UN_I[where a="{i}"]) auto
-  qed
-  also have "\<dots> = sets ?S"
-    by (simp add: sets_sigma)
-  finally show "sets (Pi\<^isub>P I M) = sets ?S" .
-qed simp_all
-
-lemma (in product_prob_space) measurable_into_infprod_algebra:
-  assumes "sigma_algebra N"
-  assumes f: "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. f x i) \<in> measurable N (M i)"
-  assumes ext: "\<And>x. x \<in> space N \<Longrightarrow> f x \<in> exte