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