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