src/HOL/Probability/Measurable.thy
author hoelzl
Thu Nov 13 17:19:52 2014 +0100 (2014-11-13)
changeset 59000 6eb0725503fc
parent 58965 a62cdcc5344b
child 59047 8d7cec9b861d
permissions -rw-r--r--
import general theorems from AFP/Markov_Models
     1 (*  Title:      HOL/Probability/Measurable.thy
     2     Author:     Johannes Hölzl <hoelzl@in.tum.de>
     3 *)
     4 theory Measurable
     5   imports
     6     Sigma_Algebra
     7     "~~/src/HOL/Library/Order_Continuity"
     8 begin
     9 
    10 hide_const (open) Order_Continuity.continuous
    11 
    12 subsection {* Measurability prover *}
    13 
    14 lemma (in algebra) sets_Collect_finite_All:
    15   assumes "\<And>i. i \<in> S \<Longrightarrow> {x\<in>\<Omega>. P i x} \<in> M" "finite S"
    16   shows "{x\<in>\<Omega>. \<forall>i\<in>S. P i x} \<in> M"
    17 proof -
    18   have "{x\<in>\<Omega>. \<forall>i\<in>S. P i x} = (if S = {} then \<Omega> else \<Inter>i\<in>S. {x\<in>\<Omega>. P i x})"
    19     by auto
    20   with assms show ?thesis by (auto intro!: sets_Collect_finite_All')
    21 qed
    22 
    23 abbreviation "pred M P \<equiv> P \<in> measurable M (count_space (UNIV::bool set))"
    24 
    25 lemma pred_def: "pred M P \<longleftrightarrow> {x\<in>space M. P x} \<in> sets M"
    26 proof
    27   assume "pred M P"
    28   then have "P -` {True} \<inter> space M \<in> sets M"
    29     by (auto simp: measurable_count_space_eq2)
    30   also have "P -` {True} \<inter> space M = {x\<in>space M. P x}" by auto
    31   finally show "{x\<in>space M. P x} \<in> sets M" .
    32 next
    33   assume P: "{x\<in>space M. P x} \<in> sets M"
    34   moreover
    35   { fix X
    36     have "X \<in> Pow (UNIV :: bool set)" by simp
    37     then have "P -` X \<inter> space M = {x\<in>space M. ((X = {True} \<longrightarrow> P x) \<and> (X = {False} \<longrightarrow> \<not> P x) \<and> X \<noteq> {})}"
    38       unfolding UNIV_bool Pow_insert Pow_empty by auto
    39     then have "P -` X \<inter> space M \<in> sets M"
    40       by (auto intro!: sets.sets_Collect_neg sets.sets_Collect_imp sets.sets_Collect_conj sets.sets_Collect_const P) }
    41   then show "pred M P"
    42     by (auto simp: measurable_def)
    43 qed
    44 
    45 lemma pred_sets1: "{x\<in>space M. P x} \<in> sets M \<Longrightarrow> f \<in> measurable N M \<Longrightarrow> pred N (\<lambda>x. P (f x))"
    46   by (rule measurable_compose[where f=f and N=M]) (auto simp: pred_def)
    47 
    48 lemma pred_sets2: "A \<in> sets N \<Longrightarrow> f \<in> measurable M N \<Longrightarrow> pred M (\<lambda>x. f x \<in> A)"
    49   by (rule measurable_compose[where f=f and N=N]) (auto simp: pred_def Int_def[symmetric])
    50 
    51 ML_file "measurable.ML"
    52 
    53 attribute_setup measurable = {*
    54   Scan.lift (Scan.optional (Args.$$$ "del" >> K false) true --
    55     Scan.optional (Args.parens (Scan.optional (Args.$$$ "raw" >> K true) false --
    56       Scan.optional (Args.$$$ "generic" >> K Measurable.Generic) Measurable.Concrete))
    57     (false, Measurable.Concrete) >> (Thm.declaration_attribute o uncurry Measurable.add_del_thm))
    58 *} "declaration of measurability theorems"
    59 
    60 attribute_setup measurable_dest = {*
    61   Scan.lift (Scan.succeed (Thm.declaration_attribute Measurable.add_dest))
    62 *} "add dest rule for measurability prover"
    63 
    64 attribute_setup measurable_app = {*
    65   Scan.lift (Scan.succeed (Thm.declaration_attribute Measurable.add_app))
    66 *} "add application rule for measurability prover"
    67 
    68 method_setup measurable = {*
    69   Scan.lift (Scan.succeed (fn ctxt => METHOD (fn facts => Measurable.measurable_tac ctxt facts)))
    70 *} "measurability prover"
    71 
    72 simproc_setup measurable ("A \<in> sets M" | "f \<in> measurable M N") = {* K Measurable.simproc *}
    73 
    74 setup {*
    75   Global_Theory.add_thms_dynamic (@{binding measurable}, Measurable.get_all o Context.proof_of)
    76 *}
    77 
    78 declare
    79   measurable_compose_rev[measurable_dest]
    80   pred_sets1[measurable_dest]
    81   pred_sets2[measurable_dest]
    82   sets.sets_into_space[measurable_dest]
    83 
    84 declare
    85   sets.top[measurable]
    86   sets.empty_sets[measurable (raw)]
    87   sets.Un[measurable (raw)]
    88   sets.Diff[measurable (raw)]
    89 
    90 declare
    91   measurable_count_space[measurable (raw)]
    92   measurable_ident[measurable (raw)]
    93   measurable_ident_sets[measurable (raw)]
    94   measurable_const[measurable (raw)]
    95   measurable_If[measurable (raw)]
    96   measurable_comp[measurable (raw)]
    97   measurable_sets[measurable (raw)]
    98 
    99 lemma predE[measurable (raw)]: 
   100   "pred M P \<Longrightarrow> {x\<in>space M. P x} \<in> sets M"
   101   unfolding pred_def .
   102 
   103 lemma pred_intros_imp'[measurable (raw)]:
   104   "(K \<Longrightarrow> pred M (\<lambda>x. P x)) \<Longrightarrow> pred M (\<lambda>x. K \<longrightarrow> P x)"
   105   by (cases K) auto
   106 
   107 lemma pred_intros_conj1'[measurable (raw)]:
   108   "(K \<Longrightarrow> pred M (\<lambda>x. P x)) \<Longrightarrow> pred M (\<lambda>x. K \<and> P x)"
   109   by (cases K) auto
   110 
   111 lemma pred_intros_conj2'[measurable (raw)]:
   112   "(K \<Longrightarrow> pred M (\<lambda>x. P x)) \<Longrightarrow> pred M (\<lambda>x. P x \<and> K)"
   113   by (cases K) auto
   114 
   115 lemma pred_intros_disj1'[measurable (raw)]:
   116   "(\<not> K \<Longrightarrow> pred M (\<lambda>x. P x)) \<Longrightarrow> pred M (\<lambda>x. K \<or> P x)"
   117   by (cases K) auto
   118 
   119 lemma pred_intros_disj2'[measurable (raw)]:
   120   "(\<not> K \<Longrightarrow> pred M (\<lambda>x. P x)) \<Longrightarrow> pred M (\<lambda>x. P x \<or> K)"
   121   by (cases K) auto
   122 
   123 lemma pred_intros_logic[measurable (raw)]:
   124   "pred M (\<lambda>x. x \<in> space M)"
   125   "pred M (\<lambda>x. P x) \<Longrightarrow> pred M (\<lambda>x. \<not> P x)"
   126   "pred M (\<lambda>x. Q x) \<Longrightarrow> pred M (\<lambda>x. P x) \<Longrightarrow> pred M (\<lambda>x. Q x \<and> P x)"
   127   "pred M (\<lambda>x. Q x) \<Longrightarrow> pred M (\<lambda>x. P x) \<Longrightarrow> pred M (\<lambda>x. Q x \<longrightarrow> P x)"
   128   "pred M (\<lambda>x. Q x) \<Longrightarrow> pred M (\<lambda>x. P x) \<Longrightarrow> pred M (\<lambda>x. Q x \<or> P x)"
   129   "pred M (\<lambda>x. Q x) \<Longrightarrow> pred M (\<lambda>x. P x) \<Longrightarrow> pred M (\<lambda>x. Q x = P x)"
   130   "pred M (\<lambda>x. f x \<in> UNIV)"
   131   "pred M (\<lambda>x. f x \<in> {})"
   132   "pred M (\<lambda>x. P' (f x) x) \<Longrightarrow> pred M (\<lambda>x. f x \<in> {y. P' y x})"
   133   "pred M (\<lambda>x. f x \<in> (B x)) \<Longrightarrow> pred M (\<lambda>x. f x \<in> - (B x))"
   134   "pred M (\<lambda>x. f x \<in> (A x)) \<Longrightarrow> pred M (\<lambda>x. f x \<in> (B x)) \<Longrightarrow> pred M (\<lambda>x. f x \<in> (A x) - (B x))"
   135   "pred M (\<lambda>x. f x \<in> (A x)) \<Longrightarrow> pred M (\<lambda>x. f x \<in> (B x)) \<Longrightarrow> pred M (\<lambda>x. f x \<in> (A x) \<inter> (B x))"
   136   "pred M (\<lambda>x. f x \<in> (A x)) \<Longrightarrow> pred M (\<lambda>x. f x \<in> (B x)) \<Longrightarrow> pred M (\<lambda>x. f x \<in> (A x) \<union> (B x))"
   137   "pred M (\<lambda>x. g x (f x) \<in> (X x)) \<Longrightarrow> pred M (\<lambda>x. f x \<in> (g x) -` (X x))"
   138   by (auto simp: iff_conv_conj_imp pred_def)
   139 
   140 lemma pred_intros_countable[measurable (raw)]:
   141   fixes P :: "'a \<Rightarrow> 'i :: countable \<Rightarrow> bool"
   142   shows 
   143     "(\<And>i. pred M (\<lambda>x. P x i)) \<Longrightarrow> pred M (\<lambda>x. \<forall>i. P x i)"
   144     "(\<And>i. pred M (\<lambda>x. P x i)) \<Longrightarrow> pred M (\<lambda>x. \<exists>i. P x i)"
   145   by (auto intro!: sets.sets_Collect_countable_All sets.sets_Collect_countable_Ex simp: pred_def)
   146 
   147 lemma pred_intros_countable_bounded[measurable (raw)]:
   148   fixes X :: "'i :: countable set"
   149   shows 
   150     "(\<And>i. i \<in> X \<Longrightarrow> pred M (\<lambda>x. x \<in> N x i)) \<Longrightarrow> pred M (\<lambda>x. x \<in> (\<Inter>i\<in>X. N x i))"
   151     "(\<And>i. i \<in> X \<Longrightarrow> pred M (\<lambda>x. x \<in> N x i)) \<Longrightarrow> pred M (\<lambda>x. x \<in> (\<Union>i\<in>X. N x i))"
   152     "(\<And>i. i \<in> X \<Longrightarrow> pred M (\<lambda>x. P x i)) \<Longrightarrow> pred M (\<lambda>x. \<forall>i\<in>X. P x i)"
   153     "(\<And>i. i \<in> X \<Longrightarrow> pred M (\<lambda>x. P x i)) \<Longrightarrow> pred M (\<lambda>x. \<exists>i\<in>X. P x i)"
   154   by (auto simp: Bex_def Ball_def)
   155 
   156 lemma pred_intros_finite[measurable (raw)]:
   157   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> pred M (\<lambda>x. x \<in> N x i)) \<Longrightarrow> pred M (\<lambda>x. x \<in> (\<Inter>i\<in>I. N x i))"
   158   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> pred M (\<lambda>x. x \<in> N x i)) \<Longrightarrow> pred M (\<lambda>x. x \<in> (\<Union>i\<in>I. N x i))"
   159   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> pred M (\<lambda>x. P x i)) \<Longrightarrow> pred M (\<lambda>x. \<forall>i\<in>I. P x i)"
   160   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> pred M (\<lambda>x. P x i)) \<Longrightarrow> pred M (\<lambda>x. \<exists>i\<in>I. P x i)"
   161   by (auto intro!: sets.sets_Collect_finite_Ex sets.sets_Collect_finite_All simp: iff_conv_conj_imp pred_def)
   162 
   163 lemma countable_Un_Int[measurable (raw)]:
   164   "(\<And>i :: 'i :: countable. i \<in> I \<Longrightarrow> N i \<in> sets M) \<Longrightarrow> (\<Union>i\<in>I. N i) \<in> sets M"
   165   "I \<noteq> {} \<Longrightarrow> (\<And>i :: 'i :: countable. i \<in> I \<Longrightarrow> N i \<in> sets M) \<Longrightarrow> (\<Inter>i\<in>I. N i) \<in> sets M"
   166   by auto
   167 
   168 declare
   169   finite_UN[measurable (raw)]
   170   finite_INT[measurable (raw)]
   171 
   172 lemma sets_Int_pred[measurable (raw)]:
   173   assumes space: "A \<inter> B \<subseteq> space M" and [measurable]: "pred M (\<lambda>x. x \<in> A)" "pred M (\<lambda>x. x \<in> B)"
   174   shows "A \<inter> B \<in> sets M"
   175 proof -
   176   have "{x\<in>space M. x \<in> A \<inter> B} \<in> sets M" by auto
   177   also have "{x\<in>space M. x \<in> A \<inter> B} = A \<inter> B"
   178     using space by auto
   179   finally show ?thesis .
   180 qed
   181 
   182 lemma [measurable (raw generic)]:
   183   assumes f: "f \<in> measurable M N" and c: "c \<in> space N \<Longrightarrow> {c} \<in> sets N"
   184   shows pred_eq_const1: "pred M (\<lambda>x. f x = c)"
   185     and pred_eq_const2: "pred M (\<lambda>x. c = f x)"
   186 proof -
   187   show "pred M (\<lambda>x. f x = c)"
   188   proof cases
   189     assume "c \<in> space N"
   190     with measurable_sets[OF f c] show ?thesis
   191       by (auto simp: Int_def conj_commute pred_def)
   192   next
   193     assume "c \<notin> space N"
   194     with f[THEN measurable_space] have "{x \<in> space M. f x = c} = {}" by auto
   195     then show ?thesis by (auto simp: pred_def cong: conj_cong)
   196   qed
   197   then show "pred M (\<lambda>x. c = f x)"
   198     by (simp add: eq_commute)
   199 qed
   200 
   201 lemma pred_count_space_const1[measurable (raw)]:
   202   "f \<in> measurable M (count_space UNIV) \<Longrightarrow> Measurable.pred M (\<lambda>x. f x = c)"
   203   by (intro pred_eq_const1[where N="count_space UNIV"]) (auto )
   204 
   205 lemma pred_count_space_const2[measurable (raw)]:
   206   "f \<in> measurable M (count_space UNIV) \<Longrightarrow> Measurable.pred M (\<lambda>x. c = f x)"
   207   by (intro pred_eq_const2[where N="count_space UNIV"]) (auto )
   208 
   209 lemma pred_le_const[measurable (raw generic)]:
   210   assumes f: "f \<in> measurable M N" and c: "{.. c} \<in> sets N" shows "pred M (\<lambda>x. f x \<le> c)"
   211   using measurable_sets[OF f c]
   212   by (auto simp: Int_def conj_commute eq_commute pred_def)
   213 
   214 lemma pred_const_le[measurable (raw generic)]:
   215   assumes f: "f \<in> measurable M N" and c: "{c ..} \<in> sets N" shows "pred M (\<lambda>x. c \<le> f x)"
   216   using measurable_sets[OF f c]
   217   by (auto simp: Int_def conj_commute eq_commute pred_def)
   218 
   219 lemma pred_less_const[measurable (raw generic)]:
   220   assumes f: "f \<in> measurable M N" and c: "{..< c} \<in> sets N" shows "pred M (\<lambda>x. f x < c)"
   221   using measurable_sets[OF f c]
   222   by (auto simp: Int_def conj_commute eq_commute pred_def)
   223 
   224 lemma pred_const_less[measurable (raw generic)]:
   225   assumes f: "f \<in> measurable M N" and c: "{c <..} \<in> sets N" shows "pred M (\<lambda>x. c < f x)"
   226   using measurable_sets[OF f c]
   227   by (auto simp: Int_def conj_commute eq_commute pred_def)
   228 
   229 declare
   230   sets.Int[measurable (raw)]
   231 
   232 lemma pred_in_If[measurable (raw)]:
   233   "(P \<Longrightarrow> pred M (\<lambda>x. x \<in> A x)) \<Longrightarrow> (\<not> P \<Longrightarrow> pred M (\<lambda>x. x \<in> B x)) \<Longrightarrow>
   234     pred M (\<lambda>x. x \<in> (if P then A x else B x))"
   235   by auto
   236 
   237 lemma sets_range[measurable_dest]:
   238   "A ` I \<subseteq> sets M \<Longrightarrow> i \<in> I \<Longrightarrow> A i \<in> sets M"
   239   by auto
   240 
   241 lemma pred_sets_range[measurable_dest]:
   242   "A ` I \<subseteq> sets N \<Longrightarrow> i \<in> I \<Longrightarrow> f \<in> measurable M N \<Longrightarrow> pred M (\<lambda>x. f x \<in> A i)"
   243   using pred_sets2[OF sets_range] by auto
   244 
   245 lemma sets_All[measurable_dest]:
   246   "\<forall>i. A i \<in> sets (M i) \<Longrightarrow> A i \<in> sets (M i)"
   247   by auto
   248 
   249 lemma pred_sets_All[measurable_dest]:
   250   "\<forall>i. A i \<in> sets (N i) \<Longrightarrow> f \<in> measurable M (N i) \<Longrightarrow> pred M (\<lambda>x. f x \<in> A i)"
   251   using pred_sets2[OF sets_All, of A N f] by auto
   252 
   253 lemma sets_Ball[measurable_dest]:
   254   "\<forall>i\<in>I. A i \<in> sets (M i) \<Longrightarrow> i\<in>I \<Longrightarrow> A i \<in> sets (M i)"
   255   by auto
   256 
   257 lemma pred_sets_Ball[measurable_dest]:
   258   "\<forall>i\<in>I. A i \<in> sets (N i) \<Longrightarrow> i\<in>I \<Longrightarrow> f \<in> measurable M (N i) \<Longrightarrow> pred M (\<lambda>x. f x \<in> A i)"
   259   using pred_sets2[OF sets_Ball, of _ _ _ f] by auto
   260 
   261 lemma measurable_finite[measurable (raw)]:
   262   fixes S :: "'a \<Rightarrow> nat set"
   263   assumes [measurable]: "\<And>i. {x\<in>space M. i \<in> S x} \<in> sets M"
   264   shows "pred M (\<lambda>x. finite (S x))"
   265   unfolding finite_nat_set_iff_bounded by (simp add: Ball_def)
   266 
   267 lemma measurable_Least[measurable]:
   268   assumes [measurable]: "(\<And>i::nat. (\<lambda>x. P i x) \<in> measurable M (count_space UNIV))"q
   269   shows "(\<lambda>x. LEAST i. P i x) \<in> measurable M (count_space UNIV)"
   270   unfolding measurable_def by (safe intro!: sets_Least) simp_all
   271 
   272 lemma measurable_Max_nat[measurable (raw)]: 
   273   fixes P :: "nat \<Rightarrow> 'a \<Rightarrow> bool"
   274   assumes [measurable]: "\<And>i. Measurable.pred M (P i)"
   275   shows "(\<lambda>x. Max {i. P i x}) \<in> measurable M (count_space UNIV)"
   276   unfolding measurable_count_space_eq2_countable
   277 proof safe
   278   fix n
   279 
   280   { fix x assume "\<forall>i. \<exists>n\<ge>i. P n x"
   281     then have "infinite {i. P i x}"
   282       unfolding infinite_nat_iff_unbounded_le by auto
   283     then have "Max {i. P i x} = the None"
   284       by (rule Max.infinite) }
   285   note 1 = this
   286 
   287   { fix x i j assume "P i x" "\<forall>n\<ge>j. \<not> P n x"
   288     then have "finite {i. P i x}"
   289       by (auto simp: subset_eq not_le[symmetric] finite_nat_iff_bounded)
   290     with `P i x` have "P (Max {i. P i x}) x" "i \<le> Max {i. P i x}" "finite {i. P i x}"
   291       using Max_in[of "{i. P i x}"] by auto }
   292   note 2 = this
   293 
   294   have "(\<lambda>x. Max {i. P i x}) -` {n} \<inter> space M = {x\<in>space M. Max {i. P i x} = n}"
   295     by auto
   296   also have "\<dots> = 
   297     {x\<in>space M. if (\<forall>i. \<exists>n\<ge>i. P n x) then the None = n else 
   298       if (\<exists>i. P i x) then P n x \<and> (\<forall>i>n. \<not> P i x)
   299       else Max {} = n}"
   300     by (intro arg_cong[where f=Collect] ext conj_cong)
   301        (auto simp add: 1 2 not_le[symmetric] intro!: Max_eqI)
   302   also have "\<dots> \<in> sets M"
   303     by measurable
   304   finally show "(\<lambda>x. Max {i. P i x}) -` {n} \<inter> space M \<in> sets M" .
   305 qed simp
   306 
   307 lemma measurable_Min_nat[measurable (raw)]: 
   308   fixes P :: "nat \<Rightarrow> 'a \<Rightarrow> bool"
   309   assumes [measurable]: "\<And>i. Measurable.pred M (P i)"
   310   shows "(\<lambda>x. Min {i. P i x}) \<in> measurable M (count_space UNIV)"
   311   unfolding measurable_count_space_eq2_countable
   312 proof safe
   313   fix n
   314 
   315   { fix x assume "\<forall>i. \<exists>n\<ge>i. P n x"
   316     then have "infinite {i. P i x}"
   317       unfolding infinite_nat_iff_unbounded_le by auto
   318     then have "Min {i. P i x} = the None"
   319       by (rule Min.infinite) }
   320   note 1 = this
   321 
   322   { fix x i j assume "P i x" "\<forall>n\<ge>j. \<not> P n x"
   323     then have "finite {i. P i x}"
   324       by (auto simp: subset_eq not_le[symmetric] finite_nat_iff_bounded)
   325     with `P i x` have "P (Min {i. P i x}) x" "Min {i. P i x} \<le> i" "finite {i. P i x}"
   326       using Min_in[of "{i. P i x}"] by auto }
   327   note 2 = this
   328 
   329   have "(\<lambda>x. Min {i. P i x}) -` {n} \<inter> space M = {x\<in>space M. Min {i. P i x} = n}"
   330     by auto
   331   also have "\<dots> = 
   332     {x\<in>space M. if (\<forall>i. \<exists>n\<ge>i. P n x) then the None = n else 
   333       if (\<exists>i. P i x) then P n x \<and> (\<forall>i<n. \<not> P i x)
   334       else Min {} = n}"
   335     by (intro arg_cong[where f=Collect] ext conj_cong)
   336        (auto simp add: 1 2 not_le[symmetric] intro!: Min_eqI)
   337   also have "\<dots> \<in> sets M"
   338     by measurable
   339   finally show "(\<lambda>x. Min {i. P i x}) -` {n} \<inter> space M \<in> sets M" .
   340 qed simp
   341 
   342 lemma measurable_count_space_insert[measurable (raw)]:
   343   "s \<in> S \<Longrightarrow> A \<in> sets (count_space S) \<Longrightarrow> insert s A \<in> sets (count_space S)"
   344   by simp
   345 
   346 lemma sets_UNIV [measurable (raw)]: "A \<in> sets (count_space UNIV)"
   347   by simp
   348 
   349 lemma measurable_card[measurable]:
   350   fixes S :: "'a \<Rightarrow> nat set"
   351   assumes [measurable]: "\<And>i. {x\<in>space M. i \<in> S x} \<in> sets M"
   352   shows "(\<lambda>x. card (S x)) \<in> measurable M (count_space UNIV)"
   353   unfolding measurable_count_space_eq2_countable
   354 proof safe
   355   fix n show "(\<lambda>x. card (S x)) -` {n} \<inter> space M \<in> sets M"
   356   proof (cases n)
   357     case 0
   358     then have "(\<lambda>x. card (S x)) -` {n} \<inter> space M = {x\<in>space M. infinite (S x) \<or> (\<forall>i. i \<notin> S x)}"
   359       by auto
   360     also have "\<dots> \<in> sets M"
   361       by measurable
   362     finally show ?thesis .
   363   next
   364     case (Suc i)
   365     then have "(\<lambda>x. card (S x)) -` {n} \<inter> space M =
   366       (\<Union>F\<in>{A\<in>{A. finite A}. card A = n}. {x\<in>space M. (\<forall>i. i \<in> S x \<longleftrightarrow> i \<in> F)})"
   367       unfolding set_eq_iff[symmetric] Collect_bex_eq[symmetric] by (auto intro: card_ge_0_finite)
   368     also have "\<dots> \<in> sets M"
   369       by (intro sets.countable_UN' countable_Collect countable_Collect_finite) auto
   370     finally show ?thesis .
   371   qed
   372 qed rule
   373 
   374 subsection {* Measurability for (co)inductive predicates *}
   375 
   376 lemma measurable_lfp:
   377   assumes "Order_Continuity.continuous F"
   378   assumes *: "\<And>A. pred M A \<Longrightarrow> pred M (F A)"
   379   shows "pred M (lfp F)"
   380 proof -
   381   { fix i have "Measurable.pred M (\<lambda>x. (F ^^ i) (\<lambda>x. False) x)"
   382       by (induct i) (auto intro!: *) }
   383   then have "Measurable.pred M (\<lambda>x. \<exists>i. (F ^^ i) (\<lambda>x. False) x)"
   384     by measurable
   385   also have "(\<lambda>x. \<exists>i. (F ^^ i) (\<lambda>x. False) x) = (SUP i. (F ^^ i) bot)"
   386     by (auto simp add: bot_fun_def)
   387   also have "\<dots> = lfp F"
   388     by (rule continuous_lfp[symmetric]) fact
   389   finally show ?thesis .
   390 qed
   391 
   392 lemma measurable_gfp:
   393   assumes "Order_Continuity.down_continuous F"
   394   assumes *: "\<And>A. pred M A \<Longrightarrow> pred M (F A)"
   395   shows "pred M (gfp F)"
   396 proof -
   397   { fix i have "Measurable.pred M (\<lambda>x. (F ^^ i) (\<lambda>x. True) x)"
   398       by (induct i) (auto intro!: *) }
   399   then have "Measurable.pred M (\<lambda>x. \<forall>i. (F ^^ i) (\<lambda>x. True) x)"
   400     by measurable
   401   also have "(\<lambda>x. \<forall>i. (F ^^ i) (\<lambda>x. True) x) = (INF i. (F ^^ i) top)"
   402     by (auto simp add: top_fun_def)
   403   also have "\<dots> = gfp F"
   404     by (rule down_continuous_gfp[symmetric]) fact
   405   finally show ?thesis .
   406 qed
   407 
   408 lemma measurable_lfp_coinduct[consumes 1, case_names continuity step]:
   409   assumes "P M"
   410   assumes "Order_Continuity.continuous F"
   411   assumes *: "\<And>M A. P M \<Longrightarrow> (\<And>N. P N \<Longrightarrow> Measurable.pred N A) \<Longrightarrow> Measurable.pred M (F A)"
   412   shows "Measurable.pred M (lfp F)"
   413 proof -
   414   { fix i from `P M` have "Measurable.pred M (\<lambda>x. (F ^^ i) (\<lambda>x. False) x)"
   415       by (induct i arbitrary: M) (auto intro!: *) }
   416   then have "Measurable.pred M (\<lambda>x. \<exists>i. (F ^^ i) (\<lambda>x. False) x)"
   417     by measurable
   418   also have "(\<lambda>x. \<exists>i. (F ^^ i) (\<lambda>x. False) x) = (SUP i. (F ^^ i) bot)"
   419     by (auto simp add: bot_fun_def)
   420   also have "\<dots> = lfp F"
   421     by (rule continuous_lfp[symmetric]) fact
   422   finally show ?thesis .
   423 qed
   424 
   425 lemma measurable_gfp_coinduct[consumes 1, case_names continuity step]:
   426   assumes "P M"
   427   assumes "Order_Continuity.down_continuous F"
   428   assumes *: "\<And>M A. P M \<Longrightarrow> (\<And>N. P N \<Longrightarrow> Measurable.pred N A) \<Longrightarrow> Measurable.pred M (F A)"
   429   shows "Measurable.pred M (gfp F)"
   430 proof -
   431   { fix i from `P M` have "Measurable.pred M (\<lambda>x. (F ^^ i) (\<lambda>x. True) x)"
   432       by (induct i arbitrary: M) (auto intro!: *) }
   433   then have "Measurable.pred M (\<lambda>x. \<forall>i. (F ^^ i) (\<lambda>x. True) x)"
   434     by measurable
   435   also have "(\<lambda>x. \<forall>i. (F ^^ i) (\<lambda>x. True) x) = (INF i. (F ^^ i) top)"
   436     by (auto simp add: top_fun_def)
   437   also have "\<dots> = gfp F"
   438     by (rule down_continuous_gfp[symmetric]) fact
   439   finally show ?thesis .
   440 qed
   441 
   442 lemma measurable_lfp2_coinduct[consumes 1, case_names continuity step]:
   443   assumes "P M s"
   444   assumes "Order_Continuity.continuous F"
   445   assumes *: "\<And>M A s. P M s \<Longrightarrow> (\<And>N t. P N t \<Longrightarrow> Measurable.pred N (A t)) \<Longrightarrow> Measurable.pred M (F A s)"
   446   shows "Measurable.pred M (lfp F s)"
   447 proof -
   448   { fix i from `P M s` have "Measurable.pred M (\<lambda>x. (F ^^ i) (\<lambda>t x. False) s x)"
   449       by (induct i arbitrary: M s) (auto intro!: *) }
   450   then have "Measurable.pred M (\<lambda>x. \<exists>i. (F ^^ i) (\<lambda>t x. False) s x)"
   451     by measurable
   452   also have "(\<lambda>x. \<exists>i. (F ^^ i) (\<lambda>t x. False) s x) = (SUP i. (F ^^ i) bot) s"
   453     by (auto simp add: bot_fun_def)
   454   also have "(SUP i. (F ^^ i) bot) = lfp F"
   455     by (rule continuous_lfp[symmetric]) fact
   456   finally show ?thesis .
   457 qed
   458 
   459 lemma measurable_gfp2_coinduct[consumes 1, case_names continuity step]:
   460   assumes "P M s"
   461   assumes "Order_Continuity.down_continuous F"
   462   assumes *: "\<And>M A s. P M s \<Longrightarrow> (\<And>N t. P N t \<Longrightarrow> Measurable.pred N (A t)) \<Longrightarrow> Measurable.pred M (F A s)"
   463   shows "Measurable.pred M (gfp F s)"
   464 proof -
   465   { fix i from `P M s` have "Measurable.pred M (\<lambda>x. (F ^^ i) (\<lambda>t x. True) s x)"
   466       by (induct i arbitrary: M s) (auto intro!: *) }
   467   then have "Measurable.pred M (\<lambda>x. \<forall>i. (F ^^ i) (\<lambda>t x. True) s x)"
   468     by measurable
   469   also have "(\<lambda>x. \<forall>i. (F ^^ i) (\<lambda>t x. True) s x) = (INF i. (F ^^ i) top) s"
   470     by (auto simp add: top_fun_def)
   471   also have "(INF i. (F ^^ i) top) = gfp F"
   472     by (rule down_continuous_gfp[symmetric]) fact
   473   finally show ?thesis .
   474 qed
   475 
   476 lemma measurable_enat_coinduct:
   477   fixes f :: "'a \<Rightarrow> enat"
   478   assumes "R f"
   479   assumes *: "\<And>f. R f \<Longrightarrow> \<exists>g h i P. R g \<and> f = (\<lambda>x. if P x then h x else eSuc (g (i x))) \<and> 
   480     Measurable.pred M P \<and>
   481     i \<in> measurable M M \<and>
   482     h \<in> measurable M (count_space UNIV)"
   483   shows "f \<in> measurable M (count_space UNIV)"
   484 proof (simp add: measurable_count_space_eq2_countable, rule )
   485   fix a :: enat
   486   have "f -` {a} \<inter> space M = {x\<in>space M. f x = a}"
   487     by auto
   488   { fix i :: nat
   489     from `R f` have "Measurable.pred M (\<lambda>x. f x = enat i)"
   490     proof (induction i arbitrary: f)
   491       case 0
   492       from *[OF this] obtain g h i P
   493         where f: "f = (\<lambda>x. if P x then h x else eSuc (g (i x)))" and
   494           [measurable]: "Measurable.pred M P" "i \<in> measurable M M" "h \<in> measurable M (count_space UNIV)"
   495         by auto
   496       have "Measurable.pred M (\<lambda>x. P x \<and> h x = 0)"
   497         by measurable
   498       also have "(\<lambda>x. P x \<and> h x = 0) = (\<lambda>x. f x = enat 0)"
   499         by (auto simp: f zero_enat_def[symmetric])
   500       finally show ?case .
   501     next
   502       case (Suc n)
   503       from *[OF Suc.prems] obtain g h i P
   504         where f: "f = (\<lambda>x. if P x then h x else eSuc (g (i x)))" and "R g" and
   505           M[measurable]: "Measurable.pred M P" "i \<in> measurable M M" "h \<in> measurable M (count_space UNIV)"
   506         by auto
   507       have "(\<lambda>x. f x = enat (Suc n)) =
   508         (\<lambda>x. (P x \<longrightarrow> h x = enat (Suc n)) \<and> (\<not> P x \<longrightarrow> g (i x) = enat n))"
   509         by (auto simp: f zero_enat_def[symmetric] eSuc_enat[symmetric])
   510       also have "Measurable.pred M \<dots>"
   511         by (intro pred_intros_logic measurable_compose[OF M(2)] Suc `R g`) measurable
   512       finally show ?case .
   513     qed
   514     then have "f -` {enat i} \<inter> space M \<in> sets M"
   515       by (simp add: pred_def Int_def conj_commute) }
   516   note fin = this
   517   show "f -` {a} \<inter> space M \<in> sets M"
   518   proof (cases a)
   519     case infinity
   520     then have "f -` {a} \<inter> space M = space M - (\<Union>n. f -` {enat n} \<inter> space M)"
   521       by auto
   522     also have "\<dots> \<in> sets M"
   523       by (intro sets.Diff sets.top sets.Un sets.countable_UN) (auto intro!: fin)
   524     finally show ?thesis .
   525   qed (simp add: fin)
   526 qed
   527 
   528 lemma measurable_pred_countable[measurable (raw)]:
   529   assumes "countable X"
   530   shows 
   531     "(\<And>i. i \<in> X \<Longrightarrow> Measurable.pred M (\<lambda>x. P x i)) \<Longrightarrow> Measurable.pred M (\<lambda>x. \<forall>i\<in>X. P x i)"
   532     "(\<And>i. i \<in> X \<Longrightarrow> Measurable.pred M (\<lambda>x. P x i)) \<Longrightarrow> Measurable.pred M (\<lambda>x. \<exists>i\<in>X. P x i)"
   533   unfolding pred_def
   534   by (auto intro!: sets.sets_Collect_countable_All' sets.sets_Collect_countable_Ex' assms)
   535 
   536 lemma measurable_THE:
   537   fixes P :: "'a \<Rightarrow> 'b \<Rightarrow> bool"
   538   assumes [measurable]: "\<And>i. Measurable.pred M (P i)"
   539   assumes I[simp]: "countable I" "\<And>i x. x \<in> space M \<Longrightarrow> P i x \<Longrightarrow> i \<in> I"
   540   assumes unique: "\<And>x i j. x \<in> space M \<Longrightarrow> P i x \<Longrightarrow> P j x \<Longrightarrow> i = j"
   541   shows "(\<lambda>x. THE i. P i x) \<in> measurable M (count_space UNIV)"
   542   unfolding measurable_def
   543 proof safe
   544   fix X
   545   def f \<equiv> "\<lambda>x. THE i. P i x" def undef \<equiv> "THE i::'a. False"
   546   { fix i x assume "x \<in> space M" "P i x" then have "f x = i"
   547       unfolding f_def using unique by auto }
   548   note f_eq = this
   549   { fix x assume "x \<in> space M" "\<forall>i\<in>I. \<not> P i x"
   550     then have "\<And>i. \<not> P i x"
   551       using I(2)[of x] by auto
   552     then have "f x = undef"
   553       by (auto simp: undef_def f_def) }
   554   then have "f -` X \<inter> space M = (\<Union>i\<in>I \<inter> X. {x\<in>space M. P i x}) \<union>
   555      (if undef \<in> X then space M - (\<Union>i\<in>I. {x\<in>space M. P i x}) else {})"
   556     by (auto dest: f_eq)
   557   also have "\<dots> \<in> sets M"
   558     by (auto intro!: sets.Diff sets.countable_UN')
   559   finally show "f -` X \<inter> space M \<in> sets M" .
   560 qed simp
   561 
   562 lemma measurable_bot[measurable]: "Measurable.pred M bot"
   563   by (simp add: bot_fun_def)
   564 
   565 lemma measurable_top[measurable]: "Measurable.pred M top"
   566   by (simp add: top_fun_def)
   567 
   568 lemma measurable_Ex1[measurable (raw)]:
   569   assumes [simp]: "countable I" and [measurable]: "\<And>i. i \<in> I \<Longrightarrow> Measurable.pred M (P i)"
   570   shows "Measurable.pred M (\<lambda>x. \<exists>!i\<in>I. P i x)"
   571   unfolding bex1_def by measurable
   572 
   573 lemma measurable_split_if[measurable (raw)]:
   574   "(c \<Longrightarrow> Measurable.pred M f) \<Longrightarrow> (\<not> c \<Longrightarrow> Measurable.pred M g) \<Longrightarrow>
   575    Measurable.pred M (if c then f else g)"
   576   by simp
   577 
   578 lemma pred_restrict_space:
   579   assumes "S \<in> sets M"
   580   shows "Measurable.pred (restrict_space M S) P \<longleftrightarrow> Measurable.pred M (\<lambda>x. x \<in> S \<and> P x)"
   581   unfolding pred_def sets_Collect_restrict_space_iff[OF assms] ..
   582 
   583 lemma measurable_predpow[measurable]:
   584   assumes "Measurable.pred M T"
   585   assumes "\<And>Q. Measurable.pred M Q \<Longrightarrow> Measurable.pred M (R Q)"
   586   shows "Measurable.pred M ((R ^^ n) T)"
   587   by (induct n) (auto intro: assms)
   588 
   589 hide_const (open) pred
   590 
   591 end