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