src/HOL/Probability/Fin_Map.thy
author hoelzl
Fri Jan 08 17:40:59 2016 +0100 (2016-01-08)
changeset 62101 26c0a70f78a3
parent 61988 34b51f436e92
child 62102 877463945ce9
permissions -rw-r--r--
add uniform spaces
     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 instantiation finmap :: (type, metric_space) metric_space
   280 begin
   281 
   282 lemma finite_proj_image': "x \<notin> domain P \<Longrightarrow> finite ((P)\<^sub>F ` S)"
   283   by (rule finite_subset[of _ "proj P ` (domain P \<inter> S \<union> {x})"]) auto
   284 
   285 lemma finite_proj_image: "finite ((P)\<^sub>F ` S)"
   286  by (cases "\<exists>x. x \<notin> domain P") (auto intro: finite_proj_image' finite_subset[where B="domain P"])
   287 
   288 lemma finite_proj_diag: "finite ((\<lambda>i. d ((P)\<^sub>F i) ((Q)\<^sub>F i)) ` S)"
   289 proof -
   290   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
   291   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
   292   moreover have "finite \<dots>" using finite_proj_image[of P S] finite_proj_image[of Q S]
   293     by (intro finite_cartesian_product) simp_all
   294   ultimately show ?thesis by (simp add: finite_subset)
   295 qed
   296 
   297 lemma dist_le_1_imp_domain_eq:
   298   shows "dist P Q < 1 \<Longrightarrow> domain P = domain Q"
   299   by (simp add: dist_finmap_def finite_proj_diag split: split_if_asm)
   300 
   301 lemma dist_proj:
   302   shows "dist ((x)\<^sub>F i) ((y)\<^sub>F i) \<le> dist x y"
   303 proof -
   304   have "dist (x i) (y i) \<le> Max (range (\<lambda>i. dist (x i) (y i)))"
   305     by (simp add: Max_ge_iff finite_proj_diag)
   306   also have "\<dots> \<le> dist x y" by (simp add: dist_finmap_def)
   307   finally show ?thesis .
   308 qed
   309 
   310 lemma dist_finmap_lessI:
   311   assumes "domain P = domain Q"
   312   assumes "0 < e"
   313   assumes "\<And>i. i \<in> domain P \<Longrightarrow> dist (P i) (Q i) < e"
   314   shows "dist P Q < e"
   315 proof -
   316   have "dist P Q = Max (range (\<lambda>i. dist (P i) (Q i)))"
   317     using assms by (simp add: dist_finmap_def finite_proj_diag)
   318   also have "\<dots> < e"
   319   proof (subst Max_less_iff, safe)
   320     fix i
   321     show "dist ((P)\<^sub>F i) ((Q)\<^sub>F i) < e" using assms
   322       by (cases "i \<in> domain P") simp_all
   323   qed (simp add: finite_proj_diag)
   324   finally show ?thesis .
   325 qed
   326 
   327 instance
   328 proof
   329   fix S::"('a \<Rightarrow>\<^sub>F 'b) set"
   330   have *: "open S = (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)" (is "_ = ?od")
   331   proof
   332     assume "open S"
   333     thus ?od
   334       unfolding open_finmap_def
   335     proof (induct rule: generate_topology.induct)
   336       case UNIV thus ?case by (auto intro: zero_less_one)
   337     next
   338       case (Int a b)
   339       show ?case
   340       proof safe
   341         fix x assume x: "x \<in> a" "x \<in> b"
   342         with Int x obtain e1 e2 where
   343           "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
   344         thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> a \<inter> b"
   345           by (auto intro!: exI[where x="min e1 e2"])
   346       qed
   347     next
   348       case (UN K)
   349       show ?case
   350       proof safe
   351         fix x X assume "x \<in> X" and X: "X \<in> K"
   352         with UN obtain e where "e>0" "\<And>y. dist y x < e \<longrightarrow> y \<in> X" by force
   353         with X show "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> \<Union>K" by auto
   354       qed
   355     next
   356       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
   357       show ?case
   358       proof safe
   359         fix x assume "x \<in> s"
   360         hence [simp]: "finite a" and a_dom: "a = domain x" using s by (auto simp: Pi'_iff)
   361         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)"
   362           using b \<open>x \<in> s\<close> by atomize_elim (intro bchoice, auto simp: open_dist s)
   363         hence in_b: "\<And>i y. i \<in> a \<Longrightarrow> dist y (proj x i) < es i \<Longrightarrow> y \<in> b i" by auto
   364         show "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> s"
   365         proof (cases, rule, safe)
   366           assume "a \<noteq> {}"
   367           show "0 < min 1 (Min (es ` a))" using es by (auto simp: \<open>a \<noteq> {}\<close>)
   368           fix y assume d: "dist y x < min 1 (Min (es ` a))"
   369           show "y \<in> s" unfolding s
   370           proof
   371             show "domain y = a" using d s \<open>a \<noteq> {}\<close> by (auto simp: dist_le_1_imp_domain_eq a_dom)
   372             fix i assume i: "i \<in> a"
   373             hence "dist ((y)\<^sub>F i) ((x)\<^sub>F i) < es i" using d
   374               by (auto simp: dist_finmap_def \<open>a \<noteq> {}\<close> intro!: le_less_trans[OF dist_proj])
   375             with i show "y i \<in> b i" by (rule in_b)
   376           qed
   377         next
   378           assume "\<not>a \<noteq> {}"
   379           thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> s"
   380             using s \<open>x \<in> s\<close> by (auto simp: Pi'_def dist_le_1_imp_domain_eq intro!: exI[where x=1])
   381         qed
   382       qed
   383     qed
   384   next
   385     assume "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   386     then obtain e where e_pos: "\<And>x. x \<in> S \<Longrightarrow> e x > 0" and
   387       e_in:  "\<And>x y . x \<in> S \<Longrightarrow> dist y x < e x \<Longrightarrow> y \<in> S"
   388       unfolding bchoice_iff
   389       by auto
   390     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))}"
   391     proof safe
   392       fix x assume "x \<in> S"
   393       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))}"
   394         using e_pos by (auto intro!: exI[where x="Pi' (domain x) (\<lambda>i. ball (x i) (e x))"])
   395     next
   396       fix x y
   397       assume "y \<in> S"
   398       moreover
   399       assume "x \<in> (\<Pi>' i\<in>domain y. ball (y i) (e y))"
   400       hence "dist x y < e y" using e_pos \<open>y \<in> S\<close>
   401         by (auto simp: dist_finmap_def Pi'_iff finite_proj_diag dist_commute)
   402       ultimately show "x \<in> S" by (rule e_in)
   403     qed
   404     also have "open \<dots>"
   405       unfolding open_finmap_def
   406       by (intro generate_topology.UN) (auto intro: generate_topology.Basis)
   407     finally show "open S" .
   408   qed
   409   show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
   410     unfolding * eventually_uniformity_metric
   411     by (simp del: split_paired_All add: dist_finmap_def dist_commute eq_commute)
   412 next
   413   fix P Q::"'a \<Rightarrow>\<^sub>F 'b"
   414   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))"
   415     by (auto intro: Max_in Max_eqI)
   416   show "dist P Q = 0 \<longleftrightarrow> P = Q"
   417     by (auto simp: finmap_eq_iff dist_finmap_def Max_ge_iff finite_proj_diag Max_eq_iff
   418         add_nonneg_eq_0_iff
   419       intro!: Max_eqI image_eqI[where x=undefined])
   420 next
   421   fix P Q R::"'a \<Rightarrow>\<^sub>F 'b"
   422   let ?dists = "\<lambda>P Q i. dist ((P)\<^sub>F i) ((Q)\<^sub>F i)"
   423   let ?dpq = "?dists P Q" and ?dpr = "?dists P R" and ?dqr = "?dists Q R"
   424   let ?dom = "\<lambda>P Q. (if domain P = domain Q then 0 else 1::real)"
   425   have "dist P Q = Max (range ?dpq) + ?dom P Q"
   426     by (simp add: dist_finmap_def)
   427   also obtain t where "t \<in> range ?dpq" "t = Max (range ?dpq)" by (simp add: finite_proj_diag)
   428   then obtain i where "Max (range ?dpq) = ?dpq i" by auto
   429   also have "?dpq i \<le> ?dpr i + ?dqr i" by (rule dist_triangle2)
   430   also have "?dpr i \<le> Max (range ?dpr)" by (simp add: finite_proj_diag)
   431   also have "?dqr i \<le> Max (range ?dqr)" by (simp add: finite_proj_diag)
   432   also have "?dom P Q \<le> ?dom P R + ?dom Q R" by simp
   433   finally show "dist P Q \<le> dist P R + dist Q R" by (simp add: dist_finmap_def ac_simps)
   434 qed
   435 
   436 end
   437 
   438 subsection \<open>Complete Space of Finite Maps\<close>
   439 
   440 lemma tendsto_finmap:
   441   fixes f::"nat \<Rightarrow> ('i \<Rightarrow>\<^sub>F ('a::metric_space))"
   442   assumes ind_f:  "\<And>n. domain (f n) = domain g"
   443   assumes proj_g:  "\<And>i. i \<in> domain g \<Longrightarrow> (\<lambda>n. (f n) i) \<longlonglongrightarrow> g i"
   444   shows "f \<longlonglongrightarrow> g"
   445   unfolding tendsto_iff
   446 proof safe
   447   fix e::real assume "0 < e"
   448   let ?dists = "\<lambda>x i. dist ((f x)\<^sub>F i) ((g)\<^sub>F i)"
   449   have "eventually (\<lambda>x. \<forall>i\<in>domain g. ?dists x i < e) sequentially"
   450     using finite_domain[of g] proj_g
   451   proof induct
   452     case (insert i G)
   453     with \<open>0 < e\<close> have "eventually (\<lambda>x. ?dists x i < e) sequentially" by (auto simp add: tendsto_iff)
   454     moreover
   455     from insert have "eventually (\<lambda>x. \<forall>i\<in>G. dist ((f x)\<^sub>F i) ((g)\<^sub>F i) < e) sequentially" by simp
   456     ultimately show ?case by eventually_elim auto
   457   qed simp
   458   thus "eventually (\<lambda>x. dist (f x) g < e) sequentially"
   459     by eventually_elim (auto simp add: dist_finmap_def finite_proj_diag ind_f \<open>0 < e\<close>)
   460 qed
   461 
   462 instance finmap :: (type, complete_space) complete_space
   463 proof
   464   fix P::"nat \<Rightarrow> 'a \<Rightarrow>\<^sub>F 'b"
   465   assume "Cauchy P"
   466   then obtain Nd where Nd: "\<And>n. n \<ge> Nd \<Longrightarrow> dist (P n) (P Nd) < 1"
   467     by (force simp: cauchy)
   468   def d \<equiv> "domain (P Nd)"
   469   with Nd have dim: "\<And>n. n \<ge> Nd \<Longrightarrow> domain (P n) = d" using dist_le_1_imp_domain_eq by auto
   470   have [simp]: "finite d" unfolding d_def by simp
   471   def p \<equiv> "\<lambda>i n. (P n) i"
   472   def q \<equiv> "\<lambda>i. lim (p i)"
   473   def Q \<equiv> "finmap_of d q"
   474   have q: "\<And>i. i \<in> d \<Longrightarrow> q i = Q i" by (auto simp add: Q_def Abs_finmap_inverse)
   475   {
   476     fix i assume "i \<in> d"
   477     have "Cauchy (p i)" unfolding cauchy p_def
   478     proof safe
   479       fix e::real assume "0 < e"
   480       with \<open>Cauchy P\<close> obtain N where N: "\<And>n. n\<ge>N \<Longrightarrow> dist (P n) (P N) < min e 1"
   481         by (force simp: cauchy min_def)
   482       hence "\<And>n. n \<ge> N \<Longrightarrow> domain (P n) = domain (P N)" using dist_le_1_imp_domain_eq by auto
   483       with dim have dim: "\<And>n. n \<ge> N \<Longrightarrow> domain (P n) = d" by (metis nat_le_linear)
   484       show "\<exists>N. \<forall>n\<ge>N. dist ((P n) i) ((P N) i) < e"
   485       proof (safe intro!: exI[where x="N"])
   486         fix n assume "N \<le> n" have "N \<le> N" by simp
   487         have "dist ((P n) i) ((P N) i) \<le> dist (P n) (P N)"
   488           using dim[OF \<open>N \<le> n\<close>]  dim[OF \<open>N \<le> N\<close>] \<open>i \<in> d\<close>
   489           by (auto intro!: dist_proj)
   490         also have "\<dots> < e" using N[OF \<open>N \<le> n\<close>] by simp
   491         finally show "dist ((P n) i) ((P N) i) < e" .
   492       qed
   493     qed
   494     hence "convergent (p i)" by (metis Cauchy_convergent_iff)
   495     hence "p i \<longlonglongrightarrow> q i" unfolding q_def convergent_def by (metis limI)
   496   } note p = this
   497   have "P \<longlonglongrightarrow> Q"
   498   proof (rule metric_LIMSEQ_I)
   499     fix e::real assume "0 < e"
   500     have "\<exists>ni. \<forall>i\<in>d. \<forall>n\<ge>ni i. dist (p i n) (q i) < e"
   501     proof (safe intro!: bchoice)
   502       fix i assume "i \<in> d"
   503       from p[OF \<open>i \<in> d\<close>, THEN metric_LIMSEQ_D, OF \<open>0 < e\<close>]
   504       show "\<exists>no. \<forall>n\<ge>no. dist (p i n) (q i) < e" .
   505     qed then guess ni .. note ni = this
   506     def N \<equiv> "max Nd (Max (ni ` d))"
   507     show "\<exists>N. \<forall>n\<ge>N. dist (P n) Q < e"
   508     proof (safe intro!: exI[where x="N"])
   509       fix n assume "N \<le> n"
   510       hence dom: "domain (P n) = d" "domain Q = d" "domain (P n) = domain Q"
   511         using dim by (simp_all add: N_def Q_def dim_def Abs_finmap_inverse)
   512       show "dist (P n) Q < e"
   513       proof (rule dist_finmap_lessI[OF dom(3) \<open>0 < e\<close>])
   514         fix i
   515         assume "i \<in> domain (P n)"
   516         hence "ni i \<le> Max (ni ` d)" using dom by simp
   517         also have "\<dots> \<le> N" by (simp add: N_def)
   518         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
   519           by (auto simp: p_def q N_def less_imp_le)
   520       qed
   521     qed
   522   qed
   523   thus "convergent P" by (auto simp: convergent_def)
   524 qed
   525 
   526 subsection \<open>Second Countable Space of Finite Maps\<close>
   527 
   528 instantiation finmap :: (countable, second_countable_topology) second_countable_topology
   529 begin
   530 
   531 definition basis_proj::"'b set set"
   532   where "basis_proj = (SOME B. countable B \<and> topological_basis B)"
   533 
   534 lemma countable_basis_proj: "countable basis_proj" and basis_proj: "topological_basis basis_proj"
   535   unfolding basis_proj_def by (intro is_basis countable_basis)+
   536 
   537 definition basis_finmap::"('a \<Rightarrow>\<^sub>F 'b) set set"
   538   where "basis_finmap = {Pi' I S|I S. finite I \<and> (\<forall>i \<in> I. S i \<in> basis_proj)}"
   539 
   540 lemma in_basis_finmapI:
   541   assumes "finite I" assumes "\<And>i. i \<in> I \<Longrightarrow> S i \<in> basis_proj"
   542   shows "Pi' I S \<in> basis_finmap"
   543   using assms unfolding basis_finmap_def by auto
   544 
   545 lemma basis_finmap_eq:
   546   assumes "basis_proj \<noteq> {}"
   547   shows "basis_finmap = (\<lambda>f. Pi' (domain f) (\<lambda>i. from_nat_into basis_proj ((f)\<^sub>F i))) `
   548     (UNIV::('a \<Rightarrow>\<^sub>F nat) set)" (is "_ = ?f ` _")
   549   unfolding basis_finmap_def
   550 proof safe
   551   fix I::"'a set" and S::"'a \<Rightarrow> 'b set"
   552   assume "finite I" "\<forall>i\<in>I. S i \<in> basis_proj"
   553   hence "Pi' I S = ?f (finmap_of I (\<lambda>x. to_nat_on basis_proj (S x)))"
   554     by (force simp: Pi'_def countable_basis_proj)
   555   thus "Pi' I S \<in> range ?f" by simp
   556 next
   557   fix x and f::"'a \<Rightarrow>\<^sub>F nat"
   558   show "\<exists>I S. (\<Pi>' i\<in>domain f. from_nat_into basis_proj ((f)\<^sub>F i)) = Pi' I S \<and>
   559     finite I \<and> (\<forall>i\<in>I. S i \<in> basis_proj)"
   560     using assms by (auto intro: from_nat_into)
   561 qed
   562 
   563 lemma basis_finmap_eq_empty: "basis_proj = {} \<Longrightarrow> basis_finmap = {Pi' {} undefined}"
   564   by (auto simp: Pi'_iff basis_finmap_def)
   565 
   566 lemma countable_basis_finmap: "countable basis_finmap"
   567   by (cases "basis_proj = {}") (auto simp: basis_finmap_eq basis_finmap_eq_empty)
   568 
   569 lemma finmap_topological_basis:
   570   "topological_basis basis_finmap"
   571 proof (subst topological_basis_iff, safe)
   572   fix B' assume "B' \<in> basis_finmap"
   573   thus "open B'"
   574     by (auto intro!: open_Pi'I topological_basis_open[OF basis_proj]
   575       simp: topological_basis_def basis_finmap_def Let_def)
   576 next
   577   fix O'::"('a \<Rightarrow>\<^sub>F 'b) set" and x
   578   assume O': "open O'" "x \<in> O'"
   579   then obtain a where a:
   580     "x \<in> Pi' (domain x) a" "Pi' (domain x) a \<subseteq> O'" "\<And>i. i\<in>domain x \<Longrightarrow> open (a i)"
   581     unfolding open_finmap_def
   582   proof (atomize_elim, induct rule: generate_topology.induct)
   583     case (Int a b)
   584     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))"
   585     from Int obtain f g where "?p a f" "?p b g" by auto
   586     thus ?case by (force intro!: exI[where x="\<lambda>i. f i \<inter> g i"] simp: Pi'_def)
   587   next
   588     case (UN k)
   589     then obtain kk a where "x \<in> kk" "kk \<in> k" "x \<in> Pi' (domain x) a" "Pi' (domain x) a \<subseteq> kk"
   590       "\<And>i. i\<in>domain x \<Longrightarrow> open (a i)"
   591       by force
   592     thus ?case by blast
   593   qed (auto simp: Pi'_def)
   594   have "\<exists>B.
   595     (\<forall>i\<in>domain x. x i \<in> B i \<and> B i \<subseteq> a i \<and> B i \<in> basis_proj)"
   596   proof (rule bchoice, safe)
   597     fix i assume "i \<in> domain x"
   598     hence "open (a i)" "x i \<in> a i" using a by auto
   599     from topological_basisE[OF basis_proj this] guess b' .
   600     thus "\<exists>y. x i \<in> y \<and> y \<subseteq> a i \<and> y \<in> basis_proj" by auto
   601   qed
   602   then guess B .. note B = this
   603   def B' \<equiv> "Pi' (domain x) (\<lambda>i. (B i)::'b set)"
   604   have "B' \<subseteq> Pi' (domain x) a" using B by (auto intro!: Pi'_mono simp: B'_def)
   605   also note \<open>\<dots> \<subseteq> O'\<close>
   606   finally show "\<exists>B'\<in>basis_finmap. x \<in> B' \<and> B' \<subseteq> O'" using B
   607     by (auto intro!: bexI[where x=B'] Pi'_mono in_basis_finmapI simp: B'_def)
   608 qed
   609 
   610 lemma range_enum_basis_finmap_imp_open:
   611   assumes "x \<in> basis_finmap"
   612   shows "open x"
   613   using finmap_topological_basis assms by (auto simp: topological_basis_def)
   614 
   615 instance proof qed (blast intro: finmap_topological_basis countable_basis_finmap topological_basis_imp_subbasis)
   616 
   617 end
   618 
   619 subsection \<open>Polish Space of Finite Maps\<close>
   620 
   621 instance finmap :: (countable, polish_space) polish_space proof qed
   622 
   623 
   624 subsection \<open>Product Measurable Space of Finite Maps\<close>
   625 
   626 definition "PiF I M \<equiv>
   627   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))}"
   628 
   629 abbreviation
   630   "Pi\<^sub>F I M \<equiv> PiF I M"
   631 
   632 syntax
   633   "_PiF" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^sub>F _\<in>_./ _)"  10)
   634 translations
   635   "\<Pi>\<^sub>F x\<in>I. M" == "CONST PiF I (%x. M)"
   636 
   637 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>
   638     Pow (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j)))"
   639   by (auto simp: Pi'_def) (blast dest: sets.sets_into_space)
   640 
   641 lemma space_PiF: "space (PiF I M) = (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j)))"
   642   unfolding PiF_def using PiF_gen_subset by (rule space_measure_of)
   643 
   644 lemma sets_PiF:
   645   "sets (PiF I M) = sigma_sets (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j)))
   646     {(\<Pi>' j\<in>J. X j) |X J. J \<in> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}"
   647   unfolding PiF_def using PiF_gen_subset by (rule sets_measure_of)
   648 
   649 lemma sets_PiF_singleton:
   650   "sets (PiF {I} M) = sigma_sets (\<Pi>' j\<in>I. space (M j))
   651     {(\<Pi>' j\<in>I. X j) |X. X \<in> (\<Pi> j\<in>I. sets (M j))}"
   652   unfolding sets_PiF by simp
   653 
   654 lemma in_sets_PiFI:
   655   assumes "X = (Pi' J S)" "J \<in> I" "\<And>i. i\<in>J \<Longrightarrow> S i \<in> sets (M i)"
   656   shows "X \<in> sets (PiF I M)"
   657   unfolding sets_PiF
   658   using assms by blast
   659 
   660 lemma product_in_sets_PiFI:
   661   assumes "J \<in> I" "\<And>i. i\<in>J \<Longrightarrow> S i \<in> sets (M i)"
   662   shows "(Pi' J S) \<in> sets (PiF I M)"
   663   unfolding sets_PiF
   664   using assms by blast
   665 
   666 lemma singleton_space_subset_in_sets:
   667   fixes J
   668   assumes "J \<in> I"
   669   assumes "finite J"
   670   shows "space (PiF {J} M) \<in> sets (PiF I M)"
   671   using assms
   672   by (intro in_sets_PiFI[where J=J and S="\<lambda>i. space (M i)"])
   673       (auto simp: product_def space_PiF)
   674 
   675 lemma singleton_subspace_set_in_sets:
   676   assumes A: "A \<in> sets (PiF {J} M)"
   677   assumes "finite J"
   678   assumes "J \<in> I"
   679   shows "A \<in> sets (PiF I M)"
   680   using A[unfolded sets_PiF]
   681   apply (induct A)
   682   unfolding sets_PiF[symmetric] unfolding space_PiF[symmetric]
   683   using assms
   684   by (auto intro: in_sets_PiFI intro!: singleton_space_subset_in_sets)
   685 
   686 lemma finite_measurable_singletonI:
   687   assumes "finite I"
   688   assumes "\<And>J. J \<in> I \<Longrightarrow> finite J"
   689   assumes MN: "\<And>J. J \<in> I \<Longrightarrow> A \<in> measurable (PiF {J} M) N"
   690   shows "A \<in> measurable (PiF I M) N"
   691   unfolding measurable_def
   692 proof safe
   693   fix y assume "y \<in> sets N"
   694   have "A -` y \<inter> space (PiF I M) = (\<Union>J\<in>I. A -` y \<inter> space (PiF {J} M))"
   695     by (auto simp: space_PiF)
   696   also have "\<dots> \<in> sets (PiF I M)"
   697   proof
   698     show "finite I" by fact
   699     fix J assume "J \<in> I"
   700     with assms have "finite J" by simp
   701     show "A -` y \<inter> space (PiF {J} M) \<in> sets (PiF I M)"
   702       by (rule singleton_subspace_set_in_sets[OF measurable_sets[OF assms(3)]]) fact+
   703   qed
   704   finally show "A -` y \<inter> space (PiF I M) \<in> sets (PiF I M)" .
   705 next
   706   fix x assume "x \<in> space (PiF I M)" thus "A x \<in> space N"
   707     using MN[of "domain x"]
   708     by (auto simp: space_PiF measurable_space Pi'_def)
   709 qed
   710 
   711 lemma countable_finite_comprehension:
   712   fixes f :: "'a::countable set \<Rightarrow> _"
   713   assumes "\<And>s. P s \<Longrightarrow> finite s"
   714   assumes "\<And>s. P s \<Longrightarrow> f s \<in> sets M"
   715   shows "\<Union>{f s|s. P s} \<in> sets M"
   716 proof -
   717   have "\<Union>{f s|s. P s} = (\<Union>n::nat. let s = set (from_nat n) in if P s then f s else {})"
   718   proof safe
   719     fix x X s assume *: "x \<in> f s" "P s"
   720     with assms obtain l where "s = set l" using finite_list by blast
   721     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>
   722       by (auto intro!: exI[where x="to_nat l"])
   723   next
   724     fix x n assume "x \<in> (let s = set (from_nat n) in if P s then f s else {})"
   725     thus "x \<in> \<Union>{f s|s. P s}" using assms by (auto simp: Let_def split: split_if_asm)
   726   qed
   727   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
   728   also have "\<dots> \<in> sets M" using assms by (auto simp: Let_def)
   729   finally show ?thesis .
   730 qed
   731 
   732 lemma space_subset_in_sets:
   733   fixes J::"'a::countable set set"
   734   assumes "J \<subseteq> I"
   735   assumes "\<And>j. j \<in> J \<Longrightarrow> finite j"
   736   shows "space (PiF J M) \<in> sets (PiF I M)"
   737 proof -
   738   have "space (PiF J M) = \<Union>{space (PiF {j} M)|j. j \<in> J}"
   739     unfolding space_PiF by blast
   740   also have "\<dots> \<in> sets (PiF I M)" using assms
   741     by (intro countable_finite_comprehension) (auto simp: singleton_space_subset_in_sets)
   742   finally show ?thesis .
   743 qed
   744 
   745 lemma subspace_set_in_sets:
   746   fixes J::"'a::countable set set"
   747   assumes A: "A \<in> sets (PiF J M)"
   748   assumes "J \<subseteq> I"
   749   assumes "\<And>j. j \<in> J \<Longrightarrow> finite j"
   750   shows "A \<in> sets (PiF I M)"
   751   using A[unfolded sets_PiF]
   752   apply (induct A)
   753   unfolding sets_PiF[symmetric] unfolding space_PiF[symmetric]
   754   using assms
   755   by (auto intro: in_sets_PiFI intro!: space_subset_in_sets)
   756 
   757 lemma countable_measurable_PiFI:
   758   fixes I::"'a::countable set set"
   759   assumes MN: "\<And>J. J \<in> I \<Longrightarrow> finite J \<Longrightarrow> A \<in> measurable (PiF {J} M) N"
   760   shows "A \<in> measurable (PiF I M) N"
   761   unfolding measurable_def
   762 proof safe
   763   fix y assume "y \<in> sets N"
   764   have "A -` y = (\<Union>{A -` y \<inter> {x. domain x = J}|J. finite J})" by auto
   765   { fix x::"'a \<Rightarrow>\<^sub>F 'b"
   766     from finite_list[of "domain x"] obtain xs where "set xs = domain x" by auto
   767     hence "\<exists>n. domain x = set (from_nat n)"
   768       by (intro exI[where x="to_nat xs"]) auto }
   769   hence "A -` y \<inter> space (PiF I M) = (\<Union>n. A -` y \<inter> space (PiF ({set (from_nat n)}\<inter>I) M))"
   770     by (auto simp: space_PiF Pi'_def)
   771   also have "\<dots> \<in> sets (PiF I M)"
   772     apply (intro sets.Int sets.countable_nat_UN subsetI, safe)
   773     apply (case_tac "set (from_nat i) \<in> I")
   774     apply simp_all
   775     apply (rule singleton_subspace_set_in_sets[OF measurable_sets[OF MN]])
   776     using assms \<open>y \<in> sets N\<close>
   777     apply (auto simp: space_PiF)
   778     done
   779   finally show "A -` y \<inter> space (PiF I M) \<in> sets (PiF I M)" .
   780 next
   781   fix x assume "x \<in> space (PiF I M)" thus "A x \<in> space N"
   782     using MN[of "domain x"] by (auto simp: space_PiF measurable_space Pi'_def)
   783 qed
   784 
   785 lemma measurable_PiF:
   786   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))"
   787   assumes S: "\<And>J S. J \<in> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> S i \<in> sets (M i)) \<Longrightarrow>
   788     f -` (Pi' J S) \<inter> space N \<in> sets N"
   789   shows "f \<in> measurable N (PiF I M)"
   790   unfolding PiF_def
   791   using PiF_gen_subset
   792   apply (rule measurable_measure_of)
   793   using f apply force
   794   apply (insert S, auto)
   795   done
   796 
   797 lemma restrict_sets_measurable:
   798   assumes A: "A \<in> sets (PiF I M)" and "J \<subseteq> I"
   799   shows "A \<inter> {m. domain m \<in> J} \<in> sets (PiF J M)"
   800   using A[unfolded sets_PiF]
   801 proof (induct A)
   802   case (Basic a)
   803   then obtain K S where S: "a = Pi' K S" "K \<in> I" "(\<forall>i\<in>K. S i \<in> sets (M i))"
   804     by auto
   805   show ?case
   806   proof cases
   807     assume "K \<in> J"
   808     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
   809       by (auto intro!: exI[where x=K] exI[where x=S] simp: Pi'_def)
   810     also have "\<dots> \<subseteq> sets (PiF J M)" unfolding sets_PiF by auto
   811     finally show ?thesis .
   812   next
   813     assume "K \<notin> J"
   814     hence "a \<inter> {m. domain m \<in> J} = {}" using S by (auto simp: Pi'_def)
   815     also have "\<dots> \<in> sets (PiF J M)" by simp
   816     finally show ?thesis .
   817   qed
   818 next
   819   case (Union a)
   820   have "UNION UNIV a \<inter> {m. domain m \<in> J} = (\<Union>i. (a i \<inter> {m. domain m \<in> J}))"
   821     by simp
   822   also have "\<dots> \<in> sets (PiF J M)" using Union by (intro sets.countable_nat_UN) auto
   823   finally show ?case .
   824 next
   825   case (Compl a)
   826   have "(space (PiF I M) - a) \<inter> {m. domain m \<in> J} = (space (PiF J M) - (a \<inter> {m. domain m \<in> J}))"
   827     using \<open>J \<subseteq> I\<close> by (auto simp: space_PiF Pi'_def)
   828   also have "\<dots> \<in> sets (PiF J M)" using Compl by auto
   829   finally show ?case by (simp add: space_PiF)
   830 qed simp
   831 
   832 lemma measurable_finmap_of:
   833   assumes f: "\<And>i. (\<exists>x \<in> space N. i \<in> J x) \<Longrightarrow> (\<lambda>x. f x i) \<in> measurable N (M i)"
   834   assumes J: "\<And>x. x \<in> space N \<Longrightarrow> J x \<in> I" "\<And>x. x \<in> space N \<Longrightarrow> finite (J x)"
   835   assumes JN: "\<And>S. {x. J x = S} \<inter> space N \<in> sets N"
   836   shows "(\<lambda>x. finmap_of (J x) (f x)) \<in> measurable N (PiF I M)"
   837 proof (rule measurable_PiF)
   838   fix x assume "x \<in> space N"
   839   with J[of x] measurable_space[OF f]
   840   show "domain (finmap_of (J x) (f x)) \<in> I \<and>
   841         (\<forall>i\<in>domain (finmap_of (J x) (f x)). (finmap_of (J x) (f x)) i \<in> space (M i))"
   842     by auto
   843 next
   844   fix K S assume "K \<in> I" and *: "\<And>i. i \<in> K \<Longrightarrow> S i \<in> sets (M i)"
   845   with J have eq: "(\<lambda>x. finmap_of (J x) (f x)) -` Pi' K S \<inter> space N =
   846     (if \<exists>x \<in> space N. K = J x \<and> finite K then if K = {} then {x \<in> space N. J x = K}
   847       else (\<Inter>i\<in>K. (\<lambda>x. f x i) -` S i \<inter> {x \<in> space N. J x = K}) else {})"
   848     by (auto simp: Pi'_def)
   849   have r: "{x \<in> space N. J x = K} = space N \<inter> ({x. J x = K} \<inter> space N)" by auto
   850   show "(\<lambda>x. finmap_of (J x) (f x)) -` Pi' K S \<inter> space N \<in> sets N"
   851     unfolding eq r
   852     apply (simp del: INT_simps add: )
   853     apply (intro conjI impI sets.finite_INT JN sets.Int[OF sets.top])
   854     apply simp apply assumption
   855     apply (subst Int_assoc[symmetric])
   856     apply (rule sets.Int)
   857     apply (intro measurable_sets[OF f] *) apply force apply assumption
   858     apply (intro JN)
   859     done
   860 qed
   861 
   862 lemma measurable_PiM_finmap_of:
   863   assumes "finite J"
   864   shows "finmap_of J \<in> measurable (Pi\<^sub>M J M) (PiF {J} M)"
   865   apply (rule measurable_finmap_of)
   866   apply (rule measurable_component_singleton)
   867   apply simp
   868   apply rule
   869   apply (rule \<open>finite J\<close>)
   870   apply simp
   871   done
   872 
   873 lemma proj_measurable_singleton:
   874   assumes "A \<in> sets (M i)"
   875   shows "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space (PiF {I} M) \<in> sets (PiF {I} M)"
   876 proof cases
   877   assume "i \<in> I"
   878   hence "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space (PiF {I} M) =
   879     Pi' I (\<lambda>x. if x = i then A else space (M x))"
   880     using sets.sets_into_space[OF ] \<open>A \<in> sets (M i)\<close> assms
   881     by (auto simp: space_PiF Pi'_def)
   882   thus ?thesis  using assms \<open>A \<in> sets (M i)\<close>
   883     by (intro in_sets_PiFI) auto
   884 next
   885   assume "i \<notin> I"
   886   hence "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space (PiF {I} M) =
   887     (if undefined \<in> A then space (PiF {I} M) else {})" by (auto simp: space_PiF Pi'_def)
   888   thus ?thesis by simp
   889 qed
   890 
   891 lemma measurable_proj_singleton:
   892   assumes "i \<in> I"
   893   shows "(\<lambda>x. (x)\<^sub>F i) \<in> measurable (PiF {I} M) (M i)"
   894   by (unfold measurable_def, intro CollectI conjI ballI proj_measurable_singleton assms)
   895      (insert \<open>i \<in> I\<close>, auto simp: space_PiF)
   896 
   897 lemma measurable_proj_countable:
   898   fixes I::"'a::countable set set"
   899   assumes "y \<in> space (M i)"
   900   shows "(\<lambda>x. if i \<in> domain x then (x)\<^sub>F i else y) \<in> measurable (PiF I M) (M i)"
   901 proof (rule countable_measurable_PiFI)
   902   fix J assume "J \<in> I" "finite J"
   903   show "(\<lambda>x. if i \<in> domain x then x i else y) \<in> measurable (PiF {J} M) (M i)"
   904     unfolding measurable_def
   905   proof safe
   906     fix z assume "z \<in> sets (M i)"
   907     have "(\<lambda>x. if i \<in> domain x then x i else y) -` z \<inter> space (PiF {J} M) =
   908       (\<lambda>x. if i \<in> J then (x)\<^sub>F i else y) -` z \<inter> space (PiF {J} M)"
   909       by (auto simp: space_PiF Pi'_def)
   910     also have "\<dots> \<in> sets (PiF {J} M)" using \<open>z \<in> sets (M i)\<close> \<open>finite J\<close>
   911       by (cases "i \<in> J") (auto intro!: measurable_sets[OF measurable_proj_singleton])
   912     finally show "(\<lambda>x. if i \<in> domain x then x i else y) -` z \<inter> space (PiF {J} M) \<in>
   913       sets (PiF {J} M)" .
   914   qed (insert \<open>y \<in> space (M i)\<close>, auto simp: space_PiF Pi'_def)
   915 qed
   916 
   917 lemma measurable_restrict_proj:
   918   assumes "J \<in> II" "finite J"
   919   shows "finmap_of J \<in> measurable (PiM J M) (PiF II M)"
   920   using assms
   921   by (intro measurable_finmap_of measurable_component_singleton) auto
   922 
   923 lemma measurable_proj_PiM:
   924   fixes J K ::"'a::countable set" and I::"'a set set"
   925   assumes "finite J" "J \<in> I"
   926   assumes "x \<in> space (PiM J M)"
   927   shows "proj \<in> measurable (PiF {J} M) (PiM J M)"
   928 proof (rule measurable_PiM_single)
   929   show "proj \<in> space (PiF {J} M) \<rightarrow> (\<Pi>\<^sub>E i \<in> J. space (M i))"
   930     using assms by (auto simp add: space_PiM space_PiF extensional_def sets_PiF Pi'_def)
   931 next
   932   fix A i assume A: "i \<in> J" "A \<in> sets (M i)"
   933   show "{\<omega> \<in> space (PiF {J} M). (\<omega>)\<^sub>F i \<in> A} \<in> sets (PiF {J} M)"
   934   proof
   935     have "{\<omega> \<in> space (PiF {J} M). (\<omega>)\<^sub>F i \<in> A} =
   936       (\<lambda>\<omega>. (\<omega>)\<^sub>F i) -` A \<inter> space (PiF {J} M)" by auto
   937     also have "\<dots> \<in> sets (PiF {J} M)"
   938       using assms A by (auto intro: measurable_sets[OF measurable_proj_singleton] simp: space_PiM)
   939     finally show ?thesis .
   940   qed simp
   941 qed
   942 
   943 lemma space_PiF_singleton_eq_product:
   944   assumes "finite I"
   945   shows "space (PiF {I} M) = (\<Pi>' i\<in>I. space (M i))"
   946   by (auto simp: product_def space_PiF assms)
   947 
   948 text \<open>adapted from @{thm sets_PiM_single}\<close>
   949 
   950 lemma sets_PiF_single:
   951   assumes "finite I" "I \<noteq> {}"
   952   shows "sets (PiF {I} M) =
   953     sigma_sets (\<Pi>' i\<in>I. space (M i))
   954       {{f\<in>\<Pi>' i\<in>I. space (M i). f i \<in> A} | i A. i \<in> I \<and> A \<in> sets (M i)}"
   955     (is "_ = sigma_sets ?\<Omega> ?R")
   956   unfolding sets_PiF_singleton
   957 proof (rule sigma_sets_eqI)
   958   interpret R: sigma_algebra ?\<Omega> "sigma_sets ?\<Omega> ?R" by (rule sigma_algebra_sigma_sets) auto
   959   fix A assume "A \<in> {Pi' I X |X. X \<in> (\<Pi> j\<in>I. sets (M j))}"
   960   then obtain X where X: "A = Pi' I X" "X \<in> (\<Pi> j\<in>I. sets (M j))" by auto
   961   show "A \<in> sigma_sets ?\<Omega> ?R"
   962   proof -
   963     from \<open>I \<noteq> {}\<close> X have "A = (\<Inter>j\<in>I. {f\<in>space (PiF {I} M). f j \<in> X j})"
   964       using sets.sets_into_space
   965       by (auto simp: space_PiF product_def) blast
   966     also have "\<dots> \<in> sigma_sets ?\<Omega> ?R"
   967       using X \<open>I \<noteq> {}\<close> assms by (intro R.finite_INT) (auto simp: space_PiF)
   968     finally show "A \<in> sigma_sets ?\<Omega> ?R" .
   969   qed
   970 next
   971   fix A assume "A \<in> ?R"
   972   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)"
   973     by auto
   974   then have "A = (\<Pi>' j \<in> I. if j = i then B else space (M j))"
   975     using sets.sets_into_space[OF A(3)]
   976     apply (auto simp: Pi'_iff split: split_if_asm)
   977     apply blast
   978     done
   979   also have "\<dots> \<in> sigma_sets ?\<Omega> {Pi' I X |X. X \<in> (\<Pi> j\<in>I. sets (M j))}"
   980     using A
   981     by (intro sigma_sets.Basic )
   982        (auto intro: exI[where x="\<lambda>j. if j = i then B else space (M j)"])
   983   finally show "A \<in> sigma_sets ?\<Omega> {Pi' I X |X. X \<in> (\<Pi> j\<in>I. sets (M j))}" .
   984 qed
   985 
   986 text \<open>adapted from @{thm PiE_cong}\<close>
   987 
   988 lemma Pi'_cong:
   989   assumes "finite I"
   990   assumes "\<And>i. i \<in> I \<Longrightarrow> f i = g i"
   991   shows "Pi' I f = Pi' I g"
   992 using assms by (auto simp: Pi'_def)
   993 
   994 text \<open>adapted from @{thm Pi_UN}\<close>
   995 
   996 lemma Pi'_UN:
   997   fixes A :: "nat \<Rightarrow> 'i \<Rightarrow> 'a set"
   998   assumes "finite I"
   999   assumes mono: "\<And>i n m. i \<in> I \<Longrightarrow> n \<le> m \<Longrightarrow> A n i \<subseteq> A m i"
  1000   shows "(\<Union>n. Pi' I (A n)) = Pi' I (\<lambda>i. \<Union>n. A n i)"
  1001 proof (intro set_eqI iffI)
  1002   fix f assume "f \<in> Pi' I (\<lambda>i. \<Union>n. A n i)"
  1003   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)
  1004   from bchoice[OF this(1)] obtain n where n: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> (A (n i) i)" by auto
  1005   obtain k where k: "\<And>i. i \<in> I \<Longrightarrow> n i \<le> k"
  1006     using \<open>finite I\<close> finite_nat_set_iff_bounded_le[of "n`I"] by auto
  1007   have "f \<in> Pi' I (\<lambda>i. A k i)"
  1008   proof
  1009     fix i assume "i \<in> I"
  1010     from mono[OF this, of "n i" k] k[OF this] n[OF this] \<open>domain f = I\<close> \<open>i \<in> I\<close>
  1011     show "f i \<in> A k i " by (auto simp: \<open>finite I\<close>)
  1012   qed (simp add: \<open>domain f = I\<close> \<open>finite I\<close>)
  1013   then show "f \<in> (\<Union>n. Pi' I (A n))" by auto
  1014 qed (auto simp: Pi'_def \<open>finite I\<close>)
  1015 
  1016 text \<open>adapted from @{thm sets_PiM_sigma}\<close>
  1017 
  1018 lemma sigma_fprod_algebra_sigma_eq:
  1019   fixes E :: "'i \<Rightarrow> 'a set set" and S :: "'i \<Rightarrow> nat \<Rightarrow> 'a set"
  1020   assumes [simp]: "finite I" "I \<noteq> {}"
  1021     and S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
  1022     and S_in_E: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> E i"
  1023   assumes E_closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M i))"
  1024     and E_generates: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sigma_sets (space (M i)) (E i)"
  1025   defines "P == { Pi' I F | F. \<forall>i\<in>I. F i \<in> E i }"
  1026   shows "sets (PiF {I} M) = sigma_sets (space (PiF {I} M)) P"
  1027 proof
  1028   let ?P = "sigma (space (Pi\<^sub>F {I} M)) P"
  1029   from \<open>finite I\<close>[THEN ex_bij_betw_finite_nat] guess T ..
  1030   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"
  1031     by (auto simp add: bij_betw_def set_eq_iff image_iff the_inv_into_f_f simp del: \<open>finite I\<close>)
  1032   have P_closed: "P \<subseteq> Pow (space (Pi\<^sub>F {I} M))"
  1033     using E_closed by (auto simp: space_PiF P_def Pi'_iff subset_eq)
  1034   then have space_P: "space ?P = (\<Pi>' i\<in>I. space (M i))"
  1035     by (simp add: space_PiF)
  1036   have "sets (PiF {I} M) =
  1037       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)}"
  1038     using sets_PiF_single[of I M] by (simp add: space_P)
  1039   also have "\<dots> \<subseteq> sets (sigma (space (PiF {I} M)) P)"
  1040   proof (safe intro!: sets.sigma_sets_subset)
  1041     fix i A assume "i \<in> I" and A: "A \<in> sets (M i)"
  1042     have "(\<lambda>x. (x)\<^sub>F i) \<in> measurable ?P (sigma (space (M i)) (E i))"
  1043     proof (subst measurable_iff_measure_of)
  1044       show "E i \<subseteq> Pow (space (M i))" using \<open>i \<in> I\<close> by fact
  1045       from space_P \<open>i \<in> I\<close> show "(\<lambda>x. (x)\<^sub>F i) \<in> space ?P \<rightarrow> space (M i)"
  1046         by auto
  1047       show "\<forall>A\<in>E i. (\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P \<in> sets ?P"
  1048       proof
  1049         fix A assume A: "A \<in> E i"
  1050         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))"
  1051           using E_closed \<open>i \<in> I\<close> by (auto simp: space_P Pi_iff subset_eq split: split_if_asm)
  1052         also have "\<dots> = (\<Pi>' j\<in>I. \<Union>n. if i = j then A else S j n)"
  1053           by (intro Pi'_cong) (simp_all add: S_union)
  1054         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))"
  1055           using T
  1056           apply auto
  1057           apply (simp_all add: Pi'_iff bchoice_iff)
  1058           apply (erule conjE exE)+
  1059           apply (rule_tac x="map (\<lambda>n. f (the_inv_into I T n)) [0..<card I]" in exI)
  1060           apply (auto simp: bij_betw_def)
  1061           done
  1062         also have "\<dots> \<in> sets ?P"
  1063         proof (safe intro!: sets.countable_UN)
  1064           fix xs show "(\<Pi>' j\<in>I. if i = j then A else S j (xs ! T j)) \<in> sets ?P"
  1065             using A S_in_E
  1066             by (simp add: P_closed)
  1067                (auto simp: P_def subset_eq intro!: exI[of _ "\<lambda>j. if i = j then A else S j (xs ! T j)"])
  1068         qed
  1069         finally show "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P \<in> sets ?P"
  1070           using P_closed by simp
  1071       qed
  1072     qed
  1073     from measurable_sets[OF this, of A] A \<open>i \<in> I\<close> E_closed
  1074     have "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P \<in> sets ?P"
  1075       by (simp add: E_generates)
  1076     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}"
  1077       using P_closed by (auto simp: space_PiF)
  1078     finally show "\<dots> \<in> sets ?P" .
  1079   qed
  1080   finally show "sets (PiF {I} M) \<subseteq> sigma_sets (space (PiF {I} M)) P"
  1081     by (simp add: P_closed)
  1082   show "sigma_sets (space (PiF {I} M)) P \<subseteq> sets (PiF {I} M)"
  1083     using \<open>finite I\<close> \<open>I \<noteq> {}\<close>
  1084     by (auto intro!: sets.sigma_sets_subset product_in_sets_PiFI simp: E_generates P_def)
  1085 qed
  1086 
  1087 lemma product_open_generates_sets_PiF_single:
  1088   assumes "I \<noteq> {}"
  1089   assumes [simp]: "finite I"
  1090   shows "sets (PiF {I} (\<lambda>_. borel::'b::second_countable_topology measure)) =
  1091     sigma_sets (space (PiF {I} (\<lambda>_. borel))) {Pi' I F |F. (\<forall>i\<in>I. F i \<in> Collect open)}"
  1092 proof -
  1093   from open_countable_basisE[OF open_UNIV] guess S::"'b set set" . note S = this
  1094   show ?thesis
  1095   proof (rule sigma_fprod_algebra_sigma_eq)
  1096     show "finite I" by simp
  1097     show "I \<noteq> {}" by fact
  1098     def S'\<equiv>"from_nat_into S"
  1099     show "(\<Union>j. S' j) = space borel"
  1100       using S
  1101       apply (auto simp add: from_nat_into countable_basis_proj S'_def basis_proj_def)
  1102       apply (metis (lifting, mono_tags) UNIV_I UnionE basis_proj_def countable_basis_proj countable_subset from_nat_into_surj)
  1103       done
  1104     show "range S' \<subseteq> Collect open"
  1105       using S
  1106       apply (auto simp add: from_nat_into countable_basis_proj S'_def)
  1107       apply (metis UNIV_not_empty Union_empty from_nat_into set_mp topological_basis_open[OF basis_proj] basis_proj_def)
  1108       done
  1109     show "Collect open \<subseteq> Pow (space borel)" by simp
  1110     show "sets borel = sigma_sets (space borel) (Collect open)"
  1111       by (simp add: borel_def)
  1112   qed
  1113 qed
  1114 
  1115 lemma finmap_UNIV[simp]: "(\<Union>J\<in>Collect finite. \<Pi>' j\<in>J. UNIV) = UNIV" by auto
  1116 
  1117 lemma borel_eq_PiF_borel:
  1118   shows "(borel :: ('i::countable \<Rightarrow>\<^sub>F 'a::polish_space) measure) =
  1119     PiF (Collect finite) (\<lambda>_. borel :: 'a measure)"
  1120   unfolding borel_def PiF_def
  1121 proof (rule measure_eqI, clarsimp, rule sigma_sets_eqI)
  1122   fix a::"('i \<Rightarrow>\<^sub>F 'a) set" assume "a \<in> Collect open" hence "open a" by simp
  1123   then obtain B' where B': "B'\<subseteq>basis_finmap" "a = \<Union>B'"
  1124     using finmap_topological_basis by (force simp add: topological_basis_def)
  1125   have "a \<in> sigma UNIV {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)}"
  1126     unfolding \<open>a = \<Union>B'\<close>
  1127   proof (rule sets.countable_Union)
  1128     from B' countable_basis_finmap show "countable B'" by (metis countable_subset)
  1129   next
  1130     show "B' \<subseteq> sets (sigma UNIV
  1131       {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)})" (is "_ \<subseteq> sets ?s")
  1132     proof
  1133       fix x assume "x \<in> B'" with B' have "x \<in> basis_finmap" by auto
  1134       then obtain J X where "x = Pi' J X" "finite J" "X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)"
  1135         by (auto simp: basis_finmap_def topological_basis_open[OF basis_proj])
  1136       thus "x \<in> sets ?s" by auto
  1137     qed
  1138   qed
  1139   thus "a \<in> sigma_sets UNIV {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)}"
  1140     by simp
  1141 next
  1142   fix b::"('i \<Rightarrow>\<^sub>F 'a) set"
  1143   assume "b \<in> {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)}"
  1144   hence b': "b \<in> sets (Pi\<^sub>F (Collect finite) (\<lambda>_. borel))" by (auto simp: sets_PiF borel_def)
  1145   let ?b = "\<lambda>J. b \<inter> {x. domain x = J}"
  1146   have "b = \<Union>((\<lambda>J. ?b J) ` Collect finite)" by auto
  1147   also have "\<dots> \<in> sets borel"
  1148   proof (rule sets.countable_Union, safe)
  1149     fix J::"'i set" assume "finite J"
  1150     { assume ef: "J = {}"
  1151       have "?b J \<in> sets borel"
  1152       proof cases
  1153         assume "?b J \<noteq> {}"
  1154         then obtain f where "f \<in> b" "domain f = {}" using ef by auto
  1155         hence "?b J = {f}" using \<open>J = {}\<close>
  1156           by (auto simp: finmap_eq_iff)
  1157         also have "{f} \<in> sets borel" by simp
  1158         finally show ?thesis .
  1159       qed simp
  1160     } moreover {
  1161       assume "J \<noteq> ({}::'i set)"
  1162       have "(?b J) = b \<inter> {m. domain m \<in> {J}}" by auto
  1163       also have "\<dots> \<in> sets (PiF {J} (\<lambda>_. borel))"
  1164         using b' by (rule restrict_sets_measurable) (auto simp: \<open>finite J\<close>)
  1165       also have "\<dots> = sigma_sets (space (PiF {J} (\<lambda>_. borel)))
  1166         {Pi' (J) F |F. (\<forall>j\<in>J. F j \<in> Collect open)}"
  1167         (is "_ = sigma_sets _ ?P")
  1168        by (rule product_open_generates_sets_PiF_single[OF \<open>J \<noteq> {}\<close> \<open>finite J\<close>])
  1169       also have "\<dots> \<subseteq> sigma_sets UNIV (Collect open)"
  1170         by (intro sigma_sets_mono'') (auto intro!: open_Pi'I simp: space_PiF)
  1171       finally have "(?b J) \<in> sets borel" by (simp add: borel_def)
  1172     } ultimately show "(?b J) \<in> sets borel" by blast
  1173   qed (simp add: countable_Collect_finite)
  1174   finally show "b \<in> sigma_sets UNIV (Collect open)" by (simp add: borel_def)
  1175 qed (simp add: emeasure_sigma borel_def PiF_def)
  1176 
  1177 subsection \<open>Isomorphism between Functions and Finite Maps\<close>
  1178 
  1179 lemma measurable_finmap_compose:
  1180   shows "(\<lambda>m. compose J m f) \<in> measurable (PiM (f ` J) (\<lambda>_. M)) (PiM J (\<lambda>_. M))"
  1181   unfolding compose_def by measurable
  1182 
  1183 lemma measurable_compose_inv:
  1184   assumes inj: "\<And>j. j \<in> J \<Longrightarrow> f' (f j) = j"
  1185   shows "(\<lambda>m. compose (f ` J) m f') \<in> measurable (PiM J (\<lambda>_. M)) (PiM (f ` J) (\<lambda>_. M))"
  1186   unfolding compose_def by (rule measurable_restrict) (auto simp: inj)
  1187 
  1188 locale function_to_finmap =
  1189   fixes J::"'a set" and f :: "'a \<Rightarrow> 'b::countable" and f'
  1190   assumes [simp]: "finite J"
  1191   assumes inv: "i \<in> J \<Longrightarrow> f' (f i) = i"
  1192 begin
  1193 
  1194 text \<open>to measure finmaps\<close>
  1195 
  1196 definition "fm = (finmap_of (f ` J)) o (\<lambda>g. compose (f ` J) g f')"
  1197 
  1198 lemma domain_fm[simp]: "domain (fm x) = f ` J"
  1199   unfolding fm_def by simp
  1200 
  1201 lemma fm_restrict[simp]: "fm (restrict y J) = fm y"
  1202   unfolding fm_def by (auto simp: compose_def inv intro: restrict_ext)
  1203 
  1204 lemma fm_product:
  1205   assumes "\<And>i. space (M i) = UNIV"
  1206   shows "fm -` Pi' (f ` J) S \<inter> space (Pi\<^sub>M J M) = (\<Pi>\<^sub>E j \<in> J. S (f j))"
  1207   using assms
  1208   by (auto simp: inv fm_def compose_def space_PiM Pi'_def)
  1209 
  1210 lemma fm_measurable:
  1211   assumes "f ` J \<in> N"
  1212   shows "fm \<in> measurable (Pi\<^sub>M J (\<lambda>_. M)) (Pi\<^sub>F N (\<lambda>_. M))"
  1213   unfolding fm_def
  1214 proof (rule measurable_comp, rule measurable_compose_inv)
  1215   show "finmap_of (f ` J) \<in> measurable (Pi\<^sub>M (f ` J) (\<lambda>_. M)) (PiF N (\<lambda>_. M)) "
  1216     using assms by (intro measurable_finmap_of measurable_component_singleton) auto
  1217 qed (simp_all add: inv)
  1218 
  1219 lemma proj_fm:
  1220   assumes "x \<in> J"
  1221   shows "fm m (f x) = m x"
  1222   using assms by (auto simp: fm_def compose_def o_def inv)
  1223 
  1224 lemma inj_on_compose_f': "inj_on (\<lambda>g. compose (f ` J) g f') (extensional J)"
  1225 proof (rule inj_on_inverseI)
  1226   fix x::"'a \<Rightarrow> 'c" assume "x \<in> extensional J"
  1227   thus "(\<lambda>x. compose J x f) (compose (f ` J) x f') = x"
  1228     by (auto simp: compose_def inv extensional_def)
  1229 qed
  1230 
  1231 lemma inj_on_fm:
  1232   assumes "\<And>i. space (M i) = UNIV"
  1233   shows "inj_on fm (space (Pi\<^sub>M J M))"
  1234   using assms
  1235   apply (auto simp: fm_def space_PiM PiE_def)
  1236   apply (rule comp_inj_on)
  1237   apply (rule inj_on_compose_f')
  1238   apply (rule finmap_of_inj_on_extensional_finite)
  1239   apply simp
  1240   apply (auto)
  1241   done
  1242 
  1243 text \<open>to measure functions\<close>
  1244 
  1245 definition "mf = (\<lambda>g. compose J g f) o proj"
  1246 
  1247 lemma mf_fm:
  1248   assumes "x \<in> space (Pi\<^sub>M J (\<lambda>_. M))"
  1249   shows "mf (fm x) = x"
  1250 proof -
  1251   have "mf (fm x) \<in> extensional J"
  1252     by (auto simp: mf_def extensional_def compose_def)
  1253   moreover
  1254   have "x \<in> extensional J" using assms sets.sets_into_space
  1255     by (force simp: space_PiM PiE_def)
  1256   moreover
  1257   { fix i assume "i \<in> J"
  1258     hence "mf (fm x) i = x i"
  1259       by (auto simp: inv mf_def compose_def fm_def)
  1260   }
  1261   ultimately
  1262   show ?thesis by (rule extensionalityI)
  1263 qed
  1264 
  1265 lemma mf_measurable:
  1266   assumes "space M = UNIV"
  1267   shows "mf \<in> measurable (PiF {f ` J} (\<lambda>_. M)) (PiM J (\<lambda>_. M))"
  1268   unfolding mf_def
  1269 proof (rule measurable_comp, rule measurable_proj_PiM)
  1270   show "(\<lambda>g. compose J g f) \<in> measurable (Pi\<^sub>M (f ` J) (\<lambda>x. M)) (Pi\<^sub>M J (\<lambda>_. M))"
  1271     by (rule measurable_finmap_compose)
  1272 qed (auto simp add: space_PiM extensional_def assms)
  1273 
  1274 lemma fm_image_measurable:
  1275   assumes "space M = UNIV"
  1276   assumes "X \<in> sets (Pi\<^sub>M J (\<lambda>_. M))"
  1277   shows "fm ` X \<in> sets (PiF {f ` J} (\<lambda>_. M))"
  1278 proof -
  1279   have "fm ` X = (mf) -` X \<inter> space (PiF {f ` J} (\<lambda>_. M))"
  1280   proof safe
  1281     fix x assume "x \<in> X"
  1282     with mf_fm[of x] sets.sets_into_space[OF assms(2)] show "fm x \<in> mf -` X" by auto
  1283     show "fm x \<in> space (PiF {f ` J} (\<lambda>_. M))" by (simp add: space_PiF assms)
  1284   next
  1285     fix y x
  1286     assume x: "mf y \<in> X"
  1287     assume y: "y \<in> space (PiF {f ` J} (\<lambda>_. M))"
  1288     thus "y \<in> fm ` X"
  1289       by (intro image_eqI[OF _ x], unfold finmap_eq_iff)
  1290          (auto simp: space_PiF fm_def mf_def compose_def inv Pi'_def)
  1291   qed
  1292   also have "\<dots> \<in> sets (PiF {f ` J} (\<lambda>_. M))"
  1293     using assms
  1294     by (intro measurable_sets[OF mf_measurable]) auto
  1295   finally show ?thesis .
  1296 qed
  1297 
  1298 lemma fm_image_measurable_finite:
  1299   assumes "space M = UNIV"
  1300   assumes "X \<in> sets (Pi\<^sub>M J (\<lambda>_. M::'c measure))"
  1301   shows "fm ` X \<in> sets (PiF (Collect finite) (\<lambda>_. M::'c measure))"
  1302   using fm_image_measurable[OF assms]
  1303   by (rule subspace_set_in_sets) (auto simp: finite_subset)
  1304 
  1305 text \<open>measure on finmaps\<close>
  1306 
  1307 definition "mapmeasure M N = distr M (PiF (Collect finite) N) (fm)"
  1308 
  1309 lemma sets_mapmeasure[simp]: "sets (mapmeasure M N) = sets (PiF (Collect finite) N)"
  1310   unfolding mapmeasure_def by simp
  1311 
  1312 lemma space_mapmeasure[simp]: "space (mapmeasure M N) = space (PiF (Collect finite) N)"
  1313   unfolding mapmeasure_def by simp
  1314 
  1315 lemma mapmeasure_PiF:
  1316   assumes s1: "space M = space (Pi\<^sub>M J (\<lambda>_. N))"
  1317   assumes s2: "sets M = sets (Pi\<^sub>M J (\<lambda>_. N))"
  1318   assumes "space N = UNIV"
  1319   assumes "X \<in> sets (PiF (Collect finite) (\<lambda>_. N))"
  1320   shows "emeasure (mapmeasure M (\<lambda>_. N)) X = emeasure M ((fm -` X \<inter> extensional J))"
  1321   using assms
  1322   by (auto simp: measurable_cong_sets[OF s2 refl] mapmeasure_def emeasure_distr
  1323     fm_measurable space_PiM PiE_def)
  1324 
  1325 lemma mapmeasure_PiM:
  1326   fixes N::"'c measure"
  1327   assumes s1: "space M = space (Pi\<^sub>M J (\<lambda>_. N))"
  1328   assumes s2: "sets M = (Pi\<^sub>M J (\<lambda>_. N))"
  1329   assumes N: "space N = UNIV"
  1330   assumes X: "X \<in> sets M"
  1331   shows "emeasure M X = emeasure (mapmeasure M (\<lambda>_. N)) (fm ` X)"
  1332   unfolding mapmeasure_def
  1333 proof (subst emeasure_distr, subst measurable_cong_sets[OF s2 refl], rule fm_measurable)
  1334   have "X \<subseteq> space (Pi\<^sub>M J (\<lambda>_. N))" using assms by (simp add: sets.sets_into_space)
  1335   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"
  1336     by (auto simp: vimage_image_eq inj_on_def)
  1337   thus "emeasure M X = emeasure M (fm -` fm ` X \<inter> space M)" using s1
  1338     by simp
  1339   show "fm ` X \<in> sets (PiF (Collect finite) (\<lambda>_. N))"
  1340     by (rule fm_image_measurable_finite[OF N X[simplified s2]])
  1341 qed simp
  1342 
  1343 end
  1344 
  1345 end