src/HOL/Library/Product_Vector.thy
author hoelzl
Fri Jan 08 17:40:59 2016 +0100 (2016-01-08)
changeset 62101 26c0a70f78a3
parent 61973 0c7e865fa7cb
child 62102 877463945ce9
permissions -rw-r--r--
add uniform spaces
huffman@30019
     1
(*  Title:      HOL/Library/Product_Vector.thy
huffman@30019
     2
    Author:     Brian Huffman
huffman@30019
     3
*)
huffman@30019
     4
wenzelm@60500
     5
section \<open>Cartesian Products as Vector Spaces\<close>
huffman@30019
     6
huffman@30019
     7
theory Product_Vector
huffman@30019
     8
imports Inner_Product Product_plus
huffman@30019
     9
begin
huffman@30019
    10
wenzelm@60500
    11
subsection \<open>Product is a real vector space\<close>
huffman@30019
    12
haftmann@37678
    13
instantiation prod :: (real_vector, real_vector) real_vector
huffman@30019
    14
begin
huffman@30019
    15
huffman@30019
    16
definition scaleR_prod_def:
huffman@30019
    17
  "scaleR r A = (scaleR r (fst A), scaleR r (snd A))"
huffman@30019
    18
huffman@30019
    19
lemma fst_scaleR [simp]: "fst (scaleR r A) = scaleR r (fst A)"
huffman@30019
    20
  unfolding scaleR_prod_def by simp
huffman@30019
    21
huffman@30019
    22
lemma snd_scaleR [simp]: "snd (scaleR r A) = scaleR r (snd A)"
huffman@30019
    23
  unfolding scaleR_prod_def by simp
huffman@30019
    24
huffman@30019
    25
lemma scaleR_Pair [simp]: "scaleR r (a, b) = (scaleR r a, scaleR r b)"
huffman@30019
    26
  unfolding scaleR_prod_def by simp
huffman@30019
    27
wenzelm@60679
    28
instance
wenzelm@60679
    29
proof
huffman@30019
    30
  fix a b :: real and x y :: "'a \<times> 'b"
huffman@30019
    31
  show "scaleR a (x + y) = scaleR a x + scaleR a y"
huffman@44066
    32
    by (simp add: prod_eq_iff scaleR_right_distrib)
huffman@30019
    33
  show "scaleR (a + b) x = scaleR a x + scaleR b x"
huffman@44066
    34
    by (simp add: prod_eq_iff scaleR_left_distrib)
huffman@30019
    35
  show "scaleR a (scaleR b x) = scaleR (a * b) x"
huffman@44066
    36
    by (simp add: prod_eq_iff)
huffman@30019
    37
  show "scaleR 1 x = x"
huffman@44066
    38
    by (simp add: prod_eq_iff)
huffman@30019
    39
qed
huffman@30019
    40
huffman@30019
    41
end
huffman@30019
    42
wenzelm@60500
    43
subsection \<open>Product is a topological space\<close>
huffman@31415
    44
haftmann@37678
    45
instantiation prod :: (topological_space, topological_space) topological_space
huffman@31415
    46
begin
huffman@31415
    47
immler@54779
    48
definition open_prod_def[code del]:
huffman@31492
    49
  "open (S :: ('a \<times> 'b) set) \<longleftrightarrow>
huffman@31492
    50
    (\<forall>x\<in>S. \<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S)"
huffman@31415
    51
huffman@36332
    52
lemma open_prod_elim:
huffman@36332
    53
  assumes "open S" and "x \<in> S"
huffman@36332
    54
  obtains A B where "open A" and "open B" and "x \<in> A \<times> B" and "A \<times> B \<subseteq> S"
huffman@36332
    55
using assms unfolding open_prod_def by fast
huffman@36332
    56
huffman@36332
    57
lemma open_prod_intro:
huffman@36332
    58
  assumes "\<And>x. x \<in> S \<Longrightarrow> \<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S"
huffman@36332
    59
  shows "open S"
huffman@36332
    60
using assms unfolding open_prod_def by fast
huffman@36332
    61
wenzelm@60679
    62
instance
wenzelm@60679
    63
proof
huffman@31492
    64
  show "open (UNIV :: ('a \<times> 'b) set)"
huffman@31492
    65
    unfolding open_prod_def by auto
huffman@31415
    66
next
huffman@31415
    67
  fix S T :: "('a \<times> 'b) set"
huffman@36332
    68
  assume "open S" "open T"
huffman@36332
    69
  show "open (S \<inter> T)"
huffman@36332
    70
  proof (rule open_prod_intro)
huffman@36332
    71
    fix x assume x: "x \<in> S \<inter> T"
huffman@36332
    72
    from x have "x \<in> S" by simp
huffman@36332
    73
    obtain Sa Sb where A: "open Sa" "open Sb" "x \<in> Sa \<times> Sb" "Sa \<times> Sb \<subseteq> S"
wenzelm@60500
    74
      using \<open>open S\<close> and \<open>x \<in> S\<close> by (rule open_prod_elim)
huffman@36332
    75
    from x have "x \<in> T" by simp
huffman@36332
    76
    obtain Ta Tb where B: "open Ta" "open Tb" "x \<in> Ta \<times> Tb" "Ta \<times> Tb \<subseteq> T"
wenzelm@60500
    77
      using \<open>open T\<close> and \<open>x \<in> T\<close> by (rule open_prod_elim)
huffman@36332
    78
    let ?A = "Sa \<inter> Ta" and ?B = "Sb \<inter> Tb"
huffman@36332
    79
    have "open ?A \<and> open ?B \<and> x \<in> ?A \<times> ?B \<and> ?A \<times> ?B \<subseteq> S \<inter> T"
huffman@36332
    80
      using A B by (auto simp add: open_Int)
huffman@36332
    81
    thus "\<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S \<inter> T"
huffman@36332
    82
      by fast
huffman@36332
    83
  qed
huffman@31415
    84
next
huffman@31492
    85
  fix K :: "('a \<times> 'b) set set"
huffman@31492
    86
  assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
huffman@31492
    87
    unfolding open_prod_def by fast
huffman@31415
    88
qed
huffman@31415
    89
huffman@31415
    90
end
huffman@31415
    91
haftmann@54890
    92
declare [[code abort: "open::('a::topological_space*'b::topological_space) set \<Rightarrow> bool"]]
immler@54779
    93
huffman@31562
    94
lemma open_Times: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<times> T)"
huffman@31562
    95
unfolding open_prod_def by auto
huffman@31562
    96
huffman@31562
    97
lemma fst_vimage_eq_Times: "fst -` S = S \<times> UNIV"
huffman@31562
    98
by auto
huffman@31562
    99
huffman@31562
   100
lemma snd_vimage_eq_Times: "snd -` S = UNIV \<times> S"
huffman@31562
   101
by auto
huffman@31562
   102
huffman@31562
   103
lemma open_vimage_fst: "open S \<Longrightarrow> open (fst -` S)"
huffman@31562
   104
by (simp add: fst_vimage_eq_Times open_Times)
huffman@31562
   105
huffman@31562
   106
lemma open_vimage_snd: "open S \<Longrightarrow> open (snd -` S)"
huffman@31562
   107
by (simp add: snd_vimage_eq_Times open_Times)
huffman@31562
   108
huffman@31568
   109
lemma closed_vimage_fst: "closed S \<Longrightarrow> closed (fst -` S)"
huffman@31568
   110
unfolding closed_open vimage_Compl [symmetric]
huffman@31568
   111
by (rule open_vimage_fst)
huffman@31568
   112
huffman@31568
   113
lemma closed_vimage_snd: "closed S \<Longrightarrow> closed (snd -` S)"
huffman@31568
   114
unfolding closed_open vimage_Compl [symmetric]
huffman@31568
   115
by (rule open_vimage_snd)
huffman@31568
   116
huffman@31568
   117
lemma closed_Times: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<times> T)"
huffman@31568
   118
proof -
huffman@31568
   119
  have "S \<times> T = (fst -` S) \<inter> (snd -` T)" by auto
huffman@31568
   120
  thus "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<times> T)"
huffman@31568
   121
    by (simp add: closed_vimage_fst closed_vimage_snd closed_Int)
huffman@31568
   122
qed
huffman@31568
   123
huffman@34110
   124
lemma subset_fst_imageI: "A \<times> B \<subseteq> S \<Longrightarrow> y \<in> B \<Longrightarrow> A \<subseteq> fst ` S"
huffman@34110
   125
  unfolding image_def subset_eq by force
huffman@34110
   126
huffman@34110
   127
lemma subset_snd_imageI: "A \<times> B \<subseteq> S \<Longrightarrow> x \<in> A \<Longrightarrow> B \<subseteq> snd ` S"
huffman@34110
   128
  unfolding image_def subset_eq by force
huffman@34110
   129
huffman@34110
   130
lemma open_image_fst: assumes "open S" shows "open (fst ` S)"
huffman@34110
   131
proof (rule openI)
huffman@34110
   132
  fix x assume "x \<in> fst ` S"
huffman@34110
   133
  then obtain y where "(x, y) \<in> S" by auto
huffman@34110
   134
  then obtain A B where "open A" "open B" "x \<in> A" "y \<in> B" "A \<times> B \<subseteq> S"
wenzelm@60500
   135
    using \<open>open S\<close> unfolding open_prod_def by auto
wenzelm@60500
   136
  from \<open>A \<times> B \<subseteq> S\<close> \<open>y \<in> B\<close> have "A \<subseteq> fst ` S" by (rule subset_fst_imageI)
wenzelm@60500
   137
  with \<open>open A\<close> \<open>x \<in> A\<close> have "open A \<and> x \<in> A \<and> A \<subseteq> fst ` S" by simp
huffman@34110
   138
  then show "\<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> fst ` S" by - (rule exI)
huffman@34110
   139
qed
huffman@34110
   140
huffman@34110
   141
lemma open_image_snd: assumes "open S" shows "open (snd ` S)"
huffman@34110
   142
proof (rule openI)
huffman@34110
   143
  fix y assume "y \<in> snd ` S"
huffman@34110
   144
  then obtain x where "(x, y) \<in> S" by auto
huffman@34110
   145
  then obtain A B where "open A" "open B" "x \<in> A" "y \<in> B" "A \<times> B \<subseteq> S"
wenzelm@60500
   146
    using \<open>open S\<close> unfolding open_prod_def by auto
wenzelm@60500
   147
  from \<open>A \<times> B \<subseteq> S\<close> \<open>x \<in> A\<close> have "B \<subseteq> snd ` S" by (rule subset_snd_imageI)
wenzelm@60500
   148
  with \<open>open B\<close> \<open>y \<in> B\<close> have "open B \<and> y \<in> B \<and> B \<subseteq> snd ` S" by simp
huffman@34110
   149
  then show "\<exists>T. open T \<and> y \<in> T \<and> T \<subseteq> snd ` S" by - (rule exI)
huffman@34110
   150
qed
huffman@31568
   151
wenzelm@60500
   152
subsubsection \<open>Continuity of operations\<close>
huffman@44575
   153
huffman@44575
   154
lemma tendsto_fst [tendsto_intros]:
wenzelm@61973
   155
  assumes "(f \<longlongrightarrow> a) F"
wenzelm@61973
   156
  shows "((\<lambda>x. fst (f x)) \<longlongrightarrow> fst a) F"
huffman@44575
   157
proof (rule topological_tendstoI)
huffman@44575
   158
  fix S assume "open S" and "fst a \<in> S"
huffman@44575
   159
  then have "open (fst -` S)" and "a \<in> fst -` S"
huffman@44575
   160
    by (simp_all add: open_vimage_fst)
huffman@44575
   161
  with assms have "eventually (\<lambda>x. f x \<in> fst -` S) F"
huffman@44575
   162
    by (rule topological_tendstoD)
huffman@44575
   163
  then show "eventually (\<lambda>x. fst (f x) \<in> S) F"
huffman@44575
   164
    by simp
huffman@44575
   165
qed
huffman@44575
   166
huffman@44575
   167
lemma tendsto_snd [tendsto_intros]:
wenzelm@61973
   168
  assumes "(f \<longlongrightarrow> a) F"
wenzelm@61973
   169
  shows "((\<lambda>x. snd (f x)) \<longlongrightarrow> snd a) F"
huffman@44575
   170
proof (rule topological_tendstoI)
huffman@44575
   171
  fix S assume "open S" and "snd a \<in> S"
huffman@44575
   172
  then have "open (snd -` S)" and "a \<in> snd -` S"
huffman@44575
   173
    by (simp_all add: open_vimage_snd)
huffman@44575
   174
  with assms have "eventually (\<lambda>x. f x \<in> snd -` S) F"
huffman@44575
   175
    by (rule topological_tendstoD)
huffman@44575
   176
  then show "eventually (\<lambda>x. snd (f x) \<in> S) F"
huffman@44575
   177
    by simp
huffman@44575
   178
qed
huffman@44575
   179
huffman@44575
   180
lemma tendsto_Pair [tendsto_intros]:
wenzelm@61973
   181
  assumes "(f \<longlongrightarrow> a) F" and "(g \<longlongrightarrow> b) F"
wenzelm@61973
   182
  shows "((\<lambda>x. (f x, g x)) \<longlongrightarrow> (a, b)) F"
huffman@44575
   183
proof (rule topological_tendstoI)
huffman@44575
   184
  fix S assume "open S" and "(a, b) \<in> S"
huffman@44575
   185
  then obtain A B where "open A" "open B" "a \<in> A" "b \<in> B" "A \<times> B \<subseteq> S"
huffman@44575
   186
    unfolding open_prod_def by fast
huffman@44575
   187
  have "eventually (\<lambda>x. f x \<in> A) F"
wenzelm@61973
   188
    using \<open>(f \<longlongrightarrow> a) F\<close> \<open>open A\<close> \<open>a \<in> A\<close>
huffman@44575
   189
    by (rule topological_tendstoD)
huffman@44575
   190
  moreover
huffman@44575
   191
  have "eventually (\<lambda>x. g x \<in> B) F"
wenzelm@61973
   192
    using \<open>(g \<longlongrightarrow> b) F\<close> \<open>open B\<close> \<open>b \<in> B\<close>
huffman@44575
   193
    by (rule topological_tendstoD)
huffman@44575
   194
  ultimately
huffman@44575
   195
  show "eventually (\<lambda>x. (f x, g x) \<in> S) F"
huffman@44575
   196
    by (rule eventually_elim2)
wenzelm@60500
   197
       (simp add: subsetD [OF \<open>A \<times> B \<subseteq> S\<close>])
huffman@44575
   198
qed
huffman@44575
   199
hoelzl@51478
   200
lemma continuous_fst[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. fst (f x))"
hoelzl@51478
   201
  unfolding continuous_def by (rule tendsto_fst)
hoelzl@51478
   202
hoelzl@51478
   203
lemma continuous_snd[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. snd (f x))"
hoelzl@51478
   204
  unfolding continuous_def by (rule tendsto_snd)
hoelzl@51478
   205
hoelzl@51478
   206
lemma continuous_Pair[continuous_intros]: "continuous F f \<Longrightarrow> continuous F g \<Longrightarrow> continuous F (\<lambda>x. (f x, g x))"
hoelzl@51478
   207
  unfolding continuous_def by (rule tendsto_Pair)
hoelzl@51478
   208
hoelzl@56371
   209
lemma continuous_on_fst[continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. fst (f x))"
hoelzl@51644
   210
  unfolding continuous_on_def by (auto intro: tendsto_fst)
hoelzl@51644
   211
hoelzl@56371
   212
lemma continuous_on_snd[continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. snd (f x))"
hoelzl@51644
   213
  unfolding continuous_on_def by (auto intro: tendsto_snd)
hoelzl@51644
   214
hoelzl@56371
   215
lemma continuous_on_Pair[continuous_intros]: "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. (f x, g x))"
hoelzl@51644
   216
  unfolding continuous_on_def by (auto intro: tendsto_Pair)
hoelzl@51644
   217
lp15@60615
   218
lemma continuous_on_swap[continuous_intros]: "continuous_on A prod.swap"
lp15@60615
   219
  by (simp add: prod.swap_def continuous_on_fst continuous_on_snd continuous_on_Pair continuous_on_id)
lp15@60615
   220
huffman@44575
   221
lemma isCont_fst [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. fst (f x)) a"
hoelzl@51478
   222
  by (fact continuous_fst)
huffman@44575
   223
huffman@44575
   224
lemma isCont_snd [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. snd (f x)) a"
hoelzl@51478
   225
  by (fact continuous_snd)
huffman@44575
   226
hoelzl@51478
   227
lemma isCont_Pair [simp]: "\<lbrakk>isCont f a; isCont g a\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. (f x, g x)) a"
hoelzl@51478
   228
  by (fact continuous_Pair)
huffman@44575
   229
wenzelm@60500
   230
subsubsection \<open>Separation axioms\<close>
huffman@44214
   231
huffman@44214
   232
instance prod :: (t0_space, t0_space) t0_space
huffman@44214
   233
proof
huffman@44214
   234
  fix x y :: "'a \<times> 'b" assume "x \<noteq> y"
huffman@44214
   235
  hence "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
huffman@44214
   236
    by (simp add: prod_eq_iff)
huffman@44214
   237
  thus "\<exists>U. open U \<and> (x \<in> U) \<noteq> (y \<in> U)"
huffman@53930
   238
    by (fast dest: t0_space elim: open_vimage_fst open_vimage_snd)
huffman@44214
   239
qed
huffman@44214
   240
huffman@44214
   241
instance prod :: (t1_space, t1_space) t1_space
huffman@44214
   242
proof
huffman@44214
   243
  fix x y :: "'a \<times> 'b" assume "x \<noteq> y"
huffman@44214
   244
  hence "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
huffman@44214
   245
    by (simp add: prod_eq_iff)
huffman@44214
   246
  thus "\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
huffman@53930
   247
    by (fast dest: t1_space elim: open_vimage_fst open_vimage_snd)
huffman@44214
   248
qed
huffman@44214
   249
huffman@44214
   250
instance prod :: (t2_space, t2_space) t2_space
huffman@44214
   251
proof
huffman@44214
   252
  fix x y :: "'a \<times> 'b" assume "x \<noteq> y"
huffman@44214
   253
  hence "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
huffman@44214
   254
    by (simp add: prod_eq_iff)
huffman@44214
   255
  thus "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
huffman@53930
   256
    by (fast dest: hausdorff elim: open_vimage_fst open_vimage_snd)
huffman@44214
   257
qed
huffman@44214
   258
lp15@60615
   259
lemma isCont_swap[continuous_intros]: "isCont prod.swap a"
lp15@60615
   260
  using continuous_on_eq_continuous_within continuous_on_swap by blast
lp15@60615
   261
wenzelm@60500
   262
subsection \<open>Product is a metric space\<close>
huffman@31339
   263
hoelzl@62101
   264
(* TODO: Product of uniform spaces and compatibility with metric_spaces! *)
hoelzl@62101
   265
hoelzl@62101
   266
instantiation prod :: (metric_space, metric_space) dist
huffman@31339
   267
begin
huffman@31339
   268
immler@54779
   269
definition dist_prod_def[code del]:
wenzelm@53015
   270
  "dist x y = sqrt ((dist (fst x) (fst y))\<^sup>2 + (dist (snd x) (snd y))\<^sup>2)"
huffman@31339
   271
hoelzl@62101
   272
instance ..
hoelzl@62101
   273
end
hoelzl@62101
   274
hoelzl@62101
   275
instantiation prod :: (metric_space, metric_space) uniformity_dist
hoelzl@62101
   276
begin
hoelzl@62101
   277
hoelzl@62101
   278
definition [code del]:
hoelzl@62101
   279
  "(uniformity :: (('a \<times> 'b) \<times> ('a \<times> 'b)) filter) = 
hoelzl@62101
   280
    (INF e:{0 <..}. principal {(x, y). dist x y < e})"
hoelzl@62101
   281
hoelzl@62101
   282
instance 
hoelzl@62101
   283
  by standard (rule uniformity_prod_def)
hoelzl@62101
   284
end
hoelzl@62101
   285
hoelzl@62101
   286
instantiation prod :: (metric_space, metric_space) metric_space
hoelzl@62101
   287
begin
hoelzl@62101
   288
wenzelm@53015
   289
lemma dist_Pair_Pair: "dist (a, b) (c, d) = sqrt ((dist a c)\<^sup>2 + (dist b d)\<^sup>2)"
huffman@31339
   290
  unfolding dist_prod_def by simp
huffman@31339
   291
huffman@36332
   292
lemma dist_fst_le: "dist (fst x) (fst y) \<le> dist x y"
huffman@53930
   293
  unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge1)
huffman@36332
   294
huffman@36332
   295
lemma dist_snd_le: "dist (snd x) (snd y) \<le> dist x y"
huffman@53930
   296
  unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge2)
huffman@36332
   297
wenzelm@60679
   298
instance
wenzelm@60679
   299
proof
huffman@31339
   300
  fix x y :: "'a \<times> 'b"
huffman@31339
   301
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@44066
   302
    unfolding dist_prod_def prod_eq_iff by simp
huffman@31339
   303
next
huffman@31339
   304
  fix x y z :: "'a \<times> 'b"
huffman@31339
   305
  show "dist x y \<le> dist x z + dist y z"
huffman@31339
   306
    unfolding dist_prod_def
huffman@31563
   307
    by (intro order_trans [OF _ real_sqrt_sum_squares_triangle_ineq]
huffman@31563
   308
        real_sqrt_le_mono add_mono power_mono dist_triangle2 zero_le_dist)
huffman@31415
   309
next
huffman@31492
   310
  fix S :: "('a \<times> 'b) set"
hoelzl@62101
   311
  have *: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@31563
   312
  proof
huffman@36332
   313
    assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@36332
   314
    proof
huffman@36332
   315
      fix x assume "x \<in> S"
huffman@36332
   316
      obtain A B where "open A" "open B" "x \<in> A \<times> B" "A \<times> B \<subseteq> S"
wenzelm@60500
   317
        using \<open>open S\<close> and \<open>x \<in> S\<close> by (rule open_prod_elim)
huffman@36332
   318
      obtain r where r: "0 < r" "\<forall>y. dist y (fst x) < r \<longrightarrow> y \<in> A"
wenzelm@60500
   319
        using \<open>open A\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
huffman@36332
   320
      obtain s where s: "0 < s" "\<forall>y. dist y (snd x) < s \<longrightarrow> y \<in> B"
wenzelm@60500
   321
        using \<open>open B\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
huffman@36332
   322
      let ?e = "min r s"
huffman@36332
   323
      have "0 < ?e \<and> (\<forall>y. dist y x < ?e \<longrightarrow> y \<in> S)"
huffman@36332
   324
      proof (intro allI impI conjI)
huffman@36332
   325
        show "0 < min r s" by (simp add: r(1) s(1))
huffman@36332
   326
      next
huffman@36332
   327
        fix y assume "dist y x < min r s"
huffman@36332
   328
        hence "dist y x < r" and "dist y x < s"
huffman@36332
   329
          by simp_all
huffman@36332
   330
        hence "dist (fst y) (fst x) < r" and "dist (snd y) (snd x) < s"
huffman@36332
   331
          by (auto intro: le_less_trans dist_fst_le dist_snd_le)
huffman@36332
   332
        hence "fst y \<in> A" and "snd y \<in> B"
huffman@36332
   333
          by (simp_all add: r(2) s(2))
huffman@36332
   334
        hence "y \<in> A \<times> B" by (induct y, simp)
wenzelm@60500
   335
        with \<open>A \<times> B \<subseteq> S\<close> show "y \<in> S" ..
huffman@36332
   336
      qed
huffman@36332
   337
      thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
huffman@36332
   338
    qed
huffman@31563
   339
  next
huffman@44575
   340
    assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
huffman@44575
   341
    proof (rule open_prod_intro)
huffman@44575
   342
      fix x assume "x \<in> S"
huffman@44575
   343
      then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44575
   344
        using * by fast
huffman@44575
   345
      def r \<equiv> "e / sqrt 2" and s \<equiv> "e / sqrt 2"
wenzelm@60500
   346
      from \<open>0 < e\<close> have "0 < r" and "0 < s"
nipkow@56541
   347
        unfolding r_def s_def by simp_all
wenzelm@60500
   348
      from \<open>0 < e\<close> have "e = sqrt (r\<^sup>2 + s\<^sup>2)"
huffman@44575
   349
        unfolding r_def s_def by (simp add: power_divide)
huffman@44575
   350
      def A \<equiv> "{y. dist (fst x) y < r}" and B \<equiv> "{y. dist (snd x) y < s}"
huffman@44575
   351
      have "open A" and "open B"
huffman@44575
   352
        unfolding A_def B_def by (simp_all add: open_ball)
huffman@44575
   353
      moreover have "x \<in> A \<times> B"
huffman@44575
   354
        unfolding A_def B_def mem_Times_iff
wenzelm@60500
   355
        using \<open>0 < r\<close> and \<open>0 < s\<close> by simp
huffman@44575
   356
      moreover have "A \<times> B \<subseteq> S"
huffman@44575
   357
      proof (clarify)
huffman@44575
   358
        fix a b assume "a \<in> A" and "b \<in> B"
huffman@44575
   359
        hence "dist a (fst x) < r" and "dist b (snd x) < s"
huffman@44575
   360
          unfolding A_def B_def by (simp_all add: dist_commute)
huffman@44575
   361
        hence "dist (a, b) x < e"
wenzelm@60500
   362
          unfolding dist_prod_def \<open>e = sqrt (r\<^sup>2 + s\<^sup>2)\<close>
huffman@44575
   363
          by (simp add: add_strict_mono power_strict_mono)
huffman@44575
   364
        thus "(a, b) \<in> S"
huffman@44575
   365
          by (simp add: S)
huffman@44575
   366
      qed
huffman@44575
   367
      ultimately show "\<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S" by fast
huffman@44575
   368
    qed
huffman@31563
   369
  qed
hoelzl@62101
   370
  show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
hoelzl@62101
   371
    unfolding * eventually_uniformity_metric
hoelzl@62101
   372
    by (simp del: split_paired_All add: dist_prod_def dist_commute)
huffman@31339
   373
qed
huffman@31339
   374
huffman@31339
   375
end
huffman@31339
   376
haftmann@54890
   377
declare [[code abort: "dist::('a::metric_space*'b::metric_space)\<Rightarrow>('a*'b) \<Rightarrow> real"]]
immler@54779
   378
huffman@31405
   379
lemma Cauchy_fst: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. fst (X n))"
huffman@53930
   380
  unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_fst_le])
huffman@31405
   381
huffman@31405
   382
lemma Cauchy_snd: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. snd (X n))"
huffman@53930
   383
  unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_snd_le])
huffman@31405
   384
huffman@31405
   385
lemma Cauchy_Pair:
huffman@31405
   386
  assumes "Cauchy X" and "Cauchy Y"
huffman@31405
   387
  shows "Cauchy (\<lambda>n. (X n, Y n))"
huffman@31405
   388
proof (rule metric_CauchyI)
huffman@31405
   389
  fix r :: real assume "0 < r"
nipkow@56541
   390
  hence "0 < r / sqrt 2" (is "0 < ?s") by simp
huffman@31405
   391
  obtain M where M: "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < ?s"
wenzelm@60500
   392
    using metric_CauchyD [OF \<open>Cauchy X\<close> \<open>0 < ?s\<close>] ..
huffman@31405
   393
  obtain N where N: "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (Y m) (Y n) < ?s"
wenzelm@60500
   394
    using metric_CauchyD [OF \<open>Cauchy Y\<close> \<open>0 < ?s\<close>] ..
huffman@31405
   395
  have "\<forall>m\<ge>max M N. \<forall>n\<ge>max M N. dist (X m, Y m) (X n, Y n) < r"
huffman@31405
   396
    using M N by (simp add: real_sqrt_sum_squares_less dist_Pair_Pair)
huffman@31405
   397
  then show "\<exists>n0. \<forall>m\<ge>n0. \<forall>n\<ge>n0. dist (X m, Y m) (X n, Y n) < r" ..
huffman@31405
   398
qed
huffman@31405
   399
wenzelm@60500
   400
subsection \<open>Product is a complete metric space\<close>
huffman@31405
   401
haftmann@37678
   402
instance prod :: (complete_space, complete_space) complete_space
huffman@31405
   403
proof
huffman@31405
   404
  fix X :: "nat \<Rightarrow> 'a \<times> 'b" assume "Cauchy X"
wenzelm@61969
   405
  have 1: "(\<lambda>n. fst (X n)) \<longlonglongrightarrow> lim (\<lambda>n. fst (X n))"
wenzelm@60500
   406
    using Cauchy_fst [OF \<open>Cauchy X\<close>]
huffman@31405
   407
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
wenzelm@61969
   408
  have 2: "(\<lambda>n. snd (X n)) \<longlonglongrightarrow> lim (\<lambda>n. snd (X n))"
wenzelm@60500
   409
    using Cauchy_snd [OF \<open>Cauchy X\<close>]
huffman@31405
   410
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
wenzelm@61969
   411
  have "X \<longlonglongrightarrow> (lim (\<lambda>n. fst (X n)), lim (\<lambda>n. snd (X n)))"
huffman@36660
   412
    using tendsto_Pair [OF 1 2] by simp
huffman@31405
   413
  then show "convergent X"
huffman@31405
   414
    by (rule convergentI)
huffman@31405
   415
qed
huffman@31405
   416
wenzelm@60500
   417
subsection \<open>Product is a normed vector space\<close>
huffman@30019
   418
haftmann@37678
   419
instantiation prod :: (real_normed_vector, real_normed_vector) real_normed_vector
huffman@30019
   420
begin
huffman@30019
   421
immler@54779
   422
definition norm_prod_def[code del]:
wenzelm@53015
   423
  "norm x = sqrt ((norm (fst x))\<^sup>2 + (norm (snd x))\<^sup>2)"
huffman@30019
   424
huffman@30019
   425
definition sgn_prod_def:
huffman@30019
   426
  "sgn (x::'a \<times> 'b) = scaleR (inverse (norm x)) x"
huffman@30019
   427
wenzelm@53015
   428
lemma norm_Pair: "norm (a, b) = sqrt ((norm a)\<^sup>2 + (norm b)\<^sup>2)"
huffman@30019
   429
  unfolding norm_prod_def by simp
huffman@30019
   430
wenzelm@60679
   431
instance
wenzelm@60679
   432
proof
huffman@30019
   433
  fix r :: real and x y :: "'a \<times> 'b"
huffman@30019
   434
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@30019
   435
    unfolding norm_prod_def
huffman@44066
   436
    by (simp add: prod_eq_iff)
huffman@30019
   437
  show "norm (x + y) \<le> norm x + norm y"
huffman@30019
   438
    unfolding norm_prod_def
huffman@30019
   439
    apply (rule order_trans [OF _ real_sqrt_sum_squares_triangle_ineq])
huffman@30019
   440
    apply (simp add: add_mono power_mono norm_triangle_ineq)
huffman@30019
   441
    done
huffman@30019
   442
  show "norm (scaleR r x) = \<bar>r\<bar> * norm x"
huffman@30019
   443
    unfolding norm_prod_def
huffman@31587
   444
    apply (simp add: power_mult_distrib)
webertj@49962
   445
    apply (simp add: distrib_left [symmetric])
huffman@30019
   446
    apply (simp add: real_sqrt_mult_distrib)
huffman@30019
   447
    done
huffman@30019
   448
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@30019
   449
    by (rule sgn_prod_def)
huffman@31290
   450
  show "dist x y = norm (x - y)"
huffman@31339
   451
    unfolding dist_prod_def norm_prod_def
huffman@31339
   452
    by (simp add: dist_norm)
huffman@30019
   453
qed
huffman@30019
   454
huffman@30019
   455
end
huffman@30019
   456
haftmann@54890
   457
declare [[code abort: "norm::('a::real_normed_vector*'b::real_normed_vector) \<Rightarrow> real"]]
immler@54779
   458
haftmann@37678
   459
instance prod :: (banach, banach) banach ..
huffman@31405
   460
wenzelm@60500
   461
subsubsection \<open>Pair operations are linear\<close>
huffman@30019
   462
huffman@44282
   463
lemma bounded_linear_fst: "bounded_linear fst"
huffman@44127
   464
  using fst_add fst_scaleR
huffman@44127
   465
  by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
huffman@30019
   466
huffman@44282
   467
lemma bounded_linear_snd: "bounded_linear snd"
huffman@44127
   468
  using snd_add snd_scaleR
huffman@44127
   469
  by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
huffman@30019
   470
immler@61915
   471
lemmas bounded_linear_fst_comp = bounded_linear_fst[THEN bounded_linear_compose]
immler@61915
   472
immler@61915
   473
lemmas bounded_linear_snd_comp = bounded_linear_snd[THEN bounded_linear_compose]
immler@61915
   474
huffman@30019
   475
lemma bounded_linear_Pair:
huffman@30019
   476
  assumes f: "bounded_linear f"
huffman@30019
   477
  assumes g: "bounded_linear g"
huffman@30019
   478
  shows "bounded_linear (\<lambda>x. (f x, g x))"
huffman@30019
   479
proof
huffman@30019
   480
  interpret f: bounded_linear f by fact
huffman@30019
   481
  interpret g: bounded_linear g by fact
huffman@30019
   482
  fix x y and r :: real
huffman@30019
   483
  show "(f (x + y), g (x + y)) = (f x, g x) + (f y, g y)"
huffman@30019
   484
    by (simp add: f.add g.add)
huffman@30019
   485
  show "(f (r *\<^sub>R x), g (r *\<^sub>R x)) = r *\<^sub>R (f x, g x)"
huffman@30019
   486
    by (simp add: f.scaleR g.scaleR)
huffman@30019
   487
  obtain Kf where "0 < Kf" and norm_f: "\<And>x. norm (f x) \<le> norm x * Kf"
huffman@30019
   488
    using f.pos_bounded by fast
huffman@30019
   489
  obtain Kg where "0 < Kg" and norm_g: "\<And>x. norm (g x) \<le> norm x * Kg"
huffman@30019
   490
    using g.pos_bounded by fast
huffman@30019
   491
  have "\<forall>x. norm (f x, g x) \<le> norm x * (Kf + Kg)"
huffman@30019
   492
    apply (rule allI)
huffman@30019
   493
    apply (simp add: norm_Pair)
huffman@30019
   494
    apply (rule order_trans [OF sqrt_add_le_add_sqrt], simp, simp)
webertj@49962
   495
    apply (simp add: distrib_left)
huffman@30019
   496
    apply (rule add_mono [OF norm_f norm_g])
huffman@30019
   497
    done
huffman@30019
   498
  then show "\<exists>K. \<forall>x. norm (f x, g x) \<le> norm x * K" ..
huffman@30019
   499
qed
huffman@30019
   500
wenzelm@60500
   501
subsubsection \<open>Frechet derivatives involving pairs\<close>
huffman@30019
   502
hoelzl@56381
   503
lemma has_derivative_Pair [derivative_intros]:
hoelzl@56181
   504
  assumes f: "(f has_derivative f') (at x within s)" and g: "(g has_derivative g') (at x within s)"
hoelzl@56181
   505
  shows "((\<lambda>x. (f x, g x)) has_derivative (\<lambda>h. (f' h, g' h))) (at x within s)"
hoelzl@56181
   506
proof (rule has_derivativeI_sandwich[of 1])
huffman@44575
   507
  show "bounded_linear (\<lambda>h. (f' h, g' h))"
hoelzl@56181
   508
    using f g by (intro bounded_linear_Pair has_derivative_bounded_linear)
hoelzl@51642
   509
  let ?Rf = "\<lambda>y. f y - f x - f' (y - x)"
hoelzl@51642
   510
  let ?Rg = "\<lambda>y. g y - g x - g' (y - x)"
hoelzl@51642
   511
  let ?R = "\<lambda>y. ((f y, g y) - (f x, g x) - (f' (y - x), g' (y - x)))"
hoelzl@51642
   512
wenzelm@61973
   513
  show "((\<lambda>y. norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)) \<longlongrightarrow> 0) (at x within s)"
hoelzl@56181
   514
    using f g by (intro tendsto_add_zero) (auto simp: has_derivative_iff_norm)
hoelzl@51642
   515
hoelzl@51642
   516
  fix y :: 'a assume "y \<noteq> x"
hoelzl@51642
   517
  show "norm (?R y) / norm (y - x) \<le> norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)"
hoelzl@51642
   518
    unfolding add_divide_distrib [symmetric]
hoelzl@51642
   519
    by (simp add: norm_Pair divide_right_mono order_trans [OF sqrt_add_le_add_sqrt])
hoelzl@51642
   520
qed simp
hoelzl@51642
   521
hoelzl@56381
   522
lemmas has_derivative_fst [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_fst]
hoelzl@56381
   523
lemmas has_derivative_snd [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_snd]
hoelzl@51642
   524
hoelzl@56381
   525
lemma has_derivative_split [derivative_intros]:
hoelzl@51642
   526
  "((\<lambda>p. f (fst p) (snd p)) has_derivative f') F \<Longrightarrow> ((\<lambda>(a, b). f a b) has_derivative f') F"
hoelzl@51642
   527
  unfolding split_beta' .
huffman@44575
   528
wenzelm@60500
   529
subsection \<open>Product is an inner product space\<close>
huffman@44575
   530
huffman@44575
   531
instantiation prod :: (real_inner, real_inner) real_inner
huffman@44575
   532
begin
huffman@44575
   533
huffman@44575
   534
definition inner_prod_def:
huffman@44575
   535
  "inner x y = inner (fst x) (fst y) + inner (snd x) (snd y)"
huffman@44575
   536
huffman@44575
   537
lemma inner_Pair [simp]: "inner (a, b) (c, d) = inner a c + inner b d"
huffman@44575
   538
  unfolding inner_prod_def by simp
huffman@44575
   539
wenzelm@60679
   540
instance
wenzelm@60679
   541
proof
huffman@44575
   542
  fix r :: real
huffman@44575
   543
  fix x y z :: "'a::real_inner \<times> 'b::real_inner"
huffman@44575
   544
  show "inner x y = inner y x"
huffman@44575
   545
    unfolding inner_prod_def
huffman@44575
   546
    by (simp add: inner_commute)
huffman@44575
   547
  show "inner (x + y) z = inner x z + inner y z"
huffman@44575
   548
    unfolding inner_prod_def
huffman@44575
   549
    by (simp add: inner_add_left)
huffman@44575
   550
  show "inner (scaleR r x) y = r * inner x y"
huffman@44575
   551
    unfolding inner_prod_def
webertj@49962
   552
    by (simp add: distrib_left)
huffman@44575
   553
  show "0 \<le> inner x x"
huffman@44575
   554
    unfolding inner_prod_def
huffman@44575
   555
    by (intro add_nonneg_nonneg inner_ge_zero)
huffman@44575
   556
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@44575
   557
    unfolding inner_prod_def prod_eq_iff
huffman@44575
   558
    by (simp add: add_nonneg_eq_0_iff)
huffman@44575
   559
  show "norm x = sqrt (inner x x)"
huffman@44575
   560
    unfolding norm_prod_def inner_prod_def
huffman@44575
   561
    by (simp add: power2_norm_eq_inner)
huffman@44575
   562
qed
huffman@30019
   563
huffman@30019
   564
end
huffman@44575
   565
hoelzl@59425
   566
lemma inner_Pair_0: "inner x (0, b) = inner (snd x) b" "inner x (a, 0) = inner (fst x) a"
hoelzl@59425
   567
    by (cases x, simp)+
hoelzl@59425
   568
lp15@60615
   569
lemma 
lp15@60615
   570
  fixes x :: "'a::real_normed_vector"
lp15@60615
   571
  shows norm_Pair1 [simp]: "norm (0,x) = norm x" 
lp15@60615
   572
    and norm_Pair2 [simp]: "norm (x,0) = norm x"
lp15@60615
   573
by (auto simp: norm_Pair)
lp15@60615
   574
hoelzl@59425
   575
huffman@44575
   576
end