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