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