src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
author hoelzl
Fri Dec 14 15:46:01 2012 +0100 (2012-12-14)
changeset 50526 899c9c4e4a4c
parent 50252 4aa34bd43228
child 50880 b22ecedde1c7
permissions -rw-r--r--
Remove the indexed basis from the definition of euclidean spaces and only use the set of Basis vectors
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
wenzelm@41413
     8
imports
huffman@44135
     9
  Euclidean_Space
wenzelm@41413
    10
  L2_Norm
wenzelm@41413
    11
  "~~/src/HOL/Library/Numeral_Type"
himmelma@33175
    12
begin
himmelma@33175
    13
himmelma@33175
    14
subsection {* Finite Cartesian products, with indexing and lambdas. *}
himmelma@33175
    15
wenzelm@49834
    16
typedef ('a, 'b) vec = "UNIV :: (('b::finite) \<Rightarrow> 'a) set"
huffman@44136
    17
  morphisms vec_nth vec_lambda ..
himmelma@33175
    18
wenzelm@35254
    19
notation
huffman@44136
    20
  vec_nth (infixl "$" 90) and
huffman@44136
    21
  vec_lambda (binder "\<chi>" 10)
himmelma@33175
    22
hoelzl@34290
    23
(*
hoelzl@34291
    24
  Translate "'b ^ 'n" into "'b ^ ('n :: finite)". When 'n has already more than
huffman@44136
    25
  the finite type class write "vec 'b 'n"
hoelzl@34290
    26
*)
hoelzl@34290
    27
huffman@44136
    28
syntax "_finite_vec" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ ^/ _)" [15, 16] 15)
hoelzl@34290
    29
hoelzl@34290
    30
parse_translation {*
hoelzl@34290
    31
let
huffman@44136
    32
  fun vec t u = Syntax.const @{type_syntax vec} $ t $ u;
wenzelm@49674
    33
  fun finite_vec_tr [t, u] =
wenzelm@49674
    34
    (case Term_Position.strip_positions u of
wenzelm@49674
    35
      v as Free (x, _) =>
wenzelm@42290
    36
        if Lexicon.is_tid x then
wenzelm@49674
    37
          vec t (Syntax.const @{syntax_const "_ofsort"} $ v $ Syntax.const @{class_syntax finite})
huffman@44136
    38
        else vec t u
wenzelm@49674
    39
    | _ => vec t u)
hoelzl@34290
    40
in
huffman@44136
    41
  [(@{syntax_const "_finite_vec"}, finite_vec_tr)]
hoelzl@34290
    42
end
hoelzl@34290
    43
*}
hoelzl@34290
    44
huffman@44136
    45
lemma vec_eq_iff: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
huffman@44136
    46
  by (simp add: vec_nth_inject [symmetric] fun_eq_iff)
himmelma@33175
    47
huffman@44136
    48
lemma vec_lambda_beta [simp]: "vec_lambda g $ i = g i"
huffman@44136
    49
  by (simp add: vec_lambda_inverse)
himmelma@33175
    50
huffman@44136
    51
lemma vec_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> vec_lambda g = f"
huffman@44136
    52
  by (auto simp add: vec_eq_iff)
himmelma@33175
    53
huffman@44136
    54
lemma vec_lambda_eta: "(\<chi> i. (g$i)) = g"
huffman@44136
    55
  by (simp add: vec_eq_iff)
himmelma@33175
    56
huffman@36591
    57
huffman@36591
    58
subsection {* Group operations and class instances *}
huffman@36591
    59
huffman@44136
    60
instantiation vec :: (zero, finite) zero
huffman@36591
    61
begin
huffman@44136
    62
  definition "0 \<equiv> (\<chi> i. 0)"
huffman@36591
    63
  instance ..
himmelma@33175
    64
end
huffman@36591
    65
huffman@44136
    66
instantiation vec :: (plus, finite) plus
huffman@36591
    67
begin
huffman@44136
    68
  definition "op + \<equiv> (\<lambda> x y. (\<chi> i. x$i + y$i))"
huffman@36591
    69
  instance ..
huffman@36591
    70
end
huffman@36591
    71
huffman@44136
    72
instantiation vec :: (minus, finite) minus
huffman@36591
    73
begin
huffman@44136
    74
  definition "op - \<equiv> (\<lambda> x y. (\<chi> i. x$i - y$i))"
huffman@36591
    75
  instance ..
huffman@36591
    76
end
huffman@36591
    77
huffman@44136
    78
instantiation vec :: (uminus, finite) uminus
huffman@36591
    79
begin
huffman@44136
    80
  definition "uminus \<equiv> (\<lambda> x. (\<chi> i. - (x$i)))"
huffman@36591
    81
  instance ..
huffman@36591
    82
end
huffman@36591
    83
huffman@36591
    84
lemma zero_index [simp]: "0 $ i = 0"
huffman@44136
    85
  unfolding zero_vec_def by simp
huffman@36591
    86
huffman@36591
    87
lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
huffman@44136
    88
  unfolding plus_vec_def by simp
huffman@36591
    89
huffman@36591
    90
lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
huffman@44136
    91
  unfolding minus_vec_def by simp
huffman@36591
    92
huffman@36591
    93
lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
huffman@44136
    94
  unfolding uminus_vec_def by simp
huffman@36591
    95
huffman@44136
    96
instance vec :: (semigroup_add, finite) semigroup_add
huffman@44136
    97
  by default (simp add: vec_eq_iff add_assoc)
huffman@36591
    98
huffman@44136
    99
instance vec :: (ab_semigroup_add, finite) ab_semigroup_add
huffman@44136
   100
  by default (simp add: vec_eq_iff add_commute)
huffman@36591
   101
huffman@44136
   102
instance vec :: (monoid_add, finite) monoid_add
huffman@44136
   103
  by default (simp_all add: vec_eq_iff)
huffman@36591
   104
huffman@44136
   105
instance vec :: (comm_monoid_add, finite) comm_monoid_add
huffman@44136
   106
  by default (simp add: vec_eq_iff)
huffman@36591
   107
huffman@44136
   108
instance vec :: (cancel_semigroup_add, finite) cancel_semigroup_add
huffman@44136
   109
  by default (simp_all add: vec_eq_iff)
huffman@36591
   110
huffman@44136
   111
instance vec :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
huffman@44136
   112
  by default (simp add: vec_eq_iff)
huffman@36591
   113
huffman@44136
   114
instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
huffman@36591
   115
huffman@44136
   116
instance vec :: (group_add, finite) group_add
huffman@44136
   117
  by default (simp_all add: vec_eq_iff diff_minus)
huffman@36591
   118
huffman@44136
   119
instance vec :: (ab_group_add, finite) ab_group_add
huffman@44136
   120
  by default (simp_all add: vec_eq_iff)
huffman@36591
   121
huffman@36591
   122
huffman@36591
   123
subsection {* Real vector space *}
huffman@36591
   124
huffman@44136
   125
instantiation vec :: (real_vector, finite) real_vector
huffman@36591
   126
begin
huffman@36591
   127
huffman@44136
   128
definition "scaleR \<equiv> (\<lambda> r x. (\<chi> i. scaleR r (x$i)))"
huffman@36591
   129
huffman@36591
   130
lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)"
huffman@44136
   131
  unfolding scaleR_vec_def by simp
huffman@36591
   132
huffman@36591
   133
instance
huffman@44136
   134
  by default (simp_all add: vec_eq_iff scaleR_left_distrib scaleR_right_distrib)
huffman@36591
   135
huffman@36591
   136
end
huffman@36591
   137
huffman@36591
   138
huffman@36591
   139
subsection {* Topological space *}
huffman@36591
   140
huffman@44136
   141
instantiation vec :: (topological_space, finite) topological_space
huffman@36591
   142
begin
huffman@36591
   143
huffman@44136
   144
definition
huffman@36591
   145
  "open (S :: ('a ^ 'b) set) \<longleftrightarrow>
huffman@36591
   146
    (\<forall>x\<in>S. \<exists>A. (\<forall>i. open (A i) \<and> x$i \<in> A i) \<and>
huffman@36591
   147
      (\<forall>y. (\<forall>i. y$i \<in> A i) \<longrightarrow> y \<in> S))"
huffman@36591
   148
huffman@36591
   149
instance proof
huffman@36591
   150
  show "open (UNIV :: ('a ^ 'b) set)"
huffman@44136
   151
    unfolding open_vec_def by auto
huffman@36591
   152
next
huffman@36591
   153
  fix S T :: "('a ^ 'b) set"
huffman@36591
   154
  assume "open S" "open T" thus "open (S \<inter> T)"
huffman@44136
   155
    unfolding open_vec_def
huffman@36591
   156
    apply clarify
huffman@36591
   157
    apply (drule (1) bspec)+
huffman@36591
   158
    apply (clarify, rename_tac Sa Ta)
huffman@36591
   159
    apply (rule_tac x="\<lambda>i. Sa i \<inter> Ta i" in exI)
huffman@36591
   160
    apply (simp add: open_Int)
huffman@36591
   161
    done
huffman@36591
   162
next
huffman@36591
   163
  fix K :: "('a ^ 'b) set set"
huffman@36591
   164
  assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
huffman@44136
   165
    unfolding open_vec_def
huffman@36591
   166
    apply clarify
huffman@36591
   167
    apply (drule (1) bspec)
huffman@36591
   168
    apply (drule (1) bspec)
huffman@36591
   169
    apply clarify
huffman@36591
   170
    apply (rule_tac x=A in exI)
huffman@36591
   171
    apply fast
huffman@36591
   172
    done
huffman@36591
   173
qed
huffman@36591
   174
huffman@36591
   175
end
huffman@36591
   176
huffman@36591
   177
lemma open_vector_box: "\<forall>i. open (S i) \<Longrightarrow> open {x. \<forall>i. x $ i \<in> S i}"
huffman@44136
   178
  unfolding open_vec_def by auto
huffman@36591
   179
huffman@44136
   180
lemma open_vimage_vec_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
huffman@44136
   181
  unfolding open_vec_def
huffman@44136
   182
  apply clarify
huffman@44136
   183
  apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI, simp)
huffman@44136
   184
  done
huffman@36591
   185
huffman@44136
   186
lemma closed_vimage_vec_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
huffman@44136
   187
  unfolding closed_open vimage_Compl [symmetric]
huffman@44136
   188
  by (rule open_vimage_vec_nth)
huffman@36591
   189
huffman@36591
   190
lemma closed_vector_box: "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@36591
   191
proof -
huffman@36591
   192
  have "{x. \<forall>i. x $ i \<in> S i} = (\<Inter>i. (\<lambda>x. x $ i) -` S i)" by auto
huffman@36591
   193
  thus "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@44136
   194
    by (simp add: closed_INT closed_vimage_vec_nth)
huffman@36591
   195
qed
huffman@36591
   196
huffman@44136
   197
lemma tendsto_vec_nth [tendsto_intros]:
huffman@36591
   198
  assumes "((\<lambda>x. f x) ---> a) net"
huffman@36591
   199
  shows "((\<lambda>x. f x $ i) ---> a $ i) net"
huffman@36591
   200
proof (rule topological_tendstoI)
huffman@36591
   201
  fix S assume "open S" "a $ i \<in> S"
huffman@36591
   202
  then have "open ((\<lambda>y. y $ i) -` S)" "a \<in> ((\<lambda>y. y $ i) -` S)"
huffman@44136
   203
    by (simp_all add: open_vimage_vec_nth)
huffman@36591
   204
  with assms have "eventually (\<lambda>x. f x \<in> (\<lambda>y. y $ i) -` S) net"
huffman@36591
   205
    by (rule topological_tendstoD)
huffman@36591
   206
  then show "eventually (\<lambda>x. f x $ i \<in> S) net"
huffman@36591
   207
    by simp
huffman@36591
   208
qed
huffman@36591
   209
huffman@44631
   210
lemma isCont_vec_nth [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. f x $ i) a"
huffman@44631
   211
  unfolding isCont_def by (rule tendsto_vec_nth)
huffman@44631
   212
huffman@36591
   213
lemma eventually_Ball_finite: (* TODO: move *)
huffman@36591
   214
  assumes "finite A" and "\<forall>y\<in>A. eventually (\<lambda>x. P x y) net"
huffman@36591
   215
  shows "eventually (\<lambda>x. \<forall>y\<in>A. P x y) net"
huffman@36591
   216
using assms by (induct set: finite, simp, simp add: eventually_conj)
huffman@36591
   217
huffman@36591
   218
lemma eventually_all_finite: (* TODO: move *)
huffman@36591
   219
  fixes P :: "'a \<Rightarrow> 'b::finite \<Rightarrow> bool"
huffman@36591
   220
  assumes "\<And>y. eventually (\<lambda>x. P x y) net"
huffman@36591
   221
  shows "eventually (\<lambda>x. \<forall>y. P x y) net"
huffman@36591
   222
using eventually_Ball_finite [of UNIV P] assms by simp
huffman@36591
   223
huffman@44136
   224
lemma vec_tendstoI:
huffman@36591
   225
  assumes "\<And>i. ((\<lambda>x. f x $ i) ---> a $ i) net"
huffman@36591
   226
  shows "((\<lambda>x. f x) ---> a) net"
huffman@36591
   227
proof (rule topological_tendstoI)
huffman@36591
   228
  fix S assume "open S" and "a \<in> S"
huffman@36591
   229
  then obtain A where A: "\<And>i. open (A i)" "\<And>i. a $ i \<in> A i"
huffman@36591
   230
    and S: "\<And>y. \<forall>i. y $ i \<in> A i \<Longrightarrow> y \<in> S"
huffman@44136
   231
    unfolding open_vec_def by metis
huffman@36591
   232
  have "\<And>i. eventually (\<lambda>x. f x $ i \<in> A i) net"
huffman@36591
   233
    using assms A by (rule topological_tendstoD)
huffman@36591
   234
  hence "eventually (\<lambda>x. \<forall>i. f x $ i \<in> A i) net"
huffman@36591
   235
    by (rule eventually_all_finite)
huffman@36591
   236
  thus "eventually (\<lambda>x. f x \<in> S) net"
huffman@36591
   237
    by (rule eventually_elim1, simp add: S)
huffman@36591
   238
qed
huffman@36591
   239
huffman@44136
   240
lemma tendsto_vec_lambda [tendsto_intros]:
huffman@36591
   241
  assumes "\<And>i. ((\<lambda>x. f x i) ---> a i) net"
huffman@36591
   242
  shows "((\<lambda>x. \<chi> i. f x i) ---> (\<chi> i. a i)) net"
huffman@44136
   243
  using assms by (simp add: vec_tendstoI)
huffman@36591
   244
huffman@44571
   245
lemma open_image_vec_nth: assumes "open S" shows "open ((\<lambda>x. x $ i) ` S)"
huffman@44571
   246
proof (rule openI)
huffman@44571
   247
  fix a assume "a \<in> (\<lambda>x. x $ i) ` S"
huffman@44571
   248
  then obtain z where "a = z $ i" and "z \<in> S" ..
huffman@44571
   249
  then obtain A where A: "\<forall>i. open (A i) \<and> z $ i \<in> A i"
huffman@44571
   250
    and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
huffman@44571
   251
    using `open S` unfolding open_vec_def by auto
huffman@44571
   252
  hence "A i \<subseteq> (\<lambda>x. x $ i) ` S"
huffman@44571
   253
    by (clarsimp, rule_tac x="\<chi> j. if j = i then x else z $ j" in image_eqI,
huffman@44571
   254
      simp_all)
huffman@44571
   255
  hence "open (A i) \<and> a \<in> A i \<and> A i \<subseteq> (\<lambda>x. x $ i) ` S"
huffman@44571
   256
    using A `a = z $ i` by simp
huffman@44571
   257
  then show "\<exists>T. open T \<and> a \<in> T \<and> T \<subseteq> (\<lambda>x. x $ i) ` S" by - (rule exI)
huffman@44571
   258
qed
huffman@36591
   259
huffman@44571
   260
instance vec :: (perfect_space, finite) perfect_space
huffman@44571
   261
proof
huffman@44571
   262
  fix x :: "'a ^ 'b" show "\<not> open {x}"
huffman@44571
   263
  proof
huffman@44571
   264
    assume "open {x}"
huffman@44571
   265
    hence "\<forall>i. open ((\<lambda>x. x $ i) ` {x})" by (fast intro: open_image_vec_nth)   
huffman@44571
   266
    hence "\<forall>i. open {x $ i}" by simp
huffman@44571
   267
    thus "False" by (simp add: not_open_singleton)
huffman@44571
   268
  qed
huffman@44571
   269
qed
huffman@44571
   270
huffman@44571
   271
huffman@44571
   272
subsection {* Metric space *}
huffman@36591
   273
huffman@44136
   274
instantiation vec :: (metric_space, finite) metric_space
huffman@36591
   275
begin
huffman@36591
   276
huffman@44136
   277
definition
huffman@36591
   278
  "dist x y = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
huffman@36591
   279
huffman@44136
   280
lemma dist_vec_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
huffman@44136
   281
  unfolding dist_vec_def by (rule member_le_setL2) simp_all
huffman@36591
   282
huffman@36591
   283
instance proof
huffman@36591
   284
  fix x y :: "'a ^ 'b"
huffman@36591
   285
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@44136
   286
    unfolding dist_vec_def
huffman@44136
   287
    by (simp add: setL2_eq_0_iff vec_eq_iff)
huffman@36591
   288
next
huffman@36591
   289
  fix x y z :: "'a ^ 'b"
huffman@36591
   290
  show "dist x y \<le> dist x z + dist y z"
huffman@44136
   291
    unfolding dist_vec_def
huffman@36591
   292
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   293
    apply (simp add: setL2_mono dist_triangle2)
huffman@36591
   294
    done
huffman@36591
   295
next
huffman@36591
   296
  fix S :: "('a ^ 'b) set"
huffman@36591
   297
  show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@44630
   298
  proof
huffman@44630
   299
    assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   300
    proof
huffman@44630
   301
      fix x assume "x \<in> S"
huffman@44630
   302
      obtain A where A: "\<forall>i. open (A i)" "\<forall>i. x $ i \<in> A i"
huffman@44630
   303
        and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
huffman@44630
   304
        using `open S` and `x \<in> S` unfolding open_vec_def by metis
huffman@44630
   305
      have "\<forall>i\<in>UNIV. \<exists>r>0. \<forall>y. dist y (x $ i) < r \<longrightarrow> y \<in> A i"
huffman@44630
   306
        using A unfolding open_dist by simp
huffman@44630
   307
      hence "\<exists>r. \<forall>i\<in>UNIV. 0 < r i \<and> (\<forall>y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i)"
huffman@44681
   308
        by (rule finite_set_choice [OF finite])
huffman@44630
   309
      then obtain r where r1: "\<forall>i. 0 < r i"
huffman@44630
   310
        and r2: "\<forall>i y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i" by fast
huffman@44630
   311
      have "0 < Min (range r) \<and> (\<forall>y. dist y x < Min (range r) \<longrightarrow> y \<in> S)"
huffman@44630
   312
        by (simp add: r1 r2 S le_less_trans [OF dist_vec_nth_le])
huffman@44630
   313
      thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
huffman@44630
   314
    qed
huffman@44630
   315
  next
huffman@44630
   316
    assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
huffman@44630
   317
    proof (unfold open_vec_def, rule)
huffman@44630
   318
      fix x assume "x \<in> S"
huffman@44630
   319
      then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   320
        using * by fast
huffman@44630
   321
      def r \<equiv> "\<lambda>i::'b. e / sqrt (of_nat CARD('b))"
huffman@44630
   322
      from `0 < e` have r: "\<forall>i. 0 < r i"
huffman@44630
   323
        unfolding r_def by (simp_all add: divide_pos_pos)
huffman@44630
   324
      from `0 < e` have e: "e = setL2 r UNIV"
huffman@44630
   325
        unfolding r_def by (simp add: setL2_constant)
huffman@44630
   326
      def A \<equiv> "\<lambda>i. {y. dist (x $ i) y < r i}"
huffman@44630
   327
      have "\<forall>i. open (A i) \<and> x $ i \<in> A i"
huffman@44630
   328
        unfolding A_def by (simp add: open_ball r)
huffman@44630
   329
      moreover have "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
huffman@44630
   330
        by (simp add: A_def S dist_vec_def e setL2_strict_mono dist_commute)
huffman@44630
   331
      ultimately show "\<exists>A. (\<forall>i. open (A i) \<and> x $ i \<in> A i) \<and>
huffman@44630
   332
        (\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S)" by metis
huffman@44630
   333
    qed
huffman@44630
   334
  qed
huffman@36591
   335
qed
huffman@36591
   336
huffman@36591
   337
end
huffman@36591
   338
huffman@44136
   339
lemma Cauchy_vec_nth:
huffman@36591
   340
  "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
huffman@44136
   341
  unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le])
huffman@36591
   342
huffman@44136
   343
lemma vec_CauchyI:
huffman@36591
   344
  fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
huffman@36591
   345
  assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
huffman@36591
   346
  shows "Cauchy (\<lambda>n. X n)"
huffman@36591
   347
proof (rule metric_CauchyI)
huffman@36591
   348
  fix r :: real assume "0 < r"
huffman@36591
   349
  then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
huffman@36591
   350
    by (simp add: divide_pos_pos)
huffman@36591
   351
  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
   352
  def M \<equiv> "Max (range N)"
huffman@36591
   353
  have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   354
    using X `0 < ?s` by (rule metric_CauchyD)
huffman@36591
   355
  hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   356
    unfolding N_def by (rule LeastI_ex)
huffman@36591
   357
  hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   358
    unfolding M_def by simp
huffman@36591
   359
  {
huffman@36591
   360
    fix m n :: nat
huffman@36591
   361
    assume "M \<le> m" "M \<le> n"
huffman@36591
   362
    have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@44136
   363
      unfolding dist_vec_def ..
huffman@36591
   364
    also have "\<dots> \<le> setsum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@36591
   365
      by (rule setL2_le_setsum [OF zero_le_dist])
huffman@36591
   366
    also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
huffman@36591
   367
      by (rule setsum_strict_mono, simp_all add: M `M \<le> m` `M \<le> n`)
huffman@36591
   368
    also have "\<dots> = r"
huffman@36591
   369
      by simp
huffman@36591
   370
    finally have "dist (X m) (X n) < r" .
huffman@36591
   371
  }
huffman@36591
   372
  hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
huffman@36591
   373
    by simp
huffman@36591
   374
  then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
huffman@36591
   375
qed
huffman@36591
   376
huffman@44136
   377
instance vec :: (complete_space, finite) complete_space
huffman@36591
   378
proof
huffman@36591
   379
  fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
huffman@36591
   380
  have "\<And>i. (\<lambda>n. X n $ i) ----> lim (\<lambda>n. X n $ i)"
huffman@44136
   381
    using Cauchy_vec_nth [OF `Cauchy X`]
huffman@36591
   382
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
huffman@44136
   383
  hence "X ----> vec_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
huffman@44136
   384
    by (simp add: vec_tendstoI)
huffman@36591
   385
  then show "convergent X"
huffman@36591
   386
    by (rule convergentI)
huffman@36591
   387
qed
huffman@36591
   388
huffman@36591
   389
huffman@36591
   390
subsection {* Normed vector space *}
huffman@36591
   391
huffman@44136
   392
instantiation vec :: (real_normed_vector, finite) real_normed_vector
huffman@36591
   393
begin
huffman@36591
   394
huffman@44136
   395
definition "norm x = setL2 (\<lambda>i. norm (x$i)) UNIV"
huffman@36591
   396
huffman@44141
   397
definition "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
huffman@36591
   398
huffman@36591
   399
instance proof
huffman@36591
   400
  fix a :: real and x y :: "'a ^ 'b"
huffman@36591
   401
  show "0 \<le> norm x"
huffman@44136
   402
    unfolding norm_vec_def
huffman@36591
   403
    by (rule setL2_nonneg)
huffman@36591
   404
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   405
    unfolding norm_vec_def
huffman@44136
   406
    by (simp add: setL2_eq_0_iff vec_eq_iff)
huffman@36591
   407
  show "norm (x + y) \<le> norm x + norm y"
huffman@44136
   408
    unfolding norm_vec_def
huffman@36591
   409
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   410
    apply (simp add: setL2_mono norm_triangle_ineq)
huffman@36591
   411
    done
huffman@36591
   412
  show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
huffman@44136
   413
    unfolding norm_vec_def
huffman@36591
   414
    by (simp add: setL2_right_distrib)
huffman@36591
   415
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@44141
   416
    by (rule sgn_vec_def)
huffman@36591
   417
  show "dist x y = norm (x - y)"
huffman@44136
   418
    unfolding dist_vec_def norm_vec_def
huffman@36591
   419
    by (simp add: dist_norm)
huffman@36591
   420
qed
huffman@36591
   421
huffman@36591
   422
end
huffman@36591
   423
huffman@36591
   424
lemma norm_nth_le: "norm (x $ i) \<le> norm x"
huffman@44136
   425
unfolding norm_vec_def
huffman@36591
   426
by (rule member_le_setL2) simp_all
huffman@36591
   427
huffman@44282
   428
lemma bounded_linear_vec_nth: "bounded_linear (\<lambda>x. x $ i)"
huffman@36591
   429
apply default
huffman@36591
   430
apply (rule vector_add_component)
huffman@36591
   431
apply (rule vector_scaleR_component)
huffman@36591
   432
apply (rule_tac x="1" in exI, simp add: norm_nth_le)
huffman@36591
   433
done
huffman@36591
   434
huffman@44136
   435
instance vec :: (banach, finite) banach ..
huffman@36591
   436
huffman@36591
   437
huffman@36591
   438
subsection {* Inner product space *}
huffman@36591
   439
huffman@44136
   440
instantiation vec :: (real_inner, finite) real_inner
huffman@36591
   441
begin
huffman@36591
   442
huffman@44136
   443
definition "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
huffman@36591
   444
huffman@36591
   445
instance proof
huffman@36591
   446
  fix r :: real and x y z :: "'a ^ 'b"
huffman@36591
   447
  show "inner x y = inner y x"
huffman@44136
   448
    unfolding inner_vec_def
huffman@36591
   449
    by (simp add: inner_commute)
huffman@36591
   450
  show "inner (x + y) z = inner x z + inner y z"
huffman@44136
   451
    unfolding inner_vec_def
huffman@36591
   452
    by (simp add: inner_add_left setsum_addf)
huffman@36591
   453
  show "inner (scaleR r x) y = r * inner x y"
huffman@44136
   454
    unfolding inner_vec_def
huffman@36591
   455
    by (simp add: setsum_right_distrib)
huffman@36591
   456
  show "0 \<le> inner x x"
huffman@44136
   457
    unfolding inner_vec_def
huffman@36591
   458
    by (simp add: setsum_nonneg)
huffman@36591
   459
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   460
    unfolding inner_vec_def
huffman@44136
   461
    by (simp add: vec_eq_iff setsum_nonneg_eq_0_iff)
huffman@36591
   462
  show "norm x = sqrt (inner x x)"
huffman@44136
   463
    unfolding inner_vec_def norm_vec_def setL2_def
huffman@36591
   464
    by (simp add: power2_norm_eq_inner)
huffman@36591
   465
qed
huffman@36591
   466
huffman@36591
   467
end
huffman@36591
   468
huffman@44166
   469
huffman@44135
   470
subsection {* Euclidean space *}
huffman@44135
   471
huffman@44166
   472
text {* Vectors pointing along a single axis. *}
huffman@44166
   473
huffman@44166
   474
definition "axis k x = (\<chi> i. if i = k then x else 0)"
huffman@44166
   475
huffman@44166
   476
lemma axis_nth [simp]: "axis i x $ i = x"
huffman@44166
   477
  unfolding axis_def by simp
huffman@44166
   478
huffman@44166
   479
lemma axis_eq_axis: "axis i x = axis j y \<longleftrightarrow> x = y \<and> i = j \<or> x = 0 \<and> y = 0"
huffman@44166
   480
  unfolding axis_def vec_eq_iff by auto
huffman@44166
   481
huffman@44166
   482
lemma inner_axis_axis:
huffman@44166
   483
  "inner (axis i x) (axis j y) = (if i = j then inner x y else 0)"
huffman@44166
   484
  unfolding inner_vec_def
huffman@44166
   485
  apply (cases "i = j")
huffman@44166
   486
  apply clarsimp
huffman@44166
   487
  apply (subst setsum_diff1' [where a=j], simp_all)
huffman@44166
   488
  apply (rule setsum_0', simp add: axis_def)
huffman@44166
   489
  apply (rule setsum_0', simp add: axis_def)
huffman@44166
   490
  done
huffman@44166
   491
huffman@44166
   492
lemma setsum_single:
huffman@44166
   493
  assumes "finite A" and "k \<in> A" and "f k = y"
huffman@44166
   494
  assumes "\<And>i. i \<in> A \<Longrightarrow> i \<noteq> k \<Longrightarrow> f i = 0"
huffman@44166
   495
  shows "(\<Sum>i\<in>A. f i) = y"
huffman@44166
   496
  apply (subst setsum_diff1' [OF assms(1,2)])
huffman@44166
   497
  apply (simp add: setsum_0' assms(3,4))
huffman@44166
   498
  done
huffman@44166
   499
huffman@44166
   500
lemma inner_axis: "inner x (axis i y) = inner (x $ i) y"
huffman@44166
   501
  unfolding inner_vec_def
huffman@44166
   502
  apply (rule_tac k=i in setsum_single)
huffman@44166
   503
  apply simp_all
huffman@44166
   504
  apply (simp add: axis_def)
huffman@44166
   505
  done
huffman@44166
   506
huffman@44136
   507
instantiation vec :: (euclidean_space, finite) euclidean_space
huffman@44135
   508
begin
huffman@44135
   509
huffman@44166
   510
definition "Basis = (\<Union>i. \<Union>u\<in>Basis. {axis i u})"
huffman@44166
   511
huffman@44135
   512
instance proof
huffman@44166
   513
  show "(Basis :: ('a ^ 'b) set) \<noteq> {}"
huffman@44166
   514
    unfolding Basis_vec_def by simp
huffman@44166
   515
next
huffman@44166
   516
  show "finite (Basis :: ('a ^ 'b) set)"
huffman@44166
   517
    unfolding Basis_vec_def by simp
huffman@44135
   518
next
huffman@44166
   519
  fix u v :: "'a ^ 'b"
huffman@44166
   520
  assume "u \<in> Basis" and "v \<in> Basis"
huffman@44166
   521
  thus "inner u v = (if u = v then 1 else 0)"
huffman@44166
   522
    unfolding Basis_vec_def
huffman@44166
   523
    by (auto simp add: inner_axis_axis axis_eq_axis inner_Basis)
huffman@44135
   524
next
huffman@44166
   525
  fix x :: "'a ^ 'b"
huffman@44166
   526
  show "(\<forall>u\<in>Basis. inner x u = 0) \<longleftrightarrow> x = 0"
huffman@44166
   527
    unfolding Basis_vec_def
huffman@44166
   528
    by (simp add: inner_axis euclidean_all_zero_iff vec_eq_iff)
hoelzl@50526
   529
qed
hoelzl@50526
   530
hoelzl@50526
   531
lemma DIM_cart[simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
hoelzl@50526
   532
  apply (simp add: Basis_vec_def)
hoelzl@50526
   533
  apply (subst card_UN_disjoint)
hoelzl@50526
   534
     apply simp
huffman@44166
   535
    apply simp
hoelzl@50526
   536
   apply (auto simp: axis_eq_axis) [1]
hoelzl@50526
   537
  apply (subst card_UN_disjoint)
hoelzl@50526
   538
     apply (auto simp: axis_eq_axis)
hoelzl@50526
   539
  done
huffman@44135
   540
huffman@36591
   541
end
huffman@44135
   542
huffman@44135
   543
end