src/HOL/Library/Product_Vector.thy
author wenzelm
Mon Dec 28 17:43:30 2015 +0100 (2015-12-28)
changeset 61952 546958347e05
parent 61915 e9812a95d108
child 61969 e01015e49041
permissions -rw-r--r--
prefer symbols for "Union", "Inter";
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]:
huffman@44575
   155
  assumes "(f ---> a) F"
huffman@44575
   156
  shows "((\<lambda>x. fst (f x)) ---> 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]:
huffman@44575
   168
  assumes "(f ---> a) F"
huffman@44575
   169
  shows "((\<lambda>x. snd (f x)) ---> 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]:
huffman@44575
   181
  assumes "(f ---> a) F" and "(g ---> b) F"
huffman@44575
   182
  shows "((\<lambda>x. (f x, g x)) ---> (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@60500
   188
    using \<open>(f ---> 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@60500
   192
    using \<open>(g ---> 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
lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"
huffman@44214
   233
  by (induct x) simp (* TODO: move elsewhere *)
huffman@44214
   234
huffman@44214
   235
instance prod :: (t0_space, t0_space) t0_space
huffman@44214
   236
proof
huffman@44214
   237
  fix x y :: "'a \<times> 'b" assume "x \<noteq> y"
huffman@44214
   238
  hence "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
huffman@44214
   239
    by (simp add: prod_eq_iff)
huffman@44214
   240
  thus "\<exists>U. open U \<and> (x \<in> U) \<noteq> (y \<in> U)"
huffman@53930
   241
    by (fast dest: t0_space elim: open_vimage_fst open_vimage_snd)
huffman@44214
   242
qed
huffman@44214
   243
huffman@44214
   244
instance prod :: (t1_space, t1_space) t1_space
huffman@44214
   245
proof
huffman@44214
   246
  fix x y :: "'a \<times> 'b" assume "x \<noteq> y"
huffman@44214
   247
  hence "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
huffman@44214
   248
    by (simp add: prod_eq_iff)
huffman@44214
   249
  thus "\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
huffman@53930
   250
    by (fast dest: t1_space elim: open_vimage_fst open_vimage_snd)
huffman@44214
   251
qed
huffman@44214
   252
huffman@44214
   253
instance prod :: (t2_space, t2_space) t2_space
huffman@44214
   254
proof
huffman@44214
   255
  fix x y :: "'a \<times> 'b" assume "x \<noteq> y"
huffman@44214
   256
  hence "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
huffman@44214
   257
    by (simp add: prod_eq_iff)
huffman@44214
   258
  thus "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
huffman@53930
   259
    by (fast dest: hausdorff elim: open_vimage_fst open_vimage_snd)
huffman@44214
   260
qed
huffman@44214
   261
lp15@60615
   262
lemma isCont_swap[continuous_intros]: "isCont prod.swap a"
lp15@60615
   263
  using continuous_on_eq_continuous_within continuous_on_swap by blast
lp15@60615
   264
wenzelm@60500
   265
subsection \<open>Product is a metric space\<close>
huffman@31339
   266
haftmann@37678
   267
instantiation prod :: (metric_space, metric_space) metric_space
huffman@31339
   268
begin
huffman@31339
   269
immler@54779
   270
definition dist_prod_def[code del]:
wenzelm@53015
   271
  "dist x y = sqrt ((dist (fst x) (fst y))\<^sup>2 + (dist (snd x) (snd y))\<^sup>2)"
huffman@31339
   272
wenzelm@53015
   273
lemma dist_Pair_Pair: "dist (a, b) (c, d) = sqrt ((dist a c)\<^sup>2 + (dist b d)\<^sup>2)"
huffman@31339
   274
  unfolding dist_prod_def by simp
huffman@31339
   275
huffman@36332
   276
lemma dist_fst_le: "dist (fst x) (fst y) \<le> dist x y"
huffman@53930
   277
  unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge1)
huffman@36332
   278
huffman@36332
   279
lemma dist_snd_le: "dist (snd x) (snd y) \<le> dist x y"
huffman@53930
   280
  unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge2)
huffman@36332
   281
wenzelm@60679
   282
instance
wenzelm@60679
   283
proof
huffman@31339
   284
  fix x y :: "'a \<times> 'b"
huffman@31339
   285
  show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@44066
   286
    unfolding dist_prod_def prod_eq_iff by simp
huffman@31339
   287
next
huffman@31339
   288
  fix x y z :: "'a \<times> 'b"
huffman@31339
   289
  show "dist x y \<le> dist x z + dist y z"
huffman@31339
   290
    unfolding dist_prod_def
huffman@31563
   291
    by (intro order_trans [OF _ real_sqrt_sum_squares_triangle_ineq]
huffman@31563
   292
        real_sqrt_le_mono add_mono power_mono dist_triangle2 zero_le_dist)
huffman@31415
   293
next
huffman@31492
   294
  fix S :: "('a \<times> 'b) set"
huffman@31492
   295
  show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
huffman@31563
   296
  proof
huffman@36332
   297
    assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@36332
   298
    proof
huffman@36332
   299
      fix x assume "x \<in> S"
huffman@36332
   300
      obtain A B where "open A" "open B" "x \<in> A \<times> B" "A \<times> B \<subseteq> S"
wenzelm@60500
   301
        using \<open>open S\<close> and \<open>x \<in> S\<close> by (rule open_prod_elim)
huffman@36332
   302
      obtain r where r: "0 < r" "\<forall>y. dist y (fst x) < r \<longrightarrow> y \<in> A"
wenzelm@60500
   303
        using \<open>open A\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
huffman@36332
   304
      obtain s where s: "0 < s" "\<forall>y. dist y (snd x) < s \<longrightarrow> y \<in> B"
wenzelm@60500
   305
        using \<open>open B\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
huffman@36332
   306
      let ?e = "min r s"
huffman@36332
   307
      have "0 < ?e \<and> (\<forall>y. dist y x < ?e \<longrightarrow> y \<in> S)"
huffman@36332
   308
      proof (intro allI impI conjI)
huffman@36332
   309
        show "0 < min r s" by (simp add: r(1) s(1))
huffman@36332
   310
      next
huffman@36332
   311
        fix y assume "dist y x < min r s"
huffman@36332
   312
        hence "dist y x < r" and "dist y x < s"
huffman@36332
   313
          by simp_all
huffman@36332
   314
        hence "dist (fst y) (fst x) < r" and "dist (snd y) (snd x) < s"
huffman@36332
   315
          by (auto intro: le_less_trans dist_fst_le dist_snd_le)
huffman@36332
   316
        hence "fst y \<in> A" and "snd y \<in> B"
huffman@36332
   317
          by (simp_all add: r(2) s(2))
huffman@36332
   318
        hence "y \<in> A \<times> B" by (induct y, simp)
wenzelm@60500
   319
        with \<open>A \<times> B \<subseteq> S\<close> show "y \<in> S" ..
huffman@36332
   320
      qed
huffman@36332
   321
      thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
huffman@36332
   322
    qed
huffman@31563
   323
  next
huffman@44575
   324
    assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
huffman@44575
   325
    proof (rule open_prod_intro)
huffman@44575
   326
      fix x assume "x \<in> S"
huffman@44575
   327
      then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
huffman@44575
   328
        using * by fast
huffman@44575
   329
      def r \<equiv> "e / sqrt 2" and s \<equiv> "e / sqrt 2"
wenzelm@60500
   330
      from \<open>0 < e\<close> have "0 < r" and "0 < s"
nipkow@56541
   331
        unfolding r_def s_def by simp_all
wenzelm@60500
   332
      from \<open>0 < e\<close> have "e = sqrt (r\<^sup>2 + s\<^sup>2)"
huffman@44575
   333
        unfolding r_def s_def by (simp add: power_divide)
huffman@44575
   334
      def A \<equiv> "{y. dist (fst x) y < r}" and B \<equiv> "{y. dist (snd x) y < s}"
huffman@44575
   335
      have "open A" and "open B"
huffman@44575
   336
        unfolding A_def B_def by (simp_all add: open_ball)
huffman@44575
   337
      moreover have "x \<in> A \<times> B"
huffman@44575
   338
        unfolding A_def B_def mem_Times_iff
wenzelm@60500
   339
        using \<open>0 < r\<close> and \<open>0 < s\<close> by simp
huffman@44575
   340
      moreover have "A \<times> B \<subseteq> S"
huffman@44575
   341
      proof (clarify)
huffman@44575
   342
        fix a b assume "a \<in> A" and "b \<in> B"
huffman@44575
   343
        hence "dist a (fst x) < r" and "dist b (snd x) < s"
huffman@44575
   344
          unfolding A_def B_def by (simp_all add: dist_commute)
huffman@44575
   345
        hence "dist (a, b) x < e"
wenzelm@60500
   346
          unfolding dist_prod_def \<open>e = sqrt (r\<^sup>2 + s\<^sup>2)\<close>
huffman@44575
   347
          by (simp add: add_strict_mono power_strict_mono)
huffman@44575
   348
        thus "(a, b) \<in> S"
huffman@44575
   349
          by (simp add: S)
huffman@44575
   350
      qed
huffman@44575
   351
      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
   352
    qed
huffman@31563
   353
  qed
huffman@31339
   354
qed
huffman@31339
   355
huffman@31339
   356
end
huffman@31339
   357
haftmann@54890
   358
declare [[code abort: "dist::('a::metric_space*'b::metric_space)\<Rightarrow>('a*'b) \<Rightarrow> real"]]
immler@54779
   359
huffman@31405
   360
lemma Cauchy_fst: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. fst (X n))"
huffman@53930
   361
  unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_fst_le])
huffman@31405
   362
huffman@31405
   363
lemma Cauchy_snd: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. snd (X n))"
huffman@53930
   364
  unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_snd_le])
huffman@31405
   365
huffman@31405
   366
lemma Cauchy_Pair:
huffman@31405
   367
  assumes "Cauchy X" and "Cauchy Y"
huffman@31405
   368
  shows "Cauchy (\<lambda>n. (X n, Y n))"
huffman@31405
   369
proof (rule metric_CauchyI)
huffman@31405
   370
  fix r :: real assume "0 < r"
nipkow@56541
   371
  hence "0 < r / sqrt 2" (is "0 < ?s") by simp
huffman@31405
   372
  obtain M where M: "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < ?s"
wenzelm@60500
   373
    using metric_CauchyD [OF \<open>Cauchy X\<close> \<open>0 < ?s\<close>] ..
huffman@31405
   374
  obtain N where N: "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (Y m) (Y n) < ?s"
wenzelm@60500
   375
    using metric_CauchyD [OF \<open>Cauchy Y\<close> \<open>0 < ?s\<close>] ..
huffman@31405
   376
  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
   377
    using M N by (simp add: real_sqrt_sum_squares_less dist_Pair_Pair)
huffman@31405
   378
  then show "\<exists>n0. \<forall>m\<ge>n0. \<forall>n\<ge>n0. dist (X m, Y m) (X n, Y n) < r" ..
huffman@31405
   379
qed
huffman@31405
   380
wenzelm@60500
   381
subsection \<open>Product is a complete metric space\<close>
huffman@31405
   382
haftmann@37678
   383
instance prod :: (complete_space, complete_space) complete_space
huffman@31405
   384
proof
huffman@31405
   385
  fix X :: "nat \<Rightarrow> 'a \<times> 'b" assume "Cauchy X"
huffman@31405
   386
  have 1: "(\<lambda>n. fst (X n)) ----> lim (\<lambda>n. fst (X n))"
wenzelm@60500
   387
    using Cauchy_fst [OF \<open>Cauchy X\<close>]
huffman@31405
   388
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
huffman@31405
   389
  have 2: "(\<lambda>n. snd (X n)) ----> lim (\<lambda>n. snd (X n))"
wenzelm@60500
   390
    using Cauchy_snd [OF \<open>Cauchy X\<close>]
huffman@31405
   391
    by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
huffman@31405
   392
  have "X ----> (lim (\<lambda>n. fst (X n)), lim (\<lambda>n. snd (X n)))"
huffman@36660
   393
    using tendsto_Pair [OF 1 2] by simp
huffman@31405
   394
  then show "convergent X"
huffman@31405
   395
    by (rule convergentI)
huffman@31405
   396
qed
huffman@31405
   397
wenzelm@60500
   398
subsection \<open>Product is a normed vector space\<close>
huffman@30019
   399
haftmann@37678
   400
instantiation prod :: (real_normed_vector, real_normed_vector) real_normed_vector
huffman@30019
   401
begin
huffman@30019
   402
immler@54779
   403
definition norm_prod_def[code del]:
wenzelm@53015
   404
  "norm x = sqrt ((norm (fst x))\<^sup>2 + (norm (snd x))\<^sup>2)"
huffman@30019
   405
huffman@30019
   406
definition sgn_prod_def:
huffman@30019
   407
  "sgn (x::'a \<times> 'b) = scaleR (inverse (norm x)) x"
huffman@30019
   408
wenzelm@53015
   409
lemma norm_Pair: "norm (a, b) = sqrt ((norm a)\<^sup>2 + (norm b)\<^sup>2)"
huffman@30019
   410
  unfolding norm_prod_def by simp
huffman@30019
   411
wenzelm@60679
   412
instance
wenzelm@60679
   413
proof
huffman@30019
   414
  fix r :: real and x y :: "'a \<times> 'b"
huffman@30019
   415
  show "norm x = 0 \<longleftrightarrow> x = 0"
huffman@30019
   416
    unfolding norm_prod_def
huffman@44066
   417
    by (simp add: prod_eq_iff)
huffman@30019
   418
  show "norm (x + y) \<le> norm x + norm y"
huffman@30019
   419
    unfolding norm_prod_def
huffman@30019
   420
    apply (rule order_trans [OF _ real_sqrt_sum_squares_triangle_ineq])
huffman@30019
   421
    apply (simp add: add_mono power_mono norm_triangle_ineq)
huffman@30019
   422
    done
huffman@30019
   423
  show "norm (scaleR r x) = \<bar>r\<bar> * norm x"
huffman@30019
   424
    unfolding norm_prod_def
huffman@31587
   425
    apply (simp add: power_mult_distrib)
webertj@49962
   426
    apply (simp add: distrib_left [symmetric])
huffman@30019
   427
    apply (simp add: real_sqrt_mult_distrib)
huffman@30019
   428
    done
huffman@30019
   429
  show "sgn x = scaleR (inverse (norm x)) x"
huffman@30019
   430
    by (rule sgn_prod_def)
huffman@31290
   431
  show "dist x y = norm (x - y)"
huffman@31339
   432
    unfolding dist_prod_def norm_prod_def
huffman@31339
   433
    by (simp add: dist_norm)
huffman@30019
   434
qed
huffman@30019
   435
huffman@30019
   436
end
huffman@30019
   437
haftmann@54890
   438
declare [[code abort: "norm::('a::real_normed_vector*'b::real_normed_vector) \<Rightarrow> real"]]
immler@54779
   439
haftmann@37678
   440
instance prod :: (banach, banach) banach ..
huffman@31405
   441
wenzelm@60500
   442
subsubsection \<open>Pair operations are linear\<close>
huffman@30019
   443
huffman@44282
   444
lemma bounded_linear_fst: "bounded_linear fst"
huffman@44127
   445
  using fst_add fst_scaleR
huffman@44127
   446
  by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
huffman@30019
   447
huffman@44282
   448
lemma bounded_linear_snd: "bounded_linear snd"
huffman@44127
   449
  using snd_add snd_scaleR
huffman@44127
   450
  by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
huffman@30019
   451
immler@61915
   452
lemmas bounded_linear_fst_comp = bounded_linear_fst[THEN bounded_linear_compose]
immler@61915
   453
immler@61915
   454
lemmas bounded_linear_snd_comp = bounded_linear_snd[THEN bounded_linear_compose]
immler@61915
   455
huffman@30019
   456
lemma bounded_linear_Pair:
huffman@30019
   457
  assumes f: "bounded_linear f"
huffman@30019
   458
  assumes g: "bounded_linear g"
huffman@30019
   459
  shows "bounded_linear (\<lambda>x. (f x, g x))"
huffman@30019
   460
proof
huffman@30019
   461
  interpret f: bounded_linear f by fact
huffman@30019
   462
  interpret g: bounded_linear g by fact
huffman@30019
   463
  fix x y and r :: real
huffman@30019
   464
  show "(f (x + y), g (x + y)) = (f x, g x) + (f y, g y)"
huffman@30019
   465
    by (simp add: f.add g.add)
huffman@30019
   466
  show "(f (r *\<^sub>R x), g (r *\<^sub>R x)) = r *\<^sub>R (f x, g x)"
huffman@30019
   467
    by (simp add: f.scaleR g.scaleR)
huffman@30019
   468
  obtain Kf where "0 < Kf" and norm_f: "\<And>x. norm (f x) \<le> norm x * Kf"
huffman@30019
   469
    using f.pos_bounded by fast
huffman@30019
   470
  obtain Kg where "0 < Kg" and norm_g: "\<And>x. norm (g x) \<le> norm x * Kg"
huffman@30019
   471
    using g.pos_bounded by fast
huffman@30019
   472
  have "\<forall>x. norm (f x, g x) \<le> norm x * (Kf + Kg)"
huffman@30019
   473
    apply (rule allI)
huffman@30019
   474
    apply (simp add: norm_Pair)
huffman@30019
   475
    apply (rule order_trans [OF sqrt_add_le_add_sqrt], simp, simp)
webertj@49962
   476
    apply (simp add: distrib_left)
huffman@30019
   477
    apply (rule add_mono [OF norm_f norm_g])
huffman@30019
   478
    done
huffman@30019
   479
  then show "\<exists>K. \<forall>x. norm (f x, g x) \<le> norm x * K" ..
huffman@30019
   480
qed
huffman@30019
   481
wenzelm@60500
   482
subsubsection \<open>Frechet derivatives involving pairs\<close>
huffman@30019
   483
hoelzl@56381
   484
lemma has_derivative_Pair [derivative_intros]:
hoelzl@56181
   485
  assumes f: "(f has_derivative f') (at x within s)" and g: "(g has_derivative g') (at x within s)"
hoelzl@56181
   486
  shows "((\<lambda>x. (f x, g x)) has_derivative (\<lambda>h. (f' h, g' h))) (at x within s)"
hoelzl@56181
   487
proof (rule has_derivativeI_sandwich[of 1])
huffman@44575
   488
  show "bounded_linear (\<lambda>h. (f' h, g' h))"
hoelzl@56181
   489
    using f g by (intro bounded_linear_Pair has_derivative_bounded_linear)
hoelzl@51642
   490
  let ?Rf = "\<lambda>y. f y - f x - f' (y - x)"
hoelzl@51642
   491
  let ?Rg = "\<lambda>y. g y - g x - g' (y - x)"
hoelzl@51642
   492
  let ?R = "\<lambda>y. ((f y, g y) - (f x, g x) - (f' (y - x), g' (y - x)))"
hoelzl@51642
   493
hoelzl@51642
   494
  show "((\<lambda>y. norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)) ---> 0) (at x within s)"
hoelzl@56181
   495
    using f g by (intro tendsto_add_zero) (auto simp: has_derivative_iff_norm)
hoelzl@51642
   496
hoelzl@51642
   497
  fix y :: 'a assume "y \<noteq> x"
hoelzl@51642
   498
  show "norm (?R y) / norm (y - x) \<le> norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)"
hoelzl@51642
   499
    unfolding add_divide_distrib [symmetric]
hoelzl@51642
   500
    by (simp add: norm_Pair divide_right_mono order_trans [OF sqrt_add_le_add_sqrt])
hoelzl@51642
   501
qed simp
hoelzl@51642
   502
hoelzl@56381
   503
lemmas has_derivative_fst [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_fst]
hoelzl@56381
   504
lemmas has_derivative_snd [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_snd]
hoelzl@51642
   505
hoelzl@56381
   506
lemma has_derivative_split [derivative_intros]:
hoelzl@51642
   507
  "((\<lambda>p. f (fst p) (snd p)) has_derivative f') F \<Longrightarrow> ((\<lambda>(a, b). f a b) has_derivative f') F"
hoelzl@51642
   508
  unfolding split_beta' .
huffman@44575
   509
wenzelm@60500
   510
subsection \<open>Product is an inner product space\<close>
huffman@44575
   511
huffman@44575
   512
instantiation prod :: (real_inner, real_inner) real_inner
huffman@44575
   513
begin
huffman@44575
   514
huffman@44575
   515
definition inner_prod_def:
huffman@44575
   516
  "inner x y = inner (fst x) (fst y) + inner (snd x) (snd y)"
huffman@44575
   517
huffman@44575
   518
lemma inner_Pair [simp]: "inner (a, b) (c, d) = inner a c + inner b d"
huffman@44575
   519
  unfolding inner_prod_def by simp
huffman@44575
   520
wenzelm@60679
   521
instance
wenzelm@60679
   522
proof
huffman@44575
   523
  fix r :: real
huffman@44575
   524
  fix x y z :: "'a::real_inner \<times> 'b::real_inner"
huffman@44575
   525
  show "inner x y = inner y x"
huffman@44575
   526
    unfolding inner_prod_def
huffman@44575
   527
    by (simp add: inner_commute)
huffman@44575
   528
  show "inner (x + y) z = inner x z + inner y z"
huffman@44575
   529
    unfolding inner_prod_def
huffman@44575
   530
    by (simp add: inner_add_left)
huffman@44575
   531
  show "inner (scaleR r x) y = r * inner x y"
huffman@44575
   532
    unfolding inner_prod_def
webertj@49962
   533
    by (simp add: distrib_left)
huffman@44575
   534
  show "0 \<le> inner x x"
huffman@44575
   535
    unfolding inner_prod_def
huffman@44575
   536
    by (intro add_nonneg_nonneg inner_ge_zero)
huffman@44575
   537
  show "inner x x = 0 \<longleftrightarrow> x = 0"
huffman@44575
   538
    unfolding inner_prod_def prod_eq_iff
huffman@44575
   539
    by (simp add: add_nonneg_eq_0_iff)
huffman@44575
   540
  show "norm x = sqrt (inner x x)"
huffman@44575
   541
    unfolding norm_prod_def inner_prod_def
huffman@44575
   542
    by (simp add: power2_norm_eq_inner)
huffman@44575
   543
qed
huffman@30019
   544
huffman@30019
   545
end
huffman@44575
   546
hoelzl@59425
   547
lemma inner_Pair_0: "inner x (0, b) = inner (snd x) b" "inner x (a, 0) = inner (fst x) a"
hoelzl@59425
   548
    by (cases x, simp)+
hoelzl@59425
   549
lp15@60615
   550
lemma 
lp15@60615
   551
  fixes x :: "'a::real_normed_vector"
lp15@60615
   552
  shows norm_Pair1 [simp]: "norm (0,x) = norm x" 
lp15@60615
   553
    and norm_Pair2 [simp]: "norm (x,0) = norm x"
lp15@60615
   554
by (auto simp: norm_Pair)
lp15@60615
   555
hoelzl@59425
   556
huffman@44575
   557
end