Theory Fin_Map

Up to index of Isabelle/HOL/HOL-Multivariate_Analysis/HOL-Probability/Stochastic_Processes

theory Fin_Map
imports Polish_Space
(* Author: Fabian Immler <immler@in.tum.de> *)

theory Fin_Map
imports Auxiliarities Polish_Space
begin

section {* Finite Maps *}

typedef (open) ('i, 'a) finmap ("(_ =>F /_)" [22, 21] 21) =
"{(I::'i set, f::'i => 'a). finite I ∧ f ∈ extensional I}"
by auto
print_theorems
subsection {* Domain and Application *}

definition domain where "domain P = fst (Rep_finmap P)"

lemma finite_domain[simp, intro]: "finite (domain P)"
by (cases P) (auto simp: domain_def Abs_finmap_inverse)

definition proj ("_F" [1000] 1000) where "proj P i = snd (Rep_finmap P) i"

declare [[coercion proj]]

lemma extensional_proj[simp, intro]: "(P)F ∈ extensional (domain P)"
by (cases P) (auto simp: domain_def Abs_finmap_inverse proj_def[abs_def])

lemma proj_undefined[simp, intro]: "i ∉ domain P ==> P i = undefined"
using extensional_proj[of P] unfolding extensional_def by auto

lemma finmap_eq_iff: "P = Q <-> (domain P = domain Q ∧ (∀i∈domain P. P i = Q i))"
by (cases P, cases Q)
(auto simp add: Abs_finmap_inject extensional_def domain_def proj_def Abs_finmap_inverse
intro: extensionalityI)

subsection {* Countable Finite Maps *}

instance finmap :: (countable, countable) countable
proof
obtain mapper where mapper: "!!fm :: 'a =>F 'b. set (mapper fm) = domain fm"
by (metis finite_list[OF finite_domain])
have "inj (λfm. map (λi. (i, (fm)F i)) (mapper fm))" (is "inj ?F")
proof (rule inj_onI)
fix f1 f2 assume "?F f1 = ?F f2"
then have "map fst (?F f1) = map fst (?F f2)" by simp
then have "mapper f1 = mapper f2" by (simp add: comp_def)
then have "domain f1 = domain f2" by (simp add: mapper[symmetric])
with `?F f1 = ?F f2` show "f1 = f2"
unfolding `mapper f1 = mapper f2` map_eq_conv mapper
by (simp add: finmap_eq_iff)
qed
then show "∃to_nat :: 'a =>F 'b => nat. inj to_nat"
by (intro exI[of _ "to_nat o ?F"] inj_comp) auto
qed

subsection {* Constructor of Finite Maps *}

definition "finmap_of inds f = Abs_finmap (inds, restrict f inds)"

lemma proj_finmap_of[simp]:
assumes "finite inds"
shows "(finmap_of inds f)F = restrict f inds"
using assms
by (auto simp: Abs_finmap_inverse finmap_of_def proj_def)

lemma domain_finmap_of[simp]:
assumes "finite inds"
shows "domain (finmap_of inds f) = inds"
using assms
by (auto simp: Abs_finmap_inverse finmap_of_def domain_def)

lemma finmap_of_eq_iff[simp]:
assumes "finite i" "finite j"
shows "finmap_of i m = finmap_of j n <-> i = j ∧ restrict m i = restrict n i"
using assms
apply (auto simp: finmap_eq_iff restrict_def) by metis

lemma
finmap_of_inj_on_extensional_finite:
assumes "finite K"
assumes "S ⊆ extensional K"
shows "inj_on (finmap_of K) S"
proof (rule inj_onI)
fix x y::"'a => 'b"
assume "finmap_of K x = finmap_of K y"
hence "(finmap_of K x)F = (finmap_of K y)F" by simp
moreover
assume "x ∈ S" "y ∈ S" hence "x ∈ extensional K" "y ∈ extensional K" using assms by auto
ultimately
show "x = y" using assms by (simp add: extensional_restrict)
qed

lemma finmap_choice:
assumes *: "!!i. i ∈ I ==> ∃x. P i x" and I: "finite I"
shows "∃fm. domain fm = I ∧ (∀i∈I. P i (fm i))"
proof -
have "∃f. ∀i∈I. P i (f i)"
unfolding bchoice_iff[symmetric] using * by auto
then guess f ..
with I show ?thesis
by (intro exI[of _ "finmap_of I f"]) auto
qed

subsection {* Product set of Finite Maps *}

text {* This is @{term Pi} for Finite Maps, most of this is copied *}

definition Pi' :: "'i set => ('i => 'a set) => ('i =>F 'a) set" where
"Pi' I A = { P. domain P = I ∧ (∀i. i ∈ I --> (P)F i ∈ A i) } "

syntax
"_Pi'" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set" ("(3PI' _:_./ _)" 10)

syntax (xsymbols)
"_Pi'" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set" ("(3Π' _∈_./ _)" 10)

syntax (HTML output)
"_Pi'" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set" ("(3Π' _∈_./ _)" 10)

translations
"PI' x:A. B" == "CONST Pi' A (%x. B)"

abbreviation
finmapset :: "['a set, 'b set] => ('a =>F 'b) set"
(infixr "~>" 60) where
"A ~> B ≡ Pi' A (%_. B)"

notation (xsymbols)
finmapset (infixr "\<leadsto>" 60)

subsubsection{*Basic Properties of @{term Pi'}*}

lemma Pi'_I[intro!]: "domain f = A ==> (!!x. x ∈ A ==> f x ∈ B x) ==> f ∈ Pi' A B"
by (simp add: Pi'_def)

lemma Pi'_I'[simp]: "domain f = A ==> (!!x. x ∈ A --> f x ∈ B x) ==> f ∈ Pi' A B"
by (simp add:Pi'_def)

lemma finmapsetI: "domain f = A ==> (!!x. x ∈ A ==> f x ∈ B) ==> f ∈ A \<leadsto> B"
by (simp add: Pi_def)

lemma Pi'_mem: "f∈ Pi' A B ==> x ∈ A ==> f x ∈ B x"
by (simp add: Pi'_def)

lemma Pi'_iff: "f ∈ Pi' I X <-> domain f = I ∧ (∀i∈I. f i ∈ X i)"
unfolding Pi'_def by auto

lemma Pi'E [elim]:
"f ∈ Pi' A B ==> (f x ∈ B x ==> domain f = A ==> Q) ==> (x ∉ A ==> Q) ==> Q"
by(auto simp: Pi'_def)

lemma in_Pi'_cong:
"domain f = domain g ==> (!! w. w ∈ A ==> f w = g w) ==> f ∈ Pi' A B <-> g ∈ Pi' A B"
by (auto simp: Pi'_def)

lemma funcset_mem: "[|f ∈ A \<leadsto> B; x ∈ A|] ==> f x ∈ B"
by (simp add: Pi'_def)

lemma funcset_image: "f ∈ A \<leadsto> B ==> f ` A ⊆ B"
by auto

lemma Pi'_eq_empty[simp]:
assumes "finite A" shows "(Pi' A B) = {} <-> (∃x∈A. B x = {})"
using assms
apply (simp add: Pi'_def, auto)
apply (drule_tac x = "finmap_of A (λu. SOME y. y ∈ B u)" in spec, auto)
apply (cut_tac P= "%y. y ∈ B i" in some_eq_ex, auto)
done

lemma Pi'_mono: "(!!x. x ∈ A ==> B x ⊆ C x) ==> Pi' A B ⊆ Pi' A C"
by (auto simp: Pi'_def)

lemma Pi_Pi': "finite A ==> (PiE A B) = proj ` Pi' A B"
apply (auto simp: Pi'_def Pi_def extensional_def)
apply (rule_tac x = "finmap_of A (restrict x A)" in image_eqI)
apply auto
done

subsection {* Metric Space of Finite Maps *}

instantiation finmap :: (type, metric_space) metric_space
begin

definition dist_finmap where
"dist P Q = (∑i∈domain P ∪ domain Q. dist ((P)F i) ((Q)F i)) +
card ((domain P - domain Q) ∪ (domain Q - domain P))"


lemma dist_finmap_extend:
assumes "finite X"
shows "dist P Q = (∑i∈domain P ∪ domain Q ∪ X. dist ((P)F i) ((Q)F i)) +
card ((domain P - domain Q) ∪ (domain Q - domain P))"

unfolding dist_finmap_def add_right_cancel
using assms extensional_arb[of "(P)F"] extensional_arb[of "(Q)F" "domain Q"]
by (intro setsum_mono_zero_cong_left) auto

definition open_finmap :: "('a =>F 'b) set => bool" where
"open_finmap S = (∀x∈S. ∃e>0. ∀y. dist y x < e --> y ∈ S)"

lemma add_eq_zero_iff[simp]:
fixes a b::real
assumes "a ≥ 0" "b ≥ 0"
shows "a + b = 0 <-> a = 0 ∧ b = 0"
using assms by auto

lemma dist_le_1_imp_domain_eq:
assumes "dist P Q < 1"
shows "domain P = domain Q"
proof -
have "0 ≤ (∑i∈domain P ∪ domain Q. dist (P i) (Q i))"
by (simp add: setsum_nonneg)
with assms have "card (domain P - domain Q ∪ (domain Q - domain P)) = 0"
unfolding dist_finmap_def by arith
thus "domain P = domain Q" by auto
qed

lemma dist_proj:
shows "dist ((x)F i) ((y)F i) ≤ dist x y"
proof -
have "dist (x i) (y i) = (∑i∈{i}. dist (x i) (y i))" by simp
also have "… ≤ (∑i∈domain x ∪ domain y ∪ {i}. dist (x i) (y i))"
by (intro setsum_mono2) auto
also have "… ≤ dist x y" by (simp add: dist_finmap_extend[of "{i}"])
finally show ?thesis by simp
qed

lemma open_Pi'I:
assumes open_component: "!!i. i ∈ I ==> open (A i)"
shows "open (Pi' I A)"
proof (subst open_finmap_def, safe)
fix x assume x: "x ∈ Pi' I A"
hence dim_x: "domain x = I" by (simp add: Pi'_def)
hence [simp]: "finite I" unfolding dim_x[symmetric] by simp
have "∃ei. ∀i∈I. 0 < ei i ∧ (∀y. dist y (x i) < ei i --> y ∈ A i)"
proof (safe intro!: bchoice)
fix i assume i: "i ∈ I"
moreover with open_component have "open (A i)" by simp
moreover have "x i ∈ A i" using x i
by (auto simp: proj_def)
ultimately show "∃e>0. ∀y. dist y (x i) < e --> y ∈ A i"
using x by (auto simp: open_dist Ball_def)
qed
then guess ei .. note ei = this
def es "ei ` I"
def e "if es = {} then 0.5 else min 0.5 (Min es)"
from ei have "e > 0" using x
by (auto simp add: e_def es_def Pi'_def Ball_def)
moreover have "∀y. dist y x < e --> y ∈ Pi' I A"
proof (intro allI impI)
fix y
assume "dist y x < e"
also have "… < 1" by (auto simp: e_def)
finally have "domain y = domain x" by (rule dist_le_1_imp_domain_eq)
with dim_x have dims: "domain y = domain x" "domain x = I" by auto
show "y ∈ Pi' I A"
proof
show "domain y = I" using dims by simp
next
fix i
assume "i ∈ I"
have "dist (y i) (x i) ≤ dist y x" using dims `i ∈ I`
by (auto intro: dist_proj)
also have "… < e" using `dist y x < e` dims
by (simp add: dist_finmap_def)
also have "e ≤ Min (ei ` I)" using dims `i ∈ I`
by (auto simp: e_def es_def)
also have "… ≤ ei i" using `i ∈ I` by (simp add: e_def)
finally have "dist (y i) (x i) < ei i" .
with ei `i ∈ I` show "y i ∈ A i" by simp
qed
qed
ultimately
show "∃e>0. ∀y. dist y x < e --> y ∈ Pi' I A" by blast
qed

instance
proof
fix S::"('a =>F 'b) set"
show "open S = (∀x∈S. ∃e>0. ∀y. dist y x < e --> y ∈ S)"
unfolding open_finmap_def ..
next
fix P Q::"'a =>F 'b"
show "dist P Q = 0 <-> P = Q"
by (auto simp: finmap_eq_iff dist_finmap_def setsum_nonneg setsum_nonneg_eq_0_iff)
next
fix P Q R::"'a =>F 'b"
let ?symdiff = "λa b. domain a - domain b ∪ (domain b - domain a)"
def E "domain P ∪ domain Q ∪ domain R"
hence "finite E" by (simp add: E_def)
have "card (?symdiff P Q) ≤ card (?symdiff P R ∪ ?symdiff Q R)"
by (auto intro: card_mono)
also have "… ≤ card (?symdiff P R) + card (?symdiff Q R)"
by (subst card_Un_Int) auto
finally have "dist P Q ≤ (∑i∈E. dist (P i) (R i) + dist (Q i) (R i)) +
real (card (?symdiff P R) + card (?symdiff Q R))"

unfolding dist_finmap_extend[OF `finite E`]
by (intro add_mono) (auto simp: E_def intro: setsum_mono dist_triangle_le)
also have "… ≤ dist P R + dist Q R"
unfolding dist_finmap_extend[OF `finite E`] by (simp add: ac_simps E_def setsum_addf[symmetric])
finally show "dist P Q ≤ dist P R + dist Q R" by simp
qed

end

lemma open_restricted_space:
shows "open {m. P (domain m)}"
proof -
have "{m. P (domain m)} = (\<Union>i ∈ Collect P. {m. domain m = i})" by auto
also have "open …"
proof (rule, safe, cases)
fix i::"'a set"
assume "finite i"
hence "{m. domain m = i} = Pi' i (λ_. UNIV)" by (auto simp: Pi'_def)
also have "open …" by (auto intro: open_Pi'I simp: `finite i`)
finally show "open {m. domain m = i}" .
next
fix i::"'a set"
assume "¬ finite i" hence "{m. domain m = i} = {}" by auto
also have "open …" by simp
finally show "open {m. domain m = i}" .
qed
finally show ?thesis .
qed

lemma closed_restricted_space:
shows "closed {m. P (domain m)}"
proof -
have "{m. P (domain m)} = - (\<Union>i ∈ - Collect P. {m. domain m = i})" by auto
also have "closed …"
proof (rule, rule, rule, cases)
fix i::"'a set"
assume "finite i"
hence "{m. domain m = i} = Pi' i (λ_. UNIV)" by (auto simp: Pi'_def)
also have "open …" by (auto intro: open_Pi'I simp: `finite i`)
finally show "open {m. domain m = i}" .
next
fix i::"'a set"
assume "¬ finite i" hence "{m. domain m = i} = {}" by auto
also have "open …" by simp
finally show "open {m. domain m = i}" .
qed
finally show ?thesis .
qed

lemma continuous_proj:
shows "continuous_on s (λx. (x)F i)"
unfolding continuous_on_topological
proof safe
fix x B assume "x ∈ s" "open B" "x i ∈ B"
let ?A = "Pi' (domain x) (λj. if i = j then B else UNIV)"
have "open ?A" using `open B` by (auto intro: open_Pi'I)
moreover have "x ∈ ?A" using `x i ∈ B` by auto
moreover have "(∀y∈s. y ∈ ?A --> y i ∈ B)"
proof (cases, safe)
fix y assume "y ∈ s"
assume "i ∉ domain x" hence "undefined ∈ B" using `x i ∈ B`
by simp
moreover
assume "y ∈ ?A" hence "domain y = domain x" by (simp add: Pi'_def)
hence "y i = undefined" using `i ∉ domain x` by simp
ultimately
show "y i ∈ B" by simp
qed force
ultimately
show "∃A. open A ∧ x ∈ A ∧ (∀y∈s. y ∈ A --> y i ∈ B)" by blast
qed

subsection {* Complete Space of Finite Maps *}

lemma tendsto_dist_zero:
assumes "(λi. dist (f i) g) ----> 0"
shows "f ----> g"
using assms by (auto simp: tendsto_iff dist_real_def)

lemma tendsto_dist_zero':
assumes "(λi. dist (f i) g) ----> x"
assumes "0 = x"
shows "f ----> g"
using assms tendsto_dist_zero by simp

lemma tendsto_finmap:
fixes f::"nat => ('i =>F ('a::metric_space))"
assumes ind_f: "!!n. domain (f n) = domain g"
assumes proj_g: "!!i. i ∈ domain g ==> (λn. (f n) i) ----> g i"
shows "f ----> g"
apply (rule tendsto_dist_zero')
unfolding dist_finmap_def assms
apply (rule tendsto_intros proj_g | simp)+
done

instance finmap :: (type, complete_space) complete_space
proof
fix P::"nat => 'a =>F 'b"
assume "Cauchy P"
then obtain Nd where Nd: "!!n. n ≥ Nd ==> dist (P n) (P Nd) < 1"
by (force simp: cauchy)
def d "domain (P Nd)"
with Nd have dim: "!!n. n ≥ Nd ==> domain (P n) = d" using dist_le_1_imp_domain_eq by auto
have [simp]: "finite d" unfolding d_def by simp
def p "λi n. (P n) i"
def q "λi. lim (p i)"
def Q "finmap_of d q"
have q: "!!i. i ∈ d ==> q i = Q i" by (auto simp add: Q_def Abs_finmap_inverse)
{
fix i assume "i ∈ d"
have "Cauchy (p i)" unfolding cauchy p_def
proof safe
fix e::real assume "0 < e"
with `Cauchy P` obtain N where N: "!!n. n≥N ==> dist (P n) (P N) < min e 1"
by (force simp: cauchy min_def)
hence "!!n. n ≥ N ==> domain (P n) = domain (P N)" using dist_le_1_imp_domain_eq by auto
with dim have dim: "!!n. n ≥ N ==> domain (P n) = d" by (metis nat_le_linear)
show "∃N. ∀n≥N. dist ((P n) i) ((P N) i) < e"
proof (safe intro!: exI[where x="N"])
fix n assume "N ≤ n" have "N ≤ N" by simp
have "dist ((P n) i) ((P N) i) ≤ dist (P n) (P N)"
using dim[OF `N ≤ n`] dim[OF `N ≤ N`] `i ∈ d`
by (auto intro!: dist_proj)
also have "… < e" using N[OF `N ≤ n`] by simp
finally show "dist ((P n) i) ((P N) i) < e" .
qed
qed
hence "convergent (p i)" by (metis Cauchy_convergent_iff)
hence "p i ----> q i" unfolding q_def convergent_def by (metis limI)
} note p = this
have "P ----> Q"
proof (rule metric_LIMSEQ_I)
fix e::real assume "0 < e"
def e' "min 1 (e / (card d + 1))"
hence "0 < e'" using `0 < e` by (auto simp: e'_def intro: divide_pos_pos)
have "∃ni. ∀i∈d. ∀n≥ni i. dist (p i n) (q i) < e'"
proof (safe intro!: bchoice)
fix i assume "i ∈ d"
from p[OF `i ∈ d`, THEN metric_LIMSEQ_D, OF `0 < e'`]
show "∃no. ∀n≥no. dist (p i n) (q i) < e'" .
qed then guess ni .. note ni = this
def N "max Nd (Max (ni ` d))"
show "∃N. ∀n≥N. dist (P n) Q < e"
proof (safe intro!: exI[where x="N"])
fix n assume "N ≤ n"
hence "domain (P n) = d" "domain Q = d" "domain (P n) = domain Q"
using dim by (simp_all add: N_def Q_def dim_def Abs_finmap_inverse)
hence "dist (P n) Q = (∑i∈d. dist ((P n) i) (Q i))" by (simp add: dist_finmap_def)
also have "… ≤ (∑i∈d. e')"
proof (intro setsum_mono less_imp_le)
fix i assume "i ∈ d"
hence "ni i ≤ Max (ni ` d)" by simp
also have "… ≤ N" by (simp add: N_def)
also have "… ≤ n" using `N ≤ n` .
finally
show "dist ((P n) i) (Q i) < e'"
using ni `i ∈ d` by (auto simp: p_def q N_def)
qed
also have "… = card d * e'" by (simp add: real_eq_of_nat)
also have "… < e" using `0 < e` by (simp add: e'_def field_simps min_def)
finally show "dist (P n) Q < e" .
qed
qed
thus "convergent P" by (auto simp: convergent_def)
qed

subsection {* Polish Space of Finite Maps *}

instantiation finmap :: (countable, polish_space) polish_space
begin

definition enum_basis_finmap :: "nat => ('a =>F 'b) set" where
"enum_basis_finmap n =
(let m = from_nat n::('a =>F nat) in Pi' (domain m) (enum_basis o (m)F))"


lemma range_enum_basis_eq:
"range enum_basis_finmap = {Pi' I S|I S. finite I ∧ (∀i ∈ I. S i ∈ range enum_basis)}"
proof (auto simp: enum_basis_finmap_def[abs_def])
fix S::"('a => 'b set)" and I
assume "∀i∈I. S i ∈ range enum_basis"
hence "∀i∈I. ∃n. S i = enum_basis n" by auto
then obtain n where n: "∀i∈I. S i = enum_basis (n i)"
unfolding bchoice_iff by blast
assume [simp]: "finite I"
have "∃fm. domain fm = I ∧ (∀i∈I. n i = (fm i))"
by (rule finmap_choice) auto
then obtain m where "Pi' I S = Pi' (domain m) (enum_basis o m)"
using n by (auto simp: Pi'_def)
hence "Pi' I S = (let m = from_nat (to_nat m) in Pi' (domain m) (enum_basis o m))"
by simp
thus "Pi' I S ∈ range (λn. let m = from_nat n in Pi' (domain m) (enum_basis o m))"
by blast
qed (metis finite_domain o_apply rangeI)

lemma in_enum_basis_finmapI:
assumes "finite I" assumes "!!i. i ∈ I ==> S i ∈ range enum_basis"
shows "Pi' I S ∈ range enum_basis_finmap"
using assms unfolding range_enum_basis_eq by auto

lemma finmap_topological_basis:
"topological_basis (range (enum_basis_finmap))"
proof (subst topological_basis_iff, safe)
fix n::nat
show "open (enum_basis_finmap n::('a =>F 'b) set)" using enumerable_basis
by (auto intro!: open_Pi'I simp: topological_basis_def enum_basis_finmap_def Let_def)
next
fix O'::"('a =>F 'b) set" and x
assume "open O'" "x ∈ O'"
then obtain e where e: "e > 0" "!!y. dist y x < e ==> y ∈ O'" unfolding open_dist by blast
def e' "e / (card (domain x) + 1)"

have "∃B.
(∀i∈domain x. x i ∈ enum_basis (B i) ∧ enum_basis (B i) ⊆ ball (x i) e')"

proof (rule bchoice, safe)
fix i assume "i ∈ domain x"
have "open (ball (x i) e')" "x i ∈ ball (x i) e'" using e
by (auto simp add: e'_def intro!: divide_pos_pos)
from enumerable_basisE[OF this] guess b' .
thus "∃y. x i ∈ enum_basis y ∧
enum_basis y ⊆ ball (x i) e'"
by auto
qed
then guess B .. note B = this
def B' "Pi' (domain x) (λi. enum_basis (B i)::'b set)"
hence "B' ∈ range enum_basis_finmap" unfolding B'_def
by (intro in_enum_basis_finmapI) auto
moreover have "x ∈ B'" unfolding B'_def using B by auto
moreover have "B' ⊆ O'"
proof
fix y assume "y ∈ B'" with B have "domain y = domain x" unfolding B'_def
by (simp add: Pi'_def)
show "y ∈ O'"
proof (rule e)
have "dist y x = (∑i ∈ domain x. dist (y i) (x i))"
using `domain y = domain x` by (simp add: dist_finmap_def)
also have "… ≤ (∑i ∈ domain x. e')"
proof (rule setsum_mono)
fix i assume "i ∈ domain x"
with `y ∈ B'` B have "y i ∈ enum_basis (B i)"
by (simp add: Pi'_def B'_def)
hence "y i ∈ ball (x i) e'" using B `domain y = domain x` `i ∈ domain x`
by force
thus "dist (y i) (x i) ≤ e'" by (simp add: dist_commute)
qed
also have "… = card (domain x) * e'" by (simp add: real_eq_of_nat)
also have "… < e" using e by (simp add: e'_def field_simps)
finally show "dist y x < e" .
qed
qed
ultimately
show "∃B'∈range enum_basis_finmap. x ∈ B' ∧ B' ⊆ O'" by blast
qed

lemma range_enum_basis_finmap_imp_open:
assumes "x ∈ range enum_basis_finmap"
shows "open x"
using finmap_topological_basis assms by (auto simp: topological_basis_def)

lemma
open_imp_ex_UNION_of_enum:
fixes X::"('a =>F 'b) set"
assumes "open X" assumes "X ≠ {}"
shows "∃A::nat=>'a set. ∃B::nat=>('a => 'b set) . X = UNION UNIV (λi. Pi' (A i) (B i)) ∧
(∀n. ∀i∈A n. (B n) i ∈ range enum_basis) ∧ (∀n. finite (A n))"

proof -
from `open X` obtain B' where B': "B'⊆range enum_basis_finmap" "\<Union>B' = X"
using finmap_topological_basis by (force simp add: topological_basis_def)
then obtain B where B: "B' = enum_basis_finmap ` B" by (auto simp: subset_image_iff)
show ?thesis
proof cases
assume "B = {}" with B have "B' = {}" by simp hence False using B' assms by simp
thus ?thesis by simp
next
assume "B ≠ {}" then obtain b where b: "b ∈ B" by auto
def NA "λn::nat. if n ∈ B
then domain ((from_nat::_=>'a =>F nat) n)
else domain ((from_nat::_=>'a=>F nat) b)"

def NB "λn::nat. if n ∈ B
then (λi. (enum_basis::nat=>'b set) (((from_nat::_=>'a =>F nat) n) i))
else (λi. (enum_basis::nat=>'b set) (((from_nat::_=>'a =>F nat) b) i))"

have "X = UNION UNIV (λi. Pi' (NA i) (NB i))" unfolding B'(2)[symmetric] using b
unfolding B
by safe
(auto simp add: NA_def NB_def enum_basis_finmap_def Let_def o_def split: split_if_asm)
moreover
have "(∀n. ∀i∈NA n. (NB n) i ∈ range enum_basis)"
using enumerable_basis by (auto simp: topological_basis_def NA_def NB_def)
moreover have "(∀n. finite (NA n))" by (simp add: NA_def)
ultimately show ?thesis by auto
qed
qed

lemma
open_imp_ex_UNION:
fixes X::"('a =>F 'b) set"
assumes "open X" assumes "X ≠ {}"
shows "∃A::nat=>'a set. ∃B::nat=>('a => 'b set) . X = UNION UNIV (λi. Pi' (A i) (B i)) ∧
(∀n. ∀i∈A n. open ((B n) i)) ∧ (∀n. finite (A n))"

using open_imp_ex_UNION_of_enum[OF assms]
apply auto
apply (rule_tac x = A in exI)
apply (rule_tac x = B in exI)
apply (auto simp: open_enum_basis)
done

lemma
open_basisE:
assumes "open X" assumes "X ≠ {}"
obtains A::"nat=>'a set" and B::"nat=>('a => 'b set)" where
"X = UNION UNIV (λi. Pi' (A i) (B i))" "!!n i. i∈A n ==> open ((B n) i)" "!!n. finite (A n)"
using open_imp_ex_UNION[OF assms] by auto

lemma
open_basis_of_enumE:
assumes "open X" assumes "X ≠ {}"
obtains A::"nat=>'a set" and B::"nat=>('a => 'b set)" where
"X = UNION UNIV (λi. Pi' (A i) (B i))" "!!n i. i∈A n ==> (B n) i ∈ range enum_basis"
"!!n. finite (A n)"
using open_imp_ex_UNION_of_enum[OF assms] by auto

instance proof qed (blast intro: finmap_topological_basis)

end

subsection {* Product Measurable Space of Finite Maps *}

definition "PiF I M ≡
sigma
(\<Union>J ∈ I. (Π' j∈J. space (M j)))
{(Π' j∈J. X j) |X J. J ∈ I ∧ X ∈ (Π j∈J. sets (M j))}"


abbreviation
"PiF I M ≡ PiF I M"

syntax
"_PiF" :: "pttrn => 'i set => 'a measure => ('i => 'a) measure" ("(3PIF _:_./ _)" 10)

syntax (xsymbols)
"_PiF" :: "pttrn => 'i set => 'a measure => ('i => 'a) measure" ("(3ΠF _∈_./ _)" 10)

syntax (HTML output)
"_PiF" :: "pttrn => 'i set => 'a measure => ('i => 'a) measure" ("(3ΠF _∈_./ _)" 10)

translations
"PIF x:I. M" == "CONST PiF I (%x. M)"

lemma PiF_gen_subset: "{(Π' j∈J. X j) |X J. J ∈ I ∧ X ∈ (Π j∈J. sets (M j))} ⊆
Pow (\<Union>J ∈ I. (Π' j∈J. space (M j)))"

by (auto simp: Pi'_def) (blast dest: sets_into_space)

lemma space_PiF: "space (PiF I M) = (\<Union>J ∈ I. (Π' j∈J. space (M j)))"
unfolding PiF_def using PiF_gen_subset by (rule space_measure_of)

lemma sets_PiF:
"sets (PiF I M) = sigma_sets (\<Union>J ∈ I. (Π' j∈J. space (M j)))
{(Π' j∈J. X j) |X J. J ∈ I ∧ X ∈ (Π j∈J. sets (M j))}"

unfolding PiF_def using PiF_gen_subset by (rule sets_measure_of)

lemma sets_PiF_singleton:
"sets (PiF {I} M) = sigma_sets (Π' j∈I. space (M j))
{(Π' j∈I. X j) |X. X ∈ (Π j∈I. sets (M j))}"

unfolding sets_PiF by simp

lemma in_sets_PiFI:
assumes "X = (Pi' J S)" "J ∈ I" "!!i. i∈J ==> S i ∈ sets (M i)"
shows "X ∈ sets (PiF I M)"
unfolding sets_PiF
using assms by blast

lemma product_in_sets_PiFI:
assumes "J ∈ I" "!!i. i∈J ==> S i ∈ sets (M i)"
shows "(Pi' J S) ∈ sets (PiF I M)"
unfolding sets_PiF
using assms by blast

lemma singleton_space_subset_in_sets:
fixes J
assumes "J ∈ I"
assumes "finite J"
shows "space (PiF {J} M) ∈ sets (PiF I M)"
using assms
by (intro in_sets_PiFI[where J=J and S="λi. space (M i)"])
(auto simp: product_def space_PiF)

lemma singleton_subspace_set_in_sets:
assumes A: "A ∈ sets (PiF {J} M)"
assumes "finite J"
assumes "J ∈ I"
shows "A ∈ sets (PiF I M)"
using A[unfolded sets_PiF]
apply (induct A)
unfolding sets_PiF[symmetric] unfolding space_PiF[symmetric]
using assms
by (auto intro: in_sets_PiFI intro!: singleton_space_subset_in_sets)

lemma
finite_measurable_singletonI:
assumes "finite I"
assumes "!!J. J ∈ I ==> finite J"
assumes MN: "!!J. J ∈ I ==> A ∈ measurable (PiF {J} M) N"
shows "A ∈ measurable (PiF I M) N"
unfolding measurable_def
proof safe
fix y assume "y ∈ sets N"
have "A -` y ∩ space (PiF I M) = (\<Union>J∈I. A -` y ∩ space (PiF {J} M))"
by (auto simp: space_PiF)
also have "… ∈ sets (PiF I M)"
proof
show "finite I" by fact
fix J assume "J ∈ I"
with assms have "finite J" by simp
show "A -` y ∩ space (PiF {J} M) ∈ sets (PiF I M)"
by (rule singleton_subspace_set_in_sets[OF measurable_sets[OF assms(3)]]) fact+
qed
finally show "A -` y ∩ space (PiF I M) ∈ sets (PiF I M)" .
next
fix x assume "x ∈ space (PiF I M)" thus "A x ∈ space N"
using MN[of "domain x"]
by (auto simp: space_PiF measurable_space Pi'_def)
qed

lemma space_subset_in_sets:
fixes J::"'a::countable set set"
assumes "J ⊆ I"
assumes "!!j. j ∈ J ==> finite j"
shows "space (PiF J M) ∈ sets (PiF I M)"
proof -
have "space (PiF J M) = \<Union>{space (PiF {j} M)|j. j ∈ J}"
unfolding space_PiF by blast
also have "… ∈ sets (PiF I M)" using assms
by (intro countable_finite_comprehension) (auto simp: singleton_space_subset_in_sets)
finally show ?thesis .
qed

lemma subspace_set_in_sets:
fixes J::"'a::countable set set"
assumes A: "A ∈ sets (PiF J M)"
assumes "J ⊆ I"
assumes "!!j. j ∈ J ==> finite j"
shows "A ∈ sets (PiF I M)"
using A[unfolded sets_PiF]
apply (induct A)
unfolding sets_PiF[symmetric] unfolding space_PiF[symmetric]
using assms
by (auto intro: in_sets_PiFI intro!: space_subset_in_sets)

lemma finmap_eq_Un:
fixes X::"('a::countable =>F 'b) set"
shows "X = (\<Union>n. X ∩ {x. domain x = set (from_nat n)})"
proof -
let ?P = "λi. finite i"
let ?f = "λs. {x ∈ X. domain x = s}"
have "X = \<Union>{?f s |s. ?P s}" by auto
also have "… = (\<Union>n. let s = set (from_nat n) in if ?P s then ?f s else {})"
by (rule UN_finite_countable_eq_Un) simp
also have "… = (\<Union>n. {x ∈ X. domain x = set (from_nat n)})"
by (intro UN_cong) (auto simp: Let_def space_PiF)
finally show ?thesis by auto
qed

lemma
countable_measurable_PiFI:
fixes I::"'a::countable set set"
assumes MN: "!!J. J ∈ I ==> finite J ==> A ∈ measurable (PiF {J} M) N"
shows "A ∈ measurable (PiF I M) N"
unfolding measurable_def
proof safe
fix y assume "y ∈ sets N"
hence "A -` y ∩ space (PiF I M) = (\<Union>n. A -` y ∩ space (PiF ({set (from_nat n)}∩I) M))"
by (subst finmap_eq_Un) (auto simp: space_PiF Pi'_def)
also have "… ∈ sets (PiF I M)"
apply (intro Int countable_nat_UN subsetI, safe)
apply (case_tac "set (from_nat i) ∈ I")
apply simp_all
apply (rule singleton_subspace_set_in_sets[OF measurable_sets[OF MN]])
using assms `y ∈ sets N`
apply (auto simp: space_PiF)
done
finally show "A -` y ∩ space (PiF I M) ∈ sets (PiF I M)" .
next
fix x assume "x ∈ space (PiF I M)" thus "A x ∈ space N"
using MN[of "domain x"] by (auto simp: space_PiF measurable_space Pi'_def)
qed

lemma measurable_PiF:
assumes f: "!!x. x ∈ space N ==> domain (f x) ∈ I ∧ (∀i∈domain (f x). (f x) i ∈ space (M i))"
assumes S: "!!J S. J ∈ I ==> (!!i. i ∈ J ==> S i ∈ sets (M i)) ==>
f -` (Pi' J S) ∩ space N ∈ sets N"

shows "f ∈ measurable N (PiF I M)"
unfolding PiF_def
using PiF_gen_subset
apply (rule measurable_measure_of)
using f apply force
apply (insert S, auto)
done

lemma
restrict_sets_measurable:
assumes A: "A ∈ sets (PiF I M)" and "J ⊆ I"
shows "A ∩ {m. domain m ∈ J} ∈ sets (PiF J M)"
using A[unfolded sets_PiF]
apply (induct A)
unfolding sets_PiF[symmetric] unfolding space_PiF[symmetric]
proof -
fix a assume "a ∈ {Pi' J X |X J. J ∈ I ∧ X ∈ (Π j∈J. sets (M j))}"
then obtain K S where S: "a = Pi' K S" "K ∈ I" "(∀i∈K. S i ∈ sets (M i))"
by auto
show "a ∩ {m. domain m ∈ J} ∈ sets (PiF J M)"
proof cases
assume "K ∈ J"
hence "a ∩ {m. domain m ∈ J} ∈ {Pi' K X |X K. K ∈ J ∧ X ∈ (Π j∈K. sets (M j))}" using S
by (auto intro!: exI[where x=K] exI[where x=S] simp: Pi'_def)
also have "… ⊆ sets (PiF J M)" unfolding sets_PiF by auto
finally show ?thesis .
next
assume "K ∉ J"
hence "a ∩ {m. domain m ∈ J} = {}" using S by (auto simp: Pi'_def)
also have "… ∈ sets (PiF J M)" by simp
finally show ?thesis .
qed
next
show "{} ∩ {m. domain m ∈ J} ∈ sets (PiF J M)" by simp
next
fix a :: "nat => _"
assume a: "(!!i. a i ∩ {m. domain m ∈ J} ∈ sets (PiF J M))"
have "UNION UNIV a ∩ {m. domain m ∈ J} = (\<Union>i. (a i ∩ {m. domain m ∈ J}))"
by simp
also have "… ∈ sets (PiF J M)" using a by (intro countable_nat_UN) auto
finally show "UNION UNIV a ∩ {m. domain m ∈ J} ∈ sets (PiF J M)" .
next
fix a assume a: "a ∩ {m. domain m ∈ J} ∈ sets (PiF J M)"
have "(space (PiF I M) - a) ∩ {m. domain m ∈ J} = (space (PiF J M) - (a ∩ {m. domain m ∈ J}))"
using `J ⊆ I` by (auto simp: space_PiF Pi'_def)
also have "… ∈ sets (PiF J M)" using a by auto
finally show "(space (PiF I M) - a) ∩ {m. domain m ∈ J} ∈ sets (PiF J M)" .
qed

lemma measurable_finmap_of:
assumes f: "!!i. (∃x ∈ space N. i ∈ J x) ==> (λx. f x i) ∈ measurable N (M i)"
assumes J: "!!x. x ∈ space N ==> J x ∈ I" "!!x. x ∈ space N ==> finite (J x)"
assumes JN: "!!S. {x. J x = S} ∩ space N ∈ sets N"
shows "(λx. finmap_of (J x) (f x)) ∈ measurable N (PiF I M)"
proof (rule measurable_PiF)
fix x assume "x ∈ space N"
with J[of x] measurable_space[OF f]
show "domain (finmap_of (J x) (f x)) ∈ I ∧
(∀i∈domain (finmap_of (J x) (f x)). (finmap_of (J x) (f x)) i ∈ space (M i))"

by auto
next
fix K S assume "K ∈ I" and *: "!!i. i ∈ K ==> S i ∈ sets (M i)"
with J have eq: "(λx. finmap_of (J x) (f x)) -` Pi' K S ∩ space N =
(if ∃x ∈ space N. K = J x ∧ finite K then if K = {} then {x ∈ space N. J x = K}
else (\<Inter>i∈K. (λx. f x i) -` S i ∩ {x ∈ space N. J x = K}) else {})"

by (auto simp: Pi'_def)
have r: "{x ∈ space N. J x = K} = space N ∩ ({x. J x = K} ∩ space N)" by auto
show "(λx. finmap_of (J x) (f x)) -` Pi' K S ∩ space N ∈ sets N"
unfolding eq r
apply (simp del: INT_simps add: )
apply (intro conjI impI finite_INT JN Int[OF top])
apply simp apply assumption
apply (subst Int_assoc[symmetric])
apply (rule Int)
apply (intro measurable_sets[OF f] *) apply force apply assumption
apply (intro JN)
done
qed

lemma measurable_PiM_finmap_of:
assumes "finite J"
shows "finmap_of J ∈ measurable (PiM J M) (PiF {J} M)"
apply (rule measurable_finmap_of)
apply (rule measurable_component_singleton)
apply simp
apply rule
apply (rule `finite J`)
apply simp
done

lemma proj_measurable_singleton:
assumes "A ∈ sets (M i)" "finite I"
shows "(λx. (x)F i) -` A ∩ space (PiF {I} M) ∈ sets (PiF {I} M)"
proof cases
assume "i ∈ I"
hence "(λx. (x)F i) -` A ∩ space (PiF {I} M) =
Pi' I (λx. if x = i then A else space (M x))"

using sets_into_space[OF ] `A ∈ sets (M i)` assms
by (auto simp: space_PiF Pi'_def)
thus ?thesis using assms `A ∈ sets (M i)`
by (intro in_sets_PiFI) auto
next
assume "i ∉ I"
hence "(λx. (x)F i) -` A ∩ space (PiF {I} M) =
(if undefined ∈ A then space (PiF {I} M) else {})"
by (auto simp: space_PiF Pi'_def)
thus ?thesis by simp
qed

lemma measurable_proj_singleton:
fixes I
assumes "finite I" "i ∈ I"
shows "(λx. (x)F i) ∈ measurable (PiF {I} M) (M i)"
proof (unfold measurable_def, intro CollectI conjI ballI proj_measurable_singleton assms)
qed (insert `i ∈ I`, auto simp: space_PiF)

lemma measurable_proj_countable:
fixes I::"'a::countable set set"
assumes "y ∈ space (M i)"
shows "(λx. if i ∈ domain x then (x)F i else y) ∈ measurable (PiF I M) (M i)"
proof (rule countable_measurable_PiFI)
fix J assume "J ∈ I" "finite J"
show "(λx. if i ∈ domain x then x i else y) ∈ measurable (PiF {J} M) (M i)"
unfolding measurable_def
proof safe
fix z assume "z ∈ sets (M i)"
have "(λx. if i ∈ domain x then x i else y) -` z ∩ space (PiF {J} M) =
(λx. if i ∈ J then (x)F i else y) -` z ∩ space (PiF {J} M)"

by (auto simp: space_PiF Pi'_def)
also have "… ∈ sets (PiF {J} M)" using `z ∈ sets (M i)` `finite J`
by (cases "i ∈ J") (auto intro!: measurable_sets[OF measurable_proj_singleton])
finally show "(λx. if i ∈ domain x then x i else y) -` z ∩ space (PiF {J} M) ∈
sets (PiF {J} M)"
.
qed (insert `y ∈ space (M i)`, auto simp: space_PiF Pi'_def)
qed

lemma measurable_restrict_proj:
assumes "J ∈ II" "finite J"
shows "finmap_of J ∈ measurable (PiM J M) (PiF II M)"
using assms
by (intro measurable_finmap_of measurable_component_singleton) auto

lemma
measurable_proj_PiM:
fixes J K ::"'a::countable set" and I::"'a set set"
assumes "finite J" "J ∈ I"
assumes "x ∈ space (PiM J M)"
shows "proj ∈
measurable (PiF {J} M) (PiM J M)"

proof (rule measurable_PiM_single)
show "proj ∈ space (PiF {J} M) -> (ΠE i ∈ J. space (M i))"
using assms by (auto simp add: space_PiM space_PiF extensional_def sets_PiF Pi'_def)
next
fix A i assume A: "i ∈ J" "A ∈ sets (M i)"
show "{ω ∈ space (PiF {J} M). (ω)F i ∈ A} ∈ sets (PiF {J} M)"
proof
have "{ω ∈ space (PiF {J} M). (ω)F i ∈ A} =
(λω. (ω)F i) -` A ∩ space (PiF {J} M)"
by auto
also have "… ∈ sets (PiF {J} M)"
using assms A by (auto intro: measurable_sets[OF measurable_proj_singleton] simp: space_PiM)
finally show ?thesis .
qed simp
qed

lemma sets_subspaceI:
assumes "A ∩ space M ∈ sets M"
assumes "B ∈ sets M"
shows "A ∩ B ∈ sets M"
using assms
proof -
have "A ∩ B = (A ∩ space M) ∩ B"
using assms sets_into_space by auto
thus ?thesis using assms by auto
qed

lemma space_PiF_singleton_eq_product:
assumes "finite I"
shows "space (PiF {I} M) = (Π' i∈I. space (M i))"
by (auto simp: product_def space_PiF assms)

text {* adapted from @{thm sets_PiM_single} *}

lemma sets_PiF_single:
assumes "finite I" "I ≠ {}"
shows "sets (PiF {I} M) =
sigma_sets (Π' i∈I. space (M i))
{{f∈Π' i∈I. space (M i). f i ∈ A} | i A. i ∈ I ∧ A ∈ sets (M i)}"

(is "_ = sigma_sets ?Ω ?R")
unfolding sets_PiF_singleton
proof (rule sigma_sets_eqI)
interpret R: sigma_algebra "sigma_sets ?Ω ?R" by (rule sigma_algebra_sigma_sets) auto
fix A assume "A ∈ {Pi' I X |X. X ∈ (Π j∈I. sets (M j))}"
then obtain X where X: "A = Pi' I X" "X ∈ (Π j∈I. sets (M j))" by auto
show "A ∈ sigma_sets ?Ω ?R"
proof -
from `I ≠ {}` X have "A = (\<Inter>j∈I. {f∈space (PiF {I} M). f j ∈ X j})"
using sets_into_space
by (auto simp: space_PiF product_def) blast
also have "… ∈ sigma_sets ?Ω ?R"
using X `I ≠ {}` assms by (intro R.finite_INT) (auto simp: space_PiF)
finally show "A ∈ sigma_sets ?Ω ?R" .
qed
next
fix A assume "A ∈ ?R"
then obtain i B where A: "A = {f∈Π' i∈I. space (M i). f i ∈ B}" "i ∈ I" "B ∈ sets (M i)"
by auto
then have "A = (Π' j ∈ I. if j = i then B else space (M j))"
using sets_into_space[OF A(3)]
apply (auto simp: Pi'_iff split: split_if_asm)
apply blast
done
also have "… ∈ sigma_sets ?Ω {Pi' I X |X. X ∈ (Π j∈I. sets (M j))}"
using A
by (intro sigma_sets.Basic )
(auto intro: exI[where x="λj. if j = i then B else space (M j)"])
finally show "A ∈ sigma_sets ?Ω {Pi' I X |X. X ∈ (Π j∈I. sets (M j))}" .
qed

text {* adapted from @{thm PiE_cong} *}

lemma Pi'_cong:
assumes "finite I"
assumes "!!i. i ∈ I ==> f i = g i"
shows "Pi' I f = Pi' I g"
using assms by (auto simp: Pi'_def)

text {* adapted from @{thm Pi_UN} *}

lemma Pi'_UN:
fixes A :: "nat => 'i => 'a set"
assumes "finite I"
assumes mono: "!!i n m. i ∈ I ==> n ≤ m ==> A n i ⊆ A m i"
shows "(\<Union>n. Pi' I (A n)) = Pi' I (λi. \<Union>n. A n i)"
proof (intro set_eqI iffI)
fix f assume "f ∈ Pi' I (λi. \<Union>n. A n i)"
then have "∀i∈I. ∃n. f i ∈ A n i" "domain f = I" by (auto simp: `finite I` Pi'_def)
from bchoice[OF this(1)] obtain n where n: "!!i. i ∈ I ==> f i ∈ (A (n i) i)" by auto
obtain k where k: "!!i. i ∈ I ==> n i ≤ k"
using `finite I` finite_nat_set_iff_bounded_le[of "n`I"] by auto
have "f ∈ Pi' I (λi. A k i)"
proof
fix i assume "i ∈ I"
from mono[OF this, of "n i" k] k[OF this] n[OF this] `domain f = I` `i ∈ I`
show "f i ∈ A k i " by (auto simp: `finite I`)
qed (simp add: `domain f = I` `finite I`)
then show "f ∈ (\<Union>n. Pi' I (A n))" by auto
qed (auto simp: Pi'_def `finite I`)

text {* adapted from @{thm sigma_prod_algebra_sigma_eq} *}

lemma sigma_fprod_algebra_sigma_eq:
fixes E :: "'i => 'a set set"
assumes [simp]: "finite I" "I ≠ {}"
assumes S_mono: "!!i. i ∈ I ==> incseq (S i)"
and S_union: "!!i. i ∈ I ==> (\<Union>j. S i j) = space (M i)"
and S_in_E: "!!i. i ∈ I ==> range (S i) ⊆ E i"
assumes E_closed: "!!i. i ∈ I ==> E i ⊆ Pow (space (M i))"
and E_generates: "!!i. i ∈ I ==> sets (M i) = sigma_sets (space (M i)) (E i)"
defines "P == { Pi' I F | F. ∀i∈I. F i ∈ E i }"
shows "sets (PiF {I} M) = sigma_sets (space (PiF {I} M)) P"
proof
let ?P = "sigma (space (PiF {I} M)) P"
have P_closed: "P ⊆ Pow (space (PiF {I} M))"
using E_closed by (auto simp: space_PiF P_def Pi'_iff subset_eq)
then have space_P: "space ?P = (Π' i∈I. space (M i))"
by (simp add: space_PiF)
have "sets (PiF {I} M) =
sigma_sets (space ?P) {{f ∈ Π' i∈I. space (M i). f i ∈ A} |i A. i ∈ I ∧ A ∈ sets (M i)}"

using sets_PiF_single[of I M] by (simp add: space_P)
also have "… ⊆ sets (sigma (space (PiF {I} M)) P)"
proof (safe intro!: sigma_sets_subset)
fix i A assume "i ∈ I" and A: "A ∈ sets (M i)"
have "(λx. (x)F i) ∈ measurable ?P (sigma (space (M i)) (E i))"
proof (subst measurable_iff_measure_of)
show "E i ⊆ Pow (space (M i))" using `i ∈ I` by fact
from space_P `i ∈ I` show "(λx. (x)F i) ∈ space ?P -> space (M i)"
by auto
show "∀A∈E i. (λx. (x)F i) -` A ∩ space ?P ∈ sets ?P"
proof
fix A assume A: "A ∈ E i"
then have "(λx. (x)F i) -` A ∩ space ?P = (Π' j∈I. if i = j then A else space (M j))"
using E_closed `i ∈ I` by (auto simp: space_P Pi_iff subset_eq split: split_if_asm)
also have "… = (Π' j∈I. \<Union>n. if i = j then A else S j n)"
by (intro Pi'_cong) (simp_all add: S_union)
also have "… = (\<Union>n. Π' j∈I. if i = j then A else S j n)"
using S_mono
by (subst Pi'_UN[symmetric, OF `finite I`]) (auto simp: incseq_def)
also have "… ∈ sets ?P"
proof (safe intro!: countable_UN)
fix n show "(Π' j∈I. if i = j then A else S j n) ∈ sets ?P"
using A S_in_E
by (simp add: P_closed)
(auto simp: P_def subset_eq intro!: exI[of _ "λj. if i = j then A else S j n"])
qed
finally show "(λx. (x)F i) -` A ∩ space ?P ∈ sets ?P"
using P_closed by simp
qed
qed
from measurable_sets[OF this, of A] A `i ∈ I` E_closed
have "(λx. (x)F i) -` A ∩ space ?P ∈ sets ?P"
by (simp add: E_generates)
also have "(λx. (x)F i) -` A ∩ space ?P = {f ∈ Π' i∈I. space (M i). f i ∈ A}"
using P_closed by (auto simp: space_PiF)
finally show "… ∈ sets ?P" .
qed
finally show "sets (PiF {I} M) ⊆ sigma_sets (space (PiF {I} M)) P"
by (simp add: P_closed)
show "sigma_sets (space (PiF {I} M)) P ⊆ sets (PiF {I} M)"
using `finite I` `I ≠ {}`
by (auto intro!: sigma_sets_subset product_in_sets_PiFI simp: E_generates P_def)
qed

lemma enumerable_sigma_fprod_algebra_sigma_eq:
assumes "I ≠ {}"
assumes [simp]: "finite I"
shows "sets (PiF {I} (λ_. borel)) = sigma_sets (space (PiF {I} (λ_. borel)))
{Pi' I F |F. (∀i∈I. F i ∈ range enum_basis)}"

proof -
from open_incseqE[OF open_UNIV] guess S::"nat => 'b set" . note S = this
show ?thesis
proof (rule sigma_fprod_algebra_sigma_eq)
show "finite I" by simp
show "I ≠ {}" by fact
show "incseq S" "(\<Union>j. S j) = space borel" "range S ⊆ range enum_basis"
using S by simp_all
show "range enum_basis ⊆ Pow (space borel)" by simp
show "sets borel = sigma_sets (space borel) (range enum_basis)"
using borel_eq_sigma_enum_basis .
qed
qed

text {* adapted from @{thm enumerable_sigma_fprod_algebra_sigma_eq} *}

lemma enumerable_sigma_prod_algebra_sigma_eq:
assumes "I ≠ {}"
assumes [simp]: "finite I"
shows "sets (PiM I (λ_. borel)) = sigma_sets (space (PiM I (λ_. borel)))
{PiE I F |F. ∀i∈I. F i ∈ range enum_basis}"

proof -
from open_incseqE[OF open_UNIV] guess S::"nat => 'b set" . note S = this
show ?thesis
proof (rule sigma_prod_algebra_sigma_eq)
show "finite I" by simp note[[show_types]]
fix i show "incseq S" "(\<Union>j. S j) = space borel" "range S ⊆ range enum_basis"
using S by simp_all
show "range enum_basis ⊆ Pow (space borel)" by simp
show "sets borel = sigma_sets (space borel) (range enum_basis)"
using borel_eq_sigma_enum_basis .
qed
qed

lemma product_open_generates_sets_PiF_single:
assumes "I ≠ {}"
assumes [simp]: "finite I"
shows "sets (PiF {I} (λ_. borel::'b::enumerable_basis measure)) =
sigma_sets (space (PiF {I} (λ_. borel))) {Pi' I F |F. (∀i∈I. F i ∈ Collect open)}"

proof -
from open_incseqE[OF open_UNIV] guess S::"nat => 'b set" . note S = this
show ?thesis
proof (rule sigma_fprod_algebra_sigma_eq)
show "finite I" by simp
show "I ≠ {}" by fact
show "incseq S" "(\<Union>j. S j) = space borel" "range S ⊆ Collect open"
using S by (auto simp: open_enum_basis)
show "Collect open ⊆ Pow (space borel)" by simp
show "sets borel = sigma_sets (space borel) (Collect open)"
by (simp add: borel_def)
qed
qed

lemma product_open_generates_sets_PiM:
assumes "I ≠ {}"
assumes [simp]: "finite I"
shows "sets (PiM I (λ_. borel::'b::enumerable_basis measure)) =
sigma_sets (space (PiM I (λ_. borel))) {PiE I F |F. ∀i∈I. F i ∈ Collect open}"

proof -
from open_incseqE[OF open_UNIV] guess S::"nat => 'b set" . note S = this
show ?thesis
proof (rule sigma_prod_algebra_sigma_eq)
show "finite I" by simp note[[show_types]]
fix i show "incseq S" "(\<Union>j. S j) = space borel" "range S ⊆ Collect open"
using S by (auto simp: open_enum_basis)
show "Collect open ⊆ Pow (space borel)" by simp
show "sets borel = sigma_sets (space borel) (Collect open)"
by (simp add: borel_def)
qed
qed

lemma finmap_UNIV[simp]: "(\<Union>J∈Collect finite. J \<leadsto> UNIV) = UNIV" by auto

lemma borel_eq_PiF_borel:
shows "(borel :: ('i::countable =>F 'a::polish_space) measure) =
PiF (Collect finite) (λ_. borel :: 'a measure)"

proof (rule measure_eqI)
have C: "Collect finite ≠ {}" by auto
show "sets (borel::('i =>F 'a) measure) = sets (PiF (Collect finite) (λ_. borel))"
proof
show "sets (borel::('i =>F 'a) measure) ⊆ sets (PiF (Collect finite) (λ_. borel))"
apply (simp add: borel_def sets_PiF)
proof (rule sigma_sets_mono, safe, cases)
fix X::"('i =>F 'a) set" assume "open X" "X ≠ {}"
from open_basisE[OF this] guess NA NB . note N = this
hence "X = (\<Union>i. Pi' (NA i) (NB i))" by simp
also have "… ∈
sigma_sets UNIV {Pi' J S |S J. finite J ∧ S ∈ J -> sigma_sets UNIV (Collect open)}"

using N by (intro Union sigma_sets.Basic) blast
finally show "X ∈ sigma_sets UNIV
{Pi' J X |X J. finite J ∧ X ∈ J -> sigma_sets UNIV (Collect open)}"
.
qed (auto simp: Empty)
next
show "sets (PiF (Collect finite) (λ_. borel)) ⊆ sets (borel::('i =>F 'a) measure)"
proof
fix x assume x: "x ∈ sets (PiF (Collect finite::'i set set) (λ_. borel::'a measure))"
hence x_sp: "x ⊆ space (PiF (Collect finite) (λ_. borel))" by (rule sets_into_space)
from finmap_eq_Un have "x = (\<Union>n. x ∩ {xa. domain xa = set (from_nat n)})"
(is "_ = (\<Union>n. ?rx n)")
.
also have "… ∈ sets borel"
proof (rule countable_nat_UN, safe)
fix i
{ assume ef: "set (from_nat i) = ({}::'i set)"
{ assume e: "(?rx i) = {}"
hence "(?rx i) ∈ sets borel" unfolding e by simp
} moreover {
assume "(?rx i) ≠ {}"
then obtain f where "f ∈ x" "domain f = {}" using ef by auto
hence "(?rx i) = {f}" using `set (from_nat i) = {}`
by (auto simp: finmap_eq_iff)
also have "{f} ∈ sets borel" by simp
finally have "(?rx i) ∈ sets borel" .
} ultimately have "(?rx i) ∈ sets borel" by blast
} moreover {
assume "set (from_nat i) ≠ ({}::'i set)"
from open_incseqE[OF open_UNIV] guess S::"nat => 'a set" . note S = this
have "(?rx i) = x ∩ {m. domain m ∈ {set (from_nat i)}}" by auto
also have "… ∈ sets (PiF {set (from_nat i)} (λ_. borel))"
using x apply (rule restrict_sets_measurable) by (simp add: enum_finite_def)
also have "… = sigma_sets (space (PiF {set (from_nat i)} (λ_. borel)))
{Pi' (set (from_nat i)) F |F. (∀j∈set (from_nat i). F j ∈ range enum_basis)}"

(is "_ = sigma_sets _ ?P")
by (rule enumerable_sigma_fprod_algebra_sigma_eq[OF `set (from_nat i) ≠ {}`])
(simp add: enum_finite_def)
also have "… ⊆ sets borel"
proof
fix x
assume "x ∈ sigma_sets (space (PiF {set (from_nat i)} (λ_. borel))) ?P"
thus "x ∈ sets borel"
proof (rule sigma_sets.induct, safe)
fix F::"'i => 'a set"
assume "∀j∈set (from_nat i). F j ∈ range enum_basis"
hence "Pi' (set (from_nat i)) F ∈ range enum_basis_finmap"
unfolding range_enum_basis_eq by auto
hence "open (Pi' (set (from_nat i)) F)" by (rule range_enum_basis_finmap_imp_open)
thus "Pi' (set (from_nat i)) F ∈ sets borel" by simp
next
fix a::"('i =>F 'a) set"
have "space (PiF {set (from_nat i)::'i set} (λ_. borel::'a measure)) =
Pi' (set (from_nat i)) (λ_. UNIV)"

by (auto simp: space_PiF product_def enum_finite_def)
moreover have "open (Pi' (set (from_nat i)::'i set) (λ_. UNIV::'a set))"
by (intro open_Pi'I) (auto simp: enum_finite_def)
ultimately
have "space (PiF {set (from_nat i)::'i set} (λ_. borel::'a measure)) ∈ sets borel"
by simp
moreover
assume "a ∈ sets borel"
ultimately show "space (PiF {set (from_nat i)} (λ_. borel)) - a ∈ sets borel" ..
qed auto
qed
finally have "(?rx i) ∈ sets borel" .
} ultimately show "(?rx i) ∈ sets borel" by blast
qed
finally show "x ∈ sets (borel)" .
qed
qed
qed (simp add: emeasure_sigma borel_def PiF_def)


subsection {* Measure preservation *}

text {* Measure preservation is not used at the moment. *}

definition "measure_preserving f A B <-> f ∈ measurable A B ∧ (∀x ∈ sets B. distr A B f x = B x)"

lemma
assumes "measure_preserving f A B"
shows measure_preserving_distr: "!!x. x ∈ sets B ==> distr A B f x = B x"
and measure_preserving_measurable: "f ∈ measurable A B"
using assms by (auto simp: measure_preserving_def)

lemma measure_preservingI:
assumes "f ∈ measurable A B" "!!x. x ∈ sets B ==> distr A B f x = B x"
shows "measure_preserving f A B"
using assms by (auto simp: measure_preserving_def)

lemma measure_preservingI'[intro]:
assumes AB: "f ∈ measurable A B"
assumes m: "!!x. x ∈ sets B ==> emeasure A (f -` x ∩ space A) = emeasure B x"
shows "measure_preserving f A B"
apply (rule measure_preservingI[OF AB])
apply (subst emeasure_distr[OF AB])
apply assumption
apply (rule m)
apply assumption
done

lemma
measure_preserving_comp:
assumes AB: "measure_preserving f A B"
assumes BC: "measure_preserving g B C"
shows "measure_preserving (g o f) A C"
proof
note mAB = measure_preserving_measurable[OF AB]
note mBC = measure_preserving_measurable[OF BC]
show "g o f ∈ measurable A C"
using mAB mBC ..
fix x assume "x ∈ sets C"
hence "C x = distr B C g x"
by (rule measure_preserving_distr[OF BC, symmetric])
also have "… = B (g -` x ∩ space B)"
using mBC `x ∈ sets C` by (rule emeasure_distr)
also have "… = distr A B f (g -` x ∩ space B)"
using measurable_sets[OF mBC `x ∈ sets C`]
by (rule measure_preserving_distr[OF AB, symmetric])
also have "… = emeasure A (f -` (g -` x ∩ space B) ∩ space A)"
using mAB measurable_sets[OF mBC `x ∈ sets C`]
by (rule emeasure_distr)
also have "… = emeasure A (f -` g -` x ∩ (f -` space B ∩ space A))"
by (simp add: Int_assoc)
also have "f -` space B ∩ space A = space A"
using sets_into_space[OF measurable_sets[OF mAB top]] measurable_space[OF mAB]
by auto
finally show "emeasure A ((g o f) -` x ∩ space A) = emeasure C x"
by (simp add: vimage_compose)
qed

subsection {* Isomorphism between Functions and Finite Maps *}

lemma
measurable_compose:
fixes f::"'a => 'b"
assumes inj: "!!j. j ∈ J ==> f' (f j) = j"
assumes "finite J"
shows "(λm. compose J m f) ∈ measurable (PiM (f ` J) (λ_. M)) (PiM J (λ_. M))"
proof (rule measurable_PiM)
show "(λm. compose J m f)
∈ space (PiM (f ` J) (λ_. M)) ->
(J -> space M) ∩ extensional J"

proof safe
fix x and i
assume x: "x ∈ space (PiM (f ` J) (λ_. M))" "i ∈ J"
with inj show "compose J x f i ∈ space M"
by (auto simp: space_PiM compose_def)
next
fix x assume "x ∈ space (PiM (f ` J) (λ_. M))"
show "(compose J x f) ∈ extensional J" by (rule compose_extensional)
qed
next
fix S X
have inv: "!!j. j ∈ f ` J ==> f (f' j) = j" using assms by auto
assume S: "S ≠ {} ∨ J = {}" "finite S" "S ⊆ J" and P: "!!i. i ∈ S ==> X i ∈ sets M"
have "(λm. compose J m f) -` prod_emb J (λ_. M) S (PiE S X) ∩
space (PiM (f ` J) (λ_. M)) = prod_emb (f ` J) (λ_. M) (f ` S) (PiE (f ` S) (λb. X (f' b)))"

using assms inv S sets_into_space[OF P]
by (force simp: prod_emb_iff compose_def space_PiM extensional_def Pi_def intro: imageI)
also have "… ∈ sets (PiM (f ` J) (λ_. M))"
proof
from S show "f ` S ⊆ f ` J" by auto
show "(ΠE b∈f ` S. X (f' b)) ∈ sets (PiM (f ` S) (λ_. M))"
proof
show "finite (f ` S)" using S by simp
fix i assume "i ∈ f ` S" hence "f' i ∈ S" using S assms by auto
thus "X (f' i) ∈ sets M" by (rule P)
qed
qed
finally show "(λm. compose J m f) -` prod_emb J (λ_. M) S (PiE S X) ∩
space (PiM (f ` J) (λ_. M)) ∈ sets (PiM (f ` J) (λ_. M))"
.
qed

lemma
measurable_compose_inv:
fixes f::"'a => 'b"
assumes inj: "!!j. j ∈ J ==> f' (f j) = j"
assumes "finite J"
shows "(λm. compose (f ` J) m f') ∈ measurable (PiM J (λ_. M)) (PiM (f ` J) (λ_. M))"
proof -
have "(λm. compose (f ` J) m f') ∈ measurable (PiM (f' ` f ` J) (λ_. M)) (PiM (f ` J) (λ_. M))"
using assms by (auto intro: measurable_compose)
moreover
from inj have "f' ` f ` J = J" by (metis (hide_lams, mono_tags) image_iff set_eqI)
ultimately show ?thesis by simp
qed

locale function_to_finmap =
fixes J::"'a set" and f :: "'a => 'b::countable" and f'
assumes [simp]: "finite J"
assumes inv: "i ∈ J ==> f' (f i) = i"
begin

text {* to measure finmaps *}

definition "fm = (finmap_of (f ` J)) o (λg. compose (f ` J) g f')"

lemma domain_fm[simp]: "domain (fm x) = f ` J"
unfolding fm_def by simp

lemma fm_restrict[simp]: "fm (restrict y J) = fm y"
unfolding fm_def by (auto simp: compose_def inv intro: restrict_ext)

lemma fm_product:
assumes "!!i. space (M i) = UNIV"
shows "fm -` Pi' (f ` J) S ∩ space (PiM J M) = (ΠE j ∈ J. S (f j))"
using assms
by (auto simp: inv fm_def compose_def space_PiM Pi'_def)

lemma fm_measurable:
assumes "f ` J ∈ N"
shows "fm ∈ measurable (PiM J (λ_. M)) (PiF N (λ_. M))"
unfolding fm_def
proof (rule measurable_comp, rule measurable_compose_inv)
show "finmap_of (f ` J) ∈ measurable (PiM (f ` J) (λ_. M)) (PiF N (λ_. M)) "
using assms by (intro measurable_finmap_of measurable_component_singleton) auto
qed (simp_all add: inv)

lemma proj_fm:
assumes "x ∈ J"
shows "fm m (f x) = m x"
using assms by (auto simp: fm_def compose_def o_def inv)

lemma inj_on_compose_f': "inj_on (λg. compose (f ` J) g f') (extensional J)"
proof (rule inj_on_inverseI)
fix x::"'a => 'c" assume "x ∈ extensional J"
thus "(λx. compose J x f) (compose (f ` J) x f') = x"
by (auto simp: compose_def inv extensional_def)
qed

lemma inj_on_fm:
assumes "!!i. space (M i) = UNIV"
shows "inj_on fm (space (PiM J M))"
using assms
apply (auto simp: fm_def space_PiM)
apply (rule comp_inj_on)
apply (rule inj_on_compose_f')
apply (rule finmap_of_inj_on_extensional_finite)
apply simp
apply (auto)
done

lemma fm_vimage_image_eq:
assumes "!!i. space (M i) = UNIV"
assumes "X ∈ sets (PiM J M)"
shows "fm -` fm ` X ∩ space (PiM J M) = X"
using assms
by (intro inj_on_vimage_image_eq inj_on_fm)
(auto simp: sets_into_space)

text {* to measure functions *}

definition "mf = (λg. compose J g f) o proj"

lemma
assumes "x ∈ space (PiM J (λ_. M))" "finite J"
shows "proj (finmap_of J x) = x"
using assms by (auto simp: space_PiM extensional_def)

lemma
assumes "x ∈ space (PiF {J} (λ_. M))"
shows "finmap_of J (proj x) = x"
using assms by (auto simp: space_PiF Pi'_def finmap_eq_iff)

lemma mf_fm:
assumes "x ∈ space (PiM J (λ_. M))"
shows "mf (fm x) = x"
proof -
have "mf (fm x) ∈ extensional J"
by (auto simp: mf_def extensional_def compose_def)
moreover
have "x ∈ extensional J" using assms sets_into_space
by (force simp: space_PiM)
moreover
{ fix i assume "i ∈ J"
hence "mf (fm x) i = x i"
by (auto simp: inv mf_def compose_def fm_def)
}
ultimately
show ?thesis by (rule extensionalityI)
qed

lemma mf_measurable:
assumes "space M = UNIV"
shows "mf ∈ measurable (PiF {f ` J} (λ_. M)) (PiM J (λ_. M))"
unfolding mf_def
proof (rule measurable_comp, rule measurable_proj_PiM)
show "(λg. compose J g f) ∈
measurable (PiM (f ` J) (λx. M)) (PiM J (λ_. M))"

by (rule measurable_compose, rule inv) auto
qed (auto simp add: space_PiM extensional_def assms)

lemma fm_image_measurable:
assumes "space M = UNIV"
assumes "X ∈ sets (PiM J (λ_. M))"
shows "fm ` X ∈ sets (PiF {f ` J} (λ_. M))"
proof -
have "fm ` X = (mf) -` X ∩ space (PiF {f ` J} (λ_. M))"
proof safe
fix x assume "x ∈ X"
with mf_fm[of x] sets_into_space[OF assms(2)] show "fm x ∈ mf -` X" by auto
show "fm x ∈ space (PiF {f ` J} (λ_. M))" by (simp add: space_PiF assms)
next
fix y x
assume x: "mf y ∈ X"
assume y: "y ∈ space (PiF {f ` J} (λ_. M))"
thus "y ∈ fm ` X"
by (intro image_eqI[OF _ x], unfold finmap_eq_iff)
(auto simp: space_PiF fm_def mf_def compose_def inv Pi'_def)
qed
also have "… ∈ sets (PiF {f ` J} (λ_. M))"
using assms
by (intro measurable_sets[OF mf_measurable]) auto
finally show ?thesis .
qed

lemma fm_image_measurable_finite:
assumes "space M = UNIV"
assumes "X ∈ sets (PiM J (λ_. M::'c measure))"
shows "fm ` X ∈ sets (PiF (Collect finite) (λ_. M::'c measure))"
using fm_image_measurable[OF assms]
by (rule subspace_set_in_sets) (auto simp: finite_subset)

text {* measure on finmaps *}

definition "mapmeasure M N = distr M (PiF (Collect finite) N) (fm)"

lemma sets_mapmeasure[simp]: "sets (mapmeasure M N) = sets (PiF (Collect finite) N)"
unfolding mapmeasure_def by simp

lemma space_mapmeasure[simp]: "space (mapmeasure M N) = space (PiF (Collect finite) N)"
unfolding mapmeasure_def by simp

lemma mapmeasure_PiF:
assumes s1: "space M = space (PiM J (λ_. N))"
assumes s2: "sets M = (PiM J (λ_. N))"
assumes "space N = UNIV"
assumes "X ∈ sets (PiF (Collect finite) (λ_. N))"
shows "emeasure (mapmeasure M (λ_. N)) X = emeasure M ((fm -` X ∩ extensional J))"
using assms
by (auto simp: measurable_eqI[OF s1 refl s2 refl] mapmeasure_def emeasure_distr
fm_measurable space_PiM)

lemma mapmeasure_PiM:
fixes N::"'c measure"
assumes s1: "space M = space (PiM J (λ_. N))"
assumes s2: "sets M = (PiM J (λ_. N))"
assumes N: "space N = UNIV"
assumes X: "X ∈ sets M"
shows "emeasure M X = emeasure (mapmeasure M (λ_. N)) (fm ` X)"
unfolding mapmeasure_def
proof (subst emeasure_distr, subst measurable_eqI[OF s1 refl s2 refl], rule fm_measurable)
from fm_vimage_image_eq[OF `space N = UNIV` X[simplified s2], simplified s1[symmetric]]
show "emeasure M X = emeasure M (fm -` fm ` X ∩ space M)"
by simp
show "fm ` X ∈ sets (PiF (Collect finite) (λ_. N))"
by (rule fm_image_measurable_finite[OF N X[simplified s2]])
qed simp

end

end