src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
author wenzelm
Tue Dec 29 23:04:53 2015 +0100 (2015-12-29)
changeset 61969 e01015e49041
parent 61810 3c5040d5694a
child 61973 0c7e865fa7cb
permissions -rw-r--r--
more symbols;
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
huffman@44136
   145
definition
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]:
huffman@36591
   199
  assumes "((\<lambda>x. f x) ---> a) net"
huffman@36591
   200
  shows "((\<lambda>x. f x $ i) ---> 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:
huffman@36591
   215
  assumes "\<And>i. ((\<lambda>x. f x $ i) ---> a $ i) net"
huffman@36591
   216
  shows "((\<lambda>x. f x) ---> 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]:
huffman@36591
   231
  assumes "\<And>i. ((\<lambda>x. f x i) ---> a i) net"
huffman@36591
   232
  shows "((\<lambda>x. \<chi> i. f x i) ---> (\<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>
huffman@36591
   263
huffman@44136
   264
instantiation vec :: (metric_space, finite) metric_space
huffman@36591
   265
begin
huffman@36591
   266
huffman@44136
   267
definition
huffman@36591
   268
  "dist x y = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
huffman@36591
   269
huffman@44136
   270
lemma dist_vec_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
huffman@44136
   271
  unfolding dist_vec_def by (rule member_le_setL2) simp_all
huffman@36591
   272
huffman@36591
   273
instance proof
huffman@36591
   274
  fix x y :: "'a ^ 'b"
huffman@36591
   275
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@44136
   276
    unfolding dist_vec_def
huffman@44136
   277
    by (simp add: setL2_eq_0_iff vec_eq_iff)
huffman@36591
   278
next
huffman@36591
   279
  fix x y z :: "'a ^ 'b"
huffman@36591
   280
  show "dist x y \<le> dist x z + dist y z"
huffman@44136
   281
    unfolding dist_vec_def
huffman@36591
   282
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   283
    apply (simp add: setL2_mono dist_triangle2)
huffman@36591
   284
    done
huffman@36591
   285
next
huffman@36591
   286
  fix S :: "('a ^ 'b) set"
huffman@36591
   287
  show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@44630
   288
  proof
huffman@44630
   289
    assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   290
    proof
huffman@44630
   291
      fix x assume "x \<in> S"
huffman@44630
   292
      obtain A where A: "\<forall>i. open (A i)" "\<forall>i. x $ i \<in> A i"
huffman@44630
   293
        and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
wenzelm@60420
   294
        using \<open>open S\<close> and \<open>x \<in> S\<close> unfolding open_vec_def by metis
huffman@44630
   295
      have "\<forall>i\<in>UNIV. \<exists>r>0. \<forall>y. dist y (x $ i) < r \<longrightarrow> y \<in> A i"
huffman@44630
   296
        using A unfolding open_dist by simp
huffman@44630
   297
      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
   298
        by (rule finite_set_choice [OF finite])
huffman@44630
   299
      then obtain r where r1: "\<forall>i. 0 < r i"
huffman@44630
   300
        and r2: "\<forall>i y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i" by fast
huffman@44630
   301
      have "0 < Min (range r) \<and> (\<forall>y. dist y x < Min (range r) \<longrightarrow> y \<in> S)"
huffman@44630
   302
        by (simp add: r1 r2 S le_less_trans [OF dist_vec_nth_le])
huffman@44630
   303
      thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
huffman@44630
   304
    qed
huffman@44630
   305
  next
huffman@44630
   306
    assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
huffman@44630
   307
    proof (unfold open_vec_def, rule)
huffman@44630
   308
      fix x assume "x \<in> S"
huffman@44630
   309
      then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   310
        using * by fast
huffman@44630
   311
      def r \<equiv> "\<lambda>i::'b. e / sqrt (of_nat CARD('b))"
wenzelm@60420
   312
      from \<open>0 < e\<close> have r: "\<forall>i. 0 < r i"
nipkow@56541
   313
        unfolding r_def by simp_all
wenzelm@60420
   314
      from \<open>0 < e\<close> have e: "e = setL2 r UNIV"
huffman@44630
   315
        unfolding r_def by (simp add: setL2_constant)
huffman@44630
   316
      def A \<equiv> "\<lambda>i. {y. dist (x $ i) y < r i}"
huffman@44630
   317
      have "\<forall>i. open (A i) \<and> x $ i \<in> A i"
huffman@44630
   318
        unfolding A_def by (simp add: open_ball r)
huffman@44630
   319
      moreover have "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
huffman@44630
   320
        by (simp add: A_def S dist_vec_def e setL2_strict_mono dist_commute)
huffman@44630
   321
      ultimately show "\<exists>A. (\<forall>i. open (A i) \<and> x $ i \<in> A i) \<and>
huffman@44630
   322
        (\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S)" by metis
huffman@44630
   323
    qed
huffman@44630
   324
  qed
huffman@36591
   325
qed
huffman@36591
   326
huffman@36591
   327
end
huffman@36591
   328
huffman@44136
   329
lemma Cauchy_vec_nth:
huffman@36591
   330
  "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
huffman@44136
   331
  unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le])
huffman@36591
   332
huffman@44136
   333
lemma vec_CauchyI:
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"
nipkow@56541
   339
  hence "0 < r / of_nat CARD('n)" (is "0 < ?s") by simp
huffman@36591
   340
  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
   341
  def M \<equiv> "Max (range N)"
huffman@36591
   342
  have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
wenzelm@60420
   343
    using X \<open>0 < ?s\<close> by (rule metric_CauchyD)
huffman@36591
   344
  hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   345
    unfolding N_def by (rule LeastI_ex)
huffman@36591
   346
  hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   347
    unfolding M_def by simp
huffman@36591
   348
  {
huffman@36591
   349
    fix m n :: nat
huffman@36591
   350
    assume "M \<le> m" "M \<le> n"
huffman@36591
   351
    have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@44136
   352
      unfolding dist_vec_def ..
huffman@36591
   353
    also have "\<dots> \<le> setsum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@36591
   354
      by (rule setL2_le_setsum [OF zero_le_dist])
huffman@36591
   355
    also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
wenzelm@60420
   356
      by (rule setsum_strict_mono, simp_all add: M \<open>M \<le> m\<close> \<open>M \<le> n\<close>)
huffman@36591
   357
    also have "\<dots> = r"
huffman@36591
   358
      by simp
huffman@36591
   359
    finally have "dist (X m) (X n) < r" .
huffman@36591
   360
  }
huffman@36591
   361
  hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
huffman@36591
   362
    by simp
huffman@36591
   363
  then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
huffman@36591
   364
qed
huffman@36591
   365
huffman@44136
   366
instance vec :: (complete_space, finite) complete_space
huffman@36591
   367
proof
huffman@36591
   368
  fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
wenzelm@61969
   369
  have "\<And>i. (\<lambda>n. X n $ i) \<longlonglongrightarrow> lim (\<lambda>n. X n $ i)"
wenzelm@60420
   370
    using Cauchy_vec_nth [OF \<open>Cauchy X\<close>]
huffman@36591
   371
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
wenzelm@61969
   372
  hence "X \<longlonglongrightarrow> vec_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
huffman@44136
   373
    by (simp add: vec_tendstoI)
huffman@36591
   374
  then show "convergent X"
huffman@36591
   375
    by (rule convergentI)
huffman@36591
   376
qed
huffman@36591
   377
huffman@36591
   378
wenzelm@60420
   379
subsection \<open>Normed vector space\<close>
huffman@36591
   380
huffman@44136
   381
instantiation vec :: (real_normed_vector, finite) real_normed_vector
huffman@36591
   382
begin
huffman@36591
   383
huffman@44136
   384
definition "norm x = setL2 (\<lambda>i. norm (x$i)) UNIV"
huffman@36591
   385
huffman@44141
   386
definition "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
huffman@36591
   387
huffman@36591
   388
instance proof
huffman@36591
   389
  fix a :: real and x y :: "'a ^ 'b"
huffman@36591
   390
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   391
    unfolding norm_vec_def
huffman@44136
   392
    by (simp add: setL2_eq_0_iff vec_eq_iff)
huffman@36591
   393
  show "norm (x + y) \<le> norm x + norm y"
huffman@44136
   394
    unfolding norm_vec_def
huffman@36591
   395
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   396
    apply (simp add: setL2_mono norm_triangle_ineq)
huffman@36591
   397
    done
huffman@36591
   398
  show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
huffman@44136
   399
    unfolding norm_vec_def
huffman@36591
   400
    by (simp add: setL2_right_distrib)
huffman@36591
   401
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@44141
   402
    by (rule sgn_vec_def)
huffman@36591
   403
  show "dist x y = norm (x - y)"
huffman@44136
   404
    unfolding dist_vec_def norm_vec_def
huffman@36591
   405
    by (simp add: dist_norm)
huffman@36591
   406
qed
huffman@36591
   407
huffman@36591
   408
end
huffman@36591
   409
huffman@36591
   410
lemma norm_nth_le: "norm (x $ i) \<le> norm x"
huffman@44136
   411
unfolding norm_vec_def
huffman@36591
   412
by (rule member_le_setL2) simp_all
huffman@36591
   413
huffman@44282
   414
lemma bounded_linear_vec_nth: "bounded_linear (\<lambda>x. x $ i)"
wenzelm@61169
   415
apply standard
huffman@36591
   416
apply (rule vector_add_component)
huffman@36591
   417
apply (rule vector_scaleR_component)
huffman@36591
   418
apply (rule_tac x="1" in exI, simp add: norm_nth_le)
huffman@36591
   419
done
huffman@36591
   420
huffman@44136
   421
instance vec :: (banach, finite) banach ..
huffman@36591
   422
huffman@36591
   423
wenzelm@60420
   424
subsection \<open>Inner product space\<close>
huffman@36591
   425
huffman@44136
   426
instantiation vec :: (real_inner, finite) real_inner
huffman@36591
   427
begin
huffman@36591
   428
huffman@44136
   429
definition "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
huffman@36591
   430
huffman@36591
   431
instance proof
huffman@36591
   432
  fix r :: real and x y z :: "'a ^ 'b"
huffman@36591
   433
  show "inner x y = inner y x"
huffman@44136
   434
    unfolding inner_vec_def
huffman@36591
   435
    by (simp add: inner_commute)
huffman@36591
   436
  show "inner (x + y) z = inner x z + inner y z"
huffman@44136
   437
    unfolding inner_vec_def
haftmann@57418
   438
    by (simp add: inner_add_left setsum.distrib)
huffman@36591
   439
  show "inner (scaleR r x) y = r * inner x y"
huffman@44136
   440
    unfolding inner_vec_def
huffman@36591
   441
    by (simp add: setsum_right_distrib)
huffman@36591
   442
  show "0 \<le> inner x x"
huffman@44136
   443
    unfolding inner_vec_def
huffman@36591
   444
    by (simp add: setsum_nonneg)
huffman@36591
   445
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   446
    unfolding inner_vec_def
huffman@44136
   447
    by (simp add: vec_eq_iff setsum_nonneg_eq_0_iff)
huffman@36591
   448
  show "norm x = sqrt (inner x x)"
huffman@44136
   449
    unfolding inner_vec_def norm_vec_def setL2_def
huffman@36591
   450
    by (simp add: power2_norm_eq_inner)
huffman@36591
   451
qed
huffman@36591
   452
huffman@36591
   453
end
huffman@36591
   454
huffman@44166
   455
wenzelm@60420
   456
subsection \<open>Euclidean space\<close>
huffman@44135
   457
wenzelm@60420
   458
text \<open>Vectors pointing along a single axis.\<close>
huffman@44166
   459
huffman@44166
   460
definition "axis k x = (\<chi> i. if i = k then x else 0)"
huffman@44166
   461
huffman@44166
   462
lemma axis_nth [simp]: "axis i x $ i = x"
huffman@44166
   463
  unfolding axis_def by simp
huffman@44166
   464
huffman@44166
   465
lemma axis_eq_axis: "axis i x = axis j y \<longleftrightarrow> x = y \<and> i = j \<or> x = 0 \<and> y = 0"
huffman@44166
   466
  unfolding axis_def vec_eq_iff by auto
huffman@44166
   467
huffman@44166
   468
lemma inner_axis_axis:
huffman@44166
   469
  "inner (axis i x) (axis j y) = (if i = j then inner x y else 0)"
huffman@44166
   470
  unfolding inner_vec_def
huffman@44166
   471
  apply (cases "i = j")
huffman@44166
   472
  apply clarsimp
haftmann@57418
   473
  apply (subst setsum.remove [of _ j], simp_all)
haftmann@57418
   474
  apply (rule setsum.neutral, simp add: axis_def)
haftmann@57418
   475
  apply (rule setsum.neutral, simp add: axis_def)
huffman@44166
   476
  done
huffman@44166
   477
huffman@44166
   478
lemma setsum_single:
huffman@44166
   479
  assumes "finite A" and "k \<in> A" and "f k = y"
huffman@44166
   480
  assumes "\<And>i. i \<in> A \<Longrightarrow> i \<noteq> k \<Longrightarrow> f i = 0"
huffman@44166
   481
  shows "(\<Sum>i\<in>A. f i) = y"
haftmann@57418
   482
  apply (subst setsum.remove [OF assms(1,2)])
haftmann@57418
   483
  apply (simp add: setsum.neutral assms(3,4))
huffman@44166
   484
  done
huffman@44166
   485
huffman@44166
   486
lemma inner_axis: "inner x (axis i y) = inner (x $ i) y"
huffman@44166
   487
  unfolding inner_vec_def
huffman@44166
   488
  apply (rule_tac k=i in setsum_single)
huffman@44166
   489
  apply simp_all
huffman@44166
   490
  apply (simp add: axis_def)
huffman@44166
   491
  done
huffman@44166
   492
huffman@44136
   493
instantiation vec :: (euclidean_space, finite) euclidean_space
huffman@44135
   494
begin
huffman@44135
   495
huffman@44166
   496
definition "Basis = (\<Union>i. \<Union>u\<in>Basis. {axis i u})"
huffman@44166
   497
huffman@44135
   498
instance proof
huffman@44166
   499
  show "(Basis :: ('a ^ 'b) set) \<noteq> {}"
huffman@44166
   500
    unfolding Basis_vec_def by simp
huffman@44166
   501
next
huffman@44166
   502
  show "finite (Basis :: ('a ^ 'b) set)"
huffman@44166
   503
    unfolding Basis_vec_def by simp
huffman@44135
   504
next
huffman@44166
   505
  fix u v :: "'a ^ 'b"
huffman@44166
   506
  assume "u \<in> Basis" and "v \<in> Basis"
huffman@44166
   507
  thus "inner u v = (if u = v then 1 else 0)"
huffman@44166
   508
    unfolding Basis_vec_def
huffman@44166
   509
    by (auto simp add: inner_axis_axis axis_eq_axis inner_Basis)
huffman@44135
   510
next
huffman@44166
   511
  fix x :: "'a ^ 'b"
huffman@44166
   512
  show "(\<forall>u\<in>Basis. inner x u = 0) \<longleftrightarrow> x = 0"
huffman@44166
   513
    unfolding Basis_vec_def
huffman@44166
   514
    by (simp add: inner_axis euclidean_all_zero_iff vec_eq_iff)
hoelzl@50526
   515
qed
hoelzl@50526
   516
hoelzl@50526
   517
lemma DIM_cart[simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
hoelzl@50526
   518
  apply (simp add: Basis_vec_def)
hoelzl@50526
   519
  apply (subst card_UN_disjoint)
hoelzl@50526
   520
     apply simp
huffman@44166
   521
    apply simp
hoelzl@50526
   522
   apply (auto simp: axis_eq_axis) [1]
hoelzl@50526
   523
  apply (subst card_UN_disjoint)
hoelzl@50526
   524
     apply (auto simp: axis_eq_axis)
hoelzl@50526
   525
  done
huffman@44135
   526
huffman@36591
   527
end
huffman@44135
   528
huffman@44135
   529
end