src/HOL/Library/Product_Vector.thy
author paulson
Mon Jan 11 22:14:15 2016 +0000 (2016-01-11)
changeset 62131 1baed43f453e
parent 62102 877463945ce9
child 62217 527488dc8b90
permissions -rw-r--r--
nonneg_Reals, nonpos_Reals, Cauchy integral formula, etc.
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@62102
   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@62102
   282
instance
hoelzl@62101
   283
  by standard (rule uniformity_prod_def)
hoelzl@62101
   284
end
hoelzl@62101
   285
hoelzl@62102
   286
declare uniformity_Abort[where 'a="'a :: metric_space \<times> 'b :: metric_space", code]
hoelzl@62102
   287
hoelzl@62101
   288
instantiation prod :: (metric_space, metric_space) metric_space
hoelzl@62101
   289
begin
hoelzl@62101
   290
wenzelm@53015
   291
lemma dist_Pair_Pair: "dist (a, b) (c, d) = sqrt ((dist a c)\<^sup>2 + (dist b d)\<^sup>2)"
huffman@31339
   292
  unfolding dist_prod_def by simp
huffman@31339
   293
huffman@36332
   294
lemma dist_fst_le: "dist (fst x) (fst y) \<le> dist x y"
huffman@53930
   295
  unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge1)
huffman@36332
   296
huffman@36332
   297
lemma dist_snd_le: "dist (snd x) (snd y) \<le> dist x y"
huffman@53930
   298
  unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge2)
huffman@36332
   299
wenzelm@60679
   300
instance
wenzelm@60679
   301
proof
huffman@31339
   302
  fix x y :: "'a \<times> 'b"
huffman@31339
   303
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@44066
   304
    unfolding dist_prod_def prod_eq_iff by simp
huffman@31339
   305
next
huffman@31339
   306
  fix x y z :: "'a \<times> 'b"
huffman@31339
   307
  show "dist x y \<le> dist x z + dist y z"
huffman@31339
   308
    unfolding dist_prod_def
huffman@31563
   309
    by (intro order_trans [OF _ real_sqrt_sum_squares_triangle_ineq]
huffman@31563
   310
        real_sqrt_le_mono add_mono power_mono dist_triangle2 zero_le_dist)
huffman@31415
   311
next
huffman@31492
   312
  fix S :: "('a \<times> 'b) set"
hoelzl@62101
   313
  have *: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@31563
   314
  proof
huffman@36332
   315
    assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@36332
   316
    proof
huffman@36332
   317
      fix x assume "x \<in> S"
huffman@36332
   318
      obtain A B where "open A" "open B" "x \<in> A \<times> B" "A \<times> B \<subseteq> S"
wenzelm@60500
   319
        using \<open>open S\<close> and \<open>x \<in> S\<close> by (rule open_prod_elim)
huffman@36332
   320
      obtain r where r: "0 < r" "\<forall>y. dist y (fst x) < r \<longrightarrow> y \<in> A"
wenzelm@60500
   321
        using \<open>open A\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
huffman@36332
   322
      obtain s where s: "0 < s" "\<forall>y. dist y (snd x) < s \<longrightarrow> y \<in> B"
wenzelm@60500
   323
        using \<open>open B\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
huffman@36332
   324
      let ?e = "min r s"
huffman@36332
   325
      have "0 < ?e \<and> (\<forall>y. dist y x < ?e \<longrightarrow> y \<in> S)"
huffman@36332
   326
      proof (intro allI impI conjI)
huffman@36332
   327
        show "0 < min r s" by (simp add: r(1) s(1))
huffman@36332
   328
      next
huffman@36332
   329
        fix y assume "dist y x < min r s"
huffman@36332
   330
        hence "dist y x < r" and "dist y x < s"
huffman@36332
   331
          by simp_all
huffman@36332
   332
        hence "dist (fst y) (fst x) < r" and "dist (snd y) (snd x) < s"
huffman@36332
   333
          by (auto intro: le_less_trans dist_fst_le dist_snd_le)
huffman@36332
   334
        hence "fst y \<in> A" and "snd y \<in> B"
huffman@36332
   335
          by (simp_all add: r(2) s(2))
huffman@36332
   336
        hence "y \<in> A \<times> B" by (induct y, simp)
wenzelm@60500
   337
        with \<open>A \<times> B \<subseteq> S\<close> show "y \<in> S" ..
huffman@36332
   338
      qed
huffman@36332
   339
      thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
huffman@36332
   340
    qed
huffman@31563
   341
  next
huffman@44575
   342
    assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
huffman@44575
   343
    proof (rule open_prod_intro)
huffman@44575
   344
      fix x assume "x \<in> S"
huffman@44575
   345
      then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44575
   346
        using * by fast
huffman@44575
   347
      def r \<equiv> "e / sqrt 2" and s \<equiv> "e / sqrt 2"
wenzelm@60500
   348
      from \<open>0 < e\<close> have "0 < r" and "0 < s"
nipkow@56541
   349
        unfolding r_def s_def by simp_all
wenzelm@60500
   350
      from \<open>0 < e\<close> have "e = sqrt (r\<^sup>2 + s\<^sup>2)"
huffman@44575
   351
        unfolding r_def s_def by (simp add: power_divide)
huffman@44575
   352
      def A \<equiv> "{y. dist (fst x) y < r}" and B \<equiv> "{y. dist (snd x) y < s}"
huffman@44575
   353
      have "open A" and "open B"
huffman@44575
   354
        unfolding A_def B_def by (simp_all add: open_ball)
huffman@44575
   355
      moreover have "x \<in> A \<times> B"
huffman@44575
   356
        unfolding A_def B_def mem_Times_iff
wenzelm@60500
   357
        using \<open>0 < r\<close> and \<open>0 < s\<close> by simp
huffman@44575
   358
      moreover have "A \<times> B \<subseteq> S"
huffman@44575
   359
      proof (clarify)
huffman@44575
   360
        fix a b assume "a \<in> A" and "b \<in> B"
huffman@44575
   361
        hence "dist a (fst x) < r" and "dist b (snd x) < s"
huffman@44575
   362
          unfolding A_def B_def by (simp_all add: dist_commute)
huffman@44575
   363
        hence "dist (a, b) x < e"
wenzelm@60500
   364
          unfolding dist_prod_def \<open>e = sqrt (r\<^sup>2 + s\<^sup>2)\<close>
huffman@44575
   365
          by (simp add: add_strict_mono power_strict_mono)
huffman@44575
   366
        thus "(a, b) \<in> S"
huffman@44575
   367
          by (simp add: S)
huffman@44575
   368
      qed
huffman@44575
   369
      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
   370
    qed
huffman@31563
   371
  qed
hoelzl@62101
   372
  show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
hoelzl@62101
   373
    unfolding * eventually_uniformity_metric
hoelzl@62101
   374
    by (simp del: split_paired_All add: dist_prod_def dist_commute)
huffman@31339
   375
qed
huffman@31339
   376
huffman@31339
   377
end
huffman@31339
   378
haftmann@54890
   379
declare [[code abort: "dist::('a::metric_space*'b::metric_space)\<Rightarrow>('a*'b) \<Rightarrow> real"]]
immler@54779
   380
huffman@31405
   381
lemma Cauchy_fst: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. fst (X n))"
huffman@53930
   382
  unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_fst_le])
huffman@31405
   383
huffman@31405
   384
lemma Cauchy_snd: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. snd (X n))"
huffman@53930
   385
  unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_snd_le])
huffman@31405
   386
huffman@31405
   387
lemma Cauchy_Pair:
huffman@31405
   388
  assumes "Cauchy X" and "Cauchy Y"
huffman@31405
   389
  shows "Cauchy (\<lambda>n. (X n, Y n))"
huffman@31405
   390
proof (rule metric_CauchyI)
huffman@31405
   391
  fix r :: real assume "0 < r"
nipkow@56541
   392
  hence "0 < r / sqrt 2" (is "0 < ?s") by simp
huffman@31405
   393
  obtain M where M: "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < ?s"
wenzelm@60500
   394
    using metric_CauchyD [OF \<open>Cauchy X\<close> \<open>0 < ?s\<close>] ..
huffman@31405
   395
  obtain N where N: "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (Y m) (Y n) < ?s"
wenzelm@60500
   396
    using metric_CauchyD [OF \<open>Cauchy Y\<close> \<open>0 < ?s\<close>] ..
huffman@31405
   397
  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
   398
    using M N by (simp add: real_sqrt_sum_squares_less dist_Pair_Pair)
huffman@31405
   399
  then show "\<exists>n0. \<forall>m\<ge>n0. \<forall>n\<ge>n0. dist (X m, Y m) (X n, Y n) < r" ..
huffman@31405
   400
qed
huffman@31405
   401
wenzelm@60500
   402
subsection \<open>Product is a complete metric space\<close>
huffman@31405
   403
haftmann@37678
   404
instance prod :: (complete_space, complete_space) complete_space
huffman@31405
   405
proof
huffman@31405
   406
  fix X :: "nat \<Rightarrow> 'a \<times> 'b" assume "Cauchy X"
wenzelm@61969
   407
  have 1: "(\<lambda>n. fst (X n)) \<longlonglongrightarrow> lim (\<lambda>n. fst (X n))"
wenzelm@60500
   408
    using Cauchy_fst [OF \<open>Cauchy X\<close>]
huffman@31405
   409
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
wenzelm@61969
   410
  have 2: "(\<lambda>n. snd (X n)) \<longlonglongrightarrow> lim (\<lambda>n. snd (X n))"
wenzelm@60500
   411
    using Cauchy_snd [OF \<open>Cauchy X\<close>]
huffman@31405
   412
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
wenzelm@61969
   413
  have "X \<longlonglongrightarrow> (lim (\<lambda>n. fst (X n)), lim (\<lambda>n. snd (X n)))"
huffman@36660
   414
    using tendsto_Pair [OF 1 2] by simp
huffman@31405
   415
  then show "convergent X"
huffman@31405
   416
    by (rule convergentI)
huffman@31405
   417
qed
huffman@31405
   418
wenzelm@60500
   419
subsection \<open>Product is a normed vector space\<close>
huffman@30019
   420
haftmann@37678
   421
instantiation prod :: (real_normed_vector, real_normed_vector) real_normed_vector
huffman@30019
   422
begin
huffman@30019
   423
immler@54779
   424
definition norm_prod_def[code del]:
wenzelm@53015
   425
  "norm x = sqrt ((norm (fst x))\<^sup>2 + (norm (snd x))\<^sup>2)"
huffman@30019
   426
huffman@30019
   427
definition sgn_prod_def:
huffman@30019
   428
  "sgn (x::'a \<times> 'b) = scaleR (inverse (norm x)) x"
huffman@30019
   429
wenzelm@53015
   430
lemma norm_Pair: "norm (a, b) = sqrt ((norm a)\<^sup>2 + (norm b)\<^sup>2)"
huffman@30019
   431
  unfolding norm_prod_def by simp
huffman@30019
   432
wenzelm@60679
   433
instance
wenzelm@60679
   434
proof
huffman@30019
   435
  fix r :: real and x y :: "'a \<times> 'b"
huffman@30019
   436
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@30019
   437
    unfolding norm_prod_def
huffman@44066
   438
    by (simp add: prod_eq_iff)
huffman@30019
   439
  show "norm (x + y) \<le> norm x + norm y"
huffman@30019
   440
    unfolding norm_prod_def
huffman@30019
   441
    apply (rule order_trans [OF _ real_sqrt_sum_squares_triangle_ineq])
huffman@30019
   442
    apply (simp add: add_mono power_mono norm_triangle_ineq)
huffman@30019
   443
    done
huffman@30019
   444
  show "norm (scaleR r x) = \<bar>r\<bar> * norm x"
huffman@30019
   445
    unfolding norm_prod_def
huffman@31587
   446
    apply (simp add: power_mult_distrib)
webertj@49962
   447
    apply (simp add: distrib_left [symmetric])
huffman@30019
   448
    apply (simp add: real_sqrt_mult_distrib)
huffman@30019
   449
    done
huffman@30019
   450
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@30019
   451
    by (rule sgn_prod_def)
huffman@31290
   452
  show "dist x y = norm (x - y)"
huffman@31339
   453
    unfolding dist_prod_def norm_prod_def
huffman@31339
   454
    by (simp add: dist_norm)
huffman@30019
   455
qed
huffman@30019
   456
huffman@30019
   457
end
huffman@30019
   458
haftmann@54890
   459
declare [[code abort: "norm::('a::real_normed_vector*'b::real_normed_vector) \<Rightarrow> real"]]
immler@54779
   460
haftmann@37678
   461
instance prod :: (banach, banach) banach ..
huffman@31405
   462
wenzelm@60500
   463
subsubsection \<open>Pair operations are linear\<close>
huffman@30019
   464
huffman@44282
   465
lemma bounded_linear_fst: "bounded_linear fst"
huffman@44127
   466
  using fst_add fst_scaleR
huffman@44127
   467
  by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
huffman@30019
   468
huffman@44282
   469
lemma bounded_linear_snd: "bounded_linear snd"
huffman@44127
   470
  using snd_add snd_scaleR
huffman@44127
   471
  by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
huffman@30019
   472
immler@61915
   473
lemmas bounded_linear_fst_comp = bounded_linear_fst[THEN bounded_linear_compose]
immler@61915
   474
immler@61915
   475
lemmas bounded_linear_snd_comp = bounded_linear_snd[THEN bounded_linear_compose]
immler@61915
   476
huffman@30019
   477
lemma bounded_linear_Pair:
huffman@30019
   478
  assumes f: "bounded_linear f"
huffman@30019
   479
  assumes g: "bounded_linear g"
huffman@30019
   480
  shows "bounded_linear (\<lambda>x. (f x, g x))"
huffman@30019
   481
proof
huffman@30019
   482
  interpret f: bounded_linear f by fact
huffman@30019
   483
  interpret g: bounded_linear g by fact
huffman@30019
   484
  fix x y and r :: real
huffman@30019
   485
  show "(f (x + y), g (x + y)) = (f x, g x) + (f y, g y)"
huffman@30019
   486
    by (simp add: f.add g.add)
huffman@30019
   487
  show "(f (r *\<^sub>R x), g (r *\<^sub>R x)) = r *\<^sub>R (f x, g x)"
huffman@30019
   488
    by (simp add: f.scaleR g.scaleR)
huffman@30019
   489
  obtain Kf where "0 < Kf" and norm_f: "\<And>x. norm (f x) \<le> norm x * Kf"
huffman@30019
   490
    using f.pos_bounded by fast
huffman@30019
   491
  obtain Kg where "0 < Kg" and norm_g: "\<And>x. norm (g x) \<le> norm x * Kg"
huffman@30019
   492
    using g.pos_bounded by fast
huffman@30019
   493
  have "\<forall>x. norm (f x, g x) \<le> norm x * (Kf + Kg)"
huffman@30019
   494
    apply (rule allI)
huffman@30019
   495
    apply (simp add: norm_Pair)
huffman@30019
   496
    apply (rule order_trans [OF sqrt_add_le_add_sqrt], simp, simp)
webertj@49962
   497
    apply (simp add: distrib_left)
huffman@30019
   498
    apply (rule add_mono [OF norm_f norm_g])
huffman@30019
   499
    done
huffman@30019
   500
  then show "\<exists>K. \<forall>x. norm (f x, g x) \<le> norm x * K" ..
huffman@30019
   501
qed
huffman@30019
   502
wenzelm@60500
   503
subsubsection \<open>Frechet derivatives involving pairs\<close>
huffman@30019
   504
hoelzl@56381
   505
lemma has_derivative_Pair [derivative_intros]:
hoelzl@56181
   506
  assumes f: "(f has_derivative f') (at x within s)" and g: "(g has_derivative g') (at x within s)"
hoelzl@56181
   507
  shows "((\<lambda>x. (f x, g x)) has_derivative (\<lambda>h. (f' h, g' h))) (at x within s)"
hoelzl@56181
   508
proof (rule has_derivativeI_sandwich[of 1])
huffman@44575
   509
  show "bounded_linear (\<lambda>h. (f' h, g' h))"
hoelzl@56181
   510
    using f g by (intro bounded_linear_Pair has_derivative_bounded_linear)
hoelzl@51642
   511
  let ?Rf = "\<lambda>y. f y - f x - f' (y - x)"
hoelzl@51642
   512
  let ?Rg = "\<lambda>y. g y - g x - g' (y - x)"
hoelzl@51642
   513
  let ?R = "\<lambda>y. ((f y, g y) - (f x, g x) - (f' (y - x), g' (y - x)))"
hoelzl@51642
   514
wenzelm@61973
   515
  show "((\<lambda>y. norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)) \<longlongrightarrow> 0) (at x within s)"
hoelzl@56181
   516
    using f g by (intro tendsto_add_zero) (auto simp: has_derivative_iff_norm)
hoelzl@51642
   517
hoelzl@51642
   518
  fix y :: 'a assume "y \<noteq> x"
hoelzl@51642
   519
  show "norm (?R y) / norm (y - x) \<le> norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)"
hoelzl@51642
   520
    unfolding add_divide_distrib [symmetric]
hoelzl@51642
   521
    by (simp add: norm_Pair divide_right_mono order_trans [OF sqrt_add_le_add_sqrt])
hoelzl@51642
   522
qed simp
hoelzl@51642
   523
hoelzl@56381
   524
lemmas has_derivative_fst [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_fst]
hoelzl@56381
   525
lemmas has_derivative_snd [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_snd]
hoelzl@51642
   526
hoelzl@56381
   527
lemma has_derivative_split [derivative_intros]:
hoelzl@51642
   528
  "((\<lambda>p. f (fst p) (snd p)) has_derivative f') F \<Longrightarrow> ((\<lambda>(a, b). f a b) has_derivative f') F"
hoelzl@51642
   529
  unfolding split_beta' .
huffman@44575
   530
wenzelm@60500
   531
subsection \<open>Product is an inner product space\<close>
huffman@44575
   532
huffman@44575
   533
instantiation prod :: (real_inner, real_inner) real_inner
huffman@44575
   534
begin
huffman@44575
   535
huffman@44575
   536
definition inner_prod_def:
huffman@44575
   537
  "inner x y = inner (fst x) (fst y) + inner (snd x) (snd y)"
huffman@44575
   538
huffman@44575
   539
lemma inner_Pair [simp]: "inner (a, b) (c, d) = inner a c + inner b d"
huffman@44575
   540
  unfolding inner_prod_def by simp
huffman@44575
   541
wenzelm@60679
   542
instance
wenzelm@60679
   543
proof
huffman@44575
   544
  fix r :: real
huffman@44575
   545
  fix x y z :: "'a::real_inner \<times> 'b::real_inner"
huffman@44575
   546
  show "inner x y = inner y x"
huffman@44575
   547
    unfolding inner_prod_def
huffman@44575
   548
    by (simp add: inner_commute)
huffman@44575
   549
  show "inner (x + y) z = inner x z + inner y z"
huffman@44575
   550
    unfolding inner_prod_def
huffman@44575
   551
    by (simp add: inner_add_left)
huffman@44575
   552
  show "inner (scaleR r x) y = r * inner x y"
huffman@44575
   553
    unfolding inner_prod_def
webertj@49962
   554
    by (simp add: distrib_left)
huffman@44575
   555
  show "0 \<le> inner x x"
huffman@44575
   556
    unfolding inner_prod_def
huffman@44575
   557
    by (intro add_nonneg_nonneg inner_ge_zero)
huffman@44575
   558
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@44575
   559
    unfolding inner_prod_def prod_eq_iff
huffman@44575
   560
    by (simp add: add_nonneg_eq_0_iff)
huffman@44575
   561
  show "norm x = sqrt (inner x x)"
huffman@44575
   562
    unfolding norm_prod_def inner_prod_def
huffman@44575
   563
    by (simp add: power2_norm_eq_inner)
huffman@44575
   564
qed
huffman@30019
   565
huffman@30019
   566
end
huffman@44575
   567
hoelzl@59425
   568
lemma inner_Pair_0: "inner x (0, b) = inner (snd x) b" "inner x (a, 0) = inner (fst x) a"
hoelzl@59425
   569
    by (cases x, simp)+
hoelzl@59425
   570
hoelzl@62102
   571
lemma
lp15@60615
   572
  fixes x :: "'a::real_normed_vector"
hoelzl@62102
   573
  shows norm_Pair1 [simp]: "norm (0,x) = norm x"
lp15@60615
   574
    and norm_Pair2 [simp]: "norm (x,0) = norm x"
lp15@60615
   575
by (auto simp: norm_Pair)
lp15@60615
   576
paulson@62131
   577
lemma norm_commute: "norm (x,y) = norm (y,x)"
paulson@62131
   578
  by (simp add: norm_Pair)
paulson@62131
   579
paulson@62131
   580
lemma norm_fst_le: "norm x \<le> norm (x,y)"
paulson@62131
   581
  by (metis dist_fst_le fst_conv fst_zero norm_conv_dist)
paulson@62131
   582
paulson@62131
   583
lemma norm_snd_le: "norm y \<le> norm (x,y)"
paulson@62131
   584
  by (metis dist_snd_le snd_conv snd_zero norm_conv_dist)
hoelzl@59425
   585
huffman@44575
   586
end