src/HOL/Probability/Borel_Space.thy
author haftmann
Tue Oct 13 09:21:15 2015 +0200 (2015-10-13)
changeset 61424 c3658c18b7bc
parent 61284 2314c2f62eb1
child 61609 77b453bd616f
permissions -rw-r--r--
prod_case as canonical name for product type eliminator
     1 (*  Title:      HOL/Probability/Borel_Space.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Armin Heller, TU München
     4 *)
     5 
     6 section {*Borel spaces*}
     7 
     8 theory Borel_Space
     9 imports
    10   Measurable
    11   "~~/src/HOL/Multivariate_Analysis/Multivariate_Analysis"
    12 begin
    13 
    14 lemma topological_basis_trivial: "topological_basis {A. open A}"
    15   by (auto simp: topological_basis_def)
    16 
    17 lemma open_prod_generated: "open = generate_topology {A \<times> B | A B. open A \<and> open B}"
    18 proof -
    19   have "{A \<times> B :: ('a \<times> 'b) set | A B. open A \<and> open B} = ((\<lambda>(a, b). a \<times> b) ` ({A. open A} \<times> {A. open A}))"
    20     by auto
    21   then show ?thesis
    22     by (auto intro: topological_basis_prod topological_basis_trivial topological_basis_imp_subbasis)  
    23 qed
    24 
    25 subsection {* Generic Borel spaces *}
    26 
    27 definition borel :: "'a::topological_space measure" where
    28   "borel = sigma UNIV {S. open S}"
    29 
    30 abbreviation "borel_measurable M \<equiv> measurable M borel"
    31 
    32 lemma in_borel_measurable:
    33    "f \<in> borel_measurable M \<longleftrightarrow>
    34     (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
    35   by (auto simp add: measurable_def borel_def)
    36 
    37 lemma in_borel_measurable_borel:
    38    "f \<in> borel_measurable M \<longleftrightarrow>
    39     (\<forall>S \<in> sets borel.
    40       f -` S \<inter> space M \<in> sets M)"
    41   by (auto simp add: measurable_def borel_def)
    42 
    43 lemma space_borel[simp]: "space borel = UNIV"
    44   unfolding borel_def by auto
    45 
    46 lemma space_in_borel[measurable]: "UNIV \<in> sets borel"
    47   unfolding borel_def by auto
    48 
    49 lemma sets_borel: "sets borel = sigma_sets UNIV {S. open S}"
    50   unfolding borel_def by (rule sets_measure_of) simp
    51 
    52 lemma pred_Collect_borel[measurable (raw)]: "Measurable.pred borel P \<Longrightarrow> {x. P x} \<in> sets borel"
    53   unfolding borel_def pred_def by auto
    54 
    55 lemma borel_open[measurable (raw generic)]:
    56   assumes "open A" shows "A \<in> sets borel"
    57 proof -
    58   have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
    59   thus ?thesis unfolding borel_def by auto
    60 qed
    61 
    62 lemma borel_closed[measurable (raw generic)]:
    63   assumes "closed A" shows "A \<in> sets borel"
    64 proof -
    65   have "space borel - (- A) \<in> sets borel"
    66     using assms unfolding closed_def by (blast intro: borel_open)
    67   thus ?thesis by simp
    68 qed
    69 
    70 lemma borel_singleton[measurable]:
    71   "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets (borel :: 'a::t1_space measure)"
    72   unfolding insert_def by (rule sets.Un) auto
    73 
    74 lemma borel_comp[measurable]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
    75   unfolding Compl_eq_Diff_UNIV by simp
    76 
    77 lemma borel_measurable_vimage:
    78   fixes f :: "'a \<Rightarrow> 'x::t2_space"
    79   assumes borel[measurable]: "f \<in> borel_measurable M"
    80   shows "f -` {x} \<inter> space M \<in> sets M"
    81   by simp
    82 
    83 lemma borel_measurableI:
    84   fixes f :: "'a \<Rightarrow> 'x::topological_space"
    85   assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
    86   shows "f \<in> borel_measurable M"
    87   unfolding borel_def
    88 proof (rule measurable_measure_of, simp_all)
    89   fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
    90     using assms[of S] by simp
    91 qed
    92 
    93 lemma borel_measurable_const:
    94   "(\<lambda>x. c) \<in> borel_measurable M"
    95   by auto
    96 
    97 lemma borel_measurable_indicator:
    98   assumes A: "A \<in> sets M"
    99   shows "indicator A \<in> borel_measurable M"
   100   unfolding indicator_def [abs_def] using A
   101   by (auto intro!: measurable_If_set)
   102 
   103 lemma borel_measurable_count_space[measurable (raw)]:
   104   "f \<in> borel_measurable (count_space S)"
   105   unfolding measurable_def by auto
   106 
   107 lemma borel_measurable_indicator'[measurable (raw)]:
   108   assumes [measurable]: "{x\<in>space M. f x \<in> A x} \<in> sets M"
   109   shows "(\<lambda>x. indicator (A x) (f x)) \<in> borel_measurable M"
   110   unfolding indicator_def[abs_def]
   111   by (auto intro!: measurable_If)
   112 
   113 lemma borel_measurable_indicator_iff:
   114   "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
   115     (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
   116 proof
   117   assume "?I \<in> borel_measurable M"
   118   then have "?I -` {1} \<inter> space M \<in> sets M"
   119     unfolding measurable_def by auto
   120   also have "?I -` {1} \<inter> space M = A \<inter> space M"
   121     unfolding indicator_def [abs_def] by auto
   122   finally show "A \<inter> space M \<in> sets M" .
   123 next
   124   assume "A \<inter> space M \<in> sets M"
   125   moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   126     (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   127     by (intro measurable_cong) (auto simp: indicator_def)
   128   ultimately show "?I \<in> borel_measurable M" by auto
   129 qed
   130 
   131 lemma borel_measurable_subalgebra:
   132   assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   133   shows "f \<in> borel_measurable M"
   134   using assms unfolding measurable_def by auto
   135 
   136 lemma borel_measurable_restrict_space_iff_ereal:
   137   fixes f :: "'a \<Rightarrow> ereal"
   138   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   139   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   140     (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
   141   by (subst measurable_restrict_space_iff)
   142      (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_cong)
   143 
   144 lemma borel_measurable_restrict_space_iff:
   145   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   146   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   147   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   148     (\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> borel_measurable M"
   149   by (subst measurable_restrict_space_iff)
   150      (auto simp: indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a] ac_simps cong del: if_cong)
   151 
   152 lemma cbox_borel[measurable]: "cbox a b \<in> sets borel"
   153   by (auto intro: borel_closed)
   154 
   155 lemma box_borel[measurable]: "box a b \<in> sets borel"
   156   by (auto intro: borel_open)
   157 
   158 lemma borel_compact: "compact (A::'a::t2_space set) \<Longrightarrow> A \<in> sets borel"
   159   by (auto intro: borel_closed dest!: compact_imp_closed)
   160 
   161 lemma second_countable_borel_measurable:
   162   fixes X :: "'a::second_countable_topology set set"
   163   assumes eq: "open = generate_topology X"
   164   shows "borel = sigma UNIV X"
   165   unfolding borel_def
   166 proof (intro sigma_eqI sigma_sets_eqI)
   167   interpret X: sigma_algebra UNIV "sigma_sets UNIV X"
   168     by (rule sigma_algebra_sigma_sets) simp
   169 
   170   fix S :: "'a set" assume "S \<in> Collect open"
   171   then have "generate_topology X S"
   172     by (auto simp: eq)
   173   then show "S \<in> sigma_sets UNIV X"
   174   proof induction
   175     case (UN K)
   176     then have K: "\<And>k. k \<in> K \<Longrightarrow> open k"
   177       unfolding eq by auto
   178     from ex_countable_basis obtain B :: "'a set set" where
   179       B:  "\<And>b. b \<in> B \<Longrightarrow> open b" "\<And>X. open X \<Longrightarrow> \<exists>b\<subseteq>B. (\<Union>b) = X" and "countable B"
   180       by (auto simp: topological_basis_def)
   181     from B(2)[OF K] obtain m where m: "\<And>k. k \<in> K \<Longrightarrow> m k \<subseteq> B" "\<And>k. k \<in> K \<Longrightarrow> (\<Union>m k) = k"
   182       by metis
   183     def U \<equiv> "(\<Union>k\<in>K. m k)"
   184     with m have "countable U"
   185       by (intro countable_subset[OF _ `countable B`]) auto
   186     have "\<Union>U = (\<Union>A\<in>U. A)" by simp
   187     also have "\<dots> = \<Union>K"
   188       unfolding U_def UN_simps by (simp add: m)
   189     finally have "\<Union>U = \<Union>K" .
   190 
   191     have "\<forall>b\<in>U. \<exists>k\<in>K. b \<subseteq> k"
   192       using m by (auto simp: U_def)
   193     then obtain u where u: "\<And>b. b \<in> U \<Longrightarrow> u b \<in> K" and "\<And>b. b \<in> U \<Longrightarrow> b \<subseteq> u b"
   194       by metis
   195     then have "(\<Union>b\<in>U. u b) \<subseteq> \<Union>K" "\<Union>U \<subseteq> (\<Union>b\<in>U. u b)"
   196       by auto
   197     then have "\<Union>K = (\<Union>b\<in>U. u b)"
   198       unfolding `\<Union>U = \<Union>K` by auto
   199     also have "\<dots> \<in> sigma_sets UNIV X"
   200       using u UN by (intro X.countable_UN' `countable U`) auto
   201     finally show "\<Union>K \<in> sigma_sets UNIV X" .
   202   qed auto
   203 qed (auto simp: eq intro: generate_topology.Basis)
   204 
   205 lemma borel_measurable_continuous_on_restrict:
   206   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   207   assumes f: "continuous_on A f"
   208   shows "f \<in> borel_measurable (restrict_space borel A)"
   209 proof (rule borel_measurableI)
   210   fix S :: "'b set" assume "open S"
   211   with f obtain T where "f -` S \<inter> A = T \<inter> A" "open T"
   212     by (metis continuous_on_open_invariant)
   213   then show "f -` S \<inter> space (restrict_space borel A) \<in> sets (restrict_space borel A)"
   214     by (force simp add: sets_restrict_space space_restrict_space)
   215 qed
   216 
   217 lemma borel_measurable_continuous_on1: "continuous_on UNIV f \<Longrightarrow> f \<in> borel_measurable borel"
   218   by (drule borel_measurable_continuous_on_restrict) simp
   219 
   220 lemma borel_measurable_continuous_on_if:
   221   "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> continuous_on (- A) g \<Longrightarrow>
   222     (\<lambda>x. if x \<in> A then f x else g x) \<in> borel_measurable borel"
   223   by (auto simp add: measurable_If_restrict_space_iff Collect_neg_eq
   224            intro!: borel_measurable_continuous_on_restrict)
   225 
   226 lemma borel_measurable_continuous_countable_exceptions:
   227   fixes f :: "'a::t1_space \<Rightarrow> 'b::topological_space"
   228   assumes X: "countable X"
   229   assumes "continuous_on (- X) f"
   230   shows "f \<in> borel_measurable borel"
   231 proof (rule measurable_discrete_difference[OF _ X])
   232   have "X \<in> sets borel"
   233     by (rule sets.countable[OF _ X]) auto
   234   then show "(\<lambda>x. if x \<in> X then undefined else f x) \<in> borel_measurable borel"
   235     by (intro borel_measurable_continuous_on_if assms continuous_intros)
   236 qed auto
   237 
   238 lemma borel_measurable_continuous_on:
   239   assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
   240   shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
   241   using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
   242 
   243 lemma borel_measurable_continuous_on_indicator:
   244   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
   245   shows "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> (\<lambda>x. indicator A x *\<^sub>R f x) \<in> borel_measurable borel"
   246   by (subst borel_measurable_restrict_space_iff[symmetric])
   247      (auto intro: borel_measurable_continuous_on_restrict)
   248 
   249 lemma borel_eq_countable_basis:
   250   fixes B::"'a::topological_space set set"
   251   assumes "countable B"
   252   assumes "topological_basis B"
   253   shows "borel = sigma UNIV B"
   254   unfolding borel_def
   255 proof (intro sigma_eqI sigma_sets_eqI, safe)
   256   interpret countable_basis using assms by unfold_locales
   257   fix X::"'a set" assume "open X"
   258   from open_countable_basisE[OF this] guess B' . note B' = this
   259   then show "X \<in> sigma_sets UNIV B"
   260     by (blast intro: sigma_sets_UNION `countable B` countable_subset)
   261 next
   262   fix b assume "b \<in> B"
   263   hence "open b" by (rule topological_basis_open[OF assms(2)])
   264   thus "b \<in> sigma_sets UNIV (Collect open)" by auto
   265 qed simp_all
   266 
   267 lemma borel_measurable_Pair[measurable (raw)]:
   268   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   269   assumes f[measurable]: "f \<in> borel_measurable M"
   270   assumes g[measurable]: "g \<in> borel_measurable M"
   271   shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
   272 proof (subst borel_eq_countable_basis)
   273   let ?B = "SOME B::'b set set. countable B \<and> topological_basis B"
   274   let ?C = "SOME B::'c set set. countable B \<and> topological_basis B"
   275   let ?P = "(\<lambda>(b, c). b \<times> c) ` (?B \<times> ?C)"
   276   show "countable ?P" "topological_basis ?P"
   277     by (auto intro!: countable_basis topological_basis_prod is_basis)
   278 
   279   show "(\<lambda>x. (f x, g x)) \<in> measurable M (sigma UNIV ?P)"
   280   proof (rule measurable_measure_of)
   281     fix S assume "S \<in> ?P"
   282     then obtain b c where "b \<in> ?B" "c \<in> ?C" and S: "S = b \<times> c" by auto
   283     then have borel: "open b" "open c"
   284       by (auto intro: is_basis topological_basis_open)
   285     have "(\<lambda>x. (f x, g x)) -` S \<inter> space M = (f -` b \<inter> space M) \<inter> (g -` c \<inter> space M)"
   286       unfolding S by auto
   287     also have "\<dots> \<in> sets M"
   288       using borel by simp
   289     finally show "(\<lambda>x. (f x, g x)) -` S \<inter> space M \<in> sets M" .
   290   qed auto
   291 qed
   292 
   293 lemma borel_measurable_continuous_Pair:
   294   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   295   assumes [measurable]: "f \<in> borel_measurable M"
   296   assumes [measurable]: "g \<in> borel_measurable M"
   297   assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
   298   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   299 proof -
   300   have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
   301   show ?thesis
   302     unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
   303 qed
   304 
   305 subsection {* Borel spaces on order topologies *}
   306 
   307 
   308 lemma borel_Iio:
   309   "borel = sigma UNIV (range lessThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   310   unfolding second_countable_borel_measurable[OF open_generated_order]
   311 proof (intro sigma_eqI sigma_sets_eqI)
   312   from countable_dense_setE guess D :: "'a set" . note D = this
   313 
   314   interpret L: sigma_algebra UNIV "sigma_sets UNIV (range lessThan)"
   315     by (rule sigma_algebra_sigma_sets) simp
   316 
   317   fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   318   then obtain y where "A = {y <..} \<or> A = {..< y}"
   319     by blast
   320   then show "A \<in> sigma_sets UNIV (range lessThan)"
   321   proof
   322     assume A: "A = {y <..}"
   323     show ?thesis
   324     proof cases
   325       assume "\<forall>x>y. \<exists>d. y < d \<and> d < x"
   326       with D(2)[of "{y <..< x}" for x] have "\<forall>x>y. \<exists>d\<in>D. y < d \<and> d < x"
   327         by (auto simp: set_eq_iff)
   328       then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. y < d}. {..< d})"
   329         by (auto simp: A) (metis less_asym)
   330       also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   331         using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   332       finally show ?thesis .
   333     next
   334       assume "\<not> (\<forall>x>y. \<exists>d. y < d \<and> d < x)"
   335       then obtain x where "y < x"  "\<And>d. y < d \<Longrightarrow> \<not> d < x"
   336         by auto
   337       then have "A = UNIV - {..< x}"
   338         unfolding A by (auto simp: not_less[symmetric])
   339       also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   340         by auto
   341       finally show ?thesis .
   342     qed
   343   qed auto
   344 qed auto
   345 
   346 lemma borel_Ioi:
   347   "borel = sigma UNIV (range greaterThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   348   unfolding second_countable_borel_measurable[OF open_generated_order]
   349 proof (intro sigma_eqI sigma_sets_eqI)
   350   from countable_dense_setE guess D :: "'a set" . note D = this
   351 
   352   interpret L: sigma_algebra UNIV "sigma_sets UNIV (range greaterThan)"
   353     by (rule sigma_algebra_sigma_sets) simp
   354 
   355   fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   356   then obtain y where "A = {y <..} \<or> A = {..< y}"
   357     by blast
   358   then show "A \<in> sigma_sets UNIV (range greaterThan)"
   359   proof
   360     assume A: "A = {..< y}"
   361     show ?thesis
   362     proof cases
   363       assume "\<forall>x<y. \<exists>d. x < d \<and> d < y"
   364       with D(2)[of "{x <..< y}" for x] have "\<forall>x<y. \<exists>d\<in>D. x < d \<and> d < y"
   365         by (auto simp: set_eq_iff)
   366       then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. d < y}. {d <..})"
   367         by (auto simp: A) (metis less_asym)
   368       also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   369         using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   370       finally show ?thesis .
   371     next
   372       assume "\<not> (\<forall>x<y. \<exists>d. x < d \<and> d < y)"
   373       then obtain x where "x < y"  "\<And>d. y > d \<Longrightarrow> x \<ge> d"
   374         by (auto simp: not_less[symmetric])
   375       then have "A = UNIV - {x <..}"
   376         unfolding A Compl_eq_Diff_UNIV[symmetric] by auto
   377       also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   378         by auto
   379       finally show ?thesis .
   380     qed
   381   qed auto
   382 qed auto
   383 
   384 lemma borel_measurableI_less:
   385   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   386   shows "(\<And>y. {x\<in>space M. f x < y} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   387   unfolding borel_Iio
   388   by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   389 
   390 lemma borel_measurableI_greater:
   391   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   392   shows "(\<And>y. {x\<in>space M. y < f x} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   393   unfolding borel_Ioi
   394   by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   395 
   396 lemma borel_measurable_SUP[measurable (raw)]:
   397   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   398   assumes [simp]: "countable I"
   399   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   400   shows "(\<lambda>x. SUP i:I. F i x) \<in> borel_measurable M"
   401   by (rule borel_measurableI_greater) (simp add: less_SUP_iff)
   402 
   403 lemma borel_measurable_INF[measurable (raw)]:
   404   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   405   assumes [simp]: "countable I"
   406   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   407   shows "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable M"
   408   by (rule borel_measurableI_less) (simp add: INF_less_iff)
   409 
   410 lemma borel_measurable_lfp[consumes 1, case_names continuity step]:
   411   fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   412   assumes "sup_continuous F"
   413   assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   414   shows "lfp F \<in> borel_measurable M"
   415 proof -
   416   { fix i have "((F ^^ i) bot) \<in> borel_measurable M"
   417       by (induct i) (auto intro!: *) }
   418   then have "(\<lambda>x. SUP i. (F ^^ i) bot x) \<in> borel_measurable M"
   419     by measurable
   420   also have "(\<lambda>x. SUP i. (F ^^ i) bot x) = (SUP i. (F ^^ i) bot)"
   421     by auto
   422   also have "(SUP i. (F ^^ i) bot) = lfp F"
   423     by (rule sup_continuous_lfp[symmetric]) fact
   424   finally show ?thesis .
   425 qed
   426 
   427 lemma borel_measurable_gfp[consumes 1, case_names continuity step]:
   428   fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   429   assumes "inf_continuous F"
   430   assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   431   shows "gfp F \<in> borel_measurable M"
   432 proof -
   433   { fix i have "((F ^^ i) top) \<in> borel_measurable M"
   434       by (induct i) (auto intro!: * simp: bot_fun_def) }
   435   then have "(\<lambda>x. INF i. (F ^^ i) top x) \<in> borel_measurable M"
   436     by measurable
   437   also have "(\<lambda>x. INF i. (F ^^ i) top x) = (INF i. (F ^^ i) top)"
   438     by auto
   439   also have "\<dots> = gfp F"
   440     by (rule inf_continuous_gfp[symmetric]) fact
   441   finally show ?thesis .
   442 qed
   443 
   444 subsection {* Borel spaces on euclidean spaces *}
   445 
   446 lemma borel_measurable_inner[measurable (raw)]:
   447   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_inner}"
   448   assumes "f \<in> borel_measurable M"
   449   assumes "g \<in> borel_measurable M"
   450   shows "(\<lambda>x. f x \<bullet> g x) \<in> borel_measurable M"
   451   using assms
   452   by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   453 
   454 lemma [measurable]:
   455   fixes a b :: "'a::linorder_topology"
   456   shows lessThan_borel: "{..< a} \<in> sets borel"
   457     and greaterThan_borel: "{a <..} \<in> sets borel"
   458     and greaterThanLessThan_borel: "{a<..<b} \<in> sets borel"
   459     and atMost_borel: "{..a} \<in> sets borel"
   460     and atLeast_borel: "{a..} \<in> sets borel"
   461     and atLeastAtMost_borel: "{a..b} \<in> sets borel"
   462     and greaterThanAtMost_borel: "{a<..b} \<in> sets borel"
   463     and atLeastLessThan_borel: "{a..<b} \<in> sets borel"
   464   unfolding greaterThanAtMost_def atLeastLessThan_def
   465   by (blast intro: borel_open borel_closed open_lessThan open_greaterThan open_greaterThanLessThan
   466                    closed_atMost closed_atLeast closed_atLeastAtMost)+
   467 
   468 notation
   469   eucl_less (infix "<e" 50)
   470 
   471 lemma box_oc: "{x. a <e x \<and> x \<le> b} = {x. a <e x} \<inter> {..b}"
   472   and box_co: "{x. a \<le> x \<and> x <e b} = {a..} \<inter> {x. x <e b}"
   473   by auto
   474 
   475 lemma eucl_ivals[measurable]:
   476   fixes a b :: "'a::ordered_euclidean_space"
   477   shows "{x. x <e a} \<in> sets borel"
   478     and "{x. a <e x} \<in> sets borel"
   479     and "{..a} \<in> sets borel"
   480     and "{a..} \<in> sets borel"
   481     and "{a..b} \<in> sets borel"
   482     and  "{x. a <e x \<and> x \<le> b} \<in> sets borel"
   483     and "{x. a \<le> x \<and>  x <e b} \<in> sets borel"
   484   unfolding box_oc box_co
   485   by (auto intro: borel_open borel_closed)
   486 
   487 lemma open_Collect_less:
   488   fixes f g :: "'i::topological_space \<Rightarrow> 'a :: {dense_linorder, linorder_topology}"
   489   assumes "continuous_on UNIV f"
   490   assumes "continuous_on UNIV g"
   491   shows "open {x. f x < g x}"
   492 proof -
   493   have "open (\<Union>y. {x \<in> UNIV. f x \<in> {..< y}} \<inter> {x \<in> UNIV. g x \<in> {y <..}})" (is "open ?X")
   494     by (intro open_UN ballI open_Int continuous_open_preimage assms) auto
   495   also have "?X = {x. f x < g x}"
   496     by (auto intro: dense)
   497   finally show ?thesis .
   498 qed
   499 
   500 lemma closed_Collect_le:
   501   fixes f g :: "'i::topological_space \<Rightarrow> 'a :: {dense_linorder, linorder_topology}"
   502   assumes f: "continuous_on UNIV f"
   503   assumes g: "continuous_on UNIV g"
   504   shows "closed {x. f x \<le> g x}"
   505   using open_Collect_less[OF g f] unfolding not_less[symmetric] Collect_neg_eq open_closed .
   506 
   507 lemma borel_measurable_less[measurable]:
   508   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, dense_linorder, linorder_topology}"
   509   assumes "f \<in> borel_measurable M"
   510   assumes "g \<in> borel_measurable M"
   511   shows "{w \<in> space M. f w < g w} \<in> sets M"
   512 proof -
   513   have "{w \<in> space M. f w < g w} = (\<lambda>x. (f x, g x)) -` {x. fst x < snd x} \<inter> space M"
   514     by auto
   515   also have "\<dots> \<in> sets M"
   516     by (intro measurable_sets[OF borel_measurable_Pair borel_open, OF assms open_Collect_less]
   517               continuous_intros)
   518   finally show ?thesis .
   519 qed
   520 
   521 lemma
   522   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, dense_linorder, linorder_topology}"
   523   assumes f[measurable]: "f \<in> borel_measurable M"
   524   assumes g[measurable]: "g \<in> borel_measurable M"
   525   shows borel_measurable_le[measurable]: "{w \<in> space M. f w \<le> g w} \<in> sets M"
   526     and borel_measurable_eq[measurable]: "{w \<in> space M. f w = g w} \<in> sets M"
   527     and borel_measurable_neq: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   528   unfolding eq_iff not_less[symmetric]
   529   by measurable
   530 
   531 lemma 
   532   fixes i :: "'a::{second_countable_topology, real_inner}"
   533   shows hafspace_less_borel: "{x. a < x \<bullet> i} \<in> sets borel"
   534     and hafspace_greater_borel: "{x. x \<bullet> i < a} \<in> sets borel"
   535     and hafspace_less_eq_borel: "{x. a \<le> x \<bullet> i} \<in> sets borel"
   536     and hafspace_greater_eq_borel: "{x. x \<bullet> i \<le> a} \<in> sets borel"
   537   by simp_all
   538 
   539 subsection "Borel space equals sigma algebras over intervals"
   540 
   541 lemma borel_sigma_sets_subset:
   542   "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
   543   using sets.sigma_sets_subset[of A borel] by simp
   544 
   545 lemma borel_eq_sigmaI1:
   546   fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   547   assumes borel_eq: "borel = sigma UNIV X"
   548   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (F ` A))"
   549   assumes F: "\<And>i. i \<in> A \<Longrightarrow> F i \<in> sets borel"
   550   shows "borel = sigma UNIV (F ` A)"
   551   unfolding borel_def
   552 proof (intro sigma_eqI antisym)
   553   have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
   554     unfolding borel_def by simp
   555   also have "\<dots> = sigma_sets UNIV X"
   556     unfolding borel_eq by simp
   557   also have "\<dots> \<subseteq> sigma_sets UNIV (F`A)"
   558     using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
   559   finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (F`A)" .
   560   show "sigma_sets UNIV (F`A) \<subseteq> sigma_sets UNIV {S. open S}"
   561     unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
   562 qed auto
   563 
   564 lemma borel_eq_sigmaI2:
   565   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
   566     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   567   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`B)"
   568   assumes X: "\<And>i j. (i, j) \<in> B \<Longrightarrow> G i j \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   569   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   570   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   571   using assms
   572   by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` B" and F="(\<lambda>(i, j). F i j)"]) auto
   573 
   574 lemma borel_eq_sigmaI3:
   575   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   576   assumes borel_eq: "borel = sigma UNIV X"
   577   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   578   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   579   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   580   using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
   581 
   582 lemma borel_eq_sigmaI4:
   583   fixes F :: "'i \<Rightarrow> 'a::topological_space set"
   584     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   585   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`A)"
   586   assumes X: "\<And>i j. (i, j) \<in> A \<Longrightarrow> G i j \<in> sets (sigma UNIV (range F))"
   587   assumes F: "\<And>i. F i \<in> sets borel"
   588   shows "borel = sigma UNIV (range F)"
   589   using assms by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` A" and F=F]) auto
   590 
   591 lemma borel_eq_sigmaI5:
   592   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
   593   assumes borel_eq: "borel = sigma UNIV (range G)"
   594   assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   595   assumes F: "\<And>i j. F i j \<in> sets borel"
   596   shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   597   using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
   598 
   599 lemma borel_eq_box:
   600   "borel = sigma UNIV (range (\<lambda> (a, b). box a b :: 'a :: euclidean_space set))"
   601     (is "_ = ?SIGMA")
   602 proof (rule borel_eq_sigmaI1[OF borel_def])
   603   fix M :: "'a set" assume "M \<in> {S. open S}"
   604   then have "open M" by simp
   605   show "M \<in> ?SIGMA"
   606     apply (subst open_UNION_box[OF `open M`])
   607     apply (safe intro!: sets.countable_UN' countable_PiE countable_Collect)
   608     apply (auto intro: countable_rat)
   609     done
   610 qed (auto simp: box_def)
   611 
   612 lemma halfspace_gt_in_halfspace:
   613   assumes i: "i \<in> A"
   614   shows "{x::'a. a < x \<bullet> i} \<in> 
   615     sigma_sets UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> A))"
   616   (is "?set \<in> ?SIGMA")
   617 proof -
   618   interpret sigma_algebra UNIV ?SIGMA
   619     by (intro sigma_algebra_sigma_sets) simp_all
   620   have *: "?set = (\<Union>n. UNIV - {x::'a. x \<bullet> i < a + 1 / real (Suc n)})"
   621   proof (safe, simp_all add: not_less del: real_of_nat_Suc)
   622     fix x :: 'a assume "a < x \<bullet> i"
   623     with reals_Archimedean[of "x \<bullet> i - a"]
   624     obtain n where "a + 1 / real (Suc n) < x \<bullet> i"
   625       by (auto simp: field_simps)
   626     then show "\<exists>n. a + 1 / real (Suc n) \<le> x \<bullet> i"
   627       by (blast intro: less_imp_le)
   628   next
   629     fix x n
   630     have "a < a + 1 / real (Suc n)" by auto
   631     also assume "\<dots> \<le> x"
   632     finally show "a < x" .
   633   qed
   634   show "?set \<in> ?SIGMA" unfolding *
   635     by (auto intro!: Diff sigma_sets_Inter i)
   636 qed
   637 
   638 lemma borel_eq_halfspace_less:
   639   "borel = sigma UNIV ((\<lambda>(a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> Basis))"
   640   (is "_ = ?SIGMA")
   641 proof (rule borel_eq_sigmaI2[OF borel_eq_box])
   642   fix a b :: 'a
   643   have "box a b = {x\<in>space ?SIGMA. \<forall>i\<in>Basis. a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i}"
   644     by (auto simp: box_def)
   645   also have "\<dots> \<in> sets ?SIGMA"
   646     by (intro sets.sets_Collect_conj sets.sets_Collect_finite_All sets.sets_Collect_const)
   647        (auto intro!: halfspace_gt_in_halfspace countable_PiE countable_rat)
   648   finally show "box a b \<in> sets ?SIGMA" .
   649 qed auto
   650 
   651 lemma borel_eq_halfspace_le:
   652   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i \<le> a}) ` (UNIV \<times> Basis))"
   653   (is "_ = ?SIGMA")
   654 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   655   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   656   then have i: "i \<in> Basis" by auto
   657   have *: "{x::'a. x\<bullet>i < a} = (\<Union>n. {x. x\<bullet>i \<le> a - 1/real (Suc n)})"
   658   proof (safe, simp_all del: real_of_nat_Suc)
   659     fix x::'a assume *: "x\<bullet>i < a"
   660     with reals_Archimedean[of "a - x\<bullet>i"]
   661     obtain n where "x \<bullet> i < a - 1 / (real (Suc n))"
   662       by (auto simp: field_simps)
   663     then show "\<exists>n. x \<bullet> i \<le> a - 1 / (real (Suc n))"
   664       by (blast intro: less_imp_le)
   665   next
   666     fix x::'a and n
   667     assume "x\<bullet>i \<le> a - 1 / real (Suc n)"
   668     also have "\<dots> < a" by auto
   669     finally show "x\<bullet>i < a" .
   670   qed
   671   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
   672     by (intro sets.countable_UN) (auto intro: i)
   673 qed auto
   674 
   675 lemma borel_eq_halfspace_ge:
   676   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a \<le> x \<bullet> i}) ` (UNIV \<times> Basis))"
   677   (is "_ = ?SIGMA")
   678 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   679   fix a :: real and i :: 'a assume i: "(a, i) \<in> UNIV \<times> Basis"
   680   have *: "{x::'a. x\<bullet>i < a} = space ?SIGMA - {x::'a. a \<le> x\<bullet>i}" by auto
   681   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
   682     using i by (intro sets.compl_sets) auto
   683 qed auto
   684 
   685 lemma borel_eq_halfspace_greater:
   686   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a < x \<bullet> i}) ` (UNIV \<times> Basis))"
   687   (is "_ = ?SIGMA")
   688 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
   689   fix a :: real and i :: 'a assume "(a, i) \<in> (UNIV \<times> Basis)"
   690   then have i: "i \<in> Basis" by auto
   691   have *: "{x::'a. x\<bullet>i \<le> a} = space ?SIGMA - {x::'a. a < x\<bullet>i}" by auto
   692   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
   693     by (intro sets.compl_sets) (auto intro: i)
   694 qed auto
   695 
   696 lemma borel_eq_atMost:
   697   "borel = sigma UNIV (range (\<lambda>a. {..a::'a::ordered_euclidean_space}))"
   698   (is "_ = ?SIGMA")
   699 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   700   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   701   then have "i \<in> Basis" by auto
   702   then have *: "{x::'a. x\<bullet>i \<le> a} = (\<Union>k::nat. {.. (\<Sum>n\<in>Basis. (if n = i then a else real k)*\<^sub>R n)})"
   703   proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   704     fix x :: 'a
   705     from real_arch_simple[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"] guess k::nat ..
   706     then have "\<And>i. i \<in> Basis \<Longrightarrow> x\<bullet>i \<le> real k"
   707       by (subst (asm) Max_le_iff) auto
   708     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia \<le> real k"
   709       by (auto intro!: exI[of _ k])
   710   qed
   711   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
   712     by (intro sets.countable_UN) auto
   713 qed auto
   714 
   715 lemma borel_eq_greaterThan:
   716   "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. a <e x}))"
   717   (is "_ = ?SIGMA")
   718 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   719   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   720   then have i: "i \<in> Basis" by auto
   721   have "{x::'a. x\<bullet>i \<le> a} = UNIV - {x::'a. a < x\<bullet>i}" by auto
   722   also have *: "{x::'a. a < x\<bullet>i} =
   723       (\<Union>k::nat. {x. (\<Sum>n\<in>Basis. (if n = i then a else -real k) *\<^sub>R n) <e x})" using i
   724   proof (safe, simp_all add: eucl_less_def split: split_if_asm)
   725     fix x :: 'a
   726     from reals_Archimedean2[of "Max ((\<lambda>i. -x\<bullet>i)`Basis)"]
   727     guess k::nat .. note k = this
   728     { fix i :: 'a assume "i \<in> Basis"
   729       then have "-x\<bullet>i < real k"
   730         using k by (subst (asm) Max_less_iff) auto
   731       then have "- real k < x\<bullet>i" by simp }
   732     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> -real k < x \<bullet> ia"
   733       by (auto intro!: exI[of _ k])
   734   qed
   735   finally show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA"
   736     apply (simp only:)
   737     apply (intro sets.countable_UN sets.Diff)
   738     apply (auto intro: sigma_sets_top)
   739     done
   740 qed auto
   741 
   742 lemma borel_eq_lessThan:
   743   "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. x <e a}))"
   744   (is "_ = ?SIGMA")
   745 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
   746   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   747   then have i: "i \<in> Basis" by auto
   748   have "{x::'a. a \<le> x\<bullet>i} = UNIV - {x::'a. x\<bullet>i < a}" by auto
   749   also have *: "{x::'a. x\<bullet>i < a} = (\<Union>k::nat. {x. x <e (\<Sum>n\<in>Basis. (if n = i then a else real k) *\<^sub>R n)})" using `i\<in> Basis`
   750   proof (safe, simp_all add: eucl_less_def split: split_if_asm)
   751     fix x :: 'a
   752     from reals_Archimedean2[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"]
   753     guess k::nat .. note k = this
   754     { fix i :: 'a assume "i \<in> Basis"
   755       then have "x\<bullet>i < real k"
   756         using k by (subst (asm) Max_less_iff) auto
   757       then have "x\<bullet>i < real k" by simp }
   758     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia < real k"
   759       by (auto intro!: exI[of _ k])
   760   qed
   761   finally show "{x. a \<le> x\<bullet>i} \<in> ?SIGMA"
   762     apply (simp only:)
   763     apply (intro sets.countable_UN sets.Diff)
   764     apply (auto intro: sigma_sets_top )
   765     done
   766 qed auto
   767 
   768 lemma borel_eq_atLeastAtMost:
   769   "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} ::'a::ordered_euclidean_space set))"
   770   (is "_ = ?SIGMA")
   771 proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
   772   fix a::'a
   773   have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   774   proof (safe, simp_all add: eucl_le[where 'a='a])
   775     fix x :: 'a
   776     from real_arch_simple[of "Max ((\<lambda>i. - x\<bullet>i)`Basis)"]
   777     guess k::nat .. note k = this
   778     { fix i :: 'a assume "i \<in> Basis"
   779       with k have "- x\<bullet>i \<le> real k"
   780         by (subst (asm) Max_le_iff) (auto simp: field_simps)
   781       then have "- real k \<le> x\<bullet>i" by simp }
   782     then show "\<exists>n::nat. \<forall>i\<in>Basis. - real n \<le> x \<bullet> i"
   783       by (auto intro!: exI[of _ k])
   784   qed
   785   show "{..a} \<in> ?SIGMA" unfolding *
   786     by (intro sets.countable_UN)
   787        (auto intro!: sigma_sets_top)
   788 qed auto
   789 
   790 lemma borel_sigma_sets_Ioc: "borel = sigma UNIV (range (\<lambda>(a, b). {a <.. b::real}))"
   791 proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
   792   fix i :: real
   793   have "{..i} = (\<Union>j::nat. {-j <.. i})"
   794     by (auto simp: minus_less_iff reals_Archimedean2)
   795   also have "\<dots> \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))"
   796     by (intro sets.countable_nat_UN) auto 
   797   finally show "{..i} \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))" .
   798 qed simp
   799 
   800 lemma eucl_lessThan: "{x::real. x <e a} = lessThan a"
   801   by (simp add: eucl_less_def lessThan_def)
   802 
   803 lemma borel_eq_atLeastLessThan:
   804   "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
   805 proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
   806   have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
   807   fix x :: real
   808   have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
   809     by (auto simp: move_uminus real_arch_simple)
   810   then show "{y. y <e x} \<in> ?SIGMA"
   811     by (auto intro: sigma_sets.intros(2-) simp: eucl_lessThan)
   812 qed auto
   813 
   814 lemma borel_eq_closed: "borel = sigma UNIV (Collect closed)"
   815   unfolding borel_def
   816 proof (intro sigma_eqI sigma_sets_eqI, safe)
   817   fix x :: "'a set" assume "open x"
   818   hence "x = UNIV - (UNIV - x)" by auto
   819   also have "\<dots> \<in> sigma_sets UNIV (Collect closed)"
   820     by (force intro: sigma_sets.Compl simp: `open x`)
   821   finally show "x \<in> sigma_sets UNIV (Collect closed)" by simp
   822 next
   823   fix x :: "'a set" assume "closed x"
   824   hence "x = UNIV - (UNIV - x)" by auto
   825   also have "\<dots> \<in> sigma_sets UNIV (Collect open)"
   826     by (force intro: sigma_sets.Compl simp: `closed x`)
   827   finally show "x \<in> sigma_sets UNIV (Collect open)" by simp
   828 qed simp_all
   829 
   830 lemma borel_measurable_halfspacesI:
   831   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
   832   assumes F: "borel = sigma UNIV (F ` (UNIV \<times> Basis))"
   833   and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M" 
   834   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a::real. S a i \<in> sets M)"
   835 proof safe
   836   fix a :: real and i :: 'b assume i: "i \<in> Basis" and f: "f \<in> borel_measurable M"
   837   then show "S a i \<in> sets M" unfolding assms
   838     by (auto intro!: measurable_sets simp: assms(1))
   839 next
   840   assume a: "\<forall>i\<in>Basis. \<forall>a. S a i \<in> sets M"
   841   then show "f \<in> borel_measurable M"
   842     by (auto intro!: measurable_measure_of simp: S_eq F)
   843 qed
   844 
   845 lemma borel_measurable_iff_halfspace_le:
   846   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
   847   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. f w \<bullet> i \<le> a} \<in> sets M)"
   848   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   849 
   850 lemma borel_measurable_iff_halfspace_less:
   851   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
   852   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. f w \<bullet> i < a} \<in> sets M)"
   853   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   854 
   855 lemma borel_measurable_iff_halfspace_ge:
   856   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
   857   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. a \<le> f w \<bullet> i} \<in> sets M)"
   858   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   859 
   860 lemma borel_measurable_iff_halfspace_greater:
   861   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
   862   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. a < f w \<bullet> i} \<in> sets M)"
   863   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
   864 
   865 lemma borel_measurable_iff_le:
   866   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   867   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   868 
   869 lemma borel_measurable_iff_less:
   870   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   871   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   872 
   873 lemma borel_measurable_iff_ge:
   874   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   875   using borel_measurable_iff_halfspace_ge[where 'c=real]
   876   by simp
   877 
   878 lemma borel_measurable_iff_greater:
   879   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   880   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   881 
   882 lemma borel_measurable_euclidean_space:
   883   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
   884   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M)"
   885 proof safe
   886   assume f: "\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M"
   887   then show "f \<in> borel_measurable M"
   888     by (subst borel_measurable_iff_halfspace_le) auto
   889 qed auto
   890 
   891 subsection "Borel measurable operators"
   892 
   893 lemma borel_measurable_norm[measurable]: "norm \<in> borel_measurable borel"
   894   by (intro borel_measurable_continuous_on1 continuous_intros)
   895 
   896 lemma borel_measurable_sgn [measurable]: "(sgn::'a::real_normed_vector \<Rightarrow> 'a) \<in> borel_measurable borel"
   897   by (rule borel_measurable_continuous_countable_exceptions[where X="{0}"])
   898      (auto intro!: continuous_on_sgn continuous_on_id)
   899 
   900 lemma borel_measurable_uminus[measurable (raw)]:
   901   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   902   assumes g: "g \<in> borel_measurable M"
   903   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   904   by (rule borel_measurable_continuous_on[OF _ g]) (intro continuous_intros)
   905 
   906 lemma borel_measurable_add[measurable (raw)]:
   907   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   908   assumes f: "f \<in> borel_measurable M"
   909   assumes g: "g \<in> borel_measurable M"
   910   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   911   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   912 
   913 lemma borel_measurable_setsum[measurable (raw)]:
   914   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   915   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   916   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   917 proof cases
   918   assume "finite S"
   919   thus ?thesis using assms by induct auto
   920 qed simp
   921 
   922 lemma borel_measurable_diff[measurable (raw)]:
   923   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   924   assumes f: "f \<in> borel_measurable M"
   925   assumes g: "g \<in> borel_measurable M"
   926   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   927   using borel_measurable_add [of f M "- g"] assms by (simp add: fun_Compl_def)
   928 
   929 lemma borel_measurable_times[measurable (raw)]:
   930   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_algebra}"
   931   assumes f: "f \<in> borel_measurable M"
   932   assumes g: "g \<in> borel_measurable M"
   933   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   934   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   935 
   936 lemma borel_measurable_setprod[measurable (raw)]:
   937   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_field}"
   938   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   939   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   940 proof cases
   941   assume "finite S"
   942   thus ?thesis using assms by induct auto
   943 qed simp
   944 
   945 lemma borel_measurable_dist[measurable (raw)]:
   946   fixes g f :: "'a \<Rightarrow> 'b::{second_countable_topology, metric_space}"
   947   assumes f: "f \<in> borel_measurable M"
   948   assumes g: "g \<in> borel_measurable M"
   949   shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
   950   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   951   
   952 lemma borel_measurable_scaleR[measurable (raw)]:
   953   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   954   assumes f: "f \<in> borel_measurable M"
   955   assumes g: "g \<in> borel_measurable M"
   956   shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
   957   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   958 
   959 lemma affine_borel_measurable_vector:
   960   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   961   assumes "f \<in> borel_measurable M"
   962   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   963 proof (rule borel_measurableI)
   964   fix S :: "'x set" assume "open S"
   965   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   966   proof cases
   967     assume "b \<noteq> 0"
   968     with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
   969       using open_affinity [of S "inverse b" "- a /\<^sub>R b"]
   970       by (auto simp: algebra_simps)
   971     hence "?S \<in> sets borel" by auto
   972     moreover
   973     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   974       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   975     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   976       by auto
   977   qed simp
   978 qed
   979 
   980 lemma borel_measurable_const_scaleR[measurable (raw)]:
   981   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
   982   using affine_borel_measurable_vector[of f M 0 b] by simp
   983 
   984 lemma borel_measurable_const_add[measurable (raw)]:
   985   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
   986   using affine_borel_measurable_vector[of f M a 1] by simp
   987 
   988 lemma borel_measurable_inverse[measurable (raw)]:
   989   fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
   990   assumes f: "f \<in> borel_measurable M"
   991   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   992   apply (rule measurable_compose[OF f])
   993   apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
   994   apply (auto intro!: continuous_on_inverse continuous_on_id)
   995   done
   996 
   997 lemma borel_measurable_divide[measurable (raw)]:
   998   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow>
   999     (\<lambda>x. f x / g x::'b::{second_countable_topology, real_normed_div_algebra}) \<in> borel_measurable M"
  1000   by (simp add: divide_inverse)
  1001 
  1002 lemma borel_measurable_max[measurable (raw)]:
  1003   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. max (g x) (f x) :: 'b::{second_countable_topology, dense_linorder, linorder_topology}) \<in> borel_measurable M"
  1004   by (simp add: max_def)
  1005 
  1006 lemma borel_measurable_min[measurable (raw)]:
  1007   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. min (g x) (f x) :: 'b::{second_countable_topology, dense_linorder, linorder_topology}) \<in> borel_measurable M"
  1008   by (simp add: min_def)
  1009 
  1010 lemma borel_measurable_Min[measurable (raw)]:
  1011   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable M) \<Longrightarrow> (\<lambda>x. Min ((\<lambda>i. f i x)`I) :: 'b::{second_countable_topology, dense_linorder, linorder_topology}) \<in> borel_measurable M"
  1012 proof (induct I rule: finite_induct)
  1013   case (insert i I) then show ?case
  1014     by (cases "I = {}") auto
  1015 qed auto
  1016 
  1017 lemma borel_measurable_Max[measurable (raw)]:
  1018   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable M) \<Longrightarrow> (\<lambda>x. Max ((\<lambda>i. f i x)`I) :: 'b::{second_countable_topology, dense_linorder, linorder_topology}) \<in> borel_measurable M"
  1019 proof (induct I rule: finite_induct)
  1020   case (insert i I) then show ?case
  1021     by (cases "I = {}") auto
  1022 qed auto
  1023 
  1024 lemma borel_measurable_abs[measurable (raw)]:
  1025   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
  1026   unfolding abs_real_def by simp
  1027 
  1028 lemma borel_measurable_nth[measurable (raw)]:
  1029   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
  1030   by (simp add: cart_eq_inner_axis)
  1031 
  1032 lemma convex_measurable:
  1033   fixes A :: "'a :: euclidean_space set"
  1034   shows "X \<in> borel_measurable M \<Longrightarrow> X ` space M \<subseteq> A \<Longrightarrow> open A \<Longrightarrow> convex_on A q \<Longrightarrow> 
  1035     (\<lambda>x. q (X x)) \<in> borel_measurable M"
  1036   by (rule measurable_compose[where f=X and N="restrict_space borel A"])
  1037      (auto intro!: borel_measurable_continuous_on_restrict convex_on_continuous measurable_restrict_space2)
  1038 
  1039 lemma borel_measurable_ln[measurable (raw)]:
  1040   assumes f: "f \<in> borel_measurable M"
  1041   shows "(\<lambda>x. ln (f x :: real)) \<in> borel_measurable M"
  1042   apply (rule measurable_compose[OF f])
  1043   apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
  1044   apply (auto intro!: continuous_on_ln continuous_on_id)
  1045   done
  1046 
  1047 lemma borel_measurable_log[measurable (raw)]:
  1048   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
  1049   unfolding log_def by auto
  1050 
  1051 lemma borel_measurable_exp[measurable]:
  1052   "(exp::'a::{real_normed_field,banach}\<Rightarrow>'a) \<in> borel_measurable borel"
  1053   by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_exp)
  1054 
  1055 lemma measurable_real_floor[measurable]:
  1056   "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  1057 proof -
  1058   have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real a \<le> x \<and> x < real (a + 1))"
  1059     by (auto intro: floor_eq2)
  1060   then show ?thesis
  1061     by (auto simp: vimage_def measurable_count_space_eq2_countable)
  1062 qed
  1063 
  1064 lemma measurable_real_ceiling[measurable]:
  1065   "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  1066   unfolding ceiling_def[abs_def] by simp
  1067 
  1068 lemma borel_measurable_real_floor: "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
  1069   by simp
  1070 
  1071 lemma borel_measurable_root [measurable]: "root n \<in> borel_measurable borel"
  1072   by (intro borel_measurable_continuous_on1 continuous_intros)
  1073 
  1074 lemma borel_measurable_sqrt [measurable]: "sqrt \<in> borel_measurable borel"
  1075   by (intro borel_measurable_continuous_on1 continuous_intros)
  1076 
  1077 lemma borel_measurable_power [measurable (raw)]:
  1078   fixes f :: "_ \<Rightarrow> 'b::{power,real_normed_algebra}"
  1079   assumes f: "f \<in> borel_measurable M"
  1080   shows "(\<lambda>x. (f x) ^ n) \<in> borel_measurable M"
  1081   by (intro borel_measurable_continuous_on [OF _ f] continuous_intros)
  1082 
  1083 lemma borel_measurable_Re [measurable]: "Re \<in> borel_measurable borel"
  1084   by (intro borel_measurable_continuous_on1 continuous_intros)
  1085 
  1086 lemma borel_measurable_Im [measurable]: "Im \<in> borel_measurable borel"
  1087   by (intro borel_measurable_continuous_on1 continuous_intros)
  1088 
  1089 lemma borel_measurable_of_real [measurable]: "(of_real :: _ \<Rightarrow> (_::real_normed_algebra)) \<in> borel_measurable borel"
  1090   by (intro borel_measurable_continuous_on1 continuous_intros)
  1091 
  1092 lemma borel_measurable_sin [measurable]: "(sin :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  1093   by (intro borel_measurable_continuous_on1 continuous_intros)
  1094 
  1095 lemma borel_measurable_cos [measurable]: "(cos :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  1096   by (intro borel_measurable_continuous_on1 continuous_intros)
  1097 
  1098 lemma borel_measurable_arctan [measurable]: "arctan \<in> borel_measurable borel"
  1099   by (intro borel_measurable_continuous_on1 continuous_intros)
  1100 
  1101 lemma borel_measurable_complex_iff:
  1102   "f \<in> borel_measurable M \<longleftrightarrow>
  1103     (\<lambda>x. Re (f x)) \<in> borel_measurable M \<and> (\<lambda>x. Im (f x)) \<in> borel_measurable M"
  1104   apply auto
  1105   apply (subst fun_complex_eq)
  1106   apply (intro borel_measurable_add)
  1107   apply auto
  1108   done
  1109 
  1110 subsection "Borel space on the extended reals"
  1111 
  1112 lemma borel_measurable_ereal[measurable (raw)]:
  1113   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1114   using continuous_on_ereal f by (rule borel_measurable_continuous_on) (rule continuous_on_id)
  1115 
  1116 lemma borel_measurable_real_of_ereal[measurable (raw)]:
  1117   fixes f :: "'a \<Rightarrow> ereal" 
  1118   assumes f: "f \<in> borel_measurable M"
  1119   shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
  1120   apply (rule measurable_compose[OF f])
  1121   apply (rule borel_measurable_continuous_countable_exceptions[of "{\<infinity>, -\<infinity> }"])
  1122   apply (auto intro: continuous_on_real simp: Compl_eq_Diff_UNIV)
  1123   done
  1124 
  1125 lemma borel_measurable_ereal_cases:
  1126   fixes f :: "'a \<Rightarrow> ereal" 
  1127   assumes f: "f \<in> borel_measurable M"
  1128   assumes H: "(\<lambda>x. H (ereal (real (f x)))) \<in> borel_measurable M"
  1129   shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
  1130 proof -
  1131   let ?F = "\<lambda>x. if f x = \<infinity> then H \<infinity> else if f x = - \<infinity> then H (-\<infinity>) else H (ereal (real (f x)))"
  1132   { fix x have "H (f x) = ?F x" by (cases "f x") auto }
  1133   with f H show ?thesis by simp
  1134 qed
  1135 
  1136 lemma
  1137   fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
  1138   shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
  1139     and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
  1140     and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
  1141   by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
  1142 
  1143 lemma borel_measurable_uminus_eq_ereal[simp]:
  1144   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  1145 proof
  1146   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
  1147 qed auto
  1148 
  1149 lemma set_Collect_ereal2:
  1150   fixes f g :: "'a \<Rightarrow> ereal" 
  1151   assumes f: "f \<in> borel_measurable M"
  1152   assumes g: "g \<in> borel_measurable M"
  1153   assumes H: "{x \<in> space M. H (ereal (real (f x))) (ereal (real (g x)))} \<in> sets M"
  1154     "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
  1155     "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
  1156     "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
  1157     "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
  1158   shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
  1159 proof -
  1160   let ?G = "\<lambda>y x. if g x = \<infinity> then H y \<infinity> else if g x = -\<infinity> then H y (-\<infinity>) else H y (ereal (real (g x)))"
  1161   let ?F = "\<lambda>x. if f x = \<infinity> then ?G \<infinity> x else if f x = -\<infinity> then ?G (-\<infinity>) x else ?G (ereal (real (f x))) x"
  1162   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1163   note * = this
  1164   from assms show ?thesis
  1165     by (subst *) (simp del: space_borel split del: split_if)
  1166 qed
  1167 
  1168 lemma borel_measurable_ereal_iff:
  1169   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  1170 proof
  1171   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1172   from borel_measurable_real_of_ereal[OF this]
  1173   show "f \<in> borel_measurable M" by auto
  1174 qed auto
  1175 
  1176 lemma borel_measurable_erealD[measurable_dest]:
  1177   "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<Longrightarrow> g \<in> measurable N M \<Longrightarrow> (\<lambda>x. f (g x)) \<in> borel_measurable N"
  1178   unfolding borel_measurable_ereal_iff by simp
  1179 
  1180 lemma borel_measurable_ereal_iff_real:
  1181   fixes f :: "'a \<Rightarrow> ereal"
  1182   shows "f \<in> borel_measurable M \<longleftrightarrow>
  1183     ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<infinity>} \<inter> space M \<in> sets M \<and> f -` {-\<infinity>} \<inter> space M \<in> sets M)"
  1184 proof safe
  1185   assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<infinity>} \<inter> space M \<in> sets M" "f -` {-\<infinity>} \<inter> space M \<in> sets M"
  1186   have "f -` {\<infinity>} \<inter> space M = {x\<in>space M. f x = \<infinity>}" "f -` {-\<infinity>} \<inter> space M = {x\<in>space M. f x = -\<infinity>}" by auto
  1187   with * have **: "{x\<in>space M. f x = \<infinity>} \<in> sets M" "{x\<in>space M. f x = -\<infinity>} \<in> sets M" by simp_all
  1188   let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real (f x))"
  1189   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
  1190   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
  1191   finally show "f \<in> borel_measurable M" .
  1192 qed simp_all
  1193 
  1194 lemma borel_measurable_ereal_iff_Iio:
  1195   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1196   by (auto simp: borel_Iio measurable_iff_measure_of)
  1197 
  1198 lemma borel_measurable_ereal_iff_Ioi:
  1199   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1200   by (auto simp: borel_Ioi measurable_iff_measure_of)
  1201 
  1202 lemma vimage_sets_compl_iff:
  1203   "f -` A \<inter> space M \<in> sets M \<longleftrightarrow> f -` (- A) \<inter> space M \<in> sets M"
  1204 proof -
  1205   { fix A assume "f -` A \<inter> space M \<in> sets M"
  1206     moreover have "f -` (- A) \<inter> space M = space M - f -` A \<inter> space M" by auto
  1207     ultimately have "f -` (- A) \<inter> space M \<in> sets M" by auto }
  1208   from this[of A] this[of "-A"] show ?thesis
  1209     by (metis double_complement)
  1210 qed
  1211 
  1212 lemma borel_measurable_iff_Iic_ereal:
  1213   "(f::'a\<Rightarrow>ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
  1214   unfolding borel_measurable_ereal_iff_Ioi vimage_sets_compl_iff[where A="{a <..}" for a] by simp
  1215 
  1216 lemma borel_measurable_iff_Ici_ereal:
  1217   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  1218   unfolding borel_measurable_ereal_iff_Iio vimage_sets_compl_iff[where A="{..< a}" for a] by simp
  1219 
  1220 lemma borel_measurable_ereal2:
  1221   fixes f g :: "'a \<Rightarrow> ereal" 
  1222   assumes f: "f \<in> borel_measurable M"
  1223   assumes g: "g \<in> borel_measurable M"
  1224   assumes H: "(\<lambda>x. H (ereal (real (f x))) (ereal (real (g x)))) \<in> borel_measurable M"
  1225     "(\<lambda>x. H (-\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1226     "(\<lambda>x. H (\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1227     "(\<lambda>x. H (ereal (real (f x))) (-\<infinity>)) \<in> borel_measurable M"
  1228     "(\<lambda>x. H (ereal (real (f x))) (\<infinity>)) \<in> borel_measurable M"
  1229   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  1230 proof -
  1231   let ?G = "\<lambda>y x. if g x = \<infinity> then H y \<infinity> else if g x = - \<infinity> then H y (-\<infinity>) else H y (ereal (real (g x)))"
  1232   let ?F = "\<lambda>x. if f x = \<infinity> then ?G \<infinity> x else if f x = - \<infinity> then ?G (-\<infinity>) x else ?G (ereal (real (f x))) x"
  1233   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1234   note * = this
  1235   from assms show ?thesis unfolding * by simp
  1236 qed
  1237 
  1238 lemma
  1239   fixes f :: "'a \<Rightarrow> ereal" assumes f: "f \<in> borel_measurable M"
  1240   shows borel_measurable_ereal_eq_const: "{x\<in>space M. f x = c} \<in> sets M"
  1241     and borel_measurable_ereal_neq_const: "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1242   using f by auto
  1243 
  1244 lemma [measurable(raw)]:
  1245   fixes f :: "'a \<Rightarrow> ereal"
  1246   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1247   shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1248     and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1249     and borel_measurable_ereal_min: "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1250     and borel_measurable_ereal_max: "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1251   by (simp_all add: borel_measurable_ereal2 min_def max_def)
  1252 
  1253 lemma [measurable(raw)]:
  1254   fixes f g :: "'a \<Rightarrow> ereal"
  1255   assumes "f \<in> borel_measurable M"
  1256   assumes "g \<in> borel_measurable M"
  1257   shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1258     and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  1259   using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  1260 
  1261 lemma borel_measurable_ereal_setsum[measurable (raw)]:
  1262   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1263   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1264   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1265   using assms by (induction S rule: infinite_finite_induct) auto
  1266 
  1267 lemma borel_measurable_ereal_setprod[measurable (raw)]:
  1268   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1269   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1270   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1271   using assms by (induction S rule: infinite_finite_induct) auto
  1272 
  1273 lemma [measurable (raw)]:
  1274   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1275   assumes "\<And>i. f i \<in> borel_measurable M"
  1276   shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1277     and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  1278   unfolding liminf_SUP_INF limsup_INF_SUP using assms by auto
  1279 
  1280 lemma sets_Collect_eventually_sequentially[measurable]:
  1281   "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
  1282   unfolding eventually_sequentially by simp
  1283 
  1284 lemma sets_Collect_ereal_convergent[measurable]: 
  1285   fixes f :: "nat \<Rightarrow> 'a => ereal"
  1286   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1287   shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
  1288   unfolding convergent_ereal by auto
  1289 
  1290 lemma borel_measurable_extreal_lim[measurable (raw)]:
  1291   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1292   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1293   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1294 proof -
  1295   have "\<And>x. lim (\<lambda>i. f i x) = (if convergent (\<lambda>i. f i x) then limsup (\<lambda>i. f i x) else (THE i. False))"
  1296     by (simp add: lim_def convergent_def convergent_limsup_cl)
  1297   then show ?thesis
  1298     by simp
  1299 qed
  1300 
  1301 lemma borel_measurable_ereal_LIMSEQ:
  1302   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1303   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1304   and u: "\<And>i. u i \<in> borel_measurable M"
  1305   shows "u' \<in> borel_measurable M"
  1306 proof -
  1307   have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
  1308     using u' by (simp add: lim_imp_Liminf[symmetric])
  1309   with u show ?thesis by (simp cong: measurable_cong)
  1310 qed
  1311 
  1312 lemma borel_measurable_extreal_suminf[measurable (raw)]:
  1313   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1314   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1315   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1316   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1317 
  1318 subsection {* LIMSEQ is borel measurable *}
  1319 
  1320 lemma borel_measurable_LIMSEQ:
  1321   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1322   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1323   and u: "\<And>i. u i \<in> borel_measurable M"
  1324   shows "u' \<in> borel_measurable M"
  1325 proof -
  1326   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1327     using u' by (simp add: lim_imp_Liminf)
  1328   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1329     by auto
  1330   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1331 qed
  1332 
  1333 lemma borel_measurable_LIMSEQ_metric:
  1334   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b :: metric_space"
  1335   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1336   assumes lim: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. f i x) ----> g x"
  1337   shows "g \<in> borel_measurable M"
  1338   unfolding borel_eq_closed
  1339 proof (safe intro!: measurable_measure_of)
  1340   fix A :: "'b set" assume "closed A" 
  1341 
  1342   have [measurable]: "(\<lambda>x. infdist (g x) A) \<in> borel_measurable M"
  1343   proof (rule borel_measurable_LIMSEQ)
  1344     show "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. infdist (f i x) A) ----> infdist (g x) A"
  1345       by (intro tendsto_infdist lim)
  1346     show "\<And>i. (\<lambda>x. infdist (f i x) A) \<in> borel_measurable M"
  1347       by (intro borel_measurable_continuous_on[where f="\<lambda>x. infdist x A"]
  1348         continuous_at_imp_continuous_on ballI continuous_infdist continuous_ident) auto
  1349   qed
  1350 
  1351   show "g -` A \<inter> space M \<in> sets M"
  1352   proof cases
  1353     assume "A \<noteq> {}"
  1354     then have "\<And>x. infdist x A = 0 \<longleftrightarrow> x \<in> A"
  1355       using `closed A` by (simp add: in_closed_iff_infdist_zero)
  1356     then have "g -` A \<inter> space M = {x\<in>space M. infdist (g x) A = 0}"
  1357       by auto
  1358     also have "\<dots> \<in> sets M"
  1359       by measurable
  1360     finally show ?thesis .
  1361   qed simp
  1362 qed auto
  1363 
  1364 lemma sets_Collect_Cauchy[measurable]: 
  1365   fixes f :: "nat \<Rightarrow> 'a => 'b::{metric_space, second_countable_topology}"
  1366   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1367   shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  1368   unfolding metric_Cauchy_iff2 using f by auto
  1369 
  1370 lemma borel_measurable_lim[measurable (raw)]:
  1371   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1372   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1373   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1374 proof -
  1375   def u' \<equiv> "\<lambda>x. lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1376   then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
  1377     by (auto simp: lim_def convergent_eq_cauchy[symmetric])
  1378   have "u' \<in> borel_measurable M"
  1379   proof (rule borel_measurable_LIMSEQ_metric)
  1380     fix x
  1381     have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1382       by (cases "Cauchy (\<lambda>i. f i x)")
  1383          (auto simp add: convergent_eq_cauchy[symmetric] convergent_def)
  1384     then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) ----> u' x"
  1385       unfolding u'_def 
  1386       by (rule convergent_LIMSEQ_iff[THEN iffD1])
  1387   qed measurable
  1388   then show ?thesis
  1389     unfolding * by measurable
  1390 qed
  1391 
  1392 lemma borel_measurable_suminf[measurable (raw)]:
  1393   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1394   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1395   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1396   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1397 
  1398 lemma borel_measurable_sup[measurable (raw)]:
  1399   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow>
  1400     (\<lambda>x. sup (f x) (g x)::ereal) \<in> borel_measurable M"
  1401   by simp
  1402 
  1403 (* Proof by Jeremy Avigad and Luke Serafin *)
  1404 lemma isCont_borel:
  1405   fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  1406   shows "{x. isCont f x} \<in> sets borel"
  1407 proof -
  1408   let ?I = "\<lambda>j. inverse(real (Suc j))"
  1409 
  1410   { fix x
  1411     have "isCont f x = (\<forall>i. \<exists>j. \<forall>y z. dist x y < ?I j \<and> dist x z < ?I j \<longrightarrow> dist (f y) (f z) \<le> ?I i)"
  1412       unfolding continuous_at_eps_delta
  1413     proof safe
  1414       fix i assume "\<forall>e>0. \<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  1415       moreover have "0 < ?I i / 2"
  1416         by simp
  1417       ultimately obtain d where d: "0 < d" "\<And>y. dist x y < d \<Longrightarrow> dist (f y) (f x) < ?I i / 2"
  1418         by (metis dist_commute)
  1419       then obtain j where j: "?I j < d"
  1420         by (metis reals_Archimedean)
  1421 
  1422       show "\<exists>j. \<forall>y z. dist x y < ?I j \<and> dist x z < ?I j \<longrightarrow> dist (f y) (f z) \<le> ?I i"
  1423       proof (safe intro!: exI[where x=j])
  1424         fix y z assume *: "dist x y < ?I j" "dist x z < ?I j"
  1425         have "dist (f y) (f z) \<le> dist (f y) (f x) + dist (f z) (f x)"
  1426           by (rule dist_triangle2)
  1427         also have "\<dots> < ?I i / 2 + ?I i / 2"
  1428           by (intro add_strict_mono d less_trans[OF _ j] *)
  1429         also have "\<dots> \<le> ?I i"
  1430           by (simp add: field_simps real_of_nat_Suc)
  1431         finally show "dist (f y) (f z) \<le> ?I i"
  1432           by simp
  1433       qed
  1434     next
  1435       fix e::real assume "0 < e"
  1436       then obtain n where n: "?I n < e"
  1437         by (metis reals_Archimedean)
  1438       assume "\<forall>i. \<exists>j. \<forall>y z. dist x y < ?I j \<and> dist x z < ?I j \<longrightarrow> dist (f y) (f z) \<le> ?I i"
  1439       from this[THEN spec, of "Suc n"]
  1440       obtain j where j: "\<And>y z. dist x y < ?I j \<Longrightarrow> dist x z < ?I j \<Longrightarrow> dist (f y) (f z) \<le> ?I (Suc n)"
  1441         by auto
  1442       
  1443       show "\<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  1444       proof (safe intro!: exI[of _ "?I j"])
  1445         fix y assume "dist y x < ?I j"
  1446         then have "dist (f y) (f x) \<le> ?I (Suc n)"
  1447           by (intro j) (auto simp: dist_commute)
  1448         also have "?I (Suc n) < ?I n"
  1449           by simp
  1450         also note n
  1451         finally show "dist (f y) (f x) < e" .
  1452       qed simp
  1453     qed }
  1454   note * = this
  1455 
  1456   have **: "\<And>e y. open {x. dist x y < e}"
  1457     using open_ball by (simp_all add: ball_def dist_commute)
  1458 
  1459   have "{x\<in>space borel. isCont f x} \<in> sets borel"
  1460     unfolding *
  1461     apply (intro sets.sets_Collect_countable_All sets.sets_Collect_countable_Ex)
  1462     apply (simp add: Collect_all_eq)
  1463     apply (intro borel_closed closed_INT ballI closed_Collect_imp open_Collect_conj **)
  1464     apply auto
  1465     done
  1466   then show ?thesis
  1467     by simp
  1468 qed
  1469 
  1470 no_notation
  1471   eucl_less (infix "<e" 50)
  1472 
  1473 end