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