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