src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
author huffman
Wed Aug 10 10:13:16 2011 -0700 (2011-08-10)
changeset 44135 18b4ab6854f1
parent 42290 b1f544c84040
child 44136 e63ad7d5158d
permissions -rw-r--r--
move euclidean_space instance from Cartesian_Euclidean_Space.thy to Finite_Cartesian_Product.thy
wenzelm@35253
     1
(*  Title:      HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
wenzelm@35253
     2
    Author:     Amine Chaieb, University of Cambridge
himmelma@33175
     3
*)
himmelma@33175
     4
himmelma@33175
     5
header {* Definition of finite Cartesian product types. *}
himmelma@33175
     6
himmelma@33175
     7
theory Finite_Cartesian_Product
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
himmelma@33175
    16
typedef (open Cart)
hoelzl@34291
    17
  ('a, 'b) cart = "UNIV :: (('b::finite) \<Rightarrow> 'a) set"
himmelma@33175
    18
  morphisms Cart_nth Cart_lambda ..
himmelma@33175
    19
wenzelm@35254
    20
notation
wenzelm@35254
    21
  Cart_nth (infixl "$" 90) and
wenzelm@35254
    22
  Cart_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
hoelzl@34291
    26
  the finite type class write "cart 'b 'n"
hoelzl@34290
    27
*)
hoelzl@34290
    28
hoelzl@34290
    29
syntax "_finite_cart" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ ^/ _)" [15, 16] 15)
hoelzl@34290
    30
hoelzl@34290
    31
parse_translation {*
hoelzl@34290
    32
let
wenzelm@35397
    33
  fun cart t u = Syntax.const @{type_syntax cart} $ t $ u;
hoelzl@34290
    34
  fun finite_cart_tr [t, u as Free (x, _)] =
wenzelm@42290
    35
        if Lexicon.is_tid x then
wenzelm@35397
    36
          cart t (Syntax.const @{syntax_const "_ofsort"} $ u $ Syntax.const @{class_syntax finite})
hoelzl@34290
    37
        else cart t u
hoelzl@34290
    38
    | finite_cart_tr [t, u] = cart t u
hoelzl@34290
    39
in
wenzelm@35113
    40
  [(@{syntax_const "_finite_cart"}, finite_cart_tr)]
hoelzl@34290
    41
end
hoelzl@34290
    42
*}
hoelzl@34290
    43
himmelma@33175
    44
lemma stupid_ext: "(\<forall>x. f x = g x) \<longleftrightarrow> (f = g)"
wenzelm@35253
    45
  by (auto intro: ext)
himmelma@33175
    46
hoelzl@34291
    47
lemma Cart_eq: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
nipkow@39302
    48
  by (simp add: Cart_nth_inject [symmetric] fun_eq_iff)
himmelma@33175
    49
himmelma@33175
    50
lemma Cart_lambda_beta [simp]: "Cart_lambda g $ i = g i"
himmelma@33175
    51
  by (simp add: Cart_lambda_inverse)
himmelma@33175
    52
hoelzl@34291
    53
lemma Cart_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> Cart_lambda g = f"
himmelma@33175
    54
  by (auto simp add: Cart_eq)
himmelma@33175
    55
himmelma@33175
    56
lemma Cart_lambda_eta: "(\<chi> i. (g$i)) = g"
himmelma@33175
    57
  by (simp add: Cart_eq)
himmelma@33175
    58
huffman@36591
    59
huffman@36591
    60
subsection {* Group operations and class instances *}
huffman@36591
    61
huffman@36591
    62
instantiation cart :: (zero,finite) zero
huffman@36591
    63
begin
huffman@36591
    64
  definition vector_zero_def : "0 \<equiv> (\<chi> i. 0)"
huffman@36591
    65
  instance ..
himmelma@33175
    66
end
huffman@36591
    67
huffman@36591
    68
instantiation cart :: (plus,finite) plus
huffman@36591
    69
begin
huffman@36591
    70
  definition  vector_add_def : "op + \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) + (y$i)))"
huffman@36591
    71
  instance ..
huffman@36591
    72
end
huffman@36591
    73
huffman@36591
    74
instantiation cart :: (minus,finite) minus
huffman@36591
    75
begin
huffman@36591
    76
  definition vector_minus_def : "op - \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) - (y$i)))"
huffman@36591
    77
  instance ..
huffman@36591
    78
end
huffman@36591
    79
huffman@36591
    80
instantiation cart :: (uminus,finite) uminus
huffman@36591
    81
begin
huffman@36591
    82
  definition vector_uminus_def : "uminus \<equiv> (\<lambda> x.  (\<chi> i. - (x$i)))"
huffman@36591
    83
  instance ..
huffman@36591
    84
end
huffman@36591
    85
huffman@36591
    86
lemma zero_index [simp]: "0 $ i = 0"
huffman@36591
    87
  unfolding vector_zero_def by simp
huffman@36591
    88
huffman@36591
    89
lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
huffman@36591
    90
  unfolding vector_add_def by simp
huffman@36591
    91
huffman@36591
    92
lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
huffman@36591
    93
  unfolding vector_minus_def by simp
huffman@36591
    94
huffman@36591
    95
lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
huffman@36591
    96
  unfolding vector_uminus_def by simp
huffman@36591
    97
huffman@36591
    98
instance cart :: (semigroup_add, finite) semigroup_add
huffman@36591
    99
  by default (simp add: Cart_eq add_assoc)
huffman@36591
   100
huffman@36591
   101
instance cart :: (ab_semigroup_add, finite) ab_semigroup_add
huffman@36591
   102
  by default (simp add: Cart_eq add_commute)
huffman@36591
   103
huffman@36591
   104
instance cart :: (monoid_add, finite) monoid_add
huffman@36591
   105
  by default (simp_all add: Cart_eq)
huffman@36591
   106
huffman@36591
   107
instance cart :: (comm_monoid_add, finite) comm_monoid_add
huffman@36591
   108
  by default (simp add: Cart_eq)
huffman@36591
   109
huffman@36591
   110
instance cart :: (cancel_semigroup_add, finite) cancel_semigroup_add
huffman@36591
   111
  by default (simp_all add: Cart_eq)
huffman@36591
   112
huffman@36591
   113
instance cart :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
huffman@36591
   114
  by default (simp add: Cart_eq)
huffman@36591
   115
huffman@36591
   116
instance cart :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
huffman@36591
   117
huffman@36591
   118
instance cart :: (group_add, finite) group_add
huffman@36591
   119
  by default (simp_all add: Cart_eq diff_minus)
huffman@36591
   120
huffman@36591
   121
instance cart :: (ab_group_add, finite) ab_group_add
huffman@36591
   122
  by default (simp_all add: Cart_eq)
huffman@36591
   123
huffman@36591
   124
huffman@36591
   125
subsection {* Real vector space *}
huffman@36591
   126
huffman@36591
   127
instantiation cart :: (real_vector, finite) real_vector
huffman@36591
   128
begin
huffman@36591
   129
huffman@36591
   130
definition vector_scaleR_def: "scaleR = (\<lambda> r x. (\<chi> i. scaleR r (x$i)))"
huffman@36591
   131
huffman@36591
   132
lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)"
huffman@36591
   133
  unfolding vector_scaleR_def by simp
huffman@36591
   134
huffman@36591
   135
instance
huffman@36591
   136
  by default (simp_all add: Cart_eq scaleR_left_distrib scaleR_right_distrib)
huffman@36591
   137
huffman@36591
   138
end
huffman@36591
   139
huffman@36591
   140
huffman@36591
   141
subsection {* Topological space *}
huffman@36591
   142
huffman@36591
   143
instantiation cart :: (topological_space, finite) topological_space
huffman@36591
   144
begin
huffman@36591
   145
huffman@36591
   146
definition open_vector_def:
huffman@36591
   147
  "open (S :: ('a ^ 'b) set) \<longleftrightarrow>
huffman@36591
   148
    (\<forall>x\<in>S. \<exists>A. (\<forall>i. open (A i) \<and> x$i \<in> A i) \<and>
huffman@36591
   149
      (\<forall>y. (\<forall>i. y$i \<in> A i) \<longrightarrow> y \<in> S))"
huffman@36591
   150
huffman@36591
   151
instance proof
huffman@36591
   152
  show "open (UNIV :: ('a ^ 'b) set)"
huffman@36591
   153
    unfolding open_vector_def by auto
huffman@36591
   154
next
huffman@36591
   155
  fix S T :: "('a ^ 'b) set"
huffman@36591
   156
  assume "open S" "open T" thus "open (S \<inter> T)"
huffman@36591
   157
    unfolding open_vector_def
huffman@36591
   158
    apply clarify
huffman@36591
   159
    apply (drule (1) bspec)+
huffman@36591
   160
    apply (clarify, rename_tac Sa Ta)
huffman@36591
   161
    apply (rule_tac x="\<lambda>i. Sa i \<inter> Ta i" in exI)
huffman@36591
   162
    apply (simp add: open_Int)
huffman@36591
   163
    done
huffman@36591
   164
next
huffman@36591
   165
  fix K :: "('a ^ 'b) set set"
huffman@36591
   166
  assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
huffman@36591
   167
    unfolding open_vector_def
huffman@36591
   168
    apply clarify
huffman@36591
   169
    apply (drule (1) bspec)
huffman@36591
   170
    apply (drule (1) bspec)
huffman@36591
   171
    apply clarify
huffman@36591
   172
    apply (rule_tac x=A in exI)
huffman@36591
   173
    apply fast
huffman@36591
   174
    done
huffman@36591
   175
qed
huffman@36591
   176
huffman@36591
   177
end
huffman@36591
   178
huffman@36591
   179
lemma open_vector_box: "\<forall>i. open (S i) \<Longrightarrow> open {x. \<forall>i. x $ i \<in> S i}"
huffman@36591
   180
unfolding open_vector_def by auto
huffman@36591
   181
huffman@36591
   182
lemma open_vimage_Cart_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
huffman@36591
   183
unfolding open_vector_def
huffman@36591
   184
apply clarify
huffman@36591
   185
apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI, simp)
huffman@36591
   186
done
huffman@36591
   187
huffman@36591
   188
lemma closed_vimage_Cart_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
huffman@36591
   189
unfolding closed_open vimage_Compl [symmetric]
huffman@36591
   190
by (rule open_vimage_Cart_nth)
huffman@36591
   191
huffman@36591
   192
lemma closed_vector_box: "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@36591
   193
proof -
huffman@36591
   194
  have "{x. \<forall>i. x $ i \<in> S i} = (\<Inter>i. (\<lambda>x. x $ i) -` S i)" by auto
huffman@36591
   195
  thus "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@36591
   196
    by (simp add: closed_INT closed_vimage_Cart_nth)
huffman@36591
   197
qed
huffman@36591
   198
huffman@36591
   199
lemma tendsto_Cart_nth [tendsto_intros]:
huffman@36591
   200
  assumes "((\<lambda>x. f x) ---> a) net"
huffman@36591
   201
  shows "((\<lambda>x. f x $ i) ---> a $ i) net"
huffman@36591
   202
proof (rule topological_tendstoI)
huffman@36591
   203
  fix S assume "open S" "a $ i \<in> S"
huffman@36591
   204
  then have "open ((\<lambda>y. y $ i) -` S)" "a \<in> ((\<lambda>y. y $ i) -` S)"
huffman@36591
   205
    by (simp_all add: open_vimage_Cart_nth)
huffman@36591
   206
  with assms have "eventually (\<lambda>x. f x \<in> (\<lambda>y. y $ i) -` S) net"
huffman@36591
   207
    by (rule topological_tendstoD)
huffman@36591
   208
  then show "eventually (\<lambda>x. f x $ i \<in> S) net"
huffman@36591
   209
    by simp
huffman@36591
   210
qed
huffman@36591
   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@36591
   223
lemma tendsto_vector:
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@36591
   230
    unfolding open_vector_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@36591
   239
lemma tendsto_Cart_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@36591
   242
using assms by (simp add: tendsto_vector)
huffman@36591
   243
huffman@36591
   244
huffman@36591
   245
subsection {* Metric *}
huffman@36591
   246
huffman@36591
   247
(* TODO: move somewhere else *)
huffman@36591
   248
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
   249
apply (induct set: finite, simp_all)
huffman@36591
   250
apply (clarify, rename_tac y)
huffman@36591
   251
apply (rule_tac x="f(x:=y)" in exI, simp)
huffman@36591
   252
done
huffman@36591
   253
huffman@36591
   254
instantiation cart :: (metric_space, finite) metric_space
huffman@36591
   255
begin
huffman@36591
   256
huffman@36591
   257
definition dist_vector_def:
huffman@36591
   258
  "dist x y = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
huffman@36591
   259
hoelzl@38656
   260
lemma dist_nth_le_cart: "dist (x $ i) (y $ i) \<le> dist x y"
huffman@36591
   261
unfolding dist_vector_def
huffman@36591
   262
by (rule member_le_setL2) simp_all
huffman@36591
   263
huffman@36591
   264
instance proof
huffman@36591
   265
  fix x y :: "'a ^ 'b"
huffman@36591
   266
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@36591
   267
    unfolding dist_vector_def
huffman@36591
   268
    by (simp add: setL2_eq_0_iff Cart_eq)
huffman@36591
   269
next
huffman@36591
   270
  fix x y z :: "'a ^ 'b"
huffman@36591
   271
  show "dist x y \<le> dist x z + dist y z"
huffman@36591
   272
    unfolding dist_vector_def
huffman@36591
   273
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   274
    apply (simp add: setL2_mono dist_triangle2)
huffman@36591
   275
    done
huffman@36591
   276
next
huffman@36591
   277
  (* FIXME: long proof! *)
huffman@36591
   278
  fix S :: "('a ^ 'b) set"
huffman@36591
   279
  show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@36591
   280
    unfolding open_vector_def open_dist
huffman@36591
   281
    apply safe
huffman@36591
   282
     apply (drule (1) bspec)
huffman@36591
   283
     apply clarify
huffman@36591
   284
     apply (subgoal_tac "\<exists>e>0. \<forall>i y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
huffman@36591
   285
      apply clarify
huffman@36591
   286
      apply (rule_tac x=e in exI, clarify)
huffman@36591
   287
      apply (drule spec, erule mp, clarify)
huffman@36591
   288
      apply (drule spec, drule spec, erule mp)
hoelzl@38656
   289
      apply (erule le_less_trans [OF dist_nth_le_cart])
huffman@36591
   290
     apply (subgoal_tac "\<forall>i\<in>UNIV. \<exists>e>0. \<forall>y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
huffman@36591
   291
      apply (drule finite_choice [OF finite], clarify)
huffman@36591
   292
      apply (rule_tac x="Min (range f)" in exI, simp)
huffman@36591
   293
     apply clarify
huffman@36591
   294
     apply (drule_tac x=i in spec, clarify)
huffman@36591
   295
     apply (erule (1) bspec)
huffman@36591
   296
    apply (drule (1) bspec, clarify)
huffman@36591
   297
    apply (subgoal_tac "\<exists>r. (\<forall>i::'b. 0 < r i) \<and> e = setL2 r UNIV")
huffman@36591
   298
     apply clarify
huffman@36591
   299
     apply (rule_tac x="\<lambda>i. {y. dist y (x$i) < r i}" in exI)
huffman@36591
   300
     apply (rule conjI)
huffman@36591
   301
      apply clarify
huffman@36591
   302
      apply (rule conjI)
huffman@36591
   303
       apply (clarify, rename_tac y)
huffman@36591
   304
       apply (rule_tac x="r i - dist y (x$i)" in exI, rule conjI, simp)
huffman@36591
   305
       apply clarify
huffman@36591
   306
       apply (simp only: less_diff_eq)
huffman@36591
   307
       apply (erule le_less_trans [OF dist_triangle])
huffman@36591
   308
      apply simp
huffman@36591
   309
     apply clarify
huffman@36591
   310
     apply (drule spec, erule mp)
huffman@36591
   311
     apply (simp add: dist_vector_def setL2_strict_mono)
huffman@36591
   312
    apply (rule_tac x="\<lambda>i. e / sqrt (of_nat CARD('b))" in exI)
huffman@36591
   313
    apply (simp add: divide_pos_pos setL2_constant)
huffman@36591
   314
    done
huffman@36591
   315
qed
huffman@36591
   316
huffman@36591
   317
end
huffman@36591
   318
huffman@36591
   319
lemma Cauchy_Cart_nth:
huffman@36591
   320
  "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
hoelzl@38656
   321
unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_nth_le_cart])
huffman@36591
   322
huffman@36591
   323
lemma Cauchy_vector:
huffman@36591
   324
  fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
huffman@36591
   325
  assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
huffman@36591
   326
  shows "Cauchy (\<lambda>n. X n)"
huffman@36591
   327
proof (rule metric_CauchyI)
huffman@36591
   328
  fix r :: real assume "0 < r"
huffman@36591
   329
  then have "0 < r / of_nat CARD('n)" (is "0 < ?s")
huffman@36591
   330
    by (simp add: divide_pos_pos)
huffman@36591
   331
  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
   332
  def M \<equiv> "Max (range N)"
huffman@36591
   333
  have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   334
    using X `0 < ?s` by (rule metric_CauchyD)
huffman@36591
   335
  hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   336
    unfolding N_def by (rule LeastI_ex)
huffman@36591
   337
  hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   338
    unfolding M_def by simp
huffman@36591
   339
  {
huffman@36591
   340
    fix m n :: nat
huffman@36591
   341
    assume "M \<le> m" "M \<le> n"
huffman@36591
   342
    have "dist (X m) (X n) = setL2 (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@36591
   343
      unfolding dist_vector_def ..
huffman@36591
   344
    also have "\<dots> \<le> setsum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@36591
   345
      by (rule setL2_le_setsum [OF zero_le_dist])
huffman@36591
   346
    also have "\<dots> < setsum (\<lambda>i::'n. ?s) UNIV"
huffman@36591
   347
      by (rule setsum_strict_mono, simp_all add: M `M \<le> m` `M \<le> n`)
huffman@36591
   348
    also have "\<dots> = r"
huffman@36591
   349
      by simp
huffman@36591
   350
    finally have "dist (X m) (X n) < r" .
huffman@36591
   351
  }
huffman@36591
   352
  hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
huffman@36591
   353
    by simp
huffman@36591
   354
  then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
huffman@36591
   355
qed
huffman@36591
   356
huffman@36591
   357
instance cart :: (complete_space, finite) complete_space
huffman@36591
   358
proof
huffman@36591
   359
  fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
huffman@36591
   360
  have "\<And>i. (\<lambda>n. X n $ i) ----> lim (\<lambda>n. X n $ i)"
huffman@36591
   361
    using Cauchy_Cart_nth [OF `Cauchy X`]
huffman@36591
   362
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
huffman@36591
   363
  hence "X ----> Cart_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
huffman@36660
   364
    by (simp add: tendsto_vector)
huffman@36591
   365
  then show "convergent X"
huffman@36591
   366
    by (rule convergentI)
huffman@36591
   367
qed
huffman@36591
   368
huffman@36591
   369
huffman@36591
   370
subsection {* Normed vector space *}
huffman@36591
   371
huffman@36591
   372
instantiation cart :: (real_normed_vector, finite) real_normed_vector
huffman@36591
   373
begin
huffman@36591
   374
huffman@36591
   375
definition norm_vector_def:
huffman@36591
   376
  "norm x = setL2 (\<lambda>i. norm (x$i)) UNIV"
huffman@36591
   377
huffman@36591
   378
definition vector_sgn_def:
huffman@36591
   379
  "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
huffman@36591
   380
huffman@36591
   381
instance proof
huffman@36591
   382
  fix a :: real and x y :: "'a ^ 'b"
huffman@36591
   383
  show "0 \<le> norm x"
huffman@36591
   384
    unfolding norm_vector_def
huffman@36591
   385
    by (rule setL2_nonneg)
huffman@36591
   386
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@36591
   387
    unfolding norm_vector_def
huffman@36591
   388
    by (simp add: setL2_eq_0_iff Cart_eq)
huffman@36591
   389
  show "norm (x + y) \<le> norm x + norm y"
huffman@36591
   390
    unfolding norm_vector_def
huffman@36591
   391
    apply (rule order_trans [OF _ setL2_triangle_ineq])
huffman@36591
   392
    apply (simp add: setL2_mono norm_triangle_ineq)
huffman@36591
   393
    done
huffman@36591
   394
  show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
huffman@36591
   395
    unfolding norm_vector_def
huffman@36591
   396
    by (simp add: setL2_right_distrib)
huffman@36591
   397
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@36591
   398
    by (rule vector_sgn_def)
huffman@36591
   399
  show "dist x y = norm (x - y)"
huffman@36591
   400
    unfolding dist_vector_def norm_vector_def
huffman@36591
   401
    by (simp add: dist_norm)
huffman@36591
   402
qed
huffman@36591
   403
huffman@36591
   404
end
huffman@36591
   405
huffman@36591
   406
lemma norm_nth_le: "norm (x $ i) \<le> norm x"
huffman@36591
   407
unfolding norm_vector_def
huffman@36591
   408
by (rule member_le_setL2) simp_all
huffman@36591
   409
huffman@36591
   410
interpretation Cart_nth: bounded_linear "\<lambda>x. x $ i"
huffman@36591
   411
apply default
huffman@36591
   412
apply (rule vector_add_component)
huffman@36591
   413
apply (rule vector_scaleR_component)
huffman@36591
   414
apply (rule_tac x="1" in exI, simp add: norm_nth_le)
huffman@36591
   415
done
huffman@36591
   416
huffman@36591
   417
instance cart :: (banach, finite) banach ..
huffman@36591
   418
huffman@36591
   419
huffman@36591
   420
subsection {* Inner product space *}
huffman@36591
   421
huffman@36591
   422
instantiation cart :: (real_inner, finite) real_inner
huffman@36591
   423
begin
huffman@36591
   424
huffman@36591
   425
definition inner_vector_def:
huffman@36591
   426
  "inner x y = setsum (\<lambda>i. inner (x$i) (y$i)) UNIV"
huffman@36591
   427
huffman@36591
   428
instance proof
huffman@36591
   429
  fix r :: real and x y z :: "'a ^ 'b"
huffman@36591
   430
  show "inner x y = inner y x"
huffman@36591
   431
    unfolding inner_vector_def
huffman@36591
   432
    by (simp add: inner_commute)
huffman@36591
   433
  show "inner (x + y) z = inner x z + inner y z"
huffman@36591
   434
    unfolding inner_vector_def
huffman@36591
   435
    by (simp add: inner_add_left setsum_addf)
huffman@36591
   436
  show "inner (scaleR r x) y = r * inner x y"
huffman@36591
   437
    unfolding inner_vector_def
huffman@36591
   438
    by (simp add: setsum_right_distrib)
huffman@36591
   439
  show "0 \<le> inner x x"
huffman@36591
   440
    unfolding inner_vector_def
huffman@36591
   441
    by (simp add: setsum_nonneg)
huffman@36591
   442
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@36591
   443
    unfolding inner_vector_def
huffman@36591
   444
    by (simp add: Cart_eq setsum_nonneg_eq_0_iff)
huffman@36591
   445
  show "norm x = sqrt (inner x x)"
huffman@36591
   446
    unfolding inner_vector_def norm_vector_def setL2_def
huffman@36591
   447
    by (simp add: power2_norm_eq_inner)
huffman@36591
   448
qed
huffman@36591
   449
huffman@36591
   450
end
huffman@36591
   451
huffman@44135
   452
subsection {* Euclidean space *}
huffman@44135
   453
huffman@44135
   454
text {* A bijection between @{text "'n::finite"} and @{text "{..<CARD('n)}"} *}
huffman@44135
   455
huffman@44135
   456
definition cart_bij_nat :: "nat \<Rightarrow> ('n::finite)" where
huffman@44135
   457
  "cart_bij_nat = (SOME p. bij_betw p {..<CARD('n)} (UNIV::'n set) )"
huffman@44135
   458
huffman@44135
   459
abbreviation "\<pi> \<equiv> cart_bij_nat"
huffman@44135
   460
definition "\<pi>' = inv_into {..<CARD('n)} (\<pi>::nat \<Rightarrow> ('n::finite))"
huffman@44135
   461
huffman@44135
   462
lemma bij_betw_pi:
huffman@44135
   463
  "bij_betw \<pi> {..<CARD('n::finite)} (UNIV::('n::finite) set)"
huffman@44135
   464
  using ex_bij_betw_nat_finite[of "UNIV::'n set"]
huffman@44135
   465
  by (auto simp: cart_bij_nat_def atLeast0LessThan
huffman@44135
   466
    intro!: someI_ex[of "\<lambda>x. bij_betw x {..<CARD('n)} (UNIV::'n set)"])
huffman@44135
   467
huffman@44135
   468
lemma bij_betw_pi'[intro]: "bij_betw \<pi>' (UNIV::'n set) {..<CARD('n::finite)}"
huffman@44135
   469
  using bij_betw_inv_into[OF bij_betw_pi] unfolding \<pi>'_def by auto
huffman@44135
   470
huffman@44135
   471
lemma pi'_inj[intro]: "inj \<pi>'"
huffman@44135
   472
  using bij_betw_pi' unfolding bij_betw_def by auto
huffman@44135
   473
huffman@44135
   474
lemma pi'_range[intro]: "\<And>i::'n. \<pi>' i < CARD('n::finite)"
huffman@44135
   475
  using bij_betw_pi' unfolding bij_betw_def by auto
huffman@44135
   476
huffman@44135
   477
lemma \<pi>\<pi>'[simp]: "\<And>i::'n::finite. \<pi> (\<pi>' i) = i"
huffman@44135
   478
  using bij_betw_pi by (auto intro!: f_inv_into_f simp: \<pi>'_def bij_betw_def)
huffman@44135
   479
huffman@44135
   480
lemma \<pi>'\<pi>[simp]: "\<And>i. i\<in>{..<CARD('n::finite)} \<Longrightarrow> \<pi>' (\<pi> i::'n) = i"
huffman@44135
   481
  using bij_betw_pi by (auto intro!: inv_into_f_eq simp: \<pi>'_def bij_betw_def)
huffman@44135
   482
huffman@44135
   483
lemma \<pi>\<pi>'_alt[simp]: "\<And>i. i<CARD('n::finite) \<Longrightarrow> \<pi>' (\<pi> i::'n) = i"
huffman@44135
   484
  by auto
huffman@44135
   485
huffman@44135
   486
lemma \<pi>_inj_on: "inj_on (\<pi>::nat\<Rightarrow>'n::finite) {..<CARD('n)}"
huffman@44135
   487
  using bij_betw_pi[where 'n='n] by (simp add: bij_betw_def)
huffman@44135
   488
huffman@44135
   489
instantiation cart :: (euclidean_space, finite) euclidean_space
huffman@44135
   490
begin
huffman@44135
   491
huffman@44135
   492
definition "dimension (t :: ('a ^ 'b) itself) = CARD('b) * DIM('a)"
huffman@44135
   493
huffman@44135
   494
definition "(basis i::'a^'b) =
huffman@44135
   495
  (if i < (CARD('b) * DIM('a))
huffman@44135
   496
  then (\<chi> j::'b. if j = \<pi>(i div DIM('a)) then basis (i mod DIM('a)) else 0)
huffman@44135
   497
  else 0)"
huffman@44135
   498
huffman@44135
   499
lemma basis_eq:
huffman@44135
   500
  assumes "i < CARD('b)" and "j < DIM('a)"
huffman@44135
   501
  shows "basis (j + i * DIM('a)) = (\<chi> k. if k = \<pi> i then basis j else 0)"
huffman@44135
   502
proof -
huffman@44135
   503
  have "j + i * DIM('a) <  DIM('a) * (i + 1)" using assms by (auto simp: field_simps)
huffman@44135
   504
  also have "\<dots> \<le> DIM('a) * CARD('b)" using assms unfolding mult_le_cancel1 by auto
huffman@44135
   505
  finally show ?thesis
huffman@44135
   506
    unfolding basis_cart_def using assms by (auto simp: Cart_eq not_less field_simps)
huffman@44135
   507
qed
huffman@44135
   508
huffman@44135
   509
lemma basis_eq_pi':
huffman@44135
   510
  assumes "j < DIM('a)"
huffman@44135
   511
  shows "basis (j + \<pi>' i * DIM('a)) $ k = (if k = i then basis j else 0)"
huffman@44135
   512
  apply (subst basis_eq)
huffman@44135
   513
  using pi'_range assms by simp_all
huffman@44135
   514
huffman@44135
   515
lemma split_times_into_modulo[consumes 1]:
huffman@44135
   516
  fixes k :: nat
huffman@44135
   517
  assumes "k < A * B"
huffman@44135
   518
  obtains i j where "i < A" and "j < B" and "k = j + i * B"
huffman@44135
   519
proof
huffman@44135
   520
  have "A * B \<noteq> 0"
huffman@44135
   521
  proof assume "A * B = 0" with assms show False by simp qed
huffman@44135
   522
  hence "0 < B" by auto
huffman@44135
   523
  thus "k mod B < B" using `0 < B` by auto
huffman@44135
   524
next
huffman@44135
   525
  have "k div B * B \<le> k div B * B + k mod B" by (rule le_add1)
huffman@44135
   526
  also have "... < A * B" using assms by simp
huffman@44135
   527
  finally show "k div B < A" by auto
huffman@44135
   528
qed simp
huffman@44135
   529
huffman@44135
   530
lemma split_CARD_DIM[consumes 1]:
huffman@44135
   531
  fixes k :: nat
huffman@44135
   532
  assumes k: "k < CARD('b) * DIM('a)"
huffman@44135
   533
  obtains i and j::'b where "i < DIM('a)" "k = i + \<pi>' j * DIM('a)"
huffman@44135
   534
proof -
huffman@44135
   535
  from split_times_into_modulo[OF k] guess i j . note ij = this
huffman@44135
   536
  show thesis
huffman@44135
   537
  proof
huffman@44135
   538
    show "j < DIM('a)" using ij by simp
huffman@44135
   539
    show "k = j + \<pi>' (\<pi> i :: 'b) * DIM('a)"
huffman@44135
   540
      using ij by simp
huffman@44135
   541
  qed
huffman@44135
   542
qed
huffman@44135
   543
huffman@44135
   544
lemma linear_less_than_times:
huffman@44135
   545
  fixes i j A B :: nat assumes "i < B" "j < A"
huffman@44135
   546
  shows "j + i * A < B * A"
huffman@44135
   547
proof -
huffman@44135
   548
  have "i * A + j < (Suc i)*A" using `j < A` by simp
huffman@44135
   549
  also have "\<dots> \<le> B * A" using `i < B` unfolding mult_le_cancel2 by simp
huffman@44135
   550
  finally show ?thesis by simp
huffman@44135
   551
qed
huffman@44135
   552
huffman@44135
   553
lemma DIM_cart[simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
huffman@44135
   554
  by (rule dimension_cart_def)
huffman@44135
   555
huffman@44135
   556
lemma all_less_DIM_cart:
huffman@44135
   557
  fixes m n :: nat
huffman@44135
   558
  shows "(\<forall>i<DIM('a^'b). P i) \<longleftrightarrow> (\<forall>x::'b. \<forall>i<DIM('a). P (i + \<pi>' x * DIM('a)))"
huffman@44135
   559
unfolding DIM_cart
huffman@44135
   560
apply safe
huffman@44135
   561
apply (drule spec, erule mp, erule linear_less_than_times [OF pi'_range])
huffman@44135
   562
apply (erule split_CARD_DIM, simp)
huffman@44135
   563
done
huffman@44135
   564
huffman@44135
   565
lemma eq_pi_iff:
huffman@44135
   566
  fixes x :: "'c::finite"
huffman@44135
   567
  shows "i < CARD('c::finite) \<Longrightarrow> x = \<pi> i \<longleftrightarrow> \<pi>' x = i"
huffman@44135
   568
  by auto
huffman@44135
   569
huffman@44135
   570
lemma all_less_mult:
huffman@44135
   571
  fixes m n :: nat
huffman@44135
   572
  shows "(\<forall>i<(m * n). P i) \<longleftrightarrow> (\<forall>i<m. \<forall>j<n. P (j + i * n))"
huffman@44135
   573
apply safe
huffman@44135
   574
apply (drule spec, erule mp, erule (1) linear_less_than_times)
huffman@44135
   575
apply (erule split_times_into_modulo, simp)
huffman@44135
   576
done
huffman@44135
   577
huffman@44135
   578
lemma inner_if:
huffman@44135
   579
  "inner (if a then x else y) z = (if a then inner x z else inner y z)"
huffman@44135
   580
  "inner x (if a then y else z) = (if a then inner x y else inner x z)"
huffman@44135
   581
  by simp_all
huffman@44135
   582
huffman@44135
   583
instance proof
huffman@44135
   584
  show "0 < DIM('a ^ 'b)"
huffman@44135
   585
    unfolding dimension_cart_def
huffman@44135
   586
    by (intro mult_pos_pos zero_less_card_finite DIM_positive)
huffman@44135
   587
next
huffman@44135
   588
  fix i :: nat
huffman@44135
   589
  assume "DIM('a ^ 'b) \<le> i" thus "basis i = (0::'a^'b)"
huffman@44135
   590
    unfolding dimension_cart_def basis_cart_def
huffman@44135
   591
    by simp
huffman@44135
   592
next
huffman@44135
   593
  show "\<forall>i<DIM('a ^ 'b). \<forall>j<DIM('a ^ 'b).
huffman@44135
   594
    inner (basis i :: 'a ^ 'b) (basis j) = (if i = j then 1 else 0)"
huffman@44135
   595
    apply (simp add: inner_vector_def)
huffman@44135
   596
    apply safe
huffman@44135
   597
    apply (erule split_CARD_DIM, simp add: basis_eq_pi')
huffman@44135
   598
    apply (simp add: inner_if setsum_delta cong: if_cong)
huffman@44135
   599
    apply (simp add: basis_orthonormal)
huffman@44135
   600
    apply (elim split_CARD_DIM, simp add: basis_eq_pi')
huffman@44135
   601
    apply (simp add: inner_if setsum_delta cong: if_cong)
huffman@44135
   602
    apply (clarsimp simp add: basis_orthonormal)
huffman@44135
   603
    done
huffman@44135
   604
next
huffman@44135
   605
  fix x :: "'a ^ 'b"
huffman@44135
   606
  show "(\<forall>i<DIM('a ^ 'b). inner (basis i) x = 0) \<longleftrightarrow> x = 0"
huffman@44135
   607
    unfolding all_less_DIM_cart
huffman@44135
   608
    unfolding inner_vector_def
huffman@44135
   609
    apply (simp add: basis_eq_pi')
huffman@44135
   610
    apply (simp add: inner_if setsum_delta cong: if_cong)
huffman@44135
   611
    apply (simp add: euclidean_all_zero)
huffman@44135
   612
    apply (simp add: Cart_eq)
huffman@44135
   613
    done
huffman@44135
   614
qed
huffman@44135
   615
huffman@36591
   616
end
huffman@44135
   617
huffman@44135
   618
end