src/HOL/Analysis/Finite_Cartesian_Product.thy
author wenzelm
Wed Feb 28 13:37:33 2018 +0100 (15 months ago)
changeset 67731 184c293f0a33
parent 67686 2c58505bf151
child 67732 39d80006fc29
permissions -rw-r--r--
clarified use of vec type syntax;
hoelzl@63627
     1
(*  Title:      HOL/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@66453
    11
  "HOL-Library.Numeral_Type"
wenzelm@66453
    12
  "HOL-Library.Countable_Set"
wenzelm@66453
    13
  "HOL-Library.FuncSet"
himmelma@33175
    14
begin
himmelma@33175
    15
wenzelm@60420
    16
subsection \<open>Finite Cartesian products, with indexing and lambdas.\<close>
himmelma@33175
    17
wenzelm@67731
    18
typedef ('a, 'b) vec = "UNIV :: ('b::finite \<Rightarrow> 'a) set"
huffman@44136
    19
  morphisms vec_nth vec_lambda ..
himmelma@33175
    20
lp15@67673
    21
declare vec_lambda_inject [simplified, simp]
lp15@67673
    22
immler@67685
    23
bundle vec_syntax begin
wenzelm@35254
    24
notation
huffman@44136
    25
  vec_nth (infixl "$" 90) and
huffman@44136
    26
  vec_lambda (binder "\<chi>" 10)
immler@67685
    27
end
immler@67685
    28
immler@67685
    29
bundle no_vec_syntax begin
immler@67685
    30
no_notation
immler@67685
    31
  vec_nth (infixl "$" 90) and
immler@67685
    32
  vec_lambda (binder "\<chi>" 10)
immler@67685
    33
end
immler@67685
    34
immler@67685
    35
unbundle vec_syntax
himmelma@33175
    36
wenzelm@67731
    37
text \<open>
wenzelm@67731
    38
  Concrete syntax for \<open>('a, 'b) vec\<close>:
wenzelm@67731
    39
    \<^item> \<open>'a^'b\<close> becomes \<open>('a, 'b::finite) vec\<close>
wenzelm@67731
    40
    \<^item> \<open>'a^'b::_\<close> becomes \<open>('a, 'b) vec\<close> without extra sort-constraint
wenzelm@67731
    41
\<close>
huffman@44136
    42
syntax "_finite_vec" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ ^/ _)" [15, 16] 15)
wenzelm@60420
    43
parse_translation \<open>
wenzelm@52143
    44
  let
wenzelm@52143
    45
    fun vec t u = Syntax.const @{type_syntax vec} $ t $ u;
wenzelm@52143
    46
    fun finite_vec_tr [t, u] =
wenzelm@52143
    47
      (case Term_Position.strip_positions u of
wenzelm@52143
    48
        v as Free (x, _) =>
wenzelm@52143
    49
          if Lexicon.is_tid x then
wenzelm@52143
    50
            vec t (Syntax.const @{syntax_const "_ofsort"} $ v $
wenzelm@52143
    51
              Syntax.const @{class_syntax finite})
wenzelm@52143
    52
          else vec t u
wenzelm@52143
    53
      | _ => vec t u)
wenzelm@52143
    54
  in
wenzelm@52143
    55
    [(@{syntax_const "_finite_vec"}, K finite_vec_tr)]
wenzelm@52143
    56
  end
wenzelm@60420
    57
\<close>
hoelzl@34290
    58
huffman@44136
    59
lemma vec_eq_iff: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
huffman@44136
    60
  by (simp add: vec_nth_inject [symmetric] fun_eq_iff)
himmelma@33175
    61
huffman@44136
    62
lemma vec_lambda_beta [simp]: "vec_lambda g $ i = g i"
huffman@44136
    63
  by (simp add: vec_lambda_inverse)
himmelma@33175
    64
huffman@44136
    65
lemma vec_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> vec_lambda g = f"
huffman@44136
    66
  by (auto simp add: vec_eq_iff)
himmelma@33175
    67
lp15@67673
    68
lemma vec_lambda_eta [simp]: "(\<chi> i. (g$i)) = g"
huffman@44136
    69
  by (simp add: vec_eq_iff)
himmelma@33175
    70
eberlm@66281
    71
subsection \<open>Cardinality of vectors\<close>
eberlm@66281
    72
eberlm@66281
    73
instance vec :: (finite, finite) finite
eberlm@66281
    74
proof
eberlm@66281
    75
  show "finite (UNIV :: ('a, 'b) vec set)"
eberlm@66281
    76
  proof (subst bij_betw_finite)
eberlm@66281
    77
    show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
    78
      by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
eberlm@66281
    79
    have "finite (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
    80
      by (intro finite_PiE) auto
eberlm@66281
    81
    also have "(PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set)) = Pi UNIV (\<lambda>_. UNIV)"
eberlm@66281
    82
      by auto
eberlm@66281
    83
    finally show "finite \<dots>" .
eberlm@66281
    84
  qed
eberlm@66281
    85
qed
eberlm@66281
    86
eberlm@66281
    87
lemma countable_PiE:
eberlm@66281
    88
  "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (Pi\<^sub>E I F)"
eberlm@66281
    89
  by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)
eberlm@66281
    90
eberlm@66281
    91
instance vec :: (countable, finite) countable
eberlm@66281
    92
proof
eberlm@66281
    93
  have "countable (UNIV :: ('a, 'b) vec set)"
eberlm@66281
    94
  proof (rule countableI_bij2)
eberlm@66281
    95
    show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
    96
      by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
eberlm@66281
    97
    have "countable (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
    98
      by (intro countable_PiE) auto
eberlm@66281
    99
    also have "(PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set)) = Pi UNIV (\<lambda>_. UNIV)"
eberlm@66281
   100
      by auto
eberlm@66281
   101
    finally show "countable \<dots>" .
eberlm@66281
   102
  qed
eberlm@66281
   103
  thus "\<exists>t::('a, 'b) vec \<Rightarrow> nat. inj t"
eberlm@66281
   104
    by (auto elim!: countableE)
eberlm@66281
   105
qed
eberlm@66281
   106
eberlm@66281
   107
lemma infinite_UNIV_vec:
eberlm@66281
   108
  assumes "infinite (UNIV :: 'a set)"
wenzelm@67731
   109
  shows   "infinite (UNIV :: ('a^'b) set)"
eberlm@66281
   110
proof (subst bij_betw_finite)
eberlm@66281
   111
  show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
   112
    by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
eberlm@66281
   113
  have "infinite (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))" (is "infinite ?A")
eberlm@66281
   114
  proof
eberlm@66281
   115
    assume "finite ?A"
eberlm@66281
   116
    hence "finite ((\<lambda>f. f undefined) ` ?A)"
eberlm@66281
   117
      by (rule finite_imageI)
eberlm@66281
   118
    also have "(\<lambda>f. f undefined) ` ?A = UNIV"
eberlm@66281
   119
      by auto
eberlm@66281
   120
    finally show False 
eberlm@66281
   121
      using \<open>infinite (UNIV :: 'a set)\<close> by contradiction
eberlm@66281
   122
  qed
eberlm@66281
   123
  also have "?A = Pi UNIV (\<lambda>_. UNIV)" 
eberlm@66281
   124
    by auto
eberlm@66281
   125
  finally show "infinite (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))" .
eberlm@66281
   126
qed
eberlm@66281
   127
eberlm@66281
   128
lemma CARD_vec [simp]:
wenzelm@67731
   129
  "CARD('a^'b) = CARD('a) ^ CARD('b)"
eberlm@66281
   130
proof (cases "finite (UNIV :: 'a set)")
eberlm@66281
   131
  case True
eberlm@66281
   132
  show ?thesis
eberlm@66281
   133
  proof (subst bij_betw_same_card)
eberlm@66281
   134
    show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
   135
      by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
eberlm@66281
   136
    have "CARD('a) ^ CARD('b) = card (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
eberlm@66281
   137
      (is "_ = card ?A")
eberlm@66281
   138
      by (subst card_PiE) (auto simp: prod_constant)
eberlm@66281
   139
    
eberlm@66281
   140
    also have "?A = Pi UNIV (\<lambda>_. UNIV)" 
eberlm@66281
   141
      by auto
eberlm@66281
   142
    finally show "card \<dots> = CARD('a) ^ CARD('b)" ..
eberlm@66281
   143
  qed
eberlm@66281
   144
qed (simp_all add: infinite_UNIV_vec)
huffman@36591
   145
lp15@67683
   146
lemma countable_vector:
lp15@67683
   147
  fixes B:: "'n::finite \<Rightarrow> 'a set"
lp15@67683
   148
  assumes "\<And>i. countable (B i)"
lp15@67683
   149
  shows "countable {V. \<forall>i::'n::finite. V $ i \<in> B i}"
lp15@67683
   150
proof -
lp15@67683
   151
  have "f \<in> ($) ` {V. \<forall>i. V $ i \<in> B i}" if "f \<in> Pi\<^sub>E UNIV B" for f
lp15@67683
   152
  proof -
lp15@67683
   153
    have "\<exists>W. (\<forall>i. W $ i \<in> B i) \<and> ($) W = f"
lp15@67683
   154
      by (metis that PiE_iff UNIV_I vec_lambda_inverse)
lp15@67683
   155
    then show "f \<in> ($) ` {v. \<forall>i. v $ i \<in> B i}"
lp15@67683
   156
      by blast
lp15@67683
   157
  qed
lp15@67683
   158
  then have "Pi\<^sub>E UNIV B = vec_nth ` {V. \<forall>i::'n. V $ i \<in> B i}"
lp15@67683
   159
    by blast
lp15@67683
   160
  then have "countable (vec_nth ` {V. \<forall>i. V $ i \<in> B i})"
lp15@67683
   161
    by (metis finite_class.finite_UNIV countable_PiE assms)
lp15@67683
   162
  then have "countable (vec_lambda ` vec_nth ` {V. \<forall>i. V $ i \<in> B i})"
lp15@67683
   163
    by auto
lp15@67683
   164
  then show ?thesis
lp15@67683
   165
    by (simp add: image_comp o_def vec_nth_inverse)
lp15@67683
   166
qed
lp15@67683
   167
wenzelm@60420
   168
subsection \<open>Group operations and class instances\<close>
huffman@36591
   169
huffman@44136
   170
instantiation vec :: (zero, finite) zero
huffman@36591
   171
begin
huffman@44136
   172
  definition "0 \<equiv> (\<chi> i. 0)"
huffman@36591
   173
  instance ..
himmelma@33175
   174
end
huffman@36591
   175
huffman@44136
   176
instantiation vec :: (plus, finite) plus
huffman@36591
   177
begin
nipkow@67399
   178
  definition "(+) \<equiv> (\<lambda> x y. (\<chi> i. x$i + y$i))"
huffman@36591
   179
  instance ..
huffman@36591
   180
end
huffman@36591
   181
huffman@44136
   182
instantiation vec :: (minus, finite) minus
huffman@36591
   183
begin
nipkow@67399
   184
  definition "(-) \<equiv> (\<lambda> x y. (\<chi> i. x$i - y$i))"
huffman@36591
   185
  instance ..
huffman@36591
   186
end
huffman@36591
   187
huffman@44136
   188
instantiation vec :: (uminus, finite) uminus
huffman@36591
   189
begin
huffman@44136
   190
  definition "uminus \<equiv> (\<lambda> x. (\<chi> i. - (x$i)))"
huffman@36591
   191
  instance ..
huffman@36591
   192
end
huffman@36591
   193
huffman@36591
   194
lemma zero_index [simp]: "0 $ i = 0"
huffman@44136
   195
  unfolding zero_vec_def by simp
huffman@36591
   196
huffman@36591
   197
lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
huffman@44136
   198
  unfolding plus_vec_def by simp
huffman@36591
   199
huffman@36591
   200
lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
huffman@44136
   201
  unfolding minus_vec_def by simp
huffman@36591
   202
huffman@36591
   203
lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
huffman@44136
   204
  unfolding uminus_vec_def by simp
huffman@36591
   205
huffman@44136
   206
instance vec :: (semigroup_add, finite) semigroup_add
wenzelm@61169
   207
  by standard (simp add: vec_eq_iff add.assoc)
huffman@36591
   208
huffman@44136
   209
instance vec :: (ab_semigroup_add, finite) ab_semigroup_add
wenzelm@61169
   210
  by standard (simp add: vec_eq_iff add.commute)
huffman@36591
   211
huffman@44136
   212
instance vec :: (monoid_add, finite) monoid_add
wenzelm@61169
   213
  by standard (simp_all add: vec_eq_iff)
huffman@36591
   214
huffman@44136
   215
instance vec :: (comm_monoid_add, finite) comm_monoid_add
wenzelm@61169
   216
  by standard (simp add: vec_eq_iff)
huffman@36591
   217
huffman@44136
   218
instance vec :: (cancel_semigroup_add, finite) cancel_semigroup_add
wenzelm@61169
   219
  by standard (simp_all add: vec_eq_iff)
huffman@36591
   220
huffman@44136
   221
instance vec :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
wenzelm@61169
   222
  by standard (simp_all add: vec_eq_iff diff_diff_eq)
huffman@36591
   223
huffman@44136
   224
instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
huffman@36591
   225
huffman@44136
   226
instance vec :: (group_add, finite) group_add
wenzelm@61169
   227
  by standard (simp_all add: vec_eq_iff)
huffman@36591
   228
huffman@44136
   229
instance vec :: (ab_group_add, finite) ab_group_add
wenzelm@61169
   230
  by standard (simp_all add: vec_eq_iff)
huffman@36591
   231
huffman@36591
   232
wenzelm@60420
   233
subsection \<open>Real vector space\<close>
huffman@36591
   234
huffman@44136
   235
instantiation vec :: (real_vector, finite) real_vector
huffman@36591
   236
begin
huffman@36591
   237
huffman@44136
   238
definition "scaleR \<equiv> (\<lambda> r x. (\<chi> i. scaleR r (x$i)))"
huffman@36591
   239
huffman@36591
   240
lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)"
huffman@44136
   241
  unfolding scaleR_vec_def by simp
huffman@36591
   242
huffman@36591
   243
instance
wenzelm@61169
   244
  by standard (simp_all add: vec_eq_iff scaleR_left_distrib scaleR_right_distrib)
huffman@36591
   245
huffman@36591
   246
end
huffman@36591
   247
huffman@36591
   248
wenzelm@60420
   249
subsection \<open>Topological space\<close>
huffman@36591
   250
huffman@44136
   251
instantiation vec :: (topological_space, finite) topological_space
huffman@36591
   252
begin
huffman@36591
   253
hoelzl@62101
   254
definition [code del]:
huffman@36591
   255
  "open (S :: ('a ^ 'b) set) \<longleftrightarrow>
huffman@36591
   256
    (\<forall>x\<in>S. \<exists>A. (\<forall>i. open (A i) \<and> x$i \<in> A i) \<and>
huffman@36591
   257
      (\<forall>y. (\<forall>i. y$i \<in> A i) \<longrightarrow> y \<in> S))"
huffman@36591
   258
huffman@36591
   259
instance proof
huffman@36591
   260
  show "open (UNIV :: ('a ^ 'b) set)"
huffman@44136
   261
    unfolding open_vec_def by auto
huffman@36591
   262
next
huffman@36591
   263
  fix S T :: "('a ^ 'b) set"
huffman@36591
   264
  assume "open S" "open T" thus "open (S \<inter> T)"
huffman@44136
   265
    unfolding open_vec_def
huffman@36591
   266
    apply clarify
huffman@36591
   267
    apply (drule (1) bspec)+
huffman@36591
   268
    apply (clarify, rename_tac Sa Ta)
huffman@36591
   269
    apply (rule_tac x="\<lambda>i. Sa i \<inter> Ta i" in exI)
huffman@36591
   270
    apply (simp add: open_Int)
huffman@36591
   271
    done
huffman@36591
   272
next
huffman@36591
   273
  fix K :: "('a ^ 'b) set set"
huffman@36591
   274
  assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
huffman@44136
   275
    unfolding open_vec_def
huffman@36591
   276
    apply clarify
huffman@36591
   277
    apply (drule (1) bspec)
huffman@36591
   278
    apply (drule (1) bspec)
huffman@36591
   279
    apply clarify
huffman@36591
   280
    apply (rule_tac x=A in exI)
huffman@36591
   281
    apply fast
huffman@36591
   282
    done
huffman@36591
   283
qed
huffman@36591
   284
huffman@36591
   285
end
huffman@36591
   286
huffman@36591
   287
lemma open_vector_box: "\<forall>i. open (S i) \<Longrightarrow> open {x. \<forall>i. x $ i \<in> S i}"
huffman@44136
   288
  unfolding open_vec_def by auto
huffman@36591
   289
huffman@44136
   290
lemma open_vimage_vec_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
huffman@44136
   291
  unfolding open_vec_def
huffman@44136
   292
  apply clarify
huffman@44136
   293
  apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI, simp)
huffman@44136
   294
  done
huffman@36591
   295
huffman@44136
   296
lemma closed_vimage_vec_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
huffman@44136
   297
  unfolding closed_open vimage_Compl [symmetric]
huffman@44136
   298
  by (rule open_vimage_vec_nth)
huffman@36591
   299
huffman@36591
   300
lemma closed_vector_box: "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@36591
   301
proof -
huffman@36591
   302
  have "{x. \<forall>i. x $ i \<in> S i} = (\<Inter>i. (\<lambda>x. x $ i) -` S i)" by auto
huffman@36591
   303
  thus "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
huffman@44136
   304
    by (simp add: closed_INT closed_vimage_vec_nth)
huffman@36591
   305
qed
huffman@36591
   306
huffman@44136
   307
lemma tendsto_vec_nth [tendsto_intros]:
wenzelm@61973
   308
  assumes "((\<lambda>x. f x) \<longlongrightarrow> a) net"
wenzelm@61973
   309
  shows "((\<lambda>x. f x $ i) \<longlongrightarrow> a $ i) net"
huffman@36591
   310
proof (rule topological_tendstoI)
huffman@36591
   311
  fix S assume "open S" "a $ i \<in> S"
huffman@36591
   312
  then have "open ((\<lambda>y. y $ i) -` S)" "a \<in> ((\<lambda>y. y $ i) -` S)"
huffman@44136
   313
    by (simp_all add: open_vimage_vec_nth)
huffman@36591
   314
  with assms have "eventually (\<lambda>x. f x \<in> (\<lambda>y. y $ i) -` S) net"
huffman@36591
   315
    by (rule topological_tendstoD)
huffman@36591
   316
  then show "eventually (\<lambda>x. f x $ i \<in> S) net"
huffman@36591
   317
    by simp
huffman@36591
   318
qed
huffman@36591
   319
huffman@44631
   320
lemma isCont_vec_nth [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. f x $ i) a"
huffman@44631
   321
  unfolding isCont_def by (rule tendsto_vec_nth)
huffman@44631
   322
huffman@44136
   323
lemma vec_tendstoI:
wenzelm@61973
   324
  assumes "\<And>i. ((\<lambda>x. f x $ i) \<longlongrightarrow> a $ i) net"
wenzelm@61973
   325
  shows "((\<lambda>x. f x) \<longlongrightarrow> a) net"
huffman@36591
   326
proof (rule topological_tendstoI)
huffman@36591
   327
  fix S assume "open S" and "a \<in> S"
huffman@36591
   328
  then obtain A where A: "\<And>i. open (A i)" "\<And>i. a $ i \<in> A i"
huffman@36591
   329
    and S: "\<And>y. \<forall>i. y $ i \<in> A i \<Longrightarrow> y \<in> S"
huffman@44136
   330
    unfolding open_vec_def by metis
huffman@36591
   331
  have "\<And>i. eventually (\<lambda>x. f x $ i \<in> A i) net"
huffman@36591
   332
    using assms A by (rule topological_tendstoD)
huffman@36591
   333
  hence "eventually (\<lambda>x. \<forall>i. f x $ i \<in> A i) net"
huffman@36591
   334
    by (rule eventually_all_finite)
huffman@36591
   335
  thus "eventually (\<lambda>x. f x \<in> S) net"
lp15@61810
   336
    by (rule eventually_mono, simp add: S)
huffman@36591
   337
qed
huffman@36591
   338
huffman@44136
   339
lemma tendsto_vec_lambda [tendsto_intros]:
wenzelm@61973
   340
  assumes "\<And>i. ((\<lambda>x. f x i) \<longlongrightarrow> a i) net"
wenzelm@61973
   341
  shows "((\<lambda>x. \<chi> i. f x i) \<longlongrightarrow> (\<chi> i. a i)) net"
huffman@44136
   342
  using assms by (simp add: vec_tendstoI)
huffman@36591
   343
huffman@44571
   344
lemma open_image_vec_nth: assumes "open S" shows "open ((\<lambda>x. x $ i) ` S)"
huffman@44571
   345
proof (rule openI)
huffman@44571
   346
  fix a assume "a \<in> (\<lambda>x. x $ i) ` S"
huffman@44571
   347
  then obtain z where "a = z $ i" and "z \<in> S" ..
huffman@44571
   348
  then obtain A where A: "\<forall>i. open (A i) \<and> z $ i \<in> A i"
huffman@44571
   349
    and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
wenzelm@60420
   350
    using \<open>open S\<close> unfolding open_vec_def by auto
huffman@44571
   351
  hence "A i \<subseteq> (\<lambda>x. x $ i) ` S"
huffman@44571
   352
    by (clarsimp, rule_tac x="\<chi> j. if j = i then x else z $ j" in image_eqI,
huffman@44571
   353
      simp_all)
huffman@44571
   354
  hence "open (A i) \<and> a \<in> A i \<and> A i \<subseteq> (\<lambda>x. x $ i) ` S"
wenzelm@60420
   355
    using A \<open>a = z $ i\<close> by simp
huffman@44571
   356
  then show "\<exists>T. open T \<and> a \<in> T \<and> T \<subseteq> (\<lambda>x. x $ i) ` S" by - (rule exI)
huffman@44571
   357
qed
huffman@36591
   358
huffman@44571
   359
instance vec :: (perfect_space, finite) perfect_space
huffman@44571
   360
proof
huffman@44571
   361
  fix x :: "'a ^ 'b" show "\<not> open {x}"
huffman@44571
   362
  proof
huffman@44571
   363
    assume "open {x}"
hoelzl@62102
   364
    hence "\<forall>i. open ((\<lambda>x. x $ i) ` {x})" by (fast intro: open_image_vec_nth)
huffman@44571
   365
    hence "\<forall>i. open {x $ i}" by simp
huffman@44571
   366
    thus "False" by (simp add: not_open_singleton)
huffman@44571
   367
  qed
huffman@44571
   368
qed
huffman@44571
   369
huffman@44571
   370
wenzelm@60420
   371
subsection \<open>Metric space\<close>
hoelzl@62101
   372
(* TODO: Product of uniform spaces and compatibility with metric_spaces! *)
huffman@36591
   373
hoelzl@62101
   374
instantiation vec :: (metric_space, finite) dist
huffman@36591
   375
begin
huffman@36591
   376
huffman@44136
   377
definition
nipkow@67155
   378
  "dist x y = L2_set (\<lambda>i. dist (x$i) (y$i)) UNIV"
huffman@36591
   379
hoelzl@62101
   380
instance ..
hoelzl@62101
   381
end
hoelzl@62101
   382
hoelzl@62101
   383
instantiation vec :: (metric_space, finite) uniformity_dist
hoelzl@62101
   384
begin
hoelzl@62101
   385
hoelzl@62101
   386
definition [code del]:
wenzelm@67731
   387
  "(uniformity :: (('a^'b::_) \<times> ('a^'b::_)) filter) =
hoelzl@62101
   388
    (INF e:{0 <..}. principal {(x, y). dist x y < e})"
hoelzl@62101
   389
hoelzl@62102
   390
instance
hoelzl@62101
   391
  by standard (rule uniformity_vec_def)
hoelzl@62101
   392
end
hoelzl@62101
   393
hoelzl@62102
   394
declare uniformity_Abort[where 'a="'a :: metric_space ^ 'b :: finite", code]
hoelzl@62102
   395
hoelzl@62101
   396
instantiation vec :: (metric_space, finite) metric_space
hoelzl@62101
   397
begin
hoelzl@62101
   398
huffman@44136
   399
lemma dist_vec_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
nipkow@67155
   400
  unfolding dist_vec_def by (rule member_le_L2_set) simp_all
huffman@36591
   401
huffman@36591
   402
instance proof
huffman@36591
   403
  fix x y :: "'a ^ 'b"
huffman@36591
   404
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@44136
   405
    unfolding dist_vec_def
nipkow@67155
   406
    by (simp add: L2_set_eq_0_iff vec_eq_iff)
huffman@36591
   407
next
huffman@36591
   408
  fix x y z :: "'a ^ 'b"
huffman@36591
   409
  show "dist x y \<le> dist x z + dist y z"
huffman@44136
   410
    unfolding dist_vec_def
nipkow@67155
   411
    apply (rule order_trans [OF _ L2_set_triangle_ineq])
nipkow@67155
   412
    apply (simp add: L2_set_mono dist_triangle2)
huffman@36591
   413
    done
huffman@36591
   414
next
huffman@36591
   415
  fix S :: "('a ^ 'b) set"
hoelzl@62101
   416
  have *: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@44630
   417
  proof
huffman@44630
   418
    assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   419
    proof
huffman@44630
   420
      fix x assume "x \<in> S"
huffman@44630
   421
      obtain A where A: "\<forall>i. open (A i)" "\<forall>i. x $ i \<in> A i"
huffman@44630
   422
        and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
wenzelm@60420
   423
        using \<open>open S\<close> and \<open>x \<in> S\<close> unfolding open_vec_def by metis
huffman@44630
   424
      have "\<forall>i\<in>UNIV. \<exists>r>0. \<forall>y. dist y (x $ i) < r \<longrightarrow> y \<in> A i"
huffman@44630
   425
        using A unfolding open_dist by simp
huffman@44630
   426
      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
   427
        by (rule finite_set_choice [OF finite])
huffman@44630
   428
      then obtain r where r1: "\<forall>i. 0 < r i"
huffman@44630
   429
        and r2: "\<forall>i y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i" by fast
huffman@44630
   430
      have "0 < Min (range r) \<and> (\<forall>y. dist y x < Min (range r) \<longrightarrow> y \<in> S)"
huffman@44630
   431
        by (simp add: r1 r2 S le_less_trans [OF dist_vec_nth_le])
huffman@44630
   432
      thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
huffman@44630
   433
    qed
huffman@44630
   434
  next
huffman@44630
   435
    assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
huffman@44630
   436
    proof (unfold open_vec_def, rule)
huffman@44630
   437
      fix x assume "x \<in> S"
huffman@44630
   438
      then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44630
   439
        using * by fast
wenzelm@63040
   440
      define r where [abs_def]: "r i = e / sqrt (of_nat CARD('b))" for i :: 'b
wenzelm@60420
   441
      from \<open>0 < e\<close> have r: "\<forall>i. 0 < r i"
nipkow@56541
   442
        unfolding r_def by simp_all
nipkow@67155
   443
      from \<open>0 < e\<close> have e: "e = L2_set r UNIV"
nipkow@67155
   444
        unfolding r_def by (simp add: L2_set_constant)
wenzelm@63040
   445
      define A where "A i = {y. dist (x $ i) y < r i}" for i
huffman@44630
   446
      have "\<forall>i. open (A i) \<and> x $ i \<in> A i"
huffman@44630
   447
        unfolding A_def by (simp add: open_ball r)
huffman@44630
   448
      moreover have "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
nipkow@67155
   449
        by (simp add: A_def S dist_vec_def e L2_set_strict_mono dist_commute)
huffman@44630
   450
      ultimately show "\<exists>A. (\<forall>i. open (A i) \<and> x $ i \<in> A i) \<and>
huffman@44630
   451
        (\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S)" by metis
huffman@44630
   452
    qed
huffman@44630
   453
  qed
hoelzl@62101
   454
  show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
hoelzl@62101
   455
    unfolding * eventually_uniformity_metric
hoelzl@62101
   456
    by (simp del: split_paired_All add: dist_vec_def dist_commute)
huffman@36591
   457
qed
huffman@36591
   458
huffman@36591
   459
end
huffman@36591
   460
huffman@44136
   461
lemma Cauchy_vec_nth:
huffman@36591
   462
  "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
huffman@44136
   463
  unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le])
huffman@36591
   464
huffman@44136
   465
lemma vec_CauchyI:
huffman@36591
   466
  fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
huffman@36591
   467
  assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
huffman@36591
   468
  shows "Cauchy (\<lambda>n. X n)"
huffman@36591
   469
proof (rule metric_CauchyI)
huffman@36591
   470
  fix r :: real assume "0 < r"
nipkow@56541
   471
  hence "0 < r / of_nat CARD('n)" (is "0 < ?s") by simp
wenzelm@63040
   472
  define N where "N i = (LEAST N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s)" for i
wenzelm@63040
   473
  define M where "M = Max (range N)"
huffman@36591
   474
  have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
wenzelm@60420
   475
    using X \<open>0 < ?s\<close> by (rule metric_CauchyD)
huffman@36591
   476
  hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   477
    unfolding N_def by (rule LeastI_ex)
huffman@36591
   478
  hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
huffman@36591
   479
    unfolding M_def by simp
huffman@36591
   480
  {
huffman@36591
   481
    fix m n :: nat
huffman@36591
   482
    assume "M \<le> m" "M \<le> n"
nipkow@67155
   483
    have "dist (X m) (X n) = L2_set (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
huffman@44136
   484
      unfolding dist_vec_def ..
nipkow@64267
   485
    also have "\<dots> \<le> sum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
nipkow@67155
   486
      by (rule L2_set_le_sum [OF zero_le_dist])
nipkow@64267
   487
    also have "\<dots> < sum (\<lambda>i::'n. ?s) UNIV"
nipkow@64267
   488
      by (rule sum_strict_mono, simp_all add: M \<open>M \<le> m\<close> \<open>M \<le> n\<close>)
huffman@36591
   489
    also have "\<dots> = r"
huffman@36591
   490
      by simp
huffman@36591
   491
    finally have "dist (X m) (X n) < r" .
huffman@36591
   492
  }
huffman@36591
   493
  hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
huffman@36591
   494
    by simp
huffman@36591
   495
  then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
huffman@36591
   496
qed
huffman@36591
   497
huffman@44136
   498
instance vec :: (complete_space, finite) complete_space
huffman@36591
   499
proof
huffman@36591
   500
  fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
wenzelm@61969
   501
  have "\<And>i. (\<lambda>n. X n $ i) \<longlonglongrightarrow> lim (\<lambda>n. X n $ i)"
wenzelm@60420
   502
    using Cauchy_vec_nth [OF \<open>Cauchy X\<close>]
huffman@36591
   503
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
wenzelm@61969
   504
  hence "X \<longlonglongrightarrow> vec_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
huffman@44136
   505
    by (simp add: vec_tendstoI)
huffman@36591
   506
  then show "convergent X"
huffman@36591
   507
    by (rule convergentI)
huffman@36591
   508
qed
huffman@36591
   509
huffman@36591
   510
wenzelm@60420
   511
subsection \<open>Normed vector space\<close>
huffman@36591
   512
huffman@44136
   513
instantiation vec :: (real_normed_vector, finite) real_normed_vector
huffman@36591
   514
begin
huffman@36591
   515
nipkow@67155
   516
definition "norm x = L2_set (\<lambda>i. norm (x$i)) UNIV"
huffman@36591
   517
huffman@44141
   518
definition "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
huffman@36591
   519
huffman@36591
   520
instance proof
huffman@36591
   521
  fix a :: real and x y :: "'a ^ 'b"
huffman@36591
   522
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   523
    unfolding norm_vec_def
nipkow@67155
   524
    by (simp add: L2_set_eq_0_iff vec_eq_iff)
huffman@36591
   525
  show "norm (x + y) \<le> norm x + norm y"
huffman@44136
   526
    unfolding norm_vec_def
nipkow@67155
   527
    apply (rule order_trans [OF _ L2_set_triangle_ineq])
nipkow@67155
   528
    apply (simp add: L2_set_mono norm_triangle_ineq)
huffman@36591
   529
    done
huffman@36591
   530
  show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
huffman@44136
   531
    unfolding norm_vec_def
nipkow@67155
   532
    by (simp add: L2_set_right_distrib)
huffman@36591
   533
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@44141
   534
    by (rule sgn_vec_def)
huffman@36591
   535
  show "dist x y = norm (x - y)"
huffman@44136
   536
    unfolding dist_vec_def norm_vec_def
huffman@36591
   537
    by (simp add: dist_norm)
huffman@36591
   538
qed
huffman@36591
   539
huffman@36591
   540
end
huffman@36591
   541
huffman@36591
   542
lemma norm_nth_le: "norm (x $ i) \<le> norm x"
huffman@44136
   543
unfolding norm_vec_def
nipkow@67155
   544
by (rule member_le_L2_set) simp_all
huffman@36591
   545
huffman@44282
   546
lemma bounded_linear_vec_nth: "bounded_linear (\<lambda>x. x $ i)"
wenzelm@61169
   547
apply standard
huffman@36591
   548
apply (rule vector_add_component)
huffman@36591
   549
apply (rule vector_scaleR_component)
huffman@36591
   550
apply (rule_tac x="1" in exI, simp add: norm_nth_le)
huffman@36591
   551
done
huffman@36591
   552
huffman@44136
   553
instance vec :: (banach, finite) banach ..
huffman@36591
   554
huffman@36591
   555
wenzelm@60420
   556
subsection \<open>Inner product space\<close>
huffman@36591
   557
huffman@44136
   558
instantiation vec :: (real_inner, finite) real_inner
huffman@36591
   559
begin
huffman@36591
   560
nipkow@64267
   561
definition "inner x y = sum (\<lambda>i. inner (x$i) (y$i)) UNIV"
huffman@36591
   562
huffman@36591
   563
instance proof
huffman@36591
   564
  fix r :: real and x y z :: "'a ^ 'b"
huffman@36591
   565
  show "inner x y = inner y x"
huffman@44136
   566
    unfolding inner_vec_def
huffman@36591
   567
    by (simp add: inner_commute)
huffman@36591
   568
  show "inner (x + y) z = inner x z + inner y z"
huffman@44136
   569
    unfolding inner_vec_def
nipkow@64267
   570
    by (simp add: inner_add_left sum.distrib)
huffman@36591
   571
  show "inner (scaleR r x) y = r * inner x y"
huffman@44136
   572
    unfolding inner_vec_def
nipkow@64267
   573
    by (simp add: sum_distrib_left)
huffman@36591
   574
  show "0 \<le> inner x x"
huffman@44136
   575
    unfolding inner_vec_def
nipkow@64267
   576
    by (simp add: sum_nonneg)
huffman@36591
   577
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@44136
   578
    unfolding inner_vec_def
nipkow@64267
   579
    by (simp add: vec_eq_iff sum_nonneg_eq_0_iff)
huffman@36591
   580
  show "norm x = sqrt (inner x x)"
nipkow@67155
   581
    unfolding inner_vec_def norm_vec_def L2_set_def
huffman@36591
   582
    by (simp add: power2_norm_eq_inner)
huffman@36591
   583
qed
huffman@36591
   584
huffman@36591
   585
end
huffman@36591
   586
huffman@44166
   587
wenzelm@60420
   588
subsection \<open>Euclidean space\<close>
huffman@44135
   589
wenzelm@60420
   590
text \<open>Vectors pointing along a single axis.\<close>
huffman@44166
   591
huffman@44166
   592
definition "axis k x = (\<chi> i. if i = k then x else 0)"
huffman@44166
   593
huffman@44166
   594
lemma axis_nth [simp]: "axis i x $ i = x"
huffman@44166
   595
  unfolding axis_def by simp
huffman@44166
   596
huffman@44166
   597
lemma axis_eq_axis: "axis i x = axis j y \<longleftrightarrow> x = y \<and> i = j \<or> x = 0 \<and> y = 0"
huffman@44166
   598
  unfolding axis_def vec_eq_iff by auto
huffman@44166
   599
huffman@44166
   600
lemma inner_axis_axis:
huffman@44166
   601
  "inner (axis i x) (axis j y) = (if i = j then inner x y else 0)"
huffman@44166
   602
  unfolding inner_vec_def
huffman@44166
   603
  apply (cases "i = j")
huffman@44166
   604
  apply clarsimp
nipkow@64267
   605
  apply (subst sum.remove [of _ j], simp_all)
nipkow@64267
   606
  apply (rule sum.neutral, simp add: axis_def)
nipkow@64267
   607
  apply (rule sum.neutral, simp add: axis_def)
huffman@44166
   608
  done
huffman@44166
   609
nipkow@64267
   610
lemma sum_single:
huffman@44166
   611
  assumes "finite A" and "k \<in> A" and "f k = y"
huffman@44166
   612
  assumes "\<And>i. i \<in> A \<Longrightarrow> i \<noteq> k \<Longrightarrow> f i = 0"
huffman@44166
   613
  shows "(\<Sum>i\<in>A. f i) = y"
nipkow@64267
   614
  apply (subst sum.remove [OF assms(1,2)])
nipkow@64267
   615
  apply (simp add: sum.neutral assms(3,4))
huffman@44166
   616
  done
huffman@44166
   617
huffman@44166
   618
lemma inner_axis: "inner x (axis i y) = inner (x $ i) y"
huffman@44166
   619
  unfolding inner_vec_def
nipkow@64267
   620
  apply (rule_tac k=i in sum_single)
huffman@44166
   621
  apply simp_all
huffman@44166
   622
  apply (simp add: axis_def)
huffman@44166
   623
  done
huffman@44166
   624
lp15@67683
   625
lemma inner_axis': "inner(axis i y) x = inner y (x $ i)"
lp15@67683
   626
  by (simp add: inner_axis inner_commute)
lp15@67683
   627
huffman@44136
   628
instantiation vec :: (euclidean_space, finite) euclidean_space
huffman@44135
   629
begin
huffman@44135
   630
huffman@44166
   631
definition "Basis = (\<Union>i. \<Union>u\<in>Basis. {axis i u})"
huffman@44166
   632
huffman@44135
   633
instance proof
huffman@44166
   634
  show "(Basis :: ('a ^ 'b) set) \<noteq> {}"
huffman@44166
   635
    unfolding Basis_vec_def by simp
huffman@44166
   636
next
huffman@44166
   637
  show "finite (Basis :: ('a ^ 'b) set)"
huffman@44166
   638
    unfolding Basis_vec_def by simp
huffman@44135
   639
next
huffman@44166
   640
  fix u v :: "'a ^ 'b"
huffman@44166
   641
  assume "u \<in> Basis" and "v \<in> Basis"
huffman@44166
   642
  thus "inner u v = (if u = v then 1 else 0)"
huffman@44166
   643
    unfolding Basis_vec_def
huffman@44166
   644
    by (auto simp add: inner_axis_axis axis_eq_axis inner_Basis)
huffman@44135
   645
next
huffman@44166
   646
  fix x :: "'a ^ 'b"
huffman@44166
   647
  show "(\<forall>u\<in>Basis. inner x u = 0) \<longleftrightarrow> x = 0"
huffman@44166
   648
    unfolding Basis_vec_def
huffman@44166
   649
    by (simp add: inner_axis euclidean_all_zero_iff vec_eq_iff)
hoelzl@50526
   650
qed
hoelzl@50526
   651
hoelzl@50526
   652
lemma DIM_cart[simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
hoelzl@50526
   653
  apply (simp add: Basis_vec_def)
hoelzl@50526
   654
  apply (subst card_UN_disjoint)
hoelzl@50526
   655
     apply simp
huffman@44166
   656
    apply simp
hoelzl@50526
   657
   apply (auto simp: axis_eq_axis) [1]
hoelzl@50526
   658
  apply (subst card_UN_disjoint)
hoelzl@50526
   659
     apply (auto simp: axis_eq_axis)
hoelzl@50526
   660
  done
huffman@44135
   661
huffman@36591
   662
end
huffman@44135
   663
lp15@62397
   664
lemma cart_eq_inner_axis: "a $ i = inner a (axis i 1)"
lp15@62397
   665
  by (simp add: inner_axis)
lp15@62397
   666
lp15@62397
   667
lemma axis_in_Basis: "a \<in> Basis \<Longrightarrow> axis i a \<in> Basis"
lp15@62397
   668
  by (auto simp add: Basis_vec_def axis_eq_axis)
lp15@62397
   669
huffman@44135
   670
end