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