src/HOL/Probability/Borel_Space.thy
author hoelzl
Tue Mar 22 20:06:10 2011 +0100 (2011-03-22)
changeset 42067 66c8281349ec
parent 41981 cdf7693bbe08
child 42150 b0c0638c4aad
permissions -rw-r--r--
standardized headers
     1 (*  Title:      HOL/Probability/Lebesgue_Integration.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 Multivariate_Analysis
    10 begin
    11 
    12 section "Generic Borel spaces"
    13 
    14 definition "borel = sigma \<lparr> space = UNIV::'a::topological_space set, sets = open\<rparr>"
    15 abbreviation "borel_measurable M \<equiv> measurable M borel"
    16 
    17 interpretation borel: sigma_algebra borel
    18   by (auto simp: borel_def intro!: sigma_algebra_sigma)
    19 
    20 lemma in_borel_measurable:
    21    "f \<in> borel_measurable M \<longleftrightarrow>
    22     (\<forall>S \<in> sets (sigma \<lparr> space = UNIV, sets = open\<rparr>).
    23       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 borel_open[simp]:
    36   assumes "open A" shows "A \<in> sets borel"
    37 proof -
    38   have "A \<in> open" unfolding mem_def using assms .
    39   thus ?thesis unfolding borel_def sigma_def by (auto intro!: sigma_sets.Basic)
    40 qed
    41 
    42 lemma borel_closed[simp]:
    43   assumes "closed A" shows "A \<in> sets borel"
    44 proof -
    45   have "space borel - (- A) \<in> sets borel"
    46     using assms unfolding closed_def by (blast intro: borel_open)
    47   thus ?thesis by simp
    48 qed
    49 
    50 lemma borel_comp[intro,simp]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
    51   unfolding Compl_eq_Diff_UNIV by (intro borel.Diff) auto
    52 
    53 lemma (in sigma_algebra) borel_measurable_vimage:
    54   fixes f :: "'a \<Rightarrow> 'x::t2_space"
    55   assumes borel: "f \<in> borel_measurable M"
    56   shows "f -` {x} \<inter> space M \<in> sets M"
    57 proof (cases "x \<in> f ` space M")
    58   case True then obtain y where "x = f y" by auto
    59   from closed_singleton[of "f y"]
    60   have "{f y} \<in> sets borel" by (rule borel_closed)
    61   with assms show ?thesis
    62     unfolding in_borel_measurable_borel `x = f y` by auto
    63 next
    64   case False hence "f -` {x} \<inter> space M = {}" by auto
    65   thus ?thesis by auto
    66 qed
    67 
    68 lemma (in sigma_algebra) borel_measurableI:
    69   fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
    70   assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
    71   shows "f \<in> borel_measurable M"
    72   unfolding borel_def
    73 proof (rule measurable_sigma, simp_all)
    74   fix S :: "'x set" assume "S \<in> open" thus "f -` S \<inter> space M \<in> sets M"
    75     using assms[of S] by (simp add: mem_def)
    76 qed
    77 
    78 lemma borel_singleton[simp, intro]:
    79   fixes x :: "'a::t1_space"
    80   shows "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets borel"
    81   proof (rule borel.insert_in_sets)
    82     show "{x} \<in> sets borel"
    83       using closed_singleton[of x] by (rule borel_closed)
    84   qed simp
    85 
    86 lemma (in sigma_algebra) borel_measurable_const[simp, intro]:
    87   "(\<lambda>x. c) \<in> borel_measurable M"
    88   by (auto intro!: measurable_const)
    89 
    90 lemma (in sigma_algebra) borel_measurable_indicator[simp, intro!]:
    91   assumes A: "A \<in> sets M"
    92   shows "indicator A \<in> borel_measurable M"
    93   unfolding indicator_def_raw using A
    94   by (auto intro!: measurable_If_set borel_measurable_const)
    95 
    96 lemma (in sigma_algebra) borel_measurable_indicator_iff:
    97   "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
    98     (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
    99 proof
   100   assume "?I \<in> borel_measurable M"
   101   then have "?I -` {1} \<inter> space M \<in> sets M"
   102     unfolding measurable_def by auto
   103   also have "?I -` {1} \<inter> space M = A \<inter> space M"
   104     unfolding indicator_def_raw by auto
   105   finally show "A \<inter> space M \<in> sets M" .
   106 next
   107   assume "A \<inter> space M \<in> sets M"
   108   moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   109     (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   110     by (intro measurable_cong) (auto simp: indicator_def)
   111   ultimately show "?I \<in> borel_measurable M" by auto
   112 qed
   113 
   114 lemma (in sigma_algebra) borel_measurable_restricted:
   115   fixes f :: "'a \<Rightarrow> extreal" assumes "A \<in> sets M"
   116   shows "f \<in> borel_measurable (restricted_space A) \<longleftrightarrow>
   117     (\<lambda>x. f x * indicator A x) \<in> borel_measurable M"
   118     (is "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable M")
   119 proof -
   120   interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
   121   have *: "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable ?R"
   122     by (auto intro!: measurable_cong)
   123   show ?thesis unfolding *
   124     unfolding in_borel_measurable_borel
   125   proof (simp, safe)
   126     fix S :: "extreal set" assume "S \<in> sets borel"
   127       "\<forall>S\<in>sets borel. ?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   128     then have "?f -` S \<inter> A \<in> op \<inter> A ` sets M" by auto
   129     then have f: "?f -` S \<inter> A \<in> sets M"
   130       using `A \<in> sets M` sets_into_space by fastsimp
   131     show "?f -` S \<inter> space M \<in> sets M"
   132     proof cases
   133       assume "0 \<in> S"
   134       then have "?f -` S \<inter> space M = ?f -` S \<inter> A \<union> (space M - A)"
   135         using `A \<in> sets M` sets_into_space by auto
   136       then show ?thesis using f `A \<in> sets M` by (auto intro!: Un Diff)
   137     next
   138       assume "0 \<notin> S"
   139       then have "?f -` S \<inter> space M = ?f -` S \<inter> A"
   140         using `A \<in> sets M` sets_into_space
   141         by (auto simp: indicator_def split: split_if_asm)
   142       then show ?thesis using f by auto
   143     qed
   144   next
   145     fix S :: "extreal set" assume "S \<in> sets borel"
   146       "\<forall>S\<in>sets borel. ?f -` S \<inter> space M \<in> sets M"
   147     then have f: "?f -` S \<inter> space M \<in> sets M" by auto
   148     then show "?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   149       using `A \<in> sets M` sets_into_space
   150       apply (simp add: image_iff)
   151       apply (rule bexI[OF _ f])
   152       by auto
   153   qed
   154 qed
   155 
   156 lemma (in sigma_algebra) borel_measurable_subalgebra:
   157   assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   158   shows "f \<in> borel_measurable M"
   159   using assms unfolding measurable_def by auto
   160 
   161 section "Borel spaces on euclidean spaces"
   162 
   163 lemma lessThan_borel[simp, intro]:
   164   fixes a :: "'a\<Colon>ordered_euclidean_space"
   165   shows "{..< a} \<in> sets borel"
   166   by (blast intro: borel_open)
   167 
   168 lemma greaterThan_borel[simp, intro]:
   169   fixes a :: "'a\<Colon>ordered_euclidean_space"
   170   shows "{a <..} \<in> sets borel"
   171   by (blast intro: borel_open)
   172 
   173 lemma greaterThanLessThan_borel[simp, intro]:
   174   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   175   shows "{a<..<b} \<in> sets borel"
   176   by (blast intro: borel_open)
   177 
   178 lemma atMost_borel[simp, intro]:
   179   fixes a :: "'a\<Colon>ordered_euclidean_space"
   180   shows "{..a} \<in> sets borel"
   181   by (blast intro: borel_closed)
   182 
   183 lemma atLeast_borel[simp, intro]:
   184   fixes a :: "'a\<Colon>ordered_euclidean_space"
   185   shows "{a..} \<in> sets borel"
   186   by (blast intro: borel_closed)
   187 
   188 lemma atLeastAtMost_borel[simp, intro]:
   189   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   190   shows "{a..b} \<in> sets borel"
   191   by (blast intro: borel_closed)
   192 
   193 lemma greaterThanAtMost_borel[simp, intro]:
   194   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   195   shows "{a<..b} \<in> sets borel"
   196   unfolding greaterThanAtMost_def by blast
   197 
   198 lemma atLeastLessThan_borel[simp, intro]:
   199   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   200   shows "{a..<b} \<in> sets borel"
   201   unfolding atLeastLessThan_def by blast
   202 
   203 lemma hafspace_less_borel[simp, intro]:
   204   fixes a :: real
   205   shows "{x::'a::euclidean_space. a < x $$ i} \<in> sets borel"
   206   by (auto intro!: borel_open open_halfspace_component_gt)
   207 
   208 lemma hafspace_greater_borel[simp, intro]:
   209   fixes a :: real
   210   shows "{x::'a::euclidean_space. x $$ i < a} \<in> sets borel"
   211   by (auto intro!: borel_open open_halfspace_component_lt)
   212 
   213 lemma hafspace_less_eq_borel[simp, intro]:
   214   fixes a :: real
   215   shows "{x::'a::euclidean_space. a \<le> x $$ i} \<in> sets borel"
   216   by (auto intro!: borel_closed closed_halfspace_component_ge)
   217 
   218 lemma hafspace_greater_eq_borel[simp, intro]:
   219   fixes a :: real
   220   shows "{x::'a::euclidean_space. x $$ i \<le> a} \<in> sets borel"
   221   by (auto intro!: borel_closed closed_halfspace_component_le)
   222 
   223 lemma (in sigma_algebra) borel_measurable_less[simp, intro]:
   224   fixes f :: "'a \<Rightarrow> real"
   225   assumes f: "f \<in> borel_measurable M"
   226   assumes g: "g \<in> borel_measurable M"
   227   shows "{w \<in> space M. f w < g w} \<in> sets M"
   228 proof -
   229   have "{w \<in> space M. f w < g w} =
   230         (\<Union>r. (f -` {..< of_rat r} \<inter> space M) \<inter> (g -` {of_rat r <..} \<inter> space M))"
   231     using Rats_dense_in_real by (auto simp add: Rats_def)
   232   then show ?thesis using f g
   233     by simp (blast intro: measurable_sets)
   234 qed
   235 
   236 lemma (in sigma_algebra) borel_measurable_le[simp, intro]:
   237   fixes f :: "'a \<Rightarrow> real"
   238   assumes f: "f \<in> borel_measurable M"
   239   assumes g: "g \<in> borel_measurable M"
   240   shows "{w \<in> space M. f w \<le> g w} \<in> sets M"
   241 proof -
   242   have "{w \<in> space M. f w \<le> g w} = space M - {w \<in> space M. g w < f w}"
   243     by auto
   244   thus ?thesis using f g
   245     by simp blast
   246 qed
   247 
   248 lemma (in sigma_algebra) borel_measurable_eq[simp, intro]:
   249   fixes f :: "'a \<Rightarrow> real"
   250   assumes f: "f \<in> borel_measurable M"
   251   assumes g: "g \<in> borel_measurable M"
   252   shows "{w \<in> space M. f w = g w} \<in> sets M"
   253 proof -
   254   have "{w \<in> space M. f w = g w} =
   255         {w \<in> space M. f w \<le> g w} \<inter> {w \<in> space M. g w \<le> f w}"
   256     by auto
   257   thus ?thesis using f g by auto
   258 qed
   259 
   260 lemma (in sigma_algebra) borel_measurable_neq[simp, intro]:
   261   fixes f :: "'a \<Rightarrow> real"
   262   assumes f: "f \<in> borel_measurable M"
   263   assumes g: "g \<in> borel_measurable M"
   264   shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   265 proof -
   266   have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}"
   267     by auto
   268   thus ?thesis using f g by auto
   269 qed
   270 
   271 subsection "Borel space equals sigma algebras over intervals"
   272 
   273 lemma rational_boxes:
   274   fixes x :: "'a\<Colon>ordered_euclidean_space"
   275   assumes "0 < e"
   276   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"
   277 proof -
   278   def e' \<equiv> "e / (2 * sqrt (real (DIM ('a))))"
   279   then have e: "0 < e'" using assms by (auto intro!: divide_pos_pos)
   280   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> y < x $$ i \<and> x $$ i - y < e'" (is "\<forall>i. ?th i")
   281   proof
   282     fix i from Rats_dense_in_real[of "x $$ i - e'" "x $$ i"] e
   283     show "?th i" by auto
   284   qed
   285   from choice[OF this] guess a .. note a = this
   286   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> x $$ i < y \<and> y - x $$ i < e'" (is "\<forall>i. ?th i")
   287   proof
   288     fix i from Rats_dense_in_real[of "x $$ i" "x $$ i + e'"] e
   289     show "?th i" by auto
   290   qed
   291   from choice[OF this] guess b .. note b = this
   292   { fix y :: 'a assume *: "Chi a < y" "y < Chi b"
   293     have "dist x y = sqrt (\<Sum>i<DIM('a). (dist (x $$ i) (y $$ i))\<twosuperior>)"
   294       unfolding setL2_def[symmetric] by (rule euclidean_dist_l2)
   295     also have "\<dots> < sqrt (\<Sum>i<DIM('a). e^2 / real (DIM('a)))"
   296     proof (rule real_sqrt_less_mono, rule setsum_strict_mono)
   297       fix i assume i: "i \<in> {..<DIM('a)}"
   298       have "a i < y$$i \<and> y$$i < b i" using * i eucl_less[where 'a='a] by auto
   299       moreover have "a i < x$$i" "x$$i - a i < e'" using a by auto
   300       moreover have "x$$i < b i" "b i - x$$i < e'" using b by auto
   301       ultimately have "\<bar>x$$i - y$$i\<bar> < 2 * e'" by auto
   302       then have "dist (x $$ i) (y $$ i) < e/sqrt (real (DIM('a)))"
   303         unfolding e'_def by (auto simp: dist_real_def)
   304       then have "(dist (x $$ i) (y $$ i))\<twosuperior> < (e/sqrt (real (DIM('a))))\<twosuperior>"
   305         by (rule power_strict_mono) auto
   306       then show "(dist (x $$ i) (y $$ i))\<twosuperior> < e\<twosuperior> / real DIM('a)"
   307         by (simp add: power_divide)
   308     qed auto
   309     also have "\<dots> = e" using `0 < e` by (simp add: real_eq_of_nat DIM_positive)
   310     finally have "dist x y < e" . }
   311   with a b show ?thesis
   312     apply (rule_tac exI[of _ "Chi a"])
   313     apply (rule_tac exI[of _ "Chi b"])
   314     using eucl_less[where 'a='a] by auto
   315 qed
   316 
   317 lemma ex_rat_list:
   318   fixes x :: "'a\<Colon>ordered_euclidean_space"
   319   assumes "\<And> i. x $$ i \<in> \<rat>"
   320   shows "\<exists> r. length r = DIM('a) \<and> (\<forall> i < DIM('a). of_rat (r ! i) = x $$ i)"
   321 proof -
   322   have "\<forall>i. \<exists>r. x $$ i = of_rat r" using assms unfolding Rats_def by blast
   323   from choice[OF this] guess r ..
   324   then show ?thesis by (auto intro!: exI[of _ "map r [0 ..< DIM('a)]"])
   325 qed
   326 
   327 lemma open_UNION:
   328   fixes M :: "'a\<Colon>ordered_euclidean_space set"
   329   assumes "open M"
   330   shows "M = UNION {(a, b) | a b. {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)} \<subseteq> M}
   331                    (\<lambda> (a, b). {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)})"
   332     (is "M = UNION ?idx ?box")
   333 proof safe
   334   fix x assume "x \<in> M"
   335   obtain e where e: "e > 0" "ball x e \<subseteq> M"
   336     using openE[OF assms `x \<in> M`] by auto
   337   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"
   338     using rational_boxes[OF e(1)] by blast
   339   then obtain p q where pq: "length p = DIM ('a)"
   340                             "length q = DIM ('a)"
   341                             "\<forall> i < DIM ('a). of_rat (p ! i) = a $$ i \<and> of_rat (q ! i) = b $$ i"
   342     using ex_rat_list[OF ab(2)] ex_rat_list[OF ab(3)] by blast
   343   hence p: "Chi (of_rat \<circ> op ! p) = a"
   344     using euclidean_eq[of "Chi (of_rat \<circ> op ! p)" a]
   345     unfolding o_def by auto
   346   from pq have q: "Chi (of_rat \<circ> op ! q) = b"
   347     using euclidean_eq[of "Chi (of_rat \<circ> op ! q)" b]
   348     unfolding o_def by auto
   349   have "x \<in> ?box (p, q)"
   350     using p q ab by auto
   351   thus "x \<in> UNION ?idx ?box" using ab e p q exI[of _ p] exI[of _ q] by auto
   352 qed auto
   353 
   354 lemma halfspace_span_open:
   355   "sigma_sets UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))
   356     \<subseteq> sets borel"
   357   by (auto intro!: borel.sigma_sets_subset[simplified] borel_open
   358                    open_halfspace_component_lt)
   359 
   360 lemma halfspace_lt_in_halfspace:
   361   "{x\<Colon>'a. x $$ i < a} \<in> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>)"
   362   by (auto intro!: sigma_sets.Basic simp: sets_sigma)
   363 
   364 lemma halfspace_gt_in_halfspace:
   365   "{x\<Colon>'a. a < x $$ i} \<in> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>)"
   366   (is "?set \<in> sets ?SIGMA")
   367 proof -
   368   interpret sigma_algebra "?SIGMA"
   369     by (intro sigma_algebra_sigma_sets) (simp_all add: sets_sigma)
   370   have *: "?set = (\<Union>n. space ?SIGMA - {x\<Colon>'a. x $$ i < a + 1 / real (Suc n)})"
   371   proof (safe, simp_all add: not_less)
   372     fix x assume "a < x $$ i"
   373     with reals_Archimedean[of "x $$ i - a"]
   374     obtain n where "a + 1 / real (Suc n) < x $$ i"
   375       by (auto simp: inverse_eq_divide field_simps)
   376     then show "\<exists>n. a + 1 / real (Suc n) \<le> x $$ i"
   377       by (blast intro: less_imp_le)
   378   next
   379     fix x n
   380     have "a < a + 1 / real (Suc n)" by auto
   381     also assume "\<dots> \<le> x"
   382     finally show "a < x" .
   383   qed
   384   show "?set \<in> sets ?SIGMA" unfolding *
   385     by (safe intro!: countable_UN Diff halfspace_lt_in_halfspace)
   386 qed
   387 
   388 lemma open_span_halfspace:
   389   "sets borel \<subseteq> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x $$ i < a})\<rparr>)"
   390     (is "_ \<subseteq> sets ?SIGMA")
   391 proof -
   392   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) simp
   393   then interpret sigma_algebra ?SIGMA .
   394   { fix S :: "'a set" assume "S \<in> open" then have "open S" unfolding mem_def .
   395     from open_UNION[OF this]
   396     obtain I where *: "S =
   397       (\<Union>(a, b)\<in>I.
   398           (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
   399           (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
   400       unfolding greaterThanLessThan_def
   401       unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
   402       unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
   403       by blast
   404     have "S \<in> sets ?SIGMA"
   405       unfolding *
   406       by (auto intro!: countable_UN Int countable_INT halfspace_lt_in_halfspace halfspace_gt_in_halfspace) }
   407   then show ?thesis unfolding borel_def
   408     by (intro sets_sigma_subset) auto
   409 qed
   410 
   411 lemma halfspace_span_halfspace_le:
   412   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   413    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. x $$ i \<le> a})\<rparr>)"
   414   (is "_ \<subseteq> sets ?SIGMA")
   415 proof -
   416   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   417   then interpret sigma_algebra ?SIGMA .
   418   { fix a i
   419     have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
   420     proof (safe, simp_all)
   421       fix x::'a assume *: "x$$i < a"
   422       with reals_Archimedean[of "a - x$$i"]
   423       obtain n where "x $$ i < a - 1 / (real (Suc n))"
   424         by (auto simp: field_simps inverse_eq_divide)
   425       then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
   426         by (blast intro: less_imp_le)
   427     next
   428       fix x::'a and n
   429       assume "x$$i \<le> a - 1 / real (Suc n)"
   430       also have "\<dots> < a" by auto
   431       finally show "x$$i < a" .
   432     qed
   433     have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   434       by (safe intro!: countable_UN)
   435          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   436   then show ?thesis by (intro sets_sigma_subset) auto
   437 qed
   438 
   439 lemma halfspace_span_halfspace_ge:
   440   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   441    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a \<le> x $$ i})\<rparr>)"
   442   (is "_ \<subseteq> sets ?SIGMA")
   443 proof -
   444   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   445   then interpret sigma_algebra ?SIGMA .
   446   { fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
   447     have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   448       by (safe intro!: Diff)
   449          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   450   then show ?thesis by (intro sets_sigma_subset) auto
   451 qed
   452 
   453 lemma halfspace_le_span_halfspace_gt:
   454   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   455    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a < x $$ i})\<rparr>)"
   456   (is "_ \<subseteq> sets ?SIGMA")
   457 proof -
   458   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   459   then interpret sigma_algebra ?SIGMA .
   460   { fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   461     have "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   462       by (safe intro!: Diff)
   463          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   464   then show ?thesis by (intro sets_sigma_subset) auto
   465 qed
   466 
   467 lemma halfspace_le_span_atMost:
   468   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   469    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>)"
   470   (is "_ \<subseteq> sets ?SIGMA")
   471 proof -
   472   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   473   then interpret sigma_algebra ?SIGMA .
   474   have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   475   proof cases
   476     fix a i assume "i < DIM('a)"
   477     then have *: "{x::'a. x$$i \<le> a} = (\<Union>k::nat. {.. (\<chi>\<chi> n. if n = i then a else real k)})"
   478     proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   479       fix x
   480       from real_arch_simple[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"] guess k::nat ..
   481       then have "\<And>i. i < DIM('a) \<Longrightarrow> x$$i \<le> real k"
   482         by (subst (asm) Max_le_iff) auto
   483       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia \<le> real k"
   484         by (auto intro!: exI[of _ k])
   485     qed
   486     show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   487       by (safe intro!: countable_UN)
   488          (auto simp: sets_sigma intro!: sigma_sets.Basic)
   489   next
   490     fix a i assume "\<not> i < DIM('a)"
   491     then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   492       using top by auto
   493   qed
   494   then show ?thesis by (intro sets_sigma_subset) auto
   495 qed
   496 
   497 lemma halfspace_le_span_greaterThan:
   498   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   499    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {a<..})\<rparr>)"
   500   (is "_ \<subseteq> sets ?SIGMA")
   501 proof -
   502   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   503   then interpret sigma_algebra ?SIGMA .
   504   have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   505   proof cases
   506     fix a i assume "i < DIM('a)"
   507     have "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   508     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)`
   509     proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   510       fix x
   511       from real_arch_lt[of "Max ((\<lambda>i. -x$$i)`{..<DIM('a)})"]
   512       guess k::nat .. note k = this
   513       { fix i assume "i < DIM('a)"
   514         then have "-x$$i < real k"
   515           using k by (subst (asm) Max_less_iff) auto
   516         then have "- real k < x$$i" by simp }
   517       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> -real k < x $$ ia"
   518         by (auto intro!: exI[of _ k])
   519     qed
   520     finally show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   521       apply (simp only:)
   522       apply (safe intro!: countable_UN Diff)
   523       by (auto simp: sets_sigma intro!: sigma_sets.Basic)
   524   next
   525     fix a i assume "\<not> i < DIM('a)"
   526     then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   527       using top by auto
   528   qed
   529   then show ?thesis by (intro sets_sigma_subset) auto
   530 qed
   531 
   532 lemma halfspace_le_span_lessThan:
   533   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a \<le> x $$ i})\<rparr>) \<subseteq>
   534    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..<a})\<rparr>)"
   535   (is "_ \<subseteq> sets ?SIGMA")
   536 proof -
   537   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   538   then interpret sigma_algebra ?SIGMA .
   539   have "\<And>a i. {x. a \<le> x$$i} \<in> sets ?SIGMA"
   540   proof cases
   541     fix a i assume "i < DIM('a)"
   542     have "{x::'a. a \<le> x$$i} = space ?SIGMA - {x::'a. x$$i < a}" by auto
   543     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)`
   544     proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   545       fix x
   546       from real_arch_lt[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"]
   547       guess k::nat .. note k = this
   548       { fix i assume "i < DIM('a)"
   549         then have "x$$i < real k"
   550           using k by (subst (asm) Max_less_iff) auto
   551         then have "x$$i < real k" by simp }
   552       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia < real k"
   553         by (auto intro!: exI[of _ k])
   554     qed
   555     finally show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   556       apply (simp only:)
   557       apply (safe intro!: countable_UN Diff)
   558       by (auto simp: sets_sigma intro!: sigma_sets.Basic)
   559   next
   560     fix a i assume "\<not> i < DIM('a)"
   561     then show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   562       using top by auto
   563   qed
   564   then show ?thesis by (intro sets_sigma_subset) auto
   565 qed
   566 
   567 lemma atMost_span_atLeastAtMost:
   568   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>) \<subseteq>
   569    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>(a,b). {a..b})\<rparr>)"
   570   (is "_ \<subseteq> sets ?SIGMA")
   571 proof -
   572   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   573   then interpret sigma_algebra ?SIGMA .
   574   { fix a::'a
   575     have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   576     proof (safe, simp_all add: eucl_le[where 'a='a])
   577       fix x
   578       from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
   579       guess k::nat .. note k = this
   580       { fix i assume "i < DIM('a)"
   581         with k have "- x$$i \<le> real k"
   582           by (subst (asm) Max_le_iff) (auto simp: field_simps)
   583         then have "- real k \<le> x$$i" by simp }
   584       then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
   585         by (auto intro!: exI[of _ k])
   586     qed
   587     have "{..a} \<in> sets ?SIGMA" unfolding *
   588       by (safe intro!: countable_UN)
   589          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   590   then show ?thesis by (intro sets_sigma_subset) auto
   591 qed
   592 
   593 lemma borel_eq_atMost:
   594   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> a. {.. a::'a\<Colon>ordered_euclidean_space})\<rparr>)"
   595     (is "_ = ?SIGMA")
   596 proof (intro algebra.equality antisym)
   597   show "sets borel \<subseteq> sets ?SIGMA"
   598     using halfspace_le_span_atMost halfspace_span_halfspace_le open_span_halfspace
   599     by auto
   600   show "sets ?SIGMA \<subseteq> sets borel"
   601     by (rule borel.sets_sigma_subset) auto
   602 qed auto
   603 
   604 lemma borel_eq_atLeastAtMost:
   605   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space, b). {a .. b})\<rparr>)"
   606    (is "_ = ?SIGMA")
   607 proof (intro algebra.equality antisym)
   608   show "sets borel \<subseteq> sets ?SIGMA"
   609     using atMost_span_atLeastAtMost halfspace_le_span_atMost
   610       halfspace_span_halfspace_le open_span_halfspace
   611     by auto
   612   show "sets ?SIGMA \<subseteq> sets borel"
   613     by (rule borel.sets_sigma_subset) auto
   614 qed auto
   615 
   616 lemma borel_eq_greaterThan:
   617   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {a <..})\<rparr>)"
   618    (is "_ = ?SIGMA")
   619 proof (intro algebra.equality antisym)
   620   show "sets borel \<subseteq> sets ?SIGMA"
   621     using halfspace_le_span_greaterThan
   622       halfspace_span_halfspace_le open_span_halfspace
   623     by auto
   624   show "sets ?SIGMA \<subseteq> sets borel"
   625     by (rule borel.sets_sigma_subset) auto
   626 qed auto
   627 
   628 lemma borel_eq_lessThan:
   629   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {..< a})\<rparr>)"
   630    (is "_ = ?SIGMA")
   631 proof (intro algebra.equality antisym)
   632   show "sets borel \<subseteq> sets ?SIGMA"
   633     using halfspace_le_span_lessThan
   634       halfspace_span_halfspace_ge open_span_halfspace
   635     by auto
   636   show "sets ?SIGMA \<subseteq> sets borel"
   637     by (rule borel.sets_sigma_subset) auto
   638 qed auto
   639 
   640 lemma borel_eq_greaterThanLessThan:
   641   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, b). {a <..< (b :: 'a \<Colon> ordered_euclidean_space)})\<rparr>)"
   642     (is "_ = ?SIGMA")
   643 proof (intro algebra.equality antisym)
   644   show "sets ?SIGMA \<subseteq> sets borel"
   645     by (rule borel.sets_sigma_subset) auto
   646   show "sets borel \<subseteq> sets ?SIGMA"
   647   proof -
   648     have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   649     then interpret sigma_algebra ?SIGMA .
   650     { fix M :: "'a set" assume "M \<in> open"
   651       then have "open M" by (simp add: mem_def)
   652       have "M \<in> sets ?SIGMA"
   653         apply (subst open_UNION[OF `open M`])
   654         apply (safe intro!: countable_UN)
   655         by (auto simp add: sigma_def intro!: sigma_sets.Basic) }
   656     then show ?thesis
   657       unfolding borel_def by (intro sets_sigma_subset) auto
   658   qed
   659 qed auto
   660 
   661 lemma borel_eq_halfspace_le:
   662   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i \<le> a})\<rparr>)"
   663    (is "_ = ?SIGMA")
   664 proof (intro algebra.equality antisym)
   665   show "sets borel \<subseteq> sets ?SIGMA"
   666     using open_span_halfspace halfspace_span_halfspace_le by auto
   667   show "sets ?SIGMA \<subseteq> sets borel"
   668     by (rule borel.sets_sigma_subset) auto
   669 qed auto
   670 
   671 lemma borel_eq_halfspace_less:
   672   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i < a})\<rparr>)"
   673    (is "_ = ?SIGMA")
   674 proof (intro algebra.equality antisym)
   675   show "sets borel \<subseteq> sets ?SIGMA"
   676     using open_span_halfspace .
   677   show "sets ?SIGMA \<subseteq> sets borel"
   678     by (rule borel.sets_sigma_subset) auto
   679 qed auto
   680 
   681 lemma borel_eq_halfspace_gt:
   682   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a < x$$i})\<rparr>)"
   683    (is "_ = ?SIGMA")
   684 proof (intro algebra.equality antisym)
   685   show "sets borel \<subseteq> sets ?SIGMA"
   686     using halfspace_le_span_halfspace_gt open_span_halfspace halfspace_span_halfspace_le by auto
   687   show "sets ?SIGMA \<subseteq> sets borel"
   688     by (rule borel.sets_sigma_subset) auto
   689 qed auto
   690 
   691 lemma borel_eq_halfspace_ge:
   692   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a \<le> x$$i})\<rparr>)"
   693    (is "_ = ?SIGMA")
   694 proof (intro algebra.equality antisym)
   695   show "sets borel \<subseteq> sets ?SIGMA"
   696     using halfspace_span_halfspace_ge open_span_halfspace by auto
   697   show "sets ?SIGMA \<subseteq> sets borel"
   698     by (rule borel.sets_sigma_subset) auto
   699 qed auto
   700 
   701 lemma (in sigma_algebra) borel_measurable_halfspacesI:
   702   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   703   assumes "borel = (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   704   and "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
   705   and "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
   706   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a::real. S a i \<in> sets M)"
   707 proof safe
   708   fix a :: real and i assume i: "i < DIM('c)" and f: "f \<in> borel_measurable M"
   709   then show "S a i \<in> sets M" unfolding assms
   710     by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1) sigma_def)
   711 next
   712   assume a: "\<forall>i<DIM('c). \<forall>a. S a i \<in> sets M"
   713   { fix a i have "S a i \<in> sets M"
   714     proof cases
   715       assume "i < DIM('c)"
   716       with a show ?thesis unfolding assms(2) by simp
   717     next
   718       assume "\<not> i < DIM('c)"
   719       from assms(3)[OF this] show ?thesis .
   720     qed }
   721   then have "f \<in> measurable M (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   722     by (auto intro!: measurable_sigma simp: assms(2))
   723   then show "f \<in> borel_measurable M" unfolding measurable_def
   724     unfolding assms(1) by simp
   725 qed
   726 
   727 lemma (in sigma_algebra) borel_measurable_iff_halfspace_le:
   728   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   729   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i \<le> a} \<in> sets M)"
   730   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   731 
   732 lemma (in sigma_algebra) borel_measurable_iff_halfspace_less:
   733   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   734   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i < a} \<in> sets M)"
   735   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   736 
   737 lemma (in sigma_algebra) borel_measurable_iff_halfspace_ge:
   738   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   739   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a \<le> f w $$ i} \<in> sets M)"
   740   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   741 
   742 lemma (in sigma_algebra) borel_measurable_iff_halfspace_greater:
   743   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   744   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a < f w $$ i} \<in> sets M)"
   745   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_gt]) auto
   746 
   747 lemma (in sigma_algebra) borel_measurable_iff_le:
   748   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   749   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   750 
   751 lemma (in sigma_algebra) borel_measurable_iff_less:
   752   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   753   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   754 
   755 lemma (in sigma_algebra) borel_measurable_iff_ge:
   756   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   757   using borel_measurable_iff_halfspace_ge[where 'c=real] by simp
   758 
   759 lemma (in sigma_algebra) borel_measurable_iff_greater:
   760   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   761   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   762 
   763 lemma borel_measurable_euclidean_component:
   764   "(\<lambda>x::'a::euclidean_space. x $$ i) \<in> borel_measurable borel"
   765   unfolding borel_def[where 'a=real]
   766 proof (rule borel.measurable_sigma, simp_all)
   767   fix S::"real set" assume "S \<in> open" then have "open S" unfolding mem_def .
   768   from open_vimage_euclidean_component[OF this]
   769   show "(\<lambda>x. x $$ i) -` S \<in> sets borel"
   770     by (auto intro: borel_open)
   771 qed
   772 
   773 lemma (in sigma_algebra) borel_measurable_euclidean_space:
   774   fixes f :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   775   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M)"
   776 proof safe
   777   fix i assume "f \<in> borel_measurable M"
   778   then show "(\<lambda>x. f x $$ i) \<in> borel_measurable M"
   779     using measurable_comp[of f _ _ "\<lambda>x. x $$ i", unfolded comp_def]
   780     by (auto intro: borel_measurable_euclidean_component)
   781 next
   782   assume f: "\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M"
   783   then show "f \<in> borel_measurable M"
   784     unfolding borel_measurable_iff_halfspace_le by auto
   785 qed
   786 
   787 subsection "Borel measurable operators"
   788 
   789 lemma (in sigma_algebra) affine_borel_measurable_vector:
   790   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   791   assumes "f \<in> borel_measurable M"
   792   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   793 proof (rule borel_measurableI)
   794   fix S :: "'x set" assume "open S"
   795   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   796   proof cases
   797     assume "b \<noteq> 0"
   798     with `open S` have "((\<lambda>x. (- a + x) /\<^sub>R b) ` S) \<in> open" (is "?S \<in> open")
   799       by (auto intro!: open_affinity simp: scaleR.add_right mem_def)
   800     hence "?S \<in> sets borel"
   801       unfolding borel_def by (auto simp: sigma_def intro!: sigma_sets.Basic)
   802     moreover
   803     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   804       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   805     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   806       by auto
   807   qed simp
   808 qed
   809 
   810 lemma (in sigma_algebra) affine_borel_measurable:
   811   fixes g :: "'a \<Rightarrow> real"
   812   assumes g: "g \<in> borel_measurable M"
   813   shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
   814   using affine_borel_measurable_vector[OF assms] by (simp add: mult_commute)
   815 
   816 lemma (in sigma_algebra) borel_measurable_add[simp, intro]:
   817   fixes f :: "'a \<Rightarrow> real"
   818   assumes f: "f \<in> borel_measurable M"
   819   assumes g: "g \<in> borel_measurable M"
   820   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   821 proof -
   822   have 1: "\<And>a. {w\<in>space M. a \<le> f w + g w} = {w \<in> space M. a + g w * -1 \<le> f w}"
   823     by auto
   824   have "\<And>a. (\<lambda>w. a + (g w) * -1) \<in> borel_measurable M"
   825     by (rule affine_borel_measurable [OF g])
   826   then have "\<And>a. {w \<in> space M. (\<lambda>w. a + (g w) * -1)(w) \<le> f w} \<in> sets M" using f
   827     by auto
   828   then have "\<And>a. {w \<in> space M. a \<le> f w + g w} \<in> sets M"
   829     by (simp add: 1)
   830   then show ?thesis
   831     by (simp add: borel_measurable_iff_ge)
   832 qed
   833 
   834 lemma (in sigma_algebra) borel_measurable_setsum[simp, intro]:
   835   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   836   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   837   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   838 proof cases
   839   assume "finite S"
   840   thus ?thesis using assms by induct auto
   841 qed simp
   842 
   843 lemma (in sigma_algebra) borel_measurable_square:
   844   fixes f :: "'a \<Rightarrow> real"
   845   assumes f: "f \<in> borel_measurable M"
   846   shows "(\<lambda>x. (f x)^2) \<in> borel_measurable M"
   847 proof -
   848   {
   849     fix a
   850     have "{w \<in> space M. (f w)\<twosuperior> \<le> a} \<in> sets M"
   851     proof (cases rule: linorder_cases [of a 0])
   852       case less
   853       hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} = {}"
   854         by auto (metis less order_le_less_trans power2_less_0)
   855       also have "... \<in> sets M"
   856         by (rule empty_sets)
   857       finally show ?thesis .
   858     next
   859       case equal
   860       hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   861              {w \<in> space M. f w \<le> 0} \<inter> {w \<in> space M. 0 \<le> f w}"
   862         by auto
   863       also have "... \<in> sets M"
   864         apply (insert f)
   865         apply (rule Int)
   866         apply (simp add: borel_measurable_iff_le)
   867         apply (simp add: borel_measurable_iff_ge)
   868         done
   869       finally show ?thesis .
   870     next
   871       case greater
   872       have "\<forall>x. (f x ^ 2 \<le> sqrt a ^ 2) = (- sqrt a  \<le> f x & f x \<le> sqrt a)"
   873         by (metis abs_le_interval_iff abs_of_pos greater real_sqrt_abs
   874                   real_sqrt_le_iff real_sqrt_power)
   875       hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   876              {w \<in> space M. -(sqrt a) \<le> f w} \<inter> {w \<in> space M. f w \<le> sqrt a}"
   877         using greater by auto
   878       also have "... \<in> sets M"
   879         apply (insert f)
   880         apply (rule Int)
   881         apply (simp add: borel_measurable_iff_ge)
   882         apply (simp add: borel_measurable_iff_le)
   883         done
   884       finally show ?thesis .
   885     qed
   886   }
   887   thus ?thesis by (auto simp add: borel_measurable_iff_le)
   888 qed
   889 
   890 lemma times_eq_sum_squares:
   891    fixes x::real
   892    shows"x*y = ((x+y)^2)/4 - ((x-y)^ 2)/4"
   893 by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric])
   894 
   895 lemma (in sigma_algebra) borel_measurable_uminus[simp, intro]:
   896   fixes g :: "'a \<Rightarrow> real"
   897   assumes g: "g \<in> borel_measurable M"
   898   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   899 proof -
   900   have "(\<lambda>x. - g x) = (\<lambda>x. 0 + (g x) * -1)"
   901     by simp
   902   also have "... \<in> borel_measurable M"
   903     by (fast intro: affine_borel_measurable g)
   904   finally show ?thesis .
   905 qed
   906 
   907 lemma (in sigma_algebra) borel_measurable_times[simp, intro]:
   908   fixes f :: "'a \<Rightarrow> real"
   909   assumes f: "f \<in> borel_measurable M"
   910   assumes g: "g \<in> borel_measurable M"
   911   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   912 proof -
   913   have 1: "(\<lambda>x. 0 + (f x + g x)\<twosuperior> * inverse 4) \<in> borel_measurable M"
   914     using assms by (fast intro: affine_borel_measurable borel_measurable_square)
   915   have "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) =
   916         (\<lambda>x. 0 + ((f x + -g x) ^ 2 * inverse -4))"
   917     by (simp add: minus_divide_right)
   918   also have "... \<in> borel_measurable M"
   919     using f g by (fast intro: affine_borel_measurable borel_measurable_square f g)
   920   finally have 2: "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) \<in> borel_measurable M" .
   921   show ?thesis
   922     apply (simp add: times_eq_sum_squares diff_minus)
   923     using 1 2 by simp
   924 qed
   925 
   926 lemma (in sigma_algebra) borel_measurable_setprod[simp, intro]:
   927   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   928   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   929   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   930 proof cases
   931   assume "finite S"
   932   thus ?thesis using assms by induct auto
   933 qed simp
   934 
   935 lemma (in sigma_algebra) borel_measurable_diff[simp, intro]:
   936   fixes f :: "'a \<Rightarrow> real"
   937   assumes f: "f \<in> borel_measurable M"
   938   assumes g: "g \<in> borel_measurable M"
   939   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   940   unfolding diff_minus using assms by fast
   941 
   942 lemma (in sigma_algebra) borel_measurable_inverse[simp, intro]:
   943   fixes f :: "'a \<Rightarrow> real"
   944   assumes "f \<in> borel_measurable M"
   945   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   946   unfolding borel_measurable_iff_ge unfolding inverse_eq_divide
   947 proof safe
   948   fix a :: real
   949   have *: "{w \<in> space M. a \<le> 1 / f w} =
   950       ({w \<in> space M. 0 < f w} \<inter> {w \<in> space M. a * f w \<le> 1}) \<union>
   951       ({w \<in> space M. f w < 0} \<inter> {w \<in> space M. 1 \<le> a * f w}) \<union>
   952       ({w \<in> space M. f w = 0} \<inter> {w \<in> space M. a \<le> 0})" by (auto simp: le_divide_eq)
   953   show "{w \<in> space M. a \<le> 1 / f w} \<in> sets M" using assms unfolding *
   954     by (auto intro!: Int Un)
   955 qed
   956 
   957 lemma (in sigma_algebra) borel_measurable_divide[simp, intro]:
   958   fixes f :: "'a \<Rightarrow> real"
   959   assumes "f \<in> borel_measurable M"
   960   and "g \<in> borel_measurable M"
   961   shows "(\<lambda>x. f x / g x) \<in> borel_measurable M"
   962   unfolding field_divide_inverse
   963   by (rule borel_measurable_inverse borel_measurable_times assms)+
   964 
   965 lemma (in sigma_algebra) borel_measurable_max[intro, simp]:
   966   fixes f g :: "'a \<Rightarrow> real"
   967   assumes "f \<in> borel_measurable M"
   968   assumes "g \<in> borel_measurable M"
   969   shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   970   unfolding borel_measurable_iff_le
   971 proof safe
   972   fix a
   973   have "{x \<in> space M. max (g x) (f x) \<le> a} =
   974     {x \<in> space M. g x \<le> a} \<inter> {x \<in> space M. f x \<le> a}" by auto
   975   thus "{x \<in> space M. max (g x) (f x) \<le> a} \<in> sets M"
   976     using assms unfolding borel_measurable_iff_le
   977     by (auto intro!: Int)
   978 qed
   979 
   980 lemma (in sigma_algebra) borel_measurable_min[intro, simp]:
   981   fixes f g :: "'a \<Rightarrow> real"
   982   assumes "f \<in> borel_measurable M"
   983   assumes "g \<in> borel_measurable M"
   984   shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
   985   unfolding borel_measurable_iff_ge
   986 proof safe
   987   fix a
   988   have "{x \<in> space M. a \<le> min (g x) (f x)} =
   989     {x \<in> space M. a \<le> g x} \<inter> {x \<in> space M. a \<le> f x}" by auto
   990   thus "{x \<in> space M. a \<le> min (g x) (f x)} \<in> sets M"
   991     using assms unfolding borel_measurable_iff_ge
   992     by (auto intro!: Int)
   993 qed
   994 
   995 lemma (in sigma_algebra) borel_measurable_abs[simp, intro]:
   996   assumes "f \<in> borel_measurable M"
   997   shows "(\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
   998 proof -
   999   have *: "\<And>x. \<bar>f x\<bar> = max 0 (f x) + max 0 (- f x)" by (simp add: max_def)
  1000   show ?thesis unfolding * using assms by auto
  1001 qed
  1002 
  1003 lemma borel_measurable_nth[simp, intro]:
  1004   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
  1005   using borel_measurable_euclidean_component
  1006   unfolding nth_conv_component by auto
  1007 
  1008 lemma borel_measurable_continuous_on1:
  1009   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
  1010   assumes "continuous_on UNIV f"
  1011   shows "f \<in> borel_measurable borel"
  1012   apply(rule borel.borel_measurableI)
  1013   using continuous_open_preimage[OF assms] unfolding vimage_def by auto
  1014 
  1015 lemma borel_measurable_continuous_on:
  1016   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
  1017   assumes cont: "continuous_on A f" "open A" and f: "f -` {c} \<inter> A \<in> sets borel"
  1018   shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
  1019 proof (rule borel.borel_measurableI)
  1020   fix S :: "'b set" assume "open S"
  1021   then have "open {x\<in>A. f x \<in> S - {c}}"
  1022     by (intro continuous_open_preimage[OF cont]) auto
  1023   then have *: "{x\<in>A. f x \<in> S - {c}} \<in> sets borel" by auto
  1024   show "?f -` S \<inter> space borel \<in> sets borel"
  1025   proof cases
  1026     assume "c \<in> S"
  1027     then have "?f -` S = {x\<in>A. f x \<in> S - {c}} \<union> (f -` {c} \<inter> A) \<union> -A"
  1028       by auto
  1029     with * show "?f -` S \<inter> space borel \<in> sets borel"
  1030       using `open A` f by (auto intro!: borel.Un)
  1031   next
  1032     assume "c \<notin> S"
  1033     then have "?f -` S = {x\<in>A. f x \<in> S - {c}}" by (auto split: split_if_asm)
  1034     with * show "?f -` S \<inter> space borel \<in> sets borel" by auto
  1035   qed
  1036 qed
  1037 
  1038 lemma borel_measurable_borel_log: assumes "1 < b" shows "log b \<in> borel_measurable borel"
  1039 proof -
  1040   { fix x :: real assume x: "x \<le> 0"
  1041     { fix x::real assume "x \<le> 0" then have "\<And>u. exp u = x \<longleftrightarrow> False" by auto }
  1042     from this[of x] x this[of 0] have "log b 0 = log b x"
  1043       by (auto simp: ln_def log_def) }
  1044   note log_imp = this
  1045   have "(\<lambda>x. if x \<in> {0<..} then log b x else log b 0) \<in> borel_measurable borel"
  1046   proof (rule borel_measurable_continuous_on)
  1047     show "continuous_on {0<..} (log b)"
  1048       by (auto intro!: continuous_at_imp_continuous_on DERIV_log DERIV_isCont
  1049                simp: continuous_isCont[symmetric])
  1050     show "open ({0<..}::real set)" by auto
  1051     show "log b -` {log b 0} \<inter> {0<..} \<in> sets borel"
  1052     proof cases
  1053       assume "log b -` {log b 0} \<inter> {0<..} = {}"
  1054       then show ?thesis by simp
  1055     next
  1056       assume "log b -` {log b 0} \<inter> {0<..} \<noteq> {}"
  1057       then obtain x where "0 < x" "log b x = log b 0" by auto
  1058       with log_inj[OF `1 < b`] have "log b -` {log b 0} \<inter> {0<..} = {x}"
  1059         by (auto simp: inj_on_def)
  1060       then show ?thesis by simp
  1061     qed
  1062   qed
  1063   also have "(\<lambda>x. if x \<in> {0<..} then log b x else log b 0) = log b"
  1064     by (simp add: fun_eq_iff not_less log_imp)
  1065   finally show ?thesis .
  1066 qed
  1067 
  1068 lemma (in sigma_algebra) borel_measurable_log[simp,intro]:
  1069   assumes f: "f \<in> borel_measurable M" and "1 < b"
  1070   shows "(\<lambda>x. log b (f x)) \<in> borel_measurable M"
  1071   using measurable_comp[OF f borel_measurable_borel_log[OF `1 < b`]]
  1072   by (simp add: comp_def)
  1073 
  1074 subsection "Borel space on the extended reals"
  1075 
  1076 lemma borel_measurable_extreal_borel:
  1077   "extreal \<in> borel_measurable borel"
  1078   unfolding borel_def[where 'a=extreal]
  1079 proof (rule borel.measurable_sigma)
  1080   fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
  1081   then have "open X" by (auto simp: mem_def)
  1082   then have "open (extreal -` X \<inter> space borel)"
  1083     by (simp add: open_extreal_vimage)
  1084   then show "extreal -` X \<inter> space borel \<in> sets borel" by auto
  1085 qed auto
  1086 
  1087 lemma (in sigma_algebra) borel_measurable_extreal[simp, intro]:
  1088   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
  1089   using measurable_comp[OF f borel_measurable_extreal_borel] unfolding comp_def .
  1090 
  1091 lemma borel_measurable_real_of_extreal_borel:
  1092   "(real :: extreal \<Rightarrow> real) \<in> borel_measurable borel"
  1093   unfolding borel_def[where 'a=real]
  1094 proof (rule borel.measurable_sigma)
  1095   fix B :: "real set" assume "B \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
  1096   then have "open B" by (auto simp: mem_def)
  1097   have *: "extreal -` real -` (B - {0}) = B - {0}" by auto
  1098   have open_real: "open (real -` (B - {0}) :: extreal set)"
  1099     unfolding open_extreal_def * using `open B` by auto
  1100   show "(real -` B \<inter> space borel :: extreal set) \<in> sets borel"
  1101   proof cases
  1102     assume "0 \<in> B"
  1103     then have *: "real -` B = real -` (B - {0}) \<union> {-\<infinity>, \<infinity>, 0}"
  1104       by (auto simp add: real_of_extreal_eq_0)
  1105     then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
  1106       using open_real by auto
  1107   next
  1108     assume "0 \<notin> B"
  1109     then have *: "(real -` B :: extreal set) = real -` (B - {0})"
  1110       by (auto simp add: real_of_extreal_eq_0)
  1111     then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
  1112       using open_real by auto
  1113   qed
  1114 qed auto
  1115 
  1116 lemma (in sigma_algebra) borel_measurable_real_of_extreal[simp, intro]:
  1117   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: extreal)) \<in> borel_measurable M"
  1118   using measurable_comp[OF f borel_measurable_real_of_extreal_borel] unfolding comp_def .
  1119 
  1120 lemma (in sigma_algebra) borel_measurable_extreal_iff:
  1121   shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  1122 proof
  1123   assume "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
  1124   from borel_measurable_real_of_extreal[OF this]
  1125   show "f \<in> borel_measurable M" by auto
  1126 qed auto
  1127 
  1128 lemma (in sigma_algebra) borel_measurable_extreal_iff_real:
  1129   "f \<in> borel_measurable M \<longleftrightarrow>
  1130     ((\<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)"
  1131 proof safe
  1132   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"
  1133   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
  1134   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
  1135   let "?f x" = "if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else extreal (real (f x))"
  1136   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
  1137   also have "?f = f" by (auto simp: fun_eq_iff extreal_real)
  1138   finally show "f \<in> borel_measurable M" .
  1139 qed (auto intro: measurable_sets borel_measurable_real_of_extreal)
  1140 
  1141 lemma (in sigma_algebra) less_eq_ge_measurable:
  1142   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1143   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
  1144 proof
  1145   assume "f -` {a <..} \<inter> space M \<in> sets M"
  1146   moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
  1147   ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
  1148 next
  1149   assume "f -` {..a} \<inter> space M \<in> sets M"
  1150   moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
  1151   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
  1152 qed
  1153 
  1154 lemma (in sigma_algebra) greater_eq_le_measurable:
  1155   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1156   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
  1157 proof
  1158   assume "f -` {a ..} \<inter> space M \<in> sets M"
  1159   moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
  1160   ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
  1161 next
  1162   assume "f -` {..< a} \<inter> space M \<in> sets M"
  1163   moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
  1164   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
  1165 qed
  1166 
  1167 lemma (in sigma_algebra) borel_measurable_uminus_borel_extreal:
  1168   "(uminus :: extreal \<Rightarrow> extreal) \<in> borel_measurable borel"
  1169 proof (subst borel_def, rule borel.measurable_sigma)
  1170   fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open\<rparr>"
  1171   then have "open X" by (simp add: mem_def)
  1172   have "uminus -` X = uminus ` X" by (force simp: image_iff)
  1173   then have "open (uminus -` X)" using `open X` extreal_open_uminus by auto
  1174   then show "uminus -` X \<inter> space borel \<in> sets borel" by auto
  1175 qed auto
  1176 
  1177 lemma (in sigma_algebra) borel_measurable_uminus_extreal[intro]:
  1178   assumes "f \<in> borel_measurable M"
  1179   shows "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M"
  1180   using measurable_comp[OF assms borel_measurable_uminus_borel_extreal] by (simp add: comp_def)
  1181 
  1182 lemma (in sigma_algebra) borel_measurable_uminus_eq_extreal[simp]:
  1183   "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  1184 proof
  1185   assume ?l from borel_measurable_uminus_extreal[OF this] show ?r by simp
  1186 qed auto
  1187 
  1188 lemma (in sigma_algebra) borel_measurable_eq_atMost_extreal:
  1189   "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
  1190 proof (intro iffI allI)
  1191   assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
  1192   show "f \<in> borel_measurable M"
  1193     unfolding borel_measurable_extreal_iff_real borel_measurable_iff_le
  1194   proof (intro conjI allI)
  1195     fix a :: real
  1196     { fix x :: extreal assume *: "\<forall>i::nat. real i < x"
  1197       have "x = \<infinity>"
  1198       proof (rule extreal_top)
  1199         fix B from real_arch_lt[of B] guess n ..
  1200         then have "extreal B < real n" by auto
  1201         with * show "B \<le> x" by (metis less_trans less_imp_le)
  1202       qed }
  1203     then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
  1204       by (auto simp: not_le)
  1205     then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos by (auto simp del: UN_simps intro!: Diff)
  1206     moreover
  1207     have "{-\<infinity>} = {..-\<infinity>}" by auto
  1208     then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
  1209     moreover have "{x\<in>space M. f x \<le> extreal a} \<in> sets M"
  1210       using pos[of "extreal a"] by (simp add: vimage_def Int_def conj_commute)
  1211     moreover have "{w \<in> space M. real (f w) \<le> a} =
  1212       (if a < 0 then {w \<in> space M. f w \<le> extreal a} - f -` {-\<infinity>} \<inter> space M
  1213       else {w \<in> space M. f w \<le> extreal a} \<union> (f -` {\<infinity>} \<inter> space M) \<union> (f -` {-\<infinity>} \<inter> space M))" (is "?l = ?r")
  1214       proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
  1215     ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
  1216   qed
  1217 qed (simp add: measurable_sets)
  1218 
  1219 lemma (in sigma_algebra) borel_measurable_eq_atLeast_extreal:
  1220   "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  1221 proof
  1222   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
  1223   moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
  1224     by (auto simp: extreal_uminus_le_reorder)
  1225   ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
  1226     unfolding borel_measurable_eq_atMost_extreal by auto
  1227   then show "f \<in> borel_measurable M" by simp
  1228 qed (simp add: measurable_sets)
  1229 
  1230 lemma (in sigma_algebra) borel_measurable_extreal_iff_less:
  1231   "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1232   unfolding borel_measurable_eq_atLeast_extreal greater_eq_le_measurable ..
  1233 
  1234 lemma (in sigma_algebra) borel_measurable_extreal_iff_ge:
  1235   "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1236   unfolding borel_measurable_eq_atMost_extreal less_eq_ge_measurable ..
  1237 
  1238 lemma (in sigma_algebra) borel_measurable_extreal_eq_const:
  1239   fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
  1240   shows "{x\<in>space M. f x = c} \<in> sets M"
  1241 proof -
  1242   have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
  1243   then show ?thesis using assms by (auto intro!: measurable_sets)
  1244 qed
  1245 
  1246 lemma (in sigma_algebra) borel_measurable_extreal_neq_const:
  1247   fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
  1248   shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1249 proof -
  1250   have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
  1251   then show ?thesis using assms by (auto intro!: measurable_sets)
  1252 qed
  1253 
  1254 lemma (in sigma_algebra) borel_measurable_extreal_le[intro,simp]:
  1255   fixes f g :: "'a \<Rightarrow> extreal"
  1256   assumes f: "f \<in> borel_measurable M"
  1257   assumes g: "g \<in> borel_measurable M"
  1258   shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
  1259 proof -
  1260   have "{x \<in> space M. f x \<le> g x} =
  1261     {x \<in> space M. real (f x) \<le> real (g x)} - (f -` {\<infinity>, -\<infinity>} \<inter> space M \<union> g -` {\<infinity>, -\<infinity>} \<inter> space M) \<union>
  1262     f -` {-\<infinity>} \<inter> space M \<union> g -` {\<infinity>} \<inter> space M" (is "?l = ?r")
  1263   proof (intro set_eqI)
  1264     fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases rule: extreal2_cases[of "f x" "g x"]) auto
  1265   qed
  1266   with f g show ?thesis by (auto intro!: Un simp: measurable_sets)
  1267 qed
  1268 
  1269 lemma (in sigma_algebra) borel_measurable_extreal_less[intro,simp]:
  1270   fixes f :: "'a \<Rightarrow> extreal"
  1271   assumes f: "f \<in> borel_measurable M"
  1272   assumes g: "g \<in> borel_measurable M"
  1273   shows "{x \<in> space M. f x < g x} \<in> sets M"
  1274 proof -
  1275   have "{x \<in> space M. f x < g x} = space M - {x \<in> space M. g x \<le> f x}" by auto
  1276   then show ?thesis using g f by auto
  1277 qed
  1278 
  1279 lemma (in sigma_algebra) borel_measurable_extreal_eq[intro,simp]:
  1280   fixes f :: "'a \<Rightarrow> extreal"
  1281   assumes f: "f \<in> borel_measurable M"
  1282   assumes g: "g \<in> borel_measurable M"
  1283   shows "{w \<in> space M. f w = g w} \<in> sets M"
  1284 proof -
  1285   have "{x \<in> space M. f x = g x} = {x \<in> space M. g x \<le> f x} \<inter> {x \<in> space M. f x \<le> g x}" by auto
  1286   then show ?thesis using g f by auto
  1287 qed
  1288 
  1289 lemma (in sigma_algebra) borel_measurable_extreal_neq[intro,simp]:
  1290   fixes f :: "'a \<Rightarrow> extreal"
  1291   assumes f: "f \<in> borel_measurable M"
  1292   assumes g: "g \<in> borel_measurable M"
  1293   shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
  1294 proof -
  1295   have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}" by auto
  1296   thus ?thesis using f g by auto
  1297 qed
  1298 
  1299 lemma (in sigma_algebra) split_sets:
  1300   "{x\<in>space M. P x \<or> Q x} = {x\<in>space M. P x} \<union> {x\<in>space M. Q x}"
  1301   "{x\<in>space M. P x \<and> Q x} = {x\<in>space M. P x} \<inter> {x\<in>space M. Q x}"
  1302   by auto
  1303 
  1304 lemma (in sigma_algebra) borel_measurable_extreal_add[intro, simp]:
  1305   fixes f :: "'a \<Rightarrow> extreal"
  1306   assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1307   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1308 proof -
  1309   { fix x assume "x \<in> space M" then have "f x + g x =
  1310       (if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
  1311         else if f x = -\<infinity> \<or> g x = -\<infinity> then -\<infinity>
  1312         else extreal (real (f x) + real (g x)))"
  1313       by (cases rule: extreal2_cases[of "f x" "g x"]) auto }
  1314   with assms show ?thesis
  1315     by (auto cong: measurable_cong simp: split_sets
  1316              intro!: Un measurable_If measurable_sets)
  1317 qed
  1318 
  1319 lemma (in sigma_algebra) borel_measurable_extreal_setsum[simp, intro]:
  1320   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
  1321   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1322   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1323 proof cases
  1324   assume "finite S"
  1325   thus ?thesis using assms
  1326     by induct auto
  1327 qed (simp add: borel_measurable_const)
  1328 
  1329 lemma abs_extreal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: extreal\<bar> = x"
  1330   by (cases x) auto
  1331 
  1332 lemma abs_extreal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: extreal\<bar> = -x"
  1333   by (cases x) auto
  1334 
  1335 lemma abs_extreal_pos[simp]: "0 \<le> \<bar>x :: extreal\<bar>"
  1336   by (cases x) auto
  1337 
  1338 lemma (in sigma_algebra) borel_measurable_extreal_abs[intro, simp]:
  1339   fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
  1340   shows "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
  1341 proof -
  1342   { fix x have "\<bar>f x\<bar> = (if 0 \<le> f x then f x else - f x)" by auto }
  1343   then show ?thesis using assms by (auto intro!: measurable_If)
  1344 qed
  1345 
  1346 lemma (in sigma_algebra) borel_measurable_extreal_times[intro, simp]:
  1347   fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1348   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1349 proof -
  1350   { fix f g :: "'a \<Rightarrow> extreal"
  1351     assume b: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1352       and pos: "\<And>x. 0 \<le> f x" "\<And>x. 0 \<le> g x"
  1353     { fix x have *: "f x * g x = (if f x = 0 \<or> g x = 0 then 0
  1354         else if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
  1355         else extreal (real (f x) * real (g x)))"
  1356       apply (cases rule: extreal2_cases[of "f x" "g x"])
  1357       using pos[of x] by auto }
  1358     with b have "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1359       by (auto cong: measurable_cong simp: split_sets
  1360                intro!: Un measurable_If measurable_sets) }
  1361   note pos_times = this
  1362   have *: "(\<lambda>x. f x * g x) =
  1363     (\<lambda>x. if 0 \<le> f x \<and> 0 \<le> g x \<or> f x < 0 \<and> g x < 0 then \<bar>f x\<bar> * \<bar>g x\<bar> else - (\<bar>f x\<bar> * \<bar>g x\<bar>))"
  1364     by (auto simp: fun_eq_iff)
  1365   show ?thesis using assms unfolding *
  1366     by (intro measurable_If pos_times borel_measurable_uminus_extreal)
  1367        (auto simp: split_sets intro!: Int)
  1368 qed
  1369 
  1370 lemma (in sigma_algebra) borel_measurable_extreal_setprod[simp, intro]:
  1371   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
  1372   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1373   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1374 proof cases
  1375   assume "finite S"
  1376   thus ?thesis using assms by induct auto
  1377 qed simp
  1378 
  1379 lemma (in sigma_algebra) borel_measurable_extreal_min[simp, intro]:
  1380   fixes f g :: "'a \<Rightarrow> extreal"
  1381   assumes "f \<in> borel_measurable M"
  1382   assumes "g \<in> borel_measurable M"
  1383   shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1384   using assms unfolding min_def by (auto intro!: measurable_If)
  1385 
  1386 lemma (in sigma_algebra) borel_measurable_extreal_max[simp, intro]:
  1387   fixes f g :: "'a \<Rightarrow> extreal"
  1388   assumes "f \<in> borel_measurable M"
  1389   and "g \<in> borel_measurable M"
  1390   shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1391   using assms unfolding max_def by (auto intro!: measurable_If)
  1392 
  1393 lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
  1394   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> extreal"
  1395   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1396   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  1397   unfolding borel_measurable_extreal_iff_ge
  1398 proof
  1399   fix a
  1400   have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  1401     by (auto simp: less_SUP_iff SUPR_apply)
  1402   then show "?sup -` {a<..} \<inter> space M \<in> sets M"
  1403     using assms by auto
  1404 qed
  1405 
  1406 lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
  1407   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> extreal"
  1408   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1409   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  1410   unfolding borel_measurable_extreal_iff_less
  1411 proof
  1412   fix a
  1413   have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  1414     by (auto simp: INF_less_iff INFI_apply)
  1415   then show "?inf -` {..<a} \<inter> space M \<in> sets M"
  1416     using assms by auto
  1417 qed
  1418 
  1419 lemma (in sigma_algebra) borel_measurable_liminf[simp, intro]:
  1420   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
  1421   assumes "\<And>i. f i \<in> borel_measurable M"
  1422   shows "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1423   unfolding liminf_SUPR_INFI using assms by auto
  1424 
  1425 lemma (in sigma_algebra) borel_measurable_limsup[simp, intro]:
  1426   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
  1427   assumes "\<And>i. f i \<in> borel_measurable M"
  1428   shows "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  1429   unfolding limsup_INFI_SUPR using assms by auto
  1430 
  1431 lemma (in sigma_algebra) borel_measurable_extreal_diff[simp, intro]:
  1432   fixes f g :: "'a \<Rightarrow> extreal"
  1433   assumes "f \<in> borel_measurable M"
  1434   assumes "g \<in> borel_measurable M"
  1435   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1436   unfolding minus_extreal_def using assms by auto
  1437 
  1438 lemma (in sigma_algebra) borel_measurable_psuminf[simp, intro]:
  1439   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
  1440   assumes "\<And>i. f i \<in> borel_measurable M" and pos: "\<And>i x. x \<in> space M \<Longrightarrow> 0 \<le> f i x"
  1441   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1442   apply (subst measurable_cong)
  1443   apply (subst suminf_extreal_eq_SUPR)
  1444   apply (rule pos)
  1445   using assms by auto
  1446 
  1447 section "LIMSEQ is borel measurable"
  1448 
  1449 lemma (in sigma_algebra) borel_measurable_LIMSEQ:
  1450   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1451   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1452   and u: "\<And>i. u i \<in> borel_measurable M"
  1453   shows "u' \<in> borel_measurable M"
  1454 proof -
  1455   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. extreal (u n x)) = extreal (u' x)"
  1456     using u' by (simp add: lim_imp_Liminf trivial_limit_sequentially lim_extreal)
  1457   moreover from u have "(\<lambda>x. liminf (\<lambda>n. extreal (u n x))) \<in> borel_measurable M"
  1458     by auto
  1459   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_extreal_iff)
  1460 qed
  1461 
  1462 end