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