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