src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
author huffman
Thu Apr 29 09:17:25 2010 -0700 (2010-04-29)
changeset 36591 df38e0c5c123
parent 36590 2cdaae32b682
child 36660 1cc4ab4b7ff7
permissions -rw-r--r--
move class instantiations from Euclidean_Space.thy to Finite_Cartesian_Product.thy
wenzelm@35253
     1
(*  Title:      HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
wenzelm@35253
     2
    Author:     Amine Chaieb, University of Cambridge
himmelma@33175
     3
*)
himmelma@33175
     4
himmelma@33175
     5
header {* Definition of finite Cartesian product types. *}
himmelma@33175
     6
himmelma@33175
     7
theory Finite_Cartesian_Product
huffman@36591
     8
imports Inner_Product L2_Norm Numeral_Type
himmelma@33175
     9
begin
himmelma@33175
    10
himmelma@33175
    11
subsection {* Finite Cartesian products, with indexing and lambdas. *}
himmelma@33175
    12
himmelma@33175
    13
typedef (open Cart)
hoelzl@34291
    14
  ('a, 'b) cart = "UNIV :: (('b::finite) \<Rightarrow> 'a) set"
himmelma@33175
    15
  morphisms Cart_nth Cart_lambda ..
himmelma@33175
    16
wenzelm@35254
    17
notation
wenzelm@35254
    18
  Cart_nth (infixl "$" 90) and
wenzelm@35254
    19
  Cart_lambda (binder "\<chi>" 10)
himmelma@33175
    20
hoelzl@34290
    21
(*
hoelzl@34291
    22
  Translate "'b ^ 'n" into "'b ^ ('n :: finite)". When 'n has already more than
hoelzl@34291
    23
  the finite type class write "cart 'b 'n"
hoelzl@34290
    24
*)
hoelzl@34290
    25
hoelzl@34290
    26
syntax "_finite_cart" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ ^/ _)" [15, 16] 15)
hoelzl@34290
    27
hoelzl@34290
    28
parse_translation {*
hoelzl@34290
    29
let
wenzelm@35397
    30
  fun cart t u = Syntax.const @{type_syntax cart} $ t $ u;
hoelzl@34290
    31
  fun finite_cart_tr [t, u as Free (x, _)] =
wenzelm@35113
    32
        if Syntax.is_tid x then
wenzelm@35397
    33
          cart t (Syntax.const @{syntax_const "_ofsort"} $ u $ Syntax.const @{class_syntax finite})
hoelzl@34290
    34
        else cart t u
hoelzl@34290
    35
    | finite_cart_tr [t, u] = cart t u
hoelzl@34290
    36
in
wenzelm@35113
    37
  [(@{syntax_const "_finite_cart"}, finite_cart_tr)]
hoelzl@34290
    38
end
hoelzl@34290
    39
*}
hoelzl@34290
    40
himmelma@33175
    41
lemma stupid_ext: "(\<forall>x. f x = g x) \<longleftrightarrow> (f = g)"
wenzelm@35253
    42
  by (auto intro: ext)
himmelma@33175
    43
hoelzl@34291
    44
lemma Cart_eq: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
himmelma@33175
    45
  by (simp add: Cart_nth_inject [symmetric] expand_fun_eq)
himmelma@33175
    46
himmelma@33175
    47
lemma Cart_lambda_beta [simp]: "Cart_lambda g $ i = g i"
himmelma@33175
    48
  by (simp add: Cart_lambda_inverse)
himmelma@33175
    49
hoelzl@34291
    50
lemma Cart_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> Cart_lambda g = f"
himmelma@33175
    51
  by (auto simp add: Cart_eq)
himmelma@33175
    52
himmelma@33175
    53
lemma Cart_lambda_eta: "(\<chi> i. (g$i)) = g"
himmelma@33175
    54
  by (simp add: Cart_eq)
himmelma@33175
    55
huffman@36591
    56
huffman@36591
    57
subsection {* Group operations and class instances *}
huffman@36591
    58
huffman@36591
    59
instantiation cart :: (zero,finite) zero
huffman@36591
    60
begin
huffman@36591
    61
  definition vector_zero_def : "0 \<equiv> (\<chi> i. 0)"
huffman@36591
    62
  instance ..
himmelma@33175
    63
end
huffman@36591
    64
huffman@36591
    65
instantiation cart :: (plus,finite) plus
huffman@36591
    66
begin
huffman@36591
    67
  definition  vector_add_def : "op + \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) + (y$i)))"
huffman@36591
    68
  instance ..
huffman@36591
    69
end
huffman@36591
    70
huffman@36591
    71
instantiation cart :: (minus,finite) minus
huffman@36591
    72
begin
huffman@36591
    73
  definition vector_minus_def : "op - \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) - (y$i)))"
huffman@36591
    74
  instance ..
huffman@36591
    75
end
huffman@36591
    76
huffman@36591
    77
instantiation cart :: (uminus,finite) uminus
huffman@36591
    78
begin
huffman@36591
    79
  definition vector_uminus_def : "uminus \<equiv> (\<lambda> x.  (\<chi> i. - (x$i)))"
huffman@36591
    80
  instance ..
huffman@36591
    81
end
huffman@36591
    82
huffman@36591
    83
lemma zero_index [simp]: "0 $ i = 0"
huffman@36591
    84
  unfolding vector_zero_def by simp
huffman@36591
    85
huffman@36591
    86
lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
huffman@36591
    87
  unfolding vector_add_def by simp
huffman@36591
    88
huffman@36591
    89
lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
huffman@36591
    90
  unfolding vector_minus_def by simp
huffman@36591
    91
huffman@36591
    92
lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
huffman@36591
    93
  unfolding vector_uminus_def by simp
huffman@36591
    94
huffman@36591
    95
instance cart :: (semigroup_add, finite) semigroup_add
huffman@36591
    96
  by default (simp add: Cart_eq add_assoc)
huffman@36591
    97
huffman@36591
    98
instance cart :: (ab_semigroup_add, finite) ab_semigroup_add
huffman@36591
    99
  by default (simp add: Cart_eq add_commute)
huffman@36591
   100
huffman@36591
   101
instance cart :: (monoid_add, finite) monoid_add
huffman@36591
   102
  by default (simp_all add: Cart_eq)
huffman@36591
   103
huffman@36591
   104
instance cart :: (comm_monoid_add, finite) comm_monoid_add
huffman@36591
   105
  by default (simp add: Cart_eq)
huffman@36591
   106
huffman@36591
   107
instance cart :: (cancel_semigroup_add, finite) cancel_semigroup_add
huffman@36591
   108
  by default (simp_all add: Cart_eq)
huffman@36591
   109
huffman@36591
   110
instance cart :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
huffman@36591
   111
  by default (simp add: Cart_eq)
huffman@36591
   112
huffman@36591
   113
instance cart :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
huffman@36591
   114
huffman@36591
   115
instance cart :: (group_add, finite) group_add
huffman@36591
   116
  by default (simp_all add: Cart_eq diff_minus)
huffman@36591
   117
huffman@36591
   118
instance cart :: (ab_group_add, finite) ab_group_add
huffman@36591
   119
  by default (simp_all add: Cart_eq)
huffman@36591
   120
huffman@36591
   121
huffman@36591
   122
subsection {* Real vector space *}
huffman@36591
   123
huffman@36591
   124
instantiation cart :: (real_vector, finite) real_vector
huffman@36591
   125
begin
huffman@36591
   126
huffman@36591
   127
definition vector_scaleR_def: "scaleR = (\<lambda> r x. (\<chi> i. scaleR r (x$i)))"
huffman@36591
   128
huffman@36591
   129
lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)"
huffman@36591
   130
  unfolding vector_scaleR_def by simp
huffman@36591
   131
huffman@36591
   132
instance
huffman@36591
   133
  by default (simp_all add: Cart_eq scaleR_left_distrib scaleR_right_distrib)
huffman@36591
   134
huffman@36591
   135
end
huffman@36591
   136
huffman@36591
   137
huffman@36591
   138
subsection {* Topological space *}
huffman@36591
   139
huffman@36591
   140
instantiation cart :: (topological_space, finite) topological_space
huffman@36591
   141
begin
huffman@36591
   142
huffman@36591
   143
definition open_vector_def:
huffman@36591
   144
  "open (S :: ('a ^ 'b) set) \<longleftrightarrow>
huffman@36591
   145
    (\<forall>x\<in>S. \<exists>A. (\<forall>i. open (A i) \<and> x$i \<in> A i) \<and>
huffman@36591
   146
      (\<forall>y. (\<forall>i. y$i \<in> A i) \<longrightarrow> y \<in> S))"
huffman@36591
   147
huffman@36591
   148
instance proof
huffman@36591
   149
  show "open (UNIV :: ('a ^ 'b) set)"
huffman@36591
   150
    unfolding open_vector_def by auto
huffman@36591
   151
next
huffman@36591
   152
  fix S T :: "('a ^ 'b) set"
huffman@36591
   153
  assume "open S" "open T" thus "open (S \<inter> T)"
huffman@36591
   154
    unfolding open_vector_def
huffman@36591
   155
    apply clarify
huffman@36591
   156
    apply (drule (1) bspec)+
huffman@36591
   157
    apply (clarify, rename_tac Sa Ta)
huffman@36591
   158
    apply (rule_tac x="\<lambda>i. Sa i \<inter> Ta i" in exI)
huffman@36591
   159
    apply (simp add: open_Int)
huffman@36591
   160
    done
huffman@36591
   161
next
huffman@36591
   162
  fix K :: "('a ^ 'b) set set"
huffman@36591
   163
  assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
huffman@36591
   164
    unfolding open_vector_def
huffman@36591
   165
    apply clarify
huffman@36591
   166
    apply (drule (1) bspec)
huffman@36591
   167
    apply (drule (1) bspec)
huffman@36591
   168
    apply clarify
huffman@36591
   169
    apply (rule_tac x=A in exI)
huffman@36591
   170
    apply fast
huffman@36591
   171
    done
huffman@36591
   172
qed
huffman@36591
   173
huffman@36591
   174
end
huffman@36591
   175
huffman@36591
   176
lemma open_vector_box: "\<forall>i. open (S i) \<Longrightarrow> open {x. \<forall>i. x $ i \<in> S i}"
huffman@36591
   177
unfolding open_vector_def by auto
huffman@36591
   178
huffman@36591
   179
lemma open_vimage_Cart_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
huffman@36591
   180
unfolding open_vector_def
huffman@36591
   181
apply clarify
huffman@36591
   182
apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI, simp)
huffman@36591
   183
done
huffman@36591
   184
huffman@36591
   185
lemma closed_vimage_Cart_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
huffman@36591
   186
unfolding closed_open vimage_Compl [symmetric]
huffman@36591
   187
by (rule open_vimage_Cart_nth)
huffman@36591
   188
huffman@36591
   189
lemma closed_vector_box: "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@36591
   190
proof -
huffman@36591
   191
  have "{x. \<forall>i. x $ i \<in> S i} = (\<Inter>i. (\<lambda>x. x $ i) -` S i)" by auto
huffman@36591
   192
  thus "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@36591
   193
    by (simp add: closed_INT closed_vimage_Cart_nth)
huffman@36591
   194
qed
huffman@36591
   195
huffman@36591
   196
lemma tendsto_Cart_nth [tendsto_intros]:
huffman@36591
   197
  assumes "((\<lambda>x. f x) ---> a) net"
huffman@36591
   198
  shows "((\<lambda>x. f x $ i) ---> a $ i) net"
huffman@36591
   199
proof (rule topological_tendstoI)
huffman@36591
   200
  fix S assume "open S" "a $ i \<in> S"
huffman@36591
   201
  then have "open ((\<lambda>y. y $ i) -` S)" "a \<in> ((\<lambda>y. y $ i) -` S)"
huffman@36591
   202
    by (simp_all add: open_vimage_Cart_nth)
huffman@36591
   203
  with assms have "eventually (\<lambda>x. f x \<in> (\<lambda>y. y $ i) -` S) net"
huffman@36591
   204
    by (rule topological_tendstoD)
huffman@36591
   205
  then show "eventually (\<lambda>x. f x $ i \<in> S) net"
huffman@36591
   206
    by simp
huffman@36591
   207
qed
huffman@36591
   208
huffman@36591
   209
lemma eventually_Ball_finite: (* TODO: move *)
huffman@36591
   210
  assumes "finite A" and "\<forall>y\<in>A. eventually (\<lambda>x. P x y) net"
huffman@36591
   211
  shows "eventually (\<lambda>x. \<forall>y\<in>A. P x y) net"
huffman@36591
   212
using assms by (induct set: finite, simp, simp add: eventually_conj)
huffman@36591
   213
huffman@36591
   214
lemma eventually_all_finite: (* TODO: move *)
huffman@36591
   215
  fixes P :: "'a \<Rightarrow> 'b::finite \<Rightarrow> bool"
huffman@36591
   216
  assumes "\<And>y. eventually (\<lambda>x. P x y) net"
huffman@36591
   217
  shows "eventually (\<lambda>x. \<forall>y. P x y) net"
huffman@36591
   218
using eventually_Ball_finite [of UNIV P] assms by simp
huffman@36591
   219
huffman@36591
   220
lemma tendsto_vector:
huffman@36591
   221
  assumes "\<And>i. ((\<lambda>x. f x $ i) ---> a $ i) net"
huffman@36591
   222
  shows "((\<lambda>x. f x) ---> a) net"
huffman@36591
   223
proof (rule topological_tendstoI)
huffman@36591
   224
  fix S assume "open S" and "a \<in> S"
huffman@36591
   225
  then obtain A where A: "\<And>i. open (A i)" "\<And>i. a $ i \<in> A i"
huffman@36591
   226
    and S: "\<And>y. \<forall>i. y $ i \<in> A i \<Longrightarrow> y \<in> S"
huffman@36591
   227
    unfolding open_vector_def by metis
huffman@36591
   228
  have "\<And>i. eventually (\<lambda>x. f x $ i \<in> A i) net"
huffman@36591
   229
    using assms A by (rule topological_tendstoD)
huffman@36591
   230
  hence "eventually (\<lambda>x. \<forall>i. f x $ i \<in> A i) net"
huffman@36591
   231
    by (rule eventually_all_finite)
huffman@36591
   232
  thus "eventually (\<lambda>x. f x \<in> S) net"
huffman@36591
   233
    by (rule eventually_elim1, simp add: S)
huffman@36591
   234
qed
huffman@36591
   235
huffman@36591
   236
lemma tendsto_Cart_lambda [tendsto_intros]:
huffman@36591
   237
  assumes "\<And>i. ((\<lambda>x. f x i) ---> a i) net"
huffman@36591
   238
  shows "((\<lambda>x. \<chi> i. f x i) ---> (\<chi> i. a i)) net"
huffman@36591
   239
using assms by (simp add: tendsto_vector)
huffman@36591
   240
huffman@36591
   241
huffman@36591
   242
subsection {* Metric *}
huffman@36591
   243
huffman@36591
   244
(* TODO: move somewhere else *)
huffman@36591
   245
lemma finite_choice: "finite A \<Longrightarrow> \<forall>x\<in>A. \<exists>y. P x y \<Longrightarrow> \<exists>f. \<forall>x\<in>A. P x (f x)"
huffman@36591
   246
apply (induct set: finite, simp_all)
huffman@36591
   247
apply (clarify, rename_tac y)
huffman@36591
   248
apply (rule_tac x="f(x:=y)" in exI, simp)
huffman@36591
   249
done
huffman@36591
   250
huffman@36591
   251
instantiation cart :: (metric_space, finite) metric_space
huffman@36591
   252
begin
huffman@36591
   253
huffman@36591
   254
definition dist_vector_def:
huffman@36591
   255
  "dist x y = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
huffman@36591
   256
huffman@36591
   257
lemma dist_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
huffman@36591
   258
unfolding dist_vector_def
huffman@36591
   259
by (rule member_le_setL2) simp_all
huffman@36591
   260
huffman@36591
   261
instance proof
huffman@36591
   262
  fix x y :: "'a ^ 'b"
huffman@36591
   263
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@36591
   264
    unfolding dist_vector_def
huffman@36591
   265
    by (simp add: setL2_eq_0_iff Cart_eq)
huffman@36591
   266
next
huffman@36591
   267
  fix x y z :: "'a ^ 'b"
huffman@36591
   268
  show "dist x y \<le> dist x z + dist y z"
huffman@36591
   269
    unfolding dist_vector_def
huffman@36591
   270
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   271
    apply (simp add: setL2_mono dist_triangle2)
huffman@36591
   272
    done
huffman@36591
   273
next
huffman@36591
   274
  (* FIXME: long proof! *)
huffman@36591
   275
  fix S :: "('a ^ 'b) set"
huffman@36591
   276
  show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@36591
   277
    unfolding open_vector_def open_dist
huffman@36591
   278
    apply safe
huffman@36591
   279
     apply (drule (1) bspec)
huffman@36591
   280
     apply clarify
huffman@36591
   281
     apply (subgoal_tac "\<exists>e>0. \<forall>i y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
huffman@36591
   282
      apply clarify
huffman@36591
   283
      apply (rule_tac x=e in exI, clarify)
huffman@36591
   284
      apply (drule spec, erule mp, clarify)
huffman@36591
   285
      apply (drule spec, drule spec, erule mp)
huffman@36591
   286
      apply (erule le_less_trans [OF dist_nth_le])
huffman@36591
   287
     apply (subgoal_tac "\<forall>i\<in>UNIV. \<exists>e>0. \<forall>y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
huffman@36591
   288
      apply (drule finite_choice [OF finite], clarify)
huffman@36591
   289
      apply (rule_tac x="Min (range f)" in exI, simp)
huffman@36591
   290
     apply clarify
huffman@36591
   291
     apply (drule_tac x=i in spec, clarify)
huffman@36591
   292
     apply (erule (1) bspec)
huffman@36591
   293
    apply (drule (1) bspec, clarify)
huffman@36591
   294
    apply (subgoal_tac "\<exists>r. (\<forall>i::'b. 0 < r i) \<and> e = setL2 r UNIV")
huffman@36591
   295
     apply clarify
huffman@36591
   296
     apply (rule_tac x="\<lambda>i. {y. dist y (x$i) < r i}" in exI)
huffman@36591
   297
     apply (rule conjI)
huffman@36591
   298
      apply clarify
huffman@36591
   299
      apply (rule conjI)
huffman@36591
   300
       apply (clarify, rename_tac y)
huffman@36591
   301
       apply (rule_tac x="r i - dist y (x$i)" in exI, rule conjI, simp)
huffman@36591
   302
       apply clarify
huffman@36591
   303
       apply (simp only: less_diff_eq)
huffman@36591
   304
       apply (erule le_less_trans [OF dist_triangle])
huffman@36591
   305
      apply simp
huffman@36591
   306
     apply clarify
huffman@36591
   307
     apply (drule spec, erule mp)
huffman@36591
   308
     apply (simp add: dist_vector_def setL2_strict_mono)
huffman@36591
   309
    apply (rule_tac x="\<lambda>i. e / sqrt (of_nat CARD('b))" in exI)
huffman@36591
   310
    apply (simp add: divide_pos_pos setL2_constant)
huffman@36591
   311
    done
huffman@36591
   312
qed
huffman@36591
   313
huffman@36591
   314
end
huffman@36591
   315
huffman@36591
   316
lemma LIMSEQ_Cart_nth:
huffman@36591
   317
  "(X ----> a) \<Longrightarrow> (\<lambda>n. X n $ i) ----> a $ i"
huffman@36591
   318
unfolding LIMSEQ_conv_tendsto by (rule tendsto_Cart_nth)
huffman@36591
   319
huffman@36591
   320
lemma LIM_Cart_nth:
huffman@36591
   321
  "(f -- x --> y) \<Longrightarrow> (\<lambda>x. f x $ i) -- x --> y $ i"
huffman@36591
   322
unfolding LIM_conv_tendsto by (rule tendsto_Cart_nth)
huffman@36591
   323
huffman@36591
   324
lemma Cauchy_Cart_nth:
huffman@36591
   325
  "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
huffman@36591
   326
unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_nth_le])
huffman@36591
   327
huffman@36591
   328
lemma LIMSEQ_vector:
huffman@36591
   329
  assumes "\<And>i. (\<lambda>n. X n $ i) ----> (a $ i)"
huffman@36591
   330
  shows "X ----> a"
huffman@36591
   331
using assms unfolding LIMSEQ_conv_tendsto by (rule tendsto_vector)
huffman@36591
   332
huffman@36591
   333
lemma Cauchy_vector:
huffman@36591
   334
  fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
huffman@36591
   335
  assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
huffman@36591
   336
  shows "Cauchy (\<lambda>n. X n)"
huffman@36591
   337
proof (rule metric_CauchyI)
huffman@36591
   338
  fix r :: real assume "0 < r"
huffman@36591
   339
  then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
huffman@36591
   340
    by (simp add: divide_pos_pos)
huffman@36591
   341
  def N \<equiv> "\<lambda>i. LEAST N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   342
  def M \<equiv> "Max (range N)"
huffman@36591
   343
  have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   344
    using X `0 < ?s` by (rule metric_CauchyD)
huffman@36591
   345
  hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   346
    unfolding N_def by (rule LeastI_ex)
huffman@36591
   347
  hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   348
    unfolding M_def by simp
huffman@36591
   349
  {
huffman@36591
   350
    fix m n :: nat
huffman@36591
   351
    assume "M \<le> m" "M \<le> n"
huffman@36591
   352
    have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@36591
   353
      unfolding dist_vector_def ..
huffman@36591
   354
    also have "\<dots> \<le> setsum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@36591
   355
      by (rule setL2_le_setsum [OF zero_le_dist])
huffman@36591
   356
    also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
huffman@36591
   357
      by (rule setsum_strict_mono, simp_all add: M `M \<le> m` `M \<le> n`)
huffman@36591
   358
    also have "\<dots> = r"
huffman@36591
   359
      by simp
huffman@36591
   360
    finally have "dist (X m) (X n) < r" .
huffman@36591
   361
  }
huffman@36591
   362
  hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
huffman@36591
   363
    by simp
huffman@36591
   364
  then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
huffman@36591
   365
qed
huffman@36591
   366
huffman@36591
   367
instance cart :: (complete_space, finite) complete_space
huffman@36591
   368
proof
huffman@36591
   369
  fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
huffman@36591
   370
  have "\<And>i. (\<lambda>n. X n $ i) ----> lim (\<lambda>n. X n $ i)"
huffman@36591
   371
    using Cauchy_Cart_nth [OF `Cauchy X`]
huffman@36591
   372
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
huffman@36591
   373
  hence "X ----> Cart_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
huffman@36591
   374
    by (simp add: LIMSEQ_vector)
huffman@36591
   375
  then show "convergent X"
huffman@36591
   376
    by (rule convergentI)
huffman@36591
   377
qed
huffman@36591
   378
huffman@36591
   379
huffman@36591
   380
subsection {* Normed vector space *}
huffman@36591
   381
huffman@36591
   382
instantiation cart :: (real_normed_vector, finite) real_normed_vector
huffman@36591
   383
begin
huffman@36591
   384
huffman@36591
   385
definition norm_vector_def:
huffman@36591
   386
  "norm x = setL2 (\<lambda>i. norm (x$i)) UNIV"
huffman@36591
   387
huffman@36591
   388
definition vector_sgn_def:
huffman@36591
   389
  "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
huffman@36591
   390
huffman@36591
   391
instance proof
huffman@36591
   392
  fix a :: real and x y :: "'a ^ 'b"
huffman@36591
   393
  show "0 \<le> norm x"
huffman@36591
   394
    unfolding norm_vector_def
huffman@36591
   395
    by (rule setL2_nonneg)
huffman@36591
   396
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@36591
   397
    unfolding norm_vector_def
huffman@36591
   398
    by (simp add: setL2_eq_0_iff Cart_eq)
huffman@36591
   399
  show "norm (x + y) \<le> norm x + norm y"
huffman@36591
   400
    unfolding norm_vector_def
huffman@36591
   401
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   402
    apply (simp add: setL2_mono norm_triangle_ineq)
huffman@36591
   403
    done
huffman@36591
   404
  show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
huffman@36591
   405
    unfolding norm_vector_def
huffman@36591
   406
    by (simp add: setL2_right_distrib)
huffman@36591
   407
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@36591
   408
    by (rule vector_sgn_def)
huffman@36591
   409
  show "dist x y = norm (x - y)"
huffman@36591
   410
    unfolding dist_vector_def norm_vector_def
huffman@36591
   411
    by (simp add: dist_norm)
huffman@36591
   412
qed
huffman@36591
   413
huffman@36591
   414
end
huffman@36591
   415
huffman@36591
   416
lemma norm_nth_le: "norm (x $ i) \<le> norm x"
huffman@36591
   417
unfolding norm_vector_def
huffman@36591
   418
by (rule member_le_setL2) simp_all
huffman@36591
   419
huffman@36591
   420
interpretation Cart_nth: bounded_linear "\<lambda>x. x $ i"
huffman@36591
   421
apply default
huffman@36591
   422
apply (rule vector_add_component)
huffman@36591
   423
apply (rule vector_scaleR_component)
huffman@36591
   424
apply (rule_tac x="1" in exI, simp add: norm_nth_le)
huffman@36591
   425
done
huffman@36591
   426
huffman@36591
   427
instance cart :: (banach, finite) banach ..
huffman@36591
   428
huffman@36591
   429
huffman@36591
   430
subsection {* Inner product space *}
huffman@36591
   431
huffman@36591
   432
instantiation cart :: (real_inner, finite) real_inner
huffman@36591
   433
begin
huffman@36591
   434
huffman@36591
   435
definition inner_vector_def:
huffman@36591
   436
  "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
huffman@36591
   437
huffman@36591
   438
instance proof
huffman@36591
   439
  fix r :: real and x y z :: "'a ^ 'b"
huffman@36591
   440
  show "inner x y = inner y x"
huffman@36591
   441
    unfolding inner_vector_def
huffman@36591
   442
    by (simp add: inner_commute)
huffman@36591
   443
  show "inner (x + y) z = inner x z + inner y z"
huffman@36591
   444
    unfolding inner_vector_def
huffman@36591
   445
    by (simp add: inner_add_left setsum_addf)
huffman@36591
   446
  show "inner (scaleR r x) y = r * inner x y"
huffman@36591
   447
    unfolding inner_vector_def
huffman@36591
   448
    by (simp add: setsum_right_distrib)
huffman@36591
   449
  show "0 \<le> inner x x"
huffman@36591
   450
    unfolding inner_vector_def
huffman@36591
   451
    by (simp add: setsum_nonneg)
huffman@36591
   452
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@36591
   453
    unfolding inner_vector_def
huffman@36591
   454
    by (simp add: Cart_eq setsum_nonneg_eq_0_iff)
huffman@36591
   455
  show "norm x = sqrt (inner x x)"
huffman@36591
   456
    unfolding inner_vector_def norm_vector_def setL2_def
huffman@36591
   457
    by (simp add: power2_norm_eq_inner)
huffman@36591
   458
qed
huffman@36591
   459
huffman@36591
   460
end
huffman@36591
   461
huffman@36591
   462
end