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