src/HOL/Probability/Sigma_Algebra.thy
author haftmann
Sat Jul 05 11:01:53 2014 +0200 (2014-07-05)
changeset 57514 bdc2c6b40bf2
parent 57512 cc97b347b301
child 58588 93d87fd1583d
permissions -rw-r--r--
prefer ac_simps collections over separate name bindings for add and mult
     1 (*  Title:      HOL/Probability/Sigma_Algebra.thy
     2     Author:     Stefan Richter, Markus Wenzel, TU München
     3     Author:     Johannes Hölzl, TU München
     4     Plus material from the Hurd/Coble measure theory development,
     5     translated by Lawrence Paulson.
     6 *)
     7 
     8 header {* Describing measurable sets *}
     9 
    10 theory Sigma_Algebra
    11 imports
    12   Complex_Main
    13   "~~/src/HOL/Library/Countable_Set"
    14   "~~/src/HOL/Library/FuncSet"
    15   "~~/src/HOL/Library/Indicator_Function"
    16   "~~/src/HOL/Library/Extended_Real"
    17 begin
    18 
    19 text {* Sigma algebras are an elementary concept in measure
    20   theory. To measure --- that is to integrate --- functions, we first have
    21   to measure sets. Unfortunately, when dealing with a large universe,
    22   it is often not possible to consistently assign a measure to every
    23   subset. Therefore it is necessary to define the set of measurable
    24   subsets of the universe. A sigma algebra is such a set that has
    25   three very natural and desirable properties. *}
    26 
    27 subsection {* Families of sets *}
    28 
    29 locale subset_class =
    30   fixes \<Omega> :: "'a set" and M :: "'a set set"
    31   assumes space_closed: "M \<subseteq> Pow \<Omega>"
    32 
    33 lemma (in subset_class) sets_into_space: "x \<in> M \<Longrightarrow> x \<subseteq> \<Omega>"
    34   by (metis PowD contra_subsetD space_closed)
    35 
    36 subsubsection {* Semiring of sets *}
    37 
    38 definition "disjoint A \<longleftrightarrow> (\<forall>a\<in>A. \<forall>b\<in>A. a \<noteq> b \<longrightarrow> a \<inter> b = {})"
    39 
    40 lemma disjointI:
    41   "(\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> a \<inter> b = {}) \<Longrightarrow> disjoint A"
    42   unfolding disjoint_def by auto
    43 
    44 lemma disjointD:
    45   "disjoint A \<Longrightarrow> a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> a \<inter> b = {}"
    46   unfolding disjoint_def by auto
    47 
    48 lemma disjoint_empty[iff]: "disjoint {}"
    49   by (auto simp: disjoint_def)
    50 
    51 lemma disjoint_union: 
    52   assumes C: "disjoint C" and B: "disjoint B" and disj: "\<Union>C \<inter> \<Union>B = {}"
    53   shows "disjoint (C \<union> B)"
    54 proof (rule disjointI)
    55   fix c d assume sets: "c \<in> C \<union> B" "d \<in> C \<union> B" and "c \<noteq> d"
    56   show "c \<inter> d = {}"
    57   proof cases
    58     assume "(c \<in> C \<and> d \<in> C) \<or> (c \<in> B \<and> d \<in> B)"
    59     then show ?thesis
    60     proof 
    61       assume "c \<in> C \<and> d \<in> C" with `c \<noteq> d` C show "c \<inter> d = {}"
    62         by (auto simp: disjoint_def)
    63     next
    64       assume "c \<in> B \<and> d \<in> B" with `c \<noteq> d` B show "c \<inter> d = {}"
    65         by (auto simp: disjoint_def)
    66     qed
    67   next
    68     assume "\<not> ((c \<in> C \<and> d \<in> C) \<or> (c \<in> B \<and> d \<in> B))"
    69     with sets have "(c \<subseteq> \<Union>C \<and> d \<subseteq> \<Union>B) \<or> (c \<subseteq> \<Union>B \<and> d \<subseteq> \<Union>C)"
    70       by auto
    71     with disj show "c \<inter> d = {}" by auto
    72   qed
    73 qed
    74 
    75 lemma disjoint_singleton [simp]: "disjoint {A}"
    76 by(simp add: disjoint_def)
    77 
    78 locale semiring_of_sets = subset_class +
    79   assumes empty_sets[iff]: "{} \<in> M"
    80   assumes Int[intro]: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<inter> b \<in> M"
    81   assumes Diff_cover:
    82     "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> \<exists>C\<subseteq>M. finite C \<and> disjoint C \<and> a - b = \<Union>C"
    83 
    84 lemma (in semiring_of_sets) finite_INT[intro]:
    85   assumes "finite I" "I \<noteq> {}" "\<And>i. i \<in> I \<Longrightarrow> A i \<in> M"
    86   shows "(\<Inter>i\<in>I. A i) \<in> M"
    87   using assms by (induct rule: finite_ne_induct) auto
    88 
    89 lemma (in semiring_of_sets) Int_space_eq1 [simp]: "x \<in> M \<Longrightarrow> \<Omega> \<inter> x = x"
    90   by (metis Int_absorb1 sets_into_space)
    91 
    92 lemma (in semiring_of_sets) Int_space_eq2 [simp]: "x \<in> M \<Longrightarrow> x \<inter> \<Omega> = x"
    93   by (metis Int_absorb2 sets_into_space)
    94 
    95 lemma (in semiring_of_sets) sets_Collect_conj:
    96   assumes "{x\<in>\<Omega>. P x} \<in> M" "{x\<in>\<Omega>. Q x} \<in> M"
    97   shows "{x\<in>\<Omega>. Q x \<and> P x} \<in> M"
    98 proof -
    99   have "{x\<in>\<Omega>. Q x \<and> P x} = {x\<in>\<Omega>. Q x} \<inter> {x\<in>\<Omega>. P x}"
   100     by auto
   101   with assms show ?thesis by auto
   102 qed
   103 
   104 lemma (in semiring_of_sets) sets_Collect_finite_All':
   105   assumes "\<And>i. i \<in> S \<Longrightarrow> {x\<in>\<Omega>. P i x} \<in> M" "finite S" "S \<noteq> {}"
   106   shows "{x\<in>\<Omega>. \<forall>i\<in>S. P i x} \<in> M"
   107 proof -
   108   have "{x\<in>\<Omega>. \<forall>i\<in>S. P i x} = (\<Inter>i\<in>S. {x\<in>\<Omega>. P i x})"
   109     using `S \<noteq> {}` by auto
   110   with assms show ?thesis by auto
   111 qed
   112 
   113 locale ring_of_sets = semiring_of_sets +
   114   assumes Un [intro]: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<union> b \<in> M"
   115 
   116 lemma (in ring_of_sets) finite_Union [intro]:
   117   "finite X \<Longrightarrow> X \<subseteq> M \<Longrightarrow> Union X \<in> M"
   118   by (induct set: finite) (auto simp add: Un)
   119 
   120 lemma (in ring_of_sets) finite_UN[intro]:
   121   assumes "finite I" and "\<And>i. i \<in> I \<Longrightarrow> A i \<in> M"
   122   shows "(\<Union>i\<in>I. A i) \<in> M"
   123   using assms by induct auto
   124 
   125 lemma (in ring_of_sets) Diff [intro]:
   126   assumes "a \<in> M" "b \<in> M" shows "a - b \<in> M"
   127   using Diff_cover[OF assms] by auto
   128 
   129 lemma ring_of_setsI:
   130   assumes space_closed: "M \<subseteq> Pow \<Omega>"
   131   assumes empty_sets[iff]: "{} \<in> M"
   132   assumes Un[intro]: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<union> b \<in> M"
   133   assumes Diff[intro]: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a - b \<in> M"
   134   shows "ring_of_sets \<Omega> M"
   135 proof
   136   fix a b assume ab: "a \<in> M" "b \<in> M"
   137   from ab show "\<exists>C\<subseteq>M. finite C \<and> disjoint C \<and> a - b = \<Union>C"
   138     by (intro exI[of _ "{a - b}"]) (auto simp: disjoint_def)
   139   have "a \<inter> b = a - (a - b)" by auto
   140   also have "\<dots> \<in> M" using ab by auto
   141   finally show "a \<inter> b \<in> M" .
   142 qed fact+
   143 
   144 lemma ring_of_sets_iff: "ring_of_sets \<Omega> M \<longleftrightarrow> M \<subseteq> Pow \<Omega> \<and> {} \<in> M \<and> (\<forall>a\<in>M. \<forall>b\<in>M. a \<union> b \<in> M) \<and> (\<forall>a\<in>M. \<forall>b\<in>M. a - b \<in> M)"
   145 proof
   146   assume "ring_of_sets \<Omega> M"
   147   then interpret ring_of_sets \<Omega> M .
   148   show "M \<subseteq> Pow \<Omega> \<and> {} \<in> M \<and> (\<forall>a\<in>M. \<forall>b\<in>M. a \<union> b \<in> M) \<and> (\<forall>a\<in>M. \<forall>b\<in>M. a - b \<in> M)"
   149     using space_closed by auto
   150 qed (auto intro!: ring_of_setsI)
   151 
   152 lemma (in ring_of_sets) insert_in_sets:
   153   assumes "{x} \<in> M" "A \<in> M" shows "insert x A \<in> M"
   154 proof -
   155   have "{x} \<union> A \<in> M" using assms by (rule Un)
   156   thus ?thesis by auto
   157 qed
   158 
   159 lemma (in ring_of_sets) sets_Collect_disj:
   160   assumes "{x\<in>\<Omega>. P x} \<in> M" "{x\<in>\<Omega>. Q x} \<in> M"
   161   shows "{x\<in>\<Omega>. Q x \<or> P x} \<in> M"
   162 proof -
   163   have "{x\<in>\<Omega>. Q x \<or> P x} = {x\<in>\<Omega>. Q x} \<union> {x\<in>\<Omega>. P x}"
   164     by auto
   165   with assms show ?thesis by auto
   166 qed
   167 
   168 lemma (in ring_of_sets) sets_Collect_finite_Ex:
   169   assumes "\<And>i. i \<in> S \<Longrightarrow> {x\<in>\<Omega>. P i x} \<in> M" "finite S"
   170   shows "{x\<in>\<Omega>. \<exists>i\<in>S. P i x} \<in> M"
   171 proof -
   172   have "{x\<in>\<Omega>. \<exists>i\<in>S. P i x} = (\<Union>i\<in>S. {x\<in>\<Omega>. P i x})"
   173     by auto
   174   with assms show ?thesis by auto
   175 qed
   176 
   177 locale algebra = ring_of_sets +
   178   assumes top [iff]: "\<Omega> \<in> M"
   179 
   180 lemma (in algebra) compl_sets [intro]:
   181   "a \<in> M \<Longrightarrow> \<Omega> - a \<in> M"
   182   by auto
   183 
   184 lemma algebra_iff_Un:
   185   "algebra \<Omega> M \<longleftrightarrow>
   186     M \<subseteq> Pow \<Omega> \<and>
   187     {} \<in> M \<and>
   188     (\<forall>a \<in> M. \<Omega> - a \<in> M) \<and>
   189     (\<forall>a \<in> M. \<forall> b \<in> M. a \<union> b \<in> M)" (is "_ \<longleftrightarrow> ?Un")
   190 proof
   191   assume "algebra \<Omega> M"
   192   then interpret algebra \<Omega> M .
   193   show ?Un using sets_into_space by auto
   194 next
   195   assume ?Un
   196   then have "\<Omega> \<in> M" by auto
   197   interpret ring_of_sets \<Omega> M
   198   proof (rule ring_of_setsI)
   199     show \<Omega>: "M \<subseteq> Pow \<Omega>" "{} \<in> M"
   200       using `?Un` by auto
   201     fix a b assume a: "a \<in> M" and b: "b \<in> M"
   202     then show "a \<union> b \<in> M" using `?Un` by auto
   203     have "a - b = \<Omega> - ((\<Omega> - a) \<union> b)"
   204       using \<Omega> a b by auto
   205     then show "a - b \<in> M"
   206       using a b  `?Un` by auto
   207   qed
   208   show "algebra \<Omega> M" proof qed fact
   209 qed
   210 
   211 lemma algebra_iff_Int:
   212      "algebra \<Omega> M \<longleftrightarrow>
   213        M \<subseteq> Pow \<Omega> & {} \<in> M &
   214        (\<forall>a \<in> M. \<Omega> - a \<in> M) &
   215        (\<forall>a \<in> M. \<forall> b \<in> M. a \<inter> b \<in> M)" (is "_ \<longleftrightarrow> ?Int")
   216 proof
   217   assume "algebra \<Omega> M"
   218   then interpret algebra \<Omega> M .
   219   show ?Int using sets_into_space by auto
   220 next
   221   assume ?Int
   222   show "algebra \<Omega> M"
   223   proof (unfold algebra_iff_Un, intro conjI ballI)
   224     show \<Omega>: "M \<subseteq> Pow \<Omega>" "{} \<in> M"
   225       using `?Int` by auto
   226     from `?Int` show "\<And>a. a \<in> M \<Longrightarrow> \<Omega> - a \<in> M" by auto
   227     fix a b assume M: "a \<in> M" "b \<in> M"
   228     hence "a \<union> b = \<Omega> - ((\<Omega> - a) \<inter> (\<Omega> - b))"
   229       using \<Omega> by blast
   230     also have "... \<in> M"
   231       using M `?Int` by auto
   232     finally show "a \<union> b \<in> M" .
   233   qed
   234 qed
   235 
   236 lemma (in algebra) sets_Collect_neg:
   237   assumes "{x\<in>\<Omega>. P x} \<in> M"
   238   shows "{x\<in>\<Omega>. \<not> P x} \<in> M"
   239 proof -
   240   have "{x\<in>\<Omega>. \<not> P x} = \<Omega> - {x\<in>\<Omega>. P x}" by auto
   241   with assms show ?thesis by auto
   242 qed
   243 
   244 lemma (in algebra) sets_Collect_imp:
   245   "{x\<in>\<Omega>. P x} \<in> M \<Longrightarrow> {x\<in>\<Omega>. Q x} \<in> M \<Longrightarrow> {x\<in>\<Omega>. Q x \<longrightarrow> P x} \<in> M"
   246   unfolding imp_conv_disj by (intro sets_Collect_disj sets_Collect_neg)
   247 
   248 lemma (in algebra) sets_Collect_const:
   249   "{x\<in>\<Omega>. P} \<in> M"
   250   by (cases P) auto
   251 
   252 lemma algebra_single_set:
   253   "X \<subseteq> S \<Longrightarrow> algebra S { {}, X, S - X, S }"
   254   by (auto simp: algebra_iff_Int)
   255 
   256 subsubsection {* Restricted algebras *}
   257 
   258 abbreviation (in algebra)
   259   "restricted_space A \<equiv> (op \<inter> A) ` M"
   260 
   261 lemma (in algebra) restricted_algebra:
   262   assumes "A \<in> M" shows "algebra A (restricted_space A)"
   263   using assms by (auto simp: algebra_iff_Int)
   264 
   265 subsubsection {* Sigma Algebras *}
   266 
   267 locale sigma_algebra = algebra +
   268   assumes countable_nat_UN [intro]: "\<And>A. range A \<subseteq> M \<Longrightarrow> (\<Union>i::nat. A i) \<in> M"
   269 
   270 lemma (in algebra) is_sigma_algebra:
   271   assumes "finite M"
   272   shows "sigma_algebra \<Omega> M"
   273 proof
   274   fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> M"
   275   then have "(\<Union>i. A i) = (\<Union>s\<in>M \<inter> range A. s)"
   276     by auto
   277   also have "(\<Union>s\<in>M \<inter> range A. s) \<in> M"
   278     using `finite M` by auto
   279   finally show "(\<Union>i. A i) \<in> M" .
   280 qed
   281 
   282 lemma countable_UN_eq:
   283   fixes A :: "'i::countable \<Rightarrow> 'a set"
   284   shows "(range A \<subseteq> M \<longrightarrow> (\<Union>i. A i) \<in> M) \<longleftrightarrow>
   285     (range (A \<circ> from_nat) \<subseteq> M \<longrightarrow> (\<Union>i. (A \<circ> from_nat) i) \<in> M)"
   286 proof -
   287   let ?A' = "A \<circ> from_nat"
   288   have *: "(\<Union>i. ?A' i) = (\<Union>i. A i)" (is "?l = ?r")
   289   proof safe
   290     fix x i assume "x \<in> A i" thus "x \<in> ?l"
   291       by (auto intro!: exI[of _ "to_nat i"])
   292   next
   293     fix x i assume "x \<in> ?A' i" thus "x \<in> ?r"
   294       by (auto intro!: exI[of _ "from_nat i"])
   295   qed
   296   have **: "range ?A' = range A"
   297     using surj_from_nat
   298     by (auto simp: image_comp [symmetric] intro!: imageI)
   299   show ?thesis unfolding * ** ..
   300 qed
   301 
   302 lemma (in sigma_algebra) countable_Union [intro]:
   303   assumes "countable X" "X \<subseteq> M" shows "Union X \<in> M"
   304 proof cases
   305   assume "X \<noteq> {}"
   306   hence "\<Union>X = (\<Union>n. from_nat_into X n)"
   307     using assms by (auto intro: from_nat_into) (metis from_nat_into_surj)
   308   also have "\<dots> \<in> M" using assms
   309     by (auto intro!: countable_nat_UN) (metis `X \<noteq> {}` from_nat_into set_mp)
   310   finally show ?thesis .
   311 qed simp
   312 
   313 lemma (in sigma_algebra) countable_UN[intro]:
   314   fixes A :: "'i::countable \<Rightarrow> 'a set"
   315   assumes "A`X \<subseteq> M"
   316   shows  "(\<Union>x\<in>X. A x) \<in> M"
   317 proof -
   318   let ?A = "\<lambda>i. if i \<in> X then A i else {}"
   319   from assms have "range ?A \<subseteq> M" by auto
   320   with countable_nat_UN[of "?A \<circ> from_nat"] countable_UN_eq[of ?A M]
   321   have "(\<Union>x. ?A x) \<in> M" by auto
   322   moreover have "(\<Union>x. ?A x) = (\<Union>x\<in>X. A x)" by (auto split: split_if_asm)
   323   ultimately show ?thesis by simp
   324 qed
   325 
   326 lemma (in sigma_algebra) countable_UN':
   327   fixes A :: "'i \<Rightarrow> 'a set"
   328   assumes X: "countable X"
   329   assumes A: "A`X \<subseteq> M"
   330   shows  "(\<Union>x\<in>X. A x) \<in> M"
   331 proof -
   332   have "(\<Union>x\<in>X. A x) = (\<Union>i\<in>to_nat_on X ` X. A (from_nat_into X i))"
   333     using X by auto
   334   also have "\<dots> \<in> M"
   335     using A X
   336     by (intro countable_UN) auto
   337   finally show ?thesis .
   338 qed
   339 
   340 lemma (in sigma_algebra) countable_INT [intro]:
   341   fixes A :: "'i::countable \<Rightarrow> 'a set"
   342   assumes A: "A`X \<subseteq> M" "X \<noteq> {}"
   343   shows "(\<Inter>i\<in>X. A i) \<in> M"
   344 proof -
   345   from A have "\<forall>i\<in>X. A i \<in> M" by fast
   346   hence "\<Omega> - (\<Union>i\<in>X. \<Omega> - A i) \<in> M" by blast
   347   moreover
   348   have "(\<Inter>i\<in>X. A i) = \<Omega> - (\<Union>i\<in>X. \<Omega> - A i)" using space_closed A
   349     by blast
   350   ultimately show ?thesis by metis
   351 qed
   352 
   353 lemma (in sigma_algebra) countable_INT':
   354   fixes A :: "'i \<Rightarrow> 'a set"
   355   assumes X: "countable X" "X \<noteq> {}"
   356   assumes A: "A`X \<subseteq> M"
   357   shows  "(\<Inter>x\<in>X. A x) \<in> M"
   358 proof -
   359   have "(\<Inter>x\<in>X. A x) = (\<Inter>i\<in>to_nat_on X ` X. A (from_nat_into X i))"
   360     using X by auto
   361   also have "\<dots> \<in> M"
   362     using A X
   363     by (intro countable_INT) auto
   364   finally show ?thesis .
   365 qed
   366 
   367 
   368 lemma (in sigma_algebra) countable:
   369   assumes "\<And>a. a \<in> A \<Longrightarrow> {a} \<in> M" "countable A"
   370   shows "A \<in> M"
   371 proof -
   372   have "(\<Union>a\<in>A. {a}) \<in> M"
   373     using assms by (intro countable_UN') auto
   374   also have "(\<Union>a\<in>A. {a}) = A" by auto
   375   finally show ?thesis by auto
   376 qed
   377 
   378 lemma ring_of_sets_Pow: "ring_of_sets sp (Pow sp)"
   379   by (auto simp: ring_of_sets_iff)
   380 
   381 lemma algebra_Pow: "algebra sp (Pow sp)"
   382   by (auto simp: algebra_iff_Un)
   383 
   384 lemma sigma_algebra_iff:
   385   "sigma_algebra \<Omega> M \<longleftrightarrow>
   386     algebra \<Omega> M \<and> (\<forall>A. range A \<subseteq> M \<longrightarrow> (\<Union>i::nat. A i) \<in> M)"
   387   by (simp add: sigma_algebra_def sigma_algebra_axioms_def)
   388 
   389 lemma sigma_algebra_Pow: "sigma_algebra sp (Pow sp)"
   390   by (auto simp: sigma_algebra_iff algebra_iff_Int)
   391 
   392 lemma (in sigma_algebra) sets_Collect_countable_All:
   393   assumes "\<And>i. {x\<in>\<Omega>. P i x} \<in> M"
   394   shows "{x\<in>\<Omega>. \<forall>i::'i::countable. P i x} \<in> M"
   395 proof -
   396   have "{x\<in>\<Omega>. \<forall>i::'i::countable. P i x} = (\<Inter>i. {x\<in>\<Omega>. P i x})" by auto
   397   with assms show ?thesis by auto
   398 qed
   399 
   400 lemma (in sigma_algebra) sets_Collect_countable_Ex:
   401   assumes "\<And>i. {x\<in>\<Omega>. P i x} \<in> M"
   402   shows "{x\<in>\<Omega>. \<exists>i::'i::countable. P i x} \<in> M"
   403 proof -
   404   have "{x\<in>\<Omega>. \<exists>i::'i::countable. P i x} = (\<Union>i. {x\<in>\<Omega>. P i x})" by auto
   405   with assms show ?thesis by auto
   406 qed
   407 
   408 lemma (in sigma_algebra) sets_Collect_countable_Ex':
   409   assumes "\<And>i. i \<in> I \<Longrightarrow> {x\<in>\<Omega>. P i x} \<in> M"
   410   assumes "countable I"
   411   shows "{x\<in>\<Omega>. \<exists>i\<in>I. P i x} \<in> M"
   412 proof -
   413   have "{x\<in>\<Omega>. \<exists>i\<in>I. P i x} = (\<Union>i\<in>I. {x\<in>\<Omega>. P i x})" by auto
   414   with assms show ?thesis 
   415     by (auto intro!: countable_UN')
   416 qed
   417 
   418 lemma (in sigma_algebra) sets_Collect_countable_All':
   419   assumes "\<And>i. i \<in> I \<Longrightarrow> {x\<in>\<Omega>. P i x} \<in> M"
   420   assumes "countable I"
   421   shows "{x\<in>\<Omega>. \<forall>i\<in>I. P i x} \<in> M"
   422 proof -
   423   have "{x\<in>\<Omega>. \<forall>i\<in>I. P i x} = (\<Inter>i\<in>I. {x\<in>\<Omega>. P i x}) \<inter> \<Omega>" by auto
   424   with assms show ?thesis 
   425     by (cases "I = {}") (auto intro!: countable_INT')
   426 qed
   427 
   428 lemma (in sigma_algebra) sets_Collect_countable_Ex1':
   429   assumes "\<And>i. i \<in> I \<Longrightarrow> {x\<in>\<Omega>. P i x} \<in> M"
   430   assumes "countable I"
   431   shows "{x\<in>\<Omega>. \<exists>!i\<in>I. P i x} \<in> M"
   432 proof -
   433   have "{x\<in>\<Omega>. \<exists>!i\<in>I. P i x} = {x\<in>\<Omega>. \<exists>i\<in>I. P i x \<and> (\<forall>j\<in>I. P j x \<longrightarrow> i = j)}"
   434     by auto
   435   with assms show ?thesis 
   436     by (auto intro!: sets_Collect_countable_All' sets_Collect_countable_Ex' sets_Collect_conj sets_Collect_imp sets_Collect_const)
   437 qed
   438 
   439 lemmas (in sigma_algebra) sets_Collect =
   440   sets_Collect_imp sets_Collect_disj sets_Collect_conj sets_Collect_neg sets_Collect_const
   441   sets_Collect_countable_All sets_Collect_countable_Ex sets_Collect_countable_All
   442 
   443 lemma (in sigma_algebra) sets_Collect_countable_Ball:
   444   assumes "\<And>i. {x\<in>\<Omega>. P i x} \<in> M"
   445   shows "{x\<in>\<Omega>. \<forall>i::'i::countable\<in>X. P i x} \<in> M"
   446   unfolding Ball_def by (intro sets_Collect assms)
   447 
   448 lemma (in sigma_algebra) sets_Collect_countable_Bex:
   449   assumes "\<And>i. {x\<in>\<Omega>. P i x} \<in> M"
   450   shows "{x\<in>\<Omega>. \<exists>i::'i::countable\<in>X. P i x} \<in> M"
   451   unfolding Bex_def by (intro sets_Collect assms)
   452 
   453 lemma sigma_algebra_single_set:
   454   assumes "X \<subseteq> S"
   455   shows "sigma_algebra S { {}, X, S - X, S }"
   456   using algebra.is_sigma_algebra[OF algebra_single_set[OF `X \<subseteq> S`]] by simp
   457 
   458 subsubsection {* Binary Unions *}
   459 
   460 definition binary :: "'a \<Rightarrow> 'a \<Rightarrow> nat \<Rightarrow> 'a"
   461   where "binary a b =  (\<lambda>x. b)(0 := a)"
   462 
   463 lemma range_binary_eq: "range(binary a b) = {a,b}"
   464   by (auto simp add: binary_def)
   465 
   466 lemma Un_range_binary: "a \<union> b = (\<Union>i::nat. binary a b i)"
   467   by (simp add: SUP_def range_binary_eq)
   468 
   469 lemma Int_range_binary: "a \<inter> b = (\<Inter>i::nat. binary a b i)"
   470   by (simp add: INF_def range_binary_eq)
   471 
   472 lemma sigma_algebra_iff2:
   473      "sigma_algebra \<Omega> M \<longleftrightarrow>
   474        M \<subseteq> Pow \<Omega> \<and>
   475        {} \<in> M \<and> (\<forall>s \<in> M. \<Omega> - s \<in> M) \<and>
   476        (\<forall>A. range A \<subseteq> M \<longrightarrow> (\<Union>i::nat. A i) \<in> M)"
   477   by (auto simp add: range_binary_eq sigma_algebra_def sigma_algebra_axioms_def
   478          algebra_iff_Un Un_range_binary)
   479 
   480 subsubsection {* Initial Sigma Algebra *}
   481 
   482 text {*Sigma algebras can naturally be created as the closure of any set of
   483   M with regard to the properties just postulated.  *}
   484 
   485 inductive_set sigma_sets :: "'a set \<Rightarrow> 'a set set \<Rightarrow> 'a set set"
   486   for sp :: "'a set" and A :: "'a set set"
   487   where
   488     Basic[intro, simp]: "a \<in> A \<Longrightarrow> a \<in> sigma_sets sp A"
   489   | Empty: "{} \<in> sigma_sets sp A"
   490   | Compl: "a \<in> sigma_sets sp A \<Longrightarrow> sp - a \<in> sigma_sets sp A"
   491   | Union: "(\<And>i::nat. a i \<in> sigma_sets sp A) \<Longrightarrow> (\<Union>i. a i) \<in> sigma_sets sp A"
   492 
   493 lemma (in sigma_algebra) sigma_sets_subset:
   494   assumes a: "a \<subseteq> M"
   495   shows "sigma_sets \<Omega> a \<subseteq> M"
   496 proof
   497   fix x
   498   assume "x \<in> sigma_sets \<Omega> a"
   499   from this show "x \<in> M"
   500     by (induct rule: sigma_sets.induct, auto) (metis a subsetD)
   501 qed
   502 
   503 lemma sigma_sets_into_sp: "A \<subseteq> Pow sp \<Longrightarrow> x \<in> sigma_sets sp A \<Longrightarrow> x \<subseteq> sp"
   504   by (erule sigma_sets.induct, auto)
   505 
   506 lemma sigma_algebra_sigma_sets:
   507      "a \<subseteq> Pow \<Omega> \<Longrightarrow> sigma_algebra \<Omega> (sigma_sets \<Omega> a)"
   508   by (auto simp add: sigma_algebra_iff2 dest: sigma_sets_into_sp
   509            intro!: sigma_sets.Union sigma_sets.Empty sigma_sets.Compl)
   510 
   511 lemma sigma_sets_least_sigma_algebra:
   512   assumes "A \<subseteq> Pow S"
   513   shows "sigma_sets S A = \<Inter>{B. A \<subseteq> B \<and> sigma_algebra S B}"
   514 proof safe
   515   fix B X assume "A \<subseteq> B" and sa: "sigma_algebra S B"
   516     and X: "X \<in> sigma_sets S A"
   517   from sigma_algebra.sigma_sets_subset[OF sa, simplified, OF `A \<subseteq> B`] X
   518   show "X \<in> B" by auto
   519 next
   520   fix X assume "X \<in> \<Inter>{B. A \<subseteq> B \<and> sigma_algebra S B}"
   521   then have [intro!]: "\<And>B. A \<subseteq> B \<Longrightarrow> sigma_algebra S B \<Longrightarrow> X \<in> B"
   522      by simp
   523   have "A \<subseteq> sigma_sets S A" using assms by auto
   524   moreover have "sigma_algebra S (sigma_sets S A)"
   525     using assms by (intro sigma_algebra_sigma_sets[of A]) auto
   526   ultimately show "X \<in> sigma_sets S A" by auto
   527 qed
   528 
   529 lemma sigma_sets_top: "sp \<in> sigma_sets sp A"
   530   by (metis Diff_empty sigma_sets.Compl sigma_sets.Empty)
   531 
   532 lemma sigma_sets_Un:
   533   "a \<in> sigma_sets sp A \<Longrightarrow> b \<in> sigma_sets sp A \<Longrightarrow> a \<union> b \<in> sigma_sets sp A"
   534 apply (simp add: Un_range_binary range_binary_eq)
   535 apply (rule Union, simp add: binary_def)
   536 done
   537 
   538 lemma sigma_sets_Inter:
   539   assumes Asb: "A \<subseteq> Pow sp"
   540   shows "(\<And>i::nat. a i \<in> sigma_sets sp A) \<Longrightarrow> (\<Inter>i. a i) \<in> sigma_sets sp A"
   541 proof -
   542   assume ai: "\<And>i::nat. a i \<in> sigma_sets sp A"
   543   hence "\<And>i::nat. sp-(a i) \<in> sigma_sets sp A"
   544     by (rule sigma_sets.Compl)
   545   hence "(\<Union>i. sp-(a i)) \<in> sigma_sets sp A"
   546     by (rule sigma_sets.Union)
   547   hence "sp-(\<Union>i. sp-(a i)) \<in> sigma_sets sp A"
   548     by (rule sigma_sets.Compl)
   549   also have "sp-(\<Union>i. sp-(a i)) = sp Int (\<Inter>i. a i)"
   550     by auto
   551   also have "... = (\<Inter>i. a i)" using ai
   552     by (blast dest: sigma_sets_into_sp [OF Asb])
   553   finally show ?thesis .
   554 qed
   555 
   556 lemma sigma_sets_INTER:
   557   assumes Asb: "A \<subseteq> Pow sp"
   558       and ai: "\<And>i::nat. i \<in> S \<Longrightarrow> a i \<in> sigma_sets sp A" and non: "S \<noteq> {}"
   559   shows "(\<Inter>i\<in>S. a i) \<in> sigma_sets sp A"
   560 proof -
   561   from ai have "\<And>i. (if i\<in>S then a i else sp) \<in> sigma_sets sp A"
   562     by (simp add: sigma_sets.intros(2-) sigma_sets_top)
   563   hence "(\<Inter>i. (if i\<in>S then a i else sp)) \<in> sigma_sets sp A"
   564     by (rule sigma_sets_Inter [OF Asb])
   565   also have "(\<Inter>i. (if i\<in>S then a i else sp)) = (\<Inter>i\<in>S. a i)"
   566     by auto (metis ai non sigma_sets_into_sp subset_empty subset_iff Asb)+
   567   finally show ?thesis .
   568 qed
   569 
   570 lemma sigma_sets_UNION: "countable B \<Longrightarrow> (\<And>b. b \<in> B \<Longrightarrow> b \<in> sigma_sets X A) \<Longrightarrow> (\<Union>B) \<in> sigma_sets X A"
   571   using from_nat_into[of B] range_from_nat_into[of B] sigma_sets.Union[of "from_nat_into B" X A]
   572   apply (cases "B = {}")
   573   apply (simp add: sigma_sets.Empty)
   574   apply (simp del: Union_image_eq add: Union_image_eq[symmetric])
   575   done
   576 
   577 lemma (in sigma_algebra) sigma_sets_eq:
   578      "sigma_sets \<Omega> M = M"
   579 proof
   580   show "M \<subseteq> sigma_sets \<Omega> M"
   581     by (metis Set.subsetI sigma_sets.Basic)
   582   next
   583   show "sigma_sets \<Omega> M \<subseteq> M"
   584     by (metis sigma_sets_subset subset_refl)
   585 qed
   586 
   587 lemma sigma_sets_eqI:
   588   assumes A: "\<And>a. a \<in> A \<Longrightarrow> a \<in> sigma_sets M B"
   589   assumes B: "\<And>b. b \<in> B \<Longrightarrow> b \<in> sigma_sets M A"
   590   shows "sigma_sets M A = sigma_sets M B"
   591 proof (intro set_eqI iffI)
   592   fix a assume "a \<in> sigma_sets M A"
   593   from this A show "a \<in> sigma_sets M B"
   594     by induct (auto intro!: sigma_sets.intros(2-) del: sigma_sets.Basic)
   595 next
   596   fix b assume "b \<in> sigma_sets M B"
   597   from this B show "b \<in> sigma_sets M A"
   598     by induct (auto intro!: sigma_sets.intros(2-) del: sigma_sets.Basic)
   599 qed
   600 
   601 lemma sigma_sets_subseteq: assumes "A \<subseteq> B" shows "sigma_sets X A \<subseteq> sigma_sets X B"
   602 proof
   603   fix x assume "x \<in> sigma_sets X A" then show "x \<in> sigma_sets X B"
   604     by induct (insert `A \<subseteq> B`, auto intro: sigma_sets.intros(2-))
   605 qed
   606 
   607 lemma sigma_sets_mono: assumes "A \<subseteq> sigma_sets X B" shows "sigma_sets X A \<subseteq> sigma_sets X B"
   608 proof
   609   fix x assume "x \<in> sigma_sets X A" then show "x \<in> sigma_sets X B"
   610     by induct (insert `A \<subseteq> sigma_sets X B`, auto intro: sigma_sets.intros(2-))
   611 qed
   612 
   613 lemma sigma_sets_mono': assumes "A \<subseteq> B" shows "sigma_sets X A \<subseteq> sigma_sets X B"
   614 proof
   615   fix x assume "x \<in> sigma_sets X A" then show "x \<in> sigma_sets X B"
   616     by induct (insert `A \<subseteq> B`, auto intro: sigma_sets.intros(2-))
   617 qed
   618 
   619 lemma sigma_sets_superset_generator: "A \<subseteq> sigma_sets X A"
   620   by (auto intro: sigma_sets.Basic)
   621 
   622 lemma (in sigma_algebra) restriction_in_sets:
   623   fixes A :: "nat \<Rightarrow> 'a set"
   624   assumes "S \<in> M"
   625   and *: "range A \<subseteq> (\<lambda>A. S \<inter> A) ` M" (is "_ \<subseteq> ?r")
   626   shows "range A \<subseteq> M" "(\<Union>i. A i) \<in> (\<lambda>A. S \<inter> A) ` M"
   627 proof -
   628   { fix i have "A i \<in> ?r" using * by auto
   629     hence "\<exists>B. A i = B \<inter> S \<and> B \<in> M" by auto
   630     hence "A i \<subseteq> S" "A i \<in> M" using `S \<in> M` by auto }
   631   thus "range A \<subseteq> M" "(\<Union>i. A i) \<in> (\<lambda>A. S \<inter> A) ` M"
   632     by (auto intro!: image_eqI[of _ _ "(\<Union>i. A i)"])
   633 qed
   634 
   635 lemma (in sigma_algebra) restricted_sigma_algebra:
   636   assumes "S \<in> M"
   637   shows "sigma_algebra S (restricted_space S)"
   638   unfolding sigma_algebra_def sigma_algebra_axioms_def
   639 proof safe
   640   show "algebra S (restricted_space S)" using restricted_algebra[OF assms] .
   641 next
   642   fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> restricted_space S"
   643   from restriction_in_sets[OF assms this[simplified]]
   644   show "(\<Union>i. A i) \<in> restricted_space S" by simp
   645 qed
   646 
   647 lemma sigma_sets_Int:
   648   assumes "A \<in> sigma_sets sp st" "A \<subseteq> sp"
   649   shows "op \<inter> A ` sigma_sets sp st = sigma_sets A (op \<inter> A ` st)"
   650 proof (intro equalityI subsetI)
   651   fix x assume "x \<in> op \<inter> A ` sigma_sets sp st"
   652   then obtain y where "y \<in> sigma_sets sp st" "x = y \<inter> A" by auto
   653   then have "x \<in> sigma_sets (A \<inter> sp) (op \<inter> A ` st)"
   654   proof (induct arbitrary: x)
   655     case (Compl a)
   656     then show ?case
   657       by (force intro!: sigma_sets.Compl simp: Diff_Int_distrib ac_simps)
   658   next
   659     case (Union a)
   660     then show ?case
   661       by (auto intro!: sigma_sets.Union
   662                simp add: UN_extend_simps simp del: UN_simps)
   663   qed (auto intro!: sigma_sets.intros(2-))
   664   then show "x \<in> sigma_sets A (op \<inter> A ` st)"
   665     using `A \<subseteq> sp` by (simp add: Int_absorb2)
   666 next
   667   fix x assume "x \<in> sigma_sets A (op \<inter> A ` st)"
   668   then show "x \<in> op \<inter> A ` sigma_sets sp st"
   669   proof induct
   670     case (Compl a)
   671     then obtain x where "a = A \<inter> x" "x \<in> sigma_sets sp st" by auto
   672     then show ?case using `A \<subseteq> sp`
   673       by (force simp add: image_iff intro!: bexI[of _ "sp - x"] sigma_sets.Compl)
   674   next
   675     case (Union a)
   676     then have "\<forall>i. \<exists>x. x \<in> sigma_sets sp st \<and> a i = A \<inter> x"
   677       by (auto simp: image_iff Bex_def)
   678     from choice[OF this] guess f ..
   679     then show ?case
   680       by (auto intro!: bexI[of _ "(\<Union>x. f x)"] sigma_sets.Union
   681                simp add: image_iff)
   682   qed (auto intro!: sigma_sets.intros(2-))
   683 qed
   684 
   685 lemma sigma_sets_empty_eq: "sigma_sets A {} = {{}, A}"
   686 proof (intro set_eqI iffI)
   687   fix a assume "a \<in> sigma_sets A {}" then show "a \<in> {{}, A}"
   688     by induct blast+
   689 qed (auto intro: sigma_sets.Empty sigma_sets_top)
   690 
   691 lemma sigma_sets_single[simp]: "sigma_sets A {A} = {{}, A}"
   692 proof (intro set_eqI iffI)
   693   fix x assume "x \<in> sigma_sets A {A}"
   694   then show "x \<in> {{}, A}"
   695     by induct blast+
   696 next
   697   fix x assume "x \<in> {{}, A}"
   698   then show "x \<in> sigma_sets A {A}"
   699     by (auto intro: sigma_sets.Empty sigma_sets_top)
   700 qed
   701 
   702 lemma sigma_sets_sigma_sets_eq:
   703   "M \<subseteq> Pow S \<Longrightarrow> sigma_sets S (sigma_sets S M) = sigma_sets S M"
   704   by (rule sigma_algebra.sigma_sets_eq[OF sigma_algebra_sigma_sets, of M S]) auto
   705 
   706 lemma sigma_sets_singleton:
   707   assumes "X \<subseteq> S"
   708   shows "sigma_sets S { X } = { {}, X, S - X, S }"
   709 proof -
   710   interpret sigma_algebra S "{ {}, X, S - X, S }"
   711     by (rule sigma_algebra_single_set) fact
   712   have "sigma_sets S { X } \<subseteq> sigma_sets S { {}, X, S - X, S }"
   713     by (rule sigma_sets_subseteq) simp
   714   moreover have "\<dots> = { {}, X, S - X, S }"
   715     using sigma_sets_eq by simp
   716   moreover
   717   { fix A assume "A \<in> { {}, X, S - X, S }"
   718     then have "A \<in> sigma_sets S { X }"
   719       by (auto intro: sigma_sets.intros(2-) sigma_sets_top) }
   720   ultimately have "sigma_sets S { X } = sigma_sets S { {}, X, S - X, S }"
   721     by (intro antisym) auto
   722   with sigma_sets_eq show ?thesis by simp
   723 qed
   724 
   725 lemma restricted_sigma:
   726   assumes S: "S \<in> sigma_sets \<Omega> M" and M: "M \<subseteq> Pow \<Omega>"
   727   shows "algebra.restricted_space (sigma_sets \<Omega> M) S =
   728     sigma_sets S (algebra.restricted_space M S)"
   729 proof -
   730   from S sigma_sets_into_sp[OF M]
   731   have "S \<in> sigma_sets \<Omega> M" "S \<subseteq> \<Omega>" by auto
   732   from sigma_sets_Int[OF this]
   733   show ?thesis by simp
   734 qed
   735 
   736 lemma sigma_sets_vimage_commute:
   737   assumes X: "X \<in> \<Omega> \<rightarrow> \<Omega>'"
   738   shows "{X -` A \<inter> \<Omega> |A. A \<in> sigma_sets \<Omega>' M'}
   739        = sigma_sets \<Omega> {X -` A \<inter> \<Omega> |A. A \<in> M'}" (is "?L = ?R")
   740 proof
   741   show "?L \<subseteq> ?R"
   742   proof clarify
   743     fix A assume "A \<in> sigma_sets \<Omega>' M'"
   744     then show "X -` A \<inter> \<Omega> \<in> ?R"
   745     proof induct
   746       case Empty then show ?case
   747         by (auto intro!: sigma_sets.Empty)
   748     next
   749       case (Compl B)
   750       have [simp]: "X -` (\<Omega>' - B) \<inter> \<Omega> = \<Omega> - (X -` B \<inter> \<Omega>)"
   751         by (auto simp add: funcset_mem [OF X])
   752       with Compl show ?case
   753         by (auto intro!: sigma_sets.Compl)
   754     next
   755       case (Union F)
   756       then show ?case
   757         by (auto simp add: vimage_UN UN_extend_simps(4) simp del: UN_simps
   758                  intro!: sigma_sets.Union)
   759     qed auto
   760   qed
   761   show "?R \<subseteq> ?L"
   762   proof clarify
   763     fix A assume "A \<in> ?R"
   764     then show "\<exists>B. A = X -` B \<inter> \<Omega> \<and> B \<in> sigma_sets \<Omega>' M'"
   765     proof induct
   766       case (Basic B) then show ?case by auto
   767     next
   768       case Empty then show ?case
   769         by (auto intro!: sigma_sets.Empty exI[of _ "{}"])
   770     next
   771       case (Compl B)
   772       then obtain A where A: "B = X -` A \<inter> \<Omega>" "A \<in> sigma_sets \<Omega>' M'" by auto
   773       then have [simp]: "\<Omega> - B = X -` (\<Omega>' - A) \<inter> \<Omega>"
   774         by (auto simp add: funcset_mem [OF X])
   775       with A(2) show ?case
   776         by (auto intro: sigma_sets.Compl)
   777     next
   778       case (Union F)
   779       then have "\<forall>i. \<exists>B. F i = X -` B \<inter> \<Omega> \<and> B \<in> sigma_sets \<Omega>' M'" by auto
   780       from choice[OF this] guess A .. note A = this
   781       with A show ?case
   782         by (auto simp: vimage_UN[symmetric] intro: sigma_sets.Union)
   783     qed
   784   qed
   785 qed
   786 
   787 subsubsection "Disjoint families"
   788 
   789 definition
   790   disjoint_family_on  where
   791   "disjoint_family_on A S \<longleftrightarrow> (\<forall>m\<in>S. \<forall>n\<in>S. m \<noteq> n \<longrightarrow> A m \<inter> A n = {})"
   792 
   793 abbreviation
   794   "disjoint_family A \<equiv> disjoint_family_on A UNIV"
   795 
   796 lemma range_subsetD: "range f \<subseteq> B \<Longrightarrow> f i \<in> B"
   797   by blast
   798 
   799 lemma disjoint_family_onD: "disjoint_family_on A I \<Longrightarrow> i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> i \<noteq> j \<Longrightarrow> A i \<inter> A j = {}"
   800   by (auto simp: disjoint_family_on_def)
   801 
   802 lemma Int_Diff_disjoint: "A \<inter> B \<inter> (A - B) = {}"
   803   by blast
   804 
   805 lemma Int_Diff_Un: "A \<inter> B \<union> (A - B) = A"
   806   by blast
   807 
   808 lemma disjoint_family_subset:
   809      "disjoint_family A \<Longrightarrow> (!!x. B x \<subseteq> A x) \<Longrightarrow> disjoint_family B"
   810   by (force simp add: disjoint_family_on_def)
   811 
   812 lemma disjoint_family_on_bisimulation:
   813   assumes "disjoint_family_on f S"
   814   and "\<And>n m. n \<in> S \<Longrightarrow> m \<in> S \<Longrightarrow> n \<noteq> m \<Longrightarrow> f n \<inter> f m = {} \<Longrightarrow> g n \<inter> g m = {}"
   815   shows "disjoint_family_on g S"
   816   using assms unfolding disjoint_family_on_def by auto
   817 
   818 lemma disjoint_family_on_mono:
   819   "A \<subseteq> B \<Longrightarrow> disjoint_family_on f B \<Longrightarrow> disjoint_family_on f A"
   820   unfolding disjoint_family_on_def by auto
   821 
   822 lemma disjoint_family_Suc:
   823   assumes Suc: "!!n. A n \<subseteq> A (Suc n)"
   824   shows "disjoint_family (\<lambda>i. A (Suc i) - A i)"
   825 proof -
   826   {
   827     fix m
   828     have "!!n. A n \<subseteq> A (m+n)"
   829     proof (induct m)
   830       case 0 show ?case by simp
   831     next
   832       case (Suc m) thus ?case
   833         by (metis Suc_eq_plus1 assms add.commute add.left_commute subset_trans)
   834     qed
   835   }
   836   hence "!!m n. m < n \<Longrightarrow> A m \<subseteq> A n"
   837     by (metis add.commute le_add_diff_inverse nat_less_le)
   838   thus ?thesis
   839     by (auto simp add: disjoint_family_on_def)
   840       (metis insert_absorb insert_subset le_SucE le_antisym not_leE)
   841 qed
   842 
   843 lemma setsum_indicator_disjoint_family:
   844   fixes f :: "'d \<Rightarrow> 'e::semiring_1"
   845   assumes d: "disjoint_family_on A P" and "x \<in> A j" and "finite P" and "j \<in> P"
   846   shows "(\<Sum>i\<in>P. f i * indicator (A i) x) = f j"
   847 proof -
   848   have "P \<inter> {i. x \<in> A i} = {j}"
   849     using d `x \<in> A j` `j \<in> P` unfolding disjoint_family_on_def
   850     by auto
   851   thus ?thesis
   852     unfolding indicator_def
   853     by (simp add: if_distrib setsum.If_cases[OF `finite P`])
   854 qed
   855 
   856 definition disjointed :: "(nat \<Rightarrow> 'a set) \<Rightarrow> nat \<Rightarrow> 'a set "
   857   where "disjointed A n = A n - (\<Union>i\<in>{0..<n}. A i)"
   858 
   859 lemma finite_UN_disjointed_eq: "(\<Union>i\<in>{0..<n}. disjointed A i) = (\<Union>i\<in>{0..<n}. A i)"
   860 proof (induct n)
   861   case 0 show ?case by simp
   862 next
   863   case (Suc n)
   864   thus ?case by (simp add: atLeastLessThanSuc disjointed_def)
   865 qed
   866 
   867 lemma UN_disjointed_eq: "(\<Union>i. disjointed A i) = (\<Union>i. A i)"
   868   apply (rule UN_finite2_eq [where k=0])
   869   apply (simp add: finite_UN_disjointed_eq)
   870   done
   871 
   872 lemma less_disjoint_disjointed: "m<n \<Longrightarrow> disjointed A m \<inter> disjointed A n = {}"
   873   by (auto simp add: disjointed_def)
   874 
   875 lemma disjoint_family_disjointed: "disjoint_family (disjointed A)"
   876   by (simp add: disjoint_family_on_def)
   877      (metis neq_iff Int_commute less_disjoint_disjointed)
   878 
   879 lemma disjointed_subset: "disjointed A n \<subseteq> A n"
   880   by (auto simp add: disjointed_def)
   881 
   882 lemma (in ring_of_sets) UNION_in_sets:
   883   fixes A:: "nat \<Rightarrow> 'a set"
   884   assumes A: "range A \<subseteq> M"
   885   shows  "(\<Union>i\<in>{0..<n}. A i) \<in> M"
   886 proof (induct n)
   887   case 0 show ?case by simp
   888 next
   889   case (Suc n)
   890   thus ?case
   891     by (simp add: atLeastLessThanSuc) (metis A Un UNIV_I image_subset_iff)
   892 qed
   893 
   894 lemma (in ring_of_sets) range_disjointed_sets:
   895   assumes A: "range A \<subseteq> M"
   896   shows  "range (disjointed A) \<subseteq> M"
   897 proof (auto simp add: disjointed_def)
   898   fix n
   899   show "A n - (\<Union>i\<in>{0..<n}. A i) \<in> M" using UNION_in_sets
   900     by (metis A Diff UNIV_I image_subset_iff)
   901 qed
   902 
   903 lemma (in algebra) range_disjointed_sets':
   904   "range A \<subseteq> M \<Longrightarrow> range (disjointed A) \<subseteq> M"
   905   using range_disjointed_sets .
   906 
   907 lemma disjointed_0[simp]: "disjointed A 0 = A 0"
   908   by (simp add: disjointed_def)
   909 
   910 lemma incseq_Un:
   911   "incseq A \<Longrightarrow> (\<Union>i\<le>n. A i) = A n"
   912   unfolding incseq_def by auto
   913 
   914 lemma disjointed_incseq:
   915   "incseq A \<Longrightarrow> disjointed A (Suc n) = A (Suc n) - A n"
   916   using incseq_Un[of A]
   917   by (simp add: disjointed_def atLeastLessThanSuc_atLeastAtMost atLeast0AtMost)
   918 
   919 lemma sigma_algebra_disjoint_iff:
   920   "sigma_algebra \<Omega> M \<longleftrightarrow> algebra \<Omega> M \<and>
   921     (\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i::nat. A i) \<in> M)"
   922 proof (auto simp add: sigma_algebra_iff)
   923   fix A :: "nat \<Rightarrow> 'a set"
   924   assume M: "algebra \<Omega> M"
   925      and A: "range A \<subseteq> M"
   926      and UnA: "\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i::nat. A i) \<in> M"
   927   hence "range (disjointed A) \<subseteq> M \<longrightarrow>
   928          disjoint_family (disjointed A) \<longrightarrow>
   929          (\<Union>i. disjointed A i) \<in> M" by blast
   930   hence "(\<Union>i. disjointed A i) \<in> M"
   931     by (simp add: algebra.range_disjointed_sets'[of \<Omega>] M A disjoint_family_disjointed)
   932   thus "(\<Union>i::nat. A i) \<in> M" by (simp add: UN_disjointed_eq)
   933 qed
   934 
   935 lemma disjoint_family_on_disjoint_image:
   936   "disjoint_family_on A I \<Longrightarrow> disjoint (A ` I)"
   937   unfolding disjoint_family_on_def disjoint_def by force
   938 
   939 lemma disjoint_image_disjoint_family_on:
   940   assumes d: "disjoint (A ` I)" and i: "inj_on A I"
   941   shows "disjoint_family_on A I"
   942   unfolding disjoint_family_on_def
   943 proof (intro ballI impI)
   944   fix n m assume nm: "m \<in> I" "n \<in> I" and "n \<noteq> m"
   945   with i[THEN inj_onD, of n m] show "A n \<inter> A m = {}"
   946     by (intro disjointD[OF d]) auto
   947 qed
   948 
   949 subsubsection {* Ring generated by a semiring *}
   950 
   951 definition (in semiring_of_sets)
   952   "generated_ring = { \<Union>C | C. C \<subseteq> M \<and> finite C \<and> disjoint C }"
   953 
   954 lemma (in semiring_of_sets) generated_ringE[elim?]:
   955   assumes "a \<in> generated_ring"
   956   obtains C where "finite C" "disjoint C" "C \<subseteq> M" "a = \<Union>C"
   957   using assms unfolding generated_ring_def by auto
   958 
   959 lemma (in semiring_of_sets) generated_ringI[intro?]:
   960   assumes "finite C" "disjoint C" "C \<subseteq> M" "a = \<Union>C"
   961   shows "a \<in> generated_ring"
   962   using assms unfolding generated_ring_def by auto
   963 
   964 lemma (in semiring_of_sets) generated_ringI_Basic:
   965   "A \<in> M \<Longrightarrow> A \<in> generated_ring"
   966   by (rule generated_ringI[of "{A}"]) (auto simp: disjoint_def)
   967 
   968 lemma (in semiring_of_sets) generated_ring_disjoint_Un[intro]:
   969   assumes a: "a \<in> generated_ring" and b: "b \<in> generated_ring"
   970   and "a \<inter> b = {}"
   971   shows "a \<union> b \<in> generated_ring"
   972 proof -
   973   from a guess Ca .. note Ca = this
   974   from b guess Cb .. note Cb = this
   975   show ?thesis
   976   proof
   977     show "disjoint (Ca \<union> Cb)"
   978       using `a \<inter> b = {}` Ca Cb by (auto intro!: disjoint_union)
   979   qed (insert Ca Cb, auto)
   980 qed
   981 
   982 lemma (in semiring_of_sets) generated_ring_empty: "{} \<in> generated_ring"
   983   by (auto simp: generated_ring_def disjoint_def)
   984 
   985 lemma (in semiring_of_sets) generated_ring_disjoint_Union:
   986   assumes "finite A" shows "A \<subseteq> generated_ring \<Longrightarrow> disjoint A \<Longrightarrow> \<Union>A \<in> generated_ring"
   987   using assms by (induct A) (auto simp: disjoint_def intro!: generated_ring_disjoint_Un generated_ring_empty)
   988 
   989 lemma (in semiring_of_sets) generated_ring_disjoint_UNION:
   990   "finite I \<Longrightarrow> disjoint (A ` I) \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> A i \<in> generated_ring) \<Longrightarrow> UNION I A \<in> generated_ring"
   991   unfolding SUP_def by (intro generated_ring_disjoint_Union) auto
   992 
   993 lemma (in semiring_of_sets) generated_ring_Int:
   994   assumes a: "a \<in> generated_ring" and b: "b \<in> generated_ring"
   995   shows "a \<inter> b \<in> generated_ring"
   996 proof -
   997   from a guess Ca .. note Ca = this
   998   from b guess Cb .. note Cb = this
   999   def C \<equiv> "(\<lambda>(a,b). a \<inter> b)` (Ca\<times>Cb)"
  1000   show ?thesis
  1001   proof
  1002     show "disjoint C"
  1003     proof (simp add: disjoint_def C_def, intro ballI impI)
  1004       fix a1 b1 a2 b2 assume sets: "a1 \<in> Ca" "b1 \<in> Cb" "a2 \<in> Ca" "b2 \<in> Cb"
  1005       assume "a1 \<inter> b1 \<noteq> a2 \<inter> b2"
  1006       then have "a1 \<noteq> a2 \<or> b1 \<noteq> b2" by auto
  1007       then show "(a1 \<inter> b1) \<inter> (a2 \<inter> b2) = {}"
  1008       proof
  1009         assume "a1 \<noteq> a2"
  1010         with sets Ca have "a1 \<inter> a2 = {}"
  1011           by (auto simp: disjoint_def)
  1012         then show ?thesis by auto
  1013       next
  1014         assume "b1 \<noteq> b2"
  1015         with sets Cb have "b1 \<inter> b2 = {}"
  1016           by (auto simp: disjoint_def)
  1017         then show ?thesis by auto
  1018       qed
  1019     qed
  1020   qed (insert Ca Cb, auto simp: C_def)
  1021 qed
  1022 
  1023 lemma (in semiring_of_sets) generated_ring_Inter:
  1024   assumes "finite A" "A \<noteq> {}" shows "A \<subseteq> generated_ring \<Longrightarrow> \<Inter>A \<in> generated_ring"
  1025   using assms by (induct A rule: finite_ne_induct) (auto intro: generated_ring_Int)
  1026 
  1027 lemma (in semiring_of_sets) generated_ring_INTER:
  1028   "finite I \<Longrightarrow> I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> A i \<in> generated_ring) \<Longrightarrow> INTER I A \<in> generated_ring"
  1029   unfolding INF_def by (intro generated_ring_Inter) auto
  1030 
  1031 lemma (in semiring_of_sets) generating_ring:
  1032   "ring_of_sets \<Omega> generated_ring"
  1033 proof (rule ring_of_setsI)
  1034   let ?R = generated_ring
  1035   show "?R \<subseteq> Pow \<Omega>"
  1036     using sets_into_space by (auto simp: generated_ring_def generated_ring_empty)
  1037   show "{} \<in> ?R" by (rule generated_ring_empty)
  1038 
  1039   { fix a assume a: "a \<in> ?R" then guess Ca .. note Ca = this
  1040     fix b assume b: "b \<in> ?R" then guess Cb .. note Cb = this
  1041   
  1042     show "a - b \<in> ?R"
  1043     proof cases
  1044       assume "Cb = {}" with Cb `a \<in> ?R` show ?thesis
  1045         by simp
  1046     next
  1047       assume "Cb \<noteq> {}"
  1048       with Ca Cb have "a - b = (\<Union>a'\<in>Ca. \<Inter>b'\<in>Cb. a' - b')" by auto
  1049       also have "\<dots> \<in> ?R"
  1050       proof (intro generated_ring_INTER generated_ring_disjoint_UNION)
  1051         fix a b assume "a \<in> Ca" "b \<in> Cb"
  1052         with Ca Cb Diff_cover[of a b] show "a - b \<in> ?R"
  1053           by (auto simp add: generated_ring_def)
  1054       next
  1055         show "disjoint ((\<lambda>a'. \<Inter>b'\<in>Cb. a' - b')`Ca)"
  1056           using Ca by (auto simp add: disjoint_def `Cb \<noteq> {}`)
  1057       next
  1058         show "finite Ca" "finite Cb" "Cb \<noteq> {}" by fact+
  1059       qed
  1060       finally show "a - b \<in> ?R" .
  1061     qed }
  1062   note Diff = this
  1063 
  1064   fix a b assume sets: "a \<in> ?R" "b \<in> ?R"
  1065   have "a \<union> b = (a - b) \<union> (a \<inter> b) \<union> (b - a)" by auto
  1066   also have "\<dots> \<in> ?R"
  1067     by (intro sets generated_ring_disjoint_Un generated_ring_Int Diff) auto
  1068   finally show "a \<union> b \<in> ?R" .
  1069 qed
  1070 
  1071 lemma (in semiring_of_sets) sigma_sets_generated_ring_eq: "sigma_sets \<Omega> generated_ring = sigma_sets \<Omega> M"
  1072 proof
  1073   interpret M: sigma_algebra \<Omega> "sigma_sets \<Omega> M"
  1074     using space_closed by (rule sigma_algebra_sigma_sets)
  1075   show "sigma_sets \<Omega> generated_ring \<subseteq> sigma_sets \<Omega> M"
  1076     by (blast intro!: sigma_sets_mono elim: generated_ringE)
  1077 qed (auto intro!: generated_ringI_Basic sigma_sets_mono)
  1078 
  1079 subsubsection {* A Two-Element Series *}
  1080 
  1081 definition binaryset :: "'a set \<Rightarrow> 'a set \<Rightarrow> nat \<Rightarrow> 'a set "
  1082   where "binaryset A B = (\<lambda>x. {})(0 := A, Suc 0 := B)"
  1083 
  1084 lemma range_binaryset_eq: "range(binaryset A B) = {A,B,{}}"
  1085   apply (simp add: binaryset_def)
  1086   apply (rule set_eqI)
  1087   apply (auto simp add: image_iff)
  1088   done
  1089 
  1090 lemma UN_binaryset_eq: "(\<Union>i. binaryset A B i) = A \<union> B"
  1091   by (simp add: SUP_def range_binaryset_eq)
  1092 
  1093 subsubsection {* Closed CDI *}
  1094 
  1095 definition closed_cdi where
  1096   "closed_cdi \<Omega> M \<longleftrightarrow>
  1097    M \<subseteq> Pow \<Omega> &
  1098    (\<forall>s \<in> M. \<Omega> - s \<in> M) &
  1099    (\<forall>A. (range A \<subseteq> M) & (A 0 = {}) & (\<forall>n. A n \<subseteq> A (Suc n)) \<longrightarrow>
  1100         (\<Union>i. A i) \<in> M) &
  1101    (\<forall>A. (range A \<subseteq> M) & disjoint_family A \<longrightarrow> (\<Union>i::nat. A i) \<in> M)"
  1102 
  1103 inductive_set
  1104   smallest_ccdi_sets :: "'a set \<Rightarrow> 'a set set \<Rightarrow> 'a set set"
  1105   for \<Omega> M
  1106   where
  1107     Basic [intro]:
  1108       "a \<in> M \<Longrightarrow> a \<in> smallest_ccdi_sets \<Omega> M"
  1109   | Compl [intro]:
  1110       "a \<in> smallest_ccdi_sets \<Omega> M \<Longrightarrow> \<Omega> - a \<in> smallest_ccdi_sets \<Omega> M"
  1111   | Inc:
  1112       "range A \<in> Pow(smallest_ccdi_sets \<Omega> M) \<Longrightarrow> A 0 = {} \<Longrightarrow> (\<And>n. A n \<subseteq> A (Suc n))
  1113        \<Longrightarrow> (\<Union>i. A i) \<in> smallest_ccdi_sets \<Omega> M"
  1114   | Disj:
  1115       "range A \<in> Pow(smallest_ccdi_sets \<Omega> M) \<Longrightarrow> disjoint_family A
  1116        \<Longrightarrow> (\<Union>i::nat. A i) \<in> smallest_ccdi_sets \<Omega> M"
  1117 
  1118 lemma (in subset_class) smallest_closed_cdi1: "M \<subseteq> smallest_ccdi_sets \<Omega> M"
  1119   by auto
  1120 
  1121 lemma (in subset_class) smallest_ccdi_sets: "smallest_ccdi_sets \<Omega> M \<subseteq> Pow \<Omega>"
  1122   apply (rule subsetI)
  1123   apply (erule smallest_ccdi_sets.induct)
  1124   apply (auto intro: range_subsetD dest: sets_into_space)
  1125   done
  1126 
  1127 lemma (in subset_class) smallest_closed_cdi2: "closed_cdi \<Omega> (smallest_ccdi_sets \<Omega> M)"
  1128   apply (auto simp add: closed_cdi_def smallest_ccdi_sets)
  1129   apply (blast intro: smallest_ccdi_sets.Inc smallest_ccdi_sets.Disj) +
  1130   done
  1131 
  1132 lemma closed_cdi_subset: "closed_cdi \<Omega> M \<Longrightarrow> M \<subseteq> Pow \<Omega>"
  1133   by (simp add: closed_cdi_def)
  1134 
  1135 lemma closed_cdi_Compl: "closed_cdi \<Omega> M \<Longrightarrow> s \<in> M \<Longrightarrow> \<Omega> - s \<in> M"
  1136   by (simp add: closed_cdi_def)
  1137 
  1138 lemma closed_cdi_Inc:
  1139   "closed_cdi \<Omega> M \<Longrightarrow> range A \<subseteq> M \<Longrightarrow> A 0 = {} \<Longrightarrow> (!!n. A n \<subseteq> A (Suc n)) \<Longrightarrow> (\<Union>i. A i) \<in> M"
  1140   by (simp add: closed_cdi_def)
  1141 
  1142 lemma closed_cdi_Disj:
  1143   "closed_cdi \<Omega> M \<Longrightarrow> range A \<subseteq> M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Union>i::nat. A i) \<in> M"
  1144   by (simp add: closed_cdi_def)
  1145 
  1146 lemma closed_cdi_Un:
  1147   assumes cdi: "closed_cdi \<Omega> M" and empty: "{} \<in> M"
  1148       and A: "A \<in> M" and B: "B \<in> M"
  1149       and disj: "A \<inter> B = {}"
  1150     shows "A \<union> B \<in> M"
  1151 proof -
  1152   have ra: "range (binaryset A B) \<subseteq> M"
  1153    by (simp add: range_binaryset_eq empty A B)
  1154  have di:  "disjoint_family (binaryset A B)" using disj
  1155    by (simp add: disjoint_family_on_def binaryset_def Int_commute)
  1156  from closed_cdi_Disj [OF cdi ra di]
  1157  show ?thesis
  1158    by (simp add: UN_binaryset_eq)
  1159 qed
  1160 
  1161 lemma (in algebra) smallest_ccdi_sets_Un:
  1162   assumes A: "A \<in> smallest_ccdi_sets \<Omega> M" and B: "B \<in> smallest_ccdi_sets \<Omega> M"
  1163       and disj: "A \<inter> B = {}"
  1164     shows "A \<union> B \<in> smallest_ccdi_sets \<Omega> M"
  1165 proof -
  1166   have ra: "range (binaryset A B) \<in> Pow (smallest_ccdi_sets \<Omega> M)"
  1167     by (simp add: range_binaryset_eq  A B smallest_ccdi_sets.Basic)
  1168   have di:  "disjoint_family (binaryset A B)" using disj
  1169     by (simp add: disjoint_family_on_def binaryset_def Int_commute)
  1170   from Disj [OF ra di]
  1171   show ?thesis
  1172     by (simp add: UN_binaryset_eq)
  1173 qed
  1174 
  1175 lemma (in algebra) smallest_ccdi_sets_Int1:
  1176   assumes a: "a \<in> M"
  1177   shows "b \<in> smallest_ccdi_sets \<Omega> M \<Longrightarrow> a \<inter> b \<in> smallest_ccdi_sets \<Omega> M"
  1178 proof (induct rule: smallest_ccdi_sets.induct)
  1179   case (Basic x)
  1180   thus ?case
  1181     by (metis a Int smallest_ccdi_sets.Basic)
  1182 next
  1183   case (Compl x)
  1184   have "a \<inter> (\<Omega> - x) = \<Omega> - ((\<Omega> - a) \<union> (a \<inter> x))"
  1185     by blast
  1186   also have "... \<in> smallest_ccdi_sets \<Omega> M"
  1187     by (metis smallest_ccdi_sets.Compl a Compl(2) Diff_Int2 Diff_Int_distrib2
  1188            Diff_disjoint Int_Diff Int_empty_right smallest_ccdi_sets_Un
  1189            smallest_ccdi_sets.Basic smallest_ccdi_sets.Compl)
  1190   finally show ?case .
  1191 next
  1192   case (Inc A)
  1193   have 1: "(\<Union>i. (\<lambda>i. a \<inter> A i) i) = a \<inter> (\<Union>i. A i)"
  1194     by blast
  1195   have "range (\<lambda>i. a \<inter> A i) \<in> Pow(smallest_ccdi_sets \<Omega> M)" using Inc
  1196     by blast
  1197   moreover have "(\<lambda>i. a \<inter> A i) 0 = {}"
  1198     by (simp add: Inc)
  1199   moreover have "!!n. (\<lambda>i. a \<inter> A i) n \<subseteq> (\<lambda>i. a \<inter> A i) (Suc n)" using Inc
  1200     by blast
  1201   ultimately have 2: "(\<Union>i. (\<lambda>i. a \<inter> A i) i) \<in> smallest_ccdi_sets \<Omega> M"
  1202     by (rule smallest_ccdi_sets.Inc)
  1203   show ?case
  1204     by (metis 1 2)
  1205 next
  1206   case (Disj A)
  1207   have 1: "(\<Union>i. (\<lambda>i. a \<inter> A i) i) = a \<inter> (\<Union>i. A i)"
  1208     by blast
  1209   have "range (\<lambda>i. a \<inter> A i) \<in> Pow(smallest_ccdi_sets \<Omega> M)" using Disj
  1210     by blast
  1211   moreover have "disjoint_family (\<lambda>i. a \<inter> A i)" using Disj
  1212     by (auto simp add: disjoint_family_on_def)
  1213   ultimately have 2: "(\<Union>i. (\<lambda>i. a \<inter> A i) i) \<in> smallest_ccdi_sets \<Omega> M"
  1214     by (rule smallest_ccdi_sets.Disj)
  1215   show ?case
  1216     by (metis 1 2)
  1217 qed
  1218 
  1219 
  1220 lemma (in algebra) smallest_ccdi_sets_Int:
  1221   assumes b: "b \<in> smallest_ccdi_sets \<Omega> M"
  1222   shows "a \<in> smallest_ccdi_sets \<Omega> M \<Longrightarrow> a \<inter> b \<in> smallest_ccdi_sets \<Omega> M"
  1223 proof (induct rule: smallest_ccdi_sets.induct)
  1224   case (Basic x)
  1225   thus ?case
  1226     by (metis b smallest_ccdi_sets_Int1)
  1227 next
  1228   case (Compl x)
  1229   have "(\<Omega> - x) \<inter> b = \<Omega> - (x \<inter> b \<union> (\<Omega> - b))"
  1230     by blast
  1231   also have "... \<in> smallest_ccdi_sets \<Omega> M"
  1232     by (metis Compl(2) Diff_disjoint Int_Diff Int_commute Int_empty_right b
  1233            smallest_ccdi_sets.Compl smallest_ccdi_sets_Un)
  1234   finally show ?case .
  1235 next
  1236   case (Inc A)
  1237   have 1: "(\<Union>i. (\<lambda>i. A i \<inter> b) i) = (\<Union>i. A i) \<inter> b"
  1238     by blast
  1239   have "range (\<lambda>i. A i \<inter> b) \<in> Pow(smallest_ccdi_sets \<Omega> M)" using Inc
  1240     by blast
  1241   moreover have "(\<lambda>i. A i \<inter> b) 0 = {}"
  1242     by (simp add: Inc)
  1243   moreover have "!!n. (\<lambda>i. A i \<inter> b) n \<subseteq> (\<lambda>i. A i \<inter> b) (Suc n)" using Inc
  1244     by blast
  1245   ultimately have 2: "(\<Union>i. (\<lambda>i. A i \<inter> b) i) \<in> smallest_ccdi_sets \<Omega> M"
  1246     by (rule smallest_ccdi_sets.Inc)
  1247   show ?case
  1248     by (metis 1 2)
  1249 next
  1250   case (Disj A)
  1251   have 1: "(\<Union>i. (\<lambda>i. A i \<inter> b) i) = (\<Union>i. A i) \<inter> b"
  1252     by blast
  1253   have "range (\<lambda>i. A i \<inter> b) \<in> Pow(smallest_ccdi_sets \<Omega> M)" using Disj
  1254     by blast
  1255   moreover have "disjoint_family (\<lambda>i. A i \<inter> b)" using Disj
  1256     by (auto simp add: disjoint_family_on_def)
  1257   ultimately have 2: "(\<Union>i. (\<lambda>i. A i \<inter> b) i) \<in> smallest_ccdi_sets \<Omega> M"
  1258     by (rule smallest_ccdi_sets.Disj)
  1259   show ?case
  1260     by (metis 1 2)
  1261 qed
  1262 
  1263 lemma (in algebra) sigma_property_disjoint_lemma:
  1264   assumes sbC: "M \<subseteq> C"
  1265       and ccdi: "closed_cdi \<Omega> C"
  1266   shows "sigma_sets \<Omega> M \<subseteq> C"
  1267 proof -
  1268   have "smallest_ccdi_sets \<Omega> M \<in> {B . M \<subseteq> B \<and> sigma_algebra \<Omega> B}"
  1269     apply (auto simp add: sigma_algebra_disjoint_iff algebra_iff_Int
  1270             smallest_ccdi_sets_Int)
  1271     apply (metis Union_Pow_eq Union_upper subsetD smallest_ccdi_sets)
  1272     apply (blast intro: smallest_ccdi_sets.Disj)
  1273     done
  1274   hence "sigma_sets (\<Omega>) (M) \<subseteq> smallest_ccdi_sets \<Omega> M"
  1275     by clarsimp
  1276        (drule sigma_algebra.sigma_sets_subset [where a="M"], auto)
  1277   also have "...  \<subseteq> C"
  1278     proof
  1279       fix x
  1280       assume x: "x \<in> smallest_ccdi_sets \<Omega> M"
  1281       thus "x \<in> C"
  1282         proof (induct rule: smallest_ccdi_sets.induct)
  1283           case (Basic x)
  1284           thus ?case
  1285             by (metis Basic subsetD sbC)
  1286         next
  1287           case (Compl x)
  1288           thus ?case
  1289             by (blast intro: closed_cdi_Compl [OF ccdi, simplified])
  1290         next
  1291           case (Inc A)
  1292           thus ?case
  1293                by (auto intro: closed_cdi_Inc [OF ccdi, simplified])
  1294         next
  1295           case (Disj A)
  1296           thus ?case
  1297                by (auto intro: closed_cdi_Disj [OF ccdi, simplified])
  1298         qed
  1299     qed
  1300   finally show ?thesis .
  1301 qed
  1302 
  1303 lemma (in algebra) sigma_property_disjoint:
  1304   assumes sbC: "M \<subseteq> C"
  1305       and compl: "!!s. s \<in> C \<inter> sigma_sets (\<Omega>) (M) \<Longrightarrow> \<Omega> - s \<in> C"
  1306       and inc: "!!A. range A \<subseteq> C \<inter> sigma_sets (\<Omega>) (M)
  1307                      \<Longrightarrow> A 0 = {} \<Longrightarrow> (!!n. A n \<subseteq> A (Suc n))
  1308                      \<Longrightarrow> (\<Union>i. A i) \<in> C"
  1309       and disj: "!!A. range A \<subseteq> C \<inter> sigma_sets (\<Omega>) (M)
  1310                       \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Union>i::nat. A i) \<in> C"
  1311   shows "sigma_sets (\<Omega>) (M) \<subseteq> C"
  1312 proof -
  1313   have "sigma_sets (\<Omega>) (M) \<subseteq> C \<inter> sigma_sets (\<Omega>) (M)"
  1314     proof (rule sigma_property_disjoint_lemma)
  1315       show "M \<subseteq> C \<inter> sigma_sets (\<Omega>) (M)"
  1316         by (metis Int_greatest Set.subsetI sbC sigma_sets.Basic)
  1317     next
  1318       show "closed_cdi \<Omega> (C \<inter> sigma_sets (\<Omega>) (M))"
  1319         by (simp add: closed_cdi_def compl inc disj)
  1320            (metis PowI Set.subsetI le_infI2 sigma_sets_into_sp space_closed
  1321              IntE sigma_sets.Compl range_subsetD sigma_sets.Union)
  1322     qed
  1323   thus ?thesis
  1324     by blast
  1325 qed
  1326 
  1327 subsubsection {* Dynkin systems *}
  1328 
  1329 locale dynkin_system = subset_class +
  1330   assumes space: "\<Omega> \<in> M"
  1331     and   compl[intro!]: "\<And>A. A \<in> M \<Longrightarrow> \<Omega> - A \<in> M"
  1332     and   UN[intro!]: "\<And>A. disjoint_family A \<Longrightarrow> range A \<subseteq> M
  1333                            \<Longrightarrow> (\<Union>i::nat. A i) \<in> M"
  1334 
  1335 lemma (in dynkin_system) empty[intro, simp]: "{} \<in> M"
  1336   using space compl[of "\<Omega>"] by simp
  1337 
  1338 lemma (in dynkin_system) diff:
  1339   assumes sets: "D \<in> M" "E \<in> M" and "D \<subseteq> E"
  1340   shows "E - D \<in> M"
  1341 proof -
  1342   let ?f = "\<lambda>x. if x = 0 then D else if x = Suc 0 then \<Omega> - E else {}"
  1343   have "range ?f = {D, \<Omega> - E, {}}"
  1344     by (auto simp: image_iff)
  1345   moreover have "D \<union> (\<Omega> - E) = (\<Union>i. ?f i)"
  1346     by (auto simp: image_iff split: split_if_asm)
  1347   moreover
  1348   have "disjoint_family ?f" unfolding disjoint_family_on_def
  1349     using `D \<in> M`[THEN sets_into_space] `D \<subseteq> E` by auto
  1350   ultimately have "\<Omega> - (D \<union> (\<Omega> - E)) \<in> M"
  1351     using sets by auto
  1352   also have "\<Omega> - (D \<union> (\<Omega> - E)) = E - D"
  1353     using assms sets_into_space by auto
  1354   finally show ?thesis .
  1355 qed
  1356 
  1357 lemma dynkin_systemI:
  1358   assumes "\<And> A. A \<in> M \<Longrightarrow> A \<subseteq> \<Omega>" "\<Omega> \<in> M"
  1359   assumes "\<And> A. A \<in> M \<Longrightarrow> \<Omega> - A \<in> M"
  1360   assumes "\<And> A. disjoint_family A \<Longrightarrow> range A \<subseteq> M
  1361           \<Longrightarrow> (\<Union>i::nat. A i) \<in> M"
  1362   shows "dynkin_system \<Omega> M"
  1363   using assms by (auto simp: dynkin_system_def dynkin_system_axioms_def subset_class_def)
  1364 
  1365 lemma dynkin_systemI':
  1366   assumes 1: "\<And> A. A \<in> M \<Longrightarrow> A \<subseteq> \<Omega>"
  1367   assumes empty: "{} \<in> M"
  1368   assumes Diff: "\<And> A. A \<in> M \<Longrightarrow> \<Omega> - A \<in> M"
  1369   assumes 2: "\<And> A. disjoint_family A \<Longrightarrow> range A \<subseteq> M
  1370           \<Longrightarrow> (\<Union>i::nat. A i) \<in> M"
  1371   shows "dynkin_system \<Omega> M"
  1372 proof -
  1373   from Diff[OF empty] have "\<Omega> \<in> M" by auto
  1374   from 1 this Diff 2 show ?thesis
  1375     by (intro dynkin_systemI) auto
  1376 qed
  1377 
  1378 lemma dynkin_system_trivial:
  1379   shows "dynkin_system A (Pow A)"
  1380   by (rule dynkin_systemI) auto
  1381 
  1382 lemma sigma_algebra_imp_dynkin_system:
  1383   assumes "sigma_algebra \<Omega> M" shows "dynkin_system \<Omega> M"
  1384 proof -
  1385   interpret sigma_algebra \<Omega> M by fact
  1386   show ?thesis using sets_into_space by (fastforce intro!: dynkin_systemI)
  1387 qed
  1388 
  1389 subsubsection "Intersection sets systems"
  1390 
  1391 definition "Int_stable M \<longleftrightarrow> (\<forall> a \<in> M. \<forall> b \<in> M. a \<inter> b \<in> M)"
  1392 
  1393 lemma (in algebra) Int_stable: "Int_stable M"
  1394   unfolding Int_stable_def by auto
  1395 
  1396 lemma Int_stableI:
  1397   "(\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<inter> b \<in> A) \<Longrightarrow> Int_stable A"
  1398   unfolding Int_stable_def by auto
  1399 
  1400 lemma Int_stableD:
  1401   "Int_stable M \<Longrightarrow> a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<inter> b \<in> M"
  1402   unfolding Int_stable_def by auto
  1403 
  1404 lemma (in dynkin_system) sigma_algebra_eq_Int_stable:
  1405   "sigma_algebra \<Omega> M \<longleftrightarrow> Int_stable M"
  1406 proof
  1407   assume "sigma_algebra \<Omega> M" then show "Int_stable M"
  1408     unfolding sigma_algebra_def using algebra.Int_stable by auto
  1409 next
  1410   assume "Int_stable M"
  1411   show "sigma_algebra \<Omega> M"
  1412     unfolding sigma_algebra_disjoint_iff algebra_iff_Un
  1413   proof (intro conjI ballI allI impI)
  1414     show "M \<subseteq> Pow (\<Omega>)" using sets_into_space by auto
  1415   next
  1416     fix A B assume "A \<in> M" "B \<in> M"
  1417     then have "A \<union> B = \<Omega> - ((\<Omega> - A) \<inter> (\<Omega> - B))"
  1418               "\<Omega> - A \<in> M" "\<Omega> - B \<in> M"
  1419       using sets_into_space by auto
  1420     then show "A \<union> B \<in> M"
  1421       using `Int_stable M` unfolding Int_stable_def by auto
  1422   qed auto
  1423 qed
  1424 
  1425 subsubsection "Smallest Dynkin systems"
  1426 
  1427 definition dynkin where
  1428   "dynkin \<Omega> M =  (\<Inter>{D. dynkin_system \<Omega> D \<and> M \<subseteq> D})"
  1429 
  1430 lemma dynkin_system_dynkin:
  1431   assumes "M \<subseteq> Pow (\<Omega>)"
  1432   shows "dynkin_system \<Omega> (dynkin \<Omega> M)"
  1433 proof (rule dynkin_systemI)
  1434   fix A assume "A \<in> dynkin \<Omega> M"
  1435   moreover
  1436   { fix D assume "A \<in> D" and d: "dynkin_system \<Omega> D"
  1437     then have "A \<subseteq> \<Omega>" by (auto simp: dynkin_system_def subset_class_def) }
  1438   moreover have "{D. dynkin_system \<Omega> D \<and> M \<subseteq> D} \<noteq> {}"
  1439     using assms dynkin_system_trivial by fastforce
  1440   ultimately show "A \<subseteq> \<Omega>"
  1441     unfolding dynkin_def using assms
  1442     by auto
  1443 next
  1444   show "\<Omega> \<in> dynkin \<Omega> M"
  1445     unfolding dynkin_def using dynkin_system.space by fastforce
  1446 next
  1447   fix A assume "A \<in> dynkin \<Omega> M"
  1448   then show "\<Omega> - A \<in> dynkin \<Omega> M"
  1449     unfolding dynkin_def using dynkin_system.compl by force
  1450 next
  1451   fix A :: "nat \<Rightarrow> 'a set"
  1452   assume A: "disjoint_family A" "range A \<subseteq> dynkin \<Omega> M"
  1453   show "(\<Union>i. A i) \<in> dynkin \<Omega> M" unfolding dynkin_def
  1454   proof (simp, safe)
  1455     fix D assume "dynkin_system \<Omega> D" "M \<subseteq> D"
  1456     with A have "(\<Union>i. A i) \<in> D"
  1457       by (intro dynkin_system.UN) (auto simp: dynkin_def)
  1458     then show "(\<Union>i. A i) \<in> D" by auto
  1459   qed
  1460 qed
  1461 
  1462 lemma dynkin_Basic[intro]: "A \<in> M \<Longrightarrow> A \<in> dynkin \<Omega> M"
  1463   unfolding dynkin_def by auto
  1464 
  1465 lemma (in dynkin_system) restricted_dynkin_system:
  1466   assumes "D \<in> M"
  1467   shows "dynkin_system \<Omega> {Q. Q \<subseteq> \<Omega> \<and> Q \<inter> D \<in> M}"
  1468 proof (rule dynkin_systemI, simp_all)
  1469   have "\<Omega> \<inter> D = D"
  1470     using `D \<in> M` sets_into_space by auto
  1471   then show "\<Omega> \<inter> D \<in> M"
  1472     using `D \<in> M` by auto
  1473 next
  1474   fix A assume "A \<subseteq> \<Omega> \<and> A \<inter> D \<in> M"
  1475   moreover have "(\<Omega> - A) \<inter> D = (\<Omega> - (A \<inter> D)) - (\<Omega> - D)"
  1476     by auto
  1477   ultimately show "\<Omega> - A \<subseteq> \<Omega> \<and> (\<Omega> - A) \<inter> D \<in> M"
  1478     using  `D \<in> M` by (auto intro: diff)
  1479 next
  1480   fix A :: "nat \<Rightarrow> 'a set"
  1481   assume "disjoint_family A" "range A \<subseteq> {Q. Q \<subseteq> \<Omega> \<and> Q \<inter> D \<in> M}"
  1482   then have "\<And>i. A i \<subseteq> \<Omega>" "disjoint_family (\<lambda>i. A i \<inter> D)"
  1483     "range (\<lambda>i. A i \<inter> D) \<subseteq> M" "(\<Union>x. A x) \<inter> D = (\<Union>x. A x \<inter> D)"
  1484     by ((fastforce simp: disjoint_family_on_def)+)
  1485   then show "(\<Union>x. A x) \<subseteq> \<Omega> \<and> (\<Union>x. A x) \<inter> D \<in> M"
  1486     by (auto simp del: UN_simps)
  1487 qed
  1488 
  1489 lemma (in dynkin_system) dynkin_subset:
  1490   assumes "N \<subseteq> M"
  1491   shows "dynkin \<Omega> N \<subseteq> M"
  1492 proof -
  1493   have "dynkin_system \<Omega> M" by default
  1494   then have "dynkin_system \<Omega> M"
  1495     using assms unfolding dynkin_system_def dynkin_system_axioms_def subset_class_def by simp
  1496   with `N \<subseteq> M` show ?thesis by (auto simp add: dynkin_def)
  1497 qed
  1498 
  1499 lemma sigma_eq_dynkin:
  1500   assumes sets: "M \<subseteq> Pow \<Omega>"
  1501   assumes "Int_stable M"
  1502   shows "sigma_sets \<Omega> M = dynkin \<Omega> M"
  1503 proof -
  1504   have "dynkin \<Omega> M \<subseteq> sigma_sets (\<Omega>) (M)"
  1505     using sigma_algebra_imp_dynkin_system
  1506     unfolding dynkin_def sigma_sets_least_sigma_algebra[OF sets] by auto
  1507   moreover
  1508   interpret dynkin_system \<Omega> "dynkin \<Omega> M"
  1509     using dynkin_system_dynkin[OF sets] .
  1510   have "sigma_algebra \<Omega> (dynkin \<Omega> M)"
  1511     unfolding sigma_algebra_eq_Int_stable Int_stable_def
  1512   proof (intro ballI)
  1513     fix A B assume "A \<in> dynkin \<Omega> M" "B \<in> dynkin \<Omega> M"
  1514     let ?D = "\<lambda>E. {Q. Q \<subseteq> \<Omega> \<and> Q \<inter> E \<in> dynkin \<Omega> M}"
  1515     have "M \<subseteq> ?D B"
  1516     proof
  1517       fix E assume "E \<in> M"
  1518       then have "M \<subseteq> ?D E" "E \<in> dynkin \<Omega> M"
  1519         using sets_into_space `Int_stable M` by (auto simp: Int_stable_def)
  1520       then have "dynkin \<Omega> M \<subseteq> ?D E"
  1521         using restricted_dynkin_system `E \<in> dynkin \<Omega> M`
  1522         by (intro dynkin_system.dynkin_subset) simp_all
  1523       then have "B \<in> ?D E"
  1524         using `B \<in> dynkin \<Omega> M` by auto
  1525       then have "E \<inter> B \<in> dynkin \<Omega> M"
  1526         by (subst Int_commute) simp
  1527       then show "E \<in> ?D B"
  1528         using sets `E \<in> M` by auto
  1529     qed
  1530     then have "dynkin \<Omega> M \<subseteq> ?D B"
  1531       using restricted_dynkin_system `B \<in> dynkin \<Omega> M`
  1532       by (intro dynkin_system.dynkin_subset) simp_all
  1533     then show "A \<inter> B \<in> dynkin \<Omega> M"
  1534       using `A \<in> dynkin \<Omega> M` sets_into_space by auto
  1535   qed
  1536   from sigma_algebra.sigma_sets_subset[OF this, of "M"]
  1537   have "sigma_sets (\<Omega>) (M) \<subseteq> dynkin \<Omega> M" by auto
  1538   ultimately have "sigma_sets (\<Omega>) (M) = dynkin \<Omega> M" by auto
  1539   then show ?thesis
  1540     by (auto simp: dynkin_def)
  1541 qed
  1542 
  1543 lemma (in dynkin_system) dynkin_idem:
  1544   "dynkin \<Omega> M = M"
  1545 proof -
  1546   have "dynkin \<Omega> M = M"
  1547   proof
  1548     show "M \<subseteq> dynkin \<Omega> M"
  1549       using dynkin_Basic by auto
  1550     show "dynkin \<Omega> M \<subseteq> M"
  1551       by (intro dynkin_subset) auto
  1552   qed
  1553   then show ?thesis
  1554     by (auto simp: dynkin_def)
  1555 qed
  1556 
  1557 lemma (in dynkin_system) dynkin_lemma:
  1558   assumes "Int_stable E"
  1559   and E: "E \<subseteq> M" "M \<subseteq> sigma_sets \<Omega> E"
  1560   shows "sigma_sets \<Omega> E = M"
  1561 proof -
  1562   have "E \<subseteq> Pow \<Omega>"
  1563     using E sets_into_space by force
  1564   then have *: "sigma_sets \<Omega> E = dynkin \<Omega> E"
  1565     using `Int_stable E` by (rule sigma_eq_dynkin)
  1566   then have "dynkin \<Omega> E = M"
  1567     using assms dynkin_subset[OF E(1)] by simp
  1568   with * show ?thesis
  1569     using assms by (auto simp: dynkin_def)
  1570 qed
  1571 
  1572 subsubsection {* Induction rule for intersection-stable generators *}
  1573 
  1574 text {* The reason to introduce Dynkin-systems is the following induction rules for $\sigma$-algebras
  1575 generated by a generator closed under intersection. *}
  1576 
  1577 lemma sigma_sets_induct_disjoint[consumes 3, case_names basic empty compl union]:
  1578   assumes "Int_stable G"
  1579     and closed: "G \<subseteq> Pow \<Omega>"
  1580     and A: "A \<in> sigma_sets \<Omega> G"
  1581   assumes basic: "\<And>A. A \<in> G \<Longrightarrow> P A"
  1582     and empty: "P {}"
  1583     and compl: "\<And>A. A \<in> sigma_sets \<Omega> G \<Longrightarrow> P A \<Longrightarrow> P (\<Omega> - A)"
  1584     and union: "\<And>A. disjoint_family A \<Longrightarrow> range A \<subseteq> sigma_sets \<Omega> G \<Longrightarrow> (\<And>i. P (A i)) \<Longrightarrow> P (\<Union>i::nat. A i)"
  1585   shows "P A"
  1586 proof -
  1587   let ?D = "{ A \<in> sigma_sets \<Omega> G. P A }"
  1588   interpret sigma_algebra \<Omega> "sigma_sets \<Omega> G"
  1589     using closed by (rule sigma_algebra_sigma_sets)
  1590   from compl[OF _ empty] closed have space: "P \<Omega>" by simp
  1591   interpret dynkin_system \<Omega> ?D
  1592     by default (auto dest: sets_into_space intro!: space compl union)
  1593   have "sigma_sets \<Omega> G = ?D"
  1594     by (rule dynkin_lemma) (auto simp: basic `Int_stable G`)
  1595   with A show ?thesis by auto
  1596 qed
  1597 
  1598 subsection {* Measure type *}
  1599 
  1600 definition positive :: "'a set set \<Rightarrow> ('a set \<Rightarrow> ereal) \<Rightarrow> bool" where
  1601   "positive M \<mu> \<longleftrightarrow> \<mu> {} = 0 \<and> (\<forall>A\<in>M. 0 \<le> \<mu> A)"
  1602 
  1603 definition countably_additive :: "'a set set \<Rightarrow> ('a set \<Rightarrow> ereal) \<Rightarrow> bool" where
  1604   "countably_additive M f \<longleftrightarrow> (\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow>
  1605     (\<Sum>i. f (A i)) = f (\<Union>i. A i))"
  1606 
  1607 definition measure_space :: "'a set \<Rightarrow> 'a set set \<Rightarrow> ('a set \<Rightarrow> ereal) \<Rightarrow> bool" where
  1608   "measure_space \<Omega> A \<mu> \<longleftrightarrow> sigma_algebra \<Omega> A \<and> positive A \<mu> \<and> countably_additive A \<mu>"
  1609 
  1610 typedef 'a measure = "{(\<Omega>::'a set, A, \<mu>). (\<forall>a\<in>-A. \<mu> a = 0) \<and> measure_space \<Omega> A \<mu> }"
  1611 proof
  1612   have "sigma_algebra UNIV {{}, UNIV}"
  1613     by (auto simp: sigma_algebra_iff2)
  1614   then show "(UNIV, {{}, UNIV}, \<lambda>A. 0) \<in> {(\<Omega>, A, \<mu>). (\<forall>a\<in>-A. \<mu> a = 0) \<and> measure_space \<Omega> A \<mu>} "
  1615     by (auto simp: measure_space_def positive_def countably_additive_def)
  1616 qed
  1617 
  1618 definition space :: "'a measure \<Rightarrow> 'a set" where
  1619   "space M = fst (Rep_measure M)"
  1620 
  1621 definition sets :: "'a measure \<Rightarrow> 'a set set" where
  1622   "sets M = fst (snd (Rep_measure M))"
  1623 
  1624 definition emeasure :: "'a measure \<Rightarrow> 'a set \<Rightarrow> ereal" where
  1625   "emeasure M = snd (snd (Rep_measure M))"
  1626 
  1627 definition measure :: "'a measure \<Rightarrow> 'a set \<Rightarrow> real" where
  1628   "measure M A = real (emeasure M A)"
  1629 
  1630 declare [[coercion sets]]
  1631 
  1632 declare [[coercion measure]]
  1633 
  1634 declare [[coercion emeasure]]
  1635 
  1636 lemma measure_space: "measure_space (space M) (sets M) (emeasure M)"
  1637   by (cases M) (auto simp: space_def sets_def emeasure_def Abs_measure_inverse)
  1638 
  1639 interpretation sets!: sigma_algebra "space M" "sets M" for M :: "'a measure"
  1640   using measure_space[of M] by (auto simp: measure_space_def)
  1641 
  1642 definition measure_of :: "'a set \<Rightarrow> 'a set set \<Rightarrow> ('a set \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
  1643   "measure_of \<Omega> A \<mu> = Abs_measure (\<Omega>, if A \<subseteq> Pow \<Omega> then sigma_sets \<Omega> A else {{}, \<Omega>},
  1644     \<lambda>a. if a \<in> sigma_sets \<Omega> A \<and> measure_space \<Omega> (sigma_sets \<Omega> A) \<mu> then \<mu> a else 0)"
  1645 
  1646 abbreviation "sigma \<Omega> A \<equiv> measure_of \<Omega> A (\<lambda>x. 0)"
  1647 
  1648 lemma measure_space_0: "A \<subseteq> Pow \<Omega> \<Longrightarrow> measure_space \<Omega> (sigma_sets \<Omega> A) (\<lambda>x. 0)"
  1649   unfolding measure_space_def
  1650   by (auto intro!: sigma_algebra_sigma_sets simp: positive_def countably_additive_def)
  1651 
  1652 lemma sigma_algebra_trivial: "sigma_algebra \<Omega> {{}, \<Omega>}"
  1653 by unfold_locales(fastforce intro: exI[where x="{{}}"] exI[where x="{\<Omega>}"])+
  1654 
  1655 lemma measure_space_0': "measure_space \<Omega> {{}, \<Omega>} (\<lambda>x. 0)"
  1656 by(simp add: measure_space_def positive_def countably_additive_def sigma_algebra_trivial)
  1657 
  1658 lemma measure_space_closed:
  1659   assumes "measure_space \<Omega> M \<mu>"
  1660   shows "M \<subseteq> Pow \<Omega>"
  1661 proof -
  1662   interpret sigma_algebra \<Omega> M using assms by(simp add: measure_space_def)
  1663   show ?thesis by(rule space_closed)
  1664 qed
  1665 
  1666 lemma (in ring_of_sets) positive_cong_eq:
  1667   "(\<And>a. a \<in> M \<Longrightarrow> \<mu>' a = \<mu> a) \<Longrightarrow> positive M \<mu>' = positive M \<mu>"
  1668   by (auto simp add: positive_def)
  1669 
  1670 lemma (in sigma_algebra) countably_additive_eq:
  1671   "(\<And>a. a \<in> M \<Longrightarrow> \<mu>' a = \<mu> a) \<Longrightarrow> countably_additive M \<mu>' = countably_additive M \<mu>"
  1672   unfolding countably_additive_def
  1673   by (intro arg_cong[where f=All] ext) (auto simp add: countably_additive_def subset_eq)
  1674 
  1675 lemma measure_space_eq:
  1676   assumes closed: "A \<subseteq> Pow \<Omega>" and eq: "\<And>a. a \<in> sigma_sets \<Omega> A \<Longrightarrow> \<mu> a = \<mu>' a"
  1677   shows "measure_space \<Omega> (sigma_sets \<Omega> A) \<mu> = measure_space \<Omega> (sigma_sets \<Omega> A) \<mu>'"
  1678 proof -
  1679   interpret sigma_algebra \<Omega> "sigma_sets \<Omega> A" using closed by (rule sigma_algebra_sigma_sets)
  1680   from positive_cong_eq[OF eq, of "\<lambda>i. i"] countably_additive_eq[OF eq, of "\<lambda>i. i"] show ?thesis
  1681     by (auto simp: measure_space_def)
  1682 qed
  1683 
  1684 lemma measure_of_eq:
  1685   assumes closed: "A \<subseteq> Pow \<Omega>" and eq: "(\<And>a. a \<in> sigma_sets \<Omega> A \<Longrightarrow> \<mu> a = \<mu>' a)"
  1686   shows "measure_of \<Omega> A \<mu> = measure_of \<Omega> A \<mu>'"
  1687 proof -
  1688   have "measure_space \<Omega> (sigma_sets \<Omega> A) \<mu> = measure_space \<Omega> (sigma_sets \<Omega> A) \<mu>'"
  1689     using assms by (rule measure_space_eq)
  1690   with eq show ?thesis
  1691     by (auto simp add: measure_of_def intro!: arg_cong[where f=Abs_measure])
  1692 qed
  1693 
  1694 lemma
  1695   shows space_measure_of_conv: "space (measure_of \<Omega> A \<mu>) = \<Omega>" (is ?space)
  1696   and sets_measure_of_conv:
  1697   "sets (measure_of \<Omega> A \<mu>) = (if A \<subseteq> Pow \<Omega> then sigma_sets \<Omega> A else {{}, \<Omega>})" (is ?sets)
  1698   and emeasure_measure_of_conv: 
  1699   "emeasure (measure_of \<Omega> A \<mu>) = 
  1700   (\<lambda>B. if B \<in> sigma_sets \<Omega> A \<and> measure_space \<Omega> (sigma_sets \<Omega> A) \<mu> then \<mu> B else 0)" (is ?emeasure)
  1701 proof -
  1702   have "?space \<and> ?sets \<and> ?emeasure"
  1703   proof(cases "measure_space \<Omega> (sigma_sets \<Omega> A) \<mu>")
  1704     case True
  1705     from measure_space_closed[OF this] sigma_sets_superset_generator[of A \<Omega>]
  1706     have "A \<subseteq> Pow \<Omega>" by simp
  1707     hence "measure_space \<Omega> (sigma_sets \<Omega> A) \<mu> = measure_space \<Omega> (sigma_sets \<Omega> A)
  1708       (\<lambda>a. if a \<in> sigma_sets \<Omega> A then \<mu> a else 0)"
  1709       by(rule measure_space_eq) auto
  1710     with True `A \<subseteq> Pow \<Omega>` show ?thesis
  1711       by(simp add: measure_of_def space_def sets_def emeasure_def Abs_measure_inverse)
  1712   next
  1713     case False thus ?thesis
  1714       by(cases "A \<subseteq> Pow \<Omega>")(simp_all add: Abs_measure_inverse measure_of_def sets_def space_def emeasure_def measure_space_0 measure_space_0')
  1715   qed
  1716   thus ?space ?sets ?emeasure by simp_all
  1717 qed
  1718 
  1719 lemma [simp]:
  1720   assumes A: "A \<subseteq> Pow \<Omega>"
  1721   shows sets_measure_of: "sets (measure_of \<Omega> A \<mu>) = sigma_sets \<Omega> A"
  1722     and space_measure_of: "space (measure_of \<Omega> A \<mu>) = \<Omega>"
  1723 using assms
  1724 by(simp_all add: sets_measure_of_conv space_measure_of_conv)
  1725 
  1726 lemma (in sigma_algebra) sets_measure_of_eq[simp]: "sets (measure_of \<Omega> M \<mu>) = M"
  1727   using space_closed by (auto intro!: sigma_sets_eq)
  1728 
  1729 lemma (in sigma_algebra) space_measure_of_eq[simp]: "space (measure_of \<Omega> M \<mu>) = \<Omega>"
  1730   by (rule space_measure_of_conv)
  1731 
  1732 lemma measure_of_subset: "M \<subseteq> Pow \<Omega> \<Longrightarrow> M' \<subseteq> M \<Longrightarrow> sets (measure_of \<Omega> M' \<mu>) \<subseteq> sets (measure_of \<Omega> M \<mu>')"
  1733   by (auto intro!: sigma_sets_subseteq)
  1734 
  1735 lemma sigma_sets_mono'':
  1736   assumes "A \<in> sigma_sets C D"
  1737   assumes "B \<subseteq> D"
  1738   assumes "D \<subseteq> Pow C"
  1739   shows "sigma_sets A B \<subseteq> sigma_sets C D"
  1740 proof
  1741   fix x assume "x \<in> sigma_sets A B"
  1742   thus "x \<in> sigma_sets C D"
  1743   proof induct
  1744     case (Basic a) with assms have "a \<in> D" by auto
  1745     thus ?case ..
  1746   next
  1747     case Empty show ?case by (rule sigma_sets.Empty)
  1748   next
  1749     from assms have "A \<in> sets (sigma C D)" by (subst sets_measure_of[OF `D \<subseteq> Pow C`])
  1750     moreover case (Compl a) hence "a \<in> sets (sigma C D)" by (subst sets_measure_of[OF `D \<subseteq> Pow C`])
  1751     ultimately have "A - a \<in> sets (sigma C D)" ..
  1752     thus ?case by (subst (asm) sets_measure_of[OF `D \<subseteq> Pow C`])
  1753   next
  1754     case (Union a)
  1755     thus ?case by (intro sigma_sets.Union)
  1756   qed
  1757 qed
  1758 
  1759 lemma in_measure_of[intro, simp]: "M \<subseteq> Pow \<Omega> \<Longrightarrow> A \<in> M \<Longrightarrow> A \<in> sets (measure_of \<Omega> M \<mu>)"
  1760   by auto
  1761 
  1762 subsubsection {* Constructing simple @{typ "'a measure"} *}
  1763 
  1764 lemma emeasure_measure_of:
  1765   assumes M: "M = measure_of \<Omega> A \<mu>"
  1766   assumes ms: "A \<subseteq> Pow \<Omega>" "positive (sets M) \<mu>" "countably_additive (sets M) \<mu>"
  1767   assumes X: "X \<in> sets M"
  1768   shows "emeasure M X = \<mu> X"
  1769 proof -
  1770   interpret sigma_algebra \<Omega> "sigma_sets \<Omega> A" by (rule sigma_algebra_sigma_sets) fact
  1771   have "measure_space \<Omega> (sigma_sets \<Omega> A) \<mu>"
  1772     using ms M by (simp add: measure_space_def sigma_algebra_sigma_sets)
  1773   thus ?thesis using X ms
  1774     by(simp add: M emeasure_measure_of_conv sets_measure_of_conv)
  1775 qed
  1776 
  1777 lemma emeasure_measure_of_sigma:
  1778   assumes ms: "sigma_algebra \<Omega> M" "positive M \<mu>" "countably_additive M \<mu>"
  1779   assumes A: "A \<in> M"
  1780   shows "emeasure (measure_of \<Omega> M \<mu>) A = \<mu> A"
  1781 proof -
  1782   interpret sigma_algebra \<Omega> M by fact
  1783   have "measure_space \<Omega> (sigma_sets \<Omega> M) \<mu>"
  1784     using ms sigma_sets_eq by (simp add: measure_space_def)
  1785   thus ?thesis by(simp add: emeasure_measure_of_conv A)
  1786 qed
  1787 
  1788 lemma measure_cases[cases type: measure]:
  1789   obtains (measure) \<Omega> A \<mu> where "x = Abs_measure (\<Omega>, A, \<mu>)" "\<forall>a\<in>-A. \<mu> a = 0" "measure_space \<Omega> A \<mu>"
  1790   by atomize_elim (cases x, auto)
  1791 
  1792 lemma sets_eq_imp_space_eq:
  1793   "sets M = sets M' \<Longrightarrow> space M = space M'"
  1794   using sets.top[of M] sets.top[of M'] sets.space_closed[of M] sets.space_closed[of M']
  1795   by blast
  1796 
  1797 lemma emeasure_notin_sets: "A \<notin> sets M \<Longrightarrow> emeasure M A = 0"
  1798   by (cases M) (auto simp: sets_def emeasure_def Abs_measure_inverse measure_space_def)
  1799 
  1800 lemma emeasure_neq_0_sets: "emeasure M A \<noteq> 0 \<Longrightarrow> A \<in> sets M"
  1801   using emeasure_notin_sets[of A M] by blast
  1802 
  1803 lemma measure_notin_sets: "A \<notin> sets M \<Longrightarrow> measure M A = 0"
  1804   by (simp add: measure_def emeasure_notin_sets)
  1805 
  1806 lemma measure_eqI:
  1807   fixes M N :: "'a measure"
  1808   assumes "sets M = sets N" and eq: "\<And>A. A \<in> sets M \<Longrightarrow> emeasure M A = emeasure N A"
  1809   shows "M = N"
  1810 proof (cases M N rule: measure_cases[case_product measure_cases])
  1811   case (measure_measure \<Omega> A \<mu> \<Omega>' A' \<mu>')
  1812   interpret M: sigma_algebra \<Omega> A using measure_measure by (auto simp: measure_space_def)
  1813   interpret N: sigma_algebra \<Omega>' A' using measure_measure by (auto simp: measure_space_def)
  1814   have "A = sets M" "A' = sets N"
  1815     using measure_measure by (simp_all add: sets_def Abs_measure_inverse)
  1816   with `sets M = sets N` have AA': "A = A'" by simp
  1817   moreover from M.top N.top M.space_closed N.space_closed AA' have "\<Omega> = \<Omega>'" by auto
  1818   moreover { fix B have "\<mu> B = \<mu>' B"
  1819     proof cases
  1820       assume "B \<in> A"
  1821       with eq `A = sets M` have "emeasure M B = emeasure N B" by simp
  1822       with measure_measure show "\<mu> B = \<mu>' B"
  1823         by (simp add: emeasure_def Abs_measure_inverse)
  1824     next
  1825       assume "B \<notin> A"
  1826       with `A = sets M` `A' = sets N` `A = A'` have "B \<notin> sets M" "B \<notin> sets N"
  1827         by auto
  1828       then have "emeasure M B = 0" "emeasure N B = 0"
  1829         by (simp_all add: emeasure_notin_sets)
  1830       with measure_measure show "\<mu> B = \<mu>' B"
  1831         by (simp add: emeasure_def Abs_measure_inverse)
  1832     qed }
  1833   then have "\<mu> = \<mu>'" by auto
  1834   ultimately show "M = N"
  1835     by (simp add: measure_measure)
  1836 qed
  1837 
  1838 lemma emeasure_sigma: "A \<subseteq> Pow \<Omega> \<Longrightarrow> emeasure (sigma \<Omega> A) = (\<lambda>_. 0)"
  1839   using measure_space_0[of A \<Omega>]
  1840   by (simp add: measure_of_def emeasure_def Abs_measure_inverse)
  1841 
  1842 lemma sigma_eqI:
  1843   assumes [simp]: "M \<subseteq> Pow \<Omega>" "N \<subseteq> Pow \<Omega>" "sigma_sets \<Omega> M = sigma_sets \<Omega> N"
  1844   shows "sigma \<Omega> M = sigma \<Omega> N"
  1845   by (rule measure_eqI) (simp_all add: emeasure_sigma)
  1846 
  1847 subsubsection {* Measurable functions *}
  1848 
  1849 definition measurable :: "'a measure \<Rightarrow> 'b measure \<Rightarrow> ('a \<Rightarrow> 'b) set" where
  1850   "measurable A B = {f \<in> space A -> space B. \<forall>y \<in> sets B. f -` y \<inter> space A \<in> sets A}"
  1851 
  1852 lemma measurable_space:
  1853   "f \<in> measurable M A \<Longrightarrow> x \<in> space M \<Longrightarrow> f x \<in> space A"
  1854    unfolding measurable_def by auto
  1855 
  1856 lemma measurable_sets:
  1857   "f \<in> measurable M A \<Longrightarrow> S \<in> sets A \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
  1858    unfolding measurable_def by auto
  1859 
  1860 lemma measurable_sets_Collect:
  1861   assumes f: "f \<in> measurable M N" and P: "{x\<in>space N. P x} \<in> sets N" shows "{x\<in>space M. P (f x)} \<in> sets M"
  1862 proof -
  1863   have "f -` {x \<in> space N. P x} \<inter> space M = {x\<in>space M. P (f x)}"
  1864     using measurable_space[OF f] by auto
  1865   with measurable_sets[OF f P] show ?thesis
  1866     by simp
  1867 qed
  1868 
  1869 lemma measurable_sigma_sets:
  1870   assumes B: "sets N = sigma_sets \<Omega> A" "A \<subseteq> Pow \<Omega>"
  1871       and f: "f \<in> space M \<rightarrow> \<Omega>"
  1872       and ba: "\<And>y. y \<in> A \<Longrightarrow> (f -` y) \<inter> space M \<in> sets M"
  1873   shows "f \<in> measurable M N"
  1874 proof -
  1875   interpret A: sigma_algebra \<Omega> "sigma_sets \<Omega> A" using B(2) by (rule sigma_algebra_sigma_sets)
  1876   from B sets.top[of N] A.top sets.space_closed[of N] A.space_closed have \<Omega>: "\<Omega> = space N" by force
  1877   
  1878   { fix X assume "X \<in> sigma_sets \<Omega> A"
  1879     then have "f -` X \<inter> space M \<in> sets M \<and> X \<subseteq> \<Omega>"
  1880       proof induct
  1881         case (Basic a) then show ?case
  1882           by (auto simp add: ba) (metis B(2) subsetD PowD)
  1883       next
  1884         case (Compl a)
  1885         have [simp]: "f -` \<Omega> \<inter> space M = space M"
  1886           by (auto simp add: funcset_mem [OF f])
  1887         then show ?case
  1888           by (auto simp add: vimage_Diff Diff_Int_distrib2 sets.compl_sets Compl)
  1889       next
  1890         case (Union a)
  1891         then show ?case
  1892           by (simp add: vimage_UN, simp only: UN_extend_simps(4)) blast
  1893       qed auto }
  1894   with f show ?thesis
  1895     by (auto simp add: measurable_def B \<Omega>)
  1896 qed
  1897 
  1898 lemma measurable_measure_of:
  1899   assumes B: "N \<subseteq> Pow \<Omega>"
  1900       and f: "f \<in> space M \<rightarrow> \<Omega>"
  1901       and ba: "\<And>y. y \<in> N \<Longrightarrow> (f -` y) \<inter> space M \<in> sets M"
  1902   shows "f \<in> measurable M (measure_of \<Omega> N \<mu>)"
  1903 proof -
  1904   have "sets (measure_of \<Omega> N \<mu>) = sigma_sets \<Omega> N"
  1905     using B by (rule sets_measure_of)
  1906   from this assms show ?thesis by (rule measurable_sigma_sets)
  1907 qed
  1908 
  1909 lemma measurable_iff_measure_of:
  1910   assumes "N \<subseteq> Pow \<Omega>" "f \<in> space M \<rightarrow> \<Omega>"
  1911   shows "f \<in> measurable M (measure_of \<Omega> N \<mu>) \<longleftrightarrow> (\<forall>A\<in>N. f -` A \<inter> space M \<in> sets M)"
  1912   by (metis assms in_measure_of measurable_measure_of assms measurable_sets)
  1913 
  1914 lemma measurable_cong_sets:
  1915   assumes sets: "sets M = sets M'" "sets N = sets N'"
  1916   shows "measurable M N = measurable M' N'"
  1917   using sets[THEN sets_eq_imp_space_eq] sets by (simp add: measurable_def)
  1918 
  1919 lemma measurable_cong:
  1920   assumes "\<And> w. w \<in> space M \<Longrightarrow> f w = g w"
  1921   shows "f \<in> measurable M M' \<longleftrightarrow> g \<in> measurable M M'"
  1922   unfolding measurable_def using assms
  1923   by (simp cong: vimage_inter_cong Pi_cong)
  1924 
  1925 lemma measurable_cong_strong:
  1926   "M = N \<Longrightarrow> M' = N' \<Longrightarrow> (\<And>w. w \<in> space M \<Longrightarrow> f w = g w) \<Longrightarrow>
  1927     f \<in> measurable M M' \<longleftrightarrow> g \<in> measurable N N'"
  1928   by (metis measurable_cong)
  1929 
  1930 lemma measurable_eqI:
  1931      "\<lbrakk> space m1 = space m1' ; space m2 = space m2' ;
  1932         sets m1 = sets m1' ; sets m2 = sets m2' \<rbrakk>
  1933       \<Longrightarrow> measurable m1 m2 = measurable m1' m2'"
  1934   by (simp add: measurable_def sigma_algebra_iff2)
  1935 
  1936 lemma measurable_compose:
  1937   assumes f: "f \<in> measurable M N" and g: "g \<in> measurable N L"
  1938   shows "(\<lambda>x. g (f x)) \<in> measurable M L"
  1939 proof -
  1940   have "\<And>A. (\<lambda>x. g (f x)) -` A \<inter> space M = f -` (g -` A \<inter> space N) \<inter> space M"
  1941     using measurable_space[OF f] by auto
  1942   with measurable_space[OF f] measurable_space[OF g] show ?thesis
  1943     by (auto intro: measurable_sets[OF f] measurable_sets[OF g]
  1944              simp del: vimage_Int simp add: measurable_def)
  1945 qed
  1946 
  1947 lemma measurable_comp:
  1948   "f \<in> measurable M N \<Longrightarrow> g \<in> measurable N L \<Longrightarrow> g \<circ> f \<in> measurable M L"
  1949   using measurable_compose[of f M N g L] by (simp add: comp_def)
  1950 
  1951 lemma measurable_const:
  1952   "c \<in> space M' \<Longrightarrow> (\<lambda>x. c) \<in> measurable M M'"
  1953   by (auto simp add: measurable_def)
  1954 
  1955 lemma measurable_If:
  1956   assumes measure: "f \<in> measurable M M'" "g \<in> measurable M M'"
  1957   assumes P: "{x\<in>space M. P x} \<in> sets M"
  1958   shows "(\<lambda>x. if P x then f x else g x) \<in> measurable M M'"
  1959   unfolding measurable_def
  1960 proof safe
  1961   fix x assume "x \<in> space M"
  1962   thus "(if P x then f x else g x) \<in> space M'"
  1963     using measure unfolding measurable_def by auto
  1964 next
  1965   fix A assume "A \<in> sets M'"
  1966   hence *: "(\<lambda>x. if P x then f x else g x) -` A \<inter> space M =
  1967     ((f -` A \<inter> space M) \<inter> {x\<in>space M. P x}) \<union>
  1968     ((g -` A \<inter> space M) \<inter> (space M - {x\<in>space M. P x}))"
  1969     using measure unfolding measurable_def by (auto split: split_if_asm)
  1970   show "(\<lambda>x. if P x then f x else g x) -` A \<inter> space M \<in> sets M"
  1971     using `A \<in> sets M'` measure P unfolding * measurable_def
  1972     by (auto intro!: sets.Un)
  1973 qed
  1974 
  1975 lemma measurable_If_set:
  1976   assumes measure: "f \<in> measurable M M'" "g \<in> measurable M M'"
  1977   assumes P: "A \<inter> space M \<in> sets M"
  1978   shows "(\<lambda>x. if x \<in> A then f x else g x) \<in> measurable M M'"
  1979 proof (rule measurable_If[OF measure])
  1980   have "{x \<in> space M. x \<in> A} = A \<inter> space M" by auto
  1981   thus "{x \<in> space M. x \<in> A} \<in> sets M" using `A \<inter> space M \<in> sets M` by auto
  1982 qed
  1983 
  1984 lemma measurable_ident: "id \<in> measurable M M"
  1985   by (auto simp add: measurable_def)
  1986 
  1987 lemma measurable_ident_sets:
  1988   assumes eq: "sets M = sets M'" shows "(\<lambda>x. x) \<in> measurable M M'"
  1989   using measurable_ident[of M]
  1990   unfolding id_def measurable_def eq sets_eq_imp_space_eq[OF eq] .
  1991 
  1992 lemma sets_Least:
  1993   assumes meas: "\<And>i::nat. {x\<in>space M. P i x} \<in> M"
  1994   shows "(\<lambda>x. LEAST j. P j x) -` A \<inter> space M \<in> sets M"
  1995 proof -
  1996   { fix i have "(\<lambda>x. LEAST j. P j x) -` {i} \<inter> space M \<in> sets M"
  1997     proof cases
  1998       assume i: "(LEAST j. False) = i"
  1999       have "(\<lambda>x. LEAST j. P j x) -` {i} \<inter> space M =
  2000         {x\<in>space M. P i x} \<inter> (space M - (\<Union>j<i. {x\<in>space M. P j x})) \<union> (space M - (\<Union>i. {x\<in>space M. P i x}))"
  2001         by (simp add: set_eq_iff, safe)
  2002            (insert i, auto dest: Least_le intro: LeastI intro!: Least_equality)
  2003       with meas show ?thesis
  2004         by (auto intro!: sets.Int)
  2005     next
  2006       assume i: "(LEAST j. False) \<noteq> i"
  2007       then have "(\<lambda>x. LEAST j. P j x) -` {i} \<inter> space M =
  2008         {x\<in>space M. P i x} \<inter> (space M - (\<Union>j<i. {x\<in>space M. P j x}))"
  2009       proof (simp add: set_eq_iff, safe)
  2010         fix x assume neq: "(LEAST j. False) \<noteq> (LEAST j. P j x)"
  2011         have "\<exists>j. P j x"
  2012           by (rule ccontr) (insert neq, auto)
  2013         then show "P (LEAST j. P j x) x" by (rule LeastI_ex)
  2014       qed (auto dest: Least_le intro!: Least_equality)
  2015       with meas show ?thesis
  2016         by auto
  2017     qed }
  2018   then have "(\<Union>i\<in>A. (\<lambda>x. LEAST j. P j x) -` {i} \<inter> space M) \<in> sets M"
  2019     by (intro sets.countable_UN) auto
  2020   moreover have "(\<Union>i\<in>A. (\<lambda>x. LEAST j. P j x) -` {i} \<inter> space M) =
  2021     (\<lambda>x. LEAST j. P j x) -` A \<inter> space M" by auto
  2022   ultimately show ?thesis by auto
  2023 qed
  2024 
  2025 lemma measurable_strong:
  2026   fixes f :: "'a \<Rightarrow> 'b" and g :: "'b \<Rightarrow> 'c"
  2027   assumes f: "f \<in> measurable a b" and g: "g \<in> space b \<rightarrow> space c"
  2028       and t: "f ` (space a) \<subseteq> t"
  2029       and cb: "\<And>s. s \<in> sets c \<Longrightarrow> (g -` s) \<inter> t \<in> sets b"
  2030   shows "(g o f) \<in> measurable a c"
  2031 proof -
  2032   have fab: "f \<in> (space a -> space b)"
  2033    and ba: "\<And>y. y \<in> sets b \<Longrightarrow> (f -` y) \<inter> (space a) \<in> sets a" using f
  2034      by (auto simp add: measurable_def)
  2035   have eq: "\<And>y. (g \<circ> f) -` y \<inter> space a = f -` (g -` y \<inter> t) \<inter> space a" using t
  2036     by force
  2037   show ?thesis
  2038     apply (auto simp add: measurable_def vimage_comp)
  2039     apply (metis funcset_mem fab g)
  2040     apply (subst eq)
  2041     apply (metis ba cb)
  2042     done
  2043 qed
  2044 
  2045 lemma measurable_discrete_difference:
  2046   assumes f: "f \<in> measurable M N"
  2047   assumes X: "countable X"
  2048   assumes sets: "\<And>x. x \<in> X \<Longrightarrow> {x} \<in> sets M"
  2049   assumes space: "\<And>x. x \<in> X \<Longrightarrow> g x \<in> space N"
  2050   assumes eq: "\<And>x. x \<in> space M \<Longrightarrow> x \<notin> X \<Longrightarrow> f x = g x"
  2051   shows "g \<in> measurable M N"
  2052   unfolding measurable_def
  2053 proof safe
  2054   fix x assume "x \<in> space M" then show "g x \<in> space N"
  2055     using measurable_space[OF f, of x] eq[of x] space[of x] by (cases "x \<in> X") auto
  2056 next
  2057   fix S assume S: "S \<in> sets N"
  2058   have "g -` S \<inter> space M = (f -` S \<inter> space M) - (\<Union>x\<in>X. {x}) \<union> (\<Union>x\<in>{x\<in>X. g x \<in> S}. {x})"
  2059     using sets.sets_into_space[OF sets] eq by auto
  2060   also have "\<dots> \<in> sets M"
  2061     by (safe intro!: sets.Diff sets.Un measurable_sets[OF f] S sets.countable_UN' X countable_Collect sets)
  2062   finally show "g -` S \<inter> space M \<in> sets M" .
  2063 qed
  2064 
  2065 lemma measurable_mono1:
  2066   "M' \<subseteq> Pow \<Omega> \<Longrightarrow> M \<subseteq> M' \<Longrightarrow>
  2067     measurable (measure_of \<Omega> M \<mu>) N \<subseteq> measurable (measure_of \<Omega> M' \<mu>') N"
  2068   using measure_of_subset[of M' \<Omega> M] by (auto simp add: measurable_def)
  2069 
  2070 subsubsection {* Counting space *}
  2071 
  2072 definition count_space :: "'a set \<Rightarrow> 'a measure" where
  2073   "count_space \<Omega> = measure_of \<Omega> (Pow \<Omega>) (\<lambda>A. if finite A then ereal (card A) else \<infinity>)"
  2074 
  2075 lemma 
  2076   shows space_count_space[simp]: "space (count_space \<Omega>) = \<Omega>"
  2077     and sets_count_space[simp]: "sets (count_space \<Omega>) = Pow \<Omega>"
  2078   using sigma_sets_into_sp[of "Pow \<Omega>" \<Omega>]
  2079   by (auto simp: count_space_def)
  2080 
  2081 lemma measurable_count_space_eq1[simp]:
  2082   "f \<in> measurable (count_space A) M \<longleftrightarrow> f \<in> A \<rightarrow> space M"
  2083  unfolding measurable_def by simp
  2084 
  2085 lemma measurable_count_space_eq2:
  2086   assumes "finite A"
  2087   shows "f \<in> measurable M (count_space A) \<longleftrightarrow> (f \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. f -` {a} \<inter> space M \<in> sets M))"
  2088 proof -
  2089   { fix X assume "X \<subseteq> A" "f \<in> space M \<rightarrow> A"
  2090     with `finite A` have "f -` X \<inter> space M = (\<Union>a\<in>X. f -` {a} \<inter> space M)" "finite X"
  2091       by (auto dest: finite_subset)
  2092     moreover assume "\<forall>a\<in>A. f -` {a} \<inter> space M \<in> sets M"
  2093     ultimately have "f -` X \<inter> space M \<in> sets M"
  2094       using `X \<subseteq> A` by (auto intro!: sets.finite_UN simp del: UN_simps) }
  2095   then show ?thesis
  2096     unfolding measurable_def by auto
  2097 qed
  2098 
  2099 lemma measurable_count_space_eq2_countable:
  2100   fixes f :: "'a => 'c::countable"
  2101   shows "f \<in> measurable M (count_space A) \<longleftrightarrow> (f \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. f -` {a} \<inter> space M \<in> sets M))"
  2102 proof -
  2103   { fix X assume "X \<subseteq> A" "f \<in> space M \<rightarrow> A"
  2104     assume *: "\<And>a. a\<in>A \<Longrightarrow> f -` {a} \<inter> space M \<in> sets M"
  2105     have "f -` X \<inter> space M = (\<Union>a\<in>X. f -` {a} \<inter> space M)"
  2106       by auto
  2107     also have "\<dots> \<in> sets M"
  2108       using * `X \<subseteq> A` by (intro sets.countable_UN) auto
  2109     finally have "f -` X \<inter> space M \<in> sets M" . }
  2110   then show ?thesis
  2111     unfolding measurable_def by auto
  2112 qed
  2113 
  2114 lemma measurable_compose_countable:
  2115   assumes f: "\<And>i::'i::countable. (\<lambda>x. f i x) \<in> measurable M N" and g: "g \<in> measurable M (count_space UNIV)"
  2116   shows "(\<lambda>x. f (g x) x) \<in> measurable M N"
  2117   unfolding measurable_def
  2118 proof safe
  2119   fix x assume "x \<in> space M" then show "f (g x) x \<in> space N"
  2120     using f[THEN measurable_space] g[THEN measurable_space] by auto
  2121 next
  2122   fix A assume A: "A \<in> sets N"
  2123   have "(\<lambda>x. f (g x) x) -` A \<inter> space M = (\<Union>i. (g -` {i} \<inter> space M) \<inter> (f i -` A \<inter> space M))"
  2124     by auto
  2125   also have "\<dots> \<in> sets M" using f[THEN measurable_sets, OF A] g[THEN measurable_sets]
  2126     by (auto intro!: sets.countable_UN measurable_sets)
  2127   finally show "(\<lambda>x. f (g x) x) -` A \<inter> space M \<in> sets M" .
  2128 qed
  2129 
  2130 lemma measurable_count_space_const:
  2131   "(\<lambda>x. c) \<in> measurable M (count_space UNIV)"
  2132   by (simp add: measurable_const)
  2133 
  2134 lemma measurable_count_space:
  2135   "f \<in> measurable (count_space A) (count_space UNIV)"
  2136   by simp
  2137 
  2138 lemma measurable_compose_rev:
  2139   assumes f: "f \<in> measurable L N" and g: "g \<in> measurable M L"
  2140   shows "(\<lambda>x. f (g x)) \<in> measurable M N"
  2141   using measurable_compose[OF g f] .
  2142 
  2143 lemma measurable_count_space_eq_countable:
  2144   assumes "countable A"
  2145   shows "f \<in> measurable M (count_space A) \<longleftrightarrow> (f \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. f -` {a} \<inter> space M \<in> sets M))"
  2146 proof -
  2147   { fix X assume "X \<subseteq> A" "f \<in> space M \<rightarrow> A"
  2148     with `countable A` have "f -` X \<inter> space M = (\<Union>a\<in>X. f -` {a} \<inter> space M)" "countable X"
  2149       by (auto dest: countable_subset)
  2150     moreover assume "\<forall>a\<in>A. f -` {a} \<inter> space M \<in> sets M"
  2151     ultimately have "f -` X \<inter> space M \<in> sets M"
  2152       using `X \<subseteq> A` by (auto intro!: sets.countable_UN' simp del: UN_simps) }
  2153   then show ?thesis
  2154     unfolding measurable_def by auto
  2155 qed
  2156 
  2157 subsubsection {* Extend measure *}
  2158 
  2159 definition "extend_measure \<Omega> I G \<mu> =
  2160   (if (\<exists>\<mu>'. (\<forall>i\<in>I. \<mu>' (G i) = \<mu> i) \<and> measure_space \<Omega> (sigma_sets \<Omega> (G`I)) \<mu>') \<and> \<not> (\<forall>i\<in>I. \<mu> i = 0)
  2161       then measure_of \<Omega> (G`I) (SOME \<mu>'. (\<forall>i\<in>I. \<mu>' (G i) = \<mu> i) \<and> measure_space \<Omega> (sigma_sets \<Omega> (G`I)) \<mu>')
  2162       else measure_of \<Omega> (G`I) (\<lambda>_. 0))"
  2163 
  2164 lemma space_extend_measure: "G ` I \<subseteq> Pow \<Omega> \<Longrightarrow> space (extend_measure \<Omega> I G \<mu>) = \<Omega>"
  2165   unfolding extend_measure_def by simp
  2166 
  2167 lemma sets_extend_measure: "G ` I \<subseteq> Pow \<Omega> \<Longrightarrow> sets (extend_measure \<Omega> I G \<mu>) = sigma_sets \<Omega> (G`I)"
  2168   unfolding extend_measure_def by simp
  2169 
  2170 lemma emeasure_extend_measure:
  2171   assumes M: "M = extend_measure \<Omega> I G \<mu>"
  2172     and eq: "\<And>i. i \<in> I \<Longrightarrow> \<mu>' (G i) = \<mu> i"
  2173     and ms: "G ` I \<subseteq> Pow \<Omega>" "positive (sets M) \<mu>'" "countably_additive (sets M) \<mu>'"
  2174     and "i \<in> I"
  2175   shows "emeasure M (G i) = \<mu> i"
  2176 proof cases
  2177   assume *: "(\<forall>i\<in>I. \<mu> i = 0)"
  2178   with M have M_eq: "M = measure_of \<Omega> (G`I) (\<lambda>_. 0)"
  2179    by (simp add: extend_measure_def)
  2180   from measure_space_0[OF ms(1)] ms `i\<in>I`
  2181   have "emeasure M (G i) = 0"
  2182     by (intro emeasure_measure_of[OF M_eq]) (auto simp add: M measure_space_def sets_extend_measure)
  2183   with `i\<in>I` * show ?thesis
  2184     by simp
  2185 next
  2186   def P \<equiv> "\<lambda>\<mu>'. (\<forall>i\<in>I. \<mu>' (G i) = \<mu> i) \<and> measure_space \<Omega> (sigma_sets \<Omega> (G`I)) \<mu>'"
  2187   assume "\<not> (\<forall>i\<in>I. \<mu> i = 0)"
  2188   moreover
  2189   have "measure_space (space M) (sets M) \<mu>'"
  2190     using ms unfolding measure_space_def by auto default
  2191   with ms eq have "\<exists>\<mu>'. P \<mu>'"
  2192     unfolding P_def
  2193     by (intro exI[of _ \<mu>']) (auto simp add: M space_extend_measure sets_extend_measure)
  2194   ultimately have M_eq: "M = measure_of \<Omega> (G`I) (Eps P)"
  2195     by (simp add: M extend_measure_def P_def[symmetric])
  2196 
  2197   from `\<exists>\<mu>'. P \<mu>'` have P: "P (Eps P)" by (rule someI_ex)
  2198   show "emeasure M (G i) = \<mu> i"
  2199   proof (subst emeasure_measure_of[OF M_eq])
  2200     have sets_M: "sets M = sigma_sets \<Omega> (G`I)"
  2201       using M_eq ms by (auto simp: sets_extend_measure)
  2202     then show "G i \<in> sets M" using `i \<in> I` by auto
  2203     show "positive (sets M) (Eps P)" "countably_additive (sets M) (Eps P)" "Eps P (G i) = \<mu> i"
  2204       using P `i\<in>I` by (auto simp add: sets_M measure_space_def P_def)
  2205   qed fact
  2206 qed
  2207 
  2208 lemma emeasure_extend_measure_Pair:
  2209   assumes M: "M = extend_measure \<Omega> {(i, j). I i j} (\<lambda>(i, j). G i j) (\<lambda>(i, j). \<mu> i j)"
  2210     and eq: "\<And>i j. I i j \<Longrightarrow> \<mu>' (G i j) = \<mu> i j"
  2211     and ms: "\<And>i j. I i j \<Longrightarrow> G i j \<in> Pow \<Omega>" "positive (sets M) \<mu>'" "countably_additive (sets M) \<mu>'"
  2212     and "I i j"
  2213   shows "emeasure M (G i j) = \<mu> i j"
  2214   using emeasure_extend_measure[OF M _ _ ms(2,3), of "(i,j)"] eq ms(1) `I i j`
  2215   by (auto simp: subset_eq)
  2216 
  2217 subsubsection {* Sigma algebra generated by function preimages *}
  2218 
  2219 definition
  2220   "vimage_algebra M S X = sigma S ((\<lambda>A. X -` A \<inter> S) ` sets M)"
  2221 
  2222 lemma sigma_algebra_preimages:
  2223   fixes f :: "'x \<Rightarrow> 'a"
  2224   assumes "f \<in> S \<rightarrow> space M"
  2225   shows "sigma_algebra S ((\<lambda>A. f -` A \<inter> S) ` sets M)"
  2226     (is "sigma_algebra _ (?F ` sets M)")
  2227 proof (simp add: sigma_algebra_iff2, safe)
  2228   show "{} \<in> ?F ` sets M" by blast
  2229 next
  2230   fix A assume "A \<in> sets M"
  2231   moreover have "S - ?F A = ?F (space M - A)"
  2232     using assms by auto
  2233   ultimately show "S - ?F A \<in> ?F ` sets M"
  2234     by blast
  2235 next
  2236   fix A :: "nat \<Rightarrow> 'x set" assume *: "range A \<subseteq> ?F ` M"
  2237   have "\<forall>i. \<exists>b. b \<in> M \<and> A i = ?F b"
  2238   proof safe
  2239     fix i
  2240     have "A i \<in> ?F ` M" using * by auto
  2241     then show "\<exists>b. b \<in> M \<and> A i = ?F b" by auto
  2242   qed
  2243   from choice[OF this] obtain b where b: "range b \<subseteq> M" "\<And>i. A i = ?F (b i)"
  2244     by auto
  2245   then have "(\<Union>i. A i) = ?F (\<Union>i. b i)" by auto
  2246   then show "(\<Union>i. A i) \<in> ?F ` M" using b(1) by blast
  2247 qed
  2248 
  2249 lemma sets_vimage_algebra[simp]:
  2250   "f \<in> S \<rightarrow> space M \<Longrightarrow> sets (vimage_algebra M S f) = (\<lambda>A. f -` A \<inter> S) ` sets M"
  2251   using sigma_algebra.sets_measure_of_eq[OF sigma_algebra_preimages, of f S M]
  2252   by (simp add: vimage_algebra_def)
  2253 
  2254 lemma space_vimage_algebra[simp]:
  2255   "f \<in> S \<rightarrow> space M \<Longrightarrow> space (vimage_algebra M S f) = S"
  2256   using sigma_algebra.space_measure_of_eq[OF sigma_algebra_preimages, of f S M]
  2257   by (simp add: vimage_algebra_def)
  2258 
  2259 lemma in_vimage_algebra[simp]:
  2260   "f \<in> S \<rightarrow> space M \<Longrightarrow> A \<in> sets (vimage_algebra M S f) \<longleftrightarrow> (\<exists>B\<in>sets M. A = f -` B \<inter> S)"
  2261   by (simp add: image_iff)
  2262 
  2263 lemma measurable_vimage_algebra:
  2264   fixes S :: "'c set" assumes "f \<in> S \<rightarrow> space M"
  2265   shows "f \<in> measurable (vimage_algebra M S f) M"
  2266   unfolding measurable_def using assms by force
  2267 
  2268 lemma measurable_vimage:
  2269   fixes g :: "'a \<Rightarrow> 'c" and f :: "'d \<Rightarrow> 'a"
  2270   assumes "g \<in> measurable M M2" "f \<in> S \<rightarrow> space M"
  2271   shows "(\<lambda>x. g (f x)) \<in> measurable (vimage_algebra M S f) M2"
  2272 proof -
  2273   note measurable_vimage_algebra[OF assms(2)]
  2274   from measurable_comp[OF this assms(1)]
  2275   show ?thesis by (simp add: comp_def)
  2276 qed
  2277 
  2278 lemma sigma_sets_vimage:
  2279   assumes "f \<in> S' \<rightarrow> S" and "A \<subseteq> Pow S"
  2280   shows "sigma_sets S' ((\<lambda>X. f -` X \<inter> S') ` A) = (\<lambda>X. f -` X \<inter> S') ` sigma_sets S A"
  2281 proof (intro set_eqI iffI)
  2282   let ?F = "\<lambda>X. f -` X \<inter> S'"
  2283   fix X assume "X \<in> sigma_sets S' (?F ` A)"
  2284   then show "X \<in> ?F ` sigma_sets S A"
  2285   proof induct
  2286     case (Basic X) then obtain X' where "X = ?F X'" "X' \<in> A"
  2287       by auto
  2288     then show ?case by auto
  2289   next
  2290     case Empty then show ?case
  2291       by (auto intro!: image_eqI[of _ _ "{}"] sigma_sets.Empty)
  2292   next
  2293     case (Compl X) then obtain X' where X: "X = ?F X'" and "X' \<in> sigma_sets S A"
  2294       by auto
  2295     then have "S - X' \<in> sigma_sets S A"
  2296       by (auto intro!: sigma_sets.Compl)
  2297     then show ?case
  2298       using X assms by (auto intro!: image_eqI[where x="S - X'"])
  2299   next
  2300     case (Union F)
  2301     then have "\<forall>i. \<exists>F'.  F' \<in> sigma_sets S A \<and> F i = f -` F' \<inter> S'"
  2302       by (auto simp: image_iff Bex_def)
  2303     from choice[OF this] obtain F' where
  2304       "\<And>i. F' i \<in> sigma_sets S A" and "\<And>i. F i = f -` F' i \<inter> S'"
  2305       by auto
  2306     then show ?case
  2307       by (auto intro!: sigma_sets.Union image_eqI[where x="\<Union>i. F' i"])
  2308   qed
  2309 next
  2310   let ?F = "\<lambda>X. f -` X \<inter> S'"
  2311   fix X assume "X \<in> ?F ` sigma_sets S A"
  2312   then obtain X' where "X' \<in> sigma_sets S A" "X = ?F X'" by auto
  2313   then show "X \<in> sigma_sets S' (?F ` A)"
  2314   proof (induct arbitrary: X)
  2315     case Empty then show ?case by (auto intro: sigma_sets.Empty)
  2316   next
  2317     case (Compl X')
  2318     have "S' - (S' - X) \<in> sigma_sets S' (?F ` A)"
  2319       apply (rule sigma_sets.Compl)
  2320       using assms by (auto intro!: Compl.hyps simp: Compl.prems)
  2321     also have "S' - (S' - X) = X"
  2322       using assms Compl by auto
  2323     finally show ?case .
  2324   next
  2325     case (Union F)
  2326     have "(\<Union>i. f -` F i \<inter> S') \<in> sigma_sets S' (?F ` A)"
  2327       by (intro sigma_sets.Union Union.hyps) simp
  2328     also have "(\<Union>i. f -` F i \<inter> S') = X"
  2329       using assms Union by auto
  2330     finally show ?case .
  2331   qed auto
  2332 qed
  2333 
  2334 subsubsection {* Restricted Space Sigma Algebra *}
  2335 
  2336 definition restrict_space where
  2337   "restrict_space M \<Omega> = measure_of (\<Omega> \<inter> space M) ((op \<inter> \<Omega>) ` sets M) (emeasure M)"
  2338 
  2339 lemma space_restrict_space: "space (restrict_space M \<Omega>) = \<Omega> \<inter> space M"
  2340   using sets.sets_into_space unfolding restrict_space_def by (subst space_measure_of) auto
  2341 
  2342 lemma space_restrict_space2: "\<Omega> \<in> sets M \<Longrightarrow> space (restrict_space M \<Omega>) = \<Omega>"
  2343   by (simp add: space_restrict_space sets.sets_into_space)
  2344 
  2345 lemma sets_restrict_space: "sets (restrict_space M \<Omega>) = (op \<inter> \<Omega>) ` sets M"
  2346 proof -
  2347   have "sigma_sets (\<Omega> \<inter> space M) ((\<lambda>X. X \<inter> (\<Omega> \<inter> space M)) ` sets M) =
  2348     (\<lambda>X. X \<inter> (\<Omega> \<inter> space M)) ` sets M"
  2349     using sigma_sets_vimage[of "\<lambda>x. x" "\<Omega> \<inter> space M" "space M" "sets M"] sets.space_closed[of M]
  2350     by (simp add: sets.sigma_sets_eq)
  2351   moreover have "(\<lambda>X. X \<inter> (\<Omega> \<inter> space M)) ` sets M = (op \<inter> \<Omega>) ` sets M"
  2352     using sets.sets_into_space by (intro image_cong) auto
  2353   ultimately show ?thesis
  2354     using sets.sets_into_space[of _ M] unfolding restrict_space_def
  2355     by (subst sets_measure_of) fastforce+
  2356 qed
  2357 
  2358 lemma sets_restrict_space_iff:
  2359   "\<Omega> \<inter> space M \<in> sets M \<Longrightarrow> A \<in> sets (restrict_space M \<Omega>) \<longleftrightarrow> (A \<subseteq> \<Omega> \<and> A \<in> sets M)"
  2360 proof (subst sets_restrict_space, safe)
  2361   fix A assume "\<Omega> \<inter> space M \<in> sets M" and A: "A \<in> sets M"
  2362   then have "(\<Omega> \<inter> space M) \<inter> A \<in> sets M"
  2363     by rule
  2364   also have "(\<Omega> \<inter> space M) \<inter> A = \<Omega> \<inter> A"
  2365     using sets.sets_into_space[OF A] by auto
  2366   finally show "\<Omega> \<inter> A \<in> sets M"
  2367     by auto
  2368 qed auto
  2369 
  2370 lemma measurable_restrict_space1:
  2371   assumes \<Omega>: "\<Omega> \<inter> space M \<in> sets M" and f: "f \<in> measurable M N"
  2372   shows "f \<in> measurable (restrict_space M \<Omega>) N"
  2373   unfolding measurable_def
  2374 proof (intro CollectI conjI ballI)
  2375   show sp: "f \<in> space (restrict_space M \<Omega>) \<rightarrow> space N"
  2376     using measurable_space[OF f] sets.sets_into_space[OF \<Omega>] by (auto simp: space_restrict_space)
  2377 
  2378   fix A assume "A \<in> sets N"
  2379   have "f -` A \<inter> space (restrict_space M \<Omega>) = (f -` A \<inter> space M) \<inter> (\<Omega> \<inter> space M)"
  2380     using sets.sets_into_space[OF \<Omega>] by (auto simp: space_restrict_space)
  2381   also have "\<dots> \<in> sets (restrict_space M \<Omega>)"
  2382     unfolding sets_restrict_space_iff[OF \<Omega>]
  2383     using measurable_sets[OF f `A \<in> sets N`] \<Omega> by blast
  2384   finally show "f -` A \<inter> space (restrict_space M \<Omega>) \<in> sets (restrict_space M \<Omega>)" .
  2385 qed
  2386 
  2387 lemma measurable_restrict_space2:
  2388   "\<Omega> \<inter> space N \<in> sets N \<Longrightarrow> f \<in> space M \<rightarrow> \<Omega> \<Longrightarrow> f \<in> measurable M N \<Longrightarrow>
  2389     f \<in> measurable M (restrict_space N \<Omega>)"
  2390   by (simp add: measurable_def space_restrict_space sets_restrict_space_iff Pi_Int[symmetric])
  2391 
  2392 lemma measurable_restrict_space_iff:
  2393   assumes \<Omega>[simp, intro]: "\<Omega> \<inter> space M \<in> sets M" "c \<in> space N"
  2394   shows "f \<in> measurable (restrict_space M \<Omega>) N \<longleftrightarrow>
  2395     (\<lambda>x. if x \<in> \<Omega> then f x else c) \<in> measurable M N" (is "f \<in> measurable ?R N \<longleftrightarrow> ?f \<in> measurable M N")
  2396   unfolding measurable_def
  2397 proof safe
  2398   fix x assume "f \<in> space ?R \<rightarrow> space N" "x \<in> space M" then show "?f x \<in> space N"
  2399     using `c\<in>space N` by (auto simp: space_restrict_space)
  2400 next
  2401   fix x assume "?f \<in> space M \<rightarrow> space N" "x \<in> space ?R" then show "f x \<in> space N"
  2402     using `c\<in>space N` by (auto simp: space_restrict_space Pi_iff)
  2403 next
  2404   fix X assume X: "X \<in> sets N"
  2405   assume *[THEN bspec]: "\<forall>y\<in>sets N. f -` y \<inter> space ?R \<in> sets ?R"
  2406   have "?f -` X \<inter> space M = (f -` X \<inter> (\<Omega> \<inter> space M)) \<union> (if c \<in> X then (space M - (\<Omega> \<inter> space M)) else {})"
  2407     by (auto split: split_if_asm)
  2408   also have "\<dots> \<in> sets M"
  2409     using *[OF X] by (auto simp add: space_restrict_space sets_restrict_space_iff)
  2410   finally show "?f -` X \<inter> space M \<in> sets M" .
  2411 next
  2412   assume *[THEN bspec]: "\<forall>y\<in>sets N. ?f -` y \<inter> space M \<in> sets M"
  2413   fix X :: "'b set" assume X: "X \<in> sets N"
  2414   have "f -` X \<inter> (\<Omega> \<inter> space M) = (?f -` X \<inter> space M) \<inter> (\<Omega> \<inter> space M)"
  2415     by (auto simp: space_restrict_space)
  2416   also have "\<dots> \<in> sets M"
  2417     using *[OF X] by auto
  2418   finally show "f -` X \<inter> space ?R \<in> sets ?R"
  2419     by (auto simp add: sets_restrict_space_iff space_restrict_space)
  2420 qed
  2421 
  2422 end
  2423