src/HOL/Probability/Borel_Space.thy
author hoelzl
Fri Dec 14 15:46:01 2012 +0100 (2012-12-14)
changeset 50526 899c9c4e4a4c
parent 50419 3177d0374701
child 50881 ae630bab13da
permissions -rw-r--r--
Remove the indexed basis from the definition of euclidean spaces and only use the set of Basis vectors
     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 header {*Borel spaces*}
     7 
     8 theory Borel_Space
     9 imports
    10   Measurable
    11   "~~/src/HOL/Multivariate_Analysis/Multivariate_Analysis"
    12 begin
    13 
    14 section "Generic Borel spaces"
    15 
    16 definition borel :: "'a::topological_space measure" where
    17   "borel = sigma UNIV {S. open S}"
    18 
    19 abbreviation "borel_measurable M \<equiv> measurable M borel"
    20 
    21 lemma in_borel_measurable:
    22    "f \<in> borel_measurable M \<longleftrightarrow>
    23     (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
    24   by (auto simp add: measurable_def borel_def)
    25 
    26 lemma in_borel_measurable_borel:
    27    "f \<in> borel_measurable M \<longleftrightarrow>
    28     (\<forall>S \<in> sets borel.
    29       f -` S \<inter> space M \<in> sets M)"
    30   by (auto simp add: measurable_def borel_def)
    31 
    32 lemma space_borel[simp]: "space borel = UNIV"
    33   unfolding borel_def by auto
    34 
    35 lemma space_in_borel[measurable]: "UNIV \<in> sets borel"
    36   unfolding borel_def by auto
    37 
    38 lemma pred_Collect_borel[measurable (raw)]: "Measurable.pred borel P \<Longrightarrow> {x. P x} \<in> sets borel"
    39   unfolding borel_def pred_def by auto
    40 
    41 lemma borel_open[measurable (raw generic)]:
    42   assumes "open A" shows "A \<in> sets borel"
    43 proof -
    44   have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
    45   thus ?thesis unfolding borel_def by auto
    46 qed
    47 
    48 lemma borel_closed[measurable (raw generic)]:
    49   assumes "closed A" shows "A \<in> sets borel"
    50 proof -
    51   have "space borel - (- A) \<in> sets borel"
    52     using assms unfolding closed_def by (blast intro: borel_open)
    53   thus ?thesis by simp
    54 qed
    55 
    56 lemma borel_singleton[measurable]:
    57   "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets (borel :: 'a::t1_space measure)"
    58   unfolding insert_def by (rule sets.Un) auto
    59 
    60 lemma borel_comp[measurable]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
    61   unfolding Compl_eq_Diff_UNIV by simp
    62 
    63 lemma borel_measurable_vimage:
    64   fixes f :: "'a \<Rightarrow> 'x::t2_space"
    65   assumes borel[measurable]: "f \<in> borel_measurable M"
    66   shows "f -` {x} \<inter> space M \<in> sets M"
    67   by simp
    68 
    69 lemma borel_measurableI:
    70   fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
    71   assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
    72   shows "f \<in> borel_measurable M"
    73   unfolding borel_def
    74 proof (rule measurable_measure_of, simp_all)
    75   fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
    76     using assms[of S] by simp
    77 qed
    78 
    79 lemma borel_measurable_const:
    80   "(\<lambda>x. c) \<in> borel_measurable M"
    81   by auto
    82 
    83 lemma borel_measurable_indicator:
    84   assumes A: "A \<in> sets M"
    85   shows "indicator A \<in> borel_measurable M"
    86   unfolding indicator_def [abs_def] using A
    87   by (auto intro!: measurable_If_set)
    88 
    89 lemma borel_measurable_count_space[measurable (raw)]:
    90   "f \<in> borel_measurable (count_space S)"
    91   unfolding measurable_def by auto
    92 
    93 lemma borel_measurable_indicator'[measurable (raw)]:
    94   assumes [measurable]: "{x\<in>space M. f x \<in> A x} \<in> sets M"
    95   shows "(\<lambda>x. indicator (A x) (f x)) \<in> borel_measurable M"
    96   unfolding indicator_def[abs_def]
    97   by (auto intro!: measurable_If)
    98 
    99 lemma borel_measurable_indicator_iff:
   100   "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
   101     (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
   102 proof
   103   assume "?I \<in> borel_measurable M"
   104   then have "?I -` {1} \<inter> space M \<in> sets M"
   105     unfolding measurable_def by auto
   106   also have "?I -` {1} \<inter> space M = A \<inter> space M"
   107     unfolding indicator_def [abs_def] by auto
   108   finally show "A \<inter> space M \<in> sets M" .
   109 next
   110   assume "A \<inter> space M \<in> sets M"
   111   moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   112     (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   113     by (intro measurable_cong) (auto simp: indicator_def)
   114   ultimately show "?I \<in> borel_measurable M" by auto
   115 qed
   116 
   117 lemma borel_measurable_subalgebra:
   118   assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   119   shows "f \<in> borel_measurable M"
   120   using assms unfolding measurable_def by auto
   121 
   122 lemma borel_measurable_continuous_on1:
   123   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   124   assumes "continuous_on UNIV f"
   125   shows "f \<in> borel_measurable borel"
   126   apply(rule borel_measurableI)
   127   using continuous_open_preimage[OF assms] unfolding vimage_def by auto
   128 
   129 lemma borel_eq_countable_basis:
   130   fixes B::"'a::topological_space set set"
   131   assumes "countable B"
   132   assumes "topological_basis B"
   133   shows "borel = sigma UNIV B"
   134   unfolding borel_def
   135 proof (intro sigma_eqI sigma_sets_eqI, safe)
   136   interpret countable_basis using assms by unfold_locales
   137   fix X::"'a set" assume "open X"
   138   from open_countable_basisE[OF this] guess B' . note B' = this
   139   show "X \<in> sigma_sets UNIV B"
   140   proof cases
   141     assume "B' \<noteq> {}"
   142     thus "X \<in> sigma_sets UNIV B" using assms B'
   143       by (metis from_nat_into Union_image_eq countable_subset range_from_nat_into
   144         in_mono sigma_sets.Basic sigma_sets.Union)
   145   qed (simp add: sigma_sets.Empty B')
   146 next
   147   fix b assume "b \<in> B"
   148   hence "open b" by (rule topological_basis_open[OF assms(2)])
   149   thus "b \<in> sigma_sets UNIV (Collect open)" by auto
   150 qed simp_all
   151 
   152 lemma borel_eq_union_closed_basis:
   153   "borel = sigma UNIV union_closed_basis"
   154   by (rule borel_eq_countable_basis[OF countable_union_closed_basis basis_union_closed_basis])
   155 
   156 lemma topological_basis_prod:
   157   assumes A: "topological_basis A" and B: "topological_basis B"
   158   shows "topological_basis ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))"
   159   unfolding topological_basis_def
   160 proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric])
   161   fix S :: "('a \<times> 'b) set" assume "open S"
   162   then show "\<exists>X\<subseteq>A \<times> B. (\<Union>(a,b)\<in>X. a \<times> b) = S"
   163   proof (safe intro!: exI[of _ "{x\<in>A \<times> B. fst x \<times> snd x \<subseteq> S}"])
   164     fix x y assume "(x, y) \<in> S"
   165     from open_prod_elim[OF `open S` this]
   166     obtain a b where a: "open a""x \<in> a" and b: "open b" "y \<in> b" and "a \<times> b \<subseteq> S"
   167       by (metis mem_Sigma_iff)
   168     moreover from topological_basisE[OF A a] guess A0 .
   169     moreover from topological_basisE[OF B b] guess B0 .
   170     ultimately show "(x, y) \<in> (\<Union>(a, b)\<in>{X \<in> A \<times> B. fst X \<times> snd X \<subseteq> S}. a \<times> b)"
   171       by (intro UN_I[of "(A0, B0)"]) auto
   172   qed auto
   173 qed (metis A B topological_basis_open open_Times)
   174 
   175 instance prod :: (countable_basis_space, countable_basis_space) countable_basis_space
   176 proof
   177   obtain A :: "'a set set" where "countable A" "topological_basis A"
   178     using ex_countable_basis by auto
   179   moreover
   180   obtain B :: "'b set set" where "countable B" "topological_basis B"
   181     using ex_countable_basis by auto
   182   ultimately show "\<exists>B::('a \<times> 'b) set set. countable B \<and> topological_basis B"
   183     by (auto intro!: exI[of _ "(\<lambda>(a, b). a \<times> b) ` (A \<times> B)"] topological_basis_prod)
   184 qed
   185 
   186 lemma borel_measurable_Pair[measurable (raw)]:
   187   fixes f :: "'a \<Rightarrow> 'b::countable_basis_space" and g :: "'a \<Rightarrow> 'c::countable_basis_space"
   188   assumes f[measurable]: "f \<in> borel_measurable M"
   189   assumes g[measurable]: "g \<in> borel_measurable M"
   190   shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
   191 proof (subst borel_eq_countable_basis)
   192   let ?B = "SOME B::'b set set. countable B \<and> topological_basis B"
   193   let ?C = "SOME B::'c set set. countable B \<and> topological_basis B"
   194   let ?P = "(\<lambda>(b, c). b \<times> c) ` (?B \<times> ?C)"
   195   show "countable ?P" "topological_basis ?P"
   196     by (auto intro!: countable_basis topological_basis_prod is_basis)
   197 
   198   show "(\<lambda>x. (f x, g x)) \<in> measurable M (sigma UNIV ?P)"
   199   proof (rule measurable_measure_of)
   200     fix S assume "S \<in> ?P"
   201     then obtain b c where "b \<in> ?B" "c \<in> ?C" and S: "S = b \<times> c" by auto
   202     then have borel: "open b" "open c"
   203       by (auto intro: is_basis topological_basis_open)
   204     have "(\<lambda>x. (f x, g x)) -` S \<inter> space M = (f -` b \<inter> space M) \<inter> (g -` c \<inter> space M)"
   205       unfolding S by auto
   206     also have "\<dots> \<in> sets M"
   207       using borel by simp
   208     finally show "(\<lambda>x. (f x, g x)) -` S \<inter> space M \<in> sets M" .
   209   qed auto
   210 qed
   211 
   212 lemma borel_measurable_continuous_on:
   213   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   214   assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
   215   shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
   216   using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
   217 
   218 lemma borel_measurable_continuous_on_open':
   219   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   220   assumes cont: "continuous_on A f" "open A"
   221   shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
   222 proof (rule borel_measurableI)
   223   fix S :: "'b set" assume "open S"
   224   then have "open {x\<in>A. f x \<in> S}"
   225     by (intro continuous_open_preimage[OF cont]) auto
   226   then have *: "{x\<in>A. f x \<in> S} \<in> sets borel" by auto
   227   have "?f -` S \<inter> space borel = 
   228     {x\<in>A. f x \<in> S} \<union> (if c \<in> S then space borel - A else {})"
   229     by (auto split: split_if_asm)
   230   also have "\<dots> \<in> sets borel"
   231     using * `open A` by auto
   232   finally show "?f -` S \<inter> space borel \<in> sets borel" .
   233 qed
   234 
   235 lemma borel_measurable_continuous_on_open:
   236   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   237   assumes cont: "continuous_on A f" "open A"
   238   assumes g: "g \<in> borel_measurable M"
   239   shows "(\<lambda>x. if g x \<in> A then f (g x) else c) \<in> borel_measurable M"
   240   using measurable_comp[OF g borel_measurable_continuous_on_open'[OF cont], of c]
   241   by (simp add: comp_def)
   242 
   243 lemma continuous_on_fst: "continuous_on UNIV fst"
   244 proof -
   245   have [simp]: "range fst = UNIV" by (auto simp: image_iff)
   246   show ?thesis
   247     using closed_vimage_fst
   248     by (auto simp: continuous_on_closed closed_closedin vimage_def)
   249 qed
   250 
   251 lemma continuous_on_snd: "continuous_on UNIV snd"
   252 proof -
   253   have [simp]: "range snd = UNIV" by (auto simp: image_iff)
   254   show ?thesis
   255     using closed_vimage_snd
   256     by (auto simp: continuous_on_closed closed_closedin vimage_def)
   257 qed
   258 
   259 lemma borel_measurable_continuous_Pair:
   260   fixes f :: "'a \<Rightarrow> 'b::countable_basis_space" and g :: "'a \<Rightarrow> 'c::countable_basis_space"
   261   assumes [measurable]: "f \<in> borel_measurable M"
   262   assumes [measurable]: "g \<in> borel_measurable M"
   263   assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
   264   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   265 proof -
   266   have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
   267   show ?thesis
   268     unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
   269 qed
   270 
   271 section "Borel spaces on euclidean spaces"
   272 
   273 lemma borel_measurable_inner[measurable (raw)]:
   274   fixes f g :: "'a \<Rightarrow> 'b::{countable_basis_space, real_inner}"
   275   assumes "f \<in> borel_measurable M"
   276   assumes "g \<in> borel_measurable M"
   277   shows "(\<lambda>x. f x \<bullet> g x) \<in> borel_measurable M"
   278   using assms
   279   by (rule borel_measurable_continuous_Pair)
   280      (intro continuous_on_inner continuous_on_snd continuous_on_fst)
   281 
   282 lemma [measurable]:
   283   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   284   shows lessThan_borel: "{..< a} \<in> sets borel"
   285     and greaterThan_borel: "{a <..} \<in> sets borel"
   286     and greaterThanLessThan_borel: "{a<..<b} \<in> sets borel"
   287     and atMost_borel: "{..a} \<in> sets borel"
   288     and atLeast_borel: "{a..} \<in> sets borel"
   289     and atLeastAtMost_borel: "{a..b} \<in> sets borel"
   290     and greaterThanAtMost_borel: "{a<..b} \<in> sets borel"
   291     and atLeastLessThan_borel: "{a..<b} \<in> sets borel"
   292   unfolding greaterThanAtMost_def atLeastLessThan_def
   293   by (blast intro: borel_open borel_closed)+
   294 
   295 lemma borel_measurable_less[measurable]:
   296   fixes f :: "'a \<Rightarrow> real"
   297   assumes f: "f \<in> borel_measurable M"
   298   assumes g: "g \<in> borel_measurable M"
   299   shows "{w \<in> space M. f w < g w} \<in> sets M"
   300 proof -
   301   have "{w \<in> space M. f w < g w} = {x \<in> space M. \<exists>r. f x < of_rat r \<and> of_rat r < g x}"
   302     using Rats_dense_in_real by (auto simp add: Rats_def)
   303   with f g show ?thesis
   304     by simp
   305 qed
   306 
   307 lemma
   308   fixes f :: "'a \<Rightarrow> real"
   309   assumes f[measurable]: "f \<in> borel_measurable M"
   310   assumes g[measurable]: "g \<in> borel_measurable M"
   311   shows borel_measurable_le[measurable]: "{w \<in> space M. f w \<le> g w} \<in> sets M"
   312     and borel_measurable_eq[measurable]: "{w \<in> space M. f w = g w} \<in> sets M"
   313     and borel_measurable_neq: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   314   unfolding eq_iff not_less[symmetric]
   315   by measurable
   316 
   317 lemma 
   318   shows hafspace_less_borel: "{x::'a::euclidean_space. a < x \<bullet> i} \<in> sets borel"
   319     and hafspace_greater_borel: "{x::'a::euclidean_space. x \<bullet> i < a} \<in> sets borel"
   320     and hafspace_less_eq_borel: "{x::'a::euclidean_space. a \<le> x \<bullet> i} \<in> sets borel"
   321     and hafspace_greater_eq_borel: "{x::'a::euclidean_space. x \<bullet> i \<le> a} \<in> sets borel"
   322   by simp_all
   323 
   324 subsection "Borel space equals sigma algebras over intervals"
   325 
   326 lemma borel_sigma_sets_subset:
   327   "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
   328   using sets.sigma_sets_subset[of A borel] by simp
   329 
   330 lemma borel_eq_sigmaI1:
   331   fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   332   assumes borel_eq: "borel = sigma UNIV X"
   333   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (F ` A))"
   334   assumes F: "\<And>i. i \<in> A \<Longrightarrow> F i \<in> sets borel"
   335   shows "borel = sigma UNIV (F ` A)"
   336   unfolding borel_def
   337 proof (intro sigma_eqI antisym)
   338   have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
   339     unfolding borel_def by simp
   340   also have "\<dots> = sigma_sets UNIV X"
   341     unfolding borel_eq by simp
   342   also have "\<dots> \<subseteq> sigma_sets UNIV (F`A)"
   343     using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
   344   finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (F`A)" .
   345   show "sigma_sets UNIV (F`A) \<subseteq> sigma_sets UNIV {S. open S}"
   346     unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
   347 qed auto
   348 
   349 lemma borel_eq_sigmaI2:
   350   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
   351     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   352   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`B)"
   353   assumes X: "\<And>i j. (i, j) \<in> B \<Longrightarrow> G i j \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   354   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   355   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   356   using assms
   357   by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` B" and F="(\<lambda>(i, j). F i j)"]) auto
   358 
   359 lemma borel_eq_sigmaI3:
   360   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   361   assumes borel_eq: "borel = sigma UNIV X"
   362   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   363   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   364   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   365   using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
   366 
   367 lemma borel_eq_sigmaI4:
   368   fixes F :: "'i \<Rightarrow> 'a::topological_space set"
   369     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   370   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`A)"
   371   assumes X: "\<And>i j. (i, j) \<in> A \<Longrightarrow> G i j \<in> sets (sigma UNIV (range F))"
   372   assumes F: "\<And>i. F i \<in> sets borel"
   373   shows "borel = sigma UNIV (range F)"
   374   using assms by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` A" and F=F]) auto
   375 
   376 lemma borel_eq_sigmaI5:
   377   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
   378   assumes borel_eq: "borel = sigma UNIV (range G)"
   379   assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   380   assumes F: "\<And>i j. F i j \<in> sets borel"
   381   shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   382   using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
   383 
   384 lemma borel_eq_box:
   385   "borel = sigma UNIV (range (\<lambda> (a, b). box a b :: 'a \<Colon> euclidean_space set))"
   386     (is "_ = ?SIGMA")
   387 proof (rule borel_eq_sigmaI1[OF borel_def])
   388   fix M :: "'a set" assume "M \<in> {S. open S}"
   389   then have "open M" by simp
   390   show "M \<in> ?SIGMA"
   391     apply (subst open_UNION_box[OF `open M`])
   392     apply (safe intro!: sets.countable_UN' countable_PiE countable_Collect)
   393     apply (auto intro: countable_rat)
   394     done
   395 qed (auto simp: box_def)
   396 
   397 lemma borel_eq_greaterThanLessThan:
   398   "borel = sigma UNIV (range (\<lambda> (a, b). {a <..< b} :: 'a \<Colon> ordered_euclidean_space set))"
   399   unfolding borel_eq_box apply (rule arg_cong2[where f=sigma])
   400   by (auto simp: box_def image_iff mem_interval set_eq_iff simp del: greaterThanLessThan_iff)
   401 
   402 lemma halfspace_gt_in_halfspace:
   403   assumes i: "i \<in> A"
   404   shows "{x\<Colon>'a. a < x \<bullet> i} \<in> 
   405     sigma_sets UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> A))"
   406   (is "?set \<in> ?SIGMA")
   407 proof -
   408   interpret sigma_algebra UNIV ?SIGMA
   409     by (intro sigma_algebra_sigma_sets) simp_all
   410   have *: "?set = (\<Union>n. UNIV - {x\<Colon>'a. x \<bullet> i < a + 1 / real (Suc n)})"
   411   proof (safe, simp_all add: not_less)
   412     fix x :: 'a assume "a < x \<bullet> i"
   413     with reals_Archimedean[of "x \<bullet> i - a"]
   414     obtain n where "a + 1 / real (Suc n) < x \<bullet> i"
   415       by (auto simp: inverse_eq_divide field_simps)
   416     then show "\<exists>n. a + 1 / real (Suc n) \<le> x \<bullet> i"
   417       by (blast intro: less_imp_le)
   418   next
   419     fix x n
   420     have "a < a + 1 / real (Suc n)" by auto
   421     also assume "\<dots> \<le> x"
   422     finally show "a < x" .
   423   qed
   424   show "?set \<in> ?SIGMA" unfolding *
   425     by (auto del: Diff intro!: Diff i)
   426 qed
   427 
   428 lemma borel_eq_halfspace_less:
   429   "borel = sigma UNIV ((\<lambda>(a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> Basis))"
   430   (is "_ = ?SIGMA")
   431 proof (rule borel_eq_sigmaI2[OF borel_eq_box])
   432   fix a b :: 'a
   433   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}"
   434     by (auto simp: box_def)
   435   also have "\<dots> \<in> sets ?SIGMA"
   436     by (intro sets.sets_Collect_conj sets.sets_Collect_finite_All sets.sets_Collect_const)
   437        (auto intro!: halfspace_gt_in_halfspace countable_PiE countable_rat)
   438   finally show "box a b \<in> sets ?SIGMA" .
   439 qed auto
   440 
   441 lemma borel_eq_halfspace_le:
   442   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i \<le> a}) ` (UNIV \<times> Basis))"
   443   (is "_ = ?SIGMA")
   444 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   445   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   446   then have i: "i \<in> Basis" by auto
   447   have *: "{x::'a. x\<bullet>i < a} = (\<Union>n. {x. x\<bullet>i \<le> a - 1/real (Suc n)})"
   448   proof (safe, simp_all)
   449     fix x::'a assume *: "x\<bullet>i < a"
   450     with reals_Archimedean[of "a - x\<bullet>i"]
   451     obtain n where "x \<bullet> i < a - 1 / (real (Suc n))"
   452       by (auto simp: field_simps inverse_eq_divide)
   453     then show "\<exists>n. x \<bullet> i \<le> a - 1 / (real (Suc n))"
   454       by (blast intro: less_imp_le)
   455   next
   456     fix x::'a and n
   457     assume "x\<bullet>i \<le> a - 1 / real (Suc n)"
   458     also have "\<dots> < a" by auto
   459     finally show "x\<bullet>i < a" .
   460   qed
   461   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
   462     by (safe intro!: sets.countable_UN) (auto intro: i)
   463 qed auto
   464 
   465 lemma borel_eq_halfspace_ge:
   466   "borel = sigma UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. a \<le> x \<bullet> i}) ` (UNIV \<times> Basis))"
   467   (is "_ = ?SIGMA")
   468 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   469   fix a :: real and i :: 'a assume i: "(a, i) \<in> UNIV \<times> Basis"
   470   have *: "{x::'a. x\<bullet>i < a} = space ?SIGMA - {x::'a. a \<le> x\<bullet>i}" by auto
   471   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
   472     using i by (safe intro!: sets.compl_sets) auto
   473 qed auto
   474 
   475 lemma borel_eq_halfspace_greater:
   476   "borel = sigma UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. a < x \<bullet> i}) ` (UNIV \<times> Basis))"
   477   (is "_ = ?SIGMA")
   478 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
   479   fix a :: real and i :: 'a assume "(a, i) \<in> (UNIV \<times> Basis)"
   480   then have i: "i \<in> Basis" by auto
   481   have *: "{x::'a. x\<bullet>i \<le> a} = space ?SIGMA - {x::'a. a < x\<bullet>i}" by auto
   482   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
   483     by (safe intro!: sets.compl_sets) (auto intro: i)
   484 qed auto
   485 
   486 lemma borel_eq_atMost:
   487   "borel = sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space}))"
   488   (is "_ = ?SIGMA")
   489 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   490   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   491   then have "i \<in> Basis" by auto
   492   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)})"
   493   proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   494     fix x :: 'a
   495     from real_arch_simple[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"] guess k::nat ..
   496     then have "\<And>i. i \<in> Basis \<Longrightarrow> x\<bullet>i \<le> real k"
   497       by (subst (asm) Max_le_iff) auto
   498     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia \<le> real k"
   499       by (auto intro!: exI[of _ k])
   500   qed
   501   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
   502     by (safe intro!: sets.countable_UN) auto
   503 qed auto
   504 
   505 lemma borel_eq_greaterThan:
   506   "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {a<..}))"
   507   (is "_ = ?SIGMA")
   508 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   509   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   510   then have i: "i \<in> Basis" by auto
   511   have "{x::'a. x\<bullet>i \<le> a} = UNIV - {x::'a. a < x\<bullet>i}" by auto
   512   also have *: "{x::'a. a < x\<bullet>i} =
   513       (\<Union>k::nat. {\<Sum>n\<in>Basis. (if n = i then a else -real k) *\<^sub>R n <..})" using i
   514   proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   515     fix x :: 'a
   516     from reals_Archimedean2[of "Max ((\<lambda>i. -x\<bullet>i)`Basis)"]
   517     guess k::nat .. note k = this
   518     { fix i :: 'a assume "i \<in> Basis"
   519       then have "-x\<bullet>i < real k"
   520         using k by (subst (asm) Max_less_iff) auto
   521       then have "- real k < x\<bullet>i" by simp }
   522     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> -real k < x \<bullet> ia"
   523       by (auto intro!: exI[of _ k])
   524   qed
   525   finally show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA"
   526     apply (simp only:)
   527     apply (safe intro!: sets.countable_UN sets.Diff)
   528     apply (auto intro: sigma_sets_top)
   529     done
   530 qed auto
   531 
   532 lemma borel_eq_lessThan:
   533   "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {..<a}))"
   534   (is "_ = ?SIGMA")
   535 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
   536   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   537   then have i: "i \<in> Basis" by auto
   538   have "{x::'a. a \<le> x\<bullet>i} = UNIV - {x::'a. x\<bullet>i < a}" by auto
   539   also have *: "{x::'a. x\<bullet>i < a} = (\<Union>k::nat. {..< \<Sum>n\<in>Basis. (if n = i then a else real k) *\<^sub>R n})" using `i\<in> Basis`
   540   proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   541     fix x :: 'a
   542     from reals_Archimedean2[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"]
   543     guess k::nat .. note k = this
   544     { fix i :: 'a assume "i \<in> Basis"
   545       then have "x\<bullet>i < real k"
   546         using k by (subst (asm) Max_less_iff) auto
   547       then have "x\<bullet>i < real k" by simp }
   548     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia < real k"
   549       by (auto intro!: exI[of _ k])
   550   qed
   551   finally show "{x. a \<le> x\<bullet>i} \<in> ?SIGMA"
   552     apply (simp only:)
   553     apply (safe intro!: sets.countable_UN sets.Diff)
   554     apply (auto intro: sigma_sets_top)
   555     done
   556 qed auto
   557 
   558 lemma borel_eq_atLeastAtMost:
   559   "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} \<Colon>'a\<Colon>ordered_euclidean_space set))"
   560   (is "_ = ?SIGMA")
   561 proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
   562   fix a::'a
   563   have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   564   proof (safe, simp_all add: eucl_le[where 'a='a])
   565     fix x :: 'a
   566     from real_arch_simple[of "Max ((\<lambda>i. - x\<bullet>i)`Basis)"]
   567     guess k::nat .. note k = this
   568     { fix i :: 'a assume "i \<in> Basis"
   569       with k have "- x\<bullet>i \<le> real k"
   570         by (subst (asm) Max_le_iff) (auto simp: field_simps)
   571       then have "- real k \<le> x\<bullet>i" by simp }
   572     then show "\<exists>n::nat. \<forall>i\<in>Basis. - real n \<le> x \<bullet> i"
   573       by (auto intro!: exI[of _ k])
   574   qed
   575   show "{..a} \<in> ?SIGMA" unfolding *
   576     by (safe intro!: sets.countable_UN)
   577        (auto intro!: sigma_sets_top)
   578 qed auto
   579 
   580 lemma borel_eq_atLeastLessThan:
   581   "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
   582 proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
   583   have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
   584   fix x :: real
   585   have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
   586     by (auto simp: move_uminus real_arch_simple)
   587   then show "{..< x} \<in> ?SIGMA"
   588     by (auto intro: sigma_sets.intros)
   589 qed auto
   590 
   591 lemma borel_eq_closed: "borel = sigma UNIV (Collect closed)"
   592   unfolding borel_def
   593 proof (intro sigma_eqI sigma_sets_eqI, safe)
   594   fix x :: "'a set" assume "open x"
   595   hence "x = UNIV - (UNIV - x)" by auto
   596   also have "\<dots> \<in> sigma_sets UNIV (Collect closed)"
   597     by (rule sigma_sets.Compl)
   598        (auto intro!: sigma_sets.Basic simp: `open x`)
   599   finally show "x \<in> sigma_sets UNIV (Collect closed)" by simp
   600 next
   601   fix x :: "'a set" assume "closed x"
   602   hence "x = UNIV - (UNIV - x)" by auto
   603   also have "\<dots> \<in> sigma_sets UNIV (Collect open)"
   604     by (rule sigma_sets.Compl)
   605        (auto intro!: sigma_sets.Basic simp: `closed x`)
   606   finally show "x \<in> sigma_sets UNIV (Collect open)" by simp
   607 qed simp_all
   608 
   609 lemma borel_measurable_halfspacesI:
   610   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   611   assumes F: "borel = sigma UNIV (F ` (UNIV \<times> Basis))"
   612   and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M" 
   613   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a::real. S a i \<in> sets M)"
   614 proof safe
   615   fix a :: real and i :: 'b assume i: "i \<in> Basis" and f: "f \<in> borel_measurable M"
   616   then show "S a i \<in> sets M" unfolding assms
   617     by (auto intro!: measurable_sets simp: assms(1))
   618 next
   619   assume a: "\<forall>i\<in>Basis. \<forall>a. S a i \<in> sets M"
   620   then show "f \<in> borel_measurable M"
   621     by (auto intro!: measurable_measure_of simp: S_eq F)
   622 qed
   623 
   624 lemma borel_measurable_iff_halfspace_le:
   625   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   626   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)"
   627   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   628 
   629 lemma borel_measurable_iff_halfspace_less:
   630   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   631   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)"
   632   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   633 
   634 lemma borel_measurable_iff_halfspace_ge:
   635   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   636   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)"
   637   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   638 
   639 lemma borel_measurable_iff_halfspace_greater:
   640   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   641   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)"
   642   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
   643 
   644 lemma borel_measurable_iff_le:
   645   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   646   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   647 
   648 lemma borel_measurable_iff_less:
   649   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   650   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   651 
   652 lemma borel_measurable_iff_ge:
   653   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   654   using borel_measurable_iff_halfspace_ge[where 'c=real]
   655   by simp
   656 
   657 lemma borel_measurable_iff_greater:
   658   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   659   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   660 
   661 lemma borel_measurable_euclidean_space:
   662   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
   663   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M)"
   664 proof safe
   665   assume f: "\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M"
   666   then show "f \<in> borel_measurable M"
   667     by (subst borel_measurable_iff_halfspace_le) auto
   668 qed auto
   669 
   670 subsection "Borel measurable operators"
   671 
   672 lemma borel_measurable_uminus[measurable (raw)]:
   673   fixes g :: "'a \<Rightarrow> real"
   674   assumes g: "g \<in> borel_measurable M"
   675   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   676   by (rule borel_measurable_continuous_on[OF _ g]) (auto intro: continuous_on_minus continuous_on_id)
   677 
   678 lemma borel_measurable_add[measurable (raw)]:
   679   fixes f g :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   680   assumes f: "f \<in> borel_measurable M"
   681   assumes g: "g \<in> borel_measurable M"
   682   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   683   using f g
   684   by (rule borel_measurable_continuous_Pair)
   685      (auto intro: continuous_on_fst continuous_on_snd continuous_on_add)
   686 
   687 lemma borel_measurable_setsum[measurable (raw)]:
   688   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   689   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   690   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   691 proof cases
   692   assume "finite S"
   693   thus ?thesis using assms by induct auto
   694 qed simp
   695 
   696 lemma borel_measurable_diff[measurable (raw)]:
   697   fixes f :: "'a \<Rightarrow> real"
   698   assumes f: "f \<in> borel_measurable M"
   699   assumes g: "g \<in> borel_measurable M"
   700   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   701   unfolding diff_minus using assms by simp
   702 
   703 lemma borel_measurable_times[measurable (raw)]:
   704   fixes f :: "'a \<Rightarrow> real"
   705   assumes f: "f \<in> borel_measurable M"
   706   assumes g: "g \<in> borel_measurable M"
   707   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   708   using f g
   709   by (rule borel_measurable_continuous_Pair)
   710      (auto intro: continuous_on_fst continuous_on_snd continuous_on_mult)
   711 
   712 lemma continuous_on_dist:
   713   fixes f :: "'a :: t2_space \<Rightarrow> 'b :: metric_space"
   714   shows "continuous_on A f \<Longrightarrow> continuous_on A g \<Longrightarrow> continuous_on A (\<lambda>x. dist (f x) (g x))"
   715   unfolding continuous_on_eq_continuous_within by (auto simp: continuous_dist)
   716 
   717 lemma borel_measurable_dist[measurable (raw)]:
   718   fixes g f :: "'a \<Rightarrow> 'b::ordered_euclidean_space"
   719   assumes f: "f \<in> borel_measurable M"
   720   assumes g: "g \<in> borel_measurable M"
   721   shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
   722   using f g
   723   by (rule borel_measurable_continuous_Pair)
   724      (intro continuous_on_dist continuous_on_fst continuous_on_snd)
   725   
   726 lemma borel_measurable_scaleR[measurable (raw)]:
   727   fixes g :: "'a \<Rightarrow> 'b::ordered_euclidean_space"
   728   assumes f: "f \<in> borel_measurable M"
   729   assumes g: "g \<in> borel_measurable M"
   730   shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
   731   by (rule borel_measurable_continuous_Pair[OF f g])
   732      (auto intro!: continuous_on_scaleR continuous_on_fst continuous_on_snd)
   733 
   734 lemma affine_borel_measurable_vector:
   735   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   736   assumes "f \<in> borel_measurable M"
   737   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   738 proof (rule borel_measurableI)
   739   fix S :: "'x set" assume "open S"
   740   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   741   proof cases
   742     assume "b \<noteq> 0"
   743     with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
   744       by (auto intro!: open_affinity simp: scaleR_add_right)
   745     hence "?S \<in> sets borel" by auto
   746     moreover
   747     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   748       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   749     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   750       by auto
   751   qed simp
   752 qed
   753 
   754 lemma borel_measurable_const_scaleR[measurable (raw)]:
   755   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
   756   using affine_borel_measurable_vector[of f M 0 b] by simp
   757 
   758 lemma borel_measurable_const_add[measurable (raw)]:
   759   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
   760   using affine_borel_measurable_vector[of f M a 1] by simp
   761 
   762 lemma borel_measurable_setprod[measurable (raw)]:
   763   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   764   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   765   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   766 proof cases
   767   assume "finite S"
   768   thus ?thesis using assms by induct auto
   769 qed simp
   770 
   771 lemma borel_measurable_inverse[measurable (raw)]:
   772   fixes f :: "'a \<Rightarrow> real"
   773   assumes f: "f \<in> borel_measurable M"
   774   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   775 proof -
   776   have "(\<lambda>x::real. if x \<in> UNIV - {0} then inverse x else 0) \<in> borel_measurable borel"
   777     by (intro borel_measurable_continuous_on_open' continuous_on_inverse continuous_on_id) auto
   778   also have "(\<lambda>x::real. if x \<in> UNIV - {0} then inverse x else 0) = inverse" by (intro ext) auto
   779   finally show ?thesis using f by simp
   780 qed
   781 
   782 lemma borel_measurable_divide[measurable (raw)]:
   783   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. f x / g x::real) \<in> borel_measurable M"
   784   by (simp add: field_divide_inverse)
   785 
   786 lemma borel_measurable_max[measurable (raw)]:
   787   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. max (g x) (f x) :: real) \<in> borel_measurable M"
   788   by (simp add: max_def)
   789 
   790 lemma borel_measurable_min[measurable (raw)]:
   791   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. min (g x) (f x) :: real) \<in> borel_measurable M"
   792   by (simp add: min_def)
   793 
   794 lemma borel_measurable_abs[measurable (raw)]:
   795   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
   796   unfolding abs_real_def by simp
   797 
   798 lemma borel_measurable_nth[measurable (raw)]:
   799   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
   800   by (simp add: cart_eq_inner_axis)
   801 
   802 lemma convex_measurable:
   803   fixes a b :: real
   804   assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> { a <..< b}"
   805   assumes q: "convex_on { a <..< b} q"
   806   shows "(\<lambda>x. q (X x)) \<in> borel_measurable M"
   807 proof -
   808   have "(\<lambda>x. if X x \<in> {a <..< b} then q (X x) else 0) \<in> borel_measurable M" (is "?qX")
   809   proof (rule borel_measurable_continuous_on_open[OF _ _ X(1)])
   810     show "open {a<..<b}" by auto
   811     from this q show "continuous_on {a<..<b} q"
   812       by (rule convex_on_continuous)
   813   qed
   814   also have "?qX \<longleftrightarrow> (\<lambda>x. q (X x)) \<in> borel_measurable M"
   815     using X by (intro measurable_cong) auto
   816   finally show ?thesis .
   817 qed
   818 
   819 lemma borel_measurable_ln[measurable (raw)]:
   820   assumes f: "f \<in> borel_measurable M"
   821   shows "(\<lambda>x. ln (f x)) \<in> borel_measurable M"
   822 proof -
   823   { fix x :: real assume x: "x \<le> 0"
   824     { fix x::real assume "x \<le> 0" then have "\<And>u. exp u = x \<longleftrightarrow> False" by auto }
   825     from this[of x] x this[of 0] have "ln 0 = ln x"
   826       by (auto simp: ln_def) }
   827   note ln_imp = this
   828   have "(\<lambda>x. if f x \<in> {0<..} then ln (f x) else ln 0) \<in> borel_measurable M"
   829   proof (rule borel_measurable_continuous_on_open[OF _ _ f])
   830     show "continuous_on {0<..} ln"
   831       by (auto intro!: continuous_at_imp_continuous_on DERIV_ln DERIV_isCont
   832                simp: continuous_isCont[symmetric])
   833     show "open ({0<..}::real set)" by auto
   834   qed
   835   also have "(\<lambda>x. if x \<in> {0<..} then ln x else ln 0) = ln"
   836     by (simp add: fun_eq_iff not_less ln_imp)
   837   finally show ?thesis .
   838 qed
   839 
   840 lemma borel_measurable_log[measurable (raw)]:
   841   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
   842   unfolding log_def by auto
   843 
   844 lemma borel_measurable_exp[measurable]: "exp \<in> borel_measurable borel"
   845   by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI
   846             continuous_isCont[THEN iffD1] isCont_exp)
   847 
   848 lemma measurable_count_space_eq2_countable:
   849   fixes f :: "'a => 'c::countable"
   850   shows "f \<in> measurable M (count_space A) \<longleftrightarrow> (f \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. f -` {a} \<inter> space M \<in> sets M))"
   851 proof -
   852   { fix X assume "X \<subseteq> A" "f \<in> space M \<rightarrow> A"
   853     then have "f -` X \<inter> space M = (\<Union>a\<in>X. f -` {a} \<inter> space M)"
   854       by auto
   855     moreover assume "\<And>a. a\<in>A \<Longrightarrow> f -` {a} \<inter> space M \<in> sets M"
   856     ultimately have "f -` X \<inter> space M \<in> sets M"
   857       using `X \<subseteq> A` by (simp add: subset_eq del: UN_simps) }
   858   then show ?thesis
   859     unfolding measurable_def by auto
   860 qed
   861 
   862 lemma measurable_real_floor[measurable]:
   863   "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   864 proof -
   865   have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real a \<le> x \<and> x < real (a + 1))"
   866     by (auto intro: floor_eq2)
   867   then show ?thesis
   868     by (auto simp: vimage_def measurable_count_space_eq2_countable)
   869 qed
   870 
   871 lemma measurable_real_natfloor[measurable]:
   872   "(natfloor :: real \<Rightarrow> nat) \<in> measurable borel (count_space UNIV)"
   873   by (simp add: natfloor_def[abs_def])
   874 
   875 lemma measurable_real_ceiling[measurable]:
   876   "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   877   unfolding ceiling_def[abs_def] by simp
   878 
   879 lemma borel_measurable_real_floor: "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
   880   by simp
   881 
   882 lemma borel_measurable_real_natfloor:
   883   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
   884   by simp
   885 
   886 subsection "Borel space on the extended reals"
   887 
   888 lemma borel_measurable_ereal[measurable (raw)]:
   889   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   890   using continuous_on_ereal f by (rule borel_measurable_continuous_on)
   891 
   892 lemma borel_measurable_real_of_ereal[measurable (raw)]:
   893   fixes f :: "'a \<Rightarrow> ereal" 
   894   assumes f: "f \<in> borel_measurable M"
   895   shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
   896 proof -
   897   have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) \<in> borel_measurable M"
   898     using continuous_on_real
   899     by (rule borel_measurable_continuous_on_open[OF _ _ f]) auto
   900   also have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) = (\<lambda>x. real (f x))"
   901     by auto
   902   finally show ?thesis .
   903 qed
   904 
   905 lemma borel_measurable_ereal_cases:
   906   fixes f :: "'a \<Rightarrow> ereal" 
   907   assumes f: "f \<in> borel_measurable M"
   908   assumes H: "(\<lambda>x. H (ereal (real (f x)))) \<in> borel_measurable M"
   909   shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
   910 proof -
   911   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)))"
   912   { fix x have "H (f x) = ?F x" by (cases "f x") auto }
   913   with f H show ?thesis by simp
   914 qed
   915 
   916 lemma
   917   fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
   918   shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
   919     and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
   920     and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
   921   by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
   922 
   923 lemma borel_measurable_uminus_eq_ereal[simp]:
   924   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   925 proof
   926   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
   927 qed auto
   928 
   929 lemma set_Collect_ereal2:
   930   fixes f g :: "'a \<Rightarrow> ereal" 
   931   assumes f: "f \<in> borel_measurable M"
   932   assumes g: "g \<in> borel_measurable M"
   933   assumes H: "{x \<in> space M. H (ereal (real (f x))) (ereal (real (g x)))} \<in> sets M"
   934     "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
   935     "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
   936     "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
   937     "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
   938   shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
   939 proof -
   940   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)))"
   941   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"
   942   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
   943   note * = this
   944   from assms show ?thesis
   945     by (subst *) (simp del: space_borel split del: split_if)
   946 qed
   947 
   948 lemma [measurable]:
   949   fixes f g :: "'a \<Rightarrow> ereal"
   950   assumes f: "f \<in> borel_measurable M"
   951   assumes g: "g \<in> borel_measurable M"
   952   shows borel_measurable_ereal_le: "{x \<in> space M. f x \<le> g x} \<in> sets M"
   953     and borel_measurable_ereal_less: "{x \<in> space M. f x < g x} \<in> sets M"
   954     and borel_measurable_ereal_eq: "{w \<in> space M. f w = g w} \<in> sets M"
   955   using f g by (simp_all add: set_Collect_ereal2)
   956 
   957 lemma borel_measurable_ereal_neq:
   958   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> {w \<in> space M. f w \<noteq> (g w :: ereal)} \<in> sets M"
   959   by simp
   960 
   961 lemma borel_measurable_ereal_iff:
   962   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   963 proof
   964   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   965   from borel_measurable_real_of_ereal[OF this]
   966   show "f \<in> borel_measurable M" by auto
   967 qed auto
   968 
   969 lemma borel_measurable_ereal_iff_real:
   970   fixes f :: "'a \<Rightarrow> ereal"
   971   shows "f \<in> borel_measurable M \<longleftrightarrow>
   972     ((\<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)"
   973 proof safe
   974   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"
   975   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
   976   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
   977   let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real (f x))"
   978   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
   979   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
   980   finally show "f \<in> borel_measurable M" .
   981 qed simp_all
   982 
   983 lemma borel_measurable_eq_atMost_ereal:
   984   fixes f :: "'a \<Rightarrow> ereal"
   985   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
   986 proof (intro iffI allI)
   987   assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
   988   show "f \<in> borel_measurable M"
   989     unfolding borel_measurable_ereal_iff_real borel_measurable_iff_le
   990   proof (intro conjI allI)
   991     fix a :: real
   992     { fix x :: ereal assume *: "\<forall>i::nat. real i < x"
   993       have "x = \<infinity>"
   994       proof (rule ereal_top)
   995         fix B from reals_Archimedean2[of B] guess n ..
   996         then have "ereal B < real n" by auto
   997         with * show "B \<le> x" by (metis less_trans less_imp_le)
   998       qed }
   999     then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
  1000       by (auto simp: not_le)
  1001     then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos
  1002       by (auto simp del: UN_simps)
  1003     moreover
  1004     have "{-\<infinity>::ereal} = {..-\<infinity>}" by auto
  1005     then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
  1006     moreover have "{x\<in>space M. f x \<le> ereal a} \<in> sets M"
  1007       using pos[of "ereal a"] by (simp add: vimage_def Int_def conj_commute)
  1008     moreover have "{w \<in> space M. real (f w) \<le> a} =
  1009       (if a < 0 then {w \<in> space M. f w \<le> ereal a} - f -` {-\<infinity>} \<inter> space M
  1010       else {w \<in> space M. f w \<le> ereal a} \<union> (f -` {\<infinity>} \<inter> space M) \<union> (f -` {-\<infinity>} \<inter> space M))" (is "?l = ?r")
  1011       proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
  1012     ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
  1013   qed
  1014 qed (simp add: measurable_sets)
  1015 
  1016 lemma borel_measurable_eq_atLeast_ereal:
  1017   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  1018 proof
  1019   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
  1020   moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
  1021     by (auto simp: ereal_uminus_le_reorder)
  1022   ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
  1023     unfolding borel_measurable_eq_atMost_ereal by auto
  1024   then show "f \<in> borel_measurable M" by simp
  1025 qed (simp add: measurable_sets)
  1026 
  1027 lemma greater_eq_le_measurable:
  1028   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1029   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
  1030 proof
  1031   assume "f -` {a ..} \<inter> space M \<in> sets M"
  1032   moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
  1033   ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
  1034 next
  1035   assume "f -` {..< a} \<inter> space M \<in> sets M"
  1036   moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
  1037   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
  1038 qed
  1039 
  1040 lemma borel_measurable_ereal_iff_less:
  1041   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1042   unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
  1043 
  1044 lemma less_eq_ge_measurable:
  1045   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1046   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
  1047 proof
  1048   assume "f -` {a <..} \<inter> space M \<in> sets M"
  1049   moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
  1050   ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
  1051 next
  1052   assume "f -` {..a} \<inter> space M \<in> sets M"
  1053   moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
  1054   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
  1055 qed
  1056 
  1057 lemma borel_measurable_ereal_iff_ge:
  1058   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1059   unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
  1060 
  1061 lemma borel_measurable_ereal2:
  1062   fixes f g :: "'a \<Rightarrow> ereal" 
  1063   assumes f: "f \<in> borel_measurable M"
  1064   assumes g: "g \<in> borel_measurable M"
  1065   assumes H: "(\<lambda>x. H (ereal (real (f x))) (ereal (real (g x)))) \<in> borel_measurable M"
  1066     "(\<lambda>x. H (-\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1067     "(\<lambda>x. H (\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1068     "(\<lambda>x. H (ereal (real (f x))) (-\<infinity>)) \<in> borel_measurable M"
  1069     "(\<lambda>x. H (ereal (real (f x))) (\<infinity>)) \<in> borel_measurable M"
  1070   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  1071 proof -
  1072   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)))"
  1073   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"
  1074   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1075   note * = this
  1076   from assms show ?thesis unfolding * by simp
  1077 qed
  1078 
  1079 lemma
  1080   fixes f :: "'a \<Rightarrow> ereal" assumes f: "f \<in> borel_measurable M"
  1081   shows borel_measurable_ereal_eq_const: "{x\<in>space M. f x = c} \<in> sets M"
  1082     and borel_measurable_ereal_neq_const: "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1083   using f by auto
  1084 
  1085 lemma [measurable(raw)]:
  1086   fixes f :: "'a \<Rightarrow> ereal"
  1087   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1088   shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1089     and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1090     and borel_measurable_ereal_min: "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1091     and borel_measurable_ereal_max: "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1092   by (simp_all add: borel_measurable_ereal2 min_def max_def)
  1093 
  1094 lemma [measurable(raw)]:
  1095   fixes f g :: "'a \<Rightarrow> ereal"
  1096   assumes "f \<in> borel_measurable M"
  1097   assumes "g \<in> borel_measurable M"
  1098   shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1099     and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  1100   using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  1101 
  1102 lemma borel_measurable_ereal_setsum[measurable (raw)]:
  1103   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1104   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1105   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1106 proof cases
  1107   assume "finite S"
  1108   thus ?thesis using assms
  1109     by induct auto
  1110 qed simp
  1111 
  1112 lemma borel_measurable_ereal_setprod[measurable (raw)]:
  1113   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1114   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1115   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1116 proof cases
  1117   assume "finite S"
  1118   thus ?thesis using assms by induct auto
  1119 qed simp
  1120 
  1121 lemma borel_measurable_SUP[measurable (raw)]:
  1122   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1123   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1124   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  1125   unfolding borel_measurable_ereal_iff_ge
  1126 proof
  1127   fix a
  1128   have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  1129     by (auto simp: less_SUP_iff)
  1130   then show "?sup -` {a<..} \<inter> space M \<in> sets M"
  1131     using assms by auto
  1132 qed
  1133 
  1134 lemma borel_measurable_INF[measurable (raw)]:
  1135   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1136   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1137   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  1138   unfolding borel_measurable_ereal_iff_less
  1139 proof
  1140   fix a
  1141   have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  1142     by (auto simp: INF_less_iff)
  1143   then show "?inf -` {..<a} \<inter> space M \<in> sets M"
  1144     using assms by auto
  1145 qed
  1146 
  1147 lemma [measurable (raw)]:
  1148   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1149   assumes "\<And>i. f i \<in> borel_measurable M"
  1150   shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1151     and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  1152   unfolding liminf_SUPR_INFI limsup_INFI_SUPR using assms by auto
  1153 
  1154 lemma sets_Collect_eventually_sequentially[measurable]:
  1155   "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
  1156   unfolding eventually_sequentially by simp
  1157 
  1158 lemma sets_Collect_ereal_convergent[measurable]: 
  1159   fixes f :: "nat \<Rightarrow> 'a => ereal"
  1160   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1161   shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
  1162   unfolding convergent_ereal by auto
  1163 
  1164 lemma borel_measurable_extreal_lim[measurable (raw)]:
  1165   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1166   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1167   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1168 proof -
  1169   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))"
  1170     using convergent_ereal_limsup by (simp add: lim_def convergent_def)
  1171   then show ?thesis
  1172     by simp
  1173 qed
  1174 
  1175 lemma borel_measurable_ereal_LIMSEQ:
  1176   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1177   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1178   and u: "\<And>i. u i \<in> borel_measurable M"
  1179   shows "u' \<in> borel_measurable M"
  1180 proof -
  1181   have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
  1182     using u' by (simp add: lim_imp_Liminf[symmetric])
  1183   with u show ?thesis by (simp cong: measurable_cong)
  1184 qed
  1185 
  1186 lemma borel_measurable_extreal_suminf[measurable (raw)]:
  1187   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1188   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1189   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1190   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1191 
  1192 section "LIMSEQ is borel measurable"
  1193 
  1194 lemma borel_measurable_LIMSEQ:
  1195   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1196   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1197   and u: "\<And>i. u i \<in> borel_measurable M"
  1198   shows "u' \<in> borel_measurable M"
  1199 proof -
  1200   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1201     using u' by (simp add: lim_imp_Liminf)
  1202   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1203     by auto
  1204   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1205 qed
  1206 
  1207 lemma sets_Collect_Cauchy[measurable]: 
  1208   fixes f :: "nat \<Rightarrow> 'a => real"
  1209   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1210   shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  1211   unfolding Cauchy_iff2 using f by auto
  1212 
  1213 lemma borel_measurable_lim[measurable (raw)]:
  1214   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1215   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1216   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1217 proof -
  1218   def u' \<equiv> "\<lambda>x. lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1219   then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
  1220     by (auto simp: lim_def convergent_eq_cauchy[symmetric])
  1221   have "u' \<in> borel_measurable M"
  1222   proof (rule borel_measurable_LIMSEQ)
  1223     fix x
  1224     have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1225       by (cases "Cauchy (\<lambda>i. f i x)")
  1226          (auto simp add: convergent_eq_cauchy[symmetric] convergent_def)
  1227     then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) ----> u' x"
  1228       unfolding u'_def 
  1229       by (rule convergent_LIMSEQ_iff[THEN iffD1])
  1230   qed measurable
  1231   then show ?thesis
  1232     unfolding * by measurable
  1233 qed
  1234 
  1235 lemma borel_measurable_suminf[measurable (raw)]:
  1236   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1237   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1238   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1239   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1240 
  1241 end