src/HOL/Analysis/Borel_Space.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (3 weeks ago)
changeset 69981 3dced198b9ec
parent 69861 62e47f06d22c
child 70136 f03a01a18c6e
permissions -rw-r--r--
more strict AFP properties;
     1 (*  Title:      HOL/Analysis/Borel_Space.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Armin Heller, TU München
     4 *)
     5 
     6 section \<open>Borel Space\<close>
     7 
     8 theory Borel_Space
     9 imports
    10   Measurable Derivative Ordered_Euclidean_Space Extended_Real_Limits
    11 begin
    12 
    13 lemma sets_Collect_eventually_sequentially[measurable]:
    14   "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
    15   unfolding eventually_sequentially by simp
    16 
    17 lemma topological_basis_trivial: "topological_basis {A. open A}"
    18   by (auto simp: topological_basis_def)
    19 
    20 proposition open_prod_generated: "open = generate_topology {A \<times> B | A B. open A \<and> open B}"
    21 proof -
    22   have "{A \<times> B :: ('a \<times> 'b) set | A B. open A \<and> open B} = ((\<lambda>(a, b). a \<times> b) ` ({A. open A} \<times> {A. open A}))"
    23     by auto
    24   then show ?thesis
    25     by (auto intro: topological_basis_prod topological_basis_trivial topological_basis_imp_subbasis)
    26 qed
    27 
    28 definition%important "mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r \<le> s \<longrightarrow> f r \<le> f s"
    29 
    30 lemma mono_onI:
    31   "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r \<le> s \<Longrightarrow> f r \<le> f s) \<Longrightarrow> mono_on f A"
    32   unfolding mono_on_def by simp
    33 
    34 lemma mono_onD:
    35   "\<lbrakk>mono_on f A; r \<in> A; s \<in> A; r \<le> s\<rbrakk> \<Longrightarrow> f r \<le> f s"
    36   unfolding mono_on_def by simp
    37 
    38 lemma mono_imp_mono_on: "mono f \<Longrightarrow> mono_on f A"
    39   unfolding mono_def mono_on_def by auto
    40 
    41 lemma mono_on_subset: "mono_on f A \<Longrightarrow> B \<subseteq> A \<Longrightarrow> mono_on f B"
    42   unfolding mono_on_def by auto
    43 
    44 definition%important "strict_mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r < s \<longrightarrow> f r < f s"
    45 
    46 lemma strict_mono_onI:
    47   "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r < s \<Longrightarrow> f r < f s) \<Longrightarrow> strict_mono_on f A"
    48   unfolding strict_mono_on_def by simp
    49 
    50 lemma strict_mono_onD:
    51   "\<lbrakk>strict_mono_on f A; r \<in> A; s \<in> A; r < s\<rbrakk> \<Longrightarrow> f r < f s"
    52   unfolding strict_mono_on_def by simp
    53 
    54 lemma mono_on_greaterD:
    55   assumes "mono_on g A" "x \<in> A" "y \<in> A" "g x > (g (y::_::linorder) :: _ :: linorder)"
    56   shows "x > y"
    57 proof (rule ccontr)
    58   assume "\<not>x > y"
    59   hence "x \<le> y" by (simp add: not_less)
    60   from assms(1-3) and this have "g x \<le> g y" by (rule mono_onD)
    61   with assms(4) show False by simp
    62 qed
    63 
    64 lemma strict_mono_inv:
    65   fixes f :: "('a::linorder) \<Rightarrow> ('b::linorder)"
    66   assumes "strict_mono f" and "surj f" and inv: "\<And>x. g (f x) = x"
    67   shows "strict_mono g"
    68 proof
    69   fix x y :: 'b assume "x < y"
    70   from \<open>surj f\<close> obtain x' y' where [simp]: "x = f x'" "y = f y'" by blast
    71   with \<open>x < y\<close> and \<open>strict_mono f\<close> have "x' < y'" by (simp add: strict_mono_less)
    72   with inv show "g x < g y" by simp
    73 qed
    74 
    75 lemma strict_mono_on_imp_inj_on:
    76   assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> (_ :: preorder)) A"
    77   shows "inj_on f A"
    78 proof (rule inj_onI)
    79   fix x y assume "x \<in> A" "y \<in> A" "f x = f y"
    80   thus "x = y"
    81     by (cases x y rule: linorder_cases)
    82        (auto dest: strict_mono_onD[OF assms, of x y] strict_mono_onD[OF assms, of y x])
    83 qed
    84 
    85 lemma strict_mono_on_leD:
    86   assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A" "x \<in> A" "y \<in> A" "x \<le> y"
    87   shows "f x \<le> f y"
    88 proof (insert le_less_linear[of y x], elim disjE)
    89   assume "x < y"
    90   with assms have "f x < f y" by (rule_tac strict_mono_onD[OF assms(1)]) simp_all
    91   thus ?thesis by (rule less_imp_le)
    92 qed (insert assms, simp)
    93 
    94 lemma strict_mono_on_eqD:
    95   fixes f :: "(_ :: linorder) \<Rightarrow> (_ :: preorder)"
    96   assumes "strict_mono_on f A" "f x = f y" "x \<in> A" "y \<in> A"
    97   shows "y = x"
    98   using assms by (rule_tac linorder_cases[of x y]) (auto dest: strict_mono_onD)
    99 
   100 proposition mono_on_imp_deriv_nonneg:
   101   assumes mono: "mono_on f A" and deriv: "(f has_real_derivative D) (at x)"
   102   assumes "x \<in> interior A"
   103   shows "D \<ge> 0"
   104 proof (rule tendsto_lowerbound)
   105   let ?A' = "(\<lambda>y. y - x) ` interior A"
   106   from deriv show "((\<lambda>h. (f (x + h) - f x) / h) \<longlongrightarrow> D) (at 0)"
   107       by (simp add: field_has_derivative_at has_field_derivative_def)
   108   from mono have mono': "mono_on f (interior A)" by (rule mono_on_subset) (rule interior_subset)
   109 
   110   show "eventually (\<lambda>h. (f (x + h) - f x) / h \<ge> 0) (at 0)"
   111   proof (subst eventually_at_topological, intro exI conjI ballI impI)
   112     have "open (interior A)" by simp
   113     hence "open ((+) (-x) ` interior A)" by (rule open_translation)
   114     also have "((+) (-x) ` interior A) = ?A'" by auto
   115     finally show "open ?A'" .
   116   next
   117     from \<open>x \<in> interior A\<close> show "0 \<in> ?A'" by auto
   118   next
   119     fix h assume "h \<in> ?A'"
   120     hence "x + h \<in> interior A" by auto
   121     with mono' and \<open>x \<in> interior A\<close> show "(f (x + h) - f x) / h \<ge> 0"
   122       by (cases h rule: linorder_cases[of _ 0])
   123          (simp_all add: divide_nonpos_neg divide_nonneg_pos mono_onD field_simps)
   124   qed
   125 qed simp
   126 
   127 lemma strict_mono_on_imp_mono_on:
   128   "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A \<Longrightarrow> mono_on f A"
   129   by (rule mono_onI, rule strict_mono_on_leD)
   130 
   131 proposition mono_on_ctble_discont:
   132   fixes f :: "real \<Rightarrow> real"
   133   fixes A :: "real set"
   134   assumes "mono_on f A"
   135   shows "countable {a\<in>A. \<not> continuous (at a within A) f}"
   136 proof -
   137   have mono: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   138     using \<open>mono_on f A\<close> by (simp add: mono_on_def)
   139   have "\<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}. \<exists>q :: nat \<times> rat.
   140       (fst q = 0 \<and> of_rat (snd q) < f a \<and> (\<forall>x \<in> A. x < a \<longrightarrow> f x < of_rat (snd q))) \<or>
   141       (fst q = 1 \<and> of_rat (snd q) > f a \<and> (\<forall>x \<in> A. x > a \<longrightarrow> f x > of_rat (snd q)))"
   142   proof (clarsimp simp del: One_nat_def)
   143     fix a assume "a \<in> A" assume "\<not> continuous (at a within A) f"
   144     thus "\<exists>q1 q2.
   145             q1 = 0 \<and> real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2) \<or>
   146             q1 = 1 \<and> f a < real_of_rat q2 \<and> (\<forall>x\<in>A. a < x \<longrightarrow> real_of_rat q2 < f x)"
   147     proof (auto simp add: continuous_within order_tendsto_iff eventually_at)
   148       fix l assume "l < f a"
   149       then obtain q2 where q2: "l < of_rat q2" "of_rat q2 < f a"
   150         using of_rat_dense by blast
   151       assume * [rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> l < f x"
   152       from q2 have "real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2)"
   153       proof auto
   154         fix x assume "x \<in> A" "x < a"
   155         with q2 *[of "a - x"] show "f x < real_of_rat q2"
   156           apply (auto simp add: dist_real_def not_less)
   157           apply (subgoal_tac "f x \<le> f xa")
   158           by (auto intro: mono)
   159       qed
   160       thus ?thesis by auto
   161     next
   162       fix u assume "u > f a"
   163       then obtain q2 where q2: "f a < of_rat q2" "of_rat q2 < u"
   164         using of_rat_dense by blast
   165       assume *[rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> u > f x"
   166       from q2 have "real_of_rat q2 > f a \<and> (\<forall>x\<in>A. x > a \<longrightarrow> f x > real_of_rat q2)"
   167       proof auto
   168         fix x assume "x \<in> A" "x > a"
   169         with q2 *[of "x - a"] show "f x > real_of_rat q2"
   170           apply (auto simp add: dist_real_def)
   171           apply (subgoal_tac "f x \<ge> f xa")
   172           by (auto intro: mono)
   173       qed
   174       thus ?thesis by auto
   175     qed
   176   qed
   177   hence "\<exists>g :: real \<Rightarrow> nat \<times> rat . \<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}.
   178       (fst (g a) = 0 \<and> of_rat (snd (g a)) < f a \<and> (\<forall>x \<in> A. x < a \<longrightarrow> f x < of_rat (snd (g a)))) |
   179       (fst (g a) = 1 \<and> of_rat (snd (g a)) > f a \<and> (\<forall>x \<in> A. x > a \<longrightarrow> f x > of_rat (snd (g a))))"
   180     by (rule bchoice)
   181   then guess g ..
   182   hence g: "\<And>a x. a \<in> A \<Longrightarrow> \<not> continuous (at a within A) f \<Longrightarrow> x \<in> A \<Longrightarrow>
   183       (fst (g a) = 0 \<and> of_rat (snd (g a)) < f a \<and> (x < a \<longrightarrow> f x < of_rat (snd (g a)))) |
   184       (fst (g a) = 1 \<and> of_rat (snd (g a)) > f a \<and> (x > a \<longrightarrow> f x > of_rat (snd (g a))))"
   185     by auto
   186   have "inj_on g {a\<in>A. \<not> continuous (at a within A) f}"
   187   proof (auto simp add: inj_on_def)
   188     fix w z
   189     assume 1: "w \<in> A" and 2: "\<not> continuous (at w within A) f" and
   190            3: "z \<in> A" and 4: "\<not> continuous (at z within A) f" and
   191            5: "g w = g z"
   192     from g [OF 1 2 3] g [OF 3 4 1] 5
   193     show "w = z" by auto
   194   qed
   195   thus ?thesis
   196     by (rule countableI')
   197 qed
   198 
   199 lemma mono_on_ctble_discont_open:
   200   fixes f :: "real \<Rightarrow> real"
   201   fixes A :: "real set"
   202   assumes "open A" "mono_on f A"
   203   shows "countable {a\<in>A. \<not>isCont f a}"
   204 proof -
   205   have "{a\<in>A. \<not>isCont f a} = {a\<in>A. \<not>(continuous (at a within A) f)}"
   206     by (auto simp add: continuous_within_open [OF _ \<open>open A\<close>])
   207   thus ?thesis
   208     apply (elim ssubst)
   209     by (rule mono_on_ctble_discont, rule assms)
   210 qed
   211 
   212 lemma mono_ctble_discont:
   213   fixes f :: "real \<Rightarrow> real"
   214   assumes "mono f"
   215   shows "countable {a. \<not> isCont f a}"
   216   using assms mono_on_ctble_discont [of f UNIV] unfolding mono_on_def mono_def by auto
   217 
   218 lemma has_real_derivative_imp_continuous_on:
   219   assumes "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
   220   shows "continuous_on A f"
   221   apply (intro differentiable_imp_continuous_on, unfold differentiable_on_def)
   222   using assms differentiable_at_withinI real_differentiable_def by blast
   223 
   224 lemma closure_contains_Sup:
   225   fixes S :: "real set"
   226   assumes "S \<noteq> {}" "bdd_above S"
   227   shows "Sup S \<in> closure S"
   228 proof -
   229   have "Inf (uminus ` S) \<in> closure (uminus ` S)"
   230       using assms by (intro closure_contains_Inf) auto
   231   also have "Inf (uminus ` S) = -Sup S" by (simp add: Inf_real_def image_comp)
   232   also have "closure (uminus ` S) = uminus ` closure S"
   233       by (rule sym, intro closure_injective_linear_image) (auto intro: linearI)
   234   finally show ?thesis by auto
   235 qed
   236 
   237 lemma closed_contains_Sup:
   238   fixes S :: "real set"
   239   shows "S \<noteq> {} \<Longrightarrow> bdd_above S \<Longrightarrow> closed S \<Longrightarrow> Sup S \<in> S"
   240   by (subst closure_closed[symmetric], assumption, rule closure_contains_Sup)
   241 
   242 lemma closed_subset_contains_Sup:
   243   fixes A C :: "real set"
   244   shows "closed C \<Longrightarrow> A \<subseteq> C \<Longrightarrow> A \<noteq> {} \<Longrightarrow> bdd_above A \<Longrightarrow> Sup A \<in> C"
   245   by (metis closure_contains_Sup closure_minimal subset_eq)
   246 
   247 proposition  deriv_nonneg_imp_mono:
   248   assumes deriv: "\<And>x. x \<in> {a..b} \<Longrightarrow> (g has_real_derivative g' x) (at x)"
   249   assumes nonneg: "\<And>x. x \<in> {a..b} \<Longrightarrow> g' x \<ge> 0"
   250   assumes ab: "a \<le> b"
   251   shows "g a \<le> g b"
   252 proof (cases "a < b")
   253   assume "a < b"
   254   from deriv have "\<And>x. \<lbrakk>x \<ge> a; x \<le> b\<rbrakk> \<Longrightarrow> (g has_real_derivative g' x) (at x)" by simp
   255   with MVT2[OF \<open>a < b\<close>] and deriv
   256     obtain \<xi> where \<xi>_ab: "\<xi> > a" "\<xi> < b" and g_ab: "g b - g a = (b - a) * g' \<xi>" by blast
   257   from \<xi>_ab ab nonneg have "(b - a) * g' \<xi> \<ge> 0" by simp
   258   with g_ab show ?thesis by simp
   259 qed (insert ab, simp)
   260 
   261 lemma continuous_interval_vimage_Int:
   262   assumes "continuous_on {a::real..b} g" and mono: "\<And>x y. a \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> y \<le> b \<Longrightarrow> g x \<le> g y"
   263   assumes "a \<le> b" "(c::real) \<le> d" "{c..d} \<subseteq> {g a..g b}"
   264   obtains c' d' where "{a..b} \<inter> g -` {c..d} = {c'..d'}" "c' \<le> d'" "g c' = c" "g d' = d"
   265 proof-
   266   let ?A = "{a..b} \<inter> g -` {c..d}"
   267   from IVT'[of g a c b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5)
   268   obtain c'' where c'': "c'' \<in> ?A" "g c'' = c" by auto
   269   from IVT'[of g a d b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5)
   270   obtain d'' where d'': "d'' \<in> ?A" "g d'' = d" by auto
   271   hence [simp]: "?A \<noteq> {}" by blast
   272 
   273   define c' where "c' = Inf ?A"
   274   define d' where "d' = Sup ?A"
   275   have "?A \<subseteq> {c'..d'}" unfolding c'_def d'_def
   276     by (intro subsetI) (auto intro: cInf_lower cSup_upper)
   277   moreover from assms have "closed ?A"
   278     using continuous_on_closed_vimage[of "{a..b}" g] by (subst Int_commute) simp
   279   hence c'd'_in_set: "c' \<in> ?A" "d' \<in> ?A" unfolding c'_def d'_def
   280     by ((intro closed_contains_Inf closed_contains_Sup, simp_all)[])+
   281   hence "{c'..d'} \<subseteq> ?A" using assms
   282     by (intro subsetI)
   283        (auto intro!: order_trans[of c "g c'" "g x" for x] order_trans[of "g x" "g d'" d for x]
   284              intro!: mono)
   285   moreover have "c' \<le> d'" using c'd'_in_set(2) unfolding c'_def by (intro cInf_lower) auto
   286   moreover have "g c' \<le> c" "g d' \<ge> d"
   287     apply (insert c'' d'' c'd'_in_set)
   288     apply (subst c''(2)[symmetric])
   289     apply (auto simp: c'_def intro!: mono cInf_lower c'') []
   290     apply (subst d''(2)[symmetric])
   291     apply (auto simp: d'_def intro!: mono cSup_upper d'') []
   292     done
   293   with c'd'_in_set have "g c' = c" "g d' = d" by auto
   294   ultimately show ?thesis using that by blast
   295 qed
   296 
   297 subsection \<open>Generic Borel spaces\<close>
   298 
   299 definition%important (in topological_space) borel :: "'a measure" where
   300   "borel = sigma UNIV {S. open S}"
   301 
   302 abbreviation "borel_measurable M \<equiv> measurable M borel"
   303 
   304 lemma in_borel_measurable:
   305    "f \<in> borel_measurable M \<longleftrightarrow>
   306     (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
   307   by (auto simp add: measurable_def borel_def)
   308 
   309 lemma in_borel_measurable_borel:
   310    "f \<in> borel_measurable M \<longleftrightarrow>
   311     (\<forall>S \<in> sets borel.
   312       f -` S \<inter> space M \<in> sets M)"
   313   by (auto simp add: measurable_def borel_def)
   314 
   315 lemma space_borel[simp]: "space borel = UNIV"
   316   unfolding borel_def by auto
   317 
   318 lemma space_in_borel[measurable]: "UNIV \<in> sets borel"
   319   unfolding borel_def by auto
   320 
   321 lemma sets_borel: "sets borel = sigma_sets UNIV {S. open S}"
   322   unfolding borel_def by (rule sets_measure_of) simp
   323 
   324 lemma measurable_sets_borel:
   325     "\<lbrakk>f \<in> measurable borel M; A \<in> sets M\<rbrakk> \<Longrightarrow> f -` A \<in> sets borel"
   326   by (drule (1) measurable_sets) simp
   327 
   328 lemma pred_Collect_borel[measurable (raw)]: "Measurable.pred borel P \<Longrightarrow> {x. P x} \<in> sets borel"
   329   unfolding borel_def pred_def by auto
   330 
   331 lemma borel_open[measurable (raw generic)]:
   332   assumes "open A" shows "A \<in> sets borel"
   333 proof -
   334   have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
   335   thus ?thesis unfolding borel_def by auto
   336 qed
   337 
   338 lemma borel_closed[measurable (raw generic)]:
   339   assumes "closed A" shows "A \<in> sets borel"
   340 proof -
   341   have "space borel - (- A) \<in> sets borel"
   342     using assms unfolding closed_def by (blast intro: borel_open)
   343   thus ?thesis by simp
   344 qed
   345 
   346 lemma borel_singleton[measurable]:
   347   "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets (borel :: 'a::t1_space measure)"
   348   unfolding insert_def by (rule sets.Un) auto
   349 
   350 lemma sets_borel_eq_count_space: "sets (borel :: 'a::{countable, t2_space} measure) = count_space UNIV"
   351 proof -
   352   have "(\<Union>a\<in>A. {a}) \<in> sets borel" for A :: "'a set"
   353     by (intro sets.countable_UN') auto
   354   then show ?thesis
   355     by auto
   356 qed
   357 
   358 lemma borel_comp[measurable]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
   359   unfolding Compl_eq_Diff_UNIV by simp
   360 
   361 lemma borel_measurable_vimage:
   362   fixes f :: "'a \<Rightarrow> 'x::t2_space"
   363   assumes borel[measurable]: "f \<in> borel_measurable M"
   364   shows "f -` {x} \<inter> space M \<in> sets M"
   365   by simp
   366 
   367 lemma borel_measurableI:
   368   fixes f :: "'a \<Rightarrow> 'x::topological_space"
   369   assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
   370   shows "f \<in> borel_measurable M"
   371   unfolding borel_def
   372 proof (rule measurable_measure_of, simp_all)
   373   fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
   374     using assms[of S] by simp
   375 qed
   376 
   377 lemma borel_measurable_const:
   378   "(\<lambda>x. c) \<in> borel_measurable M"
   379   by auto
   380 
   381 lemma borel_measurable_indicator:
   382   assumes A: "A \<in> sets M"
   383   shows "indicator A \<in> borel_measurable M"
   384   unfolding indicator_def [abs_def] using A
   385   by (auto intro!: measurable_If_set)
   386 
   387 lemma borel_measurable_count_space[measurable (raw)]:
   388   "f \<in> borel_measurable (count_space S)"
   389   unfolding measurable_def by auto
   390 
   391 lemma borel_measurable_indicator'[measurable (raw)]:
   392   assumes [measurable]: "{x\<in>space M. f x \<in> A x} \<in> sets M"
   393   shows "(\<lambda>x. indicator (A x) (f x)) \<in> borel_measurable M"
   394   unfolding indicator_def[abs_def]
   395   by (auto intro!: measurable_If)
   396 
   397 lemma borel_measurable_indicator_iff:
   398   "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
   399     (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
   400 proof
   401   assume "?I \<in> borel_measurable M"
   402   then have "?I -` {1} \<inter> space M \<in> sets M"
   403     unfolding measurable_def by auto
   404   also have "?I -` {1} \<inter> space M = A \<inter> space M"
   405     unfolding indicator_def [abs_def] by auto
   406   finally show "A \<inter> space M \<in> sets M" .
   407 next
   408   assume "A \<inter> space M \<in> sets M"
   409   moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   410     (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   411     by (intro measurable_cong) (auto simp: indicator_def)
   412   ultimately show "?I \<in> borel_measurable M" by auto
   413 qed
   414 
   415 lemma borel_measurable_subalgebra:
   416   assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   417   shows "f \<in> borel_measurable M"
   418   using assms unfolding measurable_def by auto
   419 
   420 lemma borel_measurable_restrict_space_iff_ereal:
   421   fixes f :: "'a \<Rightarrow> ereal"
   422   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   423   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   424     (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
   425   by (subst measurable_restrict_space_iff)
   426      (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_weak_cong)
   427 
   428 lemma borel_measurable_restrict_space_iff_ennreal:
   429   fixes f :: "'a \<Rightarrow> ennreal"
   430   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   431   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   432     (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
   433   by (subst measurable_restrict_space_iff)
   434      (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_weak_cong)
   435 
   436 lemma borel_measurable_restrict_space_iff:
   437   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   438   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   439   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   440     (\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> borel_measurable M"
   441   by (subst measurable_restrict_space_iff)
   442      (auto simp: indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a] ac_simps
   443        cong del: if_weak_cong)
   444 
   445 lemma cbox_borel[measurable]: "cbox a b \<in> sets borel"
   446   by (auto intro: borel_closed)
   447 
   448 lemma box_borel[measurable]: "box a b \<in> sets borel"
   449   by (auto intro: borel_open)
   450 
   451 lemma borel_compact: "compact (A::'a::t2_space set) \<Longrightarrow> A \<in> sets borel"
   452   by (auto intro: borel_closed dest!: compact_imp_closed)
   453 
   454 lemma borel_sigma_sets_subset:
   455   "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
   456   using sets.sigma_sets_subset[of A borel] by simp
   457 
   458 lemma borel_eq_sigmaI1:
   459   fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   460   assumes borel_eq: "borel = sigma UNIV X"
   461   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (F ` A))"
   462   assumes F: "\<And>i. i \<in> A \<Longrightarrow> F i \<in> sets borel"
   463   shows "borel = sigma UNIV (F ` A)"
   464   unfolding borel_def
   465 proof (intro sigma_eqI antisym)
   466   have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
   467     unfolding borel_def by simp
   468   also have "\<dots> = sigma_sets UNIV X"
   469     unfolding borel_eq by simp
   470   also have "\<dots> \<subseteq> sigma_sets UNIV (F`A)"
   471     using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
   472   finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (F`A)" .
   473   show "sigma_sets UNIV (F`A) \<subseteq> sigma_sets UNIV {S. open S}"
   474     unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
   475 qed auto
   476 
   477 lemma borel_eq_sigmaI2:
   478   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
   479     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   480   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`B)"
   481   assumes X: "\<And>i j. (i, j) \<in> B \<Longrightarrow> G i j \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   482   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   483   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   484   using assms
   485   by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` B" and F="(\<lambda>(i, j). F i j)"]) auto
   486 
   487 lemma borel_eq_sigmaI3:
   488   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   489   assumes borel_eq: "borel = sigma UNIV X"
   490   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   491   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   492   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   493   using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
   494 
   495 lemma borel_eq_sigmaI4:
   496   fixes F :: "'i \<Rightarrow> 'a::topological_space set"
   497     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   498   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`A)"
   499   assumes X: "\<And>i j. (i, j) \<in> A \<Longrightarrow> G i j \<in> sets (sigma UNIV (range F))"
   500   assumes F: "\<And>i. F i \<in> sets borel"
   501   shows "borel = sigma UNIV (range F)"
   502   using assms by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` A" and F=F]) auto
   503 
   504 lemma borel_eq_sigmaI5:
   505   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
   506   assumes borel_eq: "borel = sigma UNIV (range G)"
   507   assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   508   assumes F: "\<And>i j. F i j \<in> sets borel"
   509   shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   510   using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
   511 
   512 theorem second_countable_borel_measurable:
   513   fixes X :: "'a::second_countable_topology set set"
   514   assumes eq: "open = generate_topology X"
   515   shows "borel = sigma UNIV X"
   516   unfolding borel_def
   517 proof (intro sigma_eqI sigma_sets_eqI)
   518   interpret X: sigma_algebra UNIV "sigma_sets UNIV X"
   519     by (rule sigma_algebra_sigma_sets) simp
   520 
   521   fix S :: "'a set" assume "S \<in> Collect open"
   522   then have "generate_topology X S"
   523     by (auto simp: eq)
   524   then show "S \<in> sigma_sets UNIV X"
   525   proof induction
   526     case (UN K)
   527     then have K: "\<And>k. k \<in> K \<Longrightarrow> open k"
   528       unfolding eq by auto
   529     from ex_countable_basis obtain B :: "'a set set" where
   530       B:  "\<And>b. b \<in> B \<Longrightarrow> open b" "\<And>X. open X \<Longrightarrow> \<exists>b\<subseteq>B. (\<Union>b) = X" and "countable B"
   531       by (auto simp: topological_basis_def)
   532     from B(2)[OF K] obtain m where m: "\<And>k. k \<in> K \<Longrightarrow> m k \<subseteq> B" "\<And>k. k \<in> K \<Longrightarrow> \<Union>(m k) = k"
   533       by metis
   534     define U where "U = (\<Union>k\<in>K. m k)"
   535     with m have "countable U"
   536       by (intro countable_subset[OF _ \<open>countable B\<close>]) auto
   537     have "\<Union>U = (\<Union>A\<in>U. A)" by simp
   538     also have "\<dots> = \<Union>K"
   539       unfolding U_def UN_simps by (simp add: m)
   540     finally have "\<Union>U = \<Union>K" .
   541 
   542     have "\<forall>b\<in>U. \<exists>k\<in>K. b \<subseteq> k"
   543       using m by (auto simp: U_def)
   544     then obtain u where u: "\<And>b. b \<in> U \<Longrightarrow> u b \<in> K" and "\<And>b. b \<in> U \<Longrightarrow> b \<subseteq> u b"
   545       by metis
   546     then have "(\<Union>b\<in>U. u b) \<subseteq> \<Union>K" "\<Union>U \<subseteq> (\<Union>b\<in>U. u b)"
   547       by auto
   548     then have "\<Union>K = (\<Union>b\<in>U. u b)"
   549       unfolding \<open>\<Union>U = \<Union>K\<close> by auto
   550     also have "\<dots> \<in> sigma_sets UNIV X"
   551       using u UN by (intro X.countable_UN' \<open>countable U\<close>) auto
   552     finally show "\<Union>K \<in> sigma_sets UNIV X" .
   553   qed auto
   554 qed (auto simp: eq intro: generate_topology.Basis)
   555 
   556 lemma borel_eq_closed: "borel = sigma UNIV (Collect closed)"
   557   unfolding borel_def
   558 proof (intro sigma_eqI sigma_sets_eqI, safe)
   559   fix x :: "'a set" assume "open x"
   560   hence "x = UNIV - (UNIV - x)" by auto
   561   also have "\<dots> \<in> sigma_sets UNIV (Collect closed)"
   562     by (force intro: sigma_sets.Compl simp: \<open>open x\<close>)
   563   finally show "x \<in> sigma_sets UNIV (Collect closed)" by simp
   564 next
   565   fix x :: "'a set" assume "closed x"
   566   hence "x = UNIV - (UNIV - x)" by auto
   567   also have "\<dots> \<in> sigma_sets UNIV (Collect open)"
   568     by (force intro: sigma_sets.Compl simp: \<open>closed x\<close>)
   569   finally show "x \<in> sigma_sets UNIV (Collect open)" by simp
   570 qed simp_all
   571 
   572 proposition borel_eq_countable_basis:
   573   fixes B::"'a::topological_space set set"
   574   assumes "countable B"
   575   assumes "topological_basis B"
   576   shows "borel = sigma UNIV B"
   577   unfolding borel_def
   578 proof (intro sigma_eqI sigma_sets_eqI, safe)
   579   interpret countable_basis "open" B using assms by (rule countable_basis_openI)
   580   fix X::"'a set" assume "open X"
   581   from open_countable_basisE[OF this] obtain B' where B': "B' \<subseteq> B" "X = \<Union> B'" .
   582   then show "X \<in> sigma_sets UNIV B"
   583     by (blast intro: sigma_sets_UNION \<open>countable B\<close> countable_subset)
   584 next
   585   fix b assume "b \<in> B"
   586   hence "open b" by (rule topological_basis_open[OF assms(2)])
   587   thus "b \<in> sigma_sets UNIV (Collect open)" by auto
   588 qed simp_all
   589 
   590 lemma borel_measurable_continuous_on_restrict:
   591   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   592   assumes f: "continuous_on A f"
   593   shows "f \<in> borel_measurable (restrict_space borel A)"
   594 proof (rule borel_measurableI)
   595   fix S :: "'b set" assume "open S"
   596   with f obtain T where "f -` S \<inter> A = T \<inter> A" "open T"
   597     by (metis continuous_on_open_invariant)
   598   then show "f -` S \<inter> space (restrict_space borel A) \<in> sets (restrict_space borel A)"
   599     by (force simp add: sets_restrict_space space_restrict_space)
   600 qed
   601 
   602 lemma borel_measurable_continuous_on1: "continuous_on UNIV f \<Longrightarrow> f \<in> borel_measurable borel"
   603   by (drule borel_measurable_continuous_on_restrict) simp
   604 
   605 lemma borel_measurable_continuous_on_if:
   606   "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> continuous_on (- A) g \<Longrightarrow>
   607     (\<lambda>x. if x \<in> A then f x else g x) \<in> borel_measurable borel"
   608   by (auto simp add: measurable_If_restrict_space_iff Collect_neg_eq
   609            intro!: borel_measurable_continuous_on_restrict)
   610 
   611 lemma borel_measurable_continuous_countable_exceptions:
   612   fixes f :: "'a::t1_space \<Rightarrow> 'b::topological_space"
   613   assumes X: "countable X"
   614   assumes "continuous_on (- X) f"
   615   shows "f \<in> borel_measurable borel"
   616 proof (rule measurable_discrete_difference[OF _ X])
   617   have "X \<in> sets borel"
   618     by (rule sets.countable[OF _ X]) auto
   619   then show "(\<lambda>x. if x \<in> X then undefined else f x) \<in> borel_measurable borel"
   620     by (intro borel_measurable_continuous_on_if assms continuous_intros)
   621 qed auto
   622 
   623 lemma borel_measurable_continuous_on:
   624   assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
   625   shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
   626   using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
   627 
   628 lemma borel_measurable_continuous_on_indicator:
   629   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
   630   shows "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> (\<lambda>x. indicator A x *\<^sub>R f x) \<in> borel_measurable borel"
   631   by (subst borel_measurable_restrict_space_iff[symmetric])
   632      (auto intro: borel_measurable_continuous_on_restrict)
   633 
   634 lemma borel_measurable_Pair[measurable (raw)]:
   635   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   636   assumes f[measurable]: "f \<in> borel_measurable M"
   637   assumes g[measurable]: "g \<in> borel_measurable M"
   638   shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
   639 proof (subst borel_eq_countable_basis)
   640   let ?B = "SOME B::'b set set. countable B \<and> topological_basis B"
   641   let ?C = "SOME B::'c set set. countable B \<and> topological_basis B"
   642   let ?P = "(\<lambda>(b, c). b \<times> c) ` (?B \<times> ?C)"
   643   show "countable ?P" "topological_basis ?P"
   644     by (auto intro!: countable_basis topological_basis_prod is_basis)
   645 
   646   show "(\<lambda>x. (f x, g x)) \<in> measurable M (sigma UNIV ?P)"
   647   proof (rule measurable_measure_of)
   648     fix S assume "S \<in> ?P"
   649     then obtain b c where "b \<in> ?B" "c \<in> ?C" and S: "S = b \<times> c" by auto
   650     then have borel: "open b" "open c"
   651       by (auto intro: is_basis topological_basis_open)
   652     have "(\<lambda>x. (f x, g x)) -` S \<inter> space M = (f -` b \<inter> space M) \<inter> (g -` c \<inter> space M)"
   653       unfolding S by auto
   654     also have "\<dots> \<in> sets M"
   655       using borel by simp
   656     finally show "(\<lambda>x. (f x, g x)) -` S \<inter> space M \<in> sets M" .
   657   qed auto
   658 qed
   659 
   660 lemma borel_measurable_continuous_Pair:
   661   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   662   assumes [measurable]: "f \<in> borel_measurable M"
   663   assumes [measurable]: "g \<in> borel_measurable M"
   664   assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
   665   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   666 proof -
   667   have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
   668   show ?thesis
   669     unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
   670 qed
   671 
   672 subsection \<open>Borel spaces on order topologies\<close>
   673 
   674 lemma [measurable]:
   675   fixes a b :: "'a::linorder_topology"
   676   shows lessThan_borel: "{..< a} \<in> sets borel"
   677     and greaterThan_borel: "{a <..} \<in> sets borel"
   678     and greaterThanLessThan_borel: "{a<..<b} \<in> sets borel"
   679     and atMost_borel: "{..a} \<in> sets borel"
   680     and atLeast_borel: "{a..} \<in> sets borel"
   681     and atLeastAtMost_borel: "{a..b} \<in> sets borel"
   682     and greaterThanAtMost_borel: "{a<..b} \<in> sets borel"
   683     and atLeastLessThan_borel: "{a..<b} \<in> sets borel"
   684   unfolding greaterThanAtMost_def atLeastLessThan_def
   685   by (blast intro: borel_open borel_closed open_lessThan open_greaterThan open_greaterThanLessThan
   686                    closed_atMost closed_atLeast closed_atLeastAtMost)+
   687 
   688 lemma borel_Iio:
   689   "borel = sigma UNIV (range lessThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   690   unfolding second_countable_borel_measurable[OF open_generated_order]
   691 proof (intro sigma_eqI sigma_sets_eqI)
   692   from countable_dense_setE guess D :: "'a set" . note D = this
   693 
   694   interpret L: sigma_algebra UNIV "sigma_sets UNIV (range lessThan)"
   695     by (rule sigma_algebra_sigma_sets) simp
   696 
   697   fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   698   then obtain y where "A = {y <..} \<or> A = {..< y}"
   699     by blast
   700   then show "A \<in> sigma_sets UNIV (range lessThan)"
   701   proof
   702     assume A: "A = {y <..}"
   703     show ?thesis
   704     proof cases
   705       assume "\<forall>x>y. \<exists>d. y < d \<and> d < x"
   706       with D(2)[of "{y <..< x}" for x] have "\<forall>x>y. \<exists>d\<in>D. y < d \<and> d < x"
   707         by (auto simp: set_eq_iff)
   708       then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. y < d}. {..< d})"
   709         by (auto simp: A) (metis less_asym)
   710       also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   711         using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   712       finally show ?thesis .
   713     next
   714       assume "\<not> (\<forall>x>y. \<exists>d. y < d \<and> d < x)"
   715       then obtain x where "y < x"  "\<And>d. y < d \<Longrightarrow> \<not> d < x"
   716         by auto
   717       then have "A = UNIV - {..< x}"
   718         unfolding A by (auto simp: not_less[symmetric])
   719       also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   720         by auto
   721       finally show ?thesis .
   722     qed
   723   qed auto
   724 qed auto
   725 
   726 lemma borel_Ioi:
   727   "borel = sigma UNIV (range greaterThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   728   unfolding second_countable_borel_measurable[OF open_generated_order]
   729 proof (intro sigma_eqI sigma_sets_eqI)
   730   from countable_dense_setE guess D :: "'a set" . note D = this
   731 
   732   interpret L: sigma_algebra UNIV "sigma_sets UNIV (range greaterThan)"
   733     by (rule sigma_algebra_sigma_sets) simp
   734 
   735   fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   736   then obtain y where "A = {y <..} \<or> A = {..< y}"
   737     by blast
   738   then show "A \<in> sigma_sets UNIV (range greaterThan)"
   739   proof
   740     assume A: "A = {..< y}"
   741     show ?thesis
   742     proof cases
   743       assume "\<forall>x<y. \<exists>d. x < d \<and> d < y"
   744       with D(2)[of "{x <..< y}" for x] have "\<forall>x<y. \<exists>d\<in>D. x < d \<and> d < y"
   745         by (auto simp: set_eq_iff)
   746       then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. d < y}. {d <..})"
   747         by (auto simp: A) (metis less_asym)
   748       also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   749         using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   750       finally show ?thesis .
   751     next
   752       assume "\<not> (\<forall>x<y. \<exists>d. x < d \<and> d < y)"
   753       then obtain x where "x < y"  "\<And>d. y > d \<Longrightarrow> x \<ge> d"
   754         by (auto simp: not_less[symmetric])
   755       then have "A = UNIV - {x <..}"
   756         unfolding A Compl_eq_Diff_UNIV[symmetric] by auto
   757       also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   758         by auto
   759       finally show ?thesis .
   760     qed
   761   qed auto
   762 qed auto
   763 
   764 lemma borel_measurableI_less:
   765   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   766   shows "(\<And>y. {x\<in>space M. f x < y} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   767   unfolding borel_Iio
   768   by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   769 
   770 lemma borel_measurableI_greater:
   771   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   772   shows "(\<And>y. {x\<in>space M. y < f x} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   773   unfolding borel_Ioi
   774   by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   775 
   776 lemma borel_measurableI_le:
   777   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   778   shows "(\<And>y. {x\<in>space M. f x \<le> y} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   779   by (rule borel_measurableI_greater) (auto simp: not_le[symmetric])
   780 
   781 lemma borel_measurableI_ge:
   782   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   783   shows "(\<And>y. {x\<in>space M. y \<le> f x} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   784   by (rule borel_measurableI_less) (auto simp: not_le[symmetric])
   785 
   786 lemma borel_measurable_less[measurable]:
   787   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
   788   assumes "f \<in> borel_measurable M"
   789   assumes "g \<in> borel_measurable M"
   790   shows "{w \<in> space M. f w < g w} \<in> sets M"
   791 proof -
   792   have "{w \<in> space M. f w < g w} = (\<lambda>x. (f x, g x)) -` {x. fst x < snd x} \<inter> space M"
   793     by auto
   794   also have "\<dots> \<in> sets M"
   795     by (intro measurable_sets[OF borel_measurable_Pair borel_open, OF assms open_Collect_less]
   796               continuous_intros)
   797   finally show ?thesis .
   798 qed
   799 
   800 lemma
   801   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
   802   assumes f[measurable]: "f \<in> borel_measurable M"
   803   assumes g[measurable]: "g \<in> borel_measurable M"
   804   shows borel_measurable_le[measurable]: "{w \<in> space M. f w \<le> g w} \<in> sets M"
   805     and borel_measurable_eq[measurable]: "{w \<in> space M. f w = g w} \<in> sets M"
   806     and borel_measurable_neq: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   807   unfolding eq_iff not_less[symmetric]
   808   by measurable
   809 
   810 lemma borel_measurable_SUP[measurable (raw)]:
   811   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   812   assumes [simp]: "countable I"
   813   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   814   shows "(\<lambda>x. SUP i\<in>I. F i x) \<in> borel_measurable M"
   815   by (rule borel_measurableI_greater) (simp add: less_SUP_iff)
   816 
   817 lemma borel_measurable_INF[measurable (raw)]:
   818   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   819   assumes [simp]: "countable I"
   820   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   821   shows "(\<lambda>x. INF i\<in>I. F i x) \<in> borel_measurable M"
   822   by (rule borel_measurableI_less) (simp add: INF_less_iff)
   823 
   824 lemma borel_measurable_cSUP[measurable (raw)]:
   825   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology, second_countable_topology}"
   826   assumes [simp]: "countable I"
   827   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   828   assumes bdd: "\<And>x. x \<in> space M \<Longrightarrow> bdd_above ((\<lambda>i. F i x) ` I)"
   829   shows "(\<lambda>x. SUP i\<in>I. F i x) \<in> borel_measurable M"
   830 proof cases
   831   assume "I = {}" then show ?thesis
   832     unfolding \<open>I = {}\<close> image_empty by simp
   833 next
   834   assume "I \<noteq> {}"
   835   show ?thesis
   836   proof (rule borel_measurableI_le)
   837     fix y
   838     have "{x \<in> space M. \<forall>i\<in>I. F i x \<le> y} \<in> sets M"
   839       by measurable
   840     also have "{x \<in> space M. \<forall>i\<in>I. F i x \<le> y} = {x \<in> space M. (SUP i\<in>I. F i x) \<le> y}"
   841       by (simp add: cSUP_le_iff \<open>I \<noteq> {}\<close> bdd cong: conj_cong)
   842     finally show "{x \<in> space M. (SUP i\<in>I. F i x) \<le>  y} \<in> sets M"  .
   843   qed
   844 qed
   845 
   846 lemma borel_measurable_cINF[measurable (raw)]:
   847   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology, second_countable_topology}"
   848   assumes [simp]: "countable I"
   849   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   850   assumes bdd: "\<And>x. x \<in> space M \<Longrightarrow> bdd_below ((\<lambda>i. F i x) ` I)"
   851   shows "(\<lambda>x. INF i\<in>I. F i x) \<in> borel_measurable M"
   852 proof cases
   853   assume "I = {}" then show ?thesis
   854     unfolding \<open>I = {}\<close> image_empty by simp
   855 next
   856   assume "I \<noteq> {}"
   857   show ?thesis
   858   proof (rule borel_measurableI_ge)
   859     fix y
   860     have "{x \<in> space M. \<forall>i\<in>I. y \<le> F i x} \<in> sets M"
   861       by measurable
   862     also have "{x \<in> space M. \<forall>i\<in>I. y \<le> F i x} = {x \<in> space M. y \<le> (INF i\<in>I. F i x)}"
   863       by (simp add: le_cINF_iff \<open>I \<noteq> {}\<close> bdd cong: conj_cong)
   864     finally show "{x \<in> space M. y \<le> (INF i\<in>I. F i x)} \<in> sets M"  .
   865   qed
   866 qed
   867 
   868 lemma borel_measurable_lfp[consumes 1, case_names continuity step]:
   869   fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   870   assumes "sup_continuous F"
   871   assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   872   shows "lfp F \<in> borel_measurable M"
   873 proof -
   874   { fix i have "((F ^^ i) bot) \<in> borel_measurable M"
   875       by (induct i) (auto intro!: *) }
   876   then have "(\<lambda>x. SUP i. (F ^^ i) bot x) \<in> borel_measurable M"
   877     by measurable
   878   also have "(\<lambda>x. SUP i. (F ^^ i) bot x) = (SUP i. (F ^^ i) bot)"
   879     by (auto simp add: image_comp)
   880   also have "(SUP i. (F ^^ i) bot) = lfp F"
   881     by (rule sup_continuous_lfp[symmetric]) fact
   882   finally show ?thesis .
   883 qed
   884 
   885 lemma borel_measurable_gfp[consumes 1, case_names continuity step]:
   886   fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   887   assumes "inf_continuous F"
   888   assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   889   shows "gfp F \<in> borel_measurable M"
   890 proof -
   891   { fix i have "((F ^^ i) top) \<in> borel_measurable M"
   892       by (induct i) (auto intro!: * simp: bot_fun_def) }
   893   then have "(\<lambda>x. INF i. (F ^^ i) top x) \<in> borel_measurable M"
   894     by measurable
   895   also have "(\<lambda>x. INF i. (F ^^ i) top x) = (INF i. (F ^^ i) top)"
   896     by (auto simp add: image_comp)
   897   also have "\<dots> = gfp F"
   898     by (rule inf_continuous_gfp[symmetric]) fact
   899   finally show ?thesis .
   900 qed
   901 
   902 lemma borel_measurable_max[measurable (raw)]:
   903   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. max (g x) (f x) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   904   by (rule borel_measurableI_less) simp
   905 
   906 lemma borel_measurable_min[measurable (raw)]:
   907   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. min (g x) (f x) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   908   by (rule borel_measurableI_greater) simp
   909 
   910 lemma borel_measurable_Min[measurable (raw)]:
   911   "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, linorder_topology}) \<in> borel_measurable M"
   912 proof (induct I rule: finite_induct)
   913   case (insert i I) then show ?case
   914     by (cases "I = {}") auto
   915 qed auto
   916 
   917 lemma borel_measurable_Max[measurable (raw)]:
   918   "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, linorder_topology}) \<in> borel_measurable M"
   919 proof (induct I rule: finite_induct)
   920   case (insert i I) then show ?case
   921     by (cases "I = {}") auto
   922 qed auto
   923 
   924 lemma borel_measurable_sup[measurable (raw)]:
   925   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. sup (g x) (f x) :: 'b::{lattice, second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   926   unfolding sup_max by measurable
   927 
   928 lemma borel_measurable_inf[measurable (raw)]:
   929   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. inf (g x) (f x) :: 'b::{lattice, second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   930   unfolding inf_min by measurable
   931 
   932 lemma [measurable (raw)]:
   933   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   934   assumes "\<And>i. f i \<in> borel_measurable M"
   935   shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   936     and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
   937   unfolding liminf_SUP_INF limsup_INF_SUP using assms by auto
   938 
   939 lemma measurable_convergent[measurable (raw)]:
   940   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   941   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
   942   shows "Measurable.pred M (\<lambda>x. convergent (\<lambda>i. f i x))"
   943   unfolding convergent_ereal by measurable
   944 
   945 lemma sets_Collect_convergent[measurable]:
   946   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   947   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
   948   shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
   949   by measurable
   950 
   951 lemma borel_measurable_lim[measurable (raw)]:
   952   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   953   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
   954   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
   955 proof -
   956   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))"
   957     by (simp add: lim_def convergent_def convergent_limsup_cl)
   958   then show ?thesis
   959     by simp
   960 qed
   961 
   962 lemma borel_measurable_LIMSEQ_order:
   963   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   964   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
   965   and u: "\<And>i. u i \<in> borel_measurable M"
   966   shows "u' \<in> borel_measurable M"
   967 proof -
   968   have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
   969     using u' by (simp add: lim_imp_Liminf[symmetric])
   970   with u show ?thesis by (simp cong: measurable_cong)
   971 qed
   972 
   973 subsection \<open>Borel spaces on topological monoids\<close>
   974 
   975 lemma borel_measurable_add[measurable (raw)]:
   976   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, topological_monoid_add}"
   977   assumes f: "f \<in> borel_measurable M"
   978   assumes g: "g \<in> borel_measurable M"
   979   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   980   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   981 
   982 lemma borel_measurable_sum[measurable (raw)]:
   983   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, topological_comm_monoid_add}"
   984   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   985   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   986 proof cases
   987   assume "finite S"
   988   thus ?thesis using assms by induct auto
   989 qed simp
   990 
   991 lemma borel_measurable_suminf_order[measurable (raw)]:
   992   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology, topological_comm_monoid_add}"
   993   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
   994   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   995   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
   996 
   997 subsection \<open>Borel spaces on Euclidean spaces\<close>
   998 
   999 lemma borel_measurable_inner[measurable (raw)]:
  1000   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_inner}"
  1001   assumes "f \<in> borel_measurable M"
  1002   assumes "g \<in> borel_measurable M"
  1003   shows "(\<lambda>x. f x \<bullet> g x) \<in> borel_measurable M"
  1004   using assms
  1005   by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1006 
  1007 notation
  1008   eucl_less (infix "<e" 50)
  1009 
  1010 lemma box_oc: "{x. a <e x \<and> x \<le> b} = {x. a <e x} \<inter> {..b}"
  1011   and box_co: "{x. a \<le> x \<and> x <e b} = {a..} \<inter> {x. x <e b}"
  1012   by auto
  1013 
  1014 lemma eucl_ivals[measurable]:
  1015   fixes a b :: "'a::ordered_euclidean_space"
  1016   shows "{x. x <e a} \<in> sets borel"
  1017     and "{x. a <e x} \<in> sets borel"
  1018     and "{..a} \<in> sets borel"
  1019     and "{a..} \<in> sets borel"
  1020     and "{a..b} \<in> sets borel"
  1021     and  "{x. a <e x \<and> x \<le> b} \<in> sets borel"
  1022     and "{x. a \<le> x \<and>  x <e b} \<in> sets borel"
  1023   unfolding box_oc box_co
  1024   by (auto intro: borel_open borel_closed)
  1025 
  1026 lemma
  1027   fixes i :: "'a::{second_countable_topology, real_inner}"
  1028   shows hafspace_less_borel: "{x. a < x \<bullet> i} \<in> sets borel"
  1029     and hafspace_greater_borel: "{x. x \<bullet> i < a} \<in> sets borel"
  1030     and hafspace_less_eq_borel: "{x. a \<le> x \<bullet> i} \<in> sets borel"
  1031     and hafspace_greater_eq_borel: "{x. x \<bullet> i \<le> a} \<in> sets borel"
  1032   by simp_all
  1033 
  1034 lemma borel_eq_box:
  1035   "borel = sigma UNIV (range (\<lambda> (a, b). box a b :: 'a :: euclidean_space set))"
  1036     (is "_ = ?SIGMA")
  1037 proof (rule borel_eq_sigmaI1[OF borel_def])
  1038   fix M :: "'a set" assume "M \<in> {S. open S}"
  1039   then have "open M" by simp
  1040   show "M \<in> ?SIGMA"
  1041     apply (subst open_UNION_box[OF \<open>open M\<close>])
  1042     apply (safe intro!: sets.countable_UN' countable_PiE countable_Collect)
  1043     apply (auto intro: countable_rat)
  1044     done
  1045 qed (auto simp: box_def)
  1046 
  1047 lemma halfspace_gt_in_halfspace:
  1048   assumes i: "i \<in> A"
  1049   shows "{x::'a. a < x \<bullet> i} \<in>
  1050     sigma_sets UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> A))"
  1051   (is "?set \<in> ?SIGMA")
  1052 proof -
  1053   interpret sigma_algebra UNIV ?SIGMA
  1054     by (intro sigma_algebra_sigma_sets) simp_all
  1055   have *: "?set = (\<Union>n. UNIV - {x::'a. x \<bullet> i < a + 1 / real (Suc n)})"
  1056   proof (safe, simp_all add: not_less del: of_nat_Suc)
  1057     fix x :: 'a assume "a < x \<bullet> i"
  1058     with reals_Archimedean[of "x \<bullet> i - a"]
  1059     obtain n where "a + 1 / real (Suc n) < x \<bullet> i"
  1060       by (auto simp: field_simps)
  1061     then show "\<exists>n. a + 1 / real (Suc n) \<le> x \<bullet> i"
  1062       by (blast intro: less_imp_le)
  1063   next
  1064     fix x n
  1065     have "a < a + 1 / real (Suc n)" by auto
  1066     also assume "\<dots> \<le> x"
  1067     finally show "a < x" .
  1068   qed
  1069   show "?set \<in> ?SIGMA" unfolding *
  1070     by (auto intro!: Diff sigma_sets_Inter i)
  1071 qed
  1072 
  1073 lemma borel_eq_halfspace_less:
  1074   "borel = sigma UNIV ((\<lambda>(a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> Basis))"
  1075   (is "_ = ?SIGMA")
  1076 proof (rule borel_eq_sigmaI2[OF borel_eq_box])
  1077   fix a b :: 'a
  1078   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}"
  1079     by (auto simp: box_def)
  1080   also have "\<dots> \<in> sets ?SIGMA"
  1081     by (intro sets.sets_Collect_conj sets.sets_Collect_finite_All sets.sets_Collect_const)
  1082        (auto intro!: halfspace_gt_in_halfspace countable_PiE countable_rat)
  1083   finally show "box a b \<in> sets ?SIGMA" .
  1084 qed auto
  1085 
  1086 lemma borel_eq_halfspace_le:
  1087   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i \<le> a}) ` (UNIV \<times> Basis))"
  1088   (is "_ = ?SIGMA")
  1089 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
  1090   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1091   then have i: "i \<in> Basis" by auto
  1092   have *: "{x::'a. x\<bullet>i < a} = (\<Union>n. {x. x\<bullet>i \<le> a - 1/real (Suc n)})"
  1093   proof (safe, simp_all del: of_nat_Suc)
  1094     fix x::'a assume *: "x\<bullet>i < a"
  1095     with reals_Archimedean[of "a - x\<bullet>i"]
  1096     obtain n where "x \<bullet> i < a - 1 / (real (Suc n))"
  1097       by (auto simp: field_simps)
  1098     then show "\<exists>n. x \<bullet> i \<le> a - 1 / (real (Suc n))"
  1099       by (blast intro: less_imp_le)
  1100   next
  1101     fix x::'a and n
  1102     assume "x\<bullet>i \<le> a - 1 / real (Suc n)"
  1103     also have "\<dots> < a" by auto
  1104     finally show "x\<bullet>i < a" .
  1105   qed
  1106   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
  1107     by (intro sets.countable_UN) (auto intro: i)
  1108 qed auto
  1109 
  1110 lemma borel_eq_halfspace_ge:
  1111   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a \<le> x \<bullet> i}) ` (UNIV \<times> Basis))"
  1112   (is "_ = ?SIGMA")
  1113 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
  1114   fix a :: real and i :: 'a assume i: "(a, i) \<in> UNIV \<times> Basis"
  1115   have *: "{x::'a. x\<bullet>i < a} = space ?SIGMA - {x::'a. a \<le> x\<bullet>i}" by auto
  1116   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
  1117     using i by (intro sets.compl_sets) auto
  1118 qed auto
  1119 
  1120 lemma borel_eq_halfspace_greater:
  1121   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a < x \<bullet> i}) ` (UNIV \<times> Basis))"
  1122   (is "_ = ?SIGMA")
  1123 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
  1124   fix a :: real and i :: 'a assume "(a, i) \<in> (UNIV \<times> Basis)"
  1125   then have i: "i \<in> Basis" by auto
  1126   have *: "{x::'a. x\<bullet>i \<le> a} = space ?SIGMA - {x::'a. a < x\<bullet>i}" by auto
  1127   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
  1128     by (intro sets.compl_sets) (auto intro: i)
  1129 qed auto
  1130 
  1131 lemma borel_eq_atMost:
  1132   "borel = sigma UNIV (range (\<lambda>a. {..a::'a::ordered_euclidean_space}))"
  1133   (is "_ = ?SIGMA")
  1134 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
  1135   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1136   then have "i \<in> Basis" by auto
  1137   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)})"
  1138   proof (safe, simp_all add: eucl_le[where 'a='a] split: if_split_asm)
  1139     fix x :: 'a
  1140     from real_arch_simple[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"] guess k::nat ..
  1141     then have "\<And>i. i \<in> Basis \<Longrightarrow> x\<bullet>i \<le> real k"
  1142       by (subst (asm) Max_le_iff) auto
  1143     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia \<le> real k"
  1144       by (auto intro!: exI[of _ k])
  1145   qed
  1146   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
  1147     by (intro sets.countable_UN) auto
  1148 qed auto
  1149 
  1150 lemma borel_eq_greaterThan:
  1151   "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. a <e x}))"
  1152   (is "_ = ?SIGMA")
  1153 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
  1154   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1155   then have i: "i \<in> Basis" by auto
  1156   have "{x::'a. x\<bullet>i \<le> a} = UNIV - {x::'a. a < x\<bullet>i}" by auto
  1157   also have *: "{x::'a. a < x\<bullet>i} =
  1158       (\<Union>k::nat. {x. (\<Sum>n\<in>Basis. (if n = i then a else -real k) *\<^sub>R n) <e x})" using i
  1159   proof (safe, simp_all add: eucl_less_def split: if_split_asm)
  1160     fix x :: 'a
  1161     from reals_Archimedean2[of "Max ((\<lambda>i. -x\<bullet>i)`Basis)"]
  1162     guess k::nat .. note k = this
  1163     { fix i :: 'a assume "i \<in> Basis"
  1164       then have "-x\<bullet>i < real k"
  1165         using k by (subst (asm) Max_less_iff) auto
  1166       then have "- real k < x\<bullet>i" by simp }
  1167     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> -real k < x \<bullet> ia"
  1168       by (auto intro!: exI[of _ k])
  1169   qed
  1170   finally show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA"
  1171     apply (simp only:)
  1172     apply (intro sets.countable_UN sets.Diff)
  1173     apply (auto intro: sigma_sets_top)
  1174     done
  1175 qed auto
  1176 
  1177 lemma borel_eq_lessThan:
  1178   "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. x <e a}))"
  1179   (is "_ = ?SIGMA")
  1180 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
  1181   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1182   then have i: "i \<in> Basis" by auto
  1183   have "{x::'a. a \<le> x\<bullet>i} = UNIV - {x::'a. x\<bullet>i < a}" by auto
  1184   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 \<open>i\<in> Basis\<close>
  1185   proof (safe, simp_all add: eucl_less_def split: if_split_asm)
  1186     fix x :: 'a
  1187     from reals_Archimedean2[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"]
  1188     guess k::nat .. note k = this
  1189     { fix i :: 'a assume "i \<in> Basis"
  1190       then have "x\<bullet>i < real k"
  1191         using k by (subst (asm) Max_less_iff) auto
  1192       then have "x\<bullet>i < real k" by simp }
  1193     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia < real k"
  1194       by (auto intro!: exI[of _ k])
  1195   qed
  1196   finally show "{x. a \<le> x\<bullet>i} \<in> ?SIGMA"
  1197     apply (simp only:)
  1198     apply (intro sets.countable_UN sets.Diff)
  1199     apply (auto intro: sigma_sets_top )
  1200     done
  1201 qed auto
  1202 
  1203 lemma borel_eq_atLeastAtMost:
  1204   "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} ::'a::ordered_euclidean_space set))"
  1205   (is "_ = ?SIGMA")
  1206 proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
  1207   fix a::'a
  1208   have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
  1209   proof (safe, simp_all add: eucl_le[where 'a='a])
  1210     fix x :: 'a
  1211     from real_arch_simple[of "Max ((\<lambda>i. - x\<bullet>i)`Basis)"]
  1212     guess k::nat .. note k = this
  1213     { fix i :: 'a assume "i \<in> Basis"
  1214       with k have "- x\<bullet>i \<le> real k"
  1215         by (subst (asm) Max_le_iff) (auto simp: field_simps)
  1216       then have "- real k \<le> x\<bullet>i" by simp }
  1217     then show "\<exists>n::nat. \<forall>i\<in>Basis. - real n \<le> x \<bullet> i"
  1218       by (auto intro!: exI[of _ k])
  1219   qed
  1220   show "{..a} \<in> ?SIGMA" unfolding *
  1221     by (intro sets.countable_UN)
  1222        (auto intro!: sigma_sets_top)
  1223 qed auto
  1224 
  1225 lemma borel_set_induct[consumes 1, case_names empty interval compl union]:
  1226   assumes "A \<in> sets borel"
  1227   assumes empty: "P {}" and int: "\<And>a b. a \<le> b \<Longrightarrow> P {a..b}" and compl: "\<And>A. A \<in> sets borel \<Longrightarrow> P A \<Longrightarrow> P (-A)" and
  1228           un: "\<And>f. disjoint_family f \<Longrightarrow> (\<And>i. f i \<in> sets borel) \<Longrightarrow>  (\<And>i. P (f i)) \<Longrightarrow> P (\<Union>i::nat. f i)"
  1229   shows "P (A::real set)"
  1230 proof -
  1231   let ?G = "range (\<lambda>(a,b). {a..b::real})"
  1232   have "Int_stable ?G" "?G \<subseteq> Pow UNIV" "A \<in> sigma_sets UNIV ?G"
  1233       using assms(1) by (auto simp add: borel_eq_atLeastAtMost Int_stable_def)
  1234   thus ?thesis
  1235   proof (induction rule: sigma_sets_induct_disjoint)
  1236     case (union f)
  1237       from union.hyps(2) have "\<And>i. f i \<in> sets borel" by (auto simp: borel_eq_atLeastAtMost)
  1238       with union show ?case by (auto intro: un)
  1239   next
  1240     case (basic A)
  1241     then obtain a b where "A = {a .. b}" by auto
  1242     then show ?case
  1243       by (cases "a \<le> b") (auto intro: int empty)
  1244   qed (auto intro: empty compl simp: Compl_eq_Diff_UNIV[symmetric] borel_eq_atLeastAtMost)
  1245 qed
  1246 
  1247 lemma borel_sigma_sets_Ioc: "borel = sigma UNIV (range (\<lambda>(a, b). {a <.. b::real}))"
  1248 proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
  1249   fix i :: real
  1250   have "{..i} = (\<Union>j::nat. {-j <.. i})"
  1251     by (auto simp: minus_less_iff reals_Archimedean2)
  1252   also have "\<dots> \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))"
  1253     by (intro sets.countable_nat_UN) auto
  1254   finally show "{..i} \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))" .
  1255 qed simp
  1256 
  1257 lemma eucl_lessThan: "{x::real. x <e a} = lessThan a"
  1258   by (simp add: eucl_less_def lessThan_def)
  1259 
  1260 lemma borel_eq_atLeastLessThan:
  1261   "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
  1262 proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
  1263   have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
  1264   fix x :: real
  1265   have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
  1266     by (auto simp: move_uminus real_arch_simple)
  1267   then show "{y. y <e x} \<in> ?SIGMA"
  1268     by (auto intro: sigma_sets.intros(2-) simp: eucl_lessThan)
  1269 qed auto
  1270 
  1271 lemma borel_measurable_halfspacesI:
  1272   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1273   assumes F: "borel = sigma UNIV (F ` (UNIV \<times> Basis))"
  1274   and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
  1275   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a::real. S a i \<in> sets M)"
  1276 proof safe
  1277   fix a :: real and i :: 'b assume i: "i \<in> Basis" and f: "f \<in> borel_measurable M"
  1278   then show "S a i \<in> sets M" unfolding assms
  1279     by (auto intro!: measurable_sets simp: assms(1))
  1280 next
  1281   assume a: "\<forall>i\<in>Basis. \<forall>a. S a i \<in> sets M"
  1282   then show "f \<in> borel_measurable M"
  1283     by (auto intro!: measurable_measure_of simp: S_eq F)
  1284 qed
  1285 
  1286 lemma borel_measurable_iff_halfspace_le:
  1287   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1288   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)"
  1289   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
  1290 
  1291 lemma borel_measurable_iff_halfspace_less:
  1292   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1293   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)"
  1294   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
  1295 
  1296 lemma borel_measurable_iff_halfspace_ge:
  1297   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1298   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)"
  1299   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
  1300 
  1301 lemma borel_measurable_iff_halfspace_greater:
  1302   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1303   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)"
  1304   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
  1305 
  1306 lemma borel_measurable_iff_le:
  1307   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
  1308   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
  1309 
  1310 lemma borel_measurable_iff_less:
  1311   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
  1312   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
  1313 
  1314 lemma borel_measurable_iff_ge:
  1315   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
  1316   using borel_measurable_iff_halfspace_ge[where 'c=real]
  1317   by simp
  1318 
  1319 lemma borel_measurable_iff_greater:
  1320   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
  1321   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
  1322 
  1323 lemma borel_measurable_euclidean_space:
  1324   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1325   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M)"
  1326 proof safe
  1327   assume f: "\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M"
  1328   then show "f \<in> borel_measurable M"
  1329     by (subst borel_measurable_iff_halfspace_le) auto
  1330 qed auto
  1331 
  1332 subsection "Borel measurable operators"
  1333 
  1334 lemma borel_measurable_norm[measurable]: "norm \<in> borel_measurable borel"
  1335   by (intro borel_measurable_continuous_on1 continuous_intros)
  1336 
  1337 lemma borel_measurable_sgn [measurable]: "(sgn::'a::real_normed_vector \<Rightarrow> 'a) \<in> borel_measurable borel"
  1338   by (rule borel_measurable_continuous_countable_exceptions[where X="{0}"])
  1339      (auto intro!: continuous_on_sgn continuous_on_id)
  1340 
  1341 lemma borel_measurable_uminus[measurable (raw)]:
  1342   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1343   assumes g: "g \<in> borel_measurable M"
  1344   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
  1345   by (rule borel_measurable_continuous_on[OF _ g]) (intro continuous_intros)
  1346 
  1347 lemma borel_measurable_diff[measurable (raw)]:
  1348   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1349   assumes f: "f \<in> borel_measurable M"
  1350   assumes g: "g \<in> borel_measurable M"
  1351   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1352   using borel_measurable_add [of f M "- g"] assms by (simp add: fun_Compl_def)
  1353 
  1354 lemma borel_measurable_times[measurable (raw)]:
  1355   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_algebra}"
  1356   assumes f: "f \<in> borel_measurable M"
  1357   assumes g: "g \<in> borel_measurable M"
  1358   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1359   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1360 
  1361 lemma borel_measurable_prod[measurable (raw)]:
  1362   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_field}"
  1363   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1364   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1365 proof cases
  1366   assume "finite S"
  1367   thus ?thesis using assms by induct auto
  1368 qed simp
  1369 
  1370 lemma borel_measurable_dist[measurable (raw)]:
  1371   fixes g f :: "'a \<Rightarrow> 'b::{second_countable_topology, metric_space}"
  1372   assumes f: "f \<in> borel_measurable M"
  1373   assumes g: "g \<in> borel_measurable M"
  1374   shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
  1375   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1376 
  1377 lemma borel_measurable_scaleR[measurable (raw)]:
  1378   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1379   assumes f: "f \<in> borel_measurable M"
  1380   assumes g: "g \<in> borel_measurable M"
  1381   shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
  1382   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1383 
  1384 lemma borel_measurable_uminus_eq [simp]:
  1385   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1386   shows "(\<lambda>x. - f x) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  1387 proof
  1388   assume ?l from borel_measurable_uminus[OF this] show ?r by simp
  1389 qed auto
  1390 
  1391 lemma affine_borel_measurable_vector:
  1392   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
  1393   assumes "f \<in> borel_measurable M"
  1394   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
  1395 proof (rule borel_measurableI)
  1396   fix S :: "'x set" assume "open S"
  1397   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
  1398   proof cases
  1399     assume "b \<noteq> 0"
  1400     with \<open>open S\<close> have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
  1401       using open_affinity [of S "inverse b" "- a /\<^sub>R b"]
  1402       by (auto simp: algebra_simps)
  1403     hence "?S \<in> sets borel" by auto
  1404     moreover
  1405     from \<open>b \<noteq> 0\<close> have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
  1406       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
  1407     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
  1408       by auto
  1409   qed simp
  1410 qed
  1411 
  1412 lemma borel_measurable_const_scaleR[measurable (raw)]:
  1413   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
  1414   using affine_borel_measurable_vector[of f M 0 b] by simp
  1415 
  1416 lemma borel_measurable_const_add[measurable (raw)]:
  1417   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
  1418   using affine_borel_measurable_vector[of f M a 1] by simp
  1419 
  1420 lemma borel_measurable_inverse[measurable (raw)]:
  1421   fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
  1422   assumes f: "f \<in> borel_measurable M"
  1423   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
  1424   apply (rule measurable_compose[OF f])
  1425   apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
  1426   apply (auto intro!: continuous_on_inverse continuous_on_id)
  1427   done
  1428 
  1429 lemma borel_measurable_divide[measurable (raw)]:
  1430   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow>
  1431     (\<lambda>x. f x / g x::'b::{second_countable_topology, real_normed_div_algebra}) \<in> borel_measurable M"
  1432   by (simp add: divide_inverse)
  1433 
  1434 lemma borel_measurable_abs[measurable (raw)]:
  1435   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
  1436   unfolding abs_real_def by simp
  1437 
  1438 lemma borel_measurable_nth[measurable (raw)]:
  1439   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
  1440   by (simp add: cart_eq_inner_axis)
  1441 
  1442 lemma convex_measurable:
  1443   fixes A :: "'a :: euclidean_space set"
  1444   shows "X \<in> borel_measurable M \<Longrightarrow> X ` space M \<subseteq> A \<Longrightarrow> open A \<Longrightarrow> convex_on A q \<Longrightarrow>
  1445     (\<lambda>x. q (X x)) \<in> borel_measurable M"
  1446   by (rule measurable_compose[where f=X and N="restrict_space borel A"])
  1447      (auto intro!: borel_measurable_continuous_on_restrict convex_on_continuous measurable_restrict_space2)
  1448 
  1449 lemma borel_measurable_ln[measurable (raw)]:
  1450   assumes f: "f \<in> borel_measurable M"
  1451   shows "(\<lambda>x. ln (f x :: real)) \<in> borel_measurable M"
  1452   apply (rule measurable_compose[OF f])
  1453   apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
  1454   apply (auto intro!: continuous_on_ln continuous_on_id)
  1455   done
  1456 
  1457 lemma borel_measurable_log[measurable (raw)]:
  1458   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
  1459   unfolding log_def by auto
  1460 
  1461 lemma borel_measurable_exp[measurable]:
  1462   "(exp::'a::{real_normed_field,banach}\<Rightarrow>'a) \<in> borel_measurable borel"
  1463   by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_exp)
  1464 
  1465 lemma measurable_real_floor[measurable]:
  1466   "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  1467 proof -
  1468   have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real_of_int a \<le> x \<and> x < real_of_int (a + 1))"
  1469     by (auto intro: floor_eq2)
  1470   then show ?thesis
  1471     by (auto simp: vimage_def measurable_count_space_eq2_countable)
  1472 qed
  1473 
  1474 lemma measurable_real_ceiling[measurable]:
  1475   "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  1476   unfolding ceiling_def[abs_def] by simp
  1477 
  1478 lemma borel_measurable_real_floor: "(\<lambda>x::real. real_of_int \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
  1479   by simp
  1480 
  1481 lemma borel_measurable_root [measurable]: "root n \<in> borel_measurable borel"
  1482   by (intro borel_measurable_continuous_on1 continuous_intros)
  1483 
  1484 lemma borel_measurable_sqrt [measurable]: "sqrt \<in> borel_measurable borel"
  1485   by (intro borel_measurable_continuous_on1 continuous_intros)
  1486 
  1487 lemma borel_measurable_power [measurable (raw)]:
  1488   fixes f :: "_ \<Rightarrow> 'b::{power,real_normed_algebra}"
  1489   assumes f: "f \<in> borel_measurable M"
  1490   shows "(\<lambda>x. (f x) ^ n) \<in> borel_measurable M"
  1491   by (intro borel_measurable_continuous_on [OF _ f] continuous_intros)
  1492 
  1493 lemma borel_measurable_Re [measurable]: "Re \<in> borel_measurable borel"
  1494   by (intro borel_measurable_continuous_on1 continuous_intros)
  1495 
  1496 lemma borel_measurable_Im [measurable]: "Im \<in> borel_measurable borel"
  1497   by (intro borel_measurable_continuous_on1 continuous_intros)
  1498 
  1499 lemma borel_measurable_of_real [measurable]: "(of_real :: _ \<Rightarrow> (_::real_normed_algebra)) \<in> borel_measurable borel"
  1500   by (intro borel_measurable_continuous_on1 continuous_intros)
  1501 
  1502 lemma borel_measurable_sin [measurable]: "(sin :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  1503   by (intro borel_measurable_continuous_on1 continuous_intros)
  1504 
  1505 lemma borel_measurable_cos [measurable]: "(cos :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  1506   by (intro borel_measurable_continuous_on1 continuous_intros)
  1507 
  1508 lemma borel_measurable_arctan [measurable]: "arctan \<in> borel_measurable borel"
  1509   by (intro borel_measurable_continuous_on1 continuous_intros)
  1510 
  1511 lemma%important borel_measurable_complex_iff:
  1512   "f \<in> borel_measurable M \<longleftrightarrow>
  1513     (\<lambda>x. Re (f x)) \<in> borel_measurable M \<and> (\<lambda>x. Im (f x)) \<in> borel_measurable M"
  1514   apply auto
  1515   apply (subst fun_complex_eq)
  1516   apply (intro borel_measurable_add)
  1517   apply auto
  1518   done
  1519 
  1520 lemma powr_real_measurable [measurable]:
  1521   assumes "f \<in> measurable M borel" "g \<in> measurable M borel"
  1522   shows   "(\<lambda>x. f x powr g x :: real) \<in> measurable M borel"
  1523   using assms by (simp_all add: powr_def)
  1524 
  1525 lemma measurable_of_bool[measurable]: "of_bool \<in> count_space UNIV \<rightarrow>\<^sub>M borel"
  1526   by simp
  1527 
  1528 subsection "Borel space on the extended reals"
  1529 
  1530 lemma borel_measurable_ereal[measurable (raw)]:
  1531   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1532   using continuous_on_ereal f by (rule borel_measurable_continuous_on) (rule continuous_on_id)
  1533 
  1534 lemma borel_measurable_real_of_ereal[measurable (raw)]:
  1535   fixes f :: "'a \<Rightarrow> ereal"
  1536   assumes f: "f \<in> borel_measurable M"
  1537   shows "(\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M"
  1538   apply (rule measurable_compose[OF f])
  1539   apply (rule borel_measurable_continuous_countable_exceptions[of "{\<infinity>, -\<infinity> }"])
  1540   apply (auto intro: continuous_on_real simp: Compl_eq_Diff_UNIV)
  1541   done
  1542 
  1543 lemma borel_measurable_ereal_cases:
  1544   fixes f :: "'a \<Rightarrow> ereal"
  1545   assumes f: "f \<in> borel_measurable M"
  1546   assumes H: "(\<lambda>x. H (ereal (real_of_ereal (f x)))) \<in> borel_measurable M"
  1547   shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
  1548 proof -
  1549   let ?F = "\<lambda>x. if f x = \<infinity> then H \<infinity> else if f x = - \<infinity> then H (-\<infinity>) else H (ereal (real_of_ereal (f x)))"
  1550   { fix x have "H (f x) = ?F x" by (cases "f x") auto }
  1551   with f H show ?thesis by simp
  1552 qed
  1553 
  1554 lemma
  1555   fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
  1556   shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
  1557     and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
  1558     and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
  1559   by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
  1560 
  1561 lemma borel_measurable_uminus_eq_ereal[simp]:
  1562   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  1563 proof
  1564   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
  1565 qed auto
  1566 
  1567 lemma set_Collect_ereal2:
  1568   fixes f g :: "'a \<Rightarrow> ereal"
  1569   assumes f: "f \<in> borel_measurable M"
  1570   assumes g: "g \<in> borel_measurable M"
  1571   assumes H: "{x \<in> space M. H (ereal (real_of_ereal (f x))) (ereal (real_of_ereal (g x)))} \<in> sets M"
  1572     "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
  1573     "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
  1574     "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
  1575     "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
  1576   shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
  1577 proof -
  1578   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_of_ereal (g x)))"
  1579   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_of_ereal (f x))) x"
  1580   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1581   note * = this
  1582   from assms show ?thesis
  1583     by (subst *) (simp del: space_borel split del: if_split)
  1584 qed
  1585 
  1586 lemma borel_measurable_ereal_iff:
  1587   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  1588 proof
  1589   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1590   from borel_measurable_real_of_ereal[OF this]
  1591   show "f \<in> borel_measurable M" by auto
  1592 qed auto
  1593 
  1594 lemma borel_measurable_erealD[measurable_dest]:
  1595   "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<Longrightarrow> g \<in> measurable N M \<Longrightarrow> (\<lambda>x. f (g x)) \<in> borel_measurable N"
  1596   unfolding borel_measurable_ereal_iff by simp
  1597 
  1598 theorem borel_measurable_ereal_iff_real:
  1599   fixes f :: "'a \<Rightarrow> ereal"
  1600   shows "f \<in> borel_measurable M \<longleftrightarrow>
  1601     ((\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M \<and> f -` {\<infinity>} \<inter> space M \<in> sets M \<and> f -` {-\<infinity>} \<inter> space M \<in> sets M)"
  1602 proof safe
  1603   assume *: "(\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M" "f -` {\<infinity>} \<inter> space M \<in> sets M" "f -` {-\<infinity>} \<inter> space M \<in> sets M"
  1604   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
  1605   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
  1606   let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real_of_ereal (f x))"
  1607   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
  1608   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
  1609   finally show "f \<in> borel_measurable M" .
  1610 qed simp_all
  1611 
  1612 lemma borel_measurable_ereal_iff_Iio:
  1613   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1614   by (auto simp: borel_Iio measurable_iff_measure_of)
  1615 
  1616 lemma borel_measurable_ereal_iff_Ioi:
  1617   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1618   by (auto simp: borel_Ioi measurable_iff_measure_of)
  1619 
  1620 lemma vimage_sets_compl_iff:
  1621   "f -` A \<inter> space M \<in> sets M \<longleftrightarrow> f -` (- A) \<inter> space M \<in> sets M"
  1622 proof -
  1623   { fix A assume "f -` A \<inter> space M \<in> sets M"
  1624     moreover have "f -` (- A) \<inter> space M = space M - f -` A \<inter> space M" by auto
  1625     ultimately have "f -` (- A) \<inter> space M \<in> sets M" by auto }
  1626   from this[of A] this[of "-A"] show ?thesis
  1627     by (metis double_complement)
  1628 qed
  1629 
  1630 lemma borel_measurable_iff_Iic_ereal:
  1631   "(f::'a\<Rightarrow>ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
  1632   unfolding borel_measurable_ereal_iff_Ioi vimage_sets_compl_iff[where A="{a <..}" for a] by simp
  1633 
  1634 lemma borel_measurable_iff_Ici_ereal:
  1635   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  1636   unfolding borel_measurable_ereal_iff_Iio vimage_sets_compl_iff[where A="{..< a}" for a] by simp
  1637 
  1638 lemma borel_measurable_ereal2:
  1639   fixes f g :: "'a \<Rightarrow> ereal"
  1640   assumes f: "f \<in> borel_measurable M"
  1641   assumes g: "g \<in> borel_measurable M"
  1642   assumes H: "(\<lambda>x. H (ereal (real_of_ereal (f x))) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  1643     "(\<lambda>x. H (-\<infinity>) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  1644     "(\<lambda>x. H (\<infinity>) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  1645     "(\<lambda>x. H (ereal (real_of_ereal (f x))) (-\<infinity>)) \<in> borel_measurable M"
  1646     "(\<lambda>x. H (ereal (real_of_ereal (f x))) (\<infinity>)) \<in> borel_measurable M"
  1647   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  1648 proof -
  1649   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_of_ereal (g x)))"
  1650   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_of_ereal (f x))) x"
  1651   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1652   note * = this
  1653   from assms show ?thesis unfolding * by simp
  1654 qed
  1655 
  1656 lemma [measurable(raw)]:
  1657   fixes f :: "'a \<Rightarrow> ereal"
  1658   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1659   shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1660     and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1661   by (simp_all add: borel_measurable_ereal2)
  1662 
  1663 lemma [measurable(raw)]:
  1664   fixes f g :: "'a \<Rightarrow> ereal"
  1665   assumes "f \<in> borel_measurable M"
  1666   assumes "g \<in> borel_measurable M"
  1667   shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1668     and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  1669   using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  1670 
  1671 lemma borel_measurable_ereal_sum[measurable (raw)]:
  1672   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1673   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1674   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1675   using assms by (induction S rule: infinite_finite_induct) auto
  1676 
  1677 lemma borel_measurable_ereal_prod[measurable (raw)]:
  1678   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1679   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1680   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1681   using assms by (induction S rule: infinite_finite_induct) auto
  1682 
  1683 lemma borel_measurable_extreal_suminf[measurable (raw)]:
  1684   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1685   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1686   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1687   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1688 
  1689 subsection "Borel space on the extended non-negative reals"
  1690 
  1691 text \<open> \<^type>\<open>ennreal\<close> is a topological monoid, so no rules for plus are required, also all order
  1692   statements are usually done on type classes. \<close>
  1693 
  1694 lemma measurable_enn2ereal[measurable]: "enn2ereal \<in> borel \<rightarrow>\<^sub>M borel"
  1695   by (intro borel_measurable_continuous_on1 continuous_on_enn2ereal)
  1696 
  1697 lemma measurable_e2ennreal[measurable]: "e2ennreal \<in> borel \<rightarrow>\<^sub>M borel"
  1698   by (intro borel_measurable_continuous_on1 continuous_on_e2ennreal)
  1699 
  1700 lemma borel_measurable_enn2real[measurable (raw)]:
  1701   "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. enn2real (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  1702   unfolding enn2real_def[abs_def] by measurable
  1703 
  1704 definition%important [simp]: "is_borel f M \<longleftrightarrow> f \<in> borel_measurable M"
  1705 
  1706 lemma is_borel_transfer[transfer_rule]: "rel_fun (rel_fun (=) pcr_ennreal) (=) is_borel is_borel"
  1707   unfolding is_borel_def[abs_def]
  1708 proof (safe intro!: rel_funI ext dest!: rel_fun_eq_pcr_ennreal[THEN iffD1])
  1709   fix f and M :: "'a measure"
  1710   show "f \<in> borel_measurable M" if f: "enn2ereal \<circ> f \<in> borel_measurable M"
  1711     using measurable_compose[OF f measurable_e2ennreal] by simp
  1712 qed simp
  1713 
  1714 context
  1715   includes ennreal.lifting
  1716 begin
  1717 
  1718 lemma measurable_ennreal[measurable]: "ennreal \<in> borel \<rightarrow>\<^sub>M borel"
  1719   unfolding is_borel_def[symmetric]
  1720   by transfer simp
  1721 
  1722 lemma borel_measurable_ennreal_iff[simp]:
  1723   assumes [simp]: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1724   shows "(\<lambda>x. ennreal (f x)) \<in> M \<rightarrow>\<^sub>M borel \<longleftrightarrow> f \<in> M \<rightarrow>\<^sub>M borel"
  1725 proof safe
  1726   assume "(\<lambda>x. ennreal (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  1727   then have "(\<lambda>x. enn2real (ennreal (f x))) \<in> M \<rightarrow>\<^sub>M borel"
  1728     by measurable
  1729   then show "f \<in> M \<rightarrow>\<^sub>M borel"
  1730     by (rule measurable_cong[THEN iffD1, rotated]) auto
  1731 qed measurable
  1732 
  1733 lemma borel_measurable_times_ennreal[measurable (raw)]:
  1734   fixes f g :: "'a \<Rightarrow> ennreal"
  1735   shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. f x * g x) \<in> M \<rightarrow>\<^sub>M borel"
  1736   unfolding is_borel_def[symmetric] by transfer simp
  1737 
  1738 lemma borel_measurable_inverse_ennreal[measurable (raw)]:
  1739   fixes f :: "'a \<Rightarrow> ennreal"
  1740   shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. inverse (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  1741   unfolding is_borel_def[symmetric] by transfer simp
  1742 
  1743 lemma borel_measurable_divide_ennreal[measurable (raw)]:
  1744   fixes f :: "'a \<Rightarrow> ennreal"
  1745   shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. f x / g x) \<in> M \<rightarrow>\<^sub>M borel"
  1746   unfolding divide_ennreal_def by simp
  1747 
  1748 lemma borel_measurable_minus_ennreal[measurable (raw)]:
  1749   fixes f :: "'a \<Rightarrow> ennreal"
  1750   shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. f x - g x) \<in> M \<rightarrow>\<^sub>M borel"
  1751   unfolding is_borel_def[symmetric] by transfer simp
  1752 
  1753 lemma borel_measurable_prod_ennreal[measurable (raw)]:
  1754   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ennreal"
  1755   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1756   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1757   using assms by (induction S rule: infinite_finite_induct) auto
  1758 
  1759 end
  1760 
  1761 hide_const (open) is_borel
  1762 
  1763 subsection \<open>LIMSEQ is borel measurable\<close>
  1764 
  1765 lemma borel_measurable_LIMSEQ_real:
  1766   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1767   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
  1768   and u: "\<And>i. u i \<in> borel_measurable M"
  1769   shows "u' \<in> borel_measurable M"
  1770 proof -
  1771   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1772     using u' by (simp add: lim_imp_Liminf)
  1773   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1774     by auto
  1775   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1776 qed
  1777 
  1778 lemma borel_measurable_LIMSEQ_metric:
  1779   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b :: metric_space"
  1780   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1781   assumes lim: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. f i x) \<longlonglongrightarrow> g x"
  1782   shows "g \<in> borel_measurable M"
  1783   unfolding borel_eq_closed
  1784 proof (safe intro!: measurable_measure_of)
  1785   fix A :: "'b set" assume "closed A"
  1786 
  1787   have [measurable]: "(\<lambda>x. infdist (g x) A) \<in> borel_measurable M"
  1788   proof (rule borel_measurable_LIMSEQ_real)
  1789     show "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. infdist (f i x) A) \<longlonglongrightarrow> infdist (g x) A"
  1790       by (intro tendsto_infdist lim)
  1791     show "\<And>i. (\<lambda>x. infdist (f i x) A) \<in> borel_measurable M"
  1792       by (intro borel_measurable_continuous_on[where f="\<lambda>x. infdist x A"]
  1793         continuous_at_imp_continuous_on ballI continuous_infdist continuous_ident) auto
  1794   qed
  1795 
  1796   show "g -` A \<inter> space M \<in> sets M"
  1797   proof cases
  1798     assume "A \<noteq> {}"
  1799     then have "\<And>x. infdist x A = 0 \<longleftrightarrow> x \<in> A"
  1800       using \<open>closed A\<close> by (simp add: in_closed_iff_infdist_zero)
  1801     then have "g -` A \<inter> space M = {x\<in>space M. infdist (g x) A = 0}"
  1802       by auto
  1803     also have "\<dots> \<in> sets M"
  1804       by measurable
  1805     finally show ?thesis .
  1806   qed simp
  1807 qed auto
  1808 
  1809 lemma sets_Collect_Cauchy[measurable]:
  1810   fixes f :: "nat \<Rightarrow> 'a => 'b::{metric_space, second_countable_topology}"
  1811   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1812   shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  1813   unfolding metric_Cauchy_iff2 using f by auto
  1814 
  1815 lemma borel_measurable_lim_metric[measurable (raw)]:
  1816   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1817   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1818   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1819 proof -
  1820   define u' where "u' x = lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)" for x
  1821   then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
  1822     by (auto simp: lim_def convergent_eq_Cauchy[symmetric])
  1823   have "u' \<in> borel_measurable M"
  1824   proof (rule borel_measurable_LIMSEQ_metric)
  1825     fix x
  1826     have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1827       by (cases "Cauchy (\<lambda>i. f i x)")
  1828          (auto simp add: convergent_eq_Cauchy[symmetric] convergent_def)
  1829     then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) \<longlonglongrightarrow> u' x"
  1830       unfolding u'_def
  1831       by (rule convergent_LIMSEQ_iff[THEN iffD1])
  1832   qed measurable
  1833   then show ?thesis
  1834     unfolding * by measurable
  1835 qed
  1836 
  1837 lemma borel_measurable_suminf[measurable (raw)]:
  1838   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1839   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1840   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1841   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1842 
  1843 lemma Collect_closed_imp_pred_borel: "closed {x. P x} \<Longrightarrow> Measurable.pred borel P"
  1844   by (simp add: pred_def)
  1845 
  1846 (* Proof by Jeremy Avigad and Luke Serafin *)
  1847 lemma isCont_borel_pred[measurable]:
  1848   fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  1849   shows "Measurable.pred borel (isCont f)"
  1850 proof (subst measurable_cong)
  1851   let ?I = "\<lambda>j. inverse(real (Suc j))"
  1852   show "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)" for x
  1853     unfolding continuous_at_eps_delta
  1854   proof safe
  1855     fix i assume "\<forall>e>0. \<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  1856     moreover have "0 < ?I i / 2"
  1857       by simp
  1858     ultimately obtain d where d: "0 < d" "\<And>y. dist x y < d \<Longrightarrow> dist (f y) (f x) < ?I i / 2"
  1859       by (metis dist_commute)
  1860     then obtain j where j: "?I j < d"
  1861       by (metis reals_Archimedean)
  1862 
  1863     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"
  1864     proof (safe intro!: exI[where x=j])
  1865       fix y z assume *: "dist x y < ?I j" "dist x z < ?I j"
  1866       have "dist (f y) (f z) \<le> dist (f y) (f x) + dist (f z) (f x)"
  1867         by (rule dist_triangle2)
  1868       also have "\<dots> < ?I i / 2 + ?I i / 2"
  1869         by (intro add_strict_mono d less_trans[OF _ j] *)
  1870       also have "\<dots> \<le> ?I i"
  1871         by (simp add: field_simps of_nat_Suc)
  1872       finally show "dist (f y) (f z) \<le> ?I i"
  1873         by simp
  1874     qed
  1875   next
  1876     fix e::real assume "0 < e"
  1877     then obtain n where n: "?I n < e"
  1878       by (metis reals_Archimedean)
  1879     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"
  1880     from this[THEN spec, of "Suc n"]
  1881     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)"
  1882       by auto
  1883 
  1884     show "\<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  1885     proof (safe intro!: exI[of _ "?I j"])
  1886       fix y assume "dist y x < ?I j"
  1887       then have "dist (f y) (f x) \<le> ?I (Suc n)"
  1888         by (intro j) (auto simp: dist_commute)
  1889       also have "?I (Suc n) < ?I n"
  1890         by simp
  1891       also note n
  1892       finally show "dist (f y) (f x) < e" .
  1893     qed simp
  1894   qed
  1895 qed (intro pred_intros_countable closed_Collect_all closed_Collect_le open_Collect_less
  1896            Collect_closed_imp_pred_borel closed_Collect_imp open_Collect_conj continuous_intros)
  1897 
  1898 lemma isCont_borel:
  1899   fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  1900   shows "{x. isCont f x} \<in> sets borel"
  1901   by simp
  1902 
  1903 lemma is_real_interval:
  1904   assumes S: "is_interval S"
  1905   shows "\<exists>a b::real. S = {} \<or> S = UNIV \<or> S = {..<b} \<or> S = {..b} \<or> S = {a<..} \<or> S = {a..} \<or>
  1906     S = {a<..<b} \<or> S = {a<..b} \<or> S = {a..<b} \<or> S = {a..b}"
  1907   using S unfolding is_interval_1 by (blast intro: interval_cases)
  1908 
  1909 lemma real_interval_borel_measurable:
  1910   assumes "is_interval (S::real set)"
  1911   shows "S \<in> sets borel"
  1912 proof -
  1913   from assms is_real_interval have "\<exists>a b::real. S = {} \<or> S = UNIV \<or> S = {..<b} \<or> S = {..b} \<or>
  1914     S = {a<..} \<or> S = {a..} \<or> S = {a<..<b} \<or> S = {a<..b} \<or> S = {a..<b} \<or> S = {a..b}" by auto
  1915   then guess a ..
  1916   then guess b ..
  1917   thus ?thesis
  1918     by auto
  1919 qed
  1920 
  1921 text \<open>The next lemmas hold in any second countable linorder (including ennreal or ereal for instance),
  1922 but in the current state they are restricted to reals.\<close>
  1923 
  1924 lemma borel_measurable_mono_on_fnc:
  1925   fixes f :: "real \<Rightarrow> real" and A :: "real set"
  1926   assumes "mono_on f A"
  1927   shows "f \<in> borel_measurable (restrict_space borel A)"
  1928   apply (rule measurable_restrict_countable[OF mono_on_ctble_discont[OF assms]])
  1929   apply (auto intro!: image_eqI[where x="{x}" for x] simp: sets_restrict_space)
  1930   apply (auto simp add: sets_restrict_restrict_space continuous_on_eq_continuous_within
  1931               cong: measurable_cong_sets
  1932               intro!: borel_measurable_continuous_on_restrict intro: continuous_within_subset)
  1933   done
  1934 
  1935 lemma borel_measurable_piecewise_mono:
  1936   fixes f::"real \<Rightarrow> real" and C::"real set set"
  1937   assumes "countable C" "\<And>c. c \<in> C \<Longrightarrow> c \<in> sets borel" "\<And>c. c \<in> C \<Longrightarrow> mono_on f c" "(\<Union>C) = UNIV"
  1938   shows "f \<in> borel_measurable borel"
  1939   by (rule measurable_piecewise_restrict[of C], auto intro: borel_measurable_mono_on_fnc simp: assms)
  1940 
  1941 lemma borel_measurable_mono:
  1942   fixes f :: "real \<Rightarrow> real"
  1943   shows "mono f \<Longrightarrow> f \<in> borel_measurable borel"
  1944   using borel_measurable_mono_on_fnc[of f UNIV] by (simp add: mono_def mono_on_def)
  1945 
  1946 lemma measurable_bdd_below_real[measurable (raw)]:
  1947   fixes F :: "'a \<Rightarrow> 'i \<Rightarrow> real"
  1948   assumes [simp]: "countable I" and [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> M \<rightarrow>\<^sub>M borel"
  1949   shows "Measurable.pred M (\<lambda>x. bdd_below ((\<lambda>i. F i x)`I))"
  1950 proof (subst measurable_cong)
  1951   show "bdd_below ((\<lambda>i. F i x)`I) \<longleftrightarrow> (\<exists>q\<in>\<int>. \<forall>i\<in>I. q \<le> F i x)" for x
  1952     by (auto simp: bdd_below_def intro!: bexI[of _ "of_int (floor _)"] intro: order_trans of_int_floor_le)
  1953   show "Measurable.pred M (\<lambda>w. \<exists>q\<in>\<int>. \<forall>i\<in>I. q \<le> F i w)"
  1954     using countable_int by measurable
  1955 qed
  1956 
  1957 lemma borel_measurable_cINF_real[measurable (raw)]:
  1958   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> real"
  1959   assumes [simp]: "countable I"
  1960   assumes F[measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
  1961   shows "(\<lambda>x. INF i\<in>I. F i x) \<in> borel_measurable M"
  1962 proof (rule measurable_piecewise_restrict)
  1963   let ?\<Omega> = "{x\<in>space M. bdd_below ((\<lambda>i. F i x)`I)}"
  1964   show "countable {?\<Omega>, - ?\<Omega>}" "space M \<subseteq> \<Union>{?\<Omega>, - ?\<Omega>}" "\<And>X. X \<in> {?\<Omega>, - ?\<Omega>} \<Longrightarrow> X \<inter> space M \<in> sets M"
  1965     by auto
  1966   fix X assume "X \<in> {?\<Omega>, - ?\<Omega>}" then show "(\<lambda>x. INF i\<in>I. F i x) \<in> borel_measurable (restrict_space M X)"
  1967   proof safe
  1968     show "(\<lambda>x. INF i\<in>I. F i x) \<in> borel_measurable (restrict_space M ?\<Omega>)"
  1969       by (intro borel_measurable_cINF measurable_restrict_space1 F)
  1970          (auto simp: space_restrict_space)
  1971     show "(\<lambda>x. INF i\<in>I. F i x) \<in> borel_measurable (restrict_space M (-?\<Omega>))"
  1972     proof (subst measurable_cong)
  1973       fix x assume "x \<in> space (restrict_space M (-?\<Omega>))"
  1974       then have "\<not> (\<forall>i\<in>I. - F i x \<le> y)" for y
  1975         by (auto simp: space_restrict_space bdd_above_def bdd_above_uminus[symmetric])
  1976       then show "(INF i\<in>I. F i x) = - (THE x. False)"
  1977         by (auto simp: space_restrict_space Inf_real_def Sup_real_def Least_def simp del: Set.ball_simps(10))
  1978     qed simp
  1979   qed
  1980 qed
  1981 
  1982 lemma borel_Ici: "borel = sigma UNIV (range (\<lambda>x::real. {x ..}))"
  1983 proof (safe intro!: borel_eq_sigmaI1[OF borel_Iio])
  1984   fix x :: real
  1985   have eq: "{..<x} = space (sigma UNIV (range atLeast)) - {x ..}"
  1986     by auto
  1987   show "{..<x} \<in> sets (sigma UNIV (range atLeast))"
  1988     unfolding eq by (intro sets.compl_sets) auto
  1989 qed auto
  1990 
  1991 lemma borel_measurable_pred_less[measurable (raw)]:
  1992   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
  1993   shows "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> Measurable.pred M (\<lambda>w. f w < g w)"
  1994   unfolding Measurable.pred_def by (rule borel_measurable_less)
  1995 
  1996 no_notation
  1997   eucl_less (infix "<e" 50)
  1998 
  1999 lemma borel_measurable_Max2[measurable (raw)]:
  2000   fixes f::"_ \<Rightarrow> _ \<Rightarrow> 'a::{second_countable_topology, dense_linorder, linorder_topology}"
  2001   assumes "finite I"
  2002     and [measurable]: "\<And>i. f i \<in> borel_measurable M"
  2003   shows "(\<lambda>x. Max{f i x |i. i \<in> I}) \<in> borel_measurable M"
  2004   by (simp add: borel_measurable_Max[OF assms(1), where ?f=f and ?M=M] Setcompr_eq_image)
  2005 
  2006 lemma measurable_compose_n [measurable (raw)]:
  2007   assumes "T \<in> measurable M M"
  2008   shows "(T^^n) \<in> measurable M M"
  2009 by (induction n, auto simp add: measurable_compose[OF _ assms])
  2010 
  2011 lemma measurable_real_imp_nat:
  2012   fixes f::"'a \<Rightarrow> nat"
  2013   assumes [measurable]: "(\<lambda>x. real(f x)) \<in> borel_measurable M"
  2014   shows "f \<in> measurable M (count_space UNIV)"
  2015 proof -
  2016   let ?g = "(\<lambda>x. real(f x))"
  2017   have "\<And>(n::nat). ?g-`({real n}) \<inter> space M = f-`{n} \<inter> space M" by auto
  2018   moreover have "\<And>(n::nat). ?g-`({real n}) \<inter> space M \<in> sets M" using assms by measurable
  2019   ultimately have "\<And>(n::nat). f-`{n} \<inter> space M \<in> sets M" by simp
  2020   then show ?thesis using measurable_count_space_eq2_countable by blast
  2021 qed
  2022 
  2023 lemma measurable_equality_set [measurable]:
  2024   fixes f g::"_\<Rightarrow> 'a::{second_countable_topology, t2_space}"
  2025   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  2026   shows "{x \<in> space M. f x = g x} \<in> sets M"
  2027 
  2028 proof -
  2029   define A where "A = {x \<in> space M. f x = g x}"
  2030   define B where "B = {y. \<exists>x::'a. y = (x,x)}"
  2031   have "A = (\<lambda>x. (f x, g x))-`B \<inter> space M" unfolding A_def B_def by auto
  2032   moreover have "(\<lambda>x. (f x, g x)) \<in> borel_measurable M" by simp
  2033   moreover have "B \<in> sets borel" unfolding B_def by (simp add: closed_diagonal)
  2034   ultimately have "A \<in> sets M" by simp
  2035   then show ?thesis unfolding A_def by simp
  2036 qed
  2037 
  2038 lemma measurable_inequality_set [measurable]:
  2039   fixes f g::"_ \<Rightarrow> 'a::{second_countable_topology, linorder_topology}"
  2040   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  2041   shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
  2042         "{x \<in> space M. f x < g x} \<in> sets M"
  2043         "{x \<in> space M. f x \<ge> g x} \<in> sets M"
  2044         "{x \<in> space M. f x > g x} \<in> sets M"
  2045 proof -
  2046   define F where "F = (\<lambda>x. (f x, g x))"
  2047   have * [measurable]: "F \<in> borel_measurable M" unfolding F_def by simp
  2048 
  2049   have "{x \<in> space M. f x \<le> g x} = F-`{(x, y) | x y. x \<le> y} \<inter> space M" unfolding F_def by auto
  2050   moreover have "{(x, y) | x y. x \<le> (y::'a)} \<in> sets borel" using closed_subdiagonal borel_closed by blast
  2051   ultimately show "{x \<in> space M. f x \<le> g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
  2052 
  2053   have "{x \<in> space M. f x < g x} = F-`{(x, y) | x y. x < y} \<inter> space M" unfolding F_def by auto
  2054   moreover have "{(x, y) | x y. x < (y::'a)} \<in> sets borel" using open_subdiagonal borel_open by blast
  2055   ultimately show "{x \<in> space M. f x < g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
  2056 
  2057   have "{x \<in> space M. f x \<ge> g x} = F-`{(x, y) | x y. x \<ge> y} \<inter> space M" unfolding F_def by auto
  2058   moreover have "{(x, y) | x y. x \<ge> (y::'a)} \<in> sets borel" using closed_superdiagonal borel_closed by blast
  2059   ultimately show "{x \<in> space M. f x \<ge> g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
  2060 
  2061   have "{x \<in> space M. f x > g x} = F-`{(x, y) | x y. x > y} \<inter> space M" unfolding F_def by auto
  2062   moreover have "{(x, y) | x y. x > (y::'a)} \<in> sets borel" using open_superdiagonal borel_open by blast
  2063   ultimately show "{x \<in> space M. f x > g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
  2064 qed
  2065 
  2066 proposition measurable_limit [measurable]:
  2067   fixes f::"nat \<Rightarrow> 'a \<Rightarrow> 'b::first_countable_topology"
  2068   assumes [measurable]: "\<And>n::nat. f n \<in> borel_measurable M"
  2069   shows "Measurable.pred M (\<lambda>x. (\<lambda>n. f n x) \<longlonglongrightarrow> c)"
  2070 proof -
  2071   obtain A :: "nat \<Rightarrow> 'b set" where A:
  2072     "\<And>i. open (A i)"
  2073     "\<And>i. c \<in> A i"
  2074     "\<And>S. open S \<Longrightarrow> c \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  2075   by (rule countable_basis_at_decseq) blast
  2076 
  2077   have [measurable]: "\<And>N i. (f N)-`(A i) \<inter> space M \<in> sets M" using A(1) by auto
  2078   then have mes: "(\<Inter>i. \<Union>n. \<Inter>N\<in>{n..}. (f N)-`(A i) \<inter> space M) \<in> sets M" by blast
  2079 
  2080   have "(u \<longlonglongrightarrow> c) \<longleftrightarrow> (\<forall>i. eventually (\<lambda>n. u n \<in> A i) sequentially)" for u::"nat \<Rightarrow> 'b"
  2081   proof
  2082     assume "u \<longlonglongrightarrow> c"
  2083     then have "eventually (\<lambda>n. u n \<in> A i) sequentially" for i using A(1)[of i] A(2)[of i]
  2084       by (simp add: topological_tendstoD)
  2085     then show "(\<forall>i. eventually (\<lambda>n. u n \<in> A i) sequentially)" by auto
  2086   next
  2087     assume H: "(\<forall>i. eventually (\<lambda>n. u n \<in> A i) sequentially)"
  2088     show "(u \<longlonglongrightarrow> c)"
  2089     proof (rule topological_tendstoI)
  2090       fix S assume "open S" "c \<in> S"
  2091       with A(3)[OF this] obtain i where "A i \<subseteq> S"
  2092         using eventually_False_sequentially eventually_mono by blast
  2093       moreover have "eventually (\<lambda>n. u n \<in> A i) sequentially" using H by simp
  2094       ultimately show "\<forall>\<^sub>F n in sequentially. u n \<in> S"
  2095         by (simp add: eventually_mono subset_eq)
  2096     qed
  2097   qed
  2098   then have "{x. (\<lambda>n. f n x) \<longlonglongrightarrow> c} = (\<Inter>i. \<Union>n. \<Inter>N\<in>{n..}. (f N)-`(A i))"
  2099     by (auto simp add: atLeast_def eventually_at_top_linorder)
  2100   then have "{x \<in> space M. (\<lambda>n. f n x) \<longlonglongrightarrow> c} = (\<Inter>i. \<Union>n. \<Inter>N\<in>{n..}. (f N)-`(A i) \<inter> space M)"
  2101     by auto
  2102   then have "{x \<in> space M. (\<lambda>n. f n x) \<longlonglongrightarrow> c} \<in> sets M" using mes by simp
  2103   then show ?thesis by auto
  2104 qed
  2105 
  2106 lemma measurable_limit2 [measurable]:
  2107   fixes u::"nat \<Rightarrow> 'a \<Rightarrow> real"
  2108   assumes [measurable]: "\<And>n. u n \<in> borel_measurable M" "v \<in> borel_measurable M"
  2109   shows "Measurable.pred M (\<lambda>x. (\<lambda>n. u n x) \<longlonglongrightarrow> v x)"
  2110 proof -
  2111   define w where "w = (\<lambda>n x. u n x - v x)"
  2112   have [measurable]: "w n \<in> borel_measurable M" for n unfolding w_def by auto
  2113   have "((\<lambda>n. u n x) \<longlonglongrightarrow> v x) \<longleftrightarrow> ((\<lambda>n. w n x) \<longlonglongrightarrow> 0)" for x
  2114     unfolding w_def using Lim_null by auto
  2115   then show ?thesis using measurable_limit by auto
  2116 qed
  2117 
  2118 lemma measurable_P_restriction [measurable (raw)]:
  2119   assumes [measurable]: "Measurable.pred M P" "A \<in> sets M"
  2120   shows "{x \<in> A. P x} \<in> sets M"
  2121 proof -
  2122   have "A \<subseteq> space M" using sets.sets_into_space[OF assms(2)].
  2123   then have "{x \<in> A. P x} = A \<inter> {x \<in> space M. P x}" by blast
  2124   then show ?thesis by auto
  2125 qed
  2126 
  2127 lemma measurable_sum_nat [measurable (raw)]:
  2128   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> nat"
  2129   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> measurable M (count_space UNIV)"
  2130   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> measurable M (count_space UNIV)"
  2131 proof cases
  2132   assume "finite S"
  2133   then show ?thesis using assms by induct auto
  2134 qed simp
  2135 
  2136 
  2137 lemma measurable_abs_powr [measurable]:
  2138   fixes p::real
  2139   assumes [measurable]: "f \<in> borel_measurable M"
  2140   shows "(\<lambda>x. \<bar>f x\<bar> powr p) \<in> borel_measurable M"
  2141 unfolding powr_def by auto
  2142 
  2143 text \<open>The next one is a variation around \<open>measurable_restrict_space\<close>.\<close>
  2144 
  2145 lemma measurable_restrict_space3:
  2146   assumes "f \<in> measurable M N" and
  2147           "f \<in> A \<rightarrow> B"
  2148   shows "f \<in> measurable (restrict_space M A) (restrict_space N B)"
  2149 proof -
  2150   have "f \<in> measurable (restrict_space M A) N" using assms(1) measurable_restrict_space1 by auto
  2151   then show ?thesis by (metis Int_iff funcsetI funcset_mem
  2152       measurable_restrict_space2[of f, of "restrict_space M A", of B, of N] assms(2) space_restrict_space)
  2153 qed
  2154 
  2155 text \<open>The next one is a variation around \<open>measurable_piecewise_restrict\<close>.\<close>
  2156 
  2157 lemma measurable_piecewise_restrict2:
  2158   assumes [measurable]: "\<And>n. A n \<in> sets M"
  2159       and "space M = (\<Union>(n::nat). A n)"
  2160           "\<And>n. \<exists>h \<in> measurable M N. (\<forall>x \<in> A n. f x = h x)"
  2161   shows "f \<in> measurable M N"
  2162 proof (rule measurableI)
  2163   fix B assume [measurable]: "B \<in> sets N"
  2164   {
  2165     fix n::nat
  2166     obtain h where [measurable]: "h \<in> measurable M N" and "\<forall>x \<in> A n. f x = h x" using assms(3) by blast
  2167     then have *: "f-`B \<inter> A n = h-`B \<inter> A n" by auto
  2168     have "h-`B \<inter> A n = h-`B \<inter> space M \<inter> A n" using assms(2) sets.sets_into_space by auto
  2169     then have "h-`B \<inter> A n \<in> sets M" by simp
  2170     then have "f-`B \<inter> A n \<in> sets M" using * by simp
  2171   }
  2172   then have "(\<Union>n. f-`B \<inter> A n) \<in> sets M" by measurable
  2173   moreover have "f-`B \<inter> space M = (\<Union>n. f-`B \<inter> A n)" using assms(2) by blast
  2174   ultimately show "f-`B \<inter> space M \<in> sets M" by simp
  2175 next
  2176   fix x assume "x \<in> space M"
  2177   then obtain n where "x \<in> A n" using assms(2) by blast
  2178   obtain h where [measurable]: "h \<in> measurable M N" and "\<forall>x \<in> A n. f x = h x" using assms(3) by blast
  2179   then have "f x = h x" using \<open>x \<in> A n\<close> by blast
  2180   moreover have "h x \<in> space N" by (metis measurable_space \<open>x \<in> space M\<close> \<open>h \<in> measurable M N\<close>)
  2181   ultimately show "f x \<in> space N" by simp
  2182 qed
  2183 
  2184 end