Theory Finite_Cartesian_Product

theory Finite_Cartesian_Product
imports Euclidean_Space Numeral_Type
(*  Title:      HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
Author: Amine Chaieb, University of Cambridge
*)


header {* Definition of finite Cartesian product types. *}

theory Finite_Cartesian_Product
imports
Euclidean_Space
L2_Norm
"~~/src/HOL/Library/Numeral_Type"
begin

subsection {* Finite Cartesian products, with indexing and lambdas. *}

typedef ('a, 'b) vec = "UNIV :: (('b::finite) => 'a) set"
morphisms vec_nth vec_lambda ..

notation
vec_nth (infixl "$" 90) and
vec_lambda (binder "χ" 10)

(*
Translate "'b ^ 'n" into "'b ^ ('n :: finite)". When 'n has already more than
the finite type class write "vec 'b 'n"
*)


syntax "_finite_vec" :: "type => type => type" ("(_ ^/ _)" [15, 16] 15)

parse_translation {*
let
fun vec t u = Syntax.const @{type_syntax vec} $ t $ u;
fun finite_vec_tr [t, u] =
(case Term_Position.strip_positions u of
v as Free (x, _) =>
if Lexicon.is_tid x then
vec t (Syntax.const @{syntax_const "_ofsort"} $ v $
Syntax.const @{class_syntax finite})
else vec t u
| _ => vec t u)
in
[(@{syntax_const "_finite_vec"}, K finite_vec_tr)]
end
*}


lemma vec_eq_iff: "(x = y) <-> (∀i. x$i = y$i)"
by (simp add: vec_nth_inject [symmetric] fun_eq_iff)

lemma vec_lambda_beta [simp]: "vec_lambda g $ i = g i"
by (simp add: vec_lambda_inverse)

lemma vec_lambda_unique: "(∀i. f$i = g i) <-> vec_lambda g = f"
by (auto simp add: vec_eq_iff)

lemma vec_lambda_eta: "(χ i. (g$i)) = g"
by (simp add: vec_eq_iff)


subsection {* Group operations and class instances *}

instantiation vec :: (zero, finite) zero
begin
definition "0 ≡ (χ i. 0)"
instance ..
end

instantiation vec :: (plus, finite) plus
begin
definition "op + ≡ (λ x y. (χ i. x$i + y$i))"
instance ..
end

instantiation vec :: (minus, finite) minus
begin
definition "op - ≡ (λ x y. (χ i. x$i - y$i))"
instance ..
end

instantiation vec :: (uminus, finite) uminus
begin
definition "uminus ≡ (λ x. (χ i. - (x$i)))"
instance ..
end

lemma zero_index [simp]: "0 $ i = 0"
unfolding zero_vec_def by simp

lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
unfolding plus_vec_def by simp

lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
unfolding minus_vec_def by simp

lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
unfolding uminus_vec_def by simp

instance vec :: (semigroup_add, finite) semigroup_add
by default (simp add: vec_eq_iff add_assoc)

instance vec :: (ab_semigroup_add, finite) ab_semigroup_add
by default (simp add: vec_eq_iff add_commute)

instance vec :: (monoid_add, finite) monoid_add
by default (simp_all add: vec_eq_iff)

instance vec :: (comm_monoid_add, finite) comm_monoid_add
by default (simp add: vec_eq_iff)

instance vec :: (cancel_semigroup_add, finite) cancel_semigroup_add
by default (simp_all add: vec_eq_iff)

instance vec :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
by default (simp add: vec_eq_iff)

instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..

instance vec :: (group_add, finite) group_add
by default (simp_all add: vec_eq_iff diff_minus)

instance vec :: (ab_group_add, finite) ab_group_add
by default (simp_all add: vec_eq_iff)


subsection {* Real vector space *}

instantiation vec :: (real_vector, finite) real_vector
begin

definition "scaleR ≡ (λ r x. (χ i. scaleR r (x$i)))"

lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)"
unfolding scaleR_vec_def by simp

instance
by default (simp_all add: vec_eq_iff scaleR_left_distrib scaleR_right_distrib)

end


subsection {* Topological space *}

instantiation vec :: (topological_space, finite) topological_space
begin

definition
"open (S :: ('a ^ 'b) set) <->
(∀x∈S. ∃A. (∀i. open (A i) ∧ x$i ∈ A i) ∧
(∀y. (∀i. y$i ∈ A i) --> y ∈ S))"


instance proof
show "open (UNIV :: ('a ^ 'b) set)"
unfolding open_vec_def by auto
next
fix S T :: "('a ^ 'b) set"
assume "open S" "open T" thus "open (S ∩ T)"
unfolding open_vec_def
apply clarify
apply (drule (1) bspec)+
apply (clarify, rename_tac Sa Ta)
apply (rule_tac x="λi. Sa i ∩ Ta i" in exI)
apply (simp add: open_Int)
done
next
fix K :: "('a ^ 'b) set set"
assume "∀S∈K. open S" thus "open (\<Union>K)"
unfolding open_vec_def
apply clarify
apply (drule (1) bspec)
apply (drule (1) bspec)
apply clarify
apply (rule_tac x=A in exI)
apply fast
done
qed

end

lemma open_vector_box: "∀i. open (S i) ==> open {x. ∀i. x $ i ∈ S i}"
unfolding open_vec_def by auto

lemma open_vimage_vec_nth: "open S ==> open ((λx. x $ i) -` S)"
unfolding open_vec_def
apply clarify
apply (rule_tac x="λk. if k = i then S else UNIV" in exI, simp)
done

lemma closed_vimage_vec_nth: "closed S ==> closed ((λx. x $ i) -` S)"
unfolding closed_open vimage_Compl [symmetric]
by (rule open_vimage_vec_nth)

lemma closed_vector_box: "∀i. closed (S i) ==> closed {x. ∀i. x $ i ∈ S i}"
proof -
have "{x. ∀i. x $ i ∈ S i} = (\<Inter>i. (λx. x $ i) -` S i)" by auto
thus "∀i. closed (S i) ==> closed {x. ∀i. x $ i ∈ S i}"
by (simp add: closed_INT closed_vimage_vec_nth)
qed

lemma tendsto_vec_nth [tendsto_intros]:
assumes "((λx. f x) ---> a) net"
shows "((λx. f x $ i) ---> a $ i) net"
proof (rule topological_tendstoI)
fix S assume "open S" "a $ i ∈ S"
then have "open ((λy. y $ i) -` S)" "a ∈ ((λy. y $ i) -` S)"
by (simp_all add: open_vimage_vec_nth)
with assms have "eventually (λx. f x ∈ (λy. y $ i) -` S) net"
by (rule topological_tendstoD)
then show "eventually (λx. f x $ i ∈ S) net"
by simp
qed

lemma isCont_vec_nth [simp]: "isCont f a ==> isCont (λx. f x $ i) a"
unfolding isCont_def by (rule tendsto_vec_nth)

lemma vec_tendstoI:
assumes "!!i. ((λx. f x $ i) ---> a $ i) net"
shows "((λx. f x) ---> a) net"
proof (rule topological_tendstoI)
fix S assume "open S" and "a ∈ S"
then obtain A where A: "!!i. open (A i)" "!!i. a $ i ∈ A i"
and S: "!!y. ∀i. y $ i ∈ A i ==> y ∈ S"
unfolding open_vec_def by metis
have "!!i. eventually (λx. f x $ i ∈ A i) net"
using assms A by (rule topological_tendstoD)
hence "eventually (λx. ∀i. f x $ i ∈ A i) net"
by (rule eventually_all_finite)
thus "eventually (λx. f x ∈ S) net"
by (rule eventually_elim1, simp add: S)
qed

lemma tendsto_vec_lambda [tendsto_intros]:
assumes "!!i. ((λx. f x i) ---> a i) net"
shows "((λx. χ i. f x i) ---> (χ i. a i)) net"
using assms by (simp add: vec_tendstoI)

lemma open_image_vec_nth: assumes "open S" shows "open ((λx. x $ i) ` S)"
proof (rule openI)
fix a assume "a ∈ (λx. x $ i) ` S"
then obtain z where "a = z $ i" and "z ∈ S" ..
then obtain A where A: "∀i. open (A i) ∧ z $ i ∈ A i"
and S: "∀y. (∀i. y $ i ∈ A i) --> y ∈ S"
using `open S` unfolding open_vec_def by auto
hence "A i ⊆ (λx. x $ i) ` S"
by (clarsimp, rule_tac x="χ j. if j = i then x else z $ j" in image_eqI,
simp_all)
hence "open (A i) ∧ a ∈ A i ∧ A i ⊆ (λx. x $ i) ` S"
using A `a = z $ i` by simp
then show "∃T. open T ∧ a ∈ T ∧ T ⊆ (λx. x $ i) ` S" by - (rule exI)
qed

instance vec :: (perfect_space, finite) perfect_space
proof
fix x :: "'a ^ 'b" show "¬ open {x}"
proof
assume "open {x}"
hence "∀i. open ((λx. x $ i) ` {x})" by (fast intro: open_image_vec_nth)
hence "∀i. open {x $ i}" by simp
thus "False" by (simp add: not_open_singleton)
qed
qed


subsection {* Metric space *}

instantiation vec :: (metric_space, finite) metric_space
begin

definition
"dist x y = setL2 (λi. dist (x$i) (y$i)) UNIV"

lemma dist_vec_nth_le: "dist (x $ i) (y $ i) ≤ dist x y"
unfolding dist_vec_def by (rule member_le_setL2) simp_all

instance proof
fix x y :: "'a ^ 'b"
show "dist x y = 0 <-> x = y"
unfolding dist_vec_def
by (simp add: setL2_eq_0_iff vec_eq_iff)
next
fix x y z :: "'a ^ 'b"
show "dist x y ≤ dist x z + dist y z"
unfolding dist_vec_def
apply (rule order_trans [OF _ setL2_triangle_ineq])
apply (simp add: setL2_mono dist_triangle2)
done
next
fix S :: "('a ^ 'b) set"
show "open S <-> (∀x∈S. ∃e>0. ∀y. dist y x < e --> y ∈ S)"
proof
assume "open S" show "∀x∈S. ∃e>0. ∀y. dist y x < e --> y ∈ S"
proof
fix x assume "x ∈ S"
obtain A where A: "∀i. open (A i)" "∀i. x $ i ∈ A i"
and S: "∀y. (∀i. y $ i ∈ A i) --> y ∈ S"
using `open S` and `x ∈ S` unfolding open_vec_def by metis
have "∀i∈UNIV. ∃r>0. ∀y. dist y (x $ i) < r --> y ∈ A i"
using A unfolding open_dist by simp
hence "∃r. ∀i∈UNIV. 0 < r i ∧ (∀y. dist y (x $ i) < r i --> y ∈ A i)"
by (rule finite_set_choice [OF finite])
then obtain r where r1: "∀i. 0 < r i"
and r2: "∀i y. dist y (x $ i) < r i --> y ∈ A i" by fast
have "0 < Min (range r) ∧ (∀y. dist y x < Min (range r) --> y ∈ S)"
by (simp add: r1 r2 S le_less_trans [OF dist_vec_nth_le])
thus "∃e>0. ∀y. dist y x < e --> y ∈ S" ..
qed
next
assume *: "∀x∈S. ∃e>0. ∀y. dist y x < e --> y ∈ S" show "open S"
proof (unfold open_vec_def, rule)
fix x assume "x ∈ S"
then obtain e where "0 < e" and S: "∀y. dist y x < e --> y ∈ S"
using * by fast
def r "λi::'b. e / sqrt (of_nat CARD('b))"
from `0 < e` have r: "∀i. 0 < r i"
unfolding r_def by (simp_all add: divide_pos_pos)
from `0 < e` have e: "e = setL2 r UNIV"
unfolding r_def by (simp add: setL2_constant)
def A "λi. {y. dist (x $ i) y < r i}"
have "∀i. open (A i) ∧ x $ i ∈ A i"
unfolding A_def by (simp add: open_ball r)
moreover have "∀y. (∀i. y $ i ∈ A i) --> y ∈ S"
by (simp add: A_def S dist_vec_def e setL2_strict_mono dist_commute)
ultimately show "∃A. (∀i. open (A i) ∧ x $ i ∈ A i) ∧
(∀y. (∀i. y $ i ∈ A i) --> y ∈ S)"
by metis
qed
qed
qed

end

lemma Cauchy_vec_nth:
"Cauchy (λn. X n) ==> Cauchy (λn. X n $ i)"
unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le])

lemma vec_CauchyI:
fixes X :: "nat => 'a::metric_space ^ 'n"
assumes X: "!!i. Cauchy (λn. X n $ i)"
shows "Cauchy (λn. X n)"
proof (rule metric_CauchyI)
fix r :: real assume "0 < r"
then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
by (simp add: divide_pos_pos)
def N "λi. LEAST N. ∀m≥N. ∀n≥N. dist (X m $ i) (X n $ i) < ?s"
def M "Max (range N)"
have "!!i. ∃N. ∀m≥N. ∀n≥N. dist (X m $ i) (X n $ i) < ?s"
using X `0 < ?s` by (rule metric_CauchyD)
hence "!!i. ∀m≥N i. ∀n≥N i. dist (X m $ i) (X n $ i) < ?s"
unfolding N_def by (rule LeastI_ex)
hence M: "!!i. ∀m≥M. ∀n≥M. dist (X m $ i) (X n $ i) < ?s"
unfolding M_def by simp
{
fix m n :: nat
assume "M ≤ m" "M ≤ n"
have "dist (X m) (X n) = setL2 (λi. dist (X m $ i) (X n $ i)) UNIV"
unfolding dist_vec_def ..
also have "… ≤ setsum (λi. dist (X m $ i) (X n $ i)) UNIV"
by (rule setL2_le_setsum [OF zero_le_dist])
also have "… < setsum (λi::'n. ?s) UNIV"
by (rule setsum_strict_mono, simp_all add: M `M ≤ m` `M ≤ n`)
also have "… = r"
by simp
finally have "dist (X m) (X n) < r" .
}
hence "∀m≥M. ∀n≥M. dist (X m) (X n) < r"
by simp
then show "∃M. ∀m≥M. ∀n≥M. dist (X m) (X n) < r" ..
qed

instance vec :: (complete_space, finite) complete_space
proof
fix X :: "nat => 'a ^ 'b" assume "Cauchy X"
have "!!i. (λn. X n $ i) ----> lim (λn. X n $ i)"
using Cauchy_vec_nth [OF `Cauchy X`]
by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
hence "X ----> vec_lambda (λi. lim (λn. X n $ i))"
by (simp add: vec_tendstoI)
then show "convergent X"
by (rule convergentI)
qed


subsection {* Normed vector space *}

instantiation vec :: (real_normed_vector, finite) real_normed_vector
begin

definition "norm x = setL2 (λi. norm (x$i)) UNIV"

definition "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"

instance proof
fix a :: real and x y :: "'a ^ 'b"
show "norm x = 0 <-> x = 0"
unfolding norm_vec_def
by (simp add: setL2_eq_0_iff vec_eq_iff)
show "norm (x + y) ≤ norm x + norm y"
unfolding norm_vec_def
apply (rule order_trans [OF _ setL2_triangle_ineq])
apply (simp add: setL2_mono norm_triangle_ineq)
done
show "norm (scaleR a x) = ¦a¦ * norm x"
unfolding norm_vec_def
by (simp add: setL2_right_distrib)
show "sgn x = scaleR (inverse (norm x)) x"
by (rule sgn_vec_def)
show "dist x y = norm (x - y)"
unfolding dist_vec_def norm_vec_def
by (simp add: dist_norm)
qed

end

lemma norm_nth_le: "norm (x $ i) ≤ norm x"
unfolding norm_vec_def
by (rule member_le_setL2) simp_all

lemma bounded_linear_vec_nth: "bounded_linear (λx. x $ i)"
apply default
apply (rule vector_add_component)
apply (rule vector_scaleR_component)
apply (rule_tac x="1" in exI, simp add: norm_nth_le)
done

instance vec :: (banach, finite) banach ..


subsection {* Inner product space *}

instantiation vec :: (real_inner, finite) real_inner
begin

definition "inner x y = setsum (λi. inner (x$i) (y$i)) UNIV"

instance proof
fix r :: real and x y z :: "'a ^ 'b"
show "inner x y = inner y x"
unfolding inner_vec_def
by (simp add: inner_commute)
show "inner (x + y) z = inner x z + inner y z"
unfolding inner_vec_def
by (simp add: inner_add_left setsum_addf)
show "inner (scaleR r x) y = r * inner x y"
unfolding inner_vec_def
by (simp add: setsum_right_distrib)
show "0 ≤ inner x x"
unfolding inner_vec_def
by (simp add: setsum_nonneg)
show "inner x x = 0 <-> x = 0"
unfolding inner_vec_def
by (simp add: vec_eq_iff setsum_nonneg_eq_0_iff)
show "norm x = sqrt (inner x x)"
unfolding inner_vec_def norm_vec_def setL2_def
by (simp add: power2_norm_eq_inner)
qed

end


subsection {* Euclidean space *}

text {* Vectors pointing along a single axis. *}

definition "axis k x = (χ i. if i = k then x else 0)"

lemma axis_nth [simp]: "axis i x $ i = x"
unfolding axis_def by simp

lemma axis_eq_axis: "axis i x = axis j y <-> x = y ∧ i = j ∨ x = 0 ∧ y = 0"
unfolding axis_def vec_eq_iff by auto

lemma inner_axis_axis:
"inner (axis i x) (axis j y) = (if i = j then inner x y else 0)"
unfolding inner_vec_def
apply (cases "i = j")
apply clarsimp
apply (subst setsum_diff1' [where a=j], simp_all)
apply (rule setsum_0', simp add: axis_def)
apply (rule setsum_0', simp add: axis_def)
done

lemma setsum_single:
assumes "finite A" and "k ∈ A" and "f k = y"
assumes "!!i. i ∈ A ==> i ≠ k ==> f i = 0"
shows "(∑i∈A. f i) = y"
apply (subst setsum_diff1' [OF assms(1,2)])
apply (simp add: setsum_0' assms(3,4))
done

lemma inner_axis: "inner x (axis i y) = inner (x $ i) y"
unfolding inner_vec_def
apply (rule_tac k=i in setsum_single)
apply simp_all
apply (simp add: axis_def)
done

instantiation vec :: (euclidean_space, finite) euclidean_space
begin

definition "Basis = (\<Union>i. \<Union>u∈Basis. {axis i u})"

instance proof
show "(Basis :: ('a ^ 'b) set) ≠ {}"
unfolding Basis_vec_def by simp
next
show "finite (Basis :: ('a ^ 'b) set)"
unfolding Basis_vec_def by simp
next
fix u v :: "'a ^ 'b"
assume "u ∈ Basis" and "v ∈ Basis"
thus "inner u v = (if u = v then 1 else 0)"
unfolding Basis_vec_def
by (auto simp add: inner_axis_axis axis_eq_axis inner_Basis)
next
fix x :: "'a ^ 'b"
show "(∀u∈Basis. inner x u = 0) <-> x = 0"
unfolding Basis_vec_def
by (simp add: inner_axis euclidean_all_zero_iff vec_eq_iff)
qed

lemma DIM_cart[simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
apply (simp add: Basis_vec_def)
apply (subst card_UN_disjoint)
apply simp
apply simp
apply (auto simp: axis_eq_axis) [1]
apply (subst card_UN_disjoint)
apply (auto simp: axis_eq_axis)
done

end

end