src/HOL/Probability/Fin_Map.thy
author hoelzl
Tue Mar 05 15:43:14 2013 +0100 (2013-03-05)
changeset 51343 b61b32f62c78
parent 51106 5746e671ea70
child 51473 1210309fddab
permissions -rw-r--r--
use generate_topology for second countable topologies, does not require intersection stable basis
     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" [0] 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> (\<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)\<^isub>F = (finmap_of K y)\<^isub>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 {* Product set of Finite Maps *}
    97 
    98 text {* This is @{term Pi} for Finite Maps, most of this is copied *}
    99 
   100 definition Pi' :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a set) \<Rightarrow> ('i \<Rightarrow>\<^isub>F 'a) set" where
   101   "Pi' I A = { P. domain P = I \<and> (\<forall>i. i \<in> I \<longrightarrow> (P)\<^isub>F i \<in> A i) } "
   102 
   103 syntax
   104   "_Pi'"  :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3PI' _:_./ _)" 10)
   105 
   106 syntax (xsymbols)
   107   "_Pi'" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>' _\<in>_./ _)"   10)
   108 
   109 syntax (HTML output)
   110   "_Pi'" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>' _\<in>_./ _)"   10)
   111 
   112 translations
   113   "PI' x:A. B" == "CONST Pi' A (%x. B)"
   114 
   115 subsubsection{*Basic Properties of @{term Pi'}*}
   116 
   117 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"
   118   by (simp add: Pi'_def)
   119 
   120 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"
   121   by (simp add:Pi'_def)
   122 
   123 lemma Pi'_mem: "f\<in> Pi' A B \<Longrightarrow> x \<in> A \<Longrightarrow> f x \<in> B x"
   124   by (simp add: Pi'_def)
   125 
   126 lemma Pi'_iff: "f \<in> Pi' I X \<longleftrightarrow> domain f = I \<and> (\<forall>i\<in>I. f i \<in> X i)"
   127   unfolding Pi'_def by auto
   128 
   129 lemma Pi'E [elim]:
   130   "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"
   131   by(auto simp: Pi'_def)
   132 
   133 lemma in_Pi'_cong:
   134   "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"
   135   by (auto simp: Pi'_def)
   136 
   137 lemma Pi'_eq_empty[simp]:
   138   assumes "finite A" shows "(Pi' A B) = {} \<longleftrightarrow> (\<exists>x\<in>A. B x = {})"
   139   using assms
   140   apply (simp add: Pi'_def, auto)
   141   apply (drule_tac x = "finmap_of A (\<lambda>u. SOME y. y \<in> B u)" in spec, auto)
   142   apply (cut_tac P= "%y. y \<in> B i" in some_eq_ex, auto)
   143   done
   144 
   145 lemma Pi'_mono: "(\<And>x. x \<in> A \<Longrightarrow> B x \<subseteq> C x) \<Longrightarrow> Pi' A B \<subseteq> Pi' A C"
   146   by (auto simp: Pi'_def)
   147 
   148 lemma Pi_Pi': "finite A \<Longrightarrow> (Pi\<^isub>E A B) = proj ` Pi' A B"
   149   apply (auto simp: Pi'_def Pi_def extensional_def)
   150   apply (rule_tac x = "finmap_of A (restrict x A)" in image_eqI)
   151   apply auto
   152   done
   153 
   154 subsection {* Topological Space of Finite Maps *}
   155 
   156 instantiation finmap :: (type, topological_space) topological_space
   157 begin
   158 
   159 definition open_finmap :: "('a \<Rightarrow>\<^isub>F 'b) set \<Rightarrow> bool" where
   160   "open_finmap = generate_topology {Pi' a b|a b. \<forall>i\<in>a. open (b i)}"
   161 
   162 lemma open_Pi'I: "(\<And>i. i \<in> I \<Longrightarrow> open (A i)) \<Longrightarrow> open (Pi' I A)"
   163   by (auto intro: generate_topology.Basis simp: open_finmap_def)
   164 
   165 instance using topological_space_generate_topology
   166   by intro_classes (auto simp: open_finmap_def class.topological_space_def)
   167 
   168 end
   169 
   170 lemma open_restricted_space:
   171   shows "open {m. P (domain m)}"
   172 proof -
   173   have "{m. P (domain m)} = (\<Union>i \<in> Collect P. {m. domain m = i})" by auto
   174   also have "open \<dots>"
   175   proof (rule, safe, cases)
   176     fix i::"'a set"
   177     assume "finite i"
   178     hence "{m. domain m = i} = Pi' i (\<lambda>_. UNIV)" by (auto simp: Pi'_def)
   179     also have "open \<dots>" by (auto intro: open_Pi'I simp: `finite i`)
   180     finally show "open {m. domain m = i}" .
   181   next
   182     fix i::"'a set"
   183     assume "\<not> finite i" hence "{m. domain m = i} = {}" by auto
   184     also have "open \<dots>" by simp
   185     finally show "open {m. domain m = i}" .
   186   qed
   187   finally show ?thesis .
   188 qed
   189 
   190 lemma closed_restricted_space:
   191   shows "closed {m. P (domain m)}"
   192   using open_restricted_space[of "\<lambda>x. \<not> P x"]
   193   unfolding closed_def by (rule back_subst) auto
   194 
   195 lemma tendsto_proj: "((\<lambda>x. x) ---> a) F \<Longrightarrow> ((\<lambda>x. (x)\<^isub>F i) ---> (a)\<^isub>F i) F"
   196   unfolding tendsto_def
   197 proof safe
   198   fix S::"'b set"
   199   let ?S = "Pi' (domain a) (\<lambda>x. if x = i then S else UNIV)"
   200   assume "open S" hence "open ?S" by (auto intro!: open_Pi'I)
   201   moreover assume "\<forall>S. open S \<longrightarrow> a \<in> S \<longrightarrow> eventually (\<lambda>x. x \<in> S) F" "a i \<in> S"
   202   ultimately have "eventually (\<lambda>x. x \<in> ?S) F" by auto
   203   thus "eventually (\<lambda>x. (x)\<^isub>F i \<in> S) F"
   204     by eventually_elim (insert `a i \<in> S`, force simp: Pi'_iff split: split_if_asm)
   205 qed
   206 
   207 lemma continuous_proj:
   208   shows "continuous_on s (\<lambda>x. (x)\<^isub>F i)"
   209   unfolding continuous_on_def
   210   by (safe intro!: tendsto_proj tendsto_ident_at_within)
   211 
   212 instance finmap :: (type, first_countable_topology) first_countable_topology
   213 proof
   214   fix x::"'a\<Rightarrow>\<^isub>F'b"
   215   have "\<forall>i. \<exists>A. countable A \<and> (\<forall>a\<in>A. x i \<in> a) \<and> (\<forall>a\<in>A. open a) \<and>
   216     (\<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")
   217   proof
   218     fix i from first_countable_basis_Int_stableE[of "x i"] guess A .
   219     thus "?th i" by (intro exI[where x=A]) simp
   220   qed
   221   then guess A unfolding choice_iff .. note A = this
   222   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
   223   have A_notempty: "\<And>i. i \<in> domain x \<Longrightarrow> A i \<noteq> {}" using open_sub[of _ "\<lambda>_. UNIV"] by auto
   224   let ?A = "(\<lambda>f. Pi' (domain x) f) ` (Pi\<^isub>E (domain x) A)"
   225   show "\<exists>A. countable A \<and> (\<forall>a\<in>A. x \<in> a \<and> open a) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>a\<in>A. a \<subseteq> S))"
   226   proof (rule exI[where x="?A"], safe)
   227     show "countable ?A" using A by (simp add: countable_PiE)
   228   next
   229     fix S::"('a \<Rightarrow>\<^isub>F 'b) set" assume "open S" "x \<in> S"
   230     thus "\<exists>a\<in>?A. a \<subseteq> S" unfolding open_finmap_def
   231     proof (induct rule: generate_topology.induct)
   232       case UNIV thus ?case by (auto simp add: ex_in_conv PiE_eq_empty_iff A_notempty)
   233     next
   234       case (Int a b)
   235       then obtain f g where
   236         "f \<in> Pi\<^isub>E (domain x) A" "Pi' (domain x) f \<subseteq> a" "g \<in> Pi\<^isub>E (domain x) A" "Pi' (domain x) g \<subseteq> b"
   237         by auto
   238       thus ?case using A
   239         by (auto simp: Pi'_iff PiE_iff extensional_def Int_stable_def
   240             intro!: bexI[where x="\<lambda>i. f i \<inter> g i"])
   241     next
   242       case (UN B)
   243       then obtain b where "x \<in> b" "b \<in> B" by auto
   244       hence "\<exists>a\<in>?A. a \<subseteq> b" using UN by simp
   245       thus ?case using `b \<in> B` by blast
   246     next
   247       case (Basis s)
   248       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
   249       have "\<forall>i. \<exists>a. (i \<in> domain x \<and> open (b i) \<and> (x)\<^isub>F i \<in> b i) \<longrightarrow> (a\<in>A i \<and> a \<subseteq> b i)"
   250         using open_sub[of _ b] by auto
   251       then obtain b'
   252         where "\<And>i. i \<in> domain x \<Longrightarrow> open (b i) \<Longrightarrow> (x)\<^isub>F i \<in> b i \<Longrightarrow> (b' i \<in>A i \<and> b' i \<subseteq> b i)"
   253           unfolding choice_iff by auto
   254       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"
   255         by (auto simp: Pi'_iff intro!: Pi'_mono)
   256       thus ?case using xs
   257         by (intro bexI[where x="Pi' a b'"])
   258           (auto simp: Pi'_iff intro!: image_eqI[where x="restrict b' (domain x)"])
   259     qed
   260   qed (insert A,auto simp: PiE_iff intro!: open_Pi'I)
   261 qed
   262 
   263 subsection {* Metric Space of Finite Maps *}
   264 
   265 instantiation finmap :: (type, metric_space) metric_space
   266 begin
   267 
   268 definition dist_finmap where
   269   "dist P Q = Max (range (\<lambda>i. dist ((P)\<^isub>F i) ((Q)\<^isub>F i))) + (if domain P = domain Q then 0 else 1)"
   270 
   271 lemma add_eq_zero_iff[simp]:
   272   fixes a b::real
   273   assumes "a \<ge> 0" "b \<ge> 0"
   274   shows "a + b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
   275 using assms by auto
   276 
   277 lemma finite_proj_image': "x \<notin> domain P \<Longrightarrow> finite ((P)\<^isub>F ` S)"
   278   by (rule finite_subset[of _ "proj P ` (domain P \<inter> S \<union> {x})"]) auto
   279 
   280 lemma finite_proj_image: "finite ((P)\<^isub>F ` S)"
   281  by (cases "\<exists>x. x \<notin> domain P") (auto intro: finite_proj_image' finite_subset[where B="domain P"])
   282 
   283 lemma finite_proj_diag: "finite ((\<lambda>i. d ((P)\<^isub>F i) ((Q)\<^isub>F i)) ` S)"
   284 proof -
   285   have "(\<lambda>i. d ((P)\<^isub>F i) ((Q)\<^isub>F i)) ` S = (\<lambda>(i, j). d i j) ` ((\<lambda>i. ((P)\<^isub>F i, (Q)\<^isub>F i)) ` S)" by auto
   286   moreover have "((\<lambda>i. ((P)\<^isub>F i, (Q)\<^isub>F i)) ` S) \<subseteq> (\<lambda>i. (P)\<^isub>F i) ` S \<times> (\<lambda>i. (Q)\<^isub>F i) ` S" by auto
   287   moreover have "finite \<dots>" using finite_proj_image[of P S] finite_proj_image[of Q S]
   288     by (intro finite_cartesian_product) simp_all
   289   ultimately show ?thesis by (simp add: finite_subset)
   290 qed
   291 
   292 lemma dist_le_1_imp_domain_eq:
   293   shows "dist P Q < 1 \<Longrightarrow> domain P = domain Q"
   294   by (simp add: dist_finmap_def finite_proj_diag split: split_if_asm)
   295 
   296 lemma dist_proj:
   297   shows "dist ((x)\<^isub>F i) ((y)\<^isub>F i) \<le> dist x y"
   298 proof -
   299   have "dist (x i) (y i) \<le> Max (range (\<lambda>i. dist (x i) (y i)))"
   300     by (simp add: Max_ge_iff finite_proj_diag)
   301   also have "\<dots> \<le> dist x y" by (simp add: dist_finmap_def)
   302   finally show ?thesis .
   303 qed
   304 
   305 lemma dist_finmap_lessI:
   306   assumes "domain P = domain Q"
   307   assumes "0 < e"
   308   assumes "\<And>i. i \<in> domain P \<Longrightarrow> dist (P i) (Q i) < e"
   309   shows "dist P Q < e"
   310 proof -
   311   have "dist P Q = Max (range (\<lambda>i. dist (P i) (Q i)))"
   312     using assms by (simp add: dist_finmap_def finite_proj_diag)
   313   also have "\<dots> < e"
   314   proof (subst Max_less_iff, safe)
   315     fix i
   316     show "dist ((P)\<^isub>F i) ((Q)\<^isub>F i) < e" using assms
   317       by (cases "i \<in> domain P") simp_all
   318   qed (simp add: finite_proj_diag)
   319   finally show ?thesis .
   320 qed
   321 
   322 instance
   323 proof
   324   fix S::"('a \<Rightarrow>\<^isub>F 'b) set"
   325   show "open S = (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)" (is "_ = ?od")
   326   proof
   327     assume "open S"
   328     thus ?od
   329       unfolding open_finmap_def
   330     proof (induct rule: generate_topology.induct)
   331       case UNIV thus ?case by (auto intro: zero_less_one)
   332     next
   333       case (Int a b)
   334       show ?case
   335       proof safe
   336         fix x assume x: "x \<in> a" "x \<in> b"
   337         with Int x obtain e1 e2 where
   338           "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
   339         thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> a \<inter> b"
   340           by (auto intro!: exI[where x="min e1 e2"])
   341       qed
   342     next
   343       case (UN K)
   344       show ?case
   345       proof safe
   346         fix x X assume "x \<in> X" "X \<in> K"
   347         moreover with UN obtain e where "e>0" "\<And>y. dist y x < e \<longrightarrow> y \<in> X" by force
   348         ultimately show "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> \<Union>K" by auto
   349       qed
   350     next
   351       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
   352       show ?case
   353       proof safe
   354         fix x assume "x \<in> s"
   355         hence [simp]: "finite a" and a_dom: "a = domain x" using s by (auto simp: Pi'_iff)
   356         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)"
   357           using b `x \<in> s` by atomize_elim (intro bchoice, auto simp: open_dist s)
   358         hence in_b: "\<And>i y. i \<in> a \<Longrightarrow> dist y (proj x i) < es i \<Longrightarrow> y \<in> b i" by auto
   359         show "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> s"
   360         proof (cases, rule, safe)
   361           assume "a \<noteq> {}"
   362           show "0 < min 1 (Min (es ` a))" using es by (auto simp: `a \<noteq> {}`)
   363           fix y assume d: "dist y x < min 1 (Min (es ` a))"
   364           show "y \<in> s" unfolding s
   365           proof
   366             show "domain y = a" using d s `a \<noteq> {}` by (auto simp: dist_le_1_imp_domain_eq a_dom)
   367             fix i assume "i \<in> a"
   368             moreover
   369             hence "dist ((y)\<^isub>F i) ((x)\<^isub>F i) < es i" using d
   370               by (auto simp: dist_finmap_def `a \<noteq> {}` intro!: le_less_trans[OF dist_proj])
   371             ultimately
   372             show "y i \<in> b i" by (rule in_b)
   373           qed
   374         next
   375           assume "\<not>a \<noteq> {}"
   376           thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> s"
   377             using s `x \<in> s` by (auto simp: Pi'_def dist_le_1_imp_domain_eq intro!: exI[where x=1])
   378         qed
   379       qed
   380     qed
   381   next
   382     assume "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   383     then obtain e where e_pos: "\<And>x. x \<in> S \<Longrightarrow> e x > 0" and
   384       e_in:  "\<And>x y . x \<in> S \<Longrightarrow> dist y x < e x \<Longrightarrow> y \<in> S"
   385       unfolding bchoice_iff
   386       by auto
   387     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))}"
   388     proof safe
   389       fix x assume "x \<in> S"
   390       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))}"
   391         using e_pos by (auto intro!: exI[where x="Pi' (domain x) (\<lambda>i. ball (x i) (e x))"])
   392     next
   393       fix x y
   394       assume "y \<in> S"
   395       moreover
   396       assume "x \<in> (\<Pi>' i\<in>domain y. ball (y i) (e y))"
   397       hence "dist x y < e y" using e_pos `y \<in> S`
   398         by (auto simp: dist_finmap_def Pi'_iff finite_proj_diag dist_commute)
   399       ultimately show "x \<in> S" by (rule e_in)
   400     qed
   401     also have "open \<dots>"
   402       unfolding open_finmap_def
   403       by (intro generate_topology.UN) (auto intro: generate_topology.Basis)
   404     finally show "open S" .
   405   qed
   406 next
   407   fix P Q::"'a \<Rightarrow>\<^isub>F 'b"
   408   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))"
   409     by (metis Max.in_idem Max_in max_def min_max.sup.commute order_refl)
   410   show "dist P Q = 0 \<longleftrightarrow> P = Q"
   411     by (auto simp: finmap_eq_iff dist_finmap_def Max_ge_iff finite_proj_diag Max_eq_iff
   412       intro!: Max_eqI image_eqI[where x=undefined])
   413 next
   414   fix P Q R::"'a \<Rightarrow>\<^isub>F 'b"
   415   let ?dists = "\<lambda>P Q i. dist ((P)\<^isub>F i) ((Q)\<^isub>F i)"
   416   let ?dpq = "?dists P Q" and ?dpr = "?dists P R" and ?dqr = "?dists Q R"
   417   let ?dom = "\<lambda>P Q. (if domain P = domain Q then 0 else 1::real)"
   418   have "dist P Q = Max (range ?dpq) + ?dom P Q"
   419     by (simp add: dist_finmap_def)
   420   also obtain t where "t \<in> range ?dpq" "t = Max (range ?dpq)" by (simp add: finite_proj_diag)
   421   then obtain i where "Max (range ?dpq) = ?dpq i" by auto
   422   also have "?dpq i \<le> ?dpr i + ?dqr i" by (rule dist_triangle2)
   423   also have "?dpr i \<le> Max (range ?dpr)" by (simp add: finite_proj_diag)
   424   also have "?dqr i \<le> Max (range ?dqr)" by (simp add: finite_proj_diag)
   425   also have "?dom P Q \<le> ?dom P R + ?dom Q R" by simp
   426   finally show "dist P Q \<le> dist P R + dist Q R" by (simp add: dist_finmap_def ac_simps)
   427 qed
   428 
   429 end
   430 
   431 subsection {* Complete Space of Finite Maps *}
   432 
   433 lemma tendsto_finmap:
   434   fixes f::"nat \<Rightarrow> ('i \<Rightarrow>\<^isub>F ('a::metric_space))"
   435   assumes ind_f:  "\<And>n. domain (f n) = domain g"
   436   assumes proj_g:  "\<And>i. i \<in> domain g \<Longrightarrow> (\<lambda>n. (f n) i) ----> g i"
   437   shows "f ----> g"
   438   unfolding tendsto_iff
   439 proof safe
   440   fix e::real assume "0 < e"
   441   let ?dists = "\<lambda>x i. dist ((f x)\<^isub>F i) ((g)\<^isub>F i)"
   442   have "eventually (\<lambda>x. \<forall>i\<in>domain g. ?dists x i < e) sequentially"
   443     using finite_domain[of g] proj_g
   444   proof induct
   445     case (insert i G)
   446     with `0 < e` have "eventually (\<lambda>x. ?dists x i < e) sequentially" by (auto simp add: tendsto_iff)
   447     moreover
   448     from insert have "eventually (\<lambda>x. \<forall>i\<in>G. dist ((f x)\<^isub>F i) ((g)\<^isub>F i) < e) sequentially" by simp
   449     ultimately show ?case by eventually_elim auto
   450   qed simp
   451   thus "eventually (\<lambda>x. dist (f x) g < e) sequentially"
   452     by eventually_elim (auto simp add: dist_finmap_def finite_proj_diag ind_f `0 < e`)
   453 qed
   454 
   455 instance finmap :: (type, complete_space) complete_space
   456 proof
   457   fix P::"nat \<Rightarrow> 'a \<Rightarrow>\<^isub>F 'b"
   458   assume "Cauchy P"
   459   then obtain Nd where Nd: "\<And>n. n \<ge> Nd \<Longrightarrow> dist (P n) (P Nd) < 1"
   460     by (force simp: cauchy)
   461   def d \<equiv> "domain (P Nd)"
   462   with Nd have dim: "\<And>n. n \<ge> Nd \<Longrightarrow> domain (P n) = d" using dist_le_1_imp_domain_eq by auto
   463   have [simp]: "finite d" unfolding d_def by simp
   464   def p \<equiv> "\<lambda>i n. (P n) i"
   465   def q \<equiv> "\<lambda>i. lim (p i)"
   466   def Q \<equiv> "finmap_of d q"
   467   have q: "\<And>i. i \<in> d \<Longrightarrow> q i = Q i" by (auto simp add: Q_def Abs_finmap_inverse)
   468   {
   469     fix i assume "i \<in> d"
   470     have "Cauchy (p i)" unfolding cauchy p_def
   471     proof safe
   472       fix e::real assume "0 < e"
   473       with `Cauchy P` obtain N where N: "\<And>n. n\<ge>N \<Longrightarrow> dist (P n) (P N) < min e 1"
   474         by (force simp: cauchy min_def)
   475       hence "\<And>n. n \<ge> N \<Longrightarrow> domain (P n) = domain (P N)" using dist_le_1_imp_domain_eq by auto
   476       with dim have dim: "\<And>n. n \<ge> N \<Longrightarrow> domain (P n) = d" by (metis nat_le_linear)
   477       show "\<exists>N. \<forall>n\<ge>N. dist ((P n) i) ((P N) i) < e"
   478       proof (safe intro!: exI[where x="N"])
   479         fix n assume "N \<le> n" have "N \<le> N" by simp
   480         have "dist ((P n) i) ((P N) i) \<le> dist (P n) (P N)"
   481           using dim[OF `N \<le> n`]  dim[OF `N \<le> N`] `i \<in> d`
   482           by (auto intro!: dist_proj)
   483         also have "\<dots> < e" using N[OF `N \<le> n`] by simp
   484         finally show "dist ((P n) i) ((P N) i) < e" .
   485       qed
   486     qed
   487     hence "convergent (p i)" by (metis Cauchy_convergent_iff)
   488     hence "p i ----> q i" unfolding q_def convergent_def by (metis limI)
   489   } note p = this
   490   have "P ----> Q"
   491   proof (rule metric_LIMSEQ_I)
   492     fix e::real assume "0 < e"
   493     have "\<exists>ni. \<forall>i\<in>d. \<forall>n\<ge>ni i. dist (p i n) (q i) < e"
   494     proof (safe intro!: bchoice)
   495       fix i assume "i \<in> d"
   496       from p[OF `i \<in> d`, THEN metric_LIMSEQ_D, OF `0 < e`]
   497       show "\<exists>no. \<forall>n\<ge>no. dist (p i n) (q i) < e" .
   498     qed then guess ni .. note ni = this
   499     def N \<equiv> "max Nd (Max (ni ` d))"
   500     show "\<exists>N. \<forall>n\<ge>N. dist (P n) Q < e"
   501     proof (safe intro!: exI[where x="N"])
   502       fix n assume "N \<le> n"
   503       hence dom: "domain (P n) = d" "domain Q = d" "domain (P n) = domain Q"
   504         using dim by (simp_all add: N_def Q_def dim_def Abs_finmap_inverse)
   505       show "dist (P n) Q < e"
   506       proof (rule dist_finmap_lessI[OF dom(3) `0 < e`])
   507         fix i
   508         assume "i \<in> domain (P n)"
   509         hence "ni i \<le> Max (ni ` d)" using dom by simp
   510         also have "\<dots> \<le> N" by (simp add: N_def)
   511         finally show "dist ((P n)\<^isub>F i) ((Q)\<^isub>F i) < e" using ni `i \<in> domain (P n)` `N \<le> n` dom
   512           by (auto simp: p_def q N_def less_imp_le)
   513       qed
   514     qed
   515   qed
   516   thus "convergent P" by (auto simp: convergent_def)
   517 qed
   518 
   519 subsection {* Second Countable Space of Finite Maps *}
   520 
   521 instantiation finmap :: (countable, second_countable_topology) second_countable_topology
   522 begin
   523 
   524 definition basis_proj::"'b set set"
   525   where "basis_proj = (SOME B. countable B \<and> topological_basis B)"
   526 
   527 lemma countable_basis_proj: "countable basis_proj" and basis_proj: "topological_basis basis_proj"
   528   unfolding basis_proj_def by (intro is_basis countable_basis)+
   529 
   530 definition basis_finmap::"('a \<Rightarrow>\<^isub>F 'b) set set"
   531   where "basis_finmap = {Pi' I S|I S. finite I \<and> (\<forall>i \<in> I. S i \<in> basis_proj)}"
   532 
   533 lemma in_basis_finmapI:
   534   assumes "finite I" assumes "\<And>i. i \<in> I \<Longrightarrow> S i \<in> basis_proj"
   535   shows "Pi' I S \<in> basis_finmap"
   536   using assms unfolding basis_finmap_def by auto
   537 
   538 lemma basis_finmap_eq:
   539   assumes "basis_proj \<noteq> {}"
   540   shows "basis_finmap = (\<lambda>f. Pi' (domain f) (\<lambda>i. from_nat_into basis_proj ((f)\<^isub>F i))) `
   541     (UNIV::('a \<Rightarrow>\<^isub>F nat) set)" (is "_ = ?f ` _")
   542   unfolding basis_finmap_def
   543 proof safe
   544   fix I::"'a set" and S::"'a \<Rightarrow> 'b set"
   545   assume "finite I" "\<forall>i\<in>I. S i \<in> basis_proj"
   546   hence "Pi' I S = ?f (finmap_of I (\<lambda>x. to_nat_on basis_proj (S x)))"
   547     by (force simp: Pi'_def countable_basis_proj)
   548   thus "Pi' I S \<in> range ?f" by simp
   549 next
   550   fix x and f::"'a \<Rightarrow>\<^isub>F nat"
   551   show "\<exists>I S. (\<Pi>' i\<in>domain f. from_nat_into local.basis_proj ((f)\<^isub>F i)) = Pi' I S \<and>
   552     finite I \<and> (\<forall>i\<in>I. S i \<in> local.basis_proj)"
   553     using assms by (auto intro: from_nat_into)
   554 qed
   555 
   556 lemma basis_finmap_eq_empty: "basis_proj = {} \<Longrightarrow> basis_finmap = {Pi' {} undefined}"
   557   by (auto simp: Pi'_iff basis_finmap_def)
   558 
   559 lemma countable_basis_finmap: "countable basis_finmap"
   560   by (cases "basis_proj = {}") (auto simp: basis_finmap_eq basis_finmap_eq_empty)
   561 
   562 lemma finmap_topological_basis:
   563   "topological_basis basis_finmap"
   564 proof (subst topological_basis_iff, safe)
   565   fix B' assume "B' \<in> basis_finmap"
   566   thus "open B'"
   567     by (auto intro!: open_Pi'I topological_basis_open[OF basis_proj]
   568       simp: topological_basis_def basis_finmap_def Let_def)
   569 next
   570   fix O'::"('a \<Rightarrow>\<^isub>F 'b) set" and x
   571   assume O': "open O'" "x \<in> O'"
   572   then obtain a where a:
   573     "x \<in> Pi' (domain x) a" "Pi' (domain x) a \<subseteq> O'" "\<And>i. i\<in>domain x \<Longrightarrow> open (a i)"
   574     unfolding open_finmap_def
   575   proof (atomize_elim, induct rule: generate_topology.induct)
   576     case (Int a b)
   577     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))"
   578     from Int obtain f g where "?p a f" "?p b g" by auto
   579     thus ?case by (force intro!: exI[where x="\<lambda>i. f i \<inter> g i"] simp: Pi'_def)
   580   next
   581     case (UN k)
   582     then obtain kk a where "x \<in> kk" "kk \<in> k" "x \<in> Pi' (domain x) a" "Pi' (domain x) a \<subseteq> kk"
   583       "\<And>i. i\<in>domain x \<Longrightarrow> open (a i)"
   584       by force
   585     thus ?case by blast
   586   qed (auto simp: Pi'_def)
   587   have "\<exists>B.
   588     (\<forall>i\<in>domain x. x i \<in> B i \<and> B i \<subseteq> a i \<and> B i \<in> basis_proj)"
   589   proof (rule bchoice, safe)
   590     fix i assume "i \<in> domain x"
   591     hence "open (a i)" "x i \<in> a i" using a by auto
   592     from topological_basisE[OF basis_proj this] guess b' .
   593     thus "\<exists>y. x i \<in> y \<and> y \<subseteq> a i \<and> y \<in> basis_proj" by auto
   594   qed
   595   then guess B .. note B = this
   596   def B' \<equiv> "Pi' (domain x) (\<lambda>i. (B i)::'b set)"
   597   have "B' \<subseteq> Pi' (domain x) a" using B by (auto intro!: Pi'_mono simp: B'_def)
   598   also note `\<dots> \<subseteq> O'`
   599   finally show "\<exists>B'\<in>basis_finmap. x \<in> B' \<and> B' \<subseteq> O'" using B
   600     by (auto intro!: bexI[where x=B'] Pi'_mono in_basis_finmapI simp: B'_def)
   601 qed
   602 
   603 lemma range_enum_basis_finmap_imp_open:
   604   assumes "x \<in> basis_finmap"
   605   shows "open x"
   606   using finmap_topological_basis assms by (auto simp: topological_basis_def)
   607 
   608 instance proof qed (blast intro: finmap_topological_basis countable_basis_finmap topological_basis_imp_subbasis)
   609 
   610 end
   611 
   612 subsection {* Polish Space of Finite Maps *}
   613 
   614 instance finmap :: (countable, polish_space) polish_space proof qed
   615 
   616 
   617 subsection {* Product Measurable Space of Finite Maps *}
   618 
   619 definition "PiF I M \<equiv>
   620   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))}"
   621 
   622 abbreviation
   623   "Pi\<^isub>F I M \<equiv> PiF I M"
   624 
   625 syntax
   626   "_PiF" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3PIF _:_./ _)" 10)
   627 
   628 syntax (xsymbols)
   629   "_PiF" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^isub>F _\<in>_./ _)"  10)
   630 
   631 syntax (HTML output)
   632   "_PiF" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^isub>F _\<in>_./ _)"  10)
   633 
   634 translations
   635   "PIF x: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     moreover with assms obtain l where "s = set l" using finite_list by blast
   721     ultimately show "x \<in> (\<Union>n. let s = set (from_nat n) in if P s then f s else {})" using `P s`
   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>\<^isub>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 `y \<in> sets N`
   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 `J \<subseteq> I` 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\<^isub>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 `finite J`)
   870   apply simp
   871   done
   872 
   873 lemma proj_measurable_singleton:
   874   assumes "A \<in> sets (M i)"
   875   shows "(\<lambda>x. (x)\<^isub>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)\<^isub>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 ] `A \<in> sets (M i)` assms
   881     by (auto simp: space_PiF Pi'_def)
   882   thus ?thesis  using assms `A \<in> sets (M i)`
   883     by (intro in_sets_PiFI) auto
   884 next
   885   assume "i \<notin> I"
   886   hence "(\<lambda>x. (x)\<^isub>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)\<^isub>F i) \<in> measurable (PiF {I} M) (M i)"
   894   by (unfold measurable_def, intro CollectI conjI ballI proj_measurable_singleton assms)
   895      (insert `i \<in> I`, 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)\<^isub>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)\<^isub>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 `z \<in> sets (M i)` `finite J`
   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 `y \<in> space (M i)`, 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>\<^isub>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>)\<^isub>F i \<in> A} \<in> sets (PiF {J} M)"
   934   proof
   935     have "{\<omega> \<in> space (PiF {J} M). (\<omega>)\<^isub>F i \<in> A} =
   936       (\<lambda>\<omega>. (\<omega>)\<^isub>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 {* adapted from @{thm sets_PiM_single} *}
   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 `I \<noteq> {}` 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 `I \<noteq> {}` 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 {* adapted from @{thm PiE_cong} *}
   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 {* adapted from @{thm Pi_UN} *}
   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: `finite I` 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 `finite I` 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] `domain f = I` `i \<in> I`
  1011     show "f i \<in> A k i " by (auto simp: `finite I`)
  1012   qed (simp add: `domain f = I` `finite I`)
  1013   then show "f \<in> (\<Union>n. Pi' I (A n))" by auto
  1014 qed (auto simp: Pi'_def `finite I`)
  1015 
  1016 text {* adapted from @{thm sigma_prod_algebra_sigma_eq} *}
  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\<^isub>F {I} M)) P"
  1029   from `finite I`[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: `finite I`)
  1032   have P_closed: "P \<subseteq> Pow (space (Pi\<^isub>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)\<^isub>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 `i \<in> I` by fact
  1045       from space_P `i \<in> I` show "(\<lambda>x. (x)\<^isub>F i) \<in> space ?P \<rightarrow> space (M i)"
  1046         by auto
  1047       show "\<forall>A\<in>E i. (\<lambda>x. (x)\<^isub>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)\<^isub>F i) -` A \<inter> space ?P = (\<Pi>' j\<in>I. if i = j then A else space (M j))"
  1051           using E_closed `i \<in> I` 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)\<^isub>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 `i \<in> I` E_closed
  1074     have "(\<lambda>x. (x)\<^isub>F i) -` A \<inter> space ?P \<in> sets ?P"
  1075       by (simp add: E_generates)
  1076     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}"
  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 `finite I` `I \<noteq> {}`
  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 : J. UNIV) = UNIV" by auto
  1116 
  1117 lemma borel_eq_PiF_borel:
  1118   shows "(borel :: ('i::countable \<Rightarrow>\<^isub>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>\<^isub>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 `a = \<Union>B'`
  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>\<^isub>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\<^isub>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 `J = {}`
  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: `finite J`)
  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 `J \<noteq> {}` `finite J`])
  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 {* Isomorphism between Functions and Finite Maps *}
  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 {* to measure finmaps *}
  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\<^isub>M J M) = (\<Pi>\<^isub>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\<^isub>M J (\<lambda>_. M)) (Pi\<^isub>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\<^isub>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\<^isub>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 {* to measure functions *}
  1244 
  1245 definition "mf = (\<lambda>g. compose J g f) o proj"
  1246 
  1247 lemma mf_fm:
  1248   assumes "x \<in> space (Pi\<^isub>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\<^isub>M (f ` J) (\<lambda>x. M)) (Pi\<^isub>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\<^isub>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\<^isub>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 {* measure on finmaps *}
  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\<^isub>M J (\<lambda>_. N))"
  1317   assumes s2: "sets M = sets (Pi\<^isub>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_eqI[OF s1 refl 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\<^isub>M J (\<lambda>_. N))"
  1328   assumes s2: "sets M = (Pi\<^isub>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_eqI[OF s1 refl s2 refl], rule fm_measurable)
  1334   have "X \<subseteq> space (Pi\<^isub>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\<^isub>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