src/HOL/Analysis/Finite_Cartesian_Product.thy
changeset 63627 6ddb43c6b711
parent 63040 eb4ddd18d635
child 63918 6bf55e6e0b75
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Analysis/Finite_Cartesian_Product.thy	Mon Aug 08 14:13:14 2016 +0200
     1.3 @@ -0,0 +1,558 @@
     1.4 +(*  Title:      HOL/Analysis/Finite_Cartesian_Product.thy
     1.5 +    Author:     Amine Chaieb, University of Cambridge
     1.6 +*)
     1.7 +
     1.8 +section \<open>Definition of finite Cartesian product types.\<close>
     1.9 +
    1.10 +theory Finite_Cartesian_Product
    1.11 +imports
    1.12 +  Euclidean_Space
    1.13 +  L2_Norm
    1.14 +  "~~/src/HOL/Library/Numeral_Type"
    1.15 +begin
    1.16 +
    1.17 +subsection \<open>Finite Cartesian products, with indexing and lambdas.\<close>
    1.18 +
    1.19 +typedef ('a, 'b) vec = "UNIV :: (('b::finite) \<Rightarrow> 'a) set"
    1.20 +  morphisms vec_nth vec_lambda ..
    1.21 +
    1.22 +notation
    1.23 +  vec_nth (infixl "$" 90) and
    1.24 +  vec_lambda (binder "\<chi>" 10)
    1.25 +
    1.26 +(*
    1.27 +  Translate "'b ^ 'n" into "'b ^ ('n :: finite)". When 'n has already more than
    1.28 +  the finite type class write "vec 'b 'n"
    1.29 +*)
    1.30 +
    1.31 +syntax "_finite_vec" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ ^/ _)" [15, 16] 15)
    1.32 +
    1.33 +parse_translation \<open>
    1.34 +  let
    1.35 +    fun vec t u = Syntax.const @{type_syntax vec} $ t $ u;
    1.36 +    fun finite_vec_tr [t, u] =
    1.37 +      (case Term_Position.strip_positions u of
    1.38 +        v as Free (x, _) =>
    1.39 +          if Lexicon.is_tid x then
    1.40 +            vec t (Syntax.const @{syntax_const "_ofsort"} $ v $
    1.41 +              Syntax.const @{class_syntax finite})
    1.42 +          else vec t u
    1.43 +      | _ => vec t u)
    1.44 +  in
    1.45 +    [(@{syntax_const "_finite_vec"}, K finite_vec_tr)]
    1.46 +  end
    1.47 +\<close>
    1.48 +
    1.49 +lemma vec_eq_iff: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
    1.50 +  by (simp add: vec_nth_inject [symmetric] fun_eq_iff)
    1.51 +
    1.52 +lemma vec_lambda_beta [simp]: "vec_lambda g $ i = g i"
    1.53 +  by (simp add: vec_lambda_inverse)
    1.54 +
    1.55 +lemma vec_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> vec_lambda g = f"
    1.56 +  by (auto simp add: vec_eq_iff)
    1.57 +
    1.58 +lemma vec_lambda_eta: "(\<chi> i. (g$i)) = g"
    1.59 +  by (simp add: vec_eq_iff)
    1.60 +
    1.61 +
    1.62 +subsection \<open>Group operations and class instances\<close>
    1.63 +
    1.64 +instantiation vec :: (zero, finite) zero
    1.65 +begin
    1.66 +  definition "0 \<equiv> (\<chi> i. 0)"
    1.67 +  instance ..
    1.68 +end
    1.69 +
    1.70 +instantiation vec :: (plus, finite) plus
    1.71 +begin
    1.72 +  definition "op + \<equiv> (\<lambda> x y. (\<chi> i. x$i + y$i))"
    1.73 +  instance ..
    1.74 +end
    1.75 +
    1.76 +instantiation vec :: (minus, finite) minus
    1.77 +begin
    1.78 +  definition "op - \<equiv> (\<lambda> x y. (\<chi> i. x$i - y$i))"
    1.79 +  instance ..
    1.80 +end
    1.81 +
    1.82 +instantiation vec :: (uminus, finite) uminus
    1.83 +begin
    1.84 +  definition "uminus \<equiv> (\<lambda> x. (\<chi> i. - (x$i)))"
    1.85 +  instance ..
    1.86 +end
    1.87 +
    1.88 +lemma zero_index [simp]: "0 $ i = 0"
    1.89 +  unfolding zero_vec_def by simp
    1.90 +
    1.91 +lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
    1.92 +  unfolding plus_vec_def by simp
    1.93 +
    1.94 +lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
    1.95 +  unfolding minus_vec_def by simp
    1.96 +
    1.97 +lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
    1.98 +  unfolding uminus_vec_def by simp
    1.99 +
   1.100 +instance vec :: (semigroup_add, finite) semigroup_add
   1.101 +  by standard (simp add: vec_eq_iff add.assoc)
   1.102 +
   1.103 +instance vec :: (ab_semigroup_add, finite) ab_semigroup_add
   1.104 +  by standard (simp add: vec_eq_iff add.commute)
   1.105 +
   1.106 +instance vec :: (monoid_add, finite) monoid_add
   1.107 +  by standard (simp_all add: vec_eq_iff)
   1.108 +
   1.109 +instance vec :: (comm_monoid_add, finite) comm_monoid_add
   1.110 +  by standard (simp add: vec_eq_iff)
   1.111 +
   1.112 +instance vec :: (cancel_semigroup_add, finite) cancel_semigroup_add
   1.113 +  by standard (simp_all add: vec_eq_iff)
   1.114 +
   1.115 +instance vec :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
   1.116 +  by standard (simp_all add: vec_eq_iff diff_diff_eq)
   1.117 +
   1.118 +instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
   1.119 +
   1.120 +instance vec :: (group_add, finite) group_add
   1.121 +  by standard (simp_all add: vec_eq_iff)
   1.122 +
   1.123 +instance vec :: (ab_group_add, finite) ab_group_add
   1.124 +  by standard (simp_all add: vec_eq_iff)
   1.125 +
   1.126 +
   1.127 +subsection \<open>Real vector space\<close>
   1.128 +
   1.129 +instantiation vec :: (real_vector, finite) real_vector
   1.130 +begin
   1.131 +
   1.132 +definition "scaleR \<equiv> (\<lambda> r x. (\<chi> i. scaleR r (x$i)))"
   1.133 +
   1.134 +lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)"
   1.135 +  unfolding scaleR_vec_def by simp
   1.136 +
   1.137 +instance
   1.138 +  by standard (simp_all add: vec_eq_iff scaleR_left_distrib scaleR_right_distrib)
   1.139 +
   1.140 +end
   1.141 +
   1.142 +
   1.143 +subsection \<open>Topological space\<close>
   1.144 +
   1.145 +instantiation vec :: (topological_space, finite) topological_space
   1.146 +begin
   1.147 +
   1.148 +definition [code del]:
   1.149 +  "open (S :: ('a ^ 'b) set) \<longleftrightarrow>
   1.150 +    (\<forall>x\<in>S. \<exists>A. (\<forall>i. open (A i) \<and> x$i \<in> A i) \<and>
   1.151 +      (\<forall>y. (\<forall>i. y$i \<in> A i) \<longrightarrow> y \<in> S))"
   1.152 +
   1.153 +instance proof
   1.154 +  show "open (UNIV :: ('a ^ 'b) set)"
   1.155 +    unfolding open_vec_def by auto
   1.156 +next
   1.157 +  fix S T :: "('a ^ 'b) set"
   1.158 +  assume "open S" "open T" thus "open (S \<inter> T)"
   1.159 +    unfolding open_vec_def
   1.160 +    apply clarify
   1.161 +    apply (drule (1) bspec)+
   1.162 +    apply (clarify, rename_tac Sa Ta)
   1.163 +    apply (rule_tac x="\<lambda>i. Sa i \<inter> Ta i" in exI)
   1.164 +    apply (simp add: open_Int)
   1.165 +    done
   1.166 +next
   1.167 +  fix K :: "('a ^ 'b) set set"
   1.168 +  assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
   1.169 +    unfolding open_vec_def
   1.170 +    apply clarify
   1.171 +    apply (drule (1) bspec)
   1.172 +    apply (drule (1) bspec)
   1.173 +    apply clarify
   1.174 +    apply (rule_tac x=A in exI)
   1.175 +    apply fast
   1.176 +    done
   1.177 +qed
   1.178 +
   1.179 +end
   1.180 +
   1.181 +lemma open_vector_box: "\<forall>i. open (S i) \<Longrightarrow> open {x. \<forall>i. x $ i \<in> S i}"
   1.182 +  unfolding open_vec_def by auto
   1.183 +
   1.184 +lemma open_vimage_vec_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
   1.185 +  unfolding open_vec_def
   1.186 +  apply clarify
   1.187 +  apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI, simp)
   1.188 +  done
   1.189 +
   1.190 +lemma closed_vimage_vec_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
   1.191 +  unfolding closed_open vimage_Compl [symmetric]
   1.192 +  by (rule open_vimage_vec_nth)
   1.193 +
   1.194 +lemma closed_vector_box: "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
   1.195 +proof -
   1.196 +  have "{x. \<forall>i. x $ i \<in> S i} = (\<Inter>i. (\<lambda>x. x $ i) -` S i)" by auto
   1.197 +  thus "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
   1.198 +    by (simp add: closed_INT closed_vimage_vec_nth)
   1.199 +qed
   1.200 +
   1.201 +lemma tendsto_vec_nth [tendsto_intros]:
   1.202 +  assumes "((\<lambda>x. f x) \<longlongrightarrow> a) net"
   1.203 +  shows "((\<lambda>x. f x $ i) \<longlongrightarrow> a $ i) net"
   1.204 +proof (rule topological_tendstoI)
   1.205 +  fix S assume "open S" "a $ i \<in> S"
   1.206 +  then have "open ((\<lambda>y. y $ i) -` S)" "a \<in> ((\<lambda>y. y $ i) -` S)"
   1.207 +    by (simp_all add: open_vimage_vec_nth)
   1.208 +  with assms have "eventually (\<lambda>x. f x \<in> (\<lambda>y. y $ i) -` S) net"
   1.209 +    by (rule topological_tendstoD)
   1.210 +  then show "eventually (\<lambda>x. f x $ i \<in> S) net"
   1.211 +    by simp
   1.212 +qed
   1.213 +
   1.214 +lemma isCont_vec_nth [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. f x $ i) a"
   1.215 +  unfolding isCont_def by (rule tendsto_vec_nth)
   1.216 +
   1.217 +lemma vec_tendstoI:
   1.218 +  assumes "\<And>i. ((\<lambda>x. f x $ i) \<longlongrightarrow> a $ i) net"
   1.219 +  shows "((\<lambda>x. f x) \<longlongrightarrow> a) net"
   1.220 +proof (rule topological_tendstoI)
   1.221 +  fix S assume "open S" and "a \<in> S"
   1.222 +  then obtain A where A: "\<And>i. open (A i)" "\<And>i. a $ i \<in> A i"
   1.223 +    and S: "\<And>y. \<forall>i. y $ i \<in> A i \<Longrightarrow> y \<in> S"
   1.224 +    unfolding open_vec_def by metis
   1.225 +  have "\<And>i. eventually (\<lambda>x. f x $ i \<in> A i) net"
   1.226 +    using assms A by (rule topological_tendstoD)
   1.227 +  hence "eventually (\<lambda>x. \<forall>i. f x $ i \<in> A i) net"
   1.228 +    by (rule eventually_all_finite)
   1.229 +  thus "eventually (\<lambda>x. f x \<in> S) net"
   1.230 +    by (rule eventually_mono, simp add: S)
   1.231 +qed
   1.232 +
   1.233 +lemma tendsto_vec_lambda [tendsto_intros]:
   1.234 +  assumes "\<And>i. ((\<lambda>x. f x i) \<longlongrightarrow> a i) net"
   1.235 +  shows "((\<lambda>x. \<chi> i. f x i) \<longlongrightarrow> (\<chi> i. a i)) net"
   1.236 +  using assms by (simp add: vec_tendstoI)
   1.237 +
   1.238 +lemma open_image_vec_nth: assumes "open S" shows "open ((\<lambda>x. x $ i) ` S)"
   1.239 +proof (rule openI)
   1.240 +  fix a assume "a \<in> (\<lambda>x. x $ i) ` S"
   1.241 +  then obtain z where "a = z $ i" and "z \<in> S" ..
   1.242 +  then obtain A where A: "\<forall>i. open (A i) \<and> z $ i \<in> A i"
   1.243 +    and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
   1.244 +    using \<open>open S\<close> unfolding open_vec_def by auto
   1.245 +  hence "A i \<subseteq> (\<lambda>x. x $ i) ` S"
   1.246 +    by (clarsimp, rule_tac x="\<chi> j. if j = i then x else z $ j" in image_eqI,
   1.247 +      simp_all)
   1.248 +  hence "open (A i) \<and> a \<in> A i \<and> A i \<subseteq> (\<lambda>x. x $ i) ` S"
   1.249 +    using A \<open>a = z $ i\<close> by simp
   1.250 +  then show "\<exists>T. open T \<and> a \<in> T \<and> T \<subseteq> (\<lambda>x. x $ i) ` S" by - (rule exI)
   1.251 +qed
   1.252 +
   1.253 +instance vec :: (perfect_space, finite) perfect_space
   1.254 +proof
   1.255 +  fix x :: "'a ^ 'b" show "\<not> open {x}"
   1.256 +  proof
   1.257 +    assume "open {x}"
   1.258 +    hence "\<forall>i. open ((\<lambda>x. x $ i) ` {x})" by (fast intro: open_image_vec_nth)
   1.259 +    hence "\<forall>i. open {x $ i}" by simp
   1.260 +    thus "False" by (simp add: not_open_singleton)
   1.261 +  qed
   1.262 +qed
   1.263 +
   1.264 +
   1.265 +subsection \<open>Metric space\<close>
   1.266 +(* TODO: Product of uniform spaces and compatibility with metric_spaces! *)
   1.267 +
   1.268 +instantiation vec :: (metric_space, finite) dist
   1.269 +begin
   1.270 +
   1.271 +definition
   1.272 +  "dist x y = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
   1.273 +
   1.274 +instance ..
   1.275 +end
   1.276 +
   1.277 +instantiation vec :: (metric_space, finite) uniformity_dist
   1.278 +begin
   1.279 +
   1.280 +definition [code del]:
   1.281 +  "(uniformity :: (('a, 'b) vec \<times> ('a, 'b) vec) filter) =
   1.282 +    (INF e:{0 <..}. principal {(x, y). dist x y < e})"
   1.283 +
   1.284 +instance
   1.285 +  by standard (rule uniformity_vec_def)
   1.286 +end
   1.287 +
   1.288 +declare uniformity_Abort[where 'a="'a :: metric_space ^ 'b :: finite", code]
   1.289 +
   1.290 +instantiation vec :: (metric_space, finite) metric_space
   1.291 +begin
   1.292 +
   1.293 +lemma dist_vec_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
   1.294 +  unfolding dist_vec_def by (rule member_le_setL2) simp_all
   1.295 +
   1.296 +instance proof
   1.297 +  fix x y :: "'a ^ 'b"
   1.298 +  show "dist x y = 0 \<longleftrightarrow> x = y"
   1.299 +    unfolding dist_vec_def
   1.300 +    by (simp add: setL2_eq_0_iff vec_eq_iff)
   1.301 +next
   1.302 +  fix x y z :: "'a ^ 'b"
   1.303 +  show "dist x y \<le> dist x z + dist y z"
   1.304 +    unfolding dist_vec_def
   1.305 +    apply (rule order_trans [OF _ setL2_triangle_ineq])
   1.306 +    apply (simp add: setL2_mono dist_triangle2)
   1.307 +    done
   1.308 +next
   1.309 +  fix S :: "('a ^ 'b) set"
   1.310 +  have *: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   1.311 +  proof
   1.312 +    assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   1.313 +    proof
   1.314 +      fix x assume "x \<in> S"
   1.315 +      obtain A where A: "\<forall>i. open (A i)" "\<forall>i. x $ i \<in> A i"
   1.316 +        and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
   1.317 +        using \<open>open S\<close> and \<open>x \<in> S\<close> unfolding open_vec_def by metis
   1.318 +      have "\<forall>i\<in>UNIV. \<exists>r>0. \<forall>y. dist y (x $ i) < r \<longrightarrow> y \<in> A i"
   1.319 +        using A unfolding open_dist by simp
   1.320 +      hence "\<exists>r. \<forall>i\<in>UNIV. 0 < r i \<and> (\<forall>y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i)"
   1.321 +        by (rule finite_set_choice [OF finite])
   1.322 +      then obtain r where r1: "\<forall>i. 0 < r i"
   1.323 +        and r2: "\<forall>i y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i" by fast
   1.324 +      have "0 < Min (range r) \<and> (\<forall>y. dist y x < Min (range r) \<longrightarrow> y \<in> S)"
   1.325 +        by (simp add: r1 r2 S le_less_trans [OF dist_vec_nth_le])
   1.326 +      thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
   1.327 +    qed
   1.328 +  next
   1.329 +    assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
   1.330 +    proof (unfold open_vec_def, rule)
   1.331 +      fix x assume "x \<in> S"
   1.332 +      then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   1.333 +        using * by fast
   1.334 +      define r where [abs_def]: "r i = e / sqrt (of_nat CARD('b))" for i :: 'b
   1.335 +      from \<open>0 < e\<close> have r: "\<forall>i. 0 < r i"
   1.336 +        unfolding r_def by simp_all
   1.337 +      from \<open>0 < e\<close> have e: "e = setL2 r UNIV"
   1.338 +        unfolding r_def by (simp add: setL2_constant)
   1.339 +      define A where "A i = {y. dist (x $ i) y < r i}" for i
   1.340 +      have "\<forall>i. open (A i) \<and> x $ i \<in> A i"
   1.341 +        unfolding A_def by (simp add: open_ball r)
   1.342 +      moreover have "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
   1.343 +        by (simp add: A_def S dist_vec_def e setL2_strict_mono dist_commute)
   1.344 +      ultimately show "\<exists>A. (\<forall>i. open (A i) \<and> x $ i \<in> A i) \<and>
   1.345 +        (\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S)" by metis
   1.346 +    qed
   1.347 +  qed
   1.348 +  show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
   1.349 +    unfolding * eventually_uniformity_metric
   1.350 +    by (simp del: split_paired_All add: dist_vec_def dist_commute)
   1.351 +qed
   1.352 +
   1.353 +end
   1.354 +
   1.355 +lemma Cauchy_vec_nth:
   1.356 +  "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
   1.357 +  unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le])
   1.358 +
   1.359 +lemma vec_CauchyI:
   1.360 +  fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
   1.361 +  assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
   1.362 +  shows "Cauchy (\<lambda>n. X n)"
   1.363 +proof (rule metric_CauchyI)
   1.364 +  fix r :: real assume "0 < r"
   1.365 +  hence "0 < r / of_nat CARD('n)" (is "0 < ?s") by simp
   1.366 +  define N where "N i = (LEAST N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s)" for i
   1.367 +  define M where "M = Max (range N)"
   1.368 +  have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
   1.369 +    using X \<open>0 < ?s\<close> by (rule metric_CauchyD)
   1.370 +  hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
   1.371 +    unfolding N_def by (rule LeastI_ex)
   1.372 +  hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
   1.373 +    unfolding M_def by simp
   1.374 +  {
   1.375 +    fix m n :: nat
   1.376 +    assume "M \<le> m" "M \<le> n"
   1.377 +    have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
   1.378 +      unfolding dist_vec_def ..
   1.379 +    also have "\<dots> \<le> setsum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
   1.380 +      by (rule setL2_le_setsum [OF zero_le_dist])
   1.381 +    also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
   1.382 +      by (rule setsum_strict_mono, simp_all add: M \<open>M \<le> m\<close> \<open>M \<le> n\<close>)
   1.383 +    also have "\<dots> = r"
   1.384 +      by simp
   1.385 +    finally have "dist (X m) (X n) < r" .
   1.386 +  }
   1.387 +  hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
   1.388 +    by simp
   1.389 +  then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
   1.390 +qed
   1.391 +
   1.392 +instance vec :: (complete_space, finite) complete_space
   1.393 +proof
   1.394 +  fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
   1.395 +  have "\<And>i. (\<lambda>n. X n $ i) \<longlonglongrightarrow> lim (\<lambda>n. X n $ i)"
   1.396 +    using Cauchy_vec_nth [OF \<open>Cauchy X\<close>]
   1.397 +    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   1.398 +  hence "X \<longlonglongrightarrow> vec_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
   1.399 +    by (simp add: vec_tendstoI)
   1.400 +  then show "convergent X"
   1.401 +    by (rule convergentI)
   1.402 +qed
   1.403 +
   1.404 +
   1.405 +subsection \<open>Normed vector space\<close>
   1.406 +
   1.407 +instantiation vec :: (real_normed_vector, finite) real_normed_vector
   1.408 +begin
   1.409 +
   1.410 +definition "norm x = setL2 (\<lambda>i. norm (x$i)) UNIV"
   1.411 +
   1.412 +definition "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
   1.413 +
   1.414 +instance proof
   1.415 +  fix a :: real and x y :: "'a ^ 'b"
   1.416 +  show "norm x = 0 \<longleftrightarrow> x = 0"
   1.417 +    unfolding norm_vec_def
   1.418 +    by (simp add: setL2_eq_0_iff vec_eq_iff)
   1.419 +  show "norm (x + y) \<le> norm x + norm y"
   1.420 +    unfolding norm_vec_def
   1.421 +    apply (rule order_trans [OF _ setL2_triangle_ineq])
   1.422 +    apply (simp add: setL2_mono norm_triangle_ineq)
   1.423 +    done
   1.424 +  show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   1.425 +    unfolding norm_vec_def
   1.426 +    by (simp add: setL2_right_distrib)
   1.427 +  show "sgn x = scaleR (inverse (norm x)) x"
   1.428 +    by (rule sgn_vec_def)
   1.429 +  show "dist x y = norm (x - y)"
   1.430 +    unfolding dist_vec_def norm_vec_def
   1.431 +    by (simp add: dist_norm)
   1.432 +qed
   1.433 +
   1.434 +end
   1.435 +
   1.436 +lemma norm_nth_le: "norm (x $ i) \<le> norm x"
   1.437 +unfolding norm_vec_def
   1.438 +by (rule member_le_setL2) simp_all
   1.439 +
   1.440 +lemma bounded_linear_vec_nth: "bounded_linear (\<lambda>x. x $ i)"
   1.441 +apply standard
   1.442 +apply (rule vector_add_component)
   1.443 +apply (rule vector_scaleR_component)
   1.444 +apply (rule_tac x="1" in exI, simp add: norm_nth_le)
   1.445 +done
   1.446 +
   1.447 +instance vec :: (banach, finite) banach ..
   1.448 +
   1.449 +
   1.450 +subsection \<open>Inner product space\<close>
   1.451 +
   1.452 +instantiation vec :: (real_inner, finite) real_inner
   1.453 +begin
   1.454 +
   1.455 +definition "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
   1.456 +
   1.457 +instance proof
   1.458 +  fix r :: real and x y z :: "'a ^ 'b"
   1.459 +  show "inner x y = inner y x"
   1.460 +    unfolding inner_vec_def
   1.461 +    by (simp add: inner_commute)
   1.462 +  show "inner (x + y) z = inner x z + inner y z"
   1.463 +    unfolding inner_vec_def
   1.464 +    by (simp add: inner_add_left setsum.distrib)
   1.465 +  show "inner (scaleR r x) y = r * inner x y"
   1.466 +    unfolding inner_vec_def
   1.467 +    by (simp add: setsum_right_distrib)
   1.468 +  show "0 \<le> inner x x"
   1.469 +    unfolding inner_vec_def
   1.470 +    by (simp add: setsum_nonneg)
   1.471 +  show "inner x x = 0 \<longleftrightarrow> x = 0"
   1.472 +    unfolding inner_vec_def
   1.473 +    by (simp add: vec_eq_iff setsum_nonneg_eq_0_iff)
   1.474 +  show "norm x = sqrt (inner x x)"
   1.475 +    unfolding inner_vec_def norm_vec_def setL2_def
   1.476 +    by (simp add: power2_norm_eq_inner)
   1.477 +qed
   1.478 +
   1.479 +end
   1.480 +
   1.481 +
   1.482 +subsection \<open>Euclidean space\<close>
   1.483 +
   1.484 +text \<open>Vectors pointing along a single axis.\<close>
   1.485 +
   1.486 +definition "axis k x = (\<chi> i. if i = k then x else 0)"
   1.487 +
   1.488 +lemma axis_nth [simp]: "axis i x $ i = x"
   1.489 +  unfolding axis_def by simp
   1.490 +
   1.491 +lemma axis_eq_axis: "axis i x = axis j y \<longleftrightarrow> x = y \<and> i = j \<or> x = 0 \<and> y = 0"
   1.492 +  unfolding axis_def vec_eq_iff by auto
   1.493 +
   1.494 +lemma inner_axis_axis:
   1.495 +  "inner (axis i x) (axis j y) = (if i = j then inner x y else 0)"
   1.496 +  unfolding inner_vec_def
   1.497 +  apply (cases "i = j")
   1.498 +  apply clarsimp
   1.499 +  apply (subst setsum.remove [of _ j], simp_all)
   1.500 +  apply (rule setsum.neutral, simp add: axis_def)
   1.501 +  apply (rule setsum.neutral, simp add: axis_def)
   1.502 +  done
   1.503 +
   1.504 +lemma setsum_single:
   1.505 +  assumes "finite A" and "k \<in> A" and "f k = y"
   1.506 +  assumes "\<And>i. i \<in> A \<Longrightarrow> i \<noteq> k \<Longrightarrow> f i = 0"
   1.507 +  shows "(\<Sum>i\<in>A. f i) = y"
   1.508 +  apply (subst setsum.remove [OF assms(1,2)])
   1.509 +  apply (simp add: setsum.neutral assms(3,4))
   1.510 +  done
   1.511 +
   1.512 +lemma inner_axis: "inner x (axis i y) = inner (x $ i) y"
   1.513 +  unfolding inner_vec_def
   1.514 +  apply (rule_tac k=i in setsum_single)
   1.515 +  apply simp_all
   1.516 +  apply (simp add: axis_def)
   1.517 +  done
   1.518 +
   1.519 +instantiation vec :: (euclidean_space, finite) euclidean_space
   1.520 +begin
   1.521 +
   1.522 +definition "Basis = (\<Union>i. \<Union>u\<in>Basis. {axis i u})"
   1.523 +
   1.524 +instance proof
   1.525 +  show "(Basis :: ('a ^ 'b) set) \<noteq> {}"
   1.526 +    unfolding Basis_vec_def by simp
   1.527 +next
   1.528 +  show "finite (Basis :: ('a ^ 'b) set)"
   1.529 +    unfolding Basis_vec_def by simp
   1.530 +next
   1.531 +  fix u v :: "'a ^ 'b"
   1.532 +  assume "u \<in> Basis" and "v \<in> Basis"
   1.533 +  thus "inner u v = (if u = v then 1 else 0)"
   1.534 +    unfolding Basis_vec_def
   1.535 +    by (auto simp add: inner_axis_axis axis_eq_axis inner_Basis)
   1.536 +next
   1.537 +  fix x :: "'a ^ 'b"
   1.538 +  show "(\<forall>u\<in>Basis. inner x u = 0) \<longleftrightarrow> x = 0"
   1.539 +    unfolding Basis_vec_def
   1.540 +    by (simp add: inner_axis euclidean_all_zero_iff vec_eq_iff)
   1.541 +qed
   1.542 +
   1.543 +lemma DIM_cart[simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
   1.544 +  apply (simp add: Basis_vec_def)
   1.545 +  apply (subst card_UN_disjoint)
   1.546 +     apply simp
   1.547 +    apply simp
   1.548 +   apply (auto simp: axis_eq_axis) [1]
   1.549 +  apply (subst card_UN_disjoint)
   1.550 +     apply (auto simp: axis_eq_axis)
   1.551 +  done
   1.552 +
   1.553 +end
   1.554 +
   1.555 +lemma cart_eq_inner_axis: "a $ i = inner a (axis i 1)"
   1.556 +  by (simp add: inner_axis)
   1.557 +
   1.558 +lemma axis_in_Basis: "a \<in> Basis \<Longrightarrow> axis i a \<in> Basis"
   1.559 +  by (auto simp add: Basis_vec_def axis_eq_axis)
   1.560 +
   1.561 +end