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