src/HOL/Probability/Fin_Map.thy
author hoelzl
Fri Feb 19 13:40:50 2016 +0100 (2016-02-19)
changeset 62378 85ed00c1fe7c
parent 62343 24106dc44def
child 62390 842917225d56
permissions -rw-r--r--
generalize more theorems to support enat and ennreal
     1 (*  Title:      HOL/Probability/Fin_Map.thy
     2     Author:     Fabian Immler, TU M√ľnchen
     3 *)
     4 
     5 section \<open>Finite Maps\<close>
     6 
     7 theory Fin_Map
     8 imports Finite_Product_Measure
     9 begin
    10 
    11 text \<open>Auxiliary type that is instantiated to @{class polish_space}, needed for the proof of
    12   projective limit. @{const extensional} functions are used for the representation in order to
    13   stay close to the developments of (finite) products @{const Pi\<^sub>E} and their sigma-algebra
    14   @{const Pi\<^sub>M}.\<close>
    15 
    16 typedef ('i, 'a) finmap ("(_ \<Rightarrow>\<^sub>F /_)" [22, 21] 21) =
    17   "{(I::'i set, f::'i \<Rightarrow> 'a). finite I \<and> f \<in> extensional I}" by auto
    18 
    19 subsection \<open>Domain and Application\<close>
    20 
    21 definition domain where "domain P = fst (Rep_finmap P)"
    22 
    23 lemma finite_domain[simp, intro]: "finite (domain P)"
    24   by (cases P) (auto simp: domain_def Abs_finmap_inverse)
    25 
    26 definition proj ("'((_)')\<^sub>F" [0] 1000) where "proj P i = snd (Rep_finmap P) i"
    27 
    28 declare [[coercion proj]]
    29 
    30 lemma extensional_proj[simp, intro]: "(P)\<^sub>F \<in> extensional (domain P)"
    31   by (cases P) (auto simp: domain_def Abs_finmap_inverse proj_def[abs_def])
    32 
    33 lemma proj_undefined[simp, intro]: "i \<notin> domain P \<Longrightarrow> P i = undefined"
    34   using extensional_proj[of P] unfolding extensional_def by auto
    35 
    36 lemma finmap_eq_iff: "P = Q \<longleftrightarrow> (domain P = domain Q \<and> (\<forall>i\<in>domain P. P i = Q i))"
    37   by (cases P, cases Q)
    38      (auto simp add: Abs_finmap_inject extensional_def domain_def proj_def Abs_finmap_inverse
    39               intro: extensionalityI)
    40 
    41 subsection \<open>Countable Finite Maps\<close>
    42 
    43 instance finmap :: (countable, countable) countable
    44 proof
    45   obtain mapper where mapper: "\<And>fm :: 'a \<Rightarrow>\<^sub>F 'b. set (mapper fm) = domain fm"
    46     by (metis finite_list[OF finite_domain])
    47   have "inj (\<lambda>fm. map (\<lambda>i. (i, (fm)\<^sub>F i)) (mapper fm))" (is "inj ?F")
    48   proof (rule inj_onI)
    49     fix f1 f2 assume "?F f1 = ?F f2"
    50     then have "map fst (?F f1) = map fst (?F f2)" by simp
    51     then have "mapper f1 = mapper f2" by (simp add: comp_def)
    52     then have "domain f1 = domain f2" by (simp add: mapper[symmetric])
    53     with \<open>?F f1 = ?F f2\<close> show "f1 = f2"
    54       unfolding \<open>mapper f1 = mapper f2\<close> map_eq_conv mapper
    55       by (simp add: finmap_eq_iff)
    56   qed
    57   then show "\<exists>to_nat :: 'a \<Rightarrow>\<^sub>F 'b \<Rightarrow> nat. inj to_nat"
    58     by (intro exI[of _ "to_nat \<circ> ?F"] inj_comp) auto
    59 qed
    60 
    61 subsection \<open>Constructor of Finite Maps\<close>
    62 
    63 definition "finmap_of inds f = Abs_finmap (inds, restrict f inds)"
    64 
    65 lemma proj_finmap_of[simp]:
    66   assumes "finite inds"
    67   shows "(finmap_of inds f)\<^sub>F = restrict f inds"
    68   using assms
    69   by (auto simp: Abs_finmap_inverse finmap_of_def proj_def)
    70 
    71 lemma domain_finmap_of[simp]:
    72   assumes "finite inds"
    73   shows "domain (finmap_of inds f) = inds"
    74   using assms
    75   by (auto simp: Abs_finmap_inverse finmap_of_def domain_def)
    76 
    77 lemma finmap_of_eq_iff[simp]:
    78   assumes "finite i" "finite j"
    79   shows "finmap_of i m = finmap_of j n \<longleftrightarrow> i = j \<and> (\<forall>k\<in>i. m k= n k)"
    80   using assms by (auto simp: finmap_eq_iff)
    81 
    82 lemma finmap_of_inj_on_extensional_finite:
    83   assumes "finite K"
    84   assumes "S \<subseteq> extensional K"
    85   shows "inj_on (finmap_of K) S"
    86 proof (rule inj_onI)
    87   fix x y::"'a \<Rightarrow> 'b"
    88   assume "finmap_of K x = finmap_of K y"
    89   hence "(finmap_of K x)\<^sub>F = (finmap_of K y)\<^sub>F" by simp
    90   moreover
    91   assume "x \<in> S" "y \<in> S" hence "x \<in> extensional K" "y \<in> extensional K" using assms by auto
    92   ultimately
    93   show "x = y" using assms by (simp add: extensional_restrict)
    94 qed
    95 
    96 subsection \<open>Product set of Finite Maps\<close>
    97 
    98 text \<open>This is @{term Pi} for Finite Maps, most of this is copied\<close>
    99 
   100 definition Pi' :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a set) \<Rightarrow> ('i \<Rightarrow>\<^sub>F 'a) set" where
   101   "Pi' I A = { P. domain P = I \<and> (\<forall>i. i \<in> I \<longrightarrow> (P)\<^sub>F i \<in> A i) } "
   102 
   103 syntax
   104   "_Pi'" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>' _\<in>_./ _)"   10)
   105 translations
   106   "\<Pi>' x\<in>A. B" == "CONST Pi' A (\<lambda>x. B)"
   107 
   108 subsubsection\<open>Basic Properties of @{term Pi'}\<close>
   109 
   110 lemma Pi'_I[intro!]: "domain f = A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> f x \<in> B x) \<Longrightarrow> f \<in> Pi' A B"
   111   by (simp add: Pi'_def)
   112 
   113 lemma Pi'_I'[simp]: "domain f = A \<Longrightarrow> (\<And>x. x \<in> A \<longrightarrow> f x \<in> B x) \<Longrightarrow> f \<in> Pi' A B"
   114   by (simp add:Pi'_def)
   115 
   116 lemma Pi'_mem: "f\<in> Pi' A B \<Longrightarrow> x \<in> A \<Longrightarrow> f x \<in> B x"
   117   by (simp add: Pi'_def)
   118 
   119 lemma Pi'_iff: "f \<in> Pi' I X \<longleftrightarrow> domain f = I \<and> (\<forall>i\<in>I. f i \<in> X i)"
   120   unfolding Pi'_def by auto
   121 
   122 lemma Pi'E [elim]:
   123   "f \<in> Pi' A B \<Longrightarrow> (f x \<in> B x \<Longrightarrow> domain f = A \<Longrightarrow> Q) \<Longrightarrow> (x \<notin> A \<Longrightarrow> Q) \<Longrightarrow> Q"
   124   by(auto simp: Pi'_def)
   125 
   126 lemma in_Pi'_cong:
   127   "domain f = domain g \<Longrightarrow> (\<And> w. w \<in> A \<Longrightarrow> f w = g w) \<Longrightarrow> f \<in> Pi' A B \<longleftrightarrow> g \<in> Pi' A B"
   128   by (auto simp: Pi'_def)
   129 
   130 lemma Pi'_eq_empty[simp]:
   131   assumes "finite A" shows "(Pi' A B) = {} \<longleftrightarrow> (\<exists>x\<in>A. B x = {})"
   132   using assms
   133   apply (simp add: Pi'_def, auto)
   134   apply (drule_tac x = "finmap_of A (\<lambda>u. SOME y. y \<in> B u)" in spec, auto)
   135   apply (cut_tac P= "%y. y \<in> B i" in some_eq_ex, auto)
   136   done
   137 
   138 lemma Pi'_mono: "(\<And>x. x \<in> A \<Longrightarrow> B x \<subseteq> C x) \<Longrightarrow> Pi' A B \<subseteq> Pi' A C"
   139   by (auto simp: Pi'_def)
   140 
   141 lemma Pi_Pi': "finite A \<Longrightarrow> (Pi\<^sub>E A B) = proj ` Pi' A B"
   142   apply (auto simp: Pi'_def Pi_def extensional_def)
   143   apply (rule_tac x = "finmap_of A (restrict x A)" in image_eqI)
   144   apply auto
   145   done
   146 
   147 subsection \<open>Topological Space of Finite Maps\<close>
   148 
   149 instantiation finmap :: (type, topological_space) topological_space
   150 begin
   151 
   152 definition open_finmap :: "('a \<Rightarrow>\<^sub>F 'b) set \<Rightarrow> bool" where
   153    [code del]: "open_finmap = generate_topology {Pi' a b|a b. \<forall>i\<in>a. open (b i)}"
   154 
   155 lemma open_Pi'I: "(\<And>i. i \<in> I \<Longrightarrow> open (A i)) \<Longrightarrow> open (Pi' I A)"
   156   by (auto intro: generate_topology.Basis simp: open_finmap_def)
   157 
   158 instance using topological_space_generate_topology
   159   by intro_classes (auto simp: open_finmap_def class.topological_space_def)
   160 
   161 end
   162 
   163 lemma open_restricted_space:
   164   shows "open {m. P (domain m)}"
   165 proof -
   166   have "{m. P (domain m)} = (\<Union>i \<in> Collect P. {m. domain m = i})" by auto
   167   also have "open \<dots>"
   168   proof (rule, safe, cases)
   169     fix i::"'a set"
   170     assume "finite i"
   171     hence "{m. domain m = i} = Pi' i (\<lambda>_. UNIV)" by (auto simp: Pi'_def)
   172     also have "open \<dots>" by (auto intro: open_Pi'I simp: \<open>finite i\<close>)
   173     finally show "open {m. domain m = i}" .
   174   next
   175     fix i::"'a set"
   176     assume "\<not> finite i" hence "{m. domain m = i} = {}" by auto
   177     also have "open \<dots>" by simp
   178     finally show "open {m. domain m = i}" .
   179   qed
   180   finally show ?thesis .
   181 qed
   182 
   183 lemma closed_restricted_space:
   184   shows "closed {m. P (domain m)}"
   185   using open_restricted_space[of "\<lambda>x. \<not> P x"]
   186   unfolding closed_def by (rule back_subst) auto
   187 
   188 lemma tendsto_proj: "((\<lambda>x. x) \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. (x)\<^sub>F i) \<longlongrightarrow> (a)\<^sub>F i) F"
   189   unfolding tendsto_def
   190 proof safe
   191   fix S::"'b set"
   192   let ?S = "Pi' (domain a) (\<lambda>x. if x = i then S else UNIV)"
   193   assume "open S" hence "open ?S" by (auto intro!: open_Pi'I)
   194   moreover assume "\<forall>S. open S \<longrightarrow> a \<in> S \<longrightarrow> eventually (\<lambda>x. x \<in> S) F" "a i \<in> S"
   195   ultimately have "eventually (\<lambda>x. x \<in> ?S) F" by auto
   196   thus "eventually (\<lambda>x. (x)\<^sub>F i \<in> S) F"
   197     by eventually_elim (insert \<open>a i \<in> S\<close>, force simp: Pi'_iff split: split_if_asm)
   198 qed
   199 
   200 lemma continuous_proj:
   201   shows "continuous_on s (\<lambda>x. (x)\<^sub>F i)"
   202   unfolding continuous_on_def by (safe intro!: tendsto_proj tendsto_ident_at)
   203 
   204 instance finmap :: (type, first_countable_topology) first_countable_topology
   205 proof
   206   fix x::"'a\<Rightarrow>\<^sub>F'b"
   207   have "\<forall>i. \<exists>A. countable A \<and> (\<forall>a\<in>A. x i \<in> a) \<and> (\<forall>a\<in>A. open a) \<and>
   208     (\<forall>S. open S \<and> x i \<in> S \<longrightarrow> (\<exists>a\<in>A. a \<subseteq> S)) \<and> (\<forall>a b. a \<in> A \<longrightarrow> b \<in> A \<longrightarrow> a \<inter> b \<in> A)" (is "\<forall>i. ?th i")
   209   proof
   210     fix i from first_countable_basis_Int_stableE[of "x i"] guess A .
   211     thus "?th i" by (intro exI[where x=A]) simp
   212   qed
   213   then guess A unfolding choice_iff .. note A = this
   214   hence open_sub: "\<And>i S. i\<in>domain x \<Longrightarrow> open (S i) \<Longrightarrow> x i\<in>(S i) \<Longrightarrow> (\<exists>a\<in>A i. a\<subseteq>(S i))" by auto
   215   have A_notempty: "\<And>i. i \<in> domain x \<Longrightarrow> A i \<noteq> {}" using open_sub[of _ "\<lambda>_. UNIV"] by auto
   216   let ?A = "(\<lambda>f. Pi' (domain x) f) ` (Pi\<^sub>E (domain x) A)"
   217   show "\<exists>A::nat \<Rightarrow> ('a\<Rightarrow>\<^sub>F'b) set. (\<forall>i. x \<in> (A i) \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
   218   proof (rule first_countableI[where A="?A"], safe)
   219     show "countable ?A" using A by (simp add: countable_PiE)
   220   next
   221     fix S::"('a \<Rightarrow>\<^sub>F 'b) set" assume "open S" "x \<in> S"
   222     thus "\<exists>a\<in>?A. a \<subseteq> S" unfolding open_finmap_def
   223     proof (induct rule: generate_topology.induct)
   224       case UNIV thus ?case by (auto simp add: ex_in_conv PiE_eq_empty_iff A_notempty)
   225     next
   226       case (Int a b)
   227       then obtain f g where
   228         "f \<in> Pi\<^sub>E (domain x) A" "Pi' (domain x) f \<subseteq> a" "g \<in> Pi\<^sub>E (domain x) A" "Pi' (domain x) g \<subseteq> b"
   229         by auto
   230       thus ?case using A
   231         by (auto simp: Pi'_iff PiE_iff extensional_def Int_stable_def
   232             intro!: bexI[where x="\<lambda>i. f i \<inter> g i"])
   233     next
   234       case (UN B)
   235       then obtain b where "x \<in> b" "b \<in> B" by auto
   236       hence "\<exists>a\<in>?A. a \<subseteq> b" using UN by simp
   237       thus ?case using \<open>b \<in> B\<close> by blast
   238     next
   239       case (Basis s)
   240       then obtain a b where xs: "x\<in> Pi' a b" "s = Pi' a b" "\<And>i. i\<in>a \<Longrightarrow> open (b i)" by auto
   241       have "\<forall>i. \<exists>a. (i \<in> domain x \<and> open (b i) \<and> (x)\<^sub>F i \<in> b i) \<longrightarrow> (a\<in>A i \<and> a \<subseteq> b i)"
   242         using open_sub[of _ b] by auto
   243       then obtain b'
   244         where "\<And>i. i \<in> domain x \<Longrightarrow> open (b i) \<Longrightarrow> (x)\<^sub>F i \<in> b i \<Longrightarrow> (b' i \<in>A i \<and> b' i \<subseteq> b i)"
   245           unfolding choice_iff by auto
   246       with xs have "\<And>i. i \<in> a \<Longrightarrow> (b' i \<in>A i \<and> b' i \<subseteq> b i)" "Pi' a b' \<subseteq> Pi' a b"
   247         by (auto simp: Pi'_iff intro!: Pi'_mono)
   248       thus ?case using xs
   249         by (intro bexI[where x="Pi' a b'"])
   250           (auto simp: Pi'_iff intro!: image_eqI[where x="restrict b' (domain x)"])
   251     qed
   252   qed (insert A,auto simp: PiE_iff intro!: open_Pi'I)
   253 qed
   254 
   255 subsection \<open>Metric Space of Finite Maps\<close>
   256 
   257 (* TODO: Product of uniform spaces and compatibility with metric_spaces! *)
   258 
   259 instantiation finmap :: (type, metric_space) dist
   260 begin
   261 
   262 definition dist_finmap where
   263   "dist P Q = Max (range (\<lambda>i. dist ((P)\<^sub>F i) ((Q)\<^sub>F i))) + (if domain P = domain Q then 0 else 1)"
   264 
   265 instance ..
   266 end
   267 
   268 instantiation finmap :: (type, metric_space) uniformity_dist
   269 begin
   270 
   271 definition [code del]:
   272   "(uniformity :: (('a, 'b) finmap \<times> ('a, 'b) finmap) filter) =
   273     (INF e:{0 <..}. principal {(x, y). dist x y < e})"
   274 
   275 instance
   276   by standard (rule uniformity_finmap_def)
   277 end
   278 
   279 declare uniformity_Abort[where 'a="('a, 'b::metric_space) finmap", code]
   280 
   281 instantiation finmap :: (type, metric_space) metric_space
   282 begin
   283 
   284 lemma finite_proj_image': "x \<notin> domain P \<Longrightarrow> finite ((P)\<^sub>F ` S)"
   285   by (rule finite_subset[of _ "proj P ` (domain P \<inter> S \<union> {x})"]) auto
   286 
   287 lemma finite_proj_image: "finite ((P)\<^sub>F ` S)"
   288  by (cases "\<exists>x. x \<notin> domain P") (auto intro: finite_proj_image' finite_subset[where B="domain P"])
   289 
   290 lemma finite_proj_diag: "finite ((\<lambda>i. d ((P)\<^sub>F i) ((Q)\<^sub>F i)) ` S)"
   291 proof -
   292   have "(\<lambda>i. d ((P)\<^sub>F i) ((Q)\<^sub>F i)) ` S = (\<lambda>(i, j). d i j) ` ((\<lambda>i. ((P)\<^sub>F i, (Q)\<^sub>F i)) ` S)" by auto
   293   moreover have "((\<lambda>i. ((P)\<^sub>F i, (Q)\<^sub>F i)) ` S) \<subseteq> (\<lambda>i. (P)\<^sub>F i) ` S \<times> (\<lambda>i. (Q)\<^sub>F i) ` S" by auto
   294   moreover have "finite \<dots>" using finite_proj_image[of P S] finite_proj_image[of Q S]
   295     by (intro finite_cartesian_product) simp_all
   296   ultimately show ?thesis by (simp add: finite_subset)
   297 qed
   298 
   299 lemma dist_le_1_imp_domain_eq:
   300   shows "dist P Q < 1 \<Longrightarrow> domain P = domain Q"
   301   by (simp add: dist_finmap_def finite_proj_diag split: split_if_asm)
   302 
   303 lemma dist_proj:
   304   shows "dist ((x)\<^sub>F i) ((y)\<^sub>F i) \<le> dist x y"
   305 proof -
   306   have "dist (x i) (y i) \<le> Max (range (\<lambda>i. dist (x i) (y i)))"
   307     by (simp add: Max_ge_iff finite_proj_diag)
   308   also have "\<dots> \<le> dist x y" by (simp add: dist_finmap_def)
   309   finally show ?thesis .
   310 qed
   311 
   312 lemma dist_finmap_lessI:
   313   assumes "domain P = domain Q"
   314   assumes "0 < e"
   315   assumes "\<And>i. i \<in> domain P \<Longrightarrow> dist (P i) (Q i) < e"
   316   shows "dist P Q < e"
   317 proof -
   318   have "dist P Q = Max (range (\<lambda>i. dist (P i) (Q i)))"
   319     using assms by (simp add: dist_finmap_def finite_proj_diag)
   320   also have "\<dots> < e"
   321   proof (subst Max_less_iff, safe)
   322     fix i
   323     show "dist ((P)\<^sub>F i) ((Q)\<^sub>F i) < e" using assms
   324       by (cases "i \<in> domain P") simp_all
   325   qed (simp add: finite_proj_diag)
   326   finally show ?thesis .
   327 qed
   328 
   329 instance
   330 proof
   331   fix S::"('a \<Rightarrow>\<^sub>F 'b) set"
   332   have *: "open S = (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)" (is "_ = ?od")
   333   proof
   334     assume "open S"
   335     thus ?od
   336       unfolding open_finmap_def
   337     proof (induct rule: generate_topology.induct)
   338       case UNIV thus ?case by (auto intro: zero_less_one)
   339     next
   340       case (Int a b)
   341       show ?case
   342       proof safe
   343         fix x assume x: "x \<in> a" "x \<in> b"
   344         with Int x obtain e1 e2 where
   345           "e1>0" "\<forall>y. dist y x < e1 \<longrightarrow> y \<in> a" "e2>0" "\<forall>y. dist y x < e2 \<longrightarrow> y \<in> b" by force
   346         thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> a \<inter> b"
   347           by (auto intro!: exI[where x="min e1 e2"])
   348       qed
   349     next
   350       case (UN K)
   351       show ?case
   352       proof safe
   353         fix x X assume "x \<in> X" and X: "X \<in> K"
   354         with UN obtain e where "e>0" "\<And>y. dist y x < e \<longrightarrow> y \<in> X" by force
   355         with X show "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> \<Union>K" by auto
   356       qed
   357     next
   358       case (Basis s) then obtain a b where s: "s = Pi' a b" and b: "\<And>i. i\<in>a \<Longrightarrow> open (b i)" by auto
   359       show ?case
   360       proof safe
   361         fix x assume "x \<in> s"
   362         hence [simp]: "finite a" and a_dom: "a = domain x" using s by (auto simp: Pi'_iff)
   363         obtain es where es: "\<forall>i \<in> a. es i > 0 \<and> (\<forall>y. dist y (proj x i) < es i \<longrightarrow> y \<in> b i)"
   364           using b \<open>x \<in> s\<close> by atomize_elim (intro bchoice, auto simp: open_dist s)
   365         hence in_b: "\<And>i y. i \<in> a \<Longrightarrow> dist y (proj x i) < es i \<Longrightarrow> y \<in> b i" by auto
   366         show "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> s"
   367         proof (cases, rule, safe)
   368           assume "a \<noteq> {}"
   369           show "0 < min 1 (Min (es ` a))" using es by (auto simp: \<open>a \<noteq> {}\<close>)
   370           fix y assume d: "dist y x < min 1 (Min (es ` a))"
   371           show "y \<in> s" unfolding s
   372           proof
   373             show "domain y = a" using d s \<open>a \<noteq> {}\<close> by (auto simp: dist_le_1_imp_domain_eq a_dom)
   374             fix i assume i: "i \<in> a"
   375             hence "dist ((y)\<^sub>F i) ((x)\<^sub>F i) < es i" using d
   376               by (auto simp: dist_finmap_def \<open>a \<noteq> {}\<close> intro!: le_less_trans[OF dist_proj])
   377             with i show "y i \<in> b i" by (rule in_b)
   378           qed
   379         next
   380           assume "\<not>a \<noteq> {}"
   381           thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> s"
   382             using s \<open>x \<in> s\<close> by (auto simp: Pi'_def dist_le_1_imp_domain_eq intro!: exI[where x=1])
   383         qed
   384       qed
   385     qed
   386   next
   387     assume "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   388     then obtain e where e_pos: "\<And>x. x \<in> S \<Longrightarrow> e x > 0" and
   389       e_in:  "\<And>x y . x \<in> S \<Longrightarrow> dist y x < e x \<Longrightarrow> y \<in> S"
   390       unfolding bchoice_iff
   391       by auto
   392     have S_eq: "S = \<Union>{Pi' a b| a b. \<exists>x\<in>S. domain x = a \<and> b = (\<lambda>i. ball (x i) (e x))}"
   393     proof safe
   394       fix x assume "x \<in> S"
   395       thus "x \<in> \<Union>{Pi' a b| a b. \<exists>x\<in>S. domain x = a \<and> b = (\<lambda>i. ball (x i) (e x))}"
   396         using e_pos by (auto intro!: exI[where x="Pi' (domain x) (\<lambda>i. ball (x i) (e x))"])
   397     next
   398       fix x y
   399       assume "y \<in> S"
   400       moreover
   401       assume "x \<in> (\<Pi>' i\<in>domain y. ball (y i) (e y))"
   402       hence "dist x y < e y" using e_pos \<open>y \<in> S\<close>
   403         by (auto simp: dist_finmap_def Pi'_iff finite_proj_diag dist_commute)
   404       ultimately show "x \<in> S" by (rule e_in)
   405     qed
   406     also have "open \<dots>"
   407       unfolding open_finmap_def
   408       by (intro generate_topology.UN) (auto intro: generate_topology.Basis)
   409     finally show "open S" .
   410   qed
   411   show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
   412     unfolding * eventually_uniformity_metric
   413     by (simp del: split_paired_All add: dist_finmap_def dist_commute eq_commute)
   414 next
   415   fix P Q::"'a \<Rightarrow>\<^sub>F 'b"
   416   have Max_eq_iff: "\<And>A m. finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> (Max A = m) = (m \<in> A \<and> (\<forall>a\<in>A. a \<le> m))"
   417     by (auto intro: Max_in Max_eqI)
   418   show "dist P Q = 0 \<longleftrightarrow> P = Q"
   419     by (auto simp: finmap_eq_iff dist_finmap_def Max_ge_iff finite_proj_diag Max_eq_iff
   420         add_nonneg_eq_0_iff
   421       intro!: Max_eqI image_eqI[where x=undefined])
   422 next
   423   fix P Q R::"'a \<Rightarrow>\<^sub>F 'b"
   424   let ?dists = "\<lambda>P Q i. dist ((P)\<^sub>F i) ((Q)\<^sub>F i)"
   425   let ?dpq = "?dists P Q" and ?dpr = "?dists P R" and ?dqr = "?dists Q R"
   426   let ?dom = "\<lambda>P Q. (if domain P = domain Q then 0 else 1::real)"
   427   have "dist P Q = Max (range ?dpq) + ?dom P Q"
   428     by (simp add: dist_finmap_def)
   429   also obtain t where "t \<in> range ?dpq" "t = Max (range ?dpq)" by (simp add: finite_proj_diag)
   430   then obtain i where "Max (range ?dpq) = ?dpq i" by auto
   431   also have "?dpq i \<le> ?dpr i + ?dqr i" by (rule dist_triangle2)
   432   also have "?dpr i \<le> Max (range ?dpr)" by (simp add: finite_proj_diag)
   433   also have "?dqr i \<le> Max (range ?dqr)" by (simp add: finite_proj_diag)
   434   also have "?dom P Q \<le> ?dom P R + ?dom Q R" by simp
   435   finally show "dist P Q \<le> dist P R + dist Q R" by (simp add: dist_finmap_def ac_simps)
   436 qed
   437 
   438 end
   439 
   440 subsection \<open>Complete Space of Finite Maps\<close>
   441 
   442 lemma tendsto_finmap:
   443   fixes f::"nat \<Rightarrow> ('i \<Rightarrow>\<^sub>F ('a::metric_space))"
   444   assumes ind_f:  "\<And>n. domain (f n) = domain g"
   445   assumes proj_g:  "\<And>i. i \<in> domain g \<Longrightarrow> (\<lambda>n. (f n) i) \<longlonglongrightarrow> g i"
   446   shows "f \<longlonglongrightarrow> g"
   447   unfolding tendsto_iff
   448 proof safe
   449   fix e::real assume "0 < e"
   450   let ?dists = "\<lambda>x i. dist ((f x)\<^sub>F i) ((g)\<^sub>F i)"
   451   have "eventually (\<lambda>x. \<forall>i\<in>domain g. ?dists x i < e) sequentially"
   452     using finite_domain[of g] proj_g
   453   proof induct
   454     case (insert i G)
   455     with \<open>0 < e\<close> have "eventually (\<lambda>x. ?dists x i < e) sequentially" by (auto simp add: tendsto_iff)
   456     moreover
   457     from insert have "eventually (\<lambda>x. \<forall>i\<in>G. dist ((f x)\<^sub>F i) ((g)\<^sub>F i) < e) sequentially" by simp
   458     ultimately show ?case by eventually_elim auto
   459   qed simp
   460   thus "eventually (\<lambda>x. dist (f x) g < e) sequentially"
   461     by eventually_elim (auto simp add: dist_finmap_def finite_proj_diag ind_f \<open>0 < e\<close>)
   462 qed
   463 
   464 instance finmap :: (type, complete_space) complete_space
   465 proof
   466   fix P::"nat \<Rightarrow> 'a \<Rightarrow>\<^sub>F 'b"
   467   assume "Cauchy P"
   468   then obtain Nd where Nd: "\<And>n. n \<ge> Nd \<Longrightarrow> dist (P n) (P Nd) < 1"
   469     by (force simp: cauchy)
   470   def d \<equiv> "domain (P Nd)"
   471   with Nd have dim: "\<And>n. n \<ge> Nd \<Longrightarrow> domain (P n) = d" using dist_le_1_imp_domain_eq by auto
   472   have [simp]: "finite d" unfolding d_def by simp
   473   def p \<equiv> "\<lambda>i n. (P n) i"
   474   def q \<equiv> "\<lambda>i. lim (p i)"
   475   def Q \<equiv> "finmap_of d q"
   476   have q: "\<And>i. i \<in> d \<Longrightarrow> q i = Q i" by (auto simp add: Q_def Abs_finmap_inverse)
   477   {
   478     fix i assume "i \<in> d"
   479     have "Cauchy (p i)" unfolding cauchy p_def
   480     proof safe
   481       fix e::real assume "0 < e"
   482       with \<open>Cauchy P\<close> obtain N where N: "\<And>n. n\<ge>N \<Longrightarrow> dist (P n) (P N) < min e 1"
   483         by (force simp: cauchy min_def)
   484       hence "\<And>n. n \<ge> N \<Longrightarrow> domain (P n) = domain (P N)" using dist_le_1_imp_domain_eq by auto
   485       with dim have dim: "\<And>n. n \<ge> N \<Longrightarrow> domain (P n) = d" by (metis nat_le_linear)
   486       show "\<exists>N. \<forall>n\<ge>N. dist ((P n) i) ((P N) i) < e"
   487       proof (safe intro!: exI[where x="N"])
   488         fix n assume "N \<le> n" have "N \<le> N" by simp
   489         have "dist ((P n) i) ((P N) i) \<le> dist (P n) (P N)"
   490           using dim[OF \<open>N \<le> n\<close>]  dim[OF \<open>N \<le> N\<close>] \<open>i \<in> d\<close>
   491           by (auto intro!: dist_proj)
   492         also have "\<dots> < e" using N[OF \<open>N \<le> n\<close>] by simp
   493         finally show "dist ((P n) i) ((P N) i) < e" .
   494       qed
   495     qed
   496     hence "convergent (p i)" by (metis Cauchy_convergent_iff)
   497     hence "p i \<longlonglongrightarrow> q i" unfolding q_def convergent_def by (metis limI)
   498   } note p = this
   499   have "P \<longlonglongrightarrow> Q"
   500   proof (rule metric_LIMSEQ_I)
   501     fix e::real assume "0 < e"
   502     have "\<exists>ni. \<forall>i\<in>d. \<forall>n\<ge>ni i. dist (p i n) (q i) < e"
   503     proof (safe intro!: bchoice)
   504       fix i assume "i \<in> d"
   505       from p[OF \<open>i \<in> d\<close>, THEN metric_LIMSEQ_D, OF \<open>0 < e\<close>]
   506       show "\<exists>no. \<forall>n\<ge>no. dist (p i n) (q i) < e" .
   507     qed then guess ni .. note ni = this
   508     def N \<equiv> "max Nd (Max (ni ` d))"
   509     show "\<exists>N. \<forall>n\<ge>N. dist (P n) Q < e"
   510     proof (safe intro!: exI[where x="N"])
   511       fix n assume "N \<le> n"
   512       hence dom: "domain (P n) = d" "domain Q = d" "domain (P n) = domain Q"
   513         using dim by (simp_all add: N_def Q_def dim_def Abs_finmap_inverse)
   514       show "dist (P n) Q < e"
   515       proof (rule dist_finmap_lessI[OF dom(3) \<open>0 < e\<close>])
   516         fix i
   517         assume "i \<in> domain (P n)"
   518         hence "ni i \<le> Max (ni ` d)" using dom by simp
   519         also have "\<dots> \<le> N" by (simp add: N_def)
   520         finally show "dist ((P n)\<^sub>F i) ((Q)\<^sub>F i) < e" using ni \<open>i \<in> domain (P n)\<close> \<open>N \<le> n\<close> dom
   521           by (auto simp: p_def q N_def less_imp_le)
   522       qed
   523     qed
   524   qed
   525   thus "convergent P" by (auto simp: convergent_def)
   526 qed
   527 
   528 subsection \<open>Second Countable Space of Finite Maps\<close>
   529 
   530 instantiation finmap :: (countable, second_countable_topology) second_countable_topology
   531 begin
   532 
   533 definition basis_proj::"'b set set"
   534   where "basis_proj = (SOME B. countable B \<and> topological_basis B)"
   535 
   536 lemma countable_basis_proj: "countable basis_proj" and basis_proj: "topological_basis basis_proj"
   537   unfolding basis_proj_def by (intro is_basis countable_basis)+
   538 
   539 definition basis_finmap::"('a \<Rightarrow>\<^sub>F 'b) set set"
   540   where "basis_finmap = {Pi' I S|I S. finite I \<and> (\<forall>i \<in> I. S i \<in> basis_proj)}"
   541 
   542 lemma in_basis_finmapI:
   543   assumes "finite I" assumes "\<And>i. i \<in> I \<Longrightarrow> S i \<in> basis_proj"
   544   shows "Pi' I S \<in> basis_finmap"
   545   using assms unfolding basis_finmap_def by auto
   546 
   547 lemma basis_finmap_eq:
   548   assumes "basis_proj \<noteq> {}"
   549   shows "basis_finmap = (\<lambda>f. Pi' (domain f) (\<lambda>i. from_nat_into basis_proj ((f)\<^sub>F i))) `
   550     (UNIV::('a \<Rightarrow>\<^sub>F nat) set)" (is "_ = ?f ` _")
   551   unfolding basis_finmap_def
   552 proof safe
   553   fix I::"'a set" and S::"'a \<Rightarrow> 'b set"
   554   assume "finite I" "\<forall>i\<in>I. S i \<in> basis_proj"
   555   hence "Pi' I S = ?f (finmap_of I (\<lambda>x. to_nat_on basis_proj (S x)))"
   556     by (force simp: Pi'_def countable_basis_proj)
   557   thus "Pi' I S \<in> range ?f" by simp
   558 next
   559   fix x and f::"'a \<Rightarrow>\<^sub>F nat"
   560   show "\<exists>I S. (\<Pi>' i\<in>domain f. from_nat_into basis_proj ((f)\<^sub>F i)) = Pi' I S \<and>
   561     finite I \<and> (\<forall>i\<in>I. S i \<in> basis_proj)"
   562     using assms by (auto intro: from_nat_into)
   563 qed
   564 
   565 lemma basis_finmap_eq_empty: "basis_proj = {} \<Longrightarrow> basis_finmap = {Pi' {} undefined}"
   566   by (auto simp: Pi'_iff basis_finmap_def)
   567 
   568 lemma countable_basis_finmap: "countable basis_finmap"
   569   by (cases "basis_proj = {}") (auto simp: basis_finmap_eq basis_finmap_eq_empty)
   570 
   571 lemma finmap_topological_basis:
   572   "topological_basis basis_finmap"
   573 proof (subst topological_basis_iff, safe)
   574   fix B' assume "B' \<in> basis_finmap"
   575   thus "open B'"
   576     by (auto intro!: open_Pi'I topological_basis_open[OF basis_proj]
   577       simp: topological_basis_def basis_finmap_def Let_def)
   578 next
   579   fix O'::"('a \<Rightarrow>\<^sub>F 'b) set" and x
   580   assume O': "open O'" "x \<in> O'"
   581   then obtain a where a:
   582     "x \<in> Pi' (domain x) a" "Pi' (domain x) a \<subseteq> O'" "\<And>i. i\<in>domain x \<Longrightarrow> open (a i)"
   583     unfolding open_finmap_def
   584   proof (atomize_elim, induct rule: generate_topology.induct)
   585     case (Int a b)
   586     let ?p="\<lambda>a f. x \<in> Pi' (domain x) f \<and> Pi' (domain x) f \<subseteq> a \<and> (\<forall>i. i \<in> domain x \<longrightarrow> open (f i))"
   587     from Int obtain f g where "?p a f" "?p b g" by auto
   588     thus ?case by (force intro!: exI[where x="\<lambda>i. f i \<inter> g i"] simp: Pi'_def)
   589   next
   590     case (UN k)
   591     then obtain kk a where "x \<in> kk" "kk \<in> k" "x \<in> Pi' (domain x) a" "Pi' (domain x) a \<subseteq> kk"
   592       "\<And>i. i\<in>domain x \<Longrightarrow> open (a i)"
   593       by force
   594     thus ?case by blast
   595   qed (auto simp: Pi'_def)
   596   have "\<exists>B.
   597     (\<forall>i\<in>domain x. x i \<in> B i \<and> B i \<subseteq> a i \<and> B i \<in> basis_proj)"
   598   proof (rule bchoice, safe)
   599     fix i assume "i \<in> domain x"
   600     hence "open (a i)" "x i \<in> a i" using a by auto
   601     from topological_basisE[OF basis_proj this] guess b' .
   602     thus "\<exists>y. x i \<in> y \<and> y \<subseteq> a i \<and> y \<in> basis_proj" by auto
   603   qed
   604   then guess B .. note B = this
   605   def B' \<equiv> "Pi' (domain x) (\<lambda>i. (B i)::'b set)"
   606   have "B' \<subseteq> Pi' (domain x) a" using B by (auto intro!: Pi'_mono simp: B'_def)
   607   also note \<open>\<dots> \<subseteq> O'\<close>
   608   finally show "\<exists>B'\<in>basis_finmap. x \<in> B' \<and> B' \<subseteq> O'" using B
   609     by (auto intro!: bexI[where x=B'] Pi'_mono in_basis_finmapI simp: B'_def)
   610 qed
   611 
   612 lemma range_enum_basis_finmap_imp_open:
   613   assumes "x \<in> basis_finmap"
   614   shows "open x"
   615   using finmap_topological_basis assms by (auto simp: topological_basis_def)
   616 
   617 instance proof qed (blast intro: finmap_topological_basis countable_basis_finmap topological_basis_imp_subbasis)
   618 
   619 end
   620 
   621 subsection \<open>Polish Space of Finite Maps\<close>
   622 
   623 instance finmap :: (countable, polish_space) polish_space proof qed
   624 
   625 
   626 subsection \<open>Product Measurable Space of Finite Maps\<close>
   627 
   628 definition "PiF I M \<equiv>
   629   sigma (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j))) {(\<Pi>' j\<in>J. X j) |X J. J \<in> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}"
   630 
   631 abbreviation
   632   "Pi\<^sub>F I M \<equiv> PiF I M"
   633 
   634 syntax
   635   "_PiF" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^sub>F _\<in>_./ _)"  10)
   636 translations
   637   "\<Pi>\<^sub>F x\<in>I. M" == "CONST PiF I (%x. M)"
   638 
   639 lemma PiF_gen_subset: "{(\<Pi>' j\<in>J. X j) |X J. J \<in> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))} \<subseteq>
   640     Pow (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j)))"
   641   by (auto simp: Pi'_def) (blast dest: sets.sets_into_space)
   642 
   643 lemma space_PiF: "space (PiF I M) = (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j)))"
   644   unfolding PiF_def using PiF_gen_subset by (rule space_measure_of)
   645 
   646 lemma sets_PiF:
   647   "sets (PiF I M) = sigma_sets (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j)))
   648     {(\<Pi>' j\<in>J. X j) |X J. J \<in> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}"
   649   unfolding PiF_def using PiF_gen_subset by (rule sets_measure_of)
   650 
   651 lemma sets_PiF_singleton:
   652   "sets (PiF {I} M) = sigma_sets (\<Pi>' j\<in>I. space (M j))
   653     {(\<Pi>' j\<in>I. X j) |X. X \<in> (\<Pi> j\<in>I. sets (M j))}"
   654   unfolding sets_PiF by simp
   655 
   656 lemma in_sets_PiFI:
   657   assumes "X = (Pi' J S)" "J \<in> I" "\<And>i. i\<in>J \<Longrightarrow> S i \<in> sets (M i)"
   658   shows "X \<in> sets (PiF I M)"
   659   unfolding sets_PiF
   660   using assms by blast
   661 
   662 lemma product_in_sets_PiFI:
   663   assumes "J \<in> I" "\<And>i. i\<in>J \<Longrightarrow> S i \<in> sets (M i)"
   664   shows "(Pi' J S) \<in> sets (PiF I M)"
   665   unfolding sets_PiF
   666   using assms by blast
   667 
   668 lemma singleton_space_subset_in_sets:
   669   fixes J
   670   assumes "J \<in> I"
   671   assumes "finite J"
   672   shows "space (PiF {J} M) \<in> sets (PiF I M)"
   673   using assms
   674   by (intro in_sets_PiFI[where J=J and S="\<lambda>i. space (M i)"])
   675       (auto simp: product_def space_PiF)
   676 
   677 lemma singleton_subspace_set_in_sets:
   678   assumes A: "A \<in> sets (PiF {J} M)"
   679   assumes "finite J"
   680   assumes "J \<in> I"
   681   shows "A \<in> sets (PiF I M)"
   682   using A[unfolded sets_PiF]
   683   apply (induct A)
   684   unfolding sets_PiF[symmetric] unfolding space_PiF[symmetric]
   685   using assms
   686   by (auto intro: in_sets_PiFI intro!: singleton_space_subset_in_sets)
   687 
   688 lemma finite_measurable_singletonI:
   689   assumes "finite I"
   690   assumes "\<And>J. J \<in> I \<Longrightarrow> finite J"
   691   assumes MN: "\<And>J. J \<in> I \<Longrightarrow> A \<in> measurable (PiF {J} M) N"
   692   shows "A \<in> measurable (PiF I M) N"
   693   unfolding measurable_def
   694 proof safe
   695   fix y assume "y \<in> sets N"
   696   have "A -` y \<inter> space (PiF I M) = (\<Union>J\<in>I. A -` y \<inter> space (PiF {J} M))"
   697     by (auto simp: space_PiF)
   698   also have "\<dots> \<in> sets (PiF I M)"
   699   proof (rule sets.finite_UN)
   700     show "finite I" by fact
   701     fix J assume "J \<in> I"
   702     with assms have "finite J" by simp
   703     show "A -` y \<inter> space (PiF {J} M) \<in> sets (PiF I M)"
   704       by (rule singleton_subspace_set_in_sets[OF measurable_sets[OF assms(3)]]) fact+
   705   qed
   706   finally show "A -` y \<inter> space (PiF I M) \<in> sets (PiF I M)" .
   707 next
   708   fix x assume "x \<in> space (PiF I M)" thus "A x \<in> space N"
   709     using MN[of "domain x"]
   710     by (auto simp: space_PiF measurable_space Pi'_def)
   711 qed
   712 
   713 lemma countable_finite_comprehension:
   714   fixes f :: "'a::countable set \<Rightarrow> _"
   715   assumes "\<And>s. P s \<Longrightarrow> finite s"
   716   assumes "\<And>s. P s \<Longrightarrow> f s \<in> sets M"
   717   shows "\<Union>{f s|s. P s} \<in> sets M"
   718 proof -
   719   have "\<Union>{f s|s. P s} = (\<Union>n::nat. let s = set (from_nat n) in if P s then f s else {})"
   720   proof safe
   721     fix x X s assume *: "x \<in> f s" "P s"
   722     with assms obtain l where "s = set l" using finite_list by blast
   723     with * show "x \<in> (\<Union>n. let s = set (from_nat n) in if P s then f s else {})" using \<open>P s\<close>
   724       by (auto intro!: exI[where x="to_nat l"])
   725   next
   726     fix x n assume "x \<in> (let s = set (from_nat n) in if P s then f s else {})"
   727     thus "x \<in> \<Union>{f s|s. P s}" using assms by (auto simp: Let_def split: split_if_asm)
   728   qed
   729   hence "\<Union>{f s|s. P s} = (\<Union>n. let s = set (from_nat n) in if P s then f s else {})" by simp
   730   also have "\<dots> \<in> sets M" using assms by (auto simp: Let_def)
   731   finally show ?thesis .
   732 qed
   733 
   734 lemma space_subset_in_sets:
   735   fixes J::"'a::countable set set"
   736   assumes "J \<subseteq> I"
   737   assumes "\<And>j. j \<in> J \<Longrightarrow> finite j"
   738   shows "space (PiF J M) \<in> sets (PiF I M)"
   739 proof -
   740   have "space (PiF J M) = \<Union>{space (PiF {j} M)|j. j \<in> J}"
   741     unfolding space_PiF by blast
   742   also have "\<dots> \<in> sets (PiF I M)" using assms
   743     by (intro countable_finite_comprehension) (auto simp: singleton_space_subset_in_sets)
   744   finally show ?thesis .
   745 qed
   746 
   747 lemma subspace_set_in_sets:
   748   fixes J::"'a::countable set set"
   749   assumes A: "A \<in> sets (PiF J M)"
   750   assumes "J \<subseteq> I"
   751   assumes "\<And>j. j \<in> J \<Longrightarrow> finite j"
   752   shows "A \<in> sets (PiF I M)"
   753   using A[unfolded sets_PiF]
   754   apply (induct A)
   755   unfolding sets_PiF[symmetric] unfolding space_PiF[symmetric]
   756   using assms
   757   by (auto intro: in_sets_PiFI intro!: space_subset_in_sets)
   758 
   759 lemma countable_measurable_PiFI:
   760   fixes I::"'a::countable set set"
   761   assumes MN: "\<And>J. J \<in> I \<Longrightarrow> finite J \<Longrightarrow> A \<in> measurable (PiF {J} M) N"
   762   shows "A \<in> measurable (PiF I M) N"
   763   unfolding measurable_def
   764 proof safe
   765   fix y assume "y \<in> sets N"
   766   have "A -` y = (\<Union>{A -` y \<inter> {x. domain x = J}|J. finite J})" by auto
   767   { fix x::"'a \<Rightarrow>\<^sub>F 'b"
   768     from finite_list[of "domain x"] obtain xs where "set xs = domain x" by auto
   769     hence "\<exists>n. domain x = set (from_nat n)"
   770       by (intro exI[where x="to_nat xs"]) auto }
   771   hence "A -` y \<inter> space (PiF I M) = (\<Union>n. A -` y \<inter> space (PiF ({set (from_nat n)}\<inter>I) M))"
   772     by (auto simp: space_PiF Pi'_def)
   773   also have "\<dots> \<in> sets (PiF I M)"
   774     apply (intro sets.Int sets.countable_nat_UN subsetI, safe)
   775     apply (case_tac "set (from_nat i) \<in> I")
   776     apply simp_all
   777     apply (rule singleton_subspace_set_in_sets[OF measurable_sets[OF MN]])
   778     using assms \<open>y \<in> sets N\<close>
   779     apply (auto simp: space_PiF)
   780     done
   781   finally show "A -` y \<inter> space (PiF I M) \<in> sets (PiF I M)" .
   782 next
   783   fix x assume "x \<in> space (PiF I M)" thus "A x \<in> space N"
   784     using MN[of "domain x"] by (auto simp: space_PiF measurable_space Pi'_def)
   785 qed
   786 
   787 lemma measurable_PiF:
   788   assumes f: "\<And>x. x \<in> space N \<Longrightarrow> domain (f x) \<in> I \<and> (\<forall>i\<in>domain (f x). (f x) i \<in> space (M i))"
   789   assumes S: "\<And>J S. J \<in> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> S i \<in> sets (M i)) \<Longrightarrow>
   790     f -` (Pi' J S) \<inter> space N \<in> sets N"
   791   shows "f \<in> measurable N (PiF I M)"
   792   unfolding PiF_def
   793   using PiF_gen_subset
   794   apply (rule measurable_measure_of)
   795   using f apply force
   796   apply (insert S, auto)
   797   done
   798 
   799 lemma restrict_sets_measurable:
   800   assumes A: "A \<in> sets (PiF I M)" and "J \<subseteq> I"
   801   shows "A \<inter> {m. domain m \<in> J} \<in> sets (PiF J M)"
   802   using A[unfolded sets_PiF]
   803 proof (induct A)
   804   case (Basic a)
   805   then obtain K S where S: "a = Pi' K S" "K \<in> I" "(\<forall>i\<in>K. S i \<in> sets (M i))"
   806     by auto
   807   show ?case
   808   proof cases
   809     assume "K \<in> J"
   810     hence "a \<inter> {m. domain m \<in> J} \<in> {Pi' K X |X K. K \<in> J \<and> X \<in> (\<Pi> j\<in>K. sets (M j))}" using S
   811       by (auto intro!: exI[where x=K] exI[where x=S] simp: Pi'_def)
   812     also have "\<dots> \<subseteq> sets (PiF J M)" unfolding sets_PiF by auto
   813     finally show ?thesis .
   814   next
   815     assume "K \<notin> J"
   816     hence "a \<inter> {m. domain m \<in> J} = {}" using S by (auto simp: Pi'_def)
   817     also have "\<dots> \<in> sets (PiF J M)" by simp
   818     finally show ?thesis .
   819   qed
   820 next
   821   case (Union a)
   822   have "UNION UNIV a \<inter> {m. domain m \<in> J} = (\<Union>i. (a i \<inter> {m. domain m \<in> J}))"
   823     by simp
   824   also have "\<dots> \<in> sets (PiF J M)" using Union by (intro sets.countable_nat_UN) auto
   825   finally show ?case .
   826 next
   827   case (Compl a)
   828   have "(space (PiF I M) - a) \<inter> {m. domain m \<in> J} = (space (PiF J M) - (a \<inter> {m. domain m \<in> J}))"
   829     using \<open>J \<subseteq> I\<close> by (auto simp: space_PiF Pi'_def)
   830   also have "\<dots> \<in> sets (PiF J M)" using Compl by auto
   831   finally show ?case by (simp add: space_PiF)
   832 qed simp
   833 
   834 lemma measurable_finmap_of:
   835   assumes f: "\<And>i. (\<exists>x \<in> space N. i \<in> J x) \<Longrightarrow> (\<lambda>x. f x i) \<in> measurable N (M i)"
   836   assumes J: "\<And>x. x \<in> space N \<Longrightarrow> J x \<in> I" "\<And>x. x \<in> space N \<Longrightarrow> finite (J x)"
   837   assumes JN: "\<And>S. {x. J x = S} \<inter> space N \<in> sets N"
   838   shows "(\<lambda>x. finmap_of (J x) (f x)) \<in> measurable N (PiF I M)"
   839 proof (rule measurable_PiF)
   840   fix x assume "x \<in> space N"
   841   with J[of x] measurable_space[OF f]
   842   show "domain (finmap_of (J x) (f x)) \<in> I \<and>
   843         (\<forall>i\<in>domain (finmap_of (J x) (f x)). (finmap_of (J x) (f x)) i \<in> space (M i))"
   844     by auto
   845 next
   846   fix K S assume "K \<in> I" and *: "\<And>i. i \<in> K \<Longrightarrow> S i \<in> sets (M i)"
   847   with J have eq: "(\<lambda>x. finmap_of (J x) (f x)) -` Pi' K S \<inter> space N =
   848     (if \<exists>x \<in> space N. K = J x \<and> finite K then if K = {} then {x \<in> space N. J x = K}
   849       else (\<Inter>i\<in>K. (\<lambda>x. f x i) -` S i \<inter> {x \<in> space N. J x = K}) else {})"
   850     by (auto simp: Pi'_def)
   851   have r: "{x \<in> space N. J x = K} = space N \<inter> ({x. J x = K} \<inter> space N)" by auto
   852   show "(\<lambda>x. finmap_of (J x) (f x)) -` Pi' K S \<inter> space N \<in> sets N"
   853     unfolding eq r
   854     apply (simp del: INT_simps add: )
   855     apply (intro conjI impI sets.finite_INT JN sets.Int[OF sets.top])
   856     apply simp apply assumption
   857     apply (subst Int_assoc[symmetric])
   858     apply (rule sets.Int)
   859     apply (intro measurable_sets[OF f] *) apply force apply assumption
   860     apply (intro JN)
   861     done
   862 qed
   863 
   864 lemma measurable_PiM_finmap_of:
   865   assumes "finite J"
   866   shows "finmap_of J \<in> measurable (Pi\<^sub>M J M) (PiF {J} M)"
   867   apply (rule measurable_finmap_of)
   868   apply (rule measurable_component_singleton)
   869   apply simp
   870   apply rule
   871   apply (rule \<open>finite J\<close>)
   872   apply simp
   873   done
   874 
   875 lemma proj_measurable_singleton:
   876   assumes "A \<in> sets (M i)"
   877   shows "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space (PiF {I} M) \<in> sets (PiF {I} M)"
   878 proof cases
   879   assume "i \<in> I"
   880   hence "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space (PiF {I} M) =
   881     Pi' I (\<lambda>x. if x = i then A else space (M x))"
   882     using sets.sets_into_space[OF ] \<open>A \<in> sets (M i)\<close> assms
   883     by (auto simp: space_PiF Pi'_def)
   884   thus ?thesis  using assms \<open>A \<in> sets (M i)\<close>
   885     by (intro in_sets_PiFI) auto
   886 next
   887   assume "i \<notin> I"
   888   hence "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space (PiF {I} M) =
   889     (if undefined \<in> A then space (PiF {I} M) else {})" by (auto simp: space_PiF Pi'_def)
   890   thus ?thesis by simp
   891 qed
   892 
   893 lemma measurable_proj_singleton:
   894   assumes "i \<in> I"
   895   shows "(\<lambda>x. (x)\<^sub>F i) \<in> measurable (PiF {I} M) (M i)"
   896   by (unfold measurable_def, intro CollectI conjI ballI proj_measurable_singleton assms)
   897      (insert \<open>i \<in> I\<close>, auto simp: space_PiF)
   898 
   899 lemma measurable_proj_countable:
   900   fixes I::"'a::countable set set"
   901   assumes "y \<in> space (M i)"
   902   shows "(\<lambda>x. if i \<in> domain x then (x)\<^sub>F i else y) \<in> measurable (PiF I M) (M i)"
   903 proof (rule countable_measurable_PiFI)
   904   fix J assume "J \<in> I" "finite J"
   905   show "(\<lambda>x. if i \<in> domain x then x i else y) \<in> measurable (PiF {J} M) (M i)"
   906     unfolding measurable_def
   907   proof safe
   908     fix z assume "z \<in> sets (M i)"
   909     have "(\<lambda>x. if i \<in> domain x then x i else y) -` z \<inter> space (PiF {J} M) =
   910       (\<lambda>x. if i \<in> J then (x)\<^sub>F i else y) -` z \<inter> space (PiF {J} M)"
   911       by (auto simp: space_PiF Pi'_def)
   912     also have "\<dots> \<in> sets (PiF {J} M)" using \<open>z \<in> sets (M i)\<close> \<open>finite J\<close>
   913       by (cases "i \<in> J") (auto intro!: measurable_sets[OF measurable_proj_singleton])
   914     finally show "(\<lambda>x. if i \<in> domain x then x i else y) -` z \<inter> space (PiF {J} M) \<in>
   915       sets (PiF {J} M)" .
   916   qed (insert \<open>y \<in> space (M i)\<close>, auto simp: space_PiF Pi'_def)
   917 qed
   918 
   919 lemma measurable_restrict_proj:
   920   assumes "J \<in> II" "finite J"
   921   shows "finmap_of J \<in> measurable (PiM J M) (PiF II M)"
   922   using assms
   923   by (intro measurable_finmap_of measurable_component_singleton) auto
   924 
   925 lemma measurable_proj_PiM:
   926   fixes J K ::"'a::countable set" and I::"'a set set"
   927   assumes "finite J" "J \<in> I"
   928   assumes "x \<in> space (PiM J M)"
   929   shows "proj \<in> measurable (PiF {J} M) (PiM J M)"
   930 proof (rule measurable_PiM_single)
   931   show "proj \<in> space (PiF {J} M) \<rightarrow> (\<Pi>\<^sub>E i \<in> J. space (M i))"
   932     using assms by (auto simp add: space_PiM space_PiF extensional_def sets_PiF Pi'_def)
   933 next
   934   fix A i assume A: "i \<in> J" "A \<in> sets (M i)"
   935   show "{\<omega> \<in> space (PiF {J} M). (\<omega>)\<^sub>F i \<in> A} \<in> sets (PiF {J} M)"
   936   proof
   937     have "{\<omega> \<in> space (PiF {J} M). (\<omega>)\<^sub>F i \<in> A} =
   938       (\<lambda>\<omega>. (\<omega>)\<^sub>F i) -` A \<inter> space (PiF {J} M)" by auto
   939     also have "\<dots> \<in> sets (PiF {J} M)"
   940       using assms A by (auto intro: measurable_sets[OF measurable_proj_singleton] simp: space_PiM)
   941     finally show ?thesis .
   942   qed simp
   943 qed
   944 
   945 lemma space_PiF_singleton_eq_product:
   946   assumes "finite I"
   947   shows "space (PiF {I} M) = (\<Pi>' i\<in>I. space (M i))"
   948   by (auto simp: product_def space_PiF assms)
   949 
   950 text \<open>adapted from @{thm sets_PiM_single}\<close>
   951 
   952 lemma sets_PiF_single:
   953   assumes "finite I" "I \<noteq> {}"
   954   shows "sets (PiF {I} M) =
   955     sigma_sets (\<Pi>' i\<in>I. space (M i))
   956       {{f\<in>\<Pi>' i\<in>I. space (M i). f i \<in> A} | i A. i \<in> I \<and> A \<in> sets (M i)}"
   957     (is "_ = sigma_sets ?\<Omega> ?R")
   958   unfolding sets_PiF_singleton
   959 proof (rule sigma_sets_eqI)
   960   interpret R: sigma_algebra ?\<Omega> "sigma_sets ?\<Omega> ?R" by (rule sigma_algebra_sigma_sets) auto
   961   fix A assume "A \<in> {Pi' I X |X. X \<in> (\<Pi> j\<in>I. sets (M j))}"
   962   then obtain X where X: "A = Pi' I X" "X \<in> (\<Pi> j\<in>I. sets (M j))" by auto
   963   show "A \<in> sigma_sets ?\<Omega> ?R"
   964   proof -
   965     from \<open>I \<noteq> {}\<close> X have "A = (\<Inter>j\<in>I. {f\<in>space (PiF {I} M). f j \<in> X j})"
   966       using sets.sets_into_space
   967       by (auto simp: space_PiF product_def) blast
   968     also have "\<dots> \<in> sigma_sets ?\<Omega> ?R"
   969       using X \<open>I \<noteq> {}\<close> assms by (intro R.finite_INT) (auto simp: space_PiF)
   970     finally show "A \<in> sigma_sets ?\<Omega> ?R" .
   971   qed
   972 next
   973   fix A assume "A \<in> ?R"
   974   then obtain i B where A: "A = {f\<in>\<Pi>' i\<in>I. space (M i). f i \<in> B}" "i \<in> I" "B \<in> sets (M i)"
   975     by auto
   976   then have "A = (\<Pi>' j \<in> I. if j = i then B else space (M j))"
   977     using sets.sets_into_space[OF A(3)]
   978     apply (auto simp: Pi'_iff split: split_if_asm)
   979     apply blast
   980     done
   981   also have "\<dots> \<in> sigma_sets ?\<Omega> {Pi' I X |X. X \<in> (\<Pi> j\<in>I. sets (M j))}"
   982     using A
   983     by (intro sigma_sets.Basic )
   984        (auto intro: exI[where x="\<lambda>j. if j = i then B else space (M j)"])
   985   finally show "A \<in> sigma_sets ?\<Omega> {Pi' I X |X. X \<in> (\<Pi> j\<in>I. sets (M j))}" .
   986 qed
   987 
   988 text \<open>adapted from @{thm PiE_cong}\<close>
   989 
   990 lemma Pi'_cong:
   991   assumes "finite I"
   992   assumes "\<And>i. i \<in> I \<Longrightarrow> f i = g i"
   993   shows "Pi' I f = Pi' I g"
   994 using assms by (auto simp: Pi'_def)
   995 
   996 text \<open>adapted from @{thm Pi_UN}\<close>
   997 
   998 lemma Pi'_UN:
   999   fixes A :: "nat \<Rightarrow> 'i \<Rightarrow> 'a set"
  1000   assumes "finite I"
  1001   assumes mono: "\<And>i n m. i \<in> I \<Longrightarrow> n \<le> m \<Longrightarrow> A n i \<subseteq> A m i"
  1002   shows "(\<Union>n. Pi' I (A n)) = Pi' I (\<lambda>i. \<Union>n. A n i)"
  1003 proof (intro set_eqI iffI)
  1004   fix f assume "f \<in> Pi' I (\<lambda>i. \<Union>n. A n i)"
  1005   then have "\<forall>i\<in>I. \<exists>n. f i \<in> A n i" "domain f = I" by (auto simp: \<open>finite I\<close> Pi'_def)
  1006   from bchoice[OF this(1)] obtain n where n: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> (A (n i) i)" by auto
  1007   obtain k where k: "\<And>i. i \<in> I \<Longrightarrow> n i \<le> k"
  1008     using \<open>finite I\<close> finite_nat_set_iff_bounded_le[of "n`I"] by auto
  1009   have "f \<in> Pi' I (\<lambda>i. A k i)"
  1010   proof
  1011     fix i assume "i \<in> I"
  1012     from mono[OF this, of "n i" k] k[OF this] n[OF this] \<open>domain f = I\<close> \<open>i \<in> I\<close>
  1013     show "f i \<in> A k i " by (auto simp: \<open>finite I\<close>)
  1014   qed (simp add: \<open>domain f = I\<close> \<open>finite I\<close>)
  1015   then show "f \<in> (\<Union>n. Pi' I (A n))" by auto
  1016 qed (auto simp: Pi'_def \<open>finite I\<close>)
  1017 
  1018 text \<open>adapted from @{thm sets_PiM_sigma}\<close>
  1019 
  1020 lemma sigma_fprod_algebra_sigma_eq:
  1021   fixes E :: "'i \<Rightarrow> 'a set set" and S :: "'i \<Rightarrow> nat \<Rightarrow> 'a set"
  1022   assumes [simp]: "finite I" "I \<noteq> {}"
  1023     and S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
  1024     and S_in_E: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> E i"
  1025   assumes E_closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M i))"
  1026     and E_generates: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sigma_sets (space (M i)) (E i)"
  1027   defines "P == { Pi' I F | F. \<forall>i\<in>I. F i \<in> E i }"
  1028   shows "sets (PiF {I} M) = sigma_sets (space (PiF {I} M)) P"
  1029 proof
  1030   let ?P = "sigma (space (Pi\<^sub>F {I} M)) P"
  1031   from \<open>finite I\<close>[THEN ex_bij_betw_finite_nat] guess T ..
  1032   then have T: "\<And>i. i \<in> I \<Longrightarrow> T i < card I" "\<And>i. i\<in>I \<Longrightarrow> the_inv_into I T (T i) = i"
  1033     by (auto simp add: bij_betw_def set_eq_iff image_iff the_inv_into_f_f simp del: \<open>finite I\<close>)
  1034   have P_closed: "P \<subseteq> Pow (space (Pi\<^sub>F {I} M))"
  1035     using E_closed by (auto simp: space_PiF P_def Pi'_iff subset_eq)
  1036   then have space_P: "space ?P = (\<Pi>' i\<in>I. space (M i))"
  1037     by (simp add: space_PiF)
  1038   have "sets (PiF {I} M) =
  1039       sigma_sets (space ?P) {{f \<in> \<Pi>' i\<in>I. space (M i). f i \<in> A} |i A. i \<in> I \<and> A \<in> sets (M i)}"
  1040     using sets_PiF_single[of I M] by (simp add: space_P)
  1041   also have "\<dots> \<subseteq> sets (sigma (space (PiF {I} M)) P)"
  1042   proof (safe intro!: sets.sigma_sets_subset)
  1043     fix i A assume "i \<in> I" and A: "A \<in> sets (M i)"
  1044     have "(\<lambda>x. (x)\<^sub>F i) \<in> measurable ?P (sigma (space (M i)) (E i))"
  1045     proof (subst measurable_iff_measure_of)
  1046       show "E i \<subseteq> Pow (space (M i))" using \<open>i \<in> I\<close> by fact
  1047       from space_P \<open>i \<in> I\<close> show "(\<lambda>x. (x)\<^sub>F i) \<in> space ?P \<rightarrow> space (M i)"
  1048         by auto
  1049       show "\<forall>A\<in>E i. (\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P \<in> sets ?P"
  1050       proof
  1051         fix A assume A: "A \<in> E i"
  1052         then have "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P = (\<Pi>' j\<in>I. if i = j then A else space (M j))"
  1053           using E_closed \<open>i \<in> I\<close> by (auto simp: space_P Pi_iff subset_eq split: split_if_asm)
  1054         also have "\<dots> = (\<Pi>' j\<in>I. \<Union>n. if i = j then A else S j n)"
  1055           by (intro Pi'_cong) (simp_all add: S_union)
  1056         also have "\<dots> = (\<Union>xs\<in>{xs. length xs = card I}. \<Pi>' j\<in>I. if i = j then A else S j (xs ! T j))"
  1057           using T
  1058           apply (auto simp del: Union_iff)
  1059           apply (simp_all add: Pi'_iff bchoice_iff del: Union_iff)
  1060           apply (erule conjE exE)+
  1061           apply (rule_tac x="map (\<lambda>n. f (the_inv_into I T n)) [0..<card I]" in exI)
  1062           apply (auto simp: bij_betw_def)
  1063           done
  1064         also have "\<dots> \<in> sets ?P"
  1065         proof (safe intro!: sets.countable_UN)
  1066           fix xs show "(\<Pi>' j\<in>I. if i = j then A else S j (xs ! T j)) \<in> sets ?P"
  1067             using A S_in_E
  1068             by (simp add: P_closed)
  1069                (auto simp: P_def subset_eq intro!: exI[of _ "\<lambda>j. if i = j then A else S j (xs ! T j)"])
  1070         qed
  1071         finally show "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P \<in> sets ?P"
  1072           using P_closed by simp
  1073       qed
  1074     qed
  1075     from measurable_sets[OF this, of A] A \<open>i \<in> I\<close> E_closed
  1076     have "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P \<in> sets ?P"
  1077       by (simp add: E_generates)
  1078     also have "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P = {f \<in> \<Pi>' i\<in>I. space (M i). f i \<in> A}"
  1079       using P_closed by (auto simp: space_PiF)
  1080     finally show "\<dots> \<in> sets ?P" .
  1081   qed
  1082   finally show "sets (PiF {I} M) \<subseteq> sigma_sets (space (PiF {I} M)) P"
  1083     by (simp add: P_closed)
  1084   show "sigma_sets (space (PiF {I} M)) P \<subseteq> sets (PiF {I} M)"
  1085     using \<open>finite I\<close> \<open>I \<noteq> {}\<close>
  1086     by (auto intro!: sets.sigma_sets_subset product_in_sets_PiFI simp: E_generates P_def)
  1087 qed
  1088 
  1089 lemma product_open_generates_sets_PiF_single:
  1090   assumes "I \<noteq> {}"
  1091   assumes [simp]: "finite I"
  1092   shows "sets (PiF {I} (\<lambda>_. borel::'b::second_countable_topology measure)) =
  1093     sigma_sets (space (PiF {I} (\<lambda>_. borel))) {Pi' I F |F. (\<forall>i\<in>I. F i \<in> Collect open)}"
  1094 proof -
  1095   from open_countable_basisE[OF open_UNIV] guess S::"'b set set" . note S = this
  1096   show ?thesis
  1097   proof (rule sigma_fprod_algebra_sigma_eq)
  1098     show "finite I" by simp
  1099     show "I \<noteq> {}" by fact
  1100     def S'\<equiv>"from_nat_into S"
  1101     show "(\<Union>j. S' j) = space borel"
  1102       using S
  1103       apply (auto simp add: from_nat_into countable_basis_proj S'_def basis_proj_def)
  1104       apply (metis (lifting, mono_tags) UNIV_I UnionE basis_proj_def countable_basis_proj countable_subset from_nat_into_surj)
  1105       done
  1106     show "range S' \<subseteq> Collect open"
  1107       using S
  1108       apply (auto simp add: from_nat_into countable_basis_proj S'_def)
  1109       apply (metis UNIV_not_empty Union_empty from_nat_into set_mp topological_basis_open[OF basis_proj] basis_proj_def)
  1110       done
  1111     show "Collect open \<subseteq> Pow (space borel)" by simp
  1112     show "sets borel = sigma_sets (space borel) (Collect open)"
  1113       by (simp add: borel_def)
  1114   qed
  1115 qed
  1116 
  1117 lemma finmap_UNIV[simp]: "(\<Union>J\<in>Collect finite. \<Pi>' j\<in>J. UNIV) = UNIV" by auto
  1118 
  1119 lemma borel_eq_PiF_borel:
  1120   shows "(borel :: ('i::countable \<Rightarrow>\<^sub>F 'a::polish_space) measure) =
  1121     PiF (Collect finite) (\<lambda>_. borel :: 'a measure)"
  1122   unfolding borel_def PiF_def
  1123 proof (rule measure_eqI, clarsimp, rule sigma_sets_eqI)
  1124   fix a::"('i \<Rightarrow>\<^sub>F 'a) set" assume "a \<in> Collect open" hence "open a" by simp
  1125   then obtain B' where B': "B'\<subseteq>basis_finmap" "a = \<Union>B'"
  1126     using finmap_topological_basis by (force simp add: topological_basis_def)
  1127   have "a \<in> sigma UNIV {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)}"
  1128     unfolding \<open>a = \<Union>B'\<close>
  1129   proof (rule sets.countable_Union)
  1130     from B' countable_basis_finmap show "countable B'" by (metis countable_subset)
  1131   next
  1132     show "B' \<subseteq> sets (sigma UNIV
  1133       {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)})" (is "_ \<subseteq> sets ?s")
  1134     proof
  1135       fix x assume "x \<in> B'" with B' have "x \<in> basis_finmap" by auto
  1136       then obtain J X where "x = Pi' J X" "finite J" "X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)"
  1137         by (auto simp: basis_finmap_def topological_basis_open[OF basis_proj])
  1138       thus "x \<in> sets ?s" by auto
  1139     qed
  1140   qed
  1141   thus "a \<in> sigma_sets UNIV {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)}"
  1142     by simp
  1143 next
  1144   fix b::"('i \<Rightarrow>\<^sub>F 'a) set"
  1145   assume "b \<in> {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)}"
  1146   hence b': "b \<in> sets (Pi\<^sub>F (Collect finite) (\<lambda>_. borel))" by (auto simp: sets_PiF borel_def)
  1147   let ?b = "\<lambda>J. b \<inter> {x. domain x = J}"
  1148   have "b = \<Union>((\<lambda>J. ?b J) ` Collect finite)" by auto
  1149   also have "\<dots> \<in> sets borel"
  1150   proof (rule sets.countable_Union, safe)
  1151     fix J::"'i set" assume "finite J"
  1152     { assume ef: "J = {}"
  1153       have "?b J \<in> sets borel"
  1154       proof cases
  1155         assume "?b J \<noteq> {}"
  1156         then obtain f where "f \<in> b" "domain f = {}" using ef by auto
  1157         hence "?b J = {f}" using \<open>J = {}\<close>
  1158           by (auto simp: finmap_eq_iff)
  1159         also have "{f} \<in> sets borel" by simp
  1160         finally show ?thesis .
  1161       qed simp
  1162     } moreover {
  1163       assume "J \<noteq> ({}::'i set)"
  1164       have "(?b J) = b \<inter> {m. domain m \<in> {J}}" by auto
  1165       also have "\<dots> \<in> sets (PiF {J} (\<lambda>_. borel))"
  1166         using b' by (rule restrict_sets_measurable) (auto simp: \<open>finite J\<close>)
  1167       also have "\<dots> = sigma_sets (space (PiF {J} (\<lambda>_. borel)))
  1168         {Pi' (J) F |F. (\<forall>j\<in>J. F j \<in> Collect open)}"
  1169         (is "_ = sigma_sets _ ?P")
  1170        by (rule product_open_generates_sets_PiF_single[OF \<open>J \<noteq> {}\<close> \<open>finite J\<close>])
  1171       also have "\<dots> \<subseteq> sigma_sets UNIV (Collect open)"
  1172         by (intro sigma_sets_mono'') (auto intro!: open_Pi'I simp: space_PiF)
  1173       finally have "(?b J) \<in> sets borel" by (simp add: borel_def)
  1174     } ultimately show "(?b J) \<in> sets borel" by blast
  1175   qed (simp add: countable_Collect_finite)
  1176   finally show "b \<in> sigma_sets UNIV (Collect open)" by (simp add: borel_def)
  1177 qed (simp add: emeasure_sigma borel_def PiF_def)
  1178 
  1179 subsection \<open>Isomorphism between Functions and Finite Maps\<close>
  1180 
  1181 lemma measurable_finmap_compose:
  1182   shows "(\<lambda>m. compose J m f) \<in> measurable (PiM (f ` J) (\<lambda>_. M)) (PiM J (\<lambda>_. M))"
  1183   unfolding compose_def by measurable
  1184 
  1185 lemma measurable_compose_inv:
  1186   assumes inj: "\<And>j. j \<in> J \<Longrightarrow> f' (f j) = j"
  1187   shows "(\<lambda>m. compose (f ` J) m f') \<in> measurable (PiM J (\<lambda>_. M)) (PiM (f ` J) (\<lambda>_. M))"
  1188   unfolding compose_def by (rule measurable_restrict) (auto simp: inj)
  1189 
  1190 locale function_to_finmap =
  1191   fixes J::"'a set" and f :: "'a \<Rightarrow> 'b::countable" and f'
  1192   assumes [simp]: "finite J"
  1193   assumes inv: "i \<in> J \<Longrightarrow> f' (f i) = i"
  1194 begin
  1195 
  1196 text \<open>to measure finmaps\<close>
  1197 
  1198 definition "fm = (finmap_of (f ` J)) o (\<lambda>g. compose (f ` J) g f')"
  1199 
  1200 lemma domain_fm[simp]: "domain (fm x) = f ` J"
  1201   unfolding fm_def by simp
  1202 
  1203 lemma fm_restrict[simp]: "fm (restrict y J) = fm y"
  1204   unfolding fm_def by (auto simp: compose_def inv intro: restrict_ext)
  1205 
  1206 lemma fm_product:
  1207   assumes "\<And>i. space (M i) = UNIV"
  1208   shows "fm -` Pi' (f ` J) S \<inter> space (Pi\<^sub>M J M) = (\<Pi>\<^sub>E j \<in> J. S (f j))"
  1209   using assms
  1210   by (auto simp: inv fm_def compose_def space_PiM Pi'_def)
  1211 
  1212 lemma fm_measurable:
  1213   assumes "f ` J \<in> N"
  1214   shows "fm \<in> measurable (Pi\<^sub>M J (\<lambda>_. M)) (Pi\<^sub>F N (\<lambda>_. M))"
  1215   unfolding fm_def
  1216 proof (rule measurable_comp, rule measurable_compose_inv)
  1217   show "finmap_of (f ` J) \<in> measurable (Pi\<^sub>M (f ` J) (\<lambda>_. M)) (PiF N (\<lambda>_. M)) "
  1218     using assms by (intro measurable_finmap_of measurable_component_singleton) auto
  1219 qed (simp_all add: inv)
  1220 
  1221 lemma proj_fm:
  1222   assumes "x \<in> J"
  1223   shows "fm m (f x) = m x"
  1224   using assms by (auto simp: fm_def compose_def o_def inv)
  1225 
  1226 lemma inj_on_compose_f': "inj_on (\<lambda>g. compose (f ` J) g f') (extensional J)"
  1227 proof (rule inj_on_inverseI)
  1228   fix x::"'a \<Rightarrow> 'c" assume "x \<in> extensional J"
  1229   thus "(\<lambda>x. compose J x f) (compose (f ` J) x f') = x"
  1230     by (auto simp: compose_def inv extensional_def)
  1231 qed
  1232 
  1233 lemma inj_on_fm:
  1234   assumes "\<And>i. space (M i) = UNIV"
  1235   shows "inj_on fm (space (Pi\<^sub>M J M))"
  1236   using assms
  1237   apply (auto simp: fm_def space_PiM PiE_def)
  1238   apply (rule comp_inj_on)
  1239   apply (rule inj_on_compose_f')
  1240   apply (rule finmap_of_inj_on_extensional_finite)
  1241   apply simp
  1242   apply (auto)
  1243   done
  1244 
  1245 text \<open>to measure functions\<close>
  1246 
  1247 definition "mf = (\<lambda>g. compose J g f) o proj"
  1248 
  1249 lemma mf_fm:
  1250   assumes "x \<in> space (Pi\<^sub>M J (\<lambda>_. M))"
  1251   shows "mf (fm x) = x"
  1252 proof -
  1253   have "mf (fm x) \<in> extensional J"
  1254     by (auto simp: mf_def extensional_def compose_def)
  1255   moreover
  1256   have "x \<in> extensional J" using assms sets.sets_into_space
  1257     by (force simp: space_PiM PiE_def)
  1258   moreover
  1259   { fix i assume "i \<in> J"
  1260     hence "mf (fm x) i = x i"
  1261       by (auto simp: inv mf_def compose_def fm_def)
  1262   }
  1263   ultimately
  1264   show ?thesis by (rule extensionalityI)
  1265 qed
  1266 
  1267 lemma mf_measurable:
  1268   assumes "space M = UNIV"
  1269   shows "mf \<in> measurable (PiF {f ` J} (\<lambda>_. M)) (PiM J (\<lambda>_. M))"
  1270   unfolding mf_def
  1271 proof (rule measurable_comp, rule measurable_proj_PiM)
  1272   show "(\<lambda>g. compose J g f) \<in> measurable (Pi\<^sub>M (f ` J) (\<lambda>x. M)) (Pi\<^sub>M J (\<lambda>_. M))"
  1273     by (rule measurable_finmap_compose)
  1274 qed (auto simp add: space_PiM extensional_def assms)
  1275 
  1276 lemma fm_image_measurable:
  1277   assumes "space M = UNIV"
  1278   assumes "X \<in> sets (Pi\<^sub>M J (\<lambda>_. M))"
  1279   shows "fm ` X \<in> sets (PiF {f ` J} (\<lambda>_. M))"
  1280 proof -
  1281   have "fm ` X = (mf) -` X \<inter> space (PiF {f ` J} (\<lambda>_. M))"
  1282   proof safe
  1283     fix x assume "x \<in> X"
  1284     with mf_fm[of x] sets.sets_into_space[OF assms(2)] show "fm x \<in> mf -` X" by auto
  1285     show "fm x \<in> space (PiF {f ` J} (\<lambda>_. M))" by (simp add: space_PiF assms)
  1286   next
  1287     fix y x
  1288     assume x: "mf y \<in> X"
  1289     assume y: "y \<in> space (PiF {f ` J} (\<lambda>_. M))"
  1290     thus "y \<in> fm ` X"
  1291       by (intro image_eqI[OF _ x], unfold finmap_eq_iff)
  1292          (auto simp: space_PiF fm_def mf_def compose_def inv Pi'_def)
  1293   qed
  1294   also have "\<dots> \<in> sets (PiF {f ` J} (\<lambda>_. M))"
  1295     using assms
  1296     by (intro measurable_sets[OF mf_measurable]) auto
  1297   finally show ?thesis .
  1298 qed
  1299 
  1300 lemma fm_image_measurable_finite:
  1301   assumes "space M = UNIV"
  1302   assumes "X \<in> sets (Pi\<^sub>M J (\<lambda>_. M::'c measure))"
  1303   shows "fm ` X \<in> sets (PiF (Collect finite) (\<lambda>_. M::'c measure))"
  1304   using fm_image_measurable[OF assms]
  1305   by (rule subspace_set_in_sets) (auto simp: finite_subset)
  1306 
  1307 text \<open>measure on finmaps\<close>
  1308 
  1309 definition "mapmeasure M N = distr M (PiF (Collect finite) N) (fm)"
  1310 
  1311 lemma sets_mapmeasure[simp]: "sets (mapmeasure M N) = sets (PiF (Collect finite) N)"
  1312   unfolding mapmeasure_def by simp
  1313 
  1314 lemma space_mapmeasure[simp]: "space (mapmeasure M N) = space (PiF (Collect finite) N)"
  1315   unfolding mapmeasure_def by simp
  1316 
  1317 lemma mapmeasure_PiF:
  1318   assumes s1: "space M = space (Pi\<^sub>M J (\<lambda>_. N))"
  1319   assumes s2: "sets M = sets (Pi\<^sub>M J (\<lambda>_. N))"
  1320   assumes "space N = UNIV"
  1321   assumes "X \<in> sets (PiF (Collect finite) (\<lambda>_. N))"
  1322   shows "emeasure (mapmeasure M (\<lambda>_. N)) X = emeasure M ((fm -` X \<inter> extensional J))"
  1323   using assms
  1324   by (auto simp: measurable_cong_sets[OF s2 refl] mapmeasure_def emeasure_distr
  1325     fm_measurable space_PiM PiE_def)
  1326 
  1327 lemma mapmeasure_PiM:
  1328   fixes N::"'c measure"
  1329   assumes s1: "space M = space (Pi\<^sub>M J (\<lambda>_. N))"
  1330   assumes s2: "sets M = (Pi\<^sub>M J (\<lambda>_. N))"
  1331   assumes N: "space N = UNIV"
  1332   assumes X: "X \<in> sets M"
  1333   shows "emeasure M X = emeasure (mapmeasure M (\<lambda>_. N)) (fm ` X)"
  1334   unfolding mapmeasure_def
  1335 proof (subst emeasure_distr, subst measurable_cong_sets[OF s2 refl], rule fm_measurable)
  1336   have "X \<subseteq> space (Pi\<^sub>M J (\<lambda>_. N))" using assms by (simp add: sets.sets_into_space)
  1337   from assms inj_on_fm[of "\<lambda>_. N"] set_mp[OF this] have "fm -` fm ` X \<inter> space (Pi\<^sub>M J (\<lambda>_. N)) = X"
  1338     by (auto simp: vimage_image_eq inj_on_def)
  1339   thus "emeasure M X = emeasure M (fm -` fm ` X \<inter> space M)" using s1
  1340     by simp
  1341   show "fm ` X \<in> sets (PiF (Collect finite) (\<lambda>_. N))"
  1342     by (rule fm_image_measurable_finite[OF N X[simplified s2]])
  1343 qed simp
  1344 
  1345 end
  1346 
  1347 end