
immler@50091: (*  Title:      HOL/Probability/Fin_Map.thy
immler@50088:     Author:     Fabian Immler, TU München
immler@50088: *)
immler@50088: 
immler@50091: header {* Finite Maps *}
immler@50091: 
immler@50088: theory Fin_Map
immler@50088: imports Finite_Product_Measure
immler@50088: begin
immler@50088: 
immler@50088: text {* Auxiliary type that is instantiated to @{class polish_space}, needed for the proof of
immler@50088:   projective limit. @{const extensional} functions are used for the representation in order to
wenzelm@53015:   stay close to the developments of (finite) products @{const Pi\<^sub>E} and their sigma-algebra
wenzelm@53015:   @{const Pi\<^sub>M}. *}
immler@50088: 
wenzelm@53015: typedef ('i, 'a) finmap ("(_ \<Rightarrow>\<^sub>F /_)" [22, 21] 21) =
immler@50088:   "{(I::'i set, f::'i \<Rightarrow> 'a). finite I \<and> f \<in> extensional I}" by auto
immler@50088: 
immler@50088: subsection {* Domain and Application *}
immler@50088: 
immler@50088: definition domain where "domain P = fst (Rep_finmap P)"
immler@50088: 
immler@50088: lemma finite_domain[simp, intro]: "finite (domain P)"
immler@50088:   by (cases P) (auto simp: domain_def Abs_finmap_inverse)
immler@50088: 
wenzelm@53015: definition proj ("'((_)')\<^sub>F" [0] 1000) where "proj P i = snd (Rep_finmap P) i"
immler@50088: 
immler@50088: declare [[coercion proj]]
immler@50088: 
wenzelm@53015: lemma extensional_proj[simp, intro]: "(P)\<^sub>F \<in> extensional (domain P)"
immler@50088:   by (cases P) (auto simp: domain_def Abs_finmap_inverse proj_def[abs_def])
immler@50088: 
immler@50088: lemma proj_undefined[simp, intro]: "i \<notin> domain P \<Longrightarrow> P i = undefined"
immler@50088:   using extensional_proj[of P] unfolding extensional_def by auto
immler@50088: 
immler@50088: lemma finmap_eq_iff: "P = Q \<longleftrightarrow> (domain P = domain Q \<and> (\<forall>i\<in>domain P. P i = Q i))"
immler@50088:   by (cases P, cases Q)
immler@50088:      (auto simp add: Abs_finmap_inject extensional_def domain_def proj_def Abs_finmap_inverse
immler@50088:               intro: extensionalityI)
immler@50088: 
immler@50088: subsection {* Countable Finite Maps *}
immler@50088: 
immler@50088: instance finmap :: (countable, countable) countable
immler@50088: proof
wenzelm@53015:   obtain mapper where mapper: "\<And>fm :: 'a \<Rightarrow>\<^sub>F 'b. set (mapper fm) = domain fm"
immler@50088:     by (metis finite_list[OF finite_domain])
wenzelm@53015:   have "inj (\<lambda>fm. map (\<lambda>i. (i, (fm)\<^sub>F i)) (mapper fm))" (is "inj ?F")
immler@50088:   proof (rule inj_onI)
immler@50088:     fix f1 f2 assume "?F f1 = ?F f2"
immler@50088:     then have "map fst (?F f1) = map fst (?F f2)" by simp
immler@50088:     then have "mapper f1 = mapper f2" by (simp add: comp_def)
immler@50088:     then have "domain f1 = domain f2" by (simp add: mapper[symmetric])
immler@50088:     with `?F f1 = ?F f2` show "f1 = f2"
immler@50088:       unfolding `mapper f1 = mapper f2` map_eq_conv mapper
immler@50088:       by (simp add: finmap_eq_iff)
immler@50088:   qed
wenzelm@53015:   then show "\<exists>to_nat :: 'a \<Rightarrow>\<^sub>F 'b \<Rightarrow> nat. inj to_nat"
immler@50088:     by (intro exI[of _ "to_nat \<circ> ?F"] inj_comp) auto
immler@50088: qed
immler@50088: 
immler@50088: subsection {* Constructor of Finite Maps *}
immler@50088: 
immler@50088: definition "finmap_of inds f = Abs_finmap (inds, restrict f inds)"
immler@50088: 
immler@50088: lemma proj_finmap_of[simp]:
immler@50088:   assumes "finite inds"
wenzelm@53015:   shows "(finmap_of inds f)\<^sub>F = restrict f inds"
immler@50088:   using assms
immler@50088:   by (auto simp: Abs_finmap_inverse finmap_of_def proj_def)
immler@50088: 
immler@50088: lemma domain_finmap_of[simp]:
immler@50088:   assumes "finite inds"
immler@50088:   shows "domain (finmap_of inds f) = inds"
immler@50088:   using assms
immler@50088:   by (auto simp: Abs_finmap_inverse finmap_of_def domain_def)
immler@50088: 
immler@50088: lemma finmap_of_eq_iff[simp]:
immler@50088:   assumes "finite i" "finite j"
immler@51104:   shows "finmap_of i m = finmap_of j n \<longleftrightarrow> i = j \<and> (\<forall>k\<in>i. m k= n k)"
immler@51104:   using assms by (auto simp: finmap_eq_iff)
immler@50088: 
hoelzl@50124: lemma finmap_of_inj_on_extensional_finite:
immler@50088:   assumes "finite K"
immler@50088:   assumes "S \<subseteq> extensional K"
immler@50088:   shows "inj_on (finmap_of K) S"
immler@50088: proof (rule inj_onI)
immler@50088:   fix x y::"'a \<Rightarrow> 'b"
immler@50088:   assume "finmap_of K x = finmap_of K y"
wenzelm@53015:   hence "(finmap_of K x)\<^sub>F = (finmap_of K y)\<^sub>F" by simp
immler@50088:   moreover
immler@50088:   assume "x \<in> S" "y \<in> S" hence "x \<in> extensional K" "y \<in> extensional K" using assms by auto
immler@50088:   ultimately
immler@50088:   show "x = y" using assms by (simp add: extensional_restrict)
immler@50088: qed
immler@50088: 
immler@50088: subsection {* Product set of Finite Maps *}
immler@50088: 
immler@50088: text {* This is @{term Pi} for Finite Maps, most of this is copied *}
immler@50088: 
wenzelm@53015: definition Pi' :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a set) \<Rightarrow> ('i \<Rightarrow>\<^sub>F 'a) set" where
wenzelm@53015:   "Pi' I A = { P. domain P = I \<and> (\<forall>i. i \<in> I \<longrightarrow> (P)\<^sub>F i \<in> A i) } "
immler@50088: 
immler@50088: syntax
immler@50088:   "_Pi'"  :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3PI' _:_./ _)" 10)
immler@50088: 
immler@50088: syntax (xsymbols)
immler@50088:   "_Pi'" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>' _\<in>_./ _)"   10)
immler@50088: 
immler@50088: syntax (HTML output)
immler@50088:   "_Pi'" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>' _\<in>_./ _)"   10)
immler@50088: 
immler@50088: translations
immler@50088:   "PI' x:A. B" == "CONST Pi' A (%x. B)"
immler@50088: 
immler@50088: subsubsection{*Basic Properties of @{term Pi'}*}
immler@50088: 
immler@50088: 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"
immler@50088:   by (simp add: Pi'_def)
immler@50088: 
immler@50088: 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"
immler@50088:   by (simp add:Pi'_def)
immler@50088: 
immler@50088: lemma Pi'_mem: "f\<in> Pi' A B \<Longrightarrow> x \<in> A \<Longrightarrow> f x \<in> B x"
immler@50088:   by (simp add: Pi'_def)
immler@50088: 
immler@50088: lemma Pi'_iff: "f \<in> Pi' I X \<longleftrightarrow> domain f = I \<and> (\<forall>i\<in>I. f i \<in> X i)"
immler@50088:   unfolding Pi'_def by auto
immler@50088: 
immler@50088: lemma Pi'E [elim]:
immler@50088:   "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"
immler@50088:   by(auto simp: Pi'_def)
immler@50088: 
immler@50088: lemma in_Pi'_cong:
immler@50088:   "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"
immler@50088:   by (auto simp: Pi'_def)
immler@50088: 
immler@50088: lemma Pi'_eq_empty[simp]:
immler@50088:   assumes "finite A" shows "(Pi' A B) = {} \<longleftrightarrow> (\<exists>x\<in>A. B x = {})"
immler@50088:   using assms
immler@50088:   apply (simp add: Pi'_def, auto)
immler@50088:   apply (drule_tac x = "finmap_of A (\<lambda>u. SOME y. y \<in> B u)" in spec, auto)
immler@50088:   apply (cut_tac P= "%y. y \<in> B i" in some_eq_ex, auto)
immler@50088:   done
immler@50088: 
immler@50088: lemma Pi'_mono: "(\<And>x. x \<in> A \<Longrightarrow> B x \<subseteq> C x) \<Longrightarrow> Pi' A B \<subseteq> Pi' A C"
immler@50088:   by (auto simp: Pi'_def)
immler@50088: 
wenzelm@53015: lemma Pi_Pi': "finite A \<Longrightarrow> (Pi\<^sub>E A B) = proj ` Pi' A B"
immler@50088:   apply (auto simp: Pi'_def Pi_def extensional_def)
immler@50088:   apply (rule_tac x = "finmap_of A (restrict x A)" in image_eqI)
immler@50088:   apply auto
immler@50088:   done
immler@50088: 
immler@51105: subsection {* Topological Space of Finite Maps *}
immler@51105: 
immler@51105: instantiation finmap :: (type, topological_space) topological_space
immler@51105: begin
immler@51105: 
wenzelm@53015: definition open_finmap :: "('a \<Rightarrow>\<^sub>F 'b) set \<Rightarrow> bool" where
immler@51105:   "open_finmap = generate_topology {Pi' a b|a b. \<forall>i\<in>a. open (b i)}"
immler@51105: 
immler@51105: lemma open_Pi'I: "(\<And>i. i \<in> I \<Longrightarrow> open (A i)) \<Longrightarrow> open (Pi' I A)"
immler@51105:   by (auto intro: generate_topology.Basis simp: open_finmap_def)
immler@51105: 
immler@51105: instance using topological_space_generate_topology
immler@51105:   by intro_classes (auto simp: open_finmap_def class.topological_space_def)
immler@51105: 
immler@51105: end
immler@51105: 
immler@51105: lemma open_restricted_space:
immler@51105:   shows "open {m. P (domain m)}"
immler@51105: proof -
immler@51105:   have "{m. P (domain m)} = (\<Union>i \<in> Collect P. {m. domain m = i})" by auto
immler@51105:   also have "open \<dots>"
immler@51105:   proof (rule, safe, cases)
immler@51105:     fix i::"'a set"
immler@51105:     assume "finite i"
immler@51105:     hence "{m. domain m = i} = Pi' i (\<lambda>_. UNIV)" by (auto simp: Pi'_def)
immler@51105:     also have "open \<dots>" by (auto intro: open_Pi'I simp: `finite i`)
immler@51105:     finally show "open {m. domain m = i}" .
immler@51105:   next
immler@51105:     fix i::"'a set"
immler@51105:     assume "\<not> finite i" hence "{m. domain m = i} = {}" by auto
immler@51105:     also have "open \<dots>" by simp
immler@51105:     finally show "open {m. domain m = i}" .
immler@51105:   qed
immler@51105:   finally show ?thesis .
immler@51105: qed
immler@51105: 
immler@51105: lemma closed_restricted_space:
immler@51105:   shows "closed {m. P (domain m)}"
immler@51105:   using open_restricted_space[of "\<lambda>x. \<not> P x"]
immler@51105:   unfolding closed_def by (rule back_subst) auto
immler@51105: 
wenzelm@53015: lemma tendsto_proj: "((\<lambda>x. x) ---> a) F \<Longrightarrow> ((\<lambda>x. (x)\<^sub>F i) ---> (a)\<^sub>F i) F"
immler@51105:   unfolding tendsto_def
immler@51105: proof safe
immler@51105:   fix S::"'b set"
immler@51105:   let ?S = "Pi' (domain a) (\<lambda>x. if x = i then S else UNIV)"
immler@51105:   assume "open S" hence "open ?S" by (auto intro!: open_Pi'I)
immler@51105:   moreover assume "\<forall>S. open S \<longrightarrow> a \<in> S \<longrightarrow> eventually (\<lambda>x. x \<in> S) F" "a i \<in> S"
immler@51105:   ultimately have "eventually (\<lambda>x. x \<in> ?S) F" by auto
wenzelm@53015:   thus "eventually (\<lambda>x. (x)\<^sub>F i \<in> S) F"
immler@51105:     by eventually_elim (insert `a i \<in> S`, force simp: Pi'_iff split: split_if_asm)
immler@51105: qed
immler@51105: 
immler@51105: lemma continuous_proj:
wenzelm@53015:   shows "continuous_on s (\<lambda>x. (x)\<^sub>F i)"
hoelzl@51641:   unfolding continuous_on_def by (safe intro!: tendsto_proj tendsto_ident_at)
immler@51105: 
immler@51105: instance finmap :: (type, first_countable_topology) first_countable_topology
immler@51105: proof
wenzelm@53015:   fix x::"'a\<Rightarrow>\<^sub>F'b"
immler@51105:   have "\<forall>i. \<exists>A. countable A \<and> (\<forall>a\<in>A. x i \<in> a) \<and> (\<forall>a\<in>A. open a) \<and>
immler@51105:     (\<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")
immler@51105:   proof
immler@51105:     fix i from first_countable_basis_Int_stableE[of "x i"] guess A .
immler@51105:     thus "?th i" by (intro exI[where x=A]) simp
immler@51105:   qed
immler@51105:   then guess A unfolding choice_iff .. note A = this
immler@51105:   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
immler@51105:   have A_notempty: "\<And>i. i \<in> domain x \<Longrightarrow> A i \<noteq> {}" using open_sub[of _ "\<lambda>_. UNIV"] by auto
wenzelm@53015:   let ?A = "(\<lambda>f. Pi' (domain x) f) ` (Pi\<^sub>E (domain x) A)"
wenzelm@53015:   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))"
hoelzl@51473:   proof (rule first_countableI[where A="?A"], safe)
immler@51105:     show "countable ?A" using A by (simp add: countable_PiE)
immler@51105:   next
wenzelm@53015:     fix S::"('a \<Rightarrow>\<^sub>F 'b) set" assume "open S" "x \<in> S"
immler@51105:     thus "\<exists>a\<in>?A. a \<subseteq> S" unfolding open_finmap_def
immler@51105:     proof (induct rule: generate_topology.induct)
immler@51105:       case UNIV thus ?case by (auto simp add: ex_in_conv PiE_eq_empty_iff A_notempty)
immler@51105:     next
immler@51105:       case (Int a b)
immler@51105:       then obtain f g where
wenzelm@53015:         "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"
immler@51105:         by auto
immler@51105:       thus ?case using A
immler@51105:         by (auto simp: Pi'_iff PiE_iff extensional_def Int_stable_def
immler@51105:             intro!: bexI[where x="\<lambda>i. f i \<inter> g i"])
immler@51105:     next
immler@51105:       case (UN B)
immler@51105:       then obtain b where "x \<in> b" "b \<in> B" by auto
immler@51105:       hence "\<exists>a\<in>?A. a \<subseteq> b" using UN by simp
immler@51105:       thus ?case using `b \<in> B` by blast
immler@51105:     next
immler@51105:       case (Basis s)
immler@51105:       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
wenzelm@53015:       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)"
immler@51105:         using open_sub[of _ b] by auto
immler@51105:       then obtain b'
wenzelm@53015:         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)"
immler@51105:           unfolding choice_iff by auto
immler@51105:       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"
immler@51105:         by (auto simp: Pi'_iff intro!: Pi'_mono)
immler@51105:       thus ?case using xs
immler@51105:         by (intro bexI[where x="Pi' a b'"])
immler@51105:           (auto simp: Pi'_iff intro!: image_eqI[where x="restrict b' (domain x)"])
immler@51105:     qed
immler@51105:   qed (insert A,auto simp: PiE_iff intro!: open_Pi'I)
immler@51105: qed
immler@51105: 
immler@50088: subsection {* Metric Space of Finite Maps *}
immler@50088: 
immler@50088: instantiation finmap :: (type, metric_space) metric_space
immler@50088: begin
immler@50088: 
immler@50088: definition dist_finmap where
wenzelm@53015:   "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)"
immler@50088: 
wenzelm@53015: lemma finite_proj_image': "x \<notin> domain P \<Longrightarrow> finite ((P)\<^sub>F ` S)"
immler@51104:   by (rule finite_subset[of _ "proj P ` (domain P \<inter> S \<union> {x})"]) auto
immler@51104: 
wenzelm@53015: lemma finite_proj_image: "finite ((P)\<^sub>F ` S)"
immler@51104:  by (cases "\<exists>x. x \<notin> domain P") (auto intro: finite_proj_image' finite_subset[where B="domain P"])
immler@51104: 
wenzelm@53015: lemma finite_proj_diag: "finite ((\<lambda>i. d ((P)\<^sub>F i) ((Q)\<^sub>F i)) ` S)"
immler@50088: proof -
wenzelm@53015:   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
wenzelm@53015:   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
immler@51104:   moreover have "finite \<dots>" using finite_proj_image[of P S] finite_proj_image[of Q S]
immler@51104:     by (intro finite_cartesian_product) simp_all
immler@51104:   ultimately show ?thesis by (simp add: finite_subset)
immler@50088: qed
immler@50088: 
immler@51104: lemma dist_le_1_imp_domain_eq:
immler@51104:   shows "dist P Q < 1 \<Longrightarrow> domain P = domain Q"
immler@51104:   by (simp add: dist_finmap_def finite_proj_diag split: split_if_asm)
immler@51104: 
immler@50088: lemma dist_proj:
wenzelm@53015:   shows "dist ((x)\<^sub>F i) ((y)\<^sub>F i) \<le> dist x y"
immler@50088: proof -
immler@51104:   have "dist (x i) (y i) \<le> Max (range (\<lambda>i. dist (x i) (y i)))"
immler@51104:     by (simp add: Max_ge_iff finite_proj_diag)
immler@51104:   also have "\<dots> \<le> dist x y" by (simp add: dist_finmap_def)
immler@51104:   finally show ?thesis .
immler@51104: qed
immler@51104: 
immler@51104: lemma dist_finmap_lessI:
immler@51105:   assumes "domain P = domain Q"
immler@51105:   assumes "0 < e"
immler@51105:   assumes "\<And>i. i \<in> domain P \<Longrightarrow> dist (P i) (Q i) < e"
immler@51104:   shows "dist P Q < e"
immler@51104: proof -
immler@51104:   have "dist P Q = Max (range (\<lambda>i. dist (P i) (Q i)))"
immler@51104:     using assms by (simp add: dist_finmap_def finite_proj_diag)
immler@51104:   also have "\<dots> < e"
immler@51104:   proof (subst Max_less_iff, safe)
immler@51105:     fix i
wenzelm@53015:     show "dist ((P)\<^sub>F i) ((Q)\<^sub>F i) < e" using assms
immler@51105:       by (cases "i \<in> domain P") simp_all
immler@51104:   qed (simp add: finite_proj_diag)
immler@51104:   finally show ?thesis .
immler@50088: qed
immler@50088: 
immler@50088: instance
immler@50088: proof
wenzelm@53015:   fix S::"('a \<Rightarrow>\<^sub>F 'b) set"
immler@51105:   show "open S = (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)" (is "_ = ?od")
immler@51105:   proof
immler@51105:     assume "open S"
immler@51105:     thus ?od
immler@51105:       unfolding open_finmap_def
immler@51105:     proof (induct rule: generate_topology.induct)
immler@51105:       case UNIV thus ?case by (auto intro: zero_less_one)
immler@51105:     next
immler@51105:       case (Int a b)
immler@51105:       show ?case
immler@51105:       proof safe
immler@51105:         fix x assume x: "x \<in> a" "x \<in> b"
immler@51105:         with Int x obtain e1 e2 where
immler@51105:           "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
immler@51105:         thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> a \<inter> b"
immler@51105:           by (auto intro!: exI[where x="min e1 e2"])
immler@51105:       qed
immler@51105:     next
immler@51105:       case (UN K)
immler@51105:       show ?case
immler@51105:       proof safe
wenzelm@53374:         fix x X assume "x \<in> X" and X: "X \<in> K"
wenzelm@53374:         with UN obtain e where "e>0" "\<And>y. dist y x < e \<longrightarrow> y \<in> X" by force
wenzelm@53374:         with X show "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> \<Union>K" by auto
immler@51105:       qed
immler@51105:     next
immler@51105:       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
immler@51105:       show ?case
immler@51105:       proof safe
immler@51105:         fix x assume "x \<in> s"
immler@51105:         hence [simp]: "finite a" and a_dom: "a = domain x" using s by (auto simp: Pi'_iff)
immler@51105:         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)"
immler@51105:           using b `x \<in> s` by atomize_elim (intro bchoice, auto simp: open_dist s)
immler@51105:         hence in_b: "\<And>i y. i \<in> a \<Longrightarrow> dist y (proj x i) < es i \<Longrightarrow> y \<in> b i" by auto
immler@51105:         show "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> s"
immler@51105:         proof (cases, rule, safe)
immler@51105:           assume "a \<noteq> {}"
immler@51105:           show "0 < min 1 (Min (es ` a))" using es by (auto simp: `a \<noteq> {}`)
immler@51105:           fix y assume d: "dist y x < min 1 (Min (es ` a))"
immler@51105:           show "y \<in> s" unfolding s
immler@51105:           proof
immler@51105:             show "domain y = a" using d s `a \<noteq> {}` by (auto simp: dist_le_1_imp_domain_eq a_dom)
wenzelm@53374:             fix i assume i: "i \<in> a"
wenzelm@53015:             hence "dist ((y)\<^sub>F i) ((x)\<^sub>F i) < es i" using d
immler@51105:               by (auto simp: dist_finmap_def `a \<noteq> {}` intro!: le_less_trans[OF dist_proj])
wenzelm@53374:             with i show "y i \<in> b i" by (rule in_b)
immler@51105:           qed
immler@51105:         next
immler@51105:           assume "\<not>a \<noteq> {}"
immler@51105:           thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> s"
immler@51105:             using s `x \<in> s` by (auto simp: Pi'_def dist_le_1_imp_domain_eq intro!: exI[where x=1])
immler@51105:         qed
immler@51105:       qed
immler@51105:     qed
immler@51105:   next
immler@51105:     assume "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
immler@51105:     then obtain e where e_pos: "\<And>x. x \<in> S \<Longrightarrow> e x > 0" and
immler@51105:       e_in:  "\<And>x y . x \<in> S \<Longrightarrow> dist y x < e x \<Longrightarrow> y \<in> S"
immler@51105:       unfolding bchoice_iff
immler@51105:       by auto
immler@51105:     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))}"
immler@51105:     proof safe
immler@51105:       fix x assume "x \<in> S"
immler@51105:       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))}"
immler@51105:         using e_pos by (auto intro!: exI[where x="Pi' (domain x) (\<lambda>i. ball (x i) (e x))"])
immler@51105:     next
immler@51105:       fix x y
immler@51105:       assume "y \<in> S"
immler@51105:       moreover
immler@51105:       assume "x \<in> (\<Pi>' i\<in>domain y. ball (y i) (e y))"
immler@51105:       hence "dist x y < e y" using e_pos `y \<in> S`
immler@51105:         by (auto simp: dist_finmap_def Pi'_iff finite_proj_diag dist_commute)
immler@51105:       ultimately show "x \<in> S" by (rule e_in)
immler@51105:     qed
immler@51105:     also have "open \<dots>"
immler@51105:       unfolding open_finmap_def
immler@51105:       by (intro generate_topology.UN) (auto intro: generate_topology.Basis)
immler@51105:     finally show "open S" .
immler@51105:   qed
immler@50088: next
wenzelm@53015:   fix P Q::"'a \<Rightarrow>\<^sub>F 'b"
immler@51104:   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))"
haftmann@51489:     by (auto intro: Max_in Max_eqI)
immler@50088:   show "dist P Q = 0 \<longleftrightarrow> P = Q"
immler@51104:     by (auto simp: finmap_eq_iff dist_finmap_def Max_ge_iff finite_proj_diag Max_eq_iff
hoelzl@56633:         add_nonneg_eq_0_iff
immler@51104:       intro!: Max_eqI image_eqI[where x=undefined])
immler@50088: next
wenzelm@53015:   fix P Q R::"'a \<Rightarrow>\<^sub>F 'b"
wenzelm@53015:   let ?dists = "\<lambda>P Q i. dist ((P)\<^sub>F i) ((Q)\<^sub>F i)"
immler@51104:   let ?dpq = "?dists P Q" and ?dpr = "?dists P R" and ?dqr = "?dists Q R"
immler@51104:   let ?dom = "\<lambda>P Q. (if domain P = domain Q then 0 else 1::real)"
immler@51104:   have "dist P Q = Max (range ?dpq) + ?dom P Q"
immler@51104:     by (simp add: dist_finmap_def)
immler@51104:   also obtain t where "t \<in> range ?dpq" "t = Max (range ?dpq)" by (simp add: finite_proj_diag)
immler@51104:   then obtain i where "Max (range ?dpq) = ?dpq i" by auto
immler@51104:   also have "?dpq i \<le> ?dpr i + ?dqr i" by (rule dist_triangle2)
immler@51104:   also have "?dpr i \<le> Max (range ?dpr)" by (simp add: finite_proj_diag)
immler@51104:   also have "?dqr i \<le> Max (range ?dqr)" by (simp add: finite_proj_diag)
immler@51104:   also have "?dom P Q \<le> ?dom P R + ?dom Q R" by simp
immler@51104:   finally show "dist P Q \<le> dist P R + dist Q R" by (simp add: dist_finmap_def ac_simps)
immler@50088: qed
immler@50088: 
immler@50088: end
immler@50088: 
immler@50088: subsection {* Complete Space of Finite Maps *}
immler@50088: 
immler@50088: lemma tendsto_finmap:
wenzelm@53015:   fixes f::"nat \<Rightarrow> ('i \<Rightarrow>\<^sub>F ('a::metric_space))"
immler@50088:   assumes ind_f:  "\<And>n. domain (f n) = domain g"
immler@50088:   assumes proj_g:  "\<And>i. i \<in> domain g \<Longrightarrow> (\<lambda>n. (f n) i) ----> g i"
immler@50088:   shows "f ----> g"
immler@51104:   unfolding tendsto_iff
immler@51104: proof safe
immler@51104:   fix e::real assume "0 < e"
wenzelm@53015:   let ?dists = "\<lambda>x i. dist ((f x)\<^sub>F i) ((g)\<^sub>F i)"
immler@51104:   have "eventually (\<lambda>x. \<forall>i\<in>domain g. ?dists x i < e) sequentially"
immler@51104:     using finite_domain[of g] proj_g
immler@51104:   proof induct
immler@51104:     case (insert i G)
immler@51104:     with `0 < e` have "eventually (\<lambda>x. ?dists x i < e) sequentially" by (auto simp add: tendsto_iff)
immler@51104:     moreover
wenzelm@53015:     from insert have "eventually (\<lambda>x. \<forall>i\<in>G. dist ((f x)\<^sub>F i) ((g)\<^sub>F i) < e) sequentially" by simp
immler@51104:     ultimately show ?case by eventually_elim auto
immler@51104:   qed simp
immler@51104:   thus "eventually (\<lambda>x. dist (f x) g < e) sequentially"
immler@51104:     by eventually_elim (auto simp add: dist_finmap_def finite_proj_diag ind_f `0 < e`)
immler@51104: qed
immler@50088: 
immler@50088: instance finmap :: (type, complete_space) complete_space
immler@50088: proof
wenzelm@53015:   fix P::"nat \<Rightarrow> 'a \<Rightarrow>\<^sub>F 'b"
immler@50088:   assume "Cauchy P"
immler@50088:   then obtain Nd where Nd: "\<And>n. n \<ge> Nd \<Longrightarrow> dist (P n) (P Nd) < 1"
immler@50088:     by (force simp: cauchy)
immler@50088:   def d \<equiv> "domain (P Nd)"
immler@50088:   with Nd have dim: "\<And>n. n \<ge> Nd \<Longrightarrow> domain (P n) = d" using dist_le_1_imp_domain_eq by auto
immler@50088:   have [simp]: "finite d" unfolding d_def by simp
immler@50088:   def p \<equiv> "\<lambda>i n. (P n) i"
immler@50088:   def q \<equiv> "\<lambda>i. lim (p i)"
immler@50088:   def Q \<equiv> "finmap_of d q"
immler@50088:   have q: "\<And>i. i \<in> d \<Longrightarrow> q i = Q i" by (auto simp add: Q_def Abs_finmap_inverse)
immler@50088:   {
immler@50088:     fix i assume "i \<in> d"
immler@50088:     have "Cauchy (p i)" unfolding cauchy p_def
immler@50088:     proof safe
immler@50088:       fix e::real assume "0 < e"
immler@50088:       with `Cauchy P` obtain N where N: "\<And>n. n\<ge>N \<Longrightarrow> dist (P n) (P N) < min e 1"
immler@50088:         by (force simp: cauchy min_def)
immler@50088:       hence "\<And>n. n \<ge> N \<Longrightarrow> domain (P n) = domain (P N)" using dist_le_1_imp_domain_eq by auto
immler@50088:       with dim have dim: "\<And>n. n \<ge> N \<Longrightarrow> domain (P n) = d" by (metis nat_le_linear)
immler@50088:       show "\<exists>N. \<forall>n\<ge>N. dist ((P n) i) ((P N) i) < e"
immler@50088:       proof (safe intro!: exI[where x="N"])
immler@50088:         fix n assume "N \<le> n" have "N \<le> N" by simp
immler@50088:         have "dist ((P n) i) ((P N) i) \<le> dist (P n) (P N)"
immler@50088:           using dim[OF `N \<le> n`]  dim[OF `N \<le> N`] `i \<in> d`
immler@50088:           by (auto intro!: dist_proj)
immler@50088:         also have "\<dots> < e" using N[OF `N \<le> n`] by simp
immler@50088:         finally show "dist ((P n) i) ((P N) i) < e" .
immler@50088:       qed
immler@50088:     qed
immler@50088:     hence "convergent (p i)" by (metis Cauchy_convergent_iff)
immler@50088:     hence "p i ----> q i" unfolding q_def convergent_def by (metis limI)
immler@50088:   } note p = this
immler@50088:   have "P ----> Q"
immler@50088:   proof (rule metric_LIMSEQ_I)
immler@50088:     fix e::real assume "0 < e"
immler@51104:     have "\<exists>ni. \<forall>i\<in>d. \<forall>n\<ge>ni i. dist (p i n) (q i) < e"
immler@50088:     proof (safe intro!: bchoice)
immler@50088:       fix i assume "i \<in> d"
immler@51104:       from p[OF `i \<in> d`, THEN metric_LIMSEQ_D, OF `0 < e`]
immler@51104:       show "\<exists>no. \<forall>n\<ge>no. dist (p i n) (q i) < e" .
immler@50088:     qed then guess ni .. note ni = this
immler@50088:     def N \<equiv> "max Nd (Max (ni ` d))"
immler@50088:     show "\<exists>N. \<forall>n\<ge>N. dist (P n) Q < e"
immler@50088:     proof (safe intro!: exI[where x="N"])
immler@50088:       fix n assume "N \<le> n"
immler@51104:       hence dom: "domain (P n) = d" "domain Q = d" "domain (P n) = domain Q"
immler@50088:         using dim by (simp_all add: N_def Q_def dim_def Abs_finmap_inverse)
immler@51104:       show "dist (P n) Q < e"
immler@51104:       proof (rule dist_finmap_lessI[OF dom(3) `0 < e`])
immler@51104:         fix i
immler@51104:         assume "i \<in> domain (P n)"
immler@51104:         hence "ni i \<le> Max (ni ` d)" using dom by simp
immler@50088:         also have "\<dots> \<le> N" by (simp add: N_def)
wenzelm@53015:         finally show "dist ((P n)\<^sub>F i) ((Q)\<^sub>F i) < e" using ni `i \<in> domain (P n)` `N \<le> n` dom
immler@51104:           by (auto simp: p_def q N_def less_imp_le)
immler@50088:       qed
immler@50088:     qed
immler@50088:   qed
immler@50088:   thus "convergent P" by (auto simp: convergent_def)
immler@50088: qed
immler@50088: 
immler@51105: subsection {* Second Countable Space of Finite Maps *}
immler@50088: 
immler@51105: instantiation finmap :: (countable, second_countable_topology) second_countable_topology
immler@50088: begin
immler@50088: 
immler@51106: definition basis_proj::"'b set set"
immler@51106:   where "basis_proj = (SOME B. countable B \<and> topological_basis B)"
immler@51106: 
immler@51106: lemma countable_basis_proj: "countable basis_proj" and basis_proj: "topological_basis basis_proj"
immler@51106:   unfolding basis_proj_def by (intro is_basis countable_basis)+
immler@51106: 
wenzelm@53015: definition basis_finmap::"('a \<Rightarrow>\<^sub>F 'b) set set"
immler@51106:   where "basis_finmap = {Pi' I S|I S. finite I \<and> (\<forall>i \<in> I. S i \<in> basis_proj)}"
immler@50245: 
immler@50245: lemma in_basis_finmapI:
immler@51106:   assumes "finite I" assumes "\<And>i. i \<in> I \<Longrightarrow> S i \<in> basis_proj"
immler@50245:   shows "Pi' I S \<in> basis_finmap"
immler@50245:   using assms unfolding basis_finmap_def by auto
immler@50245: 
immler@50245: lemma basis_finmap_eq:
immler@51106:   assumes "basis_proj \<noteq> {}"
wenzelm@53015:   shows "basis_finmap = (\<lambda>f. Pi' (domain f) (\<lambda>i. from_nat_into basis_proj ((f)\<^sub>F i))) `
wenzelm@53015:     (UNIV::('a \<Rightarrow>\<^sub>F nat) set)" (is "_ = ?f ` _")
immler@50245:   unfolding basis_finmap_def
immler@50245: proof safe
immler@50245:   fix I::"'a set" and S::"'a \<Rightarrow> 'b set"
immler@51106:   assume "finite I" "\<forall>i\<in>I. S i \<in> basis_proj"
immler@51106:   hence "Pi' I S = ?f (finmap_of I (\<lambda>x. to_nat_on basis_proj (S x)))"
immler@51106:     by (force simp: Pi'_def countable_basis_proj)
immler@50245:   thus "Pi' I S \<in> range ?f" by simp
immler@51106: next
wenzelm@53015:   fix x and f::"'a \<Rightarrow>\<^sub>F nat"
wenzelm@56222:   show "\<exists>I S. (\<Pi>' i\<in>domain f. from_nat_into basis_proj ((f)\<^sub>F i)) = Pi' I S \<and>
wenzelm@56222:     finite I \<and> (\<forall>i\<in>I. S i \<in> basis_proj)"
immler@51106:     using assms by (auto intro: from_nat_into)
immler@51106: qed
immler@51106: 
immler@51106: lemma basis_finmap_eq_empty: "basis_proj = {} \<Longrightarrow> basis_finmap = {Pi' {} undefined}"
immler@51106:   by (auto simp: Pi'_iff basis_finmap_def)
immler@50088: 
immler@50245: lemma countable_basis_finmap: "countable basis_finmap"
immler@51106:   by (cases "basis_proj = {}") (auto simp: basis_finmap_eq basis_finmap_eq_empty)
immler@50088: 
immler@50088: lemma finmap_topological_basis:
immler@50245:   "topological_basis basis_finmap"
immler@50088: proof (subst topological_basis_iff, safe)
immler@50245:   fix B' assume "B' \<in> basis_finmap"
immler@50245:   thus "open B'"
immler@51106:     by (auto intro!: open_Pi'I topological_basis_open[OF basis_proj]
immler@50245:       simp: topological_basis_def basis_finmap_def Let_def)
immler@50088: next
wenzelm@53015:   fix O'::"('a \<Rightarrow>\<^sub>F 'b) set" and x
immler@51105:   assume O': "open O'" "x \<in> O'"
immler@51105:   then obtain a where a:
immler@51105:     "x \<in> Pi' (domain x) a" "Pi' (domain x) a \<subseteq> O'" "\<And>i. i\<in>domain x \<Longrightarrow> open (a i)"
immler@51105:     unfolding open_finmap_def
immler@51105:   proof (atomize_elim, induct rule: generate_topology.induct)
immler@51105:     case (Int a b)
immler@51105:     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))"
immler@51105:     from Int obtain f g where "?p a f" "?p b g" by auto
immler@51105:     thus ?case by (force intro!: exI[where x="\<lambda>i. f i \<inter> g i"] simp: Pi'_def)
immler@51105:   next
immler@51105:     case (UN k)
immler@51105:     then obtain kk a where "x \<in> kk" "kk \<in> k" "x \<in> Pi' (domain x) a" "Pi' (domain x) a \<subseteq> kk"
immler@51105:       "\<And>i. i\<in>domain x \<Longrightarrow> open (a i)"
immler@51105:       by force
immler@51105:     thus ?case by blast
immler@51105:   qed (auto simp: Pi'_def)
immler@50088:   have "\<exists>B.
immler@51106:     (\<forall>i\<in>domain x. x i \<in> B i \<and> B i \<subseteq> a i \<and> B i \<in> basis_proj)"
immler@50088:   proof (rule bchoice, safe)
immler@50088:     fix i assume "i \<in> domain x"
immler@51105:     hence "open (a i)" "x i \<in> a i" using a by auto
immler@51106:     from topological_basisE[OF basis_proj this] guess b' .
immler@51106:     thus "\<exists>y. x i \<in> y \<and> y \<subseteq> a i \<and> y \<in> basis_proj" by auto
immler@50088:   qed
immler@50088:   then guess B .. note B = this
immler@50245:   def B' \<equiv> "Pi' (domain x) (\<lambda>i. (B i)::'b set)"
immler@51105:   have "B' \<subseteq> Pi' (domain x) a" using B by (auto intro!: Pi'_mono simp: B'_def)
immler@51105:   also note `\<dots> \<subseteq> O'`
immler@51105:   finally show "\<exists>B'\<in>basis_finmap. x \<in> B' \<and> B' \<subseteq> O'" using B
immler@51105:     by (auto intro!: bexI[where x=B'] Pi'_mono in_basis_finmapI simp: B'_def)
immler@50088: qed
immler@50088: 
immler@50088: lemma range_enum_basis_finmap_imp_open:
immler@50245:   assumes "x \<in> basis_finmap"
immler@50088:   shows "open x"
immler@50088:   using finmap_topological_basis assms by (auto simp: topological_basis_def)
immler@50088: 
hoelzl@51343: instance proof qed (blast intro: finmap_topological_basis countable_basis_finmap topological_basis_imp_subbasis)
immler@50088: 
immler@50088: end
immler@50088: 
immler@51105: subsection {* Polish Space of Finite Maps *}
immler@51105: 
immler@51105: instance finmap :: (countable, polish_space) polish_space proof qed
immler@51105: 
immler@51105: 
immler@50088: subsection {* Product Measurable Space of Finite Maps *}
immler@50088: 
immler@50088: definition "PiF I M \<equiv>
hoelzl@50124:   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))}"
immler@50088: 
immler@50088: abbreviation
wenzelm@53015:   "Pi\<^sub>F I M \<equiv> PiF I M"
immler@50088: 
immler@50088: syntax
immler@50088:   "_PiF" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3PIF _:_./ _)" 10)
immler@50088: 
immler@50088: syntax (xsymbols)
wenzelm@53015:   "_PiF" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^sub>F _\<in>_./ _)"  10)
immler@50088: 
immler@50088: syntax (HTML output)
wenzelm@53015:   "_PiF" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^sub>F _\<in>_./ _)"  10)
immler@50088: 
immler@50088: translations
immler@50088:   "PIF x:I. M" == "CONST PiF I (%x. M)"
immler@50088: 
immler@50088: 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>
immler@50088:     Pow (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j)))"
immler@50244:   by (auto simp: Pi'_def) (blast dest: sets.sets_into_space)
immler@50088: 
immler@50088: lemma space_PiF: "space (PiF I M) = (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j)))"
immler@50088:   unfolding PiF_def using PiF_gen_subset by (rule space_measure_of)
immler@50088: 
immler@50088: lemma sets_PiF:
immler@50088:   "sets (PiF I M) = sigma_sets (\<Union>J \<in> I. (\<Pi>' j\<in>J. space (M j)))
immler@50088:     {(\<Pi>' j\<in>J. X j) |X J. J \<in> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}"
immler@50088:   unfolding PiF_def using PiF_gen_subset by (rule sets_measure_of)
immler@50088: 
immler@50088: lemma sets_PiF_singleton:
immler@50088:   "sets (PiF {I} M) = sigma_sets (\<Pi>' j\<in>I. space (M j))
immler@50088:     {(\<Pi>' j\<in>I. X j) |X. X \<in> (\<Pi> j\<in>I. sets (M j))}"
immler@50088:   unfolding sets_PiF by simp
immler@50088: 
immler@50088: lemma in_sets_PiFI:
immler@50088:   assumes "X = (Pi' J S)" "J \<in> I" "\<And>i. i\<in>J \<Longrightarrow> S i \<in> sets (M i)"
immler@50088:   shows "X \<in> sets (PiF I M)"
immler@50088:   unfolding sets_PiF
immler@50088:   using assms by blast
immler@50088: 
immler@50088: lemma product_in_sets_PiFI:
immler@50088:   assumes "J \<in> I" "\<And>i. i\<in>J \<Longrightarrow> S i \<in> sets (M i)"
immler@50088:   shows "(Pi' J S) \<in> sets (PiF I M)"
immler@50088:   unfolding sets_PiF
immler@50088:   using assms by blast
immler@50088: 
immler@50088: lemma singleton_space_subset_in_sets:
immler@50088:   fixes J
immler@50088:   assumes "J \<in> I"
immler@50088:   assumes "finite J"
immler@50088:   shows "space (PiF {J} M) \<in> sets (PiF I M)"
immler@50088:   using assms
immler@50088:   by (intro in_sets_PiFI[where J=J and S="\<lambda>i. space (M i)"])
immler@50088:       (auto simp: product_def space_PiF)
immler@50088: 
immler@50088: lemma singleton_subspace_set_in_sets:
immler@50088:   assumes A: "A \<in> sets (PiF {J} M)"
immler@50088:   assumes "finite J"
immler@50088:   assumes "J \<in> I"
immler@50088:   shows "A \<in> sets (PiF I M)"
immler@50088:   using A[unfolded sets_PiF]
immler@50088:   apply (induct A)
immler@50088:   unfolding sets_PiF[symmetric] unfolding space_PiF[symmetric]
immler@50088:   using assms
immler@50088:   by (auto intro: in_sets_PiFI intro!: singleton_space_subset_in_sets)
immler@50088: 
hoelzl@50124: lemma finite_measurable_singletonI:
immler@50088:   assumes "finite I"
immler@50088:   assumes "\<And>J. J \<in> I \<Longrightarrow> finite J"
immler@50088:   assumes MN: "\<And>J. J \<in> I \<Longrightarrow> A \<in> measurable (PiF {J} M) N"
immler@50088:   shows "A \<in> measurable (PiF I M) N"
immler@50088:   unfolding measurable_def
immler@50088: proof safe
immler@50088:   fix y assume "y \<in> sets N"
immler@50088:   have "A -` y \<inter> space (PiF I M) = (\<Union>J\<in>I. A -` y \<inter> space (PiF {J} M))"
immler@50088:     by (auto simp: space_PiF)
immler@50088:   also have "\<dots> \<in> sets (PiF I M)"
immler@50088:   proof
immler@50088:     show "finite I" by fact
immler@50088:     fix J assume "J \<in> I"
immler@50088:     with assms have "finite J" by simp
immler@50088:     show "A -` y \<inter> space (PiF {J} M) \<in> sets (PiF I M)"
immler@50088:       by (rule singleton_subspace_set_in_sets[OF measurable_sets[OF assms(3)]]) fact+
immler@50088:   qed
immler@50088:   finally show "A -` y \<inter> space (PiF I M) \<in> sets (PiF I M)" .
immler@50088: next
immler@50088:   fix x assume "x \<in> space (PiF I M)" thus "A x \<in> space N"
immler@50088:     using MN[of "domain x"]
immler@50088:     by (auto simp: space_PiF measurable_space Pi'_def)
immler@50088: qed
immler@50088: 
hoelzl@50124: lemma countable_finite_comprehension:
immler@50088:   fixes f :: "'a::countable set \<Rightarrow> _"
immler@50088:   assumes "\<And>s. P s \<Longrightarrow> finite s"
immler@50088:   assumes "\<And>s. P s \<Longrightarrow> f s \<in> sets M"
immler@50088:   shows "\<Union>{f s|s. P s} \<in> sets M"
immler@50088: proof -
immler@50088:   have "\<Union>{f s|s. P s} = (\<Union>n::nat. let s = set (from_nat n) in if P s then f s else {})"
immler@50088:   proof safe
wenzelm@53374:     fix x X s assume *: "x \<in> f s" "P s"
wenzelm@53374:     with assms obtain l where "s = set l" using finite_list by blast
wenzelm@53374:     with * show "x \<in> (\<Union>n. let s = set (from_nat n) in if P s then f s else {})" using `P s`
immler@50088:       by (auto intro!: exI[where x="to_nat l"])
immler@50088:   next
immler@50088:     fix x n assume "x \<in> (let s = set (from_nat n) in if P s then f s else {})"
immler@50088:     thus "x \<in> \<Union>{f s|s. P s}" using assms by (auto simp: Let_def split: split_if_asm)
immler@50088:   qed
immler@50088:   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
immler@50088:   also have "\<dots> \<in> sets M" using assms by (auto simp: Let_def)
immler@50088:   finally show ?thesis .
immler@50088: qed
immler@50088: 
immler@50088: lemma space_subset_in_sets:
immler@50088:   fixes J::"'a::countable set set"
immler@50088:   assumes "J \<subseteq> I"
immler@50088:   assumes "\<And>j. j \<in> J \<Longrightarrow> finite j"
immler@50088:   shows "space (PiF J M) \<in> sets (PiF I M)"
immler@50088: proof -
immler@50088:   have "space (PiF J M) = \<Union>{space (PiF {j} M)|j. j \<in> J}"
immler@50088:     unfolding space_PiF by blast
immler@50088:   also have "\<dots> \<in> sets (PiF I M)" using assms
immler@50088:     by (intro countable_finite_comprehension) (auto simp: singleton_space_subset_in_sets)
immler@50088:   finally show ?thesis .
immler@50088: qed
immler@50088: 
immler@50088: lemma subspace_set_in_sets:
immler@50088:   fixes J::"'a::countable set set"
immler@50088:   assumes A: "A \<in> sets (PiF J M)"
immler@50088:   assumes "J \<subseteq> I"
immler@50088:   assumes "\<And>j. j \<in> J \<Longrightarrow> finite j"
immler@50088:   shows "A \<in> sets (PiF I M)"
immler@50088:   using A[unfolded sets_PiF]
immler@50088:   apply (induct A)
immler@50088:   unfolding sets_PiF[symmetric] unfolding space_PiF[symmetric]
immler@50088:   using assms
immler@50088:   by (auto intro: in_sets_PiFI intro!: space_subset_in_sets)
immler@50088: 
hoelzl@50124: lemma countable_measurable_PiFI:
immler@50088:   fixes I::"'a::countable set set"
immler@50088:   assumes MN: "\<And>J. J \<in> I \<Longrightarrow> finite J \<Longrightarrow> A \<in> measurable (PiF {J} M) N"
immler@50088:   shows "A \<in> measurable (PiF I M) N"
immler@50088:   unfolding measurable_def
immler@50088: proof safe
immler@50088:   fix y assume "y \<in> sets N"
immler@50088:   have "A -` y = (\<Union>{A -` y \<inter> {x. domain x = J}|J. finite J})" by auto
wenzelm@53015:   { fix x::"'a \<Rightarrow>\<^sub>F 'b"
immler@50088:     from finite_list[of "domain x"] obtain xs where "set xs = domain x" by auto
immler@50245:     hence "\<exists>n. domain x = set (from_nat n)"
immler@50245:       by (intro exI[where x="to_nat xs"]) auto }
immler@50245:   hence "A -` y \<inter> space (PiF I M) = (\<Union>n. A -` y \<inter> space (PiF ({set (from_nat n)}\<inter>I) M))"
immler@50245:     by (auto simp: space_PiF Pi'_def)
immler@50088:   also have "\<dots> \<in> sets (PiF I M)"
immler@50244:     apply (intro sets.Int sets.countable_nat_UN subsetI, safe)
immler@50088:     apply (case_tac "set (from_nat i) \<in> I")
immler@50088:     apply simp_all
immler@50088:     apply (rule singleton_subspace_set_in_sets[OF measurable_sets[OF MN]])
immler@50088:     using assms `y \<in> sets N`
immler@50088:     apply (auto simp: space_PiF)
immler@50088:     done
immler@50088:   finally show "A -` y \<inter> space (PiF I M) \<in> sets (PiF I M)" .
immler@50088: next
immler@50088:   fix x assume "x \<in> space (PiF I M)" thus "A x \<in> space N"
immler@50088:     using MN[of "domain x"] by (auto simp: space_PiF measurable_space Pi'_def)
immler@50088: qed
immler@50088: 
immler@50088: lemma measurable_PiF:
immler@50088:   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))"
immler@50088:   assumes S: "\<And>J S. J \<in> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> S i \<in> sets (M i)) \<Longrightarrow>
immler@50088:     f -` (Pi' J S) \<inter> space N \<in> sets N"
immler@50088:   shows "f \<in> measurable N (PiF I M)"
immler@50088:   unfolding PiF_def
immler@50088:   using PiF_gen_subset
immler@50088:   apply (rule measurable_measure_of)
immler@50088:   using f apply force
immler@50088:   apply (insert S, auto)
immler@50088:   done
immler@50088: 
hoelzl@50124: lemma restrict_sets_measurable:
immler@50088:   assumes A: "A \<in> sets (PiF I M)" and "J \<subseteq> I"
immler@50088:   shows "A \<inter> {m. domain m \<in> J} \<in> sets (PiF J M)"
immler@50088:   using A[unfolded sets_PiF]
hoelzl@50124: proof (induct A)
hoelzl@50124:   case (Basic a)
immler@50088:   then obtain K S where S: "a = Pi' K S" "K \<in> I" "(\<forall>i\<in>K. S i \<in> sets (M i))"
immler@50088:     by auto
hoelzl@50124:   show ?case
immler@50088:   proof cases
immler@50088:     assume "K \<in> J"
immler@50088:     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
immler@50088:       by (auto intro!: exI[where x=K] exI[where x=S] simp: Pi'_def)
immler@50088:     also have "\<dots> \<subseteq> sets (PiF J M)" unfolding sets_PiF by auto
immler@50088:     finally show ?thesis .
immler@50088:   next
immler@50088:     assume "K \<notin> J"
immler@50088:     hence "a \<inter> {m. domain m \<in> J} = {}" using S by (auto simp: Pi'_def)
immler@50088:     also have "\<dots> \<in> sets (PiF J M)" by simp
immler@50088:     finally show ?thesis .
immler@50088:   qed
immler@50088: next
hoelzl@50124:   case (Union a)
immler@50088:   have "UNION UNIV a \<inter> {m. domain m \<in> J} = (\<Union>i. (a i \<inter> {m. domain m \<in> J}))"
immler@50088:     by simp
immler@50244:   also have "\<dots> \<in> sets (PiF J M)" using Union by (intro sets.countable_nat_UN) auto
hoelzl@50124:   finally show ?case .
immler@50088: next
hoelzl@50124:   case (Compl a)
immler@50088:   have "(space (PiF I M) - a) \<inter> {m. domain m \<in> J} = (space (PiF J M) - (a \<inter> {m. domain m \<in> J}))"
immler@50088:     using `J \<subseteq> I` by (auto simp: space_PiF Pi'_def)
hoelzl@50124:   also have "\<dots> \<in> sets (PiF J M)" using Compl by auto
hoelzl@50124:   finally show ?case by (simp add: space_PiF)
hoelzl@50124: qed simp
immler@50088: 
immler@50088: lemma measurable_finmap_of:
immler@50088:   assumes f: "\<And>i. (\<exists>x \<in> space N. i \<in> J x) \<Longrightarrow> (\<lambda>x. f x i) \<in> measurable N (M i)"
immler@50088:   assumes J: "\<And>x. x \<in> space N \<Longrightarrow> J x \<in> I" "\<And>x. x \<in> space N \<Longrightarrow> finite (J x)"
immler@50088:   assumes JN: "\<And>S. {x. J x = S} \<inter> space N \<in> sets N"
immler@50088:   shows "(\<lambda>x. finmap_of (J x) (f x)) \<in> measurable N (PiF I M)"
immler@50088: proof (rule measurable_PiF)
immler@50088:   fix x assume "x \<in> space N"
immler@50088:   with J[of x] measurable_space[OF f]
immler@50088:   show "domain (finmap_of (J x) (f x)) \<in> I \<and>
immler@50088:         (\<forall>i\<in>domain (finmap_of (J x) (f x)). (finmap_of (J x) (f x)) i \<in> space (M i))"
immler@50088:     by auto
immler@50088: next
immler@50088:   fix K S assume "K \<in> I" and *: "\<And>i. i \<in> K \<Longrightarrow> S i \<in> sets (M i)"
immler@50088:   with J have eq: "(\<lambda>x. finmap_of (J x) (f x)) -` Pi' K S \<inter> space N =
immler@50088:     (if \<exists>x \<in> space N. K = J x \<and> finite K then if K = {} then {x \<in> space N. J x = K}
immler@50088:       else (\<Inter>i\<in>K. (\<lambda>x. f x i) -` S i \<inter> {x \<in> space N. J x = K}) else {})"
immler@50088:     by (auto simp: Pi'_def)
immler@50088:   have r: "{x \<in> space N. J x = K} = space N \<inter> ({x. J x = K} \<inter> space N)" by auto
immler@50088:   show "(\<lambda>x. finmap_of (J x) (f x)) -` Pi' K S \<inter> space N \<in> sets N"
immler@50088:     unfolding eq r
immler@50088:     apply (simp del: INT_simps add: )
immler@50244:     apply (intro conjI impI sets.finite_INT JN sets.Int[OF sets.top])
immler@50088:     apply simp apply assumption
immler@50088:     apply (subst Int_assoc[symmetric])
immler@50244:     apply (rule sets.Int)
immler@50088:     apply (intro measurable_sets[OF f] *) apply force apply assumption
immler@50088:     apply (intro JN)
immler@50088:     done
immler@50088: qed
immler@50088: 
immler@50088: lemma measurable_PiM_finmap_of:
immler@50088:   assumes "finite J"
wenzelm@53015:   shows "finmap_of J \<in> measurable (Pi\<^sub>M J M) (PiF {J} M)"
immler@50088:   apply (rule measurable_finmap_of)
immler@50088:   apply (rule measurable_component_singleton)
immler@50088:   apply simp
immler@50088:   apply rule
immler@50088:   apply (rule `finite J`)
immler@50088:   apply simp
immler@50088:   done
immler@50088: 
immler@50088: lemma proj_measurable_singleton:
hoelzl@50124:   assumes "A \<in> sets (M i)"
wenzelm@53015:   shows "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space (PiF {I} M) \<in> sets (PiF {I} M)"
immler@50088: proof cases
immler@50088:   assume "i \<in> I"
wenzelm@53015:   hence "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space (PiF {I} M) =
immler@50088:     Pi' I (\<lambda>x. if x = i then A else space (M x))"
immler@50244:     using sets.sets_into_space[OF ] `A \<in> sets (M i)` assms
immler@50088:     by (auto simp: space_PiF Pi'_def)
immler@50088:   thus ?thesis  using assms `A \<in> sets (M i)`
immler@50088:     by (intro in_sets_PiFI) auto
immler@50088: next
immler@50088:   assume "i \<notin> I"
wenzelm@53015:   hence "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space (PiF {I} M) =
immler@50088:     (if undefined \<in> A then space (PiF {I} M) else {})" by (auto simp: space_PiF Pi'_def)
immler@50088:   thus ?thesis by simp
immler@50088: qed
immler@50088: 
immler@50088: lemma measurable_proj_singleton:
hoelzl@50124:   assumes "i \<in> I"
wenzelm@53015:   shows "(\<lambda>x. (x)\<^sub>F i) \<in> measurable (PiF {I} M) (M i)"
hoelzl@50124:   by (unfold measurable_def, intro CollectI conjI ballI proj_measurable_singleton assms)
hoelzl@50124:      (insert `i \<in> I`, auto simp: space_PiF)
immler@50088: 
immler@50088: lemma measurable_proj_countable:
immler@50088:   fixes I::"'a::countable set set"
immler@50088:   assumes "y \<in> space (M i)"
wenzelm@53015:   shows "(\<lambda>x. if i \<in> domain x then (x)\<^sub>F i else y) \<in> measurable (PiF I M) (M i)"
immler@50088: proof (rule countable_measurable_PiFI)
immler@50088:   fix J assume "J \<in> I" "finite J"
immler@50088:   show "(\<lambda>x. if i \<in> domain x then x i else y) \<in> measurable (PiF {J} M) (M i)"
immler@50088:     unfolding measurable_def
immler@50088:   proof safe
immler@50088:     fix z assume "z \<in> sets (M i)"
immler@50088:     have "(\<lambda>x. if i \<in> domain x then x i else y) -` z \<inter> space (PiF {J} M) =
wenzelm@53015:       (\<lambda>x. if i \<in> J then (x)\<^sub>F i else y) -` z \<inter> space (PiF {J} M)"
immler@50088:       by (auto simp: space_PiF Pi'_def)
immler@50088:     also have "\<dots> \<in> sets (PiF {J} M)" using `z \<in> sets (M i)` `finite J`
immler@50088:       by (cases "i \<in> J") (auto intro!: measurable_sets[OF measurable_proj_singleton])
immler@50088:     finally show "(\<lambda>x. if i \<in> domain x then x i else y) -` z \<inter> space (PiF {J} M) \<in>
immler@50088:       sets (PiF {J} M)" .
immler@50088:   qed (insert `y \<in> space (M i)`, auto simp: space_PiF Pi'_def)
immler@50088: qed
immler@50088: 
immler@50088: lemma measurable_restrict_proj:
immler@50088:   assumes "J \<in> II" "finite J"
immler@50088:   shows "finmap_of J \<in> measurable (PiM J M) (PiF II M)"
immler@50088:   using assms
immler@50088:   by (intro measurable_finmap_of measurable_component_singleton) auto
immler@50088: 
hoelzl@50124: lemma measurable_proj_PiM:
immler@50088:   fixes J K ::"'a::countable set" and I::"'a set set"
immler@50088:   assumes "finite J" "J \<in> I"
immler@50088:   assumes "x \<in> space (PiM J M)"
hoelzl@50124:   shows "proj \<in> measurable (PiF {J} M) (PiM J M)"
immler@50088: proof (rule measurable_PiM_single)
wenzelm@53015:   show "proj \<in> space (PiF {J} M) \<rightarrow> (\<Pi>\<^sub>E i \<in> J. space (M i))"
immler@50088:     using assms by (auto simp add: space_PiM space_PiF extensional_def sets_PiF Pi'_def)
immler@50088: next
immler@50088:   fix A i assume A: "i \<in> J" "A \<in> sets (M i)"
wenzelm@53015:   show "{\<omega> \<in> space (PiF {J} M). (\<omega>)\<^sub>F i \<in> A} \<in> sets (PiF {J} M)"
immler@50088:   proof
wenzelm@53015:     have "{\<omega> \<in> space (PiF {J} M). (\<omega>)\<^sub>F i \<in> A} =
wenzelm@53015:       (\<lambda>\<omega>. (\<omega>)\<^sub>F i) -` A \<inter> space (PiF {J} M)" by auto
immler@50088:     also have "\<dots> \<in> sets (PiF {J} M)"
immler@50088:       using assms A by (auto intro: measurable_sets[OF measurable_proj_singleton] simp: space_PiM)
immler@50088:     finally show ?thesis .
immler@50088:   qed simp
immler@50088: qed
immler@50088: 
immler@50088: lemma space_PiF_singleton_eq_product:
immler@50088:   assumes "finite I"
immler@50088:   shows "space (PiF {I} M) = (\<Pi>' i\<in>I. space (M i))"
immler@50088:   by (auto simp: product_def space_PiF assms)
immler@50088: 
immler@50088: text {* adapted from @{thm sets_PiM_single} *}
immler@50088: 
immler@50088: lemma sets_PiF_single:
immler@50088:   assumes "finite I" "I \<noteq> {}"
immler@50088:   shows "sets (PiF {I} M) =
immler@50088:     sigma_sets (\<Pi>' i\<in>I. space (M i))
immler@50088:       {{f\<in>\<Pi>' i\<in>I. space (M i). f i \<in> A} | i A. i \<in> I \<and> A \<in> sets (M i)}"
immler@50088:     (is "_ = sigma_sets ?\<Omega> ?R")
immler@50088:   unfolding sets_PiF_singleton
immler@50088: proof (rule sigma_sets_eqI)
immler@50088:   interpret R: sigma_algebra ?\<Omega> "sigma_sets ?\<Omega> ?R" by (rule sigma_algebra_sigma_sets) auto
immler@50088:   fix A assume "A \<in> {Pi' I X |X. X \<in> (\<Pi> j\<in>I. sets (M j))}"
immler@50088:   then obtain X where X: "A = Pi' I X" "X \<in> (\<Pi> j\<in>I. sets (M j))" by auto
immler@50088:   show "A \<in> sigma_sets ?\<Omega> ?R"
immler@50088:   proof -
immler@50088:     from `I \<noteq> {}` X have "A = (\<Inter>j\<in>I. {f\<in>space (PiF {I} M). f j \<in> X j})"
immler@50244:       using sets.sets_into_space
immler@50088:       by (auto simp: space_PiF product_def) blast
immler@50088:     also have "\<dots> \<in> sigma_sets ?\<Omega> ?R"
immler@50088:       using X `I \<noteq> {}` assms by (intro R.finite_INT) (auto simp: space_PiF)
immler@50088:     finally show "A \<in> sigma_sets ?\<Omega> ?R" .
immler@50088:   qed
immler@50088: next
immler@50088:   fix A assume "A \<in> ?R"
immler@50088:   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)"
immler@50088:     by auto
immler@50088:   then have "A = (\<Pi>' j \<in> I. if j = i then B else space (M j))"
immler@50244:     using sets.sets_into_space[OF A(3)]
immler@50088:     apply (auto simp: Pi'_iff split: split_if_asm)
immler@50088:     apply blast
immler@50088:     done
immler@50088:   also have "\<dots> \<in> sigma_sets ?\<Omega> {Pi' I X |X. X \<in> (\<Pi> j\<in>I. sets (M j))}"
immler@50088:     using A
immler@50088:     by (intro sigma_sets.Basic )
immler@50088:        (auto intro: exI[where x="\<lambda>j. if j = i then B else space (M j)"])
immler@50088:   finally show "A \<in> sigma_sets ?\<Omega> {Pi' I X |X. X \<in> (\<Pi> j\<in>I. sets (M j))}" .
immler@50088: qed
immler@50088: 
immler@50088: text {* adapted from @{thm PiE_cong} *}
immler@50088: 
immler@50088: lemma Pi'_cong:
immler@50088:   assumes "finite I"
immler@50088:   assumes "\<And>i. i \<in> I \<Longrightarrow> f i = g i"
immler@50088:   shows "Pi' I f = Pi' I g"
immler@50088: using assms by (auto simp: Pi'_def)
immler@50088: 
immler@50088: text {* adapted from @{thm Pi_UN} *}
immler@50088: 
immler@50088: lemma Pi'_UN:
immler@50088:   fixes A :: "nat \<Rightarrow> 'i \<Rightarrow> 'a set"
immler@50088:   assumes "finite I"
immler@50088:   assumes mono: "\<And>i n m. i \<in> I \<Longrightarrow> n \<le> m \<Longrightarrow> A n i \<subseteq> A m i"
immler@50088:   shows "(\<Union>n. Pi' I (A n)) = Pi' I (\<lambda>i. \<Union>n. A n i)"
immler@50088: proof (intro set_eqI iffI)
immler@50088:   fix f assume "f \<in> Pi' I (\<lambda>i. \<Union>n. A n i)"
immler@50088:   then have "\<forall>i\<in>I. \<exists>n. f i \<in> A n i" "domain f = I" by (auto simp: `finite I` Pi'_def)
immler@50088:   from bchoice[OF this(1)] obtain n where n: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> (A (n i) i)" by auto
immler@50088:   obtain k where k: "\<And>i. i \<in> I \<Longrightarrow> n i \<le> k"
immler@50088:     using `finite I` finite_nat_set_iff_bounded_le[of "n`I"] by auto
immler@50088:   have "f \<in> Pi' I (\<lambda>i. A k i)"
immler@50088:   proof
immler@50088:     fix i assume "i \<in> I"
immler@50088:     from mono[OF this, of "n i" k] k[OF this] n[OF this] `domain f = I` `i \<in> I`
immler@50088:     show "f i \<in> A k i " by (auto simp: `finite I`)
immler@50088:   qed (simp add: `domain f = I` `finite I`)
immler@50088:   then show "f \<in> (\<Union>n. Pi' I (A n))" by auto
immler@50088: qed (auto simp: Pi'_def `finite I`)
immler@50088: 
immler@50088: text {* adapted from @{thm sigma_prod_algebra_sigma_eq} *}
immler@50088: 
immler@50088: lemma sigma_fprod_algebra_sigma_eq:
immler@51106:   fixes E :: "'i \<Rightarrow> 'a set set" and S :: "'i \<Rightarrow> nat \<Rightarrow> 'a set"
immler@50088:   assumes [simp]: "finite I" "I \<noteq> {}"
immler@50088:     and S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
immler@50088:     and S_in_E: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> E i"
immler@50088:   assumes E_closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M i))"
immler@50088:     and E_generates: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sigma_sets (space (M i)) (E i)"
immler@50088:   defines "P == { Pi' I F | F. \<forall>i\<in>I. F i \<in> E i }"
immler@50088:   shows "sets (PiF {I} M) = sigma_sets (space (PiF {I} M)) P"
immler@50088: proof
wenzelm@53015:   let ?P = "sigma (space (Pi\<^sub>F {I} M)) P"
immler@51106:   from `finite I`[THEN ex_bij_betw_finite_nat] guess T ..
immler@51106:   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"
immler@51106:     by (auto simp add: bij_betw_def set_eq_iff image_iff the_inv_into_f_f simp del: `finite I`)
wenzelm@53015:   have P_closed: "P \<subseteq> Pow (space (Pi\<^sub>F {I} M))"
immler@50088:     using E_closed by (auto simp: space_PiF P_def Pi'_iff subset_eq)
immler@50088:   then have space_P: "space ?P = (\<Pi>' i\<in>I. space (M i))"
immler@50088:     by (simp add: space_PiF)
immler@50088:   have "sets (PiF {I} M) =
immler@50088:       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)}"
immler@50088:     using sets_PiF_single[of I M] by (simp add: space_P)
immler@50088:   also have "\<dots> \<subseteq> sets (sigma (space (PiF {I} M)) P)"
immler@50244:   proof (safe intro!: sets.sigma_sets_subset)
immler@50088:     fix i A assume "i \<in> I" and A: "A \<in> sets (M i)"
wenzelm@53015:     have "(\<lambda>x. (x)\<^sub>F i) \<in> measurable ?P (sigma (space (M i)) (E i))"
immler@50088:     proof (subst measurable_iff_measure_of)
immler@50088:       show "E i \<subseteq> Pow (space (M i))" using `i \<in> I` by fact
wenzelm@53015:       from space_P `i \<in> I` show "(\<lambda>x. (x)\<^sub>F i) \<in> space ?P \<rightarrow> space (M i)"
immler@50088:         by auto
wenzelm@53015:       show "\<forall>A\<in>E i. (\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P \<in> sets ?P"
immler@50088:       proof
immler@50088:         fix A assume A: "A \<in> E i"
wenzelm@53015:         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))"
immler@50088:           using E_closed `i \<in> I` by (auto simp: space_P Pi_iff subset_eq split: split_if_asm)
immler@50088:         also have "\<dots> = (\<Pi>' j\<in>I. \<Union>n. if i = j then A else S j n)"
immler@50088:           by (intro Pi'_cong) (simp_all add: S_union)
immler@51106:         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))"
immler@51106:           using T
immler@51106:           apply auto
immler@51106:           apply (simp_all add: Pi'_iff bchoice_iff)
immler@51106:           apply (erule conjE exE)+
immler@51106:           apply (rule_tac x="map (\<lambda>n. f (the_inv_into I T n)) [0..<card I]" in exI)
immler@51106:           apply (auto simp: bij_betw_def)
immler@51106:           done
immler@50088:         also have "\<dots> \<in> sets ?P"
immler@50244:         proof (safe intro!: sets.countable_UN)
immler@51106:           fix xs show "(\<Pi>' j\<in>I. if i = j then A else S j (xs ! T j)) \<in> sets ?P"
immler@50088:             using A S_in_E
immler@50088:             by (simp add: P_closed)
immler@51106:                (auto simp: P_def subset_eq intro!: exI[of _ "\<lambda>j. if i = j then A else S j (xs ! T j)"])
immler@50088:         qed
wenzelm@53015:         finally show "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P \<in> sets ?P"
immler@50088:           using P_closed by simp
immler@50088:       qed
immler@50088:     qed
immler@50088:     from measurable_sets[OF this, of A] A `i \<in> I` E_closed
wenzelm@53015:     have "(\<lambda>x. (x)\<^sub>F i) -` A \<inter> space ?P \<in> sets ?P"
immler@50088:       by (simp add: E_generates)
wenzelm@53015:     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}"
immler@50088:       using P_closed by (auto simp: space_PiF)
immler@50088:     finally show "\<dots> \<in> sets ?P" .
immler@50088:   qed
immler@50088:   finally show "sets (PiF {I} M) \<subseteq> sigma_sets (space (PiF {I} M)) P"
immler@50088:     by (simp add: P_closed)
immler@50088:   show "sigma_sets (space (PiF {I} M)) P \<subseteq> sets (PiF {I} M)"
immler@50088:     using `finite I` `I \<noteq> {}`
immler@50244:     by (auto intro!: sets.sigma_sets_subset product_in_sets_PiFI simp: E_generates P_def)
immler@50088: qed
immler@50088: 
immler@50088: lemma product_open_generates_sets_PiF_single:
immler@50088:   assumes "I \<noteq> {}"
immler@50088:   assumes [simp]: "finite I"
hoelzl@50881:   shows "sets (PiF {I} (\<lambda>_. borel::'b::second_countable_topology measure)) =
immler@50088:     sigma_sets (space (PiF {I} (\<lambda>_. borel))) {Pi' I F |F. (\<forall>i\<in>I. F i \<in> Collect open)}"
immler@50088: proof -
immler@51106:   from open_countable_basisE[OF open_UNIV] guess S::"'b set set" . note S = this
immler@50088:   show ?thesis
immler@50088:   proof (rule sigma_fprod_algebra_sigma_eq)
immler@50088:     show "finite I" by simp
immler@50088:     show "I \<noteq> {}" by fact
immler@51106:     def S'\<equiv>"from_nat_into S"
immler@51106:     show "(\<Union>j. S' j) = space borel"
immler@51106:       using S
immler@51106:       apply (auto simp add: from_nat_into countable_basis_proj S'_def basis_proj_def)
immler@51106:       apply (metis (lifting, mono_tags) UNIV_I UnionE basis_proj_def countable_basis_proj countable_subset from_nat_into_surj)
immler@51106:       done
immler@51106:     show "range S' \<subseteq> Collect open"
immler@51106:       using S
immler@51106:       apply (auto simp add: from_nat_into countable_basis_proj S'_def)
immler@51106:       apply (metis UNIV_not_empty Union_empty from_nat_into set_mp topological_basis_open[OF basis_proj] basis_proj_def)
immler@51106:       done
immler@50088:     show "Collect open \<subseteq> Pow (space borel)" by simp
immler@50088:     show "sets borel = sigma_sets (space borel) (Collect open)"
immler@50088:       by (simp add: borel_def)
immler@50088:   qed
immler@50088: qed
immler@50088: 
hoelzl@50124: lemma finmap_UNIV[simp]: "(\<Union>J\<in>Collect finite. PI' j : J. UNIV) = UNIV" by auto
immler@50088: 
immler@50088: lemma borel_eq_PiF_borel:
wenzelm@53015:   shows "(borel :: ('i::countable \<Rightarrow>\<^sub>F 'a::polish_space) measure) =
immler@50245:     PiF (Collect finite) (\<lambda>_. borel :: 'a measure)"
immler@50245:   unfolding borel_def PiF_def
immler@50245: proof (rule measure_eqI, clarsimp, rule sigma_sets_eqI)
wenzelm@53015:   fix a::"('i \<Rightarrow>\<^sub>F 'a) set" assume "a \<in> Collect open" hence "open a" by simp
immler@50245:   then obtain B' where B': "B'\<subseteq>basis_finmap" "a = \<Union>B'"
immler@50245:     using finmap_topological_basis by (force simp add: topological_basis_def)
immler@50245:   have "a \<in> sigma UNIV {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)}"
immler@50245:     unfolding `a = \<Union>B'`
immler@50245:   proof (rule sets.countable_Union)
immler@50245:     from B' countable_basis_finmap show "countable B'" by (metis countable_subset)
immler@50088:   next
immler@50245:     show "B' \<subseteq> sets (sigma UNIV
immler@50245:       {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)})" (is "_ \<subseteq> sets ?s")
immler@50088:     proof
immler@50245:       fix x assume "x \<in> B'" with B' have "x \<in> basis_finmap" by auto
immler@50245:       then obtain J X where "x = Pi' J X" "finite J" "X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)"
immler@51106:         by (auto simp: basis_finmap_def topological_basis_open[OF basis_proj])
immler@50245:       thus "x \<in> sets ?s" by auto
immler@50088:     qed
immler@50088:   qed
immler@50245:   thus "a \<in> sigma_sets UNIV {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)}"
immler@50245:     by simp
immler@50245: next
wenzelm@53015:   fix b::"('i \<Rightarrow>\<^sub>F 'a) set"
immler@50245:   assume "b \<in> {Pi' J X |X J. finite J \<and> X \<in> J \<rightarrow> sigma_sets UNIV (Collect open)}"
wenzelm@53015:   hence b': "b \<in> sets (Pi\<^sub>F (Collect finite) (\<lambda>_. borel))" by (auto simp: sets_PiF borel_def)
immler@50245:   let ?b = "\<lambda>J. b \<inter> {x. domain x = J}"
immler@50245:   have "b = \<Union>((\<lambda>J. ?b J) ` Collect finite)" by auto
immler@50245:   also have "\<dots> \<in> sets borel"
immler@50245:   proof (rule sets.countable_Union, safe)
immler@50245:     fix J::"'i set" assume "finite J"
immler@50245:     { assume ef: "J = {}"
immler@50245:       have "?b J \<in> sets borel"
immler@50245:       proof cases
immler@50245:         assume "?b J \<noteq> {}"
immler@50245:         then obtain f where "f \<in> b" "domain f = {}" using ef by auto
immler@50245:         hence "?b J = {f}" using `J = {}`
immler@50245:           by (auto simp: finmap_eq_iff)
immler@50245:         also have "{f} \<in> sets borel" by simp
immler@50245:         finally show ?thesis .
immler@50245:       qed simp
immler@50245:     } moreover {
immler@50245:       assume "J \<noteq> ({}::'i set)"
immler@50245:       have "(?b J) = b \<inter> {m. domain m \<in> {J}}" by auto
immler@50245:       also have "\<dots> \<in> sets (PiF {J} (\<lambda>_. borel))"
immler@50245:         using b' by (rule restrict_sets_measurable) (auto simp: `finite J`)
immler@50245:       also have "\<dots> = sigma_sets (space (PiF {J} (\<lambda>_. borel)))
immler@50245:         {Pi' (J) F |F. (\<forall>j\<in>J. F j \<in> Collect open)}"
immler@50245:         (is "_ = sigma_sets _ ?P")
immler@50245:        by (rule product_open_generates_sets_PiF_single[OF `J \<noteq> {}` `finite J`])
immler@50245:       also have "\<dots> \<subseteq> sigma_sets UNIV (Collect open)"
immler@50245:         by (intro sigma_sets_mono'') (auto intro!: open_Pi'I simp: space_PiF)
immler@50245:       finally have "(?b J) \<in> sets borel" by (simp add: borel_def)
immler@50245:     } ultimately show "(?b J) \<in> sets borel" by blast
immler@50245:   qed (simp add: countable_Collect_finite)
immler@50245:   finally show "b \<in> sigma_sets UNIV (Collect open)" by (simp add: borel_def)
immler@50088: qed (simp add: emeasure_sigma borel_def PiF_def)
immler@50088: 
immler@50088: subsection {* Isomorphism between Functions and Finite Maps *}
immler@50088: 
hoelzl@50124: lemma measurable_finmap_compose:
immler@50088:   shows "(\<lambda>m. compose J m f) \<in> measurable (PiM (f ` J) (\<lambda>_. M)) (PiM J (\<lambda>_. M))"
hoelzl@50124:   unfolding compose_def by measurable
immler@50088: 
hoelzl@50124: lemma measurable_compose_inv:
immler@50088:   assumes inj: "\<And>j. j \<in> J \<Longrightarrow> f' (f j) = j"
immler@50088:   shows "(\<lambda>m. compose (f ` J) m f') \<in> measurable (PiM J (\<lambda>_. M)) (PiM (f ` J) (\<lambda>_. M))"
hoelzl@50124:   unfolding compose_def by (rule measurable_restrict) (auto simp: inj)
immler@50088: 
immler@50088: locale function_to_finmap =
immler@50088:   fixes J::"'a set" and f :: "'a \<Rightarrow> 'b::countable" and f'
immler@50088:   assumes [simp]: "finite J"
immler@50088:   assumes inv: "i \<in> J \<Longrightarrow> f' (f i) = i"
immler@50088: begin
immler@50088: 
immler@50088: text {* to measure finmaps *}
immler@50088: 
immler@50088: definition "fm = (finmap_of (f ` J)) o (\<lambda>g. compose (f ` J) g f')"
immler@50088: 
immler@50088: lemma domain_fm[simp]: "domain (fm x) = f ` J"
immler@50088:   unfolding fm_def by simp
immler@50088: 
immler@50088: lemma fm_restrict[simp]: "fm (restrict y J) = fm y"
immler@50088:   unfolding fm_def by (auto simp: compose_def inv intro: restrict_ext)
immler@50088: 
immler@50088: lemma fm_product:
immler@50088:   assumes "\<And>i. space (M i) = UNIV"
wenzelm@53015:   shows "fm -` Pi' (f ` J) S \<inter> space (Pi\<^sub>M J M) = (\<Pi>\<^sub>E j \<in> J. S (f j))"
immler@50088:   using assms
immler@50088:   by (auto simp: inv fm_def compose_def space_PiM Pi'_def)
immler@50088: 
immler@50088: lemma fm_measurable:
immler@50088:   assumes "f ` J \<in> N"
wenzelm@53015:   shows "fm \<in> measurable (Pi\<^sub>M J (\<lambda>_. M)) (Pi\<^sub>F N (\<lambda>_. M))"
immler@50088:   unfolding fm_def
immler@50088: proof (rule measurable_comp, rule measurable_compose_inv)
wenzelm@53015:   show "finmap_of (f ` J) \<in> measurable (Pi\<^sub>M (f ` J) (\<lambda>_. M)) (PiF N (\<lambda>_. M)) "
immler@50088:     using assms by (intro measurable_finmap_of measurable_component_singleton) auto
immler@50088: qed (simp_all add: inv)
immler@50088: 
immler@50088: lemma proj_fm:
immler@50088:   assumes "x \<in> J"
immler@50088:   shows "fm m (f x) = m x"
immler@50088:   using assms by (auto simp: fm_def compose_def o_def inv)
immler@50088: 
immler@50088: lemma inj_on_compose_f': "inj_on (\<lambda>g. compose (f ` J) g f') (extensional J)"
immler@50088: proof (rule inj_on_inverseI)
immler@50088:   fix x::"'a \<Rightarrow> 'c" assume "x \<in> extensional J"
immler@50088:   thus "(\<lambda>x. compose J x f) (compose (f ` J) x f') = x"
immler@50088:     by (auto simp: compose_def inv extensional_def)
immler@50088: qed
immler@50088: 
immler@50088: lemma inj_on_fm:
immler@50088:   assumes "\<And>i. space (M i) = UNIV"
wenzelm@53015:   shows "inj_on fm (space (Pi\<^sub>M J M))"
immler@50088:   using assms
hoelzl@50123:   apply (auto simp: fm_def space_PiM PiE_def)
immler@50088:   apply (rule comp_inj_on)
immler@50088:   apply (rule inj_on_compose_f')
immler@50088:   apply (rule finmap_of_inj_on_extensional_finite)
immler@50088:   apply simp
immler@50088:   apply (auto)
immler@50088:   done
immler@50088: 
immler@50088: text {* to measure functions *}
immler@50088: 
immler@50088: definition "mf = (\<lambda>g. compose J g f) o proj"
immler@50088: 
immler@50088: lemma mf_fm:
wenzelm@53015:   assumes "x \<in> space (Pi\<^sub>M J (\<lambda>_. M))"
immler@50088:   shows "mf (fm x) = x"
immler@50088: proof -
immler@50088:   have "mf (fm x) \<in> extensional J"
immler@50088:     by (auto simp: mf_def extensional_def compose_def)
immler@50088:   moreover
immler@50244:   have "x \<in> extensional J" using assms sets.sets_into_space
hoelzl@50123:     by (force simp: space_PiM PiE_def)
immler@50088:   moreover
immler@50088:   { fix i assume "i \<in> J"
immler@50088:     hence "mf (fm x) i = x i"
immler@50088:       by (auto simp: inv mf_def compose_def fm_def)
immler@50088:   }
immler@50088:   ultimately
immler@50088:   show ?thesis by (rule extensionalityI)
immler@50088: qed
immler@50088: 
immler@50088: lemma mf_measurable:
immler@50088:   assumes "space M = UNIV"
immler@50088:   shows "mf \<in> measurable (PiF {f ` J} (\<lambda>_. M)) (PiM J (\<lambda>_. M))"
immler@50088:   unfolding mf_def
immler@50088: proof (rule measurable_comp, rule measurable_proj_PiM)
wenzelm@53015:   show "(\<lambda>g. compose J g f) \<in> measurable (Pi\<^sub>M (f ` J) (\<lambda>x. M)) (Pi\<^sub>M J (\<lambda>_. M))"
hoelzl@50124:     by (rule measurable_finmap_compose)
immler@50088: qed (auto simp add: space_PiM extensional_def assms)
immler@50088: 
immler@50088: lemma fm_image_measurable:
immler@50088:   assumes "space M = UNIV"
wenzelm@53015:   assumes "X \<in> sets (Pi\<^sub>M J (\<lambda>_. M))"
immler@50088:   shows "fm ` X \<in> sets (PiF {f ` J} (\<lambda>_. M))"
immler@50088: proof -
immler@50088:   have "fm ` X = (mf) -` X \<inter> space (PiF {f ` J} (\<lambda>_. M))"
immler@50088:   proof safe
immler@50088:     fix x assume "x \<in> X"
immler@50244:     with mf_fm[of x] sets.sets_into_space[OF assms(2)] show "fm x \<in> mf -` X" by auto
immler@50088:     show "fm x \<in> space (PiF {f ` J} (\<lambda>_. M))" by (simp add: space_PiF assms)
immler@50088:   next
immler@50088:     fix y x
immler@50088:     assume x: "mf y \<in> X"
immler@50088:     assume y: "y \<in> space (PiF {f ` J} (\<lambda>_. M))"
immler@50088:     thus "y \<in> fm ` X"
immler@50088:       by (intro image_eqI[OF _ x], unfold finmap_eq_iff)
immler@50088:          (auto simp: space_PiF fm_def mf_def compose_def inv Pi'_def)
immler@50088:   qed
immler@50088:   also have "\<dots> \<in> sets (PiF {f ` J} (\<lambda>_. M))"
immler@50088:     using assms
immler@50088:     by (intro measurable_sets[OF mf_measurable]) auto
immler@50088:   finally show ?thesis .
immler@50088: qed
immler@50088: 
immler@50088: lemma fm_image_measurable_finite:
immler@50088:   assumes "space M = UNIV"
wenzelm@53015:   assumes "X \<in> sets (Pi\<^sub>M J (\<lambda>_. M::'c measure))"
immler@50088:   shows "fm ` X \<in> sets (PiF (Collect finite) (\<lambda>_. M::'c measure))"
immler@50088:   using fm_image_measurable[OF assms]
immler@50088:   by (rule subspace_set_in_sets) (auto simp: finite_subset)
immler@50088: 
immler@50088: text {* measure on finmaps *}
immler@50088: 
immler@50088: definition "mapmeasure M N = distr M (PiF (Collect finite) N) (fm)"
immler@50088: 
immler@50088: lemma sets_mapmeasure[simp]: "sets (mapmeasure M N) = sets (PiF (Collect finite) N)"
immler@50088:   unfolding mapmeasure_def by simp
immler@50088: 
immler@50088: lemma space_mapmeasure[simp]: "space (mapmeasure M N) = space (PiF (Collect finite) N)"
immler@50088:   unfolding mapmeasure_def by simp
immler@50088: 
immler@50088: lemma mapmeasure_PiF:
wenzelm@53015:   assumes s1: "space M = space (Pi\<^sub>M J (\<lambda>_. N))"
wenzelm@53015:   assumes s2: "sets M = sets (Pi\<^sub>M J (\<lambda>_. N))"
immler@50088:   assumes "space N = UNIV"
immler@50088:   assumes "X \<in> sets (PiF (Collect finite) (\<lambda>_. N))"
immler@50088:   shows "emeasure (mapmeasure M (\<lambda>_. N)) X = emeasure M ((fm -` X \<inter> extensional J))"
immler@50088:   using assms
immler@50088:   by (auto simp: measurable_eqI[OF s1 refl s2 refl] mapmeasure_def emeasure_distr
hoelzl@50123:     fm_measurable space_PiM PiE_def)
immler@50088: 
immler@50088: lemma mapmeasure_PiM:
immler@50088:   fixes N::"'c measure"
wenzelm@53015:   assumes s1: "space M = space (Pi\<^sub>M J (\<lambda>_. N))"
wenzelm@53015:   assumes s2: "sets M = (Pi\<^sub>M J (\<lambda>_. N))"
immler@50088:   assumes N: "space N = UNIV"
immler@50088:   assumes X: "X \<in> sets M"
immler@50088:   shows "emeasure M X = emeasure (mapmeasure M (\<lambda>_. N)) (fm ` X)"
immler@50088:   unfolding mapmeasure_def
immler@50088: proof (subst emeasure_distr, subst measurable_eqI[OF s1 refl s2 refl], rule fm_measurable)
wenzelm@53015:   have "X \<subseteq> space (Pi\<^sub>M J (\<lambda>_. N))" using assms by (simp add: sets.sets_into_space)
wenzelm@53015:   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"
immler@50088:     by (auto simp: vimage_image_eq inj_on_def)
immler@50088:   thus "emeasure M X = emeasure M (fm -` fm ` X \<inter> space M)" using s1
immler@50088:     by simp
immler@50088:   show "fm ` X \<in> sets (PiF (Collect finite) (\<lambda>_. N))"
immler@50088:     by (rule fm_image_measurable_finite[OF N X[simplified s2]])
immler@50088: qed simp
immler@50088: 
immler@50088: end
immler@50088: 
immler@50088: end




