src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
author huffman
Wed Aug 31 10:42:31 2011 -0700 (2011-08-31)
changeset 44631 6820684c7a58
parent 44630 d08cb39b628a
child 44681 49ef76b4a634
permissions -rw-r--r--
generalize lemma isCont_vec_nth
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
huffman@44136
    16
typedef (open)
huffman@44136
    17
  ('a, 'b) vec = "UNIV :: (('b::finite) \<Rightarrow> 'a) set"
huffman@44136
    18
  morphisms vec_nth vec_lambda ..
himmelma@33175
    19
wenzelm@35254
    20
notation
huffman@44136
    21
  vec_nth (infixl "$" 90) and
huffman@44136
    22
  vec_lambda (binder "\<chi>" 10)
himmelma@33175
    23
hoelzl@34290
    24
(*
hoelzl@34291
    25
  Translate "'b ^ 'n" into "'b ^ ('n :: finite)". When 'n has already more than
huffman@44136
    26
  the finite type class write "vec 'b 'n"
hoelzl@34290
    27
*)
hoelzl@34290
    28
huffman@44136
    29
syntax "_finite_vec" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ ^/ _)" [15, 16] 15)
hoelzl@34290
    30
hoelzl@34290
    31
parse_translation {*
hoelzl@34290
    32
let
huffman@44136
    33
  fun vec t u = Syntax.const @{type_syntax vec} $ t $ u;
huffman@44136
    34
  fun finite_vec_tr [t, u as Free (x, _)] =
wenzelm@42290
    35
        if Lexicon.is_tid x then
huffman@44136
    36
          vec t (Syntax.const @{syntax_const "_ofsort"} $ u $ Syntax.const @{class_syntax finite})
huffman@44136
    37
        else vec t u
huffman@44136
    38
    | finite_vec_tr [t, u] = vec t u
hoelzl@34290
    39
in
huffman@44136
    40
  [(@{syntax_const "_finite_vec"}, finite_vec_tr)]
hoelzl@34290
    41
end
hoelzl@34290
    42
*}
hoelzl@34290
    43
huffman@44136
    44
lemma vec_eq_iff: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
huffman@44136
    45
  by (simp add: vec_nth_inject [symmetric] fun_eq_iff)
himmelma@33175
    46
huffman@44136
    47
lemma vec_lambda_beta [simp]: "vec_lambda g $ i = g i"
huffman@44136
    48
  by (simp add: vec_lambda_inverse)
himmelma@33175
    49
huffman@44136
    50
lemma vec_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> vec_lambda g = f"
huffman@44136
    51
  by (auto simp add: vec_eq_iff)
himmelma@33175
    52
huffman@44136
    53
lemma vec_lambda_eta: "(\<chi> i. (g$i)) = g"
huffman@44136
    54
  by (simp add: vec_eq_iff)
himmelma@33175
    55
huffman@36591
    56
huffman@36591
    57
subsection {* Group operations and class instances *}
huffman@36591
    58
huffman@44136
    59
instantiation vec :: (zero, finite) zero
huffman@36591
    60
begin
huffman@44136
    61
  definition "0 \<equiv> (\<chi> i. 0)"
huffman@36591
    62
  instance ..
himmelma@33175
    63
end
huffman@36591
    64
huffman@44136
    65
instantiation vec :: (plus, finite) plus
huffman@36591
    66
begin
huffman@44136
    67
  definition "op + \<equiv> (\<lambda> x y. (\<chi> i. x$i + y$i))"
huffman@36591
    68
  instance ..
huffman@36591
    69
end
huffman@36591
    70
huffman@44136
    71
instantiation vec :: (minus, finite) minus
huffman@36591
    72
begin
huffman@44136
    73
  definition "op - \<equiv> (\<lambda> x y. (\<chi> i. x$i - y$i))"
huffman@36591
    74
  instance ..
huffman@36591
    75
end
huffman@36591
    76
huffman@44136
    77
instantiation vec :: (uminus, finite) uminus
huffman@36591
    78
begin
huffman@44136
    79
  definition "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@44136
    84
  unfolding zero_vec_def by simp
huffman@36591
    85
huffman@36591
    86
lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
huffman@44136
    87
  unfolding plus_vec_def by simp
huffman@36591
    88
huffman@36591
    89
lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
huffman@44136
    90
  unfolding minus_vec_def by simp
huffman@36591
    91
huffman@36591
    92
lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
huffman@44136
    93
  unfolding uminus_vec_def by simp
huffman@36591
    94
huffman@44136
    95
instance vec :: (semigroup_add, finite) semigroup_add
huffman@44136
    96
  by default (simp add: vec_eq_iff add_assoc)
huffman@36591
    97
huffman@44136
    98
instance vec :: (ab_semigroup_add, finite) ab_semigroup_add
huffman@44136
    99
  by default (simp add: vec_eq_iff add_commute)
huffman@36591
   100
huffman@44136
   101
instance vec :: (monoid_add, finite) monoid_add
huffman@44136
   102
  by default (simp_all add: vec_eq_iff)
huffman@36591
   103
huffman@44136
   104
instance vec :: (comm_monoid_add, finite) comm_monoid_add
huffman@44136
   105
  by default (simp add: vec_eq_iff)
huffman@36591
   106
huffman@44136
   107
instance vec :: (cancel_semigroup_add, finite) cancel_semigroup_add
huffman@44136
   108
  by default (simp_all add: vec_eq_iff)
huffman@36591
   109
huffman@44136
   110
instance vec :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
huffman@44136
   111
  by default (simp add: vec_eq_iff)
huffman@36591
   112
huffman@44136
   113
instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
huffman@36591
   114
huffman@44136
   115
instance vec :: (group_add, finite) group_add
huffman@44136
   116
  by default (simp_all add: vec_eq_iff diff_minus)
huffman@36591
   117
huffman@44136
   118
instance vec :: (ab_group_add, finite) ab_group_add
huffman@44136
   119
  by default (simp_all add: vec_eq_iff)
huffman@36591
   120
huffman@36591
   121
huffman@36591
   122
subsection {* Real vector space *}
huffman@36591
   123
huffman@44136
   124
instantiation vec :: (real_vector, finite) real_vector
huffman@36591
   125
begin
huffman@36591
   126
huffman@44136
   127
definition "scaleR \<equiv> (\<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@44136
   130
  unfolding scaleR_vec_def by simp
huffman@36591
   131
huffman@36591
   132
instance
huffman@44136
   133
  by default (simp_all add: vec_eq_iff 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@44136
   140
instantiation vec :: (topological_space, finite) topological_space
huffman@36591
   141
begin
huffman@36591
   142
huffman@44136
   143
definition
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@44136
   150
    unfolding open_vec_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@44136
   154
    unfolding open_vec_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@44136
   164
    unfolding open_vec_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@44136
   177
  unfolding open_vec_def by auto
huffman@36591
   178
huffman@44136
   179
lemma open_vimage_vec_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
huffman@44136
   180
  unfolding open_vec_def
huffman@44136
   181
  apply clarify
huffman@44136
   182
  apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI, simp)
huffman@44136
   183
  done
huffman@36591
   184
huffman@44136
   185
lemma closed_vimage_vec_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
huffman@44136
   186
  unfolding closed_open vimage_Compl [symmetric]
huffman@44136
   187
  by (rule open_vimage_vec_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@44136
   193
    by (simp add: closed_INT closed_vimage_vec_nth)
huffman@36591
   194
qed
huffman@36591
   195
huffman@44136
   196
lemma tendsto_vec_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@44136
   202
    by (simp_all add: open_vimage_vec_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@44631
   209
lemma isCont_vec_nth [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. f x $ i) a"
huffman@44631
   210
  unfolding isCont_def by (rule tendsto_vec_nth)
huffman@44631
   211
huffman@36591
   212
lemma eventually_Ball_finite: (* TODO: move *)
huffman@36591
   213
  assumes "finite A" and "\<forall>y\<in>A. eventually (\<lambda>x. P x y) net"
huffman@36591
   214
  shows "eventually (\<lambda>x. \<forall>y\<in>A. P x y) net"
huffman@36591
   215
using assms by (induct set: finite, simp, simp add: eventually_conj)
huffman@36591
   216
huffman@36591
   217
lemma eventually_all_finite: (* TODO: move *)
huffman@36591
   218
  fixes P :: "'a \<Rightarrow> 'b::finite \<Rightarrow> bool"
huffman@36591
   219
  assumes "\<And>y. eventually (\<lambda>x. P x y) net"
huffman@36591
   220
  shows "eventually (\<lambda>x. \<forall>y. P x y) net"
huffman@36591
   221
using eventually_Ball_finite [of UNIV P] assms by simp
huffman@36591
   222
huffman@44136
   223
lemma vec_tendstoI:
huffman@36591
   224
  assumes "\<And>i. ((\<lambda>x. f x $ i) ---> a $ i) net"
huffman@36591
   225
  shows "((\<lambda>x. f x) ---> a) net"
huffman@36591
   226
proof (rule topological_tendstoI)
huffman@36591
   227
  fix S assume "open S" and "a \<in> S"
huffman@36591
   228
  then obtain A where A: "\<And>i. open (A i)" "\<And>i. a $ i \<in> A i"
huffman@36591
   229
    and S: "\<And>y. \<forall>i. y $ i \<in> A i \<Longrightarrow> y \<in> S"
huffman@44136
   230
    unfolding open_vec_def by metis
huffman@36591
   231
  have "\<And>i. eventually (\<lambda>x. f x $ i \<in> A i) net"
huffman@36591
   232
    using assms A by (rule topological_tendstoD)
huffman@36591
   233
  hence "eventually (\<lambda>x. \<forall>i. f x $ i \<in> A i) net"
huffman@36591
   234
    by (rule eventually_all_finite)
huffman@36591
   235
  thus "eventually (\<lambda>x. f x \<in> S) net"
huffman@36591
   236
    by (rule eventually_elim1, simp add: S)
huffman@36591
   237
qed
huffman@36591
   238
huffman@44136
   239
lemma tendsto_vec_lambda [tendsto_intros]:
huffman@36591
   240
  assumes "\<And>i. ((\<lambda>x. f x i) ---> a i) net"
huffman@36591
   241
  shows "((\<lambda>x. \<chi> i. f x i) ---> (\<chi> i. a i)) net"
huffman@44136
   242
  using assms by (simp add: vec_tendstoI)
huffman@36591
   243
huffman@44571
   244
lemma open_image_vec_nth: assumes "open S" shows "open ((\<lambda>x. x $ i) ` S)"
huffman@44571
   245
proof (rule openI)
huffman@44571
   246
  fix a assume "a \<in> (\<lambda>x. x $ i) ` S"
huffman@44571
   247
  then obtain z where "a = z $ i" and "z \<in> S" ..
huffman@44571
   248
  then obtain A where A: "\<forall>i. open (A i) \<and> z $ i \<in> A i"
huffman@44571
   249
    and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
huffman@44571
   250
    using `open S` unfolding open_vec_def by auto
huffman@44571
   251
  hence "A i \<subseteq> (\<lambda>x. x $ i) ` S"
huffman@44571
   252
    by (clarsimp, rule_tac x="\<chi> j. if j = i then x else z $ j" in image_eqI,
huffman@44571
   253
      simp_all)
huffman@44571
   254
  hence "open (A i) \<and> a \<in> A i \<and> A i \<subseteq> (\<lambda>x. x $ i) ` S"
huffman@44571
   255
    using A `a = z $ i` by simp
huffman@44571
   256
  then show "\<exists>T. open T \<and> a \<in> T \<and> T \<subseteq> (\<lambda>x. x $ i) ` S" by - (rule exI)
huffman@44571
   257
qed
huffman@36591
   258
huffman@44571
   259
instance vec :: (perfect_space, finite) perfect_space
huffman@44571
   260
proof
huffman@44571
   261
  fix x :: "'a ^ 'b" show "\<not> open {x}"
huffman@44571
   262
  proof
huffman@44571
   263
    assume "open {x}"
huffman@44571
   264
    hence "\<forall>i. open ((\<lambda>x. x $ i) ` {x})" by (fast intro: open_image_vec_nth)   
huffman@44571
   265
    hence "\<forall>i. open {x $ i}" by simp
huffman@44571
   266
    thus "False" by (simp add: not_open_singleton)
huffman@44571
   267
  qed
huffman@44571
   268
qed
huffman@44571
   269
huffman@44571
   270
huffman@44571
   271
subsection {* Metric space *}
huffman@36591
   272
huffman@36591
   273
(* TODO: move somewhere else *)
huffman@36591
   274
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
   275
apply (induct set: finite, simp_all)
huffman@36591
   276
apply (clarify, rename_tac y)
huffman@36591
   277
apply (rule_tac x="f(x:=y)" in exI, simp)
huffman@36591
   278
done
huffman@36591
   279
huffman@44136
   280
instantiation vec :: (metric_space, finite) metric_space
huffman@36591
   281
begin
huffman@36591
   282
huffman@44136
   283
definition
huffman@36591
   284
  "dist x y = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
huffman@36591
   285
huffman@44136
   286
lemma dist_vec_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
huffman@44136
   287
  unfolding dist_vec_def by (rule member_le_setL2) simp_all
huffman@36591
   288
huffman@36591
   289
instance proof
huffman@36591
   290
  fix x y :: "'a ^ 'b"
huffman@36591
   291
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@44136
   292
    unfolding dist_vec_def
huffman@44136
   293
    by (simp add: setL2_eq_0_iff vec_eq_iff)
huffman@36591
   294
next
huffman@36591
   295
  fix x y z :: "'a ^ 'b"
huffman@36591
   296
  show "dist x y \<le> dist x z + dist y z"
huffman@44136
   297
    unfolding dist_vec_def
huffman@36591
   298
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   299
    apply (simp add: setL2_mono dist_triangle2)
huffman@36591
   300
    done
huffman@36591
   301
next
huffman@36591
   302
  fix S :: "('a ^ 'b) set"
huffman@36591
   303
  show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@44630
   304
  proof
huffman@44630
   305
    assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   306
    proof
huffman@44630
   307
      fix x assume "x \<in> S"
huffman@44630
   308
      obtain A where A: "\<forall>i. open (A i)" "\<forall>i. x $ i \<in> A i"
huffman@44630
   309
        and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
huffman@44630
   310
        using `open S` and `x \<in> S` unfolding open_vec_def by metis
huffman@44630
   311
      have "\<forall>i\<in>UNIV. \<exists>r>0. \<forall>y. dist y (x $ i) < r \<longrightarrow> y \<in> A i"
huffman@44630
   312
        using A unfolding open_dist by simp
huffman@44630
   313
      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@44630
   314
        by (rule finite_choice [OF finite])
huffman@44630
   315
      then obtain r where r1: "\<forall>i. 0 < r i"
huffman@44630
   316
        and r2: "\<forall>i y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i" by fast
huffman@44630
   317
      have "0 < Min (range r) \<and> (\<forall>y. dist y x < Min (range r) \<longrightarrow> y \<in> S)"
huffman@44630
   318
        by (simp add: r1 r2 S le_less_trans [OF dist_vec_nth_le])
huffman@44630
   319
      thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
huffman@44630
   320
    qed
huffman@44630
   321
  next
huffman@44630
   322
    assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
huffman@44630
   323
    proof (unfold open_vec_def, rule)
huffman@44630
   324
      fix x assume "x \<in> S"
huffman@44630
   325
      then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   326
        using * by fast
huffman@44630
   327
      def r \<equiv> "\<lambda>i::'b. e / sqrt (of_nat CARD('b))"
huffman@44630
   328
      from `0 < e` have r: "\<forall>i. 0 < r i"
huffman@44630
   329
        unfolding r_def by (simp_all add: divide_pos_pos)
huffman@44630
   330
      from `0 < e` have e: "e = setL2 r UNIV"
huffman@44630
   331
        unfolding r_def by (simp add: setL2_constant)
huffman@44630
   332
      def A \<equiv> "\<lambda>i. {y. dist (x $ i) y < r i}"
huffman@44630
   333
      have "\<forall>i. open (A i) \<and> x $ i \<in> A i"
huffman@44630
   334
        unfolding A_def by (simp add: open_ball r)
huffman@44630
   335
      moreover have "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
huffman@44630
   336
        by (simp add: A_def S dist_vec_def e setL2_strict_mono dist_commute)
huffman@44630
   337
      ultimately show "\<exists>A. (\<forall>i. open (A i) \<and> x $ i \<in> A i) \<and>
huffman@44630
   338
        (\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S)" by metis
huffman@44630
   339
    qed
huffman@44630
   340
  qed
huffman@36591
   341
qed
huffman@36591
   342
huffman@36591
   343
end
huffman@36591
   344
huffman@44136
   345
lemma Cauchy_vec_nth:
huffman@36591
   346
  "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
huffman@44136
   347
  unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le])
huffman@36591
   348
huffman@44136
   349
lemma vec_CauchyI:
huffman@36591
   350
  fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
huffman@36591
   351
  assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
huffman@36591
   352
  shows "Cauchy (\<lambda>n. X n)"
huffman@36591
   353
proof (rule metric_CauchyI)
huffman@36591
   354
  fix r :: real assume "0 < r"
huffman@36591
   355
  then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
huffman@36591
   356
    by (simp add: divide_pos_pos)
huffman@36591
   357
  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
   358
  def M \<equiv> "Max (range N)"
huffman@36591
   359
  have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   360
    using X `0 < ?s` by (rule metric_CauchyD)
huffman@36591
   361
  hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   362
    unfolding N_def by (rule LeastI_ex)
huffman@36591
   363
  hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   364
    unfolding M_def by simp
huffman@36591
   365
  {
huffman@36591
   366
    fix m n :: nat
huffman@36591
   367
    assume "M \<le> m" "M \<le> n"
huffman@36591
   368
    have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@44136
   369
      unfolding dist_vec_def ..
huffman@36591
   370
    also have "\<dots> \<le> setsum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@36591
   371
      by (rule setL2_le_setsum [OF zero_le_dist])
huffman@36591
   372
    also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
huffman@36591
   373
      by (rule setsum_strict_mono, simp_all add: M `M \<le> m` `M \<le> n`)
huffman@36591
   374
    also have "\<dots> = r"
huffman@36591
   375
      by simp
huffman@36591
   376
    finally have "dist (X m) (X n) < r" .
huffman@36591
   377
  }
huffman@36591
   378
  hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
huffman@36591
   379
    by simp
huffman@36591
   380
  then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
huffman@36591
   381
qed
huffman@36591
   382
huffman@44136
   383
instance vec :: (complete_space, finite) complete_space
huffman@36591
   384
proof
huffman@36591
   385
  fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
huffman@36591
   386
  have "\<And>i. (\<lambda>n. X n $ i) ----> lim (\<lambda>n. X n $ i)"
huffman@44136
   387
    using Cauchy_vec_nth [OF `Cauchy X`]
huffman@36591
   388
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
huffman@44136
   389
  hence "X ----> vec_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
huffman@44136
   390
    by (simp add: vec_tendstoI)
huffman@36591
   391
  then show "convergent X"
huffman@36591
   392
    by (rule convergentI)
huffman@36591
   393
qed
huffman@36591
   394
huffman@36591
   395
huffman@36591
   396
subsection {* Normed vector space *}
huffman@36591
   397
huffman@44136
   398
instantiation vec :: (real_normed_vector, finite) real_normed_vector
huffman@36591
   399
begin
huffman@36591
   400
huffman@44136
   401
definition "norm x = setL2 (\<lambda>i. norm (x$i)) UNIV"
huffman@36591
   402
huffman@44141
   403
definition "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
huffman@36591
   404
huffman@36591
   405
instance proof
huffman@36591
   406
  fix a :: real and x y :: "'a ^ 'b"
huffman@36591
   407
  show "0 \<le> norm x"
huffman@44136
   408
    unfolding norm_vec_def
huffman@36591
   409
    by (rule setL2_nonneg)
huffman@36591
   410
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   411
    unfolding norm_vec_def
huffman@44136
   412
    by (simp add: setL2_eq_0_iff vec_eq_iff)
huffman@36591
   413
  show "norm (x + y) \<le> norm x + norm y"
huffman@44136
   414
    unfolding norm_vec_def
huffman@36591
   415
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   416
    apply (simp add: setL2_mono norm_triangle_ineq)
huffman@36591
   417
    done
huffman@36591
   418
  show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
huffman@44136
   419
    unfolding norm_vec_def
huffman@36591
   420
    by (simp add: setL2_right_distrib)
huffman@36591
   421
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@44141
   422
    by (rule sgn_vec_def)
huffman@36591
   423
  show "dist x y = norm (x - y)"
huffman@44136
   424
    unfolding dist_vec_def norm_vec_def
huffman@36591
   425
    by (simp add: dist_norm)
huffman@36591
   426
qed
huffman@36591
   427
huffman@36591
   428
end
huffman@36591
   429
huffman@36591
   430
lemma norm_nth_le: "norm (x $ i) \<le> norm x"
huffman@44136
   431
unfolding norm_vec_def
huffman@36591
   432
by (rule member_le_setL2) simp_all
huffman@36591
   433
huffman@44282
   434
lemma bounded_linear_vec_nth: "bounded_linear (\<lambda>x. x $ i)"
huffman@36591
   435
apply default
huffman@36591
   436
apply (rule vector_add_component)
huffman@36591
   437
apply (rule vector_scaleR_component)
huffman@36591
   438
apply (rule_tac x="1" in exI, simp add: norm_nth_le)
huffman@36591
   439
done
huffman@36591
   440
huffman@44136
   441
instance vec :: (banach, finite) banach ..
huffman@36591
   442
huffman@36591
   443
huffman@36591
   444
subsection {* Inner product space *}
huffman@36591
   445
huffman@44136
   446
instantiation vec :: (real_inner, finite) real_inner
huffman@36591
   447
begin
huffman@36591
   448
huffman@44136
   449
definition "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
huffman@36591
   450
huffman@36591
   451
instance proof
huffman@36591
   452
  fix r :: real and x y z :: "'a ^ 'b"
huffman@36591
   453
  show "inner x y = inner y x"
huffman@44136
   454
    unfolding inner_vec_def
huffman@36591
   455
    by (simp add: inner_commute)
huffman@36591
   456
  show "inner (x + y) z = inner x z + inner y z"
huffman@44136
   457
    unfolding inner_vec_def
huffman@36591
   458
    by (simp add: inner_add_left setsum_addf)
huffman@36591
   459
  show "inner (scaleR r x) y = r * inner x y"
huffman@44136
   460
    unfolding inner_vec_def
huffman@36591
   461
    by (simp add: setsum_right_distrib)
huffman@36591
   462
  show "0 \<le> inner x x"
huffman@44136
   463
    unfolding inner_vec_def
huffman@36591
   464
    by (simp add: setsum_nonneg)
huffman@36591
   465
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   466
    unfolding inner_vec_def
huffman@44136
   467
    by (simp add: vec_eq_iff setsum_nonneg_eq_0_iff)
huffman@36591
   468
  show "norm x = sqrt (inner x x)"
huffman@44136
   469
    unfolding inner_vec_def norm_vec_def setL2_def
huffman@36591
   470
    by (simp add: power2_norm_eq_inner)
huffman@36591
   471
qed
huffman@36591
   472
huffman@36591
   473
end
huffman@36591
   474
huffman@44166
   475
huffman@44135
   476
subsection {* Euclidean space *}
huffman@44135
   477
huffman@44166
   478
text {* Vectors pointing along a single axis. *}
huffman@44166
   479
huffman@44166
   480
definition "axis k x = (\<chi> i. if i = k then x else 0)"
huffman@44166
   481
huffman@44166
   482
lemma axis_nth [simp]: "axis i x $ i = x"
huffman@44166
   483
  unfolding axis_def by simp
huffman@44166
   484
huffman@44166
   485
lemma axis_eq_axis: "axis i x = axis j y \<longleftrightarrow> x = y \<and> i = j \<or> x = 0 \<and> y = 0"
huffman@44166
   486
  unfolding axis_def vec_eq_iff by auto
huffman@44166
   487
huffman@44166
   488
lemma inner_axis_axis:
huffman@44166
   489
  "inner (axis i x) (axis j y) = (if i = j then inner x y else 0)"
huffman@44166
   490
  unfolding inner_vec_def
huffman@44166
   491
  apply (cases "i = j")
huffman@44166
   492
  apply clarsimp
huffman@44166
   493
  apply (subst setsum_diff1' [where a=j], simp_all)
huffman@44166
   494
  apply (rule setsum_0', simp add: axis_def)
huffman@44166
   495
  apply (rule setsum_0', simp add: axis_def)
huffman@44166
   496
  done
huffman@44166
   497
huffman@44166
   498
lemma setsum_single:
huffman@44166
   499
  assumes "finite A" and "k \<in> A" and "f k = y"
huffman@44166
   500
  assumes "\<And>i. i \<in> A \<Longrightarrow> i \<noteq> k \<Longrightarrow> f i = 0"
huffman@44166
   501
  shows "(\<Sum>i\<in>A. f i) = y"
huffman@44166
   502
  apply (subst setsum_diff1' [OF assms(1,2)])
huffman@44166
   503
  apply (simp add: setsum_0' assms(3,4))
huffman@44166
   504
  done
huffman@44166
   505
huffman@44166
   506
lemma inner_axis: "inner x (axis i y) = inner (x $ i) y"
huffman@44166
   507
  unfolding inner_vec_def
huffman@44166
   508
  apply (rule_tac k=i in setsum_single)
huffman@44166
   509
  apply simp_all
huffman@44166
   510
  apply (simp add: axis_def)
huffman@44166
   511
  done
huffman@44166
   512
huffman@44135
   513
text {* A bijection between @{text "'n::finite"} and @{text "{..<CARD('n)}"} *}
huffman@44135
   514
huffman@44136
   515
definition vec_bij_nat :: "nat \<Rightarrow> ('n::finite)" where
huffman@44136
   516
  "vec_bij_nat = (SOME p. bij_betw p {..<CARD('n)} (UNIV::'n set) )"
huffman@44135
   517
huffman@44136
   518
abbreviation "\<pi> \<equiv> vec_bij_nat"
huffman@44135
   519
definition "\<pi>' = inv_into {..<CARD('n)} (\<pi>::nat \<Rightarrow> ('n::finite))"
huffman@44135
   520
huffman@44135
   521
lemma bij_betw_pi:
huffman@44135
   522
  "bij_betw \<pi> {..<CARD('n::finite)} (UNIV::('n::finite) set)"
huffman@44135
   523
  using ex_bij_betw_nat_finite[of "UNIV::'n set"]
huffman@44136
   524
  by (auto simp: vec_bij_nat_def atLeast0LessThan
huffman@44135
   525
    intro!: someI_ex[of "\<lambda>x. bij_betw x {..<CARD('n)} (UNIV::'n set)"])
huffman@44135
   526
huffman@44135
   527
lemma bij_betw_pi'[intro]: "bij_betw \<pi>' (UNIV::'n set) {..<CARD('n::finite)}"
huffman@44135
   528
  using bij_betw_inv_into[OF bij_betw_pi] unfolding \<pi>'_def by auto
huffman@44135
   529
huffman@44135
   530
lemma pi'_inj[intro]: "inj \<pi>'"
huffman@44135
   531
  using bij_betw_pi' unfolding bij_betw_def by auto
huffman@44135
   532
huffman@44135
   533
lemma pi'_range[intro]: "\<And>i::'n. \<pi>' i < CARD('n::finite)"
huffman@44135
   534
  using bij_betw_pi' unfolding bij_betw_def by auto
huffman@44135
   535
huffman@44135
   536
lemma \<pi>\<pi>'[simp]: "\<And>i::'n::finite. \<pi> (\<pi>' i) = i"
huffman@44135
   537
  using bij_betw_pi by (auto intro!: f_inv_into_f simp: \<pi>'_def bij_betw_def)
huffman@44135
   538
huffman@44135
   539
lemma \<pi>'\<pi>[simp]: "\<And>i. i\<in>{..<CARD('n::finite)} \<Longrightarrow> \<pi>' (\<pi> i::'n) = i"
huffman@44135
   540
  using bij_betw_pi by (auto intro!: inv_into_f_eq simp: \<pi>'_def bij_betw_def)
huffman@44135
   541
huffman@44135
   542
lemma \<pi>\<pi>'_alt[simp]: "\<And>i. i<CARD('n::finite) \<Longrightarrow> \<pi>' (\<pi> i::'n) = i"
huffman@44135
   543
  by auto
huffman@44135
   544
huffman@44135
   545
lemma \<pi>_inj_on: "inj_on (\<pi>::nat\<Rightarrow>'n::finite) {..<CARD('n)}"
huffman@44135
   546
  using bij_betw_pi[where 'n='n] by (simp add: bij_betw_def)
huffman@44135
   547
huffman@44136
   548
instantiation vec :: (euclidean_space, finite) euclidean_space
huffman@44135
   549
begin
huffman@44135
   550
huffman@44166
   551
definition "Basis = (\<Union>i. \<Union>u\<in>Basis. {axis i u})"
huffman@44166
   552
huffman@44135
   553
definition "dimension (t :: ('a ^ 'b) itself) = CARD('b) * DIM('a)"
huffman@44135
   554
huffman@44166
   555
definition "basis i =
huffman@44135
   556
  (if i < (CARD('b) * DIM('a))
huffman@44166
   557
  then axis (\<pi>(i div DIM('a))) (basis (i mod DIM('a)))
huffman@44135
   558
  else 0)"
huffman@44135
   559
huffman@44135
   560
lemma basis_eq:
huffman@44135
   561
  assumes "i < CARD('b)" and "j < DIM('a)"
huffman@44166
   562
  shows "basis (j + i * DIM('a)) = axis (\<pi> i) (basis j)"
huffman@44135
   563
proof -
huffman@44135
   564
  have "j + i * DIM('a) <  DIM('a) * (i + 1)" using assms by (auto simp: field_simps)
huffman@44135
   565
  also have "\<dots> \<le> DIM('a) * CARD('b)" using assms unfolding mult_le_cancel1 by auto
huffman@44135
   566
  finally show ?thesis
huffman@44136
   567
    unfolding basis_vec_def using assms by (auto simp: vec_eq_iff not_less field_simps)
huffman@44135
   568
qed
huffman@44135
   569
huffman@44135
   570
lemma basis_eq_pi':
huffman@44135
   571
  assumes "j < DIM('a)"
huffman@44135
   572
  shows "basis (j + \<pi>' i * DIM('a)) $ k = (if k = i then basis j else 0)"
huffman@44135
   573
  apply (subst basis_eq)
huffman@44166
   574
  using pi'_range assms by (simp_all add: axis_def)
huffman@44135
   575
huffman@44135
   576
lemma split_times_into_modulo[consumes 1]:
huffman@44135
   577
  fixes k :: nat
huffman@44135
   578
  assumes "k < A * B"
huffman@44135
   579
  obtains i j where "i < A" and "j < B" and "k = j + i * B"
huffman@44135
   580
proof
huffman@44135
   581
  have "A * B \<noteq> 0"
huffman@44135
   582
  proof assume "A * B = 0" with assms show False by simp qed
huffman@44135
   583
  hence "0 < B" by auto
huffman@44135
   584
  thus "k mod B < B" using `0 < B` by auto
huffman@44135
   585
next
huffman@44135
   586
  have "k div B * B \<le> k div B * B + k mod B" by (rule le_add1)
huffman@44135
   587
  also have "... < A * B" using assms by simp
huffman@44135
   588
  finally show "k div B < A" by auto
huffman@44135
   589
qed simp
huffman@44135
   590
huffman@44135
   591
lemma linear_less_than_times:
huffman@44135
   592
  fixes i j A B :: nat assumes "i < B" "j < A"
huffman@44135
   593
  shows "j + i * A < B * A"
huffman@44135
   594
proof -
huffman@44135
   595
  have "i * A + j < (Suc i)*A" using `j < A` by simp
huffman@44135
   596
  also have "\<dots> \<le> B * A" using `i < B` unfolding mult_le_cancel2 by simp
huffman@44135
   597
  finally show ?thesis by simp
huffman@44135
   598
qed
huffman@44135
   599
huffman@44135
   600
lemma DIM_cart[simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
huffman@44136
   601
  by (rule dimension_vec_def)
huffman@44135
   602
huffman@44135
   603
instance proof
huffman@44166
   604
  show "(Basis :: ('a ^ 'b) set) \<noteq> {}"
huffman@44166
   605
    unfolding Basis_vec_def by simp
huffman@44166
   606
next
huffman@44166
   607
  show "finite (Basis :: ('a ^ 'b) set)"
huffman@44166
   608
    unfolding Basis_vec_def by simp
huffman@44135
   609
next
huffman@44166
   610
  fix u v :: "'a ^ 'b"
huffman@44166
   611
  assume "u \<in> Basis" and "v \<in> Basis"
huffman@44166
   612
  thus "inner u v = (if u = v then 1 else 0)"
huffman@44166
   613
    unfolding Basis_vec_def
huffman@44166
   614
    by (auto simp add: inner_axis_axis axis_eq_axis inner_Basis)
huffman@44135
   615
next
huffman@44166
   616
  fix x :: "'a ^ 'b"
huffman@44166
   617
  show "(\<forall>u\<in>Basis. inner x u = 0) \<longleftrightarrow> x = 0"
huffman@44166
   618
    unfolding Basis_vec_def
huffman@44166
   619
    by (simp add: inner_axis euclidean_all_zero_iff vec_eq_iff)
huffman@44166
   620
next
huffman@44166
   621
  show "DIM('a ^ 'b) = card (Basis :: ('a ^ 'b) set)"
huffman@44166
   622
    unfolding Basis_vec_def dimension_vec_def dimension_def
huffman@44215
   623
    by (simp add: card_UN_disjoint [unfolded disjoint_iff_not_equal]
huffman@44166
   624
      axis_eq_axis nonzero_Basis)
huffman@44166
   625
next
huffman@44166
   626
  show "basis ` {..<DIM('a ^ 'b)} = (Basis :: ('a ^ 'b) set)"
huffman@44166
   627
    unfolding Basis_vec_def
huffman@44166
   628
    apply auto
huffman@44166
   629
    apply (erule split_times_into_modulo)
huffman@44166
   630
    apply (simp add: basis_eq axis_eq_axis)
huffman@44166
   631
    apply (erule Basis_elim)
huffman@44166
   632
    apply (simp add: image_def basis_vec_def axis_eq_axis)
huffman@44166
   633
    apply (rule rev_bexI, simp)
huffman@44166
   634
    apply (erule linear_less_than_times [OF pi'_range])
huffman@44166
   635
    apply simp
huffman@44135
   636
    done
huffman@44135
   637
next
huffman@44166
   638
  show "basis ` {DIM('a ^ 'b)..} = {0::'a ^ 'b}"
huffman@44166
   639
    by (auto simp add: image_def basis_vec_def)
huffman@44135
   640
qed
huffman@44135
   641
huffman@36591
   642
end
huffman@44135
   643
huffman@44135
   644
end